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