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")
150 (back-to-indentation)
151 (setq extra c-basic-offset)))
152 (t (goto-char iopl)))
153 (+ (- (current-column) langelem-col) extra))))
155 (defun c-lineup-C-comments (langelem)
156 ;; line up C block comment continuation lines
159 (stars (progn (back-to-indentation)
160 (skip-chars-forward "*")))
161 (langelem-col (c-langelem-col langelem)))
162 (back-to-indentation)
163 (if (not (re-search-forward "/\\([*]+\\)" (c-point 'eol) t))
165 (if (not (looking-at "[*]+"))
167 ;; we now have to figure out where this comment begins.
169 (back-to-indentation)
170 (if (looking-at "[*]+/")
171 (progn (goto-char (match-end 0))
172 (forward-comment -1))
173 (goto-char (cdr langelem))
174 (back-to-indentation))))
175 (- (current-column) langelem-col))
178 (skip-chars-forward " \t")
179 (- (current-column) langelem-col))
180 ;; how many stars on comment opening line? if greater than
181 ;; on current line, align left. if less than or equal,
182 ;; align right. this should also pick up Javadoc style
184 (if (> (length (match-string 1)) stars)
186 (back-to-indentation)
187 (- (current-column) -1 langelem-col))
188 (- (current-column) stars langelem-col))
191 (defun c-lineup-comment (langelem)
192 ;; support old behavior for comment indentation. we look at
193 ;; c-comment-only-line-offset to decide how to indent comment
196 (back-to-indentation)
197 ;; this highly kludgiforous flag prevents the mapcar over
198 ;; c-syntactic-context from entering an infinite loop
199 (let ((recurse-prevention-flag (boundp 'recurse-prevention-flag)))
201 ;; CASE 1: preserve comment-column
202 (recurse-prevention-flag 0)
203 ((= (current-column) comment-column)
204 ;; we have to subtract out all other indentation
205 (- comment-column (apply '+ (mapcar 'c-get-offset
206 c-syntactic-context))))
207 ;; indent as specified by c-comment-only-line-offset
209 (or (car-safe c-comment-only-line-offset)
210 c-comment-only-line-offset))
212 (or (cdr-safe c-comment-only-line-offset)
213 (car-safe c-comment-only-line-offset)
214 -1000)) ;jam it against the left side
217 (defun c-lineup-runin-statements (langelem)
218 ;; line up statements in coding standards which place the first
219 ;; statement on the same line as the block opening brace.
220 (if (eq (char-after (cdr langelem)) ?{)
222 (let ((langelem-col (c-langelem-col langelem)))
224 (skip-chars-forward " \t")
225 (- (current-column) langelem-col)))
228 (defun c-lineup-math (langelem)
229 ;; line up math statement-cont after the equals
231 (let ((equalp (save-excursion
232 (goto-char (c-point 'boi))
233 (skip-chars-forward "^=" (c-point 'eol))
234 (and (eq (char-after) ?=)
235 (- (point) (c-point 'boi)))))
236 (langelem-col (c-langelem-col langelem))
238 (while (and (not donep)
239 (< (point) (c-point 'eol)))
240 (skip-chars-forward "^=" (c-point 'eol))
241 (if (c-in-literal (cdr langelem))
244 (if (not (eq (char-after) ?=))
245 ;; there's no equal sign on the line
247 ;; calculate indentation column after equals and ws, unless
248 ;; our line contains an equals sign
252 (skip-chars-forward " \t")
254 (- (current-column) equalp langelem-col))
257 (defun c-lineup-ObjC-method-call (langelem)
258 ;; Line up methods args as elisp-mode does with function args: go to
259 ;; the position right after the message receiver, and if you are at
260 ;; (eolp) indent the current line by a constant offset from the
261 ;; opening bracket; otherwise we are looking at the first character
262 ;; of the first method call argument, so lineup the current line
265 (let* ((extra (save-excursion
266 (back-to-indentation)
267 (c-backward-syntactic-ws (cdr langelem))
268 (if (eq (char-before) ?:)
271 (open-bracket-pos (cdr langelem))
272 (open-bracket-col (progn
273 (goto-char open-bracket-pos)
278 (skip-chars-forward " \t")
280 (+ open-bracket-col c-basic-offset)
283 (- target-col open-bracket-col extra))))
285 (defun c-lineup-ObjC-method-args (langelem)
286 ;; Line up the colons that separate args. This is done trying to
287 ;; align colons vertically.
289 (let* ((here (c-point 'boi))
290 (curcol (progn (goto-char here) (current-column)))
292 (relpos (cdr langelem))
293 (first-col-column (progn
295 (skip-chars-forward "^:" eol)
296 (and (eq (char-after) ?:)
298 (if (not first-col-column)
301 (skip-chars-forward "^:" eol)
302 (if (eq (char-after) ?:)
303 (+ curcol (- first-col-column (current-column)))
306 (defun c-lineup-ObjC-method-args-2 (langelem)
307 ;; Line up the colons that separate args. This is done trying to
308 ;; align the colon on the current line with the previous one.
310 (let* ((here (c-point 'boi))
311 (curcol (progn (goto-char here) (current-column)))
313 (relpos (cdr langelem))
314 (prev-col-column (progn
315 (skip-chars-backward "^:" relpos)
316 (and (eq (char-before) ?:)
317 (- (current-column) 1)))))
318 (if (not prev-col-column)
321 (skip-chars-forward "^:" eol)
322 (if (eq (char-after) ?:)
323 (+ curcol (- prev-col-column (current-column)))
326 (defun c-snug-do-while (syntax pos)
327 "Dynamically calculate brace hanginess for do-while statements.
328 Using this function, `while' clauses that end a `do-while' block will
329 remain on the same line as the brace that closes that block.
331 See `c-hanging-braces-alist' for how to utilize this function as an
332 ACTION associated with `block-close' syntax."
335 (if (and (eq syntax 'block-close)
336 (setq langelem (assq 'block-close c-syntactic-context))
337 (progn (goto-char (cdr langelem))
338 (if (eq (char-after) ?{)
339 (c-safe (forward-sexp -1)))
340 (looking-at "\\<do\\>[^_]")))
344 (defun c-gnu-impose-minimum ()
345 "Imposes a minimum indentation for lines inside a top-level construct.
346 The variable `c-label-minimum-indentation' specifies the minimum
348 (let ((non-top-levels '(defun-block-intro statement statement-cont
349 statement-block-intro statement-case-intro
350 statement-case-open substatement substatement-open
351 case-label label do-while-closure else-clause
353 (syntax c-syntactic-context)
356 (setq langelem (car (car syntax))
358 ;; don't adjust comment-only lines
359 (cond ((eq langelem 'comment-intro)
361 ((memq langelem non-top-levels)
364 (back-to-indentation)
365 (if (zerop (current-column))
366 (insert (make-string c-label-minimum-indentation 32)))
371 ;; Useful for c-hanging-semi&comma-criteria
372 (defun c-semi&comma-inside-parenlist ()
373 "Determine if a newline should be added after a semicolon.
374 If a comma was inserted, no determination is made. If a semicolon was
375 inserted inside a parenthesis list, no newline is added otherwise a
376 newline is added. In either case, checking is stopped. This supports
377 exactly the old newline insertion behavior."
378 ;; newline only after semicolon, but only if that semicolon is not
379 ;; inside a parenthesis list (e.g. a for loop statement)
380 (if (not (eq last-command-char ?\;))
381 nil ; continue checking
382 (if (condition-case nil
385 (not (eq (char-after) ?\()))
392 ;;; cc-align.el ends here