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