1 ;;; cc-styles.el --- support for styles in CC Mode
3 ;; Copyright (C) 1985,1987,1992-2003, 2004, 2005, 2006 Free Software
6 ;; Authors: 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs and Stewart Clamen
9 ;; 1985 Richard M. Stallman
10 ;; Maintainer: bug-cc-mode@gnu.org
11 ;; Created: 22-Apr-1997 (split from cc-mode.el)
12 ;; Version: See cc-mode.el
13 ;; Keywords: c languages oop
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with this program; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
38 (if (and (boundp 'byte-compile-dest-file
)
39 (stringp byte-compile-dest-file
))
40 (cons (file-name-directory byte-compile-dest-file
) load-path
)
42 (load "cc-bytecomp" nil t
)))
46 (cc-require 'cc-align
)
47 ;; cc-align is only indirectly required: Styles added with
48 ;; `c-add-style' often contains references to functions defined there.
50 ;; Silence the compiler.
51 (cc-bytecomp-defvar adaptive-fill-first-line-regexp
) ; Emacs
52 (cc-bytecomp-obsolete-fun make-local-hook
) ; Marked obsolete in Emacs 21.1.
58 (c-comment-only-line-offset .
(0 .
0))
59 (c-hanging-braces-alist .
((substatement-open before after
)))
60 (c-offsets-alist .
((statement-block-intro .
+)
61 (knr-argdecl-intro .
5)
62 (substatement-open .
+)
63 (substatement-label .
0)
65 (statement-case-open .
+)
67 (arglist-intro . c-lineup-arglist-intro-after-paren
)
68 (arglist-close . c-lineup-arglist
)
70 (brace-list-open .
+)))
71 (c-special-indent-hook . c-gnu-impose-minimum
)
72 (c-block-comment-prefix .
""))
76 (c-comment-only-line-offset .
0)
77 (c-offsets-alist .
((statement-block-intro .
+)
78 (knr-argdecl-intro .
0)
79 (substatement-open .
0)
80 (substatement-label .
0)
82 (statement-cont .
+))))
86 (c-comment-only-line-offset .
0)
87 (c-offsets-alist .
((statement-block-intro .
+)
88 (knr-argdecl-intro .
+)
89 (substatement-open .
0)
90 (substatement-label .
0)
98 (c-comment-only-line-offset .
0)
99 (c-offsets-alist .
((statement-block-intro .
+)
100 (substatement-open .
0)
101 (substatement-label .
0)
103 (statement-cont .
+))))
107 (c-comment-only-line-offset .
0)
108 ;; It's obvious that the CC Mode way of choosing anchor positions
109 ;; doesn't fit this style at all. :P
110 (c-offsets-alist .
((defun-open .
+)
111 (defun-close . c-lineup-whitesmith-in-block
)
112 (defun-block-intro .
(add c-lineup-whitesmith-in-block
113 c-indent-multi-line-block
))
117 (inline-close . c-lineup-whitesmith-in-block
)
118 (knr-argdecl-intro .
+)
119 (block-open .
0) ; Get indentation from `statement' instead.
120 (block-close . c-lineup-whitesmith-in-block
)
121 (brace-list-open .
+)
122 (brace-list-close . c-lineup-whitesmith-in-block
)
123 (brace-list-intro .
(add c-lineup-whitesmith-in-block
124 c-indent-multi-line-block
))
125 (brace-list-entry .
(add c-lineup-after-whitesmith-blocks
126 c-indent-multi-line-block
))
127 (brace-entry-open .
(add c-lineup-after-whitesmith-blocks
128 c-indent-multi-line-block
))
129 (statement .
(add c-lineup-after-whitesmith-blocks
130 c-indent-multi-line-block
))
131 (statement-block-intro .
(add c-lineup-whitesmith-in-block
132 c-indent-multi-line-block
))
133 (substatement-open .
+)
134 (substatement-label .
+)
136 (arglist-intro .
(add c-lineup-whitesmith-in-block
137 c-indent-multi-line-block
))
138 (arglist-cont .
(add c-lineup-after-whitesmith-blocks
139 c-indent-multi-line-block
))
140 (arglist-cont-nonempty .
(add c-lineup-whitesmith-in-block
141 c-indent-multi-line-block
))
142 (arglist-close . c-lineup-whitesmith-in-block
)
143 (inclass . c-lineup-whitesmith-in-block
)
144 (extern-lang-open .
+)
147 (composition-open .
+)
148 (extern-lang-close .
+)
149 (namespace-close .
+)
151 (composition-close .
+)
152 (inextern-lang . c-lineup-whitesmith-in-block
)
153 (innamespace . c-lineup-whitesmith-in-block
)
154 (inmodule . c-lineup-whitesmith-in-block
)
155 (incomposition . c-lineup-whitesmith-in-block
)
156 (inexpr-class .
0))))
160 (c-comment-only-line-offset .
0)
161 (c-hanging-braces-alist .
((substatement-open before after
)))
162 (c-offsets-alist .
((topmost-intro .
0)
164 (substatement-open .
0)
172 (c-comment-only-line-offset .
0)
173 (c-hanging-braces-alist .
((brace-list-open)
175 (substatement-open after
)
176 (block-close . c-snug-do-while
)))
177 (c-cleanup-list .
(brace-else-brace))
178 (c-offsets-alist .
((statement-block-intro .
+)
179 (knr-argdecl-intro .
0)
180 (substatement-open .
0)
181 (substatement-label .
0)
183 (statement-cont .
+))))
186 (indent-tabs-mode . t
)
189 (c-offsets-alist .
((substatement-open .
0)
192 (knr-argdecl-intro .
+)))
193 (c-hanging-braces-alist .
((brace-list-open)
197 (substatement-open after
)
198 (block-close . c-snug-do-while
)))
199 (c-block-comment-prefix .
""))
203 (c-comment-only-line-offset .
(0 .
0))
204 ;; the following preserves Javadoc starter lines
205 (c-offsets-alist .
((inline-open .
0)
206 (topmost-intro-cont .
+)
207 (statement-block-intro .
+)
208 (knr-argdecl-intro .
5)
209 (substatement-open .
+)
210 (substatement-label .
+)
212 (statement-case-open .
+)
214 (arglist-intro . c-lineup-arglist-intro-after-paren
)
215 (arglist-close . c-lineup-arglist
)
217 (inher-cont . c-lineup-java-inher
)
218 (func-decl-cont . c-lineup-java-throws
))))
220 ;; awk style exists primarily for auto-newline settings. Otherwise it's
221 ;; pretty much like k&r.
224 (c-comment-only-line-offset .
0)
225 (c-hanging-braces-alist .
((defun-open after
)
226 (defun-close . c-snug-1line-defun-close
)
227 (substatement-open after
)
228 (block-close . c-snug-do-while
)))
229 (c-hanging-semi&comma-criteria . nil
)
230 (c-cleanup-list . nil
) ; You might want one-liner-defun here.
231 (c-offsets-alist .
((statement-block-intro .
+)
232 (substatement-open .
0)
233 (statement-cont .
+))))
236 "Styles of indentation.
237 Elements of this alist are of the form:
239 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
241 where STYLE-STRING is a short descriptive string used to select a
242 style, VARIABLE is any Emacs variable, and VALUE is the intended value
243 for that variable when using the selected style.
245 Optional BASE-STYLE if present, is a string and must follow
246 STYLE-STRING. BASE-STYLE names a style that this style inherits from.
247 By default, all styles inherit from the \"user\" style, which is
248 computed at run time. Style loops generate errors.
250 Two variables are treated specially. When VARIABLE is
251 `c-offsets-alist', the VALUE is a list containing elements of the
254 (SYNTACTIC-SYMBOL . OFFSET)
256 as described in `c-offsets-alist'. These are passed directly to
257 `c-set-offset' so there is no need to set every syntactic symbol in
258 your style, only those that are different from the default.
260 When VARIABLE is `c-special-indent-hook', its VALUE is added to
261 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each
262 element of the list is added with `add-hook'.
264 Do not change this variable directly. Use the function `c-add-style'
265 to add new styles or modify existing styles (it is not a good idea to
266 modify existing styles -- you should create a new style that inherits
267 the existing style).")
270 ;; Functions that manipulate styles
271 (defun c-set-style-1 (conscell dont-override
)
272 ;; Set the style for one variable
273 (let ((attr (car conscell
))
274 (val (cdr conscell
)))
276 ;; first special variable
277 ((eq attr
'c-offsets-alist
)
278 (let ((offsets (cond ((eq dont-override t
)
281 (default-value 'c-offsets-alist
)))))
282 (mapcar (lambda (langentry)
283 (let ((langelem (car langentry
))
284 (offset (cdr langentry
)))
285 (unless (assq langelem offsets
)
286 (c-set-offset langelem offset
))))
288 ;; second special variable
289 ((eq attr
'c-special-indent-hook
)
290 ;; Maybe we should ignore dont-override here and always add new
292 (unless (cond ((eq dont-override t
)
293 c-special-indent-hook
)
295 (default-value 'c-special-indent-hook
)))
297 (mapcar (lambda (func)
298 (add-hook 'c-special-indent-hook func t t
))
300 (add-hook 'c-special-indent-hook val t t
))))
301 ;; all other variables
302 (t (when (or (not dont-override
)
303 (not (memq attr c-style-variables
))
304 (eq (if (eq dont-override t
)
306 (default-value attr
))
309 ;; Must update a number of other variables if
310 ;; c-comment-prefix-regexp is set.
311 (if (eq attr
'c-comment-prefix-regexp
)
312 (c-setup-paragraph-variables)))))))
314 (defun c-get-style-variables (style basestyles
)
315 ;; Return all variables in a style by resolving inheritances.
317 (copy-alist c-fallback-style
)
318 (let ((vars (cdr (or (assoc (downcase style
) c-style-alist
)
319 (assoc (upcase style
) c-style-alist
)
320 (assoc style c-style-alist
)
322 (c-benign-error "Undefined style: %s" style
)
324 (let ((base (and (stringp (car-safe vars
))
326 (downcase (car vars
))
327 (setq vars
(cdr vars
))))))
328 (if (memq base basestyles
)
329 (c-benign-error "Style loop detected: %s in %s" base basestyles
)
330 (nconc (c-get-style-variables base
(cons base basestyles
))
331 (copy-alist vars
)))))))
333 (defvar c-set-style-history nil
)
336 (defun c-set-style (stylename &optional dont-override
)
337 "Set the current buffer to use the style STYLENAME.
338 STYLENAME, a string, must be an existing CC Mode style - These are contained
339 in the variable `c-style-alist'.
341 The variable `c-indentation-style' will get set to STYLENAME.
343 \"Setting the style\" is done by setting CC Mode's \"style variables\" to the
344 values indicated by the pertinent entry in `c-style-alist'. Other variables
347 If DONT-OVERRIDE is neither nil nor t, style variables whose default values
348 have been set (more precisely, whose default values are not the symbol
349 `set-from-style') will not be changed. This avoids overriding global settings
350 done in ~/.emacs. It is useful to call c-set-style from a mode hook in this
353 If DONT-OVERRIDE is t, style variables that already have values (i.e., whose
354 values are not the symbol `set-from-style') will not be overridden. CC Mode
355 calls c-set-style internally in this way whilst initializing a buffer; if
356 cc-set-style is called like this from anywhere else, it will usually behave as
359 (list (let ((completion-ignore-case t
)
360 (prompt (format "Which %s indentation style? "
362 (completing-read prompt c-style-alist nil t nil
364 c-indentation-style
))))
365 (or c-buffer-is-cc-mode
366 (error "Buffer %s is not a CC Mode buffer (c-set-style)" (buffer-name)))
367 (or (stringp stylename
)
368 (error "Argument to c-set-style was not a string"))
369 (c-initialize-builtin-style)
370 (let ((vars (c-get-style-variables stylename nil
)))
371 (unless dont-override
372 ;; Since we always add to c-special-indent-hook we must reset it
373 ;; first, or else the hooks from the preceding style will
374 ;; remain. This is not necessary for c-offsets-alist, since
375 ;; c-get-style-variables contains every valid offset type in the
377 (setq c-special-indent-hook
378 (default-value 'c-special-indent-hook
)))
379 (mapcar (lambda (elem)
380 (c-set-style-1 elem dont-override
))
381 ;; Need to go through the variables backwards when we
382 ;; don't override any settings.
383 (if (eq dont-override t
) (nreverse vars
) vars
)))
384 (setq c-indentation-style stylename
)
385 (c-keep-region-active))
388 (defun c-add-style (style description
&optional set-p
)
389 "Adds a style to `c-style-alist', or updates an existing one.
390 STYLE is a string identifying the style to add or update. DESCRIPTION
391 is an association list describing the style and must be of the form:
393 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
395 See the variable `c-style-alist' for the semantics of BASESTYLE,
396 VARIABLE and VALUE. This function also sets the current style to
397 STYLE using `c-set-style' if the optional SET-P flag is non-nil."
399 (let ((stylename (completing-read "Style to add: " c-style-alist
400 nil nil nil
'c-set-style-history
))
401 (descr (eval-minibuffer "Style description: ")))
402 (list stylename descr
403 (y-or-n-p "Set the style too? "))))
404 (setq style
(downcase style
))
405 (let ((s (assoc style c-style-alist
)))
407 (setcdr s
(copy-alist description
)) ; replace
408 (setq c-style-alist
(cons (cons style description
) c-style-alist
))))
409 (and set-p
(c-set-style style
)))
412 (defvar c-read-offset-history nil
)
414 (defun c-read-offset (langelem)
415 ;; read new offset value for LANGELEM from minibuffer. return a
417 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist
)
418 (assq langelem
(get 'c-offsets-alist
419 'c-stylevar-fallback
)))))
420 (symname (symbol-name langelem
))
421 (defstr (format "(default %s): " oldoff
))
422 (errmsg (concat "Offset must be int, func, var, vector, list, "
423 "or [+,-,++,--,*,/] "
425 (prompt (concat symname
" offset " defstr
))
426 (keymap (make-sparse-keymap))
427 (minibuffer-completion-table obarray
)
428 (minibuffer-completion-predicate 'fboundp
)
430 ;; In principle completing-read is used here, but SPC is unbound
431 ;; to make it less annoying to enter lists.
432 (set-keymap-parent keymap minibuffer-local-completion-map
)
433 (define-key keymap
" " 'self-insert-command
)
435 (setq input
(read-from-minibuffer prompt nil keymap t
436 'c-read-offset-history
437 (format "%s" oldoff
)))
438 (if (c-valid-offset input
)
440 ;; error, but don't signal one, keep trying
441 ;; to read an input value
443 (setq prompt errmsg
)))
447 (defun c-set-offset (symbol offset
&optional ignored
)
448 "Change the value of a syntactic element symbol in `c-offsets-alist'.
449 SYMBOL is the syntactic element symbol to change and OFFSET is the new
450 offset for that syntactic element. The optional argument is not used
451 and exists only for compatibility reasons."
454 (intern (completing-read
455 (concat "Syntactic symbol to change"
456 (if current-prefix-arg
" or add" "")
460 (cons (format "%s" (car langelem
)) nil
))
461 (get 'c-offsets-alist
'c-stylevar-fallback
))
462 nil
(not current-prefix-arg
)
463 ;; initial contents tries to be the last element
464 ;; on the syntactic analysis list for the current
466 (and c-buffer-is-cc-mode
468 ((syntax (c-guess-basic-syntax))
469 (len (length syntax
))
470 (ic (format "%s" (car (nth (1- len
) syntax
)))))
473 (offset (c-read-offset langelem
)))
474 (list langelem offset current-prefix-arg
)))
475 ;; sanity check offset
476 (if (c-valid-offset offset
)
477 (let ((entry (assq symbol c-offsets-alist
)))
479 (setcdr entry offset
)
480 (if (assq symbol
(get 'c-offsets-alist
'c-stylevar-fallback
))
481 (setq c-offsets-alist
(cons (cons symbol offset
)
483 (c-benign-error "%s is not a valid syntactic symbol" symbol
))))
484 (c-benign-error "Invalid indentation setting for symbol %s: %S"
486 (c-keep-region-active))
489 (defun c-setup-paragraph-variables ()
490 "Fix things up for paragraph recognition and filling inside comments and
491 strings by incorporating the values of `c-comment-prefix-regexp',
492 `sentence-end', `paragraph-start' and `paragraph-separate' in the relevant
496 (or c-buffer-is-cc-mode
497 (error "Buffer %s is not a CC Mode buffer (c-setup-paragraph-variables)"
499 ;; Set up the values for use in comments.
500 (setq c-current-comment-prefix
501 (if (listp c-comment-prefix-regexp
)
502 (cdr-safe (or (assoc major-mode c-comment-prefix-regexp
)
503 (assoc 'other c-comment-prefix-regexp
)))
504 c-comment-prefix-regexp
))
506 (let ((comment-line-prefix
507 (concat "[ \t]*\\(" c-current-comment-prefix
"\\)[ \t]*")))
509 (setq paragraph-start
(concat comment-line-prefix
513 paragraph-separate
(concat comment-line-prefix
517 paragraph-ignore-fill-prefix t
520 (concat comment-line-prefix
521 (if (default-value 'adaptive-fill-regexp
)
523 (default-value 'adaptive-fill-regexp
)
527 (when (boundp 'adaptive-fill-first-line-regexp
)
528 ;; XEmacs adaptive fill mode doesn't have this.
529 (make-local-variable 'adaptive-fill-first-line-regexp
)
530 (setq adaptive-fill-first-line-regexp
531 (concat "\\`" comment-line-prefix
532 ;; Maybe we should incorporate the old value here,
533 ;; but then we have to do all sorts of kludges to
534 ;; deal with the \` and \' it probably contains.
537 ;; Set up the values for use in strings. These are the default
538 ;; paragraph-start/separate values, enhanced to accept escaped EOLs as
539 ;; whitespace. Used in c-beginning/end-of-sentence-in-string in cc-cmds.
540 (setq c-string-par-start
541 ;;(concat "\\(" (default-value 'paragraph-start) "\\)\\|[ \t]*\\\\$"))
543 (setq c-string-par-separate
544 ;;(concat "\\(" (default-value 'paragraph-separate) "\\)\\|[ \t]*\\\\$"))
546 (setq c-sentence-end-with-esc-eol
547 (concat "\\(\\(" (c-default-value-sentence-end) "\\)"
548 ;; N.B.: "$" would be illegal when not enclosed like "\\($\\)".
549 "\\|" "[.?!][]\"')}]* ?\\\\\\($\\)[ \t\n]*"
553 ;; Helper for setting up Filladapt mode. It's not used by CC Mode itself.
555 (cc-bytecomp-defvar filladapt-token-table
)
556 (cc-bytecomp-defvar filladapt-token-match-table
)
557 (cc-bytecomp-defvar filladapt-token-conversion-table
)
559 (defun c-setup-filladapt ()
560 "Convenience function to configure Kyle E. Jones' Filladapt mode for
561 CC Mode by making sure the proper entries are present on
562 `filladapt-token-table', `filladapt-token-match-table', and
563 `filladapt-token-conversion-table'. This is intended to be used on
564 `c-mode-common-hook' or similar."
565 ;; This function is intended to be used explicitly by the end user
568 ;; The default configuration already handles C++ comments, but we
569 ;; need to add handling of C block comments. A new filladapt token
570 ;; `c-comment' is added for that.
572 (setq p filladapt-token-table
)
573 (while (and p
(not (eq (car-safe (cdr-safe (car-safe p
))) 'c-comment
)))
574 (setq p
(cdr-safe p
)))
576 (setcar (car p
) c-current-comment-prefix
)
577 (setq filladapt-token-table
578 (append (list (car filladapt-token-table
)
579 (list c-current-comment-prefix
'c-comment
))
580 (cdr filladapt-token-table
)))))
581 (unless (assq 'c-comment filladapt-token-match-table
)
582 (setq filladapt-token-match-table
583 (append '((c-comment c-comment
))
584 filladapt-token-match-table
)))
585 (unless (assq 'c-comment filladapt-token-conversion-table
)
586 (setq filladapt-token-conversion-table
587 (append '((c-comment . exact
))
588 filladapt-token-conversion-table
))))
591 (defun c-initialize-builtin-style ()
592 ;; Dynamically append the default value of most variables. This is
593 ;; crucial because future c-set-style calls will always reset the
594 ;; variables first to the `cc-mode' style before instituting the new
595 ;; style. Only do this once!
596 (unless (get 'c-initialize-builtin-style
'is-run
)
597 (put 'c-initialize-builtin-style
'is-run t
)
598 ;;(c-initialize-cc-mode)
599 (unless (assoc "user" c-style-alist
)
600 (let ((vars c-style-variables
) var val uservars
)
603 val
(symbol-value var
)
605 (cond ((eq var
'c-offsets-alist
)
607 (setq uservars
(cons (cons 'c-offsets-alist val
)
609 ((not (eq val
'set-from-style
))
610 (setq uservars
(cons (cons var val
)
612 (c-add-style "user" uservars
)))
613 (unless (assoc "cc-mode" c-style-alist
)
614 (c-add-style "cc-mode" '("user")))
615 (if c-style-variables-are-local-p
616 (c-make-styles-buffer-local))))
618 (defun c-make-styles-buffer-local (&optional this-buf-only-p
)
619 "Make all CC Mode style variables buffer local.
620 If `this-buf-only-p' is non-nil, the style variables will be made
621 buffer local only in the current buffer. Otherwise they'll be made
622 permanently buffer local in any buffer that changes their values.
624 The buffer localness of the style variables are normally controlled
625 with the variable `c-style-variables-are-local-p', so there's seldom
626 any reason to call this function directly."
629 (let ((func (if this-buf-only-p
631 'make-variable-buffer-local
))
632 (varsyms (cons 'c-indentation-style
(copy-alist c-style-variables
))))
633 (delq 'c-special-indent-hook varsyms
)
634 (mapcar func varsyms
)
635 ;; Hooks must be handled specially
637 (make-local-hook 'c-special-indent-hook
)
638 (with-no-warnings (make-variable-buffer-local 'c-special-indent-hook
))
639 (setq c-style-variables-are-local-p t
))
644 (cc-provide 'cc-styles
)
646 ;;; arch-tag: c764f61a-96ba-484a-a68f-101c0e9d5d2c
647 ;;; cc-styles.el ends here