165
|
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)
|
171
|
10 ;; Version: 5.12
|
165
|
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
|
173
|
196 (preserve-p (eq ? (char-syntax (char-before))))
|
165
|
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))
|
171
|
235 (memq (char-before) '(?\) ?}))
|
165
|
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)
|
171
|
294 (eq last-command-char ?\})
|
165
|
295 (c-intersect-lists '(defun-close class-close inline-close)
|
|
296 syntax)
|
|
297 (progn
|
|
298 (forward-char -1)
|
|
299 (skip-chars-backward " \t\n")
|
171
|
300 (eq (char-before) ?\{))
|
165
|
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)
|
171
|
307 (eq last-command-char ?\{)
|
165
|
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)
|
171
|
320 (eq last-command-char ?\{)
|
165
|
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))
|
171
|
341 (which (if (eq (char-after bufpos) ?{) 'open 'close))
|
165
|
342 (c-state-cache (c-hack-state bufpos which c-state-cache)))
|
|
343 (c-indent-line))))
|
|
344 ;; blink the paren
|
171
|
345 (and (eq last-command-char ?\})
|
165
|
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)
|
171
|
360 (eq (char-before) ?/)
|
|
361 (eq last-command-char ?/)
|
165
|
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))
|
171
|
381 (eq (char-before) ?*)
|
165
|
382 (save-excursion
|
|
383 (forward-char -1)
|
|
384 (skip-chars-backward "*")
|
171
|
385 (if (eq (char-before) ?/)
|
165
|
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
|
171
|
422 (eq last-command-char ?,)
|
165
|
423 (memq 'list-close-comma c-cleanup-list))
|
|
424 (and
|
171
|
425 (eq last-command-char ?\;)
|
165
|
426 (memq 'defun-close-semi c-cleanup-list)))
|
|
427 (progn
|
|
428 (forward-char -1)
|
|
429 (skip-chars-backward " \t\n")
|
171
|
430 (eq (char-before) ?}))
|
165
|
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)
|
171
|
483 (eq (char-before) ?:)
|
165
|
484 (progn
|
|
485 (forward-char -1)
|
|
486 (skip-chars-backward " \t\n")
|
171
|
487 (eq (char-before) ?:))
|
165
|
488 (not (c-in-literal))
|
171
|
489 (not (eq (char-after (- (point) 2)) ?:)))
|
165
|
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)
|
171
|
540 (eq (char-before) last-command-char)
|
165
|
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
|
173
|
641 ;; the functions which delete, which gets 'supersede.
|
165
|
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))
|
173
|
654 (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
|
|
655 (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
|
|
656 (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
|
|
657 (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
|
165
|
658
|
|
659
|
|
660 ;; This is used by indent-for-comment to decide how much to indent a
|
|
661 ;; comment in C code based on its context.
|
|
662 (defun c-comment-indent ()
|
|
663 (if (looking-at (concat "^\\(" c-comment-start-regexp "\\)"))
|
|
664 0 ;Existing comment at bol stays there.
|
|
665 (let ((opoint (point))
|
|
666 placeholder)
|
|
667 (save-excursion
|
|
668 (beginning-of-line)
|
|
669 (cond
|
|
670 ;; CASE 1: A comment following a solitary close-brace should
|
|
671 ;; have only one space.
|
|
672 ((looking-at (concat "[ \t]*}[ \t]*\\($\\|"
|
|
673 c-comment-start-regexp
|
|
674 "\\)"))
|
|
675 (search-forward "}")
|
|
676 (1+ (current-column)))
|
|
677 ;; CASE 2: 2 spaces after #endif
|
|
678 ((or (looking-at "^#[ \t]*endif[ \t]*")
|
|
679 (looking-at "^#[ \t]*else[ \t]*"))
|
|
680 7)
|
|
681 ;; CASE 3: when comment-column is nil, calculate the offset
|
|
682 ;; according to c-offsets-alist. E.g. identical to hitting
|
|
683 ;; TAB.
|
|
684 ((and c-indent-comments-syntactically-p
|
|
685 (save-excursion
|
|
686 (skip-chars-forward " \t")
|
|
687 (or (looking-at comment-start)
|
|
688 (eolp))))
|
|
689 (let ((syntax (c-guess-basic-syntax)))
|
|
690 ;; BOGOSITY ALERT: if we're looking at the eol, its
|
|
691 ;; because indent-for-comment hasn't put the comment-start
|
|
692 ;; in the buffer yet. this will screw up the syntactic
|
|
693 ;; analysis so we kludge in the necessary info. Another
|
|
694 ;; kludge is that if we're at the bol, then we really want
|
|
695 ;; to ignore any anchoring as specified by
|
|
696 ;; c-comment-only-line-offset since it doesn't apply here.
|
|
697 (if (save-excursion
|
|
698 (beginning-of-line)
|
|
699 (skip-chars-forward " \t")
|
|
700 (eolp))
|
|
701 (c-add-syntax 'comment-intro))
|
|
702 (let ((c-comment-only-line-offset
|
|
703 (if (consp c-comment-only-line-offset)
|
|
704 c-comment-only-line-offset
|
|
705 (cons c-comment-only-line-offset
|
|
706 c-comment-only-line-offset))))
|
|
707 (apply '+ (mapcar 'c-get-offset syntax)))))
|
|
708 ;; CASE 4: use comment-column if previous line is a
|
|
709 ;; comment-only line indented to the left of comment-column
|
|
710 ((save-excursion
|
|
711 (beginning-of-line)
|
|
712 (and (not (bobp))
|
|
713 (forward-line -1))
|
|
714 (skip-chars-forward " \t")
|
|
715 (prog1
|
|
716 (looking-at c-comment-start-regexp)
|
|
717 (setq placeholder (point))))
|
|
718 (goto-char placeholder)
|
|
719 (if (< (current-column) comment-column)
|
|
720 comment-column
|
|
721 (current-column)))
|
|
722 ;; CASE 5: If comment-column is 0, and nothing but space
|
|
723 ;; before the comment, align it at 0 rather than 1.
|
|
724 ((progn
|
|
725 (goto-char opoint)
|
|
726 (skip-chars-backward " \t")
|
|
727 (and (= comment-column 0) (bolp)))
|
|
728 0)
|
|
729 ;; CASE 6: indent at comment column except leave at least one
|
|
730 ;; space.
|
|
731 (t (max (1+ (current-column))
|
|
732 comment-column))
|
|
733 )))))
|
|
734
|
|
735 ;; used by outline-minor-mode
|
|
736 (defun c-outline-level ()
|
|
737 (save-excursion
|
|
738 (skip-chars-forward "\t ")
|
|
739 (current-column)))
|
|
740
|
|
741
|
|
742 (defun c-up-conditional (count)
|
|
743 "Move back to the containing preprocessor conditional, leaving mark behind.
|
|
744 A prefix argument acts as a repeat count. With a negative argument,
|
|
745 move forward to the end of the containing preprocessor conditional.
|
|
746 When going backwards, `#elif' is treated like `#else' followed by
|
|
747 `#if'. When going forwards, `#elif' is ignored."
|
|
748 (interactive "p")
|
|
749 (c-forward-conditional (- count) t)
|
|
750 (c-keep-region-active))
|
|
751
|
|
752 (defun c-backward-conditional (count &optional up-flag)
|
|
753 "Move back across a preprocessor conditional, leaving mark behind.
|
|
754 A prefix argument acts as a repeat count. With a negative argument,
|
|
755 move forward across a preprocessor conditional."
|
|
756 (interactive "p")
|
|
757 (c-forward-conditional (- count) up-flag)
|
|
758 (c-keep-region-active))
|
|
759
|
|
760 (defun c-forward-conditional (count &optional up-flag)
|
|
761 "Move forward across a preprocessor conditional, leaving mark behind.
|
|
762 A prefix argument acts as a repeat count. With a negative argument,
|
|
763 move backward across a preprocessor conditional."
|
|
764 (interactive "p")
|
|
765 (let* ((forward (> count 0))
|
|
766 (increment (if forward -1 1))
|
|
767 (search-function (if forward 're-search-forward 're-search-backward))
|
|
768 (new))
|
|
769 (save-excursion
|
|
770 (while (/= count 0)
|
|
771 (let ((depth (if up-flag 0 -1)) found)
|
|
772 (save-excursion
|
|
773 ;; Find the "next" significant line in the proper direction.
|
|
774 (while (and (not found)
|
|
775 ;; Rather than searching for a # sign that
|
|
776 ;; comes at the beginning of a line aside from
|
|
777 ;; whitespace, search first for a string
|
|
778 ;; starting with # sign. Then verify what
|
|
779 ;; precedes it. This is faster on account of
|
|
780 ;; the fastmap feature of the regexp matcher.
|
|
781 (funcall search-function
|
|
782 "#[ \t]*\\(if\\|elif\\|endif\\)"
|
|
783 nil t))
|
|
784 (beginning-of-line)
|
|
785 ;; Now verify it is really a preproc line.
|
|
786 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
|
|
787 (let ((prev depth))
|
|
788 ;; Update depth according to what we found.
|
|
789 (beginning-of-line)
|
|
790 (cond ((looking-at "[ \t]*#[ \t]*endif")
|
|
791 (setq depth (+ depth increment)))
|
|
792 ((looking-at "[ \t]*#[ \t]*elif")
|
|
793 (if (and forward (= depth 0))
|
|
794 (setq found (point))))
|
|
795 (t (setq depth (- depth increment))))
|
|
796 ;; If we are trying to move across, and we find an
|
|
797 ;; end before we find a beginning, get an error.
|
|
798 (if (and (< prev 0) (< depth prev))
|
|
799 (error (if forward
|
|
800 "No following conditional at this level"
|
|
801 "No previous conditional at this level")))
|
|
802 ;; When searching forward, start from next line so
|
|
803 ;; that we don't find the same line again.
|
|
804 (if forward (forward-line 1))
|
|
805 ;; If this line exits a level of conditional, exit
|
|
806 ;; inner loop.
|
|
807 (if (< depth 0)
|
|
808 (setq found (point))))
|
|
809 ;; else
|
|
810 (if forward (forward-line 1))
|
|
811 )))
|
|
812 (or found
|
|
813 (error "No containing preprocessor conditional"))
|
|
814 (goto-char (setq new found)))
|
|
815 (setq count (+ count increment))))
|
|
816 (push-mark)
|
|
817 (goto-char new))
|
|
818 (c-keep-region-active))
|
|
819
|
|
820
|
|
821 ;; commands to indent lines, regions, defuns, and expressions
|
|
822 (defun c-indent-command (&optional whole-exp)
|
|
823 "Indent current line as C code, and/or insert some whitespace.
|
|
824
|
|
825 If `c-tab-always-indent' is t, always just indent the current line.
|
|
826 If nil, indent the current line only if point is at the left margin or
|
|
827 in the line's indentation; otherwise insert some whitespace[*]. If
|
|
828 other than nil or t, then some whitespace[*] is inserted only within
|
|
829 literals (comments and strings) and inside preprocessor directives,
|
|
830 but the line is always reindented.
|
|
831
|
|
832 A numeric argument, regardless of its value, means indent rigidly all
|
|
833 the lines of the expression starting after point so that this line
|
|
834 becomes properly indented. The relative indentation among the lines
|
|
835 of the expression are preserved.
|
|
836
|
|
837 [*] The amount and kind of whitespace inserted is controlled by the
|
|
838 variable `c-insert-tab-function', which is called to do the actual
|
|
839 insertion of whitespace. Normally the function in this variable
|
|
840 just inserts a tab character, or the equivalent number of spaces,
|
|
841 depending on the variable `indent-tabs-mode'."
|
|
842
|
|
843 (interactive "P")
|
|
844 (let ((bod (c-point 'bod)))
|
|
845 (if whole-exp
|
|
846 ;; If arg, always indent this line as C
|
|
847 ;; and shift remaining lines of expression the same amount.
|
|
848 (let ((shift-amt (c-indent-line))
|
|
849 beg end)
|
|
850 (save-excursion
|
|
851 (if (eq c-tab-always-indent t)
|
|
852 (beginning-of-line))
|
|
853 (setq beg (point))
|
|
854 (forward-sexp 1)
|
|
855 (setq end (point))
|
|
856 (goto-char beg)
|
|
857 (forward-line 1)
|
|
858 (setq beg (point)))
|
|
859 (if (> end beg)
|
|
860 (indent-code-rigidly beg end (- shift-amt) "#")))
|
|
861 ;; No arg supplied, use c-tab-always-indent to determine
|
|
862 ;; behavior
|
|
863 (cond
|
|
864 ;; CASE 1: indent when at column zero or in lines indentation,
|
|
865 ;; otherwise insert a tab
|
|
866 ((not c-tab-always-indent)
|
|
867 (if (save-excursion
|
|
868 (skip-chars-backward " \t")
|
|
869 (not (bolp)))
|
|
870 (funcall c-insert-tab-function)
|
|
871 (c-indent-line)))
|
|
872 ;; CASE 2: just indent the line
|
|
873 ((eq c-tab-always-indent t)
|
|
874 (c-indent-line))
|
|
875 ;; CASE 3: if in a literal, insert a tab, but always indent the
|
|
876 ;; line
|
|
877 (t
|
|
878 (if (c-in-literal bod)
|
|
879 (funcall c-insert-tab-function))
|
|
880 (c-indent-line)
|
|
881 )))))
|
|
882
|
|
883 (defun c-indent-exp (&optional shutup-p)
|
|
884 "Indent each line in balanced expression following point.
|
|
885 Optional SHUTUP-P if non-nil, inhibits message printing and error checking."
|
|
886 (interactive "P")
|
|
887 (let ((here (point))
|
|
888 end progress-p)
|
|
889 (unwind-protect
|
|
890 (let ((c-echo-syntactic-information-p nil) ;keep quiet for speed
|
|
891 (start (progn
|
|
892 ;; try to be smarter about finding the range of
|
|
893 ;; lines to indent. skip all following
|
|
894 ;; whitespace. failing that, try to find any
|
|
895 ;; opening brace on the current line
|
|
896 (skip-chars-forward " \t\n")
|
171
|
897 (if (memq (char-after) '(?\( ?\[ ?\{))
|
165
|
898 (point)
|
|
899 (let ((state (parse-partial-sexp (point)
|
|
900 (c-point 'eol))))
|
|
901 (and (nth 1 state)
|
|
902 (goto-char (nth 1 state))
|
171
|
903 (memq (char-after) '(?\( ?\[ ?\{))
|
165
|
904 (point)))))))
|
|
905 ;; find balanced expression end
|
|
906 (setq end (and (c-safe (progn (forward-sexp 1) t))
|
|
907 (point-marker)))
|
|
908 ;; sanity check
|
|
909 (and (not start)
|
|
910 (not shutup-p)
|
|
911 (error "Cannot find start of balanced expression to indent."))
|
|
912 (and (not end)
|
|
913 (not shutup-p)
|
|
914 (error "Cannot find end of balanced expression to indent."))
|
|
915 (c-progress-init start end 'c-indent-exp)
|
|
916 (setq progress-p t)
|
|
917 (goto-char start)
|
|
918 (beginning-of-line)
|
|
919 (while (< (point) end)
|
|
920 (if (not (looking-at "[ \t]*$"))
|
|
921 (c-indent-line))
|
|
922 (c-progress-update)
|
|
923 (forward-line 1)))
|
|
924 ;; make sure marker is deleted
|
|
925 (and end
|
|
926 (set-marker end nil))
|
|
927 (and progress-p
|
|
928 (c-progress-fini 'c-indent-exp))
|
|
929 (goto-char here))))
|
|
930
|
|
931 (defun c-indent-defun ()
|
|
932 "Re-indents the current top-level function def, struct or class declaration."
|
|
933 (interactive)
|
|
934 (let ((here (point-marker))
|
|
935 (c-echo-syntactic-information-p nil)
|
|
936 (brace (c-least-enclosing-brace (c-parse-state))))
|
|
937 (if brace
|
|
938 (goto-char brace)
|
|
939 (beginning-of-defun))
|
|
940 ;; if we're sitting at b-o-b, it might be because there was no
|
|
941 ;; least enclosing brace and we were sitting on the defun's open
|
|
942 ;; brace.
|
171
|
943 (if (and (bobp) (not (eq (char-after) ?\{)))
|
165
|
944 (goto-char here))
|
|
945 ;; if defun-prompt-regexp is non-nil, b-o-d might not leave us at
|
|
946 ;; the open brace. I consider this an Emacs bug.
|
|
947 (and (boundp 'defun-prompt-regexp)
|
|
948 defun-prompt-regexp
|
|
949 (looking-at defun-prompt-regexp)
|
|
950 (goto-char (match-end 0)))
|
|
951 ;; catch all errors in c-indent-exp so we can 1. give more
|
|
952 ;; meaningful error message, and 2. restore point
|
|
953 (unwind-protect
|
|
954 (c-indent-exp)
|
|
955 (goto-char here)
|
|
956 (set-marker here nil))))
|
|
957
|
|
958 (defun c-indent-region (start end)
|
|
959 ;; Indent every line whose first char is between START and END inclusive.
|
|
960 (save-excursion
|
|
961 (goto-char start)
|
|
962 ;; Advance to first nonblank line.
|
|
963 (skip-chars-forward " \t\n")
|
|
964 (beginning-of-line)
|
|
965 (let (endmark)
|
|
966 (unwind-protect
|
|
967 (let ((c-tab-always-indent t)
|
|
968 ;; shut up any echo msgs on indiv lines
|
|
969 (c-echo-syntactic-information-p nil)
|
|
970 fence)
|
|
971 (c-progress-init start end 'c-indent-region)
|
|
972 (setq endmark (copy-marker end))
|
|
973 (while (and (bolp)
|
|
974 (not (eobp))
|
|
975 (< (point) endmark))
|
|
976 ;; update progress
|
|
977 (c-progress-update)
|
|
978 ;; Indent one line as with TAB.
|
|
979 (let (nextline sexpend sexpbeg)
|
|
980 ;; skip blank lines
|
|
981 (skip-chars-forward " \t\n")
|
|
982 (beginning-of-line)
|
|
983 ;; indent the current line
|
|
984 (c-indent-line)
|
|
985 (setq fence (point))
|
|
986 (if (save-excursion
|
|
987 (beginning-of-line)
|
|
988 (looking-at "[ \t]*#"))
|
|
989 (forward-line 1)
|
|
990 (save-excursion
|
|
991 ;; Find beginning of following line.
|
|
992 (setq nextline (c-point 'bonl))
|
|
993 ;; Find first beginning-of-sexp for sexp extending past
|
|
994 ;; this line.
|
|
995 (beginning-of-line)
|
|
996 (while (< (point) nextline)
|
|
997 (condition-case nil
|
|
998 (progn
|
|
999 (forward-sexp 1)
|
|
1000 (setq sexpend (point)))
|
|
1001 (error (setq sexpend nil)
|
|
1002 (goto-char nextline)))
|
|
1003 (c-forward-syntactic-ws))
|
|
1004 (if sexpend
|
|
1005 (progn
|
|
1006 ;; make sure the sexp we found really starts on the
|
|
1007 ;; current line and extends past it
|
|
1008 (goto-char sexpend)
|
|
1009 (setq sexpend (point-marker))
|
|
1010 (c-safe (backward-sexp 1))
|
|
1011 (setq sexpbeg (point))))
|
|
1012 (if (and sexpbeg (< sexpbeg fence))
|
|
1013 (setq sexpbeg fence)))
|
|
1014 ;; check to see if the next line starts a
|
|
1015 ;; comment-only line
|
|
1016 (save-excursion
|
|
1017 (forward-line 1)
|
|
1018 (skip-chars-forward " \t")
|
|
1019 (if (looking-at c-comment-start-regexp)
|
|
1020 (setq sexpbeg (c-point 'bol))))
|
|
1021 ;; If that sexp ends within the region, indent it all at
|
|
1022 ;; once, fast.
|
|
1023 (condition-case nil
|
|
1024 (if (and sexpend
|
|
1025 (> sexpend nextline)
|
|
1026 (<= sexpend endmark))
|
|
1027 (progn
|
|
1028 (goto-char sexpbeg)
|
|
1029 (c-indent-exp 'shutup)
|
|
1030 (c-progress-update)
|
|
1031 (goto-char sexpend)))
|
|
1032 (error
|
|
1033 (goto-char sexpbeg)
|
|
1034 (c-indent-line)))
|
|
1035 ;; Move to following line and try again.
|
|
1036 (and sexpend
|
|
1037 (markerp sexpend)
|
|
1038 (set-marker sexpend nil))
|
|
1039 (forward-line 1)
|
|
1040 (setq fence (point))))))
|
|
1041 (set-marker endmark nil)
|
|
1042 (c-progress-fini 'c-indent-region)
|
171
|
1043 (c-echo-parsing-error)
|
165
|
1044 ))))
|
|
1045
|
|
1046 (defun c-mark-function ()
|
|
1047 "Put mark at end of a C, C++, or Objective-C defun, point at beginning."
|
|
1048 (interactive)
|
|
1049 (let ((here (point))
|
|
1050 ;; there should be a c-point position for 'eod
|
|
1051 (eod (save-excursion (end-of-defun) (point)))
|
|
1052 (state (c-parse-state))
|
|
1053 brace)
|
|
1054 (while state
|
|
1055 (setq brace (car state))
|
|
1056 (if (consp brace)
|
|
1057 (goto-char (cdr brace))
|
|
1058 (goto-char brace))
|
|
1059 (setq state (cdr state)))
|
171
|
1060 (if (eq (char-after) ?{)
|
165
|
1061 (progn
|
|
1062 (forward-line -1)
|
|
1063 (while (not (or (bobp)
|
|
1064 (looking-at "[ \t]*$")))
|
|
1065 (forward-line -1)))
|
|
1066 (forward-line 1)
|
|
1067 (skip-chars-forward " \t\n"))
|
|
1068 (push-mark here)
|
|
1069 (push-mark eod nil t)))
|
|
1070
|
|
1071
|
|
1072 ;; for progress reporting
|
|
1073 (defvar c-progress-info nil)
|
|
1074
|
|
1075 (defun c-progress-init (start end context)
|
|
1076 ;; start the progress update messages. if this emacs doesn't have a
|
|
1077 ;; built-in timer, just be dumb about it
|
|
1078 (if (not (fboundp 'current-time))
|
|
1079 (message "indenting region... (this may take a while)")
|
|
1080 ;; if progress has already been initialized, do nothing. otherwise
|
|
1081 ;; initialize the counter with a vector of:
|
|
1082 ;; [start end lastsec context]
|
|
1083 (if c-progress-info
|
|
1084 ()
|
|
1085 (setq c-progress-info (vector start
|
|
1086 (save-excursion
|
|
1087 (goto-char end)
|
|
1088 (point-marker))
|
|
1089 (nth 1 (current-time))
|
|
1090 context))
|
|
1091 (message "indenting region..."))))
|
|
1092
|
|
1093 (defun c-progress-update ()
|
|
1094 ;; update progress
|
|
1095 (if (not (and c-progress-info c-progress-interval))
|
|
1096 nil
|
|
1097 (let ((now (nth 1 (current-time)))
|
|
1098 (start (aref c-progress-info 0))
|
|
1099 (end (aref c-progress-info 1))
|
|
1100 (lastsecs (aref c-progress-info 2)))
|
|
1101 ;; should we update? currently, update happens every 2 seconds,
|
|
1102 ;; what's the right value?
|
|
1103 (if (< c-progress-interval (- now lastsecs))
|
|
1104 (progn
|
|
1105 (message "indenting region... (%d%% complete)"
|
|
1106 (/ (* 100 (- (point) start)) (- end start)))
|
|
1107 (aset c-progress-info 2 now)))
|
|
1108 )))
|
|
1109
|
|
1110 (defun c-progress-fini (context)
|
|
1111 ;; finished
|
|
1112 (if (or (eq context (aref c-progress-info 3))
|
|
1113 (eq context t))
|
|
1114 (progn
|
|
1115 (set-marker (aref c-progress-info 1) nil)
|
|
1116 (setq c-progress-info nil)
|
|
1117 (message "indenting region...done"))))
|
|
1118
|
|
1119
|
|
1120
|
|
1121 ;;; This page handles insertion and removal of backslashes for C macros.
|
|
1122
|
|
1123 (defun c-backslash-region (from to delete-flag)
|
|
1124 "Insert, align, or delete end-of-line backslashes on the lines in the region.
|
|
1125 With no argument, inserts backslashes and aligns existing backslashes.
|
|
1126 With an argument, deletes the backslashes.
|
|
1127
|
|
1128 This function does not modify blank lines at the start of the region.
|
|
1129 If the region ends at the start of a line, it always deletes the
|
|
1130 backslash (if any) at the end of the previous line.
|
|
1131
|
|
1132 You can put the region around an entire macro definition and use this
|
|
1133 command to conveniently insert and align the necessary backslashes."
|
|
1134 (interactive "r\nP")
|
|
1135 (save-excursion
|
|
1136 (goto-char from)
|
|
1137 (let ((column c-backslash-column)
|
|
1138 (endmark (make-marker)))
|
|
1139 (move-marker endmark to)
|
|
1140 ;; Compute the smallest column number past the ends of all the lines.
|
|
1141 (if (not delete-flag)
|
|
1142 (while (< (point) to)
|
|
1143 (end-of-line)
|
171
|
1144 (if (eq (char-before) ?\\)
|
165
|
1145 (progn (forward-char -1)
|
|
1146 (skip-chars-backward " \t")))
|
|
1147 (setq column (max column (1+ (current-column))))
|
|
1148 (forward-line 1)))
|
|
1149 ;; Adjust upward to a tab column, if that doesn't push past the margin.
|
|
1150 (if (> (% column tab-width) 0)
|
|
1151 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width)))
|
|
1152 (if (< adjusted (window-width))
|
|
1153 (setq column adjusted))))
|
|
1154 ;; Don't modify blank lines at start of region.
|
|
1155 (goto-char from)
|
|
1156 (while (and (< (point) endmark) (eolp))
|
|
1157 (forward-line 1))
|
|
1158 ;; Add or remove backslashes on all the lines.
|
|
1159 (while (< (point) endmark)
|
|
1160 (if (and (not delete-flag)
|
|
1161 ;; Un-backslashify the last line
|
|
1162 ;; if the region ends right at the start of the next line.
|
|
1163 (save-excursion
|
|
1164 (forward-line 1)
|
|
1165 (< (point) endmark)))
|
|
1166 (c-append-backslash column)
|
|
1167 (c-delete-backslash))
|
|
1168 (forward-line 1))
|
|
1169 (move-marker endmark nil)))
|
|
1170 (c-keep-region-active))
|
|
1171
|
|
1172 (defun c-append-backslash (column)
|
|
1173 (end-of-line)
|
171
|
1174 (if (eq (char-before) ?\\)
|
165
|
1175 (progn (forward-char -1)
|
|
1176 (delete-horizontal-space)
|
|
1177 (indent-to column))
|
|
1178 (indent-to column)
|
|
1179 (insert "\\")))
|
|
1180
|
|
1181 (defun c-delete-backslash ()
|
|
1182 (end-of-line)
|
|
1183 (or (bolp)
|
|
1184 (progn
|
|
1185 (forward-char -1)
|
|
1186 (if (looking-at "\\\\")
|
|
1187 (delete-region (1+ (point))
|
|
1188 (progn (skip-chars-backward " \t") (point)))))))
|
|
1189
|
|
1190
|
|
1191 (defun c-fill-paragraph (&optional arg)
|
|
1192 "Like \\[fill-paragraph] but handles C and C++ style comments.
|
|
1193 If any of the current line is a comment or within a comment,
|
|
1194 fill the comment or the paragraph of it that point is in,
|
|
1195 preserving the comment indentation or line-starting decorations.
|
|
1196
|
|
1197 Optional prefix ARG means justify paragraph as well."
|
|
1198 (interactive "P")
|
|
1199 (let* (comment-start-place
|
|
1200 (first-line
|
|
1201 ;; Check for obvious entry to comment.
|
|
1202 (save-excursion
|
|
1203 (beginning-of-line)
|
|
1204 (skip-chars-forward " \t\n")
|
|
1205 (and (looking-at comment-start-skip)
|
|
1206 (setq comment-start-place (point)))))
|
|
1207 (re1 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
|
|
1208 (if (and c-double-slash-is-comments-p
|
|
1209 (save-excursion
|
|
1210 (beginning-of-line)
|
|
1211 (looking-at ".*//")))
|
|
1212 (let ((fill-prefix fill-prefix)
|
|
1213 ;; Lines containing just a comment start or just an end
|
|
1214 ;; should not be filled into paragraphs they are next
|
|
1215 ;; to.
|
|
1216 (paragraph-start (concat paragraph-start re1))
|
|
1217 (paragraph-separate (concat paragraph-separate re1)))
|
|
1218 (save-excursion
|
|
1219 (beginning-of-line)
|
|
1220 ;; Move up to first line of this comment.
|
|
1221 (while (and (not (bobp))
|
|
1222 (looking-at "[ \t]*//[ \t]*[^ \t\n]"))
|
|
1223 (forward-line -1))
|
|
1224 (if (not (looking-at ".*//[ \t]*[^ \t\n]"))
|
|
1225 (forward-line 1))
|
|
1226 ;; Find the comment start in this line.
|
|
1227 (re-search-forward "[ \t]*//[ \t]*")
|
|
1228 ;; Set the fill-prefix to be what all lines except the first
|
|
1229 ;; should start with. But do not alter a user set fill-prefix.
|
|
1230 (if (null fill-prefix)
|
|
1231 (setq fill-prefix (buffer-substring (match-beginning 0)
|
|
1232 (match-end 0))))
|
|
1233 (save-restriction
|
|
1234 ;; Narrow down to just the lines of this comment.
|
|
1235 (narrow-to-region (c-point 'bol)
|
|
1236 (save-excursion
|
|
1237 (forward-line 1)
|
|
1238 (while (looking-at fill-prefix)
|
|
1239 (forward-line 1))
|
|
1240 (point)))
|
|
1241 (fill-paragraph arg)
|
|
1242 t)))
|
|
1243 ;; else C style comments
|
|
1244 (if (or first-line
|
|
1245 ;; t if we enter a comment between start of function and
|
|
1246 ;; this line.
|
|
1247 (eq (c-in-literal) 'c)
|
|
1248 ;; t if this line contains a comment starter.
|
|
1249 (setq first-line
|
|
1250 (save-excursion
|
|
1251 (beginning-of-line)
|
|
1252 (prog1
|
|
1253 (re-search-forward comment-start-skip
|
|
1254 (save-excursion (end-of-line)
|
|
1255 (point))
|
|
1256 t)
|
|
1257 (setq comment-start-place (point))))))
|
|
1258 ;; Inside a comment: fill one comment paragraph.
|
|
1259 (let ((fill-prefix
|
|
1260 ;; The prefix for each line of this paragraph
|
|
1261 ;; is the appropriate part of the start of this line,
|
|
1262 ;; up to the column at which text should be indented.
|
|
1263 (save-excursion
|
|
1264 (beginning-of-line)
|
|
1265 (if (looking-at "[ \t]*/\\*.*\\*/")
|
|
1266 (progn (re-search-forward comment-start-skip)
|
|
1267 (make-string (current-column) ?\ ))
|
|
1268 (if first-line (forward-line 1))
|
|
1269
|
|
1270 (let ((line-width (progn (end-of-line) (current-column))))
|
|
1271 (beginning-of-line)
|
|
1272 (prog1
|
|
1273 (buffer-substring
|
|
1274 (point)
|
|
1275
|
|
1276 ;; How shall we decide where the end of the
|
|
1277 ;; fill-prefix is?
|
|
1278 (progn
|
|
1279 (beginning-of-line)
|
|
1280 (skip-chars-forward " \t*" (c-point 'eol))
|
|
1281 ;; kludge alert, watch out for */, in
|
|
1282 ;; which case fill-prefix should *not*
|
|
1283 ;; be "*"!
|
171
|
1284 (if (and (eq (char-after) ?/)
|
|
1285 (eq (char-before) ?*))
|
165
|
1286 (forward-char -1))
|
|
1287 (point)))
|
|
1288
|
|
1289 ;; If the comment is only one line followed
|
|
1290 ;; by a blank line, calling move-to-column
|
|
1291 ;; above may have added some spaces and tabs
|
|
1292 ;; to the end of the line; the fill-paragraph
|
|
1293 ;; function will then delete it and the
|
|
1294 ;; newline following it, so we'll lose a
|
|
1295 ;; blank line when we shouldn't. So delete
|
|
1296 ;; anything move-to-column added to the end
|
|
1297 ;; of the line. We record the line width
|
|
1298 ;; instead of the position of the old line
|
|
1299 ;; end because move-to-column might break a
|
|
1300 ;; tab into spaces, and the new characters
|
|
1301 ;; introduced there shouldn't be deleted.
|
|
1302
|
|
1303 ;; If you can see a better way to do this,
|
|
1304 ;; please make the change. This seems very
|
|
1305 ;; messy to me.
|
|
1306 (delete-region (progn (move-to-column line-width)
|
|
1307 (point))
|
|
1308 (progn (end-of-line) (point))))))))
|
|
1309
|
|
1310 ;; Lines containing just a comment start or just an end
|
|
1311 ;; should not be filled into paragraphs they are next
|
|
1312 ;; to.
|
|
1313 (paragraph-start (concat paragraph-start re1))
|
|
1314 (paragraph-separate (concat paragraph-separate re1))
|
|
1315 (chars-to-delete 0)
|
|
1316 )
|
|
1317 (save-restriction
|
|
1318 ;; Don't fill the comment together with the code
|
|
1319 ;; following it. So temporarily exclude everything
|
|
1320 ;; before the comment start, and everything after the
|
|
1321 ;; line where the comment ends. If comment-start-place
|
|
1322 ;; is non-nil, the comment starter is there. Otherwise,
|
|
1323 ;; point is inside the comment.
|
|
1324 (narrow-to-region (save-excursion
|
|
1325 (if comment-start-place
|
|
1326 (goto-char comment-start-place)
|
|
1327 (search-backward "/*"))
|
|
1328 (if (and (not c-hanging-comment-starter-p)
|
|
1329 (looking-at
|
|
1330 (concat c-comment-start-regexp
|
|
1331 "[ \t]*$")))
|
|
1332 (forward-line 1))
|
|
1333 ;; Protect text before the comment
|
|
1334 ;; start by excluding it. Add
|
|
1335 ;; spaces to bring back proper
|
|
1336 ;; indentation of that point.
|
|
1337 (let ((column (current-column)))
|
|
1338 (prog1 (point)
|
|
1339 (setq chars-to-delete column)
|
|
1340 (insert-char ?\ column))))
|
|
1341 (save-excursion
|
|
1342 (if comment-start-place
|
|
1343 (goto-char (+ comment-start-place 2)))
|
|
1344 (search-forward "*/" nil 'move)
|
|
1345 (forward-line 1)
|
|
1346 (point)))
|
|
1347 (fill-paragraph arg)
|
|
1348 (save-excursion
|
|
1349 ;; Delete the chars we inserted to avoid clobbering
|
|
1350 ;; the stuff before the comment start.
|
|
1351 (goto-char (point-min))
|
|
1352 (if (> chars-to-delete 0)
|
|
1353 (delete-region (point) (+ (point) chars-to-delete)))
|
|
1354 ;; Find the comment ender (should be on last line of
|
|
1355 ;; buffer, given the narrowing) and don't leave it on
|
|
1356 ;; its own line, unless that's the style that's desired.
|
|
1357 (goto-char (point-max))
|
|
1358 (forward-line -1)
|
|
1359 (search-forward "*/" nil 'move)
|
|
1360 (beginning-of-line)
|
|
1361 (if (and c-hanging-comment-ender-p
|
|
1362 (looking-at "[ \t]*\\*/"))
|
|
1363 ;(delete-indentation)))))
|
|
1364 (let ((fill-column (+ fill-column 9999)))
|
|
1365 (forward-line -1)
|
|
1366 (fill-region-as-paragraph (point) (point-max))))))
|
|
1367 t)))))
|
|
1368
|
|
1369
|
|
1370 (provide 'cc-cmds)
|
|
1371 ;;; cc-cmds.el ends here
|