Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / lisp / progmodes / cc-cmds.el
CommitLineData
0ec8351b 1;;; cc-cmds.el --- user level commands for CC Mode
785eecbb 2
92ab3834 3;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
49f70d46 4;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
d7a0267c 5;; Free Software Foundation, Inc.
785eecbb 6
e309f66c
AM
7;; Authors: 2003- Alan Mackenzie
8;; 1998- Martin Stjernholm
d9e94c22 9;; 1992-1999 Barry A. Warsaw
5858f68c
GM
10;; 1987 Dave Detlefs
11;; 1987 Stewart Clamen
785eecbb 12;; 1985 Richard M. Stallman
0ec8351b 13;; Maintainer: bug-cc-mode@gnu.org
785eecbb 14;; Created: 22-Apr-1997 (split from cc-mode.el)
a7c7b186 15;; Version: See cc-mode.el
785eecbb
RS
16;; Keywords: c languages oop
17
18;; This file is part of GNU Emacs.
19
b1fc2b50 20;; GNU Emacs is free software: you can redistribute it and/or modify
785eecbb 21;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
22;; the Free Software Foundation, either version 3 of the License, or
23;; (at your option) any later version.
785eecbb
RS
24
25;; GNU Emacs is distributed in the hope that it will be useful,
26;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28;; GNU General Public License for more details.
29
30;; You should have received a copy of the GNU General Public License
b1fc2b50 31;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
785eecbb 32
3afbc435
PJ
33;;; Commentary:
34
35;;; Code:
36
0ec8351b 37(eval-when-compile
51f606de 38 (let ((load-path
130c507e
GM
39 (if (and (boundp 'byte-compile-dest-file)
40 (stringp byte-compile-dest-file))
41 (cons (file-name-directory byte-compile-dest-file) load-path)
51f606de 42 load-path)))
d9e94c22 43 (load "cc-bytecomp" nil t)))
130c507e
GM
44
45(cc-require 'cc-defs)
46(cc-require 'cc-vars)
130c507e
GM
47(cc-require 'cc-engine)
48
49;; Silence the compiler.
0386b551 50(cc-bytecomp-defun delete-forward-p) ; XEmacs
130c507e
GM
51(cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge
52 ; which looks at this.
51f606de 53\f
51c9af45 54;; Indentation / Display syntax functions
a66cd3ee
MS
55(defvar c-fix-backslashes t)
56
a66cd3ee
MS
57(defun c-indent-line (&optional syntax quiet ignore-point-pos)
58 "Indent the current line according to the syntactic context,
59if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the
60syntactic information for the current line. Be silent about syntactic
61errors if the optional argument QUIET is non-nil, even if
62`c-report-syntactic-errors' is non-nil. Normally the position of
63point is used to decide where the old indentation is on a lines that
64is otherwise empty \(ignoring any line continuation backslash), but
65that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of
66indentation change \(in columns)."
d9e94c22 67
a66cd3ee
MS
68 (let ((line-cont-backslash (save-excursion
69 (end-of-line)
70 (eq (char-before) ?\\)))
71 (c-fix-backslashes c-fix-backslashes)
72 bs-col
73 shift-amt)
74 (when (and (not ignore-point-pos)
75 (save-excursion
76 (beginning-of-line)
77 (looking-at (if line-cont-backslash
647a3247
AM
78 ;; Don't use "\\s " - ^L doesn't count as WS
79 ;; here
80 "\\([ \t]*\\)\\\\$"
81 "\\([ \t]*\\)$")))
a66cd3ee
MS
82 (<= (point) (match-end 1)))
83 ;; Delete all whitespace after point if there's only whitespace
84 ;; on the line, so that any code that does back-to-indentation
85 ;; or similar gets the current column in this case. If this
86 ;; removes a line continuation backslash it'll be restored
87 ;; at the end.
88 (unless c-auto-align-backslashes
89 ;; Should try to keep the backslash alignment
90 ;; in this case.
91 (save-excursion
92 (goto-char (match-end 0))
93 (setq bs-col (1- (current-column)))))
94 (delete-region (point) (match-end 0))
95 (setq c-fix-backslashes t))
96 (if c-syntactic-indentation
97 (setq c-parsing-error
d9e94c22
MS
98 (or (let ((c-parsing-error nil)
99 (c-syntactic-context
100 (or syntax
101 (and (boundp 'c-syntactic-context)
102 c-syntactic-context))))
103 (c-save-buffer-state (indent)
104 (unless c-syntactic-context
105 (setq c-syntactic-context (c-guess-basic-syntax)))
106 (setq indent (c-get-syntactic-indentation
107 c-syntactic-context))
108 (and (not (c-echo-parsing-error quiet))
109 c-echo-syntactic-information-p
110 (message "syntax: %s, indent: %d"
111 c-syntactic-context indent))
112 (setq shift-amt (- indent (current-indentation))))
a66cd3ee
MS
113 (c-shift-line-indentation shift-amt)
114 (run-hooks 'c-special-indent-hook)
115 c-parsing-error)
116 c-parsing-error))
117 (let ((indent 0))
118 (save-excursion
119 (while (and (= (forward-line -1) 0)
120 (if (looking-at "\\s *\\\\?$")
121 t
122 (setq indent (current-indentation))
123 nil))))
124 (setq shift-amt (- indent (current-indentation)))
125 (c-shift-line-indentation shift-amt)))
126 (when (and c-fix-backslashes line-cont-backslash)
127 (if bs-col
128 (save-excursion
129 (indent-to bs-col)
130 (insert ?\\))
131 (when c-auto-align-backslashes
132 ;; Realign the line continuation backslash.
133 (c-backslash-region (point) (point) nil t))))
134 shift-amt))
135
136(defun c-newline-and-indent (&optional newline-arg)
0386b551 137 "Insert a newline and indent the new line.
a66cd3ee
MS
138This function fixes line continuation backslashes if inside a macro,
139and takes care to set the indentation before calling
140`indent-according-to-mode', so that lineup functions like
141`c-lineup-dont-change' works better."
d9e94c22
MS
142
143 ;; TODO: Backslashes before eol in comments and literals aren't
a66cd3ee
MS
144 ;; kept intact.
145 (let ((c-macro-start (c-query-macro-start))
146 ;; Avoid calling c-backslash-region from c-indent-line if it's
147 ;; called during the newline call, which can happen due to
148 ;; c-electric-continued-statement, for example. We also don't
149 ;; want any backslash alignment from indent-according-to-mode.
150 (c-fix-backslashes nil)
151 has-backslash insert-backslash
152 start col)
153 (save-excursion
154 (beginning-of-line)
155 (setq start (point))
156 (while (and (looking-at "[ \t]*\\\\?$")
157 (= (forward-line -1) 0)))
158 (setq col (current-indentation)))
159 (when c-macro-start
160 (if (and (eolp) (eq (char-before) ?\\))
161 (setq insert-backslash t
162 has-backslash t)
163 (setq has-backslash (eq (char-before (c-point 'eol)) ?\\))))
164 (newline newline-arg)
165 (indent-to col)
166 (when c-macro-start
167 (if insert-backslash
168 (progn
169 ;; The backslash stayed on the previous line. Insert one
170 ;; before calling c-backslash-region, so that
171 ;; bs-col-after-end in it works better. Fixup the
172 ;; backslashes on the newly inserted line.
173 (insert ?\\)
174 (backward-char)
175 (c-backslash-region (point) (point) nil t))
176 ;; The backslash moved to the new line, if there was any. Let
177 ;; c-backslash-region fix a backslash on the previous line,
178 ;; and the one that might be on the new line.
179 ;; c-auto-align-backslashes is intentionally ignored here;
180 ;; maybe the moved backslash should be left alone if it's set,
181 ;; but we fix both lines on the grounds that the old backslash
182 ;; has been moved anyway and is now in a different context.
183 (c-backslash-region start (if has-backslash (point) start) nil t)))
184 (when c-syntactic-indentation
185 ;; Reindent syntactically. The indentation done above is not
186 ;; wasted, since c-indent-line might look at the current
187 ;; indentation.
d9e94c22
MS
188 (let ((c-syntactic-context (c-save-buffer-state nil
189 (c-guess-basic-syntax))))
a66cd3ee
MS
190 ;; We temporarily insert another line break, so that the
191 ;; lineup functions will see the line as empty. That makes
192 ;; e.g. c-lineup-cpp-define more intuitive since it then
193 ;; proceeds to the preceding line in this case.
194 (insert ?\n)
195 (delete-horizontal-space)
196 (setq start (- (point-max) (point)))
197 (unwind-protect
198 (progn
199 (backward-char)
200 (indent-according-to-mode))
201 (goto-char (- (point-max) start))
202 (delete-char -1)))
203 (when has-backslash
204 ;; Must align the backslash again after reindentation. The
205 ;; c-backslash-region call above can't be optimized to ignore
206 ;; this line, since it then won't align correctly with the
207 ;; lines below if the first line in the macro is broken.
208 (c-backslash-region (point) (point) nil t)))))
209
210(defun c-show-syntactic-information (arg)
211 "Show syntactic information for current line.
212With universal argument, inserts the analysis as a comment on that line."
213 (interactive "P")
214 (let* ((c-parsing-error nil)
d9e94c22
MS
215 (syntax (if (boundp 'c-syntactic-context)
216 ;; Use `c-syntactic-context' in the same way as
217 ;; `c-indent-line', to be consistent.
218 c-syntactic-context
219 (c-save-buffer-state nil
220 (c-guess-basic-syntax)))))
a66cd3ee 221 (if (not (consp arg))
0386b551
AM
222 (let (elem pos ols)
223 (message "Syntactic analysis: %s" syntax)
224 (unwind-protect
225 (progn
226 (while syntax
227 (setq elem (pop syntax))
228 (when (setq pos (c-langelem-pos elem))
229 (push (c-put-overlay pos (1+ pos)
230 'face 'highlight)
231 ols))
232 (when (setq pos (c-langelem-2nd-pos elem))
233 (push (c-put-overlay pos (1+ pos)
234 'face 'secondary-selection)
235 ols)))
236 (sit-for 10))
237 (while ols
238 (c-delete-overlay (pop ols)))))
a66cd3ee
MS
239 (indent-for-comment)
240 (insert-and-inherit (format "%s" syntax))
241 ))
242 (c-keep-region-active))
243
244(defun c-syntactic-information-on-region (from to)
0386b551 245 "Insert a comment with the syntactic analysis on every line in the region."
a66cd3ee
MS
246 (interactive "*r")
247 (save-excursion
248 (save-restriction
249 (narrow-to-region from to)
250 (goto-char (point-min))
251 (while (not (eobp))
252 (c-show-syntactic-information '(0))
253 (forward-line)))))
254
255\f
51c9af45 256;; Minor mode functions.
0386b551
AM
257(defun c-update-modeline ()
258 (let ((fmt (format "/%s%s%s%s"
259 (if c-electric-flag "l" "")
260 (if (and c-electric-flag c-auto-newline)
261 "a" "")
262 (if c-hungry-delete-key "h" "")
263 (if (and
653d1554
TH
264 ;; subword might not be loaded.
265 (boundp 'subword-mode)
266 (symbol-value 'subword-mode))
0386b551 267 "w"
cb694ab7
AM
268 "")))
269 (bare-mode-name (if (string-match "\\(^[^/]*\\)/" mode-name)
270 (substring mode-name (match-beginning 1) (match-end 1))
271 mode-name)))
272;; (setq c-submode-indicators
273;; (if (> (length fmt) 1)
274;; fmt))
275 (setq mode-name
0386b551 276 (if (> (length fmt) 1)
17264191 277 (concat bare-mode-name fmt)
cb694ab7 278 bare-mode-name))
0386b551
AM
279 (force-mode-line-update)))
280
a66cd3ee
MS
281(defun c-toggle-syntactic-indentation (&optional arg)
282 "Toggle syntactic indentation.
283Optional numeric ARG, if supplied, turns on syntactic indentation when
284positive, turns it off when negative, and just toggles it when zero or
285left out.
286
287When syntactic indentation is turned on (the default), the indentation
288functions and the electric keys indent according to the syntactic
289context keys, when applicable.
290
0386b551
AM
291When it's turned off, the electric keys don't reindent, the indentation
292functions indents every new line to the same level as the previous
293nonempty line, and \\[c-indent-command] adjusts the indentation in steps
294specified by `c-basic-offset'. The indentation style has no effect in
295this mode, nor any of the indentation associated variables,
296e.g. `c-special-indent-hook'.
a66cd3ee
MS
297
298This command sets the variable `c-syntactic-indentation'."
299 (interactive "P")
300 (setq c-syntactic-indentation
301 (c-calculate-state arg c-syntactic-indentation))
302 (c-keep-region-active))
303
0386b551 304(defun c-toggle-auto-newline (&optional arg)
785eecbb 305 "Toggle auto-newline feature.
130c507e
GM
306Optional numeric ARG, if supplied, turns on auto-newline when
307positive, turns it off when negative, and just toggles it when zero or
308left out.
785eecbb 309
0386b551
AM
310Turning on auto-newline automatically enables electric indentation.
311
312When the auto-newline feature is enabled (indicated by \"/la\" on the
313modeline after the mode name) newlines are automatically inserted
314after special characters such as brace, comma, semi-colon, and colon."
785eecbb 315 (interactive "P")
0386b551
AM
316 (setq c-auto-newline
317 (c-calculate-state arg (and c-auto-newline c-electric-flag)))
318 (if c-auto-newline (setq c-electric-flag t))
785eecbb
RS
319 (c-update-modeline)
320 (c-keep-region-active))
321
0386b551 322(defalias 'c-toggle-auto-state 'c-toggle-auto-newline)
efbc652a 323(make-obsolete 'c-toggle-auto-state 'c-toggle-auto-newline "22.1")
0386b551 324
130c507e 325(defun c-toggle-hungry-state (&optional arg)
785eecbb 326 "Toggle hungry-delete-key feature.
130c507e
GM
327Optional numeric ARG, if supplied, turns on hungry-delete when
328positive, turns it off when negative, and just toggles it when zero or
329left out.
785eecbb 330
0386b551
AM
331When the hungry-delete-key feature is enabled (indicated by \"/h\" on
332the modeline after the mode name) the delete key gobbles all preceding
333whitespace in one fell swoop."
785eecbb
RS
334 (interactive "P")
335 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
336 (c-update-modeline)
337 (c-keep-region-active))
338
130c507e 339(defun c-toggle-auto-hungry-state (&optional arg)
785eecbb 340 "Toggle auto-newline and hungry-delete-key features.
130c507e 341Optional numeric ARG, if supplied, turns on auto-newline and
785eecbb 342hungry-delete when positive, turns them off when negative, and just
130c507e 343toggles them when zero or left out.
785eecbb 344
0386b551 345See `c-toggle-auto-newline' and `c-toggle-hungry-state' for details."
785eecbb
RS
346 (interactive "P")
347 (setq c-auto-newline (c-calculate-state arg c-auto-newline))
348 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
349 (c-update-modeline)
350 (c-keep-region-active))
351
0386b551
AM
352(defun c-toggle-electric-state (&optional arg)
353 "Toggle the electric indentation feature.
354Optional numeric ARG, if supplied, turns on electric indentation when
355positive, turns it off when negative, and just toggles it when zero or
356left out."
357 (interactive "P")
358 (setq c-electric-flag (c-calculate-state arg c-electric-flag))
359 (c-update-modeline)
360 (c-keep-region-active))
361
785eecbb
RS
362\f
363;; Electric keys
364
785eecbb 365(defun c-electric-backspace (arg)
d9e94c22 366 "Delete the preceding character or whitespace.
0386b551
AM
367If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
368line) then all preceding whitespace is consumed. If however a prefix
369argument is supplied, or `c-hungry-delete-key' is nil, or point is
370inside a literal then the function in the variable
371`c-backspace-function' is called."
28c236de 372 (interactive "*P")
0386b551
AM
373 (if (c-save-buffer-state ()
374 (or (not c-hungry-delete-key)
375 arg
376 (c-in-literal)))
785eecbb 377 (funcall c-backspace-function (prefix-numeric-value arg))
cb694ab7 378 (c-hungry-delete-backwards)))
d9e94c22 379
cb694ab7 380(defun c-hungry-delete-backwards ()
d9e94c22
MS
381 "Delete the preceding character or all preceding whitespace
382back to the previous non-whitespace character.
383See also \\[c-hungry-delete-forward]."
384 (interactive)
385 (let ((here (point)))
386 (c-skip-ws-backward)
387 (if (/= (point) here)
388 (delete-region (point) here)
389 (funcall c-backspace-function 1))))
785eecbb 390
cb694ab7
AM
391(defalias 'c-hungry-backspace 'c-hungry-delete-backwards)
392
fdea67e7 393(defun c-electric-delete-forward (arg)
d9e94c22 394 "Delete the following character or whitespace.
0386b551
AM
395If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
396line) then all following whitespace is consumed. If however a prefix
397argument is supplied, or `c-hungry-delete-key' is nil, or point is
398inside a literal then the function in the variable `c-delete-function'
399is called."
fdea67e7 400 (interactive "*P")
0386b551
AM
401 (if (c-save-buffer-state ()
402 (or (not c-hungry-delete-key)
403 arg
404 (c-in-literal)))
fdea67e7 405 (funcall c-delete-function (prefix-numeric-value arg))
d9e94c22
MS
406 (c-hungry-delete-forward)))
407
408(defun c-hungry-delete-forward ()
409 "Delete the following character or all following whitespace
410up to the next non-whitespace character.
cb694ab7 411See also \\[c-hungry-delete-backwards]."
d9e94c22
MS
412 (interactive)
413 (let ((here (point)))
414 (c-skip-ws-forward)
415 (if (/= (point) here)
416 (delete-region (point) here)
417 (funcall c-delete-function 1))))
fdea67e7 418
d9e94c22 419;; This function is only used in XEmacs.
785eecbb
RS
420(defun c-electric-delete (arg)
421 "Deletes preceding or following character or whitespace.
fdea67e7 422This function either deletes forward as `c-electric-delete-forward' or
0386b551
AM
423backward as `c-electric-backspace', depending on the configuration: If
424the function `delete-forward-p' is defined and returns non-nil, it
425deletes forward. Otherwise it deletes backward.
426
427Note: This is the way in XEmacs to choose the correct action for the
428\[delete] key, whichever key that means. Other flavors don't use this
429function to control that."
28c236de 430 (interactive "*P")
0386b551
AM
431 (if (and (fboundp 'delete-forward-p)
432 (delete-forward-p))
fdea67e7 433 (c-electric-delete-forward arg)
785eecbb
RS
434 (c-electric-backspace arg)))
435
0386b551
AM
436;; This function is only used in XEmacs.
437(defun c-hungry-delete ()
438 "Delete a non-whitespace char, or all whitespace up to the next non-whitespace char.
439The direction of deletion depends on the configuration: If the
440function `delete-forward-p' is defined and returns non-nil, it deletes
441forward using `c-hungry-delete-forward'. Otherwise it deletes
442backward using `c-hungry-backspace'.
443
444Note: This is the way in XEmacs to choose the correct action for the
445\[delete] key, whichever key that means. Other flavors don't use this
446function to control that."
447 (interactive)
448 (if (and (fboundp 'delete-forward-p)
449 (delete-forward-p))
450 (c-hungry-delete-forward)
cb694ab7 451 (c-hungry-delete-backwards)))
0386b551 452
785eecbb 453(defun c-electric-pound (arg)
0386b551
AM
454 "Insert a \"#\".
455If `c-electric-flag' is set, handle it specially according to the variable
456`c-electric-pound-behavior'. If a numeric ARG is supplied, or if point is
457inside a literal or a macro, nothing special happens."
28c236de 458 (interactive "*P")
0386b551
AM
459 (if (c-save-buffer-state ()
460 (or arg
461 (not c-electric-flag)
462 (not (memq 'alignleft c-electric-pound-behavior))
463 (save-excursion
464 (skip-chars-backward " \t")
465 (not (bolp)))
466 (save-excursion
467 (and (= (forward-line -1) 0)
468 (progn (end-of-line)
469 (eq (char-before) ?\\))))
470 (c-in-literal)))
785eecbb
RS
471 ;; do nothing special
472 (self-insert-command (prefix-numeric-value arg))
473 ;; place the pound character at the left edge
474 (let ((pos (- (point-max) (point)))
475 (bolp (bolp)))
476 (beginning-of-line)
477 (delete-horizontal-space)
1ba983e8 478 (insert last-command-event)
785eecbb
RS
479 (and (not bolp)
480 (goto-char (- (point-max) pos)))
481 )))
482
0386b551
AM
483(defun c-point-syntax ()
484 ;; Return the syntactic context of the construct at point. (This is NOT
485 ;; nec. the same as the s.c. of the line point is on). N.B. This won't work
486 ;; between the `#' of a cpp thing and what follows (see c-opt-cpp-prefix).
487 (c-save-buffer-state (;; shut this up too
488 (c-echo-syntactic-information-p nil)
489 syntax)
490 (c-tentative-buffer-changes
491 ;; insert a newline to isolate the construct at point for syntactic
492 ;; analysis.
493 (insert-char ?\n 1)
494 ;; In AWK (etc.) or in a macro, make sure this CR hasn't changed
495 ;; the syntax. (There might already be an escaped NL there.)
496 (when (or (c-at-vsemi-p (1- (point)))
497 (let ((pt (point)))
498 (save-excursion
499 (backward-char)
500 (and (c-beginning-of-macro)
501 (progn (c-end-of-macro)
502 (< (point) pt))))))
503 (backward-char)
504 (insert-char ?\\ 1)
505 (forward-char))
506 (let ((c-syntactic-indentation-in-macros t)
507 (c-auto-newline-analysis t))
508 ;; Turn on syntactic macro analysis to help with auto
509 ;; newlines only.
510 (setq syntax (c-guess-basic-syntax))
511 nil))
512 syntax))
513
514(defun c-brace-newlines (syntax)
515 ;; A brace stands at point. SYNTAX is the syntactic context of this brace
516 ;; (not necessarily the same as the S.C. of the line it is on). Return
517 ;; NEWLINES, the list containing some combination of the symbols `before'
518 ;; and `after' saying where newlines should be inserted.
519 (c-save-buffer-state
520 ((syms
521 ;; This is the list of brace syntactic symbols that can hang.
522 ;; If any new ones are added to c-offsets-alist, they should be
523 ;; added here as well.
3f71582d 524 ;;
4fae8922
AM
525 ;; The order of this list is important; if SYNTAX has several
526 ;; elements, the element that "wins" is the earliest in SYMS.
527 '(arglist-cont-nonempty ; e.g. an array literal.
528 class-open class-close defun-open defun-close
0386b551
AM
529 inline-open inline-close
530 brace-list-open brace-list-close
531 brace-list-intro brace-entry-open
532 block-open block-close
533 substatement-open statement-case-open
534 extern-lang-open extern-lang-close
535 namespace-open namespace-close
536 module-open module-close
537 composition-open composition-close
538 inexpr-class-open inexpr-class-close
539 ;; `statement-cont' is here for the case with a brace
540 ;; list opener inside a statement. C.f. CASE B.2 in
541 ;; `c-guess-continued-construct'.
542 statement-cont))
543 ;; shut this up too
544 (c-echo-syntactic-information-p nil)
545 symb-newlines) ; e.g. (substatement-open . (after))
17264191 546
0386b551
AM
547 (setq symb-newlines
548 ;; Do not try to insert newlines around a special
549 ;; (Pike-style) brace list.
550 (if (and c-special-brace-lists
551 (save-excursion
552 (c-safe (if (= (char-before) ?{)
553 (forward-char -1)
554 (c-forward-sexp -1))
555 (c-looking-at-special-brace-list))))
556 nil
557 ;; Seek the matching entry in c-hanging-braces-alist.
558 (or (c-lookup-lists
559 syms
560 ;; Substitute inexpr-class and class-open or
561 ;; class-close with inexpr-class-open or
562 ;; inexpr-class-close.
563 (if (assq 'inexpr-class syntax)
564 (cond ((assq 'class-open syntax)
565 '((inexpr-class-open)))
566 ((assq 'class-close syntax)
567 '((inexpr-class-close)))
568 (t syntax))
569 syntax)
570 c-hanging-braces-alist)
571 '(ignore before after)))) ; Default, when not in c-h-b-l.
572
573 ;; If syntax is a function symbol, then call it using the
574 ;; defined semantics.
575 (if (and (not (consp (cdr symb-newlines)))
576 (functionp (cdr symb-newlines)))
577 (let ((c-syntactic-context syntax))
578 (funcall (cdr symb-newlines)
579 (car symb-newlines)
580 (point)))
581 (cdr symb-newlines))))
582
583(defun c-try-one-liner ()
584 ;; Point is just after a newly inserted }. If the non-whitespace
585 ;; content of the braces is a single line of code, compact the whole
586 ;; construct to a single line, if this line isn't too long. The Right
587 ;; Thing is done with comments.
588 ;;
589 ;; Point will be left after the }, regardless of whether the clean-up is
590 ;; done. Return NON-NIL if the clean-up happened, NIL if it didn't.
591
592 (let ((here (point))
593 (pos (- (point-max) (point)))
594 mbeg1 mend1 mbeg4 mend4
595 eol-col cmnt-pos cmnt-col cmnt-gap)
596
597 (when
598 (save-excursion
599 (save-restriction
600 ;; Avoid backtracking over a very large block. The one we
601 ;; deal with here can never be more than three lines.
602 (narrow-to-region (save-excursion
603 (forward-line -2)
604 (point))
605 (point))
606 (and (c-safe (c-backward-sexp))
607 (progn
608 (forward-char)
609 (narrow-to-region (point) (1- here)) ; innards of {.}
610 (looking-at
611 (cc-eval-when-compile
612 (concat
613 "\\(" ; (match-beginning 1)
614 "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
615 "\\)" ; (match-end 1)
616 "[^ \t\r\n]+\\([ \t]+[^ \t\r\n]+\\)*" ; non-WS
617 "\\(" ; (match-beginning 4)
618 "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
619 "\\)\\'"))))))) ; (match-end 4) at EOB.
620
621 (if (c-tentative-buffer-changes
622 (setq mbeg1 (match-beginning 1) mend1 (match-end 1)
623 mbeg4 (match-beginning 4) mend4 (match-end 4))
624 (backward-char) ; back over the `}'
625 (save-excursion
626 (setq cmnt-pos (and (c-backward-single-comment)
627 (- (point) (- mend1 mbeg1)))))
628 (delete-region mbeg4 mend4)
629 (delete-region mbeg1 mend1)
630 (setq eol-col (save-excursion (end-of-line) (current-column)))
631
632 ;; Necessary to put the closing brace before any line
633 ;; oriented comment to keep it syntactically significant.
634 ;; This isn't necessary for block comments, but the result
635 ;; looks nicer anyway.
636 (when cmnt-pos
637 (delete-char 1) ; the `}' has blundered into a comment
638 (goto-char cmnt-pos)
639 (setq cmnt-col (1+ (current-column)))
640 (setq cmnt-pos (1+ cmnt-pos)) ; we're inserting a `}'
641 (c-skip-ws-backward)
642 (insert-char ?\} 1) ; reinsert the `}' before the comment.
643 (setq cmnt-gap (- cmnt-col (current-column)))
644 (when (zerop cmnt-gap)
645 (insert-char ?\ 1) ; Put a space before a bare comment.
646 (setq cmnt-gap 1)))
647
648 (or (null c-max-one-liner-length)
649 (zerop c-max-one-liner-length)
650 (<= eol-col c-max-one-liner-length)
651 ;; Can we trim space before comment to make the line fit?
652 (and cmnt-gap
653 (< (- eol-col cmnt-gap) c-max-one-liner-length)
654 (progn (goto-char cmnt-pos)
655 (backward-delete-char-untabify
656 (- eol-col c-max-one-liner-length))
657 t))))
658 (goto-char (- (point-max) pos))))))
659
785eecbb
RS
660(defun c-electric-brace (arg)
661 "Insert a brace.
662
0386b551
AM
663If `c-electric-flag' is non-nil, the brace is not inside a literal and a
664numeric ARG hasn't been supplied, the command performs several electric
665actions:
785eecbb 666
cb694ab7 667\(a) If the auto-newline feature is turned on (indicated by \"/la\" on
0386b551
AM
668the mode line) newlines are inserted before and after the brace as
669directed by the settings in `c-hanging-braces-alist'.
670
671\(b) Any auto-newlines are indented. The original line is also
672reindented unless `c-syntactic-indentation' is nil.
673
674\(c) If auto-newline is turned on, various newline cleanups based on the
675settings of `c-cleanup-list' are done."
0ec8351b 676
28c236de 677 (interactive "*P")
0386b551
AM
678 (let (safepos literal
679 ;; We want to inhibit blinking the paren since this would be
680 ;; most disruptive. We'll blink it ourselves later on.
681 (old-blink-paren blink-paren-function)
682 blink-paren-function)
683
684 (c-save-buffer-state ()
685 (setq safepos (c-safe-position (point) (c-parse-state))
686 literal (c-in-literal safepos)))
687
688 ;; Insert the brace. Note that expand-abbrev might reindent
689 ;; the line here if there's a preceding "else" or something.
690 (self-insert-command (prefix-numeric-value arg))
691
692 (when (and c-electric-flag (not literal) (not arg))
693 (if (not (looking-at "[ \t]*\\\\?$"))
694 (if c-syntactic-indentation
695 (indent-according-to-mode))
696
697 (let ( ;; shut this up too
698 (c-echo-syntactic-information-p nil)
699 newlines
700 ln-syntax br-syntax syntax) ; Syntactic context of the original line,
701 ; of the brace itself, of the line the brace ends up on.
702 (c-save-buffer-state ((c-syntactic-indentation-in-macros t)
703 (c-auto-newline-analysis t))
704 (setq ln-syntax (c-guess-basic-syntax)))
705 (if c-syntactic-indentation
706 (c-indent-line ln-syntax))
707
708 (when c-auto-newline
709 (backward-char)
710 (setq br-syntax (c-point-syntax)
711 newlines (c-brace-newlines br-syntax))
712
713 ;; Insert the BEFORE newline, if wanted, and reindent the newline.
714 (if (and (memq 'before newlines)
715 (> (current-column) (current-indentation)))
716 (if c-syntactic-indentation
717 ;; Only a plain newline for now - it's indented
718 ;; after the cleanups when the line has its final
719 ;; appearance.
720 (newline)
721 (c-newline-and-indent)))
722 (forward-char)
723
724 ;; `syntax' is the syntactic context of the line which ends up
725 ;; with the brace on it.
726 (setq syntax (if (memq 'before newlines) br-syntax ln-syntax))
727
728 ;; Do all appropriate clean ups
729 (let ((here (point))
730 (pos (- (point-max) (point)))
731 mbeg mend
732 )
733
734 ;; `}': clean up empty defun braces
735 (when (c-save-buffer-state ()
736 (and (memq 'empty-defun-braces c-cleanup-list)
1ba983e8 737 (eq last-command-event ?\})
0386b551
AM
738 (c-intersect-lists '(defun-close class-close inline-close)
739 syntax)
740 (progn
741 (forward-char -1)
742 (c-skip-ws-backward)
743 (eq (char-before) ?\{))
744 ;; make sure matching open brace isn't in a comment
745 (not (c-in-literal))))
746 (delete-region (point) (1- here))
747 (setq here (- (point-max) pos)))
748 (goto-char here)
749
750 ;; `}': compact to a one-liner defun?
751 (save-match-data
752 (when
1ba983e8 753 (and (eq last-command-event ?\})
0386b551
AM
754 (memq 'one-liner-defun c-cleanup-list)
755 (c-intersect-lists '(defun-close) syntax)
756 (c-try-one-liner))
757 (setq here (- (point-max) pos))))
758
759 ;; `{': clean up brace-else-brace and brace-elseif-brace
1ba983e8 760 (when (eq last-command-event ?\{)
0386b551
AM
761 (cond
762 ((and (memq 'brace-else-brace c-cleanup-list)
763 (re-search-backward
764 (concat "}"
765 "\\([ \t\n]\\|\\\\\n\\)*"
766 "else"
767 "\\([ \t\n]\\|\\\\\n\\)*"
768 "{"
769 "\\=")
770 nil t))
cb694ab7 771 (delete-region (match-beginning 0) (match-end 0))
0386b551
AM
772 (insert-and-inherit "} else {"))
773 ((and (memq 'brace-elseif-brace c-cleanup-list)
774 (progn
775 (goto-char (1- here))
776 (setq mend (point))
777 (c-skip-ws-backward)
778 (setq mbeg (point))
779 (eq (char-before) ?\)))
780 (zerop (c-save-buffer-state nil (c-backward-token-2 1 t)))
781 (eq (char-after) ?\()
782 ; (progn
783 ; (setq tmp (point))
784 (re-search-backward
785 (concat "}"
786 "\\([ \t\n]\\|\\\\\n\\)*"
787 "else"
788 "\\([ \t\n]\\|\\\\\n\\)+"
789 "if"
790 "\\([ \t\n]\\|\\\\\n\\)*"
791 "\\=")
792 nil t);)
793 ;(eq (match-end 0) tmp);
794 )
795 (delete-region mbeg mend)
796 (goto-char mbeg)
797 (insert ?\ ))))
798
799 (goto-char (- (point-max) pos))
800
801 ;; Indent the line after the cleanups since it might
802 ;; very well indent differently due to them, e.g. if
803 ;; c-indent-one-line-block is used together with the
804 ;; one-liner-defun cleanup.
805 (when c-syntactic-indentation
806 (c-indent-line)))
807
808 ;; does a newline go after the brace?
809 (if (memq 'after newlines)
810 (c-newline-and-indent))
811 ))))
812
a66cd3ee 813 ;; blink the paren
1ba983e8 814 (and (eq last-command-event ?\})
a66cd3ee
MS
815 (not executing-kbd-macro)
816 old-blink-paren
817 (save-excursion
d9e94c22
MS
818 (c-save-buffer-state nil
819 (c-backward-syntactic-ws safepos))
a66cd3ee 820 (funcall old-blink-paren)))))
0ec8351b 821
785eecbb
RS
822(defun c-electric-slash (arg)
823 "Insert a slash character.
0fc3821e 824
0386b551
AM
825If the slash is inserted immediately after the comment prefix in a c-style
826comment, the comment might get closed by removing whitespace and possibly
827inserting a \"*\". See the variable `c-cleanup-list'.
828
0fc3821e
RS
829Indent the line as a comment, if:
830
0386b551 831 1. The slash is second of a \"//\" line oriented comment introducing
0fc3821e
RS
832 token and we are on a comment-only-line, or
833
0386b551 834 2. The slash is part of a \"*/\" token that closes a block oriented
0fc3821e
RS
835 comment.
836
b8ded794 837If a numeric ARG is supplied, point is inside a literal, or
0386b551
AM
838`c-syntactic-indentation' is nil or `c-electric-flag' is nil, indentation
839is inhibited."
28c236de 840 (interactive "*P")
0386b551
AM
841 (let ((literal (c-save-buffer-state () (c-in-literal)))
842 indentp
843 ;; shut this up
844 (c-echo-syntactic-information-p nil))
845
846 ;; comment-close-slash cleanup? This DOESN'T need `c-electric-flag' or
847 ;; `c-syntactic-indentation' set.
848 (when (and (not arg)
849 (eq literal 'c)
850 (memq 'comment-close-slash c-cleanup-list)
1ba983e8 851 (eq last-command-event ?/)
51c9af45
AM
852 (looking-at (concat "[ \t]*\\("
853 (regexp-quote comment-end) "\\)?$"))
0386b551
AM
854 ; (eq c-block-comment-ender "*/") ; C-style comments ALWAYS end in */
855 (save-excursion
51c9af45
AM
856 (save-restriction
857 (narrow-to-region (point-min) (point))
858 (back-to-indentation)
859 (looking-at (concat c-current-comment-prefix "[ \t]*$")))))
0abe900b 860 (delete-region (progn (forward-line 0) (point))
c05ddcf7 861 (progn (end-of-line) (point)))
51c9af45 862 (insert-char ?* 1)) ; the / comes later. ; Do I need a t (retain sticky properties) here?
0386b551
AM
863
864 (setq indentp (and (not arg)
865 c-syntactic-indentation
866 c-electric-flag
1ba983e8 867 (eq last-command-event ?/)
0386b551 868 (eq (char-before) (if literal ?* ?/))))
785eecbb
RS
869 (self-insert-command (prefix-numeric-value arg))
870 (if indentp
130c507e 871 (indent-according-to-mode))))
785eecbb
RS
872
873(defun c-electric-star (arg)
874 "Insert a star character.
0386b551
AM
875If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, and
876the star is the second character of a C style comment starter on a
877comment-only-line, indent the line as a comment. If a numeric ARG is
878supplied, point is inside a literal, or `c-syntactic-indentation' is nil,
879this indentation is inhibited."
880
28c236de 881 (interactive "*P")
785eecbb 882 (self-insert-command (prefix-numeric-value arg))
0386b551 883 ;; if we are in a literal, or if arg is given do not reindent the
785eecbb 884 ;; current line, unless this star introduces a comment-only line.
0386b551
AM
885 (if (c-save-buffer-state ()
886 (and c-syntactic-indentation
887 c-electric-flag
888 (not arg)
889 (eq (c-in-literal) 'c)
890 (eq (char-before) ?*)
891 (save-excursion
892 (forward-char -1)
893 (skip-chars-backward "*")
894 (if (eq (char-before) ?/)
895 (forward-char -1))
896 (skip-chars-backward " \t")
897 (bolp))))
130c507e
GM
898 (let (c-echo-syntactic-information-p) ; shut this up
899 (indent-according-to-mode))
785eecbb
RS
900 ))
901
902(defun c-electric-semi&comma (arg)
903 "Insert a comma or semicolon.
785eecbb 904
0386b551
AM
905If `c-electric-flag' is non-nil, point isn't inside a literal and a
906numeric ARG hasn't been supplied, the command performs several electric
907actions:
908
cb694ab7 909\(a) When the auto-newline feature is turned on (indicated by \"/la\" on
0386b551
AM
910the mode line) a newline might be inserted. See the variable
911`c-hanging-semi&comma-criteria' for how newline insertion is determined.
0ec8351b 912
0386b551
AM
913\(b) Any auto-newlines are indented. The original line is also
914reindented unless `c-syntactic-indentation' is nil.
915
916\(c) If auto-newline is turned on, a comma following a brace list or a
917semicolon following a defun might be cleaned up, depending on the
918settings of `c-cleanup-list'."
28c236de 919 (interactive "*P")
0386b551 920 (let* (lim literal c-syntactic-context
785eecbb
RS
921 (here (point))
922 ;; shut this up
923 (c-echo-syntactic-information-p nil))
0386b551
AM
924
925 (c-save-buffer-state ()
926 (setq lim (c-most-enclosing-brace (c-parse-state))
927 literal (c-in-literal lim)))
928
929 (self-insert-command (prefix-numeric-value arg))
930
931 (if (and c-electric-flag (not literal) (not arg))
932 ;; do all cleanups and newline insertions if c-auto-newline is on.
933 (if (or (not c-auto-newline)
934 (not (looking-at "[ \t]*\\\\?$")))
935 (if c-syntactic-indentation
936 (c-indent-line))
937 ;; clean ups: list-close-comma or defun-close-semi
938 (let ((pos (- (point-max) (point))))
939 (if (c-save-buffer-state ()
940 (and (or (and
1ba983e8 941 (eq last-command-event ?,)
0386b551
AM
942 (memq 'list-close-comma c-cleanup-list))
943 (and
1ba983e8 944 (eq last-command-event ?\;)
0386b551
AM
945 (memq 'defun-close-semi c-cleanup-list)))
946 (progn
947 (forward-char -1)
948 (c-skip-ws-backward)
949 (eq (char-before) ?}))
950 ;; make sure matching open brace isn't in a comment
951 (not (c-in-literal lim))))
952 (delete-region (point) here))
953 (goto-char (- (point-max) pos)))
954 ;; reindent line
955 (when c-syntactic-indentation
956 (setq c-syntactic-context (c-guess-basic-syntax))
957 (c-indent-line c-syntactic-context))
958 ;; check to see if a newline should be added
959 (let ((criteria c-hanging-semi&comma-criteria)
960 answer add-newline-p)
961 (while criteria
962 (setq answer (funcall (car criteria)))
963 ;; only nil value means continue checking
964 (if (not answer)
965 (setq criteria (cdr criteria))
966 (setq criteria nil)
967 ;; only 'stop specifically says do not add a newline
968 (setq add-newline-p (not (eq answer 'stop)))
969 ))
970 (if add-newline-p
971 (c-newline-and-indent))
972 )))))
785eecbb
RS
973
974(defun c-electric-colon (arg)
975 "Insert a colon.
976
0386b551
AM
977If `c-electric-flag' is non-nil, the colon is not inside a literal and a
978numeric ARG hasn't been supplied, the command performs several electric
979actions:
980
cb694ab7 981\(a) If the auto-newline feature is turned on (indicated by \"/la\" on
0386b551
AM
982the mode line) newlines are inserted before and after the colon based on
983the settings in `c-hanging-colons-alist'.
785eecbb 984
0386b551
AM
985\(b) Any auto-newlines are indented. The original line is also
986reindented unless `c-syntactic-indentation' is nil.
987
988\(c) If auto-newline is turned on, whitespace between two colons will be
989\"cleaned up\" leaving a scope operator, if this action is set in
990`c-cleanup-list'."
785eecbb 991
28c236de 992 (interactive "*P")
785eecbb 993 (let* ((bod (c-point 'bod))
0386b551 994 (literal (c-save-buffer-state () (c-in-literal bod)))
a66cd3ee 995 newlines is-scope-op
785eecbb
RS
996 ;; shut this up
997 (c-echo-syntactic-information-p nil))
0386b551
AM
998 (self-insert-command (prefix-numeric-value arg))
999 ;; Any electric action?
1000 (if (and c-electric-flag (not literal) (not arg))
1001 ;; Unless we're at EOL, only re-indentation happens.
1002 (if (not (looking-at "[ \t]*\\\\?$"))
1003 (if c-syntactic-indentation
1004 (indent-according-to-mode))
1005
1006 ;; scope-operator clean-up?
1007 (let ((pos (- (point-max) (point)))
1008 (here (point)))
1009 (if (c-save-buffer-state () ; Why do we need this? [ACM, 2003-03-12]
1010 (and c-auto-newline
1011 (memq 'scope-operator c-cleanup-list)
1012 (eq (char-before) ?:)
1013 (progn
1014 (forward-char -1)
1015 (c-skip-ws-backward)
1016 (eq (char-before) ?:))
1017 (not (c-in-literal))
1018 (not (eq (char-after (- (point) 2)) ?:))))
1019 (progn
1020 (delete-region (point) (1- here))
1021 (setq is-scope-op t)))
1022 (goto-char (- (point-max) pos)))
1023
1024 ;; indent the current line if it's done syntactically.
1025 (if c-syntactic-indentation
1026 ;; Cannot use the same syntax analysis as we find below,
1027 ;; since that's made with c-syntactic-indentation-in-macros
1028 ;; always set to t.
1029 (indent-according-to-mode))
1030
1031 ;; Calculate where, if anywhere, we want newlines.
1032 (c-save-buffer-state
1033 ((c-syntactic-indentation-in-macros t)
1034 (c-auto-newline-analysis t)
1035 ;; Turn on syntactic macro analysis to help with auto newlines
1036 ;; only.
1037 (syntax (c-guess-basic-syntax))
1038 (elem syntax))
1039 ;; Translate substatement-label to label for this operation.
1040 (while elem
1041 (if (eq (car (car elem)) 'substatement-label)
1042 (setcar (car elem) 'label))
1043 (setq elem (cdr elem)))
1044 ;; some language elements can only be determined by checking
1045 ;; the following line. Lets first look for ones that can be
1046 ;; found when looking on the line with the colon
1047 (setq newlines
1048 (and c-auto-newline
1049 (or (c-lookup-lists '(case-label label access-label)
1050 syntax c-hanging-colons-alist)
1051 (c-lookup-lists '(member-init-intro inher-intro)
1052 (progn
1053 (insert ?\n)
1054 (unwind-protect
1055 (c-guess-basic-syntax)
1056 (delete-char -1)))
1057 c-hanging-colons-alist)))))
1058 ;; does a newline go before the colon? Watch out for already
1059 ;; non-hung colons. However, we don't unhang them because that
1060 ;; would be a cleanup (and anti-social).
1061 (if (and (memq 'before newlines)
1062 (not is-scope-op)
1063 (save-excursion
1064 (skip-chars-backward ": \t")
1065 (not (bolp))))
1066 (let ((pos (- (point-max) (point))))
1067 (forward-char -1)
1068 (c-newline-and-indent)
1069 (goto-char (- (point-max) pos))))
1070 ;; does a newline go after the colon?
1071 (if (and (memq 'after (cdr-safe newlines))
1072 (not is-scope-op))
1073 (c-newline-and-indent))
1074 ))))
785eecbb
RS
1075
1076(defun c-electric-lt-gt (arg)
0386b551
AM
1077 "Insert a \"<\" or \">\" character.
1078If the current language uses angle bracket parens (e.g. template
1079arguments in C++), try to find out if the inserted character is a
1080paren and give it paren syntax if appropriate.
1081
1082If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, the
1083line will be reindented if the inserted character is a paren or if it
1084finishes a C++ style stream operator in C++ mode. Exceptions are when a
1085numeric argument is supplied, or the point is inside a literal."
1086
28c236de 1087 (interactive "*P")
0386b551 1088 (let ((c-echo-syntactic-information-p nil)
7af4bf3a 1089 final-pos close-paren-inserted)
0386b551 1090
785eecbb 1091 (self-insert-command (prefix-numeric-value arg))
0386b551
AM
1092 (setq final-pos (point))
1093
7af4bf3a
AM
1094 (c-save-buffer-state (c-parse-and-markup-<>-arglists
1095 c-restricted-<>-arglists
1096 <-pos)
1097
1098 (when c-recognize-<>-arglists
1099 (if (eq last-command-event ?<)
1100 (when (and (progn
1101 (backward-char)
1102 (= (point)
1103 (progn
1104 (c-beginning-of-current-token)
1105 (point))))
dd969a56 1106 (progn
7af4bf3a
AM
1107 (c-backward-token-2)
1108 (looking-at c-opt-<>-sexp-key)))
1109 (c-mark-<-as-paren (1- final-pos)))
1110
1111 ;; It's a ">". Check if there's an earlier "<" which either has
1112 ;; open paren syntax already or that can be recognized as an arglist
1113 ;; together with this ">". Note that this won't work in cases like
1114 ;; "template <x, a < b, y>" but they ought to be rare.
0386b551 1115
7af4bf3a
AM
1116 (save-restriction
1117 ;; Narrow to avoid that `c-forward-<>-arglist' below searches past
1118 ;; our position.
1119 (narrow-to-region (point-min) final-pos)
1120
1121 (while (and
1122 (progn
1123 (goto-char final-pos)
1124 (c-syntactic-skip-backward "^<;}" nil t)
1125 (eq (char-before) ?<))
1126 (progn
1127 (backward-char)
1128 ;; If the "<" already got open paren syntax we know we
1129 ;; have the matching closer. Handle it and exit the
1130 ;; loop.
1131 (if (looking-at "\\s\(")
1132 (progn
1133 (c-mark->-as-paren (1- final-pos))
1134 (setq close-paren-inserted t)
1135 nil)
1136 t))
1137
1138 (progn
1139 (setq <-pos (point))
1140 (c-backward-syntactic-ws)
1141 (c-simple-skip-symbol-backward))
1142 (or (looking-at c-opt-<>-sexp-key)
1143 (not (looking-at c-keywords-regexp)))
1144
1145 (let ((c-parse-and-markup-<>-arglists t)
1146 c-restricted-<>-arglists
1147 (containing-sexp
1148 (c-most-enclosing-brace (c-parse-state))))
1149 (when (and containing-sexp
1150 (progn (goto-char containing-sexp)
1151 (eq (char-after) ?\())
1152 (not (eq (get-text-property (point) 'c-type)
1153 'c-decl-arg-start)))
1154 (setq c-restricted-<>-arglists t))
1155 (goto-char <-pos)
1156 (c-forward-<>-arglist nil))
1157
1158 ;; Loop here if the "<" we found above belongs to a nested
1159 ;; angle bracket sexp. When we start over we'll find the
1160 ;; previous or surrounding sexp.
1161 (if (< (point) final-pos)
1162 t
1163 (setq close-paren-inserted t)
1164 nil)))))))
0386b551 1165 (goto-char final-pos)
7af4bf3a
AM
1166
1167 ;; Indent the line if appropriate.
1168 (when (and c-electric-flag c-syntactic-indentation)
1169 (backward-char)
1170 (when (prog1 (or (looking-at "\\s\(\\|\\s\)")
1171 (and (c-major-mode-is 'c++-mode)
1172 (progn
1173 (c-beginning-of-current-token)
1174 (looking-at "<<\\|>>"))
1175 (= (match-end 0) final-pos)))
1176 (goto-char final-pos))
1177 (indent-according-to-mode)))
1178
1179 (when (and close-paren-inserted
1180 (not executing-kbd-macro)
1181 blink-paren-function)
1182 ;; Note: Most paren blink functions, such as the standard
1183 ;; `blink-matching-open', currently doesn't handle paren chars
1184 ;; marked with text properties very well. Maybe we should avoid
1185 ;; this call for the time being?
1186 (funcall blink-paren-function))))
785eecbb 1187
0ec8351b
BW
1188(defun c-electric-paren (arg)
1189 "Insert a parenthesis.
1190
0386b551
AM
1191If `c-syntactic-indentation' and `c-electric-flag' are both non-nil, the
1192line is reindented unless a numeric ARG is supplied, or the parenthesis
1193is inserted inside a literal.
0ec8351b 1194
0386b551
AM
1195Whitespace between a function name and the parenthesis may get added or
1196removed; see the variable `c-cleanup-list'.
1197
1198Also, if `c-electric-flag' and `c-auto-newline' are both non-nil, some
1199newline cleanups are done if appropriate; see the variable `c-cleanup-list'."
0ec8351b 1200 (interactive "*P")
0386b551 1201 (let ((literal (c-save-buffer-state () (c-in-literal)))
a66cd3ee 1202 ;; shut this up
0ec8351b 1203 (c-echo-syntactic-information-p nil))
0386b551
AM
1204 (self-insert-command (prefix-numeric-value arg))
1205
1206 (if (and (not arg) (not literal))
1207 (let* ( ;; We want to inhibit blinking the paren since this will
1208 ;; be most disruptive. We'll blink it ourselves
1209 ;; afterwards.
1210 (old-blink-paren blink-paren-function)
1211 blink-paren-function)
1212 (if (and c-syntactic-indentation c-electric-flag)
1213 (indent-according-to-mode))
1214
1215 ;; If we're at EOL, check for new-line clean-ups.
1216 (when (and c-electric-flag c-auto-newline
1217 (looking-at "[ \t]*\\\\?$"))
1218
1219 ;; clean up brace-elseif-brace
1220 (when
1221 (and (memq 'brace-elseif-brace c-cleanup-list)
1ba983e8 1222 (eq last-command-event ?\()
0386b551
AM
1223 (re-search-backward
1224 (concat "}"
1225 "\\([ \t\n]\\|\\\\\n\\)*"
1226 "else"
1227 "\\([ \t\n]\\|\\\\\n\\)+"
1228 "if"
1229 "\\([ \t\n]\\|\\\\\n\\)*"
1230 "("
1231 "\\=")
1232 nil t)
1233 (not (c-save-buffer-state () (c-in-literal))))
1234 (delete-region (match-beginning 0) (match-end 0))
1235 (insert-and-inherit "} else if ("))
1236
1237 ;; clean up brace-catch-brace
1238 (when
1239 (and (memq 'brace-catch-brace c-cleanup-list)
1ba983e8 1240 (eq last-command-event ?\()
0386b551
AM
1241 (re-search-backward
1242 (concat "}"
1243 "\\([ \t\n]\\|\\\\\n\\)*"
1244 "catch"
1245 "\\([ \t\n]\\|\\\\\n\\)*"
1246 "("
1247 "\\=")
1248 nil t)
1249 (not (c-save-buffer-state () (c-in-literal))))
1250 (delete-region (match-beginning 0) (match-end 0))
1251 (insert-and-inherit "} catch (")))
1252
1253 ;; Check for clean-ups at function calls. These two DON'T need
1254 ;; `c-electric-flag' or `c-syntactic-indentation' set.
1255 ;; Point is currently just after the inserted paren.
1256 (let (beg (end (1- (point))))
1257 (cond
1258
1259 ;; space-before-funcall clean-up?
1260 ((and (memq 'space-before-funcall c-cleanup-list)
1ba983e8 1261 (eq last-command-event ?\()
0386b551
AM
1262 (save-excursion
1263 (backward-char)
1264 (skip-chars-backward " \t")
1265 (setq beg (point))
8ccf9f7a
AM
1266 (and (c-save-buffer-state () (c-on-identifier))
1267 ;; Don't add a space into #define FOO()....
1268 (not (and (c-beginning-of-macro)
1269 (c-forward-over-cpp-define-id)
1270 (eq (point) beg))))))
0386b551
AM
1271 (save-excursion
1272 (delete-region beg end)
1273 (goto-char beg)
1274 (insert ?\ )))
1275
1276 ;; compact-empty-funcall clean-up?
1277 ((c-save-buffer-state ()
1278 (and (memq 'compact-empty-funcall c-cleanup-list)
1ba983e8 1279 (eq last-command-event ?\))
0386b551
AM
1280 (save-excursion
1281 (c-safe (backward-char 2))
1282 (when (looking-at "()")
1283 (setq end (point))
1284 (skip-chars-backward " \t")
1285 (setq beg (point))
1286 (c-on-identifier)))))
1287 (delete-region beg end))))
1288 (and (eq last-input-event ?\))
1289 (not executing-kbd-macro)
1290 old-blink-paren
1291 (funcall old-blink-paren))))))
0ec8351b 1292
130c507e
GM
1293(defun c-electric-continued-statement ()
1294 "Reindent the current line if appropriate.
1295
1296This function is used to reindent the line after a keyword which
1297continues an earlier statement is typed, e.g. an \"else\" or the
1298\"while\" in a do-while block.
1299
1300The line is reindented if there is nothing but whitespace before the
1301keyword on the line, the keyword is not inserted inside a literal, and
0386b551 1302`c-electric-flag' and `c-syntactic-indentation' are both non-nil."
130c507e
GM
1303 (let (;; shut this up
1304 (c-echo-syntactic-information-p nil))
0386b551
AM
1305 (when (c-save-buffer-state ()
1306 (and c-electric-flag
1307 c-syntactic-indentation
1ba983e8 1308 (not (eq last-command-event ?_))
0386b551
AM
1309 (= (save-excursion
1310 (skip-syntax-backward "w")
1311 (point))
1312 (c-point 'boi))
1313 (not (c-in-literal (c-point 'bod)))))
a66cd3ee
MS
1314 ;; Have to temporarily insert a space so that
1315 ;; c-guess-basic-syntax recognizes the keyword. Follow the
1316 ;; space with a nonspace to avoid messing up any whitespace
1317 ;; sensitive meddling that might be done, e.g. by
1318 ;; `c-backslash-region'.
1319 (insert-and-inherit " x")
1320 (unwind-protect
1321 (indent-according-to-mode)
1322 (delete-char -2)))))
785eecbb
RS
1323
1324\f
ab84bfa0 1325
a9b76eec
TH
1326(declare-function subword-forward "subword" (&optional arg))
1327(declare-function subword-backward "subword" (&optional arg))
ab84bfa0 1328
51c9af45 1329;; "nomenclature" functions + c-scope-operator.
785eecbb 1330(defun c-forward-into-nomenclature (&optional arg)
0386b551 1331 "Compatibility alias for `c-forward-subword'."
785eecbb 1332 (interactive "p")
653d1554 1333 (require 'subword)
a9b76eec
TH
1334 (subword-forward arg))
1335(make-obsolete 'c-forward-into-nomenclature 'subword-forward "23.2")
785eecbb
RS
1336
1337(defun c-backward-into-nomenclature (&optional arg)
0386b551 1338 "Compatibility alias for `c-backward-subword'."
785eecbb 1339 (interactive "p")
653d1554 1340 (require 'subword)
a9b76eec
TH
1341 (subword-backward arg))
1342(make-obsolete 'c-backward-into-nomenclature 'subword-backward "23.2")
785eecbb
RS
1343
1344(defun c-scope-operator ()
1345 "Insert a double colon scope operator at point.
1346No indentation or other \"electric\" behavior is performed."
28c236de 1347 (interactive "*")
a66cd3ee 1348 (insert-and-inherit "::"))
785eecbb 1349
51c9af45
AM
1350\f
1351;; Movement (etc.) by defuns.
1352(defun c-in-function-trailer-p (&optional lim)
1353 ;; Return non-nil if point is between the closing brace and the semicolon of
1354 ;; a brace construct which needs a semicolon, e.g. within the "variables"
1355 ;; portion of a declaration like "struct foo {...} bar ;".
1356 ;;
1357 ;; Return the position of the main declaration. Otherwise, return nil.
1358 ;; Point is assumed to be at the top level and outside of any macro or
1359 ;; literal.
1360 ;;
1361 ;; If LIM is non-nil, it is the bound on a the backward search for the
1362 ;; beginning of the declaration.
1363 ;;
1364 ;; This function might do hidden buffer changes.
1365 (and c-opt-block-decls-with-vars-key
1366 (save-excursion
1367 (c-syntactic-skip-backward "^;}" lim)
8007b73c
AM
1368 (let ((eo-block (point))
1369 bod)
1370 (and (eq (char-before) ?\})
1371 (eq (car (c-beginning-of-decl-1 lim)) 'previous)
1372 (setq bod (point))
1373 ;; Look for struct or union or ... If we find one, it might
1374 ;; be the return type of a function, or the like. Exclude
1375 ;; this case.
1376 (c-syntactic-re-search-forward
1377 (concat "[;=\(\[{]\\|\\("
1378 c-opt-block-decls-with-vars-key
1379 "\\)")
1380 eo-block t t t)
1381 (match-beginning 1) ; Is there a "struct" etc., somewhere?
1382 (not (eq (char-before) ?_))
1383 (c-syntactic-re-search-forward "[;=\(\[{]" eo-block t t t)
1384 (eq (char-before) ?\{)
1385 bod)))))
51c9af45
AM
1386
1387(defun c-where-wrt-brace-construct ()
1388 ;; Determine where we are with respect to functions (or other brace
1389 ;; constructs, included in the term "function" in the rest of this comment).
1390 ;; Point is assumed to be outside any macro or literal.
1391 ;; This is used by c-\(begining\|end\)-of-defun.
1392 ;;
1393 ;; Return one of these symbols:
1394 ;; at-header : we're at the start of a function's header.
1395 ;; in-header : we're inside a function's header, this extending right
1396 ;; up to the brace. This bit includes any k&r declarations.
1397 ;; in-block : we're inside a function's brace block.
1398 ;; in-trailer : we're in the area between the "}" and ";" of something
1399 ;; like "struct foo {...} bar, baz;".
1400 ;; at-function-end : we're just after the closing brace (or semicolon) that
1401 ;; terminates the function.
1402 ;; outwith-function: we're not at or in any function. Being inside a
1403 ;; non-brace construct also counts as 'outwith-function'.
1404 ;;
1405 ;; This function might do hidden buffer changes.
1406 (save-excursion
f325b570 1407 (let* (kluge-start
51c9af45
AM
1408 decl-result brace-decl-p
1409 (start (point))
1410 (paren-state (c-parse-state))
1411 (least-enclosing (c-least-enclosing-brace paren-state)))
1412
1413 (cond
1414 ((and least-enclosing
1415 (eq (char-after least-enclosing) ?\{))
1416 'in-block)
1417 ((c-in-function-trailer-p)
1418 'in-trailer)
1419 ((and (not least-enclosing)
1420 (consp paren-state)
1421 (consp (car paren-state))
1422 (eq start (cdar paren-state)))
1423 'at-function-end)
1424 (t
1425 ;; Find the start of the current declaration. NOTE: If we're in the
1426 ;; variables after a "struct/eval" type block, we don't get to the
1427 ;; real declaration here - we detect and correct for this later.
1428
1429 ;;If we're in the parameters' parens, move back out of them.
1430 (if least-enclosing (goto-char least-enclosing))
1431 ;; Kluge so that c-beginning-of-decl-1 won't go back if we're already
1432 ;; at a declaration.
1433 (if (or (and (eolp) (not (eobp))) ; EOL is matched by "\\s>"
1434 (not (looking-at
1435"\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\|\\s!\\)")))
1436 (forward-char))
1437 (setq kluge-start (point))
1438 (setq decl-result
1439 (car (c-beginning-of-decl-1
f325b570
AM
1440 ;; NOTE: If we're in a K&R region, this might be the start
1441 ;; of a parameter declaration, not the actual function.
51c9af45
AM
1442 (and least-enclosing ; LIMIT for c-b-of-decl-1
1443 (c-safe-position least-enclosing paren-state)))))
1444
1445 ;; Has the declaration we've gone back to got braces?
51c9af45
AM
1446 (setq brace-decl-p
1447 (save-excursion
1448 (and (c-syntactic-re-search-forward "[;{]" nil t t)
1449 (or (eq (char-before) ?\{)
1450 (and c-recognize-knr-p
1451 ;; Might have stopped on the
1452 ;; ';' in a K&R argdecl. In
1453 ;; that case the declaration
1454 ;; should contain a block.
f325b570 1455 (c-in-knr-argdecl))))))
51c9af45
AM
1456
1457 (cond
1458 ((= (point) kluge-start) ; might be BOB or unbalanced parens.
1459 'outwith-function)
1460 ((eq decl-result 'same)
1461 (if brace-decl-p
1462 (if (eq (point) start)
1463 'at-header
1464 'in-header)
1465 'outwith-function))
1466 ((eq decl-result 'previous)
1467 (if (and (not brace-decl-p)
1468 (c-in-function-trailer-p))
1469 'at-function-end
1470 'outwith-function))
1471 (t (error
1472 "c-where-wrt-brace-construct: c-beginning-of-decl-1 returned %s"
1473 decl-result))))))))
1474
1475(defun c-backward-to-nth-BOF-{ (n where)
1476 ;; Skip to the opening brace of the Nth function before point. If
1477 ;; point is inside a function, this counts as the first. Point must be
1478 ;; outside any comment/string or macro.
1479 ;;
1480 ;; N must be strictly positive.
1481 ;; WHERE describes the position of point, one of the symbols `at-header',
1482 ;; `in-header', `in-block', `in-trailer', `at-function-end',
1483 ;; `outwith-function' as returned by c-where-wrt-brace-construct.
1484 ;;
1485 ;; If we run out of functions, leave point at BOB. Return zero on success,
1486 ;; otherwise the number of {s still to go.
1487 ;;
1488 ;; This function may do hidden buffer changes
1489 (cond
1490 ;; What we do to go back the first defun depends on where we start.
1491 ((bobp))
1492 ((eq where 'in-block)
1493 (goto-char (c-least-enclosing-brace (c-parse-state)))
1494 (setq n (1- n)))
1495 ((eq where 'in-header)
1496 (c-syntactic-re-search-forward "{")
1497 (backward-char)
1498 (setq n (1- n)))
15279e74 1499 ((memq where '(at-header outwith-function at-function-end in-trailer))
51c9af45
AM
1500 (c-syntactic-skip-backward "^}")
1501 (when (eq (char-before) ?\})
1502 (backward-sexp)
1503 (setq n (1- n))))
1504 (t (error "Unknown `where' %s in c-backward-to-nth-EOF-{" where)))
1505
1506 ;; Each time round the loop, go back to a "{" at the outermost level.
1507 (while (and (> n 0) (not (bobp)))
1508 (c-parse-state) ; This call speeds up the following one
1509 ; by a factor of ~6. Hmmm. 2006/4/5.
1510 (c-syntactic-skip-backward "^}")
1511 (when (eq (char-before) ?\})
1512 (backward-sexp)
1513 (setq n (1- n))))
1514 n)
1515
28c236de
RS
1516(defun c-beginning-of-defun (&optional arg)
1517 "Move backward to the beginning of a defun.
a66cd3ee
MS
1518Every top level declaration that contains a brace paren block is
1519considered to be a defun.
1520
1521With a positive argument, move backward that many defuns. A negative
1522argument -N means move forward to the Nth following beginning. Return
1523t unless search stops due to beginning or end of buffer.
28c236de
RS
1524
1525Unlike the built-in `beginning-of-defun' this tries to be smarter
1526about finding the char with open-parenthesis syntax that starts the
1527defun."
a66cd3ee 1528
28c236de 1529 (interactive "p")
a66cd3ee
MS
1530 (or arg (setq arg 1))
1531
51c9af45 1532 (c-save-buffer-state
28abe5e2
AM
1533 (beginning-of-defun-function end-of-defun-function
1534 (start (point))
51c9af45 1535 where paren-state pos)
a66cd3ee 1536
51c9af45
AM
1537 ;; Move back out of any macro/comment/string we happen to be in.
1538 (c-beginning-of-macro)
1539 (setq pos (c-literal-limits))
1540 (if pos (goto-char (car pos)))
1541
1542 (setq where (c-where-wrt-brace-construct))
1543
1544 (if (< arg 0)
1545 ;; Move forward to the closing brace of a function.
1546 (progn
15279e74 1547 (if (memq where '(at-function-end outwith-function))
51c9af45
AM
1548 (setq arg (1+ arg)))
1549 (if (< arg 0)
1550 (setq arg (c-forward-to-nth-EOF-} (- arg) where)))
1551 ;; Move forward to the next opening brace....
1552 (when (and (= arg 0)
f7510c40 1553 (c-syntactic-re-search-forward "{" nil 'eob))
51c9af45
AM
1554 (backward-char)
1555 ;; ... and backward to the function header.
1556 (c-beginning-of-decl-1)
1557 t))
1558
1559 ;; Move backward to the opening brace of a function.
1560 (when (and (> arg 0)
1561 (eq (setq arg (c-backward-to-nth-BOF-{ arg where)) 0))
1562
1563 ;; Go backward to this function's header.
1564 (c-beginning-of-decl-1)
1565
1566 (setq pos (point))
1567 ;; We're now there, modulo comments and whitespace.
1568 ;; Try to be line oriented; position point at the closest
1569 ;; preceding boi that isn't inside a comment, but if we hit
1570 ;; the previous declaration then we use the current point
1571 ;; instead.
1572 (while (and (/= (point) (c-point 'boi))
1573 (c-backward-single-comment)))
1574 (if (/= (point) (c-point 'boi))
1575 (goto-char pos)))
1576
1577 (c-keep-region-active)
1578 (= arg 0))))
1579
1580(defun c-forward-to-nth-EOF-} (n where)
1581 ;; Skip to the closing brace of the Nth function after point. If
1582 ;; point is inside a function, this counts as the first. Point must be
1583 ;; outside any comment/string or macro.
1584 ;;
1585 ;; N must be strictly positive.
1586 ;; WHERE describes the position of point, one of the symbols `at-header',
1587 ;; `in-header', `in-block', `in-trailer', `at-function-end',
1588 ;; `outwith-function' as returned by c-where-wrt-brace-construct.
1589 ;;
1590 ;; If we run out of functions, leave point at EOB. Return zero on success,
1591 ;; otherwise the number of }s still to go.
1592 ;;
1593 ;; This function may do hidden buffer changes.
1594
1595 (cond
1596 ;; What we do to go forward over the first defun depends on where we
1597 ;; start. We go to the closing brace of that defun, even when we go
1598 ;; backwards to it (in a "struct foo {...} bar ;").
1599 ((eobp))
1600 ((eq where 'in-block)
1601 (goto-char (c-least-enclosing-brace (c-parse-state)))
1602 (forward-sexp)
1603 (setq n (1- n)))
1604 ((eq where 'in-trailer)
1605 (c-syntactic-skip-backward "^}")
1606 (setq n (1- n)))
15279e74
AM
1607 ((memq where '(at-function-end outwith-function at-header in-header))
1608 (when (c-syntactic-re-search-forward "{" nil 'eob)
1609 (backward-char)
1610 (forward-sexp)
1611 (setq n (1- n))))
51c9af45
AM
1612 (t (error "c-forward-to-nth-EOF-}: `where' is %s" where)))
1613
1614 ;; Each time round the loop, go forward to a "}" at the outermost level.
1615 (while (and (> n 0) (not (eobp)))
1616 ;(c-parse-state) ; This call speeds up the following one by a factor
1617 ; of ~6. Hmmm. 2006/4/5.
1618 (when (c-syntactic-re-search-forward "{" nil 'eob)
1619 (backward-char)
1620 (forward-sexp))
1621 (setq n (1- n)))
1622 n)
28c236de
RS
1623
1624(defun c-end-of-defun (&optional arg)
a66cd3ee
MS
1625 "Move forward to the end of a top level declaration.
1626With argument, do it that many times. Negative argument -N means move
1627back to Nth preceding end. Returns t unless search stops due to
1628beginning or end of buffer.
28c236de
RS
1629
1630An end of a defun occurs right after the close-parenthesis that matches
1631the open-parenthesis that starts a defun; see `beginning-of-defun'."
1632 (interactive "p")
a66cd3ee
MS
1633 (or arg (setq arg 1))
1634
51c9af45 1635 (c-save-buffer-state
28abe5e2
AM
1636 (beginning-of-defun-function end-of-defun-function
1637 (start (point))
51c9af45
AM
1638 where paren-state pos)
1639
1640 ;; Move back out of any macro/comment/string we happen to be in.
1641 (c-beginning-of-macro)
1642 (setq pos (c-literal-limits))
1643 (if pos (goto-char (car pos)))
1644
1645 (setq where (c-where-wrt-brace-construct))
1646
1647 (if (< arg 0)
1648 ;; Move backwards to the } of a function
1649 (progn
15279e74 1650 (if (memq where '(at-header outwith-function))
51c9af45
AM
1651 (setq arg (1+ arg)))
1652 (if (< arg 0)
1653 (setq arg (c-backward-to-nth-BOF-{ (- arg) where)))
de5c0bc1
AM
1654 (if (= arg 0)
1655 (c-syntactic-skip-backward "^}")))
51c9af45
AM
1656
1657 ;; Move forward to the } of a function
1658 (if (> arg 0)
1659 (setq arg (c-forward-to-nth-EOF-} arg where))))
1660
1661 ;; Do we need to move forward from the brace to the semicolon?
1662 (when (eq arg 0)
1663 (if (c-in-function-trailer-p) ; after "}" of struct/enum, etc.
1664 (c-syntactic-re-search-forward ";"))
1665
1666 (setq pos (point))
1667 ;; We're there now, modulo comments and whitespace.
1668 ;; Try to be line oriented; position point after the next
1669 ;; newline that isn't inside a comment, but if we hit the
1670 ;; next declaration then we use the current point instead.
1671 (while (and (not (bolp))
1672 (not (looking-at "\\s *$"))
1673 (c-forward-single-comment)))
1674 (cond ((bolp))
1675 ((looking-at "\\s *$")
1676 (forward-line 1))
1677 (t
1678 (goto-char pos))))
a66cd3ee 1679
51f606de
GM
1680 (c-keep-region-active)
1681 (= arg 0)))
28c236de 1682
fa7056bc
AM
1683(defun c-defun-name ()
1684 "Return the name of the current defun, or NIL if there isn't one.
1685\"Defun\" here means a function, or other top level construct
1686with a brace block."
1687 (interactive)
1688 (c-save-buffer-state
1689 (beginning-of-defun-function end-of-defun-function
1690 where pos name-end)
3f71582d 1691
649504a1
AM
1692 (save-restriction
1693 (widen)
1694 (save-excursion
1695 ;; Move back out of any macro/comment/string we happen to be in.
1696 (c-beginning-of-macro)
1697 (setq pos (c-literal-limits))
1698 (if pos (goto-char (car pos)))
1699
1700 (setq where (c-where-wrt-brace-construct))
1701
1702 ;; Move to the beginning of the current defun, if any, if we're not
1703 ;; already there.
1704 (if (eq where 'outwith-function)
1705 nil
1706 (unless (eq where 'at-header)
1707 (c-backward-to-nth-BOF-{ 1 where)
1708 (c-beginning-of-decl-1))
1709
1710 ;; Pick out the defun name, according to the type of defun.
1711 (cond
1712 ;; struct, union, enum, or similar:
1713 ((and (looking-at c-type-prefix-key)
1714 (progn (c-forward-token-2 2) ; over "struct foo "
1715 (or (eq (char-after) ?\{)
1716 (looking-at c-symbol-key)))) ; "struct foo bar ..."
1717 (save-match-data (c-forward-token-2))
1718 (when (eq (char-after) ?\{)
1719 (c-backward-token-2)
1720 (looking-at c-symbol-key))
1721 (match-string-no-properties 0))
1722
1723 ((looking-at "DEFUN\\_>")
1724 ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory
1725 ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK
1726 (down-list 1)
1727 (c-forward-syntactic-ws)
1728 (when (eq (char-after) ?\")
1729 (forward-sexp 1)
1730 (c-forward-token-2)) ; over the comma and following WS.
1731 (buffer-substring-no-properties
1732 (point)
1733 (progn
1734 (c-forward-token-2)
1735 (when (looking-at ":") ; CLISP: DEFUN(PACKAGE:LISP-SYMBOL,...)
1736 (skip-chars-forward "^,"))
1737 (c-backward-syntactic-ws)
1738 (point))))
1739
1740 ((looking-at "DEF[a-zA-Z0-9_]* *( *\\([^, ]*\\) *,")
1741 ;; DEFCHECKER(sysconf_arg,prefix=_SC,default=, ...) ==> sysconf_arg
1742 ;; DEFFLAGSET(syslog_opt_flags,LOG_PID ...) ==> syslog_opt_flags
1743 (match-string-no-properties 1))
1744
1745 (t
1746 ;; Normal function or initializer.
1747 (when (c-syntactic-re-search-forward "[{(]" nil t)
1748 (backward-char)
1749 (c-backward-syntactic-ws)
1750 (when (eq (char-before) ?\=) ; struct foo bar = {0, 0} ;
1751 (c-backward-token-2)
1752 (c-backward-syntactic-ws))
1753 (setq name-end (point))
fa7056bc 1754 (c-backward-token-2)
649504a1 1755 (buffer-substring-no-properties (point) name-end)))))))))
fa7056bc 1756
a66cd3ee
MS
1757(defun c-declaration-limits (near)
1758 ;; Return a cons of the beginning and end positions of the current
1759 ;; top level declaration or macro. If point is not inside any then
1760 ;; nil is returned, unless NEAR is non-nil in which case the closest
1761 ;; following one is chosen instead (if there is any). The end
1762 ;; position is at the next line, providing there is one before the
1763 ;; declaration.
0386b551
AM
1764 ;;
1765 ;; This function might do hidden buffer changes.
a66cd3ee
MS
1766 (save-excursion
1767
1768 ;; Note: Some code duplication in `c-beginning-of-defun' and
1769 ;; `c-end-of-defun'.
1770 (catch 'exit
1771 (let ((start (point))
1772 (paren-state (c-parse-state))
1773 lim pos end-pos)
1774 (unless (c-safe
1775 (goto-char (c-least-enclosing-brace paren-state))
1776 ;; If we moved to the outermost enclosing paren then we
1777 ;; can use c-safe-position to set the limit. Can't do
1778 ;; that otherwise since the earlier paren pair on
1779 ;; paren-state might very well be part of the
1780 ;; declaration we should go to.
1781 (setq lim (c-safe-position (point) paren-state))
1782 t)
1783 ;; At top level. Make sure we aren't inside a literal.
1784 (setq pos (c-literal-limits
1785 (c-safe-position (point) paren-state)))
1786 (if pos (goto-char (car pos))))
1787
1788 (when (c-beginning-of-macro)
1789 (throw 'exit
1790 (cons (point)
1791 (save-excursion
1792 (c-end-of-macro)
1793 (forward-line 1)
1794 (point)))))
1795
1796 (setq pos (point))
1797 (when (or (eq (car (c-beginning-of-decl-1 lim)) 'previous)
1798 (= pos (point)))
1799 ;; We moved back over the previous defun. Skip to the next
1800 ;; one. Not using c-forward-syntactic-ws here since we
1801 ;; should not skip a macro. We can also be directly after
1802 ;; the block in a `c-opt-block-decls-with-vars-key'
1803 ;; declaration, but then we won't move significantly far
1804 ;; here.
1805 (goto-char pos)
d9e94c22 1806 (c-forward-comments)
a66cd3ee
MS
1807
1808 (when (and near (c-beginning-of-macro))
1809 (throw 'exit
1810 (cons (point)
1811 (save-excursion
1812 (c-end-of-macro)
1813 (forward-line 1)
1814 (point))))))
1815
1816 (if (eobp) (throw 'exit nil))
1817
1818 ;; Check if `c-beginning-of-decl-1' put us after the block in a
1819 ;; declaration that doesn't end there. We're searching back and
1820 ;; forth over the block here, which can be expensive.
1821 (setq pos (point))
1822 (if (and c-opt-block-decls-with-vars-key
1823 (progn
1824 (c-backward-syntactic-ws)
1825 (eq (char-before) ?}))
1826 (eq (car (c-beginning-of-decl-1))
1827 'previous)
1828 (save-excursion
1829 (c-end-of-decl-1)
1830 (and (> (point) pos)
1831 (setq end-pos (point)))))
1832 nil
1833 (goto-char pos))
1834
1835 (if (and (not near) (> (point) start))
1836 nil
1837
1838 ;; Try to be line oriented; position the limits at the
1839 ;; closest preceding boi, and after the next newline, that
1840 ;; isn't inside a comment, but if we hit a neighboring
1841 ;; declaration then we instead use the exact declaration
1842 ;; limit in that direction.
1843 (cons (progn
1844 (setq pos (point))
1845 (while (and (/= (point) (c-point 'boi))
d9e94c22 1846 (c-backward-single-comment)))
a66cd3ee
MS
1847 (if (/= (point) (c-point 'boi))
1848 pos
1849 (point)))
1850 (progn
1851 (if end-pos
1852 (goto-char end-pos)
1853 (c-end-of-decl-1))
1854 (setq pos (point))
1855 (while (and (not (bolp))
1856 (not (looking-at "\\s *$"))
d9e94c22 1857 (c-forward-single-comment)))
a66cd3ee
MS
1858 (cond ((bolp)
1859 (point))
1860 ((looking-at "\\s *$")
1861 (forward-line 1)
1862 (point))
1863 (t
1864 pos)))))
1865 ))))
1866
1867(defun c-mark-function ()
1868 "Put mark at end of the current top-level declaration or macro, point at beginning.
1869If point is not inside any then the closest following one is chosen.
1870
1871As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
1872function does not require the declaration to contain a brace block."
1873 (interactive)
1874
d9e94c22
MS
1875 (let (decl-limits)
1876 (c-save-buffer-state nil
1877 ;; We try to be line oriented, unless there are several
1878 ;; declarations on the same line.
1879 (if (looking-at c-syntactic-eol)
1880 (c-backward-token-2 1 nil (c-point 'bol)))
1881 (setq decl-limits (c-declaration-limits t)))
a66cd3ee 1882
a66cd3ee
MS
1883 (if (not decl-limits)
1884 (error "Cannot find any declaration")
1885 (goto-char (car decl-limits))
1886 (push-mark (cdr decl-limits) nil t))))
1887
fa7056bc
AM
1888(defun c-cpp-define-name ()
1889 "Return the name of the current CPP macro, or NIL if we're not in one."
1890 (interactive)
1891 (save-excursion
1892 (and c-opt-cpp-macro-define-start
1893 (c-beginning-of-macro)
1894 (looking-at c-opt-cpp-macro-define-start)
1895 (match-string-no-properties 1))))
1896
785eecbb 1897\f
51c9af45 1898;; Movement by statements.
0386b551
AM
1899(defun c-in-comment-line-prefix-p ()
1900 ;; Point is within a comment. Is it also within a comment-prefix?
1901 ;; Space at BOL which precedes a comment-prefix counts as part of it.
1902 ;;
1903 ;; This function might do hidden buffer changes.
1904 (let ((here (point)))
1905 (save-excursion
1906 (beginning-of-line)
1907 (skip-chars-forward " \t")
1908 (and (looking-at c-current-comment-prefix)
1909 (/= (match-beginning 0) (match-end 0))
1910 (< here (match-end 0))))))
1911
1912(defun c-narrow-to-comment-innards (range)
1913 ;; Narrow to the "inside" of the comment (block) defined by range, as
1914 ;; follows:
17264191 1915 ;;
0386b551
AM
1916 ;; A c-style block comment has its opening "/*" and its closing "*/" (if
1917 ;; present) removed. A c++-style line comment retains its opening "//" but
1918 ;; has any final NL removed. If POINT is currently outwith these innards,
1919 ;; move it to the appropriate boundary.
17264191 1920 ;;
0386b551
AM
1921 ;; This narrowing simplifies the sentence movement functions, since it
1922 ;; eliminates awkward things at the boundaries of the comment (block).
1923 ;;
1924 ;; This function might do hidden buffer changes.
1925 (let* ((lit-type (c-literal-type range))
1926 (beg (if (eq lit-type 'c) (+ (car range) 2) (car range)))
1927 (end (if (eq lit-type 'c)
1928 (if (and (eq (char-before (cdr range)) ?/)
1929 (eq (char-before (1- (cdr range))) ?*))
1930 (- (cdr range) 2)
1931 (point-max))
1932 (if (eq (cdr range) (point-max))
1933 (point-max)
1934 (- (cdr range) 1)))))
1935 (if (> (point) end)
1936 (goto-char end)) ; This would be done automatically by ...
1937 (if (< (point) beg)
1938 (goto-char beg)) ; ... narrow-to-region but is not documented.
1939 (narrow-to-region beg end)))
1940
1941(defun c-beginning-of-sentence-in-comment (range)
1942 ;; Move backwards to the "beginning of a sentence" within the comment
1943 ;; defined by RANGE, a cons of its starting and ending positions. If we
1944 ;; find a BOS, return NIL. Otherwise, move point to just before the start
1945 ;; of the comment and return T.
1946 ;;
1947 ;; The BOS is either text which follows a regexp match of sentence-end,
17264191 1948 ;; or text which is a beginning of "paragraph".
0386b551
AM
1949 ;; Comment-prefixes are treated like WS when calculating BOSes or BOPs.
1950 ;;
1951 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
1952 ;; It is not a general function, but is intended only for calling from
1953 ;; c-move-over-sentence. Not all preconditions have been explicitly stated.
1954 ;;
1955 ;; This function might do hidden buffer changes.
1956 (save-match-data
1957 (let ((start-point (point)))
1958 (save-restriction
1959 (c-narrow-to-comment-innards range) ; This may move point back.
1960 (let* ((here (point))
1961 last
1962 (here-filler ; matches WS and comment-prefices at point.
1963 (concat "\\=\\(^[ \t]*\\(" c-current-comment-prefix "\\)"
1964 "\\|[ \t\n\r\f]\\)*"))
1965 (prefix-at-bol-here ; matches WS and prefix at BOL, just before point
1966 (concat "^[ \t]*\\(" c-current-comment-prefix "\\)[ \t\n\r\f]*\\="))
1967 ;; First, find the previous paragraph start, if any.
1968 (par-beg ; point where non-WS/non-prefix text of paragraph starts.
1969 (save-excursion
1970 (forward-paragraph -1) ; uses cc-mode values of
1971 ; paragraph-\(start\|separate\)
1972 (if (> (re-search-forward here-filler nil t) here)
1973 (goto-char here))
1974 (when (>= (point) here)
1975 (forward-paragraph -2)
1976 (if (> (re-search-forward here-filler nil t) here)
1977 (goto-char here)))
1978 (point))))
1979
1980 ;; Now seek successively earlier sentence ends between PAR-BEG and
1981 ;; HERE, until the "start of sentence" following it is earlier than
1982 ;; HERE, or we hit PAR-BEG. Beware of comment prefices!
1983 (while (and (re-search-backward (c-sentence-end) par-beg 'limit)
1984 (setq last (point))
1985 (goto-char (match-end 0)) ; tentative beginning of sentence
1986 (or (>= (point) here)
1987 (and (not (bolp)) ; Found a non-blank comment-prefix?
1988 (save-excursion
1989 (if (re-search-backward prefix-at-bol-here nil t)
1990 (/= (match-beginning 1) (match-end 1)))))
1991 (progn ; Skip the crud to find a real b-o-s.
1992 (if (c-in-comment-line-prefix-p)
1993 (beginning-of-line))
1994 (re-search-forward here-filler) ; always succeeds.
1995 (>= (point) here))))
1996 (goto-char last))
1997 (re-search-forward here-filler)))
1998
1999 (if (< (point) start-point)
2000 nil
2001 (goto-char (car range))
2002 t))))
2003
2004(defun c-end-of-sentence-in-comment (range)
2005 ;; Move forward to the "end of a sentence" within the comment defined by
2006 ;; RANGE, a cons of its starting and ending positions (enclosing the opening
2007 ;; comment delimiter and the terminating */ or newline). If we find an EOS,
2008 ;; return NIL. Otherwise, move point to just after the end of the comment
2009 ;; and return T.
2010 ;;
2011 ;; The EOS is just after the non-WS part of the next match of the regexp
2012 ;; sentence-end. Typically, this is just after one of [.!?]. If there is
2013 ;; no sentence-end match following point, any WS before the end of the
2014 ;; comment will count as EOS, providing we're not already in it.
2015 ;;
2016 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
2017 ;; It is not a general function, but is intended only for calling from
2018 ;; c-move-over-sentence.
2019 ;;
2020 ;; This function might do hidden buffer changes.
2021 (save-match-data
2022 (let ((start-point (point))
2023 ;; (lit-type (c-literal-type range)) ; Commented out, 2005/11/23, ACM
2024 )
2025 (save-restriction
2026 (c-narrow-to-comment-innards range) ; This might move point forwards.
2027 (let* ((here (point))
2028 (par-end ; EOL position of last text in current/next paragraph.
2029 (save-excursion
2030 ;; The cc-mode values of paragraph-\(start\|separate\), set
2031 ;; in c-setup-paragraph-variables, are used in the
2032 ;; following.
2033 (forward-paragraph 1)
2034 (if (eq (preceding-char) ?\n) (forward-char -1))
2035 (when (<= (point) here) ; can happen, e.g., when HERE is at EOL.
2036 (goto-char here)
2037 (forward-paragraph 2)
2038 (if (eq (preceding-char) ?\n) (forward-char -1)))
2039 (point)))
2040
2041 last
2042 (prefix-at-bol-here
2043 (concat "^[ \t]*\\(" c-current-comment-prefix "\\)\\=")))
2044 ;; Go forward one "comment-prefix which looks like sentence-end"
2045 ;; each time round the following:
2046 (while (and (re-search-forward (c-sentence-end) par-end 'limit)
2047 (progn
2048 (setq last (point))
2049 (skip-chars-backward " \t\n")
2050 (or (and (not (bolp))
2051 (re-search-backward prefix-at-bol-here nil t)
2052 (/= (match-beginning 1) (match-end 1)))
2053 (<= (point) here))))
2054 (goto-char last))
2055
2056 ;; Take special action if we're up against the end of a comment (of
2057 ;; either sort): Leave point just after the last non-ws text.
2058 (if (eq (point) (point-max))
2059 (while (or (/= (skip-chars-backward " \t\n") 0)
2060 (and (re-search-backward prefix-at-bol-here nil t)
2061 (/= (match-beginning 1) (match-end 1))))))))
2062
2063 (if (> (point) start-point)
2064 nil
2065 (goto-char (cdr range))
2066 t))))
2067
2068(defun c-beginning-of-sentence-in-string (range)
2069 ;; Move backwards to the "beginning of a sentence" within the string defined
2070 ;; by RANGE, a cons of its starting and ending positions (enclosing the
2071 ;; string quotes). If we find a BOS, return NIL. Otherwise, move point to
2072 ;; just before the start of the string and return T.
2073 ;;
2074 ;; The BOS is either the text which follows a regexp match of sentence-end
2075 ;; or text which is a beginning of "paragraph". For the purposes of
2076 ;; determining paragraph boundaries, escaped newlines are treated as
2077 ;; ordinary newlines.
2078 ;;
2079 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
2080 ;; It is not a general function, but is intended only for calling from
2081 ;; c-move-over-sentence.
2082 ;;
2083 ;; This function might do hidden buffer changes.
2084 (save-match-data
2085 (let* ((here (point)) last
2086 (end (1- (cdr range)))
2087 (here-filler ; matches WS and escaped newlines at point.
2088 "\\=\\([ \t\n\r\f]\\|\\\\[\n\r]\\)*")
2089 ;; Enhance paragraph-start and paragraph-separate also to recognise
2090 ;; blank lines terminated by escaped EOLs. IT MAY WELL BE that
2091 ;; these values should be customizable user options, or something.
2092 (paragraph-start c-string-par-start)
2093 (paragraph-separate c-string-par-separate)
2094
2095 (par-beg ; beginning of current (or previous) paragraph.
2096 (save-excursion
2097 (save-restriction
2098 (narrow-to-region (1+ (car range)) end)
2099 (forward-paragraph -1) ; uses above values of
2100 ; paragraph-\(start\|separate\)
2101 (if (> (re-search-forward here-filler nil t) here)
2102 (goto-char here))
2103 (when (>= (point) here)
2104 (forward-paragraph -2)
2105 (if (> (re-search-forward here-filler nil t) here)
2106 (goto-char here)))
2107 (point)))))
2108 ;; Now see if we can find a sentence end after PAR-BEG.
2109 (while (and (re-search-backward c-sentence-end-with-esc-eol par-beg 'limit)
2110 (setq last (point))
2111 (goto-char (match-end 0))
2112 (or (> (point) end)
2113 (progn
2114 (re-search-forward
2115 here-filler end t) ; always succeeds. Use end rather
2116 ; than here, in case point starts
2117 ; beyond the closing quote.
2118 (>= (point) here))))
2119 (goto-char last))
2120 (re-search-forward here-filler here t)
2121 (if (< (point) here)
2122 nil
2123 (goto-char (car range))
2124 t))))
2125
2126(defun c-end-of-sentence-in-string (range)
2127 ;; Move forward to the "end of a sentence" within the string defined by
2128 ;; RANGE, a cons of its starting and ending positions. If we find an EOS,
2129 ;; return NIL. Otherwise, move point to just after the end of the string
2130 ;; and return T.
2131 ;;
2132 ;; The EOS is just after the non-WS part of the next match of the regexp
2133 ;; sentence-end. Typically, this is just after one of [.!?]. If there is
2134 ;; no sentence-end match following point, any WS before the end of the
2135 ;; string will count as EOS, providing we're not already in it.
2136 ;;
2137 ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
2138 ;; It is not a general function, but is intended only for calling from
2139 ;; c-move-over-sentence.
2140 ;;
2141 ;; This function might do hidden buffer changes.
2142 (save-match-data
2143 (let* ((here (point))
2144 last
2145 ;; Enhance paragraph-start and paragraph-separate to recognise
2146 ;; blank lines terminated by escaped EOLs.
2147 (paragraph-start c-string-par-start)
2148 (paragraph-separate c-string-par-separate)
2149
2150 (par-end ; EOL position of last text in current/next paragraph.
2151 (save-excursion
2152 (save-restriction
2153 (narrow-to-region (car range) (1- (cdr range)))
2154 ;; The above values of paragraph-\(start\|separate\) are used
2155 ;; in the following.
2156 (forward-paragraph 1)
2157 (setq last (point))
2158 ;; (re-search-backward filler-here nil t) would find an empty
2159 ;; string. Therefore we simulate it by the following:
2160 (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
2161 (re-search-backward "\\\\\\($\\)\\=" nil t)))
2162 (unless (> (point) here)
2163 (goto-char last)
2164 (forward-paragraph 1)
2165 (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
2166 (re-search-backward "\\\\\\($\\)\\=" nil t))))
2167 (point)))))
2168 ;; Try to go forward a sentence.
2169 (when (re-search-forward c-sentence-end-with-esc-eol par-end 'limit)
2170 (setq last (point))
2171 (while (or (/= (skip-chars-backward " \t\n") 0)
2172 (re-search-backward "\\\\\\($\\)\\=" nil t))))
2173 ;; Did we move a sentence, or did we hit the end of the string?
2174 (if (> (point) here)
2175 nil
2176 (goto-char (cdr range))
2177 t))))
2178
2179(defun c-ascertain-preceding-literal ()
2180 ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
2181 ;; If a literal is the next thing (aside from whitespace) to be found before
2182 ;; point, return a cons of its start.end positions (enclosing the
2183 ;; delimiters). Otherwise return NIL.
2184 ;;
2185 ;; This function might do hidden buffer changes.
2186 (save-excursion
2187 (c-collect-line-comments
2188 (let ((here (point))
2189 pos)
2190 (if (c-backward-single-comment)
2191 (cons (point) (progn (c-forward-single-comment) (point)))
2192 (save-restriction
2193 ;; to prevent `looking-at' seeing a " at point.
2194 (narrow-to-region (point-min) here)
2195 (when
2196 (or
2197 ;; An EOL can act as an "open string" terminator in AWK.
2198 (looking-at c-ws*-string-limit-regexp)
2199 (and (not (bobp))
2200 (progn (backward-char)
2201 (looking-at c-string-limit-regexp))))
2202 (goto-char (match-end 0)) ; just after the string terminator.
2203 (setq pos (point))
2204 (c-safe (c-backward-sexp 1) ; move back over the string.
2205 (cons (point) pos)))))))))
2206
2207(defun c-ascertain-following-literal ()
2208 ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
2209 ;; If a literal is the next thing (aside from whitespace) following point,
2210 ;; return a cons of its start.end positions (enclosing the delimiters).
2211 ;; Otherwise return NIL.
2212 ;;
2213 ;; This function might do hidden buffer changes.
2214 (save-excursion
2215 (c-collect-line-comments
2216 (let (pos)
2217 (c-skip-ws-forward)
2218 (if (looking-at c-string-limit-regexp) ; string-delimiter.
2219 (cons (point) (or (c-safe (progn (c-forward-sexp 1) (point)))
2220 (point-max)))
2221 (setq pos (point))
2222 (if (c-forward-single-comment)
2223 (cons pos (point))))))))
2224
2225(defun c-after-statement-terminator-p () ; Should we pass in LIM here?
2226 ;; Does point immediately follow a statement "terminator"? A virtual
2227 ;; semicolon is regarded here as such. So is a an opening brace ;-)
2228 ;;
2229 ;; This function might do hidden buffer changes.
2230 (or (save-excursion
2231 (backward-char)
2232 (and (looking-at "[;{}]")
2233 (not (and c-special-brace-lists ; Pike special brace lists.
2234 (eq (char-after) ?{)
2235 (c-looking-at-special-brace-list)))))
2236 (c-at-vsemi-p)
2237 ;; The following (for macros) is not strict about exactly where we are
2238 ;; wrt white space at the end of the macro. Doesn't seem to matter too
2239 ;; much. ACM 2004/3/29.
2240 (let (eom)
2241 (save-excursion
2242 (if (c-beginning-of-macro)
2243 (setq eom (progn (c-end-of-macro)
2244 (point)))))
2245 (when eom
2246 (save-excursion
2247 (c-forward-comments)
2248 (>= (point) eom))))))
2249
2250(defun c-back-over-illiterals (macro-start)
2251 ;; Move backwards over code which isn't a literal (i.e. comment or string),
2252 ;; stopping before reaching BOB or a literal or the boundary of a
2253 ;; preprocessor statement or the "beginning of a statement". MACRO-START is
2254 ;; the position of the '#' beginning the current preprocessor directive, or
2255 ;; NIL if we're not in such.
2256 ;;
2257 ;; Return a cons (A.B), where
2258 ;; A is NIL if we moved back to a BOS (and know it), T otherwise (we
2259 ;; didn't move, or we hit a literal, or we're not sure about BOS).
2260 ;; B is MACRO-BOUNDARY if we are about to cross the boundary out of or
2261 ;; into a macro, otherwise LITERAL if we've hit a literal, otherwise NIL
2262 ;;
2263 ;; The total collection of returned values is as follows:
2264 ;; (nil . nil): Found a BOS whilst remaining inside the illiterals.
2265 ;; (t . literal): No BOS found: only a comment/string. We _might_ be at
2266 ;; a BOS - the caller must check this.
2267 ;; (nil . macro-boundary): only happens with non-nil macro-start. We've
2268 ;; moved and reached the opening # of the macro.
2269 ;; (t . macro-boundary): Every other circumstance in which we're at a
2270 ;; macro-boundary. We might be at a BOS.
2271 ;;
2272 ;; Point is left either at the beginning-of-statement, or at the last non-ws
2273 ;; code before encountering the literal/BOB or macro-boundary.
2274 ;;
2275 ;; Note that this function moves within either preprocessor commands
2276 ;; (macros) or normal code, but will not cross a boundary between the two,
2277 ;; or between two distinct preprocessor commands.
2278 ;;
2279 ;; Stop before `{' and after `;', `{', `}' and `};' when not followed by `}'
2280 ;; or `)', but on the other side of the syntactic ws. Move by sexps and
2281 ;; move into parens. Also stop before `#' when it's at boi on a line.
2282 ;;
2283 ;; This function might do hidden buffer changes.
2284 (save-match-data
2285 (let ((here (point))
2286 last) ; marks the position of non-ws code, what'll be BOS if, say, a
2287 ; semicolon precedes it.
2288 (catch 'done
2289 (while t ;; We go back one "token" each iteration of the loop.
2290 (setq last (point))
2291 (cond
2292 ;; Stop at the token after a comment.
2293 ((c-backward-single-comment) ; Also functions as backwards-ws.
2294 (goto-char last)
2295 (throw 'done '(t . literal)))
2296
2297 ;; If we've gone back over a LF, we might have moved into or out of
2298 ;; a preprocessor line.
2299 ((and (save-excursion
2300 (beginning-of-line)
2301 (re-search-forward "\\(^\\|[^\\]\\)[\n\r]" last t))
2302 (if macro-start
2303 (< (point) macro-start)
2304 (c-beginning-of-macro)))
2305 (goto-char last)
2306 ;; Return a car of NIL ONLY if we've hit the opening # of a macro.
2307 (throw 'done (cons (or (eq (point) here)
2308 (not macro-start))
2309 'macro-boundary)))
2310
2311 ;; Have we found a virtual semicolon? If so, stop, unless the next
2312 ;; statement is where we started from.
2313 ((and (c-at-vsemi-p)
2314 (< last here)
2315 (not (memq (char-after last) '(?\) ?})))) ; we've moved back from ) or }
2316 (goto-char last)
2317 (throw 'done '(nil . nil)))
2318
2319 ;; Hit the beginning of the buffer/region?
2320 ((bobp)
2321 (if (/= here last)
2322 (goto-char last))
2323 (throw 'done '(nil . nil)))
2324
2325 ;; Move back a character.
2326 ((progn (backward-char) nil))
2327
2328 ;; Stop at "{" (unless it's a PIKE special brace list.)
2329 ((eq (char-after) ?\{)
2330 (if (and c-special-brace-lists
2331 (c-looking-at-special-brace-list))
2332 (skip-syntax-backward "w_") ; Speedup only.
2333 (if (/= here last)
2334 (goto-char last))
2335 (throw 'done '(nil . nil))))
2336
2337 ;; Have we reached the start of a macro? This always counts as
2338 ;; BOS. (N.B. I don't think (eq (point) here) can ever be true
2339 ;; here. FIXME!!! ACM 2004/3/29)
2340 ((and macro-start (eq (point) macro-start))
2341 (throw 'done (cons (eq (point) here) 'macro-boundary)))
2342
2343 ;; Stop at token just after "}" or ";".
2344 ((looking-at "[;}]")
2345 ;; If we've gone back over ;, {, or }, we're done.
2346 (if (or (= here last)
2347 (memq (char-after last) '(?\) ?}))) ; we've moved back from ) or }
2348 (if (and (eq (char-before) ?}) ; If };, treat them as a unit.
2349 (eq (char-after) ?\;))
2350 (backward-char))
2351 (goto-char last) ; To the statement starting after the ; or }.
2352 (throw 'done '(nil . nil))))
2353
2354 ;; Stop at the token after a string.
2355 ((looking-at c-string-limit-regexp) ; Just gone back over a string terminator?
2356 (goto-char last)
2357 (throw 'done '(t . literal)))
17264191 2358
0386b551
AM
2359 ;; Nothing special: go back word characters.
2360 (t (skip-syntax-backward "w_")) ; Speedup only.
2361 ))))))
2362
2363(defun c-forward-over-illiterals (macro-end allow-early-stop)
2364 ;; Move forwards over code, stopping before reaching EOB or a literal
2365 ;; (i.e. a comment/string) or the boundary of a preprocessor statement or
2366 ;; the "end of a statement". MACRO-END is the position of the EOL/EOB which
2367 ;; terminates the current preprocessor directive, or NIL if we're not in
2368 ;; such.
2369 ;;
2370 ;; ALLOW-EARLY-STOP is non-nil if it is permissible to return without moving
2371 ;; forward at all, should we encounter a `{'. This is an ugly kludge, but
2372 ;; seems unavoidable. Depending on the context this function is called
2373 ;; from, we _sometimes_ need to stop there. Currently (2004/4/3),
2374 ;; ALLOW-EARLY-STOP is applied only to open braces, not to virtual
2375 ;; semicolons, or anything else.
2376 ;;
2377 ;; Return a cons (A.B), where
2378 ;; A is NIL if we moved forward to an EOS, or stay at one (when
2379 ;; ALLOW-EARLY-STOP is set), T otherwise (we hit a literal).
2380 ;; B is 'MACRO-BOUNDARY if we are about to cross the boundary out of or
2381 ;; into a macro, otherwise 'LITERAL if we've hit a literal, otherwise NIL
2382 ;;
2383 ;; Point is left either after the end-of-statement, or at the last non-ws
2384 ;; code before encountering the literal, or the # of the preprocessor
2385 ;; statement, or at EOB [or just after last non-WS stuff??].
2386 ;;
2387 ;; As a clarification of "after the end-of-statement", if a comment or
2388 ;; whitespace follows a completed AWK statement, that statement is treated
2389 ;; as ending just after the last non-ws character before the comment.
17264191 2390 ;;
0386b551
AM
2391 ;; Note that this function moves within either preprocessor commands
2392 ;; (macros) or normal code, but not both within the same invocation.
2393 ;;
2394 ;; Stop before `{', `}', and `#' when it's at boi on a line, but on the
2395 ;; other side of the syntactic ws, and after `;', `}' and `};'. Only
2396 ;; stop before `{' if at top level or inside braces, though. Move by
2397 ;; sexps and move into parens. Also stop at eol of lines with `#' at
2398 ;; the boi.
2399 ;;
2400 ;; This function might do hidden buffer changes.
2401 (let ((here (point))
2402 last)
2403 (catch 'done
2404 (while t ;; We go one "token" forward each time round this loop.
2405 (setq last (point))
2406
2407 ;; If we've moved forward to a virtual semicolon, we're done.
2408 (if (and (> last here) ; Should we check ALLOW-EARLY-STOP, here? 2004/4/3
2409 (c-at-vsemi-p))
2410 (throw 'done '(nil . nil)))
2411
2412 (c-skip-ws-forward)
2413 (cond
2414 ;; Gone past the end of a macro?
2415 ((and macro-end (> (point) macro-end))
2416 (goto-char last)
2417 (throw 'done (cons (eq (point) here) 'macro-boundary)))
2418
2419 ;; About to hit a comment?
2420 ((save-excursion (c-forward-single-comment))
2421 (goto-char last)
2422 (throw 'done '(t . literal)))
2423
2424 ;; End of buffer?
2425 ((eobp)
2426 (if (/= here last)
2427 (goto-char last))
2428 (throw 'done '(nil . nil)))
2429
2430 ;; If we encounter a '{', stop just after the previous token.
2431 ((and (eq (char-after) ?{)
2432 (not (and c-special-brace-lists
2433 (c-looking-at-special-brace-list)))
2434 (or allow-early-stop (/= here last))
2435 (save-excursion ; Is this a check that we're NOT at top level?
2436;;;; NO! This seems to check that (i) EITHER we're at the top level; OR (ii) The next enclosing
2437;;;; level of bracketing is a '{'. HMM. Doesn't seem to make sense.
2438;;;; 2003/8/8 This might have something to do with the GCC extension "Statement Expressions", e.g.
2439;;;; while ({stmt1 ; stmt2 ; exp ;}). This form excludes such Statement Expressions.
2440 (or (not (c-safe (up-list -1) t))
2441 (= (char-after) ?{))))
2442 (goto-char last)
2443 (throw 'done '(nil . nil)))
2444
2445 ;; End of a PIKE special brace list? If so, step over it and continue.
2446 ((and c-special-brace-lists
2447 (eq (char-after) ?})
2448 (save-excursion
2449 (and (c-safe (up-list -1) t)
2450 (c-looking-at-special-brace-list))))
2451 (forward-char)
2452 (skip-syntax-forward "w_")) ; Speedup only.
2453
2454 ;; Have we got a '}' after having moved? If so, stop after the
2455 ;; previous token.
2456 ((and (eq (char-after) ?})
2457 (/= here last))
2458 (goto-char last)
2459 (throw 'done '(nil . nil)))
2460
1df1040b
CY
2461 ;; Stop if we encounter a preprocessor line. Continue if we
2462 ;; hit a naked #
2463 ((and c-opt-cpp-prefix
2464 (not macro-end)
0386b551
AM
2465 (eq (char-after) ?#)
2466 (= (point) (c-point 'boi)))
1df1040b
CY
2467 (if (= (point) here) ; Not a macro, therefore naked #.
2468 (forward-char)
2469 (throw 'done '(t . macro-boundary))))
0386b551
AM
2470
2471 ;; Stop after a ';', '}', or "};"
2472 ((looking-at ";\\|};?")
2473 (goto-char (match-end 0))
2474 (throw 'done '(nil . nil)))
2475
2476 ;; Found a string (this subsumes AWK regexps)?
2477 ((looking-at c-string-limit-regexp)
2478 (goto-char last)
2479 (throw 'done '(t . literal)))
2480
2481 (t
2482 (forward-char) ; Can't fail - we checked (eobp) earlier on.
2483 (skip-syntax-forward "w_") ; Speedup only.
2484 (when (and macro-end (> (point) macro-end))
2485 (goto-char last)
2486 (throw 'done (cons (eq (point) here) 'macro-boundary))))
2487 )))))
2488
2489(defun c-one-line-string-p (range)
2490 ;; Is the literal defined by RANGE a string contained in a single line?
2491 ;;
2492 ;; This function might do hidden buffer changes.
2493 (save-excursion
2494 (goto-char (car range))
2495 (and (looking-at c-string-limit-regexp)
2496 (progn (skip-chars-forward "^\n" (cdr range))
2497 (eq (point) (cdr range))))))
2498
785eecbb
RS
2499(defun c-beginning-of-statement (&optional count lim sentence-flag)
2500 "Go to the beginning of the innermost C statement.
2501With prefix arg, go back N - 1 statements. If already at the
28c236de
RS
2502beginning of a statement then go to the beginning of the closest
2503preceding one, moving into nested blocks if necessary (use
130c507e
GM
2504\\[backward-sexp] to skip over a block). If within or next to a
2505comment or multiline string, move by sentences instead of statements.
785eecbb
RS
2506
2507When called from a program, this function takes 3 optional args: the
2508repetition count, a buffer position limit which is the farthest back
130c507e 2509to search for the syntactic context, and a flag saying whether to do
a66cd3ee
MS
2510sentence motion in or near comments and multiline strings.
2511
0386b551
AM
2512Note that for use in programs, `c-beginning-of-statement-1' is
2513usually better. It has much better defined semantics than this one,
2514which is intended for interactive use, and might therefore change to
2515be more \"DWIM:ey\"."
785eecbb
RS
2516 (interactive (list (prefix-numeric-value current-prefix-arg)
2517 nil t))
0386b551
AM
2518 (if (< count 0)
2519 (c-end-of-statement (- count) lim sentence-flag)
2520 (c-save-buffer-state
2521 ((count (or count 1))
2522 last ; start point for going back ONE chunk. Updated each chunk movement.
2523 (macro-fence
2524 (save-excursion (and (not (bobp)) (c-beginning-of-macro) (point))))
2525 res ; result from sub-function call
2526 not-bos ; "not beginning-of-statement"
2527 (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
2528
2529 ;; Go back one statement at each iteration of the following loop.
2530 (while (and (/= count 0)
2531 (or (not lim) (> (point) lim)))
2532 ;; Go back one "chunk" each time round the following loop, stopping
2533 ;; when we reach a statement boundary, etc.
2534 (setq last (point))
2535 (while
2536 (cond ; Each arm of this cond returns NIL on reaching a desired
2537 ; statement boundary, non-NIL otherwise.
2538 ((bobp)
2539 (setq count 0)
2540 nil)
2541
2542 (range ; point is within or approaching a literal.
2543 (cond
2544 ;; Single line string or sentence-flag is null => skip the
2545 ;; entire literal.
2546 ((or (null sentence-flag)
2547 (c-one-line-string-p range))
2548 (goto-char (car range))
2549 (setq range (c-ascertain-preceding-literal))
2550 ;; N.B. The following is essentially testing for an AWK regexp
2551 ;; at BOS:
2552 ;; Was the previous non-ws thing an end of statement?
2553 (save-excursion
2554 (if macro-fence
2555 (c-backward-comments)
2556 (c-backward-syntactic-ws))
2557 (not (or (bobp) (c-after-statement-terminator-p)))))
2558
2559 ;; Comment inside a statement or a multi-line string.
2560 (t (when (setq res ; returns non-nil when we go out of the literal
2561 (if (eq (c-literal-type range) 'string)
2562 (c-beginning-of-sentence-in-string range)
2563 (c-beginning-of-sentence-in-comment range)))
2564 (setq range (c-ascertain-preceding-literal)))
2565 res)))
2566
2567 ;; Non-literal code.
2568 (t (setq res (c-back-over-illiterals macro-fence))
2569 (setq not-bos ; "not reached beginning-of-statement".
2570 (or (= (point) last)
2571 (memq (char-after) '(?\) ?\}))
2572 (and
2573 (car res)
2574 ;; We're at a tentative BOS. The next form goes
2575 ;; back over WS looking for an end of previous
2576 ;; statement.
2577 (not (save-excursion
2578 (if macro-fence
2579 (c-backward-comments)
2580 (c-backward-syntactic-ws))
2581 (or (bobp) (c-after-statement-terminator-p)))))))
2582 ;; Are we about to move backwards into or out of a
3b95603f 2583 ;; preprocessor command? If so, locate its beginning.
0386b551 2584 (when (eq (cdr res) 'macro-boundary)
c6678f29
CY
2585 (save-excursion
2586 (beginning-of-line)
2587 (setq macro-fence
2588 (and (not (bobp))
2589 (progn (c-skip-ws-backward) (c-beginning-of-macro))
2590 (point)))))
0386b551
AM
2591 ;; Are we about to move backwards into a literal?
2592 (when (memq (cdr res) '(macro-boundary literal))
2593 (setq range (c-ascertain-preceding-literal)))
2594 not-bos))
2595 (setq last (point)))
2596
2597 (if (/= count 0) (setq count (1- count))))
2598 (c-keep-region-active))))
785eecbb
RS
2599
2600(defun c-end-of-statement (&optional count lim sentence-flag)
2601 "Go to the end of the innermost C statement.
28c236de
RS
2602With prefix arg, go forward N - 1 statements. Move forward to the end
2603of the next statement if already at end, and move into nested blocks
130c507e
GM
2604\(use \\[forward-sexp] to skip over a block). If within or next to a
2605comment or multiline string, move by sentences instead of statements.
785eecbb
RS
2606
2607When called from a program, this function takes 3 optional args: the
2608repetition count, a buffer position limit which is the farthest back
130c507e
GM
2609to search for the syntactic context, and a flag saying whether to do
2610sentence motion in or near comments and multiline strings."
785eecbb
RS
2611 (interactive (list (prefix-numeric-value current-prefix-arg)
2612 nil t))
0386b551
AM
2613 (setq count (or count 1))
2614 (if (< count 0) (c-beginning-of-statement (- count) lim sentence-flag)
2615
2616 (c-save-buffer-state
2617 (here ; start point for going forward ONE statement. Updated each statement.
2618 (macro-fence
2619 (save-excursion
2620 (and (not (eobp)) (c-beginning-of-macro)
2621 (progn (c-end-of-macro) (point)))))
2622 res
2623 (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
2624
2625 ;; Go back/forward one statement at each iteration of the following loop.
2626 (while (and (/= count 0)
2627 (or (not lim) (< (point) lim)))
2628 (setq here (point)) ; ONLY HERE is HERE updated
2629
2630 ;; Go forward one "chunk" each time round the following loop, stopping
2631 ;; when we reach a statement boundary, etc.
2632 (while
2633 (cond ; Each arm of this cond returns NIL on reaching a desired
2634 ; statement boundary, non-NIL otherwise.
2635 ((eobp)
2636 (setq count 0)
2637 nil)
2638
2639 (range ; point is within a literal.
2640 (cond
2641 ;; sentence-flag is null => skip the entire literal.
2642 ;; or a Single line string.
2643 ((or (null sentence-flag)
2644 (c-one-line-string-p range))
2645 (goto-char (cdr range))
2646 (setq range (c-ascertain-following-literal))
2647 ;; Is there a virtual semicolon here (e.g. for AWK)?
2648 (not (c-at-vsemi-p)))
2649
2650 ;; Comment or multi-line string.
2651 (t (when (setq res ; gets non-nil when we go out of the literal
2652 (if (eq (c-literal-type range) 'string)
2653 (c-end-of-sentence-in-string range)
2654 (c-end-of-sentence-in-comment range)))
2655 (setq range (c-ascertain-following-literal)))
2656 ;; If we've just come forward out of a literal, check for
2657 ;; vsemi. (N.B. AWK can't have a vsemi after a comment, but
2658 ;; some other language may do in the future)
2659 (and res
2660 (not (c-at-vsemi-p))))))
2661
2662 ;; Non-literal code.
2663 (t (setq res (c-forward-over-illiterals macro-fence
2664 (> (point) here)))
2665 ;; Are we about to move forward into or out of a
2666 ;; preprocessor command?
2667 (when (eq (cdr res) 'macro-boundary)
1df1040b
CY
2668 (setq macro-fence
2669 (save-excursion
2670 (if macro-fence
2671 (progn
2672 (end-of-line)
2673 (and (not (eobp))
2674 (progn (c-skip-ws-forward)
2675 (c-beginning-of-macro))
2676 (progn (c-end-of-macro)
2677 (point))))
2678 (and (not (eobp))
2679 (c-beginning-of-macro)
2680 (progn (c-end-of-macro) (point)))))))
0386b551
AM
2681 ;; Are we about to move forward into a literal?
2682 (when (memq (cdr res) '(macro-boundary literal))
2683 (setq range (c-ascertain-following-literal)))
2684 (car res))))
2685
2686 (if (/= count 0) (setq count (1- count))))
2687 (c-keep-region-active))))
17264191 2688
785eecbb
RS
2689\f
2690;; set up electric character functions to work with pending-del,
2691;; (a.k.a. delsel) mode. All symbols get the t value except
c93a62d8 2692;; the functions which delete, which gets 'supersede.
6b7513e3 2693(mapc
785eecbb
RS
2694 (function
2695 (lambda (sym)
2696 (put sym 'delete-selection t) ; for delsel (Emacs)
2697 (put sym 'pending-delete t))) ; for pending-del (XEmacs)
2698 '(c-electric-pound
2699 c-electric-brace
2700 c-electric-slash
2701 c-electric-star
2702 c-electric-semi&comma
2703 c-electric-lt-gt
0ec8351b
BW
2704 c-electric-colon
2705 c-electric-paren))
c93a62d8
RS
2706(put 'c-electric-delete 'delete-selection 'supersede) ; delsel
2707(put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
2708(put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
2709(put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
7443aaa6
SM
2710(put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel
2711(put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del
785eecbb
RS
2712
2713\f
51c9af45 2714;; Inserting/indenting comments
a66cd3ee 2715(defun c-calc-comment-indent (entry)
0386b551 2716 ;; This function might do hidden buffer changes.
a66cd3ee
MS
2717 (if (symbolp entry)
2718 (setq entry (or (assq entry c-indent-comment-alist)
2719 (assq 'other c-indent-comment-alist)
2720 '(default . (column . nil)))))
2721 (let ((action (car (cdr entry)))
2722 (value (cdr (cdr entry)))
2723 (col (current-column)))
2724 (cond ((eq action 'space)
2725 (+ col value))
2726 ((eq action 'column)
2727 (unless value (setq value comment-column))
2728 (if (bolp)
2729 ;; Do not pad with one space if we're at bol.
2730 value
2731 (max (1+ col) value)))
2732 ((eq action 'align)
2733 (or (save-excursion
2734 (beginning-of-line)
2735 (unless (bobp)
2736 (backward-char)
2737 (let ((lim (c-literal-limits (c-point 'bol) t)))
2738 (when (consp lim)
2739 (goto-char (car lim))
0386b551 2740 (when (looking-at "/[/*]") ; FIXME!!! Adapt for AWK! (ACM, 2005/11/18)
a66cd3ee
MS
2741 ;; Found comment to align with.
2742 (if (bolp)
2743 ;; Do not pad with one space if we're at bol.
2744 0
2745 (max (1+ col) (current-column))))))))
2746 ;; Recurse to handle value as a new spec.
2747 (c-calc-comment-indent (cdr entry)))))))
2748
785eecbb 2749(defun c-comment-indent ()
a66cd3ee
MS
2750 "Used by `indent-for-comment' to create and indent comments.
2751See `c-indent-comment-alist' for a description."
2752 (save-excursion
2753 (end-of-line)
d9e94c22
MS
2754 (c-save-buffer-state
2755 ((eot (let ((lim (c-literal-limits (c-point 'bol) t)))
a66cd3ee
MS
2756 (or (when (consp lim)
2757 (goto-char (car lim))
2758 (when (looking-at "/[/*]")
2759 (skip-chars-backward " \t")
2760 (point)))
2761 (progn
2762 (skip-chars-backward " \t")
2763 (point)))))
2764 (line-type
2765 (cond ((looking-at "^/[/*]")
2766 'anchored-comment)
2767 ((progn (beginning-of-line)
2768 (eq (point) eot))
2769 'empty-line)
2770 ((progn (back-to-indentation)
2771 (and (eq (char-after) ?})
2772 (eq (point) (1- eot))))
2773 'end-block)
2774 ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
2775 (eq (match-end 0) eot))
2776 'cpp-end-block)
2777 (t
2778 'other))))
2779 (if (and (memq line-type '(anchored-comment empty-line))
2780 c-indent-comments-syntactically-p)
d9e94c22 2781 (let ((c-syntactic-context (c-guess-basic-syntax)))
785eecbb
RS
2782 ;; BOGOSITY ALERT: if we're looking at the eol, its
2783 ;; because indent-for-comment hasn't put the comment-start
2784 ;; in the buffer yet. this will screw up the syntactic
2785 ;; analysis so we kludge in the necessary info. Another
2786 ;; kludge is that if we're at the bol, then we really want
2787 ;; to ignore any anchoring as specified by
2788 ;; c-comment-only-line-offset since it doesn't apply here.
a66cd3ee 2789 (if (eolp)
785eecbb
RS
2790 (c-add-syntax 'comment-intro))
2791 (let ((c-comment-only-line-offset
2792 (if (consp c-comment-only-line-offset)
2793 c-comment-only-line-offset
2794 (cons c-comment-only-line-offset
2795 c-comment-only-line-offset))))
d9e94c22 2796 (c-get-syntactic-indentation c-syntactic-context)))
a66cd3ee
MS
2797 (goto-char eot)
2798 (c-calc-comment-indent line-type)))))
785eecbb 2799
fd3b1ef6 2800\f
785eecbb
RS
2801;; used by outline-minor-mode
2802(defun c-outline-level ()
a66cd3ee
MS
2803 (let (buffer-invisibility-spec);; This so that `current-column' DTRT
2804 ;; in otherwise-hidden text.
de28797f
SM
2805 (save-excursion
2806 (skip-chars-forward "\t ")
2807 (current-column))))
785eecbb
RS
2808
2809\f
51c9af45 2810;; Movement by CPP conditionals.
785eecbb
RS
2811(defun c-up-conditional (count)
2812 "Move back to the containing preprocessor conditional, leaving mark behind.
2813A prefix argument acts as a repeat count. With a negative argument,
2814move forward to the end of the containing preprocessor conditional.
51f606de 2815
0386b551
AM
2816\"#elif\" is treated like \"#else\" followed by \"#if\", so the
2817function stops at them when going backward, but not when going
2818forward."
51f606de 2819 (interactive "p")
0a218d34
AM
2820 (let ((new-point (c-scan-conditionals (- count) -1)))
2821 (push-mark)
2822 (goto-char new-point))
51f606de 2823 (c-keep-region-active))
17264191 2824
51f606de 2825(defun c-up-conditional-with-else (count)
0386b551
AM
2826 "Move back to the containing preprocessor conditional, including \"#else\".
2827Just like `c-up-conditional', except it also stops at \"#else\"
51f606de
GM
2828directives."
2829 (interactive "p")
0a218d34
AM
2830 (let ((new-point (c-scan-conditionals (- count) -1 t)))
2831 (push-mark)
2832 (goto-char new-point))
51f606de
GM
2833 (c-keep-region-active))
2834
2835(defun c-down-conditional (count)
2836 "Move forward into the next preprocessor conditional, leaving mark behind.
2837A prefix argument acts as a repeat count. With a negative argument,
2838move backward into the previous preprocessor conditional.
2839
0386b551
AM
2840\"#elif\" is treated like \"#else\" followed by \"#if\", so the
2841function stops at them when going forward, but not when going
2842backward."
785eecbb 2843 (interactive "p")
0a218d34
AM
2844 (let ((new-point (c-scan-conditionals count 1)))
2845 (push-mark)
2846 (goto-char new-point))
785eecbb
RS
2847 (c-keep-region-active))
2848
51f606de 2849(defun c-down-conditional-with-else (count)
0386b551
AM
2850 "Move forward into the next preprocessor conditional, including \"#else\".
2851Just like `c-down-conditional', except it also stops at \"#else\"
51f606de
GM
2852directives."
2853 (interactive "p")
0a218d34
AM
2854 (let ((new-point (c-scan-conditionals count 1 t)))
2855 (push-mark)
2856 (goto-char new-point))
51f606de
GM
2857 (c-keep-region-active))
2858
2859(defun c-backward-conditional (count &optional target-depth with-else)
785eecbb
RS
2860 "Move back across a preprocessor conditional, leaving mark behind.
2861A prefix argument acts as a repeat count. With a negative argument,
0a218d34
AM
2862move forward across a preprocessor conditional.
2863
2864The optional arguments TARGET-DEPTH and WITH-ELSE are historical,
2865and have the same meanings as in `c-scan-conditionals'. If you
2866are calling c-forward-conditional from a program, you might want
2867to call `c-scan-conditionals' directly instead."
785eecbb 2868 (interactive "p")
0a218d34
AM
2869 (let ((new-point (c-scan-conditionals (- count) target-depth with-else)))
2870 (push-mark)
2871 (goto-char new-point))
785eecbb
RS
2872 (c-keep-region-active))
2873
51f606de 2874(defun c-forward-conditional (count &optional target-depth with-else)
785eecbb
RS
2875 "Move forward across a preprocessor conditional, leaving mark behind.
2876A prefix argument acts as a repeat count. With a negative argument,
51f606de
GM
2877move backward across a preprocessor conditional.
2878
0a218d34
AM
2879If there aren't enough conditionals after \(or before) point, an
2880error is signalled.
2881
2882\"#elif\" is treated like \"#else\" followed by \"#if\", except that
2883the nesting level isn't changed when tracking subconditionals.
2884
2885The optional arguments TARGET-DEPTH and WITH-ELSE are historical,
2886and have the same meanings as in `c-scan-conditionals'. If you
2887are calling c-forward-conditional from a program, you might want
2888to call `c-scan-conditionals' directly instead."
2889 (interactive "p")
2890 (let ((new-point (c-scan-conditionals count target-depth with-else)))
2891 (push-mark)
2892 (goto-char new-point)))
2893
2894(defun c-scan-conditionals (count &optional target-depth with-else)
2895 "Scan forward across COUNT preprocessor conditionals.
2896With a negative argument, scan backward across preprocessor
2897conditionals. Return the end position. Point is not moved.
2898
2899If there aren't enough preprocessor conditionals, throw an error.
2900
0386b551
AM
2901\"#elif\" is treated like \"#else\" followed by \"#if\", except that
2902the nesting level isn't changed when tracking subconditionals.
51f606de
GM
2903
2904The optional argument TARGET-DEPTH specifies the wanted nesting depth
0a218d34
AM
2905after each scan. E.g. if TARGET-DEPTH is -1, the end position will be
2906outside the enclosing conditional. A non-integer non-nil TARGET-DEPTH
51f606de
GM
2907counts as -1.
2908
0386b551
AM
2909If the optional argument WITH-ELSE is non-nil, \"#else\" directives
2910are treated as conditional clause limits. Normally they are ignored."
785eecbb
RS
2911 (let* ((forward (> count 0))
2912 (increment (if forward -1 1))
2913 (search-function (if forward 're-search-forward 're-search-backward))
0a218d34 2914 new)
51f606de
GM
2915 (unless (integerp target-depth)
2916 (setq target-depth (if target-depth -1 0)))
785eecbb
RS
2917 (save-excursion
2918 (while (/= count 0)
51f606de
GM
2919 (let ((depth 0)
2920 ;; subdepth is the depth in "uninteresting" subtrees,
2921 ;; i.e. those that takes us farther from the target
2922 ;; depth instead of closer.
2923 (subdepth 0)
2924 found)
785eecbb
RS
2925 (save-excursion
2926 ;; Find the "next" significant line in the proper direction.
2927 (while (and (not found)
2928 ;; Rather than searching for a # sign that
2929 ;; comes at the beginning of a line aside from
2930 ;; whitespace, search first for a string
2931 ;; starting with # sign. Then verify what
2932 ;; precedes it. This is faster on account of
2933 ;; the fastmap feature of the regexp matcher.
2934 (funcall search-function
51f606de 2935 "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
785eecbb
RS
2936 nil t))
2937 (beginning-of-line)
2938 ;; Now verify it is really a preproc line.
51f606de
GM
2939 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
2940 (let (dchange (directive (match-string 1)))
2941 (cond ((string= directive "if")
2942 (setq dchange (- increment)))
2943 ((string= directive "endif")
2944 (setq dchange increment))
2945 ((= subdepth 0)
2946 ;; When we're not in an "uninteresting"
2947 ;; subtree, we might want to act on "elif"
2948 ;; and "else" too.
2949 (if (cond (with-else
2950 ;; Always move toward the target depth.
2951 (setq dchange
2952 (if (> target-depth 0) 1 -1)))
2953 ((string= directive "elif")
2954 (setq dchange (- increment))))
2955 ;; Ignore the change if it'd take us
2956 ;; into an "uninteresting" subtree.
2957 (if (eq (> dchange 0) (<= target-depth 0))
2958 (setq dchange nil)))))
2959 (when dchange
2960 (when (or (/= subdepth 0)
2961 (eq (> dchange 0) (<= target-depth 0)))
2962 (setq subdepth (+ subdepth dchange)))
2963 (setq depth (+ depth dchange))
2964 ;; If we are trying to move across, and we find an
2965 ;; end before we find a beginning, get an error.
2966 (if (and (< depth target-depth) (< dchange 0))
2967 (error (if forward
2968 "No following conditional at this level"
2969 "No previous conditional at this level"))))
785eecbb
RS
2970 ;; When searching forward, start from next line so
2971 ;; that we don't find the same line again.
2972 (if forward (forward-line 1))
51f606de
GM
2973 ;; We found something if we've arrived at the
2974 ;; target depth.
2975 (if (and dchange (= depth target-depth))
785eecbb
RS
2976 (setq found (point))))
2977 ;; else
51f606de 2978 (if forward (forward-line 1)))))
785eecbb
RS
2979 (or found
2980 (error "No containing preprocessor conditional"))
2981 (goto-char (setq new found)))
2982 (setq count (+ count increment))))
0a218d34
AM
2983 (c-keep-region-active)
2984 new))
785eecbb
RS
2985
2986\f
2987;; commands to indent lines, regions, defuns, and expressions
b8ded794 2988(defun c-indent-command (&optional arg)
785eecbb
RS
2989 "Indent current line as C code, and/or insert some whitespace.
2990
2991If `c-tab-always-indent' is t, always just indent the current line.
2992If nil, indent the current line only if point is at the left margin or
2993in the line's indentation; otherwise insert some whitespace[*]. If
2994other than nil or t, then some whitespace[*] is inserted only within
2a15eb73 2995literals (comments and strings), but the line is always reindented.
785eecbb 2996
b8ded794 2997If `c-syntactic-indentation' is t, indentation is done according to
a66cd3ee
MS
2998the syntactic context. A numeric argument, regardless of its value,
2999means indent rigidly all the lines of the expression starting after
3000point so that this line becomes properly indented. The relative
3001indentation among the lines of the expression is preserved.
3002
3003If `c-syntactic-indentation' is nil, the line is just indented one
b8ded794
GM
3004step according to `c-basic-offset'. In this mode, a numeric argument
3005indents a number of such steps, positive or negative, and an empty
3006prefix argument is equivalent to -1.
3007
785eecbb
RS
3008 [*] The amount and kind of whitespace inserted is controlled by the
3009 variable `c-insert-tab-function', which is called to do the actual
3010 insertion of whitespace. Normally the function in this variable
3011 just inserts a tab character, or the equivalent number of spaces,
3012 depending on the variable `indent-tabs-mode'."
3013
0386b551 3014 (interactive "P")
a66cd3ee 3015 (let ((indent-function
b8ded794 3016 (if c-syntactic-indentation
130c507e 3017 (symbol-function 'indent-according-to-mode)
b8ded794 3018 (lambda ()
a66cd3ee 3019 (let ((c-macro-start c-macro-start)
0386b551
AM
3020 (steps (if (equal arg '(4))
3021 -1
3022 (prefix-numeric-value arg))))
a66cd3ee
MS
3023 (c-shift-line-indentation (* steps c-basic-offset))
3024 (when (and c-auto-align-backslashes
3025 (save-excursion
3026 (end-of-line)
3027 (eq (char-before) ?\\))
3028 (c-query-and-set-macro-start))
3029 ;; Realign the line continuation backslash if inside a macro.
3030 (c-backslash-region (point) (point) nil t)))
b8ded794 3031 ))))
0386b551 3032 (if (and c-syntactic-indentation arg)
b8ded794
GM
3033 ;; If c-syntactic-indentation and got arg, always indent this
3034 ;; line as C and shift remaining lines of expression the same
3035 ;; amount.
130c507e
GM
3036 (let ((shift-amt (save-excursion
3037 (back-to-indentation)
3038 (current-column)))
785eecbb 3039 beg end)
130c507e
GM
3040 (c-indent-line)
3041 (setq shift-amt (- (save-excursion
3042 (back-to-indentation)
3043 (current-column))
3044 shift-amt))
785eecbb
RS
3045 (save-excursion
3046 (if (eq c-tab-always-indent t)
0386b551 3047 (beginning-of-line)) ; FIXME!!! What is this here for? ACM 2005/10/31
785eecbb 3048 (setq beg (point))
0ec8351b 3049 (c-forward-sexp 1)
785eecbb
RS
3050 (setq end (point))
3051 (goto-char beg)
3052 (forward-line 1)
3053 (setq beg (point)))
3054 (if (> end beg)
130c507e 3055 (indent-code-rigidly beg end shift-amt "#")))
b8ded794 3056 ;; Else use c-tab-always-indent to determine behavior.
785eecbb 3057 (cond
0386b551 3058 ;; CASE 1: indent when at column zero or in line's indentation,
785eecbb
RS
3059 ;; otherwise insert a tab
3060 ((not c-tab-always-indent)
3061 (if (save-excursion
3062 (skip-chars-backward " \t")
3063 (not (bolp)))
3064 (funcall c-insert-tab-function)
b8ded794 3065 (funcall indent-function)))
785eecbb
RS
3066 ;; CASE 2: just indent the line
3067 ((eq c-tab-always-indent t)
b8ded794 3068 (funcall indent-function))
785eecbb
RS
3069 ;; CASE 3: if in a literal, insert a tab, but always indent the
3070 ;; line
3071 (t
0386b551 3072 (if (c-save-buffer-state () (c-in-literal))
785eecbb 3073 (funcall c-insert-tab-function))
b8ded794 3074 (funcall indent-function)
785eecbb
RS
3075 )))))
3076
3077(defun c-indent-exp (&optional shutup-p)
130c507e 3078 "Indent each line in the balanced expression following point syntactically.
17264191 3079If optional SHUTUP-P is non-nil, no errors are signaled if no
130c507e 3080balanced expression is found."
28c236de 3081 (interactive "*P")
51f606de 3082 (let ((here (point-marker))
130c507e 3083 end)
51f606de 3084 (set-marker-insertion-type here t)
785eecbb 3085 (unwind-protect
a66cd3ee
MS
3086 (let ((start (save-restriction
3087 ;; Find the closest following open paren that
3088 ;; ends on another line.
3089 (narrow-to-region (point-min) (c-point 'eol))
3090 (let (beg (end (point)))
3091 (while (and (setq beg (c-down-list-forward end))
3092 (setq end (c-up-list-forward beg))))
3093 (and beg
3094 (eq (char-syntax (char-before beg)) ?\()
3095 (1- beg))))))
785eecbb 3096 ;; sanity check
130c507e
GM
3097 (if (not start)
3098 (unless shutup-p
3099 (error "Cannot find start of balanced expression to indent"))
19da29f9 3100 (goto-char start)
a66cd3ee 3101 (setq end (c-safe (scan-sexps (point) 1)))
130c507e
GM
3102 (if (not end)
3103 (unless shutup-p
3104 (error "Cannot find end of balanced expression to indent"))
a66cd3ee
MS
3105 (forward-line)
3106 (if (< (point) end)
3107 (c-indent-region (point) end)))))
51f606de
GM
3108 (goto-char here)
3109 (set-marker here nil))))
785eecbb
RS
3110
3111(defun c-indent-defun ()
a66cd3ee
MS
3112 "Indent the current top-level declaration or macro syntactically.
3113In the macro case this also has the effect of realigning any line
3114continuation backslashes, unless `c-auto-align-backslashes' is nil."
28c236de 3115 (interactive "*")
a66cd3ee 3116 (let ((here (point-marker)) decl-limits)
785eecbb 3117 (unwind-protect
d9e94c22
MS
3118 (progn
3119 (c-save-buffer-state nil
3120 ;; We try to be line oriented, unless there are several
3121 ;; declarations on the same line.
3122 (if (looking-at c-syntactic-eol)
3123 (c-backward-token-2 1 nil (c-point 'bol))
3124 (c-forward-token-2 0 nil (c-point 'eol)))
3125 (setq decl-limits (c-declaration-limits nil)))
3126 (if decl-limits
3127 (c-indent-region (car decl-limits)
3128 (cdr decl-limits))))
785eecbb
RS
3129 (goto-char here)
3130 (set-marker here nil))))
3131
130c507e 3132(defun c-indent-region (start end &optional quiet)
a66cd3ee
MS
3133 "Indent syntactically every line whose first char is between START
3134and END inclusive. If the optional argument QUIET is non-nil then no
3135syntactic errors are reported, even if `c-report-syntactic-errors' is
3136non-nil."
785eecbb 3137 (save-excursion
a66cd3ee 3138 (goto-char end)
d9e94c22 3139 (skip-chars-backward " \t\n\r\f\v")
a66cd3ee 3140 (setq end (point))
785eecbb
RS
3141 (goto-char start)
3142 ;; Advance to first nonblank line.
a66cd3ee 3143 (beginning-of-line)
d9e94c22 3144 (skip-chars-forward " \t\n\r\f\v")
a66cd3ee 3145 (setq start (point))
785eecbb 3146 (beginning-of-line)
130c507e
GM
3147 (setq c-parsing-error
3148 (or (let ((endmark (copy-marker end))
3149 (c-parsing-error nil)
3150 ;; shut up any echo msgs on indiv lines
a66cd3ee 3151 (c-echo-syntactic-information-p nil)
9644a7da 3152 (ml-macro-start ; Start pos of multi-line macro.
24f15006
AM
3153 (and (c-save-buffer-state ()
3154 (save-excursion (c-beginning-of-macro)))
9644a7da 3155 (eq (char-before (c-point 'eol)) ?\\)
24f15006 3156 start))
a66cd3ee
MS
3157 (c-fix-backslashes nil)
3158 syntax)
130c507e
GM
3159 (unwind-protect
3160 (progn
3161 (c-progress-init start end 'c-indent-region)
24f15006
AM
3162
3163 (while (and (bolp) ;; One line each time round the loop.
130c507e
GM
3164 (not (eobp))
3165 (< (point) endmark))
3166 ;; update progress
3167 (c-progress-update)
a66cd3ee 3168 ;; skip empty lines
24f15006 3169 (unless (or (looking-at "\\s *$")
9644a7da 3170 (and ml-macro-start (looking-at "\\s *\\\\$")))
24f15006
AM
3171 ;; Get syntax and indent.
3172 (c-save-buffer-state nil
3173 (setq syntax (c-guess-basic-syntax)))
3174 (c-indent-line syntax t t))
3175
9644a7da
AM
3176 (if ml-macro-start
3177 ;; End of current multi-line macro?
3178 (when (and c-auto-align-backslashes
3179 (not (eq (char-before (c-point 'eol)) ?\\)))
3180 ;; Fixup macro backslashes.
3181 (c-backslash-region ml-macro-start (c-point 'bonl) nil)
3182 (setq ml-macro-start nil))
3183 ;; New multi-line macro?
3184 (if (and (assq 'cpp-macro syntax)
3185 (eq (char-before (c-point 'eol)) ?\\))
3186 (setq ml-macro-start (point))))
3187
24f15006
AM
3188 (forward-line))
3189
9644a7da
AM
3190 (if (and ml-macro-start c-auto-align-backslashes)
3191 (c-backslash-region ml-macro-start (c-point 'bopl) nil t)))
130c507e
GM
3192 (set-marker endmark nil)
3193 (c-progress-fini 'c-indent-region))
3194 (c-echo-parsing-error quiet))
3195 c-parsing-error))))
785eecbb 3196
130c507e
GM
3197(defun c-fn-region-is-active-p ()
3198 ;; Function version of the macro for use in places that aren't
3199 ;; compiled, e.g. in the menus.
3200 (c-region-is-active-p))
3201
c02a1ee9
RS
3202(defun c-indent-line-or-region (&optional arg region)
3203 "Indent active region, current line, or block starting on this line.
3204In Transient Mark mode, when the region is active, reindent the region.
21d46113 3205Otherwise, with a prefix argument, rigidly reindent the expression
c02a1ee9
RS
3206starting on the current line.
3207Otherwise reindent just the current line."
3208 (interactive
3209 (list current-prefix-arg (use-region-p)))
3210 (if region
0ec8351b 3211 (c-indent-region (region-beginning) (region-end))
c02a1ee9 3212 (c-indent-command arg)))
785eecbb
RS
3213\f
3214;; for progress reporting
3215(defvar c-progress-info nil)
3216
3217(defun c-progress-init (start end context)
a7c7b186
KH
3218 (cond
3219 ;; Be silent
3220 ((not c-progress-interval))
3221 ;; Start the progress update messages. If this Emacs doesn't have
3222 ;; a built-in timer, just be dumb about it.
3223 ((not (fboundp 'current-time))
130c507e 3224 (message "Indenting region... (this may take a while)"))
a7c7b186
KH
3225 ;; If progress has already been initialized, do nothing. otherwise
3226 ;; initialize the counter with a vector of:
3227 ;; [start end lastsec context]
3228 (c-progress-info)
3229 (t (setq c-progress-info (vector start
785eecbb
RS
3230 (save-excursion
3231 (goto-char end)
3232 (point-marker))
3233 (nth 1 (current-time))
3234 context))
130c507e 3235 (message "Indenting region..."))
a7c7b186 3236 ))
785eecbb
RS
3237
3238(defun c-progress-update ()
785eecbb
RS
3239 (if (not (and c-progress-info c-progress-interval))
3240 nil
3241 (let ((now (nth 1 (current-time)))
3242 (start (aref c-progress-info 0))
3243 (end (aref c-progress-info 1))
3244 (lastsecs (aref c-progress-info 2)))
3245 ;; should we update? currently, update happens every 2 seconds,
3246 ;; what's the right value?
3247 (if (< c-progress-interval (- now lastsecs))
3248 (progn
130c507e 3249 (message "Indenting region... (%d%% complete)"
785eecbb
RS
3250 (/ (* 100 (- (point) start)) (- end start)))
3251 (aset c-progress-info 2 now)))
3252 )))
3253
3254(defun c-progress-fini (context)
a7c7b186
KH
3255 (if (not c-progress-interval)
3256 nil
3257 (if (or (eq context (aref c-progress-info 3))
3258 (eq context t))
3259 (progn
3260 (set-marker (aref c-progress-info 1) nil)
3261 (setq c-progress-info nil)
130c507e 3262 (message "Indenting region... done")))))
785eecbb
RS
3263
3264
3265\f
3266;;; This page handles insertion and removal of backslashes for C macros.
3267
a66cd3ee 3268(defun c-backslash-region (from to delete-flag &optional line-mode)
785eecbb
RS
3269 "Insert, align, or delete end-of-line backslashes on the lines in the region.
3270With no argument, inserts backslashes and aligns existing backslashes.
a66cd3ee
MS
3271With an argument, deletes the backslashes. The backslash alignment is
3272done according to the settings in `c-backslash-column',
3273`c-backslash-max-column' and `c-auto-align-backslashes'.
785eecbb
RS
3274
3275This function does not modify blank lines at the start of the region.
a66cd3ee
MS
3276If the region ends at the start of a line and the macro doesn't
3277continue below it, the backslash (if any) at the end of the previous
3278line is deleted.
0ec8351b 3279
785eecbb
RS
3280You can put the region around an entire macro definition and use this
3281command to conveniently insert and align the necessary backslashes."
28c236de 3282 (interactive "*r\nP")
a66cd3ee
MS
3283 (let ((endmark (make-marker))
3284 ;; Keep the backslash trimming functions from changing the
3285 ;; whitespace around point, since in this case it's only the
3286 ;; position of point that tells the indentation of the line.
3287 (point-pos (if (save-excursion
3288 (skip-chars-backward " \t")
3289 (and (bolp) (looking-at "[ \t]*\\\\?$")))
3290 (point-marker)
3291 (point-min)))
3292 column longest-line-col bs-col-after-end)
3293 (save-excursion
3294 (goto-char to)
3295 (if (and (not line-mode) (bobp))
3296 ;; Nothing to do if to is at bob, since we should back up
3297 ;; and there's no line to back up to.
3298 nil
3299 (when (and (not line-mode) (bolp))
3300 ;; Do not back up the to line if line-mode is set, to make
3301 ;; e.g. c-newline-and-indent consistent regardless whether
3302 ;; the (newline) call leaves point at bol or not.
3303 (backward-char)
3304 (setq to (point)))
3305 (if delete-flag
3306 (progn
3307 (set-marker endmark (point))
3308 (goto-char from)
3309 (c-delete-backslashes-forward endmark point-pos))
3310 ;; Set bs-col-after-end to the column of any backslash
3311 ;; following the region, or nil if there is none.
3312 (setq bs-col-after-end
3313 (and (progn (end-of-line)
3314 (eq (char-before) ?\\))
3315 (= (forward-line 1) 0)
3316 (progn (end-of-line)
3317 (eq (char-before) ?\\))
3318 (1- (current-column))))
3319 (when line-mode
3320 ;; Back up the to line if line-mode is set, since the line
3321 ;; after the newly inserted line break should not be
3322 ;; touched in c-newline-and-indent.
3323 (setq to (max from (or (c-safe (c-point 'eopl)) from)))
3324 (unless bs-col-after-end
3325 ;; Set bs-col-after-end to non-nil in any case, since we
3326 ;; do not want to delete the backslash at the last line.
3327 (setq bs-col-after-end t)))
3328 (if (and line-mode
3329 (not c-auto-align-backslashes))
3330 (goto-char from)
3331 ;; Compute the smallest column number past the ends of all
3332 ;; the lines.
3333 (setq longest-line-col 0)
3334 (goto-char to)
3335 (if bs-col-after-end
3336 ;; Include one more line in the max column
3337 ;; calculation, since the to line will be backslashed
3338 ;; too.
3339 (forward-line 1))
3340 (end-of-line)
3341 (while (and (>= (point) from)
3342 (progn
3343 (if (eq (char-before) ?\\)
3344 (forward-char -1))
3345 (skip-chars-backward " \t")
3346 (setq longest-line-col (max longest-line-col
3347 (1+ (current-column))))
3348 (beginning-of-line)
3349 (not (bobp))))
3350 (backward-char))
3351 ;; Try to align with surrounding backslashes.
3352 (goto-char from)
3353 (beginning-of-line)
3354 (if (and (not (bobp))
3355 (progn (backward-char)
3356 (eq (char-before) ?\\)))
3357 (progn
3358 (setq column (1- (current-column)))
3359 (if (numberp bs-col-after-end)
3360 ;; Both a preceding and a following backslash.
3361 ;; Choose the greatest of them.
3362 (setq column (max column bs-col-after-end)))
3363 (goto-char from))
3364 ;; No preceding backslash. Try to align with one
3365 ;; following the region. Disregard the backslash at the
3366 ;; to line since it's likely to be bogus (e.g. when
3367 ;; called from c-newline-and-indent).
3368 (if (numberp bs-col-after-end)
3369 (setq column bs-col-after-end))
3370 ;; Don't modify blank lines at start of region.
3371 (goto-char from)
3372 (while (and (< (point) to) (bolp) (eolp))
3373 (forward-line 1)))
3374 (if (and column (< column longest-line-col))
3375 ;; Don't try to align with surrounding backslashes if
3376 ;; any line is too long.
3377 (setq column nil))
3378 (unless column
3379 ;; Impose minimum limit and tab width alignment only if
3380 ;; we can't align with surrounding backslashes.
3381 (if (> (% longest-line-col tab-width) 0)
3382 (setq longest-line-col
3383 (* (/ (+ longest-line-col tab-width -1)
3384 tab-width)
3385 tab-width)))
3386 (setq column (max c-backslash-column
3387 longest-line-col)))
3388 ;; Always impose maximum limit.
3389 (setq column (min column c-backslash-max-column)))
3390 (if bs-col-after-end
3391 ;; Add backslashes on all lines if the macro continues
3392 ;; after the to line.
3393 (progn
3394 (set-marker endmark to)
3395 (c-append-backslashes-forward endmark column point-pos))
3396 ;; Add backslashes on all lines except the last, and
3397 ;; remove any on the last line.
3398 (if (save-excursion
3399 (goto-char to)
3400 (beginning-of-line)
3401 (if (not (bobp))
3402 (set-marker endmark (1- (point)))))
3403 (progn
3404 (c-append-backslashes-forward endmark column point-pos)
3405 ;; The function above leaves point on the line
3406 ;; following endmark.
3407 (set-marker endmark (point)))
3408 (set-marker endmark to))
3409 (c-delete-backslashes-forward endmark point-pos)))))
3410 (set-marker endmark nil)
3411 (if (markerp point-pos)
3412 (set-marker point-pos nil))))
3413
3414(defun c-append-backslashes-forward (to-mark column point-pos)
3415 (let ((state (parse-partial-sexp (c-point 'bol) (point))))
3416 (if column
3417 (while
3418 (and
3419 (<= (point) to-mark)
3420
3421 (let ((start (point)) (inserted nil) end col)
3422 (end-of-line)
3423 (unless (eq (char-before) ?\\)
3424 (insert ?\\)
3425 (setq inserted t))
3426 (setq state (parse-partial-sexp
3427 start (point) nil nil state))
3428 (backward-char)
3429 (setq col (current-column))
3430
3431 ;; Avoid unnecessary changes of the buffer.
3432 (cond ((and (not inserted) (nth 3 state))
3433 ;; Don't realign backslashes in string literals
3434 ;; since that would change them.
3435 )
3436
3437 ((< col column)
3438 (delete-region
3439 (point)
3440 (progn
3441 (skip-chars-backward
3442 " \t" (if (>= (point) point-pos) point-pos))
3443 (point)))
3444 (indent-to column))
3445
3446 ((and (= col column)
3447 (memq (char-before) '(?\ ?\t))))
3448
3449 ((progn
3450 (setq end (point))
3451 (or (/= (skip-chars-backward
3452 " \t" (if (>= (point) point-pos) point-pos))
3453 -1)
3454 (/= (char-after) ?\ )))
3455 (delete-region (point) end)
3456 (indent-to column 1)))
3457
3efc2cd7
MS
3458 (zerop (forward-line 1)))
3459 (bolp))) ; forward-line has funny behavior at eob.
a66cd3ee
MS
3460
3461 ;; Make sure there are backslashes with at least one space in
3462 ;; front of them.
3463 (while
3464 (and
3465 (<= (point) to-mark)
3466
3467 (let ((start (point)))
3468 (end-of-line)
3469 (setq state (parse-partial-sexp
3470 start (point) nil nil state))
3471
3472 (if (eq (char-before) ?\\)
3473 (unless (nth 3 state)
3474 (backward-char)
3475 (unless (and (memq (char-before) '(?\ ?\t))
3476 (/= (point) point-pos))
3477 (insert ?\ )))
3478
3479 (if (and (memq (char-before) '(?\ ?\t))
3480 (/= (point) point-pos))
3481 (insert ?\\)
3482 (insert ?\ ?\\)))
3483
3efc2cd7
MS
3484 (zerop (forward-line 1)))
3485 (bolp)))))) ; forward-line has funny behavior at eob.
a66cd3ee
MS
3486
3487(defun c-delete-backslashes-forward (to-mark point-pos)
3488 (while
3489 (and (<= (point) to-mark)
3490 (progn
3491 (end-of-line)
3492 (if (eq (char-before) ?\\)
3493 (delete-region
3494 (point)
3495 (progn (backward-char)
3496 (skip-chars-backward " \t" (if (>= (point) point-pos)
3497 point-pos))
3498 (point))))
3efc2cd7
MS
3499 (zerop (forward-line 1)))
3500 (bolp)))) ; forward-line has funny behavior at eob.
785eecbb 3501
51f606de 3502
785eecbb 3503\f
51f606de
GM
3504;;; Line breaking and paragraph filling.
3505
130c507e
GM
3506(defvar c-auto-fill-prefix t)
3507(defvar c-lit-limits nil)
3508(defvar c-lit-type nil)
3509
51f606de
GM
3510;; The filling code is based on a simple theory; leave the intricacies
3511;; of the text handling to the currently active mode for that
3512;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
3513;; possible to make them work correctly wrt the comment and string
3514;; separators, one-line paragraphs etc. Unfortunately, when it comes
3515;; to it, there's quite a lot of special cases to handle which makes
3516;; the code anything but simple. The intention is that it will work
3517;; with any well-written text filling package that preserves a fill
3518;; prefix.
3519;;
3520;; We temporarily mask comment starters and enders as necessary for
3521;; the filling code to do its job on a seemingly normal text block.
3522;; We do _not_ mask the fill prefix, so it's up to the filling code to
3523;; preserve it correctly (especially important when filling C++ style
3524;; line comments). By default, we set up and use adaptive-fill-mode,
3525;; which is standard in all supported Emacs flavors.
3526
3527(defun c-guess-fill-prefix (lit-limits lit-type)
3528 ;; Determine the appropriate comment fill prefix for a block or line
3529 ;; comment. Return a cons of the prefix string and the column where
3530 ;; it ends. If fill-prefix is set, it'll override. Note that this
3531 ;; function also uses the value of point in some heuristics.
0386b551
AM
3532 ;;
3533 ;; This function might do hidden buffer changes.
d9e94c22 3534
51f606de
GM
3535 (let* ((here (point))
3536 (prefix-regexp (concat "[ \t]*\\("
130c507e 3537 c-current-comment-prefix
51f606de
GM
3538 "\\)[ \t]*"))
3539 (comment-start-regexp (if (eq lit-type 'c++)
3540 prefix-regexp
3541 comment-start-skip))
130c507e 3542 prefix-line comment-prefix res comment-text-end)
d9e94c22 3543
51f606de
GM
3544 (cond
3545 (fill-prefix
3546 (setq res (cons fill-prefix
3547 ;; Ugly way of getting the column after the fill
3548 ;; prefix; it'd be nice with a current-column
3549 ;; that works on strings..
d9e94c22 3550 (let ((start (point)))
51f606de
GM
3551 (unwind-protect
3552 (progn
a66cd3ee 3553 (insert-and-inherit "\n" fill-prefix)
51f606de 3554 (current-column))
d9e94c22
MS
3555 (delete-region start (point)))))))
3556
51f606de
GM
3557 ((eq lit-type 'c++)
3558 (save-excursion
3559 ;; Set fallback for comment-prefix if none is found.
130c507e
GM
3560 (setq comment-prefix "// "
3561 comment-text-end (cdr lit-limits))
d9e94c22 3562
51f606de
GM
3563 (beginning-of-line)
3564 (if (> (point) (car lit-limits))
3565 ;; The current line is not the comment starter, so the
3566 ;; comment has more than one line, and it can therefore be
3567 ;; used to find the comment fill prefix.
3568 (setq prefix-line (point))
d9e94c22 3569
51f606de
GM
3570 (goto-char (car lit-limits))
3571 (if (and (= (forward-line 1) 0)
3572 (< (point) (cdr lit-limits)))
3573 ;; The line after the comment starter is inside the
3574 ;; comment, so we can use it.
3575 (setq prefix-line (point))
d9e94c22 3576
51f606de
GM
3577 ;; The comment is only one line. Take the comment prefix
3578 ;; from it and keep the indentation.
3579 (goto-char (car lit-limits))
3580 (if (looking-at prefix-regexp)
3581 (goto-char (match-end 0))
3582 (forward-char 2)
3583 (skip-chars-forward " \t"))
d9e94c22 3584
a66cd3ee
MS
3585 (let (str col)
3586 (if (eq (c-point 'boi) (car lit-limits))
3587 ;; There is only whitespace before the comment
3588 ;; starter; take the prefix straight from this line.
3589 (setq str (buffer-substring-no-properties
51f606de 3590 (c-point 'bol) (point))
a66cd3ee 3591 col (current-column))
d9e94c22 3592
a66cd3ee
MS
3593 ;; There is code before the comment starter, so we
3594 ;; have to temporarily insert and indent a new line to
3595 ;; get the right space/tab mix in the indentation.
d9e94c22 3596 (let ((prefix-len (- (point) (car lit-limits)))
a66cd3ee
MS
3597 tmp)
3598 (unwind-protect
3599 (progn
3600 (goto-char (car lit-limits))
3601 (indent-to (prog1 (current-column)
3602 (insert ?\n)))
3603 (setq tmp (point))
3604 (forward-char prefix-len)
3605 (setq str (buffer-substring-no-properties
51f606de 3606 (c-point 'bol) (point))
a66cd3ee 3607 col (current-column)))
d9e94c22
MS
3608 (delete-region (car lit-limits) tmp))))
3609
a66cd3ee
MS
3610 (setq res
3611 (if (or (string-match "\\s \\'" str) (not (eolp)))
3612 (cons str col)
3613 ;; The prefix ends the line with no whitespace
3614 ;; after it. Default to a single space.
3615 (cons (concat str " ") (1+ col))))
3616 )))))
d9e94c22 3617
51f606de 3618 (t
a66cd3ee
MS
3619 (setq comment-text-end
3620 (save-excursion
3621 (goto-char (- (cdr lit-limits) 2))
3622 (if (looking-at "\\*/") (point) (cdr lit-limits))))
d9e94c22 3623
51f606de
GM
3624 (save-excursion
3625 (beginning-of-line)
3626 (if (and (> (point) (car lit-limits))
3627 (not (and (looking-at "[ \t]*\\*/")
3628 (eq (cdr lit-limits) (match-end 0)))))
3629 ;; The current line is not the comment starter and
3630 ;; contains more than just the ender, so it's good enough
3631 ;; to be used for the comment fill prefix.
3632 (setq prefix-line (point))
3633 (goto-char (car lit-limits))
d9e94c22
MS
3634
3635 (cond ((or (/= (forward-line 1) 0)
3636 (>= (point) (cdr lit-limits))
3637 (and (looking-at "[ \t]*\\*/")
3638 (eq (cdr lit-limits) (match-end 0)))
3639 (and (looking-at prefix-regexp)
3640 (<= (1- (cdr lit-limits)) (match-end 0))))
3641 ;; The comment is either one line or the next line contains
3642 ;; just the comment ender. In this case we have no
3643 ;; information about a suitable comment prefix, so we resort
3644 ;; to c-block-comment-prefix.
3645 (setq comment-prefix (or c-block-comment-prefix "")))
3646
3647 ((< here (point))
3648 ;; The point was on the comment opener line, so we might want
3649 ;; to treat this as a not yet closed comment.
3650
3651 (if (and (match-beginning 1)
3652 (/= (match-beginning 1) (match-end 1)))
3653 ;; Above `prefix-regexp' matched a nonempty prefix on the
3654 ;; second line, so let's use it. Normally it should do
3655 ;; to set `prefix-line' and let the code below pick up
3656 ;; the whole prefix, but if there's no text after the
3657 ;; match then it will probably fall back to no prefix at
3658 ;; all if the comment isn't closed yet, so in that case
3659 ;; it's better to force use of the prefix matched now.
3660 (if (= (match-end 0) (c-point 'eol))
3661 (setq comment-prefix (match-string 1))
3662 (setq prefix-line (point)))
3663
3664 ;; There's no nonempty prefix on the line after the
3665 ;; comment opener. If the line is empty, or if the
d858963e 3666 ;; text on it has less or equal indentation than the
d9e94c22
MS
3667 ;; comment starter we assume it's an unclosed
3668 ;; comment starter, i.e. that
3669 ;; `c-block-comment-prefix' should be used.
3670 ;; Otherwise we assume it's a closed comment where
3671 ;; the prefix really is the empty string.
3672 ;; E.g. this is an unclosed comment:
3673 ;;
3674 ;; /*
3675 ;; foo
3676 ;;
3677 ;; But this is not:
3678 ;;
3679 ;; /*
3680 ;; foo
3681 ;; */
3682 ;;
3683 ;; (Looking for the presence of the comment closer
3684 ;; rarely works since it's probably the closer of
3685 ;; some comment further down when the comment
3686 ;; really is unclosed.)
3687 (if (<= (save-excursion (back-to-indentation)
3688 (current-column))
3689 (save-excursion (goto-char (car lit-limits))
3690 (current-column)))
3691 (setq comment-prefix (or c-block-comment-prefix ""))
3692 (setq prefix-line (point)))))
3693
3694 (t
3695 ;; Otherwise the line after the comment starter is good
3696 ;; enough to find the prefix in.
3697 (setq prefix-line (point))))
3698
3699 (when comment-prefix
3700 ;; Haven't got the comment prefix on any real line that we
3701 ;; can take it from, so we have to temporarily insert
3702 ;; `comment-prefix' on a line and indent it to find the
3703 ;; correct column and the correct mix of tabs and spaces.
3704 (setq res
3705 (let (tmp-pre tmp-post)
3706 (unwind-protect
3707 (progn
3708
3709 (goto-char (car lit-limits))
3710 (if (looking-at comment-start-regexp)
3711 (goto-char (min (match-end 0)
3712 comment-text-end))
3713 (forward-char 2)
3714 (skip-chars-forward " \t"))
3715
3716 (when (eq (char-syntax (char-before)) ?\ )
3717 ;; If there's ws on the current line, we'll use it
3718 ;; instead of what's ending comment-prefix.
3719 (setq comment-prefix
3720 (concat (substring comment-prefix
3721 0 (string-match
3722 "\\s *\\'"
3723 comment-prefix))
3724 (buffer-substring-no-properties
3725 (save-excursion
3726 (skip-chars-backward " \t")
3727 (point))
3728 (point)))))
3729
3730 (setq tmp-pre (point-marker))
3731
3732 ;; We insert an extra non-whitespace character
3733 ;; before the line break and after comment-prefix in
3734 ;; case it's "" or ends with whitespace.
3735 (insert-and-inherit "x\n" comment-prefix "x")
3736 (setq tmp-post (point-marker))
3737
3738 (indent-according-to-mode)
3739
3740 (goto-char (1- tmp-post))
3741 (cons (buffer-substring-no-properties
3742 (c-point 'bol) (point))
3743 (current-column)))
3744
3745 (when tmp-post
3746 (delete-region tmp-pre tmp-post)
3747 (set-marker tmp-pre nil)
3748 (set-marker tmp-post nil))))))))))
3749
3750 (or res ; Found a good prefix above.
3751
51f606de
GM
3752 (save-excursion
3753 ;; prefix-line is the bol of a line on which we should try
3754 ;; to find the prefix.
3755 (let* (fb-string fb-endpos ; Contains any fallback prefix found.
3756 (test-line
3757 (lambda ()
3758 (when (and (looking-at prefix-regexp)
130c507e
GM
3759 (<= (match-end 0) comment-text-end))
3760 (unless (eq (match-end 0) (c-point 'eol))
3761 ;; The match is fine if there's text after it.
3762 (throw 'found (cons (buffer-substring-no-properties
3763 (match-beginning 0) (match-end 0))
3764 (progn (goto-char (match-end 0))
3765 (current-column)))))
51f606de 3766 (unless fb-string
130c507e
GM
3767 ;; This match is better than nothing, so let's
3768 ;; remember it in case nothing better is found
3769 ;; on another line.
51f606de
GM
3770 (setq fb-string (buffer-substring-no-properties
3771 (match-beginning 0) (match-end 0))
3772 fb-endpos (match-end 0)))
51f606de 3773 t))))
d9e94c22 3774
130c507e 3775 (or (catch 'found
51f606de
GM
3776 ;; Search for a line which has text after the prefix
3777 ;; so that we get the proper amount of whitespace
3778 ;; after it. We start with the current line, then
3779 ;; search backwards, then forwards.
d9e94c22 3780
51f606de
GM
3781 (goto-char prefix-line)
3782 (when (and (funcall test-line)
130c507e
GM
3783 (or (/= (match-end 1) (match-end 0))
3784 ;; The whitespace is sucked up by the
3785 ;; first [ \t]* glob if the prefix is empty.
3786 (and (= (match-beginning 1) (match-end 1))
3787 (/= (match-beginning 0) (match-end 0)))))
51f606de
GM
3788 ;; If the current line doesn't have text but do
3789 ;; have whitespace after the prefix, we'll use it.
130c507e
GM
3790 (throw 'found (cons fb-string
3791 (progn (goto-char fb-endpos)
3792 (current-column)))))
d9e94c22 3793
130c507e
GM
3794 (if (eq lit-type 'c++)
3795 ;; For line comments we can search up to and
3796 ;; including the first line.
3797 (while (and (zerop (forward-line -1))
3798 (>= (point) (car lit-limits)))
3799 (funcall test-line))
3800 ;; For block comments we must stop before the
3801 ;; block starter.
3802 (while (and (zerop (forward-line -1))
3803 (> (point) (car lit-limits)))
3804 (funcall test-line)))
d9e94c22 3805
51f606de
GM
3806 (goto-char prefix-line)
3807 (while (and (zerop (forward-line 1))
3808 (< (point) (cdr lit-limits)))
3809 (funcall test-line))
d9e94c22 3810
130c507e 3811 (goto-char prefix-line)
51f606de 3812 nil)
d9e94c22 3813
130c507e 3814 (when fb-string
51f606de
GM
3815 ;; A good line wasn't found, but at least we have a
3816 ;; fallback that matches the comment prefix regexp.
a66cd3ee
MS
3817 (cond ((or (string-match "\\s \\'" fb-string)
3818 (progn
3819 (goto-char fb-endpos)
3820 (not (eolp))))
3821 ;; There are ws or text after the prefix, so
3822 ;; let's use it.
3823 (cons fb-string (current-column)))
d9e94c22 3824
51f606de
GM
3825 ((progn
3826 ;; Check if there's any whitespace padding
3827 ;; on the comment start line that we can
3828 ;; use after the prefix.
3829 (goto-char (car lit-limits))
3830 (if (looking-at comment-start-regexp)
3831 (goto-char (match-end 0))
3832 (forward-char 2)
3833 (skip-chars-forward " \t"))
a66cd3ee
MS
3834 (or (not (eolp))
3835 (eq (char-syntax (char-before)) ?\ )))
d9e94c22 3836
51f606de
GM
3837 (setq fb-string (buffer-substring-no-properties
3838 (save-excursion
3839 (skip-chars-backward " \t")
3840 (point))
3841 (point)))
3842 (goto-char fb-endpos)
3843 (skip-chars-backward " \t")
d9e94c22
MS
3844
3845 (let ((tmp (point)))
51f606de
GM
3846 ;; Got to mess in the buffer once again to
3847 ;; ensure the column gets correct. :P
3848 (unwind-protect
3849 (progn
a66cd3ee 3850 (insert-and-inherit fb-string)
51f606de
GM
3851 (cons (buffer-substring-no-properties
3852 (c-point 'bol)
3853 (point))
3854 (current-column)))
d9e94c22
MS
3855 (delete-region tmp (point)))))
3856
51f606de
GM
3857 (t
3858 ;; Last resort: Just add a single space after
3859 ;; the prefix.
3860 (cons (concat fb-string " ")
3861 (progn (goto-char fb-endpos)
130c507e 3862 (1+ (current-column)))))))
d9e94c22 3863
51f606de
GM
3864 ;; The line doesn't match the comment prefix regexp.
3865 (if comment-prefix
3866 ;; We have a fallback for line comments that we must use.
3867 (cons (concat (buffer-substring-no-properties
3868 prefix-line (c-point 'boi))
3869 comment-prefix)
3870 (progn (back-to-indentation)
3871 (+ (current-column) (length comment-prefix))))
d9e94c22 3872
51f606de
GM
3873 ;; Assume we are dealing with a "free text" block
3874 ;; comment where the lines doesn't have any comment
3875 ;; prefix at all and we should just fill it as
3876 ;; normal text.
130c507e 3877 '("" . 0))))))
51f606de
GM
3878 ))
3879
d9e94c22
MS
3880(defun c-mask-paragraph (fill-paragraph apply-outside-literal fun &rest args)
3881 ;; Calls FUN with ARGS ar arguments while the current paragraph is
3882 ;; masked to allow adaptive filling to work correctly. That
3883 ;; includes narrowing the buffer and, if point is inside a comment,
3884 ;; masking the comment starter and ender appropriately.
3885 ;;
3886 ;; FILL-PARAGRAPH is non-nil if called for whole paragraph filling.
3887 ;; The position of point is then less significant when doing masking
3888 ;; and narrowing.
3889 ;;
3890 ;; If APPLY-OUTSIDE-LITERAL is nil then the function will be called
3891 ;; only if the point turns out to be inside a comment or a string.
07cc1196 3892 ;;
0386b551 3893 ;; Note that this function does not do any hidden buffer changes.
d9e94c22 3894
a66cd3ee 3895 (let (fill
e6a24f43 3896 ;; beg and end limit the region to narrow. end is a marker.
51f606de 3897 beg end
130c507e 3898 ;; tmp-pre and tmp-post mark strings that are temporarily
51f606de
GM
3899 ;; inserted at the start and end of the region. tmp-pre is a
3900 ;; cons of the positions of the prepended string. tmp-post is
3901 ;; a marker pointing to the single character of the appended
3902 ;; string.
3903 tmp-pre tmp-post
130c507e
GM
3904 ;; If hang-ender-stuck isn't nil, the comment ender is
3905 ;; hanging. In that case it's set to the number of spaces
3906 ;; that should be between the text and the ender.
3907 hang-ender-stuck
0386b551
AM
3908 ;; auto-fill-spaces is the exact sequence of whitespace between a
3909 ;; comment's last word and the comment ender, temporarily replaced
17264191 3910 ;; with 'x's before calling FUN when FILL-PARAGRAPH is nil.
0386b551 3911 auto-fill-spaces
a66cd3ee
MS
3912 (here (point))
3913 (c-lit-limits c-lit-limits)
3914 (c-lit-type c-lit-type))
d9e94c22 3915
51f606de
GM
3916 ;; Restore point on undo. It's necessary since we do a lot of
3917 ;; hidden inserts and deletes below that should be as transparent
3918 ;; as possible.
51c9af45 3919 (if (and buffer-undo-list (not (eq buffer-undo-list t)))
51f606de 3920 (setq buffer-undo-list (cons (point) buffer-undo-list)))
d9e94c22 3921
51c9af45
AM
3922 ;; Determine the limits and type of the containing literal (if any):
3923 ;; C-LIT-LIMITS, C-LIT-TYPE; and the limits of the current paragraph:
3924 ;; BEG and END.
0386b551
AM
3925 (c-save-buffer-state ()
3926 (save-restriction
3927 ;; Widen to catch comment limits correctly.
3928 (widen)
3929 (unless c-lit-limits
3930 (setq c-lit-limits (c-literal-limits nil fill-paragraph)))
3931 (setq c-lit-limits (c-collect-line-comments c-lit-limits))
3932 (unless c-lit-type
3933 (setq c-lit-type (c-literal-type c-lit-limits))))
d9e94c22 3934
0386b551
AM
3935 (save-excursion
3936 (unless (c-safe (backward-char)
3937 (forward-paragraph)
3938 (>= (point) here))
3939 (goto-char here)
3940 (forward-paragraph))
3941 (setq end (point-marker)))
3942 (save-excursion
3943 (unless (c-safe (forward-char)
3944 (backward-paragraph)
3945 (<= (point) here))
3946 (goto-char here)
3947 (backward-paragraph))
3948 (setq beg (point))))
d9e94c22 3949
130c507e
GM
3950 (unwind-protect
3951 (progn
51c9af45
AM
3952 ;; For each of the possible types of text (string, C comment ...)
3953 ;; determine BEG and END, the region we will narrow to. If we're in
3954 ;; a literal, constrain BEG and END to the limits of this literal.
3955 ;;
3956 ;; For some of these text types, particularly a block comment, we
3957 ;; may need to massage whitespace near literal delimiters, so that
3958 ;; these don't get filled inappropriately.
130c507e 3959 (cond
d9e94c22 3960
a66cd3ee 3961 ((eq c-lit-type 'c++) ; Line comment.
130c507e 3962 (save-excursion
a66cd3ee 3963 ;; Limit to the comment or paragraph end, whichever
130c507e 3964 ;; comes first.
a66cd3ee 3965 (set-marker end (min end (cdr c-lit-limits)))
d9e94c22 3966
a66cd3ee
MS
3967 (when (<= beg (car c-lit-limits))
3968 ;; The region includes the comment starter, so we must
3969 ;; check it.
3970 (goto-char (car c-lit-limits))
130c507e 3971 (back-to-indentation)
a66cd3ee
MS
3972 (if (eq (point) (car c-lit-limits))
3973 ;; Include the first line in the region.
130c507e
GM
3974 (setq beg (c-point 'bol))
3975 ;; The first line contains code before the
3976 ;; comment. We must fake a line that doesn't.
d9e94c22
MS
3977 (setq tmp-pre t))))
3978
3979 (setq apply-outside-literal t))
3980
a66cd3ee 3981 ((eq c-lit-type 'c) ; Block comment.
fd85cfb7
AM
3982 (when
3983 (or (> end (cdr c-lit-limits))
3984 (and (= end (cdr c-lit-limits))
3985 (eq (char-before end) ?/)
3986 (eq (char-before (1- end)) ?*)
3987 ;; disallow "/*/"
3988 (> (- (cdr c-lit-limits) (car c-lit-limits)) 3)))
e6a24f43
AM
3989 ;; There is a comment ender, and the region includes it. If
3990 ;; it's on its own line, it stays on its own line. If it's got
3991 ;; company on the line, it keeps (at least one word of) it.
3992 ;; "=====*/" counts as a comment ender here, but "===== */"
3993 ;; doesn't and "foo*/" doesn't.
51c9af45
AM
3994 (unless
3995 (save-excursion
3996 (goto-char (cdr c-lit-limits))
3997 (beginning-of-line)
dc56b2ba
MR
3998 ;; The following conjunct was added to avoid an
3999 ;; "Invalid search bound (wrong side of point)"
4000 ;; error in the subsequent re-search. Maybe
4001 ;; another fix would be needed (2007-12-08).
4002 (and (> (- (cdr c-lit-limits) 2) (point))
4003 (search-forward-regexp
51c9af45
AM
4004 (concat "\\=[ \t]*\\(" c-current-comment-prefix "\\)")
4005 (- (cdr c-lit-limits) 2) t)
4006 (not (search-forward-regexp
4007 "\\(\\s \\|\\sw\\)"
4008 (- (cdr c-lit-limits) 2) 'limit))
4009 ;; The comment ender IS on its own line. Exclude
4010 ;; this line from the filling.
4011 (set-marker end (c-point 'bol))))
4012
4013 ;; The comment ender is hanging. Replace all space between it
4014 ;; and the last word either by one or two 'x's (when
0386b551
AM
4015 ;; FILL-PARAGRAPH is non-nil), or a row of x's the same width
4016 ;; as the whitespace (when auto filling), and include it in
4017 ;; the region. We'll change them back to whitespace
4018 ;; afterwards. The effect of this is to glue the comment
4019 ;; ender to the last word in the comment during filling.
4020 (let* ((ender-start (save-excursion
4021 (goto-char (cdr c-lit-limits))
4022 (skip-syntax-backward "^w ")
4023 (point)))
4024 (ender-column (save-excursion
4025 (goto-char ender-start)
4026 (current-column)))
4027 (point-rel (- ender-start here))
4028 spaces)
4029
4030 (save-excursion
51c9af45 4031 ;; Insert a CR after the "*/", adjust END
0386b551
AM
4032 (goto-char (cdr c-lit-limits))
4033 (setq tmp-post (point-marker))
4034 (insert ?\n)
4035 (set-marker end (point))
51c9af45 4036
0386b551
AM
4037 (forward-line -1) ; last line of the comment
4038 (if (and (looking-at (concat "[ \t]*\\(\\("
4039 c-current-comment-prefix
4040 "\\)[ \t]*\\)"))
4041 (eq ender-start (match-end 0)))
51c9af45
AM
4042 ;; The comment ender is prefixed by nothing but a
4043 ;; comment line prefix. IS THIS POSSIBLE? (ACM,
4044 ;; 2006/4/28). Remove it along with surrounding ws.
0386b551
AM
4045 (setq spaces (- (match-end 1) (match-end 2)))
4046 (goto-char ender-start))
4047 (skip-chars-backward " \t\r\n") ; Surely this can be
4048 ; " \t"? "*/" is NOT alone on the line (ACM, 2005/8/18)
4049
51c9af45 4050 ;; What's being tested here? 2006/4/20. FIXME!!!
0386b551
AM
4051 (if (/= (point) ender-start)
4052 (progn
4053 (if (<= here (point))
4054 ;; Don't adjust point below if it's
4055 ;; before the string we replace.
4056 (setq point-rel -1))
4057 ;; Keep one or two spaces between the
4058 ;; text and the ender, depending on how
4059 ;; many there are now.
4060 (unless spaces
4061 (setq spaces (- ender-column (current-column))))
4062 (setq auto-fill-spaces (c-delete-and-extract-region
4063 (point) ender-start))
4064 ;; paragraph filling condenses multiple spaces to
4065 ;; single or double spaces. auto-fill doesn't.
4066 (if fill-paragraph
d9e94c22
MS
4067 (setq spaces
4068 (max
4069 (min spaces
4070 (if sentence-end-double-space 2 1))
0386b551
AM
4071 1)))
4072 ;; Insert the filler first to keep marks right.
4073 (insert-char ?x spaces t)
4074 (setq hang-ender-stuck spaces)
4075 (setq point-rel
4076 (and (>= point-rel 0)
4077 (- (point) (min point-rel spaces)))))
4078 (setq point-rel nil)))
4079
4080 (if point-rel
4081 ;; Point was in the middle of the string we
4082 ;; replaced above, so put it back in the same
4083 ;; relative position, counting from the end.
4084 (goto-char point-rel)))
4085 ))
d9e94c22 4086
a66cd3ee
MS
4087 (when (<= beg (car c-lit-limits))
4088 ;; The region includes the comment starter.
51f606de 4089 (save-excursion
a66cd3ee 4090 (goto-char (car c-lit-limits))
07cc1196
MS
4091 (if (looking-at (concat "\\(" comment-start-skip "\\)$"))
4092 ;; Begin with the next line.
4093 (setq beg (c-point 'bonl))
4094 ;; Fake the fill prefix in the first line.
d9e94c22
MS
4095 (setq tmp-pre t))))
4096
4097 (setq apply-outside-literal t))
4098
a66cd3ee 4099 ((eq c-lit-type 'string) ; String.
130c507e 4100 (save-excursion
a66cd3ee
MS
4101 (when (>= end (cdr c-lit-limits))
4102 (goto-char (1- (cdr c-lit-limits)))
130c507e
GM
4103 (setq tmp-post (point-marker))
4104 (insert ?\n)
4105 (set-marker end (point)))
a66cd3ee
MS
4106 (when (<= beg (car c-lit-limits))
4107 (goto-char (1+ (car c-lit-limits)))
130c507e
GM
4108 (setq beg (if (looking-at "\\\\$")
4109 ;; Leave the start line if it's
4110 ;; nothing but an escaped newline.
4111 (1+ (match-end 0))
d9e94c22
MS
4112 (point)))))
4113 (setq apply-outside-literal t))
4114
4115 ((eq c-lit-type 'pound) ; Macro
4116 ;; Narrow to the macro limits if they are nearer than the
4117 ;; paragraph limits. Don't know if this is necessary but
4118 ;; do it for completeness sake (doing auto filling at all
4119 ;; inside macros is bogus to begin with since the line
4120 ;; continuation backslashes aren't handled).
4121 (save-excursion
0386b551
AM
4122 (c-save-buffer-state ()
4123 (c-beginning-of-macro)
4124 (beginning-of-line)
4125 (if (> (point) beg)
4126 (setq beg (point)))
4127 (c-end-of-macro)
4128 (forward-line)
4129 (if (< (point) end)
4130 (set-marker end (point))))))
d9e94c22
MS
4131
4132 (t ; Other code.
4133 ;; Try to avoid comments and macros in the paragraph to
4134 ;; avoid that the adaptive fill mode gets the prefix from
4135 ;; them.
4136 (c-save-buffer-state nil
4137 (save-excursion
4138 (goto-char beg)
4139 (c-forward-syntactic-ws end)
4140 (beginning-of-line)
4141 (setq beg (point))
4142 (goto-char end)
4143 (c-backward-syntactic-ws beg)
4144 (forward-line)
4145 (set-marker end (point))))))
4146
130c507e
GM
4147 (when tmp-pre
4148 ;; Temporarily insert the fill prefix after the comment
4149 ;; starter so that the first line looks like any other
4150 ;; comment line in the narrowed region.
d9e94c22
MS
4151 (setq fill (c-save-buffer-state nil
4152 (c-guess-fill-prefix c-lit-limits c-lit-type)))
130c507e
GM
4153 (unless (string-match (concat "\\`[ \t]*\\("
4154 c-current-comment-prefix
4155 "\\)[ \t]*\\'")
4156 (car fill))
4157 ;; Oops, the prefix doesn't match the comment prefix
4158 ;; regexp. This could produce very confusing
4159 ;; results with adaptive fill packages together with
4160 ;; the insert prefix magic below, since the prefix
4161 ;; often doesn't appear at all. So let's warn about
4162 ;; it.
4163 (message "\
4164Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S"
4165 (car fill)))
4166 ;; Find the right spot on the line, break it, insert
4167 ;; the fill prefix and make sure we're back in the
4168 ;; same column by temporarily prefixing the first word
4169 ;; with a number of 'x'.
4170 (save-excursion
a66cd3ee
MS
4171 (goto-char (car c-lit-limits))
4172 (if (looking-at (if (eq c-lit-type 'c++)
fdea67e7 4173 c-current-comment-prefix
130c507e
GM
4174 comment-start-skip))
4175 (goto-char (match-end 0))
4176 (forward-char 2)
4177 (skip-chars-forward " \t"))
467690bb
MS
4178 (while (and (< (current-column) (cdr fill))
4179 (not (eolp)))
4180 (forward-char 1))
130c507e
GM
4181 (let ((col (current-column)))
4182 (setq beg (1+ (point))
4183 tmp-pre (list (point)))
4184 (unwind-protect
4185 (progn
a66cd3ee
MS
4186 (insert-and-inherit "\n" (car fill))
4187 (insert-char ?x (- col (current-column)) t))
130c507e 4188 (setcdr tmp-pre (point))))))
d9e94c22
MS
4189
4190 (when apply-outside-literal
4191 ;; `apply-outside-literal' is always set to t here if
4192 ;; we're inside a literal.
4193
4194 (let ((fill-prefix
4195 (or fill-prefix
4196 ;; Kludge: If the function that adapts the fill prefix
4197 ;; doesn't produce the required comment starter for
4198 ;; line comments, then force it by setting fill-prefix.
4199 (when (and (eq c-lit-type 'c++)
4200 ;; Kludge the kludge: filladapt-mode doesn't
4201 ;; have this problem, but it currently
4202 ;; doesn't override fill-context-prefix
4203 ;; (version 2.12).
4204 (not (and (boundp 'filladapt-mode)
4205 filladapt-mode))
4206 (not (string-match
4207 "\\`[ \t]*//"
4208 (or (fill-context-prefix beg end)
4209 ""))))
4210 (c-save-buffer-state nil
a66cd3ee 4211 (car (or fill (c-guess-fill-prefix
d9e94c22
MS
4212 c-lit-limits c-lit-type)))))))
4213
4214 ;; Save the relative position of point if it's outside the
4215 ;; region we're going to narrow. Want to restore it in that
4216 ;; case, but otherwise it should be moved according to the
4217 ;; called function.
4218 (point-rel (cond ((< (point) beg) (- (point) beg))
4219 ((> (point) end) (- (point) end)))))
4220
4221 ;; Preparations finally done! Now we can call the
4222 ;; actual function.
4223 (prog1
4224 (save-restriction
4225 (narrow-to-region beg end)
4226 (apply fun args))
4227 (if point-rel
4228 ;; Restore point if it was outside the region.
4229 (if (< point-rel 0)
4230 (goto-char (+ beg point-rel))
4231 (goto-char (+ end point-rel))))))))
4232
130c507e
GM
4233 (when (consp tmp-pre)
4234 (delete-region (car tmp-pre) (cdr tmp-pre)))
d9e94c22 4235
130c507e
GM
4236 (when tmp-post
4237 (save-excursion
4238 (goto-char tmp-post)
4239 (delete-char 1))
4240 (when hang-ender-stuck
4241 ;; Preserve point even if it's in the middle of the string
4242 ;; we replace; save-excursion doesn't work in that case.
4243 (setq here (point))
4244 (goto-char tmp-post)
4245 (skip-syntax-backward "^w ")
4246 (forward-char (- hang-ender-stuck))
0386b551
AM
4247 (if (or fill-paragraph (not auto-fill-spaces))
4248 (insert-char ?\ hang-ender-stuck t)
2b78d42c 4249 (insert auto-fill-spaces))
130c507e
GM
4250 (delete-char hang-ender-stuck)
4251 (goto-char here))
4252 (set-marker tmp-post nil))
d9e94c22 4253
a66cd3ee
MS
4254 (set-marker end nil))))
4255
4256(defun c-fill-paragraph (&optional arg)
4257 "Like \\[fill-paragraph] but handles C and C++ style comments.
4258If any of the current line is a comment or within a comment, fill the
4259comment or the paragraph of it that point is in, preserving the
4260comment indentation or line-starting decorations (see the
4261`c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
4262details).
4263
4264If point is inside multiline string literal, fill it. This currently
4265does not respect escaped newlines, except for the special case when it
4266is the very first thing in the string. The intended use for this rule
4267is in situations like the following:
4268
4269char description[] = \"\\
4270A very long description of something that you want to fill to make
4271nicely formatted output.\"\;
4272
4273If point is in any other situation, i.e. in normal code, do nothing.
4274
4275Optional prefix ARG means justify paragraph as well."
4276 (interactive "*P")
4277 (let ((fill-paragraph-function
4278 ;; Avoid infinite recursion.
4279 (if (not (eq fill-paragraph-function 'c-fill-paragraph))
4280 fill-paragraph-function)))
d9e94c22 4281 (c-mask-paragraph t nil 'fill-paragraph arg))
51f606de
GM
4282 ;; Always return t. This has the effect that if filling isn't done
4283 ;; above, it isn't done at all, and it's therefore effectively
4284 ;; disabled in normal code.
4285 t)
4286
4287(defun c-do-auto-fill ()
4288 ;; Do automatic filling if not inside a context where it should be
4289 ;; ignored.
4290 (let ((c-auto-fill-prefix
4291 ;; The decision whether the line should be broken is actually
4292 ;; done in c-indent-new-comment-line, which do-auto-fill
4293 ;; calls to break lines. We just set this special variable
4294 ;; so that we'll know when we're called from there. It's
4295 ;; also used to detect whether fill-prefix is user set or
4296 ;; generated automatically by do-auto-fill.
4297 fill-prefix))
d9e94c22 4298 (c-mask-paragraph nil t 'do-auto-fill)))
51f606de 4299
a66cd3ee
MS
4300(defun c-indent-new-comment-line (&optional soft allow-auto-fill)
4301 "Break line at point and indent, continuing comment or macro if within one.
51f606de
GM
4302If inside a comment and `comment-multi-line' is non-nil, the
4303indentation and line prefix are preserved (see the
4304`c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
b8ded794
GM
4305details). If inside a single line comment and `comment-multi-line' is
4306nil, a new comment of the same type is started on the next line and
a66cd3ee
MS
4307indented as appropriate for comments. If inside a macro, a line
4308continuation backslash is inserted and aligned as appropriate, and the
4309new line is indented according to `c-syntactic-indentation'.
51f606de
GM
4310
4311If a fill prefix is specified, it overrides all the above."
a66cd3ee
MS
4312 ;; allow-auto-fill is used from c-context-line-break to allow auto
4313 ;; filling to break the line more than once. Since this function is
4314 ;; used from auto-fill itself, that's normally disabled to avoid
4315 ;; unnecessary recursion.
51f606de
GM
4316 (interactive)
4317 (let ((fill-prefix fill-prefix)
4318 (do-line-break
4319 (lambda ()
a66cd3ee
MS
4320 (delete-horizontal-space)
4321 (if soft
4322 (insert-and-inherit ?\n)
4323 (newline (if allow-auto-fill nil 1)))))
51f606de
GM
4324 ;; Already know the literal type and limits when called from
4325 ;; c-context-line-break.
130c507e 4326 (c-lit-limits c-lit-limits)
a66cd3ee
MS
4327 (c-lit-type c-lit-type)
4328 (c-macro-start c-macro-start))
0386b551
AM
4329
4330 (c-save-buffer-state ()
4331 (when (not (eq c-auto-fill-prefix t))
4332 ;; Called from do-auto-fill.
4333 (unless c-lit-limits
4334 (setq c-lit-limits (c-literal-limits nil nil t)))
4335 (unless c-lit-type
4336 (setq c-lit-type (c-literal-type c-lit-limits)))
4337 (if (memq (cond ((c-query-and-set-macro-start) 'cpp)
4338 ((null c-lit-type) 'code)
4339 (t c-lit-type))
4340 c-ignore-auto-fill)
4341 (setq fill-prefix t) ; Used as flag in the cond.
4342 (if (and (null c-auto-fill-prefix)
4343 (eq c-lit-type 'c)
4344 (<= (c-point 'bol) (car c-lit-limits)))
4345 ;; The adaptive fill function has generated a prefix, but
4346 ;; we're on the first line in a block comment so it'll be
4347 ;; wrong. Ignore it to guess a better one below.
4348 (setq fill-prefix nil)
4349 (when (and (eq c-lit-type 'c++)
4350 (not (string-match (concat "\\`[ \t]*"
4351 c-line-comment-starter)
4352 (or fill-prefix ""))))
4353 ;; Kludge: If the function that adapted the fill prefix
4354 ;; doesn't produce the required comment starter for line
4355 ;; comments, then we ignore it.
4356 (setq fill-prefix nil)))
4357 )))
4358
51f606de
GM
4359 (cond ((eq fill-prefix t)
4360 ;; A call from do-auto-fill which should be ignored.
4361 )
4362 (fill-prefix
4363 ;; A fill-prefix overrides anything.
4364 (funcall do-line-break)
4365 (insert-and-inherit fill-prefix))
0386b551 4366 ((c-save-buffer-state ()
51f606de 4367 (unless c-lit-limits
a66cd3ee 4368 (setq c-lit-limits (c-literal-limits)))
51f606de
GM
4369 (unless c-lit-type
4370 (setq c-lit-type (c-literal-type c-lit-limits)))
4371 (memq c-lit-type '(c c++)))
a66cd3ee 4372 ;; Some sort of comment.
b8ded794
GM
4373 (if (or comment-multi-line
4374 (save-excursion
4375 (goto-char (car c-lit-limits))
4376 (end-of-line)
4377 (< (point) (cdr c-lit-limits))))
51f606de 4378 ;; Inside a comment that should be continued.
d9e94c22
MS
4379 (let ((fill (c-save-buffer-state nil
4380 (c-guess-fill-prefix
4381 (setq c-lit-limits
4382 (c-collect-line-comments c-lit-limits))
4383 c-lit-type)))
a66cd3ee 4384 (pos (point))
94dd9d6d 4385 (start-col (current-column))
a66cd3ee
MS
4386 (comment-text-end
4387 (or (and (eq c-lit-type 'c)
4388 (save-excursion
4389 (goto-char (- (cdr c-lit-limits) 2))
4390 (if (looking-at "\\*/") (point))))
4391 (cdr c-lit-limits))))
4392 ;; Skip forward past the fill prefix in case
4393 ;; we're standing in it.
4394 ;;
4395 ;; FIXME: This doesn't work well in cases like
4396 ;;
4397 ;; /* Bla bla bla bla bla
4398 ;; bla bla
4399 ;;
4400 ;; If point is on the 'B' then the line will be
4401 ;; broken after "Bla b".
94dd9d6d
AM
4402 ;;
4403 ;; If we have an empty comment, /* */, the next
4404 ;; lot of code pushes point to the */. We fix
4405 ;; this by never allowing point to end up to the
4406 ;; right of where it started.
a66cd3ee
MS
4407 (while (and (< (current-column) (cdr fill))
4408 (not (eolp)))
4409 (forward-char 1))
4410 (if (and (> (point) comment-text-end)
4411 (> (c-point 'bol) (car c-lit-limits)))
51f606de 4412 (progn
a66cd3ee
MS
4413 ;; The skip takes us out of the (block)
4414 ;; comment; insert the fill prefix at bol
4415 ;; instead and keep the position.
4416 (setq pos (copy-marker pos t))
4417 (beginning-of-line)
4418 (insert-and-inherit (car fill))
4419 (if soft (insert-and-inherit ?\n) (newline 1))
4420 (goto-char pos)
4421 (set-marker pos nil))
4422 ;; Don't break in the middle of a comment starter
4423 ;; or ender.
4424 (cond ((> (point) comment-text-end)
4425 (goto-char comment-text-end))
4426 ((< (point) (+ (car c-lit-limits) 2))
4427 (goto-char (+ (car c-lit-limits) 2))))
51f606de 4428 (funcall do-line-break)
94dd9d6d
AM
4429 (insert-and-inherit (car fill))
4430 (if (> (current-column) start-col)
4431 (move-to-column start-col)))) ; can this hit the
4432 ; middle of a TAB?
51f606de
GM
4433 ;; Inside a comment that should be broken.
4434 (let ((comment-start comment-start)
4435 (comment-end comment-end)
4436 col)
4437 (if (eq c-lit-type 'c)
4438 (unless (string-match "[ \t]*/\\*" comment-start)
4439 (setq comment-start "/* " comment-end " */"))
4440 (unless (string-match "[ \t]*//" comment-start)
4441 (setq comment-start "// " comment-end "")))
4442 (setq col (save-excursion
4443 (back-to-indentation)
4444 (current-column)))
4445 (funcall do-line-break)
4446 (when (and comment-end (not (equal comment-end "")))
4447 (forward-char -1)
4448 (insert-and-inherit comment-end)
4449 (forward-char 1))
4450 ;; c-comment-indent may look at the current
4451 ;; indentation, so let's start out with the same
4452 ;; indentation as the previous one.
4453 (indent-to col)
4454 (insert-and-inherit comment-start)
4455 (indent-for-comment))))
a66cd3ee
MS
4456 ((c-query-and-set-macro-start)
4457 ;; In a macro.
4458 (unless (looking-at "[ \t]*\\\\$")
4459 ;; Do not clobber the alignment of the line continuation
4460 ;; slash; c-backslash-region might look at it.
4461 (delete-horizontal-space))
4462 ;; Got an asymmetry here: In normal code this command
4463 ;; doesn't indent the next line syntactically, and otoh a
4464 ;; normal syntactically indenting newline doesn't continue
4465 ;; the macro.
4466 (c-newline-and-indent (if allow-auto-fill nil 1)))
51f606de
GM
4467 (t
4468 ;; Somewhere else in the code.
4469 (let ((col (save-excursion
a66cd3ee
MS
4470 (beginning-of-line)
4471 (while (and (looking-at "[ \t]*\\\\?$")
4472 (= (forward-line -1) 0)))
4473 (current-indentation))))
51f606de
GM
4474 (funcall do-line-break)
4475 (indent-to col))))))
4476
4477(defalias 'c-comment-line-break-function 'c-indent-new-comment-line)
efbc652a 4478(make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line "21.1")
51f606de
GM
4479
4480;; advice for indent-new-comment-line for older Emacsen
4481(unless (boundp 'comment-line-break-function)
130c507e 4482 (defvar c-inside-line-break-advice nil)
51f606de
GM
4483 (defadvice indent-new-comment-line (around c-line-break-advice
4484 activate preactivate)
4485 "Call `c-indent-new-comment-line' if in CC Mode."
130c507e 4486 (if (or c-inside-line-break-advice
51f606de
GM
4487 (not c-buffer-is-cc-mode))
4488 ad-do-it
130c507e 4489 (let ((c-inside-line-break-advice t))
51f606de
GM
4490 (c-indent-new-comment-line (ad-get-arg 0))))))
4491
4492(defun c-context-line-break ()
4493 "Do a line break suitable to the context.
4494
a66cd3ee
MS
4495When point is outside a comment or macro, insert a newline and indent
4496according to the syntactic context, unless `c-syntactic-indentation'
4497is nil, in which case the new line is indented as the previous
4498non-empty line instead.
4499
4500When point is inside the content of a preprocessor directive, a line
4501continuation backslash is inserted before the line break and aligned
4502appropriately. The end of the cpp directive doesn't count as inside
4503it.
51f606de
GM
4504
4505When point is inside a comment, continue it with the appropriate
4506comment prefix (see the `c-comment-prefix-regexp' and
4507`c-block-comment-prefix' variables for details). The end of a
51c9af45
AM
4508C++-style line comment doesn't count as inside it.
4509
4510When point is inside a string, only insert a backslash when it is also
4511inside a preprocessor directive."
0386b551 4512
51f606de 4513 (interactive "*")
0386b551 4514 (let* (c-lit-limits c-lit-type
a66cd3ee 4515 (c-macro-start c-macro-start))
0386b551 4516
51c9af45
AM
4517 (c-save-buffer-state ()
4518 (setq c-lit-limits (c-literal-limits nil nil t)
4519 c-lit-type (c-literal-type c-lit-limits))
4520 (when (eq c-lit-type 'c++)
4521 (setq c-lit-limits (c-collect-line-comments c-lit-limits)))
4522 (c-query-and-set-macro-start))
0386b551 4523
51c9af45
AM
4524 (cond
4525 ((or (eq c-lit-type 'c)
4526 (and (eq c-lit-type 'c++) ; C++ comment, but not at the very end of it.
4527 (< (save-excursion
4528 (skip-chars-forward " \t")
4529 (point))
4530 (1- (cdr c-lit-limits))))
4531 (and (numberp c-macro-start) ; Macro, but not at the very end of
4532 ; it, not in a string, and not in the
4533 ; cpp keyword.
4534 (not (eq c-lit-type 'string))
4535 (or (not (looking-at "\\s *$"))
4536 (eq (char-before) ?\\))
4537 (<= (save-excursion
4538 (goto-char c-macro-start)
4539 (if (looking-at c-opt-cpp-start)
4540 (goto-char (match-end 0)))
4541 (point))
4542 (point))))
4543 (let ((comment-multi-line t)
4544 (fill-prefix nil))
4545 (c-indent-new-comment-line nil t)))
4546
4547 ((eq c-lit-type 'string)
4548 (if (and (numberp c-macro-start)
4549 (not (eq (char-before) ?\\)))
4550 (insert ?\\))
4551 (newline))
4552
4553 (t (delete-horizontal-space)
4554 (newline)
51f606de
GM
4555 ;; c-indent-line may look at the current indentation, so let's
4556 ;; start out with the same indentation as the previous line.
51c9af45
AM
4557 (let ((col (save-excursion
4558 (backward-char)
4559 (forward-line 0)
4560 (while (and (looking-at "[ \t]*\\\\?$")
4561 (= (forward-line -1) 0)))
4562 (current-indentation))))
4563 (indent-to col))
4564 (indent-according-to-mode)))))
785eecbb 4565
a66cd3ee
MS
4566(defun c-context-open-line ()
4567 "Insert a line break suitable to the context and leave point before it.
4568This is the `c-context-line-break' equivalent to `open-line', which is
4569normally bound to C-o. See `c-context-line-break' for the details."
4570 (interactive "*")
4571 (let ((here (point)))
4572 (unwind-protect
4573 (progn
4574 ;; Temporarily insert a non-whitespace char to keep any
4575 ;; preceding whitespace intact.
4576 (insert ?x)
4577 (c-context-line-break))
4578 (goto-char here)
4579 (delete-char 1))))
4580
785eecbb 4581\f
130c507e 4582(cc-provide 'cc-cmds)
3afbc435 4583
cbee283d 4584;; arch-tag: bf0611dc-d1f4-449e-9e45-4ec7c6936677
785eecbb 4585;;; cc-cmds.el ends here