annotate lisp/progmodes/cc-cmds.el @ 20912:a0748eef9a76

(c-beginning-of-statement) (c-end-of-statement): Do not move by sentence in strings. (c-beginning-of-statement): Major rewrite. (c-beginning-of-defun, c-indent-defun): Use (c-point 'bod) instead of beginning-of-defun directly. (c-beginning-of-defun, c-end-of-defun): New commands. (c-beginning-of-statement): When moving forward by sentences, because we're either inside or at the start of a comment, be sure to limit movement to only within the extent of the comment. (c-electric-colon): Don't insert newlines before or after scope operators, regardless of the value of c-hanging-colons. (c-electric-brace): namespace-open and namespace-close braces can hang. (c-comment-line-break-function): When breaking a line-oriented comment, copy the comment leader from the previous line instead of hardcoding it to "// ". This ensures that whitespace between the slashes and the text is preserved. (c-electric-pound, c-electric-brace) (c-electric-slash, c-electric-star, c-electric-semi&comma) (c-electric-colon, c-electric-lt-gt, c-scope-operator) (c-electric-backspace, c-electric-delete) (c-indent-command, c-indent-exp, c-indent-defun) (c-backslash-region, c-fill-paragraph): Add "*" to interactive spec. (c-fill-paragraph): regexp-quote the fill-prefix when search forward for the end of line oriented comments. (c-backslash-region): Do not preserve the zmacs region (XEmacs).
author Richard M. Stallman <rms@gnu.org>
date Tue, 17 Feb 1998 07:04:48 +0000
parents 69ce7b7acfd5
children d67e858e738b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 ;;; cc-cmds.el --- user level commands for CC Mode
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 ;; Authors: 1992-1997 Barry A. Warsaw
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 ;; 1987 Dave Detlefs and Stewart Clamen
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 ;; 1985 Richard M. Stallman
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 ;; Maintainer: cc-mode-help@python.org
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9 ;; Created: 22-Apr-1997 (split from cc-mode.el)
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
10 ;; Version: See cc-mode.el
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 ;; Keywords: c languages oop
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; This file is part of GNU Emacs.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; it under the terms of the GNU General Public License as published by
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; the Free Software Foundation; either version 2, or (at your option)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; any later version.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20 ;; GNU Emacs is distributed in the hope that it will be useful,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 ;; GNU General Public License for more details.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25 ;; You should have received a copy of the GNU General Public License
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 ;; Boston, MA 02111-1307, USA.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29
18843
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
30 (eval-when-compile
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
31 (require 'cc-defs))
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
32
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 (defun c-calculate-state (arg prevstate)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 ;; arg is nil or zero, toggle the state. If arg is negative, turn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 ;; the state off, and if arg is positive, turn the state on
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38 (if (or (not arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 (zerop (setq arg (prefix-numeric-value arg))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 (not prevstate)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41 (> arg 0)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 ;; Auto-newline and hungry-delete
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 (defun c-toggle-auto-state (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 "Toggle auto-newline feature.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 Optional numeric ARG, if supplied turns on auto-newline when positive,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 turns it off when negative, and just toggles it when zero.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 When the auto-newline feature is enabled (as evidenced by the `/a' or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 `/ah' on the modeline after the mode name) newlines are automatically
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 inserted after special characters such as brace, comma, semi-colon,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 and colon."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 (interactive "P")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 (c-update-modeline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 (defun c-toggle-hungry-state (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 "Toggle hungry-delete-key feature.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 Optional numeric ARG, if supplied turns on hungry-delete when positive,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 turns it off when negative, and just toggles it when zero.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 When the hungry-delete-key feature is enabled (as evidenced by the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 `/h' or `/ah' on the modeline after the mode name) the delete key
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 gobbles all preceding whitespace in one fell swoop."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 (interactive "P")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 (c-update-modeline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 (defun c-toggle-auto-hungry-state (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 "Toggle auto-newline and hungry-delete-key features.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 Optional numeric ARG, if supplied turns on auto-newline and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 hungry-delete when positive, turns them off when negative, and just
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 toggles them when zero.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 See `c-toggle-auto-state' and `c-toggle-hungry-state' for details."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 (interactive "P")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 (c-update-modeline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85 ;; Electric keys
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 ;; Note: In XEmacs 20.3 the Delete and BackSpace keysyms have been
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 ;; separated and "\177" is no longer an alias for both keys. Also,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 ;; the variable delete-key-deletes-forward controls in which direction
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 ;; the Delete keysym deletes characters. The functions
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 ;; c-electric-delete and c-electric-backspace attempt to deal with
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 ;; this new functionality. For Emacs 19 and XEmacs 19 backwards
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 ;; compatibility, the old behavior has moved to c-electric-backspace
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 ;; and c-backspace-function.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 (defun c-electric-backspace (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 "Deletes preceding character or whitespace.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 \"/ah\" string on the mode line, then all preceding whitespace is
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 consumed. If however an ARG is supplied, or `c-hungry-delete-key' is
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 nil, or point is inside a literal then the function in the variable
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 `c-backspace-function' is called.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 See also \\[c-electric-delete]."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
105 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 (if (or (not c-hungry-delete-key)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 arg
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 (c-in-literal))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 (funcall c-backspace-function (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 (let ((here (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 (skip-chars-backward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 (if (/= (point) here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 (delete-region (point) here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 (funcall c-backspace-function 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 ))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 (defun c-electric-delete (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 "Deletes preceding or following character or whitespace.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 The behavior of this function depends on the variable
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 `delete-key-deletes-forward'. If this variable is nil (or does not
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 exist, as in older Emacsen), then this function behaves identical to
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 \\[c-electric-backspace].
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
125 If `delete-key-deletes-forward' is non-nil and is supported in your
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
126 Emacs, then deletion occurs in the forward direction. So if
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
127 `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
128 \"/ah\" string on the mode line, then all following whitespace is
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
129 consumed. If however an ARG is supplied, or `c-hungry-delete-key' is
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
130 nil, or point is inside a literal then the function in the variable
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
131 `c-delete-function' is called."
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
132 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 (if (and (boundp 'delete-key-deletes-forward)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134 delete-key-deletes-forward)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 (if (or (not c-hungry-delete-key)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 arg
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 (c-in-literal))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 (funcall c-delete-function (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 (let ((here (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 (skip-chars-forward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 (if (/= (point) here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 (delete-region (point) here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 (funcall c-delete-function 1))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 ;; act just like c-electric-backspace
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 (c-electric-backspace arg)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 (defun c-electric-pound (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 "Electric pound (`#') insertion.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 Inserts a `#' character specially depending on the variable
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 point is inside a literal, nothing special happens."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
152 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 (if (or (c-in-literal)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 arg
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 (not (memq 'alignleft c-electric-pound-behavior)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 ;; do nothing special
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 ;; place the pound character at the left edge
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 (let ((pos (- (point-max) (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 (bolp (bolp)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 (delete-horizontal-space)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 (insert-char last-command-char 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 (and (not bolp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 (goto-char (- (point-max) pos)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 )))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 (defun c-electric-brace (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 "Insert a brace.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 If the auto-newline feature is turned on, as evidenced by the \"/a\"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 or \"/ah\" string on the mode line, newlines are inserted before and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 after braces based on the value of `c-hanging-braces-alist'.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 Also, the line is re-indented unless a numeric ARG is supplied, there
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 are non-whitespace characters present on the line after the brace, or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 the brace is inserted inside a literal."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
178 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 (let* ((c-state-cache (c-parse-state))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 (safepos (c-safe-position (point) c-state-cache))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 (literal (c-in-literal safepos)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 ;; if we're in a literal, or we're not at the end of the line, or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 ;; a numeric arg is provided, or auto-newlining is turned off,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 ;; then just insert the character.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 (if (or literal arg
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 ; (not c-auto-newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 (not (looking-at "[ \t]*$")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 (self-insert-command (prefix-numeric-value arg))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
189 (let* ((syms
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
190 ;; This is the list of brace syntactic symbols that can
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
191 ;; hang. If any new ones are added to c-offsets-alist,
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
192 ;; they should be added here as well.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
193 '(class-open class-close defun-open defun-close
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
194 inline-open inline-close
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
195 brace-list-open brace-list-close
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
196 brace-list-intro brace-list-entry
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
197 block-open block-close
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
198 substatement-open statement-case-open
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
199 extern-lang-open extern-lang-close
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
200 namespace-open namespace-close
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
201 ))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 ;; we want to inhibit blinking the paren since this will
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 ;; be most disruptive. we'll blink it ourselves later on
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 (old-blink-paren blink-paren-function)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 blink-paren-function
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 (insertion-point (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 delete-temp-newline
19306
974544be1a17 (c-electric-brace): Make preserve-p nil at BOB.
Richard M. Stallman <rms@gnu.org>
parents: 19297
diff changeset
208 (preserve-p (and (not (bobp))
974544be1a17 (c-electric-brace): Make preserve-p nil at BOB.
Richard M. Stallman <rms@gnu.org>
parents: 19297
diff changeset
209 (eq ?\ (char-syntax (char-before)))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 ;; shut this up too
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 (c-echo-syntactic-information-p nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 (syntax (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 ;; only insert a newline if there is
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 ;; non-whitespace behind us
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 (if (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 (skip-chars-backward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 (not (bolp)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 (progn (newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 (setq delete-temp-newline t)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 ;; state cache doesn't change
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 (c-guess-basic-syntax)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 (newlines (and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 c-auto-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 (or (c-lookup-lists syms syntax c-hanging-braces-alist)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 '(ignore before after)))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 ;; If syntax is a function symbol, then call it using the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 ;; defined semantics.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 (if (and (not (consp (cdr newlines)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 (functionp (cdr newlines)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 (let ((c-syntactic-context syntax))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 (setq newlines
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 (funcall (cdr newlines) (car newlines) insertion-point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 ;; does a newline go before the open brace?
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 (if (memq 'before newlines)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 ;; we leave the newline we've put in there before,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 ;; but we need to re-indent the line above
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 (let ((pos (- (point-max) (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 (here (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 (c-state-cache c-state-cache))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 (forward-line -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 ;; we may need to update the cache. this should still be
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 ;; faster than recalculating the state in many cases
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 (save-restriction
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 (narrow-to-region here (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 (if (and (c-safe (progn (backward-up-list -1) t))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 (memq (char-before) '(?\) ?}))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 (progn (widen)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 (c-safe (progn (forward-sexp -1) t))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 (setq c-state-cache
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 (c-hack-state (point) 'open c-state-cache))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 (if (and (car c-state-cache)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 (not (consp (car c-state-cache)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 (<= (point) (car c-state-cache)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 (setq c-state-cache (cdr c-state-cache))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 ))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 (let ((here (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 (shift (c-indent-line)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 (setq c-state-cache (c-adjust-state (c-point 'bol) here
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 (- shift) c-state-cache)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 (goto-char (- (point-max) pos))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 ;; if the buffer has changed due to the indentation, we
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 ;; need to recalculate syntax for the current line, but
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 ;; we won't need to update the state cache.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 (if (/= (point) here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 (setq syntax (c-guess-basic-syntax))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 ;; must remove the newline we just stuck in (if we really did it)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 (and delete-temp-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 ;; if there is whitespace before point, then preserve
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 ;; at least one space.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 (delete-indentation)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 (just-one-space)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 (if (not preserve-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 (delete-char -1))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 ;; since we're hanging the brace, we need to recalculate
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 ;; syntax. Update the state to accurately reflect the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 ;; beginning of the line. We punt if we cross any open or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 ;; closed parens because its just too hard to modify the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 ;; known state. This limitation will be fixed in v5.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 (let ((bol (c-point 'bol)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 (if (zerop (car (parse-partial-sexp bol (1- (point)))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 (setq c-state-cache (c-whack-state bol c-state-cache)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 syntax (c-guess-basic-syntax))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287 ;; gotta punt. this requires some horrible kludgery
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 (makunbound 'c-state-cache)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 (setq c-state-cache (c-parse-state)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 syntax nil))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 )
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 ;; now adjust the line's indentation. don't update the state
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 ;; cache since c-guess-basic-syntax isn't called when the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295 ;; syntax is passed to c-indent-line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296 (let ((here (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297 (shift (c-indent-line syntax)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 (setq c-state-cache (c-adjust-state (c-point 'bol) here
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299 (- shift) c-state-cache)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 ;; Do all appropriate clean ups
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 (let ((here (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 (pos (- (point-max) (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 mbeg mend)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304 ;; clean up empty defun braces
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 (if (and c-auto-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 (memq 'empty-defun-braces c-cleanup-list)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 (eq last-command-char ?\})
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 (c-intersect-lists '(defun-close class-close inline-close)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 syntax)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 (skip-chars-backward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313 (eq (char-before) ?\{))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 ;; make sure matching open brace isn't in a comment
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 (not (c-in-literal)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316 (delete-region (point) (1- here)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
317 ;; clean up brace-else-brace
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 (if (and c-auto-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 (memq 'brace-else-brace c-cleanup-list)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 (eq last-command-char ?\{)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 (re-search-backward "}[ \t\n]*else[ \t\n]*{" nil t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323 (setq mbeg (match-beginning 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324 mend (match-end 0))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
325 (= mend here))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 (not (c-in-literal)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 (delete-region mbeg mend)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 (insert "} else {")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 ;; clean up brace-elseif-brace
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331 (if (and c-auto-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332 (memq 'brace-elseif-brace c-cleanup-list)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 (eq last-command-char ?\{)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*" nil t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 (goto-char (match-end 0))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 (c-safe (forward-sexp 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (skip-chars-forward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 (setq mbeg (match-beginning 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 mend (match-end 0))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 (= here (1+ (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 (not (c-in-literal)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (delete-region mbeg mend)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 (insert "} else if ")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 (goto-char (- (point-max) pos))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 )
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 ;; does a newline go after the brace?
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 (if (memq 'after newlines)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 (newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 ;; update on c-state-cache
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 (let* ((bufpos (- (point) 2))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 (which (if (eq (char-after bufpos) ?{) 'open 'close))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 (c-state-cache (c-hack-state bufpos which c-state-cache)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 (c-indent-line))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 ;; blink the paren
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 (and (eq last-command-char ?\})
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 old-blink-paren
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 (c-backward-syntactic-ws safepos)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 (funcall old-blink-paren)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 ))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 (defun c-electric-slash (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 "Insert a slash character.
19297
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
367
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
368 Indent the line as a comment, if:
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
369
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
370 1. The slash is second of a `//' line oriented comment introducing
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
371 token and we are on a comment-only-line, or
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
372
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
373 2. The slash is part of a `*/' token that closes a block oriented
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
374 comment.
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
375
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376 If numeric ARG is supplied or point is inside a literal, indentation
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377 is inhibited."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
378 (interactive "*P")
19297
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
379 (let* ((ch (char-before))
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
380 (indentp (and (not arg)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
381 (eq last-command-char ?/)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
382 (or (and (eq ch ?/)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
383 (not (c-in-literal)))
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
384 (and (eq ch ?*)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
385 (c-in-literal)))
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
386 ))
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
387 ;; shut this up
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
388 (c-echo-syntactic-information-p nil))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 (if indentp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 (c-indent-line))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 (defun c-electric-star (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 "Insert a star character.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 If the star is the second character of a C style comment introducing
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396 construct, and we are on a comment-only-line, indent line as comment.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 If numeric ARG is supplied or point is inside a literal, indentation
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 is inhibited."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
399 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 ;; if we are in a literal, or if arg is given do not re-indent the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 ;; current line, unless this star introduces a comment-only line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 (if (and (not arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 (memq (c-in-literal) '(c))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 (eq (char-before) ?*)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 (skip-chars-backward "*")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 (if (eq (char-before) ?/)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 (forward-char -1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411 (skip-chars-backward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412 (bolp)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413 ;; shut this up
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414 (let (c-echo-syntactic-information-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 (c-indent-line))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416 ))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
417
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 (defun c-electric-semi&comma (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419 "Insert a comma or semicolon.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420 When the auto-newline feature is turned on, as evidenced by the \"/a\"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421 or \"/ah\" string on the mode line, a newline might be inserted. See
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 the variable `c-hanging-semi&comma-criteria' for how newline insertion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 is determined.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
425 When semicolon is inserted, the line is re-indented unless a numeric
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
426 arg is supplied, point is inside a literal, or there are
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427 non-whitespace characters on the line following the semicolon."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
428 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
429 (let* ((lim (c-most-enclosing-brace (c-parse-state)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430 (literal (c-in-literal lim))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
431 (here (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
432 ;; shut this up
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433 (c-echo-syntactic-information-p nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434 (if (or literal
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435 arg
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436 (not (looking-at "[ \t]*$")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
437 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438 ;; do some special stuff with the character
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440 ;; do all cleanups, reindentations, and newline insertions, but
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
441 ;; only if c-auto-newline is turned on
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
442 (if (not c-auto-newline) nil
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
443 ;; clean ups
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
444 (let ((pos (- (point-max) (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
445 (if (and (or (and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
446 (eq last-command-char ?,)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
447 (memq 'list-close-comma c-cleanup-list))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
448 (and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
449 (eq last-command-char ?\;)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
450 (memq 'defun-close-semi c-cleanup-list)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
451 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
452 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
453 (skip-chars-backward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
454 (eq (char-before) ?}))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
455 ;; make sure matching open brace isn't in a comment
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
456 (not (c-in-literal lim)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
457 (delete-region (point) here))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
458 (goto-char (- (point-max) pos)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
459 ;; re-indent line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
460 (c-indent-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
461 ;; check to see if a newline should be added
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
462 (let ((criteria c-hanging-semi&comma-criteria)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
463 answer add-newline-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
464 (while criteria
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
465 (setq answer (funcall (car criteria)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
466 ;; only nil value means continue checking
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
467 (if (not answer)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
468 (setq criteria (cdr criteria))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
469 (setq criteria nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
470 ;; only 'stop specifically says do not add a newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
471 (setq add-newline-p (not (eq answer 'stop)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
472 ))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
473 (if add-newline-p
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
474 (progn (newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
475 (c-indent-line)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
476 )))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
477
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
478 (defun c-electric-colon (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
479 "Insert a colon.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
480
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
481 If the auto-newline feature is turned on, as evidenced by the \"/a\"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
482 or \"/ah\" string on the mode line, newlines are inserted before and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
483 after colons based on the value of `c-hanging-colons-alist'.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
484
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
485 Also, the line is re-indented unless a numeric ARG is supplied, there
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
486 are non-whitespace characters present on the line after the colon, or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
487 the colon is inserted inside a literal.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
488
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
489 This function cleans up double colon scope operators based on the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
490 value of `c-cleanup-list'."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
491 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
492 (let* ((bod (c-point 'bod))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
493 (literal (c-in-literal bod))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
494 syntax newlines is-scope-op
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
495 ;; shut this up
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
496 (c-echo-syntactic-information-p nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
497 (if (or literal
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
498 arg
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
499 (not (looking-at "[ \t]*$")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
500 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
501 ;; insert the colon, then do any specified cleanups
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
502 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
503 (let ((pos (- (point-max) (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
504 (here (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
505 (if (and c-auto-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
506 (memq 'scope-operator c-cleanup-list)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
507 (eq (char-before) ?:)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
508 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
509 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
510 (skip-chars-backward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
511 (eq (char-before) ?:))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
512 (not (c-in-literal))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
513 (not (eq (char-after (- (point) 2)) ?:)))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
514 (progn
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
515 (delete-region (point) (1- here))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
516 (setq is-scope-op t)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
517 (goto-char (- (point-max) pos)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
518 ;; lets do some special stuff with the colon character
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
519 (setq syntax (c-guess-basic-syntax)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
520 ;; some language elements can only be determined by
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
521 ;; checking the following line. Lets first look for ones
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
522 ;; that can be found when looking on the line with the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
523 ;; colon
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
524 newlines
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
525 (and c-auto-newline
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
526 (or (c-lookup-lists '(case-label label access-label)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
527 syntax c-hanging-colons-alist)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
528 (c-lookup-lists '(member-init-intro inher-intro)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
529 (prog2
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
530 (insert "\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
531 (c-guess-basic-syntax)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
532 (delete-char -1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
533 c-hanging-colons-alist))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
534 ;; indent the current line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
535 (c-indent-line syntax)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
536 ;; does a newline go before the colon? Watch out for already
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
537 ;; non-hung colons. However, we don't unhang them because that
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
538 ;; would be a cleanup (and anti-social).
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
539 (if (and (memq 'before newlines)
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
540 (not is-scope-op)
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
541 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
542 (skip-chars-backward ": \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
543 (not (bolp))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
544 (let ((pos (- (point-max) (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
545 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
546 (newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
547 (c-indent-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
548 (goto-char (- (point-max) pos))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
549 ;; does a newline go after the colon?
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
550 (if (and (memq 'after (cdr-safe newlines))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
551 (not is-scope-op))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
552 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
553 (newline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
554 (c-indent-line)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
555 )))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
556
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
557 (defun c-electric-lt-gt (arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
558 "Insert a less-than, or greater-than character.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
559 When the auto-newline feature is turned on, as evidenced by the \"/a\"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
560 or \"/ah\" string on the mode line, the line will be re-indented if
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
561 the character inserted is the second of a C++ style stream operator
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
562 and the buffer is in C++ mode.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
563
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
564 The line will also not be re-indented if a numeric argument is
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
565 supplied, or point is inside a literal."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
566 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
567 (let ((indentp (and (not arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
568 (eq (char-before) last-command-char)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
569 (not (c-in-literal))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
570 ;; shut this up
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
571 (c-echo-syntactic-information-p nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
572 (self-insert-command (prefix-numeric-value arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
573 (if indentp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
574 (c-indent-line))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
575
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
576
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
577
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
578 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
579 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
580 (defun c-forward-into-nomenclature (&optional arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
581 "Move forward to end of a nomenclature section or word.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
582 With arg, to it arg times."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
583 (interactive "p")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
584 (let ((case-fold-search nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
585 (if (> arg 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
586 (re-search-forward "\\W*\\([A-Z]*[a-z0-9]*\\)" (point-max) t arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
587 (while (and (< arg 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
588 (re-search-backward
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
589 "\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
590 (point-min) 0))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
591 (forward-char 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
592 (setq arg (1+ arg)))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
593 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
594
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
595 (defun c-backward-into-nomenclature (&optional arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
596 "Move backward to beginning of a nomenclature section or word.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
597 With optional ARG, move that many times. If ARG is negative, move
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
598 forward."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
599 (interactive "p")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
600 (c-forward-into-nomenclature (- arg))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
601 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
602
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
603 (defun c-scope-operator ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
604 "Insert a double colon scope operator at point.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
605 No indentation or other \"electric\" behavior is performed."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
606 (interactive "*")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
607 (insert "::"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
608
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
609 (defun c-beginning-of-defun (&optional arg)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
610 "Move backward to the beginning of a defun.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
611 With argument, do it that many times. Negative arg -N
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
612 means move forward to Nth following beginning of defun.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
613 Returns t unless search stops due to beginning or end of buffer.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
614
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
615 Unlike the built-in `beginning-of-defun' this tries to be smarter
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
616 about finding the char with open-parenthesis syntax that starts the
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
617 defun."
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
618 (interactive "p")
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
619 (if (< arg 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
620 (c-end-of-defun (- arg))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
621 (while (> arg 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
622 (let ((state (nreverse (c-parse-state)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
623 prevbod bod)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
624 (while (and state (not bod))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
625 (setq bod (car state)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
626 state (cdr state))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
627 (if (consp bod)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
628 (setq prevbod (car bod)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
629 bod nil)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
630 (cond
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
631 (bod (goto-char bod))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
632 (prevbod (goto-char prevbod))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
633 (t (goto-char (c-point 'bod)))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
634 (setq arg (1- arg))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
635 (c-keep-region-active))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
636
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
637 (defun c-end-of-defun (&optional arg)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
638 "Move forward to next end of defun. With argument, do it that many times.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
639 Negative argument -N means move back to Nth preceding end of defun.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
640
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
641 An end of a defun occurs right after the close-parenthesis that matches
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
642 the open-parenthesis that starts a defun; see `beginning-of-defun'."
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
643 (interactive "p")
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
644 (if (< arg 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
645 (c-beginning-of-defun (- arg))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
646 (while (> arg 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
647 ;; skip down into the next defun-block
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
648 (while (and (c-safe (down-list 1) t)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
649 (not (eq (char-before) ?{)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
650 (forward-char -1)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
651 (forward-sexp))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
652 (c-beginning-of-defun 1)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
653 (forward-sexp 1)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
654 (setq arg (1- arg)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
655 (forward-line 1))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
656 (c-keep-region-active))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
657
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
658
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
659 (defun c-beginning-of-statement (&optional count lim sentence-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
660 "Go to the beginning of the innermost C statement.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
661 With prefix arg, go back N - 1 statements. If already at the
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
662 beginning of a statement then go to the beginning of the closest
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
663 preceding one, moving into nested blocks if necessary (use
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
664 \\[backward-sexp] to skip over a block). If within a comment, or next
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
665 to a comment (only whitespace between), move by sentences instead of
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
666 statements.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
667
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
668 When called from a program, this function takes 3 optional args: the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
669 repetition count, a buffer position limit which is the farthest back
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
670 to search, and a flag saying whether to do sentence motion when in a
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
671 comment."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
672 (interactive (list (prefix-numeric-value current-prefix-arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
673 nil t))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
674 (let* ((count (or count 1))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
675 here
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
676 (range (c-collect-line-comments (c-literal-limits lim))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
677 (while (and (/= count 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
678 (or (not lim) (> (point) lim)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
679 (setq here (point))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
680 (if (and (not range) sentence-flag)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
681 (save-excursion
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
682 ;; Find the comment next to point if we're not in one.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
683 (if (> count 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
684 ;; Finding a comment backwards is a bit cumbersome
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
685 ;; because `forward-comment' regards every newline as
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
686 ;; a comment when searching backwards (Emacs 19.34).
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
687 (while (and (progn (skip-chars-backward " \t")
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
688 (setq range (point))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
689 (setq range (if (forward-comment -1)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
690 (cons (point) range)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
691 nil)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
692 (= (char-after) ?\n)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
693 (skip-chars-forward " \t\n")
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
694 (setq range (point))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
695 (setq range (if (forward-comment 1)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
696 (cons range (point))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
697 nil)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
698 (setq range (c-collect-line-comments range))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
699 (if (and (< count 0) (= here (point-max)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
700 ;; Special case because eob might be in a literal.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
701 (setq range nil))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
702 (if range
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
703 (if (and sentence-flag
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
704 (/= (char-syntax (char-after (car range))) ?\"))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
705 (progn
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
706 ;; move by sentence, but not past the limit of the literal
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
707 (save-restriction
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
708 (narrow-to-region (save-excursion
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
709 (goto-char (car range))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
710 (looking-at comment-start-skip)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
711 (goto-char (match-end 0))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
712 (point))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
713 (save-excursion
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
714 (goto-char (cdr range))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
715 (if (save-excursion
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
716 (goto-char (car range))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
717 (looking-at "/\\*"))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
718 (backward-char 2))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
719 (skip-chars-backward " \t\n")
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
720 (point)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
721 (c-safe (forward-sentence (if (> count 0) -1 1))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
722 ;; See if we should escape the literal.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
723 (if (> count 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
724 (if (< (point) here)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
725 (setq count (1- count))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
726 (goto-char (car range))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
727 (setq range nil))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
728 (if (> (point) here)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
729 (setq count (1+ count))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
730 (goto-char (cdr range))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
731 (setq range nil))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
732 (goto-char (if (> count 0) (car range) (cdr range)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
733 (setq range nil))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
734 ;; Below we do approximately the same as
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
735 ;; c-beginning-of-statement-1 and c-end-of-statement-1 and
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
736 ;; perhaps they should be changed, but that'd likely break a
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
737 ;; lot in cc-engine.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
738 (goto-char here)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
739 ;; Move out of any enclosing non-`{ }' parens.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
740 (let ((last (point)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
741 (while (and (c-safe (progn (up-list 1) t))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
742 (/= (char-before) ?\}))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
743 (setq last (point)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
744 (goto-char last))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
745 (if (> count 0)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
746 (if (condition-case nil
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
747 ;; Stop before `{' and after `;', `{', `}' and
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
748 ;; `};' when not followed by `}', but on the other
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
749 ;; side of the syntactic ws. Also stop before
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
750 ;; `}', but only to catch comments. Move by sexps
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
751 ;; and move into `{ }', but not into any other
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
752 ;; other type of paren.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
753 (catch 'done
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
754 (let (last)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
755 (while t
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
756 (setq last (point))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
757 (if (and (looking-at "[{}]")
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
758 (/= here last))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
759 (throw 'done (= (char-after) ?{)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
760 (c-backward-syntactic-ws)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
761 (cond ((bobp) ; Must handle bob specially.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
762 (if (= here last)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
763 (if (= last (point-min))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
764 (throw 'done t)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
765 (goto-char last)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
766 (throw 'done nil))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
767 (goto-char last)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
768 (throw 'done t)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
769 ((progn (backward-char)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
770 (looking-at "[;{}]"))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
771 (if (or (= here last)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
772 (= (char-after last) ?}))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
773 (if (and (= (char-before) ?})
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
774 (= (char-after) ?\;))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
775 (backward-char))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
776 (goto-char last)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
777 (throw 'done t)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
778 ((or (= (char-syntax (char-after)) ?\))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
779 (= (char-syntax (char-after)) ?\"))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
780 (forward-char)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
781 (backward-sexp))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
782 ))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
783 (error
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
784 (goto-char (point-min))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
785 t))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
786 (setq count (1- count)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
787 (if (condition-case nil
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
788 ;; Stop before `{' and `}' and after `;', `}' and
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
789 ;; `};'. Also stop after `{', but only to catch
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
790 ;; comments. Move by sexps and move into `{ }', but
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
791 ;; not into any other other type of paren.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
792 (catch 'done
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
793 (let (last)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
794 (while t
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
795 (setq last (point))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
796 (c-forward-syntactic-ws)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
797 (cond ((= (char-after) ?{)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
798 (if (= here last)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
799 (progn (forward-char)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
800 (throw 'done nil))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
801 (goto-char last)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
802 (throw 'done t)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
803 ((and (= (char-after) ?})
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
804 (/= here last))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
805 (goto-char last)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
806 (throw 'done t))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
807 ((looking-at ";\\|};?")
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
808 (goto-char (match-end 0))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
809 (throw 'done t))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
810 ((or (= (char-syntax (char-after)) ?\()
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
811 (= (char-syntax (char-after)) ?\"))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
812 (forward-sexp))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
813 (t
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
814 (forward-char))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
815 ))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
816 (error
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
817 (goto-char (point-max))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
818 t))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
819 (setq count (1+ count)))))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
820 ;; If we haven't moved we're near a buffer limit.
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
821 (when (= (point) here)
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
822 (goto-char (if (> count 0) (point-min) (point-max)))
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
823 (setq count 0)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
824 ;; its possible we've been left up-buf of lim
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
825 (if lim (goto-char (max (point) lim))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
826 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
827
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
828 (defun c-end-of-statement (&optional count lim sentence-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
829 "Go to the end of the innermost C statement.
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
830 With prefix arg, go forward N - 1 statements. Move forward to the end
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
831 of the next statement if already at end, and move into nested blocks
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
832 \(use \\[forward-sexp] to skip over a block). If within a comment, or
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
833 next to a comment (only whitespace between), move by sentences instead
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
834 of statements.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
835
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
836 When called from a program, this function takes 3 optional args: the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
837 repetition count, a buffer position limit which is the farthest back
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
838 to search, and a flag saying whether to do sentence motion when in a
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
839 comment."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
840 (interactive (list (prefix-numeric-value current-prefix-arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
841 nil t))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
842 (c-beginning-of-statement (- (or count 1)) lim sentence-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
843 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
844
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
845
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
846 ;; set up electric character functions to work with pending-del,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
847 ;; (a.k.a. delsel) mode. All symbols get the t value except
18843
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
848 ;; the functions which delete, which gets 'supersede.
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
849 (mapcar
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
850 (function
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
851 (lambda (sym)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
852 (put sym 'delete-selection t) ; for delsel (Emacs)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
853 (put sym 'pending-delete t))) ; for pending-del (XEmacs)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
854 '(c-electric-pound
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
855 c-electric-brace
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
856 c-electric-slash
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
857 c-electric-star
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
858 c-electric-semi&comma
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
859 c-electric-lt-gt
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
860 c-electric-colon))
18843
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
861 (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
862 (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
863 (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
6e04c0670f55 Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents: 18720
diff changeset
864 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
865
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
866
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
867 ;; This is used by indent-for-comment to decide how much to indent a
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
868 ;; comment in C code based on its context.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
869 (defun c-comment-indent ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
870 (if (looking-at (concat "^\\(" c-comment-start-regexp "\\)"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
871 0 ;Existing comment at bol stays there.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
872 (let ((opoint (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
873 placeholder)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
874 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
875 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
876 (cond
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
877 ;; CASE 1: A comment following a solitary close-brace should
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
878 ;; have only one space.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
879 ((looking-at (concat "[ \t]*}[ \t]*\\($\\|"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
880 c-comment-start-regexp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
881 "\\)"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
882 (search-forward "}")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
883 (1+ (current-column)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
884 ;; CASE 2: 2 spaces after #endif
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
885 ((or (looking-at "^#[ \t]*endif[ \t]*")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
886 (looking-at "^#[ \t]*else[ \t]*"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
887 7)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
888 ;; CASE 3: when comment-column is nil, calculate the offset
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
889 ;; according to c-offsets-alist. E.g. identical to hitting
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
890 ;; TAB.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
891 ((and c-indent-comments-syntactically-p
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
892 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
893 (skip-chars-forward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
894 (or (looking-at comment-start)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
895 (eolp))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
896 (let ((syntax (c-guess-basic-syntax)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
897 ;; BOGOSITY ALERT: if we're looking at the eol, its
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
898 ;; because indent-for-comment hasn't put the comment-start
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
899 ;; in the buffer yet. this will screw up the syntactic
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
900 ;; analysis so we kludge in the necessary info. Another
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
901 ;; kludge is that if we're at the bol, then we really want
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
902 ;; to ignore any anchoring as specified by
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
903 ;; c-comment-only-line-offset since it doesn't apply here.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
904 (if (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
905 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
906 (skip-chars-forward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
907 (eolp))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
908 (c-add-syntax 'comment-intro))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
909 (let ((c-comment-only-line-offset
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
910 (if (consp c-comment-only-line-offset)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
911 c-comment-only-line-offset
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
912 (cons c-comment-only-line-offset
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
913 c-comment-only-line-offset))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
914 (apply '+ (mapcar 'c-get-offset syntax)))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
915 ;; CASE 4: use comment-column if previous line is a
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
916 ;; comment-only line indented to the left of comment-column
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
917 ((save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
918 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
919 (and (not (bobp))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
920 (forward-line -1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
921 (skip-chars-forward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
922 (prog1
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
923 (looking-at c-comment-start-regexp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
924 (setq placeholder (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
925 (goto-char placeholder)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
926 (if (< (current-column) comment-column)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
927 comment-column
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
928 (current-column)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
929 ;; CASE 5: If comment-column is 0, and nothing but space
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
930 ;; before the comment, align it at 0 rather than 1.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
931 ((progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
932 (goto-char opoint)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
933 (skip-chars-backward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
934 (and (= comment-column 0) (bolp)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
935 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
936 ;; CASE 6: indent at comment column except leave at least one
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
937 ;; space.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
938 (t (max (1+ (current-column))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
939 comment-column))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
940 )))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
941
19804
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
942
19297
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
943 ;; for proposed new variable comment-line-break-function
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
944 (defun c-comment-line-break-function (&optional soft)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
945 ;; we currently don't do anything with soft line breaks
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
946 (let ((literal (c-in-literal))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
947 at-comment-col)
19804
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
948 (cond
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
949 ((eq literal 'string))
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
950 ((or (not c-comment-continuation-stars)
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
951 (not literal))
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
952 (indent-new-comment-line soft))
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
953 (t (let ((here (point))
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
954 (leader c-comment-continuation-stars))
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
955 (back-to-indentation)
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
956 ;; comment could be hanging
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
957 (if (not (c-in-literal))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
958 (progn
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
959 (forward-line 1)
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
960 (forward-comment -1)
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
961 (setq at-comment-col (= (current-column) comment-column))))
19804
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
962 ;; are we looking at a block or lines style comment?
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
963 (if (and (looking-at (concat "\\(" c-comment-start-regexp
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
964 "\\)[ \t]+"))
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
965 (string-equal (match-string 1) "//"))
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
966 ;; line style
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
967 (setq leader (match-string 0)))
19804
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
968 (goto-char here)
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
969 (delete-region (progn (skip-chars-backward " \t") (point))
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
970 (progn (skip-chars-forward " \t") (point)))
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
971 (newline)
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
972 ;; to avoid having an anchored comment that c-indent-line will
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
973 ;; trip up on
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
974 (insert " " leader)
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
975 (if at-comment-col
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
976 (indent-for-comment))
19804
37e25ff5a7f1 (c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents: 19378
diff changeset
977 (c-indent-line))))))
19297
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
978
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
979 ;; advice for indent-new-comment-line for older Emacsen
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
980 (if (boundp 'comment-line-break-function)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
981 nil
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
982 (require 'advice)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
983 (defadvice indent-new-comment-line (around c-line-break-advice activate)
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
984 (if (or (not c-buffer-is-cc-mode)
19378
a1987ab30543 (c-comment-line-break-function): In this function, and the defadvice,
Richard M. Stallman <rms@gnu.org>
parents: 19306
diff changeset
985 (not (c-in-literal))
19297
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
986 (not c-comment-continuation-stars))
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
987 ad-do-it
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
988 (c-comment-line-break-function (ad-get-arg 0)))))
1982f8488449 (indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents: 19252
diff changeset
989
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
990 ;; used by outline-minor-mode
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
991 (defun c-outline-level ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
992 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
993 (skip-chars-forward "\t ")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
994 (current-column)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
995
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
996
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
997 (defun c-up-conditional (count)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
998 "Move back to the containing preprocessor conditional, leaving mark behind.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
999 A prefix argument acts as a repeat count. With a negative argument,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1000 move forward to the end of the containing preprocessor conditional.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1001 When going backwards, `#elif' is treated like `#else' followed by
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1002 `#if'. When going forwards, `#elif' is ignored."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1003 (interactive "p")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1004 (c-forward-conditional (- count) t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1005 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1006
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1007 (defun c-backward-conditional (count &optional up-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1008 "Move back across a preprocessor conditional, leaving mark behind.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1009 A prefix argument acts as a repeat count. With a negative argument,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1010 move forward across a preprocessor conditional."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1011 (interactive "p")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1012 (c-forward-conditional (- count) up-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1013 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1014
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1015 (defun c-forward-conditional (count &optional up-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1016 "Move forward across a preprocessor conditional, leaving mark behind.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1017 A prefix argument acts as a repeat count. With a negative argument,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1018 move backward across a preprocessor conditional."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1019 (interactive "p")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1020 (let* ((forward (> count 0))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1021 (increment (if forward -1 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1022 (search-function (if forward 're-search-forward 're-search-backward))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1023 (new))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1024 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1025 (while (/= count 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1026 (let ((depth (if up-flag 0 -1)) found)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1027 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1028 ;; Find the "next" significant line in the proper direction.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1029 (while (and (not found)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1030 ;; Rather than searching for a # sign that
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1031 ;; comes at the beginning of a line aside from
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1032 ;; whitespace, search first for a string
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1033 ;; starting with # sign. Then verify what
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1034 ;; precedes it. This is faster on account of
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1035 ;; the fastmap feature of the regexp matcher.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1036 (funcall search-function
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1037 "#[ \t]*\\(if\\|elif\\|endif\\)"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1038 nil t))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1039 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1040 ;; Now verify it is really a preproc line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1041 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1042 (let ((prev depth))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1043 ;; Update depth according to what we found.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1044 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1045 (cond ((looking-at "[ \t]*#[ \t]*endif")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1046 (setq depth (+ depth increment)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1047 ((looking-at "[ \t]*#[ \t]*elif")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1048 (if (and forward (= depth 0))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1049 (setq found (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1050 (t (setq depth (- depth increment))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1051 ;; If we are trying to move across, and we find an
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1052 ;; end before we find a beginning, get an error.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1053 (if (and (< prev 0) (< depth prev))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1054 (error (if forward
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1055 "No following conditional at this level"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1056 "No previous conditional at this level")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1057 ;; When searching forward, start from next line so
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1058 ;; that we don't find the same line again.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1059 (if forward (forward-line 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1060 ;; If this line exits a level of conditional, exit
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1061 ;; inner loop.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1062 (if (< depth 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1063 (setq found (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1064 ;; else
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1065 (if forward (forward-line 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1066 )))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1067 (or found
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1068 (error "No containing preprocessor conditional"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1069 (goto-char (setq new found)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1070 (setq count (+ count increment))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1071 (push-mark)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1072 (goto-char new))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1073 (c-keep-region-active))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1074
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1075
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1076 ;; commands to indent lines, regions, defuns, and expressions
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1077 (defun c-indent-command (&optional whole-exp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1078 "Indent current line as C code, and/or insert some whitespace.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1079
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1080 If `c-tab-always-indent' is t, always just indent the current line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1081 If nil, indent the current line only if point is at the left margin or
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1082 in the line's indentation; otherwise insert some whitespace[*]. If
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1083 other than nil or t, then some whitespace[*] is inserted only within
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1084 literals (comments and strings) and inside preprocessor directives,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1085 but the line is always reindented.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1086
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1087 A numeric argument, regardless of its value, means indent rigidly all
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1088 the lines of the expression starting after point so that this line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1089 becomes properly indented. The relative indentation among the lines
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1090 of the expression are preserved.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1091
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1092 [*] The amount and kind of whitespace inserted is controlled by the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1093 variable `c-insert-tab-function', which is called to do the actual
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1094 insertion of whitespace. Normally the function in this variable
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1095 just inserts a tab character, or the equivalent number of spaces,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1096 depending on the variable `indent-tabs-mode'."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1097
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1098 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1099 (let ((bod (c-point 'bod)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1100 (if whole-exp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1101 ;; If arg, always indent this line as C
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1102 ;; and shift remaining lines of expression the same amount.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1103 (let ((shift-amt (c-indent-line))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1104 beg end)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1105 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1106 (if (eq c-tab-always-indent t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1107 (beginning-of-line))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1108 (setq beg (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1109 (forward-sexp 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1110 (setq end (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1111 (goto-char beg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1112 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1113 (setq beg (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1114 (if (> end beg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1115 (indent-code-rigidly beg end (- shift-amt) "#")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1116 ;; No arg supplied, use c-tab-always-indent to determine
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1117 ;; behavior
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1118 (cond
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1119 ;; CASE 1: indent when at column zero or in lines indentation,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1120 ;; otherwise insert a tab
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1121 ((not c-tab-always-indent)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1122 (if (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1123 (skip-chars-backward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1124 (not (bolp)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1125 (funcall c-insert-tab-function)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1126 (c-indent-line)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1127 ;; CASE 2: just indent the line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1128 ((eq c-tab-always-indent t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1129 (c-indent-line))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1130 ;; CASE 3: if in a literal, insert a tab, but always indent the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1131 ;; line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1132 (t
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1133 (if (c-in-literal bod)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1134 (funcall c-insert-tab-function))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1135 (c-indent-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1136 )))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1137
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1138 (defun c-indent-exp (&optional shutup-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1139 "Indent each line in balanced expression following point.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1140 Optional SHUTUP-P if non-nil, inhibits message printing and error checking."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1141 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1142 (let ((here (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1143 end progress-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1144 (unwind-protect
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1145 (let ((c-echo-syntactic-information-p nil) ;keep quiet for speed
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1146 (start (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1147 ;; try to be smarter about finding the range of
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1148 ;; lines to indent. skip all following
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1149 ;; whitespace. failing that, try to find any
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1150 ;; opening brace on the current line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1151 (skip-chars-forward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1152 (if (memq (char-after) '(?\( ?\[ ?\{))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1153 (point)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1154 (let ((state (parse-partial-sexp (point)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1155 (c-point 'eol))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1156 (and (nth 1 state)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1157 (goto-char (nth 1 state))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1158 (memq (char-after) '(?\( ?\[ ?\{))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1159 (point)))))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1160 ;; find balanced expression end
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1161 (setq end (and (c-safe (progn (forward-sexp 1) t))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1162 (point-marker)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1163 ;; sanity check
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1164 (and (not start)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1165 (not shutup-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1166 (error "Cannot find start of balanced expression to indent."))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1167 (and (not end)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1168 (not shutup-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1169 (error "Cannot find end of balanced expression to indent."))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1170 (c-progress-init start end 'c-indent-exp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1171 (setq progress-p t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1172 (goto-char start)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1173 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1174 (while (< (point) end)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1175 (if (not (looking-at "[ \t]*$"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1176 (c-indent-line))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1177 (c-progress-update)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1178 (forward-line 1)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1179 ;; make sure marker is deleted
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1180 (and end
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1181 (set-marker end nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1182 (and progress-p
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1183 (c-progress-fini 'c-indent-exp))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1184 (goto-char here))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1185
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1186 (defun c-indent-defun ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1187 "Re-indents the current top-level function def, struct or class declaration."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1188 (interactive "*")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1189 (let ((here (point-marker))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1190 (c-echo-syntactic-information-p nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1191 (brace (c-least-enclosing-brace (c-parse-state))))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1192 (goto-char (or brace (c-point 'bod)))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1193 ;; if we're sitting at b-o-b, it might be because there was no
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1194 ;; least enclosing brace and we were sitting on the defun's open
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1195 ;; brace.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1196 (if (and (bobp) (not (eq (char-after) ?\{)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1197 (goto-char here))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1198 ;; if defun-prompt-regexp is non-nil, b-o-d might not leave us at
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1199 ;; the open brace. I consider this an Emacs bug.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1200 (and (boundp 'defun-prompt-regexp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1201 defun-prompt-regexp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1202 (looking-at defun-prompt-regexp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1203 (goto-char (match-end 0)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1204 ;; catch all errors in c-indent-exp so we can 1. give more
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1205 ;; meaningful error message, and 2. restore point
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1206 (unwind-protect
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1207 (c-indent-exp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1208 (goto-char here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1209 (set-marker here nil))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1210
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1211 (defun c-indent-region (start end)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1212 ;; Indent every line whose first char is between START and END inclusive.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1213 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1214 (goto-char start)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1215 ;; Advance to first nonblank line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1216 (skip-chars-forward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1217 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1218 (let (endmark)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1219 (unwind-protect
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1220 (let ((c-tab-always-indent t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1221 ;; shut up any echo msgs on indiv lines
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1222 (c-echo-syntactic-information-p nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1223 fence)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1224 (c-progress-init start end 'c-indent-region)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1225 (setq endmark (copy-marker end))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1226 (while (and (bolp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1227 (not (eobp))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1228 (< (point) endmark))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1229 ;; update progress
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1230 (c-progress-update)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1231 ;; Indent one line as with TAB.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1232 (let (nextline sexpend sexpbeg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1233 ;; skip blank lines
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1234 (skip-chars-forward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1235 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1236 ;; indent the current line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1237 (c-indent-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1238 (setq fence (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1239 (if (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1240 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1241 (looking-at "[ \t]*#"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1242 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1243 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1244 ;; Find beginning of following line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1245 (setq nextline (c-point 'bonl))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1246 ;; Find first beginning-of-sexp for sexp extending past
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1247 ;; this line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1248 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1249 (while (< (point) nextline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1250 (condition-case nil
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1251 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1252 (forward-sexp 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1253 (setq sexpend (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1254 (error (setq sexpend nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1255 (goto-char nextline)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1256 (c-forward-syntactic-ws))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1257 (if sexpend
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1258 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1259 ;; make sure the sexp we found really starts on the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1260 ;; current line and extends past it
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1261 (goto-char sexpend)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1262 (setq sexpend (point-marker))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1263 (c-safe (backward-sexp 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1264 (setq sexpbeg (point))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1265 (if (and sexpbeg (< sexpbeg fence))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1266 (setq sexpbeg fence)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1267 ;; check to see if the next line starts a
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1268 ;; comment-only line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1269 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1270 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1271 (skip-chars-forward " \t")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1272 (if (looking-at c-comment-start-regexp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1273 (setq sexpbeg (c-point 'bol))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1274 ;; If that sexp ends within the region, indent it all at
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1275 ;; once, fast.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1276 (condition-case nil
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1277 (if (and sexpend
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1278 (> sexpend nextline)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1279 (<= sexpend endmark))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1280 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1281 (goto-char sexpbeg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1282 (c-indent-exp 'shutup)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1283 (c-progress-update)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1284 (goto-char sexpend)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1285 (error
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1286 (goto-char sexpbeg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1287 (c-indent-line)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1288 ;; Move to following line and try again.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1289 (and sexpend
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1290 (markerp sexpend)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1291 (set-marker sexpend nil))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1292 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1293 (setq fence (point))))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1294 (set-marker endmark nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1295 (c-progress-fini 'c-indent-region)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1296 (c-echo-parsing-error)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1297 ))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1298
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1299 (defun c-mark-function ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1300 "Put mark at end of a C, C++, or Objective-C defun, point at beginning."
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1301 (interactive)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1302 (let ((here (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1303 ;; there should be a c-point position for 'eod
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1304 (eod (save-excursion (end-of-defun) (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1305 (state (c-parse-state))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1306 brace)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1307 (while state
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1308 (setq brace (car state))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1309 (if (consp brace)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1310 (goto-char (cdr brace))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1311 (goto-char brace))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1312 (setq state (cdr state)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1313 (if (eq (char-after) ?{)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1314 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1315 (forward-line -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1316 (while (not (or (bobp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1317 (looking-at "[ \t]*$")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1318 (forward-line -1)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1319 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1320 (skip-chars-forward " \t\n"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1321 (push-mark here)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1322 (push-mark eod nil t)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1323
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1324
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1325 ;; for progress reporting
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1326 (defvar c-progress-info nil)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1327
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1328 (defun c-progress-init (start end context)
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1329 (cond
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1330 ;; Be silent
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1331 ((not c-progress-interval))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1332 ;; Start the progress update messages. If this Emacs doesn't have
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1333 ;; a built-in timer, just be dumb about it.
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1334 ((not (fboundp 'current-time))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1335 (message "indenting region... (this may take a while)"))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1336 ;; If progress has already been initialized, do nothing. otherwise
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1337 ;; initialize the counter with a vector of:
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1338 ;; [start end lastsec context]
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1339 (c-progress-info)
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1340 (t (setq c-progress-info (vector start
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1341 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1342 (goto-char end)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1343 (point-marker))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1344 (nth 1 (current-time))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1345 context))
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1346 (message "indenting region..."))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1347 ))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1348
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1349 (defun c-progress-update ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1350 ;; update progress
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1351 (if (not (and c-progress-info c-progress-interval))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1352 nil
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1353 (let ((now (nth 1 (current-time)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1354 (start (aref c-progress-info 0))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1355 (end (aref c-progress-info 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1356 (lastsecs (aref c-progress-info 2)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1357 ;; should we update? currently, update happens every 2 seconds,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1358 ;; what's the right value?
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1359 (if (< c-progress-interval (- now lastsecs))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1360 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1361 (message "indenting region... (%d%% complete)"
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1362 (/ (* 100 (- (point) start)) (- end start)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1363 (aset c-progress-info 2 now)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1364 )))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1365
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1366 (defun c-progress-fini (context)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1367 ;; finished
20143
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1368 (if (not c-progress-interval)
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1369 nil
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1370 (if (or (eq context (aref c-progress-info 3))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1371 (eq context t))
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1372 (progn
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1373 (set-marker (aref c-progress-info 1) nil)
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1374 (setq c-progress-info nil)
69ce7b7acfd5 (c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents: 20137
diff changeset
1375 (message "indenting region...done")))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1376
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1377
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1378
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1379 ;;; This page handles insertion and removal of backslashes for C macros.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1380
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1381 (defun c-backslash-region (from to delete-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1382 "Insert, align, or delete end-of-line backslashes on the lines in the region.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1383 With no argument, inserts backslashes and aligns existing backslashes.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1384 With an argument, deletes the backslashes.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1385
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1386 This function does not modify blank lines at the start of the region.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1387 If the region ends at the start of a line, it always deletes the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1388 backslash (if any) at the end of the previous line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1389
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1390 You can put the region around an entire macro definition and use this
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1391 command to conveniently insert and align the necessary backslashes."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1392 (interactive "*r\nP")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1393 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1394 (goto-char from)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1395 (let ((column c-backslash-column)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1396 (endmark (make-marker)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1397 (move-marker endmark to)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1398 ;; Compute the smallest column number past the ends of all the lines.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1399 (if (not delete-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1400 (while (< (point) to)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1401 (end-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1402 (if (eq (char-before) ?\\)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1403 (progn (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1404 (skip-chars-backward " \t")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1405 (setq column (max column (1+ (current-column))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1406 (forward-line 1)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1407 ;; Adjust upward to a tab column, if that doesn't push past the margin.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1408 (if (> (% column tab-width) 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1409 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1410 (if (< adjusted (window-width))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1411 (setq column adjusted))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1412 ;; Don't modify blank lines at start of region.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1413 (goto-char from)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1414 (while (and (< (point) endmark) (eolp))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1415 (forward-line 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1416 ;; Add or remove backslashes on all the lines.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1417 (while (< (point) endmark)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1418 (if (and (not delete-flag)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1419 ;; Un-backslashify the last line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1420 ;; if the region ends right at the start of the next line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1421 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1422 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1423 (< (point) endmark)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1424 (c-append-backslash column)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1425 (c-delete-backslash))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1426 (forward-line 1))
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1427 (move-marker endmark nil))))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1428
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1429 (defun c-append-backslash (column)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1430 (end-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1431 (if (eq (char-before) ?\\)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1432 (progn (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1433 (delete-horizontal-space)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1434 (indent-to column))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1435 (indent-to column)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1436 (insert "\\")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1437
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1438 (defun c-delete-backslash ()
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1439 (end-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1440 (or (bolp)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1441 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1442 (forward-char -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1443 (if (looking-at "\\\\")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1444 (delete-region (1+ (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1445 (progn (skip-chars-backward " \t") (point)))))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1446
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1447
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1448 (defun c-fill-paragraph (&optional arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1449 "Like \\[fill-paragraph] but handles C and C++ style comments.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1450 If any of the current line is a comment or within a comment,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1451 fill the comment or the paragraph of it that point is in,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1452 preserving the comment indentation or line-starting decorations.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1453
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1454 Optional prefix ARG means justify paragraph as well."
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1455 (interactive "*P")
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1456 (let* (comment-start-place
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1457 (first-line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1458 ;; Check for obvious entry to comment.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1459 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1460 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1461 (skip-chars-forward " \t\n")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1462 (and (looking-at comment-start-skip)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1463 (setq comment-start-place (point)))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1464 (re1 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
19378
a1987ab30543 (c-comment-line-break-function): In this function, and the defadvice,
Richard M. Stallman <rms@gnu.org>
parents: 19306
diff changeset
1465 (if (save-excursion
a1987ab30543 (c-comment-line-break-function): In this function, and the defadvice,
Richard M. Stallman <rms@gnu.org>
parents: 19306
diff changeset
1466 (beginning-of-line)
a1987ab30543 (c-comment-line-break-function): In this function, and the defadvice,
Richard M. Stallman <rms@gnu.org>
parents: 19306
diff changeset
1467 (looking-at ".*//"))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1468 (let ((fill-prefix fill-prefix)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1469 ;; Lines containing just a comment start or just an end
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1470 ;; should not be filled into paragraphs they are next
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1471 ;; to.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1472 (paragraph-start (concat paragraph-start re1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1473 (paragraph-separate (concat paragraph-separate re1)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1474 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1475 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1476 ;; Move up to first line of this comment.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1477 (while (and (not (bobp))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1478 (looking-at "[ \t]*//[ \t]*[^ \t\n]"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1479 (forward-line -1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1480 (if (not (looking-at ".*//[ \t]*[^ \t\n]"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1481 (forward-line 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1482 ;; Find the comment start in this line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1483 (re-search-forward "[ \t]*//[ \t]*")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1484 ;; Set the fill-prefix to be what all lines except the first
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1485 ;; should start with. But do not alter a user set fill-prefix.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1486 (if (null fill-prefix)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1487 (setq fill-prefix (buffer-substring (match-beginning 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1488 (match-end 0))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1489 (save-restriction
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1490 ;; Narrow down to just the lines of this comment.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1491 (narrow-to-region (c-point 'bol)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1492 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1493 (forward-line 1)
20912
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1494 (while
a0748eef9a76 (c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents: 20143
diff changeset
1495 (looking-at (regexp-quote fill-prefix))
18720
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1496 (forward-line 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1497 (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1498 (fill-paragraph arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1499 t)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1500 ;; else C style comments
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1501 (if (or first-line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1502 ;; t if we enter a comment between start of function and
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1503 ;; this line.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1504 (eq (c-in-literal) 'c)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1505 ;; t if this line contains a comment starter.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1506 (setq first-line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1507 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1508 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1509 (prog1
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1510 (re-search-forward comment-start-skip
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1511 (save-excursion (end-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1512 (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1513 t)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1514 (setq comment-start-place (point))))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1515 ;; Inside a comment: fill one comment paragraph.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1516 (let ((fill-prefix
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1517 ;; The prefix for each line of this paragraph
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1518 ;; is the appropriate part of the start of this line,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1519 ;; up to the column at which text should be indented.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1520 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1521 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1522 (if (looking-at "[ \t]*/\\*.*\\*/")
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1523 (progn (re-search-forward comment-start-skip)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1524 (make-string (current-column) ?\ ))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1525 (if first-line (forward-line 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1526
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1527 (let ((line-width (progn (end-of-line) (current-column))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1528 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1529 (prog1
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1530 (buffer-substring
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1531 (point)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1532
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1533 ;; How shall we decide where the end of the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1534 ;; fill-prefix is?
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1535 (progn
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1536 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1537 (skip-chars-forward " \t*" (c-point 'eol))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1538 ;; kludge alert, watch out for */, in
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1539 ;; which case fill-prefix should *not*
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1540 ;; be "*"!
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1541 (if (and (eq (char-after) ?/)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1542 (eq (char-before) ?*))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1543 (forward-char -1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1544 (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1545
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1546 ;; If the comment is only one line followed
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1547 ;; by a blank line, calling move-to-column
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1548 ;; above may have added some spaces and tabs
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1549 ;; to the end of the line; the fill-paragraph
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1550 ;; function will then delete it and the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1551 ;; newline following it, so we'll lose a
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1552 ;; blank line when we shouldn't. So delete
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1553 ;; anything move-to-column added to the end
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1554 ;; of the line. We record the line width
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1555 ;; instead of the position of the old line
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1556 ;; end because move-to-column might break a
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1557 ;; tab into spaces, and the new characters
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1558 ;; introduced there shouldn't be deleted.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1559
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1560 ;; If you can see a better way to do this,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1561 ;; please make the change. This seems very
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1562 ;; messy to me.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1563 (delete-region (progn (move-to-column line-width)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1564 (point))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1565 (progn (end-of-line) (point))))))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1566
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1567 ;; Lines containing just a comment start or just an end
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1568 ;; should not be filled into paragraphs they are next
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1569 ;; to.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1570 (paragraph-start (concat paragraph-start re1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1571 (paragraph-separate (concat paragraph-separate re1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1572 (chars-to-delete 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1573 )
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1574 (save-restriction
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1575 ;; Don't fill the comment together with the code
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1576 ;; following it. So temporarily exclude everything
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1577 ;; before the comment start, and everything after the
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1578 ;; line where the comment ends. If comment-start-place
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1579 ;; is non-nil, the comment starter is there. Otherwise,
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1580 ;; point is inside the comment.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1581 (narrow-to-region (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1582 (if comment-start-place
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1583 (goto-char comment-start-place)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1584 (search-backward "/*"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1585 (if (and (not c-hanging-comment-starter-p)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1586 (looking-at
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1587 (concat c-comment-start-regexp
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1588 "[ \t]*$")))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1589 (forward-line 1))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1590 ;; Protect text before the comment
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1591 ;; start by excluding it. Add
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1592 ;; spaces to bring back proper
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1593 ;; indentation of that point.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1594 (let ((column (current-column)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1595 (prog1 (point)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1596 (setq chars-to-delete column)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1597 (insert-char ?\ column))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1598 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1599 (if comment-start-place
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1600 (goto-char (+ comment-start-place 2)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1601 (search-forward "*/" nil 'move)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1602 (forward-line 1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1603 (point)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1604 (fill-paragraph arg)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1605 (save-excursion
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1606 ;; Delete the chars we inserted to avoid clobbering
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1607 ;; the stuff before the comment start.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1608 (goto-char (point-min))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1609 (if (> chars-to-delete 0)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1610 (delete-region (point) (+ (point) chars-to-delete)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1611 ;; Find the comment ender (should be on last line of
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1612 ;; buffer, given the narrowing) and don't leave it on
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1613 ;; its own line, unless that's the style that's desired.
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1614 (goto-char (point-max))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1615 (forward-line -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1616 (search-forward "*/" nil 'move)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1617 (beginning-of-line)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1618 (if (and c-hanging-comment-ender-p
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1619 (looking-at "[ \t]*\\*/"))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1620 ;(delete-indentation)))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1621 (let ((fill-column (+ fill-column 9999)))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1622 (forward-line -1)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1623 (fill-region-as-paragraph (point) (point-max))))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1624 t)))))
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1625
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1626
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1627 (provide 'cc-cmds)
fc6d08b9bbe2 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1628 ;;; cc-cmds.el ends here