165
|
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)
|
177
|
10 ;; Version: 5.14
|
165
|
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
|
167
|
30
|
165
|
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))
|
177
|
131 ;; the following preserves Javadoc starter lines
|
|
132 (c-hanging-comment-starter-p . nil)
|
165
|
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)))))
|
177
|
218 (if (not (string-equal style "cc-mode"))
|
|
219 (let ((base (if (stringp (car vars))
|
|
220 (prog1
|
|
221 (downcase (car vars))
|
|
222 (setq vars (cdr vars)))
|
|
223 "cc-mode")))
|
|
224 (if (memq base basestyles)
|
|
225 (error "Style loop detected: %s in %s" base basestyles))
|
|
226 (c-set-style-2 base (cons base basestyles))))
|
165
|
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))))
|
177
|
246 (c-initialize-builtin-style)
|
165
|
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 . +)
|
|
333 )
|
|
334 "Association list of syntactic element symbols and indentation offsets.
|
|
335 As described below, each cons cell in this list has the form:
|
|
336
|
|
337 (SYNTACTIC-SYMBOL . OFFSET)
|
|
338
|
|
339 When a line is indented, CC Mode first determines the syntactic
|
|
340 context of the line by generating a list of symbols called syntactic
|
|
341 elements. This list can contain more than one syntactic element and
|
|
342 the global variable `c-syntactic-context' contains the context list
|
|
343 for the line being indented. Each element in this list is actually a
|
|
344 cons cell of the syntactic symbol and a buffer position. This buffer
|
|
345 position is called the relative indent point for the line. Some
|
|
346 syntactic symbols may not have a relative indent point associated with
|
|
347 them.
|
|
348
|
|
349 After the syntactic context list for a line is generated, CC Mode
|
|
350 calculates the absolute indentation for the line by looking at each
|
|
351 syntactic element in the list. First, it compares the syntactic
|
|
352 element against the SYNTACTIC-SYMBOL's in `c-offsets-alist'. When it
|
|
353 finds a match, it adds the OFFSET to the column of the relative indent
|
|
354 point. The sum of this calculation for each element in the syntactic
|
|
355 list is the absolute offset for line being indented.
|
|
356
|
|
357 If the syntactic element does not match any in the `c-offsets-alist',
|
|
358 an error is generated if `c-strict-syntax-p' is non-nil, otherwise the
|
|
359 element is ignored.
|
|
360
|
|
361 Actually, OFFSET can be an integer, a function, a variable, or one of
|
|
362 the following symbols: `+', `-', `++', `--', `*', or `/'. These
|
|
363 latter designate positive or negative multiples of `c-basic-offset',
|
|
364 respectively: 1, -1, 2, -2, 0.5, and -0.5. If OFFSET is a function, it
|
|
365 is called with a single argument containing the cons of the syntactic
|
|
366 element symbol and the relative indent point. The function should
|
|
367 return an integer offset.
|
|
368
|
|
369 Here is the current list of valid syntactic element symbols:
|
|
370
|
|
371 string -- inside multi-line string
|
|
372 c -- inside a multi-line C style block comment
|
|
373 defun-open -- brace that opens a function definition
|
|
374 defun-close -- brace that closes a function definition
|
|
375 defun-block-intro -- the first line in a top-level defun
|
|
376 class-open -- brace that opens a class definition
|
|
377 class-close -- brace that closes a class definition
|
|
378 inline-open -- brace that opens an in-class inline method
|
|
379 inline-close -- brace that closes an in-class inline method
|
|
380 func-decl-cont -- the region between a function definition's
|
|
381 argument list and the function opening brace
|
|
382 (excluding K&R argument declarations). In C, you
|
|
383 cannot put anything but whitespace and comments
|
|
384 between them; in C++ and Java, throws declarations
|
|
385 and other things can appear in this context.
|
|
386 knr-argdecl-intro -- first line of a K&R C argument declaration
|
|
387 knr-argdecl -- subsequent lines in a K&R C argument declaration
|
|
388 topmost-intro -- the first line in a topmost construct definition
|
|
389 topmost-intro-cont -- topmost definition continuation lines
|
|
390 member-init-intro -- first line in a member initialization list
|
|
391 member-init-cont -- subsequent member initialization list lines
|
|
392 inher-intro -- first line of a multiple inheritance list
|
|
393 inher-cont -- subsequent multiple inheritance lines
|
|
394 block-open -- statement block open brace
|
|
395 block-close -- statement block close brace
|
|
396 brace-list-open -- open brace of an enum or static array list
|
|
397 brace-list-close -- close brace of an enum or static array list
|
|
398 brace-list-intro -- first line in an enum or static array list
|
|
399 brace-list-entry -- subsequent lines in an enum or static array list
|
|
400 statement -- a C (or like) statement
|
|
401 statement-cont -- a continuation of a C (or like) statement
|
|
402 statement-block-intro -- the first line in a new statement block
|
|
403 statement-case-intro -- the first line in a case \"block\"
|
|
404 statement-case-open -- the first line in a case block starting with brace
|
|
405 substatement -- the first line after an if/while/for/do/else
|
|
406 substatement-open -- the brace that opens a substatement block
|
|
407 case-label -- a `case' or `default' label
|
|
408 access-label -- C++ private/protected/public access label
|
|
409 label -- any ordinary label
|
|
410 do-while-closure -- the `while' that ends a do/while construct
|
|
411 else-clause -- the `else' of an if/else construct
|
|
412 comment-intro -- a line containing only a comment introduction
|
|
413 arglist-intro -- the first line in an argument list
|
|
414 arglist-cont -- subsequent argument list lines when no
|
|
415 arguments follow on the same line as the
|
|
416 arglist opening paren
|
|
417 arglist-cont-nonempty -- subsequent argument list lines when at
|
|
418 least one argument follows on the same
|
|
419 line as the arglist opening paren
|
|
420 arglist-close -- the solo close paren of an argument list
|
|
421 stream-op -- lines continuing a stream operator construct
|
|
422 inclass -- the construct is nested inside a class definition
|
|
423 cpp-macro -- the start of a cpp macro
|
|
424 friend -- a C++ friend declaration
|
|
425 objc-method-intro -- the first line of an Objective-C method definition
|
|
426 objc-method-args-cont -- lines continuing an Objective-C method definition
|
|
427 objc-method-call-cont -- lines continuing an Objective-C method call
|
|
428 extern-lang-open -- brace that opens an external language block
|
|
429 extern-lang-close -- brace that closes an external language block
|
|
430 inextern-lang -- analogous to `inclass' syntactic symbol
|
|
431 ")
|
|
432
|
|
433 (defun c-get-offset (langelem)
|
|
434 ;; Get offset from LANGELEM which is a cons cell of the form:
|
|
435 ;; (SYMBOL . RELPOS). The symbol is matched against
|
|
436 ;; c-offsets-alist and the offset found there is either returned,
|
|
437 ;; or added to the indentation at RELPOS. If RELPOS is nil, then
|
|
438 ;; the offset is simply returned.
|
|
439 (let* ((symbol (car langelem))
|
|
440 (relpos (cdr langelem))
|
|
441 (match (assq symbol c-offsets-alist))
|
|
442 (offset (cdr-safe match)))
|
|
443 ;; offset can be a number, a function, a variable, or one of the
|
|
444 ;; symbols + or -
|
|
445 (cond
|
|
446 ((not match)
|
|
447 (if c-strict-syntax-p
|
|
448 (error "don't know how to indent a %s" symbol)
|
|
449 (setq offset 0
|
|
450 relpos 0)))
|
|
451 ((eq offset '+) (setq offset c-basic-offset))
|
|
452 ((eq offset '-) (setq offset (- c-basic-offset)))
|
|
453 ((eq offset '++) (setq offset (* 2 c-basic-offset)))
|
|
454 ((eq offset '--) (setq offset (* 2 (- c-basic-offset))))
|
|
455 ((eq offset '*) (setq offset (/ c-basic-offset 2)))
|
|
456 ((eq offset '/) (setq offset (/ (- c-basic-offset) 2)))
|
|
457 ((functionp offset) (setq offset (funcall offset langelem)))
|
|
458 ((not (numberp offset)) (setq offset (symbol-value offset)))
|
|
459 )
|
|
460 (+ (if (and relpos
|
|
461 (< relpos (c-point 'bol)))
|
|
462 (save-excursion
|
|
463 (goto-char relpos)
|
|
464 (current-column))
|
|
465 0)
|
|
466 offset)))
|
|
467
|
|
468
|
|
469 (defvar c-read-offset-history nil)
|
|
470
|
|
471 (defun c-read-offset (langelem)
|
|
472 ;; read new offset value for LANGELEM from minibuffer. return a
|
|
473 ;; legal value only
|
|
474 (let* ((oldoff (cdr-safe (assq langelem c-offsets-alist)))
|
|
475 (defstr (format "(default %s): " oldoff))
|
|
476 (errmsg (concat "Offset must be int, func, var, "
|
|
477 "or in [+,-,++,--,*,/] "
|
|
478 defstr))
|
|
479 (prompt (concat "Offset " defstr))
|
|
480 offset input interned raw)
|
|
481 (while (not offset)
|
|
482 (setq input (completing-read prompt obarray 'fboundp nil nil
|
|
483 'c-read-offset-history)
|
|
484 offset (cond ((string-equal "" input) oldoff) ; default
|
|
485 ((string-equal "+" input) '+)
|
|
486 ((string-equal "-" input) '-)
|
|
487 ((string-equal "++" input) '++)
|
|
488 ((string-equal "--" input) '--)
|
|
489 ((string-equal "*" input) '*)
|
|
490 ((string-equal "/" input) '/)
|
|
491 ((string-match "^-?[0-9]+$" input)
|
|
492 (string-to-int input))
|
|
493 ;; a symbol with a function binding
|
|
494 ((fboundp (setq interned (intern input)))
|
|
495 interned)
|
|
496 ;; a lambda function
|
|
497 ((c-safe (functionp (setq raw (read input))))
|
|
498 raw)
|
|
499 ;; a symbol with variable binding
|
|
500 ((boundp interned) interned)
|
|
501 ;; error, but don't signal one, keep trying
|
|
502 ;; to read an input value
|
|
503 (t (ding)
|
|
504 (setq prompt errmsg)
|
|
505 nil))))
|
|
506 offset))
|
|
507
|
|
508 (defun c-set-offset (symbol offset &optional add-p)
|
|
509 "Change the value of a syntactic element symbol in `c-offsets-alist'.
|
|
510 SYMBOL is the syntactic element symbol to change and OFFSET is the new
|
|
511 offset for that syntactic element. Optional ADD says to add SYMBOL to
|
|
512 `c-offsets-alist' if it doesn't already appear there."
|
|
513 (interactive
|
|
514 (let* ((langelem
|
|
515 (intern (completing-read
|
|
516 (concat "Syntactic symbol to change"
|
|
517 (if current-prefix-arg " or add" "")
|
|
518 ": ")
|
|
519 (mapcar
|
|
520 #'(lambda (langelem)
|
|
521 (cons (format "%s" (car langelem)) nil))
|
|
522 c-offsets-alist)
|
|
523 nil (not current-prefix-arg)
|
|
524 ;; initial contents tries to be the last element
|
|
525 ;; on the syntactic analysis list for the current
|
|
526 ;; line
|
|
527 (let* ((syntax (c-guess-basic-syntax))
|
|
528 (len (length syntax))
|
|
529 (ic (format "%s" (car (nth (1- len) syntax)))))
|
|
530 (cons ic 0))
|
|
531 )))
|
|
532 (offset (c-read-offset langelem)))
|
|
533 (list langelem offset current-prefix-arg)))
|
|
534 ;; sanity check offset
|
|
535 (or (eq offset '+)
|
|
536 (eq offset '-)
|
|
537 (eq offset '++)
|
|
538 (eq offset '--)
|
|
539 (eq offset '*)
|
|
540 (eq offset '/)
|
|
541 (integerp offset)
|
|
542 (functionp offset)
|
|
543 (boundp offset)
|
|
544 (error "Offset must be int, func, var, or in [+,-,++,--,*,/]: %s"
|
|
545 offset))
|
|
546 (let ((entry (assq symbol c-offsets-alist)))
|
|
547 (if entry
|
|
548 (setcdr entry offset)
|
|
549 (if add-p
|
|
550 (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist))
|
|
551 (error "%s is not a valid syntactic symbol." symbol))))
|
|
552 (c-keep-region-active))
|
|
553
|
|
554
|
|
555
|
171
|
556 (defun c-initialize-builtin-style ()
|
|
557 ;; Dynamically append the default value of most variables. This is
|
|
558 ;; crucial because future c-set-style calls will always reset the
|
|
559 ;; variables first to the `cc-mode' style before instituting the new
|
|
560 ;; style. Only do this once!
|
|
561 (or (assoc "cc-mode" c-style-alist)
|
|
562 (progn
|
|
563 (c-add-style "cc-mode"
|
|
564 (mapcar
|
|
565 (function
|
|
566 (lambda (var)
|
|
567 (let ((val (symbol-value var)))
|
|
568 (cons var (if (atom val) val
|
175
|
569 (copy-sequence val)
|
171
|
570 ))
|
|
571 )))
|
|
572 '(c-backslash-column
|
|
573 c-basic-offset
|
|
574 c-cleanup-list
|
|
575 c-comment-only-line-offset
|
|
576 c-electric-pound-behavior
|
|
577 c-hanging-braces-alist
|
|
578 c-hanging-colons-alist
|
|
579 c-hanging-comment-starter-p
|
|
580 c-hanging-comment-ender-p
|
|
581 c-offsets-alist
|
|
582 )))
|
|
583 ;; the default style is now GNU. This can be overridden in
|
|
584 ;; c-mode-common-hook or {c,c++,objc,java}-mode-hook.
|
177
|
585 (c-set-style c-site-default-style)))
|
|
586 (if c-style-variables-are-local-p
|
|
587 (c-make-styles-buffer-local)))
|
|
588
|
165
|
589
|
|
590 (defun c-make-styles-buffer-local ()
|
|
591 "Make all CC Mode style variables buffer local.
|
|
592 If you edit primarily one style of C (or C++, Objective-C, Java) code,
|
|
593 you probably want style variables to be global. This is the default.
|
|
594
|
|
595 If you edit many different styles of C (or C++, Objective-C, Java) at
|
|
596 the same time, you probably want the CC Mode style variables to be
|
|
597 buffer local. If you do, then you will need to set any CC Mode style
|
|
598 variables in a hook function (e.g. off of c-mode-common-hook), instead
|
|
599 of at the top level of your ~/.emacs file.
|
|
600
|
|
601 This function makes all the CC Mode style variables buffer local.
|
|
602 Call it after CC Mode is loaded into your Emacs environment.
|
|
603 Conversely, set the variable `c-style-variables-are-local-p' to t in
|
|
604 your .emacs file, before CC Mode is loaded, and this function will be
|
|
605 automatically called when CC Mode is loaded."
|
|
606 ;; style variables
|
|
607 (make-variable-buffer-local 'c-offsets-alist)
|
|
608 (make-variable-buffer-local 'c-basic-offset)
|
|
609 (make-variable-buffer-local 'c-file-style)
|
|
610 (make-variable-buffer-local 'c-file-offsets)
|
|
611 (make-variable-buffer-local 'c-comment-only-line-offset)
|
|
612 (make-variable-buffer-local 'c-cleanup-list)
|
|
613 (make-variable-buffer-local 'c-hanging-braces-alist)
|
|
614 (make-variable-buffer-local 'c-hanging-colons-alist)
|
|
615 (make-variable-buffer-local 'c-hanging-comment-starter-p)
|
|
616 (make-variable-buffer-local 'c-hanging-comment-ender-p)
|
|
617 (make-variable-buffer-local 'c-backslash-column)
|
|
618 (make-variable-buffer-local 'c-label-minimum-indentation)
|
|
619 (make-variable-buffer-local 'c-special-indent-hook)
|
|
620 (make-variable-buffer-local 'c-indentation-style))
|
|
621
|
|
622
|
|
623 (provide 'cc-styles)
|
|
624 ;;; cc-styles.el ends here
|