1 ;;; cc-styles.el --- support for styles in CC Mode
3 ;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc.
5 ;; Authors: 1998-1999 Barry A. Warsaw and Martin Stjernholm
6 ;; 1992-1997 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Version: See cc-mode.el
12 ;; Keywords: c languages oop
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
33 (if (and (boundp 'byte-compile-current-file)
34 (stringp byte-compile-current-file))
35 (cons (file-name-directory byte-compile-current-file)
38 (load "cc-defs" nil t)))
43 ;; Warning: don't eval-defun this constant or you'll break style inheritance.
44 (defconst c-style-alist
47 (c-comment-only-line-offset . (0 . 0))
48 (c-offsets-alist . ((statement-block-intro . +)
49 (knr-argdecl-intro . 5)
50 (substatement-open . +)
52 (statement-case-open . +)
54 (arglist-intro . c-lineup-arglist-intro-after-paren)
55 (arglist-close . c-lineup-arglist)
58 (c-special-indent-hook . c-gnu-impose-minimum)
59 (c-block-comment-prefix . "")
63 (c-comment-only-line-offset . 0)
64 (c-offsets-alist . ((statement-block-intro . +)
65 (knr-argdecl-intro . 0)
66 (substatement-open . 0)
73 (c-comment-only-line-offset . 0)
74 (c-offsets-alist . ((statement-block-intro . +)
75 (knr-argdecl-intro . +)
76 (substatement-open . 0)
85 (c-comment-only-line-offset . 0)
86 (c-offsets-alist . ((statement-block-intro . +)
87 (substatement-open . 0)
94 (c-comment-only-line-offset . 0)
95 (c-offsets-alist . ((knr-argdecl-intro . +)
98 (substatement-open . +)
100 (statement-block-intro . c-lineup-whitesmith-in-block)
101 (block-close . c-lineup-whitesmith-in-block)
104 (defun-block-intro . c-lineup-whitesmith-in-block)
105 (defun-close . c-lineup-whitesmith-in-block)
106 (brace-list-open . +)
107 (brace-list-intro . c-lineup-whitesmith-in-block)
108 (brace-entry-open . c-indent-multi-line-block)
109 (brace-list-close . c-lineup-whitesmith-in-block)
111 (inclass . c-lineup-whitesmith-in-block)
114 (extern-lang-open . +)
115 (inextern-lang . c-lineup-whitesmith-in-block)
116 (extern-lang-close . +)
118 (innamespace . c-lineup-whitesmith-in-block)
119 (namespace-close . +)
124 (c-comment-only-line-offset . 0)
125 (c-hanging-braces-alist . ((substatement-open before after)))
126 (c-offsets-alist . ((topmost-intro . 0)
127 (topmost-intro-cont . 0)
129 (substatement-open . 0)
138 (c-comment-only-line-offset . 0)
139 (c-hanging-braces-alist . ((brace-list-open)
141 (substatement-open after)
142 (block-close . c-snug-do-while)))
143 (c-cleanup-list . (brace-else-brace))
144 (c-offsets-alist . ((statement-block-intro . +)
145 (knr-argdecl-intro . 0)
146 (substatement-open . 0)
152 (indent-tabs-mode . t)
155 (c-offsets-alist . ((substatement-open . 0)
158 (knr-argdecl-intro . +)
160 (c-hanging-braces-alist . ((brace-list-open)
164 (substatement-open after)
165 (block-close . c-snug-do-while)
167 (c-block-comment-prefix . "")
171 (c-comment-only-line-offset . (0 . 0))
172 ;; the following preserves Javadoc starter lines
173 (c-offsets-alist . ((inline-open . 0)
174 (topmost-intro-cont . +)
175 (statement-block-intro . +)
176 (knr-argdecl-intro . 5)
177 (substatement-open . +)
179 (statement-case-open . +)
181 (arglist-intro . c-lineup-arglist-intro-after-paren)
182 (arglist-close . c-lineup-arglist)
184 (inher-cont . c-lineup-java-inher)
185 (func-decl-cont . c-lineup-java-throws)
189 "Styles of indentation.
190 Elements of this alist are of the form:
192 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
194 where STYLE-STRING is a short descriptive string used to select a
195 style, VARIABLE is any Emacs variable, and VALUE is the intended value
196 for that variable when using the selected style.
198 Optional BASE-STYLE if present, is a string and must follow
199 STYLE-STRING. BASE-STYLE names a style that this style inherits from.
200 By default, all styles inherit from the \"user\" style, which is
201 computed at run time. Style loops generate errors.
203 Two variables are treated specially. When VARIABLE is
204 `c-offsets-alist', the VALUE is a list containing elements of the
207 (SYNTACTIC-SYMBOL . OFFSET)
209 as described in `c-offsets-alist'. These are passed directly to
210 `c-set-offset' so there is no need to set every syntactic symbol in
211 your style, only those that are different from the default.
213 When VARIABLE is `c-special-indent-hook', its VALUE is added to
214 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each
215 element of the list is added with `add-hook'.
217 Do not change this variable directly. Use the function `c-add-style'
218 to add new styles or modify existing styles (it is not a good idea to
219 modify existing styles -- you should create a new style that inherits
220 the existing style.")
224 ;; Functions that manipulate styles
225 (defun c-set-style-1 (conscell dont-override)
226 ;; Set the style for one variable
227 (let ((attr (car conscell))
228 (val (cdr conscell)))
230 ;; first special variable
231 ((eq attr 'c-offsets-alist)
235 (let ((langelem (car langentry))
236 (offset (cdr langentry)))
237 (unless (and dont-override
238 (assq langelem c-offsets-alist))
239 (c-set-offset langelem offset))
241 (if dont-override (reverse val) val)))
242 ;; second special variable
243 ((eq attr 'c-special-indent-hook)
244 (let ((add-func (if dont-override
246 (unless (memq func c-special-indent-hook)
247 (add-hook 'c-special-indent-hook func t)))
249 (add-hook 'c-special-indent-hook func)))))
251 (mapcar add-func (if dont-override (reverse val) val))
252 (funcall add-func val))))
253 ;; all other variables
254 (t (if (or (not dont-override)
255 (not (memq attr c-style-variables))
256 (eq (symbol-value attr) 'set-from-style))
260 (defun c-get-style-variables (style basestyles)
261 ;; Return all variables in a style by resolving inheritances.
262 (let ((vars (cdr (or (assoc (downcase style) c-style-alist)
263 (assoc (upcase style) c-style-alist)
264 (assoc style c-style-alist)
265 (error "Undefined style: %s" style)))))
266 (if (string-equal style "user")
268 (let ((base (if (stringp (car vars))
270 (downcase (car vars))
271 (setq vars (cdr vars)))
273 (if (memq base basestyles)
274 (error "Style loop detected: %s in %s" base basestyles))
275 (nconc (c-get-style-variables base (cons base basestyles))
276 (copy-alist vars))))))
278 (defvar c-set-style-history nil)
281 (defun c-set-style (stylename &optional dont-override)
282 "Set CC Mode variables to use one of several different indentation styles.
283 STYLENAME is a string representing the desired style from the list of
284 styles described in the variable `c-style-alist'. See that variable
285 for details of setting up styles.
287 The variable `c-indentation-style' always contains the buffer's current
290 If the optional argument DONT-OVERRIDE is non-nil, no style variables
291 that already have values will be overridden. I.e. in the case of
292 `c-offsets-alist', syntactic symbols will only be added, and in the
293 case of all other style variables, only those set to `set-from-style'
296 Obviously, specifying DONT-OVERRIDE is useful mainly when the initial
297 style is chosen for a CC Mode buffer by a major mode. Since this is
298 done internally by CC Mode, there's hardly ever a reason to use it."
299 (interactive (list (let ((completion-ignore-case t)
300 (prompt (format "Which %s indentation style? "
302 (completing-read prompt c-style-alist nil t
303 (cons c-indentation-style 0)
304 'c-set-style-history))))
305 (c-initialize-builtin-style)
306 (let ((vars (c-get-style-variables stylename nil)))
307 (mapcar (lambda (elem)
308 (c-set-style-1 elem dont-override))
309 ;; Need to go through the variables backwards when we
311 (if dont-override (nreverse vars) vars)))
312 (setq c-indentation-style stylename)
313 (c-keep-region-active))
316 (defun c-add-style (style descrip &optional set-p)
317 "Adds a style to `c-style-alist', or updates an existing one.
318 STYLE is a string identifying the style to add or update. DESCRIP is
319 an association list describing the style and must be of the form:
321 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
323 See the variable `c-style-alist' for the semantics of BASESTYLE,
324 VARIABLE and VALUE. This function also sets the current style to
325 STYLE using `c-set-style' if the optional SET-P flag is non-nil."
327 (let ((stylename (completing-read "Style to add: " c-style-alist
328 nil nil nil 'c-set-style-history))
329 (description (eval-minibuffer "Style description: ")))
330 (list stylename description
331 (y-or-n-p "Set the style too? "))))
332 (setq style (downcase style))
333 (let ((s (assoc style c-style-alist)))
335 (setcdr s (copy-alist descrip)) ; replace
336 (setq c-style-alist (cons (cons style descrip) c-style-alist))))
337 (and set-p (c-set-style style)))
341 (defun c-evaluate-offset (offset langelem symbol)
342 ;; offset can be a number, a function, a variable, a list, or one of
343 ;; the symbols + or -
345 ((eq offset '+) (setq offset c-basic-offset))
346 ((eq offset '-) (setq offset (- c-basic-offset)))
347 ((eq offset '++) (setq offset (* 2 c-basic-offset)))
348 ((eq offset '--) (setq offset (* 2 (- c-basic-offset))))
349 ((eq offset '*) (setq offset (/ c-basic-offset 2)))
350 ((eq offset '/) (setq offset (/ (- c-basic-offset) 2)))
351 ((functionp offset) (setq offset (funcall offset langelem)))
355 (while (and (not done) offset)
356 (setq done (c-evaluate-offset (car offset) langelem symbol)
357 offset (cdr offset)))
359 (if c-strict-syntax-p
360 (error "No offset found for syntactic symbol %s" symbol)
363 ((not (numberp offset)) (setq offset (symbol-value offset)))
367 (defun c-get-offset (langelem)
368 ;; Get offset from LANGELEM which is a cons cell of the form:
369 ;; (SYMBOL . RELPOS). The symbol is matched against
370 ;; c-offsets-alist and the offset found there is either returned,
371 ;; or added to the indentation at RELPOS. If RELPOS is nil, then
372 ;; the offset is simply returned.
373 (let* ((symbol (car langelem))
374 (relpos (cdr langelem))
375 (match (assq symbol c-offsets-alist))
376 (offset (cdr-safe match)))
378 (if c-strict-syntax-p
379 (error "No offset found for syntactic symbol %s" symbol)
382 (setq offset (c-evaluate-offset offset langelem symbol)))
384 (< relpos (c-point 'bol)))
389 (or (and (numberp offset) offset)
390 (and (symbolp offset) (symbol-value offset))
396 (defvar c-read-offset-history nil)
398 (defun c-read-offset (langelem)
399 ;; read new offset value for LANGELEM from minibuffer. return a
401 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist)
402 (assq langelem (get 'c-offsets-alist
403 'c-stylevar-fallback)))))
404 (symname (symbol-name langelem))
405 (defstr (format "(default %s): " oldoff))
406 (errmsg (concat "Offset must be int, func, var, list, "
407 "or [+,-,++,--,*,/] "
409 (prompt (concat symname " offset " defstr))
410 offset input interned raw)
412 (setq input (completing-read prompt obarray 'fboundp nil nil
413 'c-read-offset-history)
414 offset (cond ((string-equal "" input) oldoff) ; default
415 ((string-equal "+" input) '+)
416 ((string-equal "-" input) '-)
417 ((string-equal "++" input) '++)
418 ((string-equal "--" input) '--)
419 ((string-equal "*" input) '*)
420 ((string-equal "/" input) '/)
421 ((string-match "^-?[0-9]+$" input)
422 (string-to-int input))
423 ;; a symbol with a function binding
424 ((fboundp (setq interned (intern input)))
427 ((c-safe (functionp (setq raw (read input))))
429 ;; a symbol with variable binding
430 ((boundp interned) interned)
431 ;; error, but don't signal one, keep trying
432 ;; to read an input value
439 (defun c-set-offset (symbol offset &optional ignored)
440 "Change the value of a syntactic element symbol in `c-offsets-alist'.
441 SYMBOL is the syntactic element symbol to change and OFFSET is the new
442 offset for that syntactic element. The optional argument is not used
443 and exists only for compatibility reasons."
446 (intern (completing-read
447 (concat "Syntactic symbol to change"
448 (if current-prefix-arg " or add" "")
452 (cons (format "%s" (car langelem)) nil))
453 (get 'c-offsets-alist 'c-stylevar-fallback))
454 nil (not current-prefix-arg)
455 ;; initial contents tries to be the last element
456 ;; on the syntactic analysis list for the current
458 (let* ((syntax (c-guess-basic-syntax))
459 (len (length syntax))
460 (ic (format "%s" (car (nth (1- len) syntax)))))
463 (offset (c-read-offset langelem)))
464 (list langelem offset current-prefix-arg)))
465 ;; sanity check offset
466 (unless (c-valid-offset offset)
467 (error "Offset must be int, func, var, list, or in [+,-,++,--,*,/]: %s"
469 (let ((entry (assq symbol c-offsets-alist)))
471 (setcdr entry offset)
472 (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback))
473 (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist))
474 (error "%s is not a valid syntactic symbol" symbol))))
475 (c-keep-region-active))
479 (defun c-initialize-builtin-style ()
480 ;; Dynamically append the default value of most variables. This is
481 ;; crucial because future c-set-style calls will always reset the
482 ;; variables first to the `cc-mode' style before instituting the new
483 ;; style. Only do this once!
484 (unless (get 'c-initialize-builtin-style 'is-run)
485 (put 'c-initialize-builtin-style 'is-run t)
486 (c-initialize-cc-mode)
487 (or (assoc "cc-mode" c-style-alist)
488 (assoc "user" c-style-alist)
494 (let ((val (symbol-value var)))
496 (cond ((eq var 'c-offsets-alist)
499 (setq langentry (or (assq (car langentry) val)
501 (cons (car langentry)
503 (get var 'c-stylevar-fallback)))
504 ((eq var 'c-special-indent-hook)
507 (if (eq val 'set-from-style)
508 (get var 'c-stylevar-fallback)
511 (c-add-style "cc-mode" '("user"))))
512 (if c-style-variables-are-local-p
513 (c-make-styles-buffer-local))))
515 (defun c-make-styles-buffer-local (&optional this-buf-only-p)
516 "Make all CC Mode style variables buffer local.
517 If you edit primarily one style of C (or C++, Objective-C, Java, etc)
518 code, you probably want style variables to be global. This is the
521 If you edit many different styles of C (or C++, Objective-C, Java,
522 etc) at the same time, you probably want the CC Mode style variables
523 to be buffer local. If you do, it's advicable to set any CC Mode
524 style variables in a hook function (e.g. off of `c-mode-common-hook'),
525 instead of at the top level of your ~/.emacs file.
527 This function makes all the CC Mode style variables buffer local.
528 Call it after CC Mode is loaded into your Emacs environment.
529 Conversely, set the variable `c-style-variables-are-local-p' to t in
530 your .emacs file, before CC Mode is loaded, and this function will be
531 automatically called when CC Mode is loaded.
533 Optional argument, when non-nil, means use `make-local-variable'
534 instead of `make-variable-buffer-local'."
536 (let ((func (if this-buf-only-p
538 'make-variable-buffer-local))
539 (varsyms (cons 'c-indentation-style (copy-alist c-style-variables))))
540 (delq 'c-special-indent-hook varsyms)
541 (mapcar func varsyms)
542 ;; Hooks must be handled specially
544 (make-local-hook 'c-special-indent-hook)
545 (make-variable-buffer-local 'c-special-indent-hook)
546 (setq c-style-variables-are-local-p t))
552 ;;; cc-styles.el ends here