Merge from origin/emacs-24
[emacs.git] / lisp / progmodes / cc-cmds.el
blob68075f356ab8a6c17de8433c5254b21152362ecc
1 ;;; cc-cmds.el --- user level commands for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2015 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
14 ;; Package: cc-mode
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;;; Commentary:
33 ;;; Code:
35 (eval-when-compile
36 (let ((load-path
37 (if (and (boundp 'byte-compile-dest-file)
38 (stringp byte-compile-dest-file))
39 (cons (file-name-directory byte-compile-dest-file) load-path)
40 load-path)))
41 (load "cc-bytecomp" nil t)))
43 (cc-require 'cc-defs)
44 (cc-require 'cc-vars)
45 (cc-require 'cc-engine)
47 ;; Silence the compiler.
48 (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge
49 ; which looks at this.
51 ;; Indentation / Display syntax functions
52 (defvar c-fix-backslashes t)
54 (defun c-indent-line (&optional syntax quiet ignore-point-pos)
55 "Indent the current line according to the syntactic context,
56 if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the
57 syntactic information for the current line. Be silent about syntactic
58 errors if the optional argument QUIET is non-nil, even if
59 `c-report-syntactic-errors' is non-nil. Normally the position of
60 point is used to decide where the old indentation is on a lines that
61 is otherwise empty \(ignoring any line continuation backslash), but
62 that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of
63 indentation change \(in columns)."
65 (let ((line-cont-backslash (save-excursion
66 (end-of-line)
67 (eq (char-before) ?\\)))
68 (c-fix-backslashes c-fix-backslashes)
69 bs-col
70 shift-amt)
71 (when (and (not ignore-point-pos)
72 (save-excursion
73 (beginning-of-line)
74 (looking-at (if line-cont-backslash
75 ;; Don't use "\\s " - ^L doesn't count as WS
76 ;; here
77 "\\([ \t]*\\)\\\\$"
78 "\\([ \t]*\\)$")))
79 (<= (point) (match-end 1)))
80 ;; Delete all whitespace after point if there's only whitespace
81 ;; on the line, so that any code that does back-to-indentation
82 ;; or similar gets the current column in this case. If this
83 ;; removes a line continuation backslash it'll be restored
84 ;; at the end.
85 (unless c-auto-align-backslashes
86 ;; Should try to keep the backslash alignment
87 ;; in this case.
88 (save-excursion
89 (goto-char (match-end 0))
90 (setq bs-col (1- (current-column)))))
91 (delete-region (point) (match-end 0))
92 (setq c-fix-backslashes t))
93 (if c-syntactic-indentation
94 (setq c-parsing-error
95 (or (let ((c-parsing-error nil)
96 (c-syntactic-context
97 (or syntax
98 (and (boundp 'c-syntactic-context)
99 c-syntactic-context))))
100 (c-save-buffer-state (indent)
101 (unless c-syntactic-context
102 (setq c-syntactic-context (c-guess-basic-syntax)))
103 (setq indent (c-get-syntactic-indentation
104 c-syntactic-context))
105 (and (not (c-echo-parsing-error quiet))
106 c-echo-syntactic-information-p
107 (message "syntax: %s, indent: %d"
108 c-syntactic-context indent))
109 (setq shift-amt (- indent (current-indentation))))
110 (c-shift-line-indentation shift-amt)
111 (run-hooks 'c-special-indent-hook)
112 c-parsing-error)
113 c-parsing-error))
114 (let ((indent 0))
115 (save-excursion
116 (while (and (= (forward-line -1) 0)
117 (if (looking-at "\\s *\\\\?$")
119 (setq indent (current-indentation))
120 nil))))
121 (setq shift-amt (- indent (current-indentation)))
122 (c-shift-line-indentation shift-amt)))
123 (when (and c-fix-backslashes line-cont-backslash)
124 (if bs-col
125 (save-excursion
126 (indent-to bs-col)
127 (insert ?\\))
128 (when c-auto-align-backslashes
129 ;; Realign the line continuation backslash.
130 (c-backslash-region (point) (point) nil t))))
131 shift-amt))
133 (defun c-newline-and-indent (&optional newline-arg)
134 "Insert a newline and indent the new line.
135 This function fixes line continuation backslashes if inside a macro,
136 and takes care to set the indentation before calling
137 `indent-according-to-mode', so that lineup functions like
138 `c-lineup-dont-change' works better."
140 ;; TODO: Backslashes before eol in comments and literals aren't
141 ;; kept intact.
142 (let ((c-macro-start (c-query-macro-start))
143 ;; Avoid calling c-backslash-region from c-indent-line if it's
144 ;; called during the newline call, which can happen due to
145 ;; c-electric-continued-statement, for example. We also don't
146 ;; want any backslash alignment from indent-according-to-mode.
147 (c-fix-backslashes nil)
148 has-backslash insert-backslash
149 start col)
150 (save-excursion
151 (beginning-of-line)
152 (setq start (point))
153 (while (and (looking-at "[ \t]*\\\\?$")
154 (= (forward-line -1) 0)))
155 (setq col (current-indentation)))
156 (when c-macro-start
157 (if (and (eolp) (eq (char-before) ?\\))
158 (setq insert-backslash t
159 has-backslash t)
160 (setq has-backslash (eq (char-before (c-point 'eol)) ?\\))))
161 (newline newline-arg)
162 (indent-to col)
163 (when c-macro-start
164 (if insert-backslash
165 (progn
166 ;; The backslash stayed on the previous line. Insert one
167 ;; before calling c-backslash-region, so that
168 ;; bs-col-after-end in it works better. Fixup the
169 ;; backslashes on the newly inserted line.
170 (insert ?\\)
171 (backward-char)
172 (c-backslash-region (point) (point) nil t))
173 ;; The backslash moved to the new line, if there was any. Let
174 ;; c-backslash-region fix a backslash on the previous line,
175 ;; and the one that might be on the new line.
176 ;; c-auto-align-backslashes is intentionally ignored here;
177 ;; maybe the moved backslash should be left alone if it's set,
178 ;; but we fix both lines on the grounds that the old backslash
179 ;; has been moved anyway and is now in a different context.
180 (c-backslash-region start (if has-backslash (point) start) nil t)))
181 (when c-syntactic-indentation
182 ;; Reindent syntactically. The indentation done above is not
183 ;; wasted, since c-indent-line might look at the current
184 ;; indentation.
185 (let ((c-syntactic-context (c-save-buffer-state nil
186 (c-guess-basic-syntax))))
187 ;; We temporarily insert another line break, so that the
188 ;; lineup functions will see the line as empty. That makes
189 ;; e.g. c-lineup-cpp-define more intuitive since it then
190 ;; proceeds to the preceding line in this case.
191 (insert ?\n)
192 (delete-horizontal-space)
193 (setq start (- (point-max) (point)))
194 (unwind-protect
195 (progn
196 (backward-char)
197 (indent-according-to-mode))
198 (goto-char (- (point-max) start))
199 (delete-char -1)))
200 (when has-backslash
201 ;; Must align the backslash again after reindentation. The
202 ;; c-backslash-region call above can't be optimized to ignore
203 ;; this line, since it then won't align correctly with the
204 ;; lines below if the first line in the macro is broken.
205 (c-backslash-region (point) (point) nil t)))))
207 (defun c-show-syntactic-information (arg)
208 "Show syntactic information for current line.
209 With universal argument, inserts the analysis as a comment on that line."
210 (interactive "P")
211 (let* ((c-parsing-error nil)
212 (syntax (if (boundp 'c-syntactic-context)
213 ;; Use `c-syntactic-context' in the same way as
214 ;; `c-indent-line', to be consistent.
215 c-syntactic-context
216 (c-save-buffer-state nil
217 (c-guess-basic-syntax)))))
218 (if (not (consp arg))
219 (let (elem pos ols)
220 (message "Syntactic analysis: %s" syntax)
221 (unwind-protect
222 (progn
223 (while syntax
224 (setq elem (pop syntax))
225 (when (setq pos (c-langelem-pos elem))
226 (push (c-put-overlay pos (1+ pos)
227 'face 'highlight)
228 ols))
229 (when (setq pos (c-langelem-2nd-pos elem))
230 (push (c-put-overlay pos (1+ pos)
231 'face 'secondary-selection)
232 ols)))
233 (sit-for 10))
234 (while ols
235 (c-delete-overlay (pop ols)))))
236 (indent-for-comment)
237 (insert-and-inherit (format "%s" syntax))
239 (c-keep-region-active))
241 (defun c-syntactic-information-on-region (from to)
242 "Insert a comment with the syntactic analysis on every line in the region."
243 (interactive "*r")
244 (save-excursion
245 (save-restriction
246 (narrow-to-region from to)
247 (goto-char (point-min))
248 (while (not (eobp))
249 (c-show-syntactic-information '(0))
250 (forward-line)))))
253 ;; Minor mode functions.
254 (defun c-update-modeline ()
255 (let ((fmt (format "/%s%s%s%s"
256 (if c-electric-flag "l" "")
257 (if (and c-electric-flag c-auto-newline)
258 "a" "")
259 (if c-hungry-delete-key "h" "")
260 (if (and
261 ;; (cc-)subword might not be loaded.
262 (boundp 'c-subword-mode)
263 (symbol-value 'c-subword-mode))
264 ;; FIXME: subword-mode already comes with its
265 ;; own lighter!
267 "")))
268 ;; FIXME: Derived modes might want to use something else
269 ;; than a string for `mode-name'.
270 (bare-mode-name (if (string-match "\\(^[^/]*\\)/" mode-name)
271 (match-string 1 mode-name)
272 mode-name)))
273 ;; (setq c-submode-indicators
274 ;; (if (> (length fmt) 1)
275 ;; fmt))
276 (setq mode-name
277 (if (> (length fmt) 1)
278 (concat bare-mode-name fmt)
279 bare-mode-name))
280 (force-mode-line-update)))
282 (defun c-toggle-syntactic-indentation (&optional arg)
283 "Toggle syntactic indentation.
284 Optional numeric ARG, if supplied, turns on syntactic indentation when
285 positive, turns it off when negative, and just toggles it when zero or
286 left out.
288 When syntactic indentation is turned on (the default), the indentation
289 functions and the electric keys indent according to the syntactic
290 context keys, when applicable.
292 When it's turned off, the electric keys don't reindent, the indentation
293 functions indents every new line to the same level as the previous
294 nonempty line, and \\[c-indent-command] adjusts the indentation in steps
295 specified by `c-basic-offset'. The indentation style has no effect in
296 this mode, nor any of the indentation associated variables,
297 e.g. `c-special-indent-hook'.
299 This command sets the variable `c-syntactic-indentation'."
300 (interactive "P")
301 (setq c-syntactic-indentation
302 (c-calculate-state arg c-syntactic-indentation))
303 (c-keep-region-active))
305 (defun c-toggle-auto-newline (&optional arg)
306 "Toggle auto-newline feature.
307 Optional numeric ARG, if supplied, turns on auto-newline when
308 positive, turns it off when negative, and just toggles it when zero or
309 left out.
311 Turning on auto-newline automatically enables electric indentation.
313 When the auto-newline feature is enabled (indicated by \"/la\" on the
314 mode line after the mode name) newlines are automatically inserted
315 after special characters such as brace, comma, semi-colon, and colon."
316 (interactive "P")
317 (setq c-auto-newline
318 (c-calculate-state arg (and c-auto-newline c-electric-flag)))
319 (if c-auto-newline (setq c-electric-flag t))
320 (c-update-modeline)
321 (c-keep-region-active))
323 (defalias 'c-toggle-auto-state 'c-toggle-auto-newline)
324 (make-obsolete 'c-toggle-auto-state 'c-toggle-auto-newline "22.1")
326 (defun c-toggle-hungry-state (&optional arg)
327 "Toggle hungry-delete-key feature.
328 Optional numeric ARG, if supplied, turns on hungry-delete when
329 positive, turns it off when negative, and just toggles it when zero or
330 left out.
332 When the hungry-delete-key feature is enabled (indicated by \"/h\" on
333 the mode line after the mode name) the delete key gobbles all preceding
334 whitespace in one fell swoop."
335 (interactive "P")
336 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
337 (c-update-modeline)
338 (c-keep-region-active))
340 (defun c-toggle-auto-hungry-state (&optional arg)
341 "Toggle auto-newline and hungry-delete-key features.
342 Optional numeric ARG, if supplied, turns on auto-newline and
343 hungry-delete when positive, turns them off when negative, and just
344 toggles them when zero or left out.
346 See `c-toggle-auto-newline' and `c-toggle-hungry-state' for details."
347 (interactive "P")
348 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
349 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
350 (c-update-modeline)
351 (c-keep-region-active))
353 (defun c-toggle-electric-state (&optional arg)
354 "Toggle the electric indentation feature.
355 Optional numeric ARG, if supplied, turns on electric indentation when
356 positive, turns it off when negative, and just toggles it when zero or
357 left out."
358 (interactive "P")
359 (setq c-electric-flag (c-calculate-state arg c-electric-flag))
360 (c-update-modeline)
361 (when (fboundp 'electric-indent-local-mode) ; Emacs 24.4 or later.
362 (electric-indent-local-mode (if c-electric-flag 1 0)))
363 (c-keep-region-active))
366 ;; Electric keys
368 (defun c-electric-backspace (arg)
369 "Delete the preceding character or whitespace.
370 If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
371 line) then all preceding whitespace is consumed. If however a prefix
372 argument is supplied, or `c-hungry-delete-key' is nil, or point is
373 inside a literal then the function in the variable
374 `c-backspace-function' is called."
375 (interactive "*P")
376 (if (c-save-buffer-state ()
377 (or (not c-hungry-delete-key)
379 (c-in-literal)))
380 (funcall c-backspace-function (prefix-numeric-value arg))
381 (c-hungry-delete-backwards)))
383 (defun c-hungry-delete-backwards ()
384 "Delete the preceding character or all preceding whitespace
385 back to the previous non-whitespace character.
386 See also \\[c-hungry-delete-forward]."
387 (interactive)
388 (let ((here (point)))
389 (c-skip-ws-backward)
390 (if (/= (point) here)
391 (delete-region (point) here)
392 (funcall c-backspace-function 1))))
394 (defalias 'c-hungry-backspace 'c-hungry-delete-backwards)
396 (defun c-electric-delete-forward (arg)
397 "Delete the following character or whitespace.
398 If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
399 line) then all following whitespace is consumed. If however a prefix
400 argument is supplied, or `c-hungry-delete-key' is nil, or point is
401 inside a literal then the function in the variable `c-delete-function'
402 is called."
403 (interactive "*P")
404 (if (c-save-buffer-state ()
405 (or (not c-hungry-delete-key)
407 (c-in-literal)))
408 (funcall c-delete-function (prefix-numeric-value arg))
409 (c-hungry-delete-forward)))
411 (defun c-hungry-delete-forward ()
412 "Delete the following character or all following whitespace
413 up to the next non-whitespace character.
414 See also \\[c-hungry-delete-backwards]."
415 (interactive)
416 (let ((here (point)))
417 (c-skip-ws-forward)
418 (if (/= (point) here)
419 (delete-region (point) here)
420 (funcall c-delete-function 1))))
422 ;; This function is only used in XEmacs.
423 (defun c-electric-delete (arg)
424 "Deletes preceding or following character or whitespace.
425 This function either deletes forward as `c-electric-delete-forward' or
426 backward as `c-electric-backspace', depending on the configuration: If
427 the function `delete-forward-p' is defined and returns non-nil, it
428 deletes forward. Otherwise it deletes backward.
430 Note: This is the way in XEmacs to choose the correct action for the
431 \[delete] key, whichever key that means. Other flavors don't use this
432 function to control that."
433 (interactive "*P")
434 (if (and (fboundp 'delete-forward-p)
435 (delete-forward-p))
436 (c-electric-delete-forward arg)
437 (c-electric-backspace arg)))
439 ;; This function is only used in XEmacs.
440 (defun c-hungry-delete ()
441 "Delete a non-whitespace char, or all whitespace up to the next non-whitespace char.
442 The direction of deletion depends on the configuration: If the
443 function `delete-forward-p' is defined and returns non-nil, it deletes
444 forward using `c-hungry-delete-forward'. Otherwise it deletes
445 backward using `c-hungry-backspace'.
447 Note: This is the way in XEmacs to choose the correct action for the
448 \[delete] key, whichever key that means. Other flavors don't use this
449 function to control that."
450 (interactive)
451 (if (and (fboundp 'delete-forward-p)
452 (delete-forward-p))
453 (c-hungry-delete-forward)
454 (c-hungry-delete-backwards)))
456 (defun c-electric-pound (arg)
457 "Insert a \"#\".
458 If `c-electric-flag' is set, handle it specially according to the variable
459 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if point is
460 inside a literal or a macro, nothing special happens."
461 (interactive "*P")
462 (if (c-save-buffer-state ()
463 (or arg
464 (not c-electric-flag)
465 (not (memq 'alignleft c-electric-pound-behavior))
466 (save-excursion
467 (skip-chars-backward " \t")
468 (not (bolp)))
469 (save-excursion
470 (and (= (forward-line -1) 0)
471 (progn (end-of-line)
472 (eq (char-before) ?\\))))
473 (c-in-literal)))
474 ;; do nothing special
475 (self-insert-command (prefix-numeric-value arg))
476 ;; place the pound character at the left edge
477 (let ((pos (- (point-max) (point)))
478 (bolp (bolp)))
479 (beginning-of-line)
480 (delete-horizontal-space)
481 (insert (c-last-command-char))
482 (and (not bolp)
483 (goto-char (- (point-max) pos)))
486 (defun c-point-syntax ()
487 ;; Return the syntactic context of the construct at point. (This is NOT
488 ;; nec. the same as the s.c. of the line point is on). N.B. This won't work
489 ;; between the `#' of a cpp thing and what follows (see c-opt-cpp-prefix).
490 (c-save-buffer-state (;; shut this up too
491 (c-echo-syntactic-information-p nil)
492 syntax)
493 (c-tentative-buffer-changes
494 ;; insert a newline to isolate the construct at point for syntactic
495 ;; analysis.
496 (insert-char ?\n 1)
497 ;; In AWK (etc.) or in a macro, make sure this CR hasn't changed
498 ;; the syntax. (There might already be an escaped NL there.)
499 (when (or
500 (save-excursion
501 (c-skip-ws-backward (c-point 'bopl))
502 (c-at-vsemi-p))
503 (let ((pt (point)))
504 (save-excursion
505 (backward-char)
506 (and (c-beginning-of-macro)
507 (progn (c-end-of-macro)
508 (< (point) pt))))))
509 (backward-char)
510 (insert-char ?\\ 1)
511 (forward-char))
512 (let ((c-syntactic-indentation-in-macros t)
513 (c-auto-newline-analysis t))
514 ;; Turn on syntactic macro analysis to help with auto
515 ;; newlines only.
516 (setq syntax (c-guess-basic-syntax))
517 nil))
518 syntax))
520 (defun c-brace-newlines (syntax)
521 ;; A brace stands at point. SYNTAX is the syntactic context of this brace
522 ;; (not necessarily the same as the S.C. of the line it is on). Return
523 ;; NEWLINES, the list containing some combination of the symbols `before'
524 ;; and `after' saying where newlines should be inserted.
525 (c-save-buffer-state
526 ((syms
527 ;; This is the list of brace syntactic symbols that can hang.
528 ;; If any new ones are added to c-offsets-alist, they should be
529 ;; added here as well.
531 ;; The order of this list is important; if SYNTAX has several
532 ;; elements, the element that "wins" is the earliest in SYMS.
533 '(arglist-cont-nonempty ; e.g. an array literal.
534 class-open class-close defun-open defun-close
535 inline-open inline-close
536 brace-list-open brace-list-close
537 brace-list-intro brace-entry-open
538 block-open block-close
539 substatement-open statement-case-open
540 extern-lang-open extern-lang-close
541 namespace-open namespace-close
542 module-open module-close
543 composition-open composition-close
544 inexpr-class-open inexpr-class-close
545 ;; `statement-cont' is here for the case with a brace
546 ;; list opener inside a statement. C.f. CASE B.2 in
547 ;; `c-guess-continued-construct'.
548 statement-cont))
549 ;; shut this up too
550 (c-echo-syntactic-information-p nil)
551 symb-newlines) ; e.g. (substatement-open . (after))
553 (setq symb-newlines
554 ;; Do not try to insert newlines around a special
555 ;; (Pike-style) brace list.
556 (if (and c-special-brace-lists
557 (save-excursion
558 (c-safe (if (= (char-before) ?{)
559 (forward-char -1)
560 (c-forward-sexp -1))
561 (c-looking-at-special-brace-list))))
563 ;; Seek the matching entry in c-hanging-braces-alist.
564 (or (c-lookup-lists
565 syms
566 ;; Substitute inexpr-class and class-open or
567 ;; class-close with inexpr-class-open or
568 ;; inexpr-class-close.
569 (if (assq 'inexpr-class syntax)
570 (cond ((assq 'class-open syntax)
571 '((inexpr-class-open)))
572 ((assq 'class-close syntax)
573 '((inexpr-class-close)))
574 (t syntax))
575 syntax)
576 c-hanging-braces-alist)
577 '(ignore before after)))) ; Default, when not in c-h-b-l.
579 ;; If syntax is a function symbol, then call it using the
580 ;; defined semantics.
581 (if (and (not (consp (cdr symb-newlines)))
582 (functionp (cdr symb-newlines)))
583 (let ((c-syntactic-context syntax))
584 (funcall (cdr symb-newlines)
585 (car symb-newlines)
586 (point)))
587 (cdr symb-newlines))))
589 (defun c-try-one-liner ()
590 ;; Point is just after a newly inserted }. If the non-whitespace
591 ;; content of the braces is a single line of code, compact the whole
592 ;; construct to a single line, if this line isn't too long. The Right
593 ;; Thing is done with comments.
595 ;; Point will be left after the }, regardless of whether the clean-up is
596 ;; done. Return NON-NIL if the clean-up happened, NIL if it didn't.
598 (let ((here (point))
599 (pos (- (point-max) (point)))
600 mbeg1 mend1 mbeg4 mend4
601 eol-col cmnt-pos cmnt-col cmnt-gap)
603 (when
604 (save-excursion
605 (save-restriction
606 ;; Avoid backtracking over a very large block. The one we
607 ;; deal with here can never be more than three lines.
608 (narrow-to-region (save-excursion
609 (forward-line -2)
610 (point))
611 (point))
612 (and (c-safe (c-backward-sexp))
613 (progn
614 (forward-char)
615 (narrow-to-region (point) (1- here)) ; innards of {.}
616 (looking-at
617 (cc-eval-when-compile
618 (concat
619 "\\(" ; (match-beginning 1)
620 "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
621 "\\)" ; (match-end 1)
622 "[^ \t\r\n]+\\([ \t]+[^ \t\r\n]+\\)*" ; non-WS
623 "\\(" ; (match-beginning 4)
624 "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
625 "\\)\\'"))))))) ; (match-end 4) at EOB.
627 (if (c-tentative-buffer-changes
628 (setq mbeg1 (match-beginning 1) mend1 (match-end 1)
629 mbeg4 (match-beginning 4) mend4 (match-end 4))
630 (backward-char) ; back over the `}'
631 (save-excursion
632 (setq cmnt-pos (and (c-backward-single-comment)
633 (- (point) (- mend1 mbeg1)))))
634 (delete-region mbeg4 mend4)
635 (delete-region mbeg1 mend1)
636 (setq eol-col (save-excursion (end-of-line) (current-column)))
638 ;; Necessary to put the closing brace before any line
639 ;; oriented comment to keep it syntactically significant.
640 ;; This isn't necessary for block comments, but the result
641 ;; looks nicer anyway.
642 (when cmnt-pos
643 (delete-char 1) ; the `}' has blundered into a comment
644 (goto-char cmnt-pos)
645 (setq cmnt-col (1+ (current-column)))
646 (setq cmnt-pos (1+ cmnt-pos)) ; we're inserting a `}'
647 (c-skip-ws-backward)
648 (insert-char ?\} 1) ; reinsert the `}' before the comment.
649 (setq cmnt-gap (- cmnt-col (current-column)))
650 (when (zerop cmnt-gap)
651 (insert-char ?\ 1) ; Put a space before a bare comment.
652 (setq cmnt-gap 1)))
654 (or (null c-max-one-liner-length)
655 (zerop c-max-one-liner-length)
656 (<= eol-col c-max-one-liner-length)
657 ;; Can we trim space before comment to make the line fit?
658 (and cmnt-gap
659 (< (- eol-col cmnt-gap) c-max-one-liner-length)
660 (progn (goto-char cmnt-pos)
661 (backward-delete-char-untabify
662 (- eol-col c-max-one-liner-length))
663 t))))
664 (goto-char (- (point-max) pos))))))
666 (defun c-electric-brace (arg)
667 "Insert a brace.
669 If `c-electric-flag' is non-nil, the brace is not inside a literal and a
670 numeric ARG hasn't been supplied, the command performs several electric
671 actions:
673 \(a) If the auto-newline feature is turned on (indicated by \"/la\" on
674 the mode line) newlines are inserted before and after the brace as
675 directed by the settings in `c-hanging-braces-alist'.
677 \(b) Any auto-newlines are indented. The original line is also
678 reindented unless `c-syntactic-indentation' is nil.
680 \(c) If auto-newline is turned on, various newline cleanups based on the
681 settings of `c-cleanup-list' are done."
683 (interactive "*P")
684 (let (safepos literal
685 ;; We want to inhibit blinking the paren since this would be
686 ;; most disruptive. We'll blink it ourselves later on.
687 (old-blink-paren blink-paren-function)
688 blink-paren-function case-fold-search)
690 (c-save-buffer-state ()
691 (setq safepos (c-safe-position (point) (c-parse-state))
692 literal (c-in-literal safepos)))
694 ;; Insert the brace. Note that expand-abbrev might reindent
695 ;; the line here if there's a preceding "else" or something.
696 (self-insert-command (prefix-numeric-value arg))
698 (when (and c-electric-flag (not literal) (not arg))
699 (if (not (looking-at "[ \t]*\\\\?$"))
700 (if c-syntactic-indentation
701 (indent-according-to-mode))
703 (let ( ;; shut this up too
704 (c-echo-syntactic-information-p nil)
705 newlines
706 ln-syntax br-syntax syntax) ; Syntactic context of the original line,
707 ; of the brace itself, of the line the brace ends up on.
708 (c-save-buffer-state ((c-syntactic-indentation-in-macros t)
709 (c-auto-newline-analysis t))
710 (setq ln-syntax (c-guess-basic-syntax)))
711 (if c-syntactic-indentation
712 (c-indent-line ln-syntax))
714 (when c-auto-newline
715 (backward-char)
716 (setq br-syntax (c-point-syntax)
717 newlines (c-brace-newlines br-syntax))
719 ;; Insert the BEFORE newline, if wanted, and reindent the newline.
720 (if (and (memq 'before newlines)
721 (> (current-column) (current-indentation)))
722 (if c-syntactic-indentation
723 ;; Only a plain newline for now - it's indented
724 ;; after the cleanups when the line has its final
725 ;; appearance.
726 (newline)
727 (c-newline-and-indent)))
728 (forward-char)
730 ;; `syntax' is the syntactic context of the line which ends up
731 ;; with the brace on it.
732 (setq syntax (if (memq 'before newlines) br-syntax ln-syntax))
734 ;; Do all appropriate clean ups
735 (let ((here (point))
736 (pos (- (point-max) (point)))
737 mbeg mend
740 ;; `}': clean up empty defun braces
741 (when (c-save-buffer-state ()
742 (and (memq 'empty-defun-braces c-cleanup-list)
743 (eq (c-last-command-char) ?\})
744 (c-intersect-lists '(defun-close class-close inline-close)
745 syntax)
746 (progn
747 (forward-char -1)
748 (c-skip-ws-backward)
749 (eq (char-before) ?\{))
750 ;; make sure matching open brace isn't in a comment
751 (not (c-in-literal))))
752 (delete-region (point) (1- here))
753 (setq here (- (point-max) pos)))
754 (goto-char here)
756 ;; `}': compact to a one-liner defun?
757 (save-match-data
758 (when
759 (and (eq (c-last-command-char) ?\})
760 (memq 'one-liner-defun c-cleanup-list)
761 (c-intersect-lists '(defun-close) syntax)
762 (c-try-one-liner))
763 (setq here (- (point-max) pos))))
765 ;; `{': clean up brace-else-brace and brace-elseif-brace
766 (when (eq (c-last-command-char) ?\{)
767 (cond
768 ((and (memq 'brace-else-brace c-cleanup-list)
769 (re-search-backward
770 (concat "}"
771 "\\([ \t\n]\\|\\\\\n\\)*"
772 "else"
773 "\\([ \t\n]\\|\\\\\n\\)*"
775 "\\=")
776 nil t))
777 (delete-region (match-beginning 0) (match-end 0))
778 (insert-and-inherit "} else {"))
779 ((and (memq 'brace-elseif-brace c-cleanup-list)
780 (progn
781 (goto-char (1- here))
782 (setq mend (point))
783 (c-skip-ws-backward)
784 (setq mbeg (point))
785 (eq (char-before) ?\)))
786 (zerop (c-save-buffer-state nil (c-backward-token-2 1 t)))
787 (eq (char-after) ?\()
788 ; (progn
789 ; (setq tmp (point))
790 (re-search-backward
791 (concat "}"
792 "\\([ \t\n]\\|\\\\\n\\)*"
793 "else"
794 "\\([ \t\n]\\|\\\\\n\\)+"
795 "if"
796 "\\([ \t\n]\\|\\\\\n\\)*"
797 "\\=")
798 nil t);)
799 ;(eq (match-end 0) tmp);
801 (delete-region mbeg mend)
802 (goto-char mbeg)
803 (insert ?\ ))))
805 (goto-char (- (point-max) pos))
807 ;; Indent the line after the cleanups since it might
808 ;; very well indent differently due to them, e.g. if
809 ;; c-indent-one-line-block is used together with the
810 ;; one-liner-defun cleanup.
811 (when c-syntactic-indentation
812 (c-indent-line)))
814 ;; does a newline go after the brace?
815 (if (memq 'after newlines)
816 (c-newline-and-indent))
817 ))))
819 ;; blink the paren
820 (and (eq (c-last-command-char) ?\})
821 (not executing-kbd-macro)
822 old-blink-paren
823 (save-excursion
824 (c-save-buffer-state nil
825 (c-backward-syntactic-ws safepos))
826 (funcall old-blink-paren)))))
828 (defun c-electric-slash (arg)
829 "Insert a slash character.
831 If the slash is inserted immediately after the comment prefix in a c-style
832 comment, the comment might get closed by removing whitespace and possibly
833 inserting a \"*\". See the variable `c-cleanup-list'.
835 Indent the line as a comment, if:
837 1. The slash is second of a \"//\" line oriented comment introducing
838 token and we are on a comment-only-line, or
840 2. The slash is part of a \"*/\" token that closes a block oriented
841 comment.
843 If a numeric ARG is supplied, point is inside a literal, or
844 `c-syntactic-indentation' is nil or `c-electric-flag' is nil, indentation
845 is inhibited."
846 (interactive "*P")
847 (let ((literal (c-save-buffer-state () (c-in-literal)))
848 indentp
849 ;; shut this up
850 (c-echo-syntactic-information-p nil))
852 ;; comment-close-slash cleanup? This DOESN'T need `c-electric-flag' or
853 ;; `c-syntactic-indentation' set.
854 (when (and (not arg)
855 (eq literal 'c)
856 (memq 'comment-close-slash c-cleanup-list)
857 (eq (c-last-command-char) ?/)
858 (looking-at (concat "[ \t]*\\("
859 (regexp-quote comment-end) "\\)?$"))
860 ; (eq c-block-comment-ender "*/") ; C-style comments ALWAYS end in */
861 (save-excursion
862 (save-restriction
863 (narrow-to-region (point-min) (point))
864 (back-to-indentation)
865 (looking-at (concat c-current-comment-prefix "[ \t]*$")))))
866 (delete-region (progn (forward-line 0) (point))
867 (progn (end-of-line) (point)))
868 (insert-char ?* 1)) ; the / comes later. ; Do I need a t (retain sticky properties) here?
870 (setq indentp (and (not arg)
871 c-syntactic-indentation
872 c-electric-flag
873 (eq (c-last-command-char) ?/)
874 (eq (char-before) (if literal ?* ?/))))
875 (self-insert-command (prefix-numeric-value arg))
876 (if indentp
877 (indent-according-to-mode))))
879 (defun c-electric-star (arg)
880 "Insert a star character.
881 If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, and
882 the star is the second character of a C style comment starter on a
883 comment-only-line, indent the line as a comment. If a numeric ARG is
884 supplied, point is inside a literal, or `c-syntactic-indentation' is nil,
885 this indentation is inhibited."
887 (interactive "*P")
888 (self-insert-command (prefix-numeric-value arg))
889 ;; if we are in a literal, or if arg is given do not reindent the
890 ;; current line, unless this star introduces a comment-only line.
891 (if (c-save-buffer-state ()
892 (and c-syntactic-indentation
893 c-electric-flag
894 (not arg)
895 (eq (c-in-literal) 'c)
896 (eq (char-before) ?*)
897 (save-excursion
898 (forward-char -1)
899 (skip-chars-backward "*")
900 (if (eq (char-before) ?/)
901 (forward-char -1))
902 (skip-chars-backward " \t")
903 (bolp))))
904 (let (c-echo-syntactic-information-p) ; shut this up
905 (indent-according-to-mode))
908 (defun c-electric-semi&comma (arg)
909 "Insert a comma or semicolon.
911 If `c-electric-flag' is non-nil, point isn't inside a literal and a
912 numeric ARG hasn't been supplied, the command performs several electric
913 actions:
915 \(a) When the auto-newline feature is turned on (indicated by \"/la\" on
916 the mode line) a newline might be inserted. See the variable
917 `c-hanging-semi&comma-criteria' for how newline insertion is determined.
919 \(b) Any auto-newlines are indented. The original line is also
920 reindented unless `c-syntactic-indentation' is nil.
922 \(c) If auto-newline is turned on, a comma following a brace list or a
923 semicolon following a defun might be cleaned up, depending on the
924 settings of `c-cleanup-list'."
925 (interactive "*P")
926 (let* (lim literal c-syntactic-context
927 (here (point))
928 ;; shut this up
929 (c-echo-syntactic-information-p nil))
931 (c-save-buffer-state ()
932 (setq lim (c-most-enclosing-brace (c-parse-state))
933 literal (c-in-literal lim)))
935 (self-insert-command (prefix-numeric-value arg))
937 (if (and c-electric-flag (not literal) (not arg))
938 ;; do all cleanups and newline insertions if c-auto-newline is on.
939 (if (or (not c-auto-newline)
940 (not (looking-at "[ \t]*\\\\?$")))
941 (if c-syntactic-indentation
942 (c-indent-line))
943 ;; clean ups: list-close-comma or defun-close-semi
944 (let ((pos (- (point-max) (point))))
945 (if (c-save-buffer-state ()
946 (and (or (and
947 (eq (c-last-command-char) ?,)
948 (memq 'list-close-comma c-cleanup-list))
949 (and
950 (eq (c-last-command-char) ?\;)
951 (memq 'defun-close-semi c-cleanup-list)))
952 (progn
953 (forward-char -1)
954 (c-skip-ws-backward)
955 (eq (char-before) ?}))
956 ;; make sure matching open brace isn't in a comment
957 (not (c-in-literal lim))))
958 (delete-region (point) here))
959 (goto-char (- (point-max) pos)))
960 ;; reindent line
961 (when c-syntactic-indentation
962 (setq c-syntactic-context (c-guess-basic-syntax))
963 (c-indent-line c-syntactic-context))
964 ;; check to see if a newline should be added
965 (let ((criteria c-hanging-semi&comma-criteria)
966 answer add-newline-p)
967 (while criteria
968 (setq answer (funcall (car criteria)))
969 ;; only nil value means continue checking
970 (if (not answer)
971 (setq criteria (cdr criteria))
972 (setq criteria nil)
973 ;; only 'stop specifically says do not add a newline
974 (setq add-newline-p (not (eq answer 'stop)))
976 (if add-newline-p
977 (c-newline-and-indent))
978 )))))
980 (defun c-electric-colon (arg)
981 "Insert a colon.
983 If `c-electric-flag' is non-nil, the colon is not inside a literal and a
984 numeric ARG hasn't been supplied, the command performs several electric
985 actions:
987 \(a) If the auto-newline feature is turned on (indicated by \"/la\" on
988 the mode line) newlines are inserted before and after the colon based on
989 the settings in `c-hanging-colons-alist'.
991 \(b) Any auto-newlines are indented. The original line is also
992 reindented unless `c-syntactic-indentation' is nil.
994 \(c) If auto-newline is turned on, whitespace between two colons will be
995 \"cleaned up\" leaving a scope operator, if this action is set in
996 `c-cleanup-list'."
998 (interactive "*P")
999 (let* ((bod (c-point 'bod))
1000 (literal (c-save-buffer-state () (c-in-literal bod)))
1001 newlines is-scope-op
1002 ;; shut this up
1003 (c-echo-syntactic-information-p nil))
1004 (self-insert-command (prefix-numeric-value arg))
1005 ;; Any electric action?
1006 (if (and c-electric-flag (not literal) (not arg))
1007 ;; Unless we're at EOL, only re-indentation happens.
1008 (if (not (looking-at "[ \t]*\\\\?$"))
1009 (if c-syntactic-indentation
1010 (indent-according-to-mode))
1012 ;; scope-operator clean-up?
1013 (let ((pos (- (point-max) (point)))
1014 (here (point)))
1015 (if (c-save-buffer-state () ; Why do we need this? [ACM, 2003-03-12]
1016 (and c-auto-newline
1017 (memq 'scope-operator c-cleanup-list)
1018 (eq (char-before) ?:)
1019 (progn
1020 (forward-char -1)
1021 (c-skip-ws-backward)
1022 (eq (char-before) ?:))
1023 (not (c-in-literal))
1024 (not (eq (char-after (- (point) 2)) ?:))))
1025 (progn
1026 (delete-region (point) (1- here))
1027 (setq is-scope-op t)))
1028 (goto-char (- (point-max) pos)))
1030 ;; indent the current line if it's done syntactically.
1031 (if c-syntactic-indentation
1032 ;; Cannot use the same syntax analysis as we find below,
1033 ;; since that's made with c-syntactic-indentation-in-macros
1034 ;; always set to t.
1035 (indent-according-to-mode))
1037 ;; Calculate where, if anywhere, we want newlines.
1038 (c-save-buffer-state
1039 ((c-syntactic-indentation-in-macros t)
1040 (c-auto-newline-analysis t)
1041 ;; Turn on syntactic macro analysis to help with auto newlines
1042 ;; only.
1043 (syntax (c-guess-basic-syntax))
1044 (elem syntax))
1045 ;; Translate substatement-label to label for this operation.
1046 (while elem
1047 (if (eq (car (car elem)) 'substatement-label)
1048 (setcar (car elem) 'label))
1049 (setq elem (cdr elem)))
1050 ;; some language elements can only be determined by checking
1051 ;; the following line. Let's first look for ones that can be
1052 ;; found when looking on the line with the colon
1053 (setq newlines
1054 (and c-auto-newline
1055 (or (c-lookup-lists '(case-label label access-label)
1056 syntax c-hanging-colons-alist)
1057 (c-lookup-lists '(member-init-intro inher-intro)
1058 (progn
1059 (insert ?\n)
1060 (unwind-protect
1061 (c-guess-basic-syntax)
1062 (delete-char -1)))
1063 c-hanging-colons-alist)))))
1064 ;; does a newline go before the colon? Watch out for already
1065 ;; non-hung colons. However, we don't unhang them because that
1066 ;; would be a cleanup (and anti-social).
1067 (if (and (memq 'before newlines)
1068 (not is-scope-op)
1069 (save-excursion
1070 (skip-chars-backward ": \t")
1071 (not (bolp))))
1072 (let ((pos (- (point-max) (point))))
1073 (forward-char -1)
1074 (c-newline-and-indent)
1075 (goto-char (- (point-max) pos))))
1076 ;; does a newline go after the colon?
1077 (if (and (memq 'after (cdr-safe newlines))
1078 (not is-scope-op))
1079 (c-newline-and-indent))
1080 ))))
1082 (defun c-electric-lt-gt (arg)
1083 "Insert a \"<\" or \">\" character.
1084 If the current language uses angle bracket parens (e.g. template
1085 arguments in C++), try to find out if the inserted character is a
1086 paren and give it paren syntax if appropriate.
1088 If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, the
1089 line will be reindented if the inserted character is a paren or if it
1090 finishes a C++ style stream operator in C++ mode. Exceptions are when a
1091 numeric argument is supplied, or the point is inside a literal."
1093 (interactive "*P")
1094 (let ((c-echo-syntactic-information-p nil)
1095 final-pos found-delim case-fold-search)
1097 (self-insert-command (prefix-numeric-value arg))
1098 (setq final-pos (point))
1100 ;;;; 2010-01-31: There used to be code here to put a syntax-table text
1101 ;;;; property on the new < or > and its mate (if any) when they are template
1102 ;;;; parens. This is now done in an after-change function.
1104 ;; Indent the line if appropriate.
1105 (when (and c-electric-flag c-syntactic-indentation c-recognize-<>-arglists)
1106 (setq found-delim
1107 (if (eq (c-last-command-char) ?<)
1108 ;; If a <, basically see if it's got "template" before it .....
1109 (or (and (progn
1110 (backward-char)
1111 (= (point)
1112 (progn (c-beginning-of-current-token) (point))))
1113 (progn
1114 (c-backward-token-2)
1115 (looking-at c-opt-<>-sexp-key)))
1116 ;; ..... or is a C++ << operator.
1117 (and (c-major-mode-is 'c++-mode)
1118 (progn
1119 (goto-char (1- final-pos))
1120 (c-beginning-of-current-token)
1121 (looking-at "<<"))
1122 (>= (match-end 0) final-pos)))
1124 ;; It's a >. Either a C++ >> operator. ......
1125 (or (and (c-major-mode-is 'c++-mode)
1126 (progn
1127 (goto-char (1- final-pos))
1128 (c-beginning-of-current-token)
1129 (looking-at ">>"))
1130 (>= (match-end 0) final-pos))
1131 ;; ...., or search back for a < which isn't already marked as an
1132 ;; opening template delimiter.
1133 (save-restriction
1134 (widen)
1135 ;; Narrow to avoid `c-forward-<>-arglist' below searching past
1136 ;; our position.
1137 (narrow-to-region (point-min) final-pos)
1138 (goto-char final-pos)
1139 (while
1140 (and
1141 (progn
1142 (c-syntactic-skip-backward "^<;}" nil t)
1143 (eq (char-before) ?<))
1144 (progn
1145 (backward-char)
1146 (looking-at "\\s\("))))
1147 (and (eq (char-after) ?<)
1148 (not (looking-at "\\s\("))
1149 (progn (c-backward-syntactic-ws)
1150 (c-simple-skip-symbol-backward))
1151 (or (looking-at c-opt-<>-sexp-key)
1152 (not (looking-at c-keywords-regexp)))))))))
1154 (goto-char final-pos)
1155 (when found-delim
1156 (indent-according-to-mode)
1157 (when (and (eq (char-before) ?>)
1158 (not executing-kbd-macro)
1159 blink-paren-function)
1160 ;; Currently (2014-10-19), the syntax-table text properties on < and >
1161 ;; are only applied in code called during Emacs redisplay. We thus
1162 ;; explicitly cause a redisplay so that these properties have been
1163 ;; applied when `blink-paren-function' gets called.
1164 (sit-for 0)
1165 (funcall blink-paren-function)))))
1167 (defun c-electric-paren (arg)
1168 "Insert a parenthesis.
1170 If `c-syntactic-indentation' and `c-electric-flag' are both non-nil, the
1171 line is reindented unless a numeric ARG is supplied, or the parenthesis
1172 is inserted inside a literal.
1174 Whitespace between a function name and the parenthesis may get added or
1175 removed; see the variable `c-cleanup-list'.
1177 Also, if `c-electric-flag' and `c-auto-newline' are both non-nil, some
1178 newline cleanups are done if appropriate; see the variable `c-cleanup-list'."
1179 (interactive "*P")
1180 (let ((literal (c-save-buffer-state () (c-in-literal)))
1181 ;; shut this up
1182 (c-echo-syntactic-information-p nil)
1183 case-fold-search)
1184 (self-insert-command (prefix-numeric-value arg))
1186 (if (and (not arg) (not literal))
1187 (let* ( ;; We want to inhibit blinking the paren since this will
1188 ;; be most disruptive. We'll blink it ourselves
1189 ;; afterwards.
1190 (old-blink-paren blink-paren-function)
1191 blink-paren-function)
1192 (if (and c-syntactic-indentation c-electric-flag)
1193 (indent-according-to-mode))
1195 ;; If we're at EOL, check for new-line clean-ups.
1196 (when (and c-electric-flag c-auto-newline
1197 (looking-at "[ \t]*\\\\?$"))
1199 ;; clean up brace-elseif-brace
1200 (when
1201 (and (memq 'brace-elseif-brace c-cleanup-list)
1202 (eq (c-last-command-char) ?\()
1203 (re-search-backward
1204 (concat "}"
1205 "\\([ \t\n]\\|\\\\\n\\)*"
1206 "else"
1207 "\\([ \t\n]\\|\\\\\n\\)+"
1208 "if"
1209 "\\([ \t\n]\\|\\\\\n\\)*"
1211 "\\=")
1212 nil t)
1213 (not (c-save-buffer-state () (c-in-literal))))
1214 (delete-region (match-beginning 0) (match-end 0))
1215 (insert-and-inherit "} else if ("))
1217 ;; clean up brace-catch-brace
1218 (when
1219 (and (memq 'brace-catch-brace c-cleanup-list)
1220 (eq (c-last-command-char) ?\()
1221 (re-search-backward
1222 (concat "}"
1223 "\\([ \t\n]\\|\\\\\n\\)*"
1224 "catch"
1225 "\\([ \t\n]\\|\\\\\n\\)*"
1227 "\\=")
1228 nil t)
1229 (not (c-save-buffer-state () (c-in-literal))))
1230 (delete-region (match-beginning 0) (match-end 0))
1231 (insert-and-inherit "} catch (")))
1233 ;; Check for clean-ups at function calls. These two DON'T need
1234 ;; `c-electric-flag' or `c-syntactic-indentation' set.
1235 ;; Point is currently just after the inserted paren.
1236 (let (beg (end (1- (point))))
1237 (cond
1239 ;; space-before-funcall clean-up?
1240 ((and (memq 'space-before-funcall c-cleanup-list)
1241 (eq (c-last-command-char) ?\()
1242 (save-excursion
1243 (backward-char)
1244 (skip-chars-backward " \t")
1245 (setq beg (point))
1246 (and (c-save-buffer-state () (c-on-identifier))
1247 ;; Don't add a space into #define FOO()....
1248 (not (and (c-beginning-of-macro)
1249 (c-forward-over-cpp-define-id)
1250 (eq (point) beg))))))
1251 (save-excursion
1252 (delete-region beg end)
1253 (goto-char beg)
1254 (insert ?\ )))
1256 ;; compact-empty-funcall clean-up?
1257 ((c-save-buffer-state ()
1258 (and (memq 'compact-empty-funcall c-cleanup-list)
1259 (eq (c-last-command-char) ?\))
1260 (save-excursion
1261 (c-safe (backward-char 2))
1262 (when (looking-at "()")
1263 (setq end (point))
1264 (skip-chars-backward " \t")
1265 (setq beg (point))
1266 (c-on-identifier)))))
1267 (delete-region beg end))))
1268 (and (eq last-input-event ?\))
1269 (not executing-kbd-macro)
1270 old-blink-paren
1271 (funcall old-blink-paren))))))
1273 (defun c-electric-continued-statement ()
1274 "Reindent the current line if appropriate.
1276 This function is used to reindent the line after a keyword which
1277 continues an earlier statement is typed, e.g. an \"else\" or the
1278 \"while\" in a do-while block.
1280 The line is reindented if there is nothing but whitespace before the
1281 keyword on the line, the keyword is not inserted inside a literal, and
1282 `c-electric-flag' and `c-syntactic-indentation' are both non-nil."
1283 (let (;; shut this up
1284 (c-echo-syntactic-information-p nil))
1285 (when (c-save-buffer-state ()
1286 (and c-electric-flag
1287 c-syntactic-indentation
1288 (not (eq (c-last-command-char) ?_))
1289 (= (save-excursion
1290 (skip-syntax-backward "w")
1291 (point))
1292 (c-point 'boi))
1293 (not (c-in-literal (c-point 'bod)))))
1294 ;; Have to temporarily insert a space so that
1295 ;; c-guess-basic-syntax recognizes the keyword. Follow the
1296 ;; space with a nonspace to avoid messing up any whitespace
1297 ;; sensitive meddling that might be done, e.g. by
1298 ;; `c-backslash-region'.
1299 (insert-and-inherit " x")
1300 (unwind-protect
1301 (indent-according-to-mode)
1302 (delete-char -2)))))
1306 (declare-function subword-forward "subword" (&optional arg))
1307 (declare-function subword-backward "subword" (&optional arg))
1309 (cond
1310 ((and (fboundp 'subword-mode) (not (fboundp 'c-subword-mode)))
1311 ;; Recent Emacsen come with their own subword support. Use that.
1312 (define-obsolete-function-alias 'c-subword-mode 'subword-mode "24.3")
1313 (define-obsolete-variable-alias 'c-subword-mode 'subword-mode "24.3"))
1315 ;; Autoload directive for emacsen that doesn't have an older CC Mode
1316 ;; version in the dist.
1317 (autoload 'c-subword-mode "cc-subword"
1318 "Mode enabling subword movement and editing keys." t)))
1320 ;; "nomenclature" functions + c-scope-operator.
1321 (defun c-forward-into-nomenclature (&optional arg)
1322 "Compatibility alias for `c-forward-subword'."
1323 (interactive "p")
1324 (if (fboundp 'subword-mode)
1325 (progn
1326 (require 'subword)
1327 (subword-forward arg))
1328 (require 'cc-subword)
1329 (c-forward-subword arg)))
1330 (make-obsolete 'c-forward-into-nomenclature
1331 (if (fboundp 'subword-mode) 'subword-forward 'c-forward-subword)
1332 "23.2")
1334 (defun c-backward-into-nomenclature (&optional arg)
1335 "Compatibility alias for `c-backward-subword'."
1336 (interactive "p")
1337 (if (fboundp 'subword-mode)
1338 (progn
1339 (require 'subword)
1340 (subword-backward arg))
1341 (require 'cc-subword)
1342 (c-backward-subword arg)))
1343 (make-obsolete
1344 'c-backward-into-nomenclature
1345 (if (fboundp 'subword-mode) 'subword-backward 'c-backward-subword) "23.2")
1347 (defun c-scope-operator ()
1348 "Insert a double colon scope operator at point.
1349 No indentation or other \"electric\" behavior is performed."
1350 (interactive "*")
1351 (insert-and-inherit "::"))
1354 ;; Movement (etc.) by defuns.
1355 (defun c-in-function-trailer-p (&optional lim)
1356 ;; Return non-nil if point is between the closing brace and the semicolon of
1357 ;; a brace construct which needs a semicolon, e.g. within the "variables"
1358 ;; portion of a declaration like "struct foo {...} bar ;".
1360 ;; Return the position of the main declaration. Otherwise, return nil.
1361 ;; Point is assumed to be at the top level and outside of any macro or
1362 ;; literal.
1364 ;; If LIM is non-nil, it is the bound on a the backward search for the
1365 ;; beginning of the declaration.
1367 ;; This function might do hidden buffer changes.
1368 (and c-opt-block-decls-with-vars-key
1369 (save-excursion
1370 (c-syntactic-skip-backward "^;}" lim)
1371 (let ((eo-block (point))
1372 bod)
1373 (and (eq (char-before) ?\})
1374 (eq (car (c-beginning-of-decl-1 lim)) 'previous)
1375 (setq bod (point))
1376 ;; Look for struct or union or ... If we find one, it might
1377 ;; be the return type of a function, or the like. Exclude
1378 ;; this case.
1379 (c-syntactic-re-search-forward
1380 (concat "[;=\(\[{]\\|\\("
1381 c-opt-block-decls-with-vars-key
1382 "\\)")
1383 eo-block t t t)
1384 (match-beginning 1) ; Is there a "struct" etc., somewhere?
1385 (not (eq (char-before) ?_))
1386 (c-syntactic-re-search-forward "[;=\(\[{]" eo-block t t t)
1387 (eq (char-before) ?\{)
1388 bod)))))
1390 (defun c-where-wrt-brace-construct ()
1391 ;; Determine where we are with respect to functions (or other brace
1392 ;; constructs, included in the term "function" in the rest of this comment).
1393 ;; Point is assumed to be outside any macro or literal.
1394 ;; This is used by c-\(beginning\|end\)-of-defun.
1396 ;; Return one of these symbols:
1397 ;; at-header : we're at the start of a function's header.
1398 ;; in-header : we're inside a function's header, this extending right
1399 ;; up to the brace. This bit includes any k&r declarations.
1400 ;; in-block : we're inside a function's brace block.
1401 ;; in-trailer : we're in the area between the "}" and ";" of something
1402 ;; like "struct foo {...} bar, baz;".
1403 ;; at-function-end : we're just after the closing brace (or semicolon) that
1404 ;; terminates the function.
1405 ;; outwith-function: we're not at or in any function. Being inside a
1406 ;; non-brace construct also counts as 'outwith-function'.
1408 ;; This function might do hidden buffer changes.
1409 (save-excursion
1410 (let* (kluge-start
1411 decl-result brace-decl-p
1412 (start (point))
1413 (paren-state (c-parse-state))
1414 (least-enclosing (c-least-enclosing-brace paren-state)))
1416 (cond
1417 ((and least-enclosing
1418 (eq (char-after least-enclosing) ?\{))
1419 'in-block)
1420 ((c-in-function-trailer-p)
1421 'in-trailer)
1422 ((and (not least-enclosing)
1423 (consp paren-state)
1424 (consp (car paren-state))
1425 (eq start (cdar paren-state)))
1426 'at-function-end)
1428 ;; Find the start of the current declaration. NOTE: If we're in the
1429 ;; variables after a "struct/eval" type block, we don't get to the
1430 ;; real declaration here - we detect and correct for this later.
1432 ;;If we're in the parameters' parens, move back out of them.
1433 (if least-enclosing (goto-char least-enclosing))
1434 ;; Kluge so that c-beginning-of-decl-1 won't go back if we're already
1435 ;; at a declaration.
1436 (if (or (and (eolp) (not (eobp))) ; EOL is matched by "\\s>"
1437 (not (looking-at
1438 "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\|\\s!\\)")))
1439 (forward-char))
1440 (setq kluge-start (point))
1441 (setq decl-result
1442 (car (c-beginning-of-decl-1
1443 ;; NOTE: If we're in a K&R region, this might be the start
1444 ;; of a parameter declaration, not the actual function.
1445 ;; It might also leave us at a label or "label" like
1446 ;; "private:".
1447 (and least-enclosing ; LIMIT for c-b-of-decl-1
1448 (c-safe-position least-enclosing paren-state)))))
1450 ;; Has the declaration we've gone back to got braces?
1451 (or (eq decl-result 'label)
1452 (setq brace-decl-p
1453 (save-excursion
1454 (and (c-syntactic-re-search-forward "[;{]" nil t t)
1455 (or (eq (char-before) ?\{)
1456 (and c-recognize-knr-p
1457 ;; Might have stopped on the
1458 ;; ';' in a K&R argdecl. In
1459 ;; that case the declaration
1460 ;; should contain a block.
1461 (c-in-knr-argdecl)))))))
1463 (cond
1464 ((or (eq decl-result 'label) ; e.g. "private:" or invalid syntax.
1465 (= (point) kluge-start)) ; might be BOB or unbalanced parens.
1466 'outwith-function)
1467 ((eq decl-result 'same)
1468 (if brace-decl-p
1469 (if (eq (point) start)
1470 'at-header
1471 'in-header)
1472 'outwith-function))
1473 ((eq decl-result 'previous)
1474 (if (and (not brace-decl-p)
1475 (c-in-function-trailer-p))
1476 'at-function-end
1477 'outwith-function))
1478 (t (error
1479 "c-where-wrt-brace-construct: c-beginning-of-decl-1 returned %s"
1480 decl-result))))))))
1482 (defun c-backward-to-nth-BOF-{ (n where)
1483 ;; Skip to the opening brace of the Nth function before point. If
1484 ;; point is inside a function, this counts as the first. Point must be
1485 ;; outside any comment/string or macro.
1487 ;; N must be strictly positive.
1488 ;; WHERE describes the position of point, one of the symbols `at-header',
1489 ;; `in-header', `in-block', `in-trailer', `at-function-end',
1490 ;; `outwith-function' as returned by c-where-wrt-brace-construct.
1492 ;; If we run out of functions, leave point at BOB. Return zero on success,
1493 ;; otherwise the number of {s still to go.
1495 ;; This function may do hidden buffer changes
1496 (cond
1497 ;; What we do to go back the first defun depends on where we start.
1498 ((bobp))
1499 ((eq where 'in-block)
1500 (goto-char (c-least-enclosing-brace (c-parse-state)))
1501 (setq n (1- n)))
1502 ((eq where 'in-header)
1503 (c-syntactic-re-search-forward "{")
1504 (backward-char)
1505 (setq n (1- n)))
1506 ((memq where '(at-header outwith-function at-function-end in-trailer))
1507 (c-syntactic-skip-backward "^}")
1508 (when (eq (char-before) ?\})
1509 (backward-sexp)
1510 (setq n (1- n))))
1511 (t (error "Unknown `where' %s in c-backward-to-nth-EOF-{" where)))
1513 ;; Each time round the loop, go back to a "{" at the outermost level.
1514 (while (and (> n 0) (not (bobp)))
1515 (c-parse-state) ; This call speeds up the following one
1516 ; by a factor of ~6. Hmmm. 2006/4/5.
1517 (c-syntactic-skip-backward "^}")
1518 (when (eq (char-before) ?\})
1519 (backward-sexp)
1520 (setq n (1- n))))
1523 (defun c-narrow-to-most-enclosing-decl-block (&optional inclusive)
1524 ;; If we are inside a decl-block (in the sense of c-looking-at-decl-block),
1525 ;; i.e. something like namespace{} or extern{}, narrow to the insides of
1526 ;; that block (NOT including the enclosing braces) if INCLUSIVE is nil,
1527 ;; otherwise include the braces. If the closing brace is missing,
1528 ;; (point-max) is used instead.
1529 (let ((paren-state (c-parse-state))
1530 encl-decl)
1531 (setq encl-decl (and paren-state (c-most-enclosing-decl-block paren-state)))
1532 (if encl-decl
1533 (save-excursion
1534 (narrow-to-region
1535 (if inclusive
1536 (progn (goto-char encl-decl)
1537 (c-beginning-of-decl-1)
1538 (point))
1539 (1+ encl-decl))
1540 (progn
1541 (goto-char encl-decl)
1542 (or (c-safe (forward-list)
1543 (if inclusive
1544 (point)
1545 (1- (point))))
1546 (point-max))))))))
1548 (defun c-widen-to-enclosing-decl-scope (paren-state orig-point-min orig-point-max)
1549 ;; Narrow the buffer to the innermost declaration scope (e.g. a class, a
1550 ;; namespace or the "whole buffer") recorded in PAREN-STATE, the bounding
1551 ;; braces NOT being included in the resulting region. On no account may the
1552 ;; final region exceed that bounded by ORIG-POINT-MIN, ORIG-POINT-MAX.
1553 ;; PAREN-STATE is a list of buffer positions in the style of
1554 ;; (c-parse-state), one of which will be that of the desired opening brace,
1555 ;; if there is one.
1557 ;; Return the position of the enclosing opening brace, or nil
1558 (let (encl-decl) ; putative position of decl-scope's opening brace.
1559 (save-restriction
1560 (narrow-to-region orig-point-min orig-point-max)
1561 (setq encl-decl (and paren-state
1562 (c-most-enclosing-decl-block paren-state))))
1563 (if encl-decl
1564 (progn
1565 (widen)
1566 (narrow-to-region (1+ encl-decl)
1567 (save-excursion
1568 (goto-char encl-decl)
1569 (or (c-safe (forward-list)
1570 (1- (point)))
1571 orig-point-max)))
1572 encl-decl)
1573 (narrow-to-region orig-point-min orig-point-max)
1574 nil)))
1576 (eval-and-compile
1577 (defmacro c-while-widening-to-decl-block (condition)
1578 ;; Repeatedly evaluate CONDITION until it returns nil. After each
1579 ;; evaluation, if `c-defun-tactic' is set appropriately, widen to innards
1580 ;; of the next enclosing declaration block (e.g. namespace, class), or the
1581 ;; buffer's original restriction.
1583 ;; This is a very special purpose macro, which assumes the existence of
1584 ;; several variables. It is for use only in c-beginning-of-defun and
1585 ;; c-end-of-defun.
1586 `(while
1587 (and ,condition
1588 (eq c-defun-tactic 'go-outward)
1589 lim)
1590 (setq paren-state (c-whack-state-after lim paren-state))
1591 (setq lim (c-widen-to-enclosing-decl-scope
1592 paren-state orig-point-min orig-point-max))
1593 (setq where 'in-block))))
1595 (defun c-beginning-of-defun (&optional arg)
1596 "Move backward to the beginning of a defun.
1597 Every top level declaration that contains a brace paren block is
1598 considered to be a defun.
1600 With a positive argument, move backward that many defuns. A negative
1601 argument -N means move forward to the Nth following beginning. Return
1602 t unless search stops due to beginning or end of buffer.
1604 Unlike the built-in `beginning-of-defun' this tries to be smarter
1605 about finding the char with open-parenthesis syntax that starts the
1606 defun."
1608 (interactive "p")
1609 (or arg (setq arg 1))
1611 (or (not (eq this-command 'c-beginning-of-defun))
1612 (eq last-command 'c-beginning-of-defun)
1613 (c-region-is-active-p)
1614 (push-mark))
1616 (c-save-buffer-state
1617 (beginning-of-defun-function end-of-defun-function
1618 (start (point))
1619 (paren-state (copy-tree (c-parse-state))) ; This must not share list
1620 ; structure with other users of c-state-cache.
1621 (orig-point-min (point-min)) (orig-point-max (point-max))
1622 lim ; Position of { which has been widened to.
1623 where pos case-fold-search)
1625 (save-restriction
1626 (if (eq c-defun-tactic 'go-outward)
1627 (setq lim (c-widen-to-enclosing-decl-scope ; e.g. class, namespace.
1628 paren-state orig-point-min orig-point-max)))
1630 ;; Move back out of any macro/comment/string we happen to be in.
1631 (c-beginning-of-macro)
1632 (setq pos (c-literal-limits))
1633 (if pos (goto-char (car pos)))
1635 (setq where (c-where-wrt-brace-construct))
1637 (if (< arg 0)
1638 ;; Move forward to the closing brace of a function.
1639 (progn
1640 (if (memq where '(at-function-end outwith-function))
1641 (setq arg (1+ arg)))
1642 (if (< arg 0)
1643 (c-while-widening-to-decl-block
1644 (< (setq arg (- (c-forward-to-nth-EOF-} (- arg) where))) 0)))
1645 ;; Move forward to the next opening brace....
1646 (when (and (= arg 0)
1647 (progn
1648 (c-while-widening-to-decl-block
1649 (not (c-syntactic-re-search-forward "{" nil 'eob)))
1650 (eq (char-before) ?{)))
1651 (backward-char)
1652 ;; ... and backward to the function header.
1653 (c-beginning-of-decl-1)
1656 ;; Move backward to the opening brace of a function, making successively
1657 ;; larger portions of the buffer visible as necessary.
1658 (when (> arg 0)
1659 (c-while-widening-to-decl-block
1660 (> (setq arg (c-backward-to-nth-BOF-{ arg where)) 0)))
1662 (when (eq arg 0)
1663 ;; Go backward to this function's header.
1664 (c-beginning-of-decl-1)
1666 (setq pos (point))
1667 ;; We're now there, modulo comments and whitespace.
1668 ;; Try to be line oriented; position point at the closest
1669 ;; preceding boi that isn't inside a comment, but if we hit
1670 ;; the previous declaration then we use the current point
1671 ;; instead.
1672 (while (and (/= (point) (c-point 'boi))
1673 (c-backward-single-comment)))
1674 (if (/= (point) (c-point 'boi))
1675 (goto-char pos)))
1677 (c-keep-region-active)
1678 (= arg 0)))))
1680 (defun c-forward-to-nth-EOF-} (n where)
1681 ;; Skip to the closing brace of the Nth function after point. If
1682 ;; point is inside a function, this counts as the first. Point must be
1683 ;; outside any comment/string or macro.
1685 ;; N must be strictly positive.
1686 ;; WHERE describes the position of point, one of the symbols `at-header',
1687 ;; `in-header', `in-block', `in-trailer', `at-function-end',
1688 ;; `outwith-function' as returned by c-where-wrt-brace-construct.
1690 ;; If we run out of functions, leave point at EOB. Return zero on success,
1691 ;; otherwise the number of }s still to go.
1693 ;; This function may do hidden buffer changes.
1695 (cond
1696 ;; What we do to go forward over the first defun depends on where we
1697 ;; start. We go to the closing brace of that defun, even when we go
1698 ;; backwards to it (in a "struct foo {...} bar ;").
1699 ((eobp))
1700 ((eq where 'in-block)
1701 (goto-char (c-least-enclosing-brace (c-parse-state)))
1702 (forward-sexp)
1703 (setq n (1- n)))
1704 ((eq where 'in-trailer)
1705 (c-syntactic-skip-backward "^}")
1706 (setq n (1- n)))
1707 ((memq where '(at-function-end outwith-function at-header in-header))
1708 (when (c-syntactic-re-search-forward "{" nil 'eob)
1709 (backward-char)
1710 (forward-sexp)
1711 (setq n (1- n))))
1712 (t (error "c-forward-to-nth-EOF-}: `where' is %s" where)))
1714 ;; Each time round the loop, go forward to a "}" at the outermost level.
1715 (while (and (> n 0) (not (eobp)))
1716 ;(c-parse-state) ; This call speeds up the following one by a factor
1717 ; of ~6. Hmmm. 2006/4/5.
1718 (when (c-syntactic-re-search-forward "{" nil 'eob)
1719 (backward-char)
1720 (forward-sexp))
1721 (setq n (1- n)))
1724 (defun c-end-of-defun (&optional arg)
1725 "Move forward to the end of a top level declaration.
1726 With argument, do it that many times. Negative argument -N means move
1727 back to Nth preceding end. Returns t unless search stops due to
1728 beginning or end of buffer.
1730 An end of a defun occurs right after the close-parenthesis that matches
1731 the open-parenthesis that starts a defun; see `beginning-of-defun'."
1732 (interactive "p")
1733 (or arg (setq arg 1))
1735 (or (not (eq this-command 'c-end-of-defun))
1736 (eq last-command 'c-end-of-defun)
1737 (c-region-is-active-p)
1738 (push-mark))
1740 (c-save-buffer-state
1741 (beginning-of-defun-function end-of-defun-function
1742 (start (point))
1743 (paren-state (copy-tree (c-parse-state))) ; This must not share list
1744 ; structure with other users of c-state-cache.
1745 (orig-point-min (point-min)) (orig-point-max (point-max))
1747 where pos case-fold-search)
1749 (save-restriction
1750 (if (eq c-defun-tactic 'go-outward)
1751 (setq lim (c-widen-to-enclosing-decl-scope ; e.g. class, namespace
1752 paren-state orig-point-min orig-point-max)))
1754 ;; Move back out of any macro/comment/string we happen to be in.
1755 (c-beginning-of-macro)
1756 (setq pos (c-literal-limits))
1757 (if pos (goto-char (car pos)))
1759 (setq where (c-where-wrt-brace-construct))
1761 (if (< arg 0)
1762 ;; Move backwards to the } of a function
1763 (progn
1764 (if (memq where '(at-header outwith-function))
1765 (setq arg (1+ arg)))
1766 (if (< arg 0)
1767 (c-while-widening-to-decl-block
1768 (< (setq arg (- (c-backward-to-nth-BOF-{ (- arg) where))) 0)))
1769 (if (= arg 0)
1770 (c-while-widening-to-decl-block
1771 (progn (c-syntactic-skip-backward "^}")
1772 (not (eq (char-before) ?}))))))
1774 ;; Move forward to the } of a function
1775 (if (> arg 0)
1776 (c-while-widening-to-decl-block
1777 (> (setq arg (c-forward-to-nth-EOF-} arg where)) 0))))
1779 ;; Do we need to move forward from the brace to the semicolon?
1780 (when (eq arg 0)
1781 (if (c-in-function-trailer-p) ; after "}" of struct/enum, etc.
1782 (c-syntactic-re-search-forward ";"))
1784 (setq pos (point))
1785 ;; We're there now, modulo comments and whitespace.
1786 ;; Try to be line oriented; position point after the next
1787 ;; newline that isn't inside a comment, but if we hit the
1788 ;; next declaration then we use the current point instead.
1789 (while (and (not (bolp))
1790 (not (looking-at "\\s *$"))
1791 (c-forward-single-comment)))
1792 (cond ((bolp))
1793 ((looking-at "\\s *$")
1794 (forward-line 1))
1796 (goto-char pos))))
1798 (c-keep-region-active)
1799 (= arg 0))))
1801 (defun c-defun-name ()
1802 "Return the name of the current defun, or NIL if there isn't one.
1803 \"Defun\" here means a function, or other top level construct
1804 with a brace block."
1805 (interactive)
1806 (c-save-buffer-state
1807 (beginning-of-defun-function end-of-defun-function
1808 where pos name-end case-fold-search)
1810 (save-restriction
1811 (widen)
1812 (save-excursion
1813 ;; Move back out of any macro/comment/string we happen to be in.
1814 (c-beginning-of-macro)
1815 (setq pos (c-literal-limits))
1816 (if pos (goto-char (car pos)))
1818 (setq where (c-where-wrt-brace-construct))
1820 ;; Move to the beginning of the current defun, if any, if we're not
1821 ;; already there.
1822 (if (eq where 'outwith-function)
1824 (unless (eq where 'at-header)
1825 (c-backward-to-nth-BOF-{ 1 where)
1826 (c-beginning-of-decl-1))
1828 ;; Pick out the defun name, according to the type of defun.
1829 (cond
1830 ;; struct, union, enum, or similar:
1831 ((and (looking-at c-type-prefix-key)
1832 (progn (c-forward-token-2 2) ; over "struct foo "
1833 (or (eq (char-after) ?\{)
1834 (looking-at c-symbol-key)))) ; "struct foo bar ..."
1835 (save-match-data (c-forward-token-2))
1836 (when (eq (char-after) ?\{)
1837 (c-backward-token-2)
1838 (looking-at c-symbol-key))
1839 (match-string-no-properties 0))
1841 ((looking-at "DEFUN\\s-*(") ;"DEFUN\\_>") think of XEmacs!
1842 ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory
1843 ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK
1844 (down-list 1)
1845 (c-forward-syntactic-ws)
1846 (when (eq (char-after) ?\")
1847 (forward-sexp 1)
1848 (c-forward-token-2)) ; over the comma and following WS.
1849 (buffer-substring-no-properties
1850 (point)
1851 (progn
1852 (c-forward-token-2)
1853 (when (looking-at ":") ; CLISP: DEFUN(PACKAGE:LISP-SYMBOL,...)
1854 (skip-chars-forward "^,"))
1855 (c-backward-syntactic-ws)
1856 (point))))
1858 ((looking-at "DEF[a-zA-Z0-9_]* *( *\\([^, ]*\\) *,")
1859 ;; DEFCHECKER(sysconf_arg,prefix=_SC,default=, ...) ==> sysconf_arg
1860 ;; DEFFLAGSET(syslog_opt_flags,LOG_PID ...) ==> syslog_opt_flags
1861 (match-string-no-properties 1))
1863 ;; Objc selectors.
1864 ((assq 'objc-method-intro (c-guess-basic-syntax))
1865 (let ((bound (save-excursion (c-end-of-statement) (point)))
1866 (kw-re (concat "\\(?:" c-symbol-key "\\)?:"))
1867 (stretches))
1868 (when (c-syntactic-re-search-forward c-symbol-key bound t t t)
1869 (push (match-string-no-properties 0) stretches)
1870 (while (c-syntactic-re-search-forward kw-re bound t t t)
1871 (push (match-string-no-properties 0) stretches)))
1872 (apply 'concat (nreverse stretches))))
1875 ;; Normal function or initializer.
1876 (when (c-syntactic-re-search-forward "[{(]" nil t)
1877 (backward-char)
1878 (c-backward-syntactic-ws)
1879 (when (eq (char-before) ?\=) ; struct foo bar = {0, 0} ;
1880 (c-backward-token-2)
1881 (c-backward-syntactic-ws))
1882 (setq name-end (point))
1883 (c-backward-token-2)
1884 (buffer-substring-no-properties (point) name-end)))))))))
1886 (defun c-declaration-limits (near)
1887 ;; Return a cons of the beginning and end positions of the current
1888 ;; top level declaration or macro. If point is not inside any then
1889 ;; nil is returned, unless NEAR is non-nil in which case the closest
1890 ;; following one is chosen instead (if there is any). The end
1891 ;; position is at the next line, providing there is one before the
1892 ;; declaration.
1894 ;; This function might do hidden buffer changes.
1895 (save-excursion
1896 (save-restriction
1897 (when (eq c-defun-tactic 'go-outward)
1898 (c-narrow-to-most-enclosing-decl-block t) ; e.g. class, namespace
1899 (or (save-restriction
1900 (c-narrow-to-most-enclosing-decl-block nil)
1902 ;; Note: Some code duplication in `c-beginning-of-defun' and
1903 ;; `c-end-of-defun'.
1904 (catch 'exit
1905 (let ((start (point))
1906 (paren-state (c-parse-state))
1907 lim pos end-pos)
1908 (unless (c-safe
1909 (goto-char (c-least-enclosing-brace paren-state))
1910 ;; If we moved to the outermost enclosing paren
1911 ;; then we can use c-safe-position to set the
1912 ;; limit. Can't do that otherwise since the
1913 ;; earlier paren pair on paren-state might very
1914 ;; well be part of the declaration we should go
1915 ;; to.
1916 (setq lim (c-safe-position (point) paren-state))
1918 ;; At top level. Make sure we aren't inside a literal.
1919 (setq pos (c-literal-limits
1920 (c-safe-position (point) paren-state)))
1921 (if pos (goto-char (car pos))))
1923 (when (c-beginning-of-macro)
1924 (throw 'exit
1925 (cons (point)
1926 (save-excursion
1927 (c-end-of-macro)
1928 (forward-line 1)
1929 (point)))))
1931 (setq pos (point))
1932 (when (or (eq (car (c-beginning-of-decl-1 lim)) 'previous)
1933 (= pos (point)))
1934 ;; We moved back over the previous defun. Skip to the next
1935 ;; one. Not using c-forward-syntactic-ws here since we
1936 ;; should not skip a macro. We can also be directly after
1937 ;; the block in a `c-opt-block-decls-with-vars-key'
1938 ;; declaration, but then we won't move significantly far
1939 ;; here.
1940 (goto-char pos)
1941 (c-forward-comments)
1943 (when (and near (c-beginning-of-macro))
1944 (throw 'exit
1945 (cons (point)
1946 (save-excursion
1947 (c-end-of-macro)
1948 (forward-line 1)
1949 (point))))))
1951 (if (eobp) (throw 'exit nil))
1953 ;; Check if `c-beginning-of-decl-1' put us after the block in a
1954 ;; declaration that doesn't end there. We're searching back and
1955 ;; forth over the block here, which can be expensive.
1956 (setq pos (point))
1957 (if (and c-opt-block-decls-with-vars-key
1958 (progn
1959 (c-backward-syntactic-ws)
1960 (eq (char-before) ?}))
1961 (eq (car (c-beginning-of-decl-1))
1962 'previous)
1963 (save-excursion
1964 (c-end-of-decl-1)
1965 (and (> (point) pos)
1966 (setq end-pos (point)))))
1968 (goto-char pos))
1970 (if (and (not near) (> (point) start))
1973 ;; Try to be line oriented; position the limits at the
1974 ;; closest preceding boi, and after the next newline, that
1975 ;; isn't inside a comment, but if we hit a neighboring
1976 ;; declaration then we instead use the exact declaration
1977 ;; limit in that direction.
1978 (cons (progn
1979 (setq pos (point))
1980 (while (and (/= (point) (c-point 'boi))
1981 (c-backward-single-comment)))
1982 (if (/= (point) (c-point 'boi))
1984 (point)))
1985 (progn
1986 (if end-pos
1987 (goto-char end-pos)
1988 (c-end-of-decl-1))
1989 (setq pos (point))
1990 (while (and (not (bolp))
1991 (not (looking-at "\\s *$"))
1992 (c-forward-single-comment)))
1993 (cond ((bolp)
1994 (point))
1995 ((looking-at "\\s *$")
1996 (forward-line 1)
1997 (point))
1999 pos))))))))
2000 (and (not near)
2001 (goto-char (point-min))
2002 (c-forward-decl-or-cast-1 -1 nil nil)
2003 (eq (char-after) ?\{)
2004 (cons (point-min) (point-max))))))))
2006 (defun c-mark-function ()
2007 "Put mark at end of the current top-level declaration or macro, point at beginning.
2008 If point is not inside any then the closest following one is
2009 chosen. Each successive call of this command extends the marked
2010 region by one function.
2012 A mark is left where the command started, unless the region is already active
2013 \(in Transient Mark mode).
2015 As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
2016 function does not require the declaration to contain a brace block."
2017 (interactive)
2019 (let (decl-limits case-fold-search)
2020 (c-save-buffer-state nil
2021 ;; We try to be line oriented, unless there are several
2022 ;; declarations on the same line.
2023 (if (looking-at c-syntactic-eol)
2024 (c-backward-token-2 1 nil (c-point 'bol)))
2025 (setq decl-limits (c-declaration-limits t)))
2027 (if (not decl-limits)
2028 (error "Cannot find any declaration")
2029 (let* ((extend-region-p
2030 (and (eq this-command 'c-mark-function)
2031 (eq last-command 'c-mark-function)))
2032 (push-mark-p (and (eq this-command 'c-mark-function)
2033 (not extend-region-p)
2034 (not (c-region-is-active-p)))))
2035 (if push-mark-p (push-mark (point)))
2036 (if extend-region-p
2037 (progn
2038 (exchange-point-and-mark)
2039 (setq decl-limits (c-declaration-limits t))
2040 (when (not decl-limits)
2041 (exchange-point-and-mark)
2042 (error "Cannot find any declaration"))
2043 (goto-char (cdr decl-limits))
2044 (exchange-point-and-mark))
2045 (goto-char (car decl-limits))
2046 (push-mark (cdr decl-limits) nil t))))))
2048 (defun c-cpp-define-name ()
2049 "Return the name of the current CPP macro, or NIL if we're not in one."
2050 (interactive)
2051 (let (case-fold-search)
2052 (save-excursion
2053 (and c-opt-cpp-macro-define-start
2054 (c-beginning-of-macro)
2055 (looking-at c-opt-cpp-macro-define-start)
2056 (match-string-no-properties 1)))))
2059 ;; Movement by statements.
2060 (defun c-in-comment-line-prefix-p ()
2061 ;; Point is within a comment. Is it also within a comment-prefix?
2062 ;; Space at BOL which precedes a comment-prefix counts as part of it.
2064 ;; This function might do hidden buffer changes.
2065 (let ((here (point)))
2066 (save-excursion
2067 (beginning-of-line)
2068 (skip-chars-forward " \t")
2069 (and (looking-at c-current-comment-prefix)
2070 (/= (match-beginning 0) (match-end 0))
2071 (< here (match-end 0))))))
2073 (defun c-narrow-to-comment-innards (range)
2074 ;; Narrow to the "inside" of the comment (block) defined by range, as
2075 ;; follows:
2077 ;; A c-style block comment has its opening "/*" and its closing "*/" (if
2078 ;; present) removed. A c++-style line comment retains its opening "//" but
2079 ;; has any final NL removed. If POINT is currently outwith these innards,
2080 ;; move it to the appropriate boundary.
2082 ;; This narrowing simplifies the sentence movement functions, since it
2083 ;; eliminates awkward things at the boundaries of the comment (block).
2085 ;; This function might do hidden buffer changes.
2086 (let* ((lit-type (c-literal-type range))
2087 (beg (if (eq lit-type 'c) (+ (car range) 2) (car range)))
2088 (end (if (eq lit-type 'c)
2089 (if (and (eq (char-before (cdr range)) ?/)
2090 (eq (char-before (1- (cdr range))) ?*))
2091 (- (cdr range) 2)
2092 (point-max))
2093 (if (eq (cdr range) (point-max))
2094 (point-max)
2095 (- (cdr range) 1)))))
2096 (if (> (point) end)
2097 (goto-char end)) ; This would be done automatically by ...
2098 (if (< (point) beg)
2099 (goto-char beg)) ; ... narrow-to-region but is not documented.
2100 (narrow-to-region beg end)))
2102 (defun c-beginning-of-sentence-in-comment (range)
2103 ;; Move backwards to the "beginning of a sentence" within the comment
2104 ;; defined by RANGE, a cons of its starting and ending positions. If we
2105 ;; find a BOS, return NIL. Otherwise, move point to just before the start
2106 ;; of the comment and return T.
2108 ;; The BOS is either text which follows a regexp match of sentence-end,
2109 ;; or text which is a beginning of "paragraph".
2110 ;; Comment-prefixes are treated like WS when calculating BOSes or BOPs.
2112 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
2113 ;; It is not a general function, but is intended only for calling from
2114 ;; c-move-over-sentence. Not all preconditions have been explicitly stated.
2116 ;; This function might do hidden buffer changes.
2117 (save-match-data
2118 (let ((start-point (point)))
2119 (save-restriction
2120 (c-narrow-to-comment-innards range) ; This may move point back.
2121 (let* ((here (point))
2122 last
2123 (here-filler ; matches WS and comment-prefixes at point.
2124 (concat "\\=\\(^[ \t]*\\(" c-current-comment-prefix "\\)"
2125 "\\|[ \t\n\r\f]\\)*"))
2126 (prefix-at-bol-here ; matches WS and prefix at BOL, just before point
2127 (concat "^[ \t]*\\(" c-current-comment-prefix "\\)[ \t\n\r\f]*\\="))
2128 ;; First, find the previous paragraph start, if any.
2129 (par-beg ; point where non-WS/non-prefix text of paragraph starts.
2130 (save-excursion
2131 (forward-paragraph -1) ; uses cc-mode values of
2132 ; paragraph-\(start\|separate\)
2133 (if (> (re-search-forward here-filler nil t) here)
2134 (goto-char here))
2135 (when (>= (point) here)
2136 (forward-paragraph -2)
2137 (if (> (re-search-forward here-filler nil t) here)
2138 (goto-char here)))
2139 (point))))
2141 ;; Now seek successively earlier sentence ends between PAR-BEG and
2142 ;; HERE, until the "start of sentence" following it is earlier than
2143 ;; HERE, or we hit PAR-BEG. Beware of comment prefixes!
2144 (while (and (re-search-backward (c-sentence-end) par-beg 'limit)
2145 (setq last (point))
2146 (goto-char (match-end 0)) ; tentative beginning of sentence
2147 (or (>= (point) here)
2148 (and (not (bolp)) ; Found a non-blank comment-prefix?
2149 (save-excursion
2150 (if (re-search-backward prefix-at-bol-here nil t)
2151 (/= (match-beginning 1) (match-end 1)))))
2152 (progn ; Skip the crud to find a real b-o-s.
2153 (if (c-in-comment-line-prefix-p)
2154 (beginning-of-line))
2155 (re-search-forward here-filler) ; always succeeds.
2156 (>= (point) here))))
2157 (goto-char last))
2158 (re-search-forward here-filler)))
2160 (if (< (point) start-point)
2162 (goto-char (car range))
2163 t))))
2165 (defun c-end-of-sentence-in-comment (range)
2166 ;; Move forward to the "end of a sentence" within the comment defined by
2167 ;; RANGE, a cons of its starting and ending positions (enclosing the opening
2168 ;; comment delimiter and the terminating */ or newline). If we find an EOS,
2169 ;; return NIL. Otherwise, move point to just after the end of the comment
2170 ;; and return T.
2172 ;; The EOS is just after the non-WS part of the next match of the regexp
2173 ;; sentence-end. Typically, this is just after one of [.!?]. If there is
2174 ;; no sentence-end match following point, any WS before the end of the
2175 ;; comment will count as EOS, providing we're not already in it.
2177 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
2178 ;; It is not a general function, but is intended only for calling from
2179 ;; c-move-over-sentence.
2181 ;; This function might do hidden buffer changes.
2182 (save-match-data
2183 (let ((start-point (point))
2184 ;; (lit-type (c-literal-type range)) ; Commented out, 2005/11/23, ACM
2186 (save-restriction
2187 (c-narrow-to-comment-innards range) ; This might move point forwards.
2188 (let* ((here (point))
2189 (par-end ; EOL position of last text in current/next paragraph.
2190 (save-excursion
2191 ;; The cc-mode values of paragraph-\(start\|separate\), set
2192 ;; in c-setup-paragraph-variables, are used in the
2193 ;; following.
2194 (forward-paragraph 1)
2195 (if (eq (preceding-char) ?\n) (forward-char -1))
2196 (when (<= (point) here) ; can happen, e.g., when HERE is at EOL.
2197 (goto-char here)
2198 (forward-paragraph 2)
2199 (if (eq (preceding-char) ?\n) (forward-char -1)))
2200 (point)))
2202 last
2203 (prefix-at-bol-here
2204 (concat "^[ \t]*\\(" c-current-comment-prefix "\\)\\=")))
2205 ;; Go forward one "comment-prefix which looks like sentence-end"
2206 ;; each time round the following:
2207 (while (and (re-search-forward (c-sentence-end) par-end 'limit)
2208 (progn
2209 (setq last (point))
2210 (skip-chars-backward " \t\n")
2211 (or (and (not (bolp))
2212 (re-search-backward prefix-at-bol-here nil t)
2213 (/= (match-beginning 1) (match-end 1)))
2214 (<= (point) here))))
2215 (goto-char last))
2217 ;; Take special action if we're up against the end of a comment (of
2218 ;; either sort): Leave point just after the last non-ws text.
2219 (if (eq (point) (point-max))
2220 (while (or (/= (skip-chars-backward " \t\n") 0)
2221 (and (re-search-backward prefix-at-bol-here nil t)
2222 (/= (match-beginning 1) (match-end 1))))))))
2224 (if (> (point) start-point)
2226 (goto-char (cdr range))
2227 t))))
2229 (defun c-beginning-of-sentence-in-string (range)
2230 ;; Move backwards to the "beginning of a sentence" within the string defined
2231 ;; by RANGE, a cons of its starting and ending positions (enclosing the
2232 ;; string quotes). If we find a BOS, return NIL. Otherwise, move point to
2233 ;; just before the start of the string and return T.
2235 ;; The BOS is either the text which follows a regexp match of sentence-end
2236 ;; or text which is a beginning of "paragraph". For the purposes of
2237 ;; determining paragraph boundaries, escaped newlines are treated as
2238 ;; ordinary newlines.
2240 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
2241 ;; It is not a general function, but is intended only for calling from
2242 ;; c-move-over-sentence.
2244 ;; This function might do hidden buffer changes.
2245 (save-match-data
2246 (let* ((here (point)) last
2247 (end (1- (cdr range)))
2248 (here-filler ; matches WS and escaped newlines at point.
2249 "\\=\\([ \t\n\r\f]\\|\\\\[\n\r]\\)*")
2250 ;; Enhance paragraph-start and paragraph-separate also to recognize
2251 ;; blank lines terminated by escaped EOLs. IT MAY WELL BE that
2252 ;; these values should be customizable user options, or something.
2253 (paragraph-start c-string-par-start)
2254 (paragraph-separate c-string-par-separate)
2256 (par-beg ; beginning of current (or previous) paragraph.
2257 (save-excursion
2258 (save-restriction
2259 (narrow-to-region (1+ (car range)) end)
2260 (forward-paragraph -1) ; uses above values of
2261 ; paragraph-\(start\|separate\)
2262 (if (> (re-search-forward here-filler nil t) here)
2263 (goto-char here))
2264 (when (>= (point) here)
2265 (forward-paragraph -2)
2266 (if (> (re-search-forward here-filler nil t) here)
2267 (goto-char here)))
2268 (point)))))
2269 ;; Now see if we can find a sentence end after PAR-BEG.
2270 (while (and (re-search-backward c-sentence-end-with-esc-eol par-beg 'limit)
2271 (setq last (point))
2272 (goto-char (match-end 0))
2273 (or (> (point) end)
2274 (progn
2275 (re-search-forward
2276 here-filler end t) ; always succeeds. Use end rather
2277 ; than here, in case point starts
2278 ; beyond the closing quote.
2279 (>= (point) here))))
2280 (goto-char last))
2281 (re-search-forward here-filler here t)
2282 (if (< (point) here)
2284 (goto-char (car range))
2285 t))))
2287 (defun c-end-of-sentence-in-string (range)
2288 ;; Move forward to the "end of a sentence" within the string defined by
2289 ;; RANGE, a cons of its starting and ending positions. If we find an EOS,
2290 ;; return NIL. Otherwise, move point to just after the end of the string
2291 ;; and return T.
2293 ;; The EOS is just after the non-WS part of the next match of the regexp
2294 ;; sentence-end. Typically, this is just after one of [.!?]. If there is
2295 ;; no sentence-end match following point, any WS before the end of the
2296 ;; string will count as EOS, providing we're not already in it.
2298 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
2299 ;; It is not a general function, but is intended only for calling from
2300 ;; c-move-over-sentence.
2302 ;; This function might do hidden buffer changes.
2303 (save-match-data
2304 (let* ((here (point))
2305 last
2306 ;; Enhance paragraph-start and paragraph-separate to recognize
2307 ;; blank lines terminated by escaped EOLs.
2308 (paragraph-start c-string-par-start)
2309 (paragraph-separate c-string-par-separate)
2311 (par-end ; EOL position of last text in current/next paragraph.
2312 (save-excursion
2313 (save-restriction
2314 (narrow-to-region (car range) (1- (cdr range)))
2315 ;; The above values of paragraph-\(start\|separate\) are used
2316 ;; in the following.
2317 (forward-paragraph 1)
2318 (setq last (point))
2319 ;; (re-search-backward filler-here nil t) would find an empty
2320 ;; string. Therefore we simulate it by the following:
2321 (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
2322 (re-search-backward "\\\\\\($\\)\\=" nil t)))
2323 (unless (> (point) here)
2324 (goto-char last)
2325 (forward-paragraph 1)
2326 (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
2327 (re-search-backward "\\\\\\($\\)\\=" nil t))))
2328 (point)))))
2329 ;; Try to go forward a sentence.
2330 (when (re-search-forward c-sentence-end-with-esc-eol par-end 'limit)
2331 (setq last (point))
2332 (while (or (/= (skip-chars-backward " \t\n") 0)
2333 (re-search-backward "\\\\\\($\\)\\=" nil t))))
2334 ;; Did we move a sentence, or did we hit the end of the string?
2335 (if (> (point) here)
2337 (goto-char (cdr range))
2338 t))))
2340 (defun c-ascertain-preceding-literal ()
2341 ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
2342 ;; If a literal is the next thing (aside from whitespace) to be found before
2343 ;; point, return a cons of its start.end positions (enclosing the
2344 ;; delimiters). Otherwise return NIL.
2346 ;; This function might do hidden buffer changes.
2347 (save-excursion
2348 (c-collect-line-comments
2349 (let ((here (point))
2350 pos)
2351 (if (c-backward-single-comment)
2352 (cons (point) (progn (c-forward-single-comment) (point)))
2353 (save-restriction
2354 ;; to prevent `looking-at' seeing a " at point.
2355 (narrow-to-region (point-min) here)
2356 (when
2358 ;; An EOL can act as an "open string" terminator in AWK.
2359 (looking-at c-ws*-string-limit-regexp)
2360 (and (not (bobp))
2361 (progn (backward-char)
2362 (looking-at c-string-limit-regexp))))
2363 (goto-char (match-end 0)) ; just after the string terminator.
2364 (setq pos (point))
2365 (c-safe (c-backward-sexp 1) ; move back over the string.
2366 (cons (point) pos)))))))))
2368 (defun c-ascertain-following-literal ()
2369 ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
2370 ;; If a literal is the next thing (aside from whitespace) following point,
2371 ;; return a cons of its start.end positions (enclosing the delimiters).
2372 ;; Otherwise return NIL.
2374 ;; This function might do hidden buffer changes.
2375 (save-excursion
2376 (c-collect-line-comments
2377 (let (pos)
2378 (c-skip-ws-forward)
2379 (if (looking-at c-string-limit-regexp) ; string-delimiter.
2380 (cons (point) (or (c-safe (progn (c-forward-sexp 1) (point)))
2381 (point-max)))
2382 (setq pos (point))
2383 (if (c-forward-single-comment)
2384 (cons pos (point))))))))
2386 (defun c-after-statement-terminator-p () ; Should we pass in LIM here?
2387 ;; Does point immediately follow a statement "terminator"? A virtual
2388 ;; semicolon is regarded here as such. So is an opening brace ;-)
2390 ;; This function might do hidden buffer changes.
2391 (or (save-excursion
2392 (backward-char)
2393 (and (looking-at "[;{}]")
2394 (not (and c-special-brace-lists ; Pike special brace lists.
2395 (eq (char-after) ?{)
2396 (c-looking-at-special-brace-list)))))
2397 (c-at-vsemi-p)
2398 ;; The following (for macros) is not strict about exactly where we are
2399 ;; wrt white space at the end of the macro. Doesn't seem to matter too
2400 ;; much. ACM 2004/3/29.
2401 (let (eom)
2402 (save-excursion
2403 (if (c-beginning-of-macro)
2404 (setq eom (progn (c-end-of-macro)
2405 (point)))))
2406 (when eom
2407 (save-excursion
2408 (c-forward-comments)
2409 (>= (point) eom))))))
2411 (defun c-back-over-illiterals (macro-start)
2412 ;; Move backwards over code which isn't a literal (i.e. comment or string),
2413 ;; stopping before reaching BOB or a literal or the boundary of a
2414 ;; preprocessor statement or the "beginning of a statement". MACRO-START is
2415 ;; the position of the '#' beginning the current preprocessor directive, or
2416 ;; NIL if we're not in such.
2418 ;; Return a cons (A.B), where
2419 ;; A is NIL if we moved back to a BOS (and know it), T otherwise (we
2420 ;; didn't move, or we hit a literal, or we're not sure about BOS).
2421 ;; B is MACRO-BOUNDARY if we are about to cross the boundary out of or
2422 ;; into a macro, otherwise LITERAL if we've hit a literal, otherwise NIL
2424 ;; The total collection of returned values is as follows:
2425 ;; (nil . nil): Found a BOS whilst remaining inside the illiterals.
2426 ;; (t . literal): No BOS found: only a comment/string. We _might_ be at
2427 ;; a BOS - the caller must check this.
2428 ;; (nil . macro-boundary): only happens with non-nil macro-start. We've
2429 ;; moved and reached the opening # of the macro.
2430 ;; (t . macro-boundary): Every other circumstance in which we're at a
2431 ;; macro-boundary. We might be at a BOS.
2433 ;; Point is left either at the beginning-of-statement, or at the last non-ws
2434 ;; code before encountering the literal/BOB or macro-boundary.
2436 ;; Note that this function moves within either preprocessor commands
2437 ;; (macros) or normal code, but will not cross a boundary between the two,
2438 ;; or between two distinct preprocessor commands.
2440 ;; Stop before `{' and after `;', `{', `}' and `};' when not followed by `}'
2441 ;; or `)', but on the other side of the syntactic ws. Move by sexps and
2442 ;; move into parens. Also stop before `#' when it's at boi on a line.
2444 ;; This function might do hidden buffer changes.
2445 (save-match-data
2446 (let ((here (point))
2447 last) ; marks the position of non-ws code, what'll be BOS if, say, a
2448 ; semicolon precedes it.
2449 (catch 'done
2450 (while t ;; We go back one "token" each iteration of the loop.
2451 (setq last (point))
2452 (cond
2453 ;; Stop at the token after a comment.
2454 ((c-backward-single-comment) ; Also functions as backwards-ws.
2455 (goto-char last)
2456 (throw 'done '(t . literal)))
2458 ;; If we've gone back over a LF, we might have moved into or out of
2459 ;; a preprocessor line.
2460 ((and (save-excursion
2461 (beginning-of-line)
2462 (re-search-forward "\\(^\\|[^\\]\\)[\n\r]" last t))
2463 (if macro-start
2464 (< (point) macro-start)
2465 (c-beginning-of-macro)))
2466 (goto-char last)
2467 ;; Return a car of NIL ONLY if we've hit the opening # of a macro.
2468 (throw 'done (cons (or (eq (point) here)
2469 (not macro-start))
2470 'macro-boundary)))
2472 ;; Have we found a virtual semicolon? If so, stop, unless the next
2473 ;; statement is where we started from.
2474 ((and (c-at-vsemi-p)
2475 (< last here)
2476 (not (memq (char-after last) '(?\) ?})))) ; we've moved back from ) or }
2477 (goto-char last)
2478 (throw 'done '(nil . nil)))
2480 ;; Hit the beginning of the buffer/region?
2481 ((bobp)
2482 (if (/= here last)
2483 (goto-char last))
2484 (throw 'done '(nil . nil)))
2486 ;; Move back a character.
2487 ((progn (backward-char) nil))
2489 ;; Stop at "{" (unless it's a PIKE special brace list.)
2490 ((eq (char-after) ?\{)
2491 (if (and c-special-brace-lists
2492 (c-looking-at-special-brace-list))
2493 (skip-syntax-backward "w_") ; Speedup only.
2494 (if (/= here last)
2495 (goto-char last))
2496 (throw 'done '(nil . nil))))
2498 ;; Have we reached the start of a macro? This always counts as
2499 ;; BOS. (N.B. I don't think (eq (point) here) can ever be true
2500 ;; here. FIXME!!! ACM 2004/3/29)
2501 ((and macro-start (eq (point) macro-start))
2502 (throw 'done (cons (eq (point) here) 'macro-boundary)))
2504 ;; Stop at token just after "}" or ";".
2505 ((looking-at "[;}]")
2506 ;; If we've gone back over ;, {, or }, we're done.
2507 (if (or (= here last)
2508 (memq (char-after last) '(?\) ?}))) ; we've moved back from ) or }
2509 (if (and (eq (char-before) ?}) ; If };, treat them as a unit.
2510 (eq (char-after) ?\;))
2511 (backward-char))
2512 (goto-char last) ; To the statement starting after the ; or }.
2513 (throw 'done '(nil . nil))))
2515 ;; Stop at the token after a string.
2516 ((looking-at c-string-limit-regexp) ; Just gone back over a string terminator?
2517 (goto-char last)
2518 (throw 'done '(t . literal)))
2520 ;; Nothing special: go back word characters.
2521 (t (skip-syntax-backward "w_")) ; Speedup only.
2522 ))))))
2524 (defun c-forward-over-illiterals (macro-end allow-early-stop)
2525 ;; Move forwards over code, stopping before reaching EOB or a literal
2526 ;; (i.e. a comment/string) or the boundary of a preprocessor statement or
2527 ;; the "end of a statement". MACRO-END is the position of the EOL/EOB which
2528 ;; terminates the current preprocessor directive, or NIL if we're not in
2529 ;; such.
2531 ;; ALLOW-EARLY-STOP is non-nil if it is permissible to return without moving
2532 ;; forward at all, should we encounter a `{'. This is an ugly kludge, but
2533 ;; seems unavoidable. Depending on the context this function is called
2534 ;; from, we _sometimes_ need to stop there. Currently (2004/4/3),
2535 ;; ALLOW-EARLY-STOP is applied only to open braces, not to virtual
2536 ;; semicolons, or anything else.
2538 ;; Return a cons (A.B), where
2539 ;; A is NIL if we moved forward to an EOS, or stay at one (when
2540 ;; ALLOW-EARLY-STOP is set), T otherwise (we hit a literal).
2541 ;; B is 'MACRO-BOUNDARY if we are about to cross the boundary out of or
2542 ;; into a macro, otherwise 'LITERAL if we've hit a literal, otherwise NIL
2544 ;; Point is left either after the end-of-statement, or at the last non-ws
2545 ;; code before encountering the literal, or the # of the preprocessor
2546 ;; statement, or at EOB [or just after last non-WS stuff??].
2548 ;; As a clarification of "after the end-of-statement", if a comment or
2549 ;; whitespace follows a completed AWK statement, that statement is treated
2550 ;; as ending just after the last non-ws character before the comment.
2552 ;; Note that this function moves within either preprocessor commands
2553 ;; (macros) or normal code, but not both within the same invocation.
2555 ;; Stop before `{', `}', and `#' when it's at boi on a line, but on the
2556 ;; other side of the syntactic ws, and after `;', `}' and `};'. Only
2557 ;; stop before `{' if at top level or inside braces, though. Move by
2558 ;; sexps and move into parens. Also stop at eol of lines with `#' at
2559 ;; the boi.
2561 ;; This function might do hidden buffer changes.
2562 (let ((here (point))
2563 last)
2564 (catch 'done
2565 (while t ;; We go one "token" forward each time round this loop.
2566 (setq last (point))
2568 ;; If we've moved forward to a virtual semicolon, we're done.
2569 (if (and (> last here) ; Should we check ALLOW-EARLY-STOP, here? 2004/4/3
2570 (c-at-vsemi-p))
2571 (throw 'done '(nil . nil)))
2573 (c-skip-ws-forward)
2574 (cond
2575 ;; Gone past the end of a macro?
2576 ((and macro-end (> (point) macro-end))
2577 (goto-char last)
2578 (throw 'done (cons (eq (point) here) 'macro-boundary)))
2580 ;; About to hit a comment?
2581 ((save-excursion (c-forward-single-comment))
2582 (goto-char last)
2583 (throw 'done '(t . literal)))
2585 ;; End of buffer?
2586 ((eobp)
2587 (if (/= here last)
2588 (goto-char last))
2589 (throw 'done '(nil . nil)))
2591 ;; If we encounter a '{', stop just after the previous token.
2592 ((and (eq (char-after) ?{)
2593 (not (and c-special-brace-lists
2594 (c-looking-at-special-brace-list)))
2595 (or allow-early-stop (/= here last))
2596 (save-excursion ; Is this a check that we're NOT at top level?
2597 ;;;; NO! This seems to check that (i) EITHER we're at the top level; OR (ii) The next enclosing
2598 ;;;; level of bracketing is a '{'. HMM. Doesn't seem to make sense.
2599 ;;;; 2003/8/8 This might have something to do with the GCC extension "Statement Expressions", e.g.
2600 ;;;; while ({stmt1 ; stmt2 ; exp ;}). This form excludes such Statement Expressions.
2601 (or (not (c-safe (up-list -1) t))
2602 (= (char-after) ?{))))
2603 (goto-char last)
2604 (throw 'done '(nil . nil)))
2606 ;; End of a PIKE special brace list? If so, step over it and continue.
2607 ((and c-special-brace-lists
2608 (eq (char-after) ?})
2609 (save-excursion
2610 (and (c-safe (up-list -1) t)
2611 (c-looking-at-special-brace-list))))
2612 (forward-char)
2613 (skip-syntax-forward "w_")) ; Speedup only.
2615 ;; Have we got a '}' after having moved? If so, stop after the
2616 ;; previous token.
2617 ((and (eq (char-after) ?})
2618 (/= here last))
2619 (goto-char last)
2620 (throw 'done '(nil . nil)))
2622 ;; Stop if we encounter a preprocessor line. Continue if we
2623 ;; hit a naked #
2624 ((and c-opt-cpp-prefix
2625 (not macro-end)
2626 (eq (char-after) ?#)
2627 (= (point) (c-point 'boi)))
2628 (if (= (point) here) ; Not a macro, therefore naked #.
2629 (forward-char)
2630 (throw 'done '(t . macro-boundary))))
2632 ;; Stop after a ';', '}', or "};"
2633 ((looking-at ";\\|};?")
2634 (goto-char (match-end 0))
2635 (throw 'done '(nil . nil)))
2637 ;; Found a string (this subsumes AWK regexps)?
2638 ((looking-at c-string-limit-regexp)
2639 (goto-char last)
2640 (throw 'done '(t . literal)))
2643 (forward-char) ; Can't fail - we checked (eobp) earlier on.
2644 (skip-syntax-forward "w_") ; Speedup only.
2645 (when (and macro-end (> (point) macro-end))
2646 (goto-char last)
2647 (throw 'done (cons (eq (point) here) 'macro-boundary))))
2648 )))))
2650 (defun c-one-line-string-p (range)
2651 ;; Is the literal defined by RANGE a string contained in a single line?
2653 ;; This function might do hidden buffer changes.
2654 (save-excursion
2655 (goto-char (car range))
2656 (and (looking-at c-string-limit-regexp)
2657 (progn (skip-chars-forward "^\n" (cdr range))
2658 (eq (point) (cdr range))))))
2660 (defun c-beginning-of-statement (&optional count lim sentence-flag)
2661 "Go to the beginning of the innermost C statement.
2662 With prefix arg, go back N - 1 statements. If already at the
2663 beginning of a statement then go to the beginning of the closest
2664 preceding one, moving into nested blocks if necessary (use
2665 \\[backward-sexp] to skip over a block). If within or next to a
2666 comment or multiline string, move by sentences instead of statements.
2668 When called from a program, this function takes 3 optional args: the
2669 repetition count, a buffer position limit which is the farthest back
2670 to search for the syntactic context, and a flag saying whether to do
2671 sentence motion in or near comments and multiline strings.
2673 Note that for use in programs, `c-beginning-of-statement-1' is
2674 usually better. It has much better defined semantics than this one,
2675 which is intended for interactive use, and might therefore change to
2676 be more \"DWIM:ey\"."
2677 (interactive (list (prefix-numeric-value current-prefix-arg)
2678 nil t))
2679 (if (< count 0)
2680 (c-end-of-statement (- count) lim sentence-flag)
2681 (c-save-buffer-state
2682 ((count (or count 1))
2683 last ; start point for going back ONE chunk. Updated each chunk movement.
2684 (macro-fence
2685 (save-excursion (and (not (bobp)) (c-beginning-of-macro) (point))))
2686 res ; result from sub-function call
2687 not-bos ; "not beginning-of-statement"
2688 (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
2690 ;; Go back one statement at each iteration of the following loop.
2691 (while (and (/= count 0)
2692 (or (not lim) (> (point) lim)))
2693 ;; Go back one "chunk" each time round the following loop, stopping
2694 ;; when we reach a statement boundary, etc.
2695 (setq last (point))
2696 (while
2697 (cond ; Each arm of this cond returns NIL on reaching a desired
2698 ; statement boundary, non-NIL otherwise.
2699 ((bobp)
2700 (setq count 0)
2701 nil)
2703 (range ; point is within or approaching a literal.
2704 (cond
2705 ;; Single line string or sentence-flag is null => skip the
2706 ;; entire literal.
2707 ((or (null sentence-flag)
2708 (c-one-line-string-p range))
2709 (goto-char (car range))
2710 (setq range (c-ascertain-preceding-literal))
2711 ;; N.B. The following is essentially testing for an AWK regexp
2712 ;; at BOS:
2713 ;; Was the previous non-ws thing an end of statement?
2714 (save-excursion
2715 (if macro-fence
2716 (c-backward-comments)
2717 (c-backward-syntactic-ws))
2718 (not (or (bobp) (c-after-statement-terminator-p)))))
2720 ;; Comment inside a statement or a multi-line string.
2721 (t (when (setq res ; returns non-nil when we go out of the literal
2722 (if (eq (c-literal-type range) 'string)
2723 (c-beginning-of-sentence-in-string range)
2724 (c-beginning-of-sentence-in-comment range)))
2725 (setq range (c-ascertain-preceding-literal)))
2726 res)))
2728 ;; Non-literal code.
2729 (t (setq res (c-back-over-illiterals macro-fence))
2730 (setq not-bos ; "not reached beginning-of-statement".
2731 (or (= (point) last)
2732 (memq (char-after) '(?\) ?\}))
2733 (and
2734 (car res)
2735 ;; We're at a tentative BOS. The next form goes
2736 ;; back over WS looking for an end of previous
2737 ;; statement.
2738 (not (save-excursion
2739 (if macro-fence
2740 (c-backward-comments)
2741 (c-backward-syntactic-ws))
2742 (or (bobp) (c-after-statement-terminator-p)))))))
2743 ;; Are we about to move backwards into or out of a
2744 ;; preprocessor command? If so, locate its beginning.
2745 (when (eq (cdr res) 'macro-boundary)
2746 (save-excursion
2747 (beginning-of-line)
2748 (setq macro-fence
2749 (and (not (bobp))
2750 (progn (c-skip-ws-backward) (c-beginning-of-macro))
2751 (point)))))
2752 ;; Are we about to move backwards into a literal?
2753 (when (memq (cdr res) '(macro-boundary literal))
2754 (setq range (c-ascertain-preceding-literal)))
2755 not-bos))
2756 (setq last (point)))
2758 (if (/= count 0) (setq count (1- count))))
2759 (c-keep-region-active))))
2761 (defun c-end-of-statement (&optional count lim sentence-flag)
2762 "Go to the end of the innermost C statement.
2763 With prefix arg, go forward N - 1 statements. Move forward to the end
2764 of the next statement if already at end, and move into nested blocks
2765 \(use \\[forward-sexp] to skip over a block). If within or next to a
2766 comment or multiline string, move by sentences instead of statements.
2768 When called from a program, this function takes 3 optional args: the
2769 repetition count, a buffer position limit which is the farthest back
2770 to search for the syntactic context, and a flag saying whether to do
2771 sentence motion in or near comments and multiline strings."
2772 (interactive (list (prefix-numeric-value current-prefix-arg)
2773 nil t))
2774 (setq count (or count 1))
2775 (if (< count 0) (c-beginning-of-statement (- count) lim sentence-flag)
2777 (c-save-buffer-state
2778 (here ; start point for going forward ONE statement. Updated each statement.
2779 (macro-fence
2780 (save-excursion
2781 (and (not (eobp)) (c-beginning-of-macro)
2782 (progn (c-end-of-macro) (point)))))
2784 (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
2786 ;; Go back/forward one statement at each iteration of the following loop.
2787 (while (and (/= count 0)
2788 (or (not lim) (< (point) lim)))
2789 (setq here (point)) ; ONLY HERE is HERE updated
2791 ;; Go forward one "chunk" each time round the following loop, stopping
2792 ;; when we reach a statement boundary, etc.
2793 (while
2794 (cond ; Each arm of this cond returns NIL on reaching a desired
2795 ; statement boundary, non-NIL otherwise.
2796 ((eobp)
2797 (setq count 0)
2798 nil)
2800 (range ; point is within a literal.
2801 (cond
2802 ;; sentence-flag is null => skip the entire literal.
2803 ;; or a Single line string.
2804 ((or (null sentence-flag)
2805 (c-one-line-string-p range))
2806 (goto-char (cdr range))
2807 (setq range (c-ascertain-following-literal))
2808 ;; Is there a virtual semicolon here (e.g. for AWK)?
2809 (not (c-at-vsemi-p)))
2811 ;; Comment or multi-line string.
2812 (t (when (setq res ; gets non-nil when we go out of the literal
2813 (if (eq (c-literal-type range) 'string)
2814 (c-end-of-sentence-in-string range)
2815 (c-end-of-sentence-in-comment range)))
2816 (setq range (c-ascertain-following-literal)))
2817 ;; If we've just come forward out of a literal, check for
2818 ;; vsemi. (N.B. AWK can't have a vsemi after a comment, but
2819 ;; some other language may do in the future)
2820 (and res
2821 (not (c-at-vsemi-p))))))
2823 ;; Non-literal code.
2824 (t (setq res (c-forward-over-illiterals macro-fence
2825 (> (point) here)))
2826 ;; Are we about to move forward into or out of a
2827 ;; preprocessor command?
2828 (when (eq (cdr res) 'macro-boundary)
2829 (setq macro-fence
2830 (save-excursion
2831 (if macro-fence
2832 (progn
2833 (end-of-line)
2834 (and (not (eobp))
2835 (progn (c-skip-ws-forward)
2836 (c-beginning-of-macro))
2837 (progn (c-end-of-macro)
2838 (point))))
2839 (and (not (eobp))
2840 (c-beginning-of-macro)
2841 (progn (c-end-of-macro) (point)))))))
2842 ;; Are we about to move forward into a literal?
2843 (when (memq (cdr res) '(macro-boundary literal))
2844 (setq range (c-ascertain-following-literal)))
2845 (car res))))
2847 (if (/= count 0) (setq count (1- count))))
2848 (c-keep-region-active))))
2851 ;; set up electric character functions to work with pending-del,
2852 ;; (a.k.a. delsel) mode. All symbols get the t value except
2853 ;; the functions which delete, which gets 'supersede.
2854 (mapc
2855 (function
2856 (lambda (sym)
2857 (put sym 'delete-selection t) ; for delsel (Emacs)
2858 (put sym 'pending-delete t))) ; for pending-del (XEmacs)
2859 '(c-electric-pound
2860 c-electric-brace
2861 c-electric-slash
2862 c-electric-star
2863 c-electric-semi&comma
2864 c-electric-lt-gt
2865 c-electric-colon
2866 c-electric-paren))
2867 (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
2868 (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
2869 (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
2870 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
2871 (put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel
2872 (put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del
2875 ;; Inserting/indenting comments
2876 (defun c-calc-comment-indent (entry)
2877 ;; This function might do hidden buffer changes.
2878 (if (symbolp entry)
2879 (setq entry (or (assq entry c-indent-comment-alist)
2880 (assq 'other c-indent-comment-alist)
2881 '(default . (column . nil)))))
2882 (let ((action (car (cdr entry)))
2883 (value (cdr (cdr entry)))
2884 (col (current-column)))
2885 (cond ((eq action 'space)
2886 (+ col value))
2887 ((eq action 'column)
2888 (unless value (setq value comment-column))
2889 (if (bolp)
2890 ;; Do not pad with one space if we're at bol.
2891 value
2892 (max (1+ col) value)))
2893 ((eq action 'align)
2894 (or (save-excursion
2895 (beginning-of-line)
2896 (unless (bobp)
2897 (backward-char)
2898 (let ((lim (c-literal-limits (c-point 'bol) t)))
2899 (when (consp lim)
2900 (goto-char (car lim))
2901 (when (looking-at "/[/*]") ; FIXME!!! Adapt for AWK! (ACM, 2005/11/18)
2902 ;; Found comment to align with.
2903 (if (bolp)
2904 ;; Do not pad with one space if we're at bol.
2906 (max (1+ col) (current-column))))))))
2907 ;; Recurse to handle value as a new spec.
2908 (c-calc-comment-indent (cdr entry)))))))
2910 (defun c-comment-indent ()
2911 "Used by `indent-for-comment' to create and indent comments.
2912 See `c-indent-comment-alist' for a description."
2913 (save-excursion
2914 (end-of-line)
2915 (c-save-buffer-state
2916 ((eot (let ((lim (c-literal-limits (c-point 'bol) t)))
2917 (or (when (consp lim)
2918 (goto-char (car lim))
2919 (when (looking-at "/[/*]")
2920 (skip-chars-backward " \t")
2921 (point)))
2922 (progn
2923 (skip-chars-backward " \t")
2924 (point)))))
2925 (line-type
2926 (cond ((looking-at "^/[/*]")
2927 'anchored-comment)
2928 ((progn (beginning-of-line)
2929 (eq (point) eot))
2930 'empty-line)
2931 ((progn (back-to-indentation)
2932 (and (eq (char-after) ?})
2933 (eq (point) (1- eot))))
2934 'end-block)
2935 ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
2936 (eq (match-end 0) eot))
2937 'cpp-end-block)
2939 'other)))
2940 case-fold-search)
2941 (if (and (memq line-type '(anchored-comment empty-line))
2942 c-indent-comments-syntactically-p)
2943 (let ((c-syntactic-context (c-guess-basic-syntax)))
2944 ;; BOGOSITY ALERT: if we're looking at the eol, its
2945 ;; because indent-for-comment hasn't put the comment-start
2946 ;; in the buffer yet. this will screw up the syntactic
2947 ;; analysis so we kludge in the necessary info. Another
2948 ;; kludge is that if we're at the bol, then we really want
2949 ;; to ignore any anchoring as specified by
2950 ;; c-comment-only-line-offset since it doesn't apply here.
2951 (if (eolp)
2952 (c-add-syntax 'comment-intro))
2953 (let ((c-comment-only-line-offset
2954 (if (consp c-comment-only-line-offset)
2955 c-comment-only-line-offset
2956 (cons c-comment-only-line-offset
2957 c-comment-only-line-offset))))
2958 (c-get-syntactic-indentation c-syntactic-context)))
2959 (goto-char eot)
2960 (c-calc-comment-indent line-type)))))
2963 ;; used by outline-minor-mode
2964 (defun c-outline-level ()
2965 (let (buffer-invisibility-spec);; This so that `current-column' DTRT
2966 ;; in otherwise-hidden text.
2967 (save-excursion
2968 (skip-chars-forward "\t ")
2969 (current-column))))
2972 ;; Movement by CPP conditionals.
2973 (defun c-up-conditional (count)
2974 "Move back to the containing preprocessor conditional, leaving mark behind.
2975 A prefix argument acts as a repeat count. With a negative argument,
2976 move forward to the end of the containing preprocessor conditional.
2978 \"#elif\" is treated like \"#else\" followed by \"#if\", so the
2979 function stops at them when going backward, but not when going
2980 forward."
2981 (interactive "p")
2982 (let ((new-point (c-scan-conditionals (- count) -1)))
2983 (push-mark)
2984 (goto-char new-point))
2985 (c-keep-region-active))
2987 (defun c-up-conditional-with-else (count)
2988 "Move back to the containing preprocessor conditional, including \"#else\".
2989 Just like `c-up-conditional', except it also stops at \"#else\"
2990 directives."
2991 (interactive "p")
2992 (let ((new-point (c-scan-conditionals (- count) -1 t)))
2993 (push-mark)
2994 (goto-char new-point))
2995 (c-keep-region-active))
2997 (defun c-down-conditional (count)
2998 "Move forward into the next preprocessor conditional, leaving mark behind.
2999 A prefix argument acts as a repeat count. With a negative argument,
3000 move backward into the previous preprocessor conditional.
3002 \"#elif\" is treated like \"#else\" followed by \"#if\", so the
3003 function stops at them when going forward, but not when going
3004 backward."
3005 (interactive "p")
3006 (let ((new-point (c-scan-conditionals count 1)))
3007 (push-mark)
3008 (goto-char new-point))
3009 (c-keep-region-active))
3011 (defun c-down-conditional-with-else (count)
3012 "Move forward into the next preprocessor conditional, including \"#else\".
3013 Just like `c-down-conditional', except it also stops at \"#else\"
3014 directives."
3015 (interactive "p")
3016 (let ((new-point (c-scan-conditionals count 1 t)))
3017 (push-mark)
3018 (goto-char new-point))
3019 (c-keep-region-active))
3021 (defun c-backward-conditional (count &optional target-depth with-else)
3022 "Move back across a preprocessor conditional, leaving mark behind.
3023 A prefix argument acts as a repeat count. With a negative argument,
3024 move forward across a preprocessor conditional.
3026 The optional arguments TARGET-DEPTH and WITH-ELSE are historical,
3027 and have the same meanings as in `c-scan-conditionals'. If you
3028 are calling c-forward-conditional from a program, you might want
3029 to call `c-scan-conditionals' directly instead."
3030 (interactive "p")
3031 (let ((new-point (c-scan-conditionals (- count) target-depth with-else)))
3032 (push-mark)
3033 (goto-char new-point))
3034 (c-keep-region-active))
3036 (defun c-forward-conditional (count &optional target-depth with-else)
3037 "Move forward across a preprocessor conditional, leaving mark behind.
3038 A prefix argument acts as a repeat count. With a negative argument,
3039 move backward across a preprocessor conditional.
3041 If there aren't enough conditionals after \(or before) point, an
3042 error is signaled.
3044 \"#elif\" is treated like \"#else\" followed by \"#if\", except that
3045 the nesting level isn't changed when tracking subconditionals.
3047 The optional arguments TARGET-DEPTH and WITH-ELSE are historical,
3048 and have the same meanings as in `c-scan-conditionals'. If you
3049 are calling c-forward-conditional from a program, you might want
3050 to call `c-scan-conditionals' directly instead."
3051 (interactive "p")
3052 (let ((new-point (c-scan-conditionals count target-depth with-else)))
3053 (push-mark)
3054 (goto-char new-point)))
3056 (defun c-scan-conditionals (count &optional target-depth with-else)
3057 "Scan forward across COUNT preprocessor conditionals.
3058 With a negative argument, scan backward across preprocessor
3059 conditionals. Return the end position. Point is not moved.
3061 If there aren't enough preprocessor conditionals, throw an error.
3063 \"#elif\" is treated like \"#else\" followed by \"#if\", except that
3064 the nesting level isn't changed when tracking subconditionals.
3066 The optional argument TARGET-DEPTH specifies the wanted nesting depth
3067 after each scan. E.g. if TARGET-DEPTH is -1, the end position will be
3068 outside the enclosing conditional. A non-integer non-nil TARGET-DEPTH
3069 counts as -1.
3071 If the optional argument WITH-ELSE is non-nil, \"#else\" directives
3072 are treated as conditional clause limits. Normally they are ignored."
3073 (let* ((forward (> count 0))
3074 (increment (if forward -1 1))
3075 (search-function (if forward 're-search-forward 're-search-backward))
3076 new case-fold-search)
3077 (unless (integerp target-depth)
3078 (setq target-depth (if target-depth -1 0)))
3079 (save-excursion
3080 (while (/= count 0)
3081 (let ((depth 0)
3082 ;; subdepth is the depth in "uninteresting" subtrees,
3083 ;; i.e. those that takes us farther from the target
3084 ;; depth instead of closer.
3085 (subdepth 0)
3086 found)
3087 (save-excursion
3088 ;; Find the "next" significant line in the proper direction.
3089 (while (and (not found)
3090 ;; Rather than searching for a # sign that
3091 ;; comes at the beginning of a line aside from
3092 ;; whitespace, search first for a string
3093 ;; starting with # sign. Then verify what
3094 ;; precedes it. This is faster on account of
3095 ;; the fastmap feature of the regexp matcher.
3096 (funcall search-function
3097 "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
3098 nil t))
3099 (beginning-of-line)
3100 ;; Now verify it is really a preproc line.
3101 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
3102 (let (dchange (directive (match-string 1)))
3103 (cond ((string= directive "if")
3104 (setq dchange (- increment)))
3105 ((string= directive "endif")
3106 (setq dchange increment))
3107 ((= subdepth 0)
3108 ;; When we're not in an "uninteresting"
3109 ;; subtree, we might want to act on "elif"
3110 ;; and "else" too.
3111 (if (cond (with-else
3112 ;; Always move toward the target depth.
3113 (setq dchange
3114 (if (> target-depth 0) 1 -1)))
3115 ((string= directive "elif")
3116 (setq dchange (- increment))))
3117 ;; Ignore the change if it'd take us
3118 ;; into an "uninteresting" subtree.
3119 (if (eq (> dchange 0) (<= target-depth 0))
3120 (setq dchange nil)))))
3121 (when dchange
3122 (when (or (/= subdepth 0)
3123 (eq (> dchange 0) (<= target-depth 0)))
3124 (setq subdepth (+ subdepth dchange)))
3125 (setq depth (+ depth dchange))
3126 ;; If we are trying to move across, and we find an
3127 ;; end before we find a beginning, get an error.
3128 (if (and (< depth target-depth) (< dchange 0))
3129 (error (if forward
3130 "No following conditional at this level"
3131 "No previous conditional at this level"))))
3132 ;; When searching forward, start from next line so
3133 ;; that we don't find the same line again.
3134 (if forward (forward-line 1))
3135 ;; We found something if we've arrived at the
3136 ;; target depth.
3137 (if (and dchange (= depth target-depth))
3138 (setq found (point))))
3139 ;; else
3140 (if forward (forward-line 1)))))
3141 (or found
3142 (error "No containing preprocessor conditional"))
3143 (goto-char (setq new found)))
3144 (setq count (+ count increment))))
3145 (c-keep-region-active)
3146 new))
3149 ;; commands to indent lines, regions, defuns, and expressions
3150 (defun c-indent-command (&optional arg)
3151 "Indent current line as C code, and/or insert some whitespace.
3153 If `c-tab-always-indent' is t, always just indent the current line.
3154 If nil, indent the current line only if point is at the left margin or
3155 in the line's indentation; otherwise insert some whitespace[*]. If
3156 other than nil or t, then some whitespace[*] is inserted only within
3157 literals (comments and strings), but the line is always reindented.
3159 If `c-syntactic-indentation' is t, indentation is done according to
3160 the syntactic context. A numeric argument, regardless of its value,
3161 means indent rigidly all the lines of the expression starting after
3162 point so that this line becomes properly indented. The relative
3163 indentation among the lines of the expression is preserved.
3165 If `c-syntactic-indentation' is nil, the line is just indented one
3166 step according to `c-basic-offset'. In this mode, a numeric argument
3167 indents a number of such steps, positive or negative, and an empty
3168 prefix argument is equivalent to -1.
3170 [*] The amount and kind of whitespace inserted is controlled by the
3171 variable `c-insert-tab-function', which is called to do the actual
3172 insertion of whitespace. Normally the function in this variable
3173 just inserts a tab character, or the equivalent number of spaces,
3174 depending on the variable `indent-tabs-mode'."
3176 (interactive "P")
3177 (let ((indent-function
3178 (if c-syntactic-indentation
3179 (symbol-function 'indent-according-to-mode)
3180 (lambda ()
3181 (let ((c-macro-start c-macro-start)
3182 (steps (if (equal arg '(4))
3184 (prefix-numeric-value arg))))
3185 (c-shift-line-indentation (* steps c-basic-offset))
3186 (when (and c-auto-align-backslashes
3187 (save-excursion
3188 (end-of-line)
3189 (eq (char-before) ?\\))
3190 (c-query-and-set-macro-start))
3191 ;; Realign the line continuation backslash if inside a macro.
3192 (c-backslash-region (point) (point) nil t)))
3193 ))))
3194 (if (and c-syntactic-indentation arg)
3195 ;; If c-syntactic-indentation and got arg, always indent this
3196 ;; line as C and shift remaining lines of expression the same
3197 ;; amount.
3198 (let ((shift-amt (save-excursion
3199 (back-to-indentation)
3200 (current-column)))
3201 beg end)
3202 (c-indent-line)
3203 (setq shift-amt (- (save-excursion
3204 (back-to-indentation)
3205 (current-column))
3206 shift-amt))
3207 (save-excursion
3208 (if (eq c-tab-always-indent t)
3209 (beginning-of-line)) ; FIXME!!! What is this here for? ACM 2005/10/31
3210 (setq beg (point))
3211 (c-forward-sexp 1)
3212 (setq end (point))
3213 (goto-char beg)
3214 (forward-line 1)
3215 (setq beg (point)))
3216 (if (> end beg)
3217 (indent-code-rigidly beg end shift-amt "#")))
3218 ;; Else use c-tab-always-indent to determine behavior.
3219 (cond
3220 ;; CASE 1: indent when at column zero or in line's indentation,
3221 ;; otherwise insert a tab
3222 ((not c-tab-always-indent)
3223 (if (save-excursion
3224 (skip-chars-backward " \t")
3225 (not (bolp)))
3226 (funcall c-insert-tab-function)
3227 (funcall indent-function)))
3228 ;; CASE 2: just indent the line
3229 ((eq c-tab-always-indent t)
3230 (funcall indent-function))
3231 ;; CASE 3: if in a literal, insert a tab, but always indent the
3232 ;; line
3234 (if (c-save-buffer-state () (c-in-literal))
3235 (funcall c-insert-tab-function))
3236 (funcall indent-function)
3237 )))))
3239 (defun c-indent-exp (&optional shutup-p)
3240 "Indent each line in the balanced expression following point syntactically.
3241 If optional SHUTUP-P is non-nil, no errors are signaled if no
3242 balanced expression is found."
3243 (interactive "*P")
3244 (let ((here (point-marker))
3245 end)
3246 (set-marker-insertion-type here t)
3247 (unwind-protect
3248 (let ((start (save-restriction
3249 ;; Find the closest following open paren that
3250 ;; ends on another line.
3251 (narrow-to-region (point-min) (c-point 'eol))
3252 (let (beg (end (point)))
3253 (while (and (setq beg (c-down-list-forward end))
3254 (setq end (c-up-list-forward beg))))
3255 (and beg
3256 (eq (char-syntax (char-before beg)) ?\()
3257 (1- beg))))))
3258 ;; sanity check
3259 (if (not start)
3260 (unless shutup-p
3261 (error "Cannot find start of balanced expression to indent"))
3262 (goto-char start)
3263 (setq end (c-safe (scan-sexps (point) 1)))
3264 (if (not end)
3265 (unless shutup-p
3266 (error "Cannot find end of balanced expression to indent"))
3267 (forward-line)
3268 (if (< (point) end)
3269 (c-indent-region (point) end)))))
3270 (goto-char here)
3271 (set-marker here nil))))
3273 (defun c-indent-defun ()
3274 "Indent the current top-level declaration or macro syntactically.
3275 In the macro case this also has the effect of realigning any line
3276 continuation backslashes, unless `c-auto-align-backslashes' is nil."
3277 (interactive "*")
3278 (let ((here (point-marker)) decl-limits case-fold-search)
3279 (unwind-protect
3280 (progn
3281 (c-save-buffer-state nil
3282 ;; We try to be line oriented, unless there are several
3283 ;; declarations on the same line.
3284 (if (looking-at c-syntactic-eol)
3285 (c-backward-token-2 1 nil (c-point 'bol))
3286 (c-forward-token-2 0 nil (c-point 'eol)))
3287 (setq decl-limits (c-declaration-limits nil)))
3288 (if decl-limits
3289 (c-indent-region (car decl-limits)
3290 (cdr decl-limits))))
3291 (goto-char here)
3292 (set-marker here nil))))
3294 (defun c-indent-region (start end &optional quiet)
3295 "Indent syntactically every line whose first char is between START
3296 and END inclusive. If the optional argument QUIET is non-nil then no
3297 syntactic errors are reported, even if `c-report-syntactic-errors' is
3298 non-nil."
3299 (save-excursion
3300 (goto-char end)
3301 (skip-chars-backward " \t\n\r\f\v")
3302 (setq end (point))
3303 (goto-char start)
3304 ;; Advance to first nonblank line.
3305 (beginning-of-line)
3306 (skip-chars-forward " \t\n\r\f\v")
3307 (setq start (point))
3308 (beginning-of-line)
3309 (setq c-parsing-error
3310 (or (let ((endmark (copy-marker end))
3311 (c-parsing-error nil)
3312 ;; shut up any echo msgs on indiv lines
3313 (c-echo-syntactic-information-p nil)
3314 (ml-macro-start ; Start pos of multi-line macro.
3315 (and (c-save-buffer-state ()
3316 (save-excursion (c-beginning-of-macro)))
3317 (eq (char-before (c-point 'eol)) ?\\)
3318 start))
3319 (c-fix-backslashes nil)
3320 syntax)
3321 (unwind-protect
3322 (progn
3323 (c-progress-init start end 'c-indent-region)
3325 (while (and (bolp) ;; One line each time round the loop.
3326 (not (eobp))
3327 (< (point) endmark))
3328 ;; update progress
3329 (c-progress-update)
3330 ;; skip empty lines
3331 (unless (or (looking-at "\\s *$")
3332 (and ml-macro-start (looking-at "\\s *\\\\$")))
3333 ;; Get syntax and indent.
3334 (c-save-buffer-state nil
3335 (setq syntax (c-guess-basic-syntax)))
3336 (c-indent-line syntax t t))
3338 (if ml-macro-start
3339 ;; End of current multi-line macro?
3340 (when (and c-auto-align-backslashes
3341 (not (eq (char-before (c-point 'eol)) ?\\)))
3342 ;; Fixup macro backslashes.
3343 (c-backslash-region ml-macro-start (c-point 'bonl) nil)
3344 (setq ml-macro-start nil))
3345 ;; New multi-line macro?
3346 (if (and (assq 'cpp-macro syntax)
3347 (eq (char-before (c-point 'eol)) ?\\))
3348 (setq ml-macro-start (point))))
3350 (forward-line))
3352 (if (and ml-macro-start c-auto-align-backslashes)
3353 (c-backslash-region ml-macro-start (c-point 'bopl) nil t)))
3354 (set-marker endmark nil)
3355 (c-progress-fini 'c-indent-region))
3356 (c-echo-parsing-error quiet))
3357 c-parsing-error))))
3359 (defun c-fn-region-is-active-p ()
3360 ;; Function version of the macro for use in places that aren't
3361 ;; compiled, e.g. in the menus.
3362 (c-region-is-active-p))
3364 (defun c-indent-line-or-region (&optional arg region)
3365 "Indent active region, current line, or block starting on this line.
3366 In Transient Mark mode, when the region is active, reindent the region.
3367 Otherwise, with a prefix argument, rigidly reindent the expression
3368 starting on the current line.
3369 Otherwise reindent just the current line."
3370 (interactive
3371 (list current-prefix-arg (c-region-is-active-p)))
3372 (if region
3373 (c-indent-region (region-beginning) (region-end))
3374 (c-indent-command arg)))
3376 ;; for progress reporting
3377 (defvar c-progress-info nil)
3379 (defun c-progress-init (start end context)
3380 (cond
3381 ;; Be silent
3382 ((not c-progress-interval))
3383 ;; Start the progress update messages. If this Emacs doesn't have
3384 ;; a built-in timer, just be dumb about it.
3385 ((not (fboundp 'current-time))
3386 (message "Indenting region... (this may take a while)"))
3387 ;; If progress has already been initialized, do nothing. otherwise
3388 ;; initialize the counter with a vector of:
3389 ;; [start end lastsec context]
3390 (c-progress-info)
3391 (t (setq c-progress-info (vector start
3392 (save-excursion
3393 (goto-char end)
3394 (point-marker))
3395 (nth 1 (current-time))
3396 context))
3397 (message "Indenting region..."))
3400 (defun c-progress-update ()
3401 (if (not (and c-progress-info c-progress-interval))
3403 (let ((now (nth 1 (current-time)))
3404 (start (aref c-progress-info 0))
3405 (end (aref c-progress-info 1))
3406 (lastsecs (aref c-progress-info 2)))
3407 ;; should we update? currently, update happens every 2 seconds,
3408 ;; what's the right value?
3409 (if (< c-progress-interval (- now lastsecs))
3410 (progn
3411 (message "Indenting region... (%d%% complete)"
3412 (/ (* 100 (- (point) start)) (- end start)))
3413 (aset c-progress-info 2 now)))
3416 (defun c-progress-fini (context)
3417 (if (not c-progress-interval)
3419 (if (or (eq context (aref c-progress-info 3))
3420 (eq context t))
3421 (progn
3422 (set-marker (aref c-progress-info 1) nil)
3423 (setq c-progress-info nil)
3424 (message "Indenting region... done")))))
3428 ;;; This page handles insertion and removal of backslashes for C macros.
3430 (defun c-backslash-region (from to delete-flag &optional line-mode)
3431 "Insert, align, or delete end-of-line backslashes on the lines in the region.
3432 With no argument, inserts backslashes and aligns existing backslashes.
3433 With an argument, deletes the backslashes. The backslash alignment is
3434 done according to the settings in `c-backslash-column',
3435 `c-backslash-max-column' and `c-auto-align-backslashes'.
3437 This function does not modify blank lines at the start of the region.
3438 If the region ends at the start of a line and the macro doesn't
3439 continue below it, the backslash (if any) at the end of the previous
3440 line is deleted.
3442 You can put the region around an entire macro definition and use this
3443 command to conveniently insert and align the necessary backslashes."
3444 (interactive "*r\nP")
3445 (let ((endmark (make-marker))
3446 ;; Keep the backslash trimming functions from changing the
3447 ;; whitespace around point, since in this case it's only the
3448 ;; position of point that tells the indentation of the line.
3449 (point-pos (if (save-excursion
3450 (skip-chars-backward " \t")
3451 (and (bolp) (looking-at "[ \t]*\\\\?$")))
3452 (point-marker)
3453 (point-min)))
3454 column longest-line-col bs-col-after-end)
3455 (save-excursion
3456 (goto-char to)
3457 (if (and (not line-mode) (bobp))
3458 ;; Nothing to do if to is at bob, since we should back up
3459 ;; and there's no line to back up to.
3461 (when (and (not line-mode) (bolp))
3462 ;; Do not back up the to line if line-mode is set, to make
3463 ;; e.g. c-newline-and-indent consistent regardless whether
3464 ;; the (newline) call leaves point at bol or not.
3465 (backward-char)
3466 (setq to (point)))
3467 (if delete-flag
3468 (progn
3469 (set-marker endmark (point))
3470 (goto-char from)
3471 (c-delete-backslashes-forward endmark point-pos))
3472 ;; Set bs-col-after-end to the column of any backslash
3473 ;; following the region, or nil if there is none.
3474 (setq bs-col-after-end
3475 (and (progn (end-of-line)
3476 (eq (char-before) ?\\))
3477 (= (forward-line 1) 0)
3478 (progn (end-of-line)
3479 (eq (char-before) ?\\))
3480 (1- (current-column))))
3481 (when line-mode
3482 ;; Back up the to line if line-mode is set, since the line
3483 ;; after the newly inserted line break should not be
3484 ;; touched in c-newline-and-indent.
3485 (setq to (max from (or (c-safe (c-point 'eopl)) from)))
3486 (unless bs-col-after-end
3487 ;; Set bs-col-after-end to non-nil in any case, since we
3488 ;; do not want to delete the backslash at the last line.
3489 (setq bs-col-after-end t)))
3490 (if (and line-mode
3491 (not c-auto-align-backslashes))
3492 (goto-char from)
3493 ;; Compute the smallest column number past the ends of all
3494 ;; the lines.
3495 (setq longest-line-col 0)
3496 (goto-char to)
3497 (if bs-col-after-end
3498 ;; Include one more line in the max column
3499 ;; calculation, since the to line will be backslashed
3500 ;; too.
3501 (forward-line 1))
3502 (end-of-line)
3503 (while (and (>= (point) from)
3504 (progn
3505 (if (eq (char-before) ?\\)
3506 (forward-char -1))
3507 (skip-chars-backward " \t")
3508 (setq longest-line-col (max longest-line-col
3509 (1+ (current-column))))
3510 (beginning-of-line)
3511 (not (bobp))))
3512 (backward-char))
3513 ;; Try to align with surrounding backslashes.
3514 (goto-char from)
3515 (beginning-of-line)
3516 (if (and (not (bobp))
3517 (progn (backward-char)
3518 (eq (char-before) ?\\)))
3519 (progn
3520 (setq column (1- (current-column)))
3521 (if (numberp bs-col-after-end)
3522 ;; Both a preceding and a following backslash.
3523 ;; Choose the greatest of them.
3524 (setq column (max column bs-col-after-end)))
3525 (goto-char from))
3526 ;; No preceding backslash. Try to align with one
3527 ;; following the region. Disregard the backslash at the
3528 ;; to line since it's likely to be bogus (e.g. when
3529 ;; called from c-newline-and-indent).
3530 (if (numberp bs-col-after-end)
3531 (setq column bs-col-after-end))
3532 ;; Don't modify blank lines at start of region.
3533 (goto-char from)
3534 (while (and (< (point) to) (bolp) (eolp))
3535 (forward-line 1)))
3536 (if (and column (< column longest-line-col))
3537 ;; Don't try to align with surrounding backslashes if
3538 ;; any line is too long.
3539 (setq column nil))
3540 (unless column
3541 ;; Impose minimum limit and tab width alignment only if
3542 ;; we can't align with surrounding backslashes.
3543 (if (> (% longest-line-col tab-width) 0)
3544 (setq longest-line-col
3545 (* (/ (+ longest-line-col tab-width -1)
3546 tab-width)
3547 tab-width)))
3548 (setq column (max c-backslash-column
3549 longest-line-col)))
3550 ;; Always impose maximum limit.
3551 (setq column (min column c-backslash-max-column)))
3552 (if bs-col-after-end
3553 ;; Add backslashes on all lines if the macro continues
3554 ;; after the to line.
3555 (progn
3556 (set-marker endmark to)
3557 (c-append-backslashes-forward endmark column point-pos))
3558 ;; Add backslashes on all lines except the last, and
3559 ;; remove any on the last line.
3560 (if (save-excursion
3561 (goto-char to)
3562 (beginning-of-line)
3563 (if (not (bobp))
3564 (set-marker endmark (1- (point)))))
3565 (progn
3566 (c-append-backslashes-forward endmark column point-pos)
3567 ;; The function above leaves point on the line
3568 ;; following endmark.
3569 (set-marker endmark (point)))
3570 (set-marker endmark to))
3571 (c-delete-backslashes-forward endmark point-pos)))))
3572 (set-marker endmark nil)
3573 (if (markerp point-pos)
3574 (set-marker point-pos nil))))
3576 (defun c-append-backslashes-forward (to-mark column point-pos)
3577 (let ((state (parse-partial-sexp (c-point 'bol) (point))))
3578 (if column
3579 (while
3580 (and
3581 (<= (point) to-mark)
3583 (let ((start (point)) (inserted nil) end col)
3584 (end-of-line)
3585 (unless (eq (char-before) ?\\)
3586 (insert ?\\)
3587 (setq inserted t))
3588 (setq state (parse-partial-sexp
3589 start (point) nil nil state))
3590 (backward-char)
3591 (setq col (current-column))
3593 ;; Avoid unnecessary changes of the buffer.
3594 (cond ((and (not inserted) (nth 3 state))
3595 ;; Don't realign backslashes in string literals
3596 ;; since that would change them.
3599 ((< col column)
3600 (delete-region
3601 (point)
3602 (progn
3603 (skip-chars-backward
3604 " \t" (if (>= (point) point-pos) point-pos))
3605 (point)))
3606 (indent-to column))
3608 ((and (= col column)
3609 (memq (char-before) '(?\ ?\t))))
3611 ((progn
3612 (setq end (point))
3613 (or (/= (skip-chars-backward
3614 " \t" (if (>= (point) point-pos) point-pos))
3616 (/= (char-after) ?\ )))
3617 (delete-region (point) end)
3618 (indent-to column 1)))
3620 (zerop (forward-line 1)))
3621 (bolp))) ; forward-line has funny behavior at eob.
3623 ;; Make sure there are backslashes with at least one space in
3624 ;; front of them.
3625 (while
3626 (and
3627 (<= (point) to-mark)
3629 (let ((start (point)))
3630 (end-of-line)
3631 (setq state (parse-partial-sexp
3632 start (point) nil nil state))
3634 (if (eq (char-before) ?\\)
3635 (unless (nth 3 state)
3636 (backward-char)
3637 (unless (and (memq (char-before) '(?\ ?\t))
3638 (/= (point) point-pos))
3639 (insert ?\ )))
3641 (if (and (memq (char-before) '(?\ ?\t))
3642 (/= (point) point-pos))
3643 (insert ?\\)
3644 (insert ?\ ?\\)))
3646 (zerop (forward-line 1)))
3647 (bolp)))))) ; forward-line has funny behavior at eob.
3649 (defun c-delete-backslashes-forward (to-mark point-pos)
3650 (while
3651 (and (<= (point) to-mark)
3652 (progn
3653 (end-of-line)
3654 (if (eq (char-before) ?\\)
3655 (delete-region
3656 (point)
3657 (progn (backward-char)
3658 (skip-chars-backward " \t" (if (>= (point) point-pos)
3659 point-pos))
3660 (point))))
3661 (zerop (forward-line 1)))
3662 (bolp)))) ; forward-line has funny behavior at eob.
3666 ;;; Line breaking and paragraph filling.
3668 (defvar c-auto-fill-prefix t)
3669 (defvar c-lit-limits nil)
3670 (defvar c-lit-type nil)
3672 ;; The filling code is based on a simple theory; leave the intricacies
3673 ;; of the text handling to the currently active mode for that
3674 ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
3675 ;; possible to make them work correctly wrt the comment and string
3676 ;; separators, one-line paragraphs etc. Unfortunately, when it comes
3677 ;; to it, there's quite a lot of special cases to handle which makes
3678 ;; the code anything but simple. The intention is that it will work
3679 ;; with any well-written text filling package that preserves a fill
3680 ;; prefix.
3682 ;; We temporarily mask comment starters and enders as necessary for
3683 ;; the filling code to do its job on a seemingly normal text block.
3684 ;; We do _not_ mask the fill prefix, so it's up to the filling code to
3685 ;; preserve it correctly (especially important when filling C++ style
3686 ;; line comments). By default, we set up and use adaptive-fill-mode,
3687 ;; which is standard in all supported Emacs flavors.
3689 (defun c-guess-fill-prefix (lit-limits lit-type)
3690 ;; Determine the appropriate comment fill prefix for a block or line
3691 ;; comment. Return a cons of the prefix string and the column where
3692 ;; it ends. If fill-prefix is set, it'll override. Note that this
3693 ;; function also uses the value of point in some heuristics.
3695 ;; This function might do hidden buffer changes.
3697 (let* ((here (point))
3698 (prefix-regexp (concat "[ \t]*\\("
3699 c-current-comment-prefix
3700 "\\)[ \t]*"))
3701 (comment-start-regexp (if (eq lit-type 'c++)
3702 prefix-regexp
3703 comment-start-skip))
3704 prefix-line comment-prefix res comment-text-end)
3706 (cond
3707 (fill-prefix
3708 (setq res (cons fill-prefix
3709 ;; Ugly way of getting the column after the fill
3710 ;; prefix; it'd be nice with a current-column
3711 ;; that works on strings..
3712 (let ((start (point)))
3713 (unwind-protect
3714 (progn
3715 (insert-and-inherit "\n" fill-prefix)
3716 (current-column))
3717 (delete-region start (point)))))))
3719 ((eq lit-type 'c++)
3720 (save-excursion
3721 ;; Set fallback for comment-prefix if none is found.
3722 (setq comment-prefix "// "
3723 comment-text-end (cdr lit-limits))
3725 (beginning-of-line)
3726 (if (> (point) (car lit-limits))
3727 ;; The current line is not the comment starter, so the
3728 ;; comment has more than one line, and it can therefore be
3729 ;; used to find the comment fill prefix.
3730 (setq prefix-line (point))
3732 (goto-char (car lit-limits))
3733 (if (and (= (forward-line 1) 0)
3734 (< (point) (cdr lit-limits)))
3735 ;; The line after the comment starter is inside the
3736 ;; comment, so we can use it.
3737 (setq prefix-line (point))
3739 ;; The comment is only one line. Take the comment prefix
3740 ;; from it and keep the indentation.
3741 (goto-char (car lit-limits))
3742 (if (looking-at prefix-regexp)
3743 (goto-char (match-end 0))
3744 (forward-char 2)
3745 (skip-chars-forward " \t"))
3747 (let (str col)
3748 (if (eq (c-point 'boi) (car lit-limits))
3749 ;; There is only whitespace before the comment
3750 ;; starter; take the prefix straight from this line.
3751 (setq str (buffer-substring-no-properties
3752 (c-point 'bol) (point))
3753 col (current-column))
3755 ;; There is code before the comment starter, so we
3756 ;; have to temporarily insert and indent a new line to
3757 ;; get the right space/tab mix in the indentation.
3758 (let ((prefix-len (- (point) (car lit-limits)))
3759 tmp)
3760 (unwind-protect
3761 (progn
3762 (goto-char (car lit-limits))
3763 (indent-to (prog1 (current-column)
3764 (insert ?\n)))
3765 (setq tmp (point))
3766 (forward-char prefix-len)
3767 (setq str (buffer-substring-no-properties
3768 (c-point 'bol) (point))
3769 col (current-column)))
3770 (delete-region (car lit-limits) tmp))))
3772 (setq res
3773 (if (or (string-match "\\s \\'" str) (not (eolp)))
3774 (cons str col)
3775 ;; The prefix ends the line with no whitespace
3776 ;; after it. Default to a single space.
3777 (cons (concat str " ") (1+ col))))
3778 )))))
3781 (setq comment-text-end
3782 (save-excursion
3783 (goto-char (- (cdr lit-limits) 2))
3784 (if (looking-at "\\*/") (point) (cdr lit-limits))))
3786 (save-excursion
3787 (beginning-of-line)
3788 (if (and (> (point) (car lit-limits))
3789 (not (and (looking-at "[ \t]*\\*/")
3790 (eq (cdr lit-limits) (match-end 0)))))
3791 ;; The current line is not the comment starter and
3792 ;; contains more than just the ender, so it's good enough
3793 ;; to be used for the comment fill prefix.
3794 (setq prefix-line (point))
3795 (goto-char (car lit-limits))
3797 (cond ((or (/= (forward-line 1) 0)
3798 (>= (point) (cdr lit-limits))
3799 (and (looking-at "[ \t]*\\*/")
3800 (eq (cdr lit-limits) (match-end 0)))
3801 (and (looking-at prefix-regexp)
3802 (<= (1- (cdr lit-limits)) (match-end 0))))
3803 ;; The comment is either one line or the next line contains
3804 ;; just the comment ender. In this case we have no
3805 ;; information about a suitable comment prefix, so we resort
3806 ;; to c-block-comment-prefix.
3807 (setq comment-prefix (or c-block-comment-prefix "")))
3809 ((< here (point))
3810 ;; The point was on the comment opener line, so we might want
3811 ;; to treat this as a not yet closed comment.
3813 (if (and (match-beginning 1)
3814 (/= (match-beginning 1) (match-end 1)))
3815 ;; Above `prefix-regexp' matched a nonempty prefix on the
3816 ;; second line, so let's use it. Normally it should do
3817 ;; to set `prefix-line' and let the code below pick up
3818 ;; the whole prefix, but if there's no text after the
3819 ;; match then it will probably fall back to no prefix at
3820 ;; all if the comment isn't closed yet, so in that case
3821 ;; it's better to force use of the prefix matched now.
3822 (if (= (match-end 0) (c-point 'eol))
3823 (setq comment-prefix (match-string 1))
3824 (setq prefix-line (point)))
3826 ;; There's no nonempty prefix on the line after the
3827 ;; comment opener. If the line is empty, or if the
3828 ;; text on it has less or equal indentation than the
3829 ;; comment starter we assume it's an unclosed
3830 ;; comment starter, i.e. that
3831 ;; `c-block-comment-prefix' should be used.
3832 ;; Otherwise we assume it's a closed comment where
3833 ;; the prefix really is the empty string.
3834 ;; E.g. this is an unclosed comment:
3836 ;; /*
3837 ;; foo
3839 ;; But this is not:
3841 ;; /*
3842 ;; foo
3843 ;; */
3845 ;; (Looking for the presence of the comment closer
3846 ;; rarely works since it's probably the closer of
3847 ;; some comment further down when the comment
3848 ;; really is unclosed.)
3849 (if (<= (save-excursion (back-to-indentation)
3850 (current-column))
3851 (save-excursion (goto-char (car lit-limits))
3852 (current-column)))
3853 (setq comment-prefix (or c-block-comment-prefix ""))
3854 (setq prefix-line (point)))))
3857 ;; Otherwise the line after the comment starter is good
3858 ;; enough to find the prefix in.
3859 (setq prefix-line (point))))
3861 (when comment-prefix
3862 ;; Haven't got the comment prefix on any real line that we
3863 ;; can take it from, so we have to temporarily insert
3864 ;; `comment-prefix' on a line and indent it to find the
3865 ;; correct column and the correct mix of tabs and spaces.
3866 (setq res
3867 (let (tmp-pre tmp-post)
3868 (unwind-protect
3869 (progn
3871 (goto-char (car lit-limits))
3872 (if (looking-at comment-start-regexp)
3873 (goto-char (min (match-end 0)
3874 comment-text-end))
3875 (forward-char 2)
3876 (skip-chars-forward " \t"))
3878 (when (eq (char-syntax (char-before)) ?\ )
3879 ;; If there's ws on the current line, we'll use it
3880 ;; instead of what's ending comment-prefix.
3881 (setq comment-prefix
3882 (concat (substring comment-prefix
3883 0 (string-match
3884 "\\s *\\'"
3885 comment-prefix))
3886 (buffer-substring-no-properties
3887 (save-excursion
3888 (skip-chars-backward " \t")
3889 (point))
3890 (point)))))
3892 (setq tmp-pre (point-marker))
3894 ;; We insert an extra non-whitespace character
3895 ;; before the line break and after comment-prefix in
3896 ;; case it's "" or ends with whitespace.
3897 (insert-and-inherit "x\n" comment-prefix "x")
3898 (setq tmp-post (point-marker))
3900 (indent-according-to-mode)
3902 (goto-char (1- tmp-post))
3903 (cons (buffer-substring-no-properties
3904 (c-point 'bol) (point))
3905 (current-column)))
3907 (when tmp-post
3908 (delete-region tmp-pre tmp-post)
3909 (set-marker tmp-pre nil)
3910 (set-marker tmp-post nil))))))))))
3912 (or res ; Found a good prefix above.
3914 (save-excursion
3915 ;; prefix-line is the bol of a line on which we should try
3916 ;; to find the prefix.
3917 (let* (fb-string fb-endpos ; Contains any fallback prefix found.
3918 (test-line
3919 (lambda ()
3920 (when (and (looking-at prefix-regexp)
3921 (<= (match-end 0) comment-text-end))
3922 (unless (eq (match-end 0) (c-point 'eol))
3923 ;; The match is fine if there's text after it.
3924 (throw 'found (cons (buffer-substring-no-properties
3925 (match-beginning 0) (match-end 0))
3926 (progn (goto-char (match-end 0))
3927 (current-column)))))
3928 (unless fb-string
3929 ;; This match is better than nothing, so let's
3930 ;; remember it in case nothing better is found
3931 ;; on another line.
3932 (setq fb-string (buffer-substring-no-properties
3933 (match-beginning 0) (match-end 0))
3934 fb-endpos (match-end 0)))
3935 t))))
3937 (or (catch 'found
3938 ;; Search for a line which has text after the prefix
3939 ;; so that we get the proper amount of whitespace
3940 ;; after it. We start with the current line, then
3941 ;; search backwards, then forwards.
3943 (goto-char prefix-line)
3944 (when (and (funcall test-line)
3945 (or (/= (match-end 1) (match-end 0))
3946 ;; The whitespace is sucked up by the
3947 ;; first [ \t]* glob if the prefix is empty.
3948 (and (= (match-beginning 1) (match-end 1))
3949 (/= (match-beginning 0) (match-end 0)))))
3950 ;; If the current line doesn't have text but do
3951 ;; have whitespace after the prefix, we'll use it.
3952 (throw 'found (cons fb-string
3953 (progn (goto-char fb-endpos)
3954 (current-column)))))
3956 (if (eq lit-type 'c++)
3957 ;; For line comments we can search up to and
3958 ;; including the first line.
3959 (while (and (zerop (forward-line -1))
3960 (>= (point) (car lit-limits)))
3961 (funcall test-line))
3962 ;; For block comments we must stop before the
3963 ;; block starter.
3964 (while (and (zerop (forward-line -1))
3965 (> (point) (car lit-limits)))
3966 (funcall test-line)))
3968 (goto-char prefix-line)
3969 (while (and (zerop (forward-line 1))
3970 (< (point) (cdr lit-limits)))
3971 (funcall test-line))
3973 (goto-char prefix-line)
3974 nil)
3976 (when fb-string
3977 ;; A good line wasn't found, but at least we have a
3978 ;; fallback that matches the comment prefix regexp.
3979 (cond ((or (string-match "\\s \\'" fb-string)
3980 (progn
3981 (goto-char fb-endpos)
3982 (not (eolp))))
3983 ;; There are ws or text after the prefix, so
3984 ;; let's use it.
3985 (cons fb-string (current-column)))
3987 ((progn
3988 ;; Check if there's any whitespace padding
3989 ;; on the comment start line that we can
3990 ;; use after the prefix.
3991 (goto-char (car lit-limits))
3992 (if (looking-at comment-start-regexp)
3993 (goto-char (match-end 0))
3994 (forward-char 2)
3995 (skip-chars-forward " \t"))
3996 (or (not (eolp))
3997 (eq (char-syntax (char-before)) ?\ )))
3999 (setq fb-string (buffer-substring-no-properties
4000 (save-excursion
4001 (skip-chars-backward " \t")
4002 (point))
4003 (point)))
4004 (goto-char fb-endpos)
4005 (skip-chars-backward " \t")
4007 (let ((tmp (point)))
4008 ;; Got to mess in the buffer once again to
4009 ;; ensure the column gets correct. :P
4010 (unwind-protect
4011 (progn
4012 (insert-and-inherit fb-string)
4013 (cons (buffer-substring-no-properties
4014 (c-point 'bol)
4015 (point))
4016 (current-column)))
4017 (delete-region tmp (point)))))
4020 ;; Last resort: Just add a single space after
4021 ;; the prefix.
4022 (cons (concat fb-string " ")
4023 (progn (goto-char fb-endpos)
4024 (1+ (current-column)))))))
4026 ;; The line doesn't match the comment prefix regexp.
4027 (if comment-prefix
4028 ;; We have a fallback for line comments that we must use.
4029 (cons (concat (buffer-substring-no-properties
4030 prefix-line (c-point 'boi))
4031 comment-prefix)
4032 (progn (back-to-indentation)
4033 (+ (current-column) (length comment-prefix))))
4035 ;; Assume we are dealing with a "free text" block
4036 ;; comment where the lines doesn't have any comment
4037 ;; prefix at all and we should just fill it as
4038 ;; normal text.
4039 '("" . 0))))))
4042 (defun c-mask-paragraph (fill-paragraph apply-outside-literal fun &rest args)
4043 ;; Calls FUN with ARGS ar arguments while the current paragraph is
4044 ;; masked to allow adaptive filling to work correctly. That
4045 ;; includes narrowing the buffer and, if point is inside a comment,
4046 ;; masking the comment starter and ender appropriately.
4048 ;; FILL-PARAGRAPH is non-nil if called for whole paragraph filling.
4049 ;; The position of point is then less significant when doing masking
4050 ;; and narrowing.
4052 ;; If APPLY-OUTSIDE-LITERAL is nil then the function will be called
4053 ;; only if the point turns out to be inside a comment or a string.
4055 ;; Note that this function does not do any hidden buffer changes.
4057 (let (fill
4058 ;; beg and end limit the region to narrow. end is a marker.
4059 beg end
4060 ;; tmp-pre and tmp-post mark strings that are temporarily
4061 ;; inserted at the start and end of the region. tmp-pre is a
4062 ;; cons of the positions of the prepended string. tmp-post is
4063 ;; a marker pointing to the single character of the appended
4064 ;; string.
4065 tmp-pre tmp-post
4066 ;; If hang-ender-stuck isn't nil, the comment ender is
4067 ;; hanging. In that case it's set to the number of spaces
4068 ;; that should be between the text and the ender.
4069 hang-ender-stuck
4070 ;; auto-fill-spaces is the exact sequence of whitespace between a
4071 ;; comment's last word and the comment ender, temporarily replaced
4072 ;; with 'x's before calling FUN when FILL-PARAGRAPH is nil.
4073 auto-fill-spaces
4074 (here (point))
4075 (c-lit-limits c-lit-limits)
4076 (c-lit-type c-lit-type))
4078 ;; Restore point on undo. It's necessary since we do a lot of
4079 ;; hidden inserts and deletes below that should be as transparent
4080 ;; as possible.
4081 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
4082 (setq buffer-undo-list (cons (point) buffer-undo-list)))
4084 ;; Determine the limits and type of the containing literal (if any):
4085 ;; C-LIT-LIMITS, C-LIT-TYPE; and the limits of the current paragraph:
4086 ;; BEG and END.
4087 (c-save-buffer-state ()
4088 (save-restriction
4089 ;; Widen to catch comment limits correctly.
4090 (widen)
4091 (unless c-lit-limits
4092 (setq c-lit-limits (c-literal-limits nil fill-paragraph)))
4093 (setq c-lit-limits (c-collect-line-comments c-lit-limits))
4094 (unless c-lit-type
4095 (setq c-lit-type (c-literal-type c-lit-limits))))
4097 (save-excursion
4098 (unless (c-safe (backward-char)
4099 (forward-paragraph)
4100 (>= (point) here))
4101 (goto-char here)
4102 (forward-paragraph))
4103 (setq end (point-marker)))
4104 (save-excursion
4105 (unless (c-safe (forward-char)
4106 (backward-paragraph)
4107 (<= (point) here))
4108 (goto-char here)
4109 (backward-paragraph))
4110 (setq beg (point))))
4112 (unwind-protect
4113 (progn
4114 ;; For each of the possible types of text (string, C comment ...)
4115 ;; determine BEG and END, the region we will narrow to. If we're in
4116 ;; a literal, constrain BEG and END to the limits of this literal.
4118 ;; For some of these text types, particularly a block comment, we
4119 ;; may need to massage whitespace near literal delimiters, so that
4120 ;; these don't get filled inappropriately.
4121 (cond
4123 ((eq c-lit-type 'c++) ; Line comment.
4124 (save-excursion
4125 ;; Limit to the comment or paragraph end, whichever
4126 ;; comes first.
4127 (set-marker end (min end (cdr c-lit-limits)))
4129 (when (<= beg (car c-lit-limits))
4130 ;; The region includes the comment starter, so we must
4131 ;; check it.
4132 (goto-char (car c-lit-limits))
4133 (back-to-indentation)
4134 (if (eq (point) (car c-lit-limits))
4135 ;; Include the first line in the region.
4136 (setq beg (c-point 'bol))
4137 ;; The first line contains code before the
4138 ;; comment. We must fake a line that doesn't.
4139 (setq tmp-pre t))))
4141 (setq apply-outside-literal t))
4143 ((eq c-lit-type 'c) ; Block comment.
4144 (when
4145 (or (> end (cdr c-lit-limits))
4146 (and (= end (cdr c-lit-limits))
4147 (eq (char-before end) ?/)
4148 (eq (char-before (1- end)) ?*)
4149 ;; disallow "/*/"
4150 (> (- (cdr c-lit-limits) (car c-lit-limits)) 3)))
4151 ;; There is a comment ender, and the region includes it. If
4152 ;; it's on its own line, it stays on its own line. If it's got
4153 ;; company on the line, it keeps (at least one word of) it.
4154 ;; "=====*/" counts as a comment ender here, but "===== */"
4155 ;; doesn't and "foo*/" doesn't.
4156 (unless
4157 (save-excursion
4158 (goto-char (cdr c-lit-limits))
4159 (beginning-of-line)
4160 ;; The following conjunct was added to avoid an
4161 ;; "Invalid search bound (wrong side of point)"
4162 ;; error in the subsequent re-search. Maybe
4163 ;; another fix would be needed (2007-12-08).
4164 ; (or (<= (- (cdr c-lit-limits) 2) (point))
4165 ; 2010-10-17 Construct removed.
4166 ; (or (< (- (cdr c-lit-limits) 2) (point))
4167 (and
4168 (search-forward-regexp
4169 (concat "\\=[ \t]*\\(" c-current-comment-prefix "\\)")
4170 (- (cdr c-lit-limits) 2) t)
4171 (not (search-forward-regexp
4172 "\\(\\s \\|\\sw\\)"
4173 (- (cdr c-lit-limits) 2) 'limit))
4174 ;; The comment ender IS on its own line. Exclude this
4175 ;; line from the filling.
4176 (set-marker end (c-point 'bol))));)
4178 ;; The comment ender is hanging. Replace all space between it
4179 ;; and the last word either by one or two 'x's (when
4180 ;; FILL-PARAGRAPH is non-nil), or a row of x's the same width
4181 ;; as the whitespace (when auto filling), and include it in
4182 ;; the region. We'll change them back to whitespace
4183 ;; afterwards. The effect of this is to glue the comment
4184 ;; ender to the last word in the comment during filling.
4185 (let* ((ender-start (save-excursion
4186 (goto-char (cdr c-lit-limits))
4187 (skip-syntax-backward "^w ")
4188 (point)))
4189 (ender-column (save-excursion
4190 (goto-char ender-start)
4191 (current-column)))
4192 (point-rel (- ender-start here))
4193 (sentence-ends-comment
4194 (save-excursion
4195 (goto-char ender-start)
4196 (and (search-backward-regexp
4197 (c-sentence-end) (c-point 'bol) t)
4198 (goto-char (match-end 0))
4199 (looking-at "[ \t]*")
4200 (= (match-end 0) ender-start))))
4201 spaces)
4203 (save-excursion
4204 ;; Insert a CR after the "*/", adjust END
4205 (goto-char (cdr c-lit-limits))
4206 (setq tmp-post (point-marker))
4207 (insert ?\n)
4208 (set-marker end (point))
4210 (forward-line -1) ; last line of the comment
4211 (if (and (looking-at (concat "[ \t]*\\(\\("
4212 c-current-comment-prefix
4213 "\\)[ \t]*\\)"))
4214 (eq ender-start (match-end 0)))
4215 ;; The comment ender is prefixed by nothing but a
4216 ;; comment line prefix. IS THIS POSSIBLE? (ACM,
4217 ;; 2006/4/28). Remove it along with surrounding ws.
4218 (setq spaces (- (match-end 1) (match-end 2)))
4219 (goto-char ender-start))
4220 (skip-chars-backward " \t\r\n") ; Surely this can be
4221 ; " \t"? "*/" is NOT alone on the line (ACM, 2005/8/18)
4223 ;; What's being tested here? 2006/4/20. FIXME!!!
4224 (if (/= (point) ender-start)
4225 (progn
4226 (if (<= here (point))
4227 ;; Don't adjust point below if it's
4228 ;; before the string we replace.
4229 (setq point-rel -1))
4230 ;; Keep one or two spaces between the
4231 ;; text and the ender, depending on how
4232 ;; many there are now.
4233 (unless spaces
4234 (setq spaces (- ender-column (current-column))))
4235 (setq auto-fill-spaces (c-delete-and-extract-region
4236 (point) ender-start))
4237 ;; paragraph filling condenses multiple spaces to
4238 ;; single or double spaces. auto-fill doesn't.
4239 (if fill-paragraph
4240 (setq spaces
4241 (max
4242 (min spaces
4243 (if (and sentence-ends-comment
4244 sentence-end-double-space)
4245 2 1))
4246 1)))
4247 ;; Insert the filler first to keep marks right.
4248 (insert-char ?x spaces t)
4249 (setq hang-ender-stuck spaces)
4250 (setq point-rel
4251 (and (>= point-rel 0)
4252 (- (point) (min point-rel spaces)))))
4253 (setq point-rel nil)))
4255 (if point-rel
4256 ;; Point was in the middle of the string we
4257 ;; replaced above, so put it back in the same
4258 ;; relative position, counting from the end.
4259 (goto-char point-rel)))
4262 (when (<= beg (car c-lit-limits))
4263 ;; The region includes the comment starter.
4264 (save-excursion
4265 (goto-char (car c-lit-limits))
4266 (if (looking-at (concat "\\(" comment-start-skip "\\)$"))
4267 ;; Begin with the next line.
4268 (setq beg (c-point 'bonl))
4269 ;; Fake the fill prefix in the first line.
4270 (setq tmp-pre t))))
4272 (setq apply-outside-literal t))
4274 ((eq c-lit-type 'string) ; String.
4275 (save-excursion
4276 (when (>= end (cdr c-lit-limits))
4277 (goto-char (1- (cdr c-lit-limits)))
4278 (setq tmp-post (point-marker))
4279 (insert ?\n)
4280 (set-marker end (point)))
4281 (when (<= beg (car c-lit-limits))
4282 (goto-char (1+ (car c-lit-limits)))
4283 (setq beg (if (looking-at "\\\\$")
4284 ;; Leave the start line if it's
4285 ;; nothing but an escaped newline.
4286 (1+ (match-end 0))
4287 (point)))))
4288 (setq apply-outside-literal t))
4290 ((eq c-lit-type 'pound) ; Macro
4291 ;; Narrow to the macro limits if they are nearer than the
4292 ;; paragraph limits. Don't know if this is necessary but
4293 ;; do it for completeness sake (doing auto filling at all
4294 ;; inside macros is bogus to begin with since the line
4295 ;; continuation backslashes aren't handled).
4296 (save-excursion
4297 (c-save-buffer-state ()
4298 (c-beginning-of-macro)
4299 (beginning-of-line)
4300 (if (> (point) beg)
4301 (setq beg (point)))
4302 (c-end-of-macro)
4303 (forward-line)
4304 (if (< (point) end)
4305 (set-marker end (point))))))
4307 (t ; Other code.
4308 ;; Try to avoid comments and macros in the paragraph to
4309 ;; avoid that the adaptive fill mode gets the prefix from
4310 ;; them.
4311 (c-save-buffer-state nil
4312 (save-excursion
4313 (goto-char beg)
4314 (c-forward-syntactic-ws end)
4315 (beginning-of-line)
4316 (setq beg (point))
4317 (goto-char end)
4318 (c-backward-syntactic-ws beg)
4319 (forward-line)
4320 (set-marker end (point))))))
4322 (when tmp-pre
4323 ;; Temporarily insert the fill prefix after the comment
4324 ;; starter so that the first line looks like any other
4325 ;; comment line in the narrowed region.
4326 (setq fill (c-save-buffer-state nil
4327 (c-guess-fill-prefix c-lit-limits c-lit-type)))
4328 (unless (string-match (concat "\\`[ \t]*\\("
4329 c-current-comment-prefix
4330 "\\)[ \t]*\\'")
4331 (car fill))
4332 ;; Oops, the prefix doesn't match the comment prefix
4333 ;; regexp. This could produce very confusing
4334 ;; results with adaptive fill packages together with
4335 ;; the insert prefix magic below, since the prefix
4336 ;; often doesn't appear at all. So let's warn about
4337 ;; it.
4338 (message "\
4339 Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S"
4340 (car fill)))
4341 ;; Find the right spot on the line, break it, insert
4342 ;; the fill prefix and make sure we're back in the
4343 ;; same column by temporarily prefixing the first word
4344 ;; with a number of 'x'.
4345 (save-excursion
4346 (goto-char (car c-lit-limits))
4347 (if (looking-at (if (eq c-lit-type 'c++)
4348 c-current-comment-prefix
4349 comment-start-skip))
4350 (goto-char (match-end 0))
4351 (forward-char 2)
4352 (skip-chars-forward " \t"))
4353 (while (and (< (current-column) (cdr fill))
4354 (not (eolp)))
4355 (forward-char 1))
4356 (let ((col (current-column)))
4357 (setq beg (1+ (point))
4358 tmp-pre (list (point)))
4359 (unwind-protect
4360 (progn
4361 (insert-and-inherit "\n" (car fill))
4362 (insert-char ?x (- col (current-column)) t))
4363 (setcdr tmp-pre (point))))))
4365 (when apply-outside-literal
4366 ;; `apply-outside-literal' is always set to t here if
4367 ;; we're inside a literal.
4369 (let ((fill-prefix
4370 (or fill-prefix
4371 ;; Kludge: If the function that adapts the fill prefix
4372 ;; doesn't produce the required comment starter for
4373 ;; line comments, then force it by setting fill-prefix.
4374 (when (and (eq c-lit-type 'c++)
4375 ;; Kludge the kludge: filladapt-mode doesn't
4376 ;; have this problem, but it currently
4377 ;; doesn't override fill-context-prefix
4378 ;; (version 2.12).
4379 (not (and (boundp 'filladapt-mode)
4380 filladapt-mode))
4381 (not (string-match
4382 "\\`[ \t]*//"
4383 (or (fill-context-prefix beg end)
4384 ""))))
4385 (c-save-buffer-state nil
4386 (car (or fill (c-guess-fill-prefix
4387 c-lit-limits c-lit-type)))))))
4389 ;; Save the relative position of point if it's outside the
4390 ;; region we're going to narrow. Want to restore it in that
4391 ;; case, but otherwise it should be moved according to the
4392 ;; called function.
4393 (point-rel (cond ((< (point) beg) (- (point) beg))
4394 ((> (point) end) (- (point) end)))))
4396 ;; Preparations finally done! Now we can call the
4397 ;; actual function.
4398 (prog1
4399 (save-restriction
4400 (narrow-to-region beg end)
4401 (apply fun args))
4402 (if point-rel
4403 ;; Restore point if it was outside the region.
4404 (if (< point-rel 0)
4405 (goto-char (+ beg point-rel))
4406 (goto-char (+ end point-rel))))))))
4408 (when (consp tmp-pre)
4409 (delete-region (car tmp-pre) (cdr tmp-pre)))
4411 (when tmp-post
4412 (save-excursion
4413 (goto-char tmp-post)
4414 (delete-char 1))
4415 (when hang-ender-stuck
4416 ;; Preserve point even if it's in the middle of the string
4417 ;; we replace; save-excursion doesn't work in that case.
4418 (setq here (point))
4419 (goto-char tmp-post)
4420 (skip-syntax-backward "^w ")
4421 (forward-char (- hang-ender-stuck))
4422 (if (or fill-paragraph (not auto-fill-spaces))
4423 (insert-char ?\ hang-ender-stuck t)
4424 (insert auto-fill-spaces))
4425 (delete-char hang-ender-stuck)
4426 (goto-char here))
4427 (set-marker tmp-post nil))
4429 (set-marker end nil))))
4431 (defun c-fill-paragraph (&optional arg)
4432 "Like \\[fill-paragraph] but handles C and C++ style comments.
4433 If any of the current line is a comment or within a comment, fill the
4434 comment or the paragraph of it that point is in, preserving the
4435 comment indentation or line-starting decorations (see the
4436 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
4437 details).
4439 If point is inside multiline string literal, fill it. This currently
4440 does not respect escaped newlines, except for the special case when it
4441 is the very first thing in the string. The intended use for this rule
4442 is in situations like the following:
4444 char description[] = \"\\
4445 A very long description of something that you want to fill to make
4446 nicely formatted output.\"\;
4448 If point is in any other situation, i.e. in normal code, do nothing.
4450 Optional prefix ARG means justify paragraph as well."
4451 (interactive "*P")
4452 (let ((fill-paragraph-function
4453 ;; Avoid infinite recursion.
4454 (if (not (eq fill-paragraph-function 'c-fill-paragraph))
4455 fill-paragraph-function)))
4456 (c-mask-paragraph t nil 'fill-paragraph arg))
4457 ;; Always return t. This has the effect that if filling isn't done
4458 ;; above, it isn't done at all, and it's therefore effectively
4459 ;; disabled in normal code.
4462 (defun c-do-auto-fill ()
4463 ;; Do automatic filling if not inside a context where it should be
4464 ;; ignored.
4465 (let ((c-auto-fill-prefix
4466 ;; The decision whether the line should be broken is actually
4467 ;; done in c-indent-new-comment-line, which do-auto-fill
4468 ;; calls to break lines. We just set this special variable
4469 ;; so that we'll know when we're called from there. It's
4470 ;; also used to detect whether fill-prefix is user set or
4471 ;; generated automatically by do-auto-fill.
4472 fill-prefix))
4473 (c-mask-paragraph nil t 'do-auto-fill)))
4475 (defun c-indent-new-comment-line (&optional soft allow-auto-fill)
4476 "Break line at point and indent, continuing comment or macro if within one.
4477 If inside a comment and `comment-multi-line' is non-nil, the
4478 indentation and line prefix are preserved (see the
4479 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
4480 details). If inside a single line comment and `comment-multi-line' is
4481 nil, a new comment of the same type is started on the next line and
4482 indented as appropriate for comments. If inside a macro, a line
4483 continuation backslash is inserted and aligned as appropriate, and the
4484 new line is indented according to `c-syntactic-indentation'.
4486 If a fill prefix is specified, it overrides all the above."
4487 ;; allow-auto-fill is used from c-context-line-break to allow auto
4488 ;; filling to break the line more than once. Since this function is
4489 ;; used from auto-fill itself, that's normally disabled to avoid
4490 ;; unnecessary recursion.
4491 (interactive)
4492 (let ((fill-prefix fill-prefix)
4493 (do-line-break
4494 (lambda ()
4495 (delete-horizontal-space)
4496 (if soft
4497 (insert-and-inherit ?\n)
4498 (newline (if allow-auto-fill nil 1)))))
4499 ;; Already know the literal type and limits when called from
4500 ;; c-context-line-break.
4501 (c-lit-limits c-lit-limits)
4502 (c-lit-type c-lit-type)
4503 (c-macro-start c-macro-start))
4505 (c-save-buffer-state ()
4506 (when (not (eq c-auto-fill-prefix t))
4507 ;; Called from do-auto-fill.
4508 (unless c-lit-limits
4509 (setq c-lit-limits (c-literal-limits nil nil t)))
4510 (unless c-lit-type
4511 (setq c-lit-type (c-literal-type c-lit-limits)))
4512 (if (memq (cond ((c-query-and-set-macro-start) 'cpp)
4513 ((null c-lit-type) 'code)
4514 (t c-lit-type))
4515 c-ignore-auto-fill)
4516 (setq fill-prefix t) ; Used as flag in the cond.
4517 (if (and (null c-auto-fill-prefix)
4518 (eq c-lit-type 'c)
4519 (<= (c-point 'bol) (car c-lit-limits)))
4520 ;; The adaptive fill function has generated a prefix, but
4521 ;; we're on the first line in a block comment so it'll be
4522 ;; wrong. Ignore it to guess a better one below.
4523 (setq fill-prefix nil)
4524 (when (and (eq c-lit-type 'c++)
4525 (not (string-match (concat "\\`[ \t]*"
4526 c-line-comment-starter)
4527 (or fill-prefix ""))))
4528 ;; Kludge: If the function that adapted the fill prefix
4529 ;; doesn't produce the required comment starter for line
4530 ;; comments, then we ignore it.
4531 (setq fill-prefix nil)))
4534 (cond ((eq fill-prefix t)
4535 ;; A call from do-auto-fill which should be ignored.
4537 (fill-prefix
4538 ;; A fill-prefix overrides anything.
4539 (funcall do-line-break)
4540 (insert-and-inherit fill-prefix))
4541 ((c-save-buffer-state ()
4542 (unless c-lit-limits
4543 (setq c-lit-limits (c-literal-limits)))
4544 (unless c-lit-type
4545 (setq c-lit-type (c-literal-type c-lit-limits)))
4546 (memq c-lit-type '(c c++)))
4547 ;; Some sort of comment.
4548 (if (or comment-multi-line
4549 (save-excursion
4550 (goto-char (car c-lit-limits))
4551 (end-of-line)
4552 (< (point) (cdr c-lit-limits))))
4553 ;; Inside a comment that should be continued.
4554 (let ((fill (c-save-buffer-state nil
4555 (c-guess-fill-prefix
4556 (setq c-lit-limits
4557 (c-collect-line-comments c-lit-limits))
4558 c-lit-type)))
4559 (pos (point))
4560 (start-col (current-column))
4561 (comment-text-end
4562 (or (and (eq c-lit-type 'c)
4563 (save-excursion
4564 (goto-char (- (cdr c-lit-limits) 2))
4565 (if (looking-at "\\*/") (point))))
4566 (cdr c-lit-limits))))
4567 ;; Skip forward past the fill prefix in case
4568 ;; we're standing in it.
4570 ;; FIXME: This doesn't work well in cases like
4572 ;; /* Bla bla bla bla bla
4573 ;; bla bla
4575 ;; If point is on the 'B' then the line will be
4576 ;; broken after "Bla b".
4578 ;; If we have an empty comment, /* */, the next
4579 ;; lot of code pushes point to the */. We fix
4580 ;; this by never allowing point to end up to the
4581 ;; right of where it started.
4582 (while (and (< (current-column) (cdr fill))
4583 (not (eolp)))
4584 (forward-char 1))
4585 (if (and (> (point) comment-text-end)
4586 (> (c-point 'bol) (car c-lit-limits)))
4587 (progn
4588 ;; The skip takes us out of the (block)
4589 ;; comment; insert the fill prefix at bol
4590 ;; instead and keep the position.
4591 (setq pos (copy-marker pos t))
4592 (beginning-of-line)
4593 (insert-and-inherit (car fill))
4594 (if soft (insert-and-inherit ?\n) (newline 1))
4595 (goto-char pos)
4596 (set-marker pos nil))
4597 ;; Don't break in the middle of a comment starter
4598 ;; or ender.
4599 (cond ((> (point) comment-text-end)
4600 (goto-char comment-text-end))
4601 ((< (point) (+ (car c-lit-limits) 2))
4602 (goto-char (+ (car c-lit-limits) 2))))
4603 (funcall do-line-break)
4604 (insert-and-inherit (car fill))
4605 (if (> (current-column) start-col)
4606 (move-to-column start-col)))) ; can this hit the
4607 ; middle of a TAB?
4608 ;; Inside a comment that should be broken.
4609 (let ((comment-start comment-start)
4610 (comment-end comment-end)
4611 col)
4612 (if (eq c-lit-type 'c)
4613 (unless (string-match "[ \t]*/\\*" comment-start)
4614 (setq comment-start "/* " comment-end " */"))
4615 (unless (string-match "[ \t]*//" comment-start)
4616 (setq comment-start "// " comment-end "")))
4617 (setq col (save-excursion
4618 (back-to-indentation)
4619 (current-column)))
4620 (funcall do-line-break)
4621 (when (and comment-end (not (equal comment-end "")))
4622 (forward-char -1)
4623 (insert-and-inherit comment-end)
4624 (forward-char 1))
4625 ;; c-comment-indent may look at the current
4626 ;; indentation, so let's start out with the same
4627 ;; indentation as the previous one.
4628 (indent-to col)
4629 (insert-and-inherit comment-start)
4630 (indent-for-comment))))
4631 ((c-query-and-set-macro-start)
4632 ;; In a macro.
4633 (unless (looking-at "[ \t]*\\\\$")
4634 ;; Do not clobber the alignment of the line continuation
4635 ;; slash; c-backslash-region might look at it.
4636 (delete-horizontal-space))
4637 ;; Got an asymmetry here: In normal code this command
4638 ;; doesn't indent the next line syntactically, and otoh a
4639 ;; normal syntactically indenting newline doesn't continue
4640 ;; the macro.
4641 (c-newline-and-indent (if allow-auto-fill nil 1)))
4643 ;; Somewhere else in the code.
4644 (let ((col (save-excursion
4645 (beginning-of-line)
4646 (while (and (looking-at "[ \t]*\\\\?$")
4647 (= (forward-line -1) 0)))
4648 (current-indentation))))
4649 (funcall do-line-break)
4650 (indent-to col))))))
4652 (defalias 'c-comment-line-break-function 'c-indent-new-comment-line)
4653 (make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line "21.1")
4655 ;; advice for indent-new-comment-line for older Emacsen
4656 (unless (boundp 'comment-line-break-function)
4657 (defvar c-inside-line-break-advice nil)
4658 (defadvice indent-new-comment-line (around c-line-break-advice
4659 activate preactivate)
4660 "Call `c-indent-new-comment-line' if in CC Mode."
4661 (if (or c-inside-line-break-advice
4662 (not c-buffer-is-cc-mode))
4663 ad-do-it
4664 (let ((c-inside-line-break-advice t))
4665 (c-indent-new-comment-line (ad-get-arg 0))))))
4667 (defun c-context-line-break ()
4668 "Do a line break suitable to the context.
4670 When point is outside a comment or macro, insert a newline and indent
4671 according to the syntactic context, unless `c-syntactic-indentation'
4672 is nil, in which case the new line is indented as the previous
4673 non-empty line instead.
4675 When point is inside the content of a preprocessor directive, a line
4676 continuation backslash is inserted before the line break and aligned
4677 appropriately. The end of the cpp directive doesn't count as inside
4680 When point is inside a comment, continue it with the appropriate
4681 comment prefix (see the `c-comment-prefix-regexp' and
4682 `c-block-comment-prefix' variables for details). The end of a
4683 C++-style line comment doesn't count as inside it.
4685 When point is inside a string, only insert a backslash when it is also
4686 inside a preprocessor directive."
4688 (interactive "*")
4689 (let* (c-lit-limits c-lit-type
4690 (c-macro-start c-macro-start)
4691 case-fold-search)
4693 (c-save-buffer-state ()
4694 (setq c-lit-limits (c-literal-limits nil nil t)
4695 c-lit-type (c-literal-type c-lit-limits))
4696 (when (eq c-lit-type 'c++)
4697 (setq c-lit-limits (c-collect-line-comments c-lit-limits)))
4698 (c-query-and-set-macro-start))
4700 (cond
4701 ((or (eq c-lit-type 'c)
4702 (and (eq c-lit-type 'c++) ; C++ comment, but not at the very end of it.
4703 (< (save-excursion
4704 (skip-chars-forward " \t")
4705 (point))
4706 (1- (cdr c-lit-limits))))
4707 (and (numberp c-macro-start) ; Macro, but not at the very end of
4708 ; it, not in a string, and not in the
4709 ; cpp keyword.
4710 (not (eq c-lit-type 'string))
4711 (or (not (looking-at "\\s *$"))
4712 (eq (char-before) ?\\))
4713 (<= (save-excursion
4714 (goto-char c-macro-start)
4715 (if (looking-at c-opt-cpp-start)
4716 (goto-char (match-end 0)))
4717 (point))
4718 (point))))
4719 (let ((comment-multi-line t)
4720 (fill-prefix nil))
4721 (c-indent-new-comment-line nil t)))
4723 ((eq c-lit-type 'string)
4724 (if (and (numberp c-macro-start)
4725 (not (eq (char-before) ?\\)))
4726 (insert ?\\))
4727 (newline))
4729 (t (delete-horizontal-space)
4730 (newline)
4731 ;; c-indent-line may look at the current indentation, so let's
4732 ;; start out with the same indentation as the previous line.
4733 (let ((col (save-excursion
4734 (backward-char)
4735 (forward-line 0)
4736 (while (and (looking-at "[ \t]*\\\\?$")
4737 (= (forward-line -1) 0)))
4738 (current-indentation))))
4739 (indent-to col))
4740 (indent-according-to-mode)))))
4742 (defun c-context-open-line ()
4743 "Insert a line break suitable to the context and leave point before it.
4744 This is the `c-context-line-break' equivalent to `open-line', which is
4745 normally bound to C-o. See `c-context-line-break' for the details."
4746 (interactive "*")
4747 (let ((here (point)))
4748 (unwind-protect
4749 (progn
4750 ;; Temporarily insert a non-whitespace char to keep any
4751 ;; preceding whitespace intact.
4752 (insert ?x)
4753 (c-context-line-break))
4754 (goto-char here)
4755 (delete-char 1))))
4758 (cc-provide 'cc-cmds)
4760 ;;; Local Variables:
4761 ;;; indent-tabs-mode: t
4762 ;;; tab-width: 8
4763 ;;; End:
4764 ;;; cc-cmds.el ends here