(auto-mode-alist): .f95 files in f90-mode.
[bpt/emacs.git] / lisp / progmodes / cc-styles.el
CommitLineData
785eecbb
RS
1;;; cc-styles.el --- support for styles in CC Mode
2
130c507e 3;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
785eecbb 4
6cfd56b3
GM
5;; Authors: 2000- Martin Stjernholm
6;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
0ec8351b 7;; 1992-1997 Barry A. Warsaw
785eecbb
RS
8;; 1987 Dave Detlefs and Stewart Clamen
9;; 1985 Richard M. Stallman
0ec8351b 10;; Maintainer: bug-cc-mode@gnu.org
785eecbb 11;; Created: 22-Apr-1997 (split from cc-mode.el)
82aba9f4 12;; Version: See cc-mode.el
785eecbb
RS
13;; Keywords: c languages oop
14
15;; This file is part of GNU Emacs.
16
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.
21
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.
26
27;; You should have received a copy of the GNU General Public License
130c507e
GM
28;; along with this program; see the file COPYING. If not, write to
29;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
785eecbb
RS
30;; Boston, MA 02111-1307, USA.
31
3afbc435
PJ
32;;; Commentary:
33
34;;; Code:
35
0ec8351b 36(eval-when-compile
51f606de 37 (let ((load-path
130c507e
GM
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)
51f606de 41 load-path)))
130c507e 42 (require 'cc-bytecomp)))
51f606de 43
130c507e
GM
44(cc-require 'cc-defs)
45(cc-require 'cc-vars)
0ec8351b 46
785eecbb 47\f
0ec8351b 48;; Warning: don't eval-defun this constant or you'll break style inheritance.
785eecbb
RS
49(defconst c-style-alist
50 '(("gnu"
51 (c-basic-offset . 2)
52 (c-comment-only-line-offset . (0 . 0))
53 (c-offsets-alist . ((statement-block-intro . +)
54 (knr-argdecl-intro . 5)
55 (substatement-open . +)
56 (label . 0)
57 (statement-case-open . +)
58 (statement-cont . +)
59 (arglist-intro . c-lineup-arglist-intro-after-paren)
60 (arglist-close . c-lineup-arglist)
0ec8351b 61 (inline-open . 0)
6cfd56b3 62 (brace-list-open . +)
785eecbb
RS
63 ))
64 (c-special-indent-hook . c-gnu-impose-minimum)
51f606de 65 (c-block-comment-prefix . "")
785eecbb
RS
66 )
67 ("k&r"
68 (c-basic-offset . 5)
69 (c-comment-only-line-offset . 0)
70 (c-offsets-alist . ((statement-block-intro . +)
71 (knr-argdecl-intro . 0)
72 (substatement-open . 0)
73 (label . 0)
74 (statement-cont . +)
75 ))
76 )
77 ("bsd"
6cfd56b3 78 (c-basic-offset . 8)
785eecbb
RS
79 (c-comment-only-line-offset . 0)
80 (c-offsets-alist . ((statement-block-intro . +)
81 (knr-argdecl-intro . +)
82 (substatement-open . 0)
83 (label . 0)
84 (statement-cont . +)
51f606de
GM
85 (inline-open . 0)
86 (inexpr-class . 0)
785eecbb
RS
87 ))
88 )
89 ("stroustrup"
90 (c-basic-offset . 4)
91 (c-comment-only-line-offset . 0)
92 (c-offsets-alist . ((statement-block-intro . +)
93 (substatement-open . 0)
94 (label . 0)
95 (statement-cont . +)
96 ))
97 )
98 ("whitesmith"
99 (c-basic-offset . 4)
100 (c-comment-only-line-offset . 0)
51f606de 101 (c-offsets-alist . ((knr-argdecl-intro . +)
785eecbb
RS
102 (label . 0)
103 (statement-cont . +)
51f606de
GM
104 (substatement-open . +)
105 (block-open . +)
106 (statement-block-intro . c-lineup-whitesmith-in-block)
107 (block-close . c-lineup-whitesmith-in-block)
108 (inline-open . +)
109 (defun-open . +)
110 (defun-block-intro . c-lineup-whitesmith-in-block)
111 (defun-close . c-lineup-whitesmith-in-block)
112 (brace-list-open . +)
113 (brace-list-intro . c-lineup-whitesmith-in-block)
114 (brace-entry-open . c-indent-multi-line-block)
115 (brace-list-close . c-lineup-whitesmith-in-block)
116 (class-open . +)
117 (inclass . c-lineup-whitesmith-in-block)
118 (class-close . +)
119 (inexpr-class . 0)
120 (extern-lang-open . +)
121 (inextern-lang . c-lineup-whitesmith-in-block)
122 (extern-lang-close . +)
123 (namespace-open . +)
124 (innamespace . c-lineup-whitesmith-in-block)
125 (namespace-close . +)
785eecbb 126 ))
785eecbb
RS
127 )
128 ("ellemtel"
129 (c-basic-offset . 3)
130 (c-comment-only-line-offset . 0)
131 (c-hanging-braces-alist . ((substatement-open before after)))
132 (c-offsets-alist . ((topmost-intro . 0)
133 (topmost-intro-cont . 0)
134 (substatement . +)
135 (substatement-open . 0)
136 (case-label . +)
137 (access-label . -)
138 (inclass . ++)
139 (inline-open . 0)
140 ))
141 )
142 ("linux"
143 (c-basic-offset . 8)
144 (c-comment-only-line-offset . 0)
145 (c-hanging-braces-alist . ((brace-list-open)
0ec8351b 146 (brace-entry-open)
785eecbb
RS
147 (substatement-open after)
148 (block-close . c-snug-do-while)))
149 (c-cleanup-list . (brace-else-brace))
150 (c-offsets-alist . ((statement-block-intro . +)
151 (knr-argdecl-intro . 0)
152 (substatement-open . 0)
153 (label . 0)
154 (statement-cont . +)
155 ))
156 )
157 ("python"
158 (indent-tabs-mode . t)
51f606de 159 (fill-column . 78)
785eecbb
RS
160 (c-basic-offset . 8)
161 (c-offsets-alist . ((substatement-open . 0)
62971612
RS
162 (inextern-lang . 0)
163 (arglist-intro . +)
164 (knr-argdecl-intro . +)
785eecbb
RS
165 ))
166 (c-hanging-braces-alist . ((brace-list-open)
167 (brace-list-intro)
168 (brace-list-close)
0ec8351b 169 (brace-entry-open)
785eecbb
RS
170 (substatement-open after)
171 (block-close . c-snug-do-while)
172 ))
51f606de 173 (c-block-comment-prefix . "")
785eecbb
RS
174 )
175 ("java"
0bacd8d0 176 (c-basic-offset . 4)
785eecbb 177 (c-comment-only-line-offset . (0 . 0))
63add9c9 178 ;; the following preserves Javadoc starter lines
0bacd8d0
RS
179 (c-offsets-alist . ((inline-open . 0)
180 (topmost-intro-cont . +)
785eecbb
RS
181 (statement-block-intro . +)
182 (knr-argdecl-intro . 5)
183 (substatement-open . +)
51f606de 184 (label . +)
785eecbb
RS
185 (statement-case-open . +)
186 (statement-cont . +)
187 (arglist-intro . c-lineup-arglist-intro-after-paren)
188 (arglist-close . c-lineup-arglist)
189 (access-label . 0)
190 (inher-cont . c-lineup-java-inher)
191 (func-decl-cont . c-lineup-java-throws)
192 ))
785eecbb
RS
193 )
194 )
195 "Styles of indentation.
196Elements of this alist are of the form:
197
198 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
199
200where STYLE-STRING is a short descriptive string used to select a
201style, VARIABLE is any Emacs variable, and VALUE is the intended value
202for that variable when using the selected style.
203
204Optional BASE-STYLE if present, is a string and must follow
205STYLE-STRING. BASE-STYLE names a style that this style inherits from.
51f606de 206By default, all styles inherit from the \"user\" style, which is
785eecbb
RS
207computed at run time. Style loops generate errors.
208
209Two variables are treated specially. When VARIABLE is
210`c-offsets-alist', the VALUE is a list containing elements of the
211form:
212
213 (SYNTACTIC-SYMBOL . OFFSET)
214
215as described in `c-offsets-alist'. These are passed directly to
216`c-set-offset' so there is no need to set every syntactic symbol in
217your style, only those that are different from the default.
218
219When VARIABLE is `c-special-indent-hook', its VALUE is added to
220`c-special-indent-hook' using `add-hook'. If VALUE is a list, each
221element of the list is added with `add-hook'.
222
223Do not change this variable directly. Use the function `c-add-style'
224to add new styles or modify existing styles (it is not a good idea to
225modify existing styles -- you should create a new style that inherits
226the existing style.")
227
51f606de 228
785eecbb
RS
229\f
230;; Functions that manipulate styles
51f606de 231(defun c-set-style-1 (conscell dont-override)
785eecbb
RS
232 ;; Set the style for one variable
233 (let ((attr (car conscell))
234 (val (cdr conscell)))
235 (cond
236 ;; first special variable
237 ((eq attr 'c-offsets-alist)
238 (mapcar
239 (function
240 (lambda (langentry)
241 (let ((langelem (car langentry))
242 (offset (cdr langentry)))
51f606de
GM
243 (unless (and dont-override
244 (assq langelem c-offsets-alist))
245 (c-set-offset langelem offset))
785eecbb 246 )))
51f606de 247 (if dont-override (reverse val) val)))
785eecbb
RS
248 ;; second special variable
249 ((eq attr 'c-special-indent-hook)
51f606de
GM
250 (let ((add-func (if dont-override
251 (lambda (func)
252 (unless (memq func c-special-indent-hook)
253 (add-hook 'c-special-indent-hook func t)))
254 (lambda (func)
255 (add-hook 'c-special-indent-hook func)))))
256 (if (listp val)
257 (mapcar add-func (if dont-override (reverse val) val))
258 (funcall add-func val))))
785eecbb 259 ;; all other variables
51f606de
GM
260 (t (if (or (not dont-override)
261 (not (memq attr c-style-variables))
262 (eq (symbol-value attr) 'set-from-style))
263 (set attr val))))
785eecbb
RS
264 ))
265
51f606de
GM
266(defun c-get-style-variables (style basestyles)
267 ;; Return all variables in a style by resolving inheritances.
785eecbb
RS
268 (let ((vars (cdr (or (assoc (downcase style) c-style-alist)
269 (assoc (upcase style) c-style-alist)
270 (assoc style c-style-alist)
271 (error "Undefined style: %s" style)))))
51f606de
GM
272 (if (string-equal style "user")
273 (copy-alist vars)
274 (let ((base (if (stringp (car vars))
275 (prog1
276 (downcase (car vars))
277 (setq vars (cdr vars)))
278 "user")))
279 (if (memq base basestyles)
280 (error "Style loop detected: %s in %s" base basestyles))
281 (nconc (c-get-style-variables base (cons base basestyles))
282 (copy-alist vars))))))
283
785eecbb
RS
284(defvar c-set-style-history nil)
285
286;;;###autoload
51f606de 287(defun c-set-style (stylename &optional dont-override)
785eecbb
RS
288 "Set CC Mode variables to use one of several different indentation styles.
289STYLENAME is a string representing the desired style from the list of
290styles described in the variable `c-style-alist'. See that variable
291for details of setting up styles.
292
293The variable `c-indentation-style' always contains the buffer's current
51f606de
GM
294style name.
295
296If the optional argument DONT-OVERRIDE is non-nil, no style variables
297that already have values will be overridden. I.e. in the case of
298`c-offsets-alist', syntactic symbols will only be added, and in the
299case of all other style variables, only those set to `set-from-style'
300will be reassigned.
301
302Obviously, specifying DONT-OVERRIDE is useful mainly when the initial
303style is chosen for a CC Mode buffer by a major mode. Since this is
304done internally by CC Mode, there's hardly ever a reason to use it."
785eecbb
RS
305 (interactive (list (let ((completion-ignore-case t)
306 (prompt (format "Which %s indentation style? "
307 mode-name)))
308 (completing-read prompt c-style-alist nil t
7c2ef733
RS
309 nil
310 'c-set-style-history
311 c-indentation-style))))
63add9c9 312 (c-initialize-builtin-style)
51f606de
GM
313 (let ((vars (c-get-style-variables stylename nil)))
314 (mapcar (lambda (elem)
315 (c-set-style-1 elem dont-override))
316 ;; Need to go through the variables backwards when we
317 ;; don't override.
318 (if dont-override (nreverse vars) vars)))
785eecbb
RS
319 (setq c-indentation-style stylename)
320 (c-keep-region-active))
321
322;;;###autoload
323(defun c-add-style (style descrip &optional set-p)
324 "Adds a style to `c-style-alist', or updates an existing one.
325STYLE is a string identifying the style to add or update. DESCRIP is
326an association list describing the style and must be of the form:
327
328 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
329
330See the variable `c-style-alist' for the semantics of BASESTYLE,
331VARIABLE and VALUE. This function also sets the current style to
332STYLE using `c-set-style' if the optional SET-P flag is non-nil."
333 (interactive
334 (let ((stylename (completing-read "Style to add: " c-style-alist
335 nil nil nil 'c-set-style-history))
336 (description (eval-minibuffer "Style description: ")))
337 (list stylename description
338 (y-or-n-p "Set the style too? "))))
339 (setq style (downcase style))
340 (let ((s (assoc style c-style-alist)))
341 (if s
342 (setcdr s (copy-alist descrip)) ; replace
343 (setq c-style-alist (cons (cons style descrip) c-style-alist))))
344 (and set-p (c-set-style style)))
345
346
347\f
785eecbb
RS
348(defvar c-read-offset-history nil)
349
350(defun c-read-offset (langelem)
351 ;; read new offset value for LANGELEM from minibuffer. return a
352 ;; legal value only
51f606de
GM
353 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist)
354 (assq langelem (get 'c-offsets-alist
355 'c-stylevar-fallback)))))
0ec8351b
BW
356 (symname (symbol-name langelem))
357 (defstr (format "(default %s): " oldoff))
130c507e 358 (errmsg (concat "Offset must be int, func, var, vector, list, "
0ec8351b
BW
359 "or [+,-,++,--,*,/] "
360 defstr))
361 (prompt (concat symname " offset " defstr))
785eecbb
RS
362 offset input interned raw)
363 (while (not offset)
364 (setq input (completing-read prompt obarray 'fboundp nil nil
365 'c-read-offset-history)
366 offset (cond ((string-equal "" input) oldoff) ; default
367 ((string-equal "+" input) '+)
368 ((string-equal "-" input) '-)
369 ((string-equal "++" input) '++)
370 ((string-equal "--" input) '--)
371 ((string-equal "*" input) '*)
372 ((string-equal "/" input) '/)
373 ((string-match "^-?[0-9]+$" input)
374 (string-to-int input))
375 ;; a symbol with a function binding
376 ((fboundp (setq interned (intern input)))
377 interned)
785eecbb
RS
378 ;; a symbol with variable binding
379 ((boundp interned) interned)
130c507e
GM
380 ;; a lambda function or a vector
381 ((progn
382 (c-safe (setq raw (read input)))
383 (or (functionp raw)
384 (vectorp raw)))
385 raw)
785eecbb
RS
386 ;; error, but don't signal one, keep trying
387 ;; to read an input value
388 (t (ding)
389 (setq prompt errmsg)
390 nil))))
391 offset))
392
275a02d4 393;;;###autoload
51f606de 394(defun c-set-offset (symbol offset &optional ignored)
785eecbb
RS
395 "Change the value of a syntactic element symbol in `c-offsets-alist'.
396SYMBOL is the syntactic element symbol to change and OFFSET is the new
51f606de
GM
397offset for that syntactic element. The optional argument is not used
398and exists only for compatibility reasons."
785eecbb
RS
399 (interactive
400 (let* ((langelem
401 (intern (completing-read
402 (concat "Syntactic symbol to change"
403 (if current-prefix-arg " or add" "")
404 ": ")
405 (mapcar
406 #'(lambda (langelem)
407 (cons (format "%s" (car langelem)) nil))
51f606de 408 (get 'c-offsets-alist 'c-stylevar-fallback))
785eecbb
RS
409 nil (not current-prefix-arg)
410 ;; initial contents tries to be the last element
411 ;; on the syntactic analysis list for the current
412 ;; line
413 (let* ((syntax (c-guess-basic-syntax))
414 (len (length syntax))
415 (ic (format "%s" (car (nth (1- len) syntax)))))
416 (cons ic 0))
417 )))
418 (offset (c-read-offset langelem)))
419 (list langelem offset current-prefix-arg)))
420 ;; sanity check offset
51f606de 421 (unless (c-valid-offset offset)
130c507e
GM
422 (error (concat "Offset must be int, func, var, vector, list, "
423 "or in [+,-,++,--,*,/]: %s")
51f606de 424 offset))
785eecbb
RS
425 (let ((entry (assq symbol c-offsets-alist)))
426 (if entry
427 (setcdr entry offset)
51f606de 428 (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback))
785eecbb 429 (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist))
51f606de 430 (error "%s is not a valid syntactic symbol" symbol))))
785eecbb
RS
431 (c-keep-region-active))
432
0ec8351b 433
51f606de 434\f
785eecbb
RS
435(defun c-initialize-builtin-style ()
436 ;; Dynamically append the default value of most variables. This is
437 ;; crucial because future c-set-style calls will always reset the
438 ;; variables first to the `cc-mode' style before instituting the new
439 ;; style. Only do this once!
0ec8351b
BW
440 (unless (get 'c-initialize-builtin-style 'is-run)
441 (put 'c-initialize-builtin-style 'is-run t)
130c507e 442 ;;(c-initialize-cc-mode)
0ec8351b
BW
443 (or (assoc "cc-mode" c-style-alist)
444 (assoc "user" c-style-alist)
445 (progn
51f606de
GM
446 (c-add-style
447 "user"
448 (mapcar
449 (lambda (var)
450 (let ((val (symbol-value var)))
451 (cons var
452 (cond ((eq var 'c-offsets-alist)
453 (mapcar
454 (lambda (langentry)
455 (setq langentry (or (assq (car langentry) val)
456 langentry))
457 (cons (car langentry)
458 (cdr langentry)))
459 (get var 'c-stylevar-fallback)))
460 ((eq var 'c-special-indent-hook)
461 val)
462 (t
463 (if (eq val 'set-from-style)
464 (get var 'c-stylevar-fallback)
465 val))))))
466 c-style-variables))
467 (c-add-style "cc-mode" '("user"))))
0ec8351b
BW
468 (if c-style-variables-are-local-p
469 (c-make-styles-buffer-local))))
63add9c9 470
0bacd8d0 471(defun c-make-styles-buffer-local (&optional this-buf-only-p)
785eecbb 472 "Make all CC Mode style variables buffer local.
51f606de
GM
473If you edit primarily one style of C (or C++, Objective-C, Java, etc)
474code, you probably want style variables to be global. This is the
475default.
785eecbb 476
51f606de
GM
477If you edit many different styles of C (or C++, Objective-C, Java,
478etc) at the same time, you probably want the CC Mode style variables
479to be buffer local. If you do, it's advicable to set any CC Mode
480style variables in a hook function (e.g. off of `c-mode-common-hook'),
481instead of at the top level of your ~/.emacs file.
785eecbb
RS
482
483This function makes all the CC Mode style variables buffer local.
484Call it after CC Mode is loaded into your Emacs environment.
485Conversely, set the variable `c-style-variables-are-local-p' to t in
486your .emacs file, before CC Mode is loaded, and this function will be
0bacd8d0
RS
487automatically called when CC Mode is loaded.
488
489Optional argument, when non-nil, means use `make-local-variable'
490instead of `make-variable-buffer-local'."
785eecbb 491 ;; style variables
0bacd8d0
RS
492 (let ((func (if this-buf-only-p
493 'make-local-variable
494 'make-variable-buffer-local))
51f606de
GM
495 (varsyms (cons 'c-indentation-style (copy-alist c-style-variables))))
496 (delq 'c-special-indent-hook varsyms)
0bacd8d0
RS
497 (mapcar func varsyms)
498 ;; Hooks must be handled specially
499 (if this-buf-only-p
500 (make-local-hook 'c-special-indent-hook)
51f606de
GM
501 (make-variable-buffer-local 'c-special-indent-hook)
502 (setq c-style-variables-are-local-p t))
0bacd8d0
RS
503 ))
504
785eecbb
RS
505
506\f
130c507e 507(cc-provide 'cc-styles)
3afbc435 508
785eecbb 509;;; cc-styles.el ends here