1 ;;; c-mode.el --- C code editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 92, 94, 95 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; A smart editing mode for C code. It knows a lot about C syntax and tries
28 ;; to position the cursor according to C layout conventions. You can
29 ;; change the details of the layout style with option variables. Load it
30 ;; and do M-x describe-mode for details.
34 "Old C code editing commands for Emacs."
35 ;; This group should have no parent.
36 ;; We don't want it to be loaded except on explicit request.
40 (defvar c-mode-abbrev-table nil
41 "Abbrev table in use in C mode.")
42 (define-abbrev-table 'c-mode-abbrev-table
())
44 (defvar c-mode-map
(make-sparse-keymap)
45 "Keymap used in C mode.")
47 (define-key c-mode-map
"{" 'electric-c-brace
)
48 (define-key c-mode-map
"}" 'electric-c-brace
)
49 (define-key c-mode-map
";" 'electric-c-semi
)
50 (define-key c-mode-map
"#" 'electric-c-sharp-sign
)
51 (define-key c-mode-map
":" 'electric-c-terminator
)
52 (define-key c-mode-map
"\e\C-h" 'mark-c-function
)
53 (define-key c-mode-map
"\e\C-q" 'indent-c-exp
)
54 (define-key c-mode-map
"\ea" 'c-beginning-of-statement
)
55 (define-key c-mode-map
"\ee" 'c-end-of-statement
)
56 (define-key c-mode-map
"\C-c\C-n" 'c-forward-conditional
)
57 (define-key c-mode-map
"\C-c\C-p" 'c-backward-conditional
)
58 (define-key c-mode-map
"\C-c\C-u" 'c-up-conditional
)
59 (define-key c-mode-map
"\177" 'backward-delete-char-untabify
)
60 (define-key c-mode-map
"\t" 'c-indent-command
)
62 (define-key c-mode-map
[menu-bar
] (make-sparse-keymap))
64 ;; "C-mode" is not strictly the right punctuation--it should be "C
65 ;; mode"--but that would look like two menu items. "C-mode" is the
66 ;; best alternative I can think of.
67 (define-key c-mode-map
[menu-bar c
]
68 (cons "C-mode" (make-sparse-keymap "C-mode")))
70 (define-key c-mode-map
[menu-bar c comment-region
]
71 '("Comment Out Region" . comment-region
))
72 (define-key c-mode-map
[menu-bar c c-macro-expand
]
73 '("Macro Expand Region" . c-macro-expand
))
74 (define-key c-mode-map
[menu-bar c c-backslash-region
]
75 '("Backslashify" . c-backslash-region
))
76 (define-key c-mode-map
[menu-bar c indent-exp
]
77 '("Indent Expression" . indent-c-exp
))
78 (define-key c-mode-map
[menu-bar c indent-line
]
79 '("Indent Line" . c-indent-command
))
80 (define-key c-mode-map
[menu-bar c fill
]
81 '("Fill Comment Paragraph" . c-fill-paragraph
))
82 (define-key c-mode-map
[menu-bar c cpp-highlight-buffer
]
83 '("Highlight Conditionals" . cpp-highlight-buffer
))
84 (define-key c-mode-map
[menu-bar c up
]
85 '("Up Conditional" . c-up-conditional
))
86 (define-key c-mode-map
[menu-bar c backward
]
87 '("Backward Conditional" . c-backward-conditional
))
88 (define-key c-mode-map
[menu-bar c forward
]
89 '("Forward Conditional" . c-forward-conditional
))
90 (define-key c-mode-map
[menu-bar c backward-stmt
]
91 '("Backward Statement" . c-beginning-of-statement
))
92 (define-key c-mode-map
[menu-bar c forward-stmt
]
93 '("Forward Statement" . c-end-of-statement
))
95 (put 'comment-region
'menu-enable
'mark-active
)
96 (put 'c-macro-expand
'menu-enable
'mark-active
)
97 (put 'c-backslash-region
'menu-enable
'mark-active
)
99 (autoload 'c-macro-expand
"cmacexp"
100 "Display the result of expanding all C macros occurring in the region.
101 The expansion is entirely correct because it uses the C preprocessor."
104 (defvar c-mode-syntax-table nil
105 "Syntax table in use in C-mode buffers.")
107 (if c-mode-syntax-table
109 (setq c-mode-syntax-table
(make-syntax-table))
110 (modify-syntax-entry ?
\\ "\\" c-mode-syntax-table
)
111 (modify-syntax-entry ?
/ ". 14" c-mode-syntax-table
)
112 (modify-syntax-entry ?
* ". 23" c-mode-syntax-table
)
113 (modify-syntax-entry ?
+ "." c-mode-syntax-table
)
114 (modify-syntax-entry ?-
"." c-mode-syntax-table
)
115 (modify-syntax-entry ?
= "." c-mode-syntax-table
)
116 (modify-syntax-entry ?%
"." c-mode-syntax-table
)
117 (modify-syntax-entry ?
< "." c-mode-syntax-table
)
118 (modify-syntax-entry ?
> "." c-mode-syntax-table
)
119 (modify-syntax-entry ?
& "." c-mode-syntax-table
)
120 (modify-syntax-entry ?|
"." c-mode-syntax-table
)
121 (modify-syntax-entry ?
\' "\"" c-mode-syntax-table
))
123 (defcustom c-indent-level
2
124 "*Indentation of C statements with respect to containing block."
127 (defcustom c-brace-imaginary-offset
0
128 "*Imagined indentation of a C open brace that actually follows a statement."
131 (defcustom c-brace-offset
0
132 "*Extra indentation for braces, compared with other text in same context."
135 (defcustom c-argdecl-indent
5
136 "*Indentation level of declarations of C function arguments."
139 (defcustom c-label-offset -
2
140 "*Offset of C label lines and case statements relative to usual indentation."
143 (defcustom c-continued-statement-offset
2
144 "*Extra indent for lines not starting new statements."
147 (defcustom c-continued-brace-offset
0
148 "*Extra indent for substatements that start with open-braces.
149 This is in addition to `c-continued-statement-offset'."
152 (defconst c-style-alist
155 (c-argdecl-indent .
5)
157 (c-continued-brace-offset .
0)
158 (c-label-offset . -
2)
159 (c-continued-statement-offset .
2))
162 (c-argdecl-indent .
0)
164 (c-continued-brace-offset . -
5)
165 (c-label-offset . -
5)
166 (c-continued-statement-offset .
5))
169 (c-argdecl-indent .
4)
171 (c-continued-brace-offset . -
4)
172 (c-label-offset . -
4)
173 (c-continued-statement-offset .
4))
176 (c-argdecl-indent .
0)
178 (c-continued-brace-offset . -
4)
179 (c-label-offset . -
4)
180 (c-continued-statement-offset .
4)
181 (c-auto-newline . t
))
184 (c-argdecl-indent .
4)
186 (c-continued-brace-offset .
0)
187 (c-label-offset . -
4)
188 (c-continued-statement-offset .
4))))
190 (defcustom c-auto-newline nil
191 "*Non-nil means automatically newline before and after braces,
192 and after colons and semicolons, inserted in C code.
193 If you do not want a leading newline before braces then use:
194 (define-key c-mode-map \"{\" 'electric-c-semi)"
198 (defcustom c-tab-always-indent t
199 "*Non-nil means TAB in C mode should always reindent the current line,
200 regardless of where in the line point is when the TAB command is used."
204 ;;; Regular expression used internally to recognize labels in switch
206 (defconst c-switch-label-regexp
"case[ \t'/(]\\|default[ \t]*:")
208 ;; This is actually the expression for C++ mode, but it's used for C too.
209 (defvar c-imenu-generic-expression
212 "^" ; beginning of line is required
213 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
214 "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no
215 "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right?
217 "\\(" ; last type spec including */&
219 "\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace
220 "\\)?" ; if there is a last type spec
221 "\\(" ; name; take that into the imenu entry
222 "[a-zA-Z0-9_:~]+" ; member function, ctor or dtor...
223 ; (may not contain * because then
224 ; "a::operator char*" would become "char*"!)
226 "\\([a-zA-Z0-9_:~]*::\\)?operator"
227 "[^a-zA-Z1-9_][^(]*" ; ...or operator
229 "[ \t]*([^)]*)[ \t\n]*[^ ;]" ; require something other than a ; after
230 ; the (...) to avoid prototypes. Can't
231 ; catch cases with () inside the parentheses
232 ; surrounding the parameters
233 ; (like "int foo(int a=bar()) {...}"
238 "^" ; beginning of line is required
239 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
241 "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
244 ;; Example of generic expression for finding prototypes, structs, unions, enums.
245 ;; Uncomment if you want to find these too. It will be a bit slower gathering
250 ;;; "^" ; beginning of line is required
251 ;;; "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
252 ;;; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no
253 ;;; "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right?
255 ;;; "\\(" ; last type spec including */&
257 ;;; "\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace
258 ;;; "\\)?" ; if there is a last type spec
259 ;;; "\\(" ; name; take that into the imenu entry
260 ;;; "[a-zA-Z0-9_:~]+" ; member function, ctor or dtor...
261 ;;; ; (may not contain * because then
262 ;;; ; "a::operator char*" would become "char*"!)
264 ;;; "\\([a-zA-Z0-9_:~]*::\\)?operator"
265 ;;; "[^a-zA-Z1-9_][^(]*" ; ...or operator
267 ;;; "[ \t]*([^)]*)[ \t\n]*;" ; require ';' after
268 ;;; ; the (...) Can't
269 ;;; ; catch cases with () inside the parentheses
270 ;;; ; surrounding the parameters
271 ;;; ; (like "int foo(int a=bar());"
275 ;;; "^" ; beginning of line is required
276 ;;; "\\(static[ \t]+\\)?" ; there may be static or const.
277 ;;; "\\(const[ \t]+\\)?"
279 ;;; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
284 ;;; "^" ; beginning of line is required
285 ;;; "\\(static[ \t]+\\)?" ; there may be static or const.
286 ;;; "\\(const[ \t]+\\)?"
288 ;;; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
293 ;;; "^" ; beginning of line is required
294 ;;; "\\(static[ \t]+\\)?" ; there may be static or const.
295 ;;; "\\(const[ \t]+\\)?"
297 ;;; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
301 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
304 "Major mode for editing C code.
305 Expression and list commands understand all C brackets.
306 Tab indents for C code.
307 Comments are delimited with /* ... */.
308 Paragraphs are separated by blank lines only.
309 Delete converts tabs to spaces as it moves back.
311 Variables controlling indentation style:
313 Non-nil means TAB in C mode should always reindent the current line,
314 regardless of where in the line point is when the TAB command is used.
316 Non-nil means automatically newline before and after braces,
317 and after colons and semicolons, inserted in C code.
319 Indentation of C statements within surrounding block.
320 The surrounding block's indentation is the indentation
321 of the line on which the open-brace appears.
322 c-continued-statement-offset
323 Extra indentation given to a substatement, such as the
324 then-clause of an if or body of a while.
325 c-continued-brace-offset
326 Extra indentation given to a brace that starts a substatement.
327 This is in addition to c-continued-statement-offset.
329 Extra indentation for line if it starts with an open brace.
330 c-brace-imaginary-offset
331 An open brace following other text is treated as if it were
332 this far to the right of the start of its line.
334 Indentation level of declarations of C function arguments.
336 Extra indentation for line that is a label, or case or default.
338 Settings for K&R and BSD indentation styles are
340 c-continued-statement-offset 5 8
345 Turning on C mode calls the value of the variable c-mode-hook with no args,
346 if that value is non-nil."
348 (kill-all-local-variables)
349 (use-local-map c-mode-map
)
350 (setq major-mode
'c-mode
)
352 (setq local-abbrev-table c-mode-abbrev-table
)
353 (set-syntax-table c-mode-syntax-table
)
354 (make-local-variable 'paragraph-start
)
355 (setq paragraph-start
(concat "$\\|" page-delimiter
))
356 (make-local-variable 'paragraph-separate
)
357 (setq paragraph-separate paragraph-start
)
358 (make-local-variable 'paragraph-ignore-fill-prefix
)
359 (setq paragraph-ignore-fill-prefix t
)
360 (make-local-variable 'fill-paragraph-function
)
361 (setq fill-paragraph-function
'c-fill-paragraph
)
362 (make-local-variable 'indent-line-function
)
363 (setq indent-line-function
'c-indent-line
)
364 (make-local-variable 'indent-region-function
)
365 (setq indent-region-function
'c-indent-region
)
366 (make-local-variable 'require-final-newline
)
367 (setq require-final-newline t
)
368 (make-local-variable 'outline-regexp
)
369 (setq outline-regexp
"[^#\n\^M]")
370 (make-local-variable 'outline-level
)
371 (setq outline-level
'c-outline-level
)
372 (make-local-variable 'comment-start
)
373 (setq comment-start
"/* ")
374 (make-local-variable 'comment-end
)
375 (setq comment-end
" */")
376 (make-local-variable 'comment-start-skip
)
377 (setq comment-start-skip
"/\\*+ *")
378 (make-local-variable 'comment-indent-function
)
379 (setq comment-indent-function
'c-comment-indent
)
380 (make-local-variable 'comment-multi-line
)
381 (setq comment-multi-line t
)
382 (make-local-variable 'parse-sexp-ignore-comments
)
383 (setq parse-sexp-ignore-comments t
)
384 (make-local-variable 'imenu-generic-expression
)
385 (setq imenu-generic-expression c-imenu-generic-expression
)
386 (setq imenu-case-fold-search nil
)
387 (run-hooks 'c-mode-hook
))
389 (defun c-outline-level ()
390 ;; This so that `current-column' DTRT in otherwise-hidden text.
391 (let (buffer-invisibility-spec)
393 (skip-chars-forward "\t ")
396 ;; This is used by indent-for-comment
397 ;; to decide how much to indent a comment in C code
398 ;; based on its context.
399 (defun c-comment-indent ()
400 (if (looking-at "^/\\*")
401 0 ;Existing comment at bol stays there.
402 (let ((opoint (point)))
405 (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)")
406 ;; A comment following a solitary close-brace
407 ;; should have only one space.
409 (1+ (current-column)))
410 ((or (looking-at "^#[ \t]*endif[ \t]*")
411 (looking-at "^#[ \t]*else[ \t]*"))
412 7) ;2 spaces after #endif
415 (skip-chars-backward " \t")
416 (and (= comment-column
0) (bolp)))
417 ;; If comment-column is 0, and nothing but space
418 ;; before the comment, align it at 0 rather than 1.
421 (max (1+ (current-column)) ;Else indent at comment column
422 comment-column
))))))) ; except leave at least one space.
424 (defun c-fill-paragraph (&optional arg
)
425 "Like \\[fill-paragraph] but handle C comments.
426 If any of the current line is a comment or within a comment,
427 fill the comment or the paragraph of it that point is in,
428 preserving the comment indentation or line-starting decorations."
430 (let* (comment-start-place
432 ;; Check for obvious entry to comment.
435 (skip-chars-forward " \t\n")
436 (and (looking-at comment-start-skip
)
437 (setq comment-start-place
(point))))))
438 (if (and (eq major-mode
'c
++-mode
)
441 (looking-at ".*//")))
444 ;; Lines containing just a comment start or just an end
445 ;; should not be filled into paragraphs they are next to.
448 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
452 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")))
455 ;; Move up to first line of this comment.
456 (while (and (not (bobp)) (looking-at "[ \t]*//"))
458 (if (not (looking-at ".*//"))
460 ;; Find the comment start in this line.
461 (re-search-forward "[ \t]*//[ \t]*")
462 ;; Set the fill-prefix to be what all lines except the first
463 ;; should start with.
464 (let ((endcol (current-column)))
465 (skip-chars-backward " \t")
467 (concat (make-string (- (current-column) 2) ?\
)
469 (make-string (- endcol
(current-column)) ?\
))))
471 ;; Narrow down to just the lines of this comment.
472 (narrow-to-region (point)
475 (while (looking-at "[ \t]*//")
480 (delete-region (point-min)
481 (+ (point-min) (length fill-prefix
))))))
483 ;; t if we enter a comment between start of function and this line.
484 (eq (calculate-c-indent) t
)
485 ;; t if this line contains a comment starter.
490 (re-search-forward comment-start-skip
491 (save-excursion (end-of-line)
494 (setq comment-start-place
(point))))))
495 ;; Inside a comment: fill one comment paragraph.
497 ;; The prefix for each line of this paragraph
498 ;; is the appropriate part of the start of this line,
499 ;; up to the column at which text should be indented.
502 (if (looking-at "[ \t]*/\\*.*\\*/")
503 (progn (re-search-forward comment-start-skip
)
504 (make-string (current-column) ?\
))
505 (if first-line
(forward-line 1))
507 (let ((line-width (progn (end-of-line) (current-column))))
513 ;; How shall we decide where the end of the
515 ;; calculate-c-indent-within-comment bases its value
516 ;; on the indentation of previous lines; if they're
517 ;; indented specially, it could return a column
518 ;; that's well into the current line's text. So
519 ;; we'll take at most that many space, tab, or *
520 ;; characters, and use that as our fill prefix.
521 (let ((max-prefix-end
524 (calculate-c-indent-within-comment t
)
528 (skip-chars-forward " \t*" max-prefix-end
)
529 ;; Don't include part of comment terminator
530 ;; in the fill-prefix.
531 (and (eq (following-char) ?
/)
532 (eq (preceding-char) ?
*)
536 ;; If the comment is only one line followed by a blank
537 ;; line, calling move-to-column above may have added
538 ;; some spaces and tabs to the end of the line; the
539 ;; fill-paragraph function will then delete it and the
540 ;; newline following it, so we'll lose a blank line
541 ;; when we shouldn't. So delete anything
542 ;; move-to-column added to the end of the line. We
543 ;; record the line width instead of the position of the
544 ;; old line end because move-to-column might break a
545 ;; tab into spaces, and the new characters introduced
546 ;; there shouldn't be deleted.
548 ;; If you can see a better way to do this, please make
549 ;; the change. This seems very messy to me.
550 (delete-region (progn (move-to-column line-width
)
552 (progn (end-of-line) (point))))))))
555 ;; Lines containing just a comment start or just an end
556 ;; should not be filled into paragraphs they are next to.
559 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
563 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
566 ;; Don't fill the comment together with the code following it.
567 ;; So temporarily exclude everything before the comment start,
568 ;; and everything after the line where the comment ends.
569 ;; If comment-start-place is non-nil, the comment starter is there.
570 ;; Otherwise, point is inside the comment.
571 (narrow-to-region (save-excursion
572 (if comment-start-place
573 (goto-char comment-start-place
)
574 (search-backward "/*"))
575 ;; Protect text before the comment start
576 ;; by excluding it. Add spaces to bring back
577 ;; proper indentation of that point.
578 (let ((column (current-column)))
580 (setq chars-to-delete column
)
581 (insert-char ?\ column
))))
583 (if comment-start-place
584 (goto-char (+ comment-start-place
2)))
585 (search-forward "*/" nil
'move
)
589 (goto-char (point-max))
591 ;; And comment terminator was on a separate line before,
593 ;; This also avoids another problem:
594 ;; if the fill-prefix ends in a *, it could eat up
595 ;; the * of the comment terminator.
596 (if (looking-at "[ \t]*\\*/")
597 (narrow-to-region (point-min) (point))))
600 ;; Delete the chars we inserted to avoid clobbering
601 ;; the stuff before the comment start.
602 (goto-char (point-min))
603 (if (> chars-to-delete
0)
604 (delete-region (point) (+ (point) chars-to-delete
)))
605 ;; Find the comment ender (should be on last line of buffer,
606 ;; given the narrowing) and don't leave it on its own line.
607 ;; Do this with a fill command, so as to preserve sentence
609 (goto-char (point-max))
611 (search-forward "*/" nil
'move
)
613 (if (looking-at "[ \t]*\\*/")
614 (let ((fill-column (+ fill-column
9999)))
616 (fill-region-as-paragraph (point) (point-max)))))))
617 ;; Outside of comments: do ordinary filling.
618 (fill-paragraph arg
)))
621 (defun electric-c-brace (arg)
622 "Insert character and correct line's indentation."
628 (skip-chars-backward " \t")
630 (if c-auto-newline
(progn (c-indent-line) (newline) t
) nil
)))
632 (insert last-command-char
)
637 ;; (newline) may have done auto-fill
638 (setq insertpos
(- (point) 2))
641 (if insertpos
(goto-char (1+ insertpos
)))
645 (goto-char insertpos
)
646 (self-insert-command (prefix-numeric-value arg
)))
647 (self-insert-command (prefix-numeric-value arg
)))))
649 (defun electric-c-sharp-sign (arg)
650 "Insert character and correct line's indentation."
653 (skip-chars-backward " \t")
655 (let ((c-auto-newline nil
))
656 (electric-c-terminator arg
))
657 (self-insert-command (prefix-numeric-value arg
))))
659 (defun electric-c-semi (arg)
660 "Insert character and correct line's indentation."
663 (electric-c-terminator arg
)
664 (self-insert-command (prefix-numeric-value arg
))))
666 (defun electric-c-terminator (arg)
667 "Insert character and correct line's indentation."
669 (let (insertpos (end (point)))
670 (if (and (not arg
) (eolp)
673 (skip-chars-forward " \t")
674 (or (= (following-char) ?
#)
675 ;; Colon is special only after a label, or case ....
676 ;; So quickly rule out most other uses of colon
677 ;; and do no indentation for them.
678 (and (eq last-command-char ?
:)
679 (not (looking-at c-switch-label-regexp
))
681 (skip-chars-forward "a-zA-Z0-9_$")
682 (skip-chars-forward " \t")
686 (let ((pps (parse-partial-sexp (point) end
)))
687 (or (nth 3 pps
) (nth 4 pps
) (nth 5 pps
))))))))
689 (insert last-command-char
)
692 (not (c-inside-parens-p))
695 ;; (newline) may have done auto-fill
696 (setq insertpos
(- (point) 2))
699 (if insertpos
(goto-char (1+ insertpos
)))
703 (goto-char insertpos
)
704 (self-insert-command (prefix-numeric-value arg
)))
705 (self-insert-command (prefix-numeric-value arg
)))))
707 (defun c-inside-parens-p ()
711 (narrow-to-region (point)
712 (progn (beginning-of-defun) (point)))
713 (goto-char (point-max))
714 (= (char-after (or (scan-lists (point) -
1 1) (point-min))) ?\
()))
717 (defun c-indent-command (&optional whole-exp
)
718 "Indent current line as C code, or in some cases insert a tab character.
719 If `c-tab-always-indent' is non-nil (the default), always indent current line.
720 Otherwise, indent the current line only if point is at the left margin or
721 in the line's indentation; otherwise insert a tab.
723 A numeric argument, regardless of its value, means indent rigidly all the
724 lines of the expression starting after point so that this line becomes
725 properly indented. The relative indentation among the lines of the
726 expression are preserved."
729 ;; If arg, always indent this line as C
730 ;; and shift remaining lines of expression the same amount.
731 (let ((shift-amt (c-indent-line))
734 (if c-tab-always-indent
736 ;; Find beginning of following line.
738 (forward-line 1) (setq beg
(point)))
739 ;; Find first beginning-of-sexp for sexp extending past this line.
740 (while (< (point) beg
)
743 (skip-chars-forward " \t\n")))
745 (indent-code-rigidly beg end shift-amt
"#")))
746 (if (and (not c-tab-always-indent
)
748 (skip-chars-backward " \t")
753 (defun c-indent-line ()
754 "Indent current line as C code.
755 Return the amount the indentation changed by."
756 (let ((indent (calculate-c-indent nil
))
758 (case-fold-search nil
)
759 (pos (- (point-max) (point))))
762 (cond ((eq indent nil
)
763 (setq indent
(current-indentation)))
765 (setq indent
(calculate-c-indent-within-comment)))
766 ((looking-at "[ \t]*#")
769 (skip-chars-forward " \t")
770 (if (listp indent
) (setq indent
(car indent
)))
771 (cond ((or (looking-at c-switch-label-regexp
)
772 (and (looking-at "[A-Za-z]")
776 (setq indent
(max 1 (+ indent c-label-offset
))))
777 ((and (looking-at "else\\b")
778 (not (looking-at "else\\s_")))
779 (setq indent
(save-excursion
780 (c-backward-to-start-of-if)
781 (current-indentation))))
782 ((and (looking-at "}[ \t]*else\\b")
783 (not (looking-at "}[ \t]*else\\s_")))
784 (setq indent
(save-excursion
787 (c-backward-to-start-of-if)
788 (current-indentation))))
789 ((and (looking-at "while\\b")
790 (not (looking-at "while\\s_"))
792 (c-backward-to-start-of-do)))
793 ;; This is a `while' that ends a do-while.
794 (setq indent
(save-excursion
795 (c-backward-to-start-of-do)
796 (current-indentation))))
797 ((= (following-char) ?\
})
798 (setq indent
(- indent c-indent-level
)))
799 ((= (following-char) ?\
{)
800 (setq indent
(+ indent c-brace-offset
))))))
801 (skip-chars-forward " \t")
802 (setq shift-amt
(- indent
(current-column)))
803 (if (zerop shift-amt
)
804 (if (> (- (point-max) pos
) (point))
805 (goto-char (- (point-max) pos
)))
806 (delete-region beg
(point))
808 ;; If initial point was within line's indentation,
809 ;; position after the indentation. Else stay at same point in text.
810 (if (> (- (point-max) pos
) (point))
811 (goto-char (- (point-max) pos
))))
814 (defun calculate-c-indent (&optional parse-start
)
815 "Return appropriate indentation for current line as C code.
816 In usual case returns an integer: the column to indent to.
817 Returns nil if line starts inside a string, t if in a comment."
820 (let ((indent-point (point))
821 (case-fold-search nil
)
825 (goto-char parse-start
)
826 (beginning-of-defun))
827 (while (< (point) indent-point
)
828 (setq parse-start
(point))
829 (setq state
(parse-partial-sexp (point) indent-point
0))
830 (setq containing-sexp
(car (cdr state
))))
831 (cond ((or (nth 3 state
) (nth 4 state
))
832 ;; return nil or t if should not change this line
834 ((null containing-sexp
)
835 ;; Line is at top level. May be data or function definition,
836 ;; or may be function argument declaration.
837 ;; Indent like the previous top level line
838 ;; unless that ends in a closeparen without semicolon,
839 ;; in which case this line is the first argument decl.
840 (goto-char indent-point
)
841 (skip-chars-forward " \t")
842 (if (= (following-char) ?\
{)
843 0 ; Unless it starts a function body
844 (c-backward-to-noncomment (or parse-start
(point-min)))
845 ;; Look at previous line that's at column 0
846 ;; to determine whether we are in top-level decls
847 ;; or function's arg decls. Set basic-indent accordingly.
850 (re-search-backward "^[^ \^L\t\n#]" nil
'move
)
852 ;; Recognize the DEFUN macro in Emacs.
854 ;; Move down to the (putative) argnames line.
855 (while (and (not (eobp))
856 (not (looking-at " *[({}#/]")))
858 ;; Go back to the DEFUN, if it is one.
863 (looking-at "DEFUN\\b"))
865 (if (and (looking-at "\\sw\\|\\s_")
866 ;; This is careful to stop at the first
868 ;; int foo Proto ((int, int));
869 (looking-at "[^\"\n=(]*(")
871 (goto-char (1- (match-end 0)))
872 ;; Skip any number of paren-groups.
873 ;; Consider typedef int (*fcn) (int);
874 (while (= (following-char) ?\
()
879 (skip-chars-forward " \t\f"))
880 ;; Have we reached something
881 ;; that shows this isn't a function
883 (and (< (point) indent-point
)
884 (not (memq (following-char)
886 ;; Make sure the "function decl" we found
887 ;; is not inside a comment.
889 ;; Move back to the `(' starting arglist
892 (while (and (not comment
)
893 (search-forward "/*" lim t
))
895 (not (search-forward "*/" lim t
))))
897 c-argdecl-indent
0))))))
900 ;; ;; Now add a little if this is a continuation line.
901 ;; (+ basic-indent (if (or (bobp)
902 ;; (memq (preceding-char) '(?\) ?\; ?\}))
903 ;; ;; Line with zero indentation
904 ;; ;; is probably the return-type
905 ;; ;; of a function definition,
906 ;; ;; so following line is function name.
907 ;; (= (current-indentation) 0))
908 ;; 0 c-continued-statement-offset))
910 ((/= (char-after containing-sexp
) ?\
{)
911 ;; line is expression, not statement:
912 ;; indent to just after the surrounding open.
913 (goto-char (1+ containing-sexp
))
916 ;; Statement level. Is it a continuation or a new statement?
917 ;; Find previous non-comment character.
918 (goto-char indent-point
)
919 (c-backward-to-noncomment containing-sexp
)
920 ;; Back up over label lines, since they don't
921 ;; affect whether our line is a continuation.
922 (while (or (eq (preceding-char) ?\
,)
923 (and (eq (preceding-char) ?
:)
924 (or (eq (char-after (- (point) 2)) ?
\')
925 (memq (char-syntax (char-after (- (point) 2)))
927 (if (eq (preceding-char) ?\
,)
928 (progn (forward-char -
1)
929 (c-backward-to-start-of-continued-exp containing-sexp
)))
931 (c-backward-to-noncomment containing-sexp
))
932 ;; Check for a preprocessor statement or its continuation lines.
933 ;; Move back to end of previous non-preprocessor line,
934 ;; or possibly beginning of buffer.
935 (let ((found (point)) stop
)
941 ((save-excursion (forward-char -
1)
942 (= (preceding-char) ?
\\))
944 ;; This line is not preceded by a backslash.
945 ;; So either it starts a preprocessor command
946 ;; or any following continuation lines
947 ;; should not be skipped.
948 ((= (following-char) ?
#)
950 (setq found
(point)))
953 ;; Now we get the answer.
954 (if (and (not (memq (preceding-char) '(0 ?\
, ?\
; ?\} ?\{)))
955 ;; But don't treat a line with a close-brace
956 ;; as a continuation. It is probably the
957 ;; end of an enum type declaration.
959 (goto-char indent-point
)
960 (skip-chars-forward " \t")
961 (not (= (following-char) ?\
}))))
962 ;; This line is continuation of preceding line's statement;
963 ;; indent c-continued-statement-offset more than the
964 ;; previous line of the statement.
966 (c-backward-to-start-of-continued-exp containing-sexp
)
967 (+ c-continued-statement-offset
(current-column)
968 (if (save-excursion (goto-char indent-point
)
969 (skip-chars-forward " \t")
970 (eq (following-char) ?\
{))
971 c-continued-brace-offset
0)))
972 ;; This line starts a new statement.
973 ;; Position following last unclosed open.
974 (goto-char containing-sexp
)
975 ;; Is line first statement after an open-brace?
977 ;; If no, find that first statement and indent like it.
980 (let ((colon-line-end 0))
981 (while (progn (skip-chars-forward " \t\n")
982 (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:"))
983 ;; Skip over comments and labels following openbrace.
984 (cond ((= (following-char) ?\
#)
986 ((= (following-char) ?\
/)
988 (search-forward "*/" nil
'move
))
991 (save-excursion (end-of-line)
992 (setq colon-line-end
(point)))
993 (search-forward ":"))))
994 ;; The first following code counts
995 ;; if it is before the line we want to indent.
996 (and (< (point) indent-point
)
998 (if (> colon-line-end
(point))
999 (- (current-indentation) c-label-offset
)
1001 ;; If prev stmt starts with open-brace, that
1002 ;; open brace was offset by c-brace-offset.
1003 ;; Compensate to get the column where
1004 ;; an ordinary statement would start.
1005 (if (= (following-char) ?\
{) c-brace-offset
0)))))
1006 ;; If no previous statement,
1007 ;; indent it relative to line brace is on.
1008 (calculate-c-indent-after-brace))))))))
1010 (defun calculate-c-indent-after-brace ()
1011 "Return the proper C indent for the first line after an open-brace.
1012 This function is called with point before the brace."
1013 ;; For open brace in column zero, don't let statement
1014 ;; start there too. If c-indent-level is zero,
1015 ;; use c-brace-offset + c-continued-statement-offset instead.
1016 ;; For open-braces not the first thing in a line,
1017 ;; add in c-brace-imaginary-offset.
1018 (+ (if (and (bolp) (zerop c-indent-level
))
1019 (+ c-brace-offset c-continued-statement-offset
)
1021 ;; Move back over whitespace before the openbrace.
1022 ;; If openbrace is not first nonwhite thing on the line,
1023 ;; add the c-brace-imaginary-offset.
1024 (progn (skip-chars-backward " \t")
1025 (if (bolp) 0 c-brace-imaginary-offset
))
1026 ;; If the openbrace is preceded by a parenthesized exp,
1027 ;; move to the beginning of that;
1028 ;; possibly a different line
1030 (if (eq (preceding-char) ?\
))
1032 ;; Get initial indentation of the line we are on.
1033 (current-indentation))))
1035 (defun calculate-c-indent-within-comment (&optional after-star
)
1036 "Return the indentation amount for line inside a block comment.
1037 Optional arg AFTER-STAR means, if lines in the comment have a leading star,
1038 return the indentation of the text that would follow this star."
1039 (let (end star-start
)
1042 (skip-chars-forward " \t")
1043 (setq star-start
(= (following-char) ?\
*))
1044 (skip-chars-backward " \t\n")
1047 (skip-chars-forward " \t")
1049 (and (looking-at "\\*")
1050 (re-search-forward "\\*[ \t]*")))
1051 (and (re-search-forward "/\\*[ \t]*" end t
)
1054 (goto-char (1+ (match-beginning 0))))
1055 (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\
*))
1056 (1+ (current-column))
1057 (current-column)))))
1060 (defun c-backward-to-noncomment (lim)
1063 (skip-chars-backward " \t\n\f" lim
)
1064 (setq opoint
(point))
1065 (if (and (>= (point) (+ 2 lim
))
1068 (looking-at "\\*/")))
1069 (search-backward "/*" lim
'move
)
1070 (setq stop
(or (<= (point) lim
)
1073 (skip-chars-forward " \t")
1074 (not (looking-at "#")))))
1075 (or stop
(beginning-of-line))))))
1077 (defun c-backward-to-start-of-continued-exp (lim)
1078 (if (memq (preceding-char) '(?\
) ?
\"))
1081 (if (<= (point) lim
)
1082 (goto-char (1+ lim
)))
1083 (skip-chars-forward " \t"))
1085 (defun c-backward-to-start-of-if (&optional limit
)
1086 "Move to the start of the last \"unbalanced\" `if'."
1087 (or limit
(setq limit
(save-excursion (beginning-of-defun) (point))))
1089 (case-fold-search nil
))
1090 (while (and (not (bobp)) (not (zerop if-level
)))
1092 (cond ((and (looking-at "else\\b")
1093 (not (looking-at "else\\s_")))
1094 (setq if-level
(1+ if-level
)))
1095 ((and (looking-at "if\\b")
1096 (not (looking-at "if\\s_")))
1097 (setq if-level
(1- if-level
)))
1100 (goto-char limit
))))))
1102 (defun c-backward-to-start-of-do (&optional limit
)
1103 "If point follows a `do' statement, move to beginning of it and return t.
1104 Otherwise return nil and don't move point."
1105 (or limit
(setq limit
(save-excursion (beginning-of-defun) (point))))
1110 (let ((next-start (point)))
1112 ;; Move back one token or one brace or paren group.
1114 ;; If we find an open-brace, we lose.
1115 (error (setq done
'fail
)))
1118 ;; If we reached a `do', we win.
1119 (if (looking-at "do\\b")
1120 (setq done
'succeed
)
1121 ;; Otherwise, if we skipped a semicolon, we lose.
1122 ;; (Exception: we can skip one semicolon before getting
1123 ;; to a the last token of the statement, unless that token
1124 ;; is a close brace.)
1127 (or (and (not first
) (= (preceding-char) ?\
}))
1128 (search-forward ";" next-start t
1130 (/= (preceding-char) ?\
}))
1134 ;; If we go too far back in the buffer, we lose.
1135 (if (< (point) limit
)
1136 (setq done
'fail
)))))))
1137 (if (eq done
'succeed
)
1139 (goto-char startpos
)
1142 (defun c-beginning-of-statement (count)
1143 "Go to the beginning of the innermost C statement.
1144 With prefix arg, go back N - 1 statements. If already at the beginning of a
1145 statement then go to the beginning of the preceding one.
1146 If within a string or comment, or next to a comment (only whitespace between),
1147 move by sentences instead of statements."
1149 (let ((here (point)) state
)
1151 (beginning-of-defun)
1152 (setq state
(parse-partial-sexp (point) here nil nil
)))
1153 (if (or (nth 3 state
) (nth 4 state
)
1154 (looking-at (concat "[ \t]*" comment-start-skip
))
1155 (save-excursion (skip-chars-backward " \t")
1156 (goto-char (- (point) 2))
1157 (looking-at "\\*/")))
1158 (forward-sentence (- count
))
1160 (c-beginning-of-statement-1)
1161 (setq count
(1- count
)))
1163 (c-end-of-statement-1)
1164 (setq count
(1+ count
))))))
1166 (defun c-end-of-statement (count)
1167 "Go to the end of the innermost C statement.
1168 With prefix arg, go forward N - 1 statements.
1169 Move forward to end of the next statement if already at end.
1170 If within a string or comment, move by sentences instead of statements."
1172 (c-beginning-of-statement (- count
)))
1174 (defun c-beginning-of-statement-1 ()
1175 (let ((last-begin (point))
1179 (while (and (not (bobp))
1183 (not (re-search-forward "[;{}]" last-begin t
)))))
1184 (setq last-begin
(point) first nil
))
1185 (goto-char last-begin
))
1186 (error (if first
(backward-up-list 1) (goto-char last-begin
))))))
1188 (defun c-end-of-statement-1 ()
1191 (while (and (not (eobp))
1192 (let ((beg (point)))
1194 (let ((end (point)))
1197 (not (re-search-forward "[;{}]" end t
)))))))
1198 (re-search-backward "[;}]")
1201 (let ((beg (point)))
1202 (backward-up-list -
1)
1203 (let ((end (point)))
1205 (search-forward ";" end
'move
))))))
1207 (defun mark-c-function ()
1208 "Put mark at end of C function, point at beginning."
1212 (push-mark (point) nil t
)
1213 (beginning-of-defun)
1214 (backward-paragraph))
1216 ;; Idea of ENDPOS is, indent each line, stopping when
1217 ;; ENDPOS is encountered. But it's too much of a pain to make that work.
1218 (defun indent-c-exp (&optional endpos
)
1219 "Indent each line of the C grouping following point."
1221 (let* ((indent-stack (list nil
))
1222 (opoint (point)) ;; May be altered below.
1226 ;; Find previous fcn-start.
1227 (save-excursion (forward-char 1)
1228 (beginning-of-defun)
1229 (setq funbeg
(point)))
1230 (setq opoint funbeg
)
1231 ;; Try to find containing open,
1232 ;; but don't scan past that fcn-start.
1234 (narrow-to-region funbeg
(point))
1237 (backward-up-list 1)
1239 ;; We gave up: must be between fcns.
1240 ;; Set opoint to beg of prev fcn
1241 ;; since otherwise calculate-c-indent
1242 ;; will get wrong answers.
1243 (error (setq opoint funbeg
)
1246 (case-fold-search nil
)
1247 restart outer-loop-done inner-loop-done state ostate
1248 this-indent last-sexp
1249 at-else at-brace at-while
1250 last-depth this-point
1252 ;; If the braces don't match, get an error right away.
1255 ;; Realign the comment on the first line, even though we don't reindent it.
1257 (let ((beg (point)))
1258 (and (re-search-forward
1260 (save-excursion (end-of-line) (point)) t
)
1261 ;; Make sure this isn't a comment alone on a line
1262 ;; (which should be indented like code instead).
1264 (goto-char (match-beginning 0))
1265 (skip-chars-backward " \t")
1267 ;; Make sure the comment starter we found
1268 ;; is not actually in a string or quoted.
1270 (parse-partial-sexp beg
(point)
1272 (and (not (nth 3 new-state
)) (not (nth 5 new-state
))))
1273 (progn (indent-for-comment) (beginning-of-line)))))
1275 (setq outer-loop-done nil
)
1276 (while (and (not (eobp))
1277 (if endpos
(< (point) endpos
)
1278 (not outer-loop-done
)))
1279 (setq last-depth next-depth
)
1280 ;; Compute how depth changes over this line
1281 ;; plus enough other lines to get to one that
1282 ;; does not end inside a comment or string.
1283 ;; Meanwhile, do appropriate indentation on comment lines.
1284 (setq inner-loop-done nil
)
1285 (while (and (not inner-loop-done
)
1286 (not (and (eobp) (setq outer-loop-done t
))))
1288 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
1290 (setq next-depth
(car state
))
1291 (if (and (car (cdr (cdr state
)))
1292 (>= (car (cdr (cdr state
))) 0))
1293 (setq last-sexp
(car (cdr (cdr state
)))))
1294 ;; If this line started within a comment, indent it as such.
1295 (if (or (nth 4 ostate
) (nth 7 ostate
))
1297 ;; If it ends outside of comments or strings, exit the inner loop.
1298 ;; Otherwise move on to next line.
1299 (if (or (nth 3 state
) (nth 4 state
) (nth 7 state
))
1301 (setq inner-loop-done t
)))
1303 (while (< next-depth
0)
1304 (setq indent-stack
(append indent-stack
(list nil
)))
1305 (setq contain-stack
(append contain-stack
(list nil
)))
1306 (setq next-depth
(1+ next-depth
))
1307 (setq last-depth
(1+ last-depth
))
1308 (setcar (nthcdr 6 state
) (1+ (nth 6 state
)))))
1309 (setq outer-loop-done
(and (not endpos
) (<= next-depth
0)))
1312 ;; If this line had ..))) (((.. in it, pop out of the levels
1313 ;; that ended anywhere in this line, even if the final depth
1314 ;; doesn't indicate that they ended.
1315 (while (> last-depth
(nth 6 state
))
1316 (setq indent-stack
(cdr indent-stack
)
1317 contain-stack
(cdr contain-stack
)
1318 last-depth
(1- last-depth
)))
1319 (if (/= last-depth next-depth
)
1320 (setq last-sexp nil
))
1321 ;; Add levels for any parens that were started in this line.
1322 (while (< last-depth next-depth
)
1323 (setq indent-stack
(cons nil indent-stack
)
1324 contain-stack
(cons nil contain-stack
)
1325 last-depth
(1+ last-depth
)))
1326 (if (null (car contain-stack
))
1327 (setcar contain-stack
(or (car (cdr state
))
1328 (save-excursion (forward-sexp -
1)
1331 (skip-chars-forward " \t")
1332 ;; Don't really reindent if the line is just whitespace,
1333 ;; or if it is past the endpos.
1334 ;; (The exit test in the outer while
1335 ;; does not exit until we have passed the first line
1336 ;; past the region.)
1337 (if (or (eolp) (and endpos
(>= (point) endpos
)))
1339 ;; Is this line in a new nesting level?
1340 ;; In other words, is this the first line that
1341 ;; starts in the new level?
1342 (if (and (car indent-stack
)
1343 (>= (car indent-stack
) 0))
1346 ;; Compute the standard indent for this level.
1348 (if (= (char-after (car contain-stack
)) ?\
{)
1350 (goto-char (car contain-stack
))
1351 (setq val
(calculate-c-indent-after-brace)))
1352 (setq val
(calculate-c-indent
1353 (if (car indent-stack
)
1354 (- (car indent-stack
))
1356 ;; t means we are in a block comment and should
1357 ;; calculate accordingly.
1359 (setq val
(calculate-c-indent-within-comment)))
1360 (setcar indent-stack val
)))
1361 ;; Adjust indent of this individual line
1362 ;; based on its predecessor.
1363 ;; Handle continuation lines, if, else, while, and so on.
1364 (if (/= (char-after (car contain-stack
)) ?\
{)
1365 (setq this-indent
(car indent-stack
))
1366 ;; Line is at statement level.
1367 ;; Is it a new statement? Is it an else?
1368 ;; Find last non-comment character before this line
1370 (setq this-point
(point))
1371 (setq at-else
(and (looking-at "else\\b")
1372 (not (looking-at "else\\s_"))))
1373 (setq at-brace
(= (following-char) ?\
{))
1374 (setq at-while
(and (looking-at "while\\b")
1375 (not (looking-at "while\\s_"))))
1376 (if (= (following-char) ?\
})
1377 (setq this-indent
(car indent-stack
))
1378 (c-backward-to-noncomment opoint
)
1379 (if (not (memq (preceding-char) '(0 ?\
, ?\
; ?\} ?: ?\{)))
1380 ;; Preceding line did not end in comma or semi;
1381 ;; indent this line c-continued-statement-offset
1382 ;; more than previous.
1384 (c-backward-to-start-of-continued-exp (car contain-stack
))
1386 (+ c-continued-statement-offset
(current-column)
1387 (if at-brace c-continued-brace-offset
0))))
1388 ;; Preceding line ended in comma or semi;
1389 ;; use the standard indent for this level.
1390 (cond (at-else (progn (c-backward-to-start-of-if opoint
)
1392 (current-indentation))))
1393 ((and at-while
(c-backward-to-start-of-do opoint
))
1394 (setq this-indent
(current-indentation)))
1395 ((eq (preceding-char) ?\
,)
1396 (goto-char this-point
)
1397 (setq this-indent
(calculate-c-indent)))
1398 (t (setq this-indent
(car indent-stack
))))))))
1399 ;; Adjust line indentation according to its contents
1400 (if (or (looking-at c-switch-label-regexp
)
1401 (and (looking-at "[A-Za-z]")
1405 (setq this-indent
(max 1 (+ this-indent c-label-offset
))))
1406 (if (= (following-char) ?\
})
1407 (setq this-indent
(- this-indent c-indent-level
)))
1408 (if (= (following-char) ?\
{)
1409 ;; Don't move an open-brace in column 0.
1410 ;; This is good when constructs such as
1411 ;; `extern "C" {' surround a function definition
1412 ;; that should be indented as usual.
1413 ;; It is also good for nested functions.
1414 ;; It is bad when an open-brace is indented at column 0
1415 ;; and you want to fix that, but we can't win 'em all.
1416 (if (zerop (current-column))
1417 (setq this-indent
0)
1418 (setq this-indent
(+ this-indent c-brace-offset
))))
1419 ;; Don't leave indentation in empty lines.
1420 (if (eolp) (setq this-indent
0))
1421 ;; Put chosen indentation into effect.
1422 (or (= (current-column) this-indent
)
1423 (= (following-char) ?\
#)
1425 (delete-region (point) (progn (beginning-of-line) (point)))
1426 (indent-to this-indent
)))
1427 ;; Indent any comment following the text.
1428 (or (looking-at comment-start-skip
)
1430 (let ((beg (point)))
1431 (and (re-search-forward
1433 (save-excursion (end-of-line) (point)) t
)
1434 ;; Make sure the comment starter we found
1435 ;; is not actually in a string or quoted.
1437 (parse-partial-sexp beg
(point)
1439 (and (not (nth 3 new-state
)) (not (nth 5 new-state
))))
1440 (indent-for-comment)))))))))))
1442 ;; Look at all comment-start strings in the current line after point.
1443 ;; Return t if one of them starts a real comment.
1444 ;; This is not used yet, because indent-for-comment
1445 ;; isn't smart enough to handle the cases this can find.
1446 (defun indent-c-find-real-comment ()
1448 (while (and (not win
)
1449 (re-search-forward comment-start-skip
1450 (save-excursion (end-of-line) (point))
1452 ;; Make sure the comment start is not quoted.
1455 (save-excursion (beginning-of-line) (point))
1456 (point) nil nil state
)))
1457 (setq win
(and (null (nth 3 state-1
)) (null (nth 5 state-1
))))))
1460 ;; Indent every line whose first char is between START and END inclusive.
1461 (defun c-indent-region (start end
)
1464 ;; Advance to first nonblank line.
1465 (skip-chars-forward " \t\n")
1467 (let ((endmark (copy-marker end
))
1468 (c-tab-always-indent t
))
1469 (while (and (bolp) (not (eobp)) (< (point) endmark
))
1470 ;; Indent one line as with TAB.
1471 (let ((shift-amt (c-indent-line))
1472 nextline sexpbeg sexpend
)
1473 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*#"))
1476 ;; Find beginning of following line.
1478 (forward-line 1) (setq nextline
(point)))
1479 ;; Find first beginning-of-sexp for sexp extending past this line.
1481 (while (< (point) nextline
)
1485 (setq sexpend
(point-marker)))
1486 (error (setq sexpend nil
)
1487 (goto-char nextline
)))
1488 (skip-chars-forward " \t\n"))
1491 ;; Make sure the sexp we found really starts on the
1492 ;; current line and extends past it.
1495 (setq sexpbeg
(point)))))
1496 ;; If that sexp ends within the region,
1497 ;; indent it all at once, fast.
1498 (if (and sexpend
(> sexpend nextline
) (<= sexpend endmark
)
1499 (< sexpbeg nextline
))
1502 (goto-char sexpend
)))
1503 ;; Move to following line and try again.
1504 (and sexpend
(set-marker sexpend nil
))
1506 (set-marker endmark nil
))))
1508 (defun set-c-style (style &optional global
)
1509 "Set C-mode variables to use one of several different indentation styles.
1510 The arguments are a string representing the desired style
1511 and a flag which, if non-nil, means to set the style globally.
1512 \(Interactively, the flag comes from the prefix argument.)
1513 Available styles are GNU, K&R, BSD and Whitesmith."
1514 (interactive (list (let ((completion-ignore-case t
))
1515 (completing-read "Use which C indentation style? "
1516 c-style-alist nil t
))
1517 current-prefix-arg
))
1518 (let ((vars (cdr (assoc style c-style-alist
))))
1520 (error "Invalid C indentation style `%s'" style
))
1523 (make-local-variable (car (car vars
))))
1524 (set (car (car vars
)) (cdr (car vars
)))
1525 (setq vars
(cdr vars
)))))
1527 ;;; This page handles insertion and removal of backslashes for C macros.
1529 (defcustom c-backslash-column
48
1530 "*Minimum column for end-of-line backslashes of macro definitions."
1534 (defun c-backslash-region (from to delete-flag
)
1535 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1536 With no argument, inserts backslashes and aligns existing backslashes.
1537 With an argument, deletes the backslashes.
1539 This function does not modify the last line of the region if the region ends
1540 right at the start of the following line; it does not modify blank lines
1541 at the start of the region. So you can put the region around an entire macro
1542 definition and conveniently use this command."
1543 (interactive "r\nP")
1546 (let ((column c-backslash-column
)
1547 (endmark (make-marker)))
1548 (move-marker endmark to
)
1549 ;; Compute the smallest column number past the ends of all the lines.
1550 (if (not delete-flag
)
1551 (while (< (point) to
)
1553 (if (= (preceding-char) ?
\\)
1554 (progn (forward-char -
1)
1555 (skip-chars-backward " \t")))
1556 (setq column
(max column
(1+ (current-column))))
1558 ;; Adjust upward to a tab column, if that doesn't push past the margin.
1559 (if (> (% column tab-width
) 0)
1560 (let ((adjusted (* (/ (+ column tab-width -
1) tab-width
) tab-width
)))
1561 (if (< adjusted
(window-width))
1562 (setq column adjusted
))))
1563 ;; Don't modify blank lines at start of region.
1565 (while (and (< (point) endmark
) (eolp))
1567 ;; Add or remove backslashes on all the lines.
1568 (while (and (< (point) endmark
)
1569 ;; Don't backslashify the last line
1570 ;; if the region ends right at the start of the next line.
1573 (< (point) endmark
)))
1574 (if (not delete-flag
)
1575 (c-append-backslash column
)
1576 (c-delete-backslash))
1578 (move-marker endmark nil
))))
1580 (defun c-append-backslash (column)
1582 ;; Note that "\\\\" is needed to get one backslash.
1583 (if (= (preceding-char) ?
\\)
1584 (progn (forward-char -
1)
1585 (delete-horizontal-space)
1590 (defun c-delete-backslash ()
1595 (if (looking-at "\\\\")
1596 (delete-region (1+ (point))
1597 (progn (skip-chars-backward " \t") (point)))))))
1599 (defun c-up-conditional (count)
1600 "Move back to the containing preprocessor conditional, leaving mark behind.
1601 A prefix argument acts as a repeat count. With a negative argument,
1602 move forward to the end of the containing preprocessor conditional.
1603 When going backwards, `#elif' is treated like `#else' followed by `#if'.
1604 When going forwards, `#elif' is ignored."
1606 (c-forward-conditional (- count
) t
))
1608 (defun c-backward-conditional (count &optional up-flag
)
1609 "Move back across a preprocessor conditional, leaving mark behind.
1610 A prefix argument acts as a repeat count. With a negative argument,
1611 move forward across a preprocessor conditional."
1613 (c-forward-conditional (- count
) up-flag
))
1615 (defun c-forward-conditional (count &optional up-flag
)
1616 "Move forward across a preprocessor conditional, leaving mark behind.
1617 A prefix argument acts as a repeat count. With a negative argument,
1618 move backward across a preprocessor conditional."
1620 (let* ((forward (> count
0))
1621 (increment (if forward -
1 1))
1622 (search-function (if forward
're-search-forward
're-search-backward
))
1627 (let ((depth (if up-flag
0 -
1)) found
)
1629 ;; Find the "next" significant line in the proper direction.
1630 (while (and (not found
)
1631 ;; Rather than searching for a # sign that comes
1632 ;; at the beginning of a line aside from whitespace,
1633 ;; search first for a string starting with # sign.
1634 ;; Then verify what precedes it.
1635 ;; This is faster on account of the fastmap feature of
1636 ;; the regexp matcher.
1637 (funcall search-function
1638 "#[ \t]*\\(if\\|elif\\|endif\\)"
1641 ;; Now verify it is really a preproc line.
1642 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
1644 ;; Update depth according to what we found.
1646 (cond ((looking-at "[ \t]*#[ \t]*endif")
1647 (setq depth
(+ depth increment
)))
1648 ((looking-at "[ \t]*#[ \t]*elif")
1649 (if (and forward
(= depth
0))
1650 (setq found
(point))))
1651 (t (setq depth
(- depth increment
))))
1652 ;; If we are trying to move across, and we find
1653 ;; an end before we find a beginning, get an error.
1654 (if (and (< prev
0) (< depth prev
))
1656 "No following conditional at this level"
1657 "No previous conditional at this level")))
1658 ;; When searching forward, start from next line
1659 ;; so that we don't find the same line again.
1660 (if forward
(forward-line 1))
1661 ;; If this line exits a level of conditional, exit inner loop.
1663 (setq found
(point))))
1664 ;; If the line is not really a conditional, skip past it.
1665 (if forward
(end-of-line)))))
1667 (error "No containing preprocessor conditional"))
1668 (goto-char (setq new found
)))
1669 (setq count
(+ count increment
))))
1675 ;;; c-mode.el ends here