Commit | Line | Data |
---|---|---|
785eecbb RS |
1 | ;;; cc-styles.el --- support for styles in CC Mode |
2 | ||
92ab3834 | 3 | ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
ae940284 | 4 | ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
d7a0267c | 5 | ;; Free Software Foundation, Inc. |
785eecbb | 6 | |
e309f66c AM |
7 | ;; Authors: 2004- Alan Mackenzie |
8 | ;; 1998- Martin Stjernholm | |
d9e94c22 | 9 | ;; 1992-1999 Barry A. Warsaw |
785eecbb RS |
10 | ;; 1987 Dave Detlefs and Stewart Clamen |
11 | ;; 1985 Richard M. Stallman | |
0ec8351b | 12 | ;; Maintainer: bug-cc-mode@gnu.org |
785eecbb | 13 | ;; Created: 22-Apr-1997 (split from cc-mode.el) |
82aba9f4 | 14 | ;; Version: See cc-mode.el |
785eecbb RS |
15 | ;; Keywords: c languages oop |
16 | ||
17 | ;; This file is part of GNU Emacs. | |
18 | ||
b1fc2b50 | 19 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
785eecbb | 20 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
21 | ;; the Free Software Foundation, either version 3 of the License, or |
22 | ;; (at your option) any later version. | |
785eecbb RS |
23 | |
24 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
25 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | ;; GNU General Public License for more details. | |
28 | ||
29 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 30 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
785eecbb | 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))) |
d9e94c22 | 42 | (load "cc-bytecomp" nil t))) |
51f606de | 43 | |
130c507e GM |
44 | (cc-require 'cc-defs) |
45 | (cc-require 'cc-vars) | |
a66cd3ee MS |
46 | (cc-require 'cc-align) |
47 | ;; cc-align is only indirectly required: Styles added with | |
48 | ;; `c-add-style' often contains references to functions defined there. | |
49 | ||
50 | ;; Silence the compiler. | |
51 | (cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs | |
3efc2cd7 | 52 | (cc-bytecomp-obsolete-fun make-local-hook) ; Marked obsolete in Emacs 21.1. |
0ec8351b | 53 | |
785eecbb | 54 | \f |
2eb455ab | 55 | (defvar c-style-alist |
785eecbb RS |
56 | '(("gnu" |
57 | (c-basic-offset . 2) | |
58 | (c-comment-only-line-offset . (0 . 0)) | |
4fae8922 AM |
59 | (c-hanging-braces-alist . ((substatement-open before after) |
60 | (arglist-cont-nonempty))) | |
785eecbb RS |
61 | (c-offsets-alist . ((statement-block-intro . +) |
62 | (knr-argdecl-intro . 5) | |
63 | (substatement-open . +) | |
a66cd3ee | 64 | (substatement-label . 0) |
785eecbb RS |
65 | (label . 0) |
66 | (statement-case-open . +) | |
67 | (statement-cont . +) | |
68 | (arglist-intro . c-lineup-arglist-intro-after-paren) | |
69 | (arglist-close . c-lineup-arglist) | |
0ec8351b | 70 | (inline-open . 0) |
51c9af45 AM |
71 | (brace-list-open . +) |
72 | (topmost-intro-cont | |
73 | . (first c-lineup-topmost-intro-cont | |
74 | c-lineup-gnu-DEFUN-intro-cont)))) | |
785eecbb | 75 | (c-special-indent-hook . c-gnu-impose-minimum) |
0386b551 AM |
76 | (c-block-comment-prefix . "")) |
77 | ||
785eecbb RS |
78 | ("k&r" |
79 | (c-basic-offset . 5) | |
80 | (c-comment-only-line-offset . 0) | |
81 | (c-offsets-alist . ((statement-block-intro . +) | |
82 | (knr-argdecl-intro . 0) | |
83 | (substatement-open . 0) | |
a66cd3ee | 84 | (substatement-label . 0) |
785eecbb | 85 | (label . 0) |
0386b551 AM |
86 | (statement-cont . +)))) |
87 | ||
785eecbb | 88 | ("bsd" |
6cfd56b3 | 89 | (c-basic-offset . 8) |
785eecbb RS |
90 | (c-comment-only-line-offset . 0) |
91 | (c-offsets-alist . ((statement-block-intro . +) | |
92 | (knr-argdecl-intro . +) | |
93 | (substatement-open . 0) | |
a66cd3ee | 94 | (substatement-label . 0) |
785eecbb RS |
95 | (label . 0) |
96 | (statement-cont . +) | |
51f606de | 97 | (inline-open . 0) |
0386b551 AM |
98 | (inexpr-class . 0)))) |
99 | ||
785eecbb RS |
100 | ("stroustrup" |
101 | (c-basic-offset . 4) | |
102 | (c-comment-only-line-offset . 0) | |
103 | (c-offsets-alist . ((statement-block-intro . +) | |
104 | (substatement-open . 0) | |
a66cd3ee | 105 | (substatement-label . 0) |
785eecbb | 106 | (label . 0) |
0386b551 AM |
107 | (statement-cont . +)))) |
108 | ||
785eecbb RS |
109 | ("whitesmith" |
110 | (c-basic-offset . 4) | |
111 | (c-comment-only-line-offset . 0) | |
0386b551 AM |
112 | ;; It's obvious that the CC Mode way of choosing anchor positions |
113 | ;; doesn't fit this style at all. :P | |
114 | (c-offsets-alist . ((defun-open . +) | |
51f606de | 115 | (defun-close . c-lineup-whitesmith-in-block) |
0386b551 AM |
116 | (defun-block-intro . (add c-lineup-whitesmith-in-block |
117 | c-indent-multi-line-block)) | |
118 | (class-open . +) | |
119 | (class-close . +) | |
120 | (inline-open . +) | |
121 | (inline-close . c-lineup-whitesmith-in-block) | |
122 | (knr-argdecl-intro . +) | |
123 | (block-open . 0) ; Get indentation from `statement' instead. | |
124 | (block-close . c-lineup-whitesmith-in-block) | |
51f606de | 125 | (brace-list-open . +) |
51f606de | 126 | (brace-list-close . c-lineup-whitesmith-in-block) |
0386b551 AM |
127 | (brace-list-intro . (add c-lineup-whitesmith-in-block |
128 | c-indent-multi-line-block)) | |
129 | (brace-list-entry . (add c-lineup-after-whitesmith-blocks | |
130 | c-indent-multi-line-block)) | |
131 | (brace-entry-open . (add c-lineup-after-whitesmith-blocks | |
132 | c-indent-multi-line-block)) | |
133 | (statement . (add c-lineup-after-whitesmith-blocks | |
134 | c-indent-multi-line-block)) | |
135 | (statement-block-intro . (add c-lineup-whitesmith-in-block | |
136 | c-indent-multi-line-block)) | |
137 | (substatement-open . +) | |
138 | (substatement-label . +) | |
139 | (label . 0) | |
140 | (arglist-intro . (add c-lineup-whitesmith-in-block | |
141 | c-indent-multi-line-block)) | |
142 | (arglist-cont . (add c-lineup-after-whitesmith-blocks | |
143 | c-indent-multi-line-block)) | |
144 | (arglist-cont-nonempty . (add c-lineup-whitesmith-in-block | |
145 | c-indent-multi-line-block)) | |
146 | (arglist-close . c-lineup-whitesmith-in-block) | |
51f606de | 147 | (inclass . c-lineup-whitesmith-in-block) |
51f606de | 148 | (extern-lang-open . +) |
51f606de | 149 | (namespace-open . +) |
d9e94c22 | 150 | (module-open . +) |
d9e94c22 | 151 | (composition-open . +) |
0386b551 AM |
152 | (extern-lang-close . +) |
153 | (namespace-close . +) | |
154 | (module-close . +) | |
d9e94c22 | 155 | (composition-close . +) |
0386b551 AM |
156 | (inextern-lang . c-lineup-whitesmith-in-block) |
157 | (innamespace . c-lineup-whitesmith-in-block) | |
158 | (inmodule . c-lineup-whitesmith-in-block) | |
159 | (incomposition . c-lineup-whitesmith-in-block) | |
160 | (inexpr-class . 0)))) | |
161 | ||
785eecbb RS |
162 | ("ellemtel" |
163 | (c-basic-offset . 3) | |
164 | (c-comment-only-line-offset . 0) | |
0467478b CY |
165 | (c-hanging-braces-alist . ((substatement-open before after) |
166 | (arglist-cont-nonempty))) | |
785eecbb | 167 | (c-offsets-alist . ((topmost-intro . 0) |
a66cd3ee | 168 | (substatement . +) |
785eecbb RS |
169 | (substatement-open . 0) |
170 | (case-label . +) | |
171 | (access-label . -) | |
0467478b CY |
172 | (inclass . +) |
173 | (inline-open . 0)))) | |
785eecbb RS |
174 | ("linux" |
175 | (c-basic-offset . 8) | |
176 | (c-comment-only-line-offset . 0) | |
177 | (c-hanging-braces-alist . ((brace-list-open) | |
0ec8351b | 178 | (brace-entry-open) |
785eecbb | 179 | (substatement-open after) |
4fae8922 AM |
180 | (block-close . c-snug-do-while) |
181 | (arglist-cont-nonempty))) | |
785eecbb RS |
182 | (c-cleanup-list . (brace-else-brace)) |
183 | (c-offsets-alist . ((statement-block-intro . +) | |
184 | (knr-argdecl-intro . 0) | |
185 | (substatement-open . 0) | |
a66cd3ee | 186 | (substatement-label . 0) |
785eecbb | 187 | (label . 0) |
0386b551 AM |
188 | (statement-cont . +)))) |
189 | ||
785eecbb RS |
190 | ("python" |
191 | (indent-tabs-mode . t) | |
51f606de | 192 | (fill-column . 78) |
785eecbb RS |
193 | (c-basic-offset . 8) |
194 | (c-offsets-alist . ((substatement-open . 0) | |
62971612 RS |
195 | (inextern-lang . 0) |
196 | (arglist-intro . +) | |
0386b551 | 197 | (knr-argdecl-intro . +))) |
785eecbb RS |
198 | (c-hanging-braces-alist . ((brace-list-open) |
199 | (brace-list-intro) | |
200 | (brace-list-close) | |
0ec8351b | 201 | (brace-entry-open) |
785eecbb | 202 | (substatement-open after) |
4fae8922 AM |
203 | (block-close . c-snug-do-while) |
204 | (arglist-cont-nonempty))) | |
0386b551 AM |
205 | (c-block-comment-prefix . "")) |
206 | ||
785eecbb | 207 | ("java" |
0bacd8d0 | 208 | (c-basic-offset . 4) |
785eecbb | 209 | (c-comment-only-line-offset . (0 . 0)) |
63add9c9 | 210 | ;; the following preserves Javadoc starter lines |
0bacd8d0 RS |
211 | (c-offsets-alist . ((inline-open . 0) |
212 | (topmost-intro-cont . +) | |
785eecbb RS |
213 | (statement-block-intro . +) |
214 | (knr-argdecl-intro . 5) | |
a66cd3ee MS |
215 | (substatement-open . +) |
216 | (substatement-label . +) | |
51f606de | 217 | (label . +) |
785eecbb RS |
218 | (statement-case-open . +) |
219 | (statement-cont . +) | |
220 | (arglist-intro . c-lineup-arglist-intro-after-paren) | |
221 | (arglist-close . c-lineup-arglist) | |
222 | (access-label . 0) | |
223 | (inher-cont . c-lineup-java-inher) | |
0386b551 AM |
224 | (func-decl-cont . c-lineup-java-throws)))) |
225 | ||
226 | ;; awk style exists primarily for auto-newline settings. Otherwise it's | |
227 | ;; pretty much like k&r. | |
228 | ("awk" | |
229 | (c-basic-offset . 4) | |
230 | (c-comment-only-line-offset . 0) | |
231 | (c-hanging-braces-alist . ((defun-open after) | |
232 | (defun-close . c-snug-1line-defun-close) | |
233 | (substatement-open after) | |
4fae8922 AM |
234 | (block-close . c-snug-do-while) |
235 | (arglist-cont-nonempty))) | |
0386b551 AM |
236 | (c-hanging-semi&comma-criteria . nil) |
237 | (c-cleanup-list . nil) ; You might want one-liner-defun here. | |
238 | (c-offsets-alist . ((statement-block-intro . +) | |
239 | (substatement-open . 0) | |
240 | (statement-cont . +)))) | |
241 | ||
785eecbb RS |
242 | ) |
243 | "Styles of indentation. | |
244 | Elements of this alist are of the form: | |
245 | ||
246 | (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...]) | |
247 | ||
248 | where STYLE-STRING is a short descriptive string used to select a | |
249 | style, VARIABLE is any Emacs variable, and VALUE is the intended value | |
250 | for that variable when using the selected style. | |
251 | ||
252 | Optional BASE-STYLE if present, is a string and must follow | |
253 | STYLE-STRING. BASE-STYLE names a style that this style inherits from. | |
51f606de | 254 | By default, all styles inherit from the \"user\" style, which is |
785eecbb RS |
255 | computed at run time. Style loops generate errors. |
256 | ||
257 | Two variables are treated specially. When VARIABLE is | |
258 | `c-offsets-alist', the VALUE is a list containing elements of the | |
259 | form: | |
260 | ||
261 | (SYNTACTIC-SYMBOL . OFFSET) | |
262 | ||
263 | as described in `c-offsets-alist'. These are passed directly to | |
264 | `c-set-offset' so there is no need to set every syntactic symbol in | |
265 | your style, only those that are different from the default. | |
266 | ||
267 | When VARIABLE is `c-special-indent-hook', its VALUE is added to | |
268 | `c-special-indent-hook' using `add-hook'. If VALUE is a list, each | |
269 | element of the list is added with `add-hook'. | |
270 | ||
271 | Do not change this variable directly. Use the function `c-add-style' | |
272 | to add new styles or modify existing styles (it is not a good idea to | |
273 | modify existing styles -- you should create a new style that inherits | |
74456292 | 274 | the existing style).") |
785eecbb RS |
275 | |
276 | \f | |
277 | ;; Functions that manipulate styles | |
51f606de | 278 | (defun c-set-style-1 (conscell dont-override) |
785eecbb RS |
279 | ;; Set the style for one variable |
280 | (let ((attr (car conscell)) | |
281 | (val (cdr conscell))) | |
282 | (cond | |
283 | ;; first special variable | |
284 | ((eq attr 'c-offsets-alist) | |
6635cf6e MS |
285 | (let ((offsets (cond ((eq dont-override t) |
286 | c-offsets-alist) | |
287 | (dont-override | |
288 | (default-value 'c-offsets-alist))))) | |
289 | (mapcar (lambda (langentry) | |
290 | (let ((langelem (car langentry)) | |
291 | (offset (cdr langentry))) | |
292 | (unless (assq langelem offsets) | |
293 | (c-set-offset langelem offset)))) | |
294 | val))) | |
785eecbb RS |
295 | ;; second special variable |
296 | ((eq attr 'c-special-indent-hook) | |
6635cf6e MS |
297 | ;; Maybe we should ignore dont-override here and always add new |
298 | ;; hooks? | |
299 | (unless (cond ((eq dont-override t) | |
300 | c-special-indent-hook) | |
301 | (dont-override | |
302 | (default-value 'c-special-indent-hook))) | |
51f606de | 303 | (if (listp val) |
6635cf6e MS |
304 | (mapcar (lambda (func) |
305 | (add-hook 'c-special-indent-hook func t t)) | |
306 | val) | |
307 | (add-hook 'c-special-indent-hook val t t)))) | |
785eecbb | 308 | ;; all other variables |
a66cd3ee MS |
309 | (t (when (or (not dont-override) |
310 | (not (memq attr c-style-variables)) | |
6635cf6e MS |
311 | (eq (if (eq dont-override t) |
312 | (symbol-value attr) | |
313 | (default-value attr)) | |
314 | 'set-from-style)) | |
a66cd3ee MS |
315 | (set attr val) |
316 | ;; Must update a number of other variables if | |
317 | ;; c-comment-prefix-regexp is set. | |
318 | (if (eq attr 'c-comment-prefix-regexp) | |
319 | (c-setup-paragraph-variables))))))) | |
785eecbb | 320 | |
51f606de GM |
321 | (defun c-get-style-variables (style basestyles) |
322 | ;; Return all variables in a style by resolving inheritances. | |
a66cd3ee MS |
323 | (if (not style) |
324 | (copy-alist c-fallback-style) | |
325 | (let ((vars (cdr (or (assoc (downcase style) c-style-alist) | |
326 | (assoc (upcase style) c-style-alist) | |
327 | (assoc style c-style-alist) | |
328 | (progn | |
329 | (c-benign-error "Undefined style: %s" style) | |
330 | nil))))) | |
331 | (let ((base (and (stringp (car-safe vars)) | |
332 | (prog1 | |
333 | (downcase (car vars)) | |
334 | (setq vars (cdr vars)))))) | |
51f606de | 335 | (if (memq base basestyles) |
a66cd3ee MS |
336 | (c-benign-error "Style loop detected: %s in %s" base basestyles) |
337 | (nconc (c-get-style-variables base (cons base basestyles)) | |
338 | (copy-alist vars))))))) | |
51f606de | 339 | |
785eecbb RS |
340 | (defvar c-set-style-history nil) |
341 | ||
342 | ;;;###autoload | |
51f606de | 343 | (defun c-set-style (stylename &optional dont-override) |
0386b551 AM |
344 | "Set the current buffer to use the style STYLENAME. |
345 | STYLENAME, a string, must be an existing CC Mode style - These are contained | |
346 | in the variable `c-style-alist'. | |
347 | ||
348 | The variable `c-indentation-style' will get set to STYLENAME. | |
349 | ||
350 | \"Setting the style\" is done by setting CC Mode's \"style variables\" to the | |
351 | values indicated by the pertinent entry in `c-style-alist'. Other variables | |
352 | might get set too. | |
353 | ||
354 | If DONT-OVERRIDE is neither nil nor t, style variables whose default values | |
355 | have been set (more precisely, whose default values are not the symbol | |
356 | `set-from-style') will not be changed. This avoids overriding global settings | |
357 | done in ~/.emacs. It is useful to call c-set-style from a mode hook in this | |
358 | way. | |
359 | ||
360 | If DONT-OVERRIDE is t, style variables that already have values (i.e., whose | |
361 | values are not the symbol `set-from-style') will not be overridden. CC Mode | |
362 | calls c-set-style internally in this way whilst initializing a buffer; if | |
363 | cc-set-style is called like this from anywhere else, it will usually behave as | |
364 | a null operation." | |
a66cd3ee MS |
365 | (interactive |
366 | (list (let ((completion-ignore-case t) | |
367 | (prompt (format "Which %s indentation style? " | |
368 | mode-name))) | |
0386b551 AM |
369 | (completing-read prompt c-style-alist nil t nil |
370 | 'c-set-style-history | |
371 | c-indentation-style)))) | |
372 | (or c-buffer-is-cc-mode | |
373 | (error "Buffer %s is not a CC Mode buffer (c-set-style)" (buffer-name))) | |
2a15eb73 MS |
374 | (or (stringp stylename) |
375 | (error "Argument to c-set-style was not a string")) | |
63add9c9 | 376 | (c-initialize-builtin-style) |
51f606de | 377 | (let ((vars (c-get-style-variables stylename nil))) |
a66cd3ee MS |
378 | (unless dont-override |
379 | ;; Since we always add to c-special-indent-hook we must reset it | |
380 | ;; first, or else the hooks from the preceding style will | |
381 | ;; remain. This is not necessary for c-offsets-alist, since | |
382 | ;; c-get-style-variables contains every valid offset type in the | |
383 | ;; fallback entry. | |
384 | (setq c-special-indent-hook | |
385 | (default-value 'c-special-indent-hook))) | |
21a55d57 GM |
386 | (mapc (lambda (elem) |
387 | (c-set-style-1 elem dont-override)) | |
388 | ;; Need to go through the variables backwards when we | |
389 | ;; don't override any settings. | |
390 | (if (eq dont-override t) (nreverse vars) vars))) | |
785eecbb RS |
391 | (setq c-indentation-style stylename) |
392 | (c-keep-region-active)) | |
393 | ||
394 | ;;;###autoload | |
d9e94c22 | 395 | (defun c-add-style (style description &optional set-p) |
785eecbb | 396 | "Adds a style to `c-style-alist', or updates an existing one. |
d9e94c22 MS |
397 | STYLE is a string identifying the style to add or update. DESCRIPTION |
398 | is an association list describing the style and must be of the form: | |
785eecbb RS |
399 | |
400 | ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...]) | |
401 | ||
402 | See the variable `c-style-alist' for the semantics of BASESTYLE, | |
403 | VARIABLE and VALUE. This function also sets the current style to | |
404 | STYLE using `c-set-style' if the optional SET-P flag is non-nil." | |
405 | (interactive | |
406 | (let ((stylename (completing-read "Style to add: " c-style-alist | |
407 | nil nil nil 'c-set-style-history)) | |
d9e94c22 MS |
408 | (descr (eval-minibuffer "Style description: "))) |
409 | (list stylename descr | |
785eecbb RS |
410 | (y-or-n-p "Set the style too? ")))) |
411 | (setq style (downcase style)) | |
412 | (let ((s (assoc style c-style-alist))) | |
413 | (if s | |
d9e94c22 MS |
414 | (setcdr s (copy-alist description)) ; replace |
415 | (setq c-style-alist (cons (cons style description) c-style-alist)))) | |
785eecbb RS |
416 | (and set-p (c-set-style style))) |
417 | ||
785eecbb | 418 | \f |
785eecbb RS |
419 | (defvar c-read-offset-history nil) |
420 | ||
421 | (defun c-read-offset (langelem) | |
422 | ;; read new offset value for LANGELEM from minibuffer. return a | |
cb5bf6ba | 423 | ;; valid value only |
51f606de GM |
424 | (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist) |
425 | (assq langelem (get 'c-offsets-alist | |
426 | 'c-stylevar-fallback))))) | |
0ec8351b BW |
427 | (symname (symbol-name langelem)) |
428 | (defstr (format "(default %s): " oldoff)) | |
130c507e | 429 | (errmsg (concat "Offset must be int, func, var, vector, list, " |
0ec8351b BW |
430 | "or [+,-,++,--,*,/] " |
431 | defstr)) | |
432 | (prompt (concat symname " offset " defstr)) | |
a66cd3ee MS |
433 | (keymap (make-sparse-keymap)) |
434 | (minibuffer-completion-table obarray) | |
435 | (minibuffer-completion-predicate 'fboundp) | |
436 | offset input) | |
437 | ;; In principle completing-read is used here, but SPC is unbound | |
438 | ;; to make it less annoying to enter lists. | |
439 | (set-keymap-parent keymap minibuffer-local-completion-map) | |
440 | (define-key keymap " " 'self-insert-command) | |
785eecbb | 441 | (while (not offset) |
a66cd3ee MS |
442 | (setq input (read-from-minibuffer prompt nil keymap t |
443 | 'c-read-offset-history | |
444 | (format "%s" oldoff))) | |
445 | (if (c-valid-offset input) | |
446 | (setq offset input) | |
447 | ;; error, but don't signal one, keep trying | |
448 | ;; to read an input value | |
449 | (ding) | |
450 | (setq prompt errmsg))) | |
785eecbb RS |
451 | offset)) |
452 | ||
275a02d4 | 453 | ;;;###autoload |
51f606de | 454 | (defun c-set-offset (symbol offset &optional ignored) |
785eecbb RS |
455 | "Change the value of a syntactic element symbol in `c-offsets-alist'. |
456 | SYMBOL is the syntactic element symbol to change and OFFSET is the new | |
51f606de GM |
457 | offset for that syntactic element. The optional argument is not used |
458 | and exists only for compatibility reasons." | |
785eecbb RS |
459 | (interactive |
460 | (let* ((langelem | |
461 | (intern (completing-read | |
462 | (concat "Syntactic symbol to change" | |
463 | (if current-prefix-arg " or add" "") | |
464 | ": ") | |
465 | (mapcar | |
466 | #'(lambda (langelem) | |
467 | (cons (format "%s" (car langelem)) nil)) | |
51f606de | 468 | (get 'c-offsets-alist 'c-stylevar-fallback)) |
785eecbb RS |
469 | nil (not current-prefix-arg) |
470 | ;; initial contents tries to be the last element | |
471 | ;; on the syntactic analysis list for the current | |
472 | ;; line | |
ea703822 | 473 | (and c-buffer-is-cc-mode |
d9e94c22 MS |
474 | (c-save-buffer-state |
475 | ((syntax (c-guess-basic-syntax)) | |
476 | (len (length syntax)) | |
477 | (ic (format "%s" (car (nth (1- len) syntax))))) | |
ea703822 | 478 | (cons ic 0))) |
785eecbb RS |
479 | ))) |
480 | (offset (c-read-offset langelem))) | |
481 | (list langelem offset current-prefix-arg))) | |
482 | ;; sanity check offset | |
a66cd3ee MS |
483 | (if (c-valid-offset offset) |
484 | (let ((entry (assq symbol c-offsets-alist))) | |
485 | (if entry | |
486 | (setcdr entry offset) | |
487 | (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback)) | |
488 | (setq c-offsets-alist (cons (cons symbol offset) | |
489 | c-offsets-alist)) | |
490 | (c-benign-error "%s is not a valid syntactic symbol" symbol)))) | |
0386b551 | 491 | (c-benign-error "Invalid indentation setting for symbol %s: %S" |
a66cd3ee | 492 | symbol offset)) |
785eecbb RS |
493 | (c-keep-region-active)) |
494 | ||
a66cd3ee MS |
495 | \f |
496 | (defun c-setup-paragraph-variables () | |
0386b551 AM |
497 | "Fix things up for paragraph recognition and filling inside comments and |
498 | strings by incorporating the values of `c-comment-prefix-regexp', | |
499 | `sentence-end', `paragraph-start' and `paragraph-separate' in the relevant | |
a66cd3ee | 500 | variables." |
d9e94c22 | 501 | |
cfb966f4 | 502 | (interactive) |
0386b551 AM |
503 | (or c-buffer-is-cc-mode |
504 | (error "Buffer %s is not a CC Mode buffer (c-setup-paragraph-variables)" | |
505 | (buffer-name))) | |
506 | ;; Set up the values for use in comments. | |
a66cd3ee MS |
507 | (setq c-current-comment-prefix |
508 | (if (listp c-comment-prefix-regexp) | |
509 | (cdr-safe (or (assoc major-mode c-comment-prefix-regexp) | |
510 | (assoc 'other c-comment-prefix-regexp))) | |
511 | c-comment-prefix-regexp)) | |
cfb966f4 | 512 | |
49be4f88 AM |
513 | (let* ((empty-is-prefix (string-match c-current-comment-prefix "")) |
514 | (nonws-comment-line-prefix | |
515 | (concat "\\(" c-current-comment-prefix "\\)[ \t]*")) | |
516 | (comment-line-prefix (concat "[ \t]*" nonws-comment-line-prefix)) | |
517 | (blank-or-comment-line-prefix | |
518 | (concat "[ \t]*" | |
519 | (if empty-is-prefix "" "\\(") | |
520 | nonws-comment-line-prefix | |
521 | (if empty-is-prefix "" "\\)?")))) | |
522 | ||
523 | (setq paragraph-start (concat blank-or-comment-line-prefix | |
0386b551 AM |
524 | c-paragraph-start |
525 | "\\|" | |
526 | page-delimiter) | |
49be4f88 | 527 | paragraph-separate (concat blank-or-comment-line-prefix |
0386b551 AM |
528 | c-paragraph-separate |
529 | "\\|" | |
530 | page-delimiter) | |
531 | paragraph-ignore-fill-prefix t | |
532 | adaptive-fill-mode t | |
533 | adaptive-fill-regexp | |
534 | (concat comment-line-prefix | |
535 | (if (default-value 'adaptive-fill-regexp) | |
536 | (concat "\\(" | |
537 | (default-value 'adaptive-fill-regexp) | |
538 | "\\)") | |
539 | ""))) | |
cfb966f4 | 540 | |
a66cd3ee | 541 | (when (boundp 'adaptive-fill-first-line-regexp) |
0386b551 AM |
542 | ;; XEmacs adaptive fill mode doesn't have this. |
543 | (make-local-variable 'adaptive-fill-first-line-regexp) | |
544 | (setq adaptive-fill-first-line-regexp | |
545 | (concat "\\`" comment-line-prefix | |
546 | ;; Maybe we should incorporate the old value here, | |
547 | ;; but then we have to do all sorts of kludges to | |
548 | ;; deal with the \` and \' it probably contains. | |
549 | "\\'")))) | |
550 | ||
551 | ;; Set up the values for use in strings. These are the default | |
552 | ;; paragraph-start/separate values, enhanced to accept escaped EOLs as | |
553 | ;; whitespace. Used in c-beginning/end-of-sentence-in-string in cc-cmds. | |
554 | (setq c-string-par-start | |
555 | ;;(concat "\\(" (default-value 'paragraph-start) "\\)\\|[ \t]*\\\\$")) | |
556 | "\f\\|[ \t]*\\\\?$") | |
557 | (setq c-string-par-separate | |
558 | ;;(concat "\\(" (default-value 'paragraph-separate) "\\)\\|[ \t]*\\\\$")) | |
559 | "[ \t\f]*\\\\?$") | |
560 | (setq c-sentence-end-with-esc-eol | |
561 | (concat "\\(\\(" (c-default-value-sentence-end) "\\)" | |
cb5bf6ba | 562 | ;; N.B.: "$" would be invalid when not enclosed like "\\($\\)". |
0386b551 AM |
563 | "\\|" "[.?!][]\"')}]* ?\\\\\\($\\)[ \t\n]*" |
564 | "\\)"))) | |
a66cd3ee MS |
565 | |
566 | \f | |
567 | ;; Helper for setting up Filladapt mode. It's not used by CC Mode itself. | |
568 | ||
569 | (cc-bytecomp-defvar filladapt-token-table) | |
570 | (cc-bytecomp-defvar filladapt-token-match-table) | |
571 | (cc-bytecomp-defvar filladapt-token-conversion-table) | |
572 | ||
573 | (defun c-setup-filladapt () | |
574 | "Convenience function to configure Kyle E. Jones' Filladapt mode for | |
575 | CC Mode by making sure the proper entries are present on | |
576 | `filladapt-token-table', `filladapt-token-match-table', and | |
577 | `filladapt-token-conversion-table'. This is intended to be used on | |
578 | `c-mode-common-hook' or similar." | |
579 | ;; This function is intended to be used explicitly by the end user | |
580 | ;; only. | |
d9e94c22 | 581 | |
a66cd3ee MS |
582 | ;; The default configuration already handles C++ comments, but we |
583 | ;; need to add handling of C block comments. A new filladapt token | |
584 | ;; `c-comment' is added for that. | |
585 | (let (p) | |
586 | (setq p filladapt-token-table) | |
587 | (while (and p (not (eq (car-safe (cdr-safe (car-safe p))) 'c-comment))) | |
588 | (setq p (cdr-safe p))) | |
589 | (if p | |
590 | (setcar (car p) c-current-comment-prefix) | |
591 | (setq filladapt-token-table | |
592 | (append (list (car filladapt-token-table) | |
593 | (list c-current-comment-prefix 'c-comment)) | |
594 | (cdr filladapt-token-table))))) | |
595 | (unless (assq 'c-comment filladapt-token-match-table) | |
596 | (setq filladapt-token-match-table | |
597 | (append '((c-comment c-comment)) | |
598 | filladapt-token-match-table))) | |
599 | (unless (assq 'c-comment filladapt-token-conversion-table) | |
600 | (setq filladapt-token-conversion-table | |
601 | (append '((c-comment . exact)) | |
602 | filladapt-token-conversion-table)))) | |
0ec8351b | 603 | |
51f606de | 604 | \f |
785eecbb RS |
605 | (defun c-initialize-builtin-style () |
606 | ;; Dynamically append the default value of most variables. This is | |
607 | ;; crucial because future c-set-style calls will always reset the | |
608 | ;; variables first to the `cc-mode' style before instituting the new | |
609 | ;; style. Only do this once! | |
0ec8351b BW |
610 | (unless (get 'c-initialize-builtin-style 'is-run) |
611 | (put 'c-initialize-builtin-style 'is-run t) | |
130c507e | 612 | ;;(c-initialize-cc-mode) |
a66cd3ee MS |
613 | (unless (assoc "user" c-style-alist) |
614 | (let ((vars c-style-variables) var val uservars) | |
615 | (while vars | |
616 | (setq var (car vars) | |
617 | val (symbol-value var) | |
618 | vars (cdr vars)) | |
619 | (cond ((eq var 'c-offsets-alist) | |
620 | (or (null val) | |
621 | (setq uservars (cons (cons 'c-offsets-alist val) | |
622 | uservars)))) | |
623 | ((not (eq val 'set-from-style)) | |
624 | (setq uservars (cons (cons var val) | |
625 | uservars))))) | |
626 | (c-add-style "user" uservars))) | |
627 | (unless (assoc "cc-mode" c-style-alist) | |
628 | (c-add-style "cc-mode" '("user"))) | |
0ec8351b BW |
629 | (if c-style-variables-are-local-p |
630 | (c-make-styles-buffer-local)))) | |
63add9c9 | 631 | |
0bacd8d0 | 632 | (defun c-make-styles-buffer-local (&optional this-buf-only-p) |
785eecbb | 633 | "Make all CC Mode style variables buffer local. |
d9e94c22 MS |
634 | If `this-buf-only-p' is non-nil, the style variables will be made |
635 | buffer local only in the current buffer. Otherwise they'll be made | |
0386b551 | 636 | permanently buffer local in any buffer that changes their values. |
d9e94c22 MS |
637 | |
638 | The buffer localness of the style variables are normally controlled | |
639 | with the variable `c-style-variables-are-local-p', so there's seldom | |
640 | any reason to call this function directly." | |
d9e94c22 | 641 | |
785eecbb | 642 | ;; style variables |
0bacd8d0 RS |
643 | (let ((func (if this-buf-only-p |
644 | 'make-local-variable | |
645 | 'make-variable-buffer-local)) | |
51f606de GM |
646 | (varsyms (cons 'c-indentation-style (copy-alist c-style-variables)))) |
647 | (delq 'c-special-indent-hook varsyms) | |
21a55d57 | 648 | (mapc func varsyms) |
0bacd8d0 RS |
649 | ;; Hooks must be handled specially |
650 | (if this-buf-only-p | |
651 | (make-local-hook 'c-special-indent-hook) | |
0386b551 | 652 | (with-no-warnings (make-variable-buffer-local 'c-special-indent-hook)) |
51f606de | 653 | (setq c-style-variables-are-local-p t)) |
0bacd8d0 RS |
654 | )) |
655 | ||
785eecbb RS |
656 | |
657 | \f | |
130c507e | 658 | (cc-provide 'cc-styles) |
3afbc435 | 659 | |
cbee283d | 660 | ;; arch-tag: c764f61a-96ba-484a-a68f-101c0e9d5d2c |
785eecbb | 661 | ;;; cc-styles.el ends here |