Mercurial > emacs
annotate lisp/progmodes/cc-align.el @ 23906:ab252c25faf9
(map-y-or-n-p): Ignore input methods.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Wed, 16 Dec 1998 20:45:29 +0000 |
parents | 95f894e68525 |
children | 5b0864259a4b |
rev | line source |
---|---|
18720 | 1 ;;; cc-align.el --- custom indentation functions for CC Mode |
2 | |
20911 | 3 ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc. |
18720 | 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) | |
20141 | 10 ;; Version: See cc-mode.el |
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-vars) | |
32 (require 'cc-engine) | |
33 (require 'cc-langs)) | |
34 | |
35 | |
36 ;; Standard indentation line-ups | |
37 (defun c-lineup-arglist (langelem) | |
38 ;; lineup the current arglist line with the arglist appearing just | |
39 ;; after the containing paren which starts the arglist. | |
40 (save-excursion | |
41 (let* ((containing-sexp | |
42 (save-excursion | |
43 ;; arglist-cont-nonempty gives relpos == | |
44 ;; to boi of containing-sexp paren. This | |
45 ;; is good when offset is +, but bad | |
46 ;; when it is c-lineup-arglist, so we | |
47 ;; have to special case a kludge here. | |
48 (if (memq (car langelem) '(arglist-intro arglist-cont-nonempty)) | |
49 (progn | |
50 (beginning-of-line) | |
51 (backward-up-list 1) | |
52 (skip-chars-forward " \t" (c-point 'eol))) | |
53 (goto-char (cdr langelem))) | |
54 (point))) | |
55 (langelem-col (c-langelem-col langelem t))) | |
56 (if (save-excursion | |
57 (beginning-of-line) | |
58 (looking-at "[ \t]*)")) | |
59 (progn (goto-char (match-end 0)) | |
60 (forward-sexp -1) | |
61 (forward-char 1) | |
62 (c-forward-syntactic-ws) | |
63 (- (current-column) langelem-col)) | |
64 (goto-char containing-sexp) | |
65 (or (eolp) | |
66 (not (memq (char-after) '(?{ ?\( ))) | |
67 (let ((eol (c-point 'eol)) | |
68 (here (progn | |
69 (forward-char 1) | |
70 (skip-chars-forward " \t") | |
71 (point)))) | |
72 (c-forward-syntactic-ws) | |
73 (if (< (point) eol) | |
74 (goto-char here)))) | |
75 (- (current-column) langelem-col) | |
76 )))) | |
77 | |
78 (defun c-lineup-arglist-intro-after-paren (langelem) | |
79 ;; lineup an arglist-intro line to just after the open paren | |
80 (save-excursion | |
81 (let ((langelem-col (c-langelem-col langelem t)) | |
82 (ce-curcol (save-excursion | |
83 (beginning-of-line) | |
84 (backward-up-list 1) | |
85 (skip-chars-forward " \t" (c-point 'eol)) | |
86 (current-column)))) | |
87 (- ce-curcol langelem-col -1)))) | |
88 | |
89 (defun c-lineup-arglist-close-under-paren (langelem) | |
20911 | 90 ;; lineup an arglist-close line under the corresponding open paren |
18720 | 91 (save-excursion |
92 (let ((langelem-col (c-langelem-col langelem t)) | |
93 (ce-curcol (save-excursion | |
94 (beginning-of-line) | |
95 (backward-up-list 1) | |
96 (current-column)))) | |
97 (- ce-curcol langelem-col)))) | |
98 | |
20911 | 99 (defun c-lineup-close-paren (langelem) |
100 ;; Indents the closing paren under its corresponding open paren if | |
101 ;; the open paren is followed by code. If the open paren ends its | |
102 ;; line, no indentation is added. E.g: | |
103 ;; | |
104 ;; main (int, main ( | |
105 ;; char ** int, char ** | |
106 ;; ) <-> ) <- c-lineup-close-paren | |
107 ;; | |
108 ;; Works with any type of paren. | |
109 (save-excursion | |
110 (condition-case nil | |
111 (let (opencol spec) | |
112 (beginning-of-line) | |
113 (backward-up-list 1) | |
114 (setq spec (if (fboundp 'c-looking-at-special-brace-list) | |
115 (c-looking-at-special-brace-list))) | |
116 (if spec (goto-char (car spec))) | |
117 (setq opencol (current-column)) | |
118 (forward-char 1) | |
119 (if spec (progn | |
120 (c-forward-syntactic-ws) | |
121 (forward-char 1))) | |
122 (c-forward-syntactic-ws (c-point 'eol)) | |
123 (if (eolp) | |
124 0 | |
125 (- opencol (c-langelem-col langelem t)))) | |
126 (error 0)))) | |
127 | |
18720 | 128 (defun c-lineup-streamop (langelem) |
129 ;; lineup stream operators | |
130 (save-excursion | |
131 (let ((langelem-col (c-langelem-col langelem))) | |
132 (re-search-forward "<<\\|>>" (c-point 'eol) 'move) | |
133 (goto-char (match-beginning 0)) | |
134 (- (current-column) langelem-col)))) | |
135 | |
136 (defun c-lineup-multi-inher (langelem) | |
137 ;; line up multiple inheritance lines | |
138 (save-excursion | |
139 (let ((eol (c-point 'eol)) | |
140 (here (point)) | |
141 (langelem-col (c-langelem-col langelem))) | |
142 (skip-chars-forward "^:" eol) | |
143 (skip-chars-forward " \t:" eol) | |
144 (if (or (eolp) | |
145 (looking-at c-comment-start-regexp)) | |
146 (c-forward-syntactic-ws here)) | |
147 (- (current-column) langelem-col) | |
148 ))) | |
149 | |
150 (defun c-lineup-java-inher (langelem) | |
151 ;; line up Java implements and extends continuations | |
152 (save-excursion | |
153 (let ((langelem-col (c-langelem-col langelem))) | |
154 (forward-word 1) | |
155 (if (looking-at "[ \t]*$") | |
156 langelem-col | |
157 (c-forward-syntactic-ws) | |
158 (- (current-column) langelem-col))))) | |
159 | |
160 (defun c-lineup-java-throws (langelem) | |
161 ;; lineup func-decl-cont's in Java which are continuations of throws | |
162 ;; declarations. If `throws' starts the previous line, line up to | |
163 ;; just after that keyword. If not, lineup under the previous line. | |
164 (save-excursion | |
165 (let ((iopl (c-point 'iopl)) | |
166 (langelem-col (c-langelem-col langelem t)) | |
167 (extra 0)) | |
168 (back-to-indentation) | |
169 (cond | |
170 ((looking-at "throws[ \t\n]") | |
171 (goto-char (cdr langelem)) | |
172 (setq extra c-basic-offset)) | |
173 ((and (goto-char iopl) | |
174 (looking-at "throws[ \t\n]")) | |
175 (forward-word 1) | |
176 (skip-chars-forward " \t") | |
19296
885a72deec97
(c-lineup-java-throws): Change the `when' clause to an `if-progn'.
Richard M. Stallman <rms@gnu.org>
parents:
19252
diff
changeset
|
177 (if (eolp) |
885a72deec97
(c-lineup-java-throws): Change the `when' clause to an `if-progn'.
Richard M. Stallman <rms@gnu.org>
parents:
19252
diff
changeset
|
178 (progn |
885a72deec97
(c-lineup-java-throws): Change the `when' clause to an `if-progn'.
Richard M. Stallman <rms@gnu.org>
parents:
19252
diff
changeset
|
179 (back-to-indentation) |
885a72deec97
(c-lineup-java-throws): Change the `when' clause to an `if-progn'.
Richard M. Stallman <rms@gnu.org>
parents:
19252
diff
changeset
|
180 (setq extra c-basic-offset)))) |
18720 | 181 (t (goto-char iopl))) |
182 (+ (- (current-column) langelem-col) extra)))) | |
183 | |
20911 | 184 (defun c-indent-one-line-block (langelem) |
185 ;; Adds c-basic-offset to the indentation if the line is a one line | |
186 ;; block, otherwise 0. E.g: | |
187 ;; | |
188 ;; if (n) if (n) | |
189 ;; {m+=n; n=0;} <-> { <- c-indent-one-line-block | |
190 ;; m+=n; n=0; | |
191 ;; } | |
192 (save-excursion | |
193 (let ((eol (progn (end-of-line) (point)))) | |
194 (beginning-of-line) | |
195 (skip-chars-forward " \t") | |
196 (if (and (eq (following-char) ?{) | |
197 (condition-case nil | |
198 (progn (forward-sexp) t) | |
199 (error nil)) | |
200 (<= (point) eol) | |
201 (eq (preceding-char) ?})) | |
202 c-basic-offset | |
203 0)))) | |
204 | |
18720 | 205 (defun c-lineup-C-comments (langelem) |
206 ;; line up C block comment continuation lines | |
207 (save-excursion | |
208 (let ((here (point)) | |
209 (stars (progn (back-to-indentation) | |
210 (skip-chars-forward "*"))) | |
211 (langelem-col (c-langelem-col langelem))) | |
212 (back-to-indentation) | |
213 (if (not (re-search-forward "/\\([*]+\\)" (c-point 'eol) t)) | |
214 (progn | |
215 (if (not (looking-at "[*]+")) | |
216 (progn | |
217 ;; we now have to figure out where this comment begins. | |
218 (goto-char here) | |
219 (back-to-indentation) | |
220 (if (looking-at "[*]+/") | |
221 (progn (goto-char (match-end 0)) | |
222 (forward-comment -1)) | |
223 (goto-char (cdr langelem)) | |
224 (back-to-indentation)))) | |
225 (- (current-column) langelem-col)) | |
226 (if (zerop stars) | |
227 (progn | |
228 (skip-chars-forward " \t") | |
229 (- (current-column) langelem-col)) | |
230 ;; how many stars on comment opening line? if greater than | |
231 ;; on current line, align left. if less than or equal, | |
232 ;; align right. this should also pick up Javadoc style | |
233 ;; comments. | |
234 (if (> (length (match-string 1)) stars) | |
235 (progn | |
236 (back-to-indentation) | |
237 (- (current-column) -1 langelem-col)) | |
238 (- (current-column) stars langelem-col)) | |
239 ))))) | |
240 | |
241 (defun c-lineup-comment (langelem) | |
242 ;; support old behavior for comment indentation. we look at | |
243 ;; c-comment-only-line-offset to decide how to indent comment | |
244 ;; only-lines | |
245 (save-excursion | |
246 (back-to-indentation) | |
247 ;; this highly kludgiforous flag prevents the mapcar over | |
248 ;; c-syntactic-context from entering an infinite loop | |
249 (let ((recurse-prevention-flag (boundp 'recurse-prevention-flag))) | |
250 (cond | |
251 ;; CASE 1: preserve comment-column | |
252 (recurse-prevention-flag 0) | |
253 ((= (current-column) comment-column) | |
254 ;; we have to subtract out all other indentation | |
255 (- comment-column (apply '+ (mapcar 'c-get-offset | |
256 c-syntactic-context)))) | |
257 ;; indent as specified by c-comment-only-line-offset | |
258 ((not (bolp)) | |
259 (or (car-safe c-comment-only-line-offset) | |
260 c-comment-only-line-offset)) | |
261 (t | |
262 (or (cdr-safe c-comment-only-line-offset) | |
263 (car-safe c-comment-only-line-offset) | |
264 -1000)) ;jam it against the left side | |
265 )))) | |
266 | |
267 (defun c-lineup-runin-statements (langelem) | |
268 ;; line up statements in coding standards which place the first | |
269 ;; statement on the same line as the block opening brace. | |
270 (if (eq (char-after (cdr langelem)) ?{) | |
271 (save-excursion | |
272 (let ((langelem-col (c-langelem-col langelem))) | |
273 (forward-char 1) | |
274 (skip-chars-forward " \t") | |
275 (- (current-column) langelem-col))) | |
276 0)) | |
277 | |
278 (defun c-lineup-math (langelem) | |
279 ;; line up math statement-cont after the equals | |
280 (save-excursion | |
281 (let ((equalp (save-excursion | |
282 (goto-char (c-point 'boi)) | |
283 (skip-chars-forward "^=" (c-point 'eol)) | |
284 (and (eq (char-after) ?=) | |
285 (- (point) (c-point 'boi))))) | |
286 (langelem-col (c-langelem-col langelem)) | |
287 donep) | |
288 (while (and (not donep) | |
289 (< (point) (c-point 'eol))) | |
290 (skip-chars-forward "^=" (c-point 'eol)) | |
291 (if (c-in-literal (cdr langelem)) | |
292 (forward-char 1) | |
293 (setq donep t))) | |
294 (if (not (eq (char-after) ?=)) | |
295 ;; there's no equal sign on the line | |
296 c-basic-offset | |
297 ;; calculate indentation column after equals and ws, unless | |
298 ;; our line contains an equals sign | |
299 (if (not equalp) | |
300 (progn | |
301 (forward-char 1) | |
302 (skip-chars-forward " \t") | |
303 (setq equalp 0))) | |
304 (- (current-column) equalp langelem-col)) | |
305 ))) | |
306 | |
307 (defun c-lineup-ObjC-method-call (langelem) | |
308 ;; Line up methods args as elisp-mode does with function args: go to | |
309 ;; the position right after the message receiver, and if you are at | |
310 ;; (eolp) indent the current line by a constant offset from the | |
311 ;; opening bracket; otherwise we are looking at the first character | |
312 ;; of the first method call argument, so lineup the current line | |
313 ;; with it. | |
314 (save-excursion | |
315 (let* ((extra (save-excursion | |
316 (back-to-indentation) | |
317 (c-backward-syntactic-ws (cdr langelem)) | |
318 (if (eq (char-before) ?:) | |
319 (- c-basic-offset) | |
320 0))) | |
321 (open-bracket-pos (cdr langelem)) | |
322 (open-bracket-col (progn | |
323 (goto-char open-bracket-pos) | |
324 (current-column))) | |
325 (target-col (progn | |
326 (forward-char) | |
327 (forward-sexp) | |
328 (skip-chars-forward " \t") | |
329 (if (eolp) | |
330 (+ open-bracket-col c-basic-offset) | |
331 (current-column)))) | |
332 ) | |
333 (- target-col open-bracket-col extra)))) | |
334 | |
335 (defun c-lineup-ObjC-method-args (langelem) | |
336 ;; Line up the colons that separate args. This is done trying to | |
337 ;; align colons vertically. | |
338 (save-excursion | |
339 (let* ((here (c-point 'boi)) | |
340 (curcol (progn (goto-char here) (current-column))) | |
341 (eol (c-point 'eol)) | |
342 (relpos (cdr langelem)) | |
343 (first-col-column (progn | |
344 (goto-char relpos) | |
345 (skip-chars-forward "^:" eol) | |
346 (and (eq (char-after) ?:) | |
347 (current-column))))) | |
348 (if (not first-col-column) | |
349 c-basic-offset | |
350 (goto-char here) | |
351 (skip-chars-forward "^:" eol) | |
352 (if (eq (char-after) ?:) | |
353 (+ curcol (- first-col-column (current-column))) | |
354 c-basic-offset))))) | |
355 | |
356 (defun c-lineup-ObjC-method-args-2 (langelem) | |
357 ;; Line up the colons that separate args. This is done trying to | |
358 ;; align the colon on the current line with the previous one. | |
359 (save-excursion | |
360 (let* ((here (c-point 'boi)) | |
361 (curcol (progn (goto-char here) (current-column))) | |
362 (eol (c-point 'eol)) | |
363 (relpos (cdr langelem)) | |
364 (prev-col-column (progn | |
365 (skip-chars-backward "^:" relpos) | |
366 (and (eq (char-before) ?:) | |
367 (- (current-column) 1))))) | |
368 (if (not prev-col-column) | |
369 c-basic-offset | |
370 (goto-char here) | |
371 (skip-chars-forward "^:" eol) | |
372 (if (eq (char-after) ?:) | |
373 (+ curcol (- prev-col-column (current-column))) | |
374 c-basic-offset))))) | |
375 | |
20911 | 376 (defun c-lineup-dont-change (langelem) |
377 ;; Do not change the indentation of the current line | |
378 (save-excursion | |
379 (back-to-indentation) | |
380 (current-column))) | |
381 | |
382 | |
383 | |
18720 | 384 (defun c-snug-do-while (syntax pos) |
385 "Dynamically calculate brace hanginess for do-while statements. | |
386 Using this function, `while' clauses that end a `do-while' block will | |
387 remain on the same line as the brace that closes that block. | |
388 | |
389 See `c-hanging-braces-alist' for how to utilize this function as an | |
390 ACTION associated with `block-close' syntax." | |
391 (save-excursion | |
392 (let (langelem) | |
393 (if (and (eq syntax 'block-close) | |
394 (setq langelem (assq 'block-close c-syntactic-context)) | |
395 (progn (goto-char (cdr langelem)) | |
396 (if (eq (char-after) ?{) | |
397 (c-safe (forward-sexp -1))) | |
398 (looking-at "\\<do\\>[^_]"))) | |
399 '(before) | |
400 '(before after))))) | |
401 | |
402 (defun c-gnu-impose-minimum () | |
403 "Imposes a minimum indentation for lines inside a top-level construct. | |
404 The variable `c-label-minimum-indentation' specifies the minimum | |
405 indentation amount." | |
406 (let ((non-top-levels '(defun-block-intro statement statement-cont | |
407 statement-block-intro statement-case-intro | |
408 statement-case-open substatement substatement-open | |
409 case-label label do-while-closure else-clause | |
410 )) | |
411 (syntax c-syntactic-context) | |
412 langelem) | |
413 (while syntax | |
414 (setq langelem (car (car syntax)) | |
415 syntax (cdr syntax)) | |
416 ;; don't adjust comment-only lines | |
417 (cond ((eq langelem 'comment-intro) | |
418 (setq syntax nil)) | |
419 ((memq langelem non-top-levels) | |
420 (save-excursion | |
421 (setq syntax nil) | |
422 (back-to-indentation) | |
423 (if (zerop (current-column)) | |
424 (insert (make-string c-label-minimum-indentation 32))) | |
425 )) | |
426 )))) | |
427 | |
428 | |
429 ;; Useful for c-hanging-semi&comma-criteria | |
430 (defun c-semi&comma-inside-parenlist () | |
20911 | 431 "Controls newline insertion after semicolons in parenthesis lists. |
18720 | 432 If a comma was inserted, no determination is made. If a semicolon was |
433 inserted inside a parenthesis list, no newline is added otherwise a | |
434 newline is added. In either case, checking is stopped. This supports | |
435 exactly the old newline insertion behavior." | |
436 ;; newline only after semicolon, but only if that semicolon is not | |
437 ;; inside a parenthesis list (e.g. a for loop statement) | |
438 (if (not (eq last-command-char ?\;)) | |
439 nil ; continue checking | |
440 (if (condition-case nil | |
441 (save-excursion | |
442 (up-list -1) | |
443 (not (eq (char-after) ?\())) | |
444 (error t)) | |
445 t | |
446 'stop))) | |
447 | |
20911 | 448 ;; Suppresses newlines before non-blank lines |
449 (defun c-semi&comma-no-newlines-before-nonblanks () | |
450 "Controls newline insertion after semicolons. | |
451 If a comma was inserted, no determination is made. If a semicolon was | |
452 inserted, and the following line is not blank, no newline is inserted. | |
453 Otherwise, no determination is made." | |
454 (save-excursion | |
455 (if (and (= last-command-char ?\;) | |
456 ;;(/= (point-max) | |
457 ;; (save-excursion (skip-syntax-forward " ") (point)) | |
458 (zerop (forward-line 1)) | |
459 (not (looking-at "^[ \t]*$"))) | |
460 'stop | |
461 nil))) | |
462 | |
463 ;; Suppresses new lines after semicolons in one-liners methods | |
464 (defun c-semi&comma-no-newlines-for-oneline-inliners () | |
465 "Controls newline insertion after semicolons for some one-line methods. | |
466 If a comma was inserted, no determination is made. Newlines are | |
467 suppressed in one-liners, if the line is an in-class inline function. | |
468 For other semicolon contexts, no determination is made." | |
469 (let ((syntax (c-guess-basic-syntax)) | |
470 (bol (save-excursion | |
471 (if (c-safe (up-list -1) t) | |
472 (c-point 'bol) | |
473 -1)))) | |
474 (if (and (eq last-command-char ?\;) | |
475 (eq (car (car syntax)) 'inclass) | |
476 (eq (car (car (cdr syntax))) 'topmost-intro) | |
477 (= (c-point 'bol) bol)) | |
478 'stop | |
479 nil))) | |
480 | |
18720 | 481 |
482 (provide 'cc-align) | |
483 ;;; cc-align.el ends here |