Mercurial > emacs
comparison lisp/progmodes/cc-styles.el @ 24282:5b0864259a4b Release_5_25
Installed CC Mode 5.25.
author | Barry A. Warsaw <barry@zope.org> |
---|---|
date | Mon, 08 Feb 1999 16:53:18 +0000 |
parents | 81c1f55424f2 |
children | 03befb219d03 |
comparison
equal
deleted
inserted
replaced
24281:d03b1e915af1 | 24282:5b0864259a4b |
---|---|
1 ;;; cc-styles.el --- support for styles in CC Mode | 1 ;;; cc-styles.el --- support for styles in CC Mode |
2 | 2 |
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc. | 3 ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc. |
4 | 4 |
5 ;; Authors: 1992-1997 Barry A. Warsaw | 5 ;; Authors: 1998 Barry A. Warsaw and Martin Stjernholm |
6 ;; 1992-1997 Barry A. Warsaw | |
6 ;; 1987 Dave Detlefs and Stewart Clamen | 7 ;; 1987 Dave Detlefs and Stewart Clamen |
7 ;; 1985 Richard M. Stallman | 8 ;; 1985 Richard M. Stallman |
8 ;; Maintainer: cc-mode-help@python.org | 9 ;; Maintainer: bug-cc-mode@gnu.org |
9 ;; Created: 22-Apr-1997 (split from cc-mode.el) | 10 ;; Created: 22-Apr-1997 (split from cc-mode.el) |
10 ;; Version: See cc-mode.el | 11 ;; Version: See cc-mode.el |
11 ;; Keywords: c languages oop | 12 ;; Keywords: c languages oop |
12 | 13 |
13 ;; This file is part of GNU Emacs. | 14 ;; This file is part of GNU Emacs. |
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the | 27 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
28 ;; Boston, MA 02111-1307, USA. | 29 ;; Boston, MA 02111-1307, USA. |
29 | 30 |
30 | 31 |
32 ;; explicit compile-time dependencies | |
33 (eval-when-compile | |
34 (require 'cc-defs)) | |
35 | |
31 | 36 |
37 ;; Warning: don't eval-defun this constant or you'll break style inheritance. | |
32 (defconst c-style-alist | 38 (defconst c-style-alist |
33 '(("gnu" | 39 '(("gnu" |
34 (c-basic-offset . 2) | 40 (c-basic-offset . 2) |
35 (c-comment-only-line-offset . (0 . 0)) | 41 (c-comment-only-line-offset . (0 . 0)) |
36 (c-offsets-alist . ((statement-block-intro . +) | 42 (c-offsets-alist . ((statement-block-intro . +) |
39 (label . 0) | 45 (label . 0) |
40 (statement-case-open . +) | 46 (statement-case-open . +) |
41 (statement-cont . +) | 47 (statement-cont . +) |
42 (arglist-intro . c-lineup-arglist-intro-after-paren) | 48 (arglist-intro . c-lineup-arglist-intro-after-paren) |
43 (arglist-close . c-lineup-arglist) | 49 (arglist-close . c-lineup-arglist) |
50 (inline-open . 0) | |
44 )) | 51 )) |
45 (c-special-indent-hook . c-gnu-impose-minimum) | 52 (c-special-indent-hook . c-gnu-impose-minimum) |
46 (c-comment-continuation-stars . "") | 53 (c-comment-continuation-stars . "") |
47 (c-hanging-comment-ender-p . t) | 54 (c-hanging-comment-ender-p . t) |
48 ) | 55 ) |
102 ) | 109 ) |
103 ("linux" | 110 ("linux" |
104 (c-basic-offset . 8) | 111 (c-basic-offset . 8) |
105 (c-comment-only-line-offset . 0) | 112 (c-comment-only-line-offset . 0) |
106 (c-hanging-braces-alist . ((brace-list-open) | 113 (c-hanging-braces-alist . ((brace-list-open) |
114 (brace-entry-open) | |
107 (substatement-open after) | 115 (substatement-open after) |
108 (block-close . c-snug-do-while))) | 116 (block-close . c-snug-do-while))) |
109 (c-cleanup-list . (brace-else-brace)) | 117 (c-cleanup-list . (brace-else-brace)) |
110 (c-offsets-alist . ((statement-block-intro . +) | 118 (c-offsets-alist . ((statement-block-intro . +) |
111 (knr-argdecl-intro . 0) | 119 (knr-argdecl-intro . 0) |
124 (knr-argdecl-intro . +) | 132 (knr-argdecl-intro . +) |
125 )) | 133 )) |
126 (c-hanging-braces-alist . ((brace-list-open) | 134 (c-hanging-braces-alist . ((brace-list-open) |
127 (brace-list-intro) | 135 (brace-list-intro) |
128 (brace-list-close) | 136 (brace-list-close) |
137 (brace-entry-open) | |
129 (substatement-open after) | 138 (substatement-open after) |
130 (block-close . c-snug-do-while) | 139 (block-close . c-snug-do-while) |
131 )) | 140 )) |
132 (c-comment-continuation-stars . "") | 141 (c-comment-continuation-stars . "") |
133 (c-hanging-comment-ender-p . nil) | 142 (c-hanging-comment-ender-p . nil) |
282 (and set-p (c-set-style style))) | 291 (and set-p (c-set-style style))) |
283 | 292 |
284 | 293 |
285 | 294 |
286 (defconst c-offsets-alist | 295 (defconst c-offsets-alist |
287 '((string . -1000) | 296 '((string . c-lineup-dont-change) |
288 (c . c-lineup-C-comments) | 297 (c . c-lineup-C-comments) |
289 (defun-open . 0) | 298 (defun-open . 0) |
290 (defun-close . 0) | 299 (defun-close . 0) |
291 (defun-block-intro . +) | 300 (defun-block-intro . +) |
292 (class-open . 0) | 301 (class-open . 0) |
306 (block-close . 0) | 315 (block-close . 0) |
307 (brace-list-open . 0) | 316 (brace-list-open . 0) |
308 (brace-list-close . 0) | 317 (brace-list-close . 0) |
309 (brace-list-intro . +) | 318 (brace-list-intro . +) |
310 (brace-list-entry . 0) | 319 (brace-list-entry . 0) |
320 (brace-entry-open . 0) | |
311 (statement . 0) | 321 (statement . 0) |
312 ;; some people might prefer | 322 ;; some people might prefer |
313 ;;(statement . c-lineup-runin-statements) | 323 ;;(statement . c-lineup-runin-statements) |
314 (statement-cont . +) | 324 (statement-cont . +) |
315 ;; some people might prefer | 325 ;; some people might prefer |
322 (case-label . 0) | 332 (case-label . 0) |
323 (access-label . -) | 333 (access-label . -) |
324 (label . 2) | 334 (label . 2) |
325 (do-while-closure . 0) | 335 (do-while-closure . 0) |
326 (else-clause . 0) | 336 (else-clause . 0) |
337 (catch-clause . 0) | |
327 (comment-intro . c-lineup-comment) | 338 (comment-intro . c-lineup-comment) |
328 (arglist-intro . +) | 339 (arglist-intro . +) |
329 (arglist-cont . 0) | 340 (arglist-cont . 0) |
330 (arglist-cont-nonempty . c-lineup-arglist) | 341 (arglist-cont-nonempty . c-lineup-arglist) |
331 (arglist-close . +) | 342 (arglist-close . +) |
342 (inextern-lang . +) | 353 (inextern-lang . +) |
343 (namespace-open . 0) | 354 (namespace-open . 0) |
344 (namespace-close . 0) | 355 (namespace-close . 0) |
345 (innamespace . +) | 356 (innamespace . +) |
346 (template-args-cont . +) | 357 (template-args-cont . +) |
358 (inlambda . c-lineup-inexpr-block) | |
359 (lambda-intro-cont . +) | |
360 (inexpr-statement . 0) | |
361 (inexpr-class . +) | |
347 ) | 362 ) |
348 "Association list of syntactic element symbols and indentation offsets. | 363 "Association list of syntactic element symbols and indentation offsets. |
349 As described below, each cons cell in this list has the form: | 364 As described below, each cons cell in this list has the form: |
350 | 365 |
351 (SYNTACTIC-SYMBOL . OFFSET) | 366 (SYNTACTIC-SYMBOL . OFFSET) |
377 latter designate positive or negative multiples of `c-basic-offset', | 392 latter designate positive or negative multiples of `c-basic-offset', |
378 respectively: 1, -1, 2, -2, 0.5, and -0.5. If OFFSET is a function, it | 393 respectively: 1, -1, 2, -2, 0.5, and -0.5. If OFFSET is a function, it |
379 is called with a single argument containing the cons of the syntactic | 394 is called with a single argument containing the cons of the syntactic |
380 element symbol and the relative indent point. The function should | 395 element symbol and the relative indent point. The function should |
381 return an integer offset. | 396 return an integer offset. |
397 | |
398 OFFSET can also be a list, in which case it is recursively evaluated | |
399 using the semantics described above. The first element of the list to | |
400 return a non-nil value succeeds. If none of the elements returns a | |
401 non-nil value, then what happends depends on the value of | |
402 `c-strict-syntax-p'. When `c-strict-syntax-p' is nil, then an offset | |
403 of zero is used, otherwise an error is generated. | |
382 | 404 |
383 Here is the current list of valid syntactic element symbols: | 405 Here is the current list of valid syntactic element symbols: |
384 | 406 |
385 string -- inside multi-line string | 407 string -- inside multi-line string |
386 c -- inside a multi-line C style block comment | 408 c -- inside a multi-line C style block comment |
409 block-close -- statement block close brace | 431 block-close -- statement block close brace |
410 brace-list-open -- open brace of an enum or static array list | 432 brace-list-open -- open brace of an enum or static array list |
411 brace-list-close -- close brace of an enum or static array list | 433 brace-list-close -- close brace of an enum or static array list |
412 brace-list-intro -- first line in an enum or static array list | 434 brace-list-intro -- first line in an enum or static array list |
413 brace-list-entry -- subsequent lines in an enum or static array list | 435 brace-list-entry -- subsequent lines in an enum or static array list |
436 brace-entry-open -- subsequent lines in an enum or static array | |
437 list that start with an open brace. | |
414 statement -- a C (or like) statement | 438 statement -- a C (or like) statement |
415 statement-cont -- a continuation of a C (or like) statement | 439 statement-cont -- a continuation of a C (or like) statement |
416 statement-block-intro -- the first line in a new statement block | 440 statement-block-intro -- the first line in a new statement block |
417 statement-case-intro -- the first line in a case \"block\" | 441 statement-case-intro -- the first line in a case \"block\" |
418 statement-case-open -- the first line in a case block starting with brace | 442 statement-case-open -- the first line in a case block starting with brace |
421 case-label -- a `case' or `default' label | 445 case-label -- a `case' or `default' label |
422 access-label -- C++ private/protected/public access label | 446 access-label -- C++ private/protected/public access label |
423 label -- any ordinary label | 447 label -- any ordinary label |
424 do-while-closure -- the `while' that ends a do/while construct | 448 do-while-closure -- the `while' that ends a do/while construct |
425 else-clause -- the `else' of an if/else construct | 449 else-clause -- the `else' of an if/else construct |
450 catch-clause -- the `catch' or `finally' of a try/catch construct | |
426 comment-intro -- a line containing only a comment introduction | 451 comment-intro -- a line containing only a comment introduction |
427 arglist-intro -- the first line in an argument list | 452 arglist-intro -- the first line in an argument list |
428 arglist-cont -- subsequent argument list lines when no | 453 arglist-cont -- subsequent argument list lines when no |
429 arguments follow on the same line as the | 454 arguments follow on the same line as the |
430 arglist opening paren | 455 arglist opening paren |
448 namespace-open -- brace that opens a C++ namespace block | 473 namespace-open -- brace that opens a C++ namespace block |
449 namespace-close -- brace that closes a C++ namespace block | 474 namespace-close -- brace that closes a C++ namespace block |
450 innamespace -- analogous to `inextern-lang' syntactic | 475 innamespace -- analogous to `inextern-lang' syntactic |
451 symbol, but used inside C++ namespace constructs | 476 symbol, but used inside C++ namespace constructs |
452 template-args-cont -- C++ template argument list continuations | 477 template-args-cont -- C++ template argument list continuations |
478 inlambda -- in the header or body of a lambda function | |
479 lambda-intro-cont -- continuation of the header of a lambda function | |
480 inexpr-statement -- the statement is inside an expression | |
481 inexpr-class -- the class is inside an expression | |
453 ") | 482 ") |
483 | |
484 (defun c-evaluate-offset (offset langelem symbol) | |
485 ;; offset can be a number, a function, a variable, a list, or one of | |
486 ;; the symbols + or - | |
487 (cond | |
488 ((eq offset '+) (setq offset c-basic-offset)) | |
489 ((eq offset '-) (setq offset (- c-basic-offset))) | |
490 ((eq offset '++) (setq offset (* 2 c-basic-offset))) | |
491 ((eq offset '--) (setq offset (* 2 (- c-basic-offset)))) | |
492 ((eq offset '*) (setq offset (/ c-basic-offset 2))) | |
493 ((eq offset '/) (setq offset (/ (- c-basic-offset) 2))) | |
494 ((functionp offset) (setq offset (funcall offset langelem))) | |
495 ((listp offset) | |
496 (setq offset | |
497 (let (done) | |
498 (while (and (not done) offset) | |
499 (setq done (c-evaluate-offset (car offset) langelem symbol) | |
500 offset (cdr offset))) | |
501 (if (not done) | |
502 (if c-strict-syntax-p | |
503 (error "No offset found for syntactic symbol %s" symbol) | |
504 0) | |
505 done)))) | |
506 ((not (numberp offset)) (setq offset (symbol-value offset))) | |
507 ) | |
508 offset) | |
454 | 509 |
455 (defun c-get-offset (langelem) | 510 (defun c-get-offset (langelem) |
456 ;; Get offset from LANGELEM which is a cons cell of the form: | 511 ;; Get offset from LANGELEM which is a cons cell of the form: |
457 ;; (SYMBOL . RELPOS). The symbol is matched against | 512 ;; (SYMBOL . RELPOS). The symbol is matched against |
458 ;; c-offsets-alist and the offset found there is either returned, | 513 ;; c-offsets-alist and the offset found there is either returned, |
460 ;; the offset is simply returned. | 515 ;; the offset is simply returned. |
461 (let* ((symbol (car langelem)) | 516 (let* ((symbol (car langelem)) |
462 (relpos (cdr langelem)) | 517 (relpos (cdr langelem)) |
463 (match (assq symbol c-offsets-alist)) | 518 (match (assq symbol c-offsets-alist)) |
464 (offset (cdr-safe match))) | 519 (offset (cdr-safe match))) |
465 ;; offset can be a number, a function, a variable, or one of the | 520 (if (not match) |
466 ;; symbols + or - | 521 (if c-strict-syntax-p |
467 (cond | 522 (error "No offset found for syntactic symbol %s" symbol) |
468 ((not match) | 523 (setq offset 0 |
469 (if c-strict-syntax-p | 524 relpos 0)) |
470 (error "don't know how to indent a %s" symbol) | 525 (setq offset (c-evaluate-offset offset langelem symbol))) |
471 (setq offset 0 | |
472 relpos 0))) | |
473 ((eq offset '+) (setq offset c-basic-offset)) | |
474 ((eq offset '-) (setq offset (- c-basic-offset))) | |
475 ((eq offset '++) (setq offset (* 2 c-basic-offset))) | |
476 ((eq offset '--) (setq offset (* 2 (- c-basic-offset)))) | |
477 ((eq offset '*) (setq offset (/ c-basic-offset 2))) | |
478 ((eq offset '/) (setq offset (/ (- c-basic-offset) 2))) | |
479 ((functionp offset) (setq offset (funcall offset langelem))) | |
480 ((not (numberp offset)) (setq offset (symbol-value offset))) | |
481 ) | |
482 (+ (if (and relpos | 526 (+ (if (and relpos |
483 (< relpos (c-point 'bol))) | 527 (< relpos (c-point 'bol))) |
484 (save-excursion | 528 (save-excursion |
485 (goto-char relpos) | 529 (goto-char relpos) |
486 (current-column)) | 530 (current-column)) |
487 0) | 531 0) |
488 offset))) | 532 (c-evaluate-offset offset langelem symbol)) |
533 )) | |
489 | 534 |
490 | 535 |
491 (defvar c-read-offset-history nil) | 536 (defvar c-read-offset-history nil) |
492 | 537 |
493 (defun c-read-offset (langelem) | 538 (defun c-read-offset (langelem) |
494 ;; read new offset value for LANGELEM from minibuffer. return a | 539 ;; read new offset value for LANGELEM from minibuffer. return a |
495 ;; legal value only | 540 ;; legal value only |
496 (let* ((oldoff (cdr-safe (assq langelem c-offsets-alist))) | 541 (let* ((oldoff (cdr-safe (assq langelem c-offsets-alist))) |
497 (defstr (format "(default %s): " oldoff)) | 542 (symname (symbol-name langelem)) |
498 (errmsg (concat "Offset must be int, func, var, " | 543 (defstr (format "(default %s): " oldoff)) |
499 "or in [+,-,++,--,*,/] " | 544 (errmsg (concat "Offset must be int, func, var, list, " |
500 defstr)) | 545 "or [+,-,++,--,*,/] " |
501 (prompt (concat "Offset " defstr)) | 546 defstr)) |
547 (prompt (concat symname " offset " defstr)) | |
502 offset input interned raw) | 548 offset input interned raw) |
503 (while (not offset) | 549 (while (not offset) |
504 (setq input (completing-read prompt obarray 'fboundp nil nil | 550 (setq input (completing-read prompt obarray 'fboundp nil nil |
505 'c-read-offset-history) | 551 'c-read-offset-history) |
506 offset (cond ((string-equal "" input) oldoff) ; default | 552 offset (cond ((string-equal "" input) oldoff) ; default |
561 (eq offset '--) | 607 (eq offset '--) |
562 (eq offset '*) | 608 (eq offset '*) |
563 (eq offset '/) | 609 (eq offset '/) |
564 (integerp offset) | 610 (integerp offset) |
565 (functionp offset) | 611 (functionp offset) |
612 (listp offset) | |
566 (boundp offset) | 613 (boundp offset) |
567 (error "Offset must be int, func, var, or in [+,-,++,--,*,/]: %s" | 614 (error "Offset must be int, func, var, list, or in [+,-,++,--,*,/]: %s" |
568 offset)) | 615 offset)) |
569 (let ((entry (assq symbol c-offsets-alist))) | 616 (let ((entry (assq symbol c-offsets-alist))) |
570 (if entry | 617 (if entry |
571 (setcdr entry offset) | 618 (setcdr entry offset) |
572 (if add-p | 619 (if add-p |
573 (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist)) | 620 (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist)) |
574 (error "%s is not a valid syntactic symbol." symbol)))) | 621 (error "%s is not a valid syntactic symbol." symbol)))) |
575 (c-keep-region-active)) | 622 (c-keep-region-active)) |
576 | 623 |
577 | |
578 | 624 |
625 (defun c-copy-tree (tree) | |
626 (if (consp tree) | |
627 (cons (c-copy-tree (car tree)) | |
628 (c-copy-tree (cdr tree))) | |
629 tree)) | |
630 | |
579 (defun c-initialize-builtin-style () | 631 (defun c-initialize-builtin-style () |
580 ;; Dynamically append the default value of most variables. This is | 632 ;; Dynamically append the default value of most variables. This is |
581 ;; crucial because future c-set-style calls will always reset the | 633 ;; crucial because future c-set-style calls will always reset the |
582 ;; variables first to the `cc-mode' style before instituting the new | 634 ;; variables first to the `cc-mode' style before instituting the new |
583 ;; style. Only do this once! | 635 ;; style. Only do this once! |
584 (c-initialize-cc-mode t) | 636 (unless (get 'c-initialize-builtin-style 'is-run) |
585 (or (assoc "cc-mode" c-style-alist) | 637 (put 'c-initialize-builtin-style 'is-run t) |
586 (assoc "user" c-style-alist) | 638 (c-initialize-cc-mode) |
587 (progn | 639 (or (assoc "cc-mode" c-style-alist) |
588 (c-add-style "user" | 640 (assoc "user" c-style-alist) |
589 (mapcar | 641 (progn |
590 (function | 642 (c-add-style "user" |
591 (lambda (var) | 643 (mapcar |
592 (let ((val (symbol-value var))) | 644 (function |
593 (cons var (c-copy-tree val))))) | 645 (lambda (var) |
594 '(c-backslash-column | 646 (let ((val (symbol-value var))) |
595 c-basic-offset | 647 (cons var (c-copy-tree val))))) |
596 c-cleanup-list | 648 '(c-backslash-column |
597 c-comment-only-line-offset | 649 c-basic-offset |
598 c-electric-pound-behavior | 650 c-cleanup-list |
599 c-hanging-braces-alist | 651 c-comment-only-line-offset |
600 c-hanging-colons-alist | 652 c-electric-pound-behavior |
601 c-hanging-comment-starter-p | 653 c-hanging-braces-alist |
602 c-hanging-comment-ender-p | 654 c-hanging-colons-alist |
603 c-offsets-alist | 655 c-hanging-comment-starter-p |
604 ))) | 656 c-hanging-comment-ender-p |
605 (c-add-style "cc-mode" '("user")) | 657 c-offsets-alist |
606 ;; the default style is now GNU. This can be overridden in | 658 ))) |
607 ;; c-mode-common-hook or {c,c++,objc,java}-mode-hook. | 659 (c-add-style "cc-mode" '("user")) |
608 (c-set-style c-default-style))) | 660 )) |
609 (if c-style-variables-are-local-p | 661 (if c-style-variables-are-local-p |
610 (c-make-styles-buffer-local))) | 662 (c-make-styles-buffer-local)))) |
611 | 663 |
612 (defun c-copy-tree (tree) | |
613 (if (consp tree) | |
614 (if (consp (cdr tree)) | |
615 (cons (c-copy-tree (car tree)) | |
616 (cons (c-copy-tree (cadr tree)) | |
617 (c-copy-tree (cddr tree)))) | |
618 (cons (c-copy-tree (car tree)) | |
619 (c-copy-tree (cdr tree)))) | |
620 tree)) | |
621 | 664 |
622 (defun c-make-styles-buffer-local (&optional this-buf-only-p) | 665 (defun c-make-styles-buffer-local (&optional this-buf-only-p) |
623 "Make all CC Mode style variables buffer local. | 666 "Make all CC Mode style variables buffer local. |
624 If you edit primarily one style of C (or C++, Objective-C, Java) code, | 667 If you edit primarily one style of C (or C++, Objective-C, Java) code, |
625 you probably want style variables to be global. This is the default. | 668 you probably want style variables to be global. This is the default. |