]> code.delx.au - gnu-emacs/blob - lisp/progmodes/c-mode.el
Fix capitalization and punctuation in menu bar.
[gnu-emacs] / lisp / progmodes / c-mode.el
1 ;;; c-mode.el --- C code editing commands for Emacs
2 ;; Copyright (C) 1985, 86, 87, 92, 94, 95 Free Software Foundation, Inc.
3
4 ;; Maintainer: FSF
5 ;; Keywords: c
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;; A smart editing mode for C code. It knows a lot about C syntax and tries
26 ;; to position the cursor according to C layout conventions. You can
27 ;; change the details of the layout style with option variables. Load it
28 ;; and do M-x describe-mode for details.
29
30 ;;; Code:
31
32 (defvar c-mode-abbrev-table nil
33 "Abbrev table in use in C mode.")
34 (define-abbrev-table 'c-mode-abbrev-table ())
35
36 (defvar c-mode-map (make-sparse-keymap)
37 "Keymap used in C mode.")
38
39 (define-key c-mode-map "{" 'electric-c-brace)
40 (define-key c-mode-map "}" 'electric-c-brace)
41 (define-key c-mode-map ";" 'electric-c-semi)
42 (define-key c-mode-map "#" 'electric-c-sharp-sign)
43 (define-key c-mode-map ":" 'electric-c-terminator)
44 (define-key c-mode-map "\e\C-h" 'mark-c-function)
45 (define-key c-mode-map "\e\C-q" 'indent-c-exp)
46 (define-key c-mode-map "\ea" 'c-beginning-of-statement)
47 (define-key c-mode-map "\ee" 'c-end-of-statement)
48 (define-key c-mode-map "\C-c\C-n" 'c-forward-conditional)
49 (define-key c-mode-map "\C-c\C-p" 'c-backward-conditional)
50 (define-key c-mode-map "\C-c\C-u" 'c-up-conditional)
51 (define-key c-mode-map "\177" 'backward-delete-char-untabify)
52 (define-key c-mode-map "\t" 'c-indent-command)
53
54 (define-key c-mode-map [menu-bar] (make-sparse-keymap))
55
56 ;; "C-mode" is not strictly the right punctuation--it should be "C
57 ;; mode"--but that would look like two menu items. "C-mode" is the
58 ;; best alternative I can think of.
59 (define-key c-mode-map [menu-bar c]
60 (cons "C-mode" (make-sparse-keymap "C-mode")))
61
62 (define-key c-mode-map [menu-bar c comment-region]
63 '("Comment Out Region" . comment-region))
64 (define-key c-mode-map [menu-bar c c-macro-expand]
65 '("Macro Expand Region" . c-macro-expand))
66 (define-key c-mode-map [menu-bar c c-backslash-region]
67 '("Backslashify" . c-backslash-region))
68 (define-key c-mode-map [menu-bar c indent-exp]
69 '("Indent Expression" . indent-c-exp))
70 (define-key c-mode-map [menu-bar c indent-line]
71 '("Indent Line" . c-indent-command))
72 (define-key c-mode-map [menu-bar c fill]
73 '("Fill Comment Paragraph" . c-fill-paragraph))
74 (define-key c-mode-map [menu-bar c cpp-highlight-buffer]
75 '("Highlight Conditionals" . cpp-highlight-buffer))
76 (define-key c-mode-map [menu-bar c up]
77 '("Up Conditional" . c-up-conditional))
78 (define-key c-mode-map [menu-bar c backward]
79 '("Backward Conditional" . c-backward-conditional))
80 (define-key c-mode-map [menu-bar c forward]
81 '("Forward Conditional" . c-forward-conditional))
82 (define-key c-mode-map [menu-bar c backward-stmt]
83 '("Backward Statement" . c-beginning-of-statement))
84 (define-key c-mode-map [menu-bar c forward-stmt]
85 '("Forward Statement" . c-end-of-statement))
86
87 (autoload 'c-macro-expand "cmacexp"
88 "Display the result of expanding all C macros occurring in the region.
89 The expansion is entirely correct because it uses the C preprocessor."
90 t)
91
92 (defvar c-mode-syntax-table nil
93 "Syntax table in use in C-mode buffers.")
94
95 (if c-mode-syntax-table
96 ()
97 (setq c-mode-syntax-table (make-syntax-table))
98 (modify-syntax-entry ?\\ "\\" c-mode-syntax-table)
99 (modify-syntax-entry ?/ ". 14" c-mode-syntax-table)
100 (modify-syntax-entry ?* ". 23" c-mode-syntax-table)
101 (modify-syntax-entry ?+ "." c-mode-syntax-table)
102 (modify-syntax-entry ?- "." c-mode-syntax-table)
103 (modify-syntax-entry ?= "." c-mode-syntax-table)
104 (modify-syntax-entry ?% "." c-mode-syntax-table)
105 (modify-syntax-entry ?< "." c-mode-syntax-table)
106 (modify-syntax-entry ?> "." c-mode-syntax-table)
107 (modify-syntax-entry ?& "." c-mode-syntax-table)
108 (modify-syntax-entry ?| "." c-mode-syntax-table)
109 (modify-syntax-entry ?\' "\"" c-mode-syntax-table))
110
111 (defconst c-indent-level 2
112 "*Indentation of C statements with respect to containing block.")
113 (defconst c-brace-imaginary-offset 0
114 "*Imagined indentation of a C open brace that actually follows a statement.")
115 (defconst c-brace-offset 0
116 "*Extra indentation for braces, compared with other text in same context.")
117 (defconst c-argdecl-indent 5
118 "*Indentation level of declarations of C function arguments.")
119 (defconst c-label-offset -2
120 "*Offset of C label lines and case statements relative to usual indentation.")
121 (defconst c-continued-statement-offset 2
122 "*Extra indent for lines not starting new statements.")
123 (defconst c-continued-brace-offset 0
124 "*Extra indent for substatements that start with open-braces.
125 This is in addition to `c-continued-statement-offset'.")
126 (defconst c-style-alist
127 '(("GNU"
128 (c-indent-level . 2)
129 (c-argdecl-indent . 5)
130 (c-brace-offset . 0)
131 (c-continued-brace-offset . 0)
132 (c-label-offset . -2)
133 (c-continued-statement-offset . 2))
134 ("K&R"
135 (c-indent-level . 5)
136 (c-argdecl-indent . 0)
137 (c-brace-offset . 0)
138 (c-continued-brace-offset . -5)
139 (c-label-offset . -5)
140 (c-continued-statement-offset . 5))
141 ("BSD"
142 (c-indent-level . 4)
143 (c-argdecl-indent . 4)
144 (c-brace-offset . 0)
145 (c-continued-brace-offset . -4)
146 (c-label-offset . -4)
147 (c-continued-statement-offset . 4))
148 ("C++"
149 (c-indent-level . 4)
150 (c-argdecl-indent . 0)
151 (c-brace-offset . 0)
152 (c-continued-brace-offset . -4)
153 (c-label-offset . -4)
154 (c-continued-statement-offset . 4)
155 (c-auto-newline . t))
156 ("Whitesmith"
157 (c-indent-level . 4)
158 (c-argdecl-indent . 4)
159 (c-brace-offset . 0)
160 (c-continued-brace-offset . 0)
161 (c-label-offset . -4)
162 (c-continued-statement-offset . 4))))
163
164 (defconst c-auto-newline nil
165 "*Non-nil means automatically newline before and after braces,
166 and after colons and semicolons, inserted in C code.
167 If you do not want a leading newline before braces then use:
168 (define-key c-mode-map \"{\" 'electric-c-semi)")
169
170 (defconst c-tab-always-indent t
171 "*Non-nil means TAB in C mode should always reindent the current line,
172 regardless of where in the line point is when the TAB command is used.")
173
174 ;;; Regular expression used internally to recognize labels in switch
175 ;;; statements.
176 (defconst c-switch-label-regexp "case[ \t'/(]\\|default[ \t]*:")
177
178 \f
179 (defun c-mode ()
180 "Major mode for editing C code.
181 Expression and list commands understand all C brackets.
182 Tab indents for C code.
183 Comments are delimited with /* ... */.
184 Paragraphs are separated by blank lines only.
185 Delete converts tabs to spaces as it moves back.
186 \\{c-mode-map}
187 Variables controlling indentation style:
188 c-tab-always-indent
189 Non-nil means TAB in C mode should always reindent the current line,
190 regardless of where in the line point is when the TAB command is used.
191 c-auto-newline
192 Non-nil means automatically newline before and after braces,
193 and after colons and semicolons, inserted in C code.
194 c-indent-level
195 Indentation of C statements within surrounding block.
196 The surrounding block's indentation is the indentation
197 of the line on which the open-brace appears.
198 c-continued-statement-offset
199 Extra indentation given to a substatement, such as the
200 then-clause of an if or body of a while.
201 c-continued-brace-offset
202 Extra indentation given to a brace that starts a substatement.
203 This is in addition to c-continued-statement-offset.
204 c-brace-offset
205 Extra indentation for line if it starts with an open brace.
206 c-brace-imaginary-offset
207 An open brace following other text is treated as if it were
208 this far to the right of the start of its line.
209 c-argdecl-indent
210 Indentation level of declarations of C function arguments.
211 c-label-offset
212 Extra indentation for line that is a label, or case or default.
213
214 Settings for K&R and BSD indentation styles are
215 c-indent-level 5 8
216 c-continued-statement-offset 5 8
217 c-brace-offset -5 -8
218 c-argdecl-indent 0 8
219 c-label-offset -5 -8
220
221 Turning on C mode calls the value of the variable c-mode-hook with no args,
222 if that value is non-nil."
223 (interactive)
224 (kill-all-local-variables)
225 (use-local-map c-mode-map)
226 (setq major-mode 'c-mode)
227 (setq mode-name "C")
228 (setq local-abbrev-table c-mode-abbrev-table)
229 (set-syntax-table c-mode-syntax-table)
230 (make-local-variable 'paragraph-start)
231 (setq paragraph-start (concat "$\\|" page-delimiter))
232 (make-local-variable 'paragraph-separate)
233 (setq paragraph-separate paragraph-start)
234 (make-local-variable 'paragraph-ignore-fill-prefix)
235 (setq paragraph-ignore-fill-prefix t)
236 (make-local-variable 'fill-paragraph-function)
237 (setq fill-paragraph-function 'c-fill-paragraph)
238 (make-local-variable 'indent-line-function)
239 (setq indent-line-function 'c-indent-line)
240 (make-local-variable 'indent-region-function)
241 (setq indent-region-function 'c-indent-region)
242 (make-local-variable 'require-final-newline)
243 (setq require-final-newline t)
244 (make-local-variable 'outline-regexp)
245 (setq outline-regexp "[^#\n\^M]")
246 (make-local-variable 'outline-level)
247 (setq outline-level 'c-outline-level)
248 (make-local-variable 'comment-start)
249 (setq comment-start "/* ")
250 (make-local-variable 'comment-end)
251 (setq comment-end " */")
252 (make-local-variable 'comment-column)
253 (setq comment-column 32)
254 (make-local-variable 'comment-start-skip)
255 (setq comment-start-skip "/\\*+ *")
256 (make-local-variable 'comment-indent-function)
257 (setq comment-indent-function 'c-comment-indent)
258 (make-local-variable 'comment-multi-line)
259 (setq comment-multi-line t)
260 (make-local-variable 'parse-sexp-ignore-comments)
261 (setq parse-sexp-ignore-comments t)
262 (run-hooks 'c-mode-hook))
263
264 (defun c-outline-level ()
265 (save-excursion
266 (skip-chars-forward "\t ")
267 (current-column)))
268 \f
269 ;; This is used by indent-for-comment
270 ;; to decide how much to indent a comment in C code
271 ;; based on its context.
272 (defun c-comment-indent ()
273 (if (looking-at "^/\\*")
274 0 ;Existing comment at bol stays there.
275 (let ((opoint (point)))
276 (save-excursion
277 (beginning-of-line)
278 (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)")
279 ;; A comment following a solitary close-brace
280 ;; should have only one space.
281 (search-forward "}")
282 (1+ (current-column)))
283 ((or (looking-at "^#[ \t]*endif[ \t]*")
284 (looking-at "^#[ \t]*else[ \t]*"))
285 7) ;2 spaces after #endif
286 ((progn
287 (goto-char opoint)
288 (skip-chars-backward " \t")
289 (and (= comment-column 0) (bolp)))
290 ;; If comment-column is 0, and nothing but space
291 ;; before the comment, align it at 0 rather than 1.
292 0)
293 (t
294 (max (1+ (current-column)) ;Else indent at comment column
295 comment-column))))))) ; except leave at least one space.
296
297 (defun c-fill-paragraph (&optional arg)
298 "Like \\[fill-paragraph] but handle C comments.
299 If any of the current line is a comment or within a comment,
300 fill the comment or the paragraph of it that point is in,
301 preserving the comment indentation or line-starting decorations."
302 (interactive "P")
303 (let* (comment-start-place
304 (first-line
305 ;; Check for obvious entry to comment.
306 (save-excursion
307 (beginning-of-line)
308 (skip-chars-forward " \t\n")
309 (and (looking-at comment-start-skip)
310 (setq comment-start-place (point))))))
311 (if (and (eq major-mode 'c++-mode)
312 (save-excursion
313 (beginning-of-line)
314 (looking-at ".*//")))
315 (let (fill-prefix
316 (paragraph-start
317 ;; Lines containing just a comment start or just an end
318 ;; should not be filled into paragraphs they are next to.
319 (concat
320 paragraph-start
321 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
322 (paragraph-separate
323 (concat
324 paragraph-separate
325 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")))
326 (save-excursion
327 (beginning-of-line)
328 ;; Move up to first line of this comment.
329 (while (and (not (bobp)) (looking-at "[ \t]*//"))
330 (forward-line -1))
331 (if (not (looking-at ".*//"))
332 (forward-line 1))
333 ;; Find the comment start in this line.
334 (re-search-forward "[ \t]*//[ \t]*")
335 ;; Set the fill-prefix to be what all lines except the first
336 ;; should start with.
337 (let ((endcol (current-column)))
338 (skip-chars-backward " \t")
339 (setq fill-prefix
340 (concat (make-string (- (current-column) 2) ?\ )
341 "//"
342 (make-string (- endcol (current-column)) ?\ ))))
343 (save-restriction
344 ;; Narrow down to just the lines of this comment.
345 (narrow-to-region (point)
346 (save-excursion
347 (forward-line 1)
348 (while (looking-at "[ \t]*//")
349 (forward-line 1))
350 (point)))
351 (insert fill-prefix)
352 (fill-paragraph arg)
353 (delete-region (point-min)
354 (+ (point-min) (length fill-prefix))))))
355 (if (or first-line
356 ;; t if we enter a comment between start of function and this line.
357 (eq (calculate-c-indent) t)
358 ;; t if this line contains a comment starter.
359 (setq first-line
360 (save-excursion
361 (beginning-of-line)
362 (prog1
363 (re-search-forward comment-start-skip
364 (save-excursion (end-of-line)
365 (point))
366 t)
367 (setq comment-start-place (point))))))
368 ;; Inside a comment: fill one comment paragraph.
369 (let ((fill-prefix
370 ;; The prefix for each line of this paragraph
371 ;; is the appropriate part of the start of this line,
372 ;; up to the column at which text should be indented.
373 (save-excursion
374 (beginning-of-line)
375 (if (looking-at "[ \t]*/\\*.*\\*/")
376 (progn (re-search-forward comment-start-skip)
377 (make-string (current-column) ?\ ))
378 (if first-line (forward-line 1))
379
380 (let ((line-width (progn (end-of-line) (current-column))))
381 (beginning-of-line)
382 (prog1
383 (buffer-substring
384 (point)
385
386 ;; How shall we decide where the end of the
387 ;; fill-prefix is?
388 ;; calculate-c-indent-within-comment bases its value
389 ;; on the indentation of previous lines; if they're
390 ;; indented specially, it could return a column
391 ;; that's well into the current line's text. So
392 ;; we'll take at most that many space, tab, or *
393 ;; characters, and use that as our fill prefix.
394 (let ((max-prefix-end
395 (progn
396 (move-to-column
397 (calculate-c-indent-within-comment t)
398 t)
399 (point))))
400 (beginning-of-line)
401 (skip-chars-forward " \t*" max-prefix-end)
402 ;; Don't include part of comment terminator
403 ;; in the fill-prefix.
404 (and (eq (following-char) ?/)
405 (eq (preceding-char) ?*)
406 (backward-char 1))
407 (point)))
408
409 ;; If the comment is only one line followed by a blank
410 ;; line, calling move-to-column above may have added
411 ;; some spaces and tabs to the end of the line; the
412 ;; fill-paragraph function will then delete it and the
413 ;; newline following it, so we'll lose a blank line
414 ;; when we shouldn't. So delete anything
415 ;; move-to-column added to the end of the line. We
416 ;; record the line width instead of the position of the
417 ;; old line end because move-to-column might break a
418 ;; tab into spaces, and the new characters introduced
419 ;; there shouldn't be deleted.
420
421 ;; If you can see a better way to do this, please make
422 ;; the change. This seems very messy to me.
423 (delete-region (progn (move-to-column line-width)
424 (point))
425 (progn (end-of-line) (point))))))))
426
427 (paragraph-start
428 ;; Lines containing just a comment start or just an end
429 ;; should not be filled into paragraphs they are next to.
430 (concat
431 paragraph-start
432 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
433 (paragraph-separate
434 (concat
435 paragraph-separate
436 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
437 (chars-to-delete 0))
438 (save-restriction
439 ;; Don't fill the comment together with the code following it.
440 ;; So temporarily exclude everything before the comment start,
441 ;; and everything after the line where the comment ends.
442 ;; If comment-start-place is non-nil, the comment starter is there.
443 ;; Otherwise, point is inside the comment.
444 (narrow-to-region (save-excursion
445 (if comment-start-place
446 (goto-char comment-start-place)
447 (search-backward "/*"))
448 ;; Protect text before the comment start
449 ;; by excluding it. Add spaces to bring back
450 ;; proper indentation of that point.
451 (let ((column (current-column)))
452 (prog1 (point)
453 (setq chars-to-delete column)
454 (insert-char ?\ column))))
455 (save-excursion
456 (if comment-start-place
457 (goto-char (+ comment-start-place 2)))
458 (search-forward "*/" nil 'move)
459 (forward-line 1)
460 (point)))
461 (save-excursion
462 (goto-char (point-max))
463 (forward-line -1)
464 ;; And comment terminator was on a separate line before,
465 ;; keep it that way.
466 ;; This also avoids another problem:
467 ;; if the fill-prefix ends in a *, it could eat up
468 ;; the * of the comment terminator.
469 (if (looking-at "[ \t]*\\*/")
470 (narrow-to-region (point-min) (point))))
471 (fill-paragraph arg)
472 (save-excursion
473 ;; Delete the chars we inserted to avoid clobbering
474 ;; the stuff before the comment start.
475 (goto-char (point-min))
476 (if (> chars-to-delete 0)
477 (delete-region (point) (+ (point) chars-to-delete)))
478 ;; Find the comment ender (should be on last line of buffer,
479 ;; given the narrowing) and don't leave it on its own line.
480 ;; Do this with a fill command, so as to preserve sentence
481 ;; boundaries.
482 (goto-char (point-max))
483 (forward-line -1)
484 (search-forward "*/" nil 'move)
485 (beginning-of-line)
486 (if (looking-at "[ \t]*\\*/")
487 (let ((fill-column (+ fill-column 9999)))
488 (forward-line -1)
489 (fill-region-as-paragraph (point) (point-max)))))))
490 ;; Outside of comments: do ordinary filling.
491 (fill-paragraph arg)))
492 t))
493
494 (defun electric-c-brace (arg)
495 "Insert character and correct line's indentation."
496 (interactive "P")
497 (let (insertpos)
498 (if (and (not arg)
499 (eolp)
500 (or (save-excursion
501 (skip-chars-backward " \t")
502 (bolp))
503 (if c-auto-newline (progn (c-indent-line) (newline) t) nil)))
504 (progn
505 (insert last-command-char)
506 (c-indent-line)
507 (if c-auto-newline
508 (progn
509 (newline)
510 ;; (newline) may have done auto-fill
511 (setq insertpos (- (point) 2))
512 (c-indent-line)))
513 (save-excursion
514 (if insertpos (goto-char (1+ insertpos)))
515 (delete-char -1))))
516 (if insertpos
517 (save-excursion
518 (goto-char insertpos)
519 (self-insert-command (prefix-numeric-value arg)))
520 (self-insert-command (prefix-numeric-value arg)))))
521
522 (defun electric-c-sharp-sign (arg)
523 "Insert character and correct line's indentation."
524 (interactive "P")
525 (if (save-excursion
526 (skip-chars-backward " \t")
527 (bolp))
528 (let ((c-auto-newline nil))
529 (electric-c-terminator arg))
530 (self-insert-command (prefix-numeric-value arg))))
531
532 (defun electric-c-semi (arg)
533 "Insert character and correct line's indentation."
534 (interactive "P")
535 (if c-auto-newline
536 (electric-c-terminator arg)
537 (self-insert-command (prefix-numeric-value arg))))
538
539 (defun electric-c-terminator (arg)
540 "Insert character and correct line's indentation."
541 (interactive "P")
542 (let (insertpos (end (point)))
543 (if (and (not arg) (eolp)
544 (not (save-excursion
545 (beginning-of-line)
546 (skip-chars-forward " \t")
547 (or (= (following-char) ?#)
548 ;; Colon is special only after a label, or case ....
549 ;; So quickly rule out most other uses of colon
550 ;; and do no indentation for them.
551 (and (eq last-command-char ?:)
552 (not (looking-at c-switch-label-regexp))
553 (save-excursion
554 (skip-chars-forward "a-zA-Z0-9_$")
555 (skip-chars-forward " \t")
556 (< (point) end)))
557 (progn
558 (beginning-of-defun)
559 (let ((pps (parse-partial-sexp (point) end)))
560 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
561 (progn
562 (insert last-command-char)
563 (c-indent-line)
564 (and c-auto-newline
565 (not (c-inside-parens-p))
566 (progn
567 (newline)
568 ;; (newline) may have done auto-fill
569 (setq insertpos (- (point) 2))
570 (c-indent-line)))
571 (save-excursion
572 (if insertpos (goto-char (1+ insertpos)))
573 (delete-char -1))))
574 (if insertpos
575 (save-excursion
576 (goto-char insertpos)
577 (self-insert-command (prefix-numeric-value arg)))
578 (self-insert-command (prefix-numeric-value arg)))))
579
580 (defun c-inside-parens-p ()
581 (condition-case ()
582 (save-excursion
583 (save-restriction
584 (narrow-to-region (point)
585 (progn (beginning-of-defun) (point)))
586 (goto-char (point-max))
587 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
588 (error nil)))
589 \f
590 (defun c-indent-command (&optional whole-exp)
591 "Indent current line as C code, or in some cases insert a tab character.
592 If `c-tab-always-indent' is non-nil (the default), always indent current line.
593 Otherwise, indent the current line only if point is at the left margin or
594 in the line's indentation; otherwise insert a tab.
595
596 A numeric argument, regardless of its value, means indent rigidly all the
597 lines of the expression starting after point so that this line becomes
598 properly indented. The relative indentation among the lines of the
599 expression are preserved."
600 (interactive "P")
601 (if whole-exp
602 ;; If arg, always indent this line as C
603 ;; and shift remaining lines of expression the same amount.
604 (let ((shift-amt (c-indent-line))
605 beg end)
606 (save-excursion
607 (if c-tab-always-indent
608 (beginning-of-line))
609 ;; Find beginning of following line.
610 (save-excursion
611 (forward-line 1) (setq beg (point)))
612 ;; Find first beginning-of-sexp for sexp extending past this line.
613 (while (< (point) beg)
614 (forward-sexp 1)
615 (setq end (point))
616 (skip-chars-forward " \t\n")))
617 (if (> end beg)
618 (indent-code-rigidly beg end shift-amt "#")))
619 (if (and (not c-tab-always-indent)
620 (save-excursion
621 (skip-chars-backward " \t")
622 (not (bolp))))
623 (insert-tab)
624 (c-indent-line))))
625
626 (defun c-indent-line ()
627 "Indent current line as C code.
628 Return the amount the indentation changed by."
629 (let ((indent (calculate-c-indent nil))
630 beg shift-amt
631 (case-fold-search nil)
632 (pos (- (point-max) (point))))
633 (beginning-of-line)
634 (setq beg (point))
635 (cond ((eq indent nil)
636 (setq indent (current-indentation)))
637 ((eq indent t)
638 (setq indent (calculate-c-indent-within-comment)))
639 ((looking-at "[ \t]*#")
640 (setq indent 0))
641 (t
642 (skip-chars-forward " \t")
643 (if (listp indent) (setq indent (car indent)))
644 (cond ((or (looking-at c-switch-label-regexp)
645 (and (looking-at "[A-Za-z]")
646 (save-excursion
647 (forward-sexp 1)
648 (looking-at ":"))))
649 (setq indent (max 1 (+ indent c-label-offset))))
650 ((and (looking-at "else\\b")
651 (not (looking-at "else\\s_")))
652 (setq indent (save-excursion
653 (c-backward-to-start-of-if)
654 (current-indentation))))
655 ((and (looking-at "}[ \t]*else\\b")
656 (not (looking-at "}[ \t]*else\\s_")))
657 (setq indent (save-excursion
658 (forward-char)
659 (backward-sexp)
660 (c-backward-to-start-of-if)
661 (current-indentation))))
662 ((and (looking-at "while\\b")
663 (not (looking-at "while\\s_"))
664 (save-excursion
665 (c-backward-to-start-of-do)))
666 ;; This is a `while' that ends a do-while.
667 (setq indent (save-excursion
668 (c-backward-to-start-of-do)
669 (current-indentation))))
670 ((= (following-char) ?})
671 (setq indent (- indent c-indent-level)))
672 ((= (following-char) ?{)
673 (setq indent (+ indent c-brace-offset))))))
674 (skip-chars-forward " \t")
675 (setq shift-amt (- indent (current-column)))
676 (if (zerop shift-amt)
677 (if (> (- (point-max) pos) (point))
678 (goto-char (- (point-max) pos)))
679 (delete-region beg (point))
680 (indent-to indent)
681 ;; If initial point was within line's indentation,
682 ;; position after the indentation. Else stay at same point in text.
683 (if (> (- (point-max) pos) (point))
684 (goto-char (- (point-max) pos))))
685 shift-amt))
686
687 (defun calculate-c-indent (&optional parse-start)
688 "Return appropriate indentation for current line as C code.
689 In usual case returns an integer: the column to indent to.
690 Returns nil if line starts inside a string, t if in a comment."
691 (save-excursion
692 (beginning-of-line)
693 (let ((indent-point (point))
694 (case-fold-search nil)
695 state
696 containing-sexp)
697 (if parse-start
698 (goto-char parse-start)
699 (beginning-of-defun))
700 (while (< (point) indent-point)
701 (setq parse-start (point))
702 (setq state (parse-partial-sexp (point) indent-point 0))
703 (setq containing-sexp (car (cdr state))))
704 (cond ((or (nth 3 state) (nth 4 state))
705 ;; return nil or t if should not change this line
706 (nth 4 state))
707 ((null containing-sexp)
708 ;; Line is at top level. May be data or function definition,
709 ;; or may be function argument declaration.
710 ;; Indent like the previous top level line
711 ;; unless that ends in a closeparen without semicolon,
712 ;; in which case this line is the first argument decl.
713 (goto-char indent-point)
714 (skip-chars-forward " \t")
715 (if (= (following-char) ?{)
716 0 ; Unless it starts a function body
717 (c-backward-to-noncomment (or parse-start (point-min)))
718 ;; Look at previous line that's at column 0
719 ;; to determine whether we are in top-level decls
720 ;; or function's arg decls. Set basic-indent accordingly.
721 (let ((basic-indent
722 (save-excursion
723 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
724 (let (comment lim)
725 ;; Recognize the DEFUN macro in Emacs.
726 (if (save-excursion
727 ;; Move down to the (putative) argnames line.
728 (while (and (not (eobp))
729 (not (looking-at " *[({}#/]")))
730 (forward-line 1))
731 ;; Go back to the DEFUN, if it is one.
732 (condition-case nil
733 (backward-sexp 1)
734 (error))
735 (beginning-of-line)
736 (looking-at "DEFUN\\b"))
737 c-argdecl-indent
738 (if (and (looking-at "\\sw\\|\\s_")
739 ;; This is careful to stop at the first
740 ;; paren if we have
741 ;; int foo Proto ((int, int));
742 (looking-at "[^\"\n=(]*(")
743 (progn
744 (goto-char (1- (match-end 0)))
745 ;; Skip any number of paren-groups.
746 ;; Consider typedef int (*fcn) (int);
747 (while (= (following-char) ?\()
748 (setq lim (point))
749 (condition-case nil
750 (forward-sexp 1)
751 (error))
752 (skip-chars-forward " \t\f"))
753 ;; Have we reached something
754 ;; that shows this isn't a function
755 ;; definition?
756 (and (< (point) indent-point)
757 (not (memq (following-char)
758 '(?\, ?\;)))))
759 ;; Make sure the "function decl" we found
760 ;; is not inside a comment.
761 (progn
762 ;; Move back to the `(' starting arglist
763 (goto-char lim)
764 (beginning-of-line)
765 (while (and (not comment)
766 (search-forward "/*" lim t))
767 (setq comment
768 (not (search-forward "*/" lim t))))
769 (not comment)))
770 c-argdecl-indent 0))))))
771 basic-indent)))
772
773 ;; ;; Now add a little if this is a continuation line.
774 ;; (+ basic-indent (if (or (bobp)
775 ;; (memq (preceding-char) '(?\) ?\; ?\}))
776 ;; ;; Line with zero indentation
777 ;; ;; is probably the return-type
778 ;; ;; of a function definition,
779 ;; ;; so following line is function name.
780 ;; (= (current-indentation) 0))
781 ;; 0 c-continued-statement-offset))
782
783 ((/= (char-after containing-sexp) ?{)
784 ;; line is expression, not statement:
785 ;; indent to just after the surrounding open.
786 (goto-char (1+ containing-sexp))
787 (current-column))
788 (t
789 ;; Statement level. Is it a continuation or a new statement?
790 ;; Find previous non-comment character.
791 (goto-char indent-point)
792 (c-backward-to-noncomment containing-sexp)
793 ;; Back up over label lines, since they don't
794 ;; affect whether our line is a continuation.
795 (while (or (eq (preceding-char) ?\,)
796 (and (eq (preceding-char) ?:)
797 (or (eq (char-after (- (point) 2)) ?\')
798 (memq (char-syntax (char-after (- (point) 2)))
799 '(?w ?_)))))
800 (if (eq (preceding-char) ?\,)
801 (progn (forward-char -1)
802 (c-backward-to-start-of-continued-exp containing-sexp)))
803 (beginning-of-line)
804 (c-backward-to-noncomment containing-sexp))
805 ;; Check for a preprocessor statement or its continuation lines.
806 ;; Move back to end of previous non-preprocessor line,
807 ;; or possibly beginning of buffer.
808 (let ((found (point)) stop)
809 (while (not stop)
810 (beginning-of-line)
811 (cond ((bobp)
812 (setq found (point)
813 stop t))
814 ((save-excursion (forward-char -1)
815 (= (preceding-char) ?\\))
816 (forward-char -1))
817 ;; This line is not preceded by a backslash.
818 ;; So either it starts a preprocessor command
819 ;; or any following continuation lines
820 ;; should not be skipped.
821 ((= (following-char) ?#)
822 (forward-char -1)
823 (setq found (point)))
824 (t (setq stop t))))
825 (goto-char found))
826 ;; Now we get the answer.
827 (if (and (not (memq (preceding-char) '(0 ?\, ?\; ?\} ?\{)))
828 ;; But don't treat a line with a close-brace
829 ;; as a continuation. It is probably the
830 ;; end of an enum type declaration.
831 (save-excursion
832 (goto-char indent-point)
833 (skip-chars-forward " \t")
834 (not (= (following-char) ?}))))
835 ;; This line is continuation of preceding line's statement;
836 ;; indent c-continued-statement-offset more than the
837 ;; previous line of the statement.
838 (progn
839 (c-backward-to-start-of-continued-exp containing-sexp)
840 (+ c-continued-statement-offset (current-column)
841 (if (save-excursion (goto-char indent-point)
842 (skip-chars-forward " \t")
843 (eq (following-char) ?{))
844 c-continued-brace-offset 0)))
845 ;; This line starts a new statement.
846 ;; Position following last unclosed open.
847 (goto-char containing-sexp)
848 ;; Is line first statement after an open-brace?
849 (or
850 ;; If no, find that first statement and indent like it.
851 (save-excursion
852 (forward-char 1)
853 (let ((colon-line-end 0))
854 (while (progn (skip-chars-forward " \t\n")
855 (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:"))
856 ;; Skip over comments and labels following openbrace.
857 (cond ((= (following-char) ?\#)
858 (forward-line 1))
859 ((= (following-char) ?\/)
860 (forward-char 2)
861 (search-forward "*/" nil 'move))
862 ;; case or label:
863 (t
864 (save-excursion (end-of-line)
865 (setq colon-line-end (point)))
866 (search-forward ":"))))
867 ;; The first following code counts
868 ;; if it is before the line we want to indent.
869 (and (< (point) indent-point)
870 (-
871 (if (> colon-line-end (point))
872 (- (current-indentation) c-label-offset)
873 (current-column))
874 ;; If prev stmt starts with open-brace, that
875 ;; open brace was offset by c-brace-offset.
876 ;; Compensate to get the column where
877 ;; an ordinary statement would start.
878 (if (= (following-char) ?\{) c-brace-offset 0)))))
879 ;; If no previous statement,
880 ;; indent it relative to line brace is on.
881 ;; For open brace in column zero, don't let statement
882 ;; start there too. If c-indent-level is zero,
883 ;; use c-brace-offset + c-continued-statement-offset instead.
884 ;; For open-braces not the first thing in a line,
885 ;; add in c-brace-imaginary-offset.
886 (+ (if (and (bolp) (zerop c-indent-level))
887 (+ c-brace-offset c-continued-statement-offset)
888 c-indent-level)
889 ;; Move back over whitespace before the openbrace.
890 ;; If openbrace is not first nonwhite thing on the line,
891 ;; add the c-brace-imaginary-offset.
892 (progn (skip-chars-backward " \t")
893 (if (bolp) 0 c-brace-imaginary-offset))
894 ;; If the openbrace is preceded by a parenthesized exp,
895 ;; move to the beginning of that;
896 ;; possibly a different line
897 (progn
898 (if (eq (preceding-char) ?\))
899 (forward-sexp -1))
900 ;; Get initial indentation of the line we are on.
901 (current-indentation))))))))))
902
903 (defun calculate-c-indent-within-comment (&optional after-star)
904 "Return the indentation amount for line inside a block comment.
905 Optional arg AFTER-STAR means, if lines in the comment have a leading star,
906 return the indentation of the text that would follow this star."
907 (let (end star-start)
908 (save-excursion
909 (beginning-of-line)
910 (skip-chars-forward " \t")
911 (setq star-start (= (following-char) ?\*))
912 (skip-chars-backward " \t\n")
913 (setq end (point))
914 (beginning-of-line)
915 (skip-chars-forward " \t")
916 (if after-star
917 (and (looking-at "\\*")
918 (re-search-forward "\\*[ \t]*")))
919 (and (re-search-forward "/\\*[ \t]*" end t)
920 star-start
921 (not after-star)
922 (goto-char (1+ (match-beginning 0))))
923 (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\*))
924 (1+ (current-column))
925 (current-column)))))
926
927
928 (defun c-backward-to-noncomment (lim)
929 (let (opoint stop)
930 (while (not stop)
931 (skip-chars-backward " \t\n\f" lim)
932 (setq opoint (point))
933 (if (and (>= (point) (+ 2 lim))
934 (save-excursion
935 (forward-char -2)
936 (looking-at "\\*/")))
937 (search-backward "/*" lim 'move)
938 (setq stop (or (<= (point) lim)
939 (save-excursion
940 (beginning-of-line)
941 (skip-chars-forward " \t")
942 (not (looking-at "#")))))
943 (or stop (beginning-of-line))))))
944
945 (defun c-backward-to-start-of-continued-exp (lim)
946 (if (memq (preceding-char) '(?\) ?\"))
947 (forward-sexp -1))
948 (beginning-of-line)
949 (if (<= (point) lim)
950 (goto-char (1+ lim)))
951 (skip-chars-forward " \t"))
952
953 (defun c-backward-to-start-of-if (&optional limit)
954 "Move to the start of the last \"unbalanced\" `if'."
955 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
956 (let ((if-level 1)
957 (case-fold-search nil))
958 (while (and (not (bobp)) (not (zerop if-level)))
959 (backward-sexp 1)
960 (cond ((and (looking-at "else\\b")
961 (not (looking-at "else\\s_")))
962 (setq if-level (1+ if-level)))
963 ((and (looking-at "if\\b")
964 (not (looking-at "if\\s_")))
965 (setq if-level (1- if-level)))
966 ((< (point) limit)
967 (setq if-level 0)
968 (goto-char limit))))))
969
970 (defun c-backward-to-start-of-do (&optional limit)
971 "If point follows a `do' statement, move to beginning of it and return t.
972 Otherwise return nil and don't move point."
973 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
974 (let ((first t)
975 (startpos (point))
976 (done nil))
977 (while (not done)
978 (let ((next-start (point)))
979 (condition-case nil
980 ;; Move back one token or one brace or paren group.
981 (backward-sexp 1)
982 ;; If we find an open-brace, we lose.
983 (error (setq done 'fail)))
984 (if done
985 nil
986 ;; If we reached a `do', we win.
987 (if (looking-at "do\\b")
988 (setq done 'succeed)
989 ;; Otherwise, if we skipped a semicolon, we lose.
990 ;; (Exception: we can skip one semicolon before getting
991 ;; to a the last token of the statement, unless that token
992 ;; is a close brace.)
993 (if (save-excursion
994 (forward-sexp 1)
995 (or (and (not first) (= (preceding-char) ?}))
996 (search-forward ";" next-start t
997 (if (and first
998 (/= (preceding-char) ?}))
999 2 1))))
1000 (setq done 'fail)
1001 (setq first nil)
1002 ;; If we go too far back in the buffer, we lose.
1003 (if (< (point) limit)
1004 (setq done 'fail)))))))
1005 (if (eq done 'succeed)
1006 t
1007 (goto-char startpos)
1008 nil)))
1009 \f
1010 (defun c-beginning-of-statement (count)
1011 "Go to the beginning of the innermost C statement.
1012 With prefix arg, go back N - 1 statements. If already at the beginning of a
1013 statement then go to the beginning of the preceding one.
1014 If within a string or comment, or next to a comment (only whitespace between),
1015 move by sentences instead of statements."
1016 (interactive "p")
1017 (let ((here (point)) state)
1018 (save-excursion
1019 (beginning-of-defun)
1020 (setq state (parse-partial-sexp (point) here nil nil)))
1021 (if (or (nth 3 state) (nth 4 state)
1022 (looking-at (concat "[ \t]*" comment-start-skip))
1023 (save-excursion (skip-chars-backward " \t")
1024 (goto-char (- (point) 2))
1025 (looking-at "\\*/")))
1026 (forward-sentence (- count))
1027 (while (> count 0)
1028 (c-beginning-of-statement-1)
1029 (setq count (1- count)))
1030 (while (< count 0)
1031 (c-end-of-statement-1)
1032 (setq count (1+ count))))))
1033
1034 (defun c-end-of-statement (count)
1035 "Go to the end of the innermost C statement.
1036 With prefix arg, go forward N - 1 statements.
1037 Move forward to end of the next statement if already at end.
1038 If within a string or comment, move by sentences instead of statements."
1039 (interactive "p")
1040 (c-beginning-of-statement (- count)))
1041
1042 (defun c-beginning-of-statement-1 ()
1043 (let ((last-begin (point))
1044 (first t))
1045 (condition-case ()
1046 (progn
1047 (while (and (not (bobp))
1048 (progn
1049 (backward-sexp 1)
1050 (or first
1051 (not (re-search-forward "[;{}]" last-begin t)))))
1052 (setq last-begin (point) first nil))
1053 (goto-char last-begin))
1054 (error (if first (backward-up-list 1) (goto-char last-begin))))))
1055
1056 (defun c-end-of-statement-1 ()
1057 (condition-case ()
1058 (progn
1059 (while (and (not (eobp))
1060 (let ((beg (point)))
1061 (forward-sexp 1)
1062 (let ((end (point)))
1063 (save-excursion
1064 (goto-char beg)
1065 (not (re-search-forward "[;{}]" end t)))))))
1066 (re-search-backward "[;}]")
1067 (forward-char 1))
1068 (error
1069 (let ((beg (point)))
1070 (backward-up-list -1)
1071 (let ((end (point)))
1072 (goto-char beg)
1073 (search-forward ";" end 'move))))))
1074 \f
1075 (defun mark-c-function ()
1076 "Put mark at end of C function, point at beginning."
1077 (interactive)
1078 (push-mark (point))
1079 (end-of-defun)
1080 (push-mark (point) nil t)
1081 (beginning-of-defun)
1082 (backward-paragraph))
1083 \f
1084 ;; Idea of ENDPOS is, indent each line, stopping when
1085 ;; ENDPOS is encountered. But it's too much of a pain to make that work.
1086 (defun indent-c-exp (&optional endpos)
1087 "Indent each line of the C grouping following point."
1088 (interactive)
1089 (let* ((indent-stack (list nil))
1090 (opoint (point)) ;; May be altered below.
1091 (contain-stack
1092 (list (if endpos
1093 (let (funbeg)
1094 ;; Find previous fcn-start.
1095 (save-excursion (forward-char 1)
1096 (beginning-of-defun)
1097 (setq funbeg (point)))
1098 (setq opoint funbeg)
1099 ;; Try to find containing open,
1100 ;; but don't scan past that fcn-start.
1101 (save-restriction
1102 (narrow-to-region funbeg (point))
1103 (condition-case nil
1104 (save-excursion
1105 (backward-up-list 1)
1106 (point))
1107 ;; We gave up: must be between fcns.
1108 ;; Set opoint to beg of prev fcn
1109 ;; since otherwise calculate-c-indent
1110 ;; will get wrong answers.
1111 (error (setq opoint funbeg)
1112 (point)))))
1113 (point))))
1114 (case-fold-search nil)
1115 restart outer-loop-done inner-loop-done state ostate
1116 this-indent last-sexp
1117 at-else at-brace at-while
1118 last-depth this-point
1119 (next-depth 0))
1120 ;; If the braces don't match, get an error right away.
1121 (save-excursion
1122 (forward-sexp 1))
1123 ;; Realign the comment on the first line, even though we don't reindent it.
1124 (save-excursion
1125 (let ((beg (point)))
1126 (and (re-search-forward
1127 comment-start-skip
1128 (save-excursion (end-of-line) (point)) t)
1129 ;; Make sure this isn't a comment alone on a line
1130 ;; (which should be indented like code instead).
1131 (save-excursion
1132 (goto-char (match-beginning 0))
1133 (skip-chars-backward " \t")
1134 (not (bolp)))
1135 ;; Make sure the comment starter we found
1136 ;; is not actually in a string or quoted.
1137 (let ((new-state
1138 (parse-partial-sexp beg (point)
1139 nil nil state)))
1140 (and (not (nth 3 new-state)) (not (nth 5 new-state))))
1141 (progn (indent-for-comment) (beginning-of-line)))))
1142 (save-excursion
1143 (setq outer-loop-done nil)
1144 (while (and (not (eobp))
1145 (if endpos (< (point) endpos)
1146 (not outer-loop-done)))
1147 (setq last-depth next-depth)
1148 ;; Compute how depth changes over this line
1149 ;; plus enough other lines to get to one that
1150 ;; does not end inside a comment or string.
1151 ;; Meanwhile, do appropriate indentation on comment lines.
1152 (setq inner-loop-done nil)
1153 (while (and (not inner-loop-done)
1154 (not (and (eobp) (setq outer-loop-done t))))
1155 (setq ostate state)
1156 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1157 nil nil state))
1158 (setq next-depth (car state))
1159 (if (and (car (cdr (cdr state)))
1160 (>= (car (cdr (cdr state))) 0))
1161 (setq last-sexp (car (cdr (cdr state)))))
1162 ;; If this line started within a comment, indent it as such.
1163 (if (or (nth 4 ostate) (nth 7 ostate))
1164 (c-indent-line))
1165 ;; If it ends outside of comments or strings, exit the inner loop.
1166 ;; Otherwise move on to next line.
1167 (if (or (nth 3 state) (nth 4 state) (nth 7 state))
1168 (forward-line 1)
1169 (setq inner-loop-done t)))
1170 (and endpos
1171 (while (< next-depth 0)
1172 (setq indent-stack (append indent-stack (list nil)))
1173 (setq contain-stack (append contain-stack (list nil)))
1174 (setq next-depth (1+ next-depth))
1175 (setq last-depth (1+ last-depth))
1176 (setcar (nthcdr 6 state) (1+ (nth 6 state)))))
1177 (setq outer-loop-done (and (not endpos) (<= next-depth 0)))
1178 (if outer-loop-done
1179 nil
1180 ;; If this line had ..))) (((.. in it, pop out of the levels
1181 ;; that ended anywhere in this line, even if the final depth
1182 ;; doesn't indicate that they ended.
1183 (while (> last-depth (nth 6 state))
1184 (setq indent-stack (cdr indent-stack)
1185 contain-stack (cdr contain-stack)
1186 last-depth (1- last-depth)))
1187 (if (/= last-depth next-depth)
1188 (setq last-sexp nil))
1189 ;; Add levels for any parens that were started in this line.
1190 (while (< last-depth next-depth)
1191 (setq indent-stack (cons nil indent-stack)
1192 contain-stack (cons nil contain-stack)
1193 last-depth (1+ last-depth)))
1194 (if (null (car contain-stack))
1195 (setcar contain-stack (or (car (cdr state))
1196 (save-excursion (forward-sexp -1)
1197 (point)))))
1198 (forward-line 1)
1199 (skip-chars-forward " \t")
1200 ;; Don't really reindent if the line is just whitespace,
1201 ;; or if it is past the endpos.
1202 ;; (The exit test in the outer while
1203 ;; does not exit until we have passed the first line
1204 ;; past the region.)
1205 (if (or (eolp) (and endpos (>= (point) endpos)))
1206 nil
1207 ;; Is this line in a new nesting level?
1208 ;; In other words, is this the first line that
1209 ;; starts in the new level?
1210 (if (and (car indent-stack)
1211 (>= (car indent-stack) 0))
1212 nil
1213 ;; Yes.
1214 ;; Compute the standard indent for this level.
1215 (let (val)
1216 (if (= (char-after (car contain-stack)) ?{)
1217 (save-excursion
1218 (goto-char (car contain-stack))
1219 (setq val (+ c-indent-level (current-column))))
1220 (setq val (calculate-c-indent
1221 (if (car indent-stack)
1222 (- (car indent-stack))
1223 opoint))))
1224 ;; t means we are in a block comment and should
1225 ;; calculate accordingly.
1226 (if (eq val t)
1227 (setq val (calculate-c-indent-within-comment)))
1228 (setcar indent-stack val)))
1229 ;; Adjust indent of this individual line
1230 ;; based on its predecessor.
1231 ;; Handle continuation lines, if, else, while, and so on.
1232 (if (/= (char-after (car contain-stack)) ?{)
1233 (setq this-indent (car indent-stack))
1234 ;; Line is at statement level.
1235 ;; Is it a new statement? Is it an else?
1236 ;; Find last non-comment character before this line
1237 (save-excursion
1238 (setq this-point (point))
1239 (setq at-else (and (looking-at "else\\b")
1240 (not (looking-at "else\\s_"))))
1241 (setq at-brace (= (following-char) ?{))
1242 (setq at-while (and (looking-at "while\\b")
1243 (not (looking-at "while\\s_"))))
1244 (if (= (following-char) ?})
1245 (setq this-indent (car indent-stack))
1246 (c-backward-to-noncomment opoint)
1247 (if (not (memq (preceding-char) '(0 ?\, ?\; ?} ?: ?{)))
1248 ;; Preceding line did not end in comma or semi;
1249 ;; indent this line c-continued-statement-offset
1250 ;; more than previous.
1251 (progn
1252 (c-backward-to-start-of-continued-exp (car contain-stack))
1253 (setq this-indent
1254 (+ c-continued-statement-offset (current-column)
1255 (if at-brace c-continued-brace-offset 0))))
1256 ;; Preceding line ended in comma or semi;
1257 ;; use the standard indent for this level.
1258 (cond (at-else (progn (c-backward-to-start-of-if opoint)
1259 (setq this-indent
1260 (current-indentation))))
1261 ((and at-while (c-backward-to-start-of-do opoint))
1262 (setq this-indent (current-indentation)))
1263 ((eq (preceding-char) ?\,)
1264 (goto-char this-point)
1265 (setq this-indent (calculate-c-indent)))
1266 (t (setq this-indent (car indent-stack))))))))
1267 ;; Adjust line indentation according to its contents
1268 (if (or (looking-at c-switch-label-regexp)
1269 (and (looking-at "[A-Za-z]")
1270 (save-excursion
1271 (forward-sexp 1)
1272 (looking-at ":"))))
1273 (setq this-indent (max 1 (+ this-indent c-label-offset))))
1274 (if (= (following-char) ?})
1275 (setq this-indent (- this-indent c-indent-level)))
1276 (if (= (following-char) ?{)
1277 ;; Don't move an open-brace in column 0.
1278 ;; This is good when constructs such as
1279 ;; `extern "C" {' surround a function definition
1280 ;; that should be indented as usual.
1281 ;; It is also good for nested functions.
1282 ;; It is bad when an open-brace is indented at column 0
1283 ;; and you want to fix that, but we can't win 'em all.
1284 (if (zerop (current-column))
1285 (setq this-indent 0)
1286 (setq this-indent (+ this-indent c-brace-offset))))
1287 ;; Don't leave indentation in empty lines.
1288 (if (eolp) (setq this-indent 0))
1289 ;; Put chosen indentation into effect.
1290 (or (= (current-column) this-indent)
1291 (= (following-char) ?\#)
1292 (progn
1293 (delete-region (point) (progn (beginning-of-line) (point)))
1294 (indent-to this-indent)))
1295 ;; Indent any comment following the text.
1296 (or (looking-at comment-start-skip)
1297 (save-excursion
1298 (let ((beg (point)))
1299 (and (re-search-forward
1300 comment-start-skip
1301 (save-excursion (end-of-line) (point)) t)
1302 ;; Make sure the comment starter we found
1303 ;; is not actually in a string or quoted.
1304 (let ((new-state
1305 (parse-partial-sexp beg (point)
1306 nil nil state)))
1307 (and (not (nth 3 new-state)) (not (nth 5 new-state))))
1308 (indent-for-comment)))))))))))
1309
1310 ;; Look at all comment-start strings in the current line after point.
1311 ;; Return t if one of them starts a real comment.
1312 ;; This is not used yet, because indent-for-comment
1313 ;; isn't smart enough to handle the cases this can find.
1314 (defun indent-c-find-real-comment ()
1315 (let (win)
1316 (while (and (not win)
1317 (re-search-forward comment-start-skip
1318 (save-excursion (end-of-line) (point))
1319 t))
1320 ;; Make sure the comment start is not quoted.
1321 (let ((state-1
1322 (parse-partial-sexp
1323 (save-excursion (beginning-of-line) (point))
1324 (point) nil nil state)))
1325 (setq win (and (null (nth 3 state-1)) (null (nth 5 state-1))))))
1326 win))
1327
1328 ;; Indent every line whose first char is between START and END inclusive.
1329 (defun c-indent-region (start end)
1330 (save-excursion
1331 (goto-char start)
1332 ;; Advance to first nonblank line.
1333 (skip-chars-forward " \t\n")
1334 (beginning-of-line)
1335 (let ((endmark (copy-marker end))
1336 (c-tab-always-indent t))
1337 (while (and (bolp) (not (eobp)) (< (point) endmark))
1338 ;; Indent one line as with TAB.
1339 (let ((shift-amt (c-indent-line))
1340 nextline sexpbeg sexpend)
1341 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*#"))
1342 (forward-line 1)
1343 (save-excursion
1344 ;; Find beginning of following line.
1345 (save-excursion
1346 (forward-line 1) (setq nextline (point)))
1347 ;; Find first beginning-of-sexp for sexp extending past this line.
1348 (beginning-of-line)
1349 (while (< (point) nextline)
1350 (condition-case nil
1351 (progn
1352 (forward-sexp 1)
1353 (setq sexpend (point-marker)))
1354 (error (setq sexpend nil)
1355 (goto-char nextline)))
1356 (skip-chars-forward " \t\n"))
1357 (if sexpend
1358 (progn
1359 ;; Make sure the sexp we found really starts on the
1360 ;; current line and extends past it.
1361 (goto-char sexpend)
1362 (backward-sexp 1)
1363 (setq sexpbeg (point)))))
1364 ;; If that sexp ends within the region,
1365 ;; indent it all at once, fast.
1366 (if (and sexpend (> sexpend nextline) (<= sexpend endmark)
1367 (< sexpbeg nextline))
1368 (progn
1369 (indent-c-exp)
1370 (goto-char sexpend)))
1371 ;; Move to following line and try again.
1372 (and sexpend (set-marker sexpend nil))
1373 (forward-line 1))))
1374 (set-marker endmark nil))))
1375 \f
1376 (defun set-c-style (style &optional global)
1377 "Set C-mode variables to use one of several different indentation styles.
1378 The arguments are a string representing the desired style
1379 and a flag which, if non-nil, means to set the style globally.
1380 \(Interactively, the flag comes from the prefix argument.)
1381 Available styles are GNU, K&R, BSD and Whitesmith."
1382 (interactive (list (let ((completion-ignore-case t))
1383 (completing-read "Use which C indentation style? "
1384 c-style-alist nil t))
1385 current-prefix-arg))
1386 (let ((vars (cdr (assoc style c-style-alist))))
1387 (or vars
1388 (error "Invalid C indentation style `%s'" style))
1389 (while vars
1390 (or global
1391 (make-local-variable (car (car vars))))
1392 (set (car (car vars)) (cdr (car vars)))
1393 (setq vars (cdr vars)))))
1394 \f
1395 ;;; This page handles insertion and removal of backslashes for C macros.
1396
1397 (defvar c-backslash-column 48
1398 "*Minimum column for end-of-line backslashes of macro definitions.")
1399
1400 (defun c-backslash-region (from to delete-flag)
1401 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1402 With no argument, inserts backslashes and aligns existing backslashes.
1403 With an argument, deletes the backslashes.
1404
1405 This function does not modify the last line of the region if the region ends
1406 right at the start of the following line; it does not modify blank lines
1407 at the start of the region. So you can put the region around an entire macro
1408 definition and conveniently use this command."
1409 (interactive "r\nP")
1410 (save-excursion
1411 (goto-char from)
1412 (let ((column c-backslash-column)
1413 (endmark (make-marker)))
1414 (move-marker endmark to)
1415 ;; Compute the smallest column number past the ends of all the lines.
1416 (if (not delete-flag)
1417 (while (< (point) to)
1418 (end-of-line)
1419 (if (= (preceding-char) ?\\)
1420 (progn (forward-char -1)
1421 (skip-chars-backward " \t")))
1422 (setq column (max column (1+ (current-column))))
1423 (forward-line 1)))
1424 ;; Adjust upward to a tab column, if that doesn't push past the margin.
1425 (if (> (% column tab-width) 0)
1426 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width)))
1427 (if (< adjusted (window-width))
1428 (setq column adjusted))))
1429 ;; Don't modify blank lines at start of region.
1430 (goto-char from)
1431 (while (and (< (point) endmark) (eolp))
1432 (forward-line 1))
1433 ;; Add or remove backslashes on all the lines.
1434 (while (and (< (point) endmark)
1435 ;; Don't backslashify the last line
1436 ;; if the region ends right at the start of the next line.
1437 (save-excursion
1438 (forward-line 1)
1439 (< (point) endmark)))
1440 (if (not delete-flag)
1441 (c-append-backslash column)
1442 (c-delete-backslash))
1443 (forward-line 1))
1444 (move-marker endmark nil))))
1445
1446 (defun c-append-backslash (column)
1447 (end-of-line)
1448 ;; Note that "\\\\" is needed to get one backslash.
1449 (if (= (preceding-char) ?\\)
1450 (progn (forward-char -1)
1451 (delete-horizontal-space)
1452 (indent-to column))
1453 (indent-to column)
1454 (insert "\\")))
1455
1456 (defun c-delete-backslash ()
1457 (end-of-line)
1458 (or (bolp)
1459 (progn
1460 (forward-char -1)
1461 (if (looking-at "\\\\")
1462 (delete-region (1+ (point))
1463 (progn (skip-chars-backward " \t") (point)))))))
1464 \f
1465 (defun c-up-conditional (count)
1466 "Move back to the containing preprocessor conditional, leaving mark behind.
1467 A prefix argument acts as a repeat count. With a negative argument,
1468 move forward to the end of the containing preprocessor conditional.
1469 When going backwards, `#elif' is treated like `#else' followed by `#if'.
1470 When going forwards, `#elif' is ignored."
1471 (interactive "p")
1472 (c-forward-conditional (- count) t))
1473
1474 (defun c-backward-conditional (count &optional up-flag)
1475 "Move back across a preprocessor conditional, leaving mark behind.
1476 A prefix argument acts as a repeat count. With a negative argument,
1477 move forward across a preprocessor conditional."
1478 (interactive "p")
1479 (c-forward-conditional (- count) up-flag))
1480
1481 (defun c-forward-conditional (count &optional up-flag)
1482 "Move forward across a preprocessor conditional, leaving mark behind.
1483 A prefix argument acts as a repeat count. With a negative argument,
1484 move backward across a preprocessor conditional."
1485 (interactive "p")
1486 (let* ((forward (> count 0))
1487 (increment (if forward -1 1))
1488 (search-function (if forward 're-search-forward 're-search-backward))
1489 (opoint (point))
1490 (new))
1491 (save-excursion
1492 (while (/= count 0)
1493 (let ((depth (if up-flag 0 -1)) found)
1494 (save-excursion
1495 ;; Find the "next" significant line in the proper direction.
1496 (while (and (not found)
1497 ;; Rather than searching for a # sign that comes
1498 ;; at the beginning of a line aside from whitespace,
1499 ;; search first for a string starting with # sign.
1500 ;; Then verify what precedes it.
1501 ;; This is faster on account of the fastmap feature of
1502 ;; the regexp matcher.
1503 (funcall search-function
1504 "#[ \t]*\\(if\\|elif\\|endif\\)"
1505 nil t))
1506 (beginning-of-line)
1507 ;; Now verify it is really a preproc line.
1508 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
1509 (let ((prev depth))
1510 ;; Update depth according to what we found.
1511 (beginning-of-line)
1512 (cond ((looking-at "[ \t]*#[ \t]*endif")
1513 (setq depth (+ depth increment)))
1514 ((looking-at "[ \t]*#[ \t]*elif")
1515 (if (and forward (= depth 0))
1516 (setq found (point))))
1517 (t (setq depth (- depth increment))))
1518 ;; If we are trying to move across, and we find
1519 ;; an end before we find a beginning, get an error.
1520 (if (and (< prev 0) (< depth prev))
1521 (error (if forward
1522 "No following conditional at this level"
1523 "No previous conditional at this level")))
1524 ;; When searching forward, start from next line
1525 ;; so that we don't find the same line again.
1526 (if forward (forward-line 1))
1527 ;; If this line exits a level of conditional, exit inner loop.
1528 (if (< depth 0)
1529 (setq found (point))))
1530 ;; If the line is not really a conditional, skip past it.
1531 (if forward (end-of-line)))))
1532 (or found
1533 (error "No containing preprocessor conditional"))
1534 (goto-char (setq new found)))
1535 (setq count (+ count increment))))
1536 (push-mark)
1537 (goto-char new)))
1538
1539 ;;; c-mode.el ends here