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