]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-cmds.el
(compilation-mode-map): Don't inherit from compilation-minor-mode-map;
[gnu-emacs] / lisp / progmodes / cc-cmds.el
1 ;;; cc-cmds.el --- user level commands for CC Mode
2
3 ;; Copyright (C) 1985,1987,1992-2003 Free Software Foundation, Inc.
4
5 ;; Authors: 1998- Martin Stjernholm
6 ;; 1992-1999 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Version: See cc-mode.el
12 ;; Keywords: c languages oop
13
14 ;; This file is part of GNU Emacs.
15
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
20
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
25
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to
28 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
30
31 ;;; Commentary:
32
33 ;;; Code:
34
35 (eval-when-compile
36 (let ((load-path
37 (if (and (boundp 'byte-compile-dest-file)
38 (stringp byte-compile-dest-file))
39 (cons (file-name-directory byte-compile-dest-file) load-path)
40 load-path)))
41 (load "cc-bytecomp" nil t)))
42
43 (cc-require 'cc-defs)
44 (cc-require 'cc-vars)
45 (cc-require 'cc-engine)
46
47 ;; Silence the compiler.
48 (cc-bytecomp-defvar delete-key-deletes-forward) ; XEmacs 20+
49 (cc-bytecomp-defun delete-forward-p) ; XEmacs 21+
50 (cc-bytecomp-obsolete-fun insert-and-inherit) ; Marked obsolete in XEmacs 19
51 (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge
52 ; which looks at this.
53
54 \f
55 (defvar c-fix-backslashes t)
56
57 (defun c-indent-line (&optional syntax quiet ignore-point-pos)
58 "Indent the current line according to the syntactic context,
59 if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the
60 syntactic information for the current line. Be silent about syntactic
61 errors if the optional argument QUIET is non-nil, even if
62 `c-report-syntactic-errors' is non-nil. Normally the position of
63 point is used to decide where the old indentation is on a lines that
64 is otherwise empty \(ignoring any line continuation backslash), but
65 that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of
66 indentation change \(in columns)."
67 ;;
68 ;; This function does not do any hidden buffer changes.
69
70 (let ((line-cont-backslash (save-excursion
71 (end-of-line)
72 (eq (char-before) ?\\)))
73 (c-fix-backslashes c-fix-backslashes)
74 bs-col
75 shift-amt)
76 (when (and (not ignore-point-pos)
77 (save-excursion
78 (beginning-of-line)
79 (looking-at (if line-cont-backslash
80 "\\(\\s *\\)\\\\$"
81 "\\(\\s *\\)$")))
82 (<= (point) (match-end 1)))
83 ;; Delete all whitespace after point if there's only whitespace
84 ;; on the line, so that any code that does back-to-indentation
85 ;; or similar gets the current column in this case. If this
86 ;; removes a line continuation backslash it'll be restored
87 ;; at the end.
88 (unless c-auto-align-backslashes
89 ;; Should try to keep the backslash alignment
90 ;; in this case.
91 (save-excursion
92 (goto-char (match-end 0))
93 (setq bs-col (1- (current-column)))))
94 (delete-region (point) (match-end 0))
95 (setq c-fix-backslashes t))
96 (if c-syntactic-indentation
97 (setq c-parsing-error
98 (or (let ((c-parsing-error nil)
99 (c-syntactic-context
100 (or syntax
101 (and (boundp 'c-syntactic-context)
102 c-syntactic-context))))
103 (c-save-buffer-state (indent)
104 (unless c-syntactic-context
105 (setq c-syntactic-context (c-guess-basic-syntax)))
106 (setq indent (c-get-syntactic-indentation
107 c-syntactic-context))
108 (and (not (c-echo-parsing-error quiet))
109 c-echo-syntactic-information-p
110 (message "syntax: %s, indent: %d"
111 c-syntactic-context indent))
112 (setq shift-amt (- indent (current-indentation))))
113 (c-shift-line-indentation shift-amt)
114 (run-hooks 'c-special-indent-hook)
115 c-parsing-error)
116 c-parsing-error))
117 (let ((indent 0))
118 (save-excursion
119 (while (and (= (forward-line -1) 0)
120 (if (looking-at "\\s *\\\\?$")
121 t
122 (setq indent (current-indentation))
123 nil))))
124 (setq shift-amt (- indent (current-indentation)))
125 (c-shift-line-indentation shift-amt)))
126 (when (and c-fix-backslashes line-cont-backslash)
127 (if bs-col
128 (save-excursion
129 (indent-to bs-col)
130 (insert ?\\))
131 (when c-auto-align-backslashes
132 ;; Realign the line continuation backslash.
133 (c-backslash-region (point) (point) nil t))))
134 shift-amt))
135
136 (defun c-newline-and-indent (&optional newline-arg)
137 "Inserts a newline and indents the new line.
138 This function fixes line continuation backslashes if inside a macro,
139 and takes care to set the indentation before calling
140 `indent-according-to-mode', so that lineup functions like
141 `c-lineup-dont-change' works better."
142 ;;
143 ;; This function does not do any hidden buffer changes.
144
145 ;; TODO: Backslashes before eol in comments and literals aren't
146 ;; kept intact.
147 (let ((c-macro-start (c-query-macro-start))
148 ;; Avoid calling c-backslash-region from c-indent-line if it's
149 ;; called during the newline call, which can happen due to
150 ;; c-electric-continued-statement, for example. We also don't
151 ;; want any backslash alignment from indent-according-to-mode.
152 (c-fix-backslashes nil)
153 has-backslash insert-backslash
154 start col)
155 (save-excursion
156 (beginning-of-line)
157 (setq start (point))
158 (while (and (looking-at "[ \t]*\\\\?$")
159 (= (forward-line -1) 0)))
160 (setq col (current-indentation)))
161 (when c-macro-start
162 (if (and (eolp) (eq (char-before) ?\\))
163 (setq insert-backslash t
164 has-backslash t)
165 (setq has-backslash (eq (char-before (c-point 'eol)) ?\\))))
166 (newline newline-arg)
167 (indent-to col)
168 (when c-macro-start
169 (if insert-backslash
170 (progn
171 ;; The backslash stayed on the previous line. Insert one
172 ;; before calling c-backslash-region, so that
173 ;; bs-col-after-end in it works better. Fixup the
174 ;; backslashes on the newly inserted line.
175 (insert ?\\)
176 (backward-char)
177 (c-backslash-region (point) (point) nil t))
178 ;; The backslash moved to the new line, if there was any. Let
179 ;; c-backslash-region fix a backslash on the previous line,
180 ;; and the one that might be on the new line.
181 ;; c-auto-align-backslashes is intentionally ignored here;
182 ;; maybe the moved backslash should be left alone if it's set,
183 ;; but we fix both lines on the grounds that the old backslash
184 ;; has been moved anyway and is now in a different context.
185 (c-backslash-region start (if has-backslash (point) start) nil t)))
186 (when c-syntactic-indentation
187 ;; Reindent syntactically. The indentation done above is not
188 ;; wasted, since c-indent-line might look at the current
189 ;; indentation.
190 (let ((c-syntactic-context (c-save-buffer-state nil
191 (c-guess-basic-syntax))))
192 ;; We temporarily insert another line break, so that the
193 ;; lineup functions will see the line as empty. That makes
194 ;; e.g. c-lineup-cpp-define more intuitive since it then
195 ;; proceeds to the preceding line in this case.
196 (insert ?\n)
197 (delete-horizontal-space)
198 (setq start (- (point-max) (point)))
199 (unwind-protect
200 (progn
201 (backward-char)
202 (indent-according-to-mode))
203 (goto-char (- (point-max) start))
204 (delete-char -1)))
205 (when has-backslash
206 ;; Must align the backslash again after reindentation. The
207 ;; c-backslash-region call above can't be optimized to ignore
208 ;; this line, since it then won't align correctly with the
209 ;; lines below if the first line in the macro is broken.
210 (c-backslash-region (point) (point) nil t)))))
211
212 (defun c-show-syntactic-information (arg)
213 "Show syntactic information for current line.
214 With universal argument, inserts the analysis as a comment on that line."
215 (interactive "P")
216 (let* ((c-parsing-error nil)
217 (syntax (if (boundp 'c-syntactic-context)
218 ;; Use `c-syntactic-context' in the same way as
219 ;; `c-indent-line', to be consistent.
220 c-syntactic-context
221 (c-save-buffer-state nil
222 (c-guess-basic-syntax)))))
223 (if (not (consp arg))
224 (message "syntactic analysis: %s" syntax)
225 (indent-for-comment)
226 (insert-and-inherit (format "%s" syntax))
227 ))
228 (c-keep-region-active))
229
230 (defun c-syntactic-information-on-region (from to)
231 "Inserts a comment with the syntactic analysis on every line in the region."
232 (interactive "*r")
233 (save-excursion
234 (save-restriction
235 (narrow-to-region from to)
236 (goto-char (point-min))
237 (while (not (eobp))
238 (c-show-syntactic-information '(0))
239 (forward-line)))))
240
241 \f
242 (defun c-toggle-syntactic-indentation (&optional arg)
243 "Toggle syntactic indentation.
244 Optional numeric ARG, if supplied, turns on syntactic indentation when
245 positive, turns it off when negative, and just toggles it when zero or
246 left out.
247
248 When syntactic indentation is turned on (the default), the indentation
249 functions and the electric keys indent according to the syntactic
250 context keys, when applicable.
251
252 When it's turned off, the electric keys does no reindentation, the
253 indentation functions indents every new line to the same level as the
254 previous nonempty line, and \\[c-indent-command] adjusts the
255 indentation in seps specified `c-basic-offset'. The indentation style
256 has no effect in this mode, nor any of the indentation associated
257 variables, e.g. `c-special-indent-hook'.
258
259 This command sets the variable `c-syntactic-indentation'."
260 (interactive "P")
261 (setq c-syntactic-indentation
262 (c-calculate-state arg c-syntactic-indentation))
263 (c-keep-region-active))
264
265 (defun c-toggle-auto-state (&optional arg)
266 "Toggle auto-newline feature.
267 Optional numeric ARG, if supplied, turns on auto-newline when
268 positive, turns it off when negative, and just toggles it when zero or
269 left out.
270
271 When the auto-newline feature is enabled (as evidenced by the `/a' or
272 `/ah' on the modeline after the mode name) newlines are automatically
273 inserted after special characters such as brace, comma, semi-colon,
274 and colon."
275 (interactive "P")
276 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
277 (c-update-modeline)
278 (c-keep-region-active))
279
280 (defun c-toggle-hungry-state (&optional arg)
281 "Toggle hungry-delete-key feature.
282 Optional numeric ARG, if supplied, turns on hungry-delete when
283 positive, turns it off when negative, and just toggles it when zero or
284 left out.
285
286 When the hungry-delete-key feature is enabled (as evidenced by the
287 `/h' or `/ah' on the modeline after the mode name) the delete key
288 gobbles all preceding whitespace in one fell swoop."
289 (interactive "P")
290 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
291 (c-update-modeline)
292 (c-keep-region-active))
293
294 (defun c-toggle-auto-hungry-state (&optional arg)
295 "Toggle auto-newline and hungry-delete-key features.
296 Optional numeric ARG, if supplied, turns on auto-newline and
297 hungry-delete when positive, turns them off when negative, and just
298 toggles them when zero or left out.
299
300 See `c-toggle-auto-state' and `c-toggle-hungry-state' for details."
301 (interactive "P")
302 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
303 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
304 (c-update-modeline)
305 (c-keep-region-active))
306
307 \f
308 ;; Electric keys
309
310 (defun c-electric-backspace (arg)
311 "Delete the preceding character or whitespace.
312 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
313 \"/ah\" string on the mode line, then all preceding whitespace is
314 consumed. If however a prefix argument is supplied, or
315 `c-hungry-delete-key' is nil, or point is inside a literal then the
316 function in the variable `c-backspace-function' is called."
317 (interactive "*P")
318 (if (or (not c-hungry-delete-key)
319 arg
320 (c-in-literal))
321 (funcall c-backspace-function (prefix-numeric-value arg))
322 (c-hungry-backspace)))
323
324 (defun c-hungry-backspace ()
325 "Delete the preceding character or all preceding whitespace
326 back to the previous non-whitespace character.
327 See also \\[c-hungry-delete-forward]."
328 (interactive)
329 (let ((here (point)))
330 (c-skip-ws-backward)
331 (if (/= (point) here)
332 (delete-region (point) here)
333 (funcall c-backspace-function 1))))
334
335 (defun c-electric-delete-forward (arg)
336 "Delete the following character or whitespace.
337 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or
338 \"/ah\" string on the mode line, then all following whitespace is
339 consumed. If however a prefix argument is supplied, or
340 `c-hungry-delete-key' is nil, or point is inside a literal then the
341 function in the variable `c-delete-function' is called."
342 (interactive "*P")
343 (if (or (not c-hungry-delete-key)
344 arg
345 (c-in-literal))
346 (funcall c-delete-function (prefix-numeric-value arg))
347 (c-hungry-delete-forward)))
348
349 (defun c-hungry-delete-forward ()
350 "Delete the following character or all following whitespace
351 up to the next non-whitespace character.
352 See also \\[c-hungry-backspace]."
353 (interactive)
354 (let ((here (point)))
355 (c-skip-ws-forward)
356 (if (/= (point) here)
357 (delete-region (point) here)
358 (funcall c-delete-function 1))))
359
360 ;; This function is only used in XEmacs.
361 (defun c-electric-delete (arg)
362 "Deletes preceding or following character or whitespace.
363 This function either deletes forward as `c-electric-delete-forward' or
364 backward as `c-electric-backspace', depending on the configuration:
365
366 If the function `delete-forward-p' is defined (XEmacs 21) and returns
367 non-nil, it deletes forward. Else, if the variable
368 `delete-key-deletes-forward' is defined (XEmacs 20) and is set to
369 non-nil, it deletes forward. Otherwise it deletes backward.
370
371 Note: This is the way in XEmacs 20 and later to choose the correct
372 action for the [delete] key, whichever key that means. In other
373 flavors this function isn't used, instead it's left to the user to
374 bind [delete] to either \\[c-electric-delete-forward] or \\[c-electric-backspace] as appropriate
375 \(the keymap `function-key-map' is useful for that). Emacs 21 handles
376 that automatically, though."
377 (interactive "*P")
378 (if (or (and (fboundp 'delete-forward-p) ;XEmacs 21
379 (delete-forward-p))
380 (and (boundp 'delete-key-deletes-forward) ;XEmacs 20
381 delete-key-deletes-forward))
382 (c-electric-delete-forward arg)
383 (c-electric-backspace arg)))
384
385 (defun c-electric-pound (arg)
386 "Electric pound (`#') insertion.
387 Inserts a `#' character specially depending on the variable
388 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if
389 point is inside a literal or a macro, nothing special happens."
390 (interactive "*P")
391 (if (or arg
392 (not (memq 'alignleft c-electric-pound-behavior))
393 (save-excursion
394 (skip-chars-backward " \t")
395 (not (bolp)))
396 (save-excursion
397 (and (= (forward-line -1) 0)
398 (progn (end-of-line)
399 (eq (char-before) ?\\))))
400 (c-in-literal))
401 ;; do nothing special
402 (self-insert-command (prefix-numeric-value arg))
403 ;; place the pound character at the left edge
404 (let ((pos (- (point-max) (point)))
405 (bolp (bolp)))
406 (beginning-of-line)
407 (delete-horizontal-space)
408 (insert last-command-char)
409 (and (not bolp)
410 (goto-char (- (point-max) pos)))
411 )))
412
413 (defun c-electric-brace (arg)
414 "Insert a brace.
415
416 If the auto-newline feature is turned on, as evidenced by the \"/a\"
417 or \"/ah\" string on the mode line, newlines are inserted before and
418 after braces based on the value of `c-hanging-braces-alist'.
419
420 Also, the line is re-indented unless a numeric ARG is supplied, the
421 brace is inserted inside a literal, or `c-syntactic-indentation' is
422 nil.
423
424 This function does various newline cleanups based on the value of
425 `c-cleanup-list'."
426 (interactive "*P")
427 (let* ((safepos (c-safe-position (point) (c-parse-state)))
428 (literal (c-in-literal safepos))
429 ;; We want to inhibit blinking the paren since this will be
430 ;; most disruptive. We'll blink it ourselves later on.
431 (old-blink-paren blink-paren-function)
432 blink-paren-function)
433 (cond
434 ((or literal arg)
435 (self-insert-command (prefix-numeric-value arg)))
436 ((not (looking-at "[ \t]*\\\\?$"))
437 (self-insert-command (prefix-numeric-value arg))
438 (if c-syntactic-indentation
439 (indent-according-to-mode)))
440 (t
441 (let* ((syms
442 ;; This is the list of brace syntactic symbols that can
443 ;; hang. If any new ones are added to c-offsets-alist,
444 ;; they should be added here as well.
445 '(class-open class-close defun-open defun-close
446 inline-open inline-close
447 brace-list-open brace-list-close
448 brace-list-intro brace-entry-open
449 block-open block-close
450 substatement-open statement-case-open
451 extern-lang-open extern-lang-close
452 namespace-open namespace-close
453 module-open module-close
454 composition-open composition-close
455 inexpr-class-open inexpr-class-close
456 ;; `statement-cont' is here for the case with a brace
457 ;; list opener inside a statement. C.f. CASE B.2 in
458 ;; `c-guess-continued-construct'.
459 statement-cont))
460 (insertion-point (point))
461 (preserve-p (and (not (bobp))
462 (eq ?\ (char-syntax (char-before)))))
463 ;; shut this up too
464 (c-echo-syntactic-information-p nil)
465 delete-temp-newline syntax newlines)
466 ;; only insert a newline if there is non-whitespace behind us
467 (when (save-excursion
468 (skip-chars-backward " \t")
469 (not (bolp)))
470 (c-newline-and-indent)
471 ;; Set markers around the newline and indention inserted
472 ;; above. We insert the start marker here and not before
473 ;; the call to kludge around a misfeature in expand-abbrev:
474 ;; If the line contains e.g. "else" then expand-abbrev will
475 ;; be called when c-newline-and-indent inserts the newline.
476 ;; That function first removes the abbrev "else" and then
477 ;; inserts the expansion, which is an identical "else" in
478 ;; this case. So the marker that we put after "else" would
479 ;; end up before it.
480 (setq delete-temp-newline
481 (cons (save-excursion
482 (end-of-line 0)
483 (if (eq (char-before) ?\\)
484 ;; Ignore a line continuation.
485 (backward-char))
486 (skip-chars-backward " \t")
487 (copy-marker (point) t))
488 (point-marker))))
489 (unwind-protect
490 (progn
491 (if (eq last-command-char ?{)
492 (setq c-state-cache (cons (point) c-state-cache)))
493 (self-insert-command (prefix-numeric-value arg))
494 (c-save-buffer-state ((c-syntactic-indentation-in-macros t)
495 (c-auto-newline-analysis t))
496 ;; Turn on syntactic macro analysis to help with auto
497 ;; newlines only.
498 (setq syntax (c-guess-basic-syntax)))
499 (setq newlines
500 (and
501 c-auto-newline
502 (or (c-lookup-lists
503 syms
504 ;; Substitute inexpr-class and class-open or
505 ;; class-close with inexpr-class-open or
506 ;; inexpr-class-close.
507 (if (assq 'inexpr-class syntax)
508 (cond ((assq 'class-open syntax)
509 '((inexpr-class-open)))
510 ((assq 'class-close syntax)
511 '((inexpr-class-close)))
512 (t syntax))
513 syntax)
514 c-hanging-braces-alist)
515 '(ignore before after))))
516 ;; Do not try to insert newlines around a special
517 ;; (Pike-style) brace list.
518 (if (and c-special-brace-lists
519 (save-excursion
520 (c-save-buffer-state nil
521 (c-safe (if (= (char-before) ?{)
522 (forward-char -1)
523 (c-forward-sexp -1))
524 (c-looking-at-special-brace-list)))))
525 (setq newlines nil))
526 ;; If syntax is a function symbol, then call it using the
527 ;; defined semantics.
528 (if (and (not (consp (cdr newlines)))
529 (functionp (cdr newlines)))
530 (let ((c-syntactic-context syntax))
531 (setq newlines
532 (funcall (cdr newlines)
533 (car newlines)
534 insertion-point))))
535 ;; does a newline go before the open brace?
536 (when (memq 'before newlines)
537 ;; we leave the newline we've put in there before,
538 ;; but we need to re-indent the line above
539 (when delete-temp-newline
540 (set-marker (car delete-temp-newline) nil)
541 (set-marker (cdr delete-temp-newline) nil)
542 (setq delete-temp-newline nil))
543 (when c-syntactic-indentation
544 (let ((pos (- (point-max) (point)))
545 (here (point)))
546 (forward-line -1)
547 (indent-according-to-mode)
548 (goto-char (- (point-max) pos))
549 ;; if the buffer has changed due to the
550 ;; indentation, we need to recalculate syntax for
551 ;; the current line.
552 (if (/= (point) here)
553 (c-save-buffer-state
554 ((c-syntactic-indentation-in-macros t)
555 (c-auto-newline-analysis t))
556 ;; Turn on syntactic macro analysis to help
557 ;; with auto newlines only.
558 (setq syntax (c-guess-basic-syntax))))))))
559 ;; must remove the newline we just stuck in (if we really did it)
560 (when delete-temp-newline
561 (save-excursion
562 (delete-region (car delete-temp-newline)
563 (cdr delete-temp-newline))
564 (goto-char (car delete-temp-newline))
565 (set-marker (car delete-temp-newline) nil)
566 (set-marker (cdr delete-temp-newline) nil)
567 ;; if there is whitespace before point, then preserve
568 ;; at least one space.
569 (just-one-space)
570 (if (not preserve-p)
571 (delete-char -1)))))
572 (if (not (memq 'before newlines))
573 ;; since we're hanging the brace, we need to recalculate
574 ;; syntax.
575 (c-save-buffer-state ((c-syntactic-indentation-in-macros t)
576 (c-auto-newline-analysis t))
577 ;; Turn on syntactic macro analysis to help with auto
578 ;; newlines only.
579 (setq syntax (c-guess-basic-syntax))))
580 (when c-syntactic-indentation
581 ;; Now adjust the line's indentation. Don't update the state
582 ;; cache since c-guess-basic-syntax isn't called when
583 ;; c-syntactic-context is set.
584 (let* ((c-syntactic-context syntax))
585 (indent-according-to-mode)))
586 ;; Do all appropriate clean ups
587 (let ((here (point))
588 (pos (- (point-max) (point)))
589 mbeg mend tmp)
590 ;; clean up empty defun braces
591 (if (and c-auto-newline
592 (memq 'empty-defun-braces c-cleanup-list)
593 (eq last-command-char ?\})
594 (c-intersect-lists '(defun-close class-close inline-close)
595 syntax)
596 (progn
597 (forward-char -1)
598 (c-skip-ws-backward)
599 (eq (char-before) ?\{))
600 ;; make sure matching open brace isn't in a comment
601 (not (c-in-literal)))
602 (delete-region (point) (1- here)))
603 ;; clean up brace-else-brace and brace-elseif-brace
604 (when (and c-auto-newline
605 (eq last-command-char ?\{))
606 (cond
607 ((and (memq 'brace-else-brace c-cleanup-list)
608 (re-search-backward
609 (concat "}"
610 "\\([ \t\n]\\|\\\\\n\\)*"
611 "else"
612 "\\([ \t\n]\\|\\\\\n\\)*"
613 "{")
614 nil t)
615 (progn
616 (setq mbeg (match-beginning 0)
617 mend (match-end 0))
618 (eq (match-end 0) here)))
619 (delete-region mbeg mend)
620 (insert-and-inherit "} else {"))
621 ((and (memq 'brace-elseif-brace c-cleanup-list)
622 (progn
623 (goto-char (1- here))
624 (setq mend (point))
625 (c-skip-ws-backward)
626 (setq mbeg (point))
627 (eq (char-before) ?\)))
628 (zerop (c-save-buffer-state nil (c-backward-token-2 1 t)))
629 (eq (char-after) ?\()
630 (progn
631 (setq tmp (point))
632 (re-search-backward
633 (concat "}"
634 "\\([ \t\n]\\|\\\\\n\\)*"
635 "else"
636 "\\([ \t\n]\\|\\\\\n\\)+"
637 "if"
638 "\\([ \t\n]\\|\\\\\n\\)*")
639 nil t))
640 (eq (match-end 0) tmp))
641 (delete-region mbeg mend)
642 (goto-char mbeg)
643 (insert ?\ ))))
644 (goto-char (- (point-max) pos))
645 )
646 ;; does a newline go after the brace?
647 (if (memq 'after newlines)
648 (c-newline-and-indent))
649 )))
650 ;; blink the paren
651 (and (eq last-command-char ?\})
652 (not executing-kbd-macro)
653 old-blink-paren
654 (save-excursion
655 (c-save-buffer-state nil
656 (c-backward-syntactic-ws safepos))
657 (funcall old-blink-paren)))))
658
659 (defun c-electric-slash (arg)
660 "Insert a slash character.
661
662 Indent the line as a comment, if:
663
664 1. The slash is second of a `//' line oriented comment introducing
665 token and we are on a comment-only-line, or
666
667 2. The slash is part of a `*/' token that closes a block oriented
668 comment.
669
670 If a numeric ARG is supplied, point is inside a literal, or
671 `c-syntactic-indentation' is nil, indentation is inhibited."
672 (interactive "*P")
673 (let* ((ch (char-before))
674 (literal (c-in-literal))
675 (indentp (and c-syntactic-indentation
676 (not arg)
677 (eq last-command-char ?/)
678 (or (and (eq ch ?/)
679 (not literal))
680 (and (eq ch ?*)
681 literal))
682 ))
683 ;; shut this up
684 (c-echo-syntactic-information-p nil))
685 (self-insert-command (prefix-numeric-value arg))
686 (if indentp
687 (indent-according-to-mode))))
688
689 (defun c-electric-star (arg)
690 "Insert a star character.
691 If the star is the second character of a C style comment introducing
692 construct, and we are on a comment-only-line, indent line as comment.
693 If a numeric ARG is supplied, point is inside a literal, or
694 `c-syntactic-indentation' is nil, indentation is inhibited."
695 (interactive "*P")
696 (self-insert-command (prefix-numeric-value arg))
697 ;; if we are in a literal, or if arg is given do not re-indent the
698 ;; current line, unless this star introduces a comment-only line.
699 (if (and c-syntactic-indentation
700 (not arg)
701 (eq (c-in-literal) 'c)
702 (eq (char-before) ?*)
703 (save-excursion
704 (forward-char -1)
705 (skip-chars-backward "*")
706 (if (eq (char-before) ?/)
707 (forward-char -1))
708 (skip-chars-backward " \t")
709 (bolp)))
710 (let (c-echo-syntactic-information-p) ; shut this up
711 (indent-according-to-mode))
712 ))
713
714 (defun c-electric-semi&comma (arg)
715 "Insert a comma or semicolon.
716 When the auto-newline feature is turned on, as evidenced by the \"/a\"
717 or \"/ah\" string on the mode line, a newline might be inserted. See
718 the variable `c-hanging-semi&comma-criteria' for how newline insertion
719 is determined.
720
721 When a semicolon is inserted, the line is re-indented unless a numeric
722 arg is supplied, point is inside a literal, or
723 `c-syntactic-indentation' is nil.
724
725 Based on the value of `c-cleanup-list', this function cleans up commas
726 following brace lists and semicolons following defuns."
727 (interactive "*P")
728 (let* ((lim (c-most-enclosing-brace (c-parse-state)))
729 (literal (c-in-literal lim))
730 (here (point))
731 ;; shut this up
732 (c-echo-syntactic-information-p nil))
733 (if (or literal arg)
734 (self-insert-command (prefix-numeric-value arg))
735 ;; do some special stuff with the character
736 (self-insert-command (prefix-numeric-value arg))
737 ;; do all cleanups and newline insertions if c-auto-newline is
738 ;; turned on
739 (if (or (not c-auto-newline)
740 (not (looking-at "[ \t]*\\\\?$")))
741 (if c-syntactic-indentation
742 (indent-according-to-mode))
743 ;; clean ups
744 (let ((pos (- (point-max) (point))))
745 (if (and (or (and
746 (eq last-command-char ?,)
747 (memq 'list-close-comma c-cleanup-list))
748 (and
749 (eq last-command-char ?\;)
750 (memq 'defun-close-semi c-cleanup-list)))
751 (progn
752 (forward-char -1)
753 (c-skip-ws-backward)
754 (eq (char-before) ?}))
755 ;; make sure matching open brace isn't in a comment
756 (not (c-in-literal lim)))
757 (delete-region (point) here))
758 (goto-char (- (point-max) pos)))
759 ;; re-indent line
760 (if c-syntactic-indentation
761 (indent-according-to-mode))
762 ;; check to see if a newline should be added
763 (let ((criteria c-hanging-semi&comma-criteria)
764 answer add-newline-p)
765 (while criteria
766 (setq answer (funcall (car criteria)))
767 ;; only nil value means continue checking
768 (if (not answer)
769 (setq criteria (cdr criteria))
770 (setq criteria nil)
771 ;; only 'stop specifically says do not add a newline
772 (setq add-newline-p (not (eq answer 'stop)))
773 ))
774 (if add-newline-p
775 (c-newline-and-indent))
776 )))))
777
778 (defun c-electric-colon (arg)
779 "Insert a colon.
780
781 If the auto-newline feature is turned on, as evidenced by the \"/a\"
782 or \"/ah\" string on the mode line, newlines are inserted before and
783 after colons based on the value of `c-hanging-colons-alist'.
784
785 Also, the line is re-indented unless a numeric ARG is supplied, the
786 colon is inserted inside a literal, or `c-syntactic-indentation' is
787 nil.
788
789 This function cleans up double colon scope operators based on the
790 value of `c-cleanup-list'."
791 (interactive "*P")
792 (let* ((bod (c-point 'bod))
793 (literal (c-in-literal bod))
794 newlines is-scope-op
795 ;; shut this up
796 (c-echo-syntactic-information-p nil))
797 (cond
798 ((or literal arg)
799 (self-insert-command (prefix-numeric-value arg)))
800 ((not (looking-at "[ \t]*\\\\?$"))
801 (self-insert-command (prefix-numeric-value arg))
802 (if c-syntactic-indentation
803 (indent-according-to-mode)))
804 (t
805 ;; insert the colon, then do any specified cleanups
806 (self-insert-command (prefix-numeric-value arg))
807 (let ((pos (- (point-max) (point)))
808 (here (point)))
809 (if (and c-auto-newline
810 (memq 'scope-operator c-cleanup-list)
811 (eq (char-before) ?:)
812 (progn
813 (forward-char -1)
814 (c-skip-ws-backward)
815 (eq (char-before) ?:))
816 (not (c-in-literal))
817 (not (eq (char-after (- (point) 2)) ?:)))
818 (progn
819 (delete-region (point) (1- here))
820 (setq is-scope-op t)))
821 (goto-char (- (point-max) pos)))
822 ;; indent the current line if it's done syntactically.
823 (if c-syntactic-indentation
824 ;; Cannot use the same syntax analysis as we find below,
825 ;; since that's made with c-syntactic-indentation-in-macros
826 ;; always set to t.
827 (indent-according-to-mode))
828 (c-save-buffer-state
829 ((c-syntactic-indentation-in-macros t)
830 (c-auto-newline-analysis t)
831 ;; Turn on syntactic macro analysis to help with auto newlines
832 ;; only.
833 (syntax (c-guess-basic-syntax))
834 (elem syntax))
835 ;; Translate substatement-label to label for this operation.
836 (while elem
837 (if (eq (car (car elem)) 'substatement-label)
838 (setcar (car elem) 'label))
839 (setq elem (cdr elem)))
840 ;; some language elements can only be determined by checking
841 ;; the following line. Lets first look for ones that can be
842 ;; found when looking on the line with the colon
843 (setq newlines
844 (and c-auto-newline
845 (or (c-lookup-lists '(case-label label access-label)
846 syntax c-hanging-colons-alist)
847 (c-lookup-lists '(member-init-intro inher-intro)
848 (progn
849 (insert ?\n)
850 (unwind-protect
851 (c-guess-basic-syntax)
852 (delete-char -1)))
853 c-hanging-colons-alist)))))
854 ;; does a newline go before the colon? Watch out for already
855 ;; non-hung colons. However, we don't unhang them because that
856 ;; would be a cleanup (and anti-social).
857 (if (and (memq 'before newlines)
858 (not is-scope-op)
859 (save-excursion
860 (skip-chars-backward ": \t")
861 (not (bolp))))
862 (let ((pos (- (point-max) (point))))
863 (forward-char -1)
864 (c-newline-and-indent)
865 (goto-char (- (point-max) pos))))
866 ;; does a newline go after the colon?
867 (if (and (memq 'after (cdr-safe newlines))
868 (not is-scope-op))
869 (c-newline-and-indent))
870 ))))
871
872 (defun c-electric-lt-gt (arg)
873 "Insert a less-than, or greater-than character.
874 The line will be re-indented if the character inserted is the second
875 of a C++ style stream operator and the buffer is in C++ mode.
876 Exceptions are when a numeric argument is supplied, point is inside a
877 literal, or `c-syntactic-indentation' is nil, in which case the line
878 will not be re-indented."
879 (interactive "*P")
880 (let ((indentp (and c-syntactic-indentation
881 (not arg)
882 (eq (char-before) last-command-char)
883 (not (c-in-literal))))
884 ;; shut this up
885 (c-echo-syntactic-information-p nil))
886 (self-insert-command (prefix-numeric-value arg))
887 (if indentp
888 (indent-according-to-mode))))
889
890 (defun c-electric-paren (arg)
891 "Insert a parenthesis.
892
893 Some newline cleanups are done if appropriate; see the variable
894 `c-cleanup-list'.
895
896 Also, the line is re-indented unless a numeric ARG is supplied, the
897 parenthesis is inserted inside a literal, or `c-syntactic-indentation'
898 is nil."
899 (interactive "*P")
900 (let ((literal (c-in-literal (c-point 'bod)))
901 ;; shut this up
902 (c-echo-syntactic-information-p nil))
903 (if (or arg literal)
904 (self-insert-command (prefix-numeric-value arg))
905 ;; do some special stuff with the character
906 (let* (;; We want to inhibit blinking the paren since this will
907 ;; be most disruptive. We'll blink it ourselves
908 ;; afterwards.
909 (old-blink-paren blink-paren-function)
910 blink-paren-function)
911 (self-insert-command (prefix-numeric-value arg))
912 (if c-syntactic-indentation
913 (indent-according-to-mode))
914 (when (looking-at "[ \t]*\\\\?$")
915 (when c-auto-newline
916 ;; Do all appropriate clean ups
917 (let ((here (point))
918 (pos (- (point-max) (point)))
919 mbeg mend)
920 ;; clean up brace-elseif-brace
921 (if (and (memq 'brace-elseif-brace c-cleanup-list)
922 (eq last-command-char ?\()
923 (re-search-backward
924 (concat "}"
925 "\\([ \t\n]\\|\\\\\n\\)*"
926 "else"
927 "\\([ \t\n]\\|\\\\\n\\)+"
928 "if"
929 "\\([ \t\n]\\|\\\\\n\\)*"
930 "(")
931 nil t)
932 (save-excursion
933 (setq mbeg (match-beginning 0)
934 mend (match-end 0))
935 (= mend here))
936 (not (c-in-literal)))
937 (progn
938 (delete-region mbeg mend)
939 (insert-and-inherit "} else if ("))
940 ;; clean up brace-catch-brace
941 (goto-char here)
942 (if (and (memq 'brace-catch-brace c-cleanup-list)
943 (eq last-command-char ?\()
944 (re-search-backward
945 (concat "}"
946 "\\([ \t\n]\\|\\\\\n\\)*"
947 "catch"
948 "\\([ \t\n]\\|\\\\\n\\)*"
949 "(")
950 nil t)
951 (save-excursion
952 (setq mbeg (match-beginning 0)
953 mend (match-end 0))
954 (= mend here))
955 (not (c-in-literal)))
956 (progn
957 (delete-region mbeg mend)
958 (insert-and-inherit "} catch ("))))
959 (goto-char (- (point-max) pos))
960 )))
961 (let (beg (end (1- (point))))
962 (cond ((and (memq 'space-before-funcall c-cleanup-list)
963 (eq last-command-char ?\()
964 (save-excursion
965 (backward-char)
966 (skip-chars-backward " \t")
967 (setq beg (point))
968 (c-on-identifier)))
969 (save-excursion
970 (delete-region beg end)
971 (goto-char beg)
972 (insert ?\ )))
973 ((and (memq 'compact-empty-funcall c-cleanup-list)
974 (eq last-command-char ?\))
975 (save-excursion
976 (c-safe (backward-char 2))
977 (when (looking-at "()")
978 (setq end (point))
979 (skip-chars-backward " \t")
980 (setq beg (point))
981 (c-on-identifier))))
982 (delete-region beg end))))
983 (and (not executing-kbd-macro)
984 old-blink-paren
985 (funcall old-blink-paren))))))
986
987 (defun c-electric-continued-statement ()
988 "Reindent the current line if appropriate.
989
990 This function is used to reindent the line after a keyword which
991 continues an earlier statement is typed, e.g. an \"else\" or the
992 \"while\" in a do-while block.
993
994 The line is reindented if there is nothing but whitespace before the
995 keyword on the line, the keyword is not inserted inside a literal, and
996 `c-syntactic-indentation' is non-nil."
997 (let (;; shut this up
998 (c-echo-syntactic-information-p nil))
999 (when (and c-syntactic-indentation
1000 (not (eq last-command-char ?_))
1001 (= (save-excursion
1002 (skip-syntax-backward "w")
1003 (point))
1004 (c-point 'boi))
1005 (not (c-in-literal (c-point 'bod))))
1006 ;; Have to temporarily insert a space so that
1007 ;; c-guess-basic-syntax recognizes the keyword. Follow the
1008 ;; space with a nonspace to avoid messing up any whitespace
1009 ;; sensitive meddling that might be done, e.g. by
1010 ;; `c-backslash-region'.
1011 (insert-and-inherit " x")
1012 (unwind-protect
1013 (indent-according-to-mode)
1014 (delete-char -2)))))
1015
1016 \f
1017 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus
1018 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com
1019 (defun c-forward-into-nomenclature (&optional arg)
1020 "Move forward to end of a nomenclature section or word.
1021 With arg, do it arg times."
1022 (interactive "p")
1023 (let ((case-fold-search nil))
1024 (if (> arg 0)
1025 (re-search-forward
1026 (cc-eval-when-compile
1027 (concat "\\W*\\([" c-upper "]*[" c-lower c-digit "]*\\)"))
1028 (point-max) t arg)
1029 (while (and (< arg 0)
1030 (re-search-backward
1031 (cc-eval-when-compile
1032 (concat
1033 "\\(\\(\\W\\|[" c-lower c-digit "]\\)[" c-upper "]+"
1034 "\\|\\W\\w+\\)"))
1035 (point-min) 0))
1036 (forward-char 1)
1037 (setq arg (1+ arg)))))
1038 (c-keep-region-active))
1039
1040 (defun c-backward-into-nomenclature (&optional arg)
1041 "Move backward to beginning of a nomenclature section or word.
1042 With optional ARG, move that many times. If ARG is negative, move
1043 forward."
1044 (interactive "p")
1045 (c-forward-into-nomenclature (- arg))
1046 (c-keep-region-active))
1047
1048 (defun c-scope-operator ()
1049 "Insert a double colon scope operator at point.
1050 No indentation or other \"electric\" behavior is performed."
1051 (interactive "*")
1052 (insert-and-inherit "::"))
1053
1054 (defun c-beginning-of-defun (&optional arg)
1055 "Move backward to the beginning of a defun.
1056 Every top level declaration that contains a brace paren block is
1057 considered to be a defun.
1058
1059 With a positive argument, move backward that many defuns. A negative
1060 argument -N means move forward to the Nth following beginning. Return
1061 t unless search stops due to beginning or end of buffer.
1062
1063 Unlike the built-in `beginning-of-defun' this tries to be smarter
1064 about finding the char with open-parenthesis syntax that starts the
1065 defun."
1066
1067 (interactive "p")
1068 (or arg (setq arg 1))
1069
1070 (if (< arg 0)
1071 (when (c-end-of-defun (- arg))
1072 (c-save-buffer-state nil (c-forward-syntactic-ws))
1073 t)
1074
1075 (c-save-buffer-state (paren-state lim pos)
1076 (catch 'exit
1077 (while (> arg 0)
1078 ;; Note: Partial code duplication in `c-end-of-defun' and
1079 ;; `c-declaration-limits'.
1080
1081 (setq paren-state (c-parse-state))
1082 (unless (c-safe
1083 (goto-char (c-least-enclosing-brace paren-state))
1084 ;; If we moved to the outermost enclosing paren
1085 ;; then we can use c-safe-position to set the
1086 ;; limit. Can't do that otherwise since the
1087 ;; earlier paren pair on paren-state might very
1088 ;; well be part of the declaration we should go
1089 ;; to.
1090 (setq lim (c-safe-position (point) paren-state))
1091 t)
1092 ;; At top level. Make sure we aren't inside a literal.
1093 (setq pos (c-literal-limits
1094 (c-safe-position (point) paren-state)))
1095 (if pos (goto-char (car pos))))
1096
1097 (while (let ((start (point)))
1098 (c-beginning-of-decl-1 lim)
1099 (if (= (point) start)
1100 ;; Didn't move. Might be due to bob or unbalanced
1101 ;; parens. Try to continue if it's the latter.
1102 (unless (c-safe (goto-char
1103 (c-down-list-backward (point))))
1104 ;; Didn't work, so it's bob then.
1105 (goto-char (point-min))
1106 (throw 'exit nil)))
1107
1108 (save-excursion
1109 ;; Check if the declaration contains a brace
1110 ;; block. If not, we try another one.
1111 (setq pos (point))
1112 (not (and (c-syntactic-re-search-forward "[;{]" nil t t)
1113 (or (eq (char-before) ?{)
1114 (and c-recognize-knr-p
1115 ;; Might have stopped on the
1116 ;; ';' in a K&R argdecl. In
1117 ;; that case the declaration
1118 ;; should contain a block.
1119 (c-in-knr-argdecl pos)))))))
1120 (setq lim nil))
1121
1122 ;; Check if `c-beginning-of-decl-1' put us after the block
1123 ;; in a declaration that doesn't end there. We're searching
1124 ;; back and forth over the block here, which can be
1125 ;; expensive.
1126 (setq pos (point))
1127 (if (and c-opt-block-decls-with-vars-key
1128 (progn
1129 (c-backward-syntactic-ws)
1130 (eq (char-before) ?}))
1131 (eq (car (c-beginning-of-decl-1))
1132 'previous)
1133 (save-excursion
1134 (c-end-of-decl-1)
1135 (> (point) pos)))
1136 nil
1137 (goto-char pos))
1138
1139 (setq pos (point))
1140 ;; Try to be line oriented; position point at the closest
1141 ;; preceding boi that isn't inside a comment, but if we hit
1142 ;; the previous declaration then we use the current point
1143 ;; instead.
1144 (while (and (/= (point) (c-point 'boi))
1145 (c-backward-single-comment)))
1146 (if (/= (point) (c-point 'boi))
1147 (goto-char pos))
1148
1149 (setq arg (1- arg)))))
1150 (c-keep-region-active)
1151 (= arg 0)))
1152
1153 (defun c-end-of-defun (&optional arg)
1154 "Move forward to the end of a top level declaration.
1155 With argument, do it that many times. Negative argument -N means move
1156 back to Nth preceding end. Returns t unless search stops due to
1157 beginning or end of buffer.
1158
1159 An end of a defun occurs right after the close-parenthesis that matches
1160 the open-parenthesis that starts a defun; see `beginning-of-defun'."
1161
1162 (interactive "p")
1163 (or arg (setq arg 1))
1164
1165 (if (< arg 0)
1166 (when (c-beginning-of-defun (- arg))
1167 (c-save-buffer-state nil (c-backward-syntactic-ws))
1168 t)
1169
1170 (c-save-buffer-state (paren-state lim pos)
1171 (catch 'exit
1172 (while (> arg 0)
1173 ;; Note: Partial code duplication in `c-beginning-of-defun'
1174 ;; and `c-declaration-limits'.
1175
1176 (setq paren-state (c-parse-state))
1177 (unless (c-safe
1178 (goto-char (c-least-enclosing-brace paren-state))
1179 ;; If we moved to the outermost enclosing paren
1180 ;; then we can use c-safe-position to set the
1181 ;; limit. Can't do that otherwise since the
1182 ;; earlier paren pair on paren-state might very
1183 ;; well be part of the declaration we should go
1184 ;; to.
1185 (setq lim (c-safe-position (point) paren-state))
1186 t)
1187 ;; At top level. Make sure we aren't inside a literal.
1188 (setq pos (car-safe (c-literal-limits
1189 (c-safe-position (point) paren-state))))
1190 (if pos (goto-char pos)))
1191
1192 ;; Have to move to the start first so that `c-end-of-decl-1'
1193 ;; has the correct start position.
1194 (setq pos (point))
1195 (when (memq (car (c-beginning-of-decl-1 lim))
1196 '(previous macro))
1197 ;; We moved back over the previous defun or a macro. Move
1198 ;; to the next token; it's the start of the next
1199 ;; declaration. We can also be directly after the block
1200 ;; in a `c-opt-block-decls-with-vars-key' declaration, but
1201 ;; then we won't move significantly far here.
1202 (goto-char pos)
1203 (c-forward-token-2 0))
1204
1205 (while (let ((start (point)))
1206 (c-end-of-decl-1)
1207 (if (= (point) start)
1208 ;; Didn't move. Might be due to eob or unbalanced
1209 ;; parens. Try to continue if it's the latter.
1210 (if (c-safe (goto-char (c-up-list-forward (point))))
1211 t
1212 ;; Didn't work, so it's eob then.
1213 (goto-char (point-max))
1214 (throw 'exit nil))
1215
1216 (save-excursion
1217 ;; Check if the declaration contains a brace
1218 ;; block. If not, we try another one.
1219 (setq pos (point))
1220 (goto-char start)
1221 (not (c-syntactic-re-search-forward "{" pos t t))))))
1222
1223 (setq pos (point))
1224 ;; Try to be line oriented; position point after the next
1225 ;; newline that isn't inside a comment, but if we hit the
1226 ;; next declaration then we use the current point instead.
1227 (while (and (not (bolp))
1228 (not (looking-at "\\s *$"))
1229 (c-forward-single-comment)))
1230 (cond ((bolp))
1231 ((looking-at "\\s *$")
1232 (forward-line 1))
1233 (t
1234 (goto-char pos)))
1235
1236 (setq arg (1- arg)))))
1237 (c-keep-region-active)
1238 (= arg 0)))
1239
1240 (defun c-declaration-limits (near)
1241 ;; Return a cons of the beginning and end positions of the current
1242 ;; top level declaration or macro. If point is not inside any then
1243 ;; nil is returned, unless NEAR is non-nil in which case the closest
1244 ;; following one is chosen instead (if there is any). The end
1245 ;; position is at the next line, providing there is one before the
1246 ;; declaration.
1247 (save-excursion
1248
1249 ;; Note: Some code duplication in `c-beginning-of-defun' and
1250 ;; `c-end-of-defun'.
1251 (catch 'exit
1252 (let ((start (point))
1253 (paren-state (c-parse-state))
1254 lim pos end-pos)
1255 (unless (c-safe
1256 (goto-char (c-least-enclosing-brace paren-state))
1257 ;; If we moved to the outermost enclosing paren then we
1258 ;; can use c-safe-position to set the limit. Can't do
1259 ;; that otherwise since the earlier paren pair on
1260 ;; paren-state might very well be part of the
1261 ;; declaration we should go to.
1262 (setq lim (c-safe-position (point) paren-state))
1263 t)
1264 ;; At top level. Make sure we aren't inside a literal.
1265 (setq pos (c-literal-limits
1266 (c-safe-position (point) paren-state)))
1267 (if pos (goto-char (car pos))))
1268
1269 (when (c-beginning-of-macro)
1270 (throw 'exit
1271 (cons (point)
1272 (save-excursion
1273 (c-end-of-macro)
1274 (forward-line 1)
1275 (point)))))
1276
1277 (setq pos (point))
1278 (when (or (eq (car (c-beginning-of-decl-1 lim)) 'previous)
1279 (= pos (point)))
1280 ;; We moved back over the previous defun. Skip to the next
1281 ;; one. Not using c-forward-syntactic-ws here since we
1282 ;; should not skip a macro. We can also be directly after
1283 ;; the block in a `c-opt-block-decls-with-vars-key'
1284 ;; declaration, but then we won't move significantly far
1285 ;; here.
1286 (goto-char pos)
1287 (c-forward-comments)
1288
1289 (when (and near (c-beginning-of-macro))
1290 (throw 'exit
1291 (cons (point)
1292 (save-excursion
1293 (c-end-of-macro)
1294 (forward-line 1)
1295 (point))))))
1296
1297 (if (eobp) (throw 'exit nil))
1298
1299 ;; Check if `c-beginning-of-decl-1' put us after the block in a
1300 ;; declaration that doesn't end there. We're searching back and
1301 ;; forth over the block here, which can be expensive.
1302 (setq pos (point))
1303 (if (and c-opt-block-decls-with-vars-key
1304 (progn
1305 (c-backward-syntactic-ws)
1306 (eq (char-before) ?}))
1307 (eq (car (c-beginning-of-decl-1))
1308 'previous)
1309 (save-excursion
1310 (c-end-of-decl-1)
1311 (and (> (point) pos)
1312 (setq end-pos (point)))))
1313 nil
1314 (goto-char pos))
1315
1316 (if (and (not near) (> (point) start))
1317 nil
1318
1319 ;; Try to be line oriented; position the limits at the
1320 ;; closest preceding boi, and after the next newline, that
1321 ;; isn't inside a comment, but if we hit a neighboring
1322 ;; declaration then we instead use the exact declaration
1323 ;; limit in that direction.
1324 (cons (progn
1325 (setq pos (point))
1326 (while (and (/= (point) (c-point 'boi))
1327 (c-backward-single-comment)))
1328 (if (/= (point) (c-point 'boi))
1329 pos
1330 (point)))
1331 (progn
1332 (if end-pos
1333 (goto-char end-pos)
1334 (c-end-of-decl-1))
1335 (setq pos (point))
1336 (while (and (not (bolp))
1337 (not (looking-at "\\s *$"))
1338 (c-forward-single-comment)))
1339 (cond ((bolp)
1340 (point))
1341 ((looking-at "\\s *$")
1342 (forward-line 1)
1343 (point))
1344 (t
1345 pos)))))
1346 ))))
1347
1348 (defun c-mark-function ()
1349 "Put mark at end of the current top-level declaration or macro, point at beginning.
1350 If point is not inside any then the closest following one is chosen.
1351
1352 As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
1353 function does not require the declaration to contain a brace block."
1354 (interactive)
1355
1356 (let (decl-limits)
1357 (c-save-buffer-state nil
1358 ;; We try to be line oriented, unless there are several
1359 ;; declarations on the same line.
1360 (if (looking-at c-syntactic-eol)
1361 (c-backward-token-2 1 nil (c-point 'bol)))
1362 (setq decl-limits (c-declaration-limits t)))
1363
1364 (if (not decl-limits)
1365 (error "Cannot find any declaration")
1366 (goto-char (car decl-limits))
1367 (push-mark (cdr decl-limits) nil t))))
1368
1369 \f
1370 (defun c-beginning-of-statement (&optional count lim sentence-flag)
1371 "Go to the beginning of the innermost C statement.
1372 With prefix arg, go back N - 1 statements. If already at the
1373 beginning of a statement then go to the beginning of the closest
1374 preceding one, moving into nested blocks if necessary (use
1375 \\[backward-sexp] to skip over a block). If within or next to a
1376 comment or multiline string, move by sentences instead of statements.
1377
1378 When called from a program, this function takes 3 optional args: the
1379 repetition count, a buffer position limit which is the farthest back
1380 to search for the syntactic context, and a flag saying whether to do
1381 sentence motion in or near comments and multiline strings.
1382
1383 Note that `c-beginning-of-statement-1' is usually better to use from
1384 programs. It has much more well defined semantics than this one,
1385 which is intended for interactive use and might therefore change to be
1386 more \"DWIM:ey\"."
1387 (interactive (list (prefix-numeric-value current-prefix-arg)
1388 nil t))
1389 (c-save-buffer-state
1390 ((count (or count 1))
1391 here
1392 (range (c-collect-line-comments (c-literal-limits lim))))
1393 (while (and (/= count 0)
1394 (or (not lim) (> (point) lim)))
1395 (setq here (point))
1396 (if (and (not range) sentence-flag)
1397 (save-excursion
1398 ;; Find the comment next to point if we're not in one.
1399 (if (> count 0)
1400 (if (c-backward-single-comment)
1401 (setq range (cons (point)
1402 (progn (c-forward-single-comment)
1403 (point))))
1404 (c-skip-ws-backward)
1405 (setq range (point))
1406 (setq range
1407 (if (eq (char-before) ?\")
1408 (c-safe (c-backward-sexp 1)
1409 (cons (point) range)))))
1410 (c-skip-ws-forward)
1411 (if (eq (char-after) ?\")
1412 (setq range (cons (point)
1413 (progn
1414 (c-forward-sexp 1)
1415 (point))))
1416 (setq range (point))
1417 (setq range (if (c-forward-single-comment)
1418 (cons range (point))
1419 nil))))
1420 (setq range (c-collect-line-comments range))))
1421 (if (and (< count 0) (= here (point-max)))
1422 ;; Special case because eob might be in a literal.
1423 (setq range nil))
1424 (if range
1425 (if (and sentence-flag
1426 (or (/= (char-syntax (char-after (car range))) ?\")
1427 ;; Only visit a string if it spans more than one line.
1428 (save-excursion
1429 (goto-char (car range))
1430 (skip-chars-forward "^\n" (cdr range))
1431 (< (point) (cdr range)))))
1432 (let* ((lit-type (c-literal-type range))
1433 (line-prefix (concat "[ \t]*\\("
1434 c-current-comment-prefix
1435 "\\)[ \t]*"))
1436 (beg (if (eq lit-type 'string)
1437 (1+ (car range))
1438 (save-excursion
1439 (goto-char (car range))
1440 (max (progn
1441 (looking-at comment-start-skip)
1442 (match-end 0))
1443 (progn
1444 (looking-at line-prefix)
1445 (match-end 0))))))
1446 (end (- (cdr range) (if (eq lit-type 'c) 2 1)))
1447 (beg-of-para (if (eq lit-type 'string)
1448 (lambda ())
1449 (lambda ()
1450 (beginning-of-line)
1451 (if (looking-at line-prefix)
1452 (goto-char (match-end 0)))))))
1453 (save-restriction
1454 ;; Move by sentence, but not past the limit of the
1455 ;; literal, narrowed to the appropriate
1456 ;; paragraph(s).
1457 (narrow-to-region (save-excursion
1458 (let ((pos (min here end)))
1459 (goto-char pos)
1460 (forward-paragraph -1)
1461 (if (looking-at paragraph-separate)
1462 (forward-line))
1463 (when (> (point) beg)
1464 (funcall beg-of-para)
1465 (when (>= (point) pos)
1466 (forward-paragraph -2)
1467 (funcall beg-of-para)))
1468 (max (point) beg)))
1469 end)
1470 (c-safe (forward-sentence (if (< count 0) 1 -1)))
1471 (if (and (memq lit-type '(c c++))
1472 ;; Check if we stopped due to a comment
1473 ;; prefix and not a sentence end.
1474 (/= (point) (point-min))
1475 (/= (point) (point-max))
1476 (save-excursion
1477 (beginning-of-line)
1478 (looking-at line-prefix))
1479 (>= (point) (match-beginning 0))
1480 (/= (match-beginning 1) (match-end 1))
1481 (or (< (point) (match-end 0))
1482 (and
1483 (= (point) (match-end 0))
1484 ;; The comment prefix may contain
1485 ;; characters that is regarded as end
1486 ;; of sentence.
1487 (or (eolp)
1488 (and
1489 (save-excursion
1490 (forward-paragraph -1)
1491 (< (point) (match-beginning 0)))
1492 (save-excursion
1493 (beginning-of-line)
1494 (or (not (re-search-backward
1495 (sentence-end)
1496 (c-point 'bopl)
1497 t))
1498 (< (match-end 0)
1499 (c-point 'eol)))))))))
1500 (setq count (+ count (if (< count 0) -1 1)))
1501 (if (< count 0)
1502 (progn
1503 ;; In block comments, if there's only
1504 ;; horizontal ws between the text and the
1505 ;; comment ender, stop before it. Stop after
1506 ;; the ender if there's either nothing or
1507 ;; newlines between.
1508 (when (and (eq lit-type 'c)
1509 (eq (point) (point-max)))
1510 (widen)
1511 (when (or (= (skip-chars-backward " \t") 0)
1512 (eq (point) (point-max))
1513 (bolp))
1514 (goto-char (cdr range)))))
1515 (when (and (eq (point) (point-min))
1516 (looking-at "[ \t]*\\\\?$"))
1517 ;; Stop before instead of after the comment
1518 ;; starter if nothing follows it.
1519 (widen)
1520 (goto-char (car range))
1521 (if (and (eq lit-type 'string) (/= (point) here))
1522 (setq count (1+ count)
1523 range nil))))))
1524 ;; See if we should escape the literal.
1525 (if (> count 0)
1526 (if (< (point) here)
1527 (setq count (1- count))
1528 (goto-char (car range))
1529 (setq range nil))
1530 (if (> (point) here)
1531 (setq count (1+ count))
1532 (goto-char (cdr range))
1533 (setq range nil))))
1534 (goto-char (if (> count 0) (car range) (cdr range)))
1535 (setq range nil))
1536 (goto-char here)
1537 (if (> count 0)
1538 (condition-case nil
1539 ;; Stop before `{' and after `;', `{', `}' and `};'
1540 ;; when not followed by `}' or `)', but on the other
1541 ;; side of the syntactic ws. Move by sexps and move
1542 ;; into parens. Also stop before `#' when it's at boi
1543 ;; on a line.
1544 (let ((literal-pos (not sentence-flag))
1545 last last-below-line)
1546 (catch 'done
1547 (while t
1548 (setq last (point))
1549 (when (and (or (eq (char-after) ?\{)
1550 (and (eq (char-after) ?#)
1551 (eq (point) (c-point 'boi)))
1552 )
1553 (/= here last))
1554 (unless (and c-special-brace-lists
1555 (eq (char-after) ?{)
1556 (c-looking-at-special-brace-list))
1557 (if (and (eq (char-after) ?#)
1558 (numberp last-below-line)
1559 (not (eq last-below-line here)))
1560 (goto-char last-below-line))
1561 (throw 'done t)))
1562 ;; Don't know why I added the following, but it
1563 ;; doesn't work when point is preceded by a line
1564 ;; style comment. /mast
1565 ;;(c-skip-ws-backward)
1566 (if literal-pos
1567 (c-backward-comments)
1568 (when (c-backward-single-comment)
1569 ;; Record position of first comment.
1570 (save-excursion
1571 (c-forward-single-comment)
1572 (setq literal-pos (point)))
1573 (c-backward-comments)))
1574 (unless last-below-line
1575 (if (save-excursion
1576 (re-search-forward "\\(^\\|[^\\]\\)$" last t))
1577 (setq last-below-line last)))
1578 (cond ((bobp) ; Must handle bob specially.
1579 (if (= here last)
1580 (throw 'done t)
1581 (goto-char last)
1582 (throw 'done t)))
1583 ((progn (backward-char)
1584 (looking-at "[;{}]"))
1585 (if (and c-special-brace-lists
1586 (eq (char-after) ?{)
1587 (c-looking-at-special-brace-list))
1588 (skip-syntax-backward "w_") ; Speedup only.
1589 (if (or (= here last)
1590 (memq (char-after last) '(?\) ?})))
1591 (if (and (eq (char-before) ?})
1592 (eq (char-after) ?\;))
1593 (backward-char))
1594 (goto-char last)
1595 (throw 'done t))))
1596 ((= (char-syntax (char-after)) ?\")
1597 (let ((end (point)))
1598 (forward-char)
1599 (c-backward-sexp)
1600 (save-excursion
1601 (skip-chars-forward "^\n" end)
1602 (when (< (point) end)
1603 ;; Break at multiline string.
1604 (setq literal-pos (1+ end))
1605 (throw 'done t)))))
1606 (t (skip-syntax-backward "w_")) ; Speedup only.
1607 )))
1608 (if (and (numberp literal-pos)
1609 (< (point) literal-pos))
1610 ;; We jumped over a comment or string that
1611 ;; should be investigated.
1612 (goto-char literal-pos)
1613 (setq count (1- count))))
1614 (error
1615 (goto-char (point-min))
1616 (setq count 0)))
1617 (condition-case nil
1618 ;; Stop before `{', `}', and `#' when it's at boi on a
1619 ;; line, but on the other side of the syntactic ws, and
1620 ;; after `;', `}' and `};'. Only stop before `{' if at
1621 ;; top level or inside braces, though. Move by sexps
1622 ;; and move into parens. Also stop at eol of lines
1623 ;; with `#' at the boi.
1624 (let ((literal-pos (not sentence-flag))
1625 last)
1626 (catch 'done
1627 (while t
1628 (setq last (point))
1629 (if literal-pos
1630 (c-forward-comments)
1631 (if (progn
1632 (c-skip-ws-forward)
1633 ;; Record position of first comment.
1634 (setq literal-pos (point))
1635 (c-forward-single-comment))
1636 (c-forward-comments)
1637 (setq literal-pos nil)))
1638 (cond ((and (eq (char-after) ?{)
1639 (not (and c-special-brace-lists
1640 (c-looking-at-special-brace-list)))
1641 (/= here last)
1642 (save-excursion
1643 (or (not (c-safe (up-list -1) t))
1644 (= (char-after) ?{))))
1645 (goto-char last)
1646 (throw 'done t))
1647 ((and c-special-brace-lists
1648 (eq (char-after) ?})
1649 (save-excursion
1650 (and (c-safe (up-list -1) t)
1651 (c-looking-at-special-brace-list))))
1652 (forward-char 1)
1653 (skip-syntax-forward "w_")) ; Speedup only.
1654 ((and (eq (char-after) ?})
1655 (/= here last))
1656 (goto-char last)
1657 (throw 'done t))
1658 ; ((and (eq (char-after) ?#)
1659 ; (= (point) (c-point 'boi)))
1660 ; (if (= here last)
1661 ; (or (re-search-forward "\\(^\\|[^\\]\\)$" nil t)
1662 ; (goto-char (point-max)))
1663 ; (goto-char last))
1664 ; (throw 'done t))
1665 ((looking-at ";\\|};?")
1666 (goto-char (match-end 0))
1667 (throw 'done t))
1668 ((= (char-syntax (char-after)) ?\")
1669 (let ((beg (point)))
1670 (c-forward-sexp)
1671 (save-excursion
1672 (skip-chars-backward "^\n" beg)
1673 (when (> (point) beg)
1674 ;; Break at multiline string.
1675 (setq literal-pos beg)
1676 (throw 'done t)))))
1677 (t
1678 (forward-char 1)
1679 (skip-syntax-forward "w_")) ; Speedup only.
1680 )))
1681 (if (and (numberp literal-pos)
1682 (> (point) literal-pos))
1683 ;; We jumped over a comment that should be investigated.
1684 (goto-char literal-pos)
1685 (setq count (1+ count))))
1686 (error
1687 (goto-char (point-max))
1688 (setq count 0)))
1689 ))
1690 ;; If we haven't moved we're near a buffer limit.
1691 (when (and (not (zerop count)) (= (point) here))
1692 (goto-char (if (> count 0) (point-min) (point-max)))
1693 (setq count 0))))
1694 (c-keep-region-active))
1695
1696 (defun c-end-of-statement (&optional count lim sentence-flag)
1697 "Go to the end of the innermost C statement.
1698 With prefix arg, go forward N - 1 statements. Move forward to the end
1699 of the next statement if already at end, and move into nested blocks
1700 \(use \\[forward-sexp] to skip over a block). If within or next to a
1701 comment or multiline string, move by sentences instead of statements.
1702
1703 When called from a program, this function takes 3 optional args: the
1704 repetition count, a buffer position limit which is the farthest back
1705 to search for the syntactic context, and a flag saying whether to do
1706 sentence motion in or near comments and multiline strings."
1707 (interactive (list (prefix-numeric-value current-prefix-arg)
1708 nil t))
1709 (c-beginning-of-statement (- (or count 1)) lim sentence-flag)
1710 (c-keep-region-active))
1711
1712 \f
1713 ;; set up electric character functions to work with pending-del,
1714 ;; (a.k.a. delsel) mode. All symbols get the t value except
1715 ;; the functions which delete, which gets 'supersede.
1716 (mapcar
1717 (function
1718 (lambda (sym)
1719 (put sym 'delete-selection t) ; for delsel (Emacs)
1720 (put sym 'pending-delete t))) ; for pending-del (XEmacs)
1721 '(c-electric-pound
1722 c-electric-brace
1723 c-electric-slash
1724 c-electric-star
1725 c-electric-semi&comma
1726 c-electric-lt-gt
1727 c-electric-colon
1728 c-electric-paren))
1729 (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
1730 (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
1731 (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
1732 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
1733 (put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel
1734 (put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del
1735
1736 \f
1737 (defun c-calc-comment-indent (entry)
1738 (if (symbolp entry)
1739 (setq entry (or (assq entry c-indent-comment-alist)
1740 (assq 'other c-indent-comment-alist)
1741 '(default . (column . nil)))))
1742 (let ((action (car (cdr entry)))
1743 (value (cdr (cdr entry)))
1744 (col (current-column)))
1745 (cond ((eq action 'space)
1746 (+ col value))
1747 ((eq action 'column)
1748 (unless value (setq value comment-column))
1749 (if (bolp)
1750 ;; Do not pad with one space if we're at bol.
1751 value
1752 (max (1+ col) value)))
1753 ((eq action 'align)
1754 (or (save-excursion
1755 (beginning-of-line)
1756 (unless (bobp)
1757 (backward-char)
1758 (let ((lim (c-literal-limits (c-point 'bol) t)))
1759 (when (consp lim)
1760 (goto-char (car lim))
1761 (when (looking-at "/[/*]")
1762 ;; Found comment to align with.
1763 (if (bolp)
1764 ;; Do not pad with one space if we're at bol.
1765 0
1766 (max (1+ col) (current-column))))))))
1767 ;; Recurse to handle value as a new spec.
1768 (c-calc-comment-indent (cdr entry)))))))
1769
1770 (defun c-comment-indent ()
1771 "Used by `indent-for-comment' to create and indent comments.
1772 See `c-indent-comment-alist' for a description."
1773 (save-excursion
1774 (end-of-line)
1775 (c-save-buffer-state
1776 ((eot (let ((lim (c-literal-limits (c-point 'bol) t)))
1777 (or (when (consp lim)
1778 (goto-char (car lim))
1779 (when (looking-at "/[/*]")
1780 (skip-chars-backward " \t")
1781 (point)))
1782 (progn
1783 (skip-chars-backward " \t")
1784 (point)))))
1785 (line-type
1786 (cond ((looking-at "^/[/*]")
1787 'anchored-comment)
1788 ((progn (beginning-of-line)
1789 (eq (point) eot))
1790 'empty-line)
1791 ((progn (back-to-indentation)
1792 (and (eq (char-after) ?})
1793 (eq (point) (1- eot))))
1794 'end-block)
1795 ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
1796 (eq (match-end 0) eot))
1797 'cpp-end-block)
1798 (t
1799 'other))))
1800 (if (and (memq line-type '(anchored-comment empty-line))
1801 c-indent-comments-syntactically-p)
1802 (let ((c-syntactic-context (c-guess-basic-syntax)))
1803 ;; BOGOSITY ALERT: if we're looking at the eol, its
1804 ;; because indent-for-comment hasn't put the comment-start
1805 ;; in the buffer yet. this will screw up the syntactic
1806 ;; analysis so we kludge in the necessary info. Another
1807 ;; kludge is that if we're at the bol, then we really want
1808 ;; to ignore any anchoring as specified by
1809 ;; c-comment-only-line-offset since it doesn't apply here.
1810 (if (eolp)
1811 (c-add-syntax 'comment-intro))
1812 (let ((c-comment-only-line-offset
1813 (if (consp c-comment-only-line-offset)
1814 c-comment-only-line-offset
1815 (cons c-comment-only-line-offset
1816 c-comment-only-line-offset))))
1817 (c-get-syntactic-indentation c-syntactic-context)))
1818 (goto-char eot)
1819 (c-calc-comment-indent line-type)))))
1820
1821 \f
1822 ;; used by outline-minor-mode
1823 (defun c-outline-level ()
1824 (let (buffer-invisibility-spec);; This so that `current-column' DTRT
1825 ;; in otherwise-hidden text.
1826 (save-excursion
1827 (skip-chars-forward "\t ")
1828 (current-column))))
1829
1830 \f
1831 (defun c-up-conditional (count)
1832 "Move back to the containing preprocessor conditional, leaving mark behind.
1833 A prefix argument acts as a repeat count. With a negative argument,
1834 move forward to the end of the containing preprocessor conditional.
1835
1836 `#elif' is treated like `#else' followed by `#if', so the function
1837 stops at them when going backward, but not when going forward."
1838 (interactive "p")
1839 (c-forward-conditional (- count) -1)
1840 (c-keep-region-active))
1841
1842 (defun c-up-conditional-with-else (count)
1843 "Move back to the containing preprocessor conditional, including `#else'.
1844 Just like `c-up-conditional', except it also stops at `#else'
1845 directives."
1846 (interactive "p")
1847 (c-forward-conditional (- count) -1 t)
1848 (c-keep-region-active))
1849
1850 (defun c-down-conditional (count)
1851 "Move forward into the next preprocessor conditional, leaving mark behind.
1852 A prefix argument acts as a repeat count. With a negative argument,
1853 move backward into the previous preprocessor conditional.
1854
1855 `#elif' is treated like `#else' followed by `#if', so the function
1856 stops at them when going forward, but not when going backward."
1857 (interactive "p")
1858 (c-forward-conditional count 1)
1859 (c-keep-region-active))
1860
1861 (defun c-down-conditional-with-else (count)
1862 "Move forward into the next preprocessor conditional, including `#else'.
1863 Just like `c-down-conditional', except it also stops at `#else'
1864 directives."
1865 (interactive "p")
1866 (c-forward-conditional count 1 t)
1867 (c-keep-region-active))
1868
1869 (defun c-backward-conditional (count &optional target-depth with-else)
1870 "Move back across a preprocessor conditional, leaving mark behind.
1871 A prefix argument acts as a repeat count. With a negative argument,
1872 move forward across a preprocessor conditional."
1873 (interactive "p")
1874 (c-forward-conditional (- count) target-depth with-else)
1875 (c-keep-region-active))
1876
1877 (defun c-forward-conditional (count &optional target-depth with-else)
1878 "Move forward across a preprocessor conditional, leaving mark behind.
1879 A prefix argument acts as a repeat count. With a negative argument,
1880 move backward across a preprocessor conditional.
1881
1882 `#elif' is treated like `#else' followed by `#if', except that the
1883 nesting level isn't changed when tracking subconditionals.
1884
1885 The optional argument TARGET-DEPTH specifies the wanted nesting depth
1886 after each scan. I.e. if TARGET-DEPTH is -1, the function will move
1887 out of the enclosing conditional. A non-integer non-nil TARGET-DEPTH
1888 counts as -1.
1889
1890 If the optional argument WITH-ELSE is non-nil, `#else' directives are
1891 treated as conditional clause limits. Normally they are ignored."
1892 (interactive "p")
1893 (let* ((forward (> count 0))
1894 (increment (if forward -1 1))
1895 (search-function (if forward 're-search-forward 're-search-backward))
1896 (new))
1897 (unless (integerp target-depth)
1898 (setq target-depth (if target-depth -1 0)))
1899 (save-excursion
1900 (while (/= count 0)
1901 (let ((depth 0)
1902 ;; subdepth is the depth in "uninteresting" subtrees,
1903 ;; i.e. those that takes us farther from the target
1904 ;; depth instead of closer.
1905 (subdepth 0)
1906 found)
1907 (save-excursion
1908 ;; Find the "next" significant line in the proper direction.
1909 (while (and (not found)
1910 ;; Rather than searching for a # sign that
1911 ;; comes at the beginning of a line aside from
1912 ;; whitespace, search first for a string
1913 ;; starting with # sign. Then verify what
1914 ;; precedes it. This is faster on account of
1915 ;; the fastmap feature of the regexp matcher.
1916 (funcall search-function
1917 "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
1918 nil t))
1919 (beginning-of-line)
1920 ;; Now verify it is really a preproc line.
1921 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
1922 (let (dchange (directive (match-string 1)))
1923 (cond ((string= directive "if")
1924 (setq dchange (- increment)))
1925 ((string= directive "endif")
1926 (setq dchange increment))
1927 ((= subdepth 0)
1928 ;; When we're not in an "uninteresting"
1929 ;; subtree, we might want to act on "elif"
1930 ;; and "else" too.
1931 (if (cond (with-else
1932 ;; Always move toward the target depth.
1933 (setq dchange
1934 (if (> target-depth 0) 1 -1)))
1935 ((string= directive "elif")
1936 (setq dchange (- increment))))
1937 ;; Ignore the change if it'd take us
1938 ;; into an "uninteresting" subtree.
1939 (if (eq (> dchange 0) (<= target-depth 0))
1940 (setq dchange nil)))))
1941 (when dchange
1942 (when (or (/= subdepth 0)
1943 (eq (> dchange 0) (<= target-depth 0)))
1944 (setq subdepth (+ subdepth dchange)))
1945 (setq depth (+ depth dchange))
1946 ;; If we are trying to move across, and we find an
1947 ;; end before we find a beginning, get an error.
1948 (if (and (< depth target-depth) (< dchange 0))
1949 (error (if forward
1950 "No following conditional at this level"
1951 "No previous conditional at this level"))))
1952 ;; When searching forward, start from next line so
1953 ;; that we don't find the same line again.
1954 (if forward (forward-line 1))
1955 ;; We found something if we've arrived at the
1956 ;; target depth.
1957 (if (and dchange (= depth target-depth))
1958 (setq found (point))))
1959 ;; else
1960 (if forward (forward-line 1)))))
1961 (or found
1962 (error "No containing preprocessor conditional"))
1963 (goto-char (setq new found)))
1964 (setq count (+ count increment))))
1965 (push-mark)
1966 (goto-char new))
1967 (c-keep-region-active))
1968
1969 \f
1970 ;; commands to indent lines, regions, defuns, and expressions
1971 (defun c-indent-command (&optional arg)
1972 "Indent current line as C code, and/or insert some whitespace.
1973
1974 If `c-tab-always-indent' is t, always just indent the current line.
1975 If nil, indent the current line only if point is at the left margin or
1976 in the line's indentation; otherwise insert some whitespace[*]. If
1977 other than nil or t, then some whitespace[*] is inserted only within
1978 literals (comments and strings), but the line is always reindented.
1979
1980 If `c-syntactic-indentation' is t, indentation is done according to
1981 the syntactic context. A numeric argument, regardless of its value,
1982 means indent rigidly all the lines of the expression starting after
1983 point so that this line becomes properly indented. The relative
1984 indentation among the lines of the expression is preserved.
1985
1986 If `c-syntactic-indentation' is nil, the line is just indented one
1987 step according to `c-basic-offset'. In this mode, a numeric argument
1988 indents a number of such steps, positive or negative, and an empty
1989 prefix argument is equivalent to -1.
1990
1991 [*] The amount and kind of whitespace inserted is controlled by the
1992 variable `c-insert-tab-function', which is called to do the actual
1993 insertion of whitespace. Normally the function in this variable
1994 just inserts a tab character, or the equivalent number of spaces,
1995 depending on the variable `indent-tabs-mode'."
1996
1997 (interactive "p")
1998 (let ((indent-function
1999 (if c-syntactic-indentation
2000 (symbol-function 'indent-according-to-mode)
2001 (lambda ()
2002 (let ((c-macro-start c-macro-start)
2003 (steps (cond ((not current-prefix-arg) 1)
2004 ((equal current-prefix-arg '(4)) -1)
2005 (t arg))))
2006 (c-shift-line-indentation (* steps c-basic-offset))
2007 (when (and c-auto-align-backslashes
2008 (save-excursion
2009 (end-of-line)
2010 (eq (char-before) ?\\))
2011 (c-query-and-set-macro-start))
2012 ;; Realign the line continuation backslash if inside a macro.
2013 (c-backslash-region (point) (point) nil t)))
2014 ))))
2015 (if (and c-syntactic-indentation current-prefix-arg)
2016 ;; If c-syntactic-indentation and got arg, always indent this
2017 ;; line as C and shift remaining lines of expression the same
2018 ;; amount.
2019 (let ((shift-amt (save-excursion
2020 (back-to-indentation)
2021 (current-column)))
2022 beg end)
2023 (c-indent-line)
2024 (setq shift-amt (- (save-excursion
2025 (back-to-indentation)
2026 (current-column))
2027 shift-amt))
2028 (save-excursion
2029 (if (eq c-tab-always-indent t)
2030 (beginning-of-line))
2031 (setq beg (point))
2032 (c-forward-sexp 1)
2033 (setq end (point))
2034 (goto-char beg)
2035 (forward-line 1)
2036 (setq beg (point)))
2037 (if (> end beg)
2038 (indent-code-rigidly beg end shift-amt "#")))
2039 ;; Else use c-tab-always-indent to determine behavior.
2040 (cond
2041 ;; CASE 1: indent when at column zero or in lines indentation,
2042 ;; otherwise insert a tab
2043 ((not c-tab-always-indent)
2044 (if (save-excursion
2045 (skip-chars-backward " \t")
2046 (not (bolp)))
2047 (funcall c-insert-tab-function)
2048 (funcall indent-function)))
2049 ;; CASE 2: just indent the line
2050 ((eq c-tab-always-indent t)
2051 (funcall indent-function))
2052 ;; CASE 3: if in a literal, insert a tab, but always indent the
2053 ;; line
2054 (t
2055 (if (c-in-literal)
2056 (funcall c-insert-tab-function))
2057 (funcall indent-function)
2058 )))))
2059
2060 (defun c-indent-exp (&optional shutup-p)
2061 "Indent each line in the balanced expression following point syntactically.
2062 If optional SHUTUP-P is non-nil, no errors are signalled if no
2063 balanced expression is found."
2064 (interactive "*P")
2065 (let ((here (point-marker))
2066 end)
2067 (set-marker-insertion-type here t)
2068 (unwind-protect
2069 (let ((start (save-restriction
2070 ;; Find the closest following open paren that
2071 ;; ends on another line.
2072 (narrow-to-region (point-min) (c-point 'eol))
2073 (let (beg (end (point)))
2074 (while (and (setq beg (c-down-list-forward end))
2075 (setq end (c-up-list-forward beg))))
2076 (and beg
2077 (eq (char-syntax (char-before beg)) ?\()
2078 (1- beg))))))
2079 ;; sanity check
2080 (if (not start)
2081 (unless shutup-p
2082 (error "Cannot find start of balanced expression to indent"))
2083 (goto-char start)
2084 (setq end (c-safe (scan-sexps (point) 1)))
2085 (if (not end)
2086 (unless shutup-p
2087 (error "Cannot find end of balanced expression to indent"))
2088 (forward-line)
2089 (if (< (point) end)
2090 (c-indent-region (point) end)))))
2091 (goto-char here)
2092 (set-marker here nil))))
2093
2094 (defun c-indent-defun ()
2095 "Indent the current top-level declaration or macro syntactically.
2096 In the macro case this also has the effect of realigning any line
2097 continuation backslashes, unless `c-auto-align-backslashes' is nil."
2098 (interactive "*")
2099 (let ((here (point-marker)) decl-limits)
2100 (unwind-protect
2101 (progn
2102 (c-save-buffer-state nil
2103 ;; We try to be line oriented, unless there are several
2104 ;; declarations on the same line.
2105 (if (looking-at c-syntactic-eol)
2106 (c-backward-token-2 1 nil (c-point 'bol))
2107 (c-forward-token-2 0 nil (c-point 'eol)))
2108 (setq decl-limits (c-declaration-limits nil)))
2109 (if decl-limits
2110 (c-indent-region (car decl-limits)
2111 (cdr decl-limits))))
2112 (goto-char here)
2113 (set-marker here nil))))
2114
2115 (defun c-indent-region (start end &optional quiet)
2116 "Indent syntactically every line whose first char is between START
2117 and END inclusive. If the optional argument QUIET is non-nil then no
2118 syntactic errors are reported, even if `c-report-syntactic-errors' is
2119 non-nil."
2120 (save-excursion
2121 (goto-char end)
2122 (skip-chars-backward " \t\n\r\f\v")
2123 (setq end (point))
2124 (goto-char start)
2125 ;; Advance to first nonblank line.
2126 (beginning-of-line)
2127 (skip-chars-forward " \t\n\r\f\v")
2128 (setq start (point))
2129 (beginning-of-line)
2130 (setq c-parsing-error
2131 (or (let ((endmark (copy-marker end))
2132 (c-parsing-error nil)
2133 ;; shut up any echo msgs on indiv lines
2134 (c-echo-syntactic-information-p nil)
2135 (in-macro (and c-auto-align-backslashes
2136 (save-excursion (c-beginning-of-macro))
2137 start))
2138 (c-fix-backslashes nil)
2139 syntax)
2140 (unwind-protect
2141 (progn
2142 (c-progress-init start end 'c-indent-region)
2143 (while (and (bolp)
2144 (not (eobp))
2145 (< (point) endmark))
2146 ;; update progress
2147 (c-progress-update)
2148 ;; skip empty lines
2149 (skip-chars-forward " \t\n")
2150 (beginning-of-line)
2151 ;; Get syntax and indent.
2152 (c-save-buffer-state nil
2153 (setq syntax (c-guess-basic-syntax)))
2154 (if (and c-auto-align-backslashes
2155 (assq 'cpp-macro syntax))
2156 ;; Record macro start.
2157 (setq in-macro (point)))
2158 (if in-macro
2159 (if (looking-at "\\s *\\\\$")
2160 (forward-line)
2161 (c-indent-line syntax t t)
2162 (if (progn (end-of-line)
2163 (not (eq (char-before) ?\\)))
2164 (progn
2165 ;; Fixup macro backslashes.
2166 (forward-line)
2167 (c-backslash-region in-macro (point) nil)
2168 (setq in-macro nil))
2169 (forward-line)))
2170 (c-indent-line syntax t t)
2171 (forward-line)))
2172 (if in-macro
2173 (c-backslash-region in-macro (c-point 'bopl) nil t)))
2174 (set-marker endmark nil)
2175 (c-progress-fini 'c-indent-region))
2176 (c-echo-parsing-error quiet))
2177 c-parsing-error))))
2178
2179 (defun c-fn-region-is-active-p ()
2180 ;; Function version of the macro for use in places that aren't
2181 ;; compiled, e.g. in the menus.
2182 ;;
2183 ;; This function does not do any hidden buffer changes.
2184 (c-region-is-active-p))
2185
2186 (defun c-indent-line-or-region ()
2187 "When the region is active, indent it syntactically. Otherwise
2188 indent the current line syntactically."
2189 ;; Emacs has a variable called mark-active, XEmacs uses region-active-p
2190 (interactive)
2191 (if (c-region-is-active-p)
2192 (c-indent-region (region-beginning) (region-end))
2193 (c-indent-line)))
2194
2195 \f
2196 ;; for progress reporting
2197 (defvar c-progress-info nil)
2198
2199 (defun c-progress-init (start end context)
2200 ;; This function does not do any hidden buffer changes.
2201 (cond
2202 ;; Be silent
2203 ((not c-progress-interval))
2204 ;; Start the progress update messages. If this Emacs doesn't have
2205 ;; a built-in timer, just be dumb about it.
2206 ((not (fboundp 'current-time))
2207 (message "Indenting region... (this may take a while)"))
2208 ;; If progress has already been initialized, do nothing. otherwise
2209 ;; initialize the counter with a vector of:
2210 ;; [start end lastsec context]
2211 (c-progress-info)
2212 (t (setq c-progress-info (vector start
2213 (save-excursion
2214 (goto-char end)
2215 (point-marker))
2216 (nth 1 (current-time))
2217 context))
2218 (message "Indenting region..."))
2219 ))
2220
2221 (defun c-progress-update ()
2222 ;; This function does not do any hidden buffer changes.
2223 (if (not (and c-progress-info c-progress-interval))
2224 nil
2225 (let ((now (nth 1 (current-time)))
2226 (start (aref c-progress-info 0))
2227 (end (aref c-progress-info 1))
2228 (lastsecs (aref c-progress-info 2)))
2229 ;; should we update? currently, update happens every 2 seconds,
2230 ;; what's the right value?
2231 (if (< c-progress-interval (- now lastsecs))
2232 (progn
2233 (message "Indenting region... (%d%% complete)"
2234 (/ (* 100 (- (point) start)) (- end start)))
2235 (aset c-progress-info 2 now)))
2236 )))
2237
2238 (defun c-progress-fini (context)
2239 ;; This function does not do any hidden buffer changes.
2240 (if (not c-progress-interval)
2241 nil
2242 (if (or (eq context (aref c-progress-info 3))
2243 (eq context t))
2244 (progn
2245 (set-marker (aref c-progress-info 1) nil)
2246 (setq c-progress-info nil)
2247 (message "Indenting region... done")))))
2248
2249
2250 \f
2251 ;;; This page handles insertion and removal of backslashes for C macros.
2252
2253 (defun c-backslash-region (from to delete-flag &optional line-mode)
2254 "Insert, align, or delete end-of-line backslashes on the lines in the region.
2255 With no argument, inserts backslashes and aligns existing backslashes.
2256 With an argument, deletes the backslashes. The backslash alignment is
2257 done according to the settings in `c-backslash-column',
2258 `c-backslash-max-column' and `c-auto-align-backslashes'.
2259
2260 This function does not modify blank lines at the start of the region.
2261 If the region ends at the start of a line and the macro doesn't
2262 continue below it, the backslash (if any) at the end of the previous
2263 line is deleted.
2264
2265 You can put the region around an entire macro definition and use this
2266 command to conveniently insert and align the necessary backslashes."
2267 (interactive "*r\nP")
2268 (let ((endmark (make-marker))
2269 ;; Keep the backslash trimming functions from changing the
2270 ;; whitespace around point, since in this case it's only the
2271 ;; position of point that tells the indentation of the line.
2272 (point-pos (if (save-excursion
2273 (skip-chars-backward " \t")
2274 (and (bolp) (looking-at "[ \t]*\\\\?$")))
2275 (point-marker)
2276 (point-min)))
2277 column longest-line-col bs-col-after-end)
2278 (save-excursion
2279 (goto-char to)
2280 (if (and (not line-mode) (bobp))
2281 ;; Nothing to do if to is at bob, since we should back up
2282 ;; and there's no line to back up to.
2283 nil
2284 (when (and (not line-mode) (bolp))
2285 ;; Do not back up the to line if line-mode is set, to make
2286 ;; e.g. c-newline-and-indent consistent regardless whether
2287 ;; the (newline) call leaves point at bol or not.
2288 (backward-char)
2289 (setq to (point)))
2290 (if delete-flag
2291 (progn
2292 (set-marker endmark (point))
2293 (goto-char from)
2294 (c-delete-backslashes-forward endmark point-pos))
2295 ;; Set bs-col-after-end to the column of any backslash
2296 ;; following the region, or nil if there is none.
2297 (setq bs-col-after-end
2298 (and (progn (end-of-line)
2299 (eq (char-before) ?\\))
2300 (= (forward-line 1) 0)
2301 (progn (end-of-line)
2302 (eq (char-before) ?\\))
2303 (1- (current-column))))
2304 (when line-mode
2305 ;; Back up the to line if line-mode is set, since the line
2306 ;; after the newly inserted line break should not be
2307 ;; touched in c-newline-and-indent.
2308 (setq to (max from (or (c-safe (c-point 'eopl)) from)))
2309 (unless bs-col-after-end
2310 ;; Set bs-col-after-end to non-nil in any case, since we
2311 ;; do not want to delete the backslash at the last line.
2312 (setq bs-col-after-end t)))
2313 (if (and line-mode
2314 (not c-auto-align-backslashes))
2315 (goto-char from)
2316 ;; Compute the smallest column number past the ends of all
2317 ;; the lines.
2318 (setq longest-line-col 0)
2319 (goto-char to)
2320 (if bs-col-after-end
2321 ;; Include one more line in the max column
2322 ;; calculation, since the to line will be backslashed
2323 ;; too.
2324 (forward-line 1))
2325 (end-of-line)
2326 (while (and (>= (point) from)
2327 (progn
2328 (if (eq (char-before) ?\\)
2329 (forward-char -1))
2330 (skip-chars-backward " \t")
2331 (setq longest-line-col (max longest-line-col
2332 (1+ (current-column))))
2333 (beginning-of-line)
2334 (not (bobp))))
2335 (backward-char))
2336 ;; Try to align with surrounding backslashes.
2337 (goto-char from)
2338 (beginning-of-line)
2339 (if (and (not (bobp))
2340 (progn (backward-char)
2341 (eq (char-before) ?\\)))
2342 (progn
2343 (setq column (1- (current-column)))
2344 (if (numberp bs-col-after-end)
2345 ;; Both a preceding and a following backslash.
2346 ;; Choose the greatest of them.
2347 (setq column (max column bs-col-after-end)))
2348 (goto-char from))
2349 ;; No preceding backslash. Try to align with one
2350 ;; following the region. Disregard the backslash at the
2351 ;; to line since it's likely to be bogus (e.g. when
2352 ;; called from c-newline-and-indent).
2353 (if (numberp bs-col-after-end)
2354 (setq column bs-col-after-end))
2355 ;; Don't modify blank lines at start of region.
2356 (goto-char from)
2357 (while (and (< (point) to) (bolp) (eolp))
2358 (forward-line 1)))
2359 (if (and column (< column longest-line-col))
2360 ;; Don't try to align with surrounding backslashes if
2361 ;; any line is too long.
2362 (setq column nil))
2363 (unless column
2364 ;; Impose minimum limit and tab width alignment only if
2365 ;; we can't align with surrounding backslashes.
2366 (if (> (% longest-line-col tab-width) 0)
2367 (setq longest-line-col
2368 (* (/ (+ longest-line-col tab-width -1)
2369 tab-width)
2370 tab-width)))
2371 (setq column (max c-backslash-column
2372 longest-line-col)))
2373 ;; Always impose maximum limit.
2374 (setq column (min column c-backslash-max-column)))
2375 (if bs-col-after-end
2376 ;; Add backslashes on all lines if the macro continues
2377 ;; after the to line.
2378 (progn
2379 (set-marker endmark to)
2380 (c-append-backslashes-forward endmark column point-pos))
2381 ;; Add backslashes on all lines except the last, and
2382 ;; remove any on the last line.
2383 (if (save-excursion
2384 (goto-char to)
2385 (beginning-of-line)
2386 (if (not (bobp))
2387 (set-marker endmark (1- (point)))))
2388 (progn
2389 (c-append-backslashes-forward endmark column point-pos)
2390 ;; The function above leaves point on the line
2391 ;; following endmark.
2392 (set-marker endmark (point)))
2393 (set-marker endmark to))
2394 (c-delete-backslashes-forward endmark point-pos)))))
2395 (set-marker endmark nil)
2396 (if (markerp point-pos)
2397 (set-marker point-pos nil))))
2398
2399 (defun c-append-backslashes-forward (to-mark column point-pos)
2400 ;; This function does not do any hidden buffer changes.
2401 (let ((state (parse-partial-sexp (c-point 'bol) (point))))
2402 (if column
2403 (while
2404 (and
2405 (<= (point) to-mark)
2406
2407 (let ((start (point)) (inserted nil) end col)
2408 (end-of-line)
2409 (unless (eq (char-before) ?\\)
2410 (insert ?\\)
2411 (setq inserted t))
2412 (setq state (parse-partial-sexp
2413 start (point) nil nil state))
2414 (backward-char)
2415 (setq col (current-column))
2416
2417 ;; Avoid unnecessary changes of the buffer.
2418 (cond ((and (not inserted) (nth 3 state))
2419 ;; Don't realign backslashes in string literals
2420 ;; since that would change them.
2421 )
2422
2423 ((< col column)
2424 (delete-region
2425 (point)
2426 (progn
2427 (skip-chars-backward
2428 " \t" (if (>= (point) point-pos) point-pos))
2429 (point)))
2430 (indent-to column))
2431
2432 ((and (= col column)
2433 (memq (char-before) '(?\ ?\t))))
2434
2435 ((progn
2436 (setq end (point))
2437 (or (/= (skip-chars-backward
2438 " \t" (if (>= (point) point-pos) point-pos))
2439 -1)
2440 (/= (char-after) ?\ )))
2441 (delete-region (point) end)
2442 (indent-to column 1)))
2443
2444 (= (forward-line 1) 0))))
2445
2446 ;; Make sure there are backslashes with at least one space in
2447 ;; front of them.
2448 (while
2449 (and
2450 (<= (point) to-mark)
2451
2452 (let ((start (point)))
2453 (end-of-line)
2454 (setq state (parse-partial-sexp
2455 start (point) nil nil state))
2456
2457 (if (eq (char-before) ?\\)
2458 (unless (nth 3 state)
2459 (backward-char)
2460 (unless (and (memq (char-before) '(?\ ?\t))
2461 (/= (point) point-pos))
2462 (insert ?\ )))
2463
2464 (if (and (memq (char-before) '(?\ ?\t))
2465 (/= (point) point-pos))
2466 (insert ?\\)
2467 (insert ?\ ?\\)))
2468
2469 (= (forward-line 1) 0)))))))
2470
2471 (defun c-delete-backslashes-forward (to-mark point-pos)
2472 ;; This function does not do any hidden buffer changes.
2473 (while
2474 (and (<= (point) to-mark)
2475 (progn
2476 (end-of-line)
2477 (if (eq (char-before) ?\\)
2478 (delete-region
2479 (point)
2480 (progn (backward-char)
2481 (skip-chars-backward " \t" (if (>= (point) point-pos)
2482 point-pos))
2483 (point))))
2484 (= (forward-line 1) 0)))))
2485
2486
2487 \f
2488 ;;; Line breaking and paragraph filling.
2489
2490 (defvar c-auto-fill-prefix t)
2491 (defvar c-lit-limits nil)
2492 (defvar c-lit-type nil)
2493
2494 ;; The filling code is based on a simple theory; leave the intricacies
2495 ;; of the text handling to the currently active mode for that
2496 ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
2497 ;; possible to make them work correctly wrt the comment and string
2498 ;; separators, one-line paragraphs etc. Unfortunately, when it comes
2499 ;; to it, there's quite a lot of special cases to handle which makes
2500 ;; the code anything but simple. The intention is that it will work
2501 ;; with any well-written text filling package that preserves a fill
2502 ;; prefix.
2503 ;;
2504 ;; We temporarily mask comment starters and enders as necessary for
2505 ;; the filling code to do its job on a seemingly normal text block.
2506 ;; We do _not_ mask the fill prefix, so it's up to the filling code to
2507 ;; preserve it correctly (especially important when filling C++ style
2508 ;; line comments). By default, we set up and use adaptive-fill-mode,
2509 ;; which is standard in all supported Emacs flavors.
2510
2511 (defun c-guess-fill-prefix (lit-limits lit-type)
2512 ;; Determine the appropriate comment fill prefix for a block or line
2513 ;; comment. Return a cons of the prefix string and the column where
2514 ;; it ends. If fill-prefix is set, it'll override. Note that this
2515 ;; function also uses the value of point in some heuristics.
2516
2517 (let* ((here (point))
2518 (prefix-regexp (concat "[ \t]*\\("
2519 c-current-comment-prefix
2520 "\\)[ \t]*"))
2521 (comment-start-regexp (if (eq lit-type 'c++)
2522 prefix-regexp
2523 comment-start-skip))
2524 prefix-line comment-prefix res comment-text-end)
2525
2526 (cond
2527 (fill-prefix
2528 (setq res (cons fill-prefix
2529 ;; Ugly way of getting the column after the fill
2530 ;; prefix; it'd be nice with a current-column
2531 ;; that works on strings..
2532 (let ((start (point)))
2533 (unwind-protect
2534 (progn
2535 (insert-and-inherit "\n" fill-prefix)
2536 (current-column))
2537 (delete-region start (point)))))))
2538
2539 ((eq lit-type 'c++)
2540 (save-excursion
2541 ;; Set fallback for comment-prefix if none is found.
2542 (setq comment-prefix "// "
2543 comment-text-end (cdr lit-limits))
2544
2545 (beginning-of-line)
2546 (if (> (point) (car lit-limits))
2547 ;; The current line is not the comment starter, so the
2548 ;; comment has more than one line, and it can therefore be
2549 ;; used to find the comment fill prefix.
2550 (setq prefix-line (point))
2551
2552 (goto-char (car lit-limits))
2553 (if (and (= (forward-line 1) 0)
2554 (< (point) (cdr lit-limits)))
2555 ;; The line after the comment starter is inside the
2556 ;; comment, so we can use it.
2557 (setq prefix-line (point))
2558
2559 ;; The comment is only one line. Take the comment prefix
2560 ;; from it and keep the indentation.
2561 (goto-char (car lit-limits))
2562 (if (looking-at prefix-regexp)
2563 (goto-char (match-end 0))
2564 (forward-char 2)
2565 (skip-chars-forward " \t"))
2566
2567 (let (str col)
2568 (if (eq (c-point 'boi) (car lit-limits))
2569 ;; There is only whitespace before the comment
2570 ;; starter; take the prefix straight from this line.
2571 (setq str (buffer-substring-no-properties
2572 (c-point 'bol) (point))
2573 col (current-column))
2574
2575 ;; There is code before the comment starter, so we
2576 ;; have to temporarily insert and indent a new line to
2577 ;; get the right space/tab mix in the indentation.
2578 (let ((prefix-len (- (point) (car lit-limits)))
2579 tmp)
2580 (unwind-protect
2581 (progn
2582 (goto-char (car lit-limits))
2583 (indent-to (prog1 (current-column)
2584 (insert ?\n)))
2585 (setq tmp (point))
2586 (forward-char prefix-len)
2587 (setq str (buffer-substring-no-properties
2588 (c-point 'bol) (point))
2589 col (current-column)))
2590 (delete-region (car lit-limits) tmp))))
2591
2592 (setq res
2593 (if (or (string-match "\\s \\'" str) (not (eolp)))
2594 (cons str col)
2595 ;; The prefix ends the line with no whitespace
2596 ;; after it. Default to a single space.
2597 (cons (concat str " ") (1+ col))))
2598 )))))
2599
2600 (t
2601 (setq comment-text-end
2602 (save-excursion
2603 (goto-char (- (cdr lit-limits) 2))
2604 (if (looking-at "\\*/") (point) (cdr lit-limits))))
2605
2606 (save-excursion
2607 (beginning-of-line)
2608 (if (and (> (point) (car lit-limits))
2609 (not (and (looking-at "[ \t]*\\*/")
2610 (eq (cdr lit-limits) (match-end 0)))))
2611 ;; The current line is not the comment starter and
2612 ;; contains more than just the ender, so it's good enough
2613 ;; to be used for the comment fill prefix.
2614 (setq prefix-line (point))
2615 (goto-char (car lit-limits))
2616
2617 (cond ((or (/= (forward-line 1) 0)
2618 (>= (point) (cdr lit-limits))
2619 (and (looking-at "[ \t]*\\*/")
2620 (eq (cdr lit-limits) (match-end 0)))
2621 (and (looking-at prefix-regexp)
2622 (<= (1- (cdr lit-limits)) (match-end 0))))
2623 ;; The comment is either one line or the next line contains
2624 ;; just the comment ender. In this case we have no
2625 ;; information about a suitable comment prefix, so we resort
2626 ;; to c-block-comment-prefix.
2627 (setq comment-prefix (or c-block-comment-prefix "")))
2628
2629 ((< here (point))
2630 ;; The point was on the comment opener line, so we might want
2631 ;; to treat this as a not yet closed comment.
2632
2633 (if (and (match-beginning 1)
2634 (/= (match-beginning 1) (match-end 1)))
2635 ;; Above `prefix-regexp' matched a nonempty prefix on the
2636 ;; second line, so let's use it. Normally it should do
2637 ;; to set `prefix-line' and let the code below pick up
2638 ;; the whole prefix, but if there's no text after the
2639 ;; match then it will probably fall back to no prefix at
2640 ;; all if the comment isn't closed yet, so in that case
2641 ;; it's better to force use of the prefix matched now.
2642 (if (= (match-end 0) (c-point 'eol))
2643 (setq comment-prefix (match-string 1))
2644 (setq prefix-line (point)))
2645
2646 ;; There's no nonempty prefix on the line after the
2647 ;; comment opener. If the line is empty, or if the
2648 ;; text on it has less or equal indentation than the
2649 ;; comment starter we assume it's an unclosed
2650 ;; comment starter, i.e. that
2651 ;; `c-block-comment-prefix' should be used.
2652 ;; Otherwise we assume it's a closed comment where
2653 ;; the prefix really is the empty string.
2654 ;; E.g. this is an unclosed comment:
2655 ;;
2656 ;; /*
2657 ;; foo
2658 ;;
2659 ;; But this is not:
2660 ;;
2661 ;; /*
2662 ;; foo
2663 ;; */
2664 ;;
2665 ;; (Looking for the presence of the comment closer
2666 ;; rarely works since it's probably the closer of
2667 ;; some comment further down when the comment
2668 ;; really is unclosed.)
2669 (if (<= (save-excursion (back-to-indentation)
2670 (current-column))
2671 (save-excursion (goto-char (car lit-limits))
2672 (current-column)))
2673 (setq comment-prefix (or c-block-comment-prefix ""))
2674 (setq prefix-line (point)))))
2675
2676 (t
2677 ;; Otherwise the line after the comment starter is good
2678 ;; enough to find the prefix in.
2679 (setq prefix-line (point))))
2680
2681 (when comment-prefix
2682 ;; Haven't got the comment prefix on any real line that we
2683 ;; can take it from, so we have to temporarily insert
2684 ;; `comment-prefix' on a line and indent it to find the
2685 ;; correct column and the correct mix of tabs and spaces.
2686 (setq res
2687 (let (tmp-pre tmp-post)
2688 (unwind-protect
2689 (progn
2690
2691 (goto-char (car lit-limits))
2692 (if (looking-at comment-start-regexp)
2693 (goto-char (min (match-end 0)
2694 comment-text-end))
2695 (forward-char 2)
2696 (skip-chars-forward " \t"))
2697
2698 (when (eq (char-syntax (char-before)) ?\ )
2699 ;; If there's ws on the current line, we'll use it
2700 ;; instead of what's ending comment-prefix.
2701 (setq comment-prefix
2702 (concat (substring comment-prefix
2703 0 (string-match
2704 "\\s *\\'"
2705 comment-prefix))
2706 (buffer-substring-no-properties
2707 (save-excursion
2708 (skip-chars-backward " \t")
2709 (point))
2710 (point)))))
2711
2712 (setq tmp-pre (point-marker))
2713
2714 ;; We insert an extra non-whitespace character
2715 ;; before the line break and after comment-prefix in
2716 ;; case it's "" or ends with whitespace.
2717 (insert-and-inherit "x\n" comment-prefix "x")
2718 (setq tmp-post (point-marker))
2719
2720 (indent-according-to-mode)
2721
2722 (goto-char (1- tmp-post))
2723 (cons (buffer-substring-no-properties
2724 (c-point 'bol) (point))
2725 (current-column)))
2726
2727 (when tmp-post
2728 (delete-region tmp-pre tmp-post)
2729 (set-marker tmp-pre nil)
2730 (set-marker tmp-post nil))))))))))
2731
2732 (or res ; Found a good prefix above.
2733
2734 (save-excursion
2735 ;; prefix-line is the bol of a line on which we should try
2736 ;; to find the prefix.
2737 (let* (fb-string fb-endpos ; Contains any fallback prefix found.
2738 (test-line
2739 (lambda ()
2740 (when (and (looking-at prefix-regexp)
2741 (<= (match-end 0) comment-text-end))
2742 (unless (eq (match-end 0) (c-point 'eol))
2743 ;; The match is fine if there's text after it.
2744 (throw 'found (cons (buffer-substring-no-properties
2745 (match-beginning 0) (match-end 0))
2746 (progn (goto-char (match-end 0))
2747 (current-column)))))
2748 (unless fb-string
2749 ;; This match is better than nothing, so let's
2750 ;; remember it in case nothing better is found
2751 ;; on another line.
2752 (setq fb-string (buffer-substring-no-properties
2753 (match-beginning 0) (match-end 0))
2754 fb-endpos (match-end 0)))
2755 t))))
2756
2757 (or (catch 'found
2758 ;; Search for a line which has text after the prefix
2759 ;; so that we get the proper amount of whitespace
2760 ;; after it. We start with the current line, then
2761 ;; search backwards, then forwards.
2762
2763 (goto-char prefix-line)
2764 (when (and (funcall test-line)
2765 (or (/= (match-end 1) (match-end 0))
2766 ;; The whitespace is sucked up by the
2767 ;; first [ \t]* glob if the prefix is empty.
2768 (and (= (match-beginning 1) (match-end 1))
2769 (/= (match-beginning 0) (match-end 0)))))
2770 ;; If the current line doesn't have text but do
2771 ;; have whitespace after the prefix, we'll use it.
2772 (throw 'found (cons fb-string
2773 (progn (goto-char fb-endpos)
2774 (current-column)))))
2775
2776 (if (eq lit-type 'c++)
2777 ;; For line comments we can search up to and
2778 ;; including the first line.
2779 (while (and (zerop (forward-line -1))
2780 (>= (point) (car lit-limits)))
2781 (funcall test-line))
2782 ;; For block comments we must stop before the
2783 ;; block starter.
2784 (while (and (zerop (forward-line -1))
2785 (> (point) (car lit-limits)))
2786 (funcall test-line)))
2787
2788 (goto-char prefix-line)
2789 (while (and (zerop (forward-line 1))
2790 (< (point) (cdr lit-limits)))
2791 (funcall test-line))
2792
2793 (goto-char prefix-line)
2794 nil)
2795
2796 (when fb-string
2797 ;; A good line wasn't found, but at least we have a
2798 ;; fallback that matches the comment prefix regexp.
2799 (cond ((or (string-match "\\s \\'" fb-string)
2800 (progn
2801 (goto-char fb-endpos)
2802 (not (eolp))))
2803 ;; There are ws or text after the prefix, so
2804 ;; let's use it.
2805 (cons fb-string (current-column)))
2806
2807 ((progn
2808 ;; Check if there's any whitespace padding
2809 ;; on the comment start line that we can
2810 ;; use after the prefix.
2811 (goto-char (car lit-limits))
2812 (if (looking-at comment-start-regexp)
2813 (goto-char (match-end 0))
2814 (forward-char 2)
2815 (skip-chars-forward " \t"))
2816 (or (not (eolp))
2817 (eq (char-syntax (char-before)) ?\ )))
2818
2819 (setq fb-string (buffer-substring-no-properties
2820 (save-excursion
2821 (skip-chars-backward " \t")
2822 (point))
2823 (point)))
2824 (goto-char fb-endpos)
2825 (skip-chars-backward " \t")
2826
2827 (let ((tmp (point)))
2828 ;; Got to mess in the buffer once again to
2829 ;; ensure the column gets correct. :P
2830 (unwind-protect
2831 (progn
2832 (insert-and-inherit fb-string)
2833 (cons (buffer-substring-no-properties
2834 (c-point 'bol)
2835 (point))
2836 (current-column)))
2837 (delete-region tmp (point)))))
2838
2839 (t
2840 ;; Last resort: Just add a single space after
2841 ;; the prefix.
2842 (cons (concat fb-string " ")
2843 (progn (goto-char fb-endpos)
2844 (1+ (current-column)))))))
2845
2846 ;; The line doesn't match the comment prefix regexp.
2847 (if comment-prefix
2848 ;; We have a fallback for line comments that we must use.
2849 (cons (concat (buffer-substring-no-properties
2850 prefix-line (c-point 'boi))
2851 comment-prefix)
2852 (progn (back-to-indentation)
2853 (+ (current-column) (length comment-prefix))))
2854
2855 ;; Assume we are dealing with a "free text" block
2856 ;; comment where the lines doesn't have any comment
2857 ;; prefix at all and we should just fill it as
2858 ;; normal text.
2859 '("" . 0))))))
2860 ))
2861
2862 (defun c-mask-paragraph (fill-paragraph apply-outside-literal fun &rest args)
2863 ;; Calls FUN with ARGS ar arguments while the current paragraph is
2864 ;; masked to allow adaptive filling to work correctly. That
2865 ;; includes narrowing the buffer and, if point is inside a comment,
2866 ;; masking the comment starter and ender appropriately.
2867 ;;
2868 ;; FILL-PARAGRAPH is non-nil if called for whole paragraph filling.
2869 ;; The position of point is then less significant when doing masking
2870 ;; and narrowing.
2871 ;;
2872 ;; If APPLY-OUTSIDE-LITERAL is nil then the function will be called
2873 ;; only if the point turns out to be inside a comment or a string.
2874 ;;
2875 ;; This function does not do any hidden buffer changes.
2876
2877 (let (fill
2878 ;; beg and end limits the region to narrow. end is a marker.
2879 beg end
2880 ;; tmp-pre and tmp-post mark strings that are temporarily
2881 ;; inserted at the start and end of the region. tmp-pre is a
2882 ;; cons of the positions of the prepended string. tmp-post is
2883 ;; a marker pointing to the single character of the appended
2884 ;; string.
2885 tmp-pre tmp-post
2886 ;; If hang-ender-stuck isn't nil, the comment ender is
2887 ;; hanging. In that case it's set to the number of spaces
2888 ;; that should be between the text and the ender.
2889 hang-ender-stuck
2890 (here (point))
2891 (c-lit-limits c-lit-limits)
2892 (c-lit-type c-lit-type))
2893
2894 ;; Restore point on undo. It's necessary since we do a lot of
2895 ;; hidden inserts and deletes below that should be as transparent
2896 ;; as possible.
2897 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
2898 (setq buffer-undo-list (cons (point) buffer-undo-list)))
2899
2900 (save-restriction
2901 ;; Widen to catch comment limits correctly.
2902 (widen)
2903 (unless c-lit-limits
2904 (setq c-lit-limits (c-literal-limits nil fill-paragraph)))
2905 (setq c-lit-limits (c-collect-line-comments c-lit-limits))
2906 (unless c-lit-type
2907 (setq c-lit-type (c-literal-type c-lit-limits))))
2908
2909 (save-excursion
2910 (unless (c-safe (backward-char)
2911 (forward-paragraph)
2912 (>= (point) here))
2913 (goto-char here)
2914 (forward-paragraph))
2915 (setq end (point-marker)))
2916 (save-excursion
2917 (unless (c-safe (forward-char)
2918 (backward-paragraph)
2919 (<= (point) here))
2920 (goto-char here)
2921 (backward-paragraph))
2922 (setq beg (point)))
2923
2924 (unwind-protect
2925 (progn
2926 (cond
2927
2928 ((eq c-lit-type 'c++) ; Line comment.
2929 (save-excursion
2930 ;; Limit to the comment or paragraph end, whichever
2931 ;; comes first.
2932 (set-marker end (min end (cdr c-lit-limits)))
2933
2934 (when (<= beg (car c-lit-limits))
2935 ;; The region includes the comment starter, so we must
2936 ;; check it.
2937 (goto-char (car c-lit-limits))
2938 (back-to-indentation)
2939 (if (eq (point) (car c-lit-limits))
2940 ;; Include the first line in the region.
2941 (setq beg (c-point 'bol))
2942 ;; The first line contains code before the
2943 ;; comment. We must fake a line that doesn't.
2944 (setq tmp-pre t))))
2945
2946 (setq apply-outside-literal t))
2947
2948 ((eq c-lit-type 'c) ; Block comment.
2949 (when (>= end (cdr c-lit-limits))
2950 ;; The region includes the comment ender which we might
2951 ;; want to keep together with the last word.
2952 (unless (save-excursion
2953 (goto-char (cdr c-lit-limits))
2954 (beginning-of-line)
2955 (and (looking-at (concat "[ \t]*\\("
2956 c-current-comment-prefix
2957 "\\)\\*/"))
2958 (eq (cdr c-lit-limits) (match-end 0))
2959 ;; The comment ender is on a line of its
2960 ;; own. Keep it that way.
2961 (set-marker end (point))))
2962
2963 (if fill-paragraph
2964 ;; The comment ender should hang. Replace all
2965 ;; cruft between it and the last word with one or
2966 ;; two 'x' and include it in the region. We'll
2967 ;; change them back to spaces afterwards. This
2968 ;; isn't done when auto filling, since that'd
2969 ;; effectively make it impossible to insert extra
2970 ;; spaces before the comment ender.
2971 (let* ((ender-start (save-excursion
2972 (goto-char (cdr c-lit-limits))
2973 (skip-syntax-backward "^w ")
2974 (point)))
2975 (point-rel (- ender-start here))
2976 spaces)
2977
2978 (save-excursion
2979 (goto-char (cdr c-lit-limits))
2980 (setq tmp-post (point-marker))
2981 (insert ?\n)
2982 (set-marker end (point))
2983 (forward-line -1)
2984 (if (and (looking-at (concat "[ \t]*\\(\\("
2985 c-current-comment-prefix
2986 "\\)[ \t]*\\)"))
2987 (eq ender-start (match-end 0)))
2988 ;; The comment ender is prefixed by nothing
2989 ;; but a comment line prefix. Remove it
2990 ;; along with surrounding ws.
2991 (setq spaces (- (match-end 1) (match-end 2)))
2992 (goto-char ender-start))
2993 (skip-chars-backward " \t\r\n")
2994
2995 (if (/= (point) ender-start)
2996 (progn
2997 (if (<= here (point))
2998 ;; Don't adjust point below if it's
2999 ;; before the string we replace.
3000 (setq point-rel -1))
3001 ;; Keep one or two spaces between the
3002 ;; text and the ender, depending on how
3003 ;; many there are now.
3004 (unless spaces
3005 (setq spaces (- ender-start (point))))
3006 (setq spaces
3007 (max
3008 (min spaces
3009 (if sentence-end-double-space 2 1))
3010 1))
3011 ;; Insert the filler first to keep marks right.
3012 (insert-char ?x spaces t)
3013 (delete-region (point) (+ ender-start spaces))
3014 (setq hang-ender-stuck spaces)
3015 (setq point-rel
3016 (and (>= point-rel 0)
3017 (- (point) (min point-rel spaces)))))
3018 (setq point-rel nil)))
3019
3020 (if point-rel
3021 ;; Point was in the middle of the string we
3022 ;; replaced above, so put it back in the same
3023 ;; relative position, counting from the end.
3024 (goto-char point-rel)))
3025
3026 ;; We're doing auto filling. Just move the marker
3027 ;; to the comment end to ignore any code after the
3028 ;; comment.
3029 (move-marker end (cdr c-lit-limits)))))
3030
3031 (when (<= beg (car c-lit-limits))
3032 ;; The region includes the comment starter.
3033 (save-excursion
3034 (goto-char (car c-lit-limits))
3035 (if (looking-at (concat "\\(" comment-start-skip "\\)$"))
3036 ;; Begin with the next line.
3037 (setq beg (c-point 'bonl))
3038 ;; Fake the fill prefix in the first line.
3039 (setq tmp-pre t))))
3040
3041 (setq apply-outside-literal t))
3042
3043 ((eq c-lit-type 'string) ; String.
3044 (save-excursion
3045 (when (>= end (cdr c-lit-limits))
3046 (goto-char (1- (cdr c-lit-limits)))
3047 (setq tmp-post (point-marker))
3048 (insert ?\n)
3049 (set-marker end (point)))
3050 (when (<= beg (car c-lit-limits))
3051 (goto-char (1+ (car c-lit-limits)))
3052 (setq beg (if (looking-at "\\\\$")
3053 ;; Leave the start line if it's
3054 ;; nothing but an escaped newline.
3055 (1+ (match-end 0))
3056 (point)))))
3057 (setq apply-outside-literal t))
3058
3059 ((eq c-lit-type 'pound) ; Macro
3060 ;; Narrow to the macro limits if they are nearer than the
3061 ;; paragraph limits. Don't know if this is necessary but
3062 ;; do it for completeness sake (doing auto filling at all
3063 ;; inside macros is bogus to begin with since the line
3064 ;; continuation backslashes aren't handled).
3065 (save-excursion
3066 (c-beginning-of-macro)
3067 (beginning-of-line)
3068 (if (> (point) beg)
3069 (setq beg (point)))
3070 (c-end-of-macro)
3071 (forward-line)
3072 (if (< (point) end)
3073 (set-marker end (point)))))
3074
3075 (t ; Other code.
3076 ;; Try to avoid comments and macros in the paragraph to
3077 ;; avoid that the adaptive fill mode gets the prefix from
3078 ;; them.
3079 (c-save-buffer-state nil
3080 (save-excursion
3081 (goto-char beg)
3082 (c-forward-syntactic-ws end)
3083 (beginning-of-line)
3084 (setq beg (point))
3085 (goto-char end)
3086 (c-backward-syntactic-ws beg)
3087 (forward-line)
3088 (set-marker end (point))))))
3089
3090 (when tmp-pre
3091 ;; Temporarily insert the fill prefix after the comment
3092 ;; starter so that the first line looks like any other
3093 ;; comment line in the narrowed region.
3094 (setq fill (c-save-buffer-state nil
3095 (c-guess-fill-prefix c-lit-limits c-lit-type)))
3096 (unless (string-match (concat "\\`[ \t]*\\("
3097 c-current-comment-prefix
3098 "\\)[ \t]*\\'")
3099 (car fill))
3100 ;; Oops, the prefix doesn't match the comment prefix
3101 ;; regexp. This could produce very confusing
3102 ;; results with adaptive fill packages together with
3103 ;; the insert prefix magic below, since the prefix
3104 ;; often doesn't appear at all. So let's warn about
3105 ;; it.
3106 (message "\
3107 Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S"
3108 (car fill)))
3109 ;; Find the right spot on the line, break it, insert
3110 ;; the fill prefix and make sure we're back in the
3111 ;; same column by temporarily prefixing the first word
3112 ;; with a number of 'x'.
3113 (save-excursion
3114 (goto-char (car c-lit-limits))
3115 (if (looking-at (if (eq c-lit-type 'c++)
3116 c-current-comment-prefix
3117 comment-start-skip))
3118 (goto-char (match-end 0))
3119 (forward-char 2)
3120 (skip-chars-forward " \t"))
3121 (while (and (< (current-column) (cdr fill))
3122 (not (eolp)))
3123 (forward-char 1))
3124 (let ((col (current-column)))
3125 (setq beg (1+ (point))
3126 tmp-pre (list (point)))
3127 (unwind-protect
3128 (progn
3129 (insert-and-inherit "\n" (car fill))
3130 (insert-char ?x (- col (current-column)) t))
3131 (setcdr tmp-pre (point))))))
3132
3133 (when apply-outside-literal
3134 ;; `apply-outside-literal' is always set to t here if
3135 ;; we're inside a literal.
3136
3137 (let ((fill-prefix
3138 (or fill-prefix
3139 ;; Kludge: If the function that adapts the fill prefix
3140 ;; doesn't produce the required comment starter for
3141 ;; line comments, then force it by setting fill-prefix.
3142 (when (and (eq c-lit-type 'c++)
3143 ;; Kludge the kludge: filladapt-mode doesn't
3144 ;; have this problem, but it currently
3145 ;; doesn't override fill-context-prefix
3146 ;; (version 2.12).
3147 (not (and (boundp 'filladapt-mode)
3148 filladapt-mode))
3149 (not (string-match
3150 "\\`[ \t]*//"
3151 (or (fill-context-prefix beg end)
3152 ""))))
3153 (c-save-buffer-state nil
3154 (car (or fill (c-guess-fill-prefix
3155 c-lit-limits c-lit-type)))))))
3156
3157 ;; Save the relative position of point if it's outside the
3158 ;; region we're going to narrow. Want to restore it in that
3159 ;; case, but otherwise it should be moved according to the
3160 ;; called function.
3161 (point-rel (cond ((< (point) beg) (- (point) beg))
3162 ((> (point) end) (- (point) end)))))
3163
3164 ;; Preparations finally done! Now we can call the
3165 ;; actual function.
3166 (prog1
3167 (save-restriction
3168 (narrow-to-region beg end)
3169 (apply fun args))
3170 (if point-rel
3171 ;; Restore point if it was outside the region.
3172 (if (< point-rel 0)
3173 (goto-char (+ beg point-rel))
3174 (goto-char (+ end point-rel))))))))
3175
3176 (when (consp tmp-pre)
3177 (delete-region (car tmp-pre) (cdr tmp-pre)))
3178
3179 (when tmp-post
3180 (save-excursion
3181 (goto-char tmp-post)
3182 (delete-char 1))
3183 (when hang-ender-stuck
3184 ;; Preserve point even if it's in the middle of the string
3185 ;; we replace; save-excursion doesn't work in that case.
3186 (setq here (point))
3187 (goto-char tmp-post)
3188 (skip-syntax-backward "^w ")
3189 (forward-char (- hang-ender-stuck))
3190 (insert-char ?\ hang-ender-stuck t)
3191 (delete-char hang-ender-stuck)
3192 (goto-char here))
3193 (set-marker tmp-post nil))
3194
3195 (set-marker end nil))))
3196
3197 (defun c-fill-paragraph (&optional arg)
3198 "Like \\[fill-paragraph] but handles C and C++ style comments.
3199 If any of the current line is a comment or within a comment, fill the
3200 comment or the paragraph of it that point is in, preserving the
3201 comment indentation or line-starting decorations (see the
3202 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
3203 details).
3204
3205 If point is inside multiline string literal, fill it. This currently
3206 does not respect escaped newlines, except for the special case when it
3207 is the very first thing in the string. The intended use for this rule
3208 is in situations like the following:
3209
3210 char description[] = \"\\
3211 A very long description of something that you want to fill to make
3212 nicely formatted output.\"\;
3213
3214 If point is in any other situation, i.e. in normal code, do nothing.
3215
3216 Optional prefix ARG means justify paragraph as well."
3217 (interactive "*P")
3218 (let ((fill-paragraph-function
3219 ;; Avoid infinite recursion.
3220 (if (not (eq fill-paragraph-function 'c-fill-paragraph))
3221 fill-paragraph-function)))
3222 (c-mask-paragraph t nil 'fill-paragraph arg))
3223 ;; Always return t. This has the effect that if filling isn't done
3224 ;; above, it isn't done at all, and it's therefore effectively
3225 ;; disabled in normal code.
3226 t)
3227
3228 (defun c-do-auto-fill ()
3229 ;; Do automatic filling if not inside a context where it should be
3230 ;; ignored.
3231 ;;
3232 ;; This function does not do any hidden buffer changes.
3233 (let ((c-auto-fill-prefix
3234 ;; The decision whether the line should be broken is actually
3235 ;; done in c-indent-new-comment-line, which do-auto-fill
3236 ;; calls to break lines. We just set this special variable
3237 ;; so that we'll know when we're called from there. It's
3238 ;; also used to detect whether fill-prefix is user set or
3239 ;; generated automatically by do-auto-fill.
3240 fill-prefix))
3241 (c-mask-paragraph nil t 'do-auto-fill)))
3242
3243 (defun c-indent-new-comment-line (&optional soft allow-auto-fill)
3244 "Break line at point and indent, continuing comment or macro if within one.
3245 If inside a comment and `comment-multi-line' is non-nil, the
3246 indentation and line prefix are preserved (see the
3247 `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
3248 details). If inside a single line comment and `comment-multi-line' is
3249 nil, a new comment of the same type is started on the next line and
3250 indented as appropriate for comments. If inside a macro, a line
3251 continuation backslash is inserted and aligned as appropriate, and the
3252 new line is indented according to `c-syntactic-indentation'.
3253
3254 If a fill prefix is specified, it overrides all the above."
3255 ;; allow-auto-fill is used from c-context-line-break to allow auto
3256 ;; filling to break the line more than once. Since this function is
3257 ;; used from auto-fill itself, that's normally disabled to avoid
3258 ;; unnecessary recursion.
3259 (interactive)
3260 (let ((fill-prefix fill-prefix)
3261 (do-line-break
3262 (lambda ()
3263 (delete-horizontal-space)
3264 (if soft
3265 (insert-and-inherit ?\n)
3266 (newline (if allow-auto-fill nil 1)))))
3267 ;; Already know the literal type and limits when called from
3268 ;; c-context-line-break.
3269 (c-lit-limits c-lit-limits)
3270 (c-lit-type c-lit-type)
3271 (c-macro-start c-macro-start))
3272 (when (not (eq c-auto-fill-prefix t))
3273 ;; Called from do-auto-fill.
3274 (unless c-lit-limits
3275 (setq c-lit-limits (c-literal-limits nil nil t)))
3276 (unless c-lit-type
3277 (setq c-lit-type (c-literal-type c-lit-limits)))
3278 (if (memq (cond ((c-query-and-set-macro-start) 'cpp)
3279 ((null c-lit-type) 'code)
3280 (t c-lit-type))
3281 c-ignore-auto-fill)
3282 (setq fill-prefix t) ; Used as flag in the cond.
3283 (if (and (null c-auto-fill-prefix)
3284 (eq c-lit-type 'c)
3285 (<= (c-point 'bol) (car c-lit-limits)))
3286 ;; The adaptive fill function has generated a prefix, but
3287 ;; we're on the first line in a block comment so it'll be
3288 ;; wrong. Ignore it to guess a better one below.
3289 (setq fill-prefix nil)
3290 (when (and (eq c-lit-type 'c++)
3291 (not (string-match "\\`[ \t]*//" (or fill-prefix ""))))
3292 ;; Kludge: If the function that adapted the fill prefix
3293 ;; doesn't produce the required comment starter for line
3294 ;; comments, then we ignore it.
3295 (setq fill-prefix nil)))
3296 ))
3297 (cond ((eq fill-prefix t)
3298 ;; A call from do-auto-fill which should be ignored.
3299 )
3300 (fill-prefix
3301 ;; A fill-prefix overrides anything.
3302 (funcall do-line-break)
3303 (insert-and-inherit fill-prefix))
3304 ((progn
3305 (unless c-lit-limits
3306 (setq c-lit-limits (c-literal-limits)))
3307 (unless c-lit-type
3308 (setq c-lit-type (c-literal-type c-lit-limits)))
3309 (memq c-lit-type '(c c++)))
3310 ;; Some sort of comment.
3311 (if (or comment-multi-line
3312 (save-excursion
3313 (goto-char (car c-lit-limits))
3314 (end-of-line)
3315 (< (point) (cdr c-lit-limits))))
3316 ;; Inside a comment that should be continued.
3317 (let ((fill (c-save-buffer-state nil
3318 (c-guess-fill-prefix
3319 (setq c-lit-limits
3320 (c-collect-line-comments c-lit-limits))
3321 c-lit-type)))
3322 (pos (point))
3323 (comment-text-end
3324 (or (and (eq c-lit-type 'c)
3325 (save-excursion
3326 (goto-char (- (cdr c-lit-limits) 2))
3327 (if (looking-at "\\*/") (point))))
3328 (cdr c-lit-limits))))
3329 ;; Skip forward past the fill prefix in case
3330 ;; we're standing in it.
3331 ;;
3332 ;; FIXME: This doesn't work well in cases like
3333 ;;
3334 ;; /* Bla bla bla bla bla
3335 ;; bla bla
3336 ;;
3337 ;; If point is on the 'B' then the line will be
3338 ;; broken after "Bla b".
3339 (while (and (< (current-column) (cdr fill))
3340 (not (eolp)))
3341 (forward-char 1))
3342 (if (and (> (point) comment-text-end)
3343 (> (c-point 'bol) (car c-lit-limits)))
3344 (progn
3345 ;; The skip takes us out of the (block)
3346 ;; comment; insert the fill prefix at bol
3347 ;; instead and keep the position.
3348 (setq pos (copy-marker pos t))
3349 (beginning-of-line)
3350 (insert-and-inherit (car fill))
3351 (if soft (insert-and-inherit ?\n) (newline 1))
3352 (goto-char pos)
3353 (set-marker pos nil))
3354 ;; Don't break in the middle of a comment starter
3355 ;; or ender.
3356 (cond ((> (point) comment-text-end)
3357 (goto-char comment-text-end))
3358 ((< (point) (+ (car c-lit-limits) 2))
3359 (goto-char (+ (car c-lit-limits) 2))))
3360 (funcall do-line-break)
3361 (insert-and-inherit (car fill))))
3362 ;; Inside a comment that should be broken.
3363 (let ((comment-start comment-start)
3364 (comment-end comment-end)
3365 col)
3366 (if (eq c-lit-type 'c)
3367 (unless (string-match "[ \t]*/\\*" comment-start)
3368 (setq comment-start "/* " comment-end " */"))
3369 (unless (string-match "[ \t]*//" comment-start)
3370 (setq comment-start "// " comment-end "")))
3371 (setq col (save-excursion
3372 (back-to-indentation)
3373 (current-column)))
3374 (funcall do-line-break)
3375 (when (and comment-end (not (equal comment-end "")))
3376 (forward-char -1)
3377 (insert-and-inherit comment-end)
3378 (forward-char 1))
3379 ;; c-comment-indent may look at the current
3380 ;; indentation, so let's start out with the same
3381 ;; indentation as the previous one.
3382 (indent-to col)
3383 (insert-and-inherit comment-start)
3384 (indent-for-comment))))
3385 ((c-query-and-set-macro-start)
3386 ;; In a macro.
3387 (unless (looking-at "[ \t]*\\\\$")
3388 ;; Do not clobber the alignment of the line continuation
3389 ;; slash; c-backslash-region might look at it.
3390 (delete-horizontal-space))
3391 ;; Got an asymmetry here: In normal code this command
3392 ;; doesn't indent the next line syntactically, and otoh a
3393 ;; normal syntactically indenting newline doesn't continue
3394 ;; the macro.
3395 (c-newline-and-indent (if allow-auto-fill nil 1)))
3396 (t
3397 ;; Somewhere else in the code.
3398 (let ((col (save-excursion
3399 (beginning-of-line)
3400 (while (and (looking-at "[ \t]*\\\\?$")
3401 (= (forward-line -1) 0)))
3402 (current-indentation))))
3403 (funcall do-line-break)
3404 (indent-to col))))))
3405
3406 (defalias 'c-comment-line-break-function 'c-indent-new-comment-line)
3407 (make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line)
3408
3409 ;; advice for indent-new-comment-line for older Emacsen
3410 (unless (boundp 'comment-line-break-function)
3411 (defvar c-inside-line-break-advice nil)
3412 (defadvice indent-new-comment-line (around c-line-break-advice
3413 activate preactivate)
3414 "Call `c-indent-new-comment-line' if in CC Mode."
3415 (if (or c-inside-line-break-advice
3416 (not c-buffer-is-cc-mode))
3417 ad-do-it
3418 (let ((c-inside-line-break-advice t))
3419 (c-indent-new-comment-line (ad-get-arg 0))))))
3420
3421 (defun c-context-line-break ()
3422 "Do a line break suitable to the context.
3423
3424 When point is outside a comment or macro, insert a newline and indent
3425 according to the syntactic context, unless `c-syntactic-indentation'
3426 is nil, in which case the new line is indented as the previous
3427 non-empty line instead.
3428
3429 When point is inside the content of a preprocessor directive, a line
3430 continuation backslash is inserted before the line break and aligned
3431 appropriately. The end of the cpp directive doesn't count as inside
3432 it.
3433
3434 When point is inside a comment, continue it with the appropriate
3435 comment prefix (see the `c-comment-prefix-regexp' and
3436 `c-block-comment-prefix' variables for details). The end of a
3437 C++-style line comment doesn't count as inside it."
3438 (interactive "*")
3439 (let* ((c-lit-limits (c-literal-limits nil nil t))
3440 (c-lit-type (c-literal-type c-lit-limits))
3441 (c-macro-start c-macro-start))
3442 (if (or (eq c-lit-type 'c)
3443 (and (eq c-lit-type 'c++)
3444 (< (save-excursion
3445 (skip-chars-forward " \t")
3446 (point))
3447 (1- (cdr (setq c-lit-limits
3448 (c-collect-line-comments c-lit-limits))))))
3449 (and (or (not (looking-at "\\s *$"))
3450 (eq (char-before) ?\\))
3451 (c-query-and-set-macro-start)
3452 (<= (save-excursion
3453 (goto-char c-macro-start)
3454 (if (looking-at c-opt-cpp-start)
3455 (goto-char (match-end 0)))
3456 (point))
3457 (point))))
3458 (let ((comment-multi-line t)
3459 (fill-prefix nil))
3460 (c-indent-new-comment-line nil t))
3461 (delete-horizontal-space)
3462 (newline)
3463 ;; c-indent-line may look at the current indentation, so let's
3464 ;; start out with the same indentation as the previous line.
3465 (let ((col (save-excursion
3466 (forward-line -1)
3467 (while (and (looking-at "[ \t]*\\\\?$")
3468 (= (forward-line -1) 0)))
3469 (current-indentation))))
3470 (indent-to col))
3471 (indent-according-to-mode))))
3472
3473 (defun c-context-open-line ()
3474 "Insert a line break suitable to the context and leave point before it.
3475 This is the `c-context-line-break' equivalent to `open-line', which is
3476 normally bound to C-o. See `c-context-line-break' for the details."
3477 (interactive "*")
3478 (let ((here (point)))
3479 (unwind-protect
3480 (progn
3481 ;; Temporarily insert a non-whitespace char to keep any
3482 ;; preceding whitespace intact.
3483 (insert ?x)
3484 (c-context-line-break))
3485 (goto-char here)
3486 (delete-char 1))))
3487
3488 \f
3489 (cc-provide 'cc-cmds)
3490
3491 ;;; arch-tag: bf0611dc-d1f4-449e-9e45-4ec7c6936677
3492 ;;; cc-cmds.el ends here