Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / progmodes / cc-align.el
CommitLineData
785eecbb
RS
1;;; cc-align.el --- custom indentation functions for CC Mode
2
92ab3834 3;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4e643dd2 4;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
d7a0267c 5;; Free Software Foundation, Inc.
785eecbb 6
e309f66c
AM
7;; Authors: 2004- Alan Mackenzie
8;; 1998- Martin Stjernholm
d9e94c22 9;; 1992-1999 Barry A. Warsaw
785eecbb
RS
10;; 1987 Dave Detlefs and Stewart Clamen
11;; 1985 Richard M. Stallman
0ec8351b 12;; Maintainer: bug-cc-mode@gnu.org
785eecbb 13;; Created: 22-Apr-1997 (split from cc-mode.el)
81eb2ff9 14;; Version: See cc-mode.el
785eecbb
RS
15;; Keywords: c languages oop
16
17;; This file is part of GNU Emacs.
18
b1fc2b50 19;; GNU Emacs is free software: you can redistribute it and/or modify
785eecbb 20;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
21;; the Free Software Foundation, either version 3 of the License, or
22;; (at your option) any later version.
785eecbb
RS
23
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.
28
29;; You should have received a copy of the GNU General Public License
b1fc2b50 30;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
785eecbb 31
3afbc435
PJ
32;;; Commentary:
33
34;;; Code:
35
785eecbb 36(eval-when-compile
51f606de 37 (let ((load-path
130c507e
GM
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)
51f606de 41 load-path)))
d9e94c22 42 (load "cc-bytecomp" nil t)))
130c507e
GM
43
44(cc-require 'cc-defs)
45(cc-require 'cc-vars)
130c507e 46(cc-require 'cc-engine)
785eecbb
RS
47
48\f
0386b551 49;; Standard line-up functions
d9e94c22 50;;
0386b551
AM
51;; See the section "Custom Indentation Functions" in the manual for
52;; details on the calling convention.
d9e94c22 53
a66cd3ee
MS
54(defun c-lineup-topmost-intro-cont (langelem)
55 "Line up declaration continuation lines zero or one indentation step.
56For lines in the \"header\" of a definition, zero is used. For other
57lines, `c-basic-offset' is added to the indentation. E.g:
58
59int
60neg (int i) <- c-lineup-topmost-intro-cont
61{
62 return -i;
63}
64
65struct
66larch <- c-lineup-topmost-intro-cont
67{
68 double height;
69}
70 the_larch, <- c-lineup-topmost-intro-cont
71 another_larch; <- c-lineup-topmost-intro-cont
72<--> c-basic-offset
73
74struct larch
75the_larch, <- c-lineup-topmost-intro-cont
76 another_larch; <- c-lineup-topmost-intro-cont
77
78\(This function is mainly provided to mimic the behavior of CC Mode
795.28 and earlier where this case wasn't handled consistently so that
80these lines could be analyzed as either topmost-intro-cont or
81statement-cont.)
82
83Works with: topmost-intro-cont."
84 (save-excursion
85 (beginning-of-line)
0386b551
AM
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))))
a66cd3ee
MS
90 c-basic-offset)))
91
51c9af45
AM
92(defun c-lineup-gnu-DEFUN-intro-cont (langelem)
93 "Line up the continuation lines of a DEFUN macro in the Emacs C source.
94These lines are indented as though they were `knr-argdecl-intro' lines.
95Return nil when we're not in such a construct.
96
97This function is for historical compatibility with how previous CC Modes (5.28
98and earlier) indented such lines.
99
100Here is an example:
101
102DEFUN (\"forward-char\", Fforward_char, Sforward_char, 0, 1, \"p\",
103 doc: /* Move point right N characters (left if N is negative).
104On 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
107
108Works with: topmost-intro-cont."
109 (save-excursion
110 (let (case-fold-search)
111 (goto-char (c-langelem-pos langelem))
112 (if (looking-at "\\<DEFUN\\>")
113 (c-calc-offset '(knr-argdecl-intro))))))
114
0386b551
AM
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.
120
121 (or
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)
130
131 (save-restriction
132 ;; Search for open braces from the arglist start to the end of the
133 ;; line.
134 (narrow-to-region arglist-start (c-point 'eol arglist-start))
135
136 (goto-char arglist-start)
137 (while (and (c-syntactic-re-search-forward "{" nil t)
138 (progn
139 (backward-char)
140 (or
141 ;; Ignore starts of special brace lists.
142 (and c-special-brace-lists
143 (save-restriction
144 (widen)
145 (c-looking-at-special-brace-list)))
146 ;; Ignore complete blocks.
147 (c-safe (c-forward-sexp) t))))
148 (forward-char))
149
150 (looking-at "{"))
151
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)
161 containing-sexp))))
162
785eecbb 163(defun c-lineup-arglist (langelem)
51f606de
GM
164 "Line up the current argument line under the first argument.
165
0386b551
AM
166As a special case, if the indented line is inside a brace block
167construct, the indentation is `c-basic-offset' only. This is intended
168as a \"DWIM\" measure in cases like macros that contains statement
169blocks, e.g:
d9e94c22
MS
170
171A_VERY_LONG_MACRO_NAME ({
172 some (code, with + long, lines * in[it]);
173 });
174<--> c-basic-offset
175
176This is motivated partly because it's more in line with how code
177blocks are handled, and partly since it approximates the behavior of
178earlier CC Mode versions, which due to inaccurate analysis tended to
179indent such cases this way.
180
a66cd3ee 181Works with: arglist-cont-nonempty, arglist-close."
785eecbb 182 (save-excursion
0386b551
AM
183 (let ((indent-pos (point)))
184
185 (if (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element))
186 c-basic-offset ; DWIM case.
d9e94c22
MS
187
188 ;; Normal case. Indent to the token after the arglist open paren.
0386b551
AM
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)
194 (forward-char))
195 (forward-char))
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)))))))
a66cd3ee
MS
202
203;; Contributed by Kevin Ryde <user42@zip.com.au>.
204(defun c-lineup-argcont (elem)
205 "Line up a continued argument.
206
207foo (xyz, aaa + bbb + ccc
208 + ddd + eee + fff); <- c-lineup-argcont
209
8df87102 210Only continuation lines like this are touched, nil is returned on lines
a66cd3ee
MS
211which are the start of an argument.
212
2aa34723 213Within a gcc asm block, \":\" is recognized as an argument separator,
a66cd3ee
MS
214but of course only between operand specifications, not in the expressions
215for the operands.
216
217Works with: arglist-cont, arglist-cont-nonempty."
218
219 (save-excursion
220 (beginning-of-line)
a66cd3ee 221
d9e94c22
MS
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.
0386b551 227 (let ((open-paren (c-langelem-2nd-pos c-syntactic-element))
d9e94c22
MS
228 (paren-state (c-parse-state)))
229 (while (not (eq (car paren-state) open-paren))
3efc2cd7
MS
230 (unless (consp (car paren-state)) ;; ignore matched braces
231 (goto-char (car paren-state)))
d9e94c22
MS
232 (setq paren-state (cdr paren-state)))))
233
234 (let ((start (point)) c)
235
236 (when (bolp)
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)))
242
243 (unless (eq c ?,)
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
248 (goto-char start)
249 (c-in-gcc-asm-p))))
250 (unless (and gcc-asm
251 (or (eq c ?:)
252 (save-excursion
253 (goto-char start)
254 (looking-at "[ \t]*:"))))
255
256 (c-lineup-argcont-scan (if gcc-asm ?:))
257 (vector (current-column))))))))
a66cd3ee
MS
258
259(defun c-lineup-argcont-scan (&optional other-match)
260 ;; Find the start of an argument, for `c-lineup-argcont'.
d9e94c22 261 (when (zerop (c-backward-token-2 1 t))
a66cd3ee
MS
262 (let ((c (char-after)))
263 (if (or (eq c ?,) (eq c other-match))
264 (progn
265 (forward-char)
266 (c-forward-syntactic-ws))
267 (c-lineup-argcont-scan other-match)))))
785eecbb
RS
268
269(defun c-lineup-arglist-intro-after-paren (langelem)
d9e94c22
MS
270 "Line up a line to just after the open paren of the surrounding paren
271or brace block.
51f606de
GM
272
273Works with: defun-block-intro, brace-list-intro,
274statement-block-intro, statement-case-intro, arglist-intro."
785eecbb 275 (save-excursion
a66cd3ee
MS
276 (beginning-of-line)
277 (backward-up-list 1)
278 (skip-chars-forward " \t" (c-point 'eol))
279 (vector (1+ (current-column)))))
785eecbb
RS
280
281(defun c-lineup-arglist-close-under-paren (langelem)
d9e94c22
MS
282 "Line up a line under the enclosing open paren.
283Normally used to line up a closing paren in the same column as its
284corresponding open paren, but can also be used with arglist-cont and
285arglist-cont-nonempty to line up all lines inside a parenthesis under
286the open paren.
287
0386b551
AM
288As a special case, if a brace block construct starts at the same line
289as the open parenthesis of the argument list, the indentation is
d9e94c22
MS
290`c-basic-offset' only. See `c-lineup-arglist' for further discussion
291of this \"DWIM\" measure.
292
293Works with: Almost all symbols, but are typically most useful on
294arglist-close, brace-list-close, arglist-cont and arglist-cont-nonempty."
295 (save-excursion
0386b551
AM
296 (if (memq (c-langelem-sym langelem)
297 '(arglist-cont-nonempty arglist-close))
298 (goto-char (c-langelem-2nd-pos c-syntactic-element))
299 (beginning-of-line)
300 (c-go-up-list-backward))
301
302 (if (save-excursion (c-block-in-arglist-dwim (point)))
303 c-basic-offset ; DWIM case.
304
305 ;; Normal case. Indent to the arglist open paren.
306 (let (special-list)
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
310 ;; opener like "({".
311 (goto-char (car (car special-list))))
d9e94c22
MS
312 (vector (current-column))))))
313
314(defun c-lineup-arglist-operators (langelem)
315 "Line up lines starting with an infix operator under the open paren.
316Return nil on lines that don't start with an operator, to leave those
0386b551 317cases to other line-up functions. Example:
d9e94c22
MS
318
319if ( x < 10
320 || at_limit (x, <- c-lineup-arglist-operators
321 list) <- c-lineup-arglist-operators returns nil
322 )
323
324Since this function doesn't do anything for lines without an infix
0386b551 325operator you typically want to use it together with some other line-up
d9e94c22
MS
326settings, e.g. as follows \(the arglist-close setting is just a
327suggestion to get a consistent style):
328
329\(c-set-offset 'arglist-cont '(c-lineup-arglist-operators 0))
330\(c-set-offset 'arglist-cont-nonempty '(c-lineup-arglist-operators
331 c-lineup-arglist))
332\(c-set-offset 'arglist-close '(c-lineup-arglist-close-under-paren))
333
334Works with: arglist-cont, arglist-cont-nonempty."
785eecbb 335 (save-excursion
d9e94c22
MS
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))))
785eecbb 340
eae86618 341(defun c-lineup-close-paren (langelem)
51f606de
GM
342 "Line up the closing paren under its corresponding open paren if the
343open paren is followed by code. If the open paren ends its line, no
344indentation is added. E.g:
345
346main (int, main (
347 char ** int, char **
348 ) <-> ) <- c-lineup-close-paren
349
0386b551
AM
350As a special case, if a brace block construct starts at the same line
351as the open parenthesis of the argument list, the indentation is
d9e94c22
MS
352`c-basic-offset' instead of the open paren column. See
353`c-lineup-arglist' for further discussion of this \"DWIM\" measure.
354
355Works with: All *-close symbols."
eae86618 356 (save-excursion
0386b551
AM
357 (if (memq (c-langelem-sym langelem)
358 '(arglist-cont-nonempty arglist-close))
359 (goto-char (c-langelem-2nd-pos c-syntactic-element))
360 (beginning-of-line)
361 (c-go-up-list-backward))
d9e94c22 362
0386b551
AM
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
367 ;; opener like "({".
368 (progn
369 (goto-char (setq arglist-start (car (car special-list))))
370 (c-forward-token-2)
371 (forward-char))
372 (setq arglist-start (point))
373 (forward-char))
d9e94c22 374
0386b551
AM
375 (cond ((looking-at c-syntactic-eol)
376 0) ; The arglist is "empty".
377
378 ((c-block-in-arglist-dwim (point))
379 c-basic-offset) ; DWIM case.
380
381 (t
382 ;; Normal case. Indent to the arglist open paren.
383 (goto-char arglist-start)
384 (vector (current-column)))))))
eae86618 385
785eecbb 386(defun c-lineup-streamop (langelem)
51f606de
GM
387 "Line up C++ stream operators under each other.
388
389Works with: stream-op."
785eecbb 390 (save-excursion
0386b551 391 (goto-char (c-langelem-pos langelem))
a66cd3ee
MS
392 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
393 (goto-char (match-beginning 0))
394 (vector (current-column))))
785eecbb
RS
395
396(defun c-lineup-multi-inher (langelem)
c4052e82
GM
397 "Line up the classes in C++ multiple inheritance clauses and member
398initializers under each other. E.g:
51f606de 399
c4052e82
GM
400class Foo: Foo::Foo (int a, int b):
401 public Cyphr, Cyphr (a),
402 public Bar <-> Bar (b) <- c-lineup-multi-inher
403
404class Foo Foo::Foo (int a, int b)
405 : public Cyphr, : Cyphr (a),
406 public Bar <-> Bar (b) <- c-lineup-multi-inher
407
408class Foo Foo::Foo (int a, int b)
409 : public Cyphr : Cyphr (a)
410 , public Bar <-> , Bar (b) <- c-lineup-multi-inher
411
412Works with: inher-cont, member-init-cont."
785eecbb 413 (save-excursion
d9e94c22 414 (back-to-indentation)
c4052e82
GM
415 (let* ((eol (c-point 'eol))
416 (here (point))
2a15eb73 417 (char-after-ip (char-after)))
0386b551
AM
418 (if (c-langelem-pos langelem)
419 (goto-char (c-langelem-pos langelem)))
c4052e82
GM
420
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) ?:)
425 (backward-char)
426 (c-backward-syntactic-ws))
427
2a15eb73
MS
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 ?,)
432 (backward-char)
433 (skip-chars-forward " \t" eol)))
d9e94c22
MS
434 (if (< (point) here)
435 (vector (current-column)))
785eecbb
RS
436 )))
437
438(defun c-lineup-java-inher (langelem)
51f606de 439 "Line up Java implements and extends declarations.
d9e94c22 440If class names follow on the same line as the implements/extends
51f606de
GM
441keyword, they are lined up under each other. Otherwise, they are
442indented by adding `c-basic-offset' to the column of the keyword.
443E.g:
444
445class Foo class Foo
446 extends extends Cyphr,
447 Bar <-> Bar <- c-lineup-java-inher
448 <--> c-basic-offset
449
450Works with: inher-cont."
785eecbb 451 (save-excursion
0386b551 452 (goto-char (c-langelem-pos langelem))
a66cd3ee
MS
453 (forward-word 1)
454 (if (looking-at "[ \t]*$")
455 c-basic-offset
456 (c-forward-syntactic-ws)
457 (vector (current-column)))))
785eecbb
RS
458
459(defun c-lineup-java-throws (langelem)
51f606de 460 "Line up Java throws declarations.
d9e94c22 461If exception names follow on the same line as the throws keyword,
51f606de
GM
462they are lined up under each other. Otherwise, they are indented by
463adding `c-basic-offset' to the column of the throws keyword. The
464throws keyword itself is also indented by `c-basic-offset' from the
465function declaration start if it doesn't hang. E.g:
466
467int foo() int foo() throws Cyphr,
468 throws <-> Bar, <- c-lineup-java-throws
469 Bar <-> Vlod <- c-lineup-java-throws
470<--><--> c-basic-offset
471
472Works with: func-decl-cont."
785eecbb 473 (save-excursion
51f606de
GM
474 (let* ((lim (1- (c-point 'bol)))
475 (throws (catch 'done
0386b551 476 (goto-char (c-langelem-pos langelem))
d9e94c22 477 (while (zerop (c-forward-token-2 1 t lim))
51f606de
GM
478 (if (looking-at "throws\\>[^_]")
479 (throw 'done t))))))
480 (if throws
d9e94c22 481 (if (zerop (c-forward-token-2 1 nil (c-point 'eol)))
a66cd3ee 482 (vector (current-column))
51f606de 483 (back-to-indentation)
a66cd3ee 484 (vector (+ (current-column) c-basic-offset)))
51f606de 485 c-basic-offset))))
785eecbb 486
eae86618 487(defun c-indent-one-line-block (langelem)
51f606de
GM
488 "Indent a one line block `c-basic-offset' extra.
489E.g:
490
491if (n > 0) if (n > 0)
492 {m+=n; n=0;} <-> { <- c-indent-one-line-block
493<--> c-basic-offset m+=n; n=0;
494 }
495
130c507e
GM
496The block may use any kind of parenthesis character. nil is returned
497if the line doesn't start with a one line block, which makes the
498function usable in list expressions.
51f606de
GM
499
500Work with: Almost all syntactic symbols, but most useful on *-open."
eae86618 501 (save-excursion
51f606de
GM
502 (let ((eol (c-point 'eol)))
503 (back-to-indentation)
504 (if (and (eq (char-syntax (char-after)) ?\()
0ec8351b 505 (c-safe (progn (c-forward-sexp) t))
51f606de 506 (<= (point) eol))
eae86618 507 c-basic-offset
51f606de 508 nil))))
eae86618 509
51f606de
GM
510(defun c-indent-multi-line-block (langelem)
511 "Indent a multi line block `c-basic-offset' extra.
512E.g:
513
514int *foo[] = { int *foo[] = {
515 NULL, NULL,
516 {17}, <-> { <- c-indent-multi-line-block
517 17
518 }
519 <--> c-basic-offset
520
130c507e
GM
521The block may use any kind of parenthesis character. nil is returned
522if the line doesn't start with a multi line block, which makes the
523function usable in list expressions.
51f606de
GM
524
525Work with: Almost all syntactic symbols, but most useful on *-open."
785eecbb 526 (save-excursion
51f606de 527 (let ((eol (c-point 'eol)))
785eecbb 528 (back-to-indentation)
51f606de
GM
529 (if (and (eq (char-syntax (char-after)) ?\()
530 (or (not (c-safe (progn (c-forward-sexp) t)))
531 (> (point) eol)))
532 c-basic-offset
533 nil))))
534
535(defun c-lineup-C-comments (langelem)
536 "Line up C block comment continuation lines.
130c507e 537Various heuristics are used to handle many of the common comment
51f606de
GM
538styles. Some examples:
539
540/* /** /* /* text /* /**
541 * text * text text text ** text ** text
542 */ */ */ */ */ */
543
544/*********************************************************************
545 * text
546 ********************************************************************/
547
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*********************************************************************/
555
556The variable `c-comment-prefix-regexp' is used to recognize the
557comment line prefix, e.g. the `*' that usually starts every line
558inside a comment.
559
560Works with: The `c' syntactic symbol."
561 (save-excursion
562 (let* ((here (point))
563 (prefixlen (progn (back-to-indentation)
130c507e 564 (if (looking-at c-current-comment-prefix)
51f606de
GM
565 (- (match-end 0) (point))
566 0)))
a66cd3ee
MS
567 (starterlen
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
572 ;; longest.
573 (max (save-excursion
0386b551 574 (goto-char (1+ (c-langelem-pos langelem)))
a66cd3ee
MS
575 (if (and (match-string 0)
576 (looking-at (regexp-quote (match-string 0))))
577 (- (match-end 0) (match-beginning 0))
578 0))
579 (save-excursion
0386b551 580 (goto-char (c-langelem-pos langelem))
a66cd3ee
MS
581 (looking-at comment-start-skip)
582 (- (or (match-end 1)
583 (save-excursion
584 (goto-char (match-end 0))
585 (skip-chars-backward " \t")
586 (point)))
587 (point)
588 1)))))
51f606de
GM
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.
a66cd3ee 593 (vector (current-column))
0386b551
AM
594 ;; Go back to the previous non-blank line, if any.
595 (while
596 (progn
597 (forward-line -1)
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))
51f606de
GM
603 (if (zerop prefixlen)
604 ;; No nonempty comment prefix. Align after comment
605 ;; starter.
785eecbb 606 (progn
0386b551 607 (looking-at comment-start-skip)
51f606de 608 (goto-char (match-end 0))
a66cd3ee
MS
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)))
51f606de
GM
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)
622 (progn
0386b551 623 (goto-char (c-langelem-pos langelem))
a66cd3ee 624 (vector (1+ (current-column))))
0386b551 625 (goto-char (+ (c-langelem-pos langelem) starterlen 1))
a66cd3ee 626 (vector (- (current-column) prefixlen))))
0386b551
AM
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.
130c507e 631 (when (or (not (looking-at c-current-comment-prefix))
51f606de
GM
632 (eq (match-beginning 0) (match-end 0)))
633 (goto-char here)
634 (back-to-indentation)
130c507e 635 (if (looking-at (concat "\\(" c-current-comment-prefix "\\)\\*/"))
0386b551 636 (goto-char (c-langelem-pos langelem))
51f606de
GM
637 (while (and (zerop (forward-line -1))
638 (looking-at "^[ \t]*$")))
639 (back-to-indentation)
0386b551 640 (if (< (point) (c-langelem-pos langelem))
51f606de
GM
641 ;; Align with the comment starter rather than
642 ;; with the code before it.
0386b551 643 (goto-char (c-langelem-pos langelem)))))
a66cd3ee 644 (vector (current-column)))))))
785eecbb
RS
645
646(defun c-lineup-comment (langelem)
51f606de
GM
647 "Line up a comment start according to `c-comment-only-line-offset'.
648If the comment is lined up with a comment starter on the previous
649line, that alignment is preserved.
650
651Works with: comment-intro."
785eecbb
RS
652 (save-excursion
653 (back-to-indentation)
130c507e 654 (let ((col (current-column)))
785eecbb 655 (cond
51f606de
GM
656 ;; CASE 1: preserve aligned comments
657 ((save-excursion
d9e94c22 658 (and (c-backward-single-comment)
51f606de 659 (= col (current-column))))
130c507e 660 (vector col)) ; Return an absolute column.
785eecbb
RS
661 ;; indent as specified by c-comment-only-line-offset
662 ((not (bolp))
663 (or (car-safe c-comment-only-line-offset)
664 c-comment-only-line-offset))
665 (t
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
669 ))))
670
a66cd3ee
MS
671(defun c-lineup-knr-region-comment (langelem)
672 "Line up a comment in the \"K&R region\" with the declaration.
673That is the region between the function or class header and the
674beginning of the block. E.g:
675
676int main()
677/* This is the main function. */ <- c-lineup-knr-region-comment
678{
679 return 0;
680}
681
682Return nil if called in any other situation, to be useful in list
683expressions.
684
685Works 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))
690 (save-excursion
691 (beginning-of-line)
692 (c-beginning-of-statement-1)
693 (vector (current-column)))))
694
785eecbb 695(defun c-lineup-runin-statements (langelem)
51f606de
GM
696 "Line up statements when the first statement is on the same line as
697the block opening brace. E.g:
698
699int main()
700{ puts (\"Hello world!\");
701 return 0; <- c-lineup-runin-statements
702}
703
704If there is no statement after the opening brace to align with, nil is
705returned. This makes the function usable in list expressions.
706
707Works with: The `statement' syntactic symbol."
0386b551 708 (if (eq (char-after (c-langelem-pos langelem)) ?{)
785eecbb 709 (save-excursion
0386b551
AM
710 (if (c-langelem-pos langelem)
711 (goto-char (c-langelem-pos langelem)))
a66cd3ee
MS
712 (forward-char 1)
713 (skip-chars-forward " \t")
714 (unless (eolp)
715 (vector (current-column))))))
785eecbb 716
0386b551
AM
717(defun c-lineup-assignments (langelem)
718 "Line up the current line after the assignment operator on the first
719line in the statement. If there isn't any, return nil to allow
720stacking with other line-up functions. If the current line contains
721an assignment operator too, try to align it with the first one.
51f606de 722
d9e94c22
MS
723Works with: topmost-intro-cont, statement-cont, arglist-cont,
724arglist-cont-nonempty."
725 (let (startpos endpos equalp)
726
0386b551 727 (if (eq (c-langelem-sym langelem) 'arglist-cont-nonempty)
d9e94c22
MS
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.
732 (save-excursion
733 (save-restriction
0386b551
AM
734 (setq endpos (c-langelem-2nd-pos c-syntactic-element))
735 (narrow-to-region (c-langelem-pos langelem) endpos)
d9e94c22
MS
736 (if (setq startpos (c-up-list-backward endpos))
737 (setq startpos (1+ startpos))
0386b551 738 (setq startpos (c-langelem-pos langelem)))))
d9e94c22 739
0386b551 740 (setq startpos (c-langelem-pos langelem)
d9e94c22
MS
741 endpos (point))
742
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
746 ;; goal column.
747 (save-excursion
748 (beginning-of-line)
749 (when (c-syntactic-re-search-forward
846f5040
MS
750 c-assignment-op-regexp
751 (c-point 'eol) t t t)
752 (setq equalp (- (or (match-beginning 1)
753 (match-end 0))
754 (c-point 'boi))))))
d9e94c22
MS
755
756 (save-excursion
757 (goto-char startpos)
758 (if (or (if (c-syntactic-re-search-forward
846f5040
MS
759 c-assignment-op-regexp
760 (min endpos (c-point 'eol)) t t t)
d9e94c22 761 (progn
846f5040
MS
762 (goto-char (or (match-beginning 1)
763 (match-end 0)))
d9e94c22
MS
764 nil)
765 t)
0ec8351b 766 (save-excursion
0ec8351b
BW
767 (c-forward-syntactic-ws (c-point 'eol))
768 (eolp)))
d9e94c22
MS
769 ;; There's no equal sign on the line, or there is one but
770 ;; nothing follows it.
0386b551 771 nil
d9e94c22 772
785eecbb
RS
773 ;; calculate indentation column after equals and ws, unless
774 ;; our line contains an equals sign
775 (if (not equalp)
776 (progn
785eecbb
RS
777 (skip-chars-forward " \t")
778 (setq equalp 0)))
d9e94c22 779
a66cd3ee 780 (vector (- (current-column) equalp)))
785eecbb
RS
781 )))
782
0386b551
AM
783(defun c-lineup-math (langelem)
784 "Like `c-lineup-assignments' but indent with `c-basic-offset' if no
785assignment operator was found on the first line. I.e. this function
786is the same as specifying a list (c-lineup-assignments +). It's
787provided for compatibility with old configurations.
788
789Works with: topmost-intro-cont, statement-cont, arglist-cont,
790arglist-cont-nonempty."
791 (or (c-lineup-assignments langelem)
792 c-basic-offset))
793
a66cd3ee
MS
794(defun c-lineup-cascaded-calls (langelem)
795 "Line up \"cascaded calls\" under each other.
d9e94c22
MS
796If the line begins with \"->\" or \".\" and the preceding line ends
797with one or more function calls preceded by the same token, then the
798arrow is lined up with the first of those tokens. E.g:
a66cd3ee
MS
799
800result = proc->add(17)->add(18)
801 ->add(19) + <- c-lineup-cascaded-calls
802 offset; <- c-lineup-cascaded-calls (inactive)
803
804In any other situation nil is returned to allow use in list
805expressions.
806
d9e94c22
MS
807Works with: topmost-intro-cont, statement-cont, arglist-cont,
808arglist-cont-nonempty."
809
0386b551
AM
810 (if (and (eq (c-langelem-sym langelem) 'arglist-cont-nonempty)
811 (not (eq (c-langelem-2nd-pos c-syntactic-element)
d9e94c22
MS
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.
816 nil
817
818 (save-excursion
a66cd3ee 819 (back-to-indentation)
d9e94c22
MS
820 (let ((operator (and (looking-at "->\\|\\.")
821 (regexp-quote (match-string 0))))
0386b551 822 (stmt-start (c-langelem-pos langelem)) col)
d9e94c22
MS
823
824 (when (and operator
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))
831
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)))
837
838 (vector col))))))
839
840(defun c-lineup-string-cont (langelem)
841 "Line up a continued string under the one it continues.
842A continued string in this sense is where a string literal follows
843directly after another one. E.g:
844
845result = prefix + \"A message \"
846 \"string.\"; <- c-lineup-string-cont
847
d616b579 848In other situations, returns nil, to allow stacking with other
0386b551 849line-up functions.
d9e94c22
MS
850
851Works with: topmost-intro-cont, statement-cont, arglist-cont,
852arglist-cont-nonempty."
853 (save-excursion
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))))
861 (when pos
862 (goto-char pos)
863 (vector (current-column)))))))
a66cd3ee 864
51f606de
GM
865(defun c-lineup-template-args (langelem)
866 "Line up template argument lines under the first argument.
867To allow this function to be used in a list expression, nil is
868returned if there's no template argument on the first line.
869
870Works with: template-args-cont."
871 (save-excursion
872 (c-with-syntax-table c++-template-syntax-table
873 (beginning-of-line)
874 (backward-up-list 1)
875 (if (and (eq (char-after) ?<)
d9e94c22 876 (zerop (c-forward-token-2 1 nil (c-point 'eol))))
a66cd3ee 877 (vector (current-column))))))
51f606de 878
785eecbb 879(defun c-lineup-ObjC-method-call (langelem)
d9e94c22 880 "Line up selector args as Emacs Lisp mode does with function args:
51f606de
GM
881Go to the position right after the message receiver, and if you are at
882the end of the line, indent the current line c-basic-offset columns
883from the opening bracket; otherwise you are looking at the first
0386b551 884character of the first method call argument, so line up the current
51f606de
GM
885line with it.
886
887Works with: objc-method-call-cont."
785eecbb
RS
888 (save-excursion
889 (let* ((extra (save-excursion
890 (back-to-indentation)
0386b551 891 (c-backward-syntactic-ws (c-langelem-pos langelem))
785eecbb
RS
892 (if (eq (char-before) ?:)
893 (- c-basic-offset)
894 0)))
0386b551 895 (open-bracket-pos (c-langelem-pos langelem))
785eecbb
RS
896 (open-bracket-col (progn
897 (goto-char open-bracket-pos)
898 (current-column)))
899 (target-col (progn
900 (forward-char)
0ec8351b 901 (c-forward-sexp)
785eecbb
RS
902 (skip-chars-forward " \t")
903 (if (eolp)
904 (+ open-bracket-col c-basic-offset)
905 (current-column))))
906 )
907 (- target-col open-bracket-col extra))))
908
909(defun c-lineup-ObjC-method-args (langelem)
51f606de
GM
910 "Line up the colons that separate args.
911The colon on the current line is aligned with the one on the first
912line.
913
914Works with: objc-method-args-cont."
785eecbb
RS
915 (save-excursion
916 (let* ((here (c-point 'boi))
917 (curcol (progn (goto-char here) (current-column)))
918 (eol (c-point 'eol))
0386b551 919 (relpos (c-langelem-pos langelem))
785eecbb
RS
920 (first-col-column (progn
921 (goto-char relpos)
922 (skip-chars-forward "^:" eol)
923 (and (eq (char-after) ?:)
924 (current-column)))))
925 (if (not first-col-column)
926 c-basic-offset
927 (goto-char here)
928 (skip-chars-forward "^:" eol)
929 (if (eq (char-after) ?:)
930 (+ curcol (- first-col-column (current-column)))
931 c-basic-offset)))))
932
933(defun c-lineup-ObjC-method-args-2 (langelem)
51f606de
GM
934 "Line up the colons that separate args.
935The colon on the current line is aligned with the one on the previous
936line.
937
938Works with: objc-method-args-cont."
785eecbb
RS
939 (save-excursion
940 (let* ((here (c-point 'boi))
941 (curcol (progn (goto-char here) (current-column)))
942 (eol (c-point 'eol))
0386b551 943 (relpos (c-langelem-pos langelem))
785eecbb
RS
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)
949 c-basic-offset
950 (goto-char here)
951 (skip-chars-forward "^:" eol)
952 (if (eq (char-after) ?:)
953 (+ curcol (- prev-col-column (current-column)))
954 c-basic-offset)))))
955
0ec8351b 956(defun c-lineup-inexpr-block (langelem)
51f606de
GM
957 "Line up the block for constructs that use a block inside an expression,
958e.g. anonymous classes in Java and lambda functions in Pike. The body
959is 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
961construct.
962
963Works with: inlambda, inexpr-statement, inexpr-class."
0ec8351b
BW
964 (save-excursion
965 (back-to-indentation)
a66cd3ee
MS
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)
970 containing-sexp)
971 (and containing-sexp
972 (progn (goto-char containing-sexp)
973 (eq (char-after) ?{))
974 (progn (setq containing-sexp
975 (c-most-enclosing-brace paren-state
976 (point)))
977 (c-looking-at-inexpr-block
978 (c-safe-position containing-sexp paren-state)
979 containing-sexp))))))
51f606de 980 (when res
0ec8351b 981 (goto-char (cdr res))
0386b551 982 (vector (current-column))))))
0ec8351b 983
51f606de 984(defun c-lineup-whitesmith-in-block (langelem)
0386b551 985 "Line up lines inside a block in Whitesmith style.
51f606de
GM
986It's done in a way that works both when the opening brace hangs and
987when it doesn't. E.g:
988
989something
990 { something {
991 foo; <-> foo; <- c-lineup-whitesmith-in-block
992 } }
993 <--> c-basic-offset
994
995In the first case the indentation is kept unchanged, in the
996second `c-basic-offset' is added.
997
0386b551
AM
998Works with: defun-close, defun-block-intro, inline-close, block-close,
999brace-list-close, brace-list-intro, statement-block-intro,
1000arglist-intro, arglist-cont-nonempty, arglist-close, and all in*
d9e94c22 1001symbols, e.g. inclass and inextern-lang."
eae86618 1002 (save-excursion
0386b551
AM
1003 (if (and (c-go-up-list-backward)
1004 (= (point) (c-point 'boi)))
1005 nil
1006 c-basic-offset)))
1007
1008(defun c-lineup-after-whitesmith-blocks (langelem)
1009 "Compensate for Whitesmith style indentation of blocks.
1010Due to the way CC Mode calculates anchor positions for normal lines
1011inside blocks, this function is necessary for those lines to get
1012correct Whitesmith style indentation. Consider the following
1013examples:
1014
1015 int foo()
1016 {
1017int foo() {
1018 { a;
1019 a; }
1020 x; <-> x; <- c-lineup-after-whitesmith-blocks
1021
1022The fact that the line with \"x\" is preceded by a Whitesmith style
1023indented block in one case and not the other should not affect its
1024indentation. But since CC Mode in cases like this uses the
1025indentation of the preceding statement as anchor position, the \"x\"
1026would in the rightmost case be indented too much if the offset for
1027`statement' was set simply to zero.
1028
1029This lineup function corrects for this situation by detecting if the
1030anchor position is at an open paren character. In that case, it
1031instead indents relative to the surrounding block just like
1032`c-lineup-whitesmith-in-block'.
1033
1034Works with: brace-list-entry, brace-entry-open, statement,
1035arglist-cont."
1036 (save-excursion
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)
1042 (if (= pos (point))
1043 (vector (current-column))
1044 (vector (+ (current-column) c-basic-offset))))
1045 (vector 0)))))
eae86618 1046
a66cd3ee
MS
1047(defun c-lineup-cpp-define (langelem)
1048 "Line up macro continuation lines according to the indentation of
1049the construct preceding the macro. E.g:
1050
1051v beg of preceding constr v beg of preceding constr
1052 int dribble() {
1053const char msg[] = if (!running)
1054 \"Some text.\"; error(\"Not running!\");
1055
1056#define X(A, B) \ #define X(A, B) \
1057do { \ <-> do { \ <- c-lineup-cpp-define
1058 printf (A, B); \ printf (A, B); \
1059} while (0) } while (0)
1060
1061If `c-syntactic-indentation-in-macros' is non-nil, the function
1062returns the relative indentation to the macro start line to allow
1063accumulation with other offsets. E.g. in the following cases,
1064cpp-define-intro is combined with the statement-block-intro that comes
1065from the \"do {\" that hangs on the \"#define\" line:
1066
1067 int dribble() {
1068const char msg[] = if (!running)
1069 \"Some text.\"; error(\"Not running!\");
1070
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
1075
1076The relative indentation returned by `c-lineup-cpp-define' is zero and
d9e94c22 1077two, respectively, in these two examples. They are then added to the
a66cd3ee
MS
1078two column indentation that statement-block-intro gives in both cases
1079here.
1080
1081If the relative indentation is zero, then nil is returned instead.
d9e94c22
MS
1082That is useful in a list expression to specify the default indentation
1083on the top level.
a66cd3ee
MS
1084
1085If `c-syntactic-indentation-in-macros' is nil then this function keeps
1086the current indentation, except for empty lines \(ignoring the ending
1087backslash) where it takes the indentation from the closest preceding
1088nonempty line in the macro. If there's no such line in the macro then
1089the indentation is taken from the construct preceding it, as described
1090above.
1091
1092Works with: cpp-define-intro."
1093 (let (offset)
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.
1098 (save-excursion
1099 (c-beginning-of-macro)
1100 (setq offset (- (c-get-syntactic-indentation
1101 (delete '(cpp-macro) (c-guess-basic-syntax)))
1102 (save-excursion
1103 (back-to-indentation)
1104 (current-column))))
1105 (if (zerop offset)
1106 nil
1107 offset))
1108 ;; Do not indent syntactically inside the macro.
1109 (save-excursion
1110 (let ((macro-start-line (save-excursion
1111 (goto-char (c-query-macro-start))
1112 (beginning-of-line)
1113 (point))))
1114 (beginning-of-line)
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)))
1125 (if (zerop offset)
1126 nil
1127 (vector offset)))))))
1128
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.
1132
1133 asm (\"foo %1, %0\\n\"
1134 \"bar %0, %1\"
1135 : \"=r\" (w),
1136 \"=r\" (x)
1137 : \"0\" (y),
1138 \"1\" (z));
1139
1140The \"x\" line is aligned to the text after the \":\" on the \"w\" line, and
1141similarly \"z\" under \"y\".
1142
0386b551
AM
1143This is done only in an \"asm\" or \"__asm__\" block, and only to
1144those lines mentioned. Anywhere else nil is returned. The usual
1145arrangement is to have this routine as an extra feature at the start
1146of arglist line-ups, e.g.
a66cd3ee
MS
1147
1148 (c-lineup-gcc-asm-reg c-lineup-arglist)
1149
1150Works with: arglist-cont, arglist-cont-nonempty."
1151
1152 (let ((orig-pos (point))
1153 alignto)
1154 (save-excursion
1155 (and
1156 c-opt-asm-stmt-key
1157
d9e94c22
MS
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))
0386b551 1162 (eq (c-langelem-2nd-pos c-syntactic-element)
d9e94c22
MS
1163 (c-most-enclosing-brace (c-parse-state))))
1164
a66cd3ee
MS
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)
1168
1169 ;; Must have something after the ":".
1170 (setq alignto (match-beginning 1))
1171
1172 ;; Don't touch ":" lines themselves.
1173 (progn (goto-char orig-pos)
1174 (beginning-of-line)
1175 (not (looking-at "^[ \t]*:")))
1176
1177 ;; Only operate in an asm statement.
1178 (progn (goto-char orig-pos)
1179 (c-in-gcc-asm-p))
1180
1181 (vector (progn (goto-char alignto) (current-column)))))))
1182
51f606de
GM
1183(defun c-lineup-dont-change (langelem)
1184 "Do not change the indentation of the current line.
1185
1186Works with: Any syntactic symbol."
1187 (save-excursion
1188 (back-to-indentation)
130c507e 1189 (vector (current-column))))
eae86618
RS
1190
1191\f
785eecbb
RS
1192(defun c-snug-do-while (syntax pos)
1193 "Dynamically calculate brace hanginess for do-while statements.
1194Using this function, `while' clauses that end a `do-while' block will
1195remain on the same line as the brace that closes that block.
1196
1197See `c-hanging-braces-alist' for how to utilize this function as an
1198ACTION associated with `block-close' syntax."
1199 (save-excursion
1200 (let (langelem)
1201 (if (and (eq syntax 'block-close)
1202 (setq langelem (assq 'block-close c-syntactic-context))
0386b551 1203 (progn (goto-char (c-langelem-pos langelem))
785eecbb 1204 (if (eq (char-after) ?{)
0ec8351b 1205 (c-safe (c-forward-sexp -1)))
785eecbb
RS
1206 (looking-at "\\<do\\>[^_]")))
1207 '(before)
1208 '(before after)))))
1209
0386b551
AM
1210(defun c-snug-1line-defun-close (syntax pos)
1211 "Determine the brace hanginess for an AWK defun-close.
1212If the action/function being closed is a one-liner, keep it so. Otherwise put
1213the closing brace on its own line."
1214 (save-excursion
1215 (goto-char pos)
1216 (if (> (c-point 'bol)
1217 (progn (up-list -1) (point)))
1218 '(before after)
1219 '(after))))
1220
785eecbb 1221(defun c-gnu-impose-minimum ()
037558bf 1222 "Imposes a minimum indentation for lines inside code blocks.
785eecbb
RS
1223The variable `c-label-minimum-indentation' specifies the minimum
1224indentation amount."
d9e94c22 1225
037558bf
MS
1226 (when (and (not
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)
1231 (save-excursion
1232 (back-to-indentation)
1233 (< (current-column) c-label-minimum-indentation)))
1234 (c-shift-line-indentation (- c-label-minimum-indentation
1235 (current-indentation)))))
785eecbb
RS
1236
1237\f
1238;; Useful for c-hanging-semi&comma-criteria
51f606de 1239
785eecbb 1240(defun c-semi&comma-inside-parenlist ()
eae86618 1241 "Controls newline insertion after semicolons in parenthesis lists.
785eecbb
RS
1242If a comma was inserted, no determination is made. If a semicolon was
1243inserted inside a parenthesis list, no newline is added otherwise a
1244newline is added. In either case, checking is stopped. This supports
1245exactly 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
1251 (save-excursion
1252 (up-list -1)
1253 (not (eq (char-after) ?\()))
1254 (error t))
1255 t
1256 'stop)))
1257
eae86618
RS
1258;; Suppresses newlines before non-blank lines
1259(defun c-semi&comma-no-newlines-before-nonblanks ()
1260 "Controls newline insertion after semicolons.
1261If a comma was inserted, no determination is made. If a semicolon was
1262inserted, and the following line is not blank, no newline is inserted.
1263Otherwise, no determination is made."
1264 (save-excursion
1265 (if (and (= last-command-char ?\;)
1266 ;;(/= (point-max)
1267 ;; (save-excursion (skip-syntax-forward " ") (point))
1268 (zerop (forward-line 1))
3efc2cd7 1269 (bolp) ; forward-line has funny behavior at eob.
eae86618
RS
1270 (not (looking-at "^[ \t]*$")))
1271 'stop
1272 nil)))
1273
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.
1277If a comma was inserted, no determination is made. Newlines are
1278suppressed in one-liners, if the line is an in-class inline function.
1279For 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)
1283 (c-point 'bol)
1284 -1))))
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))
1289 'stop
1290 nil)))
1291
785eecbb 1292\f
130c507e 1293(cc-provide 'cc-align)
3afbc435 1294
cbee283d 1295;; arch-tag: 4d71ed28-bf51-4509-a148-f39669669a2e
785eecbb 1296;;; cc-align.el ends here