Mercurial > hg > xemacs-beta
comparison lisp/cc-mode/cc-align.el @ 165:5a88923fcbfe r20-3b9
Import from CVS: tag r20-3b9
author | cvs |
---|---|
date | Mon, 13 Aug 2007 09:44:42 +0200 |
parents | |
children | 929b76928fce |
comparison
equal
deleted
inserted
replaced
164:4e0740e5aab2 | 165:5a88923fcbfe |
---|---|
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.11 | |
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 | |
31 (eval-when-compile | |
32 (load-file "./cc-engine.el")) | |
33 | |
34 (defsubst c-langelem-col (langelem &optional preserve-point) | |
35 ;; convenience routine to return the column of langelem's relpos. | |
36 ;; Leaves point at the relpos unless preserve-point is non-nil. | |
37 (let ((here (point))) | |
38 (goto-char (cdr langelem)) | |
39 (prog1 (current-column) | |
40 (if preserve-point | |
41 (goto-char here)) | |
42 ))) | |
43 | |
44 | |
45 ;; Standard indentation line-ups | |
46 (defun c-lineup-arglist (langelem) | |
47 ;; lineup the current arglist line with the arglist appearing just | |
48 ;; after the containing paren which starts the arglist. | |
49 (save-excursion | |
50 (let* ((containing-sexp | |
51 (save-excursion | |
52 ;; arglist-cont-nonempty gives relpos == | |
53 ;; to boi of containing-sexp paren. This | |
54 ;; is good when offset is +, but bad | |
55 ;; when it is c-lineup-arglist, so we | |
56 ;; have to special case a kludge here. | |
57 (if (memq (car langelem) '(arglist-intro arglist-cont-nonempty)) | |
58 (progn | |
59 (beginning-of-line) | |
60 (backward-up-list 1) | |
61 (skip-chars-forward " \t" (c-point 'eol))) | |
62 (goto-char (cdr langelem))) | |
63 (point))) | |
64 (langelem-col (c-langelem-col langelem t))) | |
65 (if (save-excursion | |
66 (beginning-of-line) | |
67 (looking-at "[ \t]*)")) | |
68 (progn (goto-char (match-end 0)) | |
69 (forward-sexp -1) | |
70 (forward-char 1) | |
71 (c-forward-syntactic-ws) | |
72 (- (current-column) langelem-col)) | |
73 (goto-char containing-sexp) | |
74 (or (eolp) | |
75 (not (memq (following-char) '(?{ ?\( ))) | |
76 (let ((eol (c-point 'eol)) | |
77 (here (progn | |
78 (forward-char 1) | |
79 (skip-chars-forward " \t") | |
80 (point)))) | |
81 (c-forward-syntactic-ws) | |
82 (if (< (point) eol) | |
83 (goto-char here)))) | |
84 (- (current-column) langelem-col) | |
85 )))) | |
86 | |
87 (defun c-lineup-arglist-intro-after-paren (langelem) | |
88 ;; lineup an arglist-intro line to just after the open paren | |
89 (save-excursion | |
90 (let ((langelem-col (c-langelem-col langelem t)) | |
91 (ce-curcol (save-excursion | |
92 (beginning-of-line) | |
93 (backward-up-list 1) | |
94 (skip-chars-forward " \t" (c-point 'eol)) | |
95 (current-column)))) | |
96 (- ce-curcol langelem-col -1)))) | |
97 | |
98 (defun c-lineup-arglist-close-under-paren (langelem) | |
99 ;; lineup an arglist-intro line to just after the open paren | |
100 (save-excursion | |
101 (let ((langelem-col (c-langelem-col langelem t)) | |
102 (ce-curcol (save-excursion | |
103 (beginning-of-line) | |
104 (backward-up-list 1) | |
105 (current-column)))) | |
106 (- ce-curcol langelem-col)))) | |
107 | |
108 (defun c-lineup-streamop (langelem) | |
109 ;; lineup stream operators | |
110 (save-excursion | |
111 (let ((langelem-col (c-langelem-col langelem))) | |
112 (re-search-forward "<<\\|>>" (c-point 'eol) 'move) | |
113 (goto-char (match-beginning 0)) | |
114 (- (current-column) langelem-col)))) | |
115 | |
116 (defun c-lineup-multi-inher (langelem) | |
117 ;; line up multiple inheritance lines | |
118 (save-excursion | |
119 (let ((eol (c-point 'eol)) | |
120 (here (point)) | |
121 (langelem-col (c-langelem-col langelem))) | |
122 (skip-chars-forward "^:" eol) | |
123 (skip-chars-forward " \t:" eol) | |
124 (if (or (eolp) | |
125 (looking-at c-comment-start-regexp)) | |
126 (c-forward-syntactic-ws here)) | |
127 (- (current-column) langelem-col) | |
128 ))) | |
129 | |
130 (defun c-lineup-java-inher (langelem) | |
131 ;; line up Java implements and extends continuations | |
132 (save-excursion | |
133 (let ((langelem-col (c-langelem-col langelem))) | |
134 (forward-word 1) | |
135 (if (looking-at "[ \t]*$") | |
136 langelem-col | |
137 (c-forward-syntactic-ws) | |
138 (- (current-column) langelem-col))))) | |
139 | |
140 (defun c-lineup-java-throws (langelem) | |
141 ;; lineup func-decl-cont's in Java which are continuations of throws | |
142 ;; declarations. If `throws' starts the previous line, line up to | |
143 ;; just after that keyword. If not, lineup under the previous line. | |
144 (save-excursion | |
145 (let ((iopl (c-point 'iopl)) | |
146 (langelem-col (c-langelem-col langelem t)) | |
147 (extra 0)) | |
148 (back-to-indentation) | |
149 (cond | |
150 ((looking-at "throws[ \t\n]") | |
151 (goto-char (cdr langelem)) | |
152 (setq extra c-basic-offset)) | |
153 ((and (goto-char iopl) | |
154 (looking-at "throws[ \t\n]")) | |
155 (forward-word 1) | |
156 (skip-chars-forward " \t") | |
157 (when (eolp) | |
158 (back-to-indentation) | |
159 (setq extra c-basic-offset))) | |
160 (t (goto-char iopl))) | |
161 (+ (- (current-column) langelem-col) extra)))) | |
162 | |
163 (defun c-lineup-C-comments (langelem) | |
164 ;; line up C block comment continuation lines | |
165 (save-excursion | |
166 (let ((here (point)) | |
167 (stars (progn (back-to-indentation) | |
168 (skip-chars-forward "*"))) | |
169 (langelem-col (c-langelem-col langelem))) | |
170 (back-to-indentation) | |
171 (if (not (re-search-forward "/\\([*]+\\)" (c-point 'eol) t)) | |
172 (progn | |
173 (if (not (looking-at "[*]+")) | |
174 (progn | |
175 ;; we now have to figure out where this comment begins. | |
176 (goto-char here) | |
177 (back-to-indentation) | |
178 (if (looking-at "[*]+/") | |
179 (progn (goto-char (match-end 0)) | |
180 (forward-comment -1)) | |
181 (goto-char (cdr langelem)) | |
182 (back-to-indentation)))) | |
183 (- (current-column) langelem-col)) | |
184 (if (zerop stars) | |
185 (progn | |
186 (skip-chars-forward " \t") | |
187 (- (current-column) langelem-col)) | |
188 ;; how many stars on comment opening line? if greater than | |
189 ;; on current line, align left. if less than or equal, | |
190 ;; align right. this should also pick up Javadoc style | |
191 ;; comments. | |
192 (if (> (length (match-string 1)) stars) | |
193 (progn | |
194 (back-to-indentation) | |
195 (- (current-column) -1 langelem-col)) | |
196 (- (current-column) stars langelem-col)) | |
197 ))))) | |
198 | |
199 (defun c-lineup-comment (langelem) | |
200 ;; support old behavior for comment indentation. we look at | |
201 ;; c-comment-only-line-offset to decide how to indent comment | |
202 ;; only-lines | |
203 (save-excursion | |
204 (back-to-indentation) | |
205 ;; this highly kludgiforous flag prevents the mapcar over | |
206 ;; c-syntactic-context from entering an infinite loop | |
207 (let ((recurse-prevention-flag (boundp 'recurse-prevention-flag))) | |
208 (cond | |
209 ;; CASE 1: preserve comment-column | |
210 (recurse-prevention-flag 0) | |
211 ((= (current-column) comment-column) | |
212 ;; we have to subtract out all other indentation | |
213 (- comment-column (apply '+ (mapcar 'c-get-offset | |
214 c-syntactic-context)))) | |
215 ;; indent as specified by c-comment-only-line-offset | |
216 ((not (bolp)) | |
217 (or (car-safe c-comment-only-line-offset) | |
218 c-comment-only-line-offset)) | |
219 (t | |
220 (or (cdr-safe c-comment-only-line-offset) | |
221 (car-safe c-comment-only-line-offset) | |
222 -1000)) ;jam it against the left side | |
223 )))) | |
224 | |
225 (defun c-lineup-runin-statements (langelem) | |
226 ;; line up statements in coding standards which place the first | |
227 ;; statement on the same line as the block opening brace. | |
228 (if (= (char-after (cdr langelem)) ?{) | |
229 (save-excursion | |
230 (let ((langelem-col (c-langelem-col langelem))) | |
231 (forward-char 1) | |
232 (skip-chars-forward " \t") | |
233 (- (current-column) langelem-col))) | |
234 0)) | |
235 | |
236 (defun c-lineup-math (langelem) | |
237 ;; line up math statement-cont after the equals | |
238 (save-excursion | |
239 (let ((equalp (save-excursion | |
240 (goto-char (c-point 'boi)) | |
241 (skip-chars-forward "^=" (c-point 'eol)) | |
242 (and (= (following-char) ?=) | |
243 (- (point) (c-point 'boi))))) | |
244 (langelem-col (c-langelem-col langelem)) | |
245 donep) | |
246 (while (and (not donep) | |
247 (< (point) (c-point 'eol))) | |
248 (skip-chars-forward "^=" (c-point 'eol)) | |
249 (if (c-in-literal (cdr langelem)) | |
250 (forward-char 1) | |
251 (setq donep t))) | |
252 (if (/= (following-char) ?=) | |
253 ;; there's no equal sign on the line | |
254 c-basic-offset | |
255 ;; calculate indentation column after equals and ws, unless | |
256 ;; our line contains an equals sign | |
257 (if (not equalp) | |
258 (progn | |
259 (forward-char 1) | |
260 (skip-chars-forward " \t") | |
261 (setq equalp 0))) | |
262 (- (current-column) equalp langelem-col)) | |
263 ))) | |
264 | |
265 (defun c-lineup-ObjC-method-call (langelem) | |
266 ;; Line up methods args as elisp-mode does with function args: go to | |
267 ;; the position right after the message receiver, and if you are at | |
268 ;; (eolp) indent the current line by a constant offset from the | |
269 ;; opening bracket; otherwise we are looking at the first character | |
270 ;; of the first method call argument, so lineup the current line | |
271 ;; with it. | |
272 (save-excursion | |
273 (let* ((extra (save-excursion | |
274 (back-to-indentation) | |
275 (c-backward-syntactic-ws (cdr langelem)) | |
276 (if (= (preceding-char) ?:) | |
277 (- c-basic-offset) | |
278 0))) | |
279 (open-bracket-pos (cdr langelem)) | |
280 (open-bracket-col (progn | |
281 (goto-char open-bracket-pos) | |
282 (current-column))) | |
283 (target-col (progn | |
284 (forward-char) | |
285 (forward-sexp) | |
286 (skip-chars-forward " \t") | |
287 (if (eolp) | |
288 (+ open-bracket-col c-basic-offset) | |
289 (current-column)))) | |
290 ) | |
291 (- target-col open-bracket-col extra)))) | |
292 | |
293 (defun c-lineup-ObjC-method-args (langelem) | |
294 ;; Line up the colons that separate args. This is done trying to | |
295 ;; align colons vertically. | |
296 (save-excursion | |
297 (let* ((here (c-point 'boi)) | |
298 (curcol (progn (goto-char here) (current-column))) | |
299 (eol (c-point 'eol)) | |
300 (relpos (cdr langelem)) | |
301 (first-col-column (progn | |
302 (goto-char relpos) | |
303 (skip-chars-forward "^:" eol) | |
304 (and (= (following-char) ?:) | |
305 (current-column))))) | |
306 (if (not first-col-column) | |
307 c-basic-offset | |
308 (goto-char here) | |
309 (skip-chars-forward "^:" eol) | |
310 (if (= (following-char) ?:) | |
311 (+ curcol (- first-col-column (current-column))) | |
312 c-basic-offset))))) | |
313 | |
314 (defun c-lineup-ObjC-method-args-2 (langelem) | |
315 ;; Line up the colons that separate args. This is done trying to | |
316 ;; align the colon on the current line with the previous one. | |
317 (save-excursion | |
318 (let* ((here (c-point 'boi)) | |
319 (curcol (progn (goto-char here) (current-column))) | |
320 (eol (c-point 'eol)) | |
321 (relpos (cdr langelem)) | |
322 (prev-col-column (progn | |
323 (skip-chars-backward "^:" relpos) | |
324 (and (= (preceding-char) ?:) | |
325 (- (current-column) 1))))) | |
326 (if (not prev-col-column) | |
327 c-basic-offset | |
328 (goto-char here) | |
329 (skip-chars-forward "^:" eol) | |
330 (if (= (following-char) ?:) | |
331 (+ curcol (- prev-col-column (current-column))) | |
332 c-basic-offset))))) | |
333 | |
334 (defun c-snug-do-while (syntax pos) | |
335 "Dynamically calculate brace hanginess for do-while statements. | |
336 Using this function, `while' clauses that end a `do-while' block will | |
337 remain on the same line as the brace that closes that block. | |
338 | |
339 See `c-hanging-braces-alist' for how to utilize this function as an | |
340 ACTION associated with `block-close' syntax." | |
341 (save-excursion | |
342 (let (langelem) | |
343 (if (and (eq syntax 'block-close) | |
344 (setq langelem (assq 'block-close c-syntactic-context)) | |
345 (progn (goto-char (cdr langelem)) | |
346 (if (= (following-char) ?{) | |
347 (c-safe (forward-sexp -1))) | |
348 (looking-at "\\<do\\>[^_]"))) | |
349 '(before) | |
350 '(before after))))) | |
351 | |
352 (defun c-gnu-impose-minimum () | |
353 "Imposes a minimum indentation for lines inside a top-level construct. | |
354 The variable `c-label-minimum-indentation' specifies the minimum | |
355 indentation amount." | |
356 (let ((non-top-levels '(defun-block-intro statement statement-cont | |
357 statement-block-intro statement-case-intro | |
358 statement-case-open substatement substatement-open | |
359 case-label label do-while-closure else-clause | |
360 )) | |
361 (syntax c-syntactic-context) | |
362 langelem) | |
363 (while syntax | |
364 (setq langelem (car (car syntax)) | |
365 syntax (cdr syntax)) | |
366 ;; don't adjust comment-only lines | |
367 (cond ((eq langelem 'comment-intro) | |
368 (setq syntax nil)) | |
369 ((memq langelem non-top-levels) | |
370 (save-excursion | |
371 (setq syntax nil) | |
372 (back-to-indentation) | |
373 (if (zerop (current-column)) | |
374 (insert (make-string c-label-minimum-indentation 32))) | |
375 )) | |
376 )))) | |
377 | |
378 | |
379 ;; Useful for c-hanging-semi&comma-criteria | |
380 (defun c-semi&comma-inside-parenlist () | |
381 "Determine if a newline should be added after a semicolon. | |
382 If a comma was inserted, no determination is made. If a semicolon was | |
383 inserted inside a parenthesis list, no newline is added otherwise a | |
384 newline is added. In either case, checking is stopped. This supports | |
385 exactly the old newline insertion behavior." | |
386 ;; newline only after semicolon, but only if that semicolon is not | |
387 ;; inside a parenthesis list (e.g. a for loop statement) | |
388 (if (/= last-command-char ?\;) | |
389 nil ; continue checking | |
390 (if (condition-case nil | |
391 (save-excursion | |
392 (up-list -1) | |
393 (/= (following-char) ?\()) | |
394 (error t)) | |
395 t | |
396 'stop))) | |
397 | |
398 | |
399 (provide 'cc-align) | |
400 ;;; cc-align.el ends here |