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

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
Glenn Morris's avatar
Glenn Morris committed
4
;;   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
Glenn Morris's avatar
Glenn Morris committed
5
;;   Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
6

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

;; This file is part of GNU Emacs.

20
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
21
;; it under the terms of the GNU General Public License as published by
22 23
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
24 25 26 27 28 29 30

;; 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
31
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
32

33 34 35 36
;;; Commentary:

;;; Code:

Barry A. Warsaw's avatar
Barry A. Warsaw committed
37
(eval-when-compile
Gerd Moellmann's avatar
Gerd Moellmann committed
38
  (let ((load-path
Gerd Moellmann's avatar
Gerd Moellmann committed
39 40 41
	 (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
42
	   load-path)))
43
    (load "cc-bytecomp" nil t)))
Gerd Moellmann's avatar
Gerd Moellmann committed
44

Gerd Moellmann's avatar
Gerd Moellmann committed
45 46
(cc-require 'cc-defs)
(cc-require 'cc-vars)
47 48 49 50 51 52
(cc-require 'cc-align)
;; cc-align is only indirectly required: Styles added with
;; `c-add-style' often contains references to functions defined there.

;; Silence the compiler.
(cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs
53
(cc-bytecomp-obsolete-fun make-local-hook) ; Marked obsolete in Emacs 21.1.
Barry A. Warsaw's avatar
Barry A. Warsaw committed
54

Richard M. Stallman's avatar
Richard M. Stallman committed
55

56
(defvar c-style-alist
Richard M. Stallman's avatar
Richard M. Stallman committed
57 58 59
  '(("gnu"
     (c-basic-offset . 2)
     (c-comment-only-line-offset . (0 . 0))
60 61
     (c-hanging-braces-alist     . ((substatement-open before after)
				    (arglist-cont-nonempty)))
Richard M. Stallman's avatar
Richard M. Stallman committed
62 63 64
     (c-offsets-alist . ((statement-block-intro . +)
			 (knr-argdecl-intro . 5)
			 (substatement-open . +)
65
			 (substatement-label . 0)
Richard M. Stallman's avatar
Richard M. Stallman committed
66 67 68 69 70
			 (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
71
			 (inline-open . 0)
72 73 74 75
			 (brace-list-open . +)
			 (topmost-intro-cont
			  . (first c-lineup-topmost-intro-cont
				   c-lineup-gnu-DEFUN-intro-cont))))
Richard M. Stallman's avatar
Richard M. Stallman committed
76
     (c-special-indent-hook . c-gnu-impose-minimum)
77 78
     (c-block-comment-prefix . ""))

Richard M. Stallman's avatar
Richard M. Stallman committed
79 80 81 82 83 84
    ("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)
85
			 (substatement-label . 0)
Richard M. Stallman's avatar
Richard M. Stallman committed
86
			 (label . 0)
87 88
			 (statement-cont . +))))

Richard M. Stallman's avatar
Richard M. Stallman committed
89
    ("bsd"
90
     (c-basic-offset . 8)
Richard M. Stallman's avatar
Richard M. Stallman committed
91 92 93 94
     (c-comment-only-line-offset . 0)
     (c-offsets-alist . ((statement-block-intro . +)
			 (knr-argdecl-intro . +)
			 (substatement-open . 0)
95
			 (substatement-label . 0)
Richard M. Stallman's avatar
Richard M. Stallman committed
96 97
			 (label . 0)
			 (statement-cont . +)
Gerd Moellmann's avatar
Gerd Moellmann committed
98
			 (inline-open . 0)
99 100
			 (inexpr-class . 0))))

Richard M. Stallman's avatar
Richard M. Stallman committed
101 102 103 104 105
    ("stroustrup"
     (c-basic-offset . 4)
     (c-comment-only-line-offset . 0)
     (c-offsets-alist . ((statement-block-intro . +)
			 (substatement-open . 0)
106
			 (substatement-label . 0)
Richard M. Stallman's avatar
Richard M. Stallman committed
107
			 (label . 0)
108 109
			 (statement-cont . +))))

Richard M. Stallman's avatar
Richard M. Stallman committed
110 111 112
    ("whitesmith"
     (c-basic-offset . 4)
     (c-comment-only-line-offset . 0)
113 114 115
     ;; It's obvious that the CC Mode way of choosing anchor positions
     ;; doesn't fit this style at all. :P
     (c-offsets-alist . ((defun-open . +)
Gerd Moellmann's avatar
Gerd Moellmann committed
116
			 (defun-close . c-lineup-whitesmith-in-block)
117 118 119 120 121 122 123 124 125
			 (defun-block-intro . (add c-lineup-whitesmith-in-block
						   c-indent-multi-line-block))
			 (class-open . +)
			 (class-close . +)
			 (inline-open . +)
			 (inline-close . c-lineup-whitesmith-in-block)
			 (knr-argdecl-intro . +)
			 (block-open . 0) ; Get indentation from `statement' instead.
			 (block-close . c-lineup-whitesmith-in-block)
Gerd Moellmann's avatar
Gerd Moellmann committed
126 127
			 (brace-list-open . +)
			 (brace-list-close . c-lineup-whitesmith-in-block)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
			 (brace-list-intro . (add c-lineup-whitesmith-in-block
						  c-indent-multi-line-block))
			 (brace-list-entry . (add c-lineup-after-whitesmith-blocks
						  c-indent-multi-line-block))
			 (brace-entry-open . (add c-lineup-after-whitesmith-blocks
						  c-indent-multi-line-block))
			 (statement . (add c-lineup-after-whitesmith-blocks
					   c-indent-multi-line-block))
			 (statement-block-intro . (add c-lineup-whitesmith-in-block
						       c-indent-multi-line-block))
			 (substatement-open . +)
			 (substatement-label . +)
			 (label . 0)
			 (arglist-intro . (add c-lineup-whitesmith-in-block
					       c-indent-multi-line-block))
			 (arglist-cont . (add c-lineup-after-whitesmith-blocks
					      c-indent-multi-line-block))
			 (arglist-cont-nonempty . (add c-lineup-whitesmith-in-block
						       c-indent-multi-line-block))
			 (arglist-close . c-lineup-whitesmith-in-block)
Gerd Moellmann's avatar
Gerd Moellmann committed
148 149 150
			 (inclass . c-lineup-whitesmith-in-block)
			 (extern-lang-open . +)
			 (namespace-open . +)
151 152
			 (module-open . +)
			 (composition-open . +)
153 154 155
			 (extern-lang-close . +)
			 (namespace-close . +)
			 (module-close . +)
156
			 (composition-close . +)
157 158 159 160 161 162
			 (inextern-lang . c-lineup-whitesmith-in-block)
			 (innamespace . c-lineup-whitesmith-in-block)
			 (inmodule . c-lineup-whitesmith-in-block)
			 (incomposition . c-lineup-whitesmith-in-block)
			 (inexpr-class . 0))))

