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, 2009,
5 ;; 2010 Free Software Foundation, Inc.
7 ;; Authors: 2001- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
11 ;; 1987 Stewart Clamen
12 ;; 1985 Richard M. Stallman
13 ;; Maintainer: bug-cc-mode@gnu.org
14 ;; Created: 22-Apr-1997 (split from cc-mode.el)
15 ;; Keywords: c languages
18 ;; This file is part of GNU Emacs.
20 ;; GNU Emacs is free software: you can redistribute it and/or modify
21 ;; it under the terms of the GNU General Public License as published by
22 ;; the Free Software Foundation, either version 3 of the License, or
23 ;; (at your option) any later version.
25 ;; GNU Emacs is distributed in the hope that it will be useful,
26 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;; GNU General Public License for more details.
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
35 ;; The functions which have docstring documentation can be considered
36 ;; part of an API which other packages can use in CC Mode buffers.
37 ;; Otoh, undocumented functions and functions with the documentation
38 ;; in comments are considered purely internal and can change semantics
39 ;; or even disappear in the future.
41 ;; (This policy applies to CC Mode as a whole, not just this file. It
42 ;; probably also applies to many other Emacs packages, but here it's
43 ;; clearly spelled out.)
45 ;; Hidden buffer changes
47 ;; Various functions in CC Mode use text properties for caching and
48 ;; syntactic markup purposes, and those of them that might modify such
49 ;; properties but still don't modify the buffer in a visible way are
50 ;; said to do "hidden buffer changes". They should be used within
51 ;; `c-save-buffer-state' or a similar function that saves and restores
52 ;; buffer modifiedness, disables buffer change hooks, etc.
54 ;; Interactive functions are assumed to not do hidden buffer changes,
55 ;; except in the specific parts of them that do real changes.
57 ;; Lineup functions are assumed to do hidden buffer changes. They
58 ;; must not do real changes, though.
60 ;; All other functions that do hidden buffer changes have that noted
61 ;; in their doc string or comment.
63 ;; The intention with this system is to avoid wrapping every leaf
64 ;; function that do hidden buffer changes inside
65 ;; `c-save-buffer-state'. It should be used as near the top of the
66 ;; interactive functions as possible.
68 ;; Functions called during font locking are allowed to do hidden
69 ;; buffer changes since the font-lock package run them in a context
70 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
71 ;; inspired by `save-buffer-state' in the font-lock package).
73 ;; Use of text properties
75 ;; CC Mode uses several text properties internally to mark up various
76 ;; positions, e.g. to improve speed and to eliminate glitches in
77 ;; interactive refontification.
79 ;; Note: This doc is for internal use only. Other packages should not
80 ;; assume that these text properties are used as described here.
83 ;; Used for "indirection". With its help, some other property can
84 ;; be cheaply and easily switched on or off everywhere it occurs.
87 ;; Used to modify the syntax of some characters. It is used to
88 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
89 ;; to "hide" obtrusive characters in preprocessor lines.
91 ;; This property is used on single characters and is therefore
92 ;; always treated as front and rear nonsticky (or start and end open
93 ;; in XEmacs vocabulary). It's therefore installed on
94 ;; `text-property-default-nonsticky' if that variable exists (Emacs
97 ;; 'c-is-sws and 'c-in-sws
98 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
99 ;; speed them up. See the comment blurb before `c-put-is-sws'
100 ;; below for further details.
103 ;; This property is used on single characters to mark positions with
104 ;; special syntactic relevance of various sorts. Its primary use is
105 ;; to avoid glitches when multiline constructs are refontified
106 ;; interactively (on font lock decoration level 3). It's cleared in
107 ;; a region before it's fontified and is then put on relevant chars
108 ;; in that region as they are encountered during the fontification.
109 ;; The value specifies the kind of position:
112 ;; Put on the last char of the token preceding each declaration
113 ;; inside a declaration style arglist (typically in a function
117 ;; Put on the last char of the token preceding a declaration.
118 ;; This is used in cases where declaration boundaries can't be
119 ;; recognized simply by looking for a token like ";" or "}".
120 ;; `c-type-decl-end-used' must be set if this is used (see also
121 ;; `c-find-decl-spots').
124 ;; Put on the commas that separate arguments in angle bracket
125 ;; arglists like C++ template arglists.
127 ;; 'c-decl-id-start and 'c-decl-type-start
128 ;; Put on the last char of the token preceding each declarator
129 ;; in the declarator list of a declaration. They are also used
130 ;; between the identifiers cases like enum declarations.
131 ;; 'c-decl-type-start is used when the declarators are types,
132 ;; 'c-decl-id-start otherwise.
135 ;; Used in AWK mode to mark the various kinds of newlines. See
142 (if (and (boundp 'byte-compile-dest-file
)
143 (stringp byte-compile-dest-file
))
144 (cons (file-name-directory byte-compile-dest-file
) load-path
)
146 (load "cc-bytecomp" nil t
)))
148 (cc-require 'cc-defs
)
149 (cc-require-when-compile 'cc-langs
)
150 (cc-require 'cc-vars
)
152 ;; Silence the compiler.
153 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
156 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
158 (defmacro c-declare-lang-variables
()
161 (mapcar (lambda (init)
163 `(defvar ,(car init
) nil
,(elt init
2))
164 `(defvar ,(car init
) nil
))
165 (make-variable-buffer-local ',(car init
))))
166 (cdr c-lang-variable-inits
)))))
167 (c-declare-lang-variables)
170 ;;; Internal state variables.
172 ;; Internal state of hungry delete key feature
173 (defvar c-hungry-delete-key nil
)
174 (make-variable-buffer-local 'c-hungry-delete-key
)
176 ;; The electric flag (toggled by `c-toggle-electric-state').
177 ;; If t, electric actions (like automatic reindentation, and (if
178 ;; c-auto-newline is also set) auto newlining) will happen when an electric
179 ;; key like `{' is pressed (or an electric keyword like `else').
180 (defvar c-electric-flag t
)
181 (make-variable-buffer-local 'c-electric-flag
)
183 ;; Internal state of auto newline feature.
184 (defvar c-auto-newline nil
)
185 (make-variable-buffer-local 'c-auto-newline
)
187 ;; Included in the mode line to indicate the active submodes.
188 ;; (defvar c-submode-indicators nil)
189 ;; (make-variable-buffer-local 'c-submode-indicators)
191 (defun c-calculate-state (arg prevstate
)
192 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
193 ;; arg is nil or zero, toggle the state. If arg is negative, turn
194 ;; the state off, and if arg is positive, turn the state on
196 (zerop (setq arg
(prefix-numeric-value arg
))))
200 ;; Dynamically bound cache for `c-in-literal'.
201 (defvar c-in-literal-cache t
)
204 ;; Basic handling of preprocessor directives.
206 ;; This is a dynamically bound cache used together with
207 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
208 ;; works as long as point doesn't cross a macro boundary.
209 (defvar c-macro-start
'unknown
)
211 (defsubst c-query-and-set-macro-start
()
212 (if (symbolp c-macro-start
)
213 (setq c-macro-start
(save-excursion
214 (c-save-buffer-state ()
215 (and (c-beginning-of-macro)
219 (defsubst c-query-macro-start
()
220 (if (symbolp c-macro-start
)
222 (c-save-buffer-state ()
223 (and (c-beginning-of-macro)
227 (defun c-beginning-of-macro (&optional lim
)
228 "Go to the beginning of a preprocessor directive.
229 Leave point at the beginning of the directive and return t if in one,
230 otherwise return nil and leave point unchanged.
232 Note that this function might do hidden buffer changes. See the
233 comment at the start of cc-engine.el for more info."
234 (when c-opt-cpp-prefix
235 (let ((here (point)))
237 (if lim
(narrow-to-region lim
(point-max)))
239 (while (eq (char-before (1- (point))) ?
\\)
241 (back-to-indentation)
242 (if (and (<= (point) here
)
243 (looking-at c-opt-cpp-start
))
248 (defun c-end-of-macro ()
249 "Go to the end of a preprocessor directive.
250 More accurately, move the point to the end of the closest following
251 line that doesn't end with a line continuation backslash - no check is
252 done that the point is inside a cpp directive to begin with.
254 Note that this function might do hidden buffer changes. See the
255 comment at the start of cc-engine.el for more info."
258 (when (and (eq (char-before) ?
\\)
263 (defun c-syntactic-end-of-macro ()
264 ;; Go to the end of a CPP directive, or a "safe" pos just before.
266 ;; This is normally the end of the next non-escaped line. A "safe"
267 ;; position is one not within a string or comment. (The EOL on a line
268 ;; comment is NOT "safe").
270 ;; This function must only be called from the beginning of a CPP construct.
272 ;; Note that this function might do hidden buffer changes. See the comment
273 ;; at the start of cc-engine.el for more info.
274 (let* ((here (point))
275 (there (progn (c-end-of-macro) (point)))
276 (s (parse-partial-sexp here there
)))
277 (while (and (or (nth 3 s
) ; in a string
278 (nth 4 s
)) ; in a comment (maybe at end of line comment)
279 (> there here
)) ; No infinite loops, please.
280 (setq there
(1- (nth 8 s
)))
281 (setq s
(parse-partial-sexp here there
)))
284 (defun c-forward-over-cpp-define-id ()
285 ;; Assuming point is at the "#" that introduces a preprocessor
286 ;; directive, it's moved forward to the end of the identifier which is
287 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
288 ;; is returned in this case, in all other cases nil is returned and
289 ;; point isn't moved.
291 ;; This function might do hidden buffer changes.
292 (when (and c-opt-cpp-macro-define-id
293 (looking-at c-opt-cpp-macro-define-id
))
294 (goto-char (match-end 0))))
296 (defun c-forward-to-cpp-define-body ()
297 ;; Assuming point is at the "#" that introduces a preprocessor
298 ;; directive, it's moved forward to the start of the definition body
299 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
300 ;; specifies). Non-nil is returned in this case, in all other cases
301 ;; nil is returned and point isn't moved.
303 ;; This function might do hidden buffer changes.
304 (when (and c-opt-cpp-macro-define-start
305 (looking-at c-opt-cpp-macro-define-start
)
306 (not (= (match-end 0) (c-point 'eol
))))
307 (goto-char (match-end 0))))
310 ;;; Basic utility functions.
312 (defun c-syntactic-content (from to paren-level
)
313 ;; Return the given region as a string where all syntactic
314 ;; whitespace is removed or, where necessary, replaced with a single
315 ;; space. If PAREN-LEVEL is given then all parens in the region are
316 ;; collapsed to "()", "[]" etc.
318 ;; This function might do hidden buffer changes.
322 (narrow-to-region from to
)
324 (let* ((parts (list nil
)) (tail parts
) pos in-paren
)
326 (while (re-search-forward c-syntactic-ws-start to t
)
327 (goto-char (setq pos
(match-beginning 0)))
328 (c-forward-syntactic-ws)
334 (setq in-paren
(= (car (parse-partial-sexp from pos
1)) 1)
337 (if (and (> pos from
)
339 (looking-at "\\w\\|\\s_")
342 (looking-at "\\w\\|\\s_")))
344 (setcdr tail
(list (buffer-substring-no-properties from pos
)
346 (setq tail
(cddr tail
)))
347 (setcdr tail
(list (buffer-substring-no-properties from pos
)))
348 (setq tail
(cdr tail
)))
351 (when (= (car (parse-partial-sexp pos to -
1)) -
1)
352 (setcdr tail
(list (buffer-substring-no-properties
353 (1- (point)) (point))))
354 (setq tail
(cdr tail
))))
356 (setq from
(point))))
358 (setcdr tail
(list (buffer-substring-no-properties from to
)))
359 (apply 'concat
(cdr parts
))))))
361 (defun c-shift-line-indentation (shift-amt)
362 ;; Shift the indentation of the current line with the specified
363 ;; amount (positive inwards). The buffer is modified only if
364 ;; SHIFT-AMT isn't equal to zero.
365 (let ((pos (- (point-max) (point)))
366 (c-macro-start c-macro-start
)
368 (if (zerop shift-amt
)
370 ;; If we're on an empty line inside a macro, we take the point
371 ;; to be at the current indentation and shift it to the
372 ;; appropriate column. This way we don't treat the extra
373 ;; whitespace out to the line continuation as indentation.
374 (when (and (c-query-and-set-macro-start)
375 (looking-at "[ \t]*\\\\$")
377 (skip-chars-backward " \t")
381 (setq tmp-char-inserted t
))
383 (let ((col (current-indentation)))
384 (delete-region (c-point 'bol
) (c-point 'boi
))
386 (indent-to (+ col shift-amt
)))
387 (when tmp-char-inserted
389 ;; If initial point was within line's indentation and we're not on
390 ;; a line with a line continuation in a macro, position after the
391 ;; indentation. Else stay at same point in text.
392 (if (and (< (point) (c-point 'boi
))
393 (not tmp-char-inserted
))
394 (back-to-indentation)
395 (if (> (- (point-max) pos
) (point))
396 (goto-char (- (point-max) pos
))))))
398 (defsubst c-keyword-sym
(keyword)
399 ;; Return non-nil if the string KEYWORD is a known keyword. More
400 ;; precisely, the value is the symbol for the keyword in
401 ;; `c-keywords-obarray'.
402 (intern-soft keyword c-keywords-obarray
))
404 (defsubst c-keyword-member
(keyword-sym lang-constant
)
405 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
406 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
407 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
408 ;; nil then the result is nil.
409 (get keyword-sym lang-constant
))
411 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
412 (defconst c-string-syntax
(if (memq 'gen-string-delim c-emacs-features
)
416 ;; Regexp matching string limit syntax.
417 (defconst c-string-limit-regexp
(if (memq 'gen-string-delim c-emacs-features
)
421 ;; Regexp matching WS followed by string limit syntax.
422 (defconst c-ws
*-string-limit-regexp
423 (concat "[ \t]*\\(" c-string-limit-regexp
"\\)"))
425 ;; Holds formatted error strings for the few cases where parse errors
427 (defvar c-parsing-error nil
)
428 (make-variable-buffer-local 'c-parsing-error
)
430 (defun c-echo-parsing-error (&optional quiet
)
431 (when (and c-report-syntactic-errors c-parsing-error
(not quiet
))
432 (c-benign-error "%s" c-parsing-error
))
435 ;; Faces given to comments and string literals. This is used in some
436 ;; situations to speed up recognition; it isn't mandatory that font
437 ;; locking is in use. This variable is extended with the face in
438 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
439 (defvar c-literal-faces
440 (append '(font-lock-comment-face font-lock-string-face
)
441 (when (facep 'font-lock-comment-delimiter-face
)
443 '(font-lock-comment-delimiter-face))))
445 (defsubst c-put-c-type-property
(pos value
)
446 ;; Put a c-type property with the given value at POS.
447 (c-put-char-property pos
'c-type value
))
449 (defun c-clear-c-type-property (from to value
)
450 ;; Remove all occurrences of the c-type property that has the given
451 ;; value in the region between FROM and TO. VALUE is assumed to not
454 ;; Note: This assumes that c-type is put on single chars only; it's
455 ;; very inefficient if matching properties cover large regions.
459 (when (eq (get-text-property (point) 'c-type
) value
)
460 (c-clear-char-property (point) 'c-type
))
461 (goto-char (next-single-property-change (point) 'c-type nil to
))
465 ;; Some debug tools to visualize various special positions. This
466 ;; debug code isn't as portable as the rest of CC Mode.
468 (cc-bytecomp-defun overlays-in)
469 (cc-bytecomp-defun overlay-get)
470 (cc-bytecomp-defun overlay-start)
471 (cc-bytecomp-defun overlay-end)
472 (cc-bytecomp-defun delete-overlay)
473 (cc-bytecomp-defun overlay-put)
474 (cc-bytecomp-defun make-overlay)
476 (defun c-debug-add-face (beg end face
)
477 (c-save-buffer-state ((overlays (overlays-in beg end
)) overlay
)
479 (setq overlay
(car overlays
)
480 overlays
(cdr overlays
))
481 (when (eq (overlay-get overlay
'face
) face
)
482 (setq beg
(min beg
(overlay-start overlay
))
483 end
(max end
(overlay-end overlay
)))
484 (delete-overlay overlay
)))
485 (overlay-put (make-overlay beg end
) 'face face
)))
487 (defun c-debug-remove-face (beg end face
)
488 (c-save-buffer-state ((overlays (overlays-in beg end
)) overlay
489 (ol-beg beg
) (ol-end end
))
491 (setq overlay
(car overlays
)
492 overlays
(cdr overlays
))
493 (when (eq (overlay-get overlay
'face
) face
)
494 (setq ol-beg
(min ol-beg
(overlay-start overlay
))
495 ol-end
(max ol-end
(overlay-end overlay
)))
496 (delete-overlay overlay
)))
498 (overlay-put (make-overlay ol-beg beg
) 'face face
))
500 (overlay-put (make-overlay end ol-end
) 'face face
))))
503 ;; `c-beginning-of-statement-1' and accompanying stuff.
505 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
506 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
507 ;; better way should be implemented, but this will at least shut up
508 ;; the byte compiler.
509 (defvar c-maybe-labelp
)
511 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
513 ;; Macros used internally in c-beginning-of-statement-1 for the
514 ;; automaton actions.
515 (defmacro c-bos-push-state
()
516 '(setq stack
(cons (cons state saved-pos
)
518 (defmacro c-bos-pop-state
(&optional do-if-done
)
519 `(if (setq state
(car (car stack
))
520 saved-pos
(cdr (car stack
))
525 (defmacro c-bos-pop-state-and-retry
()
526 '(throw 'loop
(setq state
(car (car stack
))
527 saved-pos
(cdr (car stack
))
528 ;; Throw nil if stack is empty, else throw non-nil.
530 (defmacro c-bos-save-pos
()
531 '(setq saved-pos
(vector pos tok ptok pptok
)))
532 (defmacro c-bos-restore-pos
()
533 '(unless (eq (elt saved-pos
0) start
)
534 (setq pos
(elt saved-pos
0)
535 tok
(elt saved-pos
1)
536 ptok
(elt saved-pos
2)
537 pptok
(elt saved-pos
3))
540 (defmacro c-bos-save-error-info
(missing got
)
541 `(setq saved-pos
(vector pos
,missing
,got
)))
542 (defmacro c-bos-report-error
()
544 (setq c-parsing-error
545 (format "No matching `%s' found for `%s' on line %d"
548 (1+ (count-lines (point-min)
549 (c-point 'bol
(elt saved-pos
0))))))))
551 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
553 "Move to the start of the current statement or declaration, or to
554 the previous one if already at the beginning of one. Only
555 statements/declarations on the same level are considered, i.e. don't
556 move into or out of sexps (not even normal expression parentheses).
558 If point is already at the earliest statement within braces or parens,
559 this function doesn't move back into any whitespace preceding it; it
560 returns 'same in this case.
562 Stop at statement continuation tokens like \"else\", \"catch\",
563 \"finally\" and the \"while\" in \"do ... while\" if the start point
564 is within the continuation. If starting at such a token, move to the
565 corresponding statement start. If at the beginning of a statement,
566 move to the closest containing statement if there is any. This might
567 also stop at a continuation clause.
569 Labels are treated as part of the following statements if
570 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
571 statement start keyword.) Otherwise, each label is treated as a
574 Macros are ignored \(i.e. skipped over) unless point is within one, in
575 which case the content of the macro is treated as normal code. Aside
576 from any normal statement starts found in it, stop at the first token
577 of the content in the macro, i.e. the expression of an \"#if\" or the
578 start of the definition in a \"#define\". Also stop at start of
579 macros before leaving them.
582 'label if stopped at a label or \"case...:\" or \"default:\";
583 'same if stopped at the beginning of the current statement;
584 'up if stepped to a containing statement;
585 'previous if stepped to a preceding statement;
586 'beginning if stepped from a statement continuation clause to
588 'macro if stepped to a macro start.
589 Note that 'same and not 'label is returned if stopped at the same
590 label without crossing the colon character.
592 LIM may be given to limit the search. If the search hits the limit,
593 point will be left at the closest following token, or at the start
594 position if that is less ('same is returned in this case).
596 NOERROR turns off error logging to `c-parsing-error'.
598 Normally only ';' and virtual semicolons are considered to delimit
599 statements, but if COMMA-DELIM is non-nil then ',' is treated
602 Note that this function might do hidden buffer changes. See the
603 comment at the start of cc-engine.el for more info."
605 ;; The bulk of this function is a pushdown automaton that looks at statement
606 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
607 ;; purpose is to keep track of nested statements, ensuring that such
608 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
609 ;; does with nested braces/brackets/parentheses).
611 ;; Note: The position of a boundary is the following token.
613 ;; Beginning with the current token (the one following point), move back one
614 ;; sexp at a time (where a sexp is, more or less, either a token or the
615 ;; entire contents of a brace/bracket/paren pair). Each time a statement
616 ;; boundary is crossed or a "while"-like token is found, update the state of
617 ;; the PDA. Stop at the beginning of a statement when the stack (holding
618 ;; nested statement info) is empty and the position has been moved.
620 ;; The following variables constitute the PDA:
622 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
623 ;; scanned back over, 'boundary if we've just gone back over a
624 ;; statement boundary, or nil otherwise.
625 ;; state: takes one of the values (nil else else-boundary while
626 ;; while-boundary catch catch-boundary).
627 ;; nil means "no "while"-like token yet scanned".
628 ;; 'else, for example, means "just gone back over an else".
629 ;; 'else-boundary means "just gone back over a statement boundary
630 ;; immediately after having gone back over an else".
631 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
632 ;; of error reporting information.
633 ;; stack: The stack onto which the PDA pushes its state. Each entry
634 ;; consists of a saved value of state and saved-pos. An entry is
635 ;; pushed when we move back over a "continuation" token (e.g. else)
636 ;; and popped when we encounter the corresponding opening token
640 ;; The following diagram briefly outlines the PDA.
643 ;; "else": Push state, goto state `else'.
644 ;; "while": Push state, goto state `while'.
645 ;; "catch" or "finally": Push state, goto state `catch'.
646 ;; boundary: Pop state.
647 ;; other: Do nothing special.
650 ;; boundary: Goto state `else-boundary'.
651 ;; other: Error, pop state, retry token.
653 ;; State `else-boundary':
655 ;; boundary: Error, pop state.
656 ;; other: See common state.
659 ;; boundary: Save position, goto state `while-boundary'.
660 ;; other: Pop state, retry token.
662 ;; State `while-boundary':
664 ;; boundary: Restore position if it's not at start, pop state. [*see below]
665 ;; other: See common state.
668 ;; boundary: Goto state `catch-boundary'.
669 ;; other: Error, pop state, retry token.
671 ;; State `catch-boundary':
673 ;; "catch": Goto state `catch'.
674 ;; boundary: Error, pop state.
675 ;; other: See common state.
677 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
678 ;; searching for a "do" which would have opened a do-while. If we didn't
679 ;; find it, we discard the analysis done since the "while", go back to this
680 ;; token in the buffer and restart the scanning there, this time WITHOUT
681 ;; pushing the 'while state onto the stack.
683 ;; In addition to the above there is some special handling of labels
686 (let ((case-fold-search nil
)
689 (delims (if comma-delim
'(?\
; ?,) '(?\;)))
690 (c-stmt-delim-chars (if comma-delim
691 c-stmt-delim-chars-with-comma
693 c-in-literal-cache c-maybe-labelp after-case
:-pos saved
696 ;; Position of last stmt boundary character (e.g. ;).
698 ;; The position of the last sexp or bound that follows the
699 ;; first found colon, i.e. the start of the nonlabel part of
700 ;; the statement. It's `start' if a colon is found just after
703 ;; Like `after-labels-pos', but the first such position inside
704 ;; a label, i.e. the start of the last label before the start
705 ;; of the nonlabel part of the statement.
707 ;; The last position where a label is possible provided the
708 ;; statement started there. It's nil as long as no invalid
709 ;; label content has been found (according to
710 ;; `c-nonlabel-token-key'. It's `start' if no valid label
711 ;; content was found in the label. Note that we might still
712 ;; regard it a label if it starts with `c-label-kwds'.
714 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
717 ;; Current state in the automaton. See above.
719 ;; Current saved positions. See above.
721 ;; Stack of conses (state . saved-pos).
723 ;; Regexp which matches "for", "if", etc.
724 (cond-key (or c-opt-block-stmt-key
725 "\\<\\>")) ; Matches nothing.
728 ;; Positions of the last three sexps or bounds we've stopped at.
732 (if lim
(narrow-to-region lim
(point-max)))
735 (and (c-beginning-of-macro)
737 (setq macro-start
(point)))
739 ;; Try to skip back over unary operator characters, to register
743 (c-backward-syntactic-ws)
744 ;; Protect post-++/-- operators just before a virtual semicolon.
745 (and (not (c-at-vsemi-p))
746 (/= (skip-chars-backward "-+!*&~@`#") 0))))
748 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
749 ;; done. Later on we ignore the boundaries for statements that don't
750 ;; contain any sexp. The only thing that is affected is that the error
751 ;; checking is a little less strict, and we really don't bother.
752 (if (and (memq (char-before) delims
)
753 (progn (forward-char -
1)
755 (c-backward-syntactic-ws)
756 (or (memq (char-before) delims
)
757 (memq (char-before) '(?
: nil
))
758 (eq (char-syntax (char-before)) ?\
()
763 ;; Begin at start and not pos to detect macros if we stand
764 ;; directly after the #.
766 (if (looking-at "\\<\\|\\W")
767 ;; Record this as the first token if not starting inside it.
770 ;; The following while loop goes back one sexp (balanced parens,
771 ;; etc. with contents, or symbol or suchlike) each iteration. This
772 ;; movement is accomplished with a call to scan-sexps approx 130 lines
775 (catch 'loop
;; Throw nil to break, non-nil to continue.
778 (and macro-start
; Always NIL for AWK.
779 (progn (skip-chars-backward " \t")
780 (eq (char-before) ?
#))
781 (progn (setq saved
(1- (point)))
783 (not (eq (char-before (1- (point))) ?
\\)))
784 (looking-at c-opt-cpp-start
)
785 (progn (skip-chars-forward " \t")
786 (eq (point) saved
))))
788 (if (and (c-forward-to-cpp-define-body)
789 (progn (c-forward-syntactic-ws start
)
791 ;; Stop at the first token in the content of the macro.
793 ignore-labels t
) ; Avoid the label check on exit.
799 ;; Do a round through the automaton if we've just passed a
800 ;; statement boundary or passed a "while"-like token.
802 (and (looking-at cond-key
)
803 (setq sym
(intern (match-string 1)))))
805 (when (and (< pos start
) (null stack
))
808 ;; The PDA state handling.
810 ;; Refer to the description of the PDA in the opening
811 ;; comments. In the following OR form, the first leaf
812 ;; attempts to handles one of the specific actions detailed
813 ;; (e.g., finding token "if" whilst in state `else-boundary').
814 ;; We drop through to the second leaf (which handles common
815 ;; state) if no specific handler is found in the first cond.
816 ;; If a parsing error is detected (e.g. an "else" with no
817 ;; preceding "if"), we throw to the enclosing catch.
819 ;; Note that the (eq state 'else) means
820 ;; "we've just passed an else", NOT "we're looking for an
824 (if (eq sym
'boundary
)
825 (setq state
'else-boundary
)
827 (c-bos-pop-state-and-retry)))
829 ((eq state
'else-boundary
)
831 (c-bos-pop-state (setq ret
'beginning
)))
837 (if (and (eq sym
'boundary
)
838 ;; Since this can cause backtracking we do a
839 ;; little more careful analysis to avoid it:
840 ;; If there's a label in front of the while
841 ;; it can't be part of a do-while.
842 (not after-labels-pos
))
843 (progn (c-bos-save-pos)
844 (setq state
'while-boundary
))
845 (c-bos-pop-state-and-retry))) ; Can't be a do-while
847 ((eq state
'while-boundary
)
849 (c-bos-pop-state (setq ret
'beginning
)))
850 ((eq sym
'boundary
) ; isn't a do-while
851 (c-bos-restore-pos) ; the position of the while
852 (c-bos-pop-state)))) ; no longer searching for do.
855 (if (eq sym
'boundary
)
856 (setq state
'catch-boundary
)
858 (c-bos-pop-state-and-retry)))
860 ((eq state
'catch-boundary
)
863 (c-bos-pop-state (setq ret
'beginning
)))
868 (c-bos-pop-state)))))
870 ;; This is state common. We get here when the previous
871 ;; cond statement found no particular state handler.
872 (cond ((eq sym
'boundary
)
873 ;; If we have a boundary at the start
874 ;; position we push a frame to go to the
875 ;; previous statement.
881 (c-bos-save-error-info 'if
'else
)
884 ;; Is this a real while, or a do-while?
885 ;; The next `when' triggers unless we are SURE that
886 ;; the `while' is not the tailend of a `do-while'.
887 (when (or (not pptok
)
888 (memq (char-after pptok
) delims
)
889 ;; The following kludge is to prevent
890 ;; infinite recursion when called from
891 ;; c-awk-after-if-for-while-condition-p,
893 (and (eq (point) start
)
894 (c-vsemi-status-unknown-p))
895 (c-at-vsemi-p pptok
))
896 ;; Since this can cause backtracking we do a
897 ;; little more careful analysis to avoid it: If
898 ;; the while isn't followed by a (possibly
899 ;; virtual) semicolon it can't be a do-while.
901 (setq state
'while
)))
902 ((memq sym
'(catch finally
))
904 (c-bos-save-error-info 'try sym
)
905 (setq state
'catch
))))
908 ;; We're either past a statement boundary or at the
909 ;; start of a statement, so throw away any label data
910 ;; for the previous one.
911 (setq after-labels-pos nil
913 c-maybe-labelp nil
))))
915 ;; Step to the previous sexp, but not if we crossed a
916 ;; boundary, since that doesn't consume an sexp.
917 (if (eq sym
'boundary
)
920 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
921 ;; BACKWARDS THROUGH THE SOURCE.
923 ;; This is typically fast with the caching done by
924 ;; c-(backward|forward)-sws.
925 (c-backward-syntactic-ws)
927 (let ((before-sws-pos (point))
928 ;; Set as long as we have to continue jumping by sexps.
929 ;; It's the position to use as end in the next round.
930 sexp-loop-continue-pos
931 ;; The end position of the area to search for statement
932 ;; barriers in this round.
933 (sexp-loop-end-pos pos
))
935 ;; The following while goes back one sexp per iteration.
938 (unless (c-safe (c-backward-sexp) t
)
939 ;; Give up if we hit an unbalanced block. Since the
940 ;; stack won't be empty the code below will report a
944 ;; Check if the sexp movement crossed a statement or
945 ;; declaration boundary. But first modify the point
946 ;; so that `c-crosses-statement-barrier-p' only looks
947 ;; at the non-sexp chars following the sexp.
955 (when (c-beginning-of-macro)
956 ;; Set continuation position in case
957 ;; `c-crosses-statement-barrier-p'
958 ;; doesn't detect anything below.
959 (setq sexp-loop-continue-pos
(point)))))
960 ;; If the sexp movement took us into a
961 ;; macro then there were only some non-sexp
962 ;; chars after it. Skip out of the macro
963 ;; to analyze them but not the non-sexp
964 ;; chars that might be inside the macro.
966 (c-crosses-statement-barrier-p
967 (point) sexp-loop-end-pos
))
971 (not (c-looking-at-inexpr-block lim nil t
)))
972 ;; Passed a block sexp. That's a boundary
976 ((looking-at "\\s\(")
977 ;; Passed some other paren. Only analyze
978 ;; the non-sexp chars after it.
979 (goto-char (1+ (c-down-list-backward
981 ;; We're at a valid token start position
982 ;; (outside the `save-excursion') if
983 ;; `c-crosses-statement-barrier-p' failed.
984 (c-crosses-statement-barrier-p
985 (point) sexp-loop-end-pos
))
988 ;; Passed a symbol sexp or line
989 ;; continuation. It doesn't matter that
990 ;; it's included in the analyzed region.
991 (if (c-crosses-statement-barrier-p
992 (point) sexp-loop-end-pos
)
994 ;; If it was a line continuation then we
995 ;; have to continue looping.
996 (if (looking-at "\\\\$")
997 (setq sexp-loop-continue-pos
(point)))
1004 ;; Like a C "continue". Analyze the next sexp.
1007 sexp-loop-continue-pos
) ; End of "go back a sexp" loop condition.
1008 (goto-char sexp-loop-continue-pos
)
1009 (setq sexp-loop-end-pos sexp-loop-continue-pos
1010 sexp-loop-continue-pos nil
))))
1013 (when (and c-opt-method-key
1014 (setq saved
(c-in-method-def-p)))
1016 ignore-labels t
) ; Avoid the label check on exit.
1020 (unless (eq ignore-labels t
)
1021 (when (numberp c-maybe-labelp
)
1022 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1023 ;; might be in a label now. Have we got a real label
1024 ;; (including a case label) or something like C++'s "public:"?
1025 ;; A case label might use an expression rather than a token.
1026 (setq after-case
:-pos
(or tok start
))
1027 (if (looking-at c-nonlabel-token-key
) ; e.g. "while" or "'a'"
1028 (setq c-maybe-labelp nil
)
1029 (if after-labels-pos
; Have we already encountered a label?
1030 (if (not last-label-pos
)
1031 (setq last-label-pos
(or tok start
)))
1032 (setq after-labels-pos
(or tok start
)))
1033 (setq c-maybe-labelp t
1034 label-good-pos nil
))) ; bogus "label"
1036 (when (and (not label-good-pos
) ; i.e. no invalid "label"'s yet
1038 (looking-at c-nonlabel-token-key
)) ; e.g. "while :"
1039 ;; We're in a potential label and it's the first
1040 ;; time we've found something that isn't allowed in
1042 (setq label-good-pos
(or tok start
))))
1044 ;; We've moved back by a sexp, so update the token positions.
1049 pos tok
))) ; Not nil (for the while loop).
1051 ;; If the stack isn't empty there might be errors to report.
1053 (if (and (vectorp saved-pos
) (eq (length saved-pos
) 3))
1054 (c-bos-report-error))
1055 (setq saved-pos
(cdr (car stack
))
1058 (when (and (eq ret
'same
)
1059 (not (memq sym
'(boundary ignore nil
))))
1060 ;; Need to investigate closer whether we've crossed
1061 ;; between a substatement and its containing statement.
1062 (if (setq saved
(if (looking-at c-block-stmt-1-key
)
1065 (cond ((> start saved
) (setq pos saved
))
1066 ((= start saved
) (setq ret
'up
)))))
1068 (when (and (not ignore-labels
)
1069 (eq c-maybe-labelp t
)
1070 (not (eq ret
'beginning
))
1072 (or (not label-good-pos
)
1073 (<= label-good-pos pos
)
1075 (goto-char (if (and last-label-pos
1076 (< last-label-pos start
))
1079 (looking-at c-label-kwds-regexp
))))
1080 ;; We're in a label. Maybe we should step to the statement
1082 (if (< after-labels-pos start
)
1083 (setq pos after-labels-pos
)
1085 (if (and last-label-pos
(< last-label-pos start
))
1086 ;; Might have jumped over several labels. Go to the last one.
1087 (setq pos last-label-pos
)))))
1089 ;; Have we got "case <expression>:"?
1091 (when (and after-case
:-pos
1092 (not (eq ret
'beginning
))
1093 (looking-at c-case-kwds-regexp
))
1094 (if (< after-case
:-pos start
)
1095 (setq pos after-case
:-pos
))
1099 ;; Skip over the unary operators that can start the statement.
1101 (c-backward-syntactic-ws)
1102 ;; protect AWK post-inc/decrement operators, etc.
1103 (and (not (c-at-vsemi-p (point)))
1104 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1109 (defun c-crosses-statement-barrier-p (from to
)
1110 "Return non-nil if buffer positions FROM to TO cross one or more
1111 statement or declaration boundaries. The returned value is actually
1112 the position of the earliest boundary char. FROM must not be within
1113 a string or comment.
1115 The variable `c-maybe-labelp' is set to the position of the first `:' that
1116 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1117 single `?' is found, then `c-maybe-labelp' is cleared.
1119 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1120 regarded as having a \"virtual semicolon\" immediately after the last token on
1121 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1123 Note that this function might do hidden buffer changes. See the
1124 comment at the start of cc-engine.el for more info."
1125 (let ((skip-chars c-stmt-delim-chars
)
1130 (while (progn (skip-chars-forward skip-chars to
)
1133 ((setq lit-range
(c-literal-limits from
)) ; Have we landed in a string/comment?
1134 (goto-char (cdr lit-range
)))
1135 ((eq (char-after) ?
:)
1137 (if (and (eq (char-after) ?
:)
1139 ;; Ignore scope operators.
1141 (setq c-maybe-labelp
(1- (point)))))
1142 ((eq (char-after) ??
)
1143 ;; A question mark. Can't be a label, so stop
1144 ;; looking for more : and ?.
1145 (setq c-maybe-labelp nil
1146 skip-chars
(substring c-stmt-delim-chars
0 -
2)))
1147 ((memq (char-after) '(?
# ?
\n ?
\r)) ; A virtual semicolon?
1148 (if (and (eq (char-before) ?
\\) (memq (char-after) '(?
\n ?
\r)))
1150 (skip-chars-backward " \t" from
)
1152 (throw 'done
(point))
1154 (t (throw 'done
(point)))))
1155 ;; In trailing space after an as yet undetected virtual semicolon?
1156 (c-backward-syntactic-ws from
)
1157 (if (and (< (point) to
)
1162 (defun c-at-statement-start-p ()
1163 "Return non-nil if the point is at the first token in a statement
1164 or somewhere in the syntactic whitespace before it.
1166 A \"statement\" here is not restricted to those inside code blocks.
1167 Any kind of declaration-like construct that occur outside function
1168 bodies is also considered a \"statement\".
1170 Note that this function might do hidden buffer changes. See the
1171 comment at the start of cc-engine.el for more info."
1176 (c-syntactic-skip-backward (substring c-stmt-delim-chars
1) nil t
)
1178 (eq (char-before) ?
})
1179 (and (eq (char-before) ?
{)
1180 (not (and c-special-brace-lists
1181 (progn (backward-char)
1182 (c-looking-at-special-brace-list)))))
1183 (c-crosses-statement-barrier-p (point) end
)))))
1185 (defun c-at-expression-start-p ()
1186 "Return non-nil if the point is at the first token in an expression or
1187 statement, or somewhere in the syntactic whitespace before it.
1189 An \"expression\" here is a bit different from the normal language
1190 grammar sense: It's any sequence of expression tokens except commas,
1191 unless they are enclosed inside parentheses of some kind. Also, an
1192 expression never continues past an enclosing parenthesis, but it might
1193 contain parenthesis pairs of any sort except braces.
1195 Since expressions never cross statement boundaries, this function also
1196 recognizes statement beginnings, just like `c-at-statement-start-p'.
1198 Note that this function might do hidden buffer changes. See the
1199 comment at the start of cc-engine.el for more info."
1203 (c-stmt-delim-chars c-stmt-delim-chars-with-comma
)
1205 (c-syntactic-skip-backward (substring c-stmt-delim-chars
1) nil t
)
1207 (memq (char-before) '(?
{ ?
}))
1208 (save-excursion (backward-char)
1209 (looking-at "\\s("))
1210 (c-crosses-statement-barrier-p (point) end
)))))
1213 ;; A set of functions that covers various idiosyncrasies in
1214 ;; implementations of `forward-comment'.
1216 ;; Note: Some emacsen considers incorrectly that any line comment
1217 ;; ending with a backslash continues to the next line. I can't think
1218 ;; of any way to work around that in a reliable way without changing
1219 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1220 ;; changing the syntax for backslash doesn't work since we must treat
1221 ;; escapes in string literals correctly.)
1223 (defun c-forward-single-comment ()
1224 "Move forward past whitespace and the closest following comment, if any.
1225 Return t if a comment was found, nil otherwise. In either case, the
1226 point is moved past the following whitespace. Line continuations,
1227 i.e. a backslashes followed by line breaks, are treated as whitespace.
1228 The line breaks that end line comments are considered to be the
1229 comment enders, so the point will be put on the beginning of the next
1230 line if it moved past a line comment.
1232 This function does not do any hidden buffer changes."
1234 (let ((start (point)))
1235 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1236 (goto-char (match-end 0)))
1238 (when (forward-comment 1)
1240 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1244 ;; Emacs includes the ending newline in a b-style (c++)
1245 ;; comment, but XEmacs doesn't. We depend on the Emacs
1246 ;; behavior (which also is symmetric).
1247 (if (and (eolp) (elt (parse-partial-sexp start
(point)) 7))
1248 (condition-case nil
(forward-char 1)))
1252 (defsubst c-forward-comments
()
1253 "Move forward past all following whitespace and comments.
1254 Line continuations, i.e. a backslashes followed by line breaks, are
1255 treated as whitespace.
1257 Note that this function might do hidden buffer changes. See the
1258 comment at the start of cc-engine.el for more info."
1261 ;; If forward-comment in at least XEmacs 21 is given a large
1262 ;; positive value, it'll loop all the way through if it hits
1264 (and (forward-comment 5)
1265 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1269 (when (looking-at "\\\\[\n\r]")
1273 (defun c-backward-single-comment ()
1274 "Move backward past whitespace and the closest preceding comment, if any.
1275 Return t if a comment was found, nil otherwise. In either case, the
1276 point is moved past the preceding whitespace. Line continuations,
1277 i.e. a backslashes followed by line breaks, are treated as whitespace.
1278 The line breaks that end line comments are considered to be the
1279 comment enders, so the point cannot be at the end of the same line to
1280 move over a line comment.
1282 This function does not do any hidden buffer changes."
1284 (let ((start (point)))
1285 ;; When we got newline terminated comments, forward-comment in all
1286 ;; supported emacsen so far will stop at eol of each line not
1287 ;; ending with a comment when moving backwards. This corrects for
1288 ;; that, and at the same time handles line continuations.
1290 (skip-chars-backward " \t\n\r\f\v")
1291 (and (looking-at "[\n\r]")
1292 (eq (char-before) ?
\\)))
1296 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1297 ;; backwards at bob.
1300 ;; Leave point after the closest following newline if we've
1301 ;; backed up over any above, since forward-comment won't move
1302 ;; backward over a line comment if point is at the end of the
1304 (re-search-forward "\\=\\s *[\n\r]" start t
)
1306 (if (if (forward-comment -
1)
1308 ;; If forward-comment above succeeded and we're at eol
1309 ;; then the newline we moved over above didn't end a
1310 ;; line comment, so we give it another go.
1311 (forward-comment -
1)
1314 ;; Emacs <= 20 and XEmacs move back over the closer of a
1315 ;; block comment that lacks an opener.
1316 (if (looking-at "\\*/")
1317 (progn (forward-char 2) nil
)
1320 (defsubst c-backward-comments
()
1321 "Move backward past all preceding whitespace and comments.
1322 Line continuations, i.e. a backslashes followed by line breaks, are
1323 treated as whitespace. The line breaks that end line comments are
1324 considered to be the comment enders, so the point cannot be at the end
1325 of the same line to move over a line comment. Unlike
1326 c-backward-syntactic-ws, this function doesn't move back over
1327 preprocessor directives.
1329 Note that this function might do hidden buffer changes. See the
1330 comment at the start of cc-engine.el for more info."
1332 (let ((start (point)))
1334 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1335 ;; return t when moving backwards at bob.
1338 (if (forward-comment -
1)
1339 (if (looking-at "\\*/")
1340 ;; Emacs <= 20 and XEmacs move back over the
1341 ;; closer of a block comment that lacks an opener.
1342 (progn (forward-char 2) nil
)
1345 ;; XEmacs treats line continuations as whitespace but
1346 ;; only in the backward direction, which seems a bit
1347 ;; odd. Anyway, this is necessary for Emacs.
1348 (when (and (looking-at "[\n\r]")
1349 (eq (char-before) ?
\\)
1355 ;; Tools for skipping over syntactic whitespace.
1357 ;; The following functions use text properties to cache searches over
1358 ;; large regions of syntactic whitespace. It works as follows:
1360 ;; o If a syntactic whitespace region contains anything but simple
1361 ;; whitespace (i.e. space, tab and line breaks), the text property
1362 ;; `c-in-sws' is put over it. At places where we have stopped
1363 ;; within that region there's also a `c-is-sws' text property.
1364 ;; That since there typically are nested whitespace inside that
1365 ;; must be handled separately, e.g. whitespace inside a comment or
1366 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1367 ;; to jump to another point with that property within the same
1368 ;; `c-in-sws' region. It can be likened to a ladder where
1369 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1371 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1372 ;; a "rung position" and also maybe on the first following char.
1373 ;; As many characters as can be conveniently found in this range
1374 ;; are marked, but no assumption can be made that the whole range
1375 ;; is marked (it could be clobbered by later changes, for
1378 ;; Note that some part of the beginning of a sequence of simple
1379 ;; whitespace might be part of the end of a preceding line comment
1380 ;; or cpp directive and must not be considered part of the "rung".
1381 ;; Such whitespace is some amount of horizontal whitespace followed
1382 ;; by a newline. In the case of cpp directives it could also be
1383 ;; two newlines with horizontal whitespace between them.
1385 ;; The reason to include the first following char is to cope with
1386 ;; "rung positions" that doesn't have any ordinary whitespace. If
1387 ;; `c-is-sws' is put on a token character it does not have
1388 ;; `c-in-sws' set simultaneously. That's the only case when that
1389 ;; can occur, and the reason for not extending the `c-in-sws'
1390 ;; region to cover it is that the `c-in-sws' region could then be
1391 ;; accidentally merged with a following one if the token is only
1392 ;; one character long.
1394 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1395 ;; removed in the changed region. If the change was inside
1396 ;; syntactic whitespace that means that the "ladder" is broken, but
1397 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1398 ;; parts on either side and use an ordinary search only to "repair"
1401 ;; Special care needs to be taken if a region is removed: If there
1402 ;; are `c-in-sws' on both sides of it which do not connect inside
1403 ;; the region then they can't be joined. If e.g. a marked macro is
1404 ;; broken, syntactic whitespace inside the new text might be
1405 ;; marked. If those marks would become connected with the old
1406 ;; `c-in-sws' range around the macro then we could get a ladder
1407 ;; with one end outside the macro and the other at some whitespace
1410 ;; The main motivation for this system is to increase the speed in
1411 ;; skipping over the large whitespace regions that can occur at the
1412 ;; top level in e.g. header files that contain a lot of comments and
1413 ;; cpp directives. For small comments inside code it's probably
1414 ;; slower than using `forward-comment' straightforwardly, but speed is
1415 ;; not a significant factor there anyway.
1417 ; (defface c-debug-is-sws-face
1418 ; '((t (:background "GreenYellow")))
1419 ; "Debug face to mark the `c-is-sws' property.")
1420 ; (defface c-debug-in-sws-face
1421 ; '((t (:underline t)))
1422 ; "Debug face to mark the `c-in-sws' property.")
1424 ; (defun c-debug-put-sws-faces ()
1425 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1426 ; ;; properties in the buffer.
1429 ; (c-save-buffer-state (in-face)
1430 ; (goto-char (point-min))
1431 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1434 ; (goto-char (next-single-property-change
1435 ; (point) 'c-is-sws nil (point-max)))
1438 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1439 ; (setq in-face nil))
1440 ; (setq in-face (point)))
1442 ; (goto-char (point-min))
1443 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1446 ; (goto-char (next-single-property-change
1447 ; (point) 'c-in-sws nil (point-max)))
1450 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1451 ; (setq in-face nil))
1452 ; (setq in-face (point)))
1455 (defmacro c-debug-sws-msg
(&rest args
)
1459 (defmacro c-put-is-sws
(beg end
)
1460 ;; This macro does a hidden buffer change.
1461 `(let ((beg ,beg
) (end ,end
))
1462 (put-text-property beg end
'c-is-sws t
)
1463 ,@(when (facep 'c-debug-is-sws-face
)
1464 `((c-debug-add-face beg end
'c-debug-is-sws-face
)))))
1466 (defmacro c-put-in-sws
(beg end
)
1467 ;; This macro does a hidden buffer change.
1468 `(let ((beg ,beg
) (end ,end
))
1469 (put-text-property beg end
'c-in-sws t
)
1470 ,@(when (facep 'c-debug-is-sws-face
)
1471 `((c-debug-add-face beg end
'c-debug-in-sws-face
)))))
1473 (defmacro c-remove-is-sws
(beg end
)
1474 ;; This macro does a hidden buffer change.
1475 `(let ((beg ,beg
) (end ,end
))
1476 (remove-text-properties beg end
'(c-is-sws nil
))
1477 ,@(when (facep 'c-debug-is-sws-face
)
1478 `((c-debug-remove-face beg end
'c-debug-is-sws-face
)))))
1480 (defmacro c-remove-in-sws
(beg end
)
1481 ;; This macro does a hidden buffer change.
1482 `(let ((beg ,beg
) (end ,end
))
1483 (remove-text-properties beg end
'(c-in-sws nil
))
1484 ,@(when (facep 'c-debug-is-sws-face
)
1485 `((c-debug-remove-face beg end
'c-debug-in-sws-face
)))))
1487 (defmacro c-remove-is-and-in-sws
(beg end
)
1488 ;; This macro does a hidden buffer change.
1489 `(let ((beg ,beg
) (end ,end
))
1490 (remove-text-properties beg end
'(c-is-sws nil c-in-sws nil
))
1491 ,@(when (facep 'c-debug-is-sws-face
)
1492 `((c-debug-remove-face beg end
'c-debug-is-sws-face
)
1493 (c-debug-remove-face beg end
'c-debug-in-sws-face
)))))
1495 (defsubst c-invalidate-sws-region-after
(beg end
)
1496 ;; Called from `after-change-functions'. Note that if
1497 ;; `c-forward-sws' or `c-backward-sws' are used outside
1498 ;; `c-save-buffer-state' or similar then this will remove the cache
1499 ;; properties right after they're added.
1501 ;; This function does hidden buffer changes.
1504 ;; Adjust the end to remove the properties in any following simple
1505 ;; ws up to and including the next line break, if there is any
1506 ;; after the changed region. This is necessary e.g. when a rung
1507 ;; marked empty line is converted to a line comment by inserting
1508 ;; "//" before the line break. In that case the line break would
1509 ;; keep the rung mark which could make a later `c-backward-sws'
1510 ;; move into the line comment instead of over it.
1512 (skip-chars-forward " \t\f\v")
1513 (when (and (eolp) (not (eobp)))
1514 (setq end
(1+ (point)))))
1516 (when (and (= beg end
)
1517 (get-text-property beg
'c-in-sws
)
1519 (get-text-property (1- beg
) 'c-in-sws
))
1520 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1521 ;; safe to keep a range that was continuous before the change. E.g:
1527 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1528 ;; after "foo" is removed then "bar" will become part of the cpp
1529 ;; directive instead of a syntactically relevant token. In that
1530 ;; case there's no longer syntactic ws from "#" to "b".
1531 (setq beg
(1- beg
)))
1533 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end
)
1534 (c-remove-is-and-in-sws beg end
))
1536 (defun c-forward-sws ()
1537 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1539 ;; This function might do hidden buffer changes.
1541 (let (;; `rung-pos' is set to a position as early as possible in the
1542 ;; unmarked part of the simple ws region.
1543 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1544 rung-is-marked next-rung-is-marked simple-ws-end
1545 ;; `safe-start' is set when it's safe to cache the start position.
1546 ;; It's not set if we've initially skipped over comments and line
1547 ;; continuations since we might have gone out through the end of a
1548 ;; macro then. This provision makes `c-forward-sws' not populate the
1549 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1553 ;; Skip simple ws and do a quick check on the following character to see
1554 ;; if it's anything that can't start syntactic ws, so we can bail out
1555 ;; early in the majority of cases when there just are a few ws chars.
1556 (skip-chars-forward " \t\n\r\f\v")
1557 (when (looking-at c-syntactic-ws-start
)
1559 (setq rung-end-pos
(min (1+ (point)) (point-max)))
1560 (if (setq rung-is-marked
(text-property-any rung-pos rung-end-pos
1562 ;; Find the last rung position to avoid setting properties in all
1563 ;; the cases when the marked rung is complete.
1564 ;; (`next-single-property-change' is certain to move at least one
1566 (setq rung-pos
(1- (next-single-property-change
1567 rung-is-marked
'c-is-sws nil rung-end-pos
)))
1568 ;; Got no marked rung here. Since the simple ws might have started
1569 ;; inside a line comment or cpp directive we must set `rung-pos' as
1570 ;; high as possible.
1571 (setq rung-pos
(point)))
1576 (when (and rung-is-marked
1577 (get-text-property (point) 'c-in-sws
))
1579 ;; The following search is the main reason that `c-in-sws'
1580 ;; and `c-is-sws' aren't combined to one property.
1581 (goto-char (next-single-property-change
1582 (point) 'c-in-sws nil
(point-max)))
1583 (unless (get-text-property (point) 'c-is-sws
)
1584 ;; If the `c-in-sws' region extended past the last
1585 ;; `c-is-sws' char we have to go back a bit.
1586 (or (get-text-property (1- (point)) 'c-is-sws
)
1587 (goto-char (previous-single-property-change
1588 (point) 'c-is-sws
)))
1592 "c-forward-sws cached move %s -> %s (max %s)"
1593 rung-pos
(point) (point-max))
1595 (setq rung-pos
(point))
1596 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1599 ;; We'll loop here if there is simple ws after the last rung.
1600 ;; That means that there's been some change in it and it's
1601 ;; possible that we've stepped into another ladder, so extend
1602 ;; the previous one to join with it if there is one, and try to
1603 ;; use the cache again.
1605 "c-forward-sws extending rung with [%s..%s] (max %s)"
1606 (1+ rung-pos
) (1+ (point)) (point-max))
1607 (unless (get-text-property (point) 'c-is-sws
)
1608 ;; Remove any `c-in-sws' property from the last char of
1609 ;; the rung before we mark it with `c-is-sws', so that we
1610 ;; won't connect with the remains of a broken "ladder".
1611 (c-remove-in-sws (point) (1+ (point))))
1612 (c-put-is-sws (1+ rung-pos
)
1614 (c-put-in-sws rung-pos
1615 (setq rung-pos
(point)
1616 last-put-in-sws-pos rung-pos
)))
1618 (setq simple-ws-end
(point))
1619 (c-forward-comments)
1622 ((/= (point) simple-ws-end
)
1623 ;; Skipped over comments. Don't cache at eob in case the buffer
1628 (and c-opt-cpp-prefix
1629 (looking-at c-opt-cpp-start
)
1630 (progn (skip-chars-backward " \t")
1633 (progn (backward-char)
1634 (not (eq (char-before) ?
\\))))))
1635 ;; Skip a preprocessor directive.
1637 (while (and (eq (char-before) ?
\\)
1638 (= (forward-line 1) 0))
1642 ;; Don't cache at eob in case the buffer is narrowed.
1645 ;; We've searched over a piece of non-white syntactic ws. See if this
1647 (setq next-rung-pos
(point))
1648 (skip-chars-forward " \t\n\r\f\v")
1649 (setq rung-end-pos
(min (1+ (point)) (point-max)))
1652 ;; Cache if we haven't skipped comments only, and if we started
1653 ;; either from a marked rung or from a completely uncached
1657 (not (get-text-property simple-ws-end
'c-in-sws
))))
1659 ;; See if there's a marked rung in the encountered simple ws. If
1660 ;; so then we can cache, unless `safe-start' is nil. Even then
1661 ;; we need to do this to check if the cache can be used for the
1663 (and (setq next-rung-is-marked
1664 (text-property-any next-rung-pos rung-end-pos
1670 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1671 rung-pos
(1+ simple-ws-end
) next-rung-pos rung-end-pos
1674 ;; Remove the properties for any nested ws that might be cached.
1675 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1677 (c-remove-is-sws (1+ simple-ws-end
) next-rung-pos
)
1678 (unless (and rung-is-marked
(= rung-pos simple-ws-end
))
1679 (c-put-is-sws rung-pos
1681 (setq rung-is-marked t
))
1682 (c-put-in-sws rung-pos
1683 (setq rung-pos
(point)
1684 last-put-in-sws-pos rung-pos
))
1685 (unless (get-text-property (1- rung-end-pos
) 'c-is-sws
)
1686 ;; Remove any `c-in-sws' property from the last char of
1687 ;; the rung before we mark it with `c-is-sws', so that we
1688 ;; won't connect with the remains of a broken "ladder".
1689 (c-remove-in-sws (1- rung-end-pos
) rung-end-pos
))
1690 (c-put-is-sws next-rung-pos
1694 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1695 rung-pos
(1+ simple-ws-end
) next-rung-pos rung-end-pos
1698 ;; Set `rung-pos' for the next rung. It's the same thing here as
1699 ;; initially, except that the rung position is set as early as
1700 ;; possible since we can't be in the ending ws of a line comment or
1701 ;; cpp directive now.
1702 (if (setq rung-is-marked next-rung-is-marked
)
1703 (setq rung-pos
(1- (next-single-property-change
1704 rung-is-marked
'c-is-sws nil rung-end-pos
)))
1705 (setq rung-pos next-rung-pos
))
1706 (setq safe-start t
)))
1708 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1709 ;; another one after the point (which might occur when editing inside a
1710 ;; comment or macro).
1711 (when (eq last-put-in-sws-pos
(point))
1712 (cond ((< last-put-in-sws-pos
(point-max))
1714 "c-forward-sws clearing at %s for cache separation"
1715 last-put-in-sws-pos
)
1716 (c-remove-in-sws last-put-in-sws-pos
1717 (1+ last-put-in-sws-pos
)))
1719 ;; If at eob we have to clear the last character before the end
1720 ;; instead since the buffer might be narrowed and there might
1721 ;; be a `c-in-sws' after (point-max). In this case it's
1722 ;; necessary to clear both properties.
1724 "c-forward-sws clearing thoroughly at %s for cache separation"
1725 (1- last-put-in-sws-pos
))
1726 (c-remove-is-and-in-sws (1- last-put-in-sws-pos
)
1727 last-put-in-sws-pos
))))
1730 (defun c-backward-sws ()
1731 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1733 ;; This function might do hidden buffer changes.
1735 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1736 ;; part of the simple ws region.
1737 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1738 rung-is-marked simple-ws-beg cmt-skip-pos
)
1740 ;; Skip simple horizontal ws and do a quick check on the preceding
1741 ;; character to see if it's anying that can't end syntactic ws, so we can
1742 ;; bail out early in the majority of cases when there just are a few ws
1743 ;; chars. Newlines are complicated in the backward direction, so we can't
1745 (skip-chars-backward " \t\f")
1746 (when (and (not (bobp))
1749 (looking-at c-syntactic-ws-end
)))
1751 ;; Try to find a rung position in the simple ws preceding point, so that
1752 ;; we can get a cache hit even if the last bit of the simple ws has
1753 ;; changed recently.
1754 (setq simple-ws-beg
(point))
1755 (skip-chars-backward " \t\n\r\f\v")
1756 (if (setq rung-is-marked
(text-property-any
1757 (point) (min (1+ rung-pos
) (point-max))
1759 ;; `rung-pos' will be the earliest marked position, which means that
1760 ;; there might be later unmarked parts in the simple ws region.
1761 ;; It's not worth the effort to fix that; the last part of the
1762 ;; simple ws is also typically edited often, so it could be wasted.
1763 (goto-char (setq rung-pos rung-is-marked
))
1764 (goto-char simple-ws-beg
))
1769 (when (and rung-is-marked
1771 (get-text-property (1- (point)) 'c-in-sws
))
1773 ;; The following search is the main reason that `c-in-sws'
1774 ;; and `c-is-sws' aren't combined to one property.
1775 (goto-char (previous-single-property-change
1776 (point) 'c-in-sws nil
(point-min)))
1777 (unless (get-text-property (point) 'c-is-sws
)
1778 ;; If the `c-in-sws' region extended past the first
1779 ;; `c-is-sws' char we have to go forward a bit.
1780 (goto-char (next-single-property-change
1781 (point) 'c-is-sws
)))
1784 "c-backward-sws cached move %s <- %s (min %s)"
1785 (point) rung-pos
(point-min))
1787 (setq rung-pos
(point))
1788 (if (and (< (min (skip-chars-backward " \t\f\v")
1790 (setq simple-ws-beg
(point))
1791 (skip-chars-backward " \t\n\r\f\v")))
1793 (setq rung-is-marked
1794 (text-property-any (point) rung-pos
1797 (goto-char simple-ws-beg
)
1800 ;; We'll loop here if there is simple ws before the first rung.
1801 ;; That means that there's been some change in it and it's
1802 ;; possible that we've stepped into another ladder, so extend
1803 ;; the previous one to join with it if there is one, and try to
1804 ;; use the cache again.
1806 "c-backward-sws extending rung with [%s..%s] (min %s)"
1807 rung-is-marked rung-pos
(point-min))
1808 (unless (get-text-property (1- rung-pos
) 'c-is-sws
)
1809 ;; Remove any `c-in-sws' property from the last char of
1810 ;; the rung before we mark it with `c-is-sws', so that we
1811 ;; won't connect with the remains of a broken "ladder".
1812 (c-remove-in-sws (1- rung-pos
) rung-pos
))
1813 (c-put-is-sws rung-is-marked
1815 (c-put-in-sws rung-is-marked
1817 (setq rung-pos rung-is-marked
1818 last-put-in-sws-pos rung-pos
))
1820 (c-backward-comments)
1821 (setq cmt-skip-pos
(point))
1824 ((and c-opt-cpp-prefix
1825 (/= cmt-skip-pos simple-ws-beg
)
1826 (c-beginning-of-macro))
1827 ;; Inside a cpp directive. See if it should be skipped over.
1828 (let ((cpp-beg (point)))
1830 ;; Move back over all line continuations in the region skipped
1831 ;; over by `c-backward-comments'. If we go past it then we
1832 ;; started inside the cpp directive.
1833 (goto-char simple-ws-beg
)
1835 (while (and (> (point) cmt-skip-pos
)
1836 (progn (backward-char)
1837 (eq (char-before) ?
\\)))
1838 (beginning-of-line))
1840 (if (< (point) cmt-skip-pos
)
1841 ;; Don't move past the cpp directive if we began inside
1842 ;; it. Note that the position at the end of the last line
1843 ;; of the macro is also considered to be within it.
1844 (progn (goto-char cmt-skip-pos
)
1847 ;; It's worthwhile to spend a little bit of effort on finding
1848 ;; the end of the macro, to get a good `simple-ws-beg'
1849 ;; position for the cache. Note that `c-backward-comments'
1850 ;; could have stepped over some comments before going into
1851 ;; the macro, and then `simple-ws-beg' must be kept on the
1852 ;; same side of those comments.
1853 (goto-char simple-ws-beg
)
1854 (skip-chars-backward " \t\n\r\f\v")
1855 (if (eq (char-before) ?
\\)
1858 (if (< (point) simple-ws-beg
)
1859 ;; Might happen if comments after the macro were skipped
1861 (setq simple-ws-beg
(point)))
1866 ((/= (save-excursion
1867 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg
)
1868 (setq next-rung-pos
(point)))
1870 ;; Skipped over comments. Must put point at the end of
1871 ;; the simple ws at point since we might be after a line
1872 ;; comment or cpp directive that's been partially
1873 ;; narrowed out, and we can't risk marking the simple ws
1874 ;; at the end of it.
1875 (goto-char next-rung-pos
)
1878 ;; We've searched over a piece of non-white syntactic ws. See if this
1880 (setq next-rung-pos
(point))
1881 (skip-chars-backward " \t\f\v")
1884 ;; Cache if we started either from a marked rung or from a
1885 ;; completely uncached position.
1887 (not (get-text-property (1- simple-ws-beg
) 'c-in-sws
))
1889 ;; Cache if there's a marked rung in the encountered simple ws.
1891 (skip-chars-backward " \t\n\r\f\v")
1892 (text-property-any (point) (min (1+ next-rung-pos
) (point-max))
1897 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1898 (point) (1+ next-rung-pos
)
1899 simple-ws-beg
(min (1+ rung-pos
) (point-max))
1902 ;; Remove the properties for any nested ws that might be cached.
1903 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1905 (c-remove-is-sws (1+ next-rung-pos
) simple-ws-beg
)
1906 (unless (and rung-is-marked
(= simple-ws-beg rung-pos
))
1907 (let ((rung-end-pos (min (1+ rung-pos
) (point-max))))
1908 (unless (get-text-property (1- rung-end-pos
) 'c-is-sws
)
1909 ;; Remove any `c-in-sws' property from the last char of
1910 ;; the rung before we mark it with `c-is-sws', so that we
1911 ;; won't connect with the remains of a broken "ladder".
1912 (c-remove-in-sws (1- rung-end-pos
) rung-end-pos
))
1913 (c-put-is-sws simple-ws-beg
1915 (setq rung-is-marked t
)))
1916 (c-put-in-sws (setq simple-ws-beg
(point)
1917 last-put-in-sws-pos simple-ws-beg
)
1919 (c-put-is-sws (setq rung-pos simple-ws-beg
)
1920 (1+ next-rung-pos
)))
1923 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1924 (point) (1+ next-rung-pos
)
1925 simple-ws-beg
(min (1+ rung-pos
) (point-max))
1927 (setq rung-pos next-rung-pos
1928 simple-ws-beg
(point))
1931 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1932 ;; another one before the point (which might occur when editing inside a
1933 ;; comment or macro).
1934 (when (eq last-put-in-sws-pos
(point))
1935 (cond ((< (point-min) last-put-in-sws-pos
)
1937 "c-backward-sws clearing at %s for cache separation"
1938 (1- last-put-in-sws-pos
))
1939 (c-remove-in-sws (1- last-put-in-sws-pos
)
1940 last-put-in-sws-pos
))
1942 ;; If at bob and the buffer is narrowed, we have to clear the
1943 ;; character we're standing on instead since there might be a
1944 ;; `c-in-sws' before (point-min). In this case it's necessary
1945 ;; to clear both properties.
1947 "c-backward-sws clearing thoroughly at %s for cache separation"
1948 last-put-in-sws-pos
)
1949 (c-remove-is-and-in-sws last-put-in-sws-pos
1950 (1+ last-put-in-sws-pos
)))))
1954 ;; Other whitespace tools
1955 (defun c-partial-ws-p (beg end
)
1956 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1957 ;; region? This is a "heuristic" function. .....
1959 ;; The motivation for the second bit is to check whether removing this
1960 ;; region would coalesce two symbols.
1962 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1963 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1965 (let ((end+1 (min (1+ end
) (point-max))))
1966 (or (progn (goto-char (max (point-min) (1- beg
)))
1967 (c-skip-ws-forward end
)
1969 (progn (goto-char beg
)
1970 (c-skip-ws-forward end
+1)
1971 (eq (point) end
+1))))))
1973 ;; A system for finding noteworthy parens before the point.
1975 (defconst c-state-cache-too-far
5000)
1976 ;; A maximum comfortable scanning distance, e.g. between
1977 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
1978 ;; this distance is exceeded, we take "emergency meausures", e.g. by clearing
1979 ;; the cache and starting again from point-min or a beginning of defun. This
1980 ;; value can be tuned for efficiency or set to a lower value for testing.
1982 (defvar c-state-cache nil
)
1983 (make-variable-buffer-local 'c-state-cache
)
1984 ;; The state cache used by `c-parse-state' to cut down the amount of
1985 ;; searching. It's the result from some earlier `c-parse-state' call. See
1986 ;; `c-parse-state''s doc string for details of its structure.
1988 ;; The use of the cached info is more effective if the next
1989 ;; `c-parse-state' call is on a line close by the one the cached state
1990 ;; was made at; the cache can actually slow down a little if the
1991 ;; cached state was made very far back in the buffer. The cache is
1992 ;; most effective if `c-parse-state' is used on each line while moving
1995 (defvar c-state-cache-good-pos
1)
1996 (make-variable-buffer-local 'c-state-cache-good-pos
)
1997 ;; This is a position where `c-state-cache' is known to be correct, or
1998 ;; nil (see below). It's a position inside one of the recorded unclosed
1999 ;; parens or the top level, but not further nested inside any literal or
2000 ;; subparen that is closed before the last recorded position.
2002 ;; The exact position is chosen to try to be close to yet earlier than
2003 ;; the position where `c-state-cache' will be called next. Right now
2004 ;; the heuristic is to set it to the position after the last found
2005 ;; closing paren (of any type) before the line on which
2006 ;; `c-parse-state' was called. That is chosen primarily to work well
2007 ;; with refontification of the current line.
2009 ;; 2009-07-28: When `c-state-point-min' and the last position where
2010 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2011 ;; both in the same literal, there is no such "good position", and
2012 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2013 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2015 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2016 ;; the middle of the desert, as long as it is not within a brace pair
2017 ;; recorded in `c-state-cache' or a paren/bracket pair.
2020 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2021 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2022 ;; speed up testing for non-literality.
2023 (defconst c-state-nonlit-pos-interval
10000)
2024 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2026 (defvar c-state-nonlit-pos-cache nil
)
2027 (make-variable-buffer-local 'c-state-nonlit-pos-cache
)
2028 ;; A list of buffer positions which are known not to be in a literal. This is
2029 ;; ordered with higher positions at the front of the list. Only those which
2030 ;; are less than `c-state-nonlit-pos-cache-limit' are valid.
2032 (defvar c-state-nonlit-pos-cache-limit
1)
2033 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit
)
2034 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2035 ;; reduced by buffer changes, and increased by invocations of
2036 ;; `c-state-literal-at'.
2038 (defsubst c-state-pp-to-literal
(from to
)
2039 ;; Do a parse-partial-sexp from FROM to TO, returning the bounds of any
2040 ;; literal at TO as a cons, otherwise NIL.
2041 ;; FROM must not be in a literal, and the buffer should already be wide
2044 (let ((s (parse-partial-sexp from to
)))
2045 (when (or (nth 3 s
) (nth 4 s
)) ; in a string or comment
2046 (parse-partial-sexp (point) (point-max)
2050 'syntax-table
) ; stop at end of literal
2051 (cons (nth 8 s
) (point))))))
2053 (defun c-state-literal-at (here)
2054 ;; If position HERE is inside a literal, return (START . END), the
2055 ;; boundaries of the literal (which may be outside the accessible bit of the
2056 ;; buffer). Otherwise, return nil.
2058 ;; This function is almost the same as `c-literal-limits'. It differs in
2059 ;; that it is a lower level function, and that it rigourously follows the
2060 ;; syntax from BOB, whereas `c-literal-limits' uses a "local" safe position.
2064 (let ((c c-state-nonlit-pos-cache
)
2066 ;; Trim the cache to take account of buffer changes.
2067 (while (and c
(> (car c
) c-state-nonlit-pos-cache-limit
))
2069 (setq c-state-nonlit-pos-cache c
)
2071 (while (and c
(> (car c
) here
))
2073 (setq pos
(or (car c
) (point-min)))
2075 (while (<= (setq npos
(+ pos c-state-nonlit-pos-interval
))
2077 (setq lit
(c-state-pp-to-literal pos npos
))
2078 (setq pos
(or (cdr lit
) npos
)) ; end of literal containing npos.
2079 (setq c-state-nonlit-pos-cache
(cons pos c-state-nonlit-pos-cache
)))
2081 (if (> pos c-state-nonlit-pos-cache-limit
)
2082 (setq c-state-nonlit-pos-cache-limit pos
))
2084 (setq lit
(c-state-pp-to-literal pos here
)))
2087 (defsubst c-state-lit-beg
(pos)
2088 ;; Return the start of the literal containing POS, or POS itself.
2089 (or (car (c-state-literal-at pos
))
2092 (defsubst c-state-cache-non-literal-place
(pos state
)
2093 ;; Return a position outside of a string/comment at or before POS.
2094 ;; STATE is the parse-partial-sexp state at POS.
2095 (if (or (nth 3 state
) ; in a string?
2096 (nth 4 state
)) ; in a comment?
2101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2102 ;; Stuff to do with point-min, and coping with any literal there.
2103 (defvar c-state-point-min
1)
2104 (make-variable-buffer-local 'c-state-point-min
)
2105 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2106 ;; narrowing is likely to affect the parens that are visible before the point.
2108 (defvar c-state-point-min-lit-type nil
)
2109 (make-variable-buffer-local 'c-state-point-min-lit-type
)
2110 (defvar c-state-point-min-lit-start nil
)
2111 (make-variable-buffer-local 'c-state-point-min-lit-start
)
2112 ;; These two variables define the literal, if any, containing point-min.
2113 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2114 ;; literal. If there's no literal there, they're both nil.
2116 (defvar c-state-min-scan-pos
1)
2117 (make-variable-buffer-local 'c-state-min-scan-pos
)
2118 ;; This is the earliest buffer-pos from which scanning can be done. It is
2119 ;; either the end of the literal containing point-min, or point-min itself.
2120 ;; It becomes nil if the buffer is changed earlier than this point.
2121 (defun c-state-get-min-scan-pos ()
2122 ;; Return the lowest valid scanning pos. This will be the end of the
2123 ;; literal enclosing point-min, or point-min itself.
2124 (or c-state-min-scan-pos
2128 (goto-char c-state-point-min-lit-start
)
2129 (if (eq c-state-point-min-lit-type
'string
)
2131 (forward-comment 1))
2132 (setq c-state-min-scan-pos
(point))))))
2134 (defun c-state-mark-point-min-literal ()
2135 ;; Determine the properties of any literal containing POINT-MIN, setting the
2136 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2137 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2138 (let ((p-min (point-min))
2142 (setq lit
(c-state-literal-at p-min
))
2144 (setq c-state-point-min-lit-type
2146 (goto-char (car lit
))
2148 ((looking-at c-block-comment-start-regexp
) 'c
)
2149 ((looking-at c-line-comment-starter
) 'c
++)
2151 c-state-point-min-lit-start
(car lit
)
2152 c-state-min-scan-pos
(cdr lit
))
2153 (setq c-state-point-min-lit-type nil
2154 c-state-point-min-lit-start nil
2155 c-state-min-scan-pos p-min
)))))
2158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2159 ;; A variable which signals a brace dessert - helpful for reducing the number
2160 ;; of fruitless backward scans.
2161 (defvar c-state-brace-pair-desert nil
)
2162 (make-variable-buffer-local 'c-state-brace-pair-desert
)
2163 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when an
2164 ;; that defun has searched backwards for a brace pair and not found one. Its
2165 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2166 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2167 ;; nil when at top level) and FROM is where the backward search started. It
2168 ;; is reset to nil in `c-invalidate-state-cache'.
2171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2172 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2173 ;; list of like structure.
2174 (defmacro c-state-cache-top-lparen
(&optional cache
)
2175 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2176 ;; (default `c-state-cache') (or nil).
2177 (let ((cash (or cache
'c-state-cache
)))
2178 `(if (consp (car ,cash
))
2182 (defmacro c-state-cache-top-paren
(&optional cache
)
2183 ;; Return the address of the latest brace/bracket/paren (whether left or
2184 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2185 (let ((cash (or cache
'c-state-cache
)))
2186 `(if (consp (car ,cash
))
2190 (defmacro c-state-cache-after-top-paren
(&optional cache
)
2191 ;; Return the position just after the latest brace/bracket/paren (whether
2192 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2193 (let ((cash (or cache
'c-state-cache
)))
2194 `(if (consp (car ,cash
))
2197 (1+ (car ,cash
))))))
2199 (defun c-get-cache-scan-pos (here)
2200 ;; From the state-cache, determine the buffer position from which we might
2201 ;; scan forward to HERE to update this cache. This position will be just
2202 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2203 ;; return the earliest position in the accessible region which isn't within
2204 ;; a literal. If the visible portion of the buffer is entirely within a
2205 ;; literal, return NIL.
2206 (let ((c c-state-cache
) elt
)
2207 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2209 (>= (c-state-cache-top-lparen c
) here
))
2215 (if (> (cdr elt
) here
)
2219 ((<= (c-state-get-min-scan-pos) here
)
2220 (c-state-get-min-scan-pos))
2223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2224 ;; Variables which keep track of preprocessor constructs.
2225 (defvar c-state-old-cpp-beg nil
)
2226 (make-variable-buffer-local 'c-state-old-cpp-beg
)
2227 (defvar c-state-old-cpp-end nil
)
2228 (make-variable-buffer-local 'c-state-old-cpp-end
)
2229 ;; These are the limits of the macro containing point at the previous call of
2230 ;; `c-parse-state', or nil.
2232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2233 ;; Defuns which analyse the buffer, yet don't change `c-state-cache'.
2234 (defun c-get-fallback-scan-pos (here)
2235 ;; Return a start position for building `c-state-cache' from
2236 ;; scratch. This will be at the top level, 2 defuns back.
2238 ;; Go back 2 bods, but ignore any bogus positions returned by
2239 ;; beginning-of-defun (i.e. open paren in column zero).
2242 (while (not (or (bobp) (zerop cnt
)))
2243 (c-beginning-of-defun-1) ; Pure elisp BOD.
2244 (if (eq (char-after) ?\
{)
2245 (setq cnt
(1- cnt
)))))
2248 (defun c-state-balance-parens-backwards (here- here
+ top
)
2249 ;; Return the position of the opening paren/brace/bracket before HERE- which
2250 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2251 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2253 ;; ............................................
2255 ;; ( [ ( .........#macro.. ) ( ) ] )
2258 ;; return HERE- HERE+ TOP
2260 ;; If there aren't enough opening paren/brace/brackets, return the position
2261 ;; of the outermost one found, or HERE- if there are none. If there are no
2262 ;; closeing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2263 ;; must not be inside literals. Only the accessible portion of the buffer
2266 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2267 ;; `here'. Go round the next loop each time we pass over such a ")". These
2268 ;; probably match "("s before `here-'.
2269 (let (pos pa ren
+1 lonely-rens
)
2272 (narrow-to-region (point-min) top
) ; This can move point, sometimes.
2276 (setq ren
+1 (scan-lists pos
1 1)) ; might signal
2277 (setq lonely-rens
(cons ren
+1 lonely-rens
)
2280 ;; PART 2: Scan back before `here-' searching for the "("s
2281 ;; matching/mismatching the ")"s found above. We only need to direct the
2282 ;; caller to scan when we've encountered unmatched right parens.
2287 (and lonely-rens
; actual values aren't used.
2288 (setq pa
(scan-lists pos -
1 1)))
2290 (setq lonely-rens
(cdr lonely-rens
)))))
2293 (defun c-parse-state-get-strategy (here good-pos
)
2294 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2295 ;; to minimise the amount of scanning. HERE is the pertinent position in
2296 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2297 ;; its head trimmed) is known to be good, or nil if there is no such
2300 ;; The return value is a list, one of the following:
2302 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2303 ;; which is not less than CACHE-POS.
2304 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2305 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2307 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2308 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2310 (let ((cache-pos (c-get-cache-scan-pos here
)) ; highest position below HERE in cache (or 1)
2311 BOD-pos
; position of 2nd BOD before HERE.
2312 strategy
; 'forward, 'backward, 'BOD, or 'IN-LIT.
2314 how-far
) ; putative scanning distance.
2315 (setq good-pos
(or good-pos
(c-state-get-min-scan-pos)))
2317 ((< here
(c-state-get-min-scan-pos))
2318 (setq strategy
'IN-LIT
2323 (setq strategy
'forward
2324 start-point
(max good-pos cache-pos
)
2325 how-far
(- here start-point
)))
2326 ((< (- good-pos here
) (- here cache-pos
)) ; FIXME!!! ; apply some sort of weighting.
2327 (setq strategy
'backward
2328 how-far
(- good-pos here
)))
2330 (setq strategy
'forward
2331 how-far
(- here cache-pos
)
2332 start-point cache-pos
)))
2334 ;; Might we be better off starting from the top level, two defuns back,
2336 (when (> how-far c-state-cache-too-far
)
2337 (setq BOD-pos
(c-get-fallback-scan-pos here
)) ; somewhat EXPENSIVE!!!
2338 (if (< (- here BOD-pos
) how-far
)
2340 start-point BOD-pos
)))
2344 (and (memq strategy
'(forward backward
)) cache-pos
)
2345 (and (memq strategy
'(forward BOD
)) start-point
))))
2348 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2349 ;; Routines which change `c-state-cache' and associated values.
2350 (defun c-renarrow-state-cache ()
2351 ;; The region (more precisely, point-min) has changed since we
2352 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2353 (if (< (point-min) c-state-point-min
)
2354 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2355 ;; It would be possible to do a better job here and recalculate the top
2358 (c-state-mark-point-min-literal)
2359 (setq c-state-cache nil
2360 c-state-cache-good-pos c-state-min-scan-pos
2361 c-state-brace-pair-desert nil
))
2363 ;; point-min has MOVED FORWARD.
2365 ;; Is the new point-min inside a (different) literal?
2366 (unless (and c-state-point-min-lit-start
; at prev. point-min
2367 (< (point-min) (c-state-get-min-scan-pos)))
2368 (c-state-mark-point-min-literal))
2370 ;; Cut off a bit of the tail from `c-state-cache'.
2371 (let ((ptr (cons nil c-state-cache
))
2373 (while (and (setq pa
(c-state-cache-top-lparen (cdr ptr
)))
2374 (>= pa
(point-min)))
2375 (setq ptr
(cdr ptr
)))
2378 (if (eq (cdr ptr
) c-state-cache
)
2379 (setq c-state-cache nil
2380 c-state-cache-good-pos c-state-min-scan-pos
)
2382 (setq c-state-cache-good-pos
(1+ (c-state-cache-top-lparen))))
2385 (setq c-state-point-min
(point-min)))
2387 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim
)
2388 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2389 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2390 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2391 ;; the highest brace pair whose "}" is below UPPER-LIM.
2393 ;; Return non-nil when this has been done.
2395 ;; This routine should be fast. Since it can get called a LOT, we maintain
2396 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2397 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2400 (let ((bra from
) ce
; Positions of "{" and "}".
2402 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2403 (macro-start-or-from
2404 (progn (goto-char from
)
2405 (c-beginning-of-macro)
2407 (or upper-lim
(setq upper-lim from
))
2409 ;; If we're essentially repeating a fruitless search, just give up.
2410 (unless (and c-state-brace-pair-desert
2411 (eq cache-pos
(car c-state-brace-pair-desert
))
2412 (<= from
(cdr c-state-brace-pair-desert
)))
2413 ;; Only search what we absolutely need to:
2414 (if (and c-state-brace-pair-desert
2415 (> from
(cdr c-state-brace-pair-desert
)))
2416 (narrow-to-region (cdr c-state-brace-pair-desert
) (point-max)))
2418 ;; In the next pair of nested loops, the inner one moves back past a
2419 ;; pair of (mis-)matching parens or brackets; the outer one moves
2420 ;; back over a sequence of unmatched close brace/paren/bracket each
2426 (and (setq ce
(scan-lists bra -
1 -
1)) ; back past )/]/}; might signal
2427 (setq bra
(scan-lists ce -
1 1)) ; back past (/[/{; might signal
2428 (or (> ce upper-lim
)
2429 (not (eq (char-after bra
) ?\
{))
2430 (and (goto-char bra
)
2431 (c-beginning-of-macro)
2432 (< (point) macro-start-or-from
))))))
2433 (and ce
(< ce bra
)))
2434 (setq bra ce
)) ; If we just backed over an unbalanced closing
2437 (if (and ce
(< bra ce
) (eq (char-after bra
) ?\
{))
2438 ;; We've found the desired brace-pair.
2440 (setq new-cons
(cons bra
(1+ ce
)))
2442 ((consp (car c-state-cache
))
2443 (setcar c-state-cache new-cons
))
2444 ((and (numberp (car c-state-cache
)) ; probably never happens
2445 (< ce
(car c-state-cache
)))
2446 (setcdr c-state-cache
2447 (cons new-cons
(cdr c-state-cache
))))
2448 (t (setq c-state-cache
(cons new-cons c-state-cache
)))))
2450 ;; We haven't found a brace pair. Record this.
2451 (setq c-state-brace-pair-desert
(cons cache-pos from
))))))))
2453 (defsubst c-state-push-any-brace-pair
(bra+1 macro-start-or-here
)
2454 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2455 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2456 ;; "push" "a" brace pair onto `c-state-cache'.
2458 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2459 ;; otherwise push it normally.
2461 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2462 ;; latter is inside a macro, not being a macro containing
2463 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2464 ;; base pair. This latter case is assumed to be rare.
2466 ;; Note: POINT is not preserved in this routine.
2468 (if (or (> bra
+1 macro-start-or-here
)
2469 (progn (goto-char bra
+1)
2470 (not (c-beginning-of-macro))))
2472 (cons (cons (1- bra
+1)
2473 (scan-lists bra
+1 1 1))
2474 (if (consp (car c-state-cache
))
2477 ;; N.B. This defsubst codes one method for the simple, normal case,
2478 ;; and a more sophisticated, slower way for the general case. Don't
2479 ;; eliminate this defsubst - it's a speed optimisation.
2480 (c-append-lower-brace-pair-to-state-cache (1- bra
+1)))))
2482 (defun c-append-to-state-cache (from)
2483 ;; Scan the buffer from FROM to (point-max), adding elements into
2484 ;; `c-state-cache' for braces etc. Return a candidate for
2485 ;; `c-state-cache-good-pos'.
2487 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2488 ;; any. Typically, it is immediately after it. It must not be inside a
2490 (let ((here-bol (c-point 'bol
(point-max)))
2491 (macro-start-or-here
2492 (save-excursion (goto-char (point-max))
2493 (if (c-beginning-of-macro)
2496 pa
+1 ; pos just after an opening PAren (or brace).
2497 (ren+1 from
) ; usually a pos just after an closing paREN etc.
2498 ; Is actually the pos. to scan for a (/{/[ from,
2499 ; which sometimes is after a silly )/}/].
2500 paren
+1 ; Pos after some opening or closing paren.
2501 paren
+1s
; A list of `paren+1's; used to determine a
2503 bra
+1 ce
+1 ; just after L/R bra-ces.
2504 bra
+1s
; list of OLD values of bra+1.
2505 mstart
) ; start of a macro.
2508 ;; Each time round the following loop, we enter a succesively deeper
2509 ;; level of brace/paren nesting. (Except sometimes we "continue at
2510 ;; the existing level".) `pa+1' is a pos inside an opening
2511 ;; brace/paren/bracket, usually just after it.
2514 ;; Each time round the next loop moves forward over an opening then
2515 ;; a closing brace/bracket/paren. This loop is white hot, so it
2516 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2517 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2518 ;; call of `scan-lists' signals an error, which happens when there
2519 ;; are no more b/b/p's to scan.
2522 (setq pa
+1 (scan-lists ren
+1 1 -
1) ; Into (/{/[; might signal
2523 paren
+1s
(cons pa
+1 paren
+1s
))
2524 (setq ren
+1 (scan-lists pa
+1 1 1)) ; Out of )/}/]; might signal
2525 (if (and (eq (char-before pa
+1) ?
{)) ; Check for a macro later.
2527 (setcar paren
+1s ren
+1)))
2529 (if (and pa
+1 (> pa
+1 ren
+1))
2530 ;; We've just entered a deeper nesting level.
2532 ;; Insert the brace pair (if present) and the single open
2533 ;; paren/brace/bracket into `c-state-cache' It cannot be
2534 ;; inside a macro, except one around point, because of what
2535 ;; `c-neutralize-syntax-in-CPP' has done.
2536 (c-state-push-any-brace-pair bra
+1 macro-start-or-here
)
2537 ;; Insert the opening brace/bracket/paren position.
2538 (setq c-state-cache
(cons (1- pa
+1) c-state-cache
))
2539 ;; Clear admin stuff for the next more nested part of the scan.
2540 (setq ren
+1 pa
+1 pa
+1 nil bra
+1 nil bra
+1s nil
)
2541 t
) ; Carry on the loop
2543 ;; All open p/b/b's at this nesting level, if any, have probably
2544 ;; been closed by matching/mismatching ones. We're probably
2545 ;; finished - we just need to check for having found an
2546 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2547 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2548 (c-safe (setq ren
+1 (scan-lists ren
+1 1 1)))))) ; acts as loop control.
2550 ;; Record the final, innermost, brace-pair if there is one.
2551 (c-state-push-any-brace-pair bra
+1 macro-start-or-here
)
2553 ;; Determine a good pos
2554 (while (and (setq paren
+1 (car paren
+1s
))
2555 (> (if (> paren
+1 macro-start-or-here
)
2558 (setq mstart
(and (c-beginning-of-macro)
2560 (or mstart paren
+1))
2562 (setq paren
+1s
(cdr paren
+1s
)))
2564 ((and paren
+1 mstart
)
2565 (min paren
+1 mstart
))
2569 (defun c-remove-stale-state-cache (good-pos pps-point
)
2570 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2571 ;; not be in it when it is amended for position (point-max).
2572 ;; Additionally, the "outermost" open-brace entry before (point-max)
2573 ;; will be converted to a cons if the matching close-brace is scanned.
2575 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2576 ;; parens/braces/brackets between GOOD-POS and (point-max).
2578 ;; As a second thing, calculate the result of parse-partial-sexp at
2579 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2580 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2581 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2582 ;; needs to be FAST).
2584 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2585 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2586 ;; to be good (we aim for this to be as high as possible);
2587 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2588 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2589 ;; position to scan backwards from.
2590 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2592 (narrow-to-region 1 (point-max))
2594 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2596 (goto-char (point-max))
2597 (and (c-beginning-of-macro)
2599 (good-pos-actual-macro-start ; Start of macro containing good-pos
2601 (and (< good-pos
(point-max))
2603 (goto-char good-pos
)
2604 (and (c-beginning-of-macro)
2606 (good-pos-actual-macro-end ; End of this macro, (maybe
2607 ; (point-max)), or nil.
2608 (and good-pos-actual-macro-start
2610 (goto-char good-pos-actual-macro-start
)
2613 pps-state
; Will be 9 or 10 elements long.
2615 upper-lim
; ,beyond which `c-state-cache' entries are removed
2617 pair-beg pps-point-state target-depth
)
2619 ;; Remove entries beyond (point-max). Also remove any entries inside
2620 ;; a macro, unless (point-max) is in the same macro.
2622 (if (or (null c-state-old-cpp-beg
)
2623 (and (> (point-max) c-state-old-cpp-beg
)
2624 (< (point-max) c-state-old-cpp-end
)))
2626 (min (point-max) c-state-old-cpp-beg
)))
2627 (while (and c-state-cache
(>= (c-state-cache-top-lparen) upper-lim
))
2628 (setq c-state-cache
(cdr c-state-cache
)))
2629 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2630 ;; RBrace and indicate we'll need to search backwards for a previous
2632 (when (and c-state-cache
2633 (consp (car c-state-cache
))
2634 (> (cdar c-state-cache
) upper-lim
))
2635 (setcar c-state-cache
(caar c-state-cache
))
2636 (setq scan-back-pos
(car c-state-cache
)))
2638 ;; The next loop jumps forward out of a nested level of parens each
2639 ;; time round; the corresponding elements in `c-state-cache' are
2640 ;; removed. `pos' is just after the brace-pair or the open paren at
2641 ;; (car c-state-cache). There can be no open parens/braces/brackets
2642 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2643 ;; due to the interface spec to this function.
2644 (setq pos
(if (and good-pos-actual-macro-end
2645 (not (eq good-pos-actual-macro-start
2647 (1+ good-pos-actual-macro-end
) ; get outside the macro as
2648 ; marked by a `category' text property.
2651 (while (and c-state-cache
2652 (< (point) (point-max)))
2654 ((null pps-state
) ; first time through
2655 (setq target-depth -
1))
2656 ((eq (car pps-state
) target-depth
) ; found closing ),},]
2657 (setq target-depth
(1- (car pps-state
))))
2658 ;; Do nothing when we've merely reached pps-point.
2664 (point) (if (< (point) pps-point
) pps-point
(point-max))
2668 (if (= (point) pps-point
)
2669 (setq pps-point-state pps-state
))
2671 (when (eq (car pps-state
) target-depth
)
2672 (setq pos
(point)) ; POS is now just after an R-paren/brace.
2674 ((and (consp (car c-state-cache
))
2675 (eq (point) (cdar c-state-cache
)))
2676 ;; We've just moved out of the paren pair containing the brace-pair
2677 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2678 ;; and is potentially where the open brace of a cons in
2679 ;; c-state-cache will be.
2680 (setq pair-beg
(car-safe (cdr c-state-cache
))
2681 c-state-cache
(cdr-safe (cdr c-state-cache
)))) ; remove {}pair + containing Lparen.
2682 ((numberp (car c-state-cache
))
2683 (setq pair-beg
(car c-state-cache
)
2684 c-state-cache
(cdr c-state-cache
))) ; remove this
2686 ((numberp (cadr c-state-cache
))
2687 (setq pair-beg
(cadr c-state-cache
)
2688 c-state-cache
(cddr c-state-cache
))) ; Remove a paren pair
2689 ; together with enclosed brace pair.
2690 ;; (t nil) ; Ignore an unmated Rparen.
2693 (if (< (point) pps-point
)
2694 (setq pps-state
(parse-partial-sexp (point) pps-point
2695 nil nil
; TARGETDEPTH, STOPBEFORE
2698 ;; If the last paren pair we moved out of was actually a brace pair,
2699 ;; insert it into `c-state-cache'.
2700 (when (and pair-beg
(eq (char-after pair-beg
) ?
{))
2701 (if (consp (car-safe c-state-cache
))
2702 (setq c-state-cache
(cdr c-state-cache
)))
2703 (setq c-state-cache
(cons (cons pair-beg pos
)
2706 (list pos scan-back-pos pps-state
)))))
2708 (defun c-remove-stale-state-cache-backwards (here cache-pos
)
2709 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2710 ;; buffer, and inform the caller of the scenario detected.
2712 ;; HERE is the position we're setting `c-state-cache' for.
2713 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2714 ;; before HERE, or a position at or near point-min which isn't in a
2717 ;; This function must only be called only when (> `c-state-cache-good-pos'
2718 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2719 ;; optimised to eliminate (or minimise) scanning between these two
2722 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2723 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2724 ;; could become so after missing elements are inserted into
2725 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2726 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2727 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2728 ;; before `here''s line, or the start of the literal containing it.
2729 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2730 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2731 ;; to scan backwards from.
2732 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2733 ;; POS and HERE which aren't recorded in `c-state-cache'.
2735 ;; The comments in this defun use "paren" to mean parenthesis or square
2736 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2738 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2740 ;; CP E here D C good
2741 (let ((pos c-state-cache-good-pos
)
2742 pa ren
; positions of "(" and ")"
2743 dropped-cons
; whether the last element dropped from `c-state-cache'
2744 ; was a cons (representing a brace-pair)
2745 good-pos
; see above.
2746 lit
; (START . END) of a literal containing some point.
2747 here-lit-start here-lit-end
; bounds of literal containing `here'
2749 here- here
+ ; start/end of macro around HERE, or HERE
2750 (here-bol (c-point 'bol here
))
2751 (too-far-back (max (- here c-state-cache-too-far
) 1)))
2753 ;; Remove completely irrelevant entries from `c-state-cache'.
2754 (while (and c-state-cache
2755 (>= (setq pa
(c-state-cache-top-lparen)) here
))
2756 (setq dropped-cons
(consp (car c-state-cache
)))
2757 (setq c-state-cache
(cdr c-state-cache
))
2759 ;; At this stage, (> pos here);
2760 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2763 ((and (consp (car c-state-cache
))
2764 (> (cdar c-state-cache
) here
))
2765 ;; CASE 1: The top of the cache is a brace pair which now encloses
2766 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2767 ;; knowledge of what's inside these braces, we have no alternative but
2768 ;; to direct the caller to scan the buffer from the opening brace.
2769 (setq pos
(caar c-state-cache
))
2770 (setcar c-state-cache pos
)
2771 (list (1+ pos
) pos t
)) ; return value. We've just converted a brace pair
2772 ; entry into a { entry, so the caller needs to
2773 ; search for a brace pair before the {.
2775 ;; `here' might be inside a literal. Check for this.
2777 (setq lit
(c-state-literal-at here
)
2778 here-lit-start
(or (car lit
) here
)
2779 here-lit-end
(or (cdr lit
) here
))
2780 ;; Has `here' just "newly entered" a macro?
2782 (goto-char here-lit-start
)
2783 (if (and (c-beginning-of-macro)
2784 (or (null c-state-old-cpp-beg
)
2785 (not (= (point) c-state-old-cpp-beg
))))
2787 (setq here-
(point))
2789 (setq here
+ (point)))
2790 (setq here- here-lit-start
2791 here
+ here-lit-end
)))
2793 ;; `here' might be nested inside any depth of parens (or brackets but
2794 ;; not braces). Scan backwards to find the outermost such opening
2795 ;; paren, if there is one. This will be the scan position to return.
2797 (narrow-to-region cache-pos
(point-max))
2798 (setq pos
(c-state-balance-parens-backwards here- here
+ pos
)))
2799 nil
)) ; for the cond
2801 ((< pos here-lit-start
)
2802 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2803 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2804 ;; a brace pair preceding this, it will already be in `c-state-cache',
2805 ;; unless there was a brace pair after it, i.e. there'll only be one to
2806 ;; scan for if we've just deleted one.
2807 (list pos
(and dropped-cons pos
) t
)) ; Return value.
2809 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2810 ;; Further forward scanning isn't needed, but we still need to find a
2811 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2814 (narrow-to-region here-bol
(point-max))
2815 (setq pos here-lit-start
)
2816 (c-safe (while (setq pa
(scan-lists pos -
1 1))
2817 (setq pos pa
)))) ; might signal
2818 nil
)) ; for the cond
2820 ((setq ren
(c-safe-scan-lists pos -
1 -
1 too-far-back
))
2821 ;; CASE 3: After a }/)/] before `here''s BOL.
2822 (list (1+ ren
) (and dropped-cons pos
) nil
)) ; Return value
2825 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2826 ;; literal containing it.
2827 (setq good-pos
(c-state-lit-beg (c-point 'bopl here-bol
)))
2828 (list good-pos
(and dropped-cons good-pos
) nil
)))))
2831 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2832 ;; Externally visible routines.
2834 (defun c-state-cache-init ()
2835 (setq c-state-cache nil
2836 c-state-cache-good-pos
1
2837 c-state-nonlit-pos-cache nil
2838 c-state-nonlit-pos-cache-limit
1
2839 c-state-brace-pair-desert nil
2841 c-state-point-min-lit-type nil
2842 c-state-point-min-lit-start nil
2843 c-state-min-scan-pos
1
2844 c-state-old-cpp-beg nil
2845 c-state-old-cpp-end nil
)
2846 (c-state-mark-point-min-literal))
2848 (defun c-invalidate-state-cache-1 (here)
2849 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2850 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2851 ;; left in a consistent state.
2853 ;; This is much like `c-whack-state-after', but it never changes a paren
2854 ;; pair element into an open paren element. Doing that would mean that the
2855 ;; new open paren wouldn't have the required preceding paren pair element.
2857 ;; This function is called from c-after-change.
2859 ;; The cache of non-literals:
2860 (if (< here c-state-nonlit-pos-cache-limit
)
2861 (setq c-state-nonlit-pos-cache-limit here
))
2864 ;; Case 1: if `here' is in a literal containing point-min, everything
2865 ;; becomes (or is already) nil.
2866 (if (or (null c-state-cache-good-pos
)
2867 (< here
(c-state-get-min-scan-pos)))
2868 (setq c-state-cache nil
2869 c-state-cache-good-pos nil
2870 c-state-min-scan-pos nil
)
2872 ;;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value below
2873 ;;; `here'. To maintain its consistency, we may need to insert a new brace
2875 (let ((here-bol (c-point 'bol here
))
2876 too-high-pa
; recorded {/(/[ next above here, or nil.
2877 dropped-cons
; was the last removed element a brace pair?
2879 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
2880 (while (and c-state-cache
2881 (>= (setq pa
(c-state-cache-top-paren)) here
))
2882 (setq dropped-cons
(consp (car c-state-cache
))
2883 too-high-pa
(c-state-cache-top-lparen)
2884 c-state-cache
(cdr c-state-cache
)))
2886 ;; Do we need to add in an earlier brace pair, having lopped one off?
2887 (if (and dropped-cons
2888 (< too-high-pa
(+ here c-state-cache-too-far
)))
2889 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol
))
2890 (setq c-state-cache-good-pos
(or (c-state-cache-after-top-paren)
2891 (c-state-get-min-scan-pos)))))
2893 ;; The brace-pair desert marker:
2894 (when (car c-state-brace-pair-desert
)
2895 (if (< here
(car c-state-brace-pair-desert
))
2896 (setq c-state-brace-pair-desert nil
)
2897 (if (< here
(cdr c-state-brace-pair-desert
))
2898 (setcdr c-state-brace-pair-desert here
)))))
2900 (defun c-parse-state-1 ()
2901 ;; Find and record all noteworthy parens between some good point earlier in
2902 ;; the file and point. That good point is at least the beginning of the
2903 ;; top-level construct we are in, or the beginning of the preceding
2904 ;; top-level construct if we aren't in one.
2906 ;; The returned value is a list of the noteworthy parens with the last one
2907 ;; first. If an element in the list is an integer, it's the position of an
2908 ;; open paren (of any type) which has not been closed before the point. If
2909 ;; an element is a cons, it gives the position of a closed BRACE paren
2910 ;; pair[*]; the car is the start brace position and the cdr is the position
2911 ;; following the closing brace. Only the last closed brace paren pair
2912 ;; before each open paren and before the point is recorded, and thus the
2913 ;; state never contains two cons elements in succession. When a close brace
2914 ;; has no matching open brace (e.g., the matching brace is outside the
2915 ;; visible region), it is not represented in the returned value.
2917 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
2918 ;; This defun explicitly treats mismatching parens/braces/brackets as
2919 ;; matching. It is the open brace which makes it a "brace" pair.
2921 ;; If POINT is within a macro, open parens and brace pairs within
2922 ;; THIS macro MIGHT be recorded. This depends on whether their
2923 ;; syntactic properties have been suppressed by
2924 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
2926 ;; Currently no characters which are given paren syntax with the
2927 ;; syntax-table property are recorded, i.e. angle bracket arglist
2928 ;; parens are never present here. Note that this might change.
2930 ;; BUG: This function doesn't cope entirely well with unbalanced
2931 ;; parens in macros. (2008-12-11: this has probably been resolved
2932 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
2933 ;; following case the brace before the macro isn't balanced with the
2940 ;; Note to maintainers: this function DOES get called with point
2941 ;; within comments and strings, so don't assume it doesn't!
2943 ;; This function might do hidden buffer changes.
2944 (let* ((here (point))
2945 (here-bopl (c-point 'bopl
))
2946 strategy
; 'forward, 'backward etc..
2947 ;; Candidate positions to start scanning from:
2948 cache-pos
; highest position below HERE already existing in
2954 scan-backward-pos scan-forward-p
) ; used for 'backward.
2955 ;; If POINT-MIN has changed, adjust the cache
2956 (unless (= (point-min) c-state-point-min
)
2957 (c-renarrow-state-cache))
2960 (setq res
(c-parse-state-get-strategy here c-state-cache-good-pos
)
2962 cache-pos
(cadr res
)
2963 start-point
(nth 2 res
))
2965 (when (eq strategy
'BOD
)
2966 (setq c-state-cache nil
2967 c-state-cache-good-pos start-point
))
2972 ((memq strategy
'(forward BOD
))
2973 (narrow-to-region (point-min) here
)
2974 (setq res
(c-remove-stale-state-cache start-point here-bopl
))
2975 (setq cache-pos
(car res
)
2976 scan-backward-pos
(cadr res
)
2977 bopl-state
(car (cddr res
))) ; will be nil if (< here-bopl
2979 (if scan-backward-pos
2980 (c-append-lower-brace-pair-to-state-cache scan-backward-pos
))
2982 (c-append-to-state-cache cache-pos
))
2983 (setq c-state-cache-good-pos
2985 (< good-pos
(- here c-state-cache-too-far
)))
2986 (c-state-cache-non-literal-place here-bopl bopl-state
)
2989 ((eq strategy
'backward
)
2990 (setq res
(c-remove-stale-state-cache-backwards here cache-pos
)
2992 scan-backward-pos
(cadr res
)
2993 scan-forward-p
(car (cddr res
)))
2994 (if scan-backward-pos
2995 (c-append-lower-brace-pair-to-state-cache
2997 (setq c-state-cache-good-pos
2999 (progn (narrow-to-region (point-min) here
)
3000 (c-append-to-state-cache good-pos
))
3002 (c-get-cache-scan-pos good-pos
))))
3004 (t ; (eq strategy 'IN-LIT)
3005 (setq c-state-cache nil
3006 c-state-cache-good-pos nil
)))))
3010 (defun c-invalidate-state-cache (here)
3011 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3013 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3014 ;; of all parens in preprocessor constructs, except for any such construct
3015 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3016 ;; worrying further about macros and template delimiters.
3017 (c-with-<-
>-as-parens-suppressed
3018 (if (and c-state-old-cpp-beg
3019 (< c-state-old-cpp-beg here
))
3020 (c-with-all-but-one-cpps-commented-out
3022 (min c-state-old-cpp-end here
)
3023 (c-invalidate-state-cache-1 here
))
3024 (c-with-cpps-commented-out
3025 (c-invalidate-state-cache-1 here
)))))
3027 (defun c-parse-state ()
3028 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3029 ;; description of the functionality and return value.
3031 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3032 ;; of all parens in preprocessor constructs, except for any such construct
3033 ;; containing point. We can then call `c-parse-state-1' without worrying
3034 ;; further about macros and template delimiters.
3035 (let (here-cpp-beg here-cpp-end
)
3037 (when (c-beginning-of-macro)
3038 (setq here-cpp-beg
(point))
3040 (> (setq here-cpp-end
(c-syntactic-end-of-macro))
3042 (setq here-cpp-beg nil here-cpp-end nil
))))
3043 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3046 (c-with-<-
>-as-parens-suppressed
3047 (if (and here-cpp-beg
(> here-cpp-end here-cpp-beg
))
3048 (c-with-all-but-one-cpps-commented-out
3049 here-cpp-beg here-cpp-end
3051 (c-with-cpps-commented-out
3052 (c-parse-state-1))))
3053 (setq c-state-old-cpp-beg
(and here-cpp-beg
(copy-marker here-cpp-beg t
))
3054 c-state-old-cpp-end
(and here-cpp-end
(copy-marker here-cpp-end t
)))
3057 ;; Debug tool to catch cache inconsistencies. This is called from
3059 (defvar c-debug-parse-state nil
)
3060 (unless (fboundp 'c-real-parse-state
)
3061 (fset 'c-real-parse-state
(symbol-function 'c-parse-state
)))
3062 (cc-bytecomp-defun c-real-parse-state)
3063 (defun c-debug-parse-state ()
3064 (let ((here (point)) (res1 (c-real-parse-state)) res2
)
3065 (let ((c-state-cache nil
)
3066 (c-state-cache-good-pos 1)
3067 (c-state-nonlit-pos-cache nil
)
3068 (c-state-nonlit-pos-cache-limit 1)
3069 (c-state-brace-pair-desert nil
)
3070 (c-state-point-min 1)
3071 (c-state-point-min-lit-type nil
)
3072 (c-state-point-min-lit-start nil
)
3073 (c-state-min-scan-pos 1)
3074 (c-state-old-cpp-beg nil
)
3075 (c-state-old-cpp-end nil
))
3076 (setq res2
(c-real-parse-state)))
3077 (unless (equal res1 res2
)
3078 ;; The cache can actually go further back due to the ad-hoc way
3079 ;; the first paren is found, so try to whack off a bit of its
3080 ;; start before complaining.
3082 (goto-char (or (c-least-enclosing-brace res2
) (point)))
3083 (c-beginning-of-defun-1)
3084 (while (not (or (bobp) (eq (char-after) ?
{)))
3085 (c-beginning-of-defun-1))
3086 (unless (equal (c-whack-state-before (point) res1
) res2
)
3087 (message (concat "c-parse-state inconsistency at %s: "
3088 "using cache: %s, from scratch: %s")
3092 (defun c-toggle-parse-state-debug (&optional arg
)
3094 (setq c-debug-parse-state
(c-calculate-state arg c-debug-parse-state
))
3095 (fset 'c-parse-state
(symbol-function (if c-debug-parse-state
3096 'c-debug-parse-state
3097 'c-real-parse-state
)))
3098 (c-keep-region-active))
3099 (when c-debug-parse-state
3100 (c-toggle-parse-state-debug 1))
3103 (defun c-whack-state-before (bufpos paren-state
)
3104 ;; Whack off any state information from PAREN-STATE which lies
3105 ;; before BUFPOS. Not destructive on PAREN-STATE.
3106 (let* ((newstate (list nil
))
3110 (setq car
(car paren-state
)
3111 paren-state
(cdr paren-state
))
3112 (if (< (if (consp car
) (car car
) car
) bufpos
)
3113 (setq paren-state nil
)
3114 (setcdr ptr
(list car
))
3115 (setq ptr
(cdr ptr
))))
3118 (defun c-whack-state-after (bufpos paren-state
)
3119 ;; Whack off any state information from PAREN-STATE which lies at or
3120 ;; after BUFPOS. Not destructive on PAREN-STATE.
3123 (let ((car (car paren-state
)))
3125 ;; just check the car, because in a balanced brace
3126 ;; expression, it must be impossible for the corresponding
3127 ;; close brace to be before point, but the open brace to
3129 (if (<= bufpos
(car car
))
3131 (if (< bufpos
(cdr car
))
3132 ;; its possible that the open brace is before
3133 ;; bufpos, but the close brace is after. In that
3134 ;; case, convert this to a non-cons element. The
3135 ;; rest of the state is before bufpos, so we're
3137 (throw 'done
(cons (car car
) (cdr paren-state
)))
3138 ;; we know that both the open and close braces are
3139 ;; before bufpos, so we also know that everything else
3140 ;; on state is before bufpos.
3141 (throw 'done paren-state
)))
3144 ;; it's before bufpos, so everything else should too.
3145 (throw 'done paren-state
)))
3146 (setq paren-state
(cdr paren-state
)))
3149 (defun c-most-enclosing-brace (paren-state &optional bufpos
)
3150 ;; Return the bufpos of the innermost enclosing open paren before
3151 ;; bufpos, or nil if none was found.
3153 (or bufpos
(setq bufpos
134217727))
3155 (setq enclosingp
(car paren-state
)
3156 paren-state
(cdr paren-state
))
3157 (if (or (consp enclosingp
)
3158 (>= enclosingp bufpos
))
3159 (setq enclosingp nil
)
3160 (setq paren-state nil
)))
3163 (defun c-least-enclosing-brace (paren-state)
3164 ;; Return the bufpos of the outermost enclosing open paren, or nil
3165 ;; if none was found.
3168 (setq elem
(car paren-state
)
3169 paren-state
(cdr paren-state
))
3174 (defun c-safe-position (bufpos paren-state
)
3175 ;; Return the closest "safe" position recorded on PAREN-STATE that
3176 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3177 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3178 ;; find the closest limit before a given limit that might be nil.
3180 ;; A "safe" position is a position at or after a recorded open
3181 ;; paren, or after a recorded close paren. The returned position is
3182 ;; thus either the first position after a close brace, or the first
3183 ;; position after an enclosing paren, or at the enclosing paren in
3184 ;; case BUFPOS is immediately after it.
3189 (setq elem
(car paren-state
))
3191 (cond ((< (cdr elem
) bufpos
)
3192 (throw 'done
(cdr elem
)))
3193 ((< (car elem
) bufpos
)
3195 (throw 'done
(min (1+ (car elem
)) bufpos
))))
3197 ;; elem is the position at and not after the opening paren, so
3198 ;; we can go forward one more step unless it's equal to
3199 ;; bufpos. This is useful in some cases avoid an extra paren
3200 ;; level between the safe position and bufpos.
3201 (throw 'done
(min (1+ elem
) bufpos
))))
3202 (setq paren-state
(cdr paren-state
)))))))
3204 (defun c-beginning-of-syntax ()
3205 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3206 ;; goes to the closest previous point that is known to be outside
3207 ;; any string literal or comment. `c-state-cache' is used if it has
3208 ;; a position in the vicinity.
3209 (let* ((paren-state c-state-cache
)
3213 ;; Note: Similar code in `c-safe-position'. The
3214 ;; difference is that we accept a safe position at
3215 ;; the point and don't bother to go forward past open
3218 (setq elem
(car paren-state
))
3220 (cond ((<= (cdr elem
) (point))
3221 (throw 'done
(cdr elem
)))
3222 ((<= (car elem
) (point))
3223 (throw 'done
(car elem
))))
3224 (if (<= elem
(point))
3225 (throw 'done elem
)))
3226 (setq paren-state
(cdr paren-state
)))
3229 (if (> pos
(- (point) 4000))
3231 ;; The position is far back. Try `c-beginning-of-defun-1'
3232 ;; (although we can't be entirely sure it will go to a position
3233 ;; outside a comment or string in current emacsen). FIXME:
3234 ;; Consult `syntax-ppss' here.
3235 (c-beginning-of-defun-1)
3240 ;; Tools for scanning identifiers and other tokens.
3242 (defun c-on-identifier ()
3243 "Return non-nil if the point is on or directly after an identifier.
3244 Keywords are recognized and not considered identifiers. If an
3245 identifier is detected, the returned value is its starting position.
3246 If an identifier ends at the point and another begins at it \(can only
3247 happen in Pike) then the point for the preceding one is returned.
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 ;; FIXME: Shouldn't this function handle "operator" in C++?
3255 (skip-syntax-backward "w_")
3259 ;; Check for a normal (non-keyword) identifier.
3260 (and (looking-at c-symbol-start
)
3261 (not (looking-at c-keywords-regexp
))
3264 (when (c-major-mode-is 'pike-mode
)
3265 ;; Handle the `<operator> syntax in Pike.
3266 (let ((pos (point)))
3267 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3268 (and (if (< (skip-chars-backward "`") 0)
3271 (eq (char-after) ?\
`))
3272 (looking-at c-symbol-key
)
3273 (>= (match-end 0) pos
)
3276 ;; Handle the "operator +" syntax in C++.
3277 (when (and c-overloadable-operators-regexp
3278 (= (c-backward-token-2 0) 0))
3280 (cond ((and (looking-at c-overloadable-operators-regexp
)
3281 (or (not c-opt-op-identifier-prefix
)
3282 (and (= (c-backward-token-2 1) 0)
3283 (looking-at c-opt-op-identifier-prefix
))))
3287 (and c-opt-op-identifier-prefix
3288 (looking-at c-opt-op-identifier-prefix
)
3289 (= (c-forward-token-2 1) 0)
3290 (looking-at c-overloadable-operators-regexp
)))
3295 (defsubst c-simple-skip-symbol-backward
()
3296 ;; If the point is at the end of a symbol then skip backward to the
3297 ;; beginning of it. Don't move otherwise. Return non-nil if point
3300 ;; This function might do hidden buffer changes.
3301 (or (< (skip-syntax-backward "w_") 0)
3302 (and (c-major-mode-is 'pike-mode
)
3303 ;; Handle the `<operator> syntax in Pike.
3304 (let ((pos (point)))
3305 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3306 (< (skip-chars-backward "`") 0)
3307 (looking-at c-symbol-key
)
3308 (>= (match-end 0) pos
))
3313 (defun c-beginning-of-current-token (&optional back-limit
)
3314 ;; Move to the beginning of the current token. Do not move if not
3315 ;; in the middle of one. BACK-LIMIT may be used to bound the
3316 ;; backward search; if given it's assumed to be at the boundary
3317 ;; between two tokens. Return non-nil if the point is moved, nil
3320 ;; This function might do hidden buffer changes.
3321 (let ((start (point)))
3322 (if (looking-at "\\w\\|\\s_")
3323 (skip-syntax-backward "w_" back-limit
)
3324 (when (< (skip-syntax-backward ".()" back-limit
) 0)
3325 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp
)
3327 ;; `c-nonsymbol-token-regexp' should always match
3328 ;; since we've skipped backward over punctuator
3329 ;; or paren syntax, but consume one char in case
3330 ;; it doesn't so that we don't leave point before
3331 ;; some earlier incorrect token.
3334 (goto-char pos
))))))
3337 (defun c-end-of-current-token (&optional back-limit
)
3338 ;; Move to the end of the current token. Do not move if not in the
3339 ;; middle of one. BACK-LIMIT may be used to bound the backward
3340 ;; search; if given it's assumed to be at the boundary between two
3341 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3343 ;; This function might do hidden buffer changes.
3344 (let ((start (point)))
3345 (cond ((< (skip-syntax-backward "w_" (1- start
)) 0)
3346 (skip-syntax-forward "w_"))
3347 ((< (skip-syntax-backward ".()" back-limit
) 0)
3349 (if (looking-at c-nonsymbol-token-regexp
)
3350 (goto-char (match-end 0))
3351 ;; `c-nonsymbol-token-regexp' should always match since
3352 ;; we've skipped backward over punctuator or paren
3353 ;; syntax, but move forward in case it doesn't so that
3354 ;; we don't leave point earlier than we started with.
3356 (< (point) start
)))))
3359 (defconst c-jump-syntax-balanced
3360 (if (memq 'gen-string-delim c-emacs-features
)
3361 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3362 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3364 (defconst c-jump-syntax-unbalanced
3365 (if (memq 'gen-string-delim c-emacs-features
)
3366 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3367 "\\w\\|\\s_\\|\\s\""))
3369 (defun c-forward-token-2 (&optional count balanced limit
)
3370 "Move forward by tokens.
3371 A token is defined as all symbols and identifiers which aren't
3372 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3373 treated properly). Point is always either left at the beginning of a
3374 token or not moved at all. COUNT specifies the number of tokens to
3375 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3376 moves to the next token beginning only if not already at one. If
3377 BALANCED is true, move over balanced parens, otherwise move into them.
3378 Also, if BALANCED is true, never move out of an enclosing paren.
3380 LIMIT sets the limit for the movement and defaults to the point limit.
3381 The case when LIMIT is set in the middle of a token, comment or macro
3382 is handled correctly, i.e. the point won't be left there.
3384 Return the number of tokens left to move \(positive or negative). If
3385 BALANCED is true, a move over a balanced paren counts as one. Note
3386 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3387 be returned. Thus, a return value of 0 guarantees that point is at
3388 the requested position and a return value less \(without signs) than
3389 COUNT guarantees that point is at the beginning of some token.
3391 Note that this function might do hidden buffer changes. See the
3392 comment at the start of cc-engine.el for more info."
3394 (or count
(setq count
1))
3396 (- (c-backward-token-2 (- count
) balanced limit
))
3398 (let ((jump-syntax (if balanced
3399 c-jump-syntax-balanced
3400 c-jump-syntax-unbalanced
))
3405 ;; If count is zero we should jump if in the middle of a token.
3406 (c-end-of-current-token))
3409 (if limit
(narrow-to-region (point-min) limit
))
3411 (progn (c-forward-syntactic-ws) (point)))
3412 ;; Skip whitespace. Count this as a move if we did in
3414 (setq count
(max (1- count
) 0)))
3417 ;; Moved out of bounds. Make sure the returned count isn't zero.
3419 (if (zerop count
) (setq count
1))
3422 ;; Use `condition-case' to avoid having the limit tests
3429 (cond ((looking-at jump-syntax
)
3430 (goto-char (scan-sexps (point) 1))
3432 ((looking-at c-nonsymbol-token-regexp
)
3433 (goto-char (match-end 0))
3435 ;; `c-nonsymbol-token-regexp' above should always
3436 ;; match if there are correct tokens. Try to
3437 ;; widen to see if the limit was set in the
3438 ;; middle of one, else fall back to treating
3439 ;; the offending thing as a one character token.
3443 (looking-at c-nonsymbol-token-regexp
)))
3448 (c-forward-syntactic-ws)
3451 (error (goto-char last
)))
3455 (setq count
(1+ count
)))))
3459 (defun c-backward-token-2 (&optional count balanced limit
)
3460 "Move backward by tokens.
3461 See `c-forward-token-2' for details."
3463 (or count
(setq count
1))
3465 (- (c-forward-token-2 (- count
) balanced limit
))
3467 (or limit
(setq limit
(point-min)))
3468 (let ((jump-syntax (if balanced
3469 c-jump-syntax-balanced
3470 c-jump-syntax-unbalanced
))
3474 ;; The count is zero so try to skip to the beginning of the
3477 (progn (c-beginning-of-current-token) (point)))
3478 (if (< (point) limit
)
3479 ;; The limit is inside the same token, so return 1.
3482 ;; We're not in the middle of a token. If there's
3483 ;; whitespace after the point then we must move backward,
3484 ;; so set count to 1 in that case.
3485 (and (looking-at c-syntactic-ws-start
)
3486 ;; If we're looking at a '#' that might start a cpp
3487 ;; directive then we have to do a more elaborate check.
3488 (or (/= (char-after) ?
#)
3489 (not c-opt-cpp-prefix
)
3492 (progn (beginning-of-line)
3493 (looking-at "[ \t]*")
3496 (progn (backward-char)
3497 (not (eq (char-before) ?
\\)))))))
3500 ;; Use `condition-case' to avoid having to check for buffer
3501 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3506 (c-backward-syntactic-ws)
3508 (if (looking-at jump-syntax
)
3509 (goto-char (scan-sexps (1+ (point)) -
1))
3510 ;; This can be very inefficient if there's a long
3511 ;; sequence of operator tokens without any separation.
3512 ;; That doesn't happen in practice, anyway.
3513 (c-beginning-of-current-token))
3514 (>= (point) limit
)))
3517 (error (goto-char last
)))
3519 (if (< (point) limit
)
3524 (defun c-forward-token-1 (&optional count balanced limit
)
3525 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3526 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3527 characters are jumped over character by character. This function is
3528 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3529 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3530 (c-forward-token-2 count balanced limit
)))
3532 (defun c-backward-token-1 (&optional count balanced limit
)
3533 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3534 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3535 characters are jumped over character by character. This function is
3536 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3537 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3538 (c-backward-token-2 count balanced limit
)))
3541 ;; Tools for doing searches restricted to syntactically relevant text.
3543 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3544 paren-level not-inside-token
3545 lookbehind-submatch
)
3546 "Like `re-search-forward', but only report matches that are found
3547 in syntactically significant text. I.e. matches in comments, macros
3548 or string literals are ignored. The start point is assumed to be
3549 outside any comment, macro or string literal, or else the content of
3550 that region is taken as syntactically significant text.
3552 If PAREN-LEVEL is non-nil, an additional restriction is added to
3553 ignore matches in nested paren sexps. The search will also not go
3554 outside the current list sexp, which has the effect that if the point
3555 should be moved to BOUND when no match is found \(i.e. NOERROR is
3556 neither nil nor t), then it will be at the closing paren if the end of
3557 the current list sexp is encountered first.
3559 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3560 ignored. Things like multicharacter operators and special symbols
3561 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3564 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3565 subexpression in REGEXP. The end of that submatch is used as the
3566 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3567 isn't used or if that subexpression didn't match then the start
3568 position of the whole match is used instead. The \"look behind\"
3569 subexpression is never tested before the starting position, so it
3570 might be a good idea to include \\=\\= as a match alternative in it.
3572 Optimization note: Matches might be missed if the \"look behind\"
3573 subexpression can match the end of nonwhite syntactic whitespace,
3574 i.e. the end of comments or cpp directives. This since the function
3575 skips over such things before resuming the search. It's on the other
3576 hand not safe to assume that the \"look behind\" subexpression never
3577 matches syntactic whitespace.
3579 Bug: Unbalanced parens inside cpp directives are currently not handled
3580 correctly \(i.e. they don't get ignored as they should) when
3583 Note that this function might do hidden buffer changes. See the
3584 comment at the start of cc-engine.el for more info."
3586 (or bound
(setq bound
(point-max)))
3587 (if paren-level
(setq paren-level -
1))
3589 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3591 (let ((start (point))
3593 ;; Start position for the last search.
3595 ;; The `parse-partial-sexp' state between the start position
3598 ;; The current position after the last state update. The next
3599 ;; `parse-partial-sexp' continues from here.
3601 ;; The position at which to check the state and the state
3602 ;; there. This is separate from `state-pos' since we might
3603 ;; need to back up before doing the next search round.
3604 check-pos check-state
3605 ;; Last position known to end a token.
3606 (last-token-end-pos (point-min))
3607 ;; Set when a valid match is found.
3614 (setq search-pos
(point))
3615 (re-search-forward regexp bound noerror
))
3618 (setq state
(parse-partial-sexp
3619 state-pos
(match-beginning 0) paren-level nil state
)
3621 (if (setq check-pos
(and lookbehind-submatch
3622 (or (not paren-level
)
3624 (match-end lookbehind-submatch
)))
3625 (setq check-state
(parse-partial-sexp
3626 state-pos check-pos paren-level nil state
))
3627 (setq check-pos state-pos
3630 ;; NOTE: If we got a look behind subexpression and get
3631 ;; an insignificant match in something that isn't
3632 ;; syntactic whitespace (i.e. strings or in nested
3633 ;; parentheses), then we can never skip more than a
3634 ;; single character from the match start position
3635 ;; (i.e. `state-pos' here) before continuing the
3636 ;; search. That since the look behind subexpression
3637 ;; might match the end of the insignificant region in
3641 ((elt check-state
7)
3642 ;; Match inside a line comment. Skip to eol. Use
3643 ;; `re-search-forward' instead of `skip-chars-forward' to get
3644 ;; the right bound behavior.
3645 (re-search-forward "[\n\r]" bound noerror
))
3647 ((elt check-state
4)
3648 ;; Match inside a block comment. Skip to the '*/'.
3649 (search-forward "*/" bound noerror
))
3651 ((and (not (elt check-state
5))
3652 (eq (char-before check-pos
) ?
/)
3653 (not (c-get-char-property (1- check-pos
) 'syntax-table
))
3654 (memq (char-after check-pos
) '(?
/ ?
*)))
3655 ;; Match in the middle of the opener of a block or line
3657 (if (= (char-after check-pos
) ?
/)
3658 (re-search-forward "[\n\r]" bound noerror
)
3659 (search-forward "*/" bound noerror
)))
3661 ;; The last `parse-partial-sexp' above might have
3662 ;; stopped short of the real check position if the end
3663 ;; of the current sexp was encountered in paren-level
3664 ;; mode. The checks above are always false in that
3665 ;; case, and since they can do better skipping in
3666 ;; lookbehind-submatch mode, we do them before
3667 ;; checking the paren level.
3670 (/= (setq tmp
(car check-state
)) 0))
3671 ;; Check the paren level first since we're short of the
3672 ;; syntactic checking position if the end of the
3673 ;; current sexp was encountered by `parse-partial-sexp'.
3676 ;; Inside a nested paren sexp.
3677 (if lookbehind-submatch
3678 ;; See the NOTE above.
3679 (progn (goto-char state-pos
) t
)
3680 ;; Skip out of the paren quickly.
3681 (setq state
(parse-partial-sexp state-pos bound
0 nil state
)
3684 ;; Have exited the current paren sexp.
3687 ;; The last `parse-partial-sexp' call above
3688 ;; has left us just after the closing paren
3689 ;; in this case, so we can modify the bound
3690 ;; to leave the point at the right position
3692 (setq bound
(1- (point)))
3694 (signal 'search-failed
(list regexp
)))))
3696 ((setq tmp
(elt check-state
3))
3697 ;; Match inside a string.
3698 (if (or lookbehind-submatch
3699 (not (integerp tmp
)))
3700 ;; See the NOTE above.
3701 (progn (goto-char state-pos
) t
)
3702 ;; Skip to the end of the string before continuing.
3703 (let ((ender (make-string 1 tmp
)) (continue t
))
3704 (while (if (search-forward ender bound noerror
)
3706 (setq state
(parse-partial-sexp
3707 state-pos
(point) nil nil state
)
3710 (setq continue nil
)))
3715 (c-beginning-of-macro start
)))
3716 ;; Match inside a macro. Skip to the end of it.
3718 (cond ((<= (point) bound
) t
)
3720 (t (signal 'search-failed
(list regexp
)))))
3722 ((and not-inside-token
3723 (or (< check-pos last-token-end-pos
)
3726 (goto-char check-pos
)
3728 (c-end-of-current-token last-token-end-pos
))
3729 (setq last-token-end-pos
(point))))))
3731 (if lookbehind-submatch
3732 ;; See the NOTE above.
3733 (goto-char state-pos
)
3734 (goto-char (min last-token-end-pos bound
))))
3741 ;; Should loop to search again, but take care to avoid
3742 ;; looping on the same spot.
3743 (or (/= search-pos
(point))
3744 (if (= (point) bound
)
3747 (signal 'search-failed
(list regexp
)))
3753 (signal (car err
) (cdr err
))))
3755 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3759 (goto-char (match-end 0))
3762 ;; Search failed. Set point as appropriate.
3768 (defvar safe-pos-list
) ; bound in c-syntactic-skip-backward
3770 (defsubst c-ssb-lit-begin
()
3771 ;; Return the start of the literal point is in, or nil.
3772 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3773 ;; bound in the caller.
3775 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3776 ;; if it's outside comments and strings.
3778 (let ((pos (point)) safe-pos state pps-end-pos
)
3779 ;; Pick a safe position as close to the point as possible.
3781 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3784 (while (and safe-pos-list
3785 (> (car safe-pos-list
) (point)))
3786 (setq safe-pos-list
(cdr safe-pos-list
)))
3787 (unless (setq safe-pos
(car-safe safe-pos-list
))
3788 (setq safe-pos
(max (or (c-safe-position
3789 (point) (or c-state-cache
3793 safe-pos-list
(list safe-pos
)))
3795 ;; Cache positions along the way to use if we have to back up more. We
3796 ;; cache every closing paren on the same level. If the paren cache is
3797 ;; relevant in this region then we're typically already on the same
3798 ;; level as the target position. Note that we might cache positions
3799 ;; after opening parens in case safe-pos is in a nested list. That's
3800 ;; both uncommon and harmless.
3802 (setq state
(parse-partial-sexp
3805 (setq safe-pos
(point)
3806 safe-pos-list
(cons safe-pos safe-pos-list
)))
3808 ;; If the state contains the start of the containing sexp we cache that
3809 ;; position too, so that parse-partial-sexp in the next run has a bigger
3810 ;; chance of starting at the same level as the target position and thus
3811 ;; will get more good safe positions into the list.
3813 (setq safe-pos
(1+ (elt state
1))
3814 safe-pos-list
(cons safe-pos safe-pos-list
)))
3816 (if (or (elt state
3) (elt state
4))
3817 ;; Inside string or comment. Continue search at the
3821 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level
)
3822 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3823 i.e. don't stop at positions inside syntactic whitespace or string
3824 literals. Preprocessor directives are also ignored, with the exception
3825 of the one that the point starts within, if any. If LIMIT is given,
3826 it's assumed to be at a syntactically relevant position.
3828 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3829 sexps, and the search will also not go outside the current paren sexp.
3830 However, if LIMIT or the buffer limit is reached inside a nested paren
3831 then the point will be left at the limit.
3833 Non-nil is returned if the point moved, nil otherwise.
3835 Note that this function might do hidden buffer changes. See the
3836 comment at the start of cc-engine.el for more info."
3838 (let ((start (point))
3840 ;; A list of syntactically relevant positions in descending
3841 ;; order. It's used to avoid scanning repeatedly over
3842 ;; potentially large regions with `parse-partial-sexp' to verify
3843 ;; each position. Used in `c-ssb-lit-begin'
3845 ;; The result from `c-beginning-of-macro' at the start position or the
3846 ;; start position itself if it isn't within a macro. Evaluated on
3849 ;; The earliest position after the current one with the same paren
3850 ;; level. Used only when `paren-level' is set.
3852 (paren-level-pos (point)))
3856 ;; The next loop "tries" to find the end point each time round,
3857 ;; loops when it hasn't succeeded.
3860 (< (skip-chars-backward skip-chars limit
) 0)
3862 (let ((pos (point)) state-2 pps-end-pos
)
3865 ;; Don't stop inside a literal
3866 ((setq lit-beg
(c-ssb-lit-begin))
3872 (setq state-2
(parse-partial-sexp
3873 pos paren-level-pos -
1)
3874 pps-end-pos
(point))
3875 (/= (car state-2
) 0)))
3876 ;; Not at the right level.
3878 (if (and (< (car state-2
) 0)
3879 ;; We stop above if we go out of a paren.
3880 ;; Now check whether it precedes or is
3881 ;; nested in the starting sexp.
3885 pps-end-pos paren-level-pos
3887 (< (car state-2
) 0)))
3889 ;; We've stopped short of the starting position
3890 ;; so the hit was inside a nested list. Go up
3891 ;; until we are at the right level.
3894 (goto-char (scan-lists pos -
1
3896 (setq paren-level-pos
(point))
3897 (if (and limit
(>= limit paren-level-pos
))
3903 (goto-char (or limit
(point-min)))
3906 ;; The hit was outside the list at the start
3907 ;; position. Go to the start of the list and exit.
3908 (goto-char (1+ (elt state-2
1)))
3911 ((c-beginning-of-macro limit
)
3915 (setq start-macro-beg
3918 (c-beginning-of-macro limit
)
3922 ;; It's inside the same macro we started in so it's
3923 ;; a relevant match.
3929 ;; Skip syntactic ws afterwards so that we don't stop at the
3930 ;; end of a comment if `skip-chars' is something like "^/".
3931 (c-backward-syntactic-ws)
3934 ;; We might want to extend this with more useful return values in
3936 (/= (point) start
)))
3938 ;; The following is an alternative implementation of
3939 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3940 ;; track of the syntactic context. It turned out to be generally
3941 ;; slower than the one above which uses forward checks from earlier
3944 ;;(defconst c-ssb-stop-re
3945 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3946 ;; ;; stop at to avoid going into comments and literals.
3948 ;; ;; Match comment end syntax and string literal syntax. Also match
3949 ;; ;; '/' for block comment endings (not covered by comment end
3951 ;; "\\s>\\|/\\|\\s\""
3952 ;; (if (memq 'gen-string-delim c-emacs-features)
3955 ;; (if (memq 'gen-comment-delim c-emacs-features)
3959 ;;(defconst c-ssb-stop-paren-re
3960 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3961 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3963 ;;(defconst c-ssb-sexp-end-re
3964 ;; ;; Regexp matching the ending syntax of a complex sexp.
3965 ;; (concat c-string-limit-regexp "\\|\\s)"))
3967 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3968 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3969 ;;i.e. don't stop at positions inside syntactic whitespace or string
3970 ;;literals. Preprocessor directives are also ignored. However, if the
3971 ;;point is within a comment, string literal or preprocessor directory to
3972 ;;begin with, its contents is treated as syntactically relevant chars.
3973 ;;If LIMIT is given, it limits the backward search and the point will be
3974 ;;left there if no earlier position is found.
3976 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3977 ;;sexps, and the search will also not go outside the current paren sexp.
3978 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3979 ;;then the point will be left at the limit.
3981 ;;Non-nil is returned if the point moved, nil otherwise.
3983 ;;Note that this function might do hidden buffer changes. See the
3984 ;;comment at the start of cc-engine.el for more info."
3986 ;; (save-restriction
3988 ;; (narrow-to-region limit (point-max)))
3990 ;; (let ((start (point)))
3992 ;; (while (let ((last-pos (point))
3994 ;; (skip-chars-backward skip-chars)
3997 ;; ;; Skip back over the same region as
3998 ;; ;; `skip-chars-backward' above, but keep to
3999 ;; ;; syntactically relevant positions.
4000 ;; (goto-char last-pos)
4002 ;; ;; `re-search-backward' with a single char regexp
4003 ;; ;; should be fast.
4004 ;; (re-search-backward
4005 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4010 ;; ((looking-at "\\s(")
4011 ;; ;; `paren-level' is set and we've found the
4012 ;; ;; start of the containing paren.
4016 ;; ((looking-at c-ssb-sexp-end-re)
4017 ;; ;; We're at the end of a string literal or paren
4018 ;; ;; sexp (if `paren-level' is set).
4020 ;; (condition-case nil
4021 ;; (c-backward-sexp)
4023 ;; (goto-char limit)
4024 ;; (throw 'done t))))
4028 ;; ;; At the end of some syntactic ws or possibly
4029 ;; ;; after a plain '/' operator.
4030 ;; (let ((pos (point)))
4031 ;; (c-backward-syntactic-ws)
4032 ;; (if (= pos (point))
4033 ;; ;; Was a plain '/' operator. Go past it.
4034 ;; (backward-char)))))
4036 ;; (> (point) stop-pos))))
4038 ;; ;; Now the point is either at `stop-pos' or at some
4039 ;; ;; position further back if `stop-pos' was at a
4040 ;; ;; syntactically irrelevant place.
4042 ;; ;; Skip additional syntactic ws so that we don't stop
4043 ;; ;; at the end of a comment if `skip-chars' is
4044 ;; ;; something like "^/".
4045 ;; (c-backward-syntactic-ws)
4047 ;; (< (point) stop-pos))))
4049 ;; ;; We might want to extend this with more useful return values
4050 ;; ;; in the future.
4051 ;; (/= (point) start))))
4054 ;; Tools for handling comments and string literals.
4056 (defun c-slow-in-literal (&optional lim detect-cpp
)
4057 "Return the type of literal point is in, if any.
4058 The return value is `c' if in a C-style comment, `c++' if in a C++
4059 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4060 is non-nil and in a preprocessor line, or nil if somewhere else.
4061 Optional LIM is used as the backward limit of the search. If omitted,
4062 or nil, `c-beginning-of-defun' is used.
4064 The last point calculated is cached if the cache is enabled, i.e. if
4065 `c-in-literal-cache' is bound to a two element vector.
4067 Note that this function might do hidden buffer changes. See the
4068 comment at the start of cc-engine.el for more info."
4070 (if (and (vectorp c-in-literal-cache
)
4071 (= (point) (aref c-in-literal-cache
0)))
4072 (aref c-in-literal-cache
1)
4073 (let ((rtn (save-excursion
4074 (let* ((pos (point))
4076 (c-beginning-of-syntax)
4078 (state (parse-partial-sexp lim pos
)))
4080 ((elt state
3) 'string
)
4081 ((elt state
4) (if (elt state
7) 'c
++ 'c
))
4082 ((and detect-cpp
(c-beginning-of-macro lim
)) 'pound
)
4084 ;; cache this result if the cache is enabled
4085 (if (not c-in-literal-cache
)
4086 (setq c-in-literal-cache
(vector (point) rtn
)))
4089 ;; XEmacs has a built-in function that should make this much quicker.
4090 ;; I don't think we even need the cache, which makes our lives more
4091 ;; complicated anyway. In this case, lim is only used to detect
4094 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
4095 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
4096 ;; Xemacs 21.4.4. It manifested itself thus:
4098 ;; Starting with an empty AWK Mode buffer, type
4100 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
4102 ;; AWK Mode is designed such that when the first / is typed, it gets the
4103 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
4104 ;; are given the s-t property "string". However, buffer-syntactic-context
4105 ;; fails to take account of the change of the s-t property on the opening / to
4106 ;; "string", and reports that the { is within a string started by the second /.
4108 ;; The workaround for this is for the AWK Mode initialisation to switch the
4109 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
4110 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
4112 ;; (Alan Mackenzie, 2003/4/30).
4114 (defun c-fast-in-literal (&optional lim detect-cpp
)
4115 ;; This function might do hidden buffer changes.
4116 (let ((context (buffer-syntactic-context)))
4118 ((eq context
'string
) 'string
)
4119 ((eq context
'comment
) 'c
++)
4120 ((eq context
'block-comment
) 'c
)
4121 ((and detect-cpp
(save-excursion (c-beginning-of-macro lim
))) 'pound
))))
4123 (defalias 'c-in-literal
4124 (if (fboundp 'buffer-syntactic-context
)
4125 'c-fast-in-literal
; XEmacs
4126 'c-slow-in-literal
)) ; GNU Emacs
4128 ;; The defalias above isn't enough to shut up the byte compiler.
4129 (cc-bytecomp-defun c-in-literal)
4131 (defun c-literal-limits (&optional lim near not-in-delimiter
)
4132 "Return a cons of the beginning and end positions of the comment or
4133 string surrounding point (including both delimiters), or nil if point
4134 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4135 to start parsing from. If NEAR is non-nil, then the limits of any
4136 literal next to point is returned. \"Next to\" means there's only
4137 spaces and tabs between point and the literal. The search for such a
4138 literal is done first in forward direction. If NOT-IN-DELIMITER is
4139 non-nil, the case when point is inside a starting delimiter won't be
4140 recognized. This only has effect for comments which have starting
4141 delimiters with more than one character.
4143 Note that this function might do hidden buffer changes. See the
4144 comment at the start of cc-engine.el for more info."
4147 (let* ((pos (point))
4149 (c-beginning-of-syntax)
4151 (state (parse-partial-sexp lim pos
)))
4153 (cond ((elt state
3) ; String.
4154 (goto-char (elt state
8))
4155 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4158 ((elt state
4) ; Comment.
4159 (goto-char (elt state
8))
4160 (cons (point) (progn (c-forward-single-comment) (point))))
4162 ((and (not not-in-delimiter
)
4164 (eq (char-before) ?
/)
4165 (looking-at "[/*]"))
4166 ;; We're standing in a comment starter.
4168 (cons (point) (progn (c-forward-single-comment) (point))))
4173 ;; Search forward for a literal.
4174 (skip-chars-forward " \t")
4177 ((looking-at c-string-limit-regexp
) ; String.
4178 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4181 ((looking-at c-comment-start-regexp
) ; Line or block comment.
4182 (cons (point) (progn (c-forward-single-comment) (point))))
4186 (skip-chars-backward " \t")
4188 (let ((end (point)) beg
)
4191 (< (skip-syntax-backward c-string-syntax
) 0)) ; String.
4192 (setq beg
(c-safe (c-backward-sexp 1) (point))))
4194 ((and (c-safe (forward-char -
2) t
)
4196 ;; Block comment. Due to the nature of line
4197 ;; comments, they will always be covered by the
4198 ;; normal case above.
4200 (c-backward-single-comment)
4201 ;; If LIM is bogus, beg will be bogus.
4202 (setq beg
(point))))
4204 (if beg
(cons beg end
))))))
4207 ;; In case external callers use this; it did have a docstring.
4208 (defalias 'c-literal-limits-fast
'c-literal-limits
)
4210 (defun c-collect-line-comments (range)
4211 "If the argument is a cons of two buffer positions (such as returned by
4212 `c-literal-limits'), and that range contains a C++ style line comment,
4213 then an extended range is returned that contains all adjacent line
4214 comments (i.e. all comments that starts in the same column with no
4215 empty lines or non-whitespace characters between them). Otherwise the
4216 argument is returned.
4218 Note that this function might do hidden buffer changes. See the
4219 comment at the start of cc-engine.el for more info."
4223 (if (and (consp range
) (progn
4224 (goto-char (car range
))
4225 (looking-at c-line-comment-starter
)))
4226 (let ((col (current-column))
4228 (bopl (c-point 'bopl
))
4230 ;; Got to take care in the backward direction to handle
4231 ;; comments which are preceded by code.
4232 (while (and (c-backward-single-comment)
4234 (looking-at c-line-comment-starter
)
4235 (= col
(current-column)))
4237 bopl
(c-point 'bopl
)))
4239 (while (and (progn (skip-chars-forward " \t")
4240 (looking-at c-line-comment-starter
))
4241 (= col
(current-column))
4242 (prog1 (zerop (forward-line 1))
4243 (setq end
(point)))))
4248 (defun c-literal-type (range)
4249 "Convenience function that given the result of `c-literal-limits',
4250 returns nil or the type of literal that the range surrounds, one
4251 of the symbols 'c, 'c++ or 'string. It's much faster than using
4252 `c-in-literal' and is intended to be used when you need both the
4253 type of a literal and its limits.
4255 Note that this function might do hidden buffer changes. See the
4256 comment at the start of cc-engine.el for more info."
4260 (goto-char (car range
))
4261 (cond ((looking-at c-string-limit-regexp
) 'string
)
4262 ((or (looking-at "//") ; c++ line comment
4263 (and (looking-at "\\s<") ; comment starter
4264 (looking-at "#"))) ; awk comment.
4266 (t 'c
))) ; Assuming the range is valid.
4270 ;; `c-find-decl-spots' and accompanying stuff.
4272 ;; Variables used in `c-find-decl-spots' to cache the search done for
4273 ;; the first declaration in the last call. When that function starts,
4274 ;; it needs to back up over syntactic whitespace to look at the last
4275 ;; token before the region being searched. That can sometimes cause
4276 ;; moves back and forth over a quite large region of comments and
4277 ;; macros, which would be repeated for each changed character when
4278 ;; we're called during fontification, since font-lock refontifies the
4279 ;; current line for each change. Thus it's worthwhile to cache the
4282 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4283 ;; the syntactic whitespace less or equal to some start position.
4284 ;; There's no cached value if it's nil.
4286 ;; `c-find-decl-match-pos' is the match position if
4287 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4288 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4289 (defvar c-find-decl-syntactic-pos nil
)
4290 (make-variable-buffer-local 'c-find-decl-syntactic-pos
)
4291 (defvar c-find-decl-match-pos nil
)
4292 (make-variable-buffer-local 'c-find-decl-match-pos
)
4294 (defsubst c-invalidate-find-decl-cache
(change-min-pos)
4295 (and c-find-decl-syntactic-pos
4296 (< change-min-pos c-find-decl-syntactic-pos
)
4297 (setq c-find-decl-syntactic-pos nil
)))
4299 ; (defface c-debug-decl-spot-face
4300 ; '((t (:background "Turquoise")))
4301 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4302 ; (defface c-debug-decl-sws-face
4303 ; '((t (:background "Khaki")))
4304 ; "Debug face to mark the syntactic whitespace between the declaration
4305 ; spots and the preceding token end.")
4307 (defmacro c-debug-put-decl-spot-faces
(match-pos decl-pos
)
4308 (when (facep 'c-debug-decl-spot-face
)
4309 `(c-save-buffer-state ((match-pos ,match-pos
) (decl-pos ,decl-pos
))
4310 (c-debug-add-face (max match-pos
(point-min)) decl-pos
4311 'c-debug-decl-sws-face
)
4312 (c-debug-add-face decl-pos
(min (1+ decl-pos
) (point-max))
4313 'c-debug-decl-spot-face
))))
4314 (defmacro c-debug-remove-decl-spot-faces
(beg end
)
4315 (when (facep 'c-debug-decl-spot-face
)
4316 `(c-save-buffer-state ()
4317 (c-debug-remove-face ,beg
,end
'c-debug-decl-spot-face
)
4318 (c-debug-remove-face ,beg
,end
'c-debug-decl-sws-face
))))
4320 (defmacro c-find-decl-prefix-search
()
4321 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4322 ;; but it contains lots of free variables that refer to things
4323 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4324 ;; if there is a match, otherwise at `cfd-limit'.
4326 ;; This macro might do hidden buffer changes.
4329 ;; Find the next property match position if we haven't got one already.
4330 (unless cfd-prop-match
4333 (goto-char (next-single-property-change
4334 (point) 'c-type nil cfd-limit
))
4335 (and (< (point) cfd-limit
)
4336 (not (eq (c-get-char-property (1- (point)) 'c-type
)
4338 (setq cfd-prop-match
(point))))
4340 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4342 (unless cfd-re-match
4344 (if (> cfd-re-match-end
(point))
4345 (goto-char cfd-re-match-end
))
4347 (while (if (setq cfd-re-match-end
4348 (re-search-forward c-decl-prefix-or-start-re
4351 ;; Match. Check if it's inside a comment or string literal.
4353 (if (setq cfd-re-match
(match-end 1))
4354 ;; Matched the end of a token preceding a decl spot.
4356 (goto-char cfd-re-match
)
4358 ;; Matched a token that start a decl spot.
4359 (goto-char (match-beginning 0))
4363 ;; No match. Finish up and exit the loop.
4364 (setq cfd-re-match cfd-limit
)
4367 ;; Skip out of comments and string literals.
4369 (goto-char (next-single-property-change
4370 (point) 'face nil cfd-limit
))
4371 (and (< (point) cfd-limit
)
4372 (c-got-face-at (point) c-literal-faces
)))))
4374 ;; If we matched at the decl start, we have to back up over the
4375 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4376 ;; any decl spots in the syntactic ws.
4377 (unless cfd-re-match
4378 (c-backward-syntactic-ws)
4379 (setq cfd-re-match
(point))))
4381 ;; Choose whichever match is closer to the start.
4382 (if (< cfd-re-match cfd-prop-match
)
4383 (setq cfd-match-pos cfd-re-match
4385 (setq cfd-match-pos cfd-prop-match
4386 cfd-prop-match nil
))
4388 (goto-char cfd-match-pos
)
4390 (when (< cfd-match-pos cfd-limit
)
4391 ;; Skip forward past comments only so we don't skip macros.
4392 (c-forward-comments)
4393 ;; Set the position to continue at. We can avoid going over
4394 ;; the comments skipped above a second time, but it's possible
4395 ;; that the comment skipping has taken us past `cfd-prop-match'
4396 ;; since the property might be used inside comments.
4397 (setq cfd-continue-pos
(if cfd-prop-match
4398 (min cfd-prop-match
(point))
4401 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun
)
4402 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4403 ;; label from the point to CFD-LIMIT.
4405 ;; CFD-FUN is called with point at the start of the spot. It's
4406 ;; passed two arguments: The first is the end position of the token
4407 ;; preceding the spot, or 0 for the implicit match at bob. The
4408 ;; second is a flag that is t when the match is inside a macro. If
4409 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
4410 ;; spot, it should return non-nil to ensure that the next search
4414 ;; o The first token after bob.
4415 ;; o The first token after the end of submatch 1 in
4416 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4417 ;; o The start of each `c-decl-prefix-or-start-re' match when
4418 ;; submatch 1 doesn't match.
4419 ;; o The first token after the end of each occurrence of the
4420 ;; `c-type' text property with the value `c-decl-end', provided
4421 ;; `c-type-decl-end-used' is set.
4423 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4424 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4425 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4427 ;; If the match is inside a macro then the buffer is narrowed to the
4428 ;; end of it, so that CFD-FUN can investigate the following tokens
4429 ;; without matching something that begins inside a macro and ends
4430 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4431 ;; CFD-FACE-CHECKLIST checks exist.
4433 ;; The spots are visited approximately in order from top to bottom.
4434 ;; It's however the positions where `c-decl-prefix-or-start-re'
4435 ;; matches and where `c-decl-end' properties are found that are in
4436 ;; order. Since the spots often are at the following token, they
4437 ;; might be visited out of order insofar as more spots are reported
4438 ;; later on within the syntactic whitespace between the match
4439 ;; positions and their spots.
4441 ;; It's assumed that comments and strings are fontified in the
4444 ;; This is mainly used in fontification, and so has an elaborate
4445 ;; cache to handle repeated calls from the same start position; see
4446 ;; the variables above.
4448 ;; All variables in this function begin with `cfd-' to avoid name
4449 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4451 ;; This function might do hidden buffer changes.
4453 (let ((cfd-start-pos (point))
4454 (cfd-buffer-end (point-max))
4455 ;; The end of the token preceding the decl spot last found
4456 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4459 ;; The end position of the last `c-decl-prefix-or-start-re'
4460 ;; match. If this is greater than `cfd-continue-pos', the
4461 ;; next regexp search is started here instead.
4462 (cfd-re-match-end (point-min))
4463 ;; The end of the last `c-decl-end' found by
4464 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4465 ;; match. If searching for the property isn't needed then we
4466 ;; disable it by setting it to `cfd-limit' directly.
4467 (cfd-prop-match (unless c-type-decl-end-used cfd-limit
))
4468 ;; The end of the token preceding the decl spot last found by
4469 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4470 ;; bob. `cfd-limit' if there's no match. In other words,
4471 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4472 (cfd-match-pos cfd-limit
)
4473 ;; The position to continue searching at.
4475 ;; The position of the last "real" token we've stopped at.
4476 ;; This can be greater than `cfd-continue-pos' when we get
4477 ;; hits inside macros or at `c-decl-end' positions inside
4480 ;; The end position of the last entered macro.
4483 ;; Initialize by finding a syntactically relevant start position
4484 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4485 ;; search unless we're at bob.
4487 (let (start-in-literal start-in-macro syntactic-pos
)
4488 ;; Must back up a bit since we look for the end of the previous
4489 ;; statement or declaration, which is earlier than the first
4493 ;; First we need to move to a syntactically relevant position.
4494 ;; Begin by backing out of comment or string literals.
4496 (when (c-got-face-at (point) c-literal-faces
)
4497 ;; Try to use the faces to back up to the start of the
4498 ;; literal. FIXME: What if the point is on a declaration
4499 ;; inside a comment?
4500 (while (and (not (bobp))
4501 (c-got-face-at (1- (point)) c-literal-faces
))
4502 (goto-char (previous-single-property-change
4503 (point) 'face nil
(point-min))))
4505 ;; XEmacs doesn't fontify the quotes surrounding string
4507 (and (featurep 'xemacs
)
4508 (eq (get-text-property (point) 'face
)
4509 'font-lock-string-face
)
4511 (progn (backward-char)
4512 (not (looking-at c-string-limit-regexp
)))
4515 ;; Don't trust the literal to contain only literal faces
4516 ;; (the font lock package might not have fontified the
4517 ;; start of it at all, for instance) so check that we have
4518 ;; arrived at something that looks like a start or else
4519 ;; resort to `c-literal-limits'.
4520 (unless (looking-at c-literal-start-regexp
)
4521 (let ((range (c-literal-limits)))
4522 (if range
(goto-char (car range
)))))
4524 (setq start-in-literal
(point)))
4526 ;; The start is in a literal. If the limit is in the same
4527 ;; one we don't have to find a syntactic position etc. We
4528 ;; only check that if the limit is at or before bonl to save
4529 ;; time; it covers the by far most common case when font-lock
4530 ;; refontifies the current line only.
4531 (<= cfd-limit
(c-point 'bonl cfd-start-pos
))
4533 (goto-char cfd-start-pos
)
4535 (goto-char (next-single-property-change
4536 (point) 'face nil cfd-limit
))
4537 (and (< (point) cfd-limit
)
4538 (c-got-face-at (point) c-literal-faces
))))
4539 (= (point) cfd-limit
)))
4541 ;; Completely inside a literal. Set up variables to trig the
4542 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4543 ;; find a suitable start position.
4544 (setq cfd-continue-pos start-in-literal
))
4546 ;; Check if the region might be completely inside a macro, to
4547 ;; optimize that like the completely-inside-literal above.
4549 (and (= (forward-line 1) 0)
4550 (bolp) ; forward-line has funny behavior at eob.
4551 (>= (point) cfd-limit
)
4552 (progn (backward-char)
4553 (eq (char-before) ?
\\))))
4554 ;; (Maybe) completely inside a macro. Only need to trig the
4555 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4557 (setq cfd-continue-pos
(1- cfd-start-pos
)
4561 ;; Back out of any macro so we don't miss any declaration
4562 ;; that could follow after it.
4563 (when (c-beginning-of-macro)
4564 (setq start-in-macro t
))
4566 ;; Now we're at a proper syntactically relevant position so we
4567 ;; can use the cache. But first clear it if it applied
4569 (c-invalidate-find-decl-cache cfd-start-pos
)
4571 (setq syntactic-pos
(point))
4572 (unless (eq syntactic-pos c-find-decl-syntactic-pos
)
4573 ;; Don't have to do this if the cache is relevant here,
4574 ;; typically if the same line is refontified again. If
4575 ;; we're just some syntactic whitespace further down we can
4576 ;; still use the cache to limit the skipping.
4577 (c-backward-syntactic-ws c-find-decl-syntactic-pos
))
4579 ;; If we hit `c-find-decl-syntactic-pos' and
4580 ;; `c-find-decl-match-pos' is set then we install the cached
4581 ;; values. If we hit `c-find-decl-syntactic-pos' and
4582 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4583 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4584 ;; and so we can continue the search from this point. If we
4585 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4586 ;; the right spot to begin searching anyway.
4587 (if (and (eq (point) c-find-decl-syntactic-pos
)
4588 c-find-decl-match-pos
)
4589 (setq cfd-match-pos c-find-decl-match-pos
4590 cfd-continue-pos syntactic-pos
)
4592 (setq c-find-decl-syntactic-pos syntactic-pos
)
4595 ;; Always consider bob a match to get the first
4596 ;; declaration in the file. Do this separately instead of
4597 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4598 ;; regexp always can consume at least one character to
4599 ;; ensure that we won't get stuck in an infinite loop.
4600 (setq cfd-re-match
0)
4602 (c-beginning-of-current-token)
4603 (< (point) cfd-limit
))
4604 ;; Do an initial search now. In the bob case above it's
4605 ;; only done to search for a `c-decl-end' spot.
4606 (c-find-decl-prefix-search))
4608 (setq c-find-decl-match-pos
(and (< cfd-match-pos cfd-start-pos
)
4611 ;; Advance `cfd-continue-pos' if it's before the start position.
4612 ;; The closest continue position that might have effect at or
4613 ;; after the start depends on what we started in. This also
4614 ;; finds a suitable start position in the special cases when the
4615 ;; region is completely within a literal or macro.
4616 (when (and cfd-continue-pos
(< cfd-continue-pos cfd-start-pos
))
4620 ;; If we're in a macro then it's the closest preceding token
4621 ;; in the macro. Check this before `start-in-literal',
4622 ;; since if we're inside a literal in a macro, the preceding
4623 ;; token is earlier than any `c-decl-end' spot inside the
4624 ;; literal (comment).
4625 (goto-char (or start-in-literal cfd-start-pos
))
4626 ;; The only syntactic ws in macros are comments.
4627 (c-backward-comments)
4629 (c-beginning-of-current-token))
4632 ;; If we're in a comment it can only be the closest
4633 ;; preceding `c-decl-end' position within that comment, if
4634 ;; any. Go back to the beginning of such a property so that
4635 ;; `c-find-decl-prefix-search' will find the end of it.
4636 ;; (Can't stop at the end and install it directly on
4637 ;; `cfd-prop-match' since that variable might be cleared
4638 ;; after `cfd-fun' below.)
4640 ;; Note that if the literal is a string then the property
4641 ;; search will simply skip to the beginning of it right
4643 (if (not c-type-decl-end-used
)
4644 (goto-char start-in-literal
)
4645 (goto-char cfd-start-pos
)
4647 (goto-char (previous-single-property-change
4648 (point) 'c-type nil start-in-literal
))
4649 (and (> (point) start-in-literal
)
4650 (not (eq (c-get-char-property (point) 'c-type
)
4653 (when (= (point) start-in-literal
)
4654 ;; Didn't find any property inside the comment, so we can
4655 ;; skip it entirely. (This won't skip past a string, but
4656 ;; that'll be handled quickly by the next
4657 ;; `c-find-decl-prefix-search' anyway.)
4658 (c-forward-single-comment)
4659 (if (> (point) cfd-limit
)
4660 (goto-char cfd-limit
))))
4663 ;; If we started in normal code, the only match that might
4664 ;; apply before the start is what we already got in
4665 ;; `cfd-match-pos' so we can continue at the start position.
4666 ;; (Note that we don't get here if the first match is below
4668 (goto-char cfd-start-pos
)))
4670 ;; Delete found matches if they are before our new continue
4671 ;; position, so that `c-find-decl-prefix-search' won't back up
4672 ;; to them later on.
4673 (setq cfd-continue-pos
(point))
4674 (when (and cfd-re-match
(< cfd-re-match cfd-continue-pos
))
4675 (setq cfd-re-match nil
))
4676 (when (and cfd-prop-match
(< cfd-prop-match cfd-continue-pos
))
4677 (setq cfd-prop-match nil
)))
4680 ;; This is the normal case and we got a proper syntactic
4681 ;; position. If there's a match then it's always outside
4682 ;; macros and comments, so advance to the next token and set
4683 ;; `cfd-token-pos'. The loop below will later go back using
4684 ;; `cfd-continue-pos' to fix declarations inside the
4686 (when (and cfd-match-pos
(< cfd-match-pos syntactic-pos
))
4687 (goto-char syntactic-pos
)
4688 (c-forward-syntactic-ws)
4689 (and cfd-continue-pos
4690 (< cfd-continue-pos
(point))
4691 (setq cfd-token-pos
(point))))
4693 ;; Have one of the special cases when the region is completely
4694 ;; within a literal or macro. `cfd-continue-pos' is set to a
4695 ;; good start position for the search, so do it.
4696 (c-find-decl-prefix-search)))
4698 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4702 (< cfd-match-pos cfd-limit
)
4705 ;; Kludge to filter out matches on the "<" that
4706 ;; aren't open parens, for the sake of languages
4707 ;; that got `c-recognize-<>-arglists' set.
4708 (and (eq (char-before cfd-match-pos
) ?
<)
4709 (not (c-get-char-property (1- cfd-match-pos
)
4712 ;; If `cfd-continue-pos' is less or equal to
4713 ;; `cfd-token-pos', we've got a hit inside a macro
4714 ;; that's in the syntactic whitespace before the last
4715 ;; "real" declaration we've checked. If they're equal
4716 ;; we've arrived at the declaration a second time, so
4717 ;; there's nothing to do.
4718 (= cfd-continue-pos cfd-token-pos
)
4721 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4722 ;; we're still searching for declarations embedded in
4723 ;; the syntactic whitespace. In that case we need
4724 ;; only to skip comments and not macros, since they
4725 ;; can't be nested, and that's already been done in
4726 ;; `c-find-decl-prefix-search'.
4727 (when (> cfd-continue-pos cfd-token-pos
)
4728 (c-forward-syntactic-ws)
4729 (setq cfd-token-pos
(point)))
4731 ;; Continue if the following token fails the
4732 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4733 (when (or (>= (point) cfd-limit
)
4734 (not (looking-at cfd-decl-re
))
4735 (and cfd-face-checklist
4737 (point) cfd-face-checklist
))))
4738 (goto-char cfd-continue-pos
)
4741 (< (point) cfd-limit
))
4742 (c-find-decl-prefix-search))
4744 (< (point) cfd-limit
))
4747 (>= (point) cfd-start-pos
)
4750 ;; Narrow to the end of the macro if we got a hit inside
4751 ;; one, to avoid recognizing things that start inside the
4752 ;; macro and end outside it.
4753 (when (> cfd-match-pos cfd-macro-end
)
4754 ;; Not in the same macro as in the previous round.
4756 (goto-char cfd-match-pos
)
4758 (if (save-excursion (and (c-beginning-of-macro)
4759 (< (point) cfd-match-pos
)))
4760 (progn (c-end-of-macro)
4764 (if (zerop cfd-macro-end
)
4766 (if (> cfd-macro-end
(point))
4767 (progn (narrow-to-region (point-min) cfd-macro-end
)
4769 ;; The matched token was the last thing in the macro,
4770 ;; so the whole match is bogus.
4771 (setq cfd-macro-end
0)
4774 (c-debug-put-decl-spot-faces cfd-match-pos
(point))
4775 (if (funcall cfd-fun cfd-match-pos
(/= cfd-macro-end
0))
4776 (setq cfd-prop-match nil
))
4778 (when (/= cfd-macro-end
0)
4779 ;; Restore limits if we did macro narrowment above.
4780 (narrow-to-region (point-min) cfd-buffer-end
)))
4782 (goto-char cfd-continue-pos
)
4783 (if (= cfd-continue-pos cfd-limit
)
4784 (setq cfd-match-pos cfd-limit
)
4785 (c-find-decl-prefix-search)))))
4788 ;; A cache for found types.
4790 ;; Buffer local variable that contains an obarray with the types we've
4791 ;; found. If a declaration is recognized somewhere we record the
4792 ;; fully qualified identifier in it to recognize it as a type
4793 ;; elsewhere in the file too. This is not accurate since we do not
4794 ;; bother with the scoping rules of the languages, but in practice the
4795 ;; same name is seldom used as both a type and something else in a
4796 ;; file, and we only use this as a last resort in ambiguous cases (see
4797 ;; `c-forward-decl-or-cast-1').
4799 ;; Not every type need be in this cache. However, things which have
4800 ;; ceased to be types must be removed from it.
4802 ;; Template types in C++ are added here too but with the template
4803 ;; arglist replaced with "<>" in references or "<" for the one in the
4804 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4805 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4806 ;; template specs can be fairly sized programs in themselves) and
4807 ;; improves the hit ratio (it's a type regardless of the template
4808 ;; args; it's just not the same type, but we're only interested in
4809 ;; recognizing types, not telling distinct types apart). Note that
4810 ;; template types in references are added here too; from the example
4811 ;; above there will also be an entry "Foo<".
4812 (defvar c-found-types nil
)
4813 (make-variable-buffer-local 'c-found-types
)
4815 (defsubst c-clear-found-types
()
4816 ;; Clears `c-found-types'.
4817 (setq c-found-types
(make-vector 53 0)))
4819 (defun c-add-type (from to
)
4820 ;; Add the given region as a type in `c-found-types'. If the region
4821 ;; doesn't match an existing type but there is a type which is equal
4822 ;; to the given one except that the last character is missing, then
4823 ;; the shorter type is removed. That's done to avoid adding all
4824 ;; prefixes of a type as it's being entered and font locked. This
4825 ;; doesn't cover cases like when characters are removed from a type
4826 ;; or added in the middle. We'd need the position of point when the
4827 ;; font locking is invoked to solve this well.
4829 ;; This function might do hidden buffer changes.
4830 (let ((type (c-syntactic-content from to c-recognize-
<>-arglists
)))
4831 (unless (intern-soft type c-found-types
)
4832 (unintern (substring type
0 -
1) c-found-types
)
4833 (intern type c-found-types
))))
4835 (defun c-unfind-type (name)
4836 ;; Remove the "NAME" from c-found-types, if present.
4837 (unintern name c-found-types
))
4839 (defsubst c-check-type
(from to
)
4840 ;; Return non-nil if the given region contains a type in
4843 ;; This function might do hidden buffer changes.
4844 (intern-soft (c-syntactic-content from to c-recognize-
<>-arglists
)
4847 (defun c-list-found-types ()
4848 ;; Return all the types in `c-found-types' as a sorted list of
4851 (mapatoms (lambda (type)
4852 (setq type-list
(cons (symbol-name type
)
4855 (sort type-list
'string-lessp
)))
4857 ;; Shut up the byte compiler.
4858 (defvar c-maybe-stale-found-type
)
4860 (defun c-trim-found-types (beg end old-len
)
4861 ;; An after change function which, in conjunction with the info in
4862 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4863 ;; from `c-found-types', should this type have become stale. For
4864 ;; example, this happens to "foo" when "foo \n bar();" becomes
4865 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4866 ;; the fontification.
4868 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4872 (when (< end
(point-max))
4874 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4875 (progn (goto-char end
)
4876 (c-end-of-current-token)))
4877 (c-unfind-type (buffer-substring-no-properties
4879 (when (> beg
(point-min))
4881 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4882 (progn (goto-char beg
)
4883 (c-beginning-of-current-token)))
4884 (c-unfind-type (buffer-substring-no-properties
4887 (if c-maybe-stale-found-type
; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4889 ;; Changing the amount of (already existing) whitespace - don't do anything.
4890 ((and (c-partial-ws-p beg end
)
4891 (or (= beg end
) ; removal of WS
4892 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type
)))))
4894 ;; The syntactic relationship which defined a "found type" has been
4896 ((eq (car c-maybe-stale-found-type
) 'c-decl-id-start
)
4897 (c-unfind-type (cadr c-maybe-stale-found-type
)))
4898 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4902 ;; Setting and removing syntax properties on < and > in languages (C++
4903 ;; and Java) where they can be template/generic delimiters as well as
4904 ;; their normal meaning of "less/greater than".
4906 ;; Normally, < and > have syntax 'punctuation'. When they are found to
4907 ;; be delimiters, they are marked as such with the category properties
4908 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
4912 ;; It is impossible to determine with certainty whether a <..> pair in
4913 ;; C++ is two comparison operators or is template delimiters, unless
4914 ;; one duplicates a lot of a C++ compiler. For example, the following
4917 ;; foo (a < b, c > d) ;
4919 ;; could be a function call with two integer parameters (each a
4920 ;; relational expression), or it could be a constructor for class foo
4921 ;; taking one parameter d of templated type "a < b, c >". They are
4922 ;; somewhat easier to distinguish in Java.
4924 ;; The strategy now (2010-01) adopted is to mark and unmark < and
4925 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
4926 ;; individually when their context so indicated. This gave rise to
4927 ;; intractible problems when one of a matching pair was deleted, or
4928 ;; pulled into a literal.]
4930 ;; At each buffer change, the syntax-table properties are removed in a
4931 ;; before-change function and reapplied, when needed, in an
4932 ;; after-change function. It is far more important that the
4933 ;; properties get removed when they they are spurious than that they
4934 ;; be present when wanted.
4935 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4936 (defun c-clear-<-pair-props
(&optional pos
)
4937 ;; POS (default point) is at a < character. If it is marked with
4938 ;; open paren syntax-table text property, remove the property,
4939 ;; together with the close paren property on the matching > (if
4945 (when (equal (c-get-char-property (point) 'syntax-table
)
4946 c-
<-as-paren-syntax
)
4947 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
4948 (c-go-list-forward))
4949 (when (equal (c-get-char-property (1- (point)) 'syntax-table
)
4950 c-
>-as-paren-syntax
) ; should always be true.
4951 (c-clear-char-property (1- (point)) 'category
))
4952 (c-clear-char-property pos
'category
))))
4954 (defun c-clear->-pair-props
(&optional pos
)
4955 ;; POS (default point) is at a > character. If it is marked with
4956 ;; close paren syntax-table property, remove the property, together
4957 ;; with the open paren property on the matching < (if any).
4962 (when (equal (c-get-char-property (point) 'syntax-table
)
4963 c-
>-as-paren-syntax
)
4964 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
4965 (c-go-up-list-backward))
4966 (when (equal (c-get-char-property (point) 'syntax-table
)
4967 c-
<-as-paren-syntax
) ; should always be true.
4968 (c-clear-char-property (point) 'category
))
4969 (c-clear-char-property pos
'category
))))
4971 (defun c-clear-<>-pair-props
(&optional pos
)
4972 ;; POS (default point) is at a < or > character. If it has an
4973 ;; open/close paren syntax-table property, remove this property both
4974 ;; from the current character and its partner (which will also be
4977 ((eq (char-after) ?\
<)
4978 (c-clear-<-pair-props pos
))
4979 ((eq (char-after) ?\
>)
4980 (c-clear->-pair-props pos
))
4982 "c-clear-<>-pair-props called from wrong position"))))
4984 (defun c-clear-<-pair-props-if-match-after
(lim &optional pos
)
4985 ;; POS (default point) is at a < character. If it is both marked
4986 ;; with open/close paren syntax-table property, and has a matching >
4987 ;; (also marked) which is after LIM, remove the property both from
4988 ;; the current > and its partner. Return t when this happens, nil
4994 (when (equal (c-get-char-property (point) 'syntax-table
)
4995 c-
<-as-paren-syntax
)
4996 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
4997 (c-go-list-forward))
4998 (when (and (>= (point) lim
)
4999 (equal (c-get-char-property (1- (point)) 'syntax-table
)
5000 c-
>-as-paren-syntax
)) ; should always be true.
5001 (c-unmark-<-
>-as-paren
(1- (point)))
5002 (c-unmark-<-
>-as-paren pos
))
5005 (defun c-clear->-pair-props-if-match-before
(lim &optional pos
)
5006 ;; POS (default point) is at a > character. If it is both marked
5007 ;; with open/close paren syntax-table property, and has a matching <
5008 ;; (also marked) which is before LIM, remove the property both from
5009 ;; the current < and its partner. Return t when this happens, nil
5015 (when (equal (c-get-char-property (point) 'syntax-table
)
5016 c-
>-as-paren-syntax
)
5017 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
5018 (c-go-up-list-backward))
5019 (when (and (<= (point) lim
)
5020 (equal (c-get-char-property (point) 'syntax-table
)
5021 c-
<-as-paren-syntax
)) ; should always be true.
5022 (c-unmark-<-
>-as-paren
(point))
5023 (c-unmark-<-
>-as-paren pos
))
5026 ;; Set by c-common-init in cc-mode.el.
5030 (defun c-before-change-check-<>-operators
(beg end
)
5031 ;; Unmark certain pairs of "< .... >" which are currently marked as
5032 ;; template/generic delimiters. (This marking is via syntax-table
5033 ;; text properties).
5035 ;; These pairs are those which are in the current "statement" (i.e.,
5036 ;; the region between the {, }, or ; before BEG and the one after
5037 ;; END), and which enclose any part of the interval (BEG END).
5039 ;; Note that in C++ (?and Java), template/generic parens cannot
5040 ;; enclose a brace or semicolon, so we use these as bounds on the
5041 ;; region we must work on.
5043 ;; This function is called from before-change-functions (via
5044 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5045 ;; and point is undefined, both at entry and exit.
5047 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5050 (let ((beg-lit-limits (progn (goto-char beg
) (c-literal-limits)))
5051 (end-lit-limits (progn (goto-char end
) (c-literal-limits)))
5052 new-beg new-end need-new-beg need-new-end
)
5053 ;; Locate the barrier before the changed region
5054 (goto-char (if beg-lit-limits
(car beg-lit-limits
) beg
))
5055 (c-syntactic-skip-backward "^;{}" (max (- beg
2048) (point-min)))
5056 (setq new-beg
(point))
5058 ;; Remove the syntax-table properties from each pertinent <...> pair.
5059 ;; Firsly, the ones with the < before beg and > after beg.
5060 (while (c-search-forward-char-property 'category
'c-
<-as-paren-syntax beg
)
5061 (if (c-clear-<-pair-props-if-match-after beg
(1- (point)))
5062 (setq need-new-beg t
)))
5064 ;; Locate the barrier after END.
5065 (goto-char (if end-lit-limits
(cdr end-lit-limits
) end
))
5066 (c-syntactic-re-search-forward "[;{}]"
5067 (min (+ end
2048) (point-max)) 'end
)
5068 (setq new-end
(point))
5070 ;; Remove syntax-table properties from the remaining pertinent <...>
5071 ;; pairs, those with a > after end and < before end.
5072 (while (c-search-backward-char-property 'category
'c-
>-as-paren-syntax end
)
5073 (if (c-clear->-pair-props-if-match-before end
)
5074 (setq need-new-end t
)))
5076 ;; Extend the fontification region, if needed.
5079 (c-forward-syntactic-ws)
5080 (and (< (point) c-new-BEG
) (setq c-new-BEG
(point))))
5083 (and (> new-end c-new-END
) (setq c-new-END new-end
))))))
5087 (defun c-after-change-check-<>-operators
(beg end
)
5088 ;; This is called from `after-change-functions' when
5089 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5090 ;; chars with paren syntax become part of another operator like "<<"
5093 ;; This function might do hidden buffer changes.
5097 (when (or (looking-at "[<>]")
5098 (< (skip-chars-backward "<>") 0))
5101 (c-beginning-of-current-token)
5102 (when (and (< (point) beg
)
5103 (looking-at c-
<>-multichar-token-regexp
)
5104 (< beg
(setq beg
(match-end 0))))
5105 (while (progn (skip-chars-forward "^<>" beg
)
5107 (c-clear-<>-pair-props
)
5112 (when (or (looking-at "[<>]")
5113 (< (skip-chars-backward "<>") 0))
5116 (c-beginning-of-current-token)
5117 (when (and (< (point) end
)
5118 (looking-at c-
<>-multichar-token-regexp
)
5119 (< end
(setq end
(match-end 0))))
5120 (while (progn (skip-chars-forward "^<>" end
)
5122 (c-clear-<>-pair-props
)
5123 (forward-char)))))))
5127 ;; Handling of small scale constructs like types and names.
5129 ;; Dynamically bound variable that instructs `c-forward-type' to also
5130 ;; treat possible types (i.e. those that it normally returns 'maybe or
5131 ;; 'found for) as actual types (and always return 'found for them).
5132 ;; This means that it records them in `c-record-type-identifiers' if
5133 ;; that is set, and that it adds them to `c-found-types'.
5134 (defvar c-promote-possible-types nil
)
5136 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5137 ;; mark up successfully parsed arglists with paren syntax properties on
5138 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5139 ;; `c-type' property of each argument separating comma.
5141 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5142 ;; all arglists for side effects (i.e. recording types), otherwise it
5143 ;; exploits any existing paren syntax properties to quickly jump to the
5144 ;; end of already parsed arglists.
5146 ;; Marking up the arglists is not the default since doing that correctly
5147 ;; depends on a proper value for `c-restricted-<>-arglists'.
5148 (defvar c-parse-and-markup-
<>-arglists nil
)
5150 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5151 ;; not accept arglists that contain binary operators.
5153 ;; This is primarily used to handle C++ template arglists. C++
5154 ;; disambiguates them by checking whether the preceding name is a
5155 ;; template or not. We can't do that, so we assume it is a template
5156 ;; if it can be parsed as one. That usually works well since
5157 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5158 ;; in almost all cases would be pointless.
5160 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5161 ;; should let the comma separate the function arguments instead. And
5162 ;; in a context where the value of the expression is taken, e.g. in
5163 ;; "if (a < b || c > d)", it's probably not a template.
5164 (defvar c-restricted-
<>-arglists nil
)
5166 ;; Dynamically bound variables that instructs
5167 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5168 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5169 ;; `c-forward-label' to record the ranges of all the type and
5170 ;; reference identifiers they encounter. They will build lists on
5171 ;; these variables where each element is a cons of the buffer
5172 ;; positions surrounding each identifier. This recording is only
5173 ;; activated when `c-record-type-identifiers' is non-nil.
5175 ;; All known types that can't be identifiers are recorded, and also
5176 ;; other possible types if `c-promote-possible-types' is set.
5177 ;; Recording is however disabled inside angle bracket arglists that
5178 ;; are encountered inside names and other angle bracket arglists.
5179 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5182 ;; Only the names in C++ template style references (e.g. "tmpl" in
5183 ;; "tmpl<a,b>::foo") are recorded as references, other references
5184 ;; aren't handled here.
5186 ;; `c-forward-label' records the label identifier(s) on
5187 ;; `c-record-ref-identifiers'.
5188 (defvar c-record-type-identifiers nil
)
5189 (defvar c-record-ref-identifiers nil
)
5191 ;; This variable will receive a cons cell of the range of the last
5192 ;; single identifier symbol stepped over by `c-forward-name' if it's
5193 ;; successful. This is the range that should be put on one of the
5194 ;; record lists above by the caller. It's assigned nil if there's no
5195 ;; such symbol in the name.
5196 (defvar c-last-identifier-range nil
)
5198 (defmacro c-record-type-id
(range)
5199 (if (eq (car-safe range
) 'cons
)
5201 `(setq c-record-type-identifiers
5202 (cons ,range c-record-type-identifiers
))
5203 `(let ((range ,range
))
5205 (setq c-record-type-identifiers
5206 (cons range c-record-type-identifiers
))))))
5208 (defmacro c-record-ref-id
(range)
5209 (if (eq (car-safe range
) 'cons
)
5211 `(setq c-record-ref-identifiers
5212 (cons ,range c-record-ref-identifiers
))
5213 `(let ((range ,range
))
5215 (setq c-record-ref-identifiers
5216 (cons range c-record-ref-identifiers
))))))
5218 ;; Dynamically bound variable that instructs `c-forward-type' to
5219 ;; record the ranges of types that only are found. Behaves otherwise
5220 ;; like `c-record-type-identifiers'.
5221 (defvar c-record-found-types nil
)
5223 (defmacro c-forward-keyword-prefixed-id
(type)
5224 ;; Used internally in `c-forward-keyword-clause' to move forward
5225 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5226 ;; possibly is prefixed by keywords and their associated clauses.
5227 ;; Try with a type/name first to not trip up on those that begin
5228 ;; with a keyword. Return t if a known or found type is moved
5229 ;; over. The point is clobbered if nil is returned. If range
5230 ;; recording is enabled, the identifier is recorded on as a type
5231 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5233 ;; This macro might do hidden buffer changes.
5235 (while (if (setq res
,(if (eq type
'type
)
5239 (and (looking-at c-keywords-regexp
)
5240 (c-forward-keyword-clause 1))))
5241 (when (memq res
'(t known found prefix
))
5242 ,(when (eq type
'ref
)
5243 `(when c-record-type-identifiers
5244 (c-record-ref-id c-last-identifier-range
)))
5247 (defmacro c-forward-id-comma-list
(type update-safe-pos
)
5248 ;; Used internally in `c-forward-keyword-clause' to move forward
5249 ;; over a comma separated list of types or names using
5250 ;; `c-forward-keyword-prefixed-id'.
5252 ;; This macro might do hidden buffer changes.
5254 ,(when update-safe-pos
5255 `(setq safe-pos
(point)))
5256 (eq (char-after) ?
,))
5259 (c-forward-syntactic-ws)
5260 (c-forward-keyword-prefixed-id ,type
)))))
5262 (defun c-forward-keyword-clause (match)
5263 ;; Submatch MATCH in the current match data is assumed to surround a
5264 ;; token. If it's a keyword, move over it and any immediately
5265 ;; following clauses associated with it, stopping at the start of
5266 ;; the next token. t is returned in that case, otherwise the point
5267 ;; stays and nil is returned. The kind of clauses that are
5268 ;; recognized are those specified by `c-type-list-kwds',
5269 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5270 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5271 ;; and `c-<>-arglist-kwds'.
5273 ;; This function records identifier ranges on
5274 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5275 ;; `c-record-type-identifiers' is non-nil.
5277 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5278 ;; apply directly after the keyword, the type list is moved over
5279 ;; only when there is no unaccounted token before it (i.e. a token
5280 ;; that isn't moved over due to some other keyword list). The
5281 ;; identifier ranges in the list are still recorded if that should
5284 ;; This function might do hidden buffer changes.
5286 (let ((kwd-sym (c-keyword-sym (match-string match
))) safe-pos pos
5287 ;; The call to `c-forward-<>-arglist' below is made after
5288 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5289 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5290 ;; should therefore be nil.
5291 (c-parse-and-markup-<>-arglists t
)
5292 c-restricted-
<>-arglists
)
5295 (goto-char (match-end match
))
5296 (c-forward-syntactic-ws)
5297 (setq safe-pos
(point))
5300 ((and (c-keyword-member kwd-sym
'c-type-list-kwds
)
5301 (c-forward-keyword-prefixed-id type
))
5302 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5303 (c-forward-id-comma-list type t
))
5305 ((and (c-keyword-member kwd-sym
'c-ref-list-kwds
)
5306 (c-forward-keyword-prefixed-id ref
))
5307 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5308 (c-forward-id-comma-list ref t
))
5310 ((and (c-keyword-member kwd-sym
'c-paren-any-kwds
)
5311 (eq (char-after) ?\
())
5312 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5315 (when (and (setq pos
(c-up-list-forward))
5316 (eq (char-before pos
) ?\
)))
5317 (when (and c-record-type-identifiers
5318 (c-keyword-member kwd-sym
'c-paren-type-kwds
))
5319 ;; Use `c-forward-type' on every identifier we can find
5320 ;; inside the paren, to record the types.
5321 (while (c-syntactic-re-search-forward c-symbol-start pos t
)
5322 (goto-char (match-beginning 0))
5323 (unless (c-forward-type)
5324 (looking-at c-symbol-key
) ; Always matches.
5325 (goto-char (match-end 0)))))
5328 (c-forward-syntactic-ws)
5329 (setq safe-pos
(point))))
5331 ((and (c-keyword-member kwd-sym
'c-
<>-sexp-kwds
)
5332 (eq (char-after) ?
<)
5333 (c-forward-<>-arglist
(c-keyword-member kwd-sym
'c-
<>-type-kwds
)))
5334 (c-forward-syntactic-ws)
5335 (setq safe-pos
(point)))
5337 ((and (c-keyword-member kwd-sym
'c-nonsymbol-sexp-kwds
)
5338 (not (looking-at c-symbol-start
))
5339 (c-safe (c-forward-sexp) t
))
5340 (c-forward-syntactic-ws)
5341 (setq safe-pos
(point))))
5343 (when (c-keyword-member kwd-sym
'c-colon-type-list-kwds
)
5344 (if (eq (char-after) ?
:)
5345 ;; If we are at the colon already, we move over the type
5349 (c-forward-syntactic-ws)
5350 (when (c-forward-keyword-prefixed-id type
)
5351 (c-forward-id-comma-list type t
)))
5352 ;; Not at the colon, so stop here. But the identifier
5353 ;; ranges in the type list later on should still be
5355 (and c-record-type-identifiers
5357 ;; If a keyword matched both one of the types above and
5358 ;; this one, we match `c-colon-type-list-re' after the
5359 ;; clause matched above.
5360 (goto-char safe-pos
)
5361 (looking-at c-colon-type-list-re
))
5363 (goto-char (match-end 0))
5364 (c-forward-syntactic-ws)
5365 (c-forward-keyword-prefixed-id type
))
5366 ;; There's a type after the `c-colon-type-list-re' match
5367 ;; after a keyword in `c-colon-type-list-kwds'.
5368 (c-forward-id-comma-list type nil
))))
5370 (goto-char safe-pos
)
5373 ;; cc-mode requires cc-fonts.
5374 (declare-function c-fontify-recorded-types-and-refs
"cc-fonts" ())
5376 (defun c-forward-<>-arglist
(all-types)
5377 ;; The point is assumed to be at a "<". Try to treat it as the open
5378 ;; paren of an angle bracket arglist and move forward to the
5379 ;; corresponding ">". If successful, the point is left after the
5380 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5381 ;; returned. If ALL-TYPES is t then all encountered arguments in
5382 ;; the arglist that might be types are treated as found types.
5384 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5385 ;; function handles text properties on the angle brackets and argument
5386 ;; separating commas.
5388 ;; `c-restricted-<>-arglists' controls how lenient the template
5389 ;; arglist recognition should be.
5391 ;; This function records identifier ranges on
5392 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5393 ;; `c-record-type-identifiers' is non-nil.
5395 ;; This function might do hidden buffer changes.
5397 (let ((start (point))
5398 ;; If `c-record-type-identifiers' is set then activate
5399 ;; recording of any found types that constitute an argument in
5401 (c-record-found-types (if c-record-type-identifiers t
)))
5402 (if (catch 'angle-bracket-arglist-escape
5403 (setq c-record-found-types
5404 (c-forward-<>-arglist-recur all-types
)))
5406 (when (consp c-record-found-types
)
5407 (setq c-record-type-identifiers
5408 ;; `nconc' doesn't mind that the tail of
5409 ;; `c-record-found-types' is t.
5410 (nconc c-record-found-types c-record-type-identifiers
)))
5411 (if (c-major-mode-is 'java-mode
) (c-fontify-recorded-types-and-refs))
5417 (defun c-forward-<>-arglist-recur
(all-types)
5418 ;; Recursive part of `c-forward-<>-arglist'.
5420 ;; This function might do hidden buffer changes.
5422 (let ((start (point)) res pos tmp
5423 ;; Cover this so that any recorded found type ranges are
5424 ;; automatically lost if it turns out to not be an angle
5425 ;; bracket arglist. It's propagated through the return value
5426 ;; on successful completion.
5427 (c-record-found-types c-record-found-types
)
5428 ;; List that collects the positions after the argument
5429 ;; separating ',' in the arglist.
5431 ;; If the '<' has paren open syntax then we've marked it as an angle
5432 ;; bracket arglist before, so skip to the end.
5433 (if (and (not c-parse-and-markup-
<>-arglists
)
5434 (c-get-char-property (point) 'syntax-table
))
5438 (if (and (c-go-up-list-forward)
5439 (eq (char-before) ?
>))
5441 ;; Got unmatched paren angle brackets. We don't clear the paren
5442 ;; syntax properties and retry, on the basis that it's very
5443 ;; unlikely that paren angle brackets become operators by code
5444 ;; manipulation. It's far more likely that it doesn't match due
5445 ;; to narrowing or some temporary change.
5451 (unless (looking-at c-
<-op-cont-regexp
)
5454 (c-forward-syntactic-ws)
5455 (let ((orig-record-found-types c-record-found-types
))
5456 (when (or (and c-record-type-identifiers all-types
)
5457 (c-major-mode-is 'java-mode
))
5458 ;; All encountered identifiers are types, so set the
5459 ;; promote flag and parse the type.
5461 (c-forward-syntactic-ws)
5462 (if (looking-at "\\?")
5464 (when (looking-at c-identifier-start
)
5465 (let ((c-promote-possible-types t
)
5466 (c-record-found-types t
))
5469 (c-forward-syntactic-ws)
5471 (when (or (looking-at "extends")
5472 (looking-at "super"))
5474 (c-forward-syntactic-ws)
5475 (let ((c-promote-possible-types t
)
5476 (c-record-found-types t
))
5478 (c-forward-syntactic-ws))))))
5482 ;; Note: These regexps exploit the match order in \| so
5483 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5484 (c-syntactic-re-search-forward
5485 ;; Stop on ',', '|', '&', '+' and '-' to catch
5486 ;; common binary operators that could be between
5487 ;; two comparison expressions "a<b" and "c>d".
5488 "[<;{},|+&-]\\|[>)]"
5492 ((eq (char-before) ?
>)
5493 ;; Either an operator starting with '>' or the end of
5494 ;; the angle bracket arglist.
5496 (if (looking-at c-
>-op-cont-regexp
)
5498 (goto-char (match-end 0))
5499 t
) ; Continue the loop.
5501 ;; The angle bracket arglist is finished.
5502 (when c-parse-and-markup-
<>-arglists
5503 (while arg-start-pos
5504 (c-put-c-type-property (1- (car arg-start-pos
))
5506 (setq arg-start-pos
(cdr arg-start-pos
)))
5507 (c-mark-<-as-paren start
)
5508 (c-mark->-as-paren
(1- (point))))
5510 nil
)) ; Exit the loop.
5512 ((eq (char-before) ?
<)
5513 ;; Either an operator starting with '<' or a nested arglist.
5515 (let (id-start id-end subres keyword-match
)
5516 (if (if (looking-at c-
<-op-cont-regexp
)
5517 (setq tmp
(match-end 0))
5524 ;; There's always an identifier before an angle
5525 ;; bracket arglist, or a keyword in
5526 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
5527 (c-backward-syntactic-ws)
5528 (setq id-end
(point))
5529 (c-simple-skip-symbol-backward)
5530 (when (or (setq keyword-match
5531 (looking-at c-opt-
<>-sexp-key
))
5532 (not (looking-at c-keywords-regexp
)))
5533 (setq id-start
(point))))
5536 (let ((c-promote-possible-types t
)
5537 (c-record-found-types t
))
5538 (c-forward-<>-arglist-recur
5541 (c-keyword-sym (match-string 1))
5542 'c-
<>-type-kwds
)))))
5545 ;; It was not an angle bracket arglist.
5548 ;; It was an angle bracket arglist.
5549 (setq c-record-found-types subres
)
5551 ;; Record the identifier before the template as a type
5552 ;; or reference depending on whether the arglist is last
5553 ;; in a qualified identifier.
5554 (when (and c-record-type-identifiers
5555 (not keyword-match
))
5556 (if (and c-opt-identifier-concat-key
5558 (c-forward-syntactic-ws)
5559 (looking-at c-opt-identifier-concat-key
)))
5560 (c-record-ref-id (cons id-start id-end
))
5561 (c-record-type-id (cons id-start id-end
))))))
5564 ((and (not c-restricted-
<>-arglists
)
5565 (or (and (eq (char-before) ?
&)
5566 (not (eq (char-after) ?
&)))
5567 (eq (char-before) ?
,)))
5568 ;; Just another argument. Record the position. The
5569 ;; type check stuff that made us stop at it is at
5570 ;; the top of the loop.
5571 (setq arg-start-pos
(cons (point) arg-start-pos
)))
5574 ;; Got a character that can't be in an angle bracket
5575 ;; arglist argument. Abort using `throw', since
5576 ;; it's useless to try to find a surrounding arglist
5578 (throw 'angle-bracket-arglist-escape nil
))))))
5580 (or c-record-found-types t
)))))
5582 (defun c-backward-<>-arglist
(all-types &optional limit
)
5583 ;; The point is assumed to be directly after a ">". Try to treat it
5584 ;; as the close paren of an angle bracket arglist and move back to
5585 ;; the corresponding "<". If successful, the point is left at
5586 ;; the "<" and t is returned, otherwise the point isn't moved and
5587 ;; nil is returned. ALL-TYPES is passed on to
5588 ;; `c-forward-<>-arglist'.
5590 ;; If the optional LIMIT is given, it bounds the backward search.
5591 ;; It's then assumed to be at a syntactically relevant position.
5593 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5594 ;; function for more details.
5596 (let ((start (point)))
5598 (if (and (not c-parse-and-markup-
<>-arglists
)
5599 (c-get-char-property (point) 'syntax-table
))
5601 (if (and (c-go-up-list-backward)
5602 (eq (char-after) ?
<))
5604 ;; See corresponding note in `c-forward-<>-arglist'.
5609 (c-syntactic-skip-backward "^<;{}" limit t
)
5612 (if (eq (char-before) ?
<)
5614 ;; Stopped at bob or a char that isn't allowed in an
5615 ;; arglist, so we've failed.
5620 (progn (c-beginning-of-current-token)
5622 ;; If we moved then the "<" was part of some
5623 ;; multicharacter token.
5627 (let ((beg-pos (point)))
5628 (if (c-forward-<>-arglist all-types
)
5629 (cond ((= (point) start
)
5630 ;; Matched the arglist. Break the while.
5634 ;; We started from a non-paren ">" inside an
5639 ;; Matched a shorter arglist. Can be a nested
5640 ;; one so continue looking.
5645 (/= (point) start
))))
5647 (defun c-forward-name ()
5648 ;; Move forward over a complete name if at the beginning of one,
5649 ;; stopping at the next following token. A keyword, as such,
5650 ;; doesn't count as a name. If the point is not at something that
5651 ;; is recognized as a name then it stays put.
5653 ;; A name could be something as simple as "foo" in C or something as
5654 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5655 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5656 ;; int>::*volatile const" in C++ (this function is actually little
5657 ;; more than a `looking-at' call in all modes except those that,
5658 ;; like C++, have `c-recognize-<>-arglists' set).
5661 ;; o - nil if no name is found;
5662 ;; o - 'template if it's an identifier ending with an angle bracket
5664 ;; o - 'operator of it's an operator identifier;
5665 ;; o - t if it's some other kind of name.
5667 ;; This function records identifier ranges on
5668 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5669 ;; `c-record-type-identifiers' is non-nil.
5671 ;; This function might do hidden buffer changes.
5673 (let ((pos (point)) (start (point)) res id-start id-end
5674 ;; Turn off `c-promote-possible-types' here since we might
5675 ;; call `c-forward-<>-arglist' and we don't want it to promote
5676 ;; every suspect thing in the arglist to a type. We're
5677 ;; typically called from `c-forward-type' in this case, and
5678 ;; the caller only wants the top level type that it finds to
5680 c-promote-possible-types
)
5683 (looking-at c-identifier-key
)
5686 ;; Check for keyword. We go to the last symbol in
5687 ;; `c-identifier-key' first.
5688 (goto-char (setq id-end
(match-end 0)))
5689 (c-simple-skip-symbol-backward)
5690 (setq id-start
(point))
5692 (if (looking-at c-keywords-regexp
)
5693 (when (and (c-major-mode-is 'c
++-mode
)
5695 (cc-eval-when-compile
5696 (concat "\\(operator\\|\\(template\\)\\)"
5697 "\\(" (c-lang-const c-nonsymbol-key c
++)
5699 (if (match-beginning 2)
5700 ;; "template" is only valid inside an
5701 ;; identifier if preceded by "::".
5703 (c-backward-syntactic-ws)
5704 (and (c-safe (backward-char 2) t
)
5708 ;; Handle a C++ operator or template identifier.
5710 (c-forward-syntactic-ws)
5711 (cond ((eq (char-before id-end
) ?e
)
5712 ;; Got "... ::template".
5713 (let ((subres (c-forward-name)))
5718 ((looking-at c-identifier-start
)
5719 ;; Got a cast operator.
5720 (when (c-forward-type)
5723 ;; Now we should match a sequence of either
5724 ;; '*', '&' or a name followed by ":: *",
5725 ;; where each can be followed by a sequence
5726 ;; of `c-opt-type-modifier-key'.
5727 (while (cond ((looking-at "[*&]")
5728 (goto-char (match-end 0))
5730 ((looking-at c-identifier-start
)
5731 (and (c-forward-name)
5734 (goto-char (match-end 0))
5735 (c-forward-syntactic-ws)
5736 (eq (char-after) ?
*))
5741 (c-forward-syntactic-ws)
5743 (looking-at c-opt-type-modifier-key
))
5744 (goto-char (match-end 1))))))
5746 ((looking-at c-overloadable-operators-regexp
)
5747 ;; Got some other operator.
5748 (setq c-last-identifier-range
5749 (cons (point) (match-end 0)))
5750 (goto-char (match-end 0))
5751 (c-forward-syntactic-ws)
5757 ;; `id-start' is equal to `id-end' if we've jumped over
5758 ;; an identifier that doesn't end with a symbol token.
5759 ;; That can occur e.g. for Java import directives on the
5760 ;; form "foo.bar.*".
5761 (when (and id-start
(/= id-start id-end
))
5762 (setq c-last-identifier-range
5763 (cons id-start id-end
)))
5765 (c-forward-syntactic-ws)
5771 (when (or c-opt-identifier-concat-key
5772 c-recognize-
<>-arglists
)
5775 ((and c-opt-identifier-concat-key
5776 (looking-at c-opt-identifier-concat-key
))
5777 ;; Got a concatenated identifier. This handles the
5778 ;; cases with tricky syntactic whitespace that aren't
5779 ;; covered in `c-identifier-key'.
5780 (goto-char (match-end 0))
5781 (c-forward-syntactic-ws)
5784 ((and c-recognize-
<>-arglists
5785 (eq (char-after) ?
<))
5786 ;; Maybe an angle bracket arglist.
5787 (when (let ((c-record-type-identifiers t
)
5788 (c-record-found-types t
))
5789 (c-forward-<>-arglist nil
))
5791 (c-add-type start
(1+ pos
))
5792 (c-forward-syntactic-ws)
5794 c-last-identifier-range nil
)
5796 (if (and c-opt-identifier-concat-key
5797 (looking-at c-opt-identifier-concat-key
))
5799 ;; Continue if there's an identifier concatenation
5800 ;; operator after the template argument.
5802 (when (and c-record-type-identifiers id-start
)
5803 (c-record-ref-id (cons id-start id-end
)))
5805 (c-forward-syntactic-ws)
5808 (when (and c-record-type-identifiers id-start
)
5809 (c-record-type-id (cons id-start id-end
)))
5810 (setq res
'template
)
5817 (defun c-forward-type (&optional brace-block-too
)
5818 ;; Move forward over a type spec if at the beginning of one,
5819 ;; stopping at the next following token. The keyword "typedef"
5820 ;; isn't part of a type spec here.
5822 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
5823 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
5824 ;; The current (2009-03-10) intention is to convert all uses of
5825 ;; `c-forward-type' to call with this parameter set, then to
5829 ;; o - t if it's a known type that can't be a name or other
5831 ;; o - 'known if it's an otherwise known type (according to
5832 ;; `*-font-lock-extra-types');
5833 ;; o - 'prefix if it's a known prefix of a type;
5834 ;; o - 'found if it's a type that matches one in `c-found-types';
5835 ;; o - 'maybe if it's an identfier that might be a type; or
5836 ;; o - nil if it can't be a type (the point isn't moved then).
5838 ;; The point is assumed to be at the beginning of a token.
5840 ;; Note that this function doesn't skip past the brace definition
5841 ;; that might be considered part of the type, e.g.
5842 ;; "enum {a, b, c} foo".
5844 ;; This function records identifier ranges on
5845 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5846 ;; `c-record-type-identifiers' is non-nil.
5848 ;; This function might do hidden buffer changes.
5849 (when (and c-recognize-
<>-arglists
5851 (c-forward-<>-arglist t
)
5852 (c-forward-syntactic-ws))
5854 (let ((start (point)) pos res name-res id-start id-end id-range
)
5856 ;; Skip leading type modifiers. If any are found we know it's a
5857 ;; prefix of a type.
5858 (when c-opt-type-modifier-key
; e.g. "const" "volatile", but NOT "typedef"
5859 (while (looking-at c-opt-type-modifier-key
)
5860 (goto-char (match-end 1))
5861 (c-forward-syntactic-ws)
5862 (setq res
'prefix
)))
5865 ((looking-at c-type-prefix-key
) ; e.g. "struct", "class", but NOT
5867 (goto-char (match-end 1))
5868 (c-forward-syntactic-ws)
5871 (setq name-res
(c-forward-name))
5872 (setq res
(not (null name-res
)))
5873 (when (eq name-res t
)
5874 ;; In many languages the name can be used without the
5875 ;; prefix, so we add it to `c-found-types'.
5876 (c-add-type pos
(point))
5877 (when (and c-record-type-identifiers
5878 c-last-identifier-range
)
5879 (c-record-type-id c-last-identifier-range
)))
5880 (when (and brace-block-too
5882 (eq (char-after) ?\
{)
5885 (progn (c-forward-sexp)
5886 (c-forward-syntactic-ws)
5887 (setq pos
(point))))))
5890 (unless res
(goto-char start
))) ; invalid syntax
5894 (if (looking-at c-identifier-start
)
5896 (setq id-start
(point)
5897 name-res
(c-forward-name))
5899 (setq id-end
(point)
5900 id-range c-last-identifier-range
))))
5901 (and (cond ((looking-at c-primitive-type-key
)
5903 ((c-with-syntax-table c-identifier-syntax-table
5904 (looking-at c-known-type-key
))
5909 (goto-char (match-end 1))
5910 (c-forward-syntactic-ws)
5911 (setq pos
(point))))
5914 ;; Looking at a primitive or known type identifier. We've
5915 ;; checked for a name first so that we don't go here if the
5916 ;; known type match only is a prefix of another name.
5918 (setq id-end
(match-end 1))
5920 (when (and c-record-type-identifiers
5921 (or c-promote-possible-types
(eq res t
)))
5922 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
5924 (if (and c-opt-type-component-key
5926 (looking-at c-opt-type-component-key
)))
5927 ;; There might be more keywords for the type.
5929 (c-forward-keyword-clause 1)
5931 (setq safe-pos
(point))
5932 (looking-at c-opt-type-component-key
))
5933 (when (and c-record-type-identifiers
5934 (looking-at c-primitive-type-key
))
5935 (c-record-type-id (cons (match-beginning 1)
5937 (c-forward-keyword-clause 1))
5938 (if (looking-at c-primitive-type-key
)
5940 (when c-record-type-identifiers
5941 (c-record-type-id (cons (match-beginning 1)
5943 (c-forward-keyword-clause 1)
5945 (goto-char safe-pos
)
5946 (setq res
'prefix
)))
5947 (unless (save-match-data (c-forward-keyword-clause 1))
5950 (goto-char (match-end 1))
5951 (c-forward-syntactic-ws)))))
5954 (cond ((eq name-res t
)
5955 ;; A normal identifier.
5957 (if (or res c-promote-possible-types
)
5959 (c-add-type id-start id-end
)
5960 (when (and c-record-type-identifiers id-range
)
5961 (c-record-type-id id-range
))
5964 (setq res
(if (c-check-type id-start id-end
)
5965 ;; It's an identifier that has been used as
5966 ;; a type somewhere else.
5968 ;; It's an identifier that might be a type.
5970 ((eq name-res
'template
)
5971 ;; A template is a type.
5975 ;; Otherwise it's an operator identifier, which is not a type.
5980 ;; Skip trailing type modifiers. If any are found we know it's
5982 (when c-opt-type-modifier-key
5983 (while (looking-at c-opt-type-modifier-key
) ; e.g. "const", "volatile"
5984 (goto-char (match-end 1))
5985 (c-forward-syntactic-ws)
5987 ;; Step over any type suffix operator. Do not let the existence
5988 ;; of these alter the classification of the found type, since
5989 ;; these operators typically are allowed in normal expressions
5991 (when c-opt-type-suffix-key
5992 (while (looking-at c-opt-type-suffix-key
)
5993 (goto-char (match-end 1))
5994 (c-forward-syntactic-ws)))
5996 (when c-opt-type-concat-key
; Only/mainly for pike.
5997 ;; Look for a trailing operator that concatenates the type
5998 ;; with a following one, and if so step past that one through
5999 ;; a recursive call. Note that we don't record concatenated
6000 ;; types in `c-found-types' - it's the component types that
6001 ;; are recorded when appropriate.
6003 (let* ((c-promote-possible-types (or (memq res
'(t known
))
6004 c-promote-possible-types
))
6005 ;; If we can't promote then set `c-record-found-types' so that
6006 ;; we can merge in the types from the second part afterwards if
6007 ;; it turns out to be a known type there.
6008 (c-record-found-types (and c-record-type-identifiers
6009 (not c-promote-possible-types
)))
6011 (if (and (looking-at c-opt-type-concat-key
)
6014 (goto-char (match-end 1))
6015 (c-forward-syntactic-ws)
6016 (setq subres
(c-forward-type))))
6019 ;; If either operand certainly is a type then both are, but we
6020 ;; don't let the existence of the operator itself promote two
6021 ;; uncertain types to a certain one.
6024 (unless (eq name-res
'template
)
6025 (c-add-type id-start id-end
))
6026 (when (and c-record-type-identifiers id-range
)
6027 (c-record-type-id id-range
))
6038 (when (and (eq res t
)
6039 (consp c-record-found-types
))
6040 ;; Merge in the ranges of any types found by the second
6041 ;; `c-forward-type'.
6042 (setq c-record-type-identifiers
6043 ;; `nconc' doesn't mind that the tail of
6044 ;; `c-record-found-types' is t.
6045 (nconc c-record-found-types
6046 c-record-type-identifiers
))))
6050 (when (and c-record-found-types
(memq res
'(known found
)) id-range
)
6051 (setq c-record-found-types
6052 (cons id-range c-record-found-types
))))
6054 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6058 (defun c-forward-annotation ()
6059 ;; Used for Java code only at the moment. Assumes point is on the
6060 ;; @, moves forward an annotation. returns nil if there is no
6061 ;; annotation at point.
6062 (and (looking-at "@")
6063 (progn (forward-char) t
)
6065 (progn (c-forward-syntactic-ws) t
)
6066 (if (looking-at "(")
6071 ;; Handling of large scale constructs like statements and declarations.
6073 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6074 ;; defsubst or perhaps even a defun, but it contains lots of free
6075 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6076 (defmacro c-fdoc-shift-type-backward
(&optional short
)
6077 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6078 ;; of types when parsing a declaration, which means that it
6079 ;; sometimes consumes the identifier in the declaration as a type.
6080 ;; This is used to "backtrack" and make the last type be treated as
6081 ;; an identifier instead.
6084 ;; These identifiers are bound only in the inner let.
6085 '(setq identifier-type at-type
6086 identifier-start type-start
6090 got-suffix-after-parens id-start
6093 (if (setq at-type
(if (eq backup-at-type
'prefix
)
6096 (setq type-start backup-type-start
6097 id-start backup-id-start
)
6098 (setq type-start start-pos
6099 id-start start-pos
))
6101 ;; When these flags already are set we've found specifiers that
6102 ;; unconditionally signal these attributes - backtracking doesn't
6103 ;; change that. So keep them set in that case.
6105 (setq at-type-decl backup-at-type-decl
))
6107 (setq maybe-typeless backup-maybe-typeless
))
6110 ;; This identifier is bound only in the inner let.
6111 '(setq start id-start
))))
6113 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end
)
6114 ;; Move forward over a declaration or a cast if at the start of one.
6115 ;; The point is assumed to be at the start of some token. Nil is
6116 ;; returned if no declaration or cast is recognized, and the point
6117 ;; is clobbered in that case.
6119 ;; If a declaration is parsed:
6121 ;; The point is left at the first token after the first complete
6122 ;; declarator, if there is one. The return value is a cons where
6123 ;; the car is the position of the first token in the declarator. (See
6124 ;; below for the cdr.)
6127 ;; void foo (int a, char *b) stuff ...
6131 ;; unsigned int a = c_style_initializer, b;
6133 ;; unsigned int a (cplusplus_style_initializer), b;
6134 ;; car ^ ^ point (might change)
6135 ;; class Foo : public Bar {}
6137 ;; class PikeClass (int a, string b) stuff ...
6143 ;; void cplusplus_function (int x) throw (Bad);
6145 ;; Foo::Foo (int b) : Base (b) {}
6148 ;; The cdr of the return value is non-nil when a
6149 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6150 ;; Specifically it is a dotted pair (A . B) where B is t when a
6151 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6152 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6153 ;; specifier is present. I.e., (some of) the declared
6154 ;; identifier(s) are types.
6156 ;; If a cast is parsed:
6158 ;; The point is left at the first token after the closing paren of
6159 ;; the cast. The return value is `cast'. Note that the start
6160 ;; position must be at the first token inside the cast parenthesis
6163 ;; PRECEDING-TOKEN-END is the first position after the preceding
6164 ;; token, i.e. on the other side of the syntactic ws from the point.
6165 ;; Use a value less than or equal to (point-min) if the point is at
6166 ;; the first token in (the visible part of) the buffer.
6168 ;; CONTEXT is a symbol that describes the context at the point:
6169 ;; 'decl In a comma-separated declaration context (typically
6170 ;; inside a function declaration arglist).
6171 ;; '<> In an angle bracket arglist.
6172 ;; 'arglist Some other type of arglist.
6173 ;; nil Some other context or unknown context. Includes
6174 ;; within the parens of an if, for, ... construct.
6176 ;; LAST-CAST-END is the first token after the closing paren of a
6177 ;; preceding cast, or nil if none is known. If
6178 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6179 ;; the position after the closest preceding call where a cast was
6180 ;; matched. In that case it's used to discover chains of casts like
6183 ;; This function records identifier ranges on
6184 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6185 ;; `c-record-type-identifiers' is non-nil.
6187 ;; This function might do hidden buffer changes.
6189 (let (;; `start-pos' is used below to point to the start of the
6190 ;; first type, i.e. after any leading specifiers. It might
6191 ;; also point at the beginning of the preceding syntactic
6194 ;; Set to the result of `c-forward-type'.
6196 ;; The position of the first token in what we currently
6197 ;; believe is the type in the declaration or cast, after any
6198 ;; specifiers and their associated clauses.
6200 ;; The position of the first token in what we currently
6201 ;; believe is the declarator for the first identifier. Set
6202 ;; when the type is found, and moved forward over any
6203 ;; `c-decl-hangon-kwds' and their associated clauses that
6204 ;; occurs after the type.
6206 ;; These store `at-type', `type-start' and `id-start' of the
6207 ;; identifier before the one in those variables. The previous
6208 ;; identifier might turn out to be the real type in a
6209 ;; declaration if the last one has to be the declarator in it.
6210 ;; If `backup-at-type' is nil then the other variables have
6211 ;; undefined values.
6212 backup-at-type backup-type-start backup-id-start
6213 ;; Set if we've found a specifier (apart from "typedef") that makes
6214 ;; the defined identifier(s) types.
6216 ;; Set if we've a "typedef" keyword.
6218 ;; Set if we've found a specifier that can start a declaration
6219 ;; where there's no type.
6221 ;; If a specifier is found that also can be a type prefix,
6222 ;; these flags are set instead of those above. If we need to
6223 ;; back up an identifier, they are copied to the real flag
6224 ;; variables. Thus they only take effect if we fail to
6225 ;; interpret it as a type.
6226 backup-at-type-decl backup-maybe-typeless
6227 ;; Whether we've found a declaration or a cast. We might know
6228 ;; this before we've found the type in it. It's 'ids if we've
6229 ;; found two consecutive identifiers (usually a sure sign, but
6230 ;; we should allow that in labels too), and t if we've found a
6231 ;; specifier keyword (a 100% sure sign).
6233 ;; Set when we need to back up to parse this as a declaration
6234 ;; but not as a cast.
6236 ;; For casts, the return position.
6238 ;; Save `c-record-type-identifiers' and
6239 ;; `c-record-ref-identifiers' since ranges are recorded
6240 ;; speculatively and should be thrown away if it turns out
6241 ;; that it isn't a declaration or cast.
6242 (save-rec-type-ids c-record-type-identifiers
)
6243 (save-rec-ref-ids c-record-ref-identifiers
))
6245 (while (c-forward-annotation)
6246 (c-forward-syntactic-ws))
6248 ;; Check for a type. Unknown symbols are treated as possible
6249 ;; types, but they could also be specifiers disguised through
6250 ;; macros like __INLINE__, so we recognize both types and known
6251 ;; specifiers after them too.
6253 (let* ((start (point)) kwd-sym kwd-clause-end found-type
)
6255 ;; Look for a specifier keyword clause.
6256 (when (looking-at c-prefix-spec-kwds-re
)
6257 (if (looking-at c-typedef-key
)
6258 (setq at-typedef t
))
6259 (setq kwd-sym
(c-keyword-sym (match-string 1)))
6261 (c-forward-keyword-clause 1)
6262 (setq kwd-clause-end
(point))))
6264 (when (setq found-type
(c-forward-type t
)) ; brace-block-too
6265 ;; Found a known or possible type or a prefix of a known type.
6268 ;; Got two identifiers with nothing but whitespace
6269 ;; between them. That can only happen in declarations.
6270 (setq at-decl-or-cast
'ids
)
6272 (when (eq at-type
'found
)
6273 ;; If the previous identifier is a found type we
6274 ;; record it as a real one; it might be some sort of
6275 ;; alias for a prefix like "unsigned".
6277 (goto-char type-start
)
6278 (let ((c-promote-possible-types t
))
6279 (c-forward-type)))))
6281 (setq backup-at-type at-type
6282 backup-type-start type-start
6283 backup-id-start id-start
6287 ;; The previous ambiguous specifier/type turned out
6288 ;; to be a type since we've parsed another one after
6289 ;; it, so clear these backup flags.
6290 backup-at-type-decl nil
6291 backup-maybe-typeless nil
))
6295 ;; Handle known specifier keywords and
6296 ;; `c-decl-hangon-kwds' which can occur after known
6299 (if (c-keyword-member kwd-sym
'c-decl-hangon-kwds
)
6300 ;; It's a hang-on keyword that can occur anywhere.
6302 (setq at-decl-or-cast t
)
6304 ;; Move the identifier start position if
6305 ;; we've passed a type.
6306 (setq id-start kwd-clause-end
)
6307 ;; Otherwise treat this as a specifier and
6308 ;; move the fallback position.
6309 (setq start-pos kwd-clause-end
))
6310 (goto-char kwd-clause-end
))
6312 ;; It's an ordinary specifier so we know that
6313 ;; anything before this can't be the type.
6314 (setq backup-at-type nil
6315 start-pos kwd-clause-end
)
6318 ;; It's ambiguous whether this keyword is a
6319 ;; specifier or a type prefix, so set the backup
6320 ;; flags. (It's assumed that `c-forward-type'
6321 ;; moved further than `c-forward-keyword-clause'.)
6323 (when (c-keyword-member kwd-sym
'c-typedef-decl-kwds
)
6324 (setq backup-at-type-decl t
))
6325 (when (c-keyword-member kwd-sym
'c-typeless-decl-kwds
)
6326 (setq backup-maybe-typeless t
)))
6328 (when (c-keyword-member kwd-sym
'c-typedef-decl-kwds
)
6329 ;; This test only happens after we've scanned a type.
6330 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6331 (setq at-type-decl t
))
6332 (when (c-keyword-member kwd-sym
'c-typeless-decl-kwds
)
6333 (setq maybe-typeless t
))
6335 ;; Haven't matched a type so it's an umambiguous
6336 ;; specifier keyword and we know we're in a
6338 (setq at-decl-or-cast t
)
6340 (goto-char kwd-clause-end
))))
6342 ;; If the type isn't known we continue so that we'll jump
6343 ;; over all specifiers and type identifiers. The reason
6344 ;; to do this for a known type prefix is to make things
6345 ;; like "unsigned INT16" work.
6346 (and found-type
(not (eq found-type t
))))))
6350 ;; If a known type was found, we still need to skip over any
6351 ;; hangon keyword clauses after it. Otherwise it has already
6352 ;; been done in the loop above.
6353 (while (looking-at c-decl-hangon-key
)
6354 (c-forward-keyword-clause 1))
6355 (setq id-start
(point)))
6357 ((eq at-type
'prefix
)
6358 ;; A prefix type is itself a primitive type when it's not
6359 ;; followed by another type.
6363 ;; Got no type but set things up to continue anyway to handle
6364 ;; the various cases when a declaration doesn't start with a
6366 (setq id-start start-pos
))
6368 ((and (eq at-type
'maybe
)
6369 (c-major-mode-is 'c
++-mode
))
6370 ;; If it's C++ then check if the last "type" ends on the form
6371 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6372 ;; (con|de)structor.
6374 (let (name end-2 end-1
)
6375 (goto-char id-start
)
6376 (c-backward-syntactic-ws)
6377 (setq end-2
(point))
6379 (c-simple-skip-symbol-backward)
6382 (buffer-substring-no-properties (point) end-2
))
6383 ;; Cheating in the handling of syntactic ws below.
6384 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6386 (setq end-1
(point))
6387 (c-simple-skip-symbol-backward))
6388 (>= (point) type-start
)
6389 (equal (buffer-substring-no-properties (point) end-1
)
6391 ;; It is a (con|de)structor name. In that case the
6392 ;; declaration is typeless so zap out any preceding
6393 ;; identifier(s) that we might have taken as types.
6394 (goto-char type-start
)
6397 id-start type-start
))))))
6399 ;; Check for and step over a type decl expression after the thing
6400 ;; that is or might be a type. This can't be skipped since we
6401 ;; need the correct end position of the declarator for
6402 ;; `max-type-decl-end-*'.
6403 (let ((start (point)) (paren-depth 0) pos
6404 ;; True if there's a non-open-paren match of
6405 ;; `c-type-decl-prefix-key'.
6407 ;; True if the declarator is surrounded by a parenthesis pair.
6409 ;; True if there is an identifier in the declarator.
6411 ;; True if there's a non-close-paren match of
6412 ;; `c-type-decl-suffix-key'.
6414 ;; True if there's a prefix match outside the outermost
6415 ;; paren pair that surrounds the declarator.
6416 got-prefix-before-parens
6417 ;; True if there's a suffix match outside the outermost
6418 ;; paren pair that surrounds the declarator. The value is
6419 ;; the position of the first suffix match.
6420 got-suffix-after-parens
6421 ;; True if we've parsed the type decl to a token that is
6422 ;; known to end declarations in this context.
6424 ;; The earlier values of `at-type' and `type-start' if we've
6425 ;; shifted the type backwards.
6426 identifier-type identifier-start
6427 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6428 ;; turn it off during the name skipping below to avoid
6429 ;; getting `c-type' properties that might be bogus. That
6430 ;; can happen since we don't know if
6431 ;; `c-restricted-<>-arglists' will be correct inside the
6432 ;; arglist paren that gets entered.
6433 c-parse-and-markup-
<>-arglists
)
6435 (goto-char id-start
)
6437 ;; Skip over type decl prefix operators. (Note similar code in
6438 ;; `c-font-lock-declarators'.)
6439 (while (and (looking-at c-type-decl-prefix-key
)
6440 (if (and (c-major-mode-is 'c
++-mode
)
6441 (match-beginning 2))
6442 ;; If the second submatch matches in C++ then
6443 ;; we're looking at an identifier that's a
6444 ;; prefix only if it specifies a member pointer.
6445 (when (setq got-identifier
(c-forward-name))
6446 (if (looking-at "\\(::\\)")
6447 ;; We only check for a trailing "::" and
6448 ;; let the "*" that should follow be
6449 ;; matched in the next round.
6450 (progn (setq got-identifier nil
) t
)
6451 ;; It turned out to be the real identifier,
6456 (if (eq (char-after) ?\
()
6458 (setq paren-depth
(1+ paren-depth
))
6460 (unless got-prefix-before-parens
6461 (setq got-prefix-before-parens
(= paren-depth
0)))
6463 (goto-char (match-end 1)))
6464 (c-forward-syntactic-ws))
6466 (setq got-parens
(> paren-depth
0))
6468 ;; Skip over an identifier.
6470 (and (looking-at c-identifier-start
)
6471 (setq got-identifier
(c-forward-name))))
6473 ;; Skip over type decl suffix operators.
6474 (while (if (looking-at c-type-decl-suffix-key
)
6476 (if (eq (char-after) ?\
))
6477 (when (> paren-depth
0)
6478 (setq paren-depth
(1- paren-depth
))
6481 (when (if (save-match-data (looking-at "\\s\("))
6482 (c-safe (c-forward-sexp 1) t
)
6483 (goto-char (match-end 1))
6485 (when (and (not got-suffix-after-parens
)
6487 (setq got-suffix-after-parens
(match-beginning 0)))
6488 (setq got-suffix t
)))
6490 ;; No suffix matched. We might have matched the
6491 ;; identifier as a type and the open paren of a
6492 ;; function arglist as a type decl prefix. In that
6493 ;; case we should "backtrack": Reinterpret the last
6494 ;; type as the identifier, move out of the arglist and
6495 ;; continue searching for suffix operators.
6497 ;; Do this even if there's no preceding type, to cope
6498 ;; with old style function declarations in K&R C,
6499 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6500 ;; style declarations. That isn't applicable in an
6501 ;; arglist context, though.
6502 (when (and (= paren-depth
1)
6503 (not got-prefix-before-parens
)
6504 (not (eq at-type t
))
6507 backup-maybe-typeless
6508 (when c-recognize-typeless-decls
6510 (setq pos
(c-up-list-forward (point)))
6511 (eq (char-before pos
) ?\
)))
6512 (c-fdoc-shift-type-backward)
6516 (c-forward-syntactic-ws))
6518 (when (and (or maybe-typeless backup-maybe-typeless
)
6519 (not got-identifier
)
6522 ;; Have found no identifier but `c-typeless-decl-kwds' has
6523 ;; matched so we know we're inside a declaration. The
6524 ;; preceding type must be the identifier instead.
6525 (c-fdoc-shift-type-backward))
6529 (catch 'at-decl-or-cast
6532 (when (> paren-depth
0)
6533 ;; Encountered something inside parens that isn't matched by
6534 ;; the `c-type-decl-*' regexps, so it's not a type decl
6535 ;; expression. Try to skip out to the same paren depth to
6536 ;; not confuse the cast check below.
6537 (c-safe (goto-char (scan-lists (point) 1 paren-depth
)))
6538 ;; If we've found a specifier keyword then it's a
6539 ;; declaration regardless.
6540 (throw 'at-decl-or-cast
(eq at-decl-or-cast t
)))
6543 (looking-at (cond ((eq context
'<>) "[,>]")
6547 ;; Now we've collected info about various characteristics of
6548 ;; the construct we're looking at. Below follows a decision
6549 ;; tree based on that. It's ordered to check more certain
6550 ;; signs before less certain ones.
6556 (when (and (or at-type maybe-typeless
)
6557 (not (or got-prefix got-parens
)))
6558 ;; Got another identifier directly after the type, so it's a
6560 (throw 'at-decl-or-cast t
))
6562 (when (and got-parens
6564 (not got-suffix-after-parens
)
6567 backup-maybe-typeless
))
6568 ;; Got a declaration of the form "foo bar (gnu);" where we've
6569 ;; recognized "bar" as the type and "gnu" as the declarator.
6570 ;; In this case it's however more likely that "bar" is the
6571 ;; declarator and "gnu" a function argument or initializer (if
6572 ;; `c-recognize-paren-inits' is set), since the parens around
6573 ;; "gnu" would be superfluous if it's a declarator. Shift the
6574 ;; type one step backward.
6575 (c-fdoc-shift-type-backward)))
6577 ;; Found no identifier.
6583 (when (= (point) start
)
6584 ;; Got a plain list of identifiers. If a colon follows it's
6585 ;; a valid label. Otherwise the last one probably is the
6586 ;; declared identifier and we should back up to the previous
6587 ;; type, providing it isn't a cast.
6588 (if (and (eq (char-after) ?
:)
6589 (not (c-major-mode-is 'java-mode
)))
6590 ;; If we've found a specifier keyword then it's a
6591 ;; declaration regardless.
6592 (throw 'at-decl-or-cast
(eq at-decl-or-cast t
))
6593 (setq backup-if-not-cast t
)
6594 (throw 'at-decl-or-cast t
)))
6597 (when (and got-suffix
6600 ;; Got a plain list of identifiers followed by some suffix.
6601 ;; If this isn't a cast then the last identifier probably is
6602 ;; the declared one and we should back up to the previous
6604 (setq backup-if-not-cast t
)
6605 (throw 'at-decl-or-cast t
)))
6608 (when (eq at-type t
)
6609 ;; If the type is known we know that there can't be any
6610 ;; identifier somewhere else, and it's only in declarations in
6611 ;; e.g. function prototypes and in casts that the identifier may
6613 (throw 'at-decl-or-cast t
))
6615 (when (= (point) start
)
6616 ;; Only got a single identifier (parsed as a type so far).
6619 ;; Check that the identifier isn't at the start of an
6624 ;; Inside an arglist that contains declarations. If K&R
6625 ;; style declarations and parenthesis style initializers
6626 ;; aren't allowed then the single identifier must be a
6627 ;; type, else we require that it's known or found
6628 ;; (primitive types are handled above).
6629 (or (and (not c-recognize-knr-p
)
6630 (not c-recognize-paren-inits
))
6631 (memq at-type
'(known found
))))
6633 ;; Inside a template arglist. Accept known and found
6634 ;; types; other identifiers could just as well be
6635 ;; constants in C++.
6636 (memq at-type
'(known found
)))))
6637 (throw 'at-decl-or-cast t
)
6639 ;; Can't be a valid declaration or cast, but if we've found a
6640 ;; specifier it can't be anything else either, so treat it as
6641 ;; an invalid/unfinished declaration or cast.
6642 (throw 'at-decl-or-cast at-decl-or-cast
))))
6647 (not (eq at-type t
))
6650 backup-maybe-typeless
6651 (when c-recognize-typeless-decls
6652 (or (not got-suffix
)
6654 c-after-suffixed-type-maybe-decl-key
))))))
6655 ;; Got an empty paren pair and a preceding type that probably
6656 ;; really is the identifier. Shift the type backwards to make
6657 ;; the last one the identifier. This is analogous to the
6658 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6661 ;; Exception: In addition to the conditions in that
6662 ;; "backtracking" code, do not shift backward if we're not
6663 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6664 ;; Since there's no preceding type, the shift would mean that
6665 ;; the declaration is typeless. But if the regexp doesn't match
6666 ;; then we will simply fall through in the tests below and not
6667 ;; recognize it at all, so it's better to try it as an abstract
6668 ;; declarator instead.
6669 (c-fdoc-shift-type-backward)
6671 ;; Still no identifier.
6673 (when (and got-prefix
(or got-parens got-suffix
))
6674 ;; Require `got-prefix' together with either `got-parens' or
6675 ;; `got-suffix' to recognize it as an abstract declarator:
6676 ;; `got-parens' only is probably an empty function call.
6677 ;; `got-suffix' only can build an ordinary expression together
6678 ;; with the preceding identifier which we've taken as a type.
6679 ;; We could actually accept on `got-prefix' only, but that can
6680 ;; easily occur temporarily while writing an expression so we
6681 ;; avoid that case anyway. We could do a better job if we knew
6682 ;; the point when the fontification was invoked.
6683 (throw 'at-decl-or-cast t
))
6689 got-suffix-after-parens
6690 (eq (char-after got-suffix-after-parens
) ?\
())
6691 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6692 ;; normal function call afterall (or perhaps a C++ style object
6693 ;; instantiation expression).
6694 (throw 'at-decl-or-cast nil
))))
6697 (when at-decl-or-cast
6698 ;; By now we've located the type in the declaration that we know
6700 (throw 'at-decl-or-cast t
))
6703 (when (and got-identifier
6705 (looking-at c-after-suffixed-type-decl-key
)
6709 (not (eq at-type t
)))
6710 ;; Shift the type backward in the case that there's a
6711 ;; single identifier inside parens. That can only
6712 ;; occur in K&R style function declarations so it's
6713 ;; more likely that it really is a function call.
6714 ;; Therefore we only do this after
6715 ;; `c-after-suffixed-type-decl-key' has matched.
6716 (progn (c-fdoc-shift-type-backward) t
)
6717 got-suffix-after-parens
))
6718 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6719 (throw 'at-decl-or-cast t
))
6722 (when (and (or got-prefix
(not got-parens
))
6723 (memq at-type
'(t known
)))
6724 ;; It's a declaration if a known type precedes it and it can't be a
6726 (throw 'at-decl-or-cast t
))
6728 ;; If we get here we can't tell if this is a type decl or a normal
6729 ;; expression by looking at it alone. (That's under the assumption
6730 ;; that normal expressions always can look like type decl expressions,
6731 ;; which isn't really true but the cases where it doesn't hold are so
6732 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6733 ;; the effort to look for them.)
6735 (unless (or at-decl-end
(looking-at "=[^=]"))
6736 ;; If this is a declaration it should end here or its initializer(*)
6737 ;; should start here, so check for allowed separation tokens. Note
6738 ;; that this rule doesn't work e.g. with a K&R arglist after a
6741 ;; *) Don't check for C++ style initializers using parens
6742 ;; since those already have been matched as suffixes.
6744 ;; If `at-decl-or-cast' is then we've found some other sign that
6745 ;; it's a declaration or cast, so then it's probably an
6746 ;; invalid/unfinished one.
6747 (throw 'at-decl-or-cast at-decl-or-cast
))
6749 ;; Below are tests that only should be applied when we're certain to
6750 ;; not have parsed halfway through an expression.
6753 (when (memq at-type
'(t known
))
6754 ;; The expression starts with a known type so treat it as a
6756 (throw 'at-decl-or-cast t
))
6759 (when (and (c-major-mode-is 'c
++-mode
)
6760 ;; In C++ we check if the identifier is a known type, since
6761 ;; (con|de)structors use the class name as identifier.
6762 ;; We've always shifted over the identifier as a type and
6763 ;; then backed up again in this case.
6765 (or (memq identifier-type
'(found known
))
6766 (and (eq (char-after identifier-start
) ?~
)
6767 ;; `at-type' probably won't be 'found for
6768 ;; destructors since the "~" is then part of the
6769 ;; type name being checked against the list of
6770 ;; known types, so do a check without that
6773 (goto-char (1+ identifier-start
))
6774 (c-forward-syntactic-ws)
6775 (c-with-syntax-table
6776 c-identifier-syntax-table
6777 (looking-at c-known-type-key
)))
6779 (goto-char (1+ identifier-start
))
6780 ;; We have already parsed the type earlier,
6781 ;; so it'd be possible to cache the end
6782 ;; position instead of redoing it here, but
6783 ;; then we'd need to keep track of another
6784 ;; position everywhere.
6785 (c-check-type (point)
6786 (progn (c-forward-type)
6788 (throw 'at-decl-or-cast t
))
6793 (when (and got-prefix-before-parens
6795 (or at-decl-end
(looking-at "=[^=]"))
6798 ;; Got something like "foo * bar;". Since we're not inside an
6799 ;; arglist it would be a meaningless expression because the
6800 ;; result isn't used. We therefore choose to recognize it as
6801 ;; a declaration. Do not allow a suffix since it could then
6802 ;; be a function call.
6803 (throw 'at-decl-or-cast t
))
6806 (when (and (or got-suffix-after-parens
6807 (looking-at "=[^=]"))
6809 (not (eq context
'arglist
)))
6810 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
6811 ;; be an odd expression or it could be a declaration. Treat
6812 ;; it as a declaration if "a" has been used as a type
6813 ;; somewhere else (if it's a known type we won't get here).
6814 (throw 'at-decl-or-cast t
)))
6819 (and (eq context
'decl
)
6820 (not c-recognize-paren-inits
)
6821 (or got-parens got-suffix
))))
6822 ;; Got a type followed by an abstract declarator. If `got-prefix'
6823 ;; is set it's something like "a *" without anything after it. If
6824 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
6825 ;; or similar, which we accept only if the context rules out
6827 (throw 'at-decl-or-cast t
)))
6829 ;; If we had a complete symbol table here (which rules out
6830 ;; `c-found-types') we should return t due to the disambiguation rule
6831 ;; (in at least C++) that anything that can be parsed as a declaration
6832 ;; is a declaration. Now we're being more defensive and prefer to
6833 ;; highlight things like "foo (bar);" as a declaration only if we're
6834 ;; inside an arglist that contains declarations.
6835 (eq context
'decl
))))
6837 ;; The point is now after the type decl expression.
6840 ;; Check for a cast.
6845 ;; Should be the first type/identifier in a cast paren.
6846 (> preceding-token-end
(point-min))
6847 (memq (char-before preceding-token-end
) c-cast-parens
)
6849 ;; The closing paren should follow.
6851 (c-forward-syntactic-ws)
6852 (looking-at "\\s\)"))
6854 ;; There should be a primary expression after it.
6857 (c-forward-syntactic-ws)
6858 (setq cast-end
(point))
6859 (and (looking-at c-primary-expr-regexp
)
6861 (setq pos
(match-end 0))
6863 ;; Check if the expression begins with a prefix keyword.
6865 (if (match-beginning 1)
6866 ;; Expression begins with an ambiguous operator. Treat
6867 ;; it as a cast if it's a type decl or if we've
6868 ;; recognized the type somewhere else.
6870 (memq at-type
'(t known found
)))
6871 ;; Unless it's a keyword, it's the beginning of a primary
6873 (not (looking-at c-keywords-regexp
)))))
6874 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
6875 ;; that it matched a whole one so that we don't e.g. confuse
6876 ;; the operator '-' with '->'. It's ok if it matches further,
6877 ;; though, since it e.g. can match the float '.5' while the
6878 ;; operator regexp only matches '.'.
6879 (or (not (looking-at c-nonsymbol-token-regexp
))
6880 (<= (match-end 0) pos
))))
6882 ;; There should either be a cast before it or something that isn't an
6883 ;; identifier or close paren.
6884 (> preceding-token-end
(point-min))
6886 (goto-char (1- preceding-token-end
))
6887 (or (eq (point) last-cast-end
)
6889 (c-backward-syntactic-ws)
6890 (if (< (skip-syntax-backward "w_") 0)
6891 ;; It's a symbol. Accept it only if it's one of the
6892 ;; keywords that can precede an expression (without
6893 ;; surrounding parens).
6894 (looking-at c-simple-stmt-key
)
6896 ;; Check that it isn't a close paren (block close is ok,
6898 (not (memq (char-before) '(?\
) ?\
])))
6899 ;; Check that it isn't a nonsymbol identifier.
6900 (not (c-on-identifier)))))))))
6903 (when (and c-record-type-identifiers at-type
(not (eq at-type t
)))
6904 (let ((c-promote-possible-types t
))
6905 (goto-char type-start
)
6908 (goto-char cast-end
)
6912 ;; We're at a declaration. Highlight the type and the following
6915 (when backup-if-not-cast
6916 (c-fdoc-shift-type-backward t
))
6918 (when (and (eq context
'decl
) (looking-at ","))
6919 ;; Make sure to propagate the `c-decl-arg-start' property to
6920 ;; the next argument if it's set in this one, to cope with
6921 ;; interactive refontification.
6922 (c-put-c-type-property (point) 'c-decl-arg-start
))
6924 (when (and c-record-type-identifiers at-type
(not (eq at-type t
)))
6925 (let ((c-promote-possible-types t
))
6927 (goto-char type-start
)
6931 (and (or at-type-decl at-typedef
)
6932 (cons at-type-decl at-typedef
))))
6935 ;; False alarm. Restore the recorded ranges.
6936 (setq c-record-type-identifiers save-rec-type-ids
6937 c-record-ref-identifiers save-rec-ref-ids
)
6940 (defun c-forward-label (&optional assume-markup preceding-token-end limit
)
6941 ;; Assuming that point is at the beginning of a token, check if it starts a
6942 ;; label and if so move over it and return non-nil (t in default situations,
6943 ;; specific symbols (see below) for interesting situations), otherwise don't
6944 ;; move and return nil. "Label" here means "most things with a colon".
6946 ;; More precisely, a "label" is regarded as one of:
6947 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
6948 ;; (ii) A case label - either the entire construct "case FOO:", or just the
6949 ;; bare "case", should the colon be missing. We return t;
6950 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
6952 ;; (iv) One of QT's "extended" C++ variants of
6953 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
6954 ;; Returns the symbol `qt-2kwds-colon'.
6955 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
6956 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
6957 ;; colon). Currently (2006-03), this applies only to Objective C's
6958 ;; keywords "@private", "@protected", and "@public". Returns t.
6960 ;; One of the things which will NOT be recognised as a label is a bit-field
6961 ;; element of a struct, something like "int foo:5".
6963 ;; The end of the label is taken to be just after the colon, or the end of
6964 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
6965 ;; after the end on return. The terminating char gets marked with
6966 ;; `c-decl-end' to improve recognition of the following declaration or
6969 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
6970 ;; label, if any, has already been marked up like that.
6972 ;; If PRECEDING-TOKEN-END is given, it should be the first position
6973 ;; after the preceding token, i.e. on the other side of the
6974 ;; syntactic ws from the point. Use a value less than or equal to
6975 ;; (point-min) if the point is at the first token in (the visible
6976 ;; part of) the buffer.
6978 ;; The optional LIMIT limits the forward scan for the colon.
6980 ;; This function records the ranges of the label symbols on
6981 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
6984 ;; This function might do hidden buffer changes.
6986 (let ((start (point))
6989 macro-start
; if we're in one.
6993 ;; "case" or "default" (Doesn't apply to AWK).
6994 ((looking-at c-label-kwds-regexp
)
6995 (let ((kwd-end (match-end 1)))
6996 ;; Record only the keyword itself for fontification, since in
6997 ;; case labels the following is a constant expression and not
6999 (when c-record-type-identifiers
7000 (c-record-ref-id (cons (match-beginning 1) kwd-end
)))
7002 ;; Find the label end.
7005 (if (and (c-syntactic-re-search-forward
7006 ;; Stop on chars that aren't allowed in expressions,
7007 ;; and on operator chars that would be meaningless
7008 ;; there. FIXME: This doesn't cope with ?: operators.
7009 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7011 (match-beginning 2))
7013 (progn ; there's a proper :
7014 (goto-char (match-beginning 2)) ; just after the :
7015 (c-put-c-type-property (1- (point)) 'c-decl-end
)
7018 ;; It's an unfinished label. We consider the keyword enough
7019 ;; to recognize it as a label, so that it gets fontified.
7020 ;; Leave the point at the end of it, but don't put any
7021 ;; `c-decl-end' marker.
7025 ;; @private, @protected, @public, in Objective C, or similar.
7026 ((and c-opt-extra-label-key
7027 (looking-at c-opt-extra-label-key
))
7028 ;; For a `c-opt-extra-label-key' match, we record the whole
7029 ;; thing for fontification. That's to get the leading '@' in
7030 ;; Objective-C protection labels fontified.
7031 (goto-char (match-end 1))
7032 (when c-record-type-identifiers
7033 (c-record-ref-id (cons (match-beginning 1) (point))))
7034 (c-put-c-type-property (1- (point)) 'c-decl-end
)
7035 (setq label-type t
))
7037 ;; All other cases of labels.
7038 ((and c-recognize-colon-labels
; nil for AWK and IDL, otherwise t.
7040 ;; A colon label must have something before the colon.
7041 (not (eq (char-after) ?
:))
7043 ;; Check that we're not after a token that can't precede a label.
7045 ;; Trivially succeeds when there's no preceding token.
7046 (if preceding-token-end
7047 (<= preceding-token-end
(point-min))
7049 (c-backward-syntactic-ws)
7050 (setq preceding-token-end
(point))
7053 ;; Check if we're after a label, if we're after a closing
7054 ;; paren that belong to statement, and with
7055 ;; `c-label-prefix-re'. It's done in different order
7056 ;; depending on `assume-markup' since the checks have
7057 ;; different expensiveness.
7060 (eq (c-get-char-property (1- preceding-token-end
) 'c-type
)
7064 (goto-char (1- preceding-token-end
))
7065 (c-beginning-of-current-token)
7066 (or (looking-at c-label-prefix-re
)
7067 (looking-at c-block-stmt-1-key
)))
7069 (and (eq (char-before preceding-token-end
) ?\
))
7070 (c-after-conditional)))
7074 (goto-char (1- preceding-token-end
))
7075 (c-beginning-of-current-token)
7076 (or (looking-at c-label-prefix-re
)
7077 (looking-at c-block-stmt-1-key
)))
7080 ((eq (char-before preceding-token-end
) ?\
))
7081 (c-after-conditional))
7083 ((eq (char-before preceding-token-end
) ?
:)
7084 ;; Might be after another label, so check it recursively.
7087 (goto-char (1- preceding-token-end
))
7088 ;; Essentially the same as the
7089 ;; `c-syntactic-re-search-forward' regexp below.
7091 (save-excursion (and (c-beginning-of-macro)
7093 (if macro-start
(narrow-to-region macro-start
(point-max)))
7094 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t
)
7095 ;; Note: the following should work instead of the
7096 ;; narrow-to-region above. Investigate why not,
7097 ;; sometime. ACM, 2006-03-31.
7098 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7101 ;; If the caller turned on recording for us,
7102 ;; it shouldn't apply when we check the
7104 c-record-type-identifiers
)
7105 ;; A label can't start at a cpp directive. Check for
7106 ;; this, since c-forward-syntactic-ws would foul up on it.
7107 (unless (and c-opt-cpp-prefix
(looking-at c-opt-cpp-prefix
))
7108 (c-forward-syntactic-ws)
7109 (c-forward-label nil pte start
))))))))))
7111 ;; Point is still at the beginning of the possible label construct.
7113 ;; Check that the next nonsymbol token is ":", or that we're in one
7114 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7115 ;; arguments. FIXME: Should build this regexp from the language
7118 ;; public: protected: private:
7120 (c-major-mode-is 'c
++-mode
)
7121 (search-forward-regexp
7122 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t
)
7123 (progn (backward-char)
7124 (c-forward-syntactic-ws limit
)
7125 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7127 (setq label-type t
))
7128 ;; QT double keyword like "protected slots:" or goto target.
7129 ((progn (goto-char start
) nil
))
7130 ((when (c-syntactic-re-search-forward
7131 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t
) ; not at EOB
7133 (setq label-end
(point))
7135 (and (c-major-mode-is 'c
++-mode
)
7137 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7138 (buffer-substring start
(point)))))
7139 (c-forward-syntactic-ws limit
)
7141 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7144 (if (or (string= "signals" ; Special QT macro
7145 (setq kwd
(buffer-substring-no-properties start label-end
)))
7146 (string= "Q_SIGNALS" kwd
))
7150 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t
)
7151 (progn (c-forward-syntactic-ws limit
)
7152 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7154 (setq label-type
'qt-2kwds-colon
)))))))
7157 (narrow-to-region start
(point))
7159 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7163 (when (looking-at c-nonlabel-token-key
)
7165 (setq label-type nil
)
7166 (throw 'check-label nil
))
7167 (and (c-safe (c-forward-sexp)
7168 (c-forward-syntactic-ws)
7172 ;; Record the identifiers in the label for fontification, unless
7173 ;; it begins with `c-label-kwds' in which case the following
7174 ;; identifiers are part of a (constant) expression that
7175 ;; shouldn't be fontified.
7176 (when (and c-record-type-identifiers
7177 (progn (goto-char start
)
7178 (not (looking-at c-label-kwds-regexp
))))
7179 (while (c-syntactic-re-search-forward c-symbol-key nil t
)
7180 (c-record-ref-id (cons (match-beginning 0)
7183 (c-put-c-type-property (1- (point-max)) 'c-decl-end
)
7184 (goto-char (point-max)))))
7191 (defun c-forward-objc-directive ()
7192 ;; Assuming the point is at the beginning of a token, try to move
7193 ;; forward to the end of the Objective-C directive that starts
7194 ;; there. Return t if a directive was fully recognized, otherwise
7195 ;; the point is moved as far as one could be successfully parsed and
7198 ;; This function records identifier ranges on
7199 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7200 ;; `c-record-type-identifiers' is non-nil.
7202 ;; This function might do hidden buffer changes.
7204 (let ((start (point))
7206 (c-promote-possible-types t
)
7207 ;; Turn off recognition of angle bracket arglists while parsing
7208 ;; types here since the protocol reference list might then be
7209 ;; considered part of the preceding name or superclass-name.
7210 c-recognize-
<>-arglists
)
7215 (c-make-keywords-re t
7216 (append (c-lang-const c-protection-kwds objc
)
7219 (goto-char (match-end 1))
7225 (c-make-keywords-re t
7226 '("@interface" "@implementation" "@protocol")
7229 ;; Handle the name of the class itself.
7231 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7233 (goto-char (match-end 0))
7238 ;; Look for ": superclass-name" or "( category-name )".
7239 (when (looking-at "[:\(]")
7240 (setq start-char
(char-after))
7242 (c-forward-syntactic-ws)
7243 (unless (c-forward-type) (throw 'break nil
))
7244 (when (eq start-char ?\
()
7245 (unless (eq (char-after) ?\
)) (throw 'break nil
))
7247 (c-forward-syntactic-ws)))
7249 ;; Look for a protocol reference list.
7250 (if (eq (char-after) ?
<)
7251 (let ((c-recognize-<>-arglists t
)
7252 (c-parse-and-markup-<>-arglists t
)
7253 c-restricted-
<>-arglists
)
7254 (c-forward-<>-arglist t
))
7258 (c-backward-syntactic-ws)
7259 (c-clear-c-type-property start
(1- (point)) 'c-decl-end
)
7260 (c-put-c-type-property (1- (point)) 'c-decl-end
)
7263 (c-clear-c-type-property start
(point) 'c-decl-end
)
7266 (defun c-beginning-of-inheritance-list (&optional lim
)
7267 ;; Go to the first non-whitespace after the colon that starts a
7268 ;; multiple inheritance introduction. Optional LIM is the farthest
7269 ;; back we should search.
7271 ;; This function might do hidden buffer changes.
7272 (c-with-syntax-table c
++-template-syntax-table
7273 (c-backward-token-2 0 t lim
)
7274 (while (and (or (looking-at c-symbol-start
)
7275 (looking-at "[<,]\\|::"))
7276 (zerop (c-backward-token-2 1 t lim
))))))
7278 (defun c-in-method-def-p ()
7279 ;; Return nil if we aren't in a method definition, otherwise the
7280 ;; position of the initial [+-].
7282 ;; This function might do hidden buffer changes.
7285 (and c-opt-method-key
7286 (looking-at c-opt-method-key
)
7290 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7291 (defun c-in-gcc-asm-p ()
7292 ;; Return non-nil if point is within a gcc \"asm\" block.
7294 ;; This should be called with point inside an argument list.
7296 ;; Only one level of enclosing parentheses is considered, so for
7297 ;; instance `nil' is returned when in a function call within an asm
7300 ;; This function might do hidden buffer changes.
7302 (and c-opt-asm-stmt-key
7305 (backward-up-list 1)
7306 (c-beginning-of-statement-1 (point-min) nil t
)
7307 (looking-at c-opt-asm-stmt-key
))))
7309 (defun c-at-toplevel-p ()
7310 "Return a determination as to whether point is \"at the top level\".
7311 Informally, \"at the top level\" is anywhere where you can write
7314 More precisely, being at the top-level means that point is either
7315 outside any enclosing block (such as a function definition), or
7316 directly inside a class, namespace or other block that contains
7317 another declaration level.
7319 If point is not at the top-level (e.g. it is inside a method
7320 definition), then nil is returned. Otherwise, if point is at a
7321 top-level not enclosed within a class definition, t is returned.
7322 Otherwise, a 2-vector is returned where the zeroth element is the
7323 buffer position of the start of the class declaration, and the first
7324 element is the buffer position of the enclosing class's opening
7327 Note that this function might do hidden buffer changes. See the
7328 comment at the start of cc-engine.el for more info."
7329 (let ((paren-state (c-parse-state)))
7330 (or (not (c-most-enclosing-brace paren-state
))
7331 (c-search-uplist-for-classkey paren-state
))))
7333 (defun c-just-after-func-arglist-p (&optional lim
)
7334 ;; Return non-nil if the point is in the region after the argument
7335 ;; list of a function and its opening brace (or semicolon in case it
7336 ;; got no body). If there are K&R style argument declarations in
7337 ;; that region, the point has to be inside the first one for this
7338 ;; function to recognize it.
7340 ;; If successful, the point is moved to the first token after the
7341 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7342 ;; the position of the opening paren of the function arglist is
7345 ;; The point is clobbered if not successful.
7347 ;; LIM is used as bound for backward buffer searches.
7349 ;; This function might do hidden buffer changes.
7351 (let ((beg (point)) end id-start
)
7353 (eq (c-beginning-of-statement-1 lim
) 'same
)
7355 (not (or (c-major-mode-is 'objc-mode
)
7356 (c-forward-objc-directive)))
7359 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
)))
7362 ;; There should not be a '=' or ',' between beg and the
7363 ;; start of the declaration since that means we were in the
7364 ;; "expression part" of the declaration.
7366 (not (looking-at "[=,]")))
7369 ;; Check that there's an arglist paren in the
7371 (goto-char id-start
)
7372 (cond ((eq (char-after) ?\
()
7373 ;; The declarator is a paren expression, so skip past it
7374 ;; so that we don't get stuck on that instead of the
7375 ;; function arglist.
7377 ((and c-opt-op-identifier-prefix
7378 (looking-at c-opt-op-identifier-prefix
))
7379 ;; Don't trip up on "operator ()".
7380 (c-forward-token-2 2 t
)))
7381 (and (< (point) beg
)
7382 (c-syntactic-re-search-forward "(" beg t t
)
7385 (defun c-in-knr-argdecl (&optional lim
)
7386 ;; Return the position of the first argument declaration if point is
7387 ;; inside a K&R style argument declaration list, nil otherwise.
7388 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7389 ;; position that bounds the backward search for the argument list.
7391 ;; Point must be within a possible K&R region, e.g. just before a top-level
7392 ;; "{". It must be outside of parens and brackets. The test can return
7393 ;; false positives otherwise.
7395 ;; This function might do hidden buffer changes.
7399 ;; If we're in a macro, our search range is restricted to it. Narrow to
7400 ;; the searchable range.
7401 (let* ((macro-start (c-query-macro-start))
7402 (lim (max (or lim
(point-min)) (or macro-start
(point-min))))
7403 before-lparen after-rparen
7404 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
7405 (narrow-to-region lim
(c-point 'eol
))
7407 ;; Search backwards for the defun's argument list. We give up if we
7408 ;; encounter a "}" (end of a previous defun) or BOB.
7410 ;; The criterion for a paren structure being the arg list is:
7411 ;; o - there is non-WS stuff after it but before any "{"; AND
7412 ;; o - the token after it isn't a ";" AND
7413 ;; o - it is preceded by either an identifier (the function name) or
7414 ;; a macro expansion like "DEFUN (...)"; AND
7415 ;; o - its content is a non-empty comma-separated list of identifiers
7416 ;; (an empty arg list won't have a knr region).
7418 ;; The following snippet illustrates these rules:
7419 ;; int foo (bar, baz, yuk)
7421 ;; int (*baz) (my_type) ;
7422 ;; int (*) (void) (*yuk) (void) ;
7426 (while (> pp-count-out
0) ; go back one paren/bracket pair each time.
7427 (setq pp-count-out
(1- pp-count-out
))
7428 (c-syntactic-skip-backward "^)]}")
7429 (cond ((eq (char-before) ?\
))
7430 (setq after-rparen
(point)))
7431 ((eq (char-before) ?\
])
7432 (setq after-rparen nil
))
7433 (t ; either } (hit previous defun) or no more parens/brackets
7437 ;; We're inside a paren. Could it be our argument list....?
7441 (goto-char after-rparen
)
7442 (unless (c-go-list-backward) (throw 'knr nil
)) ;
7443 ;; FIXME!!! What about macros between the parens? 2007/01/20
7444 (setq before-lparen
(point)))
7446 ;; It can't be the arg list if next token is ; or {
7447 (progn (goto-char after-rparen
)
7448 (c-forward-syntactic-ws)
7449 (not (memq (char-after) '(?\
; ?\{))))
7451 ;; Is the thing preceding the list an identifier (the
7452 ;; function name), or a macro expansion?
7454 (goto-char before-lparen
)
7455 (eq (c-backward-token-2) 0)
7456 (or (c-on-identifier)
7457 (and (eq (char-after) ?\
))
7458 (c-go-up-list-backward)
7459 (eq (c-backward-token-2) 0)
7460 (c-on-identifier))))
7462 ;; Have we got a non-empty list of comma-separated
7465 (goto-char before-lparen
)
7466 (c-forward-token-2) ; to first token inside parens
7471 (while (eq (char-after) ?\
,)
7473 (unless (c-on-identifier) (throw 'id-list nil
))
7474 (c-forward-token-2))
7475 (eq (char-after) ?\
))))))
7477 ;; ...Yes. We've identified the function's argument list.
7479 (progn (goto-char after-rparen
)
7480 (c-forward-syntactic-ws)
7483 ;; ...No. The current parens aren't the function's arg list.
7484 (goto-char before-lparen
))
7486 (or (c-go-list-backward) ; backwards over [ .... ]
7487 (throw 'knr nil
)))))))))
7489 (defun c-skip-conditional ()
7490 ;; skip forward over conditional at point, including any predicate
7491 ;; statements in parentheses. No error checking is performed.
7493 ;; This function might do hidden buffer changes.
7494 (c-forward-sexp (cond
7496 ((looking-at (concat "\\<else"
7497 "\\([ \t\n]\\|\\\\\n\\)+"
7498 "if\\>\\([^_]\\|$\\)"))
7500 ;; do, else, try, finally
7501 ((looking-at (concat "\\<\\("
7502 "do\\|else\\|try\\|finally"
7503 "\\)\\>\\([^_]\\|$\\)"))
7505 ;; for, if, while, switch, catch, synchronized, foreach
7508 (defun c-after-conditional (&optional lim
)
7509 ;; If looking at the token after a conditional then return the
7510 ;; position of its start, otherwise return nil.
7512 ;; This function might do hidden buffer changes.
7514 (and (zerop (c-backward-token-2 1 t lim
))
7515 (or (looking-at c-block-stmt-1-key
)
7516 (and (eq (char-after) ?\
()
7517 (zerop (c-backward-token-2 1 t lim
))
7518 (looking-at c-block-stmt-2-key
)))
7521 (defun c-after-special-operator-id (&optional lim
)
7522 ;; If the point is after an operator identifier that isn't handled
7523 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7524 ;; position of the start of that identifier is returned. nil is
7525 ;; returned otherwise. The point may be anywhere in the syntactic
7526 ;; whitespace after the last token of the operator identifier.
7528 ;; This function might do hidden buffer changes.
7530 (and c-overloadable-operators-regexp
7531 (zerop (c-backward-token-2 1 nil lim
))
7532 (looking-at c-overloadable-operators-regexp
)
7533 (or (not c-opt-op-identifier-prefix
)
7535 (zerop (c-backward-token-2 1 nil lim
))
7536 (looking-at c-opt-op-identifier-prefix
)))
7539 (defsubst c-backward-to-block-anchor
(&optional lim
)
7540 ;; Assuming point is at a brace that opens a statement block of some
7541 ;; kind, move to the proper anchor point for that block. It might
7542 ;; need to be adjusted further by c-add-stmt-syntax, but the
7543 ;; position at return is suitable as start position for that
7546 ;; This function might do hidden buffer changes.
7547 (unless (= (point) (c-point 'boi
))
7548 (let ((start (c-after-conditional lim
)))
7550 (goto-char start
)))))
7552 (defsubst c-backward-to-decl-anchor
(&optional lim
)
7553 ;; Assuming point is at a brace that opens the block of a top level
7554 ;; declaration of some kind, move to the proper anchor point for
7557 ;; This function might do hidden buffer changes.
7558 (unless (= (point) (c-point 'boi
))
7559 (c-beginning-of-statement-1 lim
)))
7561 (defun c-search-decl-header-end ()
7562 ;; Search forward for the end of the "header" of the current
7563 ;; declaration. That's the position where the definition body
7564 ;; starts, or the first variable initializer, or the ending
7565 ;; semicolon. I.e. search forward for the closest following
7566 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7567 ;; _after_ the first found token, or at point-max if none is found.
7569 ;; This function might do hidden buffer changes.
7571 (let ((base (point)))
7572 (if (c-major-mode-is 'c
++-mode
)
7574 ;; In C++ we need to take special care to handle operator
7575 ;; tokens and those pesky template brackets.
7577 (c-syntactic-re-search-forward "[;{<=]" nil
'move t t
)
7579 (c-end-of-current-token base
)
7580 ;; Handle operator identifiers, i.e. ignore any
7581 ;; operator token preceded by "operator".
7583 (and (c-safe (c-backward-sexp) t
)
7584 (looking-at c-opt-op-identifier-prefix
)))
7585 (and (eq (char-before) ?
<)
7586 (c-with-syntax-table c
++-template-syntax-table
7587 (if (c-safe (goto-char (c-up-list-forward (point))))
7589 (goto-char (point-max))
7591 (setq base
(point)))
7594 (c-syntactic-re-search-forward "[;{=]" nil
'move t t
)
7595 (c-end-of-current-token base
))
7596 (setq base
(point))))))
7598 (defun c-beginning-of-decl-1 (&optional lim
)
7599 ;; Go to the beginning of the current declaration, or the beginning
7600 ;; of the previous one if already at the start of it. Point won't
7601 ;; be moved out of any surrounding paren. Return a cons cell of the
7602 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7603 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7604 ;; style argument declarations (and they are to be recognized) then
7605 ;; KNR-POS is set to the start of the first such argument
7606 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7607 ;; position that bounds the backward search.
7609 ;; NB: Cases where the declaration continues after the block, as in
7610 ;; "struct foo { ... } bar;", are currently recognized as two
7611 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7613 ;; This function might do hidden buffer changes.
7615 (let* ((start (point))
7616 (last-stmt-start (point))
7617 (move (c-beginning-of-statement-1 lim nil t
)))
7619 ;; `c-beginning-of-statement-1' stops at a block start, but we
7620 ;; want to continue if the block doesn't begin a top level
7621 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7622 ;; or an open paren.
7623 (let ((beg (point)) tentative-move
)
7624 ;; Go back one "statement" each time round the loop until we're just
7625 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7626 ;; an ObjC method. This will move over a multiple declaration whose
7627 ;; components are comma separated.
7629 ;; Must check with c-opt-method-key in ObjC mode.
7630 (not (and c-opt-method-key
7631 (looking-at c-opt-method-key
)))
7632 (/= last-stmt-start
(point))
7634 (c-backward-syntactic-ws lim
)
7635 (not (memq (char-before) '(?\
; ?} ?: nil))))
7638 (not (looking-at "\\s(")))
7639 ;; Check that we don't move from the first thing in a
7640 ;; macro to its header.
7641 (not (eq (setq tentative-move
7642 (c-beginning-of-statement-1 lim nil t
))
7644 (setq last-stmt-start beg
7646 move tentative-move
))
7649 (when c-recognize-knr-p
7650 (let ((fallback-pos (point)) knr-argdecl-start
)
7651 ;; Handle K&R argdecls. Back up after the "statement" jumped
7652 ;; over by `c-beginning-of-statement-1', unless it was the
7653 ;; function body, in which case we're sitting on the opening
7654 ;; brace now. Then test if we're in a K&R argdecl region and
7655 ;; that we started at the other side of the first argdecl in
7657 (unless (eq (char-after) ?
{)
7658 (goto-char last-stmt-start
))
7659 (if (and (setq knr-argdecl-start
(c-in-knr-argdecl lim
))
7660 (< knr-argdecl-start start
)
7662 (goto-char knr-argdecl-start
)
7663 (not (eq (c-beginning-of-statement-1 lim nil t
) 'macro
))))
7665 (cons (if (eq (char-after fallback-pos
) ?
{)
7669 (goto-char fallback-pos
))))
7671 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7672 ;; statement, so the result will be 'previous if we've moved over any.
7673 ;; So change our result back to 'same if necessary.
7675 ;; If they were brace list initializers we might not have moved over a
7676 ;; declaration boundary though, so change it to 'same if we've moved
7677 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7678 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7679 ;; potentially can search over a large amount of text.). Take special
7680 ;; pains not to get mislead by C++'s "operator=", and the like.
7681 (if (and (eq move
'previous
)
7682 (c-with-syntax-table (if (c-major-mode-is 'c
++-mode
)
7683 c
++-template-syntax-table
7688 (while ; keep going back to "[;={"s until we either find
7689 ; no more, or get to one which isn't an "operator ="
7690 (and (c-syntactic-re-search-forward "[;={]" start t t t
)
7691 (eq (char-before) ?
=)
7692 c-overloadable-operators-regexp
7693 c-opt-op-identifier-prefix
7695 (eq (c-backward-token-2) 0)
7696 (looking-at c-overloadable-operators-regexp
)
7697 (eq (c-backward-token-2) 0)
7698 (looking-at c-opt-op-identifier-prefix
))))
7699 (eq (char-before) ?
=))
7700 (c-syntactic-re-search-forward "[;{]" start t t
)
7701 (eq (char-before) ?
{)
7702 (c-safe (goto-char (c-up-list-forward (point))) t
)
7703 (not (c-syntactic-re-search-forward ";" start t t
))))))
7707 (defun c-end-of-decl-1 ()
7708 ;; Assuming point is at the start of a declaration (as detected by
7709 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7710 ;; `c-beginning-of-decl-1', this function handles the case when a
7711 ;; block is followed by identifiers in e.g. struct declarations in C
7712 ;; or C++. If a proper end was found then t is returned, otherwise
7713 ;; point is moved as far as possible within the current sexp and nil
7714 ;; is returned. This function doesn't handle macros; use
7715 ;; `c-end-of-macro' instead in those cases.
7717 ;; This function might do hidden buffer changes.
7718 (let ((start (point))
7719 (decl-syntax-table (if (c-major-mode-is 'c
++-mode
)
7720 c
++-template-syntax-table
7723 (c-search-decl-header-end)
7725 (when (and c-recognize-knr-p
7726 (eq (char-before) ?\
;)
7727 (c-in-knr-argdecl start
))
7728 ;; Stopped at the ';' in a K&R argdecl section which is
7729 ;; detected using the same criteria as in
7730 ;; `c-beginning-of-decl-1'. Move to the following block
7732 (c-syntactic-re-search-forward "{" nil
'move t
))
7734 (when (eq (char-before) ?
{)
7735 ;; Encountered a block in the declaration. Jump over it.
7737 (goto-char (c-up-list-forward (point)))
7738 (error (goto-char (point-max))
7739 (throw 'return nil
)))
7740 (if (or (not c-opt-block-decls-with-vars-key
)
7742 (c-with-syntax-table decl-syntax-table
7743 (let ((lim (point)))
7746 ;; Check for `c-opt-block-decls-with-vars-key'
7747 ;; before the first paren.
7748 (c-syntactic-re-search-forward
7749 (concat "[;=\(\[{]\\|\\("
7750 c-opt-block-decls-with-vars-key
7754 (not (eq (char-before) ?_
))
7755 ;; Check that the first following paren is
7757 (c-syntactic-re-search-forward "[;=\(\[{]"
7759 (eq (char-before) ?
{)))))))
7760 ;; The declaration doesn't have any of the
7761 ;; `c-opt-block-decls-with-vars' keywords in the
7762 ;; beginning, so it ends here at the end of the block.
7765 (c-with-syntax-table decl-syntax-table
7767 (if (eq (char-before) ?\
;)
7769 (c-syntactic-re-search-forward ";" nil
'move t
))))
7772 (defun c-looking-at-decl-block (containing-sexp goto-start
&optional limit
)
7773 ;; Assuming the point is at an open brace, check if it starts a
7774 ;; block that contains another declaration level, i.e. that isn't a
7775 ;; statement block or a brace list, and if so return non-nil.
7777 ;; If the check is successful, the return value is the start of the
7778 ;; keyword that tells what kind of construct it is, i.e. typically
7779 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7780 ;; the point will be at the start of the construct, before any
7781 ;; leading specifiers, otherwise it's at the returned position.
7783 ;; The point is clobbered if the check is unsuccessful.
7785 ;; CONTAINING-SEXP is the position of the open of the surrounding
7786 ;; paren, or nil if none.
7788 ;; The optional LIMIT limits the backward search for the start of
7789 ;; the construct. It's assumed to be at a syntactically relevant
7792 ;; If any template arglists are found in the searched region before
7793 ;; the open brace, they get marked with paren syntax.
7795 ;; This function might do hidden buffer changes.
7797 (let ((open-brace (point)) kwd-start first-specifier-pos
)
7798 (c-syntactic-skip-backward c-block-prefix-charset limit t
)
7800 (when (and c-recognize-
<>-arglists
7801 (eq (char-before) ?
>))
7802 ;; Could be at the end of a template arglist.
7803 (let ((c-parse-and-markup-<>-arglists t
)
7804 (c-disallow-comma-in-<>-arglists
7805 (and containing-sexp
7806 (not (eq (char-after containing-sexp
) ?
{)))))
7808 (c-backward-<>-arglist nil limit
)
7810 (c-syntactic-skip-backward c-block-prefix-charset limit t
)
7811 (eq (char-before) ?
>))))))
7813 ;; Note: Can't get bogus hits inside template arglists below since they
7814 ;; have gotten paren syntax above.
7816 ;; If `goto-start' is set we begin by searching for the
7817 ;; first possible position of a leading specifier list.
7818 ;; The `c-decl-block-key' search continues from there since
7819 ;; we know it can't match earlier.
7821 (when (c-syntactic-re-search-forward c-symbol-start
7823 (goto-char (setq first-specifier-pos
(match-beginning 0)))
7828 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t
)
7829 (goto-char (setq kwd-start
(match-beginning 0)))
7832 ;; Found a keyword that can't be a type?
7835 ;; Can be a type too, in which case it's the return type of a
7836 ;; function (under the assumption that no declaration level
7837 ;; block construct starts with a type).
7838 (not (c-forward-type))
7840 ;; Jumped over a type, but it could be a declaration keyword
7841 ;; followed by the declared identifier that we've jumped over
7842 ;; instead (e.g. in "class Foo {"). If it indeed is a type
7843 ;; then we should be at the declarator now, so check for a
7844 ;; valid declarator start.
7846 ;; Note: This doesn't cope with the case when a declared
7847 ;; identifier is followed by e.g. '(' in a language where '('
7848 ;; also might be part of a declarator expression. Currently
7849 ;; there's no such language.
7850 (not (or (looking-at c-symbol-start
)
7851 (looking-at c-type-decl-prefix-key
)))))
7853 ;; In Pike a list of modifiers may be followed by a brace
7854 ;; to make them apply to many identifiers. Note that the
7855 ;; match data will be empty on return in this case.
7856 ((and (c-major-mode-is 'pike-mode
)
7858 (goto-char open-brace
)
7859 (= (c-backward-token-2) 0))
7860 (looking-at c-specifier-key
)
7861 ;; Use this variant to avoid yet another special regexp.
7862 (c-keyword-member (c-keyword-sym (match-string 1))
7864 (setq kwd-start
(point))
7870 ;; Back up over any preceding specifiers and their clauses
7871 ;; by going forward from `first-specifier-pos', which is the
7872 ;; earliest possible position where the specifier list can
7875 (goto-char first-specifier-pos
)
7877 (while (< (point) kwd-start
)
7878 (if (looking-at c-symbol-key
)
7879 ;; Accept any plain symbol token on the ground that
7880 ;; it's a specifier masked through a macro (just
7881 ;; like `c-forward-decl-or-cast-1' skip forward over
7884 ;; Could be more restrictive wrt invalid keywords,
7885 ;; but that'd only occur in invalid code so there's
7886 ;; no use spending effort on it.
7887 (let ((end (match-end 0)))
7888 (unless (c-forward-keyword-clause 0)
7890 (c-forward-syntactic-ws)))
7892 ;; Can't parse a declaration preamble and is still
7893 ;; before `kwd-start'. That means `first-specifier-pos'
7894 ;; was in some earlier construct. Search again.
7895 (if (c-syntactic-re-search-forward c-symbol-start
7897 (goto-char (setq first-specifier-pos
(match-beginning 0)))
7898 ;; Got no preamble before the block declaration keyword.
7899 (setq first-specifier-pos kwd-start
))))
7901 (goto-char first-specifier-pos
))
7902 (goto-char kwd-start
))
7906 (defun c-search-uplist-for-classkey (paren-state)
7907 ;; Check if the closest containing paren sexp is a declaration
7908 ;; block, returning a 2 element vector in that case. Aref 0
7909 ;; contains the bufpos at boi of the class key line, and aref 1
7910 ;; contains the bufpos of the open brace. This function is an
7911 ;; obsolete wrapper for `c-looking-at-decl-block'.
7913 ;; This function might do hidden buffer changes.
7914 (let ((open-paren-pos (c-most-enclosing-brace paren-state
)))
7915 (when open-paren-pos
7917 (goto-char open-paren-pos
)
7918 (when (and (eq (char-after) ?
{)
7919 (c-looking-at-decl-block
7920 (c-safe-position open-paren-pos paren-state
)
7922 (back-to-indentation)
7923 (vector (point) open-paren-pos
))))))
7925 (defun c-inside-bracelist-p (containing-sexp paren-state
)
7926 ;; return the buffer position of the beginning of the brace list
7927 ;; statement if we're inside a brace list, otherwise return nil.
7928 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
7929 ;; paren. PAREN-STATE is the remainder of the state of enclosing
7932 ;; N.B.: This algorithm can potentially get confused by cpp macros
7933 ;; placed in inconvenient locations. It's a trade-off we make for
7936 ;; This function might do hidden buffer changes.
7938 ;; This will pick up brace list declarations.
7941 (goto-char containing-sexp
)
7944 (if (and (or (looking-at c-brace-list-key
)
7945 (progn (c-forward-sexp -
1)
7946 (looking-at c-brace-list-key
)))
7947 (setq bracepos
(c-down-list-forward (point)))
7948 (not (c-crosses-statement-barrier-p (point)
7951 ;; this will pick up array/aggregate init lists, even if they are nested.
7954 ;; Pike can have class definitions anywhere, so we must
7955 ;; check for the class key here.
7956 (and (c-major-mode-is 'pike-mode
)
7958 bufpos braceassignp lim next-containing
)
7959 (while (and (not bufpos
)
7962 (if (consp (car paren-state
))
7963 (setq lim
(cdr (car paren-state
))
7964 paren-state
(cdr paren-state
))
7965 (setq lim
(car paren-state
)))
7967 (setq next-containing
(car paren-state
)
7968 paren-state
(cdr paren-state
))))
7969 (goto-char containing-sexp
)
7970 (if (c-looking-at-inexpr-block next-containing next-containing
)
7971 ;; We're in an in-expression block of some kind. Do not
7972 ;; check nesting. We deliberately set the limit to the
7973 ;; containing sexp, so that c-looking-at-inexpr-block
7974 ;; doesn't check for an identifier before it.
7975 (setq containing-sexp nil
)
7976 ;; see if the open brace is preceded by = or [...] in
7977 ;; this statement, but watch out for operator=
7978 (setq braceassignp
'dontknow
)
7979 (c-backward-token-2 1 t lim
)
7980 ;; Checks to do only on the first sexp before the brace.
7981 (when (and c-opt-inexpr-brace-list-key
7982 (eq (char-after) ?\
[))
7983 ;; In Java, an initialization brace list may follow
7984 ;; directly after "new Foo[]", so check for a "new"
7986 (while (eq braceassignp
'dontknow
)
7988 (cond ((/= (c-backward-token-2 1 t lim
) 0) nil
)
7989 ((looking-at c-opt-inexpr-brace-list-key
) t
)
7990 ((looking-at "\\sw\\|\\s_\\|[.[]")
7991 ;; Carry on looking if this is an
7992 ;; identifier (may contain "." in Java)
7993 ;; or another "[]" sexp.
7996 ;; Checks to do on all sexps before the brace, up to the
7997 ;; beginning of the statement.
7998 (while (eq braceassignp
'dontknow
)
7999 (cond ((eq (char-after) ?\
;)
8000 (setq braceassignp nil
))
8002 (looking-at class-key
))
8003 (setq braceassignp nil
))
8004 ((eq (char-after) ?
=)
8005 ;; We've seen a =, but must check earlier tokens so
8006 ;; that it isn't something that should be ignored.
8007 (setq braceassignp
'maybe
)
8008 (while (and (eq braceassignp
'maybe
)
8009 (zerop (c-backward-token-2 1 t lim
)))
8012 ;; Check for operator =
8013 ((and c-opt-op-identifier-prefix
8014 (looking-at c-opt-op-identifier-prefix
))
8016 ;; Check for `<opchar>= in Pike.
8017 ((and (c-major-mode-is 'pike-mode
)
8018 (or (eq (char-after) ?
`)
8019 ;; Special case for Pikes
8020 ;; `[]=, since '[' is not in
8021 ;; the punctuation class.
8022 (and (eq (char-after) ?\
[)
8023 (eq (char-before) ?
`))))
8025 ((looking-at "\\s.") 'maybe
)
8026 ;; make sure we're not in a C++ template
8027 ;; argument assignment
8029 (c-major-mode-is 'c
++-mode
)
8031 (let ((here (point))
8033 (skip-chars-backward "^<>")
8035 (and (eq (char-before) ?
<)
8036 (not (c-crosses-statement-barrier-p
8038 (not (c-in-literal))
8042 (if (and (eq braceassignp
'dontknow
)
8043 (/= (c-backward-token-2 1 t lim
) 0))
8044 (setq braceassignp nil
)))
8045 (if (not braceassignp
)
8046 (if (eq (char-after) ?\
;)
8047 ;; Brace lists can't contain a semicolon, so we're done.
8048 (setq containing-sexp nil
)
8050 (setq containing-sexp next-containing
8052 next-containing nil
))
8053 ;; we've hit the beginning of the aggregate list
8054 (c-beginning-of-statement-1
8055 (c-most-enclosing-brace paren-state
))
8056 (setq bufpos
(point))))
8061 (defun c-looking-at-special-brace-list (&optional lim
)
8062 ;; If we're looking at the start of a pike-style list, ie `({Â })',
8063 ;; `([Â ])', `(<Â >)' etc, a cons of a cons of its starting and ending
8064 ;; positions and its entry in c-special-brace-lists is returned, nil
8065 ;; otherwise. The ending position is nil if the list is still open.
8066 ;; LIM is the limit for forward search. The point may either be at
8067 ;; the `(' or at the following paren character. Tries to check the
8068 ;; matching closer, but assumes it's correct if no balanced paren is
8069 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8070 ;; a special brace list).
8072 ;; This function might do hidden buffer changes.
8073 (if c-special-brace-lists
8078 (c-forward-syntactic-ws)
8079 (if (eq (char-after) ?\
()
8082 (c-forward-syntactic-ws)
8083 (setq inner-beg
(point))
8084 (setq type
(assq (char-after) c-special-brace-lists
)))
8085 (if (setq type
(assq (char-after) c-special-brace-lists
))
8087 (setq inner-beg
(point))
8088 (c-backward-syntactic-ws)
8090 (setq beg
(if (eq (char-after) ?\
()
8098 (= (char-before) ?\
)))
8100 (goto-char inner-beg
)
8101 (if (looking-at "\\s(")
8102 ;; Check balancing of the inner paren
8107 ;; If the inner char isn't a paren then
8108 ;; we can't check balancing, so just
8109 ;; check the char before the outer
8113 (c-backward-syntactic-ws)
8114 (= (char-before) (cdr type
)))))
8115 (if (or (/= (char-syntax (char-before)) ?\
))
8117 (c-forward-syntactic-ws)
8120 (cons (cons beg end
) type
))
8121 (cons (list beg
) type
)))))
8124 (defun c-looking-at-bos (&optional lim
)
8125 ;; Return non-nil if between two statements or declarations, assuming
8126 ;; point is not inside a literal or comment.
8128 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8129 ;; are recommended instead.
8131 ;; This function might do hidden buffer changes.
8132 (c-at-statement-start-p))
8133 (make-obsolete 'c-looking-at-bos
'c-at-statement-start-p
"22.1")
8135 (defun c-looking-at-inexpr-block (lim containing-sexp
&optional check-at-end
)
8136 ;; Return non-nil if we're looking at the beginning of a block
8137 ;; inside an expression. The value returned is actually a cons of
8138 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8139 ;; position of the beginning of the construct.
8141 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8142 ;; position of the closest containing list. If it's nil, the
8143 ;; containing paren isn't used to decide whether we're inside an
8144 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8145 ;; needs to be farther back.
8147 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8148 ;; brace block might be done. It should only be used when the
8149 ;; construct can be assumed to be complete, i.e. when the original
8150 ;; starting position was further down than that.
8152 ;; This function might do hidden buffer changes.
8155 (let ((res 'maybe
) passed-paren
8156 (closest-lim (or containing-sexp lim
(point-min)))
8157 ;; Look at the character after point only as a last resort
8158 ;; when we can't disambiguate.
8159 (block-follows (and (eq (char-after) ?
{) (point))))
8161 (while (and (eq res
'maybe
)
8162 (progn (c-backward-syntactic-ws)
8163 (> (point) closest-lim
))
8165 (progn (backward-char)
8166 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8167 (c-safe (forward-char)
8168 (goto-char (scan-sexps (point) -
1))))
8171 (if (looking-at c-keywords-regexp
)
8172 (let ((kw-sym (c-keyword-sym (match-string 1))))
8175 (c-keyword-member kw-sym
'c-inexpr-class-kwds
))
8176 (and (not (eq passed-paren ?\
[))
8177 (or (not (looking-at c-class-key
))
8178 ;; If the class definition is at the start of
8179 ;; a statement, we don't consider it an
8180 ;; in-expression class.
8181 (let ((prev (point)))
8183 (= (c-backward-token-2 1 nil closest-lim
) 0)
8184 (eq (char-syntax (char-after)) ?w
))
8185 (setq prev
(point)))
8187 (not (c-at-statement-start-p)))
8188 ;; Also, in Pike we treat it as an
8189 ;; in-expression class if it's used in an
8190 ;; object clone expression.
8193 (c-major-mode-is 'pike-mode
)
8194 (progn (goto-char block-follows
)
8195 (zerop (c-forward-token-2 1 t
)))
8196 (eq (char-after) ?\
())))
8197 (cons 'inexpr-class
(point))))
8198 ((c-keyword-member kw-sym
'c-inexpr-block-kwds
)
8199 (when (not passed-paren
)
8200 (cons 'inexpr-statement
(point))))
8201 ((c-keyword-member kw-sym
'c-lambda-kwds
)
8202 (when (or (not passed-paren
)
8203 (eq passed-paren ?\
())
8204 (cons 'inlambda
(point))))
8205 ((c-keyword-member kw-sym
'c-block-stmt-kwds
)
8210 (if (looking-at "\\s(")
8212 (if (and (eq passed-paren ?\
[)
8213 (eq (char-after) ?\
[))
8214 ;; Accept several square bracket sexps for
8215 ;; Java array initializations.
8217 (setq passed-paren
(char-after))
8222 (when (and c-recognize-paren-inexpr-blocks
8225 (eq (char-after containing-sexp
) ?\
())
8226 (goto-char containing-sexp
)
8227 (if (or (save-excursion
8228 (c-backward-syntactic-ws lim
)
8229 (and (> (point) (or lim
(point-min)))
8231 (and c-special-brace-lists
8232 (c-looking-at-special-brace-list)))
8234 (cons 'inexpr-statement
(point))))
8238 (defun c-looking-at-inexpr-block-backward (paren-state)
8239 ;; Returns non-nil if we're looking at the end of an in-expression
8240 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8241 ;; PAREN-STATE is the paren state relevant at the current position.
8243 ;; This function might do hidden buffer changes.
8245 ;; We currently only recognize a block.
8246 (let ((here (point))
8247 (elem (car-safe paren-state
))
8249 (when (and (consp elem
)
8250 (progn (goto-char (cdr elem
))
8251 (c-forward-syntactic-ws here
)
8253 (goto-char (car elem
))
8254 (if (setq paren-state
(cdr paren-state
))
8255 (setq containing-sexp
(car-safe paren-state
)))
8256 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8258 containing-sexp
)))))
8261 ;; `c-guess-basic-syntax' and the functions that precedes it below
8262 ;; implements the main decision tree for determining the syntactic
8263 ;; analysis of the current line of code.
8265 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8266 ;; auto newline analysis.
8267 (defvar c-auto-newline-analysis nil
)
8269 (defun c-brace-anchor-point (bracepos)
8270 ;; BRACEPOS is the position of a brace in a construct like "namespace
8271 ;; Bar {". Return the anchor point in this construct; this is the
8272 ;; earliest symbol on the brace's line which isn't earlier than
8275 ;; Currently (2007-08-17), "like namespace" means "matches
8276 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8277 ;; or anything like that.
8279 (let ((boi (c-point 'boi bracepos
)))
8280 (goto-char bracepos
)
8281 (while (and (> (point) boi
)
8282 (not (looking-at c-other-decl-block-key
)))
8283 (c-backward-token-2))
8284 (if (> (point) boi
) (point) boi
))))
8286 (defsubst c-add-syntax
(symbol &rest args
)
8287 ;; A simple function to prepend a new syntax element to
8288 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8289 ;; should always be dynamically bound but since we read it first
8290 ;; we'll fail properly anyway if this function is misused.
8291 (setq c-syntactic-context
(cons (cons symbol args
)
8292 c-syntactic-context
)))
8294 (defsubst c-append-syntax
(symbol &rest args
)
8295 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8296 ;; (Normally not necessary.)
8297 (setq c-syntactic-context
(nconc c-syntactic-context
8298 (list (cons symbol args
)))))
8300 (defun c-add-stmt-syntax (syntax-symbol
8305 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8306 ;; needed with further syntax elements of the types `substatement',
8307 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8308 ;; `defun-block-intro'.
8310 ;; Do the generic processing to anchor the given syntax symbol on
8311 ;; the preceding statement: Skip over any labels and containing
8312 ;; statements on the same line, and then search backward until we
8313 ;; find a statement or block start that begins at boi without a
8314 ;; label or comment.
8316 ;; Point is assumed to be at the prospective anchor point for the
8317 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8318 ;; skip past open parens and containing statements. Most of the added
8319 ;; syntax elements will get the same anchor point - the exception is
8320 ;; for an anchor in a construct like "namespace"[*] - this is as early
8321 ;; as possible in the construct but on the same line as the {.
8323 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8325 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8326 ;; syntax symbol. They are appended after the anchor point.
8328 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8329 ;; if the current statement starts there.
8331 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8332 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8334 ;; This function might do hidden buffer changes.
8336 (if (= (point) (c-point 'boi
))
8337 ;; This is by far the most common case, so let's give it special
8339 (apply 'c-add-syntax syntax-symbol
(point) syntax-extra-args
)
8341 (let ((syntax-last c-syntactic-context
)
8342 (boi (c-point 'boi
))
8343 ;; Set when we're on a label, so that we don't stop there.
8344 ;; FIXME: To be complete we should check if we're on a label
8345 ;; now at the start.
8348 ;; Use point as the anchor point for "namespace", "extern", etc.
8349 (apply 'c-add-syntax syntax-symbol
8350 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist
)
8354 ;; Loop while we have to back out of containing blocks.
8357 (catch 'back-up-block
8359 ;; Loop while we have to back up statements.
8360 (while (or (/= (point) boi
)
8362 (looking-at c-comment-start-regexp
))
8364 ;; Skip past any comments that stands between the
8365 ;; statement start and boi.
8366 (let ((savepos (point)))
8367 (while (and (/= savepos boi
)
8368 (c-backward-single-comment))
8369 (setq savepos
(point)
8370 boi
(c-point 'boi
)))
8371 (goto-char savepos
))
8373 ;; Skip to the beginning of this statement or backward
8375 (let ((old-pos (point))
8377 (step-type (c-beginning-of-statement-1 containing-sexp
)))
8378 (setq boi
(c-point 'boi
)
8379 on-label
(eq step-type
'label
))
8381 (cond ((= (point) old-pos
)
8382 ;; If we didn't move we're at the start of a block and
8383 ;; have to continue outside it.
8384 (throw 'back-up-block t
))
8386 ((and (eq step-type
'up
)
8387 (>= (point) old-boi
)
8388 (looking-at "else\\>[^_]")
8391 (looking-at "if\\>[^_]")))
8392 ;; Special case to avoid deeper and deeper indentation
8393 ;; of "else if" clauses.
8396 ((and (not stop-at-boi-only
)
8397 (/= old-pos old-boi
)
8398 (memq step-type
'(up previous
)))
8399 ;; If stop-at-boi-only is nil, we shouldn't back up
8400 ;; over previous or containing statements to try to
8401 ;; reach boi, so go back to the last position and
8404 (throw 'back-up-block nil
))
8407 (if (and (not stop-at-boi-only
)
8408 (memq step-type
'(up previous beginning
)))
8409 ;; If we've moved into another statement then we
8410 ;; should no longer try to stop in the middle of a
8412 (setq stop-at-boi-only t
))
8414 ;; Record this as a substatement if we skipped up one
8416 (when (eq step-type
'up
)
8417 (c-add-syntax 'substatement nil
))))
8422 ;; Now we have to go out of this block.
8423 (goto-char containing-sexp
)
8425 ;; Don't stop in the middle of a special brace list opener
8427 (when c-special-brace-lists
8428 (let ((special-list (c-looking-at-special-brace-list)))
8429 (when (and special-list
8430 (< (car (car special-list
)) (point)))
8431 (setq containing-sexp
(car (car special-list
)))
8432 (goto-char containing-sexp
))))
8434 (setq paren-state
(c-whack-state-after containing-sexp paren-state
)
8435 containing-sexp
(c-most-enclosing-brace paren-state
)
8438 ;; Analyze the construct in front of the block we've stepped out
8439 ;; from and add the right syntactic element for it.
8440 (let ((paren-pos (point))
8441 (paren-char (char-after))
8444 (if (eq paren-char ?\
()
8445 ;; Stepped out of a parenthesis block, so we're in an
8448 (when (/= paren-pos boi
)
8449 (if (and c-recognize-paren-inexpr-blocks
8451 (c-backward-syntactic-ws containing-sexp
)
8452 (or (not (looking-at "\\>"))
8453 (not (c-on-identifier))))
8455 (goto-char (1+ paren-pos
))
8456 (c-forward-syntactic-ws)
8457 (eq (char-after) ?
{)))
8458 ;; Stepped out of an in-expression statement. This
8459 ;; syntactic element won't get an anchor pos.
8460 (c-add-syntax 'inexpr-statement
)
8462 ;; A parenthesis normally belongs to an arglist.
8463 (c-add-syntax 'arglist-cont-nonempty nil paren-pos
)))
8467 (1+ containing-sexp
)
8469 (setq step-type
'same
8472 ;; Stepped out of a brace block.
8473 (setq step-type
(c-beginning-of-statement-1 containing-sexp
)
8474 on-label
(eq step-type
'label
))
8476 (if (and (eq step-type
'same
)
8477 (/= paren-pos
(point)))
8481 (goto-char paren-pos
)
8482 (setq inexpr
(c-looking-at-inexpr-block
8483 (c-safe-position containing-sexp paren-state
)
8485 (c-add-syntax (if (eq (car inexpr
) 'inlambda
)
8487 'statement-block-intro
)
8489 ((looking-at c-other-decl-block-key
)
8491 (cdr (assoc (match-string 1)
8492 c-other-decl-block-key-in-symbols-alist
))
8493 (max (c-point 'boi paren-pos
) (point))))
8494 (t (c-add-syntax 'defun-block-intro nil
))))
8496 (c-add-syntax 'statement-block-intro nil
)))
8498 (if (= paren-pos boi
)
8499 ;; Always done if the open brace was at boi. The
8500 ;; c-beginning-of-statement-1 call above is necessary
8501 ;; anyway, to decide the type of block-intro to add.
8502 (goto-char paren-pos
)
8503 (setq boi
(c-point 'boi
)))
8506 ;; Fill in the current point as the anchor for all the symbols
8508 (let ((p c-syntactic-context
) q
)
8509 (while (not (eq p syntax-last
))
8510 (setq q
(cdr (car p
))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8518 (defun c-add-class-syntax (symbol
8519 containing-decl-open
8520 containing-decl-start
8523 ;; The inclass and class-close syntactic symbols are added in
8524 ;; several places and some work is needed to fix everything.
8525 ;; Therefore it's collected here.
8527 ;; This function might do hidden buffer changes.
8528 (goto-char containing-decl-open
)
8529 (if (and (eq symbol
'inclass
) (= (point) (c-point 'boi
)))
8531 (c-add-syntax symbol containing-decl-open
)
8532 containing-decl-open
)
8533 (goto-char containing-decl-start
)
8534 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8535 ;; here, but we have to do like this for compatibility.
8536 (back-to-indentation)
8537 (c-add-syntax symbol
(point))
8538 (if (and (c-keyword-member containing-decl-kwd
8539 'c-inexpr-class-kwds
)
8540 (/= containing-decl-start
(c-point 'boi containing-decl-start
)))
8541 (c-add-syntax 'inexpr-class
))
8544 (defun c-guess-continued-construct (indent-point
8546 beg-of-same-or-containing-stmt
8549 ;; This function contains the decision tree reached through both
8550 ;; cases 18 and 10. It's a continued statement or top level
8551 ;; construct of some kind.
8553 ;; This function might do hidden buffer changes.
8555 (let (special-brace-list placeholder
)
8556 (goto-char indent-point
)
8557 (skip-chars-forward " \t")
8560 ;; (CASE A removed.)
8561 ;; CASE B: open braces for class or brace-lists
8562 ((setq special-brace-list
8563 (or (and c-special-brace-lists
8564 (c-looking-at-special-brace-list))
8565 (eq char-after-ip ?
{)))
8568 ;; CASE B.1: class-open
8570 (and (eq (char-after) ?
{)
8571 (c-looking-at-decl-block containing-sexp t
)
8572 (setq beg-of-same-or-containing-stmt
(point))))
8573 (c-add-syntax 'class-open beg-of-same-or-containing-stmt
))
8575 ;; CASE B.2: brace-list-open
8576 ((or (consp special-brace-list
)
8578 (goto-char beg-of-same-or-containing-stmt
)
8579 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8580 indent-point t t t
)))
8581 ;; The most semantically accurate symbol here is
8582 ;; brace-list-open, but we normally report it simply as a
8583 ;; statement-cont. The reason is that one normally adjusts
8584 ;; brace-list-open for brace lists as top-level constructs,
8585 ;; and brace lists inside statements is a completely different
8586 ;; context. C.f. case 5A.3.
8587 (c-beginning-of-statement-1 containing-sexp
)
8588 (c-add-stmt-syntax (if c-auto-newline-analysis
8589 ;; Turn off the dwim above when we're
8590 ;; analyzing the nature of the brace
8591 ;; for the auto newline feature.
8595 containing-sexp paren-state
))
8597 ;; CASE B.3: The body of a function declared inside a normal
8598 ;; block. Can occur e.g. in Pike and when using gcc
8599 ;; extensions, but watch out for macros followed by blocks.
8600 ;; C.f. cases E, 16F and 17G.
8601 ((and (not (c-at-statement-start-p))
8602 (eq (c-beginning-of-statement-1 containing-sexp nil nil t
)
8605 (let ((c-recognize-typeless-decls nil
))
8606 ;; Turn off recognition of constructs that lacks a
8607 ;; type in this case, since that's more likely to be
8608 ;; a macro followed by a block.
8609 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
8610 (c-add-stmt-syntax 'defun-open nil t
8611 containing-sexp paren-state
))
8613 ;; CASE B.4: Continued statement with block open. The most
8614 ;; accurate analysis is perhaps `statement-cont' together with
8615 ;; `block-open' but we play DWIM and use `substatement-open'
8616 ;; instead. The rationaly is that this typically is a macro
8617 ;; followed by a block which makes it very similar to a
8618 ;; statement with a substatement block.
8620 (c-add-stmt-syntax 'substatement-open nil nil
8621 containing-sexp paren-state
))
8624 ;; CASE C: iostream insertion or extraction operator
8625 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8627 (goto-char beg-of-same-or-containing-stmt
)
8628 ;; If there is no preceding streamop in the statement
8629 ;; then indent this line as a normal statement-cont.
8630 (when (c-syntactic-re-search-forward
8631 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point
'move t t
)
8632 (c-add-syntax 'stream-op
(c-point 'boi
))
8635 ;; CASE E: In the "K&R region" of a function declared inside a
8636 ;; normal block. C.f. case B.3.
8637 ((and (save-excursion
8638 ;; Check that the next token is a '{'. This works as
8639 ;; long as no language that allows nested function
8640 ;; definitions allows stuff like member init lists, K&R
8641 ;; declarations or throws clauses there.
8643 ;; Note that we do a forward search for something ahead
8644 ;; of the indentation line here. That's not good since
8645 ;; the user might not have typed it yet. Unfortunately
8646 ;; it's exceedingly tricky to recognize a function
8647 ;; prototype in a code block without resorting to this.
8648 (c-forward-syntactic-ws)
8649 (eq (char-after) ?
{))
8650 (not (c-at-statement-start-p))
8651 (eq (c-beginning-of-statement-1 containing-sexp nil nil t
)
8654 (let ((c-recognize-typeless-decls nil
))
8655 ;; Turn off recognition of constructs that lacks a
8656 ;; type in this case, since that's more likely to be
8657 ;; a macro followed by a block.
8658 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
8659 (c-add-stmt-syntax 'func-decl-cont nil t
8660 containing-sexp paren-state
))
8662 ;;CASE F: continued statement and the only preceding items are
8664 ((and (c-major-mode-is 'java-mode
)
8665 (setq placeholder
(point))
8666 (c-beginning-of-statement-1)
8668 (while (and (c-forward-annotation)
8669 (< (point) placeholder
))
8670 (c-forward-syntactic-ws))
8673 (>= (point) placeholder
)
8674 (goto-char placeholder
)))
8675 (c-beginning-of-statement-1 containing-sexp
)
8676 (c-add-syntax 'annotation-var-cont
(point)))
8678 ;; CASE D: continued statement.
8680 (c-beginning-of-statement-1 containing-sexp
)
8681 (c-add-stmt-syntax 'statement-cont nil nil
8682 containing-sexp paren-state
))
8685 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
8688 (defun c-guess-basic-syntax ()
8689 "Return the syntactic context of the current line."
8692 (c-save-buffer-state
8693 ((indent-point (point))
8694 (case-fold-search nil
)
8695 ;; A whole ugly bunch of various temporary variables. Have
8696 ;; to declare them here since it's not possible to declare
8697 ;; a variable with only the scope of a cond test and the
8698 ;; following result clauses, and most of this function is a
8699 ;; single gigantic cond. :P
8700 literal char-before-ip before-ws-ip char-after-ip macro-start
8701 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
8702 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
8704 ;; The following record some positions for the containing
8705 ;; declaration block if we're directly within one:
8706 ;; `containing-decl-open' is the position of the open
8707 ;; brace. `containing-decl-start' is the start of the
8708 ;; declaration. `containing-decl-kwd' is the keyword
8709 ;; symbol of the keyword that tells what kind of block it
8711 containing-decl-open
8712 containing-decl-start
8714 ;; The open paren of the closest surrounding sexp or nil if
8717 ;; The position after the closest preceding brace sexp
8718 ;; (nested sexps are ignored), or the position after
8719 ;; `containing-sexp' if there is none, or (point-min) if
8720 ;; `containing-sexp' is nil.
8722 ;; The paren state outside `containing-sexp', or at
8723 ;; `indent-point' if `containing-sexp' is nil.
8724 (paren-state (c-parse-state))
8725 ;; There's always at most one syntactic element which got
8726 ;; an anchor pos. It's stored in syntactic-relpos.
8728 (c-stmt-delim-chars c-stmt-delim-chars
))
8730 ;; Check if we're directly inside an enclosing declaration
8732 (when (and (setq containing-sexp
8733 (c-most-enclosing-brace paren-state
))
8735 (goto-char containing-sexp
)
8736 (eq (char-after) ?
{))
8738 (c-looking-at-decl-block
8739 (c-most-enclosing-brace paren-state
8742 (setq containing-decl-open containing-sexp
8743 containing-decl-start
(point)
8744 containing-sexp nil
)
8745 (goto-char placeholder
)
8746 (setq containing-decl-kwd
(and (looking-at c-keywords-regexp
)
8747 (c-keyword-sym (match-string 1)))))
8749 ;; Init some position variables.
8752 (setq containing-sexp
(car paren-state
)
8753 paren-state
(cdr paren-state
))
8754 (if (consp containing-sexp
)
8756 (setq lim
(cdr containing-sexp
))
8757 (if (cdr c-state-cache
)
8758 ;; Ignore balanced paren. The next entry
8759 ;; can't be another one.
8760 (setq containing-sexp
(car (cdr c-state-cache
))
8761 paren-state
(cdr paren-state
))
8762 ;; If there is no surrounding open paren then
8763 ;; put the last balanced pair back on paren-state.
8764 (setq paren-state
(cons containing-sexp paren-state
)
8765 containing-sexp nil
)))
8766 (setq lim
(1+ containing-sexp
))))
8767 (setq lim
(point-min)))
8769 ;; If we're in a parenthesis list then ',' delimits the
8770 ;; "statements" rather than being an operator (with the
8771 ;; exception of the "for" clause). This difference is
8772 ;; typically only noticeable when statements are used in macro
8774 (when (and containing-sexp
8775 (eq (char-after containing-sexp
) ?\
())
8776 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma
))
8777 ;; cache char before and after indent point, and move point to
8778 ;; the most likely position to perform the majority of tests
8779 (goto-char indent-point
)
8780 (c-backward-syntactic-ws lim
)
8781 (setq before-ws-ip
(point)
8782 char-before-ip
(char-before))
8783 (goto-char indent-point
)
8784 (skip-chars-forward " \t")
8785 (setq char-after-ip
(char-after))
8787 ;; are we in a literal?
8788 (setq literal
(c-in-literal lim
))
8790 ;; now figure out syntactic qualities of the current line
8793 ;; CASE 1: in a string.
8794 ((eq literal
'string
)
8795 (c-add-syntax 'string
(c-point 'bopl
)))
8797 ;; CASE 2: in a C or C++ style comment.
8798 ((and (memq literal
'(c c
++))
8799 ;; This is a kludge for XEmacs where we use
8800 ;; `buffer-syntactic-context', which doesn't correctly
8801 ;; recognize "\*/" to end a block comment.
8802 ;; `parse-partial-sexp' which is used by
8803 ;; `c-literal-limits' will however do that in most
8804 ;; versions, which results in that we get nil from
8805 ;; `c-literal-limits' even when `c-in-literal' claims
8806 ;; we're inside a comment.
8807 (setq placeholder
(c-literal-limits lim
)))
8808 (c-add-syntax literal
(car placeholder
)))
8810 ;; CASE 3: in a cpp preprocessor macro continuation.
8811 ((and (save-excursion
8812 (when (c-beginning-of-macro)
8813 (setq macro-start
(point))))
8814 (/= macro-start
(c-point 'boi
))
8816 (setq tmpsymbol
'cpp-macro-cont
)
8817 (or (not c-syntactic-indentation-in-macros
)
8819 (goto-char macro-start
)
8820 ;; If at the beginning of the body of a #define
8821 ;; directive then analyze as cpp-define-intro
8822 ;; only. Go on with the syntactic analysis
8823 ;; otherwise. in-macro-expr is set if we're in a
8824 ;; cpp expression, i.e. before the #define body
8825 ;; or anywhere in a non-#define directive.
8826 (if (c-forward-to-cpp-define-body)
8827 (let ((indent-boi (c-point 'boi indent-point
)))
8828 (setq in-macro-expr
(> (point) indent-boi
)
8829 tmpsymbol
'cpp-define-intro
)
8830 (= (point) indent-boi
))
8831 (setq in-macro-expr t
)
8833 (c-add-syntax tmpsymbol macro-start
)
8834 (setq macro-start nil
))
8836 ;; CASE 11: an else clause?
8837 ((looking-at "else\\>[^_]")
8838 (c-beginning-of-statement-1 containing-sexp
)
8839 (c-add-stmt-syntax 'else-clause nil t
8840 containing-sexp paren-state
))
8842 ;; CASE 12: while closure of a do/while construct?
8843 ((and (looking-at "while\\>[^_]")
8845 (prog1 (eq (c-beginning-of-statement-1 containing-sexp
)
8847 (setq placeholder
(point)))))
8848 (goto-char placeholder
)
8849 (c-add-stmt-syntax 'do-while-closure nil t
8850 containing-sexp paren-state
))
8852 ;; CASE 13: A catch or finally clause? This case is simpler
8853 ;; than if-else and do-while, because a block is required
8854 ;; after every try, catch and finally.
8856 (and (cond ((c-major-mode-is 'c
++-mode
)
8857 (looking-at "catch\\>[^_]"))
8858 ((c-major-mode-is 'java-mode
)
8859 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
8860 (and (c-safe (c-backward-syntactic-ws)
8863 (eq (char-after) ?
{)
8864 (c-safe (c-backward-syntactic-ws)
8867 (if (eq (char-after) ?\
()
8868 (c-safe (c-backward-sexp) t
)
8870 (looking-at "\\(try\\|catch\\)\\>[^_]")
8871 (setq placeholder
(point))))
8872 (goto-char placeholder
)
8873 (c-add-stmt-syntax 'catch-clause nil t
8874 containing-sexp paren-state
))
8876 ;; CASE 18: A substatement we can recognize by keyword.
8878 (and c-opt-block-stmt-key
8879 (not (eq char-before-ip ?\
;))
8880 (not (c-at-vsemi-p before-ws-ip
))
8881 (not (memq char-after-ip
'(?\
) ?\
] ?
,)))
8882 (or (not (eq char-before-ip ?
}))
8883 (c-looking-at-inexpr-block-backward c-state-cache
))
8886 ;; Ought to cache the result from the
8887 ;; c-beginning-of-statement-1 calls here.
8888 (setq placeholder
(point))
8889 (while (eq (setq step-type
8890 (c-beginning-of-statement-1 lim
))
8892 (if (eq step-type
'previous
)
8893 (goto-char placeholder
)
8894 (setq placeholder
(point))
8895 (if (and (eq step-type
'same
)
8896 (not (looking-at c-opt-block-stmt-key
)))
8897 ;; Step up to the containing statement if we
8898 ;; stayed in the same one.
8902 (c-beginning-of-statement-1 lim
))
8905 (setq placeholder
(point))
8906 ;; There was no containing statement afterall.
8907 (goto-char placeholder
)))))
8909 (if (looking-at c-block-stmt-2-key
)
8910 ;; Require a parenthesis after these keywords.
8911 ;; Necessary to catch e.g. synchronized in Java,
8912 ;; which can be used both as statement and
8914 (and (zerop (c-forward-token-2 1 nil
))
8915 (eq (char-after) ?\
())
8916 (looking-at c-opt-block-stmt-key
))))
8918 (if (eq step-type
'up
)
8919 ;; CASE 18A: Simple substatement.
8921 (goto-char placeholder
)
8923 ((eq char-after-ip ?
{)
8924 (c-add-stmt-syntax 'substatement-open nil nil
8925 containing-sexp paren-state
))
8927 (goto-char indent-point
)
8928 (back-to-indentation)
8930 (c-add-stmt-syntax 'substatement-label nil nil
8931 containing-sexp paren-state
))
8933 (c-add-stmt-syntax 'substatement nil nil
8934 containing-sexp paren-state
))))
8936 ;; CASE 18B: Some other substatement. This is shared
8938 (c-guess-continued-construct indent-point
8944 ;; CASE 14: A case or default label
8945 ((looking-at c-label-kwds-regexp
)
8948 (goto-char containing-sexp
)
8949 (setq lim
(c-most-enclosing-brace c-state-cache
8951 (c-backward-to-block-anchor lim
)
8952 (c-add-stmt-syntax 'case-label nil t lim paren-state
))
8953 ;; Got a bogus label at the top level. In lack of better
8954 ;; alternatives, anchor it on (point-min).
8955 (c-add-syntax 'case-label
(point-min))))
8957 ;; CASE 15: any other label
8959 (back-to-indentation)
8960 (and (not (looking-at c-syntactic-ws-start
))
8962 (cond (containing-decl-open
8963 (setq placeholder
(c-add-class-syntax 'inclass
8964 containing-decl-open
8965 containing-decl-start
8968 ;; Append access-label with the same anchor point as
8970 (c-append-syntax 'access-label placeholder
))
8973 (goto-char containing-sexp
)
8974 (setq lim
(c-most-enclosing-brace c-state-cache
8978 (if (and (eq (c-beginning-of-statement-1 lim
) 'up
)
8979 (looking-at "switch\\>[^_]"))
8980 ;; If the surrounding statement is a switch then
8981 ;; let's analyze all labels as switch labels, so
8982 ;; that they get lined up consistently.
8985 (c-backward-to-block-anchor lim
)
8986 (c-add-stmt-syntax tmpsymbol nil t lim paren-state
))
8989 ;; A label on the top level. Treat it as a class
8990 ;; context. (point-min) is the closest we get to the
8991 ;; class open brace.
8992 (c-add-syntax 'access-label
(point-min)))))
8994 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
8996 ((setq placeholder
(c-looking-at-inexpr-block
8997 (c-safe-position containing-sexp paren-state
)
8999 ;; Have to turn on the heuristics after
9000 ;; the point even though it doesn't work
9001 ;; very well. C.f. test case class-16.pike.
9003 (setq tmpsymbol
(assq (car placeholder
)
9004 '((inexpr-class . class-open
)
9005 (inexpr-statement . block-open
))))
9007 ;; It's a statement block or an anonymous class.
9008 (setq tmpsymbol
(cdr tmpsymbol
))
9009 ;; It's a Pike lambda. Check whether we are between the
9010 ;; lambda keyword and the argument list or at the defun
9012 (setq tmpsymbol
(if (eq char-after-ip ?
{)
9014 'lambda-intro-cont
)))
9015 (goto-char (cdr placeholder
))
9016 (back-to-indentation)
9017 (c-add-stmt-syntax tmpsymbol nil t
9018 (c-most-enclosing-brace c-state-cache
(point))
9020 (unless (eq (point) (cdr placeholder
))
9021 (c-add-syntax (car placeholder
))))
9023 ;; CASE 5: Line is inside a declaration level block or at top level.
9024 ((or containing-decl-open
(null containing-sexp
))
9027 ;; CASE 5A: we are looking at a defun, brace list, class,
9028 ;; or inline-inclass method opening brace
9029 ((setq special-brace-list
9030 (or (and c-special-brace-lists
9031 (c-looking-at-special-brace-list))
9032 (eq char-after-ip ?
{)))
9035 ;; CASE 5A.1: Non-class declaration block open.
9038 (and (eq char-after-ip ?
{)
9039 (setq tmp
(c-looking-at-decl-block containing-sexp t
))
9041 (setq placeholder
(point))
9043 (looking-at c-symbol-key
))
9045 (c-keyword-sym (setq keyword
(match-string 0)))
9046 'c-other-block-decl-kwds
))))
9047 (goto-char placeholder
)
9049 (if (string-equal keyword
"extern")
9050 ;; Special case for extern-lang-open.
9052 (intern (concat keyword
"-open")))
9053 nil t containing-sexp paren-state
))
9055 ;; CASE 5A.2: we are looking at a class opening brace
9057 (goto-char indent-point
)
9058 (skip-chars-forward " \t")
9059 (and (eq (char-after) ?
{)
9060 (c-looking-at-decl-block containing-sexp t
)
9061 (setq placeholder
(point))))
9062 (c-add-syntax 'class-open placeholder
))
9064 ;; CASE 5A.3: brace list open
9066 (c-beginning-of-decl-1 lim
)
9067 (while (looking-at c-specifier-key
)
9068 (goto-char (match-end 1))
9069 (c-forward-syntactic-ws indent-point
))
9070 (setq placeholder
(c-point 'boi
))
9071 (or (consp special-brace-list
)
9072 (and (or (save-excursion
9073 (goto-char indent-point
)
9074 (setq tmpsymbol nil
)
9075 (while (and (> (point) placeholder
)
9076 (zerop (c-backward-token-2 1 t
))
9077 (/= (char-after) ?
=))
9078 (and c-opt-inexpr-brace-list-key
9080 (looking-at c-opt-inexpr-brace-list-key
)
9081 (setq tmpsymbol
'topmost-intro-cont
)))
9082 (eq (char-after) ?
=))
9083 (looking-at c-brace-list-key
))
9085 (while (and (< (point) indent-point
)
9086 (zerop (c-forward-token-2 1 t
))
9087 (not (memq (char-after) '(?\
; ?\()))))
9088 (not (memq (char-after) '(?\
; ?\()))
9090 (if (and (not c-auto-newline-analysis
)
9091 (c-major-mode-is 'java-mode
)
9092 (eq tmpsymbol
'topmost-intro-cont
))
9093 ;; We're in Java and have found that the open brace
9094 ;; belongs to a "new Foo[]" initialization list,
9095 ;; which means the brace list is part of an
9096 ;; expression and not a top level definition. We
9097 ;; therefore treat it as any topmost continuation
9098 ;; even though the semantically correct symbol still
9099 ;; is brace-list-open, on the same grounds as in
9102 (c-beginning-of-statement-1 lim
)
9103 (c-add-syntax 'topmost-intro-cont
(c-point 'boi
)))
9104 (c-add-syntax 'brace-list-open placeholder
)))
9106 ;; CASE 5A.4: inline defun open
9107 ((and containing-decl-open
9108 (not (c-keyword-member containing-decl-kwd
9109 'c-other-block-decl-kwds
)))
9110 (c-add-syntax 'inline-open
)
9111 (c-add-class-syntax 'inclass
9112 containing-decl-open
9113 containing-decl-start
9117 ;; CASE 5A.5: ordinary defun open
9120 (c-beginning-of-decl-1 lim
)
9121 (while (looking-at c-specifier-key
)
9122 (goto-char (match-end 1))
9123 (c-forward-syntactic-ws indent-point
))
9124 (c-add-syntax 'defun-open
(c-point 'boi
))
9125 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9129 ;; CASE 5B: After a function header but before the body (or
9130 ;; the ending semicolon if there's no body).
9132 (when (setq placeholder
(c-just-after-func-arglist-p lim
))
9133 (setq tmp-pos
(point))))
9136 ;; CASE 5B.1: Member init list.
9137 ((eq (char-after tmp-pos
) ?
:)
9138 (if (or (> tmp-pos indent-point
)
9139 (= (c-point 'bosws
) (1+ tmp-pos
)))
9141 ;; There is no preceding member init clause.
9142 ;; Indent relative to the beginning of indentation
9143 ;; for the topmost-intro line that contains the
9144 ;; prototype's open paren.
9145 (goto-char placeholder
)
9146 (c-add-syntax 'member-init-intro
(c-point 'boi
)))
9147 ;; Indent relative to the first member init clause.
9148 (goto-char (1+ tmp-pos
))
9149 (c-forward-syntactic-ws)
9150 (c-add-syntax 'member-init-cont
(point))))
9152 ;; CASE 5B.2: K&R arg decl intro
9153 ((and c-recognize-knr-p
9154 (c-in-knr-argdecl lim
))
9155 (c-beginning-of-statement-1 lim
)
9156 (c-add-syntax 'knr-argdecl-intro
(c-point 'boi
))
9157 (if containing-decl-open
9158 (c-add-class-syntax 'inclass
9159 containing-decl-open
9160 containing-decl-start
9164 ;; CASE 5B.4: Nether region after a C++ or Java func
9165 ;; decl, which could include a `throws' declaration.
9167 (c-beginning-of-statement-1 lim
)
9168 (c-add-syntax 'func-decl-cont
(c-point 'boi
))
9171 ;; CASE 5C: inheritance line. could be first inheritance
9172 ;; line, or continuation of a multiple inheritance
9173 ((or (and (c-major-mode-is 'c
++-mode
)
9175 (when (eq char-after-ip ?
,)
9176 (skip-chars-forward " \t")
9178 (looking-at c-opt-postfix-decl-spec-key
)))
9179 (and (or (eq char-before-ip ?
:)
9180 ;; watch out for scope operator
9182 (and (eq char-after-ip ?
:)
9183 (c-safe (forward-char 1) t
)
9184 (not (eq (char-after) ?
:))
9187 (c-backward-syntactic-ws lim
)
9188 (if (eq char-before-ip ?
:)
9191 (c-backward-syntactic-ws lim
)))
9192 (back-to-indentation)
9193 (looking-at c-class-key
)))
9195 (and (c-major-mode-is 'java-mode
)
9196 (let ((fence (save-excursion
9197 (c-beginning-of-statement-1 lim
)
9202 (cond ((looking-at c-opt-postfix-decl-spec-key
)
9203 (setq injava-inher
(cons cont
(point))
9205 ((or (not (c-safe (c-forward-sexp -
1) t
))
9211 (not (c-crosses-statement-barrier-p (cdr injava-inher
)
9216 ;; CASE 5C.1: non-hanging colon on an inher intro
9217 ((eq char-after-ip ?
:)
9218 (c-beginning-of-statement-1 lim
)
9219 (c-add-syntax 'inher-intro
(c-point 'boi
))
9220 ;; don't add inclass symbol since relative point already
9221 ;; contains any class offset
9224 ;; CASE 5C.2: hanging colon on an inher intro
9225 ((eq char-before-ip ?
:)
9226 (c-beginning-of-statement-1 lim
)
9227 (c-add-syntax 'inher-intro
(c-point 'boi
))
9228 (if containing-decl-open
9229 (c-add-class-syntax 'inclass
9230 containing-decl-open
9231 containing-decl-start
9235 ;; CASE 5C.3: in a Java implements/extends
9237 (let ((where (cdr injava-inher
))
9238 (cont (car injava-inher
)))
9240 (cond ((looking-at "throws\\>[^_]")
9241 (c-add-syntax 'func-decl-cont
9242 (progn (c-beginning-of-statement-1 lim
)
9244 (cont (c-add-syntax 'inher-cont where
))
9245 (t (c-add-syntax 'inher-intro
9246 (progn (goto-char (cdr injava-inher
))
9247 (c-beginning-of-statement-1 lim
)
9251 ;; CASE 5C.4: a continued inheritance line
9253 (c-beginning-of-inheritance-list lim
)
9254 (c-add-syntax 'inher-cont
(point))
9255 ;; don't add inclass symbol since relative point already
9256 ;; contains any class offset
9259 ;; CASE 5D: this could be a top-level initialization, a
9260 ;; member init list continuation, or a template argument
9261 ;; list continuation.
9263 ;; Note: We use the fact that lim is always after any
9264 ;; preceding brace sexp.
9265 (if c-recognize-
<>-arglists
9268 (c-syntactic-skip-backward "^;,=<>" lim t
)
9271 (when c-overloadable-operators-regexp
9272 (when (setq placeholder
(c-after-special-operator-id lim
))
9273 (goto-char placeholder
)
9276 ((eq (char-before) ?
>)
9277 (or (c-backward-<>-arglist nil lim
)
9280 ((eq (char-before) ?
<)
9283 (c-forward-<>-arglist nil
))
9284 (progn (forward-char)
9288 ;; NB: No c-after-special-operator-id stuff in this
9289 ;; clause - we assume only C++ needs it.
9290 (c-syntactic-skip-backward "^;,=" lim t
))
9291 (memq (char-before) '(?
, ?
= ?
<)))
9294 ;; CASE 5D.3: perhaps a template list continuation?
9295 ((and (c-major-mode-is 'c
++-mode
)
9298 (c-with-syntax-table c
++-template-syntax-table
9299 (goto-char indent-point
)
9300 (setq placeholder
(c-up-list-backward))
9302 (eq (char-after placeholder
) ?
<))))))
9303 (c-with-syntax-table c
++-template-syntax-table
9304 (goto-char placeholder
)
9305 (c-beginning-of-statement-1 lim t
)
9307 (c-backward-syntactic-ws lim
)
9308 (eq (char-before) ?
<))
9309 ;; In a nested template arglist.
9311 (goto-char placeholder
)
9312 (c-syntactic-skip-backward "^,;" lim t
)
9313 (c-forward-syntactic-ws))
9314 (back-to-indentation)))
9315 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9317 (c-add-syntax 'template-args-cont
(point) placeholder
))
9319 ;; CASE 5D.4: perhaps a multiple inheritance line?
9320 ((and (c-major-mode-is 'c
++-mode
)
9322 (c-beginning-of-statement-1 lim
)
9323 (setq placeholder
(point))
9324 (if (looking-at "static\\>[^_]")
9325 (c-forward-token-2 1 nil indent-point
))
9326 (and (looking-at c-class-key
)
9327 (zerop (c-forward-token-2 2 nil indent-point
))
9328 (if (eq (char-after) ?
<)
9329 (c-with-syntax-table c
++-template-syntax-table
9330 (zerop (c-forward-token-2 1 t indent-point
)))
9332 (eq (char-after) ?
:))))
9333 (goto-char placeholder
)
9334 (c-add-syntax 'inher-cont
(c-point 'boi
)))
9336 ;; CASE 5D.5: Continuation of the "expression part" of a
9337 ;; top level construct. Or, perhaps, an unrecognised construct.
9339 (while (and (setq placeholder
(point))
9340 (eq (car (c-beginning-of-decl-1 containing-sexp
))
9343 (c-backward-syntactic-ws)
9344 (eq (char-before) ?
}))
9345 (< (point) placeholder
)))
9348 ((eq (point) placeholder
) 'statement
) ; unrecognised construct
9349 ;; A preceding comma at the top level means that a
9350 ;; new variable declaration starts here. Use
9351 ;; topmost-intro-cont for it, for consistency with
9352 ;; the first variable declaration. C.f. case 5N.
9353 ((eq char-before-ip ?
,) 'topmost-intro-cont
)
9354 (t 'statement-cont
))
9355 nil nil containing-sexp paren-state
))
9358 ;; CASE 5F: Close of a non-class declaration level block.
9359 ((and (eq char-after-ip ?
})
9360 (c-keyword-member containing-decl-kwd
9361 'c-other-block-decl-kwds
))
9362 ;; This is inconsistent: Should use `containing-decl-open'
9363 ;; here if it's at boi, like in case 5J.
9364 (goto-char containing-decl-start
)
9366 (if (string-equal (symbol-name containing-decl-kwd
) "extern")
9367 ;; Special case for compatibility with the
9368 ;; extern-lang syntactic symbols.
9370 (intern (concat (symbol-name containing-decl-kwd
)
9373 (c-most-enclosing-brace paren-state
(point))
9376 ;; CASE 5G: we are looking at the brace which closes the
9377 ;; enclosing nested class decl
9378 ((and containing-sexp
9379 (eq char-after-ip ?
})
9380 (eq containing-decl-open containing-sexp
))
9381 (c-add-class-syntax 'class-close
9382 containing-decl-open
9383 containing-decl-start
9387 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9388 ((and c-recognize-knr-p
9389 (not containing-sexp
) ; can't be knr inside braces.
9390 (not (eq char-before-ip ?
}))
9392 (setq placeholder
(cdr (c-beginning-of-decl-1 lim
)))
9394 ;; Do an extra check to avoid tripping up on
9395 ;; statements that occur in invalid contexts
9396 ;; (e.g. in macro bodies where we don't really
9397 ;; know the context of what we're looking at).
9398 (not (and c-opt-block-stmt-key
9399 (looking-at c-opt-block-stmt-key
)))))
9400 (< placeholder indent-point
))
9401 (goto-char placeholder
)
9402 (c-add-syntax 'knr-argdecl
(point)))
9404 ;; CASE 5I: ObjC method definition.
9405 ((and c-opt-method-key
9406 (looking-at c-opt-method-key
))
9407 (c-beginning-of-statement-1 nil t
)
9408 (if (= (point) indent-point
)
9409 ;; Handle the case when it's the first (non-comment)
9410 ;; thing in the buffer. Can't look for a 'same return
9411 ;; value from cbos1 since ObjC directives currently
9412 ;; aren't recognized fully, so that we get 'same
9413 ;; instead of 'previous if it moved over a preceding
9415 (goto-char (point-min)))
9416 (c-add-syntax 'objc-method-intro
(c-point 'boi
)))
9418 ;; CASE 5P: AWK pattern or function or continuation
9420 ((c-major-mode-is 'awk-mode
)
9421 (setq placeholder
(point))
9423 (if (and (eq (c-beginning-of-statement-1) 'same
)
9424 (/= (point) placeholder
))
9428 containing-sexp paren-state
))
9430 ;; CASE 5N: At a variable declaration that follows a class
9431 ;; definition or some other block declaration that doesn't
9432 ;; end at the closing '}'. C.f. case 5D.5.
9434 (c-backward-syntactic-ws lim
)
9435 (and (eq (char-before) ?
})
9437 (let ((start (point)))
9438 (if (and c-state-cache
9439 (consp (car c-state-cache
))
9440 (eq (cdar c-state-cache
) (point)))
9441 ;; Speed up the backward search a bit.
9442 (goto-char (caar c-state-cache
)))
9443 (c-beginning-of-decl-1 containing-sexp
)
9444 (setq placeholder
(point))
9445 (if (= start
(point))
9446 ;; The '}' is unbalanced.
9449 (>= (point) indent-point
))))))
9450 (goto-char placeholder
)
9451 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9452 containing-sexp paren-state
))
9454 ;; NOTE: The point is at the end of the previous token here.
9456 ;; CASE 5J: we are at the topmost level, make
9457 ;; sure we skip back past any access specifiers
9459 ;; A macro continuation line is never at top level.
9460 (not (and macro-start
9461 (> indent-point macro-start
)))
9463 (setq placeholder
(point))
9464 (or (memq char-before-ip
'(?\
; ?{ ?} nil))
9465 (c-at-vsemi-p before-ws-ip
)
9466 (when (and (eq char-before-ip ?
:)
9467 (eq (c-beginning-of-statement-1 lim
)
9469 (c-backward-syntactic-ws lim
)
9470 (setq placeholder
(point)))
9471 (and (c-major-mode-is 'objc-mode
)
9472 (catch 'not-in-directive
9473 (c-beginning-of-statement-1 lim
)
9474 (setq placeholder
(point))
9475 (while (and (c-forward-objc-directive)
9476 (< (point) indent-point
))
9477 (c-forward-syntactic-ws)
9478 (if (>= (point) indent-point
)
9479 (throw 'not-in-directive t
))
9480 (setq placeholder
(point)))
9482 ;; For historic reasons we anchor at bol of the last
9483 ;; line of the previous declaration. That's clearly
9484 ;; highly bogus and useless, and it makes our lives hard
9485 ;; to remain compatible. :P
9486 (goto-char placeholder
)
9487 (c-add-syntax 'topmost-intro
(c-point 'bol
))
9488 (if containing-decl-open
9489 (if (c-keyword-member containing-decl-kwd
9490 'c-other-block-decl-kwds
)
9492 (goto-char (c-brace-anchor-point containing-decl-open
))
9494 (if (string-equal (symbol-name containing-decl-kwd
)
9496 ;; Special case for compatibility with the
9497 ;; extern-lang syntactic symbols.
9499 (intern (concat "in"
9500 (symbol-name containing-decl-kwd
))))
9502 (c-most-enclosing-brace paren-state
(point))
9504 (c-add-class-syntax 'inclass
9505 containing-decl-open
9506 containing-decl-start
9509 (when (and c-syntactic-indentation-in-macros
9511 (/= macro-start
(c-point 'boi indent-point
)))
9512 (c-add-syntax 'cpp-define-intro
)
9513 (setq macro-start nil
)))
9515 ;; CASE 5K: we are at an ObjC method definition
9516 ;; continuation line.
9517 ((and c-opt-method-key
9519 (c-beginning-of-statement-1 lim
)
9521 (when (looking-at c-opt-method-key
)
9522 (setq placeholder
(point)))))
9523 (c-add-syntax 'objc-method-args-cont placeholder
))
9525 ;; CASE 5L: we are at the first argument of a template
9526 ;; arglist that begins on the previous line.
9527 ((and c-recognize-
<>-arglists
9528 (eq (char-before) ?
<)
9529 (not (and c-overloadable-operators-regexp
9530 (c-after-special-operator-id lim
))))
9531 (c-beginning-of-statement-1 (c-safe-position (point) paren-state
))
9532 (c-add-syntax 'template-args-cont
(c-point 'boi
)))
9534 ;; CASE 5Q: we are at a statement within a macro.
9536 (c-beginning-of-statement-1 containing-sexp
)
9537 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state
))
9539 ;;CASE 5N: We are at a tompmost continuation line and the only
9540 ;;preceding items are annotations.
9541 ((and (c-major-mode-is 'java-mode
)
9542 (setq placeholder
(point))
9543 (c-beginning-of-statement-1)
9545 (while (and (c-forward-annotation))
9546 (c-forward-syntactic-ws))
9549 (>= (point) placeholder
)
9550 (goto-char placeholder
)))
9551 (c-add-syntax 'annotation-top-cont
(c-point 'boi
)))
9553 ;; CASE 5M: we are at a topmost continuation line
9555 (c-beginning-of-statement-1 (c-safe-position (point) paren-state
))
9556 (when (c-major-mode-is 'objc-mode
)
9557 (setq placeholder
(point))
9558 (while (and (c-forward-objc-directive)
9559 (< (point) indent-point
))
9560 (c-forward-syntactic-ws)
9561 (setq placeholder
(point)))
9562 (goto-char placeholder
))
9563 (c-add-syntax 'topmost-intro-cont
(c-point 'boi
)))
9567 ;; (CASE 6 has been removed.)
9569 ;; CASE 7: line is an expression, not a statement. Most
9570 ;; likely we are either in a function prototype or a function
9571 ;; call argument list
9572 ((not (or (and c-special-brace-lists
9574 (goto-char containing-sexp
)
9575 (c-looking-at-special-brace-list)))
9576 (eq (char-after containing-sexp
) ?
{)))
9579 ;; CASE 7A: we are looking at the arglist closing paren.
9581 ((memq char-after-ip
'(?\
) ?\
]))
9582 (goto-char containing-sexp
)
9583 (setq placeholder
(c-point 'boi
))
9584 (if (and (c-safe (backward-up-list 1) t
)
9585 (>= (point) placeholder
))
9588 (skip-chars-forward " \t"))
9589 (goto-char placeholder
))
9590 (c-add-stmt-syntax 'arglist-close
(list containing-sexp
) t
9591 (c-most-enclosing-brace paren-state
(point))
9594 ;; CASE 19: line is an expression, not a statement, and is directly
9595 ;; contained by a template delimiter. Most likely, we are in a
9596 ;; template arglist within a statement. This case is based on CASE
9597 ;; 7. At some point in the future, we may wish to create more
9598 ;; syntactic symbols such as `template-intro',
9599 ;; `template-cont-nonempty', etc., and distinguish between them as we
9600 ;; do for `arglist-intro' etc. (2009-12-07).
9601 ((and c-recognize-
<>-arglists
9602 (setq containing-
< (c-up-list-backward indent-point containing-sexp
))
9603 (eq (char-after containing-
<) ?\
<))
9604 (setq placeholder
(c-point 'boi containing-
<))
9605 (goto-char containing-sexp
) ; Most nested Lbrace/Lparen (but not
9606 ; '<') before indent-point.
9607 (if (>= (point) placeholder
)
9610 (skip-chars-forward " \t"))
9611 (goto-char placeholder
))
9612 (c-add-stmt-syntax 'template-args-cont
(list containing-
<) t
9613 (c-most-enclosing-brace c-state-cache
(point))
9616 ;; CASE 7B: Looking at the opening brace of an
9617 ;; in-expression block or brace list. C.f. cases 4, 16A
9619 ((and (eq char-after-ip ?
{)
9621 (setq placeholder
(c-inside-bracelist-p (point)
9624 (setq tmpsymbol
'(brace-list-open . inexpr-class
))
9625 (setq tmpsymbol
'(block-open . inexpr-statement
)
9627 (cdr-safe (c-looking-at-inexpr-block
9628 (c-safe-position containing-sexp
9631 ;; placeholder is nil if it's a block directly in
9632 ;; a function arglist. That makes us skip out of
9635 (goto-char placeholder
)
9636 (back-to-indentation)
9637 (c-add-stmt-syntax (car tmpsymbol
) nil t
9638 (c-most-enclosing-brace paren-state
(point))
9640 (if (/= (point) placeholder
)
9641 (c-add-syntax (cdr tmpsymbol
))))
9643 ;; CASE 7C: we are looking at the first argument in an empty
9644 ;; argument list. Use arglist-close if we're actually
9645 ;; looking at a close paren or bracket.
9646 ((memq char-before-ip
'(?\
( ?\
[))
9647 (goto-char containing-sexp
)
9648 (setq placeholder
(c-point 'boi
))
9649 (if (and (c-safe (backward-up-list 1) t
)
9650 (>= (point) placeholder
))
9653 (skip-chars-forward " \t"))
9654 (goto-char placeholder
))
9655 (c-add-stmt-syntax 'arglist-intro
(list containing-sexp
) t
9656 (c-most-enclosing-brace paren-state
(point))
9659 ;; CASE 7D: we are inside a conditional test clause. treat
9660 ;; these things as statements
9662 (goto-char containing-sexp
)
9663 (and (c-safe (c-forward-sexp -
1) t
)
9664 (looking-at "\\<for\\>[^_]")))
9665 (goto-char (1+ containing-sexp
))
9666 (c-forward-syntactic-ws indent-point
)
9667 (if (eq char-before-ip ?\
;)
9668 (c-add-syntax 'statement
(point))
9669 (c-add-syntax 'statement-cont
(point))
9672 ;; CASE 7E: maybe a continued ObjC method call. This is the
9673 ;; case when we are inside a [] bracketed exp, and what
9674 ;; precede the opening bracket is not an identifier.
9675 ((and c-opt-method-key
9676 (eq (char-after containing-sexp
) ?\
[)
9678 (goto-char (1- containing-sexp
))
9679 (c-backward-syntactic-ws (c-point 'bod
))
9680 (if (not (looking-at c-symbol-key
))
9681 (c-add-syntax 'objc-method-call-cont containing-sexp
))
9684 ;; CASE 7F: we are looking at an arglist continuation line,
9685 ;; but the preceding argument is on the same line as the
9686 ;; opening paren. This case includes multi-line
9687 ;; mathematical paren groupings, but we could be on a
9688 ;; for-list continuation line. C.f. case 7A.
9690 (goto-char (1+ containing-sexp
))
9692 (c-forward-syntactic-ws)
9695 (goto-char containing-sexp
) ; paren opening the arglist
9696 (setq placeholder
(c-point 'boi
))
9697 (if (and (c-safe (backward-up-list 1) t
)
9698 (>= (point) placeholder
))
9701 (skip-chars-forward " \t"))
9702 (goto-char placeholder
))
9703 (c-add-stmt-syntax 'arglist-cont-nonempty
(list containing-sexp
) t
9704 (c-most-enclosing-brace c-state-cache
(point))
9707 ;; CASE 7G: we are looking at just a normal arglist
9708 ;; continuation line
9709 (t (c-forward-syntactic-ws indent-point
)
9710 (c-add-syntax 'arglist-cont
(c-point 'boi
)))
9713 ;; CASE 8: func-local multi-inheritance line
9714 ((and (c-major-mode-is 'c
++-mode
)
9716 (goto-char indent-point
)
9717 (skip-chars-forward " \t")
9718 (looking-at c-opt-postfix-decl-spec-key
)))
9719 (goto-char indent-point
)
9720 (skip-chars-forward " \t")
9723 ;; CASE 8A: non-hanging colon on an inher intro
9724 ((eq char-after-ip ?
:)
9725 (c-backward-syntactic-ws lim
)
9726 (c-add-syntax 'inher-intro
(c-point 'boi
)))
9728 ;; CASE 8B: hanging colon on an inher intro
9729 ((eq char-before-ip ?
:)
9730 (c-add-syntax 'inher-intro
(c-point 'boi
)))
9732 ;; CASE 8C: a continued inheritance line
9734 (c-beginning-of-inheritance-list lim
)
9735 (c-add-syntax 'inher-cont
(point))
9738 ;; CASE 9: we are inside a brace-list
9739 ((and (not (c-major-mode-is 'awk-mode
)) ; Maybe this isn't needed (ACM, 2002/3/29)
9740 (setq special-brace-list
9741 (or (and c-special-brace-lists
;;;; ALWAYS NIL FOR AWK!!
9743 (goto-char containing-sexp
)
9744 (c-looking-at-special-brace-list)))
9745 (c-inside-bracelist-p containing-sexp paren-state
))))
9748 ;; CASE 9A: In the middle of a special brace list opener.
9749 ((and (consp special-brace-list
)
9751 (goto-char containing-sexp
)
9752 (eq (char-after) ?\
())
9753 (eq char-after-ip
(car (cdr special-brace-list
))))
9754 (goto-char (car (car special-brace-list
)))
9755 (skip-chars-backward " \t")
9757 (assoc 'statement-cont
9758 (setq placeholder
(c-guess-basic-syntax))))
9759 (setq c-syntactic-context placeholder
)
9760 (c-beginning-of-statement-1
9761 (c-safe-position (1- containing-sexp
) paren-state
))
9762 (c-forward-token-2 0)
9763 (while (looking-at c-specifier-key
)
9764 (goto-char (match-end 1))
9765 (c-forward-syntactic-ws))
9766 (c-add-syntax 'brace-list-open
(c-point 'boi
))))
9768 ;; CASE 9B: brace-list-close brace
9769 ((if (consp special-brace-list
)
9770 ;; Check special brace list closer.
9772 (goto-char (car (car special-brace-list
)))
9774 (goto-char indent-point
)
9775 (back-to-indentation)
9777 ;; We were between the special close char and the `)'.
9778 (and (eq (char-after) ?\
))
9779 (eq (1+ (point)) (cdr (car special-brace-list
))))
9780 ;; We were before the special close char.
9781 (and (eq (char-after) (cdr (cdr special-brace-list
)))
9782 (zerop (c-forward-token-2))
9783 (eq (1+ (point)) (cdr (car special-brace-list
)))))))
9784 ;; Normal brace list check.
9785 (and (eq char-after-ip ?
})
9786 (c-safe (goto-char (c-up-list-backward (point))) t
)
9787 (= (point) containing-sexp
)))
9788 (if (eq (point) (c-point 'boi
))
9789 (c-add-syntax 'brace-list-close
(point))
9790 (setq lim
(c-most-enclosing-brace c-state-cache
(point)))
9791 (c-beginning-of-statement-1 lim
)
9792 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state
)))
9795 ;; Prepare for the rest of the cases below by going to the
9796 ;; token following the opening brace
9797 (if (consp special-brace-list
)
9799 (goto-char (car (car special-brace-list
)))
9800 (c-forward-token-2 1 nil indent-point
))
9801 (goto-char containing-sexp
))
9803 (let ((start (point)))
9804 (c-forward-syntactic-ws indent-point
)
9805 (goto-char (max start
(c-point 'bol
))))
9806 (c-skip-ws-forward indent-point
)
9809 ;; CASE 9C: we're looking at the first line in a brace-list
9810 ((= (point) indent-point
)
9811 (if (consp special-brace-list
)
9812 (goto-char (car (car special-brace-list
)))
9813 (goto-char containing-sexp
))
9814 (if (eq (point) (c-point 'boi
))
9815 (c-add-syntax 'brace-list-intro
(point))
9816 (setq lim
(c-most-enclosing-brace c-state-cache
(point)))
9817 (c-beginning-of-statement-1 lim
)
9818 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state
)))
9820 ;; CASE 9D: this is just a later brace-list-entry or
9822 (t (if (or (eq char-after-ip ?
{)
9823 (and c-special-brace-lists
9825 (goto-char indent-point
)
9826 (c-forward-syntactic-ws (c-point 'eol
))
9827 (c-looking-at-special-brace-list (point)))))
9828 (c-add-syntax 'brace-entry-open
(point))
9829 (c-add-syntax 'brace-list-entry
(point))
9833 ;; CASE 10: A continued statement or top level construct.
9834 ((and (not (memq char-before-ip
'(?\
; ?:)))
9835 (not (c-at-vsemi-p before-ws-ip
))
9836 (or (not (eq char-before-ip ?
}))
9837 (c-looking-at-inexpr-block-backward c-state-cache
))
9840 (c-beginning-of-statement-1 containing-sexp
)
9841 (setq placeholder
(point))))
9842 (/= placeholder containing-sexp
))
9843 ;; This is shared with case 18.
9844 (c-guess-continued-construct indent-point
9850 ;; CASE 16: block close brace, possibly closing the defun or
9852 ((eq char-after-ip ?
})
9853 ;; From here on we have the next containing sexp in lim.
9854 (setq lim
(c-most-enclosing-brace paren-state
))
9855 (goto-char containing-sexp
)
9858 ;; CASE 16E: Closing a statement block? This catches
9859 ;; cases where it's preceded by a statement keyword,
9860 ;; which works even when used in an "invalid" context,
9861 ;; e.g. a macro argument.
9862 ((c-after-conditional)
9863 (c-backward-to-block-anchor lim
)
9864 (c-add-stmt-syntax 'block-close nil t lim paren-state
))
9866 ;; CASE 16A: closing a lambda defun or an in-expression
9867 ;; block? C.f. cases 4, 7B and 17E.
9868 ((setq placeholder
(c-looking-at-inexpr-block
9869 (c-safe-position containing-sexp paren-state
)
9871 (setq tmpsymbol
(if (eq (car placeholder
) 'inlambda
)
9874 (goto-char containing-sexp
)
9875 (back-to-indentation)
9876 (if (= containing-sexp
(point))
9877 (c-add-syntax tmpsymbol
(point))
9878 (goto-char (cdr placeholder
))
9879 (back-to-indentation)
9880 (c-add-stmt-syntax tmpsymbol nil t
9881 (c-most-enclosing-brace paren-state
(point))
9883 (if (/= (point) (cdr placeholder
))
9884 (c-add-syntax (car placeholder
)))))
9886 ;; CASE 16B: does this close an inline or a function in
9887 ;; a non-class declaration level block?
9892 (c-looking-at-decl-block
9893 (c-most-enclosing-brace paren-state lim
)
9895 (setq placeholder
(point))))
9896 (c-backward-to-decl-anchor lim
)
9897 (back-to-indentation)
9899 (goto-char placeholder
)
9900 (looking-at c-other-decl-block-key
))
9901 (c-add-syntax 'defun-close
(point))
9902 (c-add-syntax 'inline-close
(point))))
9904 ;; CASE 16F: Can be a defun-close of a function declared
9905 ;; in a statement block, e.g. in Pike or when using gcc
9906 ;; extensions, but watch out for macros followed by
9907 ;; blocks. Let it through to be handled below.
9908 ;; C.f. cases B.3 and 17G.
9910 (and (not (c-at-statement-start-p))
9911 (eq (c-beginning-of-statement-1 lim nil nil t
) 'same
)
9912 (setq placeholder
(point))
9913 (let ((c-recognize-typeless-decls nil
))
9914 ;; Turn off recognition of constructs that
9915 ;; lacks a type in this case, since that's more
9916 ;; likely to be a macro followed by a block.
9917 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
9918 (back-to-indentation)
9919 (if (/= (point) containing-sexp
)
9920 (goto-char placeholder
))
9921 (c-add-stmt-syntax 'defun-close nil t lim paren-state
))
9923 ;; CASE 16C: If there is an enclosing brace then this is
9924 ;; a block close since defun closes inside declaration
9925 ;; level blocks have been handled above.
9927 ;; If the block is preceded by a case/switch label on
9928 ;; the same line, we anchor at the first preceding label
9929 ;; at boi. The default handling in c-add-stmt-syntax
9930 ;; really fixes it better, but we do like this to keep
9931 ;; the indentation compatible with version 5.28 and
9932 ;; earlier. C.f. case 17H.
9933 (while (and (/= (setq placeholder
(point)) (c-point 'boi
))
9934 (eq (c-beginning-of-statement-1 lim
) 'label
)))
9935 (goto-char placeholder
)
9936 (if (looking-at c-label-kwds-regexp
)
9937 (c-add-syntax 'block-close
(point))
9938 (goto-char containing-sexp
)
9939 ;; c-backward-to-block-anchor not necessary here; those
9940 ;; situations are handled in case 16E above.
9941 (c-add-stmt-syntax 'block-close nil t lim paren-state
)))
9943 ;; CASE 16D: Only top level defun close left.
9945 (goto-char containing-sexp
)
9946 (c-backward-to-decl-anchor lim
)
9947 (c-add-stmt-syntax 'defun-close nil nil
9948 (c-most-enclosing-brace paren-state
)
9952 ;; CASE 17: Statement or defun catchall.
9954 (goto-char indent-point
)
9955 ;; Back up statements until we find one that starts at boi.
9956 (while (let* ((prev-point (point))
9957 (last-step-type (c-beginning-of-statement-1
9959 (if (= (point) prev-point
)
9961 (setq step-type
(or step-type last-step-type
))
9963 (setq step-type last-step-type
)
9964 (/= (point) (c-point 'boi
)))))
9967 ;; CASE 17B: continued statement
9968 ((and (eq step-type
'same
)
9969 (/= (point) indent-point
))
9970 (c-add-stmt-syntax 'statement-cont nil nil
9971 containing-sexp paren-state
))
9973 ;; CASE 17A: After a case/default label?
9975 (while (and (eq step-type
'label
)
9976 (not (looking-at c-label-kwds-regexp
)))
9978 (c-beginning-of-statement-1 containing-sexp
)))
9979 (eq step-type
'label
))
9980 (c-add-stmt-syntax (if (eq char-after-ip ?
{)
9981 'statement-case-open
9982 'statement-case-intro
)
9983 nil t containing-sexp paren-state
))
9985 ;; CASE 17D: any old statement
9987 (while (eq step-type
'label
)
9989 (c-beginning-of-statement-1 containing-sexp
)))
9990 (eq step-type
'previous
))
9991 (c-add-stmt-syntax 'statement nil t
9992 containing-sexp paren-state
)
9993 (if (eq char-after-ip ?
{)
9994 (c-add-syntax 'block-open
)))
9996 ;; CASE 17I: Inside a substatement block.
9998 ;; The following tests are all based on containing-sexp.
9999 (goto-char containing-sexp
)
10000 ;; From here on we have the next containing sexp in lim.
10001 (setq lim
(c-most-enclosing-brace paren-state containing-sexp
))
10002 (c-after-conditional))
10003 (c-backward-to-block-anchor lim
)
10004 (c-add-stmt-syntax 'statement-block-intro nil t
10006 (if (eq char-after-ip ?
{)
10007 (c-add-syntax 'block-open
)))
10009 ;; CASE 17E: first statement in an in-expression block.
10010 ;; C.f. cases 4, 7B and 16A.
10011 ((setq placeholder
(c-looking-at-inexpr-block
10012 (c-safe-position containing-sexp paren-state
)
10014 (setq tmpsymbol
(if (eq (car placeholder
) 'inlambda
)
10016 'statement-block-intro
))
10017 (back-to-indentation)
10018 (if (= containing-sexp
(point))
10019 (c-add-syntax tmpsymbol
(point))
10020 (goto-char (cdr placeholder
))
10021 (back-to-indentation)
10022 (c-add-stmt-syntax tmpsymbol nil t
10023 (c-most-enclosing-brace c-state-cache
(point))
10025 (if (/= (point) (cdr placeholder
))
10026 (c-add-syntax (car placeholder
))))
10027 (if (eq char-after-ip ?
{)
10028 (c-add-syntax 'block-open
)))
10030 ;; CASE 17F: first statement in an inline, or first
10031 ;; statement in a top-level defun. we can tell this is it
10032 ;; if there are no enclosing braces that haven't been
10033 ;; narrowed out by a class (i.e. don't use bod here).
10035 (or (not (setq placeholder
(c-most-enclosing-brace
10038 (goto-char placeholder
)
10039 (eq (char-after) ?
{))
10040 (c-looking-at-decl-block (c-most-enclosing-brace
10041 paren-state
(point))
10043 (c-backward-to-decl-anchor lim
)
10044 (back-to-indentation)
10045 (c-add-syntax 'defun-block-intro
(point)))
10047 ;; CASE 17G: First statement in a function declared inside
10048 ;; a normal block. This can occur in Pike and with
10049 ;; e.g. the gcc extensions, but watch out for macros
10050 ;; followed by blocks. C.f. cases B.3 and 16F.
10052 (and (not (c-at-statement-start-p))
10053 (eq (c-beginning-of-statement-1 lim nil nil t
) 'same
)
10054 (setq placeholder
(point))
10055 (let ((c-recognize-typeless-decls nil
))
10056 ;; Turn off recognition of constructs that lacks
10057 ;; a type in this case, since that's more likely
10058 ;; to be a macro followed by a block.
10059 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
10060 (back-to-indentation)
10061 (if (/= (point) containing-sexp
)
10062 (goto-char placeholder
))
10063 (c-add-stmt-syntax 'defun-block-intro nil t
10066 ;; CASE 17H: First statement in a block.
10068 ;; If the block is preceded by a case/switch label on the
10069 ;; same line, we anchor at the first preceding label at
10070 ;; boi. The default handling in c-add-stmt-syntax is
10071 ;; really fixes it better, but we do like this to keep the
10072 ;; indentation compatible with version 5.28 and earlier.
10074 (while (and (/= (setq placeholder
(point)) (c-point 'boi
))
10075 (eq (c-beginning-of-statement-1 lim
) 'label
)))
10076 (goto-char placeholder
)
10077 (if (looking-at c-label-kwds-regexp
)
10078 (c-add-syntax 'statement-block-intro
(point))
10079 (goto-char containing-sexp
)
10080 ;; c-backward-to-block-anchor not necessary here; those
10081 ;; situations are handled in case 17I above.
10082 (c-add-stmt-syntax 'statement-block-intro nil t
10084 (if (eq char-after-ip ?
{)
10085 (c-add-syntax 'block-open
)))
10089 ;; now we need to look at any modifiers
10090 (goto-char indent-point
)
10091 (skip-chars-forward " \t")
10093 ;; are we looking at a comment only line?
10094 (when (and (looking-at c-comment-start-regexp
)
10095 (/= (c-forward-token-2 0 nil
(c-point 'eol
)) 0))
10096 (c-append-syntax 'comment-intro
))
10098 ;; we might want to give additional offset to friends (in C++).
10099 (when (and c-opt-friend-key
10100 (looking-at c-opt-friend-key
))
10101 (c-append-syntax 'friend
))
10103 ;; Set syntactic-relpos.
10104 (let ((p c-syntactic-context
))
10106 (if (integerp (c-langelem-pos (car p
)))
10108 (setq syntactic-relpos
(c-langelem-pos (car p
)))
10113 ;; Start of or a continuation of a preprocessor directive?
10114 (if (and macro-start
10115 (eq macro-start
(c-point 'boi
))
10116 (not (and (c-major-mode-is 'pike-mode
)
10117 (eq (char-after (1+ macro-start
)) ?
\"))))
10118 (c-append-syntax 'cpp-macro
)
10119 (when (and c-syntactic-indentation-in-macros macro-start
)
10122 (< syntactic-relpos macro-start
)
10124 (assq 'arglist-intro c-syntactic-context
)
10125 (assq 'arglist-cont c-syntactic-context
)
10126 (assq 'arglist-cont-nonempty c-syntactic-context
)
10127 (assq 'arglist-close c-syntactic-context
))))
10128 ;; If inside a cpp expression, i.e. anywhere in a
10129 ;; cpp directive except a #define body, we only let
10130 ;; through the syntactic analysis that is internal
10131 ;; in the expression. That means the arglist
10132 ;; elements, if they are anchored inside the cpp
10134 (setq c-syntactic-context nil
)
10135 (c-add-syntax 'cpp-macro-cont macro-start
))
10136 (when (and (eq macro-start syntactic-relpos
)
10137 (not (assq 'cpp-define-intro c-syntactic-context
))
10139 (goto-char macro-start
)
10140 (or (not (c-forward-to-cpp-define-body))
10141 (<= (point) (c-point 'boi indent-point
)))))
10142 ;; Inside a #define body and the syntactic analysis is
10143 ;; anchored on the start of the #define. In this case
10144 ;; we add cpp-define-intro to get the extra
10145 ;; indentation of the #define body.
10146 (c-add-syntax 'cpp-define-intro
)))))
10148 ;; return the syntax
10149 c-syntactic-context
)))
10152 ;; Indentation calculation.
10154 (defun c-evaluate-offset (offset langelem symbol
)
10155 ;; offset can be a number, a function, a variable, a list, or one of
10156 ;; the symbols + or -
10158 ;; This function might do hidden buffer changes.
10161 ((numberp offset
) offset
)
10162 ((vectorp offset
) offset
)
10163 ((null offset
) nil
)
10165 ((eq offset
'+) c-basic-offset
)
10166 ((eq offset
'-
) (- c-basic-offset
))
10167 ((eq offset
'++) (* 2 c-basic-offset
))
10168 ((eq offset
'--
) (* 2 (- c-basic-offset
)))
10169 ((eq offset
'*) (/ c-basic-offset
2))
10170 ((eq offset
'/) (/ (- c-basic-offset
) 2))
10172 ((functionp offset
)
10175 (cons (c-langelem-sym langelem
)
10176 (c-langelem-pos langelem
)))
10181 ((eq (car offset
) 'quote
)
10182 (c-benign-error "The offset %S for %s was mistakenly quoted"
10186 ((memq (car offset
) '(min max
))
10187 (let (res val
(method (car offset
)))
10188 (setq offset
(cdr offset
))
10190 (setq val
(c-evaluate-offset (car offset
) langelem symbol
))
10198 Error evaluating offset %S for %s: \
10199 Cannot combine absolute offset %S with relative %S in `%s' method"
10200 (car offset
) symbol res val method
)
10201 (setq res
(funcall method res val
))))
10205 Error evaluating offset %S for %s: \
10206 Cannot combine relative offset %S with absolute %S in `%s' method"
10207 (car offset
) symbol res val method
)
10208 (setq res
(vector (funcall method
(aref res
0)
10210 (setq offset
(cdr offset
)))
10213 ((eq (car offset
) 'add
)
10215 (setq offset
(cdr offset
))
10217 (setq val
(c-evaluate-offset (car offset
) langelem symbol
))
10224 (setq res
(vector (+ (aref res
0) val
)))
10225 (setq res
(+ res val
))))
10229 Error evaluating offset %S for %s: \
10230 Cannot combine absolute offsets %S and %S in `add' method"
10231 (car offset
) symbol res val
)
10232 (setq res val
)))) ; Override.
10233 (setq offset
(cdr offset
)))
10238 (when (eq (car offset
) 'first
)
10239 (setq offset
(cdr offset
)))
10240 (while (and (not res
) offset
)
10241 (setq res
(c-evaluate-offset (car offset
) langelem symbol
)
10242 offset
(cdr offset
)))
10245 ((and (symbolp offset
) (boundp offset
))
10246 (symbol-value offset
))
10249 (c-benign-error "Unknown offset format %S for %s" offset symbol
)
10252 (if (or (null res
) (integerp res
)
10253 (and (vectorp res
) (= (length res
) 1) (integerp (aref res
0))))
10255 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10259 (defun c-calc-offset (langelem)
10260 ;; Get offset from LANGELEM which is a list beginning with the
10261 ;; syntactic symbol and followed by any analysis data it provides.
10262 ;; That data may be zero or more elements, but if at least one is
10263 ;; given then the first is the anchor position (or nil). The symbol
10264 ;; is matched against `c-offsets-alist' and the offset calculated
10265 ;; from that is returned.
10267 ;; This function might do hidden buffer changes.
10268 (let* ((symbol (c-langelem-sym langelem
))
10269 (match (assq symbol c-offsets-alist
))
10270 (offset (cdr-safe match
)))
10272 (setq offset
(c-evaluate-offset offset langelem symbol
))
10273 (if c-strict-syntax-p
10274 (c-benign-error "No offset found for syntactic symbol %s" symbol
))
10276 (if (vectorp offset
)
10278 (or (and (numberp offset
) offset
)
10279 (and (symbolp offset
) (symbol-value offset
))
10283 (defun c-get-offset (langelem)
10284 ;; This is a compatibility wrapper for `c-calc-offset' in case
10285 ;; someone is calling it directly. It takes an old style syntactic
10286 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10289 ;; This function might do hidden buffer changes.
10290 (if (c-langelem-pos langelem
)
10291 (c-calc-offset (list (c-langelem-sym langelem
)
10292 (c-langelem-pos langelem
)))
10293 (c-calc-offset langelem
)))
10295 (defun c-get-syntactic-indentation (langelems)
10296 ;; Calculate the syntactic indentation from a syntactic description
10297 ;; as returned by `c-guess-syntax'.
10299 ;; Note that topmost-intro always has an anchor position at bol, for
10300 ;; historical reasons. It's often used together with other symbols
10301 ;; that has more sane positions. Since we always use the first
10302 ;; found anchor position, we rely on that these other symbols always
10303 ;; precede topmost-intro in the LANGELEMS list.
10305 ;; This function might do hidden buffer changes.
10306 (let ((indent 0) anchor
)
10309 (let* ((c-syntactic-element (car langelems
))
10310 (res (c-calc-offset c-syntactic-element
)))
10313 ;; Got an absolute column that overrides any indentation
10314 ;; we've collected so far, but not the relative
10315 ;; indentation we might get for the nested structures
10316 ;; further down the langelems list.
10317 (setq indent
(elt res
0)
10318 anchor
(point-min)) ; A position at column 0.
10320 ;; Got a relative change of the current calculated
10322 (setq indent
(+ indent res
))
10324 ;; Use the anchor position from the first syntactic
10325 ;; element with one.
10327 (setq anchor
(c-langelem-pos (car langelems
)))))
10329 (setq langelems
(cdr langelems
))))
10332 (+ indent
(save-excursion
10338 (cc-provide 'cc-engine
)
10340 ;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
10341 ;;; cc-engine.el ends here