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