Richard M. Stallman's avatar
Richard M. Stallman committed
163 164 165
    ("ellemtel"
     (c-basic-offset . 3)
     (c-comment-only-line-offset . 0)
166 167
     (c-hanging-braces-alist     . ((substatement-open before after)
				    (arglist-cont-nonempty)))
Richard M. Stallman's avatar
Richard M. Stallman committed
168
     (c-offsets-alist . ((topmost-intro        . 0)
169
			 (substatement         . +)
Richard M. Stallman's avatar
Richard M. Stallman committed
170 171 172
			 (substatement-open    . 0)
                         (case-label           . +)
                         (access-label         . -)
173 174
			 (inclass              . +)
			 (inline-open          . 0))))
Richard M. Stallman's avatar
Richard M. Stallman committed
175 176 177 178
    ("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
179
				(brace-entry-open)
Richard M. Stallman's avatar
Richard M. Stallman committed
180
				(substatement-open after)
181 182
				(block-close . c-snug-do-while)
				(arglist-cont-nonempty)))
Richard M. Stallman's avatar
Richard M. Stallman committed
183 184 185 186
     (c-cleanup-list . (brace-else-brace))
     (c-offsets-alist . ((statement-block-intro . +)
			 (knr-argdecl-intro     . 0)
			 (substatement-open     . 0)
187
			 (substatement-label    . 0)
Richard M. Stallman's avatar
Richard M. Stallman committed
188
			 (label                 . 0)
189 190
			 (statement-cont        . +))))

