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