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