1 ;;; cc-styles.el --- support for styles in CC Mode
3 ;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
5 ;; Authors: 2000- Martin Stjernholm
6 ;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
7 ;; 1992-1997 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 GNU Emacs; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, 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 (require 'cc-bytecomp
)))
45 (cc-require 'cc-langs
)
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
55 ;; Warning: don't eval-defun this constant or you'll break style inheritance.
56 (defconst c-style-alist
59 (c-comment-only-line-offset .
(0 .
0))
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
)
72 (c-special-indent-hook . c-gnu-impose-minimum
)
73 (c-block-comment-prefix .
"")
77 (c-comment-only-line-offset .
0)
78 (c-offsets-alist .
((statement-block-intro .
+)
79 (knr-argdecl-intro .
0)
80 (substatement-open .
0)
81 (substatement-label .
0)
88 (c-comment-only-line-offset .
0)
89 (c-offsets-alist .
((statement-block-intro .
+)
90 (knr-argdecl-intro .
+)
91 (substatement-open .
0)
92 (substatement-label .
0)
101 (c-comment-only-line-offset .
0)
102 (c-offsets-alist .
((statement-block-intro .
+)
103 (substatement-open .
0)
104 (substatement-label .
0)
111 (c-comment-only-line-offset .
0)
112 (c-offsets-alist .
((knr-argdecl-intro .
+)
115 (substatement-open .
+)
116 (substatement-label .
+)
118 (statement-block-intro . c-lineup-whitesmith-in-block
)
119 (block-close . c-lineup-whitesmith-in-block
)
122 (defun-block-intro . c-lineup-whitesmith-in-block
)
123 (defun-close . c-lineup-whitesmith-in-block
)
124 (brace-list-open .
+)
125 (brace-list-intro . c-lineup-whitesmith-in-block
)
126 (brace-entry-open . c-indent-multi-line-block
)
127 (brace-list-close . c-lineup-whitesmith-in-block
)
129 (inclass . c-lineup-whitesmith-in-block
)
132 (extern-lang-open .
+)
133 (inextern-lang . c-lineup-whitesmith-in-block
)
134 (extern-lang-close .
+)
136 (innamespace . c-lineup-whitesmith-in-block
)
137 (namespace-close .
+)
142 (c-comment-only-line-offset .
0)
143 (c-hanging-braces-alist .
((substatement-open before after
)))
144 (c-offsets-alist .
((topmost-intro .
0)
146 (substatement-open .
0)
155 (c-comment-only-line-offset .
0)
156 (c-hanging-braces-alist .
((brace-list-open)
158 (substatement-open after
)
159 (block-close . c-snug-do-while
)))
160 (c-cleanup-list .
(brace-else-brace))
161 (c-offsets-alist .
((statement-block-intro .
+)
162 (knr-argdecl-intro .
0)
163 (substatement-open .
0)
164 (substatement-label .
0)
170 (indent-tabs-mode . t
)
173 (c-offsets-alist .
((substatement-open .
0)
176 (knr-argdecl-intro .
+)
178 (c-hanging-braces-alist .
((brace-list-open)
182 (substatement-open after
)
183 (block-close . c-snug-do-while
)
185 (c-block-comment-prefix .
"")
189 (c-comment-only-line-offset .
(0 .
0))
190 ;; the following preserves Javadoc starter lines
191 (c-offsets-alist .
((inline-open .
0)
192 (topmost-intro-cont .
+)
193 (statement-block-intro .
+)
194 (knr-argdecl-intro .
5)
195 (substatement-open .
+)
196 (substatement-label .
+)
198 (statement-case-open .
+)
200 (arglist-intro . c-lineup-arglist-intro-after-paren
)
201 (arglist-close . c-lineup-arglist
)
203 (inher-cont . c-lineup-java-inher
)
204 (func-decl-cont . c-lineup-java-throws
)
208 "Styles of indentation.
209 Elements of this alist are of the form:
211 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
213 where STYLE-STRING is a short descriptive string used to select a
214 style, VARIABLE is any Emacs variable, and VALUE is the intended value
215 for that variable when using the selected style.
217 Optional BASE-STYLE if present, is a string and must follow
218 STYLE-STRING. BASE-STYLE names a style that this style inherits from.
219 By default, all styles inherit from the \"user\" style, which is
220 computed at run time. Style loops generate errors.
222 Two variables are treated specially. When VARIABLE is
223 `c-offsets-alist', the VALUE is a list containing elements of the
226 (SYNTACTIC-SYMBOL . OFFSET)
228 as described in `c-offsets-alist'. These are passed directly to
229 `c-set-offset' so there is no need to set every syntactic symbol in
230 your style, only those that are different from the default.
232 When VARIABLE is `c-special-indent-hook', its VALUE is added to
233 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each
234 element of the list is added with `add-hook'.
236 Do not change this variable directly. Use the function `c-add-style'
237 to add new styles or modify existing styles (it is not a good idea to
238 modify existing styles -- you should create a new style that inherits
239 the existing style.")
242 ;; Functions that manipulate styles
243 (defun c-set-style-1 (conscell dont-override
)
244 ;; Set the style for one variable
245 (let ((attr (car conscell
))
246 (val (cdr conscell
)))
248 ;; first special variable
249 ((eq attr
'c-offsets-alist
)
250 (let ((offsets (cond ((eq dont-override t
)
253 (default-value 'c-offsets-alist
)))))
254 (mapcar (lambda (langentry)
255 (let ((langelem (car langentry
))
256 (offset (cdr langentry
)))
257 (unless (assq langelem offsets
)
258 (c-set-offset langelem offset
))))
260 ;; second special variable
261 ((eq attr
'c-special-indent-hook
)
262 ;; Maybe we should ignore dont-override here and always add new
264 (unless (cond ((eq dont-override t
)
265 c-special-indent-hook
)
267 (default-value 'c-special-indent-hook
)))
269 (mapcar (lambda (func)
270 (add-hook 'c-special-indent-hook func t t
))
272 (add-hook 'c-special-indent-hook val t t
))))
273 ;; all other variables
274 (t (when (or (not dont-override
)
275 (not (memq attr c-style-variables
))
276 (eq (if (eq dont-override t
)
278 (default-value attr
))
281 ;; Must update a number of other variables if
282 ;; c-comment-prefix-regexp is set.
283 (if (eq attr
'c-comment-prefix-regexp
)
284 (c-setup-paragraph-variables)))))))
286 (defun c-get-style-variables (style basestyles
)
287 ;; Return all variables in a style by resolving inheritances.
289 (copy-alist c-fallback-style
)
290 (let ((vars (cdr (or (assoc (downcase style
) c-style-alist
)
291 (assoc (upcase style
) c-style-alist
)
292 (assoc style c-style-alist
)
294 (c-benign-error "Undefined style: %s" style
)
296 (let ((base (and (stringp (car-safe vars
))
298 (downcase (car vars
))
299 (setq vars
(cdr vars
))))))
300 (if (memq base basestyles
)
301 (c-benign-error "Style loop detected: %s in %s" base basestyles
)
302 (nconc (c-get-style-variables base
(cons base basestyles
))
303 (copy-alist vars
)))))))
305 (defvar c-set-style-history nil
)
308 (defun c-set-style (stylename &optional dont-override
)
309 "Set CC Mode variables to use one of several different indentation styles.
310 STYLENAME is a string representing the desired style from the list of
311 styles described in the variable `c-style-alist'. See that variable
312 for details of setting up styles.
314 The variable `c-indentation-style' always contains the buffer's current
317 If the optional argument DONT-OVERRIDE is t, no style variables that
318 already have values will be overridden. I.e. in the case of
319 `c-offsets-alist', syntactic symbols will only be added, and in the
320 case of all other style variables, only those set to `set-from-style'
323 If DONT-OVERRIDE is neither nil nor t, only those style variables that
324 have default (i.e. non-buffer local) values will keep their settings
325 while the rest will be overridden. This is useful to avoid overriding
326 global settings done in ~/.emacs when setting a style from a mode hook
327 \(providing the style variables are buffer local, which is the
330 Obviously, setting DONT-OVERRIDE to t is useful mainly when the
331 initial style is chosen for a CC Mode buffer by a major mode. Since
332 that is done internally by CC Mode, it typically won't have any effect
333 when used elsewhere."
335 (list (let ((completion-ignore-case t
)
336 (prompt (format "Which %s indentation style? "
339 ;; The default argument is preferred over
340 ;; initial-contents, but it only exists in Emacs >= 20
342 (completing-read prompt c-style-alist nil t nil
345 (wrong-number-of-arguments
346 ;; If the call above failed, we fall back to the old way
347 ;; of specifying the default value.
348 (completing-read prompt c-style-alist nil t
349 (cons c-indentation-style
0)
350 'c-set-style-history
))))))
351 (c-initialize-builtin-style)
352 (let ((vars (c-get-style-variables stylename nil
)))
353 (unless dont-override
354 ;; Since we always add to c-special-indent-hook we must reset it
355 ;; first, or else the hooks from the preceding style will
356 ;; remain. This is not necessary for c-offsets-alist, since
357 ;; c-get-style-variables contains every valid offset type in the
359 (setq c-special-indent-hook
360 (default-value 'c-special-indent-hook
)))
361 (mapcar (lambda (elem)
362 (c-set-style-1 elem dont-override
))
363 ;; Need to go through the variables backwards when we
364 ;; don't override any settings.
365 (if (eq dont-override t
) (nreverse vars
) vars
)))
366 (setq c-indentation-style stylename
)
367 (c-keep-region-active))
370 (defun c-add-style (style descrip
&optional set-p
)
371 "Adds a style to `c-style-alist', or updates an existing one.
372 STYLE is a string identifying the style to add or update. DESCRIP is
373 an association list describing the style and must be of the form:
375 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
377 See the variable `c-style-alist' for the semantics of BASESTYLE,
378 VARIABLE and VALUE. This function also sets the current style to
379 STYLE using `c-set-style' if the optional SET-P flag is non-nil."
381 (let ((stylename (completing-read "Style to add: " c-style-alist
382 nil nil nil
'c-set-style-history
))
383 (description (eval-minibuffer "Style description: ")))
384 (list stylename description
385 (y-or-n-p "Set the style too? "))))
386 (setq style
(downcase style
))
387 (let ((s (assoc style c-style-alist
)))
389 (setcdr s
(copy-alist descrip
)) ; replace
390 (setq c-style-alist
(cons (cons style descrip
) c-style-alist
))))
391 (and set-p
(c-set-style style
)))
394 (defvar c-read-offset-history nil
)
396 (defun c-read-offset (langelem)
397 ;; read new offset value for LANGELEM from minibuffer. return a
399 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist
)
400 (assq langelem
(get 'c-offsets-alist
401 'c-stylevar-fallback
)))))
402 (symname (symbol-name langelem
))
403 (defstr (format "(default %s): " oldoff
))
404 (errmsg (concat "Offset must be int, func, var, vector, list, "
405 "or [+,-,++,--,*,/] "
407 (prompt (concat symname
" offset " defstr
))
408 (keymap (make-sparse-keymap))
409 (minibuffer-completion-table obarray
)
410 (minibuffer-completion-predicate 'fboundp
)
412 ;; In principle completing-read is used here, but SPC is unbound
413 ;; to make it less annoying to enter lists.
414 (set-keymap-parent keymap minibuffer-local-completion-map
)
415 (define-key keymap
" " 'self-insert-command
)
417 (setq input
(read-from-minibuffer prompt nil keymap t
418 'c-read-offset-history
419 (format "%s" oldoff
)))
420 (if (c-valid-offset input
)
422 ;; error, but don't signal one, keep trying
423 ;; to read an input value
425 (setq prompt errmsg
)))
429 (defun c-set-offset (symbol offset
&optional ignored
)
430 "Change the value of a syntactic element symbol in `c-offsets-alist'.
431 SYMBOL is the syntactic element symbol to change and OFFSET is the new
432 offset for that syntactic element. The optional argument is not used
433 and exists only for compatibility reasons."
436 (intern (completing-read
437 (concat "Syntactic symbol to change"
438 (if current-prefix-arg
" or add" "")
442 (cons (format "%s" (car langelem
)) nil
))
443 (get 'c-offsets-alist
'c-stylevar-fallback
))
444 nil
(not current-prefix-arg
)
445 ;; initial contents tries to be the last element
446 ;; on the syntactic analysis list for the current
448 (let* ((syntax (c-guess-basic-syntax))
449 (len (length syntax
))
450 (ic (format "%s" (car (nth (1- len
) syntax
)))))
453 (offset (c-read-offset langelem
)))
454 (list langelem offset current-prefix-arg
)))
455 ;; sanity check offset
456 (if (c-valid-offset offset
)
457 (let ((entry (assq symbol c-offsets-alist
)))
459 (setcdr entry offset
)
460 (if (assq symbol
(get 'c-offsets-alist
'c-stylevar-fallback
))
461 (setq c-offsets-alist
(cons (cons symbol offset
)
463 (c-benign-error "%s is not a valid syntactic symbol" symbol
))))
464 (c-benign-error "Invalid indentation setting for symbol %s: %s"
466 (c-keep-region-active))
469 (defun c-setup-paragraph-variables ()
470 "Fix things up for paragraph recognition and filling inside comments by
471 incorporating the value of `c-comment-prefix-regexp' in the relevant
473 (setq c-current-comment-prefix
474 (if (listp c-comment-prefix-regexp
)
475 (cdr-safe (or (assoc major-mode c-comment-prefix-regexp
)
476 (assoc 'other c-comment-prefix-regexp
)))
477 c-comment-prefix-regexp
))
478 (let ((comment-line-prefix
479 (concat "[ \t]*\\(" c-current-comment-prefix
"\\)[ \t]*")))
480 (setq paragraph-start
(concat comment-line-prefix
481 (c-lang-var paragraph-start
)
484 paragraph-separate
(concat comment-line-prefix
485 (c-lang-var paragraph-separate
)
488 paragraph-ignore-fill-prefix t
491 (concat comment-line-prefix
492 (if (default-value 'adaptive-fill-regexp
)
494 (default-value 'adaptive-fill-regexp
)
497 (when (boundp 'adaptive-fill-first-line-regexp
)
498 ;; XEmacs (20.x) adaptive fill mode doesn't have this.
499 (make-local-variable 'adaptive-fill-first-line-regexp
)
500 (setq adaptive-fill-first-line-regexp
501 (concat "\\`" comment-line-prefix
502 ;; Maybe we should incorporate the old value here,
503 ;; but then we have to do all sorts of kludges to
504 ;; deal with the \` and \' it probably contains.
508 ;; Helper for setting up Filladapt mode. It's not used by CC Mode itself.
510 (cc-bytecomp-defvar filladapt-token-table
)
511 (cc-bytecomp-defvar filladapt-token-match-table
)
512 (cc-bytecomp-defvar filladapt-token-conversion-table
)
514 (defun c-setup-filladapt ()
515 "Convenience function to configure Kyle E. Jones' Filladapt mode for
516 CC Mode by making sure the proper entries are present on
517 `filladapt-token-table', `filladapt-token-match-table', and
518 `filladapt-token-conversion-table'. This is intended to be used on
519 `c-mode-common-hook' or similar."
520 ;; This function is intended to be used explicitly by the end user
523 ;; The default configuration already handles C++ comments, but we
524 ;; need to add handling of C block comments. A new filladapt token
525 ;; `c-comment' is added for that.
527 (setq p filladapt-token-table
)
528 (while (and p
(not (eq (car-safe (cdr-safe (car-safe p
))) 'c-comment
)))
529 (setq p
(cdr-safe p
)))
531 (setcar (car p
) c-current-comment-prefix
)
532 (setq filladapt-token-table
533 (append (list (car filladapt-token-table
)
534 (list c-current-comment-prefix
'c-comment
))
535 (cdr filladapt-token-table
)))))
536 (unless (assq 'c-comment filladapt-token-match-table
)
537 (setq filladapt-token-match-table
538 (append '((c-comment c-comment
))
539 filladapt-token-match-table
)))
540 (unless (assq 'c-comment filladapt-token-conversion-table
)
541 (setq filladapt-token-conversion-table
542 (append '((c-comment . exact
))
543 filladapt-token-conversion-table
))))
546 (defun c-initialize-builtin-style ()
547 ;; Dynamically append the default value of most variables. This is
548 ;; crucial because future c-set-style calls will always reset the
549 ;; variables first to the `cc-mode' style before instituting the new
550 ;; style. Only do this once!
551 (unless (get 'c-initialize-builtin-style
'is-run
)
552 (put 'c-initialize-builtin-style
'is-run t
)
553 ;;(c-initialize-cc-mode)
554 (unless (assoc "user" c-style-alist
)
555 (let ((vars c-style-variables
) var val uservars
)
558 val
(symbol-value var
)
560 (cond ((eq var
'c-offsets-alist
)
562 (setq uservars
(cons (cons 'c-offsets-alist val
)
564 ((not (eq val
'set-from-style
))
565 (setq uservars
(cons (cons var val
)
567 (c-add-style "user" uservars
)))
568 (unless (assoc "cc-mode" c-style-alist
)
569 (c-add-style "cc-mode" '("user")))
570 (if c-style-variables-are-local-p
571 (c-make-styles-buffer-local))))
573 (defun c-make-styles-buffer-local (&optional this-buf-only-p
)
574 "Make all CC Mode style variables buffer local.
575 If you edit primarily one style of C (or C++, Objective-C, Java, etc)
576 code, you probably want style variables to be global. This is the
579 If you edit many different styles of C (or C++, Objective-C, Java,
580 etc) at the same time, you probably want the CC Mode style variables
581 to be buffer local. If you do, it's advicable to set any CC Mode
582 style variables in a hook function (e.g. off of `c-mode-common-hook'),
583 instead of at the top level of your ~/.emacs file.
585 This function makes all the CC Mode style variables buffer local.
586 Call it after CC Mode is loaded into your Emacs environment.
587 Conversely, set the variable `c-style-variables-are-local-p' to t in
588 your .emacs file, before CC Mode is loaded, and this function will be
589 automatically called when CC Mode is loaded.
591 Optional argument, when non-nil, means use `make-local-variable'
592 instead of `make-variable-buffer-local'."
594 (let ((func (if this-buf-only-p
596 'make-variable-buffer-local
))
597 (varsyms (cons 'c-indentation-style
(copy-alist c-style-variables
))))
598 (delq 'c-special-indent-hook varsyms
)
599 (mapcar func varsyms
)
600 ;; Hooks must be handled specially
602 (make-local-hook 'c-special-indent-hook
)
603 (make-variable-buffer-local 'c-special-indent-hook
)
604 (setq c-style-variables-are-local-p t
))
609 (cc-provide 'cc-styles
)
611 ;;; cc-styles.el ends here