cc-styles.el 17 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1 2
;;; cc-styles.el --- support for styles in CC Mode

Gerd Moellmann's avatar
Gerd Moellmann committed
3
;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4

5 6
;; Authors:    2000- Martin Stjernholm
;;	       1998-1999 Barry A. Warsaw and Martin Stjernholm
Barry A. Warsaw's avatar
Barry A. Warsaw committed
7
;;             1992-1997 Barry A. Warsaw
Richard M. Stallman's avatar
Richard M. Stallman committed
8 9
;;             1987 Dave Detlefs and Stewart Clamen
;;             1985 Richard M. Stallman
Barry A. Warsaw's avatar
Barry A. Warsaw committed
10
;; Maintainer: bug-cc-mode@gnu.org
Richard M. Stallman's avatar
Richard M. Stallman committed
11
;; Created:    22-Apr-1997 (split from cc-mode.el)
Karl Heuer's avatar
Karl Heuer committed
12
;; Version:    See cc-mode.el
Richard M. Stallman's avatar
Richard M. Stallman committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
;; Keywords:   c languages oop

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Gerd Moellmann's avatar
Gerd Moellmann committed
28 29
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Richard M. Stallman's avatar
Richard M. Stallman committed
30 31
;; Boston, MA 02111-1307, USA.

