(easy-menu-intern):
[emacs.git] / lisp / progmodes / cc-styles.el
blob1a26e54bf06996306be9e3f49406bfd1a66b0170
1 ;;; cc-styles.el --- support for styles in CC Mode
3 ;; Copyright (C) 1985,1987,1992-2003 Free Software Foundation, Inc.
5 ;; Authors: 1998- Martin Stjernholm
6 ;; 1992-1999 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)
19 ;; any later version.
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
28 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
31 ;;; Commentary:
33 ;;; Code:
35 (eval-when-compile
36 (let ((load-path
37 (if (and (boundp 'byte-compile-dest-file)
38 (stringp byte-compile-dest-file))
39 (cons (file-name-directory byte-compile-dest-file) load-path)
40 load-path)))
41 (load "cc-bytecomp" nil t)))
43 (cc-require 'cc-defs)
44 (cc-require 'cc-vars)
45 (cc-require 'cc-align)
46 ;; cc-align is only indirectly required: Styles added with
47 ;; `c-add-style' often contains references to functions defined there.
49 ;; Silence the compiler.
50 (cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs
53 (defvar c-style-alist
54 '(("gnu"
55 (c-basic-offset . 2)
56 (c-comment-only-line-offset . (0 . 0))
57 (c-offsets-alist . ((statement-block-intro . +)
58 (knr-argdecl-intro . 5)
59 (substatement-open . +)
60 (substatement-label . 0)
61 (label . 0)
62 (statement-case-open . +)
63 (statement-cont . +)
64 (arglist-intro . c-lineup-arglist-intro-after-paren)
65 (arglist-close . c-lineup-arglist)
66 (inline-open . 0)
67 (brace-list-open . +)
69 (c-special-indent-hook . c-gnu-impose-minimum)
70 (c-block-comment-prefix . "")
72 ("k&r"
73 (c-basic-offset . 5)
74 (c-comment-only-line-offset . 0)
75 (c-offsets-alist . ((statement-block-intro . +)
76 (knr-argdecl-intro . 0)
77 (substatement-open . 0)
78 (substatement-label . 0)
79 (label . 0)
80 (statement-cont . +)
83 ("bsd"
84 (c-basic-offset . 8)
85 (c-comment-only-line-offset . 0)
86 (c-offsets-alist . ((statement-block-intro . +)
87 (knr-argdecl-intro . +)
88 (substatement-open . 0)
89 (substatement-label . 0)
90 (label . 0)
91 (statement-cont . +)
92 (inline-open . 0)
93 (inexpr-class . 0)
96 ("stroustrup"
97 (c-basic-offset . 4)
98 (c-comment-only-line-offset . 0)
99 (c-offsets-alist . ((statement-block-intro . +)
100 (substatement-open . 0)
101 (substatement-label . 0)
102 (label . 0)
103 (statement-cont . +)
106 ("whitesmith"
107 (c-basic-offset . 4)
108 (c-comment-only-line-offset . 0)
109 (c-offsets-alist . ((knr-argdecl-intro . +)
110 (label . 0)
111 (statement-cont . +)
112 (substatement-open . +)
113 (substatement-label . +)
114 (block-open . +)
115 (statement-block-intro . c-lineup-whitesmith-in-block)
116 (block-close . c-lineup-whitesmith-in-block)
117 (inline-open . +)
118 (defun-open . +)
119 (defun-block-intro . c-lineup-whitesmith-in-block)
120 (defun-close . c-lineup-whitesmith-in-block)
121 (brace-list-open . +)
122 (brace-list-intro . c-lineup-whitesmith-in-block)
123 (brace-entry-open . c-indent-multi-line-block)
124 (brace-list-close . c-lineup-whitesmith-in-block)
125 (class-open . +)
126 (inclass . c-lineup-whitesmith-in-block)
127 (class-close . +)
128 (inexpr-class . 0)
129 (extern-lang-open . +)
130 (inextern-lang . c-lineup-whitesmith-in-block)
131 (extern-lang-close . +)
132 (namespace-open . +)
133 (innamespace . c-lineup-whitesmith-in-block)
134 (namespace-close . +)
135 (module-open . +)
136 (inmodule . c-lineup-whitesmith-in-block)
137 (module-close . +)
138 (composition-open . +)
139 (incomposition . c-lineup-whitesmith-in-block)
140 (composition-close . +)
143 ("ellemtel"
144 (c-basic-offset . 3)
145 (c-comment-only-line-offset . 0)
146 (c-hanging-braces-alist . ((substatement-open before after)))
147 (c-offsets-alist . ((topmost-intro . 0)
148 (substatement . +)
149 (substatement-open . 0)
150 (case-label . +)
151 (access-label . -)
152 (inclass . ++)
153 (inline-open . 0)
156 ("linux"
157 (c-basic-offset . 8)
158 (c-comment-only-line-offset . 0)
159 (c-hanging-braces-alist . ((brace-list-open)
160 (brace-entry-open)
161 (substatement-open after)
162 (block-close . c-snug-do-while)))
163 (c-cleanup-list . (brace-else-brace))
164 (c-offsets-alist . ((statement-block-intro . +)
165 (knr-argdecl-intro . 0)
166 (substatement-open . 0)
167 (substatement-label . 0)
168 (label . 0)
169 (statement-cont . +)
172 ("python"
173 (indent-tabs-mode . t)
174 (fill-column . 78)
175 (c-basic-offset . 8)
176 (c-offsets-alist . ((substatement-open . 0)
177 (inextern-lang . 0)
178 (arglist-intro . +)
179 (knr-argdecl-intro . +)
181 (c-hanging-braces-alist . ((brace-list-open)
182 (brace-list-intro)
183 (brace-list-close)
184 (brace-entry-open)
185 (substatement-open after)
186 (block-close . c-snug-do-while)
188 (c-block-comment-prefix . "")
190 ("java"
191 (c-basic-offset . 4)
192 (c-comment-only-line-offset . (0 . 0))
193 ;; the following preserves Javadoc starter lines
194 (c-offsets-alist . ((inline-open . 0)
195 (topmost-intro-cont . +)
196 (statement-block-intro . +)
197 (knr-argdecl-intro . 5)
198 (substatement-open . +)
199 (substatement-label . +)
200 (label . +)
201 (statement-case-open . +)
202 (statement-cont . +)
203 (arglist-intro . c-lineup-arglist-intro-after-paren)
204 (arglist-close . c-lineup-arglist)
205 (access-label . 0)
206 (inher-cont . c-lineup-java-inher)
207 (func-decl-cont . c-lineup-java-throws)
211 "Styles of indentation.
212 Elements of this alist are of the form:
214 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
216 where STYLE-STRING is a short descriptive string used to select a
217 style, VARIABLE is any Emacs variable, and VALUE is the intended value
218 for that variable when using the selected style.
220 Optional BASE-STYLE if present, is a string and must follow
221 STYLE-STRING. BASE-STYLE names a style that this style inherits from.
222 By default, all styles inherit from the \"user\" style, which is
223 computed at run time. Style loops generate errors.
225 Two variables are treated specially. When VARIABLE is
226 `c-offsets-alist', the VALUE is a list containing elements of the
227 form:
229 (SYNTACTIC-SYMBOL . OFFSET)
231 as described in `c-offsets-alist'. These are passed directly to
232 `c-set-offset' so there is no need to set every syntactic symbol in
233 your style, only those that are different from the default.
235 When VARIABLE is `c-special-indent-hook', its VALUE is added to
236 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each
237 element of the list is added with `add-hook'.
239 Do not change this variable directly. Use the function `c-add-style'
240 to add new styles or modify existing styles (it is not a good idea to
241 modify existing styles -- you should create a new style that inherits
242 the existing style.")
245 ;; Functions that manipulate styles
246 (defun c-set-style-1 (conscell dont-override)
247 ;; Set the style for one variable
249 ;; This function does not do any hidden buffer changes.
250 (let ((attr (car conscell))
251 (val (cdr conscell)))
252 (cond
253 ;; first special variable
254 ((eq attr 'c-offsets-alist)
255 (let ((offsets (cond ((eq dont-override t)
256 c-offsets-alist)
257 (dont-override
258 (default-value 'c-offsets-alist)))))
259 (mapcar (lambda (langentry)
260 (let ((langelem (car langentry))
261 (offset (cdr langentry)))
262 (unless (assq langelem offsets)
263 (c-set-offset langelem offset))))
264 val)))
265 ;; second special variable
266 ((eq attr 'c-special-indent-hook)
267 ;; Maybe we should ignore dont-override here and always add new
268 ;; hooks?
269 (unless (cond ((eq dont-override t)
270 c-special-indent-hook)
271 (dont-override
272 (default-value 'c-special-indent-hook)))
273 (if (listp val)
274 (mapcar (lambda (func)
275 (add-hook 'c-special-indent-hook func t t))
276 val)
277 (add-hook 'c-special-indent-hook val t t))))
278 ;; all other variables
279 (t (when (or (not dont-override)
280 (not (memq attr c-style-variables))
281 (eq (if (eq dont-override t)
282 (symbol-value attr)
283 (default-value attr))
284 'set-from-style))
285 (set attr val)
286 ;; Must update a number of other variables if
287 ;; c-comment-prefix-regexp is set.
288 (if (eq attr 'c-comment-prefix-regexp)
289 (c-setup-paragraph-variables)))))))
291 (defun c-get-style-variables (style basestyles)
292 ;; Return all variables in a style by resolving inheritances.
294 ;; This function does not do any hidden buffer changes.
295 (if (not style)
296 (copy-alist c-fallback-style)
297 (let ((vars (cdr (or (assoc (downcase style) c-style-alist)
298 (assoc (upcase style) c-style-alist)
299 (assoc style c-style-alist)
300 (progn
301 (c-benign-error "Undefined style: %s" style)
302 nil)))))
303 (let ((base (and (stringp (car-safe vars))
304 (prog1
305 (downcase (car vars))
306 (setq vars (cdr vars))))))
307 (if (memq base basestyles)
308 (c-benign-error "Style loop detected: %s in %s" base basestyles)
309 (nconc (c-get-style-variables base (cons base basestyles))
310 (copy-alist vars)))))))
312 (defvar c-set-style-history nil)
314 ;;;###autoload
315 (defun c-set-style (stylename &optional dont-override)
316 "Set CC Mode variables to use one of several different indentation styles.
317 STYLENAME is a string representing the desired style from the list of
318 styles described in the variable `c-style-alist'. See that variable
319 for details of setting up styles.
321 The variable `c-indentation-style' always contains the buffer's current
322 style name.
324 If the optional argument DONT-OVERRIDE is t, no style variables that
325 already have values will be overridden. I.e. in the case of
326 `c-offsets-alist', syntactic symbols will only be added, and in the
327 case of all other style variables, only those set to `set-from-style'
328 will be reassigned.
330 If DONT-OVERRIDE is neither nil nor t, only those style variables that
331 have default (i.e. non-buffer local) values will keep their settings
332 while the rest will be overridden. This is useful to avoid overriding
333 global settings done in ~/.emacs when setting a style from a mode hook
334 \(providing the style variables are buffer local, which is the
335 default).
337 Obviously, setting DONT-OVERRIDE to t is useful mainly when the
338 initial style is chosen for a CC Mode buffer by a major mode. Since
339 that is done internally by CC Mode, it typically won't have any effect
340 when used elsewhere."
341 (interactive
342 (list (let ((completion-ignore-case t)
343 (prompt (format "Which %s indentation style? "
344 mode-name)))
345 (condition-case nil
346 ;; The default argument is preferred over
347 ;; initial-contents, but it only exists in Emacs >= 20
348 ;; and XEmacs >= 21.
349 (completing-read prompt c-style-alist nil t nil
350 'c-set-style-history
351 c-indentation-style)
352 (wrong-number-of-arguments
353 ;; If the call above failed, we fall back to the old way
354 ;; of specifying the default value.
355 (completing-read prompt c-style-alist nil t
356 (cons c-indentation-style 0)
357 'c-set-style-history))))))
358 (or (stringp stylename)
359 (error "Argument to c-set-style was not a string"))
360 (c-initialize-builtin-style)
361 (let ((vars (c-get-style-variables stylename nil)))
362 (unless dont-override
363 ;; Since we always add to c-special-indent-hook we must reset it
364 ;; first, or else the hooks from the preceding style will
365 ;; remain. This is not necessary for c-offsets-alist, since
366 ;; c-get-style-variables contains every valid offset type in the
367 ;; fallback entry.
368 (setq c-special-indent-hook
369 (default-value 'c-special-indent-hook)))
370 (mapcar (lambda (elem)
371 (c-set-style-1 elem dont-override))
372 ;; Need to go through the variables backwards when we
373 ;; don't override any settings.
374 (if (eq dont-override t) (nreverse vars) vars)))
375 (setq c-indentation-style stylename)
376 (c-keep-region-active))
378 ;;;###autoload
379 (defun c-add-style (style description &optional set-p)
380 "Adds a style to `c-style-alist', or updates an existing one.
381 STYLE is a string identifying the style to add or update. DESCRIPTION
382 is an association list describing the style and must be of the form:
384 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
386 See the variable `c-style-alist' for the semantics of BASESTYLE,
387 VARIABLE and VALUE. This function also sets the current style to
388 STYLE using `c-set-style' if the optional SET-P flag is non-nil."
389 (interactive
390 (let ((stylename (completing-read "Style to add: " c-style-alist
391 nil nil nil 'c-set-style-history))
392 (descr (eval-minibuffer "Style description: ")))
393 (list stylename descr
394 (y-or-n-p "Set the style too? "))))
395 (setq style (downcase style))
396 (let ((s (assoc style c-style-alist)))
397 (if s
398 (setcdr s (copy-alist description)) ; replace
399 (setq c-style-alist (cons (cons style description) c-style-alist))))
400 (and set-p (c-set-style style)))
403 (defvar c-read-offset-history nil)
405 (defun c-read-offset (langelem)
406 ;; read new offset value for LANGELEM from minibuffer. return a
407 ;; legal value only
409 ;; This function does not do any hidden buffer changes.
410 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist)
411 (assq langelem (get 'c-offsets-alist
412 'c-stylevar-fallback)))))
413 (symname (symbol-name langelem))
414 (defstr (format "(default %s): " oldoff))
415 (errmsg (concat "Offset must be int, func, var, vector, list, "
416 "or [+,-,++,--,*,/] "
417 defstr))
418 (prompt (concat symname " offset " defstr))
419 (keymap (make-sparse-keymap))
420 (minibuffer-completion-table obarray)
421 (minibuffer-completion-predicate 'fboundp)
422 offset input)
423 ;; In principle completing-read is used here, but SPC is unbound
424 ;; to make it less annoying to enter lists.
425 (set-keymap-parent keymap minibuffer-local-completion-map)
426 (define-key keymap " " 'self-insert-command)
427 (while (not offset)
428 (setq input (read-from-minibuffer prompt nil keymap t
429 'c-read-offset-history
430 (format "%s" oldoff)))
431 (if (c-valid-offset input)
432 (setq offset input)
433 ;; error, but don't signal one, keep trying
434 ;; to read an input value
435 (ding)
436 (setq prompt errmsg)))
437 offset))
439 ;;;###autoload
440 (defun c-set-offset (symbol offset &optional ignored)
441 "Change the value of a syntactic element symbol in `c-offsets-alist'.
442 SYMBOL is the syntactic element symbol to change and OFFSET is the new
443 offset for that syntactic element. The optional argument is not used
444 and exists only for compatibility reasons."
445 (interactive
446 (let* ((langelem
447 (intern (completing-read
448 (concat "Syntactic symbol to change"
449 (if current-prefix-arg " or add" "")
450 ": ")
451 (mapcar
452 #'(lambda (langelem)
453 (cons (format "%s" (car langelem)) nil))
454 (get 'c-offsets-alist 'c-stylevar-fallback))
455 nil (not current-prefix-arg)
456 ;; initial contents tries to be the last element
457 ;; on the syntactic analysis list for the current
458 ;; line
459 (and c-buffer-is-cc-mode
460 (c-save-buffer-state
461 ((syntax (c-guess-basic-syntax))
462 (len (length syntax))
463 (ic (format "%s" (car (nth (1- len) syntax)))))
464 (cons ic 0)))
466 (offset (c-read-offset langelem)))
467 (list langelem offset current-prefix-arg)))
468 ;; sanity check offset
469 (if (c-valid-offset offset)
470 (let ((entry (assq symbol c-offsets-alist)))
471 (if entry
472 (setcdr entry offset)
473 (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback))
474 (setq c-offsets-alist (cons (cons symbol offset)
475 c-offsets-alist))
476 (c-benign-error "%s is not a valid syntactic symbol" symbol))))
477 (c-benign-error "Invalid indentation setting for symbol %s: %s"
478 symbol offset))
479 (c-keep-region-active))
482 (defun c-setup-paragraph-variables ()
483 "Fix things up for paragraph recognition and filling inside comments by
484 incorporating the value of `c-comment-prefix-regexp' in the relevant
485 variables."
487 ;; This function does not do any hidden buffer changes.
489 (interactive)
491 (setq c-current-comment-prefix
492 (if (listp c-comment-prefix-regexp)
493 (cdr-safe (or (assoc major-mode c-comment-prefix-regexp)
494 (assoc 'other c-comment-prefix-regexp)))
495 c-comment-prefix-regexp))
497 (let ((comment-line-prefix
498 (concat "[ \t]*\\(" c-current-comment-prefix "\\)[ \t]*")))
500 (setq paragraph-start (concat comment-line-prefix
501 c-paragraph-start
502 "\\|"
503 page-delimiter)
504 paragraph-separate (concat comment-line-prefix
505 c-paragraph-separate
506 "\\|"
507 page-delimiter)
508 paragraph-ignore-fill-prefix t
509 adaptive-fill-mode t
510 adaptive-fill-regexp
511 (concat comment-line-prefix
512 (if (default-value 'adaptive-fill-regexp)
513 (concat "\\("
514 (default-value 'adaptive-fill-regexp)
515 "\\)")
516 "")))
518 (when (boundp 'adaptive-fill-first-line-regexp)
519 ;; XEmacs (20.x) adaptive fill mode doesn't have this.
520 (make-local-variable 'adaptive-fill-first-line-regexp)
521 (setq adaptive-fill-first-line-regexp
522 (concat "\\`" comment-line-prefix
523 ;; Maybe we should incorporate the old value here,
524 ;; but then we have to do all sorts of kludges to
525 ;; deal with the \` and \' it probably contains.
526 "\\'")))))
529 ;; Helper for setting up Filladapt mode. It's not used by CC Mode itself.
531 (cc-bytecomp-defvar filladapt-token-table)
532 (cc-bytecomp-defvar filladapt-token-match-table)
533 (cc-bytecomp-defvar filladapt-token-conversion-table)
535 (defun c-setup-filladapt ()
536 "Convenience function to configure Kyle E. Jones' Filladapt mode for
537 CC Mode by making sure the proper entries are present on
538 `filladapt-token-table', `filladapt-token-match-table', and
539 `filladapt-token-conversion-table'. This is intended to be used on
540 `c-mode-common-hook' or similar."
541 ;; This function is intended to be used explicitly by the end user
542 ;; only.
544 ;; This function does not do any hidden buffer changes.
546 ;; The default configuration already handles C++ comments, but we
547 ;; need to add handling of C block comments. A new filladapt token
548 ;; `c-comment' is added for that.
549 (let (p)
550 (setq p filladapt-token-table)
551 (while (and p (not (eq (car-safe (cdr-safe (car-safe p))) 'c-comment)))
552 (setq p (cdr-safe p)))
553 (if p
554 (setcar (car p) c-current-comment-prefix)
555 (setq filladapt-token-table
556 (append (list (car filladapt-token-table)
557 (list c-current-comment-prefix 'c-comment))
558 (cdr filladapt-token-table)))))
559 (unless (assq 'c-comment filladapt-token-match-table)
560 (setq filladapt-token-match-table
561 (append '((c-comment c-comment))
562 filladapt-token-match-table)))
563 (unless (assq 'c-comment filladapt-token-conversion-table)
564 (setq filladapt-token-conversion-table
565 (append '((c-comment . exact))
566 filladapt-token-conversion-table))))
569 (defun c-initialize-builtin-style ()
570 ;; Dynamically append the default value of most variables. This is
571 ;; crucial because future c-set-style calls will always reset the
572 ;; variables first to the `cc-mode' style before instituting the new
573 ;; style. Only do this once!
575 ;; This function does not do any hidden buffer changes.
576 (unless (get 'c-initialize-builtin-style 'is-run)
577 (put 'c-initialize-builtin-style 'is-run t)
578 ;;(c-initialize-cc-mode)
579 (unless (assoc "user" c-style-alist)
580 (let ((vars c-style-variables) var val uservars)
581 (while vars
582 (setq var (car vars)
583 val (symbol-value var)
584 vars (cdr vars))
585 (cond ((eq var 'c-offsets-alist)
586 (or (null val)
587 (setq uservars (cons (cons 'c-offsets-alist val)
588 uservars))))
589 ((not (eq val 'set-from-style))
590 (setq uservars (cons (cons var val)
591 uservars)))))
592 (c-add-style "user" uservars)))
593 (unless (assoc "cc-mode" c-style-alist)
594 (c-add-style "cc-mode" '("user")))
595 (if c-style-variables-are-local-p
596 (c-make-styles-buffer-local))))
598 (defun c-make-styles-buffer-local (&optional this-buf-only-p)
599 "Make all CC Mode style variables buffer local.
600 If `this-buf-only-p' is non-nil, the style variables will be made
601 buffer local only in the current buffer. Otherwise they'll be made
602 permanently buffer local in any buffer that change their values.
604 The buffer localness of the style variables are normally controlled
605 with the variable `c-style-variables-are-local-p', so there's seldom
606 any reason to call this function directly."
608 ;; This function does not do any hidden buffer changes.
610 ;; style variables
611 (let ((func (if this-buf-only-p
612 'make-local-variable
613 'make-variable-buffer-local))
614 (varsyms (cons 'c-indentation-style (copy-alist c-style-variables))))
615 (delq 'c-special-indent-hook varsyms)
616 (mapcar func varsyms)
617 ;; Hooks must be handled specially
618 (if this-buf-only-p
619 (make-local-hook 'c-special-indent-hook)
620 (make-variable-buffer-local 'c-special-indent-hook)
621 (setq c-style-variables-are-local-p t))
626 (cc-provide 'cc-styles)
628 ;;; arch-tag: c764f61a-96ba-484a-a68f-101c0e9d5d2c
629 ;;; cc-styles.el ends here