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