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