Mercurial > hg > xemacs-beta
comparison lisp/cc-mode/cc-cmds.el @ 171:929b76928fce r20-3b12
Import from CVS: tag r20-3b12
author | cvs |
---|---|
date | Mon, 13 Aug 2007 09:47:52 +0200 |
parents | 5a88923fcbfe |
children | 8eaf7971accc |
comparison
equal
deleted
inserted
replaced
170:98a42ee61975 | 171:929b76928fce |
---|---|
5 ;; Authors: 1992-1997 Barry A. Warsaw | 5 ;; Authors: 1992-1997 Barry A. Warsaw |
6 ;; 1987 Dave Detlefs and Stewart Clamen | 6 ;; 1987 Dave Detlefs and Stewart Clamen |
7 ;; 1985 Richard M. Stallman | 7 ;; 1985 Richard M. Stallman |
8 ;; Maintainer: cc-mode-help@python.org | 8 ;; Maintainer: cc-mode-help@python.org |
9 ;; Created: 22-Apr-1997 (split from cc-mode.el) | 9 ;; Created: 22-Apr-1997 (split from cc-mode.el) |
10 ;; Version: 5.11 | 10 ;; Version: 5.12 |
11 ;; Keywords: c languages oop | 11 ;; Keywords: c languages oop |
12 | 12 |
13 ;; This file is part of GNU Emacs. | 13 ;; This file is part of GNU Emacs. |
14 | 14 |
15 ;; GNU Emacs is free software; you can redistribute it and/or modify | 15 ;; GNU Emacs is free software; you can redistribute it and/or modify |
25 ;; You should have received a copy of the GNU General Public License | 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 | 26 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
28 ;; Boston, MA 02111-1307, USA. | 28 ;; Boston, MA 02111-1307, USA. |
29 | 29 |
30 (eval-when-compile | |
31 (load-file "./cc-engine.el")) | |
32 | |
33 | 30 |
34 ;; Utilities | |
35 (defsubst c-update-modeline () | |
36 ;; set the c-auto-hungry-string for the correct designation on the modeline | |
37 (setq c-auto-hungry-string | |
38 (if c-auto-newline | |
39 (if c-hungry-delete-key "/ah" "/a") | |
40 (if c-hungry-delete-key "/h" nil))) | |
41 (force-mode-line-update)) | |
42 | |
43 (defun c-calculate-state (arg prevstate) | 31 (defun c-calculate-state (arg prevstate) |
44 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If | 32 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If |
45 ;; arg is nil or zero, toggle the state. If arg is negative, turn | 33 ;; arg is nil or zero, toggle the state. If arg is negative, turn |
46 ;; the state off, and if arg is positive, turn the state on | 34 ;; the state off, and if arg is positive, turn the state on |
47 (if (or (not arg) | 35 (if (or (not arg) |
48 (zerop (setq arg (prefix-numeric-value arg)))) | 36 (zerop (setq arg (prefix-numeric-value arg)))) |
49 (not prevstate) | 37 (not prevstate) |
50 (> arg 0))) | 38 (> arg 0))) |
51 | 39 |
52 | |
53 ;; Auto-newline and hungry-delete | 40 ;; Auto-newline and hungry-delete |
54 (defun c-toggle-auto-state (arg) | 41 (defun c-toggle-auto-state (arg) |
55 "Toggle auto-newline feature. | 42 "Toggle auto-newline feature. |
56 Optional numeric ARG, if supplied turns on auto-newline when positive, | 43 Optional numeric ARG, if supplied turns on auto-newline when positive, |
57 turns it off when negative, and just toggles it when zero. | 44 turns it off when negative, and just toggles it when zero. |
204 ;; be most disruptive. we'll blink it ourselves later on | 191 ;; be most disruptive. we'll blink it ourselves later on |
205 (old-blink-paren blink-paren-function) | 192 (old-blink-paren blink-paren-function) |
206 blink-paren-function | 193 blink-paren-function |
207 (insertion-point (point)) | 194 (insertion-point (point)) |
208 delete-temp-newline | 195 delete-temp-newline |
209 (preserve-p (= 32 (char-syntax (preceding-char)))) | 196 (preserve-p (eq 32 (char-syntax (char-before)))) |
210 ;; shut this up too | 197 ;; shut this up too |
211 (c-echo-syntactic-information-p nil) | 198 (c-echo-syntactic-information-p nil) |
212 (syntax (progn | 199 (syntax (progn |
213 ;; only insert a newline if there is | 200 ;; only insert a newline if there is |
214 ;; non-whitespace behind us | 201 ;; non-whitespace behind us |
243 ;; faster than recalculating the state in many cases | 230 ;; faster than recalculating the state in many cases |
244 (save-excursion | 231 (save-excursion |
245 (save-restriction | 232 (save-restriction |
246 (narrow-to-region here (point)) | 233 (narrow-to-region here (point)) |
247 (if (and (c-safe (progn (backward-up-list -1) t)) | 234 (if (and (c-safe (progn (backward-up-list -1) t)) |
248 (memq (preceding-char) '(?\) ?})) | 235 (memq (char-before) '(?\) ?})) |
249 (progn (widen) | 236 (progn (widen) |
250 (c-safe (progn (forward-sexp -1) t)))) | 237 (c-safe (progn (forward-sexp -1) t)))) |
251 (setq c-state-cache | 238 (setq c-state-cache |
252 (c-hack-state (point) 'open c-state-cache)) | 239 (c-hack-state (point) 'open c-state-cache)) |
253 (if (and (car c-state-cache) | 240 (if (and (car c-state-cache) |
302 (pos (- (point-max) (point))) | 289 (pos (- (point-max) (point))) |
303 mbeg mend) | 290 mbeg mend) |
304 ;; clean up empty defun braces | 291 ;; clean up empty defun braces |
305 (if (and c-auto-newline | 292 (if (and c-auto-newline |
306 (memq 'empty-defun-braces c-cleanup-list) | 293 (memq 'empty-defun-braces c-cleanup-list) |
307 (= last-command-char ?\}) | 294 (eq last-command-char ?\}) |
308 (c-intersect-lists '(defun-close class-close inline-close) | 295 (c-intersect-lists '(defun-close class-close inline-close) |
309 syntax) | 296 syntax) |
310 (progn | 297 (progn |
311 (forward-char -1) | 298 (forward-char -1) |
312 (skip-chars-backward " \t\n") | 299 (skip-chars-backward " \t\n") |
313 (= (preceding-char) ?\{)) | 300 (eq (char-before) ?\{)) |
314 ;; make sure matching open brace isn't in a comment | 301 ;; make sure matching open brace isn't in a comment |
315 (not (c-in-literal))) | 302 (not (c-in-literal))) |
316 (delete-region (point) (1- here))) | 303 (delete-region (point) (1- here))) |
317 ;; clean up brace-else-brace | 304 ;; clean up brace-else-brace |
318 (if (and c-auto-newline | 305 (if (and c-auto-newline |
319 (memq 'brace-else-brace c-cleanup-list) | 306 (memq 'brace-else-brace c-cleanup-list) |
320 (= last-command-char ?\{) | 307 (eq last-command-char ?\{) |
321 (re-search-backward "}[ \t\n]*else[ \t\n]*{" nil t) | 308 (re-search-backward "}[ \t\n]*else[ \t\n]*{" nil t) |
322 (progn | 309 (progn |
323 (setq mbeg (match-beginning 0) | 310 (setq mbeg (match-beginning 0) |
324 mend (match-end 0)) | 311 mend (match-end 0)) |
325 (= mend here)) | 312 (= mend here)) |
328 (delete-region mbeg mend) | 315 (delete-region mbeg mend) |
329 (insert "} else {"))) | 316 (insert "} else {"))) |
330 ;; clean up brace-elseif-brace | 317 ;; clean up brace-elseif-brace |
331 (if (and c-auto-newline | 318 (if (and c-auto-newline |
332 (memq 'brace-elseif-brace c-cleanup-list) | 319 (memq 'brace-elseif-brace c-cleanup-list) |
333 (= last-command-char ?\{) | 320 (eq last-command-char ?\{) |
334 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*" nil t) | 321 (re-search-backward "}[ \t\n]*else[ \t\n]+if[ \t\n]*" nil t) |
335 (save-excursion | 322 (save-excursion |
336 (goto-char (match-end 0)) | 323 (goto-char (match-end 0)) |
337 (c-safe (forward-sexp 1)) | 324 (c-safe (forward-sexp 1)) |
338 (skip-chars-forward " \t\n") | 325 (skip-chars-forward " \t\n") |
349 (if (memq 'after newlines) | 336 (if (memq 'after newlines) |
350 (progn | 337 (progn |
351 (newline) | 338 (newline) |
352 ;; update on c-state-cache | 339 ;; update on c-state-cache |
353 (let* ((bufpos (- (point) 2)) | 340 (let* ((bufpos (- (point) 2)) |
354 (which (if (= (char-after bufpos) ?{) 'open 'close)) | 341 (which (if (eq (char-after bufpos) ?{) 'open 'close)) |
355 (c-state-cache (c-hack-state bufpos which c-state-cache))) | 342 (c-state-cache (c-hack-state bufpos which c-state-cache))) |
356 (c-indent-line)))) | 343 (c-indent-line)))) |
357 ;; blink the paren | 344 ;; blink the paren |
358 (and (= last-command-char ?\}) | 345 (and (eq last-command-char ?\}) |
359 old-blink-paren | 346 old-blink-paren |
360 (save-excursion | 347 (save-excursion |
361 (c-backward-syntactic-ws safepos) | 348 (c-backward-syntactic-ws safepos) |
362 (funcall old-blink-paren))) | 349 (funcall old-blink-paren))) |
363 )))) | 350 )))) |
368 construct, and we are on a comment-only-line, indent line as comment. | 355 construct, and we are on a comment-only-line, indent line as comment. |
369 If numeric ARG is supplied or point is inside a literal, indentation | 356 If numeric ARG is supplied or point is inside a literal, indentation |
370 is inhibited." | 357 is inhibited." |
371 (interactive "P") | 358 (interactive "P") |
372 (let ((indentp (and (not arg) | 359 (let ((indentp (and (not arg) |
373 (= (preceding-char) ?/) | 360 (eq (char-before) ?/) |
374 (= last-command-char ?/) | 361 (eq last-command-char ?/) |
375 (not (c-in-literal)))) | 362 (not (c-in-literal)))) |
376 ;; shut this up | 363 ;; shut this up |
377 (c-echo-syntactic-information-p nil)) | 364 (c-echo-syntactic-information-p nil)) |
378 (self-insert-command (prefix-numeric-value arg)) | 365 (self-insert-command (prefix-numeric-value arg)) |
379 (if indentp | 366 (if indentp |
389 (self-insert-command (prefix-numeric-value arg)) | 376 (self-insert-command (prefix-numeric-value arg)) |
390 ;; if we are in a literal, or if arg is given do not re-indent the | 377 ;; if we are in a literal, or if arg is given do not re-indent the |
391 ;; current line, unless this star introduces a comment-only line. | 378 ;; current line, unless this star introduces a comment-only line. |
392 (if (and (not arg) | 379 (if (and (not arg) |
393 (memq (c-in-literal) '(c)) | 380 (memq (c-in-literal) '(c)) |
394 (= (preceding-char) ?*) | 381 (eq (char-before) ?*) |
395 (save-excursion | 382 (save-excursion |
396 (forward-char -1) | 383 (forward-char -1) |
397 (skip-chars-backward "*") | 384 (skip-chars-backward "*") |
398 (if (= (preceding-char) ?/) | 385 (if (eq (char-before) ?/) |
399 (forward-char -1)) | 386 (forward-char -1)) |
400 (skip-chars-backward " \t") | 387 (skip-chars-backward " \t") |
401 (bolp))) | 388 (bolp))) |
402 ;; shut this up | 389 ;; shut this up |
403 (let (c-echo-syntactic-information-p) | 390 (let (c-echo-syntactic-information-p) |
430 ;; only if c-auto-newline is turned on | 417 ;; only if c-auto-newline is turned on |
431 (if (not c-auto-newline) nil | 418 (if (not c-auto-newline) nil |
432 ;; clean ups | 419 ;; clean ups |
433 (let ((pos (- (point-max) (point)))) | 420 (let ((pos (- (point-max) (point)))) |
434 (if (and (or (and | 421 (if (and (or (and |
435 (= last-command-char ?,) | 422 (eq last-command-char ?,) |
436 (memq 'list-close-comma c-cleanup-list)) | 423 (memq 'list-close-comma c-cleanup-list)) |
437 (and | 424 (and |
438 (= last-command-char ?\;) | 425 (eq last-command-char ?\;) |
439 (memq 'defun-close-semi c-cleanup-list))) | 426 (memq 'defun-close-semi c-cleanup-list))) |
440 (progn | 427 (progn |
441 (forward-char -1) | 428 (forward-char -1) |
442 (skip-chars-backward " \t\n") | 429 (skip-chars-backward " \t\n") |
443 (= (preceding-char) ?})) | 430 (eq (char-before) ?})) |
444 ;; make sure matching open brace isn't in a comment | 431 ;; make sure matching open brace isn't in a comment |
445 (not (c-in-literal lim))) | 432 (not (c-in-literal lim))) |
446 (delete-region (point) here)) | 433 (delete-region (point) here)) |
447 (goto-char (- (point-max) pos))) | 434 (goto-char (- (point-max) pos))) |
448 ;; re-indent line | 435 ;; re-indent line |
491 (self-insert-command (prefix-numeric-value arg)) | 478 (self-insert-command (prefix-numeric-value arg)) |
492 (let ((pos (- (point-max) (point))) | 479 (let ((pos (- (point-max) (point))) |
493 (here (point))) | 480 (here (point))) |
494 (if (and c-auto-newline | 481 (if (and c-auto-newline |
495 (memq 'scope-operator c-cleanup-list) | 482 (memq 'scope-operator c-cleanup-list) |
496 (= (preceding-char) ?:) | 483 (eq (char-before) ?:) |
497 (progn | 484 (progn |
498 (forward-char -1) | 485 (forward-char -1) |
499 (skip-chars-backward " \t\n") | 486 (skip-chars-backward " \t\n") |
500 (= (preceding-char) ?:)) | 487 (eq (char-before) ?:)) |
501 (not (c-in-literal)) | 488 (not (c-in-literal)) |
502 (not (= (char-after (- (point) 2)) ?:))) | 489 (not (eq (char-after (- (point) 2)) ?:))) |
503 (delete-region (point) (1- here))) | 490 (delete-region (point) (1- here))) |
504 (goto-char (- (point-max) pos))) | 491 (goto-char (- (point-max) pos))) |
505 ;; lets do some special stuff with the colon character | 492 ;; lets do some special stuff with the colon character |
506 (setq syntax (c-guess-basic-syntax) | 493 (setq syntax (c-guess-basic-syntax) |
507 ;; some language elements can only be determined by | 494 ;; some language elements can only be determined by |
548 | 535 |
549 The line will also not be re-indented if a numeric argument is | 536 The line will also not be re-indented if a numeric argument is |
550 supplied, or point is inside a literal." | 537 supplied, or point is inside a literal." |
551 (interactive "P") | 538 (interactive "P") |
552 (let ((indentp (and (not arg) | 539 (let ((indentp (and (not arg) |
553 (= (preceding-char) last-command-char) | 540 (eq (char-before) last-command-char) |
554 (not (c-in-literal)))) | 541 (not (c-in-literal)))) |
555 ;; shut this up | 542 ;; shut this up |
556 (c-echo-syntactic-information-p nil)) | 543 (c-echo-syntactic-information-p nil)) |
557 (self-insert-command (prefix-numeric-value arg)) | 544 (self-insert-command (prefix-numeric-value arg)) |
558 (if indentp | 545 (if indentp |
903 ;; try to be smarter about finding the range of | 890 ;; try to be smarter about finding the range of |
904 ;; lines to indent. skip all following | 891 ;; lines to indent. skip all following |
905 ;; whitespace. failing that, try to find any | 892 ;; whitespace. failing that, try to find any |
906 ;; opening brace on the current line | 893 ;; opening brace on the current line |
907 (skip-chars-forward " \t\n") | 894 (skip-chars-forward " \t\n") |
908 (if (memq (following-char) '(?\( ?\[ ?\{)) | 895 (if (memq (char-after) '(?\( ?\[ ?\{)) |
909 (point) | 896 (point) |
910 (let ((state (parse-partial-sexp (point) | 897 (let ((state (parse-partial-sexp (point) |
911 (c-point 'eol)))) | 898 (c-point 'eol)))) |
912 (and (nth 1 state) | 899 (and (nth 1 state) |
913 (goto-char (nth 1 state)) | 900 (goto-char (nth 1 state)) |
914 (memq (following-char) '(?\( ?\[ ?\{)) | 901 (memq (char-after) '(?\( ?\[ ?\{)) |
915 (point))))))) | 902 (point))))))) |
916 ;; find balanced expression end | 903 ;; find balanced expression end |
917 (setq end (and (c-safe (progn (forward-sexp 1) t)) | 904 (setq end (and (c-safe (progn (forward-sexp 1) t)) |
918 (point-marker))) | 905 (point-marker))) |
919 ;; sanity check | 906 ;; sanity check |
949 (goto-char brace) | 936 (goto-char brace) |
950 (beginning-of-defun)) | 937 (beginning-of-defun)) |
951 ;; if we're sitting at b-o-b, it might be because there was no | 938 ;; if we're sitting at b-o-b, it might be because there was no |
952 ;; least enclosing brace and we were sitting on the defun's open | 939 ;; least enclosing brace and we were sitting on the defun's open |
953 ;; brace. | 940 ;; brace. |
954 (if (and (bobp) (not (= (following-char) ?\{))) | 941 (if (and (bobp) (not (eq (char-after) ?\{))) |
955 (goto-char here)) | 942 (goto-char here)) |
956 ;; if defun-prompt-regexp is non-nil, b-o-d might not leave us at | 943 ;; if defun-prompt-regexp is non-nil, b-o-d might not leave us at |
957 ;; the open brace. I consider this an Emacs bug. | 944 ;; the open brace. I consider this an Emacs bug. |
958 (and (boundp 'defun-prompt-regexp) | 945 (and (boundp 'defun-prompt-regexp) |
959 defun-prompt-regexp | 946 defun-prompt-regexp |
1049 (set-marker sexpend nil)) | 1036 (set-marker sexpend nil)) |
1050 (forward-line 1) | 1037 (forward-line 1) |
1051 (setq fence (point)))))) | 1038 (setq fence (point)))))) |
1052 (set-marker endmark nil) | 1039 (set-marker endmark nil) |
1053 (c-progress-fini 'c-indent-region) | 1040 (c-progress-fini 'c-indent-region) |
1041 (c-echo-parsing-error) | |
1054 )))) | 1042 )))) |
1055 | 1043 |
1056 (defun c-mark-function () | 1044 (defun c-mark-function () |
1057 "Put mark at end of a C, C++, or Objective-C defun, point at beginning." | 1045 "Put mark at end of a C, C++, or Objective-C defun, point at beginning." |
1058 (interactive) | 1046 (interactive) |
1065 (setq brace (car state)) | 1053 (setq brace (car state)) |
1066 (if (consp brace) | 1054 (if (consp brace) |
1067 (goto-char (cdr brace)) | 1055 (goto-char (cdr brace)) |
1068 (goto-char brace)) | 1056 (goto-char brace)) |
1069 (setq state (cdr state))) | 1057 (setq state (cdr state))) |
1070 (if (= (following-char) ?{) | 1058 (if (eq (char-after) ?{) |
1071 (progn | 1059 (progn |
1072 (forward-line -1) | 1060 (forward-line -1) |
1073 (while (not (or (bobp) | 1061 (while (not (or (bobp) |
1074 (looking-at "[ \t]*$"))) | 1062 (looking-at "[ \t]*$"))) |
1075 (forward-line -1))) | 1063 (forward-line -1))) |
1149 (move-marker endmark to) | 1137 (move-marker endmark to) |
1150 ;; Compute the smallest column number past the ends of all the lines. | 1138 ;; Compute the smallest column number past the ends of all the lines. |
1151 (if (not delete-flag) | 1139 (if (not delete-flag) |
1152 (while (< (point) to) | 1140 (while (< (point) to) |
1153 (end-of-line) | 1141 (end-of-line) |
1154 (if (= (preceding-char) ?\\) | 1142 (if (eq (char-before) ?\\) |
1155 (progn (forward-char -1) | 1143 (progn (forward-char -1) |
1156 (skip-chars-backward " \t"))) | 1144 (skip-chars-backward " \t"))) |
1157 (setq column (max column (1+ (current-column)))) | 1145 (setq column (max column (1+ (current-column)))) |
1158 (forward-line 1))) | 1146 (forward-line 1))) |
1159 ;; Adjust upward to a tab column, if that doesn't push past the margin. | 1147 ;; Adjust upward to a tab column, if that doesn't push past the margin. |
1179 (move-marker endmark nil))) | 1167 (move-marker endmark nil))) |
1180 (c-keep-region-active)) | 1168 (c-keep-region-active)) |
1181 | 1169 |
1182 (defun c-append-backslash (column) | 1170 (defun c-append-backslash (column) |
1183 (end-of-line) | 1171 (end-of-line) |
1184 ;; Note that "\\\\" is needed to get one backslash. | 1172 (if (eq (char-before) ?\\) |
1185 (if (= (preceding-char) ?\\) | |
1186 (progn (forward-char -1) | 1173 (progn (forward-char -1) |
1187 (delete-horizontal-space) | 1174 (delete-horizontal-space) |
1188 (indent-to column)) | 1175 (indent-to column)) |
1189 (indent-to column) | 1176 (indent-to column) |
1190 (insert "\\"))) | 1177 (insert "\\"))) |
1290 (beginning-of-line) | 1277 (beginning-of-line) |
1291 (skip-chars-forward " \t*" (c-point 'eol)) | 1278 (skip-chars-forward " \t*" (c-point 'eol)) |
1292 ;; kludge alert, watch out for */, in | 1279 ;; kludge alert, watch out for */, in |
1293 ;; which case fill-prefix should *not* | 1280 ;; which case fill-prefix should *not* |
1294 ;; be "*"! | 1281 ;; be "*"! |
1295 (if (and (= (following-char) ?/) | 1282 (if (and (eq (char-after) ?/) |
1296 (= (preceding-char) ?*)) | 1283 (eq (char-before) ?*)) |
1297 (forward-char -1)) | 1284 (forward-char -1)) |
1298 (point))) | 1285 (point))) |
1299 | 1286 |
1300 ;; If the comment is only one line followed | 1287 ;; If the comment is only one line followed |
1301 ;; by a blank line, calling move-to-column | 1288 ;; by a blank line, calling move-to-column |