1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc.
5 ;; Authors: 1998-1999 Barry A. Warsaw and Martin Stjernholm
6 ;; 1992-1997 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Version: See cc-mode.el
12 ;; Keywords: c languages oop
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
33 (if (and (boundp 'byte-compile-current-file)
34 (stringp byte-compile-current-file))
35 (cons (file-name-directory byte-compile-current-file)
38 (load "cc-defs" nil t)))
42 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
43 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
44 ;; better way should be implemented, but this will at least shut up
46 (defvar c-maybe-labelp nil)
48 ;; WARNING WARNING WARNING
50 ;; Be *exceptionally* careful about modifications to this function!
51 ;; Much of CC Mode depends on this Doing The Right Thing. If you
52 ;; break it you will be sorry. If you think you know how this works,
53 ;; you probably don't. No human on Earth does! :-)
55 ;; WARNING WARNING WARNING
57 (defun c-beginning-of-statement-1 (&optional lim)
58 ;; move to the start of the current statement, or the previous
59 ;; statement if already at the beginning of one.
62 donep c-in-literal-cache saved
64 ;; first check for bare semicolon
65 (if (and (progn (c-backward-syntactic-ws lim)
66 (eq (char-before) ?\;))
67 (c-safe (progn (forward-char -1)
70 (progn (c-backward-syntactic-ws lim)
71 (memq (char-before) '(?\; ?{ ?:)))
73 (setq last-begin saved)
74 (goto-char last-begin)
76 ;; stop at beginning of buffer
77 (if (bobp) (setq donep t)
78 ;; go backwards one balanced expression, but be careful of
79 ;; unbalanced paren being reached
80 (if (not (c-safe (progn (c-backward-sexp 1) t)))
84 (goto-char last-begin))
85 ;; skip over any unary operators, or other special
86 ;; characters appearing at front of identifier
88 (c-backward-syntactic-ws lim)
89 (skip-chars-backward "-+!*&:.~@ \t\n")
90 (if (eq (char-before) ?\()
91 (setq last-begin (point))))
92 (goto-char last-begin)
93 (setq last-begin (point)
96 (setq c-maybe-labelp nil)
97 ;; see if we're in a literal. if not, then this bufpos may be
98 ;; a candidate for stopping
100 ;; CASE 0: did we hit the error condition above?
102 ;; CASE 1: are we in a literal?
103 ((eq (c-in-literal lim) 'pound)
105 ;; CASE 2: some other kind of literal?
107 ;; CASE 3: are we looking at a conditional keyword?
108 ((or (and c-conditional-key (looking-at c-conditional-key))
109 (and (eq (char-after) ?\()
112 (c-forward-syntactic-ws)
113 (not (eq (char-after) ?\;)))
116 (c-backward-syntactic-ws lim)
120 (not (c-in-literal lim))
121 (not (eq (char-before) ?_))
123 (looking-at c-conditional-key)
125 ;; did we find a conditional?
129 ;; are we in the middle of an else-if clause?
132 (c-safe (progn (c-forward-sexp -1) t))
133 (looking-at "\\<else\\>[ \t\n]+\\<if\\>")
134 (not (c-in-literal lim))))
137 (c-backward-to-start-of-if lim)))
138 ;; are we sitting at an else clause, that we are not a
140 (if (and (not substmt-p)
141 (looking-at "\\<else\\>[^_]"))
142 (c-backward-to-start-of-if lim))
143 ;; a finally or a series of catches?
145 (while (looking-at "\\<\\(catch\\|finally\\)\\>[^_]")
146 (c-safe (c-backward-sexp 2))
147 (if (eq (char-after) ?\()
148 (c-safe (c-backward-sexp)))))
149 ;; are we sitting at the while of a do-while?
150 (if (and (looking-at "\\<while\\>[^_]")
151 (c-backward-to-start-of-do lim))
152 (setq substmt-p nil))
153 (setq last-begin (point)
155 ;; CASE 4: are we looking at a label? (But we handle
156 ;; switch labels later.)
157 ((and (looking-at c-label-key)
158 (not (looking-at "default\\>"))))
159 ;; CASE 5: is this the first time we're checking?
160 (firstp (setq firstp nil
161 substmt-p (not (c-crosses-statement-barrier-p
164 ;; CASE 6: have we crossed a statement barrier?
166 ;; Move over in-expression blocks before checking the
168 (if (or (memq (char-after) '(?\( ?\[))
169 (and (eq (char-after) ?{)
170 (c-looking-at-inexpr-block lim)))
172 (c-crosses-statement-barrier-p (point) last-begin))
174 ;; CASE 7: ignore labels
176 (or (and c-access-key (looking-at c-access-key))
177 ;; with switch labels, we have to go back further
178 ;; to try to pick up the case or default
179 ;; keyword. Potential bogosity alert: we assume
180 ;; `case' or `default' is first thing on line
181 (let ((here (point)))
183 (c-forward-syntactic-ws here)
184 (if (looking-at c-switch-label-key)
188 (looking-at c-label-key))))
189 ;; CASE 8: ObjC or Java method def
191 (setq last-begin (c-in-method-def-p)))
193 ;; CASE 9: nothing special
194 (t (setq last-begin (point)))
196 (goto-char last-begin)
197 ;; We always want to skip over the non-whitespace modifier
198 ;; characters that can start a statement.
200 (skip-chars-backward "-+!*&~@` \t\n" (c-point 'boi))
201 (skip-chars-forward " \t\n" lim))))
203 (defun c-end-of-statement-1 ()
206 (while (and (not (eobp))
213 (while (and (not found)
214 (re-search-forward "[;{}]" end t))
215 (if (not (c-in-literal beg))
219 (re-search-backward "[;{}]")
223 (c-safe (backward-up-list -1))
226 (search-forward ";" end 'move)))
230 (defun c-crosses-statement-barrier-p (from to)
231 ;; Does buffer positions FROM to TO cross a C statement boundary?
238 (while (and (not crossedp)
240 (skip-chars-forward "^;{}:" (1- to))
241 (if (not (c-in-literal lim))
243 (if (memq (char-after) '(?\; ?{ ?}))
245 (if (eq (char-after) ?:)
246 (setq c-maybe-labelp t))
250 (error (setq crossedp nil)))
255 ;; Skipping of "syntactic whitespace", defined as lexical whitespace,
256 ;; C and C++ style comments, and preprocessor directives. Search no
257 ;; farther back or forward than optional LIM. If LIM is omitted,
258 ;; `beginning-of-defun' is used for backward skipping, point-max is
259 ;; used for forward skipping.
261 (defun c-forward-syntactic-ws (&optional lim)
262 ;; Forward skip of syntactic whitespace for Emacs 19.
263 (let* ((here (point-max))
264 (hugenum (point-max)))
265 (while (/= here (point))
267 (c-forward-comment hugenum)
268 ;; skip preprocessor directives
269 (when (and (eq (char-after) ?#)
270 (= (c-point 'boi) (point)))
271 (while (eq (char-before (c-point 'eol)) ?\\)
275 (if lim (goto-char (min (point) lim)))))
277 (defun c-backward-syntactic-ws (&optional lim)
278 ;; Backward skip over syntactic whitespace for Emacs 19.
279 (let* ((here (point-min))
280 (hugenum (- (point-max))))
281 (while (/= here (point))
283 (c-forward-comment hugenum)
284 (c-beginning-of-macro))
285 (if lim (goto-char (max (point) lim)))))
288 ;; Moving by tokens, where a token is defined as all symbols and
289 ;; identifiers which aren't syntactic whitespace (note that "->" is
290 ;; considered to be two tokens). Point is always either left at the
291 ;; beginning of a token or not moved at all. COUNT specifies the
292 ;; number of tokens to move; a negative COUNT moves in the opposite
293 ;; direction. A COUNT of 0 moves to the next token beginning only if
294 ;; not already at one. If BALANCED is true, move over balanced
295 ;; parens, otherwise move into them. Also, if BALANCED is true, never
296 ;; move out of an enclosing paren. LIM sets the limit for the
297 ;; movement and defaults to the point limit. Returns the number of
298 ;; tokens left to move (positive or negative). If BALANCED is true, a
299 ;; move over a balanced paren counts as one. Note that if COUNT is 0
300 ;; and no appropriate token beginning is found, 1 will be returned.
301 ;; Thus, a return value of 0 guarantees that point is at the requested
302 ;; position and a return value less (without signs) than COUNT
303 ;; guarantees that point is at the beginning of some token.
305 (defun c-forward-token-1 (&optional count balanced lim)
306 (or count (setq count 1))
308 (- (c-backward-token-1 (- count) balanced lim))
309 (let ((jump-syntax (if balanced
310 '(?w ?_ ?\( ?\) ?\" ?\\ ?/ ?$ ?')
311 '(?w ?_ ?\" ?\\ ?/ ?')))
315 (progn (c-forward-syntactic-ws) (point)))
316 ;; Skip whitespace. Count this as a move if we did in fact
317 ;; move and aren't out of bounds.
319 (and lim (> (point) lim))
320 (setq count (max (1- count) 0))))
322 (or (and (memq (char-syntax (or (char-after) ? )) '(?w ?_))
323 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
325 ;; If count is zero we should jump if in the middle of a
326 ;; token or if there is whitespace between point and the
327 ;; following token beginning.
329 ;; Avoid having the limit tests inside the loop.
331 (if lim (narrow-to-region (point-min) lim))
338 (if (memq (char-syntax (char-after)) jump-syntax)
339 (goto-char (scan-sexps (point) 1))
341 (c-forward-syntactic-ws lim)
342 (setq count (1- count)))
343 (error (goto-char last)))
346 (setq count (1+ count)))))
349 (defun c-backward-token-1 (&optional count balanced lim)
350 (or count (setq count 1))
352 (- (c-forward-token-1 (- count) balanced lim))
353 (let ((jump-syntax (if balanced
354 '(?w ?_ ?\( ?\) ?\" ?\\ ?/ ?$ ?')
355 '(?w ?_ ?\" ?\\ ?/ ?')))
358 (or (and (memq (char-syntax (or (char-after) ? )) '(?w ?_))
359 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
361 (save-excursion (c-forward-syntactic-ws) (point)))
363 ;; If count is zero we should jump if in the middle of a
364 ;; token or if there is whitespace between point and the
365 ;; following token beginning.
367 ;; Avoid having the limit tests inside the loop.
369 (if lim (narrow-to-region lim (point-max)))
376 (c-backward-syntactic-ws lim)
377 (if (memq (char-syntax (char-before)) jump-syntax)
378 (goto-char (scan-sexps (point) -1))
380 (setq count (1- count)))
381 (error (goto-char last)))
382 (if (bobp) (goto-char last)))))
386 ;; Return `c' if in a C-style comment, `c++' if in a C++ style
387 ;; comment, `string' if in a string literal, `pound' if on a
388 ;; preprocessor line, or nil if not in a comment at all. Optional LIM
389 ;; is used as the backward limit of the search. If omitted, or nil,
390 ;; `beginning-of-defun' is used."
392 (defun c-in-literal (&optional lim)
393 ;; Determine if point is in a C++ literal. we cache the last point
394 ;; calculated if the cache is enabled
395 (if (and (boundp 'c-in-literal-cache)
397 (= (point) (aref c-in-literal-cache 0)))
398 (aref c-in-literal-cache 1)
399 (let ((rtn (save-excursion
400 (let* ((lim (or lim (c-point 'bod)))
401 (state (parse-partial-sexp lim (point))))
403 ((nth 3 state) 'string)
404 ((nth 4 state) (if (nth 7 state) 'c++ 'c))
405 ((c-beginning-of-macro lim) 'pound)
407 ;; cache this result if the cache is enabled
408 (and (boundp 'c-in-literal-cache)
409 (setq c-in-literal-cache (vector (point) rtn)))
412 ;; XEmacs has a built-in function that should make this much quicker.
413 ;; I don't think we even need the cache, which makes our lives more
414 ;; complicated anyway. In this case, lim is ignored.
415 (defun c-fast-in-literal (&optional lim)
416 (let ((context (buffer-syntactic-context)))
418 ((eq context 'string) 'string)
419 ((eq context 'comment) 'c++)
420 ((eq context 'block-comment) 'c)
421 ((save-excursion (c-beginning-of-macro lim)) 'pound))))
423 (if (fboundp 'buffer-syntactic-context)
424 (defalias 'c-in-literal 'c-fast-in-literal))
426 (defun c-literal-limits (&optional lim near not-in-delimiter)
427 ;; Returns a cons of the beginning and end positions of the comment
428 ;; or string surrounding point (including both delimiters), or nil
429 ;; if point isn't in one. If LIM is non-nil, it's used as the
430 ;; "safe" position to start parsing from. If NEAR is non-nil, then
431 ;; the limits of any literal next to point is returned. "Next to"
432 ;; means there's only [ \t] between point and the literal. The
433 ;; search for such a literal is done first in forward direction. If
434 ;; NOT-IN-DELIMITER is non-nil, the case when point is inside a
435 ;; starting delimiter won't be recognized. This only has effect for
436 ;; comments, which have starting delimiters with more than one
440 (lim (or lim (c-point 'bod)))
441 (state (parse-partial-sexp lim (point))))
443 ;; String. Search backward for the start.
445 (search-backward (make-string 1 (nth 3 state)))
446 (setq state (parse-partial-sexp lim (point))))
447 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
450 ;; Line comment. Search from bol for the comment starter.
452 (setq state (parse-partial-sexp lim (point))
454 (while (not (nth 7 state))
455 (search-forward "//") ; Should never fail.
456 (setq state (parse-partial-sexp
457 lim (point) nil nil state)
460 (cons (point) (progn (c-forward-comment 1) (point))))
462 ;; Block comment. Search backward for the comment starter.
464 (search-backward "/*") ; Should never fail.
465 (setq state (parse-partial-sexp lim (point))))
466 (cons (point) (progn (c-forward-comment 1) (point))))
467 ((and (not not-in-delimiter)
469 (eq (char-before) ?/)
471 ;; We're standing in a comment starter.
473 (cons (point) (progn (c-forward-comment 1) (point))))
476 ;; Search forward for a literal.
477 (skip-chars-forward " \t")
479 ((eq (char-syntax (or (char-after) ?\ )) ?\") ; String.
480 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
482 ((looking-at "/[/*]") ; Line or block comment.
483 (cons (point) (progn (c-forward-comment 1) (point))))
486 (skip-chars-backward " \t")
487 (let ((end (point)) beg)
489 ((eq (char-syntax (or (char-before) ?\ )) ?\") ; String.
490 (setq beg (c-safe (c-backward-sexp 1) (point))))
491 ((and (c-safe (forward-char -2) t)
493 ;; Block comment. Due to the nature of line
494 ;; comments, they will always be covered by the
495 ;; normal case above.
497 (c-forward-comment -1)
498 ;; If LIM is bogus, beg will be bogus.
500 (if beg (cons beg end))))))
503 (defun c-literal-limits-fast (&optional lim near not-in-delimiter)
504 ;; Like c-literal-limits, but for emacsen whose `parse-partial-sexp'
505 ;; returns the pos of the comment start.
508 (lim (or lim (c-point 'bod)))
509 (state (parse-partial-sexp lim (point))))
510 (cond ((nth 3 state) ; String.
511 (goto-char (nth 8 state))
512 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
514 ((nth 4 state) ; Comment.
515 (goto-char (nth 8 state))
516 (cons (point) (progn (c-forward-comment 1) (point))))
517 ((and (not not-in-delimiter)
519 (eq (char-before) ?/)
521 ;; We're standing in a comment starter.
523 (cons (point) (progn (c-forward-comment 1) (point))))
526 ;; Search forward for a literal.
527 (skip-chars-forward " \t")
529 ((eq (char-syntax (or (char-after) ?\ )) ?\") ; String.
530 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
532 ((looking-at "/[/*]") ; Line or block comment.
533 (cons (point) (progn (c-forward-comment 1) (point))))
536 (skip-chars-backward " \t")
537 (let ((end (point)) beg)
539 ((eq (char-syntax (or (char-before) ?\ )) ?\") ; String.
540 (setq beg (c-safe (c-backward-sexp 1) (point))))
541 ((and (c-safe (forward-char -2) t)
543 ;; Block comment. Due to the nature of line
544 ;; comments, they will always be covered by the
545 ;; normal case above.
547 (c-forward-comment -1)
548 ;; If LIM is bogus, beg will be bogus.
550 (if beg (cons beg end))))))
553 (if (c-safe (> (length (save-excursion (parse-partial-sexp 1 1))) 8))
554 (defalias 'c-literal-limits 'c-literal-limits-fast))
556 (defun c-collect-line-comments (range)
557 ;; If the argument is a cons of two buffer positions (such as
558 ;; returned by c-literal-limits), and that range contains a C++
559 ;; style line comment, then an extended range is returned that
560 ;; contains all adjacent line comments (i.e. all comments that
561 ;; starts in the same column with no empty lines or non-whitespace
562 ;; characters between them). Otherwise the argument is returned.
565 (if (and (consp range) (progn
566 (goto-char (car range))
568 (let ((col (current-column))
571 (while (and (c-forward-comment -1)
573 (= col (current-column)))
576 (while (and (progn (skip-chars-forward " \t")
578 (= col (current-column))
579 (prog1 (zerop (forward-line 1))
580 (setq end (point)))))
585 (defun c-literal-type (range)
586 ;; Convenience function that given the result of c-literal-limits,
587 ;; returns nil or the type of literal that the range surrounds.
588 ;; It's much faster than using c-in-literal and is intended to be
589 ;; used when you need both the type of a literal and its limits.
592 (goto-char (car range))
593 (cond ((eq (char-syntax (or (char-after) ?\ )) ?\") 'string)
594 ((looking-at "//") 'c++)
595 (t 'c))) ; Assuming the range is valid.
600 ;; utilities for moving and querying around syntactic elements
601 (defvar c-parsing-error nil)
603 (defun c-parse-state ()
604 ;; Finds and records all open parens between some important point
605 ;; earlier in the file and point.
607 ;; if there's a state cache, return it
608 (setq c-parsing-error nil)
609 (if (boundp 'c-state-cache) c-state-cache
612 ;; go back 2 bods, but ignore any bogus positions
613 ;; returned by beginning-of-defun (i.e. open paren
616 (while (not (or at-bob (zerop cnt)))
617 (goto-char (c-point 'bod))
619 (eq (char-after) ?\{)
620 ;; The following catches an obscure special
621 ;; case where the brace is preceded by an
622 ;; open paren. That can only legally occur
623 ;; with blocks inside expressions and in
624 ;; Pike special brace lists. Even so, this
625 ;; test is still bogus then, but hopefully
626 ;; good enough. (We don't want to use
627 ;; up-list here since it might be slow.)
629 (c-backward-syntactic-ws)
630 (not (eq (char-before) ?\())))
635 (here (save-excursion
636 ;;(skip-chars-forward " \t}")
638 (last-bod pos) (last-pos pos)
639 placeholder state sexp-end)
640 ;; cache last bod position
641 (while (catch 'backup-bod
643 (while (and pos (< pos here))
645 (if (and (setq pos (c-safe (scan-lists pos 1 -1)))
648 (setq sexp-end (c-safe (scan-sexps (1- pos) 1)))
651 ;; we want to record both the start and end
652 ;; of this sexp, but we only want to record
653 ;; the last-most of any of them before here
655 (if (eq (char-after (1- pos)) ?\{)
656 (setq state (cons (cons (1- pos) sexp-end)
657 (if (consp (car state))
661 ;; we're contained in this sexp so put pos on
663 (setq state (cons (1- pos) state))))
664 ;; something bad happened. check to see if we
665 ;; crossed an unbalanced close brace. if so, we
666 ;; didn't really find the right `important bufpos'
667 ;; so lets back up and try again
668 (if (and (not pos) (not at-bob)
670 (c-safe (scan-lists last-pos 1 1)))
671 ;;(char-after (1- placeholder))
672 (<= placeholder here)
673 (eq (char-after (1- placeholder)) ?\}))
675 (setq last-bod (c-safe (scan-lists last-bod -1 1)))
678 ;; bogus, but what can we do here?
679 (setq c-parsing-error (1- placeholder))
680 (throw 'backup-bod nil))
681 (setq at-bob (= last-bod (point-min))
683 (if (= (char-after last-bod) ?\{)
684 (throw 'backup-bod t)))
690 (defun c-whack-state (bufpos state)
691 ;; whack off any state information that appears on STATE which lies
692 ;; after the bounds of BUFPOS.
695 (setq car (car state)
698 ;; just check the car, because in a balanced brace
699 ;; expression, it must be impossible for the corresponding
700 ;; close brace to be before point, but the open brace to be
702 (if (<= bufpos (car car))
704 ;; its possible that the open brace is before bufpos, but
705 ;; the close brace is after. In that case, convert this
706 ;; to a non-cons element.
707 (if (<= bufpos (cdr car))
708 (setq newstate (append newstate (list (car car))))
709 ;; we know that both the open and close braces are
710 ;; before bufpos, so we also know that everything else
711 ;; on state is before bufpos, so we can glom up the
712 ;; whole thing and exit.
713 (setq newstate (append newstate (list car) state)
717 ;; it's before bufpos, so everything else should too
718 (setq newstate (append newstate (list car) state)
722 (defun c-hack-state (bufpos which state)
723 ;; Using BUFPOS buffer position, and WHICH (must be 'open or
724 ;; 'close), hack the c-parse-state STATE and return the results.
726 (let ((car (car state)))
732 (if (not (eq which 'close))
733 (error "c-hack-state, bad argument: %s" which))
735 (let ((car (car state))
740 ;; TBD: is this test relevant???
742 state ;on error, don't change
743 ;; watch out for balanced expr already on cdr of list
744 (cons (cons car bufpos)
745 (if (consp (car cdr))
749 (defun c-adjust-state (from to shift state)
750 ;; Adjust all points in state that lie in the region FROM..TO by
758 (if (and (<= from car) (< car to))
759 (setcar e (+ shift car)))
760 (if (and (<= from cdr) (< cdr to))
761 (setcdr e (+ shift cdr))))
762 (if (and (<= from e) (< e to))
763 (setq e (+ shift e))))
768 (defun c-beginning-of-inheritance-list (&optional lim)
769 ;; Go to the first non-whitespace after the colon that starts a
770 ;; multiple inheritance introduction. Optional LIM is the farthest
771 ;; back we should search.
772 (let ((lim (or lim (c-point 'bod)))
774 (back-to-indentation)
776 (c-backward-syntactic-ws lim)
777 (while (and (> (point) lim)
778 (memq (char-before) '(?, ?:))
781 (setq placeholder (point))
782 (skip-chars-forward " \t")
783 (not (looking-at c-class-key))
785 (c-backward-syntactic-ws lim))
786 (goto-char placeholder)
787 (skip-chars-forward "^:" (c-point 'eol))))
789 (defun c-in-method-def-p ()
790 ;; Return nil if we aren't in a method definition, otherwise the
791 ;; position of the initial [+-].
795 (looking-at c-method-key)
799 (defun c-at-toplevel-p ()
800 "Return a determination as to whether point is at the `top-level'.
801 Being at the top-level means that point is either outside any
802 enclosing block (such function definition), or inside a class
803 definition, but outside any method blocks.
805 If point is not at the top-level (e.g. it is inside a method
806 definition), then nil is returned. Otherwise, if point is at a
807 top-level not enclosed within a class definition, t is returned.
808 Otherwise, a 2-vector is returned where the zeroth element is the
809 buffer position of the start of the class declaration, and the first
810 element is the buffer position of the enclosing class's opening
812 (let ((state (c-parse-state)))
813 (or (not (c-most-enclosing-brace state))
814 (c-search-uplist-for-classkey state))))
816 (defun c-just-after-func-arglist-p (&optional containing)
817 ;; Return t if we are between a function's argument list closing
818 ;; paren and its opening brace. Note that the list close brace
819 ;; could be followed by a "const" specifier or a member init hanging
820 ;; colon. Optional CONTAINING is position of containing s-exp open
821 ;; brace. If not supplied, point is used as search start.
823 (c-backward-syntactic-ws)
824 (let ((checkpoint (or containing (point))))
825 (goto-char checkpoint)
826 ;; could be looking at const specifier
827 (if (and (eq (char-before) ?t)
829 (looking-at "\\<const\\>"))
830 (c-backward-syntactic-ws)
831 ;; otherwise, we could be looking at a hanging member init
833 (goto-char checkpoint)
834 (if (and (eq (char-before) ?:)
837 (c-backward-syntactic-ws)
838 (looking-at "[ \t\n]*:\\([^:]+\\|$\\)")))
840 (goto-char checkpoint))
842 (and (eq (char-before) ?\))
843 ;; check if we are looking at a method def
844 (or (not c-method-key)
848 (c-backward-syntactic-ws)
849 (not (or (memq (char-before) '(?- ?+))
850 ;; or a class category
853 (looking-at c-class-key))
857 ;; defuns to look backwards for things
858 (defun c-backward-to-start-of-do (&optional lim)
859 ;; Move to the start of the last "unbalanced" do expression.
860 ;; Optional LIM is the farthest back to search. If none is found,
861 ;; nil is returned and point is left unchanged, otherwise t is returned.
863 (case-fold-search nil)
864 (lim (or lim (c-point 'bod)))
867 (while (not (zerop do-level))
868 ;; we protect this call because trying to execute this when the
869 ;; while is not associated with a do will throw an error
874 ;; break infloop for illegal C code
875 ((bobp) (setq do-level 0))
876 ((memq (c-in-literal lim) '(c c++)))
877 ((looking-at "while\\b[^_]")
878 (setq do-level (1+ do-level)))
879 ((looking-at "do\\b[^_]")
880 (if (zerop (setq do-level (1- do-level)))
892 (defun c-backward-to-start-of-if (&optional lim)
893 ;; Move to the start of the last "unbalanced" if and return t. If
894 ;; none is found, and we are looking at an if clause, nil is
895 ;; returned. If none is found and we are looking at an else clause,
896 ;; an error is thrown.
898 (here (c-point 'bol))
899 (case-fold-search nil)
900 (lim (or (and (>= (point) lim)
903 (at-if (looking-at "if\\b[^_]")))
905 (while (and (not (bobp))
906 (not (zerop if-level)))
907 (c-backward-syntactic-ws)
912 (throw 'orphan-if nil)
913 (error "No matching `if' found for `else' on line %d."
914 (1+ (count-lines 1 here))))))
916 ((looking-at "else\\b[^_]")
917 (setq if-level (1+ if-level)))
918 ((looking-at "if\\b[^_]")
919 ;; check for else if... skip over
920 (let ((here (point)))
921 (c-safe (c-forward-sexp -1))
922 (if (looking-at "\\<else\\>[ \t]+\\<if\\>")
924 (setq if-level (1- if-level))
932 (defun c-skip-conditional ()
933 ;; skip forward over conditional at point, including any predicate
934 ;; statements in parentheses. No error checking is performed.
935 (c-forward-sexp (cond
937 ((looking-at "\\<else\\>[ \t]+\\<if\\>") 3)
938 ;; do, else, try, finally
939 ((looking-at "\\<\\(do\\|else\\|try\\|finally\\)\\>") 1)
940 ;; for, if, while, switch, catch, synchronized
943 (defun c-skip-case-statement-forward (state &optional lim)
944 ;; skip forward over case/default bodies, with optional maximal
945 ;; limit. if no next case body is found, nil is returned and point
947 (let ((lim (or lim (point-max)))
951 (balanced (car state)))
952 ;; search until we've passed the limit, or we've found our match
953 (while (and (< (point) lim)
955 (setq safepos (point))
956 ;; see if we can find a case statement, not in a literal
957 (if (and (re-search-forward c-switch-label-key lim 'move)
958 (setq bufpos (match-beginning 0))
959 (not (c-in-literal safepos))
961 ;; if we crossed into a balanced sexp, we know the case is
962 ;; not part of our switch statement, so just bound over the
963 ;; sexp and keep looking.
964 (if (and (consp balanced)
965 (> bufpos (car balanced))
966 (< bufpos (cdr balanced)))
967 (goto-char (cdr balanced))
975 (defun c-search-uplist-for-classkey (brace-state)
976 ;; search for the containing class, returning a 2 element vector if
977 ;; found. aref 0 contains the bufpos of the boi of the class key
978 ;; line, and aref 1 contains the bufpos of the open brace.
979 (if (null brace-state)
980 ;; no brace-state means we cannot be inside a class
982 (let ((carcache (car brace-state))
983 search-start search-end)
985 ;; a cons cell in the first element means that there is some
986 ;; balanced sexp before the current bufpos. this we can
987 ;; ignore. the nth 1 and nth 2 elements define for us the
989 (setq search-start (nth 2 brace-state)
990 search-end (nth 1 brace-state))
991 ;; if the car was not a cons cell then nth 0 and nth 1 define
992 ;; for us the search boundaries
993 (setq search-start (nth 1 brace-state)
994 search-end (nth 0 brace-state)))
995 ;; search-end cannot be a cons cell
996 (and (consp search-end)
997 (error "consp search-end: %s" search-end))
998 ;; if search-end is nil, or if the search-end character isn't an
999 ;; open brace, we are definitely not in a class
1000 (if (or (not search-end)
1001 (< search-end (point-min))
1002 (not (eq (char-after search-end) ?{)))
1004 ;; now, we need to look more closely at search-start. if
1005 ;; search-start is nil, then our start boundary is really
1007 (if (not search-start)
1008 (setq search-start (point-min))
1009 ;; if search-start is a cons cell, then we can start
1010 ;; searching from the end of the balanced sexp just ahead of
1012 (if (consp search-start)
1013 (setq search-start (cdr search-start))))
1014 ;; now we can do a quick regexp search from search-start to
1015 ;; search-end and see if we can find a class key. watch for
1016 ;; class like strings in literals
1019 (goto-char search-start)
1020 (let ((search-key (concat c-class-key "\\|" c-extra-toplevel-key))
1021 foundp class match-end)
1022 (if c-inexpr-class-key
1023 (setq search-key (concat search-key "\\|"
1024 c-inexpr-class-key)))
1025 (while (and (not foundp)
1027 (c-forward-syntactic-ws)
1028 (> search-end (point)))
1029 (re-search-forward search-key search-end t))
1030 (setq class (match-beginning 0)
1031 match-end (match-end 0))
1032 (if (c-in-literal search-start)
1033 nil ; its in a comment or string, ignore
1035 (skip-chars-forward " \t\n")
1036 (setq foundp (vector (c-point 'boi) search-end))
1038 ;; check for embedded keywords
1039 ((let ((char (char-after (1- class))))
1041 (memq (char-syntax char) '(?w ?_))))
1042 (goto-char match-end)
1044 ;; make sure we're really looking at the start of a
1045 ;; class definition, and not a forward decl, return
1046 ;; arg, template arg list, or an ObjC or Java method.
1048 (re-search-forward c-method-key search-end t)
1049 (not (c-in-literal class)))
1051 ;; Check if this is an anonymous inner class.
1052 ((and c-inexpr-class-key
1053 (looking-at c-inexpr-class-key))
1054 (while (and (= (c-forward-token-1 1 t) 0)
1055 (looking-at "(\\|\\w\\|\\s_\\|\\.")))
1056 (if (eq (point) search-end)
1057 ;; We're done. Just trap this case in the cond.
1059 ;; False alarm; all conditions aren't satisfied.
1061 ;; Its impossible to define a regexp for this, and
1062 ;; nearly so to do it programmatically.
1064 ;; ; picks up forward decls
1065 ;; = picks up init lists
1066 ;; ) picks up return types
1067 ;; > picks up templates, but remember that we can
1068 ;; inherit from templates!
1069 ((let ((skipchars "^;=)"))
1070 ;; try to see if we found the `class' keyword
1071 ;; inside a template arg list
1073 (skip-chars-backward "^<>" search-start)
1074 (if (eq (char-before) ?<)
1075 (setq skipchars (concat skipchars ">"))))
1077 (skip-chars-forward skipchars search-end)
1078 (c-in-literal class))
1080 (/= (point) search-end))
1086 (defun c-inside-bracelist-p (containing-sexp brace-state)
1087 ;; return the buffer position of the beginning of the brace list
1088 ;; statement if we're inside a brace list, otherwise return nil.
1089 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
1090 ;; paren. BRACE-STATE is the remainder of the state of enclosing braces
1092 ;; N.B.: This algorithm can potentially get confused by cpp macros
1093 ;; places in inconvenient locations. Its a trade-off we make for
1096 ;; this will pick up enum lists
1099 (goto-char containing-sexp)
1102 (if (and (or (looking-at "enum[\t\n ]+")
1103 (progn (c-forward-sexp -1)
1104 (looking-at "enum[\t\n ]+")))
1105 (setq bracepos (c-safe (scan-lists (point) 1 -1)))
1106 (not (c-crosses-statement-barrier-p (point)
1109 ;; this will pick up array/aggregate init lists, even if they are nested.
1112 ;; Pike can have class definitions anywhere, so we must
1113 ;; check for the class key here.
1114 (and (c-major-mode-is 'pike-mode)
1115 (concat c-class-key "\\|" c-extra-toplevel-key)))
1116 bufpos lim braceassignp)
1117 (while (and (not bufpos)
1119 (if (consp containing-sexp)
1120 (setq containing-sexp (car brace-state)
1121 brace-state (cdr brace-state))
1122 (goto-char containing-sexp)
1123 (if (c-looking-at-inexpr-block)
1124 ;; We're in an in-expression block of some kind. Do
1125 ;; not check nesting.
1126 (setq containing-sexp nil)
1127 ;; see if the open brace is preceded by = or [...] in
1128 ;; this statement, but watch out for operator=
1129 (setq lim (if (consp (car brace-state))
1130 (cdr (car brace-state))
1132 braceassignp 'dontknow)
1133 (c-backward-token-1 1 t lim)
1134 ;; Checks to do only on the first sexp before the brace.
1135 (when (and (c-major-mode-is 'java-mode)
1136 (eq (char-after) ?\[))
1137 ;; In Java, an initialization brace list may follow
1138 ;; directly after "new Foo[]", so check for a "new"
1140 (while (eq braceassignp 'dontknow)
1142 (cond ((/= (c-backward-token-1 1 t lim) 0) nil)
1143 ((looking-at "new\\>") t)
1144 ((looking-at "\\sw\\|\\s_\\|[.[]")
1145 ;; Carry on looking if this is an
1146 ;; identifier (may contain "." in Java)
1147 ;; or another "[]" sexp.
1150 ;; Checks to do on all sexps before the brace, up to the
1151 ;; beginning of the statement.
1152 (while (eq braceassignp 'dontknow)
1153 (cond ((eq (char-after) ?\;)
1154 (setq braceassignp nil))
1156 (looking-at class-key))
1157 (setq braceassignp nil))
1158 ((eq (char-after) ?=)
1159 ;; We've seen a =, but must check earlier tokens so
1160 ;; that it isn't something that should be ignored.
1161 (setq braceassignp 'maybe)
1162 (while (and (eq braceassignp 'maybe)
1163 (zerop (c-backward-token-1 1 t lim)))
1166 ;; Check for operator =
1167 ((looking-at "operator\\>") nil)
1168 ;; Check for `<opchar>= (Pike)
1169 ((eq (char-after) ?`) nil)
1170 ((looking-at "\\s.") 'maybe)
1171 ;; make sure we're not in a C++ template
1172 ;; argument assignment
1174 (let ((here (point))
1176 (skip-chars-backward "^<")
1178 (and (c-major-mode-is 'c++-mode)
1179 (eq (char-before) ?<)
1180 (not (c-crosses-statement-barrier-p
1182 (not (c-in-literal))
1186 (if (and (eq braceassignp 'dontknow)
1187 (/= (c-backward-token-1 1 t lim) 0))
1188 (setq braceassignp nil)))
1189 (if (not braceassignp)
1190 (if (eq (char-after) ?\;)
1191 ;; Brace lists can't contain a semicolon, so we're done.
1192 (setq containing-sexp nil)
1193 ;; lets see if we're nested. find the most nested
1195 (setq containing-sexp (car brace-state)
1196 brace-state (cdr brace-state)))
1197 ;; we've hit the beginning of the aggregate list
1198 (c-beginning-of-statement-1
1199 (c-most-enclosing-brace brace-state))
1200 (setq bufpos (point))))
1205 (defun c-looking-at-special-brace-list (&optional lim)
1206 ;; If we're looking at the start of a pike-style list, ie `({Â })',
1207 ;; `([Â ])', `(<Â >)' etc, a cons of a cons its starting and ending
1208 ;; positions and its entry in c-special-brace-lists is returned, nil
1209 ;; otherwise. The ending position is nil if the list is still open.
1210 ;; LIM is the limit for forward search. The point may either be at
1211 ;; the `(' or at the following paren character. Tries to check the
1212 ;; matching closer, but assumes it's correct if no balanced paren is
1213 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
1214 ;; a special brace list).
1215 (if c-special-brace-lists
1220 (c-forward-syntactic-ws)
1221 (if (eq (char-after) ?\()
1224 (c-forward-syntactic-ws)
1225 (setq type (assq (char-after) c-special-brace-lists)))
1226 (if (setq type (assq (char-after) c-special-brace-lists))
1228 (c-backward-syntactic-ws)
1230 (setq beg (if (eq (char-after) ?\()
1234 (if (and (c-safe (goto-char beg)
1237 (= (char-before) ?\)))
1238 (c-safe (goto-char beg)
1241 ;; Kludges needed to handle inner
1242 ;; chars both with and without
1244 (or (/= (char-syntax (char-before)) ?\))
1245 (= (char-before) (cdr type)))))
1246 (if (or (/= (char-syntax (char-before)) ?\))
1248 (c-forward-syntactic-ws)
1251 (cons (cons beg end) type))
1252 (cons (list beg) type)))))
1255 (defun c-looking-at-inexpr-block (&optional lim)
1256 ;; Returns non-nil if we're looking at the beginning of a block
1257 ;; inside an expression. The value returned is actually a cons of
1258 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
1259 ;; position of the beginning of the construct. LIM limits the
1262 (or lim (setq lim (point-min)))
1263 (let ((block-follows (eq (char-after) ?{)))
1264 ;; Look at the character after point only as a last resort when
1265 ;; we can't disambiguate.
1266 (if (and block-follows
1267 (progn (c-backward-syntactic-ws) (> (point) lim))
1268 (eq (char-before) ?\()
1269 (not (and c-special-brace-lists
1270 (c-looking-at-special-brace-list))))
1271 (cons 'inexpr-statement (point))
1273 (while (and (not res)
1274 (= (c-backward-token-1 1 t lim) 0)
1276 (looking-at "(\\|\\w\\|\\s_\\|\\."))
1278 (cond ((and block-follows
1280 (looking-at c-inexpr-class-key))
1281 (cons 'inexpr-class (point)))
1282 ((and c-inexpr-block-key
1283 (looking-at c-inexpr-block-key))
1284 (cons 'inexpr-statement (point)))
1286 (looking-at c-lambda-key))
1287 (cons 'inlambda (point))))))
1290 (defun c-looking-at-inexpr-block-backward (&optional lim)
1291 ;; Returns non-nil if we're looking at the end of an in-expression
1292 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
1294 (let ((lim (or lim (c-point 'bod))))
1296 (c-backward-syntactic-ws lim)
1297 (if (eq (char-before) ?}) ; Recognize only a block currently.
1300 (if (>= (point) lim)
1301 (c-looking-at-inexpr-block lim))))))))
1304 (defun c-most-enclosing-brace (state)
1305 ;; return the bufpos of the most enclosing brace that hasn't been
1306 ;; narrowed out by any enclosing class, or nil if none was found
1308 (while (and state (not enclosingp))
1309 (setq enclosingp (car state)
1311 (if (consp enclosingp)
1312 (setq enclosingp nil)
1313 (if (> (point-min) enclosingp)
1314 (setq enclosingp nil))
1318 (defun c-least-enclosing-brace (state)
1319 ;; return the bufpos of the least (highest) enclosing brace that
1320 ;; hasn't been narrowed out by any enclosing class, or nil if none
1322 (c-most-enclosing-brace (nreverse state)))
1324 (defun c-safe-position (bufpos state)
1325 ;; return the closest known safe position higher up than point
1326 (let ((safepos nil))
1329 (if (consp (car state))
1332 (if (< safepos bufpos)
1334 (setq state (cdr state))))
1337 (defun c-narrow-out-enclosing-class (state lim)
1338 ;; narrow the buffer so that the enclosing class is hidden
1341 (setq inclass-p (c-search-uplist-for-classkey state))
1344 (goto-char (1+ (aref inclass-p 1)))
1345 (skip-chars-forward " \t\n" lim)
1346 ;; if point is now left of the class opening brace, we're
1347 ;; hosed, so try a different tact
1348 (if (<= (point) (aref inclass-p 1))
1350 (goto-char (1+ (aref inclass-p 1)))
1351 (c-forward-syntactic-ws lim)))
1353 ;; end point is the end of the current line
1357 ;; return the class vector
1361 ;; This function implements the main decision tree for determining the
1362 ;; syntactic analysis of the current line of code. Yes, it's huge and
1365 (defun c-guess-basic-syntax ()
1369 (let* ((indent-point (point))
1370 (case-fold-search nil)
1371 (fullstate (c-parse-state))
1373 (in-method-intro-p (and (c-major-mode-is 'objc-mode)
1375 (looking-at c-method-key)))
1376 literal containing-sexp char-before-ip char-after-ip lim
1377 syntax placeholder c-in-literal-cache inswitch-p
1378 tmpsymbol keyword injava-inher special-brace-list
1379 ;; narrow out any enclosing class or extern "C" block
1380 (inclass-p (c-narrow-out-enclosing-class state indent-point))
1382 ;; check for meta top-level enclosing constructs, possible
1383 ;; extern language definitions, possibly (in C++) namespace
1390 (goto-char (aref inclass-p 0))
1391 (looking-at (concat c-extra-toplevel-key "[^_]"))))
1392 (let ((enclosing (match-string 1)))
1394 ((string-equal enclosing "extern")
1395 (setq inenclosing-p 'extern))
1396 ((string-equal enclosing "namespace")
1397 (setq inenclosing-p 'namespace))
1399 ;; get the buffer position of the most nested opening brace,
1400 ;; if there is one, and it hasn't been narrowed out
1402 (goto-char indent-point)
1403 (skip-chars-forward " \t}")
1404 (skip-chars-backward " \t")
1406 (not in-method-intro-p)
1407 (not containing-sexp))
1408 (setq containing-sexp (car state)
1410 (if (consp containing-sexp)
1411 ;; if cdr == point, then containing sexp is the brace
1412 ;; that opens the sexp we close
1413 (if (= (cdr containing-sexp) (point))
1414 (setq containing-sexp (car containing-sexp))
1415 ;; otherwise, ignore this element
1416 (setq containing-sexp nil))
1417 ;; ignore the bufpos if its been narrowed out by the
1419 (if (<= containing-sexp (point-min))
1420 (setq containing-sexp nil)))))
1422 ;; set the limit on the farthest back we need to search
1423 (setq lim (or containing-sexp
1424 (if (consp (car fullstate))
1425 (cdr (car fullstate))
1429 ;; cache char before and after indent point, and move point to
1430 ;; the most likely position to perform the majority of tests
1431 (goto-char indent-point)
1432 (skip-chars-forward " \t")
1433 (setq char-after-ip (char-after))
1434 (c-backward-syntactic-ws lim)
1435 (setq char-before-ip (char-before))
1436 (goto-char indent-point)
1437 (skip-chars-forward " \t")
1439 ;; are we in a literal?
1440 (setq literal (c-in-literal lim))
1442 ;; now figure out syntactic qualities of the current line
1444 ;; CASE 1: in a string.
1445 ((memq literal '(string))
1446 (c-add-syntax 'string (c-point 'bopl)))
1447 ;; CASE 2: in a C or C++ style comment.
1448 ((memq literal '(c c++))
1449 (c-add-syntax literal (car (c-literal-limits lim))))
1450 ;; CASE 3: in a cpp preprocessor macro
1451 ((eq literal 'pound)
1452 (let ((boi (c-point 'boi))
1453 (macrostart (progn (c-beginning-of-macro lim) (point))))
1454 (setq tmpsymbol (if (= boi macrostart)
1457 (c-add-syntax tmpsymbol macrostart)))
1458 ;; CASE 4: in an objective-c method intro
1460 (c-add-syntax 'objc-method-intro (c-point 'boi)))
1461 ;; CASE 5: Line is at top level.
1462 ((null containing-sexp)
1464 ;; CASE 5A: we are looking at a defun, brace list, class,
1465 ;; or inline-inclass method opening brace
1466 ((setq special-brace-list
1467 (or (and c-special-brace-lists
1468 (c-looking-at-special-brace-list))
1469 (eq char-after-ip ?{)))
1471 ;; CASE 5A.1: extern language or namespace construct
1473 (goto-char indent-point)
1474 (skip-chars-forward " \t")
1475 (and (c-safe (progn (c-backward-sexp 2) t))
1476 (looking-at (concat c-extra-toplevel-key "[^_]"))
1477 (setq keyword (match-string 1)
1478 placeholder (point))
1479 (or (and (string-equal keyword "namespace")
1480 (setq tmpsymbol 'namespace-open))
1481 (and (string-equal keyword "extern")
1484 (c-forward-syntactic-ws)
1485 (eq (char-after) ?\"))
1486 (setq tmpsymbol 'extern-lang-open)))
1488 (goto-char placeholder)
1489 (c-add-syntax tmpsymbol (c-point 'boi)))
1490 ;; CASE 5A.2: we are looking at a class opening brace
1492 (goto-char indent-point)
1493 (skip-chars-forward " \t{")
1494 ;; TBD: watch out! there could be a bogus
1495 ;; c-state-cache in place when we get here. we have
1496 ;; to go through much chicanery to ignore the cache.
1497 ;; But of course, there may not be! BLECH! BOGUS!
1499 (if (boundp 'c-state-cache)
1500 (let ((old-cache c-state-cache))
1502 (makunbound 'c-state-cache)
1503 (c-search-uplist-for-classkey (c-parse-state))
1504 (setq c-state-cache old-cache)))
1505 (c-search-uplist-for-classkey (c-parse-state))
1508 (setq placeholder (aref decl 0)))
1510 (c-add-syntax 'class-open placeholder))
1511 ;; CASE 5A.3: brace list open
1513 (c-beginning-of-statement-1 lim)
1514 ;; c-b-o-s could have left us at point-min
1516 (c-forward-syntactic-ws indent-point))
1517 (if (looking-at "typedef[^_]")
1518 (progn (c-forward-sexp 1)
1519 (c-forward-syntactic-ws indent-point)))
1520 (setq placeholder (c-point 'boi))
1521 (or (consp special-brace-list)
1522 (and (or (looking-at "enum[ \t\n]+")
1524 (goto-char indent-point)
1525 (while (and (> (point) placeholder)
1526 (= (c-backward-token-1 1 t) 0)
1527 (/= (char-after) ?=)))
1528 (eq (char-after) ?=)))
1530 (while (and (< (point) indent-point)
1531 (= (c-forward-token-1 1 t) 0)
1532 (not (memq (char-after) '(?\; ?\()))))
1533 (not (memq (char-after) '(?\; ?\()))
1535 (c-add-syntax 'brace-list-open placeholder))
1536 ;; CASE 5A.4: inline defun open
1537 ((and inclass-p (not inenclosing-p))
1538 (c-add-syntax 'inline-open)
1539 (c-add-class-syntax 'inclass inclass-p))
1540 ;; CASE 5A.5: ordinary defun open
1542 (goto-char placeholder)
1544 (c-add-syntax 'defun-open (c-point 'boi))
1545 (c-add-syntax 'defun-open (c-point 'bol)))
1547 ;; CASE 5B: first K&R arg decl or member init
1548 ((c-just-after-func-arglist-p)
1550 ;; CASE 5B.1: a member init
1551 ((or (eq char-before-ip ?:)
1552 (eq char-after-ip ?:))
1553 ;; this line should be indented relative to the beginning
1554 ;; of indentation for the topmost-intro line that contains
1555 ;; the prototype's open paren
1556 ;; TBD: is the following redundant?
1557 (if (eq char-before-ip ?:)
1559 (c-backward-syntactic-ws lim)
1560 ;; TBD: is the preceding redundant?
1561 (if (eq (char-before) ?:)
1562 (progn (forward-char -1)
1563 (c-backward-syntactic-ws lim)))
1564 (if (eq (char-before) ?\))
1565 (c-backward-sexp 1))
1566 (setq placeholder (point))
1568 (and (c-safe (c-backward-sexp 1) t)
1569 (looking-at "throw[^_]")
1570 (c-safe (c-backward-sexp 1) t)
1571 (setq placeholder (point))))
1572 (goto-char placeholder)
1573 (c-add-syntax 'member-init-intro (c-point 'boi))
1574 ;; we don't need to add any class offset since this
1575 ;; should be relative to the ctor's indentation
1577 ;; CASE 5B.2: K&R arg decl intro
1579 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
1580 (if inclass-p (c-add-class-syntax 'inclass inclass-p)))
1581 ;; CASE 5B.3: Nether region after a C++ or Java func
1582 ;; decl, which could include a `throws' declaration.
1584 (c-beginning-of-statement-1 lim)
1585 (c-add-syntax 'func-decl-cont (c-point 'boi))
1587 ;; CASE 5C: inheritance line. could be first inheritance
1588 ;; line, or continuation of a multiple inheritance
1589 ((or (and c-baseclass-key (looking-at c-baseclass-key))
1590 (and (or (eq char-before-ip ?:)
1591 ;; watch out for scope operator
1593 (and (eq char-after-ip ?:)
1594 (c-safe (progn (forward-char 1) t))
1595 (not (eq (char-after) ?:))
1598 (c-backward-syntactic-ws lim)
1599 (if (eq char-before-ip ?:)
1602 (c-backward-syntactic-ws lim)))
1603 (back-to-indentation)
1604 (looking-at c-class-key)))
1606 (and (c-major-mode-is 'java-mode)
1607 (let ((fence (save-excursion
1608 (c-beginning-of-statement-1 lim)
1613 (cond ((looking-at c-Java-special-key)
1614 (setq injava-inher (cons cont (point))
1616 ((or (not (c-safe (c-forward-sexp -1) t))
1622 (not (c-crosses-statement-barrier-p (cdr injava-inher)
1626 ;; CASE 5C.1: non-hanging colon on an inher intro
1627 ((eq char-after-ip ?:)
1628 (c-backward-syntactic-ws lim)
1629 (c-add-syntax 'inher-intro (c-point 'boi))
1630 ;; don't add inclass symbol since relative point already
1631 ;; contains any class offset
1633 ;; CASE 5C.2: hanging colon on an inher intro
1634 ((eq char-before-ip ?:)
1635 (c-add-syntax 'inher-intro (c-point 'boi))
1636 (if inclass-p (c-add-class-syntax 'inclass inclass-p)))
1637 ;; CASE 5C.3: in a Java implements/extends
1639 (let ((where (cdr injava-inher))
1640 (cont (car injava-inher)))
1642 (cond ((looking-at "throws[ \t\n]")
1643 (c-add-syntax 'func-decl-cont
1644 (progn (c-beginning-of-statement-1 lim)
1646 (cont (c-add-syntax 'inher-cont where))
1647 (t (c-add-syntax 'inher-intro
1648 (progn (goto-char (cdr injava-inher))
1649 (c-beginning-of-statement-1 lim)
1652 ;; CASE 5C.4: a continued inheritance line
1654 (c-beginning-of-inheritance-list lim)
1655 (c-add-syntax 'inher-cont (point))
1656 ;; don't add inclass symbol since relative point already
1657 ;; contains any class offset
1659 ;; CASE 5D: this could be a top-level compound statement, a
1660 ;; member init list continuation, or a template argument
1661 ;; list continuation.
1662 ((c-with-syntax-table (if (c-major-mode-is 'c++-mode)
1663 c++-template-syntax-table
1666 (while (and (= (c-backward-token-1 1 t lim) 0)
1667 (not (looking-at "[;{<,]"))))
1668 (eq (char-after) ?,)))
1669 (goto-char indent-point)
1670 (c-backward-syntactic-ws lim)
1671 (while (and (< lim (point))
1672 (eq (char-before) ?,))
1673 ;; this will catch member inits with multiple
1676 (c-backward-syntactic-ws (c-point 'bol))
1677 (if (eq (char-before) ?\))
1679 (c-backward-sexp 1))
1680 ;; now continue checking
1681 (c-backward-syntactic-ws lim))
1683 ;; CASE 5D.1: hanging member init colon, but watch out
1684 ;; for bogus matches on access specifiers inside classes.
1685 ((and (save-excursion
1686 ;; There might be member inits on the first line too.
1687 (while (and (> (point) lim)
1688 (eq (char-before) ?,)
1689 (= (c-backward-token-1 2 t lim) 0)
1690 (eq (char-after) ?\()
1691 (= (c-backward-token-1 1 t lim) 0))
1692 (c-backward-syntactic-ws lim))
1693 (setq placeholder (point))
1694 (c-backward-token-1 1 t lim)
1695 (and (eq (char-after) ?:)
1696 (not (eq (char-before) ?:))))
1698 (goto-char placeholder)
1699 (back-to-indentation)
1701 (if c-access-key (not (looking-at c-access-key)) t)
1702 (not (looking-at c-class-key))
1703 (if c-bitfield-key (not (looking-at c-bitfield-key)) t))
1705 (goto-char placeholder)
1706 (c-forward-syntactic-ws)
1707 (c-add-syntax 'member-init-cont (point))
1708 ;; we do not need to add class offset since relative
1709 ;; point is the member init above us
1711 ;; CASE 5D.2: non-hanging member init colon
1713 (c-forward-syntactic-ws indent-point)
1714 (eq (char-after) ?:))
1715 (skip-chars-forward " \t:")
1716 (c-add-syntax 'member-init-cont (point)))
1717 ;; CASE 5D.3: perhaps a multiple inheritance line?
1719 (c-beginning-of-statement-1 lim)
1720 (setq placeholder (point))
1721 (looking-at c-inher-key))
1722 (goto-char placeholder)
1723 (c-add-syntax 'inher-cont (c-point 'boi)))
1724 ;; CASE 5D.4: perhaps a template list continuation?
1726 (goto-char indent-point)
1727 (skip-chars-backward "^<" lim)
1728 ;; not sure if this is the right test, but it should
1729 ;; be fast and mostly accurate.
1730 (setq placeholder (point))
1731 (and (eq (char-before) ?<)
1732 (not (c-in-literal lim))))
1733 ;; we can probably indent it just like an arglist-cont
1734 (goto-char placeholder)
1735 (c-beginning-of-statement-1 lim)
1736 (c-add-syntax 'template-args-cont (c-point 'boi)))
1737 ;; CASE 5D.5: perhaps a top-level statement-cont
1739 (c-beginning-of-statement-1 lim)
1740 ;; skip over any access-specifiers
1741 (and inclass-p c-access-key
1742 (while (looking-at c-access-key)
1744 ;; skip over comments, whitespace
1745 (c-forward-syntactic-ws indent-point)
1746 (c-add-syntax 'statement-cont (c-point 'boi)))
1748 ;; CASE 5E: we are looking at a access specifier
1751 (looking-at c-access-key))
1752 (c-add-syntax 'access-label (c-point 'bonl))
1753 (c-add-class-syntax 'inclass inclass-p))
1754 ;; CASE 5F: extern-lang-close or namespace-close?
1756 (eq char-after-ip ?}))
1757 (setq tmpsymbol (if (eq inenclosing-p 'extern)
1760 (c-add-syntax tmpsymbol (aref inclass-p 0)))
1761 ;; CASE 5G: we are looking at the brace which closes the
1762 ;; enclosing nested class decl
1764 (eq char-after-ip ?})
1769 (and (c-safe (progn (c-backward-sexp 1) t))
1770 (= (point) (aref inclass-p 1))
1772 (c-add-class-syntax 'class-close inclass-p))
1773 ;; CASE 5H: we could be looking at subsequent knr-argdecls
1774 ((and c-recognize-knr-p
1775 ;; here we essentially use the hack that is used in
1776 ;; Emacs' c-mode.el to limit how far back we should
1777 ;; look. The assumption is made that argdecls are
1778 ;; indented at least one space and that function
1779 ;; headers are not indented.
1780 (let ((limit (save-excursion
1781 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
1784 (c-backward-syntactic-ws limit)
1785 (setq placeholder (point))
1786 (while (and (memq (char-before) '(?\; ?,))
1789 (setq placeholder (point))
1790 (c-backward-syntactic-ws limit))
1791 (and (eq (char-before) ?\))
1792 (or (not c-method-key)
1796 (c-backward-syntactic-ws)
1797 (not (or (memq (char-before) '(?- ?+))
1798 ;; or a class category
1801 (looking-at c-class-key))
1805 (c-beginning-of-statement-1)
1806 (not (looking-at "typedef[ \t\n]+"))))
1807 (goto-char placeholder)
1808 (c-add-syntax 'knr-argdecl (c-point 'boi)))
1809 ;; CASE 5I: we are at the topmost level, make sure we skip
1810 ;; back past any access specifiers
1812 (c-backward-syntactic-ws lim)
1813 (while (and inclass-p
1817 (c-safe (progn (c-backward-sexp 1) t))
1818 (looking-at c-access-key)))
1820 (c-backward-syntactic-ws lim))
1822 (memq (char-before) '(?\; ?\}))))
1823 ;; real beginning-of-line could be narrowed out due to
1824 ;; enclosure in a class block
1827 (c-add-syntax 'topmost-intro (c-point 'bol))
1830 (goto-char (aref inclass-p 1))
1831 (or (= (point) (c-point 'boi))
1832 (goto-char (aref inclass-p 0)))
1834 ((eq inenclosing-p 'extern)
1835 (c-add-syntax 'inextern-lang (c-point 'boi)))
1836 ((eq inenclosing-p 'namespace)
1837 (c-add-syntax 'innamespace (c-point 'boi)))
1838 (t (c-add-class-syntax 'inclass inclass-p)))
1841 ;; CASE 5J: we are at an ObjC or Java method definition
1842 ;; continuation line.
1845 (c-beginning-of-statement-1 lim)
1847 (looking-at c-method-key)))
1848 (c-add-syntax 'objc-method-args-cont (point)))
1849 ;; CASE 5K: we are at the first argument of a template
1850 ;; arglist that begins on the previous line.
1851 ((eq (char-before) ?<)
1852 (c-beginning-of-statement-1 lim)
1853 (c-forward-syntactic-ws)
1854 (c-add-syntax 'template-args-cont (c-point 'boi)))
1855 ;; CASE 5L: we are at a topmost continuation line
1857 (c-beginning-of-statement-1 lim)
1858 (c-forward-syntactic-ws)
1859 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
1861 ;; CASE 6: In-expression statement.
1862 ((and (or c-inexpr-class-key c-inexpr-block-key c-lambda-key)
1863 (setq placeholder (c-looking-at-inexpr-block)))
1864 (setq tmpsymbol (assq (car placeholder)
1865 '((inexpr-class . class-open)
1866 (inexpr-statement . block-open))))
1868 ;; It's a statement block or an anonymous class.
1869 (setq tmpsymbol (cdr tmpsymbol))
1870 ;; It's a Pike lambda. Check whether we are between the
1871 ;; lambda keyword and the argument list or at the defun
1873 (setq tmpsymbol (if (eq char-after-ip ?{)
1875 'lambda-intro-cont)))
1876 (goto-char (cdr placeholder))
1877 (c-add-syntax tmpsymbol (c-point 'boi))
1878 (c-add-syntax (car placeholder)))
1879 ;; CASE 7: line is an expression, not a statement. Most
1880 ;; likely we are either in a function prototype or a function
1881 ;; call argument list
1882 ((not (or (and c-special-brace-lists
1884 (goto-char containing-sexp)
1885 (c-looking-at-special-brace-list)))
1886 (eq (char-after containing-sexp) ?{)))
1887 (c-backward-syntactic-ws containing-sexp)
1889 ;; CASE 7A: we are looking at the arglist closing paren
1890 ((and (or (c-major-mode-is 'pike-mode)
1891 ;; Don't check this in Pike since it allows a
1892 ;; comma after the last arg.
1893 (not (eq char-before-ip ?,)))
1894 (memq char-after-ip '(?\) ?\])))
1895 (goto-char containing-sexp)
1896 (c-add-syntax 'arglist-close (c-point 'boi)))
1897 ;; CASE 7B: Looking at the opening brace of an
1898 ;; in-expression block or brace list.
1899 ((eq char-after-ip ?{)
1900 (goto-char indent-point)
1901 (setq placeholder (c-point 'boi))
1902 (goto-char containing-sexp)
1903 (if (c-inside-bracelist-p placeholder
1904 (cons containing-sexp state))
1906 (c-add-syntax 'brace-list-open (c-point 'boi))
1907 (c-add-syntax 'inexpr-class))
1908 (c-add-syntax 'block-open (c-point 'boi))
1909 (c-add-syntax 'inexpr-statement)))
1910 ;; CASE 7C: we are looking at the first argument in an empty
1911 ;; argument list. Use arglist-close if we're actually
1912 ;; looking at a close paren or bracket.
1913 ((memq char-before-ip '(?\( ?\[))
1914 (goto-char containing-sexp)
1915 (c-add-syntax 'arglist-intro (c-point 'boi)))
1916 ;; CASE 7D: we are inside a conditional test clause. treat
1917 ;; these things as statements
1919 (goto-char containing-sexp)
1920 (and (c-safe (progn (c-forward-sexp -1) t))
1921 (looking-at "\\<for\\>[^_]")))
1922 (goto-char (1+ containing-sexp))
1923 (c-forward-syntactic-ws indent-point)
1924 (c-beginning-of-statement-1 containing-sexp)
1925 (if (eq char-before-ip ?\;)
1926 (c-add-syntax 'statement (point))
1927 (c-add-syntax 'statement-cont (point))
1929 ;; CASE 7E: maybe a continued method call. This is the case
1930 ;; when we are inside a [] bracketed exp, and what precede
1931 ;; the opening bracket is not an identifier.
1933 (eq (char-after containing-sexp) ?\[)
1935 (goto-char (1- containing-sexp))
1936 (c-backward-syntactic-ws (c-point 'bod))
1937 (if (not (looking-at c-symbol-key))
1938 (c-add-syntax 'objc-method-call-cont containing-sexp))
1940 ;; CASE 7F: we are looking at an arglist continuation line,
1941 ;; but the preceding argument is on the same line as the
1942 ;; opening paren. This case includes multi-line
1943 ;; mathematical paren groupings, but we could be on a
1944 ;; for-list continuation line
1945 ((and (save-excursion
1946 (goto-char (1+ containing-sexp))
1947 (skip-chars-forward " \t")
1950 (c-beginning-of-statement-1 lim)
1951 (skip-chars-backward " \t([")
1952 (<= (point) containing-sexp)))
1953 (goto-char containing-sexp)
1954 (c-add-syntax 'arglist-cont-nonempty (c-point 'boi)))
1955 ;; CASE 7G: we are looking at just a normal arglist
1956 ;; continuation line
1957 (t (c-beginning-of-statement-1 containing-sexp)
1959 (c-forward-syntactic-ws indent-point)
1960 (c-add-syntax 'arglist-cont (c-point 'boi)))
1962 ;; CASE 8: func-local multi-inheritance line
1963 ((and c-baseclass-key
1965 (goto-char indent-point)
1966 (skip-chars-forward " \t")
1967 (looking-at c-baseclass-key)))
1968 (goto-char indent-point)
1969 (skip-chars-forward " \t")
1971 ;; CASE 8A: non-hanging colon on an inher intro
1972 ((eq char-after-ip ?:)
1973 (c-backward-syntactic-ws lim)
1974 (c-add-syntax 'inher-intro (c-point 'boi)))
1975 ;; CASE 8B: hanging colon on an inher intro
1976 ((eq char-before-ip ?:)
1977 (c-add-syntax 'inher-intro (c-point 'boi)))
1978 ;; CASE 8C: a continued inheritance line
1980 (c-beginning-of-inheritance-list lim)
1981 (c-add-syntax 'inher-cont (point))
1983 ;; CASE 9: we are inside a brace-list
1984 ((setq special-brace-list
1985 (or (and c-special-brace-lists
1987 (goto-char containing-sexp)
1988 (c-looking-at-special-brace-list)))
1989 (c-inside-bracelist-p containing-sexp state)))
1991 ;; CASE 9A: In the middle of a special brace list opener.
1992 ((and (consp special-brace-list)
1993 (eq char-after-ip (car (cdr special-brace-list))))
1994 (goto-char (car (car special-brace-list)))
1995 (skip-chars-backward " \t")
1997 (setq syntax (c-guess-basic-syntax))
1998 (c-beginning-of-statement-1 lim)
1999 (c-forward-token-1 0)
2000 (if (looking-at "typedef\\>") (c-forward-token-1 1))
2001 (c-add-syntax 'brace-list-open (c-point 'boi))))
2002 ;; CASE 9B: brace-list-close brace
2003 ((if (consp special-brace-list)
2004 ;; Check special brace list closer.
2006 (goto-char (car (car special-brace-list)))
2008 (goto-char indent-point)
2009 (back-to-indentation)
2011 ;; We were between the special close char and the `)'.
2012 (and (eq (char-after) ?\))
2013 (eq (1+ (point)) (cdr (car special-brace-list))))
2014 ;; We were before the special close char.
2015 (and (eq (char-after) (cdr (cdr special-brace-list)))
2016 (= (c-forward-token-1) 0)
2017 (eq (1+ (point)) (cdr (car special-brace-list)))))))
2018 ;; Normal brace list check.
2019 (and (eq char-after-ip ?})
2020 (c-safe (progn (forward-char 1)
2023 (= (point) containing-sexp)))
2024 (c-add-syntax 'brace-list-close (c-point 'boi)))
2026 ;; Prepare for the rest of the cases below by going to the
2027 ;; token following the opening brace
2028 (if (consp special-brace-list)
2030 (goto-char (car (car special-brace-list)))
2031 (c-forward-token-1 1 nil indent-point))
2032 (goto-char containing-sexp))
2034 (let ((start (point)))
2035 (c-forward-syntactic-ws indent-point)
2036 (goto-char (max start (c-point 'bol))))
2037 (skip-chars-forward " \t\n\r" indent-point)
2039 ;; CASE 9C: we're looking at the first line in a brace-list
2040 ((= (point) indent-point)
2041 (goto-char containing-sexp)
2042 (c-add-syntax 'brace-list-intro (c-point 'boi))
2044 ;; CASE 9D: this is just a later brace-list-entry or
2046 (t (if (or (eq char-after-ip ?{)
2047 (and c-special-brace-lists
2049 (goto-char indent-point)
2050 (c-forward-syntactic-ws (c-point 'eol))
2051 (c-looking-at-special-brace-list (point)))))
2052 (c-add-syntax 'brace-entry-open (point))
2053 (c-add-syntax 'brace-list-entry (point))
2056 ;; CASE 10: A continued statement
2057 ((and (not (memq char-before-ip '(?\; ?:)))
2058 (or (not (eq char-before-ip ?}))
2059 (c-looking-at-inexpr-block-backward containing-sexp))
2062 (c-beginning-of-statement-1 containing-sexp)
2063 (c-forward-syntactic-ws)
2064 (setq placeholder (point))))
2065 (/= placeholder containing-sexp))
2066 (goto-char indent-point)
2067 (skip-chars-forward " \t")
2068 (let ((after-cond-placeholder
2070 (goto-char placeholder)
2071 (if (and c-conditional-key (looking-at c-conditional-key))
2073 (c-safe (c-skip-conditional))
2074 (c-forward-syntactic-ws)
2075 (if (eq (char-after) ?\;)
2078 (c-forward-syntactic-ws)))
2082 ;; CASE 10A: substatement
2083 ((and after-cond-placeholder
2084 (>= after-cond-placeholder indent-point))
2085 (goto-char placeholder)
2086 (if (eq char-after-ip ?{)
2087 (c-add-syntax 'substatement-open (c-point 'boi))
2088 (c-add-syntax 'substatement (c-point 'boi))))
2089 ;; CASE 10B: open braces for class or brace-lists
2090 ((setq special-brace-list
2091 (or (and c-special-brace-lists
2092 (c-looking-at-special-brace-list))
2093 (eq char-after-ip ?{)))
2095 ;; CASE 10B.1: class-open
2097 (goto-char indent-point)
2098 (skip-chars-forward " \t{")
2099 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
2101 (setq placeholder (aref decl 0)))
2103 (c-add-syntax 'class-open placeholder))
2104 ;; CASE 10B.2: brace-list-open
2105 ((or (consp special-brace-list)
2107 (goto-char placeholder)
2108 (looking-at "\\<enum\\>"))
2110 (goto-char indent-point)
2111 (while (and (> (point) placeholder)
2112 (= (c-backward-token-1 1 t) 0)
2113 (/= (char-after) ?=)))
2114 (eq (char-after) ?=)))
2115 ;; The most semantically accurate symbol here is
2116 ;; brace-list-open, but we report it simply as a
2117 ;; statement-cont. The reason is that one normally
2118 ;; adjusts brace-list-open for brace lists as
2119 ;; top-level constructs, and brace lists inside
2120 ;; statements is a completely different context.
2121 (goto-char placeholder)
2122 (c-add-syntax 'statement-cont (c-point 'boi)))
2123 ;; CASE 10B.3: catch-all for unknown construct.
2125 ;; Can and should I add an extensibility hook here?
2126 ;; Something like c-recognize-hook so support for
2127 ;; unknown constructs could be added. It's probably a
2128 ;; losing proposition, so I dunno.
2129 (goto-char placeholder)
2130 (c-add-syntax 'statement-cont (c-point 'boi))
2131 (c-add-syntax 'block-open))
2133 ;; CASE 10C: iostream insertion or extraction operator
2134 ((looking-at "<<\\|>>")
2135 (goto-char placeholder)
2136 (and after-cond-placeholder
2137 (goto-char after-cond-placeholder))
2138 (while (and (re-search-forward "<<\\|>>" indent-point 'move)
2139 (c-in-literal placeholder)))
2140 ;; if we ended up at indent-point, then the first
2141 ;; streamop is on a separate line. Indent the line like
2142 ;; a statement-cont instead
2143 (if (/= (point) indent-point)
2144 (c-add-syntax 'stream-op (c-point 'boi))
2145 (c-backward-syntactic-ws lim)
2146 (c-add-syntax 'statement-cont (c-point 'boi))))
2147 ;; CASE 10D: continued statement. find the accurate
2148 ;; beginning of statement or substatement
2150 (c-beginning-of-statement-1 after-cond-placeholder)
2151 ;; KLUDGE ALERT! c-beginning-of-statement-1 can leave
2152 ;; us before the lim we're passing in. It should be
2153 ;; fixed, but I'm worried about side-effects at this
2154 ;; late date. Fix for v5.
2155 (goto-char (or (and after-cond-placeholder
2156 (max after-cond-placeholder (point)))
2158 (c-add-syntax 'statement-cont (point)))
2160 ;; CASE 11: an else clause?
2161 ((looking-at "\\<else\\>[^_]")
2162 (c-backward-to-start-of-if containing-sexp)
2163 (c-add-syntax 'else-clause (c-point 'boi)))
2164 ;; CASE 12: Statement. But what kind? Lets see if its a
2165 ;; while closure of a do/while construct
2167 (goto-char indent-point)
2168 (skip-chars-forward " \t")
2169 (and (looking-at "while\\b[^_]")
2171 (c-backward-to-start-of-do containing-sexp)
2172 (setq placeholder (point))
2173 (looking-at "do\\b[^_]"))
2175 (goto-char placeholder)
2176 (c-add-syntax 'do-while-closure (c-point 'boi)))
2177 ;; CASE 13: A catch or finally clause? This case is simpler
2178 ;; than if-else and do-while, because a block is required
2179 ;; after every try, catch and finally.
2181 (and (cond ((c-major-mode-is 'c++-mode)
2182 (looking-at "\\<catch\\>[^_]"))
2183 ((c-major-mode-is 'java-mode)
2184 (looking-at "\\<\\(catch\\|finally\\)\\>[^_]")))
2185 (c-safe (c-backward-sexp) t)
2186 (eq (char-after) ?{)
2187 (c-safe (c-backward-sexp) t)
2188 (if (eq (char-after) ?\()
2189 (c-safe (c-backward-sexp) t)
2191 (looking-at "\\<\\(try\\|catch\\)\\>[^_]")
2192 (setq placeholder (c-point 'boi))))
2193 (c-add-syntax 'catch-clause placeholder))
2194 ;; CASE 14: A case or default label
2195 ((looking-at c-switch-label-key)
2196 (goto-char containing-sexp)
2197 ;; check for hanging braces
2198 (if (/= (point) (c-point 'boi))
2199 (c-forward-sexp -1))
2200 (c-add-syntax 'case-label (c-point 'boi)))
2201 ;; CASE 15: any other label
2202 ((looking-at c-label-key)
2203 (goto-char containing-sexp)
2204 ;; check for hanging braces
2205 (if (/= (point) (c-point 'boi))
2206 (c-forward-sexp -1))
2207 (c-add-syntax 'label (c-point 'boi)))
2208 ;; CASE 16: block close brace, possibly closing the defun or
2210 ((eq char-after-ip ?})
2211 (let* ((lim (c-safe-position containing-sexp fullstate))
2212 (relpos (save-excursion
2213 (goto-char containing-sexp)
2214 (if (/= (point) (c-point 'boi))
2215 (c-beginning-of-statement-1 lim))
2218 ;; CASE 16A: closing a lambda defun or an in-expression
2221 (goto-char containing-sexp)
2222 (setq placeholder (c-looking-at-inexpr-block)))
2223 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
2226 (goto-char containing-sexp)
2227 (back-to-indentation)
2228 (if (= containing-sexp (point))
2229 (c-add-syntax tmpsymbol (point))
2230 (goto-char (cdr placeholder))
2231 (c-add-syntax tmpsymbol (c-point 'boi))
2232 (c-add-syntax (car placeholder))))
2233 ;; CASE 16B: does this close an inline or a function in
2234 ;; an extern block or namespace?
2236 (goto-char containing-sexp)
2237 (setq placeholder (c-search-uplist-for-classkey state)))
2238 (goto-char (aref placeholder 0))
2239 (if (looking-at (concat c-extra-toplevel-key "[^_]"))
2240 (c-add-syntax 'defun-close relpos)
2241 (c-add-syntax 'inline-close relpos)))
2242 ;; CASE 16C: if there an enclosing brace that hasn't
2243 ;; been narrowed out by a class, then this is a
2245 ((and (not inenclosing-p)
2246 (c-most-enclosing-brace state))
2247 (c-add-syntax 'block-close relpos))
2248 ;; CASE 16D: find out whether we're closing a top-level
2252 (narrow-to-region (point-min) indent-point)
2253 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
2255 (c-add-class-syntax 'class-close decl)
2256 (c-add-syntax 'defun-close relpos)))))
2258 ;; CASE 17: statement catchall
2260 ;; we know its a statement, but we need to find out if it is
2261 ;; the first statement in a block
2262 (goto-char containing-sexp)
2264 (c-forward-syntactic-ws indent-point)
2265 ;; now skip forward past any case/default clauses we might find.
2266 (while (or (c-skip-case-statement-forward fullstate indent-point)
2267 (and (looking-at c-switch-label-key)
2269 (setq inswitch-p t))
2270 ;; we want to ignore non-case labels when skipping forward
2271 (while (and (looking-at c-label-key)
2272 (goto-char (match-end 0)))
2273 (c-forward-syntactic-ws indent-point))
2275 ;; CASE 17A: we are inside a case/default clause inside a
2276 ;; switch statement. find out if we are at the statement
2277 ;; just after the case/default label.
2280 (goto-char indent-point)
2281 (c-backward-syntactic-ws containing-sexp)
2282 (back-to-indentation)
2283 (setq placeholder (point))
2284 (looking-at c-switch-label-key)))
2285 (goto-char indent-point)
2286 (skip-chars-forward " \t")
2287 (if (eq (char-after) ?{)
2288 (c-add-syntax 'statement-case-open placeholder)
2289 (c-add-syntax 'statement-case-intro placeholder)))
2290 ;; CASE 17B: continued statement
2291 ((eq char-before-ip ?,)
2292 (c-add-syntax 'statement-cont (c-point 'boi)))
2293 ;; CASE 17C: a question/colon construct? But make sure
2294 ;; what came before was not a label, and what comes after
2295 ;; is not a globally scoped function call!
2296 ((or (and (memq char-before-ip '(?: ??))
2298 (goto-char indent-point)
2299 (c-backward-syntactic-ws lim)
2300 (back-to-indentation)
2301 (not (looking-at c-label-key))))
2302 (and (memq char-after-ip '(?: ??))
2304 (goto-char indent-point)
2305 (skip-chars-forward " \t")
2306 ;; watch out for scope operator
2307 (not (looking-at "::")))))
2308 (c-add-syntax 'statement-cont (c-point 'boi)))
2309 ;; CASE 17D: any old statement
2310 ((< (point) indent-point)
2311 (let ((safepos (c-most-enclosing-brace fullstate))
2313 (goto-char indent-point)
2314 (c-beginning-of-statement-1 safepos)
2315 ;; It is possible we're on the brace that opens a nested
2317 (if (and (eq (char-after) ?{)
2319 (c-backward-syntactic-ws safepos)
2320 (not (eq (char-before) ?\;))))
2321 (c-beginning-of-statement-1 safepos))
2323 (looking-at c-switch-label-key))
2325 (goto-char placeholder)
2327 (c-forward-sexp -1)))
2328 (setq relpos (c-point 'boi))
2329 (while (and (not done)
2330 (<= safepos (point))
2331 (/= relpos (point)))
2332 (c-beginning-of-statement-1 safepos)
2333 (if (= relpos (c-point 'boi))
2335 (setq relpos (c-point 'boi)))
2336 (c-add-syntax 'statement relpos)
2337 (if (eq char-after-ip ?{)
2338 (c-add-syntax 'block-open))))
2339 ;; CASE 17E: first statement in an in-expression block
2342 (goto-char containing-sexp)
2343 (c-looking-at-inexpr-block)))
2344 (goto-char containing-sexp)
2345 (back-to-indentation)
2346 (if (= containing-sexp (point))
2347 (c-add-syntax 'statement-block-intro (point))
2348 (goto-char (cdr placeholder))
2349 (c-add-syntax 'statement-block-intro (c-point 'boi))
2350 (c-add-syntax (car placeholder)))
2351 (if (eq char-after-ip ?{)
2352 (c-add-syntax 'block-open)))
2353 ;; CASE 17F: first statement in an inline, or first
2354 ;; statement in a top-level defun. we can tell this is it
2355 ;; if there are no enclosing braces that haven't been
2356 ;; narrowed out by a class (i.e. don't use bod here!)
2360 (goto-char containing-sexp)
2361 (c-narrow-out-enclosing-class state containing-sexp)
2362 (not (c-most-enclosing-brace state))))
2363 (goto-char containing-sexp)
2364 ;; if not at boi, then defun-opening braces are hung on
2365 ;; right side, so we need a different relpos
2366 (if (/= (point) (c-point 'boi))
2368 (c-backward-syntactic-ws)
2369 (c-safe (c-forward-sexp (if (eq (char-before) ?\))
2371 ;; looking at a Java throws clause following a
2372 ;; method's parameter list
2373 (c-beginning-of-statement-1)
2375 (c-add-syntax 'defun-block-intro (c-point 'boi)))
2376 ;; CASE 17G: first statement in a block
2377 (t (goto-char containing-sexp)
2378 (if (/= (point) (c-point 'boi))
2379 (c-beginning-of-statement-1
2381 (c-safe-position (point) state) lim)))
2382 (c-add-syntax 'statement-block-intro (c-point 'boi))
2383 (if (eq char-after-ip ?{)
2384 (c-add-syntax 'block-open)))
2388 ;; now we need to look at any modifiers
2389 (goto-char indent-point)
2390 (skip-chars-forward " \t")
2391 ;; are we looking at a comment only line?
2392 (if (looking-at c-comment-start-regexp)
2393 (c-add-syntax 'comment-intro))
2394 ;; we might want to give additional offset to friends (in C++).
2395 (if (and (c-major-mode-is 'c++-mode)
2396 (looking-at c-C++-friend-key))
2397 (c-add-syntax 'friend))
2398 ;; return the syntax
2402 (defun c-echo-parsing-error ()
2403 (if (not c-parsing-error)
2405 (message "unbalanced close brace at bufpos %d -- INDENTATION IS SUSPECT!"
2410 ;; indent via syntactic language elements
2411 (defun c-indent-line (&optional syntax)
2412 ;; indent the current line as C/C++/ObjC code. Optional SYNTAX is the
2413 ;; syntactic information for the current line. Returns the amount of
2414 ;; indentation change (in columns).
2415 (let* ((c-syntactic-context (or syntax (c-guess-basic-syntax)))
2416 (pos (- (point-max) (point)))
2417 (indent (apply '+ (mapcar 'c-get-offset c-syntactic-context)))
2418 (shift-amt (- (current-indentation) indent)))
2419 (and c-echo-syntactic-information-p
2420 (not (c-echo-parsing-error))
2421 (message "syntax: %s, indent= %d" c-syntactic-context indent))
2422 (if (zerop shift-amt)
2424 (delete-region (c-point 'bol) (c-point 'boi))
2427 (if (< (point) (c-point 'boi))
2428 (back-to-indentation)
2429 ;; If initial point was within line's indentation, position after
2430 ;; the indentation. Else stay at same point in text.
2431 (if (> (- (point-max) pos) (point))
2432 (goto-char (- (point-max) pos)))
2434 (run-hooks 'c-special-indent-hook)
2437 (defun c-show-syntactic-information (arg)
2438 "Show syntactic information for current line.
2439 With universal argument, inserts the analysis as a comment on that line."
2441 (let ((syntax (c-guess-basic-syntax)))
2442 (if (not (consp arg))
2443 (if (not (c-echo-parsing-error))
2444 (message "syntactic analysis: %s" syntax))
2445 (indent-for-comment)
2446 (insert (format "%s" syntax))
2448 (c-keep-region-active))
2450 (defun c-syntactic-information-on-region (from to)
2451 "Inserts a comment with the syntactic analysis on every line in the region."
2455 (narrow-to-region from to)
2456 (goto-char (point-min))
2458 (c-show-syntactic-information '(0))
2462 (provide 'cc-engine)
2463 ;;; cc-engine.el ends here