Mercurial > emacs
annotate lisp/progmodes/cc-cmds.el @ 19256:e4b14e6fd28f
(widget-single-line-display-table): Variable
commented out; don't use it either.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 09 Aug 1997 19:11:15 +0000 |
parents | b573491d7c8f |
children | 1982f8488449 |
rev | line source |
---|---|
18720 | 1 ;;; cc-cmds.el --- user level commands for CC Mode |
2 | |
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc. | |
4 | |
5 ;; Authors: 1992-1997 Barry A. Warsaw | |
6 ;; 1987 Dave Detlefs and Stewart Clamen | |
7 ;; 1985 Richard M. Stallman | |
8 ;; Maintainer: cc-mode-help@python.org | |
9 ;; Created: 22-Apr-1997 (split from cc-mode.el) | |
19252 | 10 ;; Version: 5.14 |
18720 | 11 ;; Keywords: c languages oop |
12 | |
13 ;; This file is part of GNU Emacs. | |
14 | |
15 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
16 ;; it under the terms of the GNU General Public License as published by | |
17 ;; the Free Software Foundation; either version 2, or (at your option) | |
18 ;; any later version. | |
19 | |
20 ;; GNU Emacs is distributed in the hope that it will be useful, | |
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 ;; GNU General Public License for more details. | |
24 | |
25 ;; You should have received a copy of the GNU General Public License | |
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
28 ;; Boston, MA 02111-1307, USA. | |
29 | |
18843
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
30 (eval-when-compile |
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
31 (require 'cc-defs)) |
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
32 |
18720 | 33 |
34 (defun c-calculate-state (arg prevstate) | |
35 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If | |
36 ;; arg is nil or zero, toggle the state. If arg is negative, turn | |
37 ;; the state off, and if arg is positive, turn the state on | |
38 (if (or (not arg) | |
39 (zerop (setq arg (prefix-numeric-value arg)))) | |
40 (not prevstate) | |
41 (> arg 0))) | |
42 | |
43 ;; Auto-newline and hungry-delete | |
44 (defun c-toggle-auto-state (arg) | |
45 "Toggle auto-newline feature. | |
46 Optional numeric ARG, if supplied turns on auto-newline when positive, | |
47 turns it off when negative, and just toggles it when zero. | |
48 | |
49 When the auto-newline feature is enabled (as evidenced by the `/a' or | |
50 `/ah' on the modeline after the mode name) newlines are automatically | |
51 inserted after special characters such as brace, comma, semi-colon, | |
52 and colon." | |
53 (interactive "P") | |
54 (setq c-auto-newline (c-calculate-state arg c-auto-newline)) | |
55 (c-update-modeline) | |
56 (c-keep-region-active)) | |
57 | |
58 (defun c-toggle-hungry-state (arg) | |
59 "Toggle hungry-delete-key feature. | |
60 Optional numeric ARG, if supplied turns on hungry-delete when positive, | |
61 turns it off when negative, and just toggles it when zero. | |
62 | |
63 When the hungry-delete-key feature is enabled (as evidenced by the | |
64 `/h' or `/ah' on the modeline after the mode name) the delete key | |
65 gobbles all preceding whitespace in one fell swoop." | |
66 (interactive "P") | |
67 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key)) | |
68 (c-update-modeline) | |
69 (c-keep-region-active)) | |
70 | |
71 (defun c-toggle-auto-hungry-state (arg) | |
72 "Toggle auto-newline and hungry-delete-key features. | |
73 Optional numeric ARG, if supplied turns on auto-newline and | |
74 hungry-delete when positive, turns them off when negative, and just | |
75 toggles them when zero. | |
76 | |
77 See `c-toggle-auto-state' and `c-toggle-hungry-state' for details." | |
78 (interactive "P") | |
79 (setq c-auto-newline (c-calculate-state arg c-auto-newline)) | |
80 (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key)) | |
81 (c-update-modeline) | |
82 (c-keep-region-active)) | |
83 | |
84 | |
85 ;; Electric keys | |
86 | |
87 ;; Note: In XEmacs 20.3 the Delete and BackSpace keysyms have been | |
88 ;; separated and "\177" is no longer an alias for both keys. Also, | |
89 ;; the variable delete-key-deletes-forward controls in which direction | |
90 ;; the Delete keysym deletes characters. The functions | |
91 ;; c-electric-delete and c-electric-backspace attempt to deal with | |
92 ;; this new functionality. For Emacs 19 and XEmacs 19 backwards | |
93 ;; compatibility, the old behavior has moved to c-electric-backspace | |
94 ;; and c-backspace-function. | |
95 | |
96 (defun c-electric-backspace (arg) | |
97 "Deletes preceding character or whitespace. | |
98 If `c-hungry-delete-key' is non-nil, as evidenced by the \"/h\" or | |
99 \"/ah\" string on the mode line, then all preceding whitespace is | |
100 consumed. If however an ARG is supplied, or `c-hungry-delete-key' is | |
101 nil, or point is inside a literal then the function in the variable | |
102 `c-backspace-function' is called. | |
103 | |
104 See also \\[c-electric-delete]." | |
105 (interactive "P") | |
106 (if (or (not c-hungry-delete-key) | |
107 arg | |
108 (c-in-literal)) | |
109 (funcall c-backspace-function (prefix-numeric-value arg)) | |
110 (let ((here (point))) | |
111 (skip-chars-backward " \t\n") | |
112 (if (/= (point) here) | |
113 (delete-region (point) here) | |
114 (funcall c-backspace-function 1) | |
115 )))) | |
116 | |
117 (defun c-electric-delete (arg) | |
118 "Deletes preceding or following character or whitespace. | |
119 | |
120 The behavior of this function depends on the variable | |
121 `delete-key-deletes-forward'. If this variable is nil (or does not | |
122 exist, as in older Emacsen), then this function behaves identical to | |
123 \\[c-electric-backspace]. | |
124 | |
125 If `delete-key-deletes-forward' is non-nil, then deletion occurs in | |
126 the forward direction. So if `c-hungry-delete-key' is non-nil, as | |
127 evidenced by the \"/h\" or \"/ah\" string on the mode line, then all | |
128 following whitespace is consumed. If however an ARG is supplied, or | |
129 `c-hungry-delete-key' is nil, or point is inside a literal then the | |
130 function in the variable `c-delete-function' is called." | |
131 (interactive "P") | |
132 (if (and (boundp 'delete-key-deletes-forward) | |
133 delete-key-deletes-forward) | |
134 (if (or (not c-hungry-delete-key) | |
135 arg | |
136 (c-in-literal)) | |
137 (funcall c-delete-function (prefix-numeric-value arg)) | |
138 (let ((here (point))) | |
139 (skip-chars-forward " \t\n") | |
140 (if (/= (point) here) | |
141 (delete-region (point) here) | |
142 (funcall c-delete-function 1)))) | |
143 ;; act just like c-electric-backspace | |
144 (c-electric-backspace arg))) | |
145 | |
146 (defun c-electric-pound (arg) | |
147 "Electric pound (`#') insertion. | |
148 Inserts a `#' character specially depending on the variable | |
149 `c-electric-pound-behavior'. If a numeric ARG is supplied, or if | |
150 point is inside a literal, nothing special happens." | |
151 (interactive "P") | |
152 (if (or (c-in-literal) | |
153 arg | |
154 (not (memq 'alignleft c-electric-pound-behavior))) | |
155 ;; do nothing special | |
156 (self-insert-command (prefix-numeric-value arg)) | |
157 ;; place the pound character at the left edge | |
158 (let ((pos (- (point-max) (point))) | |
159 (bolp (bolp))) | |
160 (beginning-of-line) | |
161 (delete-horizontal-space) | |
162 (insert-char last-command-char 1) | |
163 (and (not bolp) | |
164 (goto-char (- (point-max) pos))) | |
165 ))) | |
166 | |
167 (defun c-electric-brace (arg) | |
168 "Insert a brace. | |
169 | |
170 If the auto-newline feature is turned on, as evidenced by the \"/a\" | |
171 or \"/ah\" string on the mode line, newlines are inserted before and | |
172 after braces based on the value of `c-hanging-braces-alist'. | |
173 | |
174 Also, the line is re-indented unless a numeric ARG is supplied, there | |
175 are non-whitespace characters present on the line after the brace, or | |
176 the brace is inserted inside a literal." | |
177 (interactive "P") | |
178 (let* ((c-state-cache (c-parse-state)) | |
179 (safepos (c-safe-position (point) c-state-cache)) | |
180 (literal (c-in-literal safepos))) | |
181 ;; if we're in a literal, or we're not at the end of the line, or | |
182 ;; a numeric arg is provided, or auto-newlining is turned off, | |
183 ;; then just insert the character. | |
184 (if (or literal arg | |
185 ; (not c-auto-newline) | |
186 (not (looking-at "[ \t]*$"))) | |
187 (self-insert-command (prefix-numeric-value arg)) | |
188 (let* ((syms '(class-open class-close defun-open defun-close | |
189 inline-open inline-close brace-list-open brace-list-close | |
190 brace-list-intro brace-list-entry block-open block-close | |
191 substatement-open statement-case-open | |
192 extern-lang-open extern-lang-close)) | |
193 ;; we want to inhibit blinking the paren since this will | |
194 ;; be most disruptive. we'll blink it ourselves later on | |
195 (old-blink-paren blink-paren-function) | |
196 blink-paren-function | |
197 (insertion-point (point)) | |
198 delete-temp-newline | |
18843
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
199 (preserve-p (eq ?\ (char-syntax (char-before)))) |
18720 | 200 ;; shut this up too |
201 (c-echo-syntactic-information-p nil) | |
202 (syntax (progn | |
203 ;; only insert a newline if there is | |
204 ;; non-whitespace behind us | |
205 (if (save-excursion | |
206 (skip-chars-backward " \t") | |
207 (not (bolp))) | |
208 (progn (newline) | |
209 (setq delete-temp-newline t))) | |
210 (self-insert-command (prefix-numeric-value arg)) | |
211 ;; state cache doesn't change | |
212 (c-guess-basic-syntax))) | |
213 (newlines (and | |
214 c-auto-newline | |
215 (or (c-lookup-lists syms syntax c-hanging-braces-alist) | |
216 '(ignore before after))))) | |
217 ;; If syntax is a function symbol, then call it using the | |
218 ;; defined semantics. | |
219 (if (and (not (consp (cdr newlines))) | |
220 (functionp (cdr newlines))) | |
221 (let ((c-syntactic-context syntax)) | |
222 (setq newlines | |
223 (funcall (cdr newlines) (car newlines) insertion-point)))) | |
224 ;; does a newline go before the open brace? | |
225 (if (memq 'before newlines) | |
226 ;; we leave the newline we've put in there before, | |
227 ;; but we need to re-indent the line above | |
228 (let ((pos (- (point-max) (point))) | |
229 (here (point)) | |
230 (c-state-cache c-state-cache)) | |
231 (forward-line -1) | |
232 ;; we may need to update the cache. this should still be | |
233 ;; faster than recalculating the state in many cases | |
234 (save-excursion | |
235 (save-restriction | |
236 (narrow-to-region here (point)) | |
237 (if (and (c-safe (progn (backward-up-list -1) t)) | |
238 (memq (char-before) '(?\) ?})) | |
239 (progn (widen) | |
240 (c-safe (progn (forward-sexp -1) t)))) | |
241 (setq c-state-cache | |
242 (c-hack-state (point) 'open c-state-cache)) | |
243 (if (and (car c-state-cache) | |
244 (not (consp (car c-state-cache))) | |
245 (<= (point) (car c-state-cache))) | |
246 (setq c-state-cache (cdr c-state-cache)) | |
247 )))) | |
248 (let ((here (point)) | |
249 (shift (c-indent-line))) | |
250 (setq c-state-cache (c-adjust-state (c-point 'bol) here | |
251 (- shift) c-state-cache))) | |
252 (goto-char (- (point-max) pos)) | |
253 ;; if the buffer has changed due to the indentation, we | |
254 ;; need to recalculate syntax for the current line, but | |
255 ;; we won't need to update the state cache. | |
256 (if (/= (point) here) | |
257 (setq syntax (c-guess-basic-syntax)))) | |
258 ;; must remove the newline we just stuck in (if we really did it) | |
259 (and delete-temp-newline | |
260 (save-excursion | |
261 ;; if there is whitespace before point, then preserve | |
262 ;; at least one space. | |
263 (delete-indentation) | |
264 (just-one-space) | |
265 (if (not preserve-p) | |
266 (delete-char -1)))) | |
267 ;; since we're hanging the brace, we need to recalculate | |
268 ;; syntax. Update the state to accurately reflect the | |
269 ;; beginning of the line. We punt if we cross any open or | |
270 ;; closed parens because its just too hard to modify the | |
271 ;; known state. This limitation will be fixed in v5. | |
272 (save-excursion | |
273 (let ((bol (c-point 'bol))) | |
274 (if (zerop (car (parse-partial-sexp bol (1- (point))))) | |
275 (setq c-state-cache (c-whack-state bol c-state-cache) | |
276 syntax (c-guess-basic-syntax)) | |
277 ;; gotta punt. this requires some horrible kludgery | |
278 (beginning-of-line) | |
279 (makunbound 'c-state-cache) | |
280 (setq c-state-cache (c-parse-state) | |
281 syntax nil)))) | |
282 ) | |
283 ;; now adjust the line's indentation. don't update the state | |
284 ;; cache since c-guess-basic-syntax isn't called when the | |
285 ;; syntax is passed to c-indent-line | |
286 (let ((here (point)) | |
287 (shift (c-indent-line syntax))) | |
288 (setq c-state-cache (c-adjust-state (c-point 'bol) here | |
289 (- shift) c-state-cache))) | |
290 ;; Do all appropriate clean ups | |
291 (let ((here (point)) | |
292 (pos (- (point-max) (point))) | |
293 mbeg mend) | |
294 ;; clean up empty defun braces | |
295 (if (and c-auto-newline | |
296 (memq 'empty-defun-braces c-cleanup-list) | |
297 (eq last-command-char ?\}) | |
298 (c-intersect-lists '(defun-close class-close inline-close) | |
299 syntax) | |
300 (progn | |
301 (forward-char -1) | |
302 (skip-chars-backward " \t\n") | |
303 (eq (char-before) ?\{)) | |
304 ;; make sure matching open brace isn't in a comment | |
305 (not (c-in-literal))) | |
306 (delete-region (point) (1- here))) | |
307 ;; clean up brace-else-brace | |
308 (if (and c-auto-newline | |
309 (memq 'brace-else-brace c-cleanup-list) | |
310 (eq last-command-char ?\{) | |
311 (re-search-backward "}[ \t\n]*else[ \t\n]*{" nil t) | |
312 (progn | |
313 (setq mbeg (match-beginning 0) | |
314 mend (match-end 0)) | |
315 (= mend here)) | |
316 (not (c-in-literal))) | |
317 (progn | |
318 (delete-region mbeg mend) | |
319 (insert "} else {"))) | |
320 ;; clean up brace-elseif-brace | |
321 (if (and c-auto-newline | |
322 (memq 'brace-elseif-brace c-cleanup-list) | |
323 (eq last-command-char ?\{) | |
324 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*" nil t) | |
325 (save-excursion | |
326 (goto-char (match-end 0)) | |
327 (c-safe (forward-sexp 1)) | |
328 (skip-chars-forward " \t\n") | |
329 (setq mbeg (match-beginning 0) | |
330 mend (match-end 0)) | |
331 (= here (1+ (point)))) | |
332 (not (c-in-literal))) | |
333 (progn | |
334 (delete-region mbeg mend) | |
335 (insert "} else if "))) | |
336 (goto-char (- (point-max) pos)) | |
337 ) | |
338 ;; does a newline go after the brace? | |
339 (if (memq 'after newlines) | |
340 (progn | |
341 (newline) | |
342 ;; update on c-state-cache | |
343 (let* ((bufpos (- (point) 2)) | |
344 (which (if (eq (char-after bufpos) ?{) 'open 'close)) | |
345 (c-state-cache (c-hack-state bufpos which c-state-cache))) | |
346 (c-indent-line)))) | |
347 ;; blink the paren | |
348 (and (eq last-command-char ?\}) | |
349 old-blink-paren | |
350 (save-excursion | |
351 (c-backward-syntactic-ws safepos) | |
352 (funcall old-blink-paren))) | |
353 )))) | |
354 | |
355 (defun c-electric-slash (arg) | |
356 "Insert a slash character. | |
357 If slash is second of a double-slash C++ style comment introducing | |
358 construct, and we are on a comment-only-line, indent line as comment. | |
359 If numeric ARG is supplied or point is inside a literal, indentation | |
360 is inhibited." | |
361 (interactive "P") | |
362 (let ((indentp (and (not arg) | |
363 (eq (char-before) ?/) | |
364 (eq last-command-char ?/) | |
365 (not (c-in-literal)))) | |
366 ;; shut this up | |
367 (c-echo-syntactic-information-p nil)) | |
368 (self-insert-command (prefix-numeric-value arg)) | |
369 (if indentp | |
370 (c-indent-line)))) | |
371 | |
372 (defun c-electric-star (arg) | |
373 "Insert a star character. | |
374 If the star is the second character of a C style comment introducing | |
375 construct, and we are on a comment-only-line, indent line as comment. | |
376 If numeric ARG is supplied or point is inside a literal, indentation | |
377 is inhibited." | |
378 (interactive "P") | |
379 (self-insert-command (prefix-numeric-value arg)) | |
380 ;; if we are in a literal, or if arg is given do not re-indent the | |
381 ;; current line, unless this star introduces a comment-only line. | |
382 (if (and (not arg) | |
383 (memq (c-in-literal) '(c)) | |
384 (eq (char-before) ?*) | |
385 (save-excursion | |
386 (forward-char -1) | |
387 (skip-chars-backward "*") | |
388 (if (eq (char-before) ?/) | |
389 (forward-char -1)) | |
390 (skip-chars-backward " \t") | |
391 (bolp))) | |
392 ;; shut this up | |
393 (let (c-echo-syntactic-information-p) | |
394 (c-indent-line)) | |
395 )) | |
396 | |
397 (defun c-electric-semi&comma (arg) | |
398 "Insert a comma or semicolon. | |
399 When the auto-newline feature is turned on, as evidenced by the \"/a\" | |
400 or \"/ah\" string on the mode line, a newline might be inserted. See | |
401 the variable `c-hanging-semi&comma-criteria' for how newline insertion | |
402 is determined. | |
403 | |
404 When semicolon is inserted, the line is re-indented unless a numeric | |
405 arg is supplied, point is inside a literal, or there are | |
406 non-whitespace characters on the line following the semicolon." | |
407 (interactive "P") | |
408 (let* ((lim (c-most-enclosing-brace (c-parse-state))) | |
409 (literal (c-in-literal lim)) | |
410 (here (point)) | |
411 ;; shut this up | |
412 (c-echo-syntactic-information-p nil)) | |
413 (if (or literal | |
414 arg | |
415 (not (looking-at "[ \t]*$"))) | |
416 (self-insert-command (prefix-numeric-value arg)) | |
417 ;; do some special stuff with the character | |
418 (self-insert-command (prefix-numeric-value arg)) | |
419 ;; do all cleanups, reindentations, and newline insertions, but | |
420 ;; only if c-auto-newline is turned on | |
421 (if (not c-auto-newline) nil | |
422 ;; clean ups | |
423 (let ((pos (- (point-max) (point)))) | |
424 (if (and (or (and | |
425 (eq last-command-char ?,) | |
426 (memq 'list-close-comma c-cleanup-list)) | |
427 (and | |
428 (eq last-command-char ?\;) | |
429 (memq 'defun-close-semi c-cleanup-list))) | |
430 (progn | |
431 (forward-char -1) | |
432 (skip-chars-backward " \t\n") | |
433 (eq (char-before) ?})) | |
434 ;; make sure matching open brace isn't in a comment | |
435 (not (c-in-literal lim))) | |
436 (delete-region (point) here)) | |
437 (goto-char (- (point-max) pos))) | |
438 ;; re-indent line | |
439 (c-indent-line) | |
440 ;; check to see if a newline should be added | |
441 (let ((criteria c-hanging-semi&comma-criteria) | |
442 answer add-newline-p) | |
443 (while criteria | |
444 (setq answer (funcall (car criteria))) | |
445 ;; only nil value means continue checking | |
446 (if (not answer) | |
447 (setq criteria (cdr criteria)) | |
448 (setq criteria nil) | |
449 ;; only 'stop specifically says do not add a newline | |
450 (setq add-newline-p (not (eq answer 'stop))) | |
451 )) | |
452 (if add-newline-p | |
453 (progn (newline) | |
454 (c-indent-line))) | |
455 ))))) | |
456 | |
457 (defun c-electric-colon (arg) | |
458 "Insert a colon. | |
459 | |
460 If the auto-newline feature is turned on, as evidenced by the \"/a\" | |
461 or \"/ah\" string on the mode line, newlines are inserted before and | |
462 after colons based on the value of `c-hanging-colons-alist'. | |
463 | |
464 Also, the line is re-indented unless a numeric ARG is supplied, there | |
465 are non-whitespace characters present on the line after the colon, or | |
466 the colon is inserted inside a literal. | |
467 | |
468 This function cleans up double colon scope operators based on the | |
469 value of `c-cleanup-list'." | |
470 (interactive "P") | |
471 (let* ((bod (c-point 'bod)) | |
472 (literal (c-in-literal bod)) | |
473 syntax newlines | |
474 ;; shut this up | |
475 (c-echo-syntactic-information-p nil)) | |
476 (if (or literal | |
477 arg | |
478 (not (looking-at "[ \t]*$"))) | |
479 (self-insert-command (prefix-numeric-value arg)) | |
480 ;; insert the colon, then do any specified cleanups | |
481 (self-insert-command (prefix-numeric-value arg)) | |
482 (let ((pos (- (point-max) (point))) | |
483 (here (point))) | |
484 (if (and c-auto-newline | |
485 (memq 'scope-operator c-cleanup-list) | |
486 (eq (char-before) ?:) | |
487 (progn | |
488 (forward-char -1) | |
489 (skip-chars-backward " \t\n") | |
490 (eq (char-before) ?:)) | |
491 (not (c-in-literal)) | |
492 (not (eq (char-after (- (point) 2)) ?:))) | |
493 (delete-region (point) (1- here))) | |
494 (goto-char (- (point-max) pos))) | |
495 ;; lets do some special stuff with the colon character | |
496 (setq syntax (c-guess-basic-syntax) | |
497 ;; some language elements can only be determined by | |
498 ;; checking the following line. Lets first look for ones | |
499 ;; that can be found when looking on the line with the | |
500 ;; colon | |
501 newlines | |
502 (and c-auto-newline | |
503 (or (c-lookup-lists '(case-label label access-label) | |
504 syntax c-hanging-colons-alist) | |
505 (c-lookup-lists '(member-init-intro inher-intro) | |
506 (prog2 | |
507 (insert "\n") | |
508 (c-guess-basic-syntax) | |
509 (delete-char -1)) | |
510 c-hanging-colons-alist)))) | |
511 ;; indent the current line | |
512 (c-indent-line syntax) | |
513 ;; does a newline go before the colon? Watch out for already | |
514 ;; non-hung colons. However, we don't unhang them because that | |
515 ;; would be a cleanup (and anti-social). | |
516 (if (and (memq 'before newlines) | |
517 (save-excursion | |
518 (skip-chars-backward ": \t") | |
519 (not (bolp)))) | |
520 (let ((pos (- (point-max) (point)))) | |
521 (forward-char -1) | |
522 (newline) | |
523 (c-indent-line) | |
524 (goto-char (- (point-max) pos)))) | |
525 ;; does a newline go after the colon? | |
526 (if (memq 'after (cdr-safe newlines)) | |
527 (progn | |
528 (newline) | |
529 (c-indent-line))) | |
530 ))) | |
531 | |
532 (defun c-electric-lt-gt (arg) | |
533 "Insert a less-than, or greater-than character. | |
534 When the auto-newline feature is turned on, as evidenced by the \"/a\" | |
535 or \"/ah\" string on the mode line, the line will be re-indented if | |
536 the character inserted is the second of a C++ style stream operator | |
537 and the buffer is in C++ mode. | |
538 | |
539 The line will also not be re-indented if a numeric argument is | |
540 supplied, or point is inside a literal." | |
541 (interactive "P") | |
542 (let ((indentp (and (not arg) | |
543 (eq (char-before) last-command-char) | |
544 (not (c-in-literal)))) | |
545 ;; shut this up | |
546 (c-echo-syntactic-information-p nil)) | |
547 (self-insert-command (prefix-numeric-value arg)) | |
548 (if indentp | |
549 (c-indent-line)))) | |
550 | |
551 | |
552 | |
553 ;; better movement routines for ThisStyleOfVariablesCommonInCPlusPlus | |
554 ;; originally contributed by Terry_Glanfield.Southern@rxuk.xerox.com | |
555 (defun c-forward-into-nomenclature (&optional arg) | |
556 "Move forward to end of a nomenclature section or word. | |
557 With arg, to it arg times." | |
558 (interactive "p") | |
559 (let ((case-fold-search nil)) | |
560 (if (> arg 0) | |
561 (re-search-forward "\\W*\\([A-Z]*[a-z0-9]*\\)" (point-max) t arg) | |
562 (while (and (< arg 0) | |
563 (re-search-backward | |
564 "\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)" | |
565 (point-min) 0)) | |
566 (forward-char 1) | |
567 (setq arg (1+ arg))))) | |
568 (c-keep-region-active)) | |
569 | |
570 (defun c-backward-into-nomenclature (&optional arg) | |
571 "Move backward to beginning of a nomenclature section or word. | |
572 With optional ARG, move that many times. If ARG is negative, move | |
573 forward." | |
574 (interactive "p") | |
575 (c-forward-into-nomenclature (- arg)) | |
576 (c-keep-region-active)) | |
577 | |
578 (defun c-scope-operator () | |
579 "Insert a double colon scope operator at point. | |
580 No indentation or other \"electric\" behavior is performed." | |
581 (interactive) | |
582 (insert "::")) | |
583 | |
584 | |
585 (defun c-beginning-of-statement (&optional count lim sentence-flag) | |
586 "Go to the beginning of the innermost C statement. | |
587 With prefix arg, go back N - 1 statements. If already at the | |
588 beginning of a statement then go to the beginning of the preceding | |
589 one. If within a string or comment, or next to a comment (only | |
590 whitespace between), move by sentences instead of statements. | |
591 | |
592 When called from a program, this function takes 3 optional args: the | |
593 repetition count, a buffer position limit which is the farthest back | |
594 to search, and a flag saying whether to do sentence motion when in a | |
595 comment." | |
596 (interactive (list (prefix-numeric-value current-prefix-arg) | |
597 nil t)) | |
598 (let ((here (point)) | |
599 (count (or count 1)) | |
600 (lim (or lim (c-point 'bod))) | |
601 state) | |
602 (save-excursion | |
603 (goto-char lim) | |
604 (setq state (parse-partial-sexp (point) here nil nil))) | |
605 (if (and sentence-flag | |
606 (or (nth 3 state) | |
607 (nth 4 state) | |
608 ; (looking-at (concat "[ \t]*" comment-start-skip)) | |
609 (save-excursion | |
610 (skip-chars-backward " \t") | |
611 (goto-char (- (point) 2)) | |
612 (looking-at "\\*/")))) | |
613 (forward-sentence (- count)) | |
614 (while (> count 0) | |
615 (c-beginning-of-statement-1 lim) | |
616 (setq count (1- count))) | |
617 (while (< count 0) | |
618 (c-end-of-statement-1) | |
619 (setq count (1+ count)))) | |
620 ;; its possible we've been left up-buf of lim | |
621 (goto-char (max (point) lim)) | |
622 ) | |
623 (c-keep-region-active)) | |
624 | |
625 (defun c-end-of-statement (&optional count lim sentence-flag) | |
626 "Go to the end of the innermost C statement. | |
627 | |
628 With prefix arg, go forward N - 1 statements. Move forward to end of | |
629 the next statement if already at end. If within a string or comment, | |
630 move by sentences instead of statements. | |
631 | |
632 When called from a program, this function takes 3 optional args: the | |
633 repetition count, a buffer position limit which is the farthest back | |
634 to search, and a flag saying whether to do sentence motion when in a | |
635 comment." | |
636 (interactive (list (prefix-numeric-value current-prefix-arg) | |
637 nil t)) | |
638 (c-beginning-of-statement (- (or count 1)) lim sentence-flag) | |
639 (c-keep-region-active)) | |
640 | |
641 | |
642 ;; set up electric character functions to work with pending-del, | |
643 ;; (a.k.a. delsel) mode. All symbols get the t value except | |
18843
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
644 ;; the functions which delete, which gets 'supersede. |
18720 | 645 (mapcar |
646 (function | |
647 (lambda (sym) | |
648 (put sym 'delete-selection t) ; for delsel (Emacs) | |
649 (put sym 'pending-delete t))) ; for pending-del (XEmacs) | |
650 '(c-electric-pound | |
651 c-electric-brace | |
652 c-electric-slash | |
653 c-electric-star | |
654 c-electric-semi&comma | |
655 c-electric-lt-gt | |
656 c-electric-colon)) | |
18843
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
657 (put 'c-electric-delete 'delete-selection 'supersede) ; delsel |
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
658 (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del |
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
659 (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel |
6e04c0670f55
Require cc-defs for the c-add-syntax macro.
Richard M. Stallman <rms@gnu.org>
parents:
18720
diff
changeset
|
660 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del |
18720 | 661 |
662 | |
663 ;; This is used by indent-for-comment to decide how much to indent a | |
664 ;; comment in C code based on its context. | |
665 (defun c-comment-indent () | |
666 (if (looking-at (concat "^\\(" c-comment-start-regexp "\\)")) | |
667 0 ;Existing comment at bol stays there. | |
668 (let ((opoint (point)) | |
669 placeholder) | |
670 (save-excursion | |
671 (beginning-of-line) | |
672 (cond | |
673 ;; CASE 1: A comment following a solitary close-brace should | |
674 ;; have only one space. | |
675 ((looking-at (concat "[ \t]*}[ \t]*\\($\\|" | |
676 c-comment-start-regexp | |
677 "\\)")) | |
678 (search-forward "}") | |
679 (1+ (current-column))) | |
680 ;; CASE 2: 2 spaces after #endif | |
681 ((or (looking-at "^#[ \t]*endif[ \t]*") | |
682 (looking-at "^#[ \t]*else[ \t]*")) | |
683 7) | |
684 ;; CASE 3: when comment-column is nil, calculate the offset | |
685 ;; according to c-offsets-alist. E.g. identical to hitting | |
686 ;; TAB. | |
687 ((and c-indent-comments-syntactically-p | |
688 (save-excursion | |
689 (skip-chars-forward " \t") | |
690 (or (looking-at comment-start) | |
691 (eolp)))) | |
692 (let ((syntax (c-guess-basic-syntax))) | |
693 ;; BOGOSITY ALERT: if we're looking at the eol, its | |
694 ;; because indent-for-comment hasn't put the comment-start | |
695 ;; in the buffer yet. this will screw up the syntactic | |
696 ;; analysis so we kludge in the necessary info. Another | |
697 ;; kludge is that if we're at the bol, then we really want | |
698 ;; to ignore any anchoring as specified by | |
699 ;; c-comment-only-line-offset since it doesn't apply here. | |
700 (if (save-excursion | |
701 (beginning-of-line) | |
702 (skip-chars-forward " \t") | |
703 (eolp)) | |
704 (c-add-syntax 'comment-intro)) | |
705 (let ((c-comment-only-line-offset | |
706 (if (consp c-comment-only-line-offset) | |
707 c-comment-only-line-offset | |
708 (cons c-comment-only-line-offset | |
709 c-comment-only-line-offset)))) | |
710 (apply '+ (mapcar 'c-get-offset syntax))))) | |
711 ;; CASE 4: use comment-column if previous line is a | |
712 ;; comment-only line indented to the left of comment-column | |
713 ((save-excursion | |
714 (beginning-of-line) | |
715 (and (not (bobp)) | |
716 (forward-line -1)) | |
717 (skip-chars-forward " \t") | |
718 (prog1 | |
719 (looking-at c-comment-start-regexp) | |
720 (setq placeholder (point)))) | |
721 (goto-char placeholder) | |
722 (if (< (current-column) comment-column) | |
723 comment-column | |
724 (current-column))) | |
725 ;; CASE 5: If comment-column is 0, and nothing but space | |
726 ;; before the comment, align it at 0 rather than 1. | |
727 ((progn | |
728 (goto-char opoint) | |
729 (skip-chars-backward " \t") | |
730 (and (= comment-column 0) (bolp))) | |
731 0) | |
732 ;; CASE 6: indent at comment column except leave at least one | |
733 ;; space. | |
734 (t (max (1+ (current-column)) | |
735 comment-column)) | |
736 ))))) | |
737 | |
738 ;; used by outline-minor-mode | |
739 (defun c-outline-level () | |
740 (save-excursion | |
741 (skip-chars-forward "\t ") | |
742 (current-column))) | |
743 | |
744 | |
745 (defun c-up-conditional (count) | |
746 "Move back to the containing preprocessor conditional, leaving mark behind. | |
747 A prefix argument acts as a repeat count. With a negative argument, | |
748 move forward to the end of the containing preprocessor conditional. | |
749 When going backwards, `#elif' is treated like `#else' followed by | |
750 `#if'. When going forwards, `#elif' is ignored." | |
751 (interactive "p") | |
752 (c-forward-conditional (- count) t) | |
753 (c-keep-region-active)) | |
754 | |
755 (defun c-backward-conditional (count &optional up-flag) | |
756 "Move back across a preprocessor conditional, leaving mark behind. | |
757 A prefix argument acts as a repeat count. With a negative argument, | |
758 move forward across a preprocessor conditional." | |
759 (interactive "p") | |
760 (c-forward-conditional (- count) up-flag) | |
761 (c-keep-region-active)) | |
762 | |
763 (defun c-forward-conditional (count &optional up-flag) | |
764 "Move forward across a preprocessor conditional, leaving mark behind. | |
765 A prefix argument acts as a repeat count. With a negative argument, | |
766 move backward across a preprocessor conditional." | |
767 (interactive "p") | |
768 (let* ((forward (> count 0)) | |
769 (increment (if forward -1 1)) | |
770 (search-function (if forward 're-search-forward 're-search-backward)) | |
771 (new)) | |
772 (save-excursion | |
773 (while (/= count 0) | |
774 (let ((depth (if up-flag 0 -1)) found) | |
775 (save-excursion | |
776 ;; Find the "next" significant line in the proper direction. | |
777 (while (and (not found) | |
778 ;; Rather than searching for a # sign that | |
779 ;; comes at the beginning of a line aside from | |
780 ;; whitespace, search first for a string | |
781 ;; starting with # sign. Then verify what | |
782 ;; precedes it. This is faster on account of | |
783 ;; the fastmap feature of the regexp matcher. | |
784 (funcall search-function | |
785 "#[ \t]*\\(if\\|elif\\|endif\\)" | |
786 nil t)) | |
787 (beginning-of-line) | |
788 ;; Now verify it is really a preproc line. | |
789 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)") | |
790 (let ((prev depth)) | |
791 ;; Update depth according to what we found. | |
792 (beginning-of-line) | |
793 (cond ((looking-at "[ \t]*#[ \t]*endif") | |
794 (setq depth (+ depth increment))) | |
795 ((looking-at "[ \t]*#[ \t]*elif") | |
796 (if (and forward (= depth 0)) | |
797 (setq found (point)))) | |
798 (t (setq depth (- depth increment)))) | |
799 ;; If we are trying to move across, and we find an | |
800 ;; end before we find a beginning, get an error. | |
801 (if (and (< prev 0) (< depth prev)) | |
802 (error (if forward | |
803 "No following conditional at this level" | |
804 "No previous conditional at this level"))) | |
805 ;; When searching forward, start from next line so | |
806 ;; that we don't find the same line again. | |
807 (if forward (forward-line 1)) | |
808 ;; If this line exits a level of conditional, exit | |
809 ;; inner loop. | |
810 (if (< depth 0) | |
811 (setq found (point)))) | |
812 ;; else | |
813 (if forward (forward-line 1)) | |
814 ))) | |
815 (or found | |
816 (error "No containing preprocessor conditional")) | |
817 (goto-char (setq new found))) | |
818 (setq count (+ count increment)))) | |
819 (push-mark) | |
820 (goto-char new)) | |
821 (c-keep-region-active)) | |
822 | |
823 | |
824 ;; commands to indent lines, regions, defuns, and expressions | |
825 (defun c-indent-command (&optional whole-exp) | |
826 "Indent current line as C code, and/or insert some whitespace. | |
827 | |
828 If `c-tab-always-indent' is t, always just indent the current line. | |
829 If nil, indent the current line only if point is at the left margin or | |
830 in the line's indentation; otherwise insert some whitespace[*]. If | |
831 other than nil or t, then some whitespace[*] is inserted only within | |
832 literals (comments and strings) and inside preprocessor directives, | |
833 but the line is always reindented. | |
834 | |
835 A numeric argument, regardless of its value, means indent rigidly all | |
836 the lines of the expression starting after point so that this line | |
837 becomes properly indented. The relative indentation among the lines | |
838 of the expression are preserved. | |
839 | |
840 [*] The amount and kind of whitespace inserted is controlled by the | |
841 variable `c-insert-tab-function', which is called to do the actual | |
842 insertion of whitespace. Normally the function in this variable | |
843 just inserts a tab character, or the equivalent number of spaces, | |
844 depending on the variable `indent-tabs-mode'." | |
845 | |
846 (interactive "P") | |
847 (let ((bod (c-point 'bod))) | |
848 (if whole-exp | |
849 ;; If arg, always indent this line as C | |
850 ;; and shift remaining lines of expression the same amount. | |
851 (let ((shift-amt (c-indent-line)) | |
852 beg end) | |
853 (save-excursion | |
854 (if (eq c-tab-always-indent t) | |
855 (beginning-of-line)) | |
856 (setq beg (point)) | |
857 (forward-sexp 1) | |
858 (setq end (point)) | |
859 (goto-char beg) | |
860 (forward-line 1) | |
861 (setq beg (point))) | |
862 (if (> end beg) | |
863 (indent-code-rigidly beg end (- shift-amt) "#"))) | |
864 ;; No arg supplied, use c-tab-always-indent to determine | |
865 ;; behavior | |
866 (cond | |
867 ;; CASE 1: indent when at column zero or in lines indentation, | |
868 ;; otherwise insert a tab | |
869 ((not c-tab-always-indent) | |
870 (if (save-excursion | |
871 (skip-chars-backward " \t") | |
872 (not (bolp))) | |
873 (funcall c-insert-tab-function) | |
874 (c-indent-line))) | |
875 ;; CASE 2: just indent the line | |
876 ((eq c-tab-always-indent t) | |
877 (c-indent-line)) | |
878 ;; CASE 3: if in a literal, insert a tab, but always indent the | |
879 ;; line | |
880 (t | |
881 (if (c-in-literal bod) | |
882 (funcall c-insert-tab-function)) | |
883 (c-indent-line) | |
884 ))))) | |
885 | |
886 (defun c-indent-exp (&optional shutup-p) | |
887 "Indent each line in balanced expression following point. | |
888 Optional SHUTUP-P if non-nil, inhibits message printing and error checking." | |
889 (interactive "P") | |
890 (let ((here (point)) | |
891 end progress-p) | |
892 (unwind-protect | |
893 (let ((c-echo-syntactic-information-p nil) ;keep quiet for speed | |
894 (start (progn | |
895 ;; try to be smarter about finding the range of | |
896 ;; lines to indent. skip all following | |
897 ;; whitespace. failing that, try to find any | |
898 ;; opening brace on the current line | |
899 (skip-chars-forward " \t\n") | |
900 (if (memq (char-after) '(?\( ?\[ ?\{)) | |
901 (point) | |
902 (let ((state (parse-partial-sexp (point) | |
903 (c-point 'eol)))) | |
904 (and (nth 1 state) | |
905 (goto-char (nth 1 state)) | |
906 (memq (char-after) '(?\( ?\[ ?\{)) | |
907 (point))))))) | |
908 ;; find balanced expression end | |
909 (setq end (and (c-safe (progn (forward-sexp 1) t)) | |
910 (point-marker))) | |
911 ;; sanity check | |
912 (and (not start) | |
913 (not shutup-p) | |
914 (error "Cannot find start of balanced expression to indent.")) | |
915 (and (not end) | |
916 (not shutup-p) | |
917 (error "Cannot find end of balanced expression to indent.")) | |
918 (c-progress-init start end 'c-indent-exp) | |
919 (setq progress-p t) | |
920 (goto-char start) | |
921 (beginning-of-line) | |
922 (while (< (point) end) | |
923 (if (not (looking-at "[ \t]*$")) | |
924 (c-indent-line)) | |
925 (c-progress-update) | |
926 (forward-line 1))) | |
927 ;; make sure marker is deleted | |
928 (and end | |
929 (set-marker end nil)) | |
930 (and progress-p | |
931 (c-progress-fini 'c-indent-exp)) | |
932 (goto-char here)))) | |
933 | |
934 (defun c-indent-defun () | |
935 "Re-indents the current top-level function def, struct or class declaration." | |
936 (interactive) | |
937 (let ((here (point-marker)) | |
938 (c-echo-syntactic-information-p nil) | |
939 (brace (c-least-enclosing-brace (c-parse-state)))) | |
940 (if brace | |
941 (goto-char brace) | |
942 (beginning-of-defun)) | |
943 ;; if we're sitting at b-o-b, it might be because there was no | |
944 ;; least enclosing brace and we were sitting on the defun's open | |
945 ;; brace. | |
946 (if (and (bobp) (not (eq (char-after) ?\{))) | |
947 (goto-char here)) | |
948 ;; if defun-prompt-regexp is non-nil, b-o-d might not leave us at | |
949 ;; the open brace. I consider this an Emacs bug. | |
950 (and (boundp 'defun-prompt-regexp) | |
951 defun-prompt-regexp | |
952 (looking-at defun-prompt-regexp) | |
953 (goto-char (match-end 0))) | |
954 ;; catch all errors in c-indent-exp so we can 1. give more | |
955 ;; meaningful error message, and 2. restore point | |
956 (unwind-protect | |
957 (c-indent-exp) | |
958 (goto-char here) | |
959 (set-marker here nil)))) | |
960 | |
961 (defun c-indent-region (start end) | |
962 ;; Indent every line whose first char is between START and END inclusive. | |
963 (save-excursion | |
964 (goto-char start) | |
965 ;; Advance to first nonblank line. | |
966 (skip-chars-forward " \t\n") | |
967 (beginning-of-line) | |
968 (let (endmark) | |
969 (unwind-protect | |
970 (let ((c-tab-always-indent t) | |
971 ;; shut up any echo msgs on indiv lines | |
972 (c-echo-syntactic-information-p nil) | |
973 fence) | |
974 (c-progress-init start end 'c-indent-region) | |
975 (setq endmark (copy-marker end)) | |
976 (while (and (bolp) | |
977 (not (eobp)) | |
978 (< (point) endmark)) | |
979 ;; update progress | |
980 (c-progress-update) | |
981 ;; Indent one line as with TAB. | |
982 (let (nextline sexpend sexpbeg) | |
983 ;; skip blank lines | |
984 (skip-chars-forward " \t\n") | |
985 (beginning-of-line) | |
986 ;; indent the current line | |
987 (c-indent-line) | |
988 (setq fence (point)) | |
989 (if (save-excursion | |
990 (beginning-of-line) | |
991 (looking-at "[ \t]*#")) | |
992 (forward-line 1) | |
993 (save-excursion | |
994 ;; Find beginning of following line. | |
995 (setq nextline (c-point 'bonl)) | |
996 ;; Find first beginning-of-sexp for sexp extending past | |
997 ;; this line. | |
998 (beginning-of-line) | |
999 (while (< (point) nextline) | |
1000 (condition-case nil | |
1001 (progn | |
1002 (forward-sexp 1) | |
1003 (setq sexpend (point))) | |
1004 (error (setq sexpend nil) | |
1005 (goto-char nextline))) | |
1006 (c-forward-syntactic-ws)) | |
1007 (if sexpend | |
1008 (progn | |
1009 ;; make sure the sexp we found really starts on the | |
1010 ;; current line and extends past it | |
1011 (goto-char sexpend) | |
1012 (setq sexpend (point-marker)) | |
1013 (c-safe (backward-sexp 1)) | |
1014 (setq sexpbeg (point)))) | |
1015 (if (and sexpbeg (< sexpbeg fence)) | |
1016 (setq sexpbeg fence))) | |
1017 ;; check to see if the next line starts a | |
1018 ;; comment-only line | |
1019 (save-excursion | |
1020 (forward-line 1) | |
1021 (skip-chars-forward " \t") | |
1022 (if (looking-at c-comment-start-regexp) | |
1023 (setq sexpbeg (c-point 'bol)))) | |
1024 ;; If that sexp ends within the region, indent it all at | |
1025 ;; once, fast. | |
1026 (condition-case nil | |
1027 (if (and sexpend | |
1028 (> sexpend nextline) | |
1029 (<= sexpend endmark)) | |
1030 (progn | |
1031 (goto-char sexpbeg) | |
1032 (c-indent-exp 'shutup) | |
1033 (c-progress-update) | |
1034 (goto-char sexpend))) | |
1035 (error | |
1036 (goto-char sexpbeg) | |
1037 (c-indent-line))) | |
1038 ;; Move to following line and try again. | |
1039 (and sexpend | |
1040 (markerp sexpend) | |
1041 (set-marker sexpend nil)) | |
1042 (forward-line 1) | |
1043 (setq fence (point)))))) | |
1044 (set-marker endmark nil) | |
1045 (c-progress-fini 'c-indent-region) | |
1046 (c-echo-parsing-error) | |
1047 )))) | |
1048 | |
1049 (defun c-mark-function () | |
1050 "Put mark at end of a C, C++, or Objective-C defun, point at beginning." | |
1051 (interactive) | |
1052 (let ((here (point)) | |
1053 ;; there should be a c-point position for 'eod | |
1054 (eod (save-excursion (end-of-defun) (point))) | |
1055 (state (c-parse-state)) | |
1056 brace) | |
1057 (while state | |
1058 (setq brace (car state)) | |
1059 (if (consp brace) | |
1060 (goto-char (cdr brace)) | |
1061 (goto-char brace)) | |
1062 (setq state (cdr state))) | |
1063 (if (eq (char-after) ?{) | |
1064 (progn | |
1065 (forward-line -1) | |
1066 (while (not (or (bobp) | |
1067 (looking-at "[ \t]*$"))) | |
1068 (forward-line -1))) | |
1069 (forward-line 1) | |
1070 (skip-chars-forward " \t\n")) | |
1071 (push-mark here) | |
1072 (push-mark eod nil t))) | |
1073 | |
1074 | |
1075 ;; for progress reporting | |
1076 (defvar c-progress-info nil) | |
1077 | |
1078 (defun c-progress-init (start end context) | |
1079 ;; start the progress update messages. if this emacs doesn't have a | |
1080 ;; built-in timer, just be dumb about it | |
1081 (if (not (fboundp 'current-time)) | |
1082 (message "indenting region... (this may take a while)") | |
1083 ;; if progress has already been initialized, do nothing. otherwise | |
1084 ;; initialize the counter with a vector of: | |
1085 ;; [start end lastsec context] | |
1086 (if c-progress-info | |
1087 () | |
1088 (setq c-progress-info (vector start | |
1089 (save-excursion | |
1090 (goto-char end) | |
1091 (point-marker)) | |
1092 (nth 1 (current-time)) | |
1093 context)) | |
1094 (message "indenting region...")))) | |
1095 | |
1096 (defun c-progress-update () | |
1097 ;; update progress | |
1098 (if (not (and c-progress-info c-progress-interval)) | |
1099 nil | |
1100 (let ((now (nth 1 (current-time))) | |
1101 (start (aref c-progress-info 0)) | |
1102 (end (aref c-progress-info 1)) | |
1103 (lastsecs (aref c-progress-info 2))) | |
1104 ;; should we update? currently, update happens every 2 seconds, | |
1105 ;; what's the right value? | |
1106 (if (< c-progress-interval (- now lastsecs)) | |
1107 (progn | |
1108 (message "indenting region... (%d%% complete)" | |
1109 (/ (* 100 (- (point) start)) (- end start))) | |
1110 (aset c-progress-info 2 now))) | |
1111 ))) | |
1112 | |
1113 (defun c-progress-fini (context) | |
1114 ;; finished | |
1115 (if (or (eq context (aref c-progress-info 3)) | |
1116 (eq context t)) | |
1117 (progn | |
1118 (set-marker (aref c-progress-info 1) nil) | |
1119 (setq c-progress-info nil) | |
1120 (message "indenting region...done")))) | |
1121 | |
1122 | |
1123 | |
1124 ;;; This page handles insertion and removal of backslashes for C macros. | |
1125 | |
1126 (defun c-backslash-region (from to delete-flag) | |
1127 "Insert, align, or delete end-of-line backslashes on the lines in the region. | |
1128 With no argument, inserts backslashes and aligns existing backslashes. | |
1129 With an argument, deletes the backslashes. | |
1130 | |
1131 This function does not modify blank lines at the start of the region. | |
1132 If the region ends at the start of a line, it always deletes the | |
1133 backslash (if any) at the end of the previous line. | |
1134 | |
1135 You can put the region around an entire macro definition and use this | |
1136 command to conveniently insert and align the necessary backslashes." | |
1137 (interactive "r\nP") | |
1138 (save-excursion | |
1139 (goto-char from) | |
1140 (let ((column c-backslash-column) | |
1141 (endmark (make-marker))) | |
1142 (move-marker endmark to) | |
1143 ;; Compute the smallest column number past the ends of all the lines. | |
1144 (if (not delete-flag) | |
1145 (while (< (point) to) | |
1146 (end-of-line) | |
1147 (if (eq (char-before) ?\\) | |
1148 (progn (forward-char -1) | |
1149 (skip-chars-backward " \t"))) | |
1150 (setq column (max column (1+ (current-column)))) | |
1151 (forward-line 1))) | |
1152 ;; Adjust upward to a tab column, if that doesn't push past the margin. | |
1153 (if (> (% column tab-width) 0) | |
1154 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width))) | |
1155 (if (< adjusted (window-width)) | |
1156 (setq column adjusted)))) | |
1157 ;; Don't modify blank lines at start of region. | |
1158 (goto-char from) | |
1159 (while (and (< (point) endmark) (eolp)) | |
1160 (forward-line 1)) | |
1161 ;; Add or remove backslashes on all the lines. | |
1162 (while (< (point) endmark) | |
1163 (if (and (not delete-flag) | |
1164 ;; Un-backslashify the last line | |
1165 ;; if the region ends right at the start of the next line. | |
1166 (save-excursion | |
1167 (forward-line 1) | |
1168 (< (point) endmark))) | |
1169 (c-append-backslash column) | |
1170 (c-delete-backslash)) | |
1171 (forward-line 1)) | |
1172 (move-marker endmark nil))) | |
1173 (c-keep-region-active)) | |
1174 | |
1175 (defun c-append-backslash (column) | |
1176 (end-of-line) | |
1177 (if (eq (char-before) ?\\) | |
1178 (progn (forward-char -1) | |
1179 (delete-horizontal-space) | |
1180 (indent-to column)) | |
1181 (indent-to column) | |
1182 (insert "\\"))) | |
1183 | |
1184 (defun c-delete-backslash () | |
1185 (end-of-line) | |
1186 (or (bolp) | |
1187 (progn | |
1188 (forward-char -1) | |
1189 (if (looking-at "\\\\") | |
1190 (delete-region (1+ (point)) | |
1191 (progn (skip-chars-backward " \t") (point))))))) | |
1192 | |
1193 | |
1194 (defun c-fill-paragraph (&optional arg) | |
1195 "Like \\[fill-paragraph] but handles C and C++ style comments. | |
1196 If any of the current line is a comment or within a comment, | |
1197 fill the comment or the paragraph of it that point is in, | |
1198 preserving the comment indentation or line-starting decorations. | |
1199 | |
1200 Optional prefix ARG means justify paragraph as well." | |
1201 (interactive "P") | |
1202 (let* (comment-start-place | |
1203 (first-line | |
1204 ;; Check for obvious entry to comment. | |
1205 (save-excursion | |
1206 (beginning-of-line) | |
1207 (skip-chars-forward " \t\n") | |
1208 (and (looking-at comment-start-skip) | |
1209 (setq comment-start-place (point))))) | |
1210 (re1 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")) | |
1211 (if (and c-double-slash-is-comments-p | |
1212 (save-excursion | |
1213 (beginning-of-line) | |
1214 (looking-at ".*//"))) | |
1215 (let ((fill-prefix fill-prefix) | |
1216 ;; Lines containing just a comment start or just an end | |
1217 ;; should not be filled into paragraphs they are next | |
1218 ;; to. | |
1219 (paragraph-start (concat paragraph-start re1)) | |
1220 (paragraph-separate (concat paragraph-separate re1))) | |
1221 (save-excursion | |
1222 (beginning-of-line) | |
1223 ;; Move up to first line of this comment. | |
1224 (while (and (not (bobp)) | |
1225 (looking-at "[ \t]*//[ \t]*[^ \t\n]")) | |
1226 (forward-line -1)) | |
1227 (if (not (looking-at ".*//[ \t]*[^ \t\n]")) | |
1228 (forward-line 1)) | |
1229 ;; Find the comment start in this line. | |
1230 (re-search-forward "[ \t]*//[ \t]*") | |
1231 ;; Set the fill-prefix to be what all lines except the first | |
1232 ;; should start with. But do not alter a user set fill-prefix. | |
1233 (if (null fill-prefix) | |
1234 (setq fill-prefix (buffer-substring (match-beginning 0) | |
1235 (match-end 0)))) | |
1236 (save-restriction | |
1237 ;; Narrow down to just the lines of this comment. | |
1238 (narrow-to-region (c-point 'bol) | |
1239 (save-excursion | |
1240 (forward-line 1) | |
1241 (while (looking-at fill-prefix) | |
1242 (forward-line 1)) | |
1243 (point))) | |
1244 (fill-paragraph arg) | |
1245 t))) | |
1246 ;; else C style comments | |
1247 (if (or first-line | |
1248 ;; t if we enter a comment between start of function and | |
1249 ;; this line. | |
1250 (eq (c-in-literal) 'c) | |
1251 ;; t if this line contains a comment starter. | |
1252 (setq first-line | |
1253 (save-excursion | |
1254 (beginning-of-line) | |
1255 (prog1 | |
1256 (re-search-forward comment-start-skip | |
1257 (save-excursion (end-of-line) | |
1258 (point)) | |
1259 t) | |
1260 (setq comment-start-place (point)))))) | |
1261 ;; Inside a comment: fill one comment paragraph. | |
1262 (let ((fill-prefix | |
1263 ;; The prefix for each line of this paragraph | |
1264 ;; is the appropriate part of the start of this line, | |
1265 ;; up to the column at which text should be indented. | |
1266 (save-excursion | |
1267 (beginning-of-line) | |
1268 (if (looking-at "[ \t]*/\\*.*\\*/") | |
1269 (progn (re-search-forward comment-start-skip) | |
1270 (make-string (current-column) ?\ )) | |
1271 (if first-line (forward-line 1)) | |
1272 | |
1273 (let ((line-width (progn (end-of-line) (current-column)))) | |
1274 (beginning-of-line) | |
1275 (prog1 | |
1276 (buffer-substring | |
1277 (point) | |
1278 | |
1279 ;; How shall we decide where the end of the | |
1280 ;; fill-prefix is? | |
1281 (progn | |
1282 (beginning-of-line) | |
1283 (skip-chars-forward " \t*" (c-point 'eol)) | |
1284 ;; kludge alert, watch out for */, in | |
1285 ;; which case fill-prefix should *not* | |
1286 ;; be "*"! | |
1287 (if (and (eq (char-after) ?/) | |
1288 (eq (char-before) ?*)) | |
1289 (forward-char -1)) | |
1290 (point))) | |
1291 | |
1292 ;; If the comment is only one line followed | |
1293 ;; by a blank line, calling move-to-column | |
1294 ;; above may have added some spaces and tabs | |
1295 ;; to the end of the line; the fill-paragraph | |
1296 ;; function will then delete it and the | |
1297 ;; newline following it, so we'll lose a | |
1298 ;; blank line when we shouldn't. So delete | |
1299 ;; anything move-to-column added to the end | |
1300 ;; of the line. We record the line width | |
1301 ;; instead of the position of the old line | |
1302 ;; end because move-to-column might break a | |
1303 ;; tab into spaces, and the new characters | |
1304 ;; introduced there shouldn't be deleted. | |
1305 | |
1306 ;; If you can see a better way to do this, | |
1307 ;; please make the change. This seems very | |
1308 ;; messy to me. | |
1309 (delete-region (progn (move-to-column line-width) | |
1310 (point)) | |
1311 (progn (end-of-line) (point)))))))) | |
1312 | |
1313 ;; Lines containing just a comment start or just an end | |
1314 ;; should not be filled into paragraphs they are next | |
1315 ;; to. | |
1316 (paragraph-start (concat paragraph-start re1)) | |
1317 (paragraph-separate (concat paragraph-separate re1)) | |
1318 (chars-to-delete 0) | |
1319 ) | |
1320 (save-restriction | |
1321 ;; Don't fill the comment together with the code | |
1322 ;; following it. So temporarily exclude everything | |
1323 ;; before the comment start, and everything after the | |
1324 ;; line where the comment ends. If comment-start-place | |
1325 ;; is non-nil, the comment starter is there. Otherwise, | |
1326 ;; point is inside the comment. | |
1327 (narrow-to-region (save-excursion | |
1328 (if comment-start-place | |
1329 (goto-char comment-start-place) | |
1330 (search-backward "/*")) | |
1331 (if (and (not c-hanging-comment-starter-p) | |
1332 (looking-at | |
1333 (concat c-comment-start-regexp | |
1334 "[ \t]*$"))) | |
1335 (forward-line 1)) | |
1336 ;; Protect text before the comment | |
1337 ;; start by excluding it. Add | |
1338 ;; spaces to bring back proper | |
1339 ;; indentation of that point. | |
1340 (let ((column (current-column))) | |
1341 (prog1 (point) | |
1342 (setq chars-to-delete column) | |
1343 (insert-char ?\ column)))) | |
1344 (save-excursion | |
1345 (if comment-start-place | |
1346 (goto-char (+ comment-start-place 2))) | |
1347 (search-forward "*/" nil 'move) | |
1348 (forward-line 1) | |
1349 (point))) | |
1350 (fill-paragraph arg) | |
1351 (save-excursion | |
1352 ;; Delete the chars we inserted to avoid clobbering | |
1353 ;; the stuff before the comment start. | |
1354 (goto-char (point-min)) | |
1355 (if (> chars-to-delete 0) | |
1356 (delete-region (point) (+ (point) chars-to-delete))) | |
1357 ;; Find the comment ender (should be on last line of | |
1358 ;; buffer, given the narrowing) and don't leave it on | |
1359 ;; its own line, unless that's the style that's desired. | |
1360 (goto-char (point-max)) | |
1361 (forward-line -1) | |
1362 (search-forward "*/" nil 'move) | |
1363 (beginning-of-line) | |
1364 (if (and c-hanging-comment-ender-p | |
1365 (looking-at "[ \t]*\\*/")) | |
1366 ;(delete-indentation))))) | |
1367 (let ((fill-column (+ fill-column 9999))) | |
1368 (forward-line -1) | |
1369 (fill-region-as-paragraph (point) (point-max)))))) | |
1370 t))))) | |
1371 | |
1372 | |
1373 (provide 'cc-cmds) | |
1374 ;;; cc-cmds.el ends here |