]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-styles.el
(tcl-indent-for-comment): Ignore comment-indent-hook.
[gnu-emacs] / lisp / progmodes / cc-styles.el
1 ;;; cc-styles.el --- support for styles in CC Mode
2
3 ;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc.
4
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
13
14 ;; This file is part of GNU Emacs.
15
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.
20
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.
25
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.
30
31 (eval-when-compile
32 (let ((load-path
33 (if (and (boundp 'byte-compile-current-file)
34 (stringp byte-compile-current-file))
35 (cons (file-name-directory byte-compile-current-file)
36 load-path)
37 load-path)))
38 (load "cc-defs" nil t)))
39 (require 'cc-vars)
40
41
42 \f
43 ;; Warning: don't eval-defun this constant or you'll break style inheritance.
44 (defconst c-style-alist
45 '(("gnu"
46 (c-basic-offset . 2)
47 (c-comment-only-line-offset . (0 . 0))
48 (c-offsets-alist . ((statement-block-intro . +)
49 (knr-argdecl-intro . 5)
50 (substatement-open . +)
51 (label . 0)
52 (statement-case-open . +)
53 (statement-cont . +)
54 (arglist-intro . c-lineup-arglist-intro-after-paren)
55 (arglist-close . c-lineup-arglist)
56 (inline-open . 0)
57 ))
58 (c-special-indent-hook . c-gnu-impose-minimum)
59 (c-block-comment-prefix . "")
60 )
61 ("k&r"
62 (c-basic-offset . 5)
63 (c-comment-only-line-offset . 0)
64 (c-offsets-alist . ((statement-block-intro . +)
65 (knr-argdecl-intro . 0)
66 (substatement-open . 0)
67 (label . 0)
68 (statement-cont . +)
69 ))
70 )
71 ("bsd"
72 (c-basic-offset . 4)
73 (c-comment-only-line-offset . 0)
74 (c-offsets-alist . ((statement-block-intro . +)
75 (knr-argdecl-intro . +)
76 (substatement-open . 0)
77 (label . 0)
78 (statement-cont . +)
79 (inline-open . 0)
80 (inexpr-class . 0)
81 ))
82 )
83 ("stroustrup"
84 (c-basic-offset . 4)
85 (c-comment-only-line-offset . 0)
86 (c-offsets-alist . ((statement-block-intro . +)
87 (substatement-open . 0)
88 (label . 0)
89 (statement-cont . +)
90 ))
91 )
92 ("whitesmith"
93 (c-basic-offset . 4)
94 (c-comment-only-line-offset . 0)
95 (c-offsets-alist . ((knr-argdecl-intro . +)
96 (label . 0)
97 (statement-cont . +)
98 (substatement-open . +)
99 (block-open . +)
100 (statement-block-intro . c-lineup-whitesmith-in-block)
101 (block-close . c-lineup-whitesmith-in-block)
102 (inline-open . +)
103 (defun-open . +)
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)
110 (class-open . +)
111 (inclass . c-lineup-whitesmith-in-block)
112 (class-close . +)
113 (inexpr-class . 0)
114 (extern-lang-open . +)
115 (inextern-lang . c-lineup-whitesmith-in-block)
116 (extern-lang-close . +)
117 (namespace-open . +)
118 (innamespace . c-lineup-whitesmith-in-block)
119 (namespace-close . +)
120 ))
121 )
122 ("ellemtel"
123 (c-basic-offset . 3)
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)
128 (substatement . +)
129 (substatement-open . 0)
130 (case-label . +)
131 (access-label . -)
132 (inclass . ++)
133 (inline-open . 0)
134 ))
135 )
136 ("linux"
137 (c-basic-offset . 8)
138 (c-comment-only-line-offset . 0)
139 (c-hanging-braces-alist . ((brace-list-open)
140 (brace-entry-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)
147 (label . 0)
148 (statement-cont . +)
149 ))
150 )
151 ("python"
152 (indent-tabs-mode . t)
153 (fill-column . 78)
154 (c-basic-offset . 8)
155 (c-offsets-alist . ((substatement-open . 0)
156 (inextern-lang . 0)
157 (arglist-intro . +)
158 (knr-argdecl-intro . +)
159 ))
160 (c-hanging-braces-alist . ((brace-list-open)
161 (brace-list-intro)
162 (brace-list-close)
163 (brace-entry-open)
164 (substatement-open after)
165 (block-close . c-snug-do-while)
166 ))
167 (c-block-comment-prefix . "")
168 )
169 ("java"
170 (c-basic-offset . 4)
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 . +)
178 (label . +)
179 (statement-case-open . +)
180 (statement-cont . +)
181 (arglist-intro . c-lineup-arglist-intro-after-paren)
182 (arglist-close . c-lineup-arglist)
183 (access-label . 0)
184 (inher-cont . c-lineup-java-inher)
185 (func-decl-cont . c-lineup-java-throws)
186 ))
187 )
188 )
189 "Styles of indentation.
190 Elements of this alist are of the form:
191
192 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
193
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.
197
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.
202
203 Two variables are treated specially. When VARIABLE is
204 `c-offsets-alist', the VALUE is a list containing elements of the
205 form:
206
207 (SYNTACTIC-SYMBOL . OFFSET)
208
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.
212
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'.
216
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.")
221
222
223 \f
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)))
229 (cond
230 ;; first special variable
231 ((eq attr 'c-offsets-alist)
232 (mapcar
233 (function
234 (lambda (langentry)
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))
240 )))
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
245 (lambda (func)
246 (unless (memq func c-special-indent-hook)
247 (add-hook 'c-special-indent-hook func t)))
248 (lambda (func)
249 (add-hook 'c-special-indent-hook func)))))
250 (if (listp val)
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))
257 (set attr val))))
258 ))
259
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")
267 (copy-alist vars)
268 (let ((base (if (stringp (car vars))
269 (prog1
270 (downcase (car vars))
271 (setq vars (cdr vars)))
272 "user")))
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))))))
277
278 (defvar c-set-style-history nil)
279
280 ;;;###autoload
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.
286
287 The variable `c-indentation-style' always contains the buffer's current
288 style name.
289
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'
294 will be reassigned.
295
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? "
301 mode-name)))
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
310 ;; don't override.
311 (if dont-override (nreverse vars) vars)))
312 (setq c-indentation-style stylename)
313 (c-keep-region-active))
314
315 ;;;###autoload
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:
320
321 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
322
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."
326 (interactive
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)))
334 (if s
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)))
338
339
340 \f
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 -
344 (cond
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)))
352 ((listp offset)
353 (setq offset
354 (let (done)
355 (while (and (not done) offset)
356 (setq done (c-evaluate-offset (car offset) langelem symbol)
357 offset (cdr offset)))
358 (if (not done)
359 (if c-strict-syntax-p
360 (error "No offset found for syntactic symbol %s" symbol)
361 0)
362 done))))
363 ((not (numberp offset)) (setq offset (symbol-value offset)))
364 )
365 offset)
366
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)))
377 (if (not match)
378 (if c-strict-syntax-p
379 (error "No offset found for syntactic symbol %s" symbol)
380 (setq offset 0
381 relpos 0))
382 (setq offset (c-evaluate-offset offset langelem symbol)))
383 (+ (if (and relpos
384 (< relpos (c-point 'bol)))
385 (save-excursion
386 (goto-char relpos)
387 (current-column))
388 0)
389 (or (and (numberp offset) offset)
390 (and (symbolp offset) (symbol-value offset))
391 0))
392 ))
393
394
395 \f
396 (defvar c-read-offset-history nil)
397
398 (defun c-read-offset (langelem)
399 ;; read new offset value for LANGELEM from minibuffer. return a
400 ;; legal value only
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 [+,-,++,--,*,/] "
408 defstr))
409 (prompt (concat symname " offset " defstr))
410 offset input interned raw)
411 (while (not offset)
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)))
425 interned)
426 ;; a lambda function
427 ((c-safe (functionp (setq raw (read input))))
428 raw)
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
433 (t (ding)
434 (setq prompt errmsg)
435 nil))))
436 offset))
437
438 ;;;###autoload
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."
444 (interactive
445 (let* ((langelem
446 (intern (completing-read
447 (concat "Syntactic symbol to change"
448 (if current-prefix-arg " or add" "")
449 ": ")
450 (mapcar
451 #'(lambda (langelem)
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
457 ;; line
458 (let* ((syntax (c-guess-basic-syntax))
459 (len (length syntax))
460 (ic (format "%s" (car (nth (1- len) syntax)))))
461 (cons ic 0))
462 )))
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"
468 offset))
469 (let ((entry (assq symbol c-offsets-alist)))
470 (if entry
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))
476
477
478 \f
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)
489 (progn
490 (c-add-style
491 "user"
492 (mapcar
493 (lambda (var)
494 (let ((val (symbol-value var)))
495 (cons var
496 (cond ((eq var 'c-offsets-alist)
497 (mapcar
498 (lambda (langentry)
499 (setq langentry (or (assq (car langentry) val)
500 langentry))
501 (cons (car langentry)
502 (cdr langentry)))
503 (get var 'c-stylevar-fallback)))
504 ((eq var 'c-special-indent-hook)
505 val)
506 (t
507 (if (eq val 'set-from-style)
508 (get var 'c-stylevar-fallback)
509 val))))))
510 c-style-variables))
511 (c-add-style "cc-mode" '("user"))))
512 (if c-style-variables-are-local-p
513 (c-make-styles-buffer-local))))
514
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
519 default.
520
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.
526
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.
532
533 Optional argument, when non-nil, means use `make-local-variable'
534 instead of `make-variable-buffer-local'."
535 ;; style variables
536 (let ((func (if this-buf-only-p
537 'make-local-variable
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
543 (if this-buf-only-p
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))
547 ))
548
549
550 \f
551 (provide 'cc-styles)
552 ;;; cc-styles.el ends here