1 ;;; cc-align.el --- custom indentation functions for CC Mode
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc.
5 ;; Authors: 1998 Barry A. Warsaw and Martin Stjernholm
6 ;; 1992-1997 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Version: See cc-mode.el
12 ;; Keywords: c languages oop
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
38 ;; Standard indentation line-ups
39 (defun c-lineup-arglist (langelem)
40 ;; lineup the current arglist line with the arglist appearing just
41 ;; after the containing paren which starts the arglist.
43 (let* ((containing-sexp
45 ;; arglist-cont-nonempty gives relpos ==
46 ;; to boi of containing-sexp paren. This
47 ;; is good when offset is +, but bad
48 ;; when it is c-lineup-arglist, so we
49 ;; have to special case a kludge here.
50 (if (memq (car langelem) '(arglist-intro arglist-cont-nonempty))
54 (skip-chars-forward " \t" (c-point 'eol)))
55 (goto-char (cdr langelem)))
57 (langelem-col (c-langelem-col langelem t)))
60 (looking-at "[ \t]*)"))
61 (progn (goto-char (match-end 0))
64 (c-forward-syntactic-ws)
65 (- (current-column) langelem-col))
66 (goto-char containing-sexp)
68 (not (memq (char-after) '(?{ ?\( )))
69 (let ((eol (c-point 'eol))
72 (skip-chars-forward " \t")
74 (c-forward-syntactic-ws)
77 (- (current-column) langelem-col)
80 (defun c-lineup-arglist-intro-after-paren (langelem)
81 ;; lineup an arglist-intro line to just after the open paren
83 (let ((langelem-col (c-langelem-col langelem t))
84 (ce-curcol (save-excursion
87 (skip-chars-forward " \t" (c-point 'eol))
89 (- ce-curcol langelem-col -1))))
91 (defun c-lineup-arglist-close-under-paren (langelem)
92 ;; lineup an arglist-close line under the corresponding open paren
94 (let ((langelem-col (c-langelem-col langelem t))
95 (ce-curcol (save-excursion
99 (- ce-curcol langelem-col))))
101 (defun c-lineup-close-paren (langelem)
102 ;; Indents the closing paren under its corresponding open paren if
103 ;; the open paren is followed by code. If the open paren ends its
104 ;; line, no indentation is added. E.g:
107 ;; char ** int, char **
108 ;; ) <-> ) <- c-lineup-close-paren
110 ;; Works with any type of paren.
116 (setq spec (c-looking-at-special-brace-list))
117 (if spec (goto-char (car (car spec))))
118 (setq opencol (current-column))
121 (c-forward-syntactic-ws)
123 (c-forward-syntactic-ws (c-point 'eol))
126 (- opencol (c-langelem-col langelem t))))
129 (defun c-lineup-streamop (langelem)
130 ;; lineup stream operators
132 (let ((langelem-col (c-langelem-col langelem)))
133 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
134 (goto-char (match-beginning 0))
135 (- (current-column) langelem-col))))
137 (defun c-lineup-multi-inher (langelem)
138 ;; line up multiple inheritance lines
140 (let ((eol (c-point 'eol))
142 (langelem-col (c-langelem-col langelem)))
143 (skip-chars-forward "^:" eol)
144 (skip-chars-forward " \t:" eol)
146 (looking-at c-comment-start-regexp))
147 (c-forward-syntactic-ws here))
148 (- (current-column) langelem-col)
151 (defun c-lineup-java-inher (langelem)
152 ;; line up Java implements and extends continuations
154 (let ((langelem-col (c-langelem-col langelem)))
156 (if (looking-at "[ \t]*$")
158 (c-forward-syntactic-ws)
159 (- (current-column) langelem-col)))))
161 (defun c-lineup-java-throws (langelem)
162 ;; lineup func-decl-cont's in Java which are continuations of throws
163 ;; declarations. If `throws' starts the previous line, line up to
164 ;; just after that keyword. If not, lineup under the previous line.
166 (let ((iopl (c-point 'iopl))
167 (langelem-col (c-langelem-col langelem t))
169 (back-to-indentation)
171 ((looking-at "throws[ \t\n]")
172 (goto-char (cdr langelem))
173 (setq extra c-basic-offset))
174 ((and (goto-char iopl)
175 (looking-at "throws[ \t\n]"))
177 (skip-chars-forward " \t")
180 (back-to-indentation)
181 (setq extra c-basic-offset))))
182 (t (goto-char iopl)))
183 (+ (- (current-column) langelem-col) extra))))
185 (defun c-indent-one-line-block (langelem)
186 ;; Adds c-basic-offset to the indentation if the line is a one line
187 ;; block, otherwise 0. E.g:
190 ;; {m+=n; n=0;} <-> { <- c-indent-one-line-block
194 (let ((eol (progn (end-of-line) (point))))
196 (skip-chars-forward " \t")
197 (if (and (eq (following-char) ?{)
198 (c-safe (progn (c-forward-sexp) t))
200 (eq (preceding-char) ?}))
204 (defun c-lineup-C-comments (langelem)
205 ;; line up C block comment continuation lines
208 (stars (progn (back-to-indentation)
209 (skip-chars-forward "*")))
210 (langelem-col (c-langelem-col langelem)))
211 (back-to-indentation)
212 (if (not (re-search-forward "/\\([*]+\\)" (c-point 'eol) t))
214 (if (not (looking-at "[*]+"))
216 ;; we now have to figure out where this comment begins.
218 (back-to-indentation)
219 (if (looking-at "[*]+/")
220 (progn (goto-char (match-end 0))
221 (forward-comment -1))
222 (goto-char (cdr langelem))
223 (back-to-indentation))))
224 (- (current-column) langelem-col))
227 (skip-chars-forward " \t")
228 (- (current-column) langelem-col))
229 ;; how many stars on comment opening line? if greater than
230 ;; on current line, align left. if less than or equal,
231 ;; align right. this should also pick up Javadoc style
233 (if (> (length (match-string 1)) stars)
235 (back-to-indentation)
236 (- (current-column) -1 langelem-col))
237 (- (current-column) stars langelem-col))
240 (defun c-lineup-comment (langelem)
241 ;; support old behavior for comment indentation. we look at
242 ;; c-comment-only-line-offset to decide how to indent comment
245 (back-to-indentation)
246 ;; this highly kludgiforous flag prevents the mapcar over
247 ;; c-syntactic-context from entering an infinite loop
248 (let ((recurse-prevention-flag (boundp 'recurse-prevention-flag)))
250 ;; CASE 1: preserve comment-column
251 (recurse-prevention-flag 0)
252 ((= (current-column) comment-column)
253 ;; we have to subtract out all other indentation
254 (- comment-column (apply '+ (mapcar 'c-get-offset
255 c-syntactic-context))))
256 ;; indent as specified by c-comment-only-line-offset
258 (or (car-safe c-comment-only-line-offset)
259 c-comment-only-line-offset))
261 (or (cdr-safe c-comment-only-line-offset)
262 (car-safe c-comment-only-line-offset)
263 -1000)) ;jam it against the left side
266 (defun c-lineup-runin-statements (langelem)
267 ;; line up statements in coding standards which place the first
268 ;; statement on the same line as the block opening brace.
269 (if (eq (char-after (cdr langelem)) ?{)
271 (let ((langelem-col (c-langelem-col langelem)))
273 (skip-chars-forward " \t")
274 (- (current-column) langelem-col)))
277 (defun c-lineup-math (langelem)
278 ;; line up math statement-cont after the equals
280 (let ((equalp (save-excursion
281 (goto-char (c-point 'boi))
282 (skip-chars-forward "^=" (c-point 'eol))
283 (and (eq (char-after) ?=)
284 (- (point) (c-point 'boi)))))
285 (langelem-col (c-langelem-col langelem))
287 (while (and (not donep)
288 (< (point) (c-point 'eol)))
289 (skip-chars-forward "^=" (c-point 'eol))
290 (if (c-in-literal (cdr langelem))
293 (if (or (not (eq (char-after) ?=))
296 (c-forward-syntactic-ws (c-point 'eol))
298 ;; there's no equal sign on the line
300 ;; calculate indentation column after equals and ws, unless
301 ;; our line contains an equals sign
305 (skip-chars-forward " \t")
307 (- (current-column) equalp langelem-col))
310 (defun c-lineup-ObjC-method-call (langelem)
311 ;; Line up methods args as elisp-mode does with function args: go to
312 ;; the position right after the message receiver, and if you are at
313 ;; (eolp) indent the current line by a constant offset from the
314 ;; opening bracket; otherwise we are looking at the first character
315 ;; of the first method call argument, so lineup the current line
318 (let* ((extra (save-excursion
319 (back-to-indentation)
320 (c-backward-syntactic-ws (cdr langelem))
321 (if (eq (char-before) ?:)
324 (open-bracket-pos (cdr langelem))
325 (open-bracket-col (progn
326 (goto-char open-bracket-pos)
331 (skip-chars-forward " \t")
333 (+ open-bracket-col c-basic-offset)
336 (- target-col open-bracket-col extra))))
338 (defun c-lineup-ObjC-method-args (langelem)
339 ;; Line up the colons that separate args. This is done trying to
340 ;; align colons vertically.
342 (let* ((here (c-point 'boi))
343 (curcol (progn (goto-char here) (current-column)))
345 (relpos (cdr langelem))
346 (first-col-column (progn
348 (skip-chars-forward "^:" eol)
349 (and (eq (char-after) ?:)
351 (if (not first-col-column)
354 (skip-chars-forward "^:" eol)
355 (if (eq (char-after) ?:)
356 (+ curcol (- first-col-column (current-column)))
359 (defun c-lineup-ObjC-method-args-2 (langelem)
360 ;; Line up the colons that separate args. This is done trying to
361 ;; align the colon on the current line with the previous one.
363 (let* ((here (c-point 'boi))
364 (curcol (progn (goto-char here) (current-column)))
366 (relpos (cdr langelem))
367 (prev-col-column (progn
368 (skip-chars-backward "^:" relpos)
369 (and (eq (char-before) ?:)
370 (- (current-column) 1)))))
371 (if (not prev-col-column)
374 (skip-chars-forward "^:" eol)
375 (if (eq (char-after) ?:)
376 (+ curcol (- prev-col-column (current-column)))
379 (defun c-lineup-inexpr-block (langelem)
380 ;; This function lines up the block for the various constructs that
381 ;; uses a block inside an expression. For constructs matching
382 ;; c-lambda-key and c-inexpr-block-key, indentation to the column of
383 ;; the beginning of the match is added. For standalone statement
384 ;; blocks, indentation to the column of the opening brace is added.
386 (back-to-indentation)
387 (let ((res (or (c-looking-at-inexpr-block)
388 (if (c-safe (backward-up-list 1)
389 (eq (char-after) ?{))
390 (c-looking-at-inexpr-block)))))
393 (goto-char (cdr res))
396 (back-to-indentation)
397 (current-column)))))))
399 (defun c-lineup-dont-change (langelem)
400 ;; Do not change the indentation of the current line
402 (back-to-indentation)
407 (defun c-snug-do-while (syntax pos)
408 "Dynamically calculate brace hanginess for do-while statements.
409 Using this function, `while' clauses that end a `do-while' block will
410 remain on the same line as the brace that closes that block.
412 See `c-hanging-braces-alist' for how to utilize this function as an
413 ACTION associated with `block-close' syntax."
416 (if (and (eq syntax 'block-close)
417 (setq langelem (assq 'block-close c-syntactic-context))
418 (progn (goto-char (cdr langelem))
419 (if (eq (char-after) ?{)
420 (c-safe (c-forward-sexp -1)))
421 (looking-at "\\<do\\>[^_]")))
425 (defun c-gnu-impose-minimum ()
426 "Imposes a minimum indentation for lines inside a top-level construct.
427 The variable `c-label-minimum-indentation' specifies the minimum
429 (let ((non-top-levels '(defun-block-intro statement statement-cont
430 statement-block-intro statement-case-intro
431 statement-case-open substatement substatement-open
432 case-label label do-while-closure else-clause
434 (syntax c-syntactic-context)
437 (setq langelem (car (car syntax))
439 ;; don't adjust comment-only lines
440 (cond ((eq langelem 'comment-intro)
442 ((memq langelem non-top-levels)
445 (back-to-indentation)
446 (if (zerop (current-column))
447 (insert (make-string c-label-minimum-indentation 32)))
452 ;; Useful for c-hanging-semi&comma-criteria
453 (defun c-semi&comma-inside-parenlist ()
454 "Controls newline insertion after semicolons in parenthesis lists.
455 If a comma was inserted, no determination is made. If a semicolon was
456 inserted inside a parenthesis list, no newline is added otherwise a
457 newline is added. In either case, checking is stopped. This supports
458 exactly the old newline insertion behavior."
459 ;; newline only after semicolon, but only if that semicolon is not
460 ;; inside a parenthesis list (e.g. a for loop statement)
461 (if (not (eq last-command-char ?\;))
462 nil ; continue checking
463 (if (condition-case nil
466 (not (eq (char-after) ?\()))
471 ;; Suppresses newlines before non-blank lines
472 (defun c-semi&comma-no-newlines-before-nonblanks ()
473 "Controls newline insertion after semicolons.
474 If a comma was inserted, no determination is made. If a semicolon was
475 inserted, and the following line is not blank, no newline is inserted.
476 Otherwise, no determination is made."
478 (if (and (= last-command-char ?\;)
480 ;; (save-excursion (skip-syntax-forward " ") (point))
481 (zerop (forward-line 1))
482 (not (looking-at "^[ \t]*$")))
486 ;; Suppresses new lines after semicolons in one-liners methods
487 (defun c-semi&comma-no-newlines-for-oneline-inliners ()
488 "Controls newline insertion after semicolons for some one-line methods.
489 If a comma was inserted, no determination is made. Newlines are
490 suppressed in one-liners, if the line is an in-class inline function.
491 For other semicolon contexts, no determination is made."
492 (let ((syntax (c-guess-basic-syntax))
494 (if (c-safe (up-list -1) t)
497 (if (and (eq last-command-char ?\;)
498 (eq (car (car syntax)) 'inclass)
499 (eq (car (car (cdr syntax))) 'topmost-intro)
500 (= (c-point 'bol) bol))
506 ;;; cc-align.el ends here