1 ;;; cc-cmds.el --- user level commands for CC Mode
3 ;; Copyright (C) 1985,1987,1992-2003 Free Software Foundation, Inc.
5 ;; Authors: 1998- Martin Stjernholm
6 ;; 1992-1999 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Version: See cc-mode.el
12 ;; Keywords: c languages oop
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to
28 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
37 (if (and (boundp 'byte-compile-dest-file
)
38 (stringp byte-compile-dest-file
))
39 (cons (file-name-directory byte-compile-dest-file
) load-path
)
41 (load "cc-bytecomp" nil t
)))
45 (cc-require 'cc-engine
)
47 ;; Silence the compiler.
48 (cc-bytecomp-defvar delete-key-deletes-forward
) ; XEmacs 20+
49 (cc-bytecomp-defun delete-forward-p) ; XEmacs 21+
50 (cc-bytecomp-obsolete-fun insert-and-inherit
) ; Marked obsolete in XEmacs 19
51 (cc-bytecomp-defvar filladapt-mode
) ; c-fill-paragraph contains a kludge
52 ; which looks at this.
55 (defvar c-fix-backslashes t
)
57 (defun c-shift-line-indentation (shift-amt)
58 ;; This function does not do any hidden buffer changes.
59 (let ((pos (- (point-max) (point)))
60 (c-macro-start c-macro-start
)
64 (when (and (c-query-and-set-macro-start)
65 (looking-at "[ \t]*\\\\$")
67 (skip-chars-backward " \t")
71 (setq tmp-char-inserted t
))
73 (let ((col (current-indentation)))
74 (delete-region (c-point 'bol
) (c-point 'boi
))
76 (indent-to (+ col shift-amt
)))
77 (when tmp-char-inserted
79 ;; If initial point was within line's indentation and we're not on
80 ;; a line with a line continuation in a macro, position after the
81 ;; indentation. Else stay at same point in text.
82 (if (and (< (point) (c-point 'boi
))
83 (not tmp-char-inserted
))
85 (if (> (- (point-max) pos
) (point))
86 (goto-char (- (point-max) pos
))))))
88 (defun c-indent-line (&optional syntax quiet ignore-point-pos
)
89 "Indent the current line according to the syntactic context,
90 if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the
91 syntactic information for the current line. Be silent about syntactic
92 errors if the optional argument QUIET is non-nil, even if
93 `c-report-syntactic-errors' is non-nil. Normally the position of
94 point is used to decide where the old indentation is on a lines that
95 is otherwise empty \(ignoring any line continuation backslash), but
96 that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of
97 indentation change \(in columns)."
99 ;; This function does not do any hidden buffer changes.
101 (let ((line-cont-backslash (save-excursion
103 (eq (char-before) ?
\\)))
104 (c-fix-backslashes c-fix-backslashes
)
107 (when (and (not ignore-point-pos
)
110 (looking-at (if line-cont-backslash
113 (<= (point) (match-end 1)))
114 ;; Delete all whitespace after point if there's only whitespace
115 ;; on the line, so that any code that does back-to-indentation
116 ;; or similar gets the current column in this case. If this
117 ;; removes a line continuation backslash it'll be restored
119 (unless c-auto-align-backslashes
120 ;; Should try to keep the backslash alignment
123 (goto-char (match-end 0))
124 (setq bs-col
(1- (current-column)))))
125 (delete-region (point) (match-end 0))
126 (setq c-fix-backslashes t
))
127 (if c-syntactic-indentation
128 (setq c-parsing-error
129 (or (let ((c-parsing-error nil
)
132 (and (boundp 'c-syntactic-context
)
133 c-syntactic-context
))))
134 (c-save-buffer-state (indent)
135 (unless c-syntactic-context
136 (setq c-syntactic-context
(c-guess-basic-syntax)))
137 (setq indent
(c-get-syntactic-indentation
138 c-syntactic-context
))
139 (and (not (c-echo-parsing-error quiet
))
140 c-echo-syntactic-information-p
141 (message "syntax: %s, indent: %d"
142 c-syntactic-context indent
))
143 (setq shift-amt
(- indent
(current-indentation))))
144 (c-shift-line-indentation shift-amt
)
145 (run-hooks 'c-special-indent-hook
)
150 (while (and (= (forward-line -
1) 0)
151 (if (looking-at "\\s *\\\\?$")
153 (setq indent
(current-indentation))
155 (setq shift-amt
(- indent
(current-indentation)))
156 (c-shift-line-indentation shift-amt
)))
157 (when (and c-fix-backslashes line-cont-backslash
)
162 (when c-auto-align-backslashes
163 ;; Realign the line continuation backslash.
164 (c-backslash-region (point) (point) nil t
))))
167 (defun c-newline-and-indent (&optional newline-arg
)
168 "Inserts a newline and indents the new line.
169 This function fixes line continuation backslashes if inside a macro,
170 and takes care to set the indentation before calling
171 `indent-according-to-mode', so that lineup functions like
172 `c-lineup-dont-change' works better."
174 ;; This function does not do any hidden buffer changes.
176 ;; TODO: Backslashes before eol in comments and literals aren't
178 (let ((c-macro-start (c-query-macro-start))
179 ;; Avoid calling c-backslash-region from c-indent-line if it's
180 ;; called during the newline call, which can happen due to
181 ;; c-electric-continued-statement, for example. We also don't
182 ;; want any backslash alignment from indent-according-to-mode.
183 (c-fix-backslashes nil
)
184 has-backslash insert-backslash
189 (while (and (looking-at "[ \t]*\\\\?$")
190 (= (forward-line -
1) 0)))
191 (setq col
(current-indentation)))
193 (if (and (eolp) (eq (char-before) ?
\\))
194 (setq insert-backslash t
196 (setq has-backslash
(eq (char-before (c-point 'eol
)) ?
\\))))
197 (newline newline-arg
)
202 ;; The backslash stayed on the previous line. Insert one
203 ;; before calling c-backslash-region, so that
204 ;; bs-col-after-end in it works better. Fixup the
205 ;; backslashes on the newly inserted line.
208 (c-backslash-region (point) (point) nil t
))
209 ;; The backslash moved to the new line, if there was any. Let
210 ;; c-backslash-region fix a backslash on the previous line,
211 ;; and the one that might be on the new line.
212 ;; c-auto-align-backslashes is intentionally ignored here;
213 ;; maybe the moved backslash should be left alone if it's set,
214 ;; but we fix both lines on the grounds that the old backslash
215 ;; has been moved anyway and is now in a different context.
216 (c-backslash-region start
(if has-backslash
(point) start
) nil t
)))
217 (when c-syntactic-indentation
218 ;; Reindent syntactically. The indentation done above is not
219 ;; wasted, since c-indent-line might look at the current
221 (let ((c-syntactic-context (c-save-buffer-state nil
222 (c-guess-basic-syntax))))
223 ;; We temporarily insert another line break, so that the
224 ;; lineup functions will see the line as empty. That makes
225 ;; e.g. c-lineup-cpp-define more intuitive since it then
226 ;; proceeds to the preceding line in this case.
228 (delete-horizontal-space)
229 (setq start
(- (point-max) (point)))
233 (indent-according-to-mode))
234 (goto-char (- (point-max) start
))
237 ;; Must align the backslash again after reindentation. The
238 ;; c-backslash-region call above can't be optimized to ignore
239 ;; this line, since it then won't align correctly with the
240 ;; lines below if the first line in the macro is broken.
241 (c-backslash-region (point) (point) nil t
)))))
243 (defun c-show-syntactic-information (arg)
244 "Show syntactic information for current line.
245 With universal argument, inserts the analysis as a comment on that line."
247 (let* ((c-parsing-error nil
)
248 (syntax (if (boundp 'c-syntactic-context
)
249 ;; Use `c-syntactic-context' in the same way as
250 ;; `c-indent-line', to be consistent.
252 (c-save-buffer-state nil
253 (c-guess-basic-syntax)))))
254 (if (not (consp arg
))
255 (message "syntactic analysis: %s" syntax
)
257 (insert-and-inherit (format "%s" syntax
))
259 (c-keep-region-active))
261 (defun c-syntactic-information-on-region (from to
)
262 "Inserts a comment with the syntactic analysis on every line in the region."
266 (narrow-to-region from to
)
267 (goto-char (point-min))
269 (c-show-syntactic-information '(0))
273 (defun c-toggle-syntactic-indentation (&optional arg
)
274 "Toggle syntactic indentation.
275 Optional numeric ARG, if supplied, turns on syntactic indentation when
276 positive, turns it off when negative, and just toggles it when zero or
279 When syntactic indentation is turned on (the default), the indentation
280 functions and the electric keys indent according to the syntactic
281 context keys, when applicable.
283 When it's turned off, the electric keys does no reindentation, the
284 indentation functions indents every new line to the same level as the
285 previous nonempty line, and \\[c-indent-command] adjusts the
286 indentation in seps specified `c-basic-offset'. The indentation style
287 has no effect in this mode, nor any of the indentation associated
288 variables, e.g. `c-special-indent-hook'.
290 This command sets the variable `c-syntactic-indentation'."
292 (setq c-syntactic-indentation
293 (c-calculate-state arg c-syntactic-indentation
))
294 (c-keep-region-active))
296 (defun c-toggle-auto-state (&optional arg
)
297 "Toggle auto-newline feature.
298 Optional numeric ARG, if supplied, turns on auto-newline when
299 positive, turns it off when negative, and just toggles it when zero or
302 When the auto-newline feature is enabled (as evidenced by the `/a' or
303 `/ah' on the modeline after the mode name) newlines are automatically
304 inserted after special characters such as brace, comma, semi-colon,
307 (setq c-auto-newline
(c-calculate-state arg c-auto-newline
))
309 (c-keep-region-active))
311 (defun c-toggle-hungry-state (&optional arg
)
312 "Toggle hungry-delete-key feature.
313 Optional numeric ARG, if supplied, turns on hungry-delete when
314 positive, turns it off when negative, and just toggles it when zero or
317 When the hungry-delete-key feature is enabled (as evidenced by the
318 `/h' or `/ah' on the modeline after the mode name) the delete key
319 gobbles all preceding whitespace in one fell swoop."
321 (setq c-hungry-delete-key
(c-calculate-state arg c-hungry-delete-key
))
323 (c-keep-region-active))
325 (defun c-toggle-auto-hungry-state (&optional arg
)
326 "Toggle auto-newline and hungry-delete-key features.
327 Optional numeric ARG, if supplied, turns on auto-newline and
328 hungry-delete when positive, turns them off when negative, and just
329 toggles them when zero or left out.
331 See `c-toggle-auto-state' and `c-toggle-hungry-state' for details."
333 (setq c-auto-newline
(c-calculate-state arg c-auto-newline
))
334 (setq c-hungry-delete-key
(c-calculate-state arg c-hungry-delete-key
))
336 (c-keep-region-active))
341 (defun c-electric-backspace (arg)
342 "Delete the preceding character or whitespace.
343 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
344 \"/ah\" string on the mode line, then all preceding whitespace is
345 consumed. If however a prefix argument is supplied, or
346 `c-hungry-delete-key' is nil, or point is inside a literal then the
347 function in the variable `c-backspace-function' is called."
349 (if (or (not c-hungry-delete-key
)
352 (funcall c-backspace-function
(prefix-numeric-value arg
))
353 (c-hungry-backspace)))
355 (defun c-hungry-backspace ()
356 "Delete the preceding character or all preceding whitespace
357 back to the previous non-whitespace character.
358 See also \\[c-hungry-delete-forward]."
360 (let ((here (point)))
362 (if (/= (point) here
)
363 (delete-region (point) here
)
364 (funcall c-backspace-function
1))))
366 (defun c-electric-delete-forward (arg)
367 "Delete the following character or whitespace.
368 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
369 \"/ah\" string on the mode line, then all following whitespace is
370 consumed. If however a prefix argument is supplied, or
371 `c-hungry-delete-key' is nil, or point is inside a literal then the
372 function in the variable `c-delete-function' is called."
374 (if (or (not c-hungry-delete-key
)
377 (funcall c-delete-function
(prefix-numeric-value arg
))
378 (c-hungry-delete-forward)))
380 (defun c-hungry-delete-forward ()
381 "Delete the following character or all following whitespace
382 up to the next non-whitespace character.
383 See also \\[c-hungry-backspace]."
385 (let ((here (point)))
387 (if (/= (point) here
)
388 (delete-region (point) here
)
389 (funcall c-delete-function
1))))
391 ;; This function is only used in XEmacs.
392 (defun c-electric-delete (arg)
393 "Deletes preceding or following character or whitespace.
394 This function either deletes forward as `c-electric-delete-forward' or
395 backward as `c-electric-backspace', depending on the configuration:
397 If the function `delete-forward-p' is defined (XEmacs 21) and returns
398 non-nil, it deletes forward. Else, if the variable
399 `delete-key-deletes-forward' is defined (XEmacs 20) and is set to
400 non-nil, it deletes forward. Otherwise it deletes backward.
402 Note: This is the way in XEmacs 20 and later to choose the correct
403 action for the [delete] key, whichever key that means. In other
404 flavors this function isn't used, instead it's left to the user to
405 bind [delete] to either \\[c-electric-delete-forward] or \\[c-electric-backspace] as appropriate
406 \(the keymap `function-key-map' is useful for that). Emacs 21 handles
407 that automatically, though."
409 (if (or (and (fboundp 'delete-forward-p
) ;XEmacs 21
411 (and (boundp 'delete-key-deletes-forward
) ;XEmacs 20
412 delete-key-deletes-forward
))
413 (c-electric-delete-forward arg
)
414 (c-electric-backspace arg
)))
416 (defun c-electric-pound (arg)
417 "Electric pound (`#') insertion.
418 Inserts a `#' character specially depending on the variable
419 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if
420 point is inside a literal or a macro, nothing special happens."
423 (not (memq 'alignleft c-electric-pound-behavior
))
425 (skip-chars-backward " \t")
428 (and (= (forward-line -
1) 0)
430 (eq (char-before) ?
\\))))
432 ;; do nothing special
433 (self-insert-command (prefix-numeric-value arg
))
434 ;; place the pound character at the left edge
435 (let ((pos (- (point-max) (point)))
438 (delete-horizontal-space)
439 (insert last-command-char
)
441 (goto-char (- (point-max) pos
)))
444 (defun c-electric-brace (arg)
447 If the auto-newline feature is turned on, as evidenced by the \"/a\"
448 or \"/ah\" string on the mode line, newlines are inserted before and
449 after braces based on the value of `c-hanging-braces-alist'.
451 Also, the line is re-indented unless a numeric ARG is supplied, the
452 brace is inserted inside a literal, or `c-syntactic-indentation' is
455 This function does various newline cleanups based on the value of
458 (let* ((safepos (c-safe-position (point) (c-parse-state)))
459 (literal (c-in-literal safepos
))
460 ;; We want to inhibit blinking the paren since this will be
461 ;; most disruptive. We'll blink it ourselves later on.
462 (old-blink-paren blink-paren-function
)
463 blink-paren-function
)
466 (self-insert-command (prefix-numeric-value arg
)))
467 ((not (looking-at "[ \t]*\\\\?$"))
468 (self-insert-command (prefix-numeric-value arg
))
469 (if c-syntactic-indentation
470 (indent-according-to-mode)))
473 ;; This is the list of brace syntactic symbols that can
474 ;; hang. If any new ones are added to c-offsets-alist,
475 ;; they should be added here as well.
476 '(class-open class-close defun-open defun-close
477 inline-open inline-close
478 brace-list-open brace-list-close
479 brace-list-intro brace-entry-open
480 block-open block-close
481 substatement-open statement-case-open
482 extern-lang-open extern-lang-close
483 namespace-open namespace-close
484 module-open module-close
485 composition-open composition-close
486 inexpr-class-open inexpr-class-close
487 ;; `statement-cont' is here for the case with a brace
488 ;; list opener inside a statement. C.f. CASE B.2 in
489 ;; `c-guess-continued-construct'.
491 (insertion-point (point))
492 (preserve-p (and (not (bobp))
493 (eq ?\
(char-syntax (char-before)))))
495 (c-echo-syntactic-information-p nil
)
496 delete-temp-newline syntax newlines
)
497 ;; only insert a newline if there is non-whitespace behind us
498 (when (save-excursion
499 (skip-chars-backward " \t")
501 (setq delete-temp-newline
502 (list (point-marker)))
503 (c-newline-and-indent)
504 (setcdr delete-temp-newline
(point-marker)))
507 (if (eq last-command-char ?
{)
508 (setq c-state-cache
(cons (point) c-state-cache
)))
509 (self-insert-command (prefix-numeric-value arg
))
510 (c-save-buffer-state ((c-syntactic-indentation-in-macros t
)
511 (c-auto-newline-analysis t
))
512 ;; Turn on syntactic macro analysis to help with auto
514 (setq syntax
(c-guess-basic-syntax)))
520 ;; Substitute inexpr-class and class-open or
521 ;; class-close with inexpr-class-open or
522 ;; inexpr-class-close.
523 (if (assq 'inexpr-class syntax
)
524 (cond ((assq 'class-open syntax
)
525 '((inexpr-class-open)))
526 ((assq 'class-close syntax
)
527 '((inexpr-class-close)))
530 c-hanging-braces-alist
)
531 '(ignore before after
))))
532 ;; Do not try to insert newlines around a special
533 ;; (Pike-style) brace list.
534 (if (and c-special-brace-lists
536 (c-save-buffer-state nil
537 (c-safe (if (= (char-before) ?
{)
540 (c-looking-at-special-brace-list)))))
542 ;; If syntax is a function symbol, then call it using the
543 ;; defined semantics.
544 (if (and (not (consp (cdr newlines
)))
545 (functionp (cdr newlines
)))
546 (let ((c-syntactic-context syntax
))
548 (funcall (cdr newlines
)
551 ;; does a newline go before the open brace?
552 (when (memq 'before newlines
)
553 ;; we leave the newline we've put in there before,
554 ;; but we need to re-indent the line above
555 (when delete-temp-newline
556 (set-marker (car delete-temp-newline
) nil
)
557 (set-marker (cdr delete-temp-newline
) nil
)
558 (setq delete-temp-newline nil
))
559 (when c-syntactic-indentation
560 (let ((pos (- (point-max) (point)))
563 (indent-according-to-mode)
564 (goto-char (- (point-max) pos
))
565 ;; if the buffer has changed due to the
566 ;; indentation, we need to recalculate syntax for
568 (if (/= (point) here
)
570 ((c-syntactic-indentation-in-macros t
)
571 (c-auto-newline-analysis t
))
572 ;; Turn on syntactic macro analysis to help
573 ;; with auto newlines only.
574 (setq syntax
(c-guess-basic-syntax))))))))
575 ;; must remove the newline we just stuck in (if we really did it)
576 (when delete-temp-newline
578 (delete-region (car delete-temp-newline
)
579 (cdr delete-temp-newline
))
580 (goto-char (car delete-temp-newline
))
581 (set-marker (car delete-temp-newline
) nil
)
582 (set-marker (cdr delete-temp-newline
) nil
)
583 ;; if there is whitespace before point, then preserve
584 ;; at least one space.
588 (if (not (memq 'before newlines
))
589 ;; since we're hanging the brace, we need to recalculate
591 (c-save-buffer-state ((c-syntactic-indentation-in-macros t
)
592 (c-auto-newline-analysis t
))
593 ;; Turn on syntactic macro analysis to help with auto
595 (setq syntax
(c-guess-basic-syntax))))
596 (when c-syntactic-indentation
597 ;; Now adjust the line's indentation. Don't update the state
598 ;; cache since c-guess-basic-syntax isn't called when
599 ;; c-syntactic-context is set.
600 (let* ((c-syntactic-context syntax
))
601 (indent-according-to-mode)))
602 ;; Do all appropriate clean ups
604 (pos (- (point-max) (point)))
606 ;; clean up empty defun braces
607 (if (and c-auto-newline
608 (memq 'empty-defun-braces c-cleanup-list
)
609 (eq last-command-char ?\
})
610 (c-intersect-lists '(defun-close class-close inline-close
)
615 (eq (char-before) ?\
{))
616 ;; make sure matching open brace isn't in a comment
617 (not (c-in-literal)))
618 (delete-region (point) (1- here
)))
619 ;; clean up brace-else-brace and brace-elseif-brace
620 (when (and c-auto-newline
621 (eq last-command-char ?\
{))
623 ((and (memq 'brace-else-brace c-cleanup-list
)
626 "\\([ \t\n]\\|\\\\\n\\)*"
628 "\\([ \t\n]\\|\\\\\n\\)*"
632 (setq mbeg
(match-beginning 0)
634 (eq (match-end 0) here
)))
635 (delete-region mbeg mend
)
636 (insert-and-inherit "} else {"))
637 ((and (memq 'brace-elseif-brace c-cleanup-list
)
639 (goto-char (1- here
))
643 (eq (char-before) ?\
)))
644 (zerop (c-save-buffer-state nil
(c-backward-token-2 1 t
)))
645 (eq (char-after) ?\
()
650 "\\([ \t\n]\\|\\\\\n\\)*"
652 "\\([ \t\n]\\|\\\\\n\\)+"
654 "\\([ \t\n]\\|\\\\\n\\)*")
656 (eq (match-end 0) tmp
))
657 (delete-region mbeg mend
)
660 (goto-char (- (point-max) pos
))
662 ;; does a newline go after the brace?
663 (if (memq 'after newlines
)
664 (c-newline-and-indent))
667 (and (eq last-command-char ?\
})
668 (not executing-kbd-macro
)
671 (c-save-buffer-state nil
672 (c-backward-syntactic-ws safepos
))
673 (funcall old-blink-paren
)))))
675 (defun c-electric-slash (arg)
676 "Insert a slash character.
678 Indent the line as a comment, if:
680 1. The slash is second of a `//' line oriented comment introducing
681 token and we are on a comment-only-line, or
683 2. The slash is part of a `*/' token that closes a block oriented
686 If a numeric ARG is supplied, point is inside a literal, or
687 `c-syntactic-indentation' is nil, indentation is inhibited."
689 (let* ((ch (char-before))
690 (literal (c-in-literal))
691 (indentp (and c-syntactic-indentation
693 (eq last-command-char ?
/)
700 (c-echo-syntactic-information-p nil
))
701 (self-insert-command (prefix-numeric-value arg
))
703 (indent-according-to-mode))))
705 (defun c-electric-star (arg)
706 "Insert a star character.
707 If the star is the second character of a C style comment introducing
708 construct, and we are on a comment-only-line, indent line as comment.
709 If a numeric ARG is supplied, point is inside a literal, or
710 `c-syntactic-indentation' is nil, indentation is inhibited."
712 (self-insert-command (prefix-numeric-value arg
))
713 ;; if we are in a literal, or if arg is given do not re-indent the
714 ;; current line, unless this star introduces a comment-only line.
715 (if (and c-syntactic-indentation
717 (eq (c-in-literal) 'c
)
718 (eq (char-before) ?
*)
721 (skip-chars-backward "*")
722 (if (eq (char-before) ?
/)
724 (skip-chars-backward " \t")
726 (let (c-echo-syntactic-information-p) ; shut this up
727 (indent-according-to-mode))
730 (defun c-electric-semi&comma
(arg)
731 "Insert a comma or semicolon.
732 When the auto-newline feature is turned on, as evidenced by the \"/a\"
733 or \"/ah\" string on the mode line, a newline might be inserted. See
734 the variable `c-hanging-semi&comma-criteria' for how newline insertion
737 When a semicolon is inserted, the line is re-indented unless a numeric
738 arg is supplied, point is inside a literal, or
739 `c-syntactic-indentation' is nil.
741 Based on the value of `c-cleanup-list', this function cleans up commas
742 following brace lists and semicolons following defuns."
744 (let* ((lim (c-most-enclosing-brace (c-parse-state)))
745 (literal (c-in-literal lim
))
748 (c-echo-syntactic-information-p nil
))
750 (self-insert-command (prefix-numeric-value arg
))
751 ;; do some special stuff with the character
752 (self-insert-command (prefix-numeric-value arg
))
753 ;; do all cleanups and newline insertions if c-auto-newline is
755 (if (or (not c-auto-newline
)
756 (not (looking-at "[ \t]*\\\\?$")))
757 (if c-syntactic-indentation
758 (indent-according-to-mode))
760 (let ((pos (- (point-max) (point))))
762 (eq last-command-char ?
,)
763 (memq 'list-close-comma c-cleanup-list
))
765 (eq last-command-char ?\
;)
766 (memq 'defun-close-semi c-cleanup-list
)))
770 (eq (char-before) ?
}))
771 ;; make sure matching open brace isn't in a comment
772 (not (c-in-literal lim
)))
773 (delete-region (point) here
))
774 (goto-char (- (point-max) pos
)))
776 (if c-syntactic-indentation
777 (indent-according-to-mode))
778 ;; check to see if a newline should be added
779 (let ((criteria c-hanging-semi
&comma-criteria
)
780 answer add-newline-p
)
782 (setq answer
(funcall (car criteria
)))
783 ;; only nil value means continue checking
785 (setq criteria
(cdr criteria
))
787 ;; only 'stop specifically says do not add a newline
788 (setq add-newline-p
(not (eq answer
'stop
)))
791 (c-newline-and-indent))
794 (defun c-electric-colon (arg)
797 If the auto-newline feature is turned on, as evidenced by the \"/a\"
798 or \"/ah\" string on the mode line, newlines are inserted before and
799 after colons based on the value of `c-hanging-colons-alist'.
801 Also, the line is re-indented unless a numeric ARG is supplied, the
802 colon is inserted inside a literal, or `c-syntactic-indentation' is
805 This function cleans up double colon scope operators based on the
806 value of `c-cleanup-list'."
808 (let* ((bod (c-point 'bod
))
809 (literal (c-in-literal bod
))
812 (c-echo-syntactic-information-p nil
))
815 (self-insert-command (prefix-numeric-value arg
)))
816 ((not (looking-at "[ \t]*\\\\?$"))
817 (self-insert-command (prefix-numeric-value arg
))
818 (if c-syntactic-indentation
819 (indent-according-to-mode)))
821 ;; insert the colon, then do any specified cleanups
822 (self-insert-command (prefix-numeric-value arg
))
823 (let ((pos (- (point-max) (point)))
825 (if (and c-auto-newline
826 (memq 'scope-operator c-cleanup-list
)
827 (eq (char-before) ?
:)
831 (eq (char-before) ?
:))
833 (not (eq (char-after (- (point) 2)) ?
:)))
835 (delete-region (point) (1- here
))
836 (setq is-scope-op t
)))
837 (goto-char (- (point-max) pos
)))
838 ;; indent the current line if it's done syntactically.
839 (if c-syntactic-indentation
840 ;; Cannot use the same syntax analysis as we find below,
841 ;; since that's made with c-syntactic-indentation-in-macros
843 (indent-according-to-mode))
845 ((c-syntactic-indentation-in-macros t
)
846 (c-auto-newline-analysis t
)
847 ;; Turn on syntactic macro analysis to help with auto newlines
849 (syntax (c-guess-basic-syntax))
851 ;; Translate substatement-label to label for this operation.
853 (if (eq (car (car elem
)) 'substatement-label
)
854 (setcar (car elem
) 'label
))
855 (setq elem
(cdr elem
)))
856 ;; some language elements can only be determined by checking
857 ;; the following line. Lets first look for ones that can be
858 ;; found when looking on the line with the colon
861 (or (c-lookup-lists '(case-label label access-label
)
862 syntax c-hanging-colons-alist
)
863 (c-lookup-lists '(member-init-intro inher-intro
)
867 (c-guess-basic-syntax)
869 c-hanging-colons-alist
)))))
870 ;; does a newline go before the colon? Watch out for already
871 ;; non-hung colons. However, we don't unhang them because that
872 ;; would be a cleanup (and anti-social).
873 (if (and (memq 'before newlines
)
876 (skip-chars-backward ": \t")
878 (let ((pos (- (point-max) (point))))
880 (c-newline-and-indent)
881 (goto-char (- (point-max) pos
))))
882 ;; does a newline go after the colon?
883 (if (and (memq 'after
(cdr-safe newlines
))
885 (c-newline-and-indent))
888 (defun c-electric-lt-gt (arg)
889 "Insert a less-than, or greater-than character.
890 The line will be re-indented if the character inserted is the second
891 of a C++ style stream operator and the buffer is in C++ mode.
892 Exceptions are when a numeric argument is supplied, point is inside a
893 literal, or `c-syntactic-indentation' is nil, in which case the line
894 will not be re-indented."
896 (let ((indentp (and c-syntactic-indentation
898 (eq (char-before) last-command-char
)
899 (not (c-in-literal))))
901 (c-echo-syntactic-information-p nil
))
902 (self-insert-command (prefix-numeric-value arg
))
904 (indent-according-to-mode))))
906 (defun c-electric-paren (arg)
907 "Insert a parenthesis.
909 Some newline cleanups are done if appropriate; see the variable
912 Also, the line is re-indented unless a numeric ARG is supplied, the
913 parenthesis is inserted inside a literal, or `c-syntactic-indentation'
916 (let ((literal (c-in-literal (c-point 'bod
)))
918 (c-echo-syntactic-information-p nil
))
920 (self-insert-command (prefix-numeric-value arg
))
921 ;; do some special stuff with the character
922 (let* (;; We want to inhibit blinking the paren since this will
923 ;; be most disruptive. We'll blink it ourselves
925 (old-blink-paren blink-paren-function
)
926 blink-paren-function
)
927 (self-insert-command (prefix-numeric-value arg
))
928 (if c-syntactic-indentation
929 (indent-according-to-mode))
930 (when (looking-at "[ \t]*\\\\?$")
932 ;; Do all appropriate clean ups
934 (pos (- (point-max) (point)))
936 ;; clean up brace-elseif-brace
937 (if (and (memq 'brace-elseif-brace c-cleanup-list
)
938 (eq last-command-char ?\
()
941 "\\([ \t\n]\\|\\\\\n\\)*"
943 "\\([ \t\n]\\|\\\\\n\\)+"
945 "\\([ \t\n]\\|\\\\\n\\)*"
949 (setq mbeg
(match-beginning 0)
952 (not (c-in-literal)))
954 (delete-region mbeg mend
)
955 (insert-and-inherit "} else if ("))
956 ;; clean up brace-catch-brace
958 (if (and (memq 'brace-catch-brace c-cleanup-list
)
959 (eq last-command-char ?\
()
962 "\\([ \t\n]\\|\\\\\n\\)*"
964 "\\([ \t\n]\\|\\\\\n\\)*"
968 (setq mbeg
(match-beginning 0)
971 (not (c-in-literal)))
973 (delete-region mbeg mend
)
974 (insert-and-inherit "} catch ("))))
975 (goto-char (- (point-max) pos
))
977 (let (beg (end (1- (point))))
978 (cond ((and (memq 'space-before-funcall c-cleanup-list
)
979 (eq last-command-char ?\
()
982 (skip-chars-backward " \t")
986 (delete-region beg end
)
989 ((and (memq 'compact-empty-funcall c-cleanup-list
)
990 (eq last-command-char ?\
))
992 (c-safe (backward-char 2))
993 (when (looking-at "()")
995 (skip-chars-backward " \t")
998 (delete-region beg end
))))
999 (and (not executing-kbd-macro
)
1001 (funcall old-blink-paren
))))))
1003 (defun c-electric-continued-statement ()
1004 "Reindent the current line if appropriate.
1006 This function is used to reindent the line after a keyword which
1007 continues an earlier statement is typed, e.g. an \"else\" or the
1008 \"while\" in a do-while block.
1010 The line is reindented if there is nothing but whitespace before the
1011 keyword on the line, the keyword is not inserted inside a literal, and
1012 `c-syntactic-indentation' is non-nil."
1013 (let (;; shut this up
1014 (c-echo-syntactic-information-p nil
))
1015 (when (and c-syntactic-indentation
1016 (not (eq last-command-char ?_
))
1018 (skip-syntax-backward "w")
1021 (not (c-in-literal (c-point 'bod
))))
1022 ;; Have to temporarily insert a space so that
1023 ;; c-guess-basic-syntax recognizes the keyword. Follow the
1024 ;; space with a nonspace to avoid messing up any whitespace
1025 ;; sensitive meddling that might be done, e.g. by
1026 ;; `c-backslash-region'.
1027 (insert-and-inherit " x")
1029 (indent-according-to-mode)
1030 (delete-char -
2)))))
1033 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus
1034 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com
1035 (defun c-forward-into-nomenclature (&optional arg
)
1036 "Move forward to end of a nomenclature section or word.
1037 With arg, do it arg times."
1039 (let ((case-fold-search nil
))
1042 (cc-eval-when-compile
1043 (concat "\\W*\\([" c-upper
"]*[" c-lower c-digit
"]*\\)"))
1045 (while (and (< arg
0)
1047 (cc-eval-when-compile
1049 "\\(\\(\\W\\|[" c-lower c-digit
"]\\)[" c-upper
"]+"
1053 (setq arg
(1+ arg
)))))
1054 (c-keep-region-active))
1056 (defun c-backward-into-nomenclature (&optional arg
)
1057 "Move backward to beginning of a nomenclature section or word.
1058 With optional ARG, move that many times. If ARG is negative, move
1061 (c-forward-into-nomenclature (- arg
))
1062 (c-keep-region-active))
1064 (defun c-scope-operator ()
1065 "Insert a double colon scope operator at point.
1066 No indentation or other \"electric\" behavior is performed."
1068 (insert-and-inherit "::"))
1070 (defun c-beginning-of-defun (&optional arg
)
1071 "Move backward to the beginning of a defun.
1072 Every top level declaration that contains a brace paren block is
1073 considered to be a defun.
1075 With a positive argument, move backward that many defuns. A negative
1076 argument -N means move forward to the Nth following beginning. Return
1077 t unless search stops due to beginning or end of buffer.
1079 Unlike the built-in `beginning-of-defun' this tries to be smarter
1080 about finding the char with open-parenthesis syntax that starts the
1084 (or arg
(setq arg
1))
1087 (when (c-end-of-defun (- arg
))
1088 (c-save-buffer-state nil
(c-forward-syntactic-ws))
1091 (c-save-buffer-state (paren-state lim pos
)
1094 ;; Note: Partial code duplication in `c-end-of-defun' and
1095 ;; `c-declaration-limits'.
1097 (setq paren-state
(c-parse-state))
1099 (goto-char (c-least-enclosing-brace paren-state
))
1100 ;; If we moved to the outermost enclosing paren
1101 ;; then we can use c-safe-position to set the
1102 ;; limit. Can't do that otherwise since the
1103 ;; earlier paren pair on paren-state might very
1104 ;; well be part of the declaration we should go
1106 (setq lim
(c-safe-position (point) paren-state
))
1108 ;; At top level. Make sure we aren't inside a literal.
1109 (setq pos
(c-literal-limits
1110 (c-safe-position (point) paren-state
)))
1111 (if pos
(goto-char (car pos
))))
1113 (while (let ((start (point)))
1114 (c-beginning-of-decl-1 lim
)
1115 (if (= (point) start
)
1116 ;; Didn't move. Might be due to bob or unbalanced
1117 ;; parens. Try to continue if it's the latter.
1118 (unless (c-safe (goto-char
1119 (c-down-list-backward (point))))
1120 ;; Didn't work, so it's bob then.
1121 (goto-char (point-min))
1125 ;; Check if the declaration contains a brace
1126 ;; block. If not, we try another one.
1128 (not (and (c-syntactic-re-search-forward "[;{]" nil t t
)
1129 (or (eq (char-before) ?
{)
1130 (and c-recognize-knr-p
1131 ;; Might have stopped on the
1132 ;; ';' in a K&R argdecl. In
1133 ;; that case the declaration
1134 ;; should contain a block.
1135 (c-in-knr-argdecl pos
)))))))
1138 ;; Check if `c-beginning-of-decl-1' put us after the block
1139 ;; in a declaration that doesn't end there. We're searching
1140 ;; back and forth over the block here, which can be
1143 (if (and c-opt-block-decls-with-vars-key
1145 (c-backward-syntactic-ws)
1146 (eq (char-before) ?
}))
1147 (eq (car (c-beginning-of-decl-1))
1156 ;; Try to be line oriented; position point at the closest
1157 ;; preceding boi that isn't inside a comment, but if we hit
1158 ;; the previous declaration then we use the current point
1160 (while (and (/= (point) (c-point 'boi
))
1161 (c-backward-single-comment)))
1162 (if (/= (point) (c-point 'boi
))
1165 (setq arg
(1- arg
)))))
1166 (c-keep-region-active)
1169 (defun c-end-of-defun (&optional arg
)
1170 "Move forward to the end of a top level declaration.
1171 With argument, do it that many times. Negative argument -N means move
1172 back to Nth preceding end. Returns t unless search stops due to
1173 beginning or end of buffer.
1175 An end of a defun occurs right after the close-parenthesis that matches
1176 the open-parenthesis that starts a defun; see `beginning-of-defun'."
1179 (or arg
(setq arg
1))
1182 (when (c-beginning-of-defun (- arg
))
1183 (c-save-buffer-state nil
(c-backward-syntactic-ws))
1186 (c-save-buffer-state (paren-state lim pos
)
1189 ;; Note: Partial code duplication in `c-beginning-of-defun'
1190 ;; and `c-declaration-limits'.
1192 (setq paren-state
(c-parse-state))
1194 (goto-char (c-least-enclosing-brace paren-state
))
1195 ;; If we moved to the outermost enclosing paren
1196 ;; then we can use c-safe-position to set the
1197 ;; limit. Can't do that otherwise since the
1198 ;; earlier paren pair on paren-state might very
1199 ;; well be part of the declaration we should go
1201 (setq lim
(c-safe-position (point) paren-state
))
1203 ;; At top level. Make sure we aren't inside a literal.
1204 (setq pos
(car-safe (c-literal-limits
1205 (c-safe-position (point) paren-state
))))
1206 (if pos
(goto-char pos
)))
1208 ;; Have to move to the start first so that `c-end-of-decl-1'
1209 ;; has the correct start position.
1211 (when (memq (car (c-beginning-of-decl-1 lim
))
1213 ;; We moved back over the previous defun or a macro. Move
1214 ;; to the next token; it's the start of the next
1215 ;; declaration. We can also be directly after the block
1216 ;; in a `c-opt-block-decls-with-vars-key' declaration, but
1217 ;; then we won't move significantly far here.
1219 (c-forward-token-2 0))
1221 (while (let ((start (point)))
1223 (if (= (point) start
)
1224 ;; Didn't move. Might be due to eob or unbalanced
1225 ;; parens. Try to continue if it's the latter.
1226 (if (c-safe (goto-char (c-up-list-forward (point))))
1228 ;; Didn't work, so it's eob then.
1229 (goto-char (point-max))
1233 ;; Check if the declaration contains a brace
1234 ;; block. If not, we try another one.
1237 (not (c-syntactic-re-search-forward "{" pos t t
))))))
1240 ;; Try to be line oriented; position point after the next
1241 ;; newline that isn't inside a comment, but if we hit the
1242 ;; next declaration then we use the current point instead.
1243 (while (and (not (bolp))
1244 (not (looking-at "\\s *$"))
1245 (c-forward-single-comment)))
1247 ((looking-at "\\s *$")
1252 (setq arg
(1- arg
)))))
1253 (c-keep-region-active)
1256 (defun c-declaration-limits (near)
1257 ;; Return a cons of the beginning and end positions of the current
1258 ;; top level declaration or macro. If point is not inside any then
1259 ;; nil is returned, unless NEAR is non-nil in which case the closest
1260 ;; following one is chosen instead (if there is any). The end
1261 ;; position is at the next line, providing there is one before the
1265 ;; Note: Some code duplication in `c-beginning-of-defun' and
1266 ;; `c-end-of-defun'.
1268 (let ((start (point))
1269 (paren-state (c-parse-state))
1272 (goto-char (c-least-enclosing-brace paren-state
))
1273 ;; If we moved to the outermost enclosing paren then we
1274 ;; can use c-safe-position to set the limit. Can't do
1275 ;; that otherwise since the earlier paren pair on
1276 ;; paren-state might very well be part of the
1277 ;; declaration we should go to.
1278 (setq lim
(c-safe-position (point) paren-state
))
1280 ;; At top level. Make sure we aren't inside a literal.
1281 (setq pos
(c-literal-limits
1282 (c-safe-position (point) paren-state
)))
1283 (if pos
(goto-char (car pos
))))
1285 (when (c-beginning-of-macro)
1294 (when (or (eq (car (c-beginning-of-decl-1 lim
)) 'previous
)
1296 ;; We moved back over the previous defun. Skip to the next
1297 ;; one. Not using c-forward-syntactic-ws here since we
1298 ;; should not skip a macro. We can also be directly after
1299 ;; the block in a `c-opt-block-decls-with-vars-key'
1300 ;; declaration, but then we won't move significantly far
1303 (c-forward-comments)
1305 (when (and near
(c-beginning-of-macro))
1313 (if (eobp) (throw 'exit nil
))
1315 ;; Check if `c-beginning-of-decl-1' put us after the block in a
1316 ;; declaration that doesn't end there. We're searching back and
1317 ;; forth over the block here, which can be expensive.
1319 (if (and c-opt-block-decls-with-vars-key
1321 (c-backward-syntactic-ws)
1322 (eq (char-before) ?
}))
1323 (eq (car (c-beginning-of-decl-1))
1327 (and (> (point) pos
)
1328 (setq end-pos
(point)))))
1332 (if (and (not near
) (> (point) start
))
1335 ;; Try to be line oriented; position the limits at the
1336 ;; closest preceding boi, and after the next newline, that
1337 ;; isn't inside a comment, but if we hit a neighboring
1338 ;; declaration then we instead use the exact declaration
1339 ;; limit in that direction.
1342 (while (and (/= (point) (c-point 'boi
))
1343 (c-backward-single-comment)))
1344 (if (/= (point) (c-point 'boi
))
1352 (while (and (not (bolp))
1353 (not (looking-at "\\s *$"))
1354 (c-forward-single-comment)))
1357 ((looking-at "\\s *$")
1364 (defun c-mark-function ()
1365 "Put mark at end of the current top-level declaration or macro, point at beginning.
1366 If point is not inside any then the closest following one is chosen.
1368 As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
1369 function does not require the declaration to contain a brace block."
1373 (c-save-buffer-state nil
1374 ;; We try to be line oriented, unless there are several
1375 ;; declarations on the same line.
1376 (if (looking-at c-syntactic-eol
)
1377 (c-backward-token-2 1 nil
(c-point 'bol
)))
1378 (setq decl-limits
(c-declaration-limits t
)))
1380 (if (not decl-limits
)
1381 (error "Cannot find any declaration")
1382 (goto-char (car decl-limits
))
1383 (push-mark (cdr decl-limits
) nil t
))))
1386 (defun c-beginning-of-statement (&optional count lim sentence-flag
)
1387 "Go to the beginning of the innermost C statement.
1388 With prefix arg, go back N - 1 statements. If already at the
1389 beginning of a statement then go to the beginning of the closest
1390 preceding one, moving into nested blocks if necessary (use
1391 \\[backward-sexp] to skip over a block). If within or next to a
1392 comment or multiline string, move by sentences instead of statements.
1394 When called from a program, this function takes 3 optional args: the
1395 repetition count, a buffer position limit which is the farthest back
1396 to search for the syntactic context, and a flag saying whether to do
1397 sentence motion in or near comments and multiline strings.
1399 Note that `c-beginning-of-statement-1' is usually better to use from
1400 programs. It has much more well defined semantics than this one,
1401 which is intended for interactive use and might therefore change to be
1403 (interactive (list (prefix-numeric-value current-prefix-arg
)
1405 (c-save-buffer-state
1406 ((count (or count
1))
1408 (range (c-collect-line-comments (c-literal-limits lim
))))
1409 (while (and (/= count
0)
1410 (or (not lim
) (> (point) lim
)))
1412 (if (and (not range
) sentence-flag
)
1414 ;; Find the comment next to point if we're not in one.
1416 (if (c-backward-single-comment)
1417 (setq range
(cons (point)
1418 (progn (c-forward-single-comment)
1420 (c-skip-ws-backward)
1421 (setq range
(point))
1423 (if (eq (char-before) ?
\")
1424 (c-safe (c-backward-sexp 1)
1425 (cons (point) range
)))))
1427 (if (eq (char-after) ?
\")
1428 (setq range
(cons (point)
1432 (setq range
(point))
1433 (setq range
(if (c-forward-single-comment)
1434 (cons range
(point))
1436 (setq range
(c-collect-line-comments range
))))
1437 (if (and (< count
0) (= here
(point-max)))
1438 ;; Special case because eob might be in a literal.
1441 (if (and sentence-flag
1442 (or (/= (char-syntax (char-after (car range
))) ?
\")
1443 ;; Only visit a string if it spans more than one line.
1445 (goto-char (car range
))
1446 (skip-chars-forward "^\n" (cdr range
))
1447 (< (point) (cdr range
)))))
1448 (let* ((lit-type (c-literal-type range
))
1449 (line-prefix (concat "[ \t]*\\("
1450 c-current-comment-prefix
1452 (beg (if (eq lit-type
'string
)
1455 (goto-char (car range
))
1457 (looking-at comment-start-skip
)
1460 (looking-at line-prefix
)
1462 (end (- (cdr range
) (if (eq lit-type
'c
) 2 1)))
1463 (beg-of-para (if (eq lit-type
'string
)
1467 (if (looking-at line-prefix
)
1468 (goto-char (match-end 0)))))))
1470 ;; Move by sentence, but not past the limit of the
1471 ;; literal, narrowed to the appropriate
1473 (narrow-to-region (save-excursion
1474 (let ((pos (min here end
)))
1476 (forward-paragraph -
1)
1477 (if (looking-at paragraph-separate
)
1479 (when (> (point) beg
)
1480 (funcall beg-of-para
)
1481 (when (>= (point) pos
)
1482 (forward-paragraph -
2)
1483 (funcall beg-of-para
)))
1486 (c-safe (forward-sentence (if (< count
0) 1 -
1)))
1487 (if (and (memq lit-type
'(c c
++))
1488 ;; Check if we stopped due to a comment
1489 ;; prefix and not a sentence end.
1490 (/= (point) (point-min))
1491 (/= (point) (point-max))
1494 (looking-at line-prefix
))
1495 (>= (point) (match-beginning 0))
1496 (/= (match-beginning 1) (match-end 1))
1497 (or (< (point) (match-end 0))
1499 (= (point) (match-end 0))
1500 ;; The comment prefix may contain
1501 ;; characters that is regarded as end
1506 (forward-paragraph -
1)
1507 (< (point) (match-beginning 0)))
1510 (or (not (re-search-backward
1515 (c-point 'eol
)))))))))
1516 (setq count
(+ count
(if (< count
0) -
1 1)))
1519 ;; In block comments, if there's only
1520 ;; horizontal ws between the text and the
1521 ;; comment ender, stop before it. Stop after
1522 ;; the ender if there's either nothing or
1523 ;; newlines between.
1524 (when (and (eq lit-type
'c
)
1525 (eq (point) (point-max)))
1527 (when (or (= (skip-chars-backward " \t") 0)
1528 (eq (point) (point-max))
1530 (goto-char (cdr range
)))))
1531 (when (and (eq (point) (point-min))
1532 (looking-at "[ \t]*\\\\?$"))
1533 ;; Stop before instead of after the comment
1534 ;; starter if nothing follows it.
1536 (goto-char (car range
))
1537 (if (and (eq lit-type
'string
) (/= (point) here
))
1538 (setq count
(1+ count
)
1540 ;; See if we should escape the literal.
1542 (if (< (point) here
)
1543 (setq count
(1- count
))
1544 (goto-char (car range
))
1546 (if (> (point) here
)
1547 (setq count
(1+ count
))
1548 (goto-char (cdr range
))
1550 (goto-char (if (> count
0) (car range
) (cdr range
)))
1555 ;; Stop before `{' and after `;', `{', `}' and `};'
1556 ;; when not followed by `}' or `)', but on the other
1557 ;; side of the syntactic ws. Move by sexps and move
1558 ;; into parens. Also stop before `#' when it's at boi
1560 (let ((literal-pos (not sentence-flag
))
1561 last last-below-line
)
1565 (when (and (or (eq (char-after) ?\
{)
1566 (and (eq (char-after) ?
#)
1567 (eq (point) (c-point 'boi
)))
1570 (unless (and c-special-brace-lists
1571 (eq (char-after) ?
{)
1572 (c-looking-at-special-brace-list))
1573 (if (and (eq (char-after) ?
#)
1574 (numberp last-below-line
)
1575 (not (eq last-below-line here
)))
1576 (goto-char last-below-line
))
1578 ;; Don't know why I added the following, but it
1579 ;; doesn't work when point is preceded by a line
1580 ;; style comment. /mast
1581 ;;(c-skip-ws-backward)
1583 (c-backward-comments)
1584 (when (c-backward-single-comment)
1585 ;; Record position of first comment.
1587 (c-forward-single-comment)
1588 (setq literal-pos
(point)))
1589 (c-backward-comments)))
1590 (unless last-below-line
1592 (re-search-forward "\\(^\\|[^\\]\\)$" last t
))
1593 (setq last-below-line last
)))
1594 (cond ((bobp) ; Must handle bob specially.
1599 ((progn (backward-char)
1600 (looking-at "[;{}]"))
1601 (if (and c-special-brace-lists
1602 (eq (char-after) ?
{)
1603 (c-looking-at-special-brace-list))
1604 (skip-syntax-backward "w_") ; Speedup only.
1605 (if (or (= here last
)
1606 (memq (char-after last
) '(?\
) ?
})))
1607 (if (and (eq (char-before) ?
})
1608 (eq (char-after) ?\
;))
1612 ((= (char-syntax (char-after)) ?
\")
1613 (let ((end (point)))
1617 (skip-chars-forward "^\n" end
)
1618 (when (< (point) end
)
1619 ;; Break at multiline string.
1620 (setq literal-pos
(1+ end
))
1622 (t (skip-syntax-backward "w_")) ; Speedup only.
1624 (if (and (numberp literal-pos
)
1625 (< (point) literal-pos
))
1626 ;; We jumped over a comment or string that
1627 ;; should be investigated.
1628 (goto-char literal-pos
)
1629 (setq count
(1- count
))))
1631 (goto-char (point-min))
1634 ;; Stop before `{', `}', and `#' when it's at boi on a
1635 ;; line, but on the other side of the syntactic ws, and
1636 ;; after `;', `}' and `};'. Only stop before `{' if at
1637 ;; top level or inside braces, though. Move by sexps
1638 ;; and move into parens. Also stop at eol of lines
1639 ;; with `#' at the boi.
1640 (let ((literal-pos (not sentence-flag
))
1646 (c-forward-comments)
1649 ;; Record position of first comment.
1650 (setq literal-pos
(point))
1651 (c-forward-single-comment))
1652 (c-forward-comments)
1653 (setq literal-pos nil
)))
1654 (cond ((and (eq (char-after) ?
{)
1655 (not (and c-special-brace-lists
1656 (c-looking-at-special-brace-list)))
1659 (or (not (c-safe (up-list -
1) t
))
1660 (= (char-after) ?
{))))
1663 ((and c-special-brace-lists
1664 (eq (char-after) ?
})
1666 (and (c-safe (up-list -
1) t
)
1667 (c-looking-at-special-brace-list))))
1669 (skip-syntax-forward "w_")) ; Speedup only.
1670 ((and (eq (char-after) ?
})
1674 ; ((and (eq (char-after) ?#)
1675 ; (= (point) (c-point 'boi)))
1677 ; (or (re-search-forward "\\(^\\|[^\\]\\)$" nil t)
1678 ; (goto-char (point-max)))
1681 ((looking-at ";\\|};?")
1682 (goto-char (match-end 0))
1684 ((= (char-syntax (char-after)) ?
\")
1685 (let ((beg (point)))
1688 (skip-chars-backward "^\n" beg
)
1689 (when (> (point) beg
)
1690 ;; Break at multiline string.
1691 (setq literal-pos beg
)
1695 (skip-syntax-forward "w_")) ; Speedup only.
1697 (if (and (numberp literal-pos
)
1698 (> (point) literal-pos
))
1699 ;; We jumped over a comment that should be investigated.
1700 (goto-char literal-pos
)
1701 (setq count
(1+ count
))))
1703 (goto-char (point-max))
1706 ;; If we haven't moved we're near a buffer limit.
1707 (when (and (not (zerop count
)) (= (point) here
))
1708 (goto-char (if (> count
0) (point-min) (point-max)))
1710 (c-keep-region-active))
1712 (defun c-end-of-statement (&optional count lim sentence-flag
)
1713 "Go to the end of the innermost C statement.
1714 With prefix arg, go forward N - 1 statements. Move forward to the end
1715 of the next statement if already at end, and move into nested blocks
1716 \(use \\[forward-sexp] to skip over a block). If within or next to a
1717 comment or multiline string, move by sentences instead of statements.
1719 When called from a program, this function takes 3 optional args: the
1720 repetition count, a buffer position limit which is the farthest back
1721 to search for the syntactic context, and a flag saying whether to do
1722 sentence motion in or near comments and multiline strings."
1723 (interactive (list (prefix-numeric-value current-prefix-arg
)
1725 (c-beginning-of-statement (- (or count
1)) lim sentence-flag
)
1726 (c-keep-region-active))
1729 ;; set up electric character functions to work with pending-del,
1730 ;; (a.k.a. delsel) mode. All symbols get the t value except
1731 ;; the functions which delete, which gets 'supersede.
1735 (put sym
'delete-selection t
) ; for delsel (Emacs)
1736 (put sym
'pending-delete t
))) ; for pending-del (XEmacs)
1741 c-electric-semi
&comma
1745 (put 'c-electric-delete
'delete-selection
'supersede
) ; delsel
1746 (put 'c-electric-delete
'pending-delete
'supersede
) ; pending-del
1747 (put 'c-electric-backspace
'delete-selection
'supersede
) ; delsel
1748 (put 'c-electric-backspace
'pending-delete
'supersede
) ; pending-del
1749 (put 'c-electric-delete-forward
'delete-selection
'supersede
) ; delsel
1750 (put 'c-electric-delete-forward
'pending-delete
'supersede
) ; pending-del
1753 (defun c-calc-comment-indent (entry)
1755 (setq entry
(or (assq entry c-indent-comment-alist
)
1756 (assq 'other c-indent-comment-alist
)
1757 '(default .
(column . nil
)))))
1758 (let ((action (car (cdr entry
)))
1759 (value (cdr (cdr entry
)))
1760 (col (current-column)))
1761 (cond ((eq action
'space
)
1763 ((eq action
'column
)
1764 (unless value
(setq value comment-column
))
1766 ;; Do not pad with one space if we're at bol.
1768 (max (1+ col
) value
)))
1774 (let ((lim (c-literal-limits (c-point 'bol
) t
)))
1776 (goto-char (car lim
))
1777 (when (looking-at "/[/*]")
1778 ;; Found comment to align with.
1780 ;; Do not pad with one space if we're at bol.
1782 (max (1+ col
) (current-column))))))))
1783 ;; Recurse to handle value as a new spec.
1784 (c-calc-comment-indent (cdr entry
)))))))
1786 (defun c-comment-indent ()
1787 "Used by `indent-for-comment' to create and indent comments.
1788 See `c-indent-comment-alist' for a description."
1791 (c-save-buffer-state
1792 ((eot (let ((lim (c-literal-limits (c-point 'bol
) t
)))
1793 (or (when (consp lim
)
1794 (goto-char (car lim
))
1795 (when (looking-at "/[/*]")
1796 (skip-chars-backward " \t")
1799 (skip-chars-backward " \t")
1802 (cond ((looking-at "^/[/*]")
1804 ((progn (beginning-of-line)
1807 ((progn (back-to-indentation)
1808 (and (eq (char-after) ?
})
1809 (eq (point) (1- eot
))))
1811 ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
1812 (eq (match-end 0) eot
))
1816 (if (and (memq line-type
'(anchored-comment empty-line
))
1817 c-indent-comments-syntactically-p
)
1818 (let ((c-syntactic-context (c-guess-basic-syntax)))
1819 ;; BOGOSITY ALERT: if we're looking at the eol, its
1820 ;; because indent-for-comment hasn't put the comment-start
1821 ;; in the buffer yet. this will screw up the syntactic
1822 ;; analysis so we kludge in the necessary info. Another
1823 ;; kludge is that if we're at the bol, then we really want
1824 ;; to ignore any anchoring as specified by
1825 ;; c-comment-only-line-offset since it doesn't apply here.
1827 (c-add-syntax 'comment-intro
))
1828 (let ((c-comment-only-line-offset
1829 (if (consp c-comment-only-line-offset
)
1830 c-comment-only-line-offset
1831 (cons c-comment-only-line-offset
1832 c-comment-only-line-offset
))))
1833 (c-get-syntactic-indentation c-syntactic-context
)))
1835 (c-calc-comment-indent line-type
)))))
1838 ;; used by outline-minor-mode
1839 (defun c-outline-level ()
1840 (let (buffer-invisibility-spec);; This so that `current-column' DTRT
1841 ;; in otherwise-hidden text.
1843 (skip-chars-forward "\t ")
1847 (defun c-up-conditional (count)
1848 "Move back to the containing preprocessor conditional, leaving mark behind.
1849 A prefix argument acts as a repeat count. With a negative argument,
1850 move forward to the end of the containing preprocessor conditional.
1852 `#elif' is treated like `#else' followed by `#if', so the function
1853 stops at them when going backward, but not when going forward."
1855 (c-forward-conditional (- count
) -
1)
1856 (c-keep-region-active))
1858 (defun c-up-conditional-with-else (count)
1859 "Move back to the containing preprocessor conditional, including `#else'.
1860 Just like `c-up-conditional', except it also stops at `#else'
1863 (c-forward-conditional (- count
) -
1 t
)
1864 (c-keep-region-active))
1866 (defun c-down-conditional (count)
1867 "Move forward into the next preprocessor conditional, leaving mark behind.
1868 A prefix argument acts as a repeat count. With a negative argument,
1869 move backward into the previous preprocessor conditional.
1871 `#elif' is treated like `#else' followed by `#if', so the function
1872 stops at them when going forward, but not when going backward."
1874 (c-forward-conditional count
1)
1875 (c-keep-region-active))
1877 (defun c-down-conditional-with-else (count)
1878 "Move forward into the next preprocessor conditional, including `#else'.
1879 Just like `c-down-conditional', except it also stops at `#else'
1882 (c-forward-conditional count
1 t
)
1883 (c-keep-region-active))
1885 (defun c-backward-conditional (count &optional target-depth with-else
)
1886 "Move back across a preprocessor conditional, leaving mark behind.
1887 A prefix argument acts as a repeat count. With a negative argument,
1888 move forward across a preprocessor conditional."
1890 (c-forward-conditional (- count
) target-depth with-else
)
1891 (c-keep-region-active))
1893 (defun c-forward-conditional (count &optional target-depth with-else
)
1894 "Move forward across a preprocessor conditional, leaving mark behind.
1895 A prefix argument acts as a repeat count. With a negative argument,
1896 move backward across a preprocessor conditional.
1898 `#elif' is treated like `#else' followed by `#if', except that the
1899 nesting level isn't changed when tracking subconditionals.
1901 The optional argument TARGET-DEPTH specifies the wanted nesting depth
1902 after each scan. I.e. if TARGET-DEPTH is -1, the function will move
1903 out of the enclosing conditional. A non-integer non-nil TARGET-DEPTH
1906 If the optional argument WITH-ELSE is non-nil, `#else' directives are
1907 treated as conditional clause limits. Normally they are ignored."
1909 (let* ((forward (> count
0))
1910 (increment (if forward -
1 1))
1911 (search-function (if forward
're-search-forward
're-search-backward
))
1913 (unless (integerp target-depth
)
1914 (setq target-depth
(if target-depth -
1 0)))
1918 ;; subdepth is the depth in "uninteresting" subtrees,
1919 ;; i.e. those that takes us farther from the target
1920 ;; depth instead of closer.
1924 ;; Find the "next" significant line in the proper direction.
1925 (while (and (not found
)
1926 ;; Rather than searching for a # sign that
1927 ;; comes at the beginning of a line aside from
1928 ;; whitespace, search first for a string
1929 ;; starting with # sign. Then verify what
1930 ;; precedes it. This is faster on account of
1931 ;; the fastmap feature of the regexp matcher.
1932 (funcall search-function
1933 "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
1936 ;; Now verify it is really a preproc line.
1937 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
1938 (let (dchange (directive (match-string 1)))
1939 (cond ((string= directive
"if")
1940 (setq dchange
(- increment
)))
1941 ((string= directive
"endif")
1942 (setq dchange increment
))
1944 ;; When we're not in an "uninteresting"
1945 ;; subtree, we might want to act on "elif"
1947 (if (cond (with-else
1948 ;; Always move toward the target depth.
1950 (if (> target-depth
0) 1 -
1)))
1951 ((string= directive
"elif")
1952 (setq dchange
(- increment
))))
1953 ;; Ignore the change if it'd take us
1954 ;; into an "uninteresting" subtree.
1955 (if (eq (> dchange
0) (<= target-depth
0))
1956 (setq dchange nil
)))))
1958 (when (or (/= subdepth
0)
1959 (eq (> dchange
0) (<= target-depth
0)))
1960 (setq subdepth
(+ subdepth dchange
)))
1961 (setq depth
(+ depth dchange
))
1962 ;; If we are trying to move across, and we find an
1963 ;; end before we find a beginning, get an error.
1964 (if (and (< depth target-depth
) (< dchange
0))
1966 "No following conditional at this level"
1967 "No previous conditional at this level"))))
1968 ;; When searching forward, start from next line so
1969 ;; that we don't find the same line again.
1970 (if forward
(forward-line 1))
1971 ;; We found something if we've arrived at the
1973 (if (and dchange
(= depth target-depth
))
1974 (setq found
(point))))
1976 (if forward
(forward-line 1)))))
1978 (error "No containing preprocessor conditional"))
1979 (goto-char (setq new found
)))
1980 (setq count
(+ count increment
))))
1983 (c-keep-region-active))
1986 ;; commands to indent lines, regions, defuns, and expressions
1987 (defun c-indent-command (&optional arg
)
1988 "Indent current line as C code, and/or insert some whitespace.
1990 If `c-tab-always-indent' is t, always just indent the current line.
1991 If nil, indent the current line only if point is at the left margin or
1992 in the line's indentation; otherwise insert some whitespace[*]. If
1993 other than nil or t, then some whitespace[*] is inserted only within
1994 literals (comments and strings) and inside preprocessor directives,
1995 but the line is always reindented.
1997 If `c-syntactic-indentation' is t, indentation is done according to
1998 the syntactic context. A numeric argument, regardless of its value,
1999 means indent rigidly all the lines of the expression starting after
2000 point so that this line becomes properly indented. The relative
2001 indentation among the lines of the expression is preserved.
2003 If `c-syntactic-indentation' is nil, the line is just indented one
2004 step according to `c-basic-offset'. In this mode, a numeric argument
2005 indents a number of such steps, positive or negative, and an empty
2006 prefix argument is equivalent to -1.
2008 [*] The amount and kind of whitespace inserted is controlled by the
2009 variable `c-insert-tab-function', which is called to do the actual
2010 insertion of whitespace. Normally the function in this variable
2011 just inserts a tab character, or the equivalent number of spaces,
2012 depending on the variable `indent-tabs-mode'."
2015 (let ((indent-function
2016 (if c-syntactic-indentation
2017 (symbol-function 'indent-according-to-mode
)
2019 (let ((c-macro-start c-macro-start
)
2020 (steps (cond ((not current-prefix-arg
) 1)
2021 ((equal current-prefix-arg
'(4)) -
1)
2023 (c-shift-line-indentation (* steps c-basic-offset
))
2024 (when (and c-auto-align-backslashes
2027 (eq (char-before) ?
\\))
2028 (c-query-and-set-macro-start))
2029 ;; Realign the line continuation backslash if inside a macro.
2030 (c-backslash-region (point) (point) nil t
)))
2032 (if (and c-syntactic-indentation current-prefix-arg
)
2033 ;; If c-syntactic-indentation and got arg, always indent this
2034 ;; line as C and shift remaining lines of expression the same
2036 (let ((shift-amt (save-excursion
2037 (back-to-indentation)
2041 (setq shift-amt
(- (save-excursion
2042 (back-to-indentation)
2046 (if (eq c-tab-always-indent t
)
2047 (beginning-of-line))
2055 (indent-code-rigidly beg end shift-amt
"#")))
2056 ;; Else use c-tab-always-indent to determine behavior.
2058 ;; CASE 1: indent when at column zero or in lines indentation,
2059 ;; otherwise insert a tab
2060 ((not c-tab-always-indent
)
2062 (skip-chars-backward " \t")
2064 (funcall c-insert-tab-function
)
2065 (funcall indent-function
)))
2066 ;; CASE 2: just indent the line
2067 ((eq c-tab-always-indent t
)
2068 (funcall indent-function
))
2069 ;; CASE 3: if in a literal, insert a tab, but always indent the
2073 (funcall c-insert-tab-function
))
2074 (funcall indent-function
)
2077 (defun c-indent-exp (&optional shutup-p
)
2078 "Indent each line in the balanced expression following point syntactically.
2079 If optional SHUTUP-P is non-nil, no errors are signalled if no
2080 balanced expression is found."
2082 (let ((here (point-marker))
2084 (set-marker-insertion-type here t
)
2086 (let ((start (save-restriction
2087 ;; Find the closest following open paren that
2088 ;; ends on another line.
2089 (narrow-to-region (point-min) (c-point 'eol
))
2090 (let (beg (end (point)))
2091 (while (and (setq beg
(c-down-list-forward end
))
2092 (setq end
(c-up-list-forward beg
))))
2094 (eq (char-syntax (char-before beg
)) ?\
()
2099 (error "Cannot find start of balanced expression to indent"))
2101 (setq end
(c-safe (scan-sexps (point) 1)))
2104 (error "Cannot find end of balanced expression to indent"))
2107 (c-indent-region (point) end
)))))
2109 (set-marker here nil
))))
2111 (defun c-indent-defun ()
2112 "Indent the current top-level declaration or macro syntactically.
2113 In the macro case this also has the effect of realigning any line
2114 continuation backslashes, unless `c-auto-align-backslashes' is nil."
2116 (let ((here (point-marker)) decl-limits
)
2119 (c-save-buffer-state nil
2120 ;; We try to be line oriented, unless there are several
2121 ;; declarations on the same line.
2122 (if (looking-at c-syntactic-eol
)
2123 (c-backward-token-2 1 nil
(c-point 'bol
))
2124 (c-forward-token-2 0 nil
(c-point 'eol
)))
2125 (setq decl-limits
(c-declaration-limits nil
)))
2127 (c-indent-region (car decl-limits
)
2128 (cdr decl-limits
))))
2130 (set-marker here nil
))))
2132 (defun c-indent-region (start end
&optional quiet
)
2133 "Indent syntactically every line whose first char is between START
2134 and END inclusive. If the optional argument QUIET is non-nil then no
2135 syntactic errors are reported, even if `c-report-syntactic-errors' is
2139 (skip-chars-backward " \t\n\r\f\v")
2142 ;; Advance to first nonblank line.
2144 (skip-chars-forward " \t\n\r\f\v")
2145 (setq start
(point))
2147 (setq c-parsing-error
2148 (or (let ((endmark (copy-marker end
))
2149 (c-parsing-error nil
)
2150 ;; shut up any echo msgs on indiv lines
2151 (c-echo-syntactic-information-p nil
)
2152 (in-macro (and c-auto-align-backslashes
2153 (save-excursion (c-beginning-of-macro))
2155 (c-fix-backslashes nil
)
2159 (c-progress-init start end
'c-indent-region
)
2162 (< (point) endmark
))
2166 (skip-chars-forward " \t\n")
2168 ;; Get syntax and indent.
2169 (c-save-buffer-state nil
2170 (setq syntax
(c-guess-basic-syntax)))
2171 (if (and c-auto-align-backslashes
2172 (assq 'cpp-macro syntax
))
2173 ;; Record macro start.
2174 (setq in-macro
(point)))
2176 (if (looking-at "\\s *\\\\$")
2178 (c-indent-line syntax t t
)
2179 (if (progn (end-of-line)
2180 (not (eq (char-before) ?
\\)))
2182 ;; Fixup macro backslashes.
2184 (c-backslash-region in-macro
(point) nil
)
2185 (setq in-macro nil
))
2187 (c-indent-line syntax t t
)
2190 (c-backslash-region in-macro
(c-point 'bopl
) nil t
)))
2191 (set-marker endmark nil
)
2192 (c-progress-fini 'c-indent-region
))
2193 (c-echo-parsing-error quiet
))
2196 (defun c-fn-region-is-active-p ()
2197 ;; Function version of the macro for use in places that aren't
2198 ;; compiled, e.g. in the menus.
2200 ;; This function does not do any hidden buffer changes.
2201 (c-region-is-active-p))
2203 (defun c-indent-line-or-region ()
2204 "When the region is active, indent it syntactically. Otherwise
2205 indent the current line syntactically."
2206 ;; Emacs has a variable called mark-active, XEmacs uses region-active-p
2208 (if (c-region-is-active-p)
2209 (c-indent-region (region-beginning) (region-end))
2213 ;; for progress reporting
2214 (defvar c-progress-info nil
)
2216 (defun c-progress-init (start end context
)
2217 ;; This function does not do any hidden buffer changes.
2220 ((not c-progress-interval
))
2221 ;; Start the progress update messages. If this Emacs doesn't have
2222 ;; a built-in timer, just be dumb about it.
2223 ((not (fboundp 'current-time
))
2224 (message "Indenting region... (this may take a while)"))
2225 ;; If progress has already been initialized, do nothing. otherwise
2226 ;; initialize the counter with a vector of:
2227 ;; [start end lastsec context]
2229 (t (setq c-progress-info
(vector start
2233 (nth 1 (current-time))
2235 (message "Indenting region..."))
2238 (defun c-progress-update ()
2239 ;; This function does not do any hidden buffer changes.
2240 (if (not (and c-progress-info c-progress-interval
))
2242 (let ((now (nth 1 (current-time)))
2243 (start (aref c-progress-info
0))
2244 (end (aref c-progress-info
1))
2245 (lastsecs (aref c-progress-info
2)))
2246 ;; should we update? currently, update happens every 2 seconds,
2247 ;; what's the right value?
2248 (if (< c-progress-interval
(- now lastsecs
))
2250 (message "Indenting region... (%d%% complete)"
2251 (/ (* 100 (- (point) start
)) (- end start
)))
2252 (aset c-progress-info
2 now
)))
2255 (defun c-progress-fini (context)
2256 ;; This function does not do any hidden buffer changes.
2257 (if (not c-progress-interval
)
2259 (if (or (eq context
(aref c-progress-info
3))
2262 (set-marker (aref c-progress-info
1) nil
)
2263 (setq c-progress-info nil
)
2264 (message "Indenting region... done")))))
2268 ;;; This page handles insertion and removal of backslashes for C macros.
2270 (defun c-backslash-region (from to delete-flag
&optional line-mode
)
2271 "Insert, align, or delete end-of-line backslashes on the lines in the region.
2272 With no argument, inserts backslashes and aligns existing backslashes.
2273 With an argument, deletes the backslashes. The backslash alignment is
2274 done according to the settings in `c-backslash-column',
2275 `c-backslash-max-column' and `c-auto-align-backslashes'.
2277 This function does not modify blank lines at the start of the region.
2278 If the region ends at the start of a line and the macro doesn't
2279 continue below it, the backslash (if any) at the end of the previous
2282 You can put the region around an entire macro definition and use this
2283 command to conveniently insert and align the necessary backslashes."
2284 (interactive "*r\nP")
2285 (let ((endmark (make-marker))
2286 ;; Keep the backslash trimming functions from changing the
2287 ;; whitespace around point, since in this case it's only the
2288 ;; position of point that tells the indentation of the line.
2289 (point-pos (if (save-excursion
2290 (skip-chars-backward " \t")
2291 (and (bolp) (looking-at "[ \t]*\\\\?$")))
2294 column longest-line-col bs-col-after-end
)
2297 (if (and (not line-mode
) (bobp))
2298 ;; Nothing to do if to is at bob, since we should back up
2299 ;; and there's no line to back up to.
2301 (when (and (not line-mode
) (bolp))
2302 ;; Do not back up the to line if line-mode is set, to make
2303 ;; e.g. c-newline-and-indent consistent regardless whether
2304 ;; the (newline) call leaves point at bol or not.
2309 (set-marker endmark
(point))
2311 (c-delete-backslashes-forward endmark point-pos
))
2312 ;; Set bs-col-after-end to the column of any backslash
2313 ;; following the region, or nil if there is none.
2314 (setq bs-col-after-end
2315 (and (progn (end-of-line)
2316 (eq (char-before) ?
\\))
2317 (= (forward-line 1) 0)
2318 (progn (end-of-line)
2319 (eq (char-before) ?
\\))
2320 (1- (current-column))))
2322 ;; Back up the to line if line-mode is set, since the line
2323 ;; after the newly inserted line break should not be
2324 ;; touched in c-newline-and-indent.
2325 (setq to
(max from
(or (c-safe (c-point 'eopl
)) from
)))
2326 (unless bs-col-after-end
2327 ;; Set bs-col-after-end to non-nil in any case, since we
2328 ;; do not want to delete the backslash at the last line.
2329 (setq bs-col-after-end t
)))
2331 (not c-auto-align-backslashes
))
2333 ;; Compute the smallest column number past the ends of all
2335 (setq longest-line-col
0)
2337 (if bs-col-after-end
2338 ;; Include one more line in the max column
2339 ;; calculation, since the to line will be backslashed
2343 (while (and (>= (point) from
)
2345 (if (eq (char-before) ?
\\)
2347 (skip-chars-backward " \t")
2348 (setq longest-line-col
(max longest-line-col
2349 (1+ (current-column))))
2353 ;; Try to align with surrounding backslashes.
2356 (if (and (not (bobp))
2357 (progn (backward-char)
2358 (eq (char-before) ?
\\)))
2360 (setq column
(1- (current-column)))
2361 (if (numberp bs-col-after-end
)
2362 ;; Both a preceding and a following backslash.
2363 ;; Choose the greatest of them.
2364 (setq column
(max column bs-col-after-end
)))
2366 ;; No preceding backslash. Try to align with one
2367 ;; following the region. Disregard the backslash at the
2368 ;; to line since it's likely to be bogus (e.g. when
2369 ;; called from c-newline-and-indent).
2370 (if (numberp bs-col-after-end
)
2371 (setq column bs-col-after-end
))
2372 ;; Don't modify blank lines at start of region.
2374 (while (and (< (point) to
) (bolp) (eolp))
2376 (if (and column
(< column longest-line-col
))
2377 ;; Don't try to align with surrounding backslashes if
2378 ;; any line is too long.
2381 ;; Impose minimum limit and tab width alignment only if
2382 ;; we can't align with surrounding backslashes.
2383 (if (> (% longest-line-col tab-width
) 0)
2384 (setq longest-line-col
2385 (* (/ (+ longest-line-col tab-width -
1)
2388 (setq column
(max c-backslash-column
2390 ;; Always impose maximum limit.
2391 (setq column
(min column c-backslash-max-column
)))
2392 (if bs-col-after-end
2393 ;; Add backslashes on all lines if the macro continues
2394 ;; after the to line.
2396 (set-marker endmark to
)
2397 (c-append-backslashes-forward endmark column point-pos
))
2398 ;; Add backslashes on all lines except the last, and
2399 ;; remove any on the last line.
2404 (set-marker endmark
(1- (point)))))
2406 (c-append-backslashes-forward endmark column point-pos
)
2407 ;; The function above leaves point on the line
2408 ;; following endmark.
2409 (set-marker endmark
(point)))
2410 (set-marker endmark to
))
2411 (c-delete-backslashes-forward endmark point-pos
)))))
2412 (set-marker endmark nil
)
2413 (if (markerp point-pos
)
2414 (set-marker point-pos nil
))))
2416 (defun c-append-backslashes-forward (to-mark column point-pos
)
2417 ;; This function does not do any hidden buffer changes.
2418 (let ((state (parse-partial-sexp (c-point 'bol
) (point))))
2422 (<= (point) to-mark
)
2424 (let ((start (point)) (inserted nil
) end col
)
2426 (unless (eq (char-before) ?
\\)
2429 (setq state
(parse-partial-sexp
2430 start
(point) nil nil state
))
2432 (setq col
(current-column))
2434 ;; Avoid unnecessary changes of the buffer.
2435 (cond ((and (not inserted
) (nth 3 state
))
2436 ;; Don't realign backslashes in string literals
2437 ;; since that would change them.
2444 (skip-chars-backward
2445 " \t" (if (>= (point) point-pos
) point-pos
))
2449 ((and (= col column
)
2450 (memq (char-before) '(?\ ?
\t))))
2454 (or (/= (skip-chars-backward
2455 " \t" (if (>= (point) point-pos
) point-pos
))
2457 (/= (char-after) ?\
)))
2458 (delete-region (point) end
)
2459 (indent-to column
1)))
2461 (= (forward-line 1) 0))))
2463 ;; Make sure there are backslashes with at least one space in
2467 (<= (point) to-mark
)
2469 (let ((start (point)))
2471 (setq state
(parse-partial-sexp
2472 start
(point) nil nil state
))
2474 (if (eq (char-before) ?
\\)
2475 (unless (nth 3 state
)
2477 (unless (and (memq (char-before) '(?\ ?
\t))
2478 (/= (point) point-pos
))
2481 (if (and (memq (char-before) '(?\ ?
\t))
2482 (/= (point) point-pos
))
2486 (= (forward-line 1) 0)))))))
2488 (defun c-delete-backslashes-forward (to-mark point-pos
)
2489 ;; This function does not do any hidden buffer changes.
2491 (and (<= (point) to-mark
)
2494 (if (eq (char-before) ?
\\)
2497 (progn (backward-char)
2498 (skip-chars-backward " \t" (if (>= (point) point-pos
)
2501 (= (forward-line 1) 0)))))
2505 ;;; Line breaking and paragraph filling.
2507 (defvar c-auto-fill-prefix t
)
2508 (defvar c-lit-limits nil
)
2509 (defvar c-lit-type nil
)
2511 ;; The filling code is based on a simple theory; leave the intricacies
2512 ;; of the text handling to the currently active mode for that
2513 ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
2514 ;; possible to make them work correctly wrt the comment and string
2515 ;; separators, one-line paragraphs etc. Unfortunately, when it comes
2516 ;; to it, there's quite a lot of special cases to handle which makes
2517 ;; the code anything but simple. The intention is that it will work
2518 ;; with any well-written text filling package that preserves a fill
2521 ;; We temporarily mask comment starters and enders as necessary for
2522 ;; the filling code to do its job on a seemingly normal text block.
2523 ;; We do _not_ mask the fill prefix, so it's up to the filling code to
2524 ;; preserve it correctly (especially important when filling C++ style
2525 ;; line comments). By default, we set up and use adaptive-fill-mode,
2526 ;; which is standard in all supported Emacs flavors.
2528 (defun c-guess-fill-prefix (lit-limits lit-type
)
2529 ;; Determine the appropriate comment fill prefix for a block or line
2530 ;; comment. Return a cons of the prefix string and the column where
2531 ;; it ends. If fill-prefix is set, it'll override. Note that this
2532 ;; function also uses the value of point in some heuristics.
2534 (let* ((here (point))
2535 (prefix-regexp (concat "[ \t]*\\("
2536 c-current-comment-prefix
2538 (comment-start-regexp (if (eq lit-type
'c
++)
2540 comment-start-skip
))
2541 prefix-line comment-prefix res comment-text-end
)
2545 (setq res
(cons fill-prefix
2546 ;; Ugly way of getting the column after the fill
2547 ;; prefix; it'd be nice with a current-column
2548 ;; that works on strings..
2549 (let ((start (point)))
2552 (insert-and-inherit "\n" fill-prefix
)
2554 (delete-region start
(point)))))))
2558 ;; Set fallback for comment-prefix if none is found.
2559 (setq comment-prefix
"// "
2560 comment-text-end
(cdr lit-limits
))
2563 (if (> (point) (car lit-limits
))
2564 ;; The current line is not the comment starter, so the
2565 ;; comment has more than one line, and it can therefore be
2566 ;; used to find the comment fill prefix.
2567 (setq prefix-line
(point))
2569 (goto-char (car lit-limits
))
2570 (if (and (= (forward-line 1) 0)
2571 (< (point) (cdr lit-limits
)))
2572 ;; The line after the comment starter is inside the
2573 ;; comment, so we can use it.
2574 (setq prefix-line
(point))
2576 ;; The comment is only one line. Take the comment prefix
2577 ;; from it and keep the indentation.
2578 (goto-char (car lit-limits
))
2579 (if (looking-at prefix-regexp
)
2580 (goto-char (match-end 0))
2582 (skip-chars-forward " \t"))
2585 (if (eq (c-point 'boi
) (car lit-limits
))
2586 ;; There is only whitespace before the comment
2587 ;; starter; take the prefix straight from this line.
2588 (setq str
(buffer-substring-no-properties
2589 (c-point 'bol
) (point))
2590 col
(current-column))
2592 ;; There is code before the comment starter, so we
2593 ;; have to temporarily insert and indent a new line to
2594 ;; get the right space/tab mix in the indentation.
2595 (let ((prefix-len (- (point) (car lit-limits
)))
2599 (goto-char (car lit-limits
))
2600 (indent-to (prog1 (current-column)
2603 (forward-char prefix-len
)
2604 (setq str
(buffer-substring-no-properties
2605 (c-point 'bol
) (point))
2606 col
(current-column)))
2607 (delete-region (car lit-limits
) tmp
))))
2610 (if (or (string-match "\\s \\'" str
) (not (eolp)))
2612 ;; The prefix ends the line with no whitespace
2613 ;; after it. Default to a single space.
2614 (cons (concat str
" ") (1+ col
))))
2618 (setq comment-text-end
2620 (goto-char (- (cdr lit-limits
) 2))
2621 (if (looking-at "\\*/") (point) (cdr lit-limits
))))
2625 (if (and (> (point) (car lit-limits
))
2626 (not (and (looking-at "[ \t]*\\*/")
2627 (eq (cdr lit-limits
) (match-end 0)))))
2628 ;; The current line is not the comment starter and
2629 ;; contains more than just the ender, so it's good enough
2630 ;; to be used for the comment fill prefix.
2631 (setq prefix-line
(point))
2632 (goto-char (car lit-limits
))
2634 (cond ((or (/= (forward-line 1) 0)
2635 (>= (point) (cdr lit-limits
))
2636 (and (looking-at "[ \t]*\\*/")
2637 (eq (cdr lit-limits
) (match-end 0)))
2638 (and (looking-at prefix-regexp
)
2639 (<= (1- (cdr lit-limits
)) (match-end 0))))
2640 ;; The comment is either one line or the next line contains
2641 ;; just the comment ender. In this case we have no
2642 ;; information about a suitable comment prefix, so we resort
2643 ;; to c-block-comment-prefix.
2644 (setq comment-prefix
(or c-block-comment-prefix
"")))
2647 ;; The point was on the comment opener line, so we might want
2648 ;; to treat this as a not yet closed comment.
2650 (if (and (match-beginning 1)
2651 (/= (match-beginning 1) (match-end 1)))
2652 ;; Above `prefix-regexp' matched a nonempty prefix on the
2653 ;; second line, so let's use it. Normally it should do
2654 ;; to set `prefix-line' and let the code below pick up
2655 ;; the whole prefix, but if there's no text after the
2656 ;; match then it will probably fall back to no prefix at
2657 ;; all if the comment isn't closed yet, so in that case
2658 ;; it's better to force use of the prefix matched now.
2659 (if (= (match-end 0) (c-point 'eol
))
2660 (setq comment-prefix
(match-string 1))
2661 (setq prefix-line
(point)))
2663 ;; There's no nonempty prefix on the line after the
2664 ;; comment opener. If the line is empty, or if the
2665 ;; text on has less or equal indentation than the
2666 ;; comment starter we assume it's an unclosed
2667 ;; comment starter, i.e. that
2668 ;; `c-block-comment-prefix' should be used.
2669 ;; Otherwise we assume it's a closed comment where
2670 ;; the prefix really is the empty string.
2671 ;; E.g. this is an unclosed comment:
2682 ;; (Looking for the presence of the comment closer
2683 ;; rarely works since it's probably the closer of
2684 ;; some comment further down when the comment
2685 ;; really is unclosed.)
2686 (if (<= (save-excursion (back-to-indentation)
2688 (save-excursion (goto-char (car lit-limits
))
2690 (setq comment-prefix
(or c-block-comment-prefix
""))
2691 (setq prefix-line
(point)))))
2694 ;; Otherwise the line after the comment starter is good
2695 ;; enough to find the prefix in.
2696 (setq prefix-line
(point))))
2698 (when comment-prefix
2699 ;; Haven't got the comment prefix on any real line that we
2700 ;; can take it from, so we have to temporarily insert
2701 ;; `comment-prefix' on a line and indent it to find the
2702 ;; correct column and the correct mix of tabs and spaces.
2704 (let (tmp-pre tmp-post
)
2708 (goto-char (car lit-limits
))
2709 (if (looking-at comment-start-regexp
)
2710 (goto-char (min (match-end 0)
2713 (skip-chars-forward " \t"))
2715 (when (eq (char-syntax (char-before)) ?\
)
2716 ;; If there's ws on the current line, we'll use it
2717 ;; instead of what's ending comment-prefix.
2718 (setq comment-prefix
2719 (concat (substring comment-prefix
2723 (buffer-substring-no-properties
2725 (skip-chars-backward " \t")
2729 (setq tmp-pre
(point-marker))
2731 ;; We insert an extra non-whitespace character
2732 ;; before the line break and after comment-prefix in
2733 ;; case it's "" or ends with whitespace.
2734 (insert-and-inherit "x\n" comment-prefix
"x")
2735 (setq tmp-post
(point-marker))
2737 (indent-according-to-mode)
2739 (goto-char (1- tmp-post
))
2740 (cons (buffer-substring-no-properties
2741 (c-point 'bol
) (point))
2745 (delete-region tmp-pre tmp-post
)
2746 (set-marker tmp-pre nil
)
2747 (set-marker tmp-post nil
))))))))))
2749 (or res
; Found a good prefix above.
2752 ;; prefix-line is the bol of a line on which we should try
2753 ;; to find the prefix.
2754 (let* (fb-string fb-endpos
; Contains any fallback prefix found.
2757 (when (and (looking-at prefix-regexp
)
2758 (<= (match-end 0) comment-text-end
))
2759 (unless (eq (match-end 0) (c-point 'eol
))
2760 ;; The match is fine if there's text after it.
2761 (throw 'found
(cons (buffer-substring-no-properties
2762 (match-beginning 0) (match-end 0))
2763 (progn (goto-char (match-end 0))
2764 (current-column)))))
2766 ;; This match is better than nothing, so let's
2767 ;; remember it in case nothing better is found
2769 (setq fb-string
(buffer-substring-no-properties
2770 (match-beginning 0) (match-end 0))
2771 fb-endpos
(match-end 0)))
2775 ;; Search for a line which has text after the prefix
2776 ;; so that we get the proper amount of whitespace
2777 ;; after it. We start with the current line, then
2778 ;; search backwards, then forwards.
2780 (goto-char prefix-line
)
2781 (when (and (funcall test-line
)
2782 (or (/= (match-end 1) (match-end 0))
2783 ;; The whitespace is sucked up by the
2784 ;; first [ \t]* glob if the prefix is empty.
2785 (and (= (match-beginning 1) (match-end 1))
2786 (/= (match-beginning 0) (match-end 0)))))
2787 ;; If the current line doesn't have text but do
2788 ;; have whitespace after the prefix, we'll use it.
2789 (throw 'found
(cons fb-string
2790 (progn (goto-char fb-endpos
)
2791 (current-column)))))
2793 (if (eq lit-type
'c
++)
2794 ;; For line comments we can search up to and
2795 ;; including the first line.
2796 (while (and (zerop (forward-line -
1))
2797 (>= (point) (car lit-limits
)))
2798 (funcall test-line
))
2799 ;; For block comments we must stop before the
2801 (while (and (zerop (forward-line -
1))
2802 (> (point) (car lit-limits
)))
2803 (funcall test-line
)))
2805 (goto-char prefix-line
)
2806 (while (and (zerop (forward-line 1))
2807 (< (point) (cdr lit-limits
)))
2808 (funcall test-line
))
2810 (goto-char prefix-line
)
2814 ;; A good line wasn't found, but at least we have a
2815 ;; fallback that matches the comment prefix regexp.
2816 (cond ((or (string-match "\\s \\'" fb-string
)
2818 (goto-char fb-endpos
)
2820 ;; There are ws or text after the prefix, so
2822 (cons fb-string
(current-column)))
2825 ;; Check if there's any whitespace padding
2826 ;; on the comment start line that we can
2827 ;; use after the prefix.
2828 (goto-char (car lit-limits
))
2829 (if (looking-at comment-start-regexp
)
2830 (goto-char (match-end 0))
2832 (skip-chars-forward " \t"))
2834 (eq (char-syntax (char-before)) ?\
)))
2836 (setq fb-string
(buffer-substring-no-properties
2838 (skip-chars-backward " \t")
2841 (goto-char fb-endpos
)
2842 (skip-chars-backward " \t")
2844 (let ((tmp (point)))
2845 ;; Got to mess in the buffer once again to
2846 ;; ensure the column gets correct. :P
2849 (insert-and-inherit fb-string
)
2850 (cons (buffer-substring-no-properties
2854 (delete-region tmp
(point)))))
2857 ;; Last resort: Just add a single space after
2859 (cons (concat fb-string
" ")
2860 (progn (goto-char fb-endpos
)
2861 (1+ (current-column)))))))
2863 ;; The line doesn't match the comment prefix regexp.
2865 ;; We have a fallback for line comments that we must use.
2866 (cons (concat (buffer-substring-no-properties
2867 prefix-line
(c-point 'boi
))
2869 (progn (back-to-indentation)
2870 (+ (current-column) (length comment-prefix
))))
2872 ;; Assume we are dealing with a "free text" block
2873 ;; comment where the lines doesn't have any comment
2874 ;; prefix at all and we should just fill it as
2879 (defun c-mask-paragraph (fill-paragraph apply-outside-literal fun
&rest args
)
2880 ;; Calls FUN with ARGS ar arguments while the current paragraph is
2881 ;; masked to allow adaptive filling to work correctly. That
2882 ;; includes narrowing the buffer and, if point is inside a comment,
2883 ;; masking the comment starter and ender appropriately.
2885 ;; FILL-PARAGRAPH is non-nil if called for whole paragraph filling.
2886 ;; The position of point is then less significant when doing masking
2889 ;; If APPLY-OUTSIDE-LITERAL is nil then the function will be called
2890 ;; only if the point turns out to be inside a comment or a string.
2892 ;; This function does not do any hidden buffer changes.
2895 ;; beg and end limits the region to narrow. end is a marker.
2897 ;; tmp-pre and tmp-post mark strings that are temporarily
2898 ;; inserted at the start and end of the region. tmp-pre is a
2899 ;; cons of the positions of the prepended string. tmp-post is
2900 ;; a marker pointing to the single character of the appended
2903 ;; If hang-ender-stuck isn't nil, the comment ender is
2904 ;; hanging. In that case it's set to the number of spaces
2905 ;; that should be between the text and the ender.
2908 (c-lit-limits c-lit-limits
)
2909 (c-lit-type c-lit-type
))
2911 ;; Restore point on undo. It's necessary since we do a lot of
2912 ;; hidden inserts and deletes below that should be as transparent
2914 (if (and buffer-undo-list
(not (eq buffer-undo-list t
)))
2915 (setq buffer-undo-list
(cons (point) buffer-undo-list
)))
2918 ;; Widen to catch comment limits correctly.
2920 (unless c-lit-limits
2921 (setq c-lit-limits
(c-literal-limits nil fill-paragraph
)))
2922 (setq c-lit-limits
(c-collect-line-comments c-lit-limits
))
2924 (setq c-lit-type
(c-literal-type c-lit-limits
))))
2927 (unless (c-safe (backward-char)
2931 (forward-paragraph))
2932 (setq end
(point-marker)))
2934 (unless (c-safe (forward-char)
2935 (backward-paragraph)
2938 (backward-paragraph))
2945 ((eq c-lit-type
'c
++) ; Line comment.
2947 ;; Limit to the comment or paragraph end, whichever
2949 (set-marker end
(min end
(cdr c-lit-limits
)))
2951 (when (<= beg
(car c-lit-limits
))
2952 ;; The region includes the comment starter, so we must
2954 (goto-char (car c-lit-limits
))
2955 (back-to-indentation)
2956 (if (eq (point) (car c-lit-limits
))
2957 ;; Include the first line in the region.
2958 (setq beg
(c-point 'bol
))
2959 ;; The first line contains code before the
2960 ;; comment. We must fake a line that doesn't.
2963 (setq apply-outside-literal t
))
2965 ((eq c-lit-type
'c
) ; Block comment.
2966 (when (>= end
(cdr c-lit-limits
))
2967 ;; The region includes the comment ender which we might
2968 ;; want to keep together with the last word.
2969 (unless (save-excursion
2970 (goto-char (cdr c-lit-limits
))
2972 (and (looking-at (concat "[ \t]*\\("
2973 c-current-comment-prefix
2975 (eq (cdr c-lit-limits
) (match-end 0))
2976 ;; The comment ender is on a line of its
2977 ;; own. Keep it that way.
2978 (set-marker end
(point))))
2981 ;; The comment ender should hang. Replace all
2982 ;; cruft between it and the last word with one or
2983 ;; two 'x' and include it in the region. We'll
2984 ;; change them back to spaces afterwards. This
2985 ;; isn't done when auto filling, since that'd
2986 ;; effectively make it impossible to insert extra
2987 ;; spaces before the comment ender.
2988 (let* ((ender-start (save-excursion
2989 (goto-char (cdr c-lit-limits
))
2990 (skip-syntax-backward "^w ")
2992 (point-rel (- ender-start here
))
2996 (goto-char (cdr c-lit-limits
))
2997 (setq tmp-post
(point-marker))
2999 (set-marker end
(point))
3001 (if (and (looking-at (concat "[ \t]*\\(\\("
3002 c-current-comment-prefix
3004 (eq ender-start
(match-end 0)))
3005 ;; The comment ender is prefixed by nothing
3006 ;; but a comment line prefix. Remove it
3007 ;; along with surrounding ws.
3008 (setq spaces
(- (match-end 1) (match-end 2)))
3009 (goto-char ender-start
))
3010 (skip-chars-backward " \t\r\n")
3012 (if (/= (point) ender-start
)
3014 (if (<= here
(point))
3015 ;; Don't adjust point below if it's
3016 ;; before the string we replace.
3017 (setq point-rel -
1))
3018 ;; Keep one or two spaces between the
3019 ;; text and the ender, depending on how
3020 ;; many there are now.
3022 (setq spaces
(- ender-start
(point))))
3026 (if sentence-end-double-space
2 1))
3028 ;; Insert the filler first to keep marks right.
3029 (insert-char ?x spaces t
)
3030 (delete-region (point) (+ ender-start spaces
))
3031 (setq hang-ender-stuck spaces
)
3033 (and (>= point-rel
0)
3034 (- (point) (min point-rel spaces
)))))
3035 (setq point-rel nil
)))
3038 ;; Point was in the middle of the string we
3039 ;; replaced above, so put it back in the same
3040 ;; relative position, counting from the end.
3041 (goto-char point-rel
)))
3043 ;; We're doing auto filling. Just move the marker
3044 ;; to the comment end to ignore any code after the
3046 (move-marker end
(cdr c-lit-limits
)))))
3048 (when (<= beg
(car c-lit-limits
))
3049 ;; The region includes the comment starter.
3051 (goto-char (car c-lit-limits
))
3052 (if (looking-at (concat "\\(" comment-start-skip
"\\)$"))
3053 ;; Begin with the next line.
3054 (setq beg
(c-point 'bonl
))
3055 ;; Fake the fill prefix in the first line.
3058 (setq apply-outside-literal t
))
3060 ((eq c-lit-type
'string
) ; String.
3062 (when (>= end
(cdr c-lit-limits
))
3063 (goto-char (1- (cdr c-lit-limits
)))
3064 (setq tmp-post
(point-marker))
3066 (set-marker end
(point)))
3067 (when (<= beg
(car c-lit-limits
))
3068 (goto-char (1+ (car c-lit-limits
)))
3069 (setq beg
(if (looking-at "\\\\$")
3070 ;; Leave the start line if it's
3071 ;; nothing but an escaped newline.
3074 (setq apply-outside-literal t
))
3076 ((eq c-lit-type
'pound
) ; Macro
3077 ;; Narrow to the macro limits if they are nearer than the
3078 ;; paragraph limits. Don't know if this is necessary but
3079 ;; do it for completeness sake (doing auto filling at all
3080 ;; inside macros is bogus to begin with since the line
3081 ;; continuation backslashes aren't handled).
3083 (c-beginning-of-macro)
3090 (set-marker end
(point)))))
3093 ;; Try to avoid comments and macros in the paragraph to
3094 ;; avoid that the adaptive fill mode gets the prefix from
3096 (c-save-buffer-state nil
3099 (c-forward-syntactic-ws end
)
3103 (c-backward-syntactic-ws beg
)
3105 (set-marker end
(point))))))
3108 ;; Temporarily insert the fill prefix after the comment
3109 ;; starter so that the first line looks like any other
3110 ;; comment line in the narrowed region.
3111 (setq fill
(c-save-buffer-state nil
3112 (c-guess-fill-prefix c-lit-limits c-lit-type
)))
3113 (unless (string-match (concat "\\`[ \t]*\\("
3114 c-current-comment-prefix
3117 ;; Oops, the prefix doesn't match the comment prefix
3118 ;; regexp. This could produce very confusing
3119 ;; results with adaptive fill packages together with
3120 ;; the insert prefix magic below, since the prefix
3121 ;; often doesn't appear at all. So let's warn about
3124 Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S"
3126 ;; Find the right spot on the line, break it, insert
3127 ;; the fill prefix and make sure we're back in the
3128 ;; same column by temporarily prefixing the first word
3129 ;; with a number of 'x'.
3131 (goto-char (car c-lit-limits
))
3132 (if (looking-at (if (eq c-lit-type
'c
++)
3133 c-current-comment-prefix
3134 comment-start-skip
))
3135 (goto-char (match-end 0))
3137 (skip-chars-forward " \t"))
3138 (while (and (< (current-column) (cdr fill
))
3141 (let ((col (current-column)))
3142 (setq beg
(1+ (point))
3143 tmp-pre
(list (point)))
3146 (insert-and-inherit "\n" (car fill
))
3147 (insert-char ?x
(- col
(current-column)) t
))
3148 (setcdr tmp-pre
(point))))))
3150 (when apply-outside-literal
3151 ;; `apply-outside-literal' is always set to t here if
3152 ;; we're inside a literal.
3156 ;; Kludge: If the function that adapts the fill prefix
3157 ;; doesn't produce the required comment starter for
3158 ;; line comments, then force it by setting fill-prefix.
3159 (when (and (eq c-lit-type
'c
++)
3160 ;; Kludge the kludge: filladapt-mode doesn't
3161 ;; have this problem, but it currently
3162 ;; doesn't override fill-context-prefix
3164 (not (and (boundp 'filladapt-mode
)
3168 (or (fill-context-prefix beg end
)
3170 (c-save-buffer-state nil
3171 (car (or fill
(c-guess-fill-prefix
3172 c-lit-limits c-lit-type
)))))))
3174 ;; Save the relative position of point if it's outside the
3175 ;; region we're going to narrow. Want to restore it in that
3176 ;; case, but otherwise it should be moved according to the
3178 (point-rel (cond ((< (point) beg
) (- (point) beg
))
3179 ((> (point) end
) (- (point) end
)))))
3181 ;; Preparations finally done! Now we can call the
3185 (narrow-to-region beg end
)
3188 ;; Restore point if it was outside the region.
3190 (goto-char (+ beg point-rel
))
3191 (goto-char (+ end point-rel
))))))))
3193 (when (consp tmp-pre
)
3194 (delete-region (car tmp-pre
) (cdr tmp-pre
)))
3198 (goto-char tmp-post
)
3200 (when hang-ender-stuck
3201 ;; Preserve point even if it's in the middle of the string
3202 ;; we replace; save-excursion doesn't work in that case.
3204 (goto-char tmp-post
)
3205 (skip-syntax-backward "^w ")
3206 (forward-char (- hang-ender-stuck
))
3207 (insert-char ?\ hang-ender-stuck t
)
3208 (delete-char hang-ender-stuck
)
3210 (set-marker tmp-post nil
))
3212 (set-marker end nil
))))
3214 (defun c-fill-paragraph (&optional arg
)
3215 "Like \\[fill-paragraph] but handles C and C++ style comments.
3216 If any of the current line is a comment or within a comment, fill the
3217 comment or the paragraph of it that point is in, preserving the
3218 comment indentation or line-starting decorations (see the
3219 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
3222 If point is inside multiline string literal, fill it. This currently
3223 does not respect escaped newlines, except for the special case when it
3224 is the very first thing in the string. The intended use for this rule
3225 is in situations like the following:
3227 char description[] = \"\\
3228 A very long description of something that you want to fill to make
3229 nicely formatted output.\"\;
3231 If point is in any other situation, i.e. in normal code, do nothing.
3233 Optional prefix ARG means justify paragraph as well."
3235 (let ((fill-paragraph-function
3236 ;; Avoid infinite recursion.
3237 (if (not (eq fill-paragraph-function
'c-fill-paragraph
))
3238 fill-paragraph-function
)))
3239 (c-mask-paragraph t nil
'fill-paragraph arg
))
3240 ;; Always return t. This has the effect that if filling isn't done
3241 ;; above, it isn't done at all, and it's therefore effectively
3242 ;; disabled in normal code.
3245 (defun c-do-auto-fill ()
3246 ;; Do automatic filling if not inside a context where it should be
3249 ;; This function does not do any hidden buffer changes.
3250 (let ((c-auto-fill-prefix
3251 ;; The decision whether the line should be broken is actually
3252 ;; done in c-indent-new-comment-line, which do-auto-fill
3253 ;; calls to break lines. We just set this special variable
3254 ;; so that we'll know when we're called from there. It's
3255 ;; also used to detect whether fill-prefix is user set or
3256 ;; generated automatically by do-auto-fill.
3258 (c-mask-paragraph nil t
'do-auto-fill
)))
3260 (defun c-indent-new-comment-line (&optional soft allow-auto-fill
)
3261 "Break line at point and indent, continuing comment or macro if within one.
3262 If inside a comment and `comment-multi-line' is non-nil, the
3263 indentation and line prefix are preserved (see the
3264 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
3265 details). If inside a single line comment and `comment-multi-line' is
3266 nil, a new comment of the same type is started on the next line and
3267 indented as appropriate for comments. If inside a macro, a line
3268 continuation backslash is inserted and aligned as appropriate, and the
3269 new line is indented according to `c-syntactic-indentation'.
3271 If a fill prefix is specified, it overrides all the above."
3272 ;; allow-auto-fill is used from c-context-line-break to allow auto
3273 ;; filling to break the line more than once. Since this function is
3274 ;; used from auto-fill itself, that's normally disabled to avoid
3275 ;; unnecessary recursion.
3277 (let ((fill-prefix fill-prefix
)
3280 (delete-horizontal-space)
3282 (insert-and-inherit ?
\n)
3283 (newline (if allow-auto-fill nil
1)))))
3284 ;; Already know the literal type and limits when called from
3285 ;; c-context-line-break.
3286 (c-lit-limits c-lit-limits
)
3287 (c-lit-type c-lit-type
)
3288 (c-macro-start c-macro-start
))
3289 (when (not (eq c-auto-fill-prefix t
))
3290 ;; Called from do-auto-fill.
3291 (unless c-lit-limits
3292 (setq c-lit-limits
(c-literal-limits nil nil t
)))
3294 (setq c-lit-type
(c-literal-type c-lit-limits
)))
3295 (if (memq (cond ((c-query-and-set-macro-start) 'cpp
)
3296 ((null c-lit-type
) 'code
)
3299 (setq fill-prefix t
) ; Used as flag in the cond.
3300 (if (and (null c-auto-fill-prefix
)
3302 (<= (c-point 'bol
) (car c-lit-limits
)))
3303 ;; The adaptive fill function has generated a prefix, but
3304 ;; we're on the first line in a block comment so it'll be
3305 ;; wrong. Ignore it to guess a better one below.
3306 (setq fill-prefix nil
)
3307 (when (and (eq c-lit-type
'c
++)
3308 (not (string-match "\\`[ \t]*//" (or fill-prefix
""))))
3309 ;; Kludge: If the function that adapted the fill prefix
3310 ;; doesn't produce the required comment starter for line
3311 ;; comments, then we ignore it.
3312 (setq fill-prefix nil
)))
3314 (cond ((eq fill-prefix t
)
3315 ;; A call from do-auto-fill which should be ignored.
3318 ;; A fill-prefix overrides anything.
3319 (funcall do-line-break
)
3320 (insert-and-inherit fill-prefix
))
3322 (unless c-lit-limits
3323 (setq c-lit-limits
(c-literal-limits)))
3325 (setq c-lit-type
(c-literal-type c-lit-limits
)))
3326 (memq c-lit-type
'(c c
++)))
3327 ;; Some sort of comment.
3328 (if (or comment-multi-line
3330 (goto-char (car c-lit-limits
))
3332 (< (point) (cdr c-lit-limits
))))
3333 ;; Inside a comment that should be continued.
3334 (let ((fill (c-save-buffer-state nil
3335 (c-guess-fill-prefix
3337 (c-collect-line-comments c-lit-limits
))
3341 (or (and (eq c-lit-type
'c
)
3343 (goto-char (- (cdr c-lit-limits
) 2))
3344 (if (looking-at "\\*/") (point))))
3345 (cdr c-lit-limits
))))
3346 ;; Skip forward past the fill prefix in case
3347 ;; we're standing in it.
3349 ;; FIXME: This doesn't work well in cases like
3351 ;; /* Bla bla bla bla bla
3354 ;; If point is on the 'B' then the line will be
3355 ;; broken after "Bla b".
3356 (while (and (< (current-column) (cdr fill
))
3359 (if (and (> (point) comment-text-end
)
3360 (> (c-point 'bol
) (car c-lit-limits
)))
3362 ;; The skip takes us out of the (block)
3363 ;; comment; insert the fill prefix at bol
3364 ;; instead and keep the position.
3365 (setq pos
(copy-marker pos t
))
3367 (insert-and-inherit (car fill
))
3368 (if soft
(insert-and-inherit ?
\n) (newline 1))
3370 (set-marker pos nil
))
3371 ;; Don't break in the middle of a comment starter
3373 (cond ((> (point) comment-text-end
)
3374 (goto-char comment-text-end
))
3375 ((< (point) (+ (car c-lit-limits
) 2))
3376 (goto-char (+ (car c-lit-limits
) 2))))
3377 (funcall do-line-break
)
3378 (insert-and-inherit (car fill
))))
3379 ;; Inside a comment that should be broken.
3380 (let ((comment-start comment-start
)
3381 (comment-end comment-end
)
3383 (if (eq c-lit-type
'c
)
3384 (unless (string-match "[ \t]*/\\*" comment-start
)
3385 (setq comment-start
"/* " comment-end
" */"))
3386 (unless (string-match "[ \t]*//" comment-start
)
3387 (setq comment-start
"// " comment-end
"")))
3388 (setq col
(save-excursion
3389 (back-to-indentation)
3391 (funcall do-line-break
)
3392 (when (and comment-end
(not (equal comment-end
"")))
3394 (insert-and-inherit comment-end
)
3396 ;; c-comment-indent may look at the current
3397 ;; indentation, so let's start out with the same
3398 ;; indentation as the previous one.
3400 (insert-and-inherit comment-start
)
3401 (indent-for-comment))))
3402 ((c-query-and-set-macro-start)
3404 (unless (looking-at "[ \t]*\\\\$")
3405 ;; Do not clobber the alignment of the line continuation
3406 ;; slash; c-backslash-region might look at it.
3407 (delete-horizontal-space))
3408 ;; Got an asymmetry here: In normal code this command
3409 ;; doesn't indent the next line syntactically, and otoh a
3410 ;; normal syntactically indenting newline doesn't continue
3412 (c-newline-and-indent (if allow-auto-fill nil
1)))
3414 ;; Somewhere else in the code.
3415 (let ((col (save-excursion
3417 (while (and (looking-at "[ \t]*\\\\?$")
3418 (= (forward-line -
1) 0)))
3419 (current-indentation))))
3420 (funcall do-line-break
)
3421 (indent-to col
))))))
3423 (defalias 'c-comment-line-break-function
'c-indent-new-comment-line
)
3424 (make-obsolete 'c-comment-line-break-function
'c-indent-new-comment-line
)
3426 ;; advice for indent-new-comment-line for older Emacsen
3427 (unless (boundp 'comment-line-break-function
)
3428 (defvar c-inside-line-break-advice nil
)
3429 (defadvice indent-new-comment-line
(around c-line-break-advice
3430 activate preactivate
)
3431 "Call `c-indent-new-comment-line' if in CC Mode."
3432 (if (or c-inside-line-break-advice
3433 (not c-buffer-is-cc-mode
))
3435 (let ((c-inside-line-break-advice t
))
3436 (c-indent-new-comment-line (ad-get-arg 0))))))
3438 (defun c-context-line-break ()
3439 "Do a line break suitable to the context.
3441 When point is outside a comment or macro, insert a newline and indent
3442 according to the syntactic context, unless `c-syntactic-indentation'
3443 is nil, in which case the new line is indented as the previous
3444 non-empty line instead.
3446 When point is inside the content of a preprocessor directive, a line
3447 continuation backslash is inserted before the line break and aligned
3448 appropriately. The end of the cpp directive doesn't count as inside
3451 When point is inside a comment, continue it with the appropriate
3452 comment prefix (see the `c-comment-prefix-regexp' and
3453 `c-block-comment-prefix' variables for details). The end of a
3454 C++-style line comment doesn't count as inside it."
3456 (let* ((c-lit-limits (c-literal-limits nil nil t
))
3457 (c-lit-type (c-literal-type c-lit-limits
))
3458 (c-macro-start c-macro-start
))
3459 (if (or (eq c-lit-type
'c
)
3460 (and (eq c-lit-type
'c
++)
3462 (skip-chars-forward " \t")
3464 (1- (cdr (setq c-lit-limits
3465 (c-collect-line-comments c-lit-limits
))))))
3466 (and (or (not (looking-at "\\s *$"))
3467 (eq (char-before) ?
\\))
3468 (c-query-and-set-macro-start)
3470 (goto-char c-macro-start
)
3471 (if (looking-at c-opt-cpp-start
)
3472 (goto-char (match-end 0)))
3475 (let ((comment-multi-line t
)
3477 (c-indent-new-comment-line nil t
))
3478 (delete-horizontal-space)
3480 ;; c-indent-line may look at the current indentation, so let's
3481 ;; start out with the same indentation as the previous line.
3482 (let ((col (save-excursion
3484 (while (and (looking-at "[ \t]*\\\\?$")
3485 (= (forward-line -
1) 0)))
3486 (current-indentation))))
3488 (indent-according-to-mode))))
3490 (defun c-context-open-line ()
3491 "Insert a line break suitable to the context and leave point before it.
3492 This is the `c-context-line-break' equivalent to `open-line', which is
3493 normally bound to C-o. See `c-context-line-break' for the details."
3495 (let ((here (point)))
3498 ;; Temporarily insert a non-whitespace char to keep any
3499 ;; preceding whitespace intact.
3501 (c-context-line-break))
3506 (cc-provide 'cc-cmds
)
3508 ;;; cc-cmds.el ends here