Mercurial > emacs
annotate lisp/progmodes/cc-styles.el @ 19538:5fd95475742b
(gnus-group-make-useful-group): Don't use caddr in interactive.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 26 Aug 1997 06:30:41 +0000 |
parents | 6d5536cedd70 |
children | 86412da1174b |
rev | line source |
---|---|
18720 | 1 ;;; cc-styles.el --- support for styles in CC Mode |
2 | |
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc. | |
4 | |
5 ;; Authors: 1992-1997 Barry A. Warsaw | |
6 ;; 1987 Dave Detlefs and Stewart Clamen | |
7 ;; 1985 Richard M. Stallman | |
8 ;; Maintainer: cc-mode-help@python.org | |
9 ;; Created: 22-Apr-1997 (split from cc-mode.el) | |
19376
6d5536cedd70
(c-style-alist): "python" style requires c-comment-continuation-stars be "".
Richard M. Stallman <rms@gnu.org>
parents:
19349
diff
changeset
|
10 ;; Version: 5.16 |
18720 | 11 ;; Keywords: c languages oop |
12 | |
13 ;; This file is part of GNU Emacs. | |
14 | |
15 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
16 ;; it under the terms of the GNU General Public License as published by | |
17 ;; the Free Software Foundation; either version 2, or (at your option) | |
18 ;; any later version. | |
19 | |
20 ;; GNU Emacs is distributed in the hope that it will be useful, | |
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 ;; GNU General Public License for more details. | |
24 | |
25 ;; You should have received a copy of the GNU General Public License | |
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
28 ;; Boston, MA 02111-1307, USA. | |
29 | |
30 | |
31 | |
32 (defconst c-style-alist | |
33 '(("gnu" | |
34 (c-basic-offset . 2) | |
35 (c-comment-only-line-offset . (0 . 0)) | |
36 (c-offsets-alist . ((statement-block-intro . +) | |
37 (knr-argdecl-intro . 5) | |
38 (substatement-open . +) | |
39 (label . 0) | |
40 (statement-case-open . +) | |
41 (statement-cont . +) | |
42 (arglist-intro . c-lineup-arglist-intro-after-paren) | |
43 (arglist-close . c-lineup-arglist) | |
44 )) | |
45 (c-special-indent-hook . c-gnu-impose-minimum) | |
19349 | 46 (c-comment-continuation-stars . "") |
47 (c-hanging-comment-ender-p . t) | |
18720 | 48 ) |
49 ("k&r" | |
50 (c-basic-offset . 5) | |
51 (c-comment-only-line-offset . 0) | |
52 (c-offsets-alist . ((statement-block-intro . +) | |
53 (knr-argdecl-intro . 0) | |
54 (substatement-open . 0) | |
55 (label . 0) | |
56 (statement-cont . +) | |
57 )) | |
58 ) | |
59 ("bsd" | |
60 (c-basic-offset . 4) | |
61 (c-comment-only-line-offset . 0) | |
62 (c-offsets-alist . ((statement-block-intro . +) | |
63 (knr-argdecl-intro . +) | |
64 (substatement-open . 0) | |
65 (label . 0) | |
66 (statement-cont . +) | |
67 )) | |
68 ) | |
69 ("stroustrup" | |
70 (c-basic-offset . 4) | |
71 (c-comment-only-line-offset . 0) | |
72 (c-offsets-alist . ((statement-block-intro . +) | |
73 (substatement-open . 0) | |
74 (label . 0) | |
75 (statement-cont . +) | |
76 )) | |
77 ) | |
78 ("whitesmith" | |
79 (c-basic-offset . 4) | |
80 (c-comment-only-line-offset . 0) | |
81 (c-offsets-alist . ((statement-block-intro . +) | |
82 (knr-argdecl-intro . +) | |
83 (substatement-open . 0) | |
84 (label . 0) | |
85 (statement-cont . +) | |
86 )) | |
87 | |
88 ) | |
89 ("ellemtel" | |
90 (c-basic-offset . 3) | |
91 (c-comment-only-line-offset . 0) | |
92 (c-hanging-braces-alist . ((substatement-open before after))) | |
93 (c-offsets-alist . ((topmost-intro . 0) | |
94 (topmost-intro-cont . 0) | |
95 (substatement . +) | |
96 (substatement-open . 0) | |
97 (case-label . +) | |
98 (access-label . -) | |
99 (inclass . ++) | |
100 (inline-open . 0) | |
101 )) | |
102 ) | |
103 ("linux" | |
104 (c-basic-offset . 8) | |
105 (c-comment-only-line-offset . 0) | |
106 (c-hanging-braces-alist . ((brace-list-open) | |
107 (substatement-open after) | |
108 (block-close . c-snug-do-while))) | |
109 (c-cleanup-list . (brace-else-brace)) | |
110 (c-offsets-alist . ((statement-block-intro . +) | |
111 (knr-argdecl-intro . 0) | |
112 (substatement-open . 0) | |
113 (label . 0) | |
114 (statement-cont . +) | |
115 )) | |
116 ) | |
117 ("python" | |
118 (indent-tabs-mode . t) | |
119 (fill-column . 72) | |
120 (c-basic-offset . 8) | |
121 (c-offsets-alist . ((substatement-open . 0) | |
122 )) | |
123 (c-hanging-braces-alist . ((brace-list-open) | |
124 (brace-list-intro) | |
125 (brace-list-close) | |
126 (substatement-open after) | |
127 (block-close . c-snug-do-while) | |
128 )) | |
19376
6d5536cedd70
(c-style-alist): "python" style requires c-comment-continuation-stars be "".
Richard M. Stallman <rms@gnu.org>
parents:
19349
diff
changeset
|
129 (c-comment-continuation-stars "") |
18720 | 130 ) |
131 ("java" | |
132 (c-basic-offset . 2) | |
133 (c-comment-only-line-offset . (0 . 0)) | |
19254
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
134 ;; the following preserves Javadoc starter lines |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
135 (c-hanging-comment-starter-p . nil) |
18720 | 136 (c-offsets-alist . ((topmost-intro-cont . +) |
137 (statement-block-intro . +) | |
138 (knr-argdecl-intro . 5) | |
139 (substatement-open . +) | |
140 (label . 0) | |
141 (statement-case-open . +) | |
142 (statement-cont . +) | |
143 (arglist-intro . c-lineup-arglist-intro-after-paren) | |
144 (arglist-close . c-lineup-arglist) | |
145 (access-label . 0) | |
146 (inher-cont . c-lineup-java-inher) | |
147 (func-decl-cont . c-lineup-java-throws) | |
148 )) | |
149 | |
150 ) | |
151 ) | |
152 "Styles of indentation. | |
153 Elements of this alist are of the form: | |
154 | |
155 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...]) | |
156 | |
157 where STYLE-STRING is a short descriptive string used to select a | |
158 style, VARIABLE is any Emacs variable, and VALUE is the intended value | |
159 for that variable when using the selected style. | |
160 | |
161 Optional BASE-STYLE if present, is a string and must follow | |
162 STYLE-STRING. BASE-STYLE names a style that this style inherits from. | |
163 By default, all styles inherit from the \"cc-mode\" style, which is | |
164 computed at run time. Style loops generate errors. | |
165 | |
166 Two variables are treated specially. When VARIABLE is | |
167 `c-offsets-alist', the VALUE is a list containing elements of the | |
168 form: | |
169 | |
170 (SYNTACTIC-SYMBOL . OFFSET) | |
171 | |
172 as described in `c-offsets-alist'. These are passed directly to | |
173 `c-set-offset' so there is no need to set every syntactic symbol in | |
174 your style, only those that are different from the default. | |
175 | |
176 When VARIABLE is `c-special-indent-hook', its VALUE is added to | |
177 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each | |
178 element of the list is added with `add-hook'. | |
179 | |
180 Do not change this variable directly. Use the function `c-add-style' | |
181 to add new styles or modify existing styles (it is not a good idea to | |
182 modify existing styles -- you should create a new style that inherits | |
183 the existing style.") | |
184 | |
185 | |
186 ;; Functions that manipulate styles | |
187 (defun c-set-style-1 (conscell) | |
188 ;; Set the style for one variable | |
189 (let ((attr (car conscell)) | |
190 (val (cdr conscell))) | |
191 (cond | |
192 ;; first special variable | |
193 ((eq attr 'c-offsets-alist) | |
194 (mapcar | |
195 (function | |
196 (lambda (langentry) | |
197 (let ((langelem (car langentry)) | |
198 (offset (cdr langentry))) | |
199 (c-set-offset langelem offset) | |
200 ))) | |
201 val)) | |
202 ;; second special variable | |
203 ((eq attr 'c-special-indent-hook) | |
204 (if (listp val) | |
205 (while val | |
206 (add-hook 'c-special-indent-hook (car val)) | |
207 (setq val (cdr val))) | |
208 (add-hook 'c-special-indent-hook val))) | |
209 ;; all other variables | |
210 (t (set attr val))) | |
211 )) | |
212 | |
213 (defun c-set-style-2 (style basestyles) | |
214 ;; Recursively set the base style. If no base style is given, the | |
215 ;; default base style is "cc-mode" and the recursion stops. Be sure | |
216 ;; to detect loops. | |
217 (let ((vars (cdr (or (assoc (downcase style) c-style-alist) | |
218 (assoc (upcase style) c-style-alist) | |
219 (assoc style c-style-alist) | |
220 (error "Undefined style: %s" style))))) | |
19254
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
221 (if (not (string-equal style "cc-mode")) |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
222 (let ((base (if (stringp (car vars)) |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
223 (prog1 |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
224 (downcase (car vars)) |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
225 (setq vars (cdr vars))) |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
226 "cc-mode"))) |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
227 (if (memq base basestyles) |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
228 (error "Style loop detected: %s in %s" base basestyles)) |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
229 (c-set-style-2 base (cons base basestyles)))) |
18720 | 230 (mapcar 'c-set-style-1 vars))) |
231 | |
232 (defvar c-set-style-history nil) | |
233 | |
234 ;;;###autoload | |
235 (defun c-set-style (stylename) | |
236 "Set CC Mode variables to use one of several different indentation styles. | |
237 STYLENAME is a string representing the desired style from the list of | |
238 styles described in the variable `c-style-alist'. See that variable | |
239 for details of setting up styles. | |
240 | |
241 The variable `c-indentation-style' always contains the buffer's current | |
242 style name." | |
243 (interactive (list (let ((completion-ignore-case t) | |
244 (prompt (format "Which %s indentation style? " | |
245 mode-name))) | |
246 (completing-read prompt c-style-alist nil t | |
247 (cons c-indentation-style 0) | |
248 'c-set-style-history)))) | |
19254
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
249 (c-initialize-builtin-style) |
18720 | 250 (c-set-style-2 stylename nil) |
251 (setq c-indentation-style stylename) | |
252 (c-keep-region-active)) | |
253 | |
254 ;;;###autoload | |
255 (defun c-add-style (style descrip &optional set-p) | |
256 "Adds a style to `c-style-alist', or updates an existing one. | |
257 STYLE is a string identifying the style to add or update. DESCRIP is | |
258 an association list describing the style and must be of the form: | |
259 | |
260 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...]) | |
261 | |
262 See the variable `c-style-alist' for the semantics of BASESTYLE, | |
263 VARIABLE and VALUE. This function also sets the current style to | |
264 STYLE using `c-set-style' if the optional SET-P flag is non-nil." | |
265 (interactive | |
266 (let ((stylename (completing-read "Style to add: " c-style-alist | |
267 nil nil nil 'c-set-style-history)) | |
268 (description (eval-minibuffer "Style description: "))) | |
269 (list stylename description | |
270 (y-or-n-p "Set the style too? ")))) | |
271 (setq style (downcase style)) | |
272 (let ((s (assoc style c-style-alist))) | |
273 (if s | |
274 (setcdr s (copy-alist descrip)) ; replace | |
275 (setq c-style-alist (cons (cons style descrip) c-style-alist)))) | |
276 (and set-p (c-set-style style))) | |
277 | |
278 | |
279 | |
280 (defconst c-offsets-alist | |
281 '((string . -1000) | |
282 (c . c-lineup-C-comments) | |
283 (defun-open . 0) | |
284 (defun-close . 0) | |
285 (defun-block-intro . +) | |
286 (class-open . 0) | |
287 (class-close . 0) | |
288 (inline-open . +) | |
289 (inline-close . 0) | |
290 (func-decl-cont . +) | |
291 (knr-argdecl-intro . +) | |
292 (knr-argdecl . 0) | |
293 (topmost-intro . 0) | |
294 (topmost-intro-cont . 0) | |
295 (member-init-intro . +) | |
296 (member-init-cont . 0) | |
297 (inher-intro . +) | |
298 (inher-cont . c-lineup-multi-inher) | |
299 (block-open . 0) | |
300 (block-close . 0) | |
301 (brace-list-open . 0) | |
302 (brace-list-close . 0) | |
303 (brace-list-intro . +) | |
304 (brace-list-entry . 0) | |
305 (statement . 0) | |
306 ;; some people might prefer | |
307 ;;(statement . c-lineup-runin-statements) | |
308 (statement-cont . +) | |
309 ;; some people might prefer | |
310 ;;(statement-cont . c-lineup-math) | |
311 (statement-block-intro . +) | |
312 (statement-case-intro . +) | |
313 (statement-case-open . 0) | |
314 (substatement . +) | |
315 (substatement-open . +) | |
316 (case-label . 0) | |
317 (access-label . -) | |
318 (label . 2) | |
319 (do-while-closure . 0) | |
320 (else-clause . 0) | |
321 (comment-intro . c-lineup-comment) | |
322 (arglist-intro . +) | |
323 (arglist-cont . 0) | |
324 (arglist-cont-nonempty . c-lineup-arglist) | |
325 (arglist-close . +) | |
326 (stream-op . c-lineup-streamop) | |
327 (inclass . +) | |
328 (cpp-macro . -1000) | |
329 (friend . 0) | |
330 (objc-method-intro . -1000) | |
331 (objc-method-args-cont . c-lineup-ObjC-method-args) | |
332 (objc-method-call-cont . c-lineup-ObjC-method-call) | |
333 (extern-lang-open . 0) | |
334 (extern-lang-close . 0) | |
335 (inextern-lang . +) | |
19302
55c870186c51
(c-offsets-alist): Added template-args-cont syntactic symbol.
Richard M. Stallman <rms@gnu.org>
parents:
19254
diff
changeset
|
336 (template-args-cont . +) |
18720 | 337 ) |
338 "Association list of syntactic element symbols and indentation offsets. | |
339 As described below, each cons cell in this list has the form: | |
340 | |
341 (SYNTACTIC-SYMBOL . OFFSET) | |
342 | |
343 When a line is indented, CC Mode first determines the syntactic | |
344 context of the line by generating a list of symbols called syntactic | |
345 elements. This list can contain more than one syntactic element and | |
346 the global variable `c-syntactic-context' contains the context list | |
347 for the line being indented. Each element in this list is actually a | |
348 cons cell of the syntactic symbol and a buffer position. This buffer | |
349 position is called the relative indent point for the line. Some | |
350 syntactic symbols may not have a relative indent point associated with | |
351 them. | |
352 | |
353 After the syntactic context list for a line is generated, CC Mode | |
354 calculates the absolute indentation for the line by looking at each | |
355 syntactic element in the list. First, it compares the syntactic | |
356 element against the SYNTACTIC-SYMBOL's in `c-offsets-alist'. When it | |
357 finds a match, it adds the OFFSET to the column of the relative indent | |
358 point. The sum of this calculation for each element in the syntactic | |
359 list is the absolute offset for line being indented. | |
360 | |
361 If the syntactic element does not match any in the `c-offsets-alist', | |
362 an error is generated if `c-strict-syntax-p' is non-nil, otherwise the | |
363 element is ignored. | |
364 | |
365 Actually, OFFSET can be an integer, a function, a variable, or one of | |
366 the following symbols: `+', `-', `++', `--', `*', or `/'. These | |
367 latter designate positive or negative multiples of `c-basic-offset', | |
368 respectively: 1, -1, 2, -2, 0.5, and -0.5. If OFFSET is a function, it | |
369 is called with a single argument containing the cons of the syntactic | |
370 element symbol and the relative indent point. The function should | |
371 return an integer offset. | |
372 | |
373 Here is the current list of valid syntactic element symbols: | |
374 | |
375 string -- inside multi-line string | |
376 c -- inside a multi-line C style block comment | |
377 defun-open -- brace that opens a function definition | |
378 defun-close -- brace that closes a function definition | |
379 defun-block-intro -- the first line in a top-level defun | |
380 class-open -- brace that opens a class definition | |
381 class-close -- brace that closes a class definition | |
382 inline-open -- brace that opens an in-class inline method | |
383 inline-close -- brace that closes an in-class inline method | |
384 func-decl-cont -- the region between a function definition's | |
385 argument list and the function opening brace | |
386 (excluding K&R argument declarations). In C, you | |
387 cannot put anything but whitespace and comments | |
388 between them; in C++ and Java, throws declarations | |
389 and other things can appear in this context. | |
390 knr-argdecl-intro -- first line of a K&R C argument declaration | |
391 knr-argdecl -- subsequent lines in a K&R C argument declaration | |
392 topmost-intro -- the first line in a topmost construct definition | |
393 topmost-intro-cont -- topmost definition continuation lines | |
394 member-init-intro -- first line in a member initialization list | |
395 member-init-cont -- subsequent member initialization list lines | |
396 inher-intro -- first line of a multiple inheritance list | |
397 inher-cont -- subsequent multiple inheritance lines | |
398 block-open -- statement block open brace | |
399 block-close -- statement block close brace | |
400 brace-list-open -- open brace of an enum or static array list | |
401 brace-list-close -- close brace of an enum or static array list | |
402 brace-list-intro -- first line in an enum or static array list | |
403 brace-list-entry -- subsequent lines in an enum or static array list | |
404 statement -- a C (or like) statement | |
405 statement-cont -- a continuation of a C (or like) statement | |
406 statement-block-intro -- the first line in a new statement block | |
407 statement-case-intro -- the first line in a case \"block\" | |
408 statement-case-open -- the first line in a case block starting with brace | |
409 substatement -- the first line after an if/while/for/do/else | |
410 substatement-open -- the brace that opens a substatement block | |
411 case-label -- a `case' or `default' label | |
412 access-label -- C++ private/protected/public access label | |
413 label -- any ordinary label | |
414 do-while-closure -- the `while' that ends a do/while construct | |
415 else-clause -- the `else' of an if/else construct | |
416 comment-intro -- a line containing only a comment introduction | |
417 arglist-intro -- the first line in an argument list | |
418 arglist-cont -- subsequent argument list lines when no | |
419 arguments follow on the same line as the | |
420 arglist opening paren | |
421 arglist-cont-nonempty -- subsequent argument list lines when at | |
422 least one argument follows on the same | |
423 line as the arglist opening paren | |
424 arglist-close -- the solo close paren of an argument list | |
425 stream-op -- lines continuing a stream operator construct | |
426 inclass -- the construct is nested inside a class definition | |
427 cpp-macro -- the start of a cpp macro | |
428 friend -- a C++ friend declaration | |
429 objc-method-intro -- the first line of an Objective-C method definition | |
430 objc-method-args-cont -- lines continuing an Objective-C method definition | |
431 objc-method-call-cont -- lines continuing an Objective-C method call | |
432 extern-lang-open -- brace that opens an external language block | |
433 extern-lang-close -- brace that closes an external language block | |
434 inextern-lang -- analogous to `inclass' syntactic symbol | |
19302
55c870186c51
(c-offsets-alist): Added template-args-cont syntactic symbol.
Richard M. Stallman <rms@gnu.org>
parents:
19254
diff
changeset
|
435 template-args-cont -- C++ template argument list continuations |
18720 | 436 ") |
437 | |
438 (defun c-get-offset (langelem) | |
439 ;; Get offset from LANGELEM which is a cons cell of the form: | |
440 ;; (SYMBOL . RELPOS). The symbol is matched against | |
441 ;; c-offsets-alist and the offset found there is either returned, | |
442 ;; or added to the indentation at RELPOS. If RELPOS is nil, then | |
443 ;; the offset is simply returned. | |
444 (let* ((symbol (car langelem)) | |
445 (relpos (cdr langelem)) | |
446 (match (assq symbol c-offsets-alist)) | |
447 (offset (cdr-safe match))) | |
448 ;; offset can be a number, a function, a variable, or one of the | |
449 ;; symbols + or - | |
450 (cond | |
451 ((not match) | |
452 (if c-strict-syntax-p | |
453 (error "don't know how to indent a %s" symbol) | |
454 (setq offset 0 | |
455 relpos 0))) | |
456 ((eq offset '+) (setq offset c-basic-offset)) | |
457 ((eq offset '-) (setq offset (- c-basic-offset))) | |
458 ((eq offset '++) (setq offset (* 2 c-basic-offset))) | |
459 ((eq offset '--) (setq offset (* 2 (- c-basic-offset)))) | |
460 ((eq offset '*) (setq offset (/ c-basic-offset 2))) | |
461 ((eq offset '/) (setq offset (/ (- c-basic-offset) 2))) | |
462 ((functionp offset) (setq offset (funcall offset langelem))) | |
463 ((not (numberp offset)) (setq offset (symbol-value offset))) | |
464 ) | |
465 (+ (if (and relpos | |
466 (< relpos (c-point 'bol))) | |
467 (save-excursion | |
468 (goto-char relpos) | |
469 (current-column)) | |
470 0) | |
471 offset))) | |
472 | |
473 | |
474 (defvar c-read-offset-history nil) | |
475 | |
476 (defun c-read-offset (langelem) | |
477 ;; read new offset value for LANGELEM from minibuffer. return a | |
478 ;; legal value only | |
479 (let* ((oldoff (cdr-safe (assq langelem c-offsets-alist))) | |
480 (defstr (format "(default %s): " oldoff)) | |
481 (errmsg (concat "Offset must be int, func, var, " | |
482 "or in [+,-,++,--,*,/] " | |
483 defstr)) | |
484 (prompt (concat "Offset " defstr)) | |
485 offset input interned raw) | |
486 (while (not offset) | |
487 (setq input (completing-read prompt obarray 'fboundp nil nil | |
488 'c-read-offset-history) | |
489 offset (cond ((string-equal "" input) oldoff) ; default | |
490 ((string-equal "+" input) '+) | |
491 ((string-equal "-" input) '-) | |
492 ((string-equal "++" input) '++) | |
493 ((string-equal "--" input) '--) | |
494 ((string-equal "*" input) '*) | |
495 ((string-equal "/" input) '/) | |
496 ((string-match "^-?[0-9]+$" input) | |
497 (string-to-int input)) | |
498 ;; a symbol with a function binding | |
499 ((fboundp (setq interned (intern input))) | |
500 interned) | |
501 ;; a lambda function | |
502 ((c-safe (functionp (setq raw (read input)))) | |
503 raw) | |
504 ;; a symbol with variable binding | |
505 ((boundp interned) interned) | |
506 ;; error, but don't signal one, keep trying | |
507 ;; to read an input value | |
508 (t (ding) | |
509 (setq prompt errmsg) | |
510 nil)))) | |
511 offset)) | |
512 | |
19212
024594beef65
(c-set-offset): Add autoload cookie.
Richard M. Stallman <rms@gnu.org>
parents:
18845
diff
changeset
|
513 ;;;###autoload |
18720 | 514 (defun c-set-offset (symbol offset &optional add-p) |
515 "Change the value of a syntactic element symbol in `c-offsets-alist'. | |
516 SYMBOL is the syntactic element symbol to change and OFFSET is the new | |
517 offset for that syntactic element. Optional ADD says to add SYMBOL to | |
518 `c-offsets-alist' if it doesn't already appear there." | |
519 (interactive | |
520 (let* ((langelem | |
521 (intern (completing-read | |
522 (concat "Syntactic symbol to change" | |
523 (if current-prefix-arg " or add" "") | |
524 ": ") | |
525 (mapcar | |
526 #'(lambda (langelem) | |
527 (cons (format "%s" (car langelem)) nil)) | |
528 c-offsets-alist) | |
529 nil (not current-prefix-arg) | |
530 ;; initial contents tries to be the last element | |
531 ;; on the syntactic analysis list for the current | |
532 ;; line | |
533 (let* ((syntax (c-guess-basic-syntax)) | |
534 (len (length syntax)) | |
535 (ic (format "%s" (car (nth (1- len) syntax))))) | |
536 (cons ic 0)) | |
537 ))) | |
538 (offset (c-read-offset langelem))) | |
539 (list langelem offset current-prefix-arg))) | |
540 ;; sanity check offset | |
541 (or (eq offset '+) | |
542 (eq offset '-) | |
543 (eq offset '++) | |
544 (eq offset '--) | |
545 (eq offset '*) | |
546 (eq offset '/) | |
547 (integerp offset) | |
548 (functionp offset) | |
549 (boundp offset) | |
550 (error "Offset must be int, func, var, or in [+,-,++,--,*,/]: %s" | |
551 offset)) | |
552 (let ((entry (assq symbol c-offsets-alist))) | |
553 (if entry | |
554 (setcdr entry offset) | |
555 (if add-p | |
556 (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist)) | |
557 (error "%s is not a valid syntactic symbol." symbol)))) | |
558 (c-keep-region-active)) | |
559 | |
560 | |
561 | |
562 (defun c-initialize-builtin-style () | |
563 ;; Dynamically append the default value of most variables. This is | |
564 ;; crucial because future c-set-style calls will always reset the | |
565 ;; variables first to the `cc-mode' style before instituting the new | |
566 ;; style. Only do this once! | |
567 (or (assoc "cc-mode" c-style-alist) | |
568 (progn | |
569 (c-add-style "cc-mode" | |
570 (mapcar | |
571 (function | |
572 (lambda (var) | |
573 (let ((val (symbol-value var))) | |
574 (cons var (if (atom val) val | |
18845
a64fd9cec691
(c-initialize-builtin-style): Use copy-sequence
Richard M. Stallman <rms@gnu.org>
parents:
18811
diff
changeset
|
575 (copy-sequence val))) |
18720 | 576 ))) |
577 '(c-backslash-column | |
578 c-basic-offset | |
579 c-cleanup-list | |
580 c-comment-only-line-offset | |
581 c-electric-pound-behavior | |
582 c-hanging-braces-alist | |
583 c-hanging-colons-alist | |
584 c-hanging-comment-starter-p | |
585 c-hanging-comment-ender-p | |
586 c-offsets-alist | |
587 ))) | |
588 ;; the default style is now GNU. This can be overridden in | |
589 ;; c-mode-common-hook or {c,c++,objc,java}-mode-hook. | |
19254
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
590 (c-set-style c-site-default-style))) |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
591 (if c-style-variables-are-local-p |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
592 (c-make-styles-buffer-local))) |
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
593 |
18720 | 594 |
595 (defun c-make-styles-buffer-local () | |
596 "Make all CC Mode style variables buffer local. | |
597 If you edit primarily one style of C (or C++, Objective-C, Java) code, | |
598 you probably want style variables to be global. This is the default. | |
599 | |
600 If you edit many different styles of C (or C++, Objective-C, Java) at | |
601 the same time, you probably want the CC Mode style variables to be | |
602 buffer local. If you do, then you will need to set any CC Mode style | |
603 variables in a hook function (e.g. off of c-mode-common-hook), instead | |
604 of at the top level of your ~/.emacs file. | |
605 | |
606 This function makes all the CC Mode style variables buffer local. | |
607 Call it after CC Mode is loaded into your Emacs environment. | |
608 Conversely, set the variable `c-style-variables-are-local-p' to t in | |
609 your .emacs file, before CC Mode is loaded, and this function will be | |
610 automatically called when CC Mode is loaded." | |
611 ;; style variables | |
612 (make-variable-buffer-local 'c-offsets-alist) | |
613 (make-variable-buffer-local 'c-basic-offset) | |
614 (make-variable-buffer-local 'c-file-style) | |
615 (make-variable-buffer-local 'c-file-offsets) | |
616 (make-variable-buffer-local 'c-comment-only-line-offset) | |
617 (make-variable-buffer-local 'c-cleanup-list) | |
618 (make-variable-buffer-local 'c-hanging-braces-alist) | |
619 (make-variable-buffer-local 'c-hanging-colons-alist) | |
620 (make-variable-buffer-local 'c-hanging-comment-starter-p) | |
621 (make-variable-buffer-local 'c-hanging-comment-ender-p) | |
622 (make-variable-buffer-local 'c-backslash-column) | |
623 (make-variable-buffer-local 'c-label-minimum-indentation) | |
624 (make-variable-buffer-local 'c-special-indent-hook) | |
625 (make-variable-buffer-local 'c-indentation-style)) | |
626 | |
627 | |
628 (provide 'cc-styles) | |
629 ;;; cc-styles.el ends here |