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