18720
|
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)
|
18848
|
10 ;; Version: 5.13
|
18720
|
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
|
|
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 (when (eolp)
|
|
150 (back-to-indentation)
|
|
151 (setq extra c-basic-offset)))
|
|
152 (t (goto-char iopl)))
|
|
153 (+ (- (current-column) langelem-col) extra))))
|
|
154
|
|
155 (defun c-lineup-C-comments (langelem)
|
|
156 ;; line up C block comment continuation lines
|
|
157 (save-excursion
|
|
158 (let ((here (point))
|
|
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))
|
|
164 (progn
|
|
165 (if (not (looking-at "[*]+"))
|
|
166 (progn
|
|
167 ;; we now have to figure out where this comment begins.
|
|
168 (goto-char here)
|
|
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))
|
|
176 (if (zerop stars)
|
|
177 (progn
|
|
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
|
|
183 ;; comments.
|
|
184 (if (> (length (match-string 1)) stars)
|
|
185 (progn
|
|
186 (back-to-indentation)
|
|
187 (- (current-column) -1 langelem-col))
|
|
188 (- (current-column) stars langelem-col))
|
|
189 )))))
|
|
190
|
|
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
|
|
194 ;; only-lines
|
|
195 (save-excursion
|
|
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)))
|
|
200 (cond
|
|
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
|
|
208 ((not (bolp))
|
|
209 (or (car-safe c-comment-only-line-offset)
|
|
210 c-comment-only-line-offset))
|
|
211 (t
|
|
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
|
|
215 ))))
|
|
216
|
|
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)) ?{)
|
|
221 (save-excursion
|
|
222 (let ((langelem-col (c-langelem-col langelem)))
|
|
223 (forward-char 1)
|
|
224 (skip-chars-forward " \t")
|
|
225 (- (current-column) langelem-col)))
|
|
226 0))
|
|
227
|
|
228 (defun c-lineup-math (langelem)
|
|
229 ;; line up math statement-cont after the equals
|
|
230 (save-excursion
|
|
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))
|
|
237 donep)
|
|
238 (while (and (not donep)
|
|
239 (< (point) (c-point 'eol)))
|
|
240 (skip-chars-forward "^=" (c-point 'eol))
|
|
241 (if (c-in-literal (cdr langelem))
|
|
242 (forward-char 1)
|
|
243 (setq donep t)))
|
|
244 (if (not (eq (char-after) ?=))
|
|
245 ;; there's no equal sign on the line
|
|
246 c-basic-offset
|
|
247 ;; calculate indentation column after equals and ws, unless
|
|
248 ;; our line contains an equals sign
|
|
249 (if (not equalp)
|
|
250 (progn
|
|
251 (forward-char 1)
|
|
252 (skip-chars-forward " \t")
|
|
253 (setq equalp 0)))
|
|
254 (- (current-column) equalp langelem-col))
|
|
255 )))
|
|
256
|
|
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
|
|
263 ;; with it.
|
|
264 (save-excursion
|
|
265 (let* ((extra (save-excursion
|
|
266 (back-to-indentation)
|
|
267 (c-backward-syntactic-ws (cdr langelem))
|
|
268 (if (eq (char-before) ?:)
|
|
269 (- c-basic-offset)
|
|
270 0)))
|
|
271 (open-bracket-pos (cdr langelem))
|
|
272 (open-bracket-col (progn
|
|
273 (goto-char open-bracket-pos)
|
|
274 (current-column)))
|
|
275 (target-col (progn
|
|
276 (forward-char)
|
|
277 (forward-sexp)
|
|
278 (skip-chars-forward " \t")
|
|
279 (if (eolp)
|
|
280 (+ open-bracket-col c-basic-offset)
|
|
281 (current-column))))
|
|
282 )
|
|
283 (- target-col open-bracket-col extra))))
|
|
284
|
|
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.
|
|
288 (save-excursion
|
|
289 (let* ((here (c-point 'boi))
|
|
290 (curcol (progn (goto-char here) (current-column)))
|
|
291 (eol (c-point 'eol))
|
|
292 (relpos (cdr langelem))
|
|
293 (first-col-column (progn
|
|
294 (goto-char relpos)
|
|
295 (skip-chars-forward "^:" eol)
|
|
296 (and (eq (char-after) ?:)
|
|
297 (current-column)))))
|
|
298 (if (not first-col-column)
|
|
299 c-basic-offset
|
|
300 (goto-char here)
|
|
301 (skip-chars-forward "^:" eol)
|
|
302 (if (eq (char-after) ?:)
|
|
303 (+ curcol (- first-col-column (current-column)))
|
|
304 c-basic-offset)))))
|
|
305
|
|
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.
|
|
309 (save-excursion
|
|
310 (let* ((here (c-point 'boi))
|
|
311 (curcol (progn (goto-char here) (current-column)))
|
|
312 (eol (c-point 'eol))
|
|
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)
|
|
319 c-basic-offset
|
|
320 (goto-char here)
|
|
321 (skip-chars-forward "^:" eol)
|
|
322 (if (eq (char-after) ?:)
|
|
323 (+ curcol (- prev-col-column (current-column)))
|
|
324 c-basic-offset)))))
|
|
325
|
|
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.
|
|
330
|
|
331 See `c-hanging-braces-alist' for how to utilize this function as an
|
|
332 ACTION associated with `block-close' syntax."
|
|
333 (save-excursion
|
|
334 (let (langelem)
|
|
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\\>[^_]")))
|
|
341 '(before)
|
|
342 '(before after)))))
|
|
343
|
|
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
|
|
347 indentation amount."
|
|
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
|
|
352 ))
|
|
353 (syntax c-syntactic-context)
|
|
354 langelem)
|
|
355 (while syntax
|
|
356 (setq langelem (car (car syntax))
|
|
357 syntax (cdr syntax))
|
|
358 ;; don't adjust comment-only lines
|
|
359 (cond ((eq langelem 'comment-intro)
|
|
360 (setq syntax nil))
|
|
361 ((memq langelem non-top-levels)
|
|
362 (save-excursion
|
|
363 (setq syntax nil)
|
|
364 (back-to-indentation)
|
|
365 (if (zerop (current-column))
|
|
366 (insert (make-string c-label-minimum-indentation 32)))
|
|
367 ))
|
|
368 ))))
|
|
369
|
|
370
|
|
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
|
|
383 (save-excursion
|
|
384 (up-list -1)
|
|
385 (not (eq (char-after) ?\()))
|
|
386 (error t))
|
|
387 t
|
|
388 'stop)))
|
|
389
|
|
390
|
|
391 (provide 'cc-align)
|
|
392 ;;; cc-align.el ends here
|