Richard M. Stallman's avatar
Richard M. Stallman committed
191 192
    ("python"
     (indent-tabs-mode . t)
Gerd Moellmann's avatar
Gerd Moellmann committed
193
     (fill-column      . 78)
Richard M. Stallman's avatar
Richard M. Stallman committed
194 195
     (c-basic-offset   . 8)
     (c-offsets-alist  . ((substatement-open . 0)
196 197
			  (inextern-lang . 0)
			  (arglist-intro . +)
198
			  (knr-argdecl-intro . +)))
Richard M. Stallman's avatar
Richard M. Stallman committed
199 200 201
     (c-hanging-braces-alist . ((brace-list-open)
				(brace-list-intro)
				(brace-list-close)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
202
				(brace-entry-open)
Richard M. Stallman's avatar
Richard M. Stallman committed
203
				(substatement-open after)
204 205
				(block-close . c-snug-do-while)
				(arglist-cont-nonempty)))
206 207
     (c-block-comment-prefix . ""))

Richard M. Stallman's avatar
Richard M. Stallman committed
208
    ("java"
209
     (c-basic-offset . 4)
Richard M. Stallman's avatar
Richard M. Stallman committed
210
     (c-comment-only-line-offset . (0 . 0))
211
     ;; the following preserves Javadoc starter lines
212 213
     (c-offsets-alist . ((inline-open . 0)
			 (topmost-intro-cont    . +)
Richard M. Stallman's avatar
Richard M. Stallman committed
214 215
			 (statement-block-intro . +)
 			 (knr-argdecl-intro     . 5)
216 217
			 (substatement-open     . +)
			 (substatement-label    . +)
Gerd Moellmann's avatar
Gerd Moellmann committed
218
 			 (label                 . +)
Richard M. Stallman's avatar
Richard M. Stallman committed
219 220 221 222 223 224
 			 (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)
225 226 227 228 229 230 231 232 233 234
			 (func-decl-cont . c-lineup-java-throws))))

    ;; awk style exists primarily for auto-newline settings.  Otherwise it's
    ;; pretty much like k&r.
    ("awk"
     (c-basic-offset . 4)
     (c-comment-only-line-offset . 0)
     (c-hanging-braces-alist . ((defun-open after)
				(defun-close . c-snug-1line-defun-close)
				(substatement-open after)
235 236
				(block-close . c-snug-do-while)
				(arglist-cont-nonempty)))
237 238 239 240 241 242
     (c-hanging-semi&comma-criteria . nil)
     (c-cleanup-list . nil)		; You might want one-liner-defun here.
     (c-offsets-alist . ((statement-block-intro . +)
			 (substatement-open . 0)
			 (statement-cont . +))))

Richard M. Stallman's avatar
Richard M. Stallman committed
243 244 245 246 247 248 249 250 251 252 253 254
    )
  "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
255
By default, all styles inherit from the \"user\" style, which is
Richard M. Stallman's avatar
Richard M. Stallman committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
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
Eli Zaretskii's avatar
Eli Zaretskii committed
275
the existing style).")
Richard M. Stallman's avatar
Richard M. Stallman committed
276 277 278


