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