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