1 ;;; cc-styles.el --- support for styles in CC Mode
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
7 ;; Authors: 2004- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs and Stewart Clamen
11 ;; 1985 Richard M. Stallman
12 ;; Maintainer: bug-cc-mode@gnu.org
13 ;; Created: 22-Apr-1997 (split from cc-mode.el)
14 ;; Version: See cc-mode.el
15 ;; Keywords: c languages oop
17 ;; This file is part of GNU Emacs.
19 ;; GNU Emacs is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation; either version 3, or (at your option)
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with this program; see the file COPYING. If not, write to
31 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 ;; Boston, MA 02110-1301, USA.
40 (if (and (boundp 'byte-compile-dest-file
)
41 (stringp byte-compile-dest-file
))
42 (cons (file-name-directory byte-compile-dest-file
) load-path
)
44 (load "cc-bytecomp" nil t
)))
48 (cc-require 'cc-align
)
49 ;; cc-align is only indirectly required: Styles added with
50 ;; `c-add-style' often contains references to functions defined there.
52 ;; Silence the compiler.
53 (cc-bytecomp-defvar adaptive-fill-first-line-regexp
) ; Emacs
54 (cc-bytecomp-obsolete-fun make-local-hook
) ; Marked obsolete in Emacs 21.1.
60 (c-comment-only-line-offset .
(0 .
0))
61 (c-hanging-braces-alist .
((substatement-open before after
)
62 (arglist-cont-nonempty)))
63 (c-offsets-alist .
((statement-block-intro .
+)
64 (knr-argdecl-intro .
5)
65 (substatement-open .
+)
66 (substatement-label .
0)
68 (statement-case-open .
+)
70 (arglist-intro . c-lineup-arglist-intro-after-paren
)
71 (arglist-close . c-lineup-arglist
)
75 .
(first c-lineup-topmost-intro-cont
76 c-lineup-gnu-DEFUN-intro-cont
))))
77 (c-special-indent-hook . c-gnu-impose-minimum
)
78 (c-block-comment-prefix .
""))
82 (c-comment-only-line-offset .
0)
83 (c-offsets-alist .
((statement-block-intro .
+)
84 (knr-argdecl-intro .
0)
85 (substatement-open .
0)
86 (substatement-label .
0)
88 (statement-cont .
+))))
92 (c-comment-only-line-offset .
0)
93 (c-offsets-alist .
((statement-block-intro .
+)
94 (knr-argdecl-intro .
+)
95 (substatement-open .
0)
96 (substatement-label .
0)
100 (inexpr-class .
0))))
104 (c-comment-only-line-offset .
0)
105 (c-offsets-alist .
((statement-block-intro .
+)
106 (substatement-open .
0)
107 (substatement-label .
0)
109 (statement-cont .
+))))
113 (c-comment-only-line-offset .
0)
114 ;; It's obvious that the CC Mode way of choosing anchor positions
115 ;; doesn't fit this style at all. :P
116 (c-offsets-alist .
((defun-open .
+)
117 (defun-close . c-lineup-whitesmith-in-block
)
118 (defun-block-intro .
(add c-lineup-whitesmith-in-block
119 c-indent-multi-line-block
))
123 (inline-close . c-lineup-whitesmith-in-block
)
124 (knr-argdecl-intro .
+)
125 (block-open .
0) ; Get indentation from `statement' instead.
126 (block-close . c-lineup-whitesmith-in-block
)
127 (brace-list-open .
+)
128 (brace-list-close . c-lineup-whitesmith-in-block
)
129 (brace-list-intro .
(add c-lineup-whitesmith-in-block
130 c-indent-multi-line-block
))
131 (brace-list-entry .
(add c-lineup-after-whitesmith-blocks
132 c-indent-multi-line-block
))
133 (brace-entry-open .
(add c-lineup-after-whitesmith-blocks
134 c-indent-multi-line-block
))
135 (statement .
(add c-lineup-after-whitesmith-blocks
136 c-indent-multi-line-block
))
137 (statement-block-intro .
(add c-lineup-whitesmith-in-block
138 c-indent-multi-line-block
))
139 (substatement-open .
+)
140 (substatement-label .
+)
142 (arglist-intro .
(add c-lineup-whitesmith-in-block
143 c-indent-multi-line-block
))
144 (arglist-cont .
(add c-lineup-after-whitesmith-blocks
145 c-indent-multi-line-block
))
146 (arglist-cont-nonempty .
(add c-lineup-whitesmith-in-block
147 c-indent-multi-line-block
))
148 (arglist-close . c-lineup-whitesmith-in-block
)
149 (inclass . c-lineup-whitesmith-in-block
)
150 (extern-lang-open .
+)
153 (composition-open .
+)
154 (extern-lang-close .
+)
155 (namespace-close .
+)
157 (composition-close .
+)
158 (inextern-lang . c-lineup-whitesmith-in-block
)
159 (innamespace . c-lineup-whitesmith-in-block
)
160 (inmodule . c-lineup-whitesmith-in-block
)
161 (incomposition . c-lineup-whitesmith-in-block
)
162 (inexpr-class .
0))))
166 (c-comment-only-line-offset .
0)
167 (c-hanging-braces-alist .
((substatement-open before after
)))
168 (c-offsets-alist .
((topmost-intro .
0)
170 (substatement-open .
0)
175 (arglist-cont-nonempty))))
179 (c-comment-only-line-offset .
0)
180 (c-hanging-braces-alist .
((brace-list-open)
182 (substatement-open after
)
183 (block-close . c-snug-do-while
)
184 (arglist-cont-nonempty)))
185 (c-cleanup-list .
(brace-else-brace))
186 (c-offsets-alist .
((statement-block-intro .
+)
187 (knr-argdecl-intro .
0)
188 (substatement-open .
0)
189 (substatement-label .
0)
191 (statement-cont .
+))))
194 (indent-tabs-mode . t
)
197 (c-offsets-alist .
((substatement-open .
0)
200 (knr-argdecl-intro .
+)))
201 (c-hanging-braces-alist .
((brace-list-open)
205 (substatement-open after
)
206 (block-close . c-snug-do-while
)
207 (arglist-cont-nonempty)))
208 (c-block-comment-prefix .
""))
212 (c-comment-only-line-offset .
(0 .
0))
213 ;; the following preserves Javadoc starter lines
214 (c-offsets-alist .
((inline-open .
0)
215 (topmost-intro-cont .
+)
216 (statement-block-intro .
+)
217 (knr-argdecl-intro .
5)
218 (substatement-open .
+)
219 (substatement-label .
+)
221 (statement-case-open .
+)
223 (arglist-intro . c-lineup-arglist-intro-after-paren
)
224 (arglist-close . c-lineup-arglist
)
226 (inher-cont . c-lineup-java-inher
)
227 (func-decl-cont . c-lineup-java-throws
))))
229 ;; awk style exists primarily for auto-newline settings. Otherwise it's
230 ;; pretty much like k&r.
233 (c-comment-only-line-offset .
0)
234 (c-hanging-braces-alist .
((defun-open after
)
235 (defun-close . c-snug-1line-defun-close
)
236 (substatement-open after
)
237 (block-close . c-snug-do-while
)
238 (arglist-cont-nonempty)))
239 (c-hanging-semi&comma-criteria . nil
)
240 (c-cleanup-list . nil
) ; You might want one-liner-defun here.
241 (c-offsets-alist .
((statement-block-intro .
+)
242 (substatement-open .
0)
243 (statement-cont .
+))))
246 "Styles of indentation.
247 Elements of this alist are of the form:
249 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
251 where STYLE-STRING is a short descriptive string used to select a
252 style, VARIABLE is any Emacs variable, and VALUE is the intended value
253 for that variable when using the selected style.
255 Optional BASE-STYLE if present, is a string and must follow
256 STYLE-STRING. BASE-STYLE names a style that this style inherits from.
257 By default, all styles inherit from the \"user\" style, which is
258 computed at run time. Style loops generate errors.
260 Two variables are treated specially. When VARIABLE is
261 `c-offsets-alist', the VALUE is a list containing elements of the
264 (SYNTACTIC-SYMBOL . OFFSET)
266 as described in `c-offsets-alist'. These are passed directly to
267 `c-set-offset' so there is no need to set every syntactic symbol in
268 your style, only those that are different from the default.
270 When VARIABLE is `c-special-indent-hook', its VALUE is added to
271 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each
272 element of the list is added with `add-hook'.
274 Do not change this variable directly. Use the function `c-add-style'
275 to add new styles or modify existing styles (it is not a good idea to
276 modify existing styles -- you should create a new style that inherits
277 the existing style).")
280 ;; Functions that manipulate styles
281 (defun c-set-style-1 (conscell dont-override
)
282 ;; Set the style for one variable
283 (let ((attr (car conscell
))
284 (val (cdr conscell
)))
286 ;; first special variable
287 ((eq attr
'c-offsets-alist
)
288 (let ((offsets (cond ((eq dont-override t
)
291 (default-value 'c-offsets-alist
)))))
292 (mapcar (lambda (langentry)
293 (let ((langelem (car langentry
))
294 (offset (cdr langentry
)))
295 (unless (assq langelem offsets
)
296 (c-set-offset langelem offset
))))
298 ;; second special variable
299 ((eq attr
'c-special-indent-hook
)
300 ;; Maybe we should ignore dont-override here and always add new
302 (unless (cond ((eq dont-override t
)
303 c-special-indent-hook
)
305 (default-value 'c-special-indent-hook
)))
307 (mapcar (lambda (func)
308 (add-hook 'c-special-indent-hook func t t
))
310 (add-hook 'c-special-indent-hook val t t
))))
311 ;; all other variables
312 (t (when (or (not dont-override
)
313 (not (memq attr c-style-variables
))
314 (eq (if (eq dont-override t
)
316 (default-value attr
))
319 ;; Must update a number of other variables if
320 ;; c-comment-prefix-regexp is set.
321 (if (eq attr
'c-comment-prefix-regexp
)
322 (c-setup-paragraph-variables)))))))
324 (defun c-get-style-variables (style basestyles
)
325 ;; Return all variables in a style by resolving inheritances.
327 (copy-alist c-fallback-style
)
328 (let ((vars (cdr (or (assoc (downcase style
) c-style-alist
)
329 (assoc (upcase style
) c-style-alist
)
330 (assoc style c-style-alist
)
332 (c-benign-error "Undefined style: %s" style
)
334 (let ((base (and (stringp (car-safe vars
))
336 (downcase (car vars
))
337 (setq vars
(cdr vars
))))))
338 (if (memq base basestyles
)
339 (c-benign-error "Style loop detected: %s in %s" base basestyles
)
340 (nconc (c-get-style-variables base
(cons base basestyles
))
341 (copy-alist vars
)))))))
343 (defvar c-set-style-history nil
)
346 (defun c-set-style (stylename &optional dont-override
)
347 "Set the current buffer to use the style STYLENAME.
348 STYLENAME, a string, must be an existing CC Mode style - These are contained
349 in the variable `c-style-alist'.
351 The variable `c-indentation-style' will get set to STYLENAME.
353 \"Setting the style\" is done by setting CC Mode's \"style variables\" to the
354 values indicated by the pertinent entry in `c-style-alist'. Other variables
357 If DONT-OVERRIDE is neither nil nor t, style variables whose default values
358 have been set (more precisely, whose default values are not the symbol
359 `set-from-style') will not be changed. This avoids overriding global settings
360 done in ~/.emacs. It is useful to call c-set-style from a mode hook in this
363 If DONT-OVERRIDE is t, style variables that already have values (i.e., whose
364 values are not the symbol `set-from-style') will not be overridden. CC Mode
365 calls c-set-style internally in this way whilst initializing a buffer; if
366 cc-set-style is called like this from anywhere else, it will usually behave as
369 (list (let ((completion-ignore-case t
)
370 (prompt (format "Which %s indentation style? "
372 (completing-read prompt c-style-alist nil t nil
374 c-indentation-style
))))
375 (or c-buffer-is-cc-mode
376 (error "Buffer %s is not a CC Mode buffer (c-set-style)" (buffer-name)))
377 (or (stringp stylename
)
378 (error "Argument to c-set-style was not a string"))
379 (c-initialize-builtin-style)
380 (let ((vars (c-get-style-variables stylename nil
)))
381 (unless dont-override
382 ;; Since we always add to c-special-indent-hook we must reset it
383 ;; first, or else the hooks from the preceding style will
384 ;; remain. This is not necessary for c-offsets-alist, since
385 ;; c-get-style-variables contains every valid offset type in the
387 (setq c-special-indent-hook
388 (default-value 'c-special-indent-hook
)))
389 (mapcar (lambda (elem)
390 (c-set-style-1 elem dont-override
))
391 ;; Need to go through the variables backwards when we
392 ;; don't override any settings.
393 (if (eq dont-override t
) (nreverse vars
) vars
)))
394 (setq c-indentation-style stylename
)
395 (c-keep-region-active))
398 (defun c-add-style (style description
&optional set-p
)
399 "Adds a style to `c-style-alist', or updates an existing one.
400 STYLE is a string identifying the style to add or update. DESCRIPTION
401 is an association list describing the style and must be of the form:
403 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
405 See the variable `c-style-alist' for the semantics of BASESTYLE,
406 VARIABLE and VALUE. This function also sets the current style to
407 STYLE using `c-set-style' if the optional SET-P flag is non-nil."
409 (let ((stylename (completing-read "Style to add: " c-style-alist
410 nil nil nil
'c-set-style-history
))
411 (descr (eval-minibuffer "Style description: ")))
412 (list stylename descr
413 (y-or-n-p "Set the style too? "))))
414 (setq style
(downcase style
))
415 (let ((s (assoc style c-style-alist
)))
417 (setcdr s
(copy-alist description
)) ; replace
418 (setq c-style-alist
(cons (cons style description
) c-style-alist
))))
419 (and set-p
(c-set-style style
)))
422 (defvar c-read-offset-history nil
)
424 (defun c-read-offset (langelem)
425 ;; read new offset value for LANGELEM from minibuffer. return a
427 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist
)
428 (assq langelem
(get 'c-offsets-alist
429 'c-stylevar-fallback
)))))
430 (symname (symbol-name langelem
))
431 (defstr (format "(default %s): " oldoff
))
432 (errmsg (concat "Offset must be int, func, var, vector, list, "
433 "or [+,-,++,--,*,/] "
435 (prompt (concat symname
" offset " defstr
))
436 (keymap (make-sparse-keymap))
437 (minibuffer-completion-table obarray
)
438 (minibuffer-completion-predicate 'fboundp
)
440 ;; In principle completing-read is used here, but SPC is unbound
441 ;; to make it less annoying to enter lists.
442 (set-keymap-parent keymap minibuffer-local-completion-map
)
443 (define-key keymap
" " 'self-insert-command
)
445 (setq input
(read-from-minibuffer prompt nil keymap t
446 'c-read-offset-history
447 (format "%s" oldoff
)))
448 (if (c-valid-offset input
)
450 ;; error, but don't signal one, keep trying
451 ;; to read an input value
453 (setq prompt errmsg
)))
457 (defun c-set-offset (symbol offset
&optional ignored
)
458 "Change the value of a syntactic element symbol in `c-offsets-alist'.
459 SYMBOL is the syntactic element symbol to change and OFFSET is the new
460 offset for that syntactic element. The optional argument is not used
461 and exists only for compatibility reasons."
464 (intern (completing-read
465 (concat "Syntactic symbol to change"
466 (if current-prefix-arg
" or add" "")
470 (cons (format "%s" (car langelem
)) nil
))
471 (get 'c-offsets-alist
'c-stylevar-fallback
))
472 nil
(not current-prefix-arg
)
473 ;; initial contents tries to be the last element
474 ;; on the syntactic analysis list for the current
476 (and c-buffer-is-cc-mode
478 ((syntax (c-guess-basic-syntax))
479 (len (length syntax
))
480 (ic (format "%s" (car (nth (1- len
) syntax
)))))
483 (offset (c-read-offset langelem
)))
484 (list langelem offset current-prefix-arg
)))
485 ;; sanity check offset
486 (if (c-valid-offset offset
)
487 (let ((entry (assq symbol c-offsets-alist
)))
489 (setcdr entry offset
)
490 (if (assq symbol
(get 'c-offsets-alist
'c-stylevar-fallback
))
491 (setq c-offsets-alist
(cons (cons symbol offset
)
493 (c-benign-error "%s is not a valid syntactic symbol" symbol
))))
494 (c-benign-error "Invalid indentation setting for symbol %s: %S"
496 (c-keep-region-active))
499 (defun c-setup-paragraph-variables ()
500 "Fix things up for paragraph recognition and filling inside comments and
501 strings by incorporating the values of `c-comment-prefix-regexp',
502 `sentence-end', `paragraph-start' and `paragraph-separate' in the relevant
506 (or c-buffer-is-cc-mode
507 (error "Buffer %s is not a CC Mode buffer (c-setup-paragraph-variables)"
509 ;; Set up the values for use in comments.
510 (setq c-current-comment-prefix
511 (if (listp c-comment-prefix-regexp
)
512 (cdr-safe (or (assoc major-mode c-comment-prefix-regexp
)
513 (assoc 'other c-comment-prefix-regexp
)))
514 c-comment-prefix-regexp
))
516 (let ((comment-line-prefix
517 (concat "[ \t]*\\(" c-current-comment-prefix
"\\)[ \t]*")))
519 (setq paragraph-start
(concat comment-line-prefix
523 paragraph-separate
(concat comment-line-prefix
527 paragraph-ignore-fill-prefix t
530 (concat comment-line-prefix
531 (if (default-value 'adaptive-fill-regexp
)
533 (default-value 'adaptive-fill-regexp
)
537 (when (boundp 'adaptive-fill-first-line-regexp
)
538 ;; XEmacs adaptive fill mode doesn't have this.
539 (make-local-variable 'adaptive-fill-first-line-regexp
)
540 (setq adaptive-fill-first-line-regexp
541 (concat "\\`" comment-line-prefix
542 ;; Maybe we should incorporate the old value here,
543 ;; but then we have to do all sorts of kludges to
544 ;; deal with the \` and \' it probably contains.
547 ;; Set up the values for use in strings. These are the default
548 ;; paragraph-start/separate values, enhanced to accept escaped EOLs as
549 ;; whitespace. Used in c-beginning/end-of-sentence-in-string in cc-cmds.
550 (setq c-string-par-start
551 ;;(concat "\\(" (default-value 'paragraph-start) "\\)\\|[ \t]*\\\\$"))
553 (setq c-string-par-separate
554 ;;(concat "\\(" (default-value 'paragraph-separate) "\\)\\|[ \t]*\\\\$"))
556 (setq c-sentence-end-with-esc-eol
557 (concat "\\(\\(" (c-default-value-sentence-end) "\\)"
558 ;; N.B.: "$" would be invalid when not enclosed like "\\($\\)".
559 "\\|" "[.?!][]\"')}]* ?\\\\\\($\\)[ \t\n]*"
563 ;; Helper for setting up Filladapt mode. It's not used by CC Mode itself.
565 (cc-bytecomp-defvar filladapt-token-table
)
566 (cc-bytecomp-defvar filladapt-token-match-table
)
567 (cc-bytecomp-defvar filladapt-token-conversion-table
)
569 (defun c-setup-filladapt ()
570 "Convenience function to configure Kyle E. Jones' Filladapt mode for
571 CC Mode by making sure the proper entries are present on
572 `filladapt-token-table', `filladapt-token-match-table', and
573 `filladapt-token-conversion-table'. This is intended to be used on
574 `c-mode-common-hook' or similar."
575 ;; This function is intended to be used explicitly by the end user
578 ;; The default configuration already handles C++ comments, but we
579 ;; need to add handling of C block comments. A new filladapt token
580 ;; `c-comment' is added for that.
582 (setq p filladapt-token-table
)
583 (while (and p
(not (eq (car-safe (cdr-safe (car-safe p
))) 'c-comment
)))
584 (setq p
(cdr-safe p
)))
586 (setcar (car p
) c-current-comment-prefix
)
587 (setq filladapt-token-table
588 (append (list (car filladapt-token-table
)
589 (list c-current-comment-prefix
'c-comment
))
590 (cdr filladapt-token-table
)))))
591 (unless (assq 'c-comment filladapt-token-match-table
)
592 (setq filladapt-token-match-table
593 (append '((c-comment c-comment
))
594 filladapt-token-match-table
)))
595 (unless (assq 'c-comment filladapt-token-conversion-table
)
596 (setq filladapt-token-conversion-table
597 (append '((c-comment . exact
))
598 filladapt-token-conversion-table
))))
601 (defun c-initialize-builtin-style ()
602 ;; Dynamically append the default value of most variables. This is
603 ;; crucial because future c-set-style calls will always reset the
604 ;; variables first to the `cc-mode' style before instituting the new
605 ;; style. Only do this once!
606 (unless (get 'c-initialize-builtin-style
'is-run
)
607 (put 'c-initialize-builtin-style
'is-run t
)
608 ;;(c-initialize-cc-mode)
609 (unless (assoc "user" c-style-alist
)
610 (let ((vars c-style-variables
) var val uservars
)
613 val
(symbol-value var
)
615 (cond ((eq var
'c-offsets-alist
)
617 (setq uservars
(cons (cons 'c-offsets-alist val
)
619 ((not (eq val
'set-from-style
))
620 (setq uservars
(cons (cons var val
)
622 (c-add-style "user" uservars
)))
623 (unless (assoc "cc-mode" c-style-alist
)
624 (c-add-style "cc-mode" '("user")))
625 (if c-style-variables-are-local-p
626 (c-make-styles-buffer-local))))
628 (defun c-make-styles-buffer-local (&optional this-buf-only-p
)
629 "Make all CC Mode style variables buffer local.
630 If `this-buf-only-p' is non-nil, the style variables will be made
631 buffer local only in the current buffer. Otherwise they'll be made
632 permanently buffer local in any buffer that changes their values.
634 The buffer localness of the style variables are normally controlled
635 with the variable `c-style-variables-are-local-p', so there's seldom
636 any reason to call this function directly."
639 (let ((func (if this-buf-only-p
641 'make-variable-buffer-local
))
642 (varsyms (cons 'c-indentation-style
(copy-alist c-style-variables
))))
643 (delq 'c-special-indent-hook varsyms
)
644 (mapcar func varsyms
)
645 ;; Hooks must be handled specially
647 (make-local-hook 'c-special-indent-hook
)
648 (with-no-warnings (make-variable-buffer-local 'c-special-indent-hook
))
649 (setq c-style-variables-are-local-p t
))
654 (cc-provide 'cc-styles
)
656 ;;; arch-tag: c764f61a-96ba-484a-a68f-101c0e9d5d2c
657 ;;; cc-styles.el ends here