*** empty log message ***
[emacs.git] / lisp / progmodes / cc-styles.el
blobc7f1adaa4348bbef226ed7cc311357d5bf532016
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)
20 ;; any later version.
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.
32 ;;; Commentary:
34 ;;; Code:
36 (eval-when-compile
37 (let ((load-path
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)
41 load-path)))
42 (require 'cc-bytecomp)))
44 (cc-require 'cc-defs)
45 (cc-require 'cc-langs)
46 (cc-require 'cc-vars)
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
57 '(("gnu"
58 (c-basic-offset . 2)
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)
64 (label . 0)
65 (statement-case-open . +)
66 (statement-cont . +)
67 (arglist-intro . c-lineup-arglist-intro-after-paren)
68 (arglist-close . c-lineup-arglist)
69 (inline-open . 0)
70 (brace-list-open . +)
72 (c-special-indent-hook . c-gnu-impose-minimum)
73 (c-block-comment-prefix . "")
75 ("k&r"
76 (c-basic-offset . 5)
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)
82 (label . 0)
83 (statement-cont . +)
86 ("bsd"
87 (c-basic-offset . 8)
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)
93 (label . 0)
94 (statement-cont . +)
95 (inline-open . 0)
96 (inexpr-class . 0)
99 ("stroustrup"
100 (c-basic-offset . 4)
101 (c-comment-only-line-offset . 0)
102 (c-offsets-alist . ((statement-block-intro . +)
103 (substatement-open . 0)
104 (substatement-label . 0)
105 (label . 0)
106 (statement-cont . +)
109 ("whitesmith"
110 (c-basic-offset . 4)
111 (c-comment-only-line-offset . 0)
112 (c-offsets-alist . ((knr-argdecl-intro . +)
113 (label . 0)
114 (statement-cont . +)
115 (substatement-open . +)
116 (substatement-label . +)
117 (block-open . +)
118 (statement-block-intro . c-lineup-whitesmith-in-block)
119 (block-close . c-lineup-whitesmith-in-block)
120 (inline-open . +)
121 (defun-open . +)
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)
128 (class-open . +)
129 (inclass . c-lineup-whitesmith-in-block)
130 (class-close . +)
131 (inexpr-class . 0)
132 (extern-lang-open . +)
133 (inextern-lang . c-lineup-whitesmith-in-block)
134 (extern-lang-close . +)
135 (namespace-open . +)
136 (innamespace . c-lineup-whitesmith-in-block)
137 (namespace-close . +)
140 ("ellemtel"
141 (c-basic-offset . 3)
142 (c-comment-only-line-offset . 0)
143 (c-hanging-braces-alist . ((substatement-open before after)))
144 (c-offsets-alist . ((topmost-intro . 0)
145 (substatement . +)
146 (substatement-open . 0)
147 (case-label . +)
148 (access-label . -)
149 (inclass . ++)
150 (inline-open . 0)
153 ("linux"
154 (c-basic-offset . 8)
155 (c-comment-only-line-offset . 0)
156 (c-hanging-braces-alist . ((brace-list-open)
157 (brace-entry-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)
165 (label . 0)
166 (statement-cont . +)
169 ("python"
170 (indent-tabs-mode . t)
171 (fill-column . 78)
172 (c-basic-offset . 8)
173 (c-offsets-alist . ((substatement-open . 0)
174 (inextern-lang . 0)
175 (arglist-intro . +)
176 (knr-argdecl-intro . +)
178 (c-hanging-braces-alist . ((brace-list-open)
179 (brace-list-intro)
180 (brace-list-close)
181 (brace-entry-open)
182 (substatement-open after)
183 (block-close . c-snug-do-while)
185 (c-block-comment-prefix . "")
187 ("java"
188 (c-basic-offset . 4)
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 . +)
197 (label . +)
198 (statement-case-open . +)
199 (statement-cont . +)
200 (arglist-intro . c-lineup-arglist-intro-after-paren)
201 (arglist-close . c-lineup-arglist)
202 (access-label . 0)
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
224 form:
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)))
247 (cond
248 ;; first special variable
249 ((eq attr 'c-offsets-alist)
250 (let ((offsets (cond ((eq dont-override t)
251 c-offsets-alist)
252 (dont-override
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))))
259 val)))
260 ;; second special variable
261 ((eq attr 'c-special-indent-hook)
262 ;; Maybe we should ignore dont-override here and always add new
263 ;; hooks?
264 (unless (cond ((eq dont-override t)
265 c-special-indent-hook)
266 (dont-override
267 (default-value 'c-special-indent-hook)))
268 (if (listp val)
269 (mapcar (lambda (func)
270 (add-hook 'c-special-indent-hook func t t))
271 val)
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)
277 (symbol-value attr)
278 (default-value attr))
279 'set-from-style))
280 (set attr val)
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.
288 (if (not style)
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)
293 (progn
294 (c-benign-error "Undefined style: %s" style)
295 nil)))))
296 (let ((base (and (stringp (car-safe vars))
297 (prog1
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)
307 ;;;###autoload
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
315 style name.
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'
321 will be reassigned.
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
328 default).
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."
334 (interactive
335 (list (let ((completion-ignore-case t)
336 (prompt (format "Which %s indentation style? "
337 mode-name)))
338 (condition-case nil
339 ;; The default argument is preferred over
340 ;; initial-contents, but it only exists in Emacs >= 20
341 ;; and XEmacs >= 21.
342 (completing-read prompt c-style-alist nil t nil
343 'c-set-style-history
344 c-indentation-style)
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
358 ;; fallback entry.
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))
369 ;;;###autoload
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."
380 (interactive
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)))
388 (if s
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
398 ;; legal value only
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 [+,-,++,--,*,/] "
406 defstr))
407 (prompt (concat symname " offset " defstr))
408 (keymap (make-sparse-keymap))
409 (minibuffer-completion-table obarray)
410 (minibuffer-completion-predicate 'fboundp)
411 offset input)
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)
416 (while (not offset)
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)
421 (setq offset input)
422 ;; error, but don't signal one, keep trying
423 ;; to read an input value
424 (ding)
425 (setq prompt errmsg)))
426 offset))
428 ;;;###autoload
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."
434 (interactive
435 (let* ((langelem
436 (intern (completing-read
437 (concat "Syntactic symbol to change"
438 (if current-prefix-arg " or add" "")
439 ": ")
440 (mapcar
441 #'(lambda (langelem)
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
447 ;; line
448 (and c-buffer-is-cc-mode
449 (let* ((syntax (c-guess-basic-syntax))
450 (len (length syntax))
451 (ic (format "%s" (car (nth (1- len) syntax)))))
452 (cons ic 0)))
454 (offset (c-read-offset langelem)))
455 (list langelem offset current-prefix-arg)))
456 ;; sanity check offset
457 (if (c-valid-offset offset)
458 (let ((entry (assq symbol c-offsets-alist)))
459 (if entry
460 (setcdr entry offset)
461 (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback))
462 (setq c-offsets-alist (cons (cons symbol offset)
463 c-offsets-alist))
464 (c-benign-error "%s is not a valid syntactic symbol" symbol))))
465 (c-benign-error "Invalid indentation setting for symbol %s: %s"
466 symbol offset))
467 (c-keep-region-active))
470 (defun c-setup-paragraph-variables ()
471 "Fix things up for paragraph recognition and filling inside comments by
472 incorporating the value of `c-comment-prefix-regexp' in the relevant
473 variables."
474 (setq c-current-comment-prefix
475 (if (listp c-comment-prefix-regexp)
476 (cdr-safe (or (assoc major-mode c-comment-prefix-regexp)
477 (assoc 'other c-comment-prefix-regexp)))
478 c-comment-prefix-regexp))
479 (let ((comment-line-prefix
480 (concat "[ \t]*\\(" c-current-comment-prefix "\\)[ \t]*")))
481 (setq paragraph-start (concat comment-line-prefix
482 (c-lang-var paragraph-start)
483 "\\|"
484 page-delimiter)
485 paragraph-separate (concat comment-line-prefix
486 (c-lang-var paragraph-separate)
487 "\\|"
488 page-delimiter)
489 paragraph-ignore-fill-prefix t
490 adaptive-fill-mode t
491 adaptive-fill-regexp
492 (concat comment-line-prefix
493 (if (default-value 'adaptive-fill-regexp)
494 (concat "\\("
495 (default-value 'adaptive-fill-regexp)
496 "\\)")
497 "")))
498 (when (boundp 'adaptive-fill-first-line-regexp)
499 ;; XEmacs (20.x) adaptive fill mode doesn't have this.
500 (make-local-variable 'adaptive-fill-first-line-regexp)
501 (setq adaptive-fill-first-line-regexp
502 (concat "\\`" comment-line-prefix
503 ;; Maybe we should incorporate the old value here,
504 ;; but then we have to do all sorts of kludges to
505 ;; deal with the \` and \' it probably contains.
506 "\\'")))))
509 ;; Helper for setting up Filladapt mode. It's not used by CC Mode itself.
511 (cc-bytecomp-defvar filladapt-token-table)
512 (cc-bytecomp-defvar filladapt-token-match-table)
513 (cc-bytecomp-defvar filladapt-token-conversion-table)
515 (defun c-setup-filladapt ()
516 "Convenience function to configure Kyle E. Jones' Filladapt mode for
517 CC Mode by making sure the proper entries are present on
518 `filladapt-token-table', `filladapt-token-match-table', and
519 `filladapt-token-conversion-table'. This is intended to be used on
520 `c-mode-common-hook' or similar."
521 ;; This function is intended to be used explicitly by the end user
522 ;; only.
524 ;; The default configuration already handles C++ comments, but we
525 ;; need to add handling of C block comments. A new filladapt token
526 ;; `c-comment' is added for that.
527 (let (p)
528 (setq p filladapt-token-table)
529 (while (and p (not (eq (car-safe (cdr-safe (car-safe p))) 'c-comment)))
530 (setq p (cdr-safe p)))
531 (if p
532 (setcar (car p) c-current-comment-prefix)
533 (setq filladapt-token-table
534 (append (list (car filladapt-token-table)
535 (list c-current-comment-prefix 'c-comment))
536 (cdr filladapt-token-table)))))
537 (unless (assq 'c-comment filladapt-token-match-table)
538 (setq filladapt-token-match-table
539 (append '((c-comment c-comment))
540 filladapt-token-match-table)))
541 (unless (assq 'c-comment filladapt-token-conversion-table)
542 (setq filladapt-token-conversion-table
543 (append '((c-comment . exact))
544 filladapt-token-conversion-table))))
547 (defun c-initialize-builtin-style ()
548 ;; Dynamically append the default value of most variables. This is
549 ;; crucial because future c-set-style calls will always reset the
550 ;; variables first to the `cc-mode' style before instituting the new
551 ;; style. Only do this once!
552 (unless (get 'c-initialize-builtin-style 'is-run)
553 (put 'c-initialize-builtin-style 'is-run t)
554 ;;(c-initialize-cc-mode)
555 (unless (assoc "user" c-style-alist)
556 (let ((vars c-style-variables) var val uservars)
557 (while vars
558 (setq var (car vars)
559 val (symbol-value var)
560 vars (cdr vars))
561 (cond ((eq var 'c-offsets-alist)
562 (or (null val)
563 (setq uservars (cons (cons 'c-offsets-alist val)
564 uservars))))
565 ((not (eq val 'set-from-style))
566 (setq uservars (cons (cons var val)
567 uservars)))))
568 (c-add-style "user" uservars)))
569 (unless (assoc "cc-mode" c-style-alist)
570 (c-add-style "cc-mode" '("user")))
571 (if c-style-variables-are-local-p
572 (c-make-styles-buffer-local))))
574 (defun c-make-styles-buffer-local (&optional this-buf-only-p)
575 "Make all CC Mode style variables buffer local.
576 If you edit primarily one style of C (or C++, Objective-C, Java, etc)
577 code, you probably want style variables to be global. This is the
578 default.
580 If you edit many different styles of C (or C++, Objective-C, Java,
581 etc) at the same time, you probably want the CC Mode style variables
582 to be buffer local. If you do, it's advisable to set any CC Mode
583 style variables in a hook function (e.g. off of `c-mode-common-hook'),
584 instead of at the top level of your ~/.emacs file.
586 This function makes all the CC Mode style variables buffer local.
587 Call it after CC Mode is loaded into your Emacs environment.
588 Conversely, set the variable `c-style-variables-are-local-p' to t in
589 your .emacs file, before CC Mode is loaded, and this function will be
590 automatically called when CC Mode is loaded.
592 Optional argument, when non-nil, means use `make-local-variable'
593 instead of `make-variable-buffer-local'."
594 ;; style variables
595 (let ((func (if this-buf-only-p
596 'make-local-variable
597 'make-variable-buffer-local))
598 (varsyms (cons 'c-indentation-style (copy-alist c-style-variables))))
599 (delq 'c-special-indent-hook varsyms)
600 (mapcar func varsyms)
601 ;; Hooks must be handled specially
602 (if this-buf-only-p
603 (make-local-hook 'c-special-indent-hook)
604 (make-variable-buffer-local 'c-special-indent-hook)
605 (setq c-style-variables-are-local-p t))
610 (cc-provide 'cc-styles)
612 ;;; cc-styles.el ends here