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