;; Functions that manipulate styles
Gerd Moellmann's avatar
Gerd Moellmann committed
279
(defun c-set-style-1 (conscell dont-override)
Richard M. Stallman's avatar
Richard M. Stallman committed
280 281 282 283 284 285
  ;; Set the style for one variable
  (let ((attr (car conscell))
	(val  (cdr conscell)))
    (cond
     ;; first special variable
     ((eq attr 'c-offsets-alist)
286 287 288 289 290 291 292 293 294 295
      (let ((offsets (cond ((eq dont-override t)
			    c-offsets-alist)
			   (dont-override
			    (default-value 'c-offsets-alist)))))
	(mapcar (lambda (langentry)
		  (let ((langelem (car langentry))
			(offset (cdr langentry)))
		    (unless (assq langelem offsets)
		      (c-set-offset langelem offset))))
		val)))
Richard M. Stallman's avatar
Richard M. Stallman committed
296 297
     ;; second special variable
     ((eq attr 'c-special-indent-hook)
298 299 300 301 302 303
      ;; Maybe we should ignore dont-override here and always add new
      ;; hooks?
      (unless (cond ((eq dont-override t)
		     c-special-indent-hook)
		    (dont-override
		     (default-value 'c-special-indent-hook)))
Gerd Moellmann's avatar
Gerd Moellmann committed
304
	(if (listp val)
305 306 307 308
	    (mapcar (lambda (func)
		      (add-hook 'c-special-indent-hook func t t))
		    val)
	  (add-hook 'c-special-indent-hook val t t))))
Richard M. Stallman's avatar
Richard M. Stallman committed
309
     ;; all other variables
310 311
     (t (when (or (not dont-override)
		  (not (memq attr c-style-variables))
312 313 314 315
		  (eq (if (eq dont-override t)
			  (symbol-value attr)
			(default-value attr))
		      'set-from-style))
316 317 318 319 320
	  (set attr val)
	  ;; Must update a number of other variables if
	  ;; c-comment-prefix-regexp is set.
	  (if (eq attr 'c-comment-prefix-regexp)
	      (c-setup-paragraph-variables)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
321

Gerd Moellmann's avatar
Gerd Moellmann committed
322 323
(defun c-get-style-variables (style basestyles)
  ;; Return all variables in a style by resolving inheritances.
324 325 326 327 328 329 330 331 332 333 334 335
  (if (not style)
      (copy-alist c-fallback-style)
    (let ((vars (cdr (or (assoc (downcase style) c-style-alist)
			 (assoc (upcase style) c-style-alist)
			 (assoc style c-style-alist)
			 (progn
			   (c-benign-error "Undefined style: %s" style)
			   nil)))))
      (let ((base (and (stringp (car-safe vars))
		       (prog1
			   (downcase (car vars))
			 (setq vars (cdr vars))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
336
	(if (memq base basestyles)
337 338 339
	    (c-benign-error "Style loop detected: %s in %s" base basestyles)
	  (nconc (c-get-style-variables base (cons base basestyles))
		 (copy-alist vars)))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
340

Richard M. Stallman's avatar
Richard M. Stallman committed
341 342 343
(defvar c-set-style-history nil)

;;;###autoload
Gerd Moellmann's avatar
Gerd Moellmann committed
344
(defun c-set-style (stylename &optional dont-override)
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
  "Set the current buffer to use the style STYLENAME.
STYLENAME, a string, must be an existing CC Mode style - These are contained
in the variable `c-style-alist'.

The variable `c-indentation-style' will get set to STYLENAME.

\"Setting the style\" is done by setting CC Mode's \"style variables\" to the
values indicated by the pertinent entry in `c-style-alist'.  Other variables
might get set too.

If DONT-OVERRIDE is neither nil nor t, style variables whose default values
have been set (more precisely, whose default values are not the symbol
`set-from-style') will not be changed.  This avoids overriding global settings
done in ~/.emacs.  It is useful to call c-set-style from a mode hook in this
way.

If DONT-OVERRIDE is t, style variables that already have values (i.e., whose
values are not the symbol `set-from-style') will not be overridden.  CC Mode
calls c-set-style internally in this way whilst initializing a buffer; if
cc-set-style is called like this from anywhere else, it will usually behave as
a null operation."
366 367 368 369
  (interactive
   (list (let ((completion-ignore-case t)
	       (prompt (format "Which %s indentation style? "
			       mode-name)))
370 371 372 373 374
	   (completing-read prompt c-style-alist nil t nil
			    'c-set-style-history
			    c-indentation-style))))
  (or c-buffer-is-cc-mode
      (error "Buffer %s is not a CC Mode buffer (c-set-style)" (buffer-name)))
Martin Stjernholm's avatar
Martin Stjernholm committed
375 376
  (or (stringp stylename)
      (error "Argument to c-set-style was not a string"))
377
  (c-initialize-builtin-style)
Gerd Moellmann's avatar
Gerd Moellmann committed
378
  (let ((vars (c-get-style-variables stylename nil)))
379 380 381 382 383 384 385 386
    (unless dont-override
      ;; Since we always add to c-special-indent-hook we must reset it
      ;; first, or else the hooks from the preceding style will
      ;; remain.  This is not necessary for c-offsets-alist, since
      ;; c-get-style-variables contains every valid offset type in the
      ;; fallback entry.
      (setq c-special-indent-hook
	    (default-value 'c-special-indent-hook)))
Glenn Morris's avatar
Glenn Morris committed
387 388 389 390 391
    (mapc (lambda (elem)
	    (c-set-style-1 elem dont-override))
	  ;; Need to go through the variables backwards when we
	  ;; don't override any settings.
	  (if (eq dont-override t) (nreverse vars) vars)))
Richard M. Stallman's avatar
Richard M. Stallman committed
392 393 394 395
  (setq c-indentation-style stylename)
  (c-keep-region-active))

;;;###autoload
396
(defun c-add-style (style description &optional set-p)
Richard M. Stallman's avatar
Richard M. Stallman committed
397
  "Adds a style to `c-style-alist', or updates an existing one.
398 399
STYLE is a string identifying the style to add or update.  DESCRIPTION
is an association list describing the style and must be of the form:
Richard M. Stallman's avatar
Richard M. Stallman committed
400 401 402 403 404 405 406 407 408

  ([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))
409 410
         (descr (eval-minibuffer "Style description: ")))
     (list stylename descr
Richard M. Stallman's avatar
Richard M. Stallman committed
411 412 413 414
	   (y-or-n-p "Set the style too? "))))
  (setq style (downcase style))
  (let ((s (assoc style c-style-alist)))
    (if s
415 416
        (setcdr s (copy-alist description)) ; replace
      (setq c-style-alist (cons (cons style description) c-style-alist))))
Richard M. Stallman's avatar
Richard M. Stallman committed
417 418 419 420 421 422 423
  (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
Juanma Barranquero's avatar
Juanma Barranquero committed
424
  ;; valid value only
Gerd Moellmann's avatar
Gerd Moellmann committed
425 426 427
  (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
428 429
	 (symname (symbol-name langelem))
	 (defstr  (format "(default %s): " oldoff))
Gerd Moellmann's avatar
Gerd Moellmann committed
430
	 (errmsg  (concat "Offset must be int, func, var, vector, list, "
Barry A. Warsaw's avatar
Barry A. Warsaw committed
431 432 433
			  "or [+,-,++,--,*,/] "
			  defstr))
	 (prompt (concat symname " offset " defstr))
434 435 436 437 438 439 440 441
	 (keymap (make-sparse-keymap))
	 (minibuffer-completion-table obarray)
	 (minibuffer-completion-predicate 'fboundp)
	 offset input)
    ;; In principle completing-read is used here, but SPC is unbound
    ;; to make it less annoying to enter lists.
    (set-keymap-parent keymap minibuffer-local-completion-map)
    (define-key keymap " " 'self-insert-command)
Richard M. Stallman's avatar
Richard M. Stallman committed
442
    (while (not offset)
443 444 445 446 447 448 449 450 451
      (setq input (read-from-minibuffer prompt nil keymap t
					'c-read-offset-history
					(format "%s" oldoff)))
      (if (c-valid-offset input)
	  (setq offset input)
	;; error, but don't signal one, keep trying
	;; to read an input value
	(ding)
	(setq prompt errmsg)))
Richard M. Stallman's avatar
Richard M. Stallman committed
452 453
    offset))

454
;;;###autoload
Gerd Moellmann's avatar
Gerd Moellmann committed
455
(defun c-set-offset (symbol offset &optional ignored)
Richard M. Stallman's avatar
Richard M. Stallman committed
456 457
  "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
458 459
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
460 461 462 463 464 465 466 467 468
  (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
469
		     (get 'c-offsets-alist 'c-stylevar-fallback))
Richard M. Stallman's avatar
Richard M. Stallman committed
470 471 472 473
		    nil (not current-prefix-arg)
		    ;; initial contents tries to be the last element
		    ;; on the syntactic analysis list for the current
		    ;; line
474
		    (and c-buffer-is-cc-mode
475 476 477 478
			 (c-save-buffer-state
			     ((syntax (c-guess-basic-syntax))
			      (len (length syntax))
			      (ic (format "%s" (car (nth (1- len) syntax)))))
479
			   (cons ic 0)))
Richard M. Stallman's avatar
Richard M. Stallman committed
480 481 482 483
		    )))
	  (offset (c-read-offset langelem)))
     (list langelem offset current-prefix-arg)))
  ;; sanity check offset
484 485 486 487 488 489 490 491
  (if (c-valid-offset offset)
      (let ((entry (assq symbol c-offsets-alist)))
	(if entry
	    (setcdr entry offset)
	  (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback))
	      (setq c-offsets-alist (cons (cons symbol offset)
					  c-offsets-alist))
	    (c-benign-error "%s is not a valid syntactic symbol" symbol))))
492
    (c-benign-error "Invalid indentation setting for symbol %s: %S"
493
		    symbol offset))
Richard M. Stallman's avatar
Richard M. Stallman committed
494 495
  (c-keep-region-active))

496 497

(defun c-setup-paragraph-variables ()
498 499 500
  "Fix things up for paragraph recognition and filling inside comments and
strings by incorporating the values of `c-comment-prefix-regexp',
`sentence-end', `paragraph-start' and `paragraph-separate' in the relevant
501
variables."
502

503
  (interactive)
504 505 506 507
  (or c-buffer-is-cc-mode
      (error "Buffer %s is not a CC Mode buffer (c-setup-paragraph-variables)"
	     (buffer-name)))
  ;; Set up the values for use in comments.
508 509 510 511 512
  (setq c-current-comment-prefix
	(if (listp c-comment-prefix-regexp)
	    (cdr-safe (or (assoc major-mode c-comment-prefix-regexp)
			  (assoc 'other c-comment-prefix-regexp)))
	  c-comment-prefix-regexp))
513

514 515 516 517 518 519 520 521 522 523 524
  (let* ((empty-is-prefix (string-match c-current-comment-prefix ""))
	 (nonws-comment-line-prefix
	  (concat "\\(" c-current-comment-prefix "\\)[ \t]*"))
	 (comment-line-prefix (concat "[ \t]*" nonws-comment-line-prefix))
	 (blank-or-comment-line-prefix
	  (concat "[ \t]*"
		  (if empty-is-prefix "" "\\(")
		  nonws-comment-line-prefix
		  (if empty-is-prefix "" "\\)?"))))

    (setq paragraph-start (concat blank-or-comment-line-prefix
525 526 527
				  c-paragraph-start
				  "\\|"
				  page-delimiter)
528
	  paragraph-separate (concat blank-or-comment-line-prefix
529 530 531 532 533 534 535 536 537 538 539 540
				     c-paragraph-separate
				     "\\|"
				     page-delimiter)
	  paragraph-ignore-fill-prefix t
	  adaptive-fill-mode t
	  adaptive-fill-regexp
	  (concat comment-line-prefix
		  (if (default-value 'adaptive-fill-regexp)
		      (concat "\\("
			      (default-value 'adaptive-fill-regexp)
			      "\\)")
		    "")))
541

542
    (when (boundp 'adaptive-fill-first-line-regexp)
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
      ;; XEmacs adaptive fill mode doesn't have this.
      (make-local-variable 'adaptive-fill-first-line-regexp)
      (setq adaptive-fill-first-line-regexp
	    (concat "\\`" comment-line-prefix
		    ;; Maybe we should incorporate the old value here,
		    ;; but then we have to do all sorts of kludges to
		    ;; deal with the \` and \' it probably contains.
		    "\\'"))))

  ;; Set up the values for use in strings.  These are the default
  ;; paragraph-start/separate values, enhanced to accept escaped EOLs as
  ;; whitespace.  Used in c-beginning/end-of-sentence-in-string in cc-cmds.
  (setq c-string-par-start
	;;(concat "\\(" (default-value 'paragraph-start) "\\)\\|[ \t]*\\\\$"))
	"\f\\|[ \t]*\\\\?$")
  (setq c-string-par-separate
	;;(concat "\\(" (default-value 'paragraph-separate) "\\)\\|[ \t]*\\\\$"))
	"[ \t\f]*\\\\?$")
  (setq c-sentence-end-with-esc-eol
	(concat "\\(\\(" (c-default-value-sentence-end) "\\)"
Juanma Barranquero's avatar
Juanma Barranquero committed
563
		;; N.B.:  "$" would be invalid when not enclosed like "\\($\\)".
564 565
		"\\|" "[.?!][]\"')}]* ?\\\\\\($\\)[ \t\n]*"
		"\\)")))
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581


;; Helper for setting up Filladapt mode.  It's not used by CC Mode itself.

(cc-bytecomp-defvar filladapt-token-table)
(cc-bytecomp-defvar filladapt-token-match-table)
(cc-bytecomp-defvar filladapt-token-conversion-table)

(defun c-setup-filladapt ()
  "Convenience function to configure Kyle E. Jones' Filladapt mode for
CC Mode by making sure the proper entries are present on
`filladapt-token-table', `filladapt-token-match-table', and
`filladapt-token-conversion-table'.  This is intended to be used on
`c-mode-common-hook' or similar."
  ;; This function is intended to be used explicitly by the end user
  ;; only.
582

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
  ;; The default configuration already handles C++ comments, but we
  ;; need to add handling of C block comments.  A new filladapt token
  ;; `c-comment' is added for that.
  (let (p)
    (setq p filladapt-token-table)
    (while (and p (not (eq (car-safe (cdr-safe (car-safe p))) 'c-comment)))
      (setq p (cdr-safe p)))
    (if p
	(setcar (car p) c-current-comment-prefix)
      (setq filladapt-token-table
	    (append (list (car filladapt-token-table)
			  (list c-current-comment-prefix 'c-comment))
		    (cdr filladapt-token-table)))))
  (unless (assq 'c-comment filladapt-token-match-table)
    (setq filladapt-token-match-table
	  (append '((c-comment c-comment))
		  filladapt-token-match-table)))
  (unless (assq 'c-comment filladapt-token-conversion-table)
    (setq filladapt-token-conversion-table
	  (append '((c-comment . exact))
		  filladapt-token-conversion-table))))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
604

Gerd Moellmann's avatar
Gerd Moellmann committed
605

Richard M. Stallman's avatar
Richard M. Stallman committed
606 607 608 609 610
(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
611 612
  (unless (get 'c-initialize-builtin-style 'is-run)
    (put 'c-initialize-builtin-style 'is-run t)
Gerd Moellmann's avatar
Gerd Moellmann committed
613
    ;;(c-initialize-cc-mode)
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
    (unless (assoc "user" c-style-alist)
      (let ((vars c-style-variables) var val uservars)
	(while vars
	  (setq var (car vars)
		val (symbol-value var)
		vars (cdr vars))
	  (cond ((eq var 'c-offsets-alist)
		 (or (null val)
		     (setq uservars (cons (cons 'c-offsets-alist val)
					  uservars))))
		((not (eq val 'set-from-style))
		 (setq uservars (cons (cons var val)
				      uservars)))))
	(c-add-style "user" uservars)))
    (unless (assoc "cc-mode" c-style-alist)
      (c-add-style "cc-mode" '("user")))
Barry A. Warsaw's avatar
Barry A. Warsaw committed
630 631
    (if c-style-variables-are-local-p
	(c-make-styles-buffer-local))))
632

633
(defun c-make-styles-buffer-local (&optional this-buf-only-p)
Richard M. Stallman's avatar
Richard M. Stallman committed
634
  "Make all CC Mode style variables buffer local.
635 636
If `this-buf-only-p' is non-nil, the style variables will be made
buffer local only in the current buffer.  Otherwise they'll be made
637
permanently buffer local in any buffer that changes their values.
638 639 640 641 642

The buffer localness of the style variables are normally controlled
with the variable `c-style-variables-are-local-p', so there's seldom
any reason to call this function directly."

Richard M. Stallman's avatar
Richard M. Stallman committed
643
  ;; style variables
644 645 646
  (let ((func (if this-buf-only-p
		  'make-local-variable
		'make-variable-buffer-local))
Gerd Moellmann's avatar
Gerd Moellmann committed
647 648
	(varsyms (cons 'c-indentation-style (copy-alist c-style-variables))))
    (delq 'c-special-indent-hook varsyms)
Glenn Morris's avatar
Glenn Morris committed
649
    (mapc func varsyms)
650 651 652
    ;; Hooks must be handled specially
    (if this-buf-only-p
	(make-local-hook 'c-special-indent-hook)
653
      (with-no-warnings (make-variable-buffer-local 'c-special-indent-hook))
Gerd Moellmann's avatar
Gerd Moellmann committed
654
      (setq c-style-variables-are-local-p t))
655 656
    ))

Richard M. Stallman's avatar
Richard M. Stallman committed
657 658


Gerd Moellmann's avatar
Gerd Moellmann committed
659
(cc-provide 'cc-styles)
660

661
;; arch-tag: c764f61a-96ba-484a-a68f-101c0e9d5d2c
Richard M. Stallman's avatar
Richard M. Stallman committed
662
;;; cc-styles.el ends here