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