1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985, 1987, 1992-2011 Free Software Foundation, Inc.
5 ;; Authors: 2001- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
33 ;; The functions which have docstring documentation can be considered
34 ;; part of an API which other packages can use in CC Mode buffers.
35 ;; Otoh, undocumented functions and functions with the documentation
36 ;; in comments are considered purely internal and can change semantics
37 ;; or even disappear in the future.
39 ;; (This policy applies to CC Mode as a whole, not just this file. It
40 ;; probably also applies to many other Emacs packages, but here it's
41 ;; clearly spelled out.)
43 ;; Hidden buffer changes
45 ;; Various functions in CC Mode use text properties for caching and
46 ;; syntactic markup purposes, and those of them that might modify such
47 ;; properties but still don't modify the buffer in a visible way are
48 ;; said to do "hidden buffer changes". They should be used within
49 ;; `c-save-buffer-state' or a similar function that saves and restores
50 ;; buffer modifiedness, disables buffer change hooks, etc.
52 ;; Interactive functions are assumed to not do hidden buffer changes,
53 ;; except in the specific parts of them that do real changes.
55 ;; Lineup functions are assumed to do hidden buffer changes. They
56 ;; must not do real changes, though.
58 ;; All other functions that do hidden buffer changes have that noted
59 ;; in their doc string or comment.
61 ;; The intention with this system is to avoid wrapping every leaf
62 ;; function that do hidden buffer changes inside
63 ;; `c-save-buffer-state'. It should be used as near the top of the
64 ;; interactive functions as possible.
66 ;; Functions called during font locking are allowed to do hidden
67 ;; buffer changes since the font-lock package run them in a context
68 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
69 ;; inspired by `save-buffer-state' in the font-lock package).
71 ;; Use of text properties
73 ;; CC Mode uses several text properties internally to mark up various
74 ;; positions, e.g. to improve speed and to eliminate glitches in
75 ;; interactive refontification.
77 ;; Note: This doc is for internal use only. Other packages should not
78 ;; assume that these text properties are used as described here.
81 ;; Used for "indirection". With its help, some other property can
82 ;; be cheaply and easily switched on or off everywhere it occurs.
85 ;; Used to modify the syntax of some characters. It is used to
86 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
87 ;; to "hide" obtrusive characters in preprocessor lines.
89 ;; This property is used on single characters and is therefore
90 ;; always treated as front and rear nonsticky (or start and end open
91 ;; in XEmacs vocabulary). It's therefore installed on
92 ;; `text-property-default-nonsticky' if that variable exists (Emacs
95 ;; 'c-is-sws and 'c-in-sws
96 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
97 ;; speed them up. See the comment blurb before `c-put-is-sws'
98 ;; below for further details.
101 ;; This property is used on single characters to mark positions with
102 ;; special syntactic relevance of various sorts. Its primary use is
103 ;; to avoid glitches when multiline constructs are refontified
104 ;; interactively (on font lock decoration level 3). It's cleared in
105 ;; a region before it's fontified and is then put on relevant chars
106 ;; in that region as they are encountered during the fontification.
107 ;; The value specifies the kind of position:
110 ;; Put on the last char of the token preceding each declaration
111 ;; inside a declaration style arglist (typically in a function
115 ;; Put on the last char of the token preceding a declaration.
116 ;; This is used in cases where declaration boundaries can't be
117 ;; recognized simply by looking for a token like ";" or "}".
118 ;; `c-type-decl-end-used' must be set if this is used (see also
119 ;; `c-find-decl-spots').
122 ;; Put on the commas that separate arguments in angle bracket
123 ;; arglists like C++ template arglists.
125 ;; 'c-decl-id-start and 'c-decl-type-start
126 ;; Put on the last char of the token preceding each declarator
127 ;; in the declarator list of a declaration. They are also used
128 ;; between the identifiers cases like enum declarations.
129 ;; 'c-decl-type-start is used when the declarators are types,
130 ;; 'c-decl-id-start otherwise.
133 ;; Used in AWK mode to mark the various kinds of newlines. See
140 (if (and (boundp 'byte-compile-dest-file
)
141 (stringp byte-compile-dest-file
))
142 (cons (file-name-directory byte-compile-dest-file
) load-path
)
144 (load "cc-bytecomp" nil t
)))
146 (cc-require 'cc-defs
)
147 (cc-require-when-compile 'cc-langs
)
148 (cc-require 'cc-vars
)
150 ;; Silence the compiler.
151 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
154 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
156 (defmacro c-declare-lang-variables
()
159 (mapcar (lambda (init)
161 `(defvar ,(car init
) nil
,(elt init
2))
162 `(defvar ,(car init
) nil
))
163 (make-variable-buffer-local ',(car init
))))
164 (cdr c-lang-variable-inits
)))))
165 (c-declare-lang-variables)
168 ;;; Internal state variables.
170 ;; Internal state of hungry delete key feature
171 (defvar c-hungry-delete-key nil
)
172 (make-variable-buffer-local 'c-hungry-delete-key
)
174 ;; The electric flag (toggled by `c-toggle-electric-state').
175 ;; If t, electric actions (like automatic reindentation, and (if
176 ;; c-auto-newline is also set) auto newlining) will happen when an electric
177 ;; key like `{' is pressed (or an electric keyword like `else').
178 (defvar c-electric-flag t
)
179 (make-variable-buffer-local 'c-electric-flag
)
181 ;; Internal state of auto newline feature.
182 (defvar c-auto-newline nil
)
183 (make-variable-buffer-local 'c-auto-newline
)
185 ;; Included in the mode line to indicate the active submodes.
186 ;; (defvar c-submode-indicators nil)
187 ;; (make-variable-buffer-local 'c-submode-indicators)
189 (defun c-calculate-state (arg prevstate
)
190 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
191 ;; arg is nil or zero, toggle the state. If arg is negative, turn
192 ;; the state off, and if arg is positive, turn the state on
194 (zerop (setq arg
(prefix-numeric-value arg
))))
198 ;; Dynamically bound cache for `c-in-literal'.
199 (defvar c-in-literal-cache t
)
202 ;; Basic handling of preprocessor directives.
204 ;; This is a dynamically bound cache used together with
205 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
206 ;; works as long as point doesn't cross a macro boundary.
207 (defvar c-macro-start
'unknown
)
209 (defsubst c-query-and-set-macro-start
()
210 (if (symbolp c-macro-start
)
211 (setq c-macro-start
(save-excursion
212 (c-save-buffer-state ()
213 (and (c-beginning-of-macro)
217 (defsubst c-query-macro-start
()
218 (if (symbolp c-macro-start
)
220 (c-save-buffer-state ()
221 (and (c-beginning-of-macro)
225 (defun c-beginning-of-macro (&optional lim
)
226 "Go to the beginning of a preprocessor directive.
227 Leave point at the beginning of the directive and return t if in one,
228 otherwise return nil and leave point unchanged.
230 Note that this function might do hidden buffer changes. See the
231 comment at the start of cc-engine.el for more info."
232 (when c-opt-cpp-prefix
233 (let ((here (point)))
235 (if lim
(narrow-to-region lim
(point-max)))
237 (while (eq (char-before (1- (point))) ?
\\)
239 (back-to-indentation)
240 (if (and (<= (point) here
)
241 (looking-at c-opt-cpp-start
))
246 (defun c-end-of-macro ()
247 "Go to the end of a preprocessor directive.
248 More accurately, move the point to the end of the closest following
249 line that doesn't end with a line continuation backslash - no check is
250 done that the point is inside a cpp directive to begin with.
252 Note that this function might do hidden buffer changes. See the
253 comment at the start of cc-engine.el for more info."
256 (when (and (eq (char-before) ?
\\)
261 (defun c-syntactic-end-of-macro ()
262 ;; Go to the end of a CPP directive, or a "safe" pos just before.
264 ;; This is normally the end of the next non-escaped line. A "safe"
265 ;; position is one not within a string or comment. (The EOL on a line
266 ;; comment is NOT "safe").
268 ;; This function must only be called from the beginning of a CPP construct.
270 ;; Note that this function might do hidden buffer changes. See the comment
271 ;; at the start of cc-engine.el for more info.
272 (let* ((here (point))
273 (there (progn (c-end-of-macro) (point)))
274 (s (parse-partial-sexp here there
)))
275 (while (and (or (nth 3 s
) ; in a string
276 (nth 4 s
)) ; in a comment (maybe at end of line comment)
277 (> there here
)) ; No infinite loops, please.
278 (setq there
(1- (nth 8 s
)))
279 (setq s
(parse-partial-sexp here there
)))
282 (defun c-forward-over-cpp-define-id ()
283 ;; Assuming point is at the "#" that introduces a preprocessor
284 ;; directive, it's moved forward to the end of the identifier which is
285 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
286 ;; is returned in this case, in all other cases nil is returned and
287 ;; point isn't moved.
289 ;; This function might do hidden buffer changes.
290 (when (and c-opt-cpp-macro-define-id
291 (looking-at c-opt-cpp-macro-define-id
))
292 (goto-char (match-end 0))))
294 (defun c-forward-to-cpp-define-body ()
295 ;; Assuming point is at the "#" that introduces a preprocessor
296 ;; directive, it's moved forward to the start of the definition body
297 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
298 ;; specifies). Non-nil is returned in this case, in all other cases
299 ;; nil is returned and point isn't moved.
301 ;; This function might do hidden buffer changes.
302 (when (and c-opt-cpp-macro-define-start
303 (looking-at c-opt-cpp-macro-define-start
)
304 (not (= (match-end 0) (c-point 'eol
))))
305 (goto-char (match-end 0))))
308 ;;; Basic utility functions.
310 (defun c-syntactic-content (from to paren-level
)
311 ;; Return the given region as a string where all syntactic
312 ;; whitespace is removed or, where necessary, replaced with a single
313 ;; space. If PAREN-LEVEL is given then all parens in the region are
314 ;; collapsed to "()", "[]" etc.
316 ;; This function might do hidden buffer changes.
320 (narrow-to-region from to
)
322 (let* ((parts (list nil
)) (tail parts
) pos in-paren
)
324 (while (re-search-forward c-syntactic-ws-start to t
)
325 (goto-char (setq pos
(match-beginning 0)))
326 (c-forward-syntactic-ws)
332 (setq in-paren
(= (car (parse-partial-sexp from pos
1)) 1)
335 (if (and (> pos from
)
337 (looking-at "\\w\\|\\s_")
340 (looking-at "\\w\\|\\s_")))
342 (setcdr tail
(list (buffer-substring-no-properties from pos
)
344 (setq tail
(cddr tail
)))
345 (setcdr tail
(list (buffer-substring-no-properties from pos
)))
346 (setq tail
(cdr tail
)))
349 (when (= (car (parse-partial-sexp pos to -
1)) -
1)
350 (setcdr tail
(list (buffer-substring-no-properties
351 (1- (point)) (point))))
352 (setq tail
(cdr tail
))))
354 (setq from
(point))))
356 (setcdr tail
(list (buffer-substring-no-properties from to
)))
357 (apply 'concat
(cdr parts
))))))
359 (defun c-shift-line-indentation (shift-amt)
360 ;; Shift the indentation of the current line with the specified
361 ;; amount (positive inwards). The buffer is modified only if
362 ;; SHIFT-AMT isn't equal to zero.
363 (let ((pos (- (point-max) (point)))
364 (c-macro-start c-macro-start
)
366 (if (zerop shift-amt
)
368 ;; If we're on an empty line inside a macro, we take the point
369 ;; to be at the current indentation and shift it to the
370 ;; appropriate column. This way we don't treat the extra
371 ;; whitespace out to the line continuation as indentation.
372 (when (and (c-query-and-set-macro-start)
373 (looking-at "[ \t]*\\\\$")
375 (skip-chars-backward " \t")
379 (setq tmp-char-inserted t
))
381 (let ((col (current-indentation)))
382 (delete-region (c-point 'bol
) (c-point 'boi
))
384 (indent-to (+ col shift-amt
)))
385 (when tmp-char-inserted
387 ;; If initial point was within line's indentation and we're not on
388 ;; a line with a line continuation in a macro, position after the
389 ;; indentation. Else stay at same point in text.
390 (if (and (< (point) (c-point 'boi
))
391 (not tmp-char-inserted
))
392 (back-to-indentation)
393 (if (> (- (point-max) pos
) (point))
394 (goto-char (- (point-max) pos
))))))
396 (defsubst c-keyword-sym
(keyword)
397 ;; Return non-nil if the string KEYWORD is a known keyword. More
398 ;; precisely, the value is the symbol for the keyword in
399 ;; `c-keywords-obarray'.
400 (intern-soft keyword c-keywords-obarray
))
402 (defsubst c-keyword-member
(keyword-sym lang-constant
)
403 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
404 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
405 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
406 ;; nil then the result is nil.
407 (get keyword-sym lang-constant
))
409 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
410 (defconst c-string-syntax
(if (memq 'gen-string-delim c-emacs-features
)
414 ;; Regexp matching string limit syntax.
415 (defconst c-string-limit-regexp
(if (memq 'gen-string-delim c-emacs-features
)
419 ;; Regexp matching WS followed by string limit syntax.
420 (defconst c-ws
*-string-limit-regexp
421 (concat "[ \t]*\\(" c-string-limit-regexp
"\\)"))
423 ;; Holds formatted error strings for the few cases where parse errors
425 (defvar c-parsing-error nil
)
426 (make-variable-buffer-local 'c-parsing-error
)
428 (defun c-echo-parsing-error (&optional quiet
)
429 (when (and c-report-syntactic-errors c-parsing-error
(not quiet
))
430 (c-benign-error "%s" c-parsing-error
))
433 ;; Faces given to comments and string literals. This is used in some
434 ;; situations to speed up recognition; it isn't mandatory that font
435 ;; locking is in use. This variable is extended with the face in
436 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
437 (defvar c-literal-faces
438 (append '(font-lock-comment-face font-lock-string-face
)
439 (when (facep 'font-lock-comment-delimiter-face
)
441 '(font-lock-comment-delimiter-face))))
443 (defsubst c-put-c-type-property
(pos value
)
444 ;; Put a c-type property with the given value at POS.
445 (c-put-char-property pos
'c-type value
))
447 (defun c-clear-c-type-property (from to value
)
448 ;; Remove all occurrences of the c-type property that has the given
449 ;; value in the region between FROM and TO. VALUE is assumed to not
452 ;; Note: This assumes that c-type is put on single chars only; it's
453 ;; very inefficient if matching properties cover large regions.
457 (when (eq (get-text-property (point) 'c-type
) value
)
458 (c-clear-char-property (point) 'c-type
))
459 (goto-char (next-single-property-change (point) 'c-type nil to
))
463 ;; Some debug tools to visualize various special positions. This
464 ;; debug code isn't as portable as the rest of CC Mode.
466 (cc-bytecomp-defun overlays-in)
467 (cc-bytecomp-defun overlay-get)
468 (cc-bytecomp-defun overlay-start)
469 (cc-bytecomp-defun overlay-end)
470 (cc-bytecomp-defun delete-overlay)
471 (cc-bytecomp-defun overlay-put)
472 (cc-bytecomp-defun make-overlay)
474 (defun c-debug-add-face (beg end face
)
475 (c-save-buffer-state ((overlays (overlays-in beg end
)) overlay
)
477 (setq overlay
(car overlays
)
478 overlays
(cdr overlays
))
479 (when (eq (overlay-get overlay
'face
) face
)
480 (setq beg
(min beg
(overlay-start overlay
))
481 end
(max end
(overlay-end overlay
)))
482 (delete-overlay overlay
)))
483 (overlay-put (make-overlay beg end
) 'face face
)))
485 (defun c-debug-remove-face (beg end face
)
486 (c-save-buffer-state ((overlays (overlays-in beg end
)) overlay
487 (ol-beg beg
) (ol-end end
))
489 (setq overlay
(car overlays
)
490 overlays
(cdr overlays
))
491 (when (eq (overlay-get overlay
'face
) face
)
492 (setq ol-beg
(min ol-beg
(overlay-start overlay
))
493 ol-end
(max ol-end
(overlay-end overlay
)))
494 (delete-overlay overlay
)))
496 (overlay-put (make-overlay ol-beg beg
) 'face face
))
498 (overlay-put (make-overlay end ol-end
) 'face face
))))
501 ;; `c-beginning-of-statement-1' and accompanying stuff.
503 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
504 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
505 ;; better way should be implemented, but this will at least shut up
506 ;; the byte compiler.
507 (defvar c-maybe-labelp
)
509 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
511 ;; Macros used internally in c-beginning-of-statement-1 for the
512 ;; automaton actions.
513 (defmacro c-bos-push-state
()
514 '(setq stack
(cons (cons state saved-pos
)
516 (defmacro c-bos-pop-state
(&optional do-if-done
)
517 `(if (setq state
(car (car stack
))
518 saved-pos
(cdr (car stack
))
523 (defmacro c-bos-pop-state-and-retry
()
524 '(throw 'loop
(setq state
(car (car stack
))
525 saved-pos
(cdr (car stack
))
526 ;; Throw nil if stack is empty, else throw non-nil.
528 (defmacro c-bos-save-pos
()
529 '(setq saved-pos
(vector pos tok ptok pptok
)))
530 (defmacro c-bos-restore-pos
()
531 '(unless (eq (elt saved-pos
0) start
)
532 (setq pos
(elt saved-pos
0)
533 tok
(elt saved-pos
1)
534 ptok
(elt saved-pos
2)
535 pptok
(elt saved-pos
3))
538 (defmacro c-bos-save-error-info
(missing got
)
539 `(setq saved-pos
(vector pos
,missing
,got
)))
540 (defmacro c-bos-report-error
()
542 (setq c-parsing-error
543 (format "No matching `%s' found for `%s' on line %d"
546 (1+ (count-lines (point-min)
547 (c-point 'bol
(elt saved-pos
0))))))))
549 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
551 "Move to the start of the current statement or declaration, or to
552 the previous one if already at the beginning of one. Only
553 statements/declarations on the same level are considered, i.e. don't
554 move into or out of sexps (not even normal expression parentheses).
556 If point is already at the earliest statement within braces or parens,
557 this function doesn't move back into any whitespace preceding it; it
558 returns 'same in this case.
560 Stop at statement continuation tokens like \"else\", \"catch\",
561 \"finally\" and the \"while\" in \"do ... while\" if the start point
562 is within the continuation. If starting at such a token, move to the
563 corresponding statement start. If at the beginning of a statement,
564 move to the closest containing statement if there is any. This might
565 also stop at a continuation clause.
567 Labels are treated as part of the following statements if
568 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
569 statement start keyword.) Otherwise, each label is treated as a
572 Macros are ignored \(i.e. skipped over) unless point is within one, in
573 which case the content of the macro is treated as normal code. Aside
574 from any normal statement starts found in it, stop at the first token
575 of the content in the macro, i.e. the expression of an \"#if\" or the
576 start of the definition in a \"#define\". Also stop at start of
577 macros before leaving them.
580 'label if stopped at a label or \"case...:\" or \"default:\";
581 'same if stopped at the beginning of the current statement;
582 'up if stepped to a containing statement;
583 'previous if stepped to a preceding statement;
584 'beginning if stepped from a statement continuation clause to
586 'macro if stepped to a macro start.
587 Note that 'same and not 'label is returned if stopped at the same
588 label without crossing the colon character.
590 LIM may be given to limit the search. If the search hits the limit,
591 point will be left at the closest following token, or at the start
592 position if that is less ('same is returned in this case).
594 NOERROR turns off error logging to `c-parsing-error'.
596 Normally only ';' and virtual semicolons are considered to delimit
597 statements, but if COMMA-DELIM is non-nil then ',' is treated
600 Note that this function might do hidden buffer changes. See the
601 comment at the start of cc-engine.el for more info."
603 ;; The bulk of this function is a pushdown automaton that looks at statement
604 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
605 ;; purpose is to keep track of nested statements, ensuring that such
606 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
607 ;; does with nested braces/brackets/parentheses).
609 ;; Note: The position of a boundary is the following token.
611 ;; Beginning with the current token (the one following point), move back one
612 ;; sexp at a time (where a sexp is, more or less, either a token or the
613 ;; entire contents of a brace/bracket/paren pair). Each time a statement
614 ;; boundary is crossed or a "while"-like token is found, update the state of
615 ;; the PDA. Stop at the beginning of a statement when the stack (holding
616 ;; nested statement info) is empty and the position has been moved.
618 ;; The following variables constitute the PDA:
620 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
621 ;; scanned back over, 'boundary if we've just gone back over a
622 ;; statement boundary, or nil otherwise.
623 ;; state: takes one of the values (nil else else-boundary while
624 ;; while-boundary catch catch-boundary).
625 ;; nil means "no "while"-like token yet scanned".
626 ;; 'else, for example, means "just gone back over an else".
627 ;; 'else-boundary means "just gone back over a statement boundary
628 ;; immediately after having gone back over an else".
629 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
630 ;; of error reporting information.
631 ;; stack: The stack onto which the PDA pushes its state. Each entry
632 ;; consists of a saved value of state and saved-pos. An entry is
633 ;; pushed when we move back over a "continuation" token (e.g. else)
634 ;; and popped when we encounter the corresponding opening token
638 ;; The following diagram briefly outlines the PDA.
641 ;; "else": Push state, goto state `else'.
642 ;; "while": Push state, goto state `while'.
643 ;; "catch" or "finally": Push state, goto state `catch'.
644 ;; boundary: Pop state.
645 ;; other: Do nothing special.
648 ;; boundary: Goto state `else-boundary'.
649 ;; other: Error, pop state, retry token.
651 ;; State `else-boundary':
653 ;; boundary: Error, pop state.
654 ;; other: See common state.
657 ;; boundary: Save position, goto state `while-boundary'.
658 ;; other: Pop state, retry token.
660 ;; State `while-boundary':
662 ;; boundary: Restore position if it's not at start, pop state. [*see below]
663 ;; other: See common state.
666 ;; boundary: Goto state `catch-boundary'.
667 ;; other: Error, pop state, retry token.
669 ;; State `catch-boundary':
671 ;; "catch": Goto state `catch'.
672 ;; boundary: Error, pop state.
673 ;; other: See common state.
675 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
676 ;; searching for a "do" which would have opened a do-while. If we didn't
677 ;; find it, we discard the analysis done since the "while", go back to this
678 ;; token in the buffer and restart the scanning there, this time WITHOUT
679 ;; pushing the 'while state onto the stack.
681 ;; In addition to the above there is some special handling of labels
684 (let ((case-fold-search nil
)
687 (delims (if comma-delim
'(?\
; ?,) '(?\;)))
688 (c-stmt-delim-chars (if comma-delim
689 c-stmt-delim-chars-with-comma
691 c-in-literal-cache c-maybe-labelp after-case
:-pos saved
694 ;; Position of last stmt boundary character (e.g. ;).
696 ;; The position of the last sexp or bound that follows the
697 ;; first found colon, i.e. the start of the nonlabel part of
698 ;; the statement. It's `start' if a colon is found just after
701 ;; Like `after-labels-pos', but the first such position inside
702 ;; a label, i.e. the start of the last label before the start
703 ;; of the nonlabel part of the statement.
705 ;; The last position where a label is possible provided the
706 ;; statement started there. It's nil as long as no invalid
707 ;; label content has been found (according to
708 ;; `c-nonlabel-token-key'. It's `start' if no valid label
709 ;; content was found in the label. Note that we might still
710 ;; regard it a label if it starts with `c-label-kwds'.
712 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
715 ;; Current state in the automaton. See above.
717 ;; Current saved positions. See above.
719 ;; Stack of conses (state . saved-pos).
721 ;; Regexp which matches "for", "if", etc.
722 (cond-key (or c-opt-block-stmt-key
723 "\\<\\>")) ; Matches nothing.
726 ;; Positions of the last three sexps or bounds we've stopped at.
730 (if lim
(narrow-to-region lim
(point-max)))
733 (and (c-beginning-of-macro)
735 (setq macro-start
(point)))
737 ;; Try to skip back over unary operator characters, to register
741 (c-backward-syntactic-ws)
742 ;; Protect post-++/-- operators just before a virtual semicolon.
743 (and (not (c-at-vsemi-p))
744 (/= (skip-chars-backward "-+!*&~@`#") 0))))
746 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
747 ;; done. Later on we ignore the boundaries for statements that don't
748 ;; contain any sexp. The only thing that is affected is that the error
749 ;; checking is a little less strict, and we really don't bother.
750 (if (and (memq (char-before) delims
)
751 (progn (forward-char -
1)
753 (c-backward-syntactic-ws)
754 (or (memq (char-before) delims
)
755 (memq (char-before) '(?
: nil
))
756 (eq (char-syntax (char-before)) ?\
()
761 ;; Begin at start and not pos to detect macros if we stand
762 ;; directly after the #.
764 (if (looking-at "\\<\\|\\W")
765 ;; Record this as the first token if not starting inside it.
768 ;; The following while loop goes back one sexp (balanced parens,
769 ;; etc. with contents, or symbol or suchlike) each iteration. This
770 ;; movement is accomplished with a call to scan-sexps approx 130 lines
773 (catch 'loop
;; Throw nil to break, non-nil to continue.
776 (and macro-start
; Always NIL for AWK.
777 (progn (skip-chars-backward " \t")
778 (eq (char-before) ?
#))
779 (progn (setq saved
(1- (point)))
781 (not (eq (char-before (1- (point))) ?
\\)))
782 (looking-at c-opt-cpp-start
)
783 (progn (skip-chars-forward " \t")
784 (eq (point) saved
))))
786 (if (and (c-forward-to-cpp-define-body)
787 (progn (c-forward-syntactic-ws start
)
789 ;; Stop at the first token in the content of the macro.
791 ignore-labels t
) ; Avoid the label check on exit.
797 ;; Do a round through the automaton if we've just passed a
798 ;; statement boundary or passed a "while"-like token.
800 (and (looking-at cond-key
)
801 (setq sym
(intern (match-string 1)))))
803 (when (and (< pos start
) (null stack
))
806 ;; The PDA state handling.
808 ;; Refer to the description of the PDA in the opening
809 ;; comments. In the following OR form, the first leaf
810 ;; attempts to handles one of the specific actions detailed
811 ;; (e.g., finding token "if" whilst in state `else-boundary').
812 ;; We drop through to the second leaf (which handles common
813 ;; state) if no specific handler is found in the first cond.
814 ;; If a parsing error is detected (e.g. an "else" with no
815 ;; preceding "if"), we throw to the enclosing catch.
817 ;; Note that the (eq state 'else) means
818 ;; "we've just passed an else", NOT "we're looking for an
822 (if (eq sym
'boundary
)
823 (setq state
'else-boundary
)
825 (c-bos-pop-state-and-retry)))
827 ((eq state
'else-boundary
)
829 (c-bos-pop-state (setq ret
'beginning
)))
835 (if (and (eq sym
'boundary
)
836 ;; Since this can cause backtracking we do a
837 ;; little more careful analysis to avoid it:
838 ;; If there's a label in front of the while
839 ;; it can't be part of a do-while.
840 (not after-labels-pos
))
841 (progn (c-bos-save-pos)
842 (setq state
'while-boundary
))
843 (c-bos-pop-state-and-retry))) ; Can't be a do-while
845 ((eq state
'while-boundary
)
847 (c-bos-pop-state (setq ret
'beginning
)))
848 ((eq sym
'boundary
) ; isn't a do-while
849 (c-bos-restore-pos) ; the position of the while
850 (c-bos-pop-state)))) ; no longer searching for do.
853 (if (eq sym
'boundary
)
854 (setq state
'catch-boundary
)
856 (c-bos-pop-state-and-retry)))
858 ((eq state
'catch-boundary
)
861 (c-bos-pop-state (setq ret
'beginning
)))
866 (c-bos-pop-state)))))
868 ;; This is state common. We get here when the previous
869 ;; cond statement found no particular state handler.
870 (cond ((eq sym
'boundary
)
871 ;; If we have a boundary at the start
872 ;; position we push a frame to go to the
873 ;; previous statement.
879 (c-bos-save-error-info 'if
'else
)
882 ;; Is this a real while, or a do-while?
883 ;; The next `when' triggers unless we are SURE that
884 ;; the `while' is not the tailend of a `do-while'.
885 (when (or (not pptok
)
886 (memq (char-after pptok
) delims
)
887 ;; The following kludge is to prevent
888 ;; infinite recursion when called from
889 ;; c-awk-after-if-for-while-condition-p,
891 (and (eq (point) start
)
892 (c-vsemi-status-unknown-p))
893 (c-at-vsemi-p pptok
))
894 ;; Since this can cause backtracking we do a
895 ;; little more careful analysis to avoid it: If
896 ;; the while isn't followed by a (possibly
897 ;; virtual) semicolon it can't be a do-while.
899 (setq state
'while
)))
900 ((memq sym
'(catch finally
))
902 (c-bos-save-error-info 'try sym
)
903 (setq state
'catch
))))
906 ;; We're either past a statement boundary or at the
907 ;; start of a statement, so throw away any label data
908 ;; for the previous one.
909 (setq after-labels-pos nil
911 c-maybe-labelp nil
))))
913 ;; Step to the previous sexp, but not if we crossed a
914 ;; boundary, since that doesn't consume an sexp.
915 (if (eq sym
'boundary
)
918 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
919 ;; BACKWARDS THROUGH THE SOURCE.
921 ;; This is typically fast with the caching done by
922 ;; c-(backward|forward)-sws.
923 (c-backward-syntactic-ws)
925 (let ((before-sws-pos (point))
926 ;; Set as long as we have to continue jumping by sexps.
927 ;; It's the position to use as end in the next round.
928 sexp-loop-continue-pos
929 ;; The end position of the area to search for statement
930 ;; barriers in this round.
931 (sexp-loop-end-pos pos
))
933 ;; The following while goes back one sexp per iteration.
936 (unless (c-safe (c-backward-sexp) t
)
937 ;; Give up if we hit an unbalanced block. Since the
938 ;; stack won't be empty the code below will report a
942 ;; Check if the sexp movement crossed a statement or
943 ;; declaration boundary. But first modify the point
944 ;; so that `c-crosses-statement-barrier-p' only looks
945 ;; at the non-sexp chars following the sexp.
953 (when (c-beginning-of-macro)
954 ;; Set continuation position in case
955 ;; `c-crosses-statement-barrier-p'
956 ;; doesn't detect anything below.
957 (setq sexp-loop-continue-pos
(point)))))
958 ;; If the sexp movement took us into a
959 ;; macro then there were only some non-sexp
960 ;; chars after it. Skip out of the macro
961 ;; to analyze them but not the non-sexp
962 ;; chars that might be inside the macro.
964 (c-crosses-statement-barrier-p
965 (point) sexp-loop-end-pos
))
969 (not (c-looking-at-inexpr-block lim nil t
)))
970 ;; Passed a block sexp. That's a boundary
974 ((looking-at "\\s\(")
975 ;; Passed some other paren. Only analyze
976 ;; the non-sexp chars after it.
977 (goto-char (1+ (c-down-list-backward
979 ;; We're at a valid token start position
980 ;; (outside the `save-excursion') if
981 ;; `c-crosses-statement-barrier-p' failed.
982 (c-crosses-statement-barrier-p
983 (point) sexp-loop-end-pos
))
986 ;; Passed a symbol sexp or line
987 ;; continuation. It doesn't matter that
988 ;; it's included in the analyzed region.
989 (if (c-crosses-statement-barrier-p
990 (point) sexp-loop-end-pos
)
992 ;; If it was a line continuation then we
993 ;; have to continue looping.
994 (if (looking-at "\\\\$")
995 (setq sexp-loop-continue-pos
(point)))
1002 ;; Like a C "continue". Analyze the next sexp.
1005 sexp-loop-continue-pos
) ; End of "go back a sexp" loop condition.
1006 (goto-char sexp-loop-continue-pos
)
1007 (setq sexp-loop-end-pos sexp-loop-continue-pos
1008 sexp-loop-continue-pos nil
))))
1011 (when (and c-opt-method-key
1012 (setq saved
(c-in-method-def-p)))
1014 ignore-labels t
) ; Avoid the label check on exit.
1018 (unless (eq ignore-labels t
)
1019 (when (numberp c-maybe-labelp
)
1020 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1021 ;; might be in a label now. Have we got a real label
1022 ;; (including a case label) or something like C++'s "public:"?
1023 ;; A case label might use an expression rather than a token.
1024 (setq after-case
:-pos
(or tok start
))
1025 (if (looking-at c-nonlabel-token-key
) ; e.g. "while" or "'a'"
1026 (setq c-maybe-labelp nil
)
1027 (if after-labels-pos
; Have we already encountered a label?
1028 (if (not last-label-pos
)
1029 (setq last-label-pos
(or tok start
)))
1030 (setq after-labels-pos
(or tok start
)))
1031 (setq c-maybe-labelp t
1032 label-good-pos nil
))) ; bogus "label"
1034 (when (and (not label-good-pos
) ; i.e. no invalid "label"'s yet
1036 (looking-at c-nonlabel-token-key
)) ; e.g. "while :"
1037 ;; We're in a potential label and it's the first
1038 ;; time we've found something that isn't allowed in
1040 (setq label-good-pos
(or tok start
))))
1042 ;; We've moved back by a sexp, so update the token positions.
1047 pos tok
))) ; Not nil (for the while loop).
1049 ;; If the stack isn't empty there might be errors to report.
1051 (if (and (vectorp saved-pos
) (eq (length saved-pos
) 3))
1052 (c-bos-report-error))
1053 (setq saved-pos
(cdr (car stack
))
1056 (when (and (eq ret
'same
)
1057 (not (memq sym
'(boundary ignore nil
))))
1058 ;; Need to investigate closer whether we've crossed
1059 ;; between a substatement and its containing statement.
1060 (if (setq saved
(if (looking-at c-block-stmt-1-key
)
1063 (cond ((> start saved
) (setq pos saved
))
1064 ((= start saved
) (setq ret
'up
)))))
1066 (when (and (not ignore-labels
)
1067 (eq c-maybe-labelp t
)
1068 (not (eq ret
'beginning
))
1070 (or (not label-good-pos
)
1071 (<= label-good-pos pos
)
1073 (goto-char (if (and last-label-pos
1074 (< last-label-pos start
))
1077 (looking-at c-label-kwds-regexp
))))
1078 ;; We're in a label. Maybe we should step to the statement
1080 (if (< after-labels-pos start
)
1081 (setq pos after-labels-pos
)
1083 (if (and last-label-pos
(< last-label-pos start
))
1084 ;; Might have jumped over several labels. Go to the last one.
1085 (setq pos last-label-pos
)))))
1087 ;; Have we got "case <expression>:"?
1089 (when (and after-case
:-pos
1090 (not (eq ret
'beginning
))
1091 (looking-at c-case-kwds-regexp
))
1092 (if (< after-case
:-pos start
)
1093 (setq pos after-case
:-pos
))
1097 ;; Skip over the unary operators that can start the statement.
1099 (c-backward-syntactic-ws)
1100 ;; protect AWK post-inc/decrement operators, etc.
1101 (and (not (c-at-vsemi-p (point)))
1102 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1107 (defun c-crosses-statement-barrier-p (from to
)
1108 "Return non-nil if buffer positions FROM to TO cross one or more
1109 statement or declaration boundaries. The returned value is actually
1110 the position of the earliest boundary char. FROM must not be within
1111 a string or comment.
1113 The variable `c-maybe-labelp' is set to the position of the first `:' that
1114 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1115 single `?' is found, then `c-maybe-labelp' is cleared.
1117 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1118 regarded as having a \"virtual semicolon\" immediately after the last token on
1119 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1121 Note that this function might do hidden buffer changes. See the
1122 comment at the start of cc-engine.el for more info."
1123 (let ((skip-chars c-stmt-delim-chars
)
1128 (while (progn (skip-chars-forward skip-chars to
)
1131 ((setq lit-range
(c-literal-limits from
)) ; Have we landed in a string/comment?
1132 (goto-char (cdr lit-range
)))
1133 ((eq (char-after) ?
:)
1135 (if (and (eq (char-after) ?
:)
1137 ;; Ignore scope operators.
1139 (setq c-maybe-labelp
(1- (point)))))
1140 ((eq (char-after) ??
)
1141 ;; A question mark. Can't be a label, so stop
1142 ;; looking for more : and ?.
1143 (setq c-maybe-labelp nil
1144 skip-chars
(substring c-stmt-delim-chars
0 -
2)))
1145 ((memq (char-after) '(?
# ?
\n ?
\r)) ; A virtual semicolon?
1146 (if (and (eq (char-before) ?
\\) (memq (char-after) '(?
\n ?
\r)))
1148 (skip-chars-backward " \t" from
)
1150 (throw 'done
(point))
1152 (t (throw 'done
(point)))))
1153 ;; In trailing space after an as yet undetected virtual semicolon?
1154 (c-backward-syntactic-ws from
)
1155 (if (and (< (point) to
)
1160 (defun c-at-statement-start-p ()
1161 "Return non-nil if the point is at the first token in a statement
1162 or somewhere in the syntactic whitespace before it.
1164 A \"statement\" here is not restricted to those inside code blocks.
1165 Any kind of declaration-like construct that occur outside function
1166 bodies is also considered a \"statement\".
1168 Note that this function might do hidden buffer changes. See the
1169 comment at the start of cc-engine.el for more info."
1174 (c-syntactic-skip-backward (substring c-stmt-delim-chars
1) nil t
)
1176 (eq (char-before) ?
})
1177 (and (eq (char-before) ?
{)
1178 (not (and c-special-brace-lists
1179 (progn (backward-char)
1180 (c-looking-at-special-brace-list)))))
1181 (c-crosses-statement-barrier-p (point) end
)))))
1183 (defun c-at-expression-start-p ()
1184 "Return non-nil if the point is at the first token in an expression or
1185 statement, or somewhere in the syntactic whitespace before it.
1187 An \"expression\" here is a bit different from the normal language
1188 grammar sense: It's any sequence of expression tokens except commas,
1189 unless they are enclosed inside parentheses of some kind. Also, an
1190 expression never continues past an enclosing parenthesis, but it might
1191 contain parenthesis pairs of any sort except braces.
1193 Since expressions never cross statement boundaries, this function also
1194 recognizes statement beginnings, just like `c-at-statement-start-p'.
1196 Note that this function might do hidden buffer changes. See the
1197 comment at the start of cc-engine.el for more info."
1201 (c-stmt-delim-chars c-stmt-delim-chars-with-comma
)
1203 (c-syntactic-skip-backward (substring c-stmt-delim-chars
1) nil t
)
1205 (memq (char-before) '(?
{ ?
}))
1206 (save-excursion (backward-char)
1207 (looking-at "\\s("))
1208 (c-crosses-statement-barrier-p (point) end
)))))
1211 ;; A set of functions that covers various idiosyncrasies in
1212 ;; implementations of `forward-comment'.
1214 ;; Note: Some emacsen considers incorrectly that any line comment
1215 ;; ending with a backslash continues to the next line. I can't think
1216 ;; of any way to work around that in a reliable way without changing
1217 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1218 ;; changing the syntax for backslash doesn't work since we must treat
1219 ;; escapes in string literals correctly.)
1221 (defun c-forward-single-comment ()
1222 "Move forward past whitespace and the closest following comment, if any.
1223 Return t if a comment was found, nil otherwise. In either case, the
1224 point is moved past the following whitespace. Line continuations,
1225 i.e. a backslashes followed by line breaks, are treated as whitespace.
1226 The line breaks that end line comments are considered to be the
1227 comment enders, so the point will be put on the beginning of the next
1228 line if it moved past a line comment.
1230 This function does not do any hidden buffer changes."
1232 (let ((start (point)))
1233 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1234 (goto-char (match-end 0)))
1236 (when (forward-comment 1)
1238 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1242 ;; Emacs includes the ending newline in a b-style (c++)
1243 ;; comment, but XEmacs doesn't. We depend on the Emacs
1244 ;; behavior (which also is symmetric).
1245 (if (and (eolp) (elt (parse-partial-sexp start
(point)) 7))
1246 (condition-case nil
(forward-char 1)))
1250 (defsubst c-forward-comments
()
1251 "Move forward past all following whitespace and comments.
1252 Line continuations, i.e. a backslashes followed by line breaks, are
1253 treated as whitespace.
1255 Note that this function might do hidden buffer changes. See the
1256 comment at the start of cc-engine.el for more info."
1259 ;; If forward-comment in at least XEmacs 21 is given a large
1260 ;; positive value, it'll loop all the way through if it hits
1262 (and (forward-comment 5)
1263 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1267 (when (looking-at "\\\\[\n\r]")
1271 (defun c-backward-single-comment ()
1272 "Move backward past whitespace and the closest preceding comment, if any.
1273 Return t if a comment was found, nil otherwise. In either case, the
1274 point is moved past the preceding whitespace. Line continuations,
1275 i.e. a backslashes followed by line breaks, are treated as whitespace.
1276 The line breaks that end line comments are considered to be the
1277 comment enders, so the point cannot be at the end of the same line to
1278 move over a line comment.
1280 This function does not do any hidden buffer changes."
1282 (let ((start (point)))
1283 ;; When we got newline terminated comments, forward-comment in all
1284 ;; supported emacsen so far will stop at eol of each line not
1285 ;; ending with a comment when moving backwards. This corrects for
1286 ;; that, and at the same time handles line continuations.
1288 (skip-chars-backward " \t\n\r\f\v")
1289 (and (looking-at "[\n\r]")
1290 (eq (char-before) ?
\\)))
1294 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1295 ;; backwards at bob.
1298 ;; Leave point after the closest following newline if we've
1299 ;; backed up over any above, since forward-comment won't move
1300 ;; backward over a line comment if point is at the end of the
1302 (re-search-forward "\\=\\s *[\n\r]" start t
)
1304 (if (if (forward-comment -
1)
1306 ;; If forward-comment above succeeded and we're at eol
1307 ;; then the newline we moved over above didn't end a
1308 ;; line comment, so we give it another go.
1309 (forward-comment -
1)
1312 ;; Emacs <= 20 and XEmacs move back over the closer of a
1313 ;; block comment that lacks an opener.
1314 (if (looking-at "\\*/")
1315 (progn (forward-char 2) nil
)
1318 (defsubst c-backward-comments
()
1319 "Move backward past all preceding whitespace and comments.
1320 Line continuations, i.e. a backslashes followed by line breaks, are
1321 treated as whitespace. The line breaks that end line comments are
1322 considered to be the comment enders, so the point cannot be at the end
1323 of the same line to move over a line comment. Unlike
1324 c-backward-syntactic-ws, this function doesn't move back over
1325 preprocessor directives.
1327 Note that this function might do hidden buffer changes. See the
1328 comment at the start of cc-engine.el for more info."
1330 (let ((start (point)))
1332 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1333 ;; return t when moving backwards at bob.
1336 (if (forward-comment -
1)
1337 (if (looking-at "\\*/")
1338 ;; Emacs <= 20 and XEmacs move back over the
1339 ;; closer of a block comment that lacks an opener.
1340 (progn (forward-char 2) nil
)
1343 ;; XEmacs treats line continuations as whitespace but
1344 ;; only in the backward direction, which seems a bit
1345 ;; odd. Anyway, this is necessary for Emacs.
1346 (when (and (looking-at "[\n\r]")
1347 (eq (char-before) ?
\\)
1353 ;; Tools for skipping over syntactic whitespace.
1355 ;; The following functions use text properties to cache searches over
1356 ;; large regions of syntactic whitespace. It works as follows:
1358 ;; o If a syntactic whitespace region contains anything but simple
1359 ;; whitespace (i.e. space, tab and line breaks), the text property
1360 ;; `c-in-sws' is put over it. At places where we have stopped
1361 ;; within that region there's also a `c-is-sws' text property.
1362 ;; That since there typically are nested whitespace inside that
1363 ;; must be handled separately, e.g. whitespace inside a comment or
1364 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1365 ;; to jump to another point with that property within the same
1366 ;; `c-in-sws' region. It can be likened to a ladder where
1367 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1369 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1370 ;; a "rung position" and also maybe on the first following char.
1371 ;; As many characters as can be conveniently found in this range
1372 ;; are marked, but no assumption can be made that the whole range
1373 ;; is marked (it could be clobbered by later changes, for
1376 ;; Note that some part of the beginning of a sequence of simple
1377 ;; whitespace might be part of the end of a preceding line comment
1378 ;; or cpp directive and must not be considered part of the "rung".
1379 ;; Such whitespace is some amount of horizontal whitespace followed
1380 ;; by a newline. In the case of cpp directives it could also be
1381 ;; two newlines with horizontal whitespace between them.
1383 ;; The reason to include the first following char is to cope with
1384 ;; "rung positions" that doesn't have any ordinary whitespace. If
1385 ;; `c-is-sws' is put on a token character it does not have
1386 ;; `c-in-sws' set simultaneously. That's the only case when that
1387 ;; can occur, and the reason for not extending the `c-in-sws'
1388 ;; region to cover it is that the `c-in-sws' region could then be
1389 ;; accidentally merged with a following one if the token is only
1390 ;; one character long.
1392 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1393 ;; removed in the changed region. If the change was inside
1394 ;; syntactic whitespace that means that the "ladder" is broken, but
1395 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1396 ;; parts on either side and use an ordinary search only to "repair"
1399 ;; Special care needs to be taken if a region is removed: If there
1400 ;; are `c-in-sws' on both sides of it which do not connect inside
1401 ;; the region then they can't be joined. If e.g. a marked macro is
1402 ;; broken, syntactic whitespace inside the new text might be
1403 ;; marked. If those marks would become connected with the old
1404 ;; `c-in-sws' range around the macro then we could get a ladder
1405 ;; with one end outside the macro and the other at some whitespace
1408 ;; The main motivation for this system is to increase the speed in
1409 ;; skipping over the large whitespace regions that can occur at the
1410 ;; top level in e.g. header files that contain a lot of comments and
1411 ;; cpp directives. For small comments inside code it's probably
1412 ;; slower than using `forward-comment' straightforwardly, but speed is
1413 ;; not a significant factor there anyway.
1415 ; (defface c-debug-is-sws-face
1416 ; '((t (:background "GreenYellow")))
1417 ; "Debug face to mark the `c-is-sws' property.")
1418 ; (defface c-debug-in-sws-face
1419 ; '((t (:underline t)))
1420 ; "Debug face to mark the `c-in-sws' property.")
1422 ; (defun c-debug-put-sws-faces ()
1423 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1424 ; ;; properties in the buffer.
1427 ; (c-save-buffer-state (in-face)
1428 ; (goto-char (point-min))
1429 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1432 ; (goto-char (next-single-property-change
1433 ; (point) 'c-is-sws nil (point-max)))
1436 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1437 ; (setq in-face nil))
1438 ; (setq in-face (point)))
1440 ; (goto-char (point-min))
1441 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1444 ; (goto-char (next-single-property-change
1445 ; (point) 'c-in-sws nil (point-max)))
1448 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1449 ; (setq in-face nil))
1450 ; (setq in-face (point)))
1453 (defmacro c-debug-sws-msg
(&rest args
)
1457 (defmacro c-put-is-sws
(beg end
)
1458 ;; This macro does a hidden buffer change.
1459 `(let ((beg ,beg
) (end ,end
))
1460 (put-text-property beg end
'c-is-sws t
)
1461 ,@(when (facep 'c-debug-is-sws-face
)
1462 `((c-debug-add-face beg end
'c-debug-is-sws-face
)))))
1464 (defmacro c-put-in-sws
(beg end
)
1465 ;; This macro does a hidden buffer change.
1466 `(let ((beg ,beg
) (end ,end
))
1467 (put-text-property beg end
'c-in-sws t
)
1468 ,@(when (facep 'c-debug-is-sws-face
)
1469 `((c-debug-add-face beg end
'c-debug-in-sws-face
)))))
1471 (defmacro c-remove-is-sws
(beg end
)
1472 ;; This macro does a hidden buffer change.
1473 `(let ((beg ,beg
) (end ,end
))
1474 (remove-text-properties beg end
'(c-is-sws nil
))
1475 ,@(when (facep 'c-debug-is-sws-face
)
1476 `((c-debug-remove-face beg end
'c-debug-is-sws-face
)))))
1478 (defmacro c-remove-in-sws
(beg end
)
1479 ;; This macro does a hidden buffer change.
1480 `(let ((beg ,beg
) (end ,end
))
1481 (remove-text-properties beg end
'(c-in-sws nil
))
1482 ,@(when (facep 'c-debug-is-sws-face
)
1483 `((c-debug-remove-face beg end
'c-debug-in-sws-face
)))))
1485 (defmacro c-remove-is-and-in-sws
(beg end
)
1486 ;; This macro does a hidden buffer change.
1487 `(let ((beg ,beg
) (end ,end
))
1488 (remove-text-properties beg end
'(c-is-sws nil c-in-sws nil
))
1489 ,@(when (facep 'c-debug-is-sws-face
)
1490 `((c-debug-remove-face beg end
'c-debug-is-sws-face
)
1491 (c-debug-remove-face beg end
'c-debug-in-sws-face
)))))
1493 (defsubst c-invalidate-sws-region-after
(beg end
)
1494 ;; Called from `after-change-functions'. Note that if
1495 ;; `c-forward-sws' or `c-backward-sws' are used outside
1496 ;; `c-save-buffer-state' or similar then this will remove the cache
1497 ;; properties right after they're added.
1499 ;; This function does hidden buffer changes.
1502 ;; Adjust the end to remove the properties in any following simple
1503 ;; ws up to and including the next line break, if there is any
1504 ;; after the changed region. This is necessary e.g. when a rung
1505 ;; marked empty line is converted to a line comment by inserting
1506 ;; "//" before the line break. In that case the line break would
1507 ;; keep the rung mark which could make a later `c-backward-sws'
1508 ;; move into the line comment instead of over it.
1510 (skip-chars-forward " \t\f\v")
1511 (when (and (eolp) (not (eobp)))
1512 (setq end
(1+ (point)))))
1514 (when (and (= beg end
)
1515 (get-text-property beg
'c-in-sws
)
1517 (get-text-property (1- beg
) 'c-in-sws
))
1518 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1519 ;; safe to keep a range that was continuous before the change. E.g:
1525 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1526 ;; after "foo" is removed then "bar" will become part of the cpp
1527 ;; directive instead of a syntactically relevant token. In that
1528 ;; case there's no longer syntactic ws from "#" to "b".
1529 (setq beg
(1- beg
)))
1531 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end
)
1532 (c-remove-is-and-in-sws beg end
))
1534 (defun c-forward-sws ()
1535 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1537 ;; This function might do hidden buffer changes.
1539 (let (;; `rung-pos' is set to a position as early as possible in the
1540 ;; unmarked part of the simple ws region.
1541 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1542 rung-is-marked next-rung-is-marked simple-ws-end
1543 ;; `safe-start' is set when it's safe to cache the start position.
1544 ;; It's not set if we've initially skipped over comments and line
1545 ;; continuations since we might have gone out through the end of a
1546 ;; macro then. This provision makes `c-forward-sws' not populate the
1547 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1551 ;; Skip simple ws and do a quick check on the following character to see
1552 ;; if it's anything that can't start syntactic ws, so we can bail out
1553 ;; early in the majority of cases when there just are a few ws chars.
1554 (skip-chars-forward " \t\n\r\f\v")
1555 (when (looking-at c-syntactic-ws-start
)
1557 (setq rung-end-pos
(min (1+ (point)) (point-max)))
1558 (if (setq rung-is-marked
(text-property-any rung-pos rung-end-pos
1560 ;; Find the last rung position to avoid setting properties in all
1561 ;; the cases when the marked rung is complete.
1562 ;; (`next-single-property-change' is certain to move at least one
1564 (setq rung-pos
(1- (next-single-property-change
1565 rung-is-marked
'c-is-sws nil rung-end-pos
)))
1566 ;; Got no marked rung here. Since the simple ws might have started
1567 ;; inside a line comment or cpp directive we must set `rung-pos' as
1568 ;; high as possible.
1569 (setq rung-pos
(point)))
1574 (when (and rung-is-marked
1575 (get-text-property (point) 'c-in-sws
))
1577 ;; The following search is the main reason that `c-in-sws'
1578 ;; and `c-is-sws' aren't combined to one property.
1579 (goto-char (next-single-property-change
1580 (point) 'c-in-sws nil
(point-max)))
1581 (unless (get-text-property (point) 'c-is-sws
)
1582 ;; If the `c-in-sws' region extended past the last
1583 ;; `c-is-sws' char we have to go back a bit.
1584 (or (get-text-property (1- (point)) 'c-is-sws
)
1585 (goto-char (previous-single-property-change
1586 (point) 'c-is-sws
)))
1590 "c-forward-sws cached move %s -> %s (max %s)"
1591 rung-pos
(point) (point-max))
1593 (setq rung-pos
(point))
1594 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1597 ;; We'll loop here if there is simple ws after the last rung.
1598 ;; That means that there's been some change in it and it's
1599 ;; possible that we've stepped into another ladder, so extend
1600 ;; the previous one to join with it if there is one, and try to
1601 ;; use the cache again.
1603 "c-forward-sws extending rung with [%s..%s] (max %s)"
1604 (1+ rung-pos
) (1+ (point)) (point-max))
1605 (unless (get-text-property (point) 'c-is-sws
)
1606 ;; Remove any `c-in-sws' property from the last char of
1607 ;; the rung before we mark it with `c-is-sws', so that we
1608 ;; won't connect with the remains of a broken "ladder".
1609 (c-remove-in-sws (point) (1+ (point))))
1610 (c-put-is-sws (1+ rung-pos
)
1612 (c-put-in-sws rung-pos
1613 (setq rung-pos
(point)
1614 last-put-in-sws-pos rung-pos
)))
1616 (setq simple-ws-end
(point))
1617 (c-forward-comments)
1620 ((/= (point) simple-ws-end
)
1621 ;; Skipped over comments. Don't cache at eob in case the buffer
1626 (and c-opt-cpp-prefix
1627 (looking-at c-opt-cpp-start
)
1628 (progn (skip-chars-backward " \t")
1631 (progn (backward-char)
1632 (not (eq (char-before) ?
\\))))))
1633 ;; Skip a preprocessor directive.
1635 (while (and (eq (char-before) ?
\\)
1636 (= (forward-line 1) 0))
1640 ;; Don't cache at eob in case the buffer is narrowed.
1643 ;; We've searched over a piece of non-white syntactic ws. See if this
1645 (setq next-rung-pos
(point))
1646 (skip-chars-forward " \t\n\r\f\v")
1647 (setq rung-end-pos
(min (1+ (point)) (point-max)))
1650 ;; Cache if we haven't skipped comments only, and if we started
1651 ;; either from a marked rung or from a completely uncached
1655 (not (get-text-property simple-ws-end
'c-in-sws
))))
1657 ;; See if there's a marked rung in the encountered simple ws. If
1658 ;; so then we can cache, unless `safe-start' is nil. Even then
1659 ;; we need to do this to check if the cache can be used for the
1661 (and (setq next-rung-is-marked
1662 (text-property-any next-rung-pos rung-end-pos
1668 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1669 rung-pos
(1+ simple-ws-end
) next-rung-pos rung-end-pos
1672 ;; Remove the properties for any nested ws that might be cached.
1673 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1675 (c-remove-is-sws (1+ simple-ws-end
) next-rung-pos
)
1676 (unless (and rung-is-marked
(= rung-pos simple-ws-end
))
1677 (c-put-is-sws rung-pos
1679 (setq rung-is-marked t
))
1680 (c-put-in-sws rung-pos
1681 (setq rung-pos
(point)
1682 last-put-in-sws-pos rung-pos
))
1683 (unless (get-text-property (1- rung-end-pos
) 'c-is-sws
)
1684 ;; Remove any `c-in-sws' property from the last char of
1685 ;; the rung before we mark it with `c-is-sws', so that we
1686 ;; won't connect with the remains of a broken "ladder".
1687 (c-remove-in-sws (1- rung-end-pos
) rung-end-pos
))
1688 (c-put-is-sws next-rung-pos
1692 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1693 rung-pos
(1+ simple-ws-end
) next-rung-pos rung-end-pos
1696 ;; Set `rung-pos' for the next rung. It's the same thing here as
1697 ;; initially, except that the rung position is set as early as
1698 ;; possible since we can't be in the ending ws of a line comment or
1699 ;; cpp directive now.
1700 (if (setq rung-is-marked next-rung-is-marked
)
1701 (setq rung-pos
(1- (next-single-property-change
1702 rung-is-marked
'c-is-sws nil rung-end-pos
)))
1703 (setq rung-pos next-rung-pos
))
1704 (setq safe-start t
)))
1706 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1707 ;; another one after the point (which might occur when editing inside a
1708 ;; comment or macro).
1709 (when (eq last-put-in-sws-pos
(point))
1710 (cond ((< last-put-in-sws-pos
(point-max))
1712 "c-forward-sws clearing at %s for cache separation"
1713 last-put-in-sws-pos
)
1714 (c-remove-in-sws last-put-in-sws-pos
1715 (1+ last-put-in-sws-pos
)))
1717 ;; If at eob we have to clear the last character before the end
1718 ;; instead since the buffer might be narrowed and there might
1719 ;; be a `c-in-sws' after (point-max). In this case it's
1720 ;; necessary to clear both properties.
1722 "c-forward-sws clearing thoroughly at %s for cache separation"
1723 (1- last-put-in-sws-pos
))
1724 (c-remove-is-and-in-sws (1- last-put-in-sws-pos
)
1725 last-put-in-sws-pos
))))
1728 (defun c-backward-sws ()
1729 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1731 ;; This function might do hidden buffer changes.
1733 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1734 ;; part of the simple ws region.
1735 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1736 rung-is-marked simple-ws-beg cmt-skip-pos
)
1738 ;; Skip simple horizontal ws and do a quick check on the preceding
1739 ;; character to see if it's anying that can't end syntactic ws, so we can
1740 ;; bail out early in the majority of cases when there just are a few ws
1741 ;; chars. Newlines are complicated in the backward direction, so we can't
1743 (skip-chars-backward " \t\f")
1744 (when (and (not (bobp))
1747 (looking-at c-syntactic-ws-end
)))
1749 ;; Try to find a rung position in the simple ws preceding point, so that
1750 ;; we can get a cache hit even if the last bit of the simple ws has
1751 ;; changed recently.
1752 (setq simple-ws-beg
(point))
1753 (skip-chars-backward " \t\n\r\f\v")
1754 (if (setq rung-is-marked
(text-property-any
1755 (point) (min (1+ rung-pos
) (point-max))
1757 ;; `rung-pos' will be the earliest marked position, which means that
1758 ;; there might be later unmarked parts in the simple ws region.
1759 ;; It's not worth the effort to fix that; the last part of the
1760 ;; simple ws is also typically edited often, so it could be wasted.
1761 (goto-char (setq rung-pos rung-is-marked
))
1762 (goto-char simple-ws-beg
))
1767 (when (and rung-is-marked
1769 (get-text-property (1- (point)) 'c-in-sws
))
1771 ;; The following search is the main reason that `c-in-sws'
1772 ;; and `c-is-sws' aren't combined to one property.
1773 (goto-char (previous-single-property-change
1774 (point) 'c-in-sws nil
(point-min)))
1775 (unless (get-text-property (point) 'c-is-sws
)
1776 ;; If the `c-in-sws' region extended past the first
1777 ;; `c-is-sws' char we have to go forward a bit.
1778 (goto-char (next-single-property-change
1779 (point) 'c-is-sws
)))
1782 "c-backward-sws cached move %s <- %s (min %s)"
1783 (point) rung-pos
(point-min))
1785 (setq rung-pos
(point))
1786 (if (and (< (min (skip-chars-backward " \t\f\v")
1788 (setq simple-ws-beg
(point))
1789 (skip-chars-backward " \t\n\r\f\v")))
1791 (setq rung-is-marked
1792 (text-property-any (point) rung-pos
1795 (goto-char simple-ws-beg
)
1798 ;; We'll loop here if there is simple ws before the first rung.
1799 ;; That means that there's been some change in it and it's
1800 ;; possible that we've stepped into another ladder, so extend
1801 ;; the previous one to join with it if there is one, and try to
1802 ;; use the cache again.
1804 "c-backward-sws extending rung with [%s..%s] (min %s)"
1805 rung-is-marked rung-pos
(point-min))
1806 (unless (get-text-property (1- rung-pos
) 'c-is-sws
)
1807 ;; Remove any `c-in-sws' property from the last char of
1808 ;; the rung before we mark it with `c-is-sws', so that we
1809 ;; won't connect with the remains of a broken "ladder".
1810 (c-remove-in-sws (1- rung-pos
) rung-pos
))
1811 (c-put-is-sws rung-is-marked
1813 (c-put-in-sws rung-is-marked
1815 (setq rung-pos rung-is-marked
1816 last-put-in-sws-pos rung-pos
))
1818 (c-backward-comments)
1819 (setq cmt-skip-pos
(point))
1822 ((and c-opt-cpp-prefix
1823 (/= cmt-skip-pos simple-ws-beg
)
1824 (c-beginning-of-macro))
1825 ;; Inside a cpp directive. See if it should be skipped over.
1826 (let ((cpp-beg (point)))
1828 ;; Move back over all line continuations in the region skipped
1829 ;; over by `c-backward-comments'. If we go past it then we
1830 ;; started inside the cpp directive.
1831 (goto-char simple-ws-beg
)
1833 (while (and (> (point) cmt-skip-pos
)
1834 (progn (backward-char)
1835 (eq (char-before) ?
\\)))
1836 (beginning-of-line))
1838 (if (< (point) cmt-skip-pos
)
1839 ;; Don't move past the cpp directive if we began inside
1840 ;; it. Note that the position at the end of the last line
1841 ;; of the macro is also considered to be within it.
1842 (progn (goto-char cmt-skip-pos
)
1845 ;; It's worthwhile to spend a little bit of effort on finding
1846 ;; the end of the macro, to get a good `simple-ws-beg'
1847 ;; position for the cache. Note that `c-backward-comments'
1848 ;; could have stepped over some comments before going into
1849 ;; the macro, and then `simple-ws-beg' must be kept on the
1850 ;; same side of those comments.
1851 (goto-char simple-ws-beg
)
1852 (skip-chars-backward " \t\n\r\f\v")
1853 (if (eq (char-before) ?
\\)
1856 (if (< (point) simple-ws-beg
)
1857 ;; Might happen if comments after the macro were skipped
1859 (setq simple-ws-beg
(point)))
1864 ((/= (save-excursion
1865 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg
)
1866 (setq next-rung-pos
(point)))
1868 ;; Skipped over comments. Must put point at the end of
1869 ;; the simple ws at point since we might be after a line
1870 ;; comment or cpp directive that's been partially
1871 ;; narrowed out, and we can't risk marking the simple ws
1872 ;; at the end of it.
1873 (goto-char next-rung-pos
)
1876 ;; We've searched over a piece of non-white syntactic ws. See if this
1878 (setq next-rung-pos
(point))
1879 (skip-chars-backward " \t\f\v")
1882 ;; Cache if we started either from a marked rung or from a
1883 ;; completely uncached position.
1885 (not (get-text-property (1- simple-ws-beg
) 'c-in-sws
))
1887 ;; Cache if there's a marked rung in the encountered simple ws.
1889 (skip-chars-backward " \t\n\r\f\v")
1890 (text-property-any (point) (min (1+ next-rung-pos
) (point-max))
1895 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1896 (point) (1+ next-rung-pos
)
1897 simple-ws-beg
(min (1+ rung-pos
) (point-max))
1900 ;; Remove the properties for any nested ws that might be cached.
1901 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1903 (c-remove-is-sws (1+ next-rung-pos
) simple-ws-beg
)
1904 (unless (and rung-is-marked
(= simple-ws-beg rung-pos
))
1905 (let ((rung-end-pos (min (1+ rung-pos
) (point-max))))
1906 (unless (get-text-property (1- rung-end-pos
) 'c-is-sws
)
1907 ;; Remove any `c-in-sws' property from the last char of
1908 ;; the rung before we mark it with `c-is-sws', so that we
1909 ;; won't connect with the remains of a broken "ladder".
1910 (c-remove-in-sws (1- rung-end-pos
) rung-end-pos
))
1911 (c-put-is-sws simple-ws-beg
1913 (setq rung-is-marked t
)))
1914 (c-put-in-sws (setq simple-ws-beg
(point)
1915 last-put-in-sws-pos simple-ws-beg
)
1917 (c-put-is-sws (setq rung-pos simple-ws-beg
)
1918 (1+ next-rung-pos
)))
1921 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1922 (point) (1+ next-rung-pos
)
1923 simple-ws-beg
(min (1+ rung-pos
) (point-max))
1925 (setq rung-pos next-rung-pos
1926 simple-ws-beg
(point))
1929 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1930 ;; another one before the point (which might occur when editing inside a
1931 ;; comment or macro).
1932 (when (eq last-put-in-sws-pos
(point))
1933 (cond ((< (point-min) last-put-in-sws-pos
)
1935 "c-backward-sws clearing at %s for cache separation"
1936 (1- last-put-in-sws-pos
))
1937 (c-remove-in-sws (1- last-put-in-sws-pos
)
1938 last-put-in-sws-pos
))
1940 ;; If at bob and the buffer is narrowed, we have to clear the
1941 ;; character we're standing on instead since there might be a
1942 ;; `c-in-sws' before (point-min). In this case it's necessary
1943 ;; to clear both properties.
1945 "c-backward-sws clearing thoroughly at %s for cache separation"
1946 last-put-in-sws-pos
)
1947 (c-remove-is-and-in-sws last-put-in-sws-pos
1948 (1+ last-put-in-sws-pos
)))))
1952 ;; Other whitespace tools
1953 (defun c-partial-ws-p (beg end
)
1954 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1955 ;; region? This is a "heuristic" function. .....
1957 ;; The motivation for the second bit is to check whether removing this
1958 ;; region would coalesce two symbols.
1960 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1961 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1963 (let ((end+1 (min (1+ end
) (point-max))))
1964 (or (progn (goto-char (max (point-min) (1- beg
)))
1965 (c-skip-ws-forward end
)
1967 (progn (goto-char beg
)
1968 (c-skip-ws-forward end
+1)
1969 (eq (point) end
+1))))))
1971 ;; A system for finding noteworthy parens before the point.
1973 (defconst c-state-cache-too-far
5000)
1974 ;; A maximum comfortable scanning distance, e.g. between
1975 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
1976 ;; this distance is exceeded, we take "emergency meausures", e.g. by clearing
1977 ;; the cache and starting again from point-min or a beginning of defun. This
1978 ;; value can be tuned for efficiency or set to a lower value for testing.
1980 (defvar c-state-cache nil
)
1981 (make-variable-buffer-local 'c-state-cache
)
1982 ;; The state cache used by `c-parse-state' to cut down the amount of
1983 ;; searching. It's the result from some earlier `c-parse-state' call. See
1984 ;; `c-parse-state''s doc string for details of its structure.
1986 ;; The use of the cached info is more effective if the next
1987 ;; `c-parse-state' call is on a line close by the one the cached state
1988 ;; was made at; the cache can actually slow down a little if the
1989 ;; cached state was made very far back in the buffer. The cache is
1990 ;; most effective if `c-parse-state' is used on each line while moving
1993 (defvar c-state-cache-good-pos
1)
1994 (make-variable-buffer-local 'c-state-cache-good-pos
)
1995 ;; This is a position where `c-state-cache' is known to be correct, or
1996 ;; nil (see below). It's a position inside one of the recorded unclosed
1997 ;; parens or the top level, but not further nested inside any literal or
1998 ;; subparen that is closed before the last recorded position.
2000 ;; The exact position is chosen to try to be close to yet earlier than
2001 ;; the position where `c-state-cache' will be called next. Right now
2002 ;; the heuristic is to set it to the position after the last found
2003 ;; closing paren (of any type) before the line on which
2004 ;; `c-parse-state' was called. That is chosen primarily to work well
2005 ;; with refontification of the current line.
2007 ;; 2009-07-28: When `c-state-point-min' and the last position where
2008 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2009 ;; both in the same literal, there is no such "good position", and
2010 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2011 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2013 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2014 ;; the middle of the desert, as long as it is not within a brace pair
2015 ;; recorded in `c-state-cache' or a paren/bracket pair.
2018 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2019 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2020 ;; speed up testing for non-literality.
2021 (defconst c-state-nonlit-pos-interval
10000)
2022 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2024 (defvar c-state-nonlit-pos-cache nil
)
2025 (make-variable-buffer-local 'c-state-nonlit-pos-cache
)
2026 ;; A list of buffer positions which are known not to be in a literal or a cpp
2027 ;; construct. This is ordered with higher positions at the front of the list.
2028 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2030 (defvar c-state-nonlit-pos-cache-limit
1)
2031 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit
)
2032 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2033 ;; reduced by buffer changes, and increased by invocations of
2034 ;; `c-state-literal-at'.
2036 (defsubst c-state-pp-to-literal
(from to
)
2037 ;; Do a parse-partial-sexp from FROM to TO, returning the bounds of any
2038 ;; literal at TO as a cons, otherwise NIL.
2039 ;; FROM must not be in a literal, and the buffer should already be wide
2042 (let ((s (parse-partial-sexp from to
)))
2043 (when (or (nth 3 s
) (nth 4 s
)) ; in a string or comment
2044 (parse-partial-sexp (point) (point-max)
2048 'syntax-table
) ; stop at end of literal
2049 (cons (nth 8 s
) (point))))))
2051 (defun c-state-literal-at (here)
2052 ;; If position HERE is inside a literal, return (START . END), the
2053 ;; boundaries of the literal (which may be outside the accessible bit of the
2054 ;; buffer). Otherwise, return nil.
2056 ;; This function is almost the same as `c-literal-limits'. It differs in
2057 ;; that it is a lower level function, and that it rigourously follows the
2058 ;; syntax from BOB, whereas `c-literal-limits' uses a "local" safe position.
2060 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2061 ;; MAY NOT contain any positions within macros, since macros are frequently
2062 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2063 ;; We cannot rely on this mechanism whilst determining a cache pos since
2064 ;; this function is also called from outwith `c-parse-state'.
2068 (let ((c c-state-nonlit-pos-cache
)
2070 ;; Trim the cache to take account of buffer changes.
2071 (while (and c
(> (car c
) c-state-nonlit-pos-cache-limit
))
2073 (setq c-state-nonlit-pos-cache c
)
2075 (while (and c
(> (car c
) here
))
2077 (setq pos
(or (car c
) (point-min)))
2079 (while (<= (setq npos
(+ pos c-state-nonlit-pos-interval
))
2081 (setq lit
(c-state-pp-to-literal pos npos
))
2082 (setq pos
(or (cdr lit
) npos
)) ; end of literal containing npos.
2084 (when (and (c-beginning-of-macro) (/= (point) pos
))
2085 (c-syntactic-end-of-macro)
2086 (or (eobp) (forward-char))
2088 (setq c-state-nonlit-pos-cache
(cons pos c-state-nonlit-pos-cache
)))
2090 (if (> pos c-state-nonlit-pos-cache-limit
)
2091 (setq c-state-nonlit-pos-cache-limit pos
))
2093 (setq lit
(c-state-pp-to-literal pos here
)))
2096 (defsubst c-state-lit-beg
(pos)
2097 ;; Return the start of the literal containing POS, or POS itself.
2098 (or (car (c-state-literal-at pos
))
2101 (defsubst c-state-cache-non-literal-place
(pos state
)
2102 ;; Return a position outside of a string/comment at or before POS.
2103 ;; STATE is the parse-partial-sexp state at POS.
2104 (if (or (nth 3 state
) ; in a string?
2105 (nth 4 state
)) ; in a comment?
2110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2111 ;; Stuff to do with point-min, and coping with any literal there.
2112 (defvar c-state-point-min
1)
2113 (make-variable-buffer-local 'c-state-point-min
)
2114 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2115 ;; narrowing is likely to affect the parens that are visible before the point.
2117 (defvar c-state-point-min-lit-type nil
)
2118 (make-variable-buffer-local 'c-state-point-min-lit-type
)
2119 (defvar c-state-point-min-lit-start nil
)
2120 (make-variable-buffer-local 'c-state-point-min-lit-start
)
2121 ;; These two variables define the literal, if any, containing point-min.
2122 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2123 ;; literal. If there's no literal there, they're both nil.
2125 (defvar c-state-min-scan-pos
1)
2126 (make-variable-buffer-local 'c-state-min-scan-pos
)
2127 ;; This is the earliest buffer-pos from which scanning can be done. It is
2128 ;; either the end of the literal containing point-min, or point-min itself.
2129 ;; It becomes nil if the buffer is changed earlier than this point.
2130 (defun c-state-get-min-scan-pos ()
2131 ;; Return the lowest valid scanning pos. This will be the end of the
2132 ;; literal enclosing point-min, or point-min itself.
2133 (or c-state-min-scan-pos
2137 (goto-char c-state-point-min-lit-start
)
2138 (if (eq c-state-point-min-lit-type
'string
)
2140 (forward-comment 1))
2141 (setq c-state-min-scan-pos
(point))))))
2143 (defun c-state-mark-point-min-literal ()
2144 ;; Determine the properties of any literal containing POINT-MIN, setting the
2145 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2146 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2147 (let ((p-min (point-min))
2151 (setq lit
(c-state-literal-at p-min
))
2153 (setq c-state-point-min-lit-type
2155 (goto-char (car lit
))
2157 ((looking-at c-block-comment-start-regexp
) 'c
)
2158 ((looking-at c-line-comment-starter
) 'c
++)
2160 c-state-point-min-lit-start
(car lit
)
2161 c-state-min-scan-pos
(cdr lit
))
2162 (setq c-state-point-min-lit-type nil
2163 c-state-point-min-lit-start nil
2164 c-state-min-scan-pos p-min
)))))
2167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2168 ;; A variable which signals a brace dessert - helpful for reducing the number
2169 ;; of fruitless backward scans.
2170 (defvar c-state-brace-pair-desert nil
)
2171 (make-variable-buffer-local 'c-state-brace-pair-desert
)
2172 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2173 ;; that defun has searched backwards for a brace pair and not found one. Its
2174 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2175 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2176 ;; nil when at top level) and FROM is where the backward search started. It
2177 ;; is reset to nil in `c-invalidate-state-cache'.
2180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2181 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2182 ;; list of like structure.
2183 (defmacro c-state-cache-top-lparen
(&optional cache
)
2184 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2185 ;; (default `c-state-cache') (or nil).
2186 (let ((cash (or cache
'c-state-cache
)))
2187 `(if (consp (car ,cash
))
2191 (defmacro c-state-cache-top-paren
(&optional cache
)
2192 ;; Return the address of the latest brace/bracket/paren (whether left or
2193 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2194 (let ((cash (or cache
'c-state-cache
)))
2195 `(if (consp (car ,cash
))
2199 (defmacro c-state-cache-after-top-paren
(&optional cache
)
2200 ;; Return the position just after the latest brace/bracket/paren (whether
2201 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2202 (let ((cash (or cache
'c-state-cache
)))
2203 `(if (consp (car ,cash
))
2206 (1+ (car ,cash
))))))
2208 (defun c-get-cache-scan-pos (here)
2209 ;; From the state-cache, determine the buffer position from which we might
2210 ;; scan forward to HERE to update this cache. This position will be just
2211 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2212 ;; return the earliest position in the accessible region which isn't within
2213 ;; a literal. If the visible portion of the buffer is entirely within a
2214 ;; literal, return NIL.
2215 (let ((c c-state-cache
) elt
)
2216 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2218 (>= (c-state-cache-top-lparen c
) here
))
2224 (if (> (cdr elt
) here
)
2228 ((<= (c-state-get-min-scan-pos) here
)
2229 (c-state-get-min-scan-pos))
2232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2233 ;; Variables which keep track of preprocessor constructs.
2234 (defvar c-state-old-cpp-beg nil
)
2235 (make-variable-buffer-local 'c-state-old-cpp-beg
)
2236 (defvar c-state-old-cpp-end nil
)
2237 (make-variable-buffer-local 'c-state-old-cpp-end
)
2238 ;; These are the limits of the macro containing point at the previous call of
2239 ;; `c-parse-state', or nil.
2241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2242 ;; Defuns which analyse the buffer, yet don't change `c-state-cache'.
2243 (defun c-get-fallback-scan-pos (here)
2244 ;; Return a start position for building `c-state-cache' from
2245 ;; scratch. This will be at the top level, 2 defuns back.
2247 ;; Go back 2 bods, but ignore any bogus positions returned by
2248 ;; beginning-of-defun (i.e. open paren in column zero).
2251 (while (not (or (bobp) (zerop cnt
)))
2252 (c-beginning-of-defun-1) ; Pure elisp BOD.
2253 (if (eq (char-after) ?\
{)
2254 (setq cnt
(1- cnt
)))))
2257 (defun c-state-balance-parens-backwards (here- here
+ top
)
2258 ;; Return the position of the opening paren/brace/bracket before HERE- which
2259 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2260 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2262 ;; ............................................
2264 ;; ( [ ( .........#macro.. ) ( ) ] )
2267 ;; return HERE- HERE+ TOP
2269 ;; If there aren't enough opening paren/brace/brackets, return the position
2270 ;; of the outermost one found, or HERE- if there are none. If there are no
2271 ;; closeing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2272 ;; must not be inside literals. Only the accessible portion of the buffer
2275 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2276 ;; `here'. Go round the next loop each time we pass over such a ")". These
2277 ;; probably match "("s before `here-'.
2278 (let (pos pa ren
+1 lonely-rens
)
2281 (narrow-to-region (point-min) top
) ; This can move point, sometimes.
2285 (setq ren
+1 (scan-lists pos
1 1)) ; might signal
2286 (setq lonely-rens
(cons ren
+1 lonely-rens
)
2289 ;; PART 2: Scan back before `here-' searching for the "("s
2290 ;; matching/mismatching the ")"s found above. We only need to direct the
2291 ;; caller to scan when we've encountered unmatched right parens.
2296 (and lonely-rens
; actual values aren't used.
2297 (setq pa
(scan-lists pos -
1 1)))
2299 (setq lonely-rens
(cdr lonely-rens
)))))
2302 (defun c-parse-state-get-strategy (here good-pos
)
2303 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2304 ;; to minimise the amount of scanning. HERE is the pertinent position in
2305 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2306 ;; its head trimmed) is known to be good, or nil if there is no such
2309 ;; The return value is a list, one of the following:
2311 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2312 ;; which is not less than CACHE-POS.
2313 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2314 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2316 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2317 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2319 (let ((cache-pos (c-get-cache-scan-pos here
)) ; highest position below HERE in cache (or 1)
2320 BOD-pos
; position of 2nd BOD before HERE.
2321 strategy
; 'forward, 'backward, 'BOD, or 'IN-LIT.
2323 how-far
) ; putative scanning distance.
2324 (setq good-pos
(or good-pos
(c-state-get-min-scan-pos)))
2326 ((< here
(c-state-get-min-scan-pos))
2327 (setq strategy
'IN-LIT
2332 (setq strategy
'forward
2333 start-point
(max good-pos cache-pos
)
2334 how-far
(- here start-point
)))
2335 ((< (- good-pos here
) (- here cache-pos
)) ; FIXME!!! ; apply some sort of weighting.
2336 (setq strategy
'backward
2337 how-far
(- good-pos here
)))
2339 (setq strategy
'forward
2340 how-far
(- here cache-pos
)
2341 start-point cache-pos
)))
2343 ;; Might we be better off starting from the top level, two defuns back,
2345 (when (> how-far c-state-cache-too-far
)
2346 (setq BOD-pos
(c-get-fallback-scan-pos here
)) ; somewhat EXPENSIVE!!!
2347 (if (< (- here BOD-pos
) how-far
)
2349 start-point BOD-pos
)))
2353 (and (memq strategy
'(forward backward
)) cache-pos
)
2354 (and (memq strategy
'(forward BOD
)) start-point
))))
2357 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2358 ;; Routines which change `c-state-cache' and associated values.
2359 (defun c-renarrow-state-cache ()
2360 ;; The region (more precisely, point-min) has changed since we
2361 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2362 (if (< (point-min) c-state-point-min
)
2363 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2364 ;; It would be possible to do a better job here and recalculate the top
2367 (c-state-mark-point-min-literal)
2368 (setq c-state-cache nil
2369 c-state-cache-good-pos c-state-min-scan-pos
2370 c-state-brace-pair-desert nil
))
2372 ;; point-min has MOVED FORWARD.
2374 ;; Is the new point-min inside a (different) literal?
2375 (unless (and c-state-point-min-lit-start
; at prev. point-min
2376 (< (point-min) (c-state-get-min-scan-pos)))
2377 (c-state-mark-point-min-literal))
2379 ;; Cut off a bit of the tail from `c-state-cache'.
2380 (let ((ptr (cons nil c-state-cache
))
2382 (while (and (setq pa
(c-state-cache-top-lparen (cdr ptr
)))
2383 (>= pa
(point-min)))
2384 (setq ptr
(cdr ptr
)))
2387 (if (eq (cdr ptr
) c-state-cache
)
2388 (setq c-state-cache nil
2389 c-state-cache-good-pos c-state-min-scan-pos
)
2391 (setq c-state-cache-good-pos
(1+ (c-state-cache-top-lparen))))
2394 (setq c-state-point-min
(point-min)))
2396 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim
)
2397 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2398 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2399 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2400 ;; the highest brace pair whose "}" is below UPPER-LIM.
2402 ;; Return non-nil when this has been done.
2404 ;; This routine should be fast. Since it can get called a LOT, we maintain
2405 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2406 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2409 (let ((bra from
) ce
; Positions of "{" and "}".
2411 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2412 (macro-start-or-from
2413 (progn (goto-char from
)
2414 (c-beginning-of-macro)
2416 (or upper-lim
(setq upper-lim from
))
2418 ;; If we're essentially repeating a fruitless search, just give up.
2419 (unless (and c-state-brace-pair-desert
2420 (eq cache-pos
(car c-state-brace-pair-desert
))
2421 (<= from
(cdr c-state-brace-pair-desert
)))
2422 ;; Only search what we absolutely need to:
2423 (if (and c-state-brace-pair-desert
2424 (> from
(cdr c-state-brace-pair-desert
)))
2425 (narrow-to-region (cdr c-state-brace-pair-desert
) (point-max)))
2427 ;; In the next pair of nested loops, the inner one moves back past a
2428 ;; pair of (mis-)matching parens or brackets; the outer one moves
2429 ;; back over a sequence of unmatched close brace/paren/bracket each
2435 (and (setq ce
(scan-lists bra -
1 -
1)) ; back past )/]/}; might signal
2436 (setq bra
(scan-lists ce -
1 1)) ; back past (/[/{; might signal
2437 (or (> ce upper-lim
)
2438 (not (eq (char-after bra
) ?\
{))
2439 (and (goto-char bra
)
2440 (c-beginning-of-macro)
2441 (< (point) macro-start-or-from
))))))
2442 (and ce
(< ce bra
)))
2443 (setq bra ce
)) ; If we just backed over an unbalanced closing
2446 (if (and ce
(< bra ce
) (eq (char-after bra
) ?\
{))
2447 ;; We've found the desired brace-pair.
2449 (setq new-cons
(cons bra
(1+ ce
)))
2451 ((consp (car c-state-cache
))
2452 (setcar c-state-cache new-cons
))
2453 ((and (numberp (car c-state-cache
)) ; probably never happens
2454 (< ce
(car c-state-cache
)))
2455 (setcdr c-state-cache
2456 (cons new-cons
(cdr c-state-cache
))))
2457 (t (setq c-state-cache
(cons new-cons c-state-cache
)))))
2459 ;; We haven't found a brace pair. Record this.
2460 (setq c-state-brace-pair-desert
(cons cache-pos from
))))))))
2462 (defsubst c-state-push-any-brace-pair
(bra+1 macro-start-or-here
)
2463 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2464 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2465 ;; "push" "a" brace pair onto `c-state-cache'.
2467 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2468 ;; otherwise push it normally.
2470 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2471 ;; latter is inside a macro, not being a macro containing
2472 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2473 ;; base pair. This latter case is assumed to be rare.
2475 ;; Note: POINT is not preserved in this routine.
2477 (if (or (> bra
+1 macro-start-or-here
)
2478 (progn (goto-char bra
+1)
2479 (not (c-beginning-of-macro))))
2481 (cons (cons (1- bra
+1)
2482 (scan-lists bra
+1 1 1))
2483 (if (consp (car c-state-cache
))
2486 ;; N.B. This defsubst codes one method for the simple, normal case,
2487 ;; and a more sophisticated, slower way for the general case. Don't
2488 ;; eliminate this defsubst - it's a speed optimisation.
2489 (c-append-lower-brace-pair-to-state-cache (1- bra
+1)))))
2491 (defun c-append-to-state-cache (from)
2492 ;; Scan the buffer from FROM to (point-max), adding elements into
2493 ;; `c-state-cache' for braces etc. Return a candidate for
2494 ;; `c-state-cache-good-pos'.
2496 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2497 ;; any. Typically, it is immediately after it. It must not be inside a
2499 (let ((here-bol (c-point 'bol
(point-max)))
2500 (macro-start-or-here
2501 (save-excursion (goto-char (point-max))
2502 (if (c-beginning-of-macro)
2505 pa
+1 ; pos just after an opening PAren (or brace).
2506 (ren+1 from
) ; usually a pos just after an closing paREN etc.
2507 ; Is actually the pos. to scan for a (/{/[ from,
2508 ; which sometimes is after a silly )/}/].
2509 paren
+1 ; Pos after some opening or closing paren.
2510 paren
+1s
; A list of `paren+1's; used to determine a
2512 bra
+1 ce
+1 ; just after L/R bra-ces.
2513 bra
+1s
; list of OLD values of bra+1.
2514 mstart
) ; start of a macro.
2517 ;; Each time round the following loop, we enter a succesively deeper
2518 ;; level of brace/paren nesting. (Except sometimes we "continue at
2519 ;; the existing level".) `pa+1' is a pos inside an opening
2520 ;; brace/paren/bracket, usually just after it.
2523 ;; Each time round the next loop moves forward over an opening then
2524 ;; a closing brace/bracket/paren. This loop is white hot, so it
2525 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2526 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2527 ;; call of `scan-lists' signals an error, which happens when there
2528 ;; are no more b/b/p's to scan.
2531 (setq pa
+1 (scan-lists ren
+1 1 -
1) ; Into (/{/[; might signal
2532 paren
+1s
(cons pa
+1 paren
+1s
))
2533 (setq ren
+1 (scan-lists pa
+1 1 1)) ; Out of )/}/]; might signal
2534 (if (and (eq (char-before pa
+1) ?
{)) ; Check for a macro later.
2536 (setcar paren
+1s ren
+1)))
2538 (if (and pa
+1 (> pa
+1 ren
+1))
2539 ;; We've just entered a deeper nesting level.
2541 ;; Insert the brace pair (if present) and the single open
2542 ;; paren/brace/bracket into `c-state-cache' It cannot be
2543 ;; inside a macro, except one around point, because of what
2544 ;; `c-neutralize-syntax-in-CPP' has done.
2545 (c-state-push-any-brace-pair bra
+1 macro-start-or-here
)
2546 ;; Insert the opening brace/bracket/paren position.
2547 (setq c-state-cache
(cons (1- pa
+1) c-state-cache
))
2548 ;; Clear admin stuff for the next more nested part of the scan.
2549 (setq ren
+1 pa
+1 pa
+1 nil bra
+1 nil bra
+1s nil
)
2550 t
) ; Carry on the loop
2552 ;; All open p/b/b's at this nesting level, if any, have probably
2553 ;; been closed by matching/mismatching ones. We're probably
2554 ;; finished - we just need to check for having found an
2555 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2556 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2557 (c-safe (setq ren
+1 (scan-lists ren
+1 1 1)))))) ; acts as loop control.
2559 ;; Record the final, innermost, brace-pair if there is one.
2560 (c-state-push-any-brace-pair bra
+1 macro-start-or-here
)
2562 ;; Determine a good pos
2563 (while (and (setq paren
+1 (car paren
+1s
))
2564 (> (if (> paren
+1 macro-start-or-here
)
2567 (setq mstart
(and (c-beginning-of-macro)
2569 (or mstart paren
+1))
2571 (setq paren
+1s
(cdr paren
+1s
)))
2573 ((and paren
+1 mstart
)
2574 (min paren
+1 mstart
))
2578 (defun c-remove-stale-state-cache (good-pos pps-point
)
2579 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2580 ;; not be in it when it is amended for position (point-max).
2581 ;; Additionally, the "outermost" open-brace entry before (point-max)
2582 ;; will be converted to a cons if the matching close-brace is scanned.
2584 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2585 ;; parens/braces/brackets between GOOD-POS and (point-max).
2587 ;; As a second thing, calculate the result of parse-partial-sexp at
2588 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2589 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2590 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2591 ;; needs to be FAST).
2593 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2594 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2595 ;; to be good (we aim for this to be as high as possible);
2596 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2597 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2598 ;; position to scan backwards from.
2599 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2601 (narrow-to-region 1 (point-max))
2603 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2605 (goto-char (point-max))
2606 (and (c-beginning-of-macro)
2608 (good-pos-actual-macro-start ; Start of macro containing good-pos
2610 (and (< good-pos
(point-max))
2612 (goto-char good-pos
)
2613 (and (c-beginning-of-macro)
2615 (good-pos-actual-macro-end ; End of this macro, (maybe
2616 ; (point-max)), or nil.
2617 (and good-pos-actual-macro-start
2619 (goto-char good-pos-actual-macro-start
)
2622 pps-state
; Will be 9 or 10 elements long.
2624 upper-lim
; ,beyond which `c-state-cache' entries are removed
2626 pair-beg pps-point-state target-depth
)
2628 ;; Remove entries beyond (point-max). Also remove any entries inside
2629 ;; a macro, unless (point-max) is in the same macro.
2631 (if (or (null c-state-old-cpp-beg
)
2632 (and (> (point-max) c-state-old-cpp-beg
)
2633 (< (point-max) c-state-old-cpp-end
)))
2635 (min (point-max) c-state-old-cpp-beg
)))
2636 (while (and c-state-cache
(>= (c-state-cache-top-lparen) upper-lim
))
2637 (setq c-state-cache
(cdr c-state-cache
)))
2638 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2639 ;; RBrace and indicate we'll need to search backwards for a previous
2641 (when (and c-state-cache
2642 (consp (car c-state-cache
))
2643 (> (cdar c-state-cache
) upper-lim
))
2644 (setcar c-state-cache
(caar c-state-cache
))
2645 (setq scan-back-pos
(car c-state-cache
)))
2647 ;; The next loop jumps forward out of a nested level of parens each
2648 ;; time round; the corresponding elements in `c-state-cache' are
2649 ;; removed. `pos' is just after the brace-pair or the open paren at
2650 ;; (car c-state-cache). There can be no open parens/braces/brackets
2651 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2652 ;; due to the interface spec to this function.
2653 (setq pos
(if (and good-pos-actual-macro-end
2654 (not (eq good-pos-actual-macro-start
2656 (1+ good-pos-actual-macro-end
) ; get outside the macro as
2657 ; marked by a `category' text property.
2660 (while (and c-state-cache
2661 (< (point) (point-max)))
2663 ((null pps-state
) ; first time through
2664 (setq target-depth -
1))
2665 ((eq (car pps-state
) target-depth
) ; found closing ),},]
2666 (setq target-depth
(1- (car pps-state
))))
2667 ;; Do nothing when we've merely reached pps-point.
2673 (point) (if (< (point) pps-point
) pps-point
(point-max))
2677 (if (= (point) pps-point
)
2678 (setq pps-point-state pps-state
))
2680 (when (eq (car pps-state
) target-depth
)
2681 (setq pos
(point)) ; POS is now just after an R-paren/brace.
2683 ((and (consp (car c-state-cache
))
2684 (eq (point) (cdar c-state-cache
)))
2685 ;; We've just moved out of the paren pair containing the brace-pair
2686 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2687 ;; and is potentially where the open brace of a cons in
2688 ;; c-state-cache will be.
2689 (setq pair-beg
(car-safe (cdr c-state-cache
))
2690 c-state-cache
(cdr-safe (cdr c-state-cache
)))) ; remove {}pair + containing Lparen.
2691 ((numberp (car c-state-cache
))
2692 (setq pair-beg
(car c-state-cache
)
2693 c-state-cache
(cdr c-state-cache
))) ; remove this
2695 ((numberp (cadr c-state-cache
))
2696 (setq pair-beg
(cadr c-state-cache
)
2697 c-state-cache
(cddr c-state-cache
))) ; Remove a paren pair
2698 ; together with enclosed brace pair.
2699 ;; (t nil) ; Ignore an unmated Rparen.
2702 (if (< (point) pps-point
)
2703 (setq pps-state
(parse-partial-sexp (point) pps-point
2704 nil nil
; TARGETDEPTH, STOPBEFORE
2707 ;; If the last paren pair we moved out of was actually a brace pair,
2708 ;; insert it into `c-state-cache'.
2709 (when (and pair-beg
(eq (char-after pair-beg
) ?
{))
2710 (if (consp (car-safe c-state-cache
))
2711 (setq c-state-cache
(cdr c-state-cache
)))
2712 (setq c-state-cache
(cons (cons pair-beg pos
)
2715 (list pos scan-back-pos pps-state
)))))
2717 (defun c-remove-stale-state-cache-backwards (here cache-pos
)
2718 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2719 ;; buffer, and inform the caller of the scenario detected.
2721 ;; HERE is the position we're setting `c-state-cache' for.
2722 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2723 ;; before HERE, or a position at or near point-min which isn't in a
2726 ;; This function must only be called only when (> `c-state-cache-good-pos'
2727 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2728 ;; optimised to eliminate (or minimise) scanning between these two
2731 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2732 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2733 ;; could become so after missing elements are inserted into
2734 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2735 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2736 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2737 ;; before `here''s line, or the start of the literal containing it.
2738 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2739 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2740 ;; to scan backwards from.
2741 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2742 ;; POS and HERE which aren't recorded in `c-state-cache'.
2744 ;; The comments in this defun use "paren" to mean parenthesis or square
2745 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2747 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2749 ;; CP E here D C good
2750 (let ((pos c-state-cache-good-pos
)
2751 pa ren
; positions of "(" and ")"
2752 dropped-cons
; whether the last element dropped from `c-state-cache'
2753 ; was a cons (representing a brace-pair)
2754 good-pos
; see above.
2755 lit
; (START . END) of a literal containing some point.
2756 here-lit-start here-lit-end
; bounds of literal containing `here'
2758 here- here
+ ; start/end of macro around HERE, or HERE
2759 (here-bol (c-point 'bol here
))
2760 (too-far-back (max (- here c-state-cache-too-far
) 1)))
2762 ;; Remove completely irrelevant entries from `c-state-cache'.
2763 (while (and c-state-cache
2764 (>= (setq pa
(c-state-cache-top-lparen)) here
))
2765 (setq dropped-cons
(consp (car c-state-cache
)))
2766 (setq c-state-cache
(cdr c-state-cache
))
2768 ;; At this stage, (> pos here);
2769 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2772 ((and (consp (car c-state-cache
))
2773 (> (cdar c-state-cache
) here
))
2774 ;; CASE 1: The top of the cache is a brace pair which now encloses
2775 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2776 ;; knowledge of what's inside these braces, we have no alternative but
2777 ;; to direct the caller to scan the buffer from the opening brace.
2778 (setq pos
(caar c-state-cache
))
2779 (setcar c-state-cache pos
)
2780 (list (1+ pos
) pos t
)) ; return value. We've just converted a brace pair
2781 ; entry into a { entry, so the caller needs to
2782 ; search for a brace pair before the {.
2784 ;; `here' might be inside a literal. Check for this.
2786 (setq lit
(c-state-literal-at here
)
2787 here-lit-start
(or (car lit
) here
)
2788 here-lit-end
(or (cdr lit
) here
))
2789 ;; Has `here' just "newly entered" a macro?
2791 (goto-char here-lit-start
)
2792 (if (and (c-beginning-of-macro)
2793 (or (null c-state-old-cpp-beg
)
2794 (not (= (point) c-state-old-cpp-beg
))))
2796 (setq here-
(point))
2798 (setq here
+ (point)))
2799 (setq here- here-lit-start
2800 here
+ here-lit-end
)))
2802 ;; `here' might be nested inside any depth of parens (or brackets but
2803 ;; not braces). Scan backwards to find the outermost such opening
2804 ;; paren, if there is one. This will be the scan position to return.
2806 (narrow-to-region cache-pos
(point-max))
2807 (setq pos
(c-state-balance-parens-backwards here- here
+ pos
)))
2808 nil
)) ; for the cond
2810 ((< pos here-lit-start
)
2811 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2812 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2813 ;; a brace pair preceding this, it will already be in `c-state-cache',
2814 ;; unless there was a brace pair after it, i.e. there'll only be one to
2815 ;; scan for if we've just deleted one.
2816 (list pos
(and dropped-cons pos
) t
)) ; Return value.
2818 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2819 ;; Further forward scanning isn't needed, but we still need to find a
2820 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2823 (narrow-to-region here-bol
(point-max))
2824 (setq pos here-lit-start
)
2825 (c-safe (while (setq pa
(scan-lists pos -
1 1))
2826 (setq pos pa
)))) ; might signal
2827 nil
)) ; for the cond
2829 ((setq ren
(c-safe-scan-lists pos -
1 -
1 too-far-back
))
2830 ;; CASE 3: After a }/)/] before `here''s BOL.
2831 (list (1+ ren
) (and dropped-cons pos
) nil
)) ; Return value
2834 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2835 ;; literal containing it.
2836 (setq good-pos
(c-state-lit-beg (c-point 'bopl here-bol
)))
2837 (list good-pos
(and dropped-cons good-pos
) nil
)))))
2840 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2841 ;; Externally visible routines.
2843 (defun c-state-cache-init ()
2844 (setq c-state-cache nil
2845 c-state-cache-good-pos
1
2846 c-state-nonlit-pos-cache nil
2847 c-state-nonlit-pos-cache-limit
1
2848 c-state-brace-pair-desert nil
2850 c-state-point-min-lit-type nil
2851 c-state-point-min-lit-start nil
2852 c-state-min-scan-pos
1
2853 c-state-old-cpp-beg nil
2854 c-state-old-cpp-end nil
)
2855 (c-state-mark-point-min-literal))
2857 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2858 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
2859 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
2860 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
2861 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
2862 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
2863 ;; (defun c-state-dump ()
2864 ;; ;; For debugging.
2867 ;; (c-sc-qde c-state-cache)
2868 ;; (c-sc-de c-state-cache-good-pos)
2869 ;; (c-sc-qde c-state-nonlit-pos-cache)
2870 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
2871 ;; (c-sc-qde c-state-brace-pair-desert)
2872 ;; (c-sc-de c-state-point-min)
2873 ;; (c-sc-de c-state-point-min-lit-type)
2874 ;; (c-sc-de c-state-point-min-lit-start)
2875 ;; (c-sc-de c-state-min-scan-pos)
2876 ;; (c-sc-de c-state-old-cpp-beg)
2877 ;; (c-sc-de c-state-old-cpp-end)))
2878 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2880 (defun c-invalidate-state-cache-1 (here)
2881 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2882 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2883 ;; left in a consistent state.
2885 ;; This is much like `c-whack-state-after', but it never changes a paren
2886 ;; pair element into an open paren element. Doing that would mean that the
2887 ;; new open paren wouldn't have the required preceding paren pair element.
2889 ;; This function is called from c-after-change.
2891 ;; The cache of non-literals:
2892 (if (< here c-state-nonlit-pos-cache-limit
)
2893 (setq c-state-nonlit-pos-cache-limit here
))
2896 ;; Case 1: if `here' is in a literal containing point-min, everything
2897 ;; becomes (or is already) nil.
2898 (if (or (null c-state-cache-good-pos
)
2899 (< here
(c-state-get-min-scan-pos)))
2900 (setq c-state-cache nil
2901 c-state-cache-good-pos nil
2902 c-state-min-scan-pos nil
)
2904 ;;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value below
2905 ;;; `here'. To maintain its consistency, we may need to insert a new brace
2907 (let ((here-bol (c-point 'bol here
))
2908 too-high-pa
; recorded {/(/[ next above here, or nil.
2909 dropped-cons
; was the last removed element a brace pair?
2911 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
2912 (while (and c-state-cache
2913 (>= (setq pa
(c-state-cache-top-paren)) here
))
2914 (setq dropped-cons
(consp (car c-state-cache
))
2915 too-high-pa
(c-state-cache-top-lparen)
2916 c-state-cache
(cdr c-state-cache
)))
2918 ;; Do we need to add in an earlier brace pair, having lopped one off?
2919 (if (and dropped-cons
2920 (< too-high-pa
(+ here c-state-cache-too-far
)))
2921 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol
))
2922 (setq c-state-cache-good-pos
(or (c-state-cache-after-top-paren)
2923 (c-state-get-min-scan-pos)))))
2925 ;; The brace-pair desert marker:
2926 (when (car c-state-brace-pair-desert
)
2927 (if (< here
(car c-state-brace-pair-desert
))
2928 (setq c-state-brace-pair-desert nil
)
2929 (if (< here
(cdr c-state-brace-pair-desert
))
2930 (setcdr c-state-brace-pair-desert here
)))))
2932 (defun c-parse-state-1 ()
2933 ;; Find and record all noteworthy parens between some good point earlier in
2934 ;; the file and point. That good point is at least the beginning of the
2935 ;; top-level construct we are in, or the beginning of the preceding
2936 ;; top-level construct if we aren't in one.
2938 ;; The returned value is a list of the noteworthy parens with the last one
2939 ;; first. If an element in the list is an integer, it's the position of an
2940 ;; open paren (of any type) which has not been closed before the point. If
2941 ;; an element is a cons, it gives the position of a closed BRACE paren
2942 ;; pair[*]; the car is the start brace position and the cdr is the position
2943 ;; following the closing brace. Only the last closed brace paren pair
2944 ;; before each open paren and before the point is recorded, and thus the
2945 ;; state never contains two cons elements in succession. When a close brace
2946 ;; has no matching open brace (e.g., the matching brace is outside the
2947 ;; visible region), it is not represented in the returned value.
2949 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
2950 ;; This defun explicitly treats mismatching parens/braces/brackets as
2951 ;; matching. It is the open brace which makes it a "brace" pair.
2953 ;; If POINT is within a macro, open parens and brace pairs within
2954 ;; THIS macro MIGHT be recorded. This depends on whether their
2955 ;; syntactic properties have been suppressed by
2956 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
2958 ;; Currently no characters which are given paren syntax with the
2959 ;; syntax-table property are recorded, i.e. angle bracket arglist
2960 ;; parens are never present here. Note that this might change.
2962 ;; BUG: This function doesn't cope entirely well with unbalanced
2963 ;; parens in macros. (2008-12-11: this has probably been resolved
2964 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
2965 ;; following case the brace before the macro isn't balanced with the
2972 ;; Note to maintainers: this function DOES get called with point
2973 ;; within comments and strings, so don't assume it doesn't!
2975 ;; This function might do hidden buffer changes.
2976 (let* ((here (point))
2977 (here-bopl (c-point 'bopl
))
2978 strategy
; 'forward, 'backward etc..
2979 ;; Candidate positions to start scanning from:
2980 cache-pos
; highest position below HERE already existing in
2986 scan-backward-pos scan-forward-p
) ; used for 'backward.
2987 ;; If POINT-MIN has changed, adjust the cache
2988 (unless (= (point-min) c-state-point-min
)
2989 (c-renarrow-state-cache))
2992 (setq res
(c-parse-state-get-strategy here c-state-cache-good-pos
)
2994 cache-pos
(cadr res
)
2995 start-point
(nth 2 res
))
2997 (when (eq strategy
'BOD
)
2998 (setq c-state-cache nil
2999 c-state-cache-good-pos start-point
))
3004 ((memq strategy
'(forward BOD
))
3005 (narrow-to-region (point-min) here
)
3006 (setq res
(c-remove-stale-state-cache start-point here-bopl
))
3007 (setq cache-pos
(car res
)
3008 scan-backward-pos
(cadr res
)
3009 bopl-state
(car (cddr res
))) ; will be nil if (< here-bopl
3011 (if scan-backward-pos
3012 (c-append-lower-brace-pair-to-state-cache scan-backward-pos
))
3014 (c-append-to-state-cache cache-pos
))
3015 (setq c-state-cache-good-pos
3017 (< good-pos
(- here c-state-cache-too-far
)))
3018 (c-state-cache-non-literal-place here-bopl bopl-state
)
3021 ((eq strategy
'backward
)
3022 (setq res
(c-remove-stale-state-cache-backwards here cache-pos
)
3024 scan-backward-pos
(cadr res
)
3025 scan-forward-p
(car (cddr res
)))
3026 (if scan-backward-pos
3027 (c-append-lower-brace-pair-to-state-cache
3029 (setq c-state-cache-good-pos
3031 (progn (narrow-to-region (point-min) here
)
3032 (c-append-to-state-cache good-pos
))
3034 (c-get-cache-scan-pos good-pos
))))
3036 (t ; (eq strategy 'IN-LIT)
3037 (setq c-state-cache nil
3038 c-state-cache-good-pos nil
)))))
3042 (defun c-invalidate-state-cache (here)
3043 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3045 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3046 ;; of all parens in preprocessor constructs, except for any such construct
3047 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3048 ;; worrying further about macros and template delimiters.
3049 (c-with-<-
>-as-parens-suppressed
3050 (if (and c-state-old-cpp-beg
3051 (< c-state-old-cpp-beg here
))
3052 (c-with-all-but-one-cpps-commented-out
3054 (min c-state-old-cpp-end here
)
3055 (c-invalidate-state-cache-1 here
))
3056 (c-with-cpps-commented-out
3057 (c-invalidate-state-cache-1 here
)))))
3059 (defun c-parse-state ()
3060 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3061 ;; description of the functionality and return value.
3063 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3064 ;; of all parens in preprocessor constructs, except for any such construct
3065 ;; containing point. We can then call `c-parse-state-1' without worrying
3066 ;; further about macros and template delimiters.
3067 (let (here-cpp-beg here-cpp-end
)
3069 (when (c-beginning-of-macro)
3070 (setq here-cpp-beg
(point))
3072 (> (setq here-cpp-end
(c-syntactic-end-of-macro))
3074 (setq here-cpp-beg nil here-cpp-end nil
))))
3075 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3078 (c-with-<-
>-as-parens-suppressed
3079 (if (and here-cpp-beg
(> here-cpp-end here-cpp-beg
))
3080 (c-with-all-but-one-cpps-commented-out
3081 here-cpp-beg here-cpp-end
3083 (c-with-cpps-commented-out
3084 (c-parse-state-1))))
3085 (setq c-state-old-cpp-beg
(and here-cpp-beg
(copy-marker here-cpp-beg t
))
3086 c-state-old-cpp-end
(and here-cpp-end
(copy-marker here-cpp-end t
)))
3089 ;; Debug tool to catch cache inconsistencies. This is called from
3091 (defvar c-debug-parse-state nil
)
3092 (unless (fboundp 'c-real-parse-state
)
3093 (fset 'c-real-parse-state
(symbol-function 'c-parse-state
)))
3094 (cc-bytecomp-defun c-real-parse-state)
3095 (defun c-debug-parse-state ()
3096 (let ((here (point)) (res1 (c-real-parse-state)) res2
)
3097 (let ((c-state-cache nil
)
3098 (c-state-cache-good-pos 1)
3099 (c-state-nonlit-pos-cache nil
)
3100 (c-state-nonlit-pos-cache-limit 1)
3101 (c-state-brace-pair-desert nil
)
3102 (c-state-point-min 1)
3103 (c-state-point-min-lit-type nil
)
3104 (c-state-point-min-lit-start nil
)
3105 (c-state-min-scan-pos 1)
3106 (c-state-old-cpp-beg nil
)
3107 (c-state-old-cpp-end nil
))
3108 (setq res2
(c-real-parse-state)))
3109 (unless (equal res1 res2
)
3110 ;; The cache can actually go further back due to the ad-hoc way
3111 ;; the first paren is found, so try to whack off a bit of its
3112 ;; start before complaining.
3114 (goto-char (or (c-least-enclosing-brace res2
) (point)))
3115 (c-beginning-of-defun-1)
3116 (while (not (or (bobp) (eq (char-after) ?
{)))
3117 (c-beginning-of-defun-1))
3118 (unless (equal (c-whack-state-before (point) res1
) res2
)
3119 (message (concat "c-parse-state inconsistency at %s: "
3120 "using cache: %s, from scratch: %s")
3124 (defun c-toggle-parse-state-debug (&optional arg
)
3126 (setq c-debug-parse-state
(c-calculate-state arg c-debug-parse-state
))
3127 (fset 'c-parse-state
(symbol-function (if c-debug-parse-state
3128 'c-debug-parse-state
3129 'c-real-parse-state
)))
3130 (c-keep-region-active))
3131 (when c-debug-parse-state
3132 (c-toggle-parse-state-debug 1))
3135 (defun c-whack-state-before (bufpos paren-state
)
3136 ;; Whack off any state information from PAREN-STATE which lies
3137 ;; before BUFPOS. Not destructive on PAREN-STATE.
3138 (let* ((newstate (list nil
))
3142 (setq car
(car paren-state
)
3143 paren-state
(cdr paren-state
))
3144 (if (< (if (consp car
) (car car
) car
) bufpos
)
3145 (setq paren-state nil
)
3146 (setcdr ptr
(list car
))
3147 (setq ptr
(cdr ptr
))))
3150 (defun c-whack-state-after (bufpos paren-state
)
3151 ;; Whack off any state information from PAREN-STATE which lies at or
3152 ;; after BUFPOS. Not destructive on PAREN-STATE.
3155 (let ((car (car paren-state
)))
3157 ;; just check the car, because in a balanced brace
3158 ;; expression, it must be impossible for the corresponding
3159 ;; close brace to be before point, but the open brace to
3161 (if (<= bufpos
(car car
))
3163 (if (< bufpos
(cdr car
))
3164 ;; its possible that the open brace is before
3165 ;; bufpos, but the close brace is after. In that
3166 ;; case, convert this to a non-cons element. The
3167 ;; rest of the state is before bufpos, so we're
3169 (throw 'done
(cons (car car
) (cdr paren-state
)))
3170 ;; we know that both the open and close braces are
3171 ;; before bufpos, so we also know that everything else
3172 ;; on state is before bufpos.
3173 (throw 'done paren-state
)))
3176 ;; it's before bufpos, so everything else should too.
3177 (throw 'done paren-state
)))
3178 (setq paren-state
(cdr paren-state
)))
3181 (defun c-most-enclosing-brace (paren-state &optional bufpos
)
3182 ;; Return the bufpos of the innermost enclosing open paren before
3183 ;; bufpos, or nil if none was found.
3185 (or bufpos
(setq bufpos
134217727))
3187 (setq enclosingp
(car paren-state
)
3188 paren-state
(cdr paren-state
))
3189 (if (or (consp enclosingp
)
3190 (>= enclosingp bufpos
))
3191 (setq enclosingp nil
)
3192 (setq paren-state nil
)))
3195 (defun c-least-enclosing-brace (paren-state)
3196 ;; Return the bufpos of the outermost enclosing open paren, or nil
3197 ;; if none was found.
3200 (setq elem
(car paren-state
)
3201 paren-state
(cdr paren-state
))
3206 (defun c-safe-position (bufpos paren-state
)
3207 ;; Return the closest "safe" position recorded on PAREN-STATE that
3208 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3209 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3210 ;; find the closest limit before a given limit that might be nil.
3212 ;; A "safe" position is a position at or after a recorded open
3213 ;; paren, or after a recorded close paren. The returned position is
3214 ;; thus either the first position after a close brace, or the first
3215 ;; position after an enclosing paren, or at the enclosing paren in
3216 ;; case BUFPOS is immediately after it.
3221 (setq elem
(car paren-state
))
3223 (cond ((< (cdr elem
) bufpos
)
3224 (throw 'done
(cdr elem
)))
3225 ((< (car elem
) bufpos
)
3227 (throw 'done
(min (1+ (car elem
)) bufpos
))))
3229 ;; elem is the position at and not after the opening paren, so
3230 ;; we can go forward one more step unless it's equal to
3231 ;; bufpos. This is useful in some cases avoid an extra paren
3232 ;; level between the safe position and bufpos.
3233 (throw 'done
(min (1+ elem
) bufpos
))))
3234 (setq paren-state
(cdr paren-state
)))))))
3236 (defun c-beginning-of-syntax ()
3237 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3238 ;; goes to the closest previous point that is known to be outside
3239 ;; any string literal or comment. `c-state-cache' is used if it has
3240 ;; a position in the vicinity.
3241 (let* ((paren-state c-state-cache
)
3245 ;; Note: Similar code in `c-safe-position'. The
3246 ;; difference is that we accept a safe position at
3247 ;; the point and don't bother to go forward past open
3250 (setq elem
(car paren-state
))
3252 (cond ((<= (cdr elem
) (point))
3253 (throw 'done
(cdr elem
)))
3254 ((<= (car elem
) (point))
3255 (throw 'done
(car elem
))))
3256 (if (<= elem
(point))
3257 (throw 'done elem
)))
3258 (setq paren-state
(cdr paren-state
)))
3261 (if (> pos
(- (point) 4000))
3263 ;; The position is far back. Try `c-beginning-of-defun-1'
3264 ;; (although we can't be entirely sure it will go to a position
3265 ;; outside a comment or string in current emacsen). FIXME:
3266 ;; Consult `syntax-ppss' here.
3267 (c-beginning-of-defun-1)
3272 ;; Tools for scanning identifiers and other tokens.
3274 (defun c-on-identifier ()
3275 "Return non-nil if the point is on or directly after an identifier.
3276 Keywords are recognized and not considered identifiers. If an
3277 identifier is detected, the returned value is its starting position.
3278 If an identifier ends at the point and another begins at it \(can only
3279 happen in Pike) then the point for the preceding one is returned.
3281 Note that this function might do hidden buffer changes. See the
3282 comment at the start of cc-engine.el for more info."
3284 ;; FIXME: Shouldn't this function handle "operator" in C++?
3287 (skip-syntax-backward "w_")
3291 ;; Check for a normal (non-keyword) identifier.
3292 (and (looking-at c-symbol-start
)
3293 (not (looking-at c-keywords-regexp
))
3296 (when (c-major-mode-is 'pike-mode
)
3297 ;; Handle the `<operator> syntax in Pike.
3298 (let ((pos (point)))
3299 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3300 (and (if (< (skip-chars-backward "`") 0)
3303 (eq (char-after) ?\
`))
3304 (looking-at c-symbol-key
)
3305 (>= (match-end 0) pos
)
3308 ;; Handle the "operator +" syntax in C++.
3309 (when (and c-overloadable-operators-regexp
3310 (= (c-backward-token-2 0) 0))
3312 (cond ((and (looking-at c-overloadable-operators-regexp
)
3313 (or (not c-opt-op-identifier-prefix
)
3314 (and (= (c-backward-token-2 1) 0)
3315 (looking-at c-opt-op-identifier-prefix
))))
3319 (and c-opt-op-identifier-prefix
3320 (looking-at c-opt-op-identifier-prefix
)
3321 (= (c-forward-token-2 1) 0)
3322 (looking-at c-overloadable-operators-regexp
)))
3327 (defsubst c-simple-skip-symbol-backward
()
3328 ;; If the point is at the end of a symbol then skip backward to the
3329 ;; beginning of it. Don't move otherwise. Return non-nil if point
3332 ;; This function might do hidden buffer changes.
3333 (or (< (skip-syntax-backward "w_") 0)
3334 (and (c-major-mode-is 'pike-mode
)
3335 ;; Handle the `<operator> syntax in Pike.
3336 (let ((pos (point)))
3337 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3338 (< (skip-chars-backward "`") 0)
3339 (looking-at c-symbol-key
)
3340 (>= (match-end 0) pos
))
3345 (defun c-beginning-of-current-token (&optional back-limit
)
3346 ;; Move to the beginning of the current token. Do not move if not
3347 ;; in the middle of one. BACK-LIMIT may be used to bound the
3348 ;; backward search; if given it's assumed to be at the boundary
3349 ;; between two tokens. Return non-nil if the point is moved, nil
3352 ;; This function might do hidden buffer changes.
3353 (let ((start (point)))
3354 (if (looking-at "\\w\\|\\s_")
3355 (skip-syntax-backward "w_" back-limit
)
3356 (when (< (skip-syntax-backward ".()" back-limit
) 0)
3357 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp
)
3359 ;; `c-nonsymbol-token-regexp' should always match
3360 ;; since we've skipped backward over punctuator
3361 ;; or paren syntax, but consume one char in case
3362 ;; it doesn't so that we don't leave point before
3363 ;; some earlier incorrect token.
3366 (goto-char pos
))))))
3369 (defun c-end-of-current-token (&optional back-limit
)
3370 ;; Move to the end of the current token. Do not move if not in the
3371 ;; middle of one. BACK-LIMIT may be used to bound the backward
3372 ;; search; if given it's assumed to be at the boundary between two
3373 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3375 ;; This function might do hidden buffer changes.
3376 (let ((start (point)))
3377 (cond ((< (skip-syntax-backward "w_" (1- start
)) 0)
3378 (skip-syntax-forward "w_"))
3379 ((< (skip-syntax-backward ".()" back-limit
) 0)
3381 (if (looking-at c-nonsymbol-token-regexp
)
3382 (goto-char (match-end 0))
3383 ;; `c-nonsymbol-token-regexp' should always match since
3384 ;; we've skipped backward over punctuator or paren
3385 ;; syntax, but move forward in case it doesn't so that
3386 ;; we don't leave point earlier than we started with.
3388 (< (point) start
)))))
3391 (defconst c-jump-syntax-balanced
3392 (if (memq 'gen-string-delim c-emacs-features
)
3393 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3394 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3396 (defconst c-jump-syntax-unbalanced
3397 (if (memq 'gen-string-delim c-emacs-features
)
3398 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3399 "\\w\\|\\s_\\|\\s\""))
3401 (defun c-forward-token-2 (&optional count balanced limit
)
3402 "Move forward by tokens.
3403 A token is defined as all symbols and identifiers which aren't
3404 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3405 treated properly). Point is always either left at the beginning of a
3406 token or not moved at all. COUNT specifies the number of tokens to
3407 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3408 moves to the next token beginning only if not already at one. If
3409 BALANCED is true, move over balanced parens, otherwise move into them.
3410 Also, if BALANCED is true, never move out of an enclosing paren.
3412 LIMIT sets the limit for the movement and defaults to the point limit.
3413 The case when LIMIT is set in the middle of a token, comment or macro
3414 is handled correctly, i.e. the point won't be left there.
3416 Return the number of tokens left to move \(positive or negative). If
3417 BALANCED is true, a move over a balanced paren counts as one. Note
3418 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3419 be returned. Thus, a return value of 0 guarantees that point is at
3420 the requested position and a return value less \(without signs) than
3421 COUNT guarantees that point is at the beginning of some token.
3423 Note that this function might do hidden buffer changes. See the
3424 comment at the start of cc-engine.el for more info."
3426 (or count
(setq count
1))
3428 (- (c-backward-token-2 (- count
) balanced limit
))
3430 (let ((jump-syntax (if balanced
3431 c-jump-syntax-balanced
3432 c-jump-syntax-unbalanced
))
3437 ;; If count is zero we should jump if in the middle of a token.
3438 (c-end-of-current-token))
3441 (if limit
(narrow-to-region (point-min) limit
))
3443 (progn (c-forward-syntactic-ws) (point)))
3444 ;; Skip whitespace. Count this as a move if we did in
3446 (setq count
(max (1- count
) 0)))
3449 ;; Moved out of bounds. Make sure the returned count isn't zero.
3451 (if (zerop count
) (setq count
1))
3454 ;; Use `condition-case' to avoid having the limit tests
3461 (cond ((looking-at jump-syntax
)
3462 (goto-char (scan-sexps (point) 1))
3464 ((looking-at c-nonsymbol-token-regexp
)
3465 (goto-char (match-end 0))
3467 ;; `c-nonsymbol-token-regexp' above should always
3468 ;; match if there are correct tokens. Try to
3469 ;; widen to see if the limit was set in the
3470 ;; middle of one, else fall back to treating
3471 ;; the offending thing as a one character token.
3475 (looking-at c-nonsymbol-token-regexp
)))
3480 (c-forward-syntactic-ws)
3483 (error (goto-char last
)))
3487 (setq count
(1+ count
)))))
3491 (defun c-backward-token-2 (&optional count balanced limit
)
3492 "Move backward by tokens.
3493 See `c-forward-token-2' for details."
3495 (or count
(setq count
1))
3497 (- (c-forward-token-2 (- count
) balanced limit
))
3499 (or limit
(setq limit
(point-min)))
3500 (let ((jump-syntax (if balanced
3501 c-jump-syntax-balanced
3502 c-jump-syntax-unbalanced
))
3506 ;; The count is zero so try to skip to the beginning of the
3509 (progn (c-beginning-of-current-token) (point)))
3510 (if (< (point) limit
)
3511 ;; The limit is inside the same token, so return 1.
3514 ;; We're not in the middle of a token. If there's
3515 ;; whitespace after the point then we must move backward,
3516 ;; so set count to 1 in that case.
3517 (and (looking-at c-syntactic-ws-start
)
3518 ;; If we're looking at a '#' that might start a cpp
3519 ;; directive then we have to do a more elaborate check.
3520 (or (/= (char-after) ?
#)
3521 (not c-opt-cpp-prefix
)
3524 (progn (beginning-of-line)
3525 (looking-at "[ \t]*")
3528 (progn (backward-char)
3529 (not (eq (char-before) ?
\\)))))))
3532 ;; Use `condition-case' to avoid having to check for buffer
3533 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3538 (c-backward-syntactic-ws)
3540 (if (looking-at jump-syntax
)
3541 (goto-char (scan-sexps (1+ (point)) -
1))
3542 ;; This can be very inefficient if there's a long
3543 ;; sequence of operator tokens without any separation.
3544 ;; That doesn't happen in practice, anyway.
3545 (c-beginning-of-current-token))
3546 (>= (point) limit
)))
3549 (error (goto-char last
)))
3551 (if (< (point) limit
)
3556 (defun c-forward-token-1 (&optional count balanced limit
)
3557 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3558 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3559 characters are jumped over character by character. This function is
3560 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3561 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3562 (c-forward-token-2 count balanced limit
)))
3564 (defun c-backward-token-1 (&optional count balanced limit
)
3565 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3566 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3567 characters are jumped over character by character. This function is
3568 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3569 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3570 (c-backward-token-2 count balanced limit
)))
3573 ;; Tools for doing searches restricted to syntactically relevant text.
3575 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3576 paren-level not-inside-token
3577 lookbehind-submatch
)
3578 "Like `re-search-forward', but only report matches that are found
3579 in syntactically significant text. I.e. matches in comments, macros
3580 or string literals are ignored. The start point is assumed to be
3581 outside any comment, macro or string literal, or else the content of
3582 that region is taken as syntactically significant text.
3584 If PAREN-LEVEL is non-nil, an additional restriction is added to
3585 ignore matches in nested paren sexps. The search will also not go
3586 outside the current list sexp, which has the effect that if the point
3587 should be moved to BOUND when no match is found \(i.e. NOERROR is
3588 neither nil nor t), then it will be at the closing paren if the end of
3589 the current list sexp is encountered first.
3591 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3592 ignored. Things like multicharacter operators and special symbols
3593 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3596 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3597 subexpression in REGEXP. The end of that submatch is used as the
3598 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3599 isn't used or if that subexpression didn't match then the start
3600 position of the whole match is used instead. The \"look behind\"
3601 subexpression is never tested before the starting position, so it
3602 might be a good idea to include \\=\\= as a match alternative in it.
3604 Optimization note: Matches might be missed if the \"look behind\"
3605 subexpression can match the end of nonwhite syntactic whitespace,
3606 i.e. the end of comments or cpp directives. This since the function
3607 skips over such things before resuming the search. It's on the other
3608 hand not safe to assume that the \"look behind\" subexpression never
3609 matches syntactic whitespace.
3611 Bug: Unbalanced parens inside cpp directives are currently not handled
3612 correctly \(i.e. they don't get ignored as they should) when
3615 Note that this function might do hidden buffer changes. See the
3616 comment at the start of cc-engine.el for more info."
3618 (or bound
(setq bound
(point-max)))
3619 (if paren-level
(setq paren-level -
1))
3621 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3623 (let ((start (point))
3625 ;; Start position for the last search.
3627 ;; The `parse-partial-sexp' state between the start position
3630 ;; The current position after the last state update. The next
3631 ;; `parse-partial-sexp' continues from here.
3633 ;; The position at which to check the state and the state
3634 ;; there. This is separate from `state-pos' since we might
3635 ;; need to back up before doing the next search round.
3636 check-pos check-state
3637 ;; Last position known to end a token.
3638 (last-token-end-pos (point-min))
3639 ;; Set when a valid match is found.
3646 (setq search-pos
(point))
3647 (re-search-forward regexp bound noerror
))
3650 (setq state
(parse-partial-sexp
3651 state-pos
(match-beginning 0) paren-level nil state
)
3653 (if (setq check-pos
(and lookbehind-submatch
3654 (or (not paren-level
)
3656 (match-end lookbehind-submatch
)))
3657 (setq check-state
(parse-partial-sexp
3658 state-pos check-pos paren-level nil state
))
3659 (setq check-pos state-pos
3662 ;; NOTE: If we got a look behind subexpression and get
3663 ;; an insignificant match in something that isn't
3664 ;; syntactic whitespace (i.e. strings or in nested
3665 ;; parentheses), then we can never skip more than a
3666 ;; single character from the match start position
3667 ;; (i.e. `state-pos' here) before continuing the
3668 ;; search. That since the look behind subexpression
3669 ;; might match the end of the insignificant region in
3673 ((elt check-state
7)
3674 ;; Match inside a line comment. Skip to eol. Use
3675 ;; `re-search-forward' instead of `skip-chars-forward' to get
3676 ;; the right bound behavior.
3677 (re-search-forward "[\n\r]" bound noerror
))
3679 ((elt check-state
4)
3680 ;; Match inside a block comment. Skip to the '*/'.
3681 (search-forward "*/" bound noerror
))
3683 ((and (not (elt check-state
5))
3684 (eq (char-before check-pos
) ?
/)
3685 (not (c-get-char-property (1- check-pos
) 'syntax-table
))
3686 (memq (char-after check-pos
) '(?
/ ?
*)))
3687 ;; Match in the middle of the opener of a block or line
3689 (if (= (char-after check-pos
) ?
/)
3690 (re-search-forward "[\n\r]" bound noerror
)
3691 (search-forward "*/" bound noerror
)))
3693 ;; The last `parse-partial-sexp' above might have
3694 ;; stopped short of the real check position if the end
3695 ;; of the current sexp was encountered in paren-level
3696 ;; mode. The checks above are always false in that
3697 ;; case, and since they can do better skipping in
3698 ;; lookbehind-submatch mode, we do them before
3699 ;; checking the paren level.
3702 (/= (setq tmp
(car check-state
)) 0))
3703 ;; Check the paren level first since we're short of the
3704 ;; syntactic checking position if the end of the
3705 ;; current sexp was encountered by `parse-partial-sexp'.
3708 ;; Inside a nested paren sexp.
3709 (if lookbehind-submatch
3710 ;; See the NOTE above.
3711 (progn (goto-char state-pos
) t
)
3712 ;; Skip out of the paren quickly.
3713 (setq state
(parse-partial-sexp state-pos bound
0 nil state
)
3716 ;; Have exited the current paren sexp.
3719 ;; The last `parse-partial-sexp' call above
3720 ;; has left us just after the closing paren
3721 ;; in this case, so we can modify the bound
3722 ;; to leave the point at the right position
3724 (setq bound
(1- (point)))
3726 (signal 'search-failed
(list regexp
)))))
3728 ((setq tmp
(elt check-state
3))
3729 ;; Match inside a string.
3730 (if (or lookbehind-submatch
3731 (not (integerp tmp
)))
3732 ;; See the NOTE above.
3733 (progn (goto-char state-pos
) t
)
3734 ;; Skip to the end of the string before continuing.
3735 (let ((ender (make-string 1 tmp
)) (continue t
))
3736 (while (if (search-forward ender bound noerror
)
3738 (setq state
(parse-partial-sexp
3739 state-pos
(point) nil nil state
)
3742 (setq continue nil
)))
3747 (c-beginning-of-macro start
)))
3748 ;; Match inside a macro. Skip to the end of it.
3750 (cond ((<= (point) bound
) t
)
3752 (t (signal 'search-failed
(list regexp
)))))
3754 ((and not-inside-token
3755 (or (< check-pos last-token-end-pos
)
3758 (goto-char check-pos
)
3760 (c-end-of-current-token last-token-end-pos
))
3761 (setq last-token-end-pos
(point))))))
3763 (if lookbehind-submatch
3764 ;; See the NOTE above.
3765 (goto-char state-pos
)
3766 (goto-char (min last-token-end-pos bound
))))
3773 ;; Should loop to search again, but take care to avoid
3774 ;; looping on the same spot.
3775 (or (/= search-pos
(point))
3776 (if (= (point) bound
)
3779 (signal 'search-failed
(list regexp
)))
3785 (signal (car err
) (cdr err
))))
3787 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3791 (goto-char (match-end 0))
3794 ;; Search failed. Set point as appropriate.
3800 (defvar safe-pos-list
) ; bound in c-syntactic-skip-backward
3802 (defsubst c-ssb-lit-begin
()
3803 ;; Return the start of the literal point is in, or nil.
3804 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3805 ;; bound in the caller.
3807 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3808 ;; if it's outside comments and strings.
3810 (let ((pos (point)) safe-pos state pps-end-pos
)
3811 ;; Pick a safe position as close to the point as possible.
3813 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3816 (while (and safe-pos-list
3817 (> (car safe-pos-list
) (point)))
3818 (setq safe-pos-list
(cdr safe-pos-list
)))
3819 (unless (setq safe-pos
(car-safe safe-pos-list
))
3820 (setq safe-pos
(max (or (c-safe-position
3821 (point) (or c-state-cache
3825 safe-pos-list
(list safe-pos
)))
3827 ;; Cache positions along the way to use if we have to back up more. We
3828 ;; cache every closing paren on the same level. If the paren cache is
3829 ;; relevant in this region then we're typically already on the same
3830 ;; level as the target position. Note that we might cache positions
3831 ;; after opening parens in case safe-pos is in a nested list. That's
3832 ;; both uncommon and harmless.
3834 (setq state
(parse-partial-sexp
3837 (setq safe-pos
(point)
3838 safe-pos-list
(cons safe-pos safe-pos-list
)))
3840 ;; If the state contains the start of the containing sexp we cache that
3841 ;; position too, so that parse-partial-sexp in the next run has a bigger
3842 ;; chance of starting at the same level as the target position and thus
3843 ;; will get more good safe positions into the list.
3845 (setq safe-pos
(1+ (elt state
1))
3846 safe-pos-list
(cons safe-pos safe-pos-list
)))
3848 (if (or (elt state
3) (elt state
4))
3849 ;; Inside string or comment. Continue search at the
3853 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level
)
3854 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3855 i.e. don't stop at positions inside syntactic whitespace or string
3856 literals. Preprocessor directives are also ignored, with the exception
3857 of the one that the point starts within, if any. If LIMIT is given,
3858 it's assumed to be at a syntactically relevant position.
3860 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3861 sexps, and the search will also not go outside the current paren sexp.
3862 However, if LIMIT or the buffer limit is reached inside a nested paren
3863 then the point will be left at the limit.
3865 Non-nil is returned if the point moved, nil otherwise.
3867 Note that this function might do hidden buffer changes. See the
3868 comment at the start of cc-engine.el for more info."
3870 (let ((start (point))
3872 ;; A list of syntactically relevant positions in descending
3873 ;; order. It's used to avoid scanning repeatedly over
3874 ;; potentially large regions with `parse-partial-sexp' to verify
3875 ;; each position. Used in `c-ssb-lit-begin'
3877 ;; The result from `c-beginning-of-macro' at the start position or the
3878 ;; start position itself if it isn't within a macro. Evaluated on
3881 ;; The earliest position after the current one with the same paren
3882 ;; level. Used only when `paren-level' is set.
3884 (paren-level-pos (point)))
3888 ;; The next loop "tries" to find the end point each time round,
3889 ;; loops when it hasn't succeeded.
3892 (< (skip-chars-backward skip-chars limit
) 0)
3894 (let ((pos (point)) state-2 pps-end-pos
)
3897 ;; Don't stop inside a literal
3898 ((setq lit-beg
(c-ssb-lit-begin))
3904 (setq state-2
(parse-partial-sexp
3905 pos paren-level-pos -
1)
3906 pps-end-pos
(point))
3907 (/= (car state-2
) 0)))
3908 ;; Not at the right level.
3910 (if (and (< (car state-2
) 0)
3911 ;; We stop above if we go out of a paren.
3912 ;; Now check whether it precedes or is
3913 ;; nested in the starting sexp.
3917 pps-end-pos paren-level-pos
3919 (< (car state-2
) 0)))
3921 ;; We've stopped short of the starting position
3922 ;; so the hit was inside a nested list. Go up
3923 ;; until we are at the right level.
3926 (goto-char (scan-lists pos -
1
3928 (setq paren-level-pos
(point))
3929 (if (and limit
(>= limit paren-level-pos
))
3935 (goto-char (or limit
(point-min)))
3938 ;; The hit was outside the list at the start
3939 ;; position. Go to the start of the list and exit.
3940 (goto-char (1+ (elt state-2
1)))
3943 ((c-beginning-of-macro limit
)
3947 (setq start-macro-beg
3950 (c-beginning-of-macro limit
)
3954 ;; It's inside the same macro we started in so it's
3955 ;; a relevant match.
3961 ;; Skip syntactic ws afterwards so that we don't stop at the
3962 ;; end of a comment if `skip-chars' is something like "^/".
3963 (c-backward-syntactic-ws)
3966 ;; We might want to extend this with more useful return values in
3968 (/= (point) start
)))
3970 ;; The following is an alternative implementation of
3971 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3972 ;; track of the syntactic context. It turned out to be generally
3973 ;; slower than the one above which uses forward checks from earlier
3976 ;;(defconst c-ssb-stop-re
3977 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3978 ;; ;; stop at to avoid going into comments and literals.
3980 ;; ;; Match comment end syntax and string literal syntax. Also match
3981 ;; ;; '/' for block comment endings (not covered by comment end
3983 ;; "\\s>\\|/\\|\\s\""
3984 ;; (if (memq 'gen-string-delim c-emacs-features)
3987 ;; (if (memq 'gen-comment-delim c-emacs-features)
3991 ;;(defconst c-ssb-stop-paren-re
3992 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3993 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3995 ;;(defconst c-ssb-sexp-end-re
3996 ;; ;; Regexp matching the ending syntax of a complex sexp.
3997 ;; (concat c-string-limit-regexp "\\|\\s)"))
3999 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4000 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4001 ;;i.e. don't stop at positions inside syntactic whitespace or string
4002 ;;literals. Preprocessor directives are also ignored. However, if the
4003 ;;point is within a comment, string literal or preprocessor directory to
4004 ;;begin with, its contents is treated as syntactically relevant chars.
4005 ;;If LIMIT is given, it limits the backward search and the point will be
4006 ;;left there if no earlier position is found.
4008 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4009 ;;sexps, and the search will also not go outside the current paren sexp.
4010 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4011 ;;then the point will be left at the limit.
4013 ;;Non-nil is returned if the point moved, nil otherwise.
4015 ;;Note that this function might do hidden buffer changes. See the
4016 ;;comment at the start of cc-engine.el for more info."
4018 ;; (save-restriction
4020 ;; (narrow-to-region limit (point-max)))
4022 ;; (let ((start (point)))
4024 ;; (while (let ((last-pos (point))
4026 ;; (skip-chars-backward skip-chars)
4029 ;; ;; Skip back over the same region as
4030 ;; ;; `skip-chars-backward' above, but keep to
4031 ;; ;; syntactically relevant positions.
4032 ;; (goto-char last-pos)
4034 ;; ;; `re-search-backward' with a single char regexp
4035 ;; ;; should be fast.
4036 ;; (re-search-backward
4037 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4042 ;; ((looking-at "\\s(")
4043 ;; ;; `paren-level' is set and we've found the
4044 ;; ;; start of the containing paren.
4048 ;; ((looking-at c-ssb-sexp-end-re)
4049 ;; ;; We're at the end of a string literal or paren
4050 ;; ;; sexp (if `paren-level' is set).
4052 ;; (condition-case nil
4053 ;; (c-backward-sexp)
4055 ;; (goto-char limit)
4056 ;; (throw 'done t))))
4060 ;; ;; At the end of some syntactic ws or possibly
4061 ;; ;; after a plain '/' operator.
4062 ;; (let ((pos (point)))
4063 ;; (c-backward-syntactic-ws)
4064 ;; (if (= pos (point))
4065 ;; ;; Was a plain '/' operator. Go past it.
4066 ;; (backward-char)))))
4068 ;; (> (point) stop-pos))))
4070 ;; ;; Now the point is either at `stop-pos' or at some
4071 ;; ;; position further back if `stop-pos' was at a
4072 ;; ;; syntactically irrelevant place.
4074 ;; ;; Skip additional syntactic ws so that we don't stop
4075 ;; ;; at the end of a comment if `skip-chars' is
4076 ;; ;; something like "^/".
4077 ;; (c-backward-syntactic-ws)
4079 ;; (< (point) stop-pos))))
4081 ;; ;; We might want to extend this with more useful return values
4082 ;; ;; in the future.
4083 ;; (/= (point) start))))
4086 ;; Tools for handling comments and string literals.
4088 (defun c-slow-in-literal (&optional lim detect-cpp
)
4089 "Return the type of literal point is in, if any.
4090 The return value is `c' if in a C-style comment, `c++' if in a C++
4091 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4092 is non-nil and in a preprocessor line, or nil if somewhere else.
4093 Optional LIM is used as the backward limit of the search. If omitted,
4094 or nil, `c-beginning-of-defun' is used.
4096 The last point calculated is cached if the cache is enabled, i.e. if
4097 `c-in-literal-cache' is bound to a two element vector.
4099 Note that this function might do hidden buffer changes. See the
4100 comment at the start of cc-engine.el for more info."
4102 (if (and (vectorp c-in-literal-cache
)
4103 (= (point) (aref c-in-literal-cache
0)))
4104 (aref c-in-literal-cache
1)
4105 (let ((rtn (save-excursion
4106 (let* ((pos (point))
4108 (c-beginning-of-syntax)
4110 (state (parse-partial-sexp lim pos
)))
4112 ((elt state
3) 'string
)
4113 ((elt state
4) (if (elt state
7) 'c
++ 'c
))
4114 ((and detect-cpp
(c-beginning-of-macro lim
)) 'pound
)
4116 ;; cache this result if the cache is enabled
4117 (if (not c-in-literal-cache
)
4118 (setq c-in-literal-cache
(vector (point) rtn
)))
4121 ;; XEmacs has a built-in function that should make this much quicker.
4122 ;; I don't think we even need the cache, which makes our lives more
4123 ;; complicated anyway. In this case, lim is only used to detect
4126 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
4127 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
4128 ;; Xemacs 21.4.4. It manifested itself thus:
4130 ;; Starting with an empty AWK Mode buffer, type
4132 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
4134 ;; AWK Mode is designed such that when the first / is typed, it gets the
4135 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
4136 ;; are given the s-t property "string". However, buffer-syntactic-context
4137 ;; fails to take account of the change of the s-t property on the opening / to
4138 ;; "string", and reports that the { is within a string started by the second /.
4140 ;; The workaround for this is for the AWK Mode initialisation to switch the
4141 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
4142 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
4144 ;; (Alan Mackenzie, 2003/4/30).
4146 (defun c-fast-in-literal (&optional lim detect-cpp
)
4147 ;; This function might do hidden buffer changes.
4148 (let ((context (buffer-syntactic-context)))
4150 ((eq context
'string
) 'string
)
4151 ((eq context
'comment
) 'c
++)
4152 ((eq context
'block-comment
) 'c
)
4153 ((and detect-cpp
(save-excursion (c-beginning-of-macro lim
))) 'pound
))))
4155 (defalias 'c-in-literal
4156 (if (fboundp 'buffer-syntactic-context
)
4157 'c-fast-in-literal
; XEmacs
4158 'c-slow-in-literal
)) ; GNU Emacs
4160 ;; The defalias above isn't enough to shut up the byte compiler.
4161 (cc-bytecomp-defun c-in-literal)
4163 (defun c-literal-limits (&optional lim near not-in-delimiter
)
4164 "Return a cons of the beginning and end positions of the comment or
4165 string surrounding point (including both delimiters), or nil if point
4166 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4167 to start parsing from. If NEAR is non-nil, then the limits of any
4168 literal next to point is returned. \"Next to\" means there's only
4169 spaces and tabs between point and the literal. The search for such a
4170 literal is done first in forward direction. If NOT-IN-DELIMITER is
4171 non-nil, the case when point is inside a starting delimiter won't be
4172 recognized. This only has effect for comments which have starting
4173 delimiters with more than one character.
4175 Note that this function might do hidden buffer changes. See the
4176 comment at the start of cc-engine.el for more info."
4179 (let* ((pos (point))
4181 (c-beginning-of-syntax)
4183 (state (parse-partial-sexp lim pos
)))
4185 (cond ((elt state
3) ; String.
4186 (goto-char (elt state
8))
4187 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4190 ((elt state
4) ; Comment.
4191 (goto-char (elt state
8))
4192 (cons (point) (progn (c-forward-single-comment) (point))))
4194 ((and (not not-in-delimiter
)
4196 (eq (char-before) ?
/)
4197 (looking-at "[/*]"))
4198 ;; We're standing in a comment starter.
4200 (cons (point) (progn (c-forward-single-comment) (point))))
4205 ;; Search forward for a literal.
4206 (skip-chars-forward " \t")
4209 ((looking-at c-string-limit-regexp
) ; String.
4210 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4213 ((looking-at c-comment-start-regexp
) ; Line or block comment.
4214 (cons (point) (progn (c-forward-single-comment) (point))))
4218 (skip-chars-backward " \t")
4220 (let ((end (point)) beg
)
4223 (< (skip-syntax-backward c-string-syntax
) 0)) ; String.
4224 (setq beg
(c-safe (c-backward-sexp 1) (point))))
4226 ((and (c-safe (forward-char -
2) t
)
4228 ;; Block comment. Due to the nature of line
4229 ;; comments, they will always be covered by the
4230 ;; normal case above.
4232 (c-backward-single-comment)
4233 ;; If LIM is bogus, beg will be bogus.
4234 (setq beg
(point))))
4236 (if beg
(cons beg end
))))))
4239 ;; In case external callers use this; it did have a docstring.
4240 (defalias 'c-literal-limits-fast
'c-literal-limits
)
4242 (defun c-collect-line-comments (range)
4243 "If the argument is a cons of two buffer positions (such as returned by
4244 `c-literal-limits'), and that range contains a C++ style line comment,
4245 then an extended range is returned that contains all adjacent line
4246 comments (i.e. all comments that starts in the same column with no
4247 empty lines or non-whitespace characters between them). Otherwise the
4248 argument is returned.
4250 Note that this function might do hidden buffer changes. See the
4251 comment at the start of cc-engine.el for more info."
4255 (if (and (consp range
) (progn
4256 (goto-char (car range
))
4257 (looking-at c-line-comment-starter
)))
4258 (let ((col (current-column))
4260 (bopl (c-point 'bopl
))
4262 ;; Got to take care in the backward direction to handle
4263 ;; comments which are preceded by code.
4264 (while (and (c-backward-single-comment)
4266 (looking-at c-line-comment-starter
)
4267 (= col
(current-column)))
4269 bopl
(c-point 'bopl
)))
4271 (while (and (progn (skip-chars-forward " \t")
4272 (looking-at c-line-comment-starter
))
4273 (= col
(current-column))
4274 (prog1 (zerop (forward-line 1))
4275 (setq end
(point)))))
4280 (defun c-literal-type (range)
4281 "Convenience function that given the result of `c-literal-limits',
4282 returns nil or the type of literal that the range surrounds, one
4283 of the symbols 'c, 'c++ or 'string. It's much faster than using
4284 `c-in-literal' and is intended to be used when you need both the
4285 type of a literal and its limits.
4287 Note that this function might do hidden buffer changes. See the
4288 comment at the start of cc-engine.el for more info."
4292 (goto-char (car range
))
4293 (cond ((looking-at c-string-limit-regexp
) 'string
)
4294 ((or (looking-at "//") ; c++ line comment
4295 (and (looking-at "\\s<") ; comment starter
4296 (looking-at "#"))) ; awk comment.
4298 (t 'c
))) ; Assuming the range is valid.
4302 ;; `c-find-decl-spots' and accompanying stuff.
4304 ;; Variables used in `c-find-decl-spots' to cache the search done for
4305 ;; the first declaration in the last call. When that function starts,
4306 ;; it needs to back up over syntactic whitespace to look at the last
4307 ;; token before the region being searched. That can sometimes cause
4308 ;; moves back and forth over a quite large region of comments and
4309 ;; macros, which would be repeated for each changed character when
4310 ;; we're called during fontification, since font-lock refontifies the
4311 ;; current line for each change. Thus it's worthwhile to cache the
4314 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4315 ;; the syntactic whitespace less or equal to some start position.
4316 ;; There's no cached value if it's nil.
4318 ;; `c-find-decl-match-pos' is the match position if
4319 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4320 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4321 (defvar c-find-decl-syntactic-pos nil
)
4322 (make-variable-buffer-local 'c-find-decl-syntactic-pos
)
4323 (defvar c-find-decl-match-pos nil
)
4324 (make-variable-buffer-local 'c-find-decl-match-pos
)
4326 (defsubst c-invalidate-find-decl-cache
(change-min-pos)
4327 (and c-find-decl-syntactic-pos
4328 (< change-min-pos c-find-decl-syntactic-pos
)
4329 (setq c-find-decl-syntactic-pos nil
)))
4331 ; (defface c-debug-decl-spot-face
4332 ; '((t (:background "Turquoise")))
4333 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4334 ; (defface c-debug-decl-sws-face
4335 ; '((t (:background "Khaki")))
4336 ; "Debug face to mark the syntactic whitespace between the declaration
4337 ; spots and the preceding token end.")
4339 (defmacro c-debug-put-decl-spot-faces
(match-pos decl-pos
)
4340 (when (facep 'c-debug-decl-spot-face
)
4341 `(c-save-buffer-state ((match-pos ,match-pos
) (decl-pos ,decl-pos
))
4342 (c-debug-add-face (max match-pos
(point-min)) decl-pos
4343 'c-debug-decl-sws-face
)
4344 (c-debug-add-face decl-pos
(min (1+ decl-pos
) (point-max))
4345 'c-debug-decl-spot-face
))))
4346 (defmacro c-debug-remove-decl-spot-faces
(beg end
)
4347 (when (facep 'c-debug-decl-spot-face
)
4348 `(c-save-buffer-state ()
4349 (c-debug-remove-face ,beg
,end
'c-debug-decl-spot-face
)
4350 (c-debug-remove-face ,beg
,end
'c-debug-decl-sws-face
))))
4352 (defmacro c-find-decl-prefix-search
()
4353 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4354 ;; but it contains lots of free variables that refer to things
4355 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4356 ;; if there is a match, otherwise at `cfd-limit'.
4358 ;; This macro might do hidden buffer changes.
4361 ;; Find the next property match position if we haven't got one already.
4362 (unless cfd-prop-match
4365 (goto-char (next-single-property-change
4366 (point) 'c-type nil cfd-limit
))
4367 (and (< (point) cfd-limit
)
4368 (not (eq (c-get-char-property (1- (point)) 'c-type
)
4370 (setq cfd-prop-match
(point))))
4372 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4374 (unless cfd-re-match
4376 (if (> cfd-re-match-end
(point))
4377 (goto-char cfd-re-match-end
))
4379 (while (if (setq cfd-re-match-end
4380 (re-search-forward c-decl-prefix-or-start-re
4383 ;; Match. Check if it's inside a comment or string literal.
4385 (if (setq cfd-re-match
(match-end 1))
4386 ;; Matched the end of a token preceding a decl spot.
4388 (goto-char cfd-re-match
)
4390 ;; Matched a token that start a decl spot.
4391 (goto-char (match-beginning 0))
4395 ;; No match. Finish up and exit the loop.
4396 (setq cfd-re-match cfd-limit
)
4399 ;; Skip out of comments and string literals.
4401 (goto-char (next-single-property-change
4402 (point) 'face nil cfd-limit
))
4403 (and (< (point) cfd-limit
)
4404 (c-got-face-at (point) c-literal-faces
)))))
4406 ;; If we matched at the decl start, we have to back up over the
4407 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4408 ;; any decl spots in the syntactic ws.
4409 (unless cfd-re-match
4410 (c-backward-syntactic-ws)
4411 (setq cfd-re-match
(point))))
4413 ;; Choose whichever match is closer to the start.
4414 (if (< cfd-re-match cfd-prop-match
)
4415 (setq cfd-match-pos cfd-re-match
4417 (setq cfd-match-pos cfd-prop-match
4418 cfd-prop-match nil
))
4420 (goto-char cfd-match-pos
)
4422 (when (< cfd-match-pos cfd-limit
)
4423 ;; Skip forward past comments only so we don't skip macros.
4424 (c-forward-comments)
4425 ;; Set the position to continue at. We can avoid going over
4426 ;; the comments skipped above a second time, but it's possible
4427 ;; that the comment skipping has taken us past `cfd-prop-match'
4428 ;; since the property might be used inside comments.
4429 (setq cfd-continue-pos
(if cfd-prop-match
4430 (min cfd-prop-match
(point))
4433 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun
)
4434 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4435 ;; label from the point to CFD-LIMIT.
4437 ;; CFD-FUN is called with point at the start of the spot. It's
4438 ;; passed two arguments: The first is the end position of the token
4439 ;; preceding the spot, or 0 for the implicit match at bob. The
4440 ;; second is a flag that is t when the match is inside a macro. If
4441 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
4442 ;; spot, it should return non-nil to ensure that the next search
4446 ;; o The first token after bob.
4447 ;; o The first token after the end of submatch 1 in
4448 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4449 ;; o The start of each `c-decl-prefix-or-start-re' match when
4450 ;; submatch 1 doesn't match.
4451 ;; o The first token after the end of each occurrence of the
4452 ;; `c-type' text property with the value `c-decl-end', provided
4453 ;; `c-type-decl-end-used' is set.
4455 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4456 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4457 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4459 ;; If the match is inside a macro then the buffer is narrowed to the
4460 ;; end of it, so that CFD-FUN can investigate the following tokens
4461 ;; without matching something that begins inside a macro and ends
4462 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4463 ;; CFD-FACE-CHECKLIST checks exist.
4465 ;; The spots are visited approximately in order from top to bottom.
4466 ;; It's however the positions where `c-decl-prefix-or-start-re'
4467 ;; matches and where `c-decl-end' properties are found that are in
4468 ;; order. Since the spots often are at the following token, they
4469 ;; might be visited out of order insofar as more spots are reported
4470 ;; later on within the syntactic whitespace between the match
4471 ;; positions and their spots.
4473 ;; It's assumed that comments and strings are fontified in the
4476 ;; This is mainly used in fontification, and so has an elaborate
4477 ;; cache to handle repeated calls from the same start position; see
4478 ;; the variables above.
4480 ;; All variables in this function begin with `cfd-' to avoid name
4481 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4483 ;; This function might do hidden buffer changes.
4485 (let ((cfd-start-pos (point))
4486 (cfd-buffer-end (point-max))
4487 ;; The end of the token preceding the decl spot last found
4488 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4491 ;; The end position of the last `c-decl-prefix-or-start-re'
4492 ;; match. If this is greater than `cfd-continue-pos', the
4493 ;; next regexp search is started here instead.
4494 (cfd-re-match-end (point-min))
4495 ;; The end of the last `c-decl-end' found by
4496 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4497 ;; match. If searching for the property isn't needed then we
4498 ;; disable it by setting it to `cfd-limit' directly.
4499 (cfd-prop-match (unless c-type-decl-end-used cfd-limit
))
4500 ;; The end of the token preceding the decl spot last found by
4501 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4502 ;; bob. `cfd-limit' if there's no match. In other words,
4503 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4504 (cfd-match-pos cfd-limit
)
4505 ;; The position to continue searching at.
4507 ;; The position of the last "real" token we've stopped at.
4508 ;; This can be greater than `cfd-continue-pos' when we get
4509 ;; hits inside macros or at `c-decl-end' positions inside
4512 ;; The end position of the last entered macro.
4515 ;; Initialize by finding a syntactically relevant start position
4516 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4517 ;; search unless we're at bob.
4519 (let (start-in-literal start-in-macro syntactic-pos
)
4520 ;; Must back up a bit since we look for the end of the previous
4521 ;; statement or declaration, which is earlier than the first
4525 ;; First we need to move to a syntactically relevant position.
4526 ;; Begin by backing out of comment or string literals.
4528 (when (c-got-face-at (point) c-literal-faces
)
4529 ;; Try to use the faces to back up to the start of the
4530 ;; literal. FIXME: What if the point is on a declaration
4531 ;; inside a comment?
4532 (while (and (not (bobp))
4533 (c-got-face-at (1- (point)) c-literal-faces
))
4534 (goto-char (previous-single-property-change
4535 (point) 'face nil
(point-min))))
4537 ;; XEmacs doesn't fontify the quotes surrounding string
4539 (and (featurep 'xemacs
)
4540 (eq (get-text-property (point) 'face
)
4541 'font-lock-string-face
)
4543 (progn (backward-char)
4544 (not (looking-at c-string-limit-regexp
)))
4547 ;; Don't trust the literal to contain only literal faces
4548 ;; (the font lock package might not have fontified the
4549 ;; start of it at all, for instance) so check that we have
4550 ;; arrived at something that looks like a start or else
4551 ;; resort to `c-literal-limits'.
4552 (unless (looking-at c-literal-start-regexp
)
4553 (let ((range (c-literal-limits)))
4554 (if range
(goto-char (car range
)))))
4556 (setq start-in-literal
(point)))
4558 ;; The start is in a literal. If the limit is in the same
4559 ;; one we don't have to find a syntactic position etc. We
4560 ;; only check that if the limit is at or before bonl to save
4561 ;; time; it covers the by far most common case when font-lock
4562 ;; refontifies the current line only.
4563 (<= cfd-limit
(c-point 'bonl cfd-start-pos
))
4565 (goto-char cfd-start-pos
)
4567 (goto-char (next-single-property-change
4568 (point) 'face nil cfd-limit
))
4569 (and (< (point) cfd-limit
)
4570 (c-got-face-at (point) c-literal-faces
))))
4571 (= (point) cfd-limit
)))
4573 ;; Completely inside a literal. Set up variables to trig the
4574 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4575 ;; find a suitable start position.
4576 (setq cfd-continue-pos start-in-literal
))
4578 ;; Check if the region might be completely inside a macro, to
4579 ;; optimize that like the completely-inside-literal above.
4581 (and (= (forward-line 1) 0)
4582 (bolp) ; forward-line has funny behavior at eob.
4583 (>= (point) cfd-limit
)
4584 (progn (backward-char)
4585 (eq (char-before) ?
\\))))
4586 ;; (Maybe) completely inside a macro. Only need to trig the
4587 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4589 (setq cfd-continue-pos
(1- cfd-start-pos
)
4593 ;; Back out of any macro so we don't miss any declaration
4594 ;; that could follow after it.
4595 (when (c-beginning-of-macro)
4596 (setq start-in-macro t
))
4598 ;; Now we're at a proper syntactically relevant position so we
4599 ;; can use the cache. But first clear it if it applied
4601 (c-invalidate-find-decl-cache cfd-start-pos
)
4603 (setq syntactic-pos
(point))
4604 (unless (eq syntactic-pos c-find-decl-syntactic-pos
)
4605 ;; Don't have to do this if the cache is relevant here,
4606 ;; typically if the same line is refontified again. If
4607 ;; we're just some syntactic whitespace further down we can
4608 ;; still use the cache to limit the skipping.
4609 (c-backward-syntactic-ws c-find-decl-syntactic-pos
))
4611 ;; If we hit `c-find-decl-syntactic-pos' and
4612 ;; `c-find-decl-match-pos' is set then we install the cached
4613 ;; values. If we hit `c-find-decl-syntactic-pos' and
4614 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4615 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4616 ;; and so we can continue the search from this point. If we
4617 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4618 ;; the right spot to begin searching anyway.
4619 (if (and (eq (point) c-find-decl-syntactic-pos
)
4620 c-find-decl-match-pos
)
4621 (setq cfd-match-pos c-find-decl-match-pos
4622 cfd-continue-pos syntactic-pos
)
4624 (setq c-find-decl-syntactic-pos syntactic-pos
)
4627 ;; Always consider bob a match to get the first
4628 ;; declaration in the file. Do this separately instead of
4629 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4630 ;; regexp always can consume at least one character to
4631 ;; ensure that we won't get stuck in an infinite loop.
4632 (setq cfd-re-match
0)
4634 (c-beginning-of-current-token)
4635 (< (point) cfd-limit
))
4636 ;; Do an initial search now. In the bob case above it's
4637 ;; only done to search for a `c-decl-end' spot.
4638 (c-find-decl-prefix-search))
4640 (setq c-find-decl-match-pos
(and (< cfd-match-pos cfd-start-pos
)
4643 ;; Advance `cfd-continue-pos' if it's before the start position.
4644 ;; The closest continue position that might have effect at or
4645 ;; after the start depends on what we started in. This also
4646 ;; finds a suitable start position in the special cases when the
4647 ;; region is completely within a literal or macro.
4648 (when (and cfd-continue-pos
(< cfd-continue-pos cfd-start-pos
))
4652 ;; If we're in a macro then it's the closest preceding token
4653 ;; in the macro. Check this before `start-in-literal',
4654 ;; since if we're inside a literal in a macro, the preceding
4655 ;; token is earlier than any `c-decl-end' spot inside the
4656 ;; literal (comment).
4657 (goto-char (or start-in-literal cfd-start-pos
))
4658 ;; The only syntactic ws in macros are comments.
4659 (c-backward-comments)
4661 (c-beginning-of-current-token))
4664 ;; If we're in a comment it can only be the closest
4665 ;; preceding `c-decl-end' position within that comment, if
4666 ;; any. Go back to the beginning of such a property so that
4667 ;; `c-find-decl-prefix-search' will find the end of it.
4668 ;; (Can't stop at the end and install it directly on
4669 ;; `cfd-prop-match' since that variable might be cleared
4670 ;; after `cfd-fun' below.)
4672 ;; Note that if the literal is a string then the property
4673 ;; search will simply skip to the beginning of it right
4675 (if (not c-type-decl-end-used
)
4676 (goto-char start-in-literal
)
4677 (goto-char cfd-start-pos
)
4679 (goto-char (previous-single-property-change
4680 (point) 'c-type nil start-in-literal
))
4681 (and (> (point) start-in-literal
)
4682 (not (eq (c-get-char-property (point) 'c-type
)
4685 (when (= (point) start-in-literal
)
4686 ;; Didn't find any property inside the comment, so we can
4687 ;; skip it entirely. (This won't skip past a string, but
4688 ;; that'll be handled quickly by the next
4689 ;; `c-find-decl-prefix-search' anyway.)
4690 (c-forward-single-comment)
4691 (if (> (point) cfd-limit
)
4692 (goto-char cfd-limit
))))
4695 ;; If we started in normal code, the only match that might
4696 ;; apply before the start is what we already got in
4697 ;; `cfd-match-pos' so we can continue at the start position.
4698 ;; (Note that we don't get here if the first match is below
4700 (goto-char cfd-start-pos
)))
4702 ;; Delete found matches if they are before our new continue
4703 ;; position, so that `c-find-decl-prefix-search' won't back up
4704 ;; to them later on.
4705 (setq cfd-continue-pos
(point))
4706 (when (and cfd-re-match
(< cfd-re-match cfd-continue-pos
))
4707 (setq cfd-re-match nil
))
4708 (when (and cfd-prop-match
(< cfd-prop-match cfd-continue-pos
))
4709 (setq cfd-prop-match nil
)))
4712 ;; This is the normal case and we got a proper syntactic
4713 ;; position. If there's a match then it's always outside
4714 ;; macros and comments, so advance to the next token and set
4715 ;; `cfd-token-pos'. The loop below will later go back using
4716 ;; `cfd-continue-pos' to fix declarations inside the
4718 (when (and cfd-match-pos
(< cfd-match-pos syntactic-pos
))
4719 (goto-char syntactic-pos
)
4720 (c-forward-syntactic-ws)
4721 (and cfd-continue-pos
4722 (< cfd-continue-pos
(point))
4723 (setq cfd-token-pos
(point))))
4725 ;; Have one of the special cases when the region is completely
4726 ;; within a literal or macro. `cfd-continue-pos' is set to a
4727 ;; good start position for the search, so do it.
4728 (c-find-decl-prefix-search)))
4730 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4734 (< cfd-match-pos cfd-limit
)
4737 ;; Kludge to filter out matches on the "<" that
4738 ;; aren't open parens, for the sake of languages
4739 ;; that got `c-recognize-<>-arglists' set.
4740 (and (eq (char-before cfd-match-pos
) ?
<)
4741 (not (c-get-char-property (1- cfd-match-pos
)
4744 ;; If `cfd-continue-pos' is less or equal to
4745 ;; `cfd-token-pos', we've got a hit inside a macro
4746 ;; that's in the syntactic whitespace before the last
4747 ;; "real" declaration we've checked. If they're equal
4748 ;; we've arrived at the declaration a second time, so
4749 ;; there's nothing to do.
4750 (= cfd-continue-pos cfd-token-pos
)
4753 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4754 ;; we're still searching for declarations embedded in
4755 ;; the syntactic whitespace. In that case we need
4756 ;; only to skip comments and not macros, since they
4757 ;; can't be nested, and that's already been done in
4758 ;; `c-find-decl-prefix-search'.
4759 (when (> cfd-continue-pos cfd-token-pos
)
4760 (c-forward-syntactic-ws)
4761 (setq cfd-token-pos
(point)))
4763 ;; Continue if the following token fails the
4764 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4765 (when (or (>= (point) cfd-limit
)
4766 (not (looking-at cfd-decl-re
))
4767 (and cfd-face-checklist
4769 (point) cfd-face-checklist
))))
4770 (goto-char cfd-continue-pos
)
4773 (< (point) cfd-limit
))
4774 (c-find-decl-prefix-search))
4776 (< (point) cfd-limit
))
4779 (>= (point) cfd-start-pos
)
4782 ;; Narrow to the end of the macro if we got a hit inside
4783 ;; one, to avoid recognizing things that start inside the
4784 ;; macro and end outside it.
4785 (when (> cfd-match-pos cfd-macro-end
)
4786 ;; Not in the same macro as in the previous round.
4788 (goto-char cfd-match-pos
)
4790 (if (save-excursion (and (c-beginning-of-macro)
4791 (< (point) cfd-match-pos
)))
4792 (progn (c-end-of-macro)
4796 (if (zerop cfd-macro-end
)
4798 (if (> cfd-macro-end
(point))
4799 (progn (narrow-to-region (point-min) cfd-macro-end
)
4801 ;; The matched token was the last thing in the macro,
4802 ;; so the whole match is bogus.
4803 (setq cfd-macro-end
0)
4806 (c-debug-put-decl-spot-faces cfd-match-pos
(point))
4807 (if (funcall cfd-fun cfd-match-pos
(/= cfd-macro-end
0))
4808 (setq cfd-prop-match nil
))
4810 (when (/= cfd-macro-end
0)
4811 ;; Restore limits if we did macro narrowment above.
4812 (narrow-to-region (point-min) cfd-buffer-end
)))
4814 (goto-char cfd-continue-pos
)
4815 (if (= cfd-continue-pos cfd-limit
)
4816 (setq cfd-match-pos cfd-limit
)
4817 (c-find-decl-prefix-search)))))
4820 ;; A cache for found types.
4822 ;; Buffer local variable that contains an obarray with the types we've
4823 ;; found. If a declaration is recognized somewhere we record the
4824 ;; fully qualified identifier in it to recognize it as a type
4825 ;; elsewhere in the file too. This is not accurate since we do not
4826 ;; bother with the scoping rules of the languages, but in practice the
4827 ;; same name is seldom used as both a type and something else in a
4828 ;; file, and we only use this as a last resort in ambiguous cases (see
4829 ;; `c-forward-decl-or-cast-1').
4831 ;; Not every type need be in this cache. However, things which have
4832 ;; ceased to be types must be removed from it.
4834 ;; Template types in C++ are added here too but with the template
4835 ;; arglist replaced with "<>" in references or "<" for the one in the
4836 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4837 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4838 ;; template specs can be fairly sized programs in themselves) and
4839 ;; improves the hit ratio (it's a type regardless of the template
4840 ;; args; it's just not the same type, but we're only interested in
4841 ;; recognizing types, not telling distinct types apart). Note that
4842 ;; template types in references are added here too; from the example
4843 ;; above there will also be an entry "Foo<".
4844 (defvar c-found-types nil
)
4845 (make-variable-buffer-local 'c-found-types
)
4847 (defsubst c-clear-found-types
()
4848 ;; Clears `c-found-types'.
4849 (setq c-found-types
(make-vector 53 0)))
4851 (defun c-add-type (from to
)
4852 ;; Add the given region as a type in `c-found-types'. If the region
4853 ;; doesn't match an existing type but there is a type which is equal
4854 ;; to the given one except that the last character is missing, then
4855 ;; the shorter type is removed. That's done to avoid adding all
4856 ;; prefixes of a type as it's being entered and font locked. This
4857 ;; doesn't cover cases like when characters are removed from a type
4858 ;; or added in the middle. We'd need the position of point when the
4859 ;; font locking is invoked to solve this well.
4861 ;; This function might do hidden buffer changes.
4862 (let ((type (c-syntactic-content from to c-recognize-
<>-arglists
)))
4863 (unless (intern-soft type c-found-types
)
4864 (unintern (substring type
0 -
1) c-found-types
)
4865 (intern type c-found-types
))))
4867 (defun c-unfind-type (name)
4868 ;; Remove the "NAME" from c-found-types, if present.
4869 (unintern name c-found-types
))
4871 (defsubst c-check-type
(from to
)
4872 ;; Return non-nil if the given region contains a type in
4875 ;; This function might do hidden buffer changes.
4876 (intern-soft (c-syntactic-content from to c-recognize-
<>-arglists
)
4879 (defun c-list-found-types ()
4880 ;; Return all the types in `c-found-types' as a sorted list of
4883 (mapatoms (lambda (type)
4884 (setq type-list
(cons (symbol-name type
)
4887 (sort type-list
'string-lessp
)))
4889 ;; Shut up the byte compiler.
4890 (defvar c-maybe-stale-found-type
)
4892 (defun c-trim-found-types (beg end old-len
)
4893 ;; An after change function which, in conjunction with the info in
4894 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4895 ;; from `c-found-types', should this type have become stale. For
4896 ;; example, this happens to "foo" when "foo \n bar();" becomes
4897 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4898 ;; the fontification.
4900 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4904 (when (< end
(point-max))
4906 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4907 (progn (goto-char end
)
4908 (c-end-of-current-token)))
4909 (c-unfind-type (buffer-substring-no-properties
4911 (when (> beg
(point-min))
4913 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4914 (progn (goto-char beg
)
4915 (c-beginning-of-current-token)))
4916 (c-unfind-type (buffer-substring-no-properties
4919 (if c-maybe-stale-found-type
; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4921 ;; Changing the amount of (already existing) whitespace - don't do anything.
4922 ((and (c-partial-ws-p beg end
)
4923 (or (= beg end
) ; removal of WS
4924 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type
)))))
4926 ;; The syntactic relationship which defined a "found type" has been
4928 ((eq (car c-maybe-stale-found-type
) 'c-decl-id-start
)
4929 (c-unfind-type (cadr c-maybe-stale-found-type
)))
4930 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4934 ;; Setting and removing syntax properties on < and > in languages (C++
4935 ;; and Java) where they can be template/generic delimiters as well as
4936 ;; their normal meaning of "less/greater than".
4938 ;; Normally, < and > have syntax 'punctuation'. When they are found to
4939 ;; be delimiters, they are marked as such with the category properties
4940 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
4944 ;; It is impossible to determine with certainty whether a <..> pair in
4945 ;; C++ is two comparison operators or is template delimiters, unless
4946 ;; one duplicates a lot of a C++ compiler. For example, the following
4949 ;; foo (a < b, c > d) ;
4951 ;; could be a function call with two integer parameters (each a
4952 ;; relational expression), or it could be a constructor for class foo
4953 ;; taking one parameter d of templated type "a < b, c >". They are
4954 ;; somewhat easier to distinguish in Java.
4956 ;; The strategy now (2010-01) adopted is to mark and unmark < and
4957 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
4958 ;; individually when their context so indicated. This gave rise to
4959 ;; intractible problems when one of a matching pair was deleted, or
4960 ;; pulled into a literal.]
4962 ;; At each buffer change, the syntax-table properties are removed in a
4963 ;; before-change function and reapplied, when needed, in an
4964 ;; after-change function. It is far more important that the
4965 ;; properties get removed when they they are spurious than that they
4966 ;; be present when wanted.
4967 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4968 (defun c-clear-<-pair-props
(&optional pos
)
4969 ;; POS (default point) is at a < character. If it is marked with
4970 ;; open paren syntax-table text property, remove the property,
4971 ;; together with the close paren property on the matching > (if
4977 (when (equal (c-get-char-property (point) 'syntax-table
)
4978 c-
<-as-paren-syntax
)
4979 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
4980 (c-go-list-forward))
4981 (when (equal (c-get-char-property (1- (point)) 'syntax-table
)
4982 c-
>-as-paren-syntax
) ; should always be true.
4983 (c-clear-char-property (1- (point)) 'category
))
4984 (c-clear-char-property pos
'category
))))
4986 (defun c-clear->-pair-props
(&optional pos
)
4987 ;; POS (default point) is at a > character. If it is marked with
4988 ;; close paren syntax-table property, remove the property, together
4989 ;; with the open paren property on the matching < (if any).
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-up-list-backward))
4998 (when (equal (c-get-char-property (point) 'syntax-table
)
4999 c-
<-as-paren-syntax
) ; should always be true.
5000 (c-clear-char-property (point) 'category
))
5001 (c-clear-char-property pos
'category
))))
5003 (defun c-clear-<>-pair-props
(&optional pos
)
5004 ;; POS (default point) is at a < or > character. If it has an
5005 ;; open/close paren syntax-table property, remove this property both
5006 ;; from the current character and its partner (which will also be
5009 ((eq (char-after) ?\
<)
5010 (c-clear-<-pair-props pos
))
5011 ((eq (char-after) ?\
>)
5012 (c-clear->-pair-props pos
))
5014 "c-clear-<>-pair-props called from wrong position"))))
5016 (defun c-clear-<-pair-props-if-match-after
(lim &optional pos
)
5017 ;; POS (default point) is at a < character. If it is both marked
5018 ;; with open/close paren syntax-table property, and has a matching >
5019 ;; (also marked) which is after LIM, remove the property both from
5020 ;; the current > and its partner. Return t when this happens, nil
5026 (when (equal (c-get-char-property (point) 'syntax-table
)
5027 c-
<-as-paren-syntax
)
5028 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
5029 (c-go-list-forward))
5030 (when (and (>= (point) lim
)
5031 (equal (c-get-char-property (1- (point)) 'syntax-table
)
5032 c-
>-as-paren-syntax
)) ; should always be true.
5033 (c-unmark-<-
>-as-paren
(1- (point)))
5034 (c-unmark-<-
>-as-paren pos
))
5037 (defun c-clear->-pair-props-if-match-before
(lim &optional pos
)
5038 ;; POS (default point) is at a > character. If it is both marked
5039 ;; with open/close paren syntax-table property, and has a matching <
5040 ;; (also marked) which is before LIM, remove the property both from
5041 ;; the current < and its partner. Return t when this happens, nil
5047 (when (equal (c-get-char-property (point) 'syntax-table
)
5048 c-
>-as-paren-syntax
)
5049 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
5050 (c-go-up-list-backward))
5051 (when (and (<= (point) lim
)
5052 (equal (c-get-char-property (point) 'syntax-table
)
5053 c-
<-as-paren-syntax
)) ; should always be true.
5054 (c-unmark-<-
>-as-paren
(point))
5055 (c-unmark-<-
>-as-paren pos
))
5058 ;; Set by c-common-init in cc-mode.el.
5062 (defun c-before-change-check-<>-operators
(beg end
)
5063 ;; Unmark certain pairs of "< .... >" which are currently marked as
5064 ;; template/generic delimiters. (This marking is via syntax-table
5065 ;; text properties).
5067 ;; These pairs are those which are in the current "statement" (i.e.,
5068 ;; the region between the {, }, or ; before BEG and the one after
5069 ;; END), and which enclose any part of the interval (BEG END).
5071 ;; Note that in C++ (?and Java), template/generic parens cannot
5072 ;; enclose a brace or semicolon, so we use these as bounds on the
5073 ;; region we must work on.
5075 ;; This function is called from before-change-functions (via
5076 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5077 ;; and point is undefined, both at entry and exit.
5079 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5082 (let ((beg-lit-limits (progn (goto-char beg
) (c-literal-limits)))
5083 (end-lit-limits (progn (goto-char end
) (c-literal-limits)))
5084 new-beg new-end need-new-beg need-new-end
)
5085 ;; Locate the barrier before the changed region
5086 (goto-char (if beg-lit-limits
(car beg-lit-limits
) beg
))
5087 (c-syntactic-skip-backward "^;{}" (max (- beg
2048) (point-min)))
5088 (setq new-beg
(point))
5090 ;; Remove the syntax-table properties from each pertinent <...> pair.
5091 ;; Firsly, the ones with the < before beg and > after beg.
5092 (while (c-search-forward-char-property 'category
'c-
<-as-paren-syntax beg
)
5093 (if (c-clear-<-pair-props-if-match-after beg
(1- (point)))
5094 (setq need-new-beg t
)))
5096 ;; Locate the barrier after END.
5097 (goto-char (if end-lit-limits
(cdr end-lit-limits
) end
))
5098 (c-syntactic-re-search-forward "[;{}]"
5099 (min (+ end
2048) (point-max)) 'end
)
5100 (setq new-end
(point))
5102 ;; Remove syntax-table properties from the remaining pertinent <...>
5103 ;; pairs, those with a > after end and < before end.
5104 (while (c-search-backward-char-property 'category
'c-
>-as-paren-syntax end
)
5105 (if (c-clear->-pair-props-if-match-before end
)
5106 (setq need-new-end t
)))
5108 ;; Extend the fontification region, if needed.
5111 (c-forward-syntactic-ws)
5112 (and (< (point) c-new-BEG
) (setq c-new-BEG
(point))))
5115 (and (> new-end c-new-END
) (setq c-new-END new-end
))))))
5119 (defun c-after-change-check-<>-operators
(beg end
)
5120 ;; This is called from `after-change-functions' when
5121 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5122 ;; chars with paren syntax become part of another operator like "<<"
5125 ;; This function might do hidden buffer changes.
5129 (when (or (looking-at "[<>]")
5130 (< (skip-chars-backward "<>") 0))
5133 (c-beginning-of-current-token)
5134 (when (and (< (point) beg
)
5135 (looking-at c-
<>-multichar-token-regexp
)
5136 (< beg
(setq beg
(match-end 0))))
5137 (while (progn (skip-chars-forward "^<>" beg
)
5139 (c-clear-<>-pair-props
)
5144 (when (or (looking-at "[<>]")
5145 (< (skip-chars-backward "<>") 0))
5148 (c-beginning-of-current-token)
5149 (when (and (< (point) end
)
5150 (looking-at c-
<>-multichar-token-regexp
)
5151 (< end
(setq end
(match-end 0))))
5152 (while (progn (skip-chars-forward "^<>" end
)
5154 (c-clear-<>-pair-props
)
5155 (forward-char)))))))
5159 ;; Handling of small scale constructs like types and names.
5161 ;; Dynamically bound variable that instructs `c-forward-type' to also
5162 ;; treat possible types (i.e. those that it normally returns 'maybe or
5163 ;; 'found for) as actual types (and always return 'found for them).
5164 ;; This means that it records them in `c-record-type-identifiers' if
5165 ;; that is set, and that it adds them to `c-found-types'.
5166 (defvar c-promote-possible-types nil
)
5168 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5169 ;; mark up successfully parsed arglists with paren syntax properties on
5170 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5171 ;; `c-type' property of each argument separating comma.
5173 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5174 ;; all arglists for side effects (i.e. recording types), otherwise it
5175 ;; exploits any existing paren syntax properties to quickly jump to the
5176 ;; end of already parsed arglists.
5178 ;; Marking up the arglists is not the default since doing that correctly
5179 ;; depends on a proper value for `c-restricted-<>-arglists'.
5180 (defvar c-parse-and-markup-
<>-arglists nil
)
5182 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5183 ;; not accept arglists that contain binary operators.
5185 ;; This is primarily used to handle C++ template arglists. C++
5186 ;; disambiguates them by checking whether the preceding name is a
5187 ;; template or not. We can't do that, so we assume it is a template
5188 ;; if it can be parsed as one. That usually works well since
5189 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5190 ;; in almost all cases would be pointless.
5192 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5193 ;; should let the comma separate the function arguments instead. And
5194 ;; in a context where the value of the expression is taken, e.g. in
5195 ;; "if (a < b || c > d)", it's probably not a template.
5196 (defvar c-restricted-
<>-arglists nil
)
5198 ;; Dynamically bound variables that instructs
5199 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5200 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5201 ;; `c-forward-label' to record the ranges of all the type and
5202 ;; reference identifiers they encounter. They will build lists on
5203 ;; these variables where each element is a cons of the buffer
5204 ;; positions surrounding each identifier. This recording is only
5205 ;; activated when `c-record-type-identifiers' is non-nil.
5207 ;; All known types that can't be identifiers are recorded, and also
5208 ;; other possible types if `c-promote-possible-types' is set.
5209 ;; Recording is however disabled inside angle bracket arglists that
5210 ;; are encountered inside names and other angle bracket arglists.
5211 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5214 ;; Only the names in C++ template style references (e.g. "tmpl" in
5215 ;; "tmpl<a,b>::foo") are recorded as references, other references
5216 ;; aren't handled here.
5218 ;; `c-forward-label' records the label identifier(s) on
5219 ;; `c-record-ref-identifiers'.
5220 (defvar c-record-type-identifiers nil
)
5221 (defvar c-record-ref-identifiers nil
)
5223 ;; This variable will receive a cons cell of the range of the last
5224 ;; single identifier symbol stepped over by `c-forward-name' if it's
5225 ;; successful. This is the range that should be put on one of the
5226 ;; record lists above by the caller. It's assigned nil if there's no
5227 ;; such symbol in the name.
5228 (defvar c-last-identifier-range nil
)
5230 (defmacro c-record-type-id
(range)
5231 (if (eq (car-safe range
) 'cons
)
5233 `(setq c-record-type-identifiers
5234 (cons ,range c-record-type-identifiers
))
5235 `(let ((range ,range
))
5237 (setq c-record-type-identifiers
5238 (cons range c-record-type-identifiers
))))))
5240 (defmacro c-record-ref-id
(range)
5241 (if (eq (car-safe range
) 'cons
)
5243 `(setq c-record-ref-identifiers
5244 (cons ,range c-record-ref-identifiers
))
5245 `(let ((range ,range
))
5247 (setq c-record-ref-identifiers
5248 (cons range c-record-ref-identifiers
))))))
5250 ;; Dynamically bound variable that instructs `c-forward-type' to
5251 ;; record the ranges of types that only are found. Behaves otherwise
5252 ;; like `c-record-type-identifiers'.
5253 (defvar c-record-found-types nil
)
5255 (defmacro c-forward-keyword-prefixed-id
(type)
5256 ;; Used internally in `c-forward-keyword-clause' to move forward
5257 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5258 ;; possibly is prefixed by keywords and their associated clauses.
5259 ;; Try with a type/name first to not trip up on those that begin
5260 ;; with a keyword. Return t if a known or found type is moved
5261 ;; over. The point is clobbered if nil is returned. If range
5262 ;; recording is enabled, the identifier is recorded on as a type
5263 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5265 ;; This macro might do hidden buffer changes.
5267 (while (if (setq res
,(if (eq type
'type
)
5271 (and (looking-at c-keywords-regexp
)
5272 (c-forward-keyword-clause 1))))
5273 (when (memq res
'(t known found prefix
))
5274 ,(when (eq type
'ref
)
5275 `(when c-record-type-identifiers
5276 (c-record-ref-id c-last-identifier-range
)))
5279 (defmacro c-forward-id-comma-list
(type update-safe-pos
)
5280 ;; Used internally in `c-forward-keyword-clause' to move forward
5281 ;; over a comma separated list of types or names using
5282 ;; `c-forward-keyword-prefixed-id'.
5284 ;; This macro might do hidden buffer changes.
5286 ,(when update-safe-pos
5287 `(setq safe-pos
(point)))
5288 (eq (char-after) ?
,))
5291 (c-forward-syntactic-ws)
5292 (c-forward-keyword-prefixed-id ,type
)))))
5294 (defun c-forward-keyword-clause (match)
5295 ;; Submatch MATCH in the current match data is assumed to surround a
5296 ;; token. If it's a keyword, move over it and any immediately
5297 ;; following clauses associated with it, stopping at the start of
5298 ;; the next token. t is returned in that case, otherwise the point
5299 ;; stays and nil is returned. The kind of clauses that are
5300 ;; recognized are those specified by `c-type-list-kwds',
5301 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5302 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5303 ;; and `c-<>-arglist-kwds'.
5305 ;; This function records identifier ranges on
5306 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5307 ;; `c-record-type-identifiers' is non-nil.
5309 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5310 ;; apply directly after the keyword, the type list is moved over
5311 ;; only when there is no unaccounted token before it (i.e. a token
5312 ;; that isn't moved over due to some other keyword list). The
5313 ;; identifier ranges in the list are still recorded if that should
5316 ;; This function might do hidden buffer changes.
5318 (let ((kwd-sym (c-keyword-sym (match-string match
))) safe-pos pos
5319 ;; The call to `c-forward-<>-arglist' below is made after
5320 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5321 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5322 ;; should therefore be nil.
5323 (c-parse-and-markup-<>-arglists t
)
5324 c-restricted-
<>-arglists
)
5327 (goto-char (match-end match
))
5328 (c-forward-syntactic-ws)
5329 (setq safe-pos
(point))
5332 ((and (c-keyword-member kwd-sym
'c-type-list-kwds
)
5333 (c-forward-keyword-prefixed-id type
))
5334 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5335 (c-forward-id-comma-list type t
))
5337 ((and (c-keyword-member kwd-sym
'c-ref-list-kwds
)
5338 (c-forward-keyword-prefixed-id ref
))
5339 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5340 (c-forward-id-comma-list ref t
))
5342 ((and (c-keyword-member kwd-sym
'c-paren-any-kwds
)
5343 (eq (char-after) ?\
())
5344 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5347 (when (and (setq pos
(c-up-list-forward))
5348 (eq (char-before pos
) ?\
)))
5349 (when (and c-record-type-identifiers
5350 (c-keyword-member kwd-sym
'c-paren-type-kwds
))
5351 ;; Use `c-forward-type' on every identifier we can find
5352 ;; inside the paren, to record the types.
5353 (while (c-syntactic-re-search-forward c-symbol-start pos t
)
5354 (goto-char (match-beginning 0))
5355 (unless (c-forward-type)
5356 (looking-at c-symbol-key
) ; Always matches.
5357 (goto-char (match-end 0)))))
5360 (c-forward-syntactic-ws)
5361 (setq safe-pos
(point))))
5363 ((and (c-keyword-member kwd-sym
'c-
<>-sexp-kwds
)
5364 (eq (char-after) ?
<)
5365 (c-forward-<>-arglist
(c-keyword-member kwd-sym
'c-
<>-type-kwds
)))
5366 (c-forward-syntactic-ws)
5367 (setq safe-pos
(point)))
5369 ((and (c-keyword-member kwd-sym
'c-nonsymbol-sexp-kwds
)
5370 (not (looking-at c-symbol-start
))
5371 (c-safe (c-forward-sexp) t
))
5372 (c-forward-syntactic-ws)
5373 (setq safe-pos
(point))))
5375 (when (c-keyword-member kwd-sym
'c-colon-type-list-kwds
)
5376 (if (eq (char-after) ?
:)
5377 ;; If we are at the colon already, we move over the type
5381 (c-forward-syntactic-ws)
5382 (when (c-forward-keyword-prefixed-id type
)
5383 (c-forward-id-comma-list type t
)))
5384 ;; Not at the colon, so stop here. But the identifier
5385 ;; ranges in the type list later on should still be
5387 (and c-record-type-identifiers
5389 ;; If a keyword matched both one of the types above and
5390 ;; this one, we match `c-colon-type-list-re' after the
5391 ;; clause matched above.
5392 (goto-char safe-pos
)
5393 (looking-at c-colon-type-list-re
))
5395 (goto-char (match-end 0))
5396 (c-forward-syntactic-ws)
5397 (c-forward-keyword-prefixed-id type
))
5398 ;; There's a type after the `c-colon-type-list-re' match
5399 ;; after a keyword in `c-colon-type-list-kwds'.
5400 (c-forward-id-comma-list type nil
))))
5402 (goto-char safe-pos
)
5405 ;; cc-mode requires cc-fonts.
5406 (declare-function c-fontify-recorded-types-and-refs
"cc-fonts" ())
5408 (defun c-forward-<>-arglist
(all-types)
5409 ;; The point is assumed to be at a "<". Try to treat it as the open
5410 ;; paren of an angle bracket arglist and move forward to the
5411 ;; corresponding ">". If successful, the point is left after the
5412 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5413 ;; returned. If ALL-TYPES is t then all encountered arguments in
5414 ;; the arglist that might be types are treated as found types.
5416 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5417 ;; function handles text properties on the angle brackets and argument
5418 ;; separating commas.
5420 ;; `c-restricted-<>-arglists' controls how lenient the template
5421 ;; arglist recognition should be.
5423 ;; This function records identifier ranges on
5424 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5425 ;; `c-record-type-identifiers' is non-nil.
5427 ;; This function might do hidden buffer changes.
5429 (let ((start (point))
5430 ;; If `c-record-type-identifiers' is set then activate
5431 ;; recording of any found types that constitute an argument in
5433 (c-record-found-types (if c-record-type-identifiers t
)))
5434 (if (catch 'angle-bracket-arglist-escape
5435 (setq c-record-found-types
5436 (c-forward-<>-arglist-recur all-types
)))
5438 (when (consp c-record-found-types
)
5439 (setq c-record-type-identifiers
5440 ;; `nconc' doesn't mind that the tail of
5441 ;; `c-record-found-types' is t.
5442 (nconc c-record-found-types c-record-type-identifiers
)))
5443 (if (c-major-mode-is 'java-mode
) (c-fontify-recorded-types-and-refs))
5449 (defun c-forward-<>-arglist-recur
(all-types)
5450 ;; Recursive part of `c-forward-<>-arglist'.
5452 ;; This function might do hidden buffer changes.
5454 (let ((start (point)) res pos tmp
5455 ;; Cover this so that any recorded found type ranges are
5456 ;; automatically lost if it turns out to not be an angle
5457 ;; bracket arglist. It's propagated through the return value
5458 ;; on successful completion.
5459 (c-record-found-types c-record-found-types
)
5460 ;; List that collects the positions after the argument
5461 ;; separating ',' in the arglist.
5463 ;; If the '<' has paren open syntax then we've marked it as an angle
5464 ;; bracket arglist before, so skip to the end.
5465 (if (and (not c-parse-and-markup-
<>-arglists
)
5466 (c-get-char-property (point) 'syntax-table
))
5470 (if (and (c-go-up-list-forward)
5471 (eq (char-before) ?
>))
5473 ;; Got unmatched paren angle brackets. We don't clear the paren
5474 ;; syntax properties and retry, on the basis that it's very
5475 ;; unlikely that paren angle brackets become operators by code
5476 ;; manipulation. It's far more likely that it doesn't match due
5477 ;; to narrowing or some temporary change.
5481 (forward-char) ; Forward over the opening '<'.
5483 (unless (looking-at c-
<-op-cont-regexp
)
5484 ;; go forward one non-alphanumeric character (group) per iteration of
5488 (c-forward-syntactic-ws)
5489 (let ((orig-record-found-types c-record-found-types
))
5490 (when (or (and c-record-type-identifiers all-types
)
5491 (c-major-mode-is 'java-mode
))
5492 ;; All encountered identifiers are types, so set the
5493 ;; promote flag and parse the type.
5495 (c-forward-syntactic-ws)
5496 (if (looking-at "\\?")
5498 (when (looking-at c-identifier-start
)
5499 (let ((c-promote-possible-types t
)
5500 (c-record-found-types t
))
5503 (c-forward-syntactic-ws)
5505 (when (or (looking-at "extends")
5506 (looking-at "super"))
5508 (c-forward-syntactic-ws)
5509 (let ((c-promote-possible-types t
)
5510 (c-record-found-types t
))
5512 (c-forward-syntactic-ws))))))
5514 (setq pos
(point)) ; e.g. first token inside the '<'
5516 ;; Note: These regexps exploit the match order in \| so
5517 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5518 (c-syntactic-re-search-forward
5519 ;; Stop on ',', '|', '&', '+' and '-' to catch
5520 ;; common binary operators that could be between
5521 ;; two comparison expressions "a<b" and "c>d".
5522 "[<;{},|+&-]\\|[>)]"
5526 ((eq (char-before) ?
>)
5527 ;; Either an operator starting with '>' or the end of
5528 ;; the angle bracket arglist.
5530 (if (looking-at c-
>-op-cont-regexp
)
5532 (goto-char (match-end 0))
5533 t
) ; Continue the loop.
5535 ;; The angle bracket arglist is finished.
5536 (when c-parse-and-markup-
<>-arglists
5537 (while arg-start-pos
5538 (c-put-c-type-property (1- (car arg-start-pos
))
5540 (setq arg-start-pos
(cdr arg-start-pos
)))
5541 (c-mark-<-as-paren start
)
5542 (c-mark->-as-paren
(1- (point))))
5544 nil
)) ; Exit the loop.
5546 ((eq (char-before) ?
<)
5547 ;; Either an operator starting with '<' or a nested arglist.
5549 (let (id-start id-end subres keyword-match
)
5551 ;; The '<' begins a multi-char operator.
5552 ((looking-at c-
<-op-cont-regexp
)
5553 (setq tmp
(match-end 0))
5554 (goto-char (match-end 0)))
5555 ;; We're at a nested <.....>
5558 (backward-char) ; to the '<'
5561 ;; There's always an identifier before an angle
5562 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5563 ;; or `c-<>-arglist-kwds'.
5564 (c-backward-syntactic-ws)
5565 (setq id-end
(point))
5566 (c-simple-skip-symbol-backward)
5567 (when (or (setq keyword-match
5568 (looking-at c-opt-
<>-sexp-key
))
5569 (not (looking-at c-keywords-regexp
)))
5570 (setq id-start
(point))))
5572 (let ((c-promote-possible-types t
)
5573 (c-record-found-types t
))
5574 (c-forward-<>-arglist-recur
5577 (c-keyword-sym (match-string 1))
5578 'c-
<>-type-kwds
)))))))
5580 ;; It was an angle bracket arglist.
5581 (setq c-record-found-types subres
)
5583 ;; Record the identifier before the template as a type
5584 ;; or reference depending on whether the arglist is last
5585 ;; in a qualified identifier.
5586 (when (and c-record-type-identifiers
5587 (not keyword-match
))
5588 (if (and c-opt-identifier-concat-key
5590 (c-forward-syntactic-ws)
5591 (looking-at c-opt-identifier-concat-key
)))
5592 (c-record-ref-id (cons id-start id-end
))
5593 (c-record-type-id (cons id-start id-end
)))))
5595 ;; At a "less than" operator.
5599 t
) ; carry on looping.
5601 ((and (not c-restricted-
<>-arglists
)
5602 (or (and (eq (char-before) ?
&)
5603 (not (eq (char-after) ?
&)))
5604 (eq (char-before) ?
,)))
5605 ;; Just another argument. Record the position. The
5606 ;; type check stuff that made us stop at it is at
5607 ;; the top of the loop.
5608 (setq arg-start-pos
(cons (point) arg-start-pos
)))
5611 ;; Got a character that can't be in an angle bracket
5612 ;; arglist argument. Abort using `throw', since
5613 ;; it's useless to try to find a surrounding arglist
5615 (throw 'angle-bracket-arglist-escape nil
))))))
5617 (or c-record-found-types t
)))))
5619 (defun c-backward-<>-arglist
(all-types &optional limit
)
5620 ;; The point is assumed to be directly after a ">". Try to treat it
5621 ;; as the close paren of an angle bracket arglist and move back to
5622 ;; the corresponding "<". If successful, the point is left at
5623 ;; the "<" and t is returned, otherwise the point isn't moved and
5624 ;; nil is returned. ALL-TYPES is passed on to
5625 ;; `c-forward-<>-arglist'.
5627 ;; If the optional LIMIT is given, it bounds the backward search.
5628 ;; It's then assumed to be at a syntactically relevant position.
5630 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5631 ;; function for more details.
5633 (let ((start (point)))
5635 (if (and (not c-parse-and-markup-
<>-arglists
)
5636 (c-get-char-property (point) 'syntax-table
))
5638 (if (and (c-go-up-list-backward)
5639 (eq (char-after) ?
<))
5641 ;; See corresponding note in `c-forward-<>-arglist'.
5646 (c-syntactic-skip-backward "^<;{}" limit t
)
5649 (if (eq (char-before) ?
<)
5651 ;; Stopped at bob or a char that isn't allowed in an
5652 ;; arglist, so we've failed.
5657 (progn (c-beginning-of-current-token)
5659 ;; If we moved then the "<" was part of some
5660 ;; multicharacter token.
5664 (let ((beg-pos (point)))
5665 (if (c-forward-<>-arglist all-types
)
5666 (cond ((= (point) start
)
5667 ;; Matched the arglist. Break the while.
5671 ;; We started from a non-paren ">" inside an
5676 ;; Matched a shorter arglist. Can be a nested
5677 ;; one so continue looking.
5682 (/= (point) start
))))
5684 (defun c-forward-name ()
5685 ;; Move forward over a complete name if at the beginning of one,
5686 ;; stopping at the next following token. A keyword, as such,
5687 ;; doesn't count as a name. If the point is not at something that
5688 ;; is recognized as a name then it stays put.
5690 ;; A name could be something as simple as "foo" in C or something as
5691 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5692 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5693 ;; int>::*volatile const" in C++ (this function is actually little
5694 ;; more than a `looking-at' call in all modes except those that,
5695 ;; like C++, have `c-recognize-<>-arglists' set).
5698 ;; o - nil if no name is found;
5699 ;; o - 'template if it's an identifier ending with an angle bracket
5701 ;; o - 'operator of it's an operator identifier;
5702 ;; o - t if it's some other kind of name.
5704 ;; This function records identifier ranges on
5705 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5706 ;; `c-record-type-identifiers' is non-nil.
5708 ;; This function might do hidden buffer changes.
5710 (let ((pos (point)) (start (point)) res id-start id-end
5711 ;; Turn off `c-promote-possible-types' here since we might
5712 ;; call `c-forward-<>-arglist' and we don't want it to promote
5713 ;; every suspect thing in the arglist to a type. We're
5714 ;; typically called from `c-forward-type' in this case, and
5715 ;; the caller only wants the top level type that it finds to
5717 c-promote-possible-types
)
5720 (looking-at c-identifier-key
)
5723 ;; Check for keyword. We go to the last symbol in
5724 ;; `c-identifier-key' first.
5725 (goto-char (setq id-end
(match-end 0)))
5726 (c-simple-skip-symbol-backward)
5727 (setq id-start
(point))
5729 (if (looking-at c-keywords-regexp
)
5730 (when (and (c-major-mode-is 'c
++-mode
)
5732 (cc-eval-when-compile
5733 (concat "\\(operator\\|\\(template\\)\\)"
5734 "\\(" (c-lang-const c-nonsymbol-key c
++)
5736 (if (match-beginning 2)
5737 ;; "template" is only valid inside an
5738 ;; identifier if preceded by "::".
5740 (c-backward-syntactic-ws)
5741 (and (c-safe (backward-char 2) t
)
5745 ;; Handle a C++ operator or template identifier.
5747 (c-forward-syntactic-ws)
5748 (cond ((eq (char-before id-end
) ?e
)
5749 ;; Got "... ::template".
5750 (let ((subres (c-forward-name)))
5755 ((looking-at c-identifier-start
)
5756 ;; Got a cast operator.
5757 (when (c-forward-type)
5760 ;; Now we should match a sequence of either
5761 ;; '*', '&' or a name followed by ":: *",
5762 ;; where each can be followed by a sequence
5763 ;; of `c-opt-type-modifier-key'.
5764 (while (cond ((looking-at "[*&]")
5765 (goto-char (match-end 0))
5767 ((looking-at c-identifier-start
)
5768 (and (c-forward-name)
5771 (goto-char (match-end 0))
5772 (c-forward-syntactic-ws)
5773 (eq (char-after) ?
*))
5778 (c-forward-syntactic-ws)
5780 (looking-at c-opt-type-modifier-key
))
5781 (goto-char (match-end 1))))))
5783 ((looking-at c-overloadable-operators-regexp
)
5784 ;; Got some other operator.
5785 (setq c-last-identifier-range
5786 (cons (point) (match-end 0)))
5787 (goto-char (match-end 0))
5788 (c-forward-syntactic-ws)
5794 ;; `id-start' is equal to `id-end' if we've jumped over
5795 ;; an identifier that doesn't end with a symbol token.
5796 ;; That can occur e.g. for Java import directives on the
5797 ;; form "foo.bar.*".
5798 (when (and id-start
(/= id-start id-end
))
5799 (setq c-last-identifier-range
5800 (cons id-start id-end
)))
5802 (c-forward-syntactic-ws)
5808 (when (or c-opt-identifier-concat-key
5809 c-recognize-
<>-arglists
)
5812 ((and c-opt-identifier-concat-key
5813 (looking-at c-opt-identifier-concat-key
))
5814 ;; Got a concatenated identifier. This handles the
5815 ;; cases with tricky syntactic whitespace that aren't
5816 ;; covered in `c-identifier-key'.
5817 (goto-char (match-end 0))
5818 (c-forward-syntactic-ws)
5821 ((and c-recognize-
<>-arglists
5822 (eq (char-after) ?
<))
5823 ;; Maybe an angle bracket arglist.
5824 (when (let ((c-record-type-identifiers t
)
5825 (c-record-found-types t
))
5826 (c-forward-<>-arglist nil
))
5828 (c-add-type start
(1+ pos
))
5829 (c-forward-syntactic-ws)
5831 c-last-identifier-range nil
)
5833 (if (and c-opt-identifier-concat-key
5834 (looking-at c-opt-identifier-concat-key
))
5836 ;; Continue if there's an identifier concatenation
5837 ;; operator after the template argument.
5839 (when (and c-record-type-identifiers id-start
)
5840 (c-record-ref-id (cons id-start id-end
)))
5842 (c-forward-syntactic-ws)
5845 (when (and c-record-type-identifiers id-start
)
5846 (c-record-type-id (cons id-start id-end
)))
5847 (setq res
'template
)
5854 (defun c-forward-type (&optional brace-block-too
)
5855 ;; Move forward over a type spec if at the beginning of one,
5856 ;; stopping at the next following token. The keyword "typedef"
5857 ;; isn't part of a type spec here.
5859 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
5860 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
5861 ;; The current (2009-03-10) intention is to convert all uses of
5862 ;; `c-forward-type' to call with this parameter set, then to
5866 ;; o - t if it's a known type that can't be a name or other
5868 ;; o - 'known if it's an otherwise known type (according to
5869 ;; `*-font-lock-extra-types');
5870 ;; o - 'prefix if it's a known prefix of a type;
5871 ;; o - 'found if it's a type that matches one in `c-found-types';
5872 ;; o - 'maybe if it's an identfier that might be a type; or
5873 ;; o - nil if it can't be a type (the point isn't moved then).
5875 ;; The point is assumed to be at the beginning of a token.
5877 ;; Note that this function doesn't skip past the brace definition
5878 ;; that might be considered part of the type, e.g.
5879 ;; "enum {a, b, c} foo".
5881 ;; This function records identifier ranges on
5882 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5883 ;; `c-record-type-identifiers' is non-nil.
5885 ;; This function might do hidden buffer changes.
5886 (when (and c-recognize-
<>-arglists
5888 (c-forward-<>-arglist t
)
5889 (c-forward-syntactic-ws))
5891 (let ((start (point)) pos res name-res id-start id-end id-range
)
5893 ;; Skip leading type modifiers. If any are found we know it's a
5894 ;; prefix of a type.
5895 (when c-opt-type-modifier-key
; e.g. "const" "volatile", but NOT "typedef"
5896 (while (looking-at c-opt-type-modifier-key
)
5897 (goto-char (match-end 1))
5898 (c-forward-syntactic-ws)
5899 (setq res
'prefix
)))
5902 ((looking-at c-type-prefix-key
) ; e.g. "struct", "class", but NOT
5904 (goto-char (match-end 1))
5905 (c-forward-syntactic-ws)
5908 (setq name-res
(c-forward-name))
5909 (setq res
(not (null name-res
)))
5910 (when (eq name-res t
)
5911 ;; In many languages the name can be used without the
5912 ;; prefix, so we add it to `c-found-types'.
5913 (c-add-type pos
(point))
5914 (when (and c-record-type-identifiers
5915 c-last-identifier-range
)
5916 (c-record-type-id c-last-identifier-range
)))
5917 (when (and brace-block-too
5919 (eq (char-after) ?\
{)
5922 (progn (c-forward-sexp)
5923 (c-forward-syntactic-ws)
5924 (setq pos
(point))))))
5927 (unless res
(goto-char start
))) ; invalid syntax
5931 (if (looking-at c-identifier-start
)
5933 (setq id-start
(point)
5934 name-res
(c-forward-name))
5936 (setq id-end
(point)
5937 id-range c-last-identifier-range
))))
5938 (and (cond ((looking-at c-primitive-type-key
)
5940 ((c-with-syntax-table c-identifier-syntax-table
5941 (looking-at c-known-type-key
))
5946 (goto-char (match-end 1))
5947 (c-forward-syntactic-ws)
5948 (setq pos
(point))))
5951 ;; Looking at a primitive or known type identifier. We've
5952 ;; checked for a name first so that we don't go here if the
5953 ;; known type match only is a prefix of another name.
5955 (setq id-end
(match-end 1))
5957 (when (and c-record-type-identifiers
5958 (or c-promote-possible-types
(eq res t
)))
5959 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
5961 (if (and c-opt-type-component-key
5963 (looking-at c-opt-type-component-key
)))
5964 ;; There might be more keywords for the type.
5966 (c-forward-keyword-clause 1)
5968 (setq safe-pos
(point))
5969 (looking-at c-opt-type-component-key
))
5970 (when (and c-record-type-identifiers
5971 (looking-at c-primitive-type-key
))
5972 (c-record-type-id (cons (match-beginning 1)
5974 (c-forward-keyword-clause 1))
5975 (if (looking-at c-primitive-type-key
)
5977 (when c-record-type-identifiers
5978 (c-record-type-id (cons (match-beginning 1)
5980 (c-forward-keyword-clause 1)
5982 (goto-char safe-pos
)
5983 (setq res
'prefix
)))
5984 (unless (save-match-data (c-forward-keyword-clause 1))
5987 (goto-char (match-end 1))
5988 (c-forward-syntactic-ws)))))
5991 (cond ((eq name-res t
)
5992 ;; A normal identifier.
5994 (if (or res c-promote-possible-types
)
5996 (c-add-type id-start id-end
)
5997 (when (and c-record-type-identifiers id-range
)
5998 (c-record-type-id id-range
))
6001 (setq res
(if (c-check-type id-start id-end
)
6002 ;; It's an identifier that has been used as
6003 ;; a type somewhere else.
6005 ;; It's an identifier that might be a type.
6007 ((eq name-res
'template
)
6008 ;; A template is a type.
6012 ;; Otherwise it's an operator identifier, which is not a type.
6017 ;; Skip trailing type modifiers. If any are found we know it's
6019 (when c-opt-type-modifier-key
6020 (while (looking-at c-opt-type-modifier-key
) ; e.g. "const", "volatile"
6021 (goto-char (match-end 1))
6022 (c-forward-syntactic-ws)
6024 ;; Step over any type suffix operator. Do not let the existence
6025 ;; of these alter the classification of the found type, since
6026 ;; these operators typically are allowed in normal expressions
6028 (when c-opt-type-suffix-key
6029 (while (looking-at c-opt-type-suffix-key
)
6030 (goto-char (match-end 1))
6031 (c-forward-syntactic-ws)))
6033 (when c-opt-type-concat-key
; Only/mainly for pike.
6034 ;; Look for a trailing operator that concatenates the type
6035 ;; with a following one, and if so step past that one through
6036 ;; a recursive call. Note that we don't record concatenated
6037 ;; types in `c-found-types' - it's the component types that
6038 ;; are recorded when appropriate.
6040 (let* ((c-promote-possible-types (or (memq res
'(t known
))
6041 c-promote-possible-types
))
6042 ;; If we can't promote then set `c-record-found-types' so that
6043 ;; we can merge in the types from the second part afterwards if
6044 ;; it turns out to be a known type there.
6045 (c-record-found-types (and c-record-type-identifiers
6046 (not c-promote-possible-types
)))
6048 (if (and (looking-at c-opt-type-concat-key
)
6051 (goto-char (match-end 1))
6052 (c-forward-syntactic-ws)
6053 (setq subres
(c-forward-type))))
6056 ;; If either operand certainly is a type then both are, but we
6057 ;; don't let the existence of the operator itself promote two
6058 ;; uncertain types to a certain one.
6061 (unless (eq name-res
'template
)
6062 (c-add-type id-start id-end
))
6063 (when (and c-record-type-identifiers id-range
)
6064 (c-record-type-id id-range
))
6075 (when (and (eq res t
)
6076 (consp c-record-found-types
))
6077 ;; Merge in the ranges of any types found by the second
6078 ;; `c-forward-type'.
6079 (setq c-record-type-identifiers
6080 ;; `nconc' doesn't mind that the tail of
6081 ;; `c-record-found-types' is t.
6082 (nconc c-record-found-types
6083 c-record-type-identifiers
))))
6087 (when (and c-record-found-types
(memq res
'(known found
)) id-range
)
6088 (setq c-record-found-types
6089 (cons id-range c-record-found-types
))))
6091 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6095 (defun c-forward-annotation ()
6096 ;; Used for Java code only at the moment. Assumes point is on the
6097 ;; @, moves forward an annotation. returns nil if there is no
6098 ;; annotation at point.
6099 (and (looking-at "@")
6100 (progn (forward-char) t
)
6102 (progn (c-forward-syntactic-ws) t
)
6103 (if (looking-at "(")
6108 ;; Handling of large scale constructs like statements and declarations.
6110 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6111 ;; defsubst or perhaps even a defun, but it contains lots of free
6112 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6113 (defmacro c-fdoc-shift-type-backward
(&optional short
)
6114 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6115 ;; of types when parsing a declaration, which means that it
6116 ;; sometimes consumes the identifier in the declaration as a type.
6117 ;; This is used to "backtrack" and make the last type be treated as
6118 ;; an identifier instead.
6121 ;; These identifiers are bound only in the inner let.
6122 '(setq identifier-type at-type
6123 identifier-start type-start
6127 got-suffix-after-parens id-start
6130 (if (setq at-type
(if (eq backup-at-type
'prefix
)
6133 (setq type-start backup-type-start
6134 id-start backup-id-start
)
6135 (setq type-start start-pos
6136 id-start start-pos
))
6138 ;; When these flags already are set we've found specifiers that
6139 ;; unconditionally signal these attributes - backtracking doesn't
6140 ;; change that. So keep them set in that case.
6142 (setq at-type-decl backup-at-type-decl
))
6144 (setq maybe-typeless backup-maybe-typeless
))
6147 ;; This identifier is bound only in the inner let.
6148 '(setq start id-start
))))
6150 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end
)
6151 ;; Move forward over a declaration or a cast if at the start of one.
6152 ;; The point is assumed to be at the start of some token. Nil is
6153 ;; returned if no declaration or cast is recognized, and the point
6154 ;; is clobbered in that case.
6156 ;; If a declaration is parsed:
6158 ;; The point is left at the first token after the first complete
6159 ;; declarator, if there is one. The return value is a cons where
6160 ;; the car is the position of the first token in the declarator. (See
6161 ;; below for the cdr.)
6164 ;; void foo (int a, char *b) stuff ...
6168 ;; unsigned int a = c_style_initializer, b;
6170 ;; unsigned int a (cplusplus_style_initializer), b;
6171 ;; car ^ ^ point (might change)
6172 ;; class Foo : public Bar {}
6174 ;; class PikeClass (int a, string b) stuff ...
6180 ;; void cplusplus_function (int x) throw (Bad);
6182 ;; Foo::Foo (int b) : Base (b) {}
6185 ;; The cdr of the return value is non-nil when a
6186 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6187 ;; Specifically it is a dotted pair (A . B) where B is t when a
6188 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6189 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6190 ;; specifier is present. I.e., (some of) the declared
6191 ;; identifier(s) are types.
6193 ;; If a cast is parsed:
6195 ;; The point is left at the first token after the closing paren of
6196 ;; the cast. The return value is `cast'. Note that the start
6197 ;; position must be at the first token inside the cast parenthesis
6200 ;; PRECEDING-TOKEN-END is the first position after the preceding
6201 ;; token, i.e. on the other side of the syntactic ws from the point.
6202 ;; Use a value less than or equal to (point-min) if the point is at
6203 ;; the first token in (the visible part of) the buffer.
6205 ;; CONTEXT is a symbol that describes the context at the point:
6206 ;; 'decl In a comma-separated declaration context (typically
6207 ;; inside a function declaration arglist).
6208 ;; '<> In an angle bracket arglist.
6209 ;; 'arglist Some other type of arglist.
6210 ;; nil Some other context or unknown context. Includes
6211 ;; within the parens of an if, for, ... construct.
6213 ;; LAST-CAST-END is the first token after the closing paren of a
6214 ;; preceding cast, or nil if none is known. If
6215 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6216 ;; the position after the closest preceding call where a cast was
6217 ;; matched. In that case it's used to discover chains of casts like
6220 ;; This function records identifier ranges on
6221 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6222 ;; `c-record-type-identifiers' is non-nil.
6224 ;; This function might do hidden buffer changes.
6226 (let (;; `start-pos' is used below to point to the start of the
6227 ;; first type, i.e. after any leading specifiers. It might
6228 ;; also point at the beginning of the preceding syntactic
6231 ;; Set to the result of `c-forward-type'.
6233 ;; The position of the first token in what we currently
6234 ;; believe is the type in the declaration or cast, after any
6235 ;; specifiers and their associated clauses.
6237 ;; The position of the first token in what we currently
6238 ;; believe is the declarator for the first identifier. Set
6239 ;; when the type is found, and moved forward over any
6240 ;; `c-decl-hangon-kwds' and their associated clauses that
6241 ;; occurs after the type.
6243 ;; These store `at-type', `type-start' and `id-start' of the
6244 ;; identifier before the one in those variables. The previous
6245 ;; identifier might turn out to be the real type in a
6246 ;; declaration if the last one has to be the declarator in it.
6247 ;; If `backup-at-type' is nil then the other variables have
6248 ;; undefined values.
6249 backup-at-type backup-type-start backup-id-start
6250 ;; Set if we've found a specifier (apart from "typedef") that makes
6251 ;; the defined identifier(s) types.
6253 ;; Set if we've a "typedef" keyword.
6255 ;; Set if we've found a specifier that can start a declaration
6256 ;; where there's no type.
6258 ;; If a specifier is found that also can be a type prefix,
6259 ;; these flags are set instead of those above. If we need to
6260 ;; back up an identifier, they are copied to the real flag
6261 ;; variables. Thus they only take effect if we fail to
6262 ;; interpret it as a type.
6263 backup-at-type-decl backup-maybe-typeless
6264 ;; Whether we've found a declaration or a cast. We might know
6265 ;; this before we've found the type in it. It's 'ids if we've
6266 ;; found two consecutive identifiers (usually a sure sign, but
6267 ;; we should allow that in labels too), and t if we've found a
6268 ;; specifier keyword (a 100% sure sign).
6270 ;; Set when we need to back up to parse this as a declaration
6271 ;; but not as a cast.
6273 ;; For casts, the return position.
6275 ;; Save `c-record-type-identifiers' and
6276 ;; `c-record-ref-identifiers' since ranges are recorded
6277 ;; speculatively and should be thrown away if it turns out
6278 ;; that it isn't a declaration or cast.
6279 (save-rec-type-ids c-record-type-identifiers
)
6280 (save-rec-ref-ids c-record-ref-identifiers
))
6282 (while (c-forward-annotation)
6283 (c-forward-syntactic-ws))
6285 ;; Check for a type. Unknown symbols are treated as possible
6286 ;; types, but they could also be specifiers disguised through
6287 ;; macros like __INLINE__, so we recognize both types and known
6288 ;; specifiers after them too.
6290 (let* ((start (point)) kwd-sym kwd-clause-end found-type
)
6292 ;; Look for a specifier keyword clause.
6293 (when (looking-at c-prefix-spec-kwds-re
)
6294 (if (looking-at c-typedef-key
)
6295 (setq at-typedef t
))
6296 (setq kwd-sym
(c-keyword-sym (match-string 1)))
6298 (c-forward-keyword-clause 1)
6299 (setq kwd-clause-end
(point))))
6301 (when (setq found-type
(c-forward-type t
)) ; brace-block-too
6302 ;; Found a known or possible type or a prefix of a known type.
6305 ;; Got two identifiers with nothing but whitespace
6306 ;; between them. That can only happen in declarations.
6307 (setq at-decl-or-cast
'ids
)
6309 (when (eq at-type
'found
)
6310 ;; If the previous identifier is a found type we
6311 ;; record it as a real one; it might be some sort of
6312 ;; alias for a prefix like "unsigned".
6314 (goto-char type-start
)
6315 (let ((c-promote-possible-types t
))
6316 (c-forward-type)))))
6318 (setq backup-at-type at-type
6319 backup-type-start type-start
6320 backup-id-start id-start
6324 ;; The previous ambiguous specifier/type turned out
6325 ;; to be a type since we've parsed another one after
6326 ;; it, so clear these backup flags.
6327 backup-at-type-decl nil
6328 backup-maybe-typeless nil
))
6332 ;; Handle known specifier keywords and
6333 ;; `c-decl-hangon-kwds' which can occur after known
6336 (if (c-keyword-member kwd-sym
'c-decl-hangon-kwds
)
6337 ;; It's a hang-on keyword that can occur anywhere.
6339 (setq at-decl-or-cast t
)
6341 ;; Move the identifier start position if
6342 ;; we've passed a type.
6343 (setq id-start kwd-clause-end
)
6344 ;; Otherwise treat this as a specifier and
6345 ;; move the fallback position.
6346 (setq start-pos kwd-clause-end
))
6347 (goto-char kwd-clause-end
))
6349 ;; It's an ordinary specifier so we know that
6350 ;; anything before this can't be the type.
6351 (setq backup-at-type nil
6352 start-pos kwd-clause-end
)
6355 ;; It's ambiguous whether this keyword is a
6356 ;; specifier or a type prefix, so set the backup
6357 ;; flags. (It's assumed that `c-forward-type'
6358 ;; moved further than `c-forward-keyword-clause'.)
6360 (when (c-keyword-member kwd-sym
'c-typedef-decl-kwds
)
6361 (setq backup-at-type-decl t
))
6362 (when (c-keyword-member kwd-sym
'c-typeless-decl-kwds
)
6363 (setq backup-maybe-typeless t
)))
6365 (when (c-keyword-member kwd-sym
'c-typedef-decl-kwds
)
6366 ;; This test only happens after we've scanned a type.
6367 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6368 (setq at-type-decl t
))
6369 (when (c-keyword-member kwd-sym
'c-typeless-decl-kwds
)
6370 (setq maybe-typeless t
))
6372 ;; Haven't matched a type so it's an umambiguous
6373 ;; specifier keyword and we know we're in a
6375 (setq at-decl-or-cast t
)
6377 (goto-char kwd-clause-end
))))
6379 ;; If the type isn't known we continue so that we'll jump
6380 ;; over all specifiers and type identifiers. The reason
6381 ;; to do this for a known type prefix is to make things
6382 ;; like "unsigned INT16" work.
6383 (and found-type
(not (eq found-type t
))))))
6387 ;; If a known type was found, we still need to skip over any
6388 ;; hangon keyword clauses after it. Otherwise it has already
6389 ;; been done in the loop above.
6390 (while (looking-at c-decl-hangon-key
)
6391 (c-forward-keyword-clause 1))
6392 (setq id-start
(point)))
6394 ((eq at-type
'prefix
)
6395 ;; A prefix type is itself a primitive type when it's not
6396 ;; followed by another type.
6400 ;; Got no type but set things up to continue anyway to handle
6401 ;; the various cases when a declaration doesn't start with a
6403 (setq id-start start-pos
))
6405 ((and (eq at-type
'maybe
)
6406 (c-major-mode-is 'c
++-mode
))
6407 ;; If it's C++ then check if the last "type" ends on the form
6408 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6409 ;; (con|de)structor.
6411 (let (name end-2 end-1
)
6412 (goto-char id-start
)
6413 (c-backward-syntactic-ws)
6414 (setq end-2
(point))
6416 (c-simple-skip-symbol-backward)
6419 (buffer-substring-no-properties (point) end-2
))
6420 ;; Cheating in the handling of syntactic ws below.
6421 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6423 (setq end-1
(point))
6424 (c-simple-skip-symbol-backward))
6425 (>= (point) type-start
)
6426 (equal (buffer-substring-no-properties (point) end-1
)
6428 ;; It is a (con|de)structor name. In that case the
6429 ;; declaration is typeless so zap out any preceding
6430 ;; identifier(s) that we might have taken as types.
6431 (goto-char type-start
)
6434 id-start type-start
))))))
6436 ;; Check for and step over a type decl expression after the thing
6437 ;; that is or might be a type. This can't be skipped since we
6438 ;; need the correct end position of the declarator for
6439 ;; `max-type-decl-end-*'.
6440 (let ((start (point)) (paren-depth 0) pos
6441 ;; True if there's a non-open-paren match of
6442 ;; `c-type-decl-prefix-key'.
6444 ;; True if the declarator is surrounded by a parenthesis pair.
6446 ;; True if there is an identifier in the declarator.
6448 ;; True if there's a non-close-paren match of
6449 ;; `c-type-decl-suffix-key'.
6451 ;; True if there's a prefix match outside the outermost
6452 ;; paren pair that surrounds the declarator.
6453 got-prefix-before-parens
6454 ;; True if there's a suffix match outside the outermost
6455 ;; paren pair that surrounds the declarator. The value is
6456 ;; the position of the first suffix match.
6457 got-suffix-after-parens
6458 ;; True if we've parsed the type decl to a token that is
6459 ;; known to end declarations in this context.
6461 ;; The earlier values of `at-type' and `type-start' if we've
6462 ;; shifted the type backwards.
6463 identifier-type identifier-start
6464 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6465 ;; turn it off during the name skipping below to avoid
6466 ;; getting `c-type' properties that might be bogus. That
6467 ;; can happen since we don't know if
6468 ;; `c-restricted-<>-arglists' will be correct inside the
6469 ;; arglist paren that gets entered.
6470 c-parse-and-markup-
<>-arglists
)
6472 (goto-char id-start
)
6474 ;; Skip over type decl prefix operators. (Note similar code in
6475 ;; `c-font-lock-declarators'.)
6476 (while (and (looking-at c-type-decl-prefix-key
)
6477 (if (and (c-major-mode-is 'c
++-mode
)
6478 (match-beginning 3))
6479 ;; If the second submatch matches in C++ then
6480 ;; we're looking at an identifier that's a
6481 ;; prefix only if it specifies a member pointer.
6482 (when (setq got-identifier
(c-forward-name))
6483 (if (looking-at "\\(::\\)")
6484 ;; We only check for a trailing "::" and
6485 ;; let the "*" that should follow be
6486 ;; matched in the next round.
6487 (progn (setq got-identifier nil
) t
)
6488 ;; It turned out to be the real identifier,
6493 (if (eq (char-after) ?\
()
6495 (setq paren-depth
(1+ paren-depth
))
6497 (unless got-prefix-before-parens
6498 (setq got-prefix-before-parens
(= paren-depth
0)))
6500 (goto-char (match-end 1)))
6501 (c-forward-syntactic-ws))
6503 (setq got-parens
(> paren-depth
0))
6505 ;; Skip over an identifier.
6507 (and (looking-at c-identifier-start
)
6508 (setq got-identifier
(c-forward-name))))
6510 ;; Skip over type decl suffix operators.
6511 (while (if (looking-at c-type-decl-suffix-key
)
6513 (if (eq (char-after) ?\
))
6514 (when (> paren-depth
0)
6515 (setq paren-depth
(1- paren-depth
))
6518 (when (if (save-match-data (looking-at "\\s\("))
6519 (c-safe (c-forward-sexp 1) t
)
6520 (goto-char (match-end 1))
6522 (when (and (not got-suffix-after-parens
)
6524 (setq got-suffix-after-parens
(match-beginning 0)))
6525 (setq got-suffix t
)))
6527 ;; No suffix matched. We might have matched the
6528 ;; identifier as a type and the open paren of a
6529 ;; function arglist as a type decl prefix. In that
6530 ;; case we should "backtrack": Reinterpret the last
6531 ;; type as the identifier, move out of the arglist and
6532 ;; continue searching for suffix operators.
6534 ;; Do this even if there's no preceding type, to cope
6535 ;; with old style function declarations in K&R C,
6536 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6537 ;; style declarations. That isn't applicable in an
6538 ;; arglist context, though.
6539 (when (and (= paren-depth
1)
6540 (not got-prefix-before-parens
)
6541 (not (eq at-type t
))
6544 backup-maybe-typeless
6545 (when c-recognize-typeless-decls
6547 (setq pos
(c-up-list-forward (point)))
6548 (eq (char-before pos
) ?\
)))
6549 (c-fdoc-shift-type-backward)
6553 (c-forward-syntactic-ws))
6555 (when (and (or maybe-typeless backup-maybe-typeless
)
6556 (not got-identifier
)
6559 ;; Have found no identifier but `c-typeless-decl-kwds' has
6560 ;; matched so we know we're inside a declaration. The
6561 ;; preceding type must be the identifier instead.
6562 (c-fdoc-shift-type-backward))
6566 (catch 'at-decl-or-cast
6569 (when (> paren-depth
0)
6570 ;; Encountered something inside parens that isn't matched by
6571 ;; the `c-type-decl-*' regexps, so it's not a type decl
6572 ;; expression. Try to skip out to the same paren depth to
6573 ;; not confuse the cast check below.
6574 (c-safe (goto-char (scan-lists (point) 1 paren-depth
)))
6575 ;; If we've found a specifier keyword then it's a
6576 ;; declaration regardless.
6577 (throw 'at-decl-or-cast
(eq at-decl-or-cast t
)))
6580 (looking-at (cond ((eq context
'<>) "[,>]")
6584 ;; Now we've collected info about various characteristics of
6585 ;; the construct we're looking at. Below follows a decision
6586 ;; tree based on that. It's ordered to check more certain
6587 ;; signs before less certain ones.
6593 (when (and (or at-type maybe-typeless
)
6594 (not (or got-prefix got-parens
)))
6595 ;; Got another identifier directly after the type, so it's a
6597 (throw 'at-decl-or-cast t
))
6599 (when (and got-parens
6601 (not got-suffix-after-parens
)
6604 backup-maybe-typeless
))
6605 ;; Got a declaration of the form "foo bar (gnu);" where we've
6606 ;; recognized "bar" as the type and "gnu" as the declarator.
6607 ;; In this case it's however more likely that "bar" is the
6608 ;; declarator and "gnu" a function argument or initializer (if
6609 ;; `c-recognize-paren-inits' is set), since the parens around
6610 ;; "gnu" would be superfluous if it's a declarator. Shift the
6611 ;; type one step backward.
6612 (c-fdoc-shift-type-backward)))
6614 ;; Found no identifier.
6620 (when (= (point) start
)
6621 ;; Got a plain list of identifiers. If a colon follows it's
6622 ;; a valid label. Otherwise the last one probably is the
6623 ;; declared identifier and we should back up to the previous
6624 ;; type, providing it isn't a cast.
6625 (if (and (eq (char-after) ?
:)
6626 (not (c-major-mode-is 'java-mode
)))
6627 ;; If we've found a specifier keyword then it's a
6628 ;; declaration regardless.
6629 (throw 'at-decl-or-cast
(eq at-decl-or-cast t
))
6630 (setq backup-if-not-cast t
)
6631 (throw 'at-decl-or-cast t
)))
6634 (when (and got-suffix
6637 ;; Got a plain list of identifiers followed by some suffix.
6638 ;; If this isn't a cast then the last identifier probably is
6639 ;; the declared one and we should back up to the previous
6641 (setq backup-if-not-cast t
)
6642 (throw 'at-decl-or-cast t
)))
6645 (when (eq at-type t
)
6646 ;; If the type is known we know that there can't be any
6647 ;; identifier somewhere else, and it's only in declarations in
6648 ;; e.g. function prototypes and in casts that the identifier may
6650 (throw 'at-decl-or-cast t
))
6652 (when (= (point) start
)
6653 ;; Only got a single identifier (parsed as a type so far).
6656 ;; Check that the identifier isn't at the start of an
6661 ;; Inside an arglist that contains declarations. If K&R
6662 ;; style declarations and parenthesis style initializers
6663 ;; aren't allowed then the single identifier must be a
6664 ;; type, else we require that it's known or found
6665 ;; (primitive types are handled above).
6666 (or (and (not c-recognize-knr-p
)
6667 (not c-recognize-paren-inits
))
6668 (memq at-type
'(known found
))))
6670 ;; Inside a template arglist. Accept known and found
6671 ;; types; other identifiers could just as well be
6672 ;; constants in C++.
6673 (memq at-type
'(known found
)))))
6674 (throw 'at-decl-or-cast t
)
6676 ;; Can't be a valid declaration or cast, but if we've found a
6677 ;; specifier it can't be anything else either, so treat it as
6678 ;; an invalid/unfinished declaration or cast.
6679 (throw 'at-decl-or-cast at-decl-or-cast
))))
6684 (not (eq at-type t
))
6687 backup-maybe-typeless
6688 (when c-recognize-typeless-decls
6689 (or (not got-suffix
)
6691 c-after-suffixed-type-maybe-decl-key
))))))
6692 ;; Got an empty paren pair and a preceding type that probably
6693 ;; really is the identifier. Shift the type backwards to make
6694 ;; the last one the identifier. This is analogous to the
6695 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6698 ;; Exception: In addition to the conditions in that
6699 ;; "backtracking" code, do not shift backward if we're not
6700 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6701 ;; Since there's no preceding type, the shift would mean that
6702 ;; the declaration is typeless. But if the regexp doesn't match
6703 ;; then we will simply fall through in the tests below and not
6704 ;; recognize it at all, so it's better to try it as an abstract
6705 ;; declarator instead.
6706 (c-fdoc-shift-type-backward)
6708 ;; Still no identifier.
6710 (when (and got-prefix
(or got-parens got-suffix
))
6711 ;; Require `got-prefix' together with either `got-parens' or
6712 ;; `got-suffix' to recognize it as an abstract declarator:
6713 ;; `got-parens' only is probably an empty function call.
6714 ;; `got-suffix' only can build an ordinary expression together
6715 ;; with the preceding identifier which we've taken as a type.
6716 ;; We could actually accept on `got-prefix' only, but that can
6717 ;; easily occur temporarily while writing an expression so we
6718 ;; avoid that case anyway. We could do a better job if we knew
6719 ;; the point when the fontification was invoked.
6720 (throw 'at-decl-or-cast t
))
6726 got-suffix-after-parens
6727 (eq (char-after got-suffix-after-parens
) ?\
())
6728 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6729 ;; normal function call afterall (or perhaps a C++ style object
6730 ;; instantiation expression).
6731 (throw 'at-decl-or-cast nil
))))
6734 (when at-decl-or-cast
6735 ;; By now we've located the type in the declaration that we know
6737 (throw 'at-decl-or-cast t
))
6740 (when (and got-identifier
6742 (looking-at c-after-suffixed-type-decl-key
)
6746 (not (eq at-type t
)))
6747 ;; Shift the type backward in the case that there's a
6748 ;; single identifier inside parens. That can only
6749 ;; occur in K&R style function declarations so it's
6750 ;; more likely that it really is a function call.
6751 ;; Therefore we only do this after
6752 ;; `c-after-suffixed-type-decl-key' has matched.
6753 (progn (c-fdoc-shift-type-backward) t
)
6754 got-suffix-after-parens
))
6755 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6756 (throw 'at-decl-or-cast t
))
6759 (when (and (or got-prefix
(not got-parens
))
6760 (memq at-type
'(t known
)))
6761 ;; It's a declaration if a known type precedes it and it can't be a
6763 (throw 'at-decl-or-cast t
))
6765 ;; If we get here we can't tell if this is a type decl or a normal
6766 ;; expression by looking at it alone. (That's under the assumption
6767 ;; that normal expressions always can look like type decl expressions,
6768 ;; which isn't really true but the cases where it doesn't hold are so
6769 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6770 ;; the effort to look for them.)
6772 (unless (or at-decl-end
(looking-at "=[^=]"))
6773 ;; If this is a declaration it should end here or its initializer(*)
6774 ;; should start here, so check for allowed separation tokens. Note
6775 ;; that this rule doesn't work e.g. with a K&R arglist after a
6778 ;; *) Don't check for C++ style initializers using parens
6779 ;; since those already have been matched as suffixes.
6781 ;; If `at-decl-or-cast' is then we've found some other sign that
6782 ;; it's a declaration or cast, so then it's probably an
6783 ;; invalid/unfinished one.
6784 (throw 'at-decl-or-cast at-decl-or-cast
))
6786 ;; Below are tests that only should be applied when we're certain to
6787 ;; not have parsed halfway through an expression.
6790 (when (memq at-type
'(t known
))
6791 ;; The expression starts with a known type so treat it as a
6793 (throw 'at-decl-or-cast t
))
6796 (when (and (c-major-mode-is 'c
++-mode
)
6797 ;; In C++ we check if the identifier is a known type, since
6798 ;; (con|de)structors use the class name as identifier.
6799 ;; We've always shifted over the identifier as a type and
6800 ;; then backed up again in this case.
6802 (or (memq identifier-type
'(found known
))
6803 (and (eq (char-after identifier-start
) ?~
)
6804 ;; `at-type' probably won't be 'found for
6805 ;; destructors since the "~" is then part of the
6806 ;; type name being checked against the list of
6807 ;; known types, so do a check without that
6810 (goto-char (1+ identifier-start
))
6811 (c-forward-syntactic-ws)
6812 (c-with-syntax-table
6813 c-identifier-syntax-table
6814 (looking-at c-known-type-key
)))
6816 (goto-char (1+ identifier-start
))
6817 ;; We have already parsed the type earlier,
6818 ;; so it'd be possible to cache the end
6819 ;; position instead of redoing it here, but
6820 ;; then we'd need to keep track of another
6821 ;; position everywhere.
6822 (c-check-type (point)
6823 (progn (c-forward-type)
6825 (throw 'at-decl-or-cast t
))
6830 (when (and got-prefix-before-parens
6832 (or at-decl-end
(looking-at "=[^=]"))
6835 ;; Got something like "foo * bar;". Since we're not inside an
6836 ;; arglist it would be a meaningless expression because the
6837 ;; result isn't used. We therefore choose to recognize it as
6838 ;; a declaration. Do not allow a suffix since it could then
6839 ;; be a function call.
6840 (throw 'at-decl-or-cast t
))
6843 (when (and (or got-suffix-after-parens
6844 (looking-at "=[^=]"))
6846 (not (eq context
'arglist
)))
6847 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
6848 ;; be an odd expression or it could be a declaration. Treat
6849 ;; it as a declaration if "a" has been used as a type
6850 ;; somewhere else (if it's a known type we won't get here).
6851 (throw 'at-decl-or-cast t
)))
6856 (and (eq context
'decl
)
6857 (not c-recognize-paren-inits
)
6858 (or got-parens got-suffix
))))
6859 ;; Got a type followed by an abstract declarator. If `got-prefix'
6860 ;; is set it's something like "a *" without anything after it. If
6861 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
6862 ;; or similar, which we accept only if the context rules out
6864 (throw 'at-decl-or-cast t
)))
6866 ;; If we had a complete symbol table here (which rules out
6867 ;; `c-found-types') we should return t due to the disambiguation rule
6868 ;; (in at least C++) that anything that can be parsed as a declaration
6869 ;; is a declaration. Now we're being more defensive and prefer to
6870 ;; highlight things like "foo (bar);" as a declaration only if we're
6871 ;; inside an arglist that contains declarations.
6872 (eq context
'decl
))))
6874 ;; The point is now after the type decl expression.
6877 ;; Check for a cast.
6882 ;; Should be the first type/identifier in a cast paren.
6883 (> preceding-token-end
(point-min))
6884 (memq (char-before preceding-token-end
) c-cast-parens
)
6886 ;; The closing paren should follow.
6888 (c-forward-syntactic-ws)
6889 (looking-at "\\s\)"))
6891 ;; There should be a primary expression after it.
6894 (c-forward-syntactic-ws)
6895 (setq cast-end
(point))
6896 (and (looking-at c-primary-expr-regexp
)
6898 (setq pos
(match-end 0))
6900 ;; Check if the expression begins with a prefix keyword.
6902 (if (match-beginning 1)
6903 ;; Expression begins with an ambiguous operator. Treat
6904 ;; it as a cast if it's a type decl or if we've
6905 ;; recognized the type somewhere else.
6907 (memq at-type
'(t known found
)))
6908 ;; Unless it's a keyword, it's the beginning of a primary
6910 (not (looking-at c-keywords-regexp
)))))
6911 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
6912 ;; that it matched a whole one so that we don't e.g. confuse
6913 ;; the operator '-' with '->'. It's ok if it matches further,
6914 ;; though, since it e.g. can match the float '.5' while the
6915 ;; operator regexp only matches '.'.
6916 (or (not (looking-at c-nonsymbol-token-regexp
))
6917 (<= (match-end 0) pos
))))
6919 ;; There should either be a cast before it or something that isn't an
6920 ;; identifier or close paren.
6921 (> preceding-token-end
(point-min))
6923 (goto-char (1- preceding-token-end
))
6924 (or (eq (point) last-cast-end
)
6926 (c-backward-syntactic-ws)
6927 (if (< (skip-syntax-backward "w_") 0)
6928 ;; It's a symbol. Accept it only if it's one of the
6929 ;; keywords that can precede an expression (without
6930 ;; surrounding parens).
6931 (looking-at c-simple-stmt-key
)
6933 ;; Check that it isn't a close paren (block close is ok,
6935 (not (memq (char-before) '(?\
) ?\
])))
6936 ;; Check that it isn't a nonsymbol identifier.
6937 (not (c-on-identifier)))))))))
6940 (when (and c-record-type-identifiers at-type
(not (eq at-type t
)))
6941 (let ((c-promote-possible-types t
))
6942 (goto-char type-start
)
6945 (goto-char cast-end
)
6949 ;; We're at a declaration. Highlight the type and the following
6952 (when backup-if-not-cast
6953 (c-fdoc-shift-type-backward t
))
6955 (when (and (eq context
'decl
) (looking-at ","))
6956 ;; Make sure to propagate the `c-decl-arg-start' property to
6957 ;; the next argument if it's set in this one, to cope with
6958 ;; interactive refontification.
6959 (c-put-c-type-property (point) 'c-decl-arg-start
))
6961 (when (and c-record-type-identifiers at-type
(not (eq at-type t
)))
6962 (let ((c-promote-possible-types t
))
6964 (goto-char type-start
)
6968 (and (or at-type-decl at-typedef
)
6969 (cons at-type-decl at-typedef
))))
6972 ;; False alarm. Restore the recorded ranges.
6973 (setq c-record-type-identifiers save-rec-type-ids
6974 c-record-ref-identifiers save-rec-ref-ids
)
6977 (defun c-forward-label (&optional assume-markup preceding-token-end limit
)
6978 ;; Assuming that point is at the beginning of a token, check if it starts a
6979 ;; label and if so move over it and return non-nil (t in default situations,
6980 ;; specific symbols (see below) for interesting situations), otherwise don't
6981 ;; move and return nil. "Label" here means "most things with a colon".
6983 ;; More precisely, a "label" is regarded as one of:
6984 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
6985 ;; (ii) A case label - either the entire construct "case FOO:", or just the
6986 ;; bare "case", should the colon be missing. We return t;
6987 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
6989 ;; (iv) One of QT's "extended" C++ variants of
6990 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
6991 ;; Returns the symbol `qt-2kwds-colon'.
6992 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
6993 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
6994 ;; colon). Currently (2006-03), this applies only to Objective C's
6995 ;; keywords "@private", "@protected", and "@public". Returns t.
6997 ;; One of the things which will NOT be recognised as a label is a bit-field
6998 ;; element of a struct, something like "int foo:5".
7000 ;; The end of the label is taken to be just after the colon, or the end of
7001 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7002 ;; after the end on return. The terminating char gets marked with
7003 ;; `c-decl-end' to improve recognition of the following declaration or
7006 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7007 ;; label, if any, has already been marked up like that.
7009 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7010 ;; after the preceding token, i.e. on the other side of the
7011 ;; syntactic ws from the point. Use a value less than or equal to
7012 ;; (point-min) if the point is at the first token in (the visible
7013 ;; part of) the buffer.
7015 ;; The optional LIMIT limits the forward scan for the colon.
7017 ;; This function records the ranges of the label symbols on
7018 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7021 ;; This function might do hidden buffer changes.
7023 (let ((start (point))
7026 macro-start
; if we're in one.
7030 ;; "case" or "default" (Doesn't apply to AWK).
7031 ((looking-at c-label-kwds-regexp
)
7032 (let ((kwd-end (match-end 1)))
7033 ;; Record only the keyword itself for fontification, since in
7034 ;; case labels the following is a constant expression and not
7036 (when c-record-type-identifiers
7037 (c-record-ref-id (cons (match-beginning 1) kwd-end
)))
7039 ;; Find the label end.
7042 (if (and (c-syntactic-re-search-forward
7043 ;; Stop on chars that aren't allowed in expressions,
7044 ;; and on operator chars that would be meaningless
7045 ;; there. FIXME: This doesn't cope with ?: operators.
7046 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7048 (match-beginning 2))
7050 (progn ; there's a proper :
7051 (goto-char (match-beginning 2)) ; just after the :
7052 (c-put-c-type-property (1- (point)) 'c-decl-end
)
7055 ;; It's an unfinished label. We consider the keyword enough
7056 ;; to recognize it as a label, so that it gets fontified.
7057 ;; Leave the point at the end of it, but don't put any
7058 ;; `c-decl-end' marker.
7062 ;; @private, @protected, @public, in Objective C, or similar.
7063 ((and c-opt-extra-label-key
7064 (looking-at c-opt-extra-label-key
))
7065 ;; For a `c-opt-extra-label-key' match, we record the whole
7066 ;; thing for fontification. That's to get the leading '@' in
7067 ;; Objective-C protection labels fontified.
7068 (goto-char (match-end 1))
7069 (when c-record-type-identifiers
7070 (c-record-ref-id (cons (match-beginning 1) (point))))
7071 (c-put-c-type-property (1- (point)) 'c-decl-end
)
7072 (setq label-type t
))
7074 ;; All other cases of labels.
7075 ((and c-recognize-colon-labels
; nil for AWK and IDL, otherwise t.
7077 ;; A colon label must have something before the colon.
7078 (not (eq (char-after) ?
:))
7080 ;; Check that we're not after a token that can't precede a label.
7082 ;; Trivially succeeds when there's no preceding token.
7083 (if preceding-token-end
7084 (<= preceding-token-end
(point-min))
7086 (c-backward-syntactic-ws)
7087 (setq preceding-token-end
(point))
7090 ;; Check if we're after a label, if we're after a closing
7091 ;; paren that belong to statement, and with
7092 ;; `c-label-prefix-re'. It's done in different order
7093 ;; depending on `assume-markup' since the checks have
7094 ;; different expensiveness.
7097 (eq (c-get-char-property (1- preceding-token-end
) 'c-type
)
7101 (goto-char (1- preceding-token-end
))
7102 (c-beginning-of-current-token)
7103 (or (looking-at c-label-prefix-re
)
7104 (looking-at c-block-stmt-1-key
)))
7106 (and (eq (char-before preceding-token-end
) ?\
))
7107 (c-after-conditional)))
7111 (goto-char (1- preceding-token-end
))
7112 (c-beginning-of-current-token)
7113 (or (looking-at c-label-prefix-re
)
7114 (looking-at c-block-stmt-1-key
)))
7117 ((eq (char-before preceding-token-end
) ?\
))
7118 (c-after-conditional))
7120 ((eq (char-before preceding-token-end
) ?
:)
7121 ;; Might be after another label, so check it recursively.
7124 (goto-char (1- preceding-token-end
))
7125 ;; Essentially the same as the
7126 ;; `c-syntactic-re-search-forward' regexp below.
7128 (save-excursion (and (c-beginning-of-macro)
7130 (if macro-start
(narrow-to-region macro-start
(point-max)))
7131 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t
)
7132 ;; Note: the following should work instead of the
7133 ;; narrow-to-region above. Investigate why not,
7134 ;; sometime. ACM, 2006-03-31.
7135 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7138 ;; If the caller turned on recording for us,
7139 ;; it shouldn't apply when we check the
7141 c-record-type-identifiers
)
7142 ;; A label can't start at a cpp directive. Check for
7143 ;; this, since c-forward-syntactic-ws would foul up on it.
7144 (unless (and c-opt-cpp-prefix
(looking-at c-opt-cpp-prefix
))
7145 (c-forward-syntactic-ws)
7146 (c-forward-label nil pte start
))))))))))
7148 ;; Point is still at the beginning of the possible label construct.
7150 ;; Check that the next nonsymbol token is ":", or that we're in one
7151 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7152 ;; arguments. FIXME: Should build this regexp from the language
7155 ;; public: protected: private:
7157 (c-major-mode-is 'c
++-mode
)
7158 (search-forward-regexp
7159 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t
)
7160 (progn (backward-char)
7161 (c-forward-syntactic-ws limit
)
7162 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7164 (setq label-type t
))
7165 ;; QT double keyword like "protected slots:" or goto target.
7166 ((progn (goto-char start
) nil
))
7167 ((when (c-syntactic-re-search-forward
7168 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t
) ; not at EOB
7170 (setq label-end
(point))
7172 (and (c-major-mode-is 'c
++-mode
)
7174 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7175 (buffer-substring start
(point)))))
7176 (c-forward-syntactic-ws limit
)
7178 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7181 (if (or (string= "signals" ; Special QT macro
7182 (setq kwd
(buffer-substring-no-properties start label-end
)))
7183 (string= "Q_SIGNALS" kwd
))
7187 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t
)
7188 (progn (c-forward-syntactic-ws limit
)
7189 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7191 (setq label-type
'qt-2kwds-colon
)))))))
7194 (narrow-to-region start
(point))
7196 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7200 (when (looking-at c-nonlabel-token-key
)
7202 (setq label-type nil
)
7203 (throw 'check-label nil
))
7204 (and (c-safe (c-forward-sexp)
7205 (c-forward-syntactic-ws)
7209 ;; Record the identifiers in the label for fontification, unless
7210 ;; it begins with `c-label-kwds' in which case the following
7211 ;; identifiers are part of a (constant) expression that
7212 ;; shouldn't be fontified.
7213 (when (and c-record-type-identifiers
7214 (progn (goto-char start
)
7215 (not (looking-at c-label-kwds-regexp
))))
7216 (while (c-syntactic-re-search-forward c-symbol-key nil t
)
7217 (c-record-ref-id (cons (match-beginning 0)
7220 (c-put-c-type-property (1- (point-max)) 'c-decl-end
)
7221 (goto-char (point-max)))))
7228 (defun c-forward-objc-directive ()
7229 ;; Assuming the point is at the beginning of a token, try to move
7230 ;; forward to the end of the Objective-C directive that starts
7231 ;; there. Return t if a directive was fully recognized, otherwise
7232 ;; the point is moved as far as one could be successfully parsed and
7235 ;; This function records identifier ranges on
7236 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7237 ;; `c-record-type-identifiers' is non-nil.
7239 ;; This function might do hidden buffer changes.
7241 (let ((start (point))
7243 (c-promote-possible-types t
)
7244 ;; Turn off recognition of angle bracket arglists while parsing
7245 ;; types here since the protocol reference list might then be
7246 ;; considered part of the preceding name or superclass-name.
7247 c-recognize-
<>-arglists
)
7252 (c-make-keywords-re t
7253 (append (c-lang-const c-protection-kwds objc
)
7256 (goto-char (match-end 1))
7262 (c-make-keywords-re t
7263 '("@interface" "@implementation" "@protocol")
7266 ;; Handle the name of the class itself.
7268 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7270 (goto-char (match-end 0))
7275 ;; Look for ": superclass-name" or "( category-name )".
7276 (when (looking-at "[:\(]")
7277 (setq start-char
(char-after))
7279 (c-forward-syntactic-ws)
7280 (unless (c-forward-type) (throw 'break nil
))
7281 (when (eq start-char ?\
()
7282 (unless (eq (char-after) ?\
)) (throw 'break nil
))
7284 (c-forward-syntactic-ws)))
7286 ;; Look for a protocol reference list.
7287 (if (eq (char-after) ?
<)
7288 (let ((c-recognize-<>-arglists t
)
7289 (c-parse-and-markup-<>-arglists t
)
7290 c-restricted-
<>-arglists
)
7291 (c-forward-<>-arglist t
))
7295 (c-backward-syntactic-ws)
7296 (c-clear-c-type-property start
(1- (point)) 'c-decl-end
)
7297 (c-put-c-type-property (1- (point)) 'c-decl-end
)
7300 (c-clear-c-type-property start
(point) 'c-decl-end
)
7303 (defun c-beginning-of-inheritance-list (&optional lim
)
7304 ;; Go to the first non-whitespace after the colon that starts a
7305 ;; multiple inheritance introduction. Optional LIM is the farthest
7306 ;; back we should search.
7308 ;; This function might do hidden buffer changes.
7309 (c-with-syntax-table c
++-template-syntax-table
7310 (c-backward-token-2 0 t lim
)
7311 (while (and (or (looking-at c-symbol-start
)
7312 (looking-at "[<,]\\|::"))
7313 (zerop (c-backward-token-2 1 t lim
))))))
7315 (defun c-in-method-def-p ()
7316 ;; Return nil if we aren't in a method definition, otherwise the
7317 ;; position of the initial [+-].
7319 ;; This function might do hidden buffer changes.
7322 (and c-opt-method-key
7323 (looking-at c-opt-method-key
)
7327 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7328 (defun c-in-gcc-asm-p ()
7329 ;; Return non-nil if point is within a gcc \"asm\" block.
7331 ;; This should be called with point inside an argument list.
7333 ;; Only one level of enclosing parentheses is considered, so for
7334 ;; instance `nil' is returned when in a function call within an asm
7337 ;; This function might do hidden buffer changes.
7339 (and c-opt-asm-stmt-key
7342 (backward-up-list 1)
7343 (c-beginning-of-statement-1 (point-min) nil t
)
7344 (looking-at c-opt-asm-stmt-key
))))
7346 (defun c-at-toplevel-p ()
7347 "Return a determination as to whether point is \"at the top level\".
7348 Informally, \"at the top level\" is anywhere where you can write
7351 More precisely, being at the top-level means that point is either
7352 outside any enclosing block (such as a function definition), or
7353 directly inside a class, namespace or other block that contains
7354 another declaration level.
7356 If point is not at the top-level (e.g. it is inside a method
7357 definition), then nil is returned. Otherwise, if point is at a
7358 top-level not enclosed within a class definition, t is returned.
7359 Otherwise, a 2-vector is returned where the zeroth element is the
7360 buffer position of the start of the class declaration, and the first
7361 element is the buffer position of the enclosing class's opening
7364 Note that this function might do hidden buffer changes. See the
7365 comment at the start of cc-engine.el for more info."
7366 (let ((paren-state (c-parse-state)))
7367 (or (not (c-most-enclosing-brace paren-state
))
7368 (c-search-uplist-for-classkey paren-state
))))
7370 (defun c-just-after-func-arglist-p (&optional lim
)
7371 ;; Return non-nil if the point is in the region after the argument
7372 ;; list of a function and its opening brace (or semicolon in case it
7373 ;; got no body). If there are K&R style argument declarations in
7374 ;; that region, the point has to be inside the first one for this
7375 ;; function to recognize it.
7377 ;; If successful, the point is moved to the first token after the
7378 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7379 ;; the position of the opening paren of the function arglist is
7382 ;; The point is clobbered if not successful.
7384 ;; LIM is used as bound for backward buffer searches.
7386 ;; This function might do hidden buffer changes.
7388 (let ((beg (point)) end id-start
)
7390 (eq (c-beginning-of-statement-1 lim
) 'same
)
7392 (not (or (c-major-mode-is 'objc-mode
)
7393 (c-forward-objc-directive)))
7396 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
)))
7399 ;; There should not be a '=' or ',' between beg and the
7400 ;; start of the declaration since that means we were in the
7401 ;; "expression part" of the declaration.
7403 (not (looking-at "[=,]")))
7406 ;; Check that there's an arglist paren in the
7408 (goto-char id-start
)
7409 (cond ((eq (char-after) ?\
()
7410 ;; The declarator is a paren expression, so skip past it
7411 ;; so that we don't get stuck on that instead of the
7412 ;; function arglist.
7414 ((and c-opt-op-identifier-prefix
7415 (looking-at c-opt-op-identifier-prefix
))
7416 ;; Don't trip up on "operator ()".
7417 (c-forward-token-2 2 t
)))
7418 (and (< (point) beg
)
7419 (c-syntactic-re-search-forward "(" beg t t
)
7422 (defun c-in-knr-argdecl (&optional lim
)
7423 ;; Return the position of the first argument declaration if point is
7424 ;; inside a K&R style argument declaration list, nil otherwise.
7425 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7426 ;; position that bounds the backward search for the argument list.
7428 ;; Point must be within a possible K&R region, e.g. just before a top-level
7429 ;; "{". It must be outside of parens and brackets. The test can return
7430 ;; false positives otherwise.
7432 ;; This function might do hidden buffer changes.
7436 ;; If we're in a macro, our search range is restricted to it. Narrow to
7437 ;; the searchable range.
7438 (let* ((macro-start (c-query-macro-start))
7439 (lim (max (or lim
(point-min)) (or macro-start
(point-min))))
7440 before-lparen after-rparen
7441 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
7442 (narrow-to-region lim
(c-point 'eol
))
7444 ;; Search backwards for the defun's argument list. We give up if we
7445 ;; encounter a "}" (end of a previous defun) or BOB.
7447 ;; The criterion for a paren structure being the arg list is:
7448 ;; o - there is non-WS stuff after it but before any "{"; AND
7449 ;; o - the token after it isn't a ";" AND
7450 ;; o - it is preceded by either an identifier (the function name) or
7451 ;; a macro expansion like "DEFUN (...)"; AND
7452 ;; o - its content is a non-empty comma-separated list of identifiers
7453 ;; (an empty arg list won't have a knr region).
7455 ;; The following snippet illustrates these rules:
7456 ;; int foo (bar, baz, yuk)
7458 ;; int (*baz) (my_type) ;
7459 ;; int (*) (void) (*yuk) (void) ;
7463 (while (> pp-count-out
0) ; go back one paren/bracket pair each time.
7464 (setq pp-count-out
(1- pp-count-out
))
7465 (c-syntactic-skip-backward "^)]}")
7466 (cond ((eq (char-before) ?\
))
7467 (setq after-rparen
(point)))
7468 ((eq (char-before) ?\
])
7469 (setq after-rparen nil
))
7470 (t ; either } (hit previous defun) or no more parens/brackets
7474 ;; We're inside a paren. Could it be our argument list....?
7478 (goto-char after-rparen
)
7479 (unless (c-go-list-backward) (throw 'knr nil
)) ;
7480 ;; FIXME!!! What about macros between the parens? 2007/01/20
7481 (setq before-lparen
(point)))
7483 ;; It can't be the arg list if next token is ; or {
7484 (progn (goto-char after-rparen
)
7485 (c-forward-syntactic-ws)
7486 (not (memq (char-after) '(?\
; ?\{))))
7488 ;; Is the thing preceding the list an identifier (the
7489 ;; function name), or a macro expansion?
7491 (goto-char before-lparen
)
7492 (eq (c-backward-token-2) 0)
7493 (or (c-on-identifier)
7494 (and (eq (char-after) ?\
))
7495 (c-go-up-list-backward)
7496 (eq (c-backward-token-2) 0)
7497 (c-on-identifier))))
7499 ;; Have we got a non-empty list of comma-separated
7502 (goto-char before-lparen
)
7503 (c-forward-token-2) ; to first token inside parens
7508 (while (eq (char-after) ?\
,)
7510 (unless (c-on-identifier) (throw 'id-list nil
))
7511 (c-forward-token-2))
7512 (eq (char-after) ?\
))))))
7514 ;; ...Yes. We've identified the function's argument list.
7516 (progn (goto-char after-rparen
)
7517 (c-forward-syntactic-ws)
7520 ;; ...No. The current parens aren't the function's arg list.
7521 (goto-char before-lparen
))
7523 (or (c-go-list-backward) ; backwards over [ .... ]
7524 (throw 'knr nil
)))))))))
7526 (defun c-skip-conditional ()
7527 ;; skip forward over conditional at point, including any predicate
7528 ;; statements in parentheses. No error checking is performed.
7530 ;; This function might do hidden buffer changes.
7531 (c-forward-sexp (cond
7533 ((looking-at (concat "\\<else"
7534 "\\([ \t\n]\\|\\\\\n\\)+"
7535 "if\\>\\([^_]\\|$\\)"))
7537 ;; do, else, try, finally
7538 ((looking-at (concat "\\<\\("
7539 "do\\|else\\|try\\|finally"
7540 "\\)\\>\\([^_]\\|$\\)"))
7542 ;; for, if, while, switch, catch, synchronized, foreach
7545 (defun c-after-conditional (&optional lim
)
7546 ;; If looking at the token after a conditional then return the
7547 ;; position of its start, otherwise return nil.
7549 ;; This function might do hidden buffer changes.
7551 (and (zerop (c-backward-token-2 1 t lim
))
7552 (or (looking-at c-block-stmt-1-key
)
7553 (and (eq (char-after) ?\
()
7554 (zerop (c-backward-token-2 1 t lim
))
7555 (looking-at c-block-stmt-2-key
)))
7558 (defun c-after-special-operator-id (&optional lim
)
7559 ;; If the point is after an operator identifier that isn't handled
7560 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7561 ;; position of the start of that identifier is returned. nil is
7562 ;; returned otherwise. The point may be anywhere in the syntactic
7563 ;; whitespace after the last token of the operator identifier.
7565 ;; This function might do hidden buffer changes.
7567 (and c-overloadable-operators-regexp
7568 (zerop (c-backward-token-2 1 nil lim
))
7569 (looking-at c-overloadable-operators-regexp
)
7570 (or (not c-opt-op-identifier-prefix
)
7572 (zerop (c-backward-token-2 1 nil lim
))
7573 (looking-at c-opt-op-identifier-prefix
)))
7576 (defsubst c-backward-to-block-anchor
(&optional lim
)
7577 ;; Assuming point is at a brace that opens a statement block of some
7578 ;; kind, move to the proper anchor point for that block. It might
7579 ;; need to be adjusted further by c-add-stmt-syntax, but the
7580 ;; position at return is suitable as start position for that
7583 ;; This function might do hidden buffer changes.
7584 (unless (= (point) (c-point 'boi
))
7585 (let ((start (c-after-conditional lim
)))
7587 (goto-char start
)))))
7589 (defsubst c-backward-to-decl-anchor
(&optional lim
)
7590 ;; Assuming point is at a brace that opens the block of a top level
7591 ;; declaration of some kind, move to the proper anchor point for
7594 ;; This function might do hidden buffer changes.
7595 (unless (= (point) (c-point 'boi
))
7596 (c-beginning-of-statement-1 lim
)))
7598 (defun c-search-decl-header-end ()
7599 ;; Search forward for the end of the "header" of the current
7600 ;; declaration. That's the position where the definition body
7601 ;; starts, or the first variable initializer, or the ending
7602 ;; semicolon. I.e. search forward for the closest following
7603 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7604 ;; _after_ the first found token, or at point-max if none is found.
7606 ;; This function might do hidden buffer changes.
7608 (let ((base (point)))
7609 (if (c-major-mode-is 'c
++-mode
)
7611 ;; In C++ we need to take special care to handle operator
7612 ;; tokens and those pesky template brackets.
7614 (c-syntactic-re-search-forward "[;{<=]" nil
'move t t
)
7616 (c-end-of-current-token base
)
7617 ;; Handle operator identifiers, i.e. ignore any
7618 ;; operator token preceded by "operator".
7620 (and (c-safe (c-backward-sexp) t
)
7621 (looking-at c-opt-op-identifier-prefix
)))
7622 (and (eq (char-before) ?
<)
7623 (c-with-syntax-table c
++-template-syntax-table
7624 (if (c-safe (goto-char (c-up-list-forward (point))))
7626 (goto-char (point-max))
7628 (setq base
(point)))
7631 (c-syntactic-re-search-forward "[;{=]" nil
'move t t
)
7632 (c-end-of-current-token base
))
7633 (setq base
(point))))))
7635 (defun c-beginning-of-decl-1 (&optional lim
)
7636 ;; Go to the beginning of the current declaration, or the beginning
7637 ;; of the previous one if already at the start of it. Point won't
7638 ;; be moved out of any surrounding paren. Return a cons cell of the
7639 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7640 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7641 ;; style argument declarations (and they are to be recognized) then
7642 ;; KNR-POS is set to the start of the first such argument
7643 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7644 ;; position that bounds the backward search.
7646 ;; NB: Cases where the declaration continues after the block, as in
7647 ;; "struct foo { ... } bar;", are currently recognized as two
7648 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7650 ;; This function might do hidden buffer changes.
7652 (let* ((start (point))
7653 (last-stmt-start (point))
7654 (move (c-beginning-of-statement-1 lim nil t
)))
7656 ;; `c-beginning-of-statement-1' stops at a block start, but we
7657 ;; want to continue if the block doesn't begin a top level
7658 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7659 ;; or an open paren.
7660 (let ((beg (point)) tentative-move
)
7661 ;; Go back one "statement" each time round the loop until we're just
7662 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7663 ;; an ObjC method. This will move over a multiple declaration whose
7664 ;; components are comma separated.
7666 ;; Must check with c-opt-method-key in ObjC mode.
7667 (not (and c-opt-method-key
7668 (looking-at c-opt-method-key
)))
7669 (/= last-stmt-start
(point))
7671 (c-backward-syntactic-ws lim
)
7672 (not (memq (char-before) '(?\
; ?} ?: nil))))
7675 (not (looking-at "\\s(")))
7676 ;; Check that we don't move from the first thing in a
7677 ;; macro to its header.
7678 (not (eq (setq tentative-move
7679 (c-beginning-of-statement-1 lim nil t
))
7681 (setq last-stmt-start beg
7683 move tentative-move
))
7686 (when c-recognize-knr-p
7687 (let ((fallback-pos (point)) knr-argdecl-start
)
7688 ;; Handle K&R argdecls. Back up after the "statement" jumped
7689 ;; over by `c-beginning-of-statement-1', unless it was the
7690 ;; function body, in which case we're sitting on the opening
7691 ;; brace now. Then test if we're in a K&R argdecl region and
7692 ;; that we started at the other side of the first argdecl in
7694 (unless (eq (char-after) ?
{)
7695 (goto-char last-stmt-start
))
7696 (if (and (setq knr-argdecl-start
(c-in-knr-argdecl lim
))
7697 (< knr-argdecl-start start
)
7699 (goto-char knr-argdecl-start
)
7700 (not (eq (c-beginning-of-statement-1 lim nil t
) 'macro
))))
7702 (cons (if (eq (char-after fallback-pos
) ?
{)
7706 (goto-char fallback-pos
))))
7708 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7709 ;; statement, so the result will be 'previous if we've moved over any.
7710 ;; So change our result back to 'same if necessary.
7712 ;; If they were brace list initializers we might not have moved over a
7713 ;; declaration boundary though, so change it to 'same if we've moved
7714 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7715 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7716 ;; potentially can search over a large amount of text.). Take special
7717 ;; pains not to get mislead by C++'s "operator=", and the like.
7718 (if (and (eq move
'previous
)
7719 (c-with-syntax-table (if (c-major-mode-is 'c
++-mode
)
7720 c
++-template-syntax-table
7725 (while ; keep going back to "[;={"s until we either find
7726 ; no more, or get to one which isn't an "operator ="
7727 (and (c-syntactic-re-search-forward "[;={]" start t t t
)
7728 (eq (char-before) ?
=)
7729 c-overloadable-operators-regexp
7730 c-opt-op-identifier-prefix
7732 (eq (c-backward-token-2) 0)
7733 (looking-at c-overloadable-operators-regexp
)
7734 (eq (c-backward-token-2) 0)
7735 (looking-at c-opt-op-identifier-prefix
))))
7736 (eq (char-before) ?
=))
7737 (c-syntactic-re-search-forward "[;{]" start t t
)
7738 (eq (char-before) ?
{)
7739 (c-safe (goto-char (c-up-list-forward (point))) t
)
7740 (not (c-syntactic-re-search-forward ";" start t t
))))))
7744 (defun c-end-of-decl-1 ()
7745 ;; Assuming point is at the start of a declaration (as detected by
7746 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7747 ;; `c-beginning-of-decl-1', this function handles the case when a
7748 ;; block is followed by identifiers in e.g. struct declarations in C
7749 ;; or C++. If a proper end was found then t is returned, otherwise
7750 ;; point is moved as far as possible within the current sexp and nil
7751 ;; is returned. This function doesn't handle macros; use
7752 ;; `c-end-of-macro' instead in those cases.
7754 ;; This function might do hidden buffer changes.
7755 (let ((start (point))
7756 (decl-syntax-table (if (c-major-mode-is 'c
++-mode
)
7757 c
++-template-syntax-table
7760 (c-search-decl-header-end)
7762 (when (and c-recognize-knr-p
7763 (eq (char-before) ?\
;)
7764 (c-in-knr-argdecl start
))
7765 ;; Stopped at the ';' in a K&R argdecl section which is
7766 ;; detected using the same criteria as in
7767 ;; `c-beginning-of-decl-1'. Move to the following block
7769 (c-syntactic-re-search-forward "{" nil
'move t
))
7771 (when (eq (char-before) ?
{)
7772 ;; Encountered a block in the declaration. Jump over it.
7774 (goto-char (c-up-list-forward (point)))
7775 (error (goto-char (point-max))
7776 (throw 'return nil
)))
7777 (if (or (not c-opt-block-decls-with-vars-key
)
7779 (c-with-syntax-table decl-syntax-table
7780 (let ((lim (point)))
7783 ;; Check for `c-opt-block-decls-with-vars-key'
7784 ;; before the first paren.
7785 (c-syntactic-re-search-forward
7786 (concat "[;=\(\[{]\\|\\("
7787 c-opt-block-decls-with-vars-key
7791 (not (eq (char-before) ?_
))
7792 ;; Check that the first following paren is
7794 (c-syntactic-re-search-forward "[;=\(\[{]"
7796 (eq (char-before) ?
{)))))))
7797 ;; The declaration doesn't have any of the
7798 ;; `c-opt-block-decls-with-vars' keywords in the
7799 ;; beginning, so it ends here at the end of the block.
7802 (c-with-syntax-table decl-syntax-table
7804 (if (eq (char-before) ?\
;)
7806 (c-syntactic-re-search-forward ";" nil
'move t
))))
7809 (defun c-looking-at-decl-block (containing-sexp goto-start
&optional limit
)
7810 ;; Assuming the point is at an open brace, check if it starts a
7811 ;; block that contains another declaration level, i.e. that isn't a
7812 ;; statement block or a brace list, and if so return non-nil.
7814 ;; If the check is successful, the return value is the start of the
7815 ;; keyword that tells what kind of construct it is, i.e. typically
7816 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7817 ;; the point will be at the start of the construct, before any
7818 ;; leading specifiers, otherwise it's at the returned position.
7820 ;; The point is clobbered if the check is unsuccessful.
7822 ;; CONTAINING-SEXP is the position of the open of the surrounding
7823 ;; paren, or nil if none.
7825 ;; The optional LIMIT limits the backward search for the start of
7826 ;; the construct. It's assumed to be at a syntactically relevant
7829 ;; If any template arglists are found in the searched region before
7830 ;; the open brace, they get marked with paren syntax.
7832 ;; This function might do hidden buffer changes.
7834 (let ((open-brace (point)) kwd-start first-specifier-pos
)
7835 (c-syntactic-skip-backward c-block-prefix-charset limit t
)
7837 (when (and c-recognize-
<>-arglists
7838 (eq (char-before) ?
>))
7839 ;; Could be at the end of a template arglist.
7840 (let ((c-parse-and-markup-<>-arglists t
)
7841 (c-disallow-comma-in-<>-arglists
7842 (and containing-sexp
7843 (not (eq (char-after containing-sexp
) ?
{)))))
7845 (c-backward-<>-arglist nil limit
)
7847 (c-syntactic-skip-backward c-block-prefix-charset limit t
)
7848 (eq (char-before) ?
>))))))
7850 ;; Note: Can't get bogus hits inside template arglists below since they
7851 ;; have gotten paren syntax above.
7853 ;; If `goto-start' is set we begin by searching for the
7854 ;; first possible position of a leading specifier list.
7855 ;; The `c-decl-block-key' search continues from there since
7856 ;; we know it can't match earlier.
7858 (when (c-syntactic-re-search-forward c-symbol-start
7860 (goto-char (setq first-specifier-pos
(match-beginning 0)))
7865 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t
)
7866 (goto-char (setq kwd-start
(match-beginning 0)))
7869 ;; Found a keyword that can't be a type?
7872 ;; Can be a type too, in which case it's the return type of a
7873 ;; function (under the assumption that no declaration level
7874 ;; block construct starts with a type).
7875 (not (c-forward-type))
7877 ;; Jumped over a type, but it could be a declaration keyword
7878 ;; followed by the declared identifier that we've jumped over
7879 ;; instead (e.g. in "class Foo {"). If it indeed is a type
7880 ;; then we should be at the declarator now, so check for a
7881 ;; valid declarator start.
7883 ;; Note: This doesn't cope with the case when a declared
7884 ;; identifier is followed by e.g. '(' in a language where '('
7885 ;; also might be part of a declarator expression. Currently
7886 ;; there's no such language.
7887 (not (or (looking-at c-symbol-start
)
7888 (looking-at c-type-decl-prefix-key
)))))
7890 ;; In Pike a list of modifiers may be followed by a brace
7891 ;; to make them apply to many identifiers. Note that the
7892 ;; match data will be empty on return in this case.
7893 ((and (c-major-mode-is 'pike-mode
)
7895 (goto-char open-brace
)
7896 (= (c-backward-token-2) 0))
7897 (looking-at c-specifier-key
)
7898 ;; Use this variant to avoid yet another special regexp.
7899 (c-keyword-member (c-keyword-sym (match-string 1))
7901 (setq kwd-start
(point))
7907 ;; Back up over any preceding specifiers and their clauses
7908 ;; by going forward from `first-specifier-pos', which is the
7909 ;; earliest possible position where the specifier list can
7912 (goto-char first-specifier-pos
)
7914 (while (< (point) kwd-start
)
7915 (if (looking-at c-symbol-key
)
7916 ;; Accept any plain symbol token on the ground that
7917 ;; it's a specifier masked through a macro (just
7918 ;; like `c-forward-decl-or-cast-1' skip forward over
7921 ;; Could be more restrictive wrt invalid keywords,
7922 ;; but that'd only occur in invalid code so there's
7923 ;; no use spending effort on it.
7924 (let ((end (match-end 0)))
7925 (unless (c-forward-keyword-clause 0)
7927 (c-forward-syntactic-ws)))
7929 ;; Can't parse a declaration preamble and is still
7930 ;; before `kwd-start'. That means `first-specifier-pos'
7931 ;; was in some earlier construct. Search again.
7932 (if (c-syntactic-re-search-forward c-symbol-start
7934 (goto-char (setq first-specifier-pos
(match-beginning 0)))
7935 ;; Got no preamble before the block declaration keyword.
7936 (setq first-specifier-pos kwd-start
))))
7938 (goto-char first-specifier-pos
))
7939 (goto-char kwd-start
))
7943 (defun c-search-uplist-for-classkey (paren-state)
7944 ;; Check if the closest containing paren sexp is a declaration
7945 ;; block, returning a 2 element vector in that case. Aref 0
7946 ;; contains the bufpos at boi of the class key line, and aref 1
7947 ;; contains the bufpos of the open brace. This function is an
7948 ;; obsolete wrapper for `c-looking-at-decl-block'.
7950 ;; This function might do hidden buffer changes.
7951 (let ((open-paren-pos (c-most-enclosing-brace paren-state
)))
7952 (when open-paren-pos
7954 (goto-char open-paren-pos
)
7955 (when (and (eq (char-after) ?
{)
7956 (c-looking-at-decl-block
7957 (c-safe-position open-paren-pos paren-state
)
7959 (back-to-indentation)
7960 (vector (point) open-paren-pos
))))))
7962 (defun c-inside-bracelist-p (containing-sexp paren-state
)
7963 ;; return the buffer position of the beginning of the brace list
7964 ;; statement if we're inside a brace list, otherwise return nil.
7965 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
7966 ;; paren. PAREN-STATE is the remainder of the state of enclosing
7969 ;; N.B.: This algorithm can potentially get confused by cpp macros
7970 ;; placed in inconvenient locations. It's a trade-off we make for
7973 ;; This function might do hidden buffer changes.
7975 ;; This will pick up brace list declarations.
7978 (goto-char containing-sexp
)
7981 (if (and (or (looking-at c-brace-list-key
)
7982 (progn (c-forward-sexp -
1)
7983 (looking-at c-brace-list-key
)))
7984 (setq bracepos
(c-down-list-forward (point)))
7985 (not (c-crosses-statement-barrier-p (point)
7988 ;; this will pick up array/aggregate init lists, even if they are nested.
7991 ;; Pike can have class definitions anywhere, so we must
7992 ;; check for the class key here.
7993 (and (c-major-mode-is 'pike-mode
)
7995 bufpos braceassignp lim next-containing
)
7996 (while (and (not bufpos
)
7999 (if (consp (car paren-state
))
8000 (setq lim
(cdr (car paren-state
))
8001 paren-state
(cdr paren-state
))
8002 (setq lim
(car paren-state
)))
8004 (setq next-containing
(car paren-state
)
8005 paren-state
(cdr paren-state
))))
8006 (goto-char containing-sexp
)
8007 (if (c-looking-at-inexpr-block next-containing next-containing
)
8008 ;; We're in an in-expression block of some kind. Do not
8009 ;; check nesting. We deliberately set the limit to the
8010 ;; containing sexp, so that c-looking-at-inexpr-block
8011 ;; doesn't check for an identifier before it.
8012 (setq containing-sexp nil
)
8013 ;; see if the open brace is preceded by = or [...] in
8014 ;; this statement, but watch out for operator=
8015 (setq braceassignp
'dontknow
)
8016 (c-backward-token-2 1 t lim
)
8017 ;; Checks to do only on the first sexp before the brace.
8018 (when (and c-opt-inexpr-brace-list-key
8019 (eq (char-after) ?\
[))
8020 ;; In Java, an initialization brace list may follow
8021 ;; directly after "new Foo[]", so check for a "new"
8023 (while (eq braceassignp
'dontknow
)
8025 (cond ((/= (c-backward-token-2 1 t lim
) 0) nil
)
8026 ((looking-at c-opt-inexpr-brace-list-key
) t
)
8027 ((looking-at "\\sw\\|\\s_\\|[.[]")
8028 ;; Carry on looking if this is an
8029 ;; identifier (may contain "." in Java)
8030 ;; or another "[]" sexp.
8033 ;; Checks to do on all sexps before the brace, up to the
8034 ;; beginning of the statement.
8035 (while (eq braceassignp
'dontknow
)
8036 (cond ((eq (char-after) ?\
;)
8037 (setq braceassignp nil
))
8039 (looking-at class-key
))
8040 (setq braceassignp nil
))
8041 ((eq (char-after) ?
=)
8042 ;; We've seen a =, but must check earlier tokens so
8043 ;; that it isn't something that should be ignored.
8044 (setq braceassignp
'maybe
)
8045 (while (and (eq braceassignp
'maybe
)
8046 (zerop (c-backward-token-2 1 t lim
)))
8049 ;; Check for operator =
8050 ((and c-opt-op-identifier-prefix
8051 (looking-at c-opt-op-identifier-prefix
))
8053 ;; Check for `<opchar>= in Pike.
8054 ((and (c-major-mode-is 'pike-mode
)
8055 (or (eq (char-after) ?
`)
8056 ;; Special case for Pikes
8057 ;; `[]=, since '[' is not in
8058 ;; the punctuation class.
8059 (and (eq (char-after) ?\
[)
8060 (eq (char-before) ?
`))))
8062 ((looking-at "\\s.") 'maybe
)
8063 ;; make sure we're not in a C++ template
8064 ;; argument assignment
8066 (c-major-mode-is 'c
++-mode
)
8068 (let ((here (point))
8070 (skip-chars-backward "^<>")
8072 (and (eq (char-before) ?
<)
8073 (not (c-crosses-statement-barrier-p
8075 (not (c-in-literal))
8079 (if (and (eq braceassignp
'dontknow
)
8080 (/= (c-backward-token-2 1 t lim
) 0))
8081 (setq braceassignp nil
)))
8082 (if (not braceassignp
)
8083 (if (eq (char-after) ?\
;)
8084 ;; Brace lists can't contain a semicolon, so we're done.
8085 (setq containing-sexp nil
)
8087 (setq containing-sexp next-containing
8089 next-containing nil
))
8090 ;; we've hit the beginning of the aggregate list
8091 (c-beginning-of-statement-1
8092 (c-most-enclosing-brace paren-state
))
8093 (setq bufpos
(point))))
8098 (defun c-looking-at-special-brace-list (&optional lim
)
8099 ;; If we're looking at the start of a pike-style list, ie `({Â })',
8100 ;; `([Â ])', `(<Â >)' etc, a cons of a cons of its starting and ending
8101 ;; positions and its entry in c-special-brace-lists is returned, nil
8102 ;; otherwise. The ending position is nil if the list is still open.
8103 ;; LIM is the limit for forward search. The point may either be at
8104 ;; the `(' or at the following paren character. Tries to check the
8105 ;; matching closer, but assumes it's correct if no balanced paren is
8106 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8107 ;; a special brace list).
8109 ;; This function might do hidden buffer changes.
8110 (if c-special-brace-lists
8115 (c-forward-syntactic-ws)
8116 (if (eq (char-after) ?\
()
8119 (c-forward-syntactic-ws)
8120 (setq inner-beg
(point))
8121 (setq type
(assq (char-after) c-special-brace-lists
)))
8122 (if (setq type
(assq (char-after) c-special-brace-lists
))
8124 (setq inner-beg
(point))
8125 (c-backward-syntactic-ws)
8127 (setq beg
(if (eq (char-after) ?\
()
8135 (= (char-before) ?\
)))
8137 (goto-char inner-beg
)
8138 (if (looking-at "\\s(")
8139 ;; Check balancing of the inner paren
8144 ;; If the inner char isn't a paren then
8145 ;; we can't check balancing, so just
8146 ;; check the char before the outer
8150 (c-backward-syntactic-ws)
8151 (= (char-before) (cdr type
)))))
8152 (if (or (/= (char-syntax (char-before)) ?\
))
8154 (c-forward-syntactic-ws)
8157 (cons (cons beg end
) type
))
8158 (cons (list beg
) type
)))))
8161 (defun c-looking-at-bos (&optional lim
)
8162 ;; Return non-nil if between two statements or declarations, assuming
8163 ;; point is not inside a literal or comment.
8165 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8166 ;; are recommended instead.
8168 ;; This function might do hidden buffer changes.
8169 (c-at-statement-start-p))
8170 (make-obsolete 'c-looking-at-bos
'c-at-statement-start-p
"22.1")
8172 (defun c-looking-at-inexpr-block (lim containing-sexp
&optional check-at-end
)
8173 ;; Return non-nil if we're looking at the beginning of a block
8174 ;; inside an expression. The value returned is actually a cons of
8175 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8176 ;; position of the beginning of the construct.
8178 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8179 ;; position of the closest containing list. If it's nil, the
8180 ;; containing paren isn't used to decide whether we're inside an
8181 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8182 ;; needs to be farther back.
8184 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8185 ;; brace block might be done. It should only be used when the
8186 ;; construct can be assumed to be complete, i.e. when the original
8187 ;; starting position was further down than that.
8189 ;; This function might do hidden buffer changes.
8192 (let ((res 'maybe
) passed-paren
8193 (closest-lim (or containing-sexp lim
(point-min)))
8194 ;; Look at the character after point only as a last resort
8195 ;; when we can't disambiguate.
8196 (block-follows (and (eq (char-after) ?
{) (point))))
8198 (while (and (eq res
'maybe
)
8199 (progn (c-backward-syntactic-ws)
8200 (> (point) closest-lim
))
8202 (progn (backward-char)
8203 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8204 (c-safe (forward-char)
8205 (goto-char (scan-sexps (point) -
1))))
8208 (if (looking-at c-keywords-regexp
)
8209 (let ((kw-sym (c-keyword-sym (match-string 1))))
8212 (c-keyword-member kw-sym
'c-inexpr-class-kwds
))
8213 (and (not (eq passed-paren ?\
[))
8214 (or (not (looking-at c-class-key
))
8215 ;; If the class definition is at the start of
8216 ;; a statement, we don't consider it an
8217 ;; in-expression class.
8218 (let ((prev (point)))
8220 (= (c-backward-token-2 1 nil closest-lim
) 0)
8221 (eq (char-syntax (char-after)) ?w
))
8222 (setq prev
(point)))
8224 (not (c-at-statement-start-p)))
8225 ;; Also, in Pike we treat it as an
8226 ;; in-expression class if it's used in an
8227 ;; object clone expression.
8230 (c-major-mode-is 'pike-mode
)
8231 (progn (goto-char block-follows
)
8232 (zerop (c-forward-token-2 1 t
)))
8233 (eq (char-after) ?\
())))
8234 (cons 'inexpr-class
(point))))
8235 ((c-keyword-member kw-sym
'c-inexpr-block-kwds
)
8236 (when (not passed-paren
)
8237 (cons 'inexpr-statement
(point))))
8238 ((c-keyword-member kw-sym
'c-lambda-kwds
)
8239 (when (or (not passed-paren
)
8240 (eq passed-paren ?\
())
8241 (cons 'inlambda
(point))))
8242 ((c-keyword-member kw-sym
'c-block-stmt-kwds
)
8247 (if (looking-at "\\s(")
8249 (if (and (eq passed-paren ?\
[)
8250 (eq (char-after) ?\
[))
8251 ;; Accept several square bracket sexps for
8252 ;; Java array initializations.
8254 (setq passed-paren
(char-after))
8259 (when (and c-recognize-paren-inexpr-blocks
8262 (eq (char-after containing-sexp
) ?\
())
8263 (goto-char containing-sexp
)
8264 (if (or (save-excursion
8265 (c-backward-syntactic-ws lim
)
8266 (and (> (point) (or lim
(point-min)))
8268 (and c-special-brace-lists
8269 (c-looking-at-special-brace-list)))
8271 (cons 'inexpr-statement
(point))))
8275 (defun c-looking-at-inexpr-block-backward (paren-state)
8276 ;; Returns non-nil if we're looking at the end of an in-expression
8277 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8278 ;; PAREN-STATE is the paren state relevant at the current position.
8280 ;; This function might do hidden buffer changes.
8282 ;; We currently only recognize a block.
8283 (let ((here (point))
8284 (elem (car-safe paren-state
))
8286 (when (and (consp elem
)
8287 (progn (goto-char (cdr elem
))
8288 (c-forward-syntactic-ws here
)
8290 (goto-char (car elem
))
8291 (if (setq paren-state
(cdr paren-state
))
8292 (setq containing-sexp
(car-safe paren-state
)))
8293 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8295 containing-sexp
)))))
8298 ;; `c-guess-basic-syntax' and the functions that precedes it below
8299 ;; implements the main decision tree for determining the syntactic
8300 ;; analysis of the current line of code.
8302 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8303 ;; auto newline analysis.
8304 (defvar c-auto-newline-analysis nil
)
8306 (defun c-brace-anchor-point (bracepos)
8307 ;; BRACEPOS is the position of a brace in a construct like "namespace
8308 ;; Bar {". Return the anchor point in this construct; this is the
8309 ;; earliest symbol on the brace's line which isn't earlier than
8312 ;; Currently (2007-08-17), "like namespace" means "matches
8313 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8314 ;; or anything like that.
8316 (let ((boi (c-point 'boi bracepos
)))
8317 (goto-char bracepos
)
8318 (while (and (> (point) boi
)
8319 (not (looking-at c-other-decl-block-key
)))
8320 (c-backward-token-2))
8321 (if (> (point) boi
) (point) boi
))))
8323 (defsubst c-add-syntax
(symbol &rest args
)
8324 ;; A simple function to prepend a new syntax element to
8325 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8326 ;; should always be dynamically bound but since we read it first
8327 ;; we'll fail properly anyway if this function is misused.
8328 (setq c-syntactic-context
(cons (cons symbol args
)
8329 c-syntactic-context
)))
8331 (defsubst c-append-syntax
(symbol &rest args
)
8332 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8333 ;; (Normally not necessary.)
8334 (setq c-syntactic-context
(nconc c-syntactic-context
8335 (list (cons symbol args
)))))
8337 (defun c-add-stmt-syntax (syntax-symbol
8342 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8343 ;; needed with further syntax elements of the types `substatement',
8344 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8345 ;; `defun-block-intro'.
8347 ;; Do the generic processing to anchor the given syntax symbol on
8348 ;; the preceding statement: Skip over any labels and containing
8349 ;; statements on the same line, and then search backward until we
8350 ;; find a statement or block start that begins at boi without a
8351 ;; label or comment.
8353 ;; Point is assumed to be at the prospective anchor point for the
8354 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8355 ;; skip past open parens and containing statements. Most of the added
8356 ;; syntax elements will get the same anchor point - the exception is
8357 ;; for an anchor in a construct like "namespace"[*] - this is as early
8358 ;; as possible in the construct but on the same line as the {.
8360 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8362 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8363 ;; syntax symbol. They are appended after the anchor point.
8365 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8366 ;; if the current statement starts there.
8368 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8369 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8371 ;; This function might do hidden buffer changes.
8373 (if (= (point) (c-point 'boi
))
8374 ;; This is by far the most common case, so let's give it special
8376 (apply 'c-add-syntax syntax-symbol
(point) syntax-extra-args
)
8378 (let ((syntax-last c-syntactic-context
)
8379 (boi (c-point 'boi
))
8380 ;; Set when we're on a label, so that we don't stop there.
8381 ;; FIXME: To be complete we should check if we're on a label
8382 ;; now at the start.
8385 ;; Use point as the anchor point for "namespace", "extern", etc.
8386 (apply 'c-add-syntax syntax-symbol
8387 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist
)
8391 ;; Loop while we have to back out of containing blocks.
8394 (catch 'back-up-block
8396 ;; Loop while we have to back up statements.
8397 (while (or (/= (point) boi
)
8399 (looking-at c-comment-start-regexp
))
8401 ;; Skip past any comments that stands between the
8402 ;; statement start and boi.
8403 (let ((savepos (point)))
8404 (while (and (/= savepos boi
)
8405 (c-backward-single-comment))
8406 (setq savepos
(point)
8407 boi
(c-point 'boi
)))
8408 (goto-char savepos
))
8410 ;; Skip to the beginning of this statement or backward
8412 (let ((old-pos (point))
8414 (step-type (c-beginning-of-statement-1 containing-sexp
)))
8415 (setq boi
(c-point 'boi
)
8416 on-label
(eq step-type
'label
))
8418 (cond ((= (point) old-pos
)
8419 ;; If we didn't move we're at the start of a block and
8420 ;; have to continue outside it.
8421 (throw 'back-up-block t
))
8423 ((and (eq step-type
'up
)
8424 (>= (point) old-boi
)
8425 (looking-at "else\\>[^_]")
8428 (looking-at "if\\>[^_]")))
8429 ;; Special case to avoid deeper and deeper indentation
8430 ;; of "else if" clauses.
8433 ((and (not stop-at-boi-only
)
8434 (/= old-pos old-boi
)
8435 (memq step-type
'(up previous
)))
8436 ;; If stop-at-boi-only is nil, we shouldn't back up
8437 ;; over previous or containing statements to try to
8438 ;; reach boi, so go back to the last position and
8441 (throw 'back-up-block nil
))
8444 (if (and (not stop-at-boi-only
)
8445 (memq step-type
'(up previous beginning
)))
8446 ;; If we've moved into another statement then we
8447 ;; should no longer try to stop in the middle of a
8449 (setq stop-at-boi-only t
))
8451 ;; Record this as a substatement if we skipped up one
8453 (when (eq step-type
'up
)
8454 (c-add-syntax 'substatement nil
))))
8459 ;; Now we have to go out of this block.
8460 (goto-char containing-sexp
)
8462 ;; Don't stop in the middle of a special brace list opener
8464 (when c-special-brace-lists
8465 (let ((special-list (c-looking-at-special-brace-list)))
8466 (when (and special-list
8467 (< (car (car special-list
)) (point)))
8468 (setq containing-sexp
(car (car special-list
)))
8469 (goto-char containing-sexp
))))
8471 (setq paren-state
(c-whack-state-after containing-sexp paren-state
)
8472 containing-sexp
(c-most-enclosing-brace paren-state
)
8475 ;; Analyze the construct in front of the block we've stepped out
8476 ;; from and add the right syntactic element for it.
8477 (let ((paren-pos (point))
8478 (paren-char (char-after))
8481 (if (eq paren-char ?\
()
8482 ;; Stepped out of a parenthesis block, so we're in an
8485 (when (/= paren-pos boi
)
8486 (if (and c-recognize-paren-inexpr-blocks
8488 (c-backward-syntactic-ws containing-sexp
)
8489 (or (not (looking-at "\\>"))
8490 (not (c-on-identifier))))
8492 (goto-char (1+ paren-pos
))
8493 (c-forward-syntactic-ws)
8494 (eq (char-after) ?
{)))
8495 ;; Stepped out of an in-expression statement. This
8496 ;; syntactic element won't get an anchor pos.
8497 (c-add-syntax 'inexpr-statement
)
8499 ;; A parenthesis normally belongs to an arglist.
8500 (c-add-syntax 'arglist-cont-nonempty nil paren-pos
)))
8504 (1+ containing-sexp
)
8506 (setq step-type
'same
8509 ;; Stepped out of a brace block.
8510 (setq step-type
(c-beginning-of-statement-1 containing-sexp
)
8511 on-label
(eq step-type
'label
))
8513 (if (and (eq step-type
'same
)
8514 (/= paren-pos
(point)))
8518 (goto-char paren-pos
)
8519 (setq inexpr
(c-looking-at-inexpr-block
8520 (c-safe-position containing-sexp paren-state
)
8522 (c-add-syntax (if (eq (car inexpr
) 'inlambda
)
8524 'statement-block-intro
)
8526 ((looking-at c-other-decl-block-key
)
8528 (cdr (assoc (match-string 1)
8529 c-other-decl-block-key-in-symbols-alist
))
8530 (max (c-point 'boi paren-pos
) (point))))
8531 (t (c-add-syntax 'defun-block-intro nil
))))
8533 (c-add-syntax 'statement-block-intro nil
)))
8535 (if (= paren-pos boi
)
8536 ;; Always done if the open brace was at boi. The
8537 ;; c-beginning-of-statement-1 call above is necessary
8538 ;; anyway, to decide the type of block-intro to add.
8539 (goto-char paren-pos
)
8540 (setq boi
(c-point 'boi
)))
8543 ;; Fill in the current point as the anchor for all the symbols
8545 (let ((p c-syntactic-context
) q
)
8546 (while (not (eq p syntax-last
))
8547 (setq q
(cdr (car p
))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8555 (defun c-add-class-syntax (symbol
8556 containing-decl-open
8557 containing-decl-start
8560 ;; The inclass and class-close syntactic symbols are added in
8561 ;; several places and some work is needed to fix everything.
8562 ;; Therefore it's collected here.
8564 ;; This function might do hidden buffer changes.
8565 (goto-char containing-decl-open
)
8566 (if (and (eq symbol
'inclass
) (= (point) (c-point 'boi
)))
8568 (c-add-syntax symbol containing-decl-open
)
8569 containing-decl-open
)
8570 (goto-char containing-decl-start
)
8571 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8572 ;; here, but we have to do like this for compatibility.
8573 (back-to-indentation)
8574 (c-add-syntax symbol
(point))
8575 (if (and (c-keyword-member containing-decl-kwd
8576 'c-inexpr-class-kwds
)
8577 (/= containing-decl-start
(c-point 'boi containing-decl-start
)))
8578 (c-add-syntax 'inexpr-class
))
8581 (defun c-guess-continued-construct (indent-point
8583 beg-of-same-or-containing-stmt
8586 ;; This function contains the decision tree reached through both
8587 ;; cases 18 and 10. It's a continued statement or top level
8588 ;; construct of some kind.
8590 ;; This function might do hidden buffer changes.
8592 (let (special-brace-list placeholder
)
8593 (goto-char indent-point
)
8594 (skip-chars-forward " \t")
8597 ;; (CASE A removed.)
8598 ;; CASE B: open braces for class or brace-lists
8599 ((setq special-brace-list
8600 (or (and c-special-brace-lists
8601 (c-looking-at-special-brace-list))
8602 (eq char-after-ip ?
{)))
8605 ;; CASE B.1: class-open
8607 (and (eq (char-after) ?
{)
8608 (c-looking-at-decl-block containing-sexp t
)
8609 (setq beg-of-same-or-containing-stmt
(point))))
8610 (c-add-syntax 'class-open beg-of-same-or-containing-stmt
))
8612 ;; CASE B.2: brace-list-open
8613 ((or (consp special-brace-list
)
8615 (goto-char beg-of-same-or-containing-stmt
)
8616 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8617 indent-point t t t
)))
8618 ;; The most semantically accurate symbol here is
8619 ;; brace-list-open, but we normally report it simply as a
8620 ;; statement-cont. The reason is that one normally adjusts
8621 ;; brace-list-open for brace lists as top-level constructs,
8622 ;; and brace lists inside statements is a completely different
8623 ;; context. C.f. case 5A.3.
8624 (c-beginning-of-statement-1 containing-sexp
)
8625 (c-add-stmt-syntax (if c-auto-newline-analysis
8626 ;; Turn off the dwim above when we're
8627 ;; analyzing the nature of the brace
8628 ;; for the auto newline feature.
8632 containing-sexp paren-state
))
8634 ;; CASE B.3: The body of a function declared inside a normal
8635 ;; block. Can occur e.g. in Pike and when using gcc
8636 ;; extensions, but watch out for macros followed by blocks.
8637 ;; C.f. cases E, 16F and 17G.
8638 ((and (not (c-at-statement-start-p))
8639 (eq (c-beginning-of-statement-1 containing-sexp nil nil t
)
8642 (let ((c-recognize-typeless-decls nil
))
8643 ;; Turn off recognition of constructs that lacks a
8644 ;; type in this case, since that's more likely to be
8645 ;; a macro followed by a block.
8646 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
8647 (c-add-stmt-syntax 'defun-open nil t
8648 containing-sexp paren-state
))
8650 ;; CASE B.4: Continued statement with block open. The most
8651 ;; accurate analysis is perhaps `statement-cont' together with
8652 ;; `block-open' but we play DWIM and use `substatement-open'
8653 ;; instead. The rationaly is that this typically is a macro
8654 ;; followed by a block which makes it very similar to a
8655 ;; statement with a substatement block.
8657 (c-add-stmt-syntax 'substatement-open nil nil
8658 containing-sexp paren-state
))
8661 ;; CASE C: iostream insertion or extraction operator
8662 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8664 (goto-char beg-of-same-or-containing-stmt
)
8665 ;; If there is no preceding streamop in the statement
8666 ;; then indent this line as a normal statement-cont.
8667 (when (c-syntactic-re-search-forward
8668 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point
'move t t
)
8669 (c-add-syntax 'stream-op
(c-point 'boi
))
8672 ;; CASE E: In the "K&R region" of a function declared inside a
8673 ;; normal block. C.f. case B.3.
8674 ((and (save-excursion
8675 ;; Check that the next token is a '{'. This works as
8676 ;; long as no language that allows nested function
8677 ;; definitions allows stuff like member init lists, K&R
8678 ;; declarations or throws clauses there.
8680 ;; Note that we do a forward search for something ahead
8681 ;; of the indentation line here. That's not good since
8682 ;; the user might not have typed it yet. Unfortunately
8683 ;; it's exceedingly tricky to recognize a function
8684 ;; prototype in a code block without resorting to this.
8685 (c-forward-syntactic-ws)
8686 (eq (char-after) ?
{))
8687 (not (c-at-statement-start-p))
8688 (eq (c-beginning-of-statement-1 containing-sexp nil nil t
)
8691 (let ((c-recognize-typeless-decls nil
))
8692 ;; Turn off recognition of constructs that lacks a
8693 ;; type in this case, since that's more likely to be
8694 ;; a macro followed by a block.
8695 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
8696 (c-add-stmt-syntax 'func-decl-cont nil t
8697 containing-sexp paren-state
))
8699 ;;CASE F: continued statement and the only preceding items are
8701 ((and (c-major-mode-is 'java-mode
)
8702 (setq placeholder
(point))
8703 (c-beginning-of-statement-1)
8705 (while (and (c-forward-annotation)
8706 (< (point) placeholder
))
8707 (c-forward-syntactic-ws))
8710 (>= (point) placeholder
)
8711 (goto-char placeholder
)))
8712 (c-beginning-of-statement-1 containing-sexp
)
8713 (c-add-syntax 'annotation-var-cont
(point)))
8715 ;; CASE G: a template list continuation?
8716 ;; Mostly a duplication of case 5D.3 to fix templates-19:
8717 ((and (c-major-mode-is 'c
++-mode
)
8719 (goto-char indent-point
)
8720 (c-with-syntax-table c
++-template-syntax-table
8721 (setq placeholder
(c-up-list-backward)))
8723 (eq (char-after placeholder
) ?
<)
8724 (/= (char-before placeholder
) ?
<)
8726 (goto-char (1+ placeholder
))
8727 (not (looking-at c-
<-op-cont-regexp
))))))
8728 (c-with-syntax-table c
++-template-syntax-table
8729 (goto-char placeholder
)
8730 (c-beginning-of-statement-1 containing-sexp t
)
8732 (c-backward-syntactic-ws containing-sexp
)
8733 (eq (char-before) ?
<))
8734 ;; In a nested template arglist.
8736 (goto-char placeholder
)
8737 (c-syntactic-skip-backward "^,;" containing-sexp t
)
8738 (c-forward-syntactic-ws))
8739 (back-to-indentation)))
8740 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8742 (c-add-syntax 'template-args-cont
(point) placeholder
))
8744 ;; CASE D: continued statement.
8746 (c-beginning-of-statement-1 containing-sexp
)
8747 (c-add-stmt-syntax 'statement-cont nil nil
8748 containing-sexp paren-state
))
8751 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
8754 (defun c-guess-basic-syntax ()
8755 "Return the syntactic context of the current line."
8758 (c-save-buffer-state
8759 ((indent-point (point))
8760 (case-fold-search nil
)
8761 ;; A whole ugly bunch of various temporary variables. Have
8762 ;; to declare them here since it's not possible to declare
8763 ;; a variable with only the scope of a cond test and the
8764 ;; following result clauses, and most of this function is a
8765 ;; single gigantic cond. :P
8766 literal char-before-ip before-ws-ip char-after-ip macro-start
8767 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
8768 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
8770 ;; The following record some positions for the containing
8771 ;; declaration block if we're directly within one:
8772 ;; `containing-decl-open' is the position of the open
8773 ;; brace. `containing-decl-start' is the start of the
8774 ;; declaration. `containing-decl-kwd' is the keyword
8775 ;; symbol of the keyword that tells what kind of block it
8777 containing-decl-open
8778 containing-decl-start
8780 ;; The open paren of the closest surrounding sexp or nil if
8783 ;; The position after the closest preceding brace sexp
8784 ;; (nested sexps are ignored), or the position after
8785 ;; `containing-sexp' if there is none, or (point-min) if
8786 ;; `containing-sexp' is nil.
8788 ;; The paren state outside `containing-sexp', or at
8789 ;; `indent-point' if `containing-sexp' is nil.
8790 (paren-state (c-parse-state))
8791 ;; There's always at most one syntactic element which got
8792 ;; an anchor pos. It's stored in syntactic-relpos.
8794 (c-stmt-delim-chars c-stmt-delim-chars
))
8796 ;; Check if we're directly inside an enclosing declaration
8798 (when (and (setq containing-sexp
8799 (c-most-enclosing-brace paren-state
))
8801 (goto-char containing-sexp
)
8802 (eq (char-after) ?
{))
8804 (c-looking-at-decl-block
8805 (c-most-enclosing-brace paren-state
8808 (setq containing-decl-open containing-sexp
8809 containing-decl-start
(point)
8810 containing-sexp nil
)
8811 (goto-char placeholder
)
8812 (setq containing-decl-kwd
(and (looking-at c-keywords-regexp
)
8813 (c-keyword-sym (match-string 1)))))
8815 ;; Init some position variables.
8818 (setq containing-sexp
(car paren-state
)
8819 paren-state
(cdr paren-state
))
8820 (if (consp containing-sexp
)
8822 (setq lim
(cdr containing-sexp
))
8823 (if (cdr c-state-cache
)
8824 ;; Ignore balanced paren. The next entry
8825 ;; can't be another one.
8826 (setq containing-sexp
(car (cdr c-state-cache
))
8827 paren-state
(cdr paren-state
))
8828 ;; If there is no surrounding open paren then
8829 ;; put the last balanced pair back on paren-state.
8830 (setq paren-state
(cons containing-sexp paren-state
)
8831 containing-sexp nil
)))
8832 (setq lim
(1+ containing-sexp
))))
8833 (setq lim
(point-min)))
8835 ;; If we're in a parenthesis list then ',' delimits the
8836 ;; "statements" rather than being an operator (with the
8837 ;; exception of the "for" clause). This difference is
8838 ;; typically only noticeable when statements are used in macro
8840 (when (and containing-sexp
8841 (eq (char-after containing-sexp
) ?\
())
8842 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma
))
8843 ;; cache char before and after indent point, and move point to
8844 ;; the most likely position to perform the majority of tests
8845 (goto-char indent-point
)
8846 (c-backward-syntactic-ws lim
)
8847 (setq before-ws-ip
(point)
8848 char-before-ip
(char-before))
8849 (goto-char indent-point
)
8850 (skip-chars-forward " \t")
8851 (setq char-after-ip
(char-after))
8853 ;; are we in a literal?
8854 (setq literal
(c-in-literal lim
))
8856 ;; now figure out syntactic qualities of the current line
8859 ;; CASE 1: in a string.
8860 ((eq literal
'string
)
8861 (c-add-syntax 'string
(c-point 'bopl
)))
8863 ;; CASE 2: in a C or C++ style comment.
8864 ((and (memq literal
'(c c
++))
8865 ;; This is a kludge for XEmacs where we use
8866 ;; `buffer-syntactic-context', which doesn't correctly
8867 ;; recognize "\*/" to end a block comment.
8868 ;; `parse-partial-sexp' which is used by
8869 ;; `c-literal-limits' will however do that in most
8870 ;; versions, which results in that we get nil from
8871 ;; `c-literal-limits' even when `c-in-literal' claims
8872 ;; we're inside a comment.
8873 (setq placeholder
(c-literal-limits lim
)))
8874 (c-add-syntax literal
(car placeholder
)))
8876 ;; CASE 3: in a cpp preprocessor macro continuation.
8877 ((and (save-excursion
8878 (when (c-beginning-of-macro)
8879 (setq macro-start
(point))))
8880 (/= macro-start
(c-point 'boi
))
8882 (setq tmpsymbol
'cpp-macro-cont
)
8883 (or (not c-syntactic-indentation-in-macros
)
8885 (goto-char macro-start
)
8886 ;; If at the beginning of the body of a #define
8887 ;; directive then analyze as cpp-define-intro
8888 ;; only. Go on with the syntactic analysis
8889 ;; otherwise. in-macro-expr is set if we're in a
8890 ;; cpp expression, i.e. before the #define body
8891 ;; or anywhere in a non-#define directive.
8892 (if (c-forward-to-cpp-define-body)
8893 (let ((indent-boi (c-point 'boi indent-point
)))
8894 (setq in-macro-expr
(> (point) indent-boi
)
8895 tmpsymbol
'cpp-define-intro
)
8896 (= (point) indent-boi
))
8897 (setq in-macro-expr t
)
8899 (c-add-syntax tmpsymbol macro-start
)
8900 (setq macro-start nil
))
8902 ;; CASE 11: an else clause?
8903 ((looking-at "else\\>[^_]")
8904 (c-beginning-of-statement-1 containing-sexp
)
8905 (c-add-stmt-syntax 'else-clause nil t
8906 containing-sexp paren-state
))
8908 ;; CASE 12: while closure of a do/while construct?
8909 ((and (looking-at "while\\>[^_]")
8911 (prog1 (eq (c-beginning-of-statement-1 containing-sexp
)
8913 (setq placeholder
(point)))))
8914 (goto-char placeholder
)
8915 (c-add-stmt-syntax 'do-while-closure nil t
8916 containing-sexp paren-state
))
8918 ;; CASE 13: A catch or finally clause? This case is simpler
8919 ;; than if-else and do-while, because a block is required
8920 ;; after every try, catch and finally.
8922 (and (cond ((c-major-mode-is 'c
++-mode
)
8923 (looking-at "catch\\>[^_]"))
8924 ((c-major-mode-is 'java-mode
)
8925 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
8926 (and (c-safe (c-backward-syntactic-ws)
8929 (eq (char-after) ?
{)
8930 (c-safe (c-backward-syntactic-ws)
8933 (if (eq (char-after) ?\
()
8934 (c-safe (c-backward-sexp) t
)
8936 (looking-at "\\(try\\|catch\\)\\>[^_]")
8937 (setq placeholder
(point))))
8938 (goto-char placeholder
)
8939 (c-add-stmt-syntax 'catch-clause nil t
8940 containing-sexp paren-state
))
8942 ;; CASE 18: A substatement we can recognize by keyword.
8944 (and c-opt-block-stmt-key
8945 (not (eq char-before-ip ?\
;))
8946 (not (c-at-vsemi-p before-ws-ip
))
8947 (not (memq char-after-ip
'(?\
) ?\
] ?
,)))
8948 (or (not (eq char-before-ip ?
}))
8949 (c-looking-at-inexpr-block-backward c-state-cache
))
8952 ;; Ought to cache the result from the
8953 ;; c-beginning-of-statement-1 calls here.
8954 (setq placeholder
(point))
8955 (while (eq (setq step-type
8956 (c-beginning-of-statement-1 lim
))
8958 (if (eq step-type
'previous
)
8959 (goto-char placeholder
)
8960 (setq placeholder
(point))
8961 (if (and (eq step-type
'same
)
8962 (not (looking-at c-opt-block-stmt-key
)))
8963 ;; Step up to the containing statement if we
8964 ;; stayed in the same one.
8968 (c-beginning-of-statement-1 lim
))
8971 (setq placeholder
(point))
8972 ;; There was no containing statement afterall.
8973 (goto-char placeholder
)))))
8975 (if (looking-at c-block-stmt-2-key
)
8976 ;; Require a parenthesis after these keywords.
8977 ;; Necessary to catch e.g. synchronized in Java,
8978 ;; which can be used both as statement and
8980 (and (zerop (c-forward-token-2 1 nil
))
8981 (eq (char-after) ?\
())
8982 (looking-at c-opt-block-stmt-key
))))
8984 (if (eq step-type
'up
)
8985 ;; CASE 18A: Simple substatement.
8987 (goto-char placeholder
)
8989 ((eq char-after-ip ?
{)
8990 (c-add-stmt-syntax 'substatement-open nil nil
8991 containing-sexp paren-state
))
8993 (goto-char indent-point
)
8994 (back-to-indentation)
8996 (c-add-stmt-syntax 'substatement-label nil nil
8997 containing-sexp paren-state
))
8999 (c-add-stmt-syntax 'substatement nil nil
9000 containing-sexp paren-state
))))
9002 ;; CASE 18B: Some other substatement. This is shared
9004 (c-guess-continued-construct indent-point
9010 ;; CASE 14: A case or default label
9011 ((looking-at c-label-kwds-regexp
)
9014 (goto-char containing-sexp
)
9015 (setq lim
(c-most-enclosing-brace c-state-cache
9017 (c-backward-to-block-anchor lim
)
9018 (c-add-stmt-syntax 'case-label nil t lim paren-state
))
9019 ;; Got a bogus label at the top level. In lack of better
9020 ;; alternatives, anchor it on (point-min).
9021 (c-add-syntax 'case-label
(point-min))))
9023 ;; CASE 15: any other label
9025 (back-to-indentation)
9026 (and (not (looking-at c-syntactic-ws-start
))
9028 (cond (containing-decl-open
9029 (setq placeholder
(c-add-class-syntax 'inclass
9030 containing-decl-open
9031 containing-decl-start
9034 ;; Append access-label with the same anchor point as
9036 (c-append-syntax 'access-label placeholder
))
9039 (goto-char containing-sexp
)
9040 (setq lim
(c-most-enclosing-brace c-state-cache
9044 (if (and (eq (c-beginning-of-statement-1 lim
) 'up
)
9045 (looking-at "switch\\>[^_]"))
9046 ;; If the surrounding statement is a switch then
9047 ;; let's analyze all labels as switch labels, so
9048 ;; that they get lined up consistently.
9051 (c-backward-to-block-anchor lim
)
9052 (c-add-stmt-syntax tmpsymbol nil t lim paren-state
))
9055 ;; A label on the top level. Treat it as a class
9056 ;; context. (point-min) is the closest we get to the
9057 ;; class open brace.
9058 (c-add-syntax 'access-label
(point-min)))))
9060 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9062 ((setq placeholder
(c-looking-at-inexpr-block
9063 (c-safe-position containing-sexp paren-state
)
9065 ;; Have to turn on the heuristics after
9066 ;; the point even though it doesn't work
9067 ;; very well. C.f. test case class-16.pike.
9069 (setq tmpsymbol
(assq (car placeholder
)
9070 '((inexpr-class . class-open
)
9071 (inexpr-statement . block-open
))))
9073 ;; It's a statement block or an anonymous class.
9074 (setq tmpsymbol
(cdr tmpsymbol
))
9075 ;; It's a Pike lambda. Check whether we are between the
9076 ;; lambda keyword and the argument list or at the defun
9078 (setq tmpsymbol
(if (eq char-after-ip ?
{)
9080 'lambda-intro-cont
)))
9081 (goto-char (cdr placeholder
))
9082 (back-to-indentation)
9083 (c-add-stmt-syntax tmpsymbol nil t
9084 (c-most-enclosing-brace c-state-cache
(point))
9086 (unless (eq (point) (cdr placeholder
))
9087 (c-add-syntax (car placeholder
))))
9089 ;; CASE 5: Line is inside a declaration level block or at top level.
9090 ((or containing-decl-open
(null containing-sexp
))
9093 ;; CASE 5A: we are looking at a defun, brace list, class,
9094 ;; or inline-inclass method opening brace
9095 ((setq special-brace-list
9096 (or (and c-special-brace-lists
9097 (c-looking-at-special-brace-list))
9098 (eq char-after-ip ?
{)))
9101 ;; CASE 5A.1: Non-class declaration block open.
9104 (and (eq char-after-ip ?
{)
9105 (setq tmp
(c-looking-at-decl-block containing-sexp t
))
9107 (setq placeholder
(point))
9109 (looking-at c-symbol-key
))
9111 (c-keyword-sym (setq keyword
(match-string 0)))
9112 'c-other-block-decl-kwds
))))
9113 (goto-char placeholder
)
9115 (if (string-equal keyword
"extern")
9116 ;; Special case for extern-lang-open.
9118 (intern (concat keyword
"-open")))
9119 nil t containing-sexp paren-state
))
9121 ;; CASE 5A.2: we are looking at a class opening brace
9123 (goto-char indent-point
)
9124 (skip-chars-forward " \t")
9125 (and (eq (char-after) ?
{)
9126 (c-looking-at-decl-block containing-sexp t
)
9127 (setq placeholder
(point))))
9128 (c-add-syntax 'class-open placeholder
))
9130 ;; CASE 5A.3: brace list open
9132 (c-beginning-of-decl-1 lim
)
9133 (while (looking-at c-specifier-key
)
9134 (goto-char (match-end 1))
9135 (c-forward-syntactic-ws indent-point
))
9136 (setq placeholder
(c-point 'boi
))
9137 (or (consp special-brace-list
)
9138 (and (or (save-excursion
9139 (goto-char indent-point
)
9140 (setq tmpsymbol nil
)
9141 (while (and (> (point) placeholder
)
9142 (zerop (c-backward-token-2 1 t
))
9143 (/= (char-after) ?
=))
9144 (and c-opt-inexpr-brace-list-key
9146 (looking-at c-opt-inexpr-brace-list-key
)
9147 (setq tmpsymbol
'topmost-intro-cont
)))
9148 (eq (char-after) ?
=))
9149 (looking-at c-brace-list-key
))
9151 (while (and (< (point) indent-point
)
9152 (zerop (c-forward-token-2 1 t
))
9153 (not (memq (char-after) '(?\
; ?\()))))
9154 (not (memq (char-after) '(?\
; ?\()))
9156 (if (and (not c-auto-newline-analysis
)
9157 (c-major-mode-is 'java-mode
)
9158 (eq tmpsymbol
'topmost-intro-cont
))
9159 ;; We're in Java and have found that the open brace
9160 ;; belongs to a "new Foo[]" initialization list,
9161 ;; which means the brace list is part of an
9162 ;; expression and not a top level definition. We
9163 ;; therefore treat it as any topmost continuation
9164 ;; even though the semantically correct symbol still
9165 ;; is brace-list-open, on the same grounds as in
9168 (c-beginning-of-statement-1 lim
)
9169 (c-add-syntax 'topmost-intro-cont
(c-point 'boi
)))
9170 (c-add-syntax 'brace-list-open placeholder
)))
9172 ;; CASE 5A.4: inline defun open
9173 ((and containing-decl-open
9174 (not (c-keyword-member containing-decl-kwd
9175 'c-other-block-decl-kwds
)))
9176 (c-add-syntax 'inline-open
)
9177 (c-add-class-syntax 'inclass
9178 containing-decl-open
9179 containing-decl-start
9183 ;; CASE 5A.5: ordinary defun open
9186 (c-beginning-of-decl-1 lim
)
9187 (while (looking-at c-specifier-key
)
9188 (goto-char (match-end 1))
9189 (c-forward-syntactic-ws indent-point
))
9190 (c-add-syntax 'defun-open
(c-point 'boi
))
9191 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9195 ;; CASE 5B: After a function header but before the body (or
9196 ;; the ending semicolon if there's no body).
9198 (when (setq placeholder
(c-just-after-func-arglist-p lim
))
9199 (setq tmp-pos
(point))))
9202 ;; CASE 5B.1: Member init list.
9203 ((eq (char-after tmp-pos
) ?
:)
9204 (if (or (> tmp-pos indent-point
)
9205 (= (c-point 'bosws
) (1+ tmp-pos
)))
9207 ;; There is no preceding member init clause.
9208 ;; Indent relative to the beginning of indentation
9209 ;; for the topmost-intro line that contains the
9210 ;; prototype's open paren.
9211 (goto-char placeholder
)
9212 (c-add-syntax 'member-init-intro
(c-point 'boi
)))
9213 ;; Indent relative to the first member init clause.
9214 (goto-char (1+ tmp-pos
))
9215 (c-forward-syntactic-ws)
9216 (c-add-syntax 'member-init-cont
(point))))
9218 ;; CASE 5B.2: K&R arg decl intro
9219 ((and c-recognize-knr-p
9220 (c-in-knr-argdecl lim
))
9221 (c-beginning-of-statement-1 lim
)
9222 (c-add-syntax 'knr-argdecl-intro
(c-point 'boi
))
9223 (if containing-decl-open
9224 (c-add-class-syntax 'inclass
9225 containing-decl-open
9226 containing-decl-start
9230 ;; CASE 5B.4: Nether region after a C++ or Java func
9231 ;; decl, which could include a `throws' declaration.
9233 (c-beginning-of-statement-1 lim
)
9234 (c-add-syntax 'func-decl-cont
(c-point 'boi
))
9237 ;; CASE 5C: inheritance line. could be first inheritance
9238 ;; line, or continuation of a multiple inheritance
9239 ((or (and (c-major-mode-is 'c
++-mode
)
9241 (when (eq char-after-ip ?
,)
9242 (skip-chars-forward " \t")
9244 (looking-at c-opt-postfix-decl-spec-key
)))
9245 (and (or (eq char-before-ip ?
:)
9246 ;; watch out for scope operator
9248 (and (eq char-after-ip ?
:)
9249 (c-safe (forward-char 1) t
)
9250 (not (eq (char-after) ?
:))
9253 (c-backward-syntactic-ws lim
)
9254 (if (eq char-before-ip ?
:)
9257 (c-backward-syntactic-ws lim
)))
9258 (back-to-indentation)
9259 (looking-at c-class-key
)))
9261 (and (c-major-mode-is 'java-mode
)
9262 (let ((fence (save-excursion
9263 (c-beginning-of-statement-1 lim
)
9268 (cond ((looking-at c-opt-postfix-decl-spec-key
)
9269 (setq injava-inher
(cons cont
(point))
9271 ((or (not (c-safe (c-forward-sexp -
1) t
))
9277 (not (c-crosses-statement-barrier-p (cdr injava-inher
)
9282 ;; CASE 5C.1: non-hanging colon on an inher intro
9283 ((eq char-after-ip ?
:)
9284 (c-beginning-of-statement-1 lim
)
9285 (c-add-syntax 'inher-intro
(c-point 'boi
))
9286 ;; don't add inclass symbol since relative point already
9287 ;; contains any class offset
9290 ;; CASE 5C.2: hanging colon on an inher intro
9291 ((eq char-before-ip ?
:)
9292 (c-beginning-of-statement-1 lim
)
9293 (c-add-syntax 'inher-intro
(c-point 'boi
))
9294 (if containing-decl-open
9295 (c-add-class-syntax 'inclass
9296 containing-decl-open
9297 containing-decl-start
9301 ;; CASE 5C.3: in a Java implements/extends
9303 (let ((where (cdr injava-inher
))
9304 (cont (car injava-inher
)))
9306 (cond ((looking-at "throws\\>[^_]")
9307 (c-add-syntax 'func-decl-cont
9308 (progn (c-beginning-of-statement-1 lim
)
9310 (cont (c-add-syntax 'inher-cont where
))
9311 (t (c-add-syntax 'inher-intro
9312 (progn (goto-char (cdr injava-inher
))
9313 (c-beginning-of-statement-1 lim
)
9317 ;; CASE 5C.4: a continued inheritance line
9319 (c-beginning-of-inheritance-list lim
)
9320 (c-add-syntax 'inher-cont
(point))
9321 ;; don't add inclass symbol since relative point already
9322 ;; contains any class offset
9325 ;; CASE 5D: this could be a top-level initialization, a
9326 ;; member init list continuation, or a template argument
9327 ;; list continuation.
9329 ;; Note: We use the fact that lim is always after any
9330 ;; preceding brace sexp.
9331 (if c-recognize-
<>-arglists
9334 (c-syntactic-skip-backward "^;,=<>" lim t
)
9337 (when c-overloadable-operators-regexp
9338 (when (setq placeholder
(c-after-special-operator-id lim
))
9339 (goto-char placeholder
)
9342 ((eq (char-before) ?
>)
9343 (or (c-backward-<>-arglist nil lim
)
9346 ((eq (char-before) ?
<)
9349 (c-forward-<>-arglist nil
))
9350 (progn (forward-char)
9354 ;; NB: No c-after-special-operator-id stuff in this
9355 ;; clause - we assume only C++ needs it.
9356 (c-syntactic-skip-backward "^;,=" lim t
))
9357 (memq (char-before) '(?
, ?
= ?
<)))
9360 ;; CASE 5D.3: perhaps a template list continuation?
9361 ((and (c-major-mode-is 'c
++-mode
)
9364 (c-with-syntax-table c
++-template-syntax-table
9365 (goto-char indent-point
)
9366 (setq placeholder
(c-up-list-backward))
9368 (eq (char-after placeholder
) ?
<))))))
9369 (c-with-syntax-table c
++-template-syntax-table
9370 (goto-char placeholder
)
9371 (c-beginning-of-statement-1 lim t
)
9373 (c-backward-syntactic-ws lim
)
9374 (eq (char-before) ?
<))
9375 ;; In a nested template arglist.
9377 (goto-char placeholder
)
9378 (c-syntactic-skip-backward "^,;" lim t
)
9379 (c-forward-syntactic-ws))
9380 (back-to-indentation)))
9381 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9383 (c-add-syntax 'template-args-cont
(point) placeholder
))
9385 ;; CASE 5D.4: perhaps a multiple inheritance line?
9386 ((and (c-major-mode-is 'c
++-mode
)
9388 (c-beginning-of-statement-1 lim
)
9389 (setq placeholder
(point))
9390 (if (looking-at "static\\>[^_]")
9391 (c-forward-token-2 1 nil indent-point
))
9392 (and (looking-at c-class-key
)
9393 (zerop (c-forward-token-2 2 nil indent-point
))
9394 (if (eq (char-after) ?
<)
9395 (c-with-syntax-table c
++-template-syntax-table
9396 (zerop (c-forward-token-2 1 t indent-point
)))
9398 (eq (char-after) ?
:))))
9399 (goto-char placeholder
)
9400 (c-add-syntax 'inher-cont
(c-point 'boi
)))
9402 ;; CASE 5D.5: Continuation of the "expression part" of a
9403 ;; top level construct. Or, perhaps, an unrecognised construct.
9405 (while (and (setq placeholder
(point))
9406 (eq (car (c-beginning-of-decl-1 containing-sexp
))
9409 (c-backward-syntactic-ws)
9410 (eq (char-before) ?
}))
9411 (< (point) placeholder
)))
9414 ((eq (point) placeholder
) 'statement
) ; unrecognised construct
9415 ;; A preceding comma at the top level means that a
9416 ;; new variable declaration starts here. Use
9417 ;; topmost-intro-cont for it, for consistency with
9418 ;; the first variable declaration. C.f. case 5N.
9419 ((eq char-before-ip ?
,) 'topmost-intro-cont
)
9420 (t 'statement-cont
))
9421 nil nil containing-sexp paren-state
))
9424 ;; CASE 5F: Close of a non-class declaration level block.
9425 ((and (eq char-after-ip ?
})
9426 (c-keyword-member containing-decl-kwd
9427 'c-other-block-decl-kwds
))
9428 ;; This is inconsistent: Should use `containing-decl-open'
9429 ;; here if it's at boi, like in case 5J.
9430 (goto-char containing-decl-start
)
9432 (if (string-equal (symbol-name containing-decl-kwd
) "extern")
9433 ;; Special case for compatibility with the
9434 ;; extern-lang syntactic symbols.
9436 (intern (concat (symbol-name containing-decl-kwd
)
9439 (c-most-enclosing-brace paren-state
(point))
9442 ;; CASE 5G: we are looking at the brace which closes the
9443 ;; enclosing nested class decl
9444 ((and containing-sexp
9445 (eq char-after-ip ?
})
9446 (eq containing-decl-open containing-sexp
))
9447 (c-add-class-syntax 'class-close
9448 containing-decl-open
9449 containing-decl-start
9453 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9454 ((and c-recognize-knr-p
9455 (not containing-sexp
) ; can't be knr inside braces.
9456 (not (eq char-before-ip ?
}))
9458 (setq placeholder
(cdr (c-beginning-of-decl-1 lim
)))
9460 ;; Do an extra check to avoid tripping up on
9461 ;; statements that occur in invalid contexts
9462 ;; (e.g. in macro bodies where we don't really
9463 ;; know the context of what we're looking at).
9464 (not (and c-opt-block-stmt-key
9465 (looking-at c-opt-block-stmt-key
)))))
9466 (< placeholder indent-point
))
9467 (goto-char placeholder
)
9468 (c-add-syntax 'knr-argdecl
(point)))
9470 ;; CASE 5I: ObjC method definition.
9471 ((and c-opt-method-key
9472 (looking-at c-opt-method-key
))
9473 (c-beginning-of-statement-1 nil t
)
9474 (if (= (point) indent-point
)
9475 ;; Handle the case when it's the first (non-comment)
9476 ;; thing in the buffer. Can't look for a 'same return
9477 ;; value from cbos1 since ObjC directives currently
9478 ;; aren't recognized fully, so that we get 'same
9479 ;; instead of 'previous if it moved over a preceding
9481 (goto-char (point-min)))
9482 (c-add-syntax 'objc-method-intro
(c-point 'boi
)))
9484 ;; CASE 5P: AWK pattern or function or continuation
9486 ((c-major-mode-is 'awk-mode
)
9487 (setq placeholder
(point))
9489 (if (and (eq (c-beginning-of-statement-1) 'same
)
9490 (/= (point) placeholder
))
9494 containing-sexp paren-state
))
9496 ;; CASE 5N: At a variable declaration that follows a class
9497 ;; definition or some other block declaration that doesn't
9498 ;; end at the closing '}'. C.f. case 5D.5.
9500 (c-backward-syntactic-ws lim
)
9501 (and (eq (char-before) ?
})
9503 (let ((start (point)))
9504 (if (and c-state-cache
9505 (consp (car c-state-cache
))
9506 (eq (cdar c-state-cache
) (point)))
9507 ;; Speed up the backward search a bit.
9508 (goto-char (caar c-state-cache
)))
9509 (c-beginning-of-decl-1 containing-sexp
)
9510 (setq placeholder
(point))
9511 (if (= start
(point))
9512 ;; The '}' is unbalanced.
9515 (>= (point) indent-point
))))))
9516 (goto-char placeholder
)
9517 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9518 containing-sexp paren-state
))
9520 ;; NOTE: The point is at the end of the previous token here.
9522 ;; CASE 5J: we are at the topmost level, make
9523 ;; sure we skip back past any access specifiers
9525 ;; A macro continuation line is never at top level.
9526 (not (and macro-start
9527 (> indent-point macro-start
)))
9529 (setq placeholder
(point))
9530 (or (memq char-before-ip
'(?\
; ?{ ?} nil))
9531 (c-at-vsemi-p before-ws-ip
)
9532 (when (and (eq char-before-ip ?
:)
9533 (eq (c-beginning-of-statement-1 lim
)
9535 (c-backward-syntactic-ws lim
)
9536 (setq placeholder
(point)))
9537 (and (c-major-mode-is 'objc-mode
)
9538 (catch 'not-in-directive
9539 (c-beginning-of-statement-1 lim
)
9540 (setq placeholder
(point))
9541 (while (and (c-forward-objc-directive)
9542 (< (point) indent-point
))
9543 (c-forward-syntactic-ws)
9544 (if (>= (point) indent-point
)
9545 (throw 'not-in-directive t
))
9546 (setq placeholder
(point)))
9548 ;; For historic reasons we anchor at bol of the last
9549 ;; line of the previous declaration. That's clearly
9550 ;; highly bogus and useless, and it makes our lives hard
9551 ;; to remain compatible. :P
9552 (goto-char placeholder
)
9553 (c-add-syntax 'topmost-intro
(c-point 'bol
))
9554 (if containing-decl-open
9555 (if (c-keyword-member containing-decl-kwd
9556 'c-other-block-decl-kwds
)
9558 (goto-char (c-brace-anchor-point containing-decl-open
))
9560 (if (string-equal (symbol-name containing-decl-kwd
)
9562 ;; Special case for compatibility with the
9563 ;; extern-lang syntactic symbols.
9565 (intern (concat "in"
9566 (symbol-name containing-decl-kwd
))))
9568 (c-most-enclosing-brace paren-state
(point))
9570 (c-add-class-syntax 'inclass
9571 containing-decl-open
9572 containing-decl-start
9575 (when (and c-syntactic-indentation-in-macros
9577 (/= macro-start
(c-point 'boi indent-point
)))
9578 (c-add-syntax 'cpp-define-intro
)
9579 (setq macro-start nil
)))
9581 ;; CASE 5K: we are at an ObjC method definition
9582 ;; continuation line.
9583 ((and c-opt-method-key
9585 (c-beginning-of-statement-1 lim
)
9587 (when (looking-at c-opt-method-key
)
9588 (setq placeholder
(point)))))
9589 (c-add-syntax 'objc-method-args-cont placeholder
))
9591 ;; CASE 5L: we are at the first argument of a template
9592 ;; arglist that begins on the previous line.
9593 ((and c-recognize-
<>-arglists
9594 (eq (char-before) ?
<)
9595 (not (and c-overloadable-operators-regexp
9596 (c-after-special-operator-id lim
))))
9597 (c-beginning-of-statement-1 (c-safe-position (point) paren-state
))
9598 (c-add-syntax 'template-args-cont
(c-point 'boi
)))
9600 ;; CASE 5Q: we are at a statement within a macro.
9602 (c-beginning-of-statement-1 containing-sexp
)
9603 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state
))
9605 ;;CASE 5N: We are at a tompmost continuation line and the only
9606 ;;preceding items are annotations.
9607 ((and (c-major-mode-is 'java-mode
)
9608 (setq placeholder
(point))
9609 (c-beginning-of-statement-1)
9611 (while (and (c-forward-annotation))
9612 (c-forward-syntactic-ws))
9615 (>= (point) placeholder
)
9616 (goto-char placeholder
)))
9617 (c-add-syntax 'annotation-top-cont
(c-point 'boi
)))
9619 ;; CASE 5M: we are at a topmost continuation line
9621 (c-beginning-of-statement-1 (c-safe-position (point) paren-state
))
9622 (when (c-major-mode-is 'objc-mode
)
9623 (setq placeholder
(point))
9624 (while (and (c-forward-objc-directive)
9625 (< (point) indent-point
))
9626 (c-forward-syntactic-ws)
9627 (setq placeholder
(point)))
9628 (goto-char placeholder
))
9629 (c-add-syntax 'topmost-intro-cont
(c-point 'boi
)))
9633 ;; (CASE 6 has been removed.)
9635 ;; CASE 7: line is an expression, not a statement. Most
9636 ;; likely we are either in a function prototype or a function
9637 ;; call argument list
9638 ((not (or (and c-special-brace-lists
9640 (goto-char containing-sexp
)
9641 (c-looking-at-special-brace-list)))
9642 (eq (char-after containing-sexp
) ?
{)))
9645 ;; CASE 7A: we are looking at the arglist closing paren.
9647 ((memq char-after-ip
'(?\
) ?\
]))
9648 (goto-char containing-sexp
)
9649 (setq placeholder
(c-point 'boi
))
9650 (if (and (c-safe (backward-up-list 1) t
)
9651 (>= (point) placeholder
))
9654 (skip-chars-forward " \t"))
9655 (goto-char placeholder
))
9656 (c-add-stmt-syntax 'arglist-close
(list containing-sexp
) t
9657 (c-most-enclosing-brace paren-state
(point))
9660 ;; CASE 7B: Looking at the opening brace of an
9661 ;; in-expression block or brace list. C.f. cases 4, 16A
9663 ((and (eq char-after-ip ?
{)
9665 (setq placeholder
(c-inside-bracelist-p (point)
9668 (setq tmpsymbol
'(brace-list-open . inexpr-class
))
9669 (setq tmpsymbol
'(block-open . inexpr-statement
)
9671 (cdr-safe (c-looking-at-inexpr-block
9672 (c-safe-position containing-sexp
9675 ;; placeholder is nil if it's a block directly in
9676 ;; a function arglist. That makes us skip out of
9679 (goto-char placeholder
)
9680 (back-to-indentation)
9681 (c-add-stmt-syntax (car tmpsymbol
) nil t
9682 (c-most-enclosing-brace paren-state
(point))
9684 (if (/= (point) placeholder
)
9685 (c-add-syntax (cdr tmpsymbol
))))
9687 ;; CASE 7C: we are looking at the first argument in an empty
9688 ;; argument list. Use arglist-close if we're actually
9689 ;; looking at a close paren or bracket.
9690 ((memq char-before-ip
'(?\
( ?\
[))
9691 (goto-char containing-sexp
)
9692 (setq placeholder
(c-point 'boi
))
9693 (if (and (c-safe (backward-up-list 1) t
)
9694 (>= (point) placeholder
))
9697 (skip-chars-forward " \t"))
9698 (goto-char placeholder
))
9699 (c-add-stmt-syntax 'arglist-intro
(list containing-sexp
) t
9700 (c-most-enclosing-brace paren-state
(point))
9703 ;; CASE 7D: we are inside a conditional test clause. treat
9704 ;; these things as statements
9706 (goto-char containing-sexp
)
9707 (and (c-safe (c-forward-sexp -
1) t
)
9708 (looking-at "\\<for\\>[^_]")))
9709 (goto-char (1+ containing-sexp
))
9710 (c-forward-syntactic-ws indent-point
)
9711 (if (eq char-before-ip ?\
;)
9712 (c-add-syntax 'statement
(point))
9713 (c-add-syntax 'statement-cont
(point))
9716 ;; CASE 7E: maybe a continued ObjC method call. This is the
9717 ;; case when we are inside a [] bracketed exp, and what
9718 ;; precede the opening bracket is not an identifier.
9719 ((and c-opt-method-key
9720 (eq (char-after containing-sexp
) ?\
[)
9722 (goto-char (1- containing-sexp
))
9723 (c-backward-syntactic-ws (c-point 'bod
))
9724 (if (not (looking-at c-symbol-key
))
9725 (c-add-syntax 'objc-method-call-cont containing-sexp
))
9728 ;; CASE 7F: we are looking at an arglist continuation line,
9729 ;; but the preceding argument is on the same line as the
9730 ;; opening paren. This case includes multi-line
9731 ;; mathematical paren groupings, but we could be on a
9732 ;; for-list continuation line. C.f. case 7A.
9734 (goto-char (1+ containing-sexp
))
9736 (c-forward-syntactic-ws)
9739 (goto-char containing-sexp
) ; paren opening the arglist
9740 (setq placeholder
(c-point 'boi
))
9741 (if (and (c-safe (backward-up-list 1) t
)
9742 (>= (point) placeholder
))
9745 (skip-chars-forward " \t"))
9746 (goto-char placeholder
))
9747 (c-add-stmt-syntax 'arglist-cont-nonempty
(list containing-sexp
) t
9748 (c-most-enclosing-brace c-state-cache
(point))
9751 ;; CASE 7G: we are looking at just a normal arglist
9752 ;; continuation line
9753 (t (c-forward-syntactic-ws indent-point
)
9754 (c-add-syntax 'arglist-cont
(c-point 'boi
)))
9757 ;; CASE 8: func-local multi-inheritance line
9758 ((and (c-major-mode-is 'c
++-mode
)
9760 (goto-char indent-point
)
9761 (skip-chars-forward " \t")
9762 (looking-at c-opt-postfix-decl-spec-key
)))
9763 (goto-char indent-point
)
9764 (skip-chars-forward " \t")
9767 ;; CASE 8A: non-hanging colon on an inher intro
9768 ((eq char-after-ip ?
:)
9769 (c-backward-syntactic-ws lim
)
9770 (c-add-syntax 'inher-intro
(c-point 'boi
)))
9772 ;; CASE 8B: hanging colon on an inher intro
9773 ((eq char-before-ip ?
:)
9774 (c-add-syntax 'inher-intro
(c-point 'boi
)))
9776 ;; CASE 8C: a continued inheritance line
9778 (c-beginning-of-inheritance-list lim
)
9779 (c-add-syntax 'inher-cont
(point))
9782 ;; CASE 9: we are inside a brace-list
9783 ((and (not (c-major-mode-is 'awk-mode
)) ; Maybe this isn't needed (ACM, 2002/3/29)
9784 (setq special-brace-list
9785 (or (and c-special-brace-lists
;;;; ALWAYS NIL FOR AWK!!
9787 (goto-char containing-sexp
)
9788 (c-looking-at-special-brace-list)))
9789 (c-inside-bracelist-p containing-sexp paren-state
))))
9792 ;; CASE 9A: In the middle of a special brace list opener.
9793 ((and (consp special-brace-list
)
9795 (goto-char containing-sexp
)
9796 (eq (char-after) ?\
())
9797 (eq char-after-ip
(car (cdr special-brace-list
))))
9798 (goto-char (car (car special-brace-list
)))
9799 (skip-chars-backward " \t")
9801 (assoc 'statement-cont
9802 (setq placeholder
(c-guess-basic-syntax))))
9803 (setq c-syntactic-context placeholder
)
9804 (c-beginning-of-statement-1
9805 (c-safe-position (1- containing-sexp
) paren-state
))
9806 (c-forward-token-2 0)
9807 (while (looking-at c-specifier-key
)
9808 (goto-char (match-end 1))
9809 (c-forward-syntactic-ws))
9810 (c-add-syntax 'brace-list-open
(c-point 'boi
))))
9812 ;; CASE 9B: brace-list-close brace
9813 ((if (consp special-brace-list
)
9814 ;; Check special brace list closer.
9816 (goto-char (car (car special-brace-list
)))
9818 (goto-char indent-point
)
9819 (back-to-indentation)
9821 ;; We were between the special close char and the `)'.
9822 (and (eq (char-after) ?\
))
9823 (eq (1+ (point)) (cdr (car special-brace-list
))))
9824 ;; We were before the special close char.
9825 (and (eq (char-after) (cdr (cdr special-brace-list
)))
9826 (zerop (c-forward-token-2))
9827 (eq (1+ (point)) (cdr (car special-brace-list
)))))))
9828 ;; Normal brace list check.
9829 (and (eq char-after-ip ?
})
9830 (c-safe (goto-char (c-up-list-backward (point))) t
)
9831 (= (point) containing-sexp
)))
9832 (if (eq (point) (c-point 'boi
))
9833 (c-add-syntax 'brace-list-close
(point))
9834 (setq lim
(c-most-enclosing-brace c-state-cache
(point)))
9835 (c-beginning-of-statement-1 lim
)
9836 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state
)))
9839 ;; Prepare for the rest of the cases below by going to the
9840 ;; token following the opening brace
9841 (if (consp special-brace-list
)
9843 (goto-char (car (car special-brace-list
)))
9844 (c-forward-token-2 1 nil indent-point
))
9845 (goto-char containing-sexp
))
9847 (let ((start (point)))
9848 (c-forward-syntactic-ws indent-point
)
9849 (goto-char (max start
(c-point 'bol
))))
9850 (c-skip-ws-forward indent-point
)
9853 ;; CASE 9C: we're looking at the first line in a brace-list
9854 ((= (point) indent-point
)
9855 (if (consp special-brace-list
)
9856 (goto-char (car (car special-brace-list
)))
9857 (goto-char containing-sexp
))
9858 (if (eq (point) (c-point 'boi
))
9859 (c-add-syntax 'brace-list-intro
(point))
9860 (setq lim
(c-most-enclosing-brace c-state-cache
(point)))
9861 (c-beginning-of-statement-1 lim
)
9862 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state
)))
9864 ;; CASE 9D: this is just a later brace-list-entry or
9866 (t (if (or (eq char-after-ip ?
{)
9867 (and c-special-brace-lists
9869 (goto-char indent-point
)
9870 (c-forward-syntactic-ws (c-point 'eol
))
9871 (c-looking-at-special-brace-list (point)))))
9872 (c-add-syntax 'brace-entry-open
(point))
9873 (c-add-syntax 'brace-list-entry
(point))
9877 ;; CASE 10: A continued statement or top level construct.
9878 ((and (not (memq char-before-ip
'(?\
; ?:)))
9879 (not (c-at-vsemi-p before-ws-ip
))
9880 (or (not (eq char-before-ip ?
}))
9881 (c-looking-at-inexpr-block-backward c-state-cache
))
9884 (c-beginning-of-statement-1 containing-sexp
)
9885 (setq placeholder
(point))))
9886 (/= placeholder containing-sexp
))
9887 ;; This is shared with case 18.
9888 (c-guess-continued-construct indent-point
9894 ;; CASE 16: block close brace, possibly closing the defun or
9896 ((eq char-after-ip ?
})
9897 ;; From here on we have the next containing sexp in lim.
9898 (setq lim
(c-most-enclosing-brace paren-state
))
9899 (goto-char containing-sexp
)
9902 ;; CASE 16E: Closing a statement block? This catches
9903 ;; cases where it's preceded by a statement keyword,
9904 ;; which works even when used in an "invalid" context,
9905 ;; e.g. a macro argument.
9906 ((c-after-conditional)
9907 (c-backward-to-block-anchor lim
)
9908 (c-add-stmt-syntax 'block-close nil t lim paren-state
))
9910 ;; CASE 16A: closing a lambda defun or an in-expression
9911 ;; block? C.f. cases 4, 7B and 17E.
9912 ((setq placeholder
(c-looking-at-inexpr-block
9913 (c-safe-position containing-sexp paren-state
)
9915 (setq tmpsymbol
(if (eq (car placeholder
) 'inlambda
)
9918 (goto-char containing-sexp
)
9919 (back-to-indentation)
9920 (if (= containing-sexp
(point))
9921 (c-add-syntax tmpsymbol
(point))
9922 (goto-char (cdr placeholder
))
9923 (back-to-indentation)
9924 (c-add-stmt-syntax tmpsymbol nil t
9925 (c-most-enclosing-brace paren-state
(point))
9927 (if (/= (point) (cdr placeholder
))
9928 (c-add-syntax (car placeholder
)))))
9930 ;; CASE 16B: does this close an inline or a function in
9931 ;; a non-class declaration level block?
9936 (c-looking-at-decl-block
9937 (c-most-enclosing-brace paren-state lim
)
9939 (setq placeholder
(point))))
9940 (c-backward-to-decl-anchor lim
)
9941 (back-to-indentation)
9943 (goto-char placeholder
)
9944 (looking-at c-other-decl-block-key
))
9945 (c-add-syntax 'defun-close
(point))
9946 (c-add-syntax 'inline-close
(point))))
9948 ;; CASE 16F: Can be a defun-close of a function declared
9949 ;; in a statement block, e.g. in Pike or when using gcc
9950 ;; extensions, but watch out for macros followed by
9951 ;; blocks. Let it through to be handled below.
9952 ;; C.f. cases B.3 and 17G.
9954 (and (not (c-at-statement-start-p))
9955 (eq (c-beginning-of-statement-1 lim nil nil t
) 'same
)
9956 (setq placeholder
(point))
9957 (let ((c-recognize-typeless-decls nil
))
9958 ;; Turn off recognition of constructs that
9959 ;; lacks a type in this case, since that's more
9960 ;; likely to be a macro followed by a block.
9961 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
9962 (back-to-indentation)
9963 (if (/= (point) containing-sexp
)
9964 (goto-char placeholder
))
9965 (c-add-stmt-syntax 'defun-close nil t lim paren-state
))
9967 ;; CASE 16C: If there is an enclosing brace then this is
9968 ;; a block close since defun closes inside declaration
9969 ;; level blocks have been handled above.
9971 ;; If the block is preceded by a case/switch label on
9972 ;; the same line, we anchor at the first preceding label
9973 ;; at boi. The default handling in c-add-stmt-syntax
9974 ;; really fixes it better, but we do like this to keep
9975 ;; the indentation compatible with version 5.28 and
9976 ;; earlier. C.f. case 17H.
9977 (while (and (/= (setq placeholder
(point)) (c-point 'boi
))
9978 (eq (c-beginning-of-statement-1 lim
) 'label
)))
9979 (goto-char placeholder
)
9980 (if (looking-at c-label-kwds-regexp
)
9981 (c-add-syntax 'block-close
(point))
9982 (goto-char containing-sexp
)
9983 ;; c-backward-to-block-anchor not necessary here; those
9984 ;; situations are handled in case 16E above.
9985 (c-add-stmt-syntax 'block-close nil t lim paren-state
)))
9987 ;; CASE 16D: Only top level defun close left.
9989 (goto-char containing-sexp
)
9990 (c-backward-to-decl-anchor lim
)
9991 (c-add-stmt-syntax 'defun-close nil nil
9992 (c-most-enclosing-brace paren-state
)
9996 ;; CASE 19: line is an expression, not a statement, and is directly
9997 ;; contained by a template delimiter. Most likely, we are in a
9998 ;; template arglist within a statement. This case is based on CASE
9999 ;; 7. At some point in the future, we may wish to create more
10000 ;; syntactic symbols such as `template-intro',
10001 ;; `template-cont-nonempty', etc., and distinguish between them as we
10002 ;; do for `arglist-intro' etc. (2009-12-07).
10003 ((and c-recognize-
<>-arglists
10004 (setq containing-
< (c-up-list-backward indent-point containing-sexp
))
10005 (eq (char-after containing-
<) ?\
<))
10006 (setq placeholder
(c-point 'boi containing-
<))
10007 (goto-char containing-sexp
) ; Most nested Lbrace/Lparen (but not
10008 ; '<') before indent-point.
10009 (if (>= (point) placeholder
)
10012 (skip-chars-forward " \t"))
10013 (goto-char placeholder
))
10014 (c-add-stmt-syntax 'template-args-cont
(list containing-
<) t
10015 (c-most-enclosing-brace c-state-cache
(point))
10018 ;; CASE 17: Statement or defun catchall.
10020 (goto-char indent-point
)
10021 ;; Back up statements until we find one that starts at boi.
10022 (while (let* ((prev-point (point))
10023 (last-step-type (c-beginning-of-statement-1
10025 (if (= (point) prev-point
)
10027 (setq step-type
(or step-type last-step-type
))
10029 (setq step-type last-step-type
)
10030 (/= (point) (c-point 'boi
)))))
10033 ;; CASE 17B: continued statement
10034 ((and (eq step-type
'same
)
10035 (/= (point) indent-point
))
10036 (c-add-stmt-syntax 'statement-cont nil nil
10037 containing-sexp paren-state
))
10039 ;; CASE 17A: After a case/default label?
10041 (while (and (eq step-type
'label
)
10042 (not (looking-at c-label-kwds-regexp
)))
10044 (c-beginning-of-statement-1 containing-sexp
)))
10045 (eq step-type
'label
))
10046 (c-add-stmt-syntax (if (eq char-after-ip ?
{)
10047 'statement-case-open
10048 'statement-case-intro
)
10049 nil t containing-sexp paren-state
))
10051 ;; CASE 17D: any old statement
10053 (while (eq step-type
'label
)
10055 (c-beginning-of-statement-1 containing-sexp
)))
10056 (eq step-type
'previous
))
10057 (c-add-stmt-syntax 'statement nil t
10058 containing-sexp paren-state
)
10059 (if (eq char-after-ip ?
{)
10060 (c-add-syntax 'block-open
)))
10062 ;; CASE 17I: Inside a substatement block.
10064 ;; The following tests are all based on containing-sexp.
10065 (goto-char containing-sexp
)
10066 ;; From here on we have the next containing sexp in lim.
10067 (setq lim
(c-most-enclosing-brace paren-state containing-sexp
))
10068 (c-after-conditional))
10069 (c-backward-to-block-anchor lim
)
10070 (c-add-stmt-syntax 'statement-block-intro nil t
10072 (if (eq char-after-ip ?
{)
10073 (c-add-syntax 'block-open
)))
10075 ;; CASE 17E: first statement in an in-expression block.
10076 ;; C.f. cases 4, 7B and 16A.
10077 ((setq placeholder
(c-looking-at-inexpr-block
10078 (c-safe-position containing-sexp paren-state
)
10080 (setq tmpsymbol
(if (eq (car placeholder
) 'inlambda
)
10082 'statement-block-intro
))
10083 (back-to-indentation)
10084 (if (= containing-sexp
(point))
10085 (c-add-syntax tmpsymbol
(point))
10086 (goto-char (cdr placeholder
))
10087 (back-to-indentation)
10088 (c-add-stmt-syntax tmpsymbol nil t
10089 (c-most-enclosing-brace c-state-cache
(point))
10091 (if (/= (point) (cdr placeholder
))
10092 (c-add-syntax (car placeholder
))))
10093 (if (eq char-after-ip ?
{)
10094 (c-add-syntax 'block-open
)))
10096 ;; CASE 17F: first statement in an inline, or first
10097 ;; statement in a top-level defun. we can tell this is it
10098 ;; if there are no enclosing braces that haven't been
10099 ;; narrowed out by a class (i.e. don't use bod here).
10101 (or (not (setq placeholder
(c-most-enclosing-brace
10104 (goto-char placeholder
)
10105 (eq (char-after) ?
{))
10106 (c-looking-at-decl-block (c-most-enclosing-brace
10107 paren-state
(point))
10109 (c-backward-to-decl-anchor lim
)
10110 (back-to-indentation)
10111 (c-add-syntax 'defun-block-intro
(point)))
10113 ;; CASE 17G: First statement in a function declared inside
10114 ;; a normal block. This can occur in Pike and with
10115 ;; e.g. the gcc extensions, but watch out for macros
10116 ;; followed by blocks. C.f. cases B.3 and 16F.
10118 (and (not (c-at-statement-start-p))
10119 (eq (c-beginning-of-statement-1 lim nil nil t
) 'same
)
10120 (setq placeholder
(point))
10121 (let ((c-recognize-typeless-decls nil
))
10122 ;; Turn off recognition of constructs that lacks
10123 ;; a type in this case, since that's more likely
10124 ;; to be a macro followed by a block.
10125 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
10126 (back-to-indentation)
10127 (if (/= (point) containing-sexp
)
10128 (goto-char placeholder
))
10129 (c-add-stmt-syntax 'defun-block-intro nil t
10132 ;; CASE 17H: First statement in a block.
10134 ;; If the block is preceded by a case/switch label on the
10135 ;; same line, we anchor at the first preceding label at
10136 ;; boi. The default handling in c-add-stmt-syntax is
10137 ;; really fixes it better, but we do like this to keep the
10138 ;; indentation compatible with version 5.28 and earlier.
10140 (while (and (/= (setq placeholder
(point)) (c-point 'boi
))
10141 (eq (c-beginning-of-statement-1 lim
) 'label
)))
10142 (goto-char placeholder
)
10143 (if (looking-at c-label-kwds-regexp
)
10144 (c-add-syntax 'statement-block-intro
(point))
10145 (goto-char containing-sexp
)
10146 ;; c-backward-to-block-anchor not necessary here; those
10147 ;; situations are handled in case 17I above.
10148 (c-add-stmt-syntax 'statement-block-intro nil t
10150 (if (eq char-after-ip ?
{)
10151 (c-add-syntax 'block-open
)))
10155 ;; now we need to look at any modifiers
10156 (goto-char indent-point
)
10157 (skip-chars-forward " \t")
10159 ;; are we looking at a comment only line?
10160 (when (and (looking-at c-comment-start-regexp
)
10161 (/= (c-forward-token-2 0 nil
(c-point 'eol
)) 0))
10162 (c-append-syntax 'comment-intro
))
10164 ;; we might want to give additional offset to friends (in C++).
10165 (when (and c-opt-friend-key
10166 (looking-at c-opt-friend-key
))
10167 (c-append-syntax 'friend
))
10169 ;; Set syntactic-relpos.
10170 (let ((p c-syntactic-context
))
10172 (if (integerp (c-langelem-pos (car p
)))
10174 (setq syntactic-relpos
(c-langelem-pos (car p
)))
10179 ;; Start of or a continuation of a preprocessor directive?
10180 (if (and macro-start
10181 (eq macro-start
(c-point 'boi
))
10182 (not (and (c-major-mode-is 'pike-mode
)
10183 (eq (char-after (1+ macro-start
)) ?
\"))))
10184 (c-append-syntax 'cpp-macro
)
10185 (when (and c-syntactic-indentation-in-macros macro-start
)
10188 (< syntactic-relpos macro-start
)
10190 (assq 'arglist-intro c-syntactic-context
)
10191 (assq 'arglist-cont c-syntactic-context
)
10192 (assq 'arglist-cont-nonempty c-syntactic-context
)
10193 (assq 'arglist-close c-syntactic-context
))))
10194 ;; If inside a cpp expression, i.e. anywhere in a
10195 ;; cpp directive except a #define body, we only let
10196 ;; through the syntactic analysis that is internal
10197 ;; in the expression. That means the arglist
10198 ;; elements, if they are anchored inside the cpp
10200 (setq c-syntactic-context nil
)
10201 (c-add-syntax 'cpp-macro-cont macro-start
))
10202 (when (and (eq macro-start syntactic-relpos
)
10203 (not (assq 'cpp-define-intro c-syntactic-context
))
10205 (goto-char macro-start
)
10206 (or (not (c-forward-to-cpp-define-body))
10207 (<= (point) (c-point 'boi indent-point
)))))
10208 ;; Inside a #define body and the syntactic analysis is
10209 ;; anchored on the start of the #define. In this case
10210 ;; we add cpp-define-intro to get the extra
10211 ;; indentation of the #define body.
10212 (c-add-syntax 'cpp-define-intro
)))))
10214 ;; return the syntax
10215 c-syntactic-context
)))
10218 ;; Indentation calculation.
10220 (defun c-evaluate-offset (offset langelem symbol
)
10221 ;; offset can be a number, a function, a variable, a list, or one of
10222 ;; the symbols + or -
10224 ;; This function might do hidden buffer changes.
10227 ((numberp offset
) offset
)
10228 ((vectorp offset
) offset
)
10229 ((null offset
) nil
)
10231 ((eq offset
'+) c-basic-offset
)
10232 ((eq offset
'-
) (- c-basic-offset
))
10233 ((eq offset
'++) (* 2 c-basic-offset
))
10234 ((eq offset
'--
) (* 2 (- c-basic-offset
)))
10235 ((eq offset
'*) (/ c-basic-offset
2))
10236 ((eq offset
'/) (/ (- c-basic-offset
) 2))
10238 ((functionp offset
)
10241 (cons (c-langelem-sym langelem
)
10242 (c-langelem-pos langelem
)))
10247 ((eq (car offset
) 'quote
)
10248 (c-benign-error "The offset %S for %s was mistakenly quoted"
10252 ((memq (car offset
) '(min max
))
10253 (let (res val
(method (car offset
)))
10254 (setq offset
(cdr offset
))
10256 (setq val
(c-evaluate-offset (car offset
) langelem symbol
))
10264 Error evaluating offset %S for %s: \
10265 Cannot combine absolute offset %S with relative %S in `%s' method"
10266 (car offset
) symbol res val method
)
10267 (setq res
(funcall method res val
))))
10271 Error evaluating offset %S for %s: \
10272 Cannot combine relative offset %S with absolute %S in `%s' method"
10273 (car offset
) symbol res val method
)
10274 (setq res
(vector (funcall method
(aref res
0)
10276 (setq offset
(cdr offset
)))
10279 ((eq (car offset
) 'add
)
10281 (setq offset
(cdr offset
))
10283 (setq val
(c-evaluate-offset (car offset
) langelem symbol
))
10290 (setq res
(vector (+ (aref res
0) val
)))
10291 (setq res
(+ res val
))))
10295 Error evaluating offset %S for %s: \
10296 Cannot combine absolute offsets %S and %S in `add' method"
10297 (car offset
) symbol res val
)
10298 (setq res val
)))) ; Override.
10299 (setq offset
(cdr offset
)))
10304 (when (eq (car offset
) 'first
)
10305 (setq offset
(cdr offset
)))
10306 (while (and (not res
) offset
)
10307 (setq res
(c-evaluate-offset (car offset
) langelem symbol
)
10308 offset
(cdr offset
)))
10311 ((and (symbolp offset
) (boundp offset
))
10312 (symbol-value offset
))
10315 (c-benign-error "Unknown offset format %S for %s" offset symbol
)
10318 (if (or (null res
) (integerp res
)
10319 (and (vectorp res
) (= (length res
) 1) (integerp (aref res
0))))
10321 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10325 (defun c-calc-offset (langelem)
10326 ;; Get offset from LANGELEM which is a list beginning with the
10327 ;; syntactic symbol and followed by any analysis data it provides.
10328 ;; That data may be zero or more elements, but if at least one is
10329 ;; given then the first is the anchor position (or nil). The symbol
10330 ;; is matched against `c-offsets-alist' and the offset calculated
10331 ;; from that is returned.
10333 ;; This function might do hidden buffer changes.
10334 (let* ((symbol (c-langelem-sym langelem
))
10335 (match (assq symbol c-offsets-alist
))
10336 (offset (cdr-safe match
)))
10338 (setq offset
(c-evaluate-offset offset langelem symbol
))
10339 (if c-strict-syntax-p
10340 (c-benign-error "No offset found for syntactic symbol %s" symbol
))
10342 (if (vectorp offset
)
10344 (or (and (numberp offset
) offset
)
10345 (and (symbolp offset
) (symbol-value offset
))
10349 (defun c-get-offset (langelem)
10350 ;; This is a compatibility wrapper for `c-calc-offset' in case
10351 ;; someone is calling it directly. It takes an old style syntactic
10352 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10355 ;; This function might do hidden buffer changes.
10356 (if (c-langelem-pos langelem
)
10357 (c-calc-offset (list (c-langelem-sym langelem
)
10358 (c-langelem-pos langelem
)))
10359 (c-calc-offset langelem
)))
10361 (defun c-get-syntactic-indentation (langelems)
10362 ;; Calculate the syntactic indentation from a syntactic description
10363 ;; as returned by `c-guess-syntax'.
10365 ;; Note that topmost-intro always has an anchor position at bol, for
10366 ;; historical reasons. It's often used together with other symbols
10367 ;; that has more sane positions. Since we always use the first
10368 ;; found anchor position, we rely on that these other symbols always
10369 ;; precede topmost-intro in the LANGELEMS list.
10371 ;; This function might do hidden buffer changes.
10372 (let ((indent 0) anchor
)
10375 (let* ((c-syntactic-element (car langelems
))
10376 (res (c-calc-offset c-syntactic-element
)))
10379 ;; Got an absolute column that overrides any indentation
10380 ;; we've collected so far, but not the relative
10381 ;; indentation we might get for the nested structures
10382 ;; further down the langelems list.
10383 (setq indent
(elt res
0)
10384 anchor
(point-min)) ; A position at column 0.
10386 ;; Got a relative change of the current calculated
10388 (setq indent
(+ indent res
))
10390 ;; Use the anchor position from the first syntactic
10391 ;; element with one.
10393 (setq anchor
(c-langelem-pos (car langelems
)))))
10395 (setq langelems
(cdr langelems
))))
10398 (+ indent
(save-excursion
10404 (cc-provide 'cc-engine
)
10406 ;;; cc-engine.el ends here