1 ;;; cc-cmds.el --- user level commands for CC Mode
3 ;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
5 ;; Authors: 2000- Martin Stjernholm
6 ;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
7 ;; 1992-1997 Barry A. Warsaw
8 ;; 1987 Dave Detlefs and Stewart Clamen
9 ;; 1985 Richard M. Stallman
10 ;; Maintainer: bug-cc-mode@gnu.org
11 ;; Created: 22-Apr-1997 (split from cc-mode.el)
12 ;; Version: See cc-mode.el
13 ;; Keywords: c languages oop
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA.
38 (if (and (boundp 'byte-compile-dest-file
)
39 (stringp byte-compile-dest-file
))
40 (cons (file-name-directory byte-compile-dest-file
) load-path
)
42 (require 'cc-bytecomp
)))
46 (cc-require 'cc-langs
)
47 (cc-require 'cc-engine
)
49 ;; Silence the compiler.
50 (cc-bytecomp-defvar delete-key-deletes-forward
) ; XEmacs 20+
51 (cc-bytecomp-defun delete-forward-p) ; XEmacs 21+
52 (cc-bytecomp-obsolete-fun insert-and-inherit
) ; Marked obsolete in XEmacs 19
53 (cc-bytecomp-defvar filladapt-mode
) ; c-fill-paragraph contains a kludge
54 ; which looks at this.
57 (defvar c-fix-backslashes t
)
59 (defun c-shift-line-indentation (shift-amt)
60 (let ((pos (- (point-max) (point)))
61 (c-macro-start c-macro-start
)
65 (when (and (c-query-and-set-macro-start)
66 (looking-at "[ \t]*\\\\$")
68 (skip-chars-backward " \t")
72 (setq tmp-char-inserted t
))
74 (let ((col (current-indentation)))
75 (delete-region (c-point 'bol
) (c-point 'boi
))
77 (indent-to (+ col shift-amt
)))
78 (when tmp-char-inserted
80 ;; If initial point was within line's indentation and we're not on
81 ;; a line with a line continuation in a macro, position after the
82 ;; indentation. Else stay at same point in text.
83 (if (and (< (point) (c-point 'boi
))
84 (not tmp-char-inserted
))
86 (if (> (- (point-max) pos
) (point))
87 (goto-char (- (point-max) pos
))))))
89 (defun c-indent-line (&optional syntax quiet ignore-point-pos
)
90 "Indent the current line according to the syntactic context,
91 if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the
92 syntactic information for the current line. Be silent about syntactic
93 errors if the optional argument QUIET is non-nil, even if
94 `c-report-syntactic-errors' is non-nil. Normally the position of
95 point is used to decide where the old indentation is on a lines that
96 is otherwise empty \(ignoring any line continuation backslash), but
97 that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of
98 indentation change \(in columns)."
99 (let ((line-cont-backslash (save-excursion
101 (eq (char-before) ?
\\)))
102 (c-fix-backslashes c-fix-backslashes
)
105 (when (and (not ignore-point-pos
)
108 (looking-at (if line-cont-backslash
111 (<= (point) (match-end 1)))
112 ;; Delete all whitespace after point if there's only whitespace
113 ;; on the line, so that any code that does back-to-indentation
114 ;; or similar gets the current column in this case. If this
115 ;; removes a line continuation backslash it'll be restored
117 (unless c-auto-align-backslashes
118 ;; Should try to keep the backslash alignment
121 (goto-char (match-end 0))
122 (setq bs-col
(1- (current-column)))))
123 (delete-region (point) (match-end 0))
124 (setq c-fix-backslashes t
))
125 (if c-syntactic-indentation
126 (setq c-parsing-error
127 (or (let* ((c-parsing-error nil
)
128 (c-syntactic-context (or syntax
130 (c-guess-basic-syntax)))
132 (setq indent
(c-get-syntactic-indentation
133 c-syntactic-context
))
134 (and (not (c-echo-parsing-error quiet
))
135 c-echo-syntactic-information-p
136 (message "syntax: %s, indent: %d"
137 c-syntactic-context indent
))
138 (setq shift-amt
(- indent
(current-indentation)))
139 (c-shift-line-indentation shift-amt
)
140 (run-hooks 'c-special-indent-hook
)
145 (while (and (= (forward-line -
1) 0)
146 (if (looking-at "\\s *\\\\?$")
148 (setq indent
(current-indentation))
150 (setq shift-amt
(- indent
(current-indentation)))
151 (c-shift-line-indentation shift-amt
)))
152 (when (and c-fix-backslashes line-cont-backslash
)
157 (when c-auto-align-backslashes
158 ;; Realign the line continuation backslash.
159 (c-backslash-region (point) (point) nil t
))))
162 (defun c-newline-and-indent (&optional newline-arg
)
163 "Inserts a newline and indents the new line.
164 This function fixes line continuation backslashes if inside a macro,
165 and takes care to set the indentation before calling
166 `indent-according-to-mode', so that lineup functions like
167 `c-lineup-dont-change' works better."
168 ;; FIXME: Backslashes before eol in comments and literals aren't
170 (let ((c-macro-start (c-query-macro-start))
171 ;; Avoid calling c-backslash-region from c-indent-line if it's
172 ;; called during the newline call, which can happen due to
173 ;; c-electric-continued-statement, for example. We also don't
174 ;; want any backslash alignment from indent-according-to-mode.
175 (c-fix-backslashes nil
)
176 has-backslash insert-backslash
181 (while (and (looking-at "[ \t]*\\\\?$")
182 (= (forward-line -
1) 0)))
183 (setq col
(current-indentation)))
185 (if (and (eolp) (eq (char-before) ?
\\))
186 (setq insert-backslash t
188 (setq has-backslash
(eq (char-before (c-point 'eol
)) ?
\\))))
189 (newline newline-arg
)
194 ;; The backslash stayed on the previous line. Insert one
195 ;; before calling c-backslash-region, so that
196 ;; bs-col-after-end in it works better. Fixup the
197 ;; backslashes on the newly inserted line.
200 (c-backslash-region (point) (point) nil t
))
201 ;; The backslash moved to the new line, if there was any. Let
202 ;; c-backslash-region fix a backslash on the previous line,
203 ;; and the one that might be on the new line.
204 ;; c-auto-align-backslashes is intentionally ignored here;
205 ;; maybe the moved backslash should be left alone if it's set,
206 ;; but we fix both lines on the grounds that the old backslash
207 ;; has been moved anyway and is now in a different context.
208 (c-backslash-region start
(if has-backslash
(point) start
) nil t
)))
209 (when c-syntactic-indentation
210 ;; Reindent syntactically. The indentation done above is not
211 ;; wasted, since c-indent-line might look at the current
213 (let ((c-syntactic-context (c-guess-basic-syntax)))
214 ;; We temporarily insert another line break, so that the
215 ;; lineup functions will see the line as empty. That makes
216 ;; e.g. c-lineup-cpp-define more intuitive since it then
217 ;; proceeds to the preceding line in this case.
219 (delete-horizontal-space)
220 (setq start
(- (point-max) (point)))
224 (indent-according-to-mode))
225 (goto-char (- (point-max) start
))
228 ;; Must align the backslash again after reindentation. The
229 ;; c-backslash-region call above can't be optimized to ignore
230 ;; this line, since it then won't align correctly with the
231 ;; lines below if the first line in the macro is broken.
232 (c-backslash-region (point) (point) nil t
)))))
234 (defun c-show-syntactic-information (arg)
235 "Show syntactic information for current line.
236 With universal argument, inserts the analysis as a comment on that line."
238 (let* ((c-parsing-error nil
)
239 (syntax (c-guess-basic-syntax)))
240 (if (not (consp arg
))
241 (message "syntactic analysis: %s" syntax
)
243 (insert-and-inherit (format "%s" syntax
))
245 (c-keep-region-active))
247 (defun c-syntactic-information-on-region (from to
)
248 "Inserts a comment with the syntactic analysis on every line in the region."
252 (narrow-to-region from to
)
253 (goto-char (point-min))
255 (c-show-syntactic-information '(0))
259 (defun c-toggle-syntactic-indentation (&optional arg
)
260 "Toggle syntactic indentation.
261 Optional numeric ARG, if supplied, turns on syntactic indentation when
262 positive, turns it off when negative, and just toggles it when zero or
265 When syntactic indentation is turned on (the default), the indentation
266 functions and the electric keys indent according to the syntactic
267 context keys, when applicable.
269 When it's turned off, the electric keys does no reindentation, the
270 indentation functions indents every new line to the same level as the
271 previous nonempty line, and \\[c-indent-command] adjusts the
272 indentation in seps specified `c-basic-offset'. The indentation style
273 has no effect in this mode, nor any of the indentation associated
274 variables, e.g. `c-special-indent-hook'.
276 This command sets the variable `c-syntactic-indentation'."
278 (setq c-syntactic-indentation
279 (c-calculate-state arg c-syntactic-indentation
))
280 (c-keep-region-active))
282 (defun c-toggle-auto-state (&optional arg
)
283 "Toggle auto-newline feature.
284 Optional numeric ARG, if supplied, turns on auto-newline when
285 positive, turns it off when negative, and just toggles it when zero or
288 When the auto-newline feature is enabled (as evidenced by the `/a' or
289 `/ah' on the modeline after the mode name) newlines are automatically
290 inserted after special characters such as brace, comma, semi-colon,
293 (setq c-auto-newline
(c-calculate-state arg c-auto-newline
))
295 (c-keep-region-active))
297 (defun c-toggle-hungry-state (&optional arg
)
298 "Toggle hungry-delete-key feature.
299 Optional numeric ARG, if supplied, turns on hungry-delete when
300 positive, turns it off when negative, and just toggles it when zero or
303 When the hungry-delete-key feature is enabled (as evidenced by the
304 `/h' or `/ah' on the modeline after the mode name) the delete key
305 gobbles all preceding whitespace in one fell swoop."
307 (setq c-hungry-delete-key
(c-calculate-state arg c-hungry-delete-key
))
309 (c-keep-region-active))
311 (defun c-toggle-auto-hungry-state (&optional arg
)
312 "Toggle auto-newline and hungry-delete-key features.
313 Optional numeric ARG, if supplied, turns on auto-newline and
314 hungry-delete when positive, turns them off when negative, and just
315 toggles them when zero or left out.
317 See `c-toggle-auto-state' and `c-toggle-hungry-state' for details."
319 (setq c-auto-newline
(c-calculate-state arg c-auto-newline
))
320 (setq c-hungry-delete-key
(c-calculate-state arg c-hungry-delete-key
))
322 (c-keep-region-active))
327 (defun c-electric-backspace (arg)
328 "Deletes preceding character or whitespace.
329 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
330 \"/ah\" string on the mode line, then all preceding whitespace is
331 consumed. If however a prefix argument is supplied, or
332 `c-hungry-delete-key' is nil, or point is inside a literal then the
333 function in the variable `c-backspace-function' is called.
335 See also \\[c-electric-delete]."
337 (if (or (not c-hungry-delete-key
)
340 (funcall c-backspace-function
(prefix-numeric-value arg
))
341 (let ((here (point)))
343 (if (/= (point) here
)
344 (delete-region (point) here
)
345 (funcall c-backspace-function
1)
348 (defun c-electric-delete-forward (arg)
349 "Deletes following character or whitespace.
350 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
351 \"/ah\" string on the mode line, then all following whitespace is
352 consumed. If however a prefix argument is supplied, or
353 `c-hungry-delete-key' is nil, or point is inside a literal then the
354 function in the variable `c-delete-function' is called."
356 (if (or (not c-hungry-delete-key
)
359 (funcall c-delete-function
(prefix-numeric-value arg
))
360 (let ((here (point)))
362 (if (/= (point) here
)
363 (delete-region (point) here
)
364 (funcall c-delete-function
1)))))
366 (defun c-electric-delete (arg)
367 "Deletes preceding or following character or whitespace.
368 This function either deletes forward as `c-electric-delete-forward' or
369 backward as `c-electric-backspace', depending on the configuration:
371 If the function `delete-forward-p' is defined (XEmacs 21) and returns
372 non-nil, it deletes forward. Else, if the variable
373 `delete-key-deletes-forward' is defined (XEmacs 20) and is set to
374 non-nil, it deletes forward. Otherwise it deletes backward.
376 Note: This is the way in XEmacs 20 and later to choose the correct
377 action for the [delete] key, whichever key that means. In other
378 flavors this function isn't used, instead it's left to the user to
379 bind [delete] to either \\[c-electric-delete-forward] or \\[c-electric-backspace] as appropriate
380 \(the keymap `function-key-map' is useful for that). Emacs 21 handles
381 that automatically, though."
383 (if (or (and (fboundp 'delete-forward-p
) ;XEmacs 21
385 (and (boundp 'delete-key-deletes-forward
) ;XEmacs 20
386 delete-key-deletes-forward
))
387 (c-electric-delete-forward arg
)
388 (c-electric-backspace arg
)))
390 (defun c-electric-pound (arg)
391 "Electric pound (`#') insertion.
392 Inserts a `#' character specially depending on the variable
393 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if
394 point is inside a literal or a macro, nothing special happens."
397 (not (memq 'alignleft c-electric-pound-behavior
))
399 (skip-chars-backward " \t")
402 (and (= (forward-line -
1) 0)
404 (eq (char-before) ?
\\))))
406 ;; do nothing special
407 (self-insert-command (prefix-numeric-value arg
))
408 ;; place the pound character at the left edge
409 (let ((pos (- (point-max) (point)))
412 (delete-horizontal-space)
413 (insert last-command-char
)
415 (goto-char (- (point-max) pos
)))
418 (defun c-electric-brace (arg)
421 If the auto-newline feature is turned on, as evidenced by the \"/a\"
422 or \"/ah\" string on the mode line, newlines are inserted before and
423 after braces based on the value of `c-hanging-braces-alist'.
425 Also, the line is re-indented unless a numeric ARG is supplied, the
426 brace is inserted inside a literal, or `c-syntactic-indentation' is
429 This function does various newline cleanups based on the value of
432 (let* ((safepos (c-safe-position (point) (c-parse-state)))
433 (literal (c-in-literal safepos
))
434 ;; We want to inhibit blinking the paren since this will be
435 ;; most disruptive. We'll blink it ourselves later on.
436 (old-blink-paren blink-paren-function
)
437 blink-paren-function
)
440 (self-insert-command (prefix-numeric-value arg
)))
441 ((not (looking-at "[ \t]*\\\\?$"))
442 (self-insert-command (prefix-numeric-value arg
))
443 (if c-syntactic-indentation
444 (indent-according-to-mode)))
447 ;; This is the list of brace syntactic symbols that can
448 ;; hang. If any new ones are added to c-offsets-alist,
449 ;; they should be added here as well.
450 '(class-open class-close defun-open defun-close
451 inline-open inline-close
452 brace-list-open brace-list-close
453 brace-list-intro brace-entry-open
454 block-open block-close
455 substatement-open statement-case-open
456 extern-lang-open extern-lang-close
457 namespace-open namespace-close
458 inexpr-class-open inexpr-class-close
460 (insertion-point (point))
461 (preserve-p (and (not (bobp))
462 (eq ?\
(char-syntax (char-before)))))
464 (c-echo-syntactic-information-p nil
)
465 delete-temp-newline syntax newlines
)
466 ;; only insert a newline if there is non-whitespace behind us
467 (when (save-excursion
468 (skip-chars-backward " \t")
470 (setq delete-temp-newline
471 (list (point-marker)))
472 (c-newline-and-indent)
473 (setcdr delete-temp-newline
(point-marker)))
476 (if (eq last-command-char ?
{)
477 (setq c-state-cache
(cons (point) c-state-cache
)))
478 (self-insert-command (prefix-numeric-value arg
))
479 (let ((c-syntactic-indentation-in-macros t
))
480 ;; Turn on syntactic macro analysis to help with auto
482 (setq syntax
(c-guess-basic-syntax)))
488 ;; Substitute inexpr-class and class-open or
489 ;; class-close with inexpr-class-open or
490 ;; inexpr-class-close.
491 (if (assq 'inexpr-class syntax
)
492 (cond ((assq 'class-open syntax
)
493 '((inexpr-class-open)))
494 ((assq 'class-close syntax
)
495 '((inexpr-class-close)))
498 c-hanging-braces-alist
)
499 '(ignore before after
))))
500 ;; Do not try to insert newlines around a special
501 ;; (Pike-style) brace list.
502 (if (and c-special-brace-lists
504 (c-safe (if (= (char-before) ?
{)
507 (c-looking-at-special-brace-list))))
509 ;; If syntax is a function symbol, then call it using the
510 ;; defined semantics.
511 (if (and (not (consp (cdr newlines
)))
512 (functionp (cdr newlines
)))
513 (let ((c-syntactic-context syntax
))
515 (funcall (cdr newlines
)
518 ;; does a newline go before the open brace?
519 (when (memq 'before newlines
)
520 ;; we leave the newline we've put in there before,
521 ;; but we need to re-indent the line above
522 (when delete-temp-newline
523 (set-marker (car delete-temp-newline
) nil
)
524 (set-marker (cdr delete-temp-newline
) nil
)
525 (setq delete-temp-newline nil
))
526 (when c-syntactic-indentation
527 (let ((pos (- (point-max) (point)))
530 (indent-according-to-mode)
531 (goto-char (- (point-max) pos
))
532 ;; if the buffer has changed due to the
533 ;; indentation, we need to recalculate syntax for
535 (if (/= (point) here
)
536 (let ((c-syntactic-indentation-in-macros t
))
537 ;; Turn on syntactic macro analysis to help
538 ;; with auto newlines only.
539 (setq syntax
(c-guess-basic-syntax))))))))
540 ;; must remove the newline we just stuck in (if we really did it)
541 (when delete-temp-newline
543 (delete-region (car delete-temp-newline
)
544 (cdr delete-temp-newline
))
545 (goto-char (car delete-temp-newline
))
546 (set-marker (car delete-temp-newline
) nil
)
547 (set-marker (cdr delete-temp-newline
) nil
)
548 ;; if there is whitespace before point, then preserve
549 ;; at least one space.
553 (if (not (memq 'before newlines
))
554 ;; since we're hanging the brace, we need to recalculate
556 (let ((c-syntactic-indentation-in-macros t
))
557 ;; Turn on syntactic macro analysis to help with auto
559 (setq syntax
(c-guess-basic-syntax))))
560 (when c-syntactic-indentation
561 ;; Now adjust the line's indentation. Don't update the state
562 ;; cache since c-guess-basic-syntax isn't called when
563 ;; c-syntactic-context is set.
564 (let* ((c-syntactic-context syntax
))
565 (indent-according-to-mode)))
566 ;; Do all appropriate clean ups
568 (pos (- (point-max) (point)))
570 ;; clean up empty defun braces
571 (if (and c-auto-newline
572 (memq 'empty-defun-braces c-cleanup-list
)
573 (eq last-command-char ?\
})
574 (c-intersect-lists '(defun-close class-close inline-close
)
579 (eq (char-before) ?\
{))
580 ;; make sure matching open brace isn't in a comment
581 (not (c-in-literal)))
582 (delete-region (point) (1- here
)))
583 ;; clean up brace-else-brace and brace-elseif-brace
584 (when (and c-auto-newline
585 (eq last-command-char ?\
{))
587 ((and (memq 'brace-else-brace c-cleanup-list
)
590 "\\([ \t\n]\\|\\\\\n\\)*"
592 "\\([ \t\n]\\|\\\\\n\\)*"
596 (setq mbeg
(match-beginning 0)
598 (eq (match-end 0) here
)))
599 (delete-region mbeg mend
)
600 (insert-and-inherit "} else {"))
601 ((and (memq 'brace-elseif-brace c-cleanup-list
)
603 (goto-char (1- here
))
607 (eq (char-before) ?\
)))
608 (= (c-backward-token-1 1 t
) 0)
609 (eq (char-after) ?\
()
614 "\\([ \t\n]\\|\\\\\n\\)*"
616 "\\([ \t\n]\\|\\\\\n\\)+"
618 "\\([ \t\n]\\|\\\\\n\\)*")
620 (eq (match-end 0) tmp
))
621 (delete-region mbeg mend
)
624 (goto-char (- (point-max) pos
))
626 ;; does a newline go after the brace?
627 (if (memq 'after newlines
)
628 (c-newline-and-indent))
631 (and (eq last-command-char ?\
})
632 (not executing-kbd-macro
)
635 (c-backward-syntactic-ws safepos
)
636 (funcall old-blink-paren
)))))
638 (defun c-electric-slash (arg)
639 "Insert a slash character.
641 Indent the line as a comment, if:
643 1. The slash is second of a `//' line oriented comment introducing
644 token and we are on a comment-only-line, or
646 2. The slash is part of a `*/' token that closes a block oriented
649 If a numeric ARG is supplied, point is inside a literal, or
650 `c-syntactic-indentation' is nil, indentation is inhibited."
652 (let* ((ch (char-before))
653 (literal (c-in-literal))
654 (indentp (and c-syntactic-indentation
656 (eq last-command-char ?
/)
663 (c-echo-syntactic-information-p nil
))
664 (self-insert-command (prefix-numeric-value arg
))
666 (indent-according-to-mode))))
668 (defun c-electric-star (arg)
669 "Insert a star character.
670 If the star is the second character of a C style comment introducing
671 construct, and we are on a comment-only-line, indent line as comment.
672 If a numeric ARG is supplied, point is inside a literal, or
673 `c-syntactic-indentation' is nil, indentation is inhibited."
675 (self-insert-command (prefix-numeric-value arg
))
676 ;; if we are in a literal, or if arg is given do not re-indent the
677 ;; current line, unless this star introduces a comment-only line.
678 (if (and c-syntactic-indentation
680 (eq (c-in-literal) 'c
)
681 (eq (char-before) ?
*)
684 (skip-chars-backward "*")
685 (if (eq (char-before) ?
/)
687 (skip-chars-backward " \t")
689 (let (c-echo-syntactic-information-p) ; shut this up
690 (indent-according-to-mode))
693 (defun c-electric-semi&comma
(arg)
694 "Insert a comma or semicolon.
695 When the auto-newline feature is turned on, as evidenced by the \"/a\"
696 or \"/ah\" string on the mode line, a newline might be inserted. See
697 the variable `c-hanging-semi&comma-criteria' for how newline insertion
700 When a semicolon is inserted, the line is re-indented unless a numeric
701 arg is supplied, point is inside a literal, or
702 `c-syntactic-indentation' is nil.
704 Based on the value of `c-cleanup-list', this function cleans up commas
705 following brace lists and semicolons following defuns."
707 (let* ((lim (c-most-enclosing-brace (c-parse-state)))
708 (literal (c-in-literal lim
))
711 (c-echo-syntactic-information-p nil
))
713 (self-insert-command (prefix-numeric-value arg
))
714 ;; do some special stuff with the character
715 (self-insert-command (prefix-numeric-value arg
))
716 ;; do all cleanups and newline insertions if c-auto-newline is
718 (if (or (not c-auto-newline
)
719 (not (looking-at "[ \t]*\\\\?$")))
720 (if c-syntactic-indentation
721 (indent-according-to-mode))
723 (let ((pos (- (point-max) (point))))
725 (eq last-command-char ?
,)
726 (memq 'list-close-comma c-cleanup-list
))
728 (eq last-command-char ?\
;)
729 (memq 'defun-close-semi c-cleanup-list
)))
733 (eq (char-before) ?
}))
734 ;; make sure matching open brace isn't in a comment
735 (not (c-in-literal lim
)))
736 (delete-region (point) here
))
737 (goto-char (- (point-max) pos
)))
739 (if c-syntactic-indentation
740 (indent-according-to-mode))
741 ;; check to see if a newline should be added
742 (let ((criteria c-hanging-semi
&comma-criteria
)
743 answer add-newline-p
)
745 (setq answer
(funcall (car criteria
)))
746 ;; only nil value means continue checking
748 (setq criteria
(cdr criteria
))
750 ;; only 'stop specifically says do not add a newline
751 (setq add-newline-p
(not (eq answer
'stop
)))
754 (c-newline-and-indent))
757 (defun c-electric-colon (arg)
760 If the auto-newline feature is turned on, as evidenced by the \"/a\"
761 or \"/ah\" string on the mode line, newlines are inserted before and
762 after colons based on the value of `c-hanging-colons-alist'.
764 Also, the line is re-indented unless a numeric ARG is supplied, the
765 colon is inserted inside a literal, or `c-syntactic-indentation' is
768 This function cleans up double colon scope operators based on the
769 value of `c-cleanup-list'."
771 (let* ((bod (c-point 'bod
))
772 (literal (c-in-literal bod
))
775 (c-echo-syntactic-information-p nil
))
778 (self-insert-command (prefix-numeric-value arg
)))
779 ((not (looking-at "[ \t]*\\\\?$"))
780 (self-insert-command (prefix-numeric-value arg
))
781 (if c-syntactic-indentation
782 (indent-according-to-mode)))
784 ;; insert the colon, then do any specified cleanups
785 (self-insert-command (prefix-numeric-value arg
))
786 (let ((pos (- (point-max) (point)))
788 (if (and c-auto-newline
789 (memq 'scope-operator c-cleanup-list
)
790 (eq (char-before) ?
:)
794 (eq (char-before) ?
:))
796 (not (eq (char-after (- (point) 2)) ?
:)))
798 (delete-region (point) (1- here
))
799 (setq is-scope-op t
)))
800 (goto-char (- (point-max) pos
)))
801 ;; indent the current line if it's done syntactically.
802 (if c-syntactic-indentation
803 ;; Cannot use the same syntax analysis as we find below,
804 ;; since that's made with c-syntactic-indentation-in-macros
806 (indent-according-to-mode))
807 (let* ((c-syntactic-indentation-in-macros t
)
808 ;; Turn on syntactic macro analysis to help with auto newlines
810 (syntax (c-guess-basic-syntax))
812 ;; Translate substatement-label to label for this operation.
814 (if (eq (car (car elem
)) 'substatement-label
)
815 (setcar (car elem
) 'label
))
816 (setq elem
(cdr elem
)))
817 ;; some language elements can only be determined by checking
818 ;; the following line. Lets first look for ones that can be
819 ;; found when looking on the line with the colon
822 (or (c-lookup-lists '(case-label label access-label
)
823 syntax c-hanging-colons-alist
)
824 (c-lookup-lists '(member-init-intro inher-intro
)
825 (let ((buffer-undo-list t
))
828 (c-guess-basic-syntax)
830 c-hanging-colons-alist
)))))
831 ;; does a newline go before the colon? Watch out for already
832 ;; non-hung colons. However, we don't unhang them because that
833 ;; would be a cleanup (and anti-social).
834 (if (and (memq 'before newlines
)
837 (skip-chars-backward ": \t")
839 (let ((pos (- (point-max) (point))))
841 (c-newline-and-indent)
842 (goto-char (- (point-max) pos
))))
843 ;; does a newline go after the colon?
844 (if (and (memq 'after
(cdr-safe newlines
))
846 (c-newline-and-indent))
849 (defun c-electric-lt-gt (arg)
850 "Insert a less-than, or greater-than character.
851 The line will be re-indented if the character inserted is the second
852 of a C++ style stream operator and the buffer is in C++ mode.
853 Exceptions are when a numeric argument is supplied, point is inside a
854 literal, or `c-syntactic-indentation' is nil, in which case the line
855 will not be re-indented."
857 (let ((indentp (and c-syntactic-indentation
859 (eq (char-before) last-command-char
)
860 (not (c-in-literal))))
862 (c-echo-syntactic-information-p nil
))
863 (self-insert-command (prefix-numeric-value arg
))
865 (indent-according-to-mode))))
867 (defun c-electric-paren (arg)
868 "Insert a parenthesis.
870 Some newline cleanups are done if appropriate; see the variable
873 Also, the line is re-indented unless a numeric ARG is supplied, the
874 parenthesis is inserted inside a literal, or `c-syntactic-indentation'
877 (let ((literal (c-in-literal (c-point 'bod
)))
879 (c-echo-syntactic-information-p nil
))
881 (self-insert-command (prefix-numeric-value arg
))
882 ;; do some special stuff with the character
883 (let* (;; We want to inhibit blinking the paren since this will
884 ;; be most disruptive. We'll blink it ourselves
886 (old-blink-paren blink-paren-function
)
887 blink-paren-function
)
888 (self-insert-command (prefix-numeric-value arg
))
889 (if c-syntactic-indentation
890 (indent-according-to-mode))
891 (when (looking-at "[ \t]*\\\\?$")
893 ;; Do all appropriate clean ups
895 (pos (- (point-max) (point)))
897 ;; clean up brace-elseif-brace
898 (if (and (memq 'brace-elseif-brace c-cleanup-list
)
899 (eq last-command-char ?\
()
902 "\\([ \t\n]\\|\\\\\n\\)*"
904 "\\([ \t\n]\\|\\\\\n\\)+"
906 "\\([ \t\n]\\|\\\\\n\\)*"
910 (setq mbeg
(match-beginning 0)
913 (not (c-in-literal)))
915 (delete-region mbeg mend
)
916 (insert-and-inherit "} else if ("))
917 ;; clean up brace-catch-brace
919 (if (and (memq 'brace-catch-brace c-cleanup-list
)
920 (eq last-command-char ?\
()
923 "\\([ \t\n]\\|\\\\\n\\)*"
925 "\\([ \t\n]\\|\\\\\n\\)*"
929 (setq mbeg
(match-beginning 0)
932 (not (c-in-literal)))
934 (delete-region mbeg mend
)
935 (insert-and-inherit "} catch ("))))
936 (goto-char (- (point-max) pos
))
938 (let (beg (end (1- (point))))
939 (cond ((and (memq 'space-before-funcall c-cleanup-list
)
940 (eq last-command-char ?\
()
943 (skip-chars-backward " \t")
947 (delete-region beg end
)
950 ((and (memq 'compact-empty-funcall c-cleanup-list
)
951 (eq last-command-char ?\
))
953 (c-safe (backward-char 2))
954 (when (looking-at "()")
956 (skip-chars-backward " \t")
959 (delete-region beg end
))))
960 (and (not executing-kbd-macro
)
962 (funcall old-blink-paren
))))))
964 (defun c-electric-continued-statement ()
965 "Reindent the current line if appropriate.
967 This function is used to reindent the line after a keyword which
968 continues an earlier statement is typed, e.g. an \"else\" or the
969 \"while\" in a do-while block.
971 The line is reindented if there is nothing but whitespace before the
972 keyword on the line, the keyword is not inserted inside a literal, and
973 `c-syntactic-indentation' is non-nil."
974 (let (;; shut this up
975 (c-echo-syntactic-information-p nil
))
976 (when (and c-syntactic-indentation
977 (not (eq last-command-char ?_
))
979 (skip-syntax-backward "w")
982 (not (c-in-literal (c-point 'bod
))))
983 ;; Have to temporarily insert a space so that
984 ;; c-guess-basic-syntax recognizes the keyword. Follow the
985 ;; space with a nonspace to avoid messing up any whitespace
986 ;; sensitive meddling that might be done, e.g. by
987 ;; `c-backslash-region'.
988 (insert-and-inherit " x")
990 (indent-according-to-mode)
994 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus
995 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com
996 (defun c-forward-into-nomenclature (&optional arg
)
997 "Move forward to end of a nomenclature section or word.
998 With arg, to it arg times."
1000 (let ((case-fold-search nil
))
1002 (re-search-forward "\\W*\\([A-Z]*[a-z0-9]*\\)" (point-max) t arg
)
1003 (while (and (< arg
0)
1005 "\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)"
1008 (setq arg
(1+ arg
)))))
1009 (c-keep-region-active))
1011 (defun c-backward-into-nomenclature (&optional arg
)
1012 "Move backward to beginning of a nomenclature section or word.
1013 With optional ARG, move that many times. If ARG is negative, move
1016 (c-forward-into-nomenclature (- arg
))
1017 (c-keep-region-active))
1019 (defun c-scope-operator ()
1020 "Insert a double colon scope operator at point.
1021 No indentation or other \"electric\" behavior is performed."
1023 (insert-and-inherit "::"))
1025 (defun c-beginning-of-defun (&optional arg
)
1026 "Move backward to the beginning of a defun.
1027 Every top level declaration that contains a brace paren block is
1028 considered to be a defun.
1030 With a positive argument, move backward that many defuns. A negative
1031 argument -N means move forward to the Nth following beginning. Return
1032 t unless search stops due to beginning or end of buffer.
1034 Unlike the built-in `beginning-of-defun' this tries to be smarter
1035 about finding the char with open-parenthesis syntax that starts the
1039 (or arg
(setq arg
1))
1042 (when (c-end-of-defun (- arg
))
1043 (c-forward-syntactic-ws)
1048 ;; Note: Partial code duplication in `c-end-of-defun' and
1049 ;; `c-declaration-limits'.
1051 (let ((paren-state (c-parse-state)) lim pos
)
1053 (goto-char (c-least-enclosing-brace paren-state
))
1054 ;; If we moved to the outermost enclosing paren
1055 ;; then we can use c-safe-position to set the
1056 ;; limit. Can't do that otherwise since the
1057 ;; earlier paren pair on paren-state might very
1058 ;; well be part of the declaration we should go
1060 (setq lim
(c-safe-position (point) paren-state
))
1062 ;; At top level. Make sure we aren't inside a literal.
1063 (setq pos
(c-literal-limits
1064 (c-safe-position (point) paren-state
)))
1065 (if pos
(goto-char (car pos
))))
1067 (while (let ((start (point)))
1068 (c-beginning-of-decl-1 lim
)
1069 (if (= (point) start
)
1070 ;; Didn't move. Might be due to bob or unbalanced
1071 ;; parens. Try to continue if it's the latter.
1072 (unless (c-safe (goto-char
1073 (c-down-list-backward (point))))
1074 ;; Didn't work, so it's bob then.
1075 (goto-char (point-min))
1079 ;; Check if the declaration contains a brace
1080 ;; block. If not, we try another one.
1082 (not (and (c-syntactic-re-search-forward
1084 (or (eq (char-before) ?
{)
1085 (and c-recognize-knr-p
1086 ;; Might have stopped on the
1087 ;; ';' in a K&R argdecl. In
1088 ;; that case the declaration
1089 ;; should contain a block.
1090 (c-in-knr-argdecl pos
)))))))
1093 ;; Check if `c-beginning-of-decl-1' put us after the block
1094 ;; in a declaration that doesn't end there. We're searching
1095 ;; back and forth over the block here, which can be
1098 (if (and c-opt-block-decls-with-vars-key
1100 (c-backward-syntactic-ws)
1101 (eq (char-before) ?
}))
1102 (eq (car (c-beginning-of-decl-1))
1111 ;; Try to be line oriented; position point at the closest
1112 ;; preceding boi that isn't inside a comment, but if we hit
1113 ;; the previous declaration then we use the current point
1115 (while (and (/= (point) (c-point 'boi
))
1116 (c-forward-comment -
1)))
1117 (if (/= (point) (c-point 'boi
))
1120 (setq arg
(1- arg
)))))
1121 (c-keep-region-active)
1124 (defun c-end-of-defun (&optional arg
)
1125 "Move forward to the end of a top level declaration.
1126 With argument, do it that many times. Negative argument -N means move
1127 back to Nth preceding end. Returns t unless search stops due to
1128 beginning or end of buffer.
1130 An end of a defun occurs right after the close-parenthesis that matches
1131 the open-parenthesis that starts a defun; see `beginning-of-defun'."
1134 (or arg
(setq arg
1))
1137 (when (c-beginning-of-defun (- arg
))
1138 (c-backward-syntactic-ws)
1143 ;; Note: Partial code duplication in `c-beginning-of-defun'
1144 ;; and `c-declaration-limits'.
1146 (let ((paren-state (c-parse-state)) lim pos
)
1148 (goto-char (c-least-enclosing-brace paren-state
))
1149 ;; If we moved to the outermost enclosing paren
1150 ;; then we can use c-safe-position to set the
1151 ;; limit. Can't do that otherwise since the
1152 ;; earlier paren pair on paren-state might very
1153 ;; well be part of the declaration we should go
1155 (setq lim
(c-safe-position (point) paren-state
))
1157 ;; At top level. Make sure we aren't inside a literal.
1158 (setq pos
(car-safe (c-literal-limits
1159 (c-safe-position (point) paren-state
))))
1160 (if pos
(goto-char pos
)))
1162 ;; Have to move to the start first so that `c-end-of-decl-1'
1163 ;; has the correct start position.
1165 (when (memq (car (c-beginning-of-decl-1 lim
))
1167 ;; We moved back over the previous defun or a macro. Move
1168 ;; to the next token; it's the start of the next
1169 ;; declaration. We can also be directly after the block
1170 ;; in a `c-opt-block-decls-with-vars-key' declaration, but
1171 ;; then we won't move significantly far here.
1173 (c-forward-token-1 0))
1175 (while (let ((start (point)))
1177 (if (= (point) start
)
1178 ;; Didn't move. Might be due to eob or unbalanced
1179 ;; parens. Try to continue if it's the latter.
1180 (if (c-safe (goto-char (c-up-list-forward (point))))
1182 ;; Didn't work, so it's eob then.
1183 (goto-char (point-max))
1187 ;; Check if the declaration contains a brace
1188 ;; block. If not, we try another one.
1191 (not (c-syntactic-re-search-forward "{" pos t
1 t
))))))
1194 ;; Try to be line oriented; position point after the next
1195 ;; newline that isn't inside a comment, but if we hit the
1196 ;; next declaration then we use the current point instead.
1197 (while (and (not (bolp))
1198 (not (looking-at "\\s *$"))
1199 (c-forward-comment 1)))
1201 ((looking-at "\\s *$")
1206 (setq arg
(1- arg
)))))
1207 (c-keep-region-active)
1210 (defun c-declaration-limits (near)
1211 ;; Return a cons of the beginning and end positions of the current
1212 ;; top level declaration or macro. If point is not inside any then
1213 ;; nil is returned, unless NEAR is non-nil in which case the closest
1214 ;; following one is chosen instead (if there is any). The end
1215 ;; position is at the next line, providing there is one before the
1219 ;; Note: Some code duplication in `c-beginning-of-defun' and
1220 ;; `c-end-of-defun'.
1222 (let ((start (point))
1223 (paren-state (c-parse-state))
1226 (goto-char (c-least-enclosing-brace paren-state
))
1227 ;; If we moved to the outermost enclosing paren then we
1228 ;; can use c-safe-position to set the limit. Can't do
1229 ;; that otherwise since the earlier paren pair on
1230 ;; paren-state might very well be part of the
1231 ;; declaration we should go to.
1232 (setq lim
(c-safe-position (point) paren-state
))
1234 ;; At top level. Make sure we aren't inside a literal.
1235 (setq pos
(c-literal-limits
1236 (c-safe-position (point) paren-state
)))
1237 (if pos
(goto-char (car pos
))))
1239 (when (c-beginning-of-macro)
1248 (when (or (eq (car (c-beginning-of-decl-1 lim
)) 'previous
)
1250 ;; We moved back over the previous defun. Skip to the next
1251 ;; one. Not using c-forward-syntactic-ws here since we
1252 ;; should not skip a macro. We can also be directly after
1253 ;; the block in a `c-opt-block-decls-with-vars-key'
1254 ;; declaration, but then we won't move significantly far
1257 (while (c-forward-comment 10))
1259 (when (and near
(c-beginning-of-macro))
1267 (if (eobp) (throw 'exit nil
))
1269 ;; Check if `c-beginning-of-decl-1' put us after the block in a
1270 ;; declaration that doesn't end there. We're searching back and
1271 ;; forth over the block here, which can be expensive.
1273 (if (and c-opt-block-decls-with-vars-key
1275 (c-backward-syntactic-ws)
1276 (eq (char-before) ?
}))
1277 (eq (car (c-beginning-of-decl-1))
1281 (and (> (point) pos
)
1282 (setq end-pos
(point)))))
1286 (if (and (not near
) (> (point) start
))
1289 ;; Try to be line oriented; position the limits at the
1290 ;; closest preceding boi, and after the next newline, that
1291 ;; isn't inside a comment, but if we hit a neighboring
1292 ;; declaration then we instead use the exact declaration
1293 ;; limit in that direction.
1296 (while (and (/= (point) (c-point 'boi
))
1297 (c-forward-comment -
1)))
1298 (if (/= (point) (c-point 'boi
))
1306 (while (and (not (bolp))
1307 (not (looking-at "\\s *$"))
1308 (c-forward-comment 1)))
1311 ((looking-at "\\s *$")
1318 (defun c-mark-function ()
1319 "Put mark at end of the current top-level declaration or macro, point at beginning.
1320 If point is not inside any then the closest following one is chosen.
1322 As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
1323 function does not require the declaration to contain a brace block."
1326 ;; We try to be line oriented, unless there are several
1327 ;; declarations on the same line.
1328 (if (looking-at c-syntactic-eol
)
1329 (c-backward-token-1 1 nil
(c-point 'bol
)))
1331 (let ((decl-limits (c-declaration-limits t
)))
1332 (if (not decl-limits
)
1333 (error "Cannot find any declaration")
1334 (goto-char (car decl-limits
))
1335 (push-mark (cdr decl-limits
) nil t
))))
1338 (defun c-beginning-of-statement (&optional count lim sentence-flag
)
1339 "Go to the beginning of the innermost C statement.
1340 With prefix arg, go back N - 1 statements. If already at the
1341 beginning of a statement then go to the beginning of the closest
1342 preceding one, moving into nested blocks if necessary (use
1343 \\[backward-sexp] to skip over a block). If within or next to a
1344 comment or multiline string, move by sentences instead of statements.
1346 When called from a program, this function takes 3 optional args: the
1347 repetition count, a buffer position limit which is the farthest back
1348 to search for the syntactic context, and a flag saying whether to do
1349 sentence motion in or near comments and multiline strings.
1351 Note that `c-beginning-of-statement-1' is usually better to use from
1352 programs. It has much more well defined semantics than this one,
1353 which is intended for interactive use and might therefore change to be
1355 (interactive (list (prefix-numeric-value current-prefix-arg
)
1357 (let* ((count (or count
1))
1359 (range (c-collect-line-comments (c-literal-limits lim
))))
1360 (while (and (/= count
0)
1361 (or (not lim
) (> (point) lim
)))
1363 (if (and (not range
) sentence-flag
)
1365 ;; Find the comment next to point if we're not in one.
1367 (if (c-forward-comment-lc -
1)
1368 (setq range
(cons (point)
1369 (progn (c-forward-comment-lc 1)
1371 (c-skip-ws-backward)
1372 (setq range
(point))
1374 (if (eq (char-before) ?
\")
1375 (c-safe (c-backward-sexp 1)
1376 (cons (point) range
)))))
1378 (if (eq (char-after) ?
\")
1379 (setq range
(cons (point)
1383 (setq range
(point))
1384 (setq range
(if (c-forward-comment-lc 1)
1385 (cons range
(point))
1387 (setq range
(c-collect-line-comments range
))))
1388 (if (and (< count
0) (= here
(point-max)))
1389 ;; Special case because eob might be in a literal.
1392 (if (and sentence-flag
1393 (or (/= (char-syntax (char-after (car range
))) ?
\")
1394 ;; Only visit a string if it spans more than one line.
1396 (goto-char (car range
))
1397 (skip-chars-forward "^\n" (cdr range
))
1398 (< (point) (cdr range
)))))
1399 (let* ((lit-type (c-literal-type range
))
1400 (line-prefix (concat "[ \t]*\\("
1401 c-current-comment-prefix
1403 (beg (if (eq lit-type
'string
)
1406 (goto-char (car range
))
1408 (looking-at comment-start-skip
)
1411 (looking-at line-prefix
)
1413 (end (- (cdr range
) (if (eq lit-type
'c
) 2 1)))
1414 (beg-of-para (if (eq lit-type
'string
)
1418 (if (looking-at line-prefix
)
1419 (goto-char (match-end 0)))))))
1421 ;; Move by sentence, but not past the limit of the
1422 ;; literal, narrowed to the appropriate
1424 (narrow-to-region (save-excursion
1425 (let ((pos (min here end
)))
1427 (forward-paragraph -
1)
1428 (if (looking-at paragraph-separate
)
1430 (when (> (point) beg
)
1431 (funcall beg-of-para
)
1432 (when (>= (point) pos
)
1433 (forward-paragraph -
2)
1434 (funcall beg-of-para
)))
1437 (c-safe (forward-sentence (if (< count
0) 1 -
1)))
1438 (if (and (memq lit-type
'(c c
++))
1439 ;; Check if we stopped due to a comment
1440 ;; prefix and not a sentence end.
1441 (/= (point) (point-min))
1442 (/= (point) (point-max))
1445 (looking-at line-prefix
))
1446 (>= (point) (match-beginning 0))
1447 (/= (match-beginning 1) (match-end 1))
1448 (or (< (point) (match-end 0))
1450 (= (point) (match-end 0))
1451 ;; The comment prefix may contain
1452 ;; characters that is regarded as end
1457 (forward-paragraph -
1)
1458 (< (point) (match-beginning 0)))
1461 (or (not (re-search-backward
1466 (c-point 'eol
)))))))))
1467 (setq count
(+ count
(if (< count
0) -
1 1)))
1470 ;; In block comments, if there's only
1471 ;; horizontal ws between the text and the
1472 ;; comment ender, stop before it. Stop after
1473 ;; the ender if there's either nothing or
1474 ;; newlines between.
1475 (when (and (eq lit-type
'c
)
1476 (eq (point) (point-max)))
1478 (when (or (= (skip-chars-backward " \t") 0)
1479 (eq (point) (point-max))
1481 (goto-char (cdr range
)))))
1482 (when (and (eq (point) (point-min))
1483 (looking-at "[ \t]*\\\\?$"))
1484 ;; Stop before instead of after the comment
1485 ;; starter if nothing follows it.
1487 (goto-char (car range
))
1488 (if (and (eq lit-type
'string
) (/= (point) here
))
1489 (setq count
(1+ count
)
1491 ;; See if we should escape the literal.
1493 (if (< (point) here
)
1494 (setq count
(1- count
))
1495 (goto-char (car range
))
1497 (if (> (point) here
)
1498 (setq count
(1+ count
))
1499 (goto-char (cdr range
))
1501 (goto-char (if (> count
0) (car range
) (cdr range
)))
1506 ;; Stop before `{' and after `;', `{', `}' and `};'
1507 ;; when not followed by `}' or `)', but on the other
1508 ;; side of the syntactic ws. Move by sexps and move
1509 ;; into parens. Also stop before `#' when it's at boi
1511 (let ((literal-pos (not sentence-flag
))
1512 (large-enough (- (point-max)))
1513 last last-below-line
)
1517 (when (and (or (eq (char-after) ?\
{)
1518 (and (eq (char-after) ?
#)
1519 (eq (point) (c-point 'boi
)))
1522 (unless (and c-special-brace-lists
1523 (eq (char-after) ?
{)
1524 (c-looking-at-special-brace-list))
1525 (if (and (eq (char-after) ?
#)
1526 (numberp last-below-line
)
1527 (not (eq last-below-line here
)))
1528 (goto-char last-below-line
))
1530 ;; Don't know why I added the following, but it
1531 ;; doesn't work when point is preceded by a line
1532 ;; style comment. /mast
1533 ;;(c-skip-ws-backward)
1535 (c-forward-comment-lc large-enough
)
1536 (when (c-forward-comment-lc -
1)
1537 ;; Record position of first comment.
1539 (c-forward-comment-lc 1)
1540 (setq literal-pos
(point)))
1541 (c-forward-comment-lc large-enough
)))
1542 (unless last-below-line
1544 (re-search-forward "\\(^\\|[^\\]\\)$" last t
))
1545 (setq last-below-line last
)))
1546 (cond ((bobp) ; Must handle bob specially.
1551 ((progn (backward-char)
1552 (looking-at "[;{}]"))
1553 (if (and c-special-brace-lists
1554 (eq (char-after) ?
{)
1555 (c-looking-at-special-brace-list))
1556 (skip-syntax-backward "w_") ; Speedup only.
1557 (if (or (= here last
)
1558 (memq (char-after last
) '(?\
) ?
})))
1559 (if (and (eq (char-before) ?
})
1560 (eq (char-after) ?\
;))
1564 ((= (char-syntax (char-after)) ?
\")
1565 (let ((end (point)))
1569 (skip-chars-forward "^\n" end
)
1570 (when (< (point) end
)
1571 ;; Break at multiline string.
1572 (setq literal-pos
(1+ end
))
1574 (t (skip-syntax-backward "w_")) ; Speedup only.
1576 (if (and (numberp literal-pos
)
1577 (< (point) literal-pos
))
1578 ;; We jumped over a comment or string that
1579 ;; should be investigated.
1580 (goto-char literal-pos
)
1581 (setq count
(1- count
))))
1583 (goto-char (point-min))
1586 ;; Stop before `{', `}', and `#' when it's at boi on a
1587 ;; line, but on the other side of the syntactic ws, and
1588 ;; after `;', `}' and `};'. Only stop before `{' if at
1589 ;; top level or inside braces, though. Move by sexps
1590 ;; and move into parens. Also stop at eol of lines
1591 ;; with `#' at the boi.
1592 (let ((literal-pos (not sentence-flag
))
1593 (large-enough (point-max))
1599 (c-forward-comment-lc large-enough
)
1602 ;; Record position of first comment.
1603 (setq literal-pos
(point))
1604 (c-forward-comment-lc 1))
1605 (c-forward-comment-lc large-enough
)
1606 (setq literal-pos nil
)))
1607 (cond ((and (eq (char-after) ?
{)
1608 (not (and c-special-brace-lists
1609 (c-looking-at-special-brace-list)))
1612 (or (not (c-safe (up-list -
1) t
))
1613 (= (char-after) ?
{))))
1616 ((and c-special-brace-lists
1617 (eq (char-after) ?
})
1619 (and (c-safe (up-list -
1) t
)
1620 (c-looking-at-special-brace-list))))
1622 (skip-syntax-forward "w_")) ; Speedup only.
1623 ((and (eq (char-after) ?
})
1627 ; ((and (eq (char-after) ?#)
1628 ; (= (point) (c-point 'boi)))
1630 ; (or (re-search-forward "\\(^\\|[^\\]\\)$" nil t)
1631 ; (goto-char (point-max)))
1634 ((looking-at ";\\|};?")
1635 (goto-char (match-end 0))
1637 ((= (char-syntax (char-after)) ?
\")
1638 (let ((beg (point)))
1641 (skip-chars-backward "^\n" beg
)
1642 (when (> (point) beg
)
1643 ;; Break at multiline string.
1644 (setq literal-pos beg
)
1648 (skip-syntax-forward "w_")) ; Speedup only.
1650 (if (and (numberp literal-pos
)
1651 (> (point) literal-pos
))
1652 ;; We jumped over a comment that should be investigated.
1653 (goto-char literal-pos
)
1654 (setq count
(1+ count
))))
1656 (goto-char (point-max))
1659 ;; If we haven't moved we're near a buffer limit.
1660 (when (and (not (zerop count
)) (= (point) here
))
1661 (goto-char (if (> count
0) (point-min) (point-max)))
1663 (c-keep-region-active))
1665 (defun c-end-of-statement (&optional count lim sentence-flag
)
1666 "Go to the end of the innermost C statement.
1667 With prefix arg, go forward N - 1 statements. Move forward to the end
1668 of the next statement if already at end, and move into nested blocks
1669 \(use \\[forward-sexp] to skip over a block). If within or next to a
1670 comment or multiline string, move by sentences instead of statements.
1672 When called from a program, this function takes 3 optional args: the
1673 repetition count, a buffer position limit which is the farthest back
1674 to search for the syntactic context, and a flag saying whether to do
1675 sentence motion in or near comments and multiline strings."
1676 (interactive (list (prefix-numeric-value current-prefix-arg
)
1678 (c-beginning-of-statement (- (or count
1)) lim sentence-flag
)
1679 (c-keep-region-active))
1682 ;; set up electric character functions to work with pending-del,
1683 ;; (a.k.a. delsel) mode. All symbols get the t value except
1684 ;; the functions which delete, which gets 'supersede.
1688 (put sym
'delete-selection t
) ; for delsel (Emacs)
1689 (put sym
'pending-delete t
))) ; for pending-del (XEmacs)
1694 c-electric-semi
&comma
1698 (put 'c-electric-delete
'delete-selection
'supersede
) ; delsel
1699 (put 'c-electric-delete
'pending-delete
'supersede
) ; pending-del
1700 (put 'c-electric-backspace
'delete-selection
'supersede
) ; delsel
1701 (put 'c-electric-backspace
'pending-delete
'supersede
) ; pending-del
1702 (put 'c-electric-delete-forward
'delete-selection
'supersede
) ; delsel
1703 (put 'c-electric-delete-forward
'pending-delete
'supersede
) ; pending-del
1706 (defun c-calc-comment-indent (entry)
1708 (setq entry
(or (assq entry c-indent-comment-alist
)
1709 (assq 'other c-indent-comment-alist
)
1710 '(default .
(column . nil
)))))
1711 (let ((action (car (cdr entry
)))
1712 (value (cdr (cdr entry
)))
1713 (col (current-column)))
1714 (cond ((eq action
'space
)
1716 ((eq action
'column
)
1717 (unless value
(setq value comment-column
))
1719 ;; Do not pad with one space if we're at bol.
1721 (max (1+ col
) value
)))
1727 (let ((lim (c-literal-limits (c-point 'bol
) t
)))
1729 (goto-char (car lim
))
1730 (when (looking-at "/[/*]")
1731 ;; Found comment to align with.
1733 ;; Do not pad with one space if we're at bol.
1735 (max (1+ col
) (current-column))))))))
1736 ;; Recurse to handle value as a new spec.
1737 (c-calc-comment-indent (cdr entry
)))))))
1739 ;; To avoid warning about assignment without reference wrt
1740 ;; c-add-syntax below.
1741 (cc-bytecomp-defvar syntactic-relpos
)
1743 (defun c-comment-indent ()
1744 "Used by `indent-for-comment' to create and indent comments.
1745 See `c-indent-comment-alist' for a description."
1748 (let* ((eot (let ((lim (c-literal-limits (c-point 'bol
) t
)))
1749 (or (when (consp lim
)
1750 (goto-char (car lim
))
1751 (when (looking-at "/[/*]")
1752 (skip-chars-backward " \t")
1755 (skip-chars-backward " \t")
1758 (cond ((looking-at "^/[/*]")
1760 ((progn (beginning-of-line)
1763 ((progn (back-to-indentation)
1764 (and (eq (char-after) ?
})
1765 (eq (point) (1- eot
))))
1767 ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
1768 (eq (match-end 0) eot
))
1772 (if (and (memq line-type
'(anchored-comment empty-line
))
1773 c-indent-comments-syntactically-p
)
1774 (let ((syntax (c-guess-basic-syntax))
1776 ;; BOGOSITY ALERT: if we're looking at the eol, its
1777 ;; because indent-for-comment hasn't put the comment-start
1778 ;; in the buffer yet. this will screw up the syntactic
1779 ;; analysis so we kludge in the necessary info. Another
1780 ;; kludge is that if we're at the bol, then we really want
1781 ;; to ignore any anchoring as specified by
1782 ;; c-comment-only-line-offset since it doesn't apply here.
1784 (c-add-syntax 'comment-intro
))
1785 (let ((c-comment-only-line-offset
1786 (if (consp c-comment-only-line-offset
)
1787 c-comment-only-line-offset
1788 (cons c-comment-only-line-offset
1789 c-comment-only-line-offset
))))
1790 (c-get-syntactic-indentation syntax
)))
1792 (c-calc-comment-indent line-type
)))))
1795 ;; used by outline-minor-mode
1796 (defun c-outline-level ()
1797 (let (buffer-invisibility-spec);; This so that `current-column' DTRT
1798 ;; in otherwise-hidden text.
1800 (skip-chars-forward "\t ")
1804 (defun c-up-conditional (count)
1805 "Move back to the containing preprocessor conditional, leaving mark behind.
1806 A prefix argument acts as a repeat count. With a negative argument,
1807 move forward to the end of the containing preprocessor conditional.
1809 `#elif' is treated like `#else' followed by `#if', so the function
1810 stops at them when going backward, but not when going forward."
1812 (c-forward-conditional (- count
) -
1)
1813 (c-keep-region-active))
1815 (defun c-up-conditional-with-else (count)
1816 "Move back to the containing preprocessor conditional, including `#else'.
1817 Just like `c-up-conditional', except it also stops at `#else'
1820 (c-forward-conditional (- count
) -
1 t
)
1821 (c-keep-region-active))
1823 (defun c-down-conditional (count)
1824 "Move forward into the next preprocessor conditional, leaving mark behind.
1825 A prefix argument acts as a repeat count. With a negative argument,
1826 move backward into the previous preprocessor conditional.
1828 `#elif' is treated like `#else' followed by `#if', so the function
1829 stops at them when going forward, but not when going backward."
1831 (c-forward-conditional count
1)
1832 (c-keep-region-active))
1834 (defun c-down-conditional-with-else (count)
1835 "Move forward into the next preprocessor conditional, including `#else'.
1836 Just like `c-down-conditional', except it also stops at `#else'
1839 (c-forward-conditional count
1 t
)
1840 (c-keep-region-active))
1842 (defun c-backward-conditional (count &optional target-depth with-else
)
1843 "Move back across a preprocessor conditional, leaving mark behind.
1844 A prefix argument acts as a repeat count. With a negative argument,
1845 move forward across a preprocessor conditional."
1847 (c-forward-conditional (- count
) target-depth with-else
)
1848 (c-keep-region-active))
1850 (defun c-forward-conditional (count &optional target-depth with-else
)
1851 "Move forward across a preprocessor conditional, leaving mark behind.
1852 A prefix argument acts as a repeat count. With a negative argument,
1853 move backward across a preprocessor conditional.
1855 `#elif' is treated like `#else' followed by `#if', except that the
1856 nesting level isn't changed when tracking subconditionals.
1858 The optional argument TARGET-DEPTH specifies the wanted nesting depth
1859 after each scan. I.e. if TARGET-DEPTH is -1, the function will move
1860 out of the enclosing conditional. A non-integer non-nil TARGET-DEPTH
1863 If the optional argument WITH-ELSE is non-nil, `#else' directives are
1864 treated as conditional clause limits. Normally they are ignored."
1866 (let* ((forward (> count
0))
1867 (increment (if forward -
1 1))
1868 (search-function (if forward
're-search-forward
're-search-backward
))
1870 (unless (integerp target-depth
)
1871 (setq target-depth
(if target-depth -
1 0)))
1875 ;; subdepth is the depth in "uninteresting" subtrees,
1876 ;; i.e. those that takes us farther from the target
1877 ;; depth instead of closer.
1881 ;; Find the "next" significant line in the proper direction.
1882 (while (and (not found
)
1883 ;; Rather than searching for a # sign that
1884 ;; comes at the beginning of a line aside from
1885 ;; whitespace, search first for a string
1886 ;; starting with # sign. Then verify what
1887 ;; precedes it. This is faster on account of
1888 ;; the fastmap feature of the regexp matcher.
1889 (funcall search-function
1890 "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
1893 ;; Now verify it is really a preproc line.
1894 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
1895 (let (dchange (directive (match-string 1)))
1896 (cond ((string= directive
"if")
1897 (setq dchange
(- increment
)))
1898 ((string= directive
"endif")
1899 (setq dchange increment
))
1901 ;; When we're not in an "uninteresting"
1902 ;; subtree, we might want to act on "elif"
1904 (if (cond (with-else
1905 ;; Always move toward the target depth.
1907 (if (> target-depth
0) 1 -
1)))
1908 ((string= directive
"elif")
1909 (setq dchange
(- increment
))))
1910 ;; Ignore the change if it'd take us
1911 ;; into an "uninteresting" subtree.
1912 (if (eq (> dchange
0) (<= target-depth
0))
1913 (setq dchange nil
)))))
1915 (when (or (/= subdepth
0)
1916 (eq (> dchange
0) (<= target-depth
0)))
1917 (setq subdepth
(+ subdepth dchange
)))
1918 (setq depth
(+ depth dchange
))
1919 ;; If we are trying to move across, and we find an
1920 ;; end before we find a beginning, get an error.
1921 (if (and (< depth target-depth
) (< dchange
0))
1923 "No following conditional at this level"
1924 "No previous conditional at this level"))))
1925 ;; When searching forward, start from next line so
1926 ;; that we don't find the same line again.
1927 (if forward
(forward-line 1))
1928 ;; We found something if we've arrived at the
1930 (if (and dchange
(= depth target-depth
))
1931 (setq found
(point))))
1933 (if forward
(forward-line 1)))))
1935 (error "No containing preprocessor conditional"))
1936 (goto-char (setq new found
)))
1937 (setq count
(+ count increment
))))
1940 (c-keep-region-active))
1943 ;; commands to indent lines, regions, defuns, and expressions
1944 (defun c-indent-command (&optional arg
)
1945 "Indent current line as C code, and/or insert some whitespace.
1947 If `c-tab-always-indent' is t, always just indent the current line.
1948 If nil, indent the current line only if point is at the left margin or
1949 in the line's indentation; otherwise insert some whitespace[*]. If
1950 other than nil or t, then some whitespace[*] is inserted only within
1951 literals (comments and strings) and inside preprocessor directives,
1952 but the line is always reindented.
1954 If `c-syntactic-indentation' is t, indentation is done according to
1955 the syntactic context. A numeric argument, regardless of its value,
1956 means indent rigidly all the lines of the expression starting after
1957 point so that this line becomes properly indented. The relative
1958 indentation among the lines of the expression is preserved.
1960 If `c-syntactic-indentation' is nil, the line is just indented one
1961 step according to `c-basic-offset'. In this mode, a numeric argument
1962 indents a number of such steps, positive or negative, and an empty
1963 prefix argument is equivalent to -1.
1965 [*] The amount and kind of whitespace inserted is controlled by the
1966 variable `c-insert-tab-function', which is called to do the actual
1967 insertion of whitespace. Normally the function in this variable
1968 just inserts a tab character, or the equivalent number of spaces,
1969 depending on the variable `indent-tabs-mode'."
1972 (let ((indent-function
1973 (if c-syntactic-indentation
1974 (symbol-function 'indent-according-to-mode
)
1976 (let ((c-macro-start c-macro-start
)
1977 (steps (cond ((not current-prefix-arg
) 1)
1978 ((equal current-prefix-arg
'(4)) -
1)
1980 (c-shift-line-indentation (* steps c-basic-offset
))
1981 (when (and c-auto-align-backslashes
1984 (eq (char-before) ?
\\))
1985 (c-query-and-set-macro-start))
1986 ;; Realign the line continuation backslash if inside a macro.
1987 (c-backslash-region (point) (point) nil t
)))
1989 (if (and c-syntactic-indentation current-prefix-arg
)
1990 ;; If c-syntactic-indentation and got arg, always indent this
1991 ;; line as C and shift remaining lines of expression the same
1993 (let ((shift-amt (save-excursion
1994 (back-to-indentation)
1998 (setq shift-amt
(- (save-excursion
1999 (back-to-indentation)
2003 (if (eq c-tab-always-indent t
)
2004 (beginning-of-line))
2012 (indent-code-rigidly beg end shift-amt
"#")))
2013 ;; Else use c-tab-always-indent to determine behavior.
2015 ;; CASE 1: indent when at column zero or in lines indentation,
2016 ;; otherwise insert a tab
2017 ((not c-tab-always-indent
)
2019 (skip-chars-backward " \t")
2021 (funcall c-insert-tab-function
)
2022 (funcall indent-function
)))
2023 ;; CASE 2: just indent the line
2024 ((eq c-tab-always-indent t
)
2025 (funcall indent-function
))
2026 ;; CASE 3: if in a literal, insert a tab, but always indent the
2030 (funcall c-insert-tab-function
))
2031 (funcall indent-function
)
2034 (defun c-indent-exp (&optional shutup-p
)
2035 "Indent each line in the balanced expression following point syntactically.
2036 If optional SHUTUP-P is non-nil, no errors are signalled if no
2037 balanced expression is found."
2039 (let ((here (point-marker))
2041 (set-marker-insertion-type here t
)
2043 (let ((start (save-restriction
2044 ;; Find the closest following open paren that
2045 ;; ends on another line.
2046 (narrow-to-region (point-min) (c-point 'eol
))
2047 (let (beg (end (point)))
2048 (while (and (setq beg
(c-down-list-forward end
))
2049 (setq end
(c-up-list-forward beg
))))
2051 (eq (char-syntax (char-before beg
)) ?\
()
2056 (error "Cannot find start of balanced expression to indent"))
2058 (setq end
(c-safe (scan-sexps (point) 1)))
2061 (error "Cannot find end of balanced expression to indent"))
2064 (c-indent-region (point) end
)))))
2066 (set-marker here nil
))))
2068 (defun c-indent-defun ()
2069 "Indent the current top-level declaration or macro syntactically.
2070 In the macro case this also has the effect of realigning any line
2071 continuation backslashes, unless `c-auto-align-backslashes' is nil."
2073 (let ((here (point-marker)) decl-limits
)
2074 ;; We try to be line oriented, unless there are several
2075 ;; declarations on the same line.
2076 (if (looking-at c-syntactic-eol
)
2077 (c-backward-token-1 1 nil
(c-point 'bol
))
2078 (c-forward-token-1 0 nil
(c-point 'eol
)))
2080 (if (setq decl-limits
(c-declaration-limits nil
))
2081 (c-indent-region (car decl-limits
)
2084 (set-marker here nil
))))
2086 (defun c-indent-region (start end
&optional quiet
)
2087 "Indent syntactically every line whose first char is between START
2088 and END inclusive. If the optional argument QUIET is non-nil then no
2089 syntactic errors are reported, even if `c-report-syntactic-errors' is
2093 (skip-chars-backward " \t\n\r")
2096 ;; Advance to first nonblank line.
2098 (skip-chars-forward " \t\n\r")
2099 (setq start
(point))
2101 (setq c-parsing-error
2102 (or (let ((endmark (copy-marker end
))
2103 (c-parsing-error nil
)
2104 ;; shut up any echo msgs on indiv lines
2105 (c-echo-syntactic-information-p nil
)
2106 (in-macro (and c-auto-align-backslashes
2107 (save-excursion (c-beginning-of-macro))
2109 (c-fix-backslashes nil
)
2113 (c-progress-init start end
'c-indent-region
)
2116 (< (point) endmark
))
2120 (skip-chars-forward " \t\n")
2122 ;; Get syntax and indent.
2123 (setq syntax
(c-guess-basic-syntax))
2124 (if (and c-auto-align-backslashes
2125 (assq 'cpp-macro syntax
))
2126 ;; Record macro start.
2127 (setq in-macro
(point)))
2129 (if (looking-at "\\s *\\\\$")
2131 (c-indent-line syntax t t
)
2132 (if (progn (end-of-line)
2133 (not (eq (char-before) ?
\\)))
2135 ;; Fixup macro backslashes.
2137 (c-backslash-region in-macro
(point) nil
)
2138 (setq in-macro nil
))
2140 (c-indent-line syntax t t
)
2143 (c-backslash-region in-macro
(c-point 'bopl
) nil t
)))
2144 (set-marker endmark nil
)
2145 (c-progress-fini 'c-indent-region
))
2146 (c-echo-parsing-error quiet
))
2149 (defun c-fn-region-is-active-p ()
2150 ;; Function version of the macro for use in places that aren't
2151 ;; compiled, e.g. in the menus.
2152 (c-region-is-active-p))
2154 (defun c-indent-line-or-region ()
2155 "When the region is active, indent it syntactically. Otherwise
2156 indent the current line syntactically."
2157 ;; Emacs has a variable called mark-active, XEmacs uses region-active-p
2159 (if (c-region-is-active-p)
2160 (c-indent-region (region-beginning) (region-end))
2164 ;; for progress reporting
2165 (defvar c-progress-info nil
)
2167 (defun c-progress-init (start end context
)
2170 ((not c-progress-interval
))
2171 ;; Start the progress update messages. If this Emacs doesn't have
2172 ;; a built-in timer, just be dumb about it.
2173 ((not (fboundp 'current-time
))
2174 (message "Indenting region... (this may take a while)"))
2175 ;; If progress has already been initialized, do nothing. otherwise
2176 ;; initialize the counter with a vector of:
2177 ;; [start end lastsec context]
2179 (t (setq c-progress-info
(vector start
2183 (nth 1 (current-time))
2185 (message "Indenting region..."))
2188 (defun c-progress-update ()
2190 (if (not (and c-progress-info c-progress-interval
))
2192 (let ((now (nth 1 (current-time)))
2193 (start (aref c-progress-info
0))
2194 (end (aref c-progress-info
1))
2195 (lastsecs (aref c-progress-info
2)))
2196 ;; should we update? currently, update happens every 2 seconds,
2197 ;; what's the right value?
2198 (if (< c-progress-interval
(- now lastsecs
))
2200 (message "Indenting region... (%d%% complete)"
2201 (/ (* 100 (- (point) start
)) (- end start
)))
2202 (aset c-progress-info
2 now
)))
2205 (defun c-progress-fini (context)
2207 (if (not c-progress-interval
)
2209 (if (or (eq context
(aref c-progress-info
3))
2212 (set-marker (aref c-progress-info
1) nil
)
2213 (setq c-progress-info nil
)
2214 (message "Indenting region... done")))))
2218 ;;; This page handles insertion and removal of backslashes for C macros.
2220 (defun c-backslash-region (from to delete-flag
&optional line-mode
)
2221 "Insert, align, or delete end-of-line backslashes on the lines in the region.
2222 With no argument, inserts backslashes and aligns existing backslashes.
2223 With an argument, deletes the backslashes. The backslash alignment is
2224 done according to the settings in `c-backslash-column',
2225 `c-backslash-max-column' and `c-auto-align-backslashes'.
2227 This function does not modify blank lines at the start of the region.
2228 If the region ends at the start of a line and the macro doesn't
2229 continue below it, the backslash (if any) at the end of the previous
2232 You can put the region around an entire macro definition and use this
2233 command to conveniently insert and align the necessary backslashes."
2234 (interactive "*r\nP")
2235 (let ((endmark (make-marker))
2236 ;; Keep the backslash trimming functions from changing the
2237 ;; whitespace around point, since in this case it's only the
2238 ;; position of point that tells the indentation of the line.
2239 (point-pos (if (save-excursion
2240 (skip-chars-backward " \t")
2241 (and (bolp) (looking-at "[ \t]*\\\\?$")))
2244 column longest-line-col bs-col-after-end
)
2247 (if (and (not line-mode
) (bobp))
2248 ;; Nothing to do if to is at bob, since we should back up
2249 ;; and there's no line to back up to.
2251 (when (and (not line-mode
) (bolp))
2252 ;; Do not back up the to line if line-mode is set, to make
2253 ;; e.g. c-newline-and-indent consistent regardless whether
2254 ;; the (newline) call leaves point at bol or not.
2259 (set-marker endmark
(point))
2261 (c-delete-backslashes-forward endmark point-pos
))
2262 ;; Set bs-col-after-end to the column of any backslash
2263 ;; following the region, or nil if there is none.
2264 (setq bs-col-after-end
2265 (and (progn (end-of-line)
2266 (eq (char-before) ?
\\))
2267 (= (forward-line 1) 0)
2268 (progn (end-of-line)
2269 (eq (char-before) ?
\\))
2270 (1- (current-column))))
2272 ;; Back up the to line if line-mode is set, since the line
2273 ;; after the newly inserted line break should not be
2274 ;; touched in c-newline-and-indent.
2275 (setq to
(max from
(or (c-safe (c-point 'eopl
)) from
)))
2276 (unless bs-col-after-end
2277 ;; Set bs-col-after-end to non-nil in any case, since we
2278 ;; do not want to delete the backslash at the last line.
2279 (setq bs-col-after-end t
)))
2281 (not c-auto-align-backslashes
))
2283 ;; Compute the smallest column number past the ends of all
2285 (setq longest-line-col
0)
2287 (if bs-col-after-end
2288 ;; Include one more line in the max column
2289 ;; calculation, since the to line will be backslashed
2293 (while (and (>= (point) from
)
2295 (if (eq (char-before) ?
\\)
2297 (skip-chars-backward " \t")
2298 (setq longest-line-col
(max longest-line-col
2299 (1+ (current-column))))
2303 ;; Try to align with surrounding backslashes.
2306 (if (and (not (bobp))
2307 (progn (backward-char)
2308 (eq (char-before) ?
\\)))
2310 (setq column
(1- (current-column)))
2311 (if (numberp bs-col-after-end
)
2312 ;; Both a preceding and a following backslash.
2313 ;; Choose the greatest of them.
2314 (setq column
(max column bs-col-after-end
)))
2316 ;; No preceding backslash. Try to align with one
2317 ;; following the region. Disregard the backslash at the
2318 ;; to line since it's likely to be bogus (e.g. when
2319 ;; called from c-newline-and-indent).
2320 (if (numberp bs-col-after-end
)
2321 (setq column bs-col-after-end
))
2322 ;; Don't modify blank lines at start of region.
2324 (while (and (< (point) to
) (bolp) (eolp))
2326 (if (and column
(< column longest-line-col
))
2327 ;; Don't try to align with surrounding backslashes if
2328 ;; any line is too long.
2331 ;; Impose minimum limit and tab width alignment only if
2332 ;; we can't align with surrounding backslashes.
2333 (if (> (% longest-line-col tab-width
) 0)
2334 (setq longest-line-col
2335 (* (/ (+ longest-line-col tab-width -
1)
2338 (setq column
(max c-backslash-column
2340 ;; Always impose maximum limit.
2341 (setq column
(min column c-backslash-max-column
)))
2342 (if bs-col-after-end
2343 ;; Add backslashes on all lines if the macro continues
2344 ;; after the to line.
2346 (set-marker endmark to
)
2347 (c-append-backslashes-forward endmark column point-pos
))
2348 ;; Add backslashes on all lines except the last, and
2349 ;; remove any on the last line.
2354 (set-marker endmark
(1- (point)))))
2356 (c-append-backslashes-forward endmark column point-pos
)
2357 ;; The function above leaves point on the line
2358 ;; following endmark.
2359 (set-marker endmark
(point)))
2360 (set-marker endmark to
))
2361 (c-delete-backslashes-forward endmark point-pos
)))))
2362 (set-marker endmark nil
)
2363 (if (markerp point-pos
)
2364 (set-marker point-pos nil
))))
2366 (defun c-append-backslashes-forward (to-mark column point-pos
)
2367 (let ((state (parse-partial-sexp (c-point 'bol
) (point))))
2371 (<= (point) to-mark
)
2373 (let ((start (point)) (inserted nil
) end col
)
2375 (unless (eq (char-before) ?
\\)
2378 (setq state
(parse-partial-sexp
2379 start
(point) nil nil state
))
2381 (setq col
(current-column))
2383 ;; Avoid unnecessary changes of the buffer.
2384 (cond ((and (not inserted
) (nth 3 state
))
2385 ;; Don't realign backslashes in string literals
2386 ;; since that would change them.
2393 (skip-chars-backward
2394 " \t" (if (>= (point) point-pos
) point-pos
))
2398 ((and (= col column
)
2399 (memq (char-before) '(?\ ?
\t))))
2403 (or (/= (skip-chars-backward
2404 " \t" (if (>= (point) point-pos
) point-pos
))
2406 (/= (char-after) ?\
)))
2407 (delete-region (point) end
)
2408 (indent-to column
1)))
2410 (= (forward-line 1) 0))))
2412 ;; Make sure there are backslashes with at least one space in
2416 (<= (point) to-mark
)
2418 (let ((start (point)))
2420 (setq state
(parse-partial-sexp
2421 start
(point) nil nil state
))
2423 (if (eq (char-before) ?
\\)
2424 (unless (nth 3 state
)
2426 (unless (and (memq (char-before) '(?\ ?
\t))
2427 (/= (point) point-pos
))
2430 (if (and (memq (char-before) '(?\ ?
\t))
2431 (/= (point) point-pos
))
2435 (= (forward-line 1) 0)))))))
2437 (defun c-delete-backslashes-forward (to-mark point-pos
)
2439 (and (<= (point) to-mark
)
2442 (if (eq (char-before) ?
\\)
2445 (progn (backward-char)
2446 (skip-chars-backward " \t" (if (>= (point) point-pos
)
2449 (= (forward-line 1) 0)))))
2453 ;;; Line breaking and paragraph filling.
2455 (defvar c-auto-fill-prefix t
)
2456 (defvar c-lit-limits nil
)
2457 (defvar c-lit-type nil
)
2459 ;; The filling code is based on a simple theory; leave the intricacies
2460 ;; of the text handling to the currently active mode for that
2461 ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
2462 ;; possible to make them work correctly wrt the comment and string
2463 ;; separators, one-line paragraphs etc. Unfortunately, when it comes
2464 ;; to it, there's quite a lot of special cases to handle which makes
2465 ;; the code anything but simple. The intention is that it will work
2466 ;; with any well-written text filling package that preserves a fill
2469 ;; We temporarily mask comment starters and enders as necessary for
2470 ;; the filling code to do its job on a seemingly normal text block.
2471 ;; We do _not_ mask the fill prefix, so it's up to the filling code to
2472 ;; preserve it correctly (especially important when filling C++ style
2473 ;; line comments). By default, we set up and use adaptive-fill-mode,
2474 ;; which is standard in all supported Emacs flavors.
2476 (defun c-guess-fill-prefix (lit-limits lit-type
)
2477 ;; Determine the appropriate comment fill prefix for a block or line
2478 ;; comment. Return a cons of the prefix string and the column where
2479 ;; it ends. If fill-prefix is set, it'll override. Note that this
2480 ;; function also uses the value of point in some heuristics.
2481 (let* ((here (point))
2482 (prefix-regexp (concat "[ \t]*\\("
2483 c-current-comment-prefix
2485 (comment-start-regexp (if (eq lit-type
'c
++)
2487 comment-start-skip
))
2488 prefix-line comment-prefix res comment-text-end
)
2491 (setq res
(cons fill-prefix
2492 ;; Ugly way of getting the column after the fill
2493 ;; prefix; it'd be nice with a current-column
2494 ;; that works on strings..
2495 (let ((buffer-modified (buffer-modified-p))
2496 (buffer-undo-list t
)
2500 (insert-and-inherit "\n" fill-prefix
)
2502 (delete-region start
(point))
2503 (set-buffer-modified-p buffer-modified
))))))
2506 ;; Set fallback for comment-prefix if none is found.
2507 (setq comment-prefix
"// "
2508 comment-text-end
(cdr lit-limits
))
2510 (if (> (point) (car lit-limits
))
2511 ;; The current line is not the comment starter, so the
2512 ;; comment has more than one line, and it can therefore be
2513 ;; used to find the comment fill prefix.
2514 (setq prefix-line
(point))
2515 (goto-char (car lit-limits
))
2516 (if (and (= (forward-line 1) 0)
2517 (< (point) (cdr lit-limits
)))
2518 ;; The line after the comment starter is inside the
2519 ;; comment, so we can use it.
2520 (setq prefix-line
(point))
2521 ;; The comment is only one line. Take the comment prefix
2522 ;; from it and keep the indentation.
2523 (goto-char (car lit-limits
))
2524 (if (looking-at prefix-regexp
)
2525 (goto-char (match-end 0))
2527 (skip-chars-forward " \t"))
2529 (if (eq (c-point 'boi
) (car lit-limits
))
2530 ;; There is only whitespace before the comment
2531 ;; starter; take the prefix straight from this line.
2532 (setq str
(buffer-substring-no-properties
2533 (c-point 'bol
) (point))
2534 col
(current-column))
2535 ;; There is code before the comment starter, so we
2536 ;; have to temporarily insert and indent a new line to
2537 ;; get the right space/tab mix in the indentation.
2538 (let ((buffer-modified (buffer-modified-p))
2539 (buffer-undo-list t
)
2540 (prefix-len (- (point) (car lit-limits
)))
2544 (goto-char (car lit-limits
))
2545 (indent-to (prog1 (current-column)
2548 (forward-char prefix-len
)
2549 (setq str
(buffer-substring-no-properties
2550 (c-point 'bol
) (point))
2551 col
(current-column)))
2552 (delete-region (car lit-limits
) tmp
)
2553 (set-buffer-modified-p buffer-modified
))))
2555 (if (or (string-match "\\s \\'" str
) (not (eolp)))
2557 ;; The prefix ends the line with no whitespace
2558 ;; after it. Default to a single space.
2559 (cons (concat str
" ") (1+ col
))))
2562 (setq comment-text-end
2564 (goto-char (- (cdr lit-limits
) 2))
2565 (if (looking-at "\\*/") (point) (cdr lit-limits
))))
2568 (if (and (> (point) (car lit-limits
))
2569 (not (and (looking-at "[ \t]*\\*/")
2570 (eq (cdr lit-limits
) (match-end 0)))))
2571 ;; The current line is not the comment starter and
2572 ;; contains more than just the ender, so it's good enough
2573 ;; to be used for the comment fill prefix.
2574 (setq prefix-line
(point))
2575 (goto-char (car lit-limits
))
2576 (if (or (/= (forward-line 1) 0)
2577 (>= (point) (cdr lit-limits
))
2578 (and (looking-at "[ \t]*\\*/")
2579 (eq (cdr lit-limits
) (match-end 0)))
2580 (and (looking-at prefix-regexp
)
2581 (<= (1- (cdr lit-limits
)) (match-end 0)))
2582 (and (< here
(point))
2583 (or (not (match-beginning 0))
2584 (looking-at "[ \t]*\\\\?$"))))
2585 ;; The comment is either one line or the next line
2586 ;; contains just the comment ender. Also, if point is
2587 ;; on the comment opener line and the following line is
2588 ;; empty or doesn't match c-current-comment-prefix we
2589 ;; assume that this is in fact a not yet closed one line
2590 ;; comment, so we shouldn't look for the comment prefix
2591 ;; on the next line. In these cases we have no
2592 ;; information about a suitable comment prefix, so we
2593 ;; resort to c-block-comment-prefix.
2594 (setq comment-prefix
(or c-block-comment-prefix
"")
2595 res
(let ((buffer-modified (buffer-modified-p))
2596 (buffer-undo-list t
)
2598 ;; The comment doesn't give any information
2599 ;; about the indentation column. We'll have to
2600 ;; temporarily insert a new comment line and
2601 ;; indent it to find the correct column.
2604 (goto-char (car lit-limits
))
2605 (if (looking-at comment-start-regexp
)
2606 (goto-char (min (match-end 0)
2609 (skip-chars-forward " \t"))
2610 (when (eq (char-syntax (char-before)) ?\
)
2611 ;; If there's ws on the current
2612 ;; line, we'll use it instead of
2613 ;; what's ending comment-prefix.
2614 (setq comment-prefix
2615 (concat (substring comment-prefix
2619 (buffer-substring-no-properties
2621 (skip-chars-backward " \t")
2624 (setq tmp-pre
(point-marker))
2625 ;; We insert an extra non-whitespace
2626 ;; character before the line break and
2627 ;; after comment-prefix in case it's
2628 ;; "" or ends with whitespace.
2629 (insert-and-inherit "x\n" comment-prefix
"x")
2630 (setq tmp-post
(point-marker))
2631 (indent-according-to-mode)
2632 (goto-char (1- tmp-post
))
2633 (cons (buffer-substring-no-properties
2634 (c-point 'bol
) (point))
2637 (delete-region tmp-pre tmp-post
)
2638 (set-marker tmp-pre nil
)
2639 (set-marker tmp-post nil
))
2640 (set-buffer-modified-p buffer-modified
))))
2641 ;; Otherwise the line after the comment starter is good
2642 ;; enough to find the prefix in.
2643 (setq prefix-line
(point)))))))
2646 ;; prefix-line is the bol of a line on which we should try
2647 ;; to find the prefix.
2648 (let* (fb-string fb-endpos
; Contains any fallback prefix found.
2651 (when (and (looking-at prefix-regexp
)
2652 (<= (match-end 0) comment-text-end
))
2653 (unless (eq (match-end 0) (c-point 'eol
))
2654 ;; The match is fine if there's text after it.
2655 (throw 'found
(cons (buffer-substring-no-properties
2656 (match-beginning 0) (match-end 0))
2657 (progn (goto-char (match-end 0))
2658 (current-column)))))
2660 ;; This match is better than nothing, so let's
2661 ;; remember it in case nothing better is found
2663 (setq fb-string
(buffer-substring-no-properties
2664 (match-beginning 0) (match-end 0))
2665 fb-endpos
(match-end 0)))
2668 ;; Search for a line which has text after the prefix
2669 ;; so that we get the proper amount of whitespace
2670 ;; after it. We start with the current line, then
2671 ;; search backwards, then forwards.
2672 (goto-char prefix-line
)
2673 (when (and (funcall test-line
)
2674 (or (/= (match-end 1) (match-end 0))
2675 ;; The whitespace is sucked up by the
2676 ;; first [ \t]* glob if the prefix is empty.
2677 (and (= (match-beginning 1) (match-end 1))
2678 (/= (match-beginning 0) (match-end 0)))))
2679 ;; If the current line doesn't have text but do
2680 ;; have whitespace after the prefix, we'll use it.
2681 (throw 'found
(cons fb-string
2682 (progn (goto-char fb-endpos
)
2683 (current-column)))))
2684 (if (eq lit-type
'c
++)
2685 ;; For line comments we can search up to and
2686 ;; including the first line.
2687 (while (and (zerop (forward-line -
1))
2688 (>= (point) (car lit-limits
)))
2689 (funcall test-line
))
2690 ;; For block comments we must stop before the
2692 (while (and (zerop (forward-line -
1))
2693 (> (point) (car lit-limits
)))
2694 (funcall test-line
)))
2695 (goto-char prefix-line
)
2696 (while (and (zerop (forward-line 1))
2697 (< (point) (cdr lit-limits
)))
2698 (funcall test-line
))
2699 (goto-char prefix-line
)
2702 ;; A good line wasn't found, but at least we have a
2703 ;; fallback that matches the comment prefix regexp.
2704 (cond ((or (string-match "\\s \\'" fb-string
)
2706 (goto-char fb-endpos
)
2708 ;; There are ws or text after the prefix, so
2710 (cons fb-string
(current-column)))
2712 ;; Check if there's any whitespace padding
2713 ;; on the comment start line that we can
2714 ;; use after the prefix.
2715 (goto-char (car lit-limits
))
2716 (if (looking-at comment-start-regexp
)
2717 (goto-char (match-end 0))
2719 (skip-chars-forward " \t"))
2721 (eq (char-syntax (char-before)) ?\
)))
2722 (setq fb-string
(buffer-substring-no-properties
2724 (skip-chars-backward " \t")
2727 (goto-char fb-endpos
)
2728 (skip-chars-backward " \t")
2729 (let ((buffer-modified (buffer-modified-p))
2730 (buffer-undo-list t
)
2732 ;; Got to mess in the buffer once again to
2733 ;; ensure the column gets correct. :P
2736 (insert-and-inherit fb-string
)
2737 (cons (buffer-substring-no-properties
2741 (delete-region tmp
(point))
2742 (set-buffer-modified-p buffer-modified
))))
2744 ;; Last resort: Just add a single space after
2746 (cons (concat fb-string
" ")
2747 (progn (goto-char fb-endpos
)
2748 (1+ (current-column)))))))
2749 ;; The line doesn't match the comment prefix regexp.
2751 ;; We have a fallback for line comments that we must use.
2752 (cons (concat (buffer-substring-no-properties
2753 prefix-line
(c-point 'boi
))
2755 (progn (back-to-indentation)
2756 (+ (current-column) (length comment-prefix
))))
2757 ;; Assume we are dealing with a "free text" block
2758 ;; comment where the lines doesn't have any comment
2759 ;; prefix at all and we should just fill it as
2764 (defun c-mask-comment (fill-paragraph apply-outside-literal fun
&rest args
)
2765 ;; Calls FUN with ARGS ar arguments. If point is inside a comment,
2766 ;; the comment starter and ender are masked and the buffer is
2767 ;; narrowed to make it look like a normal paragraph during the call.
2769 ;; FILL-PARAGRAPH is non-nil if called for paragraph filling. The
2770 ;; position of point is then less significant when doing masking and
2773 ;; beg and end limits the region to narrow. end is a marker.
2775 ;; tmp-pre and tmp-post mark strings that are temporarily
2776 ;; inserted at the start and end of the region. tmp-pre is a
2777 ;; cons of the positions of the prepended string. tmp-post is
2778 ;; a marker pointing to the single character of the appended
2781 ;; If hang-ender-stuck isn't nil, the comment ender is
2782 ;; hanging. In that case it's set to the number of spaces
2783 ;; that should be between the text and the ender.
2786 (c-lit-limits c-lit-limits
)
2787 (c-lit-type c-lit-type
))
2788 ;; Restore point on undo. It's necessary since we do a lot of
2789 ;; hidden inserts and deletes below that should be as transparent
2791 (if (and buffer-undo-list
(not (eq buffer-undo-list t
)))
2792 (setq buffer-undo-list
(cons (point) buffer-undo-list
)))
2794 ;; Widen to catch comment limits correctly.
2796 (unless c-lit-limits
2797 (setq c-lit-limits
(c-literal-limits nil fill-paragraph
)))
2798 (setq c-lit-limits
(c-collect-line-comments c-lit-limits
))
2800 (setq c-lit-type
(c-literal-type c-lit-limits
))))
2802 (unless (c-safe (backward-char)
2806 (forward-paragraph))
2807 (setq end
(point-marker)))
2809 (unless (c-safe (forward-char)
2810 (backward-paragraph)
2813 (backward-paragraph))
2818 ((eq c-lit-type
'c
++) ; Line comment.
2820 ;; Limit to the comment or paragraph end, whichever
2822 (set-marker end
(min end
(cdr c-lit-limits
)))
2823 (when (<= beg
(car c-lit-limits
))
2824 ;; The region includes the comment starter, so we must
2826 (goto-char (car c-lit-limits
))
2827 (back-to-indentation)
2828 (if (eq (point) (car c-lit-limits
))
2829 ;; Include the first line in the region.
2830 (setq beg
(c-point 'bol
))
2831 ;; The first line contains code before the
2832 ;; comment. We must fake a line that doesn't.
2835 ((eq c-lit-type
'c
) ; Block comment.
2836 (when (>= end
(cdr c-lit-limits
))
2837 ;; The region includes the comment ender which we might
2838 ;; want to keep together with the last word.
2839 (unless (save-excursion
2840 (goto-char (cdr c-lit-limits
))
2842 (and (looking-at (concat "[ \t]*\\("
2843 c-current-comment-prefix
2845 (eq (cdr c-lit-limits
) (match-end 0))
2846 ;; Leave the comment ender on its own line.
2847 (set-marker end
(point))))
2848 (when fill-paragraph
2849 ;; The comment ender should hang. Replace all cruft
2850 ;; between it and the last word with one or two 'x'
2851 ;; and include it in the region. We'll change them
2852 ;; back to spaces afterwards.
2853 (let* ((ender-start (save-excursion
2854 (goto-char (cdr c-lit-limits
))
2855 (skip-syntax-backward "^w ")
2857 (point-rel (- ender-start here
))
2860 (goto-char (cdr c-lit-limits
))
2861 (setq tmp-post
(point-marker))
2863 (set-marker end
(point))
2865 (if (and (looking-at (concat "[ \t]*\\(\\("
2866 c-current-comment-prefix
2868 (eq ender-start
(match-end 0)))
2869 ;; The comment ender is prefixed by nothing
2870 ;; but a comment line prefix. Remove it
2871 ;; along with surrounding ws.
2872 (setq spaces
(- (match-end 1) (match-end 2)))
2873 (goto-char ender-start
))
2874 (skip-chars-backward " \t\r\n")
2875 (if (/= (point) ender-start
)
2877 (if (<= here
(point))
2878 ;; Don't adjust point below if it's
2879 ;; before the string we replace.
2880 (setq point-rel -
1))
2881 ;; Keep one or two spaces between the text and
2882 ;; the ender, depending on how many there are now.
2884 (setq spaces
(- ender-start
(point))))
2887 (if sentence-end-double-space
2 1))
2889 ;; Insert the filler first to keep marks right.
2890 (insert-char ?x spaces t
)
2891 (delete-region (point) (+ ender-start spaces
))
2892 (setq hang-ender-stuck spaces
)
2894 (and (>= point-rel
0)
2895 (- (point) (min point-rel spaces
)))))
2896 (setq point-rel nil
)))
2898 ;; Point was in the middle of the string we
2899 ;; replaced above, so put it back in the same
2900 ;; relative position, counting from the end.
2901 (goto-char point-rel
))
2903 (when (<= beg
(car c-lit-limits
))
2904 ;; The region includes the comment starter.
2906 (goto-char (car c-lit-limits
))
2907 (if (looking-at (concat "\\(" comment-start-skip
"\\)$"))
2908 ;; Begin with the next line.
2909 (setq beg
(c-point 'bonl
))
2910 ;; Fake the fill prefix in the first line.
2911 (setq tmp-pre t
)))))
2912 ((eq c-lit-type
'string
) ; String.
2914 (when (>= end
(cdr c-lit-limits
))
2915 (goto-char (1- (cdr c-lit-limits
)))
2916 (setq tmp-post
(point-marker))
2918 (set-marker end
(point)))
2919 (when (<= beg
(car c-lit-limits
))
2920 (goto-char (1+ (car c-lit-limits
)))
2921 (setq beg
(if (looking-at "\\\\$")
2922 ;; Leave the start line if it's
2923 ;; nothing but an escaped newline.
2928 ;; Temporarily insert the fill prefix after the comment
2929 ;; starter so that the first line looks like any other
2930 ;; comment line in the narrowed region.
2931 (setq fill
(c-guess-fill-prefix c-lit-limits c-lit-type
))
2932 (unless (string-match (concat "\\`[ \t]*\\("
2933 c-current-comment-prefix
2936 ;; Oops, the prefix doesn't match the comment prefix
2937 ;; regexp. This could produce very confusing
2938 ;; results with adaptive fill packages together with
2939 ;; the insert prefix magic below, since the prefix
2940 ;; often doesn't appear at all. So let's warn about
2943 Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S"
2945 ;; Find the right spot on the line, break it, insert
2946 ;; the fill prefix and make sure we're back in the
2947 ;; same column by temporarily prefixing the first word
2948 ;; with a number of 'x'.
2950 (goto-char (car c-lit-limits
))
2951 (if (looking-at (if (eq c-lit-type
'c
++)
2952 c-current-comment-prefix
2953 comment-start-skip
))
2954 (goto-char (match-end 0))
2956 (skip-chars-forward " \t"))
2957 (while (and (< (current-column) (cdr fill
))
2960 (let ((col (current-column)))
2961 (setq beg
(1+ (point))
2962 tmp-pre
(list (point)))
2965 (insert-and-inherit "\n" (car fill
))
2966 (insert-char ?x
(- col
(current-column)) t
))
2967 (setcdr tmp-pre
(point))))))
2971 ;; Kludge: If the function that adapts the
2972 ;; fill prefix doesn't produce the required
2973 ;; comment starter for line comments, then
2974 ;; force it by setting fill-prefix.
2975 (when (and (eq c-lit-type
'c
++)
2976 ;; Kludge the kludge:
2977 ;; filladapt-mode doesn't have
2978 ;; this problem, but it doesn't
2979 ;; override fill-context-prefix
2980 ;; currently (version 2.12).
2981 (not (and (boundp 'filladapt-mode
)
2985 (or (fill-context-prefix beg end
)
2987 (car (or fill
(c-guess-fill-prefix
2988 c-lit-limits c-lit-type
))))))
2989 ;; Save the relative position of point if it's
2990 ;; outside the region we're going to narrow. Want
2991 ;; to restore it in that case, but otherwise it
2992 ;; should be moved according to the called
2994 (point-rel (cond ((< (point) beg
) (- (point) beg
))
2995 ((> (point) end
) (- (point) end
)))))
2996 ;; Preparations finally done! Now we can call the
3000 (narrow-to-region beg end
)
3003 ;; Restore point if it was outside the region.
3005 (goto-char (+ beg point-rel
))
3006 (goto-char (+ end point-rel
))))))
3007 (when apply-outside-literal
3009 (when (consp tmp-pre
)
3010 (delete-region (car tmp-pre
) (cdr tmp-pre
)))
3013 (goto-char tmp-post
)
3015 (when hang-ender-stuck
3016 ;; Preserve point even if it's in the middle of the string
3017 ;; we replace; save-excursion doesn't work in that case.
3019 (goto-char tmp-post
)
3020 (skip-syntax-backward "^w ")
3021 (forward-char (- hang-ender-stuck
))
3022 (insert-char ?\ hang-ender-stuck t
)
3023 (delete-char hang-ender-stuck
)
3025 (set-marker tmp-post nil
))
3026 (set-marker end nil
))))
3028 (defun c-fill-paragraph (&optional arg
)
3029 "Like \\[fill-paragraph] but handles C and C++ style comments.
3030 If any of the current line is a comment or within a comment, fill the
3031 comment or the paragraph of it that point is in, preserving the
3032 comment indentation or line-starting decorations (see the
3033 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
3036 If point is inside multiline string literal, fill it. This currently
3037 does not respect escaped newlines, except for the special case when it
3038 is the very first thing in the string. The intended use for this rule
3039 is in situations like the following:
3041 char description[] = \"\\
3042 A very long description of something that you want to fill to make
3043 nicely formatted output.\"\;
3045 If point is in any other situation, i.e. in normal code, do nothing.
3047 Optional prefix ARG means justify paragraph as well."
3049 (let ((fill-paragraph-function
3050 ;; Avoid infinite recursion.
3051 (if (not (eq fill-paragraph-function
'c-fill-paragraph
))
3052 fill-paragraph-function
)))
3053 (c-mask-comment t nil
'fill-paragraph arg
))
3054 ;; Always return t. This has the effect that if filling isn't done
3055 ;; above, it isn't done at all, and it's therefore effectively
3056 ;; disabled in normal code.
3059 (defun c-do-auto-fill ()
3060 ;; Do automatic filling if not inside a context where it should be
3062 (let ((c-auto-fill-prefix
3063 ;; The decision whether the line should be broken is actually
3064 ;; done in c-indent-new-comment-line, which do-auto-fill
3065 ;; calls to break lines. We just set this special variable
3066 ;; so that we'll know when we're called from there. It's
3067 ;; also used to detect whether fill-prefix is user set or
3068 ;; generated automatically by do-auto-fill.
3070 (c-mask-comment nil t
'do-auto-fill
)))
3072 (defun c-indent-new-comment-line (&optional soft allow-auto-fill
)
3073 "Break line at point and indent, continuing comment or macro if within one.
3074 If inside a comment and `comment-multi-line' is non-nil, the
3075 indentation and line prefix are preserved (see the
3076 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
3077 details). If inside a single line comment and `comment-multi-line' is
3078 nil, a new comment of the same type is started on the next line and
3079 indented as appropriate for comments. If inside a macro, a line
3080 continuation backslash is inserted and aligned as appropriate, and the
3081 new line is indented according to `c-syntactic-indentation'.
3083 If a fill prefix is specified, it overrides all the above."
3084 ;; allow-auto-fill is used from c-context-line-break to allow auto
3085 ;; filling to break the line more than once. Since this function is
3086 ;; used from auto-fill itself, that's normally disabled to avoid
3087 ;; unnecessary recursion.
3089 (let ((fill-prefix fill-prefix
)
3092 (delete-horizontal-space)
3094 (insert-and-inherit ?
\n)
3095 (newline (if allow-auto-fill nil
1)))))
3096 ;; Already know the literal type and limits when called from
3097 ;; c-context-line-break.
3098 (c-lit-limits c-lit-limits
)
3099 (c-lit-type c-lit-type
)
3100 (c-macro-start c-macro-start
))
3101 (when (not (eq c-auto-fill-prefix t
))
3102 ;; Called from do-auto-fill.
3103 (unless c-lit-limits
3104 (setq c-lit-limits
(c-literal-limits nil nil t
)))
3106 (setq c-lit-type
(c-literal-type c-lit-limits
)))
3107 (if (memq (cond ((c-query-and-set-macro-start) 'cpp
)
3108 ((null c-lit-type
) 'code
)
3111 (setq fill-prefix t
) ; Used as flag in the cond.
3112 (if (and (null c-auto-fill-prefix
)
3114 (<= (c-point 'bol
) (car c-lit-limits
)))
3115 ;; The adaptive fill function has generated a prefix, but
3116 ;; we're on the first line in a block comment so it'll be
3117 ;; wrong. Ignore it to guess a better one below.
3118 (setq fill-prefix nil
)
3119 (when (and (eq c-lit-type
'c
++)
3120 (not (string-match "\\`[ \t]*//" (or fill-prefix
""))))
3121 ;; Kludge: If the function that adapted the fill prefix
3122 ;; doesn't produce the required comment starter for line
3123 ;; comments, then we ignore it.
3124 (setq fill-prefix nil
)))
3126 (cond ((eq fill-prefix t
)
3127 ;; A call from do-auto-fill which should be ignored.
3130 ;; A fill-prefix overrides anything.
3131 (funcall do-line-break
)
3132 (insert-and-inherit fill-prefix
))
3134 (unless c-lit-limits
3135 (setq c-lit-limits
(c-literal-limits)))
3137 (setq c-lit-type
(c-literal-type c-lit-limits
)))
3138 (memq c-lit-type
'(c c
++)))
3139 ;; Some sort of comment.
3140 (if (or comment-multi-line
3142 (goto-char (car c-lit-limits
))
3144 (< (point) (cdr c-lit-limits
))))
3145 ;; Inside a comment that should be continued.
3146 (let ((fill (c-guess-fill-prefix
3148 (c-collect-line-comments c-lit-limits
))
3152 (or (and (eq c-lit-type
'c
)
3154 (goto-char (- (cdr c-lit-limits
) 2))
3155 (if (looking-at "\\*/") (point))))
3156 (cdr c-lit-limits
))))
3157 ;; Skip forward past the fill prefix in case
3158 ;; we're standing in it.
3160 ;; FIXME: This doesn't work well in cases like
3162 ;; /* Bla bla bla bla bla
3165 ;; If point is on the 'B' then the line will be
3166 ;; broken after "Bla b".
3167 (while (and (< (current-column) (cdr fill
))
3170 (if (and (> (point) comment-text-end
)
3171 (> (c-point 'bol
) (car c-lit-limits
)))
3173 ;; The skip takes us out of the (block)
3174 ;; comment; insert the fill prefix at bol
3175 ;; instead and keep the position.
3176 (setq pos
(copy-marker pos t
))
3178 (insert-and-inherit (car fill
))
3179 (if soft
(insert-and-inherit ?
\n) (newline 1))
3181 (set-marker pos nil
))
3182 ;; Don't break in the middle of a comment starter
3184 (cond ((> (point) comment-text-end
)
3185 (goto-char comment-text-end
))
3186 ((< (point) (+ (car c-lit-limits
) 2))
3187 (goto-char (+ (car c-lit-limits
) 2))))
3188 (funcall do-line-break
)
3189 (insert-and-inherit (car fill
))))
3190 ;; Inside a comment that should be broken.
3191 (let ((comment-start comment-start
)
3192 (comment-end comment-end
)
3194 (if (eq c-lit-type
'c
)
3195 (unless (string-match "[ \t]*/\\*" comment-start
)
3196 (setq comment-start
"/* " comment-end
" */"))
3197 (unless (string-match "[ \t]*//" comment-start
)
3198 (setq comment-start
"// " comment-end
"")))
3199 (setq col
(save-excursion
3200 (back-to-indentation)
3202 (funcall do-line-break
)
3203 (when (and comment-end
(not (equal comment-end
"")))
3205 (insert-and-inherit comment-end
)
3207 ;; c-comment-indent may look at the current
3208 ;; indentation, so let's start out with the same
3209 ;; indentation as the previous one.
3211 (insert-and-inherit comment-start
)
3212 (indent-for-comment))))
3213 ((c-query-and-set-macro-start)
3215 (unless (looking-at "[ \t]*\\\\$")
3216 ;; Do not clobber the alignment of the line continuation
3217 ;; slash; c-backslash-region might look at it.
3218 (delete-horizontal-space))
3219 ;; Got an asymmetry here: In normal code this command
3220 ;; doesn't indent the next line syntactically, and otoh a
3221 ;; normal syntactically indenting newline doesn't continue
3223 (c-newline-and-indent (if allow-auto-fill nil
1)))
3225 ;; Somewhere else in the code.
3226 (let ((col (save-excursion
3228 (while (and (looking-at "[ \t]*\\\\?$")
3229 (= (forward-line -
1) 0)))
3230 (current-indentation))))
3231 (funcall do-line-break
)
3232 (indent-to col
))))))
3234 (defalias 'c-comment-line-break-function
'c-indent-new-comment-line
)
3235 (make-obsolete 'c-comment-line-break-function
'c-indent-new-comment-line
)
3237 ;; advice for indent-new-comment-line for older Emacsen
3238 (unless (boundp 'comment-line-break-function
)
3239 (defvar c-inside-line-break-advice nil
)
3240 (defadvice indent-new-comment-line
(around c-line-break-advice
3241 activate preactivate
)
3242 "Call `c-indent-new-comment-line' if in CC Mode."
3243 (if (or c-inside-line-break-advice
3244 (not c-buffer-is-cc-mode
))
3246 (let ((c-inside-line-break-advice t
))
3247 (c-indent-new-comment-line (ad-get-arg 0))))))
3249 (defun c-context-line-break ()
3250 "Do a line break suitable to the context.
3252 When point is outside a comment or macro, insert a newline and indent
3253 according to the syntactic context, unless `c-syntactic-indentation'
3254 is nil, in which case the new line is indented as the previous
3255 non-empty line instead.
3257 When point is inside the content of a preprocessor directive, a line
3258 continuation backslash is inserted before the line break and aligned
3259 appropriately. The end of the cpp directive doesn't count as inside
3262 When point is inside a comment, continue it with the appropriate
3263 comment prefix (see the `c-comment-prefix-regexp' and
3264 `c-block-comment-prefix' variables for details). The end of a
3265 C++-style line comment doesn't count as inside it."
3267 (let* ((c-lit-limits (c-literal-limits nil nil t
))
3268 (c-lit-type (c-literal-type c-lit-limits
))
3269 (c-macro-start c-macro-start
))
3270 (if (or (eq c-lit-type
'c
)
3271 (and (eq c-lit-type
'c
++)
3273 (skip-chars-forward " \t")
3275 (1- (cdr (setq c-lit-limits
3276 (c-collect-line-comments c-lit-limits
))))))
3277 (and (or (not (looking-at "\\s *$"))
3278 (eq (char-before) ?
\\))
3279 (c-query-and-set-macro-start)
3281 (goto-char c-macro-start
)
3282 (if (looking-at "#[ \t]*[a-zA-Z0-9!]+")
3283 (goto-char (match-end 0)))
3286 (let ((comment-multi-line t
)
3288 (c-indent-new-comment-line nil t
))
3289 (delete-horizontal-space)
3291 ;; c-indent-line may look at the current indentation, so let's
3292 ;; start out with the same indentation as the previous line.
3293 (let ((col (save-excursion
3295 (while (and (looking-at "[ \t]*\\\\?$")
3296 (= (forward-line -
1) 0)))
3297 (current-indentation))))
3299 (indent-according-to-mode))))
3301 (defun c-context-open-line ()
3302 "Insert a line break suitable to the context and leave point before it.
3303 This is the `c-context-line-break' equivalent to `open-line', which is
3304 normally bound to C-o. See `c-context-line-break' for the details."
3306 (let ((here (point)))
3309 ;; Temporarily insert a non-whitespace char to keep any
3310 ;; preceding whitespace intact.
3312 (c-context-line-break))
3317 (cc-provide 'cc-cmds
)
3319 ;;; cc-cmds.el ends here