cc-styles.el 24 KB

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