1 ;;; cc-align.el --- custom indentation functions for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
7 ;; Authors: 2004- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs and Stewart Clamen
11 ;; 1985 Richard M. Stallman
12 ;; Maintainer: bug-cc-mode@gnu.org
13 ;; Created: 22-Apr-1997 (split from cc-mode.el)
14 ;; Version: See cc-mode.el
15 ;; Keywords: c languages oop
17 ;; This file is part of GNU Emacs.
19 ;; GNU Emacs is free software: you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation, either version 3 of the License, or
22 ;; (at your option) any later version.
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
38 (if (and (boundp 'byte-compile-dest-file
)
39 (stringp byte-compile-dest-file
))
40 (cons (file-name-directory byte-compile-dest-file
) load-path
)
42 (load "cc-bytecomp" nil t
)))
46 (cc-require 'cc-engine
)
49 ;; Standard line-up functions
51 ;; See the section "Custom Indentation Functions" in the manual for
52 ;; details on the calling convention.
54 (defun c-lineup-topmost-intro-cont (langelem)
55 "Line up declaration continuation lines zero or one indentation step.
56 For lines in the \"header\" of a definition, zero is used. For other
57 lines, `c-basic-offset' is added to the indentation. E.g:
60 neg (int i) <- c-lineup-topmost-intro-cont
66 larch <- c-lineup-topmost-intro-cont
70 the_larch, <- c-lineup-topmost-intro-cont
71 another_larch; <- c-lineup-topmost-intro-cont
75 the_larch, <- c-lineup-topmost-intro-cont
76 another_larch; <- c-lineup-topmost-intro-cont
78 \(This function is mainly provided to mimic the behavior of CC Mode
79 5.28 and earlier where this case wasn't handled consistently so that
80 these lines could be analyzed as either topmost-intro-cont or
83 Works with: topmost-intro-cont."
86 (c-backward-syntactic-ws (c-langelem-pos langelem
))
87 (if (and (memq (char-before) '(?
} ?
,))
88 (not (and c-overloadable-operators-regexp
89 (c-after-special-operator-id))))
92 (defun c-lineup-gnu-DEFUN-intro-cont (langelem)
93 "Line up the continuation lines of a DEFUN macro in the Emacs C source.
94 These lines are indented as though they were `knr-argdecl-intro' lines.
95 Return nil when we're not in such a construct.
97 This function is for historical compatibility with how previous CC Modes (5.28
98 and earlier) indented such lines.
102 DEFUN (\"forward-char\", Fforward_char, Sforward_char, 0, 1, \"p\",
103 doc: /* Move point right N characters (left if N is negative).
104 On reaching end of buffer, stop and signal error. */)
105 (n) <- c-lineup-gnu-DEFUN-into-cont
106 Lisp_Object n; <- c-lineup-gnu-DEFUN-into-cont
108 Works with: topmost-intro-cont."
110 (let (case-fold-search)
111 (goto-char (c-langelem-pos langelem
))
112 (if (looking-at "\\<DEFUN\\>")
113 (c-calc-offset '(knr-argdecl-intro))))))
115 (defun c-block-in-arglist-dwim (arglist-start)
116 ;; This function implements the DWIM to avoid far indentation of
117 ;; brace block constructs in arguments in `c-lineup-arglist' etc.
118 ;; Return non-nil if a brace block construct is detected within the
119 ;; arglist starting at ARGLIST-START.
122 ;; Check if the syntactic context contains any of the symbols for
123 ;; in-expression constructs. This can both save the work that we
124 ;; have to do below, and it also detect the brace list constructs
125 ;; that `c-looking-at-inexpr-block' currently misses (they are
126 ;; recognized by `c-inside-bracelist-p' instead).
127 (assq 'inexpr-class c-syntactic-context
)
128 (assq 'inexpr-statement c-syntactic-context
)
129 (assq 'inlambda c-syntactic-context
)
132 ;; Search for open braces from the arglist start to the end of the
134 (narrow-to-region arglist-start
(c-point 'eol arglist-start
))
136 (goto-char arglist-start
)
137 (while (and (c-syntactic-re-search-forward "{" nil t
)
141 ;; Ignore starts of special brace lists.
142 (and c-special-brace-lists
145 (c-looking-at-special-brace-list)))
146 ;; Ignore complete blocks.
147 (c-safe (c-forward-sexp) t
))))
152 (let (containing-sexp)
153 (goto-char arglist-start
)
154 ;; `c-syntactic-eol' always matches somewhere on the line.
155 (re-search-forward c-syntactic-eol
)
156 (goto-char (match-beginning 0))
157 (c-forward-syntactic-ws)
158 (setq containing-sexp
(c-most-enclosing-brace (c-parse-state)))
159 (c-looking-at-inexpr-block
160 (c-safe-position (or containing-sexp
(point)) c-state-cache
)
163 (defun c-lineup-arglist (langelem)
164 "Line up the current argument line under the first argument.
166 As a special case, if the indented line is inside a brace block
167 construct, the indentation is `c-basic-offset' only. This is intended
168 as a \"DWIM\" measure in cases like macros that contains statement
171 A_VERY_LONG_MACRO_NAME ({
172 some (code, with + long, lines * in[it]);
176 This is motivated partly because it's more in line with how code
177 blocks are handled, and partly since it approximates the behavior of
178 earlier CC Mode versions, which due to inaccurate analysis tended to
179 indent such cases this way.
181 Works with: arglist-cont-nonempty, arglist-close."
183 (let ((indent-pos (point)))
185 (if (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element
))
186 c-basic-offset
; DWIM case.
188 ;; Normal case. Indent to the token after the arglist open paren.
189 (goto-char (c-langelem-2nd-pos c-syntactic-element
))
190 (if (and c-special-brace-lists
191 (c-looking-at-special-brace-list))
192 ;; Skip a special brace list opener like "({".
193 (progn (c-forward-token-2)
196 (let ((arglist-content-start (point)))
197 (c-forward-syntactic-ws)
198 (when (< (point) indent-pos
)
199 (goto-char arglist-content-start
)
200 (skip-chars-forward " \t"))
201 (vector (current-column)))))))
203 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
204 (defun c-lineup-argcont (elem)
205 "Line up a continued argument.
207 foo (xyz, aaa + bbb + ccc
208 + ddd + eee + fff); <- c-lineup-argcont
210 Only continuation lines like this are touched, nil is returned on lines
211 which are the start of an argument.
213 Within a gcc asm block, \":\" is recognized as an argument separator,
214 but of course only between operand specifications, not in the expressions
217 Works with: arglist-cont, arglist-cont-nonempty."
222 (when (eq (car elem
) 'arglist-cont-nonempty
)
223 ;; Our argument list might not be the innermost one. If it
224 ;; isn't, go back to the last position in it. We do this by
225 ;; stepping back over open parens until we get to the open paren
226 ;; of our argument list.
227 (let ((open-paren (c-langelem-2nd-pos c-syntactic-element
))
228 (paren-state (c-parse-state)))
229 (while (not (eq (car paren-state
) open-paren
))
230 (unless (consp (car paren-state
)) ;; ignore matched braces
231 (goto-char (car paren-state
)))
232 (setq paren-state
(cdr paren-state
)))))
234 (let ((start (point)) c
)
237 ;; Previous line ending in a comma means we're the start of an
238 ;; argument. This should quickly catch most cases not for us.
239 ;; This case is only applicable if we're the innermost arglist.
240 (c-backward-syntactic-ws)
241 (setq c
(char-before)))
244 ;; In a gcc asm, ":" on the previous line means the start of an
245 ;; argument. And lines starting with ":" are not for us, don't
246 ;; want them to indent to the preceding operand.
247 (let ((gcc-asm (save-excursion
254 (looking-at "[ \t]*:"))))
256 (c-lineup-argcont-scan (if gcc-asm ?
:))
257 (vector (current-column))))))))
259 (defun c-lineup-argcont-scan (&optional other-match
)
260 ;; Find the start of an argument, for `c-lineup-argcont'.
261 (when (zerop (c-backward-token-2 1 t
))
262 (let ((c (char-after)))
263 (if (or (eq c ?
,) (eq c other-match
))
266 (c-forward-syntactic-ws))
267 (c-lineup-argcont-scan other-match
)))))
269 (defun c-lineup-arglist-intro-after-paren (langelem)
270 "Line up a line to just after the open paren of the surrounding paren
273 Works with: defun-block-intro, brace-list-intro,
274 statement-block-intro, statement-case-intro, arglist-intro."
278 (skip-chars-forward " \t" (c-point 'eol
))
279 (vector (1+ (current-column)))))
281 (defun c-lineup-arglist-close-under-paren (langelem)
282 "Line up a line under the enclosing open paren.
283 Normally used to line up a closing paren in the same column as its
284 corresponding open paren, but can also be used with arglist-cont and
285 arglist-cont-nonempty to line up all lines inside a parenthesis under
288 As a special case, if a brace block construct starts at the same line
289 as the open parenthesis of the argument list, the indentation is
290 `c-basic-offset' only. See `c-lineup-arglist' for further discussion
291 of this \"DWIM\" measure.
293 Works with: Almost all symbols, but are typically most useful on
294 arglist-close, brace-list-close, arglist-cont and arglist-cont-nonempty."
296 (if (memq (c-langelem-sym langelem
)
297 '(arglist-cont-nonempty arglist-close
))
298 (goto-char (c-langelem-2nd-pos c-syntactic-element
))
300 (c-go-up-list-backward))
302 (if (save-excursion (c-block-in-arglist-dwim (point)))
303 c-basic-offset
; DWIM case.
305 ;; Normal case. Indent to the arglist open paren.
307 (if (and c-special-brace-lists
308 (setq special-list
(c-looking-at-special-brace-list)))
309 ;; Cope if we're in the middle of a special brace list
311 (goto-char (car (car special-list
))))
312 (vector (current-column))))))
314 (defun c-lineup-arglist-operators (langelem)
315 "Line up lines starting with an infix operator under the open paren.
316 Return nil on lines that don't start with an operator, to leave those
317 cases to other line-up functions. Example:
320 || at_limit (x, <- c-lineup-arglist-operators
321 list) <- c-lineup-arglist-operators returns nil
324 Since this function doesn't do anything for lines without an infix
325 operator you typically want to use it together with some other line-up
326 settings, e.g. as follows \(the arglist-close setting is just a
327 suggestion to get a consistent style):
329 \(c-set-offset 'arglist-cont '(c-lineup-arglist-operators 0))
330 \(c-set-offset 'arglist-cont-nonempty '(c-lineup-arglist-operators
332 \(c-set-offset 'arglist-close '(c-lineup-arglist-close-under-paren))
334 Works with: arglist-cont, arglist-cont-nonempty."
336 (back-to-indentation)
337 (when (looking-at "[-+|&*%<>=]\\|\\(/[^/*]\\)")
338 ;; '-' can be both an infix and a prefix operator, but I'm lazy now..
339 (c-lineup-arglist-close-under-paren langelem
))))
341 (defun c-lineup-close-paren (langelem)
342 "Line up the closing paren under its corresponding open paren if the
343 open paren is followed by code. If the open paren ends its line, no
344 indentation is added. E.g:
348 ) <-> ) <- c-lineup-close-paren
350 As a special case, if a brace block construct starts at the same line
351 as the open parenthesis of the argument list, the indentation is
352 `c-basic-offset' instead of the open paren column. See
353 `c-lineup-arglist' for further discussion of this \"DWIM\" measure.
355 Works with: All *-close symbols."
357 (if (memq (c-langelem-sym langelem
)
358 '(arglist-cont-nonempty arglist-close
))
359 (goto-char (c-langelem-2nd-pos c-syntactic-element
))
361 (c-go-up-list-backward))
363 (let (special-list arglist-start
)
364 (if (and c-special-brace-lists
365 (setq special-list
(c-looking-at-special-brace-list)))
366 ;; Cope if we're in the middle of a special brace list
369 (goto-char (setq arglist-start
(car (car special-list
))))
372 (setq arglist-start
(point))
375 (cond ((looking-at c-syntactic-eol
)
376 0) ; The arglist is "empty".
378 ((c-block-in-arglist-dwim (point))
379 c-basic-offset
) ; DWIM case.
382 ;; Normal case. Indent to the arglist open paren.
383 (goto-char arglist-start
)
384 (vector (current-column)))))))
386 (defun c-lineup-streamop (langelem)
387 "Line up C++ stream operators under each other.
389 Works with: stream-op."
391 (goto-char (c-langelem-pos langelem
))
392 (re-search-forward "<<\\|>>" (c-point 'eol
) 'move
)
393 (goto-char (match-beginning 0))
394 (vector (current-column))))
396 (defun c-lineup-multi-inher (langelem)
397 "Line up the classes in C++ multiple inheritance clauses and member
398 initializers under each other. E.g:
400 class Foo: Foo::Foo (int a, int b):
401 public Cyphr, Cyphr (a),
402 public Bar <-> Bar (b) <- c-lineup-multi-inher
404 class Foo Foo::Foo (int a, int b)
405 : public Cyphr, : Cyphr (a),
406 public Bar <-> Bar (b) <- c-lineup-multi-inher
408 class Foo Foo::Foo (int a, int b)
409 : public Cyphr : Cyphr (a)
410 , public Bar <-> , Bar (b) <- c-lineup-multi-inher
412 Works with: inher-cont, member-init-cont."
414 (back-to-indentation)
415 (let* ((eol (c-point 'eol
))
417 (char-after-ip (char-after)))
418 (if (c-langelem-pos langelem
)
419 (goto-char (c-langelem-pos langelem
)))
421 ;; This kludge is necessary to support both inher-cont and
422 ;; member-init-cont, since they have different anchor positions.
423 (c-backward-syntactic-ws)
424 (when (eq (char-before) ?
:)
426 (c-backward-syntactic-ws))
428 (c-syntactic-re-search-forward ":" eol
'move
)
429 (if (looking-at c-syntactic-eol
)
430 (c-forward-syntactic-ws here
)
431 (if (eq char-after-ip ?
,)
433 (skip-chars-forward " \t" eol
)))
435 (vector (current-column)))
438 (defun c-lineup-java-inher (langelem)
439 "Line up Java implements and extends declarations.
440 If class names follow on the same line as the implements/extends
441 keyword, they are lined up under each other. Otherwise, they are
442 indented by adding `c-basic-offset' to the column of the keyword.
446 extends extends Cyphr,
447 Bar <-> Bar <- c-lineup-java-inher
450 Works with: inher-cont."
452 (goto-char (c-langelem-pos langelem
))
454 (if (looking-at "[ \t]*$")
456 (c-forward-syntactic-ws)
457 (vector (current-column)))))
459 (defun c-lineup-java-throws (langelem)
460 "Line up Java throws declarations.
461 If exception names follow on the same line as the throws keyword,
462 they are lined up under each other. Otherwise, they are indented by
463 adding `c-basic-offset' to the column of the throws keyword. The
464 throws keyword itself is also indented by `c-basic-offset' from the
465 function declaration start if it doesn't hang. E.g:
467 int foo() int foo() throws Cyphr,
468 throws <-> Bar, <- c-lineup-java-throws
469 Bar <-> Vlod <- c-lineup-java-throws
470 <--><--> c-basic-offset
472 Works with: func-decl-cont."
474 (let* ((lim (1- (c-point 'bol
)))
476 (goto-char (c-langelem-pos langelem
))
477 (while (zerop (c-forward-token-2 1 t lim
))
478 (if (looking-at "throws\\>[^_]")
481 (if (zerop (c-forward-token-2 1 nil
(c-point 'eol
)))
482 (vector (current-column))
483 (back-to-indentation)
484 (vector (+ (current-column) c-basic-offset
)))
487 (defun c-indent-one-line-block (langelem)
488 "Indent a one line block `c-basic-offset' extra.
491 if (n > 0) if (n > 0)
492 {m+=n; n=0;} <-> { <- c-indent-one-line-block
493 <--> c-basic-offset m+=n; n=0;
496 The block may use any kind of parenthesis character. nil is returned
497 if the line doesn't start with a one line block, which makes the
498 function usable in list expressions.
500 Work with: Almost all syntactic symbols, but most useful on *-open."
502 (let ((eol (c-point 'eol
)))
503 (back-to-indentation)
504 (if (and (eq (char-syntax (char-after)) ?\
()
505 (c-safe (progn (c-forward-sexp) t
))
510 (defun c-indent-multi-line-block (langelem)
511 "Indent a multi line block `c-basic-offset' extra.
514 int *foo[] = { int *foo[] = {
516 {17}, <-> { <- c-indent-multi-line-block
521 The block may use any kind of parenthesis character. nil is returned
522 if the line doesn't start with a multi line block, which makes the
523 function usable in list expressions.
525 Work with: Almost all syntactic symbols, but most useful on *-open."
527 (let ((eol (c-point 'eol
)))
528 (back-to-indentation)
529 (if (and (eq (char-syntax (char-after)) ?\
()
530 (or (not (c-safe (progn (c-forward-sexp) t
)))
535 (defun c-lineup-C-comments (langelem)
536 "Line up C block comment continuation lines.
537 Various heuristics are used to handle many of the common comment
538 styles. Some examples:
540 /* /** /* /* text /* /**
541 * text * text text text ** text ** text
544 /*********************************************************************
546 ********************************************************************/
548 /*********************************************************************
549 Free form text comments:
550 In comments with a long delimiter line at the start, the indentation
551 is kept unchanged for lines that start with an empty comment line
552 prefix. The delimiter line is whatever matches the
553 `comment-start-skip' regexp.
554 *********************************************************************/
556 The variable `c-comment-prefix-regexp' is used to recognize the
557 comment line prefix, e.g. the `*' that usually starts every line
560 Works with: The `c' syntactic symbol."
562 (let* ((here (point))
563 (prefixlen (progn (back-to-indentation)
564 (if (looking-at c-current-comment-prefix
)
565 (- (match-end 0) (point))
568 ;; Get the length of the comment starter, not including
569 ;; the first '/'. We check if the comment prefix matched
570 ;; on the current line matches the starter or if it
571 ;; matches comment-start-skip, and choose whichever is
574 (goto-char (1+ (c-langelem-pos langelem
)))
575 (if (and (match-string 0)
576 (looking-at (regexp-quote (match-string 0))))
577 (- (match-end 0) (match-beginning 0))
580 (goto-char (c-langelem-pos langelem
))
581 (looking-at comment-start-skip
)
584 (goto-char (match-end 0))
585 (skip-chars-backward " \t")
589 (if (and (> starterlen
10) (zerop prefixlen
))
590 ;; The comment has a long starter and the line doesn't have
591 ;; a nonempty comment prefix. Treat it as free form text
592 ;; and don't change the indentation.
593 (vector (current-column))
594 ;; Go back to the previous non-blank line, if any.
598 (back-to-indentation)
599 (and (> (point) (c-langelem-pos langelem
))
600 (looking-at "[ \t]*$"))))
601 ;; Is the starting line the first continuation line with content?
602 (if (>= (c-langelem-pos langelem
) (point))
603 (if (zerop prefixlen
)
604 ;; No nonempty comment prefix. Align after comment
607 (looking-at comment-start-skip
)
608 (goto-char (match-end 0))
609 ;; The following should not be necessary, since
610 ;; comment-start-skip should match everything (i.e.
611 ;; typically whitespace) that leads up to the text.
612 ;;(if (looking-at "\\([ \t]+\\).+$")
613 ;; ;; Align with the text that hangs after the
614 ;; ;; comment starter.
615 ;; (goto-char (match-end 1)))
616 (vector (current-column)))
617 ;; How long is the comment starter? if greater than the
618 ;; length of the comment prefix, align left. if less
619 ;; than or equal, align right. this should also pick up
620 ;; Javadoc style comments.
621 (if (> starterlen prefixlen
)
623 (goto-char (c-langelem-pos langelem
))
624 (vector (1+ (current-column))))
625 (goto-char (+ (c-langelem-pos langelem
) starterlen
1))
626 (vector (- (current-column) prefixlen
))))
627 ;; We didn't start on the first non-blank continuation line. If the
628 ;; previous line has a nonempty comment prefix, align with it.
629 ;; Otherwise, align with the previous nonempty line, but align the
630 ;; comment ender with the starter.
631 (when (or (not (looking-at c-current-comment-prefix
))
632 (eq (match-beginning 0) (match-end 0)))
634 (back-to-indentation)
635 (if (looking-at (concat "\\(" c-current-comment-prefix
"\\)\\*/"))
636 (goto-char (c-langelem-pos langelem
))
637 (while (and (zerop (forward-line -
1))
638 (looking-at "^[ \t]*$")))
639 (back-to-indentation)
640 (if (< (point) (c-langelem-pos langelem
))
641 ;; Align with the comment starter rather than
642 ;; with the code before it.
643 (goto-char (c-langelem-pos langelem
)))))
644 (vector (current-column)))))))
646 (defun c-lineup-comment (langelem)
647 "Line up a comment start according to `c-comment-only-line-offset'.
648 If the comment is lined up with a comment starter on the previous
649 line, that alignment is preserved.
651 Works with: comment-intro."
653 (back-to-indentation)
654 (let ((col (current-column)))
656 ;; CASE 1: preserve aligned comments
658 (and (c-backward-single-comment)
659 (= col
(current-column))))
660 (vector col
)) ; Return an absolute column.
661 ;; indent as specified by c-comment-only-line-offset
663 (or (car-safe c-comment-only-line-offset
)
664 c-comment-only-line-offset
))
666 (or (cdr-safe c-comment-only-line-offset
)
667 (car-safe c-comment-only-line-offset
)
668 -
1000)) ;jam it against the left side
671 (defun c-lineup-knr-region-comment (langelem)
672 "Line up a comment in the \"K&R region\" with the declaration.
673 That is the region between the function or class header and the
674 beginning of the block. E.g:
677 /* This is the main function. */ <- c-lineup-knr-region-comment
682 Return nil if called in any other situation, to be useful in list
685 Works with: comment-intro."
686 (when (or (assq 'topmost-intro-cont c-syntactic-context
)
687 (assq 'func-decl-cont c-syntactic-context
)
688 (assq 'knr-argdecl-intro c-syntactic-context
)
689 (assq 'lambda-intro-cont c-syntactic-context
))
692 (c-beginning-of-statement-1)
693 (vector (current-column)))))
695 (defun c-lineup-runin-statements (langelem)
696 "Line up statements when the first statement is on the same line as
697 the block opening brace. E.g:
700 { puts (\"Hello world!\");
701 return 0; <- c-lineup-runin-statements
704 If there is no statement after the opening brace to align with, nil is
705 returned. This makes the function usable in list expressions.
707 Works with: The `statement' syntactic symbol."
708 (if (eq (char-after (c-langelem-pos langelem
)) ?
{)
710 (if (c-langelem-pos langelem
)
711 (goto-char (c-langelem-pos langelem
)))
713 (skip-chars-forward " \t")
715 (vector (current-column))))))
717 (defun c-lineup-assignments (langelem)
718 "Line up the current line after the assignment operator on the first
719 line in the statement. If there isn't any, return nil to allow
720 stacking with other line-up functions. If the current line contains
721 an assignment operator too, try to align it with the first one.
723 Works with: topmost-intro-cont, statement-cont, arglist-cont,
724 arglist-cont-nonempty."
725 (let (startpos endpos equalp
)
727 (if (eq (c-langelem-sym langelem
) 'arglist-cont-nonempty
)
728 ;; If it's an arglist-cont-nonempty then we're only interested
729 ;; in equal signs outside it. We don't search for a "=" on
730 ;; the current line since that'd have a different nesting
731 ;; compared to the one we should align with.
734 (setq endpos
(c-langelem-2nd-pos c-syntactic-element
))
735 (narrow-to-region (c-langelem-pos langelem
) endpos
)
736 (if (setq startpos
(c-up-list-backward endpos
))
737 (setq startpos
(1+ startpos
))
738 (setq startpos
(c-langelem-pos langelem
)))))
740 (setq startpos
(c-langelem-pos langelem
)
743 ;; Find a syntactically relevant and unnested "=" token on the
744 ;; current line. equalp is in that case set to the number of
745 ;; columns to left shift the current line to align it with the
749 (when (c-syntactic-re-search-forward
750 c-assignment-op-regexp
751 (c-point 'eol
) t t t
)
752 (setq equalp
(- (or (match-beginning 1)
758 (if (or (if (c-syntactic-re-search-forward
759 c-assignment-op-regexp
760 (min endpos
(c-point 'eol
)) t t t
)
762 (goto-char (or (match-beginning 1)
767 (c-forward-syntactic-ws (c-point 'eol
))
769 ;; There's no equal sign on the line, or there is one but
770 ;; nothing follows it.
773 ;; calculate indentation column after equals and ws, unless
774 ;; our line contains an equals sign
777 (skip-chars-forward " \t")
780 (vector (- (current-column) equalp
)))
783 (defun c-lineup-math (langelem)
784 "Like `c-lineup-assignments' but indent with `c-basic-offset' if no
785 assignment operator was found on the first line. I.e. this function
786 is the same as specifying a list (c-lineup-assignments +). It's
787 provided for compatibility with old configurations.
789 Works with: topmost-intro-cont, statement-cont, arglist-cont,
790 arglist-cont-nonempty."
791 (or (c-lineup-assignments langelem
)
794 (defun c-lineup-cascaded-calls (langelem)
795 "Line up \"cascaded calls\" under each other.
796 If the line begins with \"->\" or \".\" and the preceding line ends
797 with one or more function calls preceded by the same token, then the
798 arrow is lined up with the first of those tokens. E.g:
800 result = proc->add(17)->add(18)
801 ->add(19) + <- c-lineup-cascaded-calls
802 offset; <- c-lineup-cascaded-calls (inactive)
804 In any other situation nil is returned to allow use in list
807 Works with: topmost-intro-cont, statement-cont, arglist-cont,
808 arglist-cont-nonempty."
810 (if (and (eq (c-langelem-sym langelem
) 'arglist-cont-nonempty
)
811 (not (eq (c-langelem-2nd-pos c-syntactic-element
)
812 (c-most-enclosing-brace (c-parse-state)))))
813 ;; The innermost open paren is not our one, so don't do
814 ;; anything. This can occur for arglist-cont-nonempty with
815 ;; nested arglist starts on the same line.
819 (back-to-indentation)
820 (let ((operator (and (looking-at "->\\|\\.")
821 (regexp-quote (match-string 0))))
822 (stmt-start (c-langelem-pos langelem
)) col
)
825 (looking-at operator
)
826 (zerop (c-backward-token-2 1 t stmt-start
))
827 (eq (char-after) ?\
()
828 (zerop (c-backward-token-2 2 t stmt-start
))
829 (looking-at operator
))
830 (setq col
(current-column))
832 (while (and (zerop (c-backward-token-2 1 t stmt-start
))
833 (eq (char-after) ?\
()
834 (zerop (c-backward-token-2 2 t stmt-start
))
835 (looking-at operator
))
836 (setq col
(current-column)))
840 (defun c-lineup-string-cont (langelem)
841 "Line up a continued string under the one it continues.
842 A continued string in this sense is where a string literal follows
843 directly after another one. E.g:
845 result = prefix + \"A message \"
846 \"string.\"; <- c-lineup-string-cont
848 In other situations, returns nil, to allow stacking with other
851 Works with: topmost-intro-cont, statement-cont, arglist-cont,
852 arglist-cont-nonempty."
854 (back-to-indentation)
855 (and (looking-at "\\s\"")
856 (let ((quote (char-after)) pos
)
857 (while (and (progn (c-backward-syntactic-ws)
858 (eq (char-before) quote
))
859 (c-safe (c-backward-sexp) t
)
860 (/= (setq pos
(point)) (c-point 'boi
))))
863 (vector (current-column)))))))
865 (defun c-lineup-template-args (langelem)
866 "Line up template argument lines under the first argument.
867 To allow this function to be used in a list expression, nil is
868 returned if there's no template argument on the first line.
870 Works with: template-args-cont."
872 (c-with-syntax-table c
++-template-syntax-table
875 (if (and (eq (char-after) ?
<)
876 (zerop (c-forward-token-2 1 nil
(c-point 'eol
))))
877 (vector (current-column))))))
879 (defun c-lineup-ObjC-method-call (langelem)
880 "Line up selector args as Emacs Lisp mode does with function args:
881 Go to the position right after the message receiver, and if you are at
882 the end of the line, indent the current line c-basic-offset columns
883 from the opening bracket; otherwise you are looking at the first
884 character of the first method call argument, so line up the current
887 Works with: objc-method-call-cont."
889 (let* ((extra (save-excursion
890 (back-to-indentation)
891 (c-backward-syntactic-ws (c-langelem-pos langelem
))
892 (if (eq (char-before) ?
:)
895 (open-bracket-pos (c-langelem-pos langelem
))
896 (open-bracket-col (progn
897 (goto-char open-bracket-pos
)
902 (skip-chars-forward " \t")
904 (+ open-bracket-col c-basic-offset
)
907 (- target-col open-bracket-col extra
))))
909 (defun c-lineup-ObjC-method-args (langelem)
910 "Line up the colons that separate args.
911 The colon on the current line is aligned with the one on the first
914 Works with: objc-method-args-cont."
916 (let* ((here (c-point 'boi
))
917 (curcol (progn (goto-char here
) (current-column)))
919 (relpos (c-langelem-pos langelem
))
920 (first-col-column (progn
922 (skip-chars-forward "^:" eol
)
923 (and (eq (char-after) ?
:)
925 (if (not first-col-column
)
928 (skip-chars-forward "^:" eol
)
929 (if (eq (char-after) ?
:)
930 (+ curcol
(- first-col-column
(current-column)))
933 (defun c-lineup-ObjC-method-args-2 (langelem)
934 "Line up the colons that separate args.
935 The colon on the current line is aligned with the one on the previous
938 Works with: objc-method-args-cont."
940 (let* ((here (c-point 'boi
))
941 (curcol (progn (goto-char here
) (current-column)))
943 (relpos (c-langelem-pos langelem
))
944 (prev-col-column (progn
945 (skip-chars-backward "^:" relpos
)
946 (and (eq (char-before) ?
:)
947 (- (current-column) 1)))))
948 (if (not prev-col-column
)
951 (skip-chars-forward "^:" eol
)
952 (if (eq (char-after) ?
:)
953 (+ curcol
(- prev-col-column
(current-column)))
956 (defun c-lineup-inexpr-block (langelem)
957 "Line up the block for constructs that use a block inside an expression,
958 e.g. anonymous classes in Java and lambda functions in Pike. The body
959 is aligned with the start of the header, e.g. with the \"new\" or
960 \"lambda\" keyword. Returns nil if the block isn't part of such a
963 Works with: inlambda, inexpr-statement, inexpr-class."
965 (back-to-indentation)
966 (let* ((paren-state (c-parse-state))
967 (containing-sexp (c-most-enclosing-brace paren-state
))
968 (res (or (c-looking-at-inexpr-block
969 (c-safe-position containing-sexp paren-state
)
972 (progn (goto-char containing-sexp
)
973 (eq (char-after) ?
{))
974 (progn (setq containing-sexp
975 (c-most-enclosing-brace paren-state
977 (c-looking-at-inexpr-block
978 (c-safe-position containing-sexp paren-state
)
979 containing-sexp
))))))
981 (goto-char (cdr res
))
982 (vector (current-column))))))
984 (defun c-lineup-whitesmith-in-block (langelem)
985 "Line up lines inside a block in Whitesmith style.
986 It's done in a way that works both when the opening brace hangs and
987 when it doesn't. E.g:
991 foo; <-> foo; <- c-lineup-whitesmith-in-block
995 In the first case the indentation is kept unchanged, in the
996 second `c-basic-offset' is added.
998 Works with: defun-close, defun-block-intro, inline-close, block-close,
999 brace-list-close, brace-list-intro, statement-block-intro,
1000 arglist-intro, arglist-cont-nonempty, arglist-close, and all in*
1001 symbols, e.g. inclass and inextern-lang."
1003 (if (and (c-go-up-list-backward)
1004 (= (point) (c-point 'boi
)))
1008 (defun c-lineup-after-whitesmith-blocks (langelem)
1009 "Compensate for Whitesmith style indentation of blocks.
1010 Due to the way CC Mode calculates anchor positions for normal lines
1011 inside blocks, this function is necessary for those lines to get
1012 correct Whitesmith style indentation. Consider the following
1020 x; <-> x; <- c-lineup-after-whitesmith-blocks
1022 The fact that the line with \"x\" is preceded by a Whitesmith style
1023 indented block in one case and not the other should not affect its
1024 indentation. But since CC Mode in cases like this uses the
1025 indentation of the preceding statement as anchor position, the \"x\"
1026 would in the rightmost case be indented too much if the offset for
1027 `statement' was set simply to zero.
1029 This lineup function corrects for this situation by detecting if the
1030 anchor position is at an open paren character. In that case, it
1031 instead indents relative to the surrounding block just like
1032 `c-lineup-whitesmith-in-block'.
1034 Works with: brace-list-entry, brace-entry-open, statement,
1037 (goto-char (c-langelem-pos langelem
))
1038 (when (looking-at "\\s\(")
1039 (if (c-go-up-list-backward)
1040 (let ((pos (point)))
1041 (back-to-indentation)
1043 (vector (current-column))
1044 (vector (+ (current-column) c-basic-offset
))))
1047 (defun c-lineup-cpp-define (langelem)
1048 "Line up macro continuation lines according to the indentation of
1049 the construct preceding the macro. E.g:
1051 v beg of preceding constr v beg of preceding constr
1053 const char msg[] = if (!running)
1054 \"Some text.\"; error(\"Not running!\");
1056 #define X(A, B) \ #define X(A, B) \
1057 do { \ <-> do { \ <- c-lineup-cpp-define
1058 printf (A, B); \ printf (A, B); \
1059 } while (0) } while (0)
1061 If `c-syntactic-indentation-in-macros' is non-nil, the function
1062 returns the relative indentation to the macro start line to allow
1063 accumulation with other offsets. E.g. in the following cases,
1064 cpp-define-intro is combined with the statement-block-intro that comes
1065 from the \"do {\" that hangs on the \"#define\" line:
1068 const char msg[] = if (!running)
1069 \"Some text.\"; error(\"Not running!\");
1071 #define X(A, B) do { \ #define X(A, B) do { \
1072 printf (A, B); \ <-> printf (A, B); \ <- c-lineup-cpp-define
1073 this->refs++; \ this->refs++; \
1074 } while (0) <-> } while (0) <- c-lineup-cpp-define
1076 The relative indentation returned by `c-lineup-cpp-define' is zero and
1077 two, respectively, in these two examples. They are then added to the
1078 two column indentation that statement-block-intro gives in both cases
1081 If the relative indentation is zero, then nil is returned instead.
1082 That is useful in a list expression to specify the default indentation
1085 If `c-syntactic-indentation-in-macros' is nil then this function keeps
1086 the current indentation, except for empty lines \(ignoring the ending
1087 backslash) where it takes the indentation from the closest preceding
1088 nonempty line in the macro. If there's no such line in the macro then
1089 the indentation is taken from the construct preceding it, as described
1092 Works with: cpp-define-intro."
1094 (if c-syntactic-indentation-in-macros
1095 ;; Go to the macro start and do a syntactic analysis of it.
1096 ;; Then remove the cpp-macro element it should contain and
1097 ;; calculate the indentation it then would get.
1099 (c-beginning-of-macro)
1100 (setq offset
(- (c-get-syntactic-indentation
1101 (delete '(cpp-macro) (c-guess-basic-syntax)))
1103 (back-to-indentation)
1108 ;; Do not indent syntactically inside the macro.
1110 (let ((macro-start-line (save-excursion
1111 (goto-char (c-query-macro-start))
1115 ;; Check every line while inside the macro.
1116 (while (and (> (point) macro-start-line
)
1117 (looking-at "[ \t]*\\\\?$")
1118 (= (forward-line -
1) 0)))
1119 (if (<= (point) macro-start-line
)
1120 ;; If we've stepped out of the macro we take the
1121 ;; syntactic offset.
1122 (setq offset
(c-get-syntactic-indentation
1123 (delete '(cpp-macro) (c-guess-basic-syntax))))
1124 (setq offset
(current-indentation)))
1127 (vector offset
)))))))
1129 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
1130 (defun c-lineup-gcc-asm-reg (elem)
1131 "Line up a gcc asm register under one on a previous line.
1133 asm (\"foo %1, %0\\n\"
1140 The \"x\" line is aligned to the text after the \":\" on the \"w\" line, and
1141 similarly \"z\" under \"y\".
1143 This is done only in an \"asm\" or \"__asm__\" block, and only to
1144 those lines mentioned. Anywhere else nil is returned. The usual
1145 arrangement is to have this routine as an extra feature at the start
1146 of arglist line-ups, e.g.
1148 (c-lineup-gcc-asm-reg c-lineup-arglist)
1150 Works with: arglist-cont, arglist-cont-nonempty."
1152 (let ((orig-pos (point))
1158 ;; Don't do anything if the innermost open paren isn't our one.
1159 ;; This can occur for arglist-cont-nonempty with nested arglist
1160 ;; starts on the same line.
1161 (or (not (eq (car elem
) 'arglist-cont-nonempty
))
1162 (eq (c-langelem-2nd-pos c-syntactic-element
)
1163 (c-most-enclosing-brace (c-parse-state))))
1165 ;; Find the ":" to align to. Look for this first so as to quickly
1166 ;; eliminate pretty much all cases which are not for us.
1167 (re-search-backward "^[ \t]*:[ \t]*\\(.\\)?" (cdr elem
) t
)
1169 ;; Must have something after the ":".
1170 (setq alignto
(match-beginning 1))
1172 ;; Don't touch ":" lines themselves.
1173 (progn (goto-char orig-pos
)
1175 (not (looking-at "^[ \t]*:")))
1177 ;; Only operate in an asm statement.
1178 (progn (goto-char orig-pos
)
1181 (vector (progn (goto-char alignto
) (current-column)))))))
1183 (defun c-lineup-dont-change (langelem)
1184 "Do not change the indentation of the current line.
1186 Works with: Any syntactic symbol."
1188 (back-to-indentation)
1189 (vector (current-column))))
1192 (defun c-snug-do-while (syntax pos
)
1193 "Dynamically calculate brace hanginess for do-while statements.
1194 Using this function, `while' clauses that end a `do-while' block will
1195 remain on the same line as the brace that closes that block.
1197 See `c-hanging-braces-alist' for how to utilize this function as an
1198 ACTION associated with `block-close' syntax."
1201 (if (and (eq syntax
'block-close
)
1202 (setq langelem
(assq 'block-close c-syntactic-context
))
1203 (progn (goto-char (c-langelem-pos langelem
))
1204 (if (eq (char-after) ?
{)
1205 (c-safe (c-forward-sexp -
1)))
1206 (looking-at "\\<do\\>[^_]")))
1210 (defun c-snug-1line-defun-close (syntax pos
)
1211 "Determine the brace hanginess for an AWK defun-close.
1212 If the action/function being closed is a one-liner, keep it so. Otherwise put
1213 the closing brace on its own line."
1216 (if (> (c-point 'bol
)
1217 (progn (up-list -
1) (point)))
1221 (defun c-gnu-impose-minimum ()
1222 "Imposes a minimum indentation for lines inside code blocks.
1223 The variable `c-label-minimum-indentation' specifies the minimum
1224 indentation amount."
1227 ;; Don't adjust macro or comment-only lines.
1228 (or (assq 'cpp-macro c-syntactic-context
)
1229 (assq 'comment-intro c-syntactic-context
)))
1230 (c-intersect-lists c-inside-block-syms c-syntactic-context
)
1232 (back-to-indentation)
1233 (< (current-column) c-label-minimum-indentation
)))
1234 (c-shift-line-indentation (- c-label-minimum-indentation
1235 (current-indentation)))))
1238 ;; Useful for c-hanging-semi&comma-criteria
1240 (defun c-semi&comma-inside-parenlist
()
1241 "Controls newline insertion after semicolons in parenthesis lists.
1242 If a comma was inserted, no determination is made. If a semicolon was
1243 inserted inside a parenthesis list, no newline is added otherwise a
1244 newline is added. In either case, checking is stopped. This supports
1245 exactly the old newline insertion behavior."
1246 ;; newline only after semicolon, but only if that semicolon is not
1247 ;; inside a parenthesis list (e.g. a for loop statement)
1248 (if (not (eq last-command-char ?\
;))
1249 nil
; continue checking
1250 (if (condition-case nil
1253 (not (eq (char-after) ?\
()))
1258 ;; Suppresses newlines before non-blank lines
1259 (defun c-semi&comma-no-newlines-before-nonblanks
()
1260 "Controls newline insertion after semicolons.
1261 If a comma was inserted, no determination is made. If a semicolon was
1262 inserted, and the following line is not blank, no newline is inserted.
1263 Otherwise, no determination is made."
1265 (if (and (= last-command-char ?\
;)
1267 ;; (save-excursion (skip-syntax-forward " ") (point))
1268 (zerop (forward-line 1))
1269 (bolp) ; forward-line has funny behavior at eob.
1270 (not (looking-at "^[ \t]*$")))
1274 ;; Suppresses new lines after semicolons in one-liners methods
1275 (defun c-semi&comma-no-newlines-for-oneline-inliners
()
1276 "Controls newline insertion after semicolons for some one-line methods.
1277 If a comma was inserted, no determination is made. Newlines are
1278 suppressed in one-liners, if the line is an in-class inline function.
1279 For other semicolon contexts, no determination is made."
1280 (let ((syntax (c-guess-basic-syntax))
1281 (bol (save-excursion
1282 (if (c-safe (up-list -
1) t
)
1285 (if (and (eq last-command-char ?\
;)
1286 (eq (car (car syntax
)) 'inclass
)
1287 (eq (car (car (cdr syntax
))) 'topmost-intro
)
1288 (= (c-point 'bol
) bol
))
1293 (cc-provide 'cc-align
)
1295 ;; arch-tag: 4d71ed28-bf51-4509-a148-f39669669a2e
1296 ;;; cc-align.el ends here