]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-align.el
(c-lineup-argcont, c-lineup-gcc-asm-reg): Don't quote nil and t in docstrings.
[gnu-emacs] / lisp / progmodes / cc-align.el
1 ;;; cc-align.el --- custom indentation functions for CC Mode
2
3 ;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
4
5 ;; Authors: 2000- Martin Stjernholm
6 ;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
7 ;; 1992-1997 Barry A. Warsaw
8 ;; 1987 Dave Detlefs and Stewart Clamen
9 ;; 1985 Richard M. Stallman
10 ;; Maintainer: bug-cc-mode@gnu.org
11 ;; Created: 22-Apr-1997 (split from cc-mode.el)
12 ;; Version: See cc-mode.el
13 ;; Keywords: c languages oop
14
15 ;; This file is part of GNU Emacs.
16
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; any later version.
21
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA.
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 (require 'cc-bytecomp)))
43
44 (cc-require 'cc-defs)
45 (cc-require 'cc-vars)
46 (cc-require 'cc-langs)
47 (cc-require 'cc-engine)
48
49 \f
50 ;; Standard indentation line-ups
51
52 (defun c-lineup-topmost-intro-cont (langelem)
53 "Line up declaration continuation lines zero or one indentation step.
54 For lines in the \"header\" of a definition, zero is used. For other
55 lines, `c-basic-offset' is added to the indentation. E.g:
56
57 int
58 neg (int i) <- c-lineup-topmost-intro-cont
59 {
60 return -i;
61 }
62
63 struct
64 larch <- c-lineup-topmost-intro-cont
65 {
66 double height;
67 }
68 the_larch, <- c-lineup-topmost-intro-cont
69 another_larch; <- c-lineup-topmost-intro-cont
70 <--> c-basic-offset
71
72 struct larch
73 the_larch, <- c-lineup-topmost-intro-cont
74 another_larch; <- c-lineup-topmost-intro-cont
75
76 \(This function is mainly provided to mimic the behavior of CC Mode
77 5.28 and earlier where this case wasn't handled consistently so that
78 these lines could be analyzed as either topmost-intro-cont or
79 statement-cont.)
80
81 Works with: topmost-intro-cont."
82 (save-excursion
83 (beginning-of-line)
84 (c-backward-syntactic-ws (cdr langelem))
85 (if (memq (char-before) '(?} ?,))
86 c-basic-offset)))
87
88 (defun c-lineup-arglist (langelem)
89 "Line up the current argument line under the first argument.
90
91 Works with: arglist-cont-nonempty, arglist-close."
92 (save-excursion
93 (beginning-of-line)
94 (let ((containing-sexp (c-most-enclosing-brace (c-parse-state))))
95 (goto-char (1+ containing-sexp))
96 (let ((eol (c-point 'eol)))
97 (c-forward-syntactic-ws)
98 (when (< (point) eol)
99 (goto-char (1+ containing-sexp))
100 (skip-chars-forward " \t")))
101 (vector (current-column)))))
102
103 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
104 (defun c-lineup-argcont (elem)
105 "Line up a continued argument.
106
107 foo (xyz, aaa + bbb + ccc
108 + ddd + eee + fff); <- c-lineup-argcont
109
110 Only continuation lines like this are touched, nil is returned on lines
111 which are the start of an argument.
112
113 Within a gcc asm block, \":\" is recognised as an argument separator,
114 but of course only between operand specifications, not in the expressions
115 for the operands.
116
117 Works with: arglist-cont, arglist-cont-nonempty."
118
119 (save-excursion
120 (beginning-of-line)
121 (let ((bol (point)))
122
123 ;; Previous line ending in a comma means we're the start of an
124 ;; argument. This should quickly catch most cases not for us.
125 (c-backward-syntactic-ws)
126 (let ((c (char-before)))
127 (unless (eq c ?,)
128
129 ;; In a gcc asm, ":" on the previous line means the start of an
130 ;; argument. And lines starting with ":" are not for us, don't
131 ;; want them to indent to the preceding operand.
132 (let ((gcc-asm (save-excursion
133 (goto-char bol)
134 (c-in-gcc-asm-p))))
135 (unless (and gcc-asm
136 (or (eq c ?:)
137 (save-excursion
138 (goto-char bol)
139 (looking-at "[ \t]*:"))))
140
141 (c-lineup-argcont-scan (if gcc-asm ?:))
142 (vector (current-column)))))))))
143
144 (defun c-lineup-argcont-scan (&optional other-match)
145 ;; Find the start of an argument, for `c-lineup-argcont'.
146 (when (eq 0 (c-backward-token-1 1 t))
147 (let ((c (char-after)))
148 (if (or (eq c ?,) (eq c other-match))
149 (progn
150 (forward-char)
151 (c-forward-syntactic-ws))
152 (c-lineup-argcont-scan other-match)))))
153
154 (defun c-lineup-arglist-intro-after-paren (langelem)
155 "Line up a line just after the open paren of the surrounding paren or
156 brace block.
157
158 Works with: defun-block-intro, brace-list-intro,
159 statement-block-intro, statement-case-intro, arglist-intro."
160 (save-excursion
161 (beginning-of-line)
162 (backward-up-list 1)
163 (skip-chars-forward " \t" (c-point 'eol))
164 (vector (1+ (current-column)))))
165
166 (defun c-lineup-arglist-close-under-paren (langelem)
167 "Line up a closing paren line under the corresponding open paren.
168
169 Works with: defun-close, class-close, inline-close, block-close,
170 brace-list-close, arglist-close, extern-lang-close, namespace-close
171 \(for most of these, a zero offset will normally produce the same
172 result, though)."
173 (save-excursion
174 (beginning-of-line)
175 (backward-up-list 1)
176 (vector (current-column))))
177
178 (defun c-lineup-close-paren (langelem)
179 "Line up the closing paren under its corresponding open paren if the
180 open paren is followed by code. If the open paren ends its line, no
181 indentation is added. E.g:
182
183 main (int, main (
184 char ** int, char **
185 ) <-> ) <- c-lineup-close-paren
186
187 Works with: defun-close, class-close, inline-close, block-close,
188 brace-list-close, arglist-close, extern-lang-close, namespace-close."
189 (save-excursion
190 (condition-case nil
191 (let (opencol spec)
192 (beginning-of-line)
193 (backward-up-list 1)
194 (setq spec (c-looking-at-special-brace-list))
195 (if spec (goto-char (car (car spec))))
196 (setq opencol (current-column))
197 (forward-char 1)
198 (if spec (progn
199 (c-forward-syntactic-ws)
200 (forward-char 1)))
201 (c-forward-syntactic-ws (c-point 'eol))
202 (if (eolp)
203 0
204 (vector opencol)))
205 (error nil))))
206
207 (defun c-lineup-streamop (langelem)
208 "Line up C++ stream operators under each other.
209
210 Works with: stream-op."
211 (save-excursion
212 (goto-char (cdr langelem))
213 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
214 (goto-char (match-beginning 0))
215 (vector (current-column))))
216
217 (defun c-lineup-multi-inher (langelem)
218 "Line up the classes in C++ multiple inheritance clauses and member
219 initializers under each other. E.g:
220
221 class Foo: Foo::Foo (int a, int b):
222 public Cyphr, Cyphr (a),
223 public Bar <-> Bar (b) <- c-lineup-multi-inher
224
225 class Foo Foo::Foo (int a, int b)
226 : public Cyphr, : Cyphr (a),
227 public Bar <-> Bar (b) <- c-lineup-multi-inher
228
229 class Foo Foo::Foo (int a, int b)
230 : public Cyphr : Cyphr (a)
231 , public Bar <-> , Bar (b) <- c-lineup-multi-inher
232
233 Works with: inher-cont, member-init-cont."
234 (save-excursion
235 (let* ((eol (c-point 'eol))
236 (here (point))
237 (char-after-ip (progn
238 (skip-chars-forward " \t")
239 (char-after))))
240 (if (cdr langelem) (goto-char (cdr langelem)))
241
242 ;; This kludge is necessary to support both inher-cont and
243 ;; member-init-cont, since they have different anchor positions.
244 (c-backward-syntactic-ws)
245 (when (eq (char-before) ?:)
246 (backward-char)
247 (c-backward-syntactic-ws))
248
249 (skip-chars-forward "^:" eol)
250 (if (eq char-after-ip ?,)
251 (skip-chars-forward " \t" eol)
252 (skip-chars-forward " \t:" eol))
253 (if (or (eolp)
254 (looking-at c-comment-start-regexp))
255 (c-forward-syntactic-ws here))
256 (vector (current-column))
257 )))
258
259 (defun c-lineup-java-inher (langelem)
260 "Line up Java implements and extends declarations.
261 If class names follows on the same line as the implements/extends
262 keyword, they are lined up under each other. Otherwise, they are
263 indented by adding `c-basic-offset' to the column of the keyword.
264 E.g:
265
266 class Foo class Foo
267 extends extends Cyphr,
268 Bar <-> Bar <- c-lineup-java-inher
269 <--> c-basic-offset
270
271 Works with: inher-cont."
272 (save-excursion
273 (goto-char (cdr langelem))
274 (forward-word 1)
275 (if (looking-at "[ \t]*$")
276 c-basic-offset
277 (c-forward-syntactic-ws)
278 (vector (current-column)))))
279
280 (defun c-lineup-java-throws (langelem)
281 "Line up Java throws declarations.
282 If exception names follows on the same line as the throws keyword,
283 they are lined up under each other. Otherwise, they are indented by
284 adding `c-basic-offset' to the column of the throws keyword. The
285 throws keyword itself is also indented by `c-basic-offset' from the
286 function declaration start if it doesn't hang. E.g:
287
288 int foo() int foo() throws Cyphr,
289 throws <-> Bar, <- c-lineup-java-throws
290 Bar <-> Vlod <- c-lineup-java-throws
291 <--><--> c-basic-offset
292
293 Works with: func-decl-cont."
294 (save-excursion
295 (let* ((lim (1- (c-point 'bol)))
296 (throws (catch 'done
297 (goto-char (cdr langelem))
298 (while (zerop (c-forward-token-1 1 t lim))
299 (if (looking-at "throws\\>[^_]")
300 (throw 'done t))))))
301 (if throws
302 (if (zerop (c-forward-token-1 1 nil (c-point 'eol)))
303 (vector (current-column))
304 (back-to-indentation)
305 (vector (+ (current-column) c-basic-offset)))
306 c-basic-offset))))
307
308 (defun c-indent-one-line-block (langelem)
309 "Indent a one line block `c-basic-offset' extra.
310 E.g:
311
312 if (n > 0) if (n > 0)
313 {m+=n; n=0;} <-> { <- c-indent-one-line-block
314 <--> c-basic-offset m+=n; n=0;
315 }
316
317 The block may use any kind of parenthesis character. nil is returned
318 if the line doesn't start with a one line block, which makes the
319 function usable in list expressions.
320
321 Work with: Almost all syntactic symbols, but most useful on *-open."
322 (save-excursion
323 (let ((eol (c-point 'eol)))
324 (back-to-indentation)
325 (if (and (eq (char-syntax (char-after)) ?\()
326 (c-safe (progn (c-forward-sexp) t))
327 (<= (point) eol))
328 c-basic-offset
329 nil))))
330
331 (defun c-indent-multi-line-block (langelem)
332 "Indent a multi line block `c-basic-offset' extra.
333 E.g:
334
335 int *foo[] = { int *foo[] = {
336 NULL, NULL,
337 {17}, <-> { <- c-indent-multi-line-block
338 17
339 }
340 <--> c-basic-offset
341
342 The block may use any kind of parenthesis character. nil is returned
343 if the line doesn't start with a multi line block, which makes the
344 function usable in list expressions.
345
346 Work with: Almost all syntactic symbols, but most useful on *-open."
347 (save-excursion
348 (let ((eol (c-point 'eol)))
349 (back-to-indentation)
350 (if (and (eq (char-syntax (char-after)) ?\()
351 (or (not (c-safe (progn (c-forward-sexp) t)))
352 (> (point) eol)))
353 c-basic-offset
354 nil))))
355
356 (defun c-lineup-C-comments (langelem)
357 "Line up C block comment continuation lines.
358 Various heuristics are used to handle many of the common comment
359 styles. Some examples:
360
361 /* /** /* /* text /* /**
362 * text * text text text ** text ** text
363 */ */ */ */ */ */
364
365 /*********************************************************************
366 * text
367 ********************************************************************/
368
369 /*********************************************************************
370 Free form text comments:
371 In comments with a long delimiter line at the start, the indentation
372 is kept unchanged for lines that start with an empty comment line
373 prefix. The delimiter line is whatever matches the
374 `comment-start-skip' regexp.
375 *********************************************************************/
376
377 The variable `c-comment-prefix-regexp' is used to recognize the
378 comment line prefix, e.g. the `*' that usually starts every line
379 inside a comment.
380
381 Works with: The `c' syntactic symbol."
382 (save-excursion
383 (let* ((here (point))
384 (prefixlen (progn (back-to-indentation)
385 (if (looking-at c-current-comment-prefix)
386 (- (match-end 0) (point))
387 0)))
388 (starterlen
389 ;; Get the length of the comment starter, not including
390 ;; the first '/'. We check if the comment prefix matched
391 ;; on the current line matches the starter or if it
392 ;; matches comment-start-skip, and choose whichever is
393 ;; longest.
394 (max (save-excursion
395 (goto-char (1+ (cdr langelem)))
396 (if (and (match-string 0)
397 (looking-at (regexp-quote (match-string 0))))
398 (- (match-end 0) (match-beginning 0))
399 0))
400 (save-excursion
401 (goto-char (cdr langelem))
402 (looking-at comment-start-skip)
403 (- (or (match-end 1)
404 (save-excursion
405 (goto-char (match-end 0))
406 (skip-chars-backward " \t")
407 (point)))
408 (point)
409 1)))))
410 (if (and (> starterlen 10) (zerop prefixlen))
411 ;; The comment has a long starter and the line doesn't have
412 ;; a nonempty comment prefix. Treat it as free form text
413 ;; and don't change the indentation.
414 (vector (current-column))
415 (forward-line -1)
416 (back-to-indentation)
417 (if (>= (cdr langelem) (point))
418 ;; On the second line in the comment.
419 (if (zerop prefixlen)
420 ;; No nonempty comment prefix. Align after comment
421 ;; starter.
422 (progn
423 (goto-char (match-end 0))
424 ;; The following should not be necessary, since
425 ;; comment-start-skip should match everything (i.e.
426 ;; typically whitespace) that leads up to the text.
427 ;;(if (looking-at "\\([ \t]+\\).+$")
428 ;; ;; Align with the text that hangs after the
429 ;; ;; comment starter.
430 ;; (goto-char (match-end 1)))
431 (vector (current-column)))
432 ;; How long is the comment starter? if greater than the
433 ;; length of the comment prefix, align left. if less
434 ;; than or equal, align right. this should also pick up
435 ;; Javadoc style comments.
436 (if (> starterlen prefixlen)
437 (progn
438 (goto-char (cdr langelem))
439 (vector (1+ (current-column))))
440 (goto-char (+ (cdr langelem) starterlen 1))
441 (vector (- (current-column) prefixlen))))
442 ;; Not on the second line in the comment. If the previous
443 ;; line has a nonempty comment prefix, align with it.
444 ;; Otherwise, align with the previous nonempty line, but
445 ;; align the comment ender with the starter.
446 (when (or (not (looking-at c-current-comment-prefix))
447 (eq (match-beginning 0) (match-end 0)))
448 (goto-char here)
449 (back-to-indentation)
450 (if (looking-at (concat "\\(" c-current-comment-prefix "\\)\\*/"))
451 (goto-char (cdr langelem))
452 (while (and (zerop (forward-line -1))
453 (looking-at "^[ \t]*$")))
454 (back-to-indentation)
455 (if (< (point) (cdr langelem))
456 ;; Align with the comment starter rather than
457 ;; with the code before it.
458 (goto-char (cdr langelem)))))
459 (vector (current-column)))))))
460
461 (defun c-lineup-comment (langelem)
462 "Line up a comment start according to `c-comment-only-line-offset'.
463 If the comment is lined up with a comment starter on the previous
464 line, that alignment is preserved.
465
466 Works with: comment-intro."
467 (save-excursion
468 (back-to-indentation)
469 (let ((col (current-column)))
470 (cond
471 ;; CASE 1: preserve aligned comments
472 ((save-excursion
473 (and (c-forward-comment -1)
474 (= col (current-column))))
475 (vector col)) ; Return an absolute column.
476 ;; indent as specified by c-comment-only-line-offset
477 ((not (bolp))
478 (or (car-safe c-comment-only-line-offset)
479 c-comment-only-line-offset))
480 (t
481 (or (cdr-safe c-comment-only-line-offset)
482 (car-safe c-comment-only-line-offset)
483 -1000)) ;jam it against the left side
484 ))))
485
486 (defun c-lineup-knr-region-comment (langelem)
487 "Line up a comment in the \"K&R region\" with the declaration.
488 That is the region between the function or class header and the
489 beginning of the block. E.g:
490
491 int main()
492 /* This is the main function. */ <- c-lineup-knr-region-comment
493 {
494 return 0;
495 }
496
497 Return nil if called in any other situation, to be useful in list
498 expressions.
499
500 Works with: comment-intro."
501 (when (or (assq 'topmost-intro-cont c-syntactic-context)
502 (assq 'func-decl-cont c-syntactic-context)
503 (assq 'knr-argdecl-intro c-syntactic-context)
504 (assq 'lambda-intro-cont c-syntactic-context))
505 (save-excursion
506 (beginning-of-line)
507 (c-beginning-of-statement-1)
508 (vector (current-column)))))
509
510 (defun c-lineup-runin-statements (langelem)
511 "Line up statements when the first statement is on the same line as
512 the block opening brace. E.g:
513
514 int main()
515 { puts (\"Hello world!\");
516 return 0; <- c-lineup-runin-statements
517 }
518
519 If there is no statement after the opening brace to align with, nil is
520 returned. This makes the function usable in list expressions.
521
522 Works with: The `statement' syntactic symbol."
523 (if (eq (char-after (cdr langelem)) ?{)
524 (save-excursion
525 (if (cdr langelem) (goto-char (cdr langelem)))
526 (forward-char 1)
527 (skip-chars-forward " \t")
528 (unless (eolp)
529 (vector (current-column))))))
530
531 (defun c-lineup-math (langelem)
532 "Line up the current line after the equal sign on the first line in
533 the statement. If there isn't any, indent with `c-basic-offset'. If
534 the current line contains an equal sign too, try to align it with the
535 first one.
536
537 Works with: statement-cont, arglist-cont, arglist-cont-nonempty."
538 (save-excursion
539 (let ((equalp (save-excursion
540 (goto-char (c-point 'boi))
541 (let ((eol (c-point 'eol)))
542 (c-forward-token-1 0 t eol)
543 (while (and (not (eq (char-after) ?=))
544 (= (c-forward-token-1 1 t eol) 0))))
545 (and (eq (char-after) ?=)
546 (- (point) (c-point 'boi)))))
547 donep)
548 (if (cdr langelem) (goto-char (cdr langelem)))
549 (while (and (not donep)
550 (< (point) (c-point 'eol)))
551 (skip-chars-forward "^=" (c-point 'eol))
552 (if (c-in-literal (cdr langelem))
553 (forward-char 1)
554 (setq donep t)))
555 (if (or (not (eq (char-after) ?=))
556 (save-excursion
557 (forward-char 1)
558 (c-forward-syntactic-ws (c-point 'eol))
559 (eolp)))
560 ;; there's no equal sign on the line
561 c-basic-offset
562 ;; calculate indentation column after equals and ws, unless
563 ;; our line contains an equals sign
564 (if (not equalp)
565 (progn
566 (forward-char 1)
567 (skip-chars-forward " \t")
568 (setq equalp 0)))
569 (vector (- (current-column) equalp)))
570 )))
571
572 (defun c-lineup-cascaded-calls (langelem)
573 "Line up \"cascaded calls\" under each other.
574 If the line begins with \"->\" and the preceding line ends with one or
575 more function calls preceded by \"->\", then the arrow is lined up with
576 the first of those \"->\". E.g:
577
578 result = proc->add(17)->add(18)
579 ->add(19) + <- c-lineup-cascaded-calls
580 offset; <- c-lineup-cascaded-calls (inactive)
581
582 In any other situation nil is returned to allow use in list
583 expressions.
584
585 Works with: statement-cont, arglist-cont, arglist-cont-nonempty."
586 (save-excursion
587 (let ((bopl (c-point 'bopl)) col)
588 (back-to-indentation)
589 (when (and (looking-at "->")
590 (= (c-backward-token-1 1 t bopl) 0)
591 (eq (char-after) ?\()
592 (= (c-backward-token-1 3 t bopl) 0)
593 (looking-at "->"))
594 (setq col (current-column))
595 (while (and (= (c-backward-token-1 1 t bopl) 0)
596 (eq (char-after) ?\()
597 (= (c-backward-token-1 3 t bopl) 0)
598 (looking-at "->"))
599 (setq col (current-column)))
600 (vector col)))))
601
602 (defun c-lineup-template-args (langelem)
603 "Line up template argument lines under the first argument.
604 To allow this function to be used in a list expression, nil is
605 returned if there's no template argument on the first line.
606
607 Works with: template-args-cont."
608 (save-excursion
609 (c-with-syntax-table c++-template-syntax-table
610 (beginning-of-line)
611 (backward-up-list 1)
612 (if (and (eq (char-after) ?<)
613 (zerop (c-forward-token-1 1 nil (c-point 'eol))))
614 (vector (current-column))))))
615
616 (defun c-lineup-ObjC-method-call (langelem)
617 "Line up selector args as elisp-mode does with function args:
618 Go to the position right after the message receiver, and if you are at
619 the end of the line, indent the current line c-basic-offset columns
620 from the opening bracket; otherwise you are looking at the first
621 character of the first method call argument, so lineup the current
622 line with it.
623
624 Works with: objc-method-call-cont."
625 (save-excursion
626 (let* ((extra (save-excursion
627 (back-to-indentation)
628 (c-backward-syntactic-ws (cdr langelem))
629 (if (eq (char-before) ?:)
630 (- c-basic-offset)
631 0)))
632 (open-bracket-pos (cdr langelem))
633 (open-bracket-col (progn
634 (goto-char open-bracket-pos)
635 (current-column)))
636 (target-col (progn
637 (forward-char)
638 (c-forward-sexp)
639 (skip-chars-forward " \t")
640 (if (eolp)
641 (+ open-bracket-col c-basic-offset)
642 (current-column))))
643 )
644 (- target-col open-bracket-col extra))))
645
646 (defun c-lineup-ObjC-method-args (langelem)
647 "Line up the colons that separate args.
648 The colon on the current line is aligned with the one on the first
649 line.
650
651 Works with: objc-method-args-cont."
652 (save-excursion
653 (let* ((here (c-point 'boi))
654 (curcol (progn (goto-char here) (current-column)))
655 (eol (c-point 'eol))
656 (relpos (cdr langelem))
657 (first-col-column (progn
658 (goto-char relpos)
659 (skip-chars-forward "^:" eol)
660 (and (eq (char-after) ?:)
661 (current-column)))))
662 (if (not first-col-column)
663 c-basic-offset
664 (goto-char here)
665 (skip-chars-forward "^:" eol)
666 (if (eq (char-after) ?:)
667 (+ curcol (- first-col-column (current-column)))
668 c-basic-offset)))))
669
670 (defun c-lineup-ObjC-method-args-2 (langelem)
671 "Line up the colons that separate args.
672 The colon on the current line is aligned with the one on the previous
673 line.
674
675 Works with: objc-method-args-cont."
676 (save-excursion
677 (let* ((here (c-point 'boi))
678 (curcol (progn (goto-char here) (current-column)))
679 (eol (c-point 'eol))
680 (relpos (cdr langelem))
681 (prev-col-column (progn
682 (skip-chars-backward "^:" relpos)
683 (and (eq (char-before) ?:)
684 (- (current-column) 1)))))
685 (if (not prev-col-column)
686 c-basic-offset
687 (goto-char here)
688 (skip-chars-forward "^:" eol)
689 (if (eq (char-after) ?:)
690 (+ curcol (- prev-col-column (current-column)))
691 c-basic-offset)))))
692
693 (defun c-lineup-inexpr-block (langelem)
694 "Line up the block for constructs that use a block inside an expression,
695 e.g. anonymous classes in Java and lambda functions in Pike. The body
696 is aligned with the start of the header, e.g. with the \"new\" or
697 \"lambda\" keyword. Returns nil if the block isn't part of such a
698 construct.
699
700 Works with: inlambda, inexpr-statement, inexpr-class."
701 (save-excursion
702 (back-to-indentation)
703 (let* ((paren-state (c-parse-state))
704 (containing-sexp (c-most-enclosing-brace paren-state))
705 (res (or (c-looking-at-inexpr-block
706 (c-safe-position containing-sexp paren-state)
707 containing-sexp)
708 (and containing-sexp
709 (progn (goto-char containing-sexp)
710 (eq (char-after) ?{))
711 (progn (setq containing-sexp
712 (c-most-enclosing-brace paren-state
713 (point)))
714 (c-looking-at-inexpr-block
715 (c-safe-position containing-sexp paren-state)
716 containing-sexp))))))
717 (when res
718 (goto-char (cdr res))
719 (- (current-column)
720 (progn
721 (back-to-indentation)
722 (current-column)))))))
723
724 (defun c-lineup-whitesmith-in-block (langelem)
725 "Line up lines inside a block in whitesmith style.
726 It's done in a way that works both when the opening brace hangs and
727 when it doesn't. E.g:
728
729 something
730 { something {
731 foo; <-> foo; <- c-lineup-whitesmith-in-block
732 } }
733 <--> c-basic-offset
734
735 In the first case the indentation is kept unchanged, in the
736 second `c-basic-offset' is added.
737
738 Works with: defun-close, defun-block-intro, block-close,
739 brace-list-close, brace-list-intro, statement-block-intro, inclass,
740 inextern-lang, innamespace."
741 (save-excursion
742 (goto-char (cdr langelem))
743 (back-to-indentation)
744 (if (eq (char-syntax (char-after)) ?\()
745 0
746 c-basic-offset)))
747
748 (defun c-lineup-cpp-define (langelem)
749 "Line up macro continuation lines according to the indentation of
750 the construct preceding the macro. E.g:
751
752 v beg of preceding constr v beg of preceding constr
753 int dribble() {
754 const char msg[] = if (!running)
755 \"Some text.\"; error(\"Not running!\");
756
757 #define X(A, B) \ #define X(A, B) \
758 do { \ <-> do { \ <- c-lineup-cpp-define
759 printf (A, B); \ printf (A, B); \
760 } while (0) } while (0)
761
762 If `c-syntactic-indentation-in-macros' is non-nil, the function
763 returns the relative indentation to the macro start line to allow
764 accumulation with other offsets. E.g. in the following cases,
765 cpp-define-intro is combined with the statement-block-intro that comes
766 from the \"do {\" that hangs on the \"#define\" line:
767
768 int dribble() {
769 const char msg[] = if (!running)
770 \"Some text.\"; error(\"Not running!\");
771
772 #define X(A, B) do { \ #define X(A, B) do { \
773 printf (A, B); \ <-> printf (A, B); \ <- c-lineup-cpp-define
774 this->refs++; \ this->refs++; \
775 } while (0) <-> } while (0) <- c-lineup-cpp-define
776
777 The relative indentation returned by `c-lineup-cpp-define' is zero and
778 two, respectively, in these two examples. They are then added to the
779 two column indentation that statement-block-intro gives in both cases
780 here.
781
782 If the relative indentation is zero, then nil is returned instead.
783 This useful in a list expression to specify the default indentation on
784 the top level.
785
786 If `c-syntactic-indentation-in-macros' is nil then this function keeps
787 the current indentation, except for empty lines \(ignoring the ending
788 backslash) where it takes the indentation from the closest preceding
789 nonempty line in the macro. If there's no such line in the macro then
790 the indentation is taken from the construct preceding it, as described
791 above.
792
793 Works with: cpp-define-intro."
794 (let (offset)
795 (if c-syntactic-indentation-in-macros
796 ;; Go to the macro start and do a syntactic analysis of it.
797 ;; Then remove the cpp-macro element it should contain and
798 ;; calculate the indentation it then would get.
799 (save-excursion
800 (c-beginning-of-macro)
801 (setq offset (- (c-get-syntactic-indentation
802 (delete '(cpp-macro) (c-guess-basic-syntax)))
803 (save-excursion
804 (back-to-indentation)
805 (current-column))))
806 (if (zerop offset)
807 nil
808 offset))
809 ;; Do not indent syntactically inside the macro.
810 (save-excursion
811 (let ((macro-start-line (save-excursion
812 (goto-char (c-query-macro-start))
813 (beginning-of-line)
814 (point))))
815 (beginning-of-line)
816 ;; Check every line while inside the macro.
817 (while (and (> (point) macro-start-line)
818 (looking-at "[ \t]*\\\\?$")
819 (= (forward-line -1) 0)))
820 (if (<= (point) macro-start-line)
821 ;; If we've stepped out of the macro we take the
822 ;; syntactic offset.
823 (setq offset (c-get-syntactic-indentation
824 (delete '(cpp-macro) (c-guess-basic-syntax))))
825 (setq offset (current-indentation)))
826 (if (zerop offset)
827 nil
828 (vector offset)))))))
829
830 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
831 (defun c-lineup-gcc-asm-reg (elem)
832 "Line up a gcc asm register under one on a previous line.
833
834 asm (\"foo %1, %0\\n\"
835 \"bar %0, %1\"
836 : \"=r\" (w),
837 \"=r\" (x)
838 : \"0\" (y),
839 \"1\" (z));
840
841 The \"x\" line is aligned to the text after the \":\" on the \"w\" line, and
842 similarly \"z\" under \"y\".
843
844 This is done only in an \"asm\" or \"__asm__\" block, and only to those
845 lines mentioned. Anywhere else nil is returned. The usual arrangement is
846 to have this routine as an extra feature at the start of arglist lineups, e.g.
847
848 (c-lineup-gcc-asm-reg c-lineup-arglist)
849
850 Works with: arglist-cont, arglist-cont-nonempty."
851
852 (let ((orig-pos (point))
853 alignto)
854 (save-excursion
855 (and
856 c-opt-asm-stmt-key
857
858 ;; Find the ":" to align to. Look for this first so as to quickly
859 ;; eliminate pretty much all cases which are not for us.
860 (re-search-backward "^[ \t]*:[ \t]*\\(.\\)?" (cdr elem) t)
861
862 ;; Must have something after the ":".
863 (setq alignto (match-beginning 1))
864
865 ;; Don't touch ":" lines themselves.
866 (progn (goto-char orig-pos)
867 (beginning-of-line)
868 (not (looking-at "^[ \t]*:")))
869
870 ;; Only operate in an asm statement.
871 (progn (goto-char orig-pos)
872 (c-in-gcc-asm-p))
873
874 (vector (progn (goto-char alignto) (current-column)))))))
875
876 (defun c-lineup-dont-change (langelem)
877 "Do not change the indentation of the current line.
878
879 Works with: Any syntactic symbol."
880 (save-excursion
881 (back-to-indentation)
882 (vector (current-column))))
883
884 \f
885 (defun c-snug-do-while (syntax pos)
886 "Dynamically calculate brace hanginess for do-while statements.
887 Using this function, `while' clauses that end a `do-while' block will
888 remain on the same line as the brace that closes that block.
889
890 See `c-hanging-braces-alist' for how to utilize this function as an
891 ACTION associated with `block-close' syntax."
892 (save-excursion
893 (let (langelem)
894 (if (and (eq syntax 'block-close)
895 (setq langelem (assq 'block-close c-syntactic-context))
896 (progn (goto-char (cdr langelem))
897 (if (eq (char-after) ?{)
898 (c-safe (c-forward-sexp -1)))
899 (looking-at "\\<do\\>[^_]")))
900 '(before)
901 '(before after)))))
902
903 (defun c-gnu-impose-minimum ()
904 "Imposes a minimum indentation for lines inside a top-level construct.
905 The variable `c-label-minimum-indentation' specifies the minimum
906 indentation amount."
907 (let ((non-top-levels '(defun-block-intro statement statement-cont
908 statement-block-intro statement-case-intro
909 statement-case-open substatement substatement-open
910 case-label label do-while-closure else-clause
911 ))
912 (syntax c-syntactic-context)
913 langelem)
914 (while syntax
915 (setq langelem (car (car syntax))
916 syntax (cdr syntax))
917 ;; don't adjust macro or comment-only lines
918 (cond ((memq langelem '(cpp-macro comment-intro))
919 (setq syntax nil))
920 ((memq langelem non-top-levels)
921 (save-excursion
922 (setq syntax nil)
923 (back-to-indentation)
924 (if (zerop (current-column))
925 (insert-char ?\ c-label-minimum-indentation t))
926 ))
927 ))))
928
929 \f
930 ;; Useful for c-hanging-semi&comma-criteria
931
932 (defun c-semi&comma-inside-parenlist ()
933 "Controls newline insertion after semicolons in parenthesis lists.
934 If a comma was inserted, no determination is made. If a semicolon was
935 inserted inside a parenthesis list, no newline is added otherwise a
936 newline is added. In either case, checking is stopped. This supports
937 exactly the old newline insertion behavior."
938 ;; newline only after semicolon, but only if that semicolon is not
939 ;; inside a parenthesis list (e.g. a for loop statement)
940 (if (not (eq last-command-char ?\;))
941 nil ; continue checking
942 (if (condition-case nil
943 (save-excursion
944 (up-list -1)
945 (not (eq (char-after) ?\()))
946 (error t))
947 t
948 'stop)))
949
950 ;; Suppresses newlines before non-blank lines
951 (defun c-semi&comma-no-newlines-before-nonblanks ()
952 "Controls newline insertion after semicolons.
953 If a comma was inserted, no determination is made. If a semicolon was
954 inserted, and the following line is not blank, no newline is inserted.
955 Otherwise, no determination is made."
956 (save-excursion
957 (if (and (= last-command-char ?\;)
958 ;;(/= (point-max)
959 ;; (save-excursion (skip-syntax-forward " ") (point))
960 (zerop (forward-line 1))
961 (not (looking-at "^[ \t]*$")))
962 'stop
963 nil)))
964
965 ;; Suppresses new lines after semicolons in one-liners methods
966 (defun c-semi&comma-no-newlines-for-oneline-inliners ()
967 "Controls newline insertion after semicolons for some one-line methods.
968 If a comma was inserted, no determination is made. Newlines are
969 suppressed in one-liners, if the line is an in-class inline function.
970 For other semicolon contexts, no determination is made."
971 (let ((syntax (c-guess-basic-syntax))
972 (bol (save-excursion
973 (if (c-safe (up-list -1) t)
974 (c-point 'bol)
975 -1))))
976 (if (and (eq last-command-char ?\;)
977 (eq (car (car syntax)) 'inclass)
978 (eq (car (car (cdr syntax))) 'topmost-intro)
979 (= (c-point 'bol) bol))
980 'stop
981 nil)))
982
983 \f
984 (cc-provide 'cc-align)
985
986 ;;; cc-align.el ends here