1 ;;; cc-styles.el --- support for styles in CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2017 Free Software Foundation, Inc.
5 ;; Authors: 2004- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
)
41 (load "cc-bytecomp" nil t
)))
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
56 (c-comment-only-line-offset .
(0 .
0))
57 (c-hanging-braces-alist .
((substatement-open before after
)
58 (arglist-cont-nonempty)))
59 (c-offsets-alist .
((statement-block-intro .
+)
60 (knr-argdecl-intro .
5)
61 (substatement-open .
+)
62 (substatement-label .
0)
64 (statement-case-open .
+)
66 (arglist-intro . c-lineup-arglist-intro-after-paren
)
67 (arglist-close . c-lineup-arglist
)
70 (brace-list-intro . c-lineup-arglist-intro-after-paren
)
72 .
(first c-lineup-topmost-intro-cont
73 c-lineup-gnu-DEFUN-intro-cont
))))
74 (c-special-indent-hook . c-gnu-impose-minimum
)
75 (c-block-comment-prefix .
""))
79 (c-comment-only-line-offset .
0)
80 (c-offsets-alist .
((statement-block-intro .
+)
81 (knr-argdecl-intro .
0)
82 (substatement-open .
0)
83 (substatement-label .
0)
85 (statement-cont .
+))))
89 (c-comment-only-line-offset .
0)
90 (c-offsets-alist .
((statement-block-intro .
+)
91 (knr-argdecl-intro .
+)
92 (substatement-open .
0)
93 (substatement-label .
0)
101 (c-comment-only-line-offset .
0)
102 (c-offsets-alist .
((statement-block-intro .
+)
103 (substatement-open .
0)
104 (substatement-label .
0)
106 (statement-cont .
+))))
110 (c-comment-only-line-offset .
0)
111 ;; It's obvious that the CC Mode way of choosing anchor positions
112 ;; doesn't fit this style at all. :P
113 (c-offsets-alist .
((defun-open .
+)
114 (defun-close . c-lineup-whitesmith-in-block
)
115 (defun-block-intro .
(add c-lineup-whitesmith-in-block
116 c-indent-multi-line-block
))
120 (inline-close . c-lineup-whitesmith-in-block
)
121 (knr-argdecl-intro .
+)
122 (block-open .
0) ; Get indentation from `statement' instead.
123 (block-close . c-lineup-whitesmith-in-block
)
124 (brace-list-open .
+)
125 (brace-list-close . c-lineup-whitesmith-in-block
)
126 (brace-list-intro .
(add c-lineup-whitesmith-in-block
127 c-indent-multi-line-block
))
128 (brace-list-entry .
(add c-lineup-after-whitesmith-blocks
129 c-indent-multi-line-block
))
130 (brace-entry-open .
(add c-lineup-after-whitesmith-blocks
131 c-indent-multi-line-block
))
132 (statement .
(add c-lineup-after-whitesmith-blocks
133 c-indent-multi-line-block
))
134 (statement-block-intro .
(add c-lineup-whitesmith-in-block
135 c-indent-multi-line-block
))
136 (substatement-open .
+)
137 (substatement-label .
+)
139 (arglist-intro .
(add c-lineup-whitesmith-in-block
140 c-indent-multi-line-block
))
141 (arglist-cont .
(add c-lineup-after-whitesmith-blocks
142 c-indent-multi-line-block
))
143 (arglist-cont-nonempty .
(add c-lineup-whitesmith-in-block
144 c-indent-multi-line-block
))
145 (arglist-close . c-lineup-whitesmith-in-block
)
146 (inclass . c-lineup-whitesmith-in-block
)
147 (extern-lang-open .
+)
150 (composition-open .
+)
151 (extern-lang-close .
+)
152 (namespace-close .
+)
154 (composition-close .
+)
155 (inextern-lang . c-lineup-whitesmith-in-block
)
156 (innamespace . c-lineup-whitesmith-in-block
)
157 (inmodule . c-lineup-whitesmith-in-block
)
158 (incomposition . c-lineup-whitesmith-in-block
)
159 (inexpr-class .
0))))
163 (c-comment-only-line-offset .
0)
164 (c-hanging-braces-alist .
((substatement-open before after
)
165 (arglist-cont-nonempty)))
166 (c-offsets-alist .
((topmost-intro .
0)
168 (substatement-open .
0)
175 (c-comment-only-line-offset .
0)
176 (c-hanging-braces-alist .
((brace-list-open)
178 (substatement-open after
)
179 (block-close . c-snug-do-while
)
180 (arglist-cont-nonempty)))
181 (c-cleanup-list .
(brace-else-brace))
182 (c-offsets-alist .
((statement-block-intro .
+)
183 (knr-argdecl-intro .
0)
184 (substatement-open .
0)
185 (substatement-label .
0)
187 (statement-cont .
+))))
190 (indent-tabs-mode . t
)
193 (c-offsets-alist .
((substatement-open .
0)
196 (knr-argdecl-intro .
+)))
197 (c-hanging-braces-alist .
((brace-list-open)
201 (substatement-open after
)
202 (block-close . c-snug-do-while
)
203 (arglist-cont-nonempty)))
204 (c-block-comment-prefix .
""))
208 (c-comment-only-line-offset .
(0 .
0))
209 ;; the following preserves Javadoc starter lines
210 (c-offsets-alist .
((inline-open .
0)
211 (topmost-intro-cont .
+)
212 (statement-block-intro .
+)
213 (knr-argdecl-intro .
5)
214 (substatement-open .
+)
215 (substatement-label .
+)
217 (statement-case-open .
+)
219 (arglist-intro . c-lineup-arglist-intro-after-paren
)
220 (arglist-close . c-lineup-arglist
)
222 (inher-cont . c-lineup-java-inher
)
223 (func-decl-cont . c-lineup-java-throws
))))
225 ;; awk style exists primarily for auto-newline settings. Otherwise it's
226 ;; pretty much like k&r.
229 (c-comment-only-line-offset .
0)
230 (c-hanging-braces-alist .
((defun-open after
)
231 (defun-close . c-snug-1line-defun-close
)
232 (substatement-open after
)
233 (block-close . c-snug-do-while
)
234 (arglist-cont-nonempty)))
235 (c-hanging-semi&comma-criteria . nil
)
236 (c-cleanup-list . nil
) ; You might want one-liner-defun here.
237 (c-offsets-alist .
((statement-block-intro .
+)
238 (substatement-open .
0)
239 (statement-cont .
+))))
242 "Styles of indentation.
243 Elements of this alist are of the form:
245 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
247 where STYLE-STRING is a short descriptive string used to select a
248 style, VARIABLE is any Emacs variable, and VALUE is the intended value
249 for that variable when using the selected style.
251 Optional BASE-STYLE if present, is a string and must follow
252 STYLE-STRING. BASE-STYLE names a style that this style inherits from.
253 By default, all styles inherit from the \"user\" style, which is
254 computed at run time. Style loops generate errors.
256 Two variables are treated specially. When VARIABLE is
257 `c-offsets-alist', the VALUE is a list containing elements of the
260 (SYNTACTIC-SYMBOL . OFFSET)
262 as described in `c-offsets-alist'. These are passed directly to
263 `c-set-offset' so there is no need to set every syntactic symbol in
264 your style, only those that are different from the default.
266 When VARIABLE is `c-special-indent-hook', its VALUE is added to
267 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each
268 element of the list is added with `add-hook'.
270 Do not change this variable directly. Use the function `c-add-style'
271 to add new styles or modify existing styles (it is not a good idea to
272 modify existing styles -- you should create a new style that inherits
273 the existing style).")
276 ;; Functions that manipulate styles
277 (defun c-set-style-1 (conscell dont-override
)
278 ;; Set the style for one variable
279 (let ((attr (car conscell
))
280 (val (cdr conscell
)))
282 ;; first special variable
283 ((eq attr
'c-offsets-alist
)
284 (let ((offsets (cond ((eq dont-override t
)
287 (default-value 'c-offsets-alist
)))))
288 (mapcar (lambda (langentry)
289 (let ((langelem (car langentry
))
290 (offset (cdr langentry
)))
291 (unless (assq langelem offsets
)
292 (c-set-offset langelem offset
))))
294 ;; second special variable
295 ((eq attr
'c-special-indent-hook
)
296 ;; Maybe we should ignore dont-override here and always add new
298 (unless (cond ((eq dont-override t
)
299 c-special-indent-hook
)
301 (default-value 'c-special-indent-hook
)))
303 (mapcar (lambda (func)
304 (add-hook 'c-special-indent-hook func t t
))
306 (add-hook 'c-special-indent-hook val t t
))))
307 ;; all other variables
308 (t (when (or (not dont-override
)
309 (not (memq attr c-style-variables
))
310 (eq (if (eq dont-override t
)
312 (default-value attr
))
315 ;; Must update a number of other variables if
316 ;; c-comment-prefix-regexp is set.
317 (if (eq attr
'c-comment-prefix-regexp
)
318 (c-setup-paragraph-variables)))))))
320 (defun c-get-style-variables (style basestyles
)
321 ;; Return all variables in a style by resolving inheritances.
323 (copy-alist c-fallback-style
)
324 (let ((vars (cdr (or (assoc (downcase style
) c-style-alist
)
325 (assoc (upcase style
) c-style-alist
)
326 (assoc style c-style-alist
)
328 (c-benign-error "Undefined style: %s" style
)
330 (let ((base (and (stringp (car-safe vars
))
332 (downcase (car vars
))
333 (setq vars
(cdr vars
))))))
334 (if (memq base basestyles
)
335 (c-benign-error "Style loop detected: %s in %s" base basestyles
)
336 (nconc (c-get-style-variables base
(cons base basestyles
))
337 (copy-alist vars
)))))))
339 (defvar c-set-style-history nil
)
342 (defun c-set-style (stylename &optional dont-override
)
343 "Set the current buffer to use the style STYLENAME.
344 STYLENAME, a string, must be an existing CC Mode style - These are contained
345 in the variable `c-style-alist'.
347 The variable `c-indentation-style' will get set to STYLENAME.
349 \"Setting the style\" is done by setting CC Mode's \"style variables\" to the
350 values indicated by the pertinent entry in `c-style-alist'. Other variables
353 If DONT-OVERRIDE is neither nil nor t, style variables whose default values
354 have been set (more precisely, whose default values are not the symbol
355 `set-from-style') will not be changed. This avoids overriding global settings
356 done in your init file. It is useful to call c-set-style from a mode hook
359 If DONT-OVERRIDE is t, style variables that already have values (i.e., whose
360 values are not the symbol `set-from-style') will not be overridden. CC Mode
361 calls c-set-style internally in this way whilst initializing a buffer; if
362 cc-set-style is called like this from anywhere else, it will usually behave as
365 (list (let ((completion-ignore-case t
)
366 (prompt (format "Which %s indentation style? "
368 (completing-read prompt c-style-alist nil t nil
370 c-indentation-style
))))
371 (or c-buffer-is-cc-mode
372 (error "Buffer %s is not a CC Mode buffer (c-set-style)" (buffer-name)))
373 (or (stringp stylename
)
374 (error "Argument to c-set-style was not a string"))
375 (c-initialize-builtin-style)
376 (let ((vars (c-get-style-variables stylename nil
)))
377 (unless dont-override
378 ;; Since we always add to c-special-indent-hook we must reset it
379 ;; first, or else the hooks from the preceding style will
380 ;; remain. This is not necessary for c-offsets-alist, since
381 ;; c-get-style-variables contains every valid offset type in the
383 (setq c-special-indent-hook
384 (default-value 'c-special-indent-hook
)))
386 (c-set-style-1 elem dont-override
))
387 ;; Need to go through the variables backwards when we
388 ;; don't override any settings.
389 (if (eq dont-override t
) (nreverse vars
) vars
)))
390 (setq c-indentation-style stylename
)
391 (c-keep-region-active))
394 (defun c-add-style (style description
&optional set-p
)
395 "Adds a style to `c-style-alist', or updates an existing one.
396 STYLE is a string identifying the style to add or update. DESCRIPTION
397 is an association list describing the style and must be of the form:
399 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])
401 See the variable `c-style-alist' for the semantics of BASESTYLE,
402 VARIABLE and VALUE. This function also sets the current style to
403 STYLE using `c-set-style' if the optional SET-P flag is non-nil."
405 (let ((stylename (completing-read "Style to add: " c-style-alist
406 nil nil nil
'c-set-style-history
))
407 (descr (eval-minibuffer "Style description: ")))
408 (list stylename descr
409 (y-or-n-p "Set the style too? "))))
410 (setq style
(downcase style
))
411 (let ((s (assoc style c-style-alist
)))
413 (setcdr s
(copy-alist description
)) ; replace
414 (setq c-style-alist
(cons (cons style description
) c-style-alist
))))
415 (and set-p
(c-set-style style
)))
418 (defvar c-read-offset-history nil
)
420 (defun c-read-offset (langelem)
421 ;; read new offset value for LANGELEM from minibuffer. return a
423 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist
)
424 (assq langelem
(get 'c-offsets-alist
425 'c-stylevar-fallback
)))))
426 (symname (symbol-name langelem
))
427 (defstr (format "(default %s): " oldoff
))
428 (errmsg (concat "Offset must be int, func, var, vector, list, "
429 "or [+,-,++,--,*,/] "
431 (prompt (concat symname
" offset " defstr
))
432 (keymap (make-sparse-keymap))
433 (minibuffer-completion-table obarray
)
434 (minibuffer-completion-predicate 'fboundp
)
436 ;; In principle completing-read is used here, but SPC is unbound
437 ;; to make it less annoying to enter lists.
438 (set-keymap-parent keymap minibuffer-local-completion-map
)
439 (define-key keymap
" " 'self-insert-command
)
441 (setq input
(read-from-minibuffer prompt nil keymap t
442 'c-read-offset-history
443 (format "%s" oldoff
)))
444 (if (c-valid-offset input
)
446 ;; error, but don't signal one, keep trying
447 ;; to read an input value
449 (setq prompt errmsg
)))
453 (defun c-set-offset (symbol offset
&optional ignored
)
454 "Change the value of a syntactic element symbol in `c-offsets-alist'.
455 SYMBOL is the syntactic element symbol to change and OFFSET is the new
456 offset for that syntactic element. The optional argument is not used
457 and exists only for compatibility reasons."
460 (intern (completing-read
461 (concat "Syntactic symbol to change"
462 (if current-prefix-arg
" or add" "")
466 (cons (format "%s" (car langelem
)) nil
))
467 (get 'c-offsets-alist
'c-stylevar-fallback
))
468 nil
(not current-prefix-arg
)
469 ;; initial contents tries to be the last element
470 ;; on the syntactic analysis list for the current
472 (and c-buffer-is-cc-mode
474 ((syntax (c-guess-basic-syntax))
475 (len (length syntax
))
476 (ic (format "%s" (car (nth (1- len
) syntax
)))))
479 (offset (c-read-offset langelem
)))
480 (list langelem offset current-prefix-arg
)))
481 ;; sanity check offset
482 (if (c-valid-offset offset
)
483 (let ((entry (assq symbol c-offsets-alist
)))
485 (setcdr entry offset
)
486 (if (assq symbol
(get 'c-offsets-alist
'c-stylevar-fallback
))
487 (setq c-offsets-alist
(cons (cons symbol offset
)
489 (c-benign-error "%s is not a valid syntactic symbol" symbol
))))
490 (c-benign-error "Invalid indentation setting for symbol %s: %S"
492 (c-keep-region-active))
495 (defun c-setup-paragraph-variables ()
496 "Fix things up for paragraph recognition and filling inside comments and
497 strings by incorporating the values of `c-comment-prefix-regexp',
498 `sentence-end', `paragraph-start' and `paragraph-separate' in the relevant
502 (or c-buffer-is-cc-mode
503 (error "Buffer %s is not a CC Mode buffer (c-setup-paragraph-variables)"
505 ;; Set up the values for use in comments.
506 (setq c-current-comment-prefix
507 (if (listp c-comment-prefix-regexp
)
508 (cdr-safe (or (assoc major-mode c-comment-prefix-regexp
)
509 (assoc 'other c-comment-prefix-regexp
)))
510 c-comment-prefix-regexp
))
512 (let* ((empty-is-prefix (string-match c-current-comment-prefix
""))
513 (nonws-comment-line-prefix
514 (concat "\\(" c-current-comment-prefix
"\\)[ \t]*"))
515 (comment-line-prefix (concat "[ \t]*" nonws-comment-line-prefix
))
516 (blank-or-comment-line-prefix
518 (if empty-is-prefix
"" "\\(")
519 nonws-comment-line-prefix
520 (if empty-is-prefix
"" "\\)?"))))
522 (setq paragraph-start
(concat blank-or-comment-line-prefix
526 paragraph-separate
(concat blank-or-comment-line-prefix
530 paragraph-ignore-fill-prefix t
533 (concat comment-line-prefix
534 (if (default-value 'adaptive-fill-regexp
)
536 (default-value 'adaptive-fill-regexp
)
540 (when (boundp 'adaptive-fill-first-line-regexp
)
541 ;; XEmacs adaptive fill mode doesn't have this.
542 (set (make-local-variable 'adaptive-fill-first-line-regexp
)
543 (concat "\\`" comment-line-prefix
544 ;; Maybe we should incorporate the old value here,
545 ;; but then we have to do all sorts of kludges to
546 ;; deal with the \` and \' it probably contains.
549 ;; Set up the values for use in strings. These are the default
550 ;; paragraph-start/separate values, enhanced to accept escaped EOLs as
551 ;; whitespace. Used in c-beginning/end-of-sentence-in-string in cc-cmds.
552 (setq c-string-par-start
553 ;;(concat "\\(" (default-value 'paragraph-start) "\\)\\|[ \t]*\\\\$"))
555 (setq c-string-par-separate
556 ;;(concat "\\(" (default-value 'paragraph-separate) "\\)\\|[ \t]*\\\\$"))
558 (setq c-sentence-end-with-esc-eol
559 (concat "\\(\\(" (c-default-value-sentence-end) "\\)"
560 ;; N.B.: "$" would be invalid when not enclosed like "\\($\\)".
561 "\\|" "[.?!][]\"')}]* ?\\\\\\($\\)[ \t\n]*"
565 ;; Helper for setting up Filladapt mode. It's not used by CC Mode itself.
567 (cc-bytecomp-defvar filladapt-token-table
)
568 (cc-bytecomp-defvar filladapt-token-match-table
)
569 (cc-bytecomp-defvar filladapt-token-conversion-table
)
571 (defun c-setup-filladapt ()
572 "Convenience function to configure Kyle E. Jones' Filladapt mode for
573 CC Mode by making sure the proper entries are present on
574 `filladapt-token-table', `filladapt-token-match-table', and
575 `filladapt-token-conversion-table'. This is intended to be used on
576 `c-mode-common-hook' or similar."
577 ;; This function is intended to be used explicitly by the end user
580 ;; The default configuration already handles C++ comments, but we
581 ;; need to add handling of C block comments. A new filladapt token
582 ;; `c-comment' is added for that.
584 (setq p filladapt-token-table
)
585 (while (and p
(not (eq (car-safe (cdr-safe (car-safe p
))) 'c-comment
)))
586 (setq p
(cdr-safe p
)))
588 (setcar (car p
) c-current-comment-prefix
)
589 (setq filladapt-token-table
590 (append (list (car filladapt-token-table
)
591 (list c-current-comment-prefix
'c-comment
))
592 (cdr filladapt-token-table
)))))
593 (unless (assq 'c-comment filladapt-token-match-table
)
594 (setq filladapt-token-match-table
595 (append '((c-comment c-comment
))
596 filladapt-token-match-table
)))
597 (unless (assq 'c-comment filladapt-token-conversion-table
)
598 (setq filladapt-token-conversion-table
599 (append '((c-comment . exact
))
600 filladapt-token-conversion-table
))))
603 (defun c-initialize-builtin-style ()
604 ;; Dynamically append the default value of most variables. This is
605 ;; crucial because future c-set-style calls will always reset the
606 ;; variables first to the `cc-mode' style before instituting the new
607 ;; style. Only do this once!
608 (unless (get 'c-initialize-builtin-style
'is-run
)
609 (put 'c-initialize-builtin-style
'is-run t
)
610 ;;(c-initialize-cc-mode)
611 (unless (assoc "user" c-style-alist
)
612 (let ((vars c-style-variables
) var val uservars
)
615 val
(symbol-value var
)
617 (cond ((eq var
'c-offsets-alist
)
619 (setq uservars
(cons (cons 'c-offsets-alist val
)
621 ((not (eq val
'set-from-style
))
622 (setq uservars
(cons (cons var val
)
624 (c-add-style "user" uservars
)))
625 (unless (assoc "cc-mode" c-style-alist
)
626 (c-add-style "cc-mode" '("user")))
627 (if c-style-variables-are-local-p
628 (c-make-styles-buffer-local))))
630 (defun c-make-styles-buffer-local (&optional this-buf-only-p
)
631 "Make all CC Mode style variables buffer local.
632 If `this-buf-only-p' is non-nil, the style variables will be made
633 buffer local only in the current buffer. Otherwise they'll be made
634 permanently buffer local in any buffer that changes their values.
636 The buffer localness of the style variables are normally controlled
637 with the variable `c-style-variables-are-local-p', so there's seldom
638 any reason to call this function directly."
641 (let ((func (if this-buf-only-p
643 'make-variable-buffer-local
))
644 (varsyms (cons 'c-indentation-style
(copy-alist c-style-variables
))))
645 (delq 'c-special-indent-hook varsyms
)
647 ;; Hooks must be handled specially
649 (if (featurep 'xemacs
) (make-local-hook 'c-special-indent-hook
))
650 (with-no-warnings (make-variable-buffer-local 'c-special-indent-hook
))
651 (setq c-style-variables-are-local-p t
))
654 (defun cc-choose-style-for-mode (mode default-style
)
655 "Return suitable style for MODE from DEFAULT-STYLE.
656 DEFAULT-STYLE has the same format as `c-default-style'."
657 (if (stringp default-style
)
659 (or (cdr (assq mode default-style
))
660 (cdr (assq 'other default-style
))
665 (cc-provide 'cc-styles
)
668 ;; indent-tabs-mode: t
671 ;;; cc-styles.el ends here