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