aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGerd Moellmann2000-07-24 11:12:07 +0000
committerGerd Moellmann2000-07-24 11:12:07 +0000
commitb8ded7944c6e975c91001c2872364a87b68dc2db (patch)
treee1ce74d4cda5502e68ff107cb90d3ece6a41c5a9
parent6862d61353f4126dd8288339afb0d114c130f091 (diff)
downloademacs-b8ded7944c6e975c91001c2872364a87b68dc2db.tar.gz
emacs-b8ded7944c6e975c91001c2872364a87b68dc2db.zip
(c-indent-new-comment-line): Added a kludge
similar to the one in c-fill-paragraph to check the fill prefix from the adaptive fill function for sanity. (c-electric-brace): Fixed some bugs in the state handling that caused class open lines to be recognized as statement-conts in some cases. (c-indent-new-comment-line): Keep the fill prefix guessed by the adaptive fill function unless point is on the first line of a block comment. (c-indent-command): Obey c-syntactic-indentation. (c-electric-brace, c-electric-slash, c-electric-star, c-electric-semi&comma, c-electric-colon, c-electric-lt-gt, c-electric-paren): Don't reindent old lines when c-syntactic-indentation is nil. (c-fill-paragraph): Keep one or two spaces between the text and the block comment ender when it hangs, depending on how many there are before the fill. (c-indent-new-comment-line): Always break multiline comments in multiline mode, regardless of comment-multi-line.
-rw-r--r--lisp/progmodes/cc-cmds.el144
1 files changed, 96 insertions, 48 deletions
diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el
index b5ff3a0d4b7..da5e309700d 100644
--- a/lisp/progmodes/cc-cmds.el
+++ b/lisp/progmodes/cc-cmds.el
@@ -1,8 +1,9 @@
1;;; cc-cmds.el --- user level commands for CC Mode 1;;; cc-cmds.el --- user level commands for CC Mode
2 2
3;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc. 3;; Copyright (C) 1985,1987,1992-2000 Free Software Foundation, Inc.
4 4
5;; Authors: 1998-1999 Barry A. Warsaw and Martin Stjernholm 5;; Authors: 2000- Martin Stjernholm
6;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
6;; 1992-1997 Barry A. Warsaw 7;; 1992-1997 Barry A. Warsaw
7;; 1987 Dave Detlefs and Stewart Clamen 8;; 1987 Dave Detlefs and Stewart Clamen
8;; 1985 Richard M. Stallman 9;; 1985 Richard M. Stallman
@@ -184,8 +185,9 @@ or \"/ah\" string on the mode line, newlines are inserted before and
184after braces based on the value of `c-hanging-braces-alist'. 185after braces based on the value of `c-hanging-braces-alist'.
185 186
186Also, the line is re-indented unless a numeric ARG is supplied, there 187Also, the line is re-indented unless a numeric ARG is supplied, there
187are non-whitespace characters present on the line after the brace, or 188are non-whitespace characters present on the line after the brace, the
188the brace is inserted inside a literal. 189brace is inserted inside a literal, or `c-syntactic-indentation' is
190nil.
189 191
190This function does various newline cleanups based on the value of 192This function does various newline cleanups based on the value of
191`c-cleanup-list'." 193`c-cleanup-list'."
@@ -232,6 +234,8 @@ This function does various newline cleanups based on the value of
232 (not (bolp))) 234 (not (bolp)))
233 (progn (newline) 235 (progn (newline)
234 (setq delete-temp-newline t))) 236 (setq delete-temp-newline t)))
237 (if (eq last-command-char ?{)
238 (setq c-state-cache (cons (point) c-state-cache)))
235 (self-insert-command (prefix-numeric-value arg)) 239 (self-insert-command (prefix-numeric-value arg))
236 ;; state cache doesn't change 240 ;; state cache doesn't change
237 (c-guess-basic-syntax))) 241 (c-guess-basic-syntax)))
@@ -291,7 +295,8 @@ This function does various newline cleanups based on the value of
291 t)))) 295 t))))
292 (setq c-state-cache 296 (setq c-state-cache
293 (c-hack-state (point) 'open c-state-cache))))) 297 (c-hack-state (point) 'open c-state-cache)))))
294 (c-indent-line)) 298 (if c-syntactic-indentation
299 (c-indent-line)))
295 (setq c-state-cache (c-adjust-state (c-point 'bol) here 300 (setq c-state-cache (c-adjust-state (c-point 'bol) here
296 (- (point) (c-point 'bol)) 301 (- (point) (c-point 'bol))
297 c-state-cache)) 302 c-state-cache))
@@ -308,6 +313,7 @@ This function does various newline cleanups based on the value of
308 ;; at least one space. 313 ;; at least one space.
309 (delete-indentation) 314 (delete-indentation)
310 (just-one-space) 315 (just-one-space)
316 (setq c-state-cache (c-whack-state (point) c-state-cache))
311 (if (not preserve-p) 317 (if (not preserve-p)
312 (delete-char -1)))) 318 (delete-char -1))))
313 ;; since we're hanging the brace, we need to recalculate 319 ;; since we're hanging the brace, we need to recalculate
@@ -318,8 +324,7 @@ This function does various newline cleanups based on the value of
318 (save-excursion 324 (save-excursion
319 (let ((bol (c-point 'bol))) 325 (let ((bol (c-point 'bol)))
320 (if (zerop (car (parse-partial-sexp bol (1- (point))))) 326 (if (zerop (car (parse-partial-sexp bol (1- (point)))))
321 (setq c-state-cache (c-whack-state bol c-state-cache) 327 (setq syntax (c-guess-basic-syntax))
322 syntax (c-guess-basic-syntax))
323 ;; gotta punt. this requires some horrible kludgery 328 ;; gotta punt. this requires some horrible kludgery
324 (beginning-of-line) 329 (beginning-of-line)
325 (makunbound 'c-state-cache) 330 (makunbound 'c-state-cache)
@@ -411,11 +416,12 @@ Indent the line as a comment, if:
411 2. The slash is part of a `*/' token that closes a block oriented 416 2. The slash is part of a `*/' token that closes a block oriented
412 comment. 417 comment.
413 418
414If numeric ARG is supplied or point is inside a literal, indentation 419If a numeric ARG is supplied, point is inside a literal, or
415is inhibited." 420`c-syntactic-indentation' is nil, indentation is inhibited."
416 (interactive "*P") 421 (interactive "*P")
417 (let* ((ch (char-before)) 422 (let* ((ch (char-before))
418 (indentp (and (not arg) 423 (indentp (and c-syntactic-indentation
424 (not arg)
419 (eq last-command-char ?/) 425 (eq last-command-char ?/)
420 (or (and (eq ch ?/) 426 (or (and (eq ch ?/)
421 (not (c-in-literal))) 427 (not (c-in-literal)))
@@ -432,13 +438,14 @@ is inhibited."
432 "Insert a star character. 438 "Insert a star character.
433If the star is the second character of a C style comment introducing 439If the star is the second character of a C style comment introducing
434construct, and we are on a comment-only-line, indent line as comment. 440construct, and we are on a comment-only-line, indent line as comment.
435If numeric ARG is supplied or point is inside a literal, indentation 441If a numeric ARG is supplied, point is inside a literal, or
436is inhibited." 442`c-syntactic-indentation' is nil, indentation is inhibited."
437 (interactive "*P") 443 (interactive "*P")
438 (self-insert-command (prefix-numeric-value arg)) 444 (self-insert-command (prefix-numeric-value arg))
439 ;; if we are in a literal, or if arg is given do not re-indent the 445 ;; if we are in a literal, or if arg is given do not re-indent the
440 ;; current line, unless this star introduces a comment-only line. 446 ;; current line, unless this star introduces a comment-only line.
441 (if (and (not arg) 447 (if (and c-syntactic-indentation
448 (not arg)
442 (memq (c-in-literal) '(c)) 449 (memq (c-in-literal) '(c))
443 (eq (char-before) ?*) 450 (eq (char-before) ?*)
444 (save-excursion 451 (save-excursion
@@ -462,7 +469,8 @@ is determined.
462 469
463When semicolon is inserted, the line is re-indented unless a numeric 470When semicolon is inserted, the line is re-indented unless a numeric
464arg is supplied, point is inside a literal, or there are 471arg is supplied, point is inside a literal, or there are
465non-whitespace characters on the line following the semicolon. 472non-whitespace characters on the line following the semicolon, or
473`c-syntactic-indentation' is nil.
466 474
467Based on the value of `c-cleanup-list', this function cleans up commas 475Based on the value of `c-cleanup-list', this function cleans up commas
468following brace lists and semicolons following defuns." 476following brace lists and semicolons following defuns."
@@ -481,7 +489,8 @@ following brace lists and semicolons following defuns."
481 ;; do all cleanups and newline insertions if c-auto-newline is 489 ;; do all cleanups and newline insertions if c-auto-newline is
482 ;; turned on 490 ;; turned on
483 (if (not c-auto-newline) 491 (if (not c-auto-newline)
484 (c-indent-line) 492 (if c-syntactic-indentation
493 (c-indent-line))
485 ;; clean ups 494 ;; clean ups
486 (let ((pos (- (point-max) (point)))) 495 (let ((pos (- (point-max) (point))))
487 (if (and (or (and 496 (if (and (or (and
@@ -499,7 +508,8 @@ following brace lists and semicolons following defuns."
499 (delete-region (point) here)) 508 (delete-region (point) here))
500 (goto-char (- (point-max) pos))) 509 (goto-char (- (point-max) pos)))
501 ;; re-indent line 510 ;; re-indent line
502 (c-indent-line) 511 (if c-syntactic-indentation
512 (c-indent-line))
503 ;; check to see if a newline should be added 513 ;; check to see if a newline should be added
504 (let ((criteria c-hanging-semi&comma-criteria) 514 (let ((criteria c-hanging-semi&comma-criteria)
505 answer add-newline-p) 515 answer add-newline-p)
@@ -525,8 +535,9 @@ or \"/ah\" string on the mode line, newlines are inserted before and
525after colons based on the value of `c-hanging-colons-alist'. 535after colons based on the value of `c-hanging-colons-alist'.
526 536
527Also, the line is re-indented unless a numeric ARG is supplied, there 537Also, the line is re-indented unless a numeric ARG is supplied, there
528are non-whitespace characters present on the line after the colon, or 538are non-whitespace characters present on the line after the colon, the
529the colon is inserted inside a literal. 539colon is inserted inside a literal, or `c-syntactic-indentation' is
540nil.
530 541
531This function cleans up double colon scope operators based on the 542This function cleans up double colon scope operators based on the
532value of `c-cleanup-list'." 543value of `c-cleanup-list'."
@@ -574,8 +585,9 @@ value of `c-cleanup-list'."
574 (c-guess-basic-syntax) 585 (c-guess-basic-syntax)
575 (delete-char -1))) 586 (delete-char -1)))
576 c-hanging-colons-alist)))) 587 c-hanging-colons-alist))))
577 ;; indent the current line 588 ;; indent the current line if it's done syntactically.
578 (c-indent-line syntax) 589 (if c-syntactic-indentation
590 (c-indent-line syntax))
579 ;; does a newline go before the colon? Watch out for already 591 ;; does a newline go before the colon? Watch out for already
580 ;; non-hung colons. However, we don't unhang them because that 592 ;; non-hung colons. However, we don't unhang them because that
581 ;; would be a cleanup (and anti-social). 593 ;; would be a cleanup (and anti-social).
@@ -601,10 +613,12 @@ value of `c-cleanup-list'."
601 "Insert a less-than, or greater-than character. 613 "Insert a less-than, or greater-than character.
602The line will be re-indented if the character inserted is the second 614The line will be re-indented if the character inserted is the second
603of a C++ style stream operator and the buffer is in C++ mode. 615of a C++ style stream operator and the buffer is in C++ mode.
604Exceptions are when a numeric argument is supplied, or point is inside 616Exceptions are when a numeric argument is supplied, point is inside a
605a literal, in which case the line will not be re-indented." 617literal, or `c-syntactic-indentation' is nil, in which case the line
618will not be re-indented."
606 (interactive "*P") 619 (interactive "*P")
607 (let ((indentp (and (not arg) 620 (let ((indentp (and c-syntactic-indentation
621 (not arg)
608 (eq (char-before) last-command-char) 622 (eq (char-before) last-command-char)
609 (not (c-in-literal)))) 623 (not (c-in-literal))))
610 ;; shut this up 624 ;; shut this up
@@ -622,7 +636,8 @@ appropriate; see the variable `c-cleanup-list'.
622 636
623Also, the line is re-indented unless a numeric ARG is supplied, there 637Also, the line is re-indented unless a numeric ARG is supplied, there
624are non-whitespace characters present on the line after the 638are non-whitespace characters present on the line after the
625parenthesis, or the parenthesis is inserted inside a literal." 639parenthesis, the parenthesis is inserted inside a literal, or
640`c-syntactic-indentation' is nil."
626 (interactive "*P") 641 (interactive "*P")
627 (let (;; shut this up 642 (let (;; shut this up
628 (c-echo-syntactic-information-p nil)) 643 (c-echo-syntactic-information-p nil))
@@ -637,7 +652,8 @@ parenthesis, or the parenthesis is inserted inside a literal."
637 (old-blink-paren blink-paren-function) 652 (old-blink-paren blink-paren-function)
638 blink-paren-function) 653 blink-paren-function)
639 (self-insert-command (prefix-numeric-value arg)) 654 (self-insert-command (prefix-numeric-value arg))
640 (c-indent-line) 655 (if c-syntactic-indentation
656 (c-indent-line))
641 (when c-auto-newline 657 (when c-auto-newline
642 ;; Do all appropriate clean ups 658 ;; Do all appropriate clean ups
643 (let ((here (point)) 659 (let ((here (point))
@@ -1299,7 +1315,7 @@ treated as conditional clause limits. Normally they are ignored."
1299 1315
1300 1316
1301;; commands to indent lines, regions, defuns, and expressions 1317;; commands to indent lines, regions, defuns, and expressions
1302(defun c-indent-command (&optional whole-exp) 1318(defun c-indent-command (&optional arg)
1303 "Indent current line as C code, and/or insert some whitespace. 1319 "Indent current line as C code, and/or insert some whitespace.
1304 1320
1305If `c-tab-always-indent' is t, always just indent the current line. 1321If `c-tab-always-indent' is t, always just indent the current line.
@@ -1309,10 +1325,16 @@ other than nil or t, then some whitespace[*] is inserted only within
1309literals (comments and strings) and inside preprocessor directives, 1325literals (comments and strings) and inside preprocessor directives,
1310but the line is always reindented. 1326but the line is always reindented.
1311 1327
1312A numeric argument, regardless of its value, means indent rigidly all 1328If `c-syntactic-indentation' is t, indentation is done according to
1313the lines of the expression starting after point so that this line 1329the syntactic context. If it's nil, the line is just indented one
1314becomes properly indented. The relative indentation among the lines 1330step according to `c-basic-offset'. In this mode, a numeric argument
1315of the expression is preserved. 1331indents a number of such steps, positive or negative, and an empty
1332prefix argument is equivalent to -1.
1333
1334If `c-syntactic-indentation' is t, then a numeric argument, regardless
1335of its value, means indent rigidly all the lines of the expression
1336starting after point so that this line becomes properly indented. The
1337relative indentation among the lines of the expression is preserved.
1316 1338
1317 [*] The amount and kind of whitespace inserted is controlled by the 1339 [*] The amount and kind of whitespace inserted is controlled by the
1318 variable `c-insert-tab-function', which is called to do the actual 1340 variable `c-insert-tab-function', which is called to do the actual
@@ -1320,11 +1342,21 @@ of the expression is preserved.
1320 just inserts a tab character, or the equivalent number of spaces, 1342 just inserts a tab character, or the equivalent number of spaces,
1321 depending on the variable `indent-tabs-mode'." 1343 depending on the variable `indent-tabs-mode'."
1322 1344
1323 (interactive "P") 1345 (interactive "p")
1324 (let ((bod (c-point 'bod))) 1346 (let ((bod (c-point 'bod))
1325 (if whole-exp 1347 (indent-function
1326 ;; If arg, always indent this line as C 1348 (if c-syntactic-indentation
1327 ;; and shift remaining lines of expression the same amount. 1349 (symbol-function 'c-indent-line)
1350 (lambda ()
1351 (let ((steps (cond ((not current-prefix-arg) 1)
1352 ((equal current-prefix-arg '(4)) -1)
1353 (t arg))))
1354 (c-shift-line-indentation (* steps c-basic-offset)))
1355 ))))
1356 (if (and c-syntactic-indentation current-prefix-arg)
1357 ;; If c-syntactic-indentation and got arg, always indent this
1358 ;; line as C and shift remaining lines of expression the same
1359 ;; amount.
1328 (let ((shift-amt (c-indent-line)) 1360 (let ((shift-amt (c-indent-line))
1329 beg end) 1361 beg end)
1330 (save-excursion 1362 (save-excursion
@@ -1338,8 +1370,7 @@ of the expression is preserved.
1338 (setq beg (point))) 1370 (setq beg (point)))
1339 (if (> end beg) 1371 (if (> end beg)
1340 (indent-code-rigidly beg end (- shift-amt) "#"))) 1372 (indent-code-rigidly beg end (- shift-amt) "#")))
1341 ;; No arg supplied, use c-tab-always-indent to determine 1373 ;; Else use c-tab-always-indent to determine behavior.
1342 ;; behavior
1343 (cond 1374 (cond
1344 ;; CASE 1: indent when at column zero or in lines indentation, 1375 ;; CASE 1: indent when at column zero or in lines indentation,
1345 ;; otherwise insert a tab 1376 ;; otherwise insert a tab
@@ -1348,20 +1379,20 @@ of the expression is preserved.
1348 (skip-chars-backward " \t") 1379 (skip-chars-backward " \t")
1349 (not (bolp))) 1380 (not (bolp)))
1350 (funcall c-insert-tab-function) 1381 (funcall c-insert-tab-function)
1351 (c-indent-line))) 1382 (funcall indent-function)))
1352 ;; CASE 2: just indent the line 1383 ;; CASE 2: just indent the line
1353 ((eq c-tab-always-indent t) 1384 ((eq c-tab-always-indent t)
1354 (c-indent-line)) 1385 (funcall indent-function))
1355 ;; CASE 3: if in a literal, insert a tab, but always indent the 1386 ;; CASE 3: if in a literal, insert a tab, but always indent the
1356 ;; line 1387 ;; line
1357 (t 1388 (t
1358 (if (c-in-literal bod) 1389 (if (c-in-literal bod)
1359 (funcall c-insert-tab-function)) 1390 (funcall c-insert-tab-function))
1360 (c-indent-line) 1391 (funcall indent-function)
1361 ))))) 1392 )))))
1362 1393
1363(defun c-indent-exp (&optional shutup-p) 1394(defun c-indent-exp (&optional shutup-p)
1364 "Indent each line in balanced expression following point. 1395 "Indent each line in balanced expression following point syntactically.
1365Optional SHUTUP-P if non-nil, inhibits message printing and error checking." 1396Optional SHUTUP-P if non-nil, inhibits message printing and error checking."
1366 (interactive "*P") 1397 (interactive "*P")
1367 (let ((here (point-marker)) 1398 (let ((here (point-marker))
@@ -1411,7 +1442,8 @@ Optional SHUTUP-P if non-nil, inhibits message printing and error checking."
1411 (set-marker here nil)))) 1442 (set-marker here nil))))
1412 1443
1413(defun c-indent-defun () 1444(defun c-indent-defun ()
1414 "Re-indents the current top-level function def, struct or class declaration." 1445 "Re-indents the current top-level function def, struct or class declaration
1446syntactically."
1415 (interactive "*") 1447 (interactive "*")
1416 (let ((here (point-marker)) 1448 (let ((here (point-marker))
1417 (c-echo-syntactic-information-p nil) 1449 (c-echo-syntactic-information-p nil)
@@ -2221,9 +2253,9 @@ Warning: `c-comment-prefix-regexp' doesn't match the comment prefix %S"
2221If inside a comment and `comment-multi-line' is non-nil, the 2253If inside a comment and `comment-multi-line' is non-nil, the
2222indentation and line prefix are preserved (see the 2254indentation and line prefix are preserved (see the
2223`c-comment-prefix-regexp' and `c-block-comment-prefix' variables for 2255`c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
2224details). If inside a comment and `comment-multi-line' is nil, a new 2256details). If inside a single line comment and `comment-multi-line' is
2225comment of the same type is started on the next line and indented as 2257nil, a new comment of the same type is started on the next line and
2226appropriate for comments. 2258indented as appropriate for comments.
2227 2259
2228If a fill prefix is specified, it overrides all the above." 2260If a fill prefix is specified, it overrides all the above."
2229 (interactive) 2261 (interactive)
@@ -2252,8 +2284,20 @@ If a fill prefix is specified, it overrides all the above."
2252 (t c-lit-type)) 2284 (t c-lit-type))
2253 c-ignore-auto-fill) 2285 c-ignore-auto-fill)
2254 (setq fill-prefix t) ; Used as flag in the cond. 2286 (setq fill-prefix t) ; Used as flag in the cond.
2255 (if (null c-auto-fill-prefix) 2287 (if (and (null c-auto-fill-prefix)
2256 (setq fill-prefix nil)))) 2288 (eq c-lit-type 'c)
2289 (<= (c-point 'bol) (car c-lit-limits)))
2290 ;; The adaptive fill function has generated a prefix, but
2291 ;; we're on the first line in a block comment so it'll be
2292 ;; wrong. Ignore it to guess a better one below.
2293 (setq fill-prefix nil)
2294 (when (and (eq c-lit-type 'c++)
2295 (not (string-match "\\`[ \t]*//" (or fill-prefix ""))))
2296 ;; Kludge: If the function that adapted the fill prefix
2297 ;; doesn't produce the required comment starter for line
2298 ;; comments, then we ignore it.
2299 (setq fill-prefix nil)))
2300 ))
2257 (cond ((eq fill-prefix t) 2301 (cond ((eq fill-prefix t)
2258 ;; A call from do-auto-fill which should be ignored. 2302 ;; A call from do-auto-fill which should be ignored.
2259 ) 2303 )
@@ -2267,7 +2311,11 @@ If a fill prefix is specified, it overrides all the above."
2267 (unless c-lit-type 2311 (unless c-lit-type
2268 (setq c-lit-type (c-literal-type c-lit-limits))) 2312 (setq c-lit-type (c-literal-type c-lit-limits)))
2269 (memq c-lit-type '(c c++))) 2313 (memq c-lit-type '(c c++)))
2270 (if comment-multi-line 2314 (if (or comment-multi-line
2315 (save-excursion
2316 (goto-char (car c-lit-limits))
2317 (end-of-line)
2318 (< (point) (cdr c-lit-limits))))
2271 ;; Inside a comment that should be continued. 2319 ;; Inside a comment that should be continued.
2272 (let ((fill (c-guess-fill-prefix 2320 (let ((fill (c-guess-fill-prefix
2273 (setq c-lit-limits 2321 (setq c-lit-limits