1 |
;;; cc-cmds.el --- user level commands for CC Mode |
;;; cc-cmds.el --- user level commands for CC Mode |
2 |
|
|
3 |
;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc. |
;; Copyright (C) 1985,1987,1992-2003 Free Software Foundation, Inc. |
4 |
|
|
5 |
;; Authors: 2000- Martin Stjernholm |
;; Authors: 1998- Martin Stjernholm |
6 |
;; 1998-1999 Barry A. Warsaw and Martin Stjernholm |
;; 1992-1999 Barry A. Warsaw |
|
;; 1992-1997 Barry A. Warsaw |
|
7 |
;; 1987 Dave Detlefs and Stewart Clamen |
;; 1987 Dave Detlefs and Stewart Clamen |
8 |
;; 1985 Richard M. Stallman |
;; 1985 Richard M. Stallman |
9 |
;; Maintainer: bug-cc-mode@gnu.org |
;; Maintainer: bug-cc-mode@gnu.org |
38 |
(stringp byte-compile-dest-file)) |
(stringp byte-compile-dest-file)) |
39 |
(cons (file-name-directory byte-compile-dest-file) load-path) |
(cons (file-name-directory byte-compile-dest-file) load-path) |
40 |
load-path))) |
load-path))) |
41 |
(require 'cc-bytecomp))) |
(load "cc-bytecomp" nil t))) |
42 |
|
|
43 |
(cc-require 'cc-defs) |
(cc-require 'cc-defs) |
44 |
(cc-require 'cc-vars) |
(cc-require 'cc-vars) |
|
(cc-require 'cc-langs) |
|
45 |
(cc-require 'cc-engine) |
(cc-require 'cc-engine) |
46 |
|
|
47 |
;; Silence the compiler. |
;; Silence the compiler. |
55 |
(defvar c-fix-backslashes t) |
(defvar c-fix-backslashes t) |
56 |
|
|
57 |
(defun c-shift-line-indentation (shift-amt) |
(defun c-shift-line-indentation (shift-amt) |
58 |
|
;; This function does not do any hidden buffer changes. |
59 |
(let ((pos (- (point-max) (point))) |
(let ((pos (- (point-max) (point))) |
60 |
(c-macro-start c-macro-start) |
(c-macro-start c-macro-start) |
61 |
tmp-char-inserted) |
tmp-char-inserted) |
95 |
is otherwise empty \(ignoring any line continuation backslash), but |
is otherwise empty \(ignoring any line continuation backslash), but |
96 |
that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of |
that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of |
97 |
indentation change \(in columns)." |
indentation change \(in columns)." |
98 |
|
;; |
99 |
|
;; This function does not do any hidden buffer changes. |
100 |
|
|
101 |
(let ((line-cont-backslash (save-excursion |
(let ((line-cont-backslash (save-excursion |
102 |
(end-of-line) |
(end-of-line) |
103 |
(eq (char-before) ?\\))) |
(eq (char-before) ?\\))) |
126 |
(setq c-fix-backslashes t)) |
(setq c-fix-backslashes t)) |
127 |
(if c-syntactic-indentation |
(if c-syntactic-indentation |
128 |
(setq c-parsing-error |
(setq c-parsing-error |
129 |
(or (let* ((c-parsing-error nil) |
(or (let ((c-parsing-error nil) |
130 |
(c-syntactic-context (or syntax |
(c-syntactic-context |
131 |
c-syntactic-context |
(or syntax |
132 |
(c-guess-basic-syntax))) |
(and (boundp 'c-syntactic-context) |
133 |
indent) |
c-syntactic-context)))) |
134 |
(setq indent (c-get-syntactic-indentation |
(c-save-buffer-state (indent) |
135 |
c-syntactic-context)) |
(unless c-syntactic-context |
136 |
(and (not (c-echo-parsing-error quiet)) |
(setq c-syntactic-context (c-guess-basic-syntax))) |
137 |
c-echo-syntactic-information-p |
(setq indent (c-get-syntactic-indentation |
138 |
(message "syntax: %s, indent: %d" |
c-syntactic-context)) |
139 |
c-syntactic-context indent)) |
(and (not (c-echo-parsing-error quiet)) |
140 |
(setq shift-amt (- indent (current-indentation))) |
c-echo-syntactic-information-p |
141 |
|
(message "syntax: %s, indent: %d" |
142 |
|
c-syntactic-context indent)) |
143 |
|
(setq shift-amt (- indent (current-indentation)))) |
144 |
(c-shift-line-indentation shift-amt) |
(c-shift-line-indentation shift-amt) |
145 |
(run-hooks 'c-special-indent-hook) |
(run-hooks 'c-special-indent-hook) |
146 |
c-parsing-error) |
c-parsing-error) |
170 |
and takes care to set the indentation before calling |
and takes care to set the indentation before calling |
171 |
`indent-according-to-mode', so that lineup functions like |
`indent-according-to-mode', so that lineup functions like |
172 |
`c-lineup-dont-change' works better." |
`c-lineup-dont-change' works better." |
173 |
;; FIXME: Backslashes before eol in comments and literals aren't |
;; |
174 |
|
;; This function does not do any hidden buffer changes. |
175 |
|
|
176 |
|
;; TODO: Backslashes before eol in comments and literals aren't |
177 |
;; kept intact. |
;; kept intact. |
178 |
(let ((c-macro-start (c-query-macro-start)) |
(let ((c-macro-start (c-query-macro-start)) |
179 |
;; Avoid calling c-backslash-region from c-indent-line if it's |
;; Avoid calling c-backslash-region from c-indent-line if it's |
218 |
;; Reindent syntactically. The indentation done above is not |
;; Reindent syntactically. The indentation done above is not |
219 |
;; wasted, since c-indent-line might look at the current |
;; wasted, since c-indent-line might look at the current |
220 |
;; indentation. |
;; indentation. |
221 |
(let ((c-syntactic-context (c-guess-basic-syntax))) |
(let ((c-syntactic-context (c-save-buffer-state nil |
222 |
|
(c-guess-basic-syntax)))) |
223 |
;; We temporarily insert another line break, so that the |
;; We temporarily insert another line break, so that the |
224 |
;; lineup functions will see the line as empty. That makes |
;; lineup functions will see the line as empty. That makes |
225 |
;; e.g. c-lineup-cpp-define more intuitive since it then |
;; e.g. c-lineup-cpp-define more intuitive since it then |
245 |
With universal argument, inserts the analysis as a comment on that line." |
With universal argument, inserts the analysis as a comment on that line." |
246 |
(interactive "P") |
(interactive "P") |
247 |
(let* ((c-parsing-error nil) |
(let* ((c-parsing-error nil) |
248 |
(syntax (c-guess-basic-syntax))) |
(syntax (if (boundp 'c-syntactic-context) |
249 |
|
;; Use `c-syntactic-context' in the same way as |
250 |
|
;; `c-indent-line', to be consistent. |
251 |
|
c-syntactic-context |
252 |
|
(c-save-buffer-state nil |
253 |
|
(c-guess-basic-syntax))))) |
254 |
(if (not (consp arg)) |
(if (not (consp arg)) |
255 |
(message "syntactic analysis: %s" syntax) |
(message "syntactic analysis: %s" syntax) |
256 |
(indent-for-comment) |
(indent-for-comment) |
339 |
;; Electric keys |
;; Electric keys |
340 |
|
|
341 |
(defun c-electric-backspace (arg) |
(defun c-electric-backspace (arg) |
342 |
"Deletes preceding character or whitespace. |
"Delete the preceding character or whitespace. |
343 |
If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or |
If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or |
344 |
\"/ah\" string on the mode line, then all preceding whitespace is |
\"/ah\" string on the mode line, then all preceding whitespace is |
345 |
consumed. If however a prefix argument is supplied, or |
consumed. If however a prefix argument is supplied, or |
346 |
`c-hungry-delete-key' is nil, or point is inside a literal then the |
`c-hungry-delete-key' is nil, or point is inside a literal then the |
347 |
function in the variable `c-backspace-function' is called. |
function in the variable `c-backspace-function' is called." |
|
|
|
|
See also \\[c-electric-delete]." |
|
348 |
(interactive "*P") |
(interactive "*P") |
349 |
(if (or (not c-hungry-delete-key) |
(if (or (not c-hungry-delete-key) |
350 |
arg |
arg |
351 |
(c-in-literal)) |
(c-in-literal)) |
352 |
(funcall c-backspace-function (prefix-numeric-value arg)) |
(funcall c-backspace-function (prefix-numeric-value arg)) |
353 |
(let ((here (point))) |
(c-hungry-backspace))) |
354 |
(c-skip-ws-backward) |
|
355 |
(if (/= (point) here) |
(defun c-hungry-backspace () |
356 |
(delete-region (point) here) |
"Delete the preceding character or all preceding whitespace |
357 |
(funcall c-backspace-function 1) |
back to the previous non-whitespace character. |
358 |
)))) |
See also \\[c-hungry-delete-forward]." |
359 |
|
(interactive) |
360 |
|
(let ((here (point))) |
361 |
|
(c-skip-ws-backward) |
362 |
|
(if (/= (point) here) |
363 |
|
(delete-region (point) here) |
364 |
|
(funcall c-backspace-function 1)))) |
365 |
|
|
366 |
(defun c-electric-delete-forward (arg) |
(defun c-electric-delete-forward (arg) |
367 |
"Deletes following character or whitespace. |
"Delete the following character or whitespace. |
368 |
If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or |
If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or |
369 |
\"/ah\" string on the mode line, then all following whitespace is |
\"/ah\" string on the mode line, then all following whitespace is |
370 |
consumed. If however a prefix argument is supplied, or |
consumed. If however a prefix argument is supplied, or |
375 |
arg |
arg |
376 |
(c-in-literal)) |
(c-in-literal)) |
377 |
(funcall c-delete-function (prefix-numeric-value arg)) |
(funcall c-delete-function (prefix-numeric-value arg)) |
378 |
(let ((here (point))) |
(c-hungry-delete-forward))) |
379 |
(c-skip-ws-forward) |
|
380 |
(if (/= (point) here) |
(defun c-hungry-delete-forward () |
381 |
(delete-region (point) here) |
"Delete the following character or all following whitespace |
382 |
(funcall c-delete-function 1))))) |
up to the next non-whitespace character. |
383 |
|
See also \\[c-hungry-backspace]." |
384 |
|
(interactive) |
385 |
|
(let ((here (point))) |
386 |
|
(c-skip-ws-forward) |
387 |
|
(if (/= (point) here) |
388 |
|
(delete-region (point) here) |
389 |
|
(funcall c-delete-function 1)))) |
390 |
|
|
391 |
|
;; This function is only used in XEmacs. |
392 |
(defun c-electric-delete (arg) |
(defun c-electric-delete (arg) |
393 |
"Deletes preceding or following character or whitespace. |
"Deletes preceding or following character or whitespace. |
394 |
This function either deletes forward as `c-electric-delete-forward' or |
This function either deletes forward as `c-electric-delete-forward' or |
481 |
substatement-open statement-case-open |
substatement-open statement-case-open |
482 |
extern-lang-open extern-lang-close |
extern-lang-open extern-lang-close |
483 |
namespace-open namespace-close |
namespace-open namespace-close |
484 |
|
module-open module-close |
485 |
|
composition-open composition-close |
486 |
inexpr-class-open inexpr-class-close |
inexpr-class-open inexpr-class-close |
487 |
)) |
;; `statement-cont' is here for the case with a brace |
488 |
|
;; list opener inside a statement. C.f. CASE B.2 in |
489 |
|
;; `c-guess-continued-construct'. |
490 |
|
statement-cont)) |
491 |
(insertion-point (point)) |
(insertion-point (point)) |
492 |
(preserve-p (and (not (bobp)) |
(preserve-p (and (not (bobp)) |
493 |
(eq ?\ (char-syntax (char-before))))) |
(eq ?\ (char-syntax (char-before))))) |
498 |
(when (save-excursion |
(when (save-excursion |
499 |
(skip-chars-backward " \t") |
(skip-chars-backward " \t") |
500 |
(not (bolp))) |
(not (bolp))) |
|
(setq delete-temp-newline |
|
|
(list (point-marker))) |
|
501 |
(c-newline-and-indent) |
(c-newline-and-indent) |
502 |
(setcdr delete-temp-newline (point-marker))) |
;; Set markers around the newline and indention inserted |
503 |
|
;; above. We insert the start marker here and not before |
504 |
|
;; the call to kludge around a misfeature in expand-abbrev: |
505 |
|
;; If the line contains e.g. "else" then expand-abbrev will |
506 |
|
;; be called when c-newline-and-indent inserts the newline. |
507 |
|
;; That function first removes the abbrev "else" and then |
508 |
|
;; inserts the expansion, which is an identical "else" in |
509 |
|
;; this case. So the marker that we put after "else" would |
510 |
|
;; end up before it. |
511 |
|
(setq delete-temp-newline |
512 |
|
(cons (save-excursion |
513 |
|
(c-backward-syntactic-ws) |
514 |
|
(copy-marker (point) t)) |
515 |
|
(point-marker)))) |
516 |
(unwind-protect |
(unwind-protect |
517 |
(progn |
(progn |
518 |
(if (eq last-command-char ?{) |
(if (eq last-command-char ?{) |
519 |
(setq c-state-cache (cons (point) c-state-cache))) |
(setq c-state-cache (cons (point) c-state-cache))) |
520 |
(self-insert-command (prefix-numeric-value arg)) |
(self-insert-command (prefix-numeric-value arg)) |
521 |
(let ((c-syntactic-indentation-in-macros t)) |
(c-save-buffer-state ((c-syntactic-indentation-in-macros t) |
522 |
|
(c-auto-newline-analysis t)) |
523 |
;; Turn on syntactic macro analysis to help with auto |
;; Turn on syntactic macro analysis to help with auto |
524 |
;; newlines only. |
;; newlines only. |
525 |
(setq syntax (c-guess-basic-syntax))) |
(setq syntax (c-guess-basic-syntax))) |
544 |
;; (Pike-style) brace list. |
;; (Pike-style) brace list. |
545 |
(if (and c-special-brace-lists |
(if (and c-special-brace-lists |
546 |
(save-excursion |
(save-excursion |
547 |
(c-safe (if (= (char-before) ?{) |
(c-save-buffer-state nil |
548 |
(forward-char -1) |
(c-safe (if (= (char-before) ?{) |
549 |
(c-forward-sexp -1)) |
(forward-char -1) |
550 |
(c-looking-at-special-brace-list)))) |
(c-forward-sexp -1)) |
551 |
|
(c-looking-at-special-brace-list))))) |
552 |
(setq newlines nil)) |
(setq newlines nil)) |
553 |
;; If syntax is a function symbol, then call it using the |
;; If syntax is a function symbol, then call it using the |
554 |
;; defined semantics. |
;; defined semantics. |
577 |
;; indentation, we need to recalculate syntax for |
;; indentation, we need to recalculate syntax for |
578 |
;; the current line. |
;; the current line. |
579 |
(if (/= (point) here) |
(if (/= (point) here) |
580 |
(let ((c-syntactic-indentation-in-macros t)) |
(c-save-buffer-state |
581 |
|
((c-syntactic-indentation-in-macros t) |
582 |
|
(c-auto-newline-analysis t)) |
583 |
;; Turn on syntactic macro analysis to help |
;; Turn on syntactic macro analysis to help |
584 |
;; with auto newlines only. |
;; with auto newlines only. |
585 |
(setq syntax (c-guess-basic-syntax)))))))) |
(setq syntax (c-guess-basic-syntax)))))))) |
599 |
(if (not (memq 'before newlines)) |
(if (not (memq 'before newlines)) |
600 |
;; since we're hanging the brace, we need to recalculate |
;; since we're hanging the brace, we need to recalculate |
601 |
;; syntax. |
;; syntax. |
602 |
(let ((c-syntactic-indentation-in-macros t)) |
(c-save-buffer-state ((c-syntactic-indentation-in-macros t) |
603 |
|
(c-auto-newline-analysis t)) |
604 |
;; Turn on syntactic macro analysis to help with auto |
;; Turn on syntactic macro analysis to help with auto |
605 |
;; newlines only. |
;; newlines only. |
606 |
(setq syntax (c-guess-basic-syntax)))) |
(setq syntax (c-guess-basic-syntax)))) |
652 |
(c-skip-ws-backward) |
(c-skip-ws-backward) |
653 |
(setq mbeg (point)) |
(setq mbeg (point)) |
654 |
(eq (char-before) ?\))) |
(eq (char-before) ?\))) |
655 |
(= (c-backward-token-1 1 t) 0) |
(zerop (c-save-buffer-state nil (c-backward-token-2 1 t))) |
656 |
(eq (char-after) ?\() |
(eq (char-after) ?\() |
657 |
(progn |
(progn |
658 |
(setq tmp (point)) |
(setq tmp (point)) |
679 |
(not executing-kbd-macro) |
(not executing-kbd-macro) |
680 |
old-blink-paren |
old-blink-paren |
681 |
(save-excursion |
(save-excursion |
682 |
(c-backward-syntactic-ws safepos) |
(c-save-buffer-state nil |
683 |
|
(c-backward-syntactic-ws safepos)) |
684 |
(funcall old-blink-paren))))) |
(funcall old-blink-paren))))) |
685 |
|
|
686 |
(defun c-electric-slash (arg) |
(defun c-electric-slash (arg) |
852 |
;; since that's made with c-syntactic-indentation-in-macros |
;; since that's made with c-syntactic-indentation-in-macros |
853 |
;; always set to t. |
;; always set to t. |
854 |
(indent-according-to-mode)) |
(indent-according-to-mode)) |
855 |
(let* ((c-syntactic-indentation-in-macros t) |
(c-save-buffer-state |
856 |
|
((c-syntactic-indentation-in-macros t) |
857 |
|
(c-auto-newline-analysis t) |
858 |
;; Turn on syntactic macro analysis to help with auto newlines |
;; Turn on syntactic macro analysis to help with auto newlines |
859 |
;; only. |
;; only. |
860 |
(syntax (c-guess-basic-syntax)) |
(syntax (c-guess-basic-syntax)) |
872 |
(or (c-lookup-lists '(case-label label access-label) |
(or (c-lookup-lists '(case-label label access-label) |
873 |
syntax c-hanging-colons-alist) |
syntax c-hanging-colons-alist) |
874 |
(c-lookup-lists '(member-init-intro inher-intro) |
(c-lookup-lists '(member-init-intro inher-intro) |
875 |
(let ((buffer-undo-list t)) |
(progn |
876 |
(insert ?\n) |
(insert ?\n) |
877 |
(unwind-protect |
(unwind-protect |
878 |
(c-guess-basic-syntax) |
(c-guess-basic-syntax) |
1045 |
;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com |
;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com |
1046 |
(defun c-forward-into-nomenclature (&optional arg) |
(defun c-forward-into-nomenclature (&optional arg) |
1047 |
"Move forward to end of a nomenclature section or word. |
"Move forward to end of a nomenclature section or word. |
1048 |
With arg, to it arg times." |
With arg, do it arg times." |
1049 |
(interactive "p") |
(interactive "p") |
1050 |
(let ((case-fold-search nil)) |
(let ((case-fold-search nil)) |
1051 |
(if (> arg 0) |
(if (> arg 0) |
1052 |
(re-search-forward "\\W*\\([A-Z]*[a-z0-9]*\\)" (point-max) t arg) |
(re-search-forward |
1053 |
|
(cc-eval-when-compile |
1054 |
|
(concat "\\W*\\([" c-upper "]*[" c-lower c-digit "]*\\)")) |
1055 |
|
(point-max) t arg) |
1056 |
(while (and (< arg 0) |
(while (and (< arg 0) |
1057 |
(re-search-backward |
(re-search-backward |
1058 |
"\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)" |
(cc-eval-when-compile |
1059 |
|
(concat |
1060 |
|
"\\(\\(\\W\\|[" c-lower c-digit "]\\)[" c-upper "]+" |
1061 |
|
"\\|\\W\\w+\\)")) |
1062 |
(point-min) 0)) |
(point-min) 0)) |
1063 |
(forward-char 1) |
(forward-char 1) |
1064 |
(setq arg (1+ arg))))) |
(setq arg (1+ arg))))) |
1096 |
|
|
1097 |
(if (< arg 0) |
(if (< arg 0) |
1098 |
(when (c-end-of-defun (- arg)) |
(when (c-end-of-defun (- arg)) |
1099 |
(c-forward-syntactic-ws) |
(c-save-buffer-state nil (c-forward-syntactic-ws)) |
1100 |
t) |
t) |
1101 |
|
|
1102 |
(catch 'exit |
(c-save-buffer-state (paren-state lim pos) |
1103 |
(while (> arg 0) |
(catch 'exit |
1104 |
;; Note: Partial code duplication in `c-end-of-defun' and |
(while (> arg 0) |
1105 |
;; `c-declaration-limits'. |
;; Note: Partial code duplication in `c-end-of-defun' and |
1106 |
|
;; `c-declaration-limits'. |
1107 |
|
|
1108 |
(let ((paren-state (c-parse-state)) lim pos) |
(setq paren-state (c-parse-state)) |
1109 |
(unless (c-safe |
(unless (c-safe |
1110 |
(goto-char (c-least-enclosing-brace paren-state)) |
(goto-char (c-least-enclosing-brace paren-state)) |
1111 |
;; If we moved to the outermost enclosing paren |
;; If we moved to the outermost enclosing paren |
1136 |
;; Check if the declaration contains a brace |
;; Check if the declaration contains a brace |
1137 |
;; block. If not, we try another one. |
;; block. If not, we try another one. |
1138 |
(setq pos (point)) |
(setq pos (point)) |
1139 |
(not (and (c-syntactic-re-search-forward |
(not (and (c-syntactic-re-search-forward "[;{]" nil t t) |
|
"[;{]" nil t 1 t) |
|
1140 |
(or (eq (char-before) ?{) |
(or (eq (char-before) ?{) |
1141 |
(and c-recognize-knr-p |
(and c-recognize-knr-p |
1142 |
;; Might have stopped on the |
;; Might have stopped on the |
1169 |
;; the previous declaration then we use the current point |
;; the previous declaration then we use the current point |
1170 |
;; instead. |
;; instead. |
1171 |
(while (and (/= (point) (c-point 'boi)) |
(while (and (/= (point) (c-point 'boi)) |
1172 |
(c-forward-comment -1))) |
(c-backward-single-comment))) |
1173 |
(if (/= (point) (c-point 'boi)) |
(if (/= (point) (c-point 'boi)) |
1174 |
(goto-char pos)) |
(goto-char pos)) |
1175 |
|
|
1191 |
|
|
1192 |
(if (< arg 0) |
(if (< arg 0) |
1193 |
(when (c-beginning-of-defun (- arg)) |
(when (c-beginning-of-defun (- arg)) |
1194 |
(c-backward-syntactic-ws) |
(c-save-buffer-state nil (c-backward-syntactic-ws)) |
1195 |
t) |
t) |
1196 |
|
|
1197 |
(catch 'exit |
(c-save-buffer-state (paren-state lim pos) |
1198 |
(while (> arg 0) |
(catch 'exit |
1199 |
;; Note: Partial code duplication in `c-beginning-of-defun' |
(while (> arg 0) |
1200 |
;; and `c-declaration-limits'. |
;; Note: Partial code duplication in `c-beginning-of-defun' |
1201 |
|
;; and `c-declaration-limits'. |
1202 |
|
|
1203 |
(let ((paren-state (c-parse-state)) lim pos) |
(setq paren-state (c-parse-state)) |
1204 |
(unless (c-safe |
(unless (c-safe |
1205 |
(goto-char (c-least-enclosing-brace paren-state)) |
(goto-char (c-least-enclosing-brace paren-state)) |
1206 |
;; If we moved to the outermost enclosing paren |
;; If we moved to the outermost enclosing paren |
1227 |
;; in a `c-opt-block-decls-with-vars-key' declaration, but |
;; in a `c-opt-block-decls-with-vars-key' declaration, but |
1228 |
;; then we won't move significantly far here. |
;; then we won't move significantly far here. |
1229 |
(goto-char pos) |
(goto-char pos) |
1230 |
(c-forward-token-1 0)) |
(c-forward-token-2 0)) |
1231 |
|
|
1232 |
(while (let ((start (point))) |
(while (let ((start (point))) |
1233 |
(c-end-of-decl-1) |
(c-end-of-decl-1) |
1245 |
;; block. If not, we try another one. |
;; block. If not, we try another one. |
1246 |
(setq pos (point)) |
(setq pos (point)) |
1247 |
(goto-char start) |
(goto-char start) |
1248 |
(not (c-syntactic-re-search-forward "{" pos t 1 t)))))) |
(not (c-syntactic-re-search-forward "{" pos t t)))))) |
1249 |
|
|
1250 |
(setq pos (point)) |
(setq pos (point)) |
1251 |
;; Try to be line oriented; position point after the next |
;; Try to be line oriented; position point after the next |
1253 |
;; next declaration then we use the current point instead. |
;; next declaration then we use the current point instead. |
1254 |
(while (and (not (bolp)) |
(while (and (not (bolp)) |
1255 |
(not (looking-at "\\s *$")) |
(not (looking-at "\\s *$")) |
1256 |
(c-forward-comment 1))) |
(c-forward-single-comment))) |
1257 |
(cond ((bolp)) |
(cond ((bolp)) |
1258 |
((looking-at "\\s *$") |
((looking-at "\\s *$") |
1259 |
(forward-line 1)) |
(forward-line 1)) |
1311 |
;; declaration, but then we won't move significantly far |
;; declaration, but then we won't move significantly far |
1312 |
;; here. |
;; here. |
1313 |
(goto-char pos) |
(goto-char pos) |
1314 |
(while (c-forward-comment 10)) |
(c-forward-comments) |
1315 |
|
|
1316 |
(when (and near (c-beginning-of-macro)) |
(when (and near (c-beginning-of-macro)) |
1317 |
(throw 'exit |
(throw 'exit |
1351 |
(cons (progn |
(cons (progn |
1352 |
(setq pos (point)) |
(setq pos (point)) |
1353 |
(while (and (/= (point) (c-point 'boi)) |
(while (and (/= (point) (c-point 'boi)) |
1354 |
(c-forward-comment -1))) |
(c-backward-single-comment))) |
1355 |
(if (/= (point) (c-point 'boi)) |
(if (/= (point) (c-point 'boi)) |
1356 |
pos |
pos |
1357 |
(point))) |
(point))) |
1362 |
(setq pos (point)) |
(setq pos (point)) |
1363 |
(while (and (not (bolp)) |
(while (and (not (bolp)) |
1364 |
(not (looking-at "\\s *$")) |
(not (looking-at "\\s *$")) |
1365 |
(c-forward-comment 1))) |
(c-forward-single-comment))) |
1366 |
(cond ((bolp) |
(cond ((bolp) |
1367 |
(point)) |
(point)) |
1368 |
((looking-at "\\s *$") |
((looking-at "\\s *$") |
1380 |
function does not require the declaration to contain a brace block." |
function does not require the declaration to contain a brace block." |
1381 |
(interactive) |
(interactive) |
1382 |
|
|
1383 |
;; We try to be line oriented, unless there are several |
(let (decl-limits) |
1384 |
;; declarations on the same line. |
(c-save-buffer-state nil |
1385 |
(if (looking-at c-syntactic-eol) |
;; We try to be line oriented, unless there are several |
1386 |
(c-backward-token-1 1 nil (c-point 'bol))) |
;; declarations on the same line. |
1387 |
|
(if (looking-at c-syntactic-eol) |
1388 |
|
(c-backward-token-2 1 nil (c-point 'bol))) |
1389 |
|
(setq decl-limits (c-declaration-limits t))) |
1390 |
|
|
|
(let ((decl-limits (c-declaration-limits t))) |
|
1391 |
(if (not decl-limits) |
(if (not decl-limits) |
1392 |
(error "Cannot find any declaration") |
(error "Cannot find any declaration") |
1393 |
(goto-char (car decl-limits)) |
(goto-char (car decl-limits)) |
1413 |
more \"DWIM:ey\"." |
more \"DWIM:ey\"." |
1414 |
(interactive (list (prefix-numeric-value current-prefix-arg) |
(interactive (list (prefix-numeric-value current-prefix-arg) |
1415 |
nil t)) |
nil t)) |
1416 |
(let* ((count (or count 1)) |
(c-save-buffer-state |
1417 |
here |
((count (or count 1)) |
1418 |
(range (c-collect-line-comments (c-literal-limits lim)))) |
here |
1419 |
|
(range (c-collect-line-comments (c-literal-limits lim)))) |
1420 |
(while (and (/= count 0) |
(while (and (/= count 0) |
1421 |
(or (not lim) (> (point) lim))) |
(or (not lim) (> (point) lim))) |
1422 |
(setq here (point)) |
(setq here (point)) |
1424 |
(save-excursion |
(save-excursion |
1425 |
;; Find the comment next to point if we're not in one. |
;; Find the comment next to point if we're not in one. |
1426 |
(if (> count 0) |
(if (> count 0) |
1427 |
(if (c-forward-comment-lc -1) |
(if (c-backward-single-comment) |
1428 |
(setq range (cons (point) |
(setq range (cons (point) |
1429 |
(progn (c-forward-comment-lc 1) |
(progn (c-forward-single-comment) |
1430 |
(point)))) |
(point)))) |
1431 |
(c-skip-ws-backward) |
(c-skip-ws-backward) |
1432 |
(setq range (point)) |
(setq range (point)) |
1441 |
(c-forward-sexp 1) |
(c-forward-sexp 1) |
1442 |
(point)))) |
(point)))) |
1443 |
(setq range (point)) |
(setq range (point)) |
1444 |
(setq range (if (c-forward-comment-lc 1) |
(setq range (if (c-forward-single-comment) |
1445 |
(cons range (point)) |
(cons range (point)) |
1446 |
nil)))) |
nil)))) |
1447 |
(setq range (c-collect-line-comments range)))) |
(setq range (c-collect-line-comments range)))) |
1569 |
;; into parens. Also stop before `#' when it's at boi |
;; into parens. Also stop before `#' when it's at boi |
1570 |
;; on a line. |
;; on a line. |
1571 |
(let ((literal-pos (not sentence-flag)) |
(let ((literal-pos (not sentence-flag)) |
|
(large-enough (- (point-max))) |
|
1572 |
last last-below-line) |
last last-below-line) |
1573 |
(catch 'done |
(catch 'done |
1574 |
(while t |
(while t |
1591 |
;; style comment. /mast |
;; style comment. /mast |
1592 |
;;(c-skip-ws-backward) |
;;(c-skip-ws-backward) |
1593 |
(if literal-pos |
(if literal-pos |
1594 |
(c-forward-comment-lc large-enough) |
(c-backward-comments) |
1595 |
(when (c-forward-comment-lc -1) |
(when (c-backward-single-comment) |
1596 |
;; Record position of first comment. |
;; Record position of first comment. |
1597 |
(save-excursion |
(save-excursion |
1598 |
(c-forward-comment-lc 1) |
(c-forward-single-comment) |
1599 |
(setq literal-pos (point))) |
(setq literal-pos (point))) |
1600 |
(c-forward-comment-lc large-enough))) |
(c-backward-comments))) |
1601 |
(unless last-below-line |
(unless last-below-line |
1602 |
(if (save-excursion |
(if (save-excursion |
1603 |
(re-search-forward "\\(^\\|[^\\]\\)$" last t)) |
(re-search-forward "\\(^\\|[^\\]\\)$" last t)) |
1649 |
;; and move into parens. Also stop at eol of lines |
;; and move into parens. Also stop at eol of lines |
1650 |
;; with `#' at the boi. |
;; with `#' at the boi. |
1651 |
(let ((literal-pos (not sentence-flag)) |
(let ((literal-pos (not sentence-flag)) |
|
(large-enough (point-max)) |
|
1652 |
last) |
last) |
1653 |
(catch 'done |
(catch 'done |
1654 |
(while t |
(while t |
1655 |
(setq last (point)) |
(setq last (point)) |
1656 |
(if literal-pos |
(if literal-pos |
1657 |
(c-forward-comment-lc large-enough) |
(c-forward-comments) |
1658 |
(if (progn |
(if (progn |
1659 |
(c-skip-ws-forward) |
(c-skip-ws-forward) |
1660 |
;; Record position of first comment. |
;; Record position of first comment. |
1661 |
(setq literal-pos (point)) |
(setq literal-pos (point)) |
1662 |
(c-forward-comment-lc 1)) |
(c-forward-single-comment)) |
1663 |
(c-forward-comment-lc large-enough) |
(c-forward-comments) |
1664 |
(setq literal-pos nil))) |
(setq literal-pos nil))) |
1665 |
(cond ((and (eq (char-after) ?{) |
(cond ((and (eq (char-after) ?{) |
1666 |
(not (and c-special-brace-lists |
(not (and c-special-brace-lists |
1794 |
;; Recurse to handle value as a new spec. |
;; Recurse to handle value as a new spec. |
1795 |
(c-calc-comment-indent (cdr entry))))))) |
(c-calc-comment-indent (cdr entry))))))) |
1796 |
|
|
|
;; To avoid warning about assignment without reference wrt |
|
|
;; c-add-syntax below. |
|
|
(cc-bytecomp-defvar syntactic-relpos) |
|
|
|
|
1797 |
(defun c-comment-indent () |
(defun c-comment-indent () |
1798 |
"Used by `indent-for-comment' to create and indent comments. |
"Used by `indent-for-comment' to create and indent comments. |
1799 |
See `c-indent-comment-alist' for a description." |
See `c-indent-comment-alist' for a description." |
1800 |
(save-excursion |
(save-excursion |
1801 |
(end-of-line) |
(end-of-line) |
1802 |
(let* ((eot (let ((lim (c-literal-limits (c-point 'bol) t))) |
(c-save-buffer-state |
1803 |
|
((eot (let ((lim (c-literal-limits (c-point 'bol) t))) |
1804 |
(or (when (consp lim) |
(or (when (consp lim) |
1805 |
(goto-char (car lim)) |
(goto-char (car lim)) |
1806 |
(when (looking-at "/[/*]") |
(when (looking-at "/[/*]") |
1826 |
'other)))) |
'other)))) |
1827 |
(if (and (memq line-type '(anchored-comment empty-line)) |
(if (and (memq line-type '(anchored-comment empty-line)) |
1828 |
c-indent-comments-syntactically-p) |
c-indent-comments-syntactically-p) |
1829 |
(let ((syntax (c-guess-basic-syntax)) |
(let ((c-syntactic-context (c-guess-basic-syntax))) |
|
syntactic-relpos) |
|
1830 |
;; BOGOSITY ALERT: if we're looking at the eol, its |
;; BOGOSITY ALERT: if we're looking at the eol, its |
1831 |
;; because indent-for-comment hasn't put the comment-start |
;; because indent-for-comment hasn't put the comment-start |
1832 |
;; in the buffer yet. this will screw up the syntactic |
;; in the buffer yet. this will screw up the syntactic |
1841 |
c-comment-only-line-offset |
c-comment-only-line-offset |
1842 |
(cons c-comment-only-line-offset |
(cons c-comment-only-line-offset |
1843 |
c-comment-only-line-offset)))) |
c-comment-only-line-offset)))) |
1844 |
(c-get-syntactic-indentation syntax))) |
(c-get-syntactic-indentation c-syntactic-context))) |
1845 |
(goto-char eot) |
(goto-char eot) |
1846 |
(c-calc-comment-indent line-type))))) |
(c-calc-comment-indent line-type))))) |
1847 |
|
|
1865 |
(interactive "p") |
(interactive "p") |
1866 |
(c-forward-conditional (- count) -1) |
(c-forward-conditional (- count) -1) |
1867 |
(c-keep-region-active)) |
(c-keep-region-active)) |
1868 |
|
|
1869 |
(defun c-up-conditional-with-else (count) |
(defun c-up-conditional-with-else (count) |
1870 |
"Move back to the containing preprocessor conditional, including `#else'. |
"Move back to the containing preprocessor conditional, including `#else'. |
1871 |
Just like `c-up-conditional', except it also stops at `#else' |
Just like `c-up-conditional', except it also stops at `#else' |
2125 |
continuation backslashes, unless `c-auto-align-backslashes' is nil." |
continuation backslashes, unless `c-auto-align-backslashes' is nil." |
2126 |
(interactive "*") |
(interactive "*") |
2127 |
(let ((here (point-marker)) decl-limits) |
(let ((here (point-marker)) decl-limits) |
|
;; We try to be line oriented, unless there are several |
|
|
;; declarations on the same line. |
|
|
(if (looking-at c-syntactic-eol) |
|
|
(c-backward-token-1 1 nil (c-point 'bol)) |
|
|
(c-forward-token-1 0 nil (c-point 'eol))) |
|
2128 |
(unwind-protect |
(unwind-protect |
2129 |
(if (setq decl-limits (c-declaration-limits nil)) |
(progn |
2130 |
(c-indent-region (car decl-limits) |
(c-save-buffer-state nil |
2131 |
(cdr decl-limits))) |
;; We try to be line oriented, unless there are several |
2132 |
|
;; declarations on the same line. |
2133 |
|
(if (looking-at c-syntactic-eol) |
2134 |
|
(c-backward-token-2 1 nil (c-point 'bol)) |
2135 |
|
(c-forward-token-2 0 nil (c-point 'eol))) |
2136 |
|
(setq decl-limits (c-declaration-limits nil))) |
2137 |
|
(if decl-limits |
2138 |
|
(c-indent-region (car decl-limits) |
2139 |
|
(cdr decl-limits)))) |
2140 |
(goto-char here) |
(goto-char here) |
2141 |
(set-marker here nil)))) |
(set-marker here nil)))) |
2142 |
|
|
2147 |
non-nil." |
non-nil." |
2148 |
(save-excursion |
(save-excursion |
2149 |
(goto-char end) |
(goto-char end) |
2150 |
(skip-chars-backward " \t\n\r") |
(skip-chars-backward " \t\n\r\f\v") |
2151 |
(setq end (point)) |
(setq end (point)) |
2152 |
(goto-char start) |
(goto-char start) |
2153 |
;; Advance to first nonblank line. |
;; Advance to first nonblank line. |
2154 |
(beginning-of-line) |
(beginning-of-line) |
2155 |
(skip-chars-forward " \t\n\r") |
(skip-chars-forward " \t\n\r\f\v") |
2156 |
(setq start (point)) |
(setq start (point)) |
2157 |
(beginning-of-line) |
(beginning-of-line) |
2158 |
(setq c-parsing-error |
(setq c-parsing-error |
2177 |
(skip-chars-forward " \t\n") |
(skip-chars-forward " \t\n") |
2178 |
(beginning-of-line) |
(beginning-of-line) |
2179 |
;; Get syntax and indent. |
;; Get syntax and indent. |
2180 |
(setq syntax (c-guess-basic-syntax)) |
(c-save-buffer-state nil |
2181 |
|
(setq syntax (c-guess-basic-syntax))) |
2182 |
(if (and c-auto-align-backslashes |
(if (and c-auto-align-backslashes |
2183 |
(assq 'cpp-macro syntax)) |
(assq 'cpp-macro syntax)) |
2184 |
;; Record macro start. |
;; Record macro start. |
2207 |
(defun c-fn-region-is-active-p () |
(defun c-fn-region-is-active-p () |
2208 |
;; Function version of the macro for use in places that aren't |
;; Function version of the macro for use in places that aren't |
2209 |
;; compiled, e.g. in the menus. |
;; compiled, e.g. in the menus. |
2210 |
|
;; |
2211 |
|
;; This function does not do any hidden buffer changes. |
2212 |
(c-region-is-active-p)) |
(c-region-is-active-p)) |
2213 |
|
|
2214 |
(defun c-indent-line-or-region () |
(defun c-indent-line-or-region () |
2225 |
(defvar c-progress-info nil) |
(defvar c-progress-info nil) |
2226 |
|
|
2227 |
(defun c-progress-init (start end context) |
(defun c-progress-init (start end context) |
2228 |
|
;; This function does not do any hidden buffer changes. |
2229 |
(cond |
(cond |
2230 |
;; Be silent |
;; Be silent |
2231 |
((not c-progress-interval)) |
((not c-progress-interval)) |
2247 |
)) |
)) |
2248 |
|
|
2249 |
(defun c-progress-update () |
(defun c-progress-update () |
2250 |
;; update progress |
;; This function does not do any hidden buffer changes. |
2251 |
(if (not (and c-progress-info c-progress-interval)) |
(if (not (and c-progress-info c-progress-interval)) |
2252 |
nil |
nil |
2253 |
(let ((now (nth 1 (current-time))) |
(let ((now (nth 1 (current-time))) |
2264 |
))) |
))) |
2265 |
|
|
2266 |
(defun c-progress-fini (context) |
(defun c-progress-fini (context) |
2267 |
;; finished |
;; This function does not do any hidden buffer changes. |
2268 |
(if (not c-progress-interval) |
(if (not c-progress-interval) |
2269 |
nil |
nil |
2270 |
(if (or (eq context (aref c-progress-info 3)) |
(if (or (eq context (aref c-progress-info 3)) |
2425 |
(set-marker point-pos nil)))) |
(set-marker point-pos nil)))) |
2426 |
|
|
2427 |
(defun c-append-backslashes-forward (to-mark column point-pos) |
(defun c-append-backslashes-forward (to-mark column point-pos) |
2428 |
|
;; This function does not do any hidden buffer changes. |
2429 |
(let ((state (parse-partial-sexp (c-point 'bol) (point)))) |
(let ((state (parse-partial-sexp (c-point 'bol) (point)))) |
2430 |
(if column |
(if column |
2431 |
(while |
(while |
2497 |
(= (forward-line 1) 0))))))) |
(= (forward-line 1) 0))))))) |
2498 |
|
|
2499 |
(defun c-delete-backslashes-forward (to-mark point-pos) |
(defun c-delete-backslashes-forward (to-mark point-pos) |
2500 |
|
;; This function does not do any hidden buffer changes. |
2501 |
(while |
(while |
2502 |
(and (<= (point) to-mark) |
(and (<= (point) to-mark) |
2503 |
(progn |
(progn |
2541 |
;; comment. Return a cons of the prefix string and the column where |
;; comment. Return a cons of the prefix string and the column where |
2542 |
;; it ends. If fill-prefix is set, it'll override. Note that this |
;; it ends. If fill-prefix is set, it'll override. Note that this |
2543 |
;; function also uses the value of point in some heuristics. |
;; function also uses the value of point in some heuristics. |
2544 |
|
|
2545 |
(let* ((here (point)) |
(let* ((here (point)) |
2546 |
(prefix-regexp (concat "[ \t]*\\(" |
(prefix-regexp (concat "[ \t]*\\(" |
2547 |
c-current-comment-prefix |
c-current-comment-prefix |
2550 |
prefix-regexp |
prefix-regexp |
2551 |
comment-start-skip)) |
comment-start-skip)) |
2552 |
prefix-line comment-prefix res comment-text-end) |
prefix-line comment-prefix res comment-text-end) |
2553 |
|
|
2554 |
(cond |
(cond |
2555 |
(fill-prefix |
(fill-prefix |
2556 |
(setq res (cons fill-prefix |
(setq res (cons fill-prefix |
2557 |
;; Ugly way of getting the column after the fill |
;; Ugly way of getting the column after the fill |
2558 |
;; prefix; it'd be nice with a current-column |
;; prefix; it'd be nice with a current-column |
2559 |
;; that works on strings.. |
;; that works on strings.. |
2560 |
(let ((buffer-modified (buffer-modified-p)) |
(let ((start (point))) |
|
(buffer-undo-list t) |
|
|
(start (point))) |
|
2561 |
(unwind-protect |
(unwind-protect |
2562 |
(progn |
(progn |
2563 |
(insert-and-inherit "\n" fill-prefix) |
(insert-and-inherit "\n" fill-prefix) |
2564 |
(current-column)) |
(current-column)) |
2565 |
(delete-region start (point)) |
(delete-region start (point))))))) |
2566 |
(set-buffer-modified-p buffer-modified)))))) |
|
2567 |
((eq lit-type 'c++) |
((eq lit-type 'c++) |
2568 |
(save-excursion |
(save-excursion |
2569 |
;; Set fallback for comment-prefix if none is found. |
;; Set fallback for comment-prefix if none is found. |
2570 |
(setq comment-prefix "// " |
(setq comment-prefix "// " |
2571 |
comment-text-end (cdr lit-limits)) |
comment-text-end (cdr lit-limits)) |
2572 |
|
|
2573 |
(beginning-of-line) |
(beginning-of-line) |
2574 |
(if (> (point) (car lit-limits)) |
(if (> (point) (car lit-limits)) |
2575 |
;; The current line is not the comment starter, so the |
;; The current line is not the comment starter, so the |
2576 |
;; comment has more than one line, and it can therefore be |
;; comment has more than one line, and it can therefore be |
2577 |
;; used to find the comment fill prefix. |
;; used to find the comment fill prefix. |
2578 |
(setq prefix-line (point)) |
(setq prefix-line (point)) |
2579 |
|
|
2580 |
(goto-char (car lit-limits)) |
(goto-char (car lit-limits)) |
2581 |
(if (and (= (forward-line 1) 0) |
(if (and (= (forward-line 1) 0) |
2582 |
(< (point) (cdr lit-limits))) |
(< (point) (cdr lit-limits))) |
2583 |
;; The line after the comment starter is inside the |
;; The line after the comment starter is inside the |
2584 |
;; comment, so we can use it. |
;; comment, so we can use it. |
2585 |
(setq prefix-line (point)) |
(setq prefix-line (point)) |
2586 |
|
|
2587 |
;; The comment is only one line. Take the comment prefix |
;; The comment is only one line. Take the comment prefix |
2588 |
;; from it and keep the indentation. |
;; from it and keep the indentation. |
2589 |
(goto-char (car lit-limits)) |
(goto-char (car lit-limits)) |
2591 |
(goto-char (match-end 0)) |
(goto-char (match-end 0)) |
2592 |
(forward-char 2) |
(forward-char 2) |
2593 |
(skip-chars-forward " \t")) |
(skip-chars-forward " \t")) |
2594 |
|
|
2595 |
(let (str col) |
(let (str col) |
2596 |
(if (eq (c-point 'boi) (car lit-limits)) |
(if (eq (c-point 'boi) (car lit-limits)) |
2597 |
;; There is only whitespace before the comment |
;; There is only whitespace before the comment |
2599 |
(setq str (buffer-substring-no-properties |
(setq str (buffer-substring-no-properties |
2600 |
(c-point 'bol) (point)) |
(c-point 'bol) (point)) |
2601 |
col (current-column)) |
col (current-column)) |
2602 |
|
|
2603 |
;; There is code before the comment starter, so we |
;; There is code before the comment starter, so we |
2604 |
;; have to temporarily insert and indent a new line to |
;; have to temporarily insert and indent a new line to |
2605 |
;; get the right space/tab mix in the indentation. |
;; get the right space/tab mix in the indentation. |
2606 |
(let ((buffer-modified (buffer-modified-p)) |
(let ((prefix-len (- (point) (car lit-limits))) |
|
(buffer-undo-list t) |
|
|
(prefix-len (- (point) (car lit-limits))) |
|
2607 |
tmp) |
tmp) |
2608 |
(unwind-protect |
(unwind-protect |
2609 |
(progn |
(progn |
2615 |
(setq str (buffer-substring-no-properties |
(setq str (buffer-substring-no-properties |
2616 |
(c-point 'bol) (point)) |
(c-point 'bol) (point)) |
2617 |
col (current-column))) |
col (current-column))) |
2618 |
(delete-region (car lit-limits) tmp) |
(delete-region (car lit-limits) tmp)))) |
2619 |
(set-buffer-modified-p buffer-modified)))) |
|
2620 |
(setq res |
(setq res |
2621 |
(if (or (string-match "\\s \\'" str) (not (eolp))) |
(if (or (string-match "\\s \\'" str) (not (eolp))) |
2622 |
(cons str col) |
(cons str col) |
2624 |
;; after it. Default to a single space. |
;; after it. Default to a single space. |
2625 |
(cons (concat str " ") (1+ col)))) |
(cons (concat str " ") (1+ col)))) |
2626 |
))))) |
))))) |
2627 |
|
|
2628 |
(t |
(t |
2629 |
(setq comment-text-end |
(setq comment-text-end |
2630 |
(save-excursion |
(save-excursion |
2631 |
(goto-char (- (cdr lit-limits) 2)) |
(goto-char (- (cdr lit-limits) 2)) |
2632 |
(if (looking-at "\\*/") (point) (cdr lit-limits)))) |
(if (looking-at "\\*/") (point) (cdr lit-limits)))) |
2633 |
|
|
2634 |
(save-excursion |
(save-excursion |
2635 |
(beginning-of-line) |
(beginning-of-line) |
2636 |
(if (and (> (point) (car lit-limits)) |
(if (and (> (point) (car lit-limits)) |
2641 |
;; to be used for the comment fill prefix. |
;; to be used for the comment fill prefix. |
2642 |
(setq prefix-line (point)) |
(setq prefix-line (point)) |
2643 |
(goto-char (car lit-limits)) |
(goto-char (car lit-limits)) |
2644 |
(if (or (/= (forward-line 1) 0) |
|
2645 |
(>= (point) (cdr lit-limits)) |
(cond ((or (/= (forward-line 1) 0) |
2646 |
(and (looking-at "[ \t]*\\*/") |
(>= (point) (cdr lit-limits)) |
2647 |
(eq (cdr lit-limits) (match-end 0))) |
(and (looking-at "[ \t]*\\*/") |
2648 |
(and (looking-at prefix-regexp) |
(eq (cdr lit-limits) (match-end 0))) |
2649 |
(<= (1- (cdr lit-limits)) (match-end 0))) |
(and (looking-at prefix-regexp) |
2650 |
(and (< here (point)) |
(<= (1- (cdr lit-limits)) (match-end 0)))) |
2651 |
(or (not (match-beginning 0)) |
;; The comment is either one line or the next line contains |
2652 |
(looking-at "[ \t]*\\\\?$")))) |
;; just the comment ender. In this case we have no |
2653 |
;; The comment is either one line or the next line |
;; information about a suitable comment prefix, so we resort |
2654 |
;; contains just the comment ender. Also, if point is |
;; to c-block-comment-prefix. |
2655 |
;; on the comment opener line and the following line is |
(setq comment-prefix (or c-block-comment-prefix ""))) |
2656 |
;; empty or doesn't match c-current-comment-prefix we |
|
2657 |
;; assume that this is in fact a not yet closed one line |
((< here (point)) |
2658 |
;; comment, so we shouldn't look for the comment prefix |
;; The point was on the comment opener line, so we might want |
2659 |
;; on the next line. In these cases we have no |
;; to treat this as a not yet closed comment. |
2660 |
;; information about a suitable comment prefix, so we |
|
2661 |
;; resort to c-block-comment-prefix. |
(if (and (match-beginning 1) |
2662 |
(setq comment-prefix (or c-block-comment-prefix "") |
(/= (match-beginning 1) (match-end 1))) |
2663 |
res (let ((buffer-modified (buffer-modified-p)) |
;; Above `prefix-regexp' matched a nonempty prefix on the |
2664 |
(buffer-undo-list t) |
;; second line, so let's use it. Normally it should do |
2665 |
tmp-pre tmp-post) |
;; to set `prefix-line' and let the code below pick up |
2666 |
;; The comment doesn't give any information |
;; the whole prefix, but if there's no text after the |
2667 |
;; about the indentation column. We'll have to |
;; match then it will probably fall back to no prefix at |
2668 |
;; temporarily insert a new comment line and |
;; all if the comment isn't closed yet, so in that case |
2669 |
;; indent it to find the correct column. |
;; it's better to force use of the prefix matched now. |
2670 |
(unwind-protect |
(if (= (match-end 0) (c-point 'eol)) |
2671 |
(progn |
(setq comment-prefix (match-string 1)) |
2672 |
(goto-char (car lit-limits)) |
(setq prefix-line (point))) |
2673 |
(if (looking-at comment-start-regexp) |
|
2674 |
(goto-char (min (match-end 0) |
;; There's no nonempty prefix on the line after the |
2675 |
comment-text-end)) |
;; comment opener. If the line is empty, or if the |
2676 |
(forward-char 2) |
;; text on it has less or equal indentation than the |
2677 |
(skip-chars-forward " \t")) |
;; comment starter we assume it's an unclosed |
2678 |
(when (eq (char-syntax (char-before)) ?\ ) |
;; comment starter, i.e. that |
2679 |
;; If there's ws on the current |
;; `c-block-comment-prefix' should be used. |
2680 |
;; line, we'll use it instead of |
;; Otherwise we assume it's a closed comment where |
2681 |
;; what's ending comment-prefix. |
;; the prefix really is the empty string. |
2682 |
(setq comment-prefix |
;; E.g. this is an unclosed comment: |
2683 |
(concat (substring comment-prefix |
;; |
2684 |
0 (string-match |
;; /* |
2685 |
"\\s *\\'" |
;; foo |
2686 |
comment-prefix)) |
;; |
2687 |
(buffer-substring-no-properties |
;; But this is not: |
2688 |
(save-excursion |
;; |
2689 |
(skip-chars-backward " \t") |
;; /* |
2690 |
(point)) |
;; foo |
2691 |
(point))))) |
;; */ |
2692 |
(setq tmp-pre (point-marker)) |
;; |
2693 |
;; We insert an extra non-whitespace |
;; (Looking for the presence of the comment closer |
2694 |
;; character before the line break and |
;; rarely works since it's probably the closer of |
2695 |
;; after comment-prefix in case it's |
;; some comment further down when the comment |
2696 |
;; "" or ends with whitespace. |
;; really is unclosed.) |
2697 |
(insert-and-inherit "x\n" comment-prefix "x") |
(if (<= (save-excursion (back-to-indentation) |
2698 |
(setq tmp-post (point-marker)) |
(current-column)) |
2699 |
(indent-according-to-mode) |
(save-excursion (goto-char (car lit-limits)) |
2700 |
(goto-char (1- tmp-post)) |
(current-column))) |
2701 |
(cons (buffer-substring-no-properties |
(setq comment-prefix (or c-block-comment-prefix "")) |
2702 |
(c-point 'bol) (point)) |
(setq prefix-line (point))))) |
2703 |
(current-column))) |
|
2704 |
(when tmp-post |
(t |
2705 |
(delete-region tmp-pre tmp-post) |
;; Otherwise the line after the comment starter is good |
2706 |
(set-marker tmp-pre nil) |
;; enough to find the prefix in. |
2707 |
(set-marker tmp-post nil)) |
(setq prefix-line (point)))) |
2708 |
(set-buffer-modified-p buffer-modified)))) |
|
2709 |
;; Otherwise the line after the comment starter is good |
(when comment-prefix |
2710 |
;; enough to find the prefix in. |
;; Haven't got the comment prefix on any real line that we |
2711 |
(setq prefix-line (point))))))) |
;; can take it from, so we have to temporarily insert |
2712 |
(or res |
;; `comment-prefix' on a line and indent it to find the |
2713 |
|
;; correct column and the correct mix of tabs and spaces. |
2714 |
|
(setq res |
2715 |
|
(let (tmp-pre tmp-post) |
2716 |
|
(unwind-protect |
2717 |
|
(progn |
2718 |
|
|
2719 |
|
(goto-char (car lit-limits)) |
2720 |
|
(if (looking-at comment-start-regexp) |
2721 |
|
(goto-char (min (match-end 0) |
2722 |
|
comment-text-end)) |
2723 |
|
(forward-char 2) |
2724 |
|
(skip-chars-forward " \t")) |
2725 |
|
|
2726 |
|
(when (eq (char-syntax (char-before)) ?\ ) |
2727 |
|
;; If there's ws on the current line, we'll use it |
2728 |
|
;; instead of what's ending comment-prefix. |
2729 |
|
(setq comment-prefix |
2730 |
|
(concat (substring comment-prefix |
2731 |
|
0 (string-match |
2732 |
|
"\\s *\\'" |
2733 |
|
comment-prefix)) |
2734 |
|
(buffer-substring-no-properties |
2735 |
|
(save-excursion |
2736 |
|
(skip-chars-backward " \t") |
2737 |
|
(point)) |
2738 |
|
(point))))) |
2739 |
|
|
2740 |
|
(setq tmp-pre (point-marker)) |
2741 |
|
|
2742 |
|
;; We insert an extra non-whitespace character |
2743 |
|
;; before the line break and after comment-prefix in |
2744 |
|
;; case it's "" or ends with whitespace. |
2745 |
|
(insert-and-inherit "x\n" comment-prefix "x") |
2746 |
|
(setq tmp-post (point-marker)) |
2747 |
|
|
2748 |
|
(indent-according-to-mode) |
2749 |
|
|
2750 |
|
(goto-char (1- tmp-post)) |
2751 |
|
(cons (buffer-substring-no-properties |
2752 |
|
(c-point 'bol) (point)) |
2753 |
|
(current-column))) |
2754 |
|
|
2755 |
|
(when tmp-post |
2756 |
|
(delete-region tmp-pre tmp-post) |
2757 |
|
(set-marker tmp-pre nil) |
2758 |
|
(set-marker tmp-post nil)))))))))) |
2759 |
|
|
2760 |
|
(or res ; Found a good prefix above. |
2761 |
|
|
2762 |
(save-excursion |
(save-excursion |
2763 |
;; prefix-line is the bol of a line on which we should try |
;; prefix-line is the bol of a line on which we should try |
2764 |
;; to find the prefix. |
;; to find the prefix. |
2781 |
(match-beginning 0) (match-end 0)) |
(match-beginning 0) (match-end 0)) |
2782 |
fb-endpos (match-end 0))) |
fb-endpos (match-end 0))) |
2783 |
t)))) |
t)))) |
2784 |
|
|
2785 |
(or (catch 'found |
(or (catch 'found |
2786 |
;; Search for a line which has text after the prefix |
;; Search for a line which has text after the prefix |
2787 |
;; so that we get the proper amount of whitespace |
;; so that we get the proper amount of whitespace |
2788 |
;; after it. We start with the current line, then |
;; after it. We start with the current line, then |
2789 |
;; search backwards, then forwards. |
;; search backwards, then forwards. |
2790 |
|
|
2791 |
(goto-char prefix-line) |
(goto-char prefix-line) |
2792 |
(when (and (funcall test-line) |
(when (and (funcall test-line) |
2793 |
(or (/= (match-end 1) (match-end 0)) |
(or (/= (match-end 1) (match-end 0)) |
2800 |
(throw 'found (cons fb-string |
(throw 'found (cons fb-string |
2801 |
(progn (goto-char fb-endpos) |
(progn (goto-char fb-endpos) |
2802 |
(current-column))))) |
(current-column))))) |
2803 |
|
|
2804 |
(if (eq lit-type 'c++) |
(if (eq lit-type 'c++) |
2805 |
;; For line comments we can search up to and |
;; For line comments we can search up to and |
2806 |
;; including the first line. |
;; including the first line. |
2812 |
(while (and (zerop (forward-line -1)) |
(while (and (zerop (forward-line -1)) |
2813 |
(> (point) (car lit-limits))) |
(> (point) (car lit-limits))) |
2814 |
(funcall test-line))) |
(funcall test-line))) |
2815 |
|
|
2816 |
(goto-char prefix-line) |
(goto-char prefix-line) |
2817 |
(while (and (zerop (forward-line 1)) |
(while (and (zerop (forward-line 1)) |
2818 |
(< (point) (cdr lit-limits))) |
(< (point) (cdr lit-limits))) |
2819 |
(funcall test-line)) |
(funcall test-line)) |
2820 |
|
|
2821 |
(goto-char prefix-line) |
(goto-char prefix-line) |
2822 |
nil) |
nil) |
2823 |
|
|
2824 |
(when fb-string |
(when fb-string |
2825 |
;; A good line wasn't found, but at least we have a |
;; A good line wasn't found, but at least we have a |
2826 |
;; fallback that matches the comment prefix regexp. |
;; fallback that matches the comment prefix regexp. |
2831 |
;; There are ws or text after the prefix, so |
;; There are ws or text after the prefix, so |
2832 |
;; let's use it. |
;; let's use it. |
2833 |
(cons fb-string (current-column))) |
(cons fb-string (current-column))) |
2834 |
|
|
2835 |
((progn |
((progn |
2836 |
;; Check if there's any whitespace padding |
;; Check if there's any whitespace padding |
2837 |
;; on the comment start line that we can |
;; on the comment start line that we can |
2843 |
(skip-chars-forward " \t")) |
(skip-chars-forward " \t")) |
2844 |
(or (not (eolp)) |
(or (not (eolp)) |
2845 |
(eq (char-syntax (char-before)) ?\ ))) |
(eq (char-syntax (char-before)) ?\ ))) |
2846 |
|
|
2847 |
(setq fb-string (buffer-substring-no-properties |
(setq fb-string (buffer-substring-no-properties |
2848 |
(save-excursion |
(save-excursion |
2849 |
(skip-chars-backward " \t") |
(skip-chars-backward " \t") |
2851 |
(point))) |
(point))) |
2852 |
(goto-char fb-endpos) |
(goto-char fb-endpos) |
2853 |
(skip-chars-backward " \t") |
(skip-chars-backward " \t") |
2854 |
(let ((buffer-modified (buffer-modified-p)) |
|
2855 |
(buffer-undo-list t) |
(let ((tmp (point))) |
|
(tmp (point))) |
|
2856 |
;; Got to mess in the buffer once again to |
;; Got to mess in the buffer once again to |
2857 |
;; ensure the column gets correct. :P |
;; ensure the column gets correct. :P |
2858 |
(unwind-protect |
(unwind-protect |
2862 |
(c-point 'bol) |
(c-point 'bol) |
2863 |
(point)) |
(point)) |
2864 |
(current-column))) |
(current-column))) |
2865 |
(delete-region tmp (point)) |
(delete-region tmp (point))))) |
2866 |
(set-buffer-modified-p buffer-modified)))) |
|
2867 |
(t |
(t |
2868 |
;; Last resort: Just add a single space after |
;; Last resort: Just add a single space after |
2869 |
;; the prefix. |
;; the prefix. |
2870 |
(cons (concat fb-string " ") |
(cons (concat fb-string " ") |
2871 |
(progn (goto-char fb-endpos) |
(progn (goto-char fb-endpos) |
2872 |
(1+ (current-column))))))) |
(1+ (current-column))))))) |
2873 |
|
|
2874 |
;; The line doesn't match the comment prefix regexp. |
;; The line doesn't match the comment prefix regexp. |
2875 |
(if comment-prefix |
(if comment-prefix |
2876 |
;; We have a fallback for line comments that we must use. |
;; We have a fallback for line comments that we must use. |
2879 |
comment-prefix) |
comment-prefix) |
2880 |
(progn (back-to-indentation) |
(progn (back-to-indentation) |
2881 |
(+ (current-column) (length comment-prefix)))) |
(+ (current-column) (length comment-prefix)))) |
2882 |
|
|
2883 |
;; Assume we are dealing with a "free text" block |
;; Assume we are dealing with a "free text" block |
2884 |
;; comment where the lines doesn't have any comment |
;; comment where the lines doesn't have any comment |
2885 |
;; prefix at all and we should just fill it as |
;; prefix at all and we should just fill it as |
2887 |
'("" . 0)))))) |
'("" . 0)))))) |
2888 |
)) |
)) |
2889 |
|
|
2890 |
(defun c-mask-comment (fill-paragraph apply-outside-literal fun &rest args) |
(defun c-mask-paragraph (fill-paragraph apply-outside-literal fun &rest args) |
2891 |
;; Calls FUN with ARGS ar arguments. If point is inside a comment, |
;; Calls FUN with ARGS ar arguments while the current paragraph is |
2892 |
;; the comment starter and ender are masked and the buffer is |
;; masked to allow adaptive filling to work correctly. That |
2893 |
;; narrowed to make it look like a normal paragraph during the call. |
;; includes narrowing the buffer and, if point is inside a comment, |
2894 |
|
;; masking the comment starter and ender appropriately. |
2895 |
|
;; |
2896 |
|
;; FILL-PARAGRAPH is non-nil if called for whole paragraph filling. |
2897 |
|
;; The position of point is then less significant when doing masking |
2898 |
|
;; and narrowing. |
2899 |
|
;; |
2900 |
|
;; If APPLY-OUTSIDE-LITERAL is nil then the function will be called |
2901 |
|
;; only if the point turns out to be inside a comment or a string. |
2902 |
;; |
;; |
2903 |
;; FILL-PARAGRAPH is non-nil if called for paragraph filling. The |
;; This function does not do any hidden buffer changes. |
2904 |
;; position of point is then less significant when doing masking and |
|
|
;; narrowing. |
|
2905 |
(let (fill |
(let (fill |
2906 |
;; beg and end limits the region to narrow. end is a marker. |
;; beg and end limits the region to narrow. end is a marker. |
2907 |
beg end |
beg end |
2918 |
(here (point)) |
(here (point)) |
2919 |
(c-lit-limits c-lit-limits) |
(c-lit-limits c-lit-limits) |
2920 |
(c-lit-type c-lit-type)) |
(c-lit-type c-lit-type)) |
2921 |
|
|
2922 |
;; Restore point on undo. It's necessary since we do a lot of |
;; Restore point on undo. It's necessary since we do a lot of |
2923 |
;; hidden inserts and deletes below that should be as transparent |
;; hidden inserts and deletes below that should be as transparent |
2924 |
;; as possible. |
;; as possible. |
2925 |
(if (and buffer-undo-list (not (eq buffer-undo-list t))) |
(if (and buffer-undo-list (not (eq buffer-undo-list t))) |
2926 |
(setq buffer-undo-list (cons (point) buffer-undo-list))) |
(setq buffer-undo-list (cons (point) buffer-undo-list))) |
2927 |
|
|
2928 |
(save-restriction |
(save-restriction |
2929 |
;; Widen to catch comment limits correctly. |
;; Widen to catch comment limits correctly. |
2930 |
(widen) |
(widen) |
2933 |
(setq c-lit-limits (c-collect-line-comments c-lit-limits)) |
(setq c-lit-limits (c-collect-line-comments c-lit-limits)) |
2934 |
(unless c-lit-type |
(unless c-lit-type |
2935 |
(setq c-lit-type (c-literal-type c-lit-limits)))) |
(setq c-lit-type (c-literal-type c-lit-limits)))) |
2936 |
|
|
2937 |
(save-excursion |
(save-excursion |
2938 |
(unless (c-safe (backward-char) |
(unless (c-safe (backward-char) |
2939 |
(forward-paragraph) |
(forward-paragraph) |
2948 |
(goto-char here) |
(goto-char here) |
2949 |
(backward-paragraph)) |
(backward-paragraph)) |
2950 |
(setq beg (point))) |
(setq beg (point))) |
2951 |
|
|
2952 |
(unwind-protect |
(unwind-protect |
2953 |
(progn |
(progn |
2954 |
(cond |
(cond |
2955 |
|
|
2956 |
((eq c-lit-type 'c++) ; Line comment. |
((eq c-lit-type 'c++) ; Line comment. |
2957 |
(save-excursion |
(save-excursion |
2958 |
;; Limit to the comment or paragraph end, whichever |
;; Limit to the comment or paragraph end, whichever |
2959 |
;; comes first. |
;; comes first. |
2960 |
(set-marker end (min end (cdr c-lit-limits))) |
(set-marker end (min end (cdr c-lit-limits))) |
2961 |
|
|
2962 |
(when (<= beg (car c-lit-limits)) |
(when (<= beg (car c-lit-limits)) |
2963 |
;; The region includes the comment starter, so we must |
;; The region includes the comment starter, so we must |
2964 |
;; check it. |
;; check it. |
2969 |
(setq beg (c-point 'bol)) |
(setq beg (c-point 'bol)) |
2970 |
;; The first line contains code before the |
;; The first line contains code before the |
2971 |
;; comment. We must fake a line that doesn't. |
;; comment. We must fake a line that doesn't. |
2972 |
(setq tmp-pre t))) |
(setq tmp-pre t)))) |
2973 |
)) |
|
2974 |
|
(setq apply-outside-literal t)) |
2975 |
|
|
2976 |
((eq c-lit-type 'c) ; Block comment. |
((eq c-lit-type 'c) ; Block comment. |
2977 |
(when (>= end (cdr c-lit-limits)) |
(when (>= end (cdr c-lit-limits)) |
2978 |
;; The region includes the comment ender which we might |
;; The region includes the comment ender which we might |
2984 |
c-current-comment-prefix |
c-current-comment-prefix |
2985 |
"\\)\\*/")) |
"\\)\\*/")) |
2986 |
(eq (cdr c-lit-limits) (match-end 0)) |
(eq (cdr c-lit-limits) (match-end 0)) |
2987 |
;; Leave the comment ender on its own line. |
;; The comment ender is on a line of its |
2988 |
|
;; own. Keep it that way. |
2989 |
(set-marker end (point)))) |
(set-marker end (point)))) |
2990 |
(when fill-paragraph |
|
2991 |
;; The comment ender should hang. Replace all cruft |
(if fill-paragraph |
2992 |
;; between it and the last word with one or two 'x' |
;; The comment ender should hang. Replace all |
2993 |
;; and include it in the region. We'll change them |
;; cruft between it and the last word with one or |
2994 |
;; back to spaces afterwards. |
;; two 'x' and include it in the region. We'll |
2995 |
(let* ((ender-start (save-excursion |
;; change them back to spaces afterwards. This |
2996 |
(goto-char (cdr c-lit-limits)) |
;; isn't done when auto filling, since that'd |
2997 |
(skip-syntax-backward "^w ") |
;; effectively make it impossible to insert extra |
2998 |
(point))) |
;; spaces before the comment ender. |
2999 |
(point-rel (- ender-start here)) |
(let* ((ender-start (save-excursion |
3000 |
spaces) |
(goto-char (cdr c-lit-limits)) |
3001 |
(save-excursion |
(skip-syntax-backward "^w ") |
3002 |
(goto-char (cdr c-lit-limits)) |
(point))) |
3003 |
(setq tmp-post (point-marker)) |
(point-rel (- ender-start here)) |
3004 |
(insert ?\n) |
spaces) |
3005 |
(set-marker end (point)) |
|
3006 |
(forward-line -1) |
(save-excursion |
3007 |
(if (and (looking-at (concat "[ \t]*\\(\\(" |
(goto-char (cdr c-lit-limits)) |
3008 |
c-current-comment-prefix |
(setq tmp-post (point-marker)) |
3009 |
"\\)[ \t]*\\)")) |
(insert ?\n) |
3010 |
(eq ender-start (match-end 0))) |
(set-marker end (point)) |
3011 |
;; The comment ender is prefixed by nothing |
(forward-line -1) |
3012 |
;; but a comment line prefix. Remove it |
(if (and (looking-at (concat "[ \t]*\\(\\(" |
3013 |
;; along with surrounding ws. |
c-current-comment-prefix |
3014 |
(setq spaces (- (match-end 1) (match-end 2))) |
"\\)[ \t]*\\)")) |
3015 |
(goto-char ender-start)) |
(eq ender-start (match-end 0))) |
3016 |
(skip-chars-backward " \t\r\n") |
;; The comment ender is prefixed by nothing |
3017 |
(if (/= (point) ender-start) |
;; but a comment line prefix. Remove it |
3018 |
(progn |
;; along with surrounding ws. |
3019 |
(if (<= here (point)) |
(setq spaces (- (match-end 1) (match-end 2))) |
3020 |
;; Don't adjust point below if it's |
(goto-char ender-start)) |
3021 |
;; before the string we replace. |
(skip-chars-backward " \t\r\n") |
3022 |
(setq point-rel -1)) |
|
3023 |
;; Keep one or two spaces between the text and |
(if (/= (point) ender-start) |
3024 |
;; the ender, depending on how many there are now. |
(progn |
3025 |
(unless spaces |
(if (<= here (point)) |
3026 |
(setq spaces (- ender-start (point)))) |
;; Don't adjust point below if it's |
3027 |
(setq spaces |
;; before the string we replace. |
3028 |
(max (min spaces |
(setq point-rel -1)) |
3029 |
(if sentence-end-double-space 2 1)) |
;; Keep one or two spaces between the |
3030 |
1)) |
;; text and the ender, depending on how |
3031 |
;; Insert the filler first to keep marks right. |
;; many there are now. |
3032 |
(insert-char ?x spaces t) |
(unless spaces |
3033 |
(delete-region (point) (+ ender-start spaces)) |
(setq spaces (- ender-start (point)))) |
3034 |
(setq hang-ender-stuck spaces) |
(setq spaces |
3035 |
(setq point-rel |
(max |
3036 |
(and (>= point-rel 0) |
(min spaces |
3037 |
(- (point) (min point-rel spaces))))) |
(if sentence-end-double-space 2 1)) |
3038 |
(setq point-rel nil))) |
1)) |
3039 |
(if point-rel |
;; Insert the filler first to keep marks right. |
3040 |
;; Point was in the middle of the string we |
(insert-char ?x spaces t) |
3041 |
;; replaced above, so put it back in the same |
(delete-region (point) (+ ender-start spaces)) |
3042 |
;; relative position, counting from the end. |
(setq hang-ender-stuck spaces) |
3043 |
(goto-char point-rel)) |
(setq point-rel |
3044 |
)))) |
(and (>= point-rel 0) |
3045 |
|
(- (point) (min point-rel spaces))))) |
3046 |
|
(setq point-rel nil))) |
3047 |
|
|
3048 |
|
(if point-rel |
3049 |
|
;; Point was in the middle of the string we |
3050 |
|
;; replaced above, so put it back in the same |
3051 |
|
;; relative position, counting from the end. |
3052 |
|
(goto-char point-rel))) |
3053 |
|
|
3054 |
|
;; We're doing auto filling. Just move the marker |
3055 |
|
;; to the comment end to ignore any code after the |
3056 |
|
;; comment. |
3057 |
|
(move-marker end (cdr c-lit-limits))))) |
3058 |
|
|
3059 |
(when (<= beg (car c-lit-limits)) |
(when (<= beg (car c-lit-limits)) |
3060 |
;; The region includes the comment starter. |
;; The region includes the comment starter. |
3061 |
(save-excursion |
(save-excursion |
3064 |
;; Begin with the next line. |
;; Begin with the next line. |
3065 |
(setq beg (c-point 'bonl)) |
(setq beg (c-point 'bonl)) |
3066 |
;; Fake the fill prefix in the first line. |
;; Fake the fill prefix in the first line. |
3067 |
(setq tmp-pre t))))) |
(setq tmp-pre t)))) |
3068 |
|
|
3069 |
|
(setq apply-outside-literal t)) |
3070 |
|
|
3071 |
((eq c-lit-type 'string) ; String. |
((eq c-lit-type 'string) ; String. |
3072 |
(save-excursion |
(save-excursion |
3073 |
(when (>= end (cdr c-lit-limits)) |
(when (>= end (cdr c-lit-limits)) |
3081 |
;; Leave the start line if it's |
;; Leave the start line if it's |
3082 |
;; nothing but an escaped newline. |
;; nothing but an escaped newline. |
3083 |
(1+ (match-end 0)) |
(1+ (match-end 0)) |
3084 |
(point)))))) |
(point))))) |
3085 |
(t (setq beg nil))) |
(setq apply-outside-literal t)) |
3086 |
|
|
3087 |
|
((eq c-lit-type 'pound) ; Macro |
3088 |
|
;; Narrow to the macro limits if they are nearer than the |
3089 |
|
;; paragraph limits. Don't know if this is necessary but |
3090 |
|
;; do it for completeness sake (doing auto filling at all |
3091 |
|
;; inside macros is bogus to begin with since the line |
3092 |
|
;; continuation backslashes aren't handled). |
3093 |
|
(save-excursion |
3094 |
|
(c-beginning-of-macro) |
3095 |
|
(beginning-of-line) |
3096 |
|
(if (> (point) beg) |
3097 |
|
(setq beg (point))) |
3098 |
|
(c-end-of-macro) |
3099 |
|
(forward-line) |
3100 |
|
(if (< (point) end) |
3101 |
|
(set-marker end (point))))) |
3102 |
|
|
3103 |
|
(t ; Other code. |
3104 |
|
;; Try to avoid comments and macros in the paragraph to |
3105 |
|
;; avoid that the adaptive fill mode gets the prefix from |
3106 |
|
;; them. |
3107 |
|
(c-save-buffer-state nil |
3108 |
|
(save-excursion |
3109 |
|
(goto-char beg) |
3110 |
|
(c-forward-syntactic-ws end) |
3111 |
|
(beginning-of-line) |
3112 |
|
(setq beg (point)) |
3113 |
|
(goto-char end) |
3114 |
|
(c-backward-syntactic-ws beg) |
3115 |
|
(forward-line) |
3116 |
|
(set-marker end (point)))))) |
3117 |
|
|
3118 |
(when tmp-pre |
(when tmp-pre |
3119 |
;; Temporarily insert the fill prefix after the comment |
;; Temporarily insert the fill prefix after the comment |
3120 |
;; starter so that the first line looks like any other |
;; starter so that the first line looks like any other |
3121 |
;; comment line in the narrowed region. |
;; comment line in the narrowed region. |
3122 |
(setq fill (c-guess-fill-prefix c-lit-limits c-lit-type)) |
(setq fill (c-save-buffer-state nil |
3123 |
|
(c-guess-fill-prefix c-lit-limits c-lit-type))) |
3124 |
(unless (string-match (concat "\\`[ \t]*\\(" |
(unless (string-match (concat "\\`[ \t]*\\(" |
3125 |
c-current-comment-prefix |
c-current-comment-prefix |
3126 |
"\\)[ \t]*\\'") |
"\\)[ \t]*\\'") |
3157 |
(insert-and-inherit "\n" (car fill)) |
(insert-and-inherit "\n" (car fill)) |
3158 |
(insert-char ?x (- col (current-column)) t)) |
(insert-char ?x (- col (current-column)) t)) |
3159 |
(setcdr tmp-pre (point)))))) |
(setcdr tmp-pre (point)))))) |
3160 |
(if beg |
|
3161 |
(let ((fill-prefix |
(when apply-outside-literal |
3162 |
(or fill-prefix |
;; `apply-outside-literal' is always set to t here if |
3163 |
;; Kludge: If the function that adapts the |
;; we're inside a literal. |
3164 |
;; fill prefix doesn't produce the required |
|
3165 |
;; comment starter for line comments, then |
(let ((fill-prefix |
3166 |
;; force it by setting fill-prefix. |
(or fill-prefix |
3167 |
(when (and (eq c-lit-type 'c++) |
;; Kludge: If the function that adapts the fill prefix |
3168 |
;; Kludge the kludge: |
;; doesn't produce the required comment starter for |
3169 |
;; filladapt-mode doesn't have |
;; line comments, then force it by setting fill-prefix. |
3170 |
;; this problem, but it doesn't |
(when (and (eq c-lit-type 'c++) |
3171 |
;; override fill-context-prefix |
;; Kludge the kludge: filladapt-mode doesn't |
3172 |
;; currently (version 2.12). |
;; have this problem, but it currently |
3173 |
(not (and (boundp 'filladapt-mode) |
;; doesn't override fill-context-prefix |
3174 |
filladapt-mode)) |
;; (version 2.12). |
3175 |
(not (string-match |
(not (and (boundp 'filladapt-mode) |
3176 |
"\\`[ \t]*//" |
filladapt-mode)) |
3177 |
(or (fill-context-prefix beg end) |
(not (string-match |
3178 |
"")))) |
"\\`[ \t]*//" |
3179 |
|
(or (fill-context-prefix beg end) |
3180 |
|
"")))) |
3181 |
|
(c-save-buffer-state nil |
3182 |
(car (or fill (c-guess-fill-prefix |
(car (or fill (c-guess-fill-prefix |
3183 |
c-lit-limits c-lit-type)))))) |
c-lit-limits c-lit-type))))))) |
3184 |
;; Save the relative position of point if it's |
|
3185 |
;; outside the region we're going to narrow. Want |
;; Save the relative position of point if it's outside the |
3186 |
;; to restore it in that case, but otherwise it |
;; region we're going to narrow. Want to restore it in that |
3187 |
;; should be moved according to the called |
;; case, but otherwise it should be moved according to the |
3188 |
;; function. |
;; called function. |
3189 |
(point-rel (cond ((< (point) beg) (- (point) beg)) |
(point-rel (cond ((< (point) beg) (- (point) beg)) |
3190 |
((> (point) end) (- (point) end))))) |
((> (point) end) (- (point) end))))) |
3191 |
;; Preparations finally done! Now we can call the |
|
3192 |
;; actual function. |
;; Preparations finally done! Now we can call the |
3193 |
(prog1 |
;; actual function. |
3194 |
(save-restriction |
(prog1 |
3195 |
(narrow-to-region beg end) |
(save-restriction |
3196 |
(apply fun args)) |
(narrow-to-region beg end) |
3197 |
(if point-rel |
(apply fun args)) |
3198 |
;; Restore point if it was outside the region. |
(if point-rel |
3199 |
(if (< point-rel 0) |
;; Restore point if it was outside the region. |
3200 |
(goto-char (+ beg point-rel)) |
(if (< point-rel 0) |
3201 |
(goto-char (+ end point-rel)))))) |
(goto-char (+ beg point-rel)) |
3202 |
(when apply-outside-literal |
(goto-char (+ end point-rel)))))))) |
3203 |
(apply fun args)))) |
|
3204 |
(when (consp tmp-pre) |
(when (consp tmp-pre) |
3205 |
(delete-region (car tmp-pre) (cdr tmp-pre))) |
(delete-region (car tmp-pre) (cdr tmp-pre))) |
3206 |
|
|
3207 |
(when tmp-post |
(when tmp-post |
3208 |
(save-excursion |
(save-excursion |
3209 |
(goto-char tmp-post) |
(goto-char tmp-post) |
3219 |
(delete-char hang-ender-stuck) |
(delete-char hang-ender-stuck) |
3220 |
(goto-char here)) |
(goto-char here)) |
3221 |
(set-marker tmp-post nil)) |
(set-marker tmp-post nil)) |
3222 |
|
|
3223 |
(set-marker end nil)))) |
(set-marker end nil)))) |
3224 |
|
|
3225 |
(defun c-fill-paragraph (&optional arg) |
(defun c-fill-paragraph (&optional arg) |
3247 |
;; Avoid infinite recursion. |
;; Avoid infinite recursion. |
3248 |
(if (not (eq fill-paragraph-function 'c-fill-paragraph)) |
(if (not (eq fill-paragraph-function 'c-fill-paragraph)) |
3249 |
fill-paragraph-function))) |
fill-paragraph-function))) |
3250 |
(c-mask-comment t nil 'fill-paragraph arg)) |
(c-mask-paragraph t nil 'fill-paragraph arg)) |
3251 |
;; Always return t. This has the effect that if filling isn't done |
;; Always return t. This has the effect that if filling isn't done |
3252 |
;; above, it isn't done at all, and it's therefore effectively |
;; above, it isn't done at all, and it's therefore effectively |
3253 |
;; disabled in normal code. |
;; disabled in normal code. |
3256 |
(defun c-do-auto-fill () |
(defun c-do-auto-fill () |
3257 |
;; Do automatic filling if not inside a context where it should be |
;; Do automatic filling if not inside a context where it should be |
3258 |
;; ignored. |
;; ignored. |
3259 |
|
;; |
3260 |
|
;; This function does not do any hidden buffer changes. |
3261 |
(let ((c-auto-fill-prefix |
(let ((c-auto-fill-prefix |
3262 |
;; The decision whether the line should be broken is actually |
;; The decision whether the line should be broken is actually |
3263 |
;; done in c-indent-new-comment-line, which do-auto-fill |
;; done in c-indent-new-comment-line, which do-auto-fill |
3266 |
;; also used to detect whether fill-prefix is user set or |
;; also used to detect whether fill-prefix is user set or |
3267 |
;; generated automatically by do-auto-fill. |
;; generated automatically by do-auto-fill. |
3268 |
fill-prefix)) |
fill-prefix)) |
3269 |
(c-mask-comment nil t 'do-auto-fill))) |
(c-mask-paragraph nil t 'do-auto-fill))) |
3270 |
|
|
3271 |
(defun c-indent-new-comment-line (&optional soft allow-auto-fill) |
(defun c-indent-new-comment-line (&optional soft allow-auto-fill) |
3272 |
"Break line at point and indent, continuing comment or macro if within one. |
"Break line at point and indent, continuing comment or macro if within one. |
3342 |
(end-of-line) |
(end-of-line) |
3343 |
(< (point) (cdr c-lit-limits)))) |
(< (point) (cdr c-lit-limits)))) |
3344 |
;; Inside a comment that should be continued. |
;; Inside a comment that should be continued. |
3345 |
(let ((fill (c-guess-fill-prefix |
(let ((fill (c-save-buffer-state nil |
3346 |
(setq c-lit-limits |
(c-guess-fill-prefix |
3347 |
(c-collect-line-comments c-lit-limits)) |
(setq c-lit-limits |
3348 |
c-lit-type)) |
(c-collect-line-comments c-lit-limits)) |
3349 |
|
c-lit-type))) |
3350 |
(pos (point)) |
(pos (point)) |
3351 |
(comment-text-end |
(comment-text-end |
3352 |
(or (and (eq c-lit-type 'c) |
(or (and (eq c-lit-type 'c) |
3479 |
(c-query-and-set-macro-start) |
(c-query-and-set-macro-start) |
3480 |
(<= (save-excursion |
(<= (save-excursion |
3481 |
(goto-char c-macro-start) |
(goto-char c-macro-start) |
3482 |
(if (looking-at "#[ \t]*[a-zA-Z0-9!]+") |
(if (looking-at c-opt-cpp-start) |
3483 |
(goto-char (match-end 0))) |
(goto-char (match-end 0))) |
3484 |
(point)) |
(point)) |
3485 |
(point)))) |
(point)))) |
3516 |
|
|
3517 |
(cc-provide 'cc-cmds) |
(cc-provide 'cc-cmds) |
3518 |
|
|
3519 |
|
;;; arch-tag: bf0611dc-d1f4-449e-9e45-4ec7c6936677 |
3520 |
;;; cc-cmds.el ends here |
;;; cc-cmds.el ends here |