1 ;;; c-mode.el --- C code editing commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 (defvar c-mode-abbrev-table nil
23 "Abbrev table in use in C mode.")
24 (define-abbrev-table 'c-mode-abbrev-table
())
27 "Keymap used in C mode.")
30 (setq c-mode-map
(make-sparse-keymap))
31 (define-key c-mode-map
"{" 'electric-c-brace
)
32 (define-key c-mode-map
"}" 'electric-c-brace
)
33 (define-key c-mode-map
";" 'electric-c-semi
)
34 (define-key c-mode-map
"#" 'electric-c-sharp-sign
)
35 (define-key c-mode-map
":" 'electric-c-terminator
)
36 (define-key c-mode-map
"\e\C-h" 'mark-c-function
)
37 (define-key c-mode-map
"\e\C-q" 'indent-c-exp
)
38 (define-key c-mode-map
"\ea" 'c-beginning-of-statement
)
39 (define-key c-mode-map
"\ee" 'c-end-of-statement
)
40 (define-key c-mode-map
"\eq" 'c-fill-paragraph
)
41 (define-key c-mode-map
"\177" 'backward-delete-char-untabify
)
42 (define-key c-mode-map
"\t" 'c-indent-command
))
44 ;; cmacexp is lame because it uses no preprocessor symbols.
45 ;; It isn't very extensible either -- hardcodes /lib/cpp.
46 (autoload 'c-macro-expand
"cmacexp"
47 "Display the result of expanding all C macros occurring in the region.
48 The expansion is entirely correct because it uses the C preprocessor."
51 (defvar c-mode-syntax-table nil
52 "Syntax table in use in C-mode buffers.")
54 (if c-mode-syntax-table
56 (setq c-mode-syntax-table
(make-syntax-table))
57 (modify-syntax-entry ?
\\ "\\" c-mode-syntax-table
)
58 (modify-syntax-entry ?
/ ". 14" c-mode-syntax-table
)
59 (modify-syntax-entry ?
* ". 23" c-mode-syntax-table
)
60 (modify-syntax-entry ?
+ "." c-mode-syntax-table
)
61 (modify-syntax-entry ?-
"." c-mode-syntax-table
)
62 (modify-syntax-entry ?
= "." c-mode-syntax-table
)
63 (modify-syntax-entry ?%
"." c-mode-syntax-table
)
64 (modify-syntax-entry ?
< "." c-mode-syntax-table
)
65 (modify-syntax-entry ?
> "." c-mode-syntax-table
)
66 (modify-syntax-entry ?
& "." c-mode-syntax-table
)
67 (modify-syntax-entry ?|
"." c-mode-syntax-table
)
68 (modify-syntax-entry ?
\' "\"" c-mode-syntax-table
))
70 (defconst c-indent-level
2
71 "*Indentation of C statements with respect to containing block.")
72 (defconst c-brace-imaginary-offset
0
73 "*Imagined indentation of a C open brace that actually follows a statement.")
74 (defconst c-brace-offset
0
75 "*Extra indentation for braces, compared with other text in same context.")
76 (defconst c-argdecl-indent
5
77 "*Indentation level of declarations of C function arguments.")
78 (defconst c-label-offset -
2
79 "*Offset of C label lines and case statements relative to usual indentation.")
80 (defconst c-continued-statement-offset
2
81 "*Extra indent for lines not starting new statements.")
82 (defconst c-continued-brace-offset
0
83 "*Extra indent for substatements that start with open-braces.
84 This is in addition to c-continued-statement-offset.")
85 (defconst c-style-alist
88 (c-argdecl-indent .
5)
91 (c-continued-statement-offset .
2))
94 (c-argdecl-indent .
0)
97 (c-continued-statement-offset .
5))
100 (c-argdecl-indent .
4)
101 (c-brace-offset . -
4)
102 (c-label-offset . -
4)
103 (c-continued-statement-offset .
4))
106 (c-continued-statement-offset .
4)
107 (c-brace-offset . -
4)
108 (c-argdecl-indent .
0)
109 (c-label-offset . -
4)
110 (c-auto-newline . t
))
113 (c-argdecl-indent .
4)
115 (c-label-offset . -
4)
116 (c-continued-statement-offset .
4))))
118 (defconst c-auto-newline nil
119 "*Non-nil means automatically newline before and after braces,
120 and after colons and semicolons, inserted in C code.
121 If you do not want a leading newline before braces then use:
122 (define-key c-mode-map "{" 'electric-c-semi)")
124 (defconst c-tab-always-indent t
125 "*Non-nil means TAB in C mode should always reindent the current line,
126 regardless of where in the line point is when the TAB command is used.")
128 (defun set-c-style (&optional style
)
129 "Set up the c-mode style variables from the c-style variable or if
130 STYLE argument is given, use that. It makes the c indentation style
131 variables buffer local."
133 (let ((c-styles (mapcar 'car c-style-alist
)))
136 (let ((style-string ; get style name with completion
138 (format "Set c mode indentation style to (default %s): "
141 (function (lambda (arg) (memq arg c-styles
)))
143 (if (string-equal "" style-string
)
145 (intern style-string
))
147 (setq style
(or style c-style
)) ; use c-style if style is nil
148 (make-local-variable 'c-style
)
149 (if (memq style c-styles
)
151 (error "Bad c style: %s" style
)
153 (message "c-style: %s" c-style
)
154 ; finally, set the indentation style variables making each one local
155 (mapcar (function (lambda (c-style-pair)
156 (make-local-variable (car c-style-pair
))
157 (set (car c-style-pair
)
158 (cdr c-style-pair
))))
159 (cdr (assq c-style c-style-alist
)))
165 "Major mode for editing C code.
166 Expression and list commands understand all C brackets.
167 Tab indents for C code.
168 Comments are delimited with /* ... */.
169 Paragraphs are separated by blank lines only.
170 Delete converts tabs to spaces as it moves back.
172 Variables controlling indentation style:
174 Non-nil means TAB in C mode should always reindent the current line,
175 regardless of where in the line point is when the TAB command is used.
177 Non-nil means automatically newline before and after braces,
178 and after colons and semicolons, inserted in C code.
180 Indentation of C statements within surrounding block.
181 The surrounding block's indentation is the indentation
182 of the line on which the open-brace appears.
183 c-continued-statement-offset
184 Extra indentation given to a substatement, such as the
185 then-clause of an if or body of a while.
186 c-continued-brace-offset
187 Extra indentation given to a brace that starts a substatement.
188 This is in addition to c-continued-statement-offset.
190 Extra indentation for line if it starts with an open brace.
191 c-brace-imaginary-offset
192 An open brace following other text is treated as if it were
193 this far to the right of the start of its line.
195 Indentation level of declarations of C function arguments.
197 Extra indentation for line that is a label, or case or default.
199 Settings for K&R and BSD indentation styles are
201 c-continued-statement-offset 5 8
206 Turning on C mode calls the value of the variable c-mode-hook with no args,
207 if that value is non-nil."
209 (kill-all-local-variables)
210 (use-local-map c-mode-map
)
211 (setq major-mode
'c-mode
)
213 (setq local-abbrev-table c-mode-abbrev-table
)
214 (set-syntax-table c-mode-syntax-table
)
215 (make-local-variable 'paragraph-start
)
216 (setq paragraph-start
(concat "^$\\|" page-delimiter
))
217 (make-local-variable 'paragraph-separate
)
218 (setq paragraph-separate paragraph-start
)
219 (make-local-variable 'paragraph-ignore-fill-prefix
)
220 (setq paragraph-ignore-fill-prefix t
)
221 (make-local-variable 'indent-line-function
)
222 (setq indent-line-function
'c-indent-line
)
223 (make-local-variable 'indent-region-function
)
224 (setq indent-region-function
'c-indent-region
)
225 (make-local-variable 'require-final-newline
)
226 (setq require-final-newline t
)
227 (make-local-variable 'comment-start
)
228 (setq comment-start
"/* ")
229 (make-local-variable 'comment-end
)
230 (setq comment-end
" */")
231 (make-local-variable 'comment-column
)
232 (setq comment-column
32)
233 (make-local-variable 'comment-start-skip
)
234 (setq comment-start-skip
"/\\*+ *")
235 (make-local-variable 'comment-indent-hook
)
236 (setq comment-indent-hook
'c-comment-indent
)
237 (make-local-variable 'parse-sexp-ignore-comments
)
238 (setq parse-sexp-ignore-comments t
)
239 (run-hooks 'c-mode-hook
))
241 ;; This is used by indent-for-comment
242 ;; to decide how much to indent a comment in C code
243 ;; based on its context.
244 (defun c-comment-indent ()
245 (if (looking-at "^/\\*")
246 0 ;Existing comment at bol stays there.
247 (let ((opoint (point)))
250 (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)")
251 ;; A comment following a solitary close-brace
252 ;; should have only one space.
254 (1+ (current-column)))
255 ((or (looking-at "^#[ \t]*endif[ \t]*")
256 (looking-at "^#[ \t]*else[ \t]*"))
257 7) ;2 spaces after #endif
260 (skip-chars-backward " \t")
261 (and (= comment-column
0) (bolp)))
262 ;; If comment-column is 0, and nothing but space
263 ;; before the comment, align it at 0 rather than 1.
266 (max (1+ (current-column)) ;Else indent at comment column
267 comment-column
))))))) ; except leave at least one space.
269 (defun c-fill-paragraph (&optional arg
)
270 "Like \\[fill-paragraph] but handle C comments.
271 If point is inside a comment, the current paragraph of the comment
272 is filled, preserving the comment indentation or line-starting decorations."
277 (skip-chars-forward " \t")
278 (looking-at comment-start-skip
))))
280 (eq (calculate-c-indent) t
))
281 ;; Inside a comment: fill one comment paragraph.
283 ;; The prefix for each line of this paragraph
284 ;; is the appropriate part of the start of this line,
285 ;; up to the column at which text should be indented.
288 (if (looking-at "[ \t]*/\\*.*\\*/")
289 (progn (re-search-forward comment-start-skip
)
290 (make-string (current-column) ?\
))
291 (if first-line
(forward-line 1))
292 (buffer-substring (point)
295 (calculate-c-indent-within-comment t
)
299 ;; Lines containing just a comment start or just an end
300 ;; should not be filled into paragraphs they are next to.
301 (concat paragraph-start
302 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[^ \t/*]"))
304 (concat paragraph-separate
305 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[^ \t/*]")))
307 ;; Don't fill the comment together with the code following it.
308 (narrow-to-region (point-min)
310 (search-forward "*/" nil
'move
)
315 (search-forward "*/")
317 (if (looking-at "[ \t]*\\*/")
318 (delete-indentation)))))
319 ;; Outside of comments: do ordinary filling.
320 (fill-paragraph arg
))))
322 (defun electric-c-brace (arg)
323 "Insert character and correct line's indentation."
329 (skip-chars-backward " \t")
331 (if c-auto-newline
(progn (c-indent-line) (newline) t
) nil
)))
333 (insert last-command-char
)
338 ;; (newline) may have done auto-fill
339 (setq insertpos
(- (point) 2))
342 (if insertpos
(goto-char (1+ insertpos
)))
346 (goto-char insertpos
)
347 (self-insert-command (prefix-numeric-value arg
)))
348 (self-insert-command (prefix-numeric-value arg
)))))
350 (defun electric-c-sharp-sign (arg)
351 "Insert character and correct line's indentation."
354 (skip-chars-backward " \t")
356 (let ((c-auto-newline nil
))
357 (electric-c-terminator arg
))
358 (self-insert-command (prefix-numeric-value arg
))))
360 (defun electric-c-semi (arg)
361 "Insert character and correct line's indentation."
364 (electric-c-terminator arg
)
365 (self-insert-command (prefix-numeric-value arg
))))
367 (defun electric-c-terminator (arg)
368 "Insert character and correct line's indentation."
370 (let (insertpos (end (point)))
371 (if (and (not arg
) (eolp)
374 (skip-chars-forward " \t")
375 (or (= (following-char) ?
#)
376 ;; Colon is special only after a label, or case ....
377 ;; So quickly rule out most other uses of colon
378 ;; and do no indentation for them.
379 (and (eq last-command-char ?
:)
380 (not (looking-at "case[ \t'/(]\\|default\\>"))
382 (skip-chars-forward "a-zA-Z0-9_$")
383 (skip-chars-forward " \t")
387 (let ((pps (parse-partial-sexp (point) end
)))
388 (or (nth 3 pps
) (nth 4 pps
) (nth 5 pps
))))))))
390 (insert last-command-char
)
393 (not (c-inside-parens-p))
396 ;; (newline) may have done auto-fill
397 (setq insertpos
(- (point) 2))
400 (if insertpos
(goto-char (1+ insertpos
)))
404 (goto-char insertpos
)
405 (self-insert-command (prefix-numeric-value arg
)))
406 (self-insert-command (prefix-numeric-value arg
)))))
408 (defun c-inside-parens-p ()
412 (narrow-to-region (point)
413 (progn (beginning-of-defun) (point)))
414 (goto-char (point-max))
415 (= (char-after (or (scan-lists (point) -
1 1) (point-min))) ?\
()))
418 (defun c-indent-command (&optional whole-exp
)
419 "Indent current line as C code, or in some cases insert a tab character.
420 If `c-tab-always-indent' is non-nil (the default), always indent current line.
421 Otherwise, indent the current line only if point is at the left margin or
422 in the line's indentation; otherwise insert a tab.
424 A numeric argument, regardless of its value, means indent rigidly all the
425 lines of the expression starting after point so that this line becomes
426 properly indented. The relative indentation among the lines of the
427 expression are preserved."
430 ;; If arg, always indent this line as C
431 ;; and shift remaining lines of expression the same amount.
432 (let ((shift-amt (c-indent-line))
435 (if c-tab-always-indent
437 ;; Find beginning of following line.
439 (forward-line 1) (setq beg
(point)))
440 ;; Find first beginning-of-sexp for sexp extending past this line.
441 (while (< (point) beg
)
444 (skip-chars-forward " \t\n")))
446 (indent-code-rigidly beg end shift-amt
"#")))
447 (if (and (not c-tab-always-indent
)
449 (skip-chars-backward " \t")
454 (defun c-indent-line ()
455 "Indent current line as C code.
456 Return the amount the indentation changed by."
457 (let ((indent (calculate-c-indent nil
))
459 (case-fold-search nil
)
460 (pos (- (point-max) (point))))
463 (cond ((eq indent nil
)
464 (setq indent
(current-indentation)))
466 (setq indent
(calculate-c-indent-within-comment)))
467 ((looking-at "[ \t]*#")
470 (skip-chars-forward " \t")
471 (if (listp indent
) (setq indent
(car indent
)))
472 (cond ((or (looking-at "case[ \t'/(]\\|default\\>")
473 (and (looking-at "[A-Za-z]")
477 (setq indent
(max 1 (+ indent c-label-offset
))))
478 ((and (looking-at "else\\b")
479 (not (looking-at "else\\s_")))
480 (setq indent
(save-excursion
481 (c-backward-to-start-of-if)
482 (current-indentation))))
483 ((looking-at "}[ \t]*else")
484 (setq indent
(save-excursion
487 (current-indentation))))
488 ((and (looking-at "while\\b")
490 (c-backward-to-start-of-do)))
491 ;; This is a `while' that ends a do-while.
492 (setq indent
(save-excursion
493 (c-backward-to-start-of-do)
494 (current-indentation))))
495 ((= (following-char) ?
})
496 (setq indent
(- indent c-indent-level
)))
497 ((= (following-char) ?
{)
498 (setq indent
(+ indent c-brace-offset
))))))
499 (skip-chars-forward " \t")
500 (setq shift-amt
(- indent
(current-column)))
501 (if (zerop shift-amt
)
502 (if (> (- (point-max) pos
) (point))
503 (goto-char (- (point-max) pos
)))
504 (delete-region beg
(point))
506 ;; If initial point was within line's indentation,
507 ;; position after the indentation. Else stay at same point in text.
508 (if (> (- (point-max) pos
) (point))
509 (goto-char (- (point-max) pos
))))
512 (defun calculate-c-indent (&optional parse-start
)
513 "Return appropriate indentation for current line as C code.
514 In usual case returns an integer: the column to indent to.
515 Returns nil if line starts inside a string, t if in a comment."
518 (let ((indent-point (point))
519 (case-fold-search nil
)
523 (goto-char parse-start
)
524 (beginning-of-defun))
525 (while (< (point) indent-point
)
526 (setq parse-start
(point))
527 (setq state
(parse-partial-sexp (point) indent-point
0))
528 (setq containing-sexp
(car (cdr state
))))
529 (cond ((or (nth 3 state
) (nth 4 state
))
530 ;; return nil or t if should not change this line
532 ((null containing-sexp
)
533 ;; Line is at top level. May be data or function definition,
534 ;; or may be function argument declaration.
535 ;; Indent like the previous top level line
536 ;; unless that ends in a closeparen without semicolon,
537 ;; in which case this line is the first argument decl.
538 (goto-char indent-point
)
539 (skip-chars-forward " \t")
540 (if (= (following-char) ?
{)
541 0 ; Unless it starts a function body
542 (c-backward-to-noncomment (or parse-start
(point-min)))
543 ;; Look at previous line that's at column 0
544 ;; to determine whether we are in top-level decls
545 ;; or function's arg decls. Set basic-indent accordingly.
548 (re-search-backward "^[^ \^L\t\n#]" nil
'move
)
549 (if (and (looking-at "\\sw\\|\\s_")
550 (looking-at "[^\"\n=]*(")
552 (goto-char (1- (match-end 0)))
554 (skip-chars-forward " \t\f")
555 (and (< (point) indent-point
)
556 (not (memq (following-char)
558 c-argdecl-indent
0))))
561 ;; ;; Now add a little if this is a continuation line.
562 ;; (+ basic-indent (if (or (bobp)
563 ;; (memq (preceding-char) '(?\) ?\; ?\}))
564 ;; ;; Line with zero indentation
565 ;; ;; is probably the return-type
566 ;; ;; of a function definition,
567 ;; ;; so following line is function name.
568 ;; (= (current-indentation) 0))
569 ;; 0 c-continued-statement-offset))
571 ((/= (char-after containing-sexp
) ?
{)
572 ;; line is expression, not statement:
573 ;; indent to just after the surrounding open.
574 (goto-char (1+ containing-sexp
))
577 ;; Statement level. Is it a continuation or a new statement?
578 ;; Find previous non-comment character.
579 (goto-char indent-point
)
580 (c-backward-to-noncomment containing-sexp
)
581 ;; Back up over label lines, since they don't
582 ;; affect whether our line is a continuation.
583 (while (or (eq (preceding-char) ?\
,)
584 (and (eq (preceding-char) ?
:)
585 (or (eq (char-after (- (point) 2)) ?
\')
586 (memq (char-syntax (char-after (- (point) 2)))
588 (if (eq (preceding-char) ?\
,)
589 (progn (forward-char -
1)
590 (c-backward-to-start-of-continued-exp containing-sexp
)))
592 (c-backward-to-noncomment containing-sexp
))
593 ;; Check for a preprocessor statement or its continuation lines.
594 ;; Move back to end of previous non-preprocessor line.
595 (let ((found (point)) stop
)
597 (cond ((save-excursion (end-of-line 0)
598 (= (preceding-char) ?
\\))
600 ;; This line is not preceded by a backslash.
601 ;; So either it starts a preprocessor command
602 ;; or any following continuation lines
603 ;; should not be skipped.
604 ((progn (beginning-of-line) (= (following-char) ?
#))
606 (setq found
(point)))
609 ;; Now we get the answer.
610 (if (and (not (memq (preceding-char) '(nil ?\
, ?\
; ?\} ?\{)))
611 ;; But don't treat a line with a close-brace
612 ;; as a continuation. It is probably the
613 ;; end of an enum type declaration.
615 (goto-char indent-point
)
616 (skip-chars-forward " \t")
617 (not (= (following-char) ?
}))))
618 ;; This line is continuation of preceding line's statement;
619 ;; indent c-continued-statement-offset more than the
620 ;; previous line of the statement.
622 (c-backward-to-start-of-continued-exp containing-sexp
)
623 (+ c-continued-statement-offset
(current-column)
624 (if (save-excursion (goto-char indent-point
)
625 (skip-chars-forward " \t")
626 (eq (following-char) ?
{))
627 c-continued-brace-offset
0)))
628 ;; This line starts a new statement.
629 ;; Position following last unclosed open.
630 (goto-char containing-sexp
)
631 ;; Is line first statement after an open-brace?
633 ;; If no, find that first statement and indent like it.
636 (let ((colon-line-end 0))
637 (while (progn (skip-chars-forward " \t\n")
638 (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:"))
639 ;; Skip over comments and labels following openbrace.
640 (cond ((= (following-char) ?\
#)
642 ((= (following-char) ?\
/)
644 (search-forward "*/" nil
'move
))
647 (save-excursion (end-of-line)
648 (setq colon-line-end
(point)))
649 (search-forward ":"))))
650 ;; The first following code counts
651 ;; if it is before the line we want to indent.
652 (and (< (point) indent-point
)
653 (if (> colon-line-end
(point))
654 (- (current-indentation) c-label-offset
)
656 ;; If no previous statement,
657 ;; indent it relative to line brace is on.
658 ;; For open brace in column zero, don't let statement
659 ;; start there too. If c-indent-level is zero,
660 ;; use c-brace-offset + c-continued-statement-offset instead.
661 ;; For open-braces not the first thing in a line,
662 ;; add in c-brace-imaginary-offset.
663 (+ (if (and (bolp) (zerop c-indent-level
))
664 (+ c-brace-offset c-continued-statement-offset
)
666 ;; Move back over whitespace before the openbrace.
667 ;; If openbrace is not first nonwhite thing on the line,
668 ;; add the c-brace-imaginary-offset.
669 (progn (skip-chars-backward " \t")
670 (if (bolp) 0 c-brace-imaginary-offset
))
671 ;; If the openbrace is preceded by a parenthesized exp,
672 ;; move to the beginning of that;
673 ;; possibly a different line
675 (if (eq (preceding-char) ?\
))
677 ;; Get initial indentation of the line we are on.
678 (current-indentation))))))))))
680 (defun calculate-c-indent-within-comment (&optional after-star
)
681 "Return the indentation amount for line inside a block comment.
682 Optional arg AFTER-STAR means, if lines in the comment have a leading star,
683 return the indentation of the text that would follow this star."
684 (let (end star-start
)
687 (skip-chars-forward " \t")
688 (setq star-start
(= (following-char) ?\
*))
689 (skip-chars-backward " \t\n")
692 (skip-chars-forward " \t")
694 (and (looking-at "\\*")
695 (re-search-forward "\\*[ \t]*")))
696 (and (re-search-forward "/\\*[ \t]*" end t
)
699 (goto-char (1+ (match-beginning 0))))
700 (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\
*))
701 (1+ (current-column))
705 (defun c-backward-to-noncomment (lim)
708 (skip-chars-backward " \t\n\f" lim
)
709 (setq opoint
(point))
710 (if (and (>= (point) (+ 2 lim
))
713 (looking-at "\\*/")))
714 (search-backward "/*" lim
'move
)
715 (setq stop
(or (<= (point) lim
)
718 (skip-chars-forward " \t")
719 (not (looking-at "#")))))
720 (or stop
(beginning-of-line))))))
722 (defun c-backward-to-start-of-continued-exp (lim)
723 (if (memq (preceding-char) '(?\
) ?
\"))
727 (goto-char (1+ lim
)))
728 (skip-chars-forward " \t"))
730 (defun c-backward-to-start-of-if (&optional limit
)
731 "Move to the start of the last \"unbalanced\" `if'."
732 (or limit
(setq limit
(save-excursion (beginning-of-defun) (point))))
734 (case-fold-search nil
))
735 (while (and (not (bobp)) (not (zerop if-level
)))
737 (cond ((looking-at "else\\b")
738 (setq if-level
(1+ if-level
)))
739 ((looking-at "if\\b")
740 (setq if-level
(1- if-level
)))
743 (goto-char limit
))))))
745 (defun c-backward-to-start-of-do (&optional limit
)
746 "If point follows a `do' statement, move to beginning of it and return t.
747 Otherwise return nil and don't move point."
748 (or limit
(setq limit
(save-excursion (beginning-of-defun) (point))))
753 (let ((next-start (point)))
755 ;; Move back one token or one brace or paren group.
757 ;; If we find an open-brace, we lose.
758 (error (setq done
'fail
)))
761 ;; If we reached a `do', we win.
762 (if (looking-at "do\\b")
764 ;; Otherwise, if we skipped a semicolon, we lose.
765 ;; (Exception: we can skip one semicolon before getting
766 ;; to a the last token of the statement, unless that token
767 ;; is a close brace.)
770 (or (and (not first
) (= (preceding-char) ?
}))
771 (search-forward ";" next-start t
773 (/= (preceding-char) ?
}))
777 ;; If we go too far back in the buffer, we lose.
778 (if (< (point) limit
)
779 (setq done
'fail
)))))))
780 (if (eq done
'succeed
)
785 (defun c-beginning-of-statement (count)
786 "Go to the beginning of the innermost C statement.
787 With prefix arg, go back N - 1 statements. If already at the beginning of a
788 statement then go to the beginning of the preceeding one."
791 (c-beginning-of-statement-1)
792 (setq count
(1- count
)))
794 (c-end-of-statement-1)
795 (setq count
(1+ count
))))
797 (defun c-end-of-statement (count)
798 "Go to the end of the innermost C statement.
799 With prefix arg, go forward N - 1 statements. Moves forward to end of the
800 next statement if already at end."
802 (c-beginning-of-statement (- count
)))
804 (defun c-beginning-of-statement-1 ()
805 (let ((last-begin (point))
809 (while (and (not (bobp))
813 (not (re-search-forward "[;{}]" last-begin t
)))))
814 (setq last-begin
(point) first nil
))
815 (goto-char last-begin
))
816 (error (if first
(backward-up-list 1) (goto-char last-begin
))))))
818 (defun c-end-of-statement-1 ()
821 (while (and (not (eobp))
827 (not (re-search-forward "[;{}]" end t
)))))))
828 (re-search-backward "[;}]")
832 (backward-up-list -
1)
835 (search-forward ";" end
'move
))))))
837 (defun mark-c-function ()
838 "Put mark at end of C function, point at beginning."
844 (backward-paragraph))
846 (defun indent-c-exp (&optional endpos
)
847 "Indent each line of the C grouping following point.
848 If optional arg ENDPOS is given, indent each line, stopping when
849 ENDPOS is encountered."
851 (let* ((indent-stack (list nil
))
852 (opoint (point)) ;; May be altered below.
856 ;; Find previous fcn-start.
857 (save-excursion (forward-char 1)
859 (setq funbeg
(point)))
860 ;; Try to find containing open,
861 ;; but don't scan past that fcn-start.
863 (narrow-to-region funbeg
(point))
866 (backward-up-list 1) (point))
867 ;; We gave up: must be between fcns.
868 ;; Set opoint to beg of prev fcn
869 ;; since otherwise calculate-c-indent
870 ;; will get wrong answers.
871 (error (setq opoint funbeg
)
874 (case-fold-search nil
)
875 restart outer-loop-done inner-loop-done state ostate
876 this-indent last-sexp
877 at-else at-brace at-while
880 ;; If the braces don't match, get an error right away.
883 ;; Realign the comment on the first line, even though we don't reindent it.
886 (and (re-search-forward
888 (save-excursion (end-of-line) (point)) t
)
889 ;; Make sure the comment starter we found
890 ;; is not actually in a string or quoted.
892 (parse-partial-sexp beg
(point)
894 (and (not (nth 3 new-state
)) (not (nth 5 new-state
))))
895 (progn (indent-for-comment) (beginning-of-line)))))
897 (setq outer-loop-done nil
)
898 (while (and (not (eobp))
899 (if endpos
(< (point) endpos
)
900 (not outer-loop-done
)))
901 (setq last-depth next-depth
)
902 ;; Compute how depth changes over this line
903 ;; plus enough other lines to get to one that
904 ;; does not end inside a comment or string.
905 ;; Meanwhile, do appropriate indentation on comment lines.
906 (setq inner-loop-done nil
)
907 (while (and (not inner-loop-done
)
908 (not (and (eobp) (setq outer-loop-done t
))))
910 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
912 (setq next-depth
(car state
))
913 (if (and (car (cdr (cdr state
)))
914 (>= (car (cdr (cdr state
))) 0))
915 (setq last-sexp
(car (cdr (cdr state
)))))
916 (if (or (nth 4 ostate
))
918 (if (or (nth 3 state
))
920 (setq inner-loop-done t
)))
922 (while (< next-depth
0)
923 (setq indent-stack
(append indent-stack
(list nil
)))
924 (setq contain-stack
(append contain-stack
(list nil
)))
925 (setq next-depth
(1+ next-depth
))
926 (setq last-depth
(1+ last-depth
))
927 (setcar (nthcdr 6 state
) (1+ (nth 6 state
)))))
928 (setq outer-loop-done
(and (not endpos
) (<= next-depth
0)))
931 ;; If this line had ..))) (((.. in it, pop out of the levels
932 ;; that ended anywhere in this line, even if the final depth
933 ;; doesn't indicate that they ended.
934 (while (> last-depth
(nth 6 state
))
935 (setq indent-stack
(cdr indent-stack
)
936 contain-stack
(cdr contain-stack
)
937 last-depth
(1- last-depth
)))
938 (if (/= last-depth next-depth
)
939 (setq last-sexp nil
))
940 ;; Add levels for any parens that were started in this line.
941 (while (< last-depth next-depth
)
942 (setq indent-stack
(cons nil indent-stack
)
943 contain-stack
(cons nil contain-stack
)
944 last-depth
(1+ last-depth
)))
945 (if (null (car contain-stack
))
946 (setcar contain-stack
(or (car (cdr state
))
947 (save-excursion (forward-sexp -
1)
950 (skip-chars-forward " \t")
953 (if (and (car indent-stack
)
954 (>= (car indent-stack
) 0))
955 ;; Line is on an existing nesting level.
956 ;; Lines inside parens are handled specially.
957 (if (/= (char-after (car contain-stack
)) ?
{)
958 (setq this-indent
(car indent-stack
))
959 ;; Line is at statement level.
960 ;; Is it a new statement? Is it an else?
961 ;; Find last non-comment character before this line
963 (setq at-else
(looking-at "else\\W"))
964 (setq at-brace
(= (following-char) ?
{))
965 (setq at-while
(looking-at "while\\b"))
966 (c-backward-to-noncomment opoint
)
967 (if (not (memq (preceding-char) '(nil ?\
, ?\
; ?} ?: ?{)))
968 ;; Preceding line did not end in comma or semi;
969 ;; indent this line c-continued-statement-offset
970 ;; more than previous.
972 (c-backward-to-start-of-continued-exp (car contain-stack
))
974 (+ c-continued-statement-offset
(current-column)
975 (if at-brace c-continued-brace-offset
0))))
976 ;; Preceding line ended in comma or semi;
977 ;; use the standard indent for this level.
978 (cond (at-else (progn (c-backward-to-start-of-if opoint
)
980 (current-indentation))))
981 ((and at-while
(c-backward-to-start-of-do opoint
))
982 (setq this-indent
(current-indentation)))
983 (t (setq this-indent
(car indent-stack
)))))))
984 ;; Just started a new nesting level.
985 ;; Compute the standard indent for this level.
986 (let ((val (calculate-c-indent
987 (if (car indent-stack
)
988 (- (car indent-stack
))
991 (setq this-indent val
))))
992 ;; Adjust line indentation according to its contents
993 (if (or (looking-at "case[ \t'/(]\\|default\\>")
994 (and (looking-at "[A-Za-z]")
998 (setq this-indent
(max 1 (+ this-indent c-label-offset
))))
999 (if (= (following-char) ?
})
1000 (setq this-indent
(- this-indent c-indent-level
)))
1001 (if (= (following-char) ?
{)
1002 (setq this-indent
(+ this-indent c-brace-offset
)))
1003 ;; Don't leave indentation in empty lines.
1004 (if (eolp) (setq this-indent
0))
1005 ;; Put chosen indentation into effect.
1006 (or (= (current-column) this-indent
)
1007 (= (following-char) ?\
#)
1009 (delete-region (point) (progn (beginning-of-line) (point)))
1010 (indent-to this-indent
)))
1011 ;; Indent any comment following the text.
1012 (or (looking-at comment-start-skip
)
1013 (let ((beg (point)))
1014 (and (re-search-forward
1016 (save-excursion (end-of-line) (point)) t
)
1017 ;; Make sure the comment starter we found
1018 ;; is not actually in a string or quoted.
1020 (parse-partial-sexp beg
(point)
1022 (and (not (nth 3 new-state
)) (not (nth 5 new-state
))))
1023 (progn (indent-for-comment) (beginning-of-line)))))))))))
1025 ;; Look at all comment-start strings in the current line after point.
1026 ;; Return t if one of them starts a real comment.
1027 ;; This is not used yet, because indent-for-comment
1028 ;; isn't smart enough to handle the cases this can find.
1029 (defun indent-c-find-real-comment ()
1031 (while (and (not win
)
1032 (re-search-forward comment-start-skip
1033 (save-excursion (end-of-line) (point))
1035 ;; Make sure the comment start is not quoted.
1038 (save-excursion (beginning-of-line) (point))
1039 (point) nil nil state
)))
1040 (setq win
(and (null (nth 3 state-1
)) (null (nth 5 state-1
))))))
1043 ;; Indent every line whose first char is between START and END inclusive.
1044 (defun c-indent-region (start end
)
1047 (let ((endmark (copy-marker end
)))
1048 (and (bolp) (not (eolp))
1050 (indent-c-exp endmark
)
1051 (set-marker endmark nil
))))
1053 (defun set-c-style (style)
1054 "Set C-mode variables to use one of several different indentation styles.
1055 Takes one argument, a string representing the desired style.
1056 Available styles are GNU, K&R, BSD and Whitesmith."
1057 (interactive (list (completing-read "Use which C indentation style? "
1058 c-style-alist nil t
)))
1059 (let ((vars (cdr (assoc style c-style-alist
))))
1061 (if (interactive-p) (message "Using %s C indentation style" style
))
1062 (error "Bogus style type, \"%s\"" style
))
1064 (set (car (car vars
)) (cdr (car vars
)))
1065 (setq vars
(cdr vars
)))))
1067 ;;; c-mode.el ends here