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