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