* viper-ex.el: Patch by Samuel Padgett. Copyright papers received.
[emacs.git] / lisp / obsolete / c-mode.el
blob7224f251ecfcee392f39ff1dfd07cf6aa55fb4ae
1 ;;; c-mode.el --- C code editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 92, 94, 95 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: c
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)
13 ;; any later version.
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.
25 ;;; Commentary:
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.
32 ;;; Code:
33 (defgroup old-c nil
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.
37 :prefix "c-")
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."
125 :type 'integer
126 :group 'old-c)
127 (defcustom c-brace-imaginary-offset 0
128 "*Imagined indentation of a C open brace that actually follows a statement."
129 :type 'integer
130 :group 'old-c)
131 (defcustom c-brace-offset 0
132 "*Extra indentation for braces, compared with other text in same context."
133 :type 'integer
134 :group 'old-c)
135 (defcustom c-argdecl-indent 5
136 "*Indentation level of declarations of C function arguments."
137 :type 'integer
138 :group 'old-c)
139 (defcustom c-label-offset -2
140 "*Offset of C label lines and case statements relative to usual indentation."
141 :type 'integer
142 :group 'old-c)
143 (defcustom c-continued-statement-offset 2
144 "*Extra indent for lines not starting new statements."
145 :type 'integer
146 :group 'old-c)
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'."
150 :type 'integer
151 :group 'old-c)
152 (defconst c-style-alist
153 '(("GNU"
154 (c-indent-level . 2)
155 (c-argdecl-indent . 5)
156 (c-brace-offset . 0)
157 (c-continued-brace-offset . 0)
158 (c-label-offset . -2)
159 (c-continued-statement-offset . 2))
160 ("K&R"
161 (c-indent-level . 5)
162 (c-argdecl-indent . 0)
163 (c-brace-offset . 0)
164 (c-continued-brace-offset . -5)
165 (c-label-offset . -5)
166 (c-continued-statement-offset . 5))
167 ("BSD"
168 (c-indent-level . 4)
169 (c-argdecl-indent . 4)
170 (c-brace-offset . 0)
171 (c-continued-brace-offset . -4)
172 (c-label-offset . -4)
173 (c-continued-statement-offset . 4))
174 ("C++"
175 (c-indent-level . 4)
176 (c-argdecl-indent . 0)
177 (c-brace-offset . 0)
178 (c-continued-brace-offset . -4)
179 (c-label-offset . -4)
180 (c-continued-statement-offset . 4)
181 (c-auto-newline . t))
182 ("Whitesmith"
183 (c-indent-level . 4)
184 (c-argdecl-indent . 4)
185 (c-brace-offset . 0)
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)"
195 :type 'boolean
196 :group 'old-c)
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."
201 :type 'boolean
202 :group 'old-c)
204 ;;; Regular expression used internally to recognize labels in switch
205 ;;; statements.
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
210 `((nil
211 ,(concat
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 */&
218 "[a-zA-Z0-9_:]+"
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*"!)
225 "\\|"
226 "\\([a-zA-Z0-9_:~]*::\\)?operator"
227 "[^a-zA-Z1-9_][^(]*" ; ...or operator
228 " \\)"
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()) {...}"
235 ) 6)
236 ("Class"
237 ,(concat
238 "^" ; beginning of line is required
239 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
240 "class[ \t]+"
241 "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
242 "[ \t]*[:{]"
243 ) 2)
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
246 ;; the indexes.
247 ;;; ("Prototypes"
248 ;;; (,
249 ;;; (concat
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 */&
256 ;;; "[a-zA-Z0-9_:]+"
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*"!)
263 ;;; "\\|"
264 ;;; "\\([a-zA-Z0-9_:~]*::\\)?operator"
265 ;;; "[^a-zA-Z1-9_][^(]*" ; ...or operator
266 ;;; " \\)"
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());"
272 ;;; )) 6)
273 ;;; ("Struct"
274 ;;; (, (concat
275 ;;; "^" ; beginning of line is required
276 ;;; "\\(static[ \t]+\\)?" ; there may be static or const.
277 ;;; "\\(const[ \t]+\\)?"
278 ;;; "struct[ \t]+"
279 ;;; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
280 ;;; "[ \t]*[{]"
281 ;;; )) 3)
282 ;;; ("Enum"
283 ;;; (, (concat
284 ;;; "^" ; beginning of line is required
285 ;;; "\\(static[ \t]+\\)?" ; there may be static or const.
286 ;;; "\\(const[ \t]+\\)?"
287 ;;; "enum[ \t]+"
288 ;;; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
289 ;;; "[ \t]*[{]"
290 ;;; )) 3)
291 ;;; ("Union"
292 ;;; (, (concat
293 ;;; "^" ; beginning of line is required
294 ;;; "\\(static[ \t]+\\)?" ; there may be static or const.
295 ;;; "\\(const[ \t]+\\)?"
296 ;;; "union[ \t]+"
297 ;;; "\\([a-zA-Z0-9_]+\\)" ; this is the string we want to get
298 ;;; "[ \t]*[{]"
299 ;;; )) 3)
301 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
303 (defun c-mode ()
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.
310 \\{c-mode-map}
311 Variables controlling indentation style:
312 c-tab-always-indent
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.
315 c-auto-newline
316 Non-nil means automatically newline before and after braces,
317 and after colons and semicolons, inserted in C code.
318 c-indent-level
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.
328 c-brace-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.
333 c-argdecl-indent
334 Indentation level of declarations of C function arguments.
335 c-label-offset
336 Extra indentation for line that is a label, or case or default.
338 Settings for K&R and BSD indentation styles are
339 c-indent-level 5 8
340 c-continued-statement-offset 5 8
341 c-brace-offset -5 -8
342 c-argdecl-indent 0 8
343 c-label-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."
347 (interactive)
348 (kill-all-local-variables)
349 (use-local-map c-mode-map)
350 (setq major-mode 'c-mode)
351 (setq mode-name "C")
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)
392 (save-excursion
393 (skip-chars-forward "\t ")
394 (current-column))))
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)))
403 (save-excursion
404 (beginning-of-line)
405 (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)")
406 ;; A comment following a solitary close-brace
407 ;; should have only one space.
408 (search-forward "}")
409 (1+ (current-column)))
410 ((or (looking-at "^#[ \t]*endif[ \t]*")
411 (looking-at "^#[ \t]*else[ \t]*"))
412 7) ;2 spaces after #endif
413 ((progn
414 (goto-char opoint)
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."
429 (interactive "P")
430 (let* (comment-start-place
431 (first-line
432 ;; Check for obvious entry to comment.
433 (save-excursion
434 (beginning-of-line)
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)
439 (save-excursion
440 (beginning-of-line)
441 (looking-at ".*//")))
442 (let (fill-prefix
443 (paragraph-start
444 ;; Lines containing just a comment start or just an end
445 ;; should not be filled into paragraphs they are next to.
446 (concat
447 paragraph-start
448 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
449 (paragraph-separate
450 (concat
451 paragraph-separate
452 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")))
453 (save-excursion
454 (beginning-of-line)
455 ;; Move up to first line of this comment.
456 (while (and (not (bobp)) (looking-at "[ \t]*//"))
457 (forward-line -1))
458 (if (not (looking-at ".*//"))
459 (forward-line 1))
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")
466 (setq fill-prefix
467 (concat (make-string (- (current-column) 2) ?\ )
468 "//"
469 (make-string (- endcol (current-column)) ?\ ))))
470 (save-restriction
471 ;; Narrow down to just the lines of this comment.
472 (narrow-to-region (point)
473 (save-excursion
474 (forward-line 1)
475 (while (looking-at "[ \t]*//")
476 (forward-line 1))
477 (point)))
478 (insert fill-prefix)
479 (fill-paragraph arg)
480 (delete-region (point-min)
481 (+ (point-min) (length fill-prefix))))))
482 (if (or first-line
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.
486 (setq first-line
487 (save-excursion
488 (beginning-of-line)
489 (prog1
490 (re-search-forward comment-start-skip
491 (save-excursion (end-of-line)
492 (point))
494 (setq comment-start-place (point))))))
495 ;; Inside a comment: fill one comment paragraph.
496 (let ((fill-prefix
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.
500 (save-excursion
501 (beginning-of-line)
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))))
508 (beginning-of-line)
509 (prog1
510 (buffer-substring
511 (point)
513 ;; How shall we decide where the end of the
514 ;; fill-prefix is?
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
522 (progn
523 (move-to-column
524 (calculate-c-indent-within-comment t)
526 (point))))
527 (beginning-of-line)
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) ?*)
533 (backward-char 1))
534 (point)))
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)
551 (point))
552 (progn (end-of-line) (point))))))))
554 (paragraph-start
555 ;; Lines containing just a comment start or just an end
556 ;; should not be filled into paragraphs they are next to.
557 (concat
558 paragraph-start
559 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
560 (paragraph-separate
561 (concat
562 paragraph-separate
563 "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))
564 (chars-to-delete 0))
565 (save-restriction
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)))
579 (prog1 (point)
580 (setq chars-to-delete column)
581 (insert-char ?\ column))))
582 (save-excursion
583 (if comment-start-place
584 (goto-char (+ comment-start-place 2)))
585 (search-forward "*/" nil 'move)
586 (forward-line 1)
587 (point)))
588 (save-excursion
589 (goto-char (point-max))
590 (forward-line -1)
591 ;; And comment terminator was on a separate line before,
592 ;; keep it that way.
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))))
598 (fill-paragraph arg)
599 (save-excursion
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
608 ;; boundaries.
609 (goto-char (point-max))
610 (forward-line -1)
611 (search-forward "*/" nil 'move)
612 (beginning-of-line)
613 (if (looking-at "[ \t]*\\*/")
614 (let ((fill-column (+ fill-column 9999)))
615 (forward-line -1)
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."
623 (interactive "P")
624 (let (insertpos)
625 (if (and (not arg)
626 (eolp)
627 (or (save-excursion
628 (skip-chars-backward " \t")
629 (bolp))
630 (if c-auto-newline (progn (c-indent-line) (newline) t) nil)))
631 (progn
632 (insert last-command-char)
633 (c-indent-line)
634 (if c-auto-newline
635 (progn
636 (newline)
637 ;; (newline) may have done auto-fill
638 (setq insertpos (- (point) 2))
639 (c-indent-line)))
640 (save-excursion
641 (if insertpos (goto-char (1+ insertpos)))
642 (delete-char -1))))
643 (if insertpos
644 (save-excursion
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."
651 (interactive "P")
652 (if (save-excursion
653 (skip-chars-backward " \t")
654 (bolp))
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."
661 (interactive "P")
662 (if c-auto-newline
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."
668 (interactive "P")
669 (let (insertpos (end (point)))
670 (if (and (not arg) (eolp)
671 (not (save-excursion
672 (beginning-of-line)
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))
680 (save-excursion
681 (skip-chars-forward "a-zA-Z0-9_$")
682 (skip-chars-forward " \t")
683 (< (point) end)))
684 (progn
685 (beginning-of-defun)
686 (let ((pps (parse-partial-sexp (point) end)))
687 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
688 (progn
689 (insert last-command-char)
690 (c-indent-line)
691 (and c-auto-newline
692 (not (c-inside-parens-p))
693 (progn
694 (newline)
695 ;; (newline) may have done auto-fill
696 (setq insertpos (- (point) 2))
697 (c-indent-line)))
698 (save-excursion
699 (if insertpos (goto-char (1+ insertpos)))
700 (delete-char -1))))
701 (if insertpos
702 (save-excursion
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 ()
708 (condition-case ()
709 (save-excursion
710 (save-restriction
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))) ?\()))
715 (error nil)))
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."
727 (interactive "P")
728 (if whole-exp
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))
732 beg end)
733 (save-excursion
734 (if c-tab-always-indent
735 (beginning-of-line))
736 ;; Find beginning of following line.
737 (save-excursion
738 (forward-line 1) (setq beg (point)))
739 ;; Find first beginning-of-sexp for sexp extending past this line.
740 (while (< (point) beg)
741 (forward-sexp 1)
742 (setq end (point))
743 (skip-chars-forward " \t\n")))
744 (if (> end beg)
745 (indent-code-rigidly beg end shift-amt "#")))
746 (if (and (not c-tab-always-indent)
747 (save-excursion
748 (skip-chars-backward " \t")
749 (not (bolp))))
750 (insert-tab)
751 (c-indent-line))))
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))
757 beg shift-amt
758 (case-fold-search nil)
759 (pos (- (point-max) (point))))
760 (beginning-of-line)
761 (setq beg (point))
762 (cond ((eq indent nil)
763 (setq indent (current-indentation)))
764 ((eq indent t)
765 (setq indent (calculate-c-indent-within-comment)))
766 ((looking-at "[ \t]*#")
767 (setq indent 0))
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]")
773 (save-excursion
774 (forward-sexp 1)
775 (looking-at ":"))))
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
785 (forward-char)
786 (backward-sexp)
787 (c-backward-to-start-of-if)
788 (current-indentation))))
789 ((and (looking-at "while\\b")
790 (not (looking-at "while\\s_"))
791 (save-excursion
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))
807 (indent-to indent)
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))))
812 shift-amt))
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."
818 (save-excursion
819 (beginning-of-line)
820 (let ((indent-point (point))
821 (case-fold-search nil)
822 state
823 containing-sexp)
824 (if parse-start
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
833 (nth 4 state))
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.
848 (let ((basic-indent
849 (save-excursion
850 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
851 (let (comment lim)
852 ;; Recognize the DEFUN macro in Emacs.
853 (if (save-excursion
854 ;; Move down to the (putative) argnames line.
855 (while (and (not (eobp))
856 (not (looking-at " *[({}#/]")))
857 (forward-line 1))
858 ;; Go back to the DEFUN, if it is one.
859 (condition-case nil
860 (backward-sexp 1)
861 (error))
862 (beginning-of-line)
863 (looking-at "DEFUN\\b"))
864 c-argdecl-indent
865 (if (and (looking-at "\\sw\\|\\s_")
866 ;; This is careful to stop at the first
867 ;; paren if we have
868 ;; int foo Proto ((int, int));
869 (looking-at "[^\"\n=(]*(")
870 (progn
871 (goto-char (1- (match-end 0)))
872 ;; Skip any number of paren-groups.
873 ;; Consider typedef int (*fcn) (int);
874 (while (= (following-char) ?\()
875 (setq lim (point))
876 (condition-case nil
877 (forward-sexp 1)
878 (error))
879 (skip-chars-forward " \t\f"))
880 ;; Have we reached something
881 ;; that shows this isn't a function
882 ;; definition?
883 (and (< (point) indent-point)
884 (not (memq (following-char)
885 '(?\, ?\;)))))
886 ;; Make sure the "function decl" we found
887 ;; is not inside a comment.
888 (progn
889 ;; Move back to the `(' starting arglist
890 (goto-char lim)
891 (beginning-of-line)
892 (while (and (not comment)
893 (search-forward "/*" lim t))
894 (setq comment
895 (not (search-forward "*/" lim t))))
896 (not comment)))
897 c-argdecl-indent 0))))))
898 basic-indent)))
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))
914 (current-column))
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)))
926 '(?w ?_)))))
927 (if (eq (preceding-char) ?\,)
928 (progn (forward-char -1)
929 (c-backward-to-start-of-continued-exp containing-sexp)))
930 (beginning-of-line)
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)
936 (while (not stop)
937 (beginning-of-line)
938 (cond ((bobp)
939 (setq found (point)
940 stop t))
941 ((save-excursion (forward-char -1)
942 (= (preceding-char) ?\\))
943 (forward-char -1))
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) ?#)
949 (forward-char -1)
950 (setq found (point)))
951 (t (setq stop t))))
952 (goto-char found))
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.
958 (save-excursion
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.
965 (progn
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.
978 (save-excursion
979 (forward-char 1)
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) ?\#)
985 (forward-line 1))
986 ((= (following-char) ?\/)
987 (forward-char 2)
988 (search-forward "*/" nil 'move))
989 ;; case or label:
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)
1000 (current-column))
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)
1020 c-indent-level)
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
1029 (progn
1030 (if (eq (preceding-char) ?\))
1031 (forward-sexp -1))
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)
1040 (save-excursion
1041 (beginning-of-line)
1042 (skip-chars-forward " \t")
1043 (setq star-start (= (following-char) ?\*))
1044 (skip-chars-backward " \t\n")
1045 (setq end (point))
1046 (beginning-of-line)
1047 (skip-chars-forward " \t")
1048 (if after-star
1049 (and (looking-at "\\*")
1050 (re-search-forward "\\*[ \t]*")))
1051 (and (re-search-forward "/\\*[ \t]*" end t)
1052 star-start
1053 (not after-star)
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)
1061 (let (opoint stop)
1062 (while (not stop)
1063 (skip-chars-backward " \t\n\f" lim)
1064 (setq opoint (point))
1065 (if (and (>= (point) (+ 2 lim))
1066 (save-excursion
1067 (forward-char -2)
1068 (looking-at "\\*/")))
1069 (search-backward "/*" lim 'move)
1070 (setq stop (or (<= (point) lim)
1071 (save-excursion
1072 (beginning-of-line)
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) '(?\) ?\"))
1079 (forward-sexp -1))
1080 (beginning-of-line)
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))))
1088 (let ((if-level 1)
1089 (case-fold-search nil))
1090 (while (and (not (bobp)) (not (zerop if-level)))
1091 (backward-sexp 1)
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)))
1098 ((< (point) limit)
1099 (setq if-level 0)
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))))
1106 (let ((first t)
1107 (startpos (point))
1108 (done nil))
1109 (while (not done)
1110 (let ((next-start (point)))
1111 (condition-case nil
1112 ;; Move back one token or one brace or paren group.
1113 (backward-sexp 1)
1114 ;; If we find an open-brace, we lose.
1115 (error (setq done 'fail)))
1116 (if done
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.)
1125 (if (save-excursion
1126 (forward-sexp 1)
1127 (or (and (not first) (= (preceding-char) ?\}))
1128 (search-forward ";" next-start t
1129 (if (and first
1130 (/= (preceding-char) ?\}))
1131 2 1))))
1132 (setq done 'fail)
1133 (setq first nil)
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)
1140 nil)))
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."
1148 (interactive "p")
1149 (let ((here (point)) state)
1150 (save-excursion
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))
1159 (while (> count 0)
1160 (c-beginning-of-statement-1)
1161 (setq count (1- count)))
1162 (while (< count 0)
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."
1171 (interactive "p")
1172 (c-beginning-of-statement (- count)))
1174 (defun c-beginning-of-statement-1 ()
1175 (let ((last-begin (point))
1176 (first t))
1177 (condition-case ()
1178 (progn
1179 (while (and (not (bobp))
1180 (progn
1181 (backward-sexp 1)
1182 (or first
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 ()
1189 (condition-case ()
1190 (progn
1191 (while (and (not (eobp))
1192 (let ((beg (point)))
1193 (forward-sexp 1)
1194 (let ((end (point)))
1195 (save-excursion
1196 (goto-char beg)
1197 (not (re-search-forward "[;{}]" end t)))))))
1198 (re-search-backward "[;}]")
1199 (forward-char 1))
1200 (error
1201 (let ((beg (point)))
1202 (backward-up-list -1)
1203 (let ((end (point)))
1204 (goto-char beg)
1205 (search-forward ";" end 'move))))))
1207 (defun mark-c-function ()
1208 "Put mark at end of C function, point at beginning."
1209 (interactive)
1210 (push-mark (point))
1211 (end-of-defun)
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."
1220 (interactive)
1221 (let* ((indent-stack (list nil))
1222 (opoint (point)) ;; May be altered below.
1223 (contain-stack
1224 (list (if endpos
1225 (let (funbeg)
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.
1233 (save-restriction
1234 (narrow-to-region funbeg (point))
1235 (condition-case nil
1236 (save-excursion
1237 (backward-up-list 1)
1238 (point))
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)
1244 (point)))))
1245 (point))))
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
1251 (next-depth 0))
1252 ;; If the braces don't match, get an error right away.
1253 (save-excursion
1254 (forward-sexp 1))
1255 ;; Realign the comment on the first line, even though we don't reindent it.
1256 (save-excursion
1257 (let ((beg (point)))
1258 (and (re-search-forward
1259 comment-start-skip
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).
1263 (save-excursion
1264 (goto-char (match-beginning 0))
1265 (skip-chars-backward " \t")
1266 (not (bolp)))
1267 ;; Make sure the comment starter we found
1268 ;; is not actually in a string or quoted.
1269 (let ((new-state
1270 (parse-partial-sexp beg (point)
1271 nil nil state)))
1272 (and (not (nth 3 new-state)) (not (nth 5 new-state))))
1273 (progn (indent-for-comment) (beginning-of-line)))))
1274 (save-excursion
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))))
1287 (setq ostate state)
1288 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1289 nil nil state))
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))
1296 (c-indent-line))
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))
1300 (forward-line 1)
1301 (setq inner-loop-done t)))
1302 (and endpos
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)))
1310 (if outer-loop-done
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)
1329 (point)))))
1330 (forward-line 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))
1345 ;; Yes.
1346 ;; Compute the standard indent for this level.
1347 (let (val)
1348 (if (= (char-after (car contain-stack)) ?\{)
1349 (save-excursion
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))
1355 opoint))))
1356 ;; t means we are in a block comment and should
1357 ;; calculate accordingly.
1358 (if (eq val t)
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
1369 (save-excursion
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.
1383 (progn
1384 (c-backward-to-start-of-continued-exp (car contain-stack))
1385 (setq this-indent
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)
1391 (setq this-indent
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]")
1402 (save-excursion
1403 (forward-sexp 1)
1404 (looking-at ":"))))
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) ?\#)
1424 (progn
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)
1429 (save-excursion
1430 (let ((beg (point)))
1431 (and (re-search-forward
1432 comment-start-skip
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.
1436 (let ((new-state
1437 (parse-partial-sexp beg (point)
1438 nil nil state)))
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 ()
1447 (let (win)
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.
1453 (let ((state-1
1454 (parse-partial-sexp
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))))))
1458 win))
1460 ;; Indent every line whose first char is between START and END inclusive.
1461 (defun c-indent-region (start end)
1462 (save-excursion
1463 (goto-char start)
1464 ;; Advance to first nonblank line.
1465 (skip-chars-forward " \t\n")
1466 (beginning-of-line)
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]*#"))
1474 (forward-line 1)
1475 (save-excursion
1476 ;; Find beginning of following line.
1477 (save-excursion
1478 (forward-line 1) (setq nextline (point)))
1479 ;; Find first beginning-of-sexp for sexp extending past this line.
1480 (beginning-of-line)
1481 (while (< (point) nextline)
1482 (condition-case nil
1483 (progn
1484 (forward-sexp 1)
1485 (setq sexpend (point-marker)))
1486 (error (setq sexpend nil)
1487 (goto-char nextline)))
1488 (skip-chars-forward " \t\n"))
1489 (if sexpend
1490 (progn
1491 ;; Make sure the sexp we found really starts on the
1492 ;; current line and extends past it.
1493 (goto-char sexpend)
1494 (backward-sexp 1)
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))
1500 (progn
1501 (indent-c-exp)
1502 (goto-char sexpend)))
1503 ;; Move to following line and try again.
1504 (and sexpend (set-marker sexpend nil))
1505 (forward-line 1))))
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))))
1519 (or vars
1520 (error "Invalid C indentation style `%s'" style))
1521 (while vars
1522 (or global
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."
1531 :type 'integer
1532 :group 'old-c)
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")
1544 (save-excursion
1545 (goto-char from)
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)
1552 (end-of-line)
1553 (if (= (preceding-char) ?\\)
1554 (progn (forward-char -1)
1555 (skip-chars-backward " \t")))
1556 (setq column (max column (1+ (current-column))))
1557 (forward-line 1)))
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.
1564 (goto-char from)
1565 (while (and (< (point) endmark) (eolp))
1566 (forward-line 1))
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.
1571 (save-excursion
1572 (forward-line 1)
1573 (< (point) endmark)))
1574 (if (not delete-flag)
1575 (c-append-backslash column)
1576 (c-delete-backslash))
1577 (forward-line 1))
1578 (move-marker endmark nil))))
1580 (defun c-append-backslash (column)
1581 (end-of-line)
1582 ;; Note that "\\\\" is needed to get one backslash.
1583 (if (= (preceding-char) ?\\)
1584 (progn (forward-char -1)
1585 (delete-horizontal-space)
1586 (indent-to column))
1587 (indent-to column)
1588 (insert "\\")))
1590 (defun c-delete-backslash ()
1591 (end-of-line)
1592 (or (bolp)
1593 (progn
1594 (forward-char -1)
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."
1605 (interactive "p")
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."
1612 (interactive "p")
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."
1619 (interactive "p")
1620 (let* ((forward (> count 0))
1621 (increment (if forward -1 1))
1622 (search-function (if forward 're-search-forward 're-search-backward))
1623 (opoint (point))
1624 (new))
1625 (save-excursion
1626 (while (/= count 0)
1627 (let ((depth (if up-flag 0 -1)) found)
1628 (save-excursion
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\\)"
1639 nil t))
1640 (beginning-of-line)
1641 ;; Now verify it is really a preproc line.
1642 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
1643 (let ((prev depth))
1644 ;; Update depth according to what we found.
1645 (beginning-of-line)
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))
1655 (error (if forward
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.
1662 (if (< depth 0)
1663 (setq found (point))))
1664 ;; If the line is not really a conditional, skip past it.
1665 (if forward (end-of-line)))))
1666 (or found
1667 (error "No containing preprocessor conditional"))
1668 (goto-char (setq new found)))
1669 (setq count (+ count increment))))
1670 (push-mark)
1671 (goto-char new)))
1673 (provide 'c-mode)
1675 ;;; c-mode.el ends here