Some fixes to follow coding conventions.
[bpt/emacs.git] / lisp / progmodes / cc-align.el
CommitLineData
785eecbb
RS
1;;; cc-align.el --- custom indentation functions for CC Mode
2
130c507e 3;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
785eecbb 4
c4052e82
GM
5;; Authors: 2000- Martin Stjernholm
6;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
0ec8351b 7;; 1992-1997 Barry A. Warsaw
785eecbb
RS
8;; 1987 Dave Detlefs and Stewart Clamen
9;; 1985 Richard M. Stallman
0ec8351b 10;; Maintainer: bug-cc-mode@gnu.org
785eecbb 11;; Created: 22-Apr-1997 (split from cc-mode.el)
81eb2ff9 12;; Version: See cc-mode.el
785eecbb
RS
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
130c507e
GM
28;; along with this program; see the file COPYING. If not, write to
29;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
785eecbb
RS
30;; Boston, MA 02111-1307, USA.
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)))
130c507e
GM
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)
785eecbb
RS
48
49\f
50;; Standard indentation line-ups
51f606de 51
785eecbb 52(defun c-lineup-arglist (langelem)
51f606de
GM
53 "Line up the current argument line under the first argument.
54
55Works with: arglist-cont-nonempty."
785eecbb
RS
56 (save-excursion
57 (let* ((containing-sexp
58 (save-excursion
59 ;; arglist-cont-nonempty gives relpos ==
60 ;; to boi of containing-sexp paren. This
61 ;; is good when offset is +, but bad
62 ;; when it is c-lineup-arglist, so we
63 ;; have to special case a kludge here.
64 (if (memq (car langelem) '(arglist-intro arglist-cont-nonempty))
65 (progn
66 (beginning-of-line)
67 (backward-up-list 1)
68 (skip-chars-forward " \t" (c-point 'eol)))
69 (goto-char (cdr langelem)))
70 (point)))
71 (langelem-col (c-langelem-col langelem t)))
72 (if (save-excursion
73 (beginning-of-line)
74 (looking-at "[ \t]*)"))
75 (progn (goto-char (match-end 0))
0ec8351b 76 (c-forward-sexp -1)
785eecbb
RS
77 (forward-char 1)
78 (c-forward-syntactic-ws)
79 (- (current-column) langelem-col))
80 (goto-char containing-sexp)
81 (or (eolp)
51f606de 82 (not (memq (char-after) '(?{ ?\( ?\[)))
785eecbb
RS
83 (let ((eol (c-point 'eol))
84 (here (progn
85 (forward-char 1)
86 (skip-chars-forward " \t")
87 (point))))
88 (c-forward-syntactic-ws)
89 (if (< (point) eol)
90 (goto-char here))))
91 (- (current-column) langelem-col)
92 ))))
93
94(defun c-lineup-arglist-intro-after-paren (langelem)
51f606de
GM
95 "Line up a line just after the open paren of the surrounding paren or
96brace block.
97
98Works with: defun-block-intro, brace-list-intro,
99statement-block-intro, statement-case-intro, arglist-intro."
785eecbb
RS
100 (save-excursion
101 (let ((langelem-col (c-langelem-col langelem t))
102 (ce-curcol (save-excursion
103 (beginning-of-line)
104 (backward-up-list 1)
105 (skip-chars-forward " \t" (c-point 'eol))
106 (current-column))))
107 (- ce-curcol langelem-col -1))))
108
109(defun c-lineup-arglist-close-under-paren (langelem)
51f606de
GM
110 "Line up a closing paren line under the corresponding open paren.
111
112Works with: defun-close, class-close, inline-close, block-close,
113brace-list-close, arglist-close, extern-lang-close, namespace-close
114\(for most of these, a zero offset will normally produce the same
115result, though)."
785eecbb
RS
116 (save-excursion
117 (let ((langelem-col (c-langelem-col langelem t))
118 (ce-curcol (save-excursion
119 (beginning-of-line)
120 (backward-up-list 1)
121 (current-column))))
122 (- ce-curcol langelem-col))))
123
eae86618 124(defun c-lineup-close-paren (langelem)
51f606de
GM
125 "Line up the closing paren under its corresponding open paren if the
126open paren is followed by code. If the open paren ends its line, no
127indentation is added. E.g:
128
129main (int, main (
130 char ** int, char **
131 ) <-> ) <- c-lineup-close-paren
132
133Works with: defun-close, class-close, inline-close, block-close,
134brace-list-close, arglist-close, extern-lang-close, namespace-close."
eae86618
RS
135 (save-excursion
136 (condition-case nil
137 (let (opencol spec)
138 (beginning-of-line)
139 (backward-up-list 1)
0ec8351b
BW
140 (setq spec (c-looking-at-special-brace-list))
141 (if spec (goto-char (car (car spec))))
eae86618
RS
142 (setq opencol (current-column))
143 (forward-char 1)
144 (if spec (progn
145 (c-forward-syntactic-ws)
146 (forward-char 1)))
147 (c-forward-syntactic-ws (c-point 'eol))
148 (if (eolp)
149 0
150 (- opencol (c-langelem-col langelem t))))
51f606de 151 (error nil))))
eae86618 152
785eecbb 153(defun c-lineup-streamop (langelem)
51f606de
GM
154 "Line up C++ stream operators under each other.
155
156Works with: stream-op."
785eecbb
RS
157 (save-excursion
158 (let ((langelem-col (c-langelem-col langelem)))
159 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
160 (goto-char (match-beginning 0))
161 (- (current-column) langelem-col))))
162
163(defun c-lineup-multi-inher (langelem)
c4052e82
GM
164 "Line up the classes in C++ multiple inheritance clauses and member
165initializers under each other. E.g:
51f606de 166
c4052e82
GM
167class Foo: Foo::Foo (int a, int b):
168 public Cyphr, Cyphr (a),
169 public Bar <-> Bar (b) <- c-lineup-multi-inher
170
171class Foo Foo::Foo (int a, int b)
172 : public Cyphr, : Cyphr (a),
173 public Bar <-> Bar (b) <- c-lineup-multi-inher
174
175class Foo Foo::Foo (int a, int b)
176 : public Cyphr : Cyphr (a)
177 , public Bar <-> , Bar (b) <- c-lineup-multi-inher
178
179Works with: inher-cont, member-init-cont."
785eecbb 180 (save-excursion
c4052e82
GM
181 (let* ((eol (c-point 'eol))
182 (here (point))
183 (char-after-ip (progn
184 (skip-chars-forward " \t")
185 (char-after)))
186 (langelem-col (c-langelem-col langelem)))
187
188 ;; This kludge is necessary to support both inher-cont and
189 ;; member-init-cont, since they have different anchor positions.
190 (c-backward-syntactic-ws)
191 (when (eq (char-before) ?:)
192 (backward-char)
193 (c-backward-syntactic-ws))
194
785eecbb 195 (skip-chars-forward "^:" eol)
c4052e82
GM
196 (if (eq char-after-ip ?,)
197 (skip-chars-forward " \t" eol)
198 (skip-chars-forward " \t:" eol))
785eecbb
RS
199 (if (or (eolp)
200 (looking-at c-comment-start-regexp))
201 (c-forward-syntactic-ws here))
202 (- (current-column) langelem-col)
203 )))
204
205(defun c-lineup-java-inher (langelem)
51f606de
GM
206 "Line up Java implements and extends declarations.
207If class names follows on the same line as the implements/extends
208keyword, they are lined up under each other. Otherwise, they are
209indented by adding `c-basic-offset' to the column of the keyword.
210E.g:
211
212class Foo class Foo
213 extends extends Cyphr,
214 Bar <-> Bar <- c-lineup-java-inher
215 <--> c-basic-offset
216
217Works with: inher-cont."
785eecbb
RS
218 (save-excursion
219 (let ((langelem-col (c-langelem-col langelem)))
220 (forward-word 1)
221 (if (looking-at "[ \t]*$")
51f606de 222 c-basic-offset
785eecbb
RS
223 (c-forward-syntactic-ws)
224 (- (current-column) langelem-col)))))
225
226(defun c-lineup-java-throws (langelem)
51f606de
GM
227 "Line up Java throws declarations.
228If exception names follows on the same line as the throws keyword,
229they are lined up under each other. Otherwise, they are indented by
230adding `c-basic-offset' to the column of the throws keyword. The
231throws keyword itself is also indented by `c-basic-offset' from the
232function declaration start if it doesn't hang. E.g:
233
234int foo() int foo() throws Cyphr,
235 throws <-> Bar, <- c-lineup-java-throws
236 Bar <-> Vlod <- c-lineup-java-throws
237<--><--> c-basic-offset
238
239Works with: func-decl-cont."
785eecbb 240 (save-excursion
51f606de
GM
241 (let* ((lim (1- (c-point 'bol)))
242 (throws (catch 'done
243 (goto-char (cdr langelem))
244 (while (zerop (c-forward-token-1 1 t lim))
245 (if (looking-at "throws\\>[^_]")
246 (throw 'done t))))))
247 (if throws
248 (if (zerop (c-forward-token-1 1 nil (c-point 'eol)))
249 (- (current-column) (c-langelem-col langelem))
250 (back-to-indentation)
251 (+ (- (current-column) (c-langelem-col langelem))
252 c-basic-offset))
253 c-basic-offset))))
785eecbb 254
eae86618 255(defun c-indent-one-line-block (langelem)
51f606de
GM
256 "Indent a one line block `c-basic-offset' extra.
257E.g:
258
259if (n > 0) if (n > 0)
260 {m+=n; n=0;} <-> { <- c-indent-one-line-block
261<--> c-basic-offset m+=n; n=0;
262 }
263
130c507e
GM
264The block may use any kind of parenthesis character. nil is returned
265if the line doesn't start with a one line block, which makes the
266function usable in list expressions.
51f606de
GM
267
268Work with: Almost all syntactic symbols, but most useful on *-open."
eae86618 269 (save-excursion
51f606de
GM
270 (let ((eol (c-point 'eol)))
271 (back-to-indentation)
272 (if (and (eq (char-syntax (char-after)) ?\()
0ec8351b 273 (c-safe (progn (c-forward-sexp) t))
51f606de 274 (<= (point) eol))
eae86618 275 c-basic-offset
51f606de 276 nil))))
eae86618 277
51f606de
GM
278(defun c-indent-multi-line-block (langelem)
279 "Indent a multi line block `c-basic-offset' extra.
280E.g:
281
282int *foo[] = { int *foo[] = {
283 NULL, NULL,
284 {17}, <-> { <- c-indent-multi-line-block
285 17
286 }
287 <--> c-basic-offset
288
130c507e
GM
289The block may use any kind of parenthesis character. nil is returned
290if the line doesn't start with a multi line block, which makes the
291function usable in list expressions.
51f606de
GM
292
293Work with: Almost all syntactic symbols, but most useful on *-open."
785eecbb 294 (save-excursion
51f606de 295 (let ((eol (c-point 'eol)))
785eecbb 296 (back-to-indentation)
51f606de
GM
297 (if (and (eq (char-syntax (char-after)) ?\()
298 (or (not (c-safe (progn (c-forward-sexp) t)))
299 (> (point) eol)))
300 c-basic-offset
301 nil))))
302
303(defun c-lineup-C-comments (langelem)
304 "Line up C block comment continuation lines.
130c507e 305Various heuristics are used to handle many of the common comment
51f606de
GM
306styles. Some examples:
307
308/* /** /* /* text /* /**
309 * text * text text text ** text ** text
310 */ */ */ */ */ */
311
312/*********************************************************************
313 * text
314 ********************************************************************/
315
316/*********************************************************************
317 Free form text comments:
318 In comments with a long delimiter line at the start, the indentation
319 is kept unchanged for lines that start with an empty comment line
320 prefix. The delimiter line is whatever matches the
321 `comment-start-skip' regexp.
322*********************************************************************/
323
324The variable `c-comment-prefix-regexp' is used to recognize the
325comment line prefix, e.g. the `*' that usually starts every line
326inside a comment.
327
328Works with: The `c' syntactic symbol."
329 (save-excursion
330 (let* ((here (point))
331 (prefixlen (progn (back-to-indentation)
130c507e 332 (if (looking-at c-current-comment-prefix)
51f606de
GM
333 (- (match-end 0) (point))
334 0)))
335 (starterlen (save-excursion
336 (goto-char (cdr langelem))
337 (looking-at comment-start-skip)
338 (- (save-excursion
339 (goto-char (match-end 0))
340 (skip-chars-backward " \t")
341 (point))
342 (or (match-end 1) (point))
343 1))) ; Don't count the first '/'.
344 (langelem-col (save-excursion (c-langelem-col langelem))))
345 (if (and (> starterlen 10) (zerop prefixlen))
346 ;; The comment has a long starter and the line doesn't have
347 ;; a nonempty comment prefix. Treat it as free form text
348 ;; and don't change the indentation.
349 (- (current-column) langelem-col)
350 (forward-line -1)
351 (back-to-indentation)
352 (if (>= (cdr langelem) (point))
353 ;; On the second line in the comment.
354 (if (zerop prefixlen)
355 ;; No nonempty comment prefix. Align after comment
356 ;; starter.
785eecbb 357 (progn
51f606de
GM
358 (goto-char (match-end 0))
359 (if (looking-at "\\([ \t]+\\).+$")
360 ;; Align with the text that hangs after the
361 ;; comment starter.
362 (goto-char (match-end 1)))
363 (- (current-column) langelem-col))
364 ;; How long is the comment starter? if greater than the
365 ;; length of the comment prefix, align left. if less
366 ;; than or equal, align right. this should also pick up
367 ;; Javadoc style comments.
368 (if (> starterlen prefixlen)
369 (progn
785eecbb 370 (goto-char (cdr langelem))
51f606de
GM
371 (- (current-column) -1 langelem-col))
372 (goto-char (match-end 0))
373 (skip-chars-backward " \t")
374 (- (current-column) prefixlen langelem-col)))
375 ;; Not on the second line in the comment. If the previous
376 ;; line has a nonempty comment prefix, align with it.
377 ;; Otherwise, align with the previous nonempty line, but
378 ;; align the comment ender with the starter.
130c507e 379 (when (or (not (looking-at c-current-comment-prefix))
51f606de
GM
380 (eq (match-beginning 0) (match-end 0)))
381 (goto-char here)
382 (back-to-indentation)
130c507e 383 (if (looking-at (concat "\\(" c-current-comment-prefix "\\)\\*/"))
51f606de
GM
384 (goto-char (cdr langelem))
385 (while (and (zerop (forward-line -1))
386 (looking-at "^[ \t]*$")))
387 (back-to-indentation)
388 (if (< (point) (cdr langelem))
389 ;; Align with the comment starter rather than
390 ;; with the code before it.
391 (goto-char (cdr langelem)))))
392 (- (current-column) langelem-col))))))
785eecbb
RS
393
394(defun c-lineup-comment (langelem)
51f606de
GM
395 "Line up a comment start according to `c-comment-only-line-offset'.
396If the comment is lined up with a comment starter on the previous
397line, that alignment is preserved.
398
399Works with: comment-intro."
785eecbb
RS
400 (save-excursion
401 (back-to-indentation)
130c507e 402 (let ((col (current-column)))
785eecbb 403 (cond
51f606de
GM
404 ;; CASE 1: preserve aligned comments
405 ((save-excursion
406 (and (c-forward-comment -1)
407 (= col (current-column))))
130c507e 408 (vector col)) ; Return an absolute column.
785eecbb
RS
409 ;; indent as specified by c-comment-only-line-offset
410 ((not (bolp))
411 (or (car-safe c-comment-only-line-offset)
412 c-comment-only-line-offset))
413 (t
414 (or (cdr-safe c-comment-only-line-offset)
415 (car-safe c-comment-only-line-offset)
416 -1000)) ;jam it against the left side
417 ))))
418
419(defun c-lineup-runin-statements (langelem)
51f606de
GM
420 "Line up statements when the first statement is on the same line as
421the block opening brace. E.g:
422
423int main()
424{ puts (\"Hello world!\");
425 return 0; <- c-lineup-runin-statements
426}
427
428If there is no statement after the opening brace to align with, nil is
429returned. This makes the function usable in list expressions.
430
431Works with: The `statement' syntactic symbol."
785eecbb
RS
432 (if (eq (char-after (cdr langelem)) ?{)
433 (save-excursion
434 (let ((langelem-col (c-langelem-col langelem)))
435 (forward-char 1)
436 (skip-chars-forward " \t")
51f606de
GM
437 (unless (eolp)
438 (- (current-column) langelem-col))))))
785eecbb
RS
439
440(defun c-lineup-math (langelem)
51f606de
GM
441 "Line up the current line after the equal sign on the first line in
442the statement. If there isn't any, indent with `c-basic-offset'. If
443the current line contains an equal sign too, try to align it with the
444first one.
445
446Works with: statement-cont."
785eecbb
RS
447 (save-excursion
448 (let ((equalp (save-excursion
449 (goto-char (c-point 'boi))
130c507e
GM
450 (let ((eol (c-point 'eol)))
451 (c-forward-token-1 0 t eol)
452 (while (and (not (eq (char-after) ?=))
453 (= (c-forward-token-1 1 t eol) 0))))
785eecbb
RS
454 (and (eq (char-after) ?=)
455 (- (point) (c-point 'boi)))))
456 (langelem-col (c-langelem-col langelem))
457 donep)
458 (while (and (not donep)
459 (< (point) (c-point 'eol)))
460 (skip-chars-forward "^=" (c-point 'eol))
461 (if (c-in-literal (cdr langelem))
462 (forward-char 1)
463 (setq donep t)))
0ec8351b
BW
464 (if (or (not (eq (char-after) ?=))
465 (save-excursion
466 (forward-char 1)
467 (c-forward-syntactic-ws (c-point 'eol))
468 (eolp)))
785eecbb
RS
469 ;; there's no equal sign on the line
470 c-basic-offset
471 ;; calculate indentation column after equals and ws, unless
472 ;; our line contains an equals sign
473 (if (not equalp)
474 (progn
475 (forward-char 1)
476 (skip-chars-forward " \t")
477 (setq equalp 0)))
478 (- (current-column) equalp langelem-col))
479 )))
480
51f606de
GM
481(defun c-lineup-template-args (langelem)
482 "Line up template argument lines under the first argument.
483To allow this function to be used in a list expression, nil is
484returned if there's no template argument on the first line.
485
486Works with: template-args-cont."
487 (save-excursion
488 (c-with-syntax-table c++-template-syntax-table
489 (beginning-of-line)
490 (backward-up-list 1)
491 (if (and (eq (char-after) ?<)
492 (zerop (c-forward-token-1 1 nil (c-point 'eol))))
493 (- (current-column) (c-langelem-col langelem))))))
494
785eecbb 495(defun c-lineup-ObjC-method-call (langelem)
51f606de
GM
496 "Line up selector args as elisp-mode does with function args:
497Go to the position right after the message receiver, and if you are at
498the end of the line, indent the current line c-basic-offset columns
499from the opening bracket; otherwise you are looking at the first
500character of the first method call argument, so lineup the current
501line with it.
502
503Works with: objc-method-call-cont."
785eecbb
RS
504 (save-excursion
505 (let* ((extra (save-excursion
506 (back-to-indentation)
507 (c-backward-syntactic-ws (cdr langelem))
508 (if (eq (char-before) ?:)
509 (- c-basic-offset)
510 0)))
511 (open-bracket-pos (cdr langelem))
512 (open-bracket-col (progn
513 (goto-char open-bracket-pos)
514 (current-column)))
515 (target-col (progn
516 (forward-char)
0ec8351b 517 (c-forward-sexp)
785eecbb
RS
518 (skip-chars-forward " \t")
519 (if (eolp)
520 (+ open-bracket-col c-basic-offset)
521 (current-column))))
522 )
523 (- target-col open-bracket-col extra))))
524
525(defun c-lineup-ObjC-method-args (langelem)
51f606de
GM
526 "Line up the colons that separate args.
527The colon on the current line is aligned with the one on the first
528line.
529
530Works with: objc-method-args-cont."
785eecbb
RS
531 (save-excursion
532 (let* ((here (c-point 'boi))
533 (curcol (progn (goto-char here) (current-column)))
534 (eol (c-point 'eol))
535 (relpos (cdr langelem))
536 (first-col-column (progn
537 (goto-char relpos)
538 (skip-chars-forward "^:" eol)
539 (and (eq (char-after) ?:)
540 (current-column)))))
541 (if (not first-col-column)
542 c-basic-offset
543 (goto-char here)
544 (skip-chars-forward "^:" eol)
545 (if (eq (char-after) ?:)
546 (+ curcol (- first-col-column (current-column)))
547 c-basic-offset)))))
548
549(defun c-lineup-ObjC-method-args-2 (langelem)
51f606de
GM
550 "Line up the colons that separate args.
551The colon on the current line is aligned with the one on the previous
552line.
553
554Works with: objc-method-args-cont."
785eecbb
RS
555 (save-excursion
556 (let* ((here (c-point 'boi))
557 (curcol (progn (goto-char here) (current-column)))
558 (eol (c-point 'eol))
559 (relpos (cdr langelem))
560 (prev-col-column (progn
561 (skip-chars-backward "^:" relpos)
562 (and (eq (char-before) ?:)
563 (- (current-column) 1)))))
564 (if (not prev-col-column)
565 c-basic-offset
566 (goto-char here)
567 (skip-chars-forward "^:" eol)
568 (if (eq (char-after) ?:)
569 (+ curcol (- prev-col-column (current-column)))
570 c-basic-offset)))))
571
0ec8351b 572(defun c-lineup-inexpr-block (langelem)
51f606de
GM
573 "Line up the block for constructs that use a block inside an expression,
574e.g. anonymous classes in Java and lambda functions in Pike. The body
575is aligned with the start of the header, e.g. with the \"new\" or
576\"lambda\" keyword. Returns nil if the block isn't part of such a
577construct.
578
579Works with: inlambda, inexpr-statement, inexpr-class."
0ec8351b
BW
580 (save-excursion
581 (back-to-indentation)
582 (let ((res (or (c-looking-at-inexpr-block)
583 (if (c-safe (backward-up-list 1)
584 (eq (char-after) ?{))
585 (c-looking-at-inexpr-block)))))
51f606de 586 (when res
0ec8351b
BW
587 (goto-char (cdr res))
588 (- (current-column)
589 (progn
590 (back-to-indentation)
591 (current-column)))))))
592
51f606de
GM
593(defun c-lineup-whitesmith-in-block (langelem)
594 "Line up lines inside a block in whitesmith style.
595It's done in a way that works both when the opening brace hangs and
596when it doesn't. E.g:
597
598something
599 { something {
600 foo; <-> foo; <- c-lineup-whitesmith-in-block
601 } }
602 <--> c-basic-offset
603
604In the first case the indentation is kept unchanged, in the
605second `c-basic-offset' is added.
606
607Works with: defun-close, defun-block-intro, block-close,
608brace-list-close, brace-list-intro, statement-block-intro, inclass,
609inextern-lang, innamespace."
eae86618 610 (save-excursion
51f606de 611 (goto-char (cdr langelem))
eae86618 612 (back-to-indentation)
51f606de
GM
613 (if (eq (char-syntax (char-after)) ?\()
614 0
615 c-basic-offset)))
eae86618 616
51f606de
GM
617(defun c-lineup-dont-change (langelem)
618 "Do not change the indentation of the current line.
619
620Works with: Any syntactic symbol."
621 (save-excursion
622 (back-to-indentation)
130c507e 623 (vector (current-column))))
eae86618
RS
624
625\f
785eecbb
RS
626(defun c-snug-do-while (syntax pos)
627 "Dynamically calculate brace hanginess for do-while statements.
628Using this function, `while' clauses that end a `do-while' block will
629remain on the same line as the brace that closes that block.
630
631See `c-hanging-braces-alist' for how to utilize this function as an
632ACTION associated with `block-close' syntax."
633 (save-excursion
634 (let (langelem)
635 (if (and (eq syntax 'block-close)
636 (setq langelem (assq 'block-close c-syntactic-context))
637 (progn (goto-char (cdr langelem))
638 (if (eq (char-after) ?{)
0ec8351b 639 (c-safe (c-forward-sexp -1)))
785eecbb
RS
640 (looking-at "\\<do\\>[^_]")))
641 '(before)
642 '(before after)))))
643
644(defun c-gnu-impose-minimum ()
645 "Imposes a minimum indentation for lines inside a top-level construct.
646The variable `c-label-minimum-indentation' specifies the minimum
647indentation amount."
648 (let ((non-top-levels '(defun-block-intro statement statement-cont
649 statement-block-intro statement-case-intro
650 statement-case-open substatement substatement-open
651 case-label label do-while-closure else-clause
652 ))
653 (syntax c-syntactic-context)
654 langelem)
655 (while syntax
656 (setq langelem (car (car syntax))
657 syntax (cdr syntax))
c4052e82
GM
658 ;; don't adjust macro or comment-only lines
659 (cond ((memq langelem '(cpp-macro comment-intro))
785eecbb
RS
660 (setq syntax nil))
661 ((memq langelem non-top-levels)
662 (save-excursion
663 (setq syntax nil)
664 (back-to-indentation)
665 (if (zerop (current-column))
666 (insert (make-string c-label-minimum-indentation 32)))
667 ))
668 ))))
669
670\f
671;; Useful for c-hanging-semi&comma-criteria
51f606de 672
785eecbb 673(defun c-semi&comma-inside-parenlist ()
eae86618 674 "Controls newline insertion after semicolons in parenthesis lists.
785eecbb
RS
675If a comma was inserted, no determination is made. If a semicolon was
676inserted inside a parenthesis list, no newline is added otherwise a
677newline is added. In either case, checking is stopped. This supports
678exactly the old newline insertion behavior."
679 ;; newline only after semicolon, but only if that semicolon is not
680 ;; inside a parenthesis list (e.g. a for loop statement)
681 (if (not (eq last-command-char ?\;))
682 nil ; continue checking
683 (if (condition-case nil
684 (save-excursion
685 (up-list -1)
686 (not (eq (char-after) ?\()))
687 (error t))
688 t
689 'stop)))
690
eae86618
RS
691;; Suppresses newlines before non-blank lines
692(defun c-semi&comma-no-newlines-before-nonblanks ()
693 "Controls newline insertion after semicolons.
694If a comma was inserted, no determination is made. If a semicolon was
695inserted, and the following line is not blank, no newline is inserted.
696Otherwise, no determination is made."
697 (save-excursion
698 (if (and (= last-command-char ?\;)
699 ;;(/= (point-max)
700 ;; (save-excursion (skip-syntax-forward " ") (point))
701 (zerop (forward-line 1))
702 (not (looking-at "^[ \t]*$")))
703 'stop
704 nil)))
705
706;; Suppresses new lines after semicolons in one-liners methods
707(defun c-semi&comma-no-newlines-for-oneline-inliners ()
708 "Controls newline insertion after semicolons for some one-line methods.
709If a comma was inserted, no determination is made. Newlines are
710suppressed in one-liners, if the line is an in-class inline function.
711For other semicolon contexts, no determination is made."
712 (let ((syntax (c-guess-basic-syntax))
713 (bol (save-excursion
714 (if (c-safe (up-list -1) t)
715 (c-point 'bol)
716 -1))))
717 (if (and (eq last-command-char ?\;)
718 (eq (car (car syntax)) 'inclass)
719 (eq (car (car (cdr syntax))) 'topmost-intro)
720 (= (c-point 'bol) bol))
721 'stop
722 nil)))
723
785eecbb 724\f
130c507e 725(cc-provide 'cc-align)
3afbc435 726
785eecbb 727;;; cc-align.el ends here