1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
5 ;; Authors: 2000- Martin Stjernholm
6 ;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
7 ;; 1992-1997 Barry A. Warsaw
8 ;; 1987 Dave Detlefs and Stewart Clamen
9 ;; 1985 Richard M. Stallman
10 ;; Maintainer: bug-cc-mode@gnu.org
11 ;; Created: 22-Apr-1997 (split from cc-mode.el)
12 ;; Version: See cc-mode.el
13 ;; Keywords: c languages oop
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with this program; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA.
38 (if (and (boundp 'byte-compile-dest-file)
39 (stringp byte-compile-dest-file))
40 (cons (file-name-directory byte-compile-dest-file) load-path)
42 (require 'cc-bytecomp)))
46 (cc-require 'cc-langs)
48 ;; Silence the compiler.
49 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
52 (defvar c-state-cache nil)
53 (defvar c-in-literal-cache t)
55 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
56 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
57 ;; better way should be implemented, but this will at least shut up
59 (defvar c-maybe-labelp nil)
61 ;; WARNING WARNING WARNING
63 ;; Be *exceptionally* careful about modifications to this function!
64 ;; Much of CC Mode depends on this Doing The Right Thing. If you
65 ;; break it you will be sorry. If you think you know how this works,
66 ;; you probably don't. No human on Earth does! :-)
68 ;; WARNING WARNING WARNING
70 (defun c-beginning-of-statement-1 (&optional lim)
71 ;; move to the start of the current statement, or the previous
72 ;; statement if already at the beginning of one.
75 donep c-in-literal-cache saved
77 ;; first check for bare semicolon
78 (if (and (progn (c-backward-syntactic-ws lim)
79 (eq (char-before) ?\;))
80 (c-safe (progn (forward-char -1)
83 (progn (c-backward-syntactic-ws lim)
84 (memq (char-before) '(?\; ?{ ?:)))
86 (setq last-begin saved)
87 (goto-char last-begin)
89 ;; stop at beginning of buffer
90 (if (bobp) (setq donep t)
91 ;; go backwards one balanced expression, but be careful of
92 ;; unbalanced paren being reached
93 (if (not (c-safe (progn (c-backward-sexp 1) t)))
97 (goto-char last-begin))
98 ;; skip over any unary operators, or other special
99 ;; characters appearing at front of identifier
101 (c-backward-syntactic-ws lim)
102 (skip-chars-backward "-+!*&:.~@ \t\n")
103 (if (eq (char-before) ?\()
104 (setq last-begin (point))))
105 (goto-char last-begin)
108 (setq c-maybe-labelp nil)
109 ;; see if we're in a literal. if not, then this bufpos may be
110 ;; a candidate for stopping
112 ;; CASE 0: did we hit the error condition above?
114 ;; CASE 1: are we in a literal?
115 ((eq (c-in-literal lim) 'pound)
117 ;; CASE 2: some other kind of literal?
119 ;; CASE 3: are we looking at a conditional keyword?
120 ((or (and c-conditional-key (looking-at c-conditional-key))
121 (and (eq (char-after) ?\()
124 (c-forward-syntactic-ws)
125 (not (eq (char-after) ?\;)))
128 (c-backward-syntactic-ws lim)
132 (not (c-in-literal lim))
133 (not (eq (char-before) ?_))
135 (looking-at c-conditional-key)
137 ;; did we find a conditional?
141 ;; are we in the middle of an else-if clause?
144 (c-safe (progn (c-forward-sexp -1) t))
145 (looking-at "\\<else\\>[ \t\n]+\\<if\\>")
146 (not (c-in-literal lim))))
149 (c-backward-to-start-of-if lim)))
150 ;; are we sitting at an else clause, that we are not a
152 (if (and (not substmt-p)
153 (looking-at "\\<else\\>[^_]"))
154 (c-backward-to-start-of-if lim))
155 ;; a finally or a series of catches?
157 (while (looking-at "\\<\\(catch\\|finally\\)\\>[^_]")
158 (c-safe (c-backward-sexp 2))
159 (if (eq (char-after) ?\()
160 (c-safe (c-backward-sexp)))))
161 ;; are we sitting at the while of a do-while?
162 (if (and (looking-at "\\<while\\>[^_]")
163 (c-backward-to-start-of-do lim))
164 (setq substmt-p nil))
165 (setq last-begin (point)
167 ;; CASE 4: are we looking at a label? (But we handle
168 ;; switch labels later.)
169 ((and (looking-at c-label-key)
170 (not (looking-at "default\\>"))
171 (not (and (c-major-mode-is 'pike-mode)
173 ;; Not inside a Pike type declaration?
174 (and (c-safe (backward-up-list 1) t)
175 (eq (char-after) ?\()))))))
176 ;; CASE 5: is this the first time we're checking?
177 (firstp (setq firstp nil
178 substmt-p (not (c-crosses-statement-barrier-p
181 ;; CASE 6: have we crossed a statement barrier?
183 ;; Move over in-expression blocks before checking the
185 (if (or (memq (char-after) '(?\( ?\[))
186 (and (eq (char-after) ?{)
187 (c-looking-at-inexpr-block lim)))
189 (c-crosses-statement-barrier-p (point) last-begin))
191 ;; CASE 7: ignore labels
193 (or (and c-access-key (looking-at c-access-key))
194 ;; with switch labels, we have to go back further
195 ;; to try to pick up the case or default
196 ;; keyword. Potential bogosity alert: we assume
197 ;; `case' or `default' is first thing on line
198 (let ((here (point)))
200 (c-forward-syntactic-ws here)
201 (if (looking-at c-switch-label-key)
205 ;; CASE 8: ObjC or Java method def
207 (setq last-begin (c-in-method-def-p)))
209 ;; CASE 9: Normal token. At bob, we can end up at ws or a
210 ;; comment, and last-begin shouldn't be updated then.
211 ((not (looking-at "\\s \\|/[/*]"))
212 (setq last-begin (point)))
214 (goto-char last-begin)
215 ;; We always want to skip over the non-whitespace modifier
216 ;; characters that can start a statement.
218 (skip-chars-backward "-+!*&~@`# \t\n" (c-point 'boi))
219 (skip-chars-forward " \t\n" lim))))
221 (defun c-end-of-statement-1 ()
224 (while (and (not (eobp))
231 (while (and (not found)
232 (re-search-forward "[;{}]" end t))
233 (if (not (c-in-literal beg))
237 (re-search-backward "[;{}]")
241 (c-safe (backward-up-list -1))
244 (search-forward ";" end 'move)))
248 (defun c-crosses-statement-barrier-p (from to)
249 ;; Does buffer positions FROM to TO cross a C statement boundary?
256 (while (and (not crossedp)
258 (skip-chars-forward "^;{}:" (1- to))
259 (if (not (c-in-literal lim))
261 (if (memq (char-after) '(?\; ?{ ?}))
263 (if (eq (char-after) ?:)
264 (setq c-maybe-labelp t))
268 (error (setq crossedp nil)))
273 (defun c-beginning-of-macro (&optional lim)
274 ;; Go to the beginning of a cpp macro definition. Leaves point at
275 ;; the beginning of the macro and returns t if in a cpp macro
276 ;; definition, otherwise returns nil and leaves point unchanged.
277 ;; `lim' is currently ignored, but the interface requires it.
278 (let ((here (point)))
280 (while (eq (char-before (1- (point))) ?\\)
282 (back-to-indentation)
283 (if (and (<= (point) here)
284 (eq (char-after) ?#))
289 ;; Skipping of "syntactic whitespace", defined as lexical whitespace,
290 ;; C and C++ style comments, and preprocessor directives. Search no
291 ;; farther back or forward than optional LIM. If LIM is omitted,
292 ;; `beginning-of-defun' is used for backward skipping, point-max is
293 ;; used for forward skipping.
295 (defun c-forward-syntactic-ws (&optional lim)
296 ;; Forward skip of syntactic whitespace for Emacs 19.
297 (let* ((here (point-max))
298 (hugenum (point-max)))
299 (while (/= here (point))
301 (c-forward-comment hugenum)
302 ;; skip preprocessor directives
303 (when (and (eq (char-after) ?#)
304 (= (c-point 'boi) (point)))
305 (while (and (eq (char-before (c-point 'eol)) ?\\)
306 (= (forward-line 1) 0)))
309 (if lim (goto-char (min (point) lim)))))
311 (defun c-backward-syntactic-ws (&optional lim)
312 ;; Backward skip over syntactic whitespace for Emacs 19.
313 (let* ((here (point-min))
314 (hugenum (- (point-max))))
315 (while (/= here (point))
317 (c-forward-comment hugenum)
318 (c-beginning-of-macro))
319 (if lim (goto-char (max (point) lim)))))
322 ;; Moving by tokens, where a token is defined as all symbols and
323 ;; identifiers which aren't syntactic whitespace (note that "->" is
324 ;; considered to be two tokens). Point is always either left at the
325 ;; beginning of a token or not moved at all. COUNT specifies the
326 ;; number of tokens to move; a negative COUNT moves in the opposite
327 ;; direction. A COUNT of 0 moves to the next token beginning only if
328 ;; not already at one. If BALANCED is true, move over balanced
329 ;; parens, otherwise move into them. Also, if BALANCED is true, never
330 ;; move out of an enclosing paren. LIM sets the limit for the
331 ;; movement and defaults to the point limit. Returns the number of
332 ;; tokens left to move (positive or negative). If BALANCED is true, a
333 ;; move over a balanced paren counts as one. Note that if COUNT is 0
334 ;; and no appropriate token beginning is found, 1 will be returned.
335 ;; Thus, a return value of 0 guarantees that point is at the requested
336 ;; position and a return value less (without signs) than COUNT
337 ;; guarantees that point is at the beginning of some token.
339 (defun c-forward-token-1 (&optional count balanced lim)
340 (or count (setq count 1))
342 (- (c-backward-token-1 (- count) balanced lim))
343 (let ((jump-syntax (if balanced
344 '(?w ?_ ?\( ?\) ?\" ?\\ ?/ ?$ ?')
345 '(?w ?_ ?\" ?\\ ?/ ?')))
349 (if lim (narrow-to-region (point-min) lim))
351 (progn (c-forward-syntactic-ws) (point)))
352 ;; Skip whitespace. Count this as a move if we did in fact
353 ;; move and aren't out of bounds.
355 (setq count (max (1- count) 0))))
357 (or (and (memq (char-syntax (or (char-after) ? )) '(?w ?_))
358 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
360 ;; If count is zero we should jump if in the middle of a
361 ;; token or if there is whitespace between point and the
362 ;; following token beginning.
366 ;; Avoid having the limit tests inside the loop.
371 (if (memq (char-syntax (char-after)) jump-syntax)
372 (goto-char (scan-sexps (point) 1))
374 (c-forward-syntactic-ws lim)
375 (setq count (1- count)))
376 (error (goto-char last)))
379 (setq count (1+ count)))))
382 (defun c-backward-token-1 (&optional count balanced lim)
383 (or count (setq count 1))
385 (- (c-forward-token-1 (- count) balanced lim))
386 (let ((jump-syntax (if balanced
387 '(?w ?_ ?\( ?\) ?\" ?\\ ?/ ?$ ?')
388 '(?w ?_ ?\" ?\\ ?/ ?')))
391 (or (and (memq (char-syntax (or (char-after) ? )) '(?w ?_))
392 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
394 (save-excursion (c-forward-syntactic-ws) (point)))
396 ;; If count is zero we should jump if in the middle of a
397 ;; token or if there is whitespace between point and the
398 ;; following token beginning.
401 (if lim (narrow-to-region lim (point-max)))
404 ;; Avoid having the limit tests inside the loop.
409 (c-backward-syntactic-ws lim)
410 (if (memq (char-syntax (char-before)) jump-syntax)
411 (goto-char (scan-sexps (point) -1))
413 (setq count (1- count)))
414 (error (goto-char last)))
415 (if (bobp) (goto-char last)))))
419 ;; Return `c' if in a C-style comment, `c++' if in a C++ style
420 ;; comment, `string' if in a string literal, `pound' if on a
421 ;; preprocessor line, or nil if not in a comment at all. Optional LIM
422 ;; is used as the backward limit of the search. If omitted, or nil,
423 ;; `beginning-of-defun' is used."
425 (defun c-in-literal (&optional lim)
426 ;; Determine if point is in a C++ literal. we cache the last point
427 ;; calculated if the cache is enabled
428 (if (and (vectorp c-in-literal-cache)
429 (= (point) (aref c-in-literal-cache 0)))
430 (aref c-in-literal-cache 1)
431 (let ((rtn (save-excursion
432 (let* ((lim (or lim (c-point 'bod)))
433 (state (parse-partial-sexp lim (point))))
435 ((nth 3 state) 'string)
436 ((nth 4 state) (if (nth 7 state) 'c++ 'c))
437 ((c-beginning-of-macro lim) 'pound)
439 ;; cache this result if the cache is enabled
440 (if (not c-in-literal-cache)
441 (setq c-in-literal-cache (vector (point) rtn)))
444 ;; XEmacs has a built-in function that should make this much quicker.
445 ;; I don't think we even need the cache, which makes our lives more
446 ;; complicated anyway. In this case, lim is ignored.
447 (defun c-fast-in-literal (&optional lim)
448 (let ((context (buffer-syntactic-context)))
450 ((eq context 'string) 'string)
451 ((eq context 'comment) 'c++)
452 ((eq context 'block-comment) 'c)
453 ((save-excursion (c-beginning-of-macro lim)) 'pound))))
455 (if (fboundp 'buffer-syntactic-context)
456 (defalias 'c-in-literal 'c-fast-in-literal))
458 (defun c-literal-limits (&optional lim near not-in-delimiter)
459 ;; Returns a cons of the beginning and end positions of the comment
460 ;; or string surrounding point (including both delimiters), or nil
461 ;; if point isn't in one. If LIM is non-nil, it's used as the
462 ;; "safe" position to start parsing from. If NEAR is non-nil, then
463 ;; the limits of any literal next to point is returned. "Next to"
464 ;; means there's only [ \t] between point and the literal. The
465 ;; search for such a literal is done first in forward direction. If
466 ;; NOT-IN-DELIMITER is non-nil, the case when point is inside a
467 ;; starting delimiter won't be recognized. This only has effect for
468 ;; comments, which have starting delimiters with more than one
472 (lim (or lim (c-point 'bod)))
473 (state (parse-partial-sexp lim (point))))
475 ;; String. Search backward for the start.
477 (search-backward (make-string 1 (nth 3 state)))
478 (setq state (parse-partial-sexp lim (point))))
479 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
482 ;; Line comment. Search from bol for the comment starter.
484 (setq state (parse-partial-sexp lim (point))
486 (while (not (nth 7 state))
487 (search-forward "//") ; Should never fail.
488 (setq state (parse-partial-sexp
489 lim (point) nil nil state)
492 (cons (point) (progn (c-forward-comment 1) (point))))
494 ;; Block comment. Search backward for the comment starter.
496 (search-backward "/*") ; Should never fail.
497 (setq state (parse-partial-sexp lim (point))))
498 (cons (point) (progn (c-forward-comment 1) (point))))
499 ((and (not not-in-delimiter)
501 (eq (char-before) ?/)
503 ;; We're standing in a comment starter.
505 (cons (point) (progn (c-forward-comment 1) (point))))
508 ;; Search forward for a literal.
509 (skip-chars-forward " \t")
511 ((eq (char-syntax (or (char-after) ?\ )) ?\") ; String.
512 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
514 ((looking-at "/[/*]") ; Line or block comment.
515 (cons (point) (progn (c-forward-comment 1) (point))))
518 (skip-chars-backward " \t")
519 (let ((end (point)) beg)
521 ((eq (char-syntax (or (char-before) ?\ )) ?\") ; String.
522 (setq beg (c-safe (c-backward-sexp 1) (point))))
523 ((and (c-safe (forward-char -2) t)
525 ;; Block comment. Due to the nature of line
526 ;; comments, they will always be covered by the
527 ;; normal case above.
529 (c-forward-comment -1)
530 ;; If LIM is bogus, beg will be bogus.
532 (if beg (cons beg end))))))
535 (defun c-literal-limits-fast (&optional lim near not-in-delimiter)
536 ;; Like c-literal-limits, but for emacsen whose `parse-partial-sexp'
537 ;; returns the pos of the comment start.
540 (lim (or lim (c-point 'bod)))
541 (state (parse-partial-sexp lim (point))))
542 (cond ((nth 3 state) ; String.
543 (goto-char (nth 8 state))
544 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
546 ((nth 4 state) ; Comment.
547 (goto-char (nth 8 state))
548 (cons (point) (progn (c-forward-comment 1) (point))))
549 ((and (not not-in-delimiter)
551 (eq (char-before) ?/)
553 ;; We're standing in a comment starter.
555 (cons (point) (progn (c-forward-comment 1) (point))))
558 ;; Search forward for a literal.
559 (skip-chars-forward " \t")
561 ((eq (char-syntax (or (char-after) ?\ )) ?\") ; String.
562 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
564 ((looking-at "/[/*]") ; Line or block comment.
565 (cons (point) (progn (c-forward-comment 1) (point))))
568 (skip-chars-backward " \t")
569 (let ((end (point)) beg)
571 ((eq (char-syntax (or (char-before) ?\ )) ?\") ; String.
572 (setq beg (c-safe (c-backward-sexp 1) (point))))
573 ((and (c-safe (forward-char -2) t)
575 ;; Block comment. Due to the nature of line
576 ;; comments, they will always be covered by the
577 ;; normal case above.
579 (c-forward-comment -1)
580 ;; If LIM is bogus, beg will be bogus.
582 (if beg (cons beg end))))))
585 (if (c-safe (> (length (save-excursion (parse-partial-sexp 1 1))) 8))
586 (defalias 'c-literal-limits 'c-literal-limits-fast))
588 (defun c-collect-line-comments (range)
589 ;; If the argument is a cons of two buffer positions (such as
590 ;; returned by c-literal-limits), and that range contains a C++
591 ;; style line comment, then an extended range is returned that
592 ;; contains all adjacent line comments (i.e. all comments that
593 ;; starts in the same column with no empty lines or non-whitespace
594 ;; characters between them). Otherwise the argument is returned.
597 (if (and (consp range) (progn
598 (goto-char (car range))
600 (let ((col (current-column))
602 (bopl (c-point 'bopl))
604 ;; Got to take care in the backward direction to handle
605 ;; comments which are preceded by code.
606 (while (and (c-forward-comment -1)
609 (= col (current-column)))
611 bopl (c-point 'bopl)))
613 (while (and (progn (skip-chars-forward " \t")
615 (= col (current-column))
616 (prog1 (zerop (forward-line 1))
617 (setq end (point)))))
622 (defun c-literal-type (range)
623 ;; Convenience function that given the result of c-literal-limits,
624 ;; returns nil or the type of literal that the range surrounds.
625 ;; It's much faster than using c-in-literal and is intended to be
626 ;; used when you need both the type of a literal and its limits.
629 (goto-char (car range))
630 (cond ((eq (char-syntax (or (char-after) ?\ )) ?\") 'string)
631 ((looking-at "//") 'c++)
632 (t 'c))) ; Assuming the range is valid.
637 ;; utilities for moving and querying around syntactic elements
638 (defvar c-parsing-error nil)
640 (defun c-parse-state ()
641 ;; Finds and records all open parens between some important point
642 ;; earlier in the file and point.
644 ;; if there's a state cache, return it
645 (if c-state-cache c-state-cache
648 ;; go back 2 bods, but ignore any bogus positions
649 ;; returned by beginning-of-defun (i.e. open paren
652 (while (not (or at-bob (zerop cnt)))
653 (goto-char (c-point 'bod))
655 (eq (char-after) ?\{)
656 ;; The following catches an obscure special
657 ;; case where the brace is preceded by an
658 ;; open paren. That can only legally occur
659 ;; with blocks inside expressions and in
660 ;; Pike special brace lists. Even so, this
661 ;; test is still bogus then, but hopefully
662 ;; good enough. (We don't want to use
663 ;; up-list here since it might be slow.)
665 (c-backward-syntactic-ws)
666 (not (eq (char-before) ?\())))
671 (here (save-excursion
672 ;;(skip-chars-forward " \t}")
674 (last-bod here) (last-pos pos)
675 placeholder state sexp-end)
676 ;; cache last bod position
677 (while (catch 'backup-bod
679 (while (and pos (< pos here))
681 (if (and (setq pos (c-safe (scan-lists pos 1 -1)))
684 (setq sexp-end (c-safe (scan-sexps (1- pos) 1)))
687 ;; we want to record both the start and end
688 ;; of this sexp, but we only want to record
689 ;; the last-most of any of them before here
691 (if (eq (char-after (1- pos)) ?\{)
692 (setq state (cons (cons (1- pos) sexp-end)
693 (if (consp (car state))
697 ;; we're contained in this sexp so put pos on
699 (setq state (cons (1- pos) state))))
700 ;; something bad happened. check to see if we
701 ;; crossed an unbalanced close brace. if so, we
702 ;; didn't really find the right `important bufpos'
703 ;; so lets back up and try again
704 (if (and (not pos) (not at-bob)
706 (c-safe (scan-lists last-pos 1 1)))
707 ;;(char-after (1- placeholder))
708 (<= placeholder here)
709 (eq (char-after (1- placeholder)) ?\}))
711 (setq last-bod (c-safe (scan-lists last-pos -1 1)))
714 ;; bogus, but what can we do here?
715 (goto-char placeholder)
717 (setq c-parsing-error
719 Unbalanced close brace at line %d" (1+ (count-lines 1 (point)))))
720 (throw 'backup-bod nil))
721 (setq at-bob (= last-bod (point-min))
723 (if (= (char-after last-bod) ?\{)
724 (throw 'backup-bod t)))
730 (defun c-whack-state (bufpos state)
731 ;; whack off any state information that appears on STATE which lies
732 ;; after the bounds of BUFPOS.
735 (setq car (car state)
738 ;; just check the car, because in a balanced brace
739 ;; expression, it must be impossible for the corresponding
740 ;; close brace to be before point, but the open brace to be
742 (if (<= bufpos (car car))
744 ;; its possible that the open brace is before bufpos, but
745 ;; the close brace is after. In that case, convert this
746 ;; to a non-cons element.
747 (if (<= bufpos (cdr car))
748 (setq newstate (append newstate (list (car car))))
749 ;; we know that both the open and close braces are
750 ;; before bufpos, so we also know that everything else
751 ;; on state is before bufpos, so we can glom up the
752 ;; whole thing and exit.
753 (setq newstate (append newstate (list car) state)
757 ;; it's before bufpos, so everything else should too
758 (setq newstate (append newstate (list car) state)
762 (defun c-hack-state (bufpos which state)
763 ;; Using BUFPOS buffer position, and WHICH (must be 'open or
764 ;; 'close), hack the c-parse-state STATE and return the results.
766 (let ((car (car state)))
772 (if (not (eq which 'close))
773 (error "c-hack-state, bad argument: %s" which))
775 (let ((car (car state))
780 ;; TBD: is this test relevant???
782 state ;on error, don't change
783 ;; watch out for balanced expr already on cdr of list
784 (cons (cons car bufpos)
785 (if (consp (car cdr))
789 (defun c-adjust-state (from to shift state)
790 ;; Adjust all points in state that lie in the region FROM..TO by
798 (if (and (<= from car) (< car to))
799 (setcar e (+ shift car)))
800 (if (and (<= from cdr) (< cdr to))
801 (setcdr e (+ shift cdr))))
802 (if (and (<= from e) (< e to))
803 (setq e (+ shift e))))
808 (defun c-beginning-of-inheritance-list (&optional lim)
809 ;; Go to the first non-whitespace after the colon that starts a
810 ;; multiple inheritance introduction. Optional LIM is the farthest
811 ;; back we should search.
812 (let* ((lim (or lim (c-point 'bod)))
814 (back-to-indentation)
817 (c-backward-syntactic-ws lim)
818 (while (and (> (point) lim)
820 (memq (char-before) '(?, ?:)))
823 (setq placeholder (point))
824 (skip-chars-forward " \t")
825 (setq chr (char-after))
826 (not (looking-at c-class-key))
828 (c-backward-syntactic-ws lim))
829 (goto-char placeholder)
830 (skip-chars-forward "^:" (c-point 'eol))))
832 (defun c-in-method-def-p ()
833 ;; Return nil if we aren't in a method definition, otherwise the
834 ;; position of the initial [+-].
838 (looking-at c-method-key)
842 (defun c-at-toplevel-p ()
843 "Return a determination as to whether point is at the `top-level'.
844 Being at the top-level means that point is either outside any
845 enclosing block (such function definition), or inside a class
846 definition, but outside any method blocks.
848 If point is not at the top-level (e.g. it is inside a method
849 definition), then nil is returned. Otherwise, if point is at a
850 top-level not enclosed within a class definition, t is returned.
851 Otherwise, a 2-vector is returned where the zeroth element is the
852 buffer position of the start of the class declaration, and the first
853 element is the buffer position of the enclosing class's opening
855 (let ((state (c-parse-state)))
856 (or (not (c-most-enclosing-brace state))
857 (c-search-uplist-for-classkey state))))
859 (defun c-just-after-func-arglist-p (&optional containing)
860 ;; Return t if we are between a function's argument list closing
861 ;; paren and its opening brace. Note that the list close brace
862 ;; could be followed by a "const" specifier or a member init hanging
863 ;; colon. Optional CONTAINING is position of containing s-exp open
864 ;; brace. If not supplied, point is used as search start.
866 (c-backward-syntactic-ws)
867 (let ((checkpoint (or containing (point))))
868 (goto-char checkpoint)
869 ;; could be looking at const specifier
870 (if (and (eq (char-before) ?t)
872 (looking-at "\\<const\\>"))
873 (c-backward-syntactic-ws)
874 ;; otherwise, we could be looking at a hanging member init
876 (goto-char checkpoint)
877 (while (eq (char-before) ?,)
878 ;; this will catch member inits with multiple
881 (c-backward-syntactic-ws (c-point 'bol))
882 (if (eq (char-before) ?\))
885 (c-backward-syntactic-ws))
886 (if (and (eq (char-before) ?:)
889 (c-backward-syntactic-ws)
890 (looking-at "[ \t\n]*:\\([^:]+\\|$\\)")))
892 (goto-char checkpoint))
894 (and (eq (char-before) ?\))
895 ;; check if we are looking at a method def
896 (or (not c-method-key)
900 (c-backward-syntactic-ws)
901 (not (or (memq (char-before) '(?- ?+))
902 ;; or a class category
905 (looking-at c-class-key))
909 ;; defuns to look backwards for things
910 (defun c-backward-to-start-of-do (&optional lim)
911 ;; Move to the start of the last "unbalanced" do expression.
912 ;; Optional LIM is the farthest back to search. If none is found,
913 ;; nil is returned and point is left unchanged, otherwise t is returned.
915 (case-fold-search nil)
916 (lim (or lim (c-point 'bod)))
919 (while (not (zerop do-level))
920 ;; we protect this call because trying to execute this when the
921 ;; while is not associated with a do will throw an error
926 ;; break infloop for illegal C code
927 ((bobp) (setq do-level 0))
928 ((memq (c-in-literal lim) '(c c++)))
929 ((looking-at "while\\b[^_]")
930 (setq do-level (1+ do-level)))
931 ((looking-at "do\\b[^_]")
932 (if (zerop (setq do-level (1- do-level)))
944 (defun c-backward-to-start-of-if (&optional lim)
945 ;; Move to the start of the last "unbalanced" if and return t. If
946 ;; none is found, and we are looking at an if clause, nil is
949 (here (c-point 'bol))
950 (case-fold-search nil)
951 (lim (or (and lim (>= (point) lim) lim)
953 (at-if (looking-at "if\\b[^_]")))
955 (while (and (not (bobp))
956 (not (zerop if-level)))
957 (c-backward-syntactic-ws)
963 (c-beginning-of-statement-1)
964 (setq c-parsing-error
965 (format "No matching `if' found for `else' on line %d"
966 (1+ (count-lines 1 here))))
967 (throw 'orphan-if nil))))
969 ((looking-at "else\\b[^_]")
970 (setq if-level (1+ if-level)))
971 ((looking-at "if\\b[^_]")
972 ;; check for else if... skip over
973 (let ((here (point)))
974 (c-safe (c-forward-sexp -1))
975 (if (looking-at "\\<else\\>[ \t]+\\<if\\>[^_]")
977 (setq if-level (1- if-level))
985 (defun c-skip-conditional ()
986 ;; skip forward over conditional at point, including any predicate
987 ;; statements in parentheses. No error checking is performed.
988 (c-forward-sexp (cond
990 ((looking-at "\\<else\\>[ \t]+\\<if\\>\\([^_]\\|$\\)") 3)
991 ;; do, else, try, finally
993 "\\<\\(do\\|else\\|try\\|finally\\)\\>\\([^_]\\|$\\)")
995 ;; for, if, while, switch, catch, synchronized, foreach
998 (defun c-beginning-of-closest-statement (&optional lim)
999 ;; Go back to the closest preceding statement start.
1000 (let ((start (point))
1001 (label-re (concat c-label-key "\\|"
1002 c-switch-label-key))
1005 (setq label-re (concat label-re "\\|" c-access-key)))
1006 (c-beginning-of-statement-1 lim)
1007 (while (and (when (<= (point) start)
1008 (setq stmtbeg (point)))
1010 ((looking-at label-re)
1012 (goto-char (match-end 0))
1014 ((looking-at c-conditional-key)
1015 ;; Skip a conditional statement.
1016 (c-safe (c-skip-conditional) t))
1018 (c-forward-syntactic-ws start))
1020 (goto-char stmtbeg))))
1022 (defun c-beginning-of-member-init-list (&optional limit)
1023 ;; Goes to the beginning of a member init list (i.e. just after the
1024 ;; ':') if inside one. Returns t in that case, nil otherwise.
1026 (setq limit (point-min)))
1027 (skip-chars-forward " \t")
1028 (if (eq (char-after) ?,)
1030 (c-backward-syntactic-ws limit))
1031 (while (and (< limit (point))
1032 (eq (char-before) ?,))
1033 ;; this will catch member inits with multiple
1036 (c-backward-syntactic-ws limit)
1037 (if (eq (char-before) ?\))
1038 (c-backward-sexp 1))
1039 (c-backward-syntactic-ws limit)
1040 ;; Skip over any template arg to the class.
1041 (if (eq (char-before) ?>)
1042 (c-with-syntax-table c++-template-syntax-table
1043 (c-backward-sexp 1)))
1045 (c-backward-syntactic-ws limit)
1046 ;; Skip backwards over a fully::qualified::name.
1047 (while (and (eq (char-before) ?:)
1050 (eq (char-before) ?:)))
1052 (c-backward-sexp 1))
1053 ;; now continue checking
1054 (c-backward-syntactic-ws limit))
1055 (and (< limit (point))
1056 (eq (char-before) ?:)))
1058 (defun c-skip-case-statement-forward (state &optional lim)
1059 ;; skip forward over case/default bodies, with optional maximal
1060 ;; limit. if no next case body is found, nil is returned and point
1062 (let ((lim (or lim (point-max)))
1066 (balanced (car state)))
1067 ;; search until we've passed the limit, or we've found our match
1068 (while (and (< (point) lim)
1070 (setq safepos (point))
1071 ;; see if we can find a case statement, not in a literal
1072 (if (and (re-search-forward c-switch-label-key lim 'move)
1073 (setq bufpos (match-beginning 0))
1074 (not (c-in-literal safepos))
1076 ;; if we crossed into a balanced sexp, we know the case is
1077 ;; not part of our switch statement, so just bound over the
1078 ;; sexp and keep looking.
1079 (if (and (consp balanced)
1080 (> bufpos (car balanced))
1081 (< bufpos (cdr balanced)))
1082 (goto-char (cdr balanced))
1090 (defun c-search-uplist-for-classkey (brace-state)
1091 ;; search for the containing class, returning a 2 element vector if
1092 ;; found. aref 0 contains the bufpos of the boi of the class key
1093 ;; line, and aref 1 contains the bufpos of the open brace.
1094 (if (null brace-state)
1095 ;; no brace-state means we cannot be inside a class
1097 (let ((carcache (car brace-state))
1098 search-start search-end)
1099 (if (consp carcache)
1100 ;; a cons cell in the first element means that there is some
1101 ;; balanced sexp before the current bufpos. this we can
1102 ;; ignore. the nth 1 and nth 2 elements define for us the
1103 ;; search boundaries
1104 (setq search-start (nth 2 brace-state)
1105 search-end (nth 1 brace-state))
1106 ;; if the car was not a cons cell then nth 0 and nth 1 define
1107 ;; for us the search boundaries
1108 (setq search-start (nth 1 brace-state)
1109 search-end (nth 0 brace-state)))
1110 ;; search-end cannot be a cons cell
1111 (and (consp search-end)
1112 (error "consp search-end: %s" search-end))
1113 ;; if search-end is nil, or if the search-end character isn't an
1114 ;; open brace, we are definitely not in a class
1115 (if (or (not search-end)
1116 (< search-end (point-min))
1117 (not (eq (char-after search-end) ?{)))
1119 ;; now, we need to look more closely at search-start. if
1120 ;; search-start is nil, then our start boundary is really
1122 (if (not search-start)
1123 (setq search-start (point-min))
1124 ;; if search-start is a cons cell, then we can start
1125 ;; searching from the end of the balanced sexp just ahead of
1127 (if (consp search-start)
1128 (setq search-start (cdr search-start))))
1129 ;; now we can do a quick regexp search from search-start to
1130 ;; search-end and see if we can find a class key. watch for
1131 ;; class like strings in literals
1134 (goto-char search-start)
1135 (let ((search-key (concat c-class-key "\\|" c-extra-toplevel-key))
1136 foundp class match-end)
1137 (if c-inexpr-class-key
1138 (setq search-key (concat search-key "\\|"
1139 c-inexpr-class-key)))
1140 (while (and (not foundp)
1142 (c-forward-syntactic-ws)
1143 (> search-end (point)))
1144 (re-search-forward search-key search-end t))
1145 (setq class (match-beginning 0)
1146 match-end (match-end 0))
1147 (if (c-in-literal search-start)
1148 nil ; its in a comment or string, ignore
1150 (skip-chars-forward " \t\n")
1151 (setq foundp (vector (c-point 'boi) search-end))
1153 ;; check for embedded keywords
1154 ((let ((char (char-after (1- class))))
1156 (memq (char-syntax char) '(?w ?_))))
1157 (goto-char match-end)
1159 ;; make sure we're really looking at the start of a
1160 ;; class definition, and not a forward decl, return
1161 ;; arg, template arg list, or an ObjC or Java method.
1163 (re-search-forward c-method-key search-end t)
1164 (not (c-in-literal class)))
1166 ;; Check if this is an anonymous inner class.
1167 ((and c-inexpr-class-key
1168 (looking-at c-inexpr-class-key))
1169 (while (and (= (c-forward-token-1 1 t) 0)
1170 (looking-at "(\\|\\w\\|\\s_\\|\\.")))
1171 (if (eq (point) search-end)
1172 ;; We're done. Just trap this case in the cond.
1174 ;; False alarm; all conditions aren't satisfied.
1176 ;; Its impossible to define a regexp for this, and
1177 ;; nearly so to do it programmatically.
1179 ;; ; picks up forward decls
1180 ;; = picks up init lists
1181 ;; ) picks up return types
1182 ;; > picks up templates, but remember that we can
1183 ;; inherit from templates!
1184 ((let ((skipchars "^;=)"))
1185 ;; try to see if we found the `class' keyword
1186 ;; inside a template arg list
1188 (skip-chars-backward "^<>" search-start)
1189 (if (eq (char-before) ?<)
1190 (setq skipchars (concat skipchars ">"))))
1192 (skip-chars-forward skipchars search-end)
1193 (c-in-literal class))
1195 (/= (point) search-end))
1201 (defun c-inside-bracelist-p (containing-sexp brace-state)
1202 ;; return the buffer position of the beginning of the brace list
1203 ;; statement if we're inside a brace list, otherwise return nil.
1204 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
1205 ;; paren. BRACE-STATE is the remainder of the state of enclosing
1208 ;; N.B.: This algorithm can potentially get confused by cpp macros
1209 ;; places in inconvenient locations. Its a trade-off we make for
1212 ;; this will pick up enum lists
1215 (goto-char containing-sexp)
1218 (if (and (or (looking-at "enum[\t\n ]+")
1219 (progn (c-forward-sexp -1)
1220 (looking-at "enum[\t\n ]+")))
1221 (setq bracepos (c-safe (scan-lists (point) 1 -1)))
1222 (not (c-crosses-statement-barrier-p (point)
1225 ;; this will pick up array/aggregate init lists, even if they are nested.
1228 ;; Pike can have class definitions anywhere, so we must
1229 ;; check for the class key here.
1230 (and (c-major-mode-is 'pike-mode)
1231 (concat c-class-key "\\|" c-extra-toplevel-key)))
1232 bufpos lim braceassignp)
1233 (while (and (not bufpos)
1235 (if (consp containing-sexp)
1236 (setq containing-sexp (car brace-state)
1237 brace-state (cdr brace-state))
1238 (goto-char containing-sexp)
1239 (if (c-looking-at-inexpr-block)
1240 ;; We're in an in-expression block of some kind. Do
1241 ;; not check nesting.
1242 (setq containing-sexp nil)
1243 ;; see if the open brace is preceded by = or [...] in
1244 ;; this statement, but watch out for operator=
1245 (setq lim (if (consp (car brace-state))
1246 (cdr (car brace-state))
1248 braceassignp 'dontknow)
1249 (c-backward-token-1 1 t lim)
1250 ;; Checks to do only on the first sexp before the brace.
1251 (when (and (c-major-mode-is 'java-mode)
1252 (eq (char-after) ?\[))
1253 ;; In Java, an initialization brace list may follow
1254 ;; directly after "new Foo[]", so check for a "new"
1256 (while (eq braceassignp 'dontknow)
1258 (cond ((/= (c-backward-token-1 1 t lim) 0) nil)
1259 ((looking-at "new\\>[^_]") t)
1260 ((looking-at "\\sw\\|\\s_\\|[.[]")
1261 ;; Carry on looking if this is an
1262 ;; identifier (may contain "." in Java)
1263 ;; or another "[]" sexp.
1266 ;; Checks to do on all sexps before the brace, up to the
1267 ;; beginning of the statement.
1268 (while (eq braceassignp 'dontknow)
1269 (cond ((eq (char-after) ?\;)
1270 (setq braceassignp nil))
1272 (looking-at class-key))
1273 (setq braceassignp nil))
1274 ((eq (char-after) ?=)
1275 ;; We've seen a =, but must check earlier tokens so
1276 ;; that it isn't something that should be ignored.
1277 (setq braceassignp 'maybe)
1278 (while (and (eq braceassignp 'maybe)
1279 (zerop (c-backward-token-1 1 t lim)))
1282 ;; Check for operator =
1283 ((looking-at "operator\\>") nil)
1284 ;; Check for `<opchar>= in Pike.
1285 ((and (c-major-mode-is 'pike-mode)
1286 (or (eq (char-after) ?`)
1287 ;; Special case for Pikes
1288 ;; `[]=, since '[' is not in
1289 ;; the punctuation class.
1290 (and (eq (char-after) ?\[)
1291 (eq (char-before) ?`))))
1293 ((looking-at "\\s.") 'maybe)
1294 ;; make sure we're not in a C++ template
1295 ;; argument assignment
1296 ((and (c-major-mode-is 'c++-mode)
1298 (let ((here (point))
1300 (skip-chars-backward "^<>")
1302 (and (eq (char-before) ?<)
1303 (not (c-crosses-statement-barrier-p
1305 (not (c-in-literal))
1309 (if (and (eq braceassignp 'dontknow)
1310 (/= (c-backward-token-1 1 t lim) 0))
1311 (setq braceassignp nil)))
1312 (if (not braceassignp)
1313 (if (eq (char-after) ?\;)
1314 ;; Brace lists can't contain a semicolon, so we're done.
1315 (setq containing-sexp nil)
1316 ;; lets see if we're nested. find the most nested
1318 (setq containing-sexp (car brace-state)
1319 brace-state (cdr brace-state)))
1320 ;; we've hit the beginning of the aggregate list
1321 (c-beginning-of-statement-1
1322 (c-most-enclosing-brace brace-state))
1323 (setq bufpos (point))))
1328 (defun c-looking-at-special-brace-list (&optional lim)
1329 ;; If we're looking at the start of a pike-style list, ie `({Â })',
1330 ;; `([Â ])', `(<Â >)' etc, a cons of a cons of its starting and ending
1331 ;; positions and its entry in c-special-brace-lists is returned, nil
1332 ;; otherwise. The ending position is nil if the list is still open.
1333 ;; LIM is the limit for forward search. The point may either be at
1334 ;; the `(' or at the following paren character. Tries to check the
1335 ;; matching closer, but assumes it's correct if no balanced paren is
1336 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
1337 ;; a special brace list).
1338 (if c-special-brace-lists
1343 (c-forward-syntactic-ws)
1344 (if (eq (char-after) ?\()
1347 (c-forward-syntactic-ws)
1348 (setq type (assq (char-after) c-special-brace-lists)))
1349 (if (setq type (assq (char-after) c-special-brace-lists))
1351 (c-backward-syntactic-ws)
1353 (setq beg (if (eq (char-after) ?\()
1357 (if (and (c-safe (goto-char beg)
1360 (= (char-before) ?\)))
1361 (c-safe (goto-char beg)
1364 ;; Kludges needed to handle inner
1365 ;; chars both with and without
1367 (or (/= (char-syntax (char-before)) ?\))
1368 (= (char-before) (cdr type)))))
1369 (if (or (/= (char-syntax (char-before)) ?\))
1371 (c-forward-syntactic-ws)
1374 (cons (cons beg end) type))
1375 (cons (list beg) type)))))
1378 (defun c-looking-at-bos ()
1379 ;; Returns nil if inside a statement or declaration.
1381 (c-backward-syntactic-ws)
1383 (memq (char-before) '(?\; ?}))
1384 (and (eq (char-before) ?{)
1385 (not (and c-special-brace-lists
1386 (progn (backward-char)
1387 (c-looking-at-special-brace-list))))))))
1389 (defun c-looking-at-inexpr-block (&optional lim)
1390 ;; Returns non-nil if we're looking at the beginning of a block
1391 ;; inside an expression. The value returned is actually a cons of
1392 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
1393 ;; position of the beginning of the construct. LIM limits the
1396 (or lim (setq lim (point-min)))
1397 (let ((block-follows (eq (char-after) ?{)))
1398 ;; Look at the character after point only as a last resort when
1399 ;; we can't disambiguate.
1400 (if (and block-follows
1401 (progn (c-backward-syntactic-ws) (> (point) lim))
1402 (eq (char-before) ?\()
1403 (not (and c-special-brace-lists
1404 (c-looking-at-special-brace-list))))
1405 (cons 'inexpr-statement (point))
1407 (while (and (not res)
1408 (= (c-backward-token-1 1 t lim) 0)
1410 (looking-at "(\\|\\w\\|\\s_\\|\\."))
1412 (cond ((and block-follows
1414 (looking-at c-inexpr-class-key)
1415 (or (not (looking-at c-class-key))
1416 (let ((prev (point)))
1417 (while (and (= (c-backward-token-1 1 t lim)
1420 (eq (char-syntax (char-after))
1422 (setq prev (point)))
1424 (not (c-looking-at-bos)))))
1425 (cons 'inexpr-class (point)))
1426 ((and c-inexpr-block-key
1427 (looking-at c-inexpr-block-key))
1428 (cons 'inexpr-statement (point)))
1430 (looking-at c-lambda-key))
1431 (cons 'inlambda (point))))))
1434 (defun c-looking-at-inexpr-block-backward (&optional lim)
1435 ;; Returns non-nil if we're looking at the end of an in-expression
1436 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
1438 (let ((lim (or lim (c-point 'bod))))
1440 (c-backward-syntactic-ws lim)
1441 (if (eq (char-before) ?}) ; Recognize only a block currently.
1444 (if (>= (point) lim)
1445 (c-looking-at-inexpr-block lim))))))))
1447 (defun c-on-identifier ()
1448 ;; Returns non-nil if we're on or directly after an identifier.
1449 (if (or (memq (char-syntax (or (char-after) ? )) '(?w ?_))
1450 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
1452 (skip-syntax-backward "w_")
1453 (not (looking-at c-keywords)))
1454 (if (c-major-mode-is 'pike-mode)
1455 ;; Handle the `<operator> syntax in Pike.
1457 (if (eq (char-after) ?\`) (forward-char))
1458 (skip-chars-backward "!%&*+\\-/<=>^|~")
1459 (let ((pos (point)))
1460 (cond ((memq (char-before) '(?\) ?\]))
1461 (c-safe (backward-char 2)))
1462 ((memq (char-before) '(?\( ?\[))
1463 (c-safe (backward-char 1))))
1464 (if (not (looking-at "()\\|\\[]"))
1466 (and (eq (char-before) ?\`)
1467 (looking-at "[-!%&*+/<=>^|~]\\|()\\|\\[]"))))))
1470 (defun c-most-enclosing-brace (state)
1471 ;; return the bufpos of the most enclosing brace that hasn't been
1472 ;; narrowed out by any enclosing class, or nil if none was found
1474 (while (and state (not enclosingp))
1475 (setq enclosingp (car state)
1477 (if (consp enclosingp)
1478 (setq enclosingp nil)
1479 (if (> (point-min) enclosingp)
1480 (setq enclosingp nil))
1484 (defun c-least-enclosing-brace (state)
1485 ;; return the bufpos of the least (highest) enclosing brace that
1486 ;; hasn't been narrowed out by any enclosing class, or nil if none
1488 (c-most-enclosing-brace (nreverse state)))
1490 (defun c-safe-position (bufpos state)
1491 ;; return the closest known safe position higher up than point
1492 (let ((safepos nil))
1495 (if (consp (car state))
1498 (if (< safepos bufpos)
1500 (setq state (cdr state))))
1503 (defun c-narrow-out-enclosing-class (state lim)
1504 ;; narrow the buffer so that the enclosing class is hidden
1505 (setq state (c-whack-state (point) state))
1508 (setq inclass-p (c-search-uplist-for-classkey state))
1511 (goto-char (1+ (aref inclass-p 1)))
1512 (skip-chars-forward " \t\n" lim)
1513 ;; if point is now left of the class opening brace, we're
1514 ;; hosed, so try a different tact
1515 (if (<= (point) (aref inclass-p 1))
1517 (goto-char (1+ (aref inclass-p 1)))
1518 (c-forward-syntactic-ws lim)))
1520 ;; end point is the end of the current line
1524 ;; return the class vector
1528 ;; This function implements the main decision tree for determining the
1529 ;; syntactic analysis of the current line of code. Yes, it's huge and
1532 (defun c-guess-basic-syntax ()
1536 (let* ((indent-point (point))
1537 (case-fold-search nil)
1538 (fullstate (c-parse-state))
1540 literal containing-sexp char-before-ip char-after-ip lim
1541 syntax placeholder c-in-literal-cache inswitch-p
1542 tmpsymbol keyword injava-inher special-brace-list
1543 ;; narrow out any enclosing class or extern "C" block
1544 (inclass-p (c-narrow-out-enclosing-class state indent-point))
1546 ;; check for meta top-level enclosing constructs, possible
1547 ;; extern language definitions, possibly (in C++) namespace
1554 (goto-char (aref inclass-p 0))
1555 (looking-at (concat c-extra-toplevel-key "[^_]"))))
1556 (let ((enclosing (match-string 1)))
1558 ((string-equal enclosing "extern")
1559 (setq inenclosing-p 'extern))
1560 ((string-equal enclosing "namespace")
1561 (setq inenclosing-p 'namespace))
1563 ;; get the buffer position of the most nested opening brace,
1564 ;; if there is one, and it hasn't been narrowed out
1566 (goto-char indent-point)
1567 (skip-chars-forward " \t}")
1568 (skip-chars-backward " \t")
1570 (not containing-sexp))
1571 (setq containing-sexp (car state)
1573 (if (consp containing-sexp)
1574 ;; if cdr == point, then containing sexp is the brace
1575 ;; that opens the sexp we close
1576 (if (= (cdr containing-sexp) (point))
1577 (setq containing-sexp (car containing-sexp))
1578 ;; otherwise, ignore this element
1579 (setq containing-sexp nil))
1580 ;; ignore the bufpos if its been narrowed out by the
1581 ;; containing class or does not contain the indent point
1582 (if (or (<= containing-sexp (point-min))
1583 (>= containing-sexp indent-point))
1584 (setq containing-sexp nil)))))
1586 ;; set the limit on the farthest back we need to search
1587 (setq lim (or containing-sexp
1588 (if (consp (car fullstate))
1589 (cdr (car fullstate))
1593 ;; cache char before and after indent point, and move point to
1594 ;; the most likely position to perform the majority of tests
1595 (goto-char indent-point)
1596 (skip-chars-forward " \t")
1597 (setq char-after-ip (char-after))
1598 (c-backward-syntactic-ws lim)
1599 (setq char-before-ip (char-before))
1600 (goto-char indent-point)
1601 (skip-chars-forward " \t")
1603 ;; are we in a literal?
1604 (setq literal (c-in-literal lim))
1606 ;; now figure out syntactic qualities of the current line
1608 ;; CASE 1: in a string.
1609 ((memq literal '(string))
1610 (c-add-syntax 'string (c-point 'bopl)))
1611 ;; CASE 2: in a C or C++ style comment.
1612 ((memq literal '(c c++))
1613 (c-add-syntax literal (car (c-literal-limits lim))))
1614 ;; CASE 3: in a cpp preprocessor macro continuation.
1615 ((and (eq literal 'pound)
1617 (c-beginning-of-macro lim)
1618 (setq placeholder (point)))
1620 (c-add-syntax 'cpp-macro-cont placeholder))
1621 ;; CASE 4: In-expression statement.
1622 ((and (or c-inexpr-class-key c-inexpr-block-key c-lambda-key)
1623 (setq placeholder (c-looking-at-inexpr-block)))
1624 (setq tmpsymbol (assq (car placeholder)
1625 '((inexpr-class . class-open)
1626 (inexpr-statement . block-open))))
1628 ;; It's a statement block or an anonymous class.
1629 (setq tmpsymbol (cdr tmpsymbol))
1630 ;; It's a Pike lambda. Check whether we are between the
1631 ;; lambda keyword and the argument list or at the defun
1633 (setq tmpsymbol (if (eq char-after-ip ?{)
1635 'lambda-intro-cont)))
1636 (goto-char (cdr placeholder))
1637 (back-to-indentation)
1638 (c-add-syntax tmpsymbol (point))
1639 (unless (eq (point) (cdr placeholder))
1640 (c-add-syntax (car placeholder))))
1641 ;; CASE 5: Line is at top level.
1642 ((null containing-sexp)
1644 ;; CASE 5A: we are looking at a defun, brace list, class,
1645 ;; or inline-inclass method opening brace
1646 ((setq special-brace-list
1647 (or (and c-special-brace-lists
1648 (c-looking-at-special-brace-list))
1649 (eq char-after-ip ?{)))
1651 ;; CASE 5A.1: extern language or namespace construct
1653 (goto-char indent-point)
1654 (skip-chars-forward " \t")
1655 (and (c-safe (progn (c-backward-sexp 2) t))
1656 (looking-at (concat c-extra-toplevel-key "[^_]"))
1657 (setq keyword (match-string 1)
1658 placeholder (point))
1659 (or (and (string-equal keyword "namespace")
1660 (setq tmpsymbol 'namespace-open))
1661 (and (string-equal keyword "extern")
1664 (c-forward-syntactic-ws)
1665 (eq (char-after) ?\"))
1666 (setq tmpsymbol 'extern-lang-open)))
1668 (goto-char placeholder)
1669 (c-add-syntax tmpsymbol (c-point 'boi)))
1670 ;; CASE 5A.2: we are looking at a class opening brace
1672 (goto-char indent-point)
1673 (skip-chars-forward " \t{")
1674 ;; TBD: watch out! there could be a bogus
1675 ;; c-state-cache in place when we get here. we have
1676 ;; to go through much chicanery to ignore the cache.
1677 ;; But of course, there may not be! BLECH! BOGUS!
1679 (let ((c-state-cache nil))
1680 (c-search-uplist-for-classkey (c-parse-state))
1683 (setq placeholder (aref decl 0)))
1685 (c-add-syntax 'class-open placeholder))
1686 ;; CASE 5A.3: brace list open
1688 (c-beginning-of-statement-1 lim)
1689 ;; c-b-o-s could have left us at point-min
1691 (c-forward-syntactic-ws indent-point))
1692 (if (looking-at "typedef[^_]")
1693 (progn (c-forward-sexp 1)
1694 (c-forward-syntactic-ws indent-point)))
1695 (setq placeholder (c-point 'boi))
1696 (or (consp special-brace-list)
1697 (and (or (save-excursion
1698 (goto-char indent-point)
1699 (setq tmpsymbol nil)
1700 (while (and (> (point) placeholder)
1701 (= (c-backward-token-1 1 t) 0)
1702 (/= (char-after) ?=))
1703 (if (and (not tmpsymbol)
1704 (looking-at "new\\>[^_]"))
1705 (setq tmpsymbol 'topmost-intro-cont)))
1706 (eq (char-after) ?=))
1707 (looking-at "enum[ \t\n]+"))
1709 (while (and (< (point) indent-point)
1710 (= (c-forward-token-1 1 t) 0)
1711 (not (memq (char-after) '(?\; ?\()))))
1712 (not (memq (char-after) '(?\; ?\()))
1714 (if (and (c-major-mode-is 'java-mode)
1715 (eq tmpsymbol 'topmost-intro-cont))
1716 ;; We're in Java and have found that the open brace
1717 ;; belongs to a "new Foo[]" initialization list,
1718 ;; which means the brace list is part of an
1719 ;; expression and not a top level definition. We
1720 ;; therefore treat it as any topmost continuation
1721 ;; even though the semantically correct symbol still
1722 ;; is brace-list-open, on the same grounds as in
1725 (c-beginning-of-statement-1 lim)
1726 (c-forward-syntactic-ws)
1727 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
1728 (c-add-syntax 'brace-list-open placeholder)))
1729 ;; CASE 5A.4: inline defun open
1730 ((and inclass-p (not inenclosing-p))
1731 (c-add-syntax 'inline-open)
1732 (c-add-class-syntax 'inclass inclass-p))
1733 ;; CASE 5A.5: ordinary defun open
1735 (goto-char placeholder)
1737 (c-add-syntax 'defun-open (c-point 'boi))
1738 (c-add-syntax 'defun-open (c-point 'bol)))
1740 ;; CASE 5B: first K&R arg decl or member init
1741 ((c-just-after-func-arglist-p)
1743 ;; CASE 5B.1: a member init
1744 ((or (eq char-before-ip ?:)
1745 (eq char-after-ip ?:))
1746 ;; this line should be indented relative to the beginning
1747 ;; of indentation for the topmost-intro line that contains
1748 ;; the prototype's open paren
1749 ;; TBD: is the following redundant?
1750 (if (eq char-before-ip ?:)
1752 (c-backward-syntactic-ws lim)
1753 ;; TBD: is the preceding redundant?
1754 (if (eq (char-before) ?:)
1755 (progn (forward-char -1)
1756 (c-backward-syntactic-ws lim)))
1757 (if (eq (char-before) ?\))
1758 (c-backward-sexp 1))
1759 (setq placeholder (point))
1761 (and (c-safe (c-backward-sexp 1) t)
1762 (looking-at "throw[^_]")
1763 (c-safe (c-backward-sexp 1) t)
1764 (setq placeholder (point))))
1765 (goto-char placeholder)
1766 (c-add-syntax 'member-init-intro (c-point 'boi))
1767 ;; we don't need to add any class offset since this
1768 ;; should be relative to the ctor's indentation
1770 ;; CASE 5B.2: K&R arg decl intro
1772 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
1773 (if inclass-p (c-add-class-syntax 'inclass inclass-p)))
1774 ;; CASE 5B.3: Inside a member init list.
1775 ((c-beginning-of-member-init-list lim)
1776 (c-forward-syntactic-ws)
1777 (c-add-syntax 'member-init-cont (point)))
1778 ;; CASE 5B.4: Nether region after a C++ or Java func
1779 ;; decl, which could include a `throws' declaration.
1781 (c-beginning-of-statement-1 lim)
1782 (c-add-syntax 'func-decl-cont (c-point 'boi))
1784 ;; CASE 5C: inheritance line. could be first inheritance
1785 ;; line, or continuation of a multiple inheritance
1786 ((or (and c-baseclass-key
1788 (when (eq char-after-ip ?,)
1789 (skip-chars-forward " \t")
1791 (looking-at c-baseclass-key)))
1792 (and (or (eq char-before-ip ?:)
1793 ;; watch out for scope operator
1795 (and (eq char-after-ip ?:)
1796 (c-safe (progn (forward-char 1) t))
1797 (not (eq (char-after) ?:))
1800 (c-backward-syntactic-ws lim)
1801 (if (eq char-before-ip ?:)
1804 (c-backward-syntactic-ws lim)))
1805 (back-to-indentation)
1806 (looking-at c-class-key)))
1808 (and (c-major-mode-is 'java-mode)
1809 (let ((fence (save-excursion
1810 (c-beginning-of-statement-1 lim)
1815 (cond ((looking-at c-Java-special-key)
1816 (setq injava-inher (cons cont (point))
1818 ((or (not (c-safe (c-forward-sexp -1) t))
1824 (not (c-crosses-statement-barrier-p (cdr injava-inher)
1828 ;; CASE 5C.1: non-hanging colon on an inher intro
1829 ((eq char-after-ip ?:)
1830 (c-backward-syntactic-ws lim)
1831 (c-add-syntax 'inher-intro (c-point 'boi))
1832 ;; don't add inclass symbol since relative point already
1833 ;; contains any class offset
1835 ;; CASE 5C.2: hanging colon on an inher intro
1836 ((eq char-before-ip ?:)
1837 (c-add-syntax 'inher-intro (c-point 'boi))
1838 (if inclass-p (c-add-class-syntax 'inclass inclass-p)))
1839 ;; CASE 5C.3: in a Java implements/extends
1841 (let ((where (cdr injava-inher))
1842 (cont (car injava-inher)))
1844 (cond ((looking-at "throws[ \t\n]")
1845 (c-add-syntax 'func-decl-cont
1846 (progn (c-beginning-of-statement-1 lim)
1848 (cont (c-add-syntax 'inher-cont where))
1849 (t (c-add-syntax 'inher-intro
1850 (progn (goto-char (cdr injava-inher))
1851 (c-beginning-of-statement-1 lim)
1854 ;; CASE 5C.4: a continued inheritance line
1856 (c-beginning-of-inheritance-list lim)
1857 (c-add-syntax 'inher-cont (point))
1858 ;; don't add inclass symbol since relative point already
1859 ;; contains any class offset
1861 ;; CASE 5D: this could be a top-level compound statement, a
1862 ;; member init list continuation, or a template argument
1863 ;; list continuation.
1864 ((c-with-syntax-table (if (c-major-mode-is 'c++-mode)
1865 c++-template-syntax-table
1868 (while (and (= (c-backward-token-1 1 t lim) 0)
1869 (not (looking-at "[;{<,]"))))
1870 (eq (char-after) ?,)))
1871 (goto-char indent-point)
1872 (c-beginning-of-member-init-list lim)
1874 ;; CASE 5D.1: hanging member init colon, but watch out
1875 ;; for bogus matches on access specifiers inside classes.
1876 ((and (save-excursion
1877 (setq placeholder (point))
1878 (c-backward-token-1 1 t lim)
1879 (and (eq (char-after) ?:)
1880 (not (eq (char-before) ?:))))
1882 (goto-char placeholder)
1883 (back-to-indentation)
1885 (/= (car (save-excursion
1886 (parse-partial-sexp (point) placeholder)))
1889 (if c-access-key (not (looking-at c-access-key)) t)
1890 (not (looking-at c-class-key))
1891 (if c-bitfield-key (not (looking-at c-bitfield-key)) t))
1893 (goto-char placeholder)
1894 (c-forward-syntactic-ws)
1895 (c-add-syntax 'member-init-cont (point))
1896 ;; we do not need to add class offset since relative
1897 ;; point is the member init above us
1899 ;; CASE 5D.2: non-hanging member init colon
1901 (c-forward-syntactic-ws indent-point)
1902 (eq (char-after) ?:))
1903 (skip-chars-forward " \t:")
1904 (c-add-syntax 'member-init-cont (point)))
1905 ;; CASE 5D.3: perhaps a multiple inheritance line?
1907 (c-beginning-of-statement-1 lim)
1908 (setq placeholder (point))
1909 (looking-at c-inher-key))
1910 (goto-char placeholder)
1911 (c-add-syntax 'inher-cont (c-point 'boi)))
1912 ;; CASE 5D.4: perhaps a template list continuation?
1914 (goto-char indent-point)
1915 (skip-chars-backward "^<" lim)
1916 ;; not sure if this is the right test, but it should
1917 ;; be fast and mostly accurate.
1918 (setq placeholder (point))
1919 (and (eq (char-before) ?<)
1920 (not (c-in-literal lim))))
1921 ;; we can probably indent it just like an arglist-cont
1922 (goto-char placeholder)
1923 (c-beginning-of-statement-1 lim)
1924 (c-add-syntax 'template-args-cont (c-point 'boi)))
1925 ;; CASE 5D.5: perhaps a top-level statement-cont
1927 (c-beginning-of-statement-1 lim)
1928 ;; skip over any access-specifiers
1929 (and inclass-p c-access-key
1930 (while (looking-at c-access-key)
1932 ;; skip over comments, whitespace
1933 (c-forward-syntactic-ws indent-point)
1934 (c-add-syntax 'statement-cont (c-point 'boi)))
1936 ;; CASE 5E: we are looking at a access specifier
1939 (looking-at c-access-key))
1940 (c-add-syntax 'access-label (c-point 'bonl))
1941 (c-add-class-syntax 'inclass inclass-p))
1942 ;; CASE 5F: extern-lang-close or namespace-close?
1944 (eq char-after-ip ?}))
1945 (setq tmpsymbol (if (eq inenclosing-p 'extern)
1948 (c-add-syntax tmpsymbol (aref inclass-p 0)))
1949 ;; CASE 5G: we are looking at the brace which closes the
1950 ;; enclosing nested class decl
1952 (eq char-after-ip ?})
1957 (and (c-safe (progn (c-backward-sexp 1) t))
1958 (= (point) (aref inclass-p 1))
1960 (c-add-class-syntax 'class-close inclass-p))
1961 ;; CASE 5H: we could be looking at subsequent knr-argdecls
1962 ((and c-recognize-knr-p
1963 ;; here we essentially use the hack that is used in
1964 ;; Emacs' c-mode.el to limit how far back we should
1965 ;; look. The assumption is made that argdecls are
1966 ;; indented at least one space and that function
1967 ;; headers are not indented.
1968 (let ((limit (save-excursion
1969 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
1972 (c-backward-syntactic-ws limit)
1973 (setq placeholder (point))
1974 (while (and (memq (char-before) '(?\; ?,))
1977 (setq placeholder (point))
1978 (c-backward-syntactic-ws limit))
1979 (and (eq (char-before) ?\))
1980 (or (not c-method-key)
1984 (c-backward-syntactic-ws)
1985 (not (or (memq (char-before) '(?- ?+))
1986 ;; or a class category
1989 (looking-at c-class-key))
1993 (c-beginning-of-statement-1)
1994 (not (looking-at "typedef[ \t\n]+"))))
1995 (goto-char placeholder)
1996 (c-add-syntax 'knr-argdecl (c-point 'boi)))
1997 ;; CASE 5I: ObjC method definition.
1999 (looking-at c-method-key))
2000 (c-add-syntax 'objc-method-intro (c-point 'boi)))
2001 ;; CASE 5J: we are at the topmost level, make sure we skip
2002 ;; back past any access specifiers
2004 (c-backward-syntactic-ws lim)
2005 (while (and inclass-p
2009 (c-safe (progn (c-backward-sexp 1) t))
2010 (looking-at c-access-key)))
2012 (c-backward-syntactic-ws lim))
2014 (memq (char-before) '(?\; ?\}))))
2015 ;; real beginning-of-line could be narrowed out due to
2016 ;; enclosure in a class block
2019 (c-add-syntax 'topmost-intro (c-point 'bol))
2022 (goto-char (aref inclass-p 1))
2023 (or (= (point) (c-point 'boi))
2024 (goto-char (aref inclass-p 0)))
2026 ((eq inenclosing-p 'extern)
2027 (c-add-syntax 'inextern-lang (c-point 'boi)))
2028 ((eq inenclosing-p 'namespace)
2029 (c-add-syntax 'innamespace (c-point 'boi)))
2030 (t (c-add-class-syntax 'inclass inclass-p)))
2033 ;; CASE 5K: we are at an ObjC or Java method definition
2034 ;; continuation line.
2037 (c-beginning-of-statement-1 lim)
2039 (looking-at c-method-key)))
2040 (c-add-syntax 'objc-method-args-cont (point)))
2041 ;; CASE 5L: we are at the first argument of a template
2042 ;; arglist that begins on the previous line.
2043 ((eq (char-before) ?<)
2044 (c-beginning-of-statement-1 lim)
2045 (c-forward-syntactic-ws)
2046 (c-add-syntax 'template-args-cont (c-point 'boi)))
2047 ;; CASE 5M: we are at a topmost continuation line
2049 (c-beginning-of-statement-1 lim)
2050 (c-forward-syntactic-ws)
2051 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
2053 ;; (CASE 6 has been removed.)
2054 ;; CASE 7: line is an expression, not a statement. Most
2055 ;; likely we are either in a function prototype or a function
2056 ;; call argument list
2057 ((not (or (and c-special-brace-lists
2059 (goto-char containing-sexp)
2060 (c-looking-at-special-brace-list)))
2061 (eq (char-after containing-sexp) ?{)))
2062 (c-backward-syntactic-ws containing-sexp)
2064 ;; CASE 7A: we are looking at the arglist closing paren
2065 ((and (or (c-major-mode-is 'pike-mode)
2066 ;; Don't check this in Pike since it allows a
2067 ;; comma after the last arg.
2068 (not (eq char-before-ip ?,)))
2069 (memq char-after-ip '(?\) ?\])))
2070 (goto-char containing-sexp)
2071 (setq placeholder (c-point 'boi))
2072 (when (and (c-safe (backward-up-list 1) t)
2073 (> (point) placeholder))
2075 (skip-chars-forward " \t")
2076 (setq placeholder (point)))
2077 (c-add-syntax 'arglist-close placeholder))
2078 ;; CASE 7B: Looking at the opening brace of an
2079 ;; in-expression block or brace list.
2080 ((eq char-after-ip ?{)
2081 (goto-char indent-point)
2082 (setq placeholder (c-point 'boi))
2083 (goto-char containing-sexp)
2084 (if (c-inside-bracelist-p placeholder
2085 (cons containing-sexp state))
2087 (c-add-syntax 'brace-list-open (c-point 'boi))
2088 (c-add-syntax 'inexpr-class))
2089 (c-add-syntax 'block-open (c-point 'boi))
2090 (c-add-syntax 'inexpr-statement)))
2091 ;; CASE 7C: we are looking at the first argument in an empty
2092 ;; argument list. Use arglist-close if we're actually
2093 ;; looking at a close paren or bracket.
2094 ((memq char-before-ip '(?\( ?\[))
2095 (goto-char containing-sexp)
2096 (setq placeholder (c-point 'boi))
2097 (when (and (c-safe (backward-up-list 1) t)
2098 (> (point) placeholder))
2100 (skip-chars-forward " \t")
2101 (setq placeholder (point)))
2102 (c-add-syntax 'arglist-intro placeholder))
2103 ;; CASE 7D: we are inside a conditional test clause. treat
2104 ;; these things as statements
2106 (goto-char containing-sexp)
2107 (and (c-safe (progn (c-forward-sexp -1) t))
2108 (looking-at "\\<for\\>[^_]")))
2109 (goto-char (1+ containing-sexp))
2110 (c-forward-syntactic-ws indent-point)
2111 (c-beginning-of-statement-1 containing-sexp)
2112 (if (eq char-before-ip ?\;)
2113 (c-add-syntax 'statement (point))
2114 (c-add-syntax 'statement-cont (point))
2116 ;; CASE 7E: maybe a continued method call. This is the case
2117 ;; when we are inside a [] bracketed exp, and what precede
2118 ;; the opening bracket is not an identifier.
2120 (eq (char-after containing-sexp) ?\[)
2122 (goto-char (1- containing-sexp))
2123 (c-backward-syntactic-ws (c-point 'bod))
2124 (if (not (looking-at c-symbol-key))
2125 (c-add-syntax 'objc-method-call-cont containing-sexp))
2127 ;; CASE 7F: we are looking at an arglist continuation line,
2128 ;; but the preceding argument is on the same line as the
2129 ;; opening paren. This case includes multi-line
2130 ;; mathematical paren groupings, but we could be on a
2131 ;; for-list continuation line
2133 (goto-char (1+ containing-sexp))
2134 (skip-chars-forward " \t")
2136 (goto-char containing-sexp)
2137 (setq placeholder (c-point 'boi))
2138 (when (and (c-safe (backward-up-list 1) t)
2139 (> (point) placeholder))
2141 (skip-chars-forward " \t")
2142 (setq placeholder (point)))
2143 (c-add-syntax 'arglist-cont-nonempty placeholder))
2144 ;; CASE 7G: we are looking at just a normal arglist
2145 ;; continuation line
2146 (t (c-beginning-of-statement-1 containing-sexp)
2148 (c-forward-syntactic-ws indent-point)
2149 (c-add-syntax 'arglist-cont (c-point 'boi)))
2151 ;; CASE 8: func-local multi-inheritance line
2152 ((and c-baseclass-key
2154 (goto-char indent-point)
2155 (skip-chars-forward " \t")
2156 (looking-at c-baseclass-key)))
2157 (goto-char indent-point)
2158 (skip-chars-forward " \t")
2160 ;; CASE 8A: non-hanging colon on an inher intro
2161 ((eq char-after-ip ?:)
2162 (c-backward-syntactic-ws lim)
2163 (c-add-syntax 'inher-intro (c-point 'boi)))
2164 ;; CASE 8B: hanging colon on an inher intro
2165 ((eq char-before-ip ?:)
2166 (c-add-syntax 'inher-intro (c-point 'boi)))
2167 ;; CASE 8C: a continued inheritance line
2169 (c-beginning-of-inheritance-list lim)
2170 (c-add-syntax 'inher-cont (point))
2172 ;; CASE 9: we are inside a brace-list
2173 ((setq special-brace-list
2174 (or (and c-special-brace-lists
2176 (goto-char containing-sexp)
2177 (c-looking-at-special-brace-list)))
2178 (c-inside-bracelist-p containing-sexp state)))
2180 ;; CASE 9A: In the middle of a special brace list opener.
2181 ((and (consp special-brace-list)
2183 (goto-char containing-sexp)
2184 (eq (char-after) ?\())
2185 (eq char-after-ip (car (cdr special-brace-list))))
2186 (goto-char (car (car special-brace-list)))
2187 (skip-chars-backward " \t")
2189 (assoc 'statement-cont
2190 (setq placeholder (c-guess-basic-syntax))))
2191 (setq syntax placeholder)
2192 (c-beginning-of-statement-1 lim)
2193 (c-forward-token-1 0)
2194 (if (looking-at "typedef\\>") (c-forward-token-1 1))
2195 (c-add-syntax 'brace-list-open (c-point 'boi))))
2196 ;; CASE 9B: brace-list-close brace
2197 ((if (consp special-brace-list)
2198 ;; Check special brace list closer.
2200 (goto-char (car (car special-brace-list)))
2202 (goto-char indent-point)
2203 (back-to-indentation)
2205 ;; We were between the special close char and the `)'.
2206 (and (eq (char-after) ?\))
2207 (eq (1+ (point)) (cdr (car special-brace-list))))
2208 ;; We were before the special close char.
2209 (and (eq (char-after) (cdr (cdr special-brace-list)))
2210 (= (c-forward-token-1) 0)
2211 (eq (1+ (point)) (cdr (car special-brace-list)))))))
2212 ;; Normal brace list check.
2213 (and (eq char-after-ip ?})
2214 (c-safe (progn (forward-char 1)
2217 (= (point) containing-sexp)))
2218 (c-add-syntax 'brace-list-close (c-point 'boi)))
2220 ;; Prepare for the rest of the cases below by going to the
2221 ;; token following the opening brace
2222 (if (consp special-brace-list)
2224 (goto-char (car (car special-brace-list)))
2225 (c-forward-token-1 1 nil indent-point))
2226 (goto-char containing-sexp))
2228 (let ((start (point)))
2229 (c-forward-syntactic-ws indent-point)
2230 (goto-char (max start (c-point 'bol))))
2231 (skip-chars-forward " \t\n\r" indent-point)
2233 ;; CASE 9C: we're looking at the first line in a brace-list
2234 ((= (point) indent-point)
2235 (goto-char containing-sexp)
2236 (c-add-syntax 'brace-list-intro (c-point 'boi))
2238 ;; CASE 9D: this is just a later brace-list-entry or
2240 (t (if (or (eq char-after-ip ?{)
2241 (and c-special-brace-lists
2243 (goto-char indent-point)
2244 (c-forward-syntactic-ws (c-point 'eol))
2245 (c-looking-at-special-brace-list (point)))))
2246 (c-add-syntax 'brace-entry-open (point))
2247 (c-add-syntax 'brace-list-entry (point))
2250 ;; CASE 10: A continued statement
2251 ((and (not (memq char-before-ip '(?\; ?:)))
2252 (or (not (eq char-before-ip ?}))
2253 (c-looking-at-inexpr-block-backward containing-sexp))
2256 (c-beginning-of-statement-1 containing-sexp)
2257 (c-forward-syntactic-ws)
2258 (setq placeholder (point))))
2259 (/= placeholder containing-sexp))
2260 (goto-char indent-point)
2261 (skip-chars-forward " \t")
2262 (let ((after-cond-placeholder
2264 (goto-char placeholder)
2265 (if (and c-conditional-key (looking-at c-conditional-key))
2267 (c-safe (c-skip-conditional))
2268 (c-forward-syntactic-ws)
2269 (if (eq (char-after) ?\;)
2272 (c-forward-syntactic-ws)))
2276 ;; CASE 10A: substatement
2277 ((and after-cond-placeholder
2278 (>= after-cond-placeholder indent-point))
2279 (goto-char placeholder)
2280 (if (eq char-after-ip ?{)
2281 (c-add-syntax 'substatement-open (c-point 'boi))
2282 (c-add-syntax 'substatement (c-point 'boi))))
2283 ;; CASE 10B: open braces for class or brace-lists
2284 ((setq special-brace-list
2285 (or (and c-special-brace-lists
2286 (c-looking-at-special-brace-list))
2287 (eq char-after-ip ?{)))
2289 ;; CASE 10B.1: class-open
2291 (goto-char indent-point)
2292 (skip-chars-forward " \t{")
2293 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
2295 (setq placeholder (aref decl 0)))
2297 (c-add-syntax 'class-open placeholder))
2298 ;; CASE 10B.2: brace-list-open
2299 ((or (consp special-brace-list)
2301 (goto-char placeholder)
2302 (looking-at "\\<enum\\>"))
2304 (goto-char indent-point)
2305 (while (and (> (point) placeholder)
2306 (= (c-backward-token-1 1 t) 0)
2307 (/= (char-after) ?=)))
2308 (eq (char-after) ?=)))
2309 ;; The most semantically accurate symbol here is
2310 ;; brace-list-open, but we report it simply as a
2311 ;; statement-cont. The reason is that one normally
2312 ;; adjusts brace-list-open for brace lists as
2313 ;; top-level constructs, and brace lists inside
2314 ;; statements is a completely different context.
2315 (goto-char indent-point)
2316 (c-beginning-of-closest-statement)
2317 (c-add-syntax 'statement-cont (c-point 'boi)))
2318 ;; CASE 10B.3: The body of a function declared inside a
2319 ;; normal block. This can only occur in Pike.
2320 ((and (c-major-mode-is 'pike-mode)
2322 (goto-char indent-point)
2323 (not (c-looking-at-bos))))
2324 (c-beginning-of-closest-statement)
2325 (c-add-syntax 'defun-open (c-point 'boi)))
2326 ;; CASE 10B.4: catch-all for unknown construct.
2328 ;; Can and should I add an extensibility hook here?
2329 ;; Something like c-recognize-hook so support for
2330 ;; unknown constructs could be added. It's probably a
2331 ;; losing proposition, so I dunno.
2332 (goto-char placeholder)
2333 (c-add-syntax 'statement-cont (c-point 'boi))
2334 (c-add-syntax 'block-open))
2336 ;; CASE 10C: iostream insertion or extraction operator
2337 ((looking-at "<<\\|>>")
2338 (goto-char placeholder)
2339 (and after-cond-placeholder
2340 (goto-char after-cond-placeholder))
2341 (while (and (re-search-forward "<<\\|>>" indent-point 'move)
2342 (c-in-literal placeholder)))
2343 ;; if we ended up at indent-point, then the first
2344 ;; streamop is on a separate line. Indent the line like
2345 ;; a statement-cont instead
2346 (if (/= (point) indent-point)
2347 (c-add-syntax 'stream-op (c-point 'boi))
2348 (c-backward-syntactic-ws lim)
2349 (c-add-syntax 'statement-cont (c-point 'boi))))
2350 ;; CASE 10D: continued statement. find the accurate
2351 ;; beginning of statement or substatement
2353 (c-beginning-of-statement-1 after-cond-placeholder)
2354 ;; KLUDGE ALERT! c-beginning-of-statement-1 can leave
2355 ;; us before the lim we're passing in. It should be
2356 ;; fixed, but I'm worried about side-effects at this
2357 ;; late date. Fix for v5.
2358 (goto-char (or (and after-cond-placeholder
2359 (max after-cond-placeholder (point)))
2361 (c-add-syntax 'statement-cont (point)))
2363 ;; CASE 11: an else clause?
2364 ((looking-at "\\<else\\>[^_]")
2365 (c-backward-to-start-of-if containing-sexp)
2366 (c-add-syntax 'else-clause (c-point 'boi)))
2367 ;; CASE 12: Statement. But what kind? Lets see if its a
2368 ;; while closure of a do/while construct
2370 (goto-char indent-point)
2371 (skip-chars-forward " \t")
2372 (and (looking-at "while\\b[^_]")
2374 (c-backward-to-start-of-do containing-sexp)
2375 (setq placeholder (point))
2376 (looking-at "do\\b[^_]"))
2378 (goto-char placeholder)
2379 (c-add-syntax 'do-while-closure (c-point 'boi)))
2380 ;; CASE 13: A catch or finally clause? This case is simpler
2381 ;; than if-else and do-while, because a block is required
2382 ;; after every try, catch and finally.
2384 (and (cond ((c-major-mode-is 'c++-mode)
2385 (looking-at "\\<catch\\>[^_]"))
2386 ((c-major-mode-is 'java-mode)
2387 (looking-at "\\<\\(catch\\|finally\\)\\>[^_]")))
2388 (c-safe (c-backward-sexp) t)
2389 (eq (char-after) ?{)
2390 (c-safe (c-backward-sexp) t)
2391 (if (eq (char-after) ?\()
2392 (c-safe (c-backward-sexp) t)
2394 (looking-at "\\<\\(try\\|catch\\)\\>[^_]")
2395 (setq placeholder (c-point 'boi))))
2396 (c-add-syntax 'catch-clause placeholder))
2397 ;; CASE 14: A case or default label
2398 ((looking-at c-switch-label-key)
2399 (goto-char containing-sexp)
2400 ;; check for hanging braces
2401 (if (/= (point) (c-point 'boi))
2402 (c-forward-sexp -1))
2403 (c-add-syntax 'case-label (c-point 'boi)))
2404 ;; CASE 15: any other label
2405 ((looking-at c-label-key)
2406 (goto-char containing-sexp)
2407 ;; check for hanging braces
2408 (if (/= (point) (c-point 'boi))
2409 (c-forward-sexp -1))
2410 (c-add-syntax 'label (c-point 'boi)))
2411 ;; CASE 16: block close brace, possibly closing the defun or
2413 ((eq char-after-ip ?})
2414 (let* ((lim (c-safe-position containing-sexp fullstate))
2415 (relpos (save-excursion
2416 (goto-char containing-sexp)
2417 (if (/= (point) (c-point 'boi))
2418 (c-beginning-of-statement-1 lim))
2421 ;; CASE 16A: closing a lambda defun or an in-expression
2424 (goto-char containing-sexp)
2425 (setq placeholder (c-looking-at-inexpr-block)))
2426 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
2429 (goto-char containing-sexp)
2430 (back-to-indentation)
2431 (if (= containing-sexp (point))
2432 (c-add-syntax tmpsymbol (point))
2433 (goto-char (cdr placeholder))
2434 (back-to-indentation)
2435 (c-add-syntax tmpsymbol (point))
2436 (if (/= (point) (cdr placeholder))
2437 (c-add-syntax (car placeholder)))))
2438 ;; CASE 16B: does this close an inline or a function in
2439 ;; an extern block or namespace?
2441 (goto-char containing-sexp)
2442 (setq placeholder (c-search-uplist-for-classkey state)))
2443 (goto-char (aref placeholder 0))
2444 (if (looking-at (concat c-extra-toplevel-key "[^_]"))
2445 (c-add-syntax 'defun-close relpos)
2446 (c-add-syntax 'inline-close relpos)))
2447 ;; CASE 16C: if there an enclosing brace that hasn't
2448 ;; been narrowed out by a class, then this is a
2450 ((and (not inenclosing-p)
2451 (c-most-enclosing-brace state)
2452 (or (not (c-major-mode-is 'pike-mode))
2453 ;; In Pike it can be a defun-close of a
2454 ;; function declared in a statement block. Let
2455 ;; it through to be handled below.
2456 (or (c-looking-at-bos)
2458 (c-beginning-of-statement-1)
2459 (looking-at c-conditional-key)))))
2460 (c-add-syntax 'block-close relpos))
2461 ;; CASE 16D: find out whether we're closing a top-level
2465 (narrow-to-region (point-min) indent-point)
2466 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
2468 (c-add-class-syntax 'class-close decl)
2469 (c-add-syntax 'defun-close relpos)))))
2471 ;; CASE 17: statement catchall
2473 ;; we know its a statement, but we need to find out if it is
2474 ;; the first statement in a block
2475 (goto-char containing-sexp)
2477 (c-forward-syntactic-ws indent-point)
2478 ;; now skip forward past any case/default clauses we might find.
2479 (while (or (c-skip-case-statement-forward fullstate indent-point)
2480 (and (looking-at c-switch-label-key)
2482 (setq inswitch-p t))
2483 ;; we want to ignore non-case labels when skipping forward
2484 (while (and (looking-at c-label-key)
2485 (goto-char (match-end 0)))
2486 (c-forward-syntactic-ws indent-point))
2488 ;; CASE 17A: we are inside a case/default clause inside a
2489 ;; switch statement. find out if we are at the statement
2490 ;; just after the case/default label.
2493 (goto-char indent-point)
2494 (c-beginning-of-statement-1 containing-sexp)
2495 (setq placeholder (point))
2497 (when (re-search-forward c-switch-label-key
2498 (max placeholder (c-point 'eol)) t)
2499 (setq placeholder (match-beginning 0)))))
2500 (goto-char indent-point)
2501 (skip-chars-forward " \t")
2502 (if (eq (char-after) ?{)
2503 (c-add-syntax 'statement-case-open placeholder)
2504 (c-add-syntax 'statement-case-intro placeholder)))
2505 ;; CASE 17B: continued statement
2506 ((eq char-before-ip ?,)
2507 (goto-char indent-point)
2508 (c-beginning-of-closest-statement)
2509 (c-add-syntax 'statement-cont (c-point 'boi)))
2510 ;; CASE 17C: a question/colon construct? But make sure
2511 ;; what came before was not a label, and what comes after
2512 ;; is not a globally scoped function call!
2513 ((or (and (memq char-before-ip '(?: ??))
2515 (goto-char indent-point)
2516 (c-backward-syntactic-ws lim)
2517 (back-to-indentation)
2518 (not (looking-at c-label-key))))
2519 (and (memq char-after-ip '(?: ??))
2521 (goto-char indent-point)
2522 (skip-chars-forward " \t")
2523 ;; watch out for scope operator
2524 (not (looking-at "::")))))
2525 (goto-char indent-point)
2526 (c-beginning-of-closest-statement)
2527 (c-add-syntax 'statement-cont (c-point 'boi)))
2528 ;; CASE 17D: any old statement
2529 ((< (point) indent-point)
2530 (let ((safepos (c-most-enclosing-brace fullstate))
2532 (goto-char indent-point)
2533 (c-beginning-of-statement-1 safepos)
2534 ;; It is possible we're on the brace that opens a nested
2536 (if (and (eq (char-after) ?{)
2538 (c-backward-syntactic-ws safepos)
2539 (not (eq (char-before) ?\;))))
2540 (c-beginning-of-statement-1 safepos))
2542 (looking-at c-switch-label-key))
2544 (goto-char (match-end 0))
2545 (c-forward-syntactic-ws)))
2546 (setq relpos (c-point 'boi))
2547 (while (and (not done)
2548 (<= safepos (point))
2549 (/= relpos (point)))
2550 (c-beginning-of-statement-1 safepos)
2551 (if (= relpos (c-point 'boi))
2553 (setq relpos (c-point 'boi)))
2554 (c-add-syntax 'statement relpos)
2555 (if (eq char-after-ip ?{)
2556 (c-add-syntax 'block-open))))
2557 ;; CASE 17E: first statement in an in-expression block
2560 (goto-char containing-sexp)
2561 (c-looking-at-inexpr-block)))
2562 (goto-char containing-sexp)
2563 (back-to-indentation)
2564 (let ((block-intro (if (eq (car placeholder) 'inlambda)
2566 'statement-block-intro)))
2567 (if (= containing-sexp (point))
2568 (c-add-syntax block-intro (point))
2569 (goto-char (cdr placeholder))
2570 (back-to-indentation)
2571 (c-add-syntax block-intro (point))
2572 (if (/= (point) (cdr placeholder))
2573 (c-add-syntax (car placeholder)))))
2574 (if (eq char-after-ip ?{)
2575 (c-add-syntax 'block-open)))
2576 ;; CASE 17F: first statement in an inline, or first
2577 ;; statement in a top-level defun. we can tell this is it
2578 ;; if there are no enclosing braces that haven't been
2579 ;; narrowed out by a class (i.e. don't use bod here!)
2583 (goto-char containing-sexp)
2584 (c-narrow-out-enclosing-class state containing-sexp)
2585 (not (c-most-enclosing-brace state))))
2586 (goto-char containing-sexp)
2587 ;; if not at boi, then defun-opening braces are hung on
2588 ;; right side, so we need a different relpos
2589 (if (/= (point) (c-point 'boi))
2591 (c-backward-syntactic-ws)
2592 (c-safe (c-forward-sexp (if (eq (char-before) ?\))
2594 ;; looking at a Java throws clause following a
2595 ;; method's parameter list
2596 (c-beginning-of-statement-1)
2598 (c-add-syntax 'defun-block-intro (c-point 'boi)))
2599 ;; CASE 17G: First statement in a function declared inside
2600 ;; a normal block. This can only occur in Pike.
2601 ((and (c-major-mode-is 'pike-mode)
2603 (goto-char containing-sexp)
2604 (and (not (c-looking-at-bos))
2606 (c-beginning-of-statement-1)
2607 (not (looking-at c-conditional-key))))))
2608 (c-add-syntax 'defun-block-intro (c-point 'boi)))
2609 ;; CASE 17H: first statement in a block
2610 (t (goto-char containing-sexp)
2611 (if (/= (point) (c-point 'boi))
2612 (c-beginning-of-statement-1
2614 (c-safe-position (point) state) lim)))
2615 (c-add-syntax 'statement-block-intro (c-point 'boi))
2616 (if (eq char-after-ip ?{)
2617 (c-add-syntax 'block-open)))
2620 ;; now we need to look at any modifiers
2621 (goto-char indent-point)
2622 (skip-chars-forward " \t")
2624 ;; are we looking at a comment only line?
2625 ((and (looking-at c-comment-start-regexp)
2626 (/= (c-forward-token-1 0 nil (c-point 'eol)) 0))
2627 (c-add-syntax 'comment-intro))
2628 ;; we might want to give additional offset to friends (in C++).
2629 ((and (c-major-mode-is 'c++-mode)
2630 (looking-at c-C++-friend-key))
2631 (c-add-syntax 'friend))
2632 ;; Start of a preprocessor directive?
2633 ((and (eq literal 'pound)
2635 (c-beginning-of-macro lim)
2636 (setq placeholder (point)))
2638 (not (and (c-major-mode-is 'pike-mode)
2639 (eq (char-after (1+ placeholder)) ?\"))))
2640 (c-add-syntax 'cpp-macro)))
2641 ;; return the syntax
2645 (defun c-echo-parsing-error (&optional quiet)
2646 (when (and c-parsing-error (not quiet))
2647 (message "%s" c-parsing-error)
2651 (defun c-shift-line-indentation (shift-amt)
2652 (let ((pos (- (point-max) (point)))
2653 (col (current-indentation)))
2654 (if (zerop shift-amt)
2656 (delete-region (c-point 'bol) (c-point 'boi))
2658 (indent-to (+ col shift-amt)))
2659 (if (< (point) (c-point 'boi))
2660 (back-to-indentation)
2661 ;; If initial point was within line's indentation, position after
2662 ;; the indentation. Else stay at same point in text.
2663 (if (> (- (point-max) pos) (point))
2664 (goto-char (- (point-max) pos))))))
2666 (defun c-evaluate-offset (offset langelem symbol)
2667 ;; offset can be a number, a function, a variable, a list, or one of
2668 ;; the symbols + or -
2670 ((eq offset '+) c-basic-offset)
2671 ((eq offset '-) (- c-basic-offset))
2672 ((eq offset '++) (* 2 c-basic-offset))
2673 ((eq offset '--) (* 2 (- c-basic-offset)))
2674 ((eq offset '*) (/ c-basic-offset 2))
2675 ((eq offset '/) (/ (- c-basic-offset) 2))
2676 ((numberp offset) offset)
2677 ((functionp offset) (c-evaluate-offset
2678 (funcall offset langelem) langelem symbol))
2679 ((vectorp offset) offset)
2683 (while (and (not done) offset)
2684 (setq done (c-evaluate-offset (car offset) langelem symbol)
2685 offset (cdr offset)))
2687 (if c-strict-syntax-p
2688 (error "No offset found for syntactic symbol %s" symbol))
2690 (t (symbol-value offset))
2693 (defun c-get-offset (langelem)
2694 ;; Get offset from LANGELEM which is a cons cell of the form:
2695 ;; (SYMBOL . RELPOS). The symbol is matched against
2696 ;; c-offsets-alist and the offset found there is either returned,
2697 ;; or added to the indentation at RELPOS. If RELPOS is nil, then
2698 ;; the offset is simply returned.
2699 (let* ((symbol (car langelem))
2700 (relpos (cdr langelem))
2701 (match (assq symbol c-offsets-alist))
2702 (offset (cdr-safe match)))
2704 (if c-strict-syntax-p
2705 (error "No offset found for syntactic symbol %s" symbol)
2708 (setq offset (c-evaluate-offset offset langelem symbol)))
2709 (if (vectorp offset)
2712 (< relpos (c-point 'bol)))
2717 (or (and (numberp offset) offset)
2718 (and (symbolp offset) (symbol-value offset))
2722 (defun c-get-syntactic-indentation (langelems)
2723 ;; Apply c-get-offset to a list of langelem cells to get the total
2724 ;; syntactic indentation. Special treatment is needed for vectors
2725 ;; containing absolute columns.
2729 (let ((res (c-get-offset (car langelems))))
2731 (throw 'done (elt res 0))
2732 (setq indent (+ indent res)
2733 langelems (cdr langelems)))))
2736 (defun c-indent-line (&optional syntax quiet)
2737 ;; Indent the current line according to the syntactic context, if
2738 ;; c-syntactic-indentation is non-nil. Optional SYNTAX is the
2739 ;; syntactic information for the current line. Be silent about
2740 ;; syntactic errors if the optional argument QUIET is non-nil.
2741 ;; Returns the amount of indentation change (in columns).
2743 (if c-syntactic-indentation
2744 (setq c-parsing-error
2745 (or (let* ((c-parsing-error nil)
2746 (c-syntactic-context (or syntax
2748 (c-guess-basic-syntax)))
2749 (indent (c-get-syntactic-indentation c-syntactic-context)))
2750 (and (not (c-echo-parsing-error quiet))
2751 c-echo-syntactic-information-p
2752 (message "syntax: %s, indent: %d"
2753 c-syntactic-context indent))
2754 (setq shift-amt (- indent (current-indentation)))
2755 (c-shift-line-indentation shift-amt)
2756 (run-hooks 'c-special-indent-hook)
2761 (while (and (= (forward-line -1) 0)
2762 (if (looking-at "\\s-*$")
2764 (back-to-indentation)
2765 (setq indent (current-indentation))
2767 (setq shift-amt (- indent (current-indentation)))
2768 (c-shift-line-indentation shift-amt)))
2771 (defun c-show-syntactic-information (arg)
2772 "Show syntactic information for current line.
2773 With universal argument, inserts the analysis as a comment on that line."
2775 (let ((syntax (c-guess-basic-syntax)))
2776 (if (not (consp arg))
2777 (message "syntactic analysis: %s" syntax)
2778 (indent-for-comment)
2779 (insert (format "%s" syntax))
2781 (c-keep-region-active))
2783 (defun c-syntactic-information-on-region (from to)
2784 "Inserts a comment with the syntactic analysis on every line in the region."
2788 (narrow-to-region from to)
2789 (goto-char (point-min))
2791 (c-show-syntactic-information '(0))
2795 (cc-provide 'cc-engine)
2797 ;;; cc-engine.el ends here