cc-align.el 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. ;;; cc-align.el --- custom indentation functions for 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-engine)
  36. ;; Standard line-up functions
  37. ;;
  38. ;; See the section "Custom Indentation Functions" in the manual for
  39. ;; details on the calling convention.
  40. (defun c-lineup-topmost-intro-cont (langelem)
  41. "Line up declaration continuation lines zero or one indentation step.
  42. For lines in the \"header\" of a definition, zero is used. For other
  43. lines, `c-basic-offset' is added to the indentation. E.g:
  44. int
  45. neg (int i) <- c-lineup-topmost-intro-cont
  46. {
  47. return -i;
  48. }
  49. struct
  50. larch <- c-lineup-topmost-intro-cont
  51. {
  52. double height;
  53. }
  54. the_larch, <- c-lineup-topmost-intro-cont
  55. another_larch; <- c-lineup-topmost-intro-cont
  56. <--> c-basic-offset
  57. struct larch
  58. the_larch, <- c-lineup-topmost-intro-cont
  59. another_larch; <- c-lineup-topmost-intro-cont
  60. \(This function is mainly provided to mimic the behavior of CC Mode
  61. 5.28 and earlier where this case wasn't handled consistently so that
  62. these lines could be analyzed as either topmost-intro-cont or
  63. statement-cont.)
  64. Works with: topmost-intro-cont."
  65. (save-excursion
  66. (beginning-of-line)
  67. (c-backward-syntactic-ws (c-langelem-pos langelem))
  68. (if (and (memq (char-before) '(?} ?,))
  69. (not (and c-overloadable-operators-regexp
  70. (c-after-special-operator-id))))
  71. c-basic-offset)))
  72. (defun c-lineup-gnu-DEFUN-intro-cont (langelem)
  73. "Line up the continuation lines of a DEFUN macro in the Emacs C source.
  74. These lines are indented as though they were `knr-argdecl-intro' lines.
  75. Return nil when we're not in such a construct.
  76. This function is for historical compatibility with how previous CC Modes (5.28
  77. and earlier) indented such lines.
  78. Here is an example:
  79. DEFUN (\"forward-char\", Fforward_char, Sforward_char, 0, 1, \"p\",
  80. doc: /* Move point right N characters (left if N is negative).
  81. On reaching end of buffer, stop and signal error. */)
  82. (n) <- c-lineup-gnu-DEFUN-into-cont
  83. Lisp_Object n; <- c-lineup-gnu-DEFUN-into-cont
  84. Works with: topmost-intro-cont."
  85. (save-excursion
  86. (let (case-fold-search)
  87. (goto-char (c-langelem-pos langelem))
  88. (if (looking-at "\\<DEFUN\\>")
  89. (c-calc-offset '(knr-argdecl-intro))))))
  90. (defun c-block-in-arglist-dwim (arglist-start)
  91. ;; This function implements the DWIM to avoid far indentation of
  92. ;; brace block constructs in arguments in `c-lineup-arglist' etc.
  93. ;; Return non-nil if a brace block construct is detected within the
  94. ;; arglist starting at ARGLIST-START.
  95. (or
  96. ;; Check if the syntactic context contains any of the symbols for
  97. ;; in-expression constructs. This can both save the work that we
  98. ;; have to do below, and it also detect the brace list constructs
  99. ;; that `c-looking-at-inexpr-block' currently misses (they are
  100. ;; recognized by `c-inside-bracelist-p' instead).
  101. (assq 'inexpr-class c-syntactic-context)
  102. (assq 'inexpr-statement c-syntactic-context)
  103. (assq 'inlambda c-syntactic-context)
  104. (save-restriction
  105. ;; Search for open braces from the arglist start to the end of the
  106. ;; line.
  107. (narrow-to-region arglist-start (c-point 'eol arglist-start))
  108. (goto-char arglist-start)
  109. (while (and (c-syntactic-re-search-forward "{" nil t)
  110. (progn
  111. (backward-char)
  112. (or
  113. ;; Ignore starts of special brace lists.
  114. (and c-special-brace-lists
  115. (save-restriction
  116. (widen)
  117. (c-looking-at-special-brace-list)))
  118. ;; Ignore complete blocks.
  119. (c-safe (c-forward-sexp) t))))
  120. (forward-char))
  121. (looking-at "{"))
  122. (let (containing-sexp)
  123. (goto-char arglist-start)
  124. ;; `c-syntactic-eol' always matches somewhere on the line.
  125. (re-search-forward c-syntactic-eol)
  126. (goto-char (match-beginning 0))
  127. (c-forward-syntactic-ws)
  128. (setq containing-sexp (c-most-enclosing-brace (c-parse-state)))
  129. (c-looking-at-inexpr-block
  130. (c-safe-position (or containing-sexp (point)) c-state-cache)
  131. containing-sexp))))
  132. (defun c-lineup-arglist (langelem)
  133. "Line up the current argument line under the first argument.
  134. As a special case, if the indented line is inside a brace block
  135. construct, the indentation is `c-basic-offset' only. This is intended
  136. as a \"DWIM\" measure in cases like macros that contains statement
  137. blocks, e.g:
  138. A_VERY_LONG_MACRO_NAME ({
  139. some (code, with + long, lines * in[it]);
  140. });
  141. <--> c-basic-offset
  142. This is motivated partly because it's more in line with how code
  143. blocks are handled, and partly since it approximates the behavior of
  144. earlier CC Mode versions, which due to inaccurate analysis tended to
  145. indent such cases this way.
  146. Works with: arglist-cont-nonempty, arglist-close."
  147. (save-excursion
  148. (let ((indent-pos (point)))
  149. (if (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element))
  150. c-basic-offset ; DWIM case.
  151. ;; Normal case. Indent to the token after the arglist open paren.
  152. (goto-char (c-langelem-2nd-pos c-syntactic-element))
  153. (if (and c-special-brace-lists
  154. (c-looking-at-special-brace-list))
  155. ;; Skip a special brace list opener like "({".
  156. (progn (c-forward-token-2)
  157. (forward-char))
  158. (forward-char))
  159. (let ((arglist-content-start (point)))
  160. (c-forward-syntactic-ws)
  161. (when (< (point) indent-pos)
  162. (goto-char arglist-content-start)
  163. (skip-chars-forward " \t"))
  164. (vector (current-column)))))))
  165. ;; Contributed by Kevin Ryde <user42@zip.com.au>.
  166. (defun c-lineup-argcont (elem)
  167. "Line up a continued argument.
  168. foo (xyz, aaa + bbb + ccc
  169. + ddd + eee + fff); <- c-lineup-argcont
  170. Only continuation lines like this are touched, nil is returned on lines
  171. which are the start of an argument.
  172. Within a gcc asm block, \":\" is recognized as an argument separator,
  173. but of course only between operand specifications, not in the expressions
  174. for the operands.
  175. Works with: arglist-cont, arglist-cont-nonempty."
  176. (save-excursion
  177. (beginning-of-line)
  178. (when (eq (car elem) 'arglist-cont-nonempty)
  179. ;; Our argument list might not be the innermost one. If it
  180. ;; isn't, go back to the last position in it. We do this by
  181. ;; stepping back over open parens until we get to the open paren
  182. ;; of our argument list.
  183. (let ((open-paren (c-langelem-2nd-pos c-syntactic-element))
  184. (paren-state (c-parse-state)))
  185. (while (not (eq (car paren-state) open-paren))
  186. (unless (consp (car paren-state)) ;; ignore matched braces
  187. (goto-char (car paren-state)))
  188. (setq paren-state (cdr paren-state)))))
  189. (let ((start (point)) c)
  190. (when (bolp)
  191. ;; Previous line ending in a comma means we're the start of an
  192. ;; argument. This should quickly catch most cases not for us.
  193. ;; This case is only applicable if we're the innermost arglist.
  194. (c-backward-syntactic-ws)
  195. (setq c (char-before)))
  196. (unless (eq c ?,)
  197. ;; In a gcc asm, ":" on the previous line means the start of an
  198. ;; argument. And lines starting with ":" are not for us, don't
  199. ;; want them to indent to the preceding operand.
  200. (let ((gcc-asm (save-excursion
  201. (goto-char start)
  202. (c-in-gcc-asm-p))))
  203. (unless (and gcc-asm
  204. (or (eq c ?:)
  205. (save-excursion
  206. (goto-char start)
  207. (looking-at "[ \t]*:"))))
  208. (c-lineup-argcont-scan (if gcc-asm ?:))
  209. (vector (current-column))))))))
  210. (defun c-lineup-argcont-scan (&optional other-match)
  211. ;; Find the start of an argument, for `c-lineup-argcont'.
  212. (when (zerop (c-backward-token-2 1 t))
  213. (let ((c (char-after)))
  214. (if (or (eq c ?,) (eq c other-match))
  215. (progn
  216. (forward-char)
  217. (c-forward-syntactic-ws))
  218. (c-lineup-argcont-scan other-match)))))
  219. (defun c-lineup-arglist-intro-after-paren (langelem)
  220. "Line up a line to just after the open paren of the surrounding paren
  221. or brace block.
  222. Works with: defun-block-intro, brace-list-intro,
  223. statement-block-intro, statement-case-intro, arglist-intro."
  224. (save-excursion
  225. (beginning-of-line)
  226. (backward-up-list 1)
  227. (skip-chars-forward " \t" (c-point 'eol))
  228. (vector (1+ (current-column)))))
  229. (defun c-lineup-arglist-close-under-paren (langelem)
  230. "Line up a line under the enclosing open paren.
  231. Normally used to line up a closing paren in the same column as its
  232. corresponding open paren, but can also be used with arglist-cont and
  233. arglist-cont-nonempty to line up all lines inside a parenthesis under
  234. the open paren.
  235. As a special case, if a brace block construct starts at the same line
  236. as the open parenthesis of the argument list, the indentation is
  237. `c-basic-offset' only. See `c-lineup-arglist' for further discussion
  238. of this \"DWIM\" measure.
  239. Works with: Almost all symbols, but are typically most useful on
  240. arglist-close, brace-list-close, arglist-cont and arglist-cont-nonempty."
  241. (save-excursion
  242. (if (memq (c-langelem-sym langelem)
  243. '(arglist-cont-nonempty arglist-close))
  244. (goto-char (c-langelem-2nd-pos c-syntactic-element))
  245. (beginning-of-line)
  246. (c-go-up-list-backward))
  247. (if (save-excursion (c-block-in-arglist-dwim (point)))
  248. c-basic-offset ; DWIM case.
  249. ;; Normal case. Indent to the arglist open paren.
  250. (let (special-list)
  251. (if (and c-special-brace-lists
  252. (setq special-list (c-looking-at-special-brace-list)))
  253. ;; Cope if we're in the middle of a special brace list
  254. ;; opener like "({".
  255. (goto-char (car (car special-list))))
  256. (vector (current-column))))))
  257. (defun c-lineup-arglist-operators (langelem)
  258. "Line up lines starting with an infix operator under the open paren.
  259. Return nil on lines that don't start with an operator, to leave those
  260. cases to other line-up functions. Example:
  261. if ( x < 10
  262. || at_limit (x, <- c-lineup-arglist-operators
  263. list) <- c-lineup-arglist-operators returns nil
  264. )
  265. Since this function doesn't do anything for lines without an infix
  266. operator you typically want to use it together with some other line-up
  267. settings, e.g. as follows \(the arglist-close setting is just a
  268. suggestion to get a consistent style):
  269. \(c-set-offset 'arglist-cont '(c-lineup-arglist-operators 0))
  270. \(c-set-offset 'arglist-cont-nonempty '(c-lineup-arglist-operators
  271. c-lineup-arglist))
  272. \(c-set-offset 'arglist-close '(c-lineup-arglist-close-under-paren))
  273. Works with: arglist-cont, arglist-cont-nonempty."
  274. (save-excursion
  275. (back-to-indentation)
  276. (when (looking-at "[-+|&*%<>=]\\|\\(/[^/*]\\)")
  277. ;; '-' can be both an infix and a prefix operator, but I'm lazy now..
  278. (c-lineup-arglist-close-under-paren langelem))))
  279. (defun c-lineup-close-paren (langelem)
  280. "Line up the closing paren under its corresponding open paren if the
  281. open paren is followed by code. If the open paren ends its line, no
  282. indentation is added. E.g:
  283. main (int, main (
  284. char ** int, char **
  285. ) <-> ) <- c-lineup-close-paren
  286. As a special case, if a brace block construct starts at the same line
  287. as the open parenthesis of the argument list, the indentation is
  288. `c-basic-offset' instead of the open paren column. See
  289. `c-lineup-arglist' for further discussion of this \"DWIM\" measure.
  290. Works with: All *-close symbols."
  291. (save-excursion
  292. (if (memq (c-langelem-sym langelem)
  293. '(arglist-cont-nonempty arglist-close))
  294. (goto-char (c-langelem-2nd-pos c-syntactic-element))
  295. (beginning-of-line)
  296. (c-go-up-list-backward))
  297. (let (special-list arglist-start)
  298. (if (and c-special-brace-lists
  299. (setq special-list (c-looking-at-special-brace-list)))
  300. ;; Cope if we're in the middle of a special brace list
  301. ;; opener like "({".
  302. (progn
  303. (goto-char (setq arglist-start (car (car special-list))))
  304. (c-forward-token-2)
  305. (forward-char))
  306. (setq arglist-start (point))
  307. (forward-char))
  308. (cond ((looking-at c-syntactic-eol)
  309. 0) ; The arglist is "empty".
  310. ((c-block-in-arglist-dwim (point))
  311. c-basic-offset) ; DWIM case.
  312. (t
  313. ;; Normal case. Indent to the arglist open paren.
  314. (goto-char arglist-start)
  315. (vector (current-column)))))))
  316. (defun c-lineup-streamop (langelem)
  317. "Line up C++ stream operators under each other.
  318. Works with: stream-op."
  319. (save-excursion
  320. (goto-char (c-langelem-pos langelem))
  321. (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
  322. (goto-char (match-beginning 0))
  323. (vector (current-column))))
  324. (defun c-lineup-multi-inher (langelem)
  325. "Line up the classes in C++ multiple inheritance clauses and member
  326. initializers under each other. E.g:
  327. class Foo: Foo::Foo (int a, int b):
  328. public Cyphr, Cyphr (a),
  329. public Bar <-> Bar (b) <- c-lineup-multi-inher
  330. class Foo Foo::Foo (int a, int b)
  331. : public Cyphr, : Cyphr (a),
  332. public Bar <-> Bar (b) <- c-lineup-multi-inher
  333. class Foo Foo::Foo (int a, int b)
  334. : public Cyphr : Cyphr (a)
  335. , public Bar <-> , Bar (b) <- c-lineup-multi-inher
  336. Works with: inher-cont, member-init-cont."
  337. (save-excursion
  338. (back-to-indentation)
  339. (let* ((eol (c-point 'eol))
  340. (here (point))
  341. (char-after-ip (char-after)))
  342. (if (c-langelem-pos langelem)
  343. (goto-char (c-langelem-pos langelem)))
  344. ;; This kludge is necessary to support both inher-cont and
  345. ;; member-init-cont, since they have different anchor positions.
  346. (c-backward-syntactic-ws)
  347. (when (eq (char-before) ?:)
  348. (backward-char)
  349. (c-backward-syntactic-ws))
  350. (c-syntactic-re-search-forward ":" eol 'move)
  351. (if (looking-at c-syntactic-eol)
  352. (c-forward-syntactic-ws here)
  353. (if (eq char-after-ip ?,)
  354. (backward-char)
  355. (skip-chars-forward " \t" eol)))
  356. (if (< (point) here)
  357. (vector (current-column)))
  358. )))
  359. (defun c-lineup-java-inher (langelem)
  360. "Line up Java implements and extends declarations.
  361. If class names follow on the same line as the implements/extends
  362. keyword, they are lined up under each other. Otherwise, they are
  363. indented by adding `c-basic-offset' to the column of the keyword.
  364. E.g:
  365. class Foo class Foo
  366. extends extends Cyphr,
  367. Bar <-> Bar <- c-lineup-java-inher
  368. <--> c-basic-offset
  369. Works with: inher-cont."
  370. (save-excursion
  371. (goto-char (c-langelem-pos langelem))
  372. (forward-word 1)
  373. (if (looking-at "[ \t]*$")
  374. c-basic-offset
  375. (c-forward-syntactic-ws)
  376. (vector (current-column)))))
  377. (defun c-lineup-java-throws (langelem)
  378. "Line up Java throws declarations.
  379. If exception names follow on the same line as the throws keyword,
  380. they are lined up under each other. Otherwise, they are indented by
  381. adding `c-basic-offset' to the column of the throws keyword. The
  382. throws keyword itself is also indented by `c-basic-offset' from the
  383. function declaration start if it doesn't hang. E.g:
  384. int foo() int foo() throws Cyphr,
  385. throws <-> Bar, <- c-lineup-java-throws
  386. Bar <-> Vlod <- c-lineup-java-throws
  387. <--><--> c-basic-offset
  388. Works with: func-decl-cont."
  389. (save-excursion
  390. (let* ((lim (1- (c-point 'bol)))
  391. (throws (catch 'done
  392. (goto-char (c-langelem-pos langelem))
  393. (while (zerop (c-forward-token-2 1 t lim))
  394. (if (looking-at "throws\\>[^_]")
  395. (throw 'done t))))))
  396. (if throws
  397. (if (zerop (c-forward-token-2 1 nil (c-point 'eol)))
  398. (vector (current-column))
  399. (back-to-indentation)
  400. (vector (+ (current-column) c-basic-offset)))
  401. c-basic-offset))))
  402. (defun c-indent-one-line-block (langelem)
  403. "Indent a one line block `c-basic-offset' extra.
  404. E.g:
  405. if (n > 0) if (n > 0)
  406. {m+=n; n=0;} <-> { <- c-indent-one-line-block
  407. <--> c-basic-offset m+=n; n=0;
  408. }
  409. The block may use any kind of parenthesis character. nil is returned
  410. if the line doesn't start with a one line block, which makes the
  411. function usable in list expressions.
  412. Work with: Almost all syntactic symbols, but most useful on *-open."
  413. (save-excursion
  414. (let ((eol (c-point 'eol)))
  415. (back-to-indentation)
  416. (if (and (eq (char-syntax (char-after)) ?\()
  417. (c-safe (progn (c-forward-sexp) t))
  418. (<= (point) eol))
  419. c-basic-offset
  420. nil))))
  421. (defun c-indent-multi-line-block (langelem)
  422. "Indent a multi line block `c-basic-offset' extra.
  423. E.g:
  424. int *foo[] = { int *foo[] = {
  425. NULL, NULL,
  426. {17}, <-> { <- c-indent-multi-line-block
  427. 17
  428. }
  429. <--> c-basic-offset
  430. The block may use any kind of parenthesis character. nil is returned
  431. if the line doesn't start with a multi line block, which makes the
  432. function usable in list expressions.
  433. Work with: Almost all syntactic symbols, but most useful on *-open."
  434. (save-excursion
  435. (let ((eol (c-point 'eol)))
  436. (back-to-indentation)
  437. (if (and (eq (char-syntax (char-after)) ?\()
  438. (or (not (c-safe (progn (c-forward-sexp) t)))
  439. (> (point) eol)))
  440. c-basic-offset
  441. nil))))
  442. (defun c-lineup-C-comments (langelem)
  443. "Line up C block comment continuation lines.
  444. Various heuristics are used to handle many of the common comment
  445. styles. Some examples:
  446. /* /** /* /* text /* /**
  447. * text * text text text ** text ** text
  448. */ */ */ */ */ */
  449. /*********************************************************************
  450. * text
  451. ********************************************************************/
  452. /*********************************************************************
  453. Free form text comments:
  454. In comments with a long delimiter line at the start, the indentation
  455. is kept unchanged for lines that start with an empty comment line
  456. prefix. The delimiter line is whatever matches the
  457. `comment-start-skip' regexp.
  458. *********************************************************************/
  459. The variable `c-comment-prefix-regexp' is used to recognize the
  460. comment line prefix, e.g. the `*' that usually starts every line
  461. inside a comment.
  462. Works with: The `c' syntactic symbol."
  463. (save-excursion
  464. (let* ((here (point))
  465. (prefixlen (progn (back-to-indentation)
  466. (if (looking-at c-current-comment-prefix)
  467. (- (match-end 0) (point))
  468. 0)))
  469. (starterlen
  470. ;; Get the length of the comment starter, not including
  471. ;; the first '/'. We check if the comment prefix matched
  472. ;; on the current line matches the starter or if it
  473. ;; matches comment-start-skip, and choose whichever is
  474. ;; longest.
  475. (max (save-excursion
  476. (goto-char (1+ (c-langelem-pos langelem)))
  477. (if (and (match-string 0)
  478. (looking-at (regexp-quote (match-string 0))))
  479. (- (match-end 0) (match-beginning 0))
  480. 0))
  481. (save-excursion
  482. (goto-char (c-langelem-pos langelem))
  483. (looking-at comment-start-skip)
  484. (- (or (match-end 1)
  485. (save-excursion
  486. (goto-char (match-end 0))
  487. (skip-chars-backward " \t")
  488. (point)))
  489. (point)
  490. 1)))))
  491. (if (and (> starterlen 10) (zerop prefixlen))
  492. ;; The comment has a long starter and the line doesn't have
  493. ;; a nonempty comment prefix. Treat it as free form text
  494. ;; and don't change the indentation.
  495. (vector (current-column))
  496. ;; Go back to the previous non-blank line, if any.
  497. (while
  498. (progn
  499. (forward-line -1)
  500. (back-to-indentation)
  501. (and (> (point) (c-langelem-pos langelem))
  502. (looking-at "[ \t]*$"))))
  503. ;; Is the starting line the first continuation line with content?
  504. (if (>= (c-langelem-pos langelem) (point))
  505. (if (zerop prefixlen)
  506. ;; No nonempty comment prefix. Align after comment
  507. ;; starter.
  508. (progn
  509. (looking-at comment-start-skip)
  510. (goto-char (match-end 0))
  511. ;; The following should not be necessary, since
  512. ;; comment-start-skip should match everything (i.e.
  513. ;; typically whitespace) that leads up to the text.
  514. ;;(if (looking-at "\\([ \t]+\\).+$")
  515. ;; ;; Align with the text that hangs after the
  516. ;; ;; comment starter.
  517. ;; (goto-char (match-end 1)))
  518. (vector (current-column)))
  519. ;; How long is the comment starter? if greater than the
  520. ;; length of the comment prefix, align left. if less
  521. ;; than or equal, align right. this should also pick up
  522. ;; Javadoc style comments.
  523. (if (> starterlen prefixlen)
  524. (progn
  525. (goto-char (c-langelem-pos langelem))
  526. (vector (1+ (current-column))))
  527. (goto-char (+ (c-langelem-pos langelem) starterlen 1))
  528. (vector (- (current-column) prefixlen))))
  529. ;; We didn't start on the first non-blank continuation line. If the
  530. ;; previous line has a nonempty comment prefix, align with it.
  531. ;; Otherwise, align with the previous nonempty line, but align the
  532. ;; comment ender with the starter.
  533. (when (or (not (looking-at c-current-comment-prefix))
  534. (eq (match-beginning 0) (match-end 0)))
  535. (goto-char here)
  536. (back-to-indentation)
  537. (if (looking-at (concat "\\(" c-current-comment-prefix "\\)\\*/"))
  538. (goto-char (c-langelem-pos langelem))
  539. (while (and (zerop (forward-line -1))
  540. (looking-at "^[ \t]*$")))
  541. (back-to-indentation)
  542. (if (< (point) (c-langelem-pos langelem))
  543. ;; Align with the comment starter rather than
  544. ;; with the code before it.
  545. (goto-char (c-langelem-pos langelem)))))
  546. (vector (current-column)))))))
  547. (defun c-lineup-comment (langelem)
  548. "Line up a comment start according to `c-comment-only-line-offset'.
  549. If the comment is lined up with a comment starter on the previous
  550. line, that alignment is preserved.
  551. Works with: comment-intro."
  552. (save-excursion
  553. (back-to-indentation)
  554. (let ((col (current-column)))
  555. (cond
  556. ;; CASE 1: preserve aligned comments
  557. ((save-excursion
  558. (and (c-backward-single-comment)
  559. (= col (current-column))))
  560. (vector col)) ; Return an absolute column.
  561. ;; indent as specified by c-comment-only-line-offset
  562. ((not (bolp))
  563. (or (car-safe c-comment-only-line-offset)
  564. c-comment-only-line-offset))
  565. (t
  566. (or (cdr-safe c-comment-only-line-offset)
  567. (car-safe c-comment-only-line-offset)
  568. -1000)) ;jam it against the left side
  569. ))))
  570. (defun c-lineup-knr-region-comment (langelem)
  571. "Line up a comment in the \"K&R region\" with the declaration.
  572. That is the region between the function or class header and the
  573. beginning of the block. E.g:
  574. int main()
  575. /* This is the main function. */ <- c-lineup-knr-region-comment
  576. {
  577. return 0;
  578. }
  579. Return nil if called in any other situation, to be useful in list
  580. expressions.
  581. Works with: comment-intro."
  582. (when (or (assq 'topmost-intro-cont c-syntactic-context)
  583. (assq 'func-decl-cont c-syntactic-context)
  584. (assq 'knr-argdecl-intro c-syntactic-context)
  585. (assq 'lambda-intro-cont c-syntactic-context))
  586. (save-excursion
  587. (beginning-of-line)
  588. (c-beginning-of-statement-1)
  589. (vector (current-column)))))
  590. (defun c-lineup-runin-statements (langelem)
  591. "Line up statements when the first statement is on the same line as
  592. the block opening brace. E.g:
  593. int main()
  594. { puts (\"Hello world!\");
  595. return 0; <- c-lineup-runin-statements
  596. }
  597. If there is no statement after the opening brace to align with, nil is
  598. returned. This makes the function usable in list expressions.
  599. Works with: The `statement' syntactic symbol."
  600. (if (eq (char-after (c-langelem-pos langelem)) ?{)
  601. (save-excursion
  602. (if (c-langelem-pos langelem)
  603. (goto-char (c-langelem-pos langelem)))
  604. (forward-char 1)
  605. (skip-chars-forward " \t")
  606. (unless (eolp)
  607. (vector (current-column))))))
  608. (defun c-lineup-assignments (langelem)
  609. "Line up the current line after the assignment operator on the first
  610. line in the statement. If there isn't any, return nil to allow
  611. stacking with other line-up functions. If the current line contains
  612. an assignment operator too, try to align it with the first one.
  613. Works with: topmost-intro-cont, statement-cont, arglist-cont,
  614. arglist-cont-nonempty."
  615. (let (startpos endpos equalp)
  616. (if (eq (c-langelem-sym langelem) 'arglist-cont-nonempty)
  617. ;; If it's an arglist-cont-nonempty then we're only interested
  618. ;; in equal signs outside it. We don't search for a "=" on
  619. ;; the current line since that'd have a different nesting
  620. ;; compared to the one we should align with.
  621. (save-excursion
  622. (save-restriction
  623. (setq endpos (c-langelem-2nd-pos c-syntactic-element))
  624. (narrow-to-region (c-langelem-pos langelem) endpos)
  625. (if (setq startpos (c-up-list-backward endpos))
  626. (setq startpos (1+ startpos))
  627. (setq startpos (c-langelem-pos langelem)))))
  628. (setq startpos (c-langelem-pos langelem)
  629. endpos (point))
  630. ;; Find a syntactically relevant and unnested "=" token on the
  631. ;; current line. equalp is in that case set to the number of
  632. ;; columns to left shift the current line to align it with the
  633. ;; goal column.
  634. (save-excursion
  635. (beginning-of-line)
  636. (when (c-syntactic-re-search-forward
  637. c-assignment-op-regexp
  638. (c-point 'eol) t t t)
  639. (setq equalp (- (or (match-beginning 1)
  640. (match-end 0))
  641. (c-point 'boi))))))
  642. (save-excursion
  643. (goto-char startpos)
  644. (if (or (if (c-syntactic-re-search-forward
  645. c-assignment-op-regexp
  646. (min endpos (c-point 'eol)) t t t)
  647. (progn
  648. (goto-char (or (match-beginning 1)
  649. (match-end 0)))
  650. nil)
  651. t)
  652. (save-excursion
  653. (c-forward-syntactic-ws (c-point 'eol))
  654. (eolp)))
  655. ;; There's no equal sign on the line, or there is one but
  656. ;; nothing follows it.
  657. nil
  658. ;; calculate indentation column after equals and ws, unless
  659. ;; our line contains an equals sign
  660. (if (not equalp)
  661. (progn
  662. (skip-chars-forward " \t")
  663. (setq equalp 0)))
  664. (vector (- (current-column) equalp)))
  665. )))
  666. (defun c-lineup-math (langelem)
  667. "Like `c-lineup-assignments' but indent with `c-basic-offset' if no
  668. assignment operator was found on the first line. I.e. this function
  669. is the same as specifying a list (c-lineup-assignments +). It's
  670. provided for compatibility with old configurations.
  671. Works with: topmost-intro-cont, statement-cont, arglist-cont,
  672. arglist-cont-nonempty."
  673. (or (c-lineup-assignments langelem)
  674. c-basic-offset))
  675. (defun c-lineup-cascaded-calls (langelem)
  676. "Line up \"cascaded calls\" under each other.
  677. If the line begins with \"->\" or \".\" and the preceding line ends
  678. with one or more function calls preceded by the same token, then the
  679. arrow is lined up with the first of those tokens. E.g:
  680. result = proc->add(17)->add(18)
  681. ->add(19) + <- c-lineup-cascaded-calls
  682. offset; <- c-lineup-cascaded-calls (inactive)
  683. In any other situation nil is returned to allow use in list
  684. expressions.
  685. Works with: topmost-intro-cont, statement-cont, arglist-cont,
  686. arglist-cont-nonempty."
  687. (if (and (eq (c-langelem-sym langelem) 'arglist-cont-nonempty)
  688. (not (eq (c-langelem-2nd-pos c-syntactic-element)
  689. (c-most-enclosing-brace (c-parse-state)))))
  690. ;; The innermost open paren is not our one, so don't do
  691. ;; anything. This can occur for arglist-cont-nonempty with
  692. ;; nested arglist starts on the same line.
  693. nil
  694. (save-excursion
  695. (back-to-indentation)
  696. (let ((operator (and (looking-at "->\\|\\.")
  697. (regexp-quote (match-string 0))))
  698. (stmt-start (c-langelem-pos langelem)) col)
  699. (when (and operator
  700. (looking-at operator)
  701. (zerop (c-backward-token-2 1 t stmt-start))
  702. (eq (char-after) ?\()
  703. (zerop (c-backward-token-2 2 t stmt-start))
  704. (looking-at operator))
  705. (setq col (current-column))
  706. (while (and (zerop (c-backward-token-2 1 t stmt-start))
  707. (eq (char-after) ?\()
  708. (zerop (c-backward-token-2 2 t stmt-start))
  709. (looking-at operator))
  710. (setq col (current-column)))
  711. (vector col))))))
  712. (defun c-lineup-string-cont (langelem)
  713. "Line up a continued string under the one it continues.
  714. A continued string in this sense is where a string literal follows
  715. directly after another one. E.g:
  716. result = prefix + \"A message \"
  717. \"string.\"; <- c-lineup-string-cont
  718. In other situations, returns nil, to allow stacking with other
  719. line-up functions.
  720. Works with: topmost-intro-cont, statement-cont, arglist-cont,
  721. arglist-cont-nonempty."
  722. (save-excursion
  723. (back-to-indentation)
  724. (and (looking-at "\\s\"")
  725. (let ((quote (char-after)) pos)
  726. (while (and (progn (c-backward-syntactic-ws)
  727. (eq (char-before) quote))
  728. (c-safe (c-backward-sexp) t)
  729. (/= (setq pos (point)) (c-point 'boi))))
  730. (when pos
  731. (goto-char pos)
  732. (vector (current-column)))))))
  733. (defun c-lineup-template-args (langelem)
  734. "Line up template argument lines under the first argument.
  735. To allow this function to be used in a list expression, nil is
  736. returned if there's no template argument on the first line.
  737. Works with: template-args-cont."
  738. (save-excursion
  739. (c-with-syntax-table c++-template-syntax-table
  740. (beginning-of-line)
  741. (backward-up-list 1)
  742. (if (and (eq (char-after) ?<)
  743. (zerop (c-forward-token-2 1 nil (c-point 'eol))))
  744. (vector (current-column))))))
  745. (defun c-lineup-ObjC-method-call (langelem)
  746. "Line up selector args as Emacs Lisp mode does with function args:
  747. Go to the position right after the message receiver, and if you are at
  748. the end of the line, indent the current line c-basic-offset columns
  749. from the opening bracket; otherwise you are looking at the first
  750. character of the first method call argument, so line up the current
  751. line with it.
  752. Works with: objc-method-call-cont."
  753. (save-excursion
  754. (let* ((extra (save-excursion
  755. (back-to-indentation)
  756. (c-backward-syntactic-ws (c-langelem-pos langelem))
  757. (if (eq (char-before) ?:)
  758. (- c-basic-offset)
  759. 0)))
  760. (open-bracket-pos (c-langelem-pos langelem))
  761. (open-bracket-col (progn
  762. (goto-char open-bracket-pos)
  763. (current-column)))
  764. (target-col (progn
  765. (forward-char)
  766. (c-forward-sexp)
  767. (skip-chars-forward " \t")
  768. (if (eolp)
  769. (+ open-bracket-col c-basic-offset)
  770. (current-column))))
  771. )
  772. (- target-col open-bracket-col extra))))
  773. (defun c-lineup-ObjC-method-call-colons (langelem)
  774. "Line up selector args as Project Builder / XCode: colons of first
  775. selector portions on successive lines are aligned. If no decision can
  776. be made return NIL, so that other lineup methods can be tried. This is
  777. typically chained with `c-lineup-ObjC-method-call'.
  778. Works with: objc-method-call-cont."
  779. (save-excursion
  780. (catch 'no-idea
  781. (let* ((method-arg-len (progn
  782. (back-to-indentation)
  783. (if (search-forward ":" (c-point 'eol) 'move)
  784. (- (point) (c-point 'boi))
  785. ; no complete argument to indent yet
  786. (throw 'no-idea nil))))
  787. (extra (save-excursion
  788. ; indent parameter to argument if needed
  789. (back-to-indentation)
  790. (c-backward-syntactic-ws (c-langelem-pos langelem))
  791. (if (eq ?: (char-before))
  792. c-objc-method-parameter-offset 0)))
  793. (open-bracket-col (c-langelem-col langelem))
  794. (arg-ralign-colon-ofs (progn
  795. (forward-char) ; skip over '['
  796. ; skip over object/class name
  797. ; and first argument
  798. (c-forward-sexp 2)
  799. (if (search-forward ":" (c-point 'eol) 'move)
  800. (- (current-column) open-bracket-col
  801. method-arg-len extra)
  802. ; previous arg has no param
  803. c-objc-method-arg-unfinished-offset))))
  804. (if (>= arg-ralign-colon-ofs c-objc-method-arg-min-delta-to-bracket)
  805. (+ arg-ralign-colon-ofs extra)
  806. (throw 'no-idea nil))))))
  807. (defun c-lineup-ObjC-method-args (langelem)
  808. "Line up the colons that separate args in a method declaration.
  809. The colon on the current line is aligned with the one on the first
  810. line.
  811. Works with: objc-method-args-cont."
  812. (save-excursion
  813. (let* ((here (c-point 'boi))
  814. (curcol (progn (goto-char here) (current-column)))
  815. (eol (c-point 'eol))
  816. (relpos (c-langelem-pos langelem))
  817. (first-col-column (progn
  818. (goto-char relpos)
  819. (skip-chars-forward "^:" eol)
  820. (and (eq (char-after) ?:)
  821. (current-column)))))
  822. (if (not first-col-column)
  823. c-basic-offset
  824. (goto-char here)
  825. (skip-chars-forward "^:" eol)
  826. (if (eq (char-after) ?:)
  827. (+ curcol (- first-col-column (current-column)))
  828. c-basic-offset)))))
  829. (defun c-lineup-ObjC-method-args-2 (langelem)
  830. "Line up the colons that separate args in a method declaration.
  831. The colon on the current line is aligned with the one on the previous
  832. line.
  833. Works with: objc-method-args-cont."
  834. (save-excursion
  835. (let* ((here (c-point 'boi))
  836. (curcol (progn (goto-char here) (current-column)))
  837. (eol (c-point 'eol))
  838. (relpos (c-langelem-pos langelem))
  839. (prev-col-column (progn
  840. (skip-chars-backward "^:" relpos)
  841. (and (eq (char-before) ?:)
  842. (- (current-column) 1)))))
  843. (if (not prev-col-column)
  844. c-basic-offset
  845. (goto-char here)
  846. (skip-chars-forward "^:" eol)
  847. (if (eq (char-after) ?:)
  848. (+ curcol (- prev-col-column (current-column)))
  849. c-basic-offset)))))
  850. (defun c-lineup-inexpr-block (langelem)
  851. "Line up the block for constructs that use a block inside an expression,
  852. e.g. anonymous classes in Java and lambda functions in Pike. The body
  853. is aligned with the start of the header, e.g. with the \"new\" or
  854. \"lambda\" keyword. Returns nil if the block isn't part of such a
  855. construct.
  856. Works with: inlambda, inexpr-statement, inexpr-class."
  857. (save-excursion
  858. (back-to-indentation)
  859. (let* ((paren-state (c-parse-state))
  860. (containing-sexp (c-most-enclosing-brace paren-state))
  861. (res (or (c-looking-at-inexpr-block
  862. (c-safe-position containing-sexp paren-state)
  863. containing-sexp)
  864. (and containing-sexp
  865. (progn (goto-char containing-sexp)
  866. (eq (char-after) ?{))
  867. (progn (setq containing-sexp
  868. (c-most-enclosing-brace paren-state
  869. (point)))
  870. (c-looking-at-inexpr-block
  871. (c-safe-position containing-sexp paren-state)
  872. containing-sexp))))))
  873. (when res
  874. (goto-char (cdr res))
  875. (vector (current-column))))))
  876. (defun c-lineup-whitesmith-in-block (langelem)
  877. "Line up lines inside a block in Whitesmith style.
  878. It's done in a way that works both when the opening brace hangs and
  879. when it doesn't. E.g:
  880. something
  881. { something {
  882. foo; <-> foo; <- c-lineup-whitesmith-in-block
  883. } }
  884. <--> c-basic-offset
  885. In the first case the indentation is kept unchanged, in the
  886. second `c-basic-offset' is added.
  887. Works with: defun-close, defun-block-intro, inline-close, block-close,
  888. brace-list-close, brace-list-intro, statement-block-intro,
  889. arglist-intro, arglist-cont-nonempty, arglist-close, and all in*
  890. symbols, e.g. inclass and inextern-lang."
  891. (save-excursion
  892. (if (and (c-go-up-list-backward)
  893. (= (point) (c-point 'boi)))
  894. nil
  895. c-basic-offset)))
  896. (defun c-lineup-after-whitesmith-blocks (langelem)
  897. "Compensate for Whitesmith style indentation of blocks.
  898. Due to the way CC Mode calculates anchor positions for normal lines
  899. inside blocks, this function is necessary for those lines to get
  900. correct Whitesmith style indentation. Consider the following
  901. examples:
  902. int foo()
  903. {
  904. int foo() {
  905. { a;
  906. a; }
  907. x; <-> x; <- c-lineup-after-whitesmith-blocks
  908. The fact that the line with \"x\" is preceded by a Whitesmith style
  909. indented block in one case and not the other should not affect its
  910. indentation. But since CC Mode in cases like this uses the
  911. indentation of the preceding statement as anchor position, the \"x\"
  912. would in the rightmost case be indented too much if the offset for
  913. `statement' was set simply to zero.
  914. This lineup function corrects for this situation by detecting if the
  915. anchor position is at an open paren character. In that case, it
  916. instead indents relative to the surrounding block just like
  917. `c-lineup-whitesmith-in-block'.
  918. Works with: brace-list-entry, brace-entry-open, statement,
  919. arglist-cont."
  920. (save-excursion
  921. (goto-char (c-langelem-pos langelem))
  922. (when (looking-at "\\s\(")
  923. (if (c-go-up-list-backward)
  924. (let ((pos (point)))
  925. (back-to-indentation)
  926. (if (= pos (point))
  927. (vector (current-column))
  928. (vector (+ (current-column) c-basic-offset))))
  929. (vector 0)))))
  930. (defun c-lineup-cpp-define (langelem)
  931. "Line up macro continuation lines according to the indentation of
  932. the construct preceding the macro. E.g:
  933. v beg of preceding constr v beg of preceding constr
  934. int dribble() {
  935. const char msg[] = if (!running)
  936. \"Some text.\"; error(\"Not running!\");
  937. #define X(A, B) \ #define X(A, B) \
  938. do { \ <-> do { \ <- c-lineup-cpp-define
  939. printf (A, B); \ printf (A, B); \
  940. } while (0) } while (0)
  941. If `c-syntactic-indentation-in-macros' is non-nil, the function
  942. returns the relative indentation to the macro start line to allow
  943. accumulation with other offsets. E.g. in the following cases,
  944. cpp-define-intro is combined with the statement-block-intro that comes
  945. from the \"do {\" that hangs on the \"#define\" line:
  946. int dribble() {
  947. const char msg[] = if (!running)
  948. \"Some text.\"; error(\"Not running!\");
  949. #define X(A, B) do { \ #define X(A, B) do { \
  950. printf (A, B); \ <-> printf (A, B); \ <- c-lineup-cpp-define
  951. this->refs++; \ this->refs++; \
  952. } while (0) <-> } while (0) <- c-lineup-cpp-define
  953. The relative indentation returned by `c-lineup-cpp-define' is zero and
  954. two, respectively, in these two examples. They are then added to the
  955. two column indentation that statement-block-intro gives in both cases
  956. here.
  957. If the relative indentation is zero, then nil is returned instead.
  958. That is useful in a list expression to specify the default indentation
  959. on the top level.
  960. If `c-syntactic-indentation-in-macros' is nil then this function keeps
  961. the current indentation, except for empty lines \(ignoring the ending
  962. backslash) where it takes the indentation from the closest preceding
  963. nonempty line in the macro. If there's no such line in the macro then
  964. the indentation is taken from the construct preceding it, as described
  965. above.
  966. Works with: cpp-define-intro."
  967. (let (offset)
  968. (if c-syntactic-indentation-in-macros
  969. ;; Go to the macro start and do a syntactic analysis of it.
  970. ;; Then remove the cpp-macro element it should contain and
  971. ;; calculate the indentation it then would get.
  972. (save-excursion
  973. (c-beginning-of-macro)
  974. (setq offset (- (c-get-syntactic-indentation
  975. (delete '(cpp-macro) (c-guess-basic-syntax)))
  976. (save-excursion
  977. (back-to-indentation)
  978. (current-column))))
  979. (if (zerop offset)
  980. nil
  981. offset))
  982. ;; Do not indent syntactically inside the macro.
  983. (save-excursion
  984. (let ((macro-start-line (save-excursion
  985. (goto-char (c-query-macro-start))
  986. (beginning-of-line)
  987. (point))))
  988. (beginning-of-line)
  989. ;; Check every line while inside the macro.
  990. (while (and (> (point) macro-start-line)
  991. (looking-at "[ \t]*\\\\?$")
  992. (= (forward-line -1) 0)))
  993. (if (<= (point) macro-start-line)
  994. ;; If we've stepped out of the macro we take the
  995. ;; syntactic offset.
  996. (setq offset (c-get-syntactic-indentation
  997. (delete '(cpp-macro) (c-guess-basic-syntax))))
  998. (setq offset (current-indentation)))
  999. (if (zerop offset)
  1000. nil
  1001. (vector offset)))))))
  1002. ;; Contributed by Kevin Ryde <user42@zip.com.au>.
  1003. (defun c-lineup-gcc-asm-reg (elem)
  1004. "Line up a gcc asm register under one on a previous line.
  1005. asm (\"foo %1, %0\\n\"
  1006. \"bar %0, %1\"
  1007. : \"=r\" (w),
  1008. \"=r\" (x)
  1009. : \"0\" (y),
  1010. \"1\" (z));
  1011. The \"x\" line is aligned to the text after the \":\" on the \"w\" line, and
  1012. similarly \"z\" under \"y\".
  1013. This is done only in an \"asm\" or \"__asm__\" block, and only to
  1014. those lines mentioned. Anywhere else nil is returned. The usual
  1015. arrangement is to have this routine as an extra feature at the start
  1016. of arglist line-ups, e.g.
  1017. (c-lineup-gcc-asm-reg c-lineup-arglist)
  1018. Works with: arglist-cont, arglist-cont-nonempty."
  1019. (let ((orig-pos (point))
  1020. alignto)
  1021. (save-excursion
  1022. (and
  1023. c-opt-asm-stmt-key
  1024. ;; Don't do anything if the innermost open paren isn't our one.
  1025. ;; This can occur for arglist-cont-nonempty with nested arglist
  1026. ;; starts on the same line.
  1027. (or (not (eq (car elem) 'arglist-cont-nonempty))
  1028. (eq (c-langelem-2nd-pos c-syntactic-element)
  1029. (c-most-enclosing-brace (c-parse-state))))
  1030. ;; Find the ":" to align to. Look for this first so as to quickly
  1031. ;; eliminate pretty much all cases which are not for us.
  1032. (re-search-backward "^[ \t]*:[ \t]*\\(.\\)?" (cdr elem) t)
  1033. ;; Must have something after the ":".
  1034. (setq alignto (match-beginning 1))
  1035. ;; Don't touch ":" lines themselves.
  1036. (progn (goto-char orig-pos)
  1037. (beginning-of-line)
  1038. (not (looking-at "^[ \t]*:")))
  1039. ;; Only operate in an asm statement.
  1040. (progn (goto-char orig-pos)
  1041. (c-in-gcc-asm-p))
  1042. (vector (progn (goto-char alignto) (current-column)))))))
  1043. (defun c-lineup-dont-change (langelem)
  1044. "Do not change the indentation of the current line.
  1045. Works with: Any syntactic symbol."
  1046. (save-excursion
  1047. (back-to-indentation)
  1048. (vector (current-column))))
  1049. (defun c-snug-do-while (syntax pos)
  1050. "Dynamically calculate brace hanginess for do-while statements.
  1051. Using this function, `while' clauses that end a `do-while' block will
  1052. remain on the same line as the brace that closes that block.
  1053. See `c-hanging-braces-alist' for how to utilize this function as an
  1054. ACTION associated with `block-close' syntax."
  1055. (save-excursion
  1056. (let (langelem)
  1057. (if (and (eq syntax 'block-close)
  1058. (setq langelem (assq 'block-close c-syntactic-context))
  1059. (progn (goto-char (c-langelem-pos langelem))
  1060. (if (eq (char-after) ?{)
  1061. (c-safe (c-forward-sexp -1)))
  1062. (looking-at "\\<do\\>[^_]")))
  1063. '(before)
  1064. '(before after)))))
  1065. (defun c-snug-1line-defun-close (syntax pos)
  1066. "Determine the brace hanginess for an AWK defun-close.
  1067. If the action/function being closed is a one-liner, keep it so. Otherwise put
  1068. the closing brace on its own line."
  1069. (save-excursion
  1070. (goto-char pos)
  1071. (if (> (c-point 'bol)
  1072. (progn (up-list -1) (point)))
  1073. '(before after)
  1074. '(after))))
  1075. (defun c-gnu-impose-minimum ()
  1076. "Imposes a minimum indentation for lines inside code blocks.
  1077. The variable `c-label-minimum-indentation' specifies the minimum
  1078. indentation amount."
  1079. (when (and (not
  1080. ;; Don't adjust macro or comment-only lines.
  1081. (or (assq 'cpp-macro c-syntactic-context)
  1082. (assq 'comment-intro c-syntactic-context)))
  1083. (c-intersect-lists c-inside-block-syms c-syntactic-context)
  1084. (save-excursion
  1085. (back-to-indentation)
  1086. (< (current-column) c-label-minimum-indentation)))
  1087. (c-shift-line-indentation (- c-label-minimum-indentation
  1088. (current-indentation)))))
  1089. ;; Useful for c-hanging-semi&comma-criteria
  1090. (defun c-semi&comma-inside-parenlist ()
  1091. "Controls newline insertion after semicolons in parenthesis lists.
  1092. If a comma was inserted, no determination is made. If a semicolon was
  1093. inserted inside a parenthesis list, no newline is added otherwise a
  1094. newline is added. In either case, checking is stopped. This supports
  1095. exactly the old newline insertion behavior."
  1096. ;; newline only after semicolon, but only if that semicolon is not
  1097. ;; inside a parenthesis list (e.g. a for loop statement)
  1098. (if (not (eq last-command-event ?\;))
  1099. nil ; continue checking
  1100. (if (condition-case nil
  1101. (save-excursion
  1102. (up-list -1)
  1103. (not (eq (char-after) ?\()))
  1104. (error t))
  1105. t
  1106. 'stop)))
  1107. ;; Suppresses newlines before non-blank lines
  1108. (defun c-semi&comma-no-newlines-before-nonblanks ()
  1109. "Controls newline insertion after semicolons.
  1110. If a comma was inserted, no determination is made. If a semicolon was
  1111. inserted, and the following line is not blank, no newline is inserted.
  1112. Otherwise, no determination is made."
  1113. (save-excursion
  1114. (if (and (= last-command-event ?\;)
  1115. ;;(/= (point-max)
  1116. ;; (save-excursion (skip-syntax-forward " ") (point))
  1117. (zerop (forward-line 1))
  1118. (bolp) ; forward-line has funny behavior at eob.
  1119. (not (looking-at "^[ \t]*$")))
  1120. 'stop
  1121. nil)))
  1122. ;; Suppresses new lines after semicolons in one-liners methods
  1123. (defun c-semi&comma-no-newlines-for-oneline-inliners ()
  1124. "Controls newline insertion after semicolons for some one-line methods.
  1125. If a comma was inserted, no determination is made. Newlines are
  1126. suppressed in one-liners, if the line is an in-class inline function.
  1127. For other semicolon contexts, no determination is made."
  1128. (let ((syntax (c-guess-basic-syntax))
  1129. (bol (save-excursion
  1130. (if (c-safe (up-list -1) t)
  1131. (c-point 'bol)
  1132. -1))))
  1133. (if (and (eq last-command-event ?\;)
  1134. (eq (car (car syntax)) 'inclass)
  1135. (eq (car (car (cdr syntax))) 'topmost-intro)
  1136. (= (c-point 'bol) bol))
  1137. 'stop
  1138. nil)))
  1139. (cc-provide 'cc-align)
  1140. ;;; cc-align.el ends here