1 ;;; cc-align.el --- custom indentation functions for CC Mode
3 ;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
5 ;; Authors: 2000- Martin Stjernholm
6 ;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
7 ;; 1992-1997 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)
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 GNU Emacs; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA.
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 (require 'cc-bytecomp)))
46 (cc-require 'cc-langs)
47 (cc-require 'cc-engine)
50 ;; Standard indentation line-ups
52 (defun c-lineup-topmost-intro-cont (langelem)
53 "Line up declaration continuation lines zero or one indentation step.
54 For lines in the \"header\" of a definition, zero is used. For other
55 lines, `c-basic-offset' is added to the indentation. E.g:
58 neg (int i) <- c-lineup-topmost-intro-cont
64 larch <- c-lineup-topmost-intro-cont
68 the_larch, <- c-lineup-topmost-intro-cont
69 another_larch; <- c-lineup-topmost-intro-cont
73 the_larch, <- c-lineup-topmost-intro-cont
74 another_larch; <- c-lineup-topmost-intro-cont
76 \(This function is mainly provided to mimic the behavior of CC Mode
77 5.28 and earlier where this case wasn't handled consistently so that
78 these lines could be analyzed as either topmost-intro-cont or
81 Works with: topmost-intro-cont."
84 (c-backward-syntactic-ws (cdr langelem))
85 (if (memq (char-before) '(?} ?,))
88 (defun c-lineup-arglist (langelem)
89 "Line up the current argument line under the first argument.
91 Works with: arglist-cont-nonempty, arglist-close."
94 (let ((containing-sexp (c-most-enclosing-brace (c-parse-state))))
95 (goto-char (1+ containing-sexp))
96 (let ((eol (c-point 'eol)))
97 (c-forward-syntactic-ws)
99 (goto-char (1+ containing-sexp))
100 (skip-chars-forward " \t")))
101 (vector (current-column)))))
103 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
104 (defun c-lineup-argcont (elem)
105 "Line up a continued argument.
107 foo (xyz, aaa + bbb + ccc
108 + ddd + eee + fff); <- c-lineup-argcont
110 Only continuation lines like this are touched, nil is returned on lines
111 which are the start of an argument.
113 Within a gcc asm block, \":\" is recognised as an argument separator,
114 but of course only between operand specifications, not in the expressions
117 Works with: arglist-cont, arglist-cont-nonempty."
123 ;; Previous line ending in a comma means we're the start of an
124 ;; argument. This should quickly catch most cases not for us.
125 (c-backward-syntactic-ws)
126 (let ((c (char-before)))
129 ;; In a gcc asm, ":" on the previous line means the start of an
130 ;; argument. And lines starting with ":" are not for us, don't
131 ;; want them to indent to the preceding operand.
132 (let ((gcc-asm (save-excursion
139 (looking-at "[ \t]*:"))))
141 (c-lineup-argcont-scan (if gcc-asm ?:))
142 (vector (current-column)))))))))
144 (defun c-lineup-argcont-scan (&optional other-match)
145 ;; Find the start of an argument, for `c-lineup-argcont'.
146 (when (eq 0 (c-backward-token-1 1 t))
147 (let ((c (char-after)))
148 (if (or (eq c ?,) (eq c other-match))
151 (c-forward-syntactic-ws))
152 (c-lineup-argcont-scan other-match)))))
154 (defun c-lineup-arglist-intro-after-paren (langelem)
155 "Line up a line just after the open paren of the surrounding paren or
158 Works with: defun-block-intro, brace-list-intro,
159 statement-block-intro, statement-case-intro, arglist-intro."
163 (skip-chars-forward " \t" (c-point 'eol))
164 (vector (1+ (current-column)))))
166 (defun c-lineup-arglist-close-under-paren (langelem)
167 "Line up a closing paren line under the corresponding open paren.
169 Works with: defun-close, class-close, inline-close, block-close,
170 brace-list-close, arglist-close, extern-lang-close, namespace-close
171 \(for most of these, a zero offset will normally produce the same
176 (vector (current-column))))
178 (defun c-lineup-close-paren (langelem)
179 "Line up the closing paren under its corresponding open paren if the
180 open paren is followed by code. If the open paren ends its line, no
181 indentation is added. E.g:
185 ) <-> ) <- c-lineup-close-paren
187 Works with: defun-close, class-close, inline-close, block-close,
188 brace-list-close, arglist-close, extern-lang-close, namespace-close."
194 (setq spec (c-looking-at-special-brace-list))
195 (if spec (goto-char (car (car spec))))
196 (setq opencol (current-column))
199 (c-forward-syntactic-ws)
201 (c-forward-syntactic-ws (c-point 'eol))
207 (defun c-lineup-streamop (langelem)
208 "Line up C++ stream operators under each other.
210 Works with: stream-op."
212 (goto-char (cdr langelem))
213 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
214 (goto-char (match-beginning 0))
215 (vector (current-column))))
217 (defun c-lineup-multi-inher (langelem)
218 "Line up the classes in C++ multiple inheritance clauses and member
219 initializers under each other. E.g:
221 class Foo: Foo::Foo (int a, int b):
222 public Cyphr, Cyphr (a),
223 public Bar <-> Bar (b) <- c-lineup-multi-inher
225 class Foo Foo::Foo (int a, int b)
226 : public Cyphr, : Cyphr (a),
227 public Bar <-> Bar (b) <- c-lineup-multi-inher
229 class Foo Foo::Foo (int a, int b)
230 : public Cyphr : Cyphr (a)
231 , public Bar <-> , Bar (b) <- c-lineup-multi-inher
233 Works with: inher-cont, member-init-cont."
235 (let* ((eol (c-point 'eol))
237 (char-after-ip (progn
238 (skip-chars-forward " \t")
240 (if (cdr langelem) (goto-char (cdr langelem)))
242 ;; This kludge is necessary to support both inher-cont and
243 ;; member-init-cont, since they have different anchor positions.
244 (c-backward-syntactic-ws)
245 (when (eq (char-before) ?:)
247 (c-backward-syntactic-ws))
249 (skip-chars-forward "^:" eol)
250 (if (eq char-after-ip ?,)
251 (skip-chars-forward " \t" eol)
252 (skip-chars-forward " \t:" eol))
254 (looking-at c-comment-start-regexp))
255 (c-forward-syntactic-ws here))
256 (vector (current-column))
259 (defun c-lineup-java-inher (langelem)
260 "Line up Java implements and extends declarations.
261 If class names follows on the same line as the implements/extends
262 keyword, they are lined up under each other. Otherwise, they are
263 indented by adding `c-basic-offset' to the column of the keyword.
267 extends extends Cyphr,
268 Bar <-> Bar <- c-lineup-java-inher
271 Works with: inher-cont."
273 (goto-char (cdr langelem))
275 (if (looking-at "[ \t]*$")
277 (c-forward-syntactic-ws)
278 (vector (current-column)))))
280 (defun c-lineup-java-throws (langelem)
281 "Line up Java throws declarations.
282 If exception names follows on the same line as the throws keyword,
283 they are lined up under each other. Otherwise, they are indented by
284 adding `c-basic-offset' to the column of the throws keyword. The
285 throws keyword itself is also indented by `c-basic-offset' from the
286 function declaration start if it doesn't hang. E.g:
288 int foo() int foo() throws Cyphr,
289 throws <-> Bar, <- c-lineup-java-throws
290 Bar <-> Vlod <- c-lineup-java-throws
291 <--><--> c-basic-offset
293 Works with: func-decl-cont."
295 (let* ((lim (1- (c-point 'bol)))
297 (goto-char (cdr langelem))
298 (while (zerop (c-forward-token-1 1 t lim))
299 (if (looking-at "throws\\>[^_]")
302 (if (zerop (c-forward-token-1 1 nil (c-point 'eol)))
303 (vector (current-column))
304 (back-to-indentation)
305 (vector (+ (current-column) c-basic-offset)))
308 (defun c-indent-one-line-block (langelem)
309 "Indent a one line block `c-basic-offset' extra.
312 if (n > 0) if (n > 0)
313 {m+=n; n=0;} <-> { <- c-indent-one-line-block
314 <--> c-basic-offset m+=n; n=0;
317 The block may use any kind of parenthesis character. nil is returned
318 if the line doesn't start with a one line block, which makes the
319 function usable in list expressions.
321 Work with: Almost all syntactic symbols, but most useful on *-open."
323 (let ((eol (c-point 'eol)))
324 (back-to-indentation)
325 (if (and (eq (char-syntax (char-after)) ?\()
326 (c-safe (progn (c-forward-sexp) t))
331 (defun c-indent-multi-line-block (langelem)
332 "Indent a multi line block `c-basic-offset' extra.
335 int *foo[] = { int *foo[] = {
337 {17}, <-> { <- c-indent-multi-line-block
342 The block may use any kind of parenthesis character. nil is returned
343 if the line doesn't start with a multi line block, which makes the
344 function usable in list expressions.
346 Work with: Almost all syntactic symbols, but most useful on *-open."
348 (let ((eol (c-point 'eol)))
349 (back-to-indentation)
350 (if (and (eq (char-syntax (char-after)) ?\()
351 (or (not (c-safe (progn (c-forward-sexp) t)))
356 (defun c-lineup-C-comments (langelem)
357 "Line up C block comment continuation lines.
358 Various heuristics are used to handle many of the common comment
359 styles. Some examples:
361 /* /** /* /* text /* /**
362 * text * text text text ** text ** text
365 /*********************************************************************
367 ********************************************************************/
369 /*********************************************************************
370 Free form text comments:
371 In comments with a long delimiter line at the start, the indentation
372 is kept unchanged for lines that start with an empty comment line
373 prefix. The delimiter line is whatever matches the
374 `comment-start-skip' regexp.
375 *********************************************************************/
377 The variable `c-comment-prefix-regexp' is used to recognize the
378 comment line prefix, e.g. the `*' that usually starts every line
381 Works with: The `c' syntactic symbol."
383 (let* ((here (point))
384 (prefixlen (progn (back-to-indentation)
385 (if (looking-at c-current-comment-prefix)
386 (- (match-end 0) (point))
389 ;; Get the length of the comment starter, not including
390 ;; the first '/'. We check if the comment prefix matched
391 ;; on the current line matches the starter or if it
392 ;; matches comment-start-skip, and choose whichever is
395 (goto-char (1+ (cdr langelem)))
396 (if (and (match-string 0)
397 (looking-at (regexp-quote (match-string 0))))
398 (- (match-end 0) (match-beginning 0))
401 (goto-char (cdr langelem))
402 (looking-at comment-start-skip)
405 (goto-char (match-end 0))
406 (skip-chars-backward " \t")
410 (if (and (> starterlen 10) (zerop prefixlen))
411 ;; The comment has a long starter and the line doesn't have
412 ;; a nonempty comment prefix. Treat it as free form text
413 ;; and don't change the indentation.
414 (vector (current-column))
416 (back-to-indentation)
417 (if (>= (cdr langelem) (point))
418 ;; On the second line in the comment.
419 (if (zerop prefixlen)
420 ;; No nonempty comment prefix. Align after comment
423 (goto-char (match-end 0))
424 ;; The following should not be necessary, since
425 ;; comment-start-skip should match everything (i.e.
426 ;; typically whitespace) that leads up to the text.
427 ;;(if (looking-at "\\([ \t]+\\).+$")
428 ;; ;; Align with the text that hangs after the
429 ;; ;; comment starter.
430 ;; (goto-char (match-end 1)))
431 (vector (current-column)))
432 ;; How long is the comment starter? if greater than the
433 ;; length of the comment prefix, align left. if less
434 ;; than or equal, align right. this should also pick up
435 ;; Javadoc style comments.
436 (if (> starterlen prefixlen)
438 (goto-char (cdr langelem))
439 (vector (1+ (current-column))))
440 (goto-char (+ (cdr langelem) starterlen 1))
441 (vector (- (current-column) prefixlen))))
442 ;; Not on the second line in the comment. If the previous
443 ;; line has a nonempty comment prefix, align with it.
444 ;; Otherwise, align with the previous nonempty line, but
445 ;; align the comment ender with the starter.
446 (when (or (not (looking-at c-current-comment-prefix))
447 (eq (match-beginning 0) (match-end 0)))
449 (back-to-indentation)
450 (if (looking-at (concat "\\(" c-current-comment-prefix "\\)\\*/"))
451 (goto-char (cdr langelem))
452 (while (and (zerop (forward-line -1))
453 (looking-at "^[ \t]*$")))
454 (back-to-indentation)
455 (if (< (point) (cdr langelem))
456 ;; Align with the comment starter rather than
457 ;; with the code before it.
458 (goto-char (cdr langelem)))))
459 (vector (current-column)))))))
461 (defun c-lineup-comment (langelem)
462 "Line up a comment start according to `c-comment-only-line-offset'.
463 If the comment is lined up with a comment starter on the previous
464 line, that alignment is preserved.
466 Works with: comment-intro."
468 (back-to-indentation)
469 (let ((col (current-column)))
471 ;; CASE 1: preserve aligned comments
473 (and (c-forward-comment -1)
474 (= col (current-column))))
475 (vector col)) ; Return an absolute column.
476 ;; indent as specified by c-comment-only-line-offset
478 (or (car-safe c-comment-only-line-offset)
479 c-comment-only-line-offset))
481 (or (cdr-safe c-comment-only-line-offset)
482 (car-safe c-comment-only-line-offset)
483 -1000)) ;jam it against the left side
486 (defun c-lineup-knr-region-comment (langelem)
487 "Line up a comment in the \"K&R region\" with the declaration.
488 That is the region between the function or class header and the
489 beginning of the block. E.g:
492 /* This is the main function. */ <- c-lineup-knr-region-comment
497 Return nil if called in any other situation, to be useful in list
500 Works with: comment-intro."
501 (when (or (assq 'topmost-intro-cont c-syntactic-context)
502 (assq 'func-decl-cont c-syntactic-context)
503 (assq 'knr-argdecl-intro c-syntactic-context)
504 (assq 'lambda-intro-cont c-syntactic-context))
507 (c-beginning-of-statement-1)
508 (vector (current-column)))))
510 (defun c-lineup-runin-statements (langelem)
511 "Line up statements when the first statement is on the same line as
512 the block opening brace. E.g:
515 { puts (\"Hello world!\");
516 return 0; <- c-lineup-runin-statements
519 If there is no statement after the opening brace to align with, nil is
520 returned. This makes the function usable in list expressions.
522 Works with: The `statement' syntactic symbol."
523 (if (eq (char-after (cdr langelem)) ?{)
525 (if (cdr langelem) (goto-char (cdr langelem)))
527 (skip-chars-forward " \t")
529 (vector (current-column))))))
531 (defun c-lineup-math (langelem)
532 "Line up the current line after the equal sign on the first line in
533 the statement. If there isn't any, indent with `c-basic-offset'. If
534 the current line contains an equal sign too, try to align it with the
537 Works with: statement-cont, arglist-cont, arglist-cont-nonempty."
539 (let ((equalp (save-excursion
540 (goto-char (c-point 'boi))
541 (let ((eol (c-point 'eol)))
542 (c-forward-token-1 0 t eol)
543 (while (and (not (eq (char-after) ?=))
544 (= (c-forward-token-1 1 t eol) 0))))
545 (and (eq (char-after) ?=)
546 (- (point) (c-point 'boi)))))
548 (if (cdr langelem) (goto-char (cdr langelem)))
549 (while (and (not donep)
550 (< (point) (c-point 'eol)))
551 (skip-chars-forward "^=" (c-point 'eol))
552 (if (c-in-literal (cdr langelem))
555 (if (or (not (eq (char-after) ?=))
558 (c-forward-syntactic-ws (c-point 'eol))
560 ;; there's no equal sign on the line
562 ;; calculate indentation column after equals and ws, unless
563 ;; our line contains an equals sign
567 (skip-chars-forward " \t")
569 (vector (- (current-column) equalp)))
572 (defun c-lineup-cascaded-calls (langelem)
573 "Line up \"cascaded calls\" under each other.
574 If the line begins with \"->\" and the preceding line ends with one or
575 more function calls preceded by \"->\", then the arrow is lined up with
576 the first of those \"->\". E.g:
578 result = proc->add(17)->add(18)
579 ->add(19) + <- c-lineup-cascaded-calls
580 offset; <- c-lineup-cascaded-calls (inactive)
582 In any other situation nil is returned to allow use in list
585 Works with: statement-cont, arglist-cont, arglist-cont-nonempty."
587 (let ((bopl (c-point 'bopl)) col)
588 (back-to-indentation)
589 (when (and (looking-at "->")
590 (= (c-backward-token-1 1 t bopl) 0)
591 (eq (char-after) ?\()
592 (= (c-backward-token-1 3 t bopl) 0)
594 (setq col (current-column))
595 (while (and (= (c-backward-token-1 1 t bopl) 0)
596 (eq (char-after) ?\()
597 (= (c-backward-token-1 3 t bopl) 0)
599 (setq col (current-column)))
602 (defun c-lineup-template-args (langelem)
603 "Line up template argument lines under the first argument.
604 To allow this function to be used in a list expression, nil is
605 returned if there's no template argument on the first line.
607 Works with: template-args-cont."
609 (c-with-syntax-table c++-template-syntax-table
612 (if (and (eq (char-after) ?<)
613 (zerop (c-forward-token-1 1 nil (c-point 'eol))))
614 (vector (current-column))))))
616 (defun c-lineup-ObjC-method-call (langelem)
617 "Line up selector args as elisp-mode does with function args:
618 Go to the position right after the message receiver, and if you are at
619 the end of the line, indent the current line c-basic-offset columns
620 from the opening bracket; otherwise you are looking at the first
621 character of the first method call argument, so lineup the current
624 Works with: objc-method-call-cont."
626 (let* ((extra (save-excursion
627 (back-to-indentation)
628 (c-backward-syntactic-ws (cdr langelem))
629 (if (eq (char-before) ?:)
632 (open-bracket-pos (cdr langelem))
633 (open-bracket-col (progn
634 (goto-char open-bracket-pos)
639 (skip-chars-forward " \t")
641 (+ open-bracket-col c-basic-offset)
644 (- target-col open-bracket-col extra))))
646 (defun c-lineup-ObjC-method-args (langelem)
647 "Line up the colons that separate args.
648 The colon on the current line is aligned with the one on the first
651 Works with: objc-method-args-cont."
653 (let* ((here (c-point 'boi))
654 (curcol (progn (goto-char here) (current-column)))
656 (relpos (cdr langelem))
657 (first-col-column (progn
659 (skip-chars-forward "^:" eol)
660 (and (eq (char-after) ?:)
662 (if (not first-col-column)
665 (skip-chars-forward "^:" eol)
666 (if (eq (char-after) ?:)
667 (+ curcol (- first-col-column (current-column)))
670 (defun c-lineup-ObjC-method-args-2 (langelem)
671 "Line up the colons that separate args.
672 The colon on the current line is aligned with the one on the previous
675 Works with: objc-method-args-cont."
677 (let* ((here (c-point 'boi))
678 (curcol (progn (goto-char here) (current-column)))
680 (relpos (cdr langelem))
681 (prev-col-column (progn
682 (skip-chars-backward "^:" relpos)
683 (and (eq (char-before) ?:)
684 (- (current-column) 1)))))
685 (if (not prev-col-column)
688 (skip-chars-forward "^:" eol)
689 (if (eq (char-after) ?:)
690 (+ curcol (- prev-col-column (current-column)))
693 (defun c-lineup-inexpr-block (langelem)
694 "Line up the block for constructs that use a block inside an expression,
695 e.g. anonymous classes in Java and lambda functions in Pike. The body
696 is aligned with the start of the header, e.g. with the \"new\" or
697 \"lambda\" keyword. Returns nil if the block isn't part of such a
700 Works with: inlambda, inexpr-statement, inexpr-class."
702 (back-to-indentation)
703 (let* ((paren-state (c-parse-state))
704 (containing-sexp (c-most-enclosing-brace paren-state))
705 (res (or (c-looking-at-inexpr-block
706 (c-safe-position containing-sexp paren-state)
709 (progn (goto-char containing-sexp)
710 (eq (char-after) ?{))
711 (progn (setq containing-sexp
712 (c-most-enclosing-brace paren-state
714 (c-looking-at-inexpr-block
715 (c-safe-position containing-sexp paren-state)
716 containing-sexp))))))
718 (goto-char (cdr res))
721 (back-to-indentation)
722 (current-column)))))))
724 (defun c-lineup-whitesmith-in-block (langelem)
725 "Line up lines inside a block in whitesmith style.
726 It's done in a way that works both when the opening brace hangs and
727 when it doesn't. E.g:
731 foo; <-> foo; <- c-lineup-whitesmith-in-block
735 In the first case the indentation is kept unchanged, in the
736 second `c-basic-offset' is added.
738 Works with: defun-close, defun-block-intro, block-close,
739 brace-list-close, brace-list-intro, statement-block-intro, inclass,
740 inextern-lang, innamespace."
742 (goto-char (cdr langelem))
743 (back-to-indentation)
744 (if (eq (char-syntax (char-after)) ?\()
748 (defun c-lineup-cpp-define (langelem)
749 "Line up macro continuation lines according to the indentation of
750 the construct preceding the macro. E.g:
752 v beg of preceding constr v beg of preceding constr
754 const char msg[] = if (!running)
755 \"Some text.\"; error(\"Not running!\");
757 #define X(A, B) \ #define X(A, B) \
758 do { \ <-> do { \ <- c-lineup-cpp-define
759 printf (A, B); \ printf (A, B); \
760 } while (0) } while (0)
762 If `c-syntactic-indentation-in-macros' is non-nil, the function
763 returns the relative indentation to the macro start line to allow
764 accumulation with other offsets. E.g. in the following cases,
765 cpp-define-intro is combined with the statement-block-intro that comes
766 from the \"do {\" that hangs on the \"#define\" line:
769 const char msg[] = if (!running)
770 \"Some text.\"; error(\"Not running!\");
772 #define X(A, B) do { \ #define X(A, B) do { \
773 printf (A, B); \ <-> printf (A, B); \ <- c-lineup-cpp-define
774 this->refs++; \ this->refs++; \
775 } while (0) <-> } while (0) <- c-lineup-cpp-define
777 The relative indentation returned by `c-lineup-cpp-define' is zero and
778 two, respectively, in these two examples. They are then added to the
779 two column indentation that statement-block-intro gives in both cases
782 If the relative indentation is zero, then nil is returned instead.
783 This useful in a list expression to specify the default indentation on
786 If `c-syntactic-indentation-in-macros' is nil then this function keeps
787 the current indentation, except for empty lines \(ignoring the ending
788 backslash) where it takes the indentation from the closest preceding
789 nonempty line in the macro. If there's no such line in the macro then
790 the indentation is taken from the construct preceding it, as described
793 Works with: cpp-define-intro."
795 (if c-syntactic-indentation-in-macros
796 ;; Go to the macro start and do a syntactic analysis of it.
797 ;; Then remove the cpp-macro element it should contain and
798 ;; calculate the indentation it then would get.
800 (c-beginning-of-macro)
801 (setq offset (- (c-get-syntactic-indentation
802 (delete '(cpp-macro) (c-guess-basic-syntax)))
804 (back-to-indentation)
809 ;; Do not indent syntactically inside the macro.
811 (let ((macro-start-line (save-excursion
812 (goto-char (c-query-macro-start))
816 ;; Check every line while inside the macro.
817 (while (and (> (point) macro-start-line)
818 (looking-at "[ \t]*\\\\?$")
819 (= (forward-line -1) 0)))
820 (if (<= (point) macro-start-line)
821 ;; If we've stepped out of the macro we take the
823 (setq offset (c-get-syntactic-indentation
824 (delete '(cpp-macro) (c-guess-basic-syntax))))
825 (setq offset (current-indentation)))
828 (vector offset)))))))
830 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
831 (defun c-lineup-gcc-asm-reg (elem)
832 "Line up a gcc asm register under one on a previous line.
834 asm (\"foo %1, %0\\n\"
841 The \"x\" line is aligned to the text after the \":\" on the \"w\" line, and
842 similarly \"z\" under \"y\".
844 This is done only in an \"asm\" or \"__asm__\" block, and only to those
845 lines mentioned. Anywhere else nil is returned. The usual arrangement is
846 to have this routine as an extra feature at the start of arglist lineups, e.g.
848 (c-lineup-gcc-asm-reg c-lineup-arglist)
850 Works with: arglist-cont, arglist-cont-nonempty."
852 (let ((orig-pos (point))
858 ;; Find the ":" to align to. Look for this first so as to quickly
859 ;; eliminate pretty much all cases which are not for us.
860 (re-search-backward "^[ \t]*:[ \t]*\\(.\\)?" (cdr elem) t)
862 ;; Must have something after the ":".
863 (setq alignto (match-beginning 1))
865 ;; Don't touch ":" lines themselves.
866 (progn (goto-char orig-pos)
868 (not (looking-at "^[ \t]*:")))
870 ;; Only operate in an asm statement.
871 (progn (goto-char orig-pos)
874 (vector (progn (goto-char alignto) (current-column)))))))
876 (defun c-lineup-dont-change (langelem)
877 "Do not change the indentation of the current line.
879 Works with: Any syntactic symbol."
881 (back-to-indentation)
882 (vector (current-column))))
885 (defun c-snug-do-while (syntax pos)
886 "Dynamically calculate brace hanginess for do-while statements.
887 Using this function, `while' clauses that end a `do-while' block will
888 remain on the same line as the brace that closes that block.
890 See `c-hanging-braces-alist' for how to utilize this function as an
891 ACTION associated with `block-close' syntax."
894 (if (and (eq syntax 'block-close)
895 (setq langelem (assq 'block-close c-syntactic-context))
896 (progn (goto-char (cdr langelem))
897 (if (eq (char-after) ?{)
898 (c-safe (c-forward-sexp -1)))
899 (looking-at "\\<do\\>[^_]")))
903 (defun c-gnu-impose-minimum ()
904 "Imposes a minimum indentation for lines inside a top-level construct.
905 The variable `c-label-minimum-indentation' specifies the minimum
907 (let ((non-top-levels '(defun-block-intro statement statement-cont
908 statement-block-intro statement-case-intro
909 statement-case-open substatement substatement-open
910 case-label label do-while-closure else-clause
912 (syntax c-syntactic-context)
915 (setq langelem (car (car syntax))
917 ;; don't adjust macro or comment-only lines
918 (cond ((memq langelem '(cpp-macro comment-intro))
920 ((memq langelem non-top-levels)
923 (back-to-indentation)
924 (if (zerop (current-column))
925 (insert-char ?\ c-label-minimum-indentation t))
930 ;; Useful for c-hanging-semi&comma-criteria
932 (defun c-semi&comma-inside-parenlist ()
933 "Controls newline insertion after semicolons in parenthesis lists.
934 If a comma was inserted, no determination is made. If a semicolon was
935 inserted inside a parenthesis list, no newline is added otherwise a
936 newline is added. In either case, checking is stopped. This supports
937 exactly the old newline insertion behavior."
938 ;; newline only after semicolon, but only if that semicolon is not
939 ;; inside a parenthesis list (e.g. a for loop statement)
940 (if (not (eq last-command-char ?\;))
941 nil ; continue checking
942 (if (condition-case nil
945 (not (eq (char-after) ?\()))
950 ;; Suppresses newlines before non-blank lines
951 (defun c-semi&comma-no-newlines-before-nonblanks ()
952 "Controls newline insertion after semicolons.
953 If a comma was inserted, no determination is made. If a semicolon was
954 inserted, and the following line is not blank, no newline is inserted.
955 Otherwise, no determination is made."
957 (if (and (= last-command-char ?\;)
959 ;; (save-excursion (skip-syntax-forward " ") (point))
960 (zerop (forward-line 1))
961 (not (looking-at "^[ \t]*$")))
965 ;; Suppresses new lines after semicolons in one-liners methods
966 (defun c-semi&comma-no-newlines-for-oneline-inliners ()
967 "Controls newline insertion after semicolons for some one-line methods.
968 If a comma was inserted, no determination is made. Newlines are
969 suppressed in one-liners, if the line is an in-class inline function.
970 For other semicolon contexts, no determination is made."
971 (let ((syntax (c-guess-basic-syntax))
973 (if (c-safe (up-list -1) t)
976 (if (and (eq last-command-char ?\;)
977 (eq (car (car syntax)) 'inclass)
978 (eq (car (car (cdr syntax))) 'topmost-intro)
979 (= (c-point 'bol) bol))
984 (cc-provide 'cc-align)
986 ;;; cc-align.el ends here