Mercurial > emacs
annotate lisp/progmodes/c-mode.el @ 662:8a533acedb77
*** empty log message ***
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Sat, 30 May 1992 23:54:21 +0000 |
parents | e6b49c51a9bb |
children | 58b7fc91b74a |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
472
diff
changeset
|
1 ;;; c-mode.el --- C code editing commands for Emacs |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
472
diff
changeset
|
2 |
453 | 3 ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc. |
4 | |
5 ;; This file is part of GNU Emacs. | |
6 | |
7 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 ;; it under the terms of the GNU General Public License as published by | |
9 ;; the Free Software Foundation; either version 1, or (at your option) | |
10 ;; any later version. | |
11 | |
12 ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 ;; GNU General Public License for more details. | |
16 | |
17 ;; You should have received a copy of the GNU General Public License | |
18 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | |
21 | |
22 (defvar c-mode-abbrev-table nil | |
23 "Abbrev table in use in C mode.") | |
24 (define-abbrev-table 'c-mode-abbrev-table ()) | |
25 | |
26 (defvar c-mode-map () | |
27 "Keymap used in C mode.") | |
28 (if c-mode-map | |
29 () | |
30 (setq c-mode-map (make-sparse-keymap)) | |
31 (define-key c-mode-map "{" 'electric-c-brace) | |
32 (define-key c-mode-map "}" 'electric-c-brace) | |
33 (define-key c-mode-map ";" 'electric-c-semi) | |
34 (define-key c-mode-map "#" 'electric-c-sharp-sign) | |
35 (define-key c-mode-map ":" 'electric-c-terminator) | |
36 (define-key c-mode-map "\e\C-h" 'mark-c-function) | |
37 (define-key c-mode-map "\e\C-q" 'indent-c-exp) | |
38 (define-key c-mode-map "\ea" 'c-beginning-of-statement) | |
39 (define-key c-mode-map "\ee" 'c-end-of-statement) | |
40 (define-key c-mode-map "\eq" 'c-fill-paragraph) | |
41 (define-key c-mode-map "\177" 'backward-delete-char-untabify) | |
42 (define-key c-mode-map "\t" 'c-indent-command)) | |
43 | |
44 ;; cmacexp is lame because it uses no preprocessor symbols. | |
45 ;; It isn't very extensible either -- hardcodes /lib/cpp. | |
46 (autoload 'c-macro-expand "cmacexp" | |
47 "Display the result of expanding all C macros occurring in the region. | |
48 The expansion is entirely correct because it uses the C preprocessor." | |
49 t) | |
50 | |
51 (defvar c-mode-syntax-table nil | |
52 "Syntax table in use in C-mode buffers.") | |
53 | |
54 (if c-mode-syntax-table | |
55 () | |
56 (setq c-mode-syntax-table (make-syntax-table)) | |
57 (modify-syntax-entry ?\\ "\\" c-mode-syntax-table) | |
58 (modify-syntax-entry ?/ ". 14" c-mode-syntax-table) | |
59 (modify-syntax-entry ?* ". 23" c-mode-syntax-table) | |
60 (modify-syntax-entry ?+ "." c-mode-syntax-table) | |
61 (modify-syntax-entry ?- "." c-mode-syntax-table) | |
62 (modify-syntax-entry ?= "." c-mode-syntax-table) | |
63 (modify-syntax-entry ?% "." c-mode-syntax-table) | |
64 (modify-syntax-entry ?< "." c-mode-syntax-table) | |
65 (modify-syntax-entry ?> "." c-mode-syntax-table) | |
66 (modify-syntax-entry ?& "." c-mode-syntax-table) | |
67 (modify-syntax-entry ?| "." c-mode-syntax-table) | |
68 (modify-syntax-entry ?\' "\"" c-mode-syntax-table)) | |
69 | |
70 (defconst c-indent-level 2 | |
71 "*Indentation of C statements with respect to containing block.") | |
72 (defconst c-brace-imaginary-offset 0 | |
73 "*Imagined indentation of a C open brace that actually follows a statement.") | |
74 (defconst c-brace-offset 0 | |
75 "*Extra indentation for braces, compared with other text in same context.") | |
76 (defconst c-argdecl-indent 5 | |
77 "*Indentation level of declarations of C function arguments.") | |
78 (defconst c-label-offset -2 | |
79 "*Offset of C label lines and case statements relative to usual indentation.") | |
80 (defconst c-continued-statement-offset 2 | |
81 "*Extra indent for lines not starting new statements.") | |
82 (defconst c-continued-brace-offset 0 | |
83 "*Extra indent for substatements that start with open-braces. | |
84 This is in addition to c-continued-statement-offset.") | |
85 (defconst c-style-alist | |
86 '(("GNU" | |
87 (c-indent-level . 2) | |
88 (c-argdecl-indent . 5) | |
89 (c-brace-offset . 0) | |
90 (c-label-offset . -2) | |
91 (c-continued-statement-offset . 2)) | |
92 ("K&R" | |
93 (c-indent-level . 5) | |
94 (c-argdecl-indent . 0) | |
95 (c-brace-offset . -5) | |
96 (c-label-offset . -5) | |
97 (c-continued-statement-offset . 5)) | |
98 ("BSD" | |
99 (c-indent-level . 4) | |
100 (c-argdecl-indent . 4) | |
101 (c-brace-offset . -4) | |
102 (c-label-offset . -4) | |
103 (c-continued-statement-offset . 4)) | |
472 | 104 (C++ |
105 (c-indent-level . 4) | |
106 (c-continued-statement-offset . 4) | |
107 (c-brace-offset . -4) | |
108 (c-argdecl-indent . 0) | |
109 (c-label-offset . -4) | |
110 (c-auto-newline . t)) | |
453 | 111 ("Whitesmith" |
112 (c-indent-level . 4) | |
113 (c-argdecl-indent . 4) | |
114 (c-brace-offset . 0) | |
115 (c-label-offset . -4) | |
116 (c-continued-statement-offset . 4)))) | |
117 | |
118 (defconst c-auto-newline nil | |
119 "*Non-nil means automatically newline before and after braces, | |
120 and after colons and semicolons, inserted in C code. | |
121 If you do not want a leading newline before braces then use: | |
122 (define-key c-mode-map "{" 'electric-c-semi)") | |
123 | |
124 (defconst c-tab-always-indent t | |
125 "*Non-nil means TAB in C mode should always reindent the current line, | |
126 regardless of where in the line point is when the TAB command is used.") | |
127 | |
128 (defun set-c-style (&optional style) | |
129 "Set up the c-mode style variables from the c-style variable or if | |
130 STYLE argument is given, use that. It makes the c indentation style | |
131 variables buffer local." | |
132 (interactive) | |
133 (let ((c-styles (mapcar 'car c-style-alist))) | |
134 (if (interactive-p) | |
135 (setq style | |
136 (let ((style-string ; get style name with completion | |
137 (completing-read | |
138 (format "Set c mode indentation style to (default %s): " | |
139 default-c-style) | |
140 (vconcat c-styles) | |
141 (function (lambda (arg) (memq arg c-styles))) | |
142 ))) | |
143 (if (string-equal "" style-string) | |
144 default-c-style | |
145 (intern style-string)) | |
146 ))) | |
147 (setq style (or style c-style)) ; use c-style if style is nil | |
148 (make-local-variable 'c-style) | |
149 (if (memq style c-styles) | |
150 (setq c-style style) | |
151 (error "Bad c style: %s" style) | |
152 ) | |
153 (message "c-style: %s" c-style) | |
154 ; finally, set the indentation style variables making each one local | |
155 (mapcar (function (lambda (c-style-pair) | |
156 (make-local-variable (car c-style-pair)) | |
157 (set (car c-style-pair) | |
158 (cdr c-style-pair)))) | |
159 (cdr (assq c-style c-style-alist))) | |
160 c-style | |
161 ) | |
162 ) | |
163 | |
164 (defun c-mode () | |
165 "Major mode for editing C code. | |
166 Expression and list commands understand all C brackets. | |
167 Tab indents for C code. | |
168 Comments are delimited with /* ... */. | |
169 Paragraphs are separated by blank lines only. | |
170 Delete converts tabs to spaces as it moves back. | |
171 \\{c-mode-map} | |
172 Variables controlling indentation style: | |
173 c-tab-always-indent | |
174 Non-nil means TAB in C mode should always reindent the current line, | |
175 regardless of where in the line point is when the TAB command is used. | |
176 c-auto-newline | |
177 Non-nil means automatically newline before and after braces, | |
178 and after colons and semicolons, inserted in C code. | |
179 c-indent-level | |
180 Indentation of C statements within surrounding block. | |
181 The surrounding block's indentation is the indentation | |
182 of the line on which the open-brace appears. | |
183 c-continued-statement-offset | |
184 Extra indentation given to a substatement, such as the | |
185 then-clause of an if or body of a while. | |
186 c-continued-brace-offset | |
187 Extra indentation given to a brace that starts a substatement. | |
188 This is in addition to c-continued-statement-offset. | |
189 c-brace-offset | |
190 Extra indentation for line if it starts with an open brace. | |
191 c-brace-imaginary-offset | |
192 An open brace following other text is treated as if it were | |
193 this far to the right of the start of its line. | |
194 c-argdecl-indent | |
195 Indentation level of declarations of C function arguments. | |
196 c-label-offset | |
197 Extra indentation for line that is a label, or case or default. | |
198 | |
199 Settings for K&R and BSD indentation styles are | |
200 c-indent-level 5 8 | |
201 c-continued-statement-offset 5 8 | |
202 c-brace-offset -5 -8 | |
203 c-argdecl-indent 0 8 | |
204 c-label-offset -5 -8 | |
205 | |
206 Turning on C mode calls the value of the variable c-mode-hook with no args, | |
207 if that value is non-nil." | |
208 (interactive) | |
209 (kill-all-local-variables) | |
210 (use-local-map c-mode-map) | |
211 (setq major-mode 'c-mode) | |
212 (setq mode-name "C") | |
213 (setq local-abbrev-table c-mode-abbrev-table) | |
214 (set-syntax-table c-mode-syntax-table) | |
215 (make-local-variable 'paragraph-start) | |
216 (setq paragraph-start (concat "^$\\|" page-delimiter)) | |
217 (make-local-variable 'paragraph-separate) | |
218 (setq paragraph-separate paragraph-start) | |
219 (make-local-variable 'paragraph-ignore-fill-prefix) | |
220 (setq paragraph-ignore-fill-prefix t) | |
221 (make-local-variable 'indent-line-function) | |
222 (setq indent-line-function 'c-indent-line) | |
223 (make-local-variable 'indent-region-function) | |
224 (setq indent-region-function 'c-indent-region) | |
225 (make-local-variable 'require-final-newline) | |
226 (setq require-final-newline t) | |
227 (make-local-variable 'comment-start) | |
228 (setq comment-start "/* ") | |
229 (make-local-variable 'comment-end) | |
230 (setq comment-end " */") | |
231 (make-local-variable 'comment-column) | |
232 (setq comment-column 32) | |
233 (make-local-variable 'comment-start-skip) | |
234 (setq comment-start-skip "/\\*+ *") | |
235 (make-local-variable 'comment-indent-hook) | |
236 (setq comment-indent-hook 'c-comment-indent) | |
237 (make-local-variable 'parse-sexp-ignore-comments) | |
238 (setq parse-sexp-ignore-comments t) | |
239 (run-hooks 'c-mode-hook)) | |
240 | |
241 ;; This is used by indent-for-comment | |
242 ;; to decide how much to indent a comment in C code | |
243 ;; based on its context. | |
244 (defun c-comment-indent () | |
245 (if (looking-at "^/\\*") | |
246 0 ;Existing comment at bol stays there. | |
247 (let ((opoint (point))) | |
248 (save-excursion | |
249 (beginning-of-line) | |
250 (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)") | |
251 ;; A comment following a solitary close-brace | |
252 ;; should have only one space. | |
253 (search-forward "}") | |
254 (1+ (current-column))) | |
255 ((or (looking-at "^#[ \t]*endif[ \t]*") | |
256 (looking-at "^#[ \t]*else[ \t]*")) | |
257 7) ;2 spaces after #endif | |
258 ((progn | |
259 (goto-char opoint) | |
260 (skip-chars-backward " \t") | |
261 (and (= comment-column 0) (bolp))) | |
262 ;; If comment-column is 0, and nothing but space | |
263 ;; before the comment, align it at 0 rather than 1. | |
264 0) | |
265 (t | |
266 (max (1+ (current-column)) ;Else indent at comment column | |
267 comment-column))))))) ; except leave at least one space. | |
268 | |
269 (defun c-fill-paragraph (&optional arg) | |
270 "Like \\[fill-paragraph] but handle C comments. | |
271 If point is inside a comment, the current paragraph of the comment | |
272 is filled, preserving the comment indentation or line-starting decorations." | |
273 (interactive "P") | |
274 (let ((first-line | |
275 (save-excursion | |
276 (beginning-of-line) | |
277 (skip-chars-forward " \t") | |
278 (looking-at comment-start-skip)))) | |
279 (if (or first-line | |
280 (eq (calculate-c-indent) t)) | |
281 ;; Inside a comment: fill one comment paragraph. | |
282 (let ((fill-prefix | |
283 ;; The prefix for each line of this paragraph | |
284 ;; is the appropriate part of the start of this line, | |
285 ;; up to the column at which text should be indented. | |
286 (save-excursion | |
287 (beginning-of-line) | |
288 (if (looking-at "[ \t]*/\\*.*\\*/") | |
289 (progn (re-search-forward comment-start-skip) | |
290 (make-string (current-column) ?\ )) | |
291 (if first-line (forward-line 1)) | |
292 (buffer-substring (point) | |
293 (progn | |
294 (move-to-column | |
295 (calculate-c-indent-within-comment t) | |
296 t) | |
297 (point)))))) | |
298 (paragraph-start | |
299 ;; Lines containing just a comment start or just an end | |
300 ;; should not be filled into paragraphs they are next to. | |
301 (concat paragraph-start | |
302 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[^ \t/*]")) | |
303 (paragraph-separate | |
304 (concat paragraph-separate | |
305 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[^ \t/*]"))) | |
306 (save-restriction | |
307 ;; Don't fill the comment together with the code following it. | |
308 (narrow-to-region (point-min) | |
309 (save-excursion | |
310 (search-forward "*/" nil 'move) | |
311 (forward-line 1) | |
312 (point))) | |
313 (fill-paragraph arg) | |
314 (save-excursion | |
315 (search-forward "*/") | |
316 (beginning-of-line) | |
317 (if (looking-at "[ \t]*\\*/") | |
318 (delete-indentation))))) | |
319 ;; Outside of comments: do ordinary filling. | |
320 (fill-paragraph arg)))) | |
321 | |
322 (defun electric-c-brace (arg) | |
323 "Insert character and correct line's indentation." | |
324 (interactive "P") | |
325 (let (insertpos) | |
326 (if (and (not arg) | |
327 (eolp) | |
328 (or (save-excursion | |
329 (skip-chars-backward " \t") | |
330 (bolp)) | |
331 (if c-auto-newline (progn (c-indent-line) (newline) t) nil))) | |
332 (progn | |
333 (insert last-command-char) | |
334 (c-indent-line) | |
335 (if c-auto-newline | |
336 (progn | |
337 (newline) | |
338 ;; (newline) may have done auto-fill | |
339 (setq insertpos (- (point) 2)) | |
340 (c-indent-line))) | |
341 (save-excursion | |
342 (if insertpos (goto-char (1+ insertpos))) | |
343 (delete-char -1)))) | |
344 (if insertpos | |
345 (save-excursion | |
346 (goto-char insertpos) | |
347 (self-insert-command (prefix-numeric-value arg))) | |
348 (self-insert-command (prefix-numeric-value arg))))) | |
349 | |
350 (defun electric-c-sharp-sign (arg) | |
351 "Insert character and correct line's indentation." | |
352 (interactive "P") | |
353 (if (save-excursion | |
354 (skip-chars-backward " \t") | |
355 (bolp)) | |
356 (let ((c-auto-newline nil)) | |
357 (electric-c-terminator arg)) | |
358 (self-insert-command (prefix-numeric-value arg)))) | |
359 | |
360 (defun electric-c-semi (arg) | |
361 "Insert character and correct line's indentation." | |
362 (interactive "P") | |
363 (if c-auto-newline | |
364 (electric-c-terminator arg) | |
365 (self-insert-command (prefix-numeric-value arg)))) | |
366 | |
367 (defun electric-c-terminator (arg) | |
368 "Insert character and correct line's indentation." | |
369 (interactive "P") | |
370 (let (insertpos (end (point))) | |
371 (if (and (not arg) (eolp) | |
372 (not (save-excursion | |
373 (beginning-of-line) | |
374 (skip-chars-forward " \t") | |
375 (or (= (following-char) ?#) | |
376 ;; Colon is special only after a label, or case .... | |
377 ;; So quickly rule out most other uses of colon | |
378 ;; and do no indentation for them. | |
379 (and (eq last-command-char ?:) | |
380 (not (looking-at "case[ \t'/(]\\|default\\>")) | |
381 (save-excursion | |
382 (skip-chars-forward "a-zA-Z0-9_$") | |
383 (skip-chars-forward " \t") | |
384 (< (point) end))) | |
385 (progn | |
386 (beginning-of-defun) | |
387 (let ((pps (parse-partial-sexp (point) end))) | |
388 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))))) | |
389 (progn | |
390 (insert last-command-char) | |
391 (c-indent-line) | |
392 (and c-auto-newline | |
393 (not (c-inside-parens-p)) | |
394 (progn | |
395 (newline) | |
396 ;; (newline) may have done auto-fill | |
397 (setq insertpos (- (point) 2)) | |
398 (c-indent-line))) | |
399 (save-excursion | |
400 (if insertpos (goto-char (1+ insertpos))) | |
401 (delete-char -1)))) | |
402 (if insertpos | |
403 (save-excursion | |
404 (goto-char insertpos) | |
405 (self-insert-command (prefix-numeric-value arg))) | |
406 (self-insert-command (prefix-numeric-value arg))))) | |
407 | |
408 (defun c-inside-parens-p () | |
409 (condition-case () | |
410 (save-excursion | |
411 (save-restriction | |
412 (narrow-to-region (point) | |
413 (progn (beginning-of-defun) (point))) | |
414 (goto-char (point-max)) | |
415 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\())) | |
416 (error nil))) | |
417 | |
418 (defun c-indent-command (&optional whole-exp) | |
419 "Indent current line as C code, or in some cases insert a tab character. | |
420 If `c-tab-always-indent' is non-nil (the default), always indent current line. | |
421 Otherwise, indent the current line only if point is at the left margin or | |
422 in the line's indentation; otherwise insert a tab. | |
423 | |
424 A numeric argument, regardless of its value, means indent rigidly all the | |
425 lines of the expression starting after point so that this line becomes | |
426 properly indented. The relative indentation among the lines of the | |
427 expression are preserved." | |
428 (interactive "P") | |
429 (if whole-exp | |
430 ;; If arg, always indent this line as C | |
431 ;; and shift remaining lines of expression the same amount. | |
432 (let ((shift-amt (c-indent-line)) | |
433 beg end) | |
434 (save-excursion | |
435 (if c-tab-always-indent | |
436 (beginning-of-line)) | |
437 ;; Find beginning of following line. | |
438 (save-excursion | |
439 (forward-line 1) (setq beg (point))) | |
440 ;; Find first beginning-of-sexp for sexp extending past this line. | |
441 (while (< (point) beg) | |
442 (forward-sexp 1) | |
443 (setq end (point)) | |
444 (skip-chars-forward " \t\n"))) | |
445 (if (> end beg) | |
446 (indent-code-rigidly beg end shift-amt "#"))) | |
447 (if (and (not c-tab-always-indent) | |
448 (save-excursion | |
449 (skip-chars-backward " \t") | |
450 (not (bolp)))) | |
451 (insert-tab) | |
452 (c-indent-line)))) | |
453 | |
454 (defun c-indent-line () | |
455 "Indent current line as C code. | |
456 Return the amount the indentation changed by." | |
457 (let ((indent (calculate-c-indent nil)) | |
458 beg shift-amt | |
459 (case-fold-search nil) | |
460 (pos (- (point-max) (point)))) | |
461 (beginning-of-line) | |
462 (setq beg (point)) | |
463 (cond ((eq indent nil) | |
464 (setq indent (current-indentation))) | |
465 ((eq indent t) | |
466 (setq indent (calculate-c-indent-within-comment))) | |
467 ((looking-at "[ \t]*#") | |
468 (setq indent 0)) | |
469 (t | |
470 (skip-chars-forward " \t") | |
471 (if (listp indent) (setq indent (car indent))) | |
472 (cond ((or (looking-at "case[ \t'/(]\\|default\\>") | |
473 (and (looking-at "[A-Za-z]") | |
474 (save-excursion | |
475 (forward-sexp 1) | |
476 (looking-at ":")))) | |
477 (setq indent (max 1 (+ indent c-label-offset)))) | |
478 ((and (looking-at "else\\b") | |
479 (not (looking-at "else\\s_"))) | |
480 (setq indent (save-excursion | |
481 (c-backward-to-start-of-if) | |
482 (current-indentation)))) | |
483 ((looking-at "}[ \t]*else") | |
484 (setq indent (save-excursion | |
485 (forward-char) | |
486 (backward-sexp) | |
487 (current-indentation)))) | |
488 ((and (looking-at "while\\b") | |
489 (save-excursion | |
490 (c-backward-to-start-of-do))) | |
491 ;; This is a `while' that ends a do-while. | |
492 (setq indent (save-excursion | |
493 (c-backward-to-start-of-do) | |
494 (current-indentation)))) | |
495 ((= (following-char) ?}) | |
496 (setq indent (- indent c-indent-level))) | |
497 ((= (following-char) ?{) | |
498 (setq indent (+ indent c-brace-offset)))))) | |
499 (skip-chars-forward " \t") | |
500 (setq shift-amt (- indent (current-column))) | |
501 (if (zerop shift-amt) | |
502 (if (> (- (point-max) pos) (point)) | |
503 (goto-char (- (point-max) pos))) | |
504 (delete-region beg (point)) | |
505 (indent-to indent) | |
506 ;; If initial point was within line's indentation, | |
507 ;; position after the indentation. Else stay at same point in text. | |
508 (if (> (- (point-max) pos) (point)) | |
509 (goto-char (- (point-max) pos)))) | |
510 shift-amt)) | |
511 | |
512 (defun calculate-c-indent (&optional parse-start) | |
513 "Return appropriate indentation for current line as C code. | |
514 In usual case returns an integer: the column to indent to. | |
515 Returns nil if line starts inside a string, t if in a comment." | |
516 (save-excursion | |
517 (beginning-of-line) | |
518 (let ((indent-point (point)) | |
519 (case-fold-search nil) | |
520 state | |
521 containing-sexp) | |
522 (if parse-start | |
523 (goto-char parse-start) | |
524 (beginning-of-defun)) | |
525 (while (< (point) indent-point) | |
526 (setq parse-start (point)) | |
527 (setq state (parse-partial-sexp (point) indent-point 0)) | |
528 (setq containing-sexp (car (cdr state)))) | |
529 (cond ((or (nth 3 state) (nth 4 state)) | |
530 ;; return nil or t if should not change this line | |
531 (nth 4 state)) | |
532 ((null containing-sexp) | |
533 ;; Line is at top level. May be data or function definition, | |
534 ;; or may be function argument declaration. | |
535 ;; Indent like the previous top level line | |
536 ;; unless that ends in a closeparen without semicolon, | |
537 ;; in which case this line is the first argument decl. | |
538 (goto-char indent-point) | |
539 (skip-chars-forward " \t") | |
540 (if (= (following-char) ?{) | |
541 0 ; Unless it starts a function body | |
542 (c-backward-to-noncomment (or parse-start (point-min))) | |
543 ;; Look at previous line that's at column 0 | |
544 ;; to determine whether we are in top-level decls | |
545 ;; or function's arg decls. Set basic-indent accordingly. | |
546 (let ((basic-indent | |
547 (save-excursion | |
548 (re-search-backward "^[^ \^L\t\n#]" nil 'move) | |
549 (if (and (looking-at "\\sw\\|\\s_") | |
550 (looking-at "[^\"\n=]*(") | |
551 (progn | |
552 (goto-char (1- (match-end 0))) | |
553 (forward-sexp 1) | |
554 (skip-chars-forward " \t\f") | |
555 (and (< (point) indent-point) | |
556 (not (memq (following-char) | |
557 '(?\, ?\;)))))) | |
558 c-argdecl-indent 0)))) | |
559 basic-indent))) | |
560 | |
561 ;; ;; Now add a little if this is a continuation line. | |
562 ;; (+ basic-indent (if (or (bobp) | |
563 ;; (memq (preceding-char) '(?\) ?\; ?\})) | |
564 ;; ;; Line with zero indentation | |
565 ;; ;; is probably the return-type | |
566 ;; ;; of a function definition, | |
567 ;; ;; so following line is function name. | |
568 ;; (= (current-indentation) 0)) | |
569 ;; 0 c-continued-statement-offset)) | |
570 | |
571 ((/= (char-after containing-sexp) ?{) | |
572 ;; line is expression, not statement: | |
573 ;; indent to just after the surrounding open. | |
574 (goto-char (1+ containing-sexp)) | |
575 (current-column)) | |
576 (t | |
577 ;; Statement level. Is it a continuation or a new statement? | |
578 ;; Find previous non-comment character. | |
579 (goto-char indent-point) | |
580 (c-backward-to-noncomment containing-sexp) | |
581 ;; Back up over label lines, since they don't | |
582 ;; affect whether our line is a continuation. | |
583 (while (or (eq (preceding-char) ?\,) | |
584 (and (eq (preceding-char) ?:) | |
585 (or (eq (char-after (- (point) 2)) ?\') | |
586 (memq (char-syntax (char-after (- (point) 2))) | |
587 '(?w ?_))))) | |
588 (if (eq (preceding-char) ?\,) | |
589 (progn (forward-char -1) | |
590 (c-backward-to-start-of-continued-exp containing-sexp))) | |
591 (beginning-of-line) | |
592 (c-backward-to-noncomment containing-sexp)) | |
593 ;; Check for a preprocessor statement or its continuation lines. | |
594 ;; Move back to end of previous non-preprocessor line. | |
595 (let ((found (point)) stop) | |
596 (while (not stop) | |
597 (cond ((save-excursion (end-of-line 0) | |
598 (= (preceding-char) ?\\)) | |
599 (end-of-line 0)) | |
600 ;; This line is not preceded by a backslash. | |
601 ;; So either it starts a preprocessor command | |
602 ;; or any following continuation lines | |
603 ;; should not be skipped. | |
604 ((progn (beginning-of-line) (= (following-char) ?#)) | |
605 (end-of-line 0) | |
606 (setq found (point))) | |
607 (t (setq stop t)))) | |
608 (goto-char found)) | |
609 ;; Now we get the answer. | |
610 (if (and (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?\{))) | |
611 ;; But don't treat a line with a close-brace | |
612 ;; as a continuation. It is probably the | |
613 ;; end of an enum type declaration. | |
614 (save-excursion | |
615 (goto-char indent-point) | |
616 (skip-chars-forward " \t") | |
617 (not (= (following-char) ?})))) | |
618 ;; This line is continuation of preceding line's statement; | |
619 ;; indent c-continued-statement-offset more than the | |
620 ;; previous line of the statement. | |
621 (progn | |
622 (c-backward-to-start-of-continued-exp containing-sexp) | |
623 (+ c-continued-statement-offset (current-column) | |
624 (if (save-excursion (goto-char indent-point) | |
625 (skip-chars-forward " \t") | |
626 (eq (following-char) ?{)) | |
627 c-continued-brace-offset 0))) | |
628 ;; This line starts a new statement. | |
629 ;; Position following last unclosed open. | |
630 (goto-char containing-sexp) | |
631 ;; Is line first statement after an open-brace? | |
632 (or | |
633 ;; If no, find that first statement and indent like it. | |
634 (save-excursion | |
635 (forward-char 1) | |
636 (let ((colon-line-end 0)) | |
637 (while (progn (skip-chars-forward " \t\n") | |
638 (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:")) | |
639 ;; Skip over comments and labels following openbrace. | |
640 (cond ((= (following-char) ?\#) | |
641 (forward-line 1)) | |
642 ((= (following-char) ?\/) | |
643 (forward-char 2) | |
644 (search-forward "*/" nil 'move)) | |
645 ;; case or label: | |
646 (t | |
647 (save-excursion (end-of-line) | |
648 (setq colon-line-end (point))) | |
649 (search-forward ":")))) | |
650 ;; The first following code counts | |
651 ;; if it is before the line we want to indent. | |
652 (and (< (point) indent-point) | |
653 (if (> colon-line-end (point)) | |
654 (- (current-indentation) c-label-offset) | |
655 (current-column))))) | |
656 ;; If no previous statement, | |
657 ;; indent it relative to line brace is on. | |
658 ;; For open brace in column zero, don't let statement | |
659 ;; start there too. If c-indent-level is zero, | |
660 ;; use c-brace-offset + c-continued-statement-offset instead. | |
661 ;; For open-braces not the first thing in a line, | |
662 ;; add in c-brace-imaginary-offset. | |
663 (+ (if (and (bolp) (zerop c-indent-level)) | |
664 (+ c-brace-offset c-continued-statement-offset) | |
665 c-indent-level) | |
666 ;; Move back over whitespace before the openbrace. | |
667 ;; If openbrace is not first nonwhite thing on the line, | |
668 ;; add the c-brace-imaginary-offset. | |
669 (progn (skip-chars-backward " \t") | |
670 (if (bolp) 0 c-brace-imaginary-offset)) | |
671 ;; If the openbrace is preceded by a parenthesized exp, | |
672 ;; move to the beginning of that; | |
673 ;; possibly a different line | |
674 (progn | |
675 (if (eq (preceding-char) ?\)) | |
676 (forward-sexp -1)) | |
677 ;; Get initial indentation of the line we are on. | |
678 (current-indentation)))))))))) | |
679 | |
680 (defun calculate-c-indent-within-comment (&optional after-star) | |
681 "Return the indentation amount for line inside a block comment. | |
682 Optional arg AFTER-STAR means, if lines in the comment have a leading star, | |
683 return the indentation of the text that would follow this star." | |
684 (let (end star-start) | |
685 (save-excursion | |
686 (beginning-of-line) | |
687 (skip-chars-forward " \t") | |
688 (setq star-start (= (following-char) ?\*)) | |
689 (skip-chars-backward " \t\n") | |
690 (setq end (point)) | |
691 (beginning-of-line) | |
692 (skip-chars-forward " \t") | |
693 (if after-star | |
694 (and (looking-at "\\*") | |
695 (re-search-forward "\\*[ \t]*"))) | |
696 (and (re-search-forward "/\\*[ \t]*" end t) | |
697 star-start | |
698 (not after-star) | |
699 (goto-char (1+ (match-beginning 0)))) | |
700 (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\*)) | |
701 (1+ (current-column)) | |
702 (current-column))))) | |
703 | |
704 | |
705 (defun c-backward-to-noncomment (lim) | |
706 (let (opoint stop) | |
707 (while (not stop) | |
708 (skip-chars-backward " \t\n\f" lim) | |
709 (setq opoint (point)) | |
710 (if (and (>= (point) (+ 2 lim)) | |
711 (save-excursion | |
712 (forward-char -2) | |
713 (looking-at "\\*/"))) | |
714 (search-backward "/*" lim 'move) | |
715 (setq stop (or (<= (point) lim) | |
716 (save-excursion | |
717 (beginning-of-line) | |
718 (skip-chars-forward " \t") | |
719 (not (looking-at "#"))))) | |
720 (or stop (beginning-of-line)))))) | |
721 | |
722 (defun c-backward-to-start-of-continued-exp (lim) | |
723 (if (memq (preceding-char) '(?\) ?\")) | |
724 (forward-sexp -1)) | |
725 (beginning-of-line) | |
726 (if (<= (point) lim) | |
727 (goto-char (1+ lim))) | |
728 (skip-chars-forward " \t")) | |
729 | |
730 (defun c-backward-to-start-of-if (&optional limit) | |
731 "Move to the start of the last \"unbalanced\" `if'." | |
732 (or limit (setq limit (save-excursion (beginning-of-defun) (point)))) | |
733 (let ((if-level 1) | |
734 (case-fold-search nil)) | |
735 (while (and (not (bobp)) (not (zerop if-level))) | |
736 (backward-sexp 1) | |
737 (cond ((looking-at "else\\b") | |
738 (setq if-level (1+ if-level))) | |
739 ((looking-at "if\\b") | |
740 (setq if-level (1- if-level))) | |
741 ((< (point) limit) | |
742 (setq if-level 0) | |
743 (goto-char limit)))))) | |
744 | |
745 (defun c-backward-to-start-of-do (&optional limit) | |
746 "If point follows a `do' statement, move to beginning of it and return t. | |
747 Otherwise return nil and don't move point." | |
748 (or limit (setq limit (save-excursion (beginning-of-defun) (point)))) | |
749 (let ((first t) | |
750 (startpos (point)) | |
751 (done nil)) | |
752 (while (not done) | |
753 (let ((next-start (point))) | |
754 (condition-case nil | |
755 ;; Move back one token or one brace or paren group. | |
756 (backward-sexp 1) | |
757 ;; If we find an open-brace, we lose. | |
758 (error (setq done 'fail))) | |
759 (if done | |
760 nil | |
761 ;; If we reached a `do', we win. | |
762 (if (looking-at "do\\b") | |
763 (setq done 'succeed) | |
764 ;; Otherwise, if we skipped a semicolon, we lose. | |
765 ;; (Exception: we can skip one semicolon before getting | |
766 ;; to a the last token of the statement, unless that token | |
767 ;; is a close brace.) | |
768 (if (save-excursion | |
769 (forward-sexp 1) | |
770 (or (and (not first) (= (preceding-char) ?})) | |
771 (search-forward ";" next-start t | |
772 (if (and first | |
773 (/= (preceding-char) ?})) | |
774 2 1)))) | |
775 (setq done 'fail) | |
776 (setq first nil) | |
777 ;; If we go too far back in the buffer, we lose. | |
778 (if (< (point) limit) | |
779 (setq done 'fail))))))) | |
780 (if (eq done 'succeed) | |
781 t | |
782 (goto-char startpos) | |
783 nil))) | |
784 | |
785 (defun c-beginning-of-statement (count) | |
786 "Go to the beginning of the innermost C statement. | |
787 With prefix arg, go back N - 1 statements. If already at the beginning of a | |
788 statement then go to the beginning of the preceeding one." | |
789 (interactive "p") | |
790 (while (> count 0) | |
791 (c-beginning-of-statement-1) | |
792 (setq count (1- count))) | |
793 (while (< count 0) | |
794 (c-end-of-statement-1) | |
795 (setq count (1+ count)))) | |
796 | |
797 (defun c-end-of-statement (count) | |
798 "Go to the end of the innermost C statement. | |
799 With prefix arg, go forward N - 1 statements. Moves forward to end of the | |
800 next statement if already at end." | |
801 (interactive "p") | |
802 (c-beginning-of-statement (- count))) | |
803 | |
804 (defun c-beginning-of-statement-1 () | |
805 (let ((last-begin (point)) | |
806 (first t)) | |
807 (condition-case () | |
808 (progn | |
809 (while (and (not (bobp)) | |
810 (progn | |
811 (backward-sexp 1) | |
812 (or first | |
813 (not (re-search-forward "[;{}]" last-begin t))))) | |
814 (setq last-begin (point) first nil)) | |
815 (goto-char last-begin)) | |
816 (error (if first (backward-up-list 1) (goto-char last-begin)))))) | |
817 | |
818 (defun c-end-of-statement-1 () | |
819 (condition-case () | |
820 (progn | |
821 (while (and (not (eobp)) | |
822 (let ((beg (point))) | |
823 (forward-sexp 1) | |
824 (let ((end (point))) | |
825 (save-excursion | |
826 (goto-char beg) | |
827 (not (re-search-forward "[;{}]" end t))))))) | |
828 (re-search-backward "[;}]") | |
829 (forward-char 1)) | |
830 (error | |
831 (let ((beg (point))) | |
832 (backward-up-list -1) | |
833 (let ((end (point))) | |
834 (goto-char beg) | |
835 (search-forward ";" end 'move)))))) | |
836 | |
837 (defun mark-c-function () | |
838 "Put mark at end of C function, point at beginning." | |
839 (interactive) | |
840 (push-mark (point)) | |
841 (end-of-defun) | |
842 (push-mark (point)) | |
843 (beginning-of-defun) | |
844 (backward-paragraph)) | |
845 | |
846 (defun indent-c-exp (&optional endpos) | |
847 "Indent each line of the C grouping following point. | |
848 If optional arg ENDPOS is given, indent each line, stopping when | |
849 ENDPOS is encountered." | |
850 (interactive) | |
851 (let* ((indent-stack (list nil)) | |
852 (opoint (point)) ;; May be altered below. | |
853 (contain-stack | |
854 (list (if endpos | |
855 (let (funbeg) | |
856 ;; Find previous fcn-start. | |
857 (save-excursion (forward-char 1) | |
858 (beginning-of-defun) | |
859 (setq funbeg (point))) | |
860 ;; Try to find containing open, | |
861 ;; but don't scan past that fcn-start. | |
862 (save-restriction | |
863 (narrow-to-region funbeg (point)) | |
864 (condition-case nil | |
865 (save-excursion | |
866 (backward-up-list 1) (point)) | |
867 ;; We gave up: must be between fcns. | |
868 ;; Set opoint to beg of prev fcn | |
869 ;; since otherwise calculate-c-indent | |
870 ;; will get wrong answers. | |
871 (error (setq opoint funbeg) | |
872 (point))))) | |
873 (point)))) | |
874 (case-fold-search nil) | |
875 restart outer-loop-done inner-loop-done state ostate | |
876 this-indent last-sexp | |
877 at-else at-brace at-while | |
878 last-depth | |
879 (next-depth 0)) | |
880 ;; If the braces don't match, get an error right away. | |
881 (save-excursion | |
882 (forward-sexp 1)) | |
883 ;; Realign the comment on the first line, even though we don't reindent it. | |
884 (save-excursion | |
885 (let ((beg (point))) | |
886 (and (re-search-forward | |
887 comment-start-skip | |
888 (save-excursion (end-of-line) (point)) t) | |
889 ;; Make sure the comment starter we found | |
890 ;; is not actually in a string or quoted. | |
891 (let ((new-state | |
892 (parse-partial-sexp beg (point) | |
893 nil nil state))) | |
894 (and (not (nth 3 new-state)) (not (nth 5 new-state)))) | |
895 (progn (indent-for-comment) (beginning-of-line))))) | |
896 (save-excursion | |
897 (setq outer-loop-done nil) | |
898 (while (and (not (eobp)) | |
899 (if endpos (< (point) endpos) | |
900 (not outer-loop-done))) | |
901 (setq last-depth next-depth) | |
902 ;; Compute how depth changes over this line | |
903 ;; plus enough other lines to get to one that | |
904 ;; does not end inside a comment or string. | |
905 ;; Meanwhile, do appropriate indentation on comment lines. | |
906 (setq inner-loop-done nil) | |
907 (while (and (not inner-loop-done) | |
908 (not (and (eobp) (setq outer-loop-done t)))) | |
909 (setq ostate state) | |
910 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
911 nil nil state)) | |
912 (setq next-depth (car state)) | |
913 (if (and (car (cdr (cdr state))) | |
914 (>= (car (cdr (cdr state))) 0)) | |
915 (setq last-sexp (car (cdr (cdr state))))) | |
916 (if (or (nth 4 ostate)) | |
917 (c-indent-line)) | |
918 (if (or (nth 3 state)) | |
919 (forward-line 1) | |
920 (setq inner-loop-done t))) | |
921 (and endpos | |
922 (while (< next-depth 0) | |
923 (setq indent-stack (append indent-stack (list nil))) | |
924 (setq contain-stack (append contain-stack (list nil))) | |
925 (setq next-depth (1+ next-depth)) | |
926 (setq last-depth (1+ last-depth)) | |
927 (setcar (nthcdr 6 state) (1+ (nth 6 state))))) | |
928 (setq outer-loop-done (and (not endpos) (<= next-depth 0))) | |
929 (if outer-loop-done | |
930 nil | |
931 ;; If this line had ..))) (((.. in it, pop out of the levels | |
932 ;; that ended anywhere in this line, even if the final depth | |
933 ;; doesn't indicate that they ended. | |
934 (while (> last-depth (nth 6 state)) | |
935 (setq indent-stack (cdr indent-stack) | |
936 contain-stack (cdr contain-stack) | |
937 last-depth (1- last-depth))) | |
938 (if (/= last-depth next-depth) | |
939 (setq last-sexp nil)) | |
940 ;; Add levels for any parens that were started in this line. | |
941 (while (< last-depth next-depth) | |
942 (setq indent-stack (cons nil indent-stack) | |
943 contain-stack (cons nil contain-stack) | |
944 last-depth (1+ last-depth))) | |
945 (if (null (car contain-stack)) | |
946 (setcar contain-stack (or (car (cdr state)) | |
947 (save-excursion (forward-sexp -1) | |
948 (point))))) | |
949 (forward-line 1) | |
950 (skip-chars-forward " \t") | |
951 (if (eolp) | |
952 nil | |
953 (if (and (car indent-stack) | |
954 (>= (car indent-stack) 0)) | |
955 ;; Line is on an existing nesting level. | |
956 ;; Lines inside parens are handled specially. | |
957 (if (/= (char-after (car contain-stack)) ?{) | |
958 (setq this-indent (car indent-stack)) | |
959 ;; Line is at statement level. | |
960 ;; Is it a new statement? Is it an else? | |
961 ;; Find last non-comment character before this line | |
962 (save-excursion | |
963 (setq at-else (looking-at "else\\W")) | |
964 (setq at-brace (= (following-char) ?{)) | |
965 (setq at-while (looking-at "while\\b")) | |
966 (c-backward-to-noncomment opoint) | |
967 (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?{))) | |
968 ;; Preceding line did not end in comma or semi; | |
969 ;; indent this line c-continued-statement-offset | |
970 ;; more than previous. | |
971 (progn | |
972 (c-backward-to-start-of-continued-exp (car contain-stack)) | |
973 (setq this-indent | |
974 (+ c-continued-statement-offset (current-column) | |
975 (if at-brace c-continued-brace-offset 0)))) | |
976 ;; Preceding line ended in comma or semi; | |
977 ;; use the standard indent for this level. | |
978 (cond (at-else (progn (c-backward-to-start-of-if opoint) | |
979 (setq this-indent | |
980 (current-indentation)))) | |
981 ((and at-while (c-backward-to-start-of-do opoint)) | |
982 (setq this-indent (current-indentation))) | |
983 (t (setq this-indent (car indent-stack))))))) | |
984 ;; Just started a new nesting level. | |
985 ;; Compute the standard indent for this level. | |
986 (let ((val (calculate-c-indent | |
987 (if (car indent-stack) | |
988 (- (car indent-stack)) | |
989 opoint)))) | |
990 (setcar indent-stack | |
991 (setq this-indent val)))) | |
992 ;; Adjust line indentation according to its contents | |
993 (if (or (looking-at "case[ \t'/(]\\|default\\>") | |
994 (and (looking-at "[A-Za-z]") | |
995 (save-excursion | |
996 (forward-sexp 1) | |
997 (looking-at ":")))) | |
998 (setq this-indent (max 1 (+ this-indent c-label-offset)))) | |
999 (if (= (following-char) ?}) | |
1000 (setq this-indent (- this-indent c-indent-level))) | |
1001 (if (= (following-char) ?{) | |
1002 (setq this-indent (+ this-indent c-brace-offset))) | |
1003 ;; Don't leave indentation in empty lines. | |
1004 (if (eolp) (setq this-indent 0)) | |
1005 ;; Put chosen indentation into effect. | |
1006 (or (= (current-column) this-indent) | |
1007 (= (following-char) ?\#) | |
1008 (progn | |
1009 (delete-region (point) (progn (beginning-of-line) (point))) | |
1010 (indent-to this-indent))) | |
1011 ;; Indent any comment following the text. | |
1012 (or (looking-at comment-start-skip) | |
1013 (let ((beg (point))) | |
1014 (and (re-search-forward | |
1015 comment-start-skip | |
1016 (save-excursion (end-of-line) (point)) t) | |
1017 ;; Make sure the comment starter we found | |
1018 ;; is not actually in a string or quoted. | |
1019 (let ((new-state | |
1020 (parse-partial-sexp beg (point) | |
1021 nil nil state))) | |
1022 (and (not (nth 3 new-state)) (not (nth 5 new-state)))) | |
1023 (progn (indent-for-comment) (beginning-of-line))))))))))) | |
1024 | |
1025 ;; Look at all comment-start strings in the current line after point. | |
1026 ;; Return t if one of them starts a real comment. | |
1027 ;; This is not used yet, because indent-for-comment | |
1028 ;; isn't smart enough to handle the cases this can find. | |
1029 (defun indent-c-find-real-comment () | |
1030 (let (win) | |
1031 (while (and (not win) | |
1032 (re-search-forward comment-start-skip | |
1033 (save-excursion (end-of-line) (point)) | |
1034 t)) | |
1035 ;; Make sure the comment start is not quoted. | |
1036 (let ((state-1 | |
1037 (parse-partial-sexp | |
1038 (save-excursion (beginning-of-line) (point)) | |
1039 (point) nil nil state))) | |
1040 (setq win (and (null (nth 3 state-1)) (null (nth 5 state-1)))))) | |
1041 win)) | |
1042 | |
1043 ;; Indent every line whose first char is between START and END inclusive. | |
1044 (defun c-indent-region (start end) | |
1045 (save-excursion | |
1046 (goto-char start) | |
1047 (let ((endmark (copy-marker end))) | |
1048 (and (bolp) (not (eolp)) | |
1049 (c-indent-line)) | |
1050 (indent-c-exp endmark) | |
1051 (set-marker endmark nil)))) | |
1052 | |
1053 (defun set-c-style (style) | |
1054 "Set C-mode variables to use one of several different indentation styles. | |
1055 Takes one argument, a string representing the desired style. | |
1056 Available styles are GNU, K&R, BSD and Whitesmith." | |
1057 (interactive (list (completing-read "Use which C indentation style? " | |
1058 c-style-alist nil t))) | |
1059 (let ((vars (cdr (assoc style c-style-alist)))) | |
1060 (if vars | |
1061 (if (interactive-p) (message "Using %s C indentation style" style)) | |
1062 (error "Bogus style type, \"%s\"" style)) | |
1063 (while vars | |
1064 (set (car (car vars)) (cdr (car vars))) | |
1065 (setq vars (cdr vars))))) | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
472
diff
changeset
|
1066 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
472
diff
changeset
|
1067 ;;; c-mode.el ends here |