Mercurial > emacs
annotate lisp/progmodes/cc-styles.el @ 28923:dcafe3c9cd6c
(sh-while-getopts) <sh>: Handle case that
user-specified option string is empty.
| author | Gerd Moellmann <gerd@gnu.org> |
|---|---|
| date | Mon, 15 May 2000 20:14:39 +0000 |
| parents | 03befb219d03 |
| children | 58d2360c8677 |
| rev | line source |
|---|---|
| 18720 | 1 ;;; cc-styles.el --- support for styles in CC Mode |
| 2 | |
| 26817 | 3 ;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc. |
| 18720 | 4 |
| 26817 | 5 ;; Authors: 1998-1999 Barry A. Warsaw and Martin Stjernholm |
| 24282 | 6 ;; 1992-1997 Barry A. Warsaw |
| 18720 | 7 ;; 1987 Dave Detlefs and Stewart Clamen |
| 8 ;; 1985 Richard M. Stallman | |
| 24282 | 9 ;; Maintainer: bug-cc-mode@gnu.org |
| 18720 | 10 ;; Created: 22-Apr-1997 (split from cc-mode.el) |
| 20147 | 11 ;; Version: See cc-mode.el |
| 18720 | 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 | |
| 27 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
| 28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
| 29 ;; Boston, MA 02111-1307, USA. | |
| 30 | |
| 26817 | 31 (eval-when-compile |
| 32 (let ((load-path | |
| 33 (if (and (boundp 'byte-compile-current-file) | |
| 34 (stringp byte-compile-current-file)) | |
| 35 (cons (file-name-directory byte-compile-current-file) | |
| 36 load-path) | |
| 37 load-path))) | |
| 38 (load "cc-defs" nil t))) | |
| 39 (require 'cc-vars) | |
| 18720 | 40 |
| 24282 | 41 |
| 18720 | 42 |
| 24282 | 43 ;; Warning: don't eval-defun this constant or you'll break style inheritance. |
| 18720 | 44 (defconst c-style-alist |
| 45 '(("gnu" | |
| 46 (c-basic-offset . 2) | |
| 47 (c-comment-only-line-offset . (0 . 0)) | |
| 48 (c-offsets-alist . ((statement-block-intro . +) | |
| 49 (knr-argdecl-intro . 5) | |
| 50 (substatement-open . +) | |
| 51 (label . 0) | |
| 52 (statement-case-open . +) | |
| 53 (statement-cont . +) | |
| 54 (arglist-intro . c-lineup-arglist-intro-after-paren) | |
| 55 (arglist-close . c-lineup-arglist) | |
| 24282 | 56 (inline-open . 0) |
| 18720 | 57 )) |
| 58 (c-special-indent-hook . c-gnu-impose-minimum) | |
| 26817 | 59 (c-block-comment-prefix . "") |
| 18720 | 60 ) |
| 61 ("k&r" | |
| 62 (c-basic-offset . 5) | |
| 63 (c-comment-only-line-offset . 0) | |
| 64 (c-offsets-alist . ((statement-block-intro . +) | |
| 65 (knr-argdecl-intro . 0) | |
| 66 (substatement-open . 0) | |
| 67 (label . 0) | |
| 68 (statement-cont . +) | |
| 69 )) | |
| 70 ) | |
| 71 ("bsd" | |
| 72 (c-basic-offset . 4) | |
| 73 (c-comment-only-line-offset . 0) | |
| 74 (c-offsets-alist . ((statement-block-intro . +) | |
| 75 (knr-argdecl-intro . +) | |
| 76 (substatement-open . 0) | |
| 77 (label . 0) | |
| 78 (statement-cont . +) | |
| 26817 | 79 (inline-open . 0) |
| 80 (inexpr-class . 0) | |
| 18720 | 81 )) |
| 82 ) | |
| 83 ("stroustrup" | |
| 84 (c-basic-offset . 4) | |
| 85 (c-comment-only-line-offset . 0) | |
| 86 (c-offsets-alist . ((statement-block-intro . +) | |
| 87 (substatement-open . 0) | |
| 88 (label . 0) | |
| 89 (statement-cont . +) | |
| 90 )) | |
| 91 ) | |
| 92 ("whitesmith" | |
| 93 (c-basic-offset . 4) | |
| 94 (c-comment-only-line-offset . 0) | |
| 26817 | 95 (c-offsets-alist . ((knr-argdecl-intro . +) |
| 18720 | 96 (label . 0) |
| 97 (statement-cont . +) | |
| 26817 | 98 (substatement-open . +) |
| 99 (block-open . +) | |
| 100 (statement-block-intro . c-lineup-whitesmith-in-block) | |
| 101 (block-close . c-lineup-whitesmith-in-block) | |
| 102 (inline-open . +) | |
| 103 (defun-open . +) | |
| 104 (defun-block-intro . c-lineup-whitesmith-in-block) | |
| 105 (defun-close . c-lineup-whitesmith-in-block) | |
| 106 (brace-list-open . +) | |
| 107 (brace-list-intro . c-lineup-whitesmith-in-block) | |
| 108 (brace-entry-open . c-indent-multi-line-block) | |
| 109 (brace-list-close . c-lineup-whitesmith-in-block) | |
| 110 (class-open . +) | |
| 111 (inclass . c-lineup-whitesmith-in-block) | |
| 112 (class-close . +) | |
| 113 (inexpr-class . 0) | |
| 114 (extern-lang-open . +) | |
| 115 (inextern-lang . c-lineup-whitesmith-in-block) | |
| 116 (extern-lang-close . +) | |
| 117 (namespace-open . +) | |
| 118 (innamespace . c-lineup-whitesmith-in-block) | |
| 119 (namespace-close . +) | |
| 18720 | 120 )) |
| 121 ) | |
| 122 ("ellemtel" | |
| 123 (c-basic-offset . 3) | |
| 124 (c-comment-only-line-offset . 0) | |
| 125 (c-hanging-braces-alist . ((substatement-open before after))) | |
| 126 (c-offsets-alist . ((topmost-intro . 0) | |
| 127 (topmost-intro-cont . 0) | |
| 128 (substatement . +) | |
| 129 (substatement-open . 0) | |
| 130 (case-label . +) | |
| 131 (access-label . -) | |
| 132 (inclass . ++) | |
| 133 (inline-open . 0) | |
| 134 )) | |
| 135 ) | |
| 136 ("linux" | |
| 137 (c-basic-offset . 8) | |
| 138 (c-comment-only-line-offset . 0) | |
| 139 (c-hanging-braces-alist . ((brace-list-open) | |
| 24282 | 140 (brace-entry-open) |
| 18720 | 141 (substatement-open after) |
| 142 (block-close . c-snug-do-while))) | |
| 143 (c-cleanup-list . (brace-else-brace)) | |
| 144 (c-offsets-alist . ((statement-block-intro . +) | |
| 145 (knr-argdecl-intro . 0) | |
| 146 (substatement-open . 0) | |
| 147 (label . 0) | |
| 148 (statement-cont . +) | |
| 149 )) | |
| 150 ) | |
| 151 ("python" | |
| 152 (indent-tabs-mode . t) | |
| 26817 | 153 (fill-column . 78) |
| 18720 | 154 (c-basic-offset . 8) |
| 155 (c-offsets-alist . ((substatement-open . 0) | |
|
19807
86412da1174b
(c-style-alist) <python>:, knr-argdecl-intro == +
Richard M. Stallman <rms@gnu.org>
parents:
19376
diff
changeset
|
156 (inextern-lang . 0) |
|
86412da1174b
(c-style-alist) <python>:, knr-argdecl-intro == +
Richard M. Stallman <rms@gnu.org>
parents:
19376
diff
changeset
|
157 (arglist-intro . +) |
|
86412da1174b
(c-style-alist) <python>:, knr-argdecl-intro == +
Richard M. Stallman <rms@gnu.org>
parents:
19376
diff
changeset
|
158 (knr-argdecl-intro . +) |
| 18720 | 159 )) |
| 160 (c-hanging-braces-alist . ((brace-list-open) | |
| 161 (brace-list-intro) | |
| 162 (brace-list-close) | |
| 24282 | 163 (brace-entry-open) |
| 18720 | 164 (substatement-open after) |
| 165 (block-close . c-snug-do-while) | |
| 166 )) | |
| 26817 | 167 (c-block-comment-prefix . "") |
| 18720 | 168 ) |
| 169 ("java" | |
|
20918
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
170 (c-basic-offset . 4) |
| 18720 | 171 (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
|
172 ;; the following preserves Javadoc starter lines |
|
20918
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
173 (c-offsets-alist . ((inline-open . 0) |
|
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
174 (topmost-intro-cont . +) |
| 18720 | 175 (statement-block-intro . +) |
| 176 (knr-argdecl-intro . 5) | |
| 177 (substatement-open . +) | |
| 26817 | 178 (label . +) |
| 18720 | 179 (statement-case-open . +) |
| 180 (statement-cont . +) | |
| 181 (arglist-intro . c-lineup-arglist-intro-after-paren) | |
| 182 (arglist-close . c-lineup-arglist) | |
| 183 (access-label . 0) | |
| 184 (inher-cont . c-lineup-java-inher) | |
| 185 (func-decl-cont . c-lineup-java-throws) | |
| 186 )) | |
| 187 ) | |
| 188 ) | |
| 189 "Styles of indentation. | |
| 190 Elements of this alist are of the form: | |
| 191 | |
| 192 (STYLE-STRING [BASE-STYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...]) | |
| 193 | |
| 194 where STYLE-STRING is a short descriptive string used to select a | |
| 195 style, VARIABLE is any Emacs variable, and VALUE is the intended value | |
| 196 for that variable when using the selected style. | |
| 197 | |
| 198 Optional BASE-STYLE if present, is a string and must follow | |
| 199 STYLE-STRING. BASE-STYLE names a style that this style inherits from. | |
| 26817 | 200 By default, all styles inherit from the \"user\" style, which is |
| 18720 | 201 computed at run time. Style loops generate errors. |
| 202 | |
| 203 Two variables are treated specially. When VARIABLE is | |
| 204 `c-offsets-alist', the VALUE is a list containing elements of the | |
| 205 form: | |
| 206 | |
| 207 (SYNTACTIC-SYMBOL . OFFSET) | |
| 208 | |
| 209 as described in `c-offsets-alist'. These are passed directly to | |
| 210 `c-set-offset' so there is no need to set every syntactic symbol in | |
| 211 your style, only those that are different from the default. | |
| 212 | |
| 213 When VARIABLE is `c-special-indent-hook', its VALUE is added to | |
| 214 `c-special-indent-hook' using `add-hook'. If VALUE is a list, each | |
| 215 element of the list is added with `add-hook'. | |
| 216 | |
| 217 Do not change this variable directly. Use the function `c-add-style' | |
| 218 to add new styles or modify existing styles (it is not a good idea to | |
| 219 modify existing styles -- you should create a new style that inherits | |
| 220 the existing style.") | |
| 221 | |
| 26817 | 222 |
| 18720 | 223 |
| 224 ;; Functions that manipulate styles | |
| 26817 | 225 (defun c-set-style-1 (conscell dont-override) |
| 18720 | 226 ;; Set the style for one variable |
| 227 (let ((attr (car conscell)) | |
| 228 (val (cdr conscell))) | |
| 229 (cond | |
| 230 ;; first special variable | |
| 231 ((eq attr 'c-offsets-alist) | |
| 232 (mapcar | |
| 233 (function | |
| 234 (lambda (langentry) | |
| 235 (let ((langelem (car langentry)) | |
| 236 (offset (cdr langentry))) | |
| 26817 | 237 (unless (and dont-override |
| 238 (assq langelem c-offsets-alist)) | |
| 239 (c-set-offset langelem offset)) | |
| 18720 | 240 ))) |
| 26817 | 241 (if dont-override (reverse val) val))) |
| 18720 | 242 ;; second special variable |
| 243 ((eq attr 'c-special-indent-hook) | |
| 26817 | 244 (let ((add-func (if dont-override |
| 245 (lambda (func) | |
| 246 (unless (memq func c-special-indent-hook) | |
| 247 (add-hook 'c-special-indent-hook func t))) | |
| 248 (lambda (func) | |
| 249 (add-hook 'c-special-indent-hook func))))) | |
| 250 (if (listp val) | |
| 251 (mapcar add-func (if dont-override (reverse val) val)) | |
| 252 (funcall add-func val)))) | |
| 18720 | 253 ;; all other variables |
| 26817 | 254 (t (if (or (not dont-override) |
| 255 (not (memq attr c-style-variables)) | |
| 256 (eq (symbol-value attr) 'set-from-style)) | |
| 257 (set attr val)))) | |
| 18720 | 258 )) |
| 259 | |
| 26817 | 260 (defun c-get-style-variables (style basestyles) |
| 261 ;; Return all variables in a style by resolving inheritances. | |
| 18720 | 262 (let ((vars (cdr (or (assoc (downcase style) c-style-alist) |
| 263 (assoc (upcase style) c-style-alist) | |
| 264 (assoc style c-style-alist) | |
| 265 (error "Undefined style: %s" style))))) | |
| 26817 | 266 (if (string-equal style "user") |
| 267 (copy-alist vars) | |
| 268 (let ((base (if (stringp (car vars)) | |
| 269 (prog1 | |
| 270 (downcase (car vars)) | |
| 271 (setq vars (cdr vars))) | |
| 272 "user"))) | |
| 273 (if (memq base basestyles) | |
| 274 (error "Style loop detected: %s in %s" base basestyles)) | |
| 275 (nconc (c-get-style-variables base (cons base basestyles)) | |
| 276 (copy-alist vars)))))) | |
| 277 | |
| 18720 | 278 (defvar c-set-style-history nil) |
| 279 | |
| 280 ;;;###autoload | |
| 26817 | 281 (defun c-set-style (stylename &optional dont-override) |
| 18720 | 282 "Set CC Mode variables to use one of several different indentation styles. |
| 283 STYLENAME is a string representing the desired style from the list of | |
| 284 styles described in the variable `c-style-alist'. See that variable | |
| 285 for details of setting up styles. | |
| 286 | |
| 287 The variable `c-indentation-style' always contains the buffer's current | |
| 26817 | 288 style name. |
| 289 | |
| 290 If the optional argument DONT-OVERRIDE is non-nil, no style variables | |
| 291 that already have values will be overridden. I.e. in the case of | |
| 292 `c-offsets-alist', syntactic symbols will only be added, and in the | |
| 293 case of all other style variables, only those set to `set-from-style' | |
| 294 will be reassigned. | |
| 295 | |
| 296 Obviously, specifying DONT-OVERRIDE is useful mainly when the initial | |
| 297 style is chosen for a CC Mode buffer by a major mode. Since this is | |
| 298 done internally by CC Mode, there's hardly ever a reason to use it." | |
| 18720 | 299 (interactive (list (let ((completion-ignore-case t) |
| 300 (prompt (format "Which %s indentation style? " | |
| 301 mode-name))) | |
| 302 (completing-read prompt c-style-alist nil t | |
| 303 (cons c-indentation-style 0) | |
| 304 'c-set-style-history)))) | |
|
19254
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
305 (c-initialize-builtin-style) |
| 26817 | 306 (let ((vars (c-get-style-variables stylename nil))) |
| 307 (mapcar (lambda (elem) | |
| 308 (c-set-style-1 elem dont-override)) | |
| 309 ;; Need to go through the variables backwards when we | |
| 310 ;; don't override. | |
| 311 (if dont-override (nreverse vars) vars))) | |
| 18720 | 312 (setq c-indentation-style stylename) |
| 313 (c-keep-region-active)) | |
| 314 | |
| 315 ;;;###autoload | |
| 316 (defun c-add-style (style descrip &optional set-p) | |
| 317 "Adds a style to `c-style-alist', or updates an existing one. | |
| 318 STYLE is a string identifying the style to add or update. DESCRIP is | |
| 319 an association list describing the style and must be of the form: | |
| 320 | |
| 321 ([BASESTYLE] (VARIABLE . VALUE) [(VARIABLE . VALUE) ...]) | |
| 322 | |
| 323 See the variable `c-style-alist' for the semantics of BASESTYLE, | |
| 324 VARIABLE and VALUE. This function also sets the current style to | |
| 325 STYLE using `c-set-style' if the optional SET-P flag is non-nil." | |
| 326 (interactive | |
| 327 (let ((stylename (completing-read "Style to add: " c-style-alist | |
| 328 nil nil nil 'c-set-style-history)) | |
| 329 (description (eval-minibuffer "Style description: "))) | |
| 330 (list stylename description | |
| 331 (y-or-n-p "Set the style too? ")))) | |
| 332 (setq style (downcase style)) | |
| 333 (let ((s (assoc style c-style-alist))) | |
| 334 (if s | |
| 335 (setcdr s (copy-alist descrip)) ; replace | |
| 336 (setq c-style-alist (cons (cons style descrip) c-style-alist)))) | |
| 337 (and set-p (c-set-style style))) | |
| 338 | |
| 339 | |
| 340 | |
| 24282 | 341 (defun c-evaluate-offset (offset langelem symbol) |
| 342 ;; offset can be a number, a function, a variable, a list, or one of | |
| 343 ;; the symbols + or - | |
| 344 (cond | |
| 345 ((eq offset '+) (setq offset c-basic-offset)) | |
| 346 ((eq offset '-) (setq offset (- c-basic-offset))) | |
| 347 ((eq offset '++) (setq offset (* 2 c-basic-offset))) | |
| 348 ((eq offset '--) (setq offset (* 2 (- c-basic-offset)))) | |
| 349 ((eq offset '*) (setq offset (/ c-basic-offset 2))) | |
| 350 ((eq offset '/) (setq offset (/ (- c-basic-offset) 2))) | |
| 351 ((functionp offset) (setq offset (funcall offset langelem))) | |
| 352 ((listp offset) | |
| 353 (setq offset | |
| 354 (let (done) | |
| 355 (while (and (not done) offset) | |
| 356 (setq done (c-evaluate-offset (car offset) langelem symbol) | |
| 357 offset (cdr offset))) | |
| 358 (if (not done) | |
| 359 (if c-strict-syntax-p | |
| 360 (error "No offset found for syntactic symbol %s" symbol) | |
| 361 0) | |
| 362 done)))) | |
| 363 ((not (numberp offset)) (setq offset (symbol-value offset))) | |
| 364 ) | |
| 365 offset) | |
| 366 | |
| 18720 | 367 (defun c-get-offset (langelem) |
| 368 ;; Get offset from LANGELEM which is a cons cell of the form: | |
| 369 ;; (SYMBOL . RELPOS). The symbol is matched against | |
| 370 ;; c-offsets-alist and the offset found there is either returned, | |
| 371 ;; or added to the indentation at RELPOS. If RELPOS is nil, then | |
| 372 ;; the offset is simply returned. | |
| 373 (let* ((symbol (car langelem)) | |
| 374 (relpos (cdr langelem)) | |
| 375 (match (assq symbol c-offsets-alist)) | |
| 376 (offset (cdr-safe match))) | |
| 24282 | 377 (if (not match) |
| 378 (if c-strict-syntax-p | |
| 379 (error "No offset found for syntactic symbol %s" symbol) | |
| 380 (setq offset 0 | |
| 381 relpos 0)) | |
| 382 (setq offset (c-evaluate-offset offset langelem symbol))) | |
| 18720 | 383 (+ (if (and relpos |
| 384 (< relpos (c-point 'bol))) | |
| 385 (save-excursion | |
| 386 (goto-char relpos) | |
| 387 (current-column)) | |
| 388 0) | |
| 26817 | 389 (or (and (numberp offset) offset) |
| 390 (and (symbolp offset) (symbol-value offset)) | |
| 391 0)) | |
| 24282 | 392 )) |
| 18720 | 393 |
| 26817 | 394 |
| 18720 | 395 |
| 396 (defvar c-read-offset-history nil) | |
| 397 | |
| 398 (defun c-read-offset (langelem) | |
| 399 ;; read new offset value for LANGELEM from minibuffer. return a | |
| 400 ;; legal value only | |
| 26817 | 401 (let* ((oldoff (cdr-safe (or (assq langelem c-offsets-alist) |
| 402 (assq langelem (get 'c-offsets-alist | |
| 403 'c-stylevar-fallback))))) | |
| 24282 | 404 (symname (symbol-name langelem)) |
| 405 (defstr (format "(default %s): " oldoff)) | |
| 406 (errmsg (concat "Offset must be int, func, var, list, " | |
| 407 "or [+,-,++,--,*,/] " | |
| 408 defstr)) | |
| 409 (prompt (concat symname " offset " defstr)) | |
| 18720 | 410 offset input interned raw) |
| 411 (while (not offset) | |
| 412 (setq input (completing-read prompt obarray 'fboundp nil nil | |
| 413 'c-read-offset-history) | |
| 414 offset (cond ((string-equal "" input) oldoff) ; default | |
| 415 ((string-equal "+" input) '+) | |
| 416 ((string-equal "-" input) '-) | |
| 417 ((string-equal "++" input) '++) | |
| 418 ((string-equal "--" input) '--) | |
| 419 ((string-equal "*" input) '*) | |
| 420 ((string-equal "/" input) '/) | |
| 421 ((string-match "^-?[0-9]+$" input) | |
| 422 (string-to-int input)) | |
| 423 ;; a symbol with a function binding | |
| 424 ((fboundp (setq interned (intern input))) | |
| 425 interned) | |
| 426 ;; a lambda function | |
| 427 ((c-safe (functionp (setq raw (read input)))) | |
| 428 raw) | |
| 429 ;; a symbol with variable binding | |
| 430 ((boundp interned) interned) | |
| 431 ;; error, but don't signal one, keep trying | |
| 432 ;; to read an input value | |
| 433 (t (ding) | |
| 434 (setq prompt errmsg) | |
| 435 nil)))) | |
| 436 offset)) | |
| 437 | |
|
19212
024594beef65
(c-set-offset): Add autoload cookie.
Richard M. Stallman <rms@gnu.org>
parents:
18845
diff
changeset
|
438 ;;;###autoload |
| 26817 | 439 (defun c-set-offset (symbol offset &optional ignored) |
| 18720 | 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 | |
| 26817 | 442 offset for that syntactic element. The optional argument is not used |
| 443 and exists only for compatibility reasons." | |
| 18720 | 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)) | |
| 26817 | 453 (get 'c-offsets-alist 'c-stylevar-fallback)) |
| 18720 | 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 | |
| 458 (let* ((syntax (c-guess-basic-syntax)) | |
| 459 (len (length syntax)) | |
| 460 (ic (format "%s" (car (nth (1- len) syntax))))) | |
| 461 (cons ic 0)) | |
| 462 ))) | |
| 463 (offset (c-read-offset langelem))) | |
| 464 (list langelem offset current-prefix-arg))) | |
| 465 ;; sanity check offset | |
| 26817 | 466 (unless (c-valid-offset offset) |
| 467 (error "Offset must be int, func, var, list, or in [+,-,++,--,*,/]: %s" | |
| 468 offset)) | |
| 18720 | 469 (let ((entry (assq symbol c-offsets-alist))) |
| 470 (if entry | |
| 471 (setcdr entry offset) | |
| 26817 | 472 (if (assq symbol (get 'c-offsets-alist 'c-stylevar-fallback)) |
| 18720 | 473 (setq c-offsets-alist (cons (cons symbol offset) c-offsets-alist)) |
| 26817 | 474 (error "%s is not a valid syntactic symbol" symbol)))) |
| 18720 | 475 (c-keep-region-active)) |
| 476 | |
| 26817 | 477 |
| 24282 | 478 |
| 18720 | 479 (defun c-initialize-builtin-style () |
| 480 ;; Dynamically append the default value of most variables. This is | |
| 481 ;; crucial because future c-set-style calls will always reset the | |
| 482 ;; variables first to the `cc-mode' style before instituting the new | |
| 483 ;; style. Only do this once! | |
| 24282 | 484 (unless (get 'c-initialize-builtin-style 'is-run) |
| 485 (put 'c-initialize-builtin-style 'is-run t) | |
| 486 (c-initialize-cc-mode) | |
| 487 (or (assoc "cc-mode" c-style-alist) | |
| 488 (assoc "user" c-style-alist) | |
| 489 (progn | |
| 26817 | 490 (c-add-style |
| 491 "user" | |
| 492 (mapcar | |
| 493 (lambda (var) | |
| 494 (let ((val (symbol-value var))) | |
| 495 (cons var | |
| 496 (cond ((eq var 'c-offsets-alist) | |
| 497 (mapcar | |
| 498 (lambda (langentry) | |
| 499 (setq langentry (or (assq (car langentry) val) | |
| 500 langentry)) | |
| 501 (cons (car langentry) | |
| 502 (cdr langentry))) | |
| 503 (get var 'c-stylevar-fallback))) | |
| 504 ((eq var 'c-special-indent-hook) | |
| 505 val) | |
| 506 (t | |
| 507 (if (eq val 'set-from-style) | |
| 508 (get var 'c-stylevar-fallback) | |
| 509 val)))))) | |
| 510 c-style-variables)) | |
| 511 (c-add-style "cc-mode" '("user")))) | |
| 24282 | 512 (if c-style-variables-are-local-p |
| 513 (c-make-styles-buffer-local)))) | |
|
19254
81353d4e05b7
(c-styles-alist): In "java" style, set
Richard M. Stallman <rms@gnu.org>
parents:
19212
diff
changeset
|
514 |
|
20918
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
515 (defun c-make-styles-buffer-local (&optional this-buf-only-p) |
| 18720 | 516 "Make all CC Mode style variables buffer local. |
| 26817 | 517 If you edit primarily one style of C (or C++, Objective-C, Java, etc) |
| 518 code, you probably want style variables to be global. This is the | |
| 519 default. | |
| 18720 | 520 |
| 26817 | 521 If you edit many different styles of C (or C++, Objective-C, Java, |
| 522 etc) at the same time, you probably want the CC Mode style variables | |
| 523 to be buffer local. If you do, it's advicable to set any CC Mode | |
| 524 style variables in a hook function (e.g. off of `c-mode-common-hook'), | |
| 525 instead of at the top level of your ~/.emacs file. | |
| 18720 | 526 |
| 527 This function makes all the CC Mode style variables buffer local. | |
| 528 Call it after CC Mode is loaded into your Emacs environment. | |
| 529 Conversely, set the variable `c-style-variables-are-local-p' to t in | |
| 530 your .emacs file, before CC Mode is loaded, and this function will be | |
|
20918
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
531 automatically called when CC Mode is loaded. |
|
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
532 |
|
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
533 Optional argument, when non-nil, means use `make-local-variable' |
|
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
534 instead of `make-variable-buffer-local'." |
| 18720 | 535 ;; style variables |
|
20918
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
536 (let ((func (if this-buf-only-p |
|
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
537 'make-local-variable |
|
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
538 'make-variable-buffer-local)) |
| 26817 | 539 (varsyms (cons 'c-indentation-style (copy-alist c-style-variables)))) |
| 540 (delq 'c-special-indent-hook varsyms) | |
|
20918
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
541 (mapcar func varsyms) |
|
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
542 ;; Hooks must be handled specially |
|
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
543 (if this-buf-only-p |
|
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
544 (make-local-hook 'c-special-indent-hook) |
| 26817 | 545 (make-variable-buffer-local 'c-special-indent-hook) |
| 546 (setq c-style-variables-are-local-p t)) | |
|
20918
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
547 )) |
|
8e64bd5b1b5f
(c-make-styles-buffer-local): Take an optional argument which switches
Richard M. Stallman <rms@gnu.org>
parents:
20147
diff
changeset
|
548 |
| 18720 | 549 |
| 550 | |
| 551 (provide 'cc-styles) | |
| 552 ;;; cc-styles.el ends here |
