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