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