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