Barry A. Warsaw's avatar
Barry A. Warsaw committed
32
(eval-when-compile
Gerd Moellmann's avatar
Gerd Moellmann committed
33
  (let ((load-path
Gerd Moellmann's avatar
Gerd Moellmann committed
34 35 36
	 (if (and (boundp 'byte-compile-dest-file)
		  (stringp byte-compile-dest-file))
	     (cons (file-name-directory byte-compile-dest-file) load-path)
Gerd Moellmann's avatar
Gerd Moellmann committed
37
	   load-path)))
Gerd Moellmann's avatar
Gerd Moellmann committed
38
    (require 'cc-bytecomp)))
Gerd Moellmann's avatar
Gerd Moellmann committed
39

Gerd Moellmann's avatar
Gerd Moellmann committed
40 41
(cc-require 'cc-defs)
(cc-require 'cc-vars)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
42

Richard M. Stallman's avatar
Richard M. Stallman committed
43

Barry A. Warsaw's avatar
Barry A. Warsaw committed
44
;; Warning: don't eval-defun this constant or you'll break style inheritance.
Richard M. Stallman's avatar
Richard M. Stallman committed
45 46 47 48 49 50 51 52 53 54 55 56
(defconst c-style-alist
  '(("gnu"
     (c-basic-offset . 2)
     (c-comment-only-line-offset . (0 . 0))
     (c-offsets-alist . ((statement-block-intro . +)
			 (knr-argdecl-intro . 5)
			 (substatement-open . +)
			 (label . 0)
			 (statement-case-open . +)
			 (statement-cont . +)
			 (arglist-intro . c-lineup-arglist-intro-after-paren)
			 (arglist-close . c-lineup-arglist)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
57
			 (inline-open . 0)
58
			 (brace-list-open . +)
Richard M. Stallman's avatar
Richard M. Stallman committed
59 60
			 ))
     (c-special-indent-hook . c-gnu-impose-minimum)
Gerd Moellmann's avatar
Gerd Moellmann committed
61
     (c-block-comment-prefix . "")
Richard M. Stallman's avatar
Richard M. Stallman committed
62 63 64 65 66 67 68 69 70 71 72 73
     )
    ("k&r"
     (c-basic-offset . 5)
     (c-comment-only-line-offset . 0)
     (c-offsets-alist . ((statement-block-intro . +)
			 (knr-argdecl-intro . 0)
			 (substatement-open . 0)
			 (label . 0)
			 (statement-cont . +)
			 ))
     )
    ("bsd"
74
     (c-basic-offset . 8)
Richard M. Stallman's avatar
Richard M. Stallman committed
75 76 77 78 79 80
     (c-comment-only-line-offset . 0)
     (c-offsets-alist . ((statement-block-intro . +)
			 (knr-argdecl-intro . +)
			 (substatement-open . 0)
			 (label . 0)
			 (statement-cont . +)
Gerd Moellmann's avatar
Gerd Moellmann committed
81 82
			 (inline-open . 0)
			 (inexpr-class . 0)
Richard M. Stallman's avatar
Richard M. Stallman committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96
			 ))
     )
    ("stroustrup"
     (c-basic-offset . 4)
     (c-comment-only-line-offset . 0)
     (c-offsets-alist . ((statement-block-intro . +)
			 (substatement-open . 0)
			 (label . 0)
			 (statement-cont . +)
			 ))
     )
    ("whitesmith"
     (c-basic-offset . 4)
     (c-comment-only-line-offset . 0)
Gerd Moellmann's avatar
Gerd Moellmann committed
97
     (c-offsets-alist . ((knr-argdecl-intro . +)
Richard M. Stallman's avatar
Richard M. Stallman committed
98 99
			 (label . 0)
			 (statement-cont . +)
Gerd Moellmann's avatar
Gerd Moellmann committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
			 (substatement-open . +)
			 (block-open . +)
			 (statement-block-intro . c-lineup-whitesmith-in-block)
			 (block-close . c-lineup-whitesmith-in-block)
			 (inline-open . +)
			 (defun-open . +)
			 (defun-block-intro . c-lineup-whitesmith-in-block)
			 (defun-close . c-lineup-whitesmith-in-block)
			 (brace-list-open . +)
			 (brace-list-intro . c-lineup-whitesmith-in-block)
			 (brace-entry-open . c-indent-multi-line-block)
			 (brace-list-close . c-lineup-whitesmith-in-block)
			 (class-open . +)
			 (inclass . c-lineup-whitesmith-in-block)
			 (class-close . +)
			 (inexpr-class . 0)
			 (extern-lang-open . +)
			 (inextern-lang . c-lineup-whitesmith-in-block)
			 (extern-lang-close . +)
			 (namespace-open . +)
			 (innamespace . c-lineup-whitesmith-in-block)
			 (namespace-close . +)
Richard M. Stallman's avatar
Richard M. Stallman committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
			 ))
     )
    ("ellemtel"
     (c-basic-offset . 3)
     (c-comment-only-line-offset . 0)
     (c-hanging-braces-alist     . ((substatement-open before after)))
     (c-offsets-alist . ((topmost-intro        . 0)
                         (topmost-intro-cont   . 0)
                         (substatement         . +)
			 (substatement-open    . 0)
                         (case-label           . +)
                         (access-label         . -)
                         (inclass              . ++)
                         (inline-open          . 0)
                         ))
     )
    ("linux"
     (c-basic-offset  . 8)
     (c-comment-only-line-offset . 0)
     (c-hanging-braces-alist . ((brace-list-open)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
142
				(brace-entry-open)
Richard M. Stallman's avatar
Richard M. Stallman committed
143 144 145 146 147 148 149 150 151 152 153 154
				(substatement-open after)
				(block-close . c-snug-do-while)))
     (c-cleanup-list . (brace-else-brace))
     (c-offsets-alist . ((statement-block-intro . +)
			 (knr-argdecl-intro     . 0)
			 (substatement-open     . 0)
			 (label                 . 0)
			 (statement-cont        . +)
			 ))
     )
    ("python"
     (indent-tabs-mode . t)
Gerd Moellmann's avatar
Gerd Moellmann committed
155
     (fill-column      . 78)
Richard M. Stallman's avatar
Richard M. Stallman committed
156 157
     (c-basic-offset   . 8)
     (c-offsets-alist  . ((substatement-open . 0)
158 159 160
			  (inextern-lang . 0)
			  (arglist-intro . +)
			  (knr-argdecl-intro . +)
Richard M. Stallman's avatar
Richard M. Stallman committed
161 162 163 164
			  ))
     (c-hanging-braces-alist . ((brace-list-open)
				(brace-list-intro)
				(brace-list-close)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
165
				(brace-entry-open)
Richard M. Stallman's avatar
Richard M. Stallman committed
166 167 168
				(substatement-open after)
				(block-close . c-snug-do-while)
				))
Gerd Moellmann's avatar
Gerd Moellmann committed
169
     (c-block-comment-prefix . "")
Richard M. Stallman's avatar
Richard M. Stallman committed
170 171
     )
    ("java"
172
     (c-basic-offset . 4)
Richard M. Stallman's avatar
Richard M. Stallman committed
173
     (c-comment-only-line-offset . (0 . 0))
174
     ;; the following preserves Javadoc starter lines
175 176
     (c-offsets-alist . ((inline-open . 0)
			 (topmost-intro-cont    . +)
Richard M. Stallman's avatar
Richard M. Stallman committed
177 178 179
			 (statement-block-intro . +)
 			 (knr-argdecl-intro     . 5)
 			 (substatement-open     . +)
Gerd Moellmann's avatar
Gerd Moellmann committed
180
 			 (label                 . +)
Richard M. Stallman's avatar
Richard M. Stallman committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
 			 (statement-case-open   . +)
 			 (statement-cont        . +)
 			 (arglist-intro  . c-lineup-arglist-intro-after-paren)
 			 (arglist-close  . c-lineup-arglist)
 			 (access-label   . 0)
			 (inher-cont     . c-lineup-java-inher)
			 (func-decl-cont . c-lineup-java-throws)
			 ))
     )
    )
  "Styles of indentation.
Elements of this alist are of the form:

  (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])

where STYLE-STRING is a short descriptive string used to select a
style, VARIABLE is any Emacs variable, and VALUE is the intended value
for that variable when using the selected style.

Optional BASE-STYLE if present, is a string and must follow
STYLE-STRING.  BASE-STYLE names a style that this style inherits from.
Gerd Moellmann's avatar
Gerd Moellmann committed
202
By default, all styles inherit from the \"user\" style, which is
Richard M. Stallman's avatar
Richard M. Stallman committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
computed at run time.  Style loops generate errors.

Two variables are treated specially.  When VARIABLE is
`c-offsets-alist', the VALUE is a list containing elements of the
form:

  (SYNTACTIC-SYMBOL . OFFSET)

as described in `c-offsets-alist'.  These are passed directly to
`c-set-offset' so there is no need to set every syntactic symbol in
your style, only those that are different from the default.

When VARIABLE is `c-special-indent-hook', its VALUE is added to
`c-special-indent-hook' using `add-hook'.  If VALUE is a list, each
element of the list is added with `add-hook'.

Do not change this variable directly.  Use the function `c-add-style'
to add new styles or modify existing styles (it is not a good idea to
modify existing styles -- you should create a new style that inherits
the existing style.")

Gerd Moellmann's avatar
Gerd Moellmann committed
224

Richard M. Stallman's avatar
Richard M. Stallman committed
225 226

;; Functions that manipulate styles
Gerd Moellmann's avatar
Gerd Moellmann committed
227
(defun c-set-style-1 (conscell dont-override)
Richard M. Stallman's avatar
Richard M. Stallman committed
228 229 230 231 232 233 234 235 236 237 238
  ;; Set the style for one variable
  (let ((attr (car conscell))
	(val  (cdr conscell)))
    (cond
     ;; first special variable
     ((eq attr 'c-offsets-alist)
      (mapcar
       (function
	(lambda (langentry)
	  (let ((langelem (car langentry))
		(offset (cdr langentry)))
Gerd Moellmann's avatar
Gerd Moellmann committed
239 240 241
	    (unless (and dont-override
			 (assq langelem c-offsets-alist))
	      (c-set-offset langelem offset))
Richard M. Stallman's avatar
Richard M. Stallman committed
242
	    )))
Gerd Moellmann's avatar
Gerd Moellmann committed
243
       (if dont-override (reverse val) val)))
Richard M. Stallman's avatar
Richard M. Stallman committed
244 245
     ;; second special variable
     ((eq attr 'c-special-indent-hook)
Gerd Moellmann's avatar
Gerd Moellmann committed
246 247 248 249 250 251 252 253 254
      (let ((add-func (if dont-override
			  (lambda (func)
			    (unless (memq func c-special-indent-hook)
			      (add-hook 'c-special-indent-hook func t)))
			(lambda (func)
			  (add-hook 'c-special-indent-hook func)))))
	(if (listp val)
	    (mapcar add-func (if dont-override (reverse val) val))
	  (funcall add-func val))))
Richard M. Stallman's avatar
Richard M. Stallman committed
255
     ;; all other variables
Gerd Moellmann's avatar
Gerd Moellmann committed
256 257 258 259
     (t (if (or (not dont-override)
		(not (memq attr c-style-variables))
		(eq (symbol-value attr) 'set-from-style))
	    (set attr val))))
Richard M. Stallman's avatar
Richard M. Stallman committed
260 261
    ))

Gerd Moellmann's avatar
Gerd Moellmann committed
262 263
(defun c-get-style-variables (style basestyles)
  ;; Return all variables in a style by resolving inheritances.
Richard M. Stallman's avatar
Richard M. Stallman committed
264 265 266 267
  (let ((vars (cdr (or (assoc (downcase style) c-style-alist)
		       (assoc (upcase style) c-style-alist)
		       (assoc style c-style-alist)
		       (error "Undefined style: %s" style)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
268 269 270 271 272 273 274 275 276 277 278 279
    (if (string-equal style "user")
	(copy-alist vars)
      (let ((base (if (stringp (car vars))
		      (prog1
			  (downcase (car vars))
			(setq vars (cdr vars)))
		    "user")))
	(if (memq base basestyles)
	    (error "Style loop detected: %s in %s" base basestyles))
	(nconc (c-get-style-variables base (cons base basestyles))
	       (copy-alist vars))))))

Richard M. Stallman's avatar
Richard M. Stallman committed
280 281 282
(defvar c-set-style-history nil)

;;;###autoload
Gerd Moellmann's avatar
Gerd Moellmann committed
283
(defun c-set-style (stylename &optional dont-override)
Richard M. Stallman's avatar
Richard M. Stallman committed
284 285 286 287 288 289
  "Set CC Mode variables to use one of several different indentation styles.
STYLENAME is a string representing the desired style from the list of
styles described in the variable `c-style-alist'.  See that variable
for details of setting up styles.

The variable `c-indentation-style' always contains the buffer's current
Gerd Moellmann's avatar
Gerd Moellmann committed
290 291 292 293 294 295 296 297 298 299 300
style name.

If the optional argument DONT-OVERRIDE is non-nil, no style variables
that already have values will be overridden.  I.e. in the case of
`c-offsets-alist', syntactic symbols will only be added, and in the
case of all other style variables, only those set to `set-from-style'
will be reassigned.

Obviously, specifying DONT-OVERRIDE is useful mainly when the initial
style is chosen for a CC Mode buffer by a major mode.  Since this is
done internally by CC Mode, there's hardly ever a reason to use it."
Richard M. Stallman's avatar
Richard M. Stallman committed
301 302 303 304
  (interactive (list (let ((completion-ignore-case t)
			   (prompt (format "Which %s indentation style? "
					   mode-name)))
		       (completing-read prompt c-style-alist nil t
305 306 307
					nil
					'c-set-style-history
					c-indentation-style))))
308
  (c-initialize-builtin-style)
Gerd Moellmann's avatar
Gerd Moellmann committed
309 310 311 312 313 314
  (let ((vars (c-get-style-variables stylename nil)))
    (mapcar (lambda (elem)
	      (c-set-style-1 elem dont-override))
	    ;; Need to go through the variables backwards when we
	    ;; don't override.
	    (if dont-override (nreverse vars) vars)))
Richard M. Stallman's avatar
Richard M. Stallman committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  (setq c-indentation-style stylename)
  (c-keep-region-active))

;;;###autoload
(defun c-add-style (style descrip &optional set-p)
  "Adds a style to `c-style-alist', or updates an existing one.
STYLE is a string identifying the style to add or update.  DESCRIP is
an association list describing the style and must be of the form:

  ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])

See the variable `c-style-alist' for the semantics of BASESTYLE,
VARIABLE and VALUE.  This function also sets the current style to
STYLE using `c-set-style' if the optional SET-P flag is non-nil."
  (interactive
   (let ((stylename (completing-read "Style to add: " c-style-alist
				     nil nil nil 'c-set-style-history))
	 (description (eval-minibuffer "Style description: ")))
     (list stylename description
	   (y-or-n-p "Set the style too? "))))
  (setq style (downcase style))
  (let ((s (assoc style c-style-alist)))
    (if s
	(setcdr s (copy-alist descrip))	; replace
      (setq c-style-alist (cons (cons style descrip) c-style-alist))))
  (and set-p (c-set-style style)))



(defvar c-read-offset-history nil)

(defun c-read-offset (langelem)
  ;; read new offset value for LANGELEM from minibuffer. return a
  ;; legal value only
Gerd Moellmann's avatar
Gerd Moellmann committed
349 350 351
  (let* ((oldoff  (cdr-safe (or (assq langelem c-offsets-alist)
				(assq langelem (get 'c-offsets-alist
						    'c-stylevar-fallback)))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
352 353
	 (symname (symbol-name langelem))
	 (defstr  (format "(default %s): " oldoff))
Gerd Moellmann's avatar
Gerd Moellmann committed
354
	 (errmsg  (concat "Offset must be int, func, var, vector, list, "
Barry A. Warsaw's avatar
Barry A. Warsaw committed
355 356 357
			  "or [+,-,++,--,*,/] "
			  defstr))
	 (prompt (concat symname " offset " defstr))
Richard M. Stallman's avatar
Richard M. Stallman committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	 offset input interned raw)
    (while (not offset)
      (setq input (completing-read prompt obarray 'fboundp nil nil
				   'c-read-offset-history)
	    offset (cond ((string-equal "" input) oldoff)  ; default
			 ((string-equal "+" input) '+)
			 ((string-equal "-" input) '-)
			 ((string-equal "++" input) '++)
			 ((string-equal "--" input) '--)
			 ((string-equal "*" input) '*)
			 ((string-equal "/" input) '/)
			 ((string-match "^-?[0-9]+$" input)
			  (string-to-int input))
			 ;; a symbol with a function binding
			 ((fboundp (setq interned (intern input)))
			  interned)
			 ;; a symbol with variable binding
			 ((boundp interned) interned)
Gerd Moellmann's avatar
Gerd Moellmann committed
376 377 378 379 380 381
			 ;; a lambda function or a vector
			 ((progn
			    (c-safe (setq raw (read input)))
			    (or (functionp raw)
				(vectorp raw)))
			  raw)
Richard M. Stallman's avatar
Richard M. Stallman committed
382 383 384 385 386 387 388
			 ;; error, but don't signal one, keep trying
			 ;; to read an input value
			 (t (ding)
			    (setq prompt errmsg)
			    nil))))
    offset))

389
;;;###autoload
Gerd Moellmann's avatar
Gerd Moellmann committed
390
(defun c-set-offset (symbol offset &optional ignored)
Richard M. Stallman's avatar
Richard M. Stallman committed
391 392
  "Change the value of a syntactic element symbol in `c-offsets-alist'.
SYMBOL is the syntactic element symbol to change and OFFSET is the new
Gerd Moellmann's avatar
Gerd Moellmann committed
393 394
offset for that syntactic element.  The optional argument is not used
and exists only for compatibility reasons."
Richard M. Stallman's avatar
Richard M. Stallman committed
395 396 397 398 399 400 401 402 403
  (interactive
   (let* ((langelem
	   (intern (completing-read
		    (concat "Syntactic symbol to change"
			    (if current-prefix-arg " or add" "")
			    ": ")
		    (mapcar
		     #'(lambda (langelem)
			 (cons (format "%s" (car langelem)) nil))
Gerd Moellmann's avatar
Gerd Moellmann committed
404
		     (get 'c-offsets-alist 'c-stylevar-fallback))
Richard M. Stallman's avatar
Richard M. Stallman committed
405 406 407 408 409 410 411 412 413 414 415 416
		    nil (not current-prefix-arg)
		    ;; initial contents tries to be the last element
		    ;; on the syntactic analysis list for the current
		    ;; line
		    (let* ((syntax (c-guess-basic-syntax))
			   (len (length syntax))
			   (ic (format "%s" (car (nth (1- len) syntax)))))
		      (cons ic 0))
		    )))
	  (offset (c-read-offset langelem)))
     (list langelem offset current-prefix-arg)))
  ;; sanity check offset
Gerd Moellmann's avatar
Gerd Moellmann committed
417
  (unless (c-valid-offset offset)
Gerd Moellmann's avatar
Gerd Moellmann committed
418 419
    (error (concat "Offset must be int, func, var, vector, list, "
		   "or in [+,-,++,--,*,/]: %s")
Gerd Moellmann's avatar
Gerd Moellmann committed
420
	   offset))
Richard M. Stallman's avatar
Richard M. Stallman committed
421 422 423
  (let ((entry (assq symbol c-offsets-alist)))
    (if entry
	(setcdr entry offset)
Gerd Moellmann's avatar
Gerd Moellmann committed
424
      (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback))
Richard M. Stallman's avatar
Richard M. Stallman committed
425
	  (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist))
Gerd Moellmann's avatar
Gerd Moellmann committed
426
	(error "%s is not a valid syntactic symbol" symbol))))
Richard M. Stallman's avatar
Richard M. Stallman committed
427 428
  (c-keep-region-active))

Barry A. Warsaw's avatar
Barry A. Warsaw committed
429

Gerd Moellmann's avatar
Gerd Moellmann committed
430

Richard M. Stallman's avatar
Richard M. Stallman committed
431 432 433 434 435
(defun c-initialize-builtin-style ()
  ;; Dynamically append the default value of most variables. This is
  ;; crucial because future c-set-style calls will always reset the
  ;; variables first to the `cc-mode' style before instituting the new
  ;; style.  Only do this once!
Barry A. Warsaw's avatar
Barry A. Warsaw committed
436 437
  (unless (get 'c-initialize-builtin-style 'is-run)
    (put 'c-initialize-builtin-style 'is-run t)
Gerd Moellmann's avatar
Gerd Moellmann committed
438
    ;;(c-initialize-cc-mode)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
439 440 441
    (or (assoc "cc-mode" c-style-alist)
	(assoc "user" c-style-alist)
	(progn
Gerd Moellmann's avatar
Gerd Moellmann committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	  (c-add-style
	   "user"
	   (mapcar
	    (lambda (var)
	      (let ((val (symbol-value var)))
		(cons var
		      (cond ((eq var 'c-offsets-alist)
			     (mapcar
			      (lambda (langentry)
				(setq langentry (or (assq (car langentry) val)
						    langentry))
				(cons (car langentry)
				      (cdr langentry)))
			      (get var 'c-stylevar-fallback)))
			    ((eq var 'c-special-indent-hook)
			     val)
			    (t
			     (if (eq val 'set-from-style)
				 (get var 'c-stylevar-fallback)
			       val))))))
	    c-style-variables))
	  (c-add-style "cc-mode" '("user"))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
464 465
    (if c-style-variables-are-local-p
	(c-make-styles-buffer-local))))
466

467
(defun c-make-styles-buffer-local (&optional this-buf-only-p)
Richard M. Stallman's avatar
Richard M. Stallman committed
468
  "Make all CC Mode style variables buffer local.
Gerd Moellmann's avatar
Gerd Moellmann committed
469 470 471
If you edit primarily one style of C (or C++, Objective-C, Java, etc)
code, you probably want style variables to be global.  This is the
default.
Richard M. Stallman's avatar
Richard M. Stallman committed
472

Gerd Moellmann's avatar
Gerd Moellmann committed
473 474 475 476 477
If you edit many different styles of C (or C++, Objective-C, Java,
etc) at the same time, you probably want the CC Mode style variables
to be buffer local.  If you do, it's advicable to set any CC Mode
style variables in a hook function (e.g. off of `c-mode-common-hook'),
instead of at the top level of your ~/.emacs file.
Richard M. Stallman's avatar
Richard M. Stallman committed
478 479 480 481 482

This function makes all the CC Mode style variables buffer local.
Call it after CC Mode is loaded into your Emacs environment.
Conversely, set the variable `c-style-variables-are-local-p' to t in
your .emacs file, before CC Mode is loaded, and this function will be
483 484 485 486
automatically called when CC Mode is loaded.

Optional argument, when non-nil, means use `make-local-variable'
instead of `make-variable-buffer-local'."
Richard M. Stallman's avatar
Richard M. Stallman committed
487
  ;; style variables
488 489 490
  (let ((func (if this-buf-only-p
		  'make-local-variable
		'make-variable-buffer-local))
Gerd Moellmann's avatar
Gerd Moellmann committed
491 492
	(varsyms (cons 'c-indentation-style (copy-alist c-style-variables))))
    (delq 'c-special-indent-hook varsyms)
493 494 495 496
    (mapcar func varsyms)
    ;; Hooks must be handled specially
    (if this-buf-only-p
	(make-local-hook 'c-special-indent-hook)
Gerd Moellmann's avatar
Gerd Moellmann committed
497 498
      (make-variable-buffer-local 'c-special-indent-hook)
      (setq c-style-variables-are-local-p t))
499 500
    ))

Richard M. Stallman's avatar
Richard M. Stallman committed
501 502


Gerd Moellmann's avatar
Gerd Moellmann committed
503
(cc-provide 'cc-styles)
Richard M. Stallman's avatar
Richard M. Stallman committed
504
;;; cc-styles.el ends here