Mercurial > emacs
comparison lisp/progmodes/cc-align.el @ 44728:7a3ac6c387fe
CC Mode update to version 5.29. This is for testing; it's not a released
version.
author | Martin Stjernholm <mast@lysator.liu.se> |
---|---|
date | Mon, 22 Apr 2002 00:35:36 +0000 |
parents | 7a94f1c588c4 |
children | 4fd35ed09e9b d7ddb3e565de |
comparison
equal
deleted
inserted
replaced
44727:3936c522b4d3 | 44728:7a3ac6c387fe |
---|---|
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | 23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 ;; GNU General Public License for more details. | 25 ;; GNU General Public License for more details. |
26 | 26 |
27 ;; You should have received a copy of the GNU General Public License | 27 ;; You should have received a copy of the GNU General Public License |
28 ;; along with this program; see the file COPYING. If not, write to | 28 ;; along with GNU Emacs; see the file COPYING. If not, write to |
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
30 ;; Boston, MA 02111-1307, USA. | 30 ;; Boston, MA 02111-1307, USA. |
31 | 31 |
32 ;;; Commentary: | 32 ;;; Commentary: |
33 | 33 |
47 (cc-require 'cc-engine) | 47 (cc-require 'cc-engine) |
48 | 48 |
49 | 49 |
50 ;; Standard indentation line-ups | 50 ;; Standard indentation line-ups |
51 | 51 |
52 (defun c-lineup-topmost-intro-cont (langelem) | |
53 "Line up declaration continuation lines zero or one indentation step. | |
54 For lines in the \"header\" of a definition, zero is used. For other | |
55 lines, `c-basic-offset' is added to the indentation. E.g: | |
56 | |
57 int | |
58 neg (int i) <- c-lineup-topmost-intro-cont | |
59 { | |
60 return -i; | |
61 } | |
62 | |
63 struct | |
64 larch <- c-lineup-topmost-intro-cont | |
65 { | |
66 double height; | |
67 } | |
68 the_larch, <- c-lineup-topmost-intro-cont | |
69 another_larch; <- c-lineup-topmost-intro-cont | |
70 <--> c-basic-offset | |
71 | |
72 struct larch | |
73 the_larch, <- c-lineup-topmost-intro-cont | |
74 another_larch; <- c-lineup-topmost-intro-cont | |
75 | |
76 \(This function is mainly provided to mimic the behavior of CC Mode | |
77 5.28 and earlier where this case wasn't handled consistently so that | |
78 these lines could be analyzed as either topmost-intro-cont or | |
79 statement-cont.) | |
80 | |
81 Works with: topmost-intro-cont." | |
82 (save-excursion | |
83 (beginning-of-line) | |
84 (c-backward-syntactic-ws (cdr langelem)) | |
85 (if (memq (char-before) '(?} ?,)) | |
86 c-basic-offset))) | |
87 | |
52 (defun c-lineup-arglist (langelem) | 88 (defun c-lineup-arglist (langelem) |
53 "Line up the current argument line under the first argument. | 89 "Line up the current argument line under the first argument. |
54 | 90 |
55 Works with: arglist-cont-nonempty." | 91 Works with: arglist-cont-nonempty, arglist-close." |
56 (save-excursion | 92 (save-excursion |
57 (let* ((containing-sexp | 93 (beginning-of-line) |
58 (save-excursion | 94 (let ((containing-sexp (c-most-enclosing-brace (c-parse-state)))) |
59 ;; arglist-cont-nonempty gives relpos == | 95 (goto-char (1+ containing-sexp)) |
60 ;; to boi of containing-sexp paren. This | 96 (let ((eol (c-point 'eol))) |
61 ;; is good when offset is +, but bad | 97 (c-forward-syntactic-ws) |
62 ;; when it is c-lineup-arglist, so we | 98 (when (< (point) eol) |
63 ;; have to special case a kludge here. | 99 (goto-char (1+ containing-sexp)) |
64 (if (memq (car langelem) '(arglist-intro arglist-cont-nonempty)) | 100 (skip-chars-forward " \t"))) |
65 (progn | 101 (vector (current-column))))) |
66 (beginning-of-line) | 102 |
67 (backward-up-list 1) | 103 ;; Contributed by Kevin Ryde <user42@zip.com.au>. |
68 (skip-chars-forward " \t" (c-point 'eol))) | 104 (defun c-lineup-argcont (elem) |
69 (goto-char (cdr langelem))) | 105 "Line up a continued argument. |
70 (point))) | 106 |
71 (langelem-col (c-langelem-col langelem t))) | 107 foo (xyz, aaa + bbb + ccc |
72 (if (save-excursion | 108 + ddd + eee + fff); <- c-lineup-argcont |
73 (beginning-of-line) | 109 |
74 (looking-at "[ \t]*)")) | 110 Only continuation lines like this are touched, `nil' is returned on lines |
75 (progn (goto-char (match-end 0)) | 111 which are the start of an argument. |
76 (c-forward-sexp -1) | 112 |
77 (forward-char 1) | 113 Within a gcc asm block, \":\" is recognised as an argument separator, |
78 (c-forward-syntactic-ws) | 114 but of course only between operand specifications, not in the expressions |
79 (- (current-column) langelem-col)) | 115 for the operands. |
80 (goto-char containing-sexp) | 116 |
81 (or (eolp) | 117 Works with: arglist-cont, arglist-cont-nonempty." |
82 (not (memq (char-after) '(?{ ?\( ?\[))) | 118 |
83 (let ((eol (c-point 'eol)) | 119 (save-excursion |
84 (here (progn | 120 (beginning-of-line) |
85 (forward-char 1) | 121 (let ((bol (point))) |
86 (skip-chars-forward " \t") | 122 |
87 (point)))) | 123 ;; Previous line ending in a comma means we're the start of an |
88 (c-forward-syntactic-ws) | 124 ;; argument. This should quickly catch most cases not for us. |
89 (if (< (point) eol) | 125 (c-backward-syntactic-ws) |
90 (goto-char here)))) | 126 (let ((c (char-before))) |
91 (- (current-column) langelem-col) | 127 (unless (eq c ?,) |
92 )))) | 128 |
129 ;; In a gcc asm, ":" on the previous line means the start of an | |
130 ;; argument. And lines starting with ":" are not for us, don't | |
131 ;; want them to indent to the preceding operand. | |
132 (let ((gcc-asm (save-excursion | |
133 (goto-char bol) | |
134 (c-in-gcc-asm-p)))) | |
135 (unless (and gcc-asm | |
136 (or (eq c ?:) | |
137 (save-excursion | |
138 (goto-char bol) | |
139 (looking-at "[ \t]*:")))) | |
140 | |
141 (c-lineup-argcont-scan (if gcc-asm ?:)) | |
142 (vector (current-column))))))))) | |
143 | |
144 (defun c-lineup-argcont-scan (&optional other-match) | |
145 ;; Find the start of an argument, for `c-lineup-argcont'. | |
146 (when (eq 0 (c-backward-token-1 1 t)) | |
147 (let ((c (char-after))) | |
148 (if (or (eq c ?,) (eq c other-match)) | |
149 (progn | |
150 (forward-char) | |
151 (c-forward-syntactic-ws)) | |
152 (c-lineup-argcont-scan other-match))))) | |
93 | 153 |
94 (defun c-lineup-arglist-intro-after-paren (langelem) | 154 (defun c-lineup-arglist-intro-after-paren (langelem) |
95 "Line up a line just after the open paren of the surrounding paren or | 155 "Line up a line just after the open paren of the surrounding paren or |
96 brace block. | 156 brace block. |
97 | 157 |
98 Works with: defun-block-intro, brace-list-intro, | 158 Works with: defun-block-intro, brace-list-intro, |
99 statement-block-intro, statement-case-intro, arglist-intro." | 159 statement-block-intro, statement-case-intro, arglist-intro." |
100 (save-excursion | 160 (save-excursion |
101 (let ((langelem-col (c-langelem-col langelem t)) | 161 (beginning-of-line) |
102 (ce-curcol (save-excursion | 162 (backward-up-list 1) |
103 (beginning-of-line) | 163 (skip-chars-forward " \t" (c-point 'eol)) |
104 (backward-up-list 1) | 164 (vector (1+ (current-column))))) |
105 (skip-chars-forward " \t" (c-point 'eol)) | |
106 (current-column)))) | |
107 (- ce-curcol langelem-col -1)))) | |
108 | 165 |
109 (defun c-lineup-arglist-close-under-paren (langelem) | 166 (defun c-lineup-arglist-close-under-paren (langelem) |
110 "Line up a closing paren line under the corresponding open paren. | 167 "Line up a closing paren line under the corresponding open paren. |
111 | 168 |
112 Works with: defun-close, class-close, inline-close, block-close, | 169 Works with: defun-close, class-close, inline-close, block-close, |
113 brace-list-close, arglist-close, extern-lang-close, namespace-close | 170 brace-list-close, arglist-close, extern-lang-close, namespace-close |
114 \(for most of these, a zero offset will normally produce the same | 171 \(for most of these, a zero offset will normally produce the same |
115 result, though)." | 172 result, though)." |
116 (save-excursion | 173 (save-excursion |
117 (let ((langelem-col (c-langelem-col langelem t)) | 174 (beginning-of-line) |
118 (ce-curcol (save-excursion | 175 (backward-up-list 1) |
119 (beginning-of-line) | 176 (vector (current-column)))) |
120 (backward-up-list 1) | |
121 (current-column)))) | |
122 (- ce-curcol langelem-col)))) | |
123 | 177 |
124 (defun c-lineup-close-paren (langelem) | 178 (defun c-lineup-close-paren (langelem) |
125 "Line up the closing paren under its corresponding open paren if the | 179 "Line up the closing paren under its corresponding open paren if the |
126 open paren is followed by code. If the open paren ends its line, no | 180 open paren is followed by code. If the open paren ends its line, no |
127 indentation is added. E.g: | 181 indentation is added. E.g: |
145 (c-forward-syntactic-ws) | 199 (c-forward-syntactic-ws) |
146 (forward-char 1))) | 200 (forward-char 1))) |
147 (c-forward-syntactic-ws (c-point 'eol)) | 201 (c-forward-syntactic-ws (c-point 'eol)) |
148 (if (eolp) | 202 (if (eolp) |
149 0 | 203 0 |
150 (- opencol (c-langelem-col langelem t)))) | 204 (vector opencol))) |
151 (error nil)))) | 205 (error nil)))) |
152 | 206 |
153 (defun c-lineup-streamop (langelem) | 207 (defun c-lineup-streamop (langelem) |
154 "Line up C++ stream operators under each other. | 208 "Line up C++ stream operators under each other. |
155 | 209 |
156 Works with: stream-op." | 210 Works with: stream-op." |
157 (save-excursion | 211 (save-excursion |
158 (let ((langelem-col (c-langelem-col langelem))) | 212 (goto-char (cdr langelem)) |
159 (re-search-forward "<<\\|>>" (c-point 'eol) 'move) | 213 (re-search-forward "<<\\|>>" (c-point 'eol) 'move) |
160 (goto-char (match-beginning 0)) | 214 (goto-char (match-beginning 0)) |
161 (- (current-column) langelem-col)))) | 215 (vector (current-column)))) |
162 | 216 |
163 (defun c-lineup-multi-inher (langelem) | 217 (defun c-lineup-multi-inher (langelem) |
164 "Line up the classes in C++ multiple inheritance clauses and member | 218 "Line up the classes in C++ multiple inheritance clauses and member |
165 initializers under each other. E.g: | 219 initializers under each other. E.g: |
166 | 220 |
180 (save-excursion | 234 (save-excursion |
181 (let* ((eol (c-point 'eol)) | 235 (let* ((eol (c-point 'eol)) |
182 (here (point)) | 236 (here (point)) |
183 (char-after-ip (progn | 237 (char-after-ip (progn |
184 (skip-chars-forward " \t") | 238 (skip-chars-forward " \t") |
185 (char-after))) | 239 (char-after)))) |
186 (langelem-col (c-langelem-col langelem))) | 240 (if (cdr langelem) (goto-char (cdr langelem))) |
187 | 241 |
188 ;; This kludge is necessary to support both inher-cont and | 242 ;; This kludge is necessary to support both inher-cont and |
189 ;; member-init-cont, since they have different anchor positions. | 243 ;; member-init-cont, since they have different anchor positions. |
190 (c-backward-syntactic-ws) | 244 (c-backward-syntactic-ws) |
191 (when (eq (char-before) ?:) | 245 (when (eq (char-before) ?:) |
197 (skip-chars-forward " \t" eol) | 251 (skip-chars-forward " \t" eol) |
198 (skip-chars-forward " \t:" eol)) | 252 (skip-chars-forward " \t:" eol)) |
199 (if (or (eolp) | 253 (if (or (eolp) |
200 (looking-at c-comment-start-regexp)) | 254 (looking-at c-comment-start-regexp)) |
201 (c-forward-syntactic-ws here)) | 255 (c-forward-syntactic-ws here)) |
202 (- (current-column) langelem-col) | 256 (vector (current-column)) |
203 ))) | 257 ))) |
204 | 258 |
205 (defun c-lineup-java-inher (langelem) | 259 (defun c-lineup-java-inher (langelem) |
206 "Line up Java implements and extends declarations. | 260 "Line up Java implements and extends declarations. |
207 If class names follows on the same line as the implements/extends | 261 If class names follows on the same line as the implements/extends |
214 Bar <-> Bar <- c-lineup-java-inher | 268 Bar <-> Bar <- c-lineup-java-inher |
215 <--> c-basic-offset | 269 <--> c-basic-offset |
216 | 270 |
217 Works with: inher-cont." | 271 Works with: inher-cont." |
218 (save-excursion | 272 (save-excursion |
219 (let ((langelem-col (c-langelem-col langelem))) | 273 (goto-char (cdr langelem)) |
220 (forward-word 1) | 274 (forward-word 1) |
221 (if (looking-at "[ \t]*$") | 275 (if (looking-at "[ \t]*$") |
222 c-basic-offset | 276 c-basic-offset |
223 (c-forward-syntactic-ws) | 277 (c-forward-syntactic-ws) |
224 (- (current-column) langelem-col))))) | 278 (vector (current-column))))) |
225 | 279 |
226 (defun c-lineup-java-throws (langelem) | 280 (defun c-lineup-java-throws (langelem) |
227 "Line up Java throws declarations. | 281 "Line up Java throws declarations. |
228 If exception names follows on the same line as the throws keyword, | 282 If exception names follows on the same line as the throws keyword, |
229 they are lined up under each other. Otherwise, they are indented by | 283 they are lined up under each other. Otherwise, they are indented by |
244 (while (zerop (c-forward-token-1 1 t lim)) | 298 (while (zerop (c-forward-token-1 1 t lim)) |
245 (if (looking-at "throws\\>[^_]") | 299 (if (looking-at "throws\\>[^_]") |
246 (throw 'done t)))))) | 300 (throw 'done t)))))) |
247 (if throws | 301 (if throws |
248 (if (zerop (c-forward-token-1 1 nil (c-point 'eol))) | 302 (if (zerop (c-forward-token-1 1 nil (c-point 'eol))) |
249 (- (current-column) (c-langelem-col langelem)) | 303 (vector (current-column)) |
250 (back-to-indentation) | 304 (back-to-indentation) |
251 (+ (- (current-column) (c-langelem-col langelem)) | 305 (vector (+ (current-column) c-basic-offset))) |
252 c-basic-offset)) | |
253 c-basic-offset)))) | 306 c-basic-offset)))) |
254 | 307 |
255 (defun c-indent-one-line-block (langelem) | 308 (defun c-indent-one-line-block (langelem) |
256 "Indent a one line block `c-basic-offset' extra. | 309 "Indent a one line block `c-basic-offset' extra. |
257 E.g: | 310 E.g: |
330 (let* ((here (point)) | 383 (let* ((here (point)) |
331 (prefixlen (progn (back-to-indentation) | 384 (prefixlen (progn (back-to-indentation) |
332 (if (looking-at c-current-comment-prefix) | 385 (if (looking-at c-current-comment-prefix) |
333 (- (match-end 0) (point)) | 386 (- (match-end 0) (point)) |
334 0))) | 387 0))) |
335 (starterlen (save-excursion | 388 (starterlen |
336 (goto-char (cdr langelem)) | 389 ;; Get the length of the comment starter, not including |
337 (looking-at comment-start-skip) | 390 ;; the first '/'. We check if the comment prefix matched |
338 (- (save-excursion | 391 ;; on the current line matches the starter or if it |
339 (goto-char (match-end 0)) | 392 ;; matches comment-start-skip, and choose whichever is |
340 (skip-chars-backward " \t") | 393 ;; longest. |
341 (point)) | 394 (max (save-excursion |
342 (or (match-end 1) (point)) | 395 (goto-char (1+ (cdr langelem))) |
343 1))) ; Don't count the first '/'. | 396 (if (and (match-string 0) |
344 (langelem-col (save-excursion (c-langelem-col langelem)))) | 397 (looking-at (regexp-quote (match-string 0)))) |
398 (- (match-end 0) (match-beginning 0)) | |
399 0)) | |
400 (save-excursion | |
401 (goto-char (cdr langelem)) | |
402 (looking-at comment-start-skip) | |
403 (- (or (match-end 1) | |
404 (save-excursion | |
405 (goto-char (match-end 0)) | |
406 (skip-chars-backward " \t") | |
407 (point))) | |
408 (point) | |
409 1))))) | |
345 (if (and (> starterlen 10) (zerop prefixlen)) | 410 (if (and (> starterlen 10) (zerop prefixlen)) |
346 ;; The comment has a long starter and the line doesn't have | 411 ;; The comment has a long starter and the line doesn't have |
347 ;; a nonempty comment prefix. Treat it as free form text | 412 ;; a nonempty comment prefix. Treat it as free form text |
348 ;; and don't change the indentation. | 413 ;; and don't change the indentation. |
349 (- (current-column) langelem-col) | 414 (vector (current-column)) |
350 (forward-line -1) | 415 (forward-line -1) |
351 (back-to-indentation) | 416 (back-to-indentation) |
352 (if (>= (cdr langelem) (point)) | 417 (if (>= (cdr langelem) (point)) |
353 ;; On the second line in the comment. | 418 ;; On the second line in the comment. |
354 (if (zerop prefixlen) | 419 (if (zerop prefixlen) |
355 ;; No nonempty comment prefix. Align after comment | 420 ;; No nonempty comment prefix. Align after comment |
356 ;; starter. | 421 ;; starter. |
357 (progn | 422 (progn |
358 (goto-char (match-end 0)) | 423 (goto-char (match-end 0)) |
359 (if (looking-at "\\([ \t]+\\).+$") | 424 ;; The following should not be necessary, since |
360 ;; Align with the text that hangs after the | 425 ;; comment-start-skip should match everything (i.e. |
361 ;; comment starter. | 426 ;; typically whitespace) that leads up to the text. |
362 (goto-char (match-end 1))) | 427 ;;(if (looking-at "\\([ \t]+\\).+$") |
363 (- (current-column) langelem-col)) | 428 ;; ;; Align with the text that hangs after the |
429 ;; ;; comment starter. | |
430 ;; (goto-char (match-end 1))) | |
431 (vector (current-column))) | |
364 ;; How long is the comment starter? if greater than the | 432 ;; How long is the comment starter? if greater than the |
365 ;; length of the comment prefix, align left. if less | 433 ;; length of the comment prefix, align left. if less |
366 ;; than or equal, align right. this should also pick up | 434 ;; than or equal, align right. this should also pick up |
367 ;; Javadoc style comments. | 435 ;; Javadoc style comments. |
368 (if (> starterlen prefixlen) | 436 (if (> starterlen prefixlen) |
369 (progn | 437 (progn |
370 (goto-char (cdr langelem)) | 438 (goto-char (cdr langelem)) |
371 (- (current-column) -1 langelem-col)) | 439 (vector (1+ (current-column)))) |
372 (goto-char (match-end 0)) | 440 (goto-char (+ (cdr langelem) starterlen 1)) |
373 (skip-chars-backward " \t") | 441 (vector (- (current-column) prefixlen)))) |
374 (- (current-column) prefixlen langelem-col))) | |
375 ;; Not on the second line in the comment. If the previous | 442 ;; Not on the second line in the comment. If the previous |
376 ;; line has a nonempty comment prefix, align with it. | 443 ;; line has a nonempty comment prefix, align with it. |
377 ;; Otherwise, align with the previous nonempty line, but | 444 ;; Otherwise, align with the previous nonempty line, but |
378 ;; align the comment ender with the starter. | 445 ;; align the comment ender with the starter. |
379 (when (or (not (looking-at c-current-comment-prefix)) | 446 (when (or (not (looking-at c-current-comment-prefix)) |
387 (back-to-indentation) | 454 (back-to-indentation) |
388 (if (< (point) (cdr langelem)) | 455 (if (< (point) (cdr langelem)) |
389 ;; Align with the comment starter rather than | 456 ;; Align with the comment starter rather than |
390 ;; with the code before it. | 457 ;; with the code before it. |
391 (goto-char (cdr langelem))))) | 458 (goto-char (cdr langelem))))) |
392 (- (current-column) langelem-col)))))) | 459 (vector (current-column))))))) |
393 | 460 |
394 (defun c-lineup-comment (langelem) | 461 (defun c-lineup-comment (langelem) |
395 "Line up a comment start according to `c-comment-only-line-offset'. | 462 "Line up a comment start according to `c-comment-only-line-offset'. |
396 If the comment is lined up with a comment starter on the previous | 463 If the comment is lined up with a comment starter on the previous |
397 line, that alignment is preserved. | 464 line, that alignment is preserved. |
414 (or (cdr-safe c-comment-only-line-offset) | 481 (or (cdr-safe c-comment-only-line-offset) |
415 (car-safe c-comment-only-line-offset) | 482 (car-safe c-comment-only-line-offset) |
416 -1000)) ;jam it against the left side | 483 -1000)) ;jam it against the left side |
417 )))) | 484 )))) |
418 | 485 |
486 (defun c-lineup-knr-region-comment (langelem) | |
487 "Line up a comment in the \"K&R region\" with the declaration. | |
488 That is the region between the function or class header and the | |
489 beginning of the block. E.g: | |
490 | |
491 int main() | |
492 /* This is the main function. */ <- c-lineup-knr-region-comment | |
493 { | |
494 return 0; | |
495 } | |
496 | |
497 Return nil if called in any other situation, to be useful in list | |
498 expressions. | |
499 | |
500 Works with: comment-intro." | |
501 (when (or (assq 'topmost-intro-cont c-syntactic-context) | |
502 (assq 'func-decl-cont c-syntactic-context) | |
503 (assq 'knr-argdecl-intro c-syntactic-context) | |
504 (assq 'lambda-intro-cont c-syntactic-context)) | |
505 (save-excursion | |
506 (beginning-of-line) | |
507 (c-beginning-of-statement-1) | |
508 (vector (current-column))))) | |
509 | |
419 (defun c-lineup-runin-statements (langelem) | 510 (defun c-lineup-runin-statements (langelem) |
420 "Line up statements when the first statement is on the same line as | 511 "Line up statements when the first statement is on the same line as |
421 the block opening brace. E.g: | 512 the block opening brace. E.g: |
422 | 513 |
423 int main() | 514 int main() |
429 returned. This makes the function usable in list expressions. | 520 returned. This makes the function usable in list expressions. |
430 | 521 |
431 Works with: The `statement' syntactic symbol." | 522 Works with: The `statement' syntactic symbol." |
432 (if (eq (char-after (cdr langelem)) ?{) | 523 (if (eq (char-after (cdr langelem)) ?{) |
433 (save-excursion | 524 (save-excursion |
434 (let ((langelem-col (c-langelem-col langelem))) | 525 (if (cdr langelem) (goto-char (cdr langelem))) |
435 (forward-char 1) | 526 (forward-char 1) |
436 (skip-chars-forward " \t") | 527 (skip-chars-forward " \t") |
437 (unless (eolp) | 528 (unless (eolp) |
438 (- (current-column) langelem-col)))))) | 529 (vector (current-column)))))) |
439 | 530 |
440 (defun c-lineup-math (langelem) | 531 (defun c-lineup-math (langelem) |
441 "Line up the current line after the equal sign on the first line in | 532 "Line up the current line after the equal sign on the first line in |
442 the statement. If there isn't any, indent with `c-basic-offset'. If | 533 the statement. If there isn't any, indent with `c-basic-offset'. If |
443 the current line contains an equal sign too, try to align it with the | 534 the current line contains an equal sign too, try to align it with the |
444 first one. | 535 first one. |
445 | 536 |
446 Works with: statement-cont." | 537 Works with: statement-cont, arglist-cont, arglist-cont-nonempty." |
447 (save-excursion | 538 (save-excursion |
448 (let ((equalp (save-excursion | 539 (let ((equalp (save-excursion |
449 (goto-char (c-point 'boi)) | 540 (goto-char (c-point 'boi)) |
450 (let ((eol (c-point 'eol))) | 541 (let ((eol (c-point 'eol))) |
451 (c-forward-token-1 0 t eol) | 542 (c-forward-token-1 0 t eol) |
452 (while (and (not (eq (char-after) ?=)) | 543 (while (and (not (eq (char-after) ?=)) |
453 (= (c-forward-token-1 1 t eol) 0)))) | 544 (= (c-forward-token-1 1 t eol) 0)))) |
454 (and (eq (char-after) ?=) | 545 (and (eq (char-after) ?=) |
455 (- (point) (c-point 'boi))))) | 546 (- (point) (c-point 'boi))))) |
456 (langelem-col (c-langelem-col langelem)) | |
457 donep) | 547 donep) |
548 (if (cdr langelem) (goto-char (cdr langelem))) | |
458 (while (and (not donep) | 549 (while (and (not donep) |
459 (< (point) (c-point 'eol))) | 550 (< (point) (c-point 'eol))) |
460 (skip-chars-forward "^=" (c-point 'eol)) | 551 (skip-chars-forward "^=" (c-point 'eol)) |
461 (if (c-in-literal (cdr langelem)) | 552 (if (c-in-literal (cdr langelem)) |
462 (forward-char 1) | 553 (forward-char 1) |
473 (if (not equalp) | 564 (if (not equalp) |
474 (progn | 565 (progn |
475 (forward-char 1) | 566 (forward-char 1) |
476 (skip-chars-forward " \t") | 567 (skip-chars-forward " \t") |
477 (setq equalp 0))) | 568 (setq equalp 0))) |
478 (- (current-column) equalp langelem-col)) | 569 (vector (- (current-column) equalp))) |
479 ))) | 570 ))) |
571 | |
572 (defun c-lineup-cascaded-calls (langelem) | |
573 "Line up \"cascaded calls\" under each other. | |
574 If the line begins with \"->\" and the preceding line ends with one or | |
575 more function calls preceded by \"->\", then the arrow is lined up with | |
576 the first of those \"->\". E.g: | |
577 | |
578 result = proc->add(17)->add(18) | |
579 ->add(19) + <- c-lineup-cascaded-calls | |
580 offset; <- c-lineup-cascaded-calls (inactive) | |
581 | |
582 In any other situation nil is returned to allow use in list | |
583 expressions. | |
584 | |
585 Works with: statement-cont, arglist-cont, arglist-cont-nonempty." | |
586 (save-excursion | |
587 (let ((bopl (c-point 'bopl)) col) | |
588 (back-to-indentation) | |
589 (when (and (looking-at "->") | |
590 (= (c-backward-token-1 1 t bopl) 0) | |
591 (eq (char-after) ?\() | |
592 (= (c-backward-token-1 3 t bopl) 0) | |
593 (looking-at "->")) | |
594 (setq col (current-column)) | |
595 (while (and (= (c-backward-token-1 1 t bopl) 0) | |
596 (eq (char-after) ?\() | |
597 (= (c-backward-token-1 3 t bopl) 0) | |
598 (looking-at "->")) | |
599 (setq col (current-column))) | |
600 (vector col))))) | |
480 | 601 |
481 (defun c-lineup-template-args (langelem) | 602 (defun c-lineup-template-args (langelem) |
482 "Line up template argument lines under the first argument. | 603 "Line up template argument lines under the first argument. |
483 To allow this function to be used in a list expression, nil is | 604 To allow this function to be used in a list expression, nil is |
484 returned if there's no template argument on the first line. | 605 returned if there's no template argument on the first line. |
488 (c-with-syntax-table c++-template-syntax-table | 609 (c-with-syntax-table c++-template-syntax-table |
489 (beginning-of-line) | 610 (beginning-of-line) |
490 (backward-up-list 1) | 611 (backward-up-list 1) |
491 (if (and (eq (char-after) ?<) | 612 (if (and (eq (char-after) ?<) |
492 (zerop (c-forward-token-1 1 nil (c-point 'eol)))) | 613 (zerop (c-forward-token-1 1 nil (c-point 'eol)))) |
493 (- (current-column) (c-langelem-col langelem)))))) | 614 (vector (current-column)))))) |
494 | 615 |
495 (defun c-lineup-ObjC-method-call (langelem) | 616 (defun c-lineup-ObjC-method-call (langelem) |
496 "Line up selector args as elisp-mode does with function args: | 617 "Line up selector args as elisp-mode does with function args: |
497 Go to the position right after the message receiver, and if you are at | 618 Go to the position right after the message receiver, and if you are at |
498 the end of the line, indent the current line c-basic-offset columns | 619 the end of the line, indent the current line c-basic-offset columns |
577 construct. | 698 construct. |
578 | 699 |
579 Works with: inlambda, inexpr-statement, inexpr-class." | 700 Works with: inlambda, inexpr-statement, inexpr-class." |
580 (save-excursion | 701 (save-excursion |
581 (back-to-indentation) | 702 (back-to-indentation) |
582 (let ((res (or (c-looking-at-inexpr-block) | 703 (let* ((paren-state (c-parse-state)) |
583 (if (c-safe (backward-up-list 1) | 704 (containing-sexp (c-most-enclosing-brace paren-state)) |
584 (eq (char-after) ?{)) | 705 (res (or (c-looking-at-inexpr-block |
585 (c-looking-at-inexpr-block))))) | 706 (c-safe-position containing-sexp paren-state) |
707 containing-sexp) | |
708 (and containing-sexp | |
709 (progn (goto-char containing-sexp) | |
710 (eq (char-after) ?{)) | |
711 (progn (setq containing-sexp | |
712 (c-most-enclosing-brace paren-state | |
713 (point))) | |
714 (c-looking-at-inexpr-block | |
715 (c-safe-position containing-sexp paren-state) | |
716 containing-sexp)))))) | |
586 (when res | 717 (when res |
587 (goto-char (cdr res)) | 718 (goto-char (cdr res)) |
588 (- (current-column) | 719 (- (current-column) |
589 (progn | 720 (progn |
590 (back-to-indentation) | 721 (back-to-indentation) |
611 (goto-char (cdr langelem)) | 742 (goto-char (cdr langelem)) |
612 (back-to-indentation) | 743 (back-to-indentation) |
613 (if (eq (char-syntax (char-after)) ?\() | 744 (if (eq (char-syntax (char-after)) ?\() |
614 0 | 745 0 |
615 c-basic-offset))) | 746 c-basic-offset))) |
747 | |
748 (defun c-lineup-cpp-define (langelem) | |
749 "Line up macro continuation lines according to the indentation of | |
750 the construct preceding the macro. E.g: | |
751 | |
752 v beg of preceding constr v beg of preceding constr | |
753 int dribble() { | |
754 const char msg[] = if (!running) | |
755 \"Some text.\"; error(\"Not running!\"); | |
756 | |
757 #define X(A, B) \ #define X(A, B) \ | |
758 do { \ <-> do { \ <- c-lineup-cpp-define | |
759 printf (A, B); \ printf (A, B); \ | |
760 } while (0) } while (0) | |
761 | |
762 If `c-syntactic-indentation-in-macros' is non-nil, the function | |
763 returns the relative indentation to the macro start line to allow | |
764 accumulation with other offsets. E.g. in the following cases, | |
765 cpp-define-intro is combined with the statement-block-intro that comes | |
766 from the \"do {\" that hangs on the \"#define\" line: | |
767 | |
768 int dribble() { | |
769 const char msg[] = if (!running) | |
770 \"Some text.\"; error(\"Not running!\"); | |
771 | |
772 #define X(A, B) do { \ #define X(A, B) do { \ | |
773 printf (A, B); \ <-> printf (A, B); \ <- c-lineup-cpp-define | |
774 this->refs++; \ this->refs++; \ | |
775 } while (0) <-> } while (0) <- c-lineup-cpp-define | |
776 | |
777 The relative indentation returned by `c-lineup-cpp-define' is zero and | |
778 two, respectively, in these two examples. They are then added to the | |
779 two column indentation that statement-block-intro gives in both cases | |
780 here. | |
781 | |
782 If the relative indentation is zero, then nil is returned instead. | |
783 This useful in a list expression to specify the default indentation on | |
784 the top level. | |
785 | |
786 If `c-syntactic-indentation-in-macros' is nil then this function keeps | |
787 the current indentation, except for empty lines \(ignoring the ending | |
788 backslash) where it takes the indentation from the closest preceding | |
789 nonempty line in the macro. If there's no such line in the macro then | |
790 the indentation is taken from the construct preceding it, as described | |
791 above. | |
792 | |
793 Works with: cpp-define-intro." | |
794 (let (offset) | |
795 (if c-syntactic-indentation-in-macros | |
796 ;; Go to the macro start and do a syntactic analysis of it. | |
797 ;; Then remove the cpp-macro element it should contain and | |
798 ;; calculate the indentation it then would get. | |
799 (save-excursion | |
800 (c-beginning-of-macro) | |
801 (setq offset (- (c-get-syntactic-indentation | |
802 (delete '(cpp-macro) (c-guess-basic-syntax))) | |
803 (save-excursion | |
804 (back-to-indentation) | |
805 (current-column)))) | |
806 (if (zerop offset) | |
807 nil | |
808 offset)) | |
809 ;; Do not indent syntactically inside the macro. | |
810 (save-excursion | |
811 (let ((macro-start-line (save-excursion | |
812 (goto-char (c-query-macro-start)) | |
813 (beginning-of-line) | |
814 (point)))) | |
815 (beginning-of-line) | |
816 ;; Check every line while inside the macro. | |
817 (while (and (> (point) macro-start-line) | |
818 (looking-at "[ \t]*\\\\?$") | |
819 (= (forward-line -1) 0))) | |
820 (if (<= (point) macro-start-line) | |
821 ;; If we've stepped out of the macro we take the | |
822 ;; syntactic offset. | |
823 (setq offset (c-get-syntactic-indentation | |
824 (delete '(cpp-macro) (c-guess-basic-syntax)))) | |
825 (setq offset (current-indentation))) | |
826 (if (zerop offset) | |
827 nil | |
828 (vector offset))))))) | |
829 | |
830 ;; Contributed by Kevin Ryde <user42@zip.com.au>. | |
831 (defun c-lineup-gcc-asm-reg (elem) | |
832 "Line up a gcc asm register under one on a previous line. | |
833 | |
834 asm (\"foo %1, %0\\n\" | |
835 \"bar %0, %1\" | |
836 : \"=r\" (w), | |
837 \"=r\" (x) | |
838 : \"0\" (y), | |
839 \"1\" (z)); | |
840 | |
841 The \"x\" line is aligned to the text after the \":\" on the \"w\" line, and | |
842 similarly \"z\" under \"y\". | |
843 | |
844 This is done only in an \"asm\" or \"__asm__\" block, and only to those | |
845 lines mentioned. Anywhere else `nil' is returned. The usual arrangement is | |
846 to have this routine as an extra feature at the start of arglist lineups, e.g. | |
847 | |
848 (c-lineup-gcc-asm-reg c-lineup-arglist) | |
849 | |
850 Works with: arglist-cont, arglist-cont-nonempty." | |
851 | |
852 (let ((orig-pos (point)) | |
853 alignto) | |
854 (save-excursion | |
855 (and | |
856 c-opt-asm-stmt-key | |
857 | |
858 ;; Find the ":" to align to. Look for this first so as to quickly | |
859 ;; eliminate pretty much all cases which are not for us. | |
860 (re-search-backward "^[ \t]*:[ \t]*\\(.\\)?" (cdr elem) t) | |
861 | |
862 ;; Must have something after the ":". | |
863 (setq alignto (match-beginning 1)) | |
864 | |
865 ;; Don't touch ":" lines themselves. | |
866 (progn (goto-char orig-pos) | |
867 (beginning-of-line) | |
868 (not (looking-at "^[ \t]*:"))) | |
869 | |
870 ;; Only operate in an asm statement. | |
871 (progn (goto-char orig-pos) | |
872 (c-in-gcc-asm-p)) | |
873 | |
874 (vector (progn (goto-char alignto) (current-column))))))) | |
616 | 875 |
617 (defun c-lineup-dont-change (langelem) | 876 (defun c-lineup-dont-change (langelem) |
618 "Do not change the indentation of the current line. | 877 "Do not change the indentation of the current line. |
619 | 878 |
620 Works with: Any syntactic symbol." | 879 Works with: Any syntactic symbol." |
661 ((memq langelem non-top-levels) | 920 ((memq langelem non-top-levels) |
662 (save-excursion | 921 (save-excursion |
663 (setq syntax nil) | 922 (setq syntax nil) |
664 (back-to-indentation) | 923 (back-to-indentation) |
665 (if (zerop (current-column)) | 924 (if (zerop (current-column)) |
666 (insert (make-string c-label-minimum-indentation 32))) | 925 (insert-char ?\ c-label-minimum-indentation t)) |
667 )) | 926 )) |
668 )))) | 927 )))) |
669 | 928 |
670 | 929 |
671 ;; Useful for c-hanging-semi&comma-criteria | 930 ;; Useful for c-hanging-semi&comma-criteria |