Merge branch 'master' into comment-cache
[emacs.git] / lisp / progmodes / cc-styles.el
blobb3848a74f97de6fa41f04d43db77ee79bebca6da
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
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
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
14 ;; Package: cc-mode
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/>.
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-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)
63 (label . 0)
64 (statement-case-open . +)
65 (statement-cont . +)
66 (arglist-intro . c-lineup-arglist-intro-after-paren)
67 (arglist-close . c-lineup-arglist)
68 (inline-open . 0)
69 (brace-list-open . +)
70 (brace-list-intro . c-lineup-arglist-intro-after-paren)
71 (topmost-intro-cont
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 . ""))
77 ("k&r"
78 (c-basic-offset . 5)
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)
84 (label . 0)
85 (statement-cont . +))))
87 ("bsd"
88 (c-basic-offset . 8)
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)
94 (label . 0)
95 (statement-cont . +)
96 (inline-open . 0)
97 (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 . +))))
108 ("whitesmith"
109 (c-basic-offset . 4)
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))
117 (class-open . +)
118 (class-close . +)
119 (inline-open . +)
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 . +)
138 (label . 0)
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 . +)
148 (namespace-open . +)
149 (module-open . +)
150 (composition-open . +)
151 (extern-lang-close . +)
152 (namespace-close . +)
153 (module-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))))
161 ("ellemtel"
162 (c-basic-offset . 3)
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)
167 (substatement . +)
168 (substatement-open . 0)
169 (case-label . +)
170 (access-label . -)
171 (inclass . +)
172 (inline-open . 0))))
173 ("linux"
174 (c-basic-offset . 8)
175 (c-comment-only-line-offset . 0)
176 (c-hanging-braces-alist . ((brace-list-open)
177 (brace-entry-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)
186 (label . 0)
187 (statement-cont . +))))
189 ("python"
190 (indent-tabs-mode . t)
191 (fill-column . 78)
192 (c-basic-offset . 8)
193 (c-offsets-alist . ((substatement-open . 0)
194 (inextern-lang . 0)
195 (arglist-intro . +)
196 (knr-argdecl-intro . +)))
197 (c-hanging-braces-alist . ((brace-list-open)
198 (brace-list-intro)
199 (brace-list-close)
200 (brace-entry-open)
201 (substatement-open after)
202 (block-close . c-snug-do-while)
203 (arglist-cont-nonempty)))
204 (c-block-comment-prefix . ""))
206 ("java"
207 (c-basic-offset . 4)
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 . +)
216 (label . +)
217 (statement-case-open . +)
218 (statement-cont . +)
219 (arglist-intro . c-lineup-arglist-intro-after-paren)
220 (arglist-close . c-lineup-arglist)
221 (access-label . 0)
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.
227 ("awk"
228 (c-basic-offset . 4)
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
258 form:
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)))
281 (cond
282 ;; first special variable
283 ((eq attr 'c-offsets-alist)
284 (let ((offsets (cond ((eq dont-override t)
285 c-offsets-alist)
286 (dont-override
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))))
293 val)))
294 ;; second special variable
295 ((eq attr 'c-special-indent-hook)
296 ;; Maybe we should ignore dont-override here and always add new
297 ;; hooks?
298 (unless (cond ((eq dont-override t)
299 c-special-indent-hook)
300 (dont-override
301 (default-value 'c-special-indent-hook)))
302 (if (listp val)
303 (mapcar (lambda (func)
304 (add-hook 'c-special-indent-hook func t t))
305 val)
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)
311 (symbol-value attr)
312 (default-value attr))
313 'set-from-style))
314 (set attr val)
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.
322 (if (not style)
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)
327 (progn
328 (c-benign-error "Undefined style: %s" style)
329 nil)))))
330 (let ((base (and (stringp (car-safe vars))
331 (prog1
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)
341 ;;;###autoload
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
351 might get set too.
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
357 in this way.
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
363 a null operation."
364 (interactive
365 (list (let ((completion-ignore-case t)
366 (prompt (format "Which %s indentation style? "
367 mode-name)))
368 (completing-read prompt c-style-alist nil t nil
369 'c-set-style-history
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
382 ;; fallback entry.
383 (setq c-special-indent-hook
384 (default-value 'c-special-indent-hook)))
385 (mapc (lambda (elem)
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))
393 ;;;###autoload
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."
404 (interactive
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)))
412 (if s
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
422 ;; valid value only
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 [+,-,++,--,*,/] "
430 defstr))
431 (prompt (concat symname " offset " defstr))
432 (keymap (make-sparse-keymap))
433 (minibuffer-completion-table obarray)
434 (minibuffer-completion-predicate 'fboundp)
435 offset input)
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)
440 (while (not offset)
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)
445 (setq offset input)
446 ;; error, but don't signal one, keep trying
447 ;; to read an input value
448 (ding)
449 (setq prompt errmsg)))
450 offset))
452 ;;;###autoload
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."
458 (interactive
459 (let* ((langelem
460 (intern (completing-read
461 (concat "Syntactic symbol to change"
462 (if current-prefix-arg " or add" "")
463 ": ")
464 (mapcar
465 #'(lambda (langelem)
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
471 ;; line
472 (and c-buffer-is-cc-mode
473 (c-save-buffer-state
474 ((syntax (c-guess-basic-syntax))
475 (len (length syntax))
476 (ic (format "%s" (car (nth (1- len) syntax)))))
477 (cons ic 0)))
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)))
484 (if entry
485 (setcdr entry offset)
486 (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback))
487 (setq c-offsets-alist (cons (cons symbol offset)
488 c-offsets-alist))
489 (c-benign-error "%s is not a valid syntactic symbol" symbol))))
490 (c-benign-error "Invalid indentation setting for symbol %s: %S"
491 symbol offset))
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
499 variables."
501 (interactive)
502 (or c-buffer-is-cc-mode
503 (error "Buffer %s is not a CC Mode buffer (c-setup-paragraph-variables)"
504 (buffer-name)))
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
517 (concat "[ \t]*"
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
523 c-paragraph-start
524 "\\|"
525 page-delimiter)
526 paragraph-separate (concat blank-or-comment-line-prefix
527 c-paragraph-separate
528 "\\|"
529 page-delimiter)
530 paragraph-ignore-fill-prefix t
531 adaptive-fill-mode t
532 adaptive-fill-regexp
533 (concat comment-line-prefix
534 (if (default-value 'adaptive-fill-regexp)
535 (concat "\\("
536 (default-value 'adaptive-fill-regexp)
537 "\\)")
538 "")))
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.
547 "\\'"))))
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]*\\\\$"))
554 "\f\\|[ \t]*\\\\?$")
555 (setq c-string-par-separate
556 ;;(concat "\\(" (default-value 'paragraph-separate) "\\)\\|[ \t]*\\\\$"))
557 "[ \t\f]*\\\\?$")
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]*"
562 "\\)")))
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
578 ;; only.
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.
583 (let (p)
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)))
587 (if 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)
613 (while vars
614 (setq var (car vars)
615 val (symbol-value var)
616 vars (cdr vars))
617 (cond ((eq var 'c-offsets-alist)
618 (or (null val)
619 (setq uservars (cons (cons 'c-offsets-alist val)
620 uservars))))
621 ((not (eq val 'set-from-style))
622 (setq uservars (cons (cons var val)
623 uservars)))))
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."
640 ;; style variables
641 (let ((func (if this-buf-only-p
642 'make-local-variable
643 'make-variable-buffer-local))
644 (varsyms (cons 'c-indentation-style (copy-alist c-style-variables))))
645 (delq 'c-special-indent-hook varsyms)
646 (mapc func varsyms)
647 ;; Hooks must be handled specially
648 (if this-buf-only-p
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)
658 default-style
659 (or (cdr (assq mode default-style))
660 (cdr (assq 'other default-style))
661 "gnu")))
665 (cc-provide 'cc-styles)
667 ;; Local Variables:
668 ;; indent-tabs-mode: t
669 ;; tab-width: 8
670 ;; End:
671 ;;; cc-styles.el ends here