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 (c-newline-and-indent)
502 ;; Set markers around the newline and indention inserted
503 ;; above. We insert the start marker here and not before
504 ;; the call to kludge around a misfeature in expand-abbrev:
505 ;; If the line contains e.g. "else" then expand-abbrev will
506 ;; be called when c-newline-and-indent inserts the newline.
507 ;; That function first removes the abbrev "else" and then
508 ;; inserts the expansion, which is an identical "else" in
509 ;; this case. So the marker that we put after "else" would
511 (setq delete-temp-newline
512 (cons (copy-marker (c-point 'eopl
) t
)
516 (if (eq last-command-char ?
{)
517 (setq c-state-cache
(cons (point) c-state-cache
)))
518 (self-insert-command (prefix-numeric-value arg
))
519 (c-save-buffer-state ((c-syntactic-indentation-in-macros t
)
520 (c-auto-newline-analysis t
))
521 ;; Turn on syntactic macro analysis to help with auto
523 (setq syntax
(c-guess-basic-syntax)))
529 ;; Substitute inexpr-class and class-open or
530 ;; class-close with inexpr-class-open or
531 ;; inexpr-class-close.
532 (if (assq 'inexpr-class syntax
)
533 (cond ((assq 'class-open syntax
)
534 '((inexpr-class-open)))
535 ((assq 'class-close syntax
)
536 '((inexpr-class-close)))
539 c-hanging-braces-alist
)
540 '(ignore before after
))))
541 ;; Do not try to insert newlines around a special
542 ;; (Pike-style) brace list.
543 (if (and c-special-brace-lists
545 (c-save-buffer-state nil
546 (c-safe (if (= (char-before) ?
{)
549 (c-looking-at-special-brace-list)))))
551 ;; If syntax is a function symbol, then call it using the
552 ;; defined semantics.
553 (if (and (not (consp (cdr newlines
)))
554 (functionp (cdr newlines
)))
555 (let ((c-syntactic-context syntax
))
557 (funcall (cdr newlines
)
560 ;; does a newline go before the open brace?
561 (when (memq 'before newlines
)
562 ;; we leave the newline we've put in there before,
563 ;; but we need to re-indent the line above
564 (when delete-temp-newline
565 (set-marker (car delete-temp-newline
) nil
)
566 (set-marker (cdr delete-temp-newline
) nil
)
567 (setq delete-temp-newline nil
))
568 (when c-syntactic-indentation
569 (let ((pos (- (point-max) (point)))
572 (indent-according-to-mode)
573 (goto-char (- (point-max) pos
))
574 ;; if the buffer has changed due to the
575 ;; indentation, we need to recalculate syntax for
577 (if (/= (point) here
)
579 ((c-syntactic-indentation-in-macros t
)
580 (c-auto-newline-analysis t
))
581 ;; Turn on syntactic macro analysis to help
582 ;; with auto newlines only.
583 (setq syntax
(c-guess-basic-syntax))))))))
584 ;; must remove the newline we just stuck in (if we really did it)
585 (when delete-temp-newline
587 (delete-region (car delete-temp-newline
)
588 (cdr delete-temp-newline
))
589 (goto-char (car delete-temp-newline
))
590 (set-marker (car delete-temp-newline
) nil
)
591 (set-marker (cdr delete-temp-newline
) nil
)
592 ;; if there is whitespace before point, then preserve
593 ;; at least one space.
597 (if (not (memq 'before newlines
))
598 ;; since we're hanging the brace, we need to recalculate
600 (c-save-buffer-state ((c-syntactic-indentation-in-macros t
)
601 (c-auto-newline-analysis t
))
602 ;; Turn on syntactic macro analysis to help with auto
604 (setq syntax
(c-guess-basic-syntax))))
605 (when c-syntactic-indentation
606 ;; Now adjust the line's indentation. Don't update the state
607 ;; cache since c-guess-basic-syntax isn't called when
608 ;; c-syntactic-context is set.
609 (let* ((c-syntactic-context syntax
))
610 (indent-according-to-mode)))
611 ;; Do all appropriate clean ups
613 (pos (- (point-max) (point)))
615 ;; clean up empty defun braces
616 (if (and c-auto-newline
617 (memq 'empty-defun-braces c-cleanup-list
)
618 (eq last-command-char ?\
})
619 (c-intersect-lists '(defun-close class-close inline-close
)
624 (eq (char-before) ?\
{))
625 ;; make sure matching open brace isn't in a comment
626 (not (c-in-literal)))
627 (delete-region (point) (1- here
)))
628 ;; clean up brace-else-brace and brace-elseif-brace
629 (when (and c-auto-newline
630 (eq last-command-char ?\
{))
632 ((and (memq 'brace-else-brace c-cleanup-list
)
635 "\\([ \t\n]\\|\\\\\n\\)*"
637 "\\([ \t\n]\\|\\\\\n\\)*"
641 (setq mbeg
(match-beginning 0)
643 (eq (match-end 0) here
)))
644 (delete-region mbeg mend
)
645 (insert-and-inherit "} else {"))
646 ((and (memq 'brace-elseif-brace c-cleanup-list
)
648 (goto-char (1- here
))
652 (eq (char-before) ?\
)))
653 (zerop (c-save-buffer-state nil
(c-backward-token-2 1 t
)))
654 (eq (char-after) ?\
()
659 "\\([ \t\n]\\|\\\\\n\\)*"
661 "\\([ \t\n]\\|\\\\\n\\)+"
663 "\\([ \t\n]\\|\\\\\n\\)*")
665 (eq (match-end 0) tmp
))
666 (delete-region mbeg mend
)
669 (goto-char (- (point-max) pos
))
671 ;; does a newline go after the brace?
672 (if (memq 'after newlines
)
673 (c-newline-and-indent))
676 (and (eq last-command-char ?\
})
677 (not executing-kbd-macro
)
680 (c-save-buffer-state nil
681 (c-backward-syntactic-ws safepos
))
682 (funcall old-blink-paren
)))))
684 (defun c-electric-slash (arg)
685 "Insert a slash character.
687 Indent the line as a comment, if:
689 1. The slash is second of a `//' line oriented comment introducing
690 token and we are on a comment-only-line, or
692 2. The slash is part of a `*/' token that closes a block oriented
695 If a numeric ARG is supplied, point is inside a literal, or
696 `c-syntactic-indentation' is nil, indentation is inhibited."
698 (let* ((ch (char-before))
699 (literal (c-in-literal))
700 (indentp (and c-syntactic-indentation
702 (eq last-command-char ?
/)
709 (c-echo-syntactic-information-p nil
))
710 (self-insert-command (prefix-numeric-value arg
))
712 (indent-according-to-mode))))
714 (defun c-electric-star (arg)
715 "Insert a star character.
716 If the star is the second character of a C style comment introducing
717 construct, and we are on a comment-only-line, indent line as comment.
718 If a numeric ARG is supplied, point is inside a literal, or
719 `c-syntactic-indentation' is nil, indentation is inhibited."
721 (self-insert-command (prefix-numeric-value arg
))
722 ;; if we are in a literal, or if arg is given do not re-indent the
723 ;; current line, unless this star introduces a comment-only line.
724 (if (and c-syntactic-indentation
726 (eq (c-in-literal) 'c
)
727 (eq (char-before) ?
*)
730 (skip-chars-backward "*")
731 (if (eq (char-before) ?
/)
733 (skip-chars-backward " \t")
735 (let (c-echo-syntactic-information-p) ; shut this up
736 (indent-according-to-mode))
739 (defun c-electric-semi&comma
(arg)
740 "Insert a comma or semicolon.
741 When the auto-newline feature is turned on, as evidenced by the \"/a\"
742 or \"/ah\" string on the mode line, a newline might be inserted. See
743 the variable `c-hanging-semi&comma-criteria' for how newline insertion
746 When a semicolon is inserted, the line is re-indented unless a numeric
747 arg is supplied, point is inside a literal, or
748 `c-syntactic-indentation' is nil.
750 Based on the value of `c-cleanup-list', this function cleans up commas
751 following brace lists and semicolons following defuns."
753 (let* ((lim (c-most-enclosing-brace (c-parse-state)))
754 (literal (c-in-literal lim
))
757 (c-echo-syntactic-information-p nil
))
759 (self-insert-command (prefix-numeric-value arg
))
760 ;; do some special stuff with the character
761 (self-insert-command (prefix-numeric-value arg
))
762 ;; do all cleanups and newline insertions if c-auto-newline is
764 (if (or (not c-auto-newline
)
765 (not (looking-at "[ \t]*\\\\?$")))
766 (if c-syntactic-indentation
767 (indent-according-to-mode))
769 (let ((pos (- (point-max) (point))))
771 (eq last-command-char ?
,)
772 (memq 'list-close-comma c-cleanup-list
))
774 (eq last-command-char ?\
;)
775 (memq 'defun-close-semi c-cleanup-list
)))
779 (eq (char-before) ?
}))
780 ;; make sure matching open brace isn't in a comment
781 (not (c-in-literal lim
)))
782 (delete-region (point) here
))
783 (goto-char (- (point-max) pos
)))
785 (if c-syntactic-indentation
786 (indent-according-to-mode))
787 ;; check to see if a newline should be added
788 (let ((criteria c-hanging-semi
&comma-criteria
)
789 answer add-newline-p
)
791 (setq answer
(funcall (car criteria
)))
792 ;; only nil value means continue checking
794 (setq criteria
(cdr criteria
))
796 ;; only 'stop specifically says do not add a newline
797 (setq add-newline-p
(not (eq answer
'stop
)))
800 (c-newline-and-indent))
803 (defun c-electric-colon (arg)
806 If the auto-newline feature is turned on, as evidenced by the \"/a\"
807 or \"/ah\" string on the mode line, newlines are inserted before and
808 after colons based on the value of `c-hanging-colons-alist'.
810 Also, the line is re-indented unless a numeric ARG is supplied, the
811 colon is inserted inside a literal, or `c-syntactic-indentation' is
814 This function cleans up double colon scope operators based on the
815 value of `c-cleanup-list'."
817 (let* ((bod (c-point 'bod
))
818 (literal (c-in-literal bod
))
821 (c-echo-syntactic-information-p nil
))
824 (self-insert-command (prefix-numeric-value arg
)))
825 ((not (looking-at "[ \t]*\\\\?$"))
826 (self-insert-command (prefix-numeric-value arg
))
827 (if c-syntactic-indentation
828 (indent-according-to-mode)))
830 ;; insert the colon, then do any specified cleanups
831 (self-insert-command (prefix-numeric-value arg
))
832 (let ((pos (- (point-max) (point)))
834 (if (and c-auto-newline
835 (memq 'scope-operator c-cleanup-list
)
836 (eq (char-before) ?
:)
840 (eq (char-before) ?
:))
842 (not (eq (char-after (- (point) 2)) ?
:)))
844 (delete-region (point) (1- here
))
845 (setq is-scope-op t
)))
846 (goto-char (- (point-max) pos
)))
847 ;; indent the current line if it's done syntactically.
848 (if c-syntactic-indentation
849 ;; Cannot use the same syntax analysis as we find below,
850 ;; since that's made with c-syntactic-indentation-in-macros
852 (indent-according-to-mode))
854 ((c-syntactic-indentation-in-macros t
)
855 (c-auto-newline-analysis t
)
856 ;; Turn on syntactic macro analysis to help with auto newlines
858 (syntax (c-guess-basic-syntax))
860 ;; Translate substatement-label to label for this operation.
862 (if (eq (car (car elem
)) 'substatement-label
)
863 (setcar (car elem
) 'label
))
864 (setq elem
(cdr elem
)))
865 ;; some language elements can only be determined by checking
866 ;; the following line. Lets first look for ones that can be
867 ;; found when looking on the line with the colon
870 (or (c-lookup-lists '(case-label label access-label
)
871 syntax c-hanging-colons-alist
)
872 (c-lookup-lists '(member-init-intro inher-intro
)
876 (c-guess-basic-syntax)
878 c-hanging-colons-alist
)))))
879 ;; does a newline go before the colon? Watch out for already
880 ;; non-hung colons. However, we don't unhang them because that
881 ;; would be a cleanup (and anti-social).
882 (if (and (memq 'before newlines
)
885 (skip-chars-backward ": \t")
887 (let ((pos (- (point-max) (point))))
889 (c-newline-and-indent)
890 (goto-char (- (point-max) pos
))))
891 ;; does a newline go after the colon?
892 (if (and (memq 'after
(cdr-safe newlines
))
894 (c-newline-and-indent))
897 (defun c-electric-lt-gt (arg)
898 "Insert a less-than, or greater-than character.
899 The line will be re-indented if the character inserted is the second
900 of a C++ style stream operator and the buffer is in C++ mode.
901 Exceptions are when a numeric argument is supplied, point is inside a
902 literal, or `c-syntactic-indentation' is nil, in which case the line
903 will not be re-indented."
905 (let ((indentp (and c-syntactic-indentation
907 (eq (char-before) last-command-char
)
908 (not (c-in-literal))))
910 (c-echo-syntactic-information-p nil
))
911 (self-insert-command (prefix-numeric-value arg
))
913 (indent-according-to-mode))))
915 (defun c-electric-paren (arg)
916 "Insert a parenthesis.
918 Some newline cleanups are done if appropriate; see the variable
921 Also, the line is re-indented unless a numeric ARG is supplied, the
922 parenthesis is inserted inside a literal, or `c-syntactic-indentation'
925 (let ((literal (c-in-literal (c-point 'bod
)))
927 (c-echo-syntactic-information-p nil
))
929 (self-insert-command (prefix-numeric-value arg
))
930 ;; do some special stuff with the character
931 (let* (;; We want to inhibit blinking the paren since this will
932 ;; be most disruptive. We'll blink it ourselves
934 (old-blink-paren blink-paren-function
)
935 blink-paren-function
)
936 (self-insert-command (prefix-numeric-value arg
))
937 (if c-syntactic-indentation
938 (indent-according-to-mode))
939 (when (looking-at "[ \t]*\\\\?$")
941 ;; Do all appropriate clean ups
943 (pos (- (point-max) (point)))
945 ;; clean up brace-elseif-brace
946 (if (and (memq 'brace-elseif-brace c-cleanup-list
)
947 (eq last-command-char ?\
()
950 "\\([ \t\n]\\|\\\\\n\\)*"
952 "\\([ \t\n]\\|\\\\\n\\)+"
954 "\\([ \t\n]\\|\\\\\n\\)*"
958 (setq mbeg
(match-beginning 0)
961 (not (c-in-literal)))
963 (delete-region mbeg mend
)
964 (insert-and-inherit "} else if ("))
965 ;; clean up brace-catch-brace
967 (if (and (memq 'brace-catch-brace c-cleanup-list
)
968 (eq last-command-char ?\
()
971 "\\([ \t\n]\\|\\\\\n\\)*"
973 "\\([ \t\n]\\|\\\\\n\\)*"
977 (setq mbeg
(match-beginning 0)
980 (not (c-in-literal)))
982 (delete-region mbeg mend
)
983 (insert-and-inherit "} catch ("))))
984 (goto-char (- (point-max) pos
))
986 (let (beg (end (1- (point))))
987 (cond ((and (memq 'space-before-funcall c-cleanup-list
)
988 (eq last-command-char ?\
()
991 (skip-chars-backward " \t")
995 (delete-region beg end
)
998 ((and (memq 'compact-empty-funcall c-cleanup-list
)
999 (eq last-command-char ?\
))
1001 (c-safe (backward-char 2))
1002 (when (looking-at "()")
1004 (skip-chars-backward " \t")
1006 (c-on-identifier))))
1007 (delete-region beg end
))))
1008 (and (not executing-kbd-macro
)
1010 (funcall old-blink-paren
))))))
1012 (defun c-electric-continued-statement ()
1013 "Reindent the current line if appropriate.
1015 This function is used to reindent the line after a keyword which
1016 continues an earlier statement is typed, e.g. an \"else\" or the
1017 \"while\" in a do-while block.
1019 The line is reindented if there is nothing but whitespace before the
1020 keyword on the line, the keyword is not inserted inside a literal, and
1021 `c-syntactic-indentation' is non-nil."
1022 (let (;; shut this up
1023 (c-echo-syntactic-information-p nil
))
1024 (when (and c-syntactic-indentation
1025 (not (eq last-command-char ?_
))
1027 (skip-syntax-backward "w")
1030 (not (c-in-literal (c-point 'bod
))))
1031 ;; Have to temporarily insert a space so that
1032 ;; c-guess-basic-syntax recognizes the keyword. Follow the
1033 ;; space with a nonspace to avoid messing up any whitespace
1034 ;; sensitive meddling that might be done, e.g. by
1035 ;; `c-backslash-region'.
1036 (insert-and-inherit " x")
1038 (indent-according-to-mode)
1039 (delete-char -
2)))))
1042 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus
1043 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com
1044 (defun c-forward-into-nomenclature (&optional arg
)
1045 "Move forward to end of a nomenclature section or word.
1046 With arg, do it arg times."
1048 (let ((case-fold-search nil
))
1051 (cc-eval-when-compile
1052 (concat "\\W*\\([" c-upper
"]*[" c-lower c-digit
"]*\\)"))
1054 (while (and (< arg
0)
1056 (cc-eval-when-compile
1058 "\\(\\(\\W\\|[" c-lower c-digit
"]\\)[" c-upper
"]+"
1062 (setq arg
(1+ arg
)))))
1063 (c-keep-region-active))
1065 (defun c-backward-into-nomenclature (&optional arg
)
1066 "Move backward to beginning of a nomenclature section or word.
1067 With optional ARG, move that many times. If ARG is negative, move
1070 (c-forward-into-nomenclature (- arg
))
1071 (c-keep-region-active))
1073 (defun c-scope-operator ()
1074 "Insert a double colon scope operator at point.
1075 No indentation or other \"electric\" behavior is performed."
1077 (insert-and-inherit "::"))
1079 (defun c-beginning-of-defun (&optional arg
)
1080 "Move backward to the beginning of a defun.
1081 Every top level declaration that contains a brace paren block is
1082 considered to be a defun.
1084 With a positive argument, move backward that many defuns. A negative
1085 argument -N means move forward to the Nth following beginning. Return
1086 t unless search stops due to beginning or end of buffer.
1088 Unlike the built-in `beginning-of-defun' this tries to be smarter
1089 about finding the char with open-parenthesis syntax that starts the
1093 (or arg
(setq arg
1))
1096 (when (c-end-of-defun (- arg
))
1097 (c-save-buffer-state nil
(c-forward-syntactic-ws))
1100 (c-save-buffer-state (paren-state lim pos
)
1103 ;; Note: Partial code duplication in `c-end-of-defun' and
1104 ;; `c-declaration-limits'.
1106 (setq paren-state
(c-parse-state))
1108 (goto-char (c-least-enclosing-brace paren-state
))
1109 ;; If we moved to the outermost enclosing paren
1110 ;; then we can use c-safe-position to set the
1111 ;; limit. Can't do that otherwise since the
1112 ;; earlier paren pair on paren-state might very
1113 ;; well be part of the declaration we should go
1115 (setq lim
(c-safe-position (point) paren-state
))
1117 ;; At top level. Make sure we aren't inside a literal.
1118 (setq pos
(c-literal-limits
1119 (c-safe-position (point) paren-state
)))
1120 (if pos
(goto-char (car pos
))))
1122 (while (let ((start (point)))
1123 (c-beginning-of-decl-1 lim
)
1124 (if (= (point) start
)
1125 ;; Didn't move. Might be due to bob or unbalanced
1126 ;; parens. Try to continue if it's the latter.
1127 (unless (c-safe (goto-char
1128 (c-down-list-backward (point))))
1129 ;; Didn't work, so it's bob then.
1130 (goto-char (point-min))
1134 ;; Check if the declaration contains a brace
1135 ;; block. If not, we try another one.
1137 (not (and (c-syntactic-re-search-forward "[;{]" nil t t
)
1138 (or (eq (char-before) ?
{)
1139 (and c-recognize-knr-p
1140 ;; Might have stopped on the
1141 ;; ';' in a K&R argdecl. In
1142 ;; that case the declaration
1143 ;; should contain a block.
1144 (c-in-knr-argdecl pos
)))))))
1147 ;; Check if `c-beginning-of-decl-1' put us after the block
1148 ;; in a declaration that doesn't end there. We're searching
1149 ;; back and forth over the block here, which can be
1152 (if (and c-opt-block-decls-with-vars-key
1154 (c-backward-syntactic-ws)
1155 (eq (char-before) ?
}))
1156 (eq (car (c-beginning-of-decl-1))
1165 ;; Try to be line oriented; position point at the closest
1166 ;; preceding boi that isn't inside a comment, but if we hit
1167 ;; the previous declaration then we use the current point
1169 (while (and (/= (point) (c-point 'boi
))
1170 (c-backward-single-comment)))
1171 (if (/= (point) (c-point 'boi
))
1174 (setq arg
(1- arg
)))))
1175 (c-keep-region-active)
1178 (defun c-end-of-defun (&optional arg
)
1179 "Move forward to the end of a top level declaration.
1180 With argument, do it that many times. Negative argument -N means move
1181 back to Nth preceding end. Returns t unless search stops due to
1182 beginning or end of buffer.
1184 An end of a defun occurs right after the close-parenthesis that matches
1185 the open-parenthesis that starts a defun; see `beginning-of-defun'."
1188 (or arg
(setq arg
1))
1191 (when (c-beginning-of-defun (- arg
))
1192 (c-save-buffer-state nil
(c-backward-syntactic-ws))
1195 (c-save-buffer-state (paren-state lim pos
)
1198 ;; Note: Partial code duplication in `c-beginning-of-defun'
1199 ;; and `c-declaration-limits'.
1201 (setq paren-state
(c-parse-state))
1203 (goto-char (c-least-enclosing-brace paren-state
))
1204 ;; If we moved to the outermost enclosing paren
1205 ;; then we can use c-safe-position to set the
1206 ;; limit. Can't do that otherwise since the
1207 ;; earlier paren pair on paren-state might very
1208 ;; well be part of the declaration we should go
1210 (setq lim
(c-safe-position (point) paren-state
))
1212 ;; At top level. Make sure we aren't inside a literal.
1213 (setq pos
(car-safe (c-literal-limits
1214 (c-safe-position (point) paren-state
))))
1215 (if pos
(goto-char pos
)))
1217 ;; Have to move to the start first so that `c-end-of-decl-1'
1218 ;; has the correct start position.
1220 (when (memq (car (c-beginning-of-decl-1 lim
))
1222 ;; We moved back over the previous defun or a macro. Move
1223 ;; to the next token; it's the start of the next
1224 ;; declaration. We can also be directly after the block
1225 ;; in a `c-opt-block-decls-with-vars-key' declaration, but
1226 ;; then we won't move significantly far here.
1228 (c-forward-token-2 0))
1230 (while (let ((start (point)))
1232 (if (= (point) start
)
1233 ;; Didn't move. Might be due to eob or unbalanced
1234 ;; parens. Try to continue if it's the latter.
1235 (if (c-safe (goto-char (c-up-list-forward (point))))
1237 ;; Didn't work, so it's eob then.
1238 (goto-char (point-max))
1242 ;; Check if the declaration contains a brace
1243 ;; block. If not, we try another one.
1246 (not (c-syntactic-re-search-forward "{" pos t t
))))))
1249 ;; Try to be line oriented; position point after the next
1250 ;; newline that isn't inside a comment, but if we hit the
1251 ;; next declaration then we use the current point instead.
1252 (while (and (not (bolp))
1253 (not (looking-at "\\s *$"))
1254 (c-forward-single-comment)))
1256 ((looking-at "\\s *$")
1261 (setq arg
(1- arg
)))))
1262 (c-keep-region-active)
1265 (defun c-declaration-limits (near)
1266 ;; Return a cons of the beginning and end positions of the current
1267 ;; top level declaration or macro. If point is not inside any then
1268 ;; nil is returned, unless NEAR is non-nil in which case the closest
1269 ;; following one is chosen instead (if there is any). The end
1270 ;; position is at the next line, providing there is one before the
1274 ;; Note: Some code duplication in `c-beginning-of-defun' and
1275 ;; `c-end-of-defun'.
1277 (let ((start (point))
1278 (paren-state (c-parse-state))
1281 (goto-char (c-least-enclosing-brace paren-state
))
1282 ;; If we moved to the outermost enclosing paren then we
1283 ;; can use c-safe-position to set the limit. Can't do
1284 ;; that otherwise since the earlier paren pair on
1285 ;; paren-state might very well be part of the
1286 ;; declaration we should go to.
1287 (setq lim
(c-safe-position (point) paren-state
))
1289 ;; At top level. Make sure we aren't inside a literal.
1290 (setq pos
(c-literal-limits
1291 (c-safe-position (point) paren-state
)))
1292 (if pos
(goto-char (car pos
))))
1294 (when (c-beginning-of-macro)
1303 (when (or (eq (car (c-beginning-of-decl-1 lim
)) 'previous
)
1305 ;; We moved back over the previous defun. Skip to the next
1306 ;; one. Not using c-forward-syntactic-ws here since we
1307 ;; should not skip a macro. We can also be directly after
1308 ;; the block in a `c-opt-block-decls-with-vars-key'
1309 ;; declaration, but then we won't move significantly far
1312 (c-forward-comments)
1314 (when (and near
(c-beginning-of-macro))
1322 (if (eobp) (throw 'exit nil
))
1324 ;; Check if `c-beginning-of-decl-1' put us after the block in a
1325 ;; declaration that doesn't end there. We're searching back and
1326 ;; forth over the block here, which can be expensive.
1328 (if (and c-opt-block-decls-with-vars-key
1330 (c-backward-syntactic-ws)
1331 (eq (char-before) ?
}))
1332 (eq (car (c-beginning-of-decl-1))
1336 (and (> (point) pos
)
1337 (setq end-pos
(point)))))
1341 (if (and (not near
) (> (point) start
))
1344 ;; Try to be line oriented; position the limits at the
1345 ;; closest preceding boi, and after the next newline, that
1346 ;; isn't inside a comment, but if we hit a neighboring
1347 ;; declaration then we instead use the exact declaration
1348 ;; limit in that direction.
1351 (while (and (/= (point) (c-point 'boi
))
1352 (c-backward-single-comment)))
1353 (if (/= (point) (c-point 'boi
))
1361 (while (and (not (bolp))
1362 (not (looking-at "\\s *$"))
1363 (c-forward-single-comment)))
1366 ((looking-at "\\s *$")
1373 (defun c-mark-function ()
1374 "Put mark at end of the current top-level declaration or macro, point at beginning.
1375 If point is not inside any then the closest following one is chosen.
1377 As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
1378 function does not require the declaration to contain a brace block."
1382 (c-save-buffer-state nil
1383 ;; We try to be line oriented, unless there are several
1384 ;; declarations on the same line.
1385 (if (looking-at c-syntactic-eol
)
1386 (c-backward-token-2 1 nil
(c-point 'bol
)))
1387 (setq decl-limits
(c-declaration-limits t
)))
1389 (if (not decl-limits
)
1390 (error "Cannot find any declaration")
1391 (goto-char (car decl-limits
))
1392 (push-mark (cdr decl-limits
) nil t
))))
1395 (defun c-beginning-of-statement (&optional count lim sentence-flag
)
1396 "Go to the beginning of the innermost C statement.
1397 With prefix arg, go back N - 1 statements. If already at the
1398 beginning of a statement then go to the beginning of the closest
1399 preceding one, moving into nested blocks if necessary (use
1400 \\[backward-sexp] to skip over a block). If within or next to a
1401 comment or multiline string, move by sentences instead of statements.
1403 When called from a program, this function takes 3 optional args: the
1404 repetition count, a buffer position limit which is the farthest back
1405 to search for the syntactic context, and a flag saying whether to do
1406 sentence motion in or near comments and multiline strings.
1408 Note that `c-beginning-of-statement-1' is usually better to use from
1409 programs. It has much more well defined semantics than this one,
1410 which is intended for interactive use and might therefore change to be
1412 (interactive (list (prefix-numeric-value current-prefix-arg
)
1414 (c-save-buffer-state
1415 ((count (or count
1))
1417 (range (c-collect-line-comments (c-literal-limits lim
))))
1418 (while (and (/= count
0)
1419 (or (not lim
) (> (point) lim
)))
1421 (if (and (not range
) sentence-flag
)
1423 ;; Find the comment next to point if we're not in one.
1425 (if (c-backward-single-comment)
1426 (setq range
(cons (point)
1427 (progn (c-forward-single-comment)
1429 (c-skip-ws-backward)
1430 (setq range
(point))
1432 (if (eq (char-before) ?
\")
1433 (c-safe (c-backward-sexp 1)
1434 (cons (point) range
)))))
1436 (if (eq (char-after) ?
\")
1437 (setq range
(cons (point)
1441 (setq range
(point))
1442 (setq range
(if (c-forward-single-comment)
1443 (cons range
(point))
1445 (setq range
(c-collect-line-comments range
))))
1446 (if (and (< count
0) (= here
(point-max)))
1447 ;; Special case because eob might be in a literal.
1450 (if (and sentence-flag
1451 (or (/= (char-syntax (char-after (car range
))) ?
\")
1452 ;; Only visit a string if it spans more than one line.
1454 (goto-char (car range
))
1455 (skip-chars-forward "^\n" (cdr range
))
1456 (< (point) (cdr range
)))))
1457 (let* ((lit-type (c-literal-type range
))
1458 (line-prefix (concat "[ \t]*\\("
1459 c-current-comment-prefix
1461 (beg (if (eq lit-type
'string
)
1464 (goto-char (car range
))
1466 (looking-at comment-start-skip
)
1469 (looking-at line-prefix
)
1471 (end (- (cdr range
) (if (eq lit-type
'c
) 2 1)))
1472 (beg-of-para (if (eq lit-type
'string
)
1476 (if (looking-at line-prefix
)
1477 (goto-char (match-end 0)))))))
1479 ;; Move by sentence, but not past the limit of the
1480 ;; literal, narrowed to the appropriate
1482 (narrow-to-region (save-excursion
1483 (let ((pos (min here end
)))
1485 (forward-paragraph -
1)
1486 (if (looking-at paragraph-separate
)
1488 (when (> (point) beg
)
1489 (funcall beg-of-para
)
1490 (when (>= (point) pos
)
1491 (forward-paragraph -
2)
1492 (funcall beg-of-para
)))
1495 (c-safe (forward-sentence (if (< count
0) 1 -
1)))
1496 (if (and (memq lit-type
'(c c
++))
1497 ;; Check if we stopped due to a comment
1498 ;; prefix and not a sentence end.
1499 (/= (point) (point-min))
1500 (/= (point) (point-max))
1503 (looking-at line-prefix
))
1504 (>= (point) (match-beginning 0))
1505 (/= (match-beginning 1) (match-end 1))
1506 (or (< (point) (match-end 0))
1508 (= (point) (match-end 0))
1509 ;; The comment prefix may contain
1510 ;; characters that is regarded as end
1515 (forward-paragraph -
1)
1516 (< (point) (match-beginning 0)))
1519 (or (not (re-search-backward
1524 (c-point 'eol
)))))))))
1525 (setq count
(+ count
(if (< count
0) -
1 1)))
1528 ;; In block comments, if there's only
1529 ;; horizontal ws between the text and the
1530 ;; comment ender, stop before it. Stop after
1531 ;; the ender if there's either nothing or
1532 ;; newlines between.
1533 (when (and (eq lit-type
'c
)
1534 (eq (point) (point-max)))
1536 (when (or (= (skip-chars-backward " \t") 0)
1537 (eq (point) (point-max))
1539 (goto-char (cdr range
)))))
1540 (when (and (eq (point) (point-min))
1541 (looking-at "[ \t]*\\\\?$"))
1542 ;; Stop before instead of after the comment
1543 ;; starter if nothing follows it.
1545 (goto-char (car range
))
1546 (if (and (eq lit-type
'string
) (/= (point) here
))
1547 (setq count
(1+ count
)
1549 ;; See if we should escape the literal.
1551 (if (< (point) here
)
1552 (setq count
(1- count
))
1553 (goto-char (car range
))
1555 (if (> (point) here
)
1556 (setq count
(1+ count
))
1557 (goto-char (cdr range
))
1559 (goto-char (if (> count
0) (car range
) (cdr range
)))
1564 ;; Stop before `{' and after `;', `{', `}' and `};'
1565 ;; when not followed by `}' or `)', but on the other
1566 ;; side of the syntactic ws. Move by sexps and move
1567 ;; into parens. Also stop before `#' when it's at boi
1569 (let ((literal-pos (not sentence-flag
))
1570 last last-below-line
)
1574 (when (and (or (eq (char-after) ?\
{)
1575 (and (eq (char-after) ?
#)
1576 (eq (point) (c-point 'boi
)))
1579 (unless (and c-special-brace-lists
1580 (eq (char-after) ?
{)
1581 (c-looking-at-special-brace-list))
1582 (if (and (eq (char-after) ?
#)
1583 (numberp last-below-line
)
1584 (not (eq last-below-line here
)))
1585 (goto-char last-below-line
))
1587 ;; Don't know why I added the following, but it
1588 ;; doesn't work when point is preceded by a line
1589 ;; style comment. /mast
1590 ;;(c-skip-ws-backward)
1592 (c-backward-comments)
1593 (when (c-backward-single-comment)
1594 ;; Record position of first comment.
1596 (c-forward-single-comment)
1597 (setq literal-pos
(point)))
1598 (c-backward-comments)))
1599 (unless last-below-line
1601 (re-search-forward "\\(^\\|[^\\]\\)$" last t
))
1602 (setq last-below-line last
)))
1603 (cond ((bobp) ; Must handle bob specially.
1608 ((progn (backward-char)
1609 (looking-at "[;{}]"))
1610 (if (and c-special-brace-lists
1611 (eq (char-after) ?
{)
1612 (c-looking-at-special-brace-list))
1613 (skip-syntax-backward "w_") ; Speedup only.
1614 (if (or (= here last
)
1615 (memq (char-after last
) '(?\
) ?
})))
1616 (if (and (eq (char-before) ?
})
1617 (eq (char-after) ?\
;))
1621 ((= (char-syntax (char-after)) ?
\")
1622 (let ((end (point)))
1626 (skip-chars-forward "^\n" end
)
1627 (when (< (point) end
)
1628 ;; Break at multiline string.
1629 (setq literal-pos
(1+ end
))
1631 (t (skip-syntax-backward "w_")) ; Speedup only.
1633 (if (and (numberp literal-pos
)
1634 (< (point) literal-pos
))
1635 ;; We jumped over a comment or string that
1636 ;; should be investigated.
1637 (goto-char literal-pos
)
1638 (setq count
(1- count
))))
1640 (goto-char (point-min))
1643 ;; Stop before `{', `}', and `#' when it's at boi on a
1644 ;; line, but on the other side of the syntactic ws, and
1645 ;; after `;', `}' and `};'. Only stop before `{' if at
1646 ;; top level or inside braces, though. Move by sexps
1647 ;; and move into parens. Also stop at eol of lines
1648 ;; with `#' at the boi.
1649 (let ((literal-pos (not sentence-flag
))
1655 (c-forward-comments)
1658 ;; Record position of first comment.
1659 (setq literal-pos
(point))
1660 (c-forward-single-comment))
1661 (c-forward-comments)
1662 (setq literal-pos nil
)))
1663 (cond ((and (eq (char-after) ?
{)
1664 (not (and c-special-brace-lists
1665 (c-looking-at-special-brace-list)))
1668 (or (not (c-safe (up-list -
1) t
))
1669 (= (char-after) ?
{))))
1672 ((and c-special-brace-lists
1673 (eq (char-after) ?
})
1675 (and (c-safe (up-list -
1) t
)
1676 (c-looking-at-special-brace-list))))
1678 (skip-syntax-forward "w_")) ; Speedup only.
1679 ((and (eq (char-after) ?
})
1683 ; ((and (eq (char-after) ?#)
1684 ; (= (point) (c-point 'boi)))
1686 ; (or (re-search-forward "\\(^\\|[^\\]\\)$" nil t)
1687 ; (goto-char (point-max)))
1690 ((looking-at ";\\|};?")
1691 (goto-char (match-end 0))
1693 ((= (char-syntax (char-after)) ?
\")
1694 (let ((beg (point)))
1697 (skip-chars-backward "^\n" beg
)
1698 (when (> (point) beg
)
1699 ;; Break at multiline string.
1700 (setq literal-pos beg
)
1704 (skip-syntax-forward "w_")) ; Speedup only.
1706 (if (and (numberp literal-pos
)
1707 (> (point) literal-pos
))
1708 ;; We jumped over a comment that should be investigated.
1709 (goto-char literal-pos
)
1710 (setq count
(1+ count
))))
1712 (goto-char (point-max))
1715 ;; If we haven't moved we're near a buffer limit.
1716 (when (and (not (zerop count
)) (= (point) here
))
1717 (goto-char (if (> count
0) (point-min) (point-max)))
1719 (c-keep-region-active))
1721 (defun c-end-of-statement (&optional count lim sentence-flag
)
1722 "Go to the end of the innermost C statement.
1723 With prefix arg, go forward N - 1 statements. Move forward to the end
1724 of the next statement if already at end, and move into nested blocks
1725 \(use \\[forward-sexp] to skip over a block). If within or next to a
1726 comment or multiline string, move by sentences instead of statements.
1728 When called from a program, this function takes 3 optional args: the
1729 repetition count, a buffer position limit which is the farthest back
1730 to search for the syntactic context, and a flag saying whether to do
1731 sentence motion in or near comments and multiline strings."
1732 (interactive (list (prefix-numeric-value current-prefix-arg
)
1734 (c-beginning-of-statement (- (or count
1)) lim sentence-flag
)
1735 (c-keep-region-active))
1738 ;; set up electric character functions to work with pending-del,
1739 ;; (a.k.a. delsel) mode. All symbols get the t value except
1740 ;; the functions which delete, which gets 'supersede.
1744 (put sym
'delete-selection t
) ; for delsel (Emacs)
1745 (put sym
'pending-delete t
))) ; for pending-del (XEmacs)
1750 c-electric-semi
&comma
1754 (put 'c-electric-delete
'delete-selection
'supersede
) ; delsel
1755 (put 'c-electric-delete
'pending-delete
'supersede
) ; pending-del
1756 (put 'c-electric-backspace
'delete-selection
'supersede
) ; delsel
1757 (put 'c-electric-backspace
'pending-delete
'supersede
) ; pending-del
1758 (put 'c-electric-delete-forward
'delete-selection
'supersede
) ; delsel
1759 (put 'c-electric-delete-forward
'pending-delete
'supersede
) ; pending-del
1762 (defun c-calc-comment-indent (entry)
1764 (setq entry
(or (assq entry c-indent-comment-alist
)
1765 (assq 'other c-indent-comment-alist
)
1766 '(default .
(column . nil
)))))
1767 (let ((action (car (cdr entry
)))
1768 (value (cdr (cdr entry
)))
1769 (col (current-column)))
1770 (cond ((eq action
'space
)
1772 ((eq action
'column
)
1773 (unless value
(setq value comment-column
))
1775 ;; Do not pad with one space if we're at bol.
1777 (max (1+ col
) value
)))
1783 (let ((lim (c-literal-limits (c-point 'bol
) t
)))
1785 (goto-char (car lim
))
1786 (when (looking-at "/[/*]")
1787 ;; Found comment to align with.
1789 ;; Do not pad with one space if we're at bol.
1791 (max (1+ col
) (current-column))))))))
1792 ;; Recurse to handle value as a new spec.
1793 (c-calc-comment-indent (cdr entry
)))))))
1795 (defun c-comment-indent ()
1796 "Used by `indent-for-comment' to create and indent comments.
1797 See `c-indent-comment-alist' for a description."
1800 (c-save-buffer-state
1801 ((eot (let ((lim (c-literal-limits (c-point 'bol
) t
)))
1802 (or (when (consp lim
)
1803 (goto-char (car lim
))
1804 (when (looking-at "/[/*]")
1805 (skip-chars-backward " \t")
1808 (skip-chars-backward " \t")
1811 (cond ((looking-at "^/[/*]")
1813 ((progn (beginning-of-line)
1816 ((progn (back-to-indentation)
1817 (and (eq (char-after) ?
})
1818 (eq (point) (1- eot
))))
1820 ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
1821 (eq (match-end 0) eot
))
1825 (if (and (memq line-type
'(anchored-comment empty-line
))
1826 c-indent-comments-syntactically-p
)
1827 (let ((c-syntactic-context (c-guess-basic-syntax)))
1828 ;; BOGOSITY ALERT: if we're looking at the eol, its
1829 ;; because indent-for-comment hasn't put the comment-start
1830 ;; in the buffer yet. this will screw up the syntactic
1831 ;; analysis so we kludge in the necessary info. Another
1832 ;; kludge is that if we're at the bol, then we really want
1833 ;; to ignore any anchoring as specified by
1834 ;; c-comment-only-line-offset since it doesn't apply here.
1836 (c-add-syntax 'comment-intro
))
1837 (let ((c-comment-only-line-offset
1838 (if (consp c-comment-only-line-offset
)
1839 c-comment-only-line-offset
1840 (cons c-comment-only-line-offset
1841 c-comment-only-line-offset
))))
1842 (c-get-syntactic-indentation c-syntactic-context
)))
1844 (c-calc-comment-indent line-type
)))))
1847 ;; used by outline-minor-mode
1848 (defun c-outline-level ()
1849 (let (buffer-invisibility-spec);; This so that `current-column' DTRT
1850 ;; in otherwise-hidden text.
1852 (skip-chars-forward "\t ")
1856 (defun c-up-conditional (count)
1857 "Move back to the containing preprocessor conditional, leaving mark behind.
1858 A prefix argument acts as a repeat count. With a negative argument,
1859 move forward to the end of the containing preprocessor conditional.
1861 `#elif' is treated like `#else' followed by `#if', so the function
1862 stops at them when going backward, but not when going forward."
1864 (c-forward-conditional (- count
) -
1)
1865 (c-keep-region-active))
1867 (defun c-up-conditional-with-else (count)
1868 "Move back to the containing preprocessor conditional, including `#else'.
1869 Just like `c-up-conditional', except it also stops at `#else'
1872 (c-forward-conditional (- count
) -
1 t
)
1873 (c-keep-region-active))
1875 (defun c-down-conditional (count)
1876 "Move forward into the next preprocessor conditional, leaving mark behind.
1877 A prefix argument acts as a repeat count. With a negative argument,
1878 move backward into the previous preprocessor conditional.
1880 `#elif' is treated like `#else' followed by `#if', so the function
1881 stops at them when going forward, but not when going backward."
1883 (c-forward-conditional count
1)
1884 (c-keep-region-active))
1886 (defun c-down-conditional-with-else (count)
1887 "Move forward into the next preprocessor conditional, including `#else'.
1888 Just like `c-down-conditional', except it also stops at `#else'
1891 (c-forward-conditional count
1 t
)
1892 (c-keep-region-active))
1894 (defun c-backward-conditional (count &optional target-depth with-else
)
1895 "Move back across a preprocessor conditional, leaving mark behind.
1896 A prefix argument acts as a repeat count. With a negative argument,
1897 move forward across a preprocessor conditional."
1899 (c-forward-conditional (- count
) target-depth with-else
)
1900 (c-keep-region-active))
1902 (defun c-forward-conditional (count &optional target-depth with-else
)
1903 "Move forward across a preprocessor conditional, leaving mark behind.
1904 A prefix argument acts as a repeat count. With a negative argument,
1905 move backward across a preprocessor conditional.
1907 `#elif' is treated like `#else' followed by `#if', except that the
1908 nesting level isn't changed when tracking subconditionals.
1910 The optional argument TARGET-DEPTH specifies the wanted nesting depth
1911 after each scan. I.e. if TARGET-DEPTH is -1, the function will move
1912 out of the enclosing conditional. A non-integer non-nil TARGET-DEPTH
1915 If the optional argument WITH-ELSE is non-nil, `#else' directives are
1916 treated as conditional clause limits. Normally they are ignored."
1918 (let* ((forward (> count
0))
1919 (increment (if forward -
1 1))
1920 (search-function (if forward
're-search-forward
're-search-backward
))
1922 (unless (integerp target-depth
)
1923 (setq target-depth
(if target-depth -
1 0)))
1927 ;; subdepth is the depth in "uninteresting" subtrees,
1928 ;; i.e. those that takes us farther from the target
1929 ;; depth instead of closer.
1933 ;; Find the "next" significant line in the proper direction.
1934 (while (and (not found
)
1935 ;; Rather than searching for a # sign that
1936 ;; comes at the beginning of a line aside from
1937 ;; whitespace, search first for a string
1938 ;; starting with # sign. Then verify what
1939 ;; precedes it. This is faster on account of
1940 ;; the fastmap feature of the regexp matcher.
1941 (funcall search-function
1942 "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
1945 ;; Now verify it is really a preproc line.
1946 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
1947 (let (dchange (directive (match-string 1)))
1948 (cond ((string= directive
"if")
1949 (setq dchange
(- increment
)))
1950 ((string= directive
"endif")
1951 (setq dchange increment
))
1953 ;; When we're not in an "uninteresting"
1954 ;; subtree, we might want to act on "elif"
1956 (if (cond (with-else
1957 ;; Always move toward the target depth.
1959 (if (> target-depth
0) 1 -
1)))
1960 ((string= directive
"elif")
1961 (setq dchange
(- increment
))))
1962 ;; Ignore the change if it'd take us
1963 ;; into an "uninteresting" subtree.
1964 (if (eq (> dchange
0) (<= target-depth
0))
1965 (setq dchange nil
)))))
1967 (when (or (/= subdepth
0)
1968 (eq (> dchange
0) (<= target-depth
0)))
1969 (setq subdepth
(+ subdepth dchange
)))
1970 (setq depth
(+ depth dchange
))
1971 ;; If we are trying to move across, and we find an
1972 ;; end before we find a beginning, get an error.
1973 (if (and (< depth target-depth
) (< dchange
0))
1975 "No following conditional at this level"
1976 "No previous conditional at this level"))))
1977 ;; When searching forward, start from next line so
1978 ;; that we don't find the same line again.
1979 (if forward
(forward-line 1))
1980 ;; We found something if we've arrived at the
1982 (if (and dchange
(= depth target-depth
))
1983 (setq found
(point))))
1985 (if forward
(forward-line 1)))))
1987 (error "No containing preprocessor conditional"))
1988 (goto-char (setq new found
)))
1989 (setq count
(+ count increment
))))
1992 (c-keep-region-active))
1995 ;; commands to indent lines, regions, defuns, and expressions
1996 (defun c-indent-command (&optional arg
)
1997 "Indent current line as C code, and/or insert some whitespace.
1999 If `c-tab-always-indent' is t, always just indent the current line.
2000 If nil, indent the current line only if point is at the left margin or
2001 in the line's indentation; otherwise insert some whitespace[*]. If
2002 other than nil or t, then some whitespace[*] is inserted only within
2003 literals (comments and strings) and inside preprocessor directives,
2004 but the line is always reindented.
2006 If `c-syntactic-indentation' is t, indentation is done according to
2007 the syntactic context. A numeric argument, regardless of its value,
2008 means indent rigidly all the lines of the expression starting after
2009 point so that this line becomes properly indented. The relative
2010 indentation among the lines of the expression is preserved.
2012 If `c-syntactic-indentation' is nil, the line is just indented one
2013 step according to `c-basic-offset'. In this mode, a numeric argument
2014 indents a number of such steps, positive or negative, and an empty
2015 prefix argument is equivalent to -1.
2017 [*] The amount and kind of whitespace inserted is controlled by the
2018 variable `c-insert-tab-function', which is called to do the actual
2019 insertion of whitespace. Normally the function in this variable
2020 just inserts a tab character, or the equivalent number of spaces,
2021 depending on the variable `indent-tabs-mode'."
2024 (let ((indent-function
2025 (if c-syntactic-indentation
2026 (symbol-function 'indent-according-to-mode
)
2028 (let ((c-macro-start c-macro-start
)
2029 (steps (cond ((not current-prefix-arg
) 1)
2030 ((equal current-prefix-arg
'(4)) -
1)
2032 (c-shift-line-indentation (* steps c-basic-offset
))
2033 (when (and c-auto-align-backslashes
2036 (eq (char-before) ?
\\))
2037 (c-query-and-set-macro-start))
2038 ;; Realign the line continuation backslash if inside a macro.
2039 (c-backslash-region (point) (point) nil t
)))
2041 (if (and c-syntactic-indentation current-prefix-arg
)
2042 ;; If c-syntactic-indentation and got arg, always indent this
2043 ;; line as C and shift remaining lines of expression the same
2045 (let ((shift-amt (save-excursion
2046 (back-to-indentation)
2050 (setq shift-amt
(- (save-excursion
2051 (back-to-indentation)
2055 (if (eq c-tab-always-indent t
)
2056 (beginning-of-line))
2064 (indent-code-rigidly beg end shift-amt
"#")))
2065 ;; Else use c-tab-always-indent to determine behavior.
2067 ;; CASE 1: indent when at column zero or in lines indentation,
2068 ;; otherwise insert a tab
2069 ((not c-tab-always-indent
)
2071 (skip-chars-backward " \t")
2073 (funcall c-insert-tab-function
)
2074 (funcall indent-function
)))
2075 ;; CASE 2: just indent the line
2076 ((eq c-tab-always-indent t
)
2077 (funcall indent-function
))
2078 ;; CASE 3: if in a literal, insert a tab, but always indent the
2082 (funcall c-insert-tab-function
))
2083 (funcall indent-function
)
2086 (defun c-indent-exp (&optional shutup-p
)
2087 "Indent each line in the balanced expression following point syntactically.
2088 If optional SHUTUP-P is non-nil, no errors are signalled if no
2089 balanced expression is found."
2091 (let ((here (point-marker))
2093 (set-marker-insertion-type here t
)
2095 (let ((start (save-restriction
2096 ;; Find the closest following open paren that
2097 ;; ends on another line.
2098 (narrow-to-region (point-min) (c-point 'eol
))
2099 (let (beg (end (point)))
2100 (while (and (setq beg
(c-down-list-forward end
))
2101 (setq end
(c-up-list-forward beg
))))
2103 (eq (char-syntax (char-before beg
)) ?\
()
2108 (error "Cannot find start of balanced expression to indent"))
2110 (setq end
(c-safe (scan-sexps (point) 1)))
2113 (error "Cannot find end of balanced expression to indent"))
2116 (c-indent-region (point) end
)))))
2118 (set-marker here nil
))))
2120 (defun c-indent-defun ()
2121 "Indent the current top-level declaration or macro syntactically.
2122 In the macro case this also has the effect of realigning any line
2123 continuation backslashes, unless `c-auto-align-backslashes' is nil."
2125 (let ((here (point-marker)) decl-limits
)
2128 (c-save-buffer-state nil
2129 ;; We try to be line oriented, unless there are several
2130 ;; declarations on the same line.
2131 (if (looking-at c-syntactic-eol
)
2132 (c-backward-token-2 1 nil
(c-point 'bol
))
2133 (c-forward-token-2 0 nil
(c-point 'eol
)))
2134 (setq decl-limits
(c-declaration-limits nil
)))
2136 (c-indent-region (car decl-limits
)
2137 (cdr decl-limits
))))
2139 (set-marker here nil
))))
2141 (defun c-indent-region (start end
&optional quiet
)
2142 "Indent syntactically every line whose first char is between START
2143 and END inclusive. If the optional argument QUIET is non-nil then no
2144 syntactic errors are reported, even if `c-report-syntactic-errors' is
2148 (skip-chars-backward " \t\n\r\f\v")
2151 ;; Advance to first nonblank line.
2153 (skip-chars-forward " \t\n\r\f\v")
2154 (setq start
(point))
2156 (setq c-parsing-error
2157 (or (let ((endmark (copy-marker end
))
2158 (c-parsing-error nil
)
2159 ;; shut up any echo msgs on indiv lines
2160 (c-echo-syntactic-information-p nil
)
2161 (in-macro (and c-auto-align-backslashes
2162 (save-excursion (c-beginning-of-macro))
2164 (c-fix-backslashes nil
)
2168 (c-progress-init start end
'c-indent-region
)
2171 (< (point) endmark
))
2175 (skip-chars-forward " \t\n")
2177 ;; Get syntax and indent.
2178 (c-save-buffer-state nil
2179 (setq syntax
(c-guess-basic-syntax)))
2180 (if (and c-auto-align-backslashes
2181 (assq 'cpp-macro syntax
))
2182 ;; Record macro start.
2183 (setq in-macro
(point)))
2185 (if (looking-at "\\s *\\\\$")
2187 (c-indent-line syntax t t
)
2188 (if (progn (end-of-line)
2189 (not (eq (char-before) ?
\\)))
2191 ;; Fixup macro backslashes.
2193 (c-backslash-region in-macro
(point) nil
)
2194 (setq in-macro nil
))
2196 (c-indent-line syntax t t
)
2199 (c-backslash-region in-macro
(c-point 'bopl
) nil t
)))
2200 (set-marker endmark nil
)
2201 (c-progress-fini 'c-indent-region
))
2202 (c-echo-parsing-error quiet
))
2205 (defun c-fn-region-is-active-p ()
2206 ;; Function version of the macro for use in places that aren't
2207 ;; compiled, e.g. in the menus.
2209 ;; This function does not do any hidden buffer changes.
2210 (c-region-is-active-p))
2212 (defun c-indent-line-or-region ()
2213 "When the region is active, indent it syntactically. Otherwise
2214 indent the current line syntactically."
2215 ;; Emacs has a variable called mark-active, XEmacs uses region-active-p
2217 (if (c-region-is-active-p)
2218 (c-indent-region (region-beginning) (region-end))
2222 ;; for progress reporting
2223 (defvar c-progress-info nil
)
2225 (defun c-progress-init (start end context
)
2226 ;; This function does not do any hidden buffer changes.
2229 ((not c-progress-interval
))
2230 ;; Start the progress update messages. If this Emacs doesn't have
2231 ;; a built-in timer, just be dumb about it.
2232 ((not (fboundp 'current-time
))
2233 (message "Indenting region... (this may take a while)"))
2234 ;; If progress has already been initialized, do nothing. otherwise
2235 ;; initialize the counter with a vector of:
2236 ;; [start end lastsec context]
2238 (t (setq c-progress-info
(vector start
2242 (nth 1 (current-time))
2244 (message "Indenting region..."))
2247 (defun c-progress-update ()
2248 ;; This function does not do any hidden buffer changes.
2249 (if (not (and c-progress-info c-progress-interval
))
2251 (let ((now (nth 1 (current-time)))
2252 (start (aref c-progress-info
0))
2253 (end (aref c-progress-info
1))
2254 (lastsecs (aref c-progress-info
2)))
2255 ;; should we update? currently, update happens every 2 seconds,
2256 ;; what's the right value?
2257 (if (< c-progress-interval
(- now lastsecs
))
2259 (message "Indenting region... (%d%% complete)"
2260 (/ (* 100 (- (point) start
)) (- end start
)))
2261 (aset c-progress-info
2 now
)))
2264 (defun c-progress-fini (context)
2265 ;; This function does not do any hidden buffer changes.
2266 (if (not c-progress-interval
)
2268 (if (or (eq context
(aref c-progress-info
3))
2271 (set-marker (aref c-progress-info
1) nil
)
2272 (setq c-progress-info nil
)
2273 (message "Indenting region... done")))))
2277 ;;; This page handles insertion and removal of backslashes for C macros.
2279 (defun c-backslash-region (from to delete-flag
&optional line-mode
)
2280 "Insert, align, or delete end-of-line backslashes on the lines in the region.
2281 With no argument, inserts backslashes and aligns existing backslashes.
2282 With an argument, deletes the backslashes. The backslash alignment is
2283 done according to the settings in `c-backslash-column',
2284 `c-backslash-max-column' and `c-auto-align-backslashes'.
2286 This function does not modify blank lines at the start of the region.
2287 If the region ends at the start of a line and the macro doesn't
2288 continue below it, the backslash (if any) at the end of the previous
2291 You can put the region around an entire macro definition and use this
2292 command to conveniently insert and align the necessary backslashes."
2293 (interactive "*r\nP")
2294 (let ((endmark (make-marker))
2295 ;; Keep the backslash trimming functions from changing the
2296 ;; whitespace around point, since in this case it's only the
2297 ;; position of point that tells the indentation of the line.
2298 (point-pos (if (save-excursion
2299 (skip-chars-backward " \t")
2300 (and (bolp) (looking-at "[ \t]*\\\\?$")))
2303 column longest-line-col bs-col-after-end
)
2306 (if (and (not line-mode
) (bobp))
2307 ;; Nothing to do if to is at bob, since we should back up
2308 ;; and there's no line to back up to.
2310 (when (and (not line-mode
) (bolp))
2311 ;; Do not back up the to line if line-mode is set, to make
2312 ;; e.g. c-newline-and-indent consistent regardless whether
2313 ;; the (newline) call leaves point at bol or not.
2318 (set-marker endmark
(point))
2320 (c-delete-backslashes-forward endmark point-pos
))
2321 ;; Set bs-col-after-end to the column of any backslash
2322 ;; following the region, or nil if there is none.
2323 (setq bs-col-after-end
2324 (and (progn (end-of-line)
2325 (eq (char-before) ?
\\))
2326 (= (forward-line 1) 0)
2327 (progn (end-of-line)
2328 (eq (char-before) ?
\\))
2329 (1- (current-column))))
2331 ;; Back up the to line if line-mode is set, since the line
2332 ;; after the newly inserted line break should not be
2333 ;; touched in c-newline-and-indent.
2334 (setq to
(max from
(or (c-safe (c-point 'eopl
)) from
)))
2335 (unless bs-col-after-end
2336 ;; Set bs-col-after-end to non-nil in any case, since we
2337 ;; do not want to delete the backslash at the last line.
2338 (setq bs-col-after-end t
)))
2340 (not c-auto-align-backslashes
))
2342 ;; Compute the smallest column number past the ends of all
2344 (setq longest-line-col
0)
2346 (if bs-col-after-end
2347 ;; Include one more line in the max column
2348 ;; calculation, since the to line will be backslashed
2352 (while (and (>= (point) from
)
2354 (if (eq (char-before) ?
\\)
2356 (skip-chars-backward " \t")
2357 (setq longest-line-col
(max longest-line-col
2358 (1+ (current-column))))
2362 ;; Try to align with surrounding backslashes.
2365 (if (and (not (bobp))
2366 (progn (backward-char)
2367 (eq (char-before) ?
\\)))
2369 (setq column
(1- (current-column)))
2370 (if (numberp bs-col-after-end
)
2371 ;; Both a preceding and a following backslash.
2372 ;; Choose the greatest of them.
2373 (setq column
(max column bs-col-after-end
)))
2375 ;; No preceding backslash. Try to align with one
2376 ;; following the region. Disregard the backslash at the
2377 ;; to line since it's likely to be bogus (e.g. when
2378 ;; called from c-newline-and-indent).
2379 (if (numberp bs-col-after-end
)
2380 (setq column bs-col-after-end
))
2381 ;; Don't modify blank lines at start of region.
2383 (while (and (< (point) to
) (bolp) (eolp))
2385 (if (and column
(< column longest-line-col
))
2386 ;; Don't try to align with surrounding backslashes if
2387 ;; any line is too long.
2390 ;; Impose minimum limit and tab width alignment only if
2391 ;; we can't align with surrounding backslashes.
2392 (if (> (% longest-line-col tab-width
) 0)
2393 (setq longest-line-col
2394 (* (/ (+ longest-line-col tab-width -
1)
2397 (setq column
(max c-backslash-column
2399 ;; Always impose maximum limit.
2400 (setq column
(min column c-backslash-max-column
)))
2401 (if bs-col-after-end
2402 ;; Add backslashes on all lines if the macro continues
2403 ;; after the to line.
2405 (set-marker endmark to
)
2406 (c-append-backslashes-forward endmark column point-pos
))
2407 ;; Add backslashes on all lines except the last, and
2408 ;; remove any on the last line.
2413 (set-marker endmark
(1- (point)))))
2415 (c-append-backslashes-forward endmark column point-pos
)
2416 ;; The function above leaves point on the line
2417 ;; following endmark.
2418 (set-marker endmark
(point)))
2419 (set-marker endmark to
))
2420 (c-delete-backslashes-forward endmark point-pos
)))))
2421 (set-marker endmark nil
)
2422 (if (markerp point-pos
)
2423 (set-marker point-pos nil
))))
2425 (defun c-append-backslashes-forward (to-mark column point-pos
)
2426 ;; This function does not do any hidden buffer changes.
2427 (let ((state (parse-partial-sexp (c-point 'bol
) (point))))
2431 (<= (point) to-mark
)
2433 (let ((start (point)) (inserted nil
) end col
)
2435 (unless (eq (char-before) ?
\\)
2438 (setq state
(parse-partial-sexp
2439 start
(point) nil nil state
))
2441 (setq col
(current-column))
2443 ;; Avoid unnecessary changes of the buffer.
2444 (cond ((and (not inserted
) (nth 3 state
))
2445 ;; Don't realign backslashes in string literals
2446 ;; since that would change them.
2453 (skip-chars-backward
2454 " \t" (if (>= (point) point-pos
) point-pos
))
2458 ((and (= col column
)
2459 (memq (char-before) '(?\ ?
\t))))
2463 (or (/= (skip-chars-backward
2464 " \t" (if (>= (point) point-pos
) point-pos
))
2466 (/= (char-after) ?\
)))
2467 (delete-region (point) end
)
2468 (indent-to column
1)))
2470 (= (forward-line 1) 0))))
2472 ;; Make sure there are backslashes with at least one space in
2476 (<= (point) to-mark
)
2478 (let ((start (point)))
2480 (setq state
(parse-partial-sexp
2481 start
(point) nil nil state
))
2483 (if (eq (char-before) ?
\\)
2484 (unless (nth 3 state
)
2486 (unless (and (memq (char-before) '(?\ ?
\t))
2487 (/= (point) point-pos
))
2490 (if (and (memq (char-before) '(?\ ?
\t))
2491 (/= (point) point-pos
))
2495 (= (forward-line 1) 0)))))))
2497 (defun c-delete-backslashes-forward (to-mark point-pos
)
2498 ;; This function does not do any hidden buffer changes.
2500 (and (<= (point) to-mark
)
2503 (if (eq (char-before) ?
\\)
2506 (progn (backward-char)
2507 (skip-chars-backward " \t" (if (>= (point) point-pos
)
2510 (= (forward-line 1) 0)))))
2514 ;;; Line breaking and paragraph filling.
2516 (defvar c-auto-fill-prefix t
)
2517 (defvar c-lit-limits nil
)
2518 (defvar c-lit-type nil
)
2520 ;; The filling code is based on a simple theory; leave the intricacies
2521 ;; of the text handling to the currently active mode for that
2522 ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
2523 ;; possible to make them work correctly wrt the comment and string
2524 ;; separators, one-line paragraphs etc. Unfortunately, when it comes
2525 ;; to it, there's quite a lot of special cases to handle which makes
2526 ;; the code anything but simple. The intention is that it will work
2527 ;; with any well-written text filling package that preserves a fill
2530 ;; We temporarily mask comment starters and enders as necessary for
2531 ;; the filling code to do its job on a seemingly normal text block.
2532 ;; We do _not_ mask the fill prefix, so it's up to the filling code to
2533 ;; preserve it correctly (especially important when filling C++ style
2534 ;; line comments). By default, we set up and use adaptive-fill-mode,
2535 ;; which is standard in all supported Emacs flavors.
2537 (defun c-guess-fill-prefix (lit-limits lit-type
)
2538 ;; Determine the appropriate comment fill prefix for a block or line
2539 ;; comment. Return a cons of the prefix string and the column where
2540 ;; it ends. If fill-prefix is set, it'll override. Note that this
2541 ;; function also uses the value of point in some heuristics.
2543 (let* ((here (point))
2544 (prefix-regexp (concat "[ \t]*\\("
2545 c-current-comment-prefix
2547 (comment-start-regexp (if (eq lit-type
'c
++)
2549 comment-start-skip
))
2550 prefix-line comment-prefix res comment-text-end
)
2554 (setq res
(cons fill-prefix
2555 ;; Ugly way of getting the column after the fill
2556 ;; prefix; it'd be nice with a current-column
2557 ;; that works on strings..
2558 (let ((start (point)))
2561 (insert-and-inherit "\n" fill-prefix
)
2563 (delete-region start
(point)))))))
2567 ;; Set fallback for comment-prefix if none is found.
2568 (setq comment-prefix
"// "
2569 comment-text-end
(cdr lit-limits
))
2572 (if (> (point) (car lit-limits
))
2573 ;; The current line is not the comment starter, so the
2574 ;; comment has more than one line, and it can therefore be
2575 ;; used to find the comment fill prefix.
2576 (setq prefix-line
(point))
2578 (goto-char (car lit-limits
))
2579 (if (and (= (forward-line 1) 0)
2580 (< (point) (cdr lit-limits
)))
2581 ;; The line after the comment starter is inside the
2582 ;; comment, so we can use it.
2583 (setq prefix-line
(point))
2585 ;; The comment is only one line. Take the comment prefix
2586 ;; from it and keep the indentation.
2587 (goto-char (car lit-limits
))
2588 (if (looking-at prefix-regexp
)
2589 (goto-char (match-end 0))
2591 (skip-chars-forward " \t"))
2594 (if (eq (c-point 'boi
) (car lit-limits
))
2595 ;; There is only whitespace before the comment
2596 ;; starter; take the prefix straight from this line.
2597 (setq str
(buffer-substring-no-properties
2598 (c-point 'bol
) (point))
2599 col
(current-column))
2601 ;; There is code before the comment starter, so we
2602 ;; have to temporarily insert and indent a new line to
2603 ;; get the right space/tab mix in the indentation.
2604 (let ((prefix-len (- (point) (car lit-limits
)))
2608 (goto-char (car lit-limits
))
2609 (indent-to (prog1 (current-column)
2612 (forward-char prefix-len
)
2613 (setq str
(buffer-substring-no-properties
2614 (c-point 'bol
) (point))
2615 col
(current-column)))
2616 (delete-region (car lit-limits
) tmp
))))
2619 (if (or (string-match "\\s \\'" str
) (not (eolp)))
2621 ;; The prefix ends the line with no whitespace
2622 ;; after it. Default to a single space.
2623 (cons (concat str
" ") (1+ col
))))
2627 (setq comment-text-end
2629 (goto-char (- (cdr lit-limits
) 2))
2630 (if (looking-at "\\*/") (point) (cdr lit-limits
))))
2634 (if (and (> (point) (car lit-limits
))
2635 (not (and (looking-at "[ \t]*\\*/")
2636 (eq (cdr lit-limits
) (match-end 0)))))
2637 ;; The current line is not the comment starter and
2638 ;; contains more than just the ender, so it's good enough
2639 ;; to be used for the comment fill prefix.
2640 (setq prefix-line
(point))
2641 (goto-char (car lit-limits
))
2643 (cond ((or (/= (forward-line 1) 0)
2644 (>= (point) (cdr lit-limits
))
2645 (and (looking-at "[ \t]*\\*/")
2646 (eq (cdr lit-limits
) (match-end 0)))
2647 (and (looking-at prefix-regexp
)
2648 (<= (1- (cdr lit-limits
)) (match-end 0))))
2649 ;; The comment is either one line or the next line contains
2650 ;; just the comment ender. In this case we have no
2651 ;; information about a suitable comment prefix, so we resort
2652 ;; to c-block-comment-prefix.
2653 (setq comment-prefix
(or c-block-comment-prefix
"")))
2656 ;; The point was on the comment opener line, so we might want
2657 ;; to treat this as a not yet closed comment.
2659 (if (and (match-beginning 1)
2660 (/= (match-beginning 1) (match-end 1)))
2661 ;; Above `prefix-regexp' matched a nonempty prefix on the
2662 ;; second line, so let's use it. Normally it should do
2663 ;; to set `prefix-line' and let the code below pick up
2664 ;; the whole prefix, but if there's no text after the
2665 ;; match then it will probably fall back to no prefix at
2666 ;; all if the comment isn't closed yet, so in that case
2667 ;; it's better to force use of the prefix matched now.
2668 (if (= (match-end 0) (c-point 'eol
))
2669 (setq comment-prefix
(match-string 1))
2670 (setq prefix-line
(point)))
2672 ;; There's no nonempty prefix on the line after the
2673 ;; comment opener. If the line is empty, or if the
2674 ;; text on has less or equal indentation than the
2675 ;; comment starter we assume it's an unclosed
2676 ;; comment starter, i.e. that
2677 ;; `c-block-comment-prefix' should be used.
2678 ;; Otherwise we assume it's a closed comment where
2679 ;; the prefix really is the empty string.
2680 ;; E.g. this is an unclosed comment:
2691 ;; (Looking for the presence of the comment closer
2692 ;; rarely works since it's probably the closer of
2693 ;; some comment further down when the comment
2694 ;; really is unclosed.)
2695 (if (<= (save-excursion (back-to-indentation)
2697 (save-excursion (goto-char (car lit-limits
))
2699 (setq comment-prefix
(or c-block-comment-prefix
""))
2700 (setq prefix-line
(point)))))
2703 ;; Otherwise the line after the comment starter is good
2704 ;; enough to find the prefix in.
2705 (setq prefix-line
(point))))
2707 (when comment-prefix
2708 ;; Haven't got the comment prefix on any real line that we
2709 ;; can take it from, so we have to temporarily insert
2710 ;; `comment-prefix' on a line and indent it to find the
2711 ;; correct column and the correct mix of tabs and spaces.
2713 (let (tmp-pre tmp-post
)
2717 (goto-char (car lit-limits
))
2718 (if (looking-at comment-start-regexp
)
2719 (goto-char (min (match-end 0)
2722 (skip-chars-forward " \t"))
2724 (when (eq (char-syntax (char-before)) ?\
)
2725 ;; If there's ws on the current line, we'll use it
2726 ;; instead of what's ending comment-prefix.
2727 (setq comment-prefix
2728 (concat (substring comment-prefix
2732 (buffer-substring-no-properties
2734 (skip-chars-backward " \t")
2738 (setq tmp-pre
(point-marker))
2740 ;; We insert an extra non-whitespace character
2741 ;; before the line break and after comment-prefix in
2742 ;; case it's "" or ends with whitespace.
2743 (insert-and-inherit "x\n" comment-prefix
"x")
2744 (setq tmp-post
(point-marker))
2746 (indent-according-to-mode)
2748 (goto-char (1- tmp-post
))
2749 (cons (buffer-substring-no-properties
2750 (c-point 'bol
) (point))
2754 (delete-region tmp-pre tmp-post
)
2755 (set-marker tmp-pre nil
)
2756 (set-marker tmp-post nil
))))))))))
2758 (or res
; Found a good prefix above.
2761 ;; prefix-line is the bol of a line on which we should try
2762 ;; to find the prefix.
2763 (let* (fb-string fb-endpos
; Contains any fallback prefix found.
2766 (when (and (looking-at prefix-regexp
)
2767 (<= (match-end 0) comment-text-end
))
2768 (unless (eq (match-end 0) (c-point 'eol
))
2769 ;; The match is fine if there's text after it.
2770 (throw 'found
(cons (buffer-substring-no-properties
2771 (match-beginning 0) (match-end 0))
2772 (progn (goto-char (match-end 0))
2773 (current-column)))))
2775 ;; This match is better than nothing, so let's
2776 ;; remember it in case nothing better is found
2778 (setq fb-string
(buffer-substring-no-properties
2779 (match-beginning 0) (match-end 0))
2780 fb-endpos
(match-end 0)))
2784 ;; Search for a line which has text after the prefix
2785 ;; so that we get the proper amount of whitespace
2786 ;; after it. We start with the current line, then
2787 ;; search backwards, then forwards.
2789 (goto-char prefix-line
)
2790 (when (and (funcall test-line
)
2791 (or (/= (match-end 1) (match-end 0))
2792 ;; The whitespace is sucked up by the
2793 ;; first [ \t]* glob if the prefix is empty.
2794 (and (= (match-beginning 1) (match-end 1))
2795 (/= (match-beginning 0) (match-end 0)))))
2796 ;; If the current line doesn't have text but do
2797 ;; have whitespace after the prefix, we'll use it.
2798 (throw 'found
(cons fb-string
2799 (progn (goto-char fb-endpos
)
2800 (current-column)))))
2802 (if (eq lit-type
'c
++)
2803 ;; For line comments we can search up to and
2804 ;; including the first line.
2805 (while (and (zerop (forward-line -
1))
2806 (>= (point) (car lit-limits
)))
2807 (funcall test-line
))
2808 ;; For block comments we must stop before the
2810 (while (and (zerop (forward-line -
1))
2811 (> (point) (car lit-limits
)))
2812 (funcall test-line
)))
2814 (goto-char prefix-line
)
2815 (while (and (zerop (forward-line 1))
2816 (< (point) (cdr lit-limits
)))
2817 (funcall test-line
))
2819 (goto-char prefix-line
)
2823 ;; A good line wasn't found, but at least we have a
2824 ;; fallback that matches the comment prefix regexp.
2825 (cond ((or (string-match "\\s \\'" fb-string
)
2827 (goto-char fb-endpos
)
2829 ;; There are ws or text after the prefix, so
2831 (cons fb-string
(current-column)))
2834 ;; Check if there's any whitespace padding
2835 ;; on the comment start line that we can
2836 ;; use after the prefix.
2837 (goto-char (car lit-limits
))
2838 (if (looking-at comment-start-regexp
)
2839 (goto-char (match-end 0))
2841 (skip-chars-forward " \t"))
2843 (eq (char-syntax (char-before)) ?\
)))
2845 (setq fb-string
(buffer-substring-no-properties
2847 (skip-chars-backward " \t")
2850 (goto-char fb-endpos
)
2851 (skip-chars-backward " \t")
2853 (let ((tmp (point)))
2854 ;; Got to mess in the buffer once again to
2855 ;; ensure the column gets correct. :P
2858 (insert-and-inherit fb-string
)
2859 (cons (buffer-substring-no-properties
2863 (delete-region tmp
(point)))))
2866 ;; Last resort: Just add a single space after
2868 (cons (concat fb-string
" ")
2869 (progn (goto-char fb-endpos
)
2870 (1+ (current-column)))))))
2872 ;; The line doesn't match the comment prefix regexp.
2874 ;; We have a fallback for line comments that we must use.
2875 (cons (concat (buffer-substring-no-properties
2876 prefix-line
(c-point 'boi
))
2878 (progn (back-to-indentation)
2879 (+ (current-column) (length comment-prefix
))))
2881 ;; Assume we are dealing with a "free text" block
2882 ;; comment where the lines doesn't have any comment
2883 ;; prefix at all and we should just fill it as
2888 (defun c-mask-paragraph (fill-paragraph apply-outside-literal fun
&rest args
)
2889 ;; Calls FUN with ARGS ar arguments while the current paragraph is
2890 ;; masked to allow adaptive filling to work correctly. That
2891 ;; includes narrowing the buffer and, if point is inside a comment,
2892 ;; masking the comment starter and ender appropriately.
2894 ;; FILL-PARAGRAPH is non-nil if called for whole paragraph filling.
2895 ;; The position of point is then less significant when doing masking
2898 ;; If APPLY-OUTSIDE-LITERAL is nil then the function will be called
2899 ;; only if the point turns out to be inside a comment or a string.
2901 ;; This function does not do any hidden buffer changes.
2904 ;; beg and end limits the region to narrow. end is a marker.
2906 ;; tmp-pre and tmp-post mark strings that are temporarily
2907 ;; inserted at the start and end of the region. tmp-pre is a
2908 ;; cons of the positions of the prepended string. tmp-post is
2909 ;; a marker pointing to the single character of the appended
2912 ;; If hang-ender-stuck isn't nil, the comment ender is
2913 ;; hanging. In that case it's set to the number of spaces
2914 ;; that should be between the text and the ender.
2917 (c-lit-limits c-lit-limits
)
2918 (c-lit-type c-lit-type
))
2920 ;; Restore point on undo. It's necessary since we do a lot of
2921 ;; hidden inserts and deletes below that should be as transparent
2923 (if (and buffer-undo-list
(not (eq buffer-undo-list t
)))
2924 (setq buffer-undo-list
(cons (point) buffer-undo-list
)))
2927 ;; Widen to catch comment limits correctly.
2929 (unless c-lit-limits
2930 (setq c-lit-limits
(c-literal-limits nil fill-paragraph
)))
2931 (setq c-lit-limits
(c-collect-line-comments c-lit-limits
))
2933 (setq c-lit-type
(c-literal-type c-lit-limits
))))
2936 (unless (c-safe (backward-char)
2940 (forward-paragraph))
2941 (setq end
(point-marker)))
2943 (unless (c-safe (forward-char)
2944 (backward-paragraph)
2947 (backward-paragraph))
2954 ((eq c-lit-type
'c
++) ; Line comment.
2956 ;; Limit to the comment or paragraph end, whichever
2958 (set-marker end
(min end
(cdr c-lit-limits
)))
2960 (when (<= beg
(car c-lit-limits
))
2961 ;; The region includes the comment starter, so we must
2963 (goto-char (car c-lit-limits
))
2964 (back-to-indentation)
2965 (if (eq (point) (car c-lit-limits
))
2966 ;; Include the first line in the region.
2967 (setq beg
(c-point 'bol
))
2968 ;; The first line contains code before the
2969 ;; comment. We must fake a line that doesn't.
2972 (setq apply-outside-literal t
))
2974 ((eq c-lit-type
'c
) ; Block comment.
2975 (when (>= end
(cdr c-lit-limits
))
2976 ;; The region includes the comment ender which we might
2977 ;; want to keep together with the last word.
2978 (unless (save-excursion
2979 (goto-char (cdr c-lit-limits
))
2981 (and (looking-at (concat "[ \t]*\\("
2982 c-current-comment-prefix
2984 (eq (cdr c-lit-limits
) (match-end 0))
2985 ;; The comment ender is on a line of its
2986 ;; own. Keep it that way.
2987 (set-marker end
(point))))
2990 ;; The comment ender should hang. Replace all
2991 ;; cruft between it and the last word with one or
2992 ;; two 'x' and include it in the region. We'll
2993 ;; change them back to spaces afterwards. This
2994 ;; isn't done when auto filling, since that'd
2995 ;; effectively make it impossible to insert extra
2996 ;; spaces before the comment ender.
2997 (let* ((ender-start (save-excursion
2998 (goto-char (cdr c-lit-limits
))
2999 (skip-syntax-backward "^w ")
3001 (point-rel (- ender-start here
))
3005 (goto-char (cdr c-lit-limits
))
3006 (setq tmp-post
(point-marker))
3008 (set-marker end
(point))
3010 (if (and (looking-at (concat "[ \t]*\\(\\("
3011 c-current-comment-prefix
3013 (eq ender-start
(match-end 0)))
3014 ;; The comment ender is prefixed by nothing
3015 ;; but a comment line prefix. Remove it
3016 ;; along with surrounding ws.
3017 (setq spaces
(- (match-end 1) (match-end 2)))
3018 (goto-char ender-start
))
3019 (skip-chars-backward " \t\r\n")
3021 (if (/= (point) ender-start
)
3023 (if (<= here
(point))
3024 ;; Don't adjust point below if it's
3025 ;; before the string we replace.
3026 (setq point-rel -
1))
3027 ;; Keep one or two spaces between the
3028 ;; text and the ender, depending on how
3029 ;; many there are now.
3031 (setq spaces
(- ender-start
(point))))
3035 (if sentence-end-double-space
2 1))
3037 ;; Insert the filler first to keep marks right.
3038 (insert-char ?x spaces t
)
3039 (delete-region (point) (+ ender-start spaces
))
3040 (setq hang-ender-stuck spaces
)
3042 (and (>= point-rel
0)
3043 (- (point) (min point-rel spaces
)))))
3044 (setq point-rel nil
)))
3047 ;; Point was in the middle of the string we
3048 ;; replaced above, so put it back in the same
3049 ;; relative position, counting from the end.
3050 (goto-char point-rel
)))
3052 ;; We're doing auto filling. Just move the marker
3053 ;; to the comment end to ignore any code after the
3055 (move-marker end
(cdr c-lit-limits
)))))
3057 (when (<= beg
(car c-lit-limits
))
3058 ;; The region includes the comment starter.
3060 (goto-char (car c-lit-limits
))
3061 (if (looking-at (concat "\\(" comment-start-skip
"\\)$"))
3062 ;; Begin with the next line.
3063 (setq beg
(c-point 'bonl
))
3064 ;; Fake the fill prefix in the first line.
3067 (setq apply-outside-literal t
))
3069 ((eq c-lit-type
'string
) ; String.
3071 (when (>= end
(cdr c-lit-limits
))
3072 (goto-char (1- (cdr c-lit-limits
)))
3073 (setq tmp-post
(point-marker))
3075 (set-marker end
(point)))
3076 (when (<= beg
(car c-lit-limits
))
3077 (goto-char (1+ (car c-lit-limits
)))
3078 (setq beg
(if (looking-at "\\\\$")
3079 ;; Leave the start line if it's
3080 ;; nothing but an escaped newline.
3083 (setq apply-outside-literal t
))
3085 ((eq c-lit-type
'pound
) ; Macro
3086 ;; Narrow to the macro limits if they are nearer than the
3087 ;; paragraph limits. Don't know if this is necessary but
3088 ;; do it for completeness sake (doing auto filling at all
3089 ;; inside macros is bogus to begin with since the line
3090 ;; continuation backslashes aren't handled).
3092 (c-beginning-of-macro)
3099 (set-marker end
(point)))))
3102 ;; Try to avoid comments and macros in the paragraph to
3103 ;; avoid that the adaptive fill mode gets the prefix from
3105 (c-save-buffer-state nil
3108 (c-forward-syntactic-ws end
)
3112 (c-backward-syntactic-ws beg
)
3114 (set-marker end
(point))))))
3117 ;; Temporarily insert the fill prefix after the comment
3118 ;; starter so that the first line looks like any other
3119 ;; comment line in the narrowed region.
3120 (setq fill
(c-save-buffer-state nil
3121 (c-guess-fill-prefix c-lit-limits c-lit-type
)))
3122 (unless (string-match (concat "\\`[ \t]*\\("
3123 c-current-comment-prefix
3126 ;; Oops, the prefix doesn't match the comment prefix
3127 ;; regexp. This could produce very confusing
3128 ;; results with adaptive fill packages together with
3129 ;; the insert prefix magic below, since the prefix
3130 ;; often doesn't appear at all. So let's warn about
3133 Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S"
3135 ;; Find the right spot on the line, break it, insert
3136 ;; the fill prefix and make sure we're back in the
3137 ;; same column by temporarily prefixing the first word
3138 ;; with a number of 'x'.
3140 (goto-char (car c-lit-limits
))
3141 (if (looking-at (if (eq c-lit-type
'c
++)
3142 c-current-comment-prefix
3143 comment-start-skip
))
3144 (goto-char (match-end 0))
3146 (skip-chars-forward " \t"))
3147 (while (and (< (current-column) (cdr fill
))
3150 (let ((col (current-column)))
3151 (setq beg
(1+ (point))
3152 tmp-pre
(list (point)))
3155 (insert-and-inherit "\n" (car fill
))
3156 (insert-char ?x
(- col
(current-column)) t
))
3157 (setcdr tmp-pre
(point))))))
3159 (when apply-outside-literal
3160 ;; `apply-outside-literal' is always set to t here if
3161 ;; we're inside a literal.
3165 ;; Kludge: If the function that adapts the fill prefix
3166 ;; doesn't produce the required comment starter for
3167 ;; line comments, then force it by setting fill-prefix.
3168 (when (and (eq c-lit-type
'c
++)
3169 ;; Kludge the kludge: filladapt-mode doesn't
3170 ;; have this problem, but it currently
3171 ;; doesn't override fill-context-prefix
3173 (not (and (boundp 'filladapt-mode
)
3177 (or (fill-context-prefix beg end
)
3179 (c-save-buffer-state nil
3180 (car (or fill
(c-guess-fill-prefix
3181 c-lit-limits c-lit-type
)))))))
3183 ;; Save the relative position of point if it's outside the
3184 ;; region we're going to narrow. Want to restore it in that
3185 ;; case, but otherwise it should be moved according to the
3187 (point-rel (cond ((< (point) beg
) (- (point) beg
))
3188 ((> (point) end
) (- (point) end
)))))
3190 ;; Preparations finally done! Now we can call the
3194 (narrow-to-region beg end
)
3197 ;; Restore point if it was outside the region.
3199 (goto-char (+ beg point-rel
))
3200 (goto-char (+ end point-rel
))))))))
3202 (when (consp tmp-pre
)
3203 (delete-region (car tmp-pre
) (cdr tmp-pre
)))
3207 (goto-char tmp-post
)
3209 (when hang-ender-stuck
3210 ;; Preserve point even if it's in the middle of the string
3211 ;; we replace; save-excursion doesn't work in that case.
3213 (goto-char tmp-post
)
3214 (skip-syntax-backward "^w ")
3215 (forward-char (- hang-ender-stuck
))
3216 (insert-char ?\ hang-ender-stuck t
)
3217 (delete-char hang-ender-stuck
)
3219 (set-marker tmp-post nil
))
3221 (set-marker end nil
))))
3223 (defun c-fill-paragraph (&optional arg
)
3224 "Like \\[fill-paragraph] but handles C and C++ style comments.
3225 If any of the current line is a comment or within a comment, fill the
3226 comment or the paragraph of it that point is in, preserving the
3227 comment indentation or line-starting decorations (see the
3228 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
3231 If point is inside multiline string literal, fill it. This currently
3232 does not respect escaped newlines, except for the special case when it
3233 is the very first thing in the string. The intended use for this rule
3234 is in situations like the following:
3236 char description[] = \"\\
3237 A very long description of something that you want to fill to make
3238 nicely formatted output.\"\;
3240 If point is in any other situation, i.e. in normal code, do nothing.
3242 Optional prefix ARG means justify paragraph as well."
3244 (let ((fill-paragraph-function
3245 ;; Avoid infinite recursion.
3246 (if (not (eq fill-paragraph-function
'c-fill-paragraph
))
3247 fill-paragraph-function
)))
3248 (c-mask-paragraph t nil
'fill-paragraph arg
))
3249 ;; Always return t. This has the effect that if filling isn't done
3250 ;; above, it isn't done at all, and it's therefore effectively
3251 ;; disabled in normal code.
3254 (defun c-do-auto-fill ()
3255 ;; Do automatic filling if not inside a context where it should be
3258 ;; This function does not do any hidden buffer changes.
3259 (let ((c-auto-fill-prefix
3260 ;; The decision whether the line should be broken is actually
3261 ;; done in c-indent-new-comment-line, which do-auto-fill
3262 ;; calls to break lines. We just set this special variable
3263 ;; so that we'll know when we're called from there. It's
3264 ;; also used to detect whether fill-prefix is user set or
3265 ;; generated automatically by do-auto-fill.
3267 (c-mask-paragraph nil t
'do-auto-fill
)))
3269 (defun c-indent-new-comment-line (&optional soft allow-auto-fill
)
3270 "Break line at point and indent, continuing comment or macro if within one.
3271 If inside a comment and `comment-multi-line' is non-nil, the
3272 indentation and line prefix are preserved (see the
3273 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
3274 details). If inside a single line comment and `comment-multi-line' is
3275 nil, a new comment of the same type is started on the next line and
3276 indented as appropriate for comments. If inside a macro, a line
3277 continuation backslash is inserted and aligned as appropriate, and the
3278 new line is indented according to `c-syntactic-indentation'.
3280 If a fill prefix is specified, it overrides all the above."
3281 ;; allow-auto-fill is used from c-context-line-break to allow auto
3282 ;; filling to break the line more than once. Since this function is
3283 ;; used from auto-fill itself, that's normally disabled to avoid
3284 ;; unnecessary recursion.
3286 (let ((fill-prefix fill-prefix
)
3289 (delete-horizontal-space)
3291 (insert-and-inherit ?
\n)
3292 (newline (if allow-auto-fill nil
1)))))
3293 ;; Already know the literal type and limits when called from
3294 ;; c-context-line-break.
3295 (c-lit-limits c-lit-limits
)
3296 (c-lit-type c-lit-type
)
3297 (c-macro-start c-macro-start
))
3298 (when (not (eq c-auto-fill-prefix t
))
3299 ;; Called from do-auto-fill.
3300 (unless c-lit-limits
3301 (setq c-lit-limits
(c-literal-limits nil nil t
)))
3303 (setq c-lit-type
(c-literal-type c-lit-limits
)))
3304 (if (memq (cond ((c-query-and-set-macro-start) 'cpp
)
3305 ((null c-lit-type
) 'code
)
3308 (setq fill-prefix t
) ; Used as flag in the cond.
3309 (if (and (null c-auto-fill-prefix
)
3311 (<= (c-point 'bol
) (car c-lit-limits
)))
3312 ;; The adaptive fill function has generated a prefix, but
3313 ;; we're on the first line in a block comment so it'll be
3314 ;; wrong. Ignore it to guess a better one below.
3315 (setq fill-prefix nil
)
3316 (when (and (eq c-lit-type
'c
++)
3317 (not (string-match "\\`[ \t]*//" (or fill-prefix
""))))
3318 ;; Kludge: If the function that adapted the fill prefix
3319 ;; doesn't produce the required comment starter for line
3320 ;; comments, then we ignore it.
3321 (setq fill-prefix nil
)))
3323 (cond ((eq fill-prefix t
)
3324 ;; A call from do-auto-fill which should be ignored.
3327 ;; A fill-prefix overrides anything.
3328 (funcall do-line-break
)
3329 (insert-and-inherit fill-prefix
))
3331 (unless c-lit-limits
3332 (setq c-lit-limits
(c-literal-limits)))
3334 (setq c-lit-type
(c-literal-type c-lit-limits
)))
3335 (memq c-lit-type
'(c c
++)))
3336 ;; Some sort of comment.
3337 (if (or comment-multi-line
3339 (goto-char (car c-lit-limits
))
3341 (< (point) (cdr c-lit-limits
))))
3342 ;; Inside a comment that should be continued.
3343 (let ((fill (c-save-buffer-state nil
3344 (c-guess-fill-prefix
3346 (c-collect-line-comments c-lit-limits
))
3350 (or (and (eq c-lit-type
'c
)
3352 (goto-char (- (cdr c-lit-limits
) 2))
3353 (if (looking-at "\\*/") (point))))
3354 (cdr c-lit-limits
))))
3355 ;; Skip forward past the fill prefix in case
3356 ;; we're standing in it.
3358 ;; FIXME: This doesn't work well in cases like
3360 ;; /* Bla bla bla bla bla
3363 ;; If point is on the 'B' then the line will be
3364 ;; broken after "Bla b".
3365 (while (and (< (current-column) (cdr fill
))
3368 (if (and (> (point) comment-text-end
)
3369 (> (c-point 'bol
) (car c-lit-limits
)))
3371 ;; The skip takes us out of the (block)
3372 ;; comment; insert the fill prefix at bol
3373 ;; instead and keep the position.
3374 (setq pos
(copy-marker pos t
))
3376 (insert-and-inherit (car fill
))
3377 (if soft
(insert-and-inherit ?
\n) (newline 1))
3379 (set-marker pos nil
))
3380 ;; Don't break in the middle of a comment starter
3382 (cond ((> (point) comment-text-end
)
3383 (goto-char comment-text-end
))
3384 ((< (point) (+ (car c-lit-limits
) 2))
3385 (goto-char (+ (car c-lit-limits
) 2))))
3386 (funcall do-line-break
)
3387 (insert-and-inherit (car fill
))))
3388 ;; Inside a comment that should be broken.
3389 (let ((comment-start comment-start
)
3390 (comment-end comment-end
)
3392 (if (eq c-lit-type
'c
)
3393 (unless (string-match "[ \t]*/\\*" comment-start
)
3394 (setq comment-start
"/* " comment-end
" */"))
3395 (unless (string-match "[ \t]*//" comment-start
)
3396 (setq comment-start
"// " comment-end
"")))
3397 (setq col
(save-excursion
3398 (back-to-indentation)
3400 (funcall do-line-break
)
3401 (when (and comment-end
(not (equal comment-end
"")))
3403 (insert-and-inherit comment-end
)
3405 ;; c-comment-indent may look at the current
3406 ;; indentation, so let's start out with the same
3407 ;; indentation as the previous one.
3409 (insert-and-inherit comment-start
)
3410 (indent-for-comment))))
3411 ((c-query-and-set-macro-start)
3413 (unless (looking-at "[ \t]*\\\\$")
3414 ;; Do not clobber the alignment of the line continuation
3415 ;; slash; c-backslash-region might look at it.
3416 (delete-horizontal-space))
3417 ;; Got an asymmetry here: In normal code this command
3418 ;; doesn't indent the next line syntactically, and otoh a
3419 ;; normal syntactically indenting newline doesn't continue
3421 (c-newline-and-indent (if allow-auto-fill nil
1)))
3423 ;; Somewhere else in the code.
3424 (let ((col (save-excursion
3426 (while (and (looking-at "[ \t]*\\\\?$")
3427 (= (forward-line -
1) 0)))
3428 (current-indentation))))
3429 (funcall do-line-break
)
3430 (indent-to col
))))))
3432 (defalias 'c-comment-line-break-function
'c-indent-new-comment-line
)
3433 (make-obsolete 'c-comment-line-break-function
'c-indent-new-comment-line
)
3435 ;; advice for indent-new-comment-line for older Emacsen
3436 (unless (boundp 'comment-line-break-function
)
3437 (defvar c-inside-line-break-advice nil
)
3438 (defadvice indent-new-comment-line
(around c-line-break-advice
3439 activate preactivate
)
3440 "Call `c-indent-new-comment-line' if in CC Mode."
3441 (if (or c-inside-line-break-advice
3442 (not c-buffer-is-cc-mode
))
3444 (let ((c-inside-line-break-advice t
))
3445 (c-indent-new-comment-line (ad-get-arg 0))))))
3447 (defun c-context-line-break ()
3448 "Do a line break suitable to the context.
3450 When point is outside a comment or macro, insert a newline and indent
3451 according to the syntactic context, unless `c-syntactic-indentation'
3452 is nil, in which case the new line is indented as the previous
3453 non-empty line instead.
3455 When point is inside the content of a preprocessor directive, a line
3456 continuation backslash is inserted before the line break and aligned
3457 appropriately. The end of the cpp directive doesn't count as inside
3460 When point is inside a comment, continue it with the appropriate
3461 comment prefix (see the `c-comment-prefix-regexp' and
3462 `c-block-comment-prefix' variables for details). The end of a
3463 C++-style line comment doesn't count as inside it."
3465 (let* ((c-lit-limits (c-literal-limits nil nil t
))
3466 (c-lit-type (c-literal-type c-lit-limits
))
3467 (c-macro-start c-macro-start
))
3468 (if (or (eq c-lit-type
'c
)
3469 (and (eq c-lit-type
'c
++)
3471 (skip-chars-forward " \t")
3473 (1- (cdr (setq c-lit-limits
3474 (c-collect-line-comments c-lit-limits
))))))
3475 (and (or (not (looking-at "\\s *$"))
3476 (eq (char-before) ?
\\))
3477 (c-query-and-set-macro-start)
3479 (goto-char c-macro-start
)
3480 (if (looking-at c-opt-cpp-start
)
3481 (goto-char (match-end 0)))
3484 (let ((comment-multi-line t
)
3486 (c-indent-new-comment-line nil t
))
3487 (delete-horizontal-space)
3489 ;; c-indent-line may look at the current indentation, so let's
3490 ;; start out with the same indentation as the previous line.
3491 (let ((col (save-excursion
3493 (while (and (looking-at "[ \t]*\\\\?$")
3494 (= (forward-line -
1) 0)))
3495 (current-indentation))))
3497 (indent-according-to-mode))))
3499 (defun c-context-open-line ()
3500 "Insert a line break suitable to the context and leave point before it.
3501 This is the `c-context-line-break' equivalent to `open-line', which is
3502 normally bound to C-o. See `c-context-line-break' for the details."
3504 (let ((here (point)))
3507 ;; Temporarily insert a non-whitespace char to keep any
3508 ;; preceding whitespace intact.
3510 (c-context-line-break))
3515 (cc-provide 'cc-cmds
)
3517 ;;; arch-tag: bf0611dc-d1f4-449e-9e45-4ec7c6936677
3518 ;;; cc-cmds.el ends here