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
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 (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 (c-offsets-alist .
((topmost-intro .
0)
169 (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 (c-cleanup-list .
(brace-else-brace))
183 (c-offsets-alist .
((statement-block-intro .
+)
184 (knr-argdecl-intro .
0)
185 (substatement-open .
0)
186 (substatement-label .
0)
188 (statement-cont .
+))))
191 (indent-tabs-mode . t
)
194 (c-offsets-alist .
((substatement-open .
0)
197 (knr-argdecl-intro .
+)))
198 (c-hanging-braces-alist .
((brace-list-open)
202 (substatement-open after
)
203 (block-close . c-snug-do-while
)))
204 (c-block-comment-prefix .
""))
208 (c-comment-only-line-offset .
(0 .
0))
209 ;; the following preserves Javadoc starter lines
210 (c-offsets-alist .
((inline-open .
0)
211 (topmost-intro-cont .
+)
212 (statement-block-intro .
+)
213 (knr-argdecl-intro .
5)
214 (substatement-open .
+)
215 (substatement-label .
+)
217 (statement-case-open .
+)
219 (arglist-intro . c-lineup-arglist-intro-after-paren
)
220 (arglist-close . c-lineup-arglist
)
222 (inher-cont . c-lineup-java-inher
)
223 (func-decl-cont . c-lineup-java-throws
))))
225 ;; awk style exists primarily for auto-newline settings. Otherwise it's
226 ;; pretty much like k&r.
229 (c-comment-only-line-offset .
0)
230 (c-hanging-braces-alist .
((defun-open after
)
231 (defun-close . c-snug-1line-defun-close
)
232 (substatement-open after
)
233 (block-close . c-snug-do-while
)))
234 (c-hanging-semi&comma-criteria . nil
)
235 (c-cleanup-list . nil
) ; You might want one-liner-defun here.
236 (c-offsets-alist .
((statement-block-intro .
+)
237 (substatement-open .
0)
238 (statement-cont .
+))))
241 "Styles of indentation.
242 Elements of this alist are of the form:
244 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
246 where STYLE-STRING is a short descriptive string used to select a
247 style, VARIABLE is any Emacs variable, and VALUE is the intended value
248 for that variable when using the selected style.
250 Optional BASE-STYLE if present, is a string and must follow
251 STYLE-STRING. BASE-STYLE names a style that this style inherits from.
252 By default, all styles inherit from the \"user\" style, which is
253 computed at run time. Style loops generate errors.
255 Two variables are treated specially. When VARIABLE is
256 `c-offsets-alist', the VALUE is a list containing elements of the
259 (SYNTACTIC-SYMBOL . OFFSET)
261 as described in `c-offsets-alist'. These are passed directly to
262 `c-set-offset' so there is no need to set every syntactic symbol in
263 your style, only those that are different from the default.
265 When VARIABLE is `c-special-indent-hook', its VALUE is added to
266 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each
267 element of the list is added with `add-hook'.
269 Do not change this variable directly. Use the function `c-add-style'
270 to add new styles or modify existing styles (it is not a good idea to
271 modify existing styles -- you should create a new style that inherits
272 the existing style).")
275 ;; Functions that manipulate styles
276 (defun c-set-style-1 (conscell dont-override
)
277 ;; Set the style for one variable
278 (let ((attr (car conscell
))
279 (val (cdr conscell
)))
281 ;; first special variable
282 ((eq attr
'c-offsets-alist
)
283 (let ((offsets (cond ((eq dont-override t
)
286 (default-value 'c-offsets-alist
)))))
287 (mapcar (lambda (langentry)
288 (let ((langelem (car langentry
))
289 (offset (cdr langentry
)))
290 (unless (assq langelem offsets
)
291 (c-set-offset langelem offset
))))
293 ;; second special variable
294 ((eq attr
'c-special-indent-hook
)
295 ;; Maybe we should ignore dont-override here and always add new
297 (unless (cond ((eq dont-override t
)
298 c-special-indent-hook
)
300 (default-value 'c-special-indent-hook
)))
302 (mapcar (lambda (func)
303 (add-hook 'c-special-indent-hook func t t
))
305 (add-hook 'c-special-indent-hook val t t
))))
306 ;; all other variables
307 (t (when (or (not dont-override
)
308 (not (memq attr c-style-variables
))
309 (eq (if (eq dont-override t
)
311 (default-value attr
))
314 ;; Must update a number of other variables if
315 ;; c-comment-prefix-regexp is set.
316 (if (eq attr
'c-comment-prefix-regexp
)
317 (c-setup-paragraph-variables)))))))
319 (defun c-get-style-variables (style basestyles
)
320 ;; Return all variables in a style by resolving inheritances.
322 (copy-alist c-fallback-style
)
323 (let ((vars (cdr (or (assoc (downcase style
) c-style-alist
)
324 (assoc (upcase style
) c-style-alist
)
325 (assoc style c-style-alist
)
327 (c-benign-error "Undefined style: %s" style
)
329 (let ((base (and (stringp (car-safe vars
))
331 (downcase (car vars
))
332 (setq vars
(cdr vars
))))))
333 (if (memq base basestyles
)
334 (c-benign-error "Style loop detected: %s in %s" base basestyles
)
335 (nconc (c-get-style-variables base
(cons base basestyles
))
336 (copy-alist vars
)))))))
338 (defvar c-set-style-history nil
)
341 (defun c-set-style (stylename &optional dont-override
)
342 "Set the current buffer to use the style STYLENAME.
343 STYLENAME, a string, must be an existing CC Mode style - These are contained
344 in the variable `c-style-alist'.
346 The variable `c-indentation-style' will get set to STYLENAME.
348 \"Setting the style\" is done by setting CC Mode's \"style variables\" to the
349 values indicated by the pertinent entry in `c-style-alist'. Other variables
352 If DONT-OVERRIDE is neither nil nor t, style variables whose default values
353 have been set (more precisely, whose default values are not the symbol
354 `set-from-style') will not be changed. This avoids overriding global settings
355 done in ~/.emacs. It is useful to call c-set-style from a mode hook in this
358 If DONT-OVERRIDE is t, style variables that already have values (i.e., whose
359 values are not the symbol `set-from-style') will not be overridden. CC Mode
360 calls c-set-style internally in this way whilst initializing a buffer; if
361 cc-set-style is called like this from anywhere else, it will usually behave as
364 (list (let ((completion-ignore-case t
)
365 (prompt (format "Which %s indentation style? "
367 (completing-read prompt c-style-alist nil t nil
369 c-indentation-style
))))
370 (or c-buffer-is-cc-mode
371 (error "Buffer %s is not a CC Mode buffer (c-set-style)" (buffer-name)))
372 (or (stringp stylename
)
373 (error "Argument to c-set-style was not a string"))
374 (c-initialize-builtin-style)
375 (let ((vars (c-get-style-variables stylename nil
)))
376 (unless dont-override
377 ;; Since we always add to c-special-indent-hook we must reset it
378 ;; first, or else the hooks from the preceding style will
379 ;; remain. This is not necessary for c-offsets-alist, since
380 ;; c-get-style-variables contains every valid offset type in the
382 (setq c-special-indent-hook
383 (default-value 'c-special-indent-hook
)))
384 (mapcar (lambda (elem)
385 (c-set-style-1 elem dont-override
))
386 ;; Need to go through the variables backwards when we
387 ;; don't override any settings.
388 (if (eq dont-override t
) (nreverse vars
) vars
)))
389 (setq c-indentation-style stylename
)
390 (c-keep-region-active))
393 (defun c-add-style (style description
&optional set-p
)
394 "Adds a style to `c-style-alist', or updates an existing one.
395 STYLE is a string identifying the style to add or update. DESCRIPTION
396 is an association list describing the style and must be of the form:
398 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
400 See the variable `c-style-alist' for the semantics of BASESTYLE,
401 VARIABLE and VALUE. This function also sets the current style to
402 STYLE using `c-set-style' if the optional SET-P flag is non-nil."
404 (let ((stylename (completing-read "Style to add: " c-style-alist
405 nil nil nil
'c-set-style-history
))
406 (descr (eval-minibuffer "Style description: ")))
407 (list stylename descr
408 (y-or-n-p "Set the style too? "))))
409 (setq style
(downcase style
))
410 (let ((s (assoc style c-style-alist
)))
412 (setcdr s
(copy-alist description
)) ; replace
413 (setq c-style-alist
(cons (cons style description
) c-style-alist
))))
414 (and set-p
(c-set-style style
)))
417 (defvar c-read-offset-history nil
)
419 (defun c-read-offset (langelem)
420 ;; read new offset value for LANGELEM from minibuffer. return a
422 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist
)
423 (assq langelem
(get 'c-offsets-alist
424 'c-stylevar-fallback
)))))
425 (symname (symbol-name langelem
))
426 (defstr (format "(default %s): " oldoff
))
427 (errmsg (concat "Offset must be int, func, var, vector, list, "
428 "or [+,-,++,--,*,/] "
430 (prompt (concat symname
" offset " defstr
))
431 (keymap (make-sparse-keymap))
432 (minibuffer-completion-table obarray
)
433 (minibuffer-completion-predicate 'fboundp
)
435 ;; In principle completing-read is used here, but SPC is unbound
436 ;; to make it less annoying to enter lists.
437 (set-keymap-parent keymap minibuffer-local-completion-map
)
438 (define-key keymap
" " 'self-insert-command
)
440 (setq input
(read-from-minibuffer prompt nil keymap t
441 'c-read-offset-history
442 (format "%s" oldoff
)))
443 (if (c-valid-offset input
)
445 ;; error, but don't signal one, keep trying
446 ;; to read an input value
448 (setq prompt errmsg
)))
452 (defun c-set-offset (symbol offset
&optional ignored
)
453 "Change the value of a syntactic element symbol in `c-offsets-alist'.
454 SYMBOL is the syntactic element symbol to change and OFFSET is the new
455 offset for that syntactic element. The optional argument is not used
456 and exists only for compatibility reasons."
459 (intern (completing-read
460 (concat "Syntactic symbol to change"
461 (if current-prefix-arg
" or add" "")
465 (cons (format "%s" (car langelem
)) nil
))
466 (get 'c-offsets-alist
'c-stylevar-fallback
))
467 nil
(not current-prefix-arg
)
468 ;; initial contents tries to be the last element
469 ;; on the syntactic analysis list for the current
471 (and c-buffer-is-cc-mode
473 ((syntax (c-guess-basic-syntax))
474 (len (length syntax
))
475 (ic (format "%s" (car (nth (1- len
) syntax
)))))
478 (offset (c-read-offset langelem
)))
479 (list langelem offset current-prefix-arg
)))
480 ;; sanity check offset
481 (if (c-valid-offset offset
)
482 (let ((entry (assq symbol c-offsets-alist
)))
484 (setcdr entry offset
)
485 (if (assq symbol
(get 'c-offsets-alist
'c-stylevar-fallback
))
486 (setq c-offsets-alist
(cons (cons symbol offset
)
488 (c-benign-error "%s is not a valid syntactic symbol" symbol
))))
489 (c-benign-error "Invalid indentation setting for symbol %s: %S"
491 (c-keep-region-active))
494 (defun c-setup-paragraph-variables ()
495 "Fix things up for paragraph recognition and filling inside comments and
496 strings by incorporating the values of `c-comment-prefix-regexp',
497 `sentence-end', `paragraph-start' and `paragraph-separate' in the relevant
501 (or c-buffer-is-cc-mode
502 (error "Buffer %s is not a CC Mode buffer (c-setup-paragraph-variables)"
504 ;; Set up the values for use in comments.
505 (setq c-current-comment-prefix
506 (if (listp c-comment-prefix-regexp
)
507 (cdr-safe (or (assoc major-mode c-comment-prefix-regexp
)
508 (assoc 'other c-comment-prefix-regexp
)))
509 c-comment-prefix-regexp
))
511 (let ((comment-line-prefix
512 (concat "[ \t]*\\(" c-current-comment-prefix
"\\)[ \t]*")))
514 (setq paragraph-start
(concat comment-line-prefix
518 paragraph-separate
(concat comment-line-prefix
522 paragraph-ignore-fill-prefix t
525 (concat comment-line-prefix
526 (if (default-value 'adaptive-fill-regexp
)
528 (default-value 'adaptive-fill-regexp
)
532 (when (boundp 'adaptive-fill-first-line-regexp
)
533 ;; XEmacs adaptive fill mode doesn't have this.
534 (make-local-variable 'adaptive-fill-first-line-regexp
)
535 (setq adaptive-fill-first-line-regexp
536 (concat "\\`" comment-line-prefix
537 ;; Maybe we should incorporate the old value here,
538 ;; but then we have to do all sorts of kludges to
539 ;; deal with the \` and \' it probably contains.
542 ;; Set up the values for use in strings. These are the default
543 ;; paragraph-start/separate values, enhanced to accept escaped EOLs as
544 ;; whitespace. Used in c-beginning/end-of-sentence-in-string in cc-cmds.
545 (setq c-string-par-start
546 ;;(concat "\\(" (default-value 'paragraph-start) "\\)\\|[ \t]*\\\\$"))
548 (setq c-string-par-separate
549 ;;(concat "\\(" (default-value 'paragraph-separate) "\\)\\|[ \t]*\\\\$"))
551 (setq c-sentence-end-with-esc-eol
552 (concat "\\(\\(" (c-default-value-sentence-end) "\\)"
553 ;; N.B.: "$" would be illegal when not enclosed like "\\($\\)".
554 "\\|" "[.?!][]\"')}]* ?\\\\\\($\\)[ \t\n]*"
558 ;; Helper for setting up Filladapt mode. It's not used by CC Mode itself.
560 (cc-bytecomp-defvar filladapt-token-table
)
561 (cc-bytecomp-defvar filladapt-token-match-table
)
562 (cc-bytecomp-defvar filladapt-token-conversion-table
)
564 (defun c-setup-filladapt ()
565 "Convenience function to configure Kyle E. Jones' Filladapt mode for
566 CC Mode by making sure the proper entries are present on
567 `filladapt-token-table', `filladapt-token-match-table', and
568 `filladapt-token-conversion-table'. This is intended to be used on
569 `c-mode-common-hook' or similar."
570 ;; This function is intended to be used explicitly by the end user
573 ;; The default configuration already handles C++ comments, but we
574 ;; need to add handling of C block comments. A new filladapt token
575 ;; `c-comment' is added for that.
577 (setq p filladapt-token-table
)
578 (while (and p
(not (eq (car-safe (cdr-safe (car-safe p
))) 'c-comment
)))
579 (setq p
(cdr-safe p
)))
581 (setcar (car p
) c-current-comment-prefix
)
582 (setq filladapt-token-table
583 (append (list (car filladapt-token-table
)
584 (list c-current-comment-prefix
'c-comment
))
585 (cdr filladapt-token-table
)))))
586 (unless (assq 'c-comment filladapt-token-match-table
)
587 (setq filladapt-token-match-table
588 (append '((c-comment c-comment
))
589 filladapt-token-match-table
)))
590 (unless (assq 'c-comment filladapt-token-conversion-table
)
591 (setq filladapt-token-conversion-table
592 (append '((c-comment . exact
))
593 filladapt-token-conversion-table
))))
596 (defun c-initialize-builtin-style ()
597 ;; Dynamically append the default value of most variables. This is
598 ;; crucial because future c-set-style calls will always reset the
599 ;; variables first to the `cc-mode' style before instituting the new
600 ;; style. Only do this once!
601 (unless (get 'c-initialize-builtin-style
'is-run
)
602 (put 'c-initialize-builtin-style
'is-run t
)
603 ;;(c-initialize-cc-mode)
604 (unless (assoc "user" c-style-alist
)
605 (let ((vars c-style-variables
) var val uservars
)
608 val
(symbol-value var
)
610 (cond ((eq var
'c-offsets-alist
)
612 (setq uservars
(cons (cons 'c-offsets-alist val
)
614 ((not (eq val
'set-from-style
))
615 (setq uservars
(cons (cons var val
)
617 (c-add-style "user" uservars
)))
618 (unless (assoc "cc-mode" c-style-alist
)
619 (c-add-style "cc-mode" '("user")))
620 (if c-style-variables-are-local-p
621 (c-make-styles-buffer-local))))
623 (defun c-make-styles-buffer-local (&optional this-buf-only-p
)
624 "Make all CC Mode style variables buffer local.
625 If `this-buf-only-p' is non-nil, the style variables will be made
626 buffer local only in the current buffer. Otherwise they'll be made
627 permanently buffer local in any buffer that changes their values.
629 The buffer localness of the style variables are normally controlled
630 with the variable `c-style-variables-are-local-p', so there's seldom
631 any reason to call this function directly."
634 (let ((func (if this-buf-only-p
636 'make-variable-buffer-local
))
637 (varsyms (cons 'c-indentation-style
(copy-alist c-style-variables
))))
638 (delq 'c-special-indent-hook varsyms
)
639 (mapcar func varsyms
)
640 ;; Hooks must be handled specially
642 (make-local-hook 'c-special-indent-hook
)
643 (with-no-warnings (make-variable-buffer-local 'c-special-indent-hook
))
644 (setq c-style-variables-are-local-p t
))
649 (cc-provide 'cc-styles
)
651 ;;; arch-tag: c764f61a-96ba-484a-a68f-101c0e9d5d2c
652 ;;; cc-styles.el ends here