]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-align.el
Comment change.
[gnu-emacs] / lisp / progmodes / cc-align.el
1 ;;; cc-align.el --- custom indentation functions for CC Mode
2
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc.
4
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)
10 ;; Version: 5.16
11 ;; Keywords: c languages oop
12
13 ;; This file is part of GNU Emacs.
14
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)
18 ;; any later version.
19
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.
24
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.
29
30 (eval-when-compile
31 (require 'cc-defs)
32 (require 'cc-vars)
33 (require 'cc-engine)
34 (require 'cc-langs))
35
36 \f
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.
41 (save-excursion
42 (let* ((containing-sexp
43 (save-excursion
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))
50 (progn
51 (beginning-of-line)
52 (backward-up-list 1)
53 (skip-chars-forward " \t" (c-point 'eol)))
54 (goto-char (cdr langelem)))
55 (point)))
56 (langelem-col (c-langelem-col langelem t)))
57 (if (save-excursion
58 (beginning-of-line)
59 (looking-at "[ \t]*)"))
60 (progn (goto-char (match-end 0))
61 (forward-sexp -1)
62 (forward-char 1)
63 (c-forward-syntactic-ws)
64 (- (current-column) langelem-col))
65 (goto-char containing-sexp)
66 (or (eolp)
67 (not (memq (char-after) '(?{ ?\( )))
68 (let ((eol (c-point 'eol))
69 (here (progn
70 (forward-char 1)
71 (skip-chars-forward " \t")
72 (point))))
73 (c-forward-syntactic-ws)
74 (if (< (point) eol)
75 (goto-char here))))
76 (- (current-column) langelem-col)
77 ))))
78
79 (defun c-lineup-arglist-intro-after-paren (langelem)
80 ;; lineup an arglist-intro line to just after the open paren
81 (save-excursion
82 (let ((langelem-col (c-langelem-col langelem t))
83 (ce-curcol (save-excursion
84 (beginning-of-line)
85 (backward-up-list 1)
86 (skip-chars-forward " \t" (c-point 'eol))
87 (current-column))))
88 (- ce-curcol langelem-col -1))))
89
90 (defun c-lineup-arglist-close-under-paren (langelem)
91 ;; lineup an arglist-intro line to just after the open paren
92 (save-excursion
93 (let ((langelem-col (c-langelem-col langelem t))
94 (ce-curcol (save-excursion
95 (beginning-of-line)
96 (backward-up-list 1)
97 (current-column))))
98 (- ce-curcol langelem-col))))
99
100 (defun c-lineup-streamop (langelem)
101 ;; lineup stream operators
102 (save-excursion
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))))
107
108 (defun c-lineup-multi-inher (langelem)
109 ;; line up multiple inheritance lines
110 (save-excursion
111 (let ((eol (c-point 'eol))
112 (here (point))
113 (langelem-col (c-langelem-col langelem)))
114 (skip-chars-forward "^:" eol)
115 (skip-chars-forward " \t:" eol)
116 (if (or (eolp)
117 (looking-at c-comment-start-regexp))
118 (c-forward-syntactic-ws here))
119 (- (current-column) langelem-col)
120 )))
121
122 (defun c-lineup-java-inher (langelem)
123 ;; line up Java implements and extends continuations
124 (save-excursion
125 (let ((langelem-col (c-langelem-col langelem)))
126 (forward-word 1)
127 (if (looking-at "[ \t]*$")
128 langelem-col
129 (c-forward-syntactic-ws)
130 (- (current-column) langelem-col)))))
131
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.
136 (save-excursion
137 (let ((iopl (c-point 'iopl))
138 (langelem-col (c-langelem-col langelem t))
139 (extra 0))
140 (back-to-indentation)
141 (cond
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]"))
147 (forward-word 1)
148 (skip-chars-forward " \t")
149 (if (eolp)
150 (progn
151 (back-to-indentation)
152 (setq extra c-basic-offset))))
153 (t (goto-char iopl)))
154 (+ (- (current-column) langelem-col) extra))))
155
156 (defun c-lineup-C-comments (langelem)
157 ;; line up C block comment continuation lines
158 (save-excursion
159 (let ((here (point))
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))
165 (progn
166 (if (not (looking-at "[*]+"))
167 (progn
168 ;; we now have to figure out where this comment begins.
169 (goto-char here)
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))
177 (if (zerop stars)
178 (progn
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
184 ;; comments.
185 (if (> (length (match-string 1)) stars)
186 (progn
187 (back-to-indentation)
188 (- (current-column) -1 langelem-col))
189 (- (current-column) stars langelem-col))
190 )))))
191
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
195 ;; only-lines
196 (save-excursion
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)))
201 (cond
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
209 ((not (bolp))
210 (or (car-safe c-comment-only-line-offset)
211 c-comment-only-line-offset))
212 (t
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
216 ))))
217
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)) ?{)
222 (save-excursion
223 (let ((langelem-col (c-langelem-col langelem)))
224 (forward-char 1)
225 (skip-chars-forward " \t")
226 (- (current-column) langelem-col)))
227 0))
228
229 (defun c-lineup-math (langelem)
230 ;; line up math statement-cont after the equals
231 (save-excursion
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))
238 donep)
239 (while (and (not donep)
240 (< (point) (c-point 'eol)))
241 (skip-chars-forward "^=" (c-point 'eol))
242 (if (c-in-literal (cdr langelem))
243 (forward-char 1)
244 (setq donep t)))
245 (if (not (eq (char-after) ?=))
246 ;; there's no equal sign on the line
247 c-basic-offset
248 ;; calculate indentation column after equals and ws, unless
249 ;; our line contains an equals sign
250 (if (not equalp)
251 (progn
252 (forward-char 1)
253 (skip-chars-forward " \t")
254 (setq equalp 0)))
255 (- (current-column) equalp langelem-col))
256 )))
257
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
264 ;; with it.
265 (save-excursion
266 (let* ((extra (save-excursion
267 (back-to-indentation)
268 (c-backward-syntactic-ws (cdr langelem))
269 (if (eq (char-before) ?:)
270 (- c-basic-offset)
271 0)))
272 (open-bracket-pos (cdr langelem))
273 (open-bracket-col (progn
274 (goto-char open-bracket-pos)
275 (current-column)))
276 (target-col (progn
277 (forward-char)
278 (forward-sexp)
279 (skip-chars-forward " \t")
280 (if (eolp)
281 (+ open-bracket-col c-basic-offset)
282 (current-column))))
283 )
284 (- target-col open-bracket-col extra))))
285
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.
289 (save-excursion
290 (let* ((here (c-point 'boi))
291 (curcol (progn (goto-char here) (current-column)))
292 (eol (c-point 'eol))
293 (relpos (cdr langelem))
294 (first-col-column (progn
295 (goto-char relpos)
296 (skip-chars-forward "^:" eol)
297 (and (eq (char-after) ?:)
298 (current-column)))))
299 (if (not first-col-column)
300 c-basic-offset
301 (goto-char here)
302 (skip-chars-forward "^:" eol)
303 (if (eq (char-after) ?:)
304 (+ curcol (- first-col-column (current-column)))
305 c-basic-offset)))))
306
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.
310 (save-excursion
311 (let* ((here (c-point 'boi))
312 (curcol (progn (goto-char here) (current-column)))
313 (eol (c-point 'eol))
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)
320 c-basic-offset
321 (goto-char here)
322 (skip-chars-forward "^:" eol)
323 (if (eq (char-after) ?:)
324 (+ curcol (- prev-col-column (current-column)))
325 c-basic-offset)))))
326
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.
331
332 See `c-hanging-braces-alist' for how to utilize this function as an
333 ACTION associated with `block-close' syntax."
334 (save-excursion
335 (let (langelem)
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\\>[^_]")))
342 '(before)
343 '(before after)))))
344
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
348 indentation amount."
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
353 ))
354 (syntax c-syntactic-context)
355 langelem)
356 (while syntax
357 (setq langelem (car (car syntax))
358 syntax (cdr syntax))
359 ;; don't adjust comment-only lines
360 (cond ((eq langelem 'comment-intro)
361 (setq syntax nil))
362 ((memq langelem non-top-levels)
363 (save-excursion
364 (setq syntax nil)
365 (back-to-indentation)
366 (if (zerop (current-column))
367 (insert (make-string c-label-minimum-indentation 32)))
368 ))
369 ))))
370
371 \f
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
384 (save-excursion
385 (up-list -1)
386 (not (eq (char-after) ?\()))
387 (error t))
388 t
389 'stop)))
390
391 \f
392 (provide 'cc-align)
393 ;;; cc-align.el ends here