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