]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-align.el
Installed CC Mode 5.25.
[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,98 Free Software Foundation, Inc.
4
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
13
14 ;; This file is part of GNU Emacs.
15
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)
19 ;; any later version.
20
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.
25
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.
30
31 (eval-when-compile
32 (require 'cc-defs)
33 (require 'cc-vars)
34 (require 'cc-engine)
35 (require 'cc-langs))
36
37 \f
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.
42 (save-excursion
43 (let* ((containing-sexp
44 (save-excursion
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))
51 (progn
52 (beginning-of-line)
53 (backward-up-list 1)
54 (skip-chars-forward " \t" (c-point 'eol)))
55 (goto-char (cdr langelem)))
56 (point)))
57 (langelem-col (c-langelem-col langelem t)))
58 (if (save-excursion
59 (beginning-of-line)
60 (looking-at "[ \t]*)"))
61 (progn (goto-char (match-end 0))
62 (c-forward-sexp -1)
63 (forward-char 1)
64 (c-forward-syntactic-ws)
65 (- (current-column) langelem-col))
66 (goto-char containing-sexp)
67 (or (eolp)
68 (not (memq (char-after) '(?{ ?\( )))
69 (let ((eol (c-point 'eol))
70 (here (progn
71 (forward-char 1)
72 (skip-chars-forward " \t")
73 (point))))
74 (c-forward-syntactic-ws)
75 (if (< (point) eol)
76 (goto-char here))))
77 (- (current-column) langelem-col)
78 ))))
79
80 (defun c-lineup-arglist-intro-after-paren (langelem)
81 ;; lineup an arglist-intro line to just after the open paren
82 (save-excursion
83 (let ((langelem-col (c-langelem-col langelem t))
84 (ce-curcol (save-excursion
85 (beginning-of-line)
86 (backward-up-list 1)
87 (skip-chars-forward " \t" (c-point 'eol))
88 (current-column))))
89 (- ce-curcol langelem-col -1))))
90
91 (defun c-lineup-arglist-close-under-paren (langelem)
92 ;; lineup an arglist-close line under the corresponding open paren
93 (save-excursion
94 (let ((langelem-col (c-langelem-col langelem t))
95 (ce-curcol (save-excursion
96 (beginning-of-line)
97 (backward-up-list 1)
98 (current-column))))
99 (- ce-curcol langelem-col))))
100
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:
105 ;;
106 ;; main (int, main (
107 ;; char ** int, char **
108 ;; ) <-> ) <- c-lineup-close-paren
109 ;;
110 ;; Works with any type of paren.
111 (save-excursion
112 (condition-case nil
113 (let (opencol spec)
114 (beginning-of-line)
115 (backward-up-list 1)
116 (setq spec (c-looking-at-special-brace-list))
117 (if spec (goto-char (car (car spec))))
118 (setq opencol (current-column))
119 (forward-char 1)
120 (if spec (progn
121 (c-forward-syntactic-ws)
122 (forward-char 1)))
123 (c-forward-syntactic-ws (c-point 'eol))
124 (if (eolp)
125 0
126 (- opencol (c-langelem-col langelem t))))
127 (error 0))))
128
129 (defun c-lineup-streamop (langelem)
130 ;; lineup stream operators
131 (save-excursion
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))))
136
137 (defun c-lineup-multi-inher (langelem)
138 ;; line up multiple inheritance lines
139 (save-excursion
140 (let ((eol (c-point 'eol))
141 (here (point))
142 (langelem-col (c-langelem-col langelem)))
143 (skip-chars-forward "^:" eol)
144 (skip-chars-forward " \t:" eol)
145 (if (or (eolp)
146 (looking-at c-comment-start-regexp))
147 (c-forward-syntactic-ws here))
148 (- (current-column) langelem-col)
149 )))
150
151 (defun c-lineup-java-inher (langelem)
152 ;; line up Java implements and extends continuations
153 (save-excursion
154 (let ((langelem-col (c-langelem-col langelem)))
155 (forward-word 1)
156 (if (looking-at "[ \t]*$")
157 langelem-col
158 (c-forward-syntactic-ws)
159 (- (current-column) langelem-col)))))
160
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.
165 (save-excursion
166 (let ((iopl (c-point 'iopl))
167 (langelem-col (c-langelem-col langelem t))
168 (extra 0))
169 (back-to-indentation)
170 (cond
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]"))
176 (forward-word 1)
177 (skip-chars-forward " \t")
178 (if (eolp)
179 (progn
180 (back-to-indentation)
181 (setq extra c-basic-offset))))
182 (t (goto-char iopl)))
183 (+ (- (current-column) langelem-col) extra))))
184
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:
188 ;;
189 ;; if (n) if (n)
190 ;; {m+=n; n=0;} <-> { <- c-indent-one-line-block
191 ;; m+=n; n=0;
192 ;; }
193 (save-excursion
194 (let ((eol (progn (end-of-line) (point))))
195 (beginning-of-line)
196 (skip-chars-forward " \t")
197 (if (and (eq (following-char) ?{)
198 (c-safe (progn (c-forward-sexp) t))
199 (<= (point) eol)
200 (eq (preceding-char) ?}))
201 c-basic-offset
202 0))))
203
204 (defun c-lineup-C-comments (langelem)
205 ;; line up C block comment continuation lines
206 (save-excursion
207 (let ((here (point))
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))
213 (progn
214 (if (not (looking-at "[*]+"))
215 (progn
216 ;; we now have to figure out where this comment begins.
217 (goto-char here)
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))
225 (if (zerop stars)
226 (progn
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
232 ;; comments.
233 (if (> (length (match-string 1)) stars)
234 (progn
235 (back-to-indentation)
236 (- (current-column) -1 langelem-col))
237 (- (current-column) stars langelem-col))
238 )))))
239
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
243 ;; only-lines
244 (save-excursion
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)))
249 (cond
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
257 ((not (bolp))
258 (or (car-safe c-comment-only-line-offset)
259 c-comment-only-line-offset))
260 (t
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
264 ))))
265
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)) ?{)
270 (save-excursion
271 (let ((langelem-col (c-langelem-col langelem)))
272 (forward-char 1)
273 (skip-chars-forward " \t")
274 (- (current-column) langelem-col)))
275 0))
276
277 (defun c-lineup-math (langelem)
278 ;; line up math statement-cont after the equals
279 (save-excursion
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))
286 donep)
287 (while (and (not donep)
288 (< (point) (c-point 'eol)))
289 (skip-chars-forward "^=" (c-point 'eol))
290 (if (c-in-literal (cdr langelem))
291 (forward-char 1)
292 (setq donep t)))
293 (if (or (not (eq (char-after) ?=))
294 (save-excursion
295 (forward-char 1)
296 (c-forward-syntactic-ws (c-point 'eol))
297 (eolp)))
298 ;; there's no equal sign on the line
299 c-basic-offset
300 ;; calculate indentation column after equals and ws, unless
301 ;; our line contains an equals sign
302 (if (not equalp)
303 (progn
304 (forward-char 1)
305 (skip-chars-forward " \t")
306 (setq equalp 0)))
307 (- (current-column) equalp langelem-col))
308 )))
309
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
316 ;; with it.
317 (save-excursion
318 (let* ((extra (save-excursion
319 (back-to-indentation)
320 (c-backward-syntactic-ws (cdr langelem))
321 (if (eq (char-before) ?:)
322 (- c-basic-offset)
323 0)))
324 (open-bracket-pos (cdr langelem))
325 (open-bracket-col (progn
326 (goto-char open-bracket-pos)
327 (current-column)))
328 (target-col (progn
329 (forward-char)
330 (c-forward-sexp)
331 (skip-chars-forward " \t")
332 (if (eolp)
333 (+ open-bracket-col c-basic-offset)
334 (current-column))))
335 )
336 (- target-col open-bracket-col extra))))
337
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.
341 (save-excursion
342 (let* ((here (c-point 'boi))
343 (curcol (progn (goto-char here) (current-column)))
344 (eol (c-point 'eol))
345 (relpos (cdr langelem))
346 (first-col-column (progn
347 (goto-char relpos)
348 (skip-chars-forward "^:" eol)
349 (and (eq (char-after) ?:)
350 (current-column)))))
351 (if (not first-col-column)
352 c-basic-offset
353 (goto-char here)
354 (skip-chars-forward "^:" eol)
355 (if (eq (char-after) ?:)
356 (+ curcol (- first-col-column (current-column)))
357 c-basic-offset)))))
358
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.
362 (save-excursion
363 (let* ((here (c-point 'boi))
364 (curcol (progn (goto-char here) (current-column)))
365 (eol (c-point 'eol))
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)
372 c-basic-offset
373 (goto-char here)
374 (skip-chars-forward "^:" eol)
375 (if (eq (char-after) ?:)
376 (+ curcol (- prev-col-column (current-column)))
377 c-basic-offset)))))
378
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.
385 (save-excursion
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)))))
391 (if (not res)
392 0
393 (goto-char (cdr res))
394 (- (current-column)
395 (progn
396 (back-to-indentation)
397 (current-column)))))))
398
399 (defun c-lineup-dont-change (langelem)
400 ;; Do not change the indentation of the current line
401 (save-excursion
402 (back-to-indentation)
403 (current-column)))
404
405
406 \f
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.
411
412 See `c-hanging-braces-alist' for how to utilize this function as an
413 ACTION associated with `block-close' syntax."
414 (save-excursion
415 (let (langelem)
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\\>[^_]")))
422 '(before)
423 '(before after)))))
424
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
428 indentation amount."
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
433 ))
434 (syntax c-syntactic-context)
435 langelem)
436 (while syntax
437 (setq langelem (car (car syntax))
438 syntax (cdr syntax))
439 ;; don't adjust comment-only lines
440 (cond ((eq langelem 'comment-intro)
441 (setq syntax nil))
442 ((memq langelem non-top-levels)
443 (save-excursion
444 (setq syntax nil)
445 (back-to-indentation)
446 (if (zerop (current-column))
447 (insert (make-string c-label-minimum-indentation 32)))
448 ))
449 ))))
450
451 \f
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
464 (save-excursion
465 (up-list -1)
466 (not (eq (char-after) ?\()))
467 (error t))
468 t
469 'stop)))
470
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."
477 (save-excursion
478 (if (and (= last-command-char ?\;)
479 ;;(/= (point-max)
480 ;; (save-excursion (skip-syntax-forward " ") (point))
481 (zerop (forward-line 1))
482 (not (looking-at "^[ \t]*$")))
483 'stop
484 nil)))
485
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))
493 (bol (save-excursion
494 (if (c-safe (up-list -1) t)
495 (c-point 'bol)
496 -1))))
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))
501 'stop
502 nil)))
503
504 \f
505 (provide 'cc-align)
506 ;;; cc-align.el ends here