1 ;;; cc-cmds.el --- user level commands for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Authors: 2003- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
11 ;; 1987 Stewart Clamen
12 ;; 1985 Richard M. Stallman
13 ;; Maintainer: bug-cc-mode@gnu.org
14 ;; Created: 22-Apr-1997 (split from cc-mode.el)
15 ;; Version: See cc-mode.el
16 ;; Keywords: c languages oop
18 ;; This file is part of GNU Emacs.
20 ;; GNU Emacs is free software: you can redistribute it and/or modify
21 ;; it under the terms of the GNU General Public License as published by
22 ;; the Free Software Foundation, either version 3 of the License, or
23 ;; (at your option) any later version.
25 ;; GNU Emacs is distributed in the hope that it will be useful,
26 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;; GNU General Public License for more details.
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
39 (if (and (boundp 'byte-compile-dest-file)
40 (stringp byte-compile-dest-file))
41 (cons (file-name-directory byte-compile-dest-file) load-path)
43 (load "cc-bytecomp" nil t)))
47 (cc-require 'cc-engine)
49 ;; Silence the compiler.
50 (cc-bytecomp-defun delete-forward-p) ; XEmacs
51 (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge
52 ; which looks at this.
53 (cc-bytecomp-defun c-forward-subword)
54 (cc-bytecomp-defun c-backward-subword)
56 ;; Indentation / Display syntax functions
57 (defvar c-fix-backslashes t)
59 (defun c-indent-line (&optional syntax quiet ignore-point-pos)
60 "Indent the current line according to the syntactic context,
61 if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the
62 syntactic information for the current line. Be silent about syntactic
63 errors if the optional argument QUIET is non-nil, even if
64 `c-report-syntactic-errors' is non-nil. Normally the position of
65 point is used to decide where the old indentation is on a lines that
66 is otherwise empty \(ignoring any line continuation backslash), but
67 that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of
68 indentation change \(in columns)."
70 (let ((line-cont-backslash (save-excursion
72 (eq (char-before) ?\\)))
73 (c-fix-backslashes c-fix-backslashes)
76 (when (and (not ignore-point-pos)
79 (looking-at (if line-cont-backslash
80 ;; Don't use "\\s " - ^L doesn't count as WS
84 (<= (point) (match-end 1)))
85 ;; Delete all whitespace after point if there's only whitespace
86 ;; on the line, so that any code that does back-to-indentation
87 ;; or similar gets the current column in this case. If this
88 ;; removes a line continuation backslash it'll be restored
90 (unless c-auto-align-backslashes
91 ;; Should try to keep the backslash alignment
94 (goto-char (match-end 0))
95 (setq bs-col (1- (current-column)))))
96 (delete-region (point) (match-end 0))
97 (setq c-fix-backslashes t))
98 (if c-syntactic-indentation
100 (or (let ((c-parsing-error nil)
103 (and (boundp 'c-syntactic-context)
104 c-syntactic-context))))
105 (c-save-buffer-state (indent)
106 (unless c-syntactic-context
107 (setq c-syntactic-context (c-guess-basic-syntax)))
108 (setq indent (c-get-syntactic-indentation
109 c-syntactic-context))
110 (and (not (c-echo-parsing-error quiet))
111 c-echo-syntactic-information-p
112 (message "syntax: %s, indent: %d"
113 c-syntactic-context indent))
114 (setq shift-amt (- indent (current-indentation))))
115 (c-shift-line-indentation shift-amt)
116 (run-hooks 'c-special-indent-hook)
121 (while (and (= (forward-line -1) 0)
122 (if (looking-at "\\s *\\\\?$")
124 (setq indent (current-indentation))
126 (setq shift-amt (- indent (current-indentation)))
127 (c-shift-line-indentation shift-amt)))
128 (when (and c-fix-backslashes line-cont-backslash)
133 (when c-auto-align-backslashes
134 ;; Realign the line continuation backslash.
135 (c-backslash-region (point) (point) nil t))))
138 (defun c-newline-and-indent (&optional newline-arg)
139 "Insert a newline and indent the new line.
140 This function fixes line continuation backslashes if inside a macro,
141 and takes care to set the indentation before calling
142 `indent-according-to-mode', so that lineup functions like
143 `c-lineup-dont-change' works better."
145 ;; TODO: Backslashes before eol in comments and literals aren't
147 (let ((c-macro-start (c-query-macro-start))
148 ;; Avoid calling c-backslash-region from c-indent-line if it's
149 ;; called during the newline call, which can happen due to
150 ;; c-electric-continued-statement, for example. We also don't
151 ;; want any backslash alignment from indent-according-to-mode.
152 (c-fix-backslashes nil)
153 has-backslash insert-backslash
158 (while (and (looking-at "[ \t]*\\\\?$")
159 (= (forward-line -1) 0)))
160 (setq col (current-indentation)))
162 (if (and (eolp) (eq (char-before) ?\\))
163 (setq insert-backslash t
165 (setq has-backslash (eq (char-before (c-point 'eol)) ?\\))))
166 (newline newline-arg)
171 ;; The backslash stayed on the previous line. Insert one
172 ;; before calling c-backslash-region, so that
173 ;; bs-col-after-end in it works better. Fixup the
174 ;; backslashes on the newly inserted line.
177 (c-backslash-region (point) (point) nil t))
178 ;; The backslash moved to the new line, if there was any. Let
179 ;; c-backslash-region fix a backslash on the previous line,
180 ;; and the one that might be on the new line.
181 ;; c-auto-align-backslashes is intentionally ignored here;
182 ;; maybe the moved backslash should be left alone if it's set,
183 ;; but we fix both lines on the grounds that the old backslash
184 ;; has been moved anyway and is now in a different context.
185 (c-backslash-region start (if has-backslash (point) start) nil t)))
186 (when c-syntactic-indentation
187 ;; Reindent syntactically. The indentation done above is not
188 ;; wasted, since c-indent-line might look at the current
190 (let ((c-syntactic-context (c-save-buffer-state nil
191 (c-guess-basic-syntax))))
192 ;; We temporarily insert another line break, so that the
193 ;; lineup functions will see the line as empty. That makes
194 ;; e.g. c-lineup-cpp-define more intuitive since it then
195 ;; proceeds to the preceding line in this case.
197 (delete-horizontal-space)
198 (setq start (- (point-max) (point)))
202 (indent-according-to-mode))
203 (goto-char (- (point-max) start))
206 ;; Must align the backslash again after reindentation. The
207 ;; c-backslash-region call above can't be optimized to ignore
208 ;; this line, since it then won't align correctly with the
209 ;; lines below if the first line in the macro is broken.
210 (c-backslash-region (point) (point) nil t)))))
212 (defun c-show-syntactic-information (arg)
213 "Show syntactic information for current line.
214 With universal argument, inserts the analysis as a comment on that line."
216 (let* ((c-parsing-error nil)
217 (syntax (if (boundp 'c-syntactic-context)
218 ;; Use `c-syntactic-context' in the same way as
219 ;; `c-indent-line', to be consistent.
221 (c-save-buffer-state nil
222 (c-guess-basic-syntax)))))
223 (if (not (consp arg))
225 (message "Syntactic analysis: %s" syntax)
229 (setq elem (pop syntax))
230 (when (setq pos (c-langelem-pos elem))
231 (push (c-put-overlay pos (1+ pos)
234 (when (setq pos (c-langelem-2nd-pos elem))
235 (push (c-put-overlay pos (1+ pos)
236 'face 'secondary-selection)
240 (c-delete-overlay (pop ols)))))
242 (insert-and-inherit (format "%s" syntax))
244 (c-keep-region-active))
246 (defun c-syntactic-information-on-region (from to)
247 "Insert a comment with the syntactic analysis on every line in the region."
251 (narrow-to-region from to)
252 (goto-char (point-min))
254 (c-show-syntactic-information '(0))
258 ;; Minor mode functions.
259 (defun c-update-modeline ()
260 (let ((fmt (format "/%s%s%s%s"
261 (if c-electric-flag "l" "")
262 (if (and c-electric-flag c-auto-newline)
264 (if c-hungry-delete-key "h" "")
266 ;; cc-subword might not be loaded.
267 (boundp 'c-subword-mode)
268 (symbol-value 'c-subword-mode))
271 (bare-mode-name (if (string-match "\\(^[^/]*\\)/" mode-name)
272 (substring mode-name (match-beginning 1) (match-end 1))
274 ;; (setq c-submode-indicators
275 ;; (if (> (length fmt) 1)
278 (if (> (length fmt) 1)
279 (concat bare-mode-name fmt)
281 (force-mode-line-update)))
283 (defun c-toggle-syntactic-indentation (&optional arg)
284 "Toggle syntactic indentation.
285 Optional numeric ARG, if supplied, turns on syntactic indentation when
286 positive, turns it off when negative, and just toggles it when zero or
289 When syntactic indentation is turned on (the default), the indentation
290 functions and the electric keys indent according to the syntactic
291 context keys, when applicable.
293 When it's turned off, the electric keys don't reindent, the indentation
294 functions indents every new line to the same level as the previous
295 nonempty line, and \\[c-indent-command] adjusts the indentation in steps
296 specified by `c-basic-offset'. The indentation style has no effect in
297 this mode, nor any of the indentation associated variables,
298 e.g. `c-special-indent-hook'.
300 This command sets the variable `c-syntactic-indentation'."
302 (setq c-syntactic-indentation
303 (c-calculate-state arg c-syntactic-indentation))
304 (c-keep-region-active))
306 (defun c-toggle-auto-newline (&optional arg)
307 "Toggle auto-newline feature.
308 Optional numeric ARG, if supplied, turns on auto-newline when
309 positive, turns it off when negative, and just toggles it when zero or
312 Turning on auto-newline automatically enables electric indentation.
314 When the auto-newline feature is enabled (indicated by \"/la\" on the
315 modeline after the mode name) newlines are automatically inserted
316 after special characters such as brace, comma, semi-colon, and colon."
319 (c-calculate-state arg (and c-auto-newline c-electric-flag)))
320 (if c-auto-newline (setq c-electric-flag t))
322 (c-keep-region-active))
324 (defalias 'c-toggle-auto-state 'c-toggle-auto-newline)
325 (make-obsolete 'c-toggle-auto-state 'c-toggle-auto-newline)
327 (defun c-toggle-hungry-state (&optional arg)
328 "Toggle hungry-delete-key feature.
329 Optional numeric ARG, if supplied, turns on hungry-delete when
330 positive, turns it off when negative, and just toggles it when zero or
333 When the hungry-delete-key feature is enabled (indicated by \"/h\" on
334 the modeline after the mode name) the delete key gobbles all preceding
335 whitespace in one fell swoop."
337 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
339 (c-keep-region-active))
341 (defun c-toggle-auto-hungry-state (&optional arg)
342 "Toggle auto-newline and hungry-delete-key features.
343 Optional numeric ARG, if supplied, turns on auto-newline and
344 hungry-delete when positive, turns them off when negative, and just
345 toggles them when zero or left out.
347 See `c-toggle-auto-newline' and `c-toggle-hungry-state' for details."
349 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
350 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
352 (c-keep-region-active))
354 (defun c-toggle-electric-state (&optional arg)
355 "Toggle the electric indentation feature.
356 Optional numeric ARG, if supplied, turns on electric indentation when
357 positive, turns it off when negative, and just toggles it when zero or
360 (setq c-electric-flag (c-calculate-state arg c-electric-flag))
362 (c-keep-region-active))
367 (defun c-electric-backspace (arg)
368 "Delete the preceding character or whitespace.
369 If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
370 line) then all preceding whitespace is consumed. If however a prefix
371 argument is supplied, or `c-hungry-delete-key' is nil, or point is
372 inside a literal then the function in the variable
373 `c-backspace-function' is called."
375 (if (c-save-buffer-state ()
376 (or (not c-hungry-delete-key)
379 (funcall c-backspace-function (prefix-numeric-value arg))
380 (c-hungry-delete-backwards)))
382 (defun c-hungry-delete-backwards ()
383 "Delete the preceding character or all preceding whitespace
384 back to the previous non-whitespace character.
385 See also \\[c-hungry-delete-forward]."
387 (let ((here (point)))
389 (if (/= (point) here)
390 (delete-region (point) here)
391 (funcall c-backspace-function 1))))
393 (defalias 'c-hungry-backspace 'c-hungry-delete-backwards)
395 (defun c-electric-delete-forward (arg)
396 "Delete the following character or whitespace.
397 If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
398 line) then all following whitespace is consumed. If however a prefix
399 argument is supplied, or `c-hungry-delete-key' is nil, or point is
400 inside a literal then the function in the variable `c-delete-function'
403 (if (c-save-buffer-state ()
404 (or (not c-hungry-delete-key)
407 (funcall c-delete-function (prefix-numeric-value arg))
408 (c-hungry-delete-forward)))
410 (defun c-hungry-delete-forward ()
411 "Delete the following character or all following whitespace
412 up to the next non-whitespace character.
413 See also \\[c-hungry-delete-backwards]."
415 (let ((here (point)))
417 (if (/= (point) here)
418 (delete-region (point) here)
419 (funcall c-delete-function 1))))
421 ;; This function is only used in XEmacs.
422 (defun c-electric-delete (arg)
423 "Deletes preceding or following character or whitespace.
424 This function either deletes forward as `c-electric-delete-forward' or
425 backward as `c-electric-backspace', depending on the configuration: If
426 the function `delete-forward-p' is defined and returns non-nil, it
427 deletes forward. Otherwise it deletes backward.
429 Note: This is the way in XEmacs to choose the correct action for the
430 \[delete] key, whichever key that means. Other flavors don't use this
431 function to control that."
433 (if (and (fboundp 'delete-forward-p)
435 (c-electric-delete-forward arg)
436 (c-electric-backspace arg)))
438 ;; This function is only used in XEmacs.
439 (defun c-hungry-delete ()
440 "Delete a non-whitespace char, or all whitespace up to the next non-whitespace char.
441 The direction of deletion depends on the configuration: If the
442 function `delete-forward-p' is defined and returns non-nil, it deletes
443 forward using `c-hungry-delete-forward'. Otherwise it deletes
444 backward using `c-hungry-backspace'.
446 Note: This is the way in XEmacs to choose the correct action for the
447 \[delete] key, whichever key that means. Other flavors don't use this
448 function to control that."
450 (if (and (fboundp 'delete-forward-p)
452 (c-hungry-delete-forward)
453 (c-hungry-delete-backwards)))
455 (defun c-electric-pound (arg)
457 If `c-electric-flag' is set, handle it specially according to the variable
458 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if point is
459 inside a literal or a macro, nothing special happens."
461 (if (c-save-buffer-state ()
463 (not c-electric-flag)
464 (not (memq 'alignleft c-electric-pound-behavior))
466 (skip-chars-backward " \t")
469 (and (= (forward-line -1) 0)
471 (eq (char-before) ?\\))))
473 ;; do nothing special
474 (self-insert-command (prefix-numeric-value arg))
475 ;; place the pound character at the left edge
476 (let ((pos (- (point-max) (point)))
479 (delete-horizontal-space)
480 (insert last-command-event)
482 (goto-char (- (point-max) pos)))
485 (defun c-point-syntax ()
486 ;; Return the syntactic context of the construct at point. (This is NOT
487 ;; nec. the same as the s.c. of the line point is on). N.B. This won't work
488 ;; between the `#' of a cpp thing and what follows (see c-opt-cpp-prefix).
489 (c-save-buffer-state (;; shut this up too
490 (c-echo-syntactic-information-p nil)
492 (c-tentative-buffer-changes
493 ;; insert a newline to isolate the construct at point for syntactic
496 ;; In AWK (etc.) or in a macro, make sure this CR hasn't changed
497 ;; the syntax. (There might already be an escaped NL there.)
498 (when (or (c-at-vsemi-p (1- (point)))
502 (and (c-beginning-of-macro)
503 (progn (c-end-of-macro)
508 (let ((c-syntactic-indentation-in-macros t)
509 (c-auto-newline-analysis t))
510 ;; Turn on syntactic macro analysis to help with auto
512 (setq syntax (c-guess-basic-syntax))
516 (defun c-brace-newlines (syntax)
517 ;; A brace stands at point. SYNTAX is the syntactic context of this brace
518 ;; (not necessarily the same as the S.C. of the line it is on). Return
519 ;; NEWLINES, the list containing some combination of the symbols `before'
520 ;; and `after' saying where newlines should be inserted.
523 ;; This is the list of brace syntactic symbols that can hang.
524 ;; If any new ones are added to c-offsets-alist, they should be
525 ;; added here as well.
527 ;; The order of this list is important; if SYNTAX has several
528 ;; elements, the element that "wins" is the earliest in SYMS.
529 '(arglist-cont-nonempty ; e.g. an array literal.
530 class-open class-close defun-open defun-close
531 inline-open inline-close
532 brace-list-open brace-list-close
533 brace-list-intro brace-entry-open
534 block-open block-close
535 substatement-open statement-case-open
536 extern-lang-open extern-lang-close
537 namespace-open namespace-close
538 module-open module-close
539 composition-open composition-close
540 inexpr-class-open inexpr-class-close
541 ;; `statement-cont' is here for the case with a brace
542 ;; list opener inside a statement. C.f. CASE B.2 in
543 ;; `c-guess-continued-construct'.
546 (c-echo-syntactic-information-p nil)
547 symb-newlines) ; e.g. (substatement-open . (after))
550 ;; Do not try to insert newlines around a special
551 ;; (Pike-style) brace list.
552 (if (and c-special-brace-lists
554 (c-safe (if (= (char-before) ?{)
557 (c-looking-at-special-brace-list))))
559 ;; Seek the matching entry in c-hanging-braces-alist.
562 ;; Substitute inexpr-class and class-open or
563 ;; class-close with inexpr-class-open or
564 ;; inexpr-class-close.
565 (if (assq 'inexpr-class syntax)
566 (cond ((assq 'class-open syntax)
567 '((inexpr-class-open)))
568 ((assq 'class-close syntax)
569 '((inexpr-class-close)))
572 c-hanging-braces-alist)
573 '(ignore before after)))) ; Default, when not in c-h-b-l.
575 ;; If syntax is a function symbol, then call it using the
576 ;; defined semantics.
577 (if (and (not (consp (cdr symb-newlines)))
578 (functionp (cdr symb-newlines)))
579 (let ((c-syntactic-context syntax))
580 (funcall (cdr symb-newlines)
583 (cdr symb-newlines))))
585 (defun c-try-one-liner ()
586 ;; Point is just after a newly inserted }. If the non-whitespace
587 ;; content of the braces is a single line of code, compact the whole
588 ;; construct to a single line, if this line isn't too long. The Right
589 ;; Thing is done with comments.
591 ;; Point will be left after the }, regardless of whether the clean-up is
592 ;; done. Return NON-NIL if the clean-up happened, NIL if it didn't.
595 (pos (- (point-max) (point)))
596 mbeg1 mend1 mbeg4 mend4
597 eol-col cmnt-pos cmnt-col cmnt-gap)
602 ;; Avoid backtracking over a very large block. The one we
603 ;; deal with here can never be more than three lines.
604 (narrow-to-region (save-excursion
608 (and (c-safe (c-backward-sexp))
611 (narrow-to-region (point) (1- here)) ; innards of {.}
613 (cc-eval-when-compile
615 "\\(" ; (match-beginning 1)
616 "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
617 "\\)" ; (match-end 1)
618 "[^ \t\r\n]+\\([ \t]+[^ \t\r\n]+\\)*" ; non-WS
619 "\\(" ; (match-beginning 4)
620 "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
621 "\\)\\'"))))))) ; (match-end 4) at EOB.
623 (if (c-tentative-buffer-changes
624 (setq mbeg1 (match-beginning 1) mend1 (match-end 1)
625 mbeg4 (match-beginning 4) mend4 (match-end 4))
626 (backward-char) ; back over the `}'
628 (setq cmnt-pos (and (c-backward-single-comment)
629 (- (point) (- mend1 mbeg1)))))
630 (delete-region mbeg4 mend4)
631 (delete-region mbeg1 mend1)
632 (setq eol-col (save-excursion (end-of-line) (current-column)))
634 ;; Necessary to put the closing brace before any line
635 ;; oriented comment to keep it syntactically significant.
636 ;; This isn't necessary for block comments, but the result
637 ;; looks nicer anyway.
639 (delete-char 1) ; the `}' has blundered into a comment
641 (setq cmnt-col (1+ (current-column)))
642 (setq cmnt-pos (1+ cmnt-pos)) ; we're inserting a `}'
644 (insert-char ?\} 1) ; reinsert the `}' before the comment.
645 (setq cmnt-gap (- cmnt-col (current-column)))
646 (when (zerop cmnt-gap)
647 (insert-char ?\ 1) ; Put a space before a bare comment.
650 (or (null c-max-one-liner-length)
651 (zerop c-max-one-liner-length)
652 (<= eol-col c-max-one-liner-length)
653 ;; Can we trim space before comment to make the line fit?
655 (< (- eol-col cmnt-gap) c-max-one-liner-length)
656 (progn (goto-char cmnt-pos)
657 (backward-delete-char-untabify
658 (- eol-col c-max-one-liner-length))
660 (goto-char (- (point-max) pos))))))
662 (defun c-electric-brace (arg)
665 If `c-electric-flag' is non-nil, the brace is not inside a literal and a
666 numeric ARG hasn't been supplied, the command performs several electric
669 \(a) If the auto-newline feature is turned on (indicated by \"/la\" on
670 the mode line) newlines are inserted before and after the brace as
671 directed by the settings in `c-hanging-braces-alist'.
673 \(b) Any auto-newlines are indented. The original line is also
674 reindented unless `c-syntactic-indentation' is nil.
676 \(c) If auto-newline is turned on, various newline cleanups based on the
677 settings of `c-cleanup-list' are done."
680 (let (safepos literal
681 ;; We want to inhibit blinking the paren since this would be
682 ;; most disruptive. We'll blink it ourselves later on.
683 (old-blink-paren blink-paren-function)
684 blink-paren-function)
686 (c-save-buffer-state ()
687 (setq safepos (c-safe-position (point) (c-parse-state))
688 literal (c-in-literal safepos)))
690 ;; Insert the brace. Note that expand-abbrev might reindent
691 ;; the line here if there's a preceding "else" or something.
692 (self-insert-command (prefix-numeric-value arg))
694 (when (and c-electric-flag (not literal) (not arg))
695 (if (not (looking-at "[ \t]*\\\\?$"))
696 (if c-syntactic-indentation
697 (indent-according-to-mode))
699 (let ( ;; shut this up too
700 (c-echo-syntactic-information-p nil)
702 ln-syntax br-syntax syntax) ; Syntactic context of the original line,
703 ; of the brace itself, of the line the brace ends up on.
704 (c-save-buffer-state ((c-syntactic-indentation-in-macros t)
705 (c-auto-newline-analysis t))
706 (setq ln-syntax (c-guess-basic-syntax)))
707 (if c-syntactic-indentation
708 (c-indent-line ln-syntax))
712 (setq br-syntax (c-point-syntax)
713 newlines (c-brace-newlines br-syntax))
715 ;; Insert the BEFORE newline, if wanted, and reindent the newline.
716 (if (and (memq 'before newlines)
717 (> (current-column) (current-indentation)))
718 (if c-syntactic-indentation
719 ;; Only a plain newline for now - it's indented
720 ;; after the cleanups when the line has its final
723 (c-newline-and-indent)))
726 ;; `syntax' is the syntactic context of the line which ends up
727 ;; with the brace on it.
728 (setq syntax (if (memq 'before newlines) br-syntax ln-syntax))
730 ;; Do all appropriate clean ups
732 (pos (- (point-max) (point)))
736 ;; `}': clean up empty defun braces
737 (when (c-save-buffer-state ()
738 (and (memq 'empty-defun-braces c-cleanup-list)
739 (eq last-command-event ?\})
740 (c-intersect-lists '(defun-close class-close inline-close)
745 (eq (char-before) ?\{))
746 ;; make sure matching open brace isn't in a comment
747 (not (c-in-literal))))
748 (delete-region (point) (1- here))
749 (setq here (- (point-max) pos)))
752 ;; `}': compact to a one-liner defun?
755 (and (eq last-command-event ?\})
756 (memq 'one-liner-defun c-cleanup-list)
757 (c-intersect-lists '(defun-close) syntax)
759 (setq here (- (point-max) pos))))
761 ;; `{': clean up brace-else-brace and brace-elseif-brace
762 (when (eq last-command-event ?\{)
764 ((and (memq 'brace-else-brace c-cleanup-list)
767 "\\([ \t\n]\\|\\\\\n\\)*"
769 "\\([ \t\n]\\|\\\\\n\\)*"
773 (delete-region (match-beginning 0) (match-end 0))
774 (insert-and-inherit "} else {"))
775 ((and (memq 'brace-elseif-brace c-cleanup-list)
777 (goto-char (1- here))
781 (eq (char-before) ?\)))
782 (zerop (c-save-buffer-state nil (c-backward-token-2 1 t)))
783 (eq (char-after) ?\()
788 "\\([ \t\n]\\|\\\\\n\\)*"
790 "\\([ \t\n]\\|\\\\\n\\)+"
792 "\\([ \t\n]\\|\\\\\n\\)*"
795 ;(eq (match-end 0) tmp);
797 (delete-region mbeg mend)
801 (goto-char (- (point-max) pos))
803 ;; Indent the line after the cleanups since it might
804 ;; very well indent differently due to them, e.g. if
805 ;; c-indent-one-line-block is used together with the
806 ;; one-liner-defun cleanup.
807 (when c-syntactic-indentation
810 ;; does a newline go after the brace?
811 (if (memq 'after newlines)
812 (c-newline-and-indent))
816 (and (eq last-command-event ?\})
817 (not executing-kbd-macro)
820 (c-save-buffer-state nil
821 (c-backward-syntactic-ws safepos))
822 (funcall old-blink-paren)))))
824 (defun c-electric-slash (arg)
825 "Insert a slash character.
827 If the slash is inserted immediately after the comment prefix in a c-style
828 comment, the comment might get closed by removing whitespace and possibly
829 inserting a \"*\". See the variable `c-cleanup-list'.
831 Indent the line as a comment, if:
833 1. The slash is second of a \"//\" line oriented comment introducing
834 token and we are on a comment-only-line, or
836 2. The slash is part of a \"*/\" token that closes a block oriented
839 If a numeric ARG is supplied, point is inside a literal, or
840 `c-syntactic-indentation' is nil or `c-electric-flag' is nil, indentation
843 (let ((literal (c-save-buffer-state () (c-in-literal)))
846 (c-echo-syntactic-information-p nil))
848 ;; comment-close-slash cleanup? This DOESN'T need `c-electric-flag' or
849 ;; `c-syntactic-indentation' set.
852 (memq 'comment-close-slash c-cleanup-list)
853 (eq last-command-event ?/)
854 (looking-at (concat "[ \t]*\\("
855 (regexp-quote comment-end) "\\)?$"))
856 ; (eq c-block-comment-ender "*/") ; C-style comments ALWAYS end in */
859 (narrow-to-region (point-min) (point))
860 (back-to-indentation)
861 (looking-at (concat c-current-comment-prefix "[ \t]*$")))))
862 (delete-region (progn (forward-line 0) (point))
863 (progn (end-of-line) (point)))
864 (insert-char ?* 1)) ; the / comes later. ; Do I need a t (retain sticky properties) here?
866 (setq indentp (and (not arg)
867 c-syntactic-indentation
869 (eq last-command-event ?/)
870 (eq (char-before) (if literal ?* ?/))))
871 (self-insert-command (prefix-numeric-value arg))
873 (indent-according-to-mode))))
875 (defun c-electric-star (arg)
876 "Insert a star character.
877 If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, and
878 the star is the second character of a C style comment starter on a
879 comment-only-line, indent the line as a comment. If a numeric ARG is
880 supplied, point is inside a literal, or `c-syntactic-indentation' is nil,
881 this indentation is inhibited."
884 (self-insert-command (prefix-numeric-value arg))
885 ;; if we are in a literal, or if arg is given do not reindent the
886 ;; current line, unless this star introduces a comment-only line.
887 (if (c-save-buffer-state ()
888 (and c-syntactic-indentation
891 (eq (c-in-literal) 'c)
892 (eq (char-before) ?*)
895 (skip-chars-backward "*")
896 (if (eq (char-before) ?/)
898 (skip-chars-backward " \t")
900 (let (c-echo-syntactic-information-p) ; shut this up
901 (indent-according-to-mode))
904 (defun c-electric-semi&comma (arg)
905 "Insert a comma or semicolon.
907 If `c-electric-flag' is non-nil, point isn't inside a literal and a
908 numeric ARG hasn't been supplied, the command performs several electric
911 \(a) When the auto-newline feature is turned on (indicated by \"/la\" on
912 the mode line) a newline might be inserted. See the variable
913 `c-hanging-semi&comma-criteria' for how newline insertion is determined.
915 \(b) Any auto-newlines are indented. The original line is also
916 reindented unless `c-syntactic-indentation' is nil.
918 \(c) If auto-newline is turned on, a comma following a brace list or a
919 semicolon following a defun might be cleaned up, depending on the
920 settings of `c-cleanup-list'."
922 (let* (lim literal c-syntactic-context
925 (c-echo-syntactic-information-p nil))
927 (c-save-buffer-state ()
928 (setq lim (c-most-enclosing-brace (c-parse-state))
929 literal (c-in-literal lim)))
931 (self-insert-command (prefix-numeric-value arg))
933 (if (and c-electric-flag (not literal) (not arg))
934 ;; do all cleanups and newline insertions if c-auto-newline is on.
935 (if (or (not c-auto-newline)
936 (not (looking-at "[ \t]*\\\\?$")))
937 (if c-syntactic-indentation
939 ;; clean ups: list-close-comma or defun-close-semi
940 (let ((pos (- (point-max) (point))))
941 (if (c-save-buffer-state ()
943 (eq last-command-event ?,)
944 (memq 'list-close-comma c-cleanup-list))
946 (eq last-command-event ?\;)
947 (memq 'defun-close-semi c-cleanup-list)))
951 (eq (char-before) ?}))
952 ;; make sure matching open brace isn't in a comment
953 (not (c-in-literal lim))))
954 (delete-region (point) here))
955 (goto-char (- (point-max) pos)))
957 (when c-syntactic-indentation
958 (setq c-syntactic-context (c-guess-basic-syntax))
959 (c-indent-line c-syntactic-context))
960 ;; check to see if a newline should be added
961 (let ((criteria c-hanging-semi&comma-criteria)
962 answer add-newline-p)
964 (setq answer (funcall (car criteria)))
965 ;; only nil value means continue checking
967 (setq criteria (cdr criteria))
969 ;; only 'stop specifically says do not add a newline
970 (setq add-newline-p (not (eq answer 'stop)))
973 (c-newline-and-indent))
976 (defun c-electric-colon (arg)
979 If `c-electric-flag' is non-nil, the colon is not inside a literal and a
980 numeric ARG hasn't been supplied, the command performs several electric
983 \(a) If the auto-newline feature is turned on (indicated by \"/la\" on
984 the mode line) newlines are inserted before and after the colon based on
985 the settings in `c-hanging-colons-alist'.
987 \(b) Any auto-newlines are indented. The original line is also
988 reindented unless `c-syntactic-indentation' is nil.
990 \(c) If auto-newline is turned on, whitespace between two colons will be
991 \"cleaned up\" leaving a scope operator, if this action is set in
995 (let* ((bod (c-point 'bod))
996 (literal (c-save-buffer-state () (c-in-literal bod)))
999 (c-echo-syntactic-information-p nil))
1000 (self-insert-command (prefix-numeric-value arg))
1001 ;; Any electric action?
1002 (if (and c-electric-flag (not literal) (not arg))
1003 ;; Unless we're at EOL, only re-indentation happens.
1004 (if (not (looking-at "[ \t]*\\\\?$"))
1005 (if c-syntactic-indentation
1006 (indent-according-to-mode))
1008 ;; scope-operator clean-up?
1009 (let ((pos (- (point-max) (point)))
1011 (if (c-save-buffer-state () ; Why do we need this? [ACM, 2003-03-12]
1013 (memq 'scope-operator c-cleanup-list)
1014 (eq (char-before) ?:)
1017 (c-skip-ws-backward)
1018 (eq (char-before) ?:))
1019 (not (c-in-literal))
1020 (not (eq (char-after (- (point) 2)) ?:))))
1022 (delete-region (point) (1- here))
1023 (setq is-scope-op t)))
1024 (goto-char (- (point-max) pos)))
1026 ;; indent the current line if it's done syntactically.
1027 (if c-syntactic-indentation
1028 ;; Cannot use the same syntax analysis as we find below,
1029 ;; since that's made with c-syntactic-indentation-in-macros
1031 (indent-according-to-mode))
1033 ;; Calculate where, if anywhere, we want newlines.
1034 (c-save-buffer-state
1035 ((c-syntactic-indentation-in-macros t)
1036 (c-auto-newline-analysis t)
1037 ;; Turn on syntactic macro analysis to help with auto newlines
1039 (syntax (c-guess-basic-syntax))
1041 ;; Translate substatement-label to label for this operation.
1043 (if (eq (car (car elem)) 'substatement-label)
1044 (setcar (car elem) 'label))
1045 (setq elem (cdr elem)))
1046 ;; some language elements can only be determined by checking
1047 ;; the following line. Lets first look for ones that can be
1048 ;; found when looking on the line with the colon
1051 (or (c-lookup-lists '(case-label label access-label)
1052 syntax c-hanging-colons-alist)
1053 (c-lookup-lists '(member-init-intro inher-intro)
1057 (c-guess-basic-syntax)
1059 c-hanging-colons-alist)))))
1060 ;; does a newline go before the colon? Watch out for already
1061 ;; non-hung colons. However, we don't unhang them because that
1062 ;; would be a cleanup (and anti-social).
1063 (if (and (memq 'before newlines)
1066 (skip-chars-backward ": \t")
1068 (let ((pos (- (point-max) (point))))
1070 (c-newline-and-indent)
1071 (goto-char (- (point-max) pos))))
1072 ;; does a newline go after the colon?
1073 (if (and (memq 'after (cdr-safe newlines))
1075 (c-newline-and-indent))
1078 (defun c-electric-lt-gt (arg)
1079 "Insert a \"<\" or \">\" character.
1080 If the current language uses angle bracket parens (e.g. template
1081 arguments in C++), try to find out if the inserted character is a
1082 paren and give it paren syntax if appropriate.
1084 If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, the
1085 line will be reindented if the inserted character is a paren or if it
1086 finishes a C++ style stream operator in C++ mode. Exceptions are when a
1087 numeric argument is supplied, or the point is inside a literal."
1090 (let ((c-echo-syntactic-information-p nil)
1091 final-pos close-paren-inserted)
1093 (self-insert-command (prefix-numeric-value arg))
1094 (setq final-pos (point))
1096 (c-save-buffer-state (c-parse-and-markup-<>-arglists
1097 c-restricted-<>-arglists
1100 (when c-recognize-<>-arglists
1101 (if (eq last-command-event ?<)
1106 (c-beginning-of-current-token)
1109 (c-backward-token-2)
1110 (looking-at c-opt-<>-sexp-key)))
1111 (c-mark-<-as-paren (1- final-pos)))
1113 ;; It's a ">". Check if there's an earlier "<" which either has
1114 ;; open paren syntax already or that can be recognized as an arglist
1115 ;; together with this ">". Note that this won't work in cases like
1116 ;; "template <x, a < b, y>" but they ought to be rare.
1119 ;; Narrow to avoid that `c-forward-<>-arglist' below searches past
1121 (narrow-to-region (point-min) final-pos)
1125 (goto-char final-pos)
1126 (c-syntactic-skip-backward "^<;}" nil t)
1127 (eq (char-before) ?<))
1130 ;; If the "<" already got open paren syntax we know we
1131 ;; have the matching closer. Handle it and exit the
1133 (if (looking-at "\\s\(")
1135 (c-mark->-as-paren (1- final-pos))
1136 (setq close-paren-inserted t)
1141 (setq <-pos (point))
1142 (c-backward-syntactic-ws)
1143 (c-simple-skip-symbol-backward))
1144 (or (looking-at c-opt-<>-sexp-key)
1145 (not (looking-at c-keywords-regexp)))
1147 (let ((c-parse-and-markup-<>-arglists t)
1148 c-restricted-<>-arglists
1150 (c-most-enclosing-brace (c-parse-state))))
1151 (when (and containing-sexp
1152 (progn (goto-char containing-sexp)
1153 (eq (char-after) ?\())
1154 (not (eq (get-text-property (point) 'c-type)
1155 'c-decl-arg-start)))
1156 (setq c-restricted-<>-arglists t))
1158 (c-forward-<>-arglist nil))
1160 ;; Loop here if the "<" we found above belongs to a nested
1161 ;; angle bracket sexp. When we start over we'll find the
1162 ;; previous or surrounding sexp.
1163 (if (< (point) final-pos)
1165 (setq close-paren-inserted t)
1167 (goto-char final-pos)
1169 ;; Indent the line if appropriate.
1170 (when (and c-electric-flag c-syntactic-indentation)
1172 (when (prog1 (or (looking-at "\\s\(\\|\\s\)")
1173 (and (c-major-mode-is 'c++-mode)
1175 (c-beginning-of-current-token)
1176 (looking-at "<<\\|>>"))
1177 (= (match-end 0) final-pos)))
1178 (goto-char final-pos))
1179 (indent-according-to-mode)))
1181 (when (and close-paren-inserted
1182 (not executing-kbd-macro)
1183 blink-paren-function)
1184 ;; Note: Most paren blink functions, such as the standard
1185 ;; `blink-matching-open', currently doesn't handle paren chars
1186 ;; marked with text properties very well. Maybe we should avoid
1187 ;; this call for the time being?
1188 (funcall blink-paren-function))))
1190 (defun c-electric-paren (arg)
1191 "Insert a parenthesis.
1193 If `c-syntactic-indentation' and `c-electric-flag' are both non-nil, the
1194 line is reindented unless a numeric ARG is supplied, or the parenthesis
1195 is inserted inside a literal.
1197 Whitespace between a function name and the parenthesis may get added or
1198 removed; see the variable `c-cleanup-list'.
1200 Also, if `c-electric-flag' and `c-auto-newline' are both non-nil, some
1201 newline cleanups are done if appropriate; see the variable `c-cleanup-list'."
1203 (let ((literal (c-save-buffer-state () (c-in-literal)))
1205 (c-echo-syntactic-information-p nil))
1206 (self-insert-command (prefix-numeric-value arg))
1208 (if (and (not arg) (not literal))
1209 (let* ( ;; We want to inhibit blinking the paren since this will
1210 ;; be most disruptive. We'll blink it ourselves
1212 (old-blink-paren blink-paren-function)
1213 blink-paren-function)
1214 (if (and c-syntactic-indentation c-electric-flag)
1215 (indent-according-to-mode))
1217 ;; If we're at EOL, check for new-line clean-ups.
1218 (when (and c-electric-flag c-auto-newline
1219 (looking-at "[ \t]*\\\\?$"))
1221 ;; clean up brace-elseif-brace
1223 (and (memq 'brace-elseif-brace c-cleanup-list)
1224 (eq last-command-event ?\()
1227 "\\([ \t\n]\\|\\\\\n\\)*"
1229 "\\([ \t\n]\\|\\\\\n\\)+"
1231 "\\([ \t\n]\\|\\\\\n\\)*"
1235 (not (c-save-buffer-state () (c-in-literal))))
1236 (delete-region (match-beginning 0) (match-end 0))
1237 (insert-and-inherit "} else if ("))
1239 ;; clean up brace-catch-brace
1241 (and (memq 'brace-catch-brace c-cleanup-list)
1242 (eq last-command-event ?\()
1245 "\\([ \t\n]\\|\\\\\n\\)*"
1247 "\\([ \t\n]\\|\\\\\n\\)*"
1251 (not (c-save-buffer-state () (c-in-literal))))
1252 (delete-region (match-beginning 0) (match-end 0))
1253 (insert-and-inherit "} catch (")))
1255 ;; Check for clean-ups at function calls. These two DON'T need
1256 ;; `c-electric-flag' or `c-syntactic-indentation' set.
1257 ;; Point is currently just after the inserted paren.
1258 (let (beg (end (1- (point))))
1261 ;; space-before-funcall clean-up?
1262 ((and (memq 'space-before-funcall c-cleanup-list)
1263 (eq last-command-event ?\()
1266 (skip-chars-backward " \t")
1268 (and (c-save-buffer-state () (c-on-identifier))
1269 ;; Don't add a space into #define FOO()....
1270 (not (and (c-beginning-of-macro)
1271 (c-forward-over-cpp-define-id)
1272 (eq (point) beg))))))
1274 (delete-region beg end)
1278 ;; compact-empty-funcall clean-up?
1279 ((c-save-buffer-state ()
1280 (and (memq 'compact-empty-funcall c-cleanup-list)
1281 (eq last-command-event ?\))
1283 (c-safe (backward-char 2))
1284 (when (looking-at "()")
1286 (skip-chars-backward " \t")
1288 (c-on-identifier)))))
1289 (delete-region beg end))))
1290 (and (eq last-input-event ?\))
1291 (not executing-kbd-macro)
1293 (funcall old-blink-paren))))))
1295 (defun c-electric-continued-statement ()
1296 "Reindent the current line if appropriate.
1298 This function is used to reindent the line after a keyword which
1299 continues an earlier statement is typed, e.g. an \"else\" or the
1300 \"while\" in a do-while block.
1302 The line is reindented if there is nothing but whitespace before the
1303 keyword on the line, the keyword is not inserted inside a literal, and
1304 `c-electric-flag' and `c-syntactic-indentation' are both non-nil."
1305 (let (;; shut this up
1306 (c-echo-syntactic-information-p nil))
1307 (when (c-save-buffer-state ()
1308 (and c-electric-flag
1309 c-syntactic-indentation
1310 (not (eq last-command-event ?_))
1312 (skip-syntax-backward "w")
1315 (not (c-in-literal (c-point 'bod)))))
1316 ;; Have to temporarily insert a space so that
1317 ;; c-guess-basic-syntax recognizes the keyword. Follow the
1318 ;; space with a nonspace to avoid messing up any whitespace
1319 ;; sensitive meddling that might be done, e.g. by
1320 ;; `c-backslash-region'.
1321 (insert-and-inherit " x")
1323 (indent-according-to-mode)
1324 (delete-char -2)))))
1327 ;; "nomenclature" functions + c-scope-operator.
1328 (defun c-forward-into-nomenclature (&optional arg)
1329 "Compatibility alias for `c-forward-subword'."
1331 (require 'cc-subword)
1332 (c-forward-subword arg))
1333 (make-obsolete 'c-forward-into-nomenclature 'c-forward-subword)
1335 (defun c-backward-into-nomenclature (&optional arg)
1336 "Compatibility alias for `c-backward-subword'."
1338 (require 'cc-subword)
1339 (c-backward-subword arg))
1340 (make-obsolete 'c-backward-into-nomenclature 'c-backward-subword)
1342 (defun c-scope-operator ()
1343 "Insert a double colon scope operator at point.
1344 No indentation or other \"electric\" behavior is performed."
1346 (insert-and-inherit "::"))
1349 ;; Movement (etc.) by defuns.
1350 (defun c-in-function-trailer-p (&optional lim)
1351 ;; Return non-nil if point is between the closing brace and the semicolon of
1352 ;; a brace construct which needs a semicolon, e.g. within the "variables"
1353 ;; portion of a declaration like "struct foo {...} bar ;".
1355 ;; Return the position of the main declaration. Otherwise, return nil.
1356 ;; Point is assumed to be at the top level and outside of any macro or
1359 ;; If LIM is non-nil, it is the bound on a the backward search for the
1360 ;; beginning of the declaration.
1362 ;; This function might do hidden buffer changes.
1363 (and c-opt-block-decls-with-vars-key
1365 (c-syntactic-skip-backward "^;}" lim)
1366 (let ((eo-block (point))
1368 (and (eq (char-before) ?\})
1369 (eq (car (c-beginning-of-decl-1 lim)) 'previous)
1371 ;; Look for struct or union or ... If we find one, it might
1372 ;; be the return type of a function, or the like. Exclude
1374 (c-syntactic-re-search-forward
1375 (concat "[;=\(\[{]\\|\\("
1376 c-opt-block-decls-with-vars-key
1379 (match-beginning 1) ; Is there a "struct" etc., somewhere?
1380 (not (eq (char-before) ?_))
1381 (c-syntactic-re-search-forward "[;=\(\[{]" eo-block t t t)
1382 (eq (char-before) ?\{)
1385 (defun c-where-wrt-brace-construct ()
1386 ;; Determine where we are with respect to functions (or other brace
1387 ;; constructs, included in the term "function" in the rest of this comment).
1388 ;; Point is assumed to be outside any macro or literal.
1389 ;; This is used by c-\(begining\|end\)-of-defun.
1391 ;; Return one of these symbols:
1392 ;; at-header : we're at the start of a function's header.
1393 ;; in-header : we're inside a function's header, this extending right
1394 ;; up to the brace. This bit includes any k&r declarations.
1395 ;; in-block : we're inside a function's brace block.
1396 ;; in-trailer : we're in the area between the "}" and ";" of something
1397 ;; like "struct foo {...} bar, baz;".
1398 ;; at-function-end : we're just after the closing brace (or semicolon) that
1399 ;; terminates the function.
1400 ;; outwith-function: we're not at or in any function. Being inside a
1401 ;; non-brace construct also counts as 'outwith-function'.
1403 ;; This function might do hidden buffer changes.
1406 decl-result brace-decl-p
1408 (paren-state (c-parse-state))
1409 (least-enclosing (c-least-enclosing-brace paren-state)))
1412 ((and least-enclosing
1413 (eq (char-after least-enclosing) ?\{))
1415 ((c-in-function-trailer-p)
1417 ((and (not least-enclosing)
1419 (consp (car paren-state))
1420 (eq start (cdar paren-state)))
1423 ;; Find the start of the current declaration. NOTE: If we're in the
1424 ;; variables after a "struct/eval" type block, we don't get to the
1425 ;; real declaration here - we detect and correct for this later.
1427 ;;If we're in the parameters' parens, move back out of them.
1428 (if least-enclosing (goto-char least-enclosing))
1429 ;; Kluge so that c-beginning-of-decl-1 won't go back if we're already
1430 ;; at a declaration.
1431 (if (or (and (eolp) (not (eobp))) ; EOL is matched by "\\s>"
1433 "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\|\\s!\\)")))
1435 (setq kluge-start (point))
1437 (car (c-beginning-of-decl-1
1438 ;; NOTE: If we're in a K&R region, this might be the start
1439 ;; of a parameter declaration, not the actual function.
1440 (and least-enclosing ; LIMIT for c-b-of-decl-1
1441 (c-safe-position least-enclosing paren-state)))))
1443 ;; Has the declaration we've gone back to got braces?
1446 (and (c-syntactic-re-search-forward "[;{]" nil t t)
1447 (or (eq (char-before) ?\{)
1448 (and c-recognize-knr-p
1449 ;; Might have stopped on the
1450 ;; ';' in a K&R argdecl. In
1451 ;; that case the declaration
1452 ;; should contain a block.
1453 (c-in-knr-argdecl))))))
1456 ((= (point) kluge-start) ; might be BOB or unbalanced parens.
1458 ((eq decl-result 'same)
1460 (if (eq (point) start)
1464 ((eq decl-result 'previous)
1465 (if (and (not brace-decl-p)
1466 (c-in-function-trailer-p))
1470 "c-where-wrt-brace-construct: c-beginning-of-decl-1 returned %s"
1473 (defun c-backward-to-nth-BOF-{ (n where)
1474 ;; Skip to the opening brace of the Nth function before point. If
1475 ;; point is inside a function, this counts as the first. Point must be
1476 ;; outside any comment/string or macro.
1478 ;; N must be strictly positive.
1479 ;; WHERE describes the position of point, one of the symbols `at-header',
1480 ;; `in-header', `in-block', `in-trailer', `at-function-end',
1481 ;; `outwith-function' as returned by c-where-wrt-brace-construct.
1483 ;; If we run out of functions, leave point at BOB. Return zero on success,
1484 ;; otherwise the number of {s still to go.
1486 ;; This function may do hidden buffer changes
1488 ;; What we do to go back the first defun depends on where we start.
1490 ((eq where 'in-block)
1491 (goto-char (c-least-enclosing-brace (c-parse-state)))
1493 ((eq where 'in-header)
1494 (c-syntactic-re-search-forward "{")
1497 ((memq where '(at-header outwith-function at-function-end in-trailer))
1498 (c-syntactic-skip-backward "^}")
1499 (when (eq (char-before) ?\})
1502 (t (error "Unknown `where' %s in c-backward-to-nth-EOF-{" where)))
1504 ;; Each time round the loop, go back to a "{" at the outermost level.
1505 (while (and (> n 0) (not (bobp)))
1506 (c-parse-state) ; This call speeds up the following one
1507 ; by a factor of ~6. Hmmm. 2006/4/5.
1508 (c-syntactic-skip-backward "^}")
1509 (when (eq (char-before) ?\})
1514 (defun c-beginning-of-defun (&optional arg)
1515 "Move backward to the beginning of a defun.
1516 Every top level declaration that contains a brace paren block is
1517 considered to be a defun.
1519 With a positive argument, move backward that many defuns. A negative
1520 argument -N means move forward to the Nth following beginning. Return
1521 t unless search stops due to beginning or end of buffer.
1523 Unlike the built-in `beginning-of-defun' this tries to be smarter
1524 about finding the char with open-parenthesis syntax that starts the
1528 (or arg (setq arg 1))
1530 (c-save-buffer-state
1531 (beginning-of-defun-function end-of-defun-function
1533 where paren-state pos)
1535 ;; Move back out of any macro/comment/string we happen to be in.
1536 (c-beginning-of-macro)
1537 (setq pos (c-literal-limits))
1538 (if pos (goto-char (car pos)))
1540 (setq where (c-where-wrt-brace-construct))
1543 ;; Move forward to the closing brace of a function.
1545 (if (memq where '(at-function-end outwith-function))
1546 (setq arg (1+ arg)))
1548 (setq arg (c-forward-to-nth-EOF-} (- arg) where)))
1549 ;; Move forward to the next opening brace....
1550 (when (and (= arg 0)
1551 (c-syntactic-re-search-forward "{" nil 'eob))
1553 ;; ... and backward to the function header.
1554 (c-beginning-of-decl-1)
1557 ;; Move backward to the opening brace of a function.
1558 (when (and (> arg 0)
1559 (eq (setq arg (c-backward-to-nth-BOF-{ arg where)) 0))
1561 ;; Go backward to this function's header.
1562 (c-beginning-of-decl-1)
1565 ;; We're now there, modulo comments and whitespace.
1566 ;; Try to be line oriented; position point at the closest
1567 ;; preceding boi that isn't inside a comment, but if we hit
1568 ;; the previous declaration then we use the current point
1570 (while (and (/= (point) (c-point 'boi))
1571 (c-backward-single-comment)))
1572 (if (/= (point) (c-point 'boi))
1575 (c-keep-region-active)
1578 (defun c-forward-to-nth-EOF-} (n where)
1579 ;; Skip to the closing brace of the Nth function after point. If
1580 ;; point is inside a function, this counts as the first. Point must be
1581 ;; outside any comment/string or macro.
1583 ;; N must be strictly positive.
1584 ;; WHERE describes the position of point, one of the symbols `at-header',
1585 ;; `in-header', `in-block', `in-trailer', `at-function-end',
1586 ;; `outwith-function' as returned by c-where-wrt-brace-construct.
1588 ;; If we run out of functions, leave point at EOB. Return zero on success,
1589 ;; otherwise the number of }s still to go.
1591 ;; This function may do hidden buffer changes.
1594 ;; What we do to go forward over the first defun depends on where we
1595 ;; start. We go to the closing brace of that defun, even when we go
1596 ;; backwards to it (in a "struct foo {...} bar ;").
1598 ((eq where 'in-block)
1599 (goto-char (c-least-enclosing-brace (c-parse-state)))
1602 ((eq where 'in-trailer)
1603 (c-syntactic-skip-backward "^}")
1605 ((memq where '(at-function-end outwith-function at-header in-header))
1606 (when (c-syntactic-re-search-forward "{" nil 'eob)
1610 (t (error "c-forward-to-nth-EOF-}: `where' is %s" where)))
1612 ;; Each time round the loop, go forward to a "}" at the outermost level.
1613 (while (and (> n 0) (not (eobp)))
1614 ;(c-parse-state) ; This call speeds up the following one by a factor
1615 ; of ~6. Hmmm. 2006/4/5.
1616 (when (c-syntactic-re-search-forward "{" nil 'eob)
1622 (defun c-end-of-defun (&optional arg)
1623 "Move forward to the end of a top level declaration.
1624 With argument, do it that many times. Negative argument -N means move
1625 back to Nth preceding end. Returns t unless search stops due to
1626 beginning or end of buffer.
1628 An end of a defun occurs right after the close-parenthesis that matches
1629 the open-parenthesis that starts a defun; see `beginning-of-defun'."
1631 (or arg (setq arg 1))
1633 (c-save-buffer-state
1634 (beginning-of-defun-function end-of-defun-function
1636 where paren-state pos)
1638 ;; Move back out of any macro/comment/string we happen to be in.
1639 (c-beginning-of-macro)
1640 (setq pos (c-literal-limits))
1641 (if pos (goto-char (car pos)))
1643 (setq where (c-where-wrt-brace-construct))
1646 ;; Move backwards to the } of a function
1648 (if (memq where '(at-header outwith-function))
1649 (setq arg (1+ arg)))
1651 (setq arg (c-backward-to-nth-BOF-{ (- arg) where)))
1653 (c-syntactic-skip-backward "^}")))
1655 ;; Move forward to the } of a function
1657 (setq arg (c-forward-to-nth-EOF-} arg where))))
1659 ;; Do we need to move forward from the brace to the semicolon?
1661 (if (c-in-function-trailer-p) ; after "}" of struct/enum, etc.
1662 (c-syntactic-re-search-forward ";"))
1665 ;; We're there now, modulo comments and whitespace.
1666 ;; Try to be line oriented; position point after the next
1667 ;; newline that isn't inside a comment, but if we hit the
1668 ;; next declaration then we use the current point instead.
1669 (while (and (not (bolp))
1670 (not (looking-at "\\s *$"))
1671 (c-forward-single-comment)))
1673 ((looking-at "\\s *$")
1678 (c-keep-region-active)
1681 (defun c-defun-name ()
1682 "Return the name of the current defun, or NIL if there isn't one.
1683 \"Defun\" here means a function, or other top level construct
1684 with a brace block."
1686 (c-save-buffer-state
1687 (beginning-of-defun-function end-of-defun-function
1691 ;; Move back out of any macro/comment/string we happen to be in.
1692 (c-beginning-of-macro)
1693 (setq pos (c-literal-limits))
1694 (if pos (goto-char (car pos)))
1696 (setq where (c-where-wrt-brace-construct))
1698 ;; Move to the beginning of the current defun, if any, if we're not
1700 (if (eq where 'outwith-function)
1702 (unless (eq where 'at-header)
1703 (c-backward-to-nth-BOF-{ 1 where)
1704 (c-beginning-of-decl-1))
1706 ;; Pick out the defun name, according to the type of defun.
1708 ;; struct, union, enum, or similar:
1709 ((and (looking-at c-type-prefix-key)
1710 (progn (c-forward-token-2 2) ; over "struct foo "
1711 (or (eq (char-after) ?\{)
1712 (looking-at c-symbol-key)))) ; "struct foo bar ..."
1713 (save-match-data (c-forward-token-2))
1714 (when (eq (char-after) ?\{)
1715 (c-backward-token-2)
1716 (looking-at c-symbol-key))
1717 (match-string-no-properties 0))
1719 ((looking-at "DEFUN\\_>")
1720 ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory
1721 ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK
1723 (c-forward-syntactic-ws)
1724 (when (eq (char-after) ?\")
1726 (c-forward-token-2)) ; over the comma and following WS.
1727 (buffer-substring-no-properties
1731 (when (looking-at ":") ; CLISP: DEFUN(PACKAGE:LISP-SYMBOL,...)
1732 (skip-chars-forward "^,"))
1733 (c-backward-syntactic-ws)
1736 ((looking-at "DEF[a-zA-Z0-9_]* *( *\\([^, ]*\\) *,")
1737 ;; DEFCHECKER(sysconf_arg,prefix=_SC,default=, ...) ==> sysconf_arg
1738 ;; DEFFLAGSET(syslog_opt_flags,LOG_PID ...) ==> syslog_opt_flags
1739 (match-string-no-properties 1))
1742 ;; Normal function or initializer.
1743 (when (c-syntactic-re-search-forward "[{(]" nil t)
1745 (c-backward-syntactic-ws)
1746 (when (eq (char-before) ?\=) ; struct foo bar = {0, 0} ;
1747 (c-backward-token-2)
1748 (c-backward-syntactic-ws))
1749 (setq name-end (point))
1750 (c-backward-token-2)
1751 (buffer-substring-no-properties (point) name-end))))))))
1753 (defun c-declaration-limits (near)
1754 ;; Return a cons of the beginning and end positions of the current
1755 ;; top level declaration or macro. If point is not inside any then
1756 ;; nil is returned, unless NEAR is non-nil in which case the closest
1757 ;; following one is chosen instead (if there is any). The end
1758 ;; position is at the next line, providing there is one before the
1761 ;; This function might do hidden buffer changes.
1764 ;; Note: Some code duplication in `c-beginning-of-defun' and
1765 ;; `c-end-of-defun'.
1767 (let ((start (point))
1768 (paren-state (c-parse-state))
1771 (goto-char (c-least-enclosing-brace paren-state))
1772 ;; If we moved to the outermost enclosing paren then we
1773 ;; can use c-safe-position to set the limit. Can't do
1774 ;; that otherwise since the earlier paren pair on
1775 ;; paren-state might very well be part of the
1776 ;; declaration we should go to.
1777 (setq lim (c-safe-position (point) paren-state))
1779 ;; At top level. Make sure we aren't inside a literal.
1780 (setq pos (c-literal-limits
1781 (c-safe-position (point) paren-state)))
1782 (if pos (goto-char (car pos))))
1784 (when (c-beginning-of-macro)
1793 (when (or (eq (car (c-beginning-of-decl-1 lim)) 'previous)
1795 ;; We moved back over the previous defun. Skip to the next
1796 ;; one. Not using c-forward-syntactic-ws here since we
1797 ;; should not skip a macro. We can also be directly after
1798 ;; the block in a `c-opt-block-decls-with-vars-key'
1799 ;; declaration, but then we won't move significantly far
1802 (c-forward-comments)
1804 (when (and near (c-beginning-of-macro))
1812 (if (eobp) (throw 'exit nil))
1814 ;; Check if `c-beginning-of-decl-1' put us after the block in a
1815 ;; declaration that doesn't end there. We're searching back and
1816 ;; forth over the block here, which can be expensive.
1818 (if (and c-opt-block-decls-with-vars-key
1820 (c-backward-syntactic-ws)
1821 (eq (char-before) ?}))
1822 (eq (car (c-beginning-of-decl-1))
1826 (and (> (point) pos)
1827 (setq end-pos (point)))))
1831 (if (and (not near) (> (point) start))
1834 ;; Try to be line oriented; position the limits at the
1835 ;; closest preceding boi, and after the next newline, that
1836 ;; isn't inside a comment, but if we hit a neighboring
1837 ;; declaration then we instead use the exact declaration
1838 ;; limit in that direction.
1841 (while (and (/= (point) (c-point 'boi))
1842 (c-backward-single-comment)))
1843 (if (/= (point) (c-point 'boi))
1851 (while (and (not (bolp))
1852 (not (looking-at "\\s *$"))
1853 (c-forward-single-comment)))
1856 ((looking-at "\\s *$")
1863 (defun c-mark-function ()
1864 "Put mark at end of the current top-level declaration or macro, point at beginning.
1865 If point is not inside any then the closest following one is chosen.
1867 As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
1868 function does not require the declaration to contain a brace block."
1872 (c-save-buffer-state nil
1873 ;; We try to be line oriented, unless there are several
1874 ;; declarations on the same line.
1875 (if (looking-at c-syntactic-eol)
1876 (c-backward-token-2 1 nil (c-point 'bol)))
1877 (setq decl-limits (c-declaration-limits t)))
1879 (if (not decl-limits)
1880 (error "Cannot find any declaration")
1881 (goto-char (car decl-limits))
1882 (push-mark (cdr decl-limits) nil t))))
1884 (defun c-cpp-define-name ()
1885 "Return the name of the current CPP macro, or NIL if we're not in one."
1888 (and c-opt-cpp-macro-define-start
1889 (c-beginning-of-macro)
1890 (looking-at c-opt-cpp-macro-define-start)
1891 (match-string-no-properties 1))))
1894 ;; Movement by statements.
1895 (defun c-in-comment-line-prefix-p ()
1896 ;; Point is within a comment. Is it also within a comment-prefix?
1897 ;; Space at BOL which precedes a comment-prefix counts as part of it.
1899 ;; This function might do hidden buffer changes.
1900 (let ((here (point)))
1903 (skip-chars-forward " \t")
1904 (and (looking-at c-current-comment-prefix)
1905 (/= (match-beginning 0) (match-end 0))
1906 (< here (match-end 0))))))
1908 (defun c-narrow-to-comment-innards (range)
1909 ;; Narrow to the "inside" of the comment (block) defined by range, as
1912 ;; A c-style block comment has its opening "/*" and its closing "*/" (if
1913 ;; present) removed. A c++-style line comment retains its opening "//" but
1914 ;; has any final NL removed. If POINT is currently outwith these innards,
1915 ;; move it to the appropriate boundary.
1917 ;; This narrowing simplifies the sentence movement functions, since it
1918 ;; eliminates awkward things at the boundaries of the comment (block).
1920 ;; This function might do hidden buffer changes.
1921 (let* ((lit-type (c-literal-type range))
1922 (beg (if (eq lit-type 'c) (+ (car range) 2) (car range)))
1923 (end (if (eq lit-type 'c)
1924 (if (and (eq (char-before (cdr range)) ?/)
1925 (eq (char-before (1- (cdr range))) ?*))
1928 (if (eq (cdr range) (point-max))
1930 (- (cdr range) 1)))))
1932 (goto-char end)) ; This would be done automatically by ...
1934 (goto-char beg)) ; ... narrow-to-region but is not documented.
1935 (narrow-to-region beg end)))
1937 (defun c-beginning-of-sentence-in-comment (range)
1938 ;; Move backwards to the "beginning of a sentence" within the comment
1939 ;; defined by RANGE, a cons of its starting and ending positions. If we
1940 ;; find a BOS, return NIL. Otherwise, move point to just before the start
1941 ;; of the comment and return T.
1943 ;; The BOS is either text which follows a regexp match of sentence-end,
1944 ;; or text which is a beginning of "paragraph".
1945 ;; Comment-prefixes are treated like WS when calculating BOSes or BOPs.
1947 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
1948 ;; It is not a general function, but is intended only for calling from
1949 ;; c-move-over-sentence. Not all preconditions have been explicitly stated.
1951 ;; This function might do hidden buffer changes.
1953 (let ((start-point (point)))
1955 (c-narrow-to-comment-innards range) ; This may move point back.
1956 (let* ((here (point))
1958 (here-filler ; matches WS and comment-prefices at point.
1959 (concat "\\=\\(^[ \t]*\\(" c-current-comment-prefix "\\)"
1960 "\\|[ \t\n\r\f]\\)*"))
1961 (prefix-at-bol-here ; matches WS and prefix at BOL, just before point
1962 (concat "^[ \t]*\\(" c-current-comment-prefix "\\)[ \t\n\r\f]*\\="))
1963 ;; First, find the previous paragraph start, if any.
1964 (par-beg ; point where non-WS/non-prefix text of paragraph starts.
1966 (forward-paragraph -1) ; uses cc-mode values of
1967 ; paragraph-\(start\|separate\)
1968 (if (> (re-search-forward here-filler nil t) here)
1970 (when (>= (point) here)
1971 (forward-paragraph -2)
1972 (if (> (re-search-forward here-filler nil t) here)
1976 ;; Now seek successively earlier sentence ends between PAR-BEG and
1977 ;; HERE, until the "start of sentence" following it is earlier than
1978 ;; HERE, or we hit PAR-BEG. Beware of comment prefices!
1979 (while (and (re-search-backward (c-sentence-end) par-beg 'limit)
1981 (goto-char (match-end 0)) ; tentative beginning of sentence
1982 (or (>= (point) here)
1983 (and (not (bolp)) ; Found a non-blank comment-prefix?
1985 (if (re-search-backward prefix-at-bol-here nil t)
1986 (/= (match-beginning 1) (match-end 1)))))
1987 (progn ; Skip the crud to find a real b-o-s.
1988 (if (c-in-comment-line-prefix-p)
1989 (beginning-of-line))
1990 (re-search-forward here-filler) ; always succeeds.
1991 (>= (point) here))))
1993 (re-search-forward here-filler)))
1995 (if (< (point) start-point)
1997 (goto-char (car range))
2000 (defun c-end-of-sentence-in-comment (range)
2001 ;; Move forward to the "end of a sentence" within the comment defined by
2002 ;; RANGE, a cons of its starting and ending positions (enclosing the opening
2003 ;; comment delimiter and the terminating */ or newline). If we find an EOS,
2004 ;; return NIL. Otherwise, move point to just after the end of the comment
2007 ;; The EOS is just after the non-WS part of the next match of the regexp
2008 ;; sentence-end. Typically, this is just after one of [.!?]. If there is
2009 ;; no sentence-end match following point, any WS before the end of the
2010 ;; comment will count as EOS, providing we're not already in it.
2012 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
2013 ;; It is not a general function, but is intended only for calling from
2014 ;; c-move-over-sentence.
2016 ;; This function might do hidden buffer changes.
2018 (let ((start-point (point))
2019 ;; (lit-type (c-literal-type range)) ; Commented out, 2005/11/23, ACM
2022 (c-narrow-to-comment-innards range) ; This might move point forwards.
2023 (let* ((here (point))
2024 (par-end ; EOL position of last text in current/next paragraph.
2026 ;; The cc-mode values of paragraph-\(start\|separate\), set
2027 ;; in c-setup-paragraph-variables, are used in the
2029 (forward-paragraph 1)
2030 (if (eq (preceding-char) ?\n) (forward-char -1))
2031 (when (<= (point) here) ; can happen, e.g., when HERE is at EOL.
2033 (forward-paragraph 2)
2034 (if (eq (preceding-char) ?\n) (forward-char -1)))
2039 (concat "^[ \t]*\\(" c-current-comment-prefix "\\)\\=")))
2040 ;; Go forward one "comment-prefix which looks like sentence-end"
2041 ;; each time round the following:
2042 (while (and (re-search-forward (c-sentence-end) par-end 'limit)
2045 (skip-chars-backward " \t\n")
2046 (or (and (not (bolp))
2047 (re-search-backward prefix-at-bol-here nil t)
2048 (/= (match-beginning 1) (match-end 1)))
2049 (<= (point) here))))
2052 ;; Take special action if we're up against the end of a comment (of
2053 ;; either sort): Leave point just after the last non-ws text.
2054 (if (eq (point) (point-max))
2055 (while (or (/= (skip-chars-backward " \t\n") 0)
2056 (and (re-search-backward prefix-at-bol-here nil t)
2057 (/= (match-beginning 1) (match-end 1))))))))
2059 (if (> (point) start-point)
2061 (goto-char (cdr range))
2064 (defun c-beginning-of-sentence-in-string (range)
2065 ;; Move backwards to the "beginning of a sentence" within the string defined
2066 ;; by RANGE, a cons of its starting and ending positions (enclosing the
2067 ;; string quotes). If we find a BOS, return NIL. Otherwise, move point to
2068 ;; just before the start of the string and return T.
2070 ;; The BOS is either the text which follows a regexp match of sentence-end
2071 ;; or text which is a beginning of "paragraph". For the purposes of
2072 ;; determining paragraph boundaries, escaped newlines are treated as
2073 ;; ordinary newlines.
2075 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
2076 ;; It is not a general function, but is intended only for calling from
2077 ;; c-move-over-sentence.
2079 ;; This function might do hidden buffer changes.
2081 (let* ((here (point)) last
2082 (end (1- (cdr range)))
2083 (here-filler ; matches WS and escaped newlines at point.
2084 "\\=\\([ \t\n\r\f]\\|\\\\[\n\r]\\)*")
2085 ;; Enhance paragraph-start and paragraph-separate also to recognise
2086 ;; blank lines terminated by escaped EOLs. IT MAY WELL BE that
2087 ;; these values should be customizable user options, or something.
2088 (paragraph-start c-string-par-start)
2089 (paragraph-separate c-string-par-separate)
2091 (par-beg ; beginning of current (or previous) paragraph.
2094 (narrow-to-region (1+ (car range)) end)
2095 (forward-paragraph -1) ; uses above values of
2096 ; paragraph-\(start\|separate\)
2097 (if (> (re-search-forward here-filler nil t) here)
2099 (when (>= (point) here)
2100 (forward-paragraph -2)
2101 (if (> (re-search-forward here-filler nil t) here)
2104 ;; Now see if we can find a sentence end after PAR-BEG.
2105 (while (and (re-search-backward c-sentence-end-with-esc-eol par-beg 'limit)
2107 (goto-char (match-end 0))
2111 here-filler end t) ; always succeeds. Use end rather
2112 ; than here, in case point starts
2113 ; beyond the closing quote.
2114 (>= (point) here))))
2116 (re-search-forward here-filler here t)
2117 (if (< (point) here)
2119 (goto-char (car range))
2122 (defun c-end-of-sentence-in-string (range)
2123 ;; Move forward to the "end of a sentence" within the string defined by
2124 ;; RANGE, a cons of its starting and ending positions. If we find an EOS,
2125 ;; return NIL. Otherwise, move point to just after the end of the string
2128 ;; The EOS is just after the non-WS part of the next match of the regexp
2129 ;; sentence-end. Typically, this is just after one of [.!?]. If there is
2130 ;; no sentence-end match following point, any WS before the end of the
2131 ;; string will count as EOS, providing we're not already in it.
2133 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
2134 ;; It is not a general function, but is intended only for calling from
2135 ;; c-move-over-sentence.
2137 ;; This function might do hidden buffer changes.
2139 (let* ((here (point))
2141 ;; Enhance paragraph-start and paragraph-separate to recognise
2142 ;; blank lines terminated by escaped EOLs.
2143 (paragraph-start c-string-par-start)
2144 (paragraph-separate c-string-par-separate)
2146 (par-end ; EOL position of last text in current/next paragraph.
2149 (narrow-to-region (car range) (1- (cdr range)))
2150 ;; The above values of paragraph-\(start\|separate\) are used
2151 ;; in the following.
2152 (forward-paragraph 1)
2154 ;; (re-search-backward filler-here nil t) would find an empty
2155 ;; string. Therefore we simulate it by the following:
2156 (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
2157 (re-search-backward "\\\\\\($\\)\\=" nil t)))
2158 (unless (> (point) here)
2160 (forward-paragraph 1)
2161 (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
2162 (re-search-backward "\\\\\\($\\)\\=" nil t))))
2164 ;; Try to go forward a sentence.
2165 (when (re-search-forward c-sentence-end-with-esc-eol par-end 'limit)
2167 (while (or (/= (skip-chars-backward " \t\n") 0)
2168 (re-search-backward "\\\\\\($\\)\\=" nil t))))
2169 ;; Did we move a sentence, or did we hit the end of the string?
2170 (if (> (point) here)
2172 (goto-char (cdr range))
2175 (defun c-ascertain-preceding-literal ()
2176 ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
2177 ;; If a literal is the next thing (aside from whitespace) to be found before
2178 ;; point, return a cons of its start.end positions (enclosing the
2179 ;; delimiters). Otherwise return NIL.
2181 ;; This function might do hidden buffer changes.
2183 (c-collect-line-comments
2184 (let ((here (point))
2186 (if (c-backward-single-comment)
2187 (cons (point) (progn (c-forward-single-comment) (point)))
2189 ;; to prevent `looking-at' seeing a " at point.
2190 (narrow-to-region (point-min) here)
2193 ;; An EOL can act as an "open string" terminator in AWK.
2194 (looking-at c-ws*-string-limit-regexp)
2196 (progn (backward-char)
2197 (looking-at c-string-limit-regexp))))
2198 (goto-char (match-end 0)) ; just after the string terminator.
2200 (c-safe (c-backward-sexp 1) ; move back over the string.
2201 (cons (point) pos)))))))))
2203 (defun c-ascertain-following-literal ()
2204 ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
2205 ;; If a literal is the next thing (aside from whitespace) following point,
2206 ;; return a cons of its start.end positions (enclosing the delimiters).
2207 ;; Otherwise return NIL.
2209 ;; This function might do hidden buffer changes.
2211 (c-collect-line-comments
2214 (if (looking-at c-string-limit-regexp) ; string-delimiter.
2215 (cons (point) (or (c-safe (progn (c-forward-sexp 1) (point)))
2218 (if (c-forward-single-comment)
2219 (cons pos (point))))))))
2221 (defun c-after-statement-terminator-p () ; Should we pass in LIM here?
2222 ;; Does point immediately follow a statement "terminator"? A virtual
2223 ;; semicolon is regarded here as such. So is a an opening brace ;-)
2225 ;; This function might do hidden buffer changes.
2228 (and (looking-at "[;{}]")
2229 (not (and c-special-brace-lists ; Pike special brace lists.
2230 (eq (char-after) ?{)
2231 (c-looking-at-special-brace-list)))))
2233 ;; The following (for macros) is not strict about exactly where we are
2234 ;; wrt white space at the end of the macro. Doesn't seem to matter too
2235 ;; much. ACM 2004/3/29.
2238 (if (c-beginning-of-macro)
2239 (setq eom (progn (c-end-of-macro)
2243 (c-forward-comments)
2244 (>= (point) eom))))))
2246 (defun c-back-over-illiterals (macro-start)
2247 ;; Move backwards over code which isn't a literal (i.e. comment or string),
2248 ;; stopping before reaching BOB or a literal or the boundary of a
2249 ;; preprocessor statement or the "beginning of a statement". MACRO-START is
2250 ;; the position of the '#' beginning the current preprocessor directive, or
2251 ;; NIL if we're not in such.
2253 ;; Return a cons (A.B), where
2254 ;; A is NIL if we moved back to a BOS (and know it), T otherwise (we
2255 ;; didn't move, or we hit a literal, or we're not sure about BOS).
2256 ;; B is MACRO-BOUNDARY if we are about to cross the boundary out of or
2257 ;; into a macro, otherwise LITERAL if we've hit a literal, otherwise NIL
2259 ;; The total collection of returned values is as follows:
2260 ;; (nil . nil): Found a BOS whilst remaining inside the illiterals.
2261 ;; (t . literal): No BOS found: only a comment/string. We _might_ be at
2262 ;; a BOS - the caller must check this.
2263 ;; (nil . macro-boundary): only happens with non-nil macro-start. We've
2264 ;; moved and reached the opening # of the macro.
2265 ;; (t . macro-boundary): Every other circumstance in which we're at a
2266 ;; macro-boundary. We might be at a BOS.
2268 ;; Point is left either at the beginning-of-statement, or at the last non-ws
2269 ;; code before encountering the literal/BOB or macro-boundary.
2271 ;; Note that this function moves within either preprocessor commands
2272 ;; (macros) or normal code, but will not cross a boundary between the two,
2273 ;; or between two distinct preprocessor commands.
2275 ;; Stop before `{' and after `;', `{', `}' and `};' when not followed by `}'
2276 ;; or `)', but on the other side of the syntactic ws. Move by sexps and
2277 ;; move into parens. Also stop before `#' when it's at boi on a line.
2279 ;; This function might do hidden buffer changes.
2281 (let ((here (point))
2282 last) ; marks the position of non-ws code, what'll be BOS if, say, a
2283 ; semicolon precedes it.
2285 (while t ;; We go back one "token" each iteration of the loop.
2288 ;; Stop at the token after a comment.
2289 ((c-backward-single-comment) ; Also functions as backwards-ws.
2291 (throw 'done '(t . literal)))
2293 ;; If we've gone back over a LF, we might have moved into or out of
2294 ;; a preprocessor line.
2295 ((and (save-excursion
2297 (re-search-forward "\\(^\\|[^\\]\\)[\n\r]" last t))
2299 (< (point) macro-start)
2300 (c-beginning-of-macro)))
2302 ;; Return a car of NIL ONLY if we've hit the opening # of a macro.
2303 (throw 'done (cons (or (eq (point) here)
2307 ;; Have we found a virtual semicolon? If so, stop, unless the next
2308 ;; statement is where we started from.
2309 ((and (c-at-vsemi-p)
2311 (not (memq (char-after last) '(?\) ?})))) ; we've moved back from ) or }
2313 (throw 'done '(nil . nil)))
2315 ;; Hit the beginning of the buffer/region?
2319 (throw 'done '(nil . nil)))
2321 ;; Move back a character.
2322 ((progn (backward-char) nil))
2324 ;; Stop at "{" (unless it's a PIKE special brace list.)
2325 ((eq (char-after) ?\{)
2326 (if (and c-special-brace-lists
2327 (c-looking-at-special-brace-list))
2328 (skip-syntax-backward "w_") ; Speedup only.
2331 (throw 'done '(nil . nil))))
2333 ;; Have we reached the start of a macro? This always counts as
2334 ;; BOS. (N.B. I don't think (eq (point) here) can ever be true
2335 ;; here. FIXME!!! ACM 2004/3/29)
2336 ((and macro-start (eq (point) macro-start))
2337 (throw 'done (cons (eq (point) here) 'macro-boundary)))
2339 ;; Stop at token just after "}" or ";".
2340 ((looking-at "[;}]")
2341 ;; If we've gone back over ;, {, or }, we're done.
2342 (if (or (= here last)
2343 (memq (char-after last) '(?\) ?}))) ; we've moved back from ) or }
2344 (if (and (eq (char-before) ?}) ; If };, treat them as a unit.
2345 (eq (char-after) ?\;))
2347 (goto-char last) ; To the statement starting after the ; or }.
2348 (throw 'done '(nil . nil))))
2350 ;; Stop at the token after a string.
2351 ((looking-at c-string-limit-regexp) ; Just gone back over a string terminator?
2353 (throw 'done '(t . literal)))
2355 ;; Nothing special: go back word characters.
2356 (t (skip-syntax-backward "w_")) ; Speedup only.
2359 (defun c-forward-over-illiterals (macro-end allow-early-stop)
2360 ;; Move forwards over code, stopping before reaching EOB or a literal
2361 ;; (i.e. a comment/string) or the boundary of a preprocessor statement or
2362 ;; the "end of a statement". MACRO-END is the position of the EOL/EOB which
2363 ;; terminates the current preprocessor directive, or NIL if we're not in
2366 ;; ALLOW-EARLY-STOP is non-nil if it is permissible to return without moving
2367 ;; forward at all, should we encounter a `{'. This is an ugly kludge, but
2368 ;; seems unavoidable. Depending on the context this function is called
2369 ;; from, we _sometimes_ need to stop there. Currently (2004/4/3),
2370 ;; ALLOW-EARLY-STOP is applied only to open braces, not to virtual
2371 ;; semicolons, or anything else.
2373 ;; Return a cons (A.B), where
2374 ;; A is NIL if we moved forward to an EOS, or stay at one (when
2375 ;; ALLOW-EARLY-STOP is set), T otherwise (we hit a literal).
2376 ;; B is 'MACRO-BOUNDARY if we are about to cross the boundary out of or
2377 ;; into a macro, otherwise 'LITERAL if we've hit a literal, otherwise NIL
2379 ;; Point is left either after the end-of-statement, or at the last non-ws
2380 ;; code before encountering the literal, or the # of the preprocessor
2381 ;; statement, or at EOB [or just after last non-WS stuff??].
2383 ;; As a clarification of "after the end-of-statement", if a comment or
2384 ;; whitespace follows a completed AWK statement, that statement is treated
2385 ;; as ending just after the last non-ws character before the comment.
2387 ;; Note that this function moves within either preprocessor commands
2388 ;; (macros) or normal code, but not both within the same invocation.
2390 ;; Stop before `{', `}', and `#' when it's at boi on a line, but on the
2391 ;; other side of the syntactic ws, and after `;', `}' and `};'. Only
2392 ;; stop before `{' if at top level or inside braces, though. Move by
2393 ;; sexps and move into parens. Also stop at eol of lines with `#' at
2396 ;; This function might do hidden buffer changes.
2397 (let ((here (point))
2400 (while t ;; We go one "token" forward each time round this loop.
2403 ;; If we've moved forward to a virtual semicolon, we're done.
2404 (if (and (> last here) ; Should we check ALLOW-EARLY-STOP, here? 2004/4/3
2406 (throw 'done '(nil . nil)))
2410 ;; Gone past the end of a macro?
2411 ((and macro-end (> (point) macro-end))
2413 (throw 'done (cons (eq (point) here) 'macro-boundary)))
2415 ;; About to hit a comment?
2416 ((save-excursion (c-forward-single-comment))
2418 (throw 'done '(t . literal)))
2424 (throw 'done '(nil . nil)))
2426 ;; If we encounter a '{', stop just after the previous token.
2427 ((and (eq (char-after) ?{)
2428 (not (and c-special-brace-lists
2429 (c-looking-at-special-brace-list)))
2430 (or allow-early-stop (/= here last))
2431 (save-excursion ; Is this a check that we're NOT at top level?
2432 ;;;; NO! This seems to check that (i) EITHER we're at the top level; OR (ii) The next enclosing
2433 ;;;; level of bracketing is a '{'. HMM. Doesn't seem to make sense.
2434 ;;;; 2003/8/8 This might have something to do with the GCC extension "Statement Expressions", e.g.
2435 ;;;; while ({stmt1 ; stmt2 ; exp ;}). This form excludes such Statement Expressions.
2436 (or (not (c-safe (up-list -1) t))
2437 (= (char-after) ?{))))
2439 (throw 'done '(nil . nil)))
2441 ;; End of a PIKE special brace list? If so, step over it and continue.
2442 ((and c-special-brace-lists
2443 (eq (char-after) ?})
2445 (and (c-safe (up-list -1) t)
2446 (c-looking-at-special-brace-list))))
2448 (skip-syntax-forward "w_")) ; Speedup only.
2450 ;; Have we got a '}' after having moved? If so, stop after the
2452 ((and (eq (char-after) ?})
2455 (throw 'done '(nil . nil)))
2457 ;; Stop if we encounter a preprocessor line.
2458 ((and (not macro-end)
2459 (eq (char-after) ?#)
2460 (= (point) (c-point 'boi)))
2462 ;(throw 'done (cons (eq (point) here) 'macro-boundary))) ; Changed 2003/3/26
2463 (throw 'done '(t . macro-boundary)))
2465 ;; Stop after a ';', '}', or "};"
2466 ((looking-at ";\\|};?")
2467 (goto-char (match-end 0))
2468 (throw 'done '(nil . nil)))
2470 ;; Found a string (this subsumes AWK regexps)?
2471 ((looking-at c-string-limit-regexp)
2473 (throw 'done '(t . literal)))
2476 (forward-char) ; Can't fail - we checked (eobp) earlier on.
2477 (skip-syntax-forward "w_") ; Speedup only.
2478 (when (and macro-end (> (point) macro-end))
2480 (throw 'done (cons (eq (point) here) 'macro-boundary))))
2483 (defun c-one-line-string-p (range)
2484 ;; Is the literal defined by RANGE a string contained in a single line?
2486 ;; This function might do hidden buffer changes.
2488 (goto-char (car range))
2489 (and (looking-at c-string-limit-regexp)
2490 (progn (skip-chars-forward "^\n" (cdr range))
2491 (eq (point) (cdr range))))))
2493 (defun c-beginning-of-statement (&optional count lim sentence-flag)
2494 "Go to the beginning of the innermost C statement.
2495 With prefix arg, go back N - 1 statements. If already at the
2496 beginning of a statement then go to the beginning of the closest
2497 preceding one, moving into nested blocks if necessary (use
2498 \\[backward-sexp] to skip over a block). If within or next to a
2499 comment or multiline string, move by sentences instead of statements.
2501 When called from a program, this function takes 3 optional args: the
2502 repetition count, a buffer position limit which is the farthest back
2503 to search for the syntactic context, and a flag saying whether to do
2504 sentence motion in or near comments and multiline strings.
2506 Note that for use in programs, `c-beginning-of-statement-1' is
2507 usually better. It has much better defined semantics than this one,
2508 which is intended for interactive use, and might therefore change to
2509 be more \"DWIM:ey\"."
2510 (interactive (list (prefix-numeric-value current-prefix-arg)
2513 (c-end-of-statement (- count) lim sentence-flag)
2514 (c-save-buffer-state
2515 ((count (or count 1))
2516 last ; start point for going back ONE chunk. Updated each chunk movement.
2518 (save-excursion (and (not (bobp)) (c-beginning-of-macro) (point))))
2519 res ; result from sub-function call
2520 not-bos ; "not beginning-of-statement"
2521 (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
2523 ;; Go back one statement at each iteration of the following loop.
2524 (while (and (/= count 0)
2525 (or (not lim) (> (point) lim)))
2526 ;; Go back one "chunk" each time round the following loop, stopping
2527 ;; when we reach a statement boundary, etc.
2530 (cond ; Each arm of this cond returns NIL on reaching a desired
2531 ; statement boundary, non-NIL otherwise.
2536 (range ; point is within or approaching a literal.
2538 ;; Single line string or sentence-flag is null => skip the
2540 ((or (null sentence-flag)
2541 (c-one-line-string-p range))
2542 (goto-char (car range))
2543 (setq range (c-ascertain-preceding-literal))
2544 ;; N.B. The following is essentially testing for an AWK regexp
2546 ;; Was the previous non-ws thing an end of statement?
2549 (c-backward-comments)
2550 (c-backward-syntactic-ws))
2551 (not (or (bobp) (c-after-statement-terminator-p)))))
2553 ;; Comment inside a statement or a multi-line string.
2554 (t (when (setq res ; returns non-nil when we go out of the literal
2555 (if (eq (c-literal-type range) 'string)
2556 (c-beginning-of-sentence-in-string range)
2557 (c-beginning-of-sentence-in-comment range)))
2558 (setq range (c-ascertain-preceding-literal)))
2561 ;; Non-literal code.
2562 (t (setq res (c-back-over-illiterals macro-fence))
2563 (setq not-bos ; "not reached beginning-of-statement".
2564 (or (= (point) last)
2565 (memq (char-after) '(?\) ?\}))
2568 ;; We're at a tentative BOS. The next form goes
2569 ;; back over WS looking for an end of previous
2571 (not (save-excursion
2573 (c-backward-comments)
2574 (c-backward-syntactic-ws))
2575 (or (bobp) (c-after-statement-terminator-p)))))))
2576 ;; Are we about to move backwards into or out of a
2577 ;; preprocessor command? If so, locate it's beginning.
2578 (when (eq (cdr res) 'macro-boundary)
2583 (progn (c-skip-ws-backward) (c-beginning-of-macro))
2585 ;; Are we about to move backwards into a literal?
2586 (when (memq (cdr res) '(macro-boundary literal))
2587 (setq range (c-ascertain-preceding-literal)))
2589 (setq last (point)))
2591 (if (/= count 0) (setq count (1- count))))
2592 (c-keep-region-active))))
2594 (defun c-end-of-statement (&optional count lim sentence-flag)
2595 "Go to the end of the innermost C statement.
2596 With prefix arg, go forward N - 1 statements. Move forward to the end
2597 of the next statement if already at end, and move into nested blocks
2598 \(use \\[forward-sexp] to skip over a block). If within or next to a
2599 comment or multiline string, move by sentences instead of statements.
2601 When called from a program, this function takes 3 optional args: the
2602 repetition count, a buffer position limit which is the farthest back
2603 to search for the syntactic context, and a flag saying whether to do
2604 sentence motion in or near comments and multiline strings."
2605 (interactive (list (prefix-numeric-value current-prefix-arg)
2607 (setq count (or count 1))
2608 (if (< count 0) (c-beginning-of-statement (- count) lim sentence-flag)
2610 (c-save-buffer-state
2611 (here ; start point for going forward ONE statement. Updated each statement.
2614 (and (not (eobp)) (c-beginning-of-macro)
2615 (progn (c-end-of-macro) (point)))))
2617 (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
2619 ;; Go back/forward one statement at each iteration of the following loop.
2620 (while (and (/= count 0)
2621 (or (not lim) (< (point) lim)))
2622 (setq here (point)) ; ONLY HERE is HERE updated
2624 ;; Go forward one "chunk" each time round the following loop, stopping
2625 ;; when we reach a statement boundary, etc.
2627 (cond ; Each arm of this cond returns NIL on reaching a desired
2628 ; statement boundary, non-NIL otherwise.
2633 (range ; point is within a literal.
2635 ;; sentence-flag is null => skip the entire literal.
2636 ;; or a Single line string.
2637 ((or (null sentence-flag)
2638 (c-one-line-string-p range))
2639 (goto-char (cdr range))
2640 (setq range (c-ascertain-following-literal))
2641 ;; Is there a virtual semicolon here (e.g. for AWK)?
2642 (not (c-at-vsemi-p)))
2644 ;; Comment or multi-line string.
2645 (t (when (setq res ; gets non-nil when we go out of the literal
2646 (if (eq (c-literal-type range) 'string)
2647 (c-end-of-sentence-in-string range)
2648 (c-end-of-sentence-in-comment range)))
2649 (setq range (c-ascertain-following-literal)))
2650 ;; If we've just come forward out of a literal, check for
2651 ;; vsemi. (N.B. AWK can't have a vsemi after a comment, but
2652 ;; some other language may do in the future)
2654 (not (c-at-vsemi-p))))))
2656 ;; Non-literal code.
2657 (t (setq res (c-forward-over-illiterals macro-fence
2659 ;; Are we about to move forward into or out of a
2660 ;; preprocessor command?
2661 (when (eq (cdr res) 'macro-boundary)
2666 (progn (c-skip-ws-forward)
2667 (c-beginning-of-macro))
2668 (progn (c-end-of-macro)
2670 ;; Are we about to move forward into a literal?
2671 (when (memq (cdr res) '(macro-boundary literal))
2672 (setq range (c-ascertain-following-literal)))
2675 (if (/= count 0) (setq count (1- count))))
2676 (c-keep-region-active))))
2679 ;; set up electric character functions to work with pending-del,
2680 ;; (a.k.a. delsel) mode. All symbols get the t value except
2681 ;; the functions which delete, which gets 'supersede.
2685 (put sym 'delete-selection t) ; for delsel (Emacs)
2686 (put sym 'pending-delete t))) ; for pending-del (XEmacs)
2691 c-electric-semi&comma
2695 (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
2696 (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
2697 (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
2698 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
2699 (put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel
2700 (put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del
2703 ;; Inserting/indenting comments
2704 (defun c-calc-comment-indent (entry)
2705 ;; This function might do hidden buffer changes.
2707 (setq entry (or (assq entry c-indent-comment-alist)
2708 (assq 'other c-indent-comment-alist)
2709 '(default . (column . nil)))))
2710 (let ((action (car (cdr entry)))
2711 (value (cdr (cdr entry)))
2712 (col (current-column)))
2713 (cond ((eq action 'space)
2715 ((eq action 'column)
2716 (unless value (setq value comment-column))
2718 ;; Do not pad with one space if we're at bol.
2720 (max (1+ col) value)))
2726 (let ((lim (c-literal-limits (c-point 'bol) t)))
2728 (goto-char (car lim))
2729 (when (looking-at "/[/*]") ; FIXME!!! Adapt for AWK! (ACM, 2005/11/18)
2730 ;; Found comment to align with.
2732 ;; Do not pad with one space if we're at bol.
2734 (max (1+ col) (current-column))))))))
2735 ;; Recurse to handle value as a new spec.
2736 (c-calc-comment-indent (cdr entry)))))))
2738 (defun c-comment-indent ()
2739 "Used by `indent-for-comment' to create and indent comments.
2740 See `c-indent-comment-alist' for a description."
2743 (c-save-buffer-state
2744 ((eot (let ((lim (c-literal-limits (c-point 'bol) t)))
2745 (or (when (consp lim)
2746 (goto-char (car lim))
2747 (when (looking-at "/[/*]")
2748 (skip-chars-backward " \t")
2751 (skip-chars-backward " \t")
2754 (cond ((looking-at "^/[/*]")
2756 ((progn (beginning-of-line)
2759 ((progn (back-to-indentation)
2760 (and (eq (char-after) ?})
2761 (eq (point) (1- eot))))
2763 ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
2764 (eq (match-end 0) eot))
2768 (if (and (memq line-type '(anchored-comment empty-line))
2769 c-indent-comments-syntactically-p)
2770 (let ((c-syntactic-context (c-guess-basic-syntax)))
2771 ;; BOGOSITY ALERT: if we're looking at the eol, its
2772 ;; because indent-for-comment hasn't put the comment-start
2773 ;; in the buffer yet. this will screw up the syntactic
2774 ;; analysis so we kludge in the necessary info. Another
2775 ;; kludge is that if we're at the bol, then we really want
2776 ;; to ignore any anchoring as specified by
2777 ;; c-comment-only-line-offset since it doesn't apply here.
2779 (c-add-syntax 'comment-intro))
2780 (let ((c-comment-only-line-offset
2781 (if (consp c-comment-only-line-offset)
2782 c-comment-only-line-offset
2783 (cons c-comment-only-line-offset
2784 c-comment-only-line-offset))))
2785 (c-get-syntactic-indentation c-syntactic-context)))
2787 (c-calc-comment-indent line-type)))))
2790 ;; used by outline-minor-mode
2791 (defun c-outline-level ()
2792 (let (buffer-invisibility-spec);; This so that `current-column' DTRT
2793 ;; in otherwise-hidden text.
2795 (skip-chars-forward "\t ")
2799 ;; Movement by CPP conditionals.
2800 (defun c-up-conditional (count)
2801 "Move back to the containing preprocessor conditional, leaving mark behind.
2802 A prefix argument acts as a repeat count. With a negative argument,
2803 move forward to the end of the containing preprocessor conditional.
2805 \"#elif\" is treated like \"#else\" followed by \"#if\", so the
2806 function stops at them when going backward, but not when going
2809 (c-forward-conditional (- count) -1)
2810 (c-keep-region-active))
2812 (defun c-up-conditional-with-else (count)
2813 "Move back to the containing preprocessor conditional, including \"#else\".
2814 Just like `c-up-conditional', except it also stops at \"#else\"
2817 (c-forward-conditional (- count) -1 t)
2818 (c-keep-region-active))
2820 (defun c-down-conditional (count)
2821 "Move forward into the next preprocessor conditional, leaving mark behind.
2822 A prefix argument acts as a repeat count. With a negative argument,
2823 move backward into the previous preprocessor conditional.
2825 \"#elif\" is treated like \"#else\" followed by \"#if\", so the
2826 function stops at them when going forward, but not when going
2829 (c-forward-conditional count 1)
2830 (c-keep-region-active))
2832 (defun c-down-conditional-with-else (count)
2833 "Move forward into the next preprocessor conditional, including \"#else\".
2834 Just like `c-down-conditional', except it also stops at \"#else\"
2837 (c-forward-conditional count 1 t)
2838 (c-keep-region-active))
2840 (defun c-backward-conditional (count &optional target-depth with-else)
2841 "Move back across a preprocessor conditional, leaving mark behind.
2842 A prefix argument acts as a repeat count. With a negative argument,
2843 move forward across a preprocessor conditional."
2845 (c-forward-conditional (- count) target-depth with-else)
2846 (c-keep-region-active))
2848 (defun c-forward-conditional (count &optional target-depth with-else)
2849 "Move forward across a preprocessor conditional, leaving mark behind.
2850 A prefix argument acts as a repeat count. With a negative argument,
2851 move backward across a preprocessor conditional.
2853 \"#elif\" is treated like \"#else\" followed by \"#if\", except that
2854 the nesting level isn't changed when tracking subconditionals.
2856 The optional argument TARGET-DEPTH specifies the wanted nesting depth
2857 after each scan. I.e. if TARGET-DEPTH is -1, the function will move
2858 out of the enclosing conditional. A non-integer non-nil TARGET-DEPTH
2861 If the optional argument WITH-ELSE is non-nil, \"#else\" directives
2862 are treated as conditional clause limits. Normally they are ignored."
2864 (let* ((forward (> count 0))
2865 (increment (if forward -1 1))
2866 (search-function (if forward 're-search-forward 're-search-backward))
2868 (unless (integerp target-depth)
2869 (setq target-depth (if target-depth -1 0)))
2873 ;; subdepth is the depth in "uninteresting" subtrees,
2874 ;; i.e. those that takes us farther from the target
2875 ;; depth instead of closer.
2879 ;; Find the "next" significant line in the proper direction.
2880 (while (and (not found)
2881 ;; Rather than searching for a # sign that
2882 ;; comes at the beginning of a line aside from
2883 ;; whitespace, search first for a string
2884 ;; starting with # sign. Then verify what
2885 ;; precedes it. This is faster on account of
2886 ;; the fastmap feature of the regexp matcher.
2887 (funcall search-function
2888 "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
2891 ;; Now verify it is really a preproc line.
2892 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
2893 (let (dchange (directive (match-string 1)))
2894 (cond ((string= directive "if")
2895 (setq dchange (- increment)))
2896 ((string= directive "endif")
2897 (setq dchange increment))
2899 ;; When we're not in an "uninteresting"
2900 ;; subtree, we might want to act on "elif"
2902 (if (cond (with-else
2903 ;; Always move toward the target depth.
2905 (if (> target-depth 0) 1 -1)))
2906 ((string= directive "elif")
2907 (setq dchange (- increment))))
2908 ;; Ignore the change if it'd take us
2909 ;; into an "uninteresting" subtree.
2910 (if (eq (> dchange 0) (<= target-depth 0))
2911 (setq dchange nil)))))
2913 (when (or (/= subdepth 0)
2914 (eq (> dchange 0) (<= target-depth 0)))
2915 (setq subdepth (+ subdepth dchange)))
2916 (setq depth (+ depth dchange))
2917 ;; If we are trying to move across, and we find an
2918 ;; end before we find a beginning, get an error.
2919 (if (and (< depth target-depth) (< dchange 0))
2921 "No following conditional at this level"
2922 "No previous conditional at this level"))))
2923 ;; When searching forward, start from next line so
2924 ;; that we don't find the same line again.
2925 (if forward (forward-line 1))
2926 ;; We found something if we've arrived at the
2928 (if (and dchange (= depth target-depth))
2929 (setq found (point))))
2931 (if forward (forward-line 1)))))
2933 (error "No containing preprocessor conditional"))
2934 (goto-char (setq new found)))
2935 (setq count (+ count increment))))
2938 (c-keep-region-active))
2941 ;; commands to indent lines, regions, defuns, and expressions
2942 (defun c-indent-command (&optional arg)
2943 "Indent current line as C code, and/or insert some whitespace.
2945 If `c-tab-always-indent' is t, always just indent the current line.
2946 If nil, indent the current line only if point is at the left margin or
2947 in the line's indentation; otherwise insert some whitespace[*]. If
2948 other than nil or t, then some whitespace[*] is inserted only within
2949 literals (comments and strings), but the line is always reindented.
2951 If `c-syntactic-indentation' is t, indentation is done according to
2952 the syntactic context. A numeric argument, regardless of its value,
2953 means indent rigidly all the lines of the expression starting after
2954 point so that this line becomes properly indented. The relative
2955 indentation among the lines of the expression is preserved.
2957 If `c-syntactic-indentation' is nil, the line is just indented one
2958 step according to `c-basic-offset'. In this mode, a numeric argument
2959 indents a number of such steps, positive or negative, and an empty
2960 prefix argument is equivalent to -1.
2962 [*] The amount and kind of whitespace inserted is controlled by the
2963 variable `c-insert-tab-function', which is called to do the actual
2964 insertion of whitespace. Normally the function in this variable
2965 just inserts a tab character, or the equivalent number of spaces,
2966 depending on the variable `indent-tabs-mode'."
2969 (let ((indent-function
2970 (if c-syntactic-indentation
2971 (symbol-function 'indent-according-to-mode)
2973 (let ((c-macro-start c-macro-start)
2974 (steps (if (equal arg '(4))
2976 (prefix-numeric-value arg))))
2977 (c-shift-line-indentation (* steps c-basic-offset))
2978 (when (and c-auto-align-backslashes
2981 (eq (char-before) ?\\))
2982 (c-query-and-set-macro-start))
2983 ;; Realign the line continuation backslash if inside a macro.
2984 (c-backslash-region (point) (point) nil t)))
2986 (if (and c-syntactic-indentation arg)
2987 ;; If c-syntactic-indentation and got arg, always indent this
2988 ;; line as C and shift remaining lines of expression the same
2990 (let ((shift-amt (save-excursion
2991 (back-to-indentation)
2995 (setq shift-amt (- (save-excursion
2996 (back-to-indentation)
3000 (if (eq c-tab-always-indent t)
3001 (beginning-of-line)) ; FIXME!!! What is this here for? ACM 2005/10/31
3009 (indent-code-rigidly beg end shift-amt "#")))
3010 ;; Else use c-tab-always-indent to determine behavior.
3012 ;; CASE 1: indent when at column zero or in line's indentation,
3013 ;; otherwise insert a tab
3014 ((not c-tab-always-indent)
3016 (skip-chars-backward " \t")
3018 (funcall c-insert-tab-function)
3019 (funcall indent-function)))
3020 ;; CASE 2: just indent the line
3021 ((eq c-tab-always-indent t)
3022 (funcall indent-function))
3023 ;; CASE 3: if in a literal, insert a tab, but always indent the
3026 (if (c-save-buffer-state () (c-in-literal))
3027 (funcall c-insert-tab-function))
3028 (funcall indent-function)
3031 (defun c-indent-exp (&optional shutup-p)
3032 "Indent each line in the balanced expression following point syntactically.
3033 If optional SHUTUP-P is non-nil, no errors are signaled if no
3034 balanced expression is found."
3036 (let ((here (point-marker))
3038 (set-marker-insertion-type here t)
3040 (let ((start (save-restriction
3041 ;; Find the closest following open paren that
3042 ;; ends on another line.
3043 (narrow-to-region (point-min) (c-point 'eol))
3044 (let (beg (end (point)))
3045 (while (and (setq beg (c-down-list-forward end))
3046 (setq end (c-up-list-forward beg))))
3048 (eq (char-syntax (char-before beg)) ?\()
3053 (error "Cannot find start of balanced expression to indent"))
3055 (setq end (c-safe (scan-sexps (point) 1)))
3058 (error "Cannot find end of balanced expression to indent"))
3061 (c-indent-region (point) end)))))
3063 (set-marker here nil))))
3065 (defun c-indent-defun ()
3066 "Indent the current top-level declaration or macro syntactically.
3067 In the macro case this also has the effect of realigning any line
3068 continuation backslashes, unless `c-auto-align-backslashes' is nil."
3070 (let ((here (point-marker)) decl-limits)
3073 (c-save-buffer-state nil
3074 ;; We try to be line oriented, unless there are several
3075 ;; declarations on the same line.
3076 (if (looking-at c-syntactic-eol)
3077 (c-backward-token-2 1 nil (c-point 'bol))
3078 (c-forward-token-2 0 nil (c-point 'eol)))
3079 (setq decl-limits (c-declaration-limits nil)))
3081 (c-indent-region (car decl-limits)
3082 (cdr decl-limits))))
3084 (set-marker here nil))))
3086 (defun c-indent-region (start end &optional quiet)
3087 "Indent syntactically every line whose first char is between START
3088 and END inclusive. If the optional argument QUIET is non-nil then no
3089 syntactic errors are reported, even if `c-report-syntactic-errors' is
3093 (skip-chars-backward " \t\n\r\f\v")
3096 ;; Advance to first nonblank line.
3098 (skip-chars-forward " \t\n\r\f\v")
3099 (setq start (point))
3101 (setq c-parsing-error
3102 (or (let ((endmark (copy-marker end))
3103 (c-parsing-error nil)
3104 ;; shut up any echo msgs on indiv lines
3105 (c-echo-syntactic-information-p nil)
3106 (ml-macro-start ; Start pos of multi-line macro.
3107 (and (c-save-buffer-state ()
3108 (save-excursion (c-beginning-of-macro)))
3109 (eq (char-before (c-point 'eol)) ?\\)
3111 (c-fix-backslashes nil)
3115 (c-progress-init start end 'c-indent-region)
3117 (while (and (bolp) ;; One line each time round the loop.
3119 (< (point) endmark))
3123 (unless (or (looking-at "\\s *$")
3124 (and ml-macro-start (looking-at "\\s *\\\\$")))
3125 ;; Get syntax and indent.
3126 (c-save-buffer-state nil
3127 (setq syntax (c-guess-basic-syntax)))
3128 (c-indent-line syntax t t))
3131 ;; End of current multi-line macro?
3132 (when (and c-auto-align-backslashes
3133 (not (eq (char-before (c-point 'eol)) ?\\)))
3134 ;; Fixup macro backslashes.
3135 (c-backslash-region ml-macro-start (c-point 'bonl) nil)
3136 (setq ml-macro-start nil))
3137 ;; New multi-line macro?
3138 (if (and (assq 'cpp-macro syntax)
3139 (eq (char-before (c-point 'eol)) ?\\))
3140 (setq ml-macro-start (point))))
3144 (if (and ml-macro-start c-auto-align-backslashes)
3145 (c-backslash-region ml-macro-start (c-point 'bopl) nil t)))
3146 (set-marker endmark nil)
3147 (c-progress-fini 'c-indent-region))
3148 (c-echo-parsing-error quiet))
3151 (defun c-fn-region-is-active-p ()
3152 ;; Function version of the macro for use in places that aren't
3153 ;; compiled, e.g. in the menus.
3154 (c-region-is-active-p))
3156 (defun c-indent-line-or-region (&optional arg region)
3157 "Indent active region, current line, or block starting on this line.
3158 In Transient Mark mode, when the region is active, reindent the region.
3159 Otherwise, with a prefix argument, rigidly reindent the expression
3160 starting on the current line.
3161 Otherwise reindent just the current line."
3163 (list current-prefix-arg (use-region-p)))
3165 (c-indent-region (region-beginning) (region-end))
3166 (c-indent-command arg)))
3168 ;; for progress reporting
3169 (defvar c-progress-info nil)
3171 (defun c-progress-init (start end context)
3174 ((not c-progress-interval))
3175 ;; Start the progress update messages. If this Emacs doesn't have
3176 ;; a built-in timer, just be dumb about it.
3177 ((not (fboundp 'current-time))
3178 (message "Indenting region... (this may take a while)"))
3179 ;; If progress has already been initialized, do nothing. otherwise
3180 ;; initialize the counter with a vector of:
3181 ;; [start end lastsec context]
3183 (t (setq c-progress-info (vector start
3187 (nth 1 (current-time))
3189 (message "Indenting region..."))
3192 (defun c-progress-update ()
3193 (if (not (and c-progress-info c-progress-interval))
3195 (let ((now (nth 1 (current-time)))
3196 (start (aref c-progress-info 0))
3197 (end (aref c-progress-info 1))
3198 (lastsecs (aref c-progress-info 2)))
3199 ;; should we update? currently, update happens every 2 seconds,
3200 ;; what's the right value?
3201 (if (< c-progress-interval (- now lastsecs))
3203 (message "Indenting region... (%d%% complete)"
3204 (/ (* 100 (- (point) start)) (- end start)))
3205 (aset c-progress-info 2 now)))
3208 (defun c-progress-fini (context)
3209 (if (not c-progress-interval)
3211 (if (or (eq context (aref c-progress-info 3))
3214 (set-marker (aref c-progress-info 1) nil)
3215 (setq c-progress-info nil)
3216 (message "Indenting region... done")))))
3220 ;;; This page handles insertion and removal of backslashes for C macros.
3222 (defun c-backslash-region (from to delete-flag &optional line-mode)
3223 "Insert, align, or delete end-of-line backslashes on the lines in the region.
3224 With no argument, inserts backslashes and aligns existing backslashes.
3225 With an argument, deletes the backslashes. The backslash alignment is
3226 done according to the settings in `c-backslash-column',
3227 `c-backslash-max-column' and `c-auto-align-backslashes'.
3229 This function does not modify blank lines at the start of the region.
3230 If the region ends at the start of a line and the macro doesn't
3231 continue below it, the backslash (if any) at the end of the previous
3234 You can put the region around an entire macro definition and use this
3235 command to conveniently insert and align the necessary backslashes."
3236 (interactive "*r\nP")
3237 (let ((endmark (make-marker))
3238 ;; Keep the backslash trimming functions from changing the
3239 ;; whitespace around point, since in this case it's only the
3240 ;; position of point that tells the indentation of the line.
3241 (point-pos (if (save-excursion
3242 (skip-chars-backward " \t")
3243 (and (bolp) (looking-at "[ \t]*\\\\?$")))
3246 column longest-line-col bs-col-after-end)
3249 (if (and (not line-mode) (bobp))
3250 ;; Nothing to do if to is at bob, since we should back up
3251 ;; and there's no line to back up to.
3253 (when (and (not line-mode) (bolp))
3254 ;; Do not back up the to line if line-mode is set, to make
3255 ;; e.g. c-newline-and-indent consistent regardless whether
3256 ;; the (newline) call leaves point at bol or not.
3261 (set-marker endmark (point))
3263 (c-delete-backslashes-forward endmark point-pos))
3264 ;; Set bs-col-after-end to the column of any backslash
3265 ;; following the region, or nil if there is none.
3266 (setq bs-col-after-end
3267 (and (progn (end-of-line)
3268 (eq (char-before) ?\\))
3269 (= (forward-line 1) 0)
3270 (progn (end-of-line)
3271 (eq (char-before) ?\\))
3272 (1- (current-column))))
3274 ;; Back up the to line if line-mode is set, since the line
3275 ;; after the newly inserted line break should not be
3276 ;; touched in c-newline-and-indent.
3277 (setq to (max from (or (c-safe (c-point 'eopl)) from)))
3278 (unless bs-col-after-end
3279 ;; Set bs-col-after-end to non-nil in any case, since we
3280 ;; do not want to delete the backslash at the last line.
3281 (setq bs-col-after-end t)))
3283 (not c-auto-align-backslashes))
3285 ;; Compute the smallest column number past the ends of all
3287 (setq longest-line-col 0)
3289 (if bs-col-after-end
3290 ;; Include one more line in the max column
3291 ;; calculation, since the to line will be backslashed
3295 (while (and (>= (point) from)
3297 (if (eq (char-before) ?\\)
3299 (skip-chars-backward " \t")
3300 (setq longest-line-col (max longest-line-col
3301 (1+ (current-column))))
3305 ;; Try to align with surrounding backslashes.
3308 (if (and (not (bobp))
3309 (progn (backward-char)
3310 (eq (char-before) ?\\)))
3312 (setq column (1- (current-column)))
3313 (if (numberp bs-col-after-end)
3314 ;; Both a preceding and a following backslash.
3315 ;; Choose the greatest of them.
3316 (setq column (max column bs-col-after-end)))
3318 ;; No preceding backslash. Try to align with one
3319 ;; following the region. Disregard the backslash at the
3320 ;; to line since it's likely to be bogus (e.g. when
3321 ;; called from c-newline-and-indent).
3322 (if (numberp bs-col-after-end)
3323 (setq column bs-col-after-end))
3324 ;; Don't modify blank lines at start of region.
3326 (while (and (< (point) to) (bolp) (eolp))
3328 (if (and column (< column longest-line-col))
3329 ;; Don't try to align with surrounding backslashes if
3330 ;; any line is too long.
3333 ;; Impose minimum limit and tab width alignment only if
3334 ;; we can't align with surrounding backslashes.
3335 (if (> (% longest-line-col tab-width) 0)
3336 (setq longest-line-col
3337 (* (/ (+ longest-line-col tab-width -1)
3340 (setq column (max c-backslash-column
3342 ;; Always impose maximum limit.
3343 (setq column (min column c-backslash-max-column)))
3344 (if bs-col-after-end
3345 ;; Add backslashes on all lines if the macro continues
3346 ;; after the to line.
3348 (set-marker endmark to)
3349 (c-append-backslashes-forward endmark column point-pos))
3350 ;; Add backslashes on all lines except the last, and
3351 ;; remove any on the last line.
3356 (set-marker endmark (1- (point)))))
3358 (c-append-backslashes-forward endmark column point-pos)
3359 ;; The function above leaves point on the line
3360 ;; following endmark.
3361 (set-marker endmark (point)))
3362 (set-marker endmark to))
3363 (c-delete-backslashes-forward endmark point-pos)))))
3364 (set-marker endmark nil)
3365 (if (markerp point-pos)
3366 (set-marker point-pos nil))))
3368 (defun c-append-backslashes-forward (to-mark column point-pos)
3369 (let ((state (parse-partial-sexp (c-point 'bol) (point))))
3373 (<= (point) to-mark)
3375 (let ((start (point)) (inserted nil) end col)
3377 (unless (eq (char-before) ?\\)
3380 (setq state (parse-partial-sexp
3381 start (point) nil nil state))
3383 (setq col (current-column))
3385 ;; Avoid unnecessary changes of the buffer.
3386 (cond ((and (not inserted) (nth 3 state))
3387 ;; Don't realign backslashes in string literals
3388 ;; since that would change them.
3395 (skip-chars-backward
3396 " \t" (if (>= (point) point-pos) point-pos))
3400 ((and (= col column)
3401 (memq (char-before) '(?\ ?\t))))
3405 (or (/= (skip-chars-backward
3406 " \t" (if (>= (point) point-pos) point-pos))
3408 (/= (char-after) ?\ )))
3409 (delete-region (point) end)
3410 (indent-to column 1)))
3412 (zerop (forward-line 1)))
3413 (bolp))) ; forward-line has funny behavior at eob.
3415 ;; Make sure there are backslashes with at least one space in
3419 (<= (point) to-mark)
3421 (let ((start (point)))
3423 (setq state (parse-partial-sexp
3424 start (point) nil nil state))
3426 (if (eq (char-before) ?\\)
3427 (unless (nth 3 state)
3429 (unless (and (memq (char-before) '(?\ ?\t))
3430 (/= (point) point-pos))
3433 (if (and (memq (char-before) '(?\ ?\t))
3434 (/= (point) point-pos))
3438 (zerop (forward-line 1)))
3439 (bolp)))))) ; forward-line has funny behavior at eob.
3441 (defun c-delete-backslashes-forward (to-mark point-pos)
3443 (and (<= (point) to-mark)
3446 (if (eq (char-before) ?\\)
3449 (progn (backward-char)
3450 (skip-chars-backward " \t" (if (>= (point) point-pos)
3453 (zerop (forward-line 1)))
3454 (bolp)))) ; forward-line has funny behavior at eob.
3458 ;;; Line breaking and paragraph filling.
3460 (defvar c-auto-fill-prefix t)
3461 (defvar c-lit-limits nil)
3462 (defvar c-lit-type nil)
3464 ;; The filling code is based on a simple theory; leave the intricacies
3465 ;; of the text handling to the currently active mode for that
3466 ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
3467 ;; possible to make them work correctly wrt the comment and string
3468 ;; separators, one-line paragraphs etc. Unfortunately, when it comes
3469 ;; to it, there's quite a lot of special cases to handle which makes
3470 ;; the code anything but simple. The intention is that it will work
3471 ;; with any well-written text filling package that preserves a fill
3474 ;; We temporarily mask comment starters and enders as necessary for
3475 ;; the filling code to do its job on a seemingly normal text block.
3476 ;; We do _not_ mask the fill prefix, so it's up to the filling code to
3477 ;; preserve it correctly (especially important when filling C++ style
3478 ;; line comments). By default, we set up and use adaptive-fill-mode,
3479 ;; which is standard in all supported Emacs flavors.
3481 (defun c-guess-fill-prefix (lit-limits lit-type)
3482 ;; Determine the appropriate comment fill prefix for a block or line
3483 ;; comment. Return a cons of the prefix string and the column where
3484 ;; it ends. If fill-prefix is set, it'll override. Note that this
3485 ;; function also uses the value of point in some heuristics.
3487 ;; This function might do hidden buffer changes.
3489 (let* ((here (point))
3490 (prefix-regexp (concat "[ \t]*\\("
3491 c-current-comment-prefix
3493 (comment-start-regexp (if (eq lit-type 'c++)
3495 comment-start-skip))
3496 prefix-line comment-prefix res comment-text-end)
3500 (setq res (cons fill-prefix
3501 ;; Ugly way of getting the column after the fill
3502 ;; prefix; it'd be nice with a current-column
3503 ;; that works on strings..
3504 (let ((start (point)))
3507 (insert-and-inherit "\n" fill-prefix)
3509 (delete-region start (point)))))))
3513 ;; Set fallback for comment-prefix if none is found.
3514 (setq comment-prefix "// "
3515 comment-text-end (cdr lit-limits))
3518 (if (> (point) (car lit-limits))
3519 ;; The current line is not the comment starter, so the
3520 ;; comment has more than one line, and it can therefore be
3521 ;; used to find the comment fill prefix.
3522 (setq prefix-line (point))
3524 (goto-char (car lit-limits))
3525 (if (and (= (forward-line 1) 0)
3526 (< (point) (cdr lit-limits)))
3527 ;; The line after the comment starter is inside the
3528 ;; comment, so we can use it.
3529 (setq prefix-line (point))
3531 ;; The comment is only one line. Take the comment prefix
3532 ;; from it and keep the indentation.
3533 (goto-char (car lit-limits))
3534 (if (looking-at prefix-regexp)
3535 (goto-char (match-end 0))
3537 (skip-chars-forward " \t"))
3540 (if (eq (c-point 'boi) (car lit-limits))
3541 ;; There is only whitespace before the comment
3542 ;; starter; take the prefix straight from this line.
3543 (setq str (buffer-substring-no-properties
3544 (c-point 'bol) (point))
3545 col (current-column))
3547 ;; There is code before the comment starter, so we
3548 ;; have to temporarily insert and indent a new line to
3549 ;; get the right space/tab mix in the indentation.
3550 (let ((prefix-len (- (point) (car lit-limits)))
3554 (goto-char (car lit-limits))
3555 (indent-to (prog1 (current-column)
3558 (forward-char prefix-len)
3559 (setq str (buffer-substring-no-properties
3560 (c-point 'bol) (point))
3561 col (current-column)))
3562 (delete-region (car lit-limits) tmp))))
3565 (if (or (string-match "\\s \\'" str) (not (eolp)))
3567 ;; The prefix ends the line with no whitespace
3568 ;; after it. Default to a single space.
3569 (cons (concat str " ") (1+ col))))
3573 (setq comment-text-end
3575 (goto-char (- (cdr lit-limits) 2))
3576 (if (looking-at "\\*/") (point) (cdr lit-limits))))
3580 (if (and (> (point) (car lit-limits))
3581 (not (and (looking-at "[ \t]*\\*/")
3582 (eq (cdr lit-limits) (match-end 0)))))
3583 ;; The current line is not the comment starter and
3584 ;; contains more than just the ender, so it's good enough
3585 ;; to be used for the comment fill prefix.
3586 (setq prefix-line (point))
3587 (goto-char (car lit-limits))
3589 (cond ((or (/= (forward-line 1) 0)
3590 (>= (point) (cdr lit-limits))
3591 (and (looking-at "[ \t]*\\*/")
3592 (eq (cdr lit-limits) (match-end 0)))
3593 (and (looking-at prefix-regexp)
3594 (<= (1- (cdr lit-limits)) (match-end 0))))
3595 ;; The comment is either one line or the next line contains
3596 ;; just the comment ender. In this case we have no
3597 ;; information about a suitable comment prefix, so we resort
3598 ;; to c-block-comment-prefix.
3599 (setq comment-prefix (or c-block-comment-prefix "")))
3602 ;; The point was on the comment opener line, so we might want
3603 ;; to treat this as a not yet closed comment.
3605 (if (and (match-beginning 1)
3606 (/= (match-beginning 1) (match-end 1)))
3607 ;; Above `prefix-regexp' matched a nonempty prefix on the
3608 ;; second line, so let's use it. Normally it should do
3609 ;; to set `prefix-line' and let the code below pick up
3610 ;; the whole prefix, but if there's no text after the
3611 ;; match then it will probably fall back to no prefix at
3612 ;; all if the comment isn't closed yet, so in that case
3613 ;; it's better to force use of the prefix matched now.
3614 (if (= (match-end 0) (c-point 'eol))
3615 (setq comment-prefix (match-string 1))
3616 (setq prefix-line (point)))
3618 ;; There's no nonempty prefix on the line after the
3619 ;; comment opener. If the line is empty, or if the
3620 ;; text on it has less or equal indentation than the
3621 ;; comment starter we assume it's an unclosed
3622 ;; comment starter, i.e. that
3623 ;; `c-block-comment-prefix' should be used.
3624 ;; Otherwise we assume it's a closed comment where
3625 ;; the prefix really is the empty string.
3626 ;; E.g. this is an unclosed comment:
3637 ;; (Looking for the presence of the comment closer
3638 ;; rarely works since it's probably the closer of
3639 ;; some comment further down when the comment
3640 ;; really is unclosed.)
3641 (if (<= (save-excursion (back-to-indentation)
3643 (save-excursion (goto-char (car lit-limits))
3645 (setq comment-prefix (or c-block-comment-prefix ""))
3646 (setq prefix-line (point)))))
3649 ;; Otherwise the line after the comment starter is good
3650 ;; enough to find the prefix in.
3651 (setq prefix-line (point))))
3653 (when comment-prefix
3654 ;; Haven't got the comment prefix on any real line that we
3655 ;; can take it from, so we have to temporarily insert
3656 ;; `comment-prefix' on a line and indent it to find the
3657 ;; correct column and the correct mix of tabs and spaces.
3659 (let (tmp-pre tmp-post)
3663 (goto-char (car lit-limits))
3664 (if (looking-at comment-start-regexp)
3665 (goto-char (min (match-end 0)
3668 (skip-chars-forward " \t"))
3670 (when (eq (char-syntax (char-before)) ?\ )
3671 ;; If there's ws on the current line, we'll use it
3672 ;; instead of what's ending comment-prefix.
3673 (setq comment-prefix
3674 (concat (substring comment-prefix
3678 (buffer-substring-no-properties
3680 (skip-chars-backward " \t")
3684 (setq tmp-pre (point-marker))
3686 ;; We insert an extra non-whitespace character
3687 ;; before the line break and after comment-prefix in
3688 ;; case it's "" or ends with whitespace.
3689 (insert-and-inherit "x\n" comment-prefix "x")
3690 (setq tmp-post (point-marker))
3692 (indent-according-to-mode)
3694 (goto-char (1- tmp-post))
3695 (cons (buffer-substring-no-properties
3696 (c-point 'bol) (point))
3700 (delete-region tmp-pre tmp-post)
3701 (set-marker tmp-pre nil)
3702 (set-marker tmp-post nil))))))))))
3704 (or res ; Found a good prefix above.
3707 ;; prefix-line is the bol of a line on which we should try
3708 ;; to find the prefix.
3709 (let* (fb-string fb-endpos ; Contains any fallback prefix found.
3712 (when (and (looking-at prefix-regexp)
3713 (<= (match-end 0) comment-text-end))
3714 (unless (eq (match-end 0) (c-point 'eol))
3715 ;; The match is fine if there's text after it.
3716 (throw 'found (cons (buffer-substring-no-properties
3717 (match-beginning 0) (match-end 0))
3718 (progn (goto-char (match-end 0))
3719 (current-column)))))
3721 ;; This match is better than nothing, so let's
3722 ;; remember it in case nothing better is found
3724 (setq fb-string (buffer-substring-no-properties
3725 (match-beginning 0) (match-end 0))
3726 fb-endpos (match-end 0)))
3730 ;; Search for a line which has text after the prefix
3731 ;; so that we get the proper amount of whitespace
3732 ;; after it. We start with the current line, then
3733 ;; search backwards, then forwards.
3735 (goto-char prefix-line)
3736 (when (and (funcall test-line)
3737 (or (/= (match-end 1) (match-end 0))
3738 ;; The whitespace is sucked up by the
3739 ;; first [ \t]* glob if the prefix is empty.
3740 (and (= (match-beginning 1) (match-end 1))
3741 (/= (match-beginning 0) (match-end 0)))))
3742 ;; If the current line doesn't have text but do
3743 ;; have whitespace after the prefix, we'll use it.
3744 (throw 'found (cons fb-string
3745 (progn (goto-char fb-endpos)
3746 (current-column)))))
3748 (if (eq lit-type 'c++)
3749 ;; For line comments we can search up to and
3750 ;; including the first line.
3751 (while (and (zerop (forward-line -1))
3752 (>= (point) (car lit-limits)))
3753 (funcall test-line))
3754 ;; For block comments we must stop before the
3756 (while (and (zerop (forward-line -1))
3757 (> (point) (car lit-limits)))
3758 (funcall test-line)))
3760 (goto-char prefix-line)
3761 (while (and (zerop (forward-line 1))
3762 (< (point) (cdr lit-limits)))
3763 (funcall test-line))
3765 (goto-char prefix-line)
3769 ;; A good line wasn't found, but at least we have a
3770 ;; fallback that matches the comment prefix regexp.
3771 (cond ((or (string-match "\\s \\'" fb-string)
3773 (goto-char fb-endpos)
3775 ;; There are ws or text after the prefix, so
3777 (cons fb-string (current-column)))
3780 ;; Check if there's any whitespace padding
3781 ;; on the comment start line that we can
3782 ;; use after the prefix.
3783 (goto-char (car lit-limits))
3784 (if (looking-at comment-start-regexp)
3785 (goto-char (match-end 0))
3787 (skip-chars-forward " \t"))
3789 (eq (char-syntax (char-before)) ?\ )))
3791 (setq fb-string (buffer-substring-no-properties
3793 (skip-chars-backward " \t")
3796 (goto-char fb-endpos)
3797 (skip-chars-backward " \t")
3799 (let ((tmp (point)))
3800 ;; Got to mess in the buffer once again to
3801 ;; ensure the column gets correct. :P
3804 (insert-and-inherit fb-string)
3805 (cons (buffer-substring-no-properties
3809 (delete-region tmp (point)))))
3812 ;; Last resort: Just add a single space after
3814 (cons (concat fb-string " ")
3815 (progn (goto-char fb-endpos)
3816 (1+ (current-column)))))))
3818 ;; The line doesn't match the comment prefix regexp.
3820 ;; We have a fallback for line comments that we must use.
3821 (cons (concat (buffer-substring-no-properties
3822 prefix-line (c-point 'boi))
3824 (progn (back-to-indentation)
3825 (+ (current-column) (length comment-prefix))))
3827 ;; Assume we are dealing with a "free text" block
3828 ;; comment where the lines doesn't have any comment
3829 ;; prefix at all and we should just fill it as
3834 (defun c-mask-paragraph (fill-paragraph apply-outside-literal fun &rest args)
3835 ;; Calls FUN with ARGS ar arguments while the current paragraph is
3836 ;; masked to allow adaptive filling to work correctly. That
3837 ;; includes narrowing the buffer and, if point is inside a comment,
3838 ;; masking the comment starter and ender appropriately.
3840 ;; FILL-PARAGRAPH is non-nil if called for whole paragraph filling.
3841 ;; The position of point is then less significant when doing masking
3844 ;; If APPLY-OUTSIDE-LITERAL is nil then the function will be called
3845 ;; only if the point turns out to be inside a comment or a string.
3847 ;; Note that this function does not do any hidden buffer changes.
3850 ;; beg and end limit the region to narrow. end is a marker.
3852 ;; tmp-pre and tmp-post mark strings that are temporarily
3853 ;; inserted at the start and end of the region. tmp-pre is a
3854 ;; cons of the positions of the prepended string. tmp-post is
3855 ;; a marker pointing to the single character of the appended
3858 ;; If hang-ender-stuck isn't nil, the comment ender is
3859 ;; hanging. In that case it's set to the number of spaces
3860 ;; that should be between the text and the ender.
3862 ;; auto-fill-spaces is the exact sequence of whitespace between a
3863 ;; comment's last word and the comment ender, temporarily replaced
3864 ;; with 'x's before calling FUN when FILL-PARAGRAPH is nil.
3867 (c-lit-limits c-lit-limits)
3868 (c-lit-type c-lit-type))
3870 ;; Restore point on undo. It's necessary since we do a lot of
3871 ;; hidden inserts and deletes below that should be as transparent
3873 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
3874 (setq buffer-undo-list (cons (point) buffer-undo-list)))
3876 ;; Determine the limits and type of the containing literal (if any):
3877 ;; C-LIT-LIMITS, C-LIT-TYPE; and the limits of the current paragraph:
3879 (c-save-buffer-state ()
3881 ;; Widen to catch comment limits correctly.
3883 (unless c-lit-limits
3884 (setq c-lit-limits (c-literal-limits nil fill-paragraph)))
3885 (setq c-lit-limits (c-collect-line-comments c-lit-limits))
3887 (setq c-lit-type (c-literal-type c-lit-limits))))
3890 (unless (c-safe (backward-char)
3894 (forward-paragraph))
3895 (setq end (point-marker)))
3897 (unless (c-safe (forward-char)
3898 (backward-paragraph)
3901 (backward-paragraph))
3902 (setq beg (point))))
3906 ;; For each of the possible types of text (string, C comment ...)
3907 ;; determine BEG and END, the region we will narrow to. If we're in
3908 ;; a literal, constrain BEG and END to the limits of this literal.
3910 ;; For some of these text types, particularly a block comment, we
3911 ;; may need to massage whitespace near literal delimiters, so that
3912 ;; these don't get filled inappropriately.
3915 ((eq c-lit-type 'c++) ; Line comment.
3917 ;; Limit to the comment or paragraph end, whichever
3919 (set-marker end (min end (cdr c-lit-limits)))
3921 (when (<= beg (car c-lit-limits))
3922 ;; The region includes the comment starter, so we must
3924 (goto-char (car c-lit-limits))
3925 (back-to-indentation)
3926 (if (eq (point) (car c-lit-limits))
3927 ;; Include the first line in the region.
3928 (setq beg (c-point 'bol))
3929 ;; The first line contains code before the
3930 ;; comment. We must fake a line that doesn't.
3933 (setq apply-outside-literal t))
3935 ((eq c-lit-type 'c) ; Block comment.
3937 (or (> end (cdr c-lit-limits))
3938 (and (= end (cdr c-lit-limits))
3939 (eq (char-before end) ?/)
3940 (eq (char-before (1- end)) ?*)
3942 (> (- (cdr c-lit-limits) (car c-lit-limits)) 3)))
3943 ;; There is a comment ender, and the region includes it. If
3944 ;; it's on its own line, it stays on its own line. If it's got
3945 ;; company on the line, it keeps (at least one word of) it.
3946 ;; "=====*/" counts as a comment ender here, but "===== */"
3947 ;; doesn't and "foo*/" doesn't.
3950 (goto-char (cdr c-lit-limits))
3952 ;; The following conjunct was added to avoid an
3953 ;; "Invalid search bound (wrong side of point)"
3954 ;; error in the subsequent re-search. Maybe
3955 ;; another fix would be needed (2007-12-08).
3956 (and (> (- (cdr c-lit-limits) 2) (point))
3957 (search-forward-regexp
3958 (concat "\\=[ \t]*\\(" c-current-comment-prefix "\\)")
3959 (- (cdr c-lit-limits) 2) t)
3960 (not (search-forward-regexp
3962 (- (cdr c-lit-limits) 2) 'limit))
3963 ;; The comment ender IS on its own line. Exclude
3964 ;; this line from the filling.
3965 (set-marker end (c-point 'bol))))
3967 ;; The comment ender is hanging. Replace all space between it
3968 ;; and the last word either by one or two 'x's (when
3969 ;; FILL-PARAGRAPH is non-nil), or a row of x's the same width
3970 ;; as the whitespace (when auto filling), and include it in
3971 ;; the region. We'll change them back to whitespace
3972 ;; afterwards. The effect of this is to glue the comment
3973 ;; ender to the last word in the comment during filling.
3974 (let* ((ender-start (save-excursion
3975 (goto-char (cdr c-lit-limits))
3976 (skip-syntax-backward "^w ")
3978 (ender-column (save-excursion
3979 (goto-char ender-start)
3981 (point-rel (- ender-start here))
3985 ;; Insert a CR after the "*/", adjust END
3986 (goto-char (cdr c-lit-limits))
3987 (setq tmp-post (point-marker))
3989 (set-marker end (point))
3991 (forward-line -1) ; last line of the comment
3992 (if (and (looking-at (concat "[ \t]*\\(\\("
3993 c-current-comment-prefix
3995 (eq ender-start (match-end 0)))
3996 ;; The comment ender is prefixed by nothing but a
3997 ;; comment line prefix. IS THIS POSSIBLE? (ACM,
3998 ;; 2006/4/28). Remove it along with surrounding ws.
3999 (setq spaces (- (match-end 1) (match-end 2)))
4000 (goto-char ender-start))
4001 (skip-chars-backward " \t\r\n") ; Surely this can be
4002 ; " \t"? "*/" is NOT alone on the line (ACM, 2005/8/18)
4004 ;; What's being tested here? 2006/4/20. FIXME!!!
4005 (if (/= (point) ender-start)
4007 (if (<= here (point))
4008 ;; Don't adjust point below if it's
4009 ;; before the string we replace.
4010 (setq point-rel -1))
4011 ;; Keep one or two spaces between the
4012 ;; text and the ender, depending on how
4013 ;; many there are now.
4015 (setq spaces (- ender-column (current-column))))
4016 (setq auto-fill-spaces (c-delete-and-extract-region
4017 (point) ender-start))
4018 ;; paragraph filling condenses multiple spaces to
4019 ;; single or double spaces. auto-fill doesn't.
4024 (if sentence-end-double-space 2 1))
4026 ;; Insert the filler first to keep marks right.
4027 (insert-char ?x spaces t)
4028 (setq hang-ender-stuck spaces)
4030 (and (>= point-rel 0)
4031 (- (point) (min point-rel spaces)))))
4032 (setq point-rel nil)))
4035 ;; Point was in the middle of the string we
4036 ;; replaced above, so put it back in the same
4037 ;; relative position, counting from the end.
4038 (goto-char point-rel)))
4041 (when (<= beg (car c-lit-limits))
4042 ;; The region includes the comment starter.
4044 (goto-char (car c-lit-limits))
4045 (if (looking-at (concat "\\(" comment-start-skip "\\)$"))
4046 ;; Begin with the next line.
4047 (setq beg (c-point 'bonl))
4048 ;; Fake the fill prefix in the first line.
4051 (setq apply-outside-literal t))
4053 ((eq c-lit-type 'string) ; String.
4055 (when (>= end (cdr c-lit-limits))
4056 (goto-char (1- (cdr c-lit-limits)))
4057 (setq tmp-post (point-marker))
4059 (set-marker end (point)))
4060 (when (<= beg (car c-lit-limits))
4061 (goto-char (1+ (car c-lit-limits)))
4062 (setq beg (if (looking-at "\\\\$")
4063 ;; Leave the start line if it's
4064 ;; nothing but an escaped newline.
4067 (setq apply-outside-literal t))
4069 ((eq c-lit-type 'pound) ; Macro
4070 ;; Narrow to the macro limits if they are nearer than the
4071 ;; paragraph limits. Don't know if this is necessary but
4072 ;; do it for completeness sake (doing auto filling at all
4073 ;; inside macros is bogus to begin with since the line
4074 ;; continuation backslashes aren't handled).
4076 (c-save-buffer-state ()
4077 (c-beginning-of-macro)
4084 (set-marker end (point))))))
4087 ;; Try to avoid comments and macros in the paragraph to
4088 ;; avoid that the adaptive fill mode gets the prefix from
4090 (c-save-buffer-state nil
4093 (c-forward-syntactic-ws end)
4097 (c-backward-syntactic-ws beg)
4099 (set-marker end (point))))))
4102 ;; Temporarily insert the fill prefix after the comment
4103 ;; starter so that the first line looks like any other
4104 ;; comment line in the narrowed region.
4105 (setq fill (c-save-buffer-state nil
4106 (c-guess-fill-prefix c-lit-limits c-lit-type)))
4107 (unless (string-match (concat "\\`[ \t]*\\("
4108 c-current-comment-prefix
4111 ;; Oops, the prefix doesn't match the comment prefix
4112 ;; regexp. This could produce very confusing
4113 ;; results with adaptive fill packages together with
4114 ;; the insert prefix magic below, since the prefix
4115 ;; often doesn't appear at all. So let's warn about
4118 Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S"
4120 ;; Find the right spot on the line, break it, insert
4121 ;; the fill prefix and make sure we're back in the
4122 ;; same column by temporarily prefixing the first word
4123 ;; with a number of 'x'.
4125 (goto-char (car c-lit-limits))
4126 (if (looking-at (if (eq c-lit-type 'c++)
4127 c-current-comment-prefix
4128 comment-start-skip))
4129 (goto-char (match-end 0))
4131 (skip-chars-forward " \t"))
4132 (while (and (< (current-column) (cdr fill))
4135 (let ((col (current-column)))
4136 (setq beg (1+ (point))
4137 tmp-pre (list (point)))
4140 (insert-and-inherit "\n" (car fill))
4141 (insert-char ?x (- col (current-column)) t))
4142 (setcdr tmp-pre (point))))))
4144 (when apply-outside-literal
4145 ;; `apply-outside-literal' is always set to t here if
4146 ;; we're inside a literal.
4150 ;; Kludge: If the function that adapts the fill prefix
4151 ;; doesn't produce the required comment starter for
4152 ;; line comments, then force it by setting fill-prefix.
4153 (when (and (eq c-lit-type 'c++)
4154 ;; Kludge the kludge: filladapt-mode doesn't
4155 ;; have this problem, but it currently
4156 ;; doesn't override fill-context-prefix
4158 (not (and (boundp 'filladapt-mode)
4162 (or (fill-context-prefix beg end)
4164 (c-save-buffer-state nil
4165 (car (or fill (c-guess-fill-prefix
4166 c-lit-limits c-lit-type)))))))
4168 ;; Save the relative position of point if it's outside the
4169 ;; region we're going to narrow. Want to restore it in that
4170 ;; case, but otherwise it should be moved according to the
4172 (point-rel (cond ((< (point) beg) (- (point) beg))
4173 ((> (point) end) (- (point) end)))))
4175 ;; Preparations finally done! Now we can call the
4179 (narrow-to-region beg end)
4182 ;; Restore point if it was outside the region.
4184 (goto-char (+ beg point-rel))
4185 (goto-char (+ end point-rel))))))))
4187 (when (consp tmp-pre)
4188 (delete-region (car tmp-pre) (cdr tmp-pre)))
4192 (goto-char tmp-post)
4194 (when hang-ender-stuck
4195 ;; Preserve point even if it's in the middle of the string
4196 ;; we replace; save-excursion doesn't work in that case.
4198 (goto-char tmp-post)
4199 (skip-syntax-backward "^w ")
4200 (forward-char (- hang-ender-stuck))
4201 (if (or fill-paragraph (not auto-fill-spaces))
4202 (insert-char ?\ hang-ender-stuck t)
4203 (insert auto-fill-spaces)
4204 (setq here (- here (- hang-ender-stuck (length auto-fill-spaces)))))
4205 (delete-char hang-ender-stuck)
4207 (set-marker tmp-post nil))
4209 (set-marker end nil))))
4211 (defun c-fill-paragraph (&optional arg)
4212 "Like \\[fill-paragraph] but handles C and C++ style comments.
4213 If any of the current line is a comment or within a comment, fill the
4214 comment or the paragraph of it that point is in, preserving the
4215 comment indentation or line-starting decorations (see the
4216 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
4219 If point is inside multiline string literal, fill it. This currently
4220 does not respect escaped newlines, except for the special case when it
4221 is the very first thing in the string. The intended use for this rule
4222 is in situations like the following:
4224 char description[] = \"\\
4225 A very long description of something that you want to fill to make
4226 nicely formatted output.\"\;
4228 If point is in any other situation, i.e. in normal code, do nothing.
4230 Optional prefix ARG means justify paragraph as well."
4232 (let ((fill-paragraph-function
4233 ;; Avoid infinite recursion.
4234 (if (not (eq fill-paragraph-function 'c-fill-paragraph))
4235 fill-paragraph-function)))
4236 (c-mask-paragraph t nil 'fill-paragraph arg))
4237 ;; Always return t. This has the effect that if filling isn't done
4238 ;; above, it isn't done at all, and it's therefore effectively
4239 ;; disabled in normal code.
4242 (defun c-do-auto-fill ()
4243 ;; Do automatic filling if not inside a context where it should be
4245 (let ((c-auto-fill-prefix
4246 ;; The decision whether the line should be broken is actually
4247 ;; done in c-indent-new-comment-line, which do-auto-fill
4248 ;; calls to break lines. We just set this special variable
4249 ;; so that we'll know when we're called from there. It's
4250 ;; also used to detect whether fill-prefix is user set or
4251 ;; generated automatically by do-auto-fill.
4253 (c-mask-paragraph nil t 'do-auto-fill)))
4255 (defun c-indent-new-comment-line (&optional soft allow-auto-fill)
4256 "Break line at point and indent, continuing comment or macro if within one.
4257 If inside a comment and `comment-multi-line' is non-nil, the
4258 indentation and line prefix are preserved (see the
4259 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
4260 details). If inside a single line comment and `comment-multi-line' is
4261 nil, a new comment of the same type is started on the next line and
4262 indented as appropriate for comments. If inside a macro, a line
4263 continuation backslash is inserted and aligned as appropriate, and the
4264 new line is indented according to `c-syntactic-indentation'.
4266 If a fill prefix is specified, it overrides all the above."
4267 ;; allow-auto-fill is used from c-context-line-break to allow auto
4268 ;; filling to break the line more than once. Since this function is
4269 ;; used from auto-fill itself, that's normally disabled to avoid
4270 ;; unnecessary recursion.
4272 (let ((fill-prefix fill-prefix)
4275 (delete-horizontal-space)
4277 (insert-and-inherit ?\n)
4278 (newline (if allow-auto-fill nil 1)))))
4279 ;; Already know the literal type and limits when called from
4280 ;; c-context-line-break.
4281 (c-lit-limits c-lit-limits)
4282 (c-lit-type c-lit-type)
4283 (c-macro-start c-macro-start))
4285 (c-save-buffer-state ()
4286 (when (not (eq c-auto-fill-prefix t))
4287 ;; Called from do-auto-fill.
4288 (unless c-lit-limits
4289 (setq c-lit-limits (c-literal-limits nil nil t)))
4291 (setq c-lit-type (c-literal-type c-lit-limits)))
4292 (if (memq (cond ((c-query-and-set-macro-start) 'cpp)
4293 ((null c-lit-type) 'code)
4296 (setq fill-prefix t) ; Used as flag in the cond.
4297 (if (and (null c-auto-fill-prefix)
4299 (<= (c-point 'bol) (car c-lit-limits)))
4300 ;; The adaptive fill function has generated a prefix, but
4301 ;; we're on the first line in a block comment so it'll be
4302 ;; wrong. Ignore it to guess a better one below.
4303 (setq fill-prefix nil)
4304 (when (and (eq c-lit-type 'c++)
4305 (not (string-match (concat "\\`[ \t]*"
4306 c-line-comment-starter)
4307 (or fill-prefix ""))))
4308 ;; Kludge: If the function that adapted the fill prefix
4309 ;; doesn't produce the required comment starter for line
4310 ;; comments, then we ignore it.
4311 (setq fill-prefix nil)))
4314 (cond ((eq fill-prefix t)
4315 ;; A call from do-auto-fill which should be ignored.
4318 ;; A fill-prefix overrides anything.
4319 (funcall do-line-break)
4320 (insert-and-inherit fill-prefix))
4321 ((c-save-buffer-state ()
4322 (unless c-lit-limits
4323 (setq c-lit-limits (c-literal-limits)))
4325 (setq c-lit-type (c-literal-type c-lit-limits)))
4326 (memq c-lit-type '(c c++)))
4327 ;; Some sort of comment.
4328 (if (or comment-multi-line
4330 (goto-char (car c-lit-limits))
4332 (< (point) (cdr c-lit-limits))))
4333 ;; Inside a comment that should be continued.
4334 (let ((fill (c-save-buffer-state nil
4335 (c-guess-fill-prefix
4337 (c-collect-line-comments c-lit-limits))
4340 (start-col (current-column))
4342 (or (and (eq c-lit-type 'c)
4344 (goto-char (- (cdr c-lit-limits) 2))
4345 (if (looking-at "\\*/") (point))))
4346 (cdr c-lit-limits))))
4347 ;; Skip forward past the fill prefix in case
4348 ;; we're standing in it.
4350 ;; FIXME: This doesn't work well in cases like
4352 ;; /* Bla bla bla bla bla
4355 ;; If point is on the 'B' then the line will be
4356 ;; broken after "Bla b".
4358 ;; If we have an empty comment, /* */, the next
4359 ;; lot of code pushes point to the */. We fix
4360 ;; this by never allowing point to end up to the
4361 ;; right of where it started.
4362 (while (and (< (current-column) (cdr fill))
4365 (if (and (> (point) comment-text-end)
4366 (> (c-point 'bol) (car c-lit-limits)))
4368 ;; The skip takes us out of the (block)
4369 ;; comment; insert the fill prefix at bol
4370 ;; instead and keep the position.
4371 (setq pos (copy-marker pos t))
4373 (insert-and-inherit (car fill))
4374 (if soft (insert-and-inherit ?\n) (newline 1))
4376 (set-marker pos nil))
4377 ;; Don't break in the middle of a comment starter
4379 (cond ((> (point) comment-text-end)
4380 (goto-char comment-text-end))
4381 ((< (point) (+ (car c-lit-limits) 2))
4382 (goto-char (+ (car c-lit-limits) 2))))
4383 (funcall do-line-break)
4384 (insert-and-inherit (car fill))
4385 (if (> (current-column) start-col)
4386 (move-to-column start-col)))) ; can this hit the
4388 ;; Inside a comment that should be broken.
4389 (let ((comment-start comment-start)
4390 (comment-end comment-end)
4392 (if (eq c-lit-type 'c)
4393 (unless (string-match "[ \t]*/\\*" comment-start)
4394 (setq comment-start "/* " comment-end " */"))
4395 (unless (string-match "[ \t]*//" comment-start)
4396 (setq comment-start "// " comment-end "")))
4397 (setq col (save-excursion
4398 (back-to-indentation)
4400 (funcall do-line-break)
4401 (when (and comment-end (not (equal comment-end "")))
4403 (insert-and-inherit comment-end)
4405 ;; c-comment-indent may look at the current
4406 ;; indentation, so let's start out with the same
4407 ;; indentation as the previous one.
4409 (insert-and-inherit comment-start)
4410 (indent-for-comment))))
4411 ((c-query-and-set-macro-start)
4413 (unless (looking-at "[ \t]*\\\\$")
4414 ;; Do not clobber the alignment of the line continuation
4415 ;; slash; c-backslash-region might look at it.
4416 (delete-horizontal-space))
4417 ;; Got an asymmetry here: In normal code this command
4418 ;; doesn't indent the next line syntactically, and otoh a
4419 ;; normal syntactically indenting newline doesn't continue
4421 (c-newline-and-indent (if allow-auto-fill nil 1)))
4423 ;; Somewhere else in the code.
4424 (let ((col (save-excursion
4426 (while (and (looking-at "[ \t]*\\\\?$")
4427 (= (forward-line -1) 0)))
4428 (current-indentation))))
4429 (funcall do-line-break)
4430 (indent-to col))))))
4432 (defalias 'c-comment-line-break-function 'c-indent-new-comment-line)
4433 (make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line)
4435 ;; advice for indent-new-comment-line for older Emacsen
4436 (unless (boundp 'comment-line-break-function)
4437 (defvar c-inside-line-break-advice nil)
4438 (defadvice indent-new-comment-line (around c-line-break-advice
4439 activate preactivate)
4440 "Call `c-indent-new-comment-line' if in CC Mode."
4441 (if (or c-inside-line-break-advice
4442 (not c-buffer-is-cc-mode))
4444 (let ((c-inside-line-break-advice t))
4445 (c-indent-new-comment-line (ad-get-arg 0))))))
4447 (defun c-context-line-break ()
4448 "Do a line break suitable to the context.
4450 When point is outside a comment or macro, insert a newline and indent
4451 according to the syntactic context, unless `c-syntactic-indentation'
4452 is nil, in which case the new line is indented as the previous
4453 non-empty line instead.
4455 When point is inside the content of a preprocessor directive, a line
4456 continuation backslash is inserted before the line break and aligned
4457 appropriately. The end of the cpp directive doesn't count as inside
4460 When point is inside a comment, continue it with the appropriate
4461 comment prefix (see the `c-comment-prefix-regexp' and
4462 `c-block-comment-prefix' variables for details). The end of a
4463 C++-style line comment doesn't count as inside it.
4465 When point is inside a string, only insert a backslash when it is also
4466 inside a preprocessor directive."
4469 (let* (c-lit-limits c-lit-type
4470 (c-macro-start c-macro-start))
4472 (c-save-buffer-state ()
4473 (setq c-lit-limits (c-literal-limits nil nil t)
4474 c-lit-type (c-literal-type c-lit-limits))
4475 (when (eq c-lit-type 'c++)
4476 (setq c-lit-limits (c-collect-line-comments c-lit-limits)))
4477 (c-query-and-set-macro-start))
4480 ((or (eq c-lit-type 'c)
4481 (and (eq c-lit-type 'c++) ; C++ comment, but not at the very end of it.
4483 (skip-chars-forward " \t")
4485 (1- (cdr c-lit-limits))))
4486 (and (numberp c-macro-start) ; Macro, but not at the very end of
4487 ; it, not in a string, and not in the
4489 (not (eq c-lit-type 'string))
4490 (or (not (looking-at "\\s *$"))
4491 (eq (char-before) ?\\))
4493 (goto-char c-macro-start)
4494 (if (looking-at c-opt-cpp-start)
4495 (goto-char (match-end 0)))
4498 (let ((comment-multi-line t)
4500 (c-indent-new-comment-line nil t)))
4502 ((eq c-lit-type 'string)
4503 (if (and (numberp c-macro-start)
4504 (not (eq (char-before) ?\\)))
4508 (t (delete-horizontal-space)
4510 ;; c-indent-line may look at the current indentation, so let's
4511 ;; start out with the same indentation as the previous line.
4512 (let ((col (save-excursion
4515 (while (and (looking-at "[ \t]*\\\\?$")
4516 (= (forward-line -1) 0)))
4517 (current-indentation))))
4519 (indent-according-to-mode)))))
4521 (defun c-context-open-line ()
4522 "Insert a line break suitable to the context and leave point before it.
4523 This is the `c-context-line-break' equivalent to `open-line', which is
4524 normally bound to C-o. See `c-context-line-break' for the details."
4526 (let ((here (point)))
4529 ;; Temporarily insert a non-whitespace char to keep any
4530 ;; preceding whitespace intact.
4532 (c-context-line-break))
4537 (cc-provide 'cc-cmds)
4539 ;; arch-tag: bf0611dc-d1f4-449e-9e45-4ec7c6936677
4540 ;;; cc-cmds.el ends here