1 ;;; cc-align.el --- custom indentation functions for CC Mode
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc.
5 ;; Authors: 1992-1997 Barry A. Warsaw
6 ;; 1987 Dave Detlefs and Stewart Clamen
7 ;; 1985 Richard M. Stallman
8 ;; Maintainer: cc-mode-help@python.org
9 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Keywords: c languages oop
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
37 ;; Standard indentation line-ups
38 (defun c-lineup-arglist (langelem)
39 ;; lineup the current arglist line with the arglist appearing just
40 ;; after the containing paren which starts the arglist.
42 (let* ((containing-sexp
44 ;; arglist-cont-nonempty gives relpos ==
45 ;; to boi of containing-sexp paren. This
46 ;; is good when offset is +, but bad
47 ;; when it is c-lineup-arglist, so we
48 ;; have to special case a kludge here.
49 (if (memq (car langelem) '(arglist-intro arglist-cont-nonempty))
53 (skip-chars-forward " \t" (c-point 'eol)))
54 (goto-char (cdr langelem)))
56 (langelem-col (c-langelem-col langelem t)))
59 (looking-at "[ \t]*)"))
60 (progn (goto-char (match-end 0))
63 (c-forward-syntactic-ws)
64 (- (current-column) langelem-col))
65 (goto-char containing-sexp)
67 (not (memq (char-after) '(?{ ?\( )))
68 (let ((eol (c-point 'eol))
71 (skip-chars-forward " \t")
73 (c-forward-syntactic-ws)
76 (- (current-column) langelem-col)
79 (defun c-lineup-arglist-intro-after-paren (langelem)
80 ;; lineup an arglist-intro line to just after the open paren
82 (let ((langelem-col (c-langelem-col langelem t))
83 (ce-curcol (save-excursion
86 (skip-chars-forward " \t" (c-point 'eol))
88 (- ce-curcol langelem-col -1))))
90 (defun c-lineup-arglist-close-under-paren (langelem)
91 ;; lineup an arglist-intro line to just after the open paren
93 (let ((langelem-col (c-langelem-col langelem t))
94 (ce-curcol (save-excursion
98 (- ce-curcol langelem-col))))
100 (defun c-lineup-streamop (langelem)
101 ;; lineup stream operators
103 (let ((langelem-col (c-langelem-col langelem)))
104 (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
105 (goto-char (match-beginning 0))
106 (- (current-column) langelem-col))))
108 (defun c-lineup-multi-inher (langelem)
109 ;; line up multiple inheritance lines
111 (let ((eol (c-point 'eol))
113 (langelem-col (c-langelem-col langelem)))
114 (skip-chars-forward "^:" eol)
115 (skip-chars-forward " \t:" eol)
117 (looking-at c-comment-start-regexp))
118 (c-forward-syntactic-ws here))
119 (- (current-column) langelem-col)
122 (defun c-lineup-java-inher (langelem)
123 ;; line up Java implements and extends continuations
125 (let ((langelem-col (c-langelem-col langelem)))
127 (if (looking-at "[ \t]*$")
129 (c-forward-syntactic-ws)
130 (- (current-column) langelem-col)))))
132 (defun c-lineup-java-throws (langelem)
133 ;; lineup func-decl-cont's in Java which are continuations of throws
134 ;; declarations. If `throws' starts the previous line, line up to
135 ;; just after that keyword. If not, lineup under the previous line.
137 (let ((iopl (c-point 'iopl))
138 (langelem-col (c-langelem-col langelem t))
140 (back-to-indentation)
142 ((looking-at "throws[ \t\n]")
143 (goto-char (cdr langelem))
144 (setq extra c-basic-offset))
145 ((and (goto-char iopl)
146 (looking-at "throws[ \t\n]"))
148 (skip-chars-forward " \t")
151 (back-to-indentation)
152 (setq extra c-basic-offset))))
153 (t (goto-char iopl)))
154 (+ (- (current-column) langelem-col) extra))))
156 (defun c-lineup-C-comments (langelem)
157 ;; line up C block comment continuation lines
160 (stars (progn (back-to-indentation)
161 (skip-chars-forward "*")))
162 (langelem-col (c-langelem-col langelem)))
163 (back-to-indentation)
164 (if (not (re-search-forward "/\\([*]+\\)" (c-point 'eol) t))
166 (if (not (looking-at "[*]+"))
168 ;; we now have to figure out where this comment begins.
170 (back-to-indentation)
171 (if (looking-at "[*]+/")
172 (progn (goto-char (match-end 0))
173 (forward-comment -1))
174 (goto-char (cdr langelem))
175 (back-to-indentation))))
176 (- (current-column) langelem-col))
179 (skip-chars-forward " \t")
180 (- (current-column) langelem-col))
181 ;; how many stars on comment opening line? if greater than
182 ;; on current line, align left. if less than or equal,
183 ;; align right. this should also pick up Javadoc style
185 (if (> (length (match-string 1)) stars)
187 (back-to-indentation)
188 (- (current-column) -1 langelem-col))
189 (- (current-column) stars langelem-col))
192 (defun c-lineup-comment (langelem)
193 ;; support old behavior for comment indentation. we look at
194 ;; c-comment-only-line-offset to decide how to indent comment
197 (back-to-indentation)
198 ;; this highly kludgiforous flag prevents the mapcar over
199 ;; c-syntactic-context from entering an infinite loop
200 (let ((recurse-prevention-flag (boundp 'recurse-prevention-flag)))
202 ;; CASE 1: preserve comment-column
203 (recurse-prevention-flag 0)
204 ((= (current-column) comment-column)
205 ;; we have to subtract out all other indentation
206 (- comment-column (apply '+ (mapcar 'c-get-offset
207 c-syntactic-context))))
208 ;; indent as specified by c-comment-only-line-offset
210 (or (car-safe c-comment-only-line-offset)
211 c-comment-only-line-offset))
213 (or (cdr-safe c-comment-only-line-offset)
214 (car-safe c-comment-only-line-offset)
215 -1000)) ;jam it against the left side
218 (defun c-lineup-runin-statements (langelem)
219 ;; line up statements in coding standards which place the first
220 ;; statement on the same line as the block opening brace.
221 (if (eq (char-after (cdr langelem)) ?{)
223 (let ((langelem-col (c-langelem-col langelem)))
225 (skip-chars-forward " \t")
226 (- (current-column) langelem-col)))
229 (defun c-lineup-math (langelem)
230 ;; line up math statement-cont after the equals
232 (let ((equalp (save-excursion
233 (goto-char (c-point 'boi))
234 (skip-chars-forward "^=" (c-point 'eol))
235 (and (eq (char-after) ?=)
236 (- (point) (c-point 'boi)))))
237 (langelem-col (c-langelem-col langelem))
239 (while (and (not donep)
240 (< (point) (c-point 'eol)))
241 (skip-chars-forward "^=" (c-point 'eol))
242 (if (c-in-literal (cdr langelem))
245 (if (not (eq (char-after) ?=))
246 ;; there's no equal sign on the line
248 ;; calculate indentation column after equals and ws, unless
249 ;; our line contains an equals sign
253 (skip-chars-forward " \t")
255 (- (current-column) equalp langelem-col))
258 (defun c-lineup-ObjC-method-call (langelem)
259 ;; Line up methods args as elisp-mode does with function args: go to
260 ;; the position right after the message receiver, and if you are at
261 ;; (eolp) indent the current line by a constant offset from the
262 ;; opening bracket; otherwise we are looking at the first character
263 ;; of the first method call argument, so lineup the current line
266 (let* ((extra (save-excursion
267 (back-to-indentation)
268 (c-backward-syntactic-ws (cdr langelem))
269 (if (eq (char-before) ?:)
272 (open-bracket-pos (cdr langelem))
273 (open-bracket-col (progn
274 (goto-char open-bracket-pos)
279 (skip-chars-forward " \t")
281 (+ open-bracket-col c-basic-offset)
284 (- target-col open-bracket-col extra))))
286 (defun c-lineup-ObjC-method-args (langelem)
287 ;; Line up the colons that separate args. This is done trying to
288 ;; align colons vertically.
290 (let* ((here (c-point 'boi))
291 (curcol (progn (goto-char here) (current-column)))
293 (relpos (cdr langelem))
294 (first-col-column (progn
296 (skip-chars-forward "^:" eol)
297 (and (eq (char-after) ?:)
299 (if (not first-col-column)
302 (skip-chars-forward "^:" eol)
303 (if (eq (char-after) ?:)
304 (+ curcol (- first-col-column (current-column)))
307 (defun c-lineup-ObjC-method-args-2 (langelem)
308 ;; Line up the colons that separate args. This is done trying to
309 ;; align the colon on the current line with the previous one.
311 (let* ((here (c-point 'boi))
312 (curcol (progn (goto-char here) (current-column)))
314 (relpos (cdr langelem))
315 (prev-col-column (progn
316 (skip-chars-backward "^:" relpos)
317 (and (eq (char-before) ?:)
318 (- (current-column) 1)))))
319 (if (not prev-col-column)
322 (skip-chars-forward "^:" eol)
323 (if (eq (char-after) ?:)
324 (+ curcol (- prev-col-column (current-column)))
327 (defun c-snug-do-while (syntax pos)
328 "Dynamically calculate brace hanginess for do-while statements.
329 Using this function, `while' clauses that end a `do-while' block will
330 remain on the same line as the brace that closes that block.
332 See `c-hanging-braces-alist' for how to utilize this function as an
333 ACTION associated with `block-close' syntax."
336 (if (and (eq syntax 'block-close)
337 (setq langelem (assq 'block-close c-syntactic-context))
338 (progn (goto-char (cdr langelem))
339 (if (eq (char-after) ?{)
340 (c-safe (forward-sexp -1)))
341 (looking-at "\\<do\\>[^_]")))
345 (defun c-gnu-impose-minimum ()
346 "Imposes a minimum indentation for lines inside a top-level construct.
347 The variable `c-label-minimum-indentation' specifies the minimum
349 (let ((non-top-levels '(defun-block-intro statement statement-cont
350 statement-block-intro statement-case-intro
351 statement-case-open substatement substatement-open
352 case-label label do-while-closure else-clause
354 (syntax c-syntactic-context)
357 (setq langelem (car (car syntax))
359 ;; don't adjust comment-only lines
360 (cond ((eq langelem 'comment-intro)
362 ((memq langelem non-top-levels)
365 (back-to-indentation)
366 (if (zerop (current-column))
367 (insert (make-string c-label-minimum-indentation 32)))
372 ;; Useful for c-hanging-semi&comma-criteria
373 (defun c-semi&comma-inside-parenlist ()
374 "Determine if a newline should be added after a semicolon.
375 If a comma was inserted, no determination is made. If a semicolon was
376 inserted inside a parenthesis list, no newline is added otherwise a
377 newline is added. In either case, checking is stopped. This supports
378 exactly the old newline insertion behavior."
379 ;; newline only after semicolon, but only if that semicolon is not
380 ;; inside a parenthesis list (e.g. a for loop statement)
381 (if (not (eq last-command-char ?\;))
382 nil ; continue checking
383 (if (condition-case nil
386 (not (eq (char-after) ?\()))
393 ;;; cc-align.el ends here