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, 2009, 2010, 2011
5 ;; Free Software Foundation, Inc.
7 ;; Authors: 2004- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
11 ;; 1987 Stewart Clamen
12 ;; 1985 Richard M. Stallman
13 ;; Maintainer: bug-cc-mode@gnu.org
14 ;; Created: 22-Apr-1997 (split from cc-mode.el)
15 ;; Version: See cc-mode.el
16 ;; Keywords: c languages oop
18 ;; This file is part of GNU Emacs.
20 ;; GNU Emacs is free software: you can redistribute it and/or modify
21 ;; it under the terms of the GNU General Public License as published by
22 ;; the Free Software Foundation, either version 3 of the License, or
23 ;; (at your option) any later version.
25 ;; GNU Emacs is distributed in the hope that it will be useful,
26 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;; GNU General Public License for more details.
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
39 (if (and (boundp 'byte-compile-dest-file
)
40 (stringp byte-compile-dest-file
))
41 (cons (file-name-directory byte-compile-dest-file
) load-path
)
43 (load "cc-bytecomp" nil t
)))
47 (cc-require 'cc-align
)
48 ;; cc-align is only indirectly required: Styles added with
49 ;; `c-add-style' often contains references to functions defined there.
51 ;; Silence the compiler.
52 (cc-bytecomp-defvar adaptive-fill-first-line-regexp
) ; Emacs
53 (cc-bytecomp-obsolete-fun make-local-hook
) ; Marked obsolete in Emacs 21.1.
59 (c-comment-only-line-offset .
(0 .
0))
60 (c-hanging-braces-alist .
((substatement-open before after
)
61 (arglist-cont-nonempty)))
62 (c-offsets-alist .
((statement-block-intro .
+)
63 (knr-argdecl-intro .
5)
64 (substatement-open .
+)
65 (substatement-label .
0)
67 (statement-case-open .
+)
69 (arglist-intro . c-lineup-arglist-intro-after-paren
)
70 (arglist-close . c-lineup-arglist
)
74 .
(first c-lineup-topmost-intro-cont
75 c-lineup-gnu-DEFUN-intro-cont
))))
76 (c-special-indent-hook . c-gnu-impose-minimum
)
77 (c-block-comment-prefix .
""))
81 (c-comment-only-line-offset .
0)
82 (c-offsets-alist .
((statement-block-intro .
+)
83 (knr-argdecl-intro .
0)
84 (substatement-open .
0)
85 (substatement-label .
0)
87 (statement-cont .
+))))
91 (c-comment-only-line-offset .
0)
92 (c-offsets-alist .
((statement-block-intro .
+)
93 (knr-argdecl-intro .
+)
94 (substatement-open .
0)
95 (substatement-label .
0)
103 (c-comment-only-line-offset .
0)
104 (c-offsets-alist .
((statement-block-intro .
+)
105 (substatement-open .
0)
106 (substatement-label .
0)
108 (statement-cont .
+))))
112 (c-comment-only-line-offset .
0)
113 ;; It's obvious that the CC Mode way of choosing anchor positions
114 ;; doesn't fit this style at all. :P
115 (c-offsets-alist .
((defun-open .
+)
116 (defun-close . c-lineup-whitesmith-in-block
)
117 (defun-block-intro .
(add c-lineup-whitesmith-in-block
118 c-indent-multi-line-block
))
122 (inline-close . c-lineup-whitesmith-in-block
)
123 (knr-argdecl-intro .
+)
124 (block-open .
0) ; Get indentation from `statement' instead.
125 (block-close . c-lineup-whitesmith-in-block
)
126 (brace-list-open .
+)
127 (brace-list-close . c-lineup-whitesmith-in-block
)
128 (brace-list-intro .
(add c-lineup-whitesmith-in-block
129 c-indent-multi-line-block
))
130 (brace-list-entry .
(add c-lineup-after-whitesmith-blocks
131 c-indent-multi-line-block
))
132 (brace-entry-open .
(add c-lineup-after-whitesmith-blocks
133 c-indent-multi-line-block
))
134 (statement .
(add c-lineup-after-whitesmith-blocks
135 c-indent-multi-line-block
))
136 (statement-block-intro .
(add c-lineup-whitesmith-in-block
137 c-indent-multi-line-block
))
138 (substatement-open .
+)
139 (substatement-label .
+)
141 (arglist-intro .
(add c-lineup-whitesmith-in-block
142 c-indent-multi-line-block
))
143 (arglist-cont .
(add c-lineup-after-whitesmith-blocks
144 c-indent-multi-line-block
))
145 (arglist-cont-nonempty .
(add c-lineup-whitesmith-in-block
146 c-indent-multi-line-block
))
147 (arglist-close . c-lineup-whitesmith-in-block
)
148 (inclass . c-lineup-whitesmith-in-block
)
149 (extern-lang-open .
+)
152 (composition-open .
+)
153 (extern-lang-close .
+)
154 (namespace-close .
+)
156 (composition-close .
+)
157 (inextern-lang . c-lineup-whitesmith-in-block
)
158 (innamespace . c-lineup-whitesmith-in-block
)
159 (inmodule . c-lineup-whitesmith-in-block
)
160 (incomposition . c-lineup-whitesmith-in-block
)
161 (inexpr-class .
0))))
165 (c-comment-only-line-offset .
0)
166 (c-hanging-braces-alist .
((substatement-open before after
)
167 (arglist-cont-nonempty)))
168 (c-offsets-alist .
((topmost-intro .
0)
170 (substatement-open .
0)
177 (c-comment-only-line-offset .
0)
178 (c-hanging-braces-alist .
((brace-list-open)
180 (substatement-open after
)
181 (block-close . c-snug-do-while
)
182 (arglist-cont-nonempty)))
183 (c-cleanup-list .
(brace-else-brace))
184 (c-offsets-alist .
((statement-block-intro .
+)
185 (knr-argdecl-intro .
0)
186 (substatement-open .
0)
187 (substatement-label .
0)
189 (statement-cont .
+))))
192 (indent-tabs-mode . t
)
195 (c-offsets-alist .
((substatement-open .
0)
198 (knr-argdecl-intro .
+)))
199 (c-hanging-braces-alist .
((brace-list-open)
203 (substatement-open after
)
204 (block-close . c-snug-do-while
)
205 (arglist-cont-nonempty)))
206 (c-block-comment-prefix .
""))
210 (c-comment-only-line-offset .
(0 .
0))
211 ;; the following preserves Javadoc starter lines
212 (c-offsets-alist .
((inline-open .
0)
213 (topmost-intro-cont .
+)
214 (statement-block-intro .
+)
215 (knr-argdecl-intro .
5)
216 (substatement-open .
+)
217 (substatement-label .
+)
219 (statement-case-open .
+)
221 (arglist-intro . c-lineup-arglist-intro-after-paren
)
222 (arglist-close . c-lineup-arglist
)
224 (inher-cont . c-lineup-java-inher
)
225 (func-decl-cont . c-lineup-java-throws
))))
227 ;; awk style exists primarily for auto-newline settings. Otherwise it's
228 ;; pretty much like k&r.
231 (c-comment-only-line-offset .
0)
232 (c-hanging-braces-alist .
((defun-open after
)
233 (defun-close . c-snug-1line-defun-close
)
234 (substatement-open after
)
235 (block-close . c-snug-do-while
)
236 (arglist-cont-nonempty)))
237 (c-hanging-semi&comma-criteria . nil
)
238 (c-cleanup-list . nil
) ; You might want one-liner-defun here.
239 (c-offsets-alist .
((statement-block-intro .
+)
240 (substatement-open .
0)
241 (statement-cont .
+))))
244 "Styles of indentation.
245 Elements of this alist are of the form:
247 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
249 where STYLE-STRING is a short descriptive string used to select a
250 style, VARIABLE is any Emacs variable, and VALUE is the intended value
251 for that variable when using the selected style.
253 Optional BASE-STYLE if present, is a string and must follow
254 STYLE-STRING. BASE-STYLE names a style that this style inherits from.
255 By default, all styles inherit from the \"user\" style, which is
256 computed at run time. Style loops generate errors.
258 Two variables are treated specially. When VARIABLE is
259 `c-offsets-alist', the VALUE is a list containing elements of the
262 (SYNTACTIC-SYMBOL . OFFSET)
264 as described in `c-offsets-alist'. These are passed directly to
265 `c-set-offset' so there is no need to set every syntactic symbol in
266 your style, only those that are different from the default.
268 When VARIABLE is `c-special-indent-hook', its VALUE is added to
269 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each
270 element of the list is added with `add-hook'.
272 Do not change this variable directly. Use the function `c-add-style'
273 to add new styles or modify existing styles (it is not a good idea to
274 modify existing styles -- you should create a new style that inherits
275 the existing style).")
278 ;; Functions that manipulate styles
279 (defun c-set-style-1 (conscell dont-override
)
280 ;; Set the style for one variable
281 (let ((attr (car conscell
))
282 (val (cdr conscell
)))
284 ;; first special variable
285 ((eq attr
'c-offsets-alist
)
286 (let ((offsets (cond ((eq dont-override t
)
289 (default-value 'c-offsets-alist
)))))
290 (mapcar (lambda (langentry)
291 (let ((langelem (car langentry
))
292 (offset (cdr langentry
)))
293 (unless (assq langelem offsets
)
294 (c-set-offset langelem offset
))))
296 ;; second special variable
297 ((eq attr
'c-special-indent-hook
)
298 ;; Maybe we should ignore dont-override here and always add new
300 (unless (cond ((eq dont-override t
)
301 c-special-indent-hook
)
303 (default-value 'c-special-indent-hook
)))
305 (mapcar (lambda (func)
306 (add-hook 'c-special-indent-hook func t t
))
308 (add-hook 'c-special-indent-hook val t t
))))
309 ;; all other variables
310 (t (when (or (not dont-override
)
311 (not (memq attr c-style-variables
))
312 (eq (if (eq dont-override t
)
314 (default-value attr
))
317 ;; Must update a number of other variables if
318 ;; c-comment-prefix-regexp is set.
319 (if (eq attr
'c-comment-prefix-regexp
)
320 (c-setup-paragraph-variables)))))))
322 (defun c-get-style-variables (style basestyles
)
323 ;; Return all variables in a style by resolving inheritances.
325 (copy-alist c-fallback-style
)
326 (let ((vars (cdr (or (assoc (downcase style
) c-style-alist
)
327 (assoc (upcase style
) c-style-alist
)
328 (assoc style c-style-alist
)
330 (c-benign-error "Undefined style: %s" style
)
332 (let ((base (and (stringp (car-safe vars
))
334 (downcase (car vars
))
335 (setq vars
(cdr vars
))))))
336 (if (memq base basestyles
)
337 (c-benign-error "Style loop detected: %s in %s" base basestyles
)
338 (nconc (c-get-style-variables base
(cons base basestyles
))
339 (copy-alist vars
)))))))
341 (defvar c-set-style-history nil
)
344 (defun c-set-style (stylename &optional dont-override
)
345 "Set the current buffer to use the style STYLENAME.
346 STYLENAME, a string, must be an existing CC Mode style - These are contained
347 in the variable `c-style-alist'.
349 The variable `c-indentation-style' will get set to STYLENAME.
351 \"Setting the style\" is done by setting CC Mode's \"style variables\" to the
352 values indicated by the pertinent entry in `c-style-alist'. Other variables
355 If DONT-OVERRIDE is neither nil nor t, style variables whose default values
356 have been set (more precisely, whose default values are not the symbol
357 `set-from-style') will not be changed. This avoids overriding global settings
358 done in ~/.emacs. It is useful to call c-set-style from a mode hook in this
361 If DONT-OVERRIDE is t, style variables that already have values (i.e., whose
362 values are not the symbol `set-from-style') will not be overridden. CC Mode
363 calls c-set-style internally in this way whilst initializing a buffer; if
364 cc-set-style is called like this from anywhere else, it will usually behave as
367 (list (let ((completion-ignore-case t
)
368 (prompt (format "Which %s indentation style? "
370 (completing-read prompt c-style-alist nil t nil
372 c-indentation-style
))))
373 (or c-buffer-is-cc-mode
374 (error "Buffer %s is not a CC Mode buffer (c-set-style)" (buffer-name)))
375 (or (stringp stylename
)
376 (error "Argument to c-set-style was not a string"))
377 (c-initialize-builtin-style)
378 (let ((vars (c-get-style-variables stylename nil
)))
379 (unless dont-override
380 ;; Since we always add to c-special-indent-hook we must reset it
381 ;; first, or else the hooks from the preceding style will
382 ;; remain. This is not necessary for c-offsets-alist, since
383 ;; c-get-style-variables contains every valid offset type in the
385 (setq c-special-indent-hook
386 (default-value 'c-special-indent-hook
)))
388 (c-set-style-1 elem dont-override
))
389 ;; Need to go through the variables backwards when we
390 ;; don't override any settings.
391 (if (eq dont-override t
) (nreverse vars
) vars
)))
392 (setq c-indentation-style stylename
)
393 (c-keep-region-active))
396 (defun c-add-style (style description
&optional set-p
)
397 "Adds a style to `c-style-alist', or updates an existing one.
398 STYLE is a string identifying the style to add or update. DESCRIPTION
399 is an association list describing the style and must be of the form:
401 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
403 See the variable `c-style-alist' for the semantics of BASESTYLE,
404 VARIABLE and VALUE. This function also sets the current style to
405 STYLE using `c-set-style' if the optional SET-P flag is non-nil."
407 (let ((stylename (completing-read "Style to add: " c-style-alist
408 nil nil nil
'c-set-style-history
))
409 (descr (eval-minibuffer "Style description: ")))
410 (list stylename descr
411 (y-or-n-p "Set the style too? "))))
412 (setq style
(downcase style
))
413 (let ((s (assoc style c-style-alist
)))
415 (setcdr s
(copy-alist description
)) ; replace
416 (setq c-style-alist
(cons (cons style description
) c-style-alist
))))
417 (and set-p
(c-set-style style
)))
420 (defvar c-read-offset-history nil
)
422 (defun c-read-offset (langelem)
423 ;; read new offset value for LANGELEM from minibuffer. return a
425 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist
)
426 (assq langelem
(get 'c-offsets-alist
427 'c-stylevar-fallback
)))))
428 (symname (symbol-name langelem
))
429 (defstr (format "(default %s): " oldoff
))
430 (errmsg (concat "Offset must be int, func, var, vector, list, "
431 "or [+,-,++,--,*,/] "
433 (prompt (concat symname
" offset " defstr
))
434 (keymap (make-sparse-keymap))
435 (minibuffer-completion-table obarray
)
436 (minibuffer-completion-predicate 'fboundp
)
438 ;; In principle completing-read is used here, but SPC is unbound
439 ;; to make it less annoying to enter lists.
440 (set-keymap-parent keymap minibuffer-local-completion-map
)
441 (define-key keymap
" " 'self-insert-command
)
443 (setq input
(read-from-minibuffer prompt nil keymap t
444 'c-read-offset-history
445 (format "%s" oldoff
)))
446 (if (c-valid-offset input
)
448 ;; error, but don't signal one, keep trying
449 ;; to read an input value
451 (setq prompt errmsg
)))
455 (defun c-set-offset (symbol offset
&optional ignored
)
456 "Change the value of a syntactic element symbol in `c-offsets-alist'.
457 SYMBOL is the syntactic element symbol to change and OFFSET is the new
458 offset for that syntactic element. The optional argument is not used
459 and exists only for compatibility reasons."
462 (intern (completing-read
463 (concat "Syntactic symbol to change"
464 (if current-prefix-arg
" or add" "")
468 (cons (format "%s" (car langelem
)) nil
))
469 (get 'c-offsets-alist
'c-stylevar-fallback
))
470 nil
(not current-prefix-arg
)
471 ;; initial contents tries to be the last element
472 ;; on the syntactic analysis list for the current
474 (and c-buffer-is-cc-mode
476 ((syntax (c-guess-basic-syntax))
477 (len (length syntax
))
478 (ic (format "%s" (car (nth (1- len
) syntax
)))))
481 (offset (c-read-offset langelem
)))
482 (list langelem offset current-prefix-arg
)))
483 ;; sanity check offset
484 (if (c-valid-offset offset
)
485 (let ((entry (assq symbol c-offsets-alist
)))
487 (setcdr entry offset
)
488 (if (assq symbol
(get 'c-offsets-alist
'c-stylevar-fallback
))
489 (setq c-offsets-alist
(cons (cons symbol offset
)
491 (c-benign-error "%s is not a valid syntactic symbol" symbol
))))
492 (c-benign-error "Invalid indentation setting for symbol %s: %S"
494 (c-keep-region-active))
497 (defun c-setup-paragraph-variables ()
498 "Fix things up for paragraph recognition and filling inside comments and
499 strings by incorporating the values of `c-comment-prefix-regexp',
500 `sentence-end', `paragraph-start' and `paragraph-separate' in the relevant
504 (or c-buffer-is-cc-mode
505 (error "Buffer %s is not a CC Mode buffer (c-setup-paragraph-variables)"
507 ;; Set up the values for use in comments.
508 (setq c-current-comment-prefix
509 (if (listp c-comment-prefix-regexp
)
510 (cdr-safe (or (assoc major-mode c-comment-prefix-regexp
)
511 (assoc 'other c-comment-prefix-regexp
)))
512 c-comment-prefix-regexp
))
514 (let* ((empty-is-prefix (string-match c-current-comment-prefix
""))
515 (nonws-comment-line-prefix
516 (concat "\\(" c-current-comment-prefix
"\\)[ \t]*"))
517 (comment-line-prefix (concat "[ \t]*" nonws-comment-line-prefix
))
518 (blank-or-comment-line-prefix
520 (if empty-is-prefix
"" "\\(")
521 nonws-comment-line-prefix
522 (if empty-is-prefix
"" "\\)?"))))
524 (setq paragraph-start
(concat blank-or-comment-line-prefix
528 paragraph-separate
(concat blank-or-comment-line-prefix
532 paragraph-ignore-fill-prefix t
535 (concat comment-line-prefix
536 (if (default-value 'adaptive-fill-regexp
)
538 (default-value 'adaptive-fill-regexp
)
542 (when (boundp 'adaptive-fill-first-line-regexp
)
543 ;; XEmacs adaptive fill mode doesn't have this.
544 (make-local-variable 'adaptive-fill-first-line-regexp
)
545 (setq adaptive-fill-first-line-regexp
546 (concat "\\`" comment-line-prefix
547 ;; Maybe we should incorporate the old value here,
548 ;; but then we have to do all sorts of kludges to
549 ;; deal with the \` and \' it probably contains.
552 ;; Set up the values for use in strings. These are the default
553 ;; paragraph-start/separate values, enhanced to accept escaped EOLs as
554 ;; whitespace. Used in c-beginning/end-of-sentence-in-string in cc-cmds.
555 (setq c-string-par-start
556 ;;(concat "\\(" (default-value 'paragraph-start) "\\)\\|[ \t]*\\\\$"))
558 (setq c-string-par-separate
559 ;;(concat "\\(" (default-value 'paragraph-separate) "\\)\\|[ \t]*\\\\$"))
561 (setq c-sentence-end-with-esc-eol
562 (concat "\\(\\(" (c-default-value-sentence-end) "\\)"
563 ;; N.B.: "$" would be invalid when not enclosed like "\\($\\)".
564 "\\|" "[.?!][]\"')}]* ?\\\\\\($\\)[ \t\n]*"
568 ;; Helper for setting up Filladapt mode. It's not used by CC Mode itself.
570 (cc-bytecomp-defvar filladapt-token-table
)
571 (cc-bytecomp-defvar filladapt-token-match-table
)
572 (cc-bytecomp-defvar filladapt-token-conversion-table
)
574 (defun c-setup-filladapt ()
575 "Convenience function to configure Kyle E. Jones' Filladapt mode for
576 CC Mode by making sure the proper entries are present on
577 `filladapt-token-table', `filladapt-token-match-table', and
578 `filladapt-token-conversion-table'. This is intended to be used on
579 `c-mode-common-hook' or similar."
580 ;; This function is intended to be used explicitly by the end user
583 ;; The default configuration already handles C++ comments, but we
584 ;; need to add handling of C block comments. A new filladapt token
585 ;; `c-comment' is added for that.
587 (setq p filladapt-token-table
)
588 (while (and p
(not (eq (car-safe (cdr-safe (car-safe p
))) 'c-comment
)))
589 (setq p
(cdr-safe p
)))
591 (setcar (car p
) c-current-comment-prefix
)
592 (setq filladapt-token-table
593 (append (list (car filladapt-token-table
)
594 (list c-current-comment-prefix
'c-comment
))
595 (cdr filladapt-token-table
)))))
596 (unless (assq 'c-comment filladapt-token-match-table
)
597 (setq filladapt-token-match-table
598 (append '((c-comment c-comment
))
599 filladapt-token-match-table
)))
600 (unless (assq 'c-comment filladapt-token-conversion-table
)
601 (setq filladapt-token-conversion-table
602 (append '((c-comment . exact
))
603 filladapt-token-conversion-table
))))
606 (defun c-initialize-builtin-style ()
607 ;; Dynamically append the default value of most variables. This is
608 ;; crucial because future c-set-style calls will always reset the
609 ;; variables first to the `cc-mode' style before instituting the new
610 ;; style. Only do this once!
611 (unless (get 'c-initialize-builtin-style
'is-run
)
612 (put 'c-initialize-builtin-style
'is-run t
)
613 ;;(c-initialize-cc-mode)
614 (unless (assoc "user" c-style-alist
)
615 (let ((vars c-style-variables
) var val uservars
)
618 val
(symbol-value var
)
620 (cond ((eq var
'c-offsets-alist
)
622 (setq uservars
(cons (cons 'c-offsets-alist val
)
624 ((not (eq val
'set-from-style
))
625 (setq uservars
(cons (cons var val
)
627 (c-add-style "user" uservars
)))
628 (unless (assoc "cc-mode" c-style-alist
)
629 (c-add-style "cc-mode" '("user")))
630 (if c-style-variables-are-local-p
631 (c-make-styles-buffer-local))))
633 (defun c-make-styles-buffer-local (&optional this-buf-only-p
)
634 "Make all CC Mode style variables buffer local.
635 If `this-buf-only-p' is non-nil, the style variables will be made
636 buffer local only in the current buffer. Otherwise they'll be made
637 permanently buffer local in any buffer that changes their values.
639 The buffer localness of the style variables are normally controlled
640 with the variable `c-style-variables-are-local-p', so there's seldom
641 any reason to call this function directly."
644 (let ((func (if this-buf-only-p
646 'make-variable-buffer-local
))
647 (varsyms (cons 'c-indentation-style
(copy-alist c-style-variables
))))
648 (delq 'c-special-indent-hook varsyms
)
650 ;; Hooks must be handled specially
652 (make-local-hook 'c-special-indent-hook
)
653 (with-no-warnings (make-variable-buffer-local 'c-special-indent-hook
))
654 (setq c-style-variables-are-local-p t
))
659 (cc-provide 'cc-styles
)
661 ;; arch-tag: c764f61a-96ba-484a-a68f-101c0e9d5d2c
662 ;;; cc-styles.el ends here