]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-align.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / progmodes / cc-align.el
1 ;;; cc-align.el --- custom indentation functions for CC Mode
2
3 ;; Copyright (C) 1985, 1987, 1992-2011
4 ;; Free Software Foundation, Inc.
5
6 ;; Authors: 2004- Alan Mackenzie
7 ;; 1998- Martin Stjernholm
8 ;; 1992-1999 Barry A. Warsaw
9 ;; 1987 Dave Detlefs
10 ;; 1987 Stewart Clamen
11 ;; 1985 Richard M. Stallman
12 ;; Maintainer: bug-cc-mode@gnu.org
13 ;; Created: 22-Apr-1997 (split from cc-mode.el)
14 ;; Keywords: c languages
15 ;; Package: cc-mode
16
17 ;; This file is part of GNU Emacs.
18
19 ;; GNU Emacs is free software: you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation, either version 3 of the License, or
22 ;; (at your option) any later version.
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
30 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31
32 ;;; Commentary:
33
34 ;;; Code:
35
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)))
43
44 (cc-require 'cc-defs)
45 (cc-require 'cc-vars)
46 (cc-require 'cc-engine)
47
48 \f
49 ;; Standard line-up functions
50 ;;
51 ;; See the section "Custom Indentation Functions" in the manual for
52 ;; details on the calling convention.
53
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:
58
59 int
60 neg (int i) <- c-lineup-topmost-intro-cont
61 {
62 return -i;
63 }
64
65 struct
66 larch <- 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
74 struct larch
75 the_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
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.)
82
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)))
91
92 (defun c-lineup-gnu-DEFUN-intro-cont (langelem)
93 "Line up the continuation lines of a DEFUN macro in the Emacs C source.
94 These lines are indented as though they were `knr-argdecl-intro' lines.
95 Return nil when we're not in such a construct.
96
97 This function is for historical compatibility with how previous CC Modes (5.28
98 and earlier) indented such lines.
99
100 Here is an example:
101
102 DEFUN (\"forward-char\", Fforward_char, Sforward_char, 0, 1, \"p\",
103 doc: /* Move point right N characters (left if N is negative).
104 On reaching end of buffer, stop and signal error. */)
105 (n) <- c-lineup-gnu-DEFUN-into-cont
106 Lisp_Object n; <- c-lineup-gnu-DEFUN-into-cont
107
108 Works 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
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
163 (defun c-lineup-arglist (langelem)
164 "Line up the current argument line under the first argument.
165
166 As a special case, if the indented line is inside a brace block
167 construct, the indentation is `c-basic-offset' only. This is intended
168 as a \"DWIM\" measure in cases like macros that contains statement
169 blocks, e.g:
170
171 A_VERY_LONG_MACRO_NAME ({
172 some (code, with + long, lines * in[it]);
173 });
174 <--> c-basic-offset
175
176 This is motivated partly because it's more in line with how code
177 blocks are handled, and partly since it approximates the behavior of
178 earlier CC Mode versions, which due to inaccurate analysis tended to
179 indent such cases this way.
180
181 Works with: arglist-cont-nonempty, arglist-close."
182 (save-excursion
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.
187
188 ;; Normal case. Indent to the token after the arglist open paren.
189 (goto-char (c-langelem-2nd-pos c-syntactic-element))
190 (if (and c-special-brace-lists
191 (c-looking-at-special-brace-list))
192 ;; Skip a special brace list opener like "({".
193 (progn (c-forward-token-2)
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)))))))
202
203 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
204 (defun c-lineup-argcont (elem)
205 "Line up a continued argument.
206
207 foo (xyz, aaa + bbb + ccc
208 + ddd + eee + fff); <- c-lineup-argcont
209
210 Only continuation lines like this are touched, nil is returned on lines
211 which are the start of an argument.
212
213 Within a gcc asm block, \":\" is recognized as an argument separator,
214 but of course only between operand specifications, not in the expressions
215 for the operands.
216
217 Works with: arglist-cont, arglist-cont-nonempty."
218
219 (save-excursion
220 (beginning-of-line)
221
222 (when (eq (car elem) 'arglist-cont-nonempty)
223 ;; Our argument list might not be the innermost one. If it
224 ;; isn't, go back to the last position in it. We do this by
225 ;; stepping back over open parens until we get to the open paren
226 ;; of our argument list.
227 (let ((open-paren (c-langelem-2nd-pos c-syntactic-element))
228 (paren-state (c-parse-state)))
229 (while (not (eq (car paren-state) open-paren))
230 (unless (consp (car paren-state)) ;; ignore matched braces
231 (goto-char (car paren-state)))
232 (setq paren-state (cdr paren-state)))))
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))))))))
258
259 (defun c-lineup-argcont-scan (&optional other-match)
260 ;; Find the start of an argument, for `c-lineup-argcont'.
261 (when (zerop (c-backward-token-2 1 t))
262 (let ((c (char-after)))
263 (if (or (eq c ?,) (eq c other-match))
264 (progn
265 (forward-char)
266 (c-forward-syntactic-ws))
267 (c-lineup-argcont-scan other-match)))))
268
269 (defun c-lineup-arglist-intro-after-paren (langelem)
270 "Line up a line to just after the open paren of the surrounding paren
271 or brace block.
272
273 Works with: defun-block-intro, brace-list-intro,
274 statement-block-intro, statement-case-intro, arglist-intro."
275 (save-excursion
276 (beginning-of-line)
277 (backward-up-list 1)
278 (skip-chars-forward " \t" (c-point 'eol))
279 (vector (1+ (current-column)))))
280
281 (defun c-lineup-arglist-close-under-paren (langelem)
282 "Line up a line under the enclosing open paren.
283 Normally used to line up a closing paren in the same column as its
284 corresponding open paren, but can also be used with arglist-cont and
285 arglist-cont-nonempty to line up all lines inside a parenthesis under
286 the open paren.
287
288 As a special case, if a brace block construct starts at the same line
289 as the open parenthesis of the argument list, the indentation is
290 `c-basic-offset' only. See `c-lineup-arglist' for further discussion
291 of this \"DWIM\" measure.
292
293 Works with: Almost all symbols, but are typically most useful on
294 arglist-close, brace-list-close, arglist-cont and arglist-cont-nonempty."
295 (save-excursion
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))))
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.
316 Return nil on lines that don't start with an operator, to leave those
317 cases to other line-up functions. Example:
318
319 if ( x < 10
320 || at_limit (x, <- c-lineup-arglist-operators
321 list) <- c-lineup-arglist-operators returns nil
322 )
323
324 Since this function doesn't do anything for lines without an infix
325 operator you typically want to use it together with some other line-up
326 settings, e.g. as follows \(the arglist-close setting is just a
327 suggestion to get a consistent style):
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
334 Works with: arglist-cont, arglist-cont-nonempty."
335 (save-excursion
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))))
340
341 (defun c-lineup-close-paren (langelem)
342 "Line up the closing paren under its corresponding open paren if the
343 open paren is followed by code. If the open paren ends its line, no
344 indentation is added. E.g:
345
346 main (int, main (
347 char ** int, char **
348 ) <-> ) <- c-lineup-close-paren
349
350 As a special case, if a brace block construct starts at the same line
351 as the open parenthesis of the argument list, the indentation is
352 `c-basic-offset' instead of the open paren column. See
353 `c-lineup-arglist' for further discussion of this \"DWIM\" measure.
354
355 Works with: All *-close symbols."
356 (save-excursion
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))
362
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))
374
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)))))))
385
386 (defun c-lineup-streamop (langelem)
387 "Line up C++ stream operators under each other.
388
389 Works with: stream-op."
390 (save-excursion
391 (goto-char (c-langelem-pos langelem))
392 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
393 (goto-char (match-beginning 0))
394 (vector (current-column))))
395
396 (defun c-lineup-multi-inher (langelem)
397 "Line up the classes in C++ multiple inheritance clauses and member
398 initializers under each other. E.g:
399
400 class Foo: Foo::Foo (int a, int b):
401 public Cyphr, Cyphr (a),
402 public Bar <-> Bar (b) <- c-lineup-multi-inher
403
404 class Foo Foo::Foo (int a, int b)
405 : public Cyphr, : Cyphr (a),
406 public Bar <-> Bar (b) <- c-lineup-multi-inher
407
408 class Foo Foo::Foo (int a, int b)
409 : public Cyphr : Cyphr (a)
410 , public Bar <-> , Bar (b) <- c-lineup-multi-inher
411
412 Works with: inher-cont, member-init-cont."
413 (save-excursion
414 (back-to-indentation)
415 (let* ((eol (c-point 'eol))
416 (here (point))
417 (char-after-ip (char-after)))
418 (if (c-langelem-pos langelem)
419 (goto-char (c-langelem-pos langelem)))
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
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)))
434 (if (< (point) here)
435 (vector (current-column)))
436 )))
437
438 (defun c-lineup-java-inher (langelem)
439 "Line up Java implements and extends declarations.
440 If class names follow on the same line as the implements/extends
441 keyword, they are lined up under each other. Otherwise, they are
442 indented by adding `c-basic-offset' to the column of the keyword.
443 E.g:
444
445 class Foo class Foo
446 extends extends Cyphr,
447 Bar <-> Bar <- c-lineup-java-inher
448 <--> c-basic-offset
449
450 Works with: inher-cont."
451 (save-excursion
452 (goto-char (c-langelem-pos langelem))
453 (forward-word 1)
454 (if (looking-at "[ \t]*$")
455 c-basic-offset
456 (c-forward-syntactic-ws)
457 (vector (current-column)))))
458
459 (defun c-lineup-java-throws (langelem)
460 "Line up Java throws declarations.
461 If exception names follow on the same line as the throws keyword,
462 they are lined up under each other. Otherwise, they are indented by
463 adding `c-basic-offset' to the column of the throws keyword. The
464 throws keyword itself is also indented by `c-basic-offset' from the
465 function declaration start if it doesn't hang. E.g:
466
467 int foo() int foo() throws Cyphr,
468 throws <-> Bar, <- c-lineup-java-throws
469 Bar <-> Vlod <- c-lineup-java-throws
470 <--><--> c-basic-offset
471
472 Works with: func-decl-cont."
473 (save-excursion
474 (let* ((lim (1- (c-point 'bol)))
475 (throws (catch 'done
476 (goto-char (c-langelem-pos langelem))
477 (while (zerop (c-forward-token-2 1 t lim))
478 (if (looking-at "throws\\>[^_]")
479 (throw 'done t))))))
480 (if throws
481 (if (zerop (c-forward-token-2 1 nil (c-point 'eol)))
482 (vector (current-column))
483 (back-to-indentation)
484 (vector (+ (current-column) c-basic-offset)))
485 c-basic-offset))))
486
487 (defun c-indent-one-line-block (langelem)
488 "Indent a one line block `c-basic-offset' extra.
489 E.g:
490
491 if (n > 0) if (n > 0)
492 {m+=n; n=0;} <-> { <- c-indent-one-line-block
493 <--> c-basic-offset m+=n; n=0;
494 }
495
496 The block may use any kind of parenthesis character. nil is returned
497 if the line doesn't start with a one line block, which makes the
498 function usable in list expressions.
499
500 Work with: Almost all syntactic symbols, but most useful on *-open."
501 (save-excursion
502 (let ((eol (c-point 'eol)))
503 (back-to-indentation)
504 (if (and (eq (char-syntax (char-after)) ?\()
505 (c-safe (progn (c-forward-sexp) t))
506 (<= (point) eol))
507 c-basic-offset
508 nil))))
509
510 (defun c-indent-multi-line-block (langelem)
511 "Indent a multi line block `c-basic-offset' extra.
512 E.g:
513
514 int *foo[] = { int *foo[] = {
515 NULL, NULL,
516 {17}, <-> { <- c-indent-multi-line-block
517 17
518 }
519 <--> c-basic-offset
520
521 The block may use any kind of parenthesis character. nil is returned
522 if the line doesn't start with a multi line block, which makes the
523 function usable in list expressions.
524
525 Work with: Almost all syntactic symbols, but most useful on *-open."
526 (save-excursion
527 (let ((eol (c-point 'eol)))
528 (back-to-indentation)
529 (if (and (eq (char-syntax (char-after)) ?\()
530 (or (not (c-safe (progn (c-forward-sexp) t)))
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.
537 Various heuristics are used to handle many of the common comment
538 styles. 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
556 The variable `c-comment-prefix-regexp' is used to recognize the
557 comment line prefix, e.g. the `*' that usually starts every line
558 inside a comment.
559
560 Works with: The `c' syntactic symbol."
561 (save-excursion
562 (let* ((here (point))
563 (prefixlen (progn (back-to-indentation)
564 (if (looking-at c-current-comment-prefix)
565 (- (match-end 0) (point))
566 0)))
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
574 (goto-char (1+ (c-langelem-pos langelem)))
575 (if (and (match-string 0)
576 (looking-at (regexp-quote (match-string 0))))
577 (- (match-end 0) (match-beginning 0))
578 0))
579 (save-excursion
580 (goto-char (c-langelem-pos langelem))
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)))))
589 (if (and (> starterlen 10) (zerop prefixlen))
590 ;; The comment has a long starter and the line doesn't have
591 ;; a nonempty comment prefix. Treat it as free form text
592 ;; and don't change the indentation.
593 (vector (current-column))
594 ;; Go back to the previous non-blank line, if any.
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))
603 (if (zerop prefixlen)
604 ;; No nonempty comment prefix. Align after comment
605 ;; starter.
606 (progn
607 (looking-at comment-start-skip)
608 (goto-char (match-end 0))
609 ;; The following should not be necessary, since
610 ;; comment-start-skip should match everything (i.e.
611 ;; typically whitespace) that leads up to the text.
612 ;;(if (looking-at "\\([ \t]+\\).+$")
613 ;; ;; Align with the text that hangs after the
614 ;; ;; comment starter.
615 ;; (goto-char (match-end 1)))
616 (vector (current-column)))
617 ;; How long is the comment starter? if greater than the
618 ;; length of the comment prefix, align left. if less
619 ;; than or equal, align right. this should also pick up
620 ;; Javadoc style comments.
621 (if (> starterlen prefixlen)
622 (progn
623 (goto-char (c-langelem-pos langelem))
624 (vector (1+ (current-column))))
625 (goto-char (+ (c-langelem-pos langelem) starterlen 1))
626 (vector (- (current-column) prefixlen))))
627 ;; We didn't start on the first non-blank continuation line. If the
628 ;; previous line has a nonempty comment prefix, align with it.
629 ;; Otherwise, align with the previous nonempty line, but align the
630 ;; comment ender with the starter.
631 (when (or (not (looking-at c-current-comment-prefix))
632 (eq (match-beginning 0) (match-end 0)))
633 (goto-char here)
634 (back-to-indentation)
635 (if (looking-at (concat "\\(" c-current-comment-prefix "\\)\\*/"))
636 (goto-char (c-langelem-pos langelem))
637 (while (and (zerop (forward-line -1))
638 (looking-at "^[ \t]*$")))
639 (back-to-indentation)
640 (if (< (point) (c-langelem-pos langelem))
641 ;; Align with the comment starter rather than
642 ;; with the code before it.
643 (goto-char (c-langelem-pos langelem)))))
644 (vector (current-column)))))))
645
646 (defun c-lineup-comment (langelem)
647 "Line up a comment start according to `c-comment-only-line-offset'.
648 If the comment is lined up with a comment starter on the previous
649 line, that alignment is preserved.
650
651 Works with: comment-intro."
652 (save-excursion
653 (back-to-indentation)
654 (let ((col (current-column)))
655 (cond
656 ;; CASE 1: preserve aligned comments
657 ((save-excursion
658 (and (c-backward-single-comment)
659 (= col (current-column))))
660 (vector col)) ; Return an absolute column.
661 ;; indent as specified by c-comment-only-line-offset
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
671 (defun c-lineup-knr-region-comment (langelem)
672 "Line up a comment in the \"K&R region\" with the declaration.
673 That is the region between the function or class header and the
674 beginning of the block. E.g:
675
676 int main()
677 /* This is the main function. */ <- c-lineup-knr-region-comment
678 {
679 return 0;
680 }
681
682 Return nil if called in any other situation, to be useful in list
683 expressions.
684
685 Works with: comment-intro."
686 (when (or (assq 'topmost-intro-cont c-syntactic-context)
687 (assq 'func-decl-cont c-syntactic-context)
688 (assq 'knr-argdecl-intro c-syntactic-context)
689 (assq 'lambda-intro-cont c-syntactic-context))
690 (save-excursion
691 (beginning-of-line)
692 (c-beginning-of-statement-1)
693 (vector (current-column)))))
694
695 (defun c-lineup-runin-statements (langelem)
696 "Line up statements when the first statement is on the same line as
697 the block opening brace. E.g:
698
699 int main()
700 { puts (\"Hello world!\");
701 return 0; <- c-lineup-runin-statements
702 }
703
704 If there is no statement after the opening brace to align with, nil is
705 returned. This makes the function usable in list expressions.
706
707 Works with: The `statement' syntactic symbol."
708 (if (eq (char-after (c-langelem-pos langelem)) ?{)
709 (save-excursion
710 (if (c-langelem-pos langelem)
711 (goto-char (c-langelem-pos langelem)))
712 (forward-char 1)
713 (skip-chars-forward " \t")
714 (unless (eolp)
715 (vector (current-column))))))
716
717 (defun c-lineup-assignments (langelem)
718 "Line up the current line after the assignment operator on the first
719 line in the statement. If there isn't any, return nil to allow
720 stacking with other line-up functions. If the current line contains
721 an assignment operator too, try to align it with the first one.
722
723 Works with: topmost-intro-cont, statement-cont, arglist-cont,
724 arglist-cont-nonempty."
725 (let (startpos endpos equalp)
726
727 (if (eq (c-langelem-sym langelem) 'arglist-cont-nonempty)
728 ;; If it's an arglist-cont-nonempty then we're only interested
729 ;; in equal signs outside it. We don't search for a "=" on
730 ;; the current line since that'd have a different nesting
731 ;; compared to the one we should align with.
732 (save-excursion
733 (save-restriction
734 (setq endpos (c-langelem-2nd-pos c-syntactic-element))
735 (narrow-to-region (c-langelem-pos langelem) endpos)
736 (if (setq startpos (c-up-list-backward endpos))
737 (setq startpos (1+ startpos))
738 (setq startpos (c-langelem-pos langelem)))))
739
740 (setq startpos (c-langelem-pos langelem)
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
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))))))
755
756 (save-excursion
757 (goto-char startpos)
758 (if (or (if (c-syntactic-re-search-forward
759 c-assignment-op-regexp
760 (min endpos (c-point 'eol)) t t t)
761 (progn
762 (goto-char (or (match-beginning 1)
763 (match-end 0)))
764 nil)
765 t)
766 (save-excursion
767 (c-forward-syntactic-ws (c-point 'eol))
768 (eolp)))
769 ;; There's no equal sign on the line, or there is one but
770 ;; nothing follows it.
771 nil
772
773 ;; calculate indentation column after equals and ws, unless
774 ;; our line contains an equals sign
775 (if (not equalp)
776 (progn
777 (skip-chars-forward " \t")
778 (setq equalp 0)))
779
780 (vector (- (current-column) equalp)))
781 )))
782
783 (defun c-lineup-math (langelem)
784 "Like `c-lineup-assignments' but indent with `c-basic-offset' if no
785 assignment operator was found on the first line. I.e. this function
786 is the same as specifying a list (c-lineup-assignments +). It's
787 provided for compatibility with old configurations.
788
789 Works with: topmost-intro-cont, statement-cont, arglist-cont,
790 arglist-cont-nonempty."
791 (or (c-lineup-assignments langelem)
792 c-basic-offset))
793
794 (defun c-lineup-cascaded-calls (langelem)
795 "Line up \"cascaded calls\" under each other.
796 If the line begins with \"->\" or \".\" and the preceding line ends
797 with one or more function calls preceded by the same token, then the
798 arrow is lined up with the first of those tokens. E.g:
799
800 result = proc->add(17)->add(18)
801 ->add(19) + <- c-lineup-cascaded-calls
802 offset; <- c-lineup-cascaded-calls (inactive)
803
804 In any other situation nil is returned to allow use in list
805 expressions.
806
807 Works with: topmost-intro-cont, statement-cont, arglist-cont,
808 arglist-cont-nonempty."
809
810 (if (and (eq (c-langelem-sym langelem) 'arglist-cont-nonempty)
811 (not (eq (c-langelem-2nd-pos c-syntactic-element)
812 (c-most-enclosing-brace (c-parse-state)))))
813 ;; The innermost open paren is not our one, so don't do
814 ;; anything. This can occur for arglist-cont-nonempty with
815 ;; nested arglist starts on the same line.
816 nil
817
818 (save-excursion
819 (back-to-indentation)
820 (let ((operator (and (looking-at "->\\|\\.")
821 (regexp-quote (match-string 0))))
822 (stmt-start (c-langelem-pos langelem)) col)
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.
842 A continued string in this sense is where a string literal follows
843 directly after another one. E.g:
844
845 result = prefix + \"A message \"
846 \"string.\"; <- c-lineup-string-cont
847
848 In other situations, returns nil, to allow stacking with other
849 line-up functions.
850
851 Works with: topmost-intro-cont, statement-cont, arglist-cont,
852 arglist-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)))))))
864
865 (defun c-lineup-template-args (langelem)
866 "Line up template argument lines under the first argument.
867 To allow this function to be used in a list expression, nil is
868 returned if there's no template argument on the first line.
869
870 Works 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) ?<)
876 (zerop (c-forward-token-2 1 nil (c-point 'eol))))
877 (vector (current-column))))))
878
879 (defun c-lineup-ObjC-method-call (langelem)
880 "Line up selector args as Emacs Lisp mode does with function args:
881 Go to the position right after the message receiver, and if you are at
882 the end of the line, indent the current line c-basic-offset columns
883 from the opening bracket; otherwise you are looking at the first
884 character of the first method call argument, so line up the current
885 line with it.
886
887 Works with: objc-method-call-cont."
888 (save-excursion
889 (let* ((extra (save-excursion
890 (back-to-indentation)
891 (c-backward-syntactic-ws (c-langelem-pos langelem))
892 (if (eq (char-before) ?:)
893 (- c-basic-offset)
894 0)))
895 (open-bracket-pos (c-langelem-pos langelem))
896 (open-bracket-col (progn
897 (goto-char open-bracket-pos)
898 (current-column)))
899 (target-col (progn
900 (forward-char)
901 (c-forward-sexp)
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-call-colons (langelem)
910 "Line up selector args as Project Builder / XCode: colons of first
911 selector portions on successive lines are aligned. If no decision can
912 be made return NIL, so that other lineup methods can be tried. This is
913 typically chained with `c-lineup-ObjC-method-call'.
914
915 Works with: objc-method-call-cont."
916 (save-excursion
917 (catch 'no-idea
918 (let* ((method-arg-len (progn
919 (back-to-indentation)
920 (if (search-forward ":" (c-point 'eol) 'move)
921 (- (point) (c-point 'boi))
922 ; no complete argument to indent yet
923 (throw 'no-idea nil))))
924
925 (extra (save-excursion
926 ; indent parameter to argument if needed
927 (back-to-indentation)
928 (c-backward-syntactic-ws (c-langelem-pos langelem))
929 (if (eq ?: (char-before))
930 c-objc-method-parameter-offset 0)))
931
932 (open-bracket-col (c-langelem-col langelem))
933
934 (arg-ralign-colon-ofs (progn
935 (forward-char) ; skip over '['
936 ; skip over object/class name
937 ; and first argument
938 (c-forward-sexp 2)
939 (if (search-forward ":" (c-point 'eol) 'move)
940 (- (current-column) open-bracket-col
941 method-arg-len extra)
942 ; previous arg has no param
943 c-objc-method-arg-unfinished-offset))))
944
945 (if (>= arg-ralign-colon-ofs c-objc-method-arg-min-delta-to-bracket)
946 (+ arg-ralign-colon-ofs extra)
947 (throw 'no-idea nil))))))
948
949 (defun c-lineup-ObjC-method-args (langelem)
950 "Line up the colons that separate args in a method declaration.
951 The colon on the current line is aligned with the one on the first
952 line.
953
954 Works with: objc-method-args-cont."
955 (save-excursion
956 (let* ((here (c-point 'boi))
957 (curcol (progn (goto-char here) (current-column)))
958 (eol (c-point 'eol))
959 (relpos (c-langelem-pos langelem))
960 (first-col-column (progn
961 (goto-char relpos)
962 (skip-chars-forward "^:" eol)
963 (and (eq (char-after) ?:)
964 (current-column)))))
965 (if (not first-col-column)
966 c-basic-offset
967 (goto-char here)
968 (skip-chars-forward "^:" eol)
969 (if (eq (char-after) ?:)
970 (+ curcol (- first-col-column (current-column)))
971 c-basic-offset)))))
972
973 (defun c-lineup-ObjC-method-args-2 (langelem)
974 "Line up the colons that separate args in a method declaration.
975 The colon on the current line is aligned with the one on the previous
976 line.
977
978 Works with: objc-method-args-cont."
979 (save-excursion
980 (let* ((here (c-point 'boi))
981 (curcol (progn (goto-char here) (current-column)))
982 (eol (c-point 'eol))
983 (relpos (c-langelem-pos langelem))
984 (prev-col-column (progn
985 (skip-chars-backward "^:" relpos)
986 (and (eq (char-before) ?:)
987 (- (current-column) 1)))))
988 (if (not prev-col-column)
989 c-basic-offset
990 (goto-char here)
991 (skip-chars-forward "^:" eol)
992 (if (eq (char-after) ?:)
993 (+ curcol (- prev-col-column (current-column)))
994 c-basic-offset)))))
995
996 (defun c-lineup-inexpr-block (langelem)
997 "Line up the block for constructs that use a block inside an expression,
998 e.g. anonymous classes in Java and lambda functions in Pike. The body
999 is aligned with the start of the header, e.g. with the \"new\" or
1000 \"lambda\" keyword. Returns nil if the block isn't part of such a
1001 construct.
1002
1003 Works with: inlambda, inexpr-statement, inexpr-class."
1004 (save-excursion
1005 (back-to-indentation)
1006 (let* ((paren-state (c-parse-state))
1007 (containing-sexp (c-most-enclosing-brace paren-state))
1008 (res (or (c-looking-at-inexpr-block
1009 (c-safe-position containing-sexp paren-state)
1010 containing-sexp)
1011 (and containing-sexp
1012 (progn (goto-char containing-sexp)
1013 (eq (char-after) ?{))
1014 (progn (setq containing-sexp
1015 (c-most-enclosing-brace paren-state
1016 (point)))
1017 (c-looking-at-inexpr-block
1018 (c-safe-position containing-sexp paren-state)
1019 containing-sexp))))))
1020 (when res
1021 (goto-char (cdr res))
1022 (vector (current-column))))))
1023
1024 (defun c-lineup-whitesmith-in-block (langelem)
1025 "Line up lines inside a block in Whitesmith style.
1026 It's done in a way that works both when the opening brace hangs and
1027 when it doesn't. E.g:
1028
1029 something
1030 { something {
1031 foo; <-> foo; <- c-lineup-whitesmith-in-block
1032 } }
1033 <--> c-basic-offset
1034
1035 In the first case the indentation is kept unchanged, in the
1036 second `c-basic-offset' is added.
1037
1038 Works with: defun-close, defun-block-intro, inline-close, block-close,
1039 brace-list-close, brace-list-intro, statement-block-intro,
1040 arglist-intro, arglist-cont-nonempty, arglist-close, and all in*
1041 symbols, e.g. inclass and inextern-lang."
1042 (save-excursion
1043 (if (and (c-go-up-list-backward)
1044 (= (point) (c-point 'boi)))
1045 nil
1046 c-basic-offset)))
1047
1048 (defun c-lineup-after-whitesmith-blocks (langelem)
1049 "Compensate for Whitesmith style indentation of blocks.
1050 Due to the way CC Mode calculates anchor positions for normal lines
1051 inside blocks, this function is necessary for those lines to get
1052 correct Whitesmith style indentation. Consider the following
1053 examples:
1054
1055 int foo()
1056 {
1057 int foo() {
1058 { a;
1059 a; }
1060 x; <-> x; <- c-lineup-after-whitesmith-blocks
1061
1062 The fact that the line with \"x\" is preceded by a Whitesmith style
1063 indented block in one case and not the other should not affect its
1064 indentation. But since CC Mode in cases like this uses the
1065 indentation of the preceding statement as anchor position, the \"x\"
1066 would in the rightmost case be indented too much if the offset for
1067 `statement' was set simply to zero.
1068
1069 This lineup function corrects for this situation by detecting if the
1070 anchor position is at an open paren character. In that case, it
1071 instead indents relative to the surrounding block just like
1072 `c-lineup-whitesmith-in-block'.
1073
1074 Works with: brace-list-entry, brace-entry-open, statement,
1075 arglist-cont."
1076 (save-excursion
1077 (goto-char (c-langelem-pos langelem))
1078 (when (looking-at "\\s\(")
1079 (if (c-go-up-list-backward)
1080 (let ((pos (point)))
1081 (back-to-indentation)
1082 (if (= pos (point))
1083 (vector (current-column))
1084 (vector (+ (current-column) c-basic-offset))))
1085 (vector 0)))))
1086
1087 (defun c-lineup-cpp-define (langelem)
1088 "Line up macro continuation lines according to the indentation of
1089 the construct preceding the macro. E.g:
1090
1091 v beg of preceding constr v beg of preceding constr
1092 int dribble() {
1093 const char msg[] = if (!running)
1094 \"Some text.\"; error(\"Not running!\");
1095
1096 #define X(A, B) \ #define X(A, B) \
1097 do { \ <-> do { \ <- c-lineup-cpp-define
1098 printf (A, B); \ printf (A, B); \
1099 } while (0) } while (0)
1100
1101 If `c-syntactic-indentation-in-macros' is non-nil, the function
1102 returns the relative indentation to the macro start line to allow
1103 accumulation with other offsets. E.g. in the following cases,
1104 cpp-define-intro is combined with the statement-block-intro that comes
1105 from the \"do {\" that hangs on the \"#define\" line:
1106
1107 int dribble() {
1108 const char msg[] = if (!running)
1109 \"Some text.\"; error(\"Not running!\");
1110
1111 #define X(A, B) do { \ #define X(A, B) do { \
1112 printf (A, B); \ <-> printf (A, B); \ <- c-lineup-cpp-define
1113 this->refs++; \ this->refs++; \
1114 } while (0) <-> } while (0) <- c-lineup-cpp-define
1115
1116 The relative indentation returned by `c-lineup-cpp-define' is zero and
1117 two, respectively, in these two examples. They are then added to the
1118 two column indentation that statement-block-intro gives in both cases
1119 here.
1120
1121 If the relative indentation is zero, then nil is returned instead.
1122 That is useful in a list expression to specify the default indentation
1123 on the top level.
1124
1125 If `c-syntactic-indentation-in-macros' is nil then this function keeps
1126 the current indentation, except for empty lines \(ignoring the ending
1127 backslash) where it takes the indentation from the closest preceding
1128 nonempty line in the macro. If there's no such line in the macro then
1129 the indentation is taken from the construct preceding it, as described
1130 above.
1131
1132 Works with: cpp-define-intro."
1133 (let (offset)
1134 (if c-syntactic-indentation-in-macros
1135 ;; Go to the macro start and do a syntactic analysis of it.
1136 ;; Then remove the cpp-macro element it should contain and
1137 ;; calculate the indentation it then would get.
1138 (save-excursion
1139 (c-beginning-of-macro)
1140 (setq offset (- (c-get-syntactic-indentation
1141 (delete '(cpp-macro) (c-guess-basic-syntax)))
1142 (save-excursion
1143 (back-to-indentation)
1144 (current-column))))
1145 (if (zerop offset)
1146 nil
1147 offset))
1148 ;; Do not indent syntactically inside the macro.
1149 (save-excursion
1150 (let ((macro-start-line (save-excursion
1151 (goto-char (c-query-macro-start))
1152 (beginning-of-line)
1153 (point))))
1154 (beginning-of-line)
1155 ;; Check every line while inside the macro.
1156 (while (and (> (point) macro-start-line)
1157 (looking-at "[ \t]*\\\\?$")
1158 (= (forward-line -1) 0)))
1159 (if (<= (point) macro-start-line)
1160 ;; If we've stepped out of the macro we take the
1161 ;; syntactic offset.
1162 (setq offset (c-get-syntactic-indentation
1163 (delete '(cpp-macro) (c-guess-basic-syntax))))
1164 (setq offset (current-indentation)))
1165 (if (zerop offset)
1166 nil
1167 (vector offset)))))))
1168
1169 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
1170 (defun c-lineup-gcc-asm-reg (elem)
1171 "Line up a gcc asm register under one on a previous line.
1172
1173 asm (\"foo %1, %0\\n\"
1174 \"bar %0, %1\"
1175 : \"=r\" (w),
1176 \"=r\" (x)
1177 : \"0\" (y),
1178 \"1\" (z));
1179
1180 The \"x\" line is aligned to the text after the \":\" on the \"w\" line, and
1181 similarly \"z\" under \"y\".
1182
1183 This is done only in an \"asm\" or \"__asm__\" block, and only to
1184 those lines mentioned. Anywhere else nil is returned. The usual
1185 arrangement is to have this routine as an extra feature at the start
1186 of arglist line-ups, e.g.
1187
1188 (c-lineup-gcc-asm-reg c-lineup-arglist)
1189
1190 Works with: arglist-cont, arglist-cont-nonempty."
1191
1192 (let ((orig-pos (point))
1193 alignto)
1194 (save-excursion
1195 (and
1196 c-opt-asm-stmt-key
1197
1198 ;; Don't do anything if the innermost open paren isn't our one.
1199 ;; This can occur for arglist-cont-nonempty with nested arglist
1200 ;; starts on the same line.
1201 (or (not (eq (car elem) 'arglist-cont-nonempty))
1202 (eq (c-langelem-2nd-pos c-syntactic-element)
1203 (c-most-enclosing-brace (c-parse-state))))
1204
1205 ;; Find the ":" to align to. Look for this first so as to quickly
1206 ;; eliminate pretty much all cases which are not for us.
1207 (re-search-backward "^[ \t]*:[ \t]*\\(.\\)?" (cdr elem) t)
1208
1209 ;; Must have something after the ":".
1210 (setq alignto (match-beginning 1))
1211
1212 ;; Don't touch ":" lines themselves.
1213 (progn (goto-char orig-pos)
1214 (beginning-of-line)
1215 (not (looking-at "^[ \t]*:")))
1216
1217 ;; Only operate in an asm statement.
1218 (progn (goto-char orig-pos)
1219 (c-in-gcc-asm-p))
1220
1221 (vector (progn (goto-char alignto) (current-column)))))))
1222
1223 (defun c-lineup-dont-change (langelem)
1224 "Do not change the indentation of the current line.
1225
1226 Works with: Any syntactic symbol."
1227 (save-excursion
1228 (back-to-indentation)
1229 (vector (current-column))))
1230
1231 \f
1232 (defun c-snug-do-while (syntax pos)
1233 "Dynamically calculate brace hanginess for do-while statements.
1234 Using this function, `while' clauses that end a `do-while' block will
1235 remain on the same line as the brace that closes that block.
1236
1237 See `c-hanging-braces-alist' for how to utilize this function as an
1238 ACTION associated with `block-close' syntax."
1239 (save-excursion
1240 (let (langelem)
1241 (if (and (eq syntax 'block-close)
1242 (setq langelem (assq 'block-close c-syntactic-context))
1243 (progn (goto-char (c-langelem-pos langelem))
1244 (if (eq (char-after) ?{)
1245 (c-safe (c-forward-sexp -1)))
1246 (looking-at "\\<do\\>[^_]")))
1247 '(before)
1248 '(before after)))))
1249
1250 (defun c-snug-1line-defun-close (syntax pos)
1251 "Determine the brace hanginess for an AWK defun-close.
1252 If the action/function being closed is a one-liner, keep it so. Otherwise put
1253 the closing brace on its own line."
1254 (save-excursion
1255 (goto-char pos)
1256 (if (> (c-point 'bol)
1257 (progn (up-list -1) (point)))
1258 '(before after)
1259 '(after))))
1260
1261 (defun c-gnu-impose-minimum ()
1262 "Imposes a minimum indentation for lines inside code blocks.
1263 The variable `c-label-minimum-indentation' specifies the minimum
1264 indentation amount."
1265
1266 (when (and (not
1267 ;; Don't adjust macro or comment-only lines.
1268 (or (assq 'cpp-macro c-syntactic-context)
1269 (assq 'comment-intro c-syntactic-context)))
1270 (c-intersect-lists c-inside-block-syms c-syntactic-context)
1271 (save-excursion
1272 (back-to-indentation)
1273 (< (current-column) c-label-minimum-indentation)))
1274 (c-shift-line-indentation (- c-label-minimum-indentation
1275 (current-indentation)))))
1276
1277 \f
1278 ;; Useful for c-hanging-semi&comma-criteria
1279
1280 (defun c-semi&comma-inside-parenlist ()
1281 "Controls newline insertion after semicolons in parenthesis lists.
1282 If a comma was inserted, no determination is made. If a semicolon was
1283 inserted inside a parenthesis list, no newline is added otherwise a
1284 newline is added. In either case, checking is stopped. This supports
1285 exactly the old newline insertion behavior."
1286 ;; newline only after semicolon, but only if that semicolon is not
1287 ;; inside a parenthesis list (e.g. a for loop statement)
1288 (if (not (eq last-command-event ?\;))
1289 nil ; continue checking
1290 (if (condition-case nil
1291 (save-excursion
1292 (up-list -1)
1293 (not (eq (char-after) ?\()))
1294 (error t))
1295 t
1296 'stop)))
1297
1298 ;; Suppresses newlines before non-blank lines
1299 (defun c-semi&comma-no-newlines-before-nonblanks ()
1300 "Controls newline insertion after semicolons.
1301 If a comma was inserted, no determination is made. If a semicolon was
1302 inserted, and the following line is not blank, no newline is inserted.
1303 Otherwise, no determination is made."
1304 (save-excursion
1305 (if (and (= last-command-event ?\;)
1306 ;;(/= (point-max)
1307 ;; (save-excursion (skip-syntax-forward " ") (point))
1308 (zerop (forward-line 1))
1309 (bolp) ; forward-line has funny behavior at eob.
1310 (not (looking-at "^[ \t]*$")))
1311 'stop
1312 nil)))
1313
1314 ;; Suppresses new lines after semicolons in one-liners methods
1315 (defun c-semi&comma-no-newlines-for-oneline-inliners ()
1316 "Controls newline insertion after semicolons for some one-line methods.
1317 If a comma was inserted, no determination is made. Newlines are
1318 suppressed in one-liners, if the line is an in-class inline function.
1319 For other semicolon contexts, no determination is made."
1320 (let ((syntax (c-guess-basic-syntax))
1321 (bol (save-excursion
1322 (if (c-safe (up-list -1) t)
1323 (c-point 'bol)
1324 -1))))
1325 (if (and (eq last-command-event ?\;)
1326 (eq (car (car syntax)) 'inclass)
1327 (eq (car (car (cdr syntax))) 'topmost-intro)
1328 (= (c-point 'bol) bol))
1329 'stop
1330 nil)))
1331
1332 \f
1333 (cc-provide 'cc-align)
1334
1335 ;;; cc-align.el ends here