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