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