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