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