1 ;;; cc-align.el --- custom indentation functions for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2015 Free Software Foundation, Inc.
5 ;; Authors: 2004- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
)
41 (load "cc-bytecomp" nil t
)))
45 (cc-require 'cc-engine
)
48 ;; Standard line-up functions
50 ;; See the section "Custom Indentation Functions" in the manual for
51 ;; details on the calling convention.
53 (defun c-lineup-topmost-intro-cont (langelem)
54 "Line up declaration continuation lines zero or one indentation step.
55 For lines in the \"header\" of a definition, zero is used. For other
56 lines, `c-basic-offset' is added to the indentation. E.g.:
59 neg (int i) <- c-lineup-topmost-intro-cont
65 larch <- c-lineup-topmost-intro-cont
69 the_larch, <- c-lineup-topmost-intro-cont
70 another_larch; <- c-lineup-topmost-intro-cont
74 the_larch, <- c-lineup-topmost-intro-cont
75 another_larch; <- c-lineup-topmost-intro-cont
77 \(This function is mainly provided to mimic the behavior of CC Mode
78 5.28 and earlier where this case wasn't handled consistently so that
79 these lines could be analyzed as either topmost-intro-cont or
82 Works with: topmost-intro-cont."
85 (c-backward-syntactic-ws (c-langelem-pos langelem
))
86 (if (and (memq (char-before) '(?
} ?
,))
87 (not (and c-overloadable-operators-regexp
88 (c-after-special-operator-id))))
91 (defun c-lineup-gnu-DEFUN-intro-cont (langelem)
92 "Line up the continuation lines of a DEFUN macro in the Emacs C source.
93 These lines are indented as though they were `knr-argdecl-intro' lines.
94 Return nil when we're not in such a construct.
96 This function is for historical compatibility with how previous CC Modes (5.28
97 and earlier) indented such lines.
101 DEFUN (\"forward-char\", Fforward_char, Sforward_char, 0, 1, \"p\",
102 doc: /* Move point right N characters (left if N is negative).
103 On reaching end of buffer, stop and signal error. */)
104 (n) <- c-lineup-gnu-DEFUN-into-cont
105 Lisp_Object n; <- c-lineup-gnu-DEFUN-into-cont
107 Works with: topmost-intro-cont."
109 (let (case-fold-search)
110 (goto-char (c-langelem-pos langelem
))
111 (if (looking-at "\\<DEFUN\\>")
112 (c-calc-offset '(knr-argdecl-intro))))))
114 (defun c-block-in-arglist-dwim (arglist-start)
115 ;; This function implements the DWIM to avoid far indentation of
116 ;; brace block constructs in arguments in `c-lineup-arglist' etc.
117 ;; Return non-nil if a brace block construct is detected within the
118 ;; arglist starting at ARGLIST-START.
121 ;; Check if the syntactic context contains any of the symbols for
122 ;; in-expression constructs. This can both save the work that we
123 ;; have to do below, and it also detect the brace list constructs
124 ;; that `c-looking-at-inexpr-block' currently misses (they are
125 ;; recognized by `c-inside-bracelist-p' instead).
126 (assq 'inexpr-class c-syntactic-context
)
127 (assq 'inexpr-statement c-syntactic-context
)
128 (assq 'inlambda c-syntactic-context
)
131 ;; Search for open braces from the arglist start to the end of the
133 (narrow-to-region arglist-start
(c-point 'eol arglist-start
))
135 (goto-char arglist-start
)
136 (while (and (c-syntactic-re-search-forward "{" nil t
)
140 ;; Ignore starts of special brace lists.
141 (and c-special-brace-lists
144 (c-looking-at-special-brace-list)))
145 ;; Ignore complete blocks.
146 (c-safe (c-forward-sexp) t
))))
151 (let (containing-sexp)
152 (goto-char arglist-start
)
153 ;; `c-syntactic-eol' always matches somewhere on the line.
154 (re-search-forward c-syntactic-eol
)
155 (goto-char (match-beginning 0))
156 (c-forward-syntactic-ws)
157 (setq containing-sexp
(c-most-enclosing-brace (c-parse-state)))
158 (c-looking-at-inexpr-block
159 (c-safe-position (or containing-sexp
(point)) c-state-cache
)
162 (defun c-lineup-arglist (langelem)
163 "Line up the current argument line under the first argument.
165 As a special case, if the indented line is inside a brace block
166 construct, the indentation is `c-basic-offset' only. This is intended
167 as a \"DWIM\" measure in cases like macros that contains statement
170 A_VERY_LONG_MACRO_NAME ({
171 some (code, with + long, lines * in[it]);
175 This is motivated partly because it's more in line with how code
176 blocks are handled, and partly since it approximates the behavior of
177 earlier CC Mode versions, which due to inaccurate analysis tended to
178 indent such cases this way.
180 Works with: arglist-cont-nonempty, arglist-close."
182 (let ((indent-pos (point)))
184 (if (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element
))
185 c-basic-offset
; DWIM case.
187 ;; Normal case. Indent to the token after the arglist open paren.
188 (goto-char (c-langelem-2nd-pos c-syntactic-element
))
189 (if (and c-special-brace-lists
190 (c-looking-at-special-brace-list))
191 ;; Skip a special brace list opener like "({".
192 (progn (c-forward-token-2)
195 (let ((arglist-content-start (point)))
196 (c-forward-syntactic-ws)
197 (when (< (point) indent-pos
)
198 (goto-char arglist-content-start
)
199 (skip-chars-forward " \t"))
200 (vector (current-column)))))))
202 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
203 (defun c-lineup-argcont (elem)
204 "Line up a continued argument.
206 foo (xyz, aaa + bbb + ccc
207 + ddd + eee + fff); <- c-lineup-argcont
209 Only continuation lines like this are touched, nil is returned on lines
210 which are the start of an argument.
212 Within a gcc asm block, \":\" is recognized as an argument separator,
213 but of course only between operand specifications, not in the expressions
216 Works with: arglist-cont, arglist-cont-nonempty."
221 (when (eq (car elem
) 'arglist-cont-nonempty
)
222 ;; Our argument list might not be the innermost one. If it
223 ;; isn't, go back to the last position in it. We do this by
224 ;; stepping back over open parens until we get to the open paren
225 ;; of our argument list.
226 (let ((open-paren (c-langelem-2nd-pos c-syntactic-element
))
227 (paren-state (c-parse-state)))
228 (while (not (eq (car paren-state
) open-paren
))
229 (unless (consp (car paren-state
)) ;; ignore matched braces
230 (goto-char (car paren-state
)))
231 (setq paren-state
(cdr paren-state
)))))
233 (let ((start (point)) c
)
236 ;; Previous line ending in a comma means we're the start of an
237 ;; argument. This should quickly catch most cases not for us.
238 ;; This case is only applicable if we're the innermost arglist.
239 (c-backward-syntactic-ws)
240 (setq c
(char-before)))
243 ;; In a gcc asm, ":" on the previous line means the start of an
244 ;; argument. And lines starting with ":" are not for us, don't
245 ;; want them to indent to the preceding operand.
246 (let ((gcc-asm (save-excursion
253 (looking-at "[ \t]*:"))))
255 (c-lineup-argcont-scan (if gcc-asm ?
:))
256 (vector (current-column))))))))
258 (defun c-lineup-argcont-scan (&optional other-match
)
259 ;; Find the start of an argument, for `c-lineup-argcont'.
260 (when (zerop (c-backward-token-2 1 t
))
261 (let ((c (char-after)))
262 (if (or (eq c ?
,) (eq c other-match
))
265 (c-forward-syntactic-ws))
266 (c-lineup-argcont-scan other-match
)))))
268 (defun c-lineup-arglist-intro-after-paren (langelem)
269 "Line up a line to just after the open paren of the surrounding paren
272 Works with: defun-block-intro, brace-list-intro,
273 statement-block-intro, statement-case-intro, arglist-intro."
277 (skip-chars-forward " \t" (c-point 'eol
))
278 (vector (1+ (current-column)))))
280 (defun c-lineup-arglist-close-under-paren (langelem)
281 "Line up a line under the enclosing open paren.
282 Normally used to line up a closing paren in the same column as its
283 corresponding open paren, but can also be used with arglist-cont and
284 arglist-cont-nonempty to line up all lines inside a parenthesis under
287 As a special case, if a brace block construct starts at the same line
288 as the open parenthesis of the argument list, the indentation is
289 `c-basic-offset' only. See `c-lineup-arglist' for further discussion
290 of this \"DWIM\" measure.
292 Works with: Almost all symbols, but are typically most useful on
293 arglist-close, brace-list-close, arglist-cont and arglist-cont-nonempty."
295 (if (memq (c-langelem-sym langelem
)
296 '(arglist-cont-nonempty arglist-close
))
297 (goto-char (c-langelem-2nd-pos c-syntactic-element
))
299 (c-go-up-list-backward))
301 (if (save-excursion (c-block-in-arglist-dwim (point)))
302 c-basic-offset
; DWIM case.
304 ;; Normal case. Indent to the arglist open paren.
306 (if (and c-special-brace-lists
307 (setq special-list
(c-looking-at-special-brace-list)))
308 ;; Cope if we're in the middle of a special brace list
310 (goto-char (car (car special-list
))))
311 (vector (current-column))))))
313 (defun c-lineup-arglist-operators (langelem)
314 "Line up lines starting with an infix operator under the open paren.
315 Return nil on lines that don't start with an operator, to leave those
316 cases to other line-up functions. Example:
319 || at_limit (x, <- c-lineup-arglist-operators
320 list) <- c-lineup-arglist-operators returns nil
323 Since this function doesn't do anything for lines without an infix
324 operator you typically want to use it together with some other line-up
325 settings, e.g. as follows \(the arglist-close setting is just a
326 suggestion to get a consistent style):
328 \(c-set-offset 'arglist-cont '(c-lineup-arglist-operators 0))
329 \(c-set-offset 'arglist-cont-nonempty '(c-lineup-arglist-operators
331 \(c-set-offset 'arglist-close '(c-lineup-arglist-close-under-paren))
333 Works with: arglist-cont, arglist-cont-nonempty."
335 (back-to-indentation)
336 (when (looking-at "[-+|&*%<>=]\\|\\(/[^/*]\\)")
337 ;; '-' can be both an infix and a prefix operator, but I'm lazy now..
338 (c-lineup-arglist-close-under-paren langelem
))))
340 (defun c-lineup-close-paren (langelem)
341 "Line up the closing paren under its corresponding open paren if the
342 open paren is followed by code. If the open paren ends its line, no
343 indentation is added. E.g.:
347 ) <-> ) <- c-lineup-close-paren
349 As a special case, if a brace block construct starts at the same line
350 as the open parenthesis of the argument list, the indentation is
351 `c-basic-offset' instead of the open paren column. See
352 `c-lineup-arglist' for further discussion of this \"DWIM\" measure.
354 Works with: All *-close symbols."
356 (if (memq (c-langelem-sym langelem
)
357 '(arglist-cont-nonempty arglist-close
))
358 (goto-char (c-langelem-2nd-pos c-syntactic-element
))
360 (c-go-up-list-backward))
362 (let (special-list arglist-start
)
363 (if (and c-special-brace-lists
364 (setq special-list
(c-looking-at-special-brace-list)))
365 ;; Cope if we're in the middle of a special brace list
368 (goto-char (setq arglist-start
(car (car special-list
))))
371 (setq arglist-start
(point))
374 (cond ((looking-at c-syntactic-eol
)
375 0) ; The arglist is "empty".
377 ((c-block-in-arglist-dwim (point))
378 c-basic-offset
) ; DWIM case.
381 ;; Normal case. Indent to the arglist open paren.
382 (goto-char arglist-start
)
383 (vector (current-column)))))))
385 (defun c-lineup-streamop (langelem)
386 "Line up C++ stream operators under each other.
388 Works with: stream-op."
390 (goto-char (c-langelem-pos langelem
))
391 (re-search-forward "<<\\|>>" (c-point 'eol
) 'move
)
392 (goto-char (match-beginning 0))
393 (vector (current-column))))
395 (defun c-lineup-multi-inher (langelem)
396 "Line up the classes in C++ multiple inheritance clauses and member
397 initializers under each other. E.g.:
399 class Foo: Foo::Foo (int a, int b):
400 public Cyphr, Cyphr (a),
401 public Bar <-> Bar (b) <- c-lineup-multi-inher
403 class Foo Foo::Foo (int a, int b)
404 : public Cyphr, : Cyphr (a),
405 public Bar <-> Bar (b) <- c-lineup-multi-inher
407 class Foo Foo::Foo (int a, int b)
408 : public Cyphr : Cyphr (a)
409 , public Bar <-> , Bar (b) <- c-lineup-multi-inher
411 Works with: inher-cont, member-init-cont."
413 (back-to-indentation)
414 (let* ((eol (c-point 'eol
))
416 (char-after-ip (char-after)))
417 (if (c-langelem-pos langelem
)
418 (goto-char (c-langelem-pos langelem
)))
420 ;; This kludge is necessary to support both inher-cont and
421 ;; member-init-cont, since they have different anchor positions.
422 (c-backward-syntactic-ws)
423 (when (eq (char-before) ?
:)
425 (c-backward-syntactic-ws))
427 (c-syntactic-re-search-forward ":" eol
'move
)
428 (if (looking-at c-syntactic-eol
)
429 (c-forward-syntactic-ws here
)
430 (if (eq char-after-ip ?
,)
432 (skip-chars-forward " \t" eol
)))
434 (vector (current-column)))
437 (defun c-lineup-java-inher (langelem)
438 "Line up Java implements and extends declarations.
439 If class names follow on the same line as the implements/extends
440 keyword, they are lined up under each other. Otherwise, they are
441 indented by adding `c-basic-offset' to the column of the keyword.
445 extends extends Cyphr,
446 Bar <-> Bar <- c-lineup-java-inher
449 Works with: inher-cont."
451 (goto-char (c-langelem-pos langelem
))
453 (if (looking-at "[ \t]*$")
455 (c-forward-syntactic-ws)
456 (vector (current-column)))))
458 (defun c-lineup-java-throws (langelem)
459 "Line up Java throws declarations.
460 If exception names follow on the same line as the throws keyword,
461 they are lined up under each other. Otherwise, they are indented by
462 adding `c-basic-offset' to the column of the throws keyword. The
463 throws keyword itself is also indented by `c-basic-offset' from the
464 function declaration start if it doesn't hang. E.g.:
466 int foo() int foo() throws Cyphr,
467 throws <-> Bar, <- c-lineup-java-throws
468 Bar <-> Vlod <- c-lineup-java-throws
469 <--><--> c-basic-offset
471 Works with: func-decl-cont."
473 (let* ((lim (1- (c-point 'bol
)))
475 (goto-char (c-langelem-pos langelem
))
476 (while (zerop (c-forward-token-2 1 t lim
))
477 (if (looking-at "throws\\>[^_]")
480 (if (zerop (c-forward-token-2 1 nil
(c-point 'eol
)))
481 (vector (current-column))
482 (back-to-indentation)
483 (vector (+ (current-column) c-basic-offset
)))
486 (defun c-indent-one-line-block (langelem)
487 "Indent a one line block `c-basic-offset' extra.
490 if (n > 0) if (n > 0)
491 {m+=n; n=0;} <-> { <- c-indent-one-line-block
492 <--> c-basic-offset m+=n; n=0;
495 The block may use any kind of parenthesis character. nil is returned
496 if the line doesn't start with a one line block, which makes the
497 function usable in list expressions.
499 Work with: Almost all syntactic symbols, but most useful on *-open."
501 (let ((eol (c-point 'eol
)))
502 (back-to-indentation)
503 (if (and (eq (char-syntax (char-after)) ?\
()
504 (c-safe (progn (c-forward-sexp) t
))
509 (defun c-indent-multi-line-block (langelem)
510 "Indent a multi line block `c-basic-offset' extra.
513 int *foo[] = { int *foo[] = {
515 {17}, <-> { <- c-indent-multi-line-block
520 The block may use any kind of parenthesis character. nil is returned
521 if the line doesn't start with a multi line block, which makes the
522 function usable in list expressions.
524 Work with: Almost all syntactic symbols, but most useful on *-open."
526 (let ((eol (c-point 'eol
)))
527 (back-to-indentation)
528 (if (and (eq (char-syntax (char-after)) ?\
()
529 (or (not (c-safe (progn (c-forward-sexp) t
)))
534 (defun c-lineup-C-comments (langelem)
535 "Line up C block comment continuation lines.
536 Various heuristics are used to handle many of the common comment
537 styles. Some examples:
539 /* /** /* /* text /* /**
540 * text * text text text ** text ** text
543 /*********************************************************************
545 ********************************************************************/
547 /*********************************************************************
548 Free form text comments:
549 In comments with a long delimiter line at the start, the indentation
550 is kept unchanged for lines that start with an empty comment line
551 prefix. The delimiter line is whatever matches the
552 `comment-start-skip' regexp.
553 *********************************************************************/
555 The variable `c-comment-prefix-regexp' is used to recognize the
556 comment line prefix, e.g. the `*' that usually starts every line
559 Works with: The `c' syntactic symbol."
561 (let* ((here (point))
562 (prefixlen (progn (back-to-indentation)
563 (if (looking-at c-current-comment-prefix
)
564 (- (match-end 0) (point))
567 ;; Get the length of the comment starter, not including
568 ;; the first '/'. We check if the comment prefix matched
569 ;; on the current line matches the starter or if it
570 ;; matches comment-start-skip, and choose whichever is
573 (goto-char (1+ (c-langelem-pos langelem
)))
574 (if (and (match-string 0)
575 (looking-at (regexp-quote (match-string 0))))
576 (- (match-end 0) (match-beginning 0))
579 (goto-char (c-langelem-pos langelem
))
580 (looking-at comment-start-skip
)
583 (goto-char (match-end 0))
584 (skip-chars-backward " \t")
588 (if (and (> starterlen
10) (zerop prefixlen
))
589 ;; The comment has a long starter and the line doesn't have
590 ;; a nonempty comment prefix. Treat it as free form text
591 ;; and don't change the indentation.
592 (vector (current-column))
593 ;; Go back to the previous non-blank line, if any.
597 (back-to-indentation)
598 (and (> (point) (c-langelem-pos langelem
))
599 (looking-at "[ \t]*$"))))
600 ;; Is the starting line the first continuation line with content?
601 (if (>= (c-langelem-pos langelem
) (point))
602 (if (zerop prefixlen
)
603 ;; No nonempty comment prefix. Align after comment
606 (looking-at comment-start-skip
)
607 (goto-char (match-end 0))
608 ;; The following should not be necessary, since
609 ;; comment-start-skip should match everything (i.e.
610 ;; typically whitespace) that leads up to the text.
611 ;;(if (looking-at "\\([ \t]+\\).+$")
612 ;; ;; Align with the text that hangs after the
613 ;; ;; comment starter.
614 ;; (goto-char (match-end 1)))
615 (vector (current-column)))
616 ;; How long is the comment starter? if greater than the
617 ;; length of the comment prefix, align left. if less
618 ;; than or equal, align right. this should also pick up
619 ;; Javadoc style comments.
620 (if (> starterlen prefixlen
)
622 (goto-char (c-langelem-pos langelem
))
623 (vector (1+ (current-column))))
624 (goto-char (+ (c-langelem-pos langelem
) starterlen
1))
625 (vector (- (current-column) prefixlen
))))
626 ;; We didn't start on the first non-blank continuation line. If the
627 ;; previous line has a nonempty comment prefix, align with it.
628 ;; Otherwise, align with the previous nonempty line, but align the
629 ;; comment ender with the starter.
630 (when (or (not (looking-at c-current-comment-prefix
))
631 (eq (match-beginning 0) (match-end 0)))
633 (back-to-indentation)
634 (if (looking-at (concat "\\(" c-current-comment-prefix
"\\)\\*/"))
635 (goto-char (c-langelem-pos langelem
))
636 (while (and (zerop (forward-line -
1))
637 (looking-at "^[ \t]*$")))
638 (back-to-indentation)
639 (if (< (point) (c-langelem-pos langelem
))
640 ;; Align with the comment starter rather than
641 ;; with the code before it.
642 (goto-char (c-langelem-pos langelem
)))))
643 (vector (current-column)))))))
645 (defun c-lineup-comment (langelem)
646 "Line up a comment start according to `c-comment-only-line-offset'.
647 If the comment is lined up with a comment starter on the previous
648 line, that alignment is preserved.
650 Works with: comment-intro."
652 (back-to-indentation)
653 (let ((col (current-column)))
655 ;; CASE 1: preserve aligned comments
657 (and (c-backward-single-comment)
658 (= col
(current-column))))
659 (vector col
)) ; Return an absolute column.
660 ;; indent as specified by c-comment-only-line-offset
662 (or (car-safe c-comment-only-line-offset
)
663 c-comment-only-line-offset
))
665 (or (cdr-safe c-comment-only-line-offset
)
666 (car-safe c-comment-only-line-offset
)
667 -
1000)) ;jam it against the left side
670 (defun c-lineup-knr-region-comment (langelem)
671 "Line up a comment in the \"K&R region\" with the declaration.
672 That is the region between the function or class header and the
673 beginning of the block. E.g.:
676 /* This is the main function. */ <- c-lineup-knr-region-comment
681 Return nil if called in any other situation, to be useful in list
684 Works with: comment-intro."
685 (when (or (assq 'topmost-intro-cont c-syntactic-context
)
686 (assq 'func-decl-cont c-syntactic-context
)
687 (assq 'knr-argdecl-intro c-syntactic-context
)
688 (assq 'lambda-intro-cont c-syntactic-context
))
691 (c-beginning-of-statement-1)
692 (vector (current-column)))))
694 (defun c-lineup-runin-statements (langelem)
695 "Line up statements when the first statement is on the same line as
696 the block opening brace. E.g.:
699 { puts (\"Hello world!\");
700 return 0; <- c-lineup-runin-statements
703 If there is no statement after the opening brace to align with, nil is
704 returned. This makes the function usable in list expressions.
706 Works with: The `statement' syntactic symbol."
707 (if (eq (char-after (c-langelem-pos langelem
)) ?
{)
709 (if (c-langelem-pos langelem
)
710 (goto-char (c-langelem-pos langelem
)))
712 (skip-chars-forward " \t")
714 (vector (current-column))))))
716 (defun c-lineup-assignments (langelem)
717 "Line up the current line after the assignment operator on the first
718 line in the statement. If there isn't any, return nil to allow
719 stacking with other line-up functions. If the current line contains
720 an assignment operator too, try to align it with the first one.
722 Works with: topmost-intro-cont, statement-cont, arglist-cont,
723 arglist-cont-nonempty."
724 (let (startpos endpos equalp
)
726 (if (eq (c-langelem-sym langelem
) 'arglist-cont-nonempty
)
727 ;; If it's an arglist-cont-nonempty then we're only interested
728 ;; in equal signs outside it. We don't search for a "=" on
729 ;; the current line since that'd have a different nesting
730 ;; compared to the one we should align with.
733 (setq endpos
(c-langelem-2nd-pos c-syntactic-element
))
734 (narrow-to-region (c-langelem-pos langelem
) endpos
)
735 (if (setq startpos
(c-up-list-backward endpos
))
736 (setq startpos
(1+ startpos
))
737 (setq startpos
(c-langelem-pos langelem
)))))
739 (setq startpos
(c-langelem-pos langelem
)
740 endpos
(c-point 'bol
))
742 ;; Find a syntactically relevant and unnested "=" token on the
743 ;; current line. equalp is in that case set to the number of
744 ;; columns to left shift the current line to align it with the
748 (when (c-syntactic-re-search-forward
749 c-assignment-op-regexp
750 (c-point 'eol
) t t t
)
751 (setq equalp
(- (or (match-beginning 1)
757 (if (or (if (c-syntactic-re-search-forward
758 c-assignment-op-regexp
759 (min endpos
(c-point 'eol
)) t t t
)
761 (goto-char (or (match-beginning 1)
766 (c-forward-syntactic-ws (c-point 'eol
))
768 ;; There's no equal sign on the line, or there is one but
769 ;; nothing follows it.
772 ;; calculate indentation column after equals and ws, unless
773 ;; our line contains an equals sign
776 (skip-chars-forward " \t")
779 (vector (- (current-column) equalp
)))
782 (defun c-lineup-math (langelem)
783 "Like `c-lineup-assignments' but indent with `c-basic-offset' if no
784 assignment operator was found on the first line. I.e. this function
785 is the same as specifying a list (c-lineup-assignments +). It's
786 provided for compatibility with old configurations.
788 Works with: topmost-intro-cont, statement-cont, arglist-cont,
789 arglist-cont-nonempty."
790 (or (c-lineup-assignments langelem
)
793 (defun c-lineup-cascaded-calls (langelem)
794 "Line up \"cascaded calls\" under each other.
795 If the line begins with \"->\" or \".\" and the preceding line ends
796 with one or more function calls preceded by the same token, then the
797 arrow is lined up with the first of those tokens. E.g.:
799 result = proc->add(17)->add(18)
800 ->add(19) + <- c-lineup-cascaded-calls
801 offset; <- c-lineup-cascaded-calls (inactive)
803 In any other situation nil is returned to allow use in list
806 Works with: topmost-intro-cont, statement-cont, arglist-cont,
807 arglist-cont-nonempty."
809 (if (and (eq (c-langelem-sym langelem
) 'arglist-cont-nonempty
)
810 (not (eq (c-langelem-2nd-pos c-syntactic-element
)
811 (c-most-enclosing-brace (c-parse-state)))))
812 ;; The innermost open paren is not our one, so don't do
813 ;; anything. This can occur for arglist-cont-nonempty with
814 ;; nested arglist starts on the same line.
818 (back-to-indentation)
819 (let ((operator (and (looking-at "->\\|\\.")
820 (regexp-quote (match-string 0))))
821 (stmt-start (c-langelem-pos langelem
)) col
)
824 (looking-at operator
)
825 (zerop (c-backward-token-2 1 t stmt-start
))
826 (eq (char-after) ?\
()
827 (zerop (c-backward-token-2 2 t stmt-start
))
828 (looking-at operator
))
829 (setq col
(current-column))
831 (while (and (zerop (c-backward-token-2 1 t stmt-start
))
832 (eq (char-after) ?\
()
833 (zerop (c-backward-token-2 2 t stmt-start
))
834 (looking-at operator
))
835 (setq col
(current-column)))
839 (defun c-lineup-string-cont (langelem)
840 "Line up a continued string under the one it continues.
841 A continued string in this sense is where a string literal follows
842 directly after another one. E.g.:
844 result = prefix + \"A message \"
845 \"string.\"; <- c-lineup-string-cont
847 In other situations, returns nil, to allow stacking with other
850 Works with: topmost-intro-cont, statement-cont, arglist-cont,
851 arglist-cont-nonempty."
853 (back-to-indentation)
854 (and (looking-at "\\s\"")
855 (let ((quote (char-after)) pos
)
856 (while (and (progn (c-backward-syntactic-ws)
857 (eq (char-before) quote
))
858 (c-safe (c-backward-sexp) t
)
859 (/= (setq pos
(point)) (c-point 'boi
))))
862 (vector (current-column)))))))
864 (defun c-lineup-template-args (langelem)
865 "Line up template argument lines under the first argument.
866 To allow this function to be used in a list expression, nil is
867 returned if there's no template argument on the first line.
869 Works with: template-args-cont."
871 (c-with-syntax-table c
++-template-syntax-table
874 (if (and (eq (char-after) ?
<)
875 (zerop (c-forward-token-2 1 nil
(c-point 'eol
))))
876 (vector (current-column))))))
878 (defun c-lineup-ObjC-method-call (langelem)
879 "Line up selector args as Emacs Lisp mode does with function args:
880 Go to the position right after the message receiver, and if you are at
881 the end of the line, indent the current line c-basic-offset columns
882 from the opening bracket; otherwise you are looking at the first
883 character of the first method call argument, so line up the current
886 Works with: objc-method-call-cont."
888 (let* ((extra (save-excursion
889 (back-to-indentation)
890 (c-backward-syntactic-ws (c-langelem-pos langelem
))
891 (if (eq (char-before) ?
:)
894 (open-bracket-pos (c-langelem-pos langelem
))
895 (open-bracket-col (progn
896 (goto-char open-bracket-pos
)
901 (skip-chars-forward " \t")
903 (+ open-bracket-col c-basic-offset
)
906 (- target-col open-bracket-col extra
))))
908 (defun c-lineup-ObjC-method-call-colons (langelem)
909 "Line up selector args as Project Builder / XCode: colons of first
910 selector portions on successive lines are aligned. If no decision can
911 be made return NIL, so that other lineup methods can be tried. This is
912 typically chained with `c-lineup-ObjC-method-call'.
914 Works with: objc-method-call-cont."
917 (let* ((method-arg-len (progn
918 (back-to-indentation)
919 (if (search-forward ":" (c-point 'eol
) 'move
)
920 (- (point) (c-point 'boi
))
921 ; no complete argument to indent yet
922 (throw 'no-idea nil
))))
924 (extra (save-excursion
925 ; indent parameter to argument if needed
926 (back-to-indentation)
927 (c-backward-syntactic-ws (c-langelem-pos langelem
))
928 (if (eq ?
: (char-before))
929 c-objc-method-parameter-offset
0)))
931 (open-bracket-col (c-langelem-col langelem
))
933 (arg-ralign-colon-ofs (progn
934 (forward-char) ; skip over '['
935 ; skip over object/class name
938 (if (search-forward ":" (c-point 'eol
) 'move
)
939 (- (current-column) open-bracket-col
940 method-arg-len extra
)
941 ; previous arg has no param
942 c-objc-method-arg-unfinished-offset
))))
944 (if (>= arg-ralign-colon-ofs c-objc-method-arg-min-delta-to-bracket
)
945 (+ arg-ralign-colon-ofs extra
)
946 (throw 'no-idea nil
))))))
948 (defun c-lineup-ObjC-method-args (langelem)
949 "Line up the colons that separate args in a method declaration.
950 The colon on the current line is aligned with the one on the first
953 Works with: objc-method-args-cont."
955 (let* ((here (c-point 'boi
))
956 (curcol (progn (goto-char here
) (current-column)))
958 (relpos (c-langelem-pos langelem
))
959 (first-col-column (progn
961 (skip-chars-forward "^:" eol
)
962 (and (eq (char-after) ?
:)
964 (if (not first-col-column
)
967 (skip-chars-forward "^:" eol
)
968 (if (eq (char-after) ?
:)
969 (+ curcol
(- first-col-column
(current-column)))
972 (defun c-lineup-ObjC-method-args-2 (langelem)
973 "Line up the colons that separate args in a method declaration.
974 The colon on the current line is aligned with the one on the previous
977 Works with: objc-method-args-cont."
979 (let* ((here (c-point 'boi
))
980 (curcol (progn (goto-char here
) (current-column)))
982 (relpos (c-langelem-pos langelem
))
983 (prev-col-column (progn
984 (skip-chars-backward "^:" relpos
)
985 (and (eq (char-before) ?
:)
986 (- (current-column) 1)))))
987 (if (not prev-col-column
)
990 (skip-chars-forward "^:" eol
)
991 (if (eq (char-after) ?
:)
992 (+ curcol
(- prev-col-column
(current-column)))
995 (defun c-lineup-inexpr-block (langelem)
996 "Line up the block for constructs that use a block inside an expression,
997 e.g. anonymous classes in Java and lambda functions in Pike. The body
998 is aligned with the start of the header, e.g. with the \"new\" or
999 \"lambda\" keyword. Returns nil if the block isn't part of such a
1002 Works with: inlambda, inexpr-statement, inexpr-class."
1004 (back-to-indentation)
1005 (let* ((paren-state (c-parse-state))
1006 (containing-sexp (c-most-enclosing-brace paren-state
))
1007 (res (or (c-looking-at-inexpr-block
1008 (c-safe-position containing-sexp paren-state
)
1010 (and containing-sexp
1011 (progn (goto-char containing-sexp
)
1012 (eq (char-after) ?
{))
1013 (progn (setq containing-sexp
1014 (c-most-enclosing-brace paren-state
1016 (c-looking-at-inexpr-block
1017 (c-safe-position containing-sexp paren-state
)
1018 containing-sexp
))))))
1020 (goto-char (cdr res
))
1021 (vector (current-column))))))
1023 (defun c-lineup-whitesmith-in-block (langelem)
1024 "Line up lines inside a block in Whitesmith style.
1025 It's done in a way that works both when the opening brace hangs and
1026 when it doesn't. E.g.:
1030 foo; <-> foo; <- c-lineup-whitesmith-in-block
1034 In the first case the indentation is kept unchanged, in the
1035 second `c-basic-offset' is added.
1037 Works with: defun-close, defun-block-intro, inline-close, block-close,
1038 brace-list-close, brace-list-intro, statement-block-intro,
1039 arglist-intro, arglist-cont-nonempty, arglist-close, and all in*
1040 symbols, e.g. inclass and inextern-lang."
1043 (if (and (c-go-up-list-backward)
1044 (= (point) (c-point 'boi
)))
1048 (defun c-lineup-after-whitesmith-blocks (langelem)
1049 "Compensate for Whitesmith style indentation of blocks.
1050 Due to the way CC Mode calculates anchor positions for normal lines
1051 inside blocks, this function is necessary for those lines to get
1052 correct Whitesmith style indentation. Consider the following
1060 x; <-> x; <- c-lineup-after-whitesmith-blocks
1062 The fact that the line with \"x\" is preceded by a Whitesmith style
1063 indented block in one case and not the other should not affect its
1064 indentation. But since CC Mode in cases like this uses the
1065 indentation of the preceding statement as anchor position, the \"x\"
1066 would in the rightmost case be indented too much if the offset for
1067 `statement' was set simply to zero.
1069 This lineup function corrects for this situation by detecting if the
1070 anchor position is at an open paren character. In that case, it
1071 instead indents relative to the surrounding block just like
1072 `c-lineup-whitesmith-in-block'.
1074 Works with: brace-list-entry, brace-entry-open, statement,
1077 (goto-char (c-langelem-pos langelem
))
1078 (when (looking-at "\\s\(")
1079 (if (c-go-up-list-backward)
1080 (let ((pos (point)))
1081 (back-to-indentation)
1083 (vector (current-column))
1084 (vector (+ (current-column) c-basic-offset
))))
1087 (defun c-lineup-cpp-define (langelem)
1088 "Line up macro continuation lines according to the indentation of
1089 the construct preceding the macro. E.g.:
1091 v beg of preceding constr v beg of preceding constr
1093 const char msg[] = if (!running)
1094 \"Some text.\"; error(\"Not running!\");
1096 #define X(A, B) \ #define X(A, B) \
1097 do { \ <-> do { \ <- c-lineup-cpp-define
1098 printf (A, B); \ printf (A, B); \
1099 } while (0) } while (0)
1101 If `c-syntactic-indentation-in-macros' is non-nil, the function
1102 returns the relative indentation to the macro start line to allow
1103 accumulation with other offsets. E.g. in the following cases,
1104 cpp-define-intro is combined with the statement-block-intro that comes
1105 from the \"do {\" that hangs on the \"#define\" line:
1108 const char msg[] = if (!running)
1109 \"Some text.\"; error(\"Not running!\");
1111 #define X(A, B) do { \ #define X(A, B) do { \
1112 printf (A, B); \ <-> printf (A, B); \ <- c-lineup-cpp-define
1113 this->refs++; \ this->refs++; \
1114 } while (0) <-> } while (0) <- c-lineup-cpp-define
1116 The relative indentation returned by `c-lineup-cpp-define' is zero and
1117 two, respectively, in these two examples. They are then added to the
1118 two column indentation that statement-block-intro gives in both cases
1121 If the relative indentation is zero, then nil is returned instead.
1122 That is useful in a list expression to specify the default indentation
1125 If `c-syntactic-indentation-in-macros' is nil then this function keeps
1126 the current indentation, except for empty lines \(ignoring the ending
1127 backslash) where it takes the indentation from the closest preceding
1128 nonempty line in the macro. If there's no such line in the macro then
1129 the indentation is taken from the construct preceding it, as described
1132 Works with: cpp-define-intro."
1134 (if c-syntactic-indentation-in-macros
1135 ;; Go to the macro start and do a syntactic analysis of it.
1136 ;; Then remove the cpp-macro element it should contain and
1137 ;; calculate the indentation it then would get.
1139 (c-beginning-of-macro)
1140 (setq offset
(- (c-get-syntactic-indentation
1141 (delete '(cpp-macro) (c-guess-basic-syntax)))
1143 (back-to-indentation)
1148 ;; Do not indent syntactically inside the macro.
1150 (let ((macro-start-line (save-excursion
1151 (goto-char (c-query-macro-start))
1155 ;; Check every line while inside the macro.
1156 (while (and (> (point) macro-start-line
)
1157 (looking-at "[ \t]*\\\\?$")
1158 (= (forward-line -
1) 0)))
1159 (if (<= (point) macro-start-line
)
1160 ;; If we've stepped out of the macro we take the
1161 ;; syntactic offset.
1162 (setq offset
(c-get-syntactic-indentation
1163 (delete '(cpp-macro) (c-guess-basic-syntax))))
1164 (setq offset
(current-indentation)))
1167 (vector offset
)))))))
1169 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
1170 (defun c-lineup-gcc-asm-reg (elem)
1171 "Line up a gcc asm register under one on a previous line.
1173 asm (\"foo %1, %0\\n\"
1180 The \"x\" line is aligned to the text after the \":\" on the \"w\" line, and
1181 similarly \"z\" under \"y\".
1183 This is done only in an \"asm\" or \"__asm__\" block, and only to
1184 those lines mentioned. Anywhere else nil is returned. The usual
1185 arrangement is to have this routine as an extra feature at the start
1186 of arglist line-ups, e.g.
1188 (c-lineup-gcc-asm-reg c-lineup-arglist)
1190 Works with: arglist-cont, arglist-cont-nonempty."
1192 (let ((orig-pos (point))
1199 ;; Don't do anything if the innermost open paren isn't our one.
1200 ;; This can occur for arglist-cont-nonempty with nested arglist
1201 ;; starts on the same line.
1202 (or (not (eq (car elem
) 'arglist-cont-nonempty
))
1203 (eq (c-langelem-2nd-pos c-syntactic-element
)
1204 (c-most-enclosing-brace (c-parse-state))))
1206 ;; Find the ":" to align to. Look for this first so as to quickly
1207 ;; eliminate pretty much all cases which are not for us.
1208 (re-search-backward "^[ \t]*:[ \t]*\\(.\\)?" (cdr elem
) t
)
1210 ;; Must have something after the ":".
1211 (setq alignto
(match-beginning 1))
1213 ;; Don't touch ":" lines themselves.
1214 (progn (goto-char orig-pos
)
1216 (not (looking-at "^[ \t]*:")))
1218 ;; Only operate in an asm statement.
1219 (progn (goto-char orig-pos
)
1222 (vector (progn (goto-char alignto
) (current-column)))))))
1224 (defun c-lineup-dont-change (langelem)
1225 "Do not change the indentation of the current line.
1227 Works with: Any syntactic symbol."
1229 (back-to-indentation)
1230 (vector (current-column))))
1232 (defun c-lineup-respect-col-0 (langelem)
1233 "If the current line starts at column 0, return [0]. Otherwise return nil.
1235 This can be used for comments (in conjunction with, say,
1236 `c-lineup-comment'), to keep comments already at column 0
1237 anchored there, but reindent other comments."
1239 (back-to-indentation)
1240 (if (eq (current-column) 0)
1245 (defun c-snug-do-while (syntax pos)
1246 "Dynamically calculate brace hanginess for do-while statements.
1247 Using this function, `while' clauses that end a `do-while' block will
1248 remain on the same line as the brace that closes that block.
1250 See `c-hanging-braces-alist' for how to utilize this function as an
1251 ACTION associated with `block-close' syntax."
1254 (if (and (eq syntax 'block-close)
1255 (setq langelem (assq 'block-close c-syntactic-context))
1256 (progn (goto-char (c-langelem-pos langelem))
1257 (if (eq (char-after) ?{)
1258 (c-safe (c-forward-sexp -1)))
1259 (looking-at "\\<do\\>[^_]")))
1263 (defun c-snug-1line-defun-close (syntax pos)
1264 "Determine the brace hanginess for an AWK defun-close.
1265 If the action/function being closed is a one-liner, keep it so. Otherwise put
1266 the closing brace on its own line."
1269 (if (> (c-point 'bol)
1270 (progn (up-list -1) (point)))
1274 (defun c-gnu-impose-minimum ()
1275 "Imposes a minimum indentation for lines inside code blocks.
1276 The variable `c-label-minimum-indentation' specifies the minimum
1277 indentation amount."
1280 ;; Don't adjust macro or comment-only lines.
1281 (or (assq 'cpp-macro c-syntactic-context)
1282 (assq 'comment-intro c-syntactic-context)))
1283 (c-intersect-lists c-inside-block-syms c-syntactic-context)
1285 (back-to-indentation)
1286 (< (current-column) c-label-minimum-indentation)))
1287 (c-shift-line-indentation (- c-label-minimum-indentation
1288 (current-indentation)))))
1291 ;; Useful for c-hanging-semi&comma-criteria
1293 (defun c-semi&comma-inside-parenlist ()
1294 "Controls newline insertion after semicolons in parenthesis lists.
1295 If a comma was inserted, no determination is made. If a semicolon was
1296 inserted inside a parenthesis list, no newline is added otherwise a
1297 newline is added. In either case, checking is stopped. This supports
1298 exactly the old newline insertion behavior."
1299 ;; newline only after semicolon, but only if that semicolon is not
1300 ;; inside a parenthesis list (e.g. a for loop statement)
1301 (if (not (eq (c-last-command-char) ?\;))
1302 nil ; continue checking
1303 (if (condition-case nil
1306 (not (eq (char-after) ?\()))
1311 ;; Suppresses newlines before non-blank lines
1312 (defun c-semi&comma-no-newlines-before-nonblanks ()
1313 "Controls newline insertion after semicolons.
1314 If a comma was inserted, no determination is made. If a semicolon was
1315 inserted, and the following line is not blank, no newline is inserted.
1316 Otherwise, no determination is made."
1318 (if (and (= (c-last-command-char) ?\;)
1320 ;; (save-excursion (skip-syntax-forward " ") (point))
1321 (zerop (forward-line 1))
1322 (bolp) ; forward-line has funny behavior at eob.
1323 (not (looking-at "^[ \t]*$")))
1327 ;; Suppresses new lines after semicolons in one-liners methods
1328 (defun c-semi&comma-no-newlines-for-oneline-inliners ()
1329 "Controls newline insertion after semicolons for some one-line methods.
1330 If a comma was inserted, no determination is made. Newlines are
1331 suppressed in one-liners, if the line is an in-class inline function.
1332 For other semicolon contexts, no determination is made."
1333 (let ((syntax (c-guess-basic-syntax))
1334 (bol (save-excursion
1335 (if (c-safe (up-list -1) t)
1338 (if (and (eq (c-last-command-char) ?\;)
1339 (eq (car (car syntax)) 'inclass)
1340 (eq (car (car (cdr syntax))) 'topmost-intro)
1341 (= (c-point 'bol) bol))
1346 (cc-provide 'cc-align)
1348 ;;; Local Variables:
1349 ;;; indent-tabs-mode: t
1352 ;;; cc-align.el ends here