Mercurial > emacs
annotate lisp/progmodes/cc-cmds.el @ 37349:9aada84f08c8
Clarify link commands for MS systems.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 14 Apr 2001 14:49:49 +0000 |
parents | 22964e38f4c2 |
children | 2692454f031f |
rev | line source |
---|---|
24282 | 1 ;;; cc-cmds.el --- user level commands for CC Mode |
18720 | 2 |
36920 | 3 ;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc. |
18720 | 4 |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
5 ;; Authors: 2000- Martin Stjernholm |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
6 ;; 1998-1999 Barry A. Warsaw and Martin Stjernholm |
24282 | 7 ;; 1992-1997 Barry A. Warsaw |
18720 | 8 ;; 1987 Dave Detlefs and Stewart Clamen |
9 ;; 1985 Richard M. Stallman | |
24282 | 10 ;; Maintainer: bug-cc-mode@gnu.org |
18720 | 11 ;; 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
|
12 ;; Version: See cc-mode.el |
18720 | 13 ;; Keywords: c languages oop |
14 | |
15 ;; This file is part of GNU Emacs. | |
16 | |
17 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
18 ;; it under the terms of the GNU General Public License as published by | |
19 ;; the Free Software Foundation; either version 2, or (at your option) | |
20 ;; any later version. | |
21 | |
22 ;; GNU Emacs is distributed in the hope that it will be useful, | |
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 ;; GNU General Public License for more details. | |
26 | |
27 ;; You should have received a copy of the GNU General Public License | |
36920 | 28 ;; along with this program; see the file COPYING. If not, write to |
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18720 | 30 ;; Boston, MA 02111-1307, USA. |
31 | |
26817 | 32 (eval-when-compile |
33 (let ((load-path | |
36920 | 34 (if (and (boundp 'byte-compile-dest-file) |
35 (stringp byte-compile-dest-file)) | |
36 (cons (file-name-directory byte-compile-dest-file) load-path) | |
26817 | 37 load-path))) |
36920 | 38 (require 'cc-bytecomp))) |
39 | |
40 (cc-require 'cc-defs) | |
41 (cc-require 'cc-vars) | |
42 (cc-require 'cc-langs) | |
43 (cc-require 'cc-engine) | |
44 | |
45 ;; Silence the compiler. | |
46 (cc-bytecomp-defvar delete-key-deletes-forward) ; XEmacs 20+ | |
47 (cc-bytecomp-defun delete-forward-p) ; XEmacs 21+ | |
48 (cc-bytecomp-obsolete-fun insert-and-inherit) ; Marked obsolete in XEmacs 19 | |
49 (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge | |
50 ; which looks at this. | |
18843
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
51 |
18720 | 52 |
53 (defun c-calculate-state (arg prevstate) | |
54 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If | |
55 ;; arg is nil or zero, toggle the state. If arg is negative, turn | |
56 ;; the state off, and if arg is positive, turn the state on | |
57 (if (or (not arg) | |
58 (zerop (setq arg (prefix-numeric-value arg)))) | |
59 (not prevstate) | |
60 (> arg 0))) | |
61 | |
62 ;; Auto-newline and hungry-delete | |
36920 | 63 (defun c-toggle-auto-state (&optional arg) |
18720 | 64 "Toggle auto-newline feature. |
36920 | 65 Optional numeric ARG, if supplied, turns on auto-newline when |
66 positive, turns it off when negative, and just toggles it when zero or | |
67 left out. | |
18720 | 68 |
69 When the auto-newline feature is enabled (as evidenced by the `/a' or | |
70 `/ah' on the modeline after the mode name) newlines are automatically | |
71 inserted after special characters such as brace, comma, semi-colon, | |
72 and colon." | |
73 (interactive "P") | |
74 (setq c-auto-newline (c-calculate-state arg c-auto-newline)) | |
75 (c-update-modeline) | |
76 (c-keep-region-active)) | |
77 | |
36920 | 78 (defun c-toggle-hungry-state (&optional arg) |
18720 | 79 "Toggle hungry-delete-key feature. |
36920 | 80 Optional numeric ARG, if supplied, turns on hungry-delete when |
81 positive, turns it off when negative, and just toggles it when zero or | |
82 left out. | |
18720 | 83 |
84 When the hungry-delete-key feature is enabled (as evidenced by the | |
85 `/h' or `/ah' on the modeline after the mode name) the delete key | |
86 gobbles all preceding whitespace in one fell swoop." | |
87 (interactive "P") | |
88 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key)) | |
89 (c-update-modeline) | |
90 (c-keep-region-active)) | |
91 | |
36920 | 92 (defun c-toggle-auto-hungry-state (&optional arg) |
18720 | 93 "Toggle auto-newline and hungry-delete-key features. |
36920 | 94 Optional numeric ARG, if supplied, turns on auto-newline and |
18720 | 95 hungry-delete when positive, turns them off when negative, and just |
36920 | 96 toggles them when zero or left out. |
18720 | 97 |
98 See `c-toggle-auto-state' and `c-toggle-hungry-state' for details." | |
99 (interactive "P") | |
100 (setq c-auto-newline (c-calculate-state arg c-auto-newline)) | |
101 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key)) | |
102 (c-update-modeline) | |
103 (c-keep-region-active)) | |
104 | |
105 | |
106 ;; Electric keys | |
107 | |
108 ;; Note: In XEmacs 20.3 the Delete and BackSpace keysyms have been | |
109 ;; separated and "\177" is no longer an alias for both keys. Also, | |
110 ;; the variable delete-key-deletes-forward controls in which direction | |
111 ;; the Delete keysym deletes characters. The functions | |
112 ;; c-electric-delete and c-electric-backspace attempt to deal with | |
113 ;; this new functionality. For Emacs 19 and XEmacs 19 backwards | |
114 ;; compatibility, the old behavior has moved to c-electric-backspace | |
115 ;; and c-backspace-function. | |
116 | |
117 (defun c-electric-backspace (arg) | |
118 "Deletes preceding character or whitespace. | |
119 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or | |
120 \"/ah\" string on the mode line, then all preceding whitespace is | |
121 consumed. If however an ARG is supplied, or `c-hungry-delete-key' is | |
122 nil, or point is inside a literal then the function in the variable | |
123 `c-backspace-function' is called. | |
124 | |
125 See also \\[c-electric-delete]." | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
126 (interactive "*P") |
18720 | 127 (if (or (not c-hungry-delete-key) |
128 arg | |
129 (c-in-literal)) | |
130 (funcall c-backspace-function (prefix-numeric-value arg)) | |
131 (let ((here (point))) | |
132 (skip-chars-backward " \t\n") | |
133 (if (/= (point) here) | |
134 (delete-region (point) here) | |
135 (funcall c-backspace-function 1) | |
136 )))) | |
137 | |
138 (defun c-electric-delete (arg) | |
139 "Deletes preceding or following character or whitespace. | |
140 | |
141 The behavior of this function depends on the variable | |
142 `delete-key-deletes-forward'. If this variable is nil (or does not | |
143 exist, as in older Emacsen), then this function behaves identical to | |
144 \\[c-electric-backspace]. | |
145 | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
146 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
|
147 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
|
148 `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
|
149 \"/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
|
150 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
|
151 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
|
152 `c-delete-function' is called." |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
153 (interactive "*P") |
24282 | 154 (if (or (and (fboundp 'delete-forward-p) ;XEmacs 21 |
155 (delete-forward-p)) | |
156 (and (boundp 'delete-key-deletes-forward) ;XEmacs 20 | |
157 delete-key-deletes-forward)) | |
18720 | 158 (if (or (not c-hungry-delete-key) |
159 arg | |
160 (c-in-literal)) | |
161 (funcall c-delete-function (prefix-numeric-value arg)) | |
162 (let ((here (point))) | |
163 (skip-chars-forward " \t\n") | |
164 (if (/= (point) here) | |
165 (delete-region (point) here) | |
166 (funcall c-delete-function 1)))) | |
167 ;; act just like c-electric-backspace | |
168 (c-electric-backspace arg))) | |
169 | |
170 (defun c-electric-pound (arg) | |
171 "Electric pound (`#') insertion. | |
172 Inserts a `#' character specially depending on the variable | |
173 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if | |
174 point is inside a literal, nothing special happens." | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
175 (interactive "*P") |
24282 | 176 (if (or arg |
177 (not (memq 'alignleft c-electric-pound-behavior)) | |
178 (save-excursion (skip-chars-backward " \t") (not (bolp))) | |
179 (c-in-literal)) | |
18720 | 180 ;; do nothing special |
181 (self-insert-command (prefix-numeric-value arg)) | |
182 ;; place the pound character at the left edge | |
183 (let ((pos (- (point-max) (point))) | |
184 (bolp (bolp))) | |
185 (beginning-of-line) | |
186 (delete-horizontal-space) | |
187 (insert-char last-command-char 1) | |
188 (and (not bolp) | |
189 (goto-char (- (point-max) pos))) | |
190 ))) | |
191 | |
192 (defun c-electric-brace (arg) | |
193 "Insert a brace. | |
194 | |
195 If the auto-newline feature is turned on, as evidenced by the \"/a\" | |
196 or \"/ah\" string on the mode line, newlines are inserted before and | |
197 after braces based on the value of `c-hanging-braces-alist'. | |
198 | |
199 Also, the line is re-indented unless a numeric ARG is supplied, there | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
200 are non-whitespace characters present on the line after the brace, the |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
201 brace is inserted inside a literal, or `c-syntactic-indentation' is |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
202 nil. |
24282 | 203 |
204 This function does various newline cleanups based on the value of | |
205 `c-cleanup-list'." | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
206 (interactive "*P") |
18720 | 207 (let* ((c-state-cache (c-parse-state)) |
208 (safepos (c-safe-position (point) c-state-cache)) | |
209 (literal (c-in-literal safepos))) | |
210 ;; if we're in a literal, or we're not at the end of the line, or | |
211 ;; a numeric arg is provided, or auto-newlining is turned off, | |
212 ;; then just insert the character. | |
24282 | 213 (if (or literal |
214 arg | |
18720 | 215 (not (looking-at "[ \t]*$"))) |
216 (self-insert-command (prefix-numeric-value arg)) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
217 (let* ((syms |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
218 ;; 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
|
219 ;; 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
|
220 ;; they should be added here as well. |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
221 '(class-open class-close defun-open defun-close |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
222 inline-open inline-close |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
223 brace-list-open brace-list-close |
24282 | 224 brace-list-intro brace-entry-open |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
225 block-open block-close |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
226 substatement-open statement-case-open |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
227 extern-lang-open extern-lang-close |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
228 namespace-open namespace-close |
24282 | 229 inexpr-class-open inexpr-class-close |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
230 )) |
18720 | 231 ;; we want to inhibit blinking the paren since this will |
232 ;; be most disruptive. we'll blink it ourselves later on | |
233 (old-blink-paren blink-paren-function) | |
234 blink-paren-function | |
235 (insertion-point (point)) | |
236 delete-temp-newline | |
19306
974544be1a17
(c-electric-brace): Make preserve-p nil at BOB.
Richard M. Stallman <rms@gnu.org>
parents:
19297
diff
changeset
|
237 (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
|
238 (eq ?\ (char-syntax (char-before))))) |
18720 | 239 ;; shut this up too |
240 (c-echo-syntactic-information-p nil) | |
241 (syntax (progn | |
242 ;; only insert a newline if there is | |
243 ;; non-whitespace behind us | |
244 (if (save-excursion | |
245 (skip-chars-backward " \t") | |
246 (not (bolp))) | |
247 (progn (newline) | |
248 (setq delete-temp-newline t))) | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
249 (if (eq last-command-char ?{) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
250 (setq c-state-cache (cons (point) c-state-cache))) |
18720 | 251 (self-insert-command (prefix-numeric-value arg)) |
252 ;; state cache doesn't change | |
253 (c-guess-basic-syntax))) | |
254 (newlines (and | |
255 c-auto-newline | |
24282 | 256 (or (c-lookup-lists |
257 syms | |
258 ;; Substitute inexpr-class and class-open | |
259 ;; or class-close with inexpr-class-open | |
260 ;; or inexpr-class-close. | |
261 (if (assq 'inexpr-class syntax) | |
262 (cond ((assq 'class-open syntax) | |
263 '((inexpr-class-open))) | |
264 ((assq 'class-close syntax) | |
265 '((inexpr-class-close))) | |
266 (t syntax)) | |
267 syntax) | |
268 c-hanging-braces-alist) | |
18720 | 269 '(ignore before after))))) |
24282 | 270 ;; Do not try to insert newlines around a special (Pike-style) |
271 ;; brace list. | |
272 (if (and c-special-brace-lists | |
273 (save-excursion | |
274 (c-safe (if (= (char-before) ?{) | |
275 (forward-char -1) | |
276 (c-forward-sexp -1)) | |
277 (c-looking-at-special-brace-list)))) | |
278 (setq newlines nil)) | |
18720 | 279 ;; If syntax is a function symbol, then call it using the |
280 ;; defined semantics. | |
281 (if (and (not (consp (cdr newlines))) | |
282 (functionp (cdr newlines))) | |
283 (let ((c-syntactic-context syntax)) | |
284 (setq newlines | |
285 (funcall (cdr newlines) (car newlines) insertion-point)))) | |
286 ;; does a newline go before the open brace? | |
287 (if (memq 'before newlines) | |
288 ;; we leave the newline we've put in there before, | |
289 ;; but we need to re-indent the line above | |
36920 | 290 (let (old-ind |
291 (old-point-max (point-max)) | |
292 (pos (- (point-max) (point))) | |
24282 | 293 (here (point))) |
18720 | 294 (forward-line -1) |
36920 | 295 (setq old-ind (c-point 'boi)) |
24282 | 296 (let ((c-state-cache (c-whack-state (point) c-state-cache))) |
297 ;; we may need to update the cache. this should | |
298 ;; still be faster than recalculating the state | |
299 ;; in many cases | |
300 (save-excursion | |
301 (save-restriction | |
302 (narrow-to-region here (point)) | |
303 (if (and (c-safe (progn (backward-up-list -1) t)) | |
304 (memq (char-before) '(?\) ?})) | |
305 (progn (widen) | |
306 (c-safe (progn (c-forward-sexp -1) | |
307 t)))) | |
308 (setq c-state-cache | |
309 (c-hack-state (point) 'open c-state-cache))))) | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
310 (if c-syntactic-indentation |
36920 | 311 (indent-according-to-mode))) |
312 (setq c-state-cache (c-adjust-state (c-point 'bol) old-point-max | |
313 (- (c-point 'boi) old-ind) | |
24282 | 314 c-state-cache)) |
18720 | 315 (goto-char (- (point-max) pos)) |
316 ;; if the buffer has changed due to the indentation, we | |
317 ;; need to recalculate syntax for the current line, but | |
318 ;; we won't need to update the state cache. | |
319 (if (/= (point) here) | |
320 (setq syntax (c-guess-basic-syntax)))) | |
321 ;; must remove the newline we just stuck in (if we really did it) | |
322 (and delete-temp-newline | |
323 (save-excursion | |
324 ;; if there is whitespace before point, then preserve | |
325 ;; at least one space. | |
326 (delete-indentation) | |
327 (just-one-space) | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
328 (setq c-state-cache (c-whack-state (point) c-state-cache)) |
18720 | 329 (if (not preserve-p) |
330 (delete-char -1)))) | |
331 ;; since we're hanging the brace, we need to recalculate | |
332 ;; syntax. Update the state to accurately reflect the | |
333 ;; beginning of the line. We punt if we cross any open or | |
334 ;; closed parens because its just too hard to modify the | |
335 ;; known state. This limitation will be fixed in v5. | |
336 (save-excursion | |
337 (let ((bol (c-point 'bol))) | |
338 (if (zerop (car (parse-partial-sexp bol (1- (point))))) | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
339 (setq syntax (c-guess-basic-syntax)) |
18720 | 340 ;; gotta punt. this requires some horrible kludgery |
341 (beginning-of-line) | |
36920 | 342 (setq c-state-cache nil |
343 c-state-cache (c-parse-state) | |
18720 | 344 syntax nil)))) |
345 ) | |
36920 | 346 ;; Now adjust the line's indentation. Don't update the state |
347 ;; cache since c-guess-basic-syntax isn't called when | |
348 ;; c-syntactic-context is set. | |
349 (let* ((old-ind (c-point 'boi)) | |
350 (old-point-max (point-max)) | |
351 (c-syntactic-context syntax)) | |
352 (indent-according-to-mode) | |
353 (setq c-state-cache (c-adjust-state (c-point 'bol) old-point-max | |
354 (- (c-point 'boi) old-ind) | |
24282 | 355 c-state-cache))) |
18720 | 356 ;; Do all appropriate clean ups |
357 (let ((here (point)) | |
358 (pos (- (point-max) (point))) | |
26817 | 359 mbeg mend tmp) |
18720 | 360 ;; clean up empty defun braces |
361 (if (and c-auto-newline | |
362 (memq 'empty-defun-braces c-cleanup-list) | |
363 (eq last-command-char ?\}) | |
364 (c-intersect-lists '(defun-close class-close inline-close) | |
365 syntax) | |
366 (progn | |
367 (forward-char -1) | |
368 (skip-chars-backward " \t\n") | |
369 (eq (char-before) ?\{)) | |
370 ;; make sure matching open brace isn't in a comment | |
371 (not (c-in-literal))) | |
372 (delete-region (point) (1- here))) | |
26817 | 373 ;; clean up brace-else-brace and brace-elseif-brace |
374 (when (and c-auto-newline | |
375 (eq last-command-char ?\{) | |
376 (not (c-in-literal))) | |
377 (cond | |
378 ((and (memq 'brace-else-brace c-cleanup-list) | |
18720 | 379 (re-search-backward "}[ \t\n]*else[ \t\n]*{" nil t) |
380 (progn | |
381 (setq mbeg (match-beginning 0) | |
382 mend (match-end 0)) | |
26817 | 383 (eq (match-end 0) here))) |
384 (delete-region mbeg mend) | |
385 (insert "} else {")) | |
386 ((and (memq 'brace-elseif-brace c-cleanup-list) | |
387 (progn | |
388 (goto-char (1- here)) | |
389 (setq mend (point)) | |
390 (skip-chars-backward " \t\n") | |
391 (setq mbeg (point)) | |
392 (eq (char-before) ?\))) | |
393 (= (c-backward-token-1 1 t) 0) | |
394 (eq (char-after) ?\() | |
395 (progn | |
396 (setq tmp (point)) | |
397 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*" | |
398 nil t)) | |
399 (eq (match-end 0) tmp)) | |
400 (delete-region mbeg mend) | |
401 (goto-char mbeg) | |
402 (insert " ")))) | |
18720 | 403 (goto-char (- (point-max) pos)) |
404 ) | |
405 ;; does a newline go after the brace? | |
406 (if (memq 'after newlines) | |
407 (progn | |
408 (newline) | |
409 ;; update on c-state-cache | |
410 (let* ((bufpos (- (point) 2)) | |
411 (which (if (eq (char-after bufpos) ?{) 'open 'close)) | |
412 (c-state-cache (c-hack-state bufpos which c-state-cache))) | |
36920 | 413 (indent-according-to-mode)))) |
18720 | 414 ;; blink the paren |
415 (and (eq last-command-char ?\}) | |
416 old-blink-paren | |
417 (save-excursion | |
418 (c-backward-syntactic-ws safepos) | |
419 (funcall old-blink-paren))) | |
420 )))) | |
24282 | 421 |
18720 | 422 (defun c-electric-slash (arg) |
423 "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
|
424 |
1982f8488449
(indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents:
19252
diff
changeset
|
425 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
|
426 |
1982f8488449
(indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents:
19252
diff
changeset
|
427 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
|
428 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
|
429 |
1982f8488449
(indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents:
19252
diff
changeset
|
430 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
|
431 comment. |
1982f8488449
(indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents:
19252
diff
changeset
|
432 |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
433 If a numeric ARG is supplied, point is inside a literal, or |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
434 `c-syntactic-indentation' is nil, indentation is inhibited." |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
435 (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
|
436 (let* ((ch (char-before)) |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
437 (indentp (and c-syntactic-indentation |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
438 (not arg) |
19297
1982f8488449
(indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents:
19252
diff
changeset
|
439 (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
|
440 (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
|
441 (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
|
442 (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
|
443 (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
|
444 )) |
1982f8488449
(indent-new-comment-line): Add advice for older Emacs versions if they
Richard M. Stallman <rms@gnu.org>
parents:
19252
diff
changeset
|
445 ;; 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
|
446 (c-echo-syntactic-information-p nil)) |
18720 | 447 (self-insert-command (prefix-numeric-value arg)) |
448 (if indentp | |
36920 | 449 (indent-according-to-mode)))) |
18720 | 450 |
451 (defun c-electric-star (arg) | |
452 "Insert a star character. | |
453 If the star is the second character of a C style comment introducing | |
454 construct, and we are on a comment-only-line, indent line as comment. | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
455 If a numeric ARG is supplied, point is inside a literal, or |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
456 `c-syntactic-indentation' is nil, indentation is inhibited." |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
457 (interactive "*P") |
18720 | 458 (self-insert-command (prefix-numeric-value arg)) |
459 ;; if we are in a literal, or if arg is given do not re-indent the | |
460 ;; current line, unless this star introduces a comment-only line. | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
461 (if (and c-syntactic-indentation |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
462 (not arg) |
18720 | 463 (memq (c-in-literal) '(c)) |
464 (eq (char-before) ?*) | |
465 (save-excursion | |
466 (forward-char -1) | |
467 (skip-chars-backward "*") | |
468 (if (eq (char-before) ?/) | |
469 (forward-char -1)) | |
470 (skip-chars-backward " \t") | |
471 (bolp))) | |
36920 | 472 (let (c-echo-syntactic-information-p) ; shut this up |
473 (indent-according-to-mode)) | |
18720 | 474 )) |
475 | |
476 (defun c-electric-semi&comma (arg) | |
477 "Insert a comma or semicolon. | |
478 When the auto-newline feature is turned on, as evidenced by the \"/a\" | |
479 or \"/ah\" string on the mode line, a newline might be inserted. See | |
480 the variable `c-hanging-semi&comma-criteria' for how newline insertion | |
481 is determined. | |
482 | |
483 When semicolon is inserted, the line is re-indented unless a numeric | |
484 arg is supplied, point is inside a literal, or there are | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
485 non-whitespace characters on the line following the semicolon, or |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
486 `c-syntactic-indentation' is nil. |
24282 | 487 |
488 Based on the value of `c-cleanup-list', this function cleans up commas | |
489 following brace lists and semicolons following defuns." | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
490 (interactive "*P") |
18720 | 491 (let* ((lim (c-most-enclosing-brace (c-parse-state))) |
492 (literal (c-in-literal lim)) | |
493 (here (point)) | |
494 ;; shut this up | |
495 (c-echo-syntactic-information-p nil)) | |
496 (if (or literal | |
497 arg | |
498 (not (looking-at "[ \t]*$"))) | |
499 (self-insert-command (prefix-numeric-value arg)) | |
500 ;; do some special stuff with the character | |
501 (self-insert-command (prefix-numeric-value arg)) | |
24282 | 502 ;; do all cleanups and newline insertions if c-auto-newline is |
503 ;; turned on | |
504 (if (not c-auto-newline) | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
505 (if c-syntactic-indentation |
36920 | 506 (indent-according-to-mode)) |
18720 | 507 ;; clean ups |
508 (let ((pos (- (point-max) (point)))) | |
509 (if (and (or (and | |
510 (eq last-command-char ?,) | |
511 (memq 'list-close-comma c-cleanup-list)) | |
512 (and | |
513 (eq last-command-char ?\;) | |
514 (memq 'defun-close-semi c-cleanup-list))) | |
515 (progn | |
516 (forward-char -1) | |
517 (skip-chars-backward " \t\n") | |
518 (eq (char-before) ?})) | |
519 ;; make sure matching open brace isn't in a comment | |
520 (not (c-in-literal lim))) | |
521 (delete-region (point) here)) | |
522 (goto-char (- (point-max) pos))) | |
523 ;; re-indent line | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
524 (if c-syntactic-indentation |
36920 | 525 (indent-according-to-mode)) |
18720 | 526 ;; check to see if a newline should be added |
527 (let ((criteria c-hanging-semi&comma-criteria) | |
528 answer add-newline-p) | |
529 (while criteria | |
530 (setq answer (funcall (car criteria))) | |
531 ;; only nil value means continue checking | |
532 (if (not answer) | |
533 (setq criteria (cdr criteria)) | |
534 (setq criteria nil) | |
535 ;; only 'stop specifically says do not add a newline | |
536 (setq add-newline-p (not (eq answer 'stop))) | |
537 )) | |
538 (if add-newline-p | |
539 (progn (newline) | |
36920 | 540 (indent-according-to-mode))) |
18720 | 541 ))))) |
542 | |
543 (defun c-electric-colon (arg) | |
544 "Insert a colon. | |
545 | |
546 If the auto-newline feature is turned on, as evidenced by the \"/a\" | |
547 or \"/ah\" string on the mode line, newlines are inserted before and | |
548 after colons based on the value of `c-hanging-colons-alist'. | |
549 | |
550 Also, the line is re-indented unless a numeric ARG is supplied, there | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
551 are non-whitespace characters present on the line after the colon, the |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
552 colon is inserted inside a literal, or `c-syntactic-indentation' is |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
553 nil. |
18720 | 554 |
555 This function cleans up double colon scope operators based on the | |
556 value of `c-cleanup-list'." | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
557 (interactive "*P") |
18720 | 558 (let* ((bod (c-point 'bod)) |
559 (literal (c-in-literal bod)) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
560 syntax newlines is-scope-op |
18720 | 561 ;; shut this up |
562 (c-echo-syntactic-information-p nil)) | |
563 (if (or literal | |
564 arg | |
565 (not (looking-at "[ \t]*$"))) | |
566 (self-insert-command (prefix-numeric-value arg)) | |
567 ;; insert the colon, then do any specified cleanups | |
568 (self-insert-command (prefix-numeric-value arg)) | |
569 (let ((pos (- (point-max) (point))) | |
570 (here (point))) | |
571 (if (and c-auto-newline | |
572 (memq 'scope-operator c-cleanup-list) | |
573 (eq (char-before) ?:) | |
574 (progn | |
575 (forward-char -1) | |
576 (skip-chars-backward " \t\n") | |
577 (eq (char-before) ?:)) | |
578 (not (c-in-literal)) | |
579 (not (eq (char-after (- (point) 2)) ?:))) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
580 (progn |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
581 (delete-region (point) (1- here)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
582 (setq is-scope-op t))) |
18720 | 583 (goto-char (- (point-max) pos))) |
584 ;; lets do some special stuff with the colon character | |
585 (setq syntax (c-guess-basic-syntax) | |
586 ;; some language elements can only be determined by | |
587 ;; checking the following line. Lets first look for ones | |
588 ;; that can be found when looking on the line with the | |
589 ;; colon | |
590 newlines | |
591 (and c-auto-newline | |
592 (or (c-lookup-lists '(case-label label access-label) | |
593 syntax c-hanging-colons-alist) | |
594 (c-lookup-lists '(member-init-intro inher-intro) | |
24282 | 595 (let ((buffer-undo-list t)) |
596 (insert "\n") | |
597 (unwind-protect | |
598 (c-guess-basic-syntax) | |
599 (delete-char -1))) | |
18720 | 600 c-hanging-colons-alist)))) |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
601 ;; indent the current line if it's done syntactically. |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
602 (if c-syntactic-indentation |
36920 | 603 (let ((c-syntactic-context syntax)) |
604 (indent-according-to-mode))) | |
18720 | 605 ;; does a newline go before the colon? Watch out for already |
606 ;; non-hung colons. However, we don't unhang them because that | |
607 ;; would be a cleanup (and anti-social). | |
608 (if (and (memq 'before newlines) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
609 (not is-scope-op) |
18720 | 610 (save-excursion |
611 (skip-chars-backward ": \t") | |
612 (not (bolp)))) | |
613 (let ((pos (- (point-max) (point)))) | |
614 (forward-char -1) | |
615 (newline) | |
36920 | 616 (indent-according-to-mode) |
18720 | 617 (goto-char (- (point-max) pos)))) |
618 ;; does a newline go after the colon? | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
619 (if (and (memq 'after (cdr-safe newlines)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
620 (not is-scope-op)) |
18720 | 621 (progn |
622 (newline) | |
36920 | 623 (indent-according-to-mode))) |
18720 | 624 ))) |
625 | |
626 (defun c-electric-lt-gt (arg) | |
627 "Insert a less-than, or greater-than character. | |
24282 | 628 The line will be re-indented if the character inserted is the second |
629 of a C++ style stream operator and the buffer is in C++ mode. | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
630 Exceptions are when a numeric argument is supplied, point is inside a |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
631 literal, or `c-syntactic-indentation' is nil, in which case the line |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
632 will not be re-indented." |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
633 (interactive "*P") |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
634 (let ((indentp (and c-syntactic-indentation |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
635 (not arg) |
18720 | 636 (eq (char-before) last-command-char) |
637 (not (c-in-literal)))) | |
638 ;; shut this up | |
639 (c-echo-syntactic-information-p nil)) | |
640 (self-insert-command (prefix-numeric-value arg)) | |
641 (if indentp | |
36920 | 642 (indent-according-to-mode)))) |
18720 | 643 |
24282 | 644 (defun c-electric-paren (arg) |
645 "Insert a parenthesis. | |
646 | |
36920 | 647 Some newline cleanups are done if appropriate; see the variable |
648 `c-cleanup-list'. | |
24282 | 649 |
650 Also, the line is re-indented unless a numeric ARG is supplied, there | |
26817 | 651 are non-whitespace characters present on the line after the |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
652 parenthesis, the parenthesis is inserted inside a literal, or |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
653 `c-syntactic-indentation' is nil." |
24282 | 654 (interactive "*P") |
655 (let (;; shut this up | |
656 (c-echo-syntactic-information-p nil)) | |
657 (if (or arg | |
658 (c-in-literal (c-point 'bod))) | |
659 (self-insert-command (prefix-numeric-value arg)) | |
660 ;; do some special stuff with the character | |
661 (let* (;; We want to inhibit blinking the paren since this will | |
662 ;; be most disruptive. We'll blink it ourselves | |
663 ;; afterwards. | |
664 (old-blink-paren blink-paren-function) | |
665 blink-paren-function) | |
666 (self-insert-command (prefix-numeric-value arg)) | |
36920 | 667 (when (looking-at "[ \t]*$") |
668 (if c-syntactic-indentation | |
669 (indent-according-to-mode)) | |
670 (when c-auto-newline | |
671 ;; Do all appropriate clean ups | |
672 (let ((here (point)) | |
673 (pos (- (point-max) (point))) | |
674 mbeg mend) | |
675 ;; clean up brace-elseif-brace | |
676 (if (and (memq 'brace-elseif-brace c-cleanup-list) | |
677 (eq last-command-char ?\() | |
678 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*(" | |
679 nil t) | |
680 (save-excursion | |
681 (setq mbeg (match-beginning 0) | |
682 mend (match-end 0)) | |
683 (= mend here)) | |
684 (not (c-in-literal))) | |
685 (progn | |
686 (delete-region mbeg mend) | |
687 (insert "} else if ("))) | |
688 ;; clean up brace-catch-brace | |
689 (if (and (memq 'brace-catch-brace c-cleanup-list) | |
690 (eq last-command-char ?\() | |
691 (re-search-backward "}[ \t\n]*catch[ \t\n]*(" nil t) | |
692 (save-excursion | |
693 (setq mbeg (match-beginning 0) | |
694 mend (match-end 0)) | |
695 (= mend here)) | |
696 (not (c-in-literal))) | |
697 (progn | |
698 (delete-region mbeg mend) | |
699 (insert "} catch ("))) | |
700 (goto-char (- (point-max) pos)) | |
701 ))) | |
702 (let (beg (end (1- (point)))) | |
703 (cond ((and (memq 'space-before-funcall c-cleanup-list) | |
704 (eq last-command-char ?\() | |
705 (save-excursion | |
706 (backward-char) | |
707 (skip-chars-backward " \t") | |
708 (setq beg (point)) | |
709 (c-on-identifier))) | |
710 (save-excursion | |
711 (delete-region beg end) | |
712 (goto-char beg) | |
713 (insert " "))) | |
714 ((and (memq 'compact-empty-funcall c-cleanup-list) | |
715 (eq last-command-char ?\)) | |
716 (save-excursion | |
717 (c-safe (backward-char 2)) | |
718 (when (looking-at "()") | |
719 (setq end (point)) | |
720 (skip-chars-backward " \t") | |
721 (setq beg (point)) | |
722 (c-on-identifier)))) | |
723 (delete-region beg end)))) | |
26817 | 724 (if old-blink-paren |
725 (funcall old-blink-paren)))))) | |
24282 | 726 |
36920 | 727 (defun c-electric-continued-statement () |
728 "Reindent the current line if appropriate. | |
729 | |
730 This function is used to reindent the line after a keyword which | |
731 continues an earlier statement is typed, e.g. an \"else\" or the | |
732 \"while\" in a do-while block. | |
733 | |
734 The line is reindented if there is nothing but whitespace before the | |
735 keyword on the line, the keyword is not inserted inside a literal, and | |
736 `c-syntactic-indentation' is non-nil." | |
737 (let (;; shut this up | |
738 (c-echo-syntactic-information-p nil)) | |
739 (when (and c-syntactic-indentation | |
740 (not (eq last-command-char ?_)) | |
741 (= (save-excursion | |
742 (skip-syntax-backward "w") | |
743 (point)) | |
744 (c-point 'boi)) | |
745 (not (c-in-literal (c-point 'bod)))) | |
746 (indent-according-to-mode)))) | |
18720 | 747 |
748 | |
749 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus | |
750 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com | |
751 (defun c-forward-into-nomenclature (&optional arg) | |
752 "Move forward to end of a nomenclature section or word. | |
753 With arg, to it arg times." | |
754 (interactive "p") | |
755 (let ((case-fold-search nil)) | |
756 (if (> arg 0) | |
757 (re-search-forward "\\W*\\([A-Z]*[a-z0-9]*\\)" (point-max) t arg) | |
758 (while (and (< arg 0) | |
759 (re-search-backward | |
760 "\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)" | |
761 (point-min) 0)) | |
762 (forward-char 1) | |
763 (setq arg (1+ arg))))) | |
764 (c-keep-region-active)) | |
765 | |
766 (defun c-backward-into-nomenclature (&optional arg) | |
767 "Move backward to beginning of a nomenclature section or word. | |
768 With optional ARG, move that many times. If ARG is negative, move | |
769 forward." | |
770 (interactive "p") | |
771 (c-forward-into-nomenclature (- arg)) | |
772 (c-keep-region-active)) | |
773 | |
774 (defun c-scope-operator () | |
775 "Insert a double colon scope operator at point. | |
776 No indentation or other \"electric\" behavior is performed." | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
777 (interactive "*") |
18720 | 778 (insert "::")) |
779 | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
780 (defun c-beginning-of-defun (&optional arg) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
781 "Move backward to the beginning of a defun. |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
782 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
|
783 means move forward to Nth following beginning of defun. |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
784 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
|
785 |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
786 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
|
787 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
|
788 defun." |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
789 (interactive "p") |
26817 | 790 (unless arg (setq arg 1)) |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
791 (if (< arg 0) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
792 (c-end-of-defun (- arg)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
793 (while (> arg 0) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
794 (let ((state (nreverse (c-parse-state))) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
795 prevbod bod) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
796 (while (and state (not bod)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
797 (setq bod (car state) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
798 state (cdr state)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
799 (if (consp bod) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
800 (setq prevbod (car bod) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
801 bod nil))) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
802 (cond |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
803 (bod (goto-char bod)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
804 (prevbod (goto-char prevbod)) |
26817 | 805 (t (goto-char (point-min)) |
806 (setq arg 0))) | |
807 (setq arg (1- arg)))) | |
808 (c-keep-region-active) | |
809 (= arg 0))) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
810 |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
811 (defun c-end-of-defun (&optional arg) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
812 "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
|
813 Negative argument -N means move back to Nth preceding end of defun. |
26817 | 814 Returns t unless search stops due to beginning or end of buffer. |
20912
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 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
|
817 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
|
818 (interactive "p") |
24282 | 819 (if (not arg) |
820 (setq arg 1)) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
821 (if (< arg 0) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
822 (c-beginning-of-defun (- arg)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
823 (while (> arg 0) |
26817 | 824 (let ((pos (point)) |
825 eol) | |
826 (while (and (c-safe (down-list 1) t) | |
827 (not (eq (char-before) ?{))) | |
828 ;; skip down into the next defun-block | |
829 (forward-char -1) | |
830 (c-forward-sexp)) | |
831 (c-beginning-of-defun 1) | |
832 (setq eol (c-point 'eol)) | |
833 (c-forward-sexp) | |
834 (if (< eol (point)) | |
835 ;; Don't move to next line for one line defuns. | |
836 (forward-line 1)) | |
837 (when (<= (point) pos) | |
838 (goto-char (point-max)) | |
839 (setq arg 0)) | |
840 (setq arg (1- arg)))) | |
841 (c-keep-region-active) | |
842 (= arg 0))) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
843 |
18720 | 844 |
845 (defun c-beginning-of-statement (&optional count lim sentence-flag) | |
846 "Go to the beginning of the innermost C statement. | |
847 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
|
848 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
|
849 preceding one, moving into nested blocks if necessary (use |
36920 | 850 \\[backward-sexp] to skip over a block). If within or next to a |
851 comment or multiline string, move by sentences instead of statements. | |
18720 | 852 |
853 When called from a program, this function takes 3 optional args: the | |
854 repetition count, a buffer position limit which is the farthest back | |
36920 | 855 to search for the syntactic context, and a flag saying whether to do |
856 sentence motion in or near comments and multiline strings." | |
18720 | 857 (interactive (list (prefix-numeric-value current-prefix-arg) |
858 nil t)) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
859 (let* ((count (or count 1)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
860 here |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
861 (range (c-collect-line-comments (c-literal-limits lim)))) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
862 (while (and (/= count 0) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
863 (or (not lim) (> (point) lim))) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
864 (setq here (point)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
865 (if (and (not range) sentence-flag) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
866 (save-excursion |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
867 ;; 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
|
868 (if (> count 0) |
36920 | 869 (if (c-forward-comment -1) |
870 (setq range (cons (point) | |
871 (progn (c-forward-comment 1) (point)))) | |
872 (skip-chars-backward " \t\n\r\f") | |
873 (setq range (point)) | |
874 (setq range | |
875 (if (eq (char-before) ?\") | |
876 (c-safe (c-backward-sexp 1) | |
877 (cons (point) range))))) | |
878 ;; skip-syntax-* doesn't count \n as whitespace.. | |
879 (skip-chars-forward " \t\n\r\f") | |
880 (if (eq (char-after) ?\") | |
881 (setq range (cons (point) | |
882 (progn | |
883 (c-forward-sexp 1) | |
884 (point)))) | |
885 (setq range (point)) | |
886 (setq range (if (c-forward-comment 1) | |
887 (cons range (point)) | |
888 nil)))) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
889 (setq range (c-collect-line-comments range)))) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
890 (if (and (< count 0) (= here (point-max))) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
891 ;; Special case because eob might be in a literal. |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
892 (setq range nil)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
893 (if range |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
894 (if (and sentence-flag |
36920 | 895 (or (/= (char-syntax (char-after (car range))) ?\") |
896 ;; Only visit a string if it spans more than one line. | |
897 (save-excursion | |
898 (goto-char (car range)) | |
899 (skip-chars-forward "^\n" (cdr range)) | |
900 (< (point) (cdr range))))) | |
26817 | 901 (let* ((lit-type (c-literal-type range)) |
36920 | 902 (line-prefix (concat "[ \t]*\\(" |
903 c-current-comment-prefix | |
904 "\\)[ \t]*")) | |
905 (beg (if (eq lit-type 'string) | |
906 (1+ (car range)) | |
907 (save-excursion | |
908 (goto-char (car range)) | |
909 (max (progn | |
910 (looking-at comment-start-skip) | |
911 (match-end 0)) | |
912 (progn | |
913 (looking-at line-prefix) | |
914 (match-end 0)))))) | |
915 (end (- (cdr range) (if (eq lit-type 'c) 2 1))) | |
916 (beg-of-para (if (eq lit-type 'string) | |
917 (lambda ()) | |
918 (lambda () | |
919 (beginning-of-line) | |
920 (if (looking-at line-prefix) | |
921 (goto-char (match-end 0))))))) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
922 (save-restriction |
36920 | 923 ;; Move by sentence, but not past the limit of the |
924 ;; literal, narrowed to the appropriate | |
925 ;; paragraph(s). | |
926 (narrow-to-region (save-excursion | |
927 (let ((pos (min here end))) | |
928 (goto-char pos) | |
929 (forward-paragraph -1) | |
930 (if (looking-at paragraph-separate) | |
931 (forward-line)) | |
932 (when (> (point) beg) | |
933 (funcall beg-of-para) | |
934 (when (>= (point) pos) | |
935 (forward-paragraph -2) | |
936 (funcall beg-of-para))) | |
937 (max (point) beg))) | |
938 end) | |
26817 | 939 (c-safe (forward-sentence (if (< count 0) 1 -1))) |
940 (if (and (memq lit-type '(c c++)) | |
941 ;; Check if we stopped due to a comment | |
942 ;; prefix and not a sentence end. | |
36920 | 943 (/= (point) (point-min)) |
944 (/= (point) (point-max)) | |
26817 | 945 (save-excursion |
946 (beginning-of-line) | |
36920 | 947 (looking-at line-prefix)) |
26817 | 948 (>= (point) (match-beginning 0)) |
949 (/= (match-beginning 1) (match-end 1)) | |
950 (or (< (point) (match-end 0)) | |
951 (and | |
952 (= (point) (match-end 0)) | |
953 ;; The comment prefix may contain | |
954 ;; characters that is regarded as end | |
955 ;; of sentence. | |
956 (or (eolp) | |
957 (and | |
958 (save-excursion | |
959 (forward-paragraph -1) | |
960 (< (point) (match-beginning 0))) | |
961 (save-excursion | |
962 (beginning-of-line) | |
963 (or (not (re-search-backward | |
964 sentence-end | |
965 (c-point 'bopl) | |
966 t)) | |
967 (< (match-end 0) | |
968 (c-point 'eol))))))))) | |
969 (setq count (+ count (if (< count 0) -1 1))) | |
970 (if (< count 0) | |
971 (progn | |
972 ;; In block comments, if there's only | |
973 ;; horizontal ws between the text and the | |
974 ;; comment ender, stop before it. Stop after | |
975 ;; the ender if there's either nothing or | |
976 ;; newlines between. | |
36920 | 977 (when (and (eq lit-type 'c) |
978 (eq (point) (point-max))) | |
26817 | 979 (widen) |
36920 | 980 (when (or (= (skip-chars-backward " \t") 0) |
981 (eq (point) (point-max)) | |
982 (bolp)) | |
26817 | 983 (goto-char (cdr range))))) |
36920 | 984 (when (and (eq (point) (point-min)) |
985 (looking-at "[ \t]*$")) | |
26817 | 986 ;; Stop before instead of after the comment |
987 ;; starter if nothing follows it. | |
988 (widen) | |
36920 | 989 (goto-char (car range)) |
990 (if (and (eq lit-type 'string) (/= (point) here)) | |
991 (setq count (1+ count) | |
992 range nil)))))) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
993 ;; See if we should escape the literal. |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
994 (if (> count 0) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
995 (if (< (point) here) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
996 (setq count (1- count)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
997 (goto-char (car range)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
998 (setq range nil)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
999 (if (> (point) here) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1000 (setq count (1+ count)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1001 (goto-char (cdr range)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1002 (setq range nil)))) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1003 (goto-char (if (> count 0) (car range) (cdr range))) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1004 (setq range nil)) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1005 ;; Below we do approximately the same as |
26817 | 1006 ;; c-beginning-of-statement-1 and c-end-of-statement-1, and |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1007 ;; 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
|
1008 ;; lot in cc-engine. |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1009 (goto-char here) |
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1010 (if (> count 0) |
26817 | 1011 (condition-case nil |
1012 ;; Stop before `{' and after `;', `{', `}' and `};' | |
1013 ;; when not followed by `}' or `)', but on the other | |
1014 ;; side of the syntactic ws. Move by sexps and move | |
36920 | 1015 ;; into parens. Also stop before `#' when it's at boi |
26817 | 1016 ;; on a line. |
36920 | 1017 (let ((literal-pos (not sentence-flag)) |
26817 | 1018 (large-enough (- (point-max))) |
1019 last last-below-line) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1020 (catch 'done |
26817 | 1021 (while t |
1022 (setq last (point)) | |
36920 | 1023 (when (and (or (eq (char-after) ?\{) |
1024 (and (eq (char-after) ?#) | |
1025 (eq (point) (c-point 'boi))) | |
1026 ) | |
1027 (/= here last)) | |
26817 | 1028 (unless (and c-special-brace-lists |
1029 (eq (char-after) ?{) | |
1030 (c-looking-at-special-brace-list)) | |
1031 (if (and (eq (char-after) ?#) | |
1032 (numberp last-below-line) | |
1033 (not (eq last-below-line here))) | |
1034 (goto-char last-below-line)) | |
1035 (throw 'done t))) | |
36920 | 1036 (if literal-pos |
26817 | 1037 (c-forward-comment large-enough) |
1038 (when (c-forward-comment -1) | |
1039 ;; Record position of first comment. | |
1040 (save-excursion | |
1041 (c-forward-comment 1) | |
36920 | 1042 (setq literal-pos (point))) |
26817 | 1043 (c-forward-comment large-enough))) |
1044 (unless last-below-line | |
1045 (if (save-excursion | |
1046 (re-search-forward "\\(^\\|[^\\]\\)$" last t)) | |
1047 (setq last-below-line last))) | |
1048 (cond ((bobp) ; Must handle bob specially. | |
1049 (if (= here last) | |
1050 (throw 'done t) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1051 (goto-char last) |
26817 | 1052 (throw 'done t))) |
1053 ((progn (backward-char) | |
1054 (looking-at "[;{}]")) | |
1055 (if (and c-special-brace-lists | |
1056 (eq (char-after) ?{) | |
1057 (c-looking-at-special-brace-list)) | |
1058 (skip-syntax-backward "w_") ; Speedup only. | |
1059 (if (or (= here last) | |
1060 (memq (char-after last) '(?\) ?}))) | |
1061 (if (and (eq (char-before) ?}) | |
1062 (eq (char-after) ?\;)) | |
1063 (backward-char)) | |
1064 (goto-char last) | |
1065 (throw 'done t)))) | |
1066 ((= (char-syntax (char-after)) ?\") | |
36920 | 1067 (let ((end (point))) |
1068 (forward-char) | |
1069 (c-backward-sexp) | |
1070 (save-excursion | |
1071 (skip-chars-forward "^\n" end) | |
1072 (when (< (point) end) | |
1073 ;; Break at multiline string. | |
1074 (setq literal-pos (1+ end)) | |
1075 (throw 'done t))))) | |
26817 | 1076 (t (skip-syntax-backward "w_")) ; Speedup only. |
1077 ))) | |
36920 | 1078 (if (and (numberp literal-pos) |
1079 (< (point) literal-pos)) | |
1080 ;; We jumped over a comment or string that | |
1081 ;; should be investigated. | |
1082 (goto-char literal-pos) | |
26817 | 1083 (setq count (1- count)))) |
1084 (error | |
1085 (goto-char (point-min)) | |
1086 (setq count 0))) | |
1087 (condition-case nil | |
36920 | 1088 ;; Stop before `{', `}', and `#' when it's at boi on a |
26817 | 1089 ;; line, but on the other side of the syntactic ws, and |
1090 ;; after `;', `}' and `};'. Only stop before `{' if at | |
1091 ;; top level or inside braces, though. Move by sexps | |
1092 ;; and move into parens. Also stop at eol of lines | |
36920 | 1093 ;; with `#' at the boi. |
1094 (let ((literal-pos (not sentence-flag)) | |
26817 | 1095 (large-enough (point-max)) |
1096 last) | |
1097 (catch 'done | |
1098 (while t | |
1099 (setq last (point)) | |
36920 | 1100 (if literal-pos |
26817 | 1101 (c-forward-comment large-enough) |
1102 (if (progn | |
1103 (skip-chars-forward " \t\n\r\f") | |
1104 ;; Record position of first comment. | |
36920 | 1105 (setq literal-pos (point)) |
26817 | 1106 (c-forward-comment 1)) |
1107 (c-forward-comment large-enough) | |
36920 | 1108 (setq literal-pos nil))) |
26817 | 1109 (cond ((and (eq (char-after) ?{) |
1110 (not (and c-special-brace-lists | |
1111 (c-looking-at-special-brace-list))) | |
1112 (/= here last) | |
1113 (save-excursion | |
1114 (or (not (c-safe (up-list -1) t)) | |
1115 (= (char-after) ?{)))) | |
1116 (goto-char last) | |
1117 (throw 'done t)) | |
1118 ((and c-special-brace-lists | |
1119 (eq (char-after) ?}) | |
1120 (save-excursion | |
1121 (and (c-safe (up-list -1) t) | |
1122 (c-looking-at-special-brace-list)))) | |
1123 (forward-char 1) | |
1124 (skip-syntax-forward "w_")) ; Speedup only. | |
1125 ((and (eq (char-after) ?}) | |
1126 (/= here last)) | |
1127 (goto-char last) | |
1128 (throw 'done t)) | |
36920 | 1129 ((and (eq (char-after) ?#) |
1130 (= (point) (c-point 'boi))) | |
26817 | 1131 (if (= here last) |
1132 (or (re-search-forward "\\(^\\|[^\\]\\)$" nil t) | |
1133 (goto-char (point-max))) | |
1134 (goto-char last)) | |
1135 (throw 'done t)) | |
1136 ((looking-at ";\\|};?") | |
1137 (goto-char (match-end 0)) | |
1138 (throw 'done t)) | |
1139 ((= (char-syntax (char-after)) ?\") | |
36920 | 1140 (let ((beg (point))) |
1141 (c-forward-sexp) | |
1142 (save-excursion | |
1143 (skip-chars-backward "^\n" beg) | |
1144 (when (> (point) beg) | |
1145 ;; Break at multiline string. | |
1146 (setq literal-pos beg) | |
1147 (throw 'done t))))) | |
26817 | 1148 (t |
1149 (forward-char 1) | |
1150 (skip-syntax-forward "w_")) ; Speedup only. | |
1151 ))) | |
36920 | 1152 (if (and (numberp literal-pos) |
1153 (> (point) literal-pos)) | |
26817 | 1154 ;; We jumped over a comment that should be investigated. |
36920 | 1155 (goto-char literal-pos) |
26817 | 1156 (setq count (1+ count)))) |
1157 (error | |
1158 (goto-char (point-max)) | |
1159 (setq count 0))) | |
1160 )) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1161 ;; If we haven't moved we're near a buffer limit. |
26817 | 1162 (when (and (not (zerop count)) (= (point) here)) |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1163 (goto-char (if (> count 0) (point-min) (point-max))) |
36920 | 1164 (setq count 0)))) |
18720 | 1165 (c-keep-region-active)) |
1166 | |
1167 (defun c-end-of-statement (&optional count lim sentence-flag) | |
1168 "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
|
1169 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
|
1170 of the next statement if already at end, and move into nested blocks |
36920 | 1171 \(use \\[forward-sexp] to skip over a block). If within or next to a |
1172 comment or multiline string, move by sentences instead of statements. | |
18720 | 1173 |
1174 When called from a program, this function takes 3 optional args: the | |
1175 repetition count, a buffer position limit which is the farthest back | |
36920 | 1176 to search for the syntactic context, and a flag saying whether to do |
1177 sentence motion in or near comments and multiline strings." | |
18720 | 1178 (interactive (list (prefix-numeric-value current-prefix-arg) |
1179 nil t)) | |
1180 (c-beginning-of-statement (- (or count 1)) lim sentence-flag) | |
1181 (c-keep-region-active)) | |
1182 | |
1183 | |
1184 ;; set up electric character functions to work with pending-del, | |
1185 ;; (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
|
1186 ;; the functions which delete, which gets 'supersede. |
18720 | 1187 (mapcar |
1188 (function | |
1189 (lambda (sym) | |
1190 (put sym 'delete-selection t) ; for delsel (Emacs) | |
1191 (put sym 'pending-delete t))) ; for pending-del (XEmacs) | |
1192 '(c-electric-pound | |
1193 c-electric-brace | |
1194 c-electric-slash | |
1195 c-electric-star | |
1196 c-electric-semi&comma | |
1197 c-electric-lt-gt | |
24282 | 1198 c-electric-colon |
1199 c-electric-paren)) | |
18843
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
1200 (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
|
1201 (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
|
1202 (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
|
1203 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del |
18720 | 1204 |
1205 | |
1206 ;; This is used by indent-for-comment to decide how much to indent a | |
1207 ;; comment in C code based on its context. | |
1208 (defun c-comment-indent () | |
1209 (if (looking-at (concat "^\\(" c-comment-start-regexp "\\)")) | |
1210 0 ;Existing comment at bol stays there. | |
1211 (let ((opoint (point)) | |
1212 placeholder) | |
1213 (save-excursion | |
1214 (beginning-of-line) | |
1215 (cond | |
1216 ;; CASE 1: A comment following a solitary close-brace should | |
1217 ;; have only one space. | |
1218 ((looking-at (concat "[ \t]*}[ \t]*\\($\\|" | |
1219 c-comment-start-regexp | |
1220 "\\)")) | |
1221 (search-forward "}") | |
1222 (1+ (current-column))) | |
1223 ;; CASE 2: 2 spaces after #endif | |
36920 | 1224 ((or (looking-at "[ \t]*#[ \t]*endif[ \t]*") |
1225 (looking-at "[ \t]*#[ \t]*else[ \t]*")) | |
18720 | 1226 7) |
24282 | 1227 ;; CASE 3: when c-indent-comments-syntactically-p is t, |
1228 ;; calculate the offset according to c-offsets-alist. | |
1229 ;; E.g. identical to hitting TAB. | |
18720 | 1230 ((and c-indent-comments-syntactically-p |
1231 (save-excursion | |
1232 (skip-chars-forward " \t") | |
24282 | 1233 (or (looking-at c-comment-start-regexp) |
18720 | 1234 (eolp)))) |
1235 (let ((syntax (c-guess-basic-syntax))) | |
1236 ;; BOGOSITY ALERT: if we're looking at the eol, its | |
1237 ;; because indent-for-comment hasn't put the comment-start | |
1238 ;; in the buffer yet. this will screw up the syntactic | |
1239 ;; analysis so we kludge in the necessary info. Another | |
1240 ;; kludge is that if we're at the bol, then we really want | |
1241 ;; to ignore any anchoring as specified by | |
1242 ;; c-comment-only-line-offset since it doesn't apply here. | |
1243 (if (save-excursion | |
36920 | 1244 (back-to-indentation) |
18720 | 1245 (eolp)) |
1246 (c-add-syntax 'comment-intro)) | |
1247 (let ((c-comment-only-line-offset | |
1248 (if (consp c-comment-only-line-offset) | |
1249 c-comment-only-line-offset | |
1250 (cons c-comment-only-line-offset | |
1251 c-comment-only-line-offset)))) | |
36920 | 1252 (c-get-syntactic-indentation syntax)))) |
26817 | 1253 ;; CASE 4: If previous line is a comment-only line, use its |
1254 ;; indentation if it's greater than comment-column. Leave at | |
1255 ;; least one space between the comment and the last nonblank | |
1256 ;; character in any case. | |
18720 | 1257 ((save-excursion |
1258 (beginning-of-line) | |
1259 (and (not (bobp)) | |
1260 (forward-line -1)) | |
1261 (skip-chars-forward " \t") | |
1262 (prog1 | |
1263 (looking-at c-comment-start-regexp) | |
26817 | 1264 (setq placeholder (current-column)))) |
1265 (goto-char opoint) | |
1266 (skip-chars-backward " \t") | |
1267 (max (if (bolp) 0 (1+ (current-column))) | |
1268 placeholder | |
1269 comment-column)) | |
18720 | 1270 ;; CASE 5: If comment-column is 0, and nothing but space |
1271 ;; before the comment, align it at 0 rather than 1. | |
1272 ((progn | |
1273 (goto-char opoint) | |
1274 (skip-chars-backward " \t") | |
1275 (and (= comment-column 0) (bolp))) | |
1276 0) | |
1277 ;; CASE 6: indent at comment column except leave at least one | |
1278 ;; space. | |
1279 (t (max (1+ (current-column)) | |
1280 comment-column)) | |
1281 ))))) | |
1282 | |
19804
37e25ff5a7f1
(c-beginning-of-statement): Fixes in sentence movement to properly
Richard M. Stallman <rms@gnu.org>
parents:
19378
diff
changeset
|
1283 |
18720 | 1284 ;; used by outline-minor-mode |
1285 (defun c-outline-level () | |
37199
22964e38f4c2
(c-outline-level):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
36920
diff
changeset
|
1286 ;; This so that `current-column' DTRT in otherwise-hidden text. |
22964e38f4c2
(c-outline-level):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
36920
diff
changeset
|
1287 (let (buffer-invisibility-spec) |
22964e38f4c2
(c-outline-level):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
36920
diff
changeset
|
1288 (save-excursion |
22964e38f4c2
(c-outline-level):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
36920
diff
changeset
|
1289 (skip-chars-forward "\t ") |
22964e38f4c2
(c-outline-level):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
36920
diff
changeset
|
1290 (current-column)))) |
18720 | 1291 |
1292 | |
1293 (defun c-up-conditional (count) | |
1294 "Move back to the containing preprocessor conditional, leaving mark behind. | |
1295 A prefix argument acts as a repeat count. With a negative argument, | |
1296 move forward to the end of the containing preprocessor conditional. | |
26817 | 1297 |
1298 `#elif' is treated like `#else' followed by `#if', so the function | |
1299 stops at them when going backward, but not when going forward." | |
18720 | 1300 (interactive "p") |
26817 | 1301 (c-forward-conditional (- count) -1) |
1302 (c-keep-region-active)) | |
1303 | |
1304 (defun c-up-conditional-with-else (count) | |
1305 "Move back to the containing preprocessor conditional, including `#else'. | |
1306 Just like `c-up-conditional', except it also stops at `#else' | |
1307 directives." | |
1308 (interactive "p") | |
1309 (c-forward-conditional (- count) -1 t) | |
18720 | 1310 (c-keep-region-active)) |
1311 | |
26817 | 1312 (defun c-down-conditional (count) |
1313 "Move forward into the next preprocessor conditional, leaving mark behind. | |
1314 A prefix argument acts as a repeat count. With a negative argument, | |
1315 move backward into the previous preprocessor conditional. | |
1316 | |
1317 `#elif' is treated like `#else' followed by `#if', so the function | |
1318 stops at them when going forward, but not when going backward." | |
1319 (interactive "p") | |
1320 (c-forward-conditional count 1) | |
1321 (c-keep-region-active)) | |
1322 | |
1323 (defun c-down-conditional-with-else (count) | |
1324 "Move forward into the next preprocessor conditional, including `#else'. | |
1325 Just like `c-down-conditional', except it also stops at `#else' | |
1326 directives." | |
1327 (interactive "p") | |
1328 (c-forward-conditional count 1 t) | |
1329 (c-keep-region-active)) | |
1330 | |
1331 (defun c-backward-conditional (count &optional target-depth with-else) | |
18720 | 1332 "Move back across a preprocessor conditional, leaving mark behind. |
1333 A prefix argument acts as a repeat count. With a negative argument, | |
1334 move forward across a preprocessor conditional." | |
1335 (interactive "p") | |
26817 | 1336 (c-forward-conditional (- count) target-depth with-else) |
18720 | 1337 (c-keep-region-active)) |
1338 | |
26817 | 1339 (defun c-forward-conditional (count &optional target-depth with-else) |
18720 | 1340 "Move forward across a preprocessor conditional, leaving mark behind. |
1341 A prefix argument acts as a repeat count. With a negative argument, | |
26817 | 1342 move backward across a preprocessor conditional. |
1343 | |
1344 `#elif' is treated like `#else' followed by `#if', except that the | |
1345 nesting level isn't changed when tracking subconditionals. | |
1346 | |
1347 The optional argument TARGET-DEPTH specifies the wanted nesting depth | |
1348 after each scan. I.e. if TARGET-DEPTH is -1, the function will move | |
1349 out of the enclosing conditional. A non-integer non-nil TARGET-DEPTH | |
1350 counts as -1. | |
1351 | |
1352 If the optional argument WITH-ELSE is non-nil, `#else' directives are | |
1353 treated as conditional clause limits. Normally they are ignored." | |
18720 | 1354 (interactive "p") |
1355 (let* ((forward (> count 0)) | |
1356 (increment (if forward -1 1)) | |
1357 (search-function (if forward 're-search-forward 're-search-backward)) | |
1358 (new)) | |
26817 | 1359 (unless (integerp target-depth) |
1360 (setq target-depth (if target-depth -1 0))) | |
18720 | 1361 (save-excursion |
1362 (while (/= count 0) | |
26817 | 1363 (let ((depth 0) |
1364 ;; subdepth is the depth in "uninteresting" subtrees, | |
1365 ;; i.e. those that takes us farther from the target | |
1366 ;; depth instead of closer. | |
1367 (subdepth 0) | |
1368 found) | |
18720 | 1369 (save-excursion |
1370 ;; Find the "next" significant line in the proper direction. | |
1371 (while (and (not found) | |
1372 ;; Rather than searching for a # sign that | |
1373 ;; comes at the beginning of a line aside from | |
1374 ;; whitespace, search first for a string | |
1375 ;; starting with # sign. Then verify what | |
1376 ;; precedes it. This is faster on account of | |
1377 ;; the fastmap feature of the regexp matcher. | |
1378 (funcall search-function | |
26817 | 1379 "#[ \t]*\\(if\\|elif\\|endif\\|else\\)" |
18720 | 1380 nil t)) |
1381 (beginning-of-line) | |
1382 ;; Now verify it is really a preproc line. | |
26817 | 1383 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)") |
1384 (let (dchange (directive (match-string 1))) | |
1385 (cond ((string= directive "if") | |
1386 (setq dchange (- increment))) | |
1387 ((string= directive "endif") | |
1388 (setq dchange increment)) | |
1389 ((= subdepth 0) | |
1390 ;; When we're not in an "uninteresting" | |
1391 ;; subtree, we might want to act on "elif" | |
1392 ;; and "else" too. | |
1393 (if (cond (with-else | |
1394 ;; Always move toward the target depth. | |
1395 (setq dchange | |
1396 (if (> target-depth 0) 1 -1))) | |
1397 ((string= directive "elif") | |
1398 (setq dchange (- increment)))) | |
1399 ;; Ignore the change if it'd take us | |
1400 ;; into an "uninteresting" subtree. | |
1401 (if (eq (> dchange 0) (<= target-depth 0)) | |
1402 (setq dchange nil))))) | |
1403 (when dchange | |
1404 (when (or (/= subdepth 0) | |
1405 (eq (> dchange 0) (<= target-depth 0))) | |
1406 (setq subdepth (+ subdepth dchange))) | |
1407 (setq depth (+ depth dchange)) | |
1408 ;; If we are trying to move across, and we find an | |
1409 ;; end before we find a beginning, get an error. | |
1410 (if (and (< depth target-depth) (< dchange 0)) | |
1411 (error (if forward | |
1412 "No following conditional at this level" | |
1413 "No previous conditional at this level")))) | |
18720 | 1414 ;; When searching forward, start from next line so |
1415 ;; that we don't find the same line again. | |
1416 (if forward (forward-line 1)) | |
26817 | 1417 ;; We found something if we've arrived at the |
1418 ;; target depth. | |
1419 (if (and dchange (= depth target-depth)) | |
18720 | 1420 (setq found (point)))) |
1421 ;; else | |
26817 | 1422 (if forward (forward-line 1))))) |
18720 | 1423 (or found |
1424 (error "No containing preprocessor conditional")) | |
1425 (goto-char (setq new found))) | |
1426 (setq count (+ count increment)))) | |
1427 (push-mark) | |
1428 (goto-char new)) | |
1429 (c-keep-region-active)) | |
1430 | |
1431 | |
1432 ;; commands to indent lines, regions, defuns, and expressions | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1433 (defun c-indent-command (&optional arg) |
18720 | 1434 "Indent current line as C code, and/or insert some whitespace. |
1435 | |
1436 If `c-tab-always-indent' is t, always just indent the current line. | |
1437 If nil, indent the current line only if point is at the left margin or | |
1438 in the line's indentation; otherwise insert some whitespace[*]. If | |
1439 other than nil or t, then some whitespace[*] is inserted only within | |
1440 literals (comments and strings) and inside preprocessor directives, | |
1441 but the line is always reindented. | |
1442 | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1443 If `c-syntactic-indentation' is t, indentation is done according to |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1444 the syntactic context. If it's nil, the line is just indented one |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1445 step according to `c-basic-offset'. In this mode, a numeric argument |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1446 indents a number of such steps, positive or negative, and an empty |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1447 prefix argument is equivalent to -1. |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1448 |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1449 If `c-syntactic-indentation' is t, then a numeric argument, regardless |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1450 of its value, means indent rigidly all the lines of the expression |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1451 starting after point so that this line becomes properly indented. The |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1452 relative indentation among the lines of the expression is preserved. |
18720 | 1453 |
1454 [*] The amount and kind of whitespace inserted is controlled by the | |
1455 variable `c-insert-tab-function', which is called to do the actual | |
1456 insertion of whitespace. Normally the function in this variable | |
1457 just inserts a tab character, or the equivalent number of spaces, | |
1458 depending on the variable `indent-tabs-mode'." | |
1459 | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1460 (interactive "p") |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1461 (let ((bod (c-point 'bod)) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1462 (indent-function |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1463 (if c-syntactic-indentation |
36920 | 1464 (symbol-function 'indent-according-to-mode) |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1465 (lambda () |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1466 (let ((steps (cond ((not current-prefix-arg) 1) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1467 ((equal current-prefix-arg '(4)) -1) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1468 (t arg)))) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1469 (c-shift-line-indentation (* steps c-basic-offset))) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1470 )))) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1471 (if (and c-syntactic-indentation current-prefix-arg) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1472 ;; If c-syntactic-indentation and got arg, always indent this |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1473 ;; line as C and shift remaining lines of expression the same |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1474 ;; amount. |
36920 | 1475 (let ((shift-amt (save-excursion |
1476 (back-to-indentation) | |
1477 (current-column))) | |
18720 | 1478 beg end) |
36920 | 1479 (c-indent-line) |
1480 (setq shift-amt (- (save-excursion | |
1481 (back-to-indentation) | |
1482 (current-column)) | |
1483 shift-amt)) | |
18720 | 1484 (save-excursion |
1485 (if (eq c-tab-always-indent t) | |
1486 (beginning-of-line)) | |
1487 (setq beg (point)) | |
24282 | 1488 (c-forward-sexp 1) |
18720 | 1489 (setq end (point)) |
1490 (goto-char beg) | |
1491 (forward-line 1) | |
1492 (setq beg (point))) | |
1493 (if (> end beg) | |
36920 | 1494 (indent-code-rigidly beg end shift-amt "#"))) |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1495 ;; Else use c-tab-always-indent to determine behavior. |
18720 | 1496 (cond |
1497 ;; CASE 1: indent when at column zero or in lines indentation, | |
1498 ;; otherwise insert a tab | |
1499 ((not c-tab-always-indent) | |
1500 (if (save-excursion | |
1501 (skip-chars-backward " \t") | |
1502 (not (bolp))) | |
1503 (funcall c-insert-tab-function) | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1504 (funcall indent-function))) |
18720 | 1505 ;; CASE 2: just indent the line |
1506 ((eq c-tab-always-indent t) | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1507 (funcall indent-function)) |
18720 | 1508 ;; CASE 3: if in a literal, insert a tab, but always indent the |
1509 ;; line | |
1510 (t | |
1511 (if (c-in-literal bod) | |
1512 (funcall c-insert-tab-function)) | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1513 (funcall indent-function) |
18720 | 1514 ))))) |
1515 | |
1516 (defun c-indent-exp (&optional shutup-p) | |
36920 | 1517 "Indent each line in the balanced expression following point syntactically. |
1518 If optional SHUTUP-P is non-nil, no errors are signalled if no | |
1519 balanced expression is found." | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1520 (interactive "*P") |
26817 | 1521 (let ((here (point-marker)) |
36920 | 1522 end) |
26817 | 1523 (set-marker-insertion-type here t) |
18720 | 1524 (unwind-protect |
36920 | 1525 (let ((start (progn |
18720 | 1526 ;; try to be smarter about finding the range of |
1527 ;; lines to indent. skip all following | |
36920 | 1528 ;; whitespace, then try to find any |
1529 ;; opening paren on the current line | |
18720 | 1530 (skip-chars-forward " \t\n") |
36920 | 1531 (save-restriction |
1532 (narrow-to-region (point-min) (c-point 'eol)) | |
1533 (c-safe (1- (scan-lists (point) 1 -1))))))) | |
18720 | 1534 ;; find balanced expression end |
24282 | 1535 (setq end (and (c-safe (progn (c-forward-sexp 1) t)) |
36920 | 1536 (point))) |
18720 | 1537 ;; sanity check |
36920 | 1538 (if (not start) |
1539 (unless shutup-p | |
1540 (error "Cannot find start of balanced expression to indent")) | |
1541 (if (not end) | |
1542 (unless shutup-p | |
1543 (error "Cannot find end of balanced expression to indent")) | |
1544 (c-indent-region start end)))) | |
26817 | 1545 (goto-char here) |
1546 (set-marker here nil)))) | |
18720 | 1547 |
1548 (defun c-indent-defun () | |
36920 | 1549 "Indent the current top-level function def, struct or class declaration |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
1550 syntactically." |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1551 (interactive "*") |
18720 | 1552 (let ((here (point-marker)) |
1553 (c-echo-syntactic-information-p nil) | |
1554 (brace (c-least-enclosing-brace (c-parse-state)))) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1555 (goto-char (or brace (c-point 'bod))) |
18720 | 1556 ;; if we're sitting at b-o-b, it might be because there was no |
1557 ;; least enclosing brace and we were sitting on the defun's open | |
1558 ;; brace. | |
1559 (if (and (bobp) (not (eq (char-after) ?\{))) | |
1560 (goto-char here)) | |
1561 ;; if defun-prompt-regexp is non-nil, b-o-d might not leave us at | |
1562 ;; the open brace. I consider this an Emacs bug. | |
1563 (and (boundp 'defun-prompt-regexp) | |
1564 defun-prompt-regexp | |
1565 (looking-at defun-prompt-regexp) | |
1566 (goto-char (match-end 0))) | |
1567 ;; catch all errors in c-indent-exp so we can 1. give more | |
1568 ;; meaningful error message, and 2. restore point | |
1569 (unwind-protect | |
1570 (c-indent-exp) | |
1571 (goto-char here) | |
1572 (set-marker here nil)))) | |
1573 | |
36920 | 1574 (defun c-indent-region (start end &optional quiet) |
1575 "Indent every line whose first char is between START and END inclusive. | |
1576 Be silent about syntactic errors if the optional argument QUIET is non-nil." | |
18720 | 1577 (save-excursion |
1578 (goto-char start) | |
1579 ;; Advance to first nonblank line. | |
1580 (skip-chars-forward " \t\n") | |
1581 (beginning-of-line) | |
36920 | 1582 (setq c-parsing-error |
1583 (or (let ((endmark (copy-marker end)) | |
1584 (c-parsing-error nil) | |
1585 ;; shut up any echo msgs on indiv lines | |
1586 (c-echo-syntactic-information-p nil)) | |
1587 (unwind-protect | |
1588 (progn | |
1589 (c-progress-init start end 'c-indent-region) | |
1590 (while (and (bolp) | |
1591 (not (eobp)) | |
1592 (< (point) endmark)) | |
1593 ;; update progress | |
1594 (c-progress-update) | |
1595 ;; skip blank lines | |
1596 (skip-chars-forward " \t\n") | |
1597 (beginning-of-line) | |
1598 ;; indent the current line | |
1599 (c-indent-line nil t) | |
1600 (forward-line))) | |
1601 (set-marker endmark nil) | |
1602 (c-progress-fini 'c-indent-region)) | |
1603 (c-echo-parsing-error quiet)) | |
1604 c-parsing-error)))) | |
18720 | 1605 |
1606 (defun c-mark-function () | |
24282 | 1607 "Put mark at end of current top-level defun, point at beginning." |
18720 | 1608 (interactive) |
1609 (let ((here (point)) | |
24282 | 1610 (eod (c-point 'eod)) |
1611 (state (c-parse-state))) | |
1612 ;; Are we sitting at the top level, someplace between either the | |
1613 ;; beginning of buffer, or the nearest preceding defun? If so, | |
1614 ;; try first to figure out whether we're sitting on the | |
1615 ;; introduction to a top-level defun, in which case we want to | |
1616 ;; mark the entire defun we're sitting on. | |
1617 ;; | |
1618 ;; If we're sitting on anything else at the top-level, we want to | |
1619 ;; just mark the statement that we're on | |
1620 (if (or (and (consp (car state)) | |
1621 (= (length state) 1)) | |
1622 (null state)) | |
1623 ;; Are we in the whitespace after the nearest preceding defun? | |
1624 (if (and state | |
1625 (looking-at "[ \t]*$") | |
1626 (= (save-excursion | |
1627 (c-backward-syntactic-ws) | |
1628 (skip-chars-backward ";") | |
1629 (point)) | |
36920 | 1630 (cdr (car state)))) |
24282 | 1631 (progn |
1632 (setq eod (point)) | |
36920 | 1633 (goto-char (car (car state))) |
24282 | 1634 (c-beginning-of-statement-1)) |
1635 (if (= ?{ (save-excursion | |
1636 (c-end-of-statement-1) | |
1637 (char-before))) | |
1638 ;; We must be in a defuns's introduction | |
1639 (progn | |
1640 (c-end-of-statement-1) | |
1641 (skip-chars-backward "{") | |
1642 (c-beginning-of-statement-1) | |
1643 (c-forward-syntactic-ws)) | |
1644 ;; Just mark the statement | |
1645 (c-end-of-statement-1) | |
1646 (forward-line 1) | |
1647 (setq eod (point)) | |
1648 (c-beginning-of-statement-1))) | |
1649 ;; We are inside some enclosing brace structure, so we first | |
1650 ;; need to find our way to the least enclosing brace. Then, in | |
1651 ;; both cases, we to mark the region from the beginning of the | |
1652 ;; current statement, until the end of the next following defun | |
1653 (while (and state) | |
1654 (or (consp (car state)) | |
1655 (goto-char (car state))) | |
1656 (setq state (cdr state))) | |
1657 (c-beginning-of-statement-1)) | |
18720 | 1658 (push-mark here) |
1659 (push-mark eod nil t))) | |
1660 | |
36920 | 1661 (defun c-fn-region-is-active-p () |
1662 ;; Function version of the macro for use in places that aren't | |
1663 ;; compiled, e.g. in the menus. | |
1664 (c-region-is-active-p)) | |
1665 | |
24282 | 1666 (defun c-indent-line-or-region () |
1667 "When the region is active, indent it. Otherwise indent the current line." | |
1668 ;; Emacs has a variable called mark-active, XEmacs uses region-active-p | |
1669 (interactive) | |
1670 (if (c-region-is-active-p) | |
1671 (c-indent-region (region-beginning) (region-end)) | |
36920 | 1672 (indent-according-to-mode))) |
24282 | 1673 |
18720 | 1674 |
1675 ;; for progress reporting | |
1676 (defvar c-progress-info nil) | |
1677 | |
1678 (defun c-progress-init (start end context) | |
20143
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1679 (cond |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1680 ;; Be silent |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1681 ((not c-progress-interval)) |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1682 ;; 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
|
1683 ;; 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
|
1684 ((not (fboundp 'current-time)) |
36920 | 1685 (message "Indenting region... (this may take a while)")) |
20143
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1686 ;; 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
|
1687 ;; initialize the counter with a vector of: |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1688 ;; [start end lastsec context] |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1689 (c-progress-info) |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1690 (t (setq c-progress-info (vector start |
18720 | 1691 (save-excursion |
1692 (goto-char end) | |
1693 (point-marker)) | |
1694 (nth 1 (current-time)) | |
1695 context)) | |
36920 | 1696 (message "Indenting region...")) |
20143
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1697 )) |
18720 | 1698 |
1699 (defun c-progress-update () | |
1700 ;; update progress | |
1701 (if (not (and c-progress-info c-progress-interval)) | |
1702 nil | |
1703 (let ((now (nth 1 (current-time))) | |
1704 (start (aref c-progress-info 0)) | |
1705 (end (aref c-progress-info 1)) | |
1706 (lastsecs (aref c-progress-info 2))) | |
1707 ;; should we update? currently, update happens every 2 seconds, | |
1708 ;; what's the right value? | |
1709 (if (< c-progress-interval (- now lastsecs)) | |
1710 (progn | |
36920 | 1711 (message "Indenting region... (%d%% complete)" |
18720 | 1712 (/ (* 100 (- (point) start)) (- end start))) |
1713 (aset c-progress-info 2 now))) | |
1714 ))) | |
1715 | |
1716 (defun c-progress-fini (context) | |
1717 ;; finished | |
20143
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1718 (if (not c-progress-interval) |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1719 nil |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1720 (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
|
1721 (eq context t)) |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1722 (progn |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1723 (set-marker (aref c-progress-info 1) nil) |
69ce7b7acfd5
(c-progress-init, c-progress-fini):
Karl Heuer <kwzh@gnu.org>
parents:
20137
diff
changeset
|
1724 (setq c-progress-info nil) |
36920 | 1725 (message "Indenting region... done"))))) |
18720 | 1726 |
1727 | |
1728 | |
1729 ;;; This page handles insertion and removal of backslashes for C macros. | |
1730 | |
1731 (defun c-backslash-region (from to delete-flag) | |
1732 "Insert, align, or delete end-of-line backslashes on the lines in the region. | |
1733 With no argument, inserts backslashes and aligns existing backslashes. | |
1734 With an argument, deletes the backslashes. | |
1735 | |
1736 This function does not modify blank lines at the start of the region. | |
1737 If the region ends at the start of a line, it always deletes the | |
1738 backslash (if any) at the end of the previous line. | |
24282 | 1739 |
18720 | 1740 You can put the region around an entire macro definition and use this |
1741 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
|
1742 (interactive "*r\nP") |
18720 | 1743 (save-excursion |
1744 (goto-char from) | |
1745 (let ((column c-backslash-column) | |
1746 (endmark (make-marker))) | |
1747 (move-marker endmark to) | |
1748 ;; Compute the smallest column number past the ends of all the lines. | |
1749 (if (not delete-flag) | |
1750 (while (< (point) to) | |
1751 (end-of-line) | |
1752 (if (eq (char-before) ?\\) | |
1753 (progn (forward-char -1) | |
1754 (skip-chars-backward " \t"))) | |
1755 (setq column (max column (1+ (current-column)))) | |
1756 (forward-line 1))) | |
1757 ;; Adjust upward to a tab column, if that doesn't push past the margin. | |
1758 (if (> (% column tab-width) 0) | |
1759 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width))) | |
1760 (if (< adjusted (window-width)) | |
1761 (setq column adjusted)))) | |
1762 ;; Don't modify blank lines at start of region. | |
1763 (goto-char from) | |
1764 (while (and (< (point) endmark) (eolp)) | |
1765 (forward-line 1)) | |
1766 ;; Add or remove backslashes on all the lines. | |
1767 (while (< (point) endmark) | |
1768 (if (and (not delete-flag) | |
1769 ;; Un-backslashify the last line | |
1770 ;; if the region ends right at the start of the next line. | |
1771 (save-excursion | |
1772 (forward-line 1) | |
1773 (< (point) endmark))) | |
1774 (c-append-backslash column) | |
1775 (c-delete-backslash)) | |
1776 (forward-line 1)) | |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
1777 (move-marker endmark nil)))) |
18720 | 1778 |
1779 (defun c-append-backslash (column) | |
1780 (end-of-line) | |
1781 (if (eq (char-before) ?\\) | |
1782 (progn (forward-char -1) | |
1783 (delete-horizontal-space) | |
1784 (indent-to column)) | |
1785 (indent-to column) | |
1786 (insert "\\"))) | |
1787 | |
1788 (defun c-delete-backslash () | |
1789 (end-of-line) | |
1790 (or (bolp) | |
1791 (progn | |
1792 (forward-char -1) | |
1793 (if (looking-at "\\\\") | |
1794 (delete-region (1+ (point)) | |
1795 (progn (skip-chars-backward " \t") (point))))))) | |
1796 | |
26817 | 1797 |
18720 | 1798 |
26817 | 1799 ;;; Line breaking and paragraph filling. |
1800 | |
36920 | 1801 (defvar c-auto-fill-prefix t) |
1802 (defvar c-lit-limits nil) | |
1803 (defvar c-lit-type nil) | |
1804 | |
26817 | 1805 ;; The filling code is based on a simple theory; leave the intricacies |
1806 ;; of the text handling to the currently active mode for that | |
1807 ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as | |
1808 ;; possible to make them work correctly wrt the comment and string | |
1809 ;; separators, one-line paragraphs etc. Unfortunately, when it comes | |
1810 ;; to it, there's quite a lot of special cases to handle which makes | |
1811 ;; the code anything but simple. The intention is that it will work | |
1812 ;; with any well-written text filling package that preserves a fill | |
1813 ;; prefix. | |
1814 ;; | |
1815 ;; We temporarily mask comment starters and enders as necessary for | |
1816 ;; the filling code to do its job on a seemingly normal text block. | |
1817 ;; We do _not_ mask the fill prefix, so it's up to the filling code to | |
1818 ;; preserve it correctly (especially important when filling C++ style | |
1819 ;; line comments). By default, we set up and use adaptive-fill-mode, | |
1820 ;; which is standard in all supported Emacs flavors. | |
1821 | |
1822 (defun c-guess-fill-prefix (lit-limits lit-type) | |
1823 ;; Determine the appropriate comment fill prefix for a block or line | |
1824 ;; comment. Return a cons of the prefix string and the column where | |
1825 ;; it ends. If fill-prefix is set, it'll override. Note that this | |
1826 ;; function also uses the value of point in some heuristics. | |
1827 (let* ((here (point)) | |
1828 (prefix-regexp (concat "[ \t]*\\(" | |
36920 | 1829 c-current-comment-prefix |
26817 | 1830 "\\)[ \t]*")) |
1831 (comment-start-regexp (if (eq lit-type 'c++) | |
1832 prefix-regexp | |
1833 comment-start-skip)) | |
36920 | 1834 prefix-line comment-prefix res comment-text-end) |
26817 | 1835 (cond |
1836 (fill-prefix | |
1837 (setq res (cons fill-prefix | |
1838 ;; Ugly way of getting the column after the fill | |
1839 ;; prefix; it'd be nice with a current-column | |
1840 ;; that works on strings.. | |
1841 (let ((buffer-modified (buffer-modified-p)) | |
1842 (buffer-undo-list t) | |
1843 (start (point))) | |
1844 (unwind-protect | |
1845 (progn | |
1846 (insert ?\n fill-prefix) | |
1847 (current-column)) | |
1848 (delete-region start (point)) | |
1849 (set-buffer-modified-p buffer-modified)))))) | |
1850 ((eq lit-type 'c++) | |
1851 (save-excursion | |
1852 ;; Set fallback for comment-prefix if none is found. | |
36920 | 1853 (setq comment-prefix "// " |
1854 comment-text-end (cdr lit-limits)) | |
26817 | 1855 (beginning-of-line) |
1856 (if (> (point) (car lit-limits)) | |
1857 ;; The current line is not the comment starter, so the | |
1858 ;; comment has more than one line, and it can therefore be | |
1859 ;; used to find the comment fill prefix. | |
1860 (setq prefix-line (point)) | |
1861 (goto-char (car lit-limits)) | |
1862 (if (and (= (forward-line 1) 0) | |
1863 (< (point) (cdr lit-limits))) | |
1864 ;; The line after the comment starter is inside the | |
1865 ;; comment, so we can use it. | |
1866 (setq prefix-line (point)) | |
1867 ;; The comment is only one line. Take the comment prefix | |
1868 ;; from it and keep the indentation. | |
1869 (goto-char (car lit-limits)) | |
1870 (if (looking-at prefix-regexp) | |
1871 (goto-char (match-end 0)) | |
1872 (forward-char 2) | |
1873 (skip-chars-forward " \t")) | |
1874 (setq res | |
1875 (if (eq (c-point 'boi) (car lit-limits)) | |
1876 ;; There is only whitespace before the comment | |
1877 ;; starter; take the prefix straight from this | |
1878 ;; line. | |
1879 (cons (buffer-substring-no-properties | |
1880 (c-point 'bol) (point)) | |
1881 (current-column)) | |
1882 ;; There is code before the comment starter, so we | |
1883 ;; have to temporarily insert and indent a new | |
1884 ;; line to get the right space/tab mix in the | |
1885 ;; indentation. | |
1886 (let ((buffer-modified (buffer-modified-p)) | |
1887 (buffer-undo-list t) | |
1888 (prefix-len (- (point) (car lit-limits))) | |
1889 tmp) | |
1890 (unwind-protect | |
1891 (progn | |
1892 (goto-char (car lit-limits)) | |
1893 (indent-to (prog1 (current-column) | |
1894 (insert ?\n))) | |
1895 (setq tmp (point)) | |
1896 (forward-char prefix-len) | |
1897 (cons (buffer-substring-no-properties | |
1898 (c-point 'bol) (point)) | |
1899 (current-column))) | |
1900 (delete-region (car lit-limits) tmp) | |
1901 (set-buffer-modified-p buffer-modified)))) | |
1902 ))))) | |
1903 (t | |
1904 (save-excursion | |
36920 | 1905 (setq comment-text-end (- (cdr lit-limits) 2)) |
26817 | 1906 (beginning-of-line) |
1907 (if (and (> (point) (car lit-limits)) | |
1908 (not (and (looking-at "[ \t]*\\*/") | |
1909 (eq (cdr lit-limits) (match-end 0))))) | |
1910 ;; The current line is not the comment starter and | |
1911 ;; contains more than just the ender, so it's good enough | |
1912 ;; to be used for the comment fill prefix. | |
1913 (setq prefix-line (point)) | |
1914 (goto-char (car lit-limits)) | |
1915 (if (or (/= (forward-line 1) 0) | |
1916 (>= (point) (cdr lit-limits)) | |
1917 (and (looking-at "[ \t]*\\*/") | |
1918 (eq (cdr lit-limits) (match-end 0))) | |
1919 (and (looking-at prefix-regexp) | |
1920 (<= (1- (cdr lit-limits)) (match-end 0))) | |
1921 (and (< here (point)) | |
1922 (or (not (match-beginning 0)) | |
1923 (looking-at "[ \t]*$")))) | |
1924 ;; The comment is either one line or the next line | |
1925 ;; contains just the comment ender. Also, if point is | |
1926 ;; on the comment opener line and the following line is | |
36920 | 1927 ;; empty or doesn't match c-current-comment-prefix we |
26817 | 1928 ;; assume that this is in fact a not yet closed one line |
1929 ;; comment, so we shouldn't look for the comment prefix | |
1930 ;; on the next line. In these cases we have no | |
1931 ;; information about a suitable comment prefix, so we | |
1932 ;; resort to c-block-comment-prefix. | |
1933 (setq comment-prefix (or c-block-comment-prefix "") | |
1934 res (let ((buffer-modified (buffer-modified-p)) | |
1935 (buffer-undo-list t) | |
1936 tmp-pre tmp-post) | |
1937 ;; The comment doesn't give any information | |
1938 ;; about the indentation column. We'll have to | |
1939 ;; temporarily insert a new comment line and | |
1940 ;; indent it to find the correct column. | |
1941 (unwind-protect | |
1942 (progn | |
1943 (goto-char (car lit-limits)) | |
1944 (if (looking-at comment-start-regexp) | |
1945 (goto-char (match-end 0)) | |
1946 (forward-char 2) | |
1947 (skip-chars-forward " \t")) | |
1948 (when (eq (char-syntax (char-before)) ?\ ) | |
1949 ;; If there's ws on the current | |
1950 ;; line, we'll use it instead of | |
1951 ;; what's ending comment-prefix. | |
1952 (setq comment-prefix | |
1953 (concat (substring comment-prefix | |
1954 0 (string-match | |
1955 "\\s *\\'" | |
1956 comment-prefix)) | |
1957 (buffer-substring-no-properties | |
1958 (save-excursion | |
1959 (skip-chars-backward " \t") | |
1960 (point)) | |
1961 (point))))) | |
1962 (setq tmp-pre (point-marker)) | |
1963 ;; We insert an extra non-whitespace | |
1964 ;; character before the line break and | |
1965 ;; after comment-prefix in case it's | |
1966 ;; "" or ends with whitespace. | |
1967 (insert "x\n" comment-prefix ?x) | |
1968 (setq tmp-post (point-marker)) | |
36920 | 1969 (indent-according-to-mode) |
26817 | 1970 (goto-char (1- tmp-post)) |
1971 (cons (buffer-substring-no-properties | |
1972 (c-point 'bol) (point)) | |
1973 (current-column))) | |
1974 (when tmp-post | |
1975 (delete-region tmp-pre tmp-post) | |
1976 (set-marker tmp-pre nil) | |
1977 (set-marker tmp-post nil)) | |
1978 (set-buffer-modified-p buffer-modified)))) | |
1979 ;; Otherwise the line after the comment starter is good | |
1980 ;; enough to find the prefix in. | |
1981 (setq prefix-line (point))))))) | |
1982 (or res | |
1983 (save-excursion | |
1984 ;; prefix-line is the bol of a line on which we should try | |
1985 ;; to find the prefix. | |
1986 (let* (fb-string fb-endpos ; Contains any fallback prefix found. | |
1987 (test-line | |
1988 (lambda () | |
1989 (when (and (looking-at prefix-regexp) | |
36920 | 1990 (<= (match-end 0) comment-text-end)) |
1991 (unless (eq (match-end 0) (c-point 'eol)) | |
1992 ;; The match is fine if there's text after it. | |
1993 (throw 'found (cons (buffer-substring-no-properties | |
1994 (match-beginning 0) (match-end 0)) | |
1995 (progn (goto-char (match-end 0)) | |
1996 (current-column))))) | |
26817 | 1997 (unless fb-string |
36920 | 1998 ;; This match is better than nothing, so let's |
1999 ;; remember it in case nothing better is found | |
2000 ;; on another line. | |
26817 | 2001 (setq fb-string (buffer-substring-no-properties |
2002 (match-beginning 0) (match-end 0)) | |
2003 fb-endpos (match-end 0))) | |
2004 t)))) | |
36920 | 2005 (or (catch 'found |
26817 | 2006 ;; Search for a line which has text after the prefix |
2007 ;; so that we get the proper amount of whitespace | |
2008 ;; after it. We start with the current line, then | |
2009 ;; search backwards, then forwards. | |
2010 (goto-char prefix-line) | |
2011 (when (and (funcall test-line) | |
36920 | 2012 (or (/= (match-end 1) (match-end 0)) |
2013 ;; The whitespace is sucked up by the | |
2014 ;; first [ \t]* glob if the prefix is empty. | |
2015 (and (= (match-beginning 1) (match-end 1)) | |
2016 (/= (match-beginning 0) (match-end 0))))) | |
26817 | 2017 ;; If the current line doesn't have text but do |
2018 ;; have whitespace after the prefix, we'll use it. | |
36920 | 2019 (throw 'found (cons fb-string |
2020 (progn (goto-char fb-endpos) | |
2021 (current-column))))) | |
2022 (if (eq lit-type 'c++) | |
2023 ;; For line comments we can search up to and | |
2024 ;; including the first line. | |
2025 (while (and (zerop (forward-line -1)) | |
2026 (>= (point) (car lit-limits))) | |
2027 (funcall test-line)) | |
2028 ;; For block comments we must stop before the | |
2029 ;; block starter. | |
2030 (while (and (zerop (forward-line -1)) | |
2031 (> (point) (car lit-limits))) | |
2032 (funcall test-line))) | |
26817 | 2033 (goto-char prefix-line) |
2034 (while (and (zerop (forward-line 1)) | |
2035 (< (point) (cdr lit-limits))) | |
2036 (funcall test-line)) | |
36920 | 2037 (goto-char prefix-line) |
26817 | 2038 nil) |
36920 | 2039 (when fb-string |
26817 | 2040 ;; A good line wasn't found, but at least we have a |
2041 ;; fallback that matches the comment prefix regexp. | |
2042 (cond ((string-match "\\s \\'" fb-string) | |
2043 ;; There are ws after the prefix, so let's use it. | |
2044 (cons fb-string | |
2045 (progn (goto-char fb-endpos) (current-column)))) | |
2046 ((progn | |
2047 ;; Check if there's any whitespace padding | |
2048 ;; on the comment start line that we can | |
2049 ;; use after the prefix. | |
2050 (goto-char (car lit-limits)) | |
2051 (if (looking-at comment-start-regexp) | |
2052 (goto-char (match-end 0)) | |
2053 (forward-char 2) | |
2054 (skip-chars-forward " \t")) | |
2055 (eq (char-syntax (char-before)) ?\ )) | |
2056 (setq fb-string (buffer-substring-no-properties | |
2057 (save-excursion | |
2058 (skip-chars-backward " \t") | |
2059 (point)) | |
2060 (point))) | |
2061 (goto-char fb-endpos) | |
2062 (skip-chars-backward " \t") | |
2063 (let ((buffer-modified (buffer-modified-p)) | |
2064 (buffer-undo-list t) | |
2065 (tmp (point))) | |
2066 ;; Got to mess in the buffer once again to | |
2067 ;; ensure the column gets correct. :P | |
2068 (unwind-protect | |
2069 (progn | |
2070 (insert fb-string) | |
2071 (cons (buffer-substring-no-properties | |
2072 (c-point 'bol) | |
2073 (point)) | |
2074 (current-column))) | |
36920 | 2075 (delete-region tmp (point)) |
2076 (set-buffer-modified-p buffer-modified)))) | |
26817 | 2077 (t |
2078 ;; Last resort: Just add a single space after | |
2079 ;; the prefix. | |
2080 (cons (concat fb-string " ") | |
2081 (progn (goto-char fb-endpos) | |
36920 | 2082 (1+ (current-column))))))) |
26817 | 2083 ;; The line doesn't match the comment prefix regexp. |
2084 (if comment-prefix | |
2085 ;; We have a fallback for line comments that we must use. | |
2086 (cons (concat (buffer-substring-no-properties | |
2087 prefix-line (c-point 'boi)) | |
2088 comment-prefix) | |
2089 (progn (back-to-indentation) | |
2090 (+ (current-column) (length comment-prefix)))) | |
2091 ;; Assume we are dealing with a "free text" block | |
2092 ;; comment where the lines doesn't have any comment | |
2093 ;; prefix at all and we should just fill it as | |
2094 ;; normal text. | |
36920 | 2095 '("" . 0)))))) |
26817 | 2096 )) |
2097 | |
18720 | 2098 (defun c-fill-paragraph (&optional arg) |
2099 "Like \\[fill-paragraph] but handles C and C++ style comments. | |
26817 | 2100 If any of the current line is a comment or within a comment, fill the |
2101 comment or the paragraph of it that point is in, preserving the | |
2102 comment indentation or line-starting decorations (see the | |
2103 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for | |
2104 details). | |
18720 | 2105 |
21107
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2106 If point is inside multiline string literal, fill it. This currently |
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2107 does not respect escaped newlines, except for the special case when it |
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2108 is the very first thing in the string. The intended use for this rule |
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2109 is in situations like the following: |
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2110 |
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2111 char description[] = \"\\ |
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2112 A very long description of something that you want to fill to make |
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2113 nicely formatted output.\"\; |
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2114 |
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2115 If point is in any other situation, i.e. in normal code, do nothing. |
d67e858e738b
(c-fill-paragraph): Bind fill-paragraph-function to
Richard M. Stallman <rms@gnu.org>
parents:
20912
diff
changeset
|
2116 |
18720 | 2117 Optional prefix ARG means justify paragraph as well." |
20912
a0748eef9a76
(c-beginning-of-statement)
Richard M. Stallman <rms@gnu.org>
parents:
20143
diff
changeset
|
2118 (interactive "*P") |
26817 | 2119 (let (lit-limits lit-type fill |
2120 ;; beg and end limits the region to be filled. end is a marker. | |
2121 beg end | |
36920 | 2122 ;; tmp-pre and tmp-post mark strings that are temporarily |
26817 | 2123 ;; inserted at the start and end of the region. tmp-pre is a |
2124 ;; cons of the positions of the prepended string. tmp-post is | |
2125 ;; a marker pointing to the single character of the appended | |
2126 ;; string. | |
2127 tmp-pre tmp-post | |
36920 | 2128 ;; If hang-ender-stuck isn't nil, the comment ender is |
2129 ;; hanging. In that case it's set to the number of spaces | |
2130 ;; that should be between the text and the ender. | |
2131 hang-ender-stuck | |
2132 (here (point))) | |
26817 | 2133 ;; Restore point on undo. It's necessary since we do a lot of |
2134 ;; hidden inserts and deletes below that should be as transparent | |
2135 ;; as possible. | |
2136 (if (and buffer-undo-list (not (eq buffer-undo-list t))) | |
2137 (setq buffer-undo-list (cons (point) buffer-undo-list))) | |
36920 | 2138 (save-restriction |
2139 ;; Widen to catch comment limits correctly. | |
2140 (widen) | |
2141 (setq lit-limits (c-collect-line-comments (c-literal-limits nil t)) | |
2142 lit-type (c-literal-type lit-limits))) | |
26817 | 2143 (save-excursion |
36920 | 2144 (unless (c-safe (backward-char) |
2145 (forward-paragraph) | |
2146 (>= (point) here)) | |
2147 (goto-char here) | |
2148 (forward-paragraph)) | |
2149 (setq end (point-marker))) | |
2150 (save-excursion | |
2151 (unless (c-safe (forward-char) | |
2152 (backward-paragraph) | |
2153 (<= (point) here)) | |
2154 (goto-char here) | |
2155 (backward-paragraph)) | |
26817 | 2156 (setq beg (point))) |
36920 | 2157 (unwind-protect |
2158 (progn | |
2159 (cond | |
2160 ((eq lit-type 'c++) ; Line comment. | |
2161 (save-excursion | |
2162 ;; Fill to the comment or paragraph end, whichever | |
2163 ;; comes first. | |
2164 (set-marker end (min end (cdr lit-limits))) | |
2165 (when (<= beg (car lit-limits)) | |
2166 ;; The region to be filled includes the comment | |
2167 ;; starter, so we must check it. | |
2168 (goto-char (car lit-limits)) | |
2169 (back-to-indentation) | |
2170 (if (eq (point) (car lit-limits)) | |
2171 ;; Include the first line in the fill. | |
2172 (setq beg (c-point 'bol)) | |
2173 ;; The first line contains code before the | |
2174 ;; comment. We must fake a line that doesn't. | |
2175 (setq tmp-pre t))) | |
2176 )) | |
2177 ((eq lit-type 'c) ; Block comment. | |
2178 (when (>= end (cdr lit-limits)) | |
2179 ;; The region to be filled includes the comment ender. | |
2180 (unless (save-excursion | |
2181 (goto-char (cdr lit-limits)) | |
2182 (beginning-of-line) | |
2183 (and (looking-at (concat "[ \t]*\\(" | |
2184 c-current-comment-prefix | |
2185 "\\)\\*/")) | |
2186 (eq (cdr lit-limits) (match-end 0)) | |
2187 ;; Leave the comment ender on its own line. | |
2188 (set-marker end (point)))) | |
2189 ;; The comment ender should hang. Replace all cruft | |
2190 ;; between it and the last word with one or two 'x' | |
2191 ;; and include it in the fill. We'll change them back | |
2192 ;; spaces afterwards. | |
2193 (let* ((ender-start (save-excursion | |
2194 (goto-char (cdr lit-limits)) | |
2195 (skip-syntax-backward "^w ") | |
2196 (point))) | |
2197 (point-rel (- ender-start here)) | |
2198 spaces) | |
2199 (save-excursion | |
2200 (goto-char (cdr lit-limits)) | |
2201 (setq tmp-post (point-marker)) | |
2202 (insert ?\n) | |
2203 (set-marker end (point)) | |
2204 (forward-line -1) | |
2205 (if (and (looking-at (concat "[ \t]*\\(\\(" | |
2206 c-current-comment-prefix | |
2207 "\\)[ \t]*\\)")) | |
2208 (eq ender-start (match-end 0))) | |
2209 ;; The comment ender is prefixed by nothing | |
2210 ;; but a comment line prefix. Remove it | |
2211 ;; along with surrounding ws. | |
2212 (setq spaces (- (match-end 1) (match-end 2))) | |
2213 (goto-char ender-start)) | |
2214 (skip-chars-backward " \t\r\n") | |
2215 (if (/= (point) ender-start) | |
2216 (progn | |
2217 (if (<= here (point)) | |
2218 ;; Don't adjust point below if it's | |
2219 ;; before the string we replace. | |
2220 (setq point-rel -1)) | |
2221 ;; Keep one or two spaces between the text and | |
2222 ;; the ender, depending on how many there are now. | |
2223 (unless spaces (setq spaces (- ender-start (point)))) | |
2224 (setq spaces (max (min spaces 2) 1)) | |
2225 ;; Insert the filler first to keep marks right. | |
2226 (insert (make-string spaces ?x)) | |
2227 (delete-region (point) (+ ender-start spaces)) | |
2228 (setq hang-ender-stuck spaces) | |
2229 (setq point-rel | |
2230 (and (>= point-rel 0) | |
2231 (- (point) (min point-rel spaces))))) | |
2232 (setq point-rel nil))) | |
2233 (if point-rel | |
2234 ;; Point was in the middle of the string we | |
2235 ;; replaced above, so put it back in the same | |
2236 ;; relative position, counting from the end. | |
2237 (goto-char point-rel)) | |
2238 ))) | |
2239 (when (<= beg (car lit-limits)) | |
2240 ;; The region to be filled includes the comment starter. | |
26817 | 2241 (save-excursion |
2242 (goto-char (car lit-limits)) | |
36920 | 2243 (if (looking-at (concat "\\(" comment-start-skip "\\)$")) |
2244 ;; Begin filling with the next line. | |
2245 (setq beg (c-point 'bonl)) | |
2246 ;; Fake the fill prefix in the first line. | |
2247 (setq tmp-pre t))))) | |
2248 ((eq lit-type 'string) ; String. | |
2249 (save-excursion | |
2250 (when (>= end (cdr lit-limits)) | |
2251 (goto-char (1- (cdr lit-limits))) | |
2252 (setq tmp-post (point-marker)) | |
2253 (insert ?\n) | |
2254 (set-marker end (point))) | |
2255 (when (<= beg (car lit-limits)) | |
2256 (goto-char (1+ (car lit-limits))) | |
2257 (setq beg (if (looking-at "\\\\$") | |
2258 ;; Leave the start line if it's | |
2259 ;; nothing but an escaped newline. | |
2260 (1+ (match-end 0)) | |
2261 (point)))))) | |
2262 (t (setq beg nil))) | |
2263 (when tmp-pre | |
2264 ;; Temporarily insert the fill prefix after the comment | |
2265 ;; starter so that the first line looks like any other | |
2266 ;; comment line in the narrowed region. | |
2267 (setq fill (c-guess-fill-prefix lit-limits lit-type)) | |
2268 (unless (string-match (concat "\\`[ \t]*\\(" | |
2269 c-current-comment-prefix | |
2270 "\\)[ \t]*\\'") | |
2271 (car fill)) | |
2272 ;; Oops, the prefix doesn't match the comment prefix | |
2273 ;; regexp. This could produce very confusing | |
2274 ;; results with adaptive fill packages together with | |
2275 ;; the insert prefix magic below, since the prefix | |
2276 ;; often doesn't appear at all. So let's warn about | |
2277 ;; it. | |
2278 (message "\ | |
2279 Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S" | |
2280 (car fill))) | |
2281 ;; Find the right spot on the line, break it, insert | |
2282 ;; the fill prefix and make sure we're back in the | |
2283 ;; same column by temporarily prefixing the first word | |
2284 ;; with a number of 'x'. | |
2285 (save-excursion | |
2286 (goto-char (car lit-limits)) | |
2287 (if (looking-at (if (eq lit-type 'c++) | |
2288 c-comment-prefix-regexp | |
2289 comment-start-skip)) | |
2290 (goto-char (match-end 0)) | |
2291 (forward-char 2) | |
2292 (skip-chars-forward " \t")) | |
2293 (while (< (current-column) (cdr fill)) (forward-char 1)) | |
2294 (let ((col (current-column))) | |
2295 (setq beg (1+ (point)) | |
2296 tmp-pre (list (point))) | |
2297 (unwind-protect | |
2298 (progn | |
2299 (insert ?\n (car fill)) | |
2300 (insert (make-string (- col (current-column)) ?x))) | |
2301 (setcdr tmp-pre (point)))))) | |
2302 (when beg | |
2303 (let ((fill-paragraph-function | |
2304 ;; Avoid infinite recursion. | |
2305 (if (not (eq fill-paragraph-function 'c-fill-paragraph)) | |
2306 fill-paragraph-function)) | |
2307 (fill-prefix | |
2308 (or fill-prefix | |
2309 ;; Kludge: If the function that adapts the fill prefix | |
2310 ;; doesn't produce the required comment starter for line | |
2311 ;; comments, then force it by setting fill-prefix. | |
2312 (when (and (eq lit-type 'c++) | |
2313 ;; Kludge the kludge: filladapt-mode doesn't | |
2314 ;; have this problem, but it doesn't override | |
2315 ;; fill-context-prefix currently (version | |
2316 ;; 2.12). | |
2317 (not (and (boundp 'filladapt-mode) | |
2318 filladapt-mode)) | |
2319 (not (string-match | |
2320 "\\`[ \t]*//" | |
2321 (or (fill-context-prefix beg end) | |
2322 "")))) | |
2323 (car (or fill (c-guess-fill-prefix | |
2324 lit-limits lit-type)))))) | |
2325 (point-rel (cond ((< here beg) (- here beg)) | |
2326 ((> here end) (- here end))))) | |
2327 ;; Preparations finally done! Now we can call the | |
2328 ;; real fill function. | |
2329 (save-restriction | |
2330 (narrow-to-region beg end) | |
2331 (fill-paragraph arg)) | |
2332 (if point-rel | |
2333 ;; Restore point if it was outside the region. | |
2334 (if (< point-rel 0) | |
2335 (goto-char (+ beg point-rel)) | |
2336 (goto-char (+ end point-rel)))) | |
2337 ))) | |
2338 (when (consp tmp-pre) | |
2339 (delete-region (car tmp-pre) (cdr tmp-pre))) | |
2340 (when tmp-post | |
2341 (save-excursion | |
2342 (goto-char tmp-post) | |
2343 (delete-char 1)) | |
2344 (when hang-ender-stuck | |
2345 ;; Preserve point even if it's in the middle of the string | |
2346 ;; we replace; save-excursion doesn't work in that case. | |
2347 (setq here (point)) | |
2348 (goto-char tmp-post) | |
2349 (skip-syntax-backward "^w ") | |
2350 (forward-char (- hang-ender-stuck)) | |
2351 (insert (make-string hang-ender-stuck ?\ )) | |
2352 (delete-char hang-ender-stuck) | |
2353 (goto-char here)) | |
2354 (set-marker tmp-post nil)) | |
2355 (set-marker end nil))) | |
26817 | 2356 ;; Always return t. This has the effect that if filling isn't done |
2357 ;; above, it isn't done at all, and it's therefore effectively | |
2358 ;; disabled in normal code. | |
2359 t) | |
18720 | 2360 |
26817 | 2361 (defun c-do-auto-fill () |
2362 ;; Do automatic filling if not inside a context where it should be | |
2363 ;; ignored. | |
2364 (let ((c-auto-fill-prefix | |
2365 ;; The decision whether the line should be broken is actually | |
2366 ;; done in c-indent-new-comment-line, which do-auto-fill | |
2367 ;; calls to break lines. We just set this special variable | |
2368 ;; so that we'll know when we're called from there. It's | |
2369 ;; also used to detect whether fill-prefix is user set or | |
2370 ;; generated automatically by do-auto-fill. | |
2371 fill-prefix)) | |
2372 (do-auto-fill))) | |
18720 | 2373 |
26817 | 2374 (defun c-indent-new-comment-line (&optional soft) |
2375 "Break line at point and indent, continuing comment if within one. | |
2376 If inside a comment and `comment-multi-line' is non-nil, the | |
2377 indentation and line prefix are preserved (see the | |
2378 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2379 details). If inside a single line comment and `comment-multi-line' is |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2380 nil, a new comment of the same type is started on the next line and |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2381 indented as appropriate for comments. |
18720 | 2382 |
26817 | 2383 If a fill prefix is specified, it overrides all the above." |
2384 (interactive) | |
2385 (let ((fill-prefix fill-prefix) | |
2386 (do-line-break | |
2387 (lambda () | |
2388 (delete-region (progn (skip-chars-backward " \t") (point)) | |
2389 (progn (skip-chars-forward " \t") (point))) | |
2390 (if soft (insert-and-inherit ?\n) (newline 1)))) | |
2391 ;; Already know the literal type and limits when called from | |
2392 ;; c-context-line-break. | |
36920 | 2393 (c-lit-limits c-lit-limits) |
2394 (c-lit-type c-lit-type)) | |
2395 (when (not (eq c-auto-fill-prefix t)) | |
26817 | 2396 ;; Called from do-auto-fill. |
2397 (unless c-lit-limits | |
2398 (setq c-lit-limits (c-literal-limits nil nil t))) | |
2399 (unless c-lit-type | |
2400 (setq c-lit-type (c-literal-type c-lit-limits))) | |
2401 (if (memq (cond ((eq c-lit-type 'pound) | |
2402 ;; Come to think about it, "pound" is a bit | |
2403 ;; of a misnomer, so call it "cpp" instead | |
2404 ;; in user interaction. | |
2405 'cpp) | |
2406 ((null c-lit-type) 'code) | |
2407 (t c-lit-type)) | |
2408 c-ignore-auto-fill) | |
2409 (setq fill-prefix t) ; Used as flag in the cond. | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2410 (if (and (null c-auto-fill-prefix) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2411 (eq c-lit-type 'c) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2412 (<= (c-point 'bol) (car c-lit-limits))) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2413 ;; The adaptive fill function has generated a prefix, but |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2414 ;; we're on the first line in a block comment so it'll be |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2415 ;; wrong. Ignore it to guess a better one below. |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2416 (setq fill-prefix nil) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2417 (when (and (eq c-lit-type 'c++) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2418 (not (string-match "\\`[ \t]*//" (or fill-prefix "")))) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2419 ;; Kludge: If the function that adapted the fill prefix |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2420 ;; doesn't produce the required comment starter for line |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2421 ;; comments, then we ignore it. |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2422 (setq fill-prefix nil))) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2423 )) |
26817 | 2424 (cond ((eq fill-prefix t) |
2425 ;; A call from do-auto-fill which should be ignored. | |
2426 ) | |
2427 (fill-prefix | |
2428 ;; A fill-prefix overrides anything. | |
2429 (funcall do-line-break) | |
2430 (insert-and-inherit fill-prefix)) | |
2431 ((progn | |
2432 (unless c-lit-limits | |
2433 (setq c-lit-limits (c-literal-limits nil nil t))) | |
2434 (unless c-lit-type | |
2435 (setq c-lit-type (c-literal-type c-lit-limits))) | |
2436 (memq c-lit-type '(c c++))) | |
30406
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2437 (if (or comment-multi-line |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2438 (save-excursion |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2439 (goto-char (car c-lit-limits)) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2440 (end-of-line) |
4fe5cb975331
(c-indent-new-comment-line): Added a kludge
Gerd Moellmann <gerd@gnu.org>
parents:
27111
diff
changeset
|
2441 (< (point) (cdr c-lit-limits)))) |
26817 | 2442 ;; Inside a comment that should be continued. |
2443 (let ((fill (c-guess-fill-prefix | |
2444 (setq c-lit-limits | |
2445 (c-collect-line-comments c-lit-limits)) | |
2446 c-lit-type)) | |
2447 (pos (point))) | |
2448 (if (save-excursion | |
2449 (back-to-indentation) | |
2450 (> (point) (car c-lit-limits)) | |
36920 | 2451 (looking-at c-current-comment-prefix)) |
26817 | 2452 (progn |
2453 ;; Skip forward past the fill prefix in case | |
2454 ;; we're standing in it. | |
2455 (while (and (< (current-column) (cdr fill)) | |
2456 (not (eolp))) | |
2457 (forward-char 1)) | |
2458 (if (> (point) (if (and (eq c-lit-type 'c) | |
2459 (save-excursion | |
2460 (forward-char -2) | |
2461 (looking-at "\\*/"))) | |
2462 (- (cdr c-lit-limits) 2) | |
2463 (cdr c-lit-limits))) | |
2464 (progn | |
2465 ;; The skip takes us out of the comment; | |
2466 ;; insert the fill prefix at bol instead | |
2467 ;; and keep the position. | |
2468 (setq pos (copy-marker pos t)) | |
2469 (beginning-of-line) | |
2470 (insert-and-inherit (car fill)) | |
2471 (if soft (insert-and-inherit ?\n) (newline 1)) | |
2472 (goto-char pos) | |
2473 (set-marker pos nil)) | |
2474 (funcall do-line-break) | |
2475 (insert-and-inherit (car fill)))) | |
2476 (funcall do-line-break) | |
2477 (insert-and-inherit (car fill)))) | |
2478 ;; Inside a comment that should be broken. | |
2479 (let ((comment-start comment-start) | |
2480 (comment-end comment-end) | |
2481 col) | |
2482 (if (eq c-lit-type 'c) | |
2483 (unless (string-match "[ \t]*/\\*" comment-start) | |
2484 (setq comment-start "/* " comment-end " */")) | |
2485 (unless (string-match "[ \t]*//" comment-start) | |
2486 (setq comment-start "// " comment-end ""))) | |
2487 (setq col (save-excursion | |
2488 (back-to-indentation) | |
2489 (current-column))) | |
2490 (funcall do-line-break) | |
2491 (when (and comment-end (not (equal comment-end ""))) | |
2492 (forward-char -1) | |
2493 (insert-and-inherit comment-end) | |
2494 (forward-char 1)) | |
2495 ;; c-comment-indent may look at the current | |
2496 ;; indentation, so let's start out with the same | |
2497 ;; indentation as the previous one. | |
2498 (indent-to col) | |
2499 (insert-and-inherit comment-start) | |
2500 (indent-for-comment)))) | |
2501 (t | |
2502 ;; Somewhere else in the code. | |
2503 (let ((col (save-excursion | |
2504 (while (progn (back-to-indentation) | |
2505 (and (looking-at "^\\s *$") | |
2506 (= (forward-line -1) 0)))) | |
2507 (current-column)))) | |
2508 (funcall do-line-break) | |
2509 (indent-to col)))))) | |
2510 | |
2511 (defalias 'c-comment-line-break-function 'c-indent-new-comment-line) | |
2512 (make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line) | |
2513 | |
2514 ;; advice for indent-new-comment-line for older Emacsen | |
2515 (unless (boundp 'comment-line-break-function) | |
36920 | 2516 (defvar c-inside-line-break-advice nil) |
26817 | 2517 (defadvice indent-new-comment-line (around c-line-break-advice |
2518 activate preactivate) | |
2519 "Call `c-indent-new-comment-line' if in CC Mode." | |
36920 | 2520 (if (or c-inside-line-break-advice |
26817 | 2521 (not c-buffer-is-cc-mode)) |
2522 ad-do-it | |
36920 | 2523 (let ((c-inside-line-break-advice t)) |
26817 | 2524 (c-indent-new-comment-line (ad-get-arg 0)))))) |
2525 | |
2526 (defun c-context-line-break () | |
2527 "Do a line break suitable to the context. | |
2528 | |
2529 When point is outside a comment, insert a newline and indent according | |
2530 to the syntactic context. | |
2531 | |
2532 When point is inside a comment, continue it with the appropriate | |
2533 comment prefix (see the `c-comment-prefix-regexp' and | |
2534 `c-block-comment-prefix' variables for details). The end of a | |
2535 C++-style line comment doesn't count as inside the comment, though." | |
2536 (interactive "*") | |
2537 (let* ((c-lit-limits (c-literal-limits nil nil t)) | |
2538 (c-lit-type (c-literal-type c-lit-limits))) | |
2539 (if (or (eq c-lit-type 'c) | |
2540 (and (eq c-lit-type 'c++) | |
2541 (< (point) | |
2542 (1- (cdr (setq c-lit-limits | |
2543 (c-collect-line-comments c-lit-limits))))))) | |
2544 (let ((comment-multi-line t) | |
2545 (fill-prefix nil)) | |
2546 (c-indent-new-comment-line)) | |
2547 (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
2548 (newline) | |
2549 ;; c-indent-line may look at the current indentation, so let's | |
2550 ;; start out with the same indentation as the previous line. | |
2551 (let ((col (save-excursion | |
2552 (forward-line -1) | |
2553 (while (progn (back-to-indentation) | |
2554 (and (looking-at "^\\s *$") | |
2555 (= (forward-line -1) 0)))) | |
2556 (current-column)))) | |
2557 (indent-to col)) | |
36920 | 2558 (indent-according-to-mode)))) |
18720 | 2559 |
2560 | |
36920 | 2561 (cc-provide 'cc-cmds) |
18720 | 2562 ;;; cc-cmds.el ends here |