1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
7 ;; Authors: 2001- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs and Stewart Clamen
11 ;; 1985 Richard M. Stallman
12 ;; Maintainer: bug-cc-mode@gnu.org
13 ;; Created: 22-Apr-1997 (split from cc-mode.el)
14 ;; Version: See cc-mode.el
15 ;; Keywords: c languages oop
17 ;; This file is part of GNU Emacs.
19 ;; GNU Emacs is free software: you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation, either version 3 of the License, or
22 ;; (at your option) any later version.
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
34 ;; The functions which have docstring documentation can be considered
35 ;; part of an API which other packages can use in CC Mode buffers.
36 ;; Otoh, undocumented functions and functions with the documentation
37 ;; in comments are considered purely internal and can change semantics
38 ;; or even disappear in the future.
40 ;; (This policy applies to CC Mode as a whole, not just this file. It
41 ;; probably also applies to many other Emacs packages, but here it's
42 ;; clearly spelled out.)
44 ;; Hidden buffer changes
46 ;; Various functions in CC Mode use text properties for caching and
47 ;; syntactic markup purposes, and those of them that might modify such
48 ;; properties but still don't modify the buffer in a visible way are
49 ;; said to do "hidden buffer changes". They should be used within
50 ;; `c-save-buffer-state' or a similar function that saves and restores
51 ;; buffer modifiedness, disables buffer change hooks, etc.
53 ;; Interactive functions are assumed to not do hidden buffer changes,
54 ;; except in the specific parts of them that do real changes.
56 ;; Lineup functions are assumed to do hidden buffer changes. They
57 ;; must not do real changes, though.
59 ;; All other functions that do hidden buffer changes have that noted
60 ;; in their doc string or comment.
62 ;; The intention with this system is to avoid wrapping every leaf
63 ;; function that do hidden buffer changes inside
64 ;; `c-save-buffer-state'. It should be used as near the top of the
65 ;; interactive functions as possible.
67 ;; Functions called during font locking are allowed to do hidden
68 ;; buffer changes since the font-lock package run them in a context
69 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
70 ;; inspired by `save-buffer-state' in the font-lock package).
72 ;; Use of text properties
74 ;; CC Mode uses several text properties internally to mark up various
75 ;; positions, e.g. to improve speed and to eliminate glitches in
76 ;; interactive refontification.
78 ;; Note: This doc is for internal use only. Other packages should not
79 ;; assume that these text properties are used as described here.
82 ;; Used to modify the syntax of some characters. It is used to
83 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
84 ;; to "hide" obtrusive characters in preprocessor lines.
86 ;; This property is used on single characters and is therefore
87 ;; always treated as front and rear nonsticky (or start and end open
88 ;; in XEmacs vocabulary). It's therefore installed on
89 ;; `text-property-default-nonsticky' if that variable exists (Emacs
92 ;; 'c-is-sws and 'c-in-sws
93 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
94 ;; speed them up. See the comment blurb before `c-put-is-sws'
95 ;; below for further details.
98 ;; This property is used on single characters to mark positions with
99 ;; special syntactic relevance of various sorts. Its primary use is
100 ;; to avoid glitches when multiline constructs are refontified
101 ;; interactively (on font lock decoration level 3). It's cleared in
102 ;; a region before it's fontified and is then put on relevant chars
103 ;; in that region as they are encountered during the fontification.
104 ;; The value specifies the kind of position:
107 ;; Put on the last char of the token preceding each declaration
108 ;; inside a declaration style arglist (typically in a function
112 ;; Put on the last char of the token preceding a declaration.
113 ;; This is used in cases where declaration boundaries can't be
114 ;; recognized simply by looking for a token like ";" or "}".
115 ;; `c-type-decl-end-used' must be set if this is used (see also
116 ;; `c-find-decl-spots').
119 ;; Put on the commas that separate arguments in angle bracket
120 ;; arglists like C++ template arglists.
122 ;; 'c-decl-id-start and 'c-decl-type-start
123 ;; Put on the last char of the token preceding each declarator
124 ;; in the declarator list of a declaration. They are also used
125 ;; between the identifiers cases like enum declarations.
126 ;; 'c-decl-type-start is used when the declarators are types,
127 ;; 'c-decl-id-start otherwise.
130 ;; Used in AWK mode to mark the various kinds of newlines. See
137 (if (and (boundp 'byte-compile-dest-file)
138 (stringp byte-compile-dest-file))
139 (cons (file-name-directory byte-compile-dest-file) load-path)
141 (load "cc-bytecomp" nil t)))
143 (cc-require 'cc-defs)
144 (cc-require-when-compile 'cc-langs)
145 (cc-require 'cc-vars)
147 ;; Silence the compiler.
148 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
151 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
153 (defmacro c-declare-lang-variables ()
156 (mapcar (lambda (init)
158 `(defvar ,(car init) nil ,(elt init 2))
159 `(defvar ,(car init) nil))
160 (make-variable-buffer-local ',(car init))))
161 (cdr c-lang-variable-inits)))))
162 (c-declare-lang-variables)
165 ;;; Internal state variables.
167 ;; Internal state of hungry delete key feature
168 (defvar c-hungry-delete-key nil)
169 (make-variable-buffer-local 'c-hungry-delete-key)
171 ;; The electric flag (toggled by `c-toggle-electric-state').
172 ;; If t, electric actions (like automatic reindentation, and (if
173 ;; c-auto-newline is also set) auto newlining) will happen when an electric
174 ;; key like `{' is pressed (or an electric keyword like `else').
175 (defvar c-electric-flag t)
176 (make-variable-buffer-local 'c-electric-flag)
178 ;; Internal state of auto newline feature.
179 (defvar c-auto-newline nil)
180 (make-variable-buffer-local 'c-auto-newline)
182 ;; Included in the mode line to indicate the active submodes.
183 ;; (defvar c-submode-indicators nil)
184 ;; (make-variable-buffer-local 'c-submode-indicators)
186 (defun c-calculate-state (arg prevstate)
187 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
188 ;; arg is nil or zero, toggle the state. If arg is negative, turn
189 ;; the state off, and if arg is positive, turn the state on
191 (zerop (setq arg (prefix-numeric-value arg))))
195 ;; Dynamically bound cache for `c-in-literal'.
196 (defvar c-in-literal-cache t)
199 ;; Basic handling of preprocessor directives.
201 ;; This is a dynamically bound cache used together with
202 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
203 ;; works as long as point doesn't cross a macro boundary.
204 (defvar c-macro-start 'unknown)
206 (defsubst c-query-and-set-macro-start ()
207 (if (symbolp c-macro-start)
208 (setq c-macro-start (save-excursion
209 (c-save-buffer-state ()
210 (and (c-beginning-of-macro)
214 (defsubst c-query-macro-start ()
215 (if (symbolp c-macro-start)
217 (c-save-buffer-state ()
218 (and (c-beginning-of-macro)
222 (defun c-beginning-of-macro (&optional lim)
223 "Go to the beginning of a preprocessor directive.
224 Leave point at the beginning of the directive and return t if in one,
225 otherwise return nil and leave point unchanged.
227 Note that this function might do hidden buffer changes. See the
228 comment at the start of cc-engine.el for more info."
229 (when c-opt-cpp-prefix
230 (let ((here (point)))
232 (if lim (narrow-to-region lim (point-max)))
234 (while (eq (char-before (1- (point))) ?\\)
236 (back-to-indentation)
237 (if (and (<= (point) here)
238 (looking-at c-opt-cpp-start))
243 (defun c-end-of-macro ()
244 "Go to the end of a preprocessor directive.
245 More accurately, move the point to the end of the closest following
246 line that doesn't end with a line continuation backslash - no check is
247 done that the point is inside a cpp directive to begin with.
249 Note that this function might do hidden buffer changes. See the
250 comment at the start of cc-engine.el for more info."
253 (when (and (eq (char-before) ?\\)
258 (defun c-forward-over-cpp-define-id ()
259 ;; Assuming point is at the "#" that introduces a preprocessor
260 ;; directive, it's moved forward to the end of the identifier which is
261 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
262 ;; is returned in this case, in all other cases nil is returned and
263 ;; point isn't moved.
265 ;; This function might do hidden buffer changes.
266 (when (and c-opt-cpp-macro-define-id
267 (looking-at c-opt-cpp-macro-define-id))
268 (goto-char (match-end 0))))
270 (defun c-forward-to-cpp-define-body ()
271 ;; Assuming point is at the "#" that introduces a preprocessor
272 ;; directive, it's moved forward to the start of the definition body
273 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
274 ;; specifies). Non-nil is returned in this case, in all other cases
275 ;; nil is returned and point isn't moved.
277 ;; This function might do hidden buffer changes.
278 (when (and c-opt-cpp-macro-define-start
279 (looking-at c-opt-cpp-macro-define-start)
280 (not (= (match-end 0) (c-point 'eol))))
281 (goto-char (match-end 0))))
284 ;;; Basic utility functions.
286 (defun c-syntactic-content (from to paren-level)
287 ;; Return the given region as a string where all syntactic
288 ;; whitespace is removed or, where necessary, replaced with a single
289 ;; space. If PAREN-LEVEL is given then all parens in the region are
290 ;; collapsed to "()", "[]" etc.
292 ;; This function might do hidden buffer changes.
296 (narrow-to-region from to)
298 (let* ((parts (list nil)) (tail parts) pos in-paren)
300 (while (re-search-forward c-syntactic-ws-start to t)
301 (goto-char (setq pos (match-beginning 0)))
302 (c-forward-syntactic-ws)
308 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
311 (if (and (> pos from)
313 (looking-at "\\w\\|\\s_")
316 (looking-at "\\w\\|\\s_")))
318 (setcdr tail (list (buffer-substring-no-properties from pos)
320 (setq tail (cddr tail)))
321 (setcdr tail (list (buffer-substring-no-properties from pos)))
322 (setq tail (cdr tail)))
325 (when (= (car (parse-partial-sexp pos to -1)) -1)
326 (setcdr tail (list (buffer-substring-no-properties
327 (1- (point)) (point))))
328 (setq tail (cdr tail))))
330 (setq from (point))))
332 (setcdr tail (list (buffer-substring-no-properties from to)))
333 (apply 'concat (cdr parts))))))
335 (defun c-shift-line-indentation (shift-amt)
336 ;; Shift the indentation of the current line with the specified
337 ;; amount (positive inwards). The buffer is modified only if
338 ;; SHIFT-AMT isn't equal to zero.
339 (let ((pos (- (point-max) (point)))
340 (c-macro-start c-macro-start)
342 (if (zerop shift-amt)
344 ;; If we're on an empty line inside a macro, we take the point
345 ;; to be at the current indentation and shift it to the
346 ;; appropriate column. This way we don't treat the extra
347 ;; whitespace out to the line continuation as indentation.
348 (when (and (c-query-and-set-macro-start)
349 (looking-at "[ \t]*\\\\$")
351 (skip-chars-backward " \t")
355 (setq tmp-char-inserted t))
357 (let ((col (current-indentation)))
358 (delete-region (c-point 'bol) (c-point 'boi))
360 (indent-to (+ col shift-amt)))
361 (when tmp-char-inserted
363 ;; If initial point was within line's indentation and we're not on
364 ;; a line with a line continuation in a macro, position after the
365 ;; indentation. Else stay at same point in text.
366 (if (and (< (point) (c-point 'boi))
367 (not tmp-char-inserted))
368 (back-to-indentation)
369 (if (> (- (point-max) pos) (point))
370 (goto-char (- (point-max) pos))))))
372 (defsubst c-keyword-sym (keyword)
373 ;; Return non-nil if the string KEYWORD is a known keyword. More
374 ;; precisely, the value is the symbol for the keyword in
375 ;; `c-keywords-obarray'.
376 (intern-soft keyword c-keywords-obarray))
378 (defsubst c-keyword-member (keyword-sym lang-constant)
379 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
380 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
381 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
382 ;; nil then the result is nil.
383 (get keyword-sym lang-constant))
385 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
386 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
390 ;; Regexp matching string limit syntax.
391 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
395 ;; Regexp matching WS followed by string limit syntax.
396 (defconst c-ws*-string-limit-regexp
397 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
399 ;; Holds formatted error strings for the few cases where parse errors
401 (defvar c-parsing-error nil)
402 (make-variable-buffer-local 'c-parsing-error)
404 (defun c-echo-parsing-error (&optional quiet)
405 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
406 (c-benign-error "%s" c-parsing-error))
409 ;; Faces given to comments and string literals. This is used in some
410 ;; situations to speed up recognition; it isn't mandatory that font
411 ;; locking is in use. This variable is extended with the face in
412 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
413 (defvar c-literal-faces
414 (append '(font-lock-comment-face font-lock-string-face)
415 (when (facep 'font-lock-comment-delimiter-face)
417 '(font-lock-comment-delimiter-face))))
419 (defsubst c-put-c-type-property (pos value)
420 ;; Put a c-type property with the given value at POS.
421 (c-put-char-property pos 'c-type value))
423 (defun c-clear-c-type-property (from to value)
424 ;; Remove all occurences of the c-type property that has the given
425 ;; value in the region between FROM and TO. VALUE is assumed to not
428 ;; Note: This assumes that c-type is put on single chars only; it's
429 ;; very inefficient if matching properties cover large regions.
433 (when (eq (get-text-property (point) 'c-type) value)
434 (c-clear-char-property (point) 'c-type))
435 (goto-char (next-single-property-change (point) 'c-type nil to))
439 ;; Some debug tools to visualize various special positions. This
440 ;; debug code isn't as portable as the rest of CC Mode.
442 (cc-bytecomp-defun overlays-in)
443 (cc-bytecomp-defun overlay-get)
444 (cc-bytecomp-defun overlay-start)
445 (cc-bytecomp-defun overlay-end)
446 (cc-bytecomp-defun delete-overlay)
447 (cc-bytecomp-defun overlay-put)
448 (cc-bytecomp-defun make-overlay)
450 (defun c-debug-add-face (beg end face)
451 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
453 (setq overlay (car overlays)
454 overlays (cdr overlays))
455 (when (eq (overlay-get overlay 'face) face)
456 (setq beg (min beg (overlay-start overlay))
457 end (max end (overlay-end overlay)))
458 (delete-overlay overlay)))
459 (overlay-put (make-overlay beg end) 'face face)))
461 (defun c-debug-remove-face (beg end face)
462 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
463 (ol-beg beg) (ol-end end))
465 (setq overlay (car overlays)
466 overlays (cdr overlays))
467 (when (eq (overlay-get overlay 'face) face)
468 (setq ol-beg (min ol-beg (overlay-start overlay))
469 ol-end (max ol-end (overlay-end overlay)))
470 (delete-overlay overlay)))
472 (overlay-put (make-overlay ol-beg beg) 'face face))
474 (overlay-put (make-overlay end ol-end) 'face face))))
477 ;; `c-beginning-of-statement-1' and accompanying stuff.
479 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
480 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
481 ;; better way should be implemented, but this will at least shut up
482 ;; the byte compiler.
483 (defvar c-maybe-labelp)
485 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
487 ;; Macros used internally in c-beginning-of-statement-1 for the
488 ;; automaton actions.
489 (defmacro c-bos-push-state ()
490 '(setq stack (cons (cons state saved-pos)
492 (defmacro c-bos-pop-state (&optional do-if-done)
493 `(if (setq state (car (car stack))
494 saved-pos (cdr (car stack))
499 (defmacro c-bos-pop-state-and-retry ()
500 '(throw 'loop (setq state (car (car stack))
501 saved-pos (cdr (car stack))
502 ;; Throw nil if stack is empty, else throw non-nil.
504 (defmacro c-bos-save-pos ()
505 '(setq saved-pos (vector pos tok ptok pptok)))
506 (defmacro c-bos-restore-pos ()
507 '(unless (eq (elt saved-pos 0) start)
508 (setq pos (elt saved-pos 0)
509 tok (elt saved-pos 1)
510 ptok (elt saved-pos 2)
511 pptok (elt saved-pos 3))
514 (defmacro c-bos-save-error-info (missing got)
515 `(setq saved-pos (vector pos ,missing ,got)))
516 (defmacro c-bos-report-error ()
518 (setq c-parsing-error
519 (format "No matching `%s' found for `%s' on line %d"
522 (1+ (count-lines (point-min)
523 (c-point 'bol (elt saved-pos 0))))))))
525 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
527 "Move to the start of the current statement or declaration, or to
528 the previous one if already at the beginning of one. Only
529 statements/declarations on the same level are considered, i.e. don't
530 move into or out of sexps (not even normal expression parentheses).
532 Stop at statement continuation tokens like \"else\", \"catch\",
533 \"finally\" and the \"while\" in \"do ... while\" if the start point
534 is within the continuation. If starting at such a token, move to the
535 corresponding statement start. If at the beginning of a statement,
536 move to the closest containing statement if there is any. This might
537 also stop at a continuation clause.
539 Labels are treated as part of the following statements if
540 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
541 statement start keyword.) Otherwise, each label is treated as a
544 Macros are ignored \(i.e. skipped over) unless point is within one, in
545 which case the content of the macro is treated as normal code. Aside
546 from any normal statement starts found in it, stop at the first token
547 of the content in the macro, i.e. the expression of an \"#if\" or the
548 start of the definition in a \"#define\". Also stop at start of
549 macros before leaving them.
551 Return 'label if stopped at a label, 'same if stopped at the beginning
552 of the current statement, 'up if stepped to a containing statement,
553 'previous if stepped to a preceding statement, 'beginning if stepped
554 from a statement continuation clause to its start clause, or 'macro if
555 stepped to a macro start. Note that 'same and not 'label is returned
556 if stopped at the same label without crossing the colon character.
558 LIM may be given to limit the search. If the search hits the limit,
559 point will be left at the closest following token, or at the start
560 position if that is less ('same is returned in this case).
562 NOERROR turns off error logging to `c-parsing-error'.
564 Normally only ';' and virtual semicolons are considered to delimit
565 statements, but if COMMA-DELIM is non-nil then ',' is treated
568 Note that this function might do hidden buffer changes. See the
569 comment at the start of cc-engine.el for more info."
571 ;; The bulk of this function is a pushdown automaton that looks at statement
572 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
573 ;; purpose is to keep track of nested statements, ensuring that such
574 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
575 ;; does with nested braces/brackets/parentheses).
577 ;; Note: The position of a boundary is the following token.
579 ;; Beginning with the current token (the one following point), move back one
580 ;; sexp at a time (where a sexp is, more or less, either a token or the
581 ;; entire contents of a brace/bracket/paren pair). Each time a statement
582 ;; boundary is crossed or a "while"-like token is found, update the state of
583 ;; the PDA. Stop at the beginning of a statement when the stack (holding
584 ;; nested statement info) is empty and the position has been moved.
586 ;; The following variables constitute the PDA:
588 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
589 ;; scanned back over, 'boundary if we've just gone back over a
590 ;; statement boundary, or nil otherwise.
591 ;; state: takes one of the values (nil else else-boundary while
592 ;; while-boundary catch catch-boundary).
593 ;; nil means "no "while"-like token yet scanned".
594 ;; 'else, for example, means "just gone back over an else".
595 ;; 'else-boundary means "just gone back over a statement boundary
596 ;; immediately after having gone back over an else".
597 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
598 ;; of error reporting information.
599 ;; stack: The stack onto which the PDA pushes its state. Each entry
600 ;; consists of a saved value of state and saved-pos. An entry is
601 ;; pushed when we move back over a "continuation" token (e.g. else)
602 ;; and popped when we encounter the corresponding opening token
606 ;; The following diagram briefly outlines the PDA.
609 ;; "else": Push state, goto state `else'.
610 ;; "while": Push state, goto state `while'.
611 ;; "catch" or "finally": Push state, goto state `catch'.
612 ;; boundary: Pop state.
613 ;; other: Do nothing special.
616 ;; boundary: Goto state `else-boundary'.
617 ;; other: Error, pop state, retry token.
619 ;; State `else-boundary':
621 ;; boundary: Error, pop state.
622 ;; other: See common state.
625 ;; boundary: Save position, goto state `while-boundary'.
626 ;; other: Pop state, retry token.
628 ;; State `while-boundary':
630 ;; boundary: Restore position if it's not at start, pop state. [*see below]
631 ;; other: See common state.
634 ;; boundary: Goto state `catch-boundary'.
635 ;; other: Error, pop state, retry token.
637 ;; State `catch-boundary':
639 ;; "catch": Goto state `catch'.
640 ;; boundary: Error, pop state.
641 ;; other: See common state.
643 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
644 ;; searching for a "do" which would have opened a do-while. If we didn't
645 ;; find it, we discard the analysis done since the "while", go back to this
646 ;; token in the buffer and restart the scanning there, this time WITHOUT
647 ;; pushing the 'while state onto the stack.
649 ;; In addition to the above there is some special handling of labels
652 (let ((case-fold-search nil)
655 (delims (if comma-delim '(?\; ?,) '(?\;)))
656 (c-stmt-delim-chars (if comma-delim
657 c-stmt-delim-chars-with-comma
659 c-in-literal-cache c-maybe-labelp saved
662 ;; Position of last stmt boundary character (e.g. ;).
664 ;; The position of the last sexp or bound that follows the
665 ;; first found colon, i.e. the start of the nonlabel part of
666 ;; the statement. It's `start' if a colon is found just after
669 ;; Like `after-labels-pos', but the first such position inside
670 ;; a label, i.e. the start of the last label before the start
671 ;; of the nonlabel part of the statement.
673 ;; The last position where a label is possible provided the
674 ;; statement started there. It's nil as long as no invalid
675 ;; label content has been found (according to
676 ;; `c-nonlabel-token-key'. It's `start' if no valid label
677 ;; content was found in the label. Note that we might still
678 ;; regard it a label if it starts with `c-label-kwds'.
680 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
683 ;; Current state in the automaton. See above.
685 ;; Current saved positions. See above.
687 ;; Stack of conses (state . saved-pos).
689 ;; Regexp which matches "for", "if", etc.
690 (cond-key (or c-opt-block-stmt-key
691 "\\<\\>")) ; Matches nothing.
694 ;; Positions of the last three sexps or bounds we've stopped at.
698 (if lim (narrow-to-region lim (point-max)))
701 (and (c-beginning-of-macro)
703 (setq macro-start (point)))
705 ;; Try to skip back over unary operator characters, to register
709 (c-backward-syntactic-ws)
710 ;; Protect post-++/-- operators just before a virtual semicolon.
711 (and (not (c-at-vsemi-p))
712 (/= (skip-chars-backward "-+!*&~@`#") 0))))
714 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
715 ;; done. Later on we ignore the boundaries for statements that don't
716 ;; contain any sexp. The only thing that is affected is that the error
717 ;; checking is a little less strict, and we really don't bother.
718 (if (and (memq (char-before) delims)
719 (progn (forward-char -1)
721 (c-backward-syntactic-ws)
722 (or (memq (char-before) delims)
723 (memq (char-before) '(?: nil))
724 (eq (char-syntax (char-before)) ?\()
729 ;; Begin at start and not pos to detect macros if we stand
730 ;; directly after the #.
732 (if (looking-at "\\<\\|\\W")
733 ;; Record this as the first token if not starting inside it.
736 ;; The following while loop goes back one sexp (balanced parens,
737 ;; etc. with contents, or symbol or suchlike) each iteration. This
738 ;; movement is accomplished with a call to scan-sexps approx 130 lines
741 (catch 'loop ;; Throw nil to break, non-nil to continue.
744 (and macro-start ; Always NIL for AWK.
745 (progn (skip-chars-backward " \t")
746 (eq (char-before) ?#))
747 (progn (setq saved (1- (point)))
749 (not (eq (char-before (1- (point))) ?\\)))
750 (looking-at c-opt-cpp-start)
751 (progn (skip-chars-forward " \t")
752 (eq (point) saved))))
754 (if (and (c-forward-to-cpp-define-body)
755 (progn (c-forward-syntactic-ws start)
757 ;; Stop at the first token in the content of the macro.
759 ignore-labels t) ; Avoid the label check on exit.
765 ;; Do a round through the automaton if we've just passed a
766 ;; statement boundary or passed a "while"-like token.
768 (and (looking-at cond-key)
769 (setq sym (intern (match-string 1)))))
771 (when (and (< pos start) (null stack))
774 ;; The PDA state handling.
776 ;; Refer to the description of the PDA in the opening
777 ;; comments. In the following OR form, the first leaf
778 ;; attempts to handles one of the specific actions detailed
779 ;; (e.g., finding token "if" whilst in state `else-boundary').
780 ;; We drop through to the second leaf (which handles common
781 ;; state) if no specific handler is found in the first cond.
782 ;; If a parsing error is detected (e.g. an "else" with no
783 ;; preceding "if"), we throw to the enclosing catch.
785 ;; Note that the (eq state 'else) means
786 ;; "we've just passed an else", NOT "we're looking for an
790 (if (eq sym 'boundary)
791 (setq state 'else-boundary)
793 (c-bos-pop-state-and-retry)))
795 ((eq state 'else-boundary)
797 (c-bos-pop-state (setq ret 'beginning)))
803 (if (and (eq sym 'boundary)
804 ;; Since this can cause backtracking we do a
805 ;; little more careful analysis to avoid it:
806 ;; If there's a label in front of the while
807 ;; it can't be part of a do-while.
808 (not after-labels-pos))
809 (progn (c-bos-save-pos)
810 (setq state 'while-boundary))
811 (c-bos-pop-state-and-retry))) ; Can't be a do-while
813 ((eq state 'while-boundary)
815 (c-bos-pop-state (setq ret 'beginning)))
816 ((eq sym 'boundary) ; isn't a do-while
817 (c-bos-restore-pos) ; the position of the while
818 (c-bos-pop-state)))) ; no longer searching for do.
821 (if (eq sym 'boundary)
822 (setq state 'catch-boundary)
824 (c-bos-pop-state-and-retry)))
826 ((eq state 'catch-boundary)
829 (c-bos-pop-state (setq ret 'beginning)))
834 (c-bos-pop-state)))))
836 ;; This is state common. We get here when the previous
837 ;; cond statement found no particular state handler.
838 (cond ((eq sym 'boundary)
839 ;; If we have a boundary at the start
840 ;; position we push a frame to go to the
841 ;; previous statement.
847 (c-bos-save-error-info 'if 'else)
850 ;; Is this a real while, or a do-while?
851 ;; The next `when' triggers unless we are SURE that
852 ;; the `while' is not the tailend of a `do-while'.
853 (when (or (not pptok)
854 (memq (char-after pptok) delims)
855 ;; The following kludge is to prevent
856 ;; infinite recursion when called from
857 ;; c-awk-after-if-for-while-condition-p,
859 (and (eq (point) start)
860 (c-vsemi-status-unknown-p))
861 (c-at-vsemi-p pptok))
862 ;; Since this can cause backtracking we do a
863 ;; little more careful analysis to avoid it: If
864 ;; the while isn't followed by a (possibly
865 ;; virtual) semicolon it can't be a do-while.
867 (setq state 'while)))
868 ((memq sym '(catch finally))
870 (c-bos-save-error-info 'try sym)
871 (setq state 'catch))))
874 ;; We're either past a statement boundary or at the
875 ;; start of a statement, so throw away any label data
876 ;; for the previous one.
877 (setq after-labels-pos nil
879 c-maybe-labelp nil))))
881 ;; Step to the previous sexp, but not if we crossed a
882 ;; boundary, since that doesn't consume an sexp.
883 (if (eq sym 'boundary)
886 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
887 ;; BACKWARDS THROUGH THE SOURCE.
889 ;; This is typically fast with the caching done by
890 ;; c-(backward|forward)-sws.
891 (c-backward-syntactic-ws)
893 (let ((before-sws-pos (point))
894 ;; Set as long as we have to continue jumping by sexps.
895 ;; It's the position to use as end in the next round.
896 sexp-loop-continue-pos
897 ;; The end position of the area to search for statement
898 ;; barriers in this round.
899 (sexp-loop-end-pos pos))
901 ;; The following while goes back one sexp per iteration.
904 (unless (c-safe (c-backward-sexp) t)
905 ;; Give up if we hit an unbalanced block. Since the
906 ;; stack won't be empty the code below will report a
910 ;; Check if the sexp movement crossed a statement or
911 ;; declaration boundary. But first modify the point
912 ;; so that `c-crosses-statement-barrier-p' only looks
913 ;; at the non-sexp chars following the sexp.
921 (when (c-beginning-of-macro)
922 ;; Set continuation position in case
923 ;; `c-crosses-statement-barrier-p'
924 ;; doesn't detect anything below.
925 (setq sexp-loop-continue-pos (point)))))
926 ;; If the sexp movement took us into a
927 ;; macro then there were only some non-sexp
928 ;; chars after it. Skip out of the macro
929 ;; to analyze them but not the non-sexp
930 ;; chars that might be inside the macro.
932 (c-crosses-statement-barrier-p
933 (point) sexp-loop-end-pos))
937 (not (c-looking-at-inexpr-block lim nil t)))
938 ;; Passed a block sexp. That's a boundary
942 ((looking-at "\\s\(")
943 ;; Passed some other paren. Only analyze
944 ;; the non-sexp chars after it.
945 (goto-char (1+ (c-down-list-backward
947 ;; We're at a valid token start position
948 ;; (outside the `save-excursion') if
949 ;; `c-crosses-statement-barrier-p' failed.
950 (c-crosses-statement-barrier-p
951 (point) sexp-loop-end-pos))
954 ;; Passed a symbol sexp or line
955 ;; continuation. It doesn't matter that
956 ;; it's included in the analyzed region.
957 (if (c-crosses-statement-barrier-p
958 (point) sexp-loop-end-pos)
960 ;; If it was a line continuation then we
961 ;; have to continue looping.
962 (if (looking-at "\\\\$")
963 (setq sexp-loop-continue-pos (point)))
970 ;; Like a C "continue". Analyze the next sexp.
973 sexp-loop-continue-pos) ; End of "go back a sexp" loop.
974 (goto-char sexp-loop-continue-pos)
975 (setq sexp-loop-end-pos sexp-loop-continue-pos
976 sexp-loop-continue-pos nil))))
979 (when (and c-opt-method-key
980 (setq saved (c-in-method-def-p)))
982 ignore-labels t) ; Avoid the label check on exit.
986 (unless (eq ignore-labels t)
987 (when (numberp c-maybe-labelp)
988 ;; `c-crosses-statement-barrier-p' has found a colon, so we
989 ;; might be in a label now. Have we got a real label
990 ;; (including a case label) or something like C++'s "public:"?
991 (if (or (not (looking-at c-nonlabel-token-key)) ; proper label
992 (save-excursion ; e.g. "case 'a':" ?
993 (and (c-safe (c-backward-sexp) t)
994 (looking-at "\\<case\\>")))) ; FIXME!!! this is
995 ; wrong for AWK. 2006/1/14.
997 (if after-labels-pos ; Have we already encountered a label?
998 (if (not last-label-pos)
999 (setq last-label-pos (or tok start)))
1000 (setq after-labels-pos (or tok start)))
1001 (setq c-maybe-labelp t
1002 label-good-pos nil))
1003 (setq c-maybe-labelp nil))) ; bogus "label"
1005 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1007 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1008 ;; We're in a potential label and it's the first
1009 ;; time we've found something that isn't allowed in
1011 (setq label-good-pos (or tok start))))
1013 ;; We've moved back by a sexp, so update the token positions.
1018 pos tok))) ; Not nil (for the while loop).
1020 ;; If the stack isn't empty there might be errors to report.
1022 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1023 (c-bos-report-error))
1024 (setq saved-pos (cdr (car stack))
1027 (when (and (eq ret 'same)
1028 (not (memq sym '(boundary ignore nil))))
1029 ;; Need to investigate closer whether we've crossed
1030 ;; between a substatement and its containing statement.
1031 (if (setq saved (if (looking-at c-block-stmt-1-key)
1034 (cond ((> start saved) (setq pos saved))
1035 ((= start saved) (setq ret 'up)))))
1037 (when (and (not ignore-labels)
1038 (eq c-maybe-labelp t)
1039 (not (eq ret 'beginning))
1041 (or (not label-good-pos)
1042 (<= label-good-pos pos)
1044 (goto-char (if (and last-label-pos
1045 (< last-label-pos start))
1048 (looking-at c-label-kwds-regexp))))
1049 ;; We're in a label. Maybe we should step to the statement
1051 (if (< after-labels-pos start)
1052 (setq pos after-labels-pos)
1054 (if (and last-label-pos (< last-label-pos start))
1055 ;; Might have jumped over several labels. Go to the last one.
1056 (setq pos last-label-pos)))))
1058 ;; Skip over the unary operators that can start the statement.
1061 (c-backward-syntactic-ws)
1062 ;; protect AWK post-inc/decrement operators, etc.
1063 (and (not (c-at-vsemi-p (point)))
1064 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1069 (defun c-crosses-statement-barrier-p (from to)
1070 "Return non-nil if buffer positions FROM to TO cross one or more
1071 statement or declaration boundaries. The returned value is actually
1072 the position of the earliest boundary char. FROM must not be within
1073 a string or comment.
1075 The variable `c-maybe-labelp' is set to the position of the first `:' that
1076 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1077 single `?' is found, then `c-maybe-labelp' is cleared.
1079 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1080 regarded as having a \"virtual semicolon\" immediately after the last token on
1081 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1083 Note that this function might do hidden buffer changes. See the
1084 comment at the start of cc-engine.el for more info."
1085 (let ((skip-chars c-stmt-delim-chars)
1090 (while (progn (skip-chars-forward skip-chars to)
1093 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1094 (goto-char (cdr lit-range)))
1095 ((eq (char-after) ?:)
1097 (if (and (eq (char-after) ?:)
1099 ;; Ignore scope operators.
1101 (setq c-maybe-labelp (1- (point)))))
1102 ((eq (char-after) ??)
1103 ;; A question mark. Can't be a label, so stop
1104 ;; looking for more : and ?.
1105 (setq c-maybe-labelp nil
1106 skip-chars (substring c-stmt-delim-chars 0 -2)))
1107 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1108 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1110 (skip-chars-backward " \t" from)
1112 (throw 'done (point))
1114 (t (throw 'done (point)))))
1115 ;; In trailing space after an as yet undetected virtual semicolon?
1116 (c-backward-syntactic-ws from)
1117 (if (and (< (point) to)
1122 (defun c-at-statement-start-p ()
1123 "Return non-nil if the point is at the first token in a statement
1124 or somewhere in the syntactic whitespace before it.
1126 A \"statement\" here is not restricted to those inside code blocks.
1127 Any kind of declaration-like construct that occur outside function
1128 bodies is also considered a \"statement\".
1130 Note that this function might do hidden buffer changes. See the
1131 comment at the start of cc-engine.el for more info."
1136 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1138 (eq (char-before) ?})
1139 (and (eq (char-before) ?{)
1140 (not (and c-special-brace-lists
1141 (progn (backward-char)
1142 (c-looking-at-special-brace-list)))))
1143 (c-crosses-statement-barrier-p (point) end)))))
1145 (defun c-at-expression-start-p ()
1146 "Return non-nil if the point is at the first token in an expression or
1147 statement, or somewhere in the syntactic whitespace before it.
1149 An \"expression\" here is a bit different from the normal language
1150 grammar sense: It's any sequence of expression tokens except commas,
1151 unless they are enclosed inside parentheses of some kind. Also, an
1152 expression never continues past an enclosing parenthesis, but it might
1153 contain parenthesis pairs of any sort except braces.
1155 Since expressions never cross statement boundaries, this function also
1156 recognizes statement beginnings, just like `c-at-statement-start-p'.
1158 Note that this function might do hidden buffer changes. See the
1159 comment at the start of cc-engine.el for more info."
1163 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1165 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1167 (memq (char-before) '(?{ ?}))
1168 (save-excursion (backward-char)
1169 (looking-at "\\s("))
1170 (c-crosses-statement-barrier-p (point) end)))))
1173 ;; A set of functions that covers various idiosyncrasies in
1174 ;; implementations of `forward-comment'.
1176 ;; Note: Some emacsen considers incorrectly that any line comment
1177 ;; ending with a backslash continues to the next line. I can't think
1178 ;; of any way to work around that in a reliable way without changing
1179 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1180 ;; changing the syntax for backslash doesn't work since we must treat
1181 ;; escapes in string literals correctly.)
1183 (defun c-forward-single-comment ()
1184 "Move forward past whitespace and the closest following comment, if any.
1185 Return t if a comment was found, nil otherwise. In either case, the
1186 point is moved past the following whitespace. Line continuations,
1187 i.e. a backslashes followed by line breaks, are treated as whitespace.
1188 The line breaks that end line comments are considered to be the
1189 comment enders, so the point will be put on the beginning of the next
1190 line if it moved past a line comment.
1192 This function does not do any hidden buffer changes."
1194 (let ((start (point)))
1195 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1196 (goto-char (match-end 0)))
1198 (when (forward-comment 1)
1200 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1204 ;; Emacs includes the ending newline in a b-style (c++)
1205 ;; comment, but XEmacs doesn't. We depend on the Emacs
1206 ;; behavior (which also is symmetric).
1207 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1208 (condition-case nil (forward-char 1)))
1212 (defsubst c-forward-comments ()
1213 "Move forward past all following whitespace and comments.
1214 Line continuations, i.e. a backslashes followed by line breaks, are
1215 treated as whitespace.
1217 Note that this function might do hidden buffer changes. See the
1218 comment at the start of cc-engine.el for more info."
1221 ;; If forward-comment in at least XEmacs 21 is given a large
1222 ;; positive value, it'll loop all the way through if it hits
1224 (and (forward-comment 5)
1225 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1229 (when (looking-at "\\\\[\n\r]")
1233 (defun c-backward-single-comment ()
1234 "Move backward past whitespace and the closest preceding comment, if any.
1235 Return t if a comment was found, nil otherwise. In either case, the
1236 point is moved past the preceding whitespace. Line continuations,
1237 i.e. a backslashes followed by line breaks, are treated as whitespace.
1238 The line breaks that end line comments are considered to be the
1239 comment enders, so the point cannot be at the end of the same line to
1240 move over a line comment.
1242 This function does not do any hidden buffer changes."
1244 (let ((start (point)))
1245 ;; When we got newline terminated comments, forward-comment in all
1246 ;; supported emacsen so far will stop at eol of each line not
1247 ;; ending with a comment when moving backwards. This corrects for
1248 ;; that, and at the same time handles line continuations.
1250 (skip-chars-backward " \t\n\r\f\v")
1251 (and (looking-at "[\n\r]")
1252 (eq (char-before) ?\\)))
1256 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1257 ;; backwards at bob.
1260 ;; Leave point after the closest following newline if we've
1261 ;; backed up over any above, since forward-comment won't move
1262 ;; backward over a line comment if point is at the end of the
1264 (re-search-forward "\\=\\s *[\n\r]" start t)
1266 (if (if (forward-comment -1)
1268 ;; If forward-comment above succeeded and we're at eol
1269 ;; then the newline we moved over above didn't end a
1270 ;; line comment, so we give it another go.
1271 (forward-comment -1)
1274 ;; Emacs <= 20 and XEmacs move back over the closer of a
1275 ;; block comment that lacks an opener.
1276 (if (looking-at "\\*/")
1277 (progn (forward-char 2) nil)
1280 (defsubst c-backward-comments ()
1281 "Move backward past all preceding whitespace and comments.
1282 Line continuations, i.e. a backslashes followed by line breaks, are
1283 treated as whitespace. The line breaks that end line comments are
1284 considered to be the comment enders, so the point cannot be at the end
1285 of the same line to move over a line comment. Unlike
1286 c-backward-syntactic-ws, this function doesn't move back over
1287 preprocessor directives.
1289 Note that this function might do hidden buffer changes. See the
1290 comment at the start of cc-engine.el for more info."
1292 (let ((start (point)))
1294 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1295 ;; return t when moving backwards at bob.
1298 (if (forward-comment -1)
1299 (if (looking-at "\\*/")
1300 ;; Emacs <= 20 and XEmacs move back over the
1301 ;; closer of a block comment that lacks an opener.
1302 (progn (forward-char 2) nil)
1305 ;; XEmacs treats line continuations as whitespace but
1306 ;; only in the backward direction, which seems a bit
1307 ;; odd. Anyway, this is necessary for Emacs.
1308 (when (and (looking-at "[\n\r]")
1309 (eq (char-before) ?\\)
1315 ;; Tools for skipping over syntactic whitespace.
1317 ;; The following functions use text properties to cache searches over
1318 ;; large regions of syntactic whitespace. It works as follows:
1320 ;; o If a syntactic whitespace region contains anything but simple
1321 ;; whitespace (i.e. space, tab and line breaks), the text property
1322 ;; `c-in-sws' is put over it. At places where we have stopped
1323 ;; within that region there's also a `c-is-sws' text property.
1324 ;; That since there typically are nested whitespace inside that
1325 ;; must be handled separately, e.g. whitespace inside a comment or
1326 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1327 ;; to jump to another point with that property within the same
1328 ;; `c-in-sws' region. It can be likened to a ladder where
1329 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1331 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1332 ;; a "rung position" and also maybe on the first following char.
1333 ;; As many characters as can be conveniently found in this range
1334 ;; are marked, but no assumption can be made that the whole range
1335 ;; is marked (it could be clobbered by later changes, for
1338 ;; Note that some part of the beginning of a sequence of simple
1339 ;; whitespace might be part of the end of a preceding line comment
1340 ;; or cpp directive and must not be considered part of the "rung".
1341 ;; Such whitespace is some amount of horizontal whitespace followed
1342 ;; by a newline. In the case of cpp directives it could also be
1343 ;; two newlines with horizontal whitespace between them.
1345 ;; The reason to include the first following char is to cope with
1346 ;; "rung positions" that doesn't have any ordinary whitespace. If
1347 ;; `c-is-sws' is put on a token character it does not have
1348 ;; `c-in-sws' set simultaneously. That's the only case when that
1349 ;; can occur, and the reason for not extending the `c-in-sws'
1350 ;; region to cover it is that the `c-in-sws' region could then be
1351 ;; accidentally merged with a following one if the token is only
1352 ;; one character long.
1354 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1355 ;; removed in the changed region. If the change was inside
1356 ;; syntactic whitespace that means that the "ladder" is broken, but
1357 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1358 ;; parts on either side and use an ordinary search only to "repair"
1361 ;; Special care needs to be taken if a region is removed: If there
1362 ;; are `c-in-sws' on both sides of it which do not connect inside
1363 ;; the region then they can't be joined. If e.g. a marked macro is
1364 ;; broken, syntactic whitespace inside the new text might be
1365 ;; marked. If those marks would become connected with the old
1366 ;; `c-in-sws' range around the macro then we could get a ladder
1367 ;; with one end outside the macro and the other at some whitespace
1370 ;; The main motivation for this system is to increase the speed in
1371 ;; skipping over the large whitespace regions that can occur at the
1372 ;; top level in e.g. header files that contain a lot of comments and
1373 ;; cpp directives. For small comments inside code it's probably
1374 ;; slower than using `forward-comment' straightforwardly, but speed is
1375 ;; not a significant factor there anyway.
1377 ; (defface c-debug-is-sws-face
1378 ; '((t (:background "GreenYellow")))
1379 ; "Debug face to mark the `c-is-sws' property.")
1380 ; (defface c-debug-in-sws-face
1381 ; '((t (:underline t)))
1382 ; "Debug face to mark the `c-in-sws' property.")
1384 ; (defun c-debug-put-sws-faces ()
1385 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1386 ; ;; properties in the buffer.
1389 ; (c-save-buffer-state (in-face)
1390 ; (goto-char (point-min))
1391 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1394 ; (goto-char (next-single-property-change
1395 ; (point) 'c-is-sws nil (point-max)))
1398 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1399 ; (setq in-face nil))
1400 ; (setq in-face (point)))
1402 ; (goto-char (point-min))
1403 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1406 ; (goto-char (next-single-property-change
1407 ; (point) 'c-in-sws nil (point-max)))
1410 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1411 ; (setq in-face nil))
1412 ; (setq in-face (point)))
1415 (defmacro c-debug-sws-msg (&rest args)
1419 (defmacro c-put-is-sws (beg end)
1420 ;; This macro does a hidden buffer change.
1421 `(let ((beg ,beg) (end ,end))
1422 (put-text-property beg end 'c-is-sws t)
1423 ,@(when (facep 'c-debug-is-sws-face)
1424 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1426 (defmacro c-put-in-sws (beg end)
1427 ;; This macro does a hidden buffer change.
1428 `(let ((beg ,beg) (end ,end))
1429 (put-text-property beg end 'c-in-sws t)
1430 ,@(when (facep 'c-debug-is-sws-face)
1431 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1433 (defmacro c-remove-is-sws (beg end)
1434 ;; This macro does a hidden buffer change.
1435 `(let ((beg ,beg) (end ,end))
1436 (remove-text-properties beg end '(c-is-sws nil))
1437 ,@(when (facep 'c-debug-is-sws-face)
1438 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1440 (defmacro c-remove-in-sws (beg end)
1441 ;; This macro does a hidden buffer change.
1442 `(let ((beg ,beg) (end ,end))
1443 (remove-text-properties beg end '(c-in-sws nil))
1444 ,@(when (facep 'c-debug-is-sws-face)
1445 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1447 (defmacro c-remove-is-and-in-sws (beg end)
1448 ;; This macro does a hidden buffer change.
1449 `(let ((beg ,beg) (end ,end))
1450 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1451 ,@(when (facep 'c-debug-is-sws-face)
1452 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1453 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1455 (defsubst c-invalidate-sws-region-after (beg end)
1456 ;; Called from `after-change-functions'. Note that if
1457 ;; `c-forward-sws' or `c-backward-sws' are used outside
1458 ;; `c-save-buffer-state' or similar then this will remove the cache
1459 ;; properties right after they're added.
1461 ;; This function does hidden buffer changes.
1464 ;; Adjust the end to remove the properties in any following simple
1465 ;; ws up to and including the next line break, if there is any
1466 ;; after the changed region. This is necessary e.g. when a rung
1467 ;; marked empty line is converted to a line comment by inserting
1468 ;; "//" before the line break. In that case the line break would
1469 ;; keep the rung mark which could make a later `c-backward-sws'
1470 ;; move into the line comment instead of over it.
1472 (skip-chars-forward " \t\f\v")
1473 (when (and (eolp) (not (eobp)))
1474 (setq end (1+ (point)))))
1476 (when (and (= beg end)
1477 (get-text-property beg 'c-in-sws)
1479 (get-text-property (1- beg) 'c-in-sws))
1480 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1481 ;; safe to keep a range that was continuous before the change. E.g:
1487 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1488 ;; after "foo" is removed then "bar" will become part of the cpp
1489 ;; directive instead of a syntactically relevant token. In that
1490 ;; case there's no longer syntactic ws from "#" to "b".
1491 (setq beg (1- beg)))
1493 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1494 (c-remove-is-and-in-sws beg end))
1496 (defun c-forward-sws ()
1497 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1499 ;; This function might do hidden buffer changes.
1501 (let (;; `rung-pos' is set to a position as early as possible in the
1502 ;; unmarked part of the simple ws region.
1503 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1504 rung-is-marked next-rung-is-marked simple-ws-end
1505 ;; `safe-start' is set when it's safe to cache the start position.
1506 ;; It's not set if we've initially skipped over comments and line
1507 ;; continuations since we might have gone out through the end of a
1508 ;; macro then. This provision makes `c-forward-sws' not populate the
1509 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1513 ;; Skip simple ws and do a quick check on the following character to see
1514 ;; if it's anything that can't start syntactic ws, so we can bail out
1515 ;; early in the majority of cases when there just are a few ws chars.
1516 (skip-chars-forward " \t\n\r\f\v")
1517 (when (looking-at c-syntactic-ws-start)
1519 (setq rung-end-pos (min (1+ (point)) (point-max)))
1520 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1522 ;; Find the last rung position to avoid setting properties in all
1523 ;; the cases when the marked rung is complete.
1524 ;; (`next-single-property-change' is certain to move at least one
1526 (setq rung-pos (1- (next-single-property-change
1527 rung-is-marked 'c-is-sws nil rung-end-pos)))
1528 ;; Got no marked rung here. Since the simple ws might have started
1529 ;; inside a line comment or cpp directive we must set `rung-pos' as
1530 ;; high as possible.
1531 (setq rung-pos (point)))
1536 (when (and rung-is-marked
1537 (get-text-property (point) 'c-in-sws))
1539 ;; The following search is the main reason that `c-in-sws'
1540 ;; and `c-is-sws' aren't combined to one property.
1541 (goto-char (next-single-property-change
1542 (point) 'c-in-sws nil (point-max)))
1543 (unless (get-text-property (point) 'c-is-sws)
1544 ;; If the `c-in-sws' region extended past the last
1545 ;; `c-is-sws' char we have to go back a bit.
1546 (or (get-text-property (1- (point)) 'c-is-sws)
1547 (goto-char (previous-single-property-change
1548 (point) 'c-is-sws)))
1552 "c-forward-sws cached move %s -> %s (max %s)"
1553 rung-pos (point) (point-max))
1555 (setq rung-pos (point))
1556 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1559 ;; We'll loop here if there is simple ws after the last rung.
1560 ;; That means that there's been some change in it and it's
1561 ;; possible that we've stepped into another ladder, so extend
1562 ;; the previous one to join with it if there is one, and try to
1563 ;; use the cache again.
1565 "c-forward-sws extending rung with [%s..%s] (max %s)"
1566 (1+ rung-pos) (1+ (point)) (point-max))
1567 (unless (get-text-property (point) 'c-is-sws)
1568 ;; Remove any `c-in-sws' property from the last char of
1569 ;; the rung before we mark it with `c-is-sws', so that we
1570 ;; won't connect with the remains of a broken "ladder".
1571 (c-remove-in-sws (point) (1+ (point))))
1572 (c-put-is-sws (1+ rung-pos)
1574 (c-put-in-sws rung-pos
1575 (setq rung-pos (point)
1576 last-put-in-sws-pos rung-pos)))
1578 (setq simple-ws-end (point))
1579 (c-forward-comments)
1582 ((/= (point) simple-ws-end)
1583 ;; Skipped over comments. Don't cache at eob in case the buffer
1588 (and c-opt-cpp-prefix
1589 (looking-at c-opt-cpp-start)
1590 (progn (skip-chars-backward " \t")
1593 (progn (backward-char)
1594 (not (eq (char-before) ?\\))))))
1595 ;; Skip a preprocessor directive.
1597 (while (and (eq (char-before) ?\\)
1598 (= (forward-line 1) 0))
1602 ;; Don't cache at eob in case the buffer is narrowed.
1605 ;; We've searched over a piece of non-white syntactic ws. See if this
1607 (setq next-rung-pos (point))
1608 (skip-chars-forward " \t\n\r\f\v")
1609 (setq rung-end-pos (min (1+ (point)) (point-max)))
1612 ;; Cache if we haven't skipped comments only, and if we started
1613 ;; either from a marked rung or from a completely uncached
1617 (not (get-text-property simple-ws-end 'c-in-sws))))
1619 ;; See if there's a marked rung in the encountered simple ws. If
1620 ;; so then we can cache, unless `safe-start' is nil. Even then
1621 ;; we need to do this to check if the cache can be used for the
1623 (and (setq next-rung-is-marked
1624 (text-property-any next-rung-pos rung-end-pos
1630 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1631 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1634 ;; Remove the properties for any nested ws that might be cached.
1635 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1637 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1638 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1639 (c-put-is-sws rung-pos
1641 (setq rung-is-marked t))
1642 (c-put-in-sws rung-pos
1643 (setq rung-pos (point)
1644 last-put-in-sws-pos rung-pos))
1645 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1646 ;; Remove any `c-in-sws' property from the last char of
1647 ;; the rung before we mark it with `c-is-sws', so that we
1648 ;; won't connect with the remains of a broken "ladder".
1649 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1650 (c-put-is-sws next-rung-pos
1654 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1655 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1658 ;; Set `rung-pos' for the next rung. It's the same thing here as
1659 ;; initially, except that the rung position is set as early as
1660 ;; possible since we can't be in the ending ws of a line comment or
1661 ;; cpp directive now.
1662 (if (setq rung-is-marked next-rung-is-marked)
1663 (setq rung-pos (1- (next-single-property-change
1664 rung-is-marked 'c-is-sws nil rung-end-pos)))
1665 (setq rung-pos next-rung-pos))
1666 (setq safe-start t)))
1668 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1669 ;; another one after the point (which might occur when editing inside a
1670 ;; comment or macro).
1671 (when (eq last-put-in-sws-pos (point))
1672 (cond ((< last-put-in-sws-pos (point-max))
1674 "c-forward-sws clearing at %s for cache separation"
1675 last-put-in-sws-pos)
1676 (c-remove-in-sws last-put-in-sws-pos
1677 (1+ last-put-in-sws-pos)))
1679 ;; If at eob we have to clear the last character before the end
1680 ;; instead since the buffer might be narrowed and there might
1681 ;; be a `c-in-sws' after (point-max). In this case it's
1682 ;; necessary to clear both properties.
1684 "c-forward-sws clearing thoroughly at %s for cache separation"
1685 (1- last-put-in-sws-pos))
1686 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1687 last-put-in-sws-pos))))
1690 (defun c-backward-sws ()
1691 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1693 ;; This function might do hidden buffer changes.
1695 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1696 ;; part of the simple ws region.
1697 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1698 rung-is-marked simple-ws-beg cmt-skip-pos)
1700 ;; Skip simple horizontal ws and do a quick check on the preceding
1701 ;; character to see if it's anying that can't end syntactic ws, so we can
1702 ;; bail out early in the majority of cases when there just are a few ws
1703 ;; chars. Newlines are complicated in the backward direction, so we can't
1705 (skip-chars-backward " \t\f")
1706 (when (and (not (bobp))
1709 (looking-at c-syntactic-ws-end)))
1711 ;; Try to find a rung position in the simple ws preceding point, so that
1712 ;; we can get a cache hit even if the last bit of the simple ws has
1713 ;; changed recently.
1714 (setq simple-ws-beg (point))
1715 (skip-chars-backward " \t\n\r\f\v")
1716 (if (setq rung-is-marked (text-property-any
1717 (point) (min (1+ rung-pos) (point-max))
1719 ;; `rung-pos' will be the earliest marked position, which means that
1720 ;; there might be later unmarked parts in the simple ws region.
1721 ;; It's not worth the effort to fix that; the last part of the
1722 ;; simple ws is also typically edited often, so it could be wasted.
1723 (goto-char (setq rung-pos rung-is-marked))
1724 (goto-char simple-ws-beg))
1729 (when (and rung-is-marked
1731 (get-text-property (1- (point)) 'c-in-sws))
1733 ;; The following search is the main reason that `c-in-sws'
1734 ;; and `c-is-sws' aren't combined to one property.
1735 (goto-char (previous-single-property-change
1736 (point) 'c-in-sws nil (point-min)))
1737 (unless (get-text-property (point) 'c-is-sws)
1738 ;; If the `c-in-sws' region extended past the first
1739 ;; `c-is-sws' char we have to go forward a bit.
1740 (goto-char (next-single-property-change
1741 (point) 'c-is-sws)))
1744 "c-backward-sws cached move %s <- %s (min %s)"
1745 (point) rung-pos (point-min))
1747 (setq rung-pos (point))
1748 (if (and (< (min (skip-chars-backward " \t\f\v")
1750 (setq simple-ws-beg (point))
1751 (skip-chars-backward " \t\n\r\f\v")))
1753 (setq rung-is-marked
1754 (text-property-any (point) rung-pos
1757 (goto-char simple-ws-beg)
1760 ;; We'll loop here if there is simple ws before the first rung.
1761 ;; That means that there's been some change in it and it's
1762 ;; possible that we've stepped into another ladder, so extend
1763 ;; the previous one to join with it if there is one, and try to
1764 ;; use the cache again.
1766 "c-backward-sws extending rung with [%s..%s] (min %s)"
1767 rung-is-marked rung-pos (point-min))
1768 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1769 ;; Remove any `c-in-sws' property from the last char of
1770 ;; the rung before we mark it with `c-is-sws', so that we
1771 ;; won't connect with the remains of a broken "ladder".
1772 (c-remove-in-sws (1- rung-pos) rung-pos))
1773 (c-put-is-sws rung-is-marked
1775 (c-put-in-sws rung-is-marked
1777 (setq rung-pos rung-is-marked
1778 last-put-in-sws-pos rung-pos))
1780 (c-backward-comments)
1781 (setq cmt-skip-pos (point))
1784 ((and c-opt-cpp-prefix
1785 (/= cmt-skip-pos simple-ws-beg)
1786 (c-beginning-of-macro))
1787 ;; Inside a cpp directive. See if it should be skipped over.
1788 (let ((cpp-beg (point)))
1790 ;; Move back over all line continuations in the region skipped
1791 ;; over by `c-backward-comments'. If we go past it then we
1792 ;; started inside the cpp directive.
1793 (goto-char simple-ws-beg)
1795 (while (and (> (point) cmt-skip-pos)
1796 (progn (backward-char)
1797 (eq (char-before) ?\\)))
1798 (beginning-of-line))
1800 (if (< (point) cmt-skip-pos)
1801 ;; Don't move past the cpp directive if we began inside
1802 ;; it. Note that the position at the end of the last line
1803 ;; of the macro is also considered to be within it.
1804 (progn (goto-char cmt-skip-pos)
1807 ;; It's worthwhile to spend a little bit of effort on finding
1808 ;; the end of the macro, to get a good `simple-ws-beg'
1809 ;; position for the cache. Note that `c-backward-comments'
1810 ;; could have stepped over some comments before going into
1811 ;; the macro, and then `simple-ws-beg' must be kept on the
1812 ;; same side of those comments.
1813 (goto-char simple-ws-beg)
1814 (skip-chars-backward " \t\n\r\f\v")
1815 (if (eq (char-before) ?\\)
1818 (if (< (point) simple-ws-beg)
1819 ;; Might happen if comments after the macro were skipped
1821 (setq simple-ws-beg (point)))
1826 ((/= (save-excursion
1827 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1828 (setq next-rung-pos (point)))
1830 ;; Skipped over comments. Must put point at the end of
1831 ;; the simple ws at point since we might be after a line
1832 ;; comment or cpp directive that's been partially
1833 ;; narrowed out, and we can't risk marking the simple ws
1834 ;; at the end of it.
1835 (goto-char next-rung-pos)
1838 ;; We've searched over a piece of non-white syntactic ws. See if this
1840 (setq next-rung-pos (point))
1841 (skip-chars-backward " \t\f\v")
1844 ;; Cache if we started either from a marked rung or from a
1845 ;; completely uncached position.
1847 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1849 ;; Cache if there's a marked rung in the encountered simple ws.
1851 (skip-chars-backward " \t\n\r\f\v")
1852 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1857 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1858 (point) (1+ next-rung-pos)
1859 simple-ws-beg (min (1+ rung-pos) (point-max))
1862 ;; Remove the properties for any nested ws that might be cached.
1863 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1865 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1866 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1867 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1868 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1869 ;; Remove any `c-in-sws' property from the last char of
1870 ;; the rung before we mark it with `c-is-sws', so that we
1871 ;; won't connect with the remains of a broken "ladder".
1872 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1873 (c-put-is-sws simple-ws-beg
1875 (setq rung-is-marked t)))
1876 (c-put-in-sws (setq simple-ws-beg (point)
1877 last-put-in-sws-pos simple-ws-beg)
1879 (c-put-is-sws (setq rung-pos simple-ws-beg)
1880 (1+ next-rung-pos)))
1883 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1884 (point) (1+ next-rung-pos)
1885 simple-ws-beg (min (1+ rung-pos) (point-max))
1887 (setq rung-pos next-rung-pos
1888 simple-ws-beg (point))
1891 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1892 ;; another one before the point (which might occur when editing inside a
1893 ;; comment or macro).
1894 (when (eq last-put-in-sws-pos (point))
1895 (cond ((< (point-min) last-put-in-sws-pos)
1897 "c-backward-sws clearing at %s for cache separation"
1898 (1- last-put-in-sws-pos))
1899 (c-remove-in-sws (1- last-put-in-sws-pos)
1900 last-put-in-sws-pos))
1902 ;; If at bob and the buffer is narrowed, we have to clear the
1903 ;; character we're standing on instead since there might be a
1904 ;; `c-in-sws' before (point-min). In this case it's necessary
1905 ;; to clear both properties.
1907 "c-backward-sws clearing thoroughly at %s for cache separation"
1908 last-put-in-sws-pos)
1909 (c-remove-is-and-in-sws last-put-in-sws-pos
1910 (1+ last-put-in-sws-pos)))))
1914 ;; Other whitespace tools
1915 (defun c-partial-ws-p (beg end)
1916 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1917 ;; region? This is a "heuristic" function. .....
1919 ;; The motivation for the second bit is to check whether removing this
1920 ;; region would coalesce two symbols.
1922 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1923 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1925 (let ((end+1 (min (1+ end) (point-max))))
1926 (or (progn (goto-char (max (point-min) (1- beg)))
1927 (c-skip-ws-forward end)
1929 (progn (goto-char beg)
1930 (c-skip-ws-forward end+1)
1931 (eq (point) end+1))))))
1933 ;; A system for finding noteworthy parens before the point.
1935 (defvar c-state-cache nil)
1936 (make-variable-buffer-local 'c-state-cache)
1937 ;; The state cache used by `c-parse-state' to cut down the amount of
1938 ;; searching. It's the result from some earlier `c-parse-state' call.
1940 ;; The use of the cached info is more effective if the next
1941 ;; `c-parse-state' call is on a line close by the one the cached state
1942 ;; was made at; the cache can actually slow down a little if the
1943 ;; cached state was made very far back in the buffer. The cache is
1944 ;; most effective if `c-parse-state' is used on each line while moving
1947 (defvar c-state-cache-start 1)
1948 (make-variable-buffer-local 'c-state-cache-start)
1949 ;; This is (point-min) when `c-state-cache' was calculated, since a
1950 ;; change of narrowing is likely to affect the parens that are visible
1951 ;; before the point.
1953 (defvar c-state-cache-good-pos 1)
1954 (make-variable-buffer-local 'c-state-cache-good-pos)
1955 ;; This is a position where `c-state-cache' is known to be correct.
1956 ;; It's a position inside one of the recorded unclosed parens or the
1957 ;; top level, but not further nested inside any literal or subparen
1958 ;; that is closed before the last recorded position.
1960 ;; The exact position is chosen to try to be close to yet earlier than
1961 ;; the position where `c-state-cache' will be called next. Right now
1962 ;; the heuristic is to set it to the position after the last found
1963 ;; closing paren (of any type) before the line on which
1964 ;; `c-parse-state' was called. That is chosen primarily to work well
1965 ;; with refontification of the current line.
1967 (defsubst c-invalidate-state-cache (pos)
1968 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1969 ;; at POS or higher. This is much like `c-whack-state-after', but
1970 ;; it never changes a paren pair element into an open paren element.
1971 ;; Doing that would mean that the new open paren wouldn't have the
1972 ;; required preceding paren pair element.
1973 (while (and (or c-state-cache
1974 (when (< pos c-state-cache-good-pos)
1975 (setq c-state-cache-good-pos 1)
1977 (let ((elem (car c-state-cache)))
1979 (or (< pos (cdr elem))
1980 (when (< pos c-state-cache-good-pos)
1981 (setq c-state-cache-good-pos (cdr elem))
1984 (when (< pos c-state-cache-good-pos)
1985 (setq c-state-cache-good-pos (1+ elem))
1987 (setq c-state-cache (cdr c-state-cache))))
1989 (defun c-get-fallback-start-pos (here)
1990 ;; Return the start position for building `c-state-cache' from
1993 ;; Go back 2 bods, but ignore any bogus positions returned by
1994 ;; beginning-of-defun (i.e. open paren in column zero).
1997 (while (not (or (bobp) (zerop cnt)))
1998 (c-beginning-of-defun-1)
1999 (if (eq (char-after) ?\{)
2000 (setq cnt (1- cnt)))))
2003 (defun c-parse-state ()
2004 ;; Find and record all noteworthy parens between some good point
2005 ;; earlier in the file and point. That good point is at least the
2006 ;; beginning of the top-level construct we are in, or the beginning
2007 ;; of the preceding top-level construct if we aren't in one.
2009 ;; The returned value is a list of the noteworthy parens with the
2010 ;; last one first. If an element in the list is an integer, it's
2011 ;; the position of an open paren which has not been closed before
2012 ;; the point. If an element is a cons, it gives the position of a
2013 ;; closed brace paren pair; the car is the start paren position and
2014 ;; the cdr is the position following the closing paren. Only the
2015 ;; last closed brace paren pair before each open paren and before
2016 ;; the point is recorded, and thus the state never contains two cons
2017 ;; elements in succession.
2019 ;; Currently no characters which are given paren syntax with the
2020 ;; syntax-table property are recorded, i.e. angle bracket arglist
2021 ;; parens are never present here. Note that this might change.
2023 ;; BUG: This function doesn't cope entirely well with unbalanced
2024 ;; parens in macros. E.g. in the following case the brace before
2025 ;; the macro isn't balanced with the one after it:
2031 ;; This function might do hidden buffer changes.
2034 (let* ((here (point))
2035 (here-bol (c-point 'bol))
2036 (c-macro-start (c-query-macro-start))
2037 (in-macro-start (or c-macro-start (point)))
2038 old-state last-pos brace-pair-open brace-pair-close
2040 (c-invalidate-state-cache here)
2042 ;; If the minimum position has changed due to narrowing then we
2043 ;; have to fix the tail of `c-state-cache' accordingly.
2044 (unless (= c-state-cache-start (point-min))
2045 (if (> (point-min) c-state-cache-start)
2046 ;; If point-min has moved forward then we just need to cut
2047 ;; off a bit of the tail.
2048 (let ((ptr (cons nil c-state-cache)) elem)
2049 (while (and (setq elem (car-safe (cdr ptr)))
2050 (>= (if (consp elem) (car elem) elem)
2052 (setq ptr (cdr ptr)))
2054 (if (eq (cdr ptr) c-state-cache)
2055 (setq c-state-cache nil
2056 c-state-cache-good-pos 1)
2058 ;; If point-min has moved backward then we drop the state
2059 ;; completely. It's possible to do a better job here and
2060 ;; recalculate the top only.
2061 (setq c-state-cache nil
2062 c-state-cache-good-pos 1))
2063 (setq c-state-cache-start (point-min)))
2065 ;; Get the latest position we know are directly inside the
2066 ;; closest containing paren of the cached state.
2067 (setq last-pos (and c-state-cache
2068 (if (consp (car c-state-cache))
2069 (cdr (car c-state-cache))
2070 (1+ (car c-state-cache)))))
2071 (if (or (not last-pos)
2072 (< last-pos c-state-cache-good-pos))
2073 (setq last-pos c-state-cache-good-pos)
2074 ;; Take the opportunity to move the cached good position
2076 (if (< last-pos here-bol)
2077 (setq c-state-cache-good-pos last-pos)))
2079 ;; Check if `last-pos' is in a macro. If it is, and we're not
2080 ;; in the same macro, we must discard everything on
2081 ;; `c-state-cache' that is inside the macro before using it.
2083 (goto-char last-pos)
2084 (when (and (c-beginning-of-macro)
2085 (/= (point) in-macro-start))
2086 (c-invalidate-state-cache (point))
2087 ;; Set `last-pos' again just like above except that there's
2088 ;; no use looking at `c-state-cache-good-pos' here.
2089 (setq last-pos (if c-state-cache
2090 (if (consp (car c-state-cache))
2091 (cdr (car c-state-cache))
2092 (1+ (car c-state-cache)))
2095 ;; If we've moved very far from the last cached position then
2096 ;; it's probably better to redo it from scratch, otherwise we
2097 ;; might spend a lot of time searching from `last-pos' down to
2099 (when (< last-pos (- here 20000))
2100 ;; First get the fallback start position. If it turns out
2101 ;; that it's so far back that the cached state is closer then
2102 ;; we'll keep it afterall.
2103 (setq pos (c-get-fallback-start-pos here))
2104 (if (<= pos last-pos)
2108 c-state-cache-good-pos 1)))
2110 ;; Find the start position for the forward search. (Can't
2111 ;; search in the backward direction since the point might be in
2112 ;; some kind of literal.)
2115 (setq old-state c-state-cache)
2117 ;; There's a cached state with a containing paren. Pop off
2118 ;; the stale containing sexps from it by going forward out of
2119 ;; parens as far as possible.
2120 (narrow-to-region (point-min) here)
2121 (let (placeholder pair-beg)
2122 (while (and c-state-cache
2124 (c-up-list-forward last-pos)))
2125 (setq last-pos placeholder)
2126 (if (consp (car c-state-cache))
2127 (setq pair-beg (car-safe (cdr c-state-cache))
2128 c-state-cache (cdr-safe (cdr c-state-cache)))
2129 (setq pair-beg (car c-state-cache)
2130 c-state-cache (cdr c-state-cache))))
2132 (when (and pair-beg (eq (char-after pair-beg) ?{))
2133 ;; The last paren pair we moved out from was a brace
2134 ;; pair. Modify the state to record this as a closed
2136 (if (consp (car-safe c-state-cache))
2137 (setq c-state-cache (cdr c-state-cache)))
2138 (setq c-state-cache (cons (cons pair-beg last-pos)
2141 ;; Check if the preceding balanced paren is within a
2142 ;; macro; it should be ignored if we're outside the
2143 ;; macro. There's no need to check any further upwards;
2144 ;; if the macro contains an unbalanced opening paren then
2145 ;; we're smoked anyway.
2146 (when (and (<= (point) in-macro-start)
2147 (consp (car c-state-cache)))
2149 (goto-char (car (car c-state-cache)))
2150 (when (c-beginning-of-macro)
2152 c-state-cache (cdr c-state-cache)))))
2154 (unless (eq c-state-cache old-state)
2155 ;; Have to adjust the cached good position if state has been
2157 (setq c-state-cache-good-pos
2159 (if (consp (car c-state-cache))
2160 (cdr (car c-state-cache))
2161 (1+ (car c-state-cache)))
2163 old-state c-state-cache))
2166 (setq pos last-pos)))
2168 ;; Get the fallback start position.
2170 (setq pos (c-get-fallback-start-pos here)
2172 c-state-cache-good-pos 1))
2174 (narrow-to-region (point-min) here)
2178 brace-pair-open nil)
2180 ;; Find the balanced brace pairs. This loop is hot, so it
2181 ;; does ugly tricks to go faster.
2183 (let (set-good-pos set-brace-pair)
2186 last-pos (scan-lists pos 1 -1)) ; Might signal.
2187 (setq pos (scan-lists last-pos 1 1) ; Might signal.
2188 set-good-pos (< pos here-bol)
2189 set-brace-pair (eq (char-before last-pos) ?{))
2191 ;; Update the cached good position and record the brace
2192 ;; pair, whichever is applicable for the paren we've
2193 ;; just jumped over. But first check that it isn't
2194 ;; inside a macro and the point isn't inside the same
2196 (when (and (or set-good-pos set-brace-pair)
2197 (or (>= pos in-macro-start)
2200 (not (c-beginning-of-macro)))))
2202 (setq c-state-cache-good-pos pos))
2204 (setq brace-pair-open last-pos
2205 brace-pair-close pos))))))
2207 ;; Record the last brace pair.
2208 (when brace-pair-open
2209 (let ((head (car-safe c-state-cache)))
2212 (setcar head (1- brace-pair-open))
2213 (setcdr head brace-pair-close))
2214 (setq c-state-cache (cons (cons (1- brace-pair-open)
2219 ;; Prepare to loop, but record the open paren only if it's
2220 ;; outside a macro or within the same macro as point, and
2221 ;; if it is a legitimate open paren and not some character
2222 ;; that got an open paren syntax-table property.
2225 (when (and (or (>= last-pos in-macro-start)
2227 (goto-char last-pos)
2228 (not (c-beginning-of-macro))))
2229 ;; Check for known types of parens that we
2230 ;; want to record. The syntax table is not to
2231 ;; be trusted here since the caller might be
2232 ;; using e.g. `c++-template-syntax-table'.
2233 (memq (char-before last-pos) '(?{ ?\( ?\[)))
2234 (if (< last-pos here-bol)
2235 (setq c-state-cache-good-pos last-pos))
2236 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
2238 (if (setq last-pos (c-up-list-forward pos))
2239 ;; Found a close paren without a corresponding opening
2240 ;; one. Maybe we didn't go back far enough, so try to
2241 ;; scan backward for the start paren and then start over.
2243 (setq pos (c-up-list-backward pos)
2245 c-state-cache-good-pos c-state-cache-start)
2247 ;; Emacs (up to at least 21.2) can get confused by
2248 ;; open parens in column zero inside comments: The
2249 ;; sexp functions can then misbehave and bring us
2250 ;; back to the same point again. Check this so that
2251 ;; we don't get an infinite loop.
2255 (format "Unbalanced close paren at line %d"
2256 (1+ (count-lines (point-min)
2257 (c-point 'bol last-pos)))))))
2260 ;;(message "c-parse-state: %S end: %S" c-state-cache c-state-cache-good-pos)
2263 ;; Debug tool to catch cache inconsistencies.
2264 (defvar c-debug-parse-state nil)
2265 (unless (fboundp 'c-real-parse-state)
2266 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
2267 (cc-bytecomp-defun c-real-parse-state)
2268 (defun c-debug-parse-state ()
2269 (let ((res1 (c-real-parse-state)) res2)
2270 (let ((c-state-cache nil)
2271 (c-state-cache-start 1)
2272 (c-state-cache-good-pos 1))
2273 (setq res2 (c-real-parse-state)))
2274 (unless (equal res1 res2)
2275 ;; The cache can actually go further back due to the ad-hoc way
2276 ;; the first paren is found, so try to whack off a bit of its
2277 ;; start before complaining.
2279 (goto-char (or (c-least-enclosing-brace res2) (point)))
2280 (c-beginning-of-defun-1)
2281 (while (not (or (bobp) (eq (char-after) ?{)))
2282 (c-beginning-of-defun-1))
2283 (unless (equal (c-whack-state-before (point) res1) res2)
2284 (message (concat "c-parse-state inconsistency: "
2285 "using cache: %s, from scratch: %s")
2288 (defun c-toggle-parse-state-debug (&optional arg)
2290 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
2291 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
2292 'c-debug-parse-state
2293 'c-real-parse-state)))
2294 (c-keep-region-active))
2295 (when c-debug-parse-state
2296 (c-toggle-parse-state-debug 1))
2298 (defun c-whack-state-before (bufpos paren-state)
2299 ;; Whack off any state information from PAREN-STATE which lies
2300 ;; before BUFPOS. Not destructive on PAREN-STATE.
2301 (let* ((newstate (list nil))
2305 (setq car (car paren-state)
2306 paren-state (cdr paren-state))
2307 (if (< (if (consp car) (car car) car) bufpos)
2308 (setq paren-state nil)
2309 (setcdr ptr (list car))
2310 (setq ptr (cdr ptr))))
2313 (defun c-whack-state-after (bufpos paren-state)
2314 ;; Whack off any state information from PAREN-STATE which lies at or
2315 ;; after BUFPOS. Not destructive on PAREN-STATE.
2318 (let ((car (car paren-state)))
2320 ;; just check the car, because in a balanced brace
2321 ;; expression, it must be impossible for the corresponding
2322 ;; close brace to be before point, but the open brace to
2324 (if (<= bufpos (car car))
2326 (if (< bufpos (cdr car))
2327 ;; its possible that the open brace is before
2328 ;; bufpos, but the close brace is after. In that
2329 ;; case, convert this to a non-cons element. The
2330 ;; rest of the state is before bufpos, so we're
2332 (throw 'done (cons (car car) (cdr paren-state)))
2333 ;; we know that both the open and close braces are
2334 ;; before bufpos, so we also know that everything else
2335 ;; on state is before bufpos.
2336 (throw 'done paren-state)))
2339 ;; it's before bufpos, so everything else should too.
2340 (throw 'done paren-state)))
2341 (setq paren-state (cdr paren-state)))
2344 (defun c-most-enclosing-brace (paren-state &optional bufpos)
2345 ;; Return the bufpos of the innermost enclosing open paren before
2346 ;; bufpos, or nil if none was found.
2348 (or bufpos (setq bufpos 134217727))
2350 (setq enclosingp (car paren-state)
2351 paren-state (cdr paren-state))
2352 (if (or (consp enclosingp)
2353 (>= enclosingp bufpos))
2354 (setq enclosingp nil)
2355 (setq paren-state nil)))
2358 (defun c-least-enclosing-brace (paren-state)
2359 ;; Return the bufpos of the outermost enclosing open paren, or nil
2360 ;; if none was found.
2363 (setq elem (car paren-state)
2364 paren-state (cdr paren-state))
2369 (defun c-safe-position (bufpos paren-state)
2370 ;; Return the closest "safe" position recorded on PAREN-STATE that
2371 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
2372 ;; contain any. Return nil if BUFPOS is nil, which is useful to
2373 ;; find the closest limit before a given limit that might be nil.
2375 ;; A "safe" position is a position at or after a recorded open
2376 ;; paren, or after a recorded close paren. The returned position is
2377 ;; thus either the first position after a close brace, or the first
2378 ;; position after an enclosing paren, or at the enclosing paren in
2379 ;; case BUFPOS is immediately after it.
2384 (setq elem (car paren-state))
2386 (cond ((< (cdr elem) bufpos)
2387 (throw 'done (cdr elem)))
2388 ((< (car elem) bufpos)
2390 (throw 'done (min (1+ (car elem)) bufpos))))
2392 ;; elem is the position at and not after the opening paren, so
2393 ;; we can go forward one more step unless it's equal to
2394 ;; bufpos. This is useful in some cases avoid an extra paren
2395 ;; level between the safe position and bufpos.
2396 (throw 'done (min (1+ elem) bufpos))))
2397 (setq paren-state (cdr paren-state)))))))
2399 (defun c-beginning-of-syntax ()
2400 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2401 ;; goes to the closest previous point that is known to be outside
2402 ;; any string literal or comment. `c-state-cache' is used if it has
2403 ;; a position in the vicinity.
2404 (let* ((paren-state c-state-cache)
2408 ;; Note: Similar code in `c-safe-position'. The
2409 ;; difference is that we accept a safe position at
2410 ;; the point and don't bother to go forward past open
2413 (setq elem (car paren-state))
2415 (cond ((<= (cdr elem) (point))
2416 (throw 'done (cdr elem)))
2417 ((<= (car elem) (point))
2418 (throw 'done (car elem))))
2419 (if (<= elem (point))
2420 (throw 'done elem)))
2421 (setq paren-state (cdr paren-state)))
2424 (if (> pos (- (point) 4000))
2426 ;; The position is far back. Try `c-beginning-of-defun-1'
2427 ;; (although we can't be entirely sure it will go to a position
2428 ;; outside a comment or string in current emacsen). FIXME:
2429 ;; Consult `syntax-ppss' here.
2430 (c-beginning-of-defun-1)
2435 ;; Tools for scanning identifiers and other tokens.
2437 (defun c-on-identifier ()
2438 "Return non-nil if the point is on or directly after an identifier.
2439 Keywords are recognized and not considered identifiers. If an
2440 identifier is detected, the returned value is its starting position.
2441 If an identifier ends at the point and another begins at it \(can only
2442 happen in Pike) then the point for the preceding one is returned.
2444 Note that this function might do hidden buffer changes. See the
2445 comment at the start of cc-engine.el for more info."
2447 ;; FIXME: Shouldn't this function handle "operator" in C++?
2450 (skip-syntax-backward "w_")
2454 ;; Check for a normal (non-keyword) identifier.
2455 (and (looking-at c-symbol-start)
2456 (not (looking-at c-keywords-regexp))
2459 (when (c-major-mode-is 'pike-mode)
2460 ;; Handle the `<operator> syntax in Pike.
2461 (let ((pos (point)))
2462 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2463 (and (if (< (skip-chars-backward "`") 0)
2466 (eq (char-after) ?\`))
2467 (looking-at c-symbol-key)
2468 (>= (match-end 0) pos)
2471 ;; Handle the "operator +" syntax in C++.
2472 (when (and c-overloadable-operators-regexp
2473 (= (c-backward-token-2 0) 0))
2475 (cond ((and (looking-at c-overloadable-operators-regexp)
2476 (or (not c-opt-op-identifier-prefix)
2477 (and (= (c-backward-token-2 1) 0)
2478 (looking-at c-opt-op-identifier-prefix))))
2482 (and c-opt-op-identifier-prefix
2483 (looking-at c-opt-op-identifier-prefix)
2484 (= (c-forward-token-2 1) 0)
2485 (looking-at c-overloadable-operators-regexp)))
2490 (defsubst c-simple-skip-symbol-backward ()
2491 ;; If the point is at the end of a symbol then skip backward to the
2492 ;; beginning of it. Don't move otherwise. Return non-nil if point
2495 ;; This function might do hidden buffer changes.
2496 (or (< (skip-syntax-backward "w_") 0)
2497 (and (c-major-mode-is 'pike-mode)
2498 ;; Handle the `<operator> syntax in Pike.
2499 (let ((pos (point)))
2500 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2501 (< (skip-chars-backward "`") 0)
2502 (looking-at c-symbol-key)
2503 (>= (match-end 0) pos))
2508 (defun c-beginning-of-current-token (&optional back-limit)
2509 ;; Move to the beginning of the current token. Do not move if not
2510 ;; in the middle of one. BACK-LIMIT may be used to bound the
2511 ;; backward search; if given it's assumed to be at the boundary
2512 ;; between two tokens. Return non-nil if the point is move, nil
2515 ;; This function might do hidden buffer changes.
2516 (let ((start (point)))
2517 (if (looking-at "\\w\\|\\s_")
2518 (skip-syntax-backward "w_" back-limit)
2519 (when (< (skip-syntax-backward ".()" back-limit) 0)
2520 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2522 ;; `c-nonsymbol-token-regexp' should always match
2523 ;; since we've skipped backward over punctuator
2524 ;; or paren syntax, but consume one char in case
2525 ;; it doesn't so that we don't leave point before
2526 ;; some earlier incorrect token.
2529 (goto-char pos))))))
2532 (defun c-end-of-current-token (&optional back-limit)
2533 ;; Move to the end of the current token. Do not move if not in the
2534 ;; middle of one. BACK-LIMIT may be used to bound the backward
2535 ;; search; if given it's assumed to be at the boundary between two
2536 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2538 ;; This function might do hidden buffer changes.
2539 (let ((start (point)))
2540 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2541 (skip-syntax-forward "w_"))
2542 ((< (skip-syntax-backward ".()" back-limit) 0)
2544 (if (looking-at c-nonsymbol-token-regexp)
2545 (goto-char (match-end 0))
2546 ;; `c-nonsymbol-token-regexp' should always match since
2547 ;; we've skipped backward over punctuator or paren
2548 ;; syntax, but move forward in case it doesn't so that
2549 ;; we don't leave point earlier than we started with.
2551 (< (point) start)))))
2554 (defconst c-jump-syntax-balanced
2555 (if (memq 'gen-string-delim c-emacs-features)
2556 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2557 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2559 (defconst c-jump-syntax-unbalanced
2560 (if (memq 'gen-string-delim c-emacs-features)
2561 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2562 "\\w\\|\\s_\\|\\s\""))
2564 (defun c-forward-token-2 (&optional count balanced limit)
2565 "Move forward by tokens.
2566 A token is defined as all symbols and identifiers which aren't
2567 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2568 treated properly). Point is always either left at the beginning of a
2569 token or not moved at all. COUNT specifies the number of tokens to
2570 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2571 moves to the next token beginning only if not already at one. If
2572 BALANCED is true, move over balanced parens, otherwise move into them.
2573 Also, if BALANCED is true, never move out of an enclosing paren.
2575 LIMIT sets the limit for the movement and defaults to the point limit.
2576 The case when LIMIT is set in the middle of a token, comment or macro
2577 is handled correctly, i.e. the point won't be left there.
2579 Return the number of tokens left to move \(positive or negative). If
2580 BALANCED is true, a move over a balanced paren counts as one. Note
2581 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2582 be returned. Thus, a return value of 0 guarantees that point is at
2583 the requested position and a return value less \(without signs) than
2584 COUNT guarantees that point is at the beginning of some token.
2586 Note that this function might do hidden buffer changes. See the
2587 comment at the start of cc-engine.el for more info."
2589 (or count (setq count 1))
2591 (- (c-backward-token-2 (- count) balanced limit))
2593 (let ((jump-syntax (if balanced
2594 c-jump-syntax-balanced
2595 c-jump-syntax-unbalanced))
2600 ;; If count is zero we should jump if in the middle of a token.
2601 (c-end-of-current-token))
2604 (if limit (narrow-to-region (point-min) limit))
2606 (progn (c-forward-syntactic-ws) (point)))
2607 ;; Skip whitespace. Count this as a move if we did in
2609 (setq count (max (1- count) 0)))
2612 ;; Moved out of bounds. Make sure the returned count isn't zero.
2614 (if (zerop count) (setq count 1))
2617 ;; Use `condition-case' to avoid having the limit tests
2624 (cond ((looking-at jump-syntax)
2625 (goto-char (scan-sexps (point) 1))
2627 ((looking-at c-nonsymbol-token-regexp)
2628 (goto-char (match-end 0))
2630 ;; `c-nonsymbol-token-regexp' above should always
2631 ;; match if there are correct tokens. Try to
2632 ;; widen to see if the limit was set in the
2633 ;; middle of one, else fall back to treating
2634 ;; the offending thing as a one character token.
2638 (looking-at c-nonsymbol-token-regexp)))
2643 (c-forward-syntactic-ws)
2646 (error (goto-char last)))
2650 (setq count (1+ count)))))
2654 (defun c-backward-token-2 (&optional count balanced limit)
2655 "Move backward by tokens.
2656 See `c-forward-token-2' for details."
2658 (or count (setq count 1))
2660 (- (c-forward-token-2 (- count) balanced limit))
2662 (or limit (setq limit (point-min)))
2663 (let ((jump-syntax (if balanced
2664 c-jump-syntax-balanced
2665 c-jump-syntax-unbalanced))
2669 ;; The count is zero so try to skip to the beginning of the
2672 (progn (c-beginning-of-current-token) (point)))
2673 (if (< (point) limit)
2674 ;; The limit is inside the same token, so return 1.
2677 ;; We're not in the middle of a token. If there's
2678 ;; whitespace after the point then we must move backward,
2679 ;; so set count to 1 in that case.
2680 (and (looking-at c-syntactic-ws-start)
2681 ;; If we're looking at a '#' that might start a cpp
2682 ;; directive then we have to do a more elaborate check.
2683 (or (/= (char-after) ?#)
2684 (not c-opt-cpp-prefix)
2687 (progn (beginning-of-line)
2688 (looking-at "[ \t]*")
2691 (progn (backward-char)
2692 (not (eq (char-before) ?\\)))))))
2695 ;; Use `condition-case' to avoid having to check for buffer
2696 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2701 (c-backward-syntactic-ws)
2703 (if (looking-at jump-syntax)
2704 (goto-char (scan-sexps (1+ (point)) -1))
2705 ;; This can be very inefficient if there's a long
2706 ;; sequence of operator tokens without any separation.
2707 ;; That doesn't happen in practice, anyway.
2708 (c-beginning-of-current-token))
2709 (>= (point) limit)))
2712 (error (goto-char last)))
2714 (if (< (point) limit)
2719 (defun c-forward-token-1 (&optional count balanced limit)
2720 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2721 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2722 characters are jumped over character by character. This function is
2723 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2724 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2725 (c-forward-token-2 count balanced limit)))
2727 (defun c-backward-token-1 (&optional count balanced limit)
2728 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2729 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2730 characters are jumped over character by character. This function is
2731 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2732 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2733 (c-backward-token-2 count balanced limit)))
2736 ;; Tools for doing searches restricted to syntactically relevant text.
2738 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2739 paren-level not-inside-token
2740 lookbehind-submatch)
2741 "Like `re-search-forward', but only report matches that are found
2742 in syntactically significant text. I.e. matches in comments, macros
2743 or string literals are ignored. The start point is assumed to be
2744 outside any comment, macro or string literal, or else the content of
2745 that region is taken as syntactically significant text.
2747 If PAREN-LEVEL is non-nil, an additional restriction is added to
2748 ignore matches in nested paren sexps. The search will also not go
2749 outside the current list sexp, which has the effect that if the point
2750 should be moved to BOUND when no match is found \(i.e. NOERROR is
2751 neither nil nor t), then it will be at the closing paren if the end of
2752 the current list sexp is encountered first.
2754 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2755 ignored. Things like multicharacter operators and special symbols
2756 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2759 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2760 subexpression in REGEXP. The end of that submatch is used as the
2761 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2762 isn't used or if that subexpression didn't match then the start
2763 position of the whole match is used instead. The \"look behind\"
2764 subexpression is never tested before the starting position, so it
2765 might be a good idea to include \\=\\= as a match alternative in it.
2767 Optimization note: Matches might be missed if the \"look behind\"
2768 subexpression can match the end of nonwhite syntactic whitespace,
2769 i.e. the end of comments or cpp directives. This since the function
2770 skips over such things before resuming the search. It's on the other
2771 hand not safe to assume that the \"look behind\" subexpression never
2772 matches syntactic whitespace.
2774 Bug: Unbalanced parens inside cpp directives are currently not handled
2775 correctly \(i.e. they don't get ignored as they should) when
2778 Note that this function might do hidden buffer changes. See the
2779 comment at the start of cc-engine.el for more info."
2781 (or bound (setq bound (point-max)))
2782 (if paren-level (setq paren-level -1))
2784 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2786 (let ((start (point))
2788 ;; Start position for the last search.
2790 ;; The `parse-partial-sexp' state between the start position
2793 ;; The current position after the last state update. The next
2794 ;; `parse-partial-sexp' continues from here.
2796 ;; The position at which to check the state and the state
2797 ;; there. This is separate from `state-pos' since we might
2798 ;; need to back up before doing the next search round.
2799 check-pos check-state
2800 ;; Last position known to end a token.
2801 (last-token-end-pos (point-min))
2802 ;; Set when a valid match is found.
2809 (setq search-pos (point))
2810 (re-search-forward regexp bound noerror))
2813 (setq state (parse-partial-sexp
2814 state-pos (match-beginning 0) paren-level nil state)
2816 (if (setq check-pos (and lookbehind-submatch
2817 (or (not paren-level)
2819 (match-end lookbehind-submatch)))
2820 (setq check-state (parse-partial-sexp
2821 state-pos check-pos paren-level nil state))
2822 (setq check-pos state-pos
2825 ;; NOTE: If we got a look behind subexpression and get
2826 ;; an insignificant match in something that isn't
2827 ;; syntactic whitespace (i.e. strings or in nested
2828 ;; parentheses), then we can never skip more than a
2829 ;; single character from the match start position
2830 ;; (i.e. `state-pos' here) before continuing the
2831 ;; search. That since the look behind subexpression
2832 ;; might match the end of the insignificant region in
2836 ((elt check-state 7)
2837 ;; Match inside a line comment. Skip to eol. Use
2838 ;; `re-search-forward' instead of `skip-chars-forward' to get
2839 ;; the right bound behavior.
2840 (re-search-forward "[\n\r]" bound noerror))
2842 ((elt check-state 4)
2843 ;; Match inside a block comment. Skip to the '*/'.
2844 (search-forward "*/" bound noerror))
2846 ((and (not (elt check-state 5))
2847 (eq (char-before check-pos) ?/)
2848 (not (c-get-char-property (1- check-pos) 'syntax-table))
2849 (memq (char-after check-pos) '(?/ ?*)))
2850 ;; Match in the middle of the opener of a block or line
2852 (if (= (char-after check-pos) ?/)
2853 (re-search-forward "[\n\r]" bound noerror)
2854 (search-forward "*/" bound noerror)))
2856 ;; The last `parse-partial-sexp' above might have
2857 ;; stopped short of the real check position if the end
2858 ;; of the current sexp was encountered in paren-level
2859 ;; mode. The checks above are always false in that
2860 ;; case, and since they can do better skipping in
2861 ;; lookbehind-submatch mode, we do them before
2862 ;; checking the paren level.
2865 (/= (setq tmp (car check-state)) 0))
2866 ;; Check the paren level first since we're short of the
2867 ;; syntactic checking position if the end of the
2868 ;; current sexp was encountered by `parse-partial-sexp'.
2871 ;; Inside a nested paren sexp.
2872 (if lookbehind-submatch
2873 ;; See the NOTE above.
2874 (progn (goto-char state-pos) t)
2875 ;; Skip out of the paren quickly.
2876 (setq state (parse-partial-sexp state-pos bound 0 nil state)
2879 ;; Have exited the current paren sexp.
2882 ;; The last `parse-partial-sexp' call above
2883 ;; has left us just after the closing paren
2884 ;; in this case, so we can modify the bound
2885 ;; to leave the point at the right position
2887 (setq bound (1- (point)))
2889 (signal 'search-failed (list regexp)))))
2891 ((setq tmp (elt check-state 3))
2892 ;; Match inside a string.
2893 (if (or lookbehind-submatch
2894 (not (integerp tmp)))
2895 ;; See the NOTE above.
2896 (progn (goto-char state-pos) t)
2897 ;; Skip to the end of the string before continuing.
2898 (let ((ender (make-string 1 tmp)) (continue t))
2899 (while (if (search-forward ender bound noerror)
2901 (setq state (parse-partial-sexp
2902 state-pos (point) nil nil state)
2905 (setq continue nil)))
2910 (c-beginning-of-macro start)))
2911 ;; Match inside a macro. Skip to the end of it.
2913 (cond ((<= (point) bound) t)
2915 (t (signal 'search-failed (list regexp)))))
2917 ((and not-inside-token
2918 (or (< check-pos last-token-end-pos)
2921 (goto-char check-pos)
2923 (c-end-of-current-token last-token-end-pos))
2924 (setq last-token-end-pos (point))))))
2926 (if lookbehind-submatch
2927 ;; See the NOTE above.
2928 (goto-char state-pos)
2929 (goto-char (min last-token-end-pos bound))))
2936 ;; Should loop to search again, but take care to avoid
2937 ;; looping on the same spot.
2938 (or (/= search-pos (point))
2939 (if (= (point) bound)
2942 (signal 'search-failed (list regexp)))
2948 (signal (car err) (cdr err))))
2950 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2954 (goto-char (match-end 0))
2957 ;; Search failed. Set point as appropriate.
2963 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
2964 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2965 i.e. don't stop at positions inside syntactic whitespace or string
2966 literals. Preprocessor directives are also ignored, with the exception
2967 of the one that the point starts within, if any. If LIMIT is given,
2968 it's assumed to be at a syntactically relevant position.
2970 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
2971 sexps, and the search will also not go outside the current paren sexp.
2972 However, if LIMIT or the buffer limit is reached inside a nested paren
2973 then the point will be left at the limit.
2975 Non-nil is returned if the point moved, nil otherwise.
2977 Note that this function might do hidden buffer changes. See the
2978 comment at the start of cc-engine.el for more info."
2980 (let ((start (point))
2982 ;; A list of syntactically relevant positions in descending
2983 ;; order. It's used to avoid scanning repeatedly over
2984 ;; potentially large regions with `parse-partial-sexp' to verify
2987 ;; The position at the beginning of `safe-pos-list'.
2989 ;; The result from `c-beginning-of-macro' at the start position or the
2990 ;; start position itself if it isn't within a macro. Evaluated on
2993 ;; The earliest position after the current one with the same paren
2994 ;; level. Used only when `paren-level' is set.
2995 (paren-level-pos (point)))
2999 (< (skip-chars-backward skip-chars limit) 0)
3001 ;; Use `parse-partial-sexp' from a safe position down to
3002 ;; the point to check if it's outside comments and
3004 (let ((pos (point)) state-2 pps-end-pos)
3005 ;; Pick a safe position as close to the point as
3008 ;; FIXME: Consult `syntax-ppss' here if our
3009 ;; cache doesn't give a good position.
3010 (while (and safe-pos-list
3011 (> (car safe-pos-list) (point)))
3012 (setq safe-pos-list (cdr safe-pos-list)))
3013 (unless (setq safe-pos (car-safe safe-pos-list))
3014 (setq safe-pos (max (or (c-safe-position
3015 (point) (or c-state-cache
3019 safe-pos-list (list safe-pos)))
3021 ;; Cache positions along the way to use if we have to
3022 ;; back up more. We cache every closing paren on the
3023 ;; same level. If the paren cache is relevant in this
3024 ;; region then we're typically already on the same
3025 ;; level as the target position. Note that we might
3026 ;; cache positions after opening parens in case
3027 ;; safe-pos is in a nested list. That's both uncommon
3030 (setq state (parse-partial-sexp
3033 (setq safe-pos (point)
3034 safe-pos-list (cons safe-pos safe-pos-list)))
3037 ((or (elt state 3) (elt state 4))
3038 ;; Inside string or comment. Continue search at the
3040 (goto-char (elt state 8))
3045 (setq state-2 (parse-partial-sexp
3046 pos paren-level-pos -1)
3047 pps-end-pos (point))
3048 (/= (car state-2) 0)))
3049 ;; Not at the right level.
3051 (if (and (< (car state-2) 0)
3052 ;; We stop above if we go out of a paren.
3053 ;; Now check whether it precedes or is
3054 ;; nested in the starting sexp.
3058 pps-end-pos paren-level-pos
3060 (< (car state-2) 0)))
3062 ;; We've stopped short of the starting position
3063 ;; so the hit was inside a nested list. Go up
3064 ;; until we are at the right level.
3067 (goto-char (scan-lists pos -1
3069 (setq paren-level-pos (point))
3070 (if (and limit (>= limit paren-level-pos))
3076 (goto-char (or limit (point-min)))
3079 ;; The hit was outside the list at the start
3080 ;; position. Go to the start of the list and exit.
3081 (goto-char (1+ (elt state-2 1)))
3084 ((c-beginning-of-macro limit)
3088 (setq start-macro-beg
3091 (c-beginning-of-macro limit)
3095 ;; It's inside the same macro we started in so it's
3096 ;; a relevant match.
3100 ;; If the state contains the start of the containing sexp we
3101 ;; cache that position too, so that parse-partial-sexp in the
3102 ;; next run has a bigger chance of starting at the same level
3103 ;; as the target position and thus will get more good safe
3104 ;; positions into the list.
3106 (setq safe-pos (1+ (elt state 1))
3107 safe-pos-list (cons safe-pos safe-pos-list))))
3111 ;; Skip syntactic ws afterwards so that we don't stop at the
3112 ;; end of a comment if `skip-chars' is something like "^/".
3113 (c-backward-syntactic-ws)
3116 ;; We might want to extend this with more useful return values in
3118 (/= (point) start)))
3120 ;; The following is an alternative implementation of
3121 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3122 ;; track of the syntactic context. It turned out to be generally
3123 ;; slower than the one above which uses forward checks from earlier
3126 ;;(defconst c-ssb-stop-re
3127 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3128 ;; ;; stop at to avoid going into comments and literals.
3130 ;; ;; Match comment end syntax and string literal syntax. Also match
3131 ;; ;; '/' for block comment endings (not covered by comment end
3133 ;; "\\s>\\|/\\|\\s\""
3134 ;; (if (memq 'gen-string-delim c-emacs-features)
3137 ;; (if (memq 'gen-comment-delim c-emacs-features)
3141 ;;(defconst c-ssb-stop-paren-re
3142 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3143 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3145 ;;(defconst c-ssb-sexp-end-re
3146 ;; ;; Regexp matching the ending syntax of a complex sexp.
3147 ;; (concat c-string-limit-regexp "\\|\\s)"))
3149 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3150 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3151 ;;i.e. don't stop at positions inside syntactic whitespace or string
3152 ;;literals. Preprocessor directives are also ignored. However, if the
3153 ;;point is within a comment, string literal or preprocessor directory to
3154 ;;begin with, its contents is treated as syntactically relevant chars.
3155 ;;If LIMIT is given, it limits the backward search and the point will be
3156 ;;left there if no earlier position is found.
3158 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3159 ;;sexps, and the search will also not go outside the current paren sexp.
3160 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3161 ;;then the point will be left at the limit.
3163 ;;Non-nil is returned if the point moved, nil otherwise.
3165 ;;Note that this function might do hidden buffer changes. See the
3166 ;;comment at the start of cc-engine.el for more info."
3168 ;; (save-restriction
3170 ;; (narrow-to-region limit (point-max)))
3172 ;; (let ((start (point)))
3174 ;; (while (let ((last-pos (point))
3176 ;; (skip-chars-backward skip-chars)
3179 ;; ;; Skip back over the same region as
3180 ;; ;; `skip-chars-backward' above, but keep to
3181 ;; ;; syntactically relevant positions.
3182 ;; (goto-char last-pos)
3184 ;; ;; `re-search-backward' with a single char regexp
3185 ;; ;; should be fast.
3186 ;; (re-search-backward
3187 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3192 ;; ((looking-at "\\s(")
3193 ;; ;; `paren-level' is set and we've found the
3194 ;; ;; start of the containing paren.
3198 ;; ((looking-at c-ssb-sexp-end-re)
3199 ;; ;; We're at the end of a string literal or paren
3200 ;; ;; sexp (if `paren-level' is set).
3202 ;; (condition-case nil
3203 ;; (c-backward-sexp)
3205 ;; (goto-char limit)
3206 ;; (throw 'done t))))
3210 ;; ;; At the end of some syntactic ws or possibly
3211 ;; ;; after a plain '/' operator.
3212 ;; (let ((pos (point)))
3213 ;; (c-backward-syntactic-ws)
3214 ;; (if (= pos (point))
3215 ;; ;; Was a plain '/' operator. Go past it.
3216 ;; (backward-char)))))
3218 ;; (> (point) stop-pos))))
3220 ;; ;; Now the point is either at `stop-pos' or at some
3221 ;; ;; position further back if `stop-pos' was at a
3222 ;; ;; syntactically irrelevant place.
3224 ;; ;; Skip additional syntactic ws so that we don't stop
3225 ;; ;; at the end of a comment if `skip-chars' is
3226 ;; ;; something like "^/".
3227 ;; (c-backward-syntactic-ws)
3229 ;; (< (point) stop-pos))))
3231 ;; ;; We might want to extend this with more useful return values
3232 ;; ;; in the future.
3233 ;; (/= (point) start))))
3236 ;; Tools for handling comments and string literals.
3238 (defun c-slow-in-literal (&optional lim detect-cpp)
3239 "Return the type of literal point is in, if any.
3240 The return value is `c' if in a C-style comment, `c++' if in a C++
3241 style comment, `string' if in a string literal, `pound' if DETECT-CPP
3242 is non-nil and in a preprocessor line, or nil if somewhere else.
3243 Optional LIM is used as the backward limit of the search. If omitted,
3244 or nil, `c-beginning-of-defun' is used.
3246 The last point calculated is cached if the cache is enabled, i.e. if
3247 `c-in-literal-cache' is bound to a two element vector.
3249 Note that this function might do hidden buffer changes. See the
3250 comment at the start of cc-engine.el for more info."
3252 (if (and (vectorp c-in-literal-cache)
3253 (= (point) (aref c-in-literal-cache 0)))
3254 (aref c-in-literal-cache 1)
3255 (let ((rtn (save-excursion
3256 (let* ((pos (point))
3258 (c-beginning-of-syntax)
3260 (state (parse-partial-sexp lim pos)))
3262 ((elt state 3) 'string)
3263 ((elt state 4) (if (elt state 7) 'c++ 'c))
3264 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
3266 ;; cache this result if the cache is enabled
3267 (if (not c-in-literal-cache)
3268 (setq c-in-literal-cache (vector (point) rtn)))
3271 ;; XEmacs has a built-in function that should make this much quicker.
3272 ;; I don't think we even need the cache, which makes our lives more
3273 ;; complicated anyway. In this case, lim is only used to detect
3276 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
3277 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
3278 ;; Xemacs 21.4.4. It manifested itself thus:
3280 ;; Starting with an empty AWK Mode buffer, type
3282 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
3284 ;; AWK Mode is designed such that when the first / is typed, it gets the
3285 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
3286 ;; are given the s-t property "string". However, buffer-syntactic-context
3287 ;; fails to take account of the change of the s-t property on the opening / to
3288 ;; "string", and reports that the { is within a string started by the second /.
3290 ;; The workaround for this is for the AWK Mode initialisation to switch the
3291 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
3292 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
3294 ;; (Alan Mackenzie, 2003/4/30).
3296 (defun c-fast-in-literal (&optional lim detect-cpp)
3297 ;; This function might do hidden buffer changes.
3298 (let ((context (buffer-syntactic-context)))
3300 ((eq context 'string) 'string)
3301 ((eq context 'comment) 'c++)
3302 ((eq context 'block-comment) 'c)
3303 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
3305 (defalias 'c-in-literal
3306 (if (fboundp 'buffer-syntactic-context)
3307 'c-fast-in-literal ; XEmacs
3308 'c-slow-in-literal)) ; GNU Emacs
3310 ;; The defalias above isn't enough to shut up the byte compiler.
3311 (cc-bytecomp-defun c-in-literal)
3313 (defun c-literal-limits (&optional lim near not-in-delimiter)
3314 "Return a cons of the beginning and end positions of the comment or
3315 string surrounding point (including both delimiters), or nil if point
3316 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
3317 to start parsing from. If NEAR is non-nil, then the limits of any
3318 literal next to point is returned. \"Next to\" means there's only
3319 spaces and tabs between point and the literal. The search for such a
3320 literal is done first in forward direction. If NOT-IN-DELIMITER is
3321 non-nil, the case when point is inside a starting delimiter won't be
3322 recognized. This only has effect for comments, which have starting
3323 delimiters with more than one character.
3325 Note that this function might do hidden buffer changes. See the
3326 comment at the start of cc-engine.el for more info."
3329 (let* ((pos (point))
3331 (c-beginning-of-syntax)
3333 (state (parse-partial-sexp lim pos)))
3335 (cond ((elt state 3) ; String.
3336 (goto-char (elt state 8))
3337 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3340 ((elt state 4) ; Comment.
3341 (goto-char (elt state 8))
3342 (cons (point) (progn (c-forward-single-comment) (point))))
3344 ((and (not not-in-delimiter)
3346 (eq (char-before) ?/)
3347 (looking-at "[/*]"))
3348 ;; We're standing in a comment starter.
3350 (cons (point) (progn (c-forward-single-comment) (point))))
3355 ;; Search forward for a literal.
3356 (skip-chars-forward " \t")
3359 ((looking-at c-string-limit-regexp) ; String.
3360 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3363 ((looking-at c-comment-start-regexp) ; Line or block comment.
3364 (cons (point) (progn (c-forward-single-comment) (point))))
3368 (skip-chars-backward " \t")
3370 (let ((end (point)) beg)
3373 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
3374 (setq beg (c-safe (c-backward-sexp 1) (point))))
3376 ((and (c-safe (forward-char -2) t)
3378 ;; Block comment. Due to the nature of line
3379 ;; comments, they will always be covered by the
3380 ;; normal case above.
3382 (c-backward-single-comment)
3383 ;; If LIM is bogus, beg will be bogus.
3384 (setq beg (point))))
3386 (if beg (cons beg end))))))
3389 ;; In case external callers use this; it did have a docstring.
3390 (defalias 'c-literal-limits-fast 'c-literal-limits)
3392 (defun c-collect-line-comments (range)
3393 "If the argument is a cons of two buffer positions (such as returned by
3394 `c-literal-limits'), and that range contains a C++ style line comment,
3395 then an extended range is returned that contains all adjacent line
3396 comments (i.e. all comments that starts in the same column with no
3397 empty lines or non-whitespace characters between them). Otherwise the
3398 argument is returned.
3400 Note that this function might do hidden buffer changes. See the
3401 comment at the start of cc-engine.el for more info."
3405 (if (and (consp range) (progn
3406 (goto-char (car range))
3407 (looking-at c-line-comment-starter)))
3408 (let ((col (current-column))
3410 (bopl (c-point 'bopl))
3412 ;; Got to take care in the backward direction to handle
3413 ;; comments which are preceded by code.
3414 (while (and (c-backward-single-comment)
3416 (looking-at c-line-comment-starter)
3417 (= col (current-column)))
3419 bopl (c-point 'bopl)))
3421 (while (and (progn (skip-chars-forward " \t")
3422 (looking-at c-line-comment-starter))
3423 (= col (current-column))
3424 (prog1 (zerop (forward-line 1))
3425 (setq end (point)))))
3430 (defun c-literal-type (range)
3431 "Convenience function that given the result of `c-literal-limits',
3432 returns nil or the type of literal that the range surrounds. It's
3433 much faster than using `c-in-literal' and is intended to be used when
3434 you need both the type of a literal and its limits.
3436 Note that this function might do hidden buffer changes. See the
3437 comment at the start of cc-engine.el for more info."
3441 (goto-char (car range))
3442 (cond ((looking-at c-string-limit-regexp) 'string)
3443 ((or (looking-at "//") ; c++ line comment
3444 (and (looking-at "\\s<") ; comment starter
3445 (looking-at "#"))) ; awk comment.
3447 (t 'c))) ; Assuming the range is valid.
3451 ;; `c-find-decl-spots' and accompanying stuff.
3453 ;; Variables used in `c-find-decl-spots' to cache the search done for
3454 ;; the first declaration in the last call. When that function starts,
3455 ;; it needs to back up over syntactic whitespace to look at the last
3456 ;; token before the region being searched. That can sometimes cause
3457 ;; moves back and forth over a quite large region of comments and
3458 ;; macros, which would be repeated for each changed character when
3459 ;; we're called during fontification, since font-lock refontifies the
3460 ;; current line for each change. Thus it's worthwhile to cache the
3463 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3464 ;; the syntactic whitespace less or equal to some start position.
3465 ;; There's no cached value if it's nil.
3467 ;; `c-find-decl-match-pos' is the match position if
3468 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3469 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3470 (defvar c-find-decl-syntactic-pos nil)
3471 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
3472 (defvar c-find-decl-match-pos nil)
3473 (make-variable-buffer-local 'c-find-decl-match-pos)
3475 (defsubst c-invalidate-find-decl-cache (change-min-pos)
3476 (and c-find-decl-syntactic-pos
3477 (< change-min-pos c-find-decl-syntactic-pos)
3478 (setq c-find-decl-syntactic-pos nil)))
3480 ; (defface c-debug-decl-spot-face
3481 ; '((t (:background "Turquoise")))
3482 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3483 ; (defface c-debug-decl-sws-face
3484 ; '((t (:background "Khaki")))
3485 ; "Debug face to mark the syntactic whitespace between the declaration
3486 ; spots and the preceding token end.")
3488 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
3489 (when (facep 'c-debug-decl-spot-face)
3490 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
3491 (c-debug-add-face (max match-pos (point-min)) decl-pos
3492 'c-debug-decl-sws-face)
3493 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
3494 'c-debug-decl-spot-face))))
3495 (defmacro c-debug-remove-decl-spot-faces (beg end)
3496 (when (facep 'c-debug-decl-spot-face)
3497 `(c-save-buffer-state ()
3498 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
3499 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
3501 (defmacro c-find-decl-prefix-search ()
3502 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3503 ;; but it contains lots of free variables that refer to things
3504 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3505 ;; if there is a match, otherwise at `cfd-limit'.
3507 ;; This macro might do hidden buffer changes.
3510 ;; Find the next property match position if we haven't got one already.
3511 (unless cfd-prop-match
3514 (goto-char (next-single-property-change
3515 (point) 'c-type nil cfd-limit))
3516 (and (< (point) cfd-limit)
3517 (not (eq (c-get-char-property (1- (point)) 'c-type)
3519 (setq cfd-prop-match (point))))
3521 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
3523 (unless cfd-re-match
3525 (if (> cfd-re-match-end (point))
3526 (goto-char cfd-re-match-end))
3528 (while (if (setq cfd-re-match-end
3529 (re-search-forward c-decl-prefix-or-start-re
3532 ;; Match. Check if it's inside a comment or string literal.
3534 (if (setq cfd-re-match (match-end 1))
3535 ;; Matched the end of a token preceding a decl spot.
3537 (goto-char cfd-re-match)
3539 ;; Matched a token that start a decl spot.
3540 (goto-char (match-beginning 0))
3544 ;; No match. Finish up and exit the loop.
3545 (setq cfd-re-match cfd-limit)
3548 ;; Skip out of comments and string literals.
3550 (goto-char (next-single-property-change
3551 (point) 'face nil cfd-limit))
3552 (and (< (point) cfd-limit)
3553 (c-got-face-at (point) c-literal-faces)))))
3555 ;; If we matched at the decl start, we have to back up over the
3556 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
3557 ;; any decl spots in the syntactic ws.
3558 (unless cfd-re-match
3559 (c-backward-syntactic-ws)
3560 (setq cfd-re-match (point))))
3562 ;; Choose whichever match is closer to the start.
3563 (if (< cfd-re-match cfd-prop-match)
3564 (setq cfd-match-pos cfd-re-match
3566 (setq cfd-match-pos cfd-prop-match
3567 cfd-prop-match nil))
3569 (goto-char cfd-match-pos)
3571 (when (< cfd-match-pos cfd-limit)
3572 ;; Skip forward past comments only so we don't skip macros.
3573 (c-forward-comments)
3574 ;; Set the position to continue at. We can avoid going over
3575 ;; the comments skipped above a second time, but it's possible
3576 ;; that the comment skipping has taken us past `cfd-prop-match'
3577 ;; since the property might be used inside comments.
3578 (setq cfd-continue-pos (if cfd-prop-match
3579 (min cfd-prop-match (point))
3582 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3583 ;; Call CFD-FUN for each possible spot for a declaration, cast or
3584 ;; label from the point to CFD-LIMIT. Such a spot is:
3586 ;; o The first token after bob.
3587 ;; o The first token after the end of submatch 1 in
3588 ;; `c-decl-prefix-or-start-re' when that submatch matches.
3589 ;; o The start of each `c-decl-prefix-or-start-re' match when
3590 ;; submatch 1 doesn't match.
3591 ;; o The first token after the end of each occurence of the
3592 ;; `c-type' text property with the value `c-decl-end', provided
3593 ;; `c-type-decl-end-used' is set.
3595 ;; Only a spot that match CFD-DECL-RE and whose face is in the
3596 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
3597 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
3599 ;; If the match is inside a macro then the buffer is narrowed to the
3600 ;; end of it, so that CFD-FUN can investigate the following tokens
3601 ;; without matching something that begins inside a macro and ends
3602 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3603 ;; CFD-FACE-CHECKLIST checks exist.
3605 ;; CFD-FUN is called with point at the start of the spot. It's
3606 ;; passed two arguments: The first is the end position of the token
3607 ;; preceding the spot, or 0 for the implicit match at bob. The
3608 ;; second is a flag that is t when the match is inside a macro. If
3609 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
3610 ;; spot, it should return non-nil to ensure that the next search
3613 ;; The spots are visited approximately in order from top to bottom.
3614 ;; It's however the positions where `c-decl-prefix-or-start-re'
3615 ;; matches and where `c-decl-end' properties are found that are in
3616 ;; order. Since the spots often are at the following token, they
3617 ;; might be visited out of order insofar as more spots are reported
3618 ;; later on within the syntactic whitespace between the match
3619 ;; positions and their spots.
3621 ;; It's assumed that comments and strings are fontified in the
3624 ;; This is mainly used in fontification, and so has an elaborate
3625 ;; cache to handle repeated calls from the same start position; see
3626 ;; the variables above.
3628 ;; All variables in this function begin with `cfd-' to avoid name
3629 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3631 ;; This function might do hidden buffer changes.
3633 (let ((cfd-start-pos (point))
3634 (cfd-buffer-end (point-max))
3635 ;; The end of the token preceding the decl spot last found
3636 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
3639 ;; The end position of the last `c-decl-prefix-or-start-re'
3640 ;; match. If this is greater than `cfd-continue-pos', the
3641 ;; next regexp search is started here instead.
3642 (cfd-re-match-end (point-min))
3643 ;; The end of the last `c-decl-end' found by
3644 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
3645 ;; match. If searching for the property isn't needed then we
3646 ;; disable it by setting it to `cfd-limit' directly.
3647 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3648 ;; The end of the token preceding the decl spot last found by
3649 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
3650 ;; bob. `cfd-limit' if there's no match. In other words,
3651 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
3652 (cfd-match-pos cfd-limit)
3653 ;; The position to continue searching at.
3655 ;; The position of the last "real" token we've stopped at.
3656 ;; This can be greater than `cfd-continue-pos' when we get
3657 ;; hits inside macros or at `c-decl-end' positions inside
3660 ;; The end position of the last entered macro.
3663 ;; Initialize by finding a syntactically relevant start position
3664 ;; before the point, and do the first `c-decl-prefix-or-start-re'
3665 ;; search unless we're at bob.
3667 (let (start-in-literal start-in-macro syntactic-pos)
3668 ;; Must back up a bit since we look for the end of the previous
3669 ;; statement or declaration, which is earlier than the first
3673 ;; First we need to move to a syntactically relevant position.
3674 ;; Begin by backing out of comment or string literals.
3676 (when (c-got-face-at (point) c-literal-faces)
3677 ;; Try to use the faces to back up to the start of the
3678 ;; literal. FIXME: What if the point is on a declaration
3679 ;; inside a comment?
3680 (while (and (not (bobp))
3681 (c-got-face-at (1- (point)) c-literal-faces))
3682 (goto-char (previous-single-property-change
3683 (point) 'face nil (point-min))))
3685 ;; XEmacs doesn't fontify the quotes surrounding string
3687 (and (featurep 'xemacs)
3688 (eq (get-text-property (point) 'face)
3689 'font-lock-string-face)
3691 (progn (backward-char)
3692 (not (looking-at c-string-limit-regexp)))
3695 ;; Don't trust the literal to contain only literal faces
3696 ;; (the font lock package might not have fontified the
3697 ;; start of it at all, for instance) so check that we have
3698 ;; arrived at something that looks like a start or else
3699 ;; resort to `c-literal-limits'.
3700 (unless (looking-at c-literal-start-regexp)
3701 (let ((range (c-literal-limits)))
3702 (if range (goto-char (car range)))))
3704 (setq start-in-literal (point)))
3706 ;; The start is in a literal. If the limit is in the same
3707 ;; one we don't have to find a syntactic position etc. We
3708 ;; only check that if the limit is at or before bonl to save
3709 ;; time; it covers the by far most common case when font-lock
3710 ;; refontifies the current line only.
3711 (<= cfd-limit (c-point 'bonl cfd-start-pos))
3713 (goto-char cfd-start-pos)
3715 (goto-char (next-single-property-change
3716 (point) 'face nil cfd-limit))
3717 (and (< (point) cfd-limit)
3718 (c-got-face-at (point) c-literal-faces))))
3719 (= (point) cfd-limit)))
3721 ;; Completely inside a literal. Set up variables to trig the
3722 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
3723 ;; find a suitable start position.
3724 (setq cfd-continue-pos start-in-literal))
3726 ;; Check if the region might be completely inside a macro, to
3727 ;; optimize that like the completely-inside-literal above.
3729 (and (= (forward-line 1) 0)
3730 (bolp) ; forward-line has funny behavior at eob.
3731 (>= (point) cfd-limit)
3732 (progn (backward-char)
3733 (eq (char-before) ?\\))))
3734 ;; (Maybe) completely inside a macro. Only need to trig the
3735 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
3737 (setq cfd-continue-pos (1- cfd-start-pos)
3741 ;; Back out of any macro so we don't miss any declaration
3742 ;; that could follow after it.
3743 (when (c-beginning-of-macro)
3744 (setq start-in-macro t))
3746 ;; Now we're at a proper syntactically relevant position so we
3747 ;; can use the cache. But first clear it if it applied
3749 (c-invalidate-find-decl-cache cfd-start-pos)
3751 (setq syntactic-pos (point))
3752 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
3753 ;; Don't have to do this if the cache is relevant here,
3754 ;; typically if the same line is refontified again. If
3755 ;; we're just some syntactic whitespace further down we can
3756 ;; still use the cache to limit the skipping.
3757 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
3759 ;; If we hit `c-find-decl-syntactic-pos' and
3760 ;; `c-find-decl-match-pos' is set then we install the cached
3761 ;; values. If we hit `c-find-decl-syntactic-pos' and
3762 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3763 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3764 ;; and so we can continue the search from this point. If we
3765 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
3766 ;; the right spot to begin searching anyway.
3767 (if (and (eq (point) c-find-decl-syntactic-pos)
3768 c-find-decl-match-pos)
3769 (setq cfd-match-pos c-find-decl-match-pos
3770 cfd-continue-pos syntactic-pos)
3772 (setq c-find-decl-syntactic-pos syntactic-pos)
3775 ;; Always consider bob a match to get the first
3776 ;; declaration in the file. Do this separately instead of
3777 ;; letting `c-decl-prefix-or-start-re' match bob, so that
3778 ;; regexp always can consume at least one character to
3779 ;; ensure that we won't get stuck in an infinite loop.
3780 (setq cfd-re-match 0)
3782 (c-beginning-of-current-token)
3783 (< (point) cfd-limit))
3784 ;; Do an initial search now. In the bob case above it's
3785 ;; only done to search for a `c-decl-end' spot.
3786 (c-find-decl-prefix-search))
3788 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
3791 ;; Advance `cfd-continue-pos' if it's before the start position.
3792 ;; The closest continue position that might have effect at or
3793 ;; after the start depends on what we started in. This also
3794 ;; finds a suitable start position in the special cases when the
3795 ;; region is completely within a literal or macro.
3796 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
3800 ;; If we're in a macro then it's the closest preceding token
3801 ;; in the macro. Check this before `start-in-literal',
3802 ;; since if we're inside a literal in a macro, the preceding
3803 ;; token is earlier than any `c-decl-end' spot inside the
3804 ;; literal (comment).
3805 (goto-char (or start-in-literal cfd-start-pos))
3806 ;; The only syntactic ws in macros are comments.
3807 (c-backward-comments)
3809 (c-beginning-of-current-token))
3812 ;; If we're in a comment it can only be the closest
3813 ;; preceding `c-decl-end' position within that comment, if
3814 ;; any. Go back to the beginning of such a property so that
3815 ;; `c-find-decl-prefix-search' will find the end of it.
3816 ;; (Can't stop at the end and install it directly on
3817 ;; `cfd-prop-match' since that variable might be cleared
3818 ;; after `cfd-fun' below.)
3820 ;; Note that if the literal is a string then the property
3821 ;; search will simply skip to the beginning of it right
3823 (if (not c-type-decl-end-used)
3824 (goto-char start-in-literal)
3825 (goto-char cfd-start-pos)
3827 (goto-char (previous-single-property-change
3828 (point) 'c-type nil start-in-literal))
3829 (and (> (point) start-in-literal)
3830 (not (eq (c-get-char-property (point) 'c-type)
3833 (when (= (point) start-in-literal)
3834 ;; Didn't find any property inside the comment, so we can
3835 ;; skip it entirely. (This won't skip past a string, but
3836 ;; that'll be handled quickly by the next
3837 ;; `c-find-decl-prefix-search' anyway.)
3838 (c-forward-single-comment)
3839 (if (> (point) cfd-limit)
3840 (goto-char cfd-limit))))
3843 ;; If we started in normal code, the only match that might
3844 ;; apply before the start is what we already got in
3845 ;; `cfd-match-pos' so we can continue at the start position.
3846 ;; (Note that we don't get here if the first match is below
3848 (goto-char cfd-start-pos)))
3850 ;; Delete found matches if they are before our new continue
3851 ;; position, so that `c-find-decl-prefix-search' won't back up
3852 ;; to them later on.
3853 (setq cfd-continue-pos (point))
3854 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
3855 (setq cfd-re-match nil))
3856 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
3857 (setq cfd-prop-match nil)))
3860 ;; This is the normal case and we got a proper syntactic
3861 ;; position. If there's a match then it's always outside
3862 ;; macros and comments, so advance to the next token and set
3863 ;; `cfd-token-pos'. The loop below will later go back using
3864 ;; `cfd-continue-pos' to fix declarations inside the
3866 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
3867 (goto-char syntactic-pos)
3868 (c-forward-syntactic-ws)
3869 (and cfd-continue-pos
3870 (< cfd-continue-pos (point))
3871 (setq cfd-token-pos (point))))
3873 ;; Have one of the special cases when the region is completely
3874 ;; within a literal or macro. `cfd-continue-pos' is set to a
3875 ;; good start position for the search, so do it.
3876 (c-find-decl-prefix-search)))
3878 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
3882 (< cfd-match-pos cfd-limit)
3885 ;; Kludge to filter out matches on the "<" that
3886 ;; aren't open parens, for the sake of languages
3887 ;; that got `c-recognize-<>-arglists' set.
3888 (and (eq (char-before cfd-match-pos) ?<)
3889 (not (c-get-char-property (1- cfd-match-pos)
3892 ;; If `cfd-continue-pos' is less or equal to
3893 ;; `cfd-token-pos', we've got a hit inside a macro
3894 ;; that's in the syntactic whitespace before the last
3895 ;; "real" declaration we've checked. If they're equal
3896 ;; we've arrived at the declaration a second time, so
3897 ;; there's nothing to do.
3898 (= cfd-continue-pos cfd-token-pos)
3901 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3902 ;; we're still searching for declarations embedded in
3903 ;; the syntactic whitespace. In that case we need
3904 ;; only to skip comments and not macros, since they
3905 ;; can't be nested, and that's already been done in
3906 ;; `c-find-decl-prefix-search'.
3907 (when (> cfd-continue-pos cfd-token-pos)
3908 (c-forward-syntactic-ws)
3909 (setq cfd-token-pos (point)))
3911 ;; Continue if the following token fails the
3912 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3913 (when (or (>= (point) cfd-limit)
3914 (not (looking-at cfd-decl-re))
3915 (and cfd-face-checklist
3917 (point) cfd-face-checklist))))
3918 (goto-char cfd-continue-pos)
3921 (< (point) cfd-limit))
3922 (c-find-decl-prefix-search))
3924 (< (point) cfd-limit))
3927 (>= (point) cfd-start-pos)
3930 ;; Narrow to the end of the macro if we got a hit inside
3931 ;; one, to avoid recognizing things that start inside the
3932 ;; macro and end outside it.
3933 (when (> cfd-match-pos cfd-macro-end)
3934 ;; Not in the same macro as in the previous round.
3936 (goto-char cfd-match-pos)
3938 (if (save-excursion (and (c-beginning-of-macro)
3939 (< (point) cfd-match-pos)))
3940 (progn (c-end-of-macro)
3944 (if (zerop cfd-macro-end)
3946 (if (> cfd-macro-end (point))
3947 (progn (narrow-to-region (point-min) cfd-macro-end)
3949 ;; The matched token was the last thing in the macro,
3950 ;; so the whole match is bogus.
3951 (setq cfd-macro-end 0)
3954 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3955 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3956 (setq cfd-prop-match nil))
3958 (when (/= cfd-macro-end 0)
3959 ;; Restore limits if we did macro narrowment above.
3960 (narrow-to-region (point-min) cfd-buffer-end)))
3962 (goto-char cfd-continue-pos)
3963 (if (= cfd-continue-pos cfd-limit)
3964 (setq cfd-match-pos cfd-limit)
3965 (c-find-decl-prefix-search)))))
3968 ;; A cache for found types.
3970 ;; Buffer local variable that contains an obarray with the types we've
3971 ;; found. If a declaration is recognized somewhere we record the
3972 ;; fully qualified identifier in it to recognize it as a type
3973 ;; elsewhere in the file too. This is not accurate since we do not
3974 ;; bother with the scoping rules of the languages, but in practice the
3975 ;; same name is seldom used as both a type and something else in a
3976 ;; file, and we only use this as a last resort in ambiguous cases (see
3977 ;; `c-forward-decl-or-cast-1').
3979 ;; Not every type need be in this cache. However, things which have
3980 ;; ceased to be types must be removed from it.
3982 ;; Template types in C++ are added here too but with the template
3983 ;; arglist replaced with "<>" in references or "<" for the one in the
3984 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
3985 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
3986 ;; template specs can be fairly sized programs in themselves) and
3987 ;; improves the hit ratio (it's a type regardless of the template
3988 ;; args; it's just not the same type, but we're only interested in
3989 ;; recognizing types, not telling distinct types apart). Note that
3990 ;; template types in references are added here too; from the example
3991 ;; above there will also be an entry "Foo<".
3992 (defvar c-found-types nil)
3993 (make-variable-buffer-local 'c-found-types)
3995 (defsubst c-clear-found-types ()
3996 ;; Clears `c-found-types'.
3997 (setq c-found-types (make-vector 53 0)))
3999 (defun c-add-type (from to)
4000 ;; Add the given region as a type in `c-found-types'. If the region
4001 ;; doesn't match an existing type but there is a type which is equal
4002 ;; to the given one except that the last character is missing, then
4003 ;; the shorter type is removed. That's done to avoid adding all
4004 ;; prefixes of a type as it's being entered and font locked. This
4005 ;; doesn't cover cases like when characters are removed from a type
4006 ;; or added in the middle. We'd need the position of point when the
4007 ;; font locking is invoked to solve this well.
4009 ;; This function might do hidden buffer changes.
4010 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4011 (unless (intern-soft type c-found-types)
4012 (unintern (substring type 0 -1) c-found-types)
4013 (intern type c-found-types))))
4015 (defun c-unfind-type (name)
4016 ;; Remove the "NAME" from c-found-types, if present.
4017 (unintern name c-found-types))
4019 (defsubst c-check-type (from to)
4020 ;; Return non-nil if the given region contains a type in
4023 ;; This function might do hidden buffer changes.
4024 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4027 (defun c-list-found-types ()
4028 ;; Return all the types in `c-found-types' as a sorted list of
4031 (mapatoms (lambda (type)
4032 (setq type-list (cons (symbol-name type)
4035 (sort type-list 'string-lessp)))
4037 ;; Shut up the byte compiler.
4038 (defvar c-maybe-stale-found-type)
4040 (defun c-trim-found-types (beg end old-len)
4041 ;; An after change function which, in conjunction with the info in
4042 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4043 ;; from `c-found-types', should this type have become stale. For
4044 ;; example, this happens to "foo" when "foo \n bar();" becomes
4045 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4046 ;; the fontification.
4048 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4052 (when (< end (point-max))
4054 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4055 (progn (goto-char end)
4056 (c-end-of-current-token)))
4057 (c-unfind-type (buffer-substring-no-properties
4059 (when (> beg (point-min))
4061 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4062 (progn (goto-char beg)
4063 (c-beginning-of-current-token)))
4064 (c-unfind-type (buffer-substring-no-properties
4067 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4069 ;; Changing the amount of (already existing) whitespace - don't do anything.
4070 ((and (c-partial-ws-p beg end)
4071 (or (= beg end) ; removal of WS
4072 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4074 ;; The syntactic relationship which defined a "found type" has been
4076 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4077 (c-unfind-type (cadr c-maybe-stale-found-type)))
4078 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4082 ;; Handling of small scale constructs like types and names.
4084 (defun c-after-change-check-<>-operators (beg end)
4085 ;; This is called from `after-change-functions' when
4086 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
4087 ;; chars with paren syntax become part of another operator like "<<"
4090 ;; This function might do hidden buffer changes.
4094 (when (or (looking-at "[<>]")
4095 (< (skip-chars-backward "<>") 0))
4098 (c-beginning-of-current-token)
4099 (when (and (< (point) beg)
4100 (looking-at c-<>-multichar-token-regexp)
4101 (< beg (setq beg (match-end 0))))
4102 (while (progn (skip-chars-forward "^<>" beg)
4104 (c-clear-char-property (point) 'syntax-table)
4109 (when (or (looking-at "[<>]")
4110 (< (skip-chars-backward "<>") 0))
4113 (c-beginning-of-current-token)
4114 (when (and (< (point) end)
4115 (looking-at c-<>-multichar-token-regexp)
4116 (< end (setq end (match-end 0))))
4117 (while (progn (skip-chars-forward "^<>" end)
4119 (c-clear-char-property (point) 'syntax-table)
4120 (forward-char)))))))
4122 ;; Dynamically bound variable that instructs `c-forward-type' to also
4123 ;; treat possible types (i.e. those that it normally returns 'maybe or
4124 ;; 'found for) as actual types (and always return 'found for them).
4125 ;; This means that it records them in `c-record-type-identifiers' if
4126 ;; that is set, and that it adds them to `c-found-types'.
4127 (defvar c-promote-possible-types nil)
4129 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4130 ;; mark up successfully parsed arglists with paren syntax properties on
4131 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
4132 ;; `c-type' property of each argument separating comma.
4134 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
4135 ;; all arglists for side effects (i.e. recording types), otherwise it
4136 ;; exploits any existing paren syntax properties to quickly jump to the
4137 ;; end of already parsed arglists.
4139 ;; Marking up the arglists is not the default since doing that correctly
4140 ;; depends on a proper value for `c-restricted-<>-arglists'.
4141 (defvar c-parse-and-markup-<>-arglists nil)
4143 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4144 ;; not accept arglists that contain binary operators.
4146 ;; This is primarily used to handle C++ template arglists. C++
4147 ;; disambiguates them by checking whether the preceding name is a
4148 ;; template or not. We can't do that, so we assume it is a template
4149 ;; if it can be parsed as one. That usually works well since
4150 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
4151 ;; in almost all cases would be pointless.
4153 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
4154 ;; should let the comma separate the function arguments instead. And
4155 ;; in a context where the value of the expression is taken, e.g. in
4156 ;; "if (a < b || c > d)", it's probably not a template.
4157 (defvar c-restricted-<>-arglists nil)
4159 ;; Dynamically bound variables that instructs
4160 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
4161 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
4162 ;; `c-forward-label' to record the ranges of all the type and
4163 ;; reference identifiers they encounter. They will build lists on
4164 ;; these variables where each element is a cons of the buffer
4165 ;; positions surrounding each identifier. This recording is only
4166 ;; activated when `c-record-type-identifiers' is non-nil.
4168 ;; All known types that can't be identifiers are recorded, and also
4169 ;; other possible types if `c-promote-possible-types' is set.
4170 ;; Recording is however disabled inside angle bracket arglists that
4171 ;; are encountered inside names and other angle bracket arglists.
4172 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
4175 ;; Only the names in C++ template style references (e.g. "tmpl" in
4176 ;; "tmpl<a,b>::foo") are recorded as references, other references
4177 ;; aren't handled here.
4179 ;; `c-forward-label' records the label identifier(s) on
4180 ;; `c-record-ref-identifiers'.
4181 (defvar c-record-type-identifiers nil)
4182 (defvar c-record-ref-identifiers nil)
4184 ;; This variable will receive a cons cell of the range of the last
4185 ;; single identifier symbol stepped over by `c-forward-name' if it's
4186 ;; successful. This is the range that should be put on one of the
4187 ;; record lists above by the caller. It's assigned nil if there's no
4188 ;; such symbol in the name.
4189 (defvar c-last-identifier-range nil)
4191 (defmacro c-record-type-id (range)
4192 (if (eq (car-safe range) 'cons)
4194 `(setq c-record-type-identifiers
4195 (cons ,range c-record-type-identifiers))
4196 `(let ((range ,range))
4198 (setq c-record-type-identifiers
4199 (cons range c-record-type-identifiers))))))
4201 (defmacro c-record-ref-id (range)
4202 (if (eq (car-safe range) 'cons)
4204 `(setq c-record-ref-identifiers
4205 (cons ,range c-record-ref-identifiers))
4206 `(let ((range ,range))
4208 (setq c-record-ref-identifiers
4209 (cons range c-record-ref-identifiers))))))
4211 ;; Dynamically bound variable that instructs `c-forward-type' to
4212 ;; record the ranges of types that only are found. Behaves otherwise
4213 ;; like `c-record-type-identifiers'.
4214 (defvar c-record-found-types nil)
4216 (defmacro c-forward-keyword-prefixed-id (type)
4217 ;; Used internally in `c-forward-keyword-clause' to move forward
4218 ;; over a type (if TYPE is 'type) or a name (otherwise) which
4219 ;; possibly is prefixed by keywords and their associated clauses.
4220 ;; Try with a type/name first to not trip up on those that begin
4221 ;; with a keyword. Return t if a known or found type is moved
4222 ;; over. The point is clobbered if nil is returned. If range
4223 ;; recording is enabled, the identifier is recorded on as a type
4224 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
4226 ;; This macro might do hidden buffer changes.
4228 (while (if (setq res ,(if (eq type 'type)
4232 (and (looking-at c-keywords-regexp)
4233 (c-forward-keyword-clause 1))))
4234 (when (memq res '(t known found prefix))
4235 ,(when (eq type 'ref)
4236 `(when c-record-type-identifiers
4237 (c-record-ref-id c-last-identifier-range)))
4240 (defmacro c-forward-id-comma-list (type update-safe-pos)
4241 ;; Used internally in `c-forward-keyword-clause' to move forward
4242 ;; over a comma separated list of types or names using
4243 ;; `c-forward-keyword-prefixed-id'.
4245 ;; This macro might do hidden buffer changes.
4247 ,(when update-safe-pos
4248 `(setq safe-pos (point)))
4249 (eq (char-after) ?,))
4252 (c-forward-syntactic-ws)
4253 (c-forward-keyword-prefixed-id ,type)))))
4255 (defun c-forward-keyword-clause (match)
4256 ;; Submatch MATCH in the current match data is assumed to surround a
4257 ;; token. If it's a keyword, move over it and any immediately
4258 ;; following clauses associated with it, stopping at the start of
4259 ;; the next token. t is returned in that case, otherwise the point
4260 ;; stays and nil is returned. The kind of clauses that are
4261 ;; recognized are those specified by `c-type-list-kwds',
4262 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
4263 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
4264 ;; and `c-<>-arglist-kwds'.
4266 ;; This function records identifier ranges on
4267 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4268 ;; `c-record-type-identifiers' is non-nil.
4270 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
4271 ;; apply directly after the keyword, the type list is moved over
4272 ;; only when there is no unaccounted token before it (i.e. a token
4273 ;; that isn't moved over due to some other keyword list). The
4274 ;; identifier ranges in the list are still recorded if that should
4277 ;; This function might do hidden buffer changes.
4279 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
4280 ;; The call to `c-forward-<>-arglist' below is made after
4281 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
4282 ;; are angle bracket arglists and `c-restricted-<>-arglists'
4283 ;; should therefore be nil.
4284 (c-parse-and-markup-<>-arglists t)
4285 c-restricted-<>-arglists)
4288 (goto-char (match-end match))
4289 (c-forward-syntactic-ws)
4290 (setq safe-pos (point))
4293 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
4294 (c-forward-keyword-prefixed-id type))
4295 ;; There's a type directly after a keyword in `c-type-list-kwds'.
4296 (c-forward-id-comma-list type t))
4298 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
4299 (c-forward-keyword-prefixed-id ref))
4300 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
4301 (c-forward-id-comma-list ref t))
4303 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
4304 (eq (char-after) ?\())
4305 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
4308 (when (and (setq pos (c-up-list-forward))
4309 (eq (char-before pos) ?\)))
4310 (when (and c-record-type-identifiers
4311 (c-keyword-member kwd-sym 'c-paren-type-kwds))
4312 ;; Use `c-forward-type' on every identifier we can find
4313 ;; inside the paren, to record the types.
4314 (while (c-syntactic-re-search-forward c-symbol-start pos t)
4315 (goto-char (match-beginning 0))
4316 (unless (c-forward-type)
4317 (looking-at c-symbol-key) ; Always matches.
4318 (goto-char (match-end 0)))))
4321 (c-forward-syntactic-ws)
4322 (setq safe-pos (point))))
4324 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
4325 (eq (char-after) ?<)
4326 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
4327 (c-forward-syntactic-ws)
4328 (setq safe-pos (point)))
4330 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
4331 (not (looking-at c-symbol-start))
4332 (c-safe (c-forward-sexp) t))
4333 (c-forward-syntactic-ws)
4334 (setq safe-pos (point))))
4336 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
4337 (if (eq (char-after) ?:)
4338 ;; If we are at the colon already, we move over the type
4342 (c-forward-syntactic-ws)
4343 (when (c-forward-keyword-prefixed-id type)
4344 (c-forward-id-comma-list type t)))
4345 ;; Not at the colon, so stop here. But the identifier
4346 ;; ranges in the type list later on should still be
4348 (and c-record-type-identifiers
4350 ;; If a keyword matched both one of the types above and
4351 ;; this one, we match `c-colon-type-list-re' after the
4352 ;; clause matched above.
4353 (goto-char safe-pos)
4354 (looking-at c-colon-type-list-re))
4356 (goto-char (match-end 0))
4357 (c-forward-syntactic-ws)
4358 (c-forward-keyword-prefixed-id type))
4359 ;; There's a type after the `c-colon-type-list-re' match
4360 ;; after a keyword in `c-colon-type-list-kwds'.
4361 (c-forward-id-comma-list type nil))))
4363 (goto-char safe-pos)
4366 (defun c-forward-<>-arglist (all-types)
4367 ;; The point is assumed to be at a "<". Try to treat it as the open
4368 ;; paren of an angle bracket arglist and move forward to the the
4369 ;; corresponding ">". If successful, the point is left after the
4370 ;; ">" and t is returned, otherwise the point isn't moved and nil is
4371 ;; returned. If ALL-TYPES is t then all encountered arguments in
4372 ;; the arglist that might be types are treated as found types.
4374 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
4375 ;; function handles text properties on the angle brackets and argument
4376 ;; separating commas.
4378 ;; `c-restricted-<>-arglists' controls how lenient the template
4379 ;; arglist recognition should be.
4381 ;; This function records identifier ranges on
4382 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4383 ;; `c-record-type-identifiers' is non-nil.
4385 ;; This function might do hidden buffer changes.
4387 (let ((start (point))
4388 ;; If `c-record-type-identifiers' is set then activate
4389 ;; recording of any found types that constitute an argument in
4391 (c-record-found-types (if c-record-type-identifiers t)))
4392 (if (catch 'angle-bracket-arglist-escape
4393 (setq c-record-found-types
4394 (c-forward-<>-arglist-recur all-types)))
4396 (when (consp c-record-found-types)
4397 (setq c-record-type-identifiers
4398 ;; `nconc' doesn't mind that the tail of
4399 ;; `c-record-found-types' is t.
4400 (nconc c-record-found-types c-record-type-identifiers)))
4406 (defun c-forward-<>-arglist-recur (all-types)
4407 ;; Recursive part of `c-forward-<>-arglist'.
4409 ;; This function might do hidden buffer changes.
4411 (let ((start (point)) res pos tmp
4412 ;; Cover this so that any recorded found type ranges are
4413 ;; automatically lost if it turns out to not be an angle
4414 ;; bracket arglist. It's propagated through the return value
4415 ;; on successful completion.
4416 (c-record-found-types c-record-found-types)
4417 ;; List that collects the positions after the argument
4418 ;; separating ',' in the arglist.
4421 ;; If the '<' has paren open syntax then we've marked it as an angle
4422 ;; bracket arglist before, so skip to the end.
4423 (if (and (not c-parse-and-markup-<>-arglists)
4424 (c-get-char-property (point) 'syntax-table))
4428 (if (and (c-go-up-list-forward)
4429 (eq (char-before) ?>))
4432 ;; Got unmatched paren angle brackets. We don't clear the paren
4433 ;; syntax properties and retry, on the basis that it's very
4434 ;; unlikely that paren angle brackets become operators by code
4435 ;; manipulation. It's far more likely that it doesn't match due
4436 ;; to narrowing or some temporary change.
4441 (unless (looking-at c-<-op-cont-regexp)
4445 (when c-record-type-identifiers
4448 ;; All encountered identifiers are types, so set the
4449 ;; promote flag and parse the type.
4451 (c-forward-syntactic-ws)
4452 (when (looking-at c-identifier-start)
4453 (let ((c-promote-possible-types t))
4456 ;; Check if this arglist argument is a sole type. If
4457 ;; it's known then it's recorded in
4458 ;; `c-record-type-identifiers'. If it only is found
4459 ;; then it's recorded in `c-record-found-types' which we
4460 ;; might roll back if it turns out that this isn't an
4461 ;; angle bracket arglist afterall.
4462 (when (memq (char-before) '(?, ?<))
4463 (let ((orig-record-found-types c-record-found-types))
4464 (c-forward-syntactic-ws)
4465 (and (memq (c-forward-type) '(known found))
4466 (not (looking-at "[,>]"))
4467 ;; A found type was recorded but it's not the
4468 ;; only thing in the arglist argument, so reset
4469 ;; `c-record-found-types'.
4470 (setq c-record-found-types
4471 orig-record-found-types))))))
4474 (or (when (eq (char-after) ?>)
4475 ;; Must check for '>' at the very start separately,
4476 ;; since the regexp below has to avoid ">>" without
4481 ;; Note: These regexps exploit the match order in \| so
4482 ;; that "<>" is matched by "<" rather than "[^>:-]>".
4483 (c-syntactic-re-search-forward
4484 (if c-restricted-<>-arglists
4485 ;; Stop on ',', '|', '&', '+' and '-' to catch
4486 ;; common binary operators that could be between
4487 ;; two comparison expressions "a<b" and "c>d".
4488 "[<;{},|&+-]\\|\\([^>:-]>\\)"
4489 ;; Otherwise we still stop on ',' to find the
4490 ;; argument start positions.
4491 "[<;{},]\\|\\([^>:-]>\\)")
4494 ;; If the arglist starter has lost its open paren
4495 ;; syntax but not the closer, we won't find the
4496 ;; closer above since we only search in the
4497 ;; balanced sexp. In that case we stop just short
4498 ;; of it so check if the following char is the closer.
4499 (when (eq (char-after) ?>)
4504 ((eq (char-before) ?>)
4505 ;; Either an operator starting with '>' or the end of
4506 ;; the angle bracket arglist.
4508 (if (looking-at c->-op-cont-regexp)
4510 (goto-char (match-end 0))
4511 t) ; Continue the loop.
4513 ;; The angle bracket arglist is finished.
4514 (when c-parse-and-markup-<>-arglists
4515 (while arg-start-pos
4516 (c-put-c-type-property (1- (car arg-start-pos))
4518 (setq arg-start-pos (cdr arg-start-pos)))
4519 (c-mark-<-as-paren start)
4520 (c-mark->-as-paren (1- (point))))
4522 nil)) ; Exit the loop.
4524 ((eq (char-before) ?<)
4525 ;; Either an operator starting with '<' or a nested arglist.
4528 (let (id-start id-end subres keyword-match)
4529 (if (if (looking-at c-<-op-cont-regexp)
4530 (setq tmp (match-end 0))
4537 ;; There's always an identifier before an angle
4538 ;; bracket arglist, or a keyword in
4539 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
4540 (c-backward-syntactic-ws)
4541 (setq id-end (point))
4542 (c-simple-skip-symbol-backward)
4543 (when (or (setq keyword-match
4544 (looking-at c-opt-<>-sexp-key))
4545 (not (looking-at c-keywords-regexp)))
4546 (setq id-start (point))))
4549 (let ((c-record-type-identifiers nil)
4550 (c-record-found-types nil))
4551 (c-forward-<>-arglist-recur
4554 (c-keyword-sym (match-string 1))
4555 'c-<>-type-kwds)))))
4558 ;; It was not an angle bracket arglist.
4561 ;; It was an angle bracket arglist.
4562 (setq c-record-found-types subres)
4564 ;; Record the identifier before the template as a type
4565 ;; or reference depending on whether the arglist is last
4566 ;; in a qualified identifier.
4567 (when (and c-record-type-identifiers
4568 (not keyword-match))
4569 (if (and c-opt-identifier-concat-key
4571 (c-forward-syntactic-ws)
4572 (looking-at c-opt-identifier-concat-key)))
4573 (c-record-ref-id (cons id-start id-end))
4574 (c-record-type-id (cons id-start id-end))))))
4577 ((and (eq (char-before) ?,)
4578 (not c-restricted-<>-arglists))
4579 ;; Just another argument. Record the position. The
4580 ;; type check stuff that made us stop at it is at
4581 ;; the top of the loop.
4582 (setq arg-start-pos (cons (point) arg-start-pos)))
4585 ;; Got a character that can't be in an angle bracket
4586 ;; arglist argument. Abort using `throw', since
4587 ;; it's useless to try to find a surrounding arglist
4589 (throw 'angle-bracket-arglist-escape nil))))))
4592 (or c-record-found-types t)))))
4594 (defun c-backward-<>-arglist (all-types &optional limit)
4595 ;; The point is assumed to be directly after a ">". Try to treat it
4596 ;; as the close paren of an angle bracket arglist and move back to
4597 ;; the corresponding "<". If successful, the point is left at
4598 ;; the "<" and t is returned, otherwise the point isn't moved and
4599 ;; nil is returned. ALL-TYPES is passed on to
4600 ;; `c-forward-<>-arglist'.
4602 ;; If the optional LIMIT is given, it bounds the backward search.
4603 ;; It's then assumed to be at a syntactically relevant position.
4605 ;; This is a wrapper around `c-forward-<>-arglist'. See that
4606 ;; function for more details.
4608 (let ((start (point)))
4610 (if (and (not c-parse-and-markup-<>-arglists)
4611 (c-get-char-property (point) 'syntax-table))
4613 (if (and (c-go-up-list-backward)
4614 (eq (char-after) ?<))
4616 ;; See corresponding note in `c-forward-<>-arglist'.
4621 (c-syntactic-skip-backward "^<;{}" limit t)
4624 (if (eq (char-before) ?<)
4626 ;; Stopped at bob or a char that isn't allowed in an
4627 ;; arglist, so we've failed.
4632 (progn (c-beginning-of-current-token)
4634 ;; If we moved then the "<" was part of some
4635 ;; multicharacter token.
4639 (let ((beg-pos (point)))
4640 (if (c-forward-<>-arglist all-types)
4641 (cond ((= (point) start)
4642 ;; Matched the arglist. Break the while.
4646 ;; We started from a non-paren ">" inside an
4651 ;; Matched a shorter arglist. Can be a nested
4652 ;; one so continue looking.
4657 (/= (point) start))))
4659 (defun c-forward-name ()
4660 ;; Move forward over a complete name if at the beginning of one,
4661 ;; stopping at the next following token. If the point is not at
4662 ;; something that are recognized as name then it stays put. A name
4663 ;; could be something as simple as "foo" in C or something as
4664 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
4665 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
4666 ;; int>::*volatile const" in C++ (this function is actually little
4667 ;; more than a `looking-at' call in all modes except those that,
4668 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
4669 ;; name is found, 'template if it's an identifier ending with an
4670 ;; angle bracket arglist, 'operator of it's an operator identifier,
4671 ;; or t if it's some other kind of name.
4673 ;; This function records identifier ranges on
4674 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4675 ;; `c-record-type-identifiers' is non-nil.
4677 ;; This function might do hidden buffer changes.
4679 (let ((pos (point)) (start (point)) res id-start id-end
4680 ;; Turn off `c-promote-possible-types' here since we might
4681 ;; call `c-forward-<>-arglist' and we don't want it to promote
4682 ;; every suspect thing in the arglist to a type. We're
4683 ;; typically called from `c-forward-type' in this case, and
4684 ;; the caller only wants the top level type that it finds to
4686 c-promote-possible-types)
4689 (looking-at c-identifier-key)
4692 ;; Check for keyword. We go to the last symbol in
4693 ;; `c-identifier-key' first.
4694 (goto-char (setq id-end (match-end 0)))
4695 (c-simple-skip-symbol-backward)
4696 (setq id-start (point))
4698 (if (looking-at c-keywords-regexp)
4699 (when (and (c-major-mode-is 'c++-mode)
4701 (cc-eval-when-compile
4702 (concat "\\(operator\\|\\(template\\)\\)"
4703 "\\(" (c-lang-const c-nonsymbol-key c++)
4705 (if (match-beginning 2)
4706 ;; "template" is only valid inside an
4707 ;; identifier if preceded by "::".
4709 (c-backward-syntactic-ws)
4710 (and (c-safe (backward-char 2) t)
4714 ;; Handle a C++ operator or template identifier.
4716 (c-forward-syntactic-ws)
4717 (cond ((eq (char-before id-end) ?e)
4718 ;; Got "... ::template".
4719 (let ((subres (c-forward-name)))
4724 ((looking-at c-identifier-start)
4725 ;; Got a cast operator.
4726 (when (c-forward-type)
4729 ;; Now we should match a sequence of either
4730 ;; '*', '&' or a name followed by ":: *",
4731 ;; where each can be followed by a sequence
4732 ;; of `c-opt-type-modifier-key'.
4733 (while (cond ((looking-at "[*&]")
4734 (goto-char (match-end 0))
4736 ((looking-at c-identifier-start)
4737 (and (c-forward-name)
4740 (goto-char (match-end 0))
4741 (c-forward-syntactic-ws)
4742 (eq (char-after) ?*))
4747 (c-forward-syntactic-ws)
4749 (looking-at c-opt-type-modifier-key))
4750 (goto-char (match-end 1))))))
4752 ((looking-at c-overloadable-operators-regexp)
4753 ;; Got some other operator.
4754 (setq c-last-identifier-range
4755 (cons (point) (match-end 0)))
4756 (goto-char (match-end 0))
4757 (c-forward-syntactic-ws)
4763 ;; `id-start' is equal to `id-end' if we've jumped over
4764 ;; an identifier that doesn't end with a symbol token.
4765 ;; That can occur e.g. for Java import directives on the
4766 ;; form "foo.bar.*".
4767 (when (and id-start (/= id-start id-end))
4768 (setq c-last-identifier-range
4769 (cons id-start id-end)))
4771 (c-forward-syntactic-ws)
4777 (when (or c-opt-identifier-concat-key
4778 c-recognize-<>-arglists)
4781 ((and c-opt-identifier-concat-key
4782 (looking-at c-opt-identifier-concat-key))
4783 ;; Got a concatenated identifier. This handles the
4784 ;; cases with tricky syntactic whitespace that aren't
4785 ;; covered in `c-identifier-key'.
4786 (goto-char (match-end 0))
4787 (c-forward-syntactic-ws)
4790 ((and c-recognize-<>-arglists
4791 (eq (char-after) ?<))
4792 ;; Maybe an angle bracket arglist.
4794 (when (let (c-record-type-identifiers
4795 c-record-found-types)
4796 (c-forward-<>-arglist nil))
4798 (c-add-type start (1+ pos))
4799 (c-forward-syntactic-ws)
4801 c-last-identifier-range nil)
4803 (if (and c-opt-identifier-concat-key
4804 (looking-at c-opt-identifier-concat-key))
4806 ;; Continue if there's an identifier concatenation
4807 ;; operator after the template argument.
4809 (when (and c-record-type-identifiers id-start)
4810 (c-record-ref-id (cons id-start id-end)))
4812 (c-forward-syntactic-ws)
4815 (when (and c-record-type-identifiers id-start)
4816 (c-record-type-id (cons id-start id-end)))
4817 (setq res 'template)
4824 (defun c-forward-type ()
4825 ;; Move forward over a type spec if at the beginning of one,
4826 ;; stopping at the next following token. Return t if it's a known
4827 ;; type that can't be a name or other expression, 'known if it's an
4828 ;; otherwise known type (according to `*-font-lock-extra-types'),
4829 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4830 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4831 ;; that might be a type, or nil if it can't be a type (the point
4832 ;; isn't moved then). The point is assumed to be at the beginning
4835 ;; Note that this function doesn't skip past the brace definition
4836 ;; that might be considered part of the type, e.g.
4837 ;; "enum {a, b, c} foo".
4839 ;; This function records identifier ranges on
4840 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4841 ;; `c-record-type-identifiers' is non-nil.
4843 ;; This function might do hidden buffer changes.
4845 (let ((start (point)) pos res name-res id-start id-end id-range)
4847 ;; Skip leading type modifiers. If any are found we know it's a
4848 ;; prefix of a type.
4849 (when c-opt-type-modifier-key
4850 (while (looking-at c-opt-type-modifier-key)
4851 (goto-char (match-end 1))
4852 (c-forward-syntactic-ws)
4853 (setq res 'prefix)))
4856 ((looking-at c-type-prefix-key)
4857 ;; Looking at a keyword that prefixes a type identifier,
4859 (goto-char (match-end 1))
4860 (c-forward-syntactic-ws)
4862 (if (memq (setq name-res (c-forward-name)) '(t template))
4864 (when (eq name-res t)
4865 ;; In many languages the name can be used without the
4866 ;; prefix, so we add it to `c-found-types'.
4867 (c-add-type pos (point))
4868 (when (and c-record-type-identifiers
4869 c-last-identifier-range)
4870 (c-record-type-id c-last-identifier-range)))
4878 (if (looking-at c-identifier-start)
4880 (setq id-start (point)
4881 name-res (c-forward-name))
4883 (setq id-end (point)
4884 id-range c-last-identifier-range))))
4885 (and (cond ((looking-at c-primitive-type-key)
4887 ((c-with-syntax-table c-identifier-syntax-table
4888 (looking-at c-known-type-key))
4893 (goto-char (match-end 1))
4894 (c-forward-syntactic-ws)
4895 (setq pos (point))))
4898 ;; Looking at a primitive or known type identifier. We've
4899 ;; checked for a name first so that we don't go here if the
4900 ;; known type match only is a prefix of another name.
4902 (setq id-end (match-end 1))
4904 (when (and c-record-type-identifiers
4905 (or c-promote-possible-types (eq res t)))
4906 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4908 (if (and c-opt-type-component-key
4910 (looking-at c-opt-type-component-key)))
4911 ;; There might be more keywords for the type.
4913 (c-forward-keyword-clause 1)
4915 (setq safe-pos (point))
4916 (looking-at c-opt-type-component-key))
4917 (when (and c-record-type-identifiers
4918 (looking-at c-primitive-type-key))
4919 (c-record-type-id (cons (match-beginning 1)
4921 (c-forward-keyword-clause 1))
4922 (if (looking-at c-primitive-type-key)
4924 (when c-record-type-identifiers
4925 (c-record-type-id (cons (match-beginning 1)
4927 (c-forward-keyword-clause 1)
4929 (goto-char safe-pos)
4930 (setq res 'prefix)))
4931 (unless (save-match-data (c-forward-keyword-clause 1))
4934 (goto-char (match-end 1))
4935 (c-forward-syntactic-ws)))))
4938 (cond ((eq name-res t)
4939 ;; A normal identifier.
4941 (if (or res c-promote-possible-types)
4943 (c-add-type id-start id-end)
4944 (when (and c-record-type-identifiers id-range)
4945 (c-record-type-id id-range))
4948 (setq res (if (c-check-type id-start id-end)
4949 ;; It's an identifier that has been used as
4950 ;; a type somewhere else.
4952 ;; It's an identifier that might be a type.
4954 ((eq name-res 'template)
4955 ;; A template is a type.
4959 ;; Otherwise it's an operator identifier, which is not a type.
4964 ;; Skip trailing type modifiers. If any are found we know it's
4966 (when c-opt-type-modifier-key
4967 (while (looking-at c-opt-type-modifier-key)
4968 (goto-char (match-end 1))
4969 (c-forward-syntactic-ws)
4972 ;; Step over any type suffix operator. Do not let the existence
4973 ;; of these alter the classification of the found type, since
4974 ;; these operators typically are allowed in normal expressions
4976 (when c-opt-type-suffix-key
4977 (while (looking-at c-opt-type-suffix-key)
4978 (goto-char (match-end 1))
4979 (c-forward-syntactic-ws)))
4981 (when c-opt-type-concat-key
4982 ;; Look for a trailing operator that concatenates the type
4983 ;; with a following one, and if so step past that one through
4984 ;; a recursive call. Note that we don't record concatenated
4985 ;; types in `c-found-types' - it's the component types that
4986 ;; are recorded when appropriate.
4988 (let* ((c-promote-possible-types (or (memq res '(t known))
4989 c-promote-possible-types))
4990 ;; If we can't promote then set `c-record-found-types' so that
4991 ;; we can merge in the types from the second part afterwards if
4992 ;; it turns out to be a known type there.
4993 (c-record-found-types (and c-record-type-identifiers
4994 (not c-promote-possible-types)))
4996 (if (and (looking-at c-opt-type-concat-key)
4999 (goto-char (match-end 1))
5000 (c-forward-syntactic-ws)
5001 (setq subres (c-forward-type))))
5004 ;; If either operand certainly is a type then both are, but we
5005 ;; don't let the existence of the operator itself promote two
5006 ;; uncertain types to a certain one.
5009 (unless (eq name-res 'template)
5010 (c-add-type id-start id-end))
5011 (when (and c-record-type-identifiers id-range)
5012 (c-record-type-id id-range))
5023 (when (and (eq res t)
5024 (consp c-record-found-types))
5025 ;; Merge in the ranges of any types found by the second
5026 ;; `c-forward-type'.
5027 (setq c-record-type-identifiers
5028 ;; `nconc' doesn't mind that the tail of
5029 ;; `c-record-found-types' is t.
5030 (nconc c-record-found-types
5031 c-record-type-identifiers))))
5035 (when (and c-record-found-types (memq res '(known found)) id-range)
5036 (setq c-record-found-types
5037 (cons id-range c-record-found-types))))
5039 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
5044 ;; Handling of large scale constructs like statements and declarations.
5046 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
5047 ;; defsubst or perhaps even a defun, but it contains lots of free
5048 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
5049 (defmacro c-fdoc-shift-type-backward (&optional short)
5050 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
5051 ;; of types when parsing a declaration, which means that it
5052 ;; sometimes consumes the identifier in the declaration as a type.
5053 ;; This is used to "backtrack" and make the last type be treated as
5054 ;; an identifier instead.
5057 ;; These identifiers are bound only in the inner let.
5058 '(setq identifier-type at-type
5059 identifier-start type-start
5063 got-suffix-after-parens id-start
5066 (if (setq at-type (if (eq backup-at-type 'prefix)
5069 (setq type-start backup-type-start
5070 id-start backup-id-start)
5071 (setq type-start start-pos
5072 id-start start-pos))
5074 ;; When these flags already are set we've found specifiers that
5075 ;; unconditionally signal these attributes - backtracking doesn't
5076 ;; change that. So keep them set in that case.
5078 (setq at-type-decl backup-at-type-decl))
5080 (setq maybe-typeless backup-maybe-typeless))
5083 ;; This identifier is bound only in the inner let.
5084 '(setq start id-start))))
5086 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
5087 ;; Move forward over a declaration or a cast if at the start of one.
5088 ;; The point is assumed to be at the start of some token. Nil is
5089 ;; returned if no declaration or cast is recognized, and the point
5090 ;; is clobbered in that case.
5092 ;; If a declaration is parsed:
5094 ;; The point is left at the first token after the first complete
5095 ;; declarator, if there is one. The return value is a cons where
5096 ;; the car is the position of the first token in the declarator. (See
5097 ;; below for the cdr.)
5100 ;; void foo (int a, char *b) stuff ...
5104 ;; unsigned int a = c_style_initializer, b;
5106 ;; unsigned int a (cplusplus_style_initializer), b;
5107 ;; car ^ ^ point (might change)
5108 ;; class Foo : public Bar {}
5110 ;; class PikeClass (int a, string b) stuff ...
5116 ;; void cplusplus_function (int x) throw (Bad);
5118 ;; Foo::Foo (int b) : Base (b) {}
5121 ;; The cdr of the return value is non-nil iff a `c-typedef-decl-kwds'
5122 ;; specifier (e.g. class, struct, enum, typedef) is found in the
5123 ;; declaration, i.e. the declared identifier(s) are types.
5125 ;; If a cast is parsed:
5127 ;; The point is left at the first token after the closing paren of
5128 ;; the cast. The return value is `cast'. Note that the start
5129 ;; position must be at the first token inside the cast parenthesis
5132 ;; PRECEDING-TOKEN-END is the first position after the preceding
5133 ;; token, i.e. on the other side of the syntactic ws from the point.
5134 ;; Use a value less than or equal to (point-min) if the point is at
5135 ;; the first token in (the visible part of) the buffer.
5137 ;; CONTEXT is a symbol that describes the context at the point:
5138 ;; 'decl In a comma-separated declaration context (typically
5139 ;; inside a function declaration arglist).
5140 ;; '<> In an angle bracket arglist.
5141 ;; 'arglist Some other type of arglist.
5142 ;; nil Some other context or unknown context.
5144 ;; LAST-CAST-END is the first token after the closing paren of a
5145 ;; preceding cast, or nil if none is known. If
5146 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
5147 ;; the position after the closest preceding call where a cast was
5148 ;; matched. In that case it's used to discover chains of casts like
5151 ;; This function records identifier ranges on
5152 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5153 ;; `c-record-type-identifiers' is non-nil.
5155 ;; This function might do hidden buffer changes.
5157 (let (;; `start-pos' is used below to point to the start of the
5158 ;; first type, i.e. after any leading specifiers. It might
5159 ;; also point at the beginning of the preceding syntactic
5162 ;; Set to the result of `c-forward-type'.
5164 ;; The position of the first token in what we currently
5165 ;; believe is the type in the declaration or cast, after any
5166 ;; specifiers and their associated clauses.
5168 ;; The position of the first token in what we currently
5169 ;; believe is the declarator for the first identifier. Set
5170 ;; when the type is found, and moved forward over any
5171 ;; `c-decl-hangon-kwds' and their associated clauses that
5172 ;; occurs after the type.
5174 ;; These store `at-type', `type-start' and `id-start' of the
5175 ;; identifier before the one in those variables. The previous
5176 ;; identifier might turn out to be the real type in a
5177 ;; declaration if the last one has to be the declarator in it.
5178 ;; If `backup-at-type' is nil then the other variables have
5179 ;; undefined values.
5180 backup-at-type backup-type-start backup-id-start
5181 ;; Set if we've found a specifier that makes the defined
5182 ;; identifier(s) types.
5184 ;; Set if we've found a specifier that can start a declaration
5185 ;; where there's no type.
5187 ;; If a specifier is found that also can be a type prefix,
5188 ;; these flags are set instead of those above. If we need to
5189 ;; back up an identifier, they are copied to the real flag
5190 ;; variables. Thus they only take effect if we fail to
5191 ;; interpret it as a type.
5192 backup-at-type-decl backup-maybe-typeless
5193 ;; Whether we've found a declaration or a cast. We might know
5194 ;; this before we've found the type in it. It's 'ids if we've
5195 ;; found two consecutive identifiers (usually a sure sign, but
5196 ;; we should allow that in labels too), and t if we've found a
5197 ;; specifier keyword (a 100% sure sign).
5199 ;; Set when we need to back up to parse this as a declaration
5200 ;; but not as a cast.
5202 ;; For casts, the return position.
5204 ;; Save `c-record-type-identifiers' and
5205 ;; `c-record-ref-identifiers' since ranges are recorded
5206 ;; speculatively and should be thrown away if it turns out
5207 ;; that it isn't a declaration or cast.
5208 (save-rec-type-ids c-record-type-identifiers)
5209 (save-rec-ref-ids c-record-ref-identifiers))
5211 ;; Check for a type. Unknown symbols are treated as possible
5212 ;; types, but they could also be specifiers disguised through
5213 ;; macros like __INLINE__, so we recognize both types and known
5214 ;; specifiers after them too.
5216 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
5218 ;; Look for a specifier keyword clause.
5219 (when (looking-at c-prefix-spec-kwds-re)
5220 (setq kwd-sym (c-keyword-sym (match-string 1)))
5222 (c-forward-keyword-clause 1)
5223 (setq kwd-clause-end (point))))
5225 (when (setq found-type (c-forward-type))
5226 ;; Found a known or possible type or a prefix of a known type.
5229 ;; Got two identifiers with nothing but whitespace
5230 ;; between them. That can only happen in declarations.
5231 (setq at-decl-or-cast 'ids)
5233 (when (eq at-type 'found)
5234 ;; If the previous identifier is a found type we
5235 ;; record it as a real one; it might be some sort of
5236 ;; alias for a prefix like "unsigned".
5238 (goto-char type-start)
5239 (let ((c-promote-possible-types t))
5240 (c-forward-type)))))
5242 (setq backup-at-type at-type
5243 backup-type-start type-start
5244 backup-id-start id-start
5248 ;; The previous ambiguous specifier/type turned out
5249 ;; to be a type since we've parsed another one after
5250 ;; it, so clear these backup flags.
5251 backup-at-type-decl nil
5252 backup-maybe-typeless nil))
5256 ;; Handle known specifier keywords and
5257 ;; `c-decl-hangon-kwds' which can occur after known
5260 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
5261 ;; It's a hang-on keyword that can occur anywhere.
5263 (setq at-decl-or-cast t)
5265 ;; Move the identifier start position if
5266 ;; we've passed a type.
5267 (setq id-start kwd-clause-end)
5268 ;; Otherwise treat this as a specifier and
5269 ;; move the fallback position.
5270 (setq start-pos kwd-clause-end))
5271 (goto-char kwd-clause-end))
5273 ;; It's an ordinary specifier so we know that
5274 ;; anything before this can't be the type.
5275 (setq backup-at-type nil
5276 start-pos kwd-clause-end)
5279 ;; It's ambiguous whether this keyword is a
5280 ;; specifier or a type prefix, so set the backup
5281 ;; flags. (It's assumed that `c-forward-type'
5282 ;; moved further than `c-forward-keyword-clause'.)
5284 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5285 (setq backup-at-type-decl t))
5286 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5287 (setq backup-maybe-typeless t)))
5289 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5290 (setq at-type-decl t))
5291 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5292 (setq maybe-typeless t))
5294 ;; Haven't matched a type so it's an umambiguous
5295 ;; specifier keyword and we know we're in a
5297 (setq at-decl-or-cast t)
5299 (goto-char kwd-clause-end))))
5301 ;; If the type isn't known we continue so that we'll jump
5302 ;; over all specifiers and type identifiers. The reason
5303 ;; to do this for a known type prefix is to make things
5304 ;; like "unsigned INT16" work.
5305 (and found-type (not (eq found-type t))))))
5309 ;; If a known type was found, we still need to skip over any
5310 ;; hangon keyword clauses after it. Otherwise it has already
5311 ;; been done in the loop above.
5312 (while (looking-at c-decl-hangon-key)
5313 (c-forward-keyword-clause 1))
5314 (setq id-start (point)))
5316 ((eq at-type 'prefix)
5317 ;; A prefix type is itself a primitive type when it's not
5318 ;; followed by another type.
5322 ;; Got no type but set things up to continue anyway to handle
5323 ;; the various cases when a declaration doesn't start with a
5325 (setq id-start start-pos))
5327 ((and (eq at-type 'maybe)
5328 (c-major-mode-is 'c++-mode))
5329 ;; If it's C++ then check if the last "type" ends on the form
5330 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
5331 ;; (con|de)structor.
5333 (let (name end-2 end-1)
5334 (goto-char id-start)
5335 (c-backward-syntactic-ws)
5336 (setq end-2 (point))
5338 (c-simple-skip-symbol-backward)
5341 (buffer-substring-no-properties (point) end-2))
5342 ;; Cheating in the handling of syntactic ws below.
5343 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
5345 (setq end-1 (point))
5346 (c-simple-skip-symbol-backward))
5347 (>= (point) type-start)
5348 (equal (buffer-substring-no-properties (point) end-1)
5350 ;; It is a (con|de)structor name. In that case the
5351 ;; declaration is typeless so zap out any preceding
5352 ;; identifier(s) that we might have taken as types.
5353 (goto-char type-start)
5356 id-start type-start))))))
5358 ;; Check for and step over a type decl expression after the thing
5359 ;; that is or might be a type. This can't be skipped since we
5360 ;; need the correct end position of the declarator for
5361 ;; `max-type-decl-end-*'.
5362 (let ((start (point)) (paren-depth 0) pos
5363 ;; True if there's a non-open-paren match of
5364 ;; `c-type-decl-prefix-key'.
5366 ;; True if the declarator is surrounded by a parenthesis pair.
5368 ;; True if there is an identifier in the declarator.
5370 ;; True if there's a non-close-paren match of
5371 ;; `c-type-decl-suffix-key'.
5373 ;; True if there's a prefix match outside the outermost
5374 ;; paren pair that surrounds the declarator.
5375 got-prefix-before-parens
5376 ;; True if there's a suffix match outside the outermost
5377 ;; paren pair that surrounds the declarator. The value is
5378 ;; the position of the first suffix match.
5379 got-suffix-after-parens
5380 ;; True if we've parsed the type decl to a token that is
5381 ;; known to end declarations in this context.
5383 ;; The earlier values of `at-type' and `type-start' if we've
5384 ;; shifted the type backwards.
5385 identifier-type identifier-start
5386 ;; If `c-parse-and-markup-<>-arglists' is set we need to
5387 ;; turn it off during the name skipping below to avoid
5388 ;; getting `c-type' properties that might be bogus. That
5389 ;; can happen since we don't know if
5390 ;; `c-restricted-<>-arglists' will be correct inside the
5391 ;; arglist paren that gets entered.
5392 c-parse-and-markup-<>-arglists)
5394 (goto-char id-start)
5396 ;; Skip over type decl prefix operators. (Note similar code in
5397 ;; `c-font-lock-declarators'.)
5398 (while (and (looking-at c-type-decl-prefix-key)
5399 (if (and (c-major-mode-is 'c++-mode)
5400 (match-beginning 2))
5401 ;; If the second submatch matches in C++ then
5402 ;; we're looking at an identifier that's a
5403 ;; prefix only if it specifies a member pointer.
5404 (when (setq got-identifier (c-forward-name))
5405 (if (looking-at "\\(::\\)")
5406 ;; We only check for a trailing "::" and
5407 ;; let the "*" that should follow be
5408 ;; matched in the next round.
5409 (progn (setq got-identifier nil) t)
5410 ;; It turned out to be the real identifier,
5415 (if (eq (char-after) ?\()
5417 (setq paren-depth (1+ paren-depth))
5419 (unless got-prefix-before-parens
5420 (setq got-prefix-before-parens (= paren-depth 0)))
5422 (goto-char (match-end 1)))
5423 (c-forward-syntactic-ws))
5425 (setq got-parens (> paren-depth 0))
5427 ;; Skip over an identifier.
5429 (and (looking-at c-identifier-start)
5430 (setq got-identifier (c-forward-name))))
5432 ;; Skip over type decl suffix operators.
5433 (while (if (looking-at c-type-decl-suffix-key)
5435 (if (eq (char-after) ?\))
5436 (when (> paren-depth 0)
5437 (setq paren-depth (1- paren-depth))
5440 (when (if (save-match-data (looking-at "\\s\("))
5441 (c-safe (c-forward-sexp 1) t)
5442 (goto-char (match-end 1))
5444 (when (and (not got-suffix-after-parens)
5446 (setq got-suffix-after-parens (match-beginning 0)))
5447 (setq got-suffix t)))
5449 ;; No suffix matched. We might have matched the
5450 ;; identifier as a type and the open paren of a
5451 ;; function arglist as a type decl prefix. In that
5452 ;; case we should "backtrack": Reinterpret the last
5453 ;; type as the identifier, move out of the arglist and
5454 ;; continue searching for suffix operators.
5456 ;; Do this even if there's no preceding type, to cope
5457 ;; with old style function declarations in K&R C,
5458 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
5459 ;; style declarations. That isn't applicable in an
5460 ;; arglist context, though.
5461 (when (and (= paren-depth 1)
5462 (not got-prefix-before-parens)
5463 (not (eq at-type t))
5466 backup-maybe-typeless
5467 (when c-recognize-typeless-decls
5469 (setq pos (c-up-list-forward (point)))
5470 (eq (char-before pos) ?\)))
5471 (c-fdoc-shift-type-backward)
5475 (c-forward-syntactic-ws))
5477 (when (and (or maybe-typeless backup-maybe-typeless)
5478 (not got-identifier)
5481 ;; Have found no identifier but `c-typeless-decl-kwds' has
5482 ;; matched so we know we're inside a declaration. The
5483 ;; preceding type must be the identifier instead.
5484 (c-fdoc-shift-type-backward))
5488 (catch 'at-decl-or-cast
5490 (when (> paren-depth 0)
5491 ;; Encountered something inside parens that isn't matched by
5492 ;; the `c-type-decl-*' regexps, so it's not a type decl
5493 ;; expression. Try to skip out to the same paren depth to
5494 ;; not confuse the cast check below.
5495 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
5496 ;; If we've found a specifier keyword then it's a
5497 ;; declaration regardless.
5498 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
5501 (looking-at (cond ((eq context '<>) "[,>]")
5505 ;; Now we've collected info about various characteristics of
5506 ;; the construct we're looking at. Below follows a decision
5507 ;; tree based on that. It's ordered to check more certain
5508 ;; signs before less certain ones.
5513 (when (and (or at-type maybe-typeless)
5514 (not (or got-prefix got-parens)))
5515 ;; Got another identifier directly after the type, so it's a
5517 (throw 'at-decl-or-cast t))
5519 (when (and got-parens
5521 (not got-suffix-after-parens)
5524 backup-maybe-typeless))
5525 ;; Got a declaration of the form "foo bar (gnu);" where we've
5526 ;; recognized "bar" as the type and "gnu" as the declarator.
5527 ;; In this case it's however more likely that "bar" is the
5528 ;; declarator and "gnu" a function argument or initializer (if
5529 ;; `c-recognize-paren-inits' is set), since the parens around
5530 ;; "gnu" would be superfluous if it's a declarator. Shift the
5531 ;; type one step backward.
5532 (c-fdoc-shift-type-backward)))
5534 ;; Found no identifier.
5539 (when (= (point) start)
5540 ;; Got a plain list of identifiers. If a colon follows it's
5541 ;; a valid label. Otherwise the last one probably is the
5542 ;; declared identifier and we should back up to the previous
5543 ;; type, providing it isn't a cast.
5544 (if (eq (char-after) ?:)
5545 ;; If we've found a specifier keyword then it's a
5546 ;; declaration regardless.
5547 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
5548 (setq backup-if-not-cast t)
5549 (throw 'at-decl-or-cast t)))
5551 (when (and got-suffix
5554 ;; Got a plain list of identifiers followed by some suffix.
5555 ;; If this isn't a cast then the last identifier probably is
5556 ;; the declared one and we should back up to the previous
5558 (setq backup-if-not-cast t)
5559 (throw 'at-decl-or-cast t)))
5561 (when (eq at-type t)
5562 ;; If the type is known we know that there can't be any
5563 ;; identifier somewhere else, and it's only in declarations in
5564 ;; e.g. function prototypes and in casts that the identifier may
5566 (throw 'at-decl-or-cast t))
5568 (when (= (point) start)
5569 ;; Only got a single identifier (parsed as a type so far).
5571 ;; Check that the identifier isn't at the start of an
5576 ;; Inside an arglist that contains declarations. If K&R
5577 ;; style declarations and parenthesis style initializers
5578 ;; aren't allowed then the single identifier must be a
5579 ;; type, else we require that it's known or found
5580 ;; (primitive types are handled above).
5581 (or (and (not c-recognize-knr-p)
5582 (not c-recognize-paren-inits))
5583 (memq at-type '(known found))))
5585 ;; Inside a template arglist. Accept known and found
5586 ;; types; other identifiers could just as well be
5587 ;; constants in C++.
5588 (memq at-type '(known found)))))
5589 (throw 'at-decl-or-cast t)
5590 ;; Can't be a valid declaration or cast, but if we've found a
5591 ;; specifier it can't be anything else either, so treat it as
5592 ;; an invalid/unfinished declaration or cast.
5593 (throw 'at-decl-or-cast at-decl-or-cast))))
5598 (not (eq at-type t))
5601 backup-maybe-typeless
5602 (when c-recognize-typeless-decls
5603 (or (not got-suffix)
5605 c-after-suffixed-type-maybe-decl-key))))))
5606 ;; Got an empty paren pair and a preceding type that probably
5607 ;; really is the identifier. Shift the type backwards to make
5608 ;; the last one the identifier. This is analogous to the
5609 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
5612 ;; Exception: In addition to the conditions in that
5613 ;; "backtracking" code, do not shift backward if we're not
5614 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
5615 ;; Since there's no preceding type, the shift would mean that
5616 ;; the declaration is typeless. But if the regexp doesn't match
5617 ;; then we will simply fall through in the tests below and not
5618 ;; recognize it at all, so it's better to try it as an abstract
5619 ;; declarator instead.
5620 (c-fdoc-shift-type-backward)
5622 ;; Still no identifier.
5624 (when (and got-prefix (or got-parens got-suffix))
5625 ;; Require `got-prefix' together with either `got-parens' or
5626 ;; `got-suffix' to recognize it as an abstract declarator:
5627 ;; `got-parens' only is probably an empty function call.
5628 ;; `got-suffix' only can build an ordinary expression together
5629 ;; with the preceding identifier which we've taken as a type.
5630 ;; We could actually accept on `got-prefix' only, but that can
5631 ;; easily occur temporarily while writing an expression so we
5632 ;; avoid that case anyway. We could do a better job if we knew
5633 ;; the point when the fontification was invoked.
5634 (throw 'at-decl-or-cast t))
5639 got-suffix-after-parens
5640 (eq (char-after got-suffix-after-parens) ?\())
5641 ;; Got a type, no declarator but a paren suffix. I.e. it's a
5642 ;; normal function call afterall (or perhaps a C++ style object
5643 ;; instantiation expression).
5644 (throw 'at-decl-or-cast nil))))
5646 (when at-decl-or-cast
5647 ;; By now we've located the type in the declaration that we know
5649 (throw 'at-decl-or-cast t))
5651 (when (and got-identifier
5653 (looking-at c-after-suffixed-type-decl-key)
5657 (not (eq at-type t)))
5658 ;; Shift the type backward in the case that there's a
5659 ;; single identifier inside parens. That can only
5660 ;; occur in K&R style function declarations so it's
5661 ;; more likely that it really is a function call.
5662 ;; Therefore we only do this after
5663 ;; `c-after-suffixed-type-decl-key' has matched.
5664 (progn (c-fdoc-shift-type-backward) t)
5665 got-suffix-after-parens))
5666 ;; A declaration according to `c-after-suffixed-type-decl-key'.
5667 (throw 'at-decl-or-cast t))
5669 (when (and (or got-prefix (not got-parens))
5670 (memq at-type '(t known)))
5671 ;; It's a declaration if a known type precedes it and it can't be a
5673 (throw 'at-decl-or-cast t))
5675 ;; If we get here we can't tell if this is a type decl or a normal
5676 ;; expression by looking at it alone. (That's under the assumption
5677 ;; that normal expressions always can look like type decl expressions,
5678 ;; which isn't really true but the cases where it doesn't hold are so
5679 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
5680 ;; the effort to look for them.)
5682 (unless (or at-decl-end (looking-at "=[^=]"))
5683 ;; If this is a declaration it should end here or its initializer(*)
5684 ;; should start here, so check for allowed separation tokens. Note
5685 ;; that this rule doesn't work e.g. with a K&R arglist after a
5688 ;; *) Don't check for C++ style initializers using parens
5689 ;; since those already have been matched as suffixes.
5691 ;; If `at-decl-or-cast' is then we've found some other sign that
5692 ;; it's a declaration or cast, so then it's probably an
5693 ;; invalid/unfinished one.
5694 (throw 'at-decl-or-cast at-decl-or-cast))
5696 ;; Below are tests that only should be applied when we're certain to
5697 ;; not have parsed halfway through an expression.
5699 (when (memq at-type '(t known))
5700 ;; The expression starts with a known type so treat it as a
5702 (throw 'at-decl-or-cast t))
5704 (when (and (c-major-mode-is 'c++-mode)
5705 ;; In C++ we check if the identifier is a known type, since
5706 ;; (con|de)structors use the class name as identifier.
5707 ;; We've always shifted over the identifier as a type and
5708 ;; then backed up again in this case.
5710 (or (memq identifier-type '(found known))
5711 (and (eq (char-after identifier-start) ?~)
5712 ;; `at-type' probably won't be 'found for
5713 ;; destructors since the "~" is then part of the
5714 ;; type name being checked against the list of
5715 ;; known types, so do a check without that
5718 (goto-char (1+ identifier-start))
5719 (c-forward-syntactic-ws)
5720 (c-with-syntax-table
5721 c-identifier-syntax-table
5722 (looking-at c-known-type-key)))
5724 (goto-char (1+ identifier-start))
5725 ;; We have already parsed the type earlier,
5726 ;; so it'd be possible to cache the end
5727 ;; position instead of redoing it here, but
5728 ;; then we'd need to keep track of another
5729 ;; position everywhere.
5730 (c-check-type (point)
5731 (progn (c-forward-type)
5733 (throw 'at-decl-or-cast t))
5737 (when (and got-prefix-before-parens
5739 (or at-decl-end (looking-at "=[^=]"))
5742 ;; Got something like "foo * bar;". Since we're not inside an
5743 ;; arglist it would be a meaningless expression because the
5744 ;; result isn't used. We therefore choose to recognize it as
5745 ;; a declaration. Do not allow a suffix since it could then
5746 ;; be a function call.
5747 (throw 'at-decl-or-cast t))
5749 (when (and (or got-suffix-after-parens
5750 (looking-at "=[^=]"))
5752 (not (eq context 'arglist)))
5753 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
5754 ;; be an odd expression or it could be a declaration. Treat
5755 ;; it as a declaration if "a" has been used as a type
5756 ;; somewhere else (if it's a known type we won't get here).
5757 (throw 'at-decl-or-cast t)))
5761 (and (eq context 'decl)
5762 (not c-recognize-paren-inits)
5763 (or got-parens got-suffix))))
5764 ;; Got a type followed by an abstract declarator. If `got-prefix'
5765 ;; is set it's something like "a *" without anything after it. If
5766 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
5767 ;; or similar, which we accept only if the context rules out
5769 (throw 'at-decl-or-cast t)))
5771 ;; If we had a complete symbol table here (which rules out
5772 ;; `c-found-types') we should return t due to the disambiguation rule
5773 ;; (in at least C++) that anything that can be parsed as a declaration
5774 ;; is a declaration. Now we're being more defensive and prefer to
5775 ;; highlight things like "foo (bar);" as a declaration only if we're
5776 ;; inside an arglist that contains declarations.
5777 (eq context 'decl))))
5779 ;; The point is now after the type decl expression.
5782 ;; Check for a cast.
5787 ;; Should be the first type/identifier in a cast paren.
5788 (> preceding-token-end (point-min))
5789 (memq (char-before preceding-token-end) c-cast-parens)
5791 ;; The closing paren should follow.
5793 (c-forward-syntactic-ws)
5794 (looking-at "\\s\)"))
5796 ;; There should be a primary expression after it.
5799 (c-forward-syntactic-ws)
5800 (setq cast-end (point))
5801 (and (looking-at c-primary-expr-regexp)
5803 (setq pos (match-end 0))
5805 ;; Check if the expression begins with a prefix keyword.
5807 (if (match-beginning 1)
5808 ;; Expression begins with an ambiguous operator. Treat
5809 ;; it as a cast if it's a type decl or if we've
5810 ;; recognized the type somewhere else.
5812 (memq at-type '(t known found)))
5813 ;; Unless it's a keyword, it's the beginning of a primary
5815 (not (looking-at c-keywords-regexp)))))
5816 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
5817 ;; that it matched a whole one so that we don't e.g. confuse
5818 ;; the operator '-' with '->'. It's ok if it matches further,
5819 ;; though, since it e.g. can match the float '.5' while the
5820 ;; operator regexp only matches '.'.
5821 (or (not (looking-at c-nonsymbol-token-regexp))
5822 (<= (match-end 0) pos))))
5824 ;; There should either be a cast before it or something that isn't an
5825 ;; identifier or close paren.
5826 (> preceding-token-end (point-min))
5828 (goto-char (1- preceding-token-end))
5829 (or (eq (point) last-cast-end)
5831 (c-backward-syntactic-ws)
5832 (if (< (skip-syntax-backward "w_") 0)
5833 ;; It's a symbol. Accept it only if it's one of the
5834 ;; keywords that can precede an expression (without
5835 ;; surrounding parens).
5836 (looking-at c-simple-stmt-key)
5838 ;; Check that it isn't a close paren (block close is ok,
5840 (not (memq (char-before) '(?\) ?\])))
5841 ;; Check that it isn't a nonsymbol identifier.
5842 (not (c-on-identifier)))))))))
5845 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5846 (let ((c-promote-possible-types t))
5847 (goto-char type-start)
5850 (goto-char cast-end)
5854 ;; We're at a declaration. Highlight the type and the following
5857 (when backup-if-not-cast
5858 (c-fdoc-shift-type-backward t))
5860 (when (and (eq context 'decl) (looking-at ","))
5861 ;; Make sure to propagate the `c-decl-arg-start' property to
5862 ;; the next argument if it's set in this one, to cope with
5863 ;; interactive refontification.
5864 (c-put-c-type-property (point) 'c-decl-arg-start))
5866 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5867 (let ((c-promote-possible-types t))
5869 (goto-char type-start)
5872 (cons id-start at-type-decl))
5875 ;; False alarm. Restore the recorded ranges.
5876 (setq c-record-type-identifiers save-rec-type-ids
5877 c-record-ref-identifiers save-rec-ref-ids)
5880 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
5881 ;; Assuming that point is at the beginning of a token, check if it starts a
5882 ;; label and if so move over it and return non-nil (t in default situations,
5883 ;; specific symbols (see below) for interesting situations), otherwise don't
5884 ;; move and return nil. "Label" here means "most things with a colon".
5886 ;; More precisely, a "label" is regarded as one of:
5887 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
5888 ;; (ii) A case label - either the entire construct "case FOO:", or just the
5889 ;; bare "case", should the colon be missing. We return t;
5890 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
5892 ;; (iv) One of QT's "extended" C++ variants of
5893 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
5894 ;; Returns the symbol `qt-2kwds-colon'.
5895 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
5896 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
5897 ;; colon). Currently (2006-03), this applies only to Objective C's
5898 ;; keywords "@private", "@protected", and "@public". Returns t.
5900 ;; One of the things which will NOT be recognised as a label is a bit-field
5901 ;; element of a struct, something like "int foo:5".
5903 ;; The end of the label is taken to be just after the colon, or the end of
5904 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
5905 ;; after the end on return. The terminating char gets marked with
5906 ;; `c-decl-end' to improve recognition of the following declaration or
5909 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
5910 ;; label, if any, has already been marked up like that.
5912 ;; If PRECEDING-TOKEN-END is given, it should be the first position
5913 ;; after the preceding token, i.e. on the other side of the
5914 ;; syntactic ws from the point. Use a value less than or equal to
5915 ;; (point-min) if the point is at the first token in (the visible
5916 ;; part of) the buffer.
5918 ;; The optional LIMIT limits the forward scan for the colon.
5920 ;; This function records the ranges of the label symbols on
5921 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
5924 ;; This function might do hidden buffer changes.
5926 (let ((start (point))
5929 macro-start ; if we're in one.
5932 ;; "case" or "default" (Doesn't apply to AWK).
5933 ((looking-at c-label-kwds-regexp)
5934 (let ((kwd-end (match-end 1)))
5935 ;; Record only the keyword itself for fontification, since in
5936 ;; case labels the following is a constant expression and not
5938 (when c-record-type-identifiers
5939 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
5941 ;; Find the label end.
5944 (if (and (c-syntactic-re-search-forward
5945 ;; Stop on chars that aren't allowed in expressions,
5946 ;; and on operator chars that would be meaningless
5947 ;; there. FIXME: This doesn't cope with ?: operators.
5948 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
5950 (match-beginning 2))
5952 (progn ; there's a proper :
5953 (goto-char (match-beginning 2)) ; just after the :
5954 (c-put-c-type-property (1- (point)) 'c-decl-end)
5957 ;; It's an unfinished label. We consider the keyword enough
5958 ;; to recognize it as a label, so that it gets fontified.
5959 ;; Leave the point at the end of it, but don't put any
5960 ;; `c-decl-end' marker.
5964 ;; @private, @protected, @public, in Objective C, or similar.
5965 ((and c-opt-extra-label-key
5966 (looking-at c-opt-extra-label-key))
5967 ;; For a `c-opt-extra-label-key' match, we record the whole
5968 ;; thing for fontification. That's to get the leading '@' in
5969 ;; Objective-C protection labels fontified.
5970 (goto-char (match-end 1))
5971 (when c-record-type-identifiers
5972 (c-record-ref-id (cons (match-beginning 1) (point))))
5973 (c-put-c-type-property (1- (point)) 'c-decl-end)
5974 (setq label-type t))
5976 ;; All other cases of labels.
5977 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
5979 ;; A colon label must have something before the colon.
5980 (not (eq (char-after) ?:))
5982 ;; Check that we're not after a token that can't precede a label.
5984 ;; Trivially succeeds when there's no preceding token.
5985 (if preceding-token-end
5986 (<= preceding-token-end (point-min))
5988 (c-backward-syntactic-ws)
5989 (setq preceding-token-end (point))
5992 ;; Check if we're after a label, if we're after a closing
5993 ;; paren that belong to statement, and with
5994 ;; `c-label-prefix-re'. It's done in different order
5995 ;; depending on `assume-markup' since the checks have
5996 ;; different expensiveness.
5999 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
6003 (goto-char (1- preceding-token-end))
6004 (c-beginning-of-current-token)
6005 (or (looking-at c-label-prefix-re)
6006 (looking-at c-block-stmt-1-key)))
6008 (and (eq (char-before preceding-token-end) ?\))
6009 (c-after-conditional)))
6013 (goto-char (1- preceding-token-end))
6014 (c-beginning-of-current-token)
6015 (or (looking-at c-label-prefix-re)
6016 (looking-at c-block-stmt-1-key)))
6019 ((eq (char-before preceding-token-end) ?\))
6020 (c-after-conditional))
6022 ((eq (char-before preceding-token-end) ?:)
6023 ;; Might be after another label, so check it recursively.
6026 (goto-char (1- preceding-token-end))
6027 ;; Essentially the same as the
6028 ;; `c-syntactic-re-search-forward' regexp below.
6030 (save-excursion (and (c-beginning-of-macro)
6032 (if macro-start (narrow-to-region macro-start (point-max)))
6033 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
6034 ;; Note: the following should work instead of the
6035 ;; narrow-to-region above. Investigate why not,
6036 ;; sometime. ACM, 2006-03-31.
6037 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
6040 ;; If the caller turned on recording for us,
6041 ;; it shouldn't apply when we check the
6043 c-record-type-identifiers)
6044 ;; A label can't start at a cpp directive. Check for
6045 ;; this, since c-forward-syntactic-ws would foul up on it.
6046 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
6047 (c-forward-syntactic-ws)
6048 (c-forward-label nil pte start))))))))))
6050 ;; Point is still at the beginning of the possible label construct.
6052 ;; Check that the next nonsymbol token is ":", or that we're in one
6053 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
6054 ;; arguments. FIXME: Should build this regexp from the language
6057 ;; public: protected: private:
6059 (c-major-mode-is 'c++-mode)
6060 (search-forward-regexp
6061 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
6062 (progn (backward-char)
6063 (c-forward-syntactic-ws limit)
6064 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
6066 (setq label-type t))
6067 ;; QT double keyword like "protected slots:" or goto target.
6068 ((progn (goto-char start) nil))
6069 ((when (c-syntactic-re-search-forward
6070 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
6072 (setq label-end (point))
6074 (and (c-major-mode-is 'c++-mode)
6076 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
6077 (buffer-substring start (point)))))
6078 (c-forward-syntactic-ws limit)
6080 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
6083 (if (string= "signals" ; Special QT macro
6084 (buffer-substring-no-properties start label-end))
6088 (search-forward-regexp "\\=slots\\>" limit t)
6089 (progn (c-forward-syntactic-ws limit)
6090 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
6092 (setq label-type 'qt-2kwds-colon)))))))
6095 (narrow-to-region start (point))
6097 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
6101 (when (looking-at c-nonlabel-token-key)
6103 (setq label-type nil)
6104 (throw 'check-label nil))
6105 (and (c-safe (c-forward-sexp)
6106 (c-forward-syntactic-ws)
6110 ;; Record the identifiers in the label for fontification, unless
6111 ;; it begins with `c-label-kwds' in which case the following
6112 ;; identifiers are part of a (constant) expression that
6113 ;; shouldn't be fontified.
6114 (when (and c-record-type-identifiers
6115 (progn (goto-char start)
6116 (not (looking-at c-label-kwds-regexp))))
6117 (while (c-syntactic-re-search-forward c-symbol-key nil t)
6118 (c-record-ref-id (cons (match-beginning 0)
6121 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
6122 (goto-char (point-max)))))
6129 (defun c-forward-objc-directive ()
6130 ;; Assuming the point is at the beginning of a token, try to move
6131 ;; forward to the end of the Objective-C directive that starts
6132 ;; there. Return t if a directive was fully recognized, otherwise
6133 ;; the point is moved as far as one could be successfully parsed and
6136 ;; This function records identifier ranges on
6137 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6138 ;; `c-record-type-identifiers' is non-nil.
6140 ;; This function might do hidden buffer changes.
6142 (let ((start (point))
6144 (c-promote-possible-types t)
6145 ;; Turn off recognition of angle bracket arglists while parsing
6146 ;; types here since the protocol reference list might then be
6147 ;; considered part of the preceding name or superclass-name.
6148 c-recognize-<>-arglists)
6153 (c-make-keywords-re t
6154 (append (c-lang-const c-protection-kwds objc)
6157 (goto-char (match-end 1))
6163 (c-make-keywords-re t
6164 '("@interface" "@implementation" "@protocol")
6167 ;; Handle the name of the class itself.
6169 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
6171 (goto-char (match-end 0))
6176 ;; Look for ": superclass-name" or "( category-name )".
6177 (when (looking-at "[:\(]")
6178 (setq start-char (char-after))
6180 (c-forward-syntactic-ws)
6181 (unless (c-forward-type) (throw 'break nil))
6182 (when (eq start-char ?\()
6183 (unless (eq (char-after) ?\)) (throw 'break nil))
6185 (c-forward-syntactic-ws)))
6187 ;; Look for a protocol reference list.
6188 (if (eq (char-after) ?<)
6189 (let ((c-recognize-<>-arglists t)
6190 (c-parse-and-markup-<>-arglists t)
6191 c-restricted-<>-arglists)
6192 (c-forward-<>-arglist t))
6196 (c-backward-syntactic-ws)
6197 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
6198 (c-put-c-type-property (1- (point)) 'c-decl-end)
6201 (c-clear-c-type-property start (point) 'c-decl-end)
6204 (defun c-beginning-of-inheritance-list (&optional lim)
6205 ;; Go to the first non-whitespace after the colon that starts a
6206 ;; multiple inheritance introduction. Optional LIM is the farthest
6207 ;; back we should search.
6209 ;; This function might do hidden buffer changes.
6210 (c-with-syntax-table c++-template-syntax-table
6211 (c-backward-token-2 0 t lim)
6212 (while (and (or (looking-at c-symbol-start)
6213 (looking-at "[<,]\\|::"))
6214 (zerop (c-backward-token-2 1 t lim))))))
6216 (defun c-in-method-def-p ()
6217 ;; Return nil if we aren't in a method definition, otherwise the
6218 ;; position of the initial [+-].
6220 ;; This function might do hidden buffer changes.
6223 (and c-opt-method-key
6224 (looking-at c-opt-method-key)
6228 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
6229 (defun c-in-gcc-asm-p ()
6230 ;; Return non-nil if point is within a gcc \"asm\" block.
6232 ;; This should be called with point inside an argument list.
6234 ;; Only one level of enclosing parentheses is considered, so for
6235 ;; instance `nil' is returned when in a function call within an asm
6238 ;; This function might do hidden buffer changes.
6240 (and c-opt-asm-stmt-key
6243 (backward-up-list 1)
6244 (c-beginning-of-statement-1 (point-min) nil t)
6245 (looking-at c-opt-asm-stmt-key))))
6247 (defun c-at-toplevel-p ()
6248 "Return a determination as to whether point is at the `top-level'.
6249 Being at the top-level means that point is either outside any
6250 enclosing block (such function definition), or only inside a class,
6251 namespace or other block that contains another declaration level.
6253 If point is not at the top-level (e.g. it is inside a method
6254 definition), then nil is returned. Otherwise, if point is at a
6255 top-level not enclosed within a class definition, t is returned.
6256 Otherwise, a 2-vector is returned where the zeroth element is the
6257 buffer position of the start of the class declaration, and the first
6258 element is the buffer position of the enclosing class's opening
6261 Note that this function might do hidden buffer changes. See the
6262 comment at the start of cc-engine.el for more info."
6263 (let ((paren-state (c-parse-state)))
6264 (or (not (c-most-enclosing-brace paren-state))
6265 (c-search-uplist-for-classkey paren-state))))
6267 (defun c-just-after-func-arglist-p (&optional lim)
6268 ;; Return non-nil if the point is in the region after the argument
6269 ;; list of a function and its opening brace (or semicolon in case it
6270 ;; got no body). If there are K&R style argument declarations in
6271 ;; that region, the point has to be inside the first one for this
6272 ;; function to recognize it.
6274 ;; If successful, the point is moved to the first token after the
6275 ;; function header (see `c-forward-decl-or-cast-1' for details) and
6276 ;; the position of the opening paren of the function arglist is
6279 ;; The point is clobbered if not successful.
6281 ;; LIM is used as bound for backward buffer searches.
6283 ;; This function might do hidden buffer changes.
6285 (let ((beg (point)) end id-start)
6287 (eq (c-beginning-of-statement-1 lim) 'same)
6289 (not (or (c-major-mode-is 'objc-mode)
6290 (c-forward-objc-directive)))
6293 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
6296 ;; There should not be a '=' or ',' between beg and the
6297 ;; start of the declaration since that means we were in the
6298 ;; "expression part" of the declaration.
6300 (not (looking-at "[=,]")))
6303 ;; Check that there's an arglist paren in the
6305 (goto-char id-start)
6306 (cond ((eq (char-after) ?\()
6307 ;; The declarator is a paren expression, so skip past it
6308 ;; so that we don't get stuck on that instead of the
6309 ;; function arglist.
6311 ((and c-opt-op-identifier-prefix
6312 (looking-at c-opt-op-identifier-prefix))
6313 ;; Don't trip up on "operator ()".
6314 (c-forward-token-2 2 t)))
6315 (and (< (point) beg)
6316 (c-syntactic-re-search-forward "(" beg t t)
6319 (defun c-in-knr-argdecl (&optional lim)
6320 ;; Return the position of the first argument declaration if point is
6321 ;; inside a K&R style argument declaration list, nil otherwise.
6322 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
6323 ;; position that bounds the backward search for the argument list.
6325 ;; Point must be within a possible K&R region, e.g. just before a top-level
6326 ;; "{". It must be outside of parens and brackets. The test can return
6327 ;; false positives otherwise.
6329 ;; This function might do hidden buffer changes.
6333 ;; If we're in a macro, our search range is restricted to it. Narrow to
6334 ;; the searchable range.
6335 (let* ((macro-start (c-query-macro-start))
6336 (lim (max (or lim (point-min)) (or macro-start (point-min))))
6337 before-lparen after-rparen
6338 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
6339 (narrow-to-region lim (c-point 'eol))
6341 ;; Search backwards for the defun's argument list. We give up if we
6342 ;; encounter a "}" (end of a previous defun) or BOB.
6344 ;; The criterion for a paren structure being the arg list is:
6345 ;; o - there is non-WS stuff after it but before any "{"; AND
6346 ;; o - the token after it isn't a ";" AND
6347 ;; o - it is preceded by either an identifier (the function name) or
6348 ;; a macro expansion like "DEFUN (...)"; AND
6349 ;; o - its content is a non-empty comma-separated list of identifiers
6350 ;; (an empty arg list won't have a knr region).
6352 ;; The following snippet illustrates these rules:
6353 ;; int foo (bar, baz, yuk)
6355 ;; int (*baz) (my_type) ;
6356 ;; int (*) (void) (*yuk) (void) ;
6360 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
6361 (setq pp-count-out (1- pp-count-out))
6362 (c-syntactic-skip-backward "^)]}")
6363 (cond ((eq (char-before) ?\))
6364 (setq after-rparen (point)))
6365 ((eq (char-before) ?\])
6366 (setq after-rparen nil))
6367 (t ; either } (hit previous defun) or no more parens/brackets
6371 ;; We're inside a paren. Could it be our argument list....?
6375 (goto-char after-rparen)
6376 (unless (c-go-list-backward) (throw 'knr nil)) ;
6377 ;; FIXME!!! What about macros between the parens? 2007/01/20
6378 (setq before-lparen (point)))
6380 ;; It can't be the arg list if next token is ; or {
6381 (progn (goto-char after-rparen)
6382 (c-forward-syntactic-ws)
6383 (not (memq (char-after) '(?\; ?\{))))
6385 ;; Is the thing preceding the list an identifier (the
6386 ;; function name), or a macro expansion?
6388 (goto-char before-lparen)
6389 (eq (c-backward-token-2) 0)
6390 (or (c-on-identifier)
6391 (and (eq (char-after) ?\))
6392 (c-go-up-list-backward)
6393 (eq (c-backward-token-2) 0)
6394 (c-on-identifier))))
6396 ;; Have we got a non-empty list of comma-separated
6399 (goto-char before-lparen)
6400 (c-forward-token-2) ; to first token inside parens
6405 (while (eq (char-after) ?\,)
6407 (unless (c-on-identifier) (throw 'id-list nil))
6408 (c-forward-token-2))
6409 (eq (char-after) ?\))))))
6411 ;; ...Yes. We've identified the function's argument list.
6413 (progn (goto-char after-rparen)
6414 (c-forward-syntactic-ws)
6417 ;; ...No. The current parens aren't the function's arg list.
6418 (goto-char before-lparen))
6420 (or (c-go-list-backward) ; backwards over [ .... ]
6421 (throw 'knr nil)))))))))
6423 (defun c-skip-conditional ()
6424 ;; skip forward over conditional at point, including any predicate
6425 ;; statements in parentheses. No error checking is performed.
6427 ;; This function might do hidden buffer changes.
6428 (c-forward-sexp (cond
6430 ((looking-at (concat "\\<else"
6431 "\\([ \t\n]\\|\\\\\n\\)+"
6432 "if\\>\\([^_]\\|$\\)"))
6434 ;; do, else, try, finally
6435 ((looking-at (concat "\\<\\("
6436 "do\\|else\\|try\\|finally"
6437 "\\)\\>\\([^_]\\|$\\)"))
6439 ;; for, if, while, switch, catch, synchronized, foreach
6442 (defun c-after-conditional (&optional lim)
6443 ;; If looking at the token after a conditional then return the
6444 ;; position of its start, otherwise return nil.
6446 ;; This function might do hidden buffer changes.
6448 (and (zerop (c-backward-token-2 1 t lim))
6449 (or (looking-at c-block-stmt-1-key)
6450 (and (eq (char-after) ?\()
6451 (zerop (c-backward-token-2 1 t lim))
6452 (looking-at c-block-stmt-2-key)))
6455 (defun c-after-special-operator-id (&optional lim)
6456 ;; If the point is after an operator identifier that isn't handled
6457 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
6458 ;; position of the start of that identifier is returned. nil is
6459 ;; returned otherwise. The point may be anywhere in the syntactic
6460 ;; whitespace after the last token of the operator identifier.
6462 ;; This function might do hidden buffer changes.
6464 (and c-overloadable-operators-regexp
6465 (zerop (c-backward-token-2 1 nil lim))
6466 (looking-at c-overloadable-operators-regexp)
6467 (or (not c-opt-op-identifier-prefix)
6469 (zerop (c-backward-token-2 1 nil lim))
6470 (looking-at c-opt-op-identifier-prefix)))
6473 (defsubst c-backward-to-block-anchor (&optional lim)
6474 ;; Assuming point is at a brace that opens a statement block of some
6475 ;; kind, move to the proper anchor point for that block. It might
6476 ;; need to be adjusted further by c-add-stmt-syntax, but the
6477 ;; position at return is suitable as start position for that
6480 ;; This function might do hidden buffer changes.
6481 (unless (= (point) (c-point 'boi))
6482 (let ((start (c-after-conditional lim)))
6484 (goto-char start)))))
6486 (defsubst c-backward-to-decl-anchor (&optional lim)
6487 ;; Assuming point is at a brace that opens the block of a top level
6488 ;; declaration of some kind, move to the proper anchor point for
6491 ;; This function might do hidden buffer changes.
6492 (unless (= (point) (c-point 'boi))
6493 (c-beginning-of-statement-1 lim)))
6495 (defun c-search-decl-header-end ()
6496 ;; Search forward for the end of the "header" of the current
6497 ;; declaration. That's the position where the definition body
6498 ;; starts, or the first variable initializer, or the ending
6499 ;; semicolon. I.e. search forward for the closest following
6500 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
6501 ;; _after_ the first found token, or at point-max if none is found.
6503 ;; This function might do hidden buffer changes.
6505 (let ((base (point)))
6506 (if (c-major-mode-is 'c++-mode)
6508 ;; In C++ we need to take special care to handle operator
6509 ;; tokens and those pesky template brackets.
6511 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
6513 (c-end-of-current-token base)
6514 ;; Handle operator identifiers, i.e. ignore any
6515 ;; operator token preceded by "operator".
6517 (and (c-safe (c-backward-sexp) t)
6518 (looking-at c-opt-op-identifier-prefix)))
6519 (and (eq (char-before) ?<)
6520 (c-with-syntax-table c++-template-syntax-table
6521 (if (c-safe (goto-char (c-up-list-forward (point))))
6523 (goto-char (point-max))
6525 (setq base (point)))
6528 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
6529 (c-end-of-current-token base))
6530 (setq base (point))))))
6532 (defun c-beginning-of-decl-1 (&optional lim)
6533 ;; Go to the beginning of the current declaration, or the beginning
6534 ;; of the previous one if already at the start of it. Point won't
6535 ;; be moved out of any surrounding paren. Return a cons cell of the
6536 ;; form (MOVE . KNR-POS). MOVE is like the return value from
6537 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
6538 ;; style argument declarations (and they are to be recognized) then
6539 ;; KNR-POS is set to the start of the first such argument
6540 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
6541 ;; position that bounds the backward search.
6543 ;; NB: Cases where the declaration continues after the block, as in
6544 ;; "struct foo { ... } bar;", are currently recognized as two
6545 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
6547 ;; This function might do hidden buffer changes.
6549 (let* ((start (point))
6550 (last-stmt-start (point))
6551 (move (c-beginning-of-statement-1 lim nil t)))
6553 ;; `c-beginning-of-statement-1' stops at a block start, but we
6554 ;; want to continue if the block doesn't begin a top level
6555 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
6556 ;; or an open paren.
6557 (let ((beg (point)) tentative-move)
6558 ;; Go back one "statement" each time round the loop until we're just
6559 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
6560 ;; an ObjC method. This will move over a multiple declaration whose
6561 ;; components are comma separated.
6563 ;; Must check with c-opt-method-key in ObjC mode.
6564 (not (and c-opt-method-key
6565 (looking-at c-opt-method-key)))
6566 (/= last-stmt-start (point))
6568 (c-backward-syntactic-ws lim)
6569 (not (memq (char-before) '(?\; ?} ?: nil))))
6572 (not (looking-at "\\s(")))
6573 ;; Check that we don't move from the first thing in a
6574 ;; macro to its header.
6575 (not (eq (setq tentative-move
6576 (c-beginning-of-statement-1 lim nil t))
6578 (setq last-stmt-start beg
6580 move tentative-move))
6583 (when c-recognize-knr-p
6584 (let ((fallback-pos (point)) knr-argdecl-start)
6585 ;; Handle K&R argdecls. Back up after the "statement" jumped
6586 ;; over by `c-beginning-of-statement-1', unless it was the
6587 ;; function body, in which case we're sitting on the opening
6588 ;; brace now. Then test if we're in a K&R argdecl region and
6589 ;; that we started at the other side of the first argdecl in
6591 (unless (eq (char-after) ?{)
6592 (goto-char last-stmt-start))
6593 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
6594 (< knr-argdecl-start start)
6596 (goto-char knr-argdecl-start)
6597 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
6599 (cons (if (eq (char-after fallback-pos) ?{)
6603 (goto-char fallback-pos))))
6605 ;; `c-beginning-of-statement-1' counts each brace block as a separate
6606 ;; statement, so the result will be 'previous if we've moved over any.
6607 ;; So change our result back to 'same if necessary.
6609 ;; If they were brace list initializers we might not have moved over a
6610 ;; declaration boundary though, so change it to 'same if we've moved
6611 ;; past a '=' before '{', but not ';'. (This ought to be integrated
6612 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
6613 ;; potentially can search over a large amount of text.). Take special
6614 ;; pains not to get mislead by C++'s "operator=", and the like.
6615 (if (and (eq move 'previous)
6616 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
6617 c++-template-syntax-table
6622 (while ; keep going back to "[;={"s until we either find
6623 ; no more, or get to one which isn't an "operator ="
6624 (and (c-syntactic-re-search-forward "[;={]" start t t t)
6625 (eq (char-before) ?=)
6626 c-overloadable-operators-regexp
6627 c-opt-op-identifier-prefix
6629 (eq (c-backward-token-2) 0)
6630 (looking-at c-overloadable-operators-regexp)
6631 (eq (c-backward-token-2) 0)
6632 (looking-at c-opt-op-identifier-prefix))))
6633 (eq (char-before) ?=))
6634 (c-syntactic-re-search-forward "[;{]" start t t)
6635 (eq (char-before) ?{)
6636 (c-safe (goto-char (c-up-list-forward (point))) t)
6637 (not (c-syntactic-re-search-forward ";" start t t))))))
6641 (defun c-end-of-decl-1 ()
6642 ;; Assuming point is at the start of a declaration (as detected by
6643 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
6644 ;; `c-beginning-of-decl-1', this function handles the case when a
6645 ;; block is followed by identifiers in e.g. struct declarations in C
6646 ;; or C++. If a proper end was found then t is returned, otherwise
6647 ;; point is moved as far as possible within the current sexp and nil
6648 ;; is returned. This function doesn't handle macros; use
6649 ;; `c-end-of-macro' instead in those cases.
6651 ;; This function might do hidden buffer changes.
6652 (let ((start (point))
6653 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
6654 c++-template-syntax-table
6657 (c-search-decl-header-end)
6659 (when (and c-recognize-knr-p
6660 (eq (char-before) ?\;)
6661 (c-in-knr-argdecl start))
6662 ;; Stopped at the ';' in a K&R argdecl section which is
6663 ;; detected using the same criteria as in
6664 ;; `c-beginning-of-decl-1'. Move to the following block
6666 (c-syntactic-re-search-forward "{" nil 'move t))
6668 (when (eq (char-before) ?{)
6669 ;; Encountered a block in the declaration. Jump over it.
6671 (goto-char (c-up-list-forward (point)))
6672 (error (goto-char (point-max))
6673 (throw 'return nil)))
6674 (if (or (not c-opt-block-decls-with-vars-key)
6676 (c-with-syntax-table decl-syntax-table
6677 (let ((lim (point)))
6680 ;; Check for `c-opt-block-decls-with-vars-key'
6681 ;; before the first paren.
6682 (c-syntactic-re-search-forward
6683 (concat "[;=\(\[{]\\|\\("
6684 c-opt-block-decls-with-vars-key
6688 (not (eq (char-before) ?_))
6689 ;; Check that the first following paren is
6691 (c-syntactic-re-search-forward "[;=\(\[{]"
6693 (eq (char-before) ?{)))))))
6694 ;; The declaration doesn't have any of the
6695 ;; `c-opt-block-decls-with-vars' keywords in the
6696 ;; beginning, so it ends here at the end of the block.
6699 (c-with-syntax-table decl-syntax-table
6701 (if (eq (char-before) ?\;)
6703 (c-syntactic-re-search-forward ";" nil 'move t))))
6706 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
6707 ;; Assuming the point is at an open brace, check if it starts a
6708 ;; block that contains another declaration level, i.e. that isn't a
6709 ;; statement block or a brace list, and if so return non-nil.
6711 ;; If the check is successful, the return value is the start of the
6712 ;; keyword that tells what kind of construct it is, i.e. typically
6713 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
6714 ;; the point will be at the start of the construct, before any
6715 ;; leading specifiers, otherwise it's at the returned position.
6717 ;; The point is clobbered if the check is unsuccessful.
6719 ;; CONTAINING-SEXP is the position of the open of the surrounding
6720 ;; paren, or nil if none.
6722 ;; The optional LIMIT limits the backward search for the start of
6723 ;; the construct. It's assumed to be at a syntactically relevant
6726 ;; If any template arglists are found in the searched region before
6727 ;; the open brace, they get marked with paren syntax.
6729 ;; This function might do hidden buffer changes.
6731 (let ((open-brace (point)) kwd-start first-specifier-pos)
6732 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6734 (when (and c-recognize-<>-arglists
6735 (eq (char-before) ?>))
6736 ;; Could be at the end of a template arglist.
6737 (let ((c-parse-and-markup-<>-arglists t)
6738 (c-disallow-comma-in-<>-arglists
6739 (and containing-sexp
6740 (not (eq (char-after containing-sexp) ?{)))))
6742 (c-backward-<>-arglist nil limit)
6744 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6745 (eq (char-before) ?>))))))
6747 ;; Note: Can't get bogus hits inside template arglists below since they
6748 ;; have gotten paren syntax above.
6750 ;; If `goto-start' is set we begin by searching for the
6751 ;; first possible position of a leading specifier list.
6752 ;; The `c-decl-block-key' search continues from there since
6753 ;; we know it can't match earlier.
6755 (when (c-syntactic-re-search-forward c-symbol-start
6757 (goto-char (setq first-specifier-pos (match-beginning 0)))
6762 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
6763 (goto-char (setq kwd-start (match-beginning 0)))
6766 ;; Found a keyword that can't be a type?
6769 ;; Can be a type too, in which case it's the return type of a
6770 ;; function (under the assumption that no declaration level
6771 ;; block construct starts with a type).
6772 (not (c-forward-type))
6774 ;; Jumped over a type, but it could be a declaration keyword
6775 ;; followed by the declared identifier that we've jumped over
6776 ;; instead (e.g. in "class Foo {"). If it indeed is a type
6777 ;; then we should be at the declarator now, so check for a
6778 ;; valid declarator start.
6780 ;; Note: This doesn't cope with the case when a declared
6781 ;; identifier is followed by e.g. '(' in a language where '('
6782 ;; also might be part of a declarator expression. Currently
6783 ;; there's no such language.
6784 (not (or (looking-at c-symbol-start)
6785 (looking-at c-type-decl-prefix-key)))))
6787 ;; In Pike a list of modifiers may be followed by a brace
6788 ;; to make them apply to many identifiers. Note that the
6789 ;; match data will be empty on return in this case.
6790 ((and (c-major-mode-is 'pike-mode)
6792 (goto-char open-brace)
6793 (= (c-backward-token-2) 0))
6794 (looking-at c-specifier-key)
6795 ;; Use this variant to avoid yet another special regexp.
6796 (c-keyword-member (c-keyword-sym (match-string 1))
6798 (setq kwd-start (point))
6804 ;; Back up over any preceding specifiers and their clauses
6805 ;; by going forward from `first-specifier-pos', which is the
6806 ;; earliest possible position where the specifier list can
6809 (goto-char first-specifier-pos)
6811 (while (< (point) kwd-start)
6812 (if (looking-at c-symbol-key)
6813 ;; Accept any plain symbol token on the ground that
6814 ;; it's a specifier masked through a macro (just
6815 ;; like `c-forward-decl-or-cast-1' skip forward over
6818 ;; Could be more restrictive wrt invalid keywords,
6819 ;; but that'd only occur in invalid code so there's
6820 ;; no use spending effort on it.
6821 (let ((end (match-end 0)))
6822 (unless (c-forward-keyword-clause 0)
6824 (c-forward-syntactic-ws)))
6826 ;; Can't parse a declaration preamble and is still
6827 ;; before `kwd-start'. That means `first-specifier-pos'
6828 ;; was in some earlier construct. Search again.
6829 (if (c-syntactic-re-search-forward c-symbol-start
6831 (goto-char (setq first-specifier-pos (match-beginning 0)))
6832 ;; Got no preamble before the block declaration keyword.
6833 (setq first-specifier-pos kwd-start))))
6835 (goto-char first-specifier-pos))
6836 (goto-char kwd-start))
6840 (defun c-search-uplist-for-classkey (paren-state)
6841 ;; Check if the closest containing paren sexp is a declaration
6842 ;; block, returning a 2 element vector in that case. Aref 0
6843 ;; contains the bufpos at boi of the class key line, and aref 1
6844 ;; contains the bufpos of the open brace. This function is an
6845 ;; obsolete wrapper for `c-looking-at-decl-block'.
6847 ;; This function might do hidden buffer changes.
6848 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
6849 (when open-paren-pos
6851 (goto-char open-paren-pos)
6852 (when (and (eq (char-after) ?{)
6853 (c-looking-at-decl-block
6854 (c-safe-position open-paren-pos paren-state)
6856 (back-to-indentation)
6857 (vector (point) open-paren-pos))))))
6859 (defun c-inside-bracelist-p (containing-sexp paren-state)
6860 ;; return the buffer position of the beginning of the brace list
6861 ;; statement if we're inside a brace list, otherwise return nil.
6862 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
6863 ;; paren. PAREN-STATE is the remainder of the state of enclosing
6866 ;; N.B.: This algorithm can potentially get confused by cpp macros
6867 ;; placed in inconvenient locations. It's a trade-off we make for
6870 ;; This function might do hidden buffer changes.
6872 ;; This will pick up brace list declarations.
6875 (goto-char containing-sexp)
6878 (if (and (or (looking-at c-brace-list-key)
6879 (progn (c-forward-sexp -1)
6880 (looking-at c-brace-list-key)))
6881 (setq bracepos (c-down-list-forward (point)))
6882 (not (c-crosses-statement-barrier-p (point)
6885 ;; this will pick up array/aggregate init lists, even if they are nested.
6888 ;; Pike can have class definitions anywhere, so we must
6889 ;; check for the class key here.
6890 (and (c-major-mode-is 'pike-mode)
6892 bufpos braceassignp lim next-containing)
6893 (while (and (not bufpos)
6896 (if (consp (car paren-state))
6897 (setq lim (cdr (car paren-state))
6898 paren-state (cdr paren-state))
6899 (setq lim (car paren-state)))
6901 (setq next-containing (car paren-state)
6902 paren-state (cdr paren-state))))
6903 (goto-char containing-sexp)
6904 (if (c-looking-at-inexpr-block next-containing next-containing)
6905 ;; We're in an in-expression block of some kind. Do not
6906 ;; check nesting. We deliberately set the limit to the
6907 ;; containing sexp, so that c-looking-at-inexpr-block
6908 ;; doesn't check for an identifier before it.
6909 (setq containing-sexp nil)
6910 ;; see if the open brace is preceded by = or [...] in
6911 ;; this statement, but watch out for operator=
6912 (setq braceassignp 'dontknow)
6913 (c-backward-token-2 1 t lim)
6914 ;; Checks to do only on the first sexp before the brace.
6915 (when (and c-opt-inexpr-brace-list-key
6916 (eq (char-after) ?\[))
6917 ;; In Java, an initialization brace list may follow
6918 ;; directly after "new Foo[]", so check for a "new"
6920 (while (eq braceassignp 'dontknow)
6922 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
6923 ((looking-at c-opt-inexpr-brace-list-key) t)
6924 ((looking-at "\\sw\\|\\s_\\|[.[]")
6925 ;; Carry on looking if this is an
6926 ;; identifier (may contain "." in Java)
6927 ;; or another "[]" sexp.
6930 ;; Checks to do on all sexps before the brace, up to the
6931 ;; beginning of the statement.
6932 (while (eq braceassignp 'dontknow)
6933 (cond ((eq (char-after) ?\;)
6934 (setq braceassignp nil))
6936 (looking-at class-key))
6937 (setq braceassignp nil))
6938 ((eq (char-after) ?=)
6939 ;; We've seen a =, but must check earlier tokens so
6940 ;; that it isn't something that should be ignored.
6941 (setq braceassignp 'maybe)
6942 (while (and (eq braceassignp 'maybe)
6943 (zerop (c-backward-token-2 1 t lim)))
6946 ;; Check for operator =
6947 ((and c-opt-op-identifier-prefix
6948 (looking-at c-opt-op-identifier-prefix))
6950 ;; Check for `<opchar>= in Pike.
6951 ((and (c-major-mode-is 'pike-mode)
6952 (or (eq (char-after) ?`)
6953 ;; Special case for Pikes
6954 ;; `[]=, since '[' is not in
6955 ;; the punctuation class.
6956 (and (eq (char-after) ?\[)
6957 (eq (char-before) ?`))))
6959 ((looking-at "\\s.") 'maybe)
6960 ;; make sure we're not in a C++ template
6961 ;; argument assignment
6963 (c-major-mode-is 'c++-mode)
6965 (let ((here (point))
6967 (skip-chars-backward "^<>")
6969 (and (eq (char-before) ?<)
6970 (not (c-crosses-statement-barrier-p
6972 (not (c-in-literal))
6976 (if (and (eq braceassignp 'dontknow)
6977 (/= (c-backward-token-2 1 t lim) 0))
6978 (setq braceassignp nil)))
6979 (if (not braceassignp)
6980 (if (eq (char-after) ?\;)
6981 ;; Brace lists can't contain a semicolon, so we're done.
6982 (setq containing-sexp nil)
6984 (setq containing-sexp next-containing
6986 next-containing nil))
6987 ;; we've hit the beginning of the aggregate list
6988 (c-beginning-of-statement-1
6989 (c-most-enclosing-brace paren-state))
6990 (setq bufpos (point))))
6995 (defun c-looking-at-special-brace-list (&optional lim)
6996 ;; If we're looking at the start of a pike-style list, ie `({Â })',
6997 ;; `([Â ])', `(<Â >)' etc, a cons of a cons of its starting and ending
6998 ;; positions and its entry in c-special-brace-lists is returned, nil
6999 ;; otherwise. The ending position is nil if the list is still open.
7000 ;; LIM is the limit for forward search. The point may either be at
7001 ;; the `(' or at the following paren character. Tries to check the
7002 ;; matching closer, but assumes it's correct if no balanced paren is
7003 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
7004 ;; a special brace list).
7006 ;; This function might do hidden buffer changes.
7007 (if c-special-brace-lists
7012 (c-forward-syntactic-ws)
7013 (if (eq (char-after) ?\()
7016 (c-forward-syntactic-ws)
7017 (setq inner-beg (point))
7018 (setq type (assq (char-after) c-special-brace-lists)))
7019 (if (setq type (assq (char-after) c-special-brace-lists))
7021 (setq inner-beg (point))
7022 (c-backward-syntactic-ws)
7024 (setq beg (if (eq (char-after) ?\()
7032 (= (char-before) ?\)))
7034 (goto-char inner-beg)
7035 (if (looking-at "\\s(")
7036 ;; Check balancing of the inner paren
7041 ;; If the inner char isn't a paren then
7042 ;; we can't check balancing, so just
7043 ;; check the char before the outer
7047 (c-backward-syntactic-ws)
7048 (= (char-before) (cdr type)))))
7049 (if (or (/= (char-syntax (char-before)) ?\))
7051 (c-forward-syntactic-ws)
7054 (cons (cons beg end) type))
7055 (cons (list beg) type)))))
7058 (defun c-looking-at-bos (&optional lim)
7059 ;; Return non-nil if between two statements or declarations, assuming
7060 ;; point is not inside a literal or comment.
7062 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
7063 ;; are recommended instead.
7065 ;; This function might do hidden buffer changes.
7066 (c-at-statement-start-p))
7067 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p)
7069 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
7070 ;; Return non-nil if we're looking at the beginning of a block
7071 ;; inside an expression. The value returned is actually a cons of
7072 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
7073 ;; position of the beginning of the construct.
7075 ;; LIM limits the backward search. CONTAINING-SEXP is the start
7076 ;; position of the closest containing list. If it's nil, the
7077 ;; containing paren isn't used to decide whether we're inside an
7078 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
7079 ;; needs to be farther back.
7081 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
7082 ;; brace block might be done. It should only be used when the
7083 ;; construct can be assumed to be complete, i.e. when the original
7084 ;; starting position was further down than that.
7086 ;; This function might do hidden buffer changes.
7089 (let ((res 'maybe) passed-paren
7090 (closest-lim (or containing-sexp lim (point-min)))
7091 ;; Look at the character after point only as a last resort
7092 ;; when we can't disambiguate.
7093 (block-follows (and (eq (char-after) ?{) (point))))
7095 (while (and (eq res 'maybe)
7096 (progn (c-backward-syntactic-ws)
7097 (> (point) closest-lim))
7099 (progn (backward-char)
7100 (looking-at "[\]\).]\\|\\w\\|\\s_"))
7101 (c-safe (forward-char)
7102 (goto-char (scan-sexps (point) -1))))
7105 (if (looking-at c-keywords-regexp)
7106 (let ((kw-sym (c-keyword-sym (match-string 1))))
7109 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
7110 (and (not (eq passed-paren ?\[))
7111 (or (not (looking-at c-class-key))
7112 ;; If the class definition is at the start of
7113 ;; a statement, we don't consider it an
7114 ;; in-expression class.
7115 (let ((prev (point)))
7117 (= (c-backward-token-2 1 nil closest-lim) 0)
7118 (eq (char-syntax (char-after)) ?w))
7119 (setq prev (point)))
7121 (not (c-at-statement-start-p)))
7122 ;; Also, in Pike we treat it as an
7123 ;; in-expression class if it's used in an
7124 ;; object clone expression.
7127 (c-major-mode-is 'pike-mode)
7128 (progn (goto-char block-follows)
7129 (zerop (c-forward-token-2 1 t)))
7130 (eq (char-after) ?\())))
7131 (cons 'inexpr-class (point))))
7132 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
7133 (when (not passed-paren)
7134 (cons 'inexpr-statement (point))))
7135 ((c-keyword-member kw-sym 'c-lambda-kwds)
7136 (when (or (not passed-paren)
7137 (eq passed-paren ?\())
7138 (cons 'inlambda (point))))
7139 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
7144 (if (looking-at "\\s(")
7146 (if (and (eq passed-paren ?\[)
7147 (eq (char-after) ?\[))
7148 ;; Accept several square bracket sexps for
7149 ;; Java array initializations.
7151 (setq passed-paren (char-after))
7156 (when (and c-recognize-paren-inexpr-blocks
7159 (eq (char-after containing-sexp) ?\())
7160 (goto-char containing-sexp)
7161 (if (or (save-excursion
7162 (c-backward-syntactic-ws lim)
7163 (and (> (point) (or lim (point-min)))
7165 (and c-special-brace-lists
7166 (c-looking-at-special-brace-list)))
7168 (cons 'inexpr-statement (point))))
7172 (defun c-looking-at-inexpr-block-backward (paren-state)
7173 ;; Returns non-nil if we're looking at the end of an in-expression
7174 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
7175 ;; PAREN-STATE is the paren state relevant at the current position.
7177 ;; This function might do hidden buffer changes.
7179 ;; We currently only recognize a block.
7180 (let ((here (point))
7181 (elem (car-safe paren-state))
7183 (when (and (consp elem)
7184 (progn (goto-char (cdr elem))
7185 (c-forward-syntactic-ws here)
7187 (goto-char (car elem))
7188 (if (setq paren-state (cdr paren-state))
7189 (setq containing-sexp (car-safe paren-state)))
7190 (c-looking-at-inexpr-block (c-safe-position containing-sexp
7192 containing-sexp)))))
7195 ;; `c-guess-basic-syntax' and the functions that precedes it below
7196 ;; implements the main decision tree for determining the syntactic
7197 ;; analysis of the current line of code.
7199 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
7200 ;; auto newline analysis.
7201 (defvar c-auto-newline-analysis nil)
7203 (defun c-brace-anchor-point (bracepos)
7204 ;; BRACEPOS is the position of a brace in a construct like "namespace
7205 ;; Bar {". Return the anchor point in this construct; this is the
7206 ;; earliest symbol on the brace's line which isn't earlier than
7209 ;; Currently (2007-08-17), "like namespace" means "matches
7210 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
7211 ;; or anything like that.
7213 (let ((boi (c-point 'boi bracepos)))
7214 (goto-char bracepos)
7215 (while (and (> (point) boi)
7216 (not (looking-at c-other-decl-block-key)))
7217 (c-backward-token-2))
7218 (if (> (point) boi) (point) boi))))
7220 (defsubst c-add-syntax (symbol &rest args)
7221 ;; A simple function to prepend a new syntax element to
7222 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
7223 ;; should always be dynamically bound but since we read it first
7224 ;; we'll fail properly anyway if this function is misused.
7225 (setq c-syntactic-context (cons (cons symbol args)
7226 c-syntactic-context)))
7228 (defsubst c-append-syntax (symbol &rest args)
7229 ;; Like `c-add-syntax' but appends to the end of the syntax list.
7230 ;; (Normally not necessary.)
7231 (setq c-syntactic-context (nconc c-syntactic-context
7232 (list (cons symbol args)))))
7234 (defun c-add-stmt-syntax (syntax-symbol
7239 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
7240 ;; needed with further syntax elements of the types `substatement',
7241 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
7242 ;; `defun-block-intro'.
7244 ;; Do the generic processing to anchor the given syntax symbol on
7245 ;; the preceding statement: Skip over any labels and containing
7246 ;; statements on the same line, and then search backward until we
7247 ;; find a statement or block start that begins at boi without a
7248 ;; label or comment.
7250 ;; Point is assumed to be at the prospective anchor point for the
7251 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
7252 ;; skip past open parens and containing statements. Most of the added
7253 ;; syntax elements will get the same anchor point - the exception is
7254 ;; for an anchor in a construct like "namespace"[*] - this is as early
7255 ;; as possible in the construct but on the same line as the {.
7257 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
7259 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
7260 ;; syntax symbol. They are appended after the anchor point.
7262 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
7263 ;; if the current statement starts there.
7265 ;; Note: It's not a problem if PAREN-STATE "overshoots"
7266 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
7268 ;; This function might do hidden buffer changes.
7270 (if (= (point) (c-point 'boi))
7271 ;; This is by far the most common case, so let's give it special
7273 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
7275 (let ((syntax-last c-syntactic-context)
7276 (boi (c-point 'boi))
7277 ;; Set when we're on a label, so that we don't stop there.
7278 ;; FIXME: To be complete we should check if we're on a label
7279 ;; now at the start.
7282 ;; Use point as the anchor point for "namespace", "extern", etc.
7283 (apply 'c-add-syntax syntax-symbol
7284 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
7288 ;; Loop while we have to back out of containing blocks.
7291 (catch 'back-up-block
7293 ;; Loop while we have to back up statements.
7294 (while (or (/= (point) boi)
7296 (looking-at c-comment-start-regexp))
7298 ;; Skip past any comments that stands between the
7299 ;; statement start and boi.
7300 (let ((savepos (point)))
7301 (while (and (/= savepos boi)
7302 (c-backward-single-comment))
7303 (setq savepos (point)
7304 boi (c-point 'boi)))
7305 (goto-char savepos))
7307 ;; Skip to the beginning of this statement or backward
7309 (let ((old-pos (point))
7311 (step-type (c-beginning-of-statement-1 containing-sexp)))
7312 (setq boi (c-point 'boi)
7313 on-label (eq step-type 'label))
7315 (cond ((= (point) old-pos)
7316 ;; If we didn't move we're at the start of a block and
7317 ;; have to continue outside it.
7318 (throw 'back-up-block t))
7320 ((and (eq step-type 'up)
7321 (>= (point) old-boi)
7322 (looking-at "else\\>[^_]")
7325 (looking-at "if\\>[^_]")))
7326 ;; Special case to avoid deeper and deeper indentation
7327 ;; of "else if" clauses.
7330 ((and (not stop-at-boi-only)
7331 (/= old-pos old-boi)
7332 (memq step-type '(up previous)))
7333 ;; If stop-at-boi-only is nil, we shouldn't back up
7334 ;; over previous or containing statements to try to
7335 ;; reach boi, so go back to the last position and
7338 (throw 'back-up-block nil))
7341 (if (and (not stop-at-boi-only)
7342 (memq step-type '(up previous beginning)))
7343 ;; If we've moved into another statement then we
7344 ;; should no longer try to stop in the middle of a
7346 (setq stop-at-boi-only t))
7348 ;; Record this as a substatement if we skipped up one
7350 (when (eq step-type 'up)
7351 (c-add-syntax 'substatement nil))))
7356 ;; Now we have to go out of this block.
7357 (goto-char containing-sexp)
7359 ;; Don't stop in the middle of a special brace list opener
7361 (when c-special-brace-lists
7362 (let ((special-list (c-looking-at-special-brace-list)))
7363 (when (and special-list
7364 (< (car (car special-list)) (point)))
7365 (setq containing-sexp (car (car special-list)))
7366 (goto-char containing-sexp))))
7368 (setq paren-state (c-whack-state-after containing-sexp paren-state)
7369 containing-sexp (c-most-enclosing-brace paren-state)
7372 ;; Analyze the construct in front of the block we've stepped out
7373 ;; from and add the right syntactic element for it.
7374 (let ((paren-pos (point))
7375 (paren-char (char-after))
7378 (if (eq paren-char ?\()
7379 ;; Stepped out of a parenthesis block, so we're in an
7382 (when (/= paren-pos boi)
7383 (if (and c-recognize-paren-inexpr-blocks
7385 (c-backward-syntactic-ws containing-sexp)
7386 (or (not (looking-at "\\>"))
7387 (not (c-on-identifier))))
7389 (goto-char (1+ paren-pos))
7390 (c-forward-syntactic-ws)
7391 (eq (char-after) ?{)))
7392 ;; Stepped out of an in-expression statement. This
7393 ;; syntactic element won't get an anchor pos.
7394 (c-add-syntax 'inexpr-statement)
7396 ;; A parenthesis normally belongs to an arglist.
7397 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
7401 (1+ containing-sexp)
7403 (setq step-type 'same
7406 ;; Stepped out of a brace block.
7407 (setq step-type (c-beginning-of-statement-1 containing-sexp)
7408 on-label (eq step-type 'label))
7410 (if (and (eq step-type 'same)
7411 (/= paren-pos (point)))
7415 (goto-char paren-pos)
7416 (setq inexpr (c-looking-at-inexpr-block
7417 (c-safe-position containing-sexp paren-state)
7419 (c-add-syntax (if (eq (car inexpr) 'inlambda)
7421 'statement-block-intro)
7423 ((looking-at c-other-decl-block-key)
7425 (cdr (assoc (match-string 1)
7426 c-other-decl-block-key-in-symbols-alist))
7427 (max (c-point 'boi paren-pos) (point))))
7428 (t (c-add-syntax 'defun-block-intro nil))))
7430 (c-add-syntax 'statement-block-intro nil)))
7432 (if (= paren-pos boi)
7433 ;; Always done if the open brace was at boi. The
7434 ;; c-beginning-of-statement-1 call above is necessary
7435 ;; anyway, to decide the type of block-intro to add.
7436 (goto-char paren-pos)
7437 (setq boi (c-point 'boi)))
7440 ;; Fill in the current point as the anchor for all the symbols
7442 (let ((p c-syntactic-context) q)
7443 (while (not (eq p syntax-last))
7444 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
7452 (defun c-add-class-syntax (symbol
7453 containing-decl-open
7454 containing-decl-start
7457 ;; The inclass and class-close syntactic symbols are added in
7458 ;; several places and some work is needed to fix everything.
7459 ;; Therefore it's collected here.
7461 ;; This function might do hidden buffer changes.
7462 (goto-char containing-decl-open)
7463 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
7465 (c-add-syntax symbol containing-decl-open)
7466 containing-decl-open)
7467 (goto-char containing-decl-start)
7468 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
7469 ;; here, but we have to do like this for compatibility.
7470 (back-to-indentation)
7471 (c-add-syntax symbol (point))
7472 (if (and (c-keyword-member containing-decl-kwd
7473 'c-inexpr-class-kwds)
7474 (/= containing-decl-start (c-point 'boi containing-decl-start)))
7475 (c-add-syntax 'inexpr-class))
7478 (defun c-guess-continued-construct (indent-point
7480 beg-of-same-or-containing-stmt
7483 ;; This function contains the decision tree reached through both
7484 ;; cases 18 and 10. It's a continued statement or top level
7485 ;; construct of some kind.
7487 ;; This function might do hidden buffer changes.
7489 (let (special-brace-list)
7490 (goto-char indent-point)
7491 (skip-chars-forward " \t")
7494 ;; (CASE A removed.)
7495 ;; CASE B: open braces for class or brace-lists
7496 ((setq special-brace-list
7497 (or (and c-special-brace-lists
7498 (c-looking-at-special-brace-list))
7499 (eq char-after-ip ?{)))
7502 ;; CASE B.1: class-open
7504 (and (eq (char-after) ?{)
7505 (c-looking-at-decl-block containing-sexp t)
7506 (setq beg-of-same-or-containing-stmt (point))))
7507 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
7509 ;; CASE B.2: brace-list-open
7510 ((or (consp special-brace-list)
7512 (goto-char beg-of-same-or-containing-stmt)
7513 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
7514 indent-point t t t)))
7515 ;; The most semantically accurate symbol here is
7516 ;; brace-list-open, but we normally report it simply as a
7517 ;; statement-cont. The reason is that one normally adjusts
7518 ;; brace-list-open for brace lists as top-level constructs,
7519 ;; and brace lists inside statements is a completely different
7520 ;; context. C.f. case 5A.3.
7521 (c-beginning-of-statement-1 containing-sexp)
7522 (c-add-stmt-syntax (if c-auto-newline-analysis
7523 ;; Turn off the dwim above when we're
7524 ;; analyzing the nature of the brace
7525 ;; for the auto newline feature.
7529 containing-sexp paren-state))
7531 ;; CASE B.3: The body of a function declared inside a normal
7532 ;; block. Can occur e.g. in Pike and when using gcc
7533 ;; extensions, but watch out for macros followed by blocks.
7534 ;; C.f. cases E, 16F and 17G.
7535 ((and (not (c-at-statement-start-p))
7536 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7539 (let ((c-recognize-typeless-decls nil))
7540 ;; Turn off recognition of constructs that lacks a
7541 ;; type in this case, since that's more likely to be
7542 ;; a macro followed by a block.
7543 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7544 (c-add-stmt-syntax 'defun-open nil t
7545 containing-sexp paren-state))
7547 ;; CASE B.4: Continued statement with block open. The most
7548 ;; accurate analysis is perhaps `statement-cont' together with
7549 ;; `block-open' but we play DWIM and use `substatement-open'
7550 ;; instead. The rationaly is that this typically is a macro
7551 ;; followed by a block which makes it very similar to a
7552 ;; statement with a substatement block.
7554 (c-add-stmt-syntax 'substatement-open nil nil
7555 containing-sexp paren-state))
7558 ;; CASE C: iostream insertion or extraction operator
7559 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
7561 (goto-char beg-of-same-or-containing-stmt)
7562 ;; If there is no preceding streamop in the statement
7563 ;; then indent this line as a normal statement-cont.
7564 (when (c-syntactic-re-search-forward
7565 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
7566 (c-add-syntax 'stream-op (c-point 'boi))
7569 ;; CASE E: In the "K&R region" of a function declared inside a
7570 ;; normal block. C.f. case B.3.
7571 ((and (save-excursion
7572 ;; Check that the next token is a '{'. This works as
7573 ;; long as no language that allows nested function
7574 ;; definitions allows stuff like member init lists, K&R
7575 ;; declarations or throws clauses there.
7577 ;; Note that we do a forward search for something ahead
7578 ;; of the indentation line here. That's not good since
7579 ;; the user might not have typed it yet. Unfortunately
7580 ;; it's exceedingly tricky to recognize a function
7581 ;; prototype in a code block without resorting to this.
7582 (c-forward-syntactic-ws)
7583 (eq (char-after) ?{))
7584 (not (c-at-statement-start-p))
7585 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7588 (let ((c-recognize-typeless-decls nil))
7589 ;; Turn off recognition of constructs that lacks a
7590 ;; type in this case, since that's more likely to be
7591 ;; a macro followed by a block.
7592 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7593 (c-add-stmt-syntax 'func-decl-cont nil t
7594 containing-sexp paren-state))
7596 ;; CASE D: continued statement.
7598 (c-beginning-of-statement-1 containing-sexp)
7599 (c-add-stmt-syntax 'statement-cont nil nil
7600 containing-sexp paren-state))
7603 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
7606 (defun c-guess-basic-syntax ()
7607 "Return the syntactic context of the current line."
7610 (c-save-buffer-state
7611 ((indent-point (point))
7612 (case-fold-search nil)
7613 ;; A whole ugly bunch of various temporary variables. Have
7614 ;; to declare them here since it's not possible to declare
7615 ;; a variable with only the scope of a cond test and the
7616 ;; following result clauses, and most of this function is a
7617 ;; single gigantic cond. :P
7618 literal char-before-ip before-ws-ip char-after-ip macro-start
7619 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
7620 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
7621 ;; The following record some positions for the containing
7622 ;; declaration block if we're directly within one:
7623 ;; `containing-decl-open' is the position of the open
7624 ;; brace. `containing-decl-start' is the start of the
7625 ;; declaration. `containing-decl-kwd' is the keyword
7626 ;; symbol of the keyword that tells what kind of block it
7628 containing-decl-open
7629 containing-decl-start
7631 ;; The open paren of the closest surrounding sexp or nil if
7634 ;; The position after the closest preceding brace sexp
7635 ;; (nested sexps are ignored), or the position after
7636 ;; `containing-sexp' if there is none, or (point-min) if
7637 ;; `containing-sexp' is nil.
7639 ;; The paren state outside `containing-sexp', or at
7640 ;; `indent-point' if `containing-sexp' is nil.
7641 (paren-state (c-parse-state))
7642 ;; There's always at most one syntactic element which got
7643 ;; an anchor pos. It's stored in syntactic-relpos.
7645 (c-stmt-delim-chars c-stmt-delim-chars))
7647 ;; Check if we're directly inside an enclosing declaration
7649 (when (and (setq containing-sexp
7650 (c-most-enclosing-brace paren-state))
7652 (goto-char containing-sexp)
7653 (eq (char-after) ?{))
7655 (c-looking-at-decl-block
7656 (c-most-enclosing-brace paren-state
7659 (setq containing-decl-open containing-sexp
7660 containing-decl-start (point)
7661 containing-sexp nil)
7662 (goto-char placeholder)
7663 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
7664 (c-keyword-sym (match-string 1)))))
7666 ;; Init some position variables.
7669 (setq containing-sexp (car paren-state)
7670 paren-state (cdr paren-state))
7671 (if (consp containing-sexp)
7673 (setq lim (cdr containing-sexp))
7674 (if (cdr c-state-cache)
7675 ;; Ignore balanced paren. The next entry
7676 ;; can't be another one.
7677 (setq containing-sexp (car (cdr c-state-cache))
7678 paren-state (cdr paren-state))
7679 ;; If there is no surrounding open paren then
7680 ;; put the last balanced pair back on paren-state.
7681 (setq paren-state (cons containing-sexp paren-state)
7682 containing-sexp nil)))
7683 (setq lim (1+ containing-sexp))))
7684 (setq lim (point-min)))
7686 ;; If we're in a parenthesis list then ',' delimits the
7687 ;; "statements" rather than being an operator (with the
7688 ;; exception of the "for" clause). This difference is
7689 ;; typically only noticeable when statements are used in macro
7691 (when (and containing-sexp
7692 (eq (char-after containing-sexp) ?\())
7693 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
7695 ;; cache char before and after indent point, and move point to
7696 ;; the most likely position to perform the majority of tests
7697 (goto-char indent-point)
7698 (c-backward-syntactic-ws lim)
7699 (setq before-ws-ip (point)
7700 char-before-ip (char-before))
7701 (goto-char indent-point)
7702 (skip-chars-forward " \t")
7703 (setq char-after-ip (char-after))
7705 ;; are we in a literal?
7706 (setq literal (c-in-literal lim))
7708 ;; now figure out syntactic qualities of the current line
7711 ;; CASE 1: in a string.
7712 ((eq literal 'string)
7713 (c-add-syntax 'string (c-point 'bopl)))
7715 ;; CASE 2: in a C or C++ style comment.
7716 ((and (memq literal '(c c++))
7717 ;; This is a kludge for XEmacs where we use
7718 ;; `buffer-syntactic-context', which doesn't correctly
7719 ;; recognize "\*/" to end a block comment.
7720 ;; `parse-partial-sexp' which is used by
7721 ;; `c-literal-limits' will however do that in most
7722 ;; versions, which results in that we get nil from
7723 ;; `c-literal-limits' even when `c-in-literal' claims
7724 ;; we're inside a comment.
7725 (setq placeholder (c-literal-limits lim)))
7726 (c-add-syntax literal (car placeholder)))
7728 ;; CASE 3: in a cpp preprocessor macro continuation.
7729 ((and (save-excursion
7730 (when (c-beginning-of-macro)
7731 (setq macro-start (point))))
7732 (/= macro-start (c-point 'boi))
7734 (setq tmpsymbol 'cpp-macro-cont)
7735 (or (not c-syntactic-indentation-in-macros)
7737 (goto-char macro-start)
7738 ;; If at the beginning of the body of a #define
7739 ;; directive then analyze as cpp-define-intro
7740 ;; only. Go on with the syntactic analysis
7741 ;; otherwise. in-macro-expr is set if we're in a
7742 ;; cpp expression, i.e. before the #define body
7743 ;; or anywhere in a non-#define directive.
7744 (if (c-forward-to-cpp-define-body)
7745 (let ((indent-boi (c-point 'boi indent-point)))
7746 (setq in-macro-expr (> (point) indent-boi)
7747 tmpsymbol 'cpp-define-intro)
7748 (= (point) indent-boi))
7749 (setq in-macro-expr t)
7751 (c-add-syntax tmpsymbol macro-start)
7752 (setq macro-start nil))
7754 ;; CASE 11: an else clause?
7755 ((looking-at "else\\>[^_]")
7756 (c-beginning-of-statement-1 containing-sexp)
7757 (c-add-stmt-syntax 'else-clause nil t
7758 containing-sexp paren-state))
7760 ;; CASE 12: while closure of a do/while construct?
7761 ((and (looking-at "while\\>[^_]")
7763 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
7765 (setq placeholder (point)))))
7766 (goto-char placeholder)
7767 (c-add-stmt-syntax 'do-while-closure nil t
7768 containing-sexp paren-state))
7770 ;; CASE 13: A catch or finally clause? This case is simpler
7771 ;; than if-else and do-while, because a block is required
7772 ;; after every try, catch and finally.
7774 (and (cond ((c-major-mode-is 'c++-mode)
7775 (looking-at "catch\\>[^_]"))
7776 ((c-major-mode-is 'java-mode)
7777 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
7778 (and (c-safe (c-backward-syntactic-ws)
7781 (eq (char-after) ?{)
7782 (c-safe (c-backward-syntactic-ws)
7785 (if (eq (char-after) ?\()
7786 (c-safe (c-backward-sexp) t)
7788 (looking-at "\\(try\\|catch\\)\\>[^_]")
7789 (setq placeholder (point))))
7790 (goto-char placeholder)
7791 (c-add-stmt-syntax 'catch-clause nil t
7792 containing-sexp paren-state))
7794 ;; CASE 18: A substatement we can recognize by keyword.
7796 (and c-opt-block-stmt-key
7797 (not (eq char-before-ip ?\;))
7798 (not (c-at-vsemi-p before-ws-ip))
7799 (not (memq char-after-ip '(?\) ?\] ?,)))
7800 (or (not (eq char-before-ip ?}))
7801 (c-looking-at-inexpr-block-backward c-state-cache))
7804 ;; Ought to cache the result from the
7805 ;; c-beginning-of-statement-1 calls here.
7806 (setq placeholder (point))
7807 (while (eq (setq step-type
7808 (c-beginning-of-statement-1 lim))
7810 (if (eq step-type 'previous)
7811 (goto-char placeholder)
7812 (setq placeholder (point))
7813 (if (and (eq step-type 'same)
7814 (not (looking-at c-opt-block-stmt-key)))
7815 ;; Step up to the containing statement if we
7816 ;; stayed in the same one.
7820 (c-beginning-of-statement-1 lim))
7823 (setq placeholder (point))
7824 ;; There was no containing statement afterall.
7825 (goto-char placeholder)))))
7827 (if (looking-at c-block-stmt-2-key)
7828 ;; Require a parenthesis after these keywords.
7829 ;; Necessary to catch e.g. synchronized in Java,
7830 ;; which can be used both as statement and
7832 (and (zerop (c-forward-token-2 1 nil))
7833 (eq (char-after) ?\())
7834 (looking-at c-opt-block-stmt-key))))
7836 (if (eq step-type 'up)
7837 ;; CASE 18A: Simple substatement.
7839 (goto-char placeholder)
7841 ((eq char-after-ip ?{)
7842 (c-add-stmt-syntax 'substatement-open nil nil
7843 containing-sexp paren-state))
7845 (goto-char indent-point)
7846 (back-to-indentation)
7848 (c-add-stmt-syntax 'substatement-label nil nil
7849 containing-sexp paren-state))
7851 (c-add-stmt-syntax 'substatement nil nil
7852 containing-sexp paren-state))))
7854 ;; CASE 18B: Some other substatement. This is shared
7856 (c-guess-continued-construct indent-point
7862 ;; CASE 14: A case or default label
7863 ((looking-at c-label-kwds-regexp)
7866 (goto-char containing-sexp)
7867 (setq lim (c-most-enclosing-brace c-state-cache
7869 (c-backward-to-block-anchor lim)
7870 (c-add-stmt-syntax 'case-label nil t lim paren-state))
7871 ;; Got a bogus label at the top level. In lack of better
7872 ;; alternatives, anchor it on (point-min).
7873 (c-add-syntax 'case-label (point-min))))
7875 ;; CASE 15: any other label
7877 (back-to-indentation)
7878 (and (not (looking-at c-syntactic-ws-start))
7880 (cond (containing-decl-open
7881 (setq placeholder (c-add-class-syntax 'inclass
7882 containing-decl-open
7883 containing-decl-start
7886 ;; Append access-label with the same anchor point as
7888 (c-append-syntax 'access-label placeholder))
7891 (goto-char containing-sexp)
7892 (setq lim (c-most-enclosing-brace c-state-cache
7896 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
7897 (looking-at "switch\\>[^_]"))
7898 ;; If the surrounding statement is a switch then
7899 ;; let's analyze all labels as switch labels, so
7900 ;; that they get lined up consistently.
7903 (c-backward-to-block-anchor lim)
7904 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
7907 ;; A label on the top level. Treat it as a class
7908 ;; context. (point-min) is the closest we get to the
7909 ;; class open brace.
7910 (c-add-syntax 'access-label (point-min)))))
7912 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
7914 ((setq placeholder (c-looking-at-inexpr-block
7915 (c-safe-position containing-sexp paren-state)
7917 ;; Have to turn on the heuristics after
7918 ;; the point even though it doesn't work
7919 ;; very well. C.f. test case class-16.pike.
7921 (setq tmpsymbol (assq (car placeholder)
7922 '((inexpr-class . class-open)
7923 (inexpr-statement . block-open))))
7925 ;; It's a statement block or an anonymous class.
7926 (setq tmpsymbol (cdr tmpsymbol))
7927 ;; It's a Pike lambda. Check whether we are between the
7928 ;; lambda keyword and the argument list or at the defun
7930 (setq tmpsymbol (if (eq char-after-ip ?{)
7932 'lambda-intro-cont)))
7933 (goto-char (cdr placeholder))
7934 (back-to-indentation)
7935 (c-add-stmt-syntax tmpsymbol nil t
7936 (c-most-enclosing-brace c-state-cache (point))
7938 (unless (eq (point) (cdr placeholder))
7939 (c-add-syntax (car placeholder))))
7941 ;; CASE 5: Line is inside a declaration level block or at top level.
7942 ((or containing-decl-open (null containing-sexp))
7945 ;; CASE 5A: we are looking at a defun, brace list, class,
7946 ;; or inline-inclass method opening brace
7947 ((setq special-brace-list
7948 (or (and c-special-brace-lists
7949 (c-looking-at-special-brace-list))
7950 (eq char-after-ip ?{)))
7953 ;; CASE 5A.1: Non-class declaration block open.
7956 (and (eq char-after-ip ?{)
7957 (setq tmp (c-looking-at-decl-block containing-sexp t))
7959 (setq placeholder (point))
7961 (looking-at c-symbol-key))
7963 (c-keyword-sym (setq keyword (match-string 0)))
7964 'c-other-block-decl-kwds))))
7965 (goto-char placeholder)
7967 (if (string-equal keyword "extern")
7968 ;; Special case for extern-lang-open.
7970 (intern (concat keyword "-open")))
7971 nil t containing-sexp paren-state))
7973 ;; CASE 5A.2: we are looking at a class opening brace
7975 (goto-char indent-point)
7976 (skip-chars-forward " \t")
7977 (and (eq (char-after) ?{)
7978 (c-looking-at-decl-block containing-sexp t)
7979 (setq placeholder (point))))
7980 (c-add-syntax 'class-open placeholder))
7982 ;; CASE 5A.3: brace list open
7984 (c-beginning-of-decl-1 lim)
7985 (while (looking-at c-specifier-key)
7986 (goto-char (match-end 1))
7987 (c-forward-syntactic-ws indent-point))
7988 (setq placeholder (c-point 'boi))
7989 (or (consp special-brace-list)
7990 (and (or (save-excursion
7991 (goto-char indent-point)
7992 (setq tmpsymbol nil)
7993 (while (and (> (point) placeholder)
7994 (zerop (c-backward-token-2 1 t))
7995 (/= (char-after) ?=))
7996 (and c-opt-inexpr-brace-list-key
7998 (looking-at c-opt-inexpr-brace-list-key)
7999 (setq tmpsymbol 'topmost-intro-cont)))
8000 (eq (char-after) ?=))
8001 (looking-at c-brace-list-key))
8003 (while (and (< (point) indent-point)
8004 (zerop (c-forward-token-2 1 t))
8005 (not (memq (char-after) '(?\; ?\()))))
8006 (not (memq (char-after) '(?\; ?\()))
8008 (if (and (not c-auto-newline-analysis)
8009 (c-major-mode-is 'java-mode)
8010 (eq tmpsymbol 'topmost-intro-cont))
8011 ;; We're in Java and have found that the open brace
8012 ;; belongs to a "new Foo[]" initialization list,
8013 ;; which means the brace list is part of an
8014 ;; expression and not a top level definition. We
8015 ;; therefore treat it as any topmost continuation
8016 ;; even though the semantically correct symbol still
8017 ;; is brace-list-open, on the same grounds as in
8020 (c-beginning-of-statement-1 lim)
8021 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8022 (c-add-syntax 'brace-list-open placeholder)))
8024 ;; CASE 5A.4: inline defun open
8025 ((and containing-decl-open
8026 (not (c-keyword-member containing-decl-kwd
8027 'c-other-block-decl-kwds)))
8028 (c-add-syntax 'inline-open)
8029 (c-add-class-syntax 'inclass
8030 containing-decl-open
8031 containing-decl-start
8035 ;; CASE 5A.5: ordinary defun open
8038 (c-beginning-of-decl-1 lim)
8039 (while (looking-at c-specifier-key)
8040 (goto-char (match-end 1))
8041 (c-forward-syntactic-ws indent-point))
8042 (c-add-syntax 'defun-open (c-point 'boi))
8043 ;; Bogus to use bol here, but it's the legacy. (Resolved,
8047 ;; CASE 5B: After a function header but before the body (or
8048 ;; the ending semicolon if there's no body).
8050 (when (setq placeholder (c-just-after-func-arglist-p lim))
8051 (setq tmp-pos (point))))
8054 ;; CASE 5B.1: Member init list.
8055 ((eq (char-after tmp-pos) ?:)
8056 (if (or (> tmp-pos indent-point)
8057 (= (c-point 'bosws) (1+ tmp-pos)))
8059 ;; There is no preceding member init clause.
8060 ;; Indent relative to the beginning of indentation
8061 ;; for the topmost-intro line that contains the
8062 ;; prototype's open paren.
8063 (goto-char placeholder)
8064 (c-add-syntax 'member-init-intro (c-point 'boi)))
8065 ;; Indent relative to the first member init clause.
8066 (goto-char (1+ tmp-pos))
8067 (c-forward-syntactic-ws)
8068 (c-add-syntax 'member-init-cont (point))))
8070 ;; CASE 5B.2: K&R arg decl intro
8071 ((and c-recognize-knr-p
8072 (c-in-knr-argdecl lim))
8073 (c-beginning-of-statement-1 lim)
8074 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
8075 (if containing-decl-open
8076 (c-add-class-syntax 'inclass
8077 containing-decl-open
8078 containing-decl-start
8082 ;; CASE 5B.4: Nether region after a C++ or Java func
8083 ;; decl, which could include a `throws' declaration.
8085 (c-beginning-of-statement-1 lim)
8086 (c-add-syntax 'func-decl-cont (c-point 'boi))
8089 ;; CASE 5C: inheritance line. could be first inheritance
8090 ;; line, or continuation of a multiple inheritance
8091 ((or (and (c-major-mode-is 'c++-mode)
8093 (when (eq char-after-ip ?,)
8094 (skip-chars-forward " \t")
8096 (looking-at c-opt-postfix-decl-spec-key)))
8097 (and (or (eq char-before-ip ?:)
8098 ;; watch out for scope operator
8100 (and (eq char-after-ip ?:)
8101 (c-safe (forward-char 1) t)
8102 (not (eq (char-after) ?:))
8105 (c-backward-syntactic-ws lim)
8106 (if (eq char-before-ip ?:)
8109 (c-backward-syntactic-ws lim)))
8110 (back-to-indentation)
8111 (looking-at c-class-key)))
8113 (and (c-major-mode-is 'java-mode)
8114 (let ((fence (save-excursion
8115 (c-beginning-of-statement-1 lim)
8120 (cond ((looking-at c-opt-postfix-decl-spec-key)
8121 (setq injava-inher (cons cont (point))
8123 ((or (not (c-safe (c-forward-sexp -1) t))
8129 (not (c-crosses-statement-barrier-p (cdr injava-inher)
8134 ;; CASE 5C.1: non-hanging colon on an inher intro
8135 ((eq char-after-ip ?:)
8136 (c-beginning-of-statement-1 lim)
8137 (c-add-syntax 'inher-intro (c-point 'boi))
8138 ;; don't add inclass symbol since relative point already
8139 ;; contains any class offset
8142 ;; CASE 5C.2: hanging colon on an inher intro
8143 ((eq char-before-ip ?:)
8144 (c-beginning-of-statement-1 lim)
8145 (c-add-syntax 'inher-intro (c-point 'boi))
8146 (if containing-decl-open
8147 (c-add-class-syntax 'inclass
8148 containing-decl-open
8149 containing-decl-start
8153 ;; CASE 5C.3: in a Java implements/extends
8155 (let ((where (cdr injava-inher))
8156 (cont (car injava-inher)))
8158 (cond ((looking-at "throws\\>[^_]")
8159 (c-add-syntax 'func-decl-cont
8160 (progn (c-beginning-of-statement-1 lim)
8162 (cont (c-add-syntax 'inher-cont where))
8163 (t (c-add-syntax 'inher-intro
8164 (progn (goto-char (cdr injava-inher))
8165 (c-beginning-of-statement-1 lim)
8169 ;; CASE 5C.4: a continued inheritance line
8171 (c-beginning-of-inheritance-list lim)
8172 (c-add-syntax 'inher-cont (point))
8173 ;; don't add inclass symbol since relative point already
8174 ;; contains any class offset
8177 ;; CASE 5D: this could be a top-level initialization, a
8178 ;; member init list continuation, or a template argument
8179 ;; list continuation.
8181 ;; Note: We use the fact that lim always is after any
8182 ;; preceding brace sexp.
8183 (if c-recognize-<>-arglists
8186 (c-syntactic-skip-backward "^;,=<>" lim t)
8189 (when c-overloadable-operators-regexp
8190 (when (setq placeholder (c-after-special-operator-id lim))
8191 (goto-char placeholder)
8194 ((eq (char-before) ?>)
8195 (or (c-backward-<>-arglist nil lim)
8198 ((eq (char-before) ?<)
8201 (c-forward-<>-arglist nil))
8202 (progn (forward-char)
8206 ;; NB: No c-after-special-operator-id stuff in this
8207 ;; clause - we assume only C++ needs it.
8208 (c-syntactic-skip-backward "^;,=" lim t))
8209 (memq (char-before) '(?, ?= ?<)))
8212 ;; CASE 5D.3: perhaps a template list continuation?
8213 ((and (c-major-mode-is 'c++-mode)
8216 (c-with-syntax-table c++-template-syntax-table
8217 (goto-char indent-point)
8218 (setq placeholder (c-up-list-backward))
8220 (eq (char-after placeholder) ?<))))))
8221 (c-with-syntax-table c++-template-syntax-table
8222 (goto-char placeholder)
8223 (c-beginning-of-statement-1 lim t)
8225 (c-backward-syntactic-ws lim)
8226 (eq (char-before) ?<))
8227 ;; In a nested template arglist.
8229 (goto-char placeholder)
8230 (c-syntactic-skip-backward "^,;" lim t)
8231 (c-forward-syntactic-ws))
8232 (back-to-indentation)))
8233 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8235 (c-add-syntax 'template-args-cont (point)))
8237 ;; CASE 5D.4: perhaps a multiple inheritance line?
8238 ((and (c-major-mode-is 'c++-mode)
8240 (c-beginning-of-statement-1 lim)
8241 (setq placeholder (point))
8242 (if (looking-at "static\\>[^_]")
8243 (c-forward-token-2 1 nil indent-point))
8244 (and (looking-at c-class-key)
8245 (zerop (c-forward-token-2 2 nil indent-point))
8246 (if (eq (char-after) ?<)
8247 (c-with-syntax-table c++-template-syntax-table
8248 (zerop (c-forward-token-2 1 t indent-point)))
8250 (eq (char-after) ?:))))
8251 (goto-char placeholder)
8252 (c-add-syntax 'inher-cont (c-point 'boi)))
8254 ;; CASE 5D.5: Continuation of the "expression part" of a
8255 ;; top level construct.
8257 (while (and (eq (car (c-beginning-of-decl-1 containing-sexp))
8260 (c-backward-syntactic-ws)
8261 (eq (char-before) ?}))))
8263 (if (eq char-before-ip ?,)
8264 ;; A preceding comma at the top level means that a
8265 ;; new variable declaration starts here. Use
8266 ;; topmost-intro-cont for it, for consistency with
8267 ;; the first variable declaration. C.f. case 5N.
8270 nil nil containing-sexp paren-state))
8273 ;; CASE 5F: Close of a non-class declaration level block.
8274 ((and (eq char-after-ip ?})
8275 (c-keyword-member containing-decl-kwd
8276 'c-other-block-decl-kwds))
8277 ;; This is inconsistent: Should use `containing-decl-open'
8278 ;; here if it's at boi, like in case 5J.
8279 (goto-char containing-decl-start)
8281 (if (string-equal (symbol-name containing-decl-kwd) "extern")
8282 ;; Special case for compatibility with the
8283 ;; extern-lang syntactic symbols.
8285 (intern (concat (symbol-name containing-decl-kwd)
8288 (c-most-enclosing-brace paren-state (point))
8291 ;; CASE 5G: we are looking at the brace which closes the
8292 ;; enclosing nested class decl
8293 ((and containing-sexp
8294 (eq char-after-ip ?})
8295 (eq containing-decl-open containing-sexp))
8296 (c-add-class-syntax 'class-close
8297 containing-decl-open
8298 containing-decl-start
8302 ;; CASE 5H: we could be looking at subsequent knr-argdecls
8303 ((and c-recognize-knr-p
8304 (not containing-sexp) ; can't be knr inside braces.
8305 (not (eq char-before-ip ?}))
8307 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
8309 ;; Do an extra check to avoid tripping up on
8310 ;; statements that occur in invalid contexts
8311 ;; (e.g. in macro bodies where we don't really
8312 ;; know the context of what we're looking at).
8313 (not (and c-opt-block-stmt-key
8314 (looking-at c-opt-block-stmt-key)))))
8315 (< placeholder indent-point))
8316 (goto-char placeholder)
8317 (c-add-syntax 'knr-argdecl (point)))
8319 ;; CASE 5I: ObjC method definition.
8320 ((and c-opt-method-key
8321 (looking-at c-opt-method-key))
8322 (c-beginning-of-statement-1 nil t)
8323 (if (= (point) indent-point)
8324 ;; Handle the case when it's the first (non-comment)
8325 ;; thing in the buffer. Can't look for a 'same return
8326 ;; value from cbos1 since ObjC directives currently
8327 ;; aren't recognized fully, so that we get 'same
8328 ;; instead of 'previous if it moved over a preceding
8330 (goto-char (point-min)))
8331 (c-add-syntax 'objc-method-intro (c-point 'boi)))
8333 ;; CASE 5P: AWK pattern or function or continuation
8335 ((c-major-mode-is 'awk-mode)
8336 (setq placeholder (point))
8338 (if (and (eq (c-beginning-of-statement-1) 'same)
8339 (/= (point) placeholder))
8343 containing-sexp paren-state))
8345 ;; CASE 5N: At a variable declaration that follows a class
8346 ;; definition or some other block declaration that doesn't
8347 ;; end at the closing '}'. C.f. case 5D.5.
8349 (c-backward-syntactic-ws lim)
8350 (and (eq (char-before) ?})
8352 (let ((start (point)))
8353 (if (and c-state-cache
8354 (consp (car c-state-cache))
8355 (eq (cdar c-state-cache) (point)))
8356 ;; Speed up the backward search a bit.
8357 (goto-char (caar c-state-cache)))
8358 (c-beginning-of-decl-1 containing-sexp)
8359 (setq placeholder (point))
8360 (if (= start (point))
8361 ;; The '}' is unbalanced.
8364 (>= (point) indent-point))))))
8365 (goto-char placeholder)
8366 (c-add-stmt-syntax 'topmost-intro-cont nil nil
8367 containing-sexp paren-state))
8369 ;; NOTE: The point is at the end of the previous token here.
8371 ;; CASE 5J: we are at the topmost level, make
8372 ;; sure we skip back past any access specifiers
8374 ;; A macro continuation line is never at top level.
8375 (not (and macro-start
8376 (> indent-point macro-start)))
8378 (setq placeholder (point))
8379 (or (memq char-before-ip '(?\; ?{ ?} nil))
8380 (c-at-vsemi-p before-ws-ip)
8381 (when (and (eq char-before-ip ?:)
8382 (eq (c-beginning-of-statement-1 lim)
8384 (c-backward-syntactic-ws lim)
8385 (setq placeholder (point)))
8386 (and (c-major-mode-is 'objc-mode)
8387 (catch 'not-in-directive
8388 (c-beginning-of-statement-1 lim)
8389 (setq placeholder (point))
8390 (while (and (c-forward-objc-directive)
8391 (< (point) indent-point))
8392 (c-forward-syntactic-ws)
8393 (if (>= (point) indent-point)
8394 (throw 'not-in-directive t))
8395 (setq placeholder (point)))
8397 ;; For historic reasons we anchor at bol of the last
8398 ;; line of the previous declaration. That's clearly
8399 ;; highly bogus and useless, and it makes our lives hard
8400 ;; to remain compatible. :P
8401 (goto-char placeholder)
8402 (c-add-syntax 'topmost-intro (c-point 'bol))
8403 (if containing-decl-open
8404 (if (c-keyword-member containing-decl-kwd
8405 'c-other-block-decl-kwds)
8407 (goto-char (c-brace-anchor-point containing-decl-open))
8409 (if (string-equal (symbol-name containing-decl-kwd)
8411 ;; Special case for compatibility with the
8412 ;; extern-lang syntactic symbols.
8414 (intern (concat "in"
8415 (symbol-name containing-decl-kwd))))
8417 (c-most-enclosing-brace paren-state (point))
8419 (c-add-class-syntax 'inclass
8420 containing-decl-open
8421 containing-decl-start
8424 (when (and c-syntactic-indentation-in-macros
8426 (/= macro-start (c-point 'boi indent-point)))
8427 (c-add-syntax 'cpp-define-intro)
8428 (setq macro-start nil)))
8430 ;; CASE 5K: we are at an ObjC method definition
8431 ;; continuation line.
8432 ((and c-opt-method-key
8434 (c-beginning-of-statement-1 lim)
8436 (when (looking-at c-opt-method-key)
8437 (setq placeholder (point)))))
8438 (c-add-syntax 'objc-method-args-cont placeholder))
8440 ;; CASE 5L: we are at the first argument of a template
8441 ;; arglist that begins on the previous line.
8442 ((and c-recognize-<>-arglists
8443 (eq (char-before) ?<)
8444 (not (and c-overloadable-operators-regexp
8445 (c-after-special-operator-id lim))))
8446 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8447 (c-add-syntax 'template-args-cont (c-point 'boi)))
8449 ;; CASE 5Q: we are at a statement within a macro.
8451 (c-beginning-of-statement-1 containing-sexp)
8452 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
8454 ;; CASE 5M: we are at a topmost continuation line
8456 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8457 (when (c-major-mode-is 'objc-mode)
8458 (setq placeholder (point))
8459 (while (and (c-forward-objc-directive)
8460 (< (point) indent-point))
8461 (c-forward-syntactic-ws)
8462 (setq placeholder (point)))
8463 (goto-char placeholder))
8464 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8467 ;; (CASE 6 has been removed.)
8469 ;; CASE 7: line is an expression, not a statement. Most
8470 ;; likely we are either in a function prototype or a function
8471 ;; call argument list
8472 ((not (or (and c-special-brace-lists
8474 (goto-char containing-sexp)
8475 (c-looking-at-special-brace-list)))
8476 (eq (char-after containing-sexp) ?{)))
8479 ;; CASE 7A: we are looking at the arglist closing paren.
8481 ((memq char-after-ip '(?\) ?\]))
8482 (goto-char containing-sexp)
8483 (setq placeholder (c-point 'boi))
8484 (if (and (c-safe (backward-up-list 1) t)
8485 (>= (point) placeholder))
8488 (skip-chars-forward " \t"))
8489 (goto-char placeholder))
8490 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
8491 (c-most-enclosing-brace paren-state (point))
8494 ;; CASE 7B: Looking at the opening brace of an
8495 ;; in-expression block or brace list. C.f. cases 4, 16A
8497 ((and (eq char-after-ip ?{)
8499 (setq placeholder (c-inside-bracelist-p (point)
8502 (setq tmpsymbol '(brace-list-open . inexpr-class))
8503 (setq tmpsymbol '(block-open . inexpr-statement)
8505 (cdr-safe (c-looking-at-inexpr-block
8506 (c-safe-position containing-sexp
8509 ;; placeholder is nil if it's a block directly in
8510 ;; a function arglist. That makes us skip out of
8513 (goto-char placeholder)
8514 (back-to-indentation)
8515 (c-add-stmt-syntax (car tmpsymbol) nil t
8516 (c-most-enclosing-brace paren-state (point))
8518 (if (/= (point) placeholder)
8519 (c-add-syntax (cdr tmpsymbol))))
8521 ;; CASE 7C: we are looking at the first argument in an empty
8522 ;; argument list. Use arglist-close if we're actually
8523 ;; looking at a close paren or bracket.
8524 ((memq char-before-ip '(?\( ?\[))
8525 (goto-char containing-sexp)
8526 (setq placeholder (c-point 'boi))
8527 (if (and (c-safe (backward-up-list 1) t)
8528 (>= (point) placeholder))
8531 (skip-chars-forward " \t"))
8532 (goto-char placeholder))
8533 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
8534 (c-most-enclosing-brace paren-state (point))
8537 ;; CASE 7D: we are inside a conditional test clause. treat
8538 ;; these things as statements
8540 (goto-char containing-sexp)
8541 (and (c-safe (c-forward-sexp -1) t)
8542 (looking-at "\\<for\\>[^_]")))
8543 (goto-char (1+ containing-sexp))
8544 (c-forward-syntactic-ws indent-point)
8545 (if (eq char-before-ip ?\;)
8546 (c-add-syntax 'statement (point))
8547 (c-add-syntax 'statement-cont (point))
8550 ;; CASE 7E: maybe a continued ObjC method call. This is the
8551 ;; case when we are inside a [] bracketed exp, and what
8552 ;; precede the opening bracket is not an identifier.
8553 ((and c-opt-method-key
8554 (eq (char-after containing-sexp) ?\[)
8556 (goto-char (1- containing-sexp))
8557 (c-backward-syntactic-ws (c-point 'bod))
8558 (if (not (looking-at c-symbol-key))
8559 (c-add-syntax 'objc-method-call-cont containing-sexp))
8562 ;; CASE 7F: we are looking at an arglist continuation line,
8563 ;; but the preceding argument is on the same line as the
8564 ;; opening paren. This case includes multi-line
8565 ;; mathematical paren groupings, but we could be on a
8566 ;; for-list continuation line. C.f. case 7A.
8568 (goto-char (1+ containing-sexp))
8570 (c-forward-syntactic-ws)
8573 (goto-char containing-sexp)
8574 (setq placeholder (c-point 'boi))
8575 (if (and (c-safe (backward-up-list 1) t)
8576 (>= (point) placeholder))
8579 (skip-chars-forward " \t"))
8580 (goto-char placeholder))
8581 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
8582 (c-most-enclosing-brace c-state-cache (point))
8585 ;; CASE 7G: we are looking at just a normal arglist
8586 ;; continuation line
8587 (t (c-forward-syntactic-ws indent-point)
8588 (c-add-syntax 'arglist-cont (c-point 'boi)))
8591 ;; CASE 8: func-local multi-inheritance line
8592 ((and (c-major-mode-is 'c++-mode)
8594 (goto-char indent-point)
8595 (skip-chars-forward " \t")
8596 (looking-at c-opt-postfix-decl-spec-key)))
8597 (goto-char indent-point)
8598 (skip-chars-forward " \t")
8601 ;; CASE 8A: non-hanging colon on an inher intro
8602 ((eq char-after-ip ?:)
8603 (c-backward-syntactic-ws lim)
8604 (c-add-syntax 'inher-intro (c-point 'boi)))
8606 ;; CASE 8B: hanging colon on an inher intro
8607 ((eq char-before-ip ?:)
8608 (c-add-syntax 'inher-intro (c-point 'boi)))
8610 ;; CASE 8C: a continued inheritance line
8612 (c-beginning-of-inheritance-list lim)
8613 (c-add-syntax 'inher-cont (point))
8616 ;; CASE 9: we are inside a brace-list
8617 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
8618 (setq special-brace-list
8619 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
8621 (goto-char containing-sexp)
8622 (c-looking-at-special-brace-list)))
8623 (c-inside-bracelist-p containing-sexp paren-state))))
8626 ;; CASE 9A: In the middle of a special brace list opener.
8627 ((and (consp special-brace-list)
8629 (goto-char containing-sexp)
8630 (eq (char-after) ?\())
8631 (eq char-after-ip (car (cdr special-brace-list))))
8632 (goto-char (car (car special-brace-list)))
8633 (skip-chars-backward " \t")
8635 (assoc 'statement-cont
8636 (setq placeholder (c-guess-basic-syntax))))
8637 (setq c-syntactic-context placeholder)
8638 (c-beginning-of-statement-1
8639 (c-safe-position (1- containing-sexp) paren-state))
8640 (c-forward-token-2 0)
8641 (while (looking-at c-specifier-key)
8642 (goto-char (match-end 1))
8643 (c-forward-syntactic-ws))
8644 (c-add-syntax 'brace-list-open (c-point 'boi))))
8646 ;; CASE 9B: brace-list-close brace
8647 ((if (consp special-brace-list)
8648 ;; Check special brace list closer.
8650 (goto-char (car (car special-brace-list)))
8652 (goto-char indent-point)
8653 (back-to-indentation)
8655 ;; We were between the special close char and the `)'.
8656 (and (eq (char-after) ?\))
8657 (eq (1+ (point)) (cdr (car special-brace-list))))
8658 ;; We were before the special close char.
8659 (and (eq (char-after) (cdr (cdr special-brace-list)))
8660 (zerop (c-forward-token-2))
8661 (eq (1+ (point)) (cdr (car special-brace-list)))))))
8662 ;; Normal brace list check.
8663 (and (eq char-after-ip ?})
8664 (c-safe (goto-char (c-up-list-backward (point))) t)
8665 (= (point) containing-sexp)))
8666 (if (eq (point) (c-point 'boi))
8667 (c-add-syntax 'brace-list-close (point))
8668 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8669 (c-beginning-of-statement-1 lim)
8670 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
8673 ;; Prepare for the rest of the cases below by going to the
8674 ;; token following the opening brace
8675 (if (consp special-brace-list)
8677 (goto-char (car (car special-brace-list)))
8678 (c-forward-token-2 1 nil indent-point))
8679 (goto-char containing-sexp))
8681 (let ((start (point)))
8682 (c-forward-syntactic-ws indent-point)
8683 (goto-char (max start (c-point 'bol))))
8684 (c-skip-ws-forward indent-point)
8687 ;; CASE 9C: we're looking at the first line in a brace-list
8688 ((= (point) indent-point)
8689 (if (consp special-brace-list)
8690 (goto-char (car (car special-brace-list)))
8691 (goto-char containing-sexp))
8692 (if (eq (point) (c-point 'boi))
8693 (c-add-syntax 'brace-list-intro (point))
8694 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8695 (c-beginning-of-statement-1 lim)
8696 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
8698 ;; CASE 9D: this is just a later brace-list-entry or
8700 (t (if (or (eq char-after-ip ?{)
8701 (and c-special-brace-lists
8703 (goto-char indent-point)
8704 (c-forward-syntactic-ws (c-point 'eol))
8705 (c-looking-at-special-brace-list (point)))))
8706 (c-add-syntax 'brace-entry-open (point))
8707 (c-add-syntax 'brace-list-entry (point))
8711 ;; CASE 10: A continued statement or top level construct.
8712 ((and (not (memq char-before-ip '(?\; ?:)))
8713 (not (c-at-vsemi-p before-ws-ip))
8714 (or (not (eq char-before-ip ?}))
8715 (c-looking-at-inexpr-block-backward c-state-cache))
8718 (c-beginning-of-statement-1 containing-sexp)
8719 (setq placeholder (point))))
8720 (/= placeholder containing-sexp))
8721 ;; This is shared with case 18.
8722 (c-guess-continued-construct indent-point
8728 ;; CASE 16: block close brace, possibly closing the defun or
8730 ((eq char-after-ip ?})
8731 ;; From here on we have the next containing sexp in lim.
8732 (setq lim (c-most-enclosing-brace paren-state))
8733 (goto-char containing-sexp)
8736 ;; CASE 16E: Closing a statement block? This catches
8737 ;; cases where it's preceded by a statement keyword,
8738 ;; which works even when used in an "invalid" context,
8739 ;; e.g. a macro argument.
8740 ((c-after-conditional)
8741 (c-backward-to-block-anchor lim)
8742 (c-add-stmt-syntax 'block-close nil t lim paren-state))
8744 ;; CASE 16A: closing a lambda defun or an in-expression
8745 ;; block? C.f. cases 4, 7B and 17E.
8746 ((setq placeholder (c-looking-at-inexpr-block
8747 (c-safe-position containing-sexp paren-state)
8749 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8752 (goto-char containing-sexp)
8753 (back-to-indentation)
8754 (if (= containing-sexp (point))
8755 (c-add-syntax tmpsymbol (point))
8756 (goto-char (cdr placeholder))
8757 (back-to-indentation)
8758 (c-add-stmt-syntax tmpsymbol nil t
8759 (c-most-enclosing-brace paren-state (point))
8761 (if (/= (point) (cdr placeholder))
8762 (c-add-syntax (car placeholder)))))
8764 ;; CASE 16B: does this close an inline or a function in
8765 ;; a non-class declaration level block?
8770 (c-looking-at-decl-block
8771 (c-most-enclosing-brace paren-state lim)
8773 (setq placeholder (point))))
8774 (c-backward-to-decl-anchor lim)
8775 (back-to-indentation)
8777 (goto-char placeholder)
8778 (looking-at c-other-decl-block-key))
8779 (c-add-syntax 'defun-close (point))
8780 (c-add-syntax 'inline-close (point))))
8782 ;; CASE 16F: Can be a defun-close of a function declared
8783 ;; in a statement block, e.g. in Pike or when using gcc
8784 ;; extensions, but watch out for macros followed by
8785 ;; blocks. Let it through to be handled below.
8786 ;; C.f. cases B.3 and 17G.
8788 (and (not (c-at-statement-start-p))
8789 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8790 (setq placeholder (point))
8791 (let ((c-recognize-typeless-decls nil))
8792 ;; Turn off recognition of constructs that
8793 ;; lacks a type in this case, since that's more
8794 ;; likely to be a macro followed by a block.
8795 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8796 (back-to-indentation)
8797 (if (/= (point) containing-sexp)
8798 (goto-char placeholder))
8799 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
8801 ;; CASE 16C: If there is an enclosing brace then this is
8802 ;; a block close since defun closes inside declaration
8803 ;; level blocks have been handled above.
8805 ;; If the block is preceded by a case/switch label on
8806 ;; the same line, we anchor at the first preceding label
8807 ;; at boi. The default handling in c-add-stmt-syntax
8808 ;; really fixes it better, but we do like this to keep
8809 ;; the indentation compatible with version 5.28 and
8810 ;; earlier. C.f. case 17H.
8811 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8812 (eq (c-beginning-of-statement-1 lim) 'label)))
8813 (goto-char placeholder)
8814 (if (looking-at c-label-kwds-regexp)
8815 (c-add-syntax 'block-close (point))
8816 (goto-char containing-sexp)
8817 ;; c-backward-to-block-anchor not necessary here; those
8818 ;; situations are handled in case 16E above.
8819 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
8821 ;; CASE 16D: Only top level defun close left.
8823 (goto-char containing-sexp)
8824 (c-backward-to-decl-anchor lim)
8825 (c-add-stmt-syntax 'defun-close nil nil
8826 (c-most-enclosing-brace paren-state)
8830 ;; CASE 17: Statement or defun catchall.
8832 (goto-char indent-point)
8833 ;; Back up statements until we find one that starts at boi.
8834 (while (let* ((prev-point (point))
8835 (last-step-type (c-beginning-of-statement-1
8837 (if (= (point) prev-point)
8839 (setq step-type (or step-type last-step-type))
8841 (setq step-type last-step-type)
8842 (/= (point) (c-point 'boi)))))
8845 ;; CASE 17B: continued statement
8846 ((and (eq step-type 'same)
8847 (/= (point) indent-point))
8848 (c-add-stmt-syntax 'statement-cont nil nil
8849 containing-sexp paren-state))
8851 ;; CASE 17A: After a case/default label?
8853 (while (and (eq step-type 'label)
8854 (not (looking-at c-label-kwds-regexp)))
8856 (c-beginning-of-statement-1 containing-sexp)))
8857 (eq step-type 'label))
8858 (c-add-stmt-syntax (if (eq char-after-ip ?{)
8859 'statement-case-open
8860 'statement-case-intro)
8861 nil t containing-sexp paren-state))
8863 ;; CASE 17D: any old statement
8865 (while (eq step-type 'label)
8867 (c-beginning-of-statement-1 containing-sexp)))
8868 (eq step-type 'previous))
8869 (c-add-stmt-syntax 'statement nil t
8870 containing-sexp paren-state)
8871 (if (eq char-after-ip ?{)
8872 (c-add-syntax 'block-open)))
8874 ;; CASE 17I: Inside a substatement block.
8876 ;; The following tests are all based on containing-sexp.
8877 (goto-char containing-sexp)
8878 ;; From here on we have the next containing sexp in lim.
8879 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
8880 (c-after-conditional))
8881 (c-backward-to-block-anchor lim)
8882 (c-add-stmt-syntax 'statement-block-intro nil t
8884 (if (eq char-after-ip ?{)
8885 (c-add-syntax 'block-open)))
8887 ;; CASE 17E: first statement in an in-expression block.
8888 ;; C.f. cases 4, 7B and 16A.
8889 ((setq placeholder (c-looking-at-inexpr-block
8890 (c-safe-position containing-sexp paren-state)
8892 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8894 'statement-block-intro))
8895 (back-to-indentation)
8896 (if (= containing-sexp (point))
8897 (c-add-syntax tmpsymbol (point))
8898 (goto-char (cdr placeholder))
8899 (back-to-indentation)
8900 (c-add-stmt-syntax tmpsymbol nil t
8901 (c-most-enclosing-brace c-state-cache (point))
8903 (if (/= (point) (cdr placeholder))
8904 (c-add-syntax (car placeholder))))
8905 (if (eq char-after-ip ?{)
8906 (c-add-syntax 'block-open)))
8908 ;; CASE 17F: first statement in an inline, or first
8909 ;; statement in a top-level defun. we can tell this is it
8910 ;; if there are no enclosing braces that haven't been
8911 ;; narrowed out by a class (i.e. don't use bod here).
8913 (or (not (setq placeholder (c-most-enclosing-brace
8916 (goto-char placeholder)
8917 (eq (char-after) ?{))
8918 (c-looking-at-decl-block (c-most-enclosing-brace
8919 paren-state (point))
8921 (c-backward-to-decl-anchor lim)
8922 (back-to-indentation)
8923 (c-add-syntax 'defun-block-intro (point)))
8925 ;; CASE 17G: First statement in a function declared inside
8926 ;; a normal block. This can occur in Pike and with
8927 ;; e.g. the gcc extensions, but watch out for macros
8928 ;; followed by blocks. C.f. cases B.3 and 16F.
8930 (and (not (c-at-statement-start-p))
8931 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8932 (setq placeholder (point))
8933 (let ((c-recognize-typeless-decls nil))
8934 ;; Turn off recognition of constructs that lacks
8935 ;; a type in this case, since that's more likely
8936 ;; to be a macro followed by a block.
8937 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8938 (back-to-indentation)
8939 (if (/= (point) containing-sexp)
8940 (goto-char placeholder))
8941 (c-add-stmt-syntax 'defun-block-intro nil t
8944 ;; CASE 17H: First statement in a block.
8946 ;; If the block is preceded by a case/switch label on the
8947 ;; same line, we anchor at the first preceding label at
8948 ;; boi. The default handling in c-add-stmt-syntax is
8949 ;; really fixes it better, but we do like this to keep the
8950 ;; indentation compatible with version 5.28 and earlier.
8952 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8953 (eq (c-beginning-of-statement-1 lim) 'label)))
8954 (goto-char placeholder)
8955 (if (looking-at c-label-kwds-regexp)
8956 (c-add-syntax 'statement-block-intro (point))
8957 (goto-char containing-sexp)
8958 ;; c-backward-to-block-anchor not necessary here; those
8959 ;; situations are handled in case 17I above.
8960 (c-add-stmt-syntax 'statement-block-intro nil t
8962 (if (eq char-after-ip ?{)
8963 (c-add-syntax 'block-open)))
8967 ;; now we need to look at any modifiers
8968 (goto-char indent-point)
8969 (skip-chars-forward " \t")
8971 ;; are we looking at a comment only line?
8972 (when (and (looking-at c-comment-start-regexp)
8973 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
8974 (c-append-syntax 'comment-intro))
8976 ;; we might want to give additional offset to friends (in C++).
8977 (when (and c-opt-friend-key
8978 (looking-at c-opt-friend-key))
8979 (c-append-syntax 'friend))
8981 ;; Set syntactic-relpos.
8982 (let ((p c-syntactic-context))
8984 (if (integerp (c-langelem-pos (car p)))
8986 (setq syntactic-relpos (c-langelem-pos (car p)))
8991 ;; Start of or a continuation of a preprocessor directive?
8992 (if (and macro-start
8993 (eq macro-start (c-point 'boi))
8994 (not (and (c-major-mode-is 'pike-mode)
8995 (eq (char-after (1+ macro-start)) ?\"))))
8996 (c-append-syntax 'cpp-macro)
8997 (when (and c-syntactic-indentation-in-macros macro-start)
9000 (< syntactic-relpos macro-start)
9002 (assq 'arglist-intro c-syntactic-context)
9003 (assq 'arglist-cont c-syntactic-context)
9004 (assq 'arglist-cont-nonempty c-syntactic-context)
9005 (assq 'arglist-close c-syntactic-context))))
9006 ;; If inside a cpp expression, i.e. anywhere in a
9007 ;; cpp directive except a #define body, we only let
9008 ;; through the syntactic analysis that is internal
9009 ;; in the expression. That means the arglist
9010 ;; elements, if they are anchored inside the cpp
9012 (setq c-syntactic-context nil)
9013 (c-add-syntax 'cpp-macro-cont macro-start))
9014 (when (and (eq macro-start syntactic-relpos)
9015 (not (assq 'cpp-define-intro c-syntactic-context))
9017 (goto-char macro-start)
9018 (or (not (c-forward-to-cpp-define-body))
9019 (<= (point) (c-point 'boi indent-point)))))
9020 ;; Inside a #define body and the syntactic analysis is
9021 ;; anchored on the start of the #define. In this case
9022 ;; we add cpp-define-intro to get the extra
9023 ;; indentation of the #define body.
9024 (c-add-syntax 'cpp-define-intro)))))
9026 ;; return the syntax
9027 c-syntactic-context)))
9030 ;; Indentation calculation.
9032 (defun c-evaluate-offset (offset langelem symbol)
9033 ;; offset can be a number, a function, a variable, a list, or one of
9034 ;; the symbols + or -
9036 ;; This function might do hidden buffer changes.
9039 ((numberp offset) offset)
9040 ((vectorp offset) offset)
9043 ((eq offset '+) c-basic-offset)
9044 ((eq offset '-) (- c-basic-offset))
9045 ((eq offset '++) (* 2 c-basic-offset))
9046 ((eq offset '--) (* 2 (- c-basic-offset)))
9047 ((eq offset '*) (/ c-basic-offset 2))
9048 ((eq offset '/) (/ (- c-basic-offset) 2))
9053 (cons (c-langelem-sym langelem)
9054 (c-langelem-pos langelem)))
9059 ((eq (car offset) 'quote)
9060 (c-benign-error "The offset %S for %s was mistakenly quoted"
9064 ((memq (car offset) '(min max))
9065 (let (res val (method (car offset)))
9066 (setq offset (cdr offset))
9068 (setq val (c-evaluate-offset (car offset) langelem symbol))
9076 Error evaluating offset %S for %s: \
9077 Cannot combine absolute offset %S with relative %S in `%s' method"
9078 (car offset) symbol res val method)
9079 (setq res (funcall method res val))))
9083 Error evaluating offset %S for %s: \
9084 Cannot combine relative offset %S with absolute %S in `%s' method"
9085 (car offset) symbol res val method)
9086 (setq res (vector (funcall method (aref res 0)
9088 (setq offset (cdr offset)))
9091 ((eq (car offset) 'add)
9093 (setq offset (cdr offset))
9095 (setq val (c-evaluate-offset (car offset) langelem symbol))
9102 (setq res (vector (+ (aref res 0) val)))
9103 (setq res (+ res val))))
9107 Error evaluating offset %S for %s: \
9108 Cannot combine absolute offsets %S and %S in `add' method"
9109 (car offset) symbol res val)
9110 (setq res val)))) ; Override.
9111 (setq offset (cdr offset)))
9116 (when (eq (car offset) 'first)
9117 (setq offset (cdr offset)))
9118 (while (and (not res) offset)
9119 (setq res (c-evaluate-offset (car offset) langelem symbol)
9120 offset (cdr offset)))
9123 ((and (symbolp offset) (boundp offset))
9124 (symbol-value offset))
9127 (c-benign-error "Unknown offset format %S for %s" offset symbol)
9130 (if (or (null res) (integerp res)
9131 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
9133 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
9137 (defun c-calc-offset (langelem)
9138 ;; Get offset from LANGELEM which is a list beginning with the
9139 ;; syntactic symbol and followed by any analysis data it provides.
9140 ;; That data may be zero or more elements, but if at least one is
9141 ;; given then the first is the anchor position (or nil). The symbol
9142 ;; is matched against `c-offsets-alist' and the offset calculated
9143 ;; from that is returned.
9145 ;; This function might do hidden buffer changes.
9146 (let* ((symbol (c-langelem-sym langelem))
9147 (match (assq symbol c-offsets-alist))
9148 (offset (cdr-safe match)))
9150 (setq offset (c-evaluate-offset offset langelem symbol))
9151 (if c-strict-syntax-p
9152 (c-benign-error "No offset found for syntactic symbol %s" symbol))
9154 (if (vectorp offset)
9156 (or (and (numberp offset) offset)
9157 (and (symbolp offset) (symbol-value offset))
9161 (defun c-get-offset (langelem)
9162 ;; This is a compatibility wrapper for `c-calc-offset' in case
9163 ;; someone is calling it directly. It takes an old style syntactic
9164 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
9167 ;; This function might do hidden buffer changes.
9168 (if (c-langelem-pos langelem)
9169 (c-calc-offset (list (c-langelem-sym langelem)
9170 (c-langelem-pos langelem)))
9171 (c-calc-offset langelem)))
9173 (defun c-get-syntactic-indentation (langelems)
9174 ;; Calculate the syntactic indentation from a syntactic description
9175 ;; as returned by `c-guess-syntax'.
9177 ;; Note that topmost-intro always has an anchor position at bol, for
9178 ;; historical reasons. It's often used together with other symbols
9179 ;; that has more sane positions. Since we always use the first
9180 ;; found anchor position, we rely on that these other symbols always
9181 ;; precede topmost-intro in the LANGELEMS list.
9183 ;; This function might do hidden buffer changes.
9184 (let ((indent 0) anchor)
9187 (let* ((c-syntactic-element (car langelems))
9188 (res (c-calc-offset c-syntactic-element)))
9191 ;; Got an absolute column that overrides any indentation
9192 ;; we've collected so far, but not the relative
9193 ;; indentation we might get for the nested structures
9194 ;; further down the langelems list.
9195 (setq indent (elt res 0)
9196 anchor (point-min)) ; A position at column 0.
9198 ;; Got a relative change of the current calculated
9200 (setq indent (+ indent res))
9202 ;; Use the anchor position from the first syntactic
9203 ;; element with one.
9205 (setq anchor (c-langelem-pos (car langelems)))))
9207 (setq langelems (cdr langelems))))
9210 (+ indent (save-excursion
9216 (cc-provide 'cc-engine)
9218 ;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
9219 ;;; cc-engine.el ends here