1 ;;; c-mode.el --- C code editing commands for Emacs
4 ;; Last-Modified: 11 Jul 1992
7 ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 (defvar c-mode-abbrev-table nil
28 "Abbrev table in use in C mode.")
29 (define-abbrev-table 'c-mode-abbrev-table ())
32 "Keymap used in C mode.")
35 (setq c-mode-map (make-sparse-keymap))
36 (define-key c-mode-map "{" 'electric-c-brace)
37 (define-key c-mode-map "}" 'electric-c-brace)
38 (define-key c-mode-map ";" 'electric-c-semi)
39 (define-key c-mode-map "#" 'electric-c-sharp-sign)
40 (define-key c-mode-map ":" 'electric-c-terminator)
41 (define-key c-mode-map "\e\C-h" 'mark-c-function)
42 (define-key c-mode-map "\e\C-q" 'indent-c-exp)
43 (define-key c-mode-map "\ea" 'c-beginning-of-statement)
44 (define-key c-mode-map "\ee" 'c-end-of-statement)
45 (define-key c-mode-map "\eq" 'c-fill-paragraph)
46 (define-key c-mode-map "\177" 'backward-delete-char-untabify)
47 (define-key c-mode-map "\t" 'c-indent-command))
49 ;; cmacexp is lame because it uses no preprocessor symbols.
50 ;; It isn't very extensible either -- hardcodes /lib/cpp.
51 (autoload 'c-macro-expand "cmacexp"
52 "Display the result of expanding all C macros occurring in the region.
53 The expansion is entirely correct because it uses the C preprocessor."
56 (defvar c-mode-syntax-table nil
57 "Syntax table in use in C-mode buffers.")
59 (if c-mode-syntax-table
61 (setq c-mode-syntax-table (make-syntax-table))
62 (modify-syntax-entry ?\\ "\\" c-mode-syntax-table)
63 (modify-syntax-entry ?/ ". 14" c-mode-syntax-table)
64 (modify-syntax-entry ?* ". 23" c-mode-syntax-table)
65 (modify-syntax-entry ?+ "." c-mode-syntax-table)
66 (modify-syntax-entry ?- "." c-mode-syntax-table)
67 (modify-syntax-entry ?= "." c-mode-syntax-table)
68 (modify-syntax-entry ?% "." c-mode-syntax-table)
69 (modify-syntax-entry ?< "." c-mode-syntax-table)
70 (modify-syntax-entry ?> "." c-mode-syntax-table)
71 (modify-syntax-entry ?& "." c-mode-syntax-table)
72 (modify-syntax-entry ?| "." c-mode-syntax-table)
73 (modify-syntax-entry ?\' "\"" c-mode-syntax-table))
75 (defconst c-indent-level 2
76 "*Indentation of C statements with respect to containing block.")
77 (defconst c-brace-imaginary-offset 0
78 "*Imagined indentation of a C open brace that actually follows a statement.")
79 (defconst c-brace-offset 0
80 "*Extra indentation for braces, compared with other text in same context.")
81 (defconst c-argdecl-indent 5
82 "*Indentation level of declarations of C function arguments.")
83 (defconst c-label-offset -2
84 "*Offset of C label lines and case statements relative to usual indentation.")
85 (defconst c-continued-statement-offset 2
86 "*Extra indent for lines not starting new statements.")
87 (defconst c-continued-brace-offset 0
88 "*Extra indent for substatements that start with open-braces.
89 This is in addition to c-continued-statement-offset.")
90 (defconst c-style-alist
93 (c-argdecl-indent . 5)
96 (c-continued-statement-offset . 2))
99 (c-argdecl-indent . 0)
100 (c-brace-offset . -5)
101 (c-label-offset . -5)
102 (c-continued-statement-offset . 5))
105 (c-argdecl-indent . 4)
106 (c-brace-offset . -4)
107 (c-label-offset . -4)
108 (c-continued-statement-offset . 4))
111 (c-continued-statement-offset . 4)
112 (c-brace-offset . -4)
113 (c-argdecl-indent . 0)
114 (c-label-offset . -4)
115 (c-auto-newline . t))
118 (c-argdecl-indent . 4)
120 (c-label-offset . -4)
121 (c-continued-statement-offset . 4))))
123 (defconst c-auto-newline nil
124 "*Non-nil means automatically newline before and after braces,
125 and after colons and semicolons, inserted in C code.
126 If you do not want a leading newline before braces then use:
127 (define-key c-mode-map "{" 'electric-c-semi)")
129 (defconst c-tab-always-indent t
130 "*Non-nil means TAB in C mode should always reindent the current line,
131 regardless of where in the line point is when the TAB command is used.")
133 (defun set-c-style (&optional style)
134 "Set up the c-mode style variables from the c-style variable or if
135 STYLE argument is given, use that. It makes the c indentation style
136 variables buffer local."
138 (let ((c-styles (mapcar 'car c-style-alist)))
141 (let ((style-string ; get style name with completion
143 (format "Set c mode indentation style to (default %s): "
146 (function (lambda (arg) (memq arg c-styles)))
148 (if (string-equal "" style-string)
150 (intern style-string))
152 (setq style (or style c-style)) ; use c-style if style is nil
153 (make-local-variable 'c-style)
154 (if (memq style c-styles)
156 (error "Bad c style: %s" style)
158 (message "c-style: %s" c-style)
159 ; finally, set the indentation style variables making each one local
160 (mapcar (function (lambda (c-style-pair)
161 (make-local-variable (car c-style-pair))
162 (set (car c-style-pair)
163 (cdr c-style-pair))))
164 (cdr (assq c-style c-style-alist)))
170 "Major mode for editing C code.
171 Expression and list commands understand all C brackets.
172 Tab indents for C code.
173 Comments are delimited with /* ... */.
174 Paragraphs are separated by blank lines only.
175 Delete converts tabs to spaces as it moves back.
177 Variables controlling indentation style:
179 Non-nil means TAB in C mode should always reindent the current line,
180 regardless of where in the line point is when the TAB command is used.
182 Non-nil means automatically newline before and after braces,
183 and after colons and semicolons, inserted in C code.
185 Indentation of C statements within surrounding block.
186 The surrounding block's indentation is the indentation
187 of the line on which the open-brace appears.
188 c-continued-statement-offset
189 Extra indentation given to a substatement, such as the
190 then-clause of an if or body of a while.
191 c-continued-brace-offset
192 Extra indentation given to a brace that starts a substatement.
193 This is in addition to c-continued-statement-offset.
195 Extra indentation for line if it starts with an open brace.
196 c-brace-imaginary-offset
197 An open brace following other text is treated as if it were
198 this far to the right of the start of its line.
200 Indentation level of declarations of C function arguments.
202 Extra indentation for line that is a label, or case or default.
204 Settings for K&R and BSD indentation styles are
206 c-continued-statement-offset 5 8
211 Turning on C mode calls the value of the variable c-mode-hook with no args,
212 if that value is non-nil."
214 (kill-all-local-variables)
215 (use-local-map c-mode-map)
216 (setq major-mode 'c-mode)
218 (setq local-abbrev-table c-mode-abbrev-table)
219 (set-syntax-table c-mode-syntax-table)
220 (make-local-variable 'paragraph-start)
221 (setq paragraph-start (concat "^$\\|" page-delimiter))
222 (make-local-variable 'paragraph-separate)
223 (setq paragraph-separate paragraph-start)
224 (make-local-variable 'paragraph-ignore-fill-prefix)
225 (setq paragraph-ignore-fill-prefix t)
226 (make-local-variable 'indent-line-function)
227 (setq indent-line-function 'c-indent-line)
228 (make-local-variable 'indent-region-function)
229 (setq indent-region-function 'c-indent-region)
230 (make-local-variable 'require-final-newline)
231 (setq require-final-newline t)
232 (make-local-variable 'comment-start)
233 (setq comment-start "/* ")
234 (make-local-variable 'comment-end)
235 (setq comment-end " */")
236 (make-local-variable 'comment-column)
237 (setq comment-column 32)
238 (make-local-variable 'comment-start-skip)
239 (setq comment-start-skip "/\\*+ *")
240 (make-local-variable 'comment-indent-hook)
241 (setq comment-indent-hook 'c-comment-indent)
242 (make-local-variable 'parse-sexp-ignore-comments)
243 (setq parse-sexp-ignore-comments t)
244 (run-hooks 'c-mode-hook))
246 ;; This is used by indent-for-comment
247 ;; to decide how much to indent a comment in C code
248 ;; based on its context.
249 (defun c-comment-indent ()
250 (if (looking-at "^/\\*")
251 0 ;Existing comment at bol stays there.
252 (let ((opoint (point)))
255 (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)")
256 ;; A comment following a solitary close-brace
257 ;; should have only one space.
259 (1+ (current-column)))
260 ((or (looking-at "^#[ \t]*endif[ \t]*")
261 (looking-at "^#[ \t]*else[ \t]*"))
262 7) ;2 spaces after #endif
265 (skip-chars-backward " \t")
266 (and (= comment-column 0) (bolp)))
267 ;; If comment-column is 0, and nothing but space
268 ;; before the comment, align it at 0 rather than 1.
271 (max (1+ (current-column)) ;Else indent at comment column
272 comment-column))))))) ; except leave at least one space.
274 (defun c-fill-paragraph (&optional arg)
275 "Like \\[fill-paragraph] but handle C comments.
276 If point is inside a comment, the current paragraph of the comment
277 is filled, preserving the comment indentation or line-starting decorations."
282 (skip-chars-forward " \t")
283 (looking-at comment-start-skip))))
285 (eq (calculate-c-indent) t))
286 ;; Inside a comment: fill one comment paragraph.
288 ;; The prefix for each line of this paragraph
289 ;; is the appropriate part of the start of this line,
290 ;; up to the column at which text should be indented.
293 (if (looking-at "[ \t]*/\\*.*\\*/")
294 (progn (re-search-forward comment-start-skip)
295 (make-string (current-column) ?\ ))
296 (if first-line (forward-line 1))
297 (buffer-substring (point)
300 (calculate-c-indent-within-comment t)
304 ;; Lines containing just a comment start or just an end
305 ;; should not be filled into paragraphs they are next to.
306 (concat paragraph-start
307 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[^ \t/*]"))
309 (concat paragraph-separate
310 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[^ \t/*]")))
312 ;; Don't fill the comment together with the code following it.
313 (narrow-to-region (point-min)
315 (search-forward "*/" nil 'move)
320 (search-forward "*/")
322 (if (looking-at "[ \t]*\\*/")
323 (delete-indentation)))))
324 ;; Outside of comments: do ordinary filling.
325 (fill-paragraph arg))))
327 (defun electric-c-brace (arg)
328 "Insert character and correct line's indentation."
334 (skip-chars-backward " \t")
336 (if c-auto-newline (progn (c-indent-line) (newline) t) nil)))
338 (insert last-command-char)
343 ;; (newline) may have done auto-fill
344 (setq insertpos (- (point) 2))
347 (if insertpos (goto-char (1+ insertpos)))
351 (goto-char insertpos)
352 (self-insert-command (prefix-numeric-value arg)))
353 (self-insert-command (prefix-numeric-value arg)))))
355 (defun electric-c-sharp-sign (arg)
356 "Insert character and correct line's indentation."
359 (skip-chars-backward " \t")
361 (let ((c-auto-newline nil))
362 (electric-c-terminator arg))
363 (self-insert-command (prefix-numeric-value arg))))
365 (defun electric-c-semi (arg)
366 "Insert character and correct line's indentation."
369 (electric-c-terminator arg)
370 (self-insert-command (prefix-numeric-value arg))))
372 (defun electric-c-terminator (arg)
373 "Insert character and correct line's indentation."
375 (let (insertpos (end (point)))
376 (if (and (not arg) (eolp)
379 (skip-chars-forward " \t")
380 (or (= (following-char) ?#)
381 ;; Colon is special only after a label, or case ....
382 ;; So quickly rule out most other uses of colon
383 ;; and do no indentation for them.
384 (and (eq last-command-char ?:)
385 (not (looking-at "case[ \t'/(]\\|default\\>"))
387 (skip-chars-forward "a-zA-Z0-9_$")
388 (skip-chars-forward " \t")
392 (let ((pps (parse-partial-sexp (point) end)))
393 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
395 (insert last-command-char)
398 (not (c-inside-parens-p))
401 ;; (newline) may have done auto-fill
402 (setq insertpos (- (point) 2))
405 (if insertpos (goto-char (1+ insertpos)))
409 (goto-char insertpos)
410 (self-insert-command (prefix-numeric-value arg)))
411 (self-insert-command (prefix-numeric-value arg)))))
413 (defun c-inside-parens-p ()
417 (narrow-to-region (point)
418 (progn (beginning-of-defun) (point)))
419 (goto-char (point-max))
420 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
423 (defun c-indent-command (&optional whole-exp)
424 "Indent current line as C code, or in some cases insert a tab character.
425 If `c-tab-always-indent' is non-nil (the default), always indent current line.
426 Otherwise, indent the current line only if point is at the left margin or
427 in the line's indentation; otherwise insert a tab.
429 A numeric argument, regardless of its value, means indent rigidly all the
430 lines of the expression starting after point so that this line becomes
431 properly indented. The relative indentation among the lines of the
432 expression are preserved."
435 ;; If arg, always indent this line as C
436 ;; and shift remaining lines of expression the same amount.
437 (let ((shift-amt (c-indent-line))
440 (if c-tab-always-indent
442 ;; Find beginning of following line.
444 (forward-line 1) (setq beg (point)))
445 ;; Find first beginning-of-sexp for sexp extending past this line.
446 (while (< (point) beg)
449 (skip-chars-forward " \t\n")))
451 (indent-code-rigidly beg end shift-amt "#")))
452 (if (and (not c-tab-always-indent)
454 (skip-chars-backward " \t")
459 (defun c-indent-line ()
460 "Indent current line as C code.
461 Return the amount the indentation changed by."
462 (let ((indent (calculate-c-indent nil))
464 (case-fold-search nil)
465 (pos (- (point-max) (point))))
468 (cond ((eq indent nil)
469 (setq indent (current-indentation)))
471 (setq indent (calculate-c-indent-within-comment)))
472 ((looking-at "[ \t]*#")
475 (skip-chars-forward " \t")
476 (if (listp indent) (setq indent (car indent)))
477 (cond ((or (looking-at "case[ \t'/(]\\|default\\>")
478 (and (looking-at "[A-Za-z]")
482 (setq indent (max 1 (+ indent c-label-offset))))
483 ((and (looking-at "else\\b")
484 (not (looking-at "else\\s_")))
485 (setq indent (save-excursion
486 (c-backward-to-start-of-if)
487 (current-indentation))))
488 ((looking-at "}[ \t]*else")
489 (setq indent (save-excursion
492 (current-indentation))))
493 ((and (looking-at "while\\b")
495 (c-backward-to-start-of-do)))
496 ;; This is a `while' that ends a do-while.
497 (setq indent (save-excursion
498 (c-backward-to-start-of-do)
499 (current-indentation))))
500 ((= (following-char) ?})
501 (setq indent (- indent c-indent-level)))
502 ((= (following-char) ?{)
503 (setq indent (+ indent c-brace-offset))))))
504 (skip-chars-forward " \t")
505 (setq shift-amt (- indent (current-column)))
506 (if (zerop shift-amt)
507 (if (> (- (point-max) pos) (point))
508 (goto-char (- (point-max) pos)))
509 (delete-region beg (point))
511 ;; If initial point was within line's indentation,
512 ;; position after the indentation. Else stay at same point in text.
513 (if (> (- (point-max) pos) (point))
514 (goto-char (- (point-max) pos))))
517 (defun calculate-c-indent (&optional parse-start)
518 "Return appropriate indentation for current line as C code.
519 In usual case returns an integer: the column to indent to.
520 Returns nil if line starts inside a string, t if in a comment."
523 (let ((indent-point (point))
524 (case-fold-search nil)
528 (goto-char parse-start)
529 (beginning-of-defun))
530 (while (< (point) indent-point)
531 (setq parse-start (point))
532 (setq state (parse-partial-sexp (point) indent-point 0))
533 (setq containing-sexp (car (cdr state))))
534 (cond ((or (nth 3 state) (nth 4 state))
535 ;; return nil or t if should not change this line
537 ((null containing-sexp)
538 ;; Line is at top level. May be data or function definition,
539 ;; or may be function argument declaration.
540 ;; Indent like the previous top level line
541 ;; unless that ends in a closeparen without semicolon,
542 ;; in which case this line is the first argument decl.
543 (goto-char indent-point)
544 (skip-chars-forward " \t")
545 (if (= (following-char) ?{)
546 0 ; Unless it starts a function body
547 (c-backward-to-noncomment (or parse-start (point-min)))
548 ;; Look at previous line that's at column 0
549 ;; to determine whether we are in top-level decls
550 ;; or function's arg decls. Set basic-indent accordingly.
553 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
555 (if (and (looking-at "\\sw\\|\\s_")
556 (looking-at "[^\"\n=]*(")
558 (goto-char (1- (match-end 0)))
561 (skip-chars-forward " \t\f")
562 (and (< (point) indent-point)
563 (not (memq (following-char)
565 ;; Make sure the "function decl" we found
566 ;; is not inside a comment.
569 (while (and (not comment)
570 (search-forward "/*" lim t))
572 (not (search-forward "*/" lim t))))
574 c-argdecl-indent 0)))))
577 ;; ;; Now add a little if this is a continuation line.
578 ;; (+ basic-indent (if (or (bobp)
579 ;; (memq (preceding-char) '(?\) ?\; ?\}))
580 ;; ;; Line with zero indentation
581 ;; ;; is probably the return-type
582 ;; ;; of a function definition,
583 ;; ;; so following line is function name.
584 ;; (= (current-indentation) 0))
585 ;; 0 c-continued-statement-offset))
587 ((/= (char-after containing-sexp) ?{)
588 ;; line is expression, not statement:
589 ;; indent to just after the surrounding open.
590 (goto-char (1+ containing-sexp))
593 ;; Statement level. Is it a continuation or a new statement?
594 ;; Find previous non-comment character.
595 (goto-char indent-point)
596 (c-backward-to-noncomment containing-sexp)
597 ;; Back up over label lines, since they don't
598 ;; affect whether our line is a continuation.
599 (while (or (eq (preceding-char) ?\,)
600 (and (eq (preceding-char) ?:)
601 (or (eq (char-after (- (point) 2)) ?\')
602 (memq (char-syntax (char-after (- (point) 2)))
604 (if (eq (preceding-char) ?\,)
605 (progn (forward-char -1)
606 (c-backward-to-start-of-continued-exp containing-sexp)))
608 (c-backward-to-noncomment containing-sexp))
609 ;; Check for a preprocessor statement or its continuation lines.
610 ;; Move back to end of previous non-preprocessor line.
611 (let ((found (point)) stop)
613 (cond ((save-excursion (end-of-line 0)
614 (= (preceding-char) ?\\))
616 ;; This line is not preceded by a backslash.
617 ;; So either it starts a preprocessor command
618 ;; or any following continuation lines
619 ;; should not be skipped.
620 ((progn (beginning-of-line) (= (following-char) ?#))
622 (setq found (point)))
625 ;; Now we get the answer.
626 (if (and (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?\{)))
627 ;; But don't treat a line with a close-brace
628 ;; as a continuation. It is probably the
629 ;; end of an enum type declaration.
631 (goto-char indent-point)
632 (skip-chars-forward " \t")
633 (not (= (following-char) ?}))))
634 ;; This line is continuation of preceding line's statement;
635 ;; indent c-continued-statement-offset more than the
636 ;; previous line of the statement.
638 (c-backward-to-start-of-continued-exp containing-sexp)
639 (+ c-continued-statement-offset (current-column)
640 (if (save-excursion (goto-char indent-point)
641 (skip-chars-forward " \t")
642 (eq (following-char) ?{))
643 c-continued-brace-offset 0)))
644 ;; This line starts a new statement.
645 ;; Position following last unclosed open.
646 (goto-char containing-sexp)
647 ;; Is line first statement after an open-brace?
649 ;; If no, find that first statement and indent like it.
652 (let ((colon-line-end 0))
653 (while (progn (skip-chars-forward " \t\n")
654 (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:"))
655 ;; Skip over comments and labels following openbrace.
656 (cond ((= (following-char) ?\#)
658 ((= (following-char) ?\/)
660 (search-forward "*/" nil 'move))
663 (save-excursion (end-of-line)
664 (setq colon-line-end (point)))
665 (search-forward ":"))))
666 ;; The first following code counts
667 ;; if it is before the line we want to indent.
668 (and (< (point) indent-point)
669 (if (> colon-line-end (point))
670 (- (current-indentation) c-label-offset)
672 ;; If no previous statement,
673 ;; indent it relative to line brace is on.
674 ;; For open brace in column zero, don't let statement
675 ;; start there too. If c-indent-level is zero,
676 ;; use c-brace-offset + c-continued-statement-offset instead.
677 ;; For open-braces not the first thing in a line,
678 ;; add in c-brace-imaginary-offset.
679 (+ (if (and (bolp) (zerop c-indent-level))
680 (+ c-brace-offset c-continued-statement-offset)
682 ;; Move back over whitespace before the openbrace.
683 ;; If openbrace is not first nonwhite thing on the line,
684 ;; add the c-brace-imaginary-offset.
685 (progn (skip-chars-backward " \t")
686 (if (bolp) 0 c-brace-imaginary-offset))
687 ;; If the openbrace is preceded by a parenthesized exp,
688 ;; move to the beginning of that;
689 ;; possibly a different line
691 (if (eq (preceding-char) ?\))
693 ;; Get initial indentation of the line we are on.
694 (current-indentation))))))))))
696 (defun calculate-c-indent-within-comment (&optional after-star)
697 "Return the indentation amount for line inside a block comment.
698 Optional arg AFTER-STAR means, if lines in the comment have a leading star,
699 return the indentation of the text that would follow this star."
700 (let (end star-start)
703 (skip-chars-forward " \t")
704 (setq star-start (= (following-char) ?\*))
705 (skip-chars-backward " \t\n")
708 (skip-chars-forward " \t")
710 (and (looking-at "\\*")
711 (re-search-forward "\\*[ \t]*")))
712 (and (re-search-forward "/\\*[ \t]*" end t)
715 (goto-char (1+ (match-beginning 0))))
716 (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\*))
717 (1+ (current-column))
721 (defun c-backward-to-noncomment (lim)
724 (skip-chars-backward " \t\n\f" lim)
725 (setq opoint (point))
726 (if (and (>= (point) (+ 2 lim))
729 (looking-at "\\*/")))
730 (search-backward "/*" lim 'move)
731 (setq stop (or (<= (point) lim)
734 (skip-chars-forward " \t")
735 (not (looking-at "#")))))
736 (or stop (beginning-of-line))))))
738 (defun c-backward-to-start-of-continued-exp (lim)
739 (if (memq (preceding-char) '(?\) ?\"))
743 (goto-char (1+ lim)))
744 (skip-chars-forward " \t"))
746 (defun c-backward-to-start-of-if (&optional limit)
747 "Move to the start of the last \"unbalanced\" `if'."
748 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
750 (case-fold-search nil))
751 (while (and (not (bobp)) (not (zerop if-level)))
753 (cond ((looking-at "else\\b")
754 (setq if-level (1+ if-level)))
755 ((looking-at "if\\b")
756 (setq if-level (1- if-level)))
759 (goto-char limit))))))
761 (defun c-backward-to-start-of-do (&optional limit)
762 "If point follows a `do' statement, move to beginning of it and return t.
763 Otherwise return nil and don't move point."
764 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
769 (let ((next-start (point)))
771 ;; Move back one token or one brace or paren group.
773 ;; If we find an open-brace, we lose.
774 (error (setq done 'fail)))
777 ;; If we reached a `do', we win.
778 (if (looking-at "do\\b")
780 ;; Otherwise, if we skipped a semicolon, we lose.
781 ;; (Exception: we can skip one semicolon before getting
782 ;; to a the last token of the statement, unless that token
783 ;; is a close brace.)
786 (or (and (not first) (= (preceding-char) ?}))
787 (search-forward ";" next-start t
789 (/= (preceding-char) ?}))
793 ;; If we go too far back in the buffer, we lose.
794 (if (< (point) limit)
795 (setq done 'fail)))))))
796 (if (eq done 'succeed)
801 (defun c-beginning-of-statement (count)
802 "Go to the beginning of the innermost C statement.
803 With prefix arg, go back N - 1 statements. If already at the beginning of a
804 statement then go to the beginning of the preceeding one."
807 (c-beginning-of-statement-1)
808 (setq count (1- count)))
810 (c-end-of-statement-1)
811 (setq count (1+ count))))
813 (defun c-end-of-statement (count)
814 "Go to the end of the innermost C statement.
815 With prefix arg, go forward N - 1 statements. Moves forward to end of the
816 next statement if already at end."
818 (c-beginning-of-statement (- count)))
820 (defun c-beginning-of-statement-1 ()
821 (let ((last-begin (point))
825 (while (and (not (bobp))
829 (not (re-search-forward "[;{}]" last-begin t)))))
830 (setq last-begin (point) first nil))
831 (goto-char last-begin))
832 (error (if first (backward-up-list 1) (goto-char last-begin))))))
834 (defun c-end-of-statement-1 ()
837 (while (and (not (eobp))
843 (not (re-search-forward "[;{}]" end t)))))))
844 (re-search-backward "[;}]")
848 (backward-up-list -1)
851 (search-forward ";" end 'move))))))
853 (defun mark-c-function ()
854 "Put mark at end of C function, point at beginning."
860 (backward-paragraph))
862 (defun indent-c-exp (&optional endpos)
863 "Indent each line of the C grouping following point.
864 If optional arg ENDPOS is given, indent each line, stopping when
865 ENDPOS is encountered."
867 (let* ((indent-stack (list nil))
868 (opoint (point)) ;; May be altered below.
872 ;; Find previous fcn-start.
873 (save-excursion (forward-char 1)
875 (setq funbeg (point)))
876 ;; Try to find containing open,
877 ;; but don't scan past that fcn-start.
879 (narrow-to-region funbeg (point))
882 (backward-up-list 1) (point))
883 ;; We gave up: must be between fcns.
884 ;; Set opoint to beg of prev fcn
885 ;; since otherwise calculate-c-indent
886 ;; will get wrong answers.
887 (error (setq opoint funbeg)
890 (case-fold-search nil)
891 restart outer-loop-done inner-loop-done state ostate
892 this-indent last-sexp
893 at-else at-brace at-while
896 ;; If the braces don't match, get an error right away.
899 ;; Realign the comment on the first line, even though we don't reindent it.
902 (and (re-search-forward
904 (save-excursion (end-of-line) (point)) t)
905 ;; Make sure the comment starter we found
906 ;; is not actually in a string or quoted.
908 (parse-partial-sexp beg (point)
910 (and (not (nth 3 new-state)) (not (nth 5 new-state))))
911 (progn (indent-for-comment) (beginning-of-line)))))
913 (setq outer-loop-done nil)
914 (while (and (not (eobp))
915 (if endpos (< (point) endpos)
916 (not outer-loop-done)))
917 (setq last-depth next-depth)
918 ;; Compute how depth changes over this line
919 ;; plus enough other lines to get to one that
920 ;; does not end inside a comment or string.
921 ;; Meanwhile, do appropriate indentation on comment lines.
922 (setq inner-loop-done nil)
923 (while (and (not inner-loop-done)
924 (not (and (eobp) (setq outer-loop-done t))))
926 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
928 (setq next-depth (car state))
929 (if (and (car (cdr (cdr state)))
930 (>= (car (cdr (cdr state))) 0))
931 (setq last-sexp (car (cdr (cdr state)))))
932 (if (or (nth 4 ostate))
934 (if (or (nth 3 state))
936 (setq inner-loop-done t)))
938 (while (< next-depth 0)
939 (setq indent-stack (append indent-stack (list nil)))
940 (setq contain-stack (append contain-stack (list nil)))
941 (setq next-depth (1+ next-depth))
942 (setq last-depth (1+ last-depth))
943 (setcar (nthcdr 6 state) (1+ (nth 6 state)))))
944 (setq outer-loop-done (and (not endpos) (<= next-depth 0)))
947 ;; If this line had ..))) (((.. in it, pop out of the levels
948 ;; that ended anywhere in this line, even if the final depth
949 ;; doesn't indicate that they ended.
950 (while (> last-depth (nth 6 state))
951 (setq indent-stack (cdr indent-stack)
952 contain-stack (cdr contain-stack)
953 last-depth (1- last-depth)))
954 (if (/= last-depth next-depth)
955 (setq last-sexp nil))
956 ;; Add levels for any parens that were started in this line.
957 (while (< last-depth next-depth)
958 (setq indent-stack (cons nil indent-stack)
959 contain-stack (cons nil contain-stack)
960 last-depth (1+ last-depth)))
961 (if (null (car contain-stack))
962 (setcar contain-stack (or (car (cdr state))
963 (save-excursion (forward-sexp -1)
966 (skip-chars-forward " \t")
969 (if (and (car indent-stack)
970 (>= (car indent-stack) 0))
971 ;; Line is on an existing nesting level.
972 ;; Lines inside parens are handled specially.
973 (if (/= (char-after (car contain-stack)) ?{)
974 (setq this-indent (car indent-stack))
975 ;; Line is at statement level.
976 ;; Is it a new statement? Is it an else?
977 ;; Find last non-comment character before this line
979 (setq at-else (looking-at "else\\W"))
980 (setq at-brace (= (following-char) ?{))
981 (setq at-while (looking-at "while\\b"))
982 (c-backward-to-noncomment opoint)
983 (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?{)))
984 ;; Preceding line did not end in comma or semi;
985 ;; indent this line c-continued-statement-offset
986 ;; more than previous.
988 (c-backward-to-start-of-continued-exp (car contain-stack))
990 (+ c-continued-statement-offset (current-column)
991 (if at-brace c-continued-brace-offset 0))))
992 ;; Preceding line ended in comma or semi;
993 ;; use the standard indent for this level.
994 (cond (at-else (progn (c-backward-to-start-of-if opoint)
996 (current-indentation))))
997 ((and at-while (c-backward-to-start-of-do opoint))
998 (setq this-indent (current-indentation)))
999 (t (setq this-indent (car indent-stack)))))))
1000 ;; Just started a new nesting level.
1001 ;; Compute the standard indent for this level.
1002 (let ((val (calculate-c-indent
1003 (if (car indent-stack)
1004 (- (car indent-stack))
1006 (setcar indent-stack
1007 (setq this-indent val))))
1008 ;; Adjust line indentation according to its contents
1009 (if (or (looking-at "case[ \t'/(]\\|default\\>")
1010 (and (looking-at "[A-Za-z]")
1014 (setq this-indent (max 1 (+ this-indent c-label-offset))))
1015 (if (= (following-char) ?})
1016 (setq this-indent (- this-indent c-indent-level)))
1017 (if (= (following-char) ?{)
1018 (setq this-indent (+ this-indent c-brace-offset)))
1019 ;; Don't leave indentation in empty lines.
1020 (if (eolp) (setq this-indent 0))
1021 ;; Put chosen indentation into effect.
1022 (or (= (current-column) this-indent)
1023 (= (following-char) ?\#)
1025 (delete-region (point) (progn (beginning-of-line) (point)))
1026 (indent-to this-indent)))
1027 ;; Indent any comment following the text.
1028 (or (looking-at comment-start-skip)
1029 (let ((beg (point)))
1030 (and (re-search-forward
1032 (save-excursion (end-of-line) (point)) t)
1033 ;; Make sure the comment starter we found
1034 ;; is not actually in a string or quoted.
1036 (parse-partial-sexp beg (point)
1038 (and (not (nth 3 new-state)) (not (nth 5 new-state))))
1039 (progn (indent-for-comment) (beginning-of-line)))))))))))
1041 ;; Look at all comment-start strings in the current line after point.
1042 ;; Return t if one of them starts a real comment.
1043 ;; This is not used yet, because indent-for-comment
1044 ;; isn't smart enough to handle the cases this can find.
1045 (defun indent-c-find-real-comment ()
1047 (while (and (not win)
1048 (re-search-forward comment-start-skip
1049 (save-excursion (end-of-line) (point))
1051 ;; Make sure the comment start is not quoted.
1054 (save-excursion (beginning-of-line) (point))
1055 (point) nil nil state)))
1056 (setq win (and (null (nth 3 state-1)) (null (nth 5 state-1))))))
1059 ;; Indent every line whose first char is between START and END inclusive.
1060 (defun c-indent-region (start end)
1063 (let ((endmark (copy-marker end)))
1064 (and (bolp) (not (eolp))
1066 (indent-c-exp endmark)
1067 (set-marker endmark nil))))
1069 (defun set-c-style (style)
1070 "Set C-mode variables to use one of several different indentation styles.
1071 Takes one argument, a string representing the desired style.
1072 Available styles are GNU, K&R, BSD and Whitesmith."
1073 (interactive (list (completing-read "Use which C indentation style? "
1074 c-style-alist nil t)))
1075 (let ((vars (cdr (assoc style c-style-alist))))
1077 (if (interactive-p) (message "Using %s C indentation style" style))
1078 (error "Bogus style type, \"%s\"" style))
1080 (set (car (car vars)) (cdr (car vars)))
1081 (setq vars (cdr vars)))))
1083 ;;; c-mode.el ends here