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 this program; 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.
34 (if (and (boundp 'byte-compile-dest-file)
35 (stringp byte-compile-dest-file))
36 (cons (file-name-directory byte-compile-dest-file) load-path)
38 (require 'cc-bytecomp)))
44 ;; Warning: don't eval-defun this constant or you'll break style inheritance.
45 (defconst c-style-alist
48 (c-comment-only-line-offset . (0 . 0))
49 (c-offsets-alist . ((statement-block-intro . +)
50 (knr-argdecl-intro . 5)
51 (substatement-open . +)
53 (statement-case-open . +)
55 (arglist-intro . c-lineup-arglist-intro-after-paren)
56 (arglist-close . c-lineup-arglist)
60 (c-special-indent-hook . c-gnu-impose-minimum)
61 (c-block-comment-prefix . "")
65 (c-comment-only-line-offset . 0)
66 (c-offsets-alist . ((statement-block-intro . +)
67 (knr-argdecl-intro . 0)
68 (substatement-open . 0)
75 (c-comment-only-line-offset . 0)
76 (c-offsets-alist . ((statement-block-intro . +)
77 (knr-argdecl-intro . +)
78 (substatement-open . 0)
87 (c-comment-only-line-offset . 0)
88 (c-offsets-alist . ((statement-block-intro . +)
89 (substatement-open . 0)
96 (c-comment-only-line-offset . 0)
97 (c-offsets-alist . ((knr-argdecl-intro . +)
100 (substatement-open . +)
102 (statement-block-intro . c-lineup-whitesmith-in-block)
103 (block-close . c-lineup-whitesmith-in-block)
106 (defun-block-intro . c-lineup-whitesmith-in-block)
107 (defun-close . c-lineup-whitesmith-in-block)
108 (brace-list-open . +)
109 (brace-list-intro . c-lineup-whitesmith-in-block)
110 (brace-entry-open . c-indent-multi-line-block)
111 (brace-list-close . c-lineup-whitesmith-in-block)
113 (inclass . c-lineup-whitesmith-in-block)
116 (extern-lang-open . +)
117 (inextern-lang . c-lineup-whitesmith-in-block)
118 (extern-lang-close . +)
120 (innamespace . c-lineup-whitesmith-in-block)
121 (namespace-close . +)
126 (c-comment-only-line-offset . 0)
127 (c-hanging-braces-alist . ((substatement-open before after)))
128 (c-offsets-alist . ((topmost-intro . 0)
129 (topmost-intro-cont . 0)
131 (substatement-open . 0)
140 (c-comment-only-line-offset . 0)
141 (c-hanging-braces-alist . ((brace-list-open)
143 (substatement-open after)
144 (block-close . c-snug-do-while)))
145 (c-cleanup-list . (brace-else-brace))
146 (c-offsets-alist . ((statement-block-intro . +)
147 (knr-argdecl-intro . 0)
148 (substatement-open . 0)
154 (indent-tabs-mode . t)
157 (c-offsets-alist . ((substatement-open . 0)
160 (knr-argdecl-intro . +)
162 (c-hanging-braces-alist . ((brace-list-open)
166 (substatement-open after)
167 (block-close . c-snug-do-while)
169 (c-block-comment-prefix . "")
173 (c-comment-only-line-offset . (0 . 0))
174 ;; the following preserves Javadoc starter lines
175 (c-offsets-alist . ((inline-open . 0)
176 (topmost-intro-cont . +)
177 (statement-block-intro . +)
178 (knr-argdecl-intro . 5)
179 (substatement-open . +)
181 (statement-case-open . +)
183 (arglist-intro . c-lineup-arglist-intro-after-paren)
184 (arglist-close . c-lineup-arglist)
186 (inher-cont . c-lineup-java-inher)
187 (func-decl-cont . c-lineup-java-throws)
191 "Styles of indentation.
192 Elements of this alist are of the form:
194 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
196 where STYLE-STRING is a short descriptive string used to select a
197 style, VARIABLE is any Emacs variable, and VALUE is the intended value
198 for that variable when using the selected style.
200 Optional BASE-STYLE if present, is a string and must follow
201 STYLE-STRING. BASE-STYLE names a style that this style inherits from.
202 By default, all styles inherit from the \"user\" style, which is
203 computed at run time. Style loops generate errors.
205 Two variables are treated specially. When VARIABLE is
206 `c-offsets-alist', the VALUE is a list containing elements of the
209 (SYNTACTIC-SYMBOL . OFFSET)
211 as described in `c-offsets-alist'. These are passed directly to
212 `c-set-offset' so there is no need to set every syntactic symbol in
213 your style, only those that are different from the default.
215 When VARIABLE is `c-special-indent-hook', its VALUE is added to
216 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each
217 element of the list is added with `add-hook'.
219 Do not change this variable directly. Use the function `c-add-style'
220 to add new styles or modify existing styles (it is not a good idea to
221 modify existing styles -- you should create a new style that inherits
222 the existing style.")
226 ;; Functions that manipulate styles
227 (defun c-set-style-1 (conscell dont-override)
228 ;; Set the style for one variable
229 (let ((attr (car conscell))
230 (val (cdr conscell)))
232 ;; first special variable
233 ((eq attr 'c-offsets-alist)
237 (let ((langelem (car langentry))
238 (offset (cdr langentry)))
239 (unless (and dont-override
240 (assq langelem c-offsets-alist))
241 (c-set-offset langelem offset))
243 (if dont-override (reverse val) val)))
244 ;; second special variable
245 ((eq attr 'c-special-indent-hook)
246 (let ((add-func (if dont-override
248 (unless (memq func c-special-indent-hook)
249 (add-hook 'c-special-indent-hook func t)))
251 (add-hook 'c-special-indent-hook func)))))
253 (mapcar add-func (if dont-override (reverse val) val))
254 (funcall add-func val))))
255 ;; all other variables
256 (t (if (or (not dont-override)
257 (not (memq attr c-style-variables))
258 (eq (symbol-value attr) 'set-from-style))
262 (defun c-get-style-variables (style basestyles)
263 ;; Return all variables in a style by resolving inheritances.
264 (let ((vars (cdr (or (assoc (downcase style) c-style-alist)
265 (assoc (upcase style) c-style-alist)
266 (assoc style c-style-alist)
267 (error "Undefined style: %s" style)))))
268 (if (string-equal style "user")
270 (let ((base (if (stringp (car vars))
272 (downcase (car vars))
273 (setq vars (cdr vars)))
275 (if (memq base basestyles)
276 (error "Style loop detected: %s in %s" base basestyles))
277 (nconc (c-get-style-variables base (cons base basestyles))
278 (copy-alist vars))))))
280 (defvar c-set-style-history nil)
283 (defun c-set-style (stylename &optional dont-override)
284 "Set CC Mode variables to use one of several different indentation styles.
285 STYLENAME is a string representing the desired style from the list of
286 styles described in the variable `c-style-alist'. See that variable
287 for details of setting up styles.
289 The variable `c-indentation-style' always contains the buffer's current
292 If the optional argument DONT-OVERRIDE is non-nil, no style variables
293 that already have values will be overridden. I.e. in the case of
294 `c-offsets-alist', syntactic symbols will only be added, and in the
295 case of all other style variables, only those set to `set-from-style'
298 Obviously, specifying DONT-OVERRIDE is useful mainly when the initial
299 style is chosen for a CC Mode buffer by a major mode. Since this is
300 done internally by CC Mode, there's hardly ever a reason to use it."
301 (interactive (list (let ((completion-ignore-case t)
302 (prompt (format "Which %s indentation style? "
304 (completing-read prompt c-style-alist nil t
305 (cons c-indentation-style 0)
306 'c-set-style-history))))
307 (c-initialize-builtin-style)
308 (let ((vars (c-get-style-variables stylename nil)))
309 (mapcar (lambda (elem)
310 (c-set-style-1 elem dont-override))
311 ;; Need to go through the variables backwards when we
313 (if dont-override (nreverse vars) vars)))
314 (setq c-indentation-style stylename)
315 (c-keep-region-active))
318 (defun c-add-style (style descrip &optional set-p)
319 "Adds a style to `c-style-alist', or updates an existing one.
320 STYLE is a string identifying the style to add or update. DESCRIP is
321 an association list describing the style and must be of the form:
323 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
325 See the variable `c-style-alist' for the semantics of BASESTYLE,
326 VARIABLE and VALUE. This function also sets the current style to
327 STYLE using `c-set-style' if the optional SET-P flag is non-nil."
329 (let ((stylename (completing-read "Style to add: " c-style-alist
330 nil nil nil 'c-set-style-history))
331 (description (eval-minibuffer "Style description: ")))
332 (list stylename description
333 (y-or-n-p "Set the style too? "))))
334 (setq style (downcase style))
335 (let ((s (assoc style c-style-alist)))
337 (setcdr s (copy-alist descrip)) ; replace
338 (setq c-style-alist (cons (cons style descrip) c-style-alist))))
339 (and set-p (c-set-style style)))
343 (defvar c-read-offset-history nil)
345 (defun c-read-offset (langelem)
346 ;; read new offset value for LANGELEM from minibuffer. return a
348 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist)
349 (assq langelem (get 'c-offsets-alist
350 'c-stylevar-fallback)))))
351 (symname (symbol-name langelem))
352 (defstr (format "(default %s): " oldoff))
353 (errmsg (concat "Offset must be int, func, var, vector, list, "
354 "or [+,-,++,--,*,/] "
356 (prompt (concat symname " offset " defstr))
357 offset input interned raw)
359 (setq input (completing-read prompt obarray 'fboundp nil nil
360 'c-read-offset-history)
361 offset (cond ((string-equal "" input) oldoff) ; default
362 ((string-equal "+" input) '+)
363 ((string-equal "-" input) '-)
364 ((string-equal "++" input) '++)
365 ((string-equal "--" input) '--)
366 ((string-equal "*" input) '*)
367 ((string-equal "/" input) '/)
368 ((string-match "^-?[0-9]+$" input)
369 (string-to-int input))
370 ;; a symbol with a function binding
371 ((fboundp (setq interned (intern input)))
373 ;; a symbol with variable binding
374 ((boundp interned) interned)
375 ;; a lambda function or a vector
377 (c-safe (setq raw (read input)))
381 ;; error, but don't signal one, keep trying
382 ;; to read an input value
389 (defun c-set-offset (symbol offset &optional ignored)
390 "Change the value of a syntactic element symbol in `c-offsets-alist'.
391 SYMBOL is the syntactic element symbol to change and OFFSET is the new
392 offset for that syntactic element. The optional argument is not used
393 and exists only for compatibility reasons."
396 (intern (completing-read
397 (concat "Syntactic symbol to change"
398 (if current-prefix-arg " or add" "")
402 (cons (format "%s" (car langelem)) nil))
403 (get 'c-offsets-alist 'c-stylevar-fallback))
404 nil (not current-prefix-arg)
405 ;; initial contents tries to be the last element
406 ;; on the syntactic analysis list for the current
408 (let* ((syntax (c-guess-basic-syntax))
409 (len (length syntax))
410 (ic (format "%s" (car (nth (1- len) syntax)))))
413 (offset (c-read-offset langelem)))
414 (list langelem offset current-prefix-arg)))
415 ;; sanity check offset
416 (unless (c-valid-offset offset)
417 (error (concat "Offset must be int, func, var, vector, list, "
418 "or in [+,-,++,--,*,/]: %s")
420 (let ((entry (assq symbol c-offsets-alist)))
422 (setcdr entry offset)
423 (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback))
424 (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist))
425 (error "%s is not a valid syntactic symbol" symbol))))
426 (c-keep-region-active))
430 (defun c-initialize-builtin-style ()
431 ;; Dynamically append the default value of most variables. This is
432 ;; crucial because future c-set-style calls will always reset the
433 ;; variables first to the `cc-mode' style before instituting the new
434 ;; style. Only do this once!
435 (unless (get 'c-initialize-builtin-style 'is-run)
436 (put 'c-initialize-builtin-style 'is-run t)
437 ;;(c-initialize-cc-mode)
438 (or (assoc "cc-mode" c-style-alist)
439 (assoc "user" c-style-alist)
445 (let ((val (symbol-value var)))
447 (cond ((eq var 'c-offsets-alist)
450 (setq langentry (or (assq (car langentry) val)
452 (cons (car langentry)
454 (get var 'c-stylevar-fallback)))
455 ((eq var 'c-special-indent-hook)
458 (if (eq val 'set-from-style)
459 (get var 'c-stylevar-fallback)
462 (c-add-style "cc-mode" '("user"))))
463 (if c-style-variables-are-local-p
464 (c-make-styles-buffer-local))))
466 (defun c-make-styles-buffer-local (&optional this-buf-only-p)
467 "Make all CC Mode style variables buffer local.
468 If you edit primarily one style of C (or C++, Objective-C, Java, etc)
469 code, you probably want style variables to be global. This is the
472 If you edit many different styles of C (or C++, Objective-C, Java,
473 etc) at the same time, you probably want the CC Mode style variables
474 to be buffer local. If you do, it's advicable to set any CC Mode
475 style variables in a hook function (e.g. off of `c-mode-common-hook'),
476 instead of at the top level of your ~/.emacs file.
478 This function makes all the CC Mode style variables buffer local.
479 Call it after CC Mode is loaded into your Emacs environment.
480 Conversely, set the variable `c-style-variables-are-local-p' to t in
481 your .emacs file, before CC Mode is loaded, and this function will be
482 automatically called when CC Mode is loaded.
484 Optional argument, when non-nil, means use `make-local-variable'
485 instead of `make-variable-buffer-local'."
487 (let ((func (if this-buf-only-p
489 'make-variable-buffer-local))
490 (varsyms (cons 'c-indentation-style (copy-alist c-style-variables))))
491 (delq 'c-special-indent-hook varsyms)
492 (mapcar func varsyms)
493 ;; Hooks must be handled specially
495 (make-local-hook 'c-special-indent-hook)
496 (make-variable-buffer-local 'c-special-indent-hook)
497 (setq c-style-variables-are-local-p t))
502 (cc-provide 'cc-styles)
503 ;;; cc-styles.el ends here