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