1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985, 1987, 1992-2012 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
))))
199 ;; Basic handling of preprocessor directives.
201 ;; This is a dynamically bound cache used together with
202 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
203 ;; works as long as point doesn't cross a macro boundary.
204 (defvar c-macro-start
'unknown
)
206 (defsubst c-query-and-set-macro-start
()
207 (if (symbolp c-macro-start
)
208 (setq c-macro-start
(save-excursion
209 (c-save-buffer-state ()
210 (and (c-beginning-of-macro)
214 (defsubst c-query-macro-start
()
215 (if (symbolp c-macro-start
)
217 (c-save-buffer-state ()
218 (and (c-beginning-of-macro)
222 (defun c-beginning-of-macro (&optional lim
)
223 "Go to the beginning of a preprocessor directive.
224 Leave point at the beginning of the directive and return t if in one,
225 otherwise return nil and leave point unchanged.
227 Note that this function might do hidden buffer changes. See the
228 comment at the start of cc-engine.el for more info."
229 (when c-opt-cpp-prefix
230 (let ((here (point)))
232 (if lim
(narrow-to-region lim
(point-max)))
234 (while (eq (char-before (1- (point))) ?
\\)
236 (back-to-indentation)
237 (if (and (<= (point) here
)
238 (looking-at c-opt-cpp-start
))
243 (defun c-end-of-macro ()
244 "Go to the end of a preprocessor directive.
245 More accurately, move the point to the end of the closest following
246 line that doesn't end with a line continuation backslash - no check is
247 done that the point is inside a cpp directive to begin with.
249 Note that this function might do hidden buffer changes. See the
250 comment at the start of cc-engine.el for more info."
253 (when (and (eq (char-before) ?
\\)
258 (defun c-syntactic-end-of-macro ()
259 ;; Go to the end of a CPP directive, or a "safe" pos just before.
261 ;; This is normally the end of the next non-escaped line. A "safe"
262 ;; position is one not within a string or comment. (The EOL on a line
263 ;; comment is NOT "safe").
265 ;; This function must only be called from the beginning of a CPP construct.
267 ;; Note that this function might do hidden buffer changes. See the comment
268 ;; at the start of cc-engine.el for more info.
269 (let* ((here (point))
270 (there (progn (c-end-of-macro) (point)))
271 (s (parse-partial-sexp here there
)))
272 (while (and (or (nth 3 s
) ; in a string
273 (nth 4 s
)) ; in a comment (maybe at end of line comment)
274 (> there here
)) ; No infinite loops, please.
275 (setq there
(1- (nth 8 s
)))
276 (setq s
(parse-partial-sexp here there
)))
279 (defun c-forward-over-cpp-define-id ()
280 ;; Assuming point is at the "#" that introduces a preprocessor
281 ;; directive, it's moved forward to the end of the identifier which is
282 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
283 ;; is returned in this case, in all other cases nil is returned and
284 ;; point isn't moved.
286 ;; This function might do hidden buffer changes.
287 (when (and c-opt-cpp-macro-define-id
288 (looking-at c-opt-cpp-macro-define-id
))
289 (goto-char (match-end 0))))
291 (defun c-forward-to-cpp-define-body ()
292 ;; Assuming point is at the "#" that introduces a preprocessor
293 ;; directive, it's moved forward to the start of the definition body
294 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
295 ;; specifies). Non-nil is returned in this case, in all other cases
296 ;; nil is returned and point isn't moved.
298 ;; This function might do hidden buffer changes.
299 (when (and c-opt-cpp-macro-define-start
300 (looking-at c-opt-cpp-macro-define-start
)
301 (not (= (match-end 0) (c-point 'eol
))))
302 (goto-char (match-end 0))))
305 ;;; Basic utility functions.
307 (defun c-syntactic-content (from to paren-level
)
308 ;; Return the given region as a string where all syntactic
309 ;; whitespace is removed or, where necessary, replaced with a single
310 ;; space. If PAREN-LEVEL is given then all parens in the region are
311 ;; collapsed to "()", "[]" etc.
313 ;; This function might do hidden buffer changes.
317 (narrow-to-region from to
)
319 (let* ((parts (list nil
)) (tail parts
) pos in-paren
)
321 (while (re-search-forward c-syntactic-ws-start to t
)
322 (goto-char (setq pos
(match-beginning 0)))
323 (c-forward-syntactic-ws)
329 (setq in-paren
(= (car (parse-partial-sexp from pos
1)) 1)
332 (if (and (> pos from
)
334 (looking-at "\\w\\|\\s_")
337 (looking-at "\\w\\|\\s_")))
339 (setcdr tail
(list (buffer-substring-no-properties from pos
)
341 (setq tail
(cddr tail
)))
342 (setcdr tail
(list (buffer-substring-no-properties from pos
)))
343 (setq tail
(cdr tail
)))
346 (when (= (car (parse-partial-sexp pos to -
1)) -
1)
347 (setcdr tail
(list (buffer-substring-no-properties
348 (1- (point)) (point))))
349 (setq tail
(cdr tail
))))
351 (setq from
(point))))
353 (setcdr tail
(list (buffer-substring-no-properties from to
)))
354 (apply 'concat
(cdr parts
))))))
356 (defun c-shift-line-indentation (shift-amt)
357 ;; Shift the indentation of the current line with the specified
358 ;; amount (positive inwards). The buffer is modified only if
359 ;; SHIFT-AMT isn't equal to zero.
360 (let ((pos (- (point-max) (point)))
361 (c-macro-start c-macro-start
)
363 (if (zerop shift-amt
)
365 ;; If we're on an empty line inside a macro, we take the point
366 ;; to be at the current indentation and shift it to the
367 ;; appropriate column. This way we don't treat the extra
368 ;; whitespace out to the line continuation as indentation.
369 (when (and (c-query-and-set-macro-start)
370 (looking-at "[ \t]*\\\\$")
372 (skip-chars-backward " \t")
376 (setq tmp-char-inserted t
))
378 (let ((col (current-indentation)))
379 (delete-region (c-point 'bol
) (c-point 'boi
))
381 (indent-to (+ col shift-amt
)))
382 (when tmp-char-inserted
384 ;; If initial point was within line's indentation and we're not on
385 ;; a line with a line continuation in a macro, position after the
386 ;; indentation. Else stay at same point in text.
387 (if (and (< (point) (c-point 'boi
))
388 (not tmp-char-inserted
))
389 (back-to-indentation)
390 (if (> (- (point-max) pos
) (point))
391 (goto-char (- (point-max) pos
))))))
393 (defsubst c-keyword-sym
(keyword)
394 ;; Return non-nil if the string KEYWORD is a known keyword. More
395 ;; precisely, the value is the symbol for the keyword in
396 ;; `c-keywords-obarray'.
397 (intern-soft keyword c-keywords-obarray
))
399 (defsubst c-keyword-member
(keyword-sym lang-constant
)
400 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
401 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
402 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
403 ;; nil then the result is nil.
404 (get keyword-sym lang-constant
))
406 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
407 (defconst c-string-syntax
(if (memq 'gen-string-delim c-emacs-features
)
411 ;; Regexp matching string limit syntax.
412 (defconst c-string-limit-regexp
(if (memq 'gen-string-delim c-emacs-features
)
416 ;; Regexp matching WS followed by string limit syntax.
417 (defconst c-ws
*-string-limit-regexp
418 (concat "[ \t]*\\(" c-string-limit-regexp
"\\)"))
420 ;; Holds formatted error strings for the few cases where parse errors
422 (defvar c-parsing-error nil
)
423 (make-variable-buffer-local 'c-parsing-error
)
425 (defun c-echo-parsing-error (&optional quiet
)
426 (when (and c-report-syntactic-errors c-parsing-error
(not quiet
))
427 (c-benign-error "%s" c-parsing-error
))
430 ;; Faces given to comments and string literals. This is used in some
431 ;; situations to speed up recognition; it isn't mandatory that font
432 ;; locking is in use. This variable is extended with the face in
433 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
434 (defvar c-literal-faces
435 (append '(font-lock-comment-face font-lock-string-face
)
436 (when (facep 'font-lock-comment-delimiter-face
)
438 '(font-lock-comment-delimiter-face))))
440 (defsubst c-put-c-type-property
(pos value
)
441 ;; Put a c-type property with the given value at POS.
442 (c-put-char-property pos
'c-type value
))
444 (defun c-clear-c-type-property (from to value
)
445 ;; Remove all occurrences of the c-type property that has the given
446 ;; value in the region between FROM and TO. VALUE is assumed to not
449 ;; Note: This assumes that c-type is put on single chars only; it's
450 ;; very inefficient if matching properties cover large regions.
454 (when (eq (get-text-property (point) 'c-type
) value
)
455 (c-clear-char-property (point) 'c-type
))
456 (goto-char (next-single-property-change (point) 'c-type nil to
))
460 ;; Some debug tools to visualize various special positions. This
461 ;; debug code isn't as portable as the rest of CC Mode.
463 (cc-bytecomp-defun overlays-in)
464 (cc-bytecomp-defun overlay-get)
465 (cc-bytecomp-defun overlay-start)
466 (cc-bytecomp-defun overlay-end)
467 (cc-bytecomp-defun delete-overlay)
468 (cc-bytecomp-defun overlay-put)
469 (cc-bytecomp-defun make-overlay)
471 (defun c-debug-add-face (beg end face
)
472 (c-save-buffer-state ((overlays (overlays-in beg end
)) overlay
)
474 (setq overlay
(car overlays
)
475 overlays
(cdr overlays
))
476 (when (eq (overlay-get overlay
'face
) face
)
477 (setq beg
(min beg
(overlay-start overlay
))
478 end
(max end
(overlay-end overlay
)))
479 (delete-overlay overlay
)))
480 (overlay-put (make-overlay beg end
) 'face face
)))
482 (defun c-debug-remove-face (beg end face
)
483 (c-save-buffer-state ((overlays (overlays-in beg end
)) overlay
484 (ol-beg beg
) (ol-end end
))
486 (setq overlay
(car overlays
)
487 overlays
(cdr overlays
))
488 (when (eq (overlay-get overlay
'face
) face
)
489 (setq ol-beg
(min ol-beg
(overlay-start overlay
))
490 ol-end
(max ol-end
(overlay-end overlay
)))
491 (delete-overlay overlay
)))
493 (overlay-put (make-overlay ol-beg beg
) 'face face
))
495 (overlay-put (make-overlay end ol-end
) 'face face
))))
498 ;; `c-beginning-of-statement-1' and accompanying stuff.
500 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
501 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
502 ;; better way should be implemented, but this will at least shut up
503 ;; the byte compiler.
504 (defvar c-maybe-labelp
)
506 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
508 ;; Macros used internally in c-beginning-of-statement-1 for the
509 ;; automaton actions.
510 (defmacro c-bos-push-state
()
511 '(setq stack
(cons (cons state saved-pos
)
513 (defmacro c-bos-pop-state
(&optional do-if-done
)
514 `(if (setq state
(car (car stack
))
515 saved-pos
(cdr (car stack
))
520 (defmacro c-bos-pop-state-and-retry
()
521 '(throw 'loop
(setq state
(car (car stack
))
522 saved-pos
(cdr (car stack
))
523 ;; Throw nil if stack is empty, else throw non-nil.
525 (defmacro c-bos-save-pos
()
526 '(setq saved-pos
(vector pos tok ptok pptok
)))
527 (defmacro c-bos-restore-pos
()
528 '(unless (eq (elt saved-pos
0) start
)
529 (setq pos
(elt saved-pos
0)
530 tok
(elt saved-pos
1)
531 ptok
(elt saved-pos
2)
532 pptok
(elt saved-pos
3))
535 (defmacro c-bos-save-error-info
(missing got
)
536 `(setq saved-pos
(vector pos
,missing
,got
)))
537 (defmacro c-bos-report-error
()
539 (setq c-parsing-error
540 (format "No matching `%s' found for `%s' on line %d"
543 (1+ (count-lines (point-min)
544 (c-point 'bol
(elt saved-pos
0))))))))
546 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
548 "Move to the start of the current statement or declaration, or to
549 the previous one if already at the beginning of one. Only
550 statements/declarations on the same level are considered, i.e. don't
551 move into or out of sexps (not even normal expression parentheses).
553 If point is already at the earliest statement within braces or parens,
554 this function doesn't move back into any whitespace preceding it; it
555 returns 'same in this case.
557 Stop at statement continuation tokens like \"else\", \"catch\",
558 \"finally\" and the \"while\" in \"do ... while\" if the start point
559 is within the continuation. If starting at such a token, move to the
560 corresponding statement start. If at the beginning of a statement,
561 move to the closest containing statement if there is any. This might
562 also stop at a continuation clause.
564 Labels are treated as part of the following statements if
565 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
566 statement start keyword.) Otherwise, each label is treated as a
569 Macros are ignored \(i.e. skipped over) unless point is within one, in
570 which case the content of the macro is treated as normal code. Aside
571 from any normal statement starts found in it, stop at the first token
572 of the content in the macro, i.e. the expression of an \"#if\" or the
573 start of the definition in a \"#define\". Also stop at start of
574 macros before leaving them.
577 'label if stopped at a label or \"case...:\" or \"default:\";
578 'same if stopped at the beginning of the current statement;
579 'up if stepped to a containing statement;
580 'previous if stepped to a preceding statement;
581 'beginning if stepped from a statement continuation clause to
583 'macro if stepped to a macro start.
584 Note that 'same and not 'label is returned if stopped at the same
585 label without crossing the colon character.
587 LIM may be given to limit the search. If the search hits the limit,
588 point will be left at the closest following token, or at the start
589 position if that is less ('same is returned in this case).
591 NOERROR turns off error logging to `c-parsing-error'.
593 Normally only ';' and virtual semicolons are considered to delimit
594 statements, but if COMMA-DELIM is non-nil then ',' is treated
597 Note that this function might do hidden buffer changes. See the
598 comment at the start of cc-engine.el for more info."
600 ;; The bulk of this function is a pushdown automaton that looks at statement
601 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
602 ;; purpose is to keep track of nested statements, ensuring that such
603 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
604 ;; does with nested braces/brackets/parentheses).
606 ;; Note: The position of a boundary is the following token.
608 ;; Beginning with the current token (the one following point), move back one
609 ;; sexp at a time (where a sexp is, more or less, either a token or the
610 ;; entire contents of a brace/bracket/paren pair). Each time a statement
611 ;; boundary is crossed or a "while"-like token is found, update the state of
612 ;; the PDA. Stop at the beginning of a statement when the stack (holding
613 ;; nested statement info) is empty and the position has been moved.
615 ;; The following variables constitute the PDA:
617 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
618 ;; scanned back over, 'boundary if we've just gone back over a
619 ;; statement boundary, or nil otherwise.
620 ;; state: takes one of the values (nil else else-boundary while
621 ;; while-boundary catch catch-boundary).
622 ;; nil means "no "while"-like token yet scanned".
623 ;; 'else, for example, means "just gone back over an else".
624 ;; 'else-boundary means "just gone back over a statement boundary
625 ;; immediately after having gone back over an else".
626 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
627 ;; of error reporting information.
628 ;; stack: The stack onto which the PDA pushes its state. Each entry
629 ;; consists of a saved value of state and saved-pos. An entry is
630 ;; pushed when we move back over a "continuation" token (e.g. else)
631 ;; and popped when we encounter the corresponding opening token
635 ;; The following diagram briefly outlines the PDA.
638 ;; "else": Push state, goto state `else'.
639 ;; "while": Push state, goto state `while'.
640 ;; "catch" or "finally": Push state, goto state `catch'.
641 ;; boundary: Pop state.
642 ;; other: Do nothing special.
645 ;; boundary: Goto state `else-boundary'.
646 ;; other: Error, pop state, retry token.
648 ;; State `else-boundary':
650 ;; boundary: Error, pop state.
651 ;; other: See common state.
654 ;; boundary: Save position, goto state `while-boundary'.
655 ;; other: Pop state, retry token.
657 ;; State `while-boundary':
659 ;; boundary: Restore position if it's not at start, pop state. [*see below]
660 ;; other: See common state.
663 ;; boundary: Goto state `catch-boundary'.
664 ;; other: Error, pop state, retry token.
666 ;; State `catch-boundary':
668 ;; "catch": Goto state `catch'.
669 ;; boundary: Error, pop state.
670 ;; other: See common state.
672 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
673 ;; searching for a "do" which would have opened a do-while. If we didn't
674 ;; find it, we discard the analysis done since the "while", go back to this
675 ;; token in the buffer and restart the scanning there, this time WITHOUT
676 ;; pushing the 'while state onto the stack.
678 ;; In addition to the above there is some special handling of labels
681 (let ((case-fold-search nil
)
684 (delims (if comma-delim
'(?\
; ?,) '(?\;)))
685 (c-stmt-delim-chars (if comma-delim
686 c-stmt-delim-chars-with-comma
688 c-in-literal-cache c-maybe-labelp after-case
:-pos saved
691 ;; Position of last stmt boundary character (e.g. ;).
693 ;; The position of the last sexp or bound that follows the
694 ;; first found colon, i.e. the start of the nonlabel part of
695 ;; the statement. It's `start' if a colon is found just after
698 ;; Like `after-labels-pos', but the first such position inside
699 ;; a label, i.e. the start of the last label before the start
700 ;; of the nonlabel part of the statement.
702 ;; The last position where a label is possible provided the
703 ;; statement started there. It's nil as long as no invalid
704 ;; label content has been found (according to
705 ;; `c-nonlabel-token-key'). It's `start' if no valid label
706 ;; content was found in the label. Note that we might still
707 ;; regard it a label if it starts with `c-label-kwds'.
709 ;; Putative positions of the components of a bitfield declaration,
710 ;; e.g. "int foo : NUM_FOO_BITS ;"
711 bitfield-type-pos bitfield-id-pos bitfield-size-pos
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.
769 ;; The following while loop goes back one sexp (balanced parens,
770 ;; etc. with contents, or symbol or suchlike) each iteration. This
771 ;; movement is accomplished with a call to c-backward-sexp approx 170
774 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
775 ;; 1. On reaching the start of a macro;
776 ;; 2. On having passed a stmt boundary with the PDA stack empty;
777 ;; 3. On reaching the start of an Objective C method def;
778 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
779 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
781 (catch 'loop
;; Throw nil to break, non-nil to continue.
783 ;; Are we in a macro, just after the opening #?
785 (and macro-start
; Always NIL for AWK.
786 (progn (skip-chars-backward " \t")
787 (eq (char-before) ?
#))
788 (progn (setq saved
(1- (point)))
790 (not (eq (char-before (1- (point))) ?
\\)))
791 (looking-at c-opt-cpp-start
)
792 (progn (skip-chars-forward " \t")
793 (eq (point) saved
))))
795 (if (and (c-forward-to-cpp-define-body)
796 (progn (c-forward-syntactic-ws start
)
798 ;; Stop at the first token in the content of the macro.
800 ignore-labels t
) ; Avoid the label check on exit.
804 (throw 'loop nil
)) ; 1. Start of macro.
806 ;; Do a round through the automaton if we've just passed a
807 ;; statement boundary or passed a "while"-like token.
809 (and (looking-at cond-key
)
810 (setq sym
(intern (match-string 1)))))
812 (when (and (< pos start
) (null stack
))
813 (throw 'loop nil
)) ; 2. Statement boundary.
815 ;; The PDA state handling.
817 ;; Refer to the description of the PDA in the opening
818 ;; comments. In the following OR form, the first leaf
819 ;; attempts to handles one of the specific actions detailed
820 ;; (e.g., finding token "if" whilst in state `else-boundary').
821 ;; We drop through to the second leaf (which handles common
822 ;; state) if no specific handler is found in the first cond.
823 ;; If a parsing error is detected (e.g. an "else" with no
824 ;; preceding "if"), we throw to the enclosing catch.
826 ;; Note that the (eq state 'else) means
827 ;; "we've just passed an else", NOT "we're looking for an
831 (if (eq sym
'boundary
)
832 (setq state
'else-boundary
)
834 (c-bos-pop-state-and-retry)))
836 ((eq state
'else-boundary
)
838 (c-bos-pop-state (setq ret
'beginning
)))
844 (if (and (eq sym
'boundary
)
845 ;; Since this can cause backtracking we do a
846 ;; little more careful analysis to avoid it:
847 ;; If there's a label in front of the while
848 ;; it can't be part of a do-while.
849 (not after-labels-pos
))
850 (progn (c-bos-save-pos)
851 (setq state
'while-boundary
))
852 (c-bos-pop-state-and-retry))) ; Can't be a do-while
854 ((eq state
'while-boundary
)
856 (c-bos-pop-state (setq ret
'beginning
)))
857 ((eq sym
'boundary
) ; isn't a do-while
858 (c-bos-restore-pos) ; the position of the while
859 (c-bos-pop-state)))) ; no longer searching for do.
862 (if (eq sym
'boundary
)
863 (setq state
'catch-boundary
)
865 (c-bos-pop-state-and-retry)))
867 ((eq state
'catch-boundary
)
870 (c-bos-pop-state (setq ret
'beginning
)))
875 (c-bos-pop-state)))))
877 ;; This is state common. We get here when the previous
878 ;; cond statement found no particular state handler.
879 (cond ((eq sym
'boundary
)
880 ;; If we have a boundary at the start
881 ;; position we push a frame to go to the
882 ;; previous statement.
888 (c-bos-save-error-info 'if
'else
)
891 ;; Is this a real while, or a do-while?
892 ;; The next `when' triggers unless we are SURE that
893 ;; the `while' is not the tail end of a `do-while'.
894 (when (or (not pptok
)
895 (memq (char-after pptok
) delims
)
896 ;; The following kludge is to prevent
897 ;; infinite recursion when called from
898 ;; c-awk-after-if-for-while-condition-p,
900 (and (eq (point) start
)
901 (c-vsemi-status-unknown-p))
902 (c-at-vsemi-p pptok
))
903 ;; Since this can cause backtracking we do a
904 ;; little more careful analysis to avoid it: If
905 ;; the while isn't followed by a (possibly
906 ;; virtual) semicolon it can't be a do-while.
908 (setq state
'while
)))
909 ((memq sym
'(catch finally
))
911 (c-bos-save-error-info 'try sym
)
912 (setq state
'catch
))))
915 ;; We're either past a statement boundary or at the
916 ;; start of a statement, so throw away any label data
917 ;; for the previous one.
918 (setq after-labels-pos nil
920 c-maybe-labelp nil
))))
922 ;; Step to the previous sexp, but not if we crossed a
923 ;; boundary, since that doesn't consume an sexp.
924 (if (eq sym
'boundary
)
927 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
928 ;; BACKWARDS THROUGH THE SOURCE.
930 (c-backward-syntactic-ws)
931 (let ((before-sws-pos (point))
932 ;; The end position of the area to search for statement
933 ;; barriers in this round.
934 (maybe-after-boundary-pos pos
))
936 ;; Go back over exactly one logical sexp, taking proper
937 ;; account of macros and escaped EOLs.
940 (unless (c-safe (c-backward-sexp) t
)
941 ;; Give up if we hit an unbalanced block. Since the
942 ;; stack won't be empty the code below will report a
946 ;; Have we moved into a macro?
947 ((and (not macro-start
)
948 (c-beginning-of-macro))
949 ;; Have we crossed a statement boundary? If not,
950 ;; keep going back until we find one or a "real" sexp.
954 (not (c-crosses-statement-barrier-p
955 (point) maybe-after-boundary-pos
)))
956 (setq maybe-after-boundary-pos
(point))))
957 ;; Have we just gone back over an escaped NL? This
958 ;; doesn't count as a sexp.
959 ((looking-at "\\\\$")))))
961 ;; Have we crossed a statement boundary?
964 ;; Are we at a macro beginning?
965 ((and (not macro-start
)
967 (looking-at c-opt-cpp-prefix
))
970 (c-crosses-statement-barrier-p
971 (point) maybe-after-boundary-pos
)))
972 ;; Just gone back over a brace block?
975 (not (c-looking-at-inexpr-block lim nil t
)))
977 (c-forward-sexp) (point)))
978 ;; Just gone back over some paren block?
979 ((looking-at "\\s\(")
981 (goto-char (1+ (c-down-list-backward
983 (c-crosses-statement-barrier-p
984 (point) maybe-after-boundary-pos
)))
985 ;; Just gone back over an ordinary symbol of some sort?
986 (t (c-crosses-statement-barrier-p
987 (point) maybe-after-boundary-pos
))))
994 ;; Like a C "continue". Analyze the next sexp.
998 (when (and c-opt-method-key
999 (setq saved
(c-in-method-def-p)))
1001 ignore-labels t
) ; Avoid the label check on exit.
1002 (throw 'loop nil
)) ; 3. ObjC method def.
1004 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1007 ;; The : <size> and <id> fields?
1008 ((and (numberp c-maybe-labelp
)
1009 (not bitfield-size-pos
)
1011 (goto-char (or tok start
))
1012 (not (looking-at c-keywords-regexp
)))
1013 (not (looking-at c-keywords-regexp
))
1014 (not (c-punctuation-in (point) c-maybe-labelp
)))
1015 (setq bitfield-size-pos
(or tok start
)
1016 bitfield-id-pos
(point)))
1017 ;; The <type> field?
1018 ((and bitfield-id-pos
1019 (not bitfield-type-pos
))
1020 (if (and (looking-at c-symbol-key
) ; Can only be an integer type. :-)
1021 (not (looking-at c-not-primitive-type-keywords-regexp
))
1022 (not (c-punctuation-in (point) tok
)))
1023 (setq bitfield-type-pos
(point))
1024 (setq bitfield-size-pos nil
1025 bitfield-id-pos nil
)))))
1028 (unless (eq ignore-labels t
)
1029 (when (numberp c-maybe-labelp
)
1030 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1031 ;; might be in a label now. Have we got a real label
1032 ;; (including a case label) or something like C++'s "public:"?
1033 ;; A case label might use an expression rather than a token.
1034 (setq after-case
:-pos
(or tok start
))
1035 (if (or (looking-at c-nonlabel-token-key
) ; e.g. "while" or "'a'"
1036 ;; Catch C++'s inheritance construct "class foo : bar".
1039 (c-safe (c-backward-sexp) t
)
1040 (looking-at c-nonlabel-token-2-key
))))
1041 (setq c-maybe-labelp nil
)
1042 (if after-labels-pos
; Have we already encountered a label?
1043 (if (not last-label-pos
)
1044 (setq last-label-pos
(or tok start
)))
1045 (setq after-labels-pos
(or tok start
)))
1046 (setq c-maybe-labelp t
1047 label-good-pos nil
))) ; bogus "label"
1049 (when (and (not label-good-pos
) ; i.e. no invalid "label"'s yet
1051 (looking-at c-nonlabel-token-key
)) ; e.g. "while :"
1052 ;; We're in a potential label and it's the first
1053 ;; time we've found something that isn't allowed in
1055 (setq label-good-pos
(or tok start
))))
1057 ;; We've moved back by a sexp, so update the token positions.
1062 pos tok
) ; always non-nil
1063 ) ; end of (catch loop ....)
1064 ) ; end of sexp-at-a-time (while ....)
1066 ;; If the stack isn't empty there might be errors to report.
1068 (if (and (vectorp saved-pos
) (eq (length saved-pos
) 3))
1069 (c-bos-report-error))
1070 (setq saved-pos
(cdr (car stack
))
1073 (when (and (eq ret
'same
)
1074 (not (memq sym
'(boundary ignore nil
))))
1075 ;; Need to investigate closer whether we've crossed
1076 ;; between a substatement and its containing statement.
1077 (if (setq saved
(if (looking-at c-block-stmt-1-key
)
1080 (cond ((> start saved
) (setq pos saved
))
1081 ((= start saved
) (setq ret
'up
)))))
1083 (when (and (not ignore-labels
)
1084 (eq c-maybe-labelp t
)
1085 (not (eq ret
'beginning
))
1087 (not bitfield-type-pos
) ; Bitfields take precedence over labels.
1088 (or (not label-good-pos
)
1089 (<= label-good-pos pos
)
1091 (goto-char (if (and last-label-pos
1092 (< last-label-pos start
))
1095 (looking-at c-label-kwds-regexp
))))
1096 ;; We're in a label. Maybe we should step to the statement
1098 (if (< after-labels-pos start
)
1099 (setq pos after-labels-pos
)
1101 (if (and last-label-pos
(< last-label-pos start
))
1102 ;; Might have jumped over several labels. Go to the last one.
1103 (setq pos last-label-pos
)))))
1105 ;; Have we got "case <expression>:"?
1107 (when (and after-case
:-pos
1108 (not (eq ret
'beginning
))
1109 (looking-at c-case-kwds-regexp
))
1110 (if (< after-case
:-pos start
)
1111 (setq pos after-case
:-pos
))
1115 ;; Skip over the unary operators that can start the statement.
1117 (c-backward-syntactic-ws)
1118 ;; protect AWK post-inc/decrement operators, etc.
1119 (and (not (c-at-vsemi-p (point)))
1120 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1125 (defun c-punctuation-in (from to
)
1126 "Return non-nil if there is a non-comment non-macro punctuation character
1127 between FROM and TO. FROM must not be in a string or comment. The returned
1128 value is the position of the first such character."
1131 (let ((pos (point)))
1132 (while (progn (skip-chars-forward c-symbol-chars to
)
1133 (c-forward-syntactic-ws to
)
1135 (setq pos
(point))))
1136 (and (< (point) to
) (point))))
1138 (defun c-crosses-statement-barrier-p (from to
)
1139 "Return non-nil if buffer positions FROM to TO cross one or more
1140 statement or declaration boundaries. The returned value is actually
1141 the position of the earliest boundary char. FROM must not be within
1142 a string or comment.
1144 The variable `c-maybe-labelp' is set to the position of the first `:' that
1145 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1146 single `?' is found, then `c-maybe-labelp' is cleared.
1148 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1149 regarded as having a \"virtual semicolon\" immediately after the last token on
1150 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1152 Note that this function might do hidden buffer changes. See the
1153 comment at the start of cc-engine.el for more info."
1155 ;; If the current language has CPP macros, insert # into skip-chars.
1156 (if c-opt-cpp-symbol
1157 (concat (substring c-stmt-delim-chars
0 1) ; "^"
1158 c-opt-cpp-symbol
; usually "#"
1159 (substring c-stmt-delim-chars
1)) ; e.g. ";{}?:"
1160 c-stmt-delim-chars
))
1162 (append (substring skip-chars
1) nil
)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1163 lit-range vsemi-pos
)
1169 (while (progn (skip-chars-forward
1171 (min to
(c-point 'bonl
)))
1174 ;; Virtual semicolon?
1178 (if (setq lit-range
(c-literal-limits from
)) ; Have we landed in a string/comment?
1179 (goto-char (car lit-range
)))
1180 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1181 (setq vsemi-pos
(point))
1183 (throw 'done vsemi-pos
))
1184 ;; In a string/comment?
1185 ((setq lit-range
(c-literal-limits))
1186 (goto-char (cdr lit-range
)))
1187 ((eq (char-after) ?
:)
1189 (if (and (eq (char-after) ?
:)
1191 ;; Ignore scope operators.
1193 (setq c-maybe-labelp
(1- (point)))))
1194 ((eq (char-after) ??
)
1195 ;; A question mark. Can't be a label, so stop
1196 ;; looking for more : and ?.
1197 (setq c-maybe-labelp nil
1198 skip-chars
(substring c-stmt-delim-chars
0 -
2)))
1199 ;; At a CPP construct?
1200 ((and c-opt-cpp-symbol
(looking-at c-opt-cpp-symbol
)
1203 (looking-at c-opt-cpp-prefix
)))
1205 ((memq (char-after) non-skip-list
)
1206 (throw 'done
(point)))))
1207 ;; In trailing space after an as yet undetected virtual semicolon?
1208 (c-backward-syntactic-ws from
)
1209 (if (and (< (point) to
)
1214 (defun c-at-statement-start-p ()
1215 "Return non-nil if the point is at the first token in a statement
1216 or somewhere in the syntactic whitespace before it.
1218 A \"statement\" here is not restricted to those inside code blocks.
1219 Any kind of declaration-like construct that occur outside function
1220 bodies is also considered a \"statement\".
1222 Note that this function might do hidden buffer changes. See the
1223 comment at the start of cc-engine.el for more info."
1228 (c-syntactic-skip-backward (substring c-stmt-delim-chars
1) nil t
)
1230 (eq (char-before) ?
})
1231 (and (eq (char-before) ?
{)
1232 (not (and c-special-brace-lists
1233 (progn (backward-char)
1234 (c-looking-at-special-brace-list)))))
1235 (c-crosses-statement-barrier-p (point) end
)))))
1237 (defun c-at-expression-start-p ()
1238 "Return non-nil if the point is at the first token in an expression or
1239 statement, or somewhere in the syntactic whitespace before it.
1241 An \"expression\" here is a bit different from the normal language
1242 grammar sense: It's any sequence of expression tokens except commas,
1243 unless they are enclosed inside parentheses of some kind. Also, an
1244 expression never continues past an enclosing parenthesis, but it might
1245 contain parenthesis pairs of any sort except braces.
1247 Since expressions never cross statement boundaries, this function also
1248 recognizes statement beginnings, just like `c-at-statement-start-p'.
1250 Note that this function might do hidden buffer changes. See the
1251 comment at the start of cc-engine.el for more info."
1255 (c-stmt-delim-chars c-stmt-delim-chars-with-comma
)
1257 (c-syntactic-skip-backward (substring c-stmt-delim-chars
1) nil t
)
1259 (memq (char-before) '(?
{ ?
}))
1260 (save-excursion (backward-char)
1261 (looking-at "\\s("))
1262 (c-crosses-statement-barrier-p (point) end
)))))
1265 ;; A set of functions that covers various idiosyncrasies in
1266 ;; implementations of `forward-comment'.
1268 ;; Note: Some emacsen considers incorrectly that any line comment
1269 ;; ending with a backslash continues to the next line. I can't think
1270 ;; of any way to work around that in a reliable way without changing
1271 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1272 ;; changing the syntax for backslash doesn't work since we must treat
1273 ;; escapes in string literals correctly.)
1275 (defun c-forward-single-comment ()
1276 "Move forward past whitespace and the closest following comment, if any.
1277 Return t if a comment was found, nil otherwise. In either case, the
1278 point is moved past the following whitespace. Line continuations,
1279 i.e. a backslashes followed by line breaks, are treated as whitespace.
1280 The line breaks that end line comments are considered to be the
1281 comment enders, so the point will be put on the beginning of the next
1282 line if it moved past a line comment.
1284 This function does not do any hidden buffer changes."
1286 (let ((start (point)))
1287 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1288 (goto-char (match-end 0)))
1290 (when (forward-comment 1)
1292 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1296 ;; Emacs includes the ending newline in a b-style (c++)
1297 ;; comment, but XEmacs doesn't. We depend on the Emacs
1298 ;; behavior (which also is symmetric).
1299 (if (and (eolp) (elt (parse-partial-sexp start
(point)) 7))
1300 (condition-case nil
(forward-char 1)))
1304 (defsubst c-forward-comments
()
1305 "Move forward past all following whitespace and comments.
1306 Line continuations, i.e. a backslashes followed by line breaks, are
1307 treated as whitespace.
1309 Note that this function might do hidden buffer changes. See the
1310 comment at the start of cc-engine.el for more info."
1313 ;; If forward-comment in at least XEmacs 21 is given a large
1314 ;; positive value, it'll loop all the way through if it hits
1316 (and (forward-comment 5)
1317 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1321 (when (looking-at "\\\\[\n\r]")
1325 (defun c-backward-single-comment ()
1326 "Move backward past whitespace and the closest preceding comment, if any.
1327 Return t if a comment was found, nil otherwise. In either case, the
1328 point is moved past the preceding whitespace. Line continuations,
1329 i.e. a backslashes followed by line breaks, are treated as whitespace.
1330 The line breaks that end line comments are considered to be the
1331 comment enders, so the point cannot be at the end of the same line to
1332 move over a line comment.
1334 This function does not do any hidden buffer changes."
1336 (let ((start (point)))
1337 ;; When we got newline terminated comments, forward-comment in all
1338 ;; supported emacsen so far will stop at eol of each line not
1339 ;; ending with a comment when moving backwards. This corrects for
1340 ;; that, and at the same time handles line continuations.
1342 (skip-chars-backward " \t\n\r\f\v")
1343 (and (looking-at "[\n\r]")
1344 (eq (char-before) ?
\\)))
1348 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1349 ;; backwards at bob.
1352 ;; Leave point after the closest following newline if we've
1353 ;; backed up over any above, since forward-comment won't move
1354 ;; backward over a line comment if point is at the end of the
1356 (re-search-forward "\\=\\s *[\n\r]" start t
)
1358 (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -
1))
1360 ;; If forward-comment above succeeded and we're at eol
1361 ;; then the newline we moved over above didn't end a
1362 ;; line comment, so we give it another go.
1363 (let (open-paren-in-column-0-is-defun-start)
1364 (forward-comment -
1))
1367 ;; Emacs <= 20 and XEmacs move back over the closer of a
1368 ;; block comment that lacks an opener.
1369 (if (looking-at "\\*/")
1370 (progn (forward-char 2) nil
)
1373 (defsubst c-backward-comments
()
1374 "Move backward past all preceding whitespace and comments.
1375 Line continuations, i.e. a backslashes followed by line breaks, are
1376 treated as whitespace. The line breaks that end line comments are
1377 considered to be the comment enders, so the point cannot be at the end
1378 of the same line to move over a line comment. Unlike
1379 c-backward-syntactic-ws, this function doesn't move back over
1380 preprocessor directives.
1382 Note that this function might do hidden buffer changes. See the
1383 comment at the start of cc-engine.el for more info."
1385 (let ((start (point)))
1387 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1388 ;; return t when moving backwards at bob.
1391 (if (let (open-paren-in-column-0-is-defun-start)
1392 (forward-comment -
1))
1393 (if (looking-at "\\*/")
1394 ;; Emacs <= 20 and XEmacs move back over the
1395 ;; closer of a block comment that lacks an opener.
1396 (progn (forward-char 2) nil
)
1399 ;; XEmacs treats line continuations as whitespace but
1400 ;; only in the backward direction, which seems a bit
1401 ;; odd. Anyway, this is necessary for Emacs.
1402 (when (and (looking-at "[\n\r]")
1403 (eq (char-before) ?
\\)
1409 ;; Tools for skipping over syntactic whitespace.
1411 ;; The following functions use text properties to cache searches over
1412 ;; large regions of syntactic whitespace. It works as follows:
1414 ;; o If a syntactic whitespace region contains anything but simple
1415 ;; whitespace (i.e. space, tab and line breaks), the text property
1416 ;; `c-in-sws' is put over it. At places where we have stopped
1417 ;; within that region there's also a `c-is-sws' text property.
1418 ;; That since there typically are nested whitespace inside that
1419 ;; must be handled separately, e.g. whitespace inside a comment or
1420 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1421 ;; to jump to another point with that property within the same
1422 ;; `c-in-sws' region. It can be likened to a ladder where
1423 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1425 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1426 ;; a "rung position" and also maybe on the first following char.
1427 ;; As many characters as can be conveniently found in this range
1428 ;; are marked, but no assumption can be made that the whole range
1429 ;; is marked (it could be clobbered by later changes, for
1432 ;; Note that some part of the beginning of a sequence of simple
1433 ;; whitespace might be part of the end of a preceding line comment
1434 ;; or cpp directive and must not be considered part of the "rung".
1435 ;; Such whitespace is some amount of horizontal whitespace followed
1436 ;; by a newline. In the case of cpp directives it could also be
1437 ;; two newlines with horizontal whitespace between them.
1439 ;; The reason to include the first following char is to cope with
1440 ;; "rung positions" that doesn't have any ordinary whitespace. If
1441 ;; `c-is-sws' is put on a token character it does not have
1442 ;; `c-in-sws' set simultaneously. That's the only case when that
1443 ;; can occur, and the reason for not extending the `c-in-sws'
1444 ;; region to cover it is that the `c-in-sws' region could then be
1445 ;; accidentally merged with a following one if the token is only
1446 ;; one character long.
1448 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1449 ;; removed in the changed region. If the change was inside
1450 ;; syntactic whitespace that means that the "ladder" is broken, but
1451 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1452 ;; parts on either side and use an ordinary search only to "repair"
1455 ;; Special care needs to be taken if a region is removed: If there
1456 ;; are `c-in-sws' on both sides of it which do not connect inside
1457 ;; the region then they can't be joined. If e.g. a marked macro is
1458 ;; broken, syntactic whitespace inside the new text might be
1459 ;; marked. If those marks would become connected with the old
1460 ;; `c-in-sws' range around the macro then we could get a ladder
1461 ;; with one end outside the macro and the other at some whitespace
1464 ;; The main motivation for this system is to increase the speed in
1465 ;; skipping over the large whitespace regions that can occur at the
1466 ;; top level in e.g. header files that contain a lot of comments and
1467 ;; cpp directives. For small comments inside code it's probably
1468 ;; slower than using `forward-comment' straightforwardly, but speed is
1469 ;; not a significant factor there anyway.
1471 ; (defface c-debug-is-sws-face
1472 ; '((t (:background "GreenYellow")))
1473 ; "Debug face to mark the `c-is-sws' property.")
1474 ; (defface c-debug-in-sws-face
1475 ; '((t (:underline t)))
1476 ; "Debug face to mark the `c-in-sws' property.")
1478 ; (defun c-debug-put-sws-faces ()
1479 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1480 ; ;; properties in the buffer.
1483 ; (c-save-buffer-state (in-face)
1484 ; (goto-char (point-min))
1485 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1488 ; (goto-char (next-single-property-change
1489 ; (point) 'c-is-sws nil (point-max)))
1492 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1493 ; (setq in-face nil))
1494 ; (setq in-face (point)))
1496 ; (goto-char (point-min))
1497 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1500 ; (goto-char (next-single-property-change
1501 ; (point) 'c-in-sws nil (point-max)))
1504 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1505 ; (setq in-face nil))
1506 ; (setq in-face (point)))
1509 (defmacro c-debug-sws-msg
(&rest args
)
1513 (defmacro c-put-is-sws
(beg end
)
1514 ;; This macro does a hidden buffer change.
1515 `(let ((beg ,beg
) (end ,end
))
1516 (put-text-property beg end
'c-is-sws t
)
1517 ,@(when (facep 'c-debug-is-sws-face
)
1518 `((c-debug-add-face beg end
'c-debug-is-sws-face
)))))
1520 (defmacro c-put-in-sws
(beg end
)
1521 ;; This macro does a hidden buffer change.
1522 `(let ((beg ,beg
) (end ,end
))
1523 (put-text-property beg end
'c-in-sws t
)
1524 ,@(when (facep 'c-debug-is-sws-face
)
1525 `((c-debug-add-face beg end
'c-debug-in-sws-face
)))))
1527 (defmacro c-remove-is-sws
(beg end
)
1528 ;; This macro does a hidden buffer change.
1529 `(let ((beg ,beg
) (end ,end
))
1530 (remove-text-properties beg end
'(c-is-sws nil
))
1531 ,@(when (facep 'c-debug-is-sws-face
)
1532 `((c-debug-remove-face beg end
'c-debug-is-sws-face
)))))
1534 (defmacro c-remove-in-sws
(beg end
)
1535 ;; This macro does a hidden buffer change.
1536 `(let ((beg ,beg
) (end ,end
))
1537 (remove-text-properties beg end
'(c-in-sws nil
))
1538 ,@(when (facep 'c-debug-is-sws-face
)
1539 `((c-debug-remove-face beg end
'c-debug-in-sws-face
)))))
1541 (defmacro c-remove-is-and-in-sws
(beg end
)
1542 ;; This macro does a hidden buffer change.
1543 `(let ((beg ,beg
) (end ,end
))
1544 (remove-text-properties beg end
'(c-is-sws nil c-in-sws nil
))
1545 ,@(when (facep 'c-debug-is-sws-face
)
1546 `((c-debug-remove-face beg end
'c-debug-is-sws-face
)
1547 (c-debug-remove-face beg end
'c-debug-in-sws-face
)))))
1549 (defsubst c-invalidate-sws-region-after
(beg end
)
1550 ;; Called from `after-change-functions'. Note that if
1551 ;; `c-forward-sws' or `c-backward-sws' are used outside
1552 ;; `c-save-buffer-state' or similar then this will remove the cache
1553 ;; properties right after they're added.
1555 ;; This function does hidden buffer changes.
1558 ;; Adjust the end to remove the properties in any following simple
1559 ;; ws up to and including the next line break, if there is any
1560 ;; after the changed region. This is necessary e.g. when a rung
1561 ;; marked empty line is converted to a line comment by inserting
1562 ;; "//" before the line break. In that case the line break would
1563 ;; keep the rung mark which could make a later `c-backward-sws'
1564 ;; move into the line comment instead of over it.
1566 (skip-chars-forward " \t\f\v")
1567 (when (and (eolp) (not (eobp)))
1568 (setq end
(1+ (point)))))
1570 (when (and (= beg end
)
1571 (get-text-property beg
'c-in-sws
)
1573 (get-text-property (1- beg
) 'c-in-sws
))
1574 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1575 ;; safe to keep a range that was continuous before the change. E.g:
1581 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1582 ;; after "foo" is removed then "bar" will become part of the cpp
1583 ;; directive instead of a syntactically relevant token. In that
1584 ;; case there's no longer syntactic ws from "#" to "b".
1585 (setq beg
(1- beg
)))
1587 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end
)
1588 (c-remove-is-and-in-sws beg end
))
1590 (defun c-forward-sws ()
1591 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1593 ;; This function might do hidden buffer changes.
1595 (let (;; `rung-pos' is set to a position as early as possible in the
1596 ;; unmarked part of the simple ws region.
1597 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1598 rung-is-marked next-rung-is-marked simple-ws-end
1599 ;; `safe-start' is set when it's safe to cache the start position.
1600 ;; It's not set if we've initially skipped over comments and line
1601 ;; continuations since we might have gone out through the end of a
1602 ;; macro then. This provision makes `c-forward-sws' not populate the
1603 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1607 ;; Skip simple ws and do a quick check on the following character to see
1608 ;; if it's anything that can't start syntactic ws, so we can bail out
1609 ;; early in the majority of cases when there just are a few ws chars.
1610 (skip-chars-forward " \t\n\r\f\v")
1611 (when (looking-at c-syntactic-ws-start
)
1613 (setq rung-end-pos
(min (1+ (point)) (point-max)))
1614 (if (setq rung-is-marked
(text-property-any rung-pos rung-end-pos
1616 ;; Find the last rung position to avoid setting properties in all
1617 ;; the cases when the marked rung is complete.
1618 ;; (`next-single-property-change' is certain to move at least one
1620 (setq rung-pos
(1- (next-single-property-change
1621 rung-is-marked
'c-is-sws nil rung-end-pos
)))
1622 ;; Got no marked rung here. Since the simple ws might have started
1623 ;; inside a line comment or cpp directive we must set `rung-pos' as
1624 ;; high as possible.
1625 (setq rung-pos
(point)))
1630 (when (and rung-is-marked
1631 (get-text-property (point) 'c-in-sws
))
1633 ;; The following search is the main reason that `c-in-sws'
1634 ;; and `c-is-sws' aren't combined to one property.
1635 (goto-char (next-single-property-change
1636 (point) 'c-in-sws nil
(point-max)))
1637 (unless (get-text-property (point) 'c-is-sws
)
1638 ;; If the `c-in-sws' region extended past the last
1639 ;; `c-is-sws' char we have to go back a bit.
1640 (or (get-text-property (1- (point)) 'c-is-sws
)
1641 (goto-char (previous-single-property-change
1642 (point) 'c-is-sws
)))
1646 "c-forward-sws cached move %s -> %s (max %s)"
1647 rung-pos
(point) (point-max))
1649 (setq rung-pos
(point))
1650 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1653 ;; We'll loop here if there is simple ws after the last rung.
1654 ;; That means that there's been some change in it and it's
1655 ;; possible that we've stepped into another ladder, so extend
1656 ;; the previous one to join with it if there is one, and try to
1657 ;; use the cache again.
1659 "c-forward-sws extending rung with [%s..%s] (max %s)"
1660 (1+ rung-pos
) (1+ (point)) (point-max))
1661 (unless (get-text-property (point) 'c-is-sws
)
1662 ;; Remove any `c-in-sws' property from the last char of
1663 ;; the rung before we mark it with `c-is-sws', so that we
1664 ;; won't connect with the remains of a broken "ladder".
1665 (c-remove-in-sws (point) (1+ (point))))
1666 (c-put-is-sws (1+ rung-pos
)
1668 (c-put-in-sws rung-pos
1669 (setq rung-pos
(point)
1670 last-put-in-sws-pos rung-pos
)))
1672 (setq simple-ws-end
(point))
1673 (c-forward-comments)
1676 ((/= (point) simple-ws-end
)
1677 ;; Skipped over comments. Don't cache at eob in case the buffer
1682 (and c-opt-cpp-prefix
1683 (looking-at c-opt-cpp-start
)
1684 (progn (skip-chars-backward " \t")
1687 (progn (backward-char)
1688 (not (eq (char-before) ?
\\))))))
1689 ;; Skip a preprocessor directive.
1691 (while (and (eq (char-before) ?
\\)
1692 (= (forward-line 1) 0))
1696 ;; Don't cache at eob in case the buffer is narrowed.
1699 ;; We've searched over a piece of non-white syntactic ws. See if this
1701 (setq next-rung-pos
(point))
1702 (skip-chars-forward " \t\n\r\f\v")
1703 (setq rung-end-pos
(min (1+ (point)) (point-max)))
1706 ;; Cache if we haven't skipped comments only, and if we started
1707 ;; either from a marked rung or from a completely uncached
1711 (not (get-text-property simple-ws-end
'c-in-sws
))))
1713 ;; See if there's a marked rung in the encountered simple ws. If
1714 ;; so then we can cache, unless `safe-start' is nil. Even then
1715 ;; we need to do this to check if the cache can be used for the
1717 (and (setq next-rung-is-marked
1718 (text-property-any next-rung-pos rung-end-pos
1724 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1725 rung-pos
(1+ simple-ws-end
) next-rung-pos rung-end-pos
1728 ;; Remove the properties for any nested ws that might be cached.
1729 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1731 (c-remove-is-sws (1+ simple-ws-end
) next-rung-pos
)
1732 (unless (and rung-is-marked
(= rung-pos simple-ws-end
))
1733 (c-put-is-sws rung-pos
1735 (setq rung-is-marked t
))
1736 (c-put-in-sws rung-pos
1737 (setq rung-pos
(point)
1738 last-put-in-sws-pos rung-pos
))
1739 (unless (get-text-property (1- rung-end-pos
) 'c-is-sws
)
1740 ;; Remove any `c-in-sws' property from the last char of
1741 ;; the rung before we mark it with `c-is-sws', so that we
1742 ;; won't connect with the remains of a broken "ladder".
1743 (c-remove-in-sws (1- rung-end-pos
) rung-end-pos
))
1744 (c-put-is-sws next-rung-pos
1748 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1749 rung-pos
(1+ simple-ws-end
) next-rung-pos rung-end-pos
1752 ;; Set `rung-pos' for the next rung. It's the same thing here as
1753 ;; initially, except that the rung position is set as early as
1754 ;; possible since we can't be in the ending ws of a line comment or
1755 ;; cpp directive now.
1756 (if (setq rung-is-marked next-rung-is-marked
)
1757 (setq rung-pos
(1- (next-single-property-change
1758 rung-is-marked
'c-is-sws nil rung-end-pos
)))
1759 (setq rung-pos next-rung-pos
))
1760 (setq safe-start t
)))
1762 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1763 ;; another one after the point (which might occur when editing inside a
1764 ;; comment or macro).
1765 (when (eq last-put-in-sws-pos
(point))
1766 (cond ((< last-put-in-sws-pos
(point-max))
1768 "c-forward-sws clearing at %s for cache separation"
1769 last-put-in-sws-pos
)
1770 (c-remove-in-sws last-put-in-sws-pos
1771 (1+ last-put-in-sws-pos
)))
1773 ;; If at eob we have to clear the last character before the end
1774 ;; instead since the buffer might be narrowed and there might
1775 ;; be a `c-in-sws' after (point-max). In this case it's
1776 ;; necessary to clear both properties.
1778 "c-forward-sws clearing thoroughly at %s for cache separation"
1779 (1- last-put-in-sws-pos
))
1780 (c-remove-is-and-in-sws (1- last-put-in-sws-pos
)
1781 last-put-in-sws-pos
))))
1784 (defun c-backward-sws ()
1785 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1787 ;; This function might do hidden buffer changes.
1789 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1790 ;; part of the simple ws region.
1791 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1792 rung-is-marked simple-ws-beg cmt-skip-pos
)
1794 ;; Skip simple horizontal ws and do a quick check on the preceding
1795 ;; character to see if it's anything that can't end syntactic ws, so we can
1796 ;; bail out early in the majority of cases when there just are a few ws
1797 ;; chars. Newlines are complicated in the backward direction, so we can't
1799 (skip-chars-backward " \t\f")
1800 (when (and (not (bobp))
1803 (looking-at c-syntactic-ws-end
)))
1805 ;; Try to find a rung position in the simple ws preceding point, so that
1806 ;; we can get a cache hit even if the last bit of the simple ws has
1807 ;; changed recently.
1808 (setq simple-ws-beg
(point))
1809 (skip-chars-backward " \t\n\r\f\v")
1810 (if (setq rung-is-marked
(text-property-any
1811 (point) (min (1+ rung-pos
) (point-max))
1813 ;; `rung-pos' will be the earliest marked position, which means that
1814 ;; there might be later unmarked parts in the simple ws region.
1815 ;; It's not worth the effort to fix that; the last part of the
1816 ;; simple ws is also typically edited often, so it could be wasted.
1817 (goto-char (setq rung-pos rung-is-marked
))
1818 (goto-char simple-ws-beg
))
1823 (when (and rung-is-marked
1825 (get-text-property (1- (point)) 'c-in-sws
))
1827 ;; The following search is the main reason that `c-in-sws'
1828 ;; and `c-is-sws' aren't combined to one property.
1829 (goto-char (previous-single-property-change
1830 (point) 'c-in-sws nil
(point-min)))
1831 (unless (get-text-property (point) 'c-is-sws
)
1832 ;; If the `c-in-sws' region extended past the first
1833 ;; `c-is-sws' char we have to go forward a bit.
1834 (goto-char (next-single-property-change
1835 (point) 'c-is-sws
)))
1838 "c-backward-sws cached move %s <- %s (min %s)"
1839 (point) rung-pos
(point-min))
1841 (setq rung-pos
(point))
1842 (if (and (< (min (skip-chars-backward " \t\f\v")
1844 (setq simple-ws-beg
(point))
1845 (skip-chars-backward " \t\n\r\f\v")))
1847 (setq rung-is-marked
1848 (text-property-any (point) rung-pos
1851 (goto-char simple-ws-beg
)
1854 ;; We'll loop here if there is simple ws before the first rung.
1855 ;; That means that there's been some change in it and it's
1856 ;; possible that we've stepped into another ladder, so extend
1857 ;; the previous one to join with it if there is one, and try to
1858 ;; use the cache again.
1860 "c-backward-sws extending rung with [%s..%s] (min %s)"
1861 rung-is-marked rung-pos
(point-min))
1862 (unless (get-text-property (1- rung-pos
) 'c-is-sws
)
1863 ;; Remove any `c-in-sws' property from the last char of
1864 ;; the rung before we mark it with `c-is-sws', so that we
1865 ;; won't connect with the remains of a broken "ladder".
1866 (c-remove-in-sws (1- rung-pos
) rung-pos
))
1867 (c-put-is-sws rung-is-marked
1869 (c-put-in-sws rung-is-marked
1871 (setq rung-pos rung-is-marked
1872 last-put-in-sws-pos rung-pos
))
1874 (c-backward-comments)
1875 (setq cmt-skip-pos
(point))
1878 ((and c-opt-cpp-prefix
1879 (/= cmt-skip-pos simple-ws-beg
)
1880 (c-beginning-of-macro))
1881 ;; Inside a cpp directive. See if it should be skipped over.
1882 (let ((cpp-beg (point)))
1884 ;; Move back over all line continuations in the region skipped
1885 ;; over by `c-backward-comments'. If we go past it then we
1886 ;; started inside the cpp directive.
1887 (goto-char simple-ws-beg
)
1889 (while (and (> (point) cmt-skip-pos
)
1890 (progn (backward-char)
1891 (eq (char-before) ?
\\)))
1892 (beginning-of-line))
1894 (if (< (point) cmt-skip-pos
)
1895 ;; Don't move past the cpp directive if we began inside
1896 ;; it. Note that the position at the end of the last line
1897 ;; of the macro is also considered to be within it.
1898 (progn (goto-char cmt-skip-pos
)
1901 ;; It's worthwhile to spend a little bit of effort on finding
1902 ;; the end of the macro, to get a good `simple-ws-beg'
1903 ;; position for the cache. Note that `c-backward-comments'
1904 ;; could have stepped over some comments before going into
1905 ;; the macro, and then `simple-ws-beg' must be kept on the
1906 ;; same side of those comments.
1907 (goto-char simple-ws-beg
)
1908 (skip-chars-backward " \t\n\r\f\v")
1909 (if (eq (char-before) ?
\\)
1912 (if (< (point) simple-ws-beg
)
1913 ;; Might happen if comments after the macro were skipped
1915 (setq simple-ws-beg
(point)))
1920 ((/= (save-excursion
1921 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg
)
1922 (setq next-rung-pos
(point)))
1924 ;; Skipped over comments. Must put point at the end of
1925 ;; the simple ws at point since we might be after a line
1926 ;; comment or cpp directive that's been partially
1927 ;; narrowed out, and we can't risk marking the simple ws
1928 ;; at the end of it.
1929 (goto-char next-rung-pos
)
1932 ;; We've searched over a piece of non-white syntactic ws. See if this
1934 (setq next-rung-pos
(point))
1935 (skip-chars-backward " \t\f\v")
1938 ;; Cache if we started either from a marked rung or from a
1939 ;; completely uncached position.
1941 (not (get-text-property (1- simple-ws-beg
) 'c-in-sws
))
1943 ;; Cache if there's a marked rung in the encountered simple ws.
1945 (skip-chars-backward " \t\n\r\f\v")
1946 (text-property-any (point) (min (1+ next-rung-pos
) (point-max))
1951 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1952 (point) (1+ next-rung-pos
)
1953 simple-ws-beg
(min (1+ rung-pos
) (point-max))
1956 ;; Remove the properties for any nested ws that might be cached.
1957 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1959 (c-remove-is-sws (1+ next-rung-pos
) simple-ws-beg
)
1960 (unless (and rung-is-marked
(= simple-ws-beg rung-pos
))
1961 (let ((rung-end-pos (min (1+ rung-pos
) (point-max))))
1962 (unless (get-text-property (1- rung-end-pos
) 'c-is-sws
)
1963 ;; Remove any `c-in-sws' property from the last char of
1964 ;; the rung before we mark it with `c-is-sws', so that we
1965 ;; won't connect with the remains of a broken "ladder".
1966 (c-remove-in-sws (1- rung-end-pos
) rung-end-pos
))
1967 (c-put-is-sws simple-ws-beg
1969 (setq rung-is-marked t
)))
1970 (c-put-in-sws (setq simple-ws-beg
(point)
1971 last-put-in-sws-pos simple-ws-beg
)
1973 (c-put-is-sws (setq rung-pos simple-ws-beg
)
1974 (1+ next-rung-pos
)))
1977 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1978 (point) (1+ next-rung-pos
)
1979 simple-ws-beg
(min (1+ rung-pos
) (point-max))
1981 (setq rung-pos next-rung-pos
1982 simple-ws-beg
(point))
1985 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1986 ;; another one before the point (which might occur when editing inside a
1987 ;; comment or macro).
1988 (when (eq last-put-in-sws-pos
(point))
1989 (cond ((< (point-min) last-put-in-sws-pos
)
1991 "c-backward-sws clearing at %s for cache separation"
1992 (1- last-put-in-sws-pos
))
1993 (c-remove-in-sws (1- last-put-in-sws-pos
)
1994 last-put-in-sws-pos
))
1996 ;; If at bob and the buffer is narrowed, we have to clear the
1997 ;; character we're standing on instead since there might be a
1998 ;; `c-in-sws' before (point-min). In this case it's necessary
1999 ;; to clear both properties.
2001 "c-backward-sws clearing thoroughly at %s for cache separation"
2002 last-put-in-sws-pos
)
2003 (c-remove-is-and-in-sws last-put-in-sws-pos
2004 (1+ last-put-in-sws-pos
)))))
2008 ;; Other whitespace tools
2009 (defun c-partial-ws-p (beg end
)
2010 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2011 ;; region? This is a "heuristic" function. .....
2013 ;; The motivation for the second bit is to check whether removing this
2014 ;; region would coalesce two symbols.
2016 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2017 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2019 (let ((end+1 (min (1+ end
) (point-max))))
2020 (or (progn (goto-char (max (point-min) (1- beg
)))
2021 (c-skip-ws-forward end
)
2023 (progn (goto-char beg
)
2024 (c-skip-ws-forward end
+1)
2025 (eq (point) end
+1))))))
2027 ;; A system for finding noteworthy parens before the point.
2029 (defconst c-state-cache-too-far
5000)
2030 ;; A maximum comfortable scanning distance, e.g. between
2031 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2032 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2033 ;; the cache and starting again from point-min or a beginning of defun. This
2034 ;; value can be tuned for efficiency or set to a lower value for testing.
2036 (defvar c-state-cache nil
)
2037 (make-variable-buffer-local 'c-state-cache
)
2038 ;; The state cache used by `c-parse-state' to cut down the amount of
2039 ;; searching. It's the result from some earlier `c-parse-state' call. See
2040 ;; `c-parse-state''s doc string for details of its structure.
2042 ;; The use of the cached info is more effective if the next
2043 ;; `c-parse-state' call is on a line close by the one the cached state
2044 ;; was made at; the cache can actually slow down a little if the
2045 ;; cached state was made very far back in the buffer. The cache is
2046 ;; most effective if `c-parse-state' is used on each line while moving
2049 (defvar c-state-cache-good-pos
1)
2050 (make-variable-buffer-local 'c-state-cache-good-pos
)
2051 ;; This is a position where `c-state-cache' is known to be correct, or
2052 ;; nil (see below). It's a position inside one of the recorded unclosed
2053 ;; parens or the top level, but not further nested inside any literal or
2054 ;; subparen that is closed before the last recorded position.
2056 ;; The exact position is chosen to try to be close to yet earlier than
2057 ;; the position where `c-state-cache' will be called next. Right now
2058 ;; the heuristic is to set it to the position after the last found
2059 ;; closing paren (of any type) before the line on which
2060 ;; `c-parse-state' was called. That is chosen primarily to work well
2061 ;; with refontification of the current line.
2063 ;; 2009-07-28: When `c-state-point-min' and the last position where
2064 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2065 ;; both in the same literal, there is no such "good position", and
2066 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2067 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2069 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2070 ;; the middle of the desert, as long as it is not within a brace pair
2071 ;; recorded in `c-state-cache' or a paren/bracket pair.
2074 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2075 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2076 ;; speed up testing for non-literality.
2077 (defconst c-state-nonlit-pos-interval
3000)
2078 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2080 (defvar c-state-nonlit-pos-cache nil
)
2081 (make-variable-buffer-local 'c-state-nonlit-pos-cache
)
2082 ;; A list of buffer positions which are known not to be in a literal or a cpp
2083 ;; construct. This is ordered with higher positions at the front of the list.
2084 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2086 (defvar c-state-nonlit-pos-cache-limit
1)
2087 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit
)
2088 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2089 ;; reduced by buffer changes, and increased by invocations of
2090 ;; `c-state-literal-at'.
2092 (defsubst c-state-pp-to-literal
(from to
)
2093 ;; Do a parse-partial-sexp from FROM to TO, returning either
2094 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2095 ;; (STATE) otherwise,
2096 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2097 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal.
2099 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2100 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2103 (let ((s (parse-partial-sexp from to
))
2105 (when (or (nth 3 s
) (nth 4 s
)) ; in a string or comment
2108 ((eq (nth 7 s
) t
) 'c
++)
2110 (parse-partial-sexp (point) (point-max)
2114 'syntax-table
)) ; stop at end of literal
2116 `(,s
,ty
(,(nth 8 s
) .
,(point)))
2119 (defun c-state-safe-place (here)
2120 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2121 ;; string, comment, or macro.
2123 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2124 ;; MAY NOT contain any positions within macros, since macros are frequently
2125 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2126 ;; We cannot rely on this mechanism whilst determining a cache pos since
2127 ;; this function is also called from outwith `c-parse-state'.
2131 (let ((c c-state-nonlit-pos-cache
)
2132 pos npos lit macro-beg
)
2133 ;; Trim the cache to take account of buffer changes.
2134 (while (and c
(> (car c
) c-state-nonlit-pos-cache-limit
))
2136 (setq c-state-nonlit-pos-cache c
)
2138 (while (and c
(> (car c
) here
))
2140 (setq pos
(or (car c
) (point-min)))
2143 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2145 (<= (setq npos
(+ pos c-state-nonlit-pos-interval
)) here
)
2147 (setq lit
(car (cddr (c-state-pp-to-literal pos npos
))))
2152 ((<= (cdr lit
) here
)
2153 (setq pos
(cdr lit
))
2156 (setq pos
(car lit
))
2160 (when (and (c-beginning-of-macro) (/= (point) pos
))
2161 (setq macro-beg
(point))
2162 (c-syntactic-end-of-macro)
2163 (or (eobp) (forward-char))
2164 (setq pos
(if (<= (point) here
)
2167 (setq c-state-nonlit-pos-cache
(cons pos c-state-nonlit-pos-cache
)))
2169 (if (> pos c-state-nonlit-pos-cache-limit
)
2170 (setq c-state-nonlit-pos-cache-limit pos
))
2173 (defun c-state-literal-at (here)
2174 ;; If position HERE is inside a literal, return (START . END), the
2175 ;; boundaries of the literal (which may be outside the accessible bit of the
2176 ;; buffer). Otherwise, return nil.
2178 ;; This function is almost the same as `c-literal-limits'. Previously, it
2179 ;; differed in that it was a lower level function, and that it rigorously
2180 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2181 ;; virtually identical to this function.
2185 (let ((pos (c-state-safe-place here
)))
2186 (car (cddr (c-state-pp-to-literal pos here
)))))))
2188 (defsubst c-state-lit-beg
(pos)
2189 ;; Return the start of the literal containing POS, or POS itself.
2190 (or (car (c-state-literal-at pos
))
2193 (defsubst c-state-cache-non-literal-place
(pos state
)
2194 ;; Return a position outside of a string/comment/macro at or before POS.
2195 ;; STATE is the parse-partial-sexp state at POS.
2196 (let ((res (if (or (nth 3 state
) ; in a string?
2197 (nth 4 state
)) ; in a comment?
2202 (if (c-beginning-of-macro)
2206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2207 ;; Stuff to do with point-min, and coping with any literal there.
2208 (defvar c-state-point-min
1)
2209 (make-variable-buffer-local 'c-state-point-min
)
2210 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2211 ;; narrowing is likely to affect the parens that are visible before the point.
2213 (defvar c-state-point-min-lit-type nil
)
2214 (make-variable-buffer-local 'c-state-point-min-lit-type
)
2215 (defvar c-state-point-min-lit-start nil
)
2216 (make-variable-buffer-local 'c-state-point-min-lit-start
)
2217 ;; These two variables define the literal, if any, containing point-min.
2218 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2219 ;; literal. If there's no literal there, they're both nil.
2221 (defvar c-state-min-scan-pos
1)
2222 (make-variable-buffer-local 'c-state-min-scan-pos
)
2223 ;; This is the earliest buffer-pos from which scanning can be done. It is
2224 ;; either the end of the literal containing point-min, or point-min itself.
2225 ;; It becomes nil if the buffer is changed earlier than this point.
2226 (defun c-state-get-min-scan-pos ()
2227 ;; Return the lowest valid scanning pos. This will be the end of the
2228 ;; literal enclosing point-min, or point-min itself.
2229 (or c-state-min-scan-pos
2233 (goto-char c-state-point-min-lit-start
)
2234 (if (eq c-state-point-min-lit-type
'string
)
2236 (forward-comment 1))
2237 (setq c-state-min-scan-pos
(point))))))
2239 (defun c-state-mark-point-min-literal ()
2240 ;; Determine the properties of any literal containing POINT-MIN, setting the
2241 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2242 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2243 (let ((p-min (point-min))
2247 (setq lit
(c-state-literal-at p-min
))
2249 (setq c-state-point-min-lit-type
2251 (goto-char (car lit
))
2253 ((looking-at c-block-comment-start-regexp
) 'c
)
2254 ((looking-at c-line-comment-starter
) 'c
++)
2256 c-state-point-min-lit-start
(car lit
)
2257 c-state-min-scan-pos
(cdr lit
))
2258 (setq c-state-point-min-lit-type nil
2259 c-state-point-min-lit-start nil
2260 c-state-min-scan-pos p-min
)))))
2263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2264 ;; A variable which signals a brace dessert - helpful for reducing the number
2265 ;; of fruitless backward scans.
2266 (defvar c-state-brace-pair-desert nil
)
2267 (make-variable-buffer-local 'c-state-brace-pair-desert
)
2268 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2269 ;; that defun has searched backwards for a brace pair and not found one. Its
2270 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2271 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2272 ;; nil when at top level) and FROM is where the backward search started. It
2273 ;; is reset to nil in `c-invalidate-state-cache'.
2276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2277 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2278 ;; list of like structure.
2279 (defmacro c-state-cache-top-lparen
(&optional cache
)
2280 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2281 ;; (default `c-state-cache') (or nil).
2282 (let ((cash (or cache
'c-state-cache
)))
2283 `(if (consp (car ,cash
))
2287 (defmacro c-state-cache-top-paren
(&optional cache
)
2288 ;; Return the address of the latest brace/bracket/paren (whether left or
2289 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2290 (let ((cash (or cache
'c-state-cache
)))
2291 `(if (consp (car ,cash
))
2295 (defmacro c-state-cache-after-top-paren
(&optional cache
)
2296 ;; Return the position just after the latest brace/bracket/paren (whether
2297 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2298 (let ((cash (or cache
'c-state-cache
)))
2299 `(if (consp (car ,cash
))
2302 (1+ (car ,cash
))))))
2304 (defun c-get-cache-scan-pos (here)
2305 ;; From the state-cache, determine the buffer position from which we might
2306 ;; scan forward to HERE to update this cache. This position will be just
2307 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2308 ;; return the earliest position in the accessible region which isn't within
2309 ;; a literal. If the visible portion of the buffer is entirely within a
2310 ;; literal, return NIL.
2311 (let ((c c-state-cache
) elt
)
2312 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2314 (>= (c-state-cache-top-lparen c
) here
))
2320 (if (> (cdr elt
) here
)
2324 ((<= (c-state-get-min-scan-pos) here
)
2325 (c-state-get-min-scan-pos))
2328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2329 ;; Variables which keep track of preprocessor constructs.
2330 (defvar c-state-old-cpp-beg nil
)
2331 (make-variable-buffer-local 'c-state-old-cpp-beg
)
2332 (defvar c-state-old-cpp-end nil
)
2333 (make-variable-buffer-local 'c-state-old-cpp-end
)
2334 ;; These are the limits of the macro containing point at the previous call of
2335 ;; `c-parse-state', or nil.
2337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2338 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2339 (defun c-get-fallback-scan-pos (here)
2340 ;; Return a start position for building `c-state-cache' from
2341 ;; scratch. This will be at the top level, 2 defuns back.
2343 ;; Go back 2 bods, but ignore any bogus positions returned by
2344 ;; beginning-of-defun (i.e. open paren in column zero).
2347 (while (not (or (bobp) (zerop cnt
)))
2348 (c-beginning-of-defun-1) ; Pure elisp BOD.
2349 (if (eq (char-after) ?\
{)
2350 (setq cnt
(1- cnt
)))))
2353 (defun c-state-balance-parens-backwards (here- here
+ top
)
2354 ;; Return the position of the opening paren/brace/bracket before HERE- which
2355 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2356 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2358 ;; ............................................
2360 ;; ( [ ( .........#macro.. ) ( ) ] )
2363 ;; return HERE- HERE+ TOP
2365 ;; If there aren't enough opening paren/brace/brackets, return the position
2366 ;; of the outermost one found, or HERE- if there are none. If there are no
2367 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2368 ;; must not be inside literals. Only the accessible portion of the buffer
2371 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2372 ;; `here'. Go round the next loop each time we pass over such a ")". These
2373 ;; probably match "("s before `here-'.
2374 (let (pos pa ren
+1 lonely-rens
)
2377 (narrow-to-region (point-min) top
) ; This can move point, sometimes.
2381 (setq ren
+1 (scan-lists pos
1 1)) ; might signal
2382 (setq lonely-rens
(cons ren
+1 lonely-rens
)
2385 ;; PART 2: Scan back before `here-' searching for the "("s
2386 ;; matching/mismatching the ")"s found above. We only need to direct the
2387 ;; caller to scan when we've encountered unmatched right parens.
2392 (and lonely-rens
; actual values aren't used.
2393 (setq pa
(scan-lists pos -
1 1)))
2395 (setq lonely-rens
(cdr lonely-rens
)))))
2398 (defun c-parse-state-get-strategy (here good-pos
)
2399 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2400 ;; to minimize the amount of scanning. HERE is the pertinent position in
2401 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2402 ;; its head trimmed) is known to be good, or nil if there is no such
2405 ;; The return value is a list, one of the following:
2407 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2408 ;; which is not less than CACHE-POS.
2409 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2410 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2412 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2413 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2415 (let ((cache-pos (c-get-cache-scan-pos here
)) ; highest position below HERE in cache (or 1)
2416 BOD-pos
; position of 2nd BOD before HERE.
2417 strategy
; 'forward, 'backward, 'BOD, or 'IN-LIT.
2419 how-far
) ; putative scanning distance.
2420 (setq good-pos
(or good-pos
(c-state-get-min-scan-pos)))
2422 ((< here
(c-state-get-min-scan-pos))
2423 (setq strategy
'IN-LIT
2428 (setq strategy
'forward
2429 start-point
(max good-pos cache-pos
)
2430 how-far
(- here start-point
)))
2431 ((< (- good-pos here
) (- here cache-pos
)) ; FIXME!!! ; apply some sort of weighting.
2432 (setq strategy
'backward
2433 how-far
(- good-pos here
)))
2435 (setq strategy
'forward
2436 how-far
(- here cache-pos
)
2437 start-point cache-pos
)))
2439 ;; Might we be better off starting from the top level, two defuns back,
2441 (when (> how-far c-state-cache-too-far
)
2442 (setq BOD-pos
(c-get-fallback-scan-pos here
)) ; somewhat EXPENSIVE!!!
2443 (if (< (- here BOD-pos
) how-far
)
2445 start-point BOD-pos
)))
2449 (and (memq strategy
'(forward backward
)) cache-pos
)
2450 (and (memq strategy
'(forward BOD
)) start-point
))))
2453 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2454 ;; Routines which change `c-state-cache' and associated values.
2455 (defun c-renarrow-state-cache ()
2456 ;; The region (more precisely, point-min) has changed since we
2457 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2458 (if (< (point-min) c-state-point-min
)
2459 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2460 ;; It would be possible to do a better job here and recalculate the top
2463 (c-state-mark-point-min-literal)
2464 (setq c-state-cache nil
2465 c-state-cache-good-pos c-state-min-scan-pos
2466 c-state-brace-pair-desert nil
))
2468 ;; point-min has MOVED FORWARD.
2470 ;; Is the new point-min inside a (different) literal?
2471 (unless (and c-state-point-min-lit-start
; at prev. point-min
2472 (< (point-min) (c-state-get-min-scan-pos)))
2473 (c-state-mark-point-min-literal))
2475 ;; Cut off a bit of the tail from `c-state-cache'.
2476 (let ((ptr (cons nil c-state-cache
))
2478 (while (and (setq pa
(c-state-cache-top-lparen (cdr ptr
)))
2479 (>= pa
(point-min)))
2480 (setq ptr
(cdr ptr
)))
2483 (if (eq (cdr ptr
) c-state-cache
)
2484 (setq c-state-cache nil
2485 c-state-cache-good-pos c-state-min-scan-pos
)
2487 (setq c-state-cache-good-pos
(1+ (c-state-cache-top-lparen))))
2490 (setq c-state-point-min
(point-min)))
2492 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim
)
2493 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2494 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2495 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2496 ;; the highest brace pair whose "}" is below UPPER-LIM.
2498 ;; Return non-nil when this has been done.
2500 ;; This routine should be fast. Since it can get called a LOT, we maintain
2501 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2502 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2505 (let ((bra from
) ce
; Positions of "{" and "}".
2507 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2508 (macro-start-or-from
2509 (progn (goto-char from
)
2510 (c-beginning-of-macro)
2512 (or upper-lim
(setq upper-lim from
))
2514 ;; If we're essentially repeating a fruitless search, just give up.
2515 (unless (and c-state-brace-pair-desert
2516 (eq cache-pos
(car c-state-brace-pair-desert
))
2517 (<= from
(cdr c-state-brace-pair-desert
)))
2518 ;; Only search what we absolutely need to:
2519 (if (and c-state-brace-pair-desert
2520 (eq cache-pos
(car c-state-brace-pair-desert
)))
2521 (narrow-to-region (cdr c-state-brace-pair-desert
) (point-max)))
2523 ;; In the next pair of nested loops, the inner one moves back past a
2524 ;; pair of (mis-)matching parens or brackets; the outer one moves
2525 ;; back over a sequence of unmatched close brace/paren/bracket each
2531 (and (setq ce
(scan-lists bra -
1 -
1)) ; back past )/]/}; might signal
2532 (setq bra
(scan-lists ce -
1 1)) ; back past (/[/{; might signal
2533 (or (> ce upper-lim
)
2534 (not (eq (char-after bra
) ?\
{))
2535 (and (goto-char bra
)
2536 (c-beginning-of-macro)
2537 (< (point) macro-start-or-from
))))))
2538 (and ce
(< ce bra
)))
2539 (setq bra ce
)) ; If we just backed over an unbalanced closing
2542 (if (and ce
(< bra ce
) (eq (char-after bra
) ?\
{))
2543 ;; We've found the desired brace-pair.
2545 (setq new-cons
(cons bra
(1+ ce
)))
2547 ((consp (car c-state-cache
))
2548 (setcar c-state-cache new-cons
))
2549 ((and (numberp (car c-state-cache
)) ; probably never happens
2550 (< ce
(car c-state-cache
)))
2551 (setcdr c-state-cache
2552 (cons new-cons
(cdr c-state-cache
))))
2553 (t (setq c-state-cache
(cons new-cons c-state-cache
)))))
2555 ;; We haven't found a brace pair. Record this.
2556 (setq c-state-brace-pair-desert
(cons cache-pos from
))))))))
2558 (defsubst c-state-push-any-brace-pair
(bra+1 macro-start-or-here
)
2559 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2560 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2561 ;; "push" "a" brace pair onto `c-state-cache'.
2563 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2564 ;; otherwise push it normally.
2566 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2567 ;; latter is inside a macro, not being a macro containing
2568 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2569 ;; base pair. This latter case is assumed to be rare.
2571 ;; Note: POINT is not preserved in this routine.
2573 (if (or (> bra
+1 macro-start-or-here
)
2574 (progn (goto-char bra
+1)
2575 (not (c-beginning-of-macro))))
2577 (cons (cons (1- bra
+1)
2578 (scan-lists bra
+1 1 1))
2579 (if (consp (car c-state-cache
))
2582 ;; N.B. This defsubst codes one method for the simple, normal case,
2583 ;; and a more sophisticated, slower way for the general case. Don't
2584 ;; eliminate this defsubst - it's a speed optimization.
2585 (c-append-lower-brace-pair-to-state-cache (1- bra
+1)))))
2587 (defun c-append-to-state-cache (from)
2588 ;; Scan the buffer from FROM to (point-max), adding elements into
2589 ;; `c-state-cache' for braces etc. Return a candidate for
2590 ;; `c-state-cache-good-pos'.
2592 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2593 ;; any. Typically, it is immediately after it. It must not be inside a
2595 (let ((here-bol (c-point 'bol
(point-max)))
2596 (macro-start-or-here
2597 (save-excursion (goto-char (point-max))
2598 (if (c-beginning-of-macro)
2601 pa
+1 ; pos just after an opening PAren (or brace).
2602 (ren+1 from
) ; usually a pos just after an closing paREN etc.
2603 ; Is actually the pos. to scan for a (/{/[ from,
2604 ; which sometimes is after a silly )/}/].
2605 paren
+1 ; Pos after some opening or closing paren.
2606 paren
+1s
; A list of `paren+1's; used to determine a
2608 bra
+1 ce
+1 ; just after L/R bra-ces.
2609 bra
+1s
; list of OLD values of bra+1.
2610 mstart
) ; start of a macro.
2613 ;; Each time round the following loop, we enter a successively deeper
2614 ;; level of brace/paren nesting. (Except sometimes we "continue at
2615 ;; the existing level".) `pa+1' is a pos inside an opening
2616 ;; brace/paren/bracket, usually just after it.
2619 ;; Each time round the next loop moves forward over an opening then
2620 ;; a closing brace/bracket/paren. This loop is white hot, so it
2621 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2622 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2623 ;; call of `scan-lists' signals an error, which happens when there
2624 ;; are no more b/b/p's to scan.
2627 (setq pa
+1 (scan-lists ren
+1 1 -
1) ; Into (/{/[; might signal
2628 paren
+1s
(cons pa
+1 paren
+1s
))
2629 (setq ren
+1 (scan-lists pa
+1 1 1)) ; Out of )/}/]; might signal
2630 (if (and (eq (char-before pa
+1) ?
{)) ; Check for a macro later.
2632 (setcar paren
+1s ren
+1)))
2634 (if (and pa
+1 (> pa
+1 ren
+1))
2635 ;; We've just entered a deeper nesting level.
2637 ;; Insert the brace pair (if present) and the single open
2638 ;; paren/brace/bracket into `c-state-cache' It cannot be
2639 ;; inside a macro, except one around point, because of what
2640 ;; `c-neutralize-syntax-in-CPP' has done.
2641 (c-state-push-any-brace-pair bra
+1 macro-start-or-here
)
2642 ;; Insert the opening brace/bracket/paren position.
2643 (setq c-state-cache
(cons (1- pa
+1) c-state-cache
))
2644 ;; Clear admin stuff for the next more nested part of the scan.
2645 (setq ren
+1 pa
+1 pa
+1 nil bra
+1 nil bra
+1s nil
)
2646 t
) ; Carry on the loop
2648 ;; All open p/b/b's at this nesting level, if any, have probably
2649 ;; been closed by matching/mismatching ones. We're probably
2650 ;; finished - we just need to check for having found an
2651 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2652 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2653 (c-safe (setq ren
+1 (scan-lists ren
+1 1 1)))))) ; acts as loop control.
2655 ;; Record the final, innermost, brace-pair if there is one.
2656 (c-state-push-any-brace-pair bra
+1 macro-start-or-here
)
2658 ;; Determine a good pos
2659 (while (and (setq paren
+1 (car paren
+1s
))
2660 (> (if (> paren
+1 macro-start-or-here
)
2663 (setq mstart
(and (c-beginning-of-macro)
2665 (or mstart paren
+1))
2667 (setq paren
+1s
(cdr paren
+1s
)))
2669 ((and paren
+1 mstart
)
2670 (min paren
+1 mstart
))
2674 (defun c-remove-stale-state-cache (good-pos pps-point
)
2675 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2676 ;; not be in it when it is amended for position (point-max).
2677 ;; Additionally, the "outermost" open-brace entry before (point-max)
2678 ;; will be converted to a cons if the matching close-brace is scanned.
2680 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2681 ;; parens/braces/brackets between GOOD-POS and (point-max).
2683 ;; As a second thing, calculate the result of parse-partial-sexp at
2684 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2685 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2686 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2687 ;; needs to be FAST).
2689 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2690 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2691 ;; to be good (we aim for this to be as high as possible);
2692 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2693 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2694 ;; position to scan backwards from.
2695 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2697 (narrow-to-region 1 (point-max))
2699 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2701 (goto-char (point-max))
2702 (and (c-beginning-of-macro)
2704 (good-pos-actual-macro-start ; Start of macro containing good-pos
2706 (and (< good-pos
(point-max))
2708 (goto-char good-pos
)
2709 (and (c-beginning-of-macro)
2711 (good-pos-actual-macro-end ; End of this macro, (maybe
2712 ; (point-max)), or nil.
2713 (and good-pos-actual-macro-start
2715 (goto-char good-pos-actual-macro-start
)
2718 pps-state
; Will be 9 or 10 elements long.
2720 upper-lim
; ,beyond which `c-state-cache' entries are removed
2722 pair-beg pps-point-state target-depth
)
2724 ;; Remove entries beyond (point-max). Also remove any entries inside
2725 ;; a macro, unless (point-max) is in the same macro.
2727 (if (or (null c-state-old-cpp-beg
)
2728 (and (> (point-max) c-state-old-cpp-beg
)
2729 (< (point-max) c-state-old-cpp-end
)))
2731 (min (point-max) c-state-old-cpp-beg
)))
2732 (while (and c-state-cache
(>= (c-state-cache-top-lparen) upper-lim
))
2733 (setq c-state-cache
(cdr c-state-cache
)))
2734 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2735 ;; RBrace and indicate we'll need to search backwards for a previous
2737 (when (and c-state-cache
2738 (consp (car c-state-cache
))
2739 (> (cdar c-state-cache
) upper-lim
))
2740 (setcar c-state-cache
(caar c-state-cache
))
2741 (setq scan-back-pos
(car c-state-cache
)))
2743 ;; The next loop jumps forward out of a nested level of parens each
2744 ;; time round; the corresponding elements in `c-state-cache' are
2745 ;; removed. `pos' is just after the brace-pair or the open paren at
2746 ;; (car c-state-cache). There can be no open parens/braces/brackets
2747 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2748 ;; due to the interface spec to this function.
2749 (setq pos
(if (and good-pos-actual-macro-end
2750 (not (eq good-pos-actual-macro-start
2752 (1+ good-pos-actual-macro-end
) ; get outside the macro as
2753 ; marked by a `category' text property.
2756 (while (and c-state-cache
2757 (< (point) (point-max)))
2759 ((null pps-state
) ; first time through
2760 (setq target-depth -
1))
2761 ((eq (car pps-state
) target-depth
) ; found closing ),},]
2762 (setq target-depth
(1- (car pps-state
))))
2763 ;; Do nothing when we've merely reached pps-point.
2769 (point) (if (< (point) pps-point
) pps-point
(point-max))
2773 (if (= (point) pps-point
)
2774 (setq pps-point-state pps-state
))
2776 (when (eq (car pps-state
) target-depth
)
2777 (setq pos
(point)) ; POS is now just after an R-paren/brace.
2779 ((and (consp (car c-state-cache
))
2780 (eq (point) (cdar c-state-cache
)))
2781 ;; We've just moved out of the paren pair containing the brace-pair
2782 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2783 ;; and is potentially where the open brace of a cons in
2784 ;; c-state-cache will be.
2785 (setq pair-beg
(car-safe (cdr c-state-cache
))
2786 c-state-cache
(cdr-safe (cdr c-state-cache
)))) ; remove {}pair + containing Lparen.
2787 ((numberp (car c-state-cache
))
2788 (setq pair-beg
(car c-state-cache
)
2789 c-state-cache
(cdr c-state-cache
))) ; remove this
2791 ((numberp (cadr c-state-cache
))
2792 (setq pair-beg
(cadr c-state-cache
)
2793 c-state-cache
(cddr c-state-cache
))) ; Remove a paren pair
2794 ; together with enclosed brace pair.
2795 ;; (t nil) ; Ignore an unmated Rparen.
2798 (if (< (point) pps-point
)
2799 (setq pps-state
(parse-partial-sexp (point) pps-point
2800 nil nil
; TARGETDEPTH, STOPBEFORE
2803 ;; If the last paren pair we moved out of was actually a brace pair,
2804 ;; insert it into `c-state-cache'.
2805 (when (and pair-beg
(eq (char-after pair-beg
) ?
{))
2806 (if (consp (car-safe c-state-cache
))
2807 (setq c-state-cache
(cdr c-state-cache
)))
2808 (setq c-state-cache
(cons (cons pair-beg pos
)
2811 (list pos scan-back-pos pps-state
)))))
2813 (defun c-remove-stale-state-cache-backwards (here cache-pos
)
2814 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2815 ;; buffer, and inform the caller of the scenario detected.
2817 ;; HERE is the position we're setting `c-state-cache' for.
2818 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2819 ;; before HERE, or a position at or near point-min which isn't in a
2822 ;; This function must only be called only when (> `c-state-cache-good-pos'
2823 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2824 ;; optimized to eliminate (or minimize) scanning between these two
2827 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2828 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2829 ;; could become so after missing elements are inserted into
2830 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2831 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2832 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2833 ;; before `here''s line, or the start of the literal containing it.
2834 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2835 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2836 ;; to scan backwards from.
2837 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2838 ;; POS and HERE which aren't recorded in `c-state-cache'.
2840 ;; The comments in this defun use "paren" to mean parenthesis or square
2841 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2843 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2845 ;; CP E here D C good
2846 (let ((pos c-state-cache-good-pos
)
2847 pa ren
; positions of "(" and ")"
2848 dropped-cons
; whether the last element dropped from `c-state-cache'
2849 ; was a cons (representing a brace-pair)
2850 good-pos
; see above.
2851 lit
; (START . END) of a literal containing some point.
2852 here-lit-start here-lit-end
; bounds of literal containing `here'
2854 here- here
+ ; start/end of macro around HERE, or HERE
2855 (here-bol (c-point 'bol here
))
2856 (too-far-back (max (- here c-state-cache-too-far
) (point-min))))
2858 ;; Remove completely irrelevant entries from `c-state-cache'.
2859 (while (and c-state-cache
2860 (>= (setq pa
(c-state-cache-top-lparen)) here
))
2861 (setq dropped-cons
(consp (car c-state-cache
)))
2862 (setq c-state-cache
(cdr c-state-cache
))
2864 ;; At this stage, (> pos here);
2865 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2868 ((and (consp (car c-state-cache
))
2869 (> (cdar c-state-cache
) here
))
2870 ;; CASE 1: The top of the cache is a brace pair which now encloses
2871 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2872 ;; knowledge of what's inside these braces, we have no alternative but
2873 ;; to direct the caller to scan the buffer from the opening brace.
2874 (setq pos
(caar c-state-cache
))
2875 (setcar c-state-cache pos
)
2876 (list (1+ pos
) pos t
)) ; return value. We've just converted a brace pair
2877 ; entry into a { entry, so the caller needs to
2878 ; search for a brace pair before the {.
2880 ;; `here' might be inside a literal. Check for this.
2882 (setq lit
(c-state-literal-at here
)
2883 here-lit-start
(or (car lit
) here
)
2884 here-lit-end
(or (cdr lit
) here
))
2885 ;; Has `here' just "newly entered" a macro?
2887 (goto-char here-lit-start
)
2888 (if (and (c-beginning-of-macro)
2889 (or (null c-state-old-cpp-beg
)
2890 (not (= (point) c-state-old-cpp-beg
))))
2892 (setq here-
(point))
2894 (setq here
+ (point)))
2895 (setq here- here-lit-start
2896 here
+ here-lit-end
)))
2898 ;; `here' might be nested inside any depth of parens (or brackets but
2899 ;; not braces). Scan backwards to find the outermost such opening
2900 ;; paren, if there is one. This will be the scan position to return.
2902 (narrow-to-region cache-pos
(point-max))
2903 (setq pos
(c-state-balance-parens-backwards here- here
+ pos
)))
2904 nil
)) ; for the cond
2906 ((< pos here-lit-start
)
2907 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2908 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2909 ;; a brace pair preceding this, it will already be in `c-state-cache',
2910 ;; unless there was a brace pair after it, i.e. there'll only be one to
2911 ;; scan for if we've just deleted one.
2912 (list pos
(and dropped-cons pos
) t
)) ; Return value.
2914 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2915 ;; Further forward scanning isn't needed, but we still need to find a
2916 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2919 (narrow-to-region here-bol
(point-max))
2920 (setq pos here-lit-start
)
2921 (c-safe (while (setq pa
(scan-lists pos -
1 1))
2922 (setq pos pa
)))) ; might signal
2923 nil
)) ; for the cond
2925 ((setq ren
(c-safe-scan-lists pos -
1 -
1 too-far-back
))
2926 ;; CASE 3: After a }/)/] before `here''s BOL.
2927 (list (1+ ren
) (and dropped-cons pos
) nil
)) ; Return value
2930 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2931 ;; literal containing it.
2932 (setq good-pos
(c-state-lit-beg (c-point 'bopl here-bol
)))
2933 (list good-pos
(and dropped-cons good-pos
) nil
)))))
2936 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2937 ;; Externally visible routines.
2939 (defun c-state-cache-init ()
2940 (setq c-state-cache nil
2941 c-state-cache-good-pos
1
2942 c-state-nonlit-pos-cache nil
2943 c-state-nonlit-pos-cache-limit
1
2944 c-state-brace-pair-desert nil
2946 c-state-point-min-lit-type nil
2947 c-state-point-min-lit-start nil
2948 c-state-min-scan-pos
1
2949 c-state-old-cpp-beg nil
2950 c-state-old-cpp-end nil
)
2951 (c-state-mark-point-min-literal))
2953 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2954 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
2955 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
2956 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
2957 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
2958 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
2959 ;; (defun c-state-dump ()
2960 ;; ;; For debugging.
2963 ;; (c-sc-qde c-state-cache)
2964 ;; (c-sc-de c-state-cache-good-pos)
2965 ;; (c-sc-qde c-state-nonlit-pos-cache)
2966 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
2967 ;; (c-sc-qde c-state-brace-pair-desert)
2968 ;; (c-sc-de c-state-point-min)
2969 ;; (c-sc-de c-state-point-min-lit-type)
2970 ;; (c-sc-de c-state-point-min-lit-start)
2971 ;; (c-sc-de c-state-min-scan-pos)
2972 ;; (c-sc-de c-state-old-cpp-beg)
2973 ;; (c-sc-de c-state-old-cpp-end)))
2974 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2976 (defun c-invalidate-state-cache-1 (here)
2977 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2978 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2979 ;; left in a consistent state.
2981 ;; This is much like `c-whack-state-after', but it never changes a paren
2982 ;; pair element into an open paren element. Doing that would mean that the
2983 ;; new open paren wouldn't have the required preceding paren pair element.
2985 ;; This function is called from c-after-change.
2987 ;; The cache of non-literals:
2988 (if (< here c-state-nonlit-pos-cache-limit
)
2989 (setq c-state-nonlit-pos-cache-limit here
))
2992 ;; Case 1: if `here' is in a literal containing point-min, everything
2993 ;; becomes (or is already) nil.
2994 (if (or (null c-state-cache-good-pos
)
2995 (< here
(c-state-get-min-scan-pos)))
2996 (setq c-state-cache nil
2997 c-state-cache-good-pos nil
2998 c-state-min-scan-pos nil
)
3000 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3001 ;; below `here'. To maintain its consistency, we may need to insert a new
3003 (let ((here-bol (c-point 'bol here
))
3004 too-high-pa
; recorded {/(/[ next above here, or nil.
3005 dropped-cons
; was the last removed element a brace pair?
3007 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3008 (while (and c-state-cache
3009 (>= (setq pa
(c-state-cache-top-paren)) here
))
3010 (setq dropped-cons
(consp (car c-state-cache
))
3011 too-high-pa
(c-state-cache-top-lparen)
3012 c-state-cache
(cdr c-state-cache
)))
3014 ;; Do we need to add in an earlier brace pair, having lopped one off?
3015 (if (and dropped-cons
3016 (< too-high-pa
(+ here c-state-cache-too-far
)))
3017 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol
))
3018 (setq c-state-cache-good-pos
(or (c-state-cache-after-top-paren)
3019 (c-state-get-min-scan-pos)))))
3021 ;; The brace-pair desert marker:
3022 (when (car c-state-brace-pair-desert
)
3023 (if (< here
(car c-state-brace-pair-desert
))
3024 (setq c-state-brace-pair-desert nil
)
3025 (if (< here
(cdr c-state-brace-pair-desert
))
3026 (setcdr c-state-brace-pair-desert here
)))))
3028 (defun c-parse-state-1 ()
3029 ;; Find and record all noteworthy parens between some good point earlier in
3030 ;; the file and point. That good point is at least the beginning of the
3031 ;; top-level construct we are in, or the beginning of the preceding
3032 ;; top-level construct if we aren't in one.
3034 ;; The returned value is a list of the noteworthy parens with the last one
3035 ;; first. If an element in the list is an integer, it's the position of an
3036 ;; open paren (of any type) which has not been closed before the point. If
3037 ;; an element is a cons, it gives the position of a closed BRACE paren
3038 ;; pair[*]; the car is the start brace position and the cdr is the position
3039 ;; following the closing brace. Only the last closed brace paren pair
3040 ;; before each open paren and before the point is recorded, and thus the
3041 ;; state never contains two cons elements in succession. When a close brace
3042 ;; has no matching open brace (e.g., the matching brace is outside the
3043 ;; visible region), it is not represented in the returned value.
3045 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3046 ;; This defun explicitly treats mismatching parens/braces/brackets as
3047 ;; matching. It is the open brace which makes it a "brace" pair.
3049 ;; If POINT is within a macro, open parens and brace pairs within
3050 ;; THIS macro MIGHT be recorded. This depends on whether their
3051 ;; syntactic properties have been suppressed by
3052 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3054 ;; Currently no characters which are given paren syntax with the
3055 ;; syntax-table property are recorded, i.e. angle bracket arglist
3056 ;; parens are never present here. Note that this might change.
3058 ;; BUG: This function doesn't cope entirely well with unbalanced
3059 ;; parens in macros. (2008-12-11: this has probably been resolved
3060 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3061 ;; following case the brace before the macro isn't balanced with the
3068 ;; Note to maintainers: this function DOES get called with point
3069 ;; within comments and strings, so don't assume it doesn't!
3071 ;; This function might do hidden buffer changes.
3072 (let* ((here (point))
3073 (here-bopl (c-point 'bopl
))
3074 strategy
; 'forward, 'backward etc..
3075 ;; Candidate positions to start scanning from:
3076 cache-pos
; highest position below HERE already existing in
3082 scan-backward-pos scan-forward-p
) ; used for 'backward.
3083 ;; If POINT-MIN has changed, adjust the cache
3084 (unless (= (point-min) c-state-point-min
)
3085 (c-renarrow-state-cache))
3088 (setq res
(c-parse-state-get-strategy here c-state-cache-good-pos
)
3090 cache-pos
(cadr res
)
3091 start-point
(nth 2 res
))
3093 (when (eq strategy
'BOD
)
3094 (setq c-state-cache nil
3095 c-state-cache-good-pos start-point
))
3100 ((memq strategy
'(forward BOD
))
3101 (narrow-to-region (point-min) here
)
3102 (setq res
(c-remove-stale-state-cache start-point here-bopl
))
3103 (setq cache-pos
(car res
)
3104 scan-backward-pos
(cadr res
)
3105 bopl-state
(car (cddr res
))) ; will be nil if (< here-bopl
3107 (if scan-backward-pos
3108 (c-append-lower-brace-pair-to-state-cache scan-backward-pos
))
3110 (c-append-to-state-cache cache-pos
))
3111 (setq c-state-cache-good-pos
3113 (< good-pos
(- here c-state-cache-too-far
)))
3114 (c-state-cache-non-literal-place here-bopl bopl-state
)
3117 ((eq strategy
'backward
)
3118 (setq res
(c-remove-stale-state-cache-backwards here cache-pos
)
3120 scan-backward-pos
(cadr res
)
3121 scan-forward-p
(car (cddr res
)))
3122 (if scan-backward-pos
3123 (c-append-lower-brace-pair-to-state-cache
3125 (setq c-state-cache-good-pos
3127 (progn (narrow-to-region (point-min) here
)
3128 (c-append-to-state-cache good-pos
))
3130 (c-get-cache-scan-pos good-pos
))))
3132 (t ; (eq strategy 'IN-LIT)
3133 (setq c-state-cache nil
3134 c-state-cache-good-pos nil
)))))
3138 (defun c-invalidate-state-cache (here)
3139 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3141 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3142 ;; of all parens in preprocessor constructs, except for any such construct
3143 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3144 ;; worrying further about macros and template delimiters.
3145 (c-with-<-
>-as-parens-suppressed
3146 (if (and c-state-old-cpp-beg
3147 (< c-state-old-cpp-beg here
))
3148 (c-with-all-but-one-cpps-commented-out
3150 (min c-state-old-cpp-end here
)
3151 (c-invalidate-state-cache-1 here
))
3152 (c-with-cpps-commented-out
3153 (c-invalidate-state-cache-1 here
)))))
3155 (defun c-parse-state ()
3156 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3157 ;; description of the functionality and return value.
3159 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3160 ;; of all parens in preprocessor constructs, except for any such construct
3161 ;; containing point. We can then call `c-parse-state-1' without worrying
3162 ;; further about macros and template delimiters.
3163 (let (here-cpp-beg here-cpp-end
)
3165 (when (c-beginning-of-macro)
3166 (setq here-cpp-beg
(point))
3168 (> (setq here-cpp-end
(c-syntactic-end-of-macro))
3170 (setq here-cpp-beg nil here-cpp-end nil
))))
3171 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3174 (c-with-<-
>-as-parens-suppressed
3175 (if (and here-cpp-beg
(> here-cpp-end here-cpp-beg
))
3176 (c-with-all-but-one-cpps-commented-out
3177 here-cpp-beg here-cpp-end
3179 (c-with-cpps-commented-out
3180 (c-parse-state-1))))
3181 (setq c-state-old-cpp-beg
(and here-cpp-beg
(copy-marker here-cpp-beg t
))
3182 c-state-old-cpp-end
(and here-cpp-end
(copy-marker here-cpp-end t
)))
3185 ;; Debug tool to catch cache inconsistencies. This is called from
3187 (defvar c-debug-parse-state nil
)
3188 (unless (fboundp 'c-real-parse-state
)
3189 (fset 'c-real-parse-state
(symbol-function 'c-parse-state
)))
3190 (cc-bytecomp-defun c-real-parse-state)
3192 (defvar c-parse-state-state nil
)
3193 (defun c-record-parse-state-state ()
3194 (setq c-parse-state-state
3197 (cons arg
(symbol-value arg
)))
3199 c-state-cache-good-pos
3200 c-state-nonlit-pos-cache
3201 c-state-nonlit-pos-cache-limit
3202 c-state-brace-pair-desert
3204 c-state-point-min-lit-type
3205 c-state-point-min-lit-start
3206 c-state-min-scan-pos
3208 c-state-old-cpp-end
))))
3209 (defun c-replay-parse-state-state ()
3214 (format "%s %s%s" (car arg
) (if (atom (cdr arg
)) "" "'") (cdr arg
)))
3215 c-parse-state-state
" ")
3218 (defun c-debug-parse-state ()
3219 (let ((here (point)) (res1 (c-real-parse-state)) res2
)
3220 (let ((c-state-cache nil
)
3221 (c-state-cache-good-pos 1)
3222 (c-state-nonlit-pos-cache nil
)
3223 (c-state-nonlit-pos-cache-limit 1)
3224 (c-state-brace-pair-desert nil
)
3225 (c-state-point-min 1)
3226 (c-state-point-min-lit-type nil
)
3227 (c-state-point-min-lit-start nil
)
3228 (c-state-min-scan-pos 1)
3229 (c-state-old-cpp-beg nil
)
3230 (c-state-old-cpp-end nil
))
3231 (setq res2
(c-real-parse-state)))
3232 (unless (equal res1 res2
)
3233 ;; The cache can actually go further back due to the ad-hoc way
3234 ;; the first paren is found, so try to whack off a bit of its
3235 ;; start before complaining.
3237 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3238 ;; (c-beginning-of-defun-1)
3239 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3240 ;; (c-beginning-of-defun-1))
3241 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3242 ;; (message (concat "c-parse-state inconsistency at %s: "
3243 ;; "using cache: %s, from scratch: %s")
3244 ;; here res1 res2)))
3245 (message (concat "c-parse-state inconsistency at %s: "
3246 "using cache: %s, from scratch: %s")
3248 (message "Old state:")
3249 (c-replay-parse-state-state))
3250 (c-record-parse-state-state)
3253 (defun c-toggle-parse-state-debug (&optional arg
)
3255 (setq c-debug-parse-state
(c-calculate-state arg c-debug-parse-state
))
3256 (fset 'c-parse-state
(symbol-function (if c-debug-parse-state
3257 'c-debug-parse-state
3258 'c-real-parse-state
)))
3259 (c-keep-region-active))
3260 (when c-debug-parse-state
3261 (c-toggle-parse-state-debug 1))
3264 (defun c-whack-state-before (bufpos paren-state
)
3265 ;; Whack off any state information from PAREN-STATE which lies
3266 ;; before BUFPOS. Not destructive on PAREN-STATE.
3267 (let* ((newstate (list nil
))
3271 (setq car
(car paren-state
)
3272 paren-state
(cdr paren-state
))
3273 (if (< (if (consp car
) (car car
) car
) bufpos
)
3274 (setq paren-state nil
)
3275 (setcdr ptr
(list car
))
3276 (setq ptr
(cdr ptr
))))
3279 (defun c-whack-state-after (bufpos paren-state
)
3280 ;; Whack off any state information from PAREN-STATE which lies at or
3281 ;; after BUFPOS. Not destructive on PAREN-STATE.
3284 (let ((car (car paren-state
)))
3286 ;; just check the car, because in a balanced brace
3287 ;; expression, it must be impossible for the corresponding
3288 ;; close brace to be before point, but the open brace to
3290 (if (<= bufpos
(car car
))
3292 (if (< bufpos
(cdr car
))
3293 ;; its possible that the open brace is before
3294 ;; bufpos, but the close brace is after. In that
3295 ;; case, convert this to a non-cons element. The
3296 ;; rest of the state is before bufpos, so we're
3298 (throw 'done
(cons (car car
) (cdr paren-state
)))
3299 ;; we know that both the open and close braces are
3300 ;; before bufpos, so we also know that everything else
3301 ;; on state is before bufpos.
3302 (throw 'done paren-state
)))
3305 ;; it's before bufpos, so everything else should too.
3306 (throw 'done paren-state
)))
3307 (setq paren-state
(cdr paren-state
)))
3310 (defun c-most-enclosing-brace (paren-state &optional bufpos
)
3311 ;; Return the bufpos of the innermost enclosing open paren before
3312 ;; bufpos, or nil if none was found.
3314 (or bufpos
(setq bufpos
134217727))
3316 (setq enclosingp
(car paren-state
)
3317 paren-state
(cdr paren-state
))
3318 (if (or (consp enclosingp
)
3319 (>= enclosingp bufpos
))
3320 (setq enclosingp nil
)
3321 (setq paren-state nil
)))
3324 (defun c-least-enclosing-brace (paren-state)
3325 ;; Return the bufpos of the outermost enclosing open paren, or nil
3326 ;; if none was found.
3329 (setq elem
(car paren-state
)
3330 paren-state
(cdr paren-state
))
3335 (defun c-safe-position (bufpos paren-state
)
3336 ;; Return the closest "safe" position recorded on PAREN-STATE that
3337 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3338 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3339 ;; find the closest limit before a given limit that might be nil.
3341 ;; A "safe" position is a position at or after a recorded open
3342 ;; paren, or after a recorded close paren. The returned position is
3343 ;; thus either the first position after a close brace, or the first
3344 ;; position after an enclosing paren, or at the enclosing paren in
3345 ;; case BUFPOS is immediately after it.
3350 (setq elem
(car paren-state
))
3352 (cond ((< (cdr elem
) bufpos
)
3353 (throw 'done
(cdr elem
)))
3354 ((< (car elem
) bufpos
)
3356 (throw 'done
(min (1+ (car elem
)) bufpos
))))
3358 ;; elem is the position at and not after the opening paren, so
3359 ;; we can go forward one more step unless it's equal to
3360 ;; bufpos. This is useful in some cases avoid an extra paren
3361 ;; level between the safe position and bufpos.
3362 (throw 'done
(min (1+ elem
) bufpos
))))
3363 (setq paren-state
(cdr paren-state
)))))))
3365 (defun c-beginning-of-syntax ()
3366 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3367 ;; goes to the closest previous point that is known to be outside
3368 ;; any string literal or comment. `c-state-cache' is used if it has
3369 ;; a position in the vicinity.
3370 (let* ((paren-state c-state-cache
)
3374 ;; Note: Similar code in `c-safe-position'. The
3375 ;; difference is that we accept a safe position at
3376 ;; the point and don't bother to go forward past open
3379 (setq elem
(car paren-state
))
3381 (cond ((<= (cdr elem
) (point))
3382 (throw 'done
(cdr elem
)))
3383 ((<= (car elem
) (point))
3384 (throw 'done
(car elem
))))
3385 (if (<= elem
(point))
3386 (throw 'done elem
)))
3387 (setq paren-state
(cdr paren-state
)))
3390 (if (> pos
(- (point) 4000))
3392 ;; The position is far back. Try `c-beginning-of-defun-1'
3393 ;; (although we can't be entirely sure it will go to a position
3394 ;; outside a comment or string in current emacsen). FIXME:
3395 ;; Consult `syntax-ppss' here.
3396 (c-beginning-of-defun-1)
3401 ;; Tools for scanning identifiers and other tokens.
3403 (defun c-on-identifier ()
3404 "Return non-nil if the point is on or directly after an identifier.
3405 Keywords are recognized and not considered identifiers. If an
3406 identifier is detected, the returned value is its starting position.
3407 If an identifier ends at the point and another begins at it \(can only
3408 happen in Pike) then the point for the preceding one is returned.
3410 Note that this function might do hidden buffer changes. See the
3411 comment at the start of cc-engine.el for more info."
3413 ;; FIXME: Shouldn't this function handle "operator" in C++?
3416 (skip-syntax-backward "w_")
3420 ;; Check for a normal (non-keyword) identifier.
3421 (and (looking-at c-symbol-start
)
3422 (not (looking-at c-keywords-regexp
))
3425 (when (c-major-mode-is 'pike-mode
)
3426 ;; Handle the `<operator> syntax in Pike.
3427 (let ((pos (point)))
3428 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3429 (and (if (< (skip-chars-backward "`") 0)
3432 (eq (char-after) ?\
`))
3433 (looking-at c-symbol-key
)
3434 (>= (match-end 0) pos
)
3437 ;; Handle the "operator +" syntax in C++.
3438 (when (and c-overloadable-operators-regexp
3439 (= (c-backward-token-2 0) 0))
3441 (cond ((and (looking-at c-overloadable-operators-regexp
)
3442 (or (not c-opt-op-identifier-prefix
)
3443 (and (= (c-backward-token-2 1) 0)
3444 (looking-at c-opt-op-identifier-prefix
))))
3448 (and c-opt-op-identifier-prefix
3449 (looking-at c-opt-op-identifier-prefix
)
3450 (= (c-forward-token-2 1) 0)
3451 (looking-at c-overloadable-operators-regexp
)))
3456 (defsubst c-simple-skip-symbol-backward
()
3457 ;; If the point is at the end of a symbol then skip backward to the
3458 ;; beginning of it. Don't move otherwise. Return non-nil if point
3461 ;; This function might do hidden buffer changes.
3462 (or (< (skip-syntax-backward "w_") 0)
3463 (and (c-major-mode-is 'pike-mode
)
3464 ;; Handle the `<operator> syntax in Pike.
3465 (let ((pos (point)))
3466 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3467 (< (skip-chars-backward "`") 0)
3468 (looking-at c-symbol-key
)
3469 (>= (match-end 0) pos
))
3474 (defun c-beginning-of-current-token (&optional back-limit
)
3475 ;; Move to the beginning of the current token. Do not move if not
3476 ;; in the middle of one. BACK-LIMIT may be used to bound the
3477 ;; backward search; if given it's assumed to be at the boundary
3478 ;; between two tokens. Return non-nil if the point is moved, nil
3481 ;; This function might do hidden buffer changes.
3482 (let ((start (point)))
3483 (if (looking-at "\\w\\|\\s_")
3484 (skip-syntax-backward "w_" back-limit
)
3485 (when (< (skip-syntax-backward ".()" back-limit
) 0)
3486 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp
)
3488 ;; `c-nonsymbol-token-regexp' should always match
3489 ;; since we've skipped backward over punctuator
3490 ;; or paren syntax, but consume one char in case
3491 ;; it doesn't so that we don't leave point before
3492 ;; some earlier incorrect token.
3495 (goto-char pos
))))))
3498 (defun c-end-of-current-token (&optional back-limit
)
3499 ;; Move to the end of the current token. Do not move if not in the
3500 ;; middle of one. BACK-LIMIT may be used to bound the backward
3501 ;; search; if given it's assumed to be at the boundary between two
3502 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3504 ;; This function might do hidden buffer changes.
3505 (let ((start (point)))
3506 (cond ((< (skip-syntax-backward "w_" (1- start
)) 0)
3507 (skip-syntax-forward "w_"))
3508 ((< (skip-syntax-backward ".()" back-limit
) 0)
3510 (if (looking-at c-nonsymbol-token-regexp
)
3511 (goto-char (match-end 0))
3512 ;; `c-nonsymbol-token-regexp' should always match since
3513 ;; we've skipped backward over punctuator or paren
3514 ;; syntax, but move forward in case it doesn't so that
3515 ;; we don't leave point earlier than we started with.
3517 (< (point) start
)))))
3520 (defconst c-jump-syntax-balanced
3521 (if (memq 'gen-string-delim c-emacs-features
)
3522 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3523 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3525 (defconst c-jump-syntax-unbalanced
3526 (if (memq 'gen-string-delim c-emacs-features
)
3527 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3528 "\\w\\|\\s_\\|\\s\""))
3530 (defun c-forward-token-2 (&optional count balanced limit
)
3531 "Move forward by tokens.
3532 A token is defined as all symbols and identifiers which aren't
3533 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3534 treated properly). Point is always either left at the beginning of a
3535 token or not moved at all. COUNT specifies the number of tokens to
3536 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3537 moves to the next token beginning only if not already at one. If
3538 BALANCED is true, move over balanced parens, otherwise move into them.
3539 Also, if BALANCED is true, never move out of an enclosing paren.
3541 LIMIT sets the limit for the movement and defaults to the point limit.
3542 The case when LIMIT is set in the middle of a token, comment or macro
3543 is handled correctly, i.e. the point won't be left there.
3545 Return the number of tokens left to move \(positive or negative). If
3546 BALANCED is true, a move over a balanced paren counts as one. Note
3547 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3548 be returned. Thus, a return value of 0 guarantees that point is at
3549 the requested position and a return value less \(without signs) than
3550 COUNT guarantees that point is at the beginning of some token.
3552 Note that this function might do hidden buffer changes. See the
3553 comment at the start of cc-engine.el for more info."
3555 (or count
(setq count
1))
3557 (- (c-backward-token-2 (- count
) balanced limit
))
3559 (let ((jump-syntax (if balanced
3560 c-jump-syntax-balanced
3561 c-jump-syntax-unbalanced
))
3566 ;; If count is zero we should jump if in the middle of a token.
3567 (c-end-of-current-token))
3570 (if limit
(narrow-to-region (point-min) limit
))
3572 (progn (c-forward-syntactic-ws) (point)))
3573 ;; Skip whitespace. Count this as a move if we did in
3575 (setq count
(max (1- count
) 0)))
3578 ;; Moved out of bounds. Make sure the returned count isn't zero.
3580 (if (zerop count
) (setq count
1))
3583 ;; Use `condition-case' to avoid having the limit tests
3590 (cond ((looking-at jump-syntax
)
3591 (goto-char (scan-sexps (point) 1))
3593 ((looking-at c-nonsymbol-token-regexp
)
3594 (goto-char (match-end 0))
3596 ;; `c-nonsymbol-token-regexp' above should always
3597 ;; match if there are correct tokens. Try to
3598 ;; widen to see if the limit was set in the
3599 ;; middle of one, else fall back to treating
3600 ;; the offending thing as a one character token.
3604 (looking-at c-nonsymbol-token-regexp
)))
3609 (c-forward-syntactic-ws)
3612 (error (goto-char last
)))
3616 (setq count
(1+ count
)))))
3620 (defun c-backward-token-2 (&optional count balanced limit
)
3621 "Move backward by tokens.
3622 See `c-forward-token-2' for details."
3624 (or count
(setq count
1))
3626 (- (c-forward-token-2 (- count
) balanced limit
))
3628 (or limit
(setq limit
(point-min)))
3629 (let ((jump-syntax (if balanced
3630 c-jump-syntax-balanced
3631 c-jump-syntax-unbalanced
))
3635 ;; The count is zero so try to skip to the beginning of the
3638 (progn (c-beginning-of-current-token) (point)))
3639 (if (< (point) limit
)
3640 ;; The limit is inside the same token, so return 1.
3643 ;; We're not in the middle of a token. If there's
3644 ;; whitespace after the point then we must move backward,
3645 ;; so set count to 1 in that case.
3646 (and (looking-at c-syntactic-ws-start
)
3647 ;; If we're looking at a '#' that might start a cpp
3648 ;; directive then we have to do a more elaborate check.
3649 (or (/= (char-after) ?
#)
3650 (not c-opt-cpp-prefix
)
3653 (progn (beginning-of-line)
3654 (looking-at "[ \t]*")
3657 (progn (backward-char)
3658 (not (eq (char-before) ?
\\)))))))
3661 ;; Use `condition-case' to avoid having to check for buffer
3662 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3667 (c-backward-syntactic-ws)
3669 (if (looking-at jump-syntax
)
3670 (goto-char (scan-sexps (1+ (point)) -
1))
3671 ;; This can be very inefficient if there's a long
3672 ;; sequence of operator tokens without any separation.
3673 ;; That doesn't happen in practice, anyway.
3674 (c-beginning-of-current-token))
3675 (>= (point) limit
)))
3678 (error (goto-char last
)))
3680 (if (< (point) limit
)
3685 (defun c-forward-token-1 (&optional count balanced limit
)
3686 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3687 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3688 characters are jumped over character by character. This function is
3689 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3690 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3691 (c-forward-token-2 count balanced limit
)))
3693 (defun c-backward-token-1 (&optional count balanced limit
)
3694 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3695 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3696 characters are jumped over character by character. This function is
3697 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3698 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3699 (c-backward-token-2 count balanced limit
)))
3702 ;; Tools for doing searches restricted to syntactically relevant text.
3704 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3705 paren-level not-inside-token
3706 lookbehind-submatch
)
3707 "Like `re-search-forward', but only report matches that are found
3708 in syntactically significant text. I.e. matches in comments, macros
3709 or string literals are ignored. The start point is assumed to be
3710 outside any comment, macro or string literal, or else the content of
3711 that region is taken as syntactically significant text.
3713 If PAREN-LEVEL is non-nil, an additional restriction is added to
3714 ignore matches in nested paren sexps. The search will also not go
3715 outside the current list sexp, which has the effect that if the point
3716 should be moved to BOUND when no match is found \(i.e. NOERROR is
3717 neither nil nor t), then it will be at the closing paren if the end of
3718 the current list sexp is encountered first.
3720 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3721 ignored. Things like multicharacter operators and special symbols
3722 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3725 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3726 subexpression in REGEXP. The end of that submatch is used as the
3727 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3728 isn't used or if that subexpression didn't match then the start
3729 position of the whole match is used instead. The \"look behind\"
3730 subexpression is never tested before the starting position, so it
3731 might be a good idea to include \\=\\= as a match alternative in it.
3733 Optimization note: Matches might be missed if the \"look behind\"
3734 subexpression can match the end of nonwhite syntactic whitespace,
3735 i.e. the end of comments or cpp directives. This since the function
3736 skips over such things before resuming the search. It's on the other
3737 hand not safe to assume that the \"look behind\" subexpression never
3738 matches syntactic whitespace.
3740 Bug: Unbalanced parens inside cpp directives are currently not handled
3741 correctly \(i.e. they don't get ignored as they should) when
3744 Note that this function might do hidden buffer changes. See the
3745 comment at the start of cc-engine.el for more info."
3747 (or bound
(setq bound
(point-max)))
3748 (if paren-level
(setq paren-level -
1))
3750 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3752 (let ((start (point))
3754 ;; Start position for the last search.
3756 ;; The `parse-partial-sexp' state between the start position
3759 ;; The current position after the last state update. The next
3760 ;; `parse-partial-sexp' continues from here.
3762 ;; The position at which to check the state and the state
3763 ;; there. This is separate from `state-pos' since we might
3764 ;; need to back up before doing the next search round.
3765 check-pos check-state
3766 ;; Last position known to end a token.
3767 (last-token-end-pos (point-min))
3768 ;; Set when a valid match is found.
3775 (setq search-pos
(point))
3776 (re-search-forward regexp bound noerror
))
3779 (setq state
(parse-partial-sexp
3780 state-pos
(match-beginning 0) paren-level nil state
)
3782 (if (setq check-pos
(and lookbehind-submatch
3783 (or (not paren-level
)
3785 (match-end lookbehind-submatch
)))
3786 (setq check-state
(parse-partial-sexp
3787 state-pos check-pos paren-level nil state
))
3788 (setq check-pos state-pos
3791 ;; NOTE: If we got a look behind subexpression and get
3792 ;; an insignificant match in something that isn't
3793 ;; syntactic whitespace (i.e. strings or in nested
3794 ;; parentheses), then we can never skip more than a
3795 ;; single character from the match start position
3796 ;; (i.e. `state-pos' here) before continuing the
3797 ;; search. That since the look behind subexpression
3798 ;; might match the end of the insignificant region in
3802 ((elt check-state
7)
3803 ;; Match inside a line comment. Skip to eol. Use
3804 ;; `re-search-forward' instead of `skip-chars-forward' to get
3805 ;; the right bound behavior.
3806 (re-search-forward "[\n\r]" bound noerror
))
3808 ((elt check-state
4)
3809 ;; Match inside a block comment. Skip to the '*/'.
3810 (search-forward "*/" bound noerror
))
3812 ((and (not (elt check-state
5))
3813 (eq (char-before check-pos
) ?
/)
3814 (not (c-get-char-property (1- check-pos
) 'syntax-table
))
3815 (memq (char-after check-pos
) '(?
/ ?
*)))
3816 ;; Match in the middle of the opener of a block or line
3818 (if (= (char-after check-pos
) ?
/)
3819 (re-search-forward "[\n\r]" bound noerror
)
3820 (search-forward "*/" bound noerror
)))
3822 ;; The last `parse-partial-sexp' above might have
3823 ;; stopped short of the real check position if the end
3824 ;; of the current sexp was encountered in paren-level
3825 ;; mode. The checks above are always false in that
3826 ;; case, and since they can do better skipping in
3827 ;; lookbehind-submatch mode, we do them before
3828 ;; checking the paren level.
3831 (/= (setq tmp
(car check-state
)) 0))
3832 ;; Check the paren level first since we're short of the
3833 ;; syntactic checking position if the end of the
3834 ;; current sexp was encountered by `parse-partial-sexp'.
3837 ;; Inside a nested paren sexp.
3838 (if lookbehind-submatch
3839 ;; See the NOTE above.
3840 (progn (goto-char state-pos
) t
)
3841 ;; Skip out of the paren quickly.
3842 (setq state
(parse-partial-sexp state-pos bound
0 nil state
)
3845 ;; Have exited the current paren sexp.
3848 ;; The last `parse-partial-sexp' call above
3849 ;; has left us just after the closing paren
3850 ;; in this case, so we can modify the bound
3851 ;; to leave the point at the right position
3853 (setq bound
(1- (point)))
3855 (signal 'search-failed
(list regexp
)))))
3857 ((setq tmp
(elt check-state
3))
3858 ;; Match inside a string.
3859 (if (or lookbehind-submatch
3860 (not (integerp tmp
)))
3861 ;; See the NOTE above.
3862 (progn (goto-char state-pos
) t
)
3863 ;; Skip to the end of the string before continuing.
3864 (let ((ender (make-string 1 tmp
)) (continue t
))
3865 (while (if (search-forward ender bound noerror
)
3867 (setq state
(parse-partial-sexp
3868 state-pos
(point) nil nil state
)
3871 (setq continue nil
)))
3876 (c-beginning-of-macro start
)))
3877 ;; Match inside a macro. Skip to the end of it.
3879 (cond ((<= (point) bound
) t
)
3881 (t (signal 'search-failed
(list regexp
)))))
3883 ((and not-inside-token
3884 (or (< check-pos last-token-end-pos
)
3887 (goto-char check-pos
)
3889 (c-end-of-current-token last-token-end-pos
))
3890 (setq last-token-end-pos
(point))))))
3892 (if lookbehind-submatch
3893 ;; See the NOTE above.
3894 (goto-char state-pos
)
3895 (goto-char (min last-token-end-pos bound
))))
3902 ;; Should loop to search again, but take care to avoid
3903 ;; looping on the same spot.
3904 (or (/= search-pos
(point))
3905 (if (= (point) bound
)
3908 (signal 'search-failed
(list regexp
)))
3914 (signal (car err
) (cdr err
))))
3916 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3920 (goto-char (match-end 0))
3923 ;; Search failed. Set point as appropriate.
3929 (defvar safe-pos-list
) ; bound in c-syntactic-skip-backward
3931 (defsubst c-ssb-lit-begin
()
3932 ;; Return the start of the literal point is in, or nil.
3933 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3934 ;; bound in the caller.
3936 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3937 ;; if it's outside comments and strings.
3939 (let ((pos (point)) safe-pos state pps-end-pos
)
3940 ;; Pick a safe position as close to the point as possible.
3942 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3945 (while (and safe-pos-list
3946 (> (car safe-pos-list
) (point)))
3947 (setq safe-pos-list
(cdr safe-pos-list
)))
3948 (unless (setq safe-pos
(car-safe safe-pos-list
))
3949 (setq safe-pos
(max (or (c-safe-position
3950 (point) (or c-state-cache
3954 safe-pos-list
(list safe-pos
)))
3956 ;; Cache positions along the way to use if we have to back up more. We
3957 ;; cache every closing paren on the same level. If the paren cache is
3958 ;; relevant in this region then we're typically already on the same
3959 ;; level as the target position. Note that we might cache positions
3960 ;; after opening parens in case safe-pos is in a nested list. That's
3961 ;; both uncommon and harmless.
3963 (setq state
(parse-partial-sexp
3966 (setq safe-pos
(point)
3967 safe-pos-list
(cons safe-pos safe-pos-list
)))
3969 ;; If the state contains the start of the containing sexp we cache that
3970 ;; position too, so that parse-partial-sexp in the next run has a bigger
3971 ;; chance of starting at the same level as the target position and thus
3972 ;; will get more good safe positions into the list.
3974 (setq safe-pos
(1+ (elt state
1))
3975 safe-pos-list
(cons safe-pos safe-pos-list
)))
3977 (if (or (elt state
3) (elt state
4))
3978 ;; Inside string or comment. Continue search at the
3982 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level
)
3983 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3984 i.e. don't stop at positions inside syntactic whitespace or string
3985 literals. Preprocessor directives are also ignored, with the exception
3986 of the one that the point starts within, if any. If LIMIT is given,
3987 it's assumed to be at a syntactically relevant position.
3989 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3990 sexps, and the search will also not go outside the current paren sexp.
3991 However, if LIMIT or the buffer limit is reached inside a nested paren
3992 then the point will be left at the limit.
3994 Non-nil is returned if the point moved, nil otherwise.
3996 Note that this function might do hidden buffer changes. See the
3997 comment at the start of cc-engine.el for more info."
3999 (let ((start (point))
4001 ;; A list of syntactically relevant positions in descending
4002 ;; order. It's used to avoid scanning repeatedly over
4003 ;; potentially large regions with `parse-partial-sexp' to verify
4004 ;; each position. Used in `c-ssb-lit-begin'
4006 ;; The result from `c-beginning-of-macro' at the start position or the
4007 ;; start position itself if it isn't within a macro. Evaluated on
4010 ;; The earliest position after the current one with the same paren
4011 ;; level. Used only when `paren-level' is set.
4013 (paren-level-pos (point)))
4017 ;; The next loop "tries" to find the end point each time round,
4018 ;; loops when it hasn't succeeded.
4021 (< (skip-chars-backward skip-chars limit
) 0)
4023 (let ((pos (point)) state-2 pps-end-pos
)
4026 ;; Don't stop inside a literal
4027 ((setq lit-beg
(c-ssb-lit-begin))
4033 (setq state-2
(parse-partial-sexp
4034 pos paren-level-pos -
1)
4035 pps-end-pos
(point))
4036 (/= (car state-2
) 0)))
4037 ;; Not at the right level.
4039 (if (and (< (car state-2
) 0)
4040 ;; We stop above if we go out of a paren.
4041 ;; Now check whether it precedes or is
4042 ;; nested in the starting sexp.
4046 pps-end-pos paren-level-pos
4048 (< (car state-2
) 0)))
4050 ;; We've stopped short of the starting position
4051 ;; so the hit was inside a nested list. Go up
4052 ;; until we are at the right level.
4055 (goto-char (scan-lists pos -
1
4057 (setq paren-level-pos
(point))
4058 (if (and limit
(>= limit paren-level-pos
))
4064 (goto-char (or limit
(point-min)))
4067 ;; The hit was outside the list at the start
4068 ;; position. Go to the start of the list and exit.
4069 (goto-char (1+ (elt state-2
1)))
4072 ((c-beginning-of-macro limit
)
4076 (setq start-macro-beg
4079 (c-beginning-of-macro limit
)
4083 ;; It's inside the same macro we started in so it's
4084 ;; a relevant match.
4090 ;; Skip syntactic ws afterwards so that we don't stop at the
4091 ;; end of a comment if `skip-chars' is something like "^/".
4092 (c-backward-syntactic-ws)
4095 ;; We might want to extend this with more useful return values in
4097 (/= (point) start
)))
4099 ;; The following is an alternative implementation of
4100 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4101 ;; track of the syntactic context. It turned out to be generally
4102 ;; slower than the one above which uses forward checks from earlier
4105 ;;(defconst c-ssb-stop-re
4106 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4107 ;; ;; stop at to avoid going into comments and literals.
4109 ;; ;; Match comment end syntax and string literal syntax. Also match
4110 ;; ;; '/' for block comment endings (not covered by comment end
4112 ;; "\\s>\\|/\\|\\s\""
4113 ;; (if (memq 'gen-string-delim c-emacs-features)
4116 ;; (if (memq 'gen-comment-delim c-emacs-features)
4120 ;;(defconst c-ssb-stop-paren-re
4121 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4122 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4124 ;;(defconst c-ssb-sexp-end-re
4125 ;; ;; Regexp matching the ending syntax of a complex sexp.
4126 ;; (concat c-string-limit-regexp "\\|\\s)"))
4128 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4129 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4130 ;;i.e. don't stop at positions inside syntactic whitespace or string
4131 ;;literals. Preprocessor directives are also ignored. However, if the
4132 ;;point is within a comment, string literal or preprocessor directory to
4133 ;;begin with, its contents is treated as syntactically relevant chars.
4134 ;;If LIMIT is given, it limits the backward search and the point will be
4135 ;;left there if no earlier position is found.
4137 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4138 ;;sexps, and the search will also not go outside the current paren sexp.
4139 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4140 ;;then the point will be left at the limit.
4142 ;;Non-nil is returned if the point moved, nil otherwise.
4144 ;;Note that this function might do hidden buffer changes. See the
4145 ;;comment at the start of cc-engine.el for more info."
4147 ;; (save-restriction
4149 ;; (narrow-to-region limit (point-max)))
4151 ;; (let ((start (point)))
4153 ;; (while (let ((last-pos (point))
4155 ;; (skip-chars-backward skip-chars)
4158 ;; ;; Skip back over the same region as
4159 ;; ;; `skip-chars-backward' above, but keep to
4160 ;; ;; syntactically relevant positions.
4161 ;; (goto-char last-pos)
4163 ;; ;; `re-search-backward' with a single char regexp
4164 ;; ;; should be fast.
4165 ;; (re-search-backward
4166 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4171 ;; ((looking-at "\\s(")
4172 ;; ;; `paren-level' is set and we've found the
4173 ;; ;; start of the containing paren.
4177 ;; ((looking-at c-ssb-sexp-end-re)
4178 ;; ;; We're at the end of a string literal or paren
4179 ;; ;; sexp (if `paren-level' is set).
4181 ;; (condition-case nil
4182 ;; (c-backward-sexp)
4184 ;; (goto-char limit)
4185 ;; (throw 'done t))))
4189 ;; ;; At the end of some syntactic ws or possibly
4190 ;; ;; after a plain '/' operator.
4191 ;; (let ((pos (point)))
4192 ;; (c-backward-syntactic-ws)
4193 ;; (if (= pos (point))
4194 ;; ;; Was a plain '/' operator. Go past it.
4195 ;; (backward-char)))))
4197 ;; (> (point) stop-pos))))
4199 ;; ;; Now the point is either at `stop-pos' or at some
4200 ;; ;; position further back if `stop-pos' was at a
4201 ;; ;; syntactically irrelevant place.
4203 ;; ;; Skip additional syntactic ws so that we don't stop
4204 ;; ;; at the end of a comment if `skip-chars' is
4205 ;; ;; something like "^/".
4206 ;; (c-backward-syntactic-ws)
4208 ;; (< (point) stop-pos))))
4210 ;; ;; We might want to extend this with more useful return values
4211 ;; ;; in the future.
4212 ;; (/= (point) start))))
4215 ;; Tools for handling comments and string literals.
4217 (defun c-in-literal (&optional lim detect-cpp
)
4218 "Return the type of literal point is in, if any.
4219 The return value is `c' if in a C-style comment, `c++' if in a C++
4220 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4221 is non-nil and in a preprocessor line, or nil if somewhere else.
4222 Optional LIM is used as the backward limit of the search. If omitted,
4223 or nil, `c-beginning-of-defun' is used.
4225 The last point calculated is cached if the cache is enabled, i.e. if
4226 `c-in-literal-cache' is bound to a two element vector.
4228 Note that this function might do hidden buffer changes. See the
4229 comment at the start of cc-engine.el for more info."
4232 (let* ((safe-place (c-state-safe-place (point)))
4233 (lit (c-state-pp-to-literal safe-place
(point))))
4236 (save-excursion (c-beginning-of-macro))
4239 (defun c-literal-limits (&optional lim near not-in-delimiter
)
4240 "Return a cons of the beginning and end positions of the comment or
4241 string surrounding point (including both delimiters), or nil if point
4242 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4243 to start parsing from. If NEAR is non-nil, then the limits of any
4244 literal next to point is returned. \"Next to\" means there's only
4245 spaces and tabs between point and the literal. The search for such a
4246 literal is done first in forward direction. If NOT-IN-DELIMITER is
4247 non-nil, the case when point is inside a starting delimiter won't be
4248 recognized. This only has effect for comments which have starting
4249 delimiters with more than one character.
4251 Note that this function might do hidden buffer changes. See the
4252 comment at the start of cc-engine.el for more info."
4255 (let* ((pos (point))
4256 (lim (or lim
(c-state-safe-place pos
)))
4257 (pp-to-lit (save-restriction
4259 (c-state-pp-to-literal lim pos
)))
4260 (state (car pp-to-lit
))
4261 (lit-limits (car (cddr pp-to-lit
))))
4265 ((and (not not-in-delimiter
)
4267 (eq (char-before) ?
/)
4268 (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28.
4269 ;; We're standing in a comment starter.
4271 (cons (point) (progn (c-forward-single-comment) (point))))
4275 ;; Search forward for a literal.
4276 (skip-chars-forward " \t")
4278 ((looking-at c-string-limit-regexp
) ; String.
4279 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4282 ((looking-at c-comment-start-regexp
) ; Line or block comment.
4283 (cons (point) (progn (c-forward-single-comment) (point))))
4287 (skip-chars-backward " \t")
4289 (let ((end (point)) beg
)
4292 (< (skip-syntax-backward c-string-syntax
) 0)) ; String.
4293 (setq beg
(c-safe (c-backward-sexp 1) (point))))
4295 ((and (c-safe (forward-char -
2) t
)
4297 ;; Block comment. Due to the nature of line
4298 ;; comments, they will always be covered by the
4299 ;; normal case above.
4301 (c-backward-single-comment)
4302 ;; If LIM is bogus, beg will be bogus.
4303 (setq beg
(point))))
4305 (if beg
(cons beg end
))))))
4308 ;; In case external callers use this; it did have a docstring.
4309 (defalias 'c-literal-limits-fast
'c-literal-limits
)
4311 (defun c-collect-line-comments (range)
4312 "If the argument is a cons of two buffer positions (such as returned by
4313 `c-literal-limits'), and that range contains a C++ style line comment,
4314 then an extended range is returned that contains all adjacent line
4315 comments (i.e. all comments that starts in the same column with no
4316 empty lines or non-whitespace characters between them). Otherwise the
4317 argument is returned.
4319 Note that this function might do hidden buffer changes. See the
4320 comment at the start of cc-engine.el for more info."
4324 (if (and (consp range
) (progn
4325 (goto-char (car range
))
4326 (looking-at c-line-comment-starter
)))
4327 (let ((col (current-column))
4329 (bopl (c-point 'bopl
))
4331 ;; Got to take care in the backward direction to handle
4332 ;; comments which are preceded by code.
4333 (while (and (c-backward-single-comment)
4335 (looking-at c-line-comment-starter
)
4336 (= col
(current-column)))
4338 bopl
(c-point 'bopl
)))
4340 (while (and (progn (skip-chars-forward " \t")
4341 (looking-at c-line-comment-starter
))
4342 (= col
(current-column))
4343 (prog1 (zerop (forward-line 1))
4344 (setq end
(point)))))
4349 (defun c-literal-type (range)
4350 "Convenience function that given the result of `c-literal-limits',
4351 returns nil or the type of literal that the range surrounds, one
4352 of the symbols 'c, 'c++ or 'string. It's much faster than using
4353 `c-in-literal' and is intended to be used when you need both the
4354 type of a literal and its limits.
4356 Note that this function might do hidden buffer changes. See the
4357 comment at the start of cc-engine.el for more info."
4361 (goto-char (car range
))
4362 (cond ((looking-at c-string-limit-regexp
) 'string
)
4363 ((or (looking-at "//") ; c++ line comment
4364 (and (looking-at "\\s<") ; comment starter
4365 (looking-at "#"))) ; awk comment.
4367 (t 'c
))) ; Assuming the range is valid.
4370 (defsubst c-determine-limit-get-base
(start try-size
)
4371 ;; Get a "safe place" approximately TRY-SIZE characters before START.
4372 ;; This doesn't preserve point.
4373 (let* ((pos (max (- start try-size
) (point-min)))
4374 (base (c-state-safe-place pos
))
4375 (s (parse-partial-sexp base pos
)))
4376 (if (or (nth 4 s
) (nth 3 s
)) ; comment or string
4380 (defun c-determine-limit (how-far-back &optional start try-size
)
4381 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
4382 ;; (default point). This is done by going back further in the buffer then
4383 ;; searching forward for literals. The position found won't be in a
4384 ;; literal. We start searching for the sought position TRY-SIZE (default
4385 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
4388 (let* ((start (or start
(point)))
4389 (try-size (or try-size
(* 2 how-far-back
)))
4390 (base (c-determine-limit-get-base start try-size
))
4393 (s (parse-partial-sexp pos pos
)) ; null state.
4396 (while (< pos start
)
4397 ;; Move forward one literal each time round this loop.
4398 ;; Move forward to the start of a comment or string.
4399 (setq s
(parse-partial-sexp
4405 'syntax-table
)) ; stop-comment
4407 ;; Gather details of the non-literal-bit - starting pos and size.
4408 (setq size
(- (if (or (nth 4 s
) (nth 3 s
))
4413 (setq stack
(cons (cons pos size
) stack
)))
4415 ;; Move forward to the end of the comment/string.
4416 (if (or (nth 4 s
) (nth 3 s
))
4417 (setq s
(parse-partial-sexp
4423 'syntax-table
))) ; stop-comment
4426 ;; Now try and find enough non-literal characters recorded on the stack.
4427 ;; Go back one recorded literal each time round this loop.
4428 (while (and (< count how-far-back
)
4430 (setq elt
(car stack
)
4432 (setq count
(+ count
(cdr elt
))))
4434 ;; Have we found enough yet?
4436 ((>= count how-far-back
)
4437 (+ (car elt
) (- count how-far-back
)))
4438 ((eq base
(point-min))
4441 (c-determine-limit (- how-far-back count
) base try-size
))))))
4443 ;; `c-find-decl-spots' and accompanying stuff.
4445 ;; Variables used in `c-find-decl-spots' to cache the search done for
4446 ;; the first declaration in the last call. When that function starts,
4447 ;; it needs to back up over syntactic whitespace to look at the last
4448 ;; token before the region being searched. That can sometimes cause
4449 ;; moves back and forth over a quite large region of comments and
4450 ;; macros, which would be repeated for each changed character when
4451 ;; we're called during fontification, since font-lock refontifies the
4452 ;; current line for each change. Thus it's worthwhile to cache the
4455 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4456 ;; the syntactic whitespace less or equal to some start position.
4457 ;; There's no cached value if it's nil.
4459 ;; `c-find-decl-match-pos' is the match position if
4460 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4461 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4462 (defvar c-find-decl-syntactic-pos nil
)
4463 (make-variable-buffer-local 'c-find-decl-syntactic-pos
)
4464 (defvar c-find-decl-match-pos nil
)
4465 (make-variable-buffer-local 'c-find-decl-match-pos
)
4467 (defsubst c-invalidate-find-decl-cache
(change-min-pos)
4468 (and c-find-decl-syntactic-pos
4469 (< change-min-pos c-find-decl-syntactic-pos
)
4470 (setq c-find-decl-syntactic-pos nil
)))
4472 ; (defface c-debug-decl-spot-face
4473 ; '((t (:background "Turquoise")))
4474 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4475 ; (defface c-debug-decl-sws-face
4476 ; '((t (:background "Khaki")))
4477 ; "Debug face to mark the syntactic whitespace between the declaration
4478 ; spots and the preceding token end.")
4480 (defmacro c-debug-put-decl-spot-faces
(match-pos decl-pos
)
4481 (when (facep 'c-debug-decl-spot-face
)
4482 `(c-save-buffer-state ((match-pos ,match-pos
) (decl-pos ,decl-pos
))
4483 (c-debug-add-face (max match-pos
(point-min)) decl-pos
4484 'c-debug-decl-sws-face
)
4485 (c-debug-add-face decl-pos
(min (1+ decl-pos
) (point-max))
4486 'c-debug-decl-spot-face
))))
4487 (defmacro c-debug-remove-decl-spot-faces
(beg end
)
4488 (when (facep 'c-debug-decl-spot-face
)
4489 `(c-save-buffer-state ()
4490 (c-debug-remove-face ,beg
,end
'c-debug-decl-spot-face
)
4491 (c-debug-remove-face ,beg
,end
'c-debug-decl-sws-face
))))
4493 (defmacro c-find-decl-prefix-search
()
4494 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4495 ;; but it contains lots of free variables that refer to things
4496 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4497 ;; if there is a match, otherwise at `cfd-limit'.
4499 ;; This macro might do hidden buffer changes.
4502 ;; Find the next property match position if we haven't got one already.
4503 (unless cfd-prop-match
4506 (goto-char (next-single-property-change
4507 (point) 'c-type nil cfd-limit
))
4508 (and (< (point) cfd-limit
)
4509 (not (eq (c-get-char-property (1- (point)) 'c-type
)
4511 (setq cfd-prop-match
(point))))
4513 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4515 (unless cfd-re-match
4517 (if (> cfd-re-match-end
(point))
4518 (goto-char cfd-re-match-end
))
4520 (while (if (setq cfd-re-match-end
4521 (re-search-forward c-decl-prefix-or-start-re
4524 ;; Match. Check if it's inside a comment or string literal.
4526 (if (setq cfd-re-match
(match-end 1))
4527 ;; Matched the end of a token preceding a decl spot.
4529 (goto-char cfd-re-match
)
4531 ;; Matched a token that start a decl spot.
4532 (goto-char (match-beginning 0))
4536 ;; No match. Finish up and exit the loop.
4537 (setq cfd-re-match cfd-limit
)
4540 ;; Skip out of comments and string literals.
4542 (goto-char (next-single-property-change
4543 (point) 'face nil cfd-limit
))
4544 (and (< (point) cfd-limit
)
4545 (c-got-face-at (point) c-literal-faces
)))))
4547 ;; If we matched at the decl start, we have to back up over the
4548 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4549 ;; any decl spots in the syntactic ws.
4550 (unless cfd-re-match
4551 (c-backward-syntactic-ws)
4552 (setq cfd-re-match
(point))))
4554 ;; Choose whichever match is closer to the start.
4555 (if (< cfd-re-match cfd-prop-match
)
4556 (setq cfd-match-pos cfd-re-match
4558 (setq cfd-match-pos cfd-prop-match
4559 cfd-prop-match nil
))
4561 (goto-char cfd-match-pos
)
4563 (when (< cfd-match-pos cfd-limit
)
4564 ;; Skip forward past comments only so we don't skip macros.
4565 (c-forward-comments)
4566 ;; Set the position to continue at. We can avoid going over
4567 ;; the comments skipped above a second time, but it's possible
4568 ;; that the comment skipping has taken us past `cfd-prop-match'
4569 ;; since the property might be used inside comments.
4570 (setq cfd-continue-pos
(if cfd-prop-match
4571 (min cfd-prop-match
(point))
4574 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun
)
4575 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4576 ;; label from the point to CFD-LIMIT.
4578 ;; CFD-FUN is called with point at the start of the spot. It's passed two
4579 ;; arguments: The first is the end position of the token preceding the spot,
4580 ;; or 0 for the implicit match at bob. The second is a flag that is t when
4581 ;; the match is inside a macro. Point should be moved forward by at least
4584 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
4585 ;; it should return non-nil to ensure that the next search will find them.
4588 ;; o The first token after bob.
4589 ;; o The first token after the end of submatch 1 in
4590 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4591 ;; o The start of each `c-decl-prefix-or-start-re' match when
4592 ;; submatch 1 doesn't match.
4593 ;; o The first token after the end of each occurrence of the
4594 ;; `c-type' text property with the value `c-decl-end', provided
4595 ;; `c-type-decl-end-used' is set.
4597 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4598 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4599 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4601 ;; If the match is inside a macro then the buffer is narrowed to the
4602 ;; end of it, so that CFD-FUN can investigate the following tokens
4603 ;; without matching something that begins inside a macro and ends
4604 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4605 ;; CFD-FACE-CHECKLIST checks exist.
4607 ;; The spots are visited approximately in order from top to bottom.
4608 ;; It's however the positions where `c-decl-prefix-or-start-re'
4609 ;; matches and where `c-decl-end' properties are found that are in
4610 ;; order. Since the spots often are at the following token, they
4611 ;; might be visited out of order insofar as more spots are reported
4612 ;; later on within the syntactic whitespace between the match
4613 ;; positions and their spots.
4615 ;; It's assumed that comments and strings are fontified in the
4618 ;; This is mainly used in fontification, and so has an elaborate
4619 ;; cache to handle repeated calls from the same start position; see
4620 ;; the variables above.
4622 ;; All variables in this function begin with `cfd-' to avoid name
4623 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4625 ;; This function might do hidden buffer changes.
4627 (let ((cfd-start-pos (point))
4628 (cfd-buffer-end (point-max))
4629 ;; The end of the token preceding the decl spot last found
4630 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4633 ;; The end position of the last `c-decl-prefix-or-start-re'
4634 ;; match. If this is greater than `cfd-continue-pos', the
4635 ;; next regexp search is started here instead.
4636 (cfd-re-match-end (point-min))
4637 ;; The end of the last `c-decl-end' found by
4638 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4639 ;; match. If searching for the property isn't needed then we
4640 ;; disable it by setting it to `cfd-limit' directly.
4641 (cfd-prop-match (unless c-type-decl-end-used cfd-limit
))
4642 ;; The end of the token preceding the decl spot last found by
4643 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4644 ;; bob. `cfd-limit' if there's no match. In other words,
4645 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4646 (cfd-match-pos cfd-limit
)
4647 ;; The position to continue searching at.
4649 ;; The position of the last "real" token we've stopped at.
4650 ;; This can be greater than `cfd-continue-pos' when we get
4651 ;; hits inside macros or at `c-decl-end' positions inside
4654 ;; The end position of the last entered macro.
4657 ;; Initialize by finding a syntactically relevant start position
4658 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4659 ;; search unless we're at bob.
4661 (let (start-in-literal start-in-macro syntactic-pos
)
4662 ;; Must back up a bit since we look for the end of the previous
4663 ;; statement or declaration, which is earlier than the first
4667 ;; First we need to move to a syntactically relevant position.
4668 ;; Begin by backing out of comment or string literals.
4670 (when (c-got-face-at (point) c-literal-faces
)
4671 ;; Try to use the faces to back up to the start of the
4672 ;; literal. FIXME: What if the point is on a declaration
4673 ;; inside a comment?
4674 (while (and (not (bobp))
4675 (c-got-face-at (1- (point)) c-literal-faces
))
4676 (goto-char (previous-single-property-change
4677 (point) 'face nil
(point-min))))
4679 ;; XEmacs doesn't fontify the quotes surrounding string
4681 (and (featurep 'xemacs
)
4682 (eq (get-text-property (point) 'face
)
4683 'font-lock-string-face
)
4685 (progn (backward-char)
4686 (not (looking-at c-string-limit-regexp
)))
4689 ;; Don't trust the literal to contain only literal faces
4690 ;; (the font lock package might not have fontified the
4691 ;; start of it at all, for instance) so check that we have
4692 ;; arrived at something that looks like a start or else
4693 ;; resort to `c-literal-limits'.
4694 (unless (looking-at c-literal-start-regexp
)
4695 (let ((range (c-literal-limits)))
4696 (if range
(goto-char (car range
)))))
4698 (setq start-in-literal
(point)))
4700 ;; The start is in a literal. If the limit is in the same
4701 ;; one we don't have to find a syntactic position etc. We
4702 ;; only check that if the limit is at or before bonl to save
4703 ;; time; it covers the by far most common case when font-lock
4704 ;; refontifies the current line only.
4705 (<= cfd-limit
(c-point 'bonl cfd-start-pos
))
4707 (goto-char cfd-start-pos
)
4709 (goto-char (next-single-property-change
4710 (point) 'face nil cfd-limit
))
4711 (and (< (point) cfd-limit
)
4712 (c-got-face-at (point) c-literal-faces
))))
4713 (= (point) cfd-limit
)))
4715 ;; Completely inside a literal. Set up variables to trig the
4716 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4717 ;; find a suitable start position.
4718 (setq cfd-continue-pos start-in-literal
))
4720 ;; Check if the region might be completely inside a macro, to
4721 ;; optimize that like the completely-inside-literal above.
4723 (and (= (forward-line 1) 0)
4724 (bolp) ; forward-line has funny behavior at eob.
4725 (>= (point) cfd-limit
)
4726 (progn (backward-char)
4727 (eq (char-before) ?
\\))))
4728 ;; (Maybe) completely inside a macro. Only need to trig the
4729 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4731 (setq cfd-continue-pos
(1- cfd-start-pos
)
4735 ;; Back out of any macro so we don't miss any declaration
4736 ;; that could follow after it.
4737 (when (c-beginning-of-macro)
4738 (setq start-in-macro t
))
4740 ;; Now we're at a proper syntactically relevant position so we
4741 ;; can use the cache. But first clear it if it applied
4743 (c-invalidate-find-decl-cache cfd-start-pos
)
4745 (setq syntactic-pos
(point))
4746 (unless (eq syntactic-pos c-find-decl-syntactic-pos
)
4747 ;; Don't have to do this if the cache is relevant here,
4748 ;; typically if the same line is refontified again. If
4749 ;; we're just some syntactic whitespace further down we can
4750 ;; still use the cache to limit the skipping.
4751 (c-backward-syntactic-ws c-find-decl-syntactic-pos
))
4753 ;; If we hit `c-find-decl-syntactic-pos' and
4754 ;; `c-find-decl-match-pos' is set then we install the cached
4755 ;; values. If we hit `c-find-decl-syntactic-pos' and
4756 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4757 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4758 ;; and so we can continue the search from this point. If we
4759 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4760 ;; the right spot to begin searching anyway.
4761 (if (and (eq (point) c-find-decl-syntactic-pos
)
4762 c-find-decl-match-pos
)
4763 (setq cfd-match-pos c-find-decl-match-pos
4764 cfd-continue-pos syntactic-pos
)
4766 (setq c-find-decl-syntactic-pos syntactic-pos
)
4769 ;; Always consider bob a match to get the first
4770 ;; declaration in the file. Do this separately instead of
4771 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4772 ;; regexp always can consume at least one character to
4773 ;; ensure that we won't get stuck in an infinite loop.
4774 (setq cfd-re-match
0)
4776 (c-beginning-of-current-token)
4777 (< (point) cfd-limit
))
4778 ;; Do an initial search now. In the bob case above it's
4779 ;; only done to search for a `c-decl-end' spot.
4780 (c-find-decl-prefix-search))
4782 (setq c-find-decl-match-pos
(and (< cfd-match-pos cfd-start-pos
)
4785 ;; Advance `cfd-continue-pos' if it's before the start position.
4786 ;; The closest continue position that might have effect at or
4787 ;; after the start depends on what we started in. This also
4788 ;; finds a suitable start position in the special cases when the
4789 ;; region is completely within a literal or macro.
4790 (when (and cfd-continue-pos
(< cfd-continue-pos cfd-start-pos
))
4794 ;; If we're in a macro then it's the closest preceding token
4795 ;; in the macro. Check this before `start-in-literal',
4796 ;; since if we're inside a literal in a macro, the preceding
4797 ;; token is earlier than any `c-decl-end' spot inside the
4798 ;; literal (comment).
4799 (goto-char (or start-in-literal cfd-start-pos
))
4800 ;; The only syntactic ws in macros are comments.
4801 (c-backward-comments)
4803 (c-beginning-of-current-token))
4806 ;; If we're in a comment it can only be the closest
4807 ;; preceding `c-decl-end' position within that comment, if
4808 ;; any. Go back to the beginning of such a property so that
4809 ;; `c-find-decl-prefix-search' will find the end of it.
4810 ;; (Can't stop at the end and install it directly on
4811 ;; `cfd-prop-match' since that variable might be cleared
4812 ;; after `cfd-fun' below.)
4814 ;; Note that if the literal is a string then the property
4815 ;; search will simply skip to the beginning of it right
4817 (if (not c-type-decl-end-used
)
4818 (goto-char start-in-literal
)
4819 (goto-char cfd-start-pos
)
4821 (goto-char (previous-single-property-change
4822 (point) 'c-type nil start-in-literal
))
4823 (and (> (point) start-in-literal
)
4824 (not (eq (c-get-char-property (point) 'c-type
)
4827 (when (= (point) start-in-literal
)
4828 ;; Didn't find any property inside the comment, so we can
4829 ;; skip it entirely. (This won't skip past a string, but
4830 ;; that'll be handled quickly by the next
4831 ;; `c-find-decl-prefix-search' anyway.)
4832 (c-forward-single-comment)
4833 (if (> (point) cfd-limit
)
4834 (goto-char cfd-limit
))))
4837 ;; If we started in normal code, the only match that might
4838 ;; apply before the start is what we already got in
4839 ;; `cfd-match-pos' so we can continue at the start position.
4840 ;; (Note that we don't get here if the first match is below
4842 (goto-char cfd-start-pos
)))
4844 ;; Delete found matches if they are before our new continue
4845 ;; position, so that `c-find-decl-prefix-search' won't back up
4846 ;; to them later on.
4847 (setq cfd-continue-pos
(point))
4848 (when (and cfd-re-match
(< cfd-re-match cfd-continue-pos
))
4849 (setq cfd-re-match nil
))
4850 (when (and cfd-prop-match
(< cfd-prop-match cfd-continue-pos
))
4851 (setq cfd-prop-match nil
)))
4854 ;; This is the normal case and we got a proper syntactic
4855 ;; position. If there's a match then it's always outside
4856 ;; macros and comments, so advance to the next token and set
4857 ;; `cfd-token-pos'. The loop below will later go back using
4858 ;; `cfd-continue-pos' to fix declarations inside the
4860 (when (and cfd-match-pos
(< cfd-match-pos syntactic-pos
))
4861 (goto-char syntactic-pos
)
4862 (c-forward-syntactic-ws)
4863 (and cfd-continue-pos
4864 (< cfd-continue-pos
(point))
4865 (setq cfd-token-pos
(point))))
4867 ;; Have one of the special cases when the region is completely
4868 ;; within a literal or macro. `cfd-continue-pos' is set to a
4869 ;; good start position for the search, so do it.
4870 (c-find-decl-prefix-search)))
4872 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4876 (< cfd-match-pos cfd-limit
)
4879 ;; Kludge to filter out matches on the "<" that
4880 ;; aren't open parens, for the sake of languages
4881 ;; that got `c-recognize-<>-arglists' set.
4882 (and (eq (char-before cfd-match-pos
) ?
<)
4883 (not (c-get-char-property (1- cfd-match-pos
)
4886 ;; If `cfd-continue-pos' is less or equal to
4887 ;; `cfd-token-pos', we've got a hit inside a macro
4888 ;; that's in the syntactic whitespace before the last
4889 ;; "real" declaration we've checked. If they're equal
4890 ;; we've arrived at the declaration a second time, so
4891 ;; there's nothing to do.
4892 (= cfd-continue-pos cfd-token-pos
)
4895 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4896 ;; we're still searching for declarations embedded in
4897 ;; the syntactic whitespace. In that case we need
4898 ;; only to skip comments and not macros, since they
4899 ;; can't be nested, and that's already been done in
4900 ;; `c-find-decl-prefix-search'.
4901 (when (> cfd-continue-pos cfd-token-pos
)
4902 (c-forward-syntactic-ws)
4903 (setq cfd-token-pos
(point)))
4905 ;; Continue if the following token fails the
4906 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4907 (when (or (>= (point) cfd-limit
)
4908 (not (looking-at cfd-decl-re
))
4909 (and cfd-face-checklist
4911 (point) cfd-face-checklist
))))
4912 (goto-char cfd-continue-pos
)
4915 (< (point) cfd-limit
))
4916 (c-find-decl-prefix-search))
4918 (< (point) cfd-limit
))
4921 (>= (point) cfd-start-pos
)
4924 ;; Narrow to the end of the macro if we got a hit inside
4925 ;; one, to avoid recognizing things that start inside the
4926 ;; macro and end outside it.
4927 (when (> cfd-match-pos cfd-macro-end
)
4928 ;; Not in the same macro as in the previous round.
4930 (goto-char cfd-match-pos
)
4932 (if (save-excursion (and (c-beginning-of-macro)
4933 (< (point) cfd-match-pos
)))
4934 (progn (c-end-of-macro)
4938 (if (zerop cfd-macro-end
)
4940 (if (> cfd-macro-end
(point))
4941 (progn (narrow-to-region (point-min) cfd-macro-end
)
4943 ;; The matched token was the last thing in the macro,
4944 ;; so the whole match is bogus.
4945 (setq cfd-macro-end
0)
4948 (c-debug-put-decl-spot-faces cfd-match-pos
(point))
4949 (if (funcall cfd-fun cfd-match-pos
(/= cfd-macro-end
0))
4950 (setq cfd-prop-match nil
))
4952 (when (/= cfd-macro-end
0)
4953 ;; Restore limits if we did macro narrowing above.
4954 (narrow-to-region (point-min) cfd-buffer-end
)))
4956 (goto-char cfd-continue-pos
)
4957 (if (= cfd-continue-pos cfd-limit
)
4958 (setq cfd-match-pos cfd-limit
)
4959 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
4960 ; cfd-match-pos, etc.
4963 ;; A cache for found types.
4965 ;; Buffer local variable that contains an obarray with the types we've
4966 ;; found. If a declaration is recognized somewhere we record the
4967 ;; fully qualified identifier in it to recognize it as a type
4968 ;; elsewhere in the file too. This is not accurate since we do not
4969 ;; bother with the scoping rules of the languages, but in practice the
4970 ;; same name is seldom used as both a type and something else in a
4971 ;; file, and we only use this as a last resort in ambiguous cases (see
4972 ;; `c-forward-decl-or-cast-1').
4974 ;; Not every type need be in this cache. However, things which have
4975 ;; ceased to be types must be removed from it.
4977 ;; Template types in C++ are added here too but with the template
4978 ;; arglist replaced with "<>" in references or "<" for the one in the
4979 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4980 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4981 ;; template specs can be fairly sized programs in themselves) and
4982 ;; improves the hit ratio (it's a type regardless of the template
4983 ;; args; it's just not the same type, but we're only interested in
4984 ;; recognizing types, not telling distinct types apart). Note that
4985 ;; template types in references are added here too; from the example
4986 ;; above there will also be an entry "Foo<".
4987 (defvar c-found-types nil
)
4988 (make-variable-buffer-local 'c-found-types
)
4990 (defsubst c-clear-found-types
()
4991 ;; Clears `c-found-types'.
4992 (setq c-found-types
(make-vector 53 0)))
4994 (defun c-add-type (from to
)
4995 ;; Add the given region as a type in `c-found-types'. If the region
4996 ;; doesn't match an existing type but there is a type which is equal
4997 ;; to the given one except that the last character is missing, then
4998 ;; the shorter type is removed. That's done to avoid adding all
4999 ;; prefixes of a type as it's being entered and font locked. This
5000 ;; doesn't cover cases like when characters are removed from a type
5001 ;; or added in the middle. We'd need the position of point when the
5002 ;; font locking is invoked to solve this well.
5004 ;; This function might do hidden buffer changes.
5005 (let ((type (c-syntactic-content from to c-recognize-
<>-arglists
)))
5006 (unless (intern-soft type c-found-types
)
5007 (unintern (substring type
0 -
1) c-found-types
)
5008 (intern type c-found-types
))))
5010 (defun c-unfind-type (name)
5011 ;; Remove the "NAME" from c-found-types, if present.
5012 (unintern name c-found-types
))
5014 (defsubst c-check-type
(from to
)
5015 ;; Return non-nil if the given region contains a type in
5018 ;; This function might do hidden buffer changes.
5019 (intern-soft (c-syntactic-content from to c-recognize-
<>-arglists
)
5022 (defun c-list-found-types ()
5023 ;; Return all the types in `c-found-types' as a sorted list of
5026 (mapatoms (lambda (type)
5027 (setq type-list
(cons (symbol-name type
)
5030 (sort type-list
'string-lessp
)))
5032 ;; Shut up the byte compiler.
5033 (defvar c-maybe-stale-found-type
)
5035 (defun c-trim-found-types (beg end old-len
)
5036 ;; An after change function which, in conjunction with the info in
5037 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
5038 ;; from `c-found-types', should this type have become stale. For
5039 ;; example, this happens to "foo" when "foo \n bar();" becomes
5040 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
5041 ;; the fontification.
5043 ;; Have we, perhaps, added non-ws characters to the front/back of a found
5047 (when (< end
(point-max))
5049 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
5050 (progn (goto-char end
)
5051 (c-end-of-current-token)))
5052 (c-unfind-type (buffer-substring-no-properties
5054 (when (> beg
(point-min))
5056 (if (and (c-end-of-current-token) ; only moves when we started in the middle
5057 (progn (goto-char beg
)
5058 (c-beginning-of-current-token)))
5059 (c-unfind-type (buffer-substring-no-properties
5062 (if c-maybe-stale-found-type
; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
5064 ;; Changing the amount of (already existing) whitespace - don't do anything.
5065 ((and (c-partial-ws-p beg end
)
5066 (or (= beg end
) ; removal of WS
5067 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type
)))))
5069 ;; The syntactic relationship which defined a "found type" has been
5071 ((eq (car c-maybe-stale-found-type
) 'c-decl-id-start
)
5072 (c-unfind-type (cadr c-maybe-stale-found-type
)))
5073 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5077 ;; Setting and removing syntax properties on < and > in languages (C++
5078 ;; and Java) where they can be template/generic delimiters as well as
5079 ;; their normal meaning of "less/greater than".
5081 ;; Normally, < and > have syntax 'punctuation'. When they are found to
5082 ;; be delimiters, they are marked as such with the category properties
5083 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5087 ;; It is impossible to determine with certainty whether a <..> pair in
5088 ;; C++ is two comparison operators or is template delimiters, unless
5089 ;; one duplicates a lot of a C++ compiler. For example, the following
5092 ;; foo (a < b, c > d) ;
5094 ;; could be a function call with two integer parameters (each a
5095 ;; relational expression), or it could be a constructor for class foo
5096 ;; taking one parameter d of templated type "a < b, c >". They are
5097 ;; somewhat easier to distinguish in Java.
5099 ;; The strategy now (2010-01) adopted is to mark and unmark < and
5100 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5101 ;; individually when their context so indicated. This gave rise to
5102 ;; intractable problems when one of a matching pair was deleted, or
5103 ;; pulled into a literal.]
5105 ;; At each buffer change, the syntax-table properties are removed in a
5106 ;; before-change function and reapplied, when needed, in an
5107 ;; after-change function. It is far more important that the
5108 ;; properties get removed when they they are spurious than that they
5109 ;; be present when wanted.
5110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5111 (defun c-clear-<-pair-props
(&optional pos
)
5112 ;; POS (default point) is at a < character. If it is marked with
5113 ;; open paren syntax-table text property, remove the property,
5114 ;; together with the close paren property on the matching > (if
5120 (when (equal (c-get-char-property (point) 'syntax-table
)
5121 c-
<-as-paren-syntax
)
5122 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
5123 (c-go-list-forward))
5124 (when (equal (c-get-char-property (1- (point)) 'syntax-table
)
5125 c-
>-as-paren-syntax
) ; should always be true.
5126 (c-clear-char-property (1- (point)) 'category
))
5127 (c-clear-char-property pos
'category
))))
5129 (defun c-clear->-pair-props
(&optional pos
)
5130 ;; POS (default point) is at a > character. If it is marked with
5131 ;; close paren syntax-table property, remove the property, together
5132 ;; with the open paren property on the matching < (if any).
5137 (when (equal (c-get-char-property (point) 'syntax-table
)
5138 c-
>-as-paren-syntax
)
5139 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
5140 (c-go-up-list-backward))
5141 (when (equal (c-get-char-property (point) 'syntax-table
)
5142 c-
<-as-paren-syntax
) ; should always be true.
5143 (c-clear-char-property (point) 'category
))
5144 (c-clear-char-property pos
'category
))))
5146 (defun c-clear-<>-pair-props
(&optional pos
)
5147 ;; POS (default point) is at a < or > character. If it has an
5148 ;; open/close paren syntax-table property, remove this property both
5149 ;; from the current character and its partner (which will also be
5152 ((eq (char-after) ?\
<)
5153 (c-clear-<-pair-props pos
))
5154 ((eq (char-after) ?\
>)
5155 (c-clear->-pair-props pos
))
5157 "c-clear-<>-pair-props called from wrong position"))))
5159 (defun c-clear-<-pair-props-if-match-after
(lim &optional pos
)
5160 ;; POS (default point) is at a < character. If it is both marked
5161 ;; with open/close paren syntax-table property, and has a matching >
5162 ;; (also marked) which is after LIM, remove the property both from
5163 ;; the current > and its partner. Return t when this happens, nil
5169 (when (equal (c-get-char-property (point) 'syntax-table
)
5170 c-
<-as-paren-syntax
)
5171 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
5172 (c-go-list-forward))
5173 (when (and (>= (point) lim
)
5174 (equal (c-get-char-property (1- (point)) 'syntax-table
)
5175 c-
>-as-paren-syntax
)) ; should always be true.
5176 (c-unmark-<-
>-as-paren
(1- (point)))
5177 (c-unmark-<-
>-as-paren pos
))
5180 (defun c-clear->-pair-props-if-match-before
(lim &optional pos
)
5181 ;; POS (default point) is at a > character. If it is both marked
5182 ;; with open/close paren syntax-table property, and has a matching <
5183 ;; (also marked) which is before LIM, remove the property both from
5184 ;; the current < and its partner. Return t when this happens, nil
5190 (when (equal (c-get-char-property (point) 'syntax-table
)
5191 c-
>-as-paren-syntax
)
5192 (with-syntax-table c-no-parens-syntax-table
; ignore unbalanced [,{,(,..
5193 (c-go-up-list-backward))
5194 (when (and (<= (point) lim
)
5195 (equal (c-get-char-property (point) 'syntax-table
)
5196 c-
<-as-paren-syntax
)) ; should always be true.
5197 (c-unmark-<-
>-as-paren
(point))
5198 (c-unmark-<-
>-as-paren pos
))
5201 ;; Set by c-common-init in cc-mode.el.
5205 (defun c-before-change-check-<>-operators
(beg end
)
5206 ;; Unmark certain pairs of "< .... >" which are currently marked as
5207 ;; template/generic delimiters. (This marking is via syntax-table
5208 ;; text properties).
5210 ;; These pairs are those which are in the current "statement" (i.e.,
5211 ;; the region between the {, }, or ; before BEG and the one after
5212 ;; END), and which enclose any part of the interval (BEG END).
5214 ;; Note that in C++ (?and Java), template/generic parens cannot
5215 ;; enclose a brace or semicolon, so we use these as bounds on the
5216 ;; region we must work on.
5218 ;; This function is called from before-change-functions (via
5219 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5220 ;; and point is undefined, both at entry and exit.
5222 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5225 (let ((beg-lit-limits (progn (goto-char beg
) (c-literal-limits)))
5226 (end-lit-limits (progn (goto-char end
) (c-literal-limits)))
5227 new-beg new-end need-new-beg need-new-end
)
5228 ;; Locate the barrier before the changed region
5229 (goto-char (if beg-lit-limits
(car beg-lit-limits
) beg
))
5230 (c-syntactic-skip-backward "^;{}" (max (- beg
2048) (point-min)))
5231 (setq new-beg
(point))
5233 ;; Remove the syntax-table properties from each pertinent <...> pair.
5234 ;; Firsly, the ones with the < before beg and > after beg.
5235 (while (c-search-forward-char-property 'category
'c-
<-as-paren-syntax beg
)
5236 (if (c-clear-<-pair-props-if-match-after beg
(1- (point)))
5237 (setq need-new-beg t
)))
5239 ;; Locate the barrier after END.
5240 (goto-char (if end-lit-limits
(cdr end-lit-limits
) end
))
5241 (c-syntactic-re-search-forward "[;{}]"
5242 (min (+ end
2048) (point-max)) 'end
)
5243 (setq new-end
(point))
5245 ;; Remove syntax-table properties from the remaining pertinent <...>
5246 ;; pairs, those with a > after end and < before end.
5247 (while (c-search-backward-char-property 'category
'c-
>-as-paren-syntax end
)
5248 (if (c-clear->-pair-props-if-match-before end
)
5249 (setq need-new-end t
)))
5251 ;; Extend the fontification region, if needed.
5254 (c-forward-syntactic-ws)
5255 (and (< (point) c-new-BEG
) (setq c-new-BEG
(point))))
5258 (and (> new-end c-new-END
) (setq c-new-END new-end
))))))
5262 (defun c-after-change-check-<>-operators
(beg end
)
5263 ;; This is called from `after-change-functions' when
5264 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5265 ;; chars with paren syntax become part of another operator like "<<"
5268 ;; This function might do hidden buffer changes.
5272 (when (or (looking-at "[<>]")
5273 (< (skip-chars-backward "<>") 0))
5276 (c-beginning-of-current-token)
5277 (when (and (< (point) beg
)
5278 (looking-at c-
<>-multichar-token-regexp
)
5279 (< beg
(setq beg
(match-end 0))))
5280 (while (progn (skip-chars-forward "^<>" beg
)
5282 (c-clear-<>-pair-props
)
5287 (when (or (looking-at "[<>]")
5288 (< (skip-chars-backward "<>") 0))
5291 (c-beginning-of-current-token)
5292 (when (and (< (point) end
)
5293 (looking-at c-
<>-multichar-token-regexp
)
5294 (< end
(setq end
(match-end 0))))
5295 (while (progn (skip-chars-forward "^<>" end
)
5297 (c-clear-<>-pair-props
)
5298 (forward-char)))))))
5302 ;; Handling of small scale constructs like types and names.
5304 ;; Dynamically bound variable that instructs `c-forward-type' to also
5305 ;; treat possible types (i.e. those that it normally returns 'maybe or
5306 ;; 'found for) as actual types (and always return 'found for them).
5307 ;; This means that it records them in `c-record-type-identifiers' if
5308 ;; that is set, and that it adds them to `c-found-types'.
5309 (defvar c-promote-possible-types nil
)
5311 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5312 ;; mark up successfully parsed arglists with paren syntax properties on
5313 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5314 ;; `c-type' property of each argument separating comma.
5316 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5317 ;; all arglists for side effects (i.e. recording types), otherwise it
5318 ;; exploits any existing paren syntax properties to quickly jump to the
5319 ;; end of already parsed arglists.
5321 ;; Marking up the arglists is not the default since doing that correctly
5322 ;; depends on a proper value for `c-restricted-<>-arglists'.
5323 (defvar c-parse-and-markup-
<>-arglists nil
)
5325 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5326 ;; not accept arglists that contain binary operators.
5328 ;; This is primarily used to handle C++ template arglists. C++
5329 ;; disambiguates them by checking whether the preceding name is a
5330 ;; template or not. We can't do that, so we assume it is a template
5331 ;; if it can be parsed as one. That usually works well since
5332 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5333 ;; in almost all cases would be pointless.
5335 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5336 ;; should let the comma separate the function arguments instead. And
5337 ;; in a context where the value of the expression is taken, e.g. in
5338 ;; "if (a < b || c > d)", it's probably not a template.
5339 (defvar c-restricted-
<>-arglists nil
)
5341 ;; Dynamically bound variables that instructs
5342 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5343 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5344 ;; `c-forward-label' to record the ranges of all the type and
5345 ;; reference identifiers they encounter. They will build lists on
5346 ;; these variables where each element is a cons of the buffer
5347 ;; positions surrounding each identifier. This recording is only
5348 ;; activated when `c-record-type-identifiers' is non-nil.
5350 ;; All known types that can't be identifiers are recorded, and also
5351 ;; other possible types if `c-promote-possible-types' is set.
5352 ;; Recording is however disabled inside angle bracket arglists that
5353 ;; are encountered inside names and other angle bracket arglists.
5354 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5357 ;; Only the names in C++ template style references (e.g. "tmpl" in
5358 ;; "tmpl<a,b>::foo") are recorded as references, other references
5359 ;; aren't handled here.
5361 ;; `c-forward-label' records the label identifier(s) on
5362 ;; `c-record-ref-identifiers'.
5363 (defvar c-record-type-identifiers nil
)
5364 (defvar c-record-ref-identifiers nil
)
5366 ;; This variable will receive a cons cell of the range of the last
5367 ;; single identifier symbol stepped over by `c-forward-name' if it's
5368 ;; successful. This is the range that should be put on one of the
5369 ;; record lists above by the caller. It's assigned nil if there's no
5370 ;; such symbol in the name.
5371 (defvar c-last-identifier-range nil
)
5373 (defmacro c-record-type-id
(range)
5374 (if (eq (car-safe range
) 'cons
)
5376 `(setq c-record-type-identifiers
5377 (cons ,range c-record-type-identifiers
))
5378 `(let ((range ,range
))
5380 (setq c-record-type-identifiers
5381 (cons range c-record-type-identifiers
))))))
5383 (defmacro c-record-ref-id
(range)
5384 (if (eq (car-safe range
) 'cons
)
5386 `(setq c-record-ref-identifiers
5387 (cons ,range c-record-ref-identifiers
))
5388 `(let ((range ,range
))
5390 (setq c-record-ref-identifiers
5391 (cons range c-record-ref-identifiers
))))))
5393 ;; Dynamically bound variable that instructs `c-forward-type' to
5394 ;; record the ranges of types that only are found. Behaves otherwise
5395 ;; like `c-record-type-identifiers'.
5396 (defvar c-record-found-types nil
)
5398 (defmacro c-forward-keyword-prefixed-id
(type)
5399 ;; Used internally in `c-forward-keyword-clause' to move forward
5400 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5401 ;; possibly is prefixed by keywords and their associated clauses.
5402 ;; Try with a type/name first to not trip up on those that begin
5403 ;; with a keyword. Return t if a known or found type is moved
5404 ;; over. The point is clobbered if nil is returned. If range
5405 ;; recording is enabled, the identifier is recorded on as a type
5406 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5408 ;; This macro might do hidden buffer changes.
5410 (while (if (setq res
,(if (eq type
'type
)
5414 (and (looking-at c-keywords-regexp
)
5415 (c-forward-keyword-clause 1))))
5416 (when (memq res
'(t known found prefix
))
5417 ,(when (eq type
'ref
)
5418 `(when c-record-type-identifiers
5419 (c-record-ref-id c-last-identifier-range
)))
5422 (defmacro c-forward-id-comma-list
(type update-safe-pos
)
5423 ;; Used internally in `c-forward-keyword-clause' to move forward
5424 ;; over a comma separated list of types or names using
5425 ;; `c-forward-keyword-prefixed-id'.
5427 ;; This macro might do hidden buffer changes.
5429 ,(when update-safe-pos
5430 `(setq safe-pos
(point)))
5431 (eq (char-after) ?
,))
5434 (c-forward-syntactic-ws)
5435 (c-forward-keyword-prefixed-id ,type
)))))
5437 (defun c-forward-keyword-clause (match)
5438 ;; Submatch MATCH in the current match data is assumed to surround a
5439 ;; token. If it's a keyword, move over it and any immediately
5440 ;; following clauses associated with it, stopping at the start of
5441 ;; the next token. t is returned in that case, otherwise the point
5442 ;; stays and nil is returned. The kind of clauses that are
5443 ;; recognized are those specified by `c-type-list-kwds',
5444 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5445 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5446 ;; and `c-<>-arglist-kwds'.
5448 ;; This function records identifier ranges on
5449 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5450 ;; `c-record-type-identifiers' is non-nil.
5452 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5453 ;; apply directly after the keyword, the type list is moved over
5454 ;; only when there is no unaccounted token before it (i.e. a token
5455 ;; that isn't moved over due to some other keyword list). The
5456 ;; identifier ranges in the list are still recorded if that should
5459 ;; This function might do hidden buffer changes.
5461 (let ((kwd-sym (c-keyword-sym (match-string match
))) safe-pos pos
5462 ;; The call to `c-forward-<>-arglist' below is made after
5463 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5464 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5465 ;; should therefore be nil.
5466 (c-parse-and-markup-<>-arglists t
)
5467 c-restricted-
<>-arglists
)
5470 (goto-char (match-end match
))
5471 (c-forward-syntactic-ws)
5472 (setq safe-pos
(point))
5475 ((and (c-keyword-member kwd-sym
'c-type-list-kwds
)
5476 (c-forward-keyword-prefixed-id type
))
5477 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5478 (c-forward-id-comma-list type t
))
5480 ((and (c-keyword-member kwd-sym
'c-ref-list-kwds
)
5481 (c-forward-keyword-prefixed-id ref
))
5482 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5483 (c-forward-id-comma-list ref t
))
5485 ((and (c-keyword-member kwd-sym
'c-paren-any-kwds
)
5486 (eq (char-after) ?\
())
5487 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5490 (when (and (setq pos
(c-up-list-forward))
5491 (eq (char-before pos
) ?\
)))
5492 (when (and c-record-type-identifiers
5493 (c-keyword-member kwd-sym
'c-paren-type-kwds
))
5494 ;; Use `c-forward-type' on every identifier we can find
5495 ;; inside the paren, to record the types.
5496 (while (c-syntactic-re-search-forward c-symbol-start pos t
)
5497 (goto-char (match-beginning 0))
5498 (unless (c-forward-type)
5499 (looking-at c-symbol-key
) ; Always matches.
5500 (goto-char (match-end 0)))))
5503 (c-forward-syntactic-ws)
5504 (setq safe-pos
(point))))
5506 ((and (c-keyword-member kwd-sym
'c-
<>-sexp-kwds
)
5507 (eq (char-after) ?
<)
5508 (c-forward-<>-arglist
(c-keyword-member kwd-sym
'c-
<>-type-kwds
)))
5509 (c-forward-syntactic-ws)
5510 (setq safe-pos
(point)))
5512 ((and (c-keyword-member kwd-sym
'c-nonsymbol-sexp-kwds
)
5513 (not (looking-at c-symbol-start
))
5514 (c-safe (c-forward-sexp) t
))
5515 (c-forward-syntactic-ws)
5516 (setq safe-pos
(point))))
5518 (when (c-keyword-member kwd-sym
'c-colon-type-list-kwds
)
5519 (if (eq (char-after) ?
:)
5520 ;; If we are at the colon already, we move over the type
5524 (c-forward-syntactic-ws)
5525 (when (c-forward-keyword-prefixed-id type
)
5526 (c-forward-id-comma-list type t
)))
5527 ;; Not at the colon, so stop here. But the identifier
5528 ;; ranges in the type list later on should still be
5530 (and c-record-type-identifiers
5532 ;; If a keyword matched both one of the types above and
5533 ;; this one, we match `c-colon-type-list-re' after the
5534 ;; clause matched above.
5535 (goto-char safe-pos
)
5536 (looking-at c-colon-type-list-re
))
5538 (goto-char (match-end 0))
5539 (c-forward-syntactic-ws)
5540 (c-forward-keyword-prefixed-id type
))
5541 ;; There's a type after the `c-colon-type-list-re' match
5542 ;; after a keyword in `c-colon-type-list-kwds'.
5543 (c-forward-id-comma-list type nil
))))
5545 (goto-char safe-pos
)
5548 ;; cc-mode requires cc-fonts.
5549 (declare-function c-fontify-recorded-types-and-refs
"cc-fonts" ())
5551 (defun c-forward-<>-arglist
(all-types)
5552 ;; The point is assumed to be at a "<". Try to treat it as the open
5553 ;; paren of an angle bracket arglist and move forward to the
5554 ;; corresponding ">". If successful, the point is left after the
5555 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5556 ;; returned. If ALL-TYPES is t then all encountered arguments in
5557 ;; the arglist that might be types are treated as found types.
5559 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5560 ;; function handles text properties on the angle brackets and argument
5561 ;; separating commas.
5563 ;; `c-restricted-<>-arglists' controls how lenient the template
5564 ;; arglist recognition should be.
5566 ;; This function records identifier ranges on
5567 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5568 ;; `c-record-type-identifiers' is non-nil.
5570 ;; This function might do hidden buffer changes.
5572 (let ((start (point))
5573 ;; If `c-record-type-identifiers' is set then activate
5574 ;; recording of any found types that constitute an argument in
5576 (c-record-found-types (if c-record-type-identifiers t
)))
5577 (if (catch 'angle-bracket-arglist-escape
5578 (setq c-record-found-types
5579 (c-forward-<>-arglist-recur all-types
)))
5581 (when (consp c-record-found-types
)
5582 (setq c-record-type-identifiers
5583 ;; `nconc' doesn't mind that the tail of
5584 ;; `c-record-found-types' is t.
5585 (nconc c-record-found-types c-record-type-identifiers
)))
5586 (if (c-major-mode-is 'java-mode
) (c-fontify-recorded-types-and-refs))
5592 (defun c-forward-<>-arglist-recur
(all-types)
5593 ;; Recursive part of `c-forward-<>-arglist'.
5595 ;; This function might do hidden buffer changes.
5597 (let ((start (point)) res pos tmp
5598 ;; Cover this so that any recorded found type ranges are
5599 ;; automatically lost if it turns out to not be an angle
5600 ;; bracket arglist. It's propagated through the return value
5601 ;; on successful completion.
5602 (c-record-found-types c-record-found-types
)
5603 ;; List that collects the positions after the argument
5604 ;; separating ',' in the arglist.
5606 ;; If the '<' has paren open syntax then we've marked it as an angle
5607 ;; bracket arglist before, so skip to the end.
5608 (if (and (not c-parse-and-markup-
<>-arglists
)
5609 (c-get-char-property (point) 'syntax-table
))
5613 (if (and (c-go-up-list-forward)
5614 (eq (char-before) ?
>))
5616 ;; Got unmatched paren angle brackets. We don't clear the paren
5617 ;; syntax properties and retry, on the basis that it's very
5618 ;; unlikely that paren angle brackets become operators by code
5619 ;; manipulation. It's far more likely that it doesn't match due
5620 ;; to narrowing or some temporary change.
5624 (forward-char) ; Forward over the opening '<'.
5626 (unless (looking-at c-
<-op-cont-regexp
)
5627 ;; go forward one non-alphanumeric character (group) per iteration of
5631 (c-forward-syntactic-ws)
5632 (let ((orig-record-found-types c-record-found-types
))
5633 (when (or (and c-record-type-identifiers all-types
)
5634 (c-major-mode-is 'java-mode
))
5635 ;; All encountered identifiers are types, so set the
5636 ;; promote flag and parse the type.
5638 (c-forward-syntactic-ws)
5639 (if (looking-at "\\?")
5641 (when (looking-at c-identifier-start
)
5642 (let ((c-promote-possible-types t
)
5643 (c-record-found-types t
))
5646 (c-forward-syntactic-ws)
5648 (when (or (looking-at "extends")
5649 (looking-at "super"))
5651 (c-forward-syntactic-ws)
5652 (let ((c-promote-possible-types t
)
5653 (c-record-found-types t
))
5655 (c-forward-syntactic-ws))))))
5657 (setq pos
(point)) ; e.g. first token inside the '<'
5659 ;; Note: These regexps exploit the match order in \| so
5660 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5661 (c-syntactic-re-search-forward
5662 ;; Stop on ',', '|', '&', '+' and '-' to catch
5663 ;; common binary operators that could be between
5664 ;; two comparison expressions "a<b" and "c>d".
5665 "[<;{},|+&-]\\|[>)]"
5669 ((eq (char-before) ?
>)
5670 ;; Either an operator starting with '>' or the end of
5671 ;; the angle bracket arglist.
5673 (if (looking-at c-
>-op-cont-regexp
)
5675 (goto-char (match-end 0))
5676 t
) ; Continue the loop.
5678 ;; The angle bracket arglist is finished.
5679 (when c-parse-and-markup-
<>-arglists
5680 (while arg-start-pos
5681 (c-put-c-type-property (1- (car arg-start-pos
))
5683 (setq arg-start-pos
(cdr arg-start-pos
)))
5684 (c-mark-<-as-paren start
)
5685 (c-mark->-as-paren
(1- (point))))
5687 nil
)) ; Exit the loop.
5689 ((eq (char-before) ?
<)
5690 ;; Either an operator starting with '<' or a nested arglist.
5692 (let (id-start id-end subres keyword-match
)
5694 ;; The '<' begins a multi-char operator.
5695 ((looking-at c-
<-op-cont-regexp
)
5696 (setq tmp
(match-end 0))
5697 (goto-char (match-end 0)))
5698 ;; We're at a nested <.....>
5701 (backward-char) ; to the '<'
5704 ;; There's always an identifier before an angle
5705 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5706 ;; or `c-<>-arglist-kwds'.
5707 (c-backward-syntactic-ws)
5708 (setq id-end
(point))
5709 (c-simple-skip-symbol-backward)
5710 (when (or (setq keyword-match
5711 (looking-at c-opt-
<>-sexp-key
))
5712 (not (looking-at c-keywords-regexp
)))
5713 (setq id-start
(point))))
5715 (let ((c-promote-possible-types t
)
5716 (c-record-found-types t
))
5717 (c-forward-<>-arglist-recur
5720 (c-keyword-sym (match-string 1))
5721 'c-
<>-type-kwds
)))))))
5723 ;; It was an angle bracket arglist.
5724 (setq c-record-found-types subres
)
5726 ;; Record the identifier before the template as a type
5727 ;; or reference depending on whether the arglist is last
5728 ;; in a qualified identifier.
5729 (when (and c-record-type-identifiers
5730 (not keyword-match
))
5731 (if (and c-opt-identifier-concat-key
5733 (c-forward-syntactic-ws)
5734 (looking-at c-opt-identifier-concat-key
)))
5735 (c-record-ref-id (cons id-start id-end
))
5736 (c-record-type-id (cons id-start id-end
)))))
5738 ;; At a "less than" operator.
5742 t
) ; carry on looping.
5744 ((and (not c-restricted-
<>-arglists
)
5745 (or (and (eq (char-before) ?
&)
5746 (not (eq (char-after) ?
&)))
5747 (eq (char-before) ?
,)))
5748 ;; Just another argument. Record the position. The
5749 ;; type check stuff that made us stop at it is at
5750 ;; the top of the loop.
5751 (setq arg-start-pos
(cons (point) arg-start-pos
)))
5754 ;; Got a character that can't be in an angle bracket
5755 ;; arglist argument. Abort using `throw', since
5756 ;; it's useless to try to find a surrounding arglist
5758 (throw 'angle-bracket-arglist-escape nil
))))))
5760 (or c-record-found-types t
)))))
5762 (defun c-backward-<>-arglist
(all-types &optional limit
)
5763 ;; The point is assumed to be directly after a ">". Try to treat it
5764 ;; as the close paren of an angle bracket arglist and move back to
5765 ;; the corresponding "<". If successful, the point is left at
5766 ;; the "<" and t is returned, otherwise the point isn't moved and
5767 ;; nil is returned. ALL-TYPES is passed on to
5768 ;; `c-forward-<>-arglist'.
5770 ;; If the optional LIMIT is given, it bounds the backward search.
5771 ;; It's then assumed to be at a syntactically relevant position.
5773 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5774 ;; function for more details.
5776 (let ((start (point)))
5778 (if (and (not c-parse-and-markup-
<>-arglists
)
5779 (c-get-char-property (point) 'syntax-table
))
5781 (if (and (c-go-up-list-backward)
5782 (eq (char-after) ?
<))
5784 ;; See corresponding note in `c-forward-<>-arglist'.
5789 (c-syntactic-skip-backward "^<;{}" limit t
)
5792 (if (eq (char-before) ?
<)
5794 ;; Stopped at bob or a char that isn't allowed in an
5795 ;; arglist, so we've failed.
5800 (progn (c-beginning-of-current-token)
5802 ;; If we moved then the "<" was part of some
5803 ;; multicharacter token.
5807 (let ((beg-pos (point)))
5808 (if (c-forward-<>-arglist all-types
)
5809 (cond ((= (point) start
)
5810 ;; Matched the arglist. Break the while.
5814 ;; We started from a non-paren ">" inside an
5819 ;; Matched a shorter arglist. Can be a nested
5820 ;; one so continue looking.
5825 (/= (point) start
))))
5827 (defun c-forward-name ()
5828 ;; Move forward over a complete name if at the beginning of one,
5829 ;; stopping at the next following token. A keyword, as such,
5830 ;; doesn't count as a name. If the point is not at something that
5831 ;; is recognized as a name then it stays put.
5833 ;; A name could be something as simple as "foo" in C or something as
5834 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5835 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5836 ;; int>::*volatile const" in C++ (this function is actually little
5837 ;; more than a `looking-at' call in all modes except those that,
5838 ;; like C++, have `c-recognize-<>-arglists' set).
5841 ;; o - nil if no name is found;
5842 ;; o - 'template if it's an identifier ending with an angle bracket
5844 ;; o - 'operator of it's an operator identifier;
5845 ;; o - t if it's some other kind of name.
5847 ;; This function records identifier ranges on
5848 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5849 ;; `c-record-type-identifiers' is non-nil.
5851 ;; This function might do hidden buffer changes.
5853 (let ((pos (point)) (start (point)) res id-start id-end
5854 ;; Turn off `c-promote-possible-types' here since we might
5855 ;; call `c-forward-<>-arglist' and we don't want it to promote
5856 ;; every suspect thing in the arglist to a type. We're
5857 ;; typically called from `c-forward-type' in this case, and
5858 ;; the caller only wants the top level type that it finds to
5860 c-promote-possible-types
)
5863 (looking-at c-identifier-key
)
5866 ;; Check for keyword. We go to the last symbol in
5867 ;; `c-identifier-key' first.
5868 (goto-char (setq id-end
(match-end 0)))
5869 (c-simple-skip-symbol-backward)
5870 (setq id-start
(point))
5872 (if (looking-at c-keywords-regexp
)
5873 (when (and (c-major-mode-is 'c
++-mode
)
5875 (cc-eval-when-compile
5876 (concat "\\(operator\\|\\(template\\)\\)"
5877 "\\(" (c-lang-const c-nonsymbol-key c
++)
5879 (if (match-beginning 2)
5880 ;; "template" is only valid inside an
5881 ;; identifier if preceded by "::".
5883 (c-backward-syntactic-ws)
5884 (and (c-safe (backward-char 2) t
)
5888 ;; Handle a C++ operator or template identifier.
5890 (c-forward-syntactic-ws)
5891 (cond ((eq (char-before id-end
) ?e
)
5892 ;; Got "... ::template".
5893 (let ((subres (c-forward-name)))
5898 ((looking-at c-identifier-start
)
5899 ;; Got a cast operator.
5900 (when (c-forward-type)
5903 ;; Now we should match a sequence of either
5904 ;; '*', '&' or a name followed by ":: *",
5905 ;; where each can be followed by a sequence
5906 ;; of `c-opt-type-modifier-key'.
5907 (while (cond ((looking-at "[*&]")
5908 (goto-char (match-end 0))
5910 ((looking-at c-identifier-start
)
5911 (and (c-forward-name)
5914 (goto-char (match-end 0))
5915 (c-forward-syntactic-ws)
5916 (eq (char-after) ?
*))
5921 (c-forward-syntactic-ws)
5923 (looking-at c-opt-type-modifier-key
))
5924 (goto-char (match-end 1))))))
5926 ((looking-at c-overloadable-operators-regexp
)
5927 ;; Got some other operator.
5928 (setq c-last-identifier-range
5929 (cons (point) (match-end 0)))
5930 (goto-char (match-end 0))
5931 (c-forward-syntactic-ws)
5937 ;; `id-start' is equal to `id-end' if we've jumped over
5938 ;; an identifier that doesn't end with a symbol token.
5939 ;; That can occur e.g. for Java import directives on the
5940 ;; form "foo.bar.*".
5941 (when (and id-start
(/= id-start id-end
))
5942 (setq c-last-identifier-range
5943 (cons id-start id-end
)))
5945 (c-forward-syntactic-ws)
5951 (when (or c-opt-identifier-concat-key
5952 c-recognize-
<>-arglists
)
5955 ((and c-opt-identifier-concat-key
5956 (looking-at c-opt-identifier-concat-key
))
5957 ;; Got a concatenated identifier. This handles the
5958 ;; cases with tricky syntactic whitespace that aren't
5959 ;; covered in `c-identifier-key'.
5960 (goto-char (match-end 0))
5961 (c-forward-syntactic-ws)
5964 ((and c-recognize-
<>-arglists
5965 (eq (char-after) ?
<))
5966 ;; Maybe an angle bracket arglist.
5967 (when (let ((c-record-type-identifiers t
)
5968 (c-record-found-types t
))
5969 (c-forward-<>-arglist nil
))
5971 (c-add-type start
(1+ pos
))
5972 (c-forward-syntactic-ws)
5974 c-last-identifier-range nil
)
5976 (if (and c-opt-identifier-concat-key
5977 (looking-at c-opt-identifier-concat-key
))
5979 ;; Continue if there's an identifier concatenation
5980 ;; operator after the template argument.
5982 (when (and c-record-type-identifiers id-start
)
5983 (c-record-ref-id (cons id-start id-end
)))
5985 (c-forward-syntactic-ws)
5988 (when (and c-record-type-identifiers id-start
)
5989 (c-record-type-id (cons id-start id-end
)))
5990 (setq res
'template
)
5997 (defun c-forward-type (&optional brace-block-too
)
5998 ;; Move forward over a type spec if at the beginning of one,
5999 ;; stopping at the next following token. The keyword "typedef"
6000 ;; isn't part of a type spec here.
6002 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
6003 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
6004 ;; The current (2009-03-10) intention is to convert all uses of
6005 ;; `c-forward-type' to call with this parameter set, then to
6009 ;; o - t if it's a known type that can't be a name or other
6011 ;; o - 'known if it's an otherwise known type (according to
6012 ;; `*-font-lock-extra-types');
6013 ;; o - 'prefix if it's a known prefix of a type;
6014 ;; o - 'found if it's a type that matches one in `c-found-types';
6015 ;; o - 'maybe if it's an identifier that might be a type; or
6016 ;; o - nil if it can't be a type (the point isn't moved then).
6018 ;; The point is assumed to be at the beginning of a token.
6020 ;; Note that this function doesn't skip past the brace definition
6021 ;; that might be considered part of the type, e.g.
6022 ;; "enum {a, b, c} foo".
6024 ;; This function records identifier ranges on
6025 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6026 ;; `c-record-type-identifiers' is non-nil.
6028 ;; This function might do hidden buffer changes.
6029 (when (and c-recognize-
<>-arglists
6031 (c-forward-<>-arglist t
)
6032 (c-forward-syntactic-ws))
6034 (let ((start (point)) pos res name-res id-start id-end id-range
)
6036 ;; Skip leading type modifiers. If any are found we know it's a
6037 ;; prefix of a type.
6038 (when c-opt-type-modifier-key
; e.g. "const" "volatile", but NOT "typedef"
6039 (while (looking-at c-opt-type-modifier-key
)
6040 (goto-char (match-end 1))
6041 (c-forward-syntactic-ws)
6042 (setq res
'prefix
)))
6045 ((looking-at c-type-prefix-key
) ; e.g. "struct", "class", but NOT
6047 (goto-char (match-end 1))
6048 (c-forward-syntactic-ws)
6051 (setq name-res
(c-forward-name))
6052 (setq res
(not (null name-res
)))
6053 (when (eq name-res t
)
6054 ;; In many languages the name can be used without the
6055 ;; prefix, so we add it to `c-found-types'.
6056 (c-add-type pos
(point))
6057 (when (and c-record-type-identifiers
6058 c-last-identifier-range
)
6059 (c-record-type-id c-last-identifier-range
)))
6060 (when (and brace-block-too
6062 (eq (char-after) ?\
{)
6065 (progn (c-forward-sexp)
6066 (c-forward-syntactic-ws)
6067 (setq pos
(point))))))
6070 (unless res
(goto-char start
))) ; invalid syntax
6074 (if (looking-at c-identifier-start
)
6076 (setq id-start
(point)
6077 name-res
(c-forward-name))
6079 (setq id-end
(point)
6080 id-range c-last-identifier-range
))))
6081 (and (cond ((looking-at c-primitive-type-key
)
6083 ((c-with-syntax-table c-identifier-syntax-table
6084 (looking-at c-known-type-key
))
6089 (goto-char (match-end 1))
6090 (c-forward-syntactic-ws)
6091 (setq pos
(point))))
6094 ;; Looking at a primitive or known type identifier. We've
6095 ;; checked for a name first so that we don't go here if the
6096 ;; known type match only is a prefix of another name.
6098 (setq id-end
(match-end 1))
6100 (when (and c-record-type-identifiers
6101 (or c-promote-possible-types
(eq res t
)))
6102 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6104 (if (and c-opt-type-component-key
6106 (looking-at c-opt-type-component-key
)))
6107 ;; There might be more keywords for the type.
6109 (c-forward-keyword-clause 1)
6111 (setq safe-pos
(point))
6112 (looking-at c-opt-type-component-key
))
6113 (when (and c-record-type-identifiers
6114 (looking-at c-primitive-type-key
))
6115 (c-record-type-id (cons (match-beginning 1)
6117 (c-forward-keyword-clause 1))
6118 (if (looking-at c-primitive-type-key
)
6120 (when c-record-type-identifiers
6121 (c-record-type-id (cons (match-beginning 1)
6123 (c-forward-keyword-clause 1)
6125 (goto-char safe-pos
)
6126 (setq res
'prefix
)))
6127 (unless (save-match-data (c-forward-keyword-clause 1))
6130 (goto-char (match-end 1))
6131 (c-forward-syntactic-ws)))))
6134 (cond ((eq name-res t
)
6135 ;; A normal identifier.
6137 (if (or res c-promote-possible-types
)
6139 (c-add-type id-start id-end
)
6140 (when (and c-record-type-identifiers id-range
)
6141 (c-record-type-id id-range
))
6144 (setq res
(if (c-check-type id-start id-end
)
6145 ;; It's an identifier that has been used as
6146 ;; a type somewhere else.
6148 ;; It's an identifier that might be a type.
6150 ((eq name-res
'template
)
6151 ;; A template is a type.
6155 ;; Otherwise it's an operator identifier, which is not a type.
6160 ;; Skip trailing type modifiers. If any are found we know it's
6162 (when c-opt-type-modifier-key
6163 (while (looking-at c-opt-type-modifier-key
) ; e.g. "const", "volatile"
6164 (goto-char (match-end 1))
6165 (c-forward-syntactic-ws)
6167 ;; Step over any type suffix operator. Do not let the existence
6168 ;; of these alter the classification of the found type, since
6169 ;; these operators typically are allowed in normal expressions
6171 (when c-opt-type-suffix-key
6172 (while (looking-at c-opt-type-suffix-key
)
6173 (goto-char (match-end 1))
6174 (c-forward-syntactic-ws)))
6176 (when c-opt-type-concat-key
; Only/mainly for pike.
6177 ;; Look for a trailing operator that concatenates the type
6178 ;; with a following one, and if so step past that one through
6179 ;; a recursive call. Note that we don't record concatenated
6180 ;; types in `c-found-types' - it's the component types that
6181 ;; are recorded when appropriate.
6183 (let* ((c-promote-possible-types (or (memq res
'(t known
))
6184 c-promote-possible-types
))
6185 ;; If we can't promote then set `c-record-found-types' so that
6186 ;; we can merge in the types from the second part afterwards if
6187 ;; it turns out to be a known type there.
6188 (c-record-found-types (and c-record-type-identifiers
6189 (not c-promote-possible-types
)))
6191 (if (and (looking-at c-opt-type-concat-key
)
6194 (goto-char (match-end 1))
6195 (c-forward-syntactic-ws)
6196 (setq subres
(c-forward-type))))
6199 ;; If either operand certainly is a type then both are, but we
6200 ;; don't let the existence of the operator itself promote two
6201 ;; uncertain types to a certain one.
6204 (unless (eq name-res
'template
)
6205 (c-add-type id-start id-end
))
6206 (when (and c-record-type-identifiers id-range
)
6207 (c-record-type-id id-range
))
6218 (when (and (eq res t
)
6219 (consp c-record-found-types
))
6220 ;; Merge in the ranges of any types found by the second
6221 ;; `c-forward-type'.
6222 (setq c-record-type-identifiers
6223 ;; `nconc' doesn't mind that the tail of
6224 ;; `c-record-found-types' is t.
6225 (nconc c-record-found-types
6226 c-record-type-identifiers
))))
6230 (when (and c-record-found-types
(memq res
'(known found
)) id-range
)
6231 (setq c-record-found-types
6232 (cons id-range c-record-found-types
))))
6234 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6238 (defun c-forward-annotation ()
6239 ;; Used for Java code only at the moment. Assumes point is on the
6240 ;; @, moves forward an annotation. returns nil if there is no
6241 ;; annotation at point.
6242 (and (looking-at "@")
6243 (progn (forward-char) t
)
6245 (progn (c-forward-syntactic-ws) t
)
6246 (if (looking-at "(")
6251 ;; Handling of large scale constructs like statements and declarations.
6253 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6254 ;; defsubst or perhaps even a defun, but it contains lots of free
6255 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6256 (defmacro c-fdoc-shift-type-backward
(&optional short
)
6257 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6258 ;; of types when parsing a declaration, which means that it
6259 ;; sometimes consumes the identifier in the declaration as a type.
6260 ;; This is used to "backtrack" and make the last type be treated as
6261 ;; an identifier instead.
6264 ;; These identifiers are bound only in the inner let.
6265 '(setq identifier-type at-type
6266 identifier-start type-start
6270 got-suffix-after-parens id-start
6273 (if (setq at-type
(if (eq backup-at-type
'prefix
)
6276 (setq type-start backup-type-start
6277 id-start backup-id-start
)
6278 (setq type-start start-pos
6279 id-start start-pos
))
6281 ;; When these flags already are set we've found specifiers that
6282 ;; unconditionally signal these attributes - backtracking doesn't
6283 ;; change that. So keep them set in that case.
6285 (setq at-type-decl backup-at-type-decl
))
6287 (setq maybe-typeless backup-maybe-typeless
))
6290 ;; This identifier is bound only in the inner let.
6291 '(setq start id-start
))))
6293 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end
)
6294 ;; Move forward over a declaration or a cast if at the start of one.
6295 ;; The point is assumed to be at the start of some token. Nil is
6296 ;; returned if no declaration or cast is recognized, and the point
6297 ;; is clobbered in that case.
6299 ;; If a declaration is parsed:
6301 ;; The point is left at the first token after the first complete
6302 ;; declarator, if there is one. The return value is a cons where
6303 ;; the car is the position of the first token in the declarator. (See
6304 ;; below for the cdr.)
6307 ;; void foo (int a, char *b) stuff ...
6311 ;; unsigned int a = c_style_initializer, b;
6313 ;; unsigned int a (cplusplus_style_initializer), b;
6314 ;; car ^ ^ point (might change)
6315 ;; class Foo : public Bar {}
6317 ;; class PikeClass (int a, string b) stuff ...
6323 ;; void cplusplus_function (int x) throw (Bad);
6325 ;; Foo::Foo (int b) : Base (b) {}
6328 ;; The cdr of the return value is non-nil when a
6329 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6330 ;; Specifically it is a dotted pair (A . B) where B is t when a
6331 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6332 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6333 ;; specifier is present. I.e., (some of) the declared
6334 ;; identifier(s) are types.
6336 ;; If a cast is parsed:
6338 ;; The point is left at the first token after the closing paren of
6339 ;; the cast. The return value is `cast'. Note that the start
6340 ;; position must be at the first token inside the cast parenthesis
6343 ;; PRECEDING-TOKEN-END is the first position after the preceding
6344 ;; token, i.e. on the other side of the syntactic ws from the point.
6345 ;; Use a value less than or equal to (point-min) if the point is at
6346 ;; the first token in (the visible part of) the buffer.
6348 ;; CONTEXT is a symbol that describes the context at the point:
6349 ;; 'decl In a comma-separated declaration context (typically
6350 ;; inside a function declaration arglist).
6351 ;; '<> In an angle bracket arglist.
6352 ;; 'arglist Some other type of arglist.
6353 ;; nil Some other context or unknown context. Includes
6354 ;; within the parens of an if, for, ... construct.
6356 ;; LAST-CAST-END is the first token after the closing paren of a
6357 ;; preceding cast, or nil if none is known. If
6358 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6359 ;; the position after the closest preceding call where a cast was
6360 ;; matched. In that case it's used to discover chains of casts like
6363 ;; This function records identifier ranges on
6364 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6365 ;; `c-record-type-identifiers' is non-nil.
6367 ;; This function might do hidden buffer changes.
6369 (let (;; `start-pos' is used below to point to the start of the
6370 ;; first type, i.e. after any leading specifiers. It might
6371 ;; also point at the beginning of the preceding syntactic
6374 ;; Set to the result of `c-forward-type'.
6376 ;; The position of the first token in what we currently
6377 ;; believe is the type in the declaration or cast, after any
6378 ;; specifiers and their associated clauses.
6380 ;; The position of the first token in what we currently
6381 ;; believe is the declarator for the first identifier. Set
6382 ;; when the type is found, and moved forward over any
6383 ;; `c-decl-hangon-kwds' and their associated clauses that
6384 ;; occurs after the type.
6386 ;; These store `at-type', `type-start' and `id-start' of the
6387 ;; identifier before the one in those variables. The previous
6388 ;; identifier might turn out to be the real type in a
6389 ;; declaration if the last one has to be the declarator in it.
6390 ;; If `backup-at-type' is nil then the other variables have
6391 ;; undefined values.
6392 backup-at-type backup-type-start backup-id-start
6393 ;; Set if we've found a specifier (apart from "typedef") that makes
6394 ;; the defined identifier(s) types.
6396 ;; Set if we've a "typedef" keyword.
6398 ;; Set if we've found a specifier that can start a declaration
6399 ;; where there's no type.
6401 ;; If a specifier is found that also can be a type prefix,
6402 ;; these flags are set instead of those above. If we need to
6403 ;; back up an identifier, they are copied to the real flag
6404 ;; variables. Thus they only take effect if we fail to
6405 ;; interpret it as a type.
6406 backup-at-type-decl backup-maybe-typeless
6407 ;; Whether we've found a declaration or a cast. We might know
6408 ;; this before we've found the type in it. It's 'ids if we've
6409 ;; found two consecutive identifiers (usually a sure sign, but
6410 ;; we should allow that in labels too), and t if we've found a
6411 ;; specifier keyword (a 100% sure sign).
6413 ;; Set when we need to back up to parse this as a declaration
6414 ;; but not as a cast.
6416 ;; For casts, the return position.
6418 ;; Save `c-record-type-identifiers' and
6419 ;; `c-record-ref-identifiers' since ranges are recorded
6420 ;; speculatively and should be thrown away if it turns out
6421 ;; that it isn't a declaration or cast.
6422 (save-rec-type-ids c-record-type-identifiers
)
6423 (save-rec-ref-ids c-record-ref-identifiers
))
6425 (while (c-forward-annotation)
6426 (c-forward-syntactic-ws))
6428 ;; Check for a type. Unknown symbols are treated as possible
6429 ;; types, but they could also be specifiers disguised through
6430 ;; macros like __INLINE__, so we recognize both types and known
6431 ;; specifiers after them too.
6433 (let* ((start (point)) kwd-sym kwd-clause-end found-type
)
6435 ;; Look for a specifier keyword clause.
6436 (when (or (looking-at c-prefix-spec-kwds-re
)
6437 (and (c-major-mode-is 'java-mode
)
6438 (looking-at "@[A-Za-z0-9]+")))
6439 (if (looking-at c-typedef-key
)
6440 (setq at-typedef t
))
6441 (setq kwd-sym
(c-keyword-sym (match-string 1)))
6443 (c-forward-keyword-clause 1)
6444 (setq kwd-clause-end
(point))))
6446 (when (setq found-type
(c-forward-type t
)) ; brace-block-too
6447 ;; Found a known or possible type or a prefix of a known type.
6450 ;; Got two identifiers with nothing but whitespace
6451 ;; between them. That can only happen in declarations.
6452 (setq at-decl-or-cast
'ids
)
6454 (when (eq at-type
'found
)
6455 ;; If the previous identifier is a found type we
6456 ;; record it as a real one; it might be some sort of
6457 ;; alias for a prefix like "unsigned".
6459 (goto-char type-start
)
6460 (let ((c-promote-possible-types t
))
6461 (c-forward-type)))))
6463 (setq backup-at-type at-type
6464 backup-type-start type-start
6465 backup-id-start id-start
6469 ;; The previous ambiguous specifier/type turned out
6470 ;; to be a type since we've parsed another one after
6471 ;; it, so clear these backup flags.
6472 backup-at-type-decl nil
6473 backup-maybe-typeless nil
))
6477 ;; Handle known specifier keywords and
6478 ;; `c-decl-hangon-kwds' which can occur after known
6481 (if (c-keyword-member kwd-sym
'c-decl-hangon-kwds
)
6482 ;; It's a hang-on keyword that can occur anywhere.
6484 (setq at-decl-or-cast t
)
6486 ;; Move the identifier start position if
6487 ;; we've passed a type.
6488 (setq id-start kwd-clause-end
)
6489 ;; Otherwise treat this as a specifier and
6490 ;; move the fallback position.
6491 (setq start-pos kwd-clause-end
))
6492 (goto-char kwd-clause-end
))
6494 ;; It's an ordinary specifier so we know that
6495 ;; anything before this can't be the type.
6496 (setq backup-at-type nil
6497 start-pos kwd-clause-end
)
6500 ;; It's ambiguous whether this keyword is a
6501 ;; specifier or a type prefix, so set the backup
6502 ;; flags. (It's assumed that `c-forward-type'
6503 ;; moved further than `c-forward-keyword-clause'.)
6505 (when (c-keyword-member kwd-sym
'c-typedef-decl-kwds
)
6506 (setq backup-at-type-decl t
))
6507 (when (c-keyword-member kwd-sym
'c-typeless-decl-kwds
)
6508 (setq backup-maybe-typeless t
)))
6510 (when (c-keyword-member kwd-sym
'c-typedef-decl-kwds
)
6511 ;; This test only happens after we've scanned a type.
6512 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6513 (setq at-type-decl t
))
6514 (when (c-keyword-member kwd-sym
'c-typeless-decl-kwds
)
6515 (setq maybe-typeless t
))
6517 ;; Haven't matched a type so it's an unambiguous
6518 ;; specifier keyword and we know we're in a
6520 (setq at-decl-or-cast t
)
6522 (goto-char kwd-clause-end
))))
6524 ;; If the type isn't known we continue so that we'll jump
6525 ;; over all specifiers and type identifiers. The reason
6526 ;; to do this for a known type prefix is to make things
6527 ;; like "unsigned INT16" work.
6528 (and found-type
(not (eq found-type t
))))))
6532 ;; If a known type was found, we still need to skip over any
6533 ;; hangon keyword clauses after it. Otherwise it has already
6534 ;; been done in the loop above.
6535 (while (looking-at c-decl-hangon-key
)
6536 (c-forward-keyword-clause 1))
6537 (setq id-start
(point)))
6539 ((eq at-type
'prefix
)
6540 ;; A prefix type is itself a primitive type when it's not
6541 ;; followed by another type.
6545 ;; Got no type but set things up to continue anyway to handle
6546 ;; the various cases when a declaration doesn't start with a
6548 (setq id-start start-pos
))
6550 ((and (eq at-type
'maybe
)
6551 (c-major-mode-is 'c
++-mode
))
6552 ;; If it's C++ then check if the last "type" ends on the form
6553 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6554 ;; (con|de)structor.
6556 (let (name end-2 end-1
)
6557 (goto-char id-start
)
6558 (c-backward-syntactic-ws)
6559 (setq end-2
(point))
6561 (c-simple-skip-symbol-backward)
6564 (buffer-substring-no-properties (point) end-2
))
6565 ;; Cheating in the handling of syntactic ws below.
6566 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6568 (setq end-1
(point))
6569 (c-simple-skip-symbol-backward))
6570 (>= (point) type-start
)
6571 (equal (buffer-substring-no-properties (point) end-1
)
6573 ;; It is a (con|de)structor name. In that case the
6574 ;; declaration is typeless so zap out any preceding
6575 ;; identifier(s) that we might have taken as types.
6576 (goto-char type-start
)
6579 id-start type-start
))))))
6581 ;; Check for and step over a type decl expression after the thing
6582 ;; that is or might be a type. This can't be skipped since we
6583 ;; need the correct end position of the declarator for
6584 ;; `max-type-decl-end-*'.
6585 (let ((start (point)) (paren-depth 0) pos
6586 ;; True if there's a non-open-paren match of
6587 ;; `c-type-decl-prefix-key'.
6589 ;; True if the declarator is surrounded by a parenthesis pair.
6591 ;; True if there is an identifier in the declarator.
6593 ;; True if there's a non-close-paren match of
6594 ;; `c-type-decl-suffix-key'.
6596 ;; True if there's a prefix match outside the outermost
6597 ;; paren pair that surrounds the declarator.
6598 got-prefix-before-parens
6599 ;; True if there's a suffix match outside the outermost
6600 ;; paren pair that surrounds the declarator. The value is
6601 ;; the position of the first suffix match.
6602 got-suffix-after-parens
6603 ;; True if we've parsed the type decl to a token that is
6604 ;; known to end declarations in this context.
6606 ;; The earlier values of `at-type' and `type-start' if we've
6607 ;; shifted the type backwards.
6608 identifier-type identifier-start
6609 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6610 ;; turn it off during the name skipping below to avoid
6611 ;; getting `c-type' properties that might be bogus. That
6612 ;; can happen since we don't know if
6613 ;; `c-restricted-<>-arglists' will be correct inside the
6614 ;; arglist paren that gets entered.
6615 c-parse-and-markup-
<>-arglists
)
6617 (goto-char id-start
)
6619 ;; Skip over type decl prefix operators. (Note similar code in
6620 ;; `c-font-lock-declarators'.)
6621 (while (and (looking-at c-type-decl-prefix-key
)
6622 (if (and (c-major-mode-is 'c
++-mode
)
6623 (match-beginning 3))
6624 ;; If the second submatch matches in C++ then
6625 ;; we're looking at an identifier that's a
6626 ;; prefix only if it specifies a member pointer.
6627 (when (setq got-identifier
(c-forward-name))
6628 (if (looking-at "\\(::\\)")
6629 ;; We only check for a trailing "::" and
6630 ;; let the "*" that should follow be
6631 ;; matched in the next round.
6632 (progn (setq got-identifier nil
) t
)
6633 ;; It turned out to be the real identifier,
6638 (if (eq (char-after) ?\
()
6640 (setq paren-depth
(1+ paren-depth
))
6642 (unless got-prefix-before-parens
6643 (setq got-prefix-before-parens
(= paren-depth
0)))
6645 (goto-char (match-end 1)))
6646 (c-forward-syntactic-ws))
6648 (setq got-parens
(> paren-depth
0))
6650 ;; Skip over an identifier.
6652 (and (looking-at c-identifier-start
)
6653 (setq got-identifier
(c-forward-name))))
6655 ;; Skip over type decl suffix operators.
6656 (while (if (looking-at c-type-decl-suffix-key
)
6658 (if (eq (char-after) ?\
))
6659 (when (> paren-depth
0)
6660 (setq paren-depth
(1- paren-depth
))
6663 (when (if (save-match-data (looking-at "\\s\("))
6664 (c-safe (c-forward-sexp 1) t
)
6665 (goto-char (match-end 1))
6667 (when (and (not got-suffix-after-parens
)
6669 (setq got-suffix-after-parens
(match-beginning 0)))
6670 (setq got-suffix t
)))
6672 ;; No suffix matched. We might have matched the
6673 ;; identifier as a type and the open paren of a
6674 ;; function arglist as a type decl prefix. In that
6675 ;; case we should "backtrack": Reinterpret the last
6676 ;; type as the identifier, move out of the arglist and
6677 ;; continue searching for suffix operators.
6679 ;; Do this even if there's no preceding type, to cope
6680 ;; with old style function declarations in K&R C,
6681 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6682 ;; style declarations. That isn't applicable in an
6683 ;; arglist context, though.
6684 (when (and (= paren-depth
1)
6685 (not got-prefix-before-parens
)
6686 (not (eq at-type t
))
6689 backup-maybe-typeless
6690 (when c-recognize-typeless-decls
6692 (setq pos
(c-up-list-forward (point)))
6693 (eq (char-before pos
) ?\
)))
6694 (c-fdoc-shift-type-backward)
6698 (c-forward-syntactic-ws))
6700 (when (and (or maybe-typeless backup-maybe-typeless
)
6701 (not got-identifier
)
6704 ;; Have found no identifier but `c-typeless-decl-kwds' has
6705 ;; matched so we know we're inside a declaration. The
6706 ;; preceding type must be the identifier instead.
6707 (c-fdoc-shift-type-backward))
6711 (catch 'at-decl-or-cast
6714 (when (> paren-depth
0)
6715 ;; Encountered something inside parens that isn't matched by
6716 ;; the `c-type-decl-*' regexps, so it's not a type decl
6717 ;; expression. Try to skip out to the same paren depth to
6718 ;; not confuse the cast check below.
6719 (c-safe (goto-char (scan-lists (point) 1 paren-depth
)))
6720 ;; If we've found a specifier keyword then it's a
6721 ;; declaration regardless.
6722 (throw 'at-decl-or-cast
(eq at-decl-or-cast t
)))
6725 (looking-at (cond ((eq context
'<>) "[,>]")
6729 ;; Now we've collected info about various characteristics of
6730 ;; the construct we're looking at. Below follows a decision
6731 ;; tree based on that. It's ordered to check more certain
6732 ;; signs before less certain ones.
6738 (when (and (or at-type maybe-typeless
)
6739 (not (or got-prefix got-parens
)))
6740 ;; Got another identifier directly after the type, so it's a
6742 (throw 'at-decl-or-cast t
))
6744 (when (and got-parens
6746 (not got-suffix-after-parens
)
6749 backup-maybe-typeless
))
6750 ;; Got a declaration of the form "foo bar (gnu);" where we've
6751 ;; recognized "bar" as the type and "gnu" as the declarator.
6752 ;; In this case it's however more likely that "bar" is the
6753 ;; declarator and "gnu" a function argument or initializer (if
6754 ;; `c-recognize-paren-inits' is set), since the parens around
6755 ;; "gnu" would be superfluous if it's a declarator. Shift the
6756 ;; type one step backward.
6757 (c-fdoc-shift-type-backward)))
6759 ;; Found no identifier.
6766 (when (= (point) start
)
6767 ;; Got a plain list of identifiers. If a colon follows it's
6768 ;; a valid label, or maybe a bitfield. Otherwise the last
6769 ;; one probably is the declared identifier and we should
6770 ;; back up to the previous type, providing it isn't a cast.
6771 (if (and (eq (char-after) ?
:)
6772 (not (c-major-mode-is 'java-mode
)))
6774 ;; If we've found a specifier keyword then it's a
6775 ;; declaration regardless.
6776 ((eq at-decl-or-cast t
)
6777 (throw 'at-decl-or-cast t
))
6778 ((and c-has-bitfields
6779 (eq at-decl-or-cast
'ids
)) ; bitfield.
6780 (setq backup-if-not-cast t
)
6781 (throw 'at-decl-or-cast t
)))
6783 (setq backup-if-not-cast t
)
6784 (throw 'at-decl-or-cast t
)))
6787 (when (and got-suffix
6790 ;; Got a plain list of identifiers followed by some suffix.
6791 ;; If this isn't a cast then the last identifier probably is
6792 ;; the declared one and we should back up to the previous
6794 (setq backup-if-not-cast t
)
6795 (throw 'at-decl-or-cast t
)))
6798 (when (eq at-type t
)
6799 ;; If the type is known we know that there can't be any
6800 ;; identifier somewhere else, and it's only in declarations in
6801 ;; e.g. function prototypes and in casts that the identifier may
6803 (throw 'at-decl-or-cast t
))
6805 (when (= (point) start
)
6806 ;; Only got a single identifier (parsed as a type so far).
6809 ;; Check that the identifier isn't at the start of an
6814 ;; Inside an arglist that contains declarations. If K&R
6815 ;; style declarations and parenthesis style initializers
6816 ;; aren't allowed then the single identifier must be a
6817 ;; type, else we require that it's known or found
6818 ;; (primitive types are handled above).
6819 (or (and (not c-recognize-knr-p
)
6820 (not c-recognize-paren-inits
))
6821 (memq at-type
'(known found
))))
6823 ;; Inside a template arglist. Accept known and found
6824 ;; types; other identifiers could just as well be
6825 ;; constants in C++.
6826 (memq at-type
'(known found
)))))
6827 (throw 'at-decl-or-cast t
)
6829 ;; Can't be a valid declaration or cast, but if we've found a
6830 ;; specifier it can't be anything else either, so treat it as
6831 ;; an invalid/unfinished declaration or cast.
6832 (throw 'at-decl-or-cast at-decl-or-cast
))))
6837 (not (eq at-type t
))
6840 backup-maybe-typeless
6841 (when c-recognize-typeless-decls
6842 (or (not got-suffix
)
6844 c-after-suffixed-type-maybe-decl-key
))))))
6845 ;; Got an empty paren pair and a preceding type that probably
6846 ;; really is the identifier. Shift the type backwards to make
6847 ;; the last one the identifier. This is analogous to the
6848 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6851 ;; Exception: In addition to the conditions in that
6852 ;; "backtracking" code, do not shift backward if we're not
6853 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6854 ;; Since there's no preceding type, the shift would mean that
6855 ;; the declaration is typeless. But if the regexp doesn't match
6856 ;; then we will simply fall through in the tests below and not
6857 ;; recognize it at all, so it's better to try it as an abstract
6858 ;; declarator instead.
6859 (c-fdoc-shift-type-backward)
6861 ;; Still no identifier.
6863 (when (and got-prefix
(or got-parens got-suffix
))
6864 ;; Require `got-prefix' together with either `got-parens' or
6865 ;; `got-suffix' to recognize it as an abstract declarator:
6866 ;; `got-parens' only is probably an empty function call.
6867 ;; `got-suffix' only can build an ordinary expression together
6868 ;; with the preceding identifier which we've taken as a type.
6869 ;; We could actually accept on `got-prefix' only, but that can
6870 ;; easily occur temporarily while writing an expression so we
6871 ;; avoid that case anyway. We could do a better job if we knew
6872 ;; the point when the fontification was invoked.
6873 (throw 'at-decl-or-cast t
))
6879 got-suffix-after-parens
6880 (eq (char-after got-suffix-after-parens
) ?\
())
6881 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6882 ;; normal function call after all (or perhaps a C++ style object
6883 ;; instantiation expression).
6884 (throw 'at-decl-or-cast nil
))))
6887 (when at-decl-or-cast
6888 ;; By now we've located the type in the declaration that we know
6890 (throw 'at-decl-or-cast t
))
6893 (when (and got-identifier
6895 (looking-at c-after-suffixed-type-decl-key
)
6899 (not (eq at-type t
)))
6900 ;; Shift the type backward in the case that there's a
6901 ;; single identifier inside parens. That can only
6902 ;; occur in K&R style function declarations so it's
6903 ;; more likely that it really is a function call.
6904 ;; Therefore we only do this after
6905 ;; `c-after-suffixed-type-decl-key' has matched.
6906 (progn (c-fdoc-shift-type-backward) t
)
6907 got-suffix-after-parens
))
6908 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6909 (throw 'at-decl-or-cast t
))
6912 (when (and (or got-prefix
(not got-parens
))
6913 (memq at-type
'(t known
)))
6914 ;; It's a declaration if a known type precedes it and it can't be a
6916 (throw 'at-decl-or-cast t
))
6918 ;; If we get here we can't tell if this is a type decl or a normal
6919 ;; expression by looking at it alone. (That's under the assumption
6920 ;; that normal expressions always can look like type decl expressions,
6921 ;; which isn't really true but the cases where it doesn't hold are so
6922 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6923 ;; the effort to look for them.)
6925 (unless (or at-decl-end
(looking-at "=[^=]"))
6926 ;; If this is a declaration it should end here or its initializer(*)
6927 ;; should start here, so check for allowed separation tokens. Note
6928 ;; that this rule doesn't work e.g. with a K&R arglist after a
6931 ;; *) Don't check for C++ style initializers using parens
6932 ;; since those already have been matched as suffixes.
6934 ;; If `at-decl-or-cast' is then we've found some other sign that
6935 ;; it's a declaration or cast, so then it's probably an
6936 ;; invalid/unfinished one.
6937 (throw 'at-decl-or-cast at-decl-or-cast
))
6939 ;; Below are tests that only should be applied when we're certain to
6940 ;; not have parsed halfway through an expression.
6943 (when (memq at-type
'(t known
))
6944 ;; The expression starts with a known type so treat it as a
6946 (throw 'at-decl-or-cast t
))
6949 (when (and (c-major-mode-is 'c
++-mode
)
6950 ;; In C++ we check if the identifier is a known type, since
6951 ;; (con|de)structors use the class name as identifier.
6952 ;; We've always shifted over the identifier as a type and
6953 ;; then backed up again in this case.
6955 (or (memq identifier-type
'(found known
))
6956 (and (eq (char-after identifier-start
) ?~
)
6957 ;; `at-type' probably won't be 'found for
6958 ;; destructors since the "~" is then part of the
6959 ;; type name being checked against the list of
6960 ;; known types, so do a check without that
6963 (goto-char (1+ identifier-start
))
6964 (c-forward-syntactic-ws)
6965 (c-with-syntax-table
6966 c-identifier-syntax-table
6967 (looking-at c-known-type-key
)))
6969 (goto-char (1+ identifier-start
))
6970 ;; We have already parsed the type earlier,
6971 ;; so it'd be possible to cache the end
6972 ;; position instead of redoing it here, but
6973 ;; then we'd need to keep track of another
6974 ;; position everywhere.
6975 (c-check-type (point)
6976 (progn (c-forward-type)
6978 (throw 'at-decl-or-cast t
))
6983 (when (and got-prefix-before-parens
6985 (or at-decl-end
(looking-at "=[^=]"))
6988 ;; Got something like "foo * bar;". Since we're not inside an
6989 ;; arglist it would be a meaningless expression because the
6990 ;; result isn't used. We therefore choose to recognize it as
6991 ;; a declaration. Do not allow a suffix since it could then
6992 ;; be a function call.
6993 (throw 'at-decl-or-cast t
))
6996 (when (and (or got-suffix-after-parens
6997 (looking-at "=[^=]"))
6999 (not (eq context
'arglist
)))
7000 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
7001 ;; be an odd expression or it could be a declaration. Treat
7002 ;; it as a declaration if "a" has been used as a type
7003 ;; somewhere else (if it's a known type we won't get here).
7004 (throw 'at-decl-or-cast t
)))
7009 (and (eq context
'decl
)
7010 (not c-recognize-paren-inits
)
7011 (or got-parens got-suffix
))))
7012 ;; Got a type followed by an abstract declarator. If `got-prefix'
7013 ;; is set it's something like "a *" without anything after it. If
7014 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
7015 ;; or similar, which we accept only if the context rules out
7017 (throw 'at-decl-or-cast t
)))
7019 ;; If we had a complete symbol table here (which rules out
7020 ;; `c-found-types') we should return t due to the disambiguation rule
7021 ;; (in at least C++) that anything that can be parsed as a declaration
7022 ;; is a declaration. Now we're being more defensive and prefer to
7023 ;; highlight things like "foo (bar);" as a declaration only if we're
7024 ;; inside an arglist that contains declarations.
7025 (eq context
'decl
))))
7027 ;; The point is now after the type decl expression.
7030 ;; Check for a cast.
7035 ;; Should be the first type/identifier in a cast paren.
7036 (> preceding-token-end
(point-min))
7037 (memq (char-before preceding-token-end
) c-cast-parens
)
7039 ;; The closing paren should follow.
7041 (c-forward-syntactic-ws)
7042 (looking-at "\\s\)"))
7044 ;; There should be a primary expression after it.
7047 (c-forward-syntactic-ws)
7048 (setq cast-end
(point))
7049 (and (looking-at c-primary-expr-regexp
)
7051 (setq pos
(match-end 0))
7053 ;; Check if the expression begins with a prefix keyword.
7055 (if (match-beginning 1)
7056 ;; Expression begins with an ambiguous operator. Treat
7057 ;; it as a cast if it's a type decl or if we've
7058 ;; recognized the type somewhere else.
7060 (memq at-type
'(t known found
)))
7061 ;; Unless it's a keyword, it's the beginning of a primary
7063 (not (looking-at c-keywords-regexp
)))))
7064 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
7065 ;; that it matched a whole one so that we don't e.g. confuse
7066 ;; the operator '-' with '->'. It's ok if it matches further,
7067 ;; though, since it e.g. can match the float '.5' while the
7068 ;; operator regexp only matches '.'.
7069 (or (not (looking-at c-nonsymbol-token-regexp
))
7070 (<= (match-end 0) pos
))))
7072 ;; There should either be a cast before it or something that isn't an
7073 ;; identifier or close paren.
7074 (> preceding-token-end
(point-min))
7076 (goto-char (1- preceding-token-end
))
7077 (or (eq (point) last-cast-end
)
7079 (c-backward-syntactic-ws)
7080 (if (< (skip-syntax-backward "w_") 0)
7081 ;; It's a symbol. Accept it only if it's one of the
7082 ;; keywords that can precede an expression (without
7083 ;; surrounding parens).
7084 (looking-at c-simple-stmt-key
)
7086 ;; Check that it isn't a close paren (block close is ok,
7088 (not (memq (char-before) '(?\
) ?\
])))
7089 ;; Check that it isn't a nonsymbol identifier.
7090 (not (c-on-identifier)))))))))
7093 (when (and c-record-type-identifiers at-type
(not (eq at-type t
)))
7094 (let ((c-promote-possible-types t
))
7095 (goto-char type-start
)
7098 (goto-char cast-end
)
7102 ;; We're at a declaration. Highlight the type and the following
7105 (when backup-if-not-cast
7106 (c-fdoc-shift-type-backward t
))
7108 (when (and (eq context
'decl
) (looking-at ","))
7109 ;; Make sure to propagate the `c-decl-arg-start' property to
7110 ;; the next argument if it's set in this one, to cope with
7111 ;; interactive refontification.
7112 (c-put-c-type-property (point) 'c-decl-arg-start
))
7114 (when (and c-record-type-identifiers at-type
(not (eq at-type t
)))
7115 (let ((c-promote-possible-types t
))
7117 (goto-char type-start
)
7121 (and (or at-type-decl at-typedef
)
7122 (cons at-type-decl at-typedef
))))
7125 ;; False alarm. Restore the recorded ranges.
7126 (setq c-record-type-identifiers save-rec-type-ids
7127 c-record-ref-identifiers save-rec-ref-ids
)
7130 (defun c-forward-label (&optional assume-markup preceding-token-end limit
)
7131 ;; Assuming that point is at the beginning of a token, check if it starts a
7132 ;; label and if so move over it and return non-nil (t in default situations,
7133 ;; specific symbols (see below) for interesting situations), otherwise don't
7134 ;; move and return nil. "Label" here means "most things with a colon".
7136 ;; More precisely, a "label" is regarded as one of:
7137 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7138 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7139 ;; bare "case", should the colon be missing. We return t;
7140 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7142 ;; (iv) One of QT's "extended" C++ variants of
7143 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7144 ;; Returns the symbol `qt-2kwds-colon'.
7145 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
7146 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
7147 ;; colon). Currently (2006-03), this applies only to Objective C's
7148 ;; keywords "@private", "@protected", and "@public". Returns t.
7150 ;; One of the things which will NOT be recognized as a label is a bit-field
7151 ;; element of a struct, something like "int foo:5".
7153 ;; The end of the label is taken to be just after the colon, or the end of
7154 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7155 ;; after the end on return. The terminating char gets marked with
7156 ;; `c-decl-end' to improve recognition of the following declaration or
7159 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7160 ;; label, if any, has already been marked up like that.
7162 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7163 ;; after the preceding token, i.e. on the other side of the
7164 ;; syntactic ws from the point. Use a value less than or equal to
7165 ;; (point-min) if the point is at the first token in (the visible
7166 ;; part of) the buffer.
7168 ;; The optional LIMIT limits the forward scan for the colon.
7170 ;; This function records the ranges of the label symbols on
7171 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7174 ;; This function might do hidden buffer changes.
7176 (let ((start (point))
7179 macro-start
; if we're in one.
7183 ;; "case" or "default" (Doesn't apply to AWK).
7184 ((looking-at c-label-kwds-regexp
)
7185 (let ((kwd-end (match-end 1)))
7186 ;; Record only the keyword itself for fontification, since in
7187 ;; case labels the following is a constant expression and not
7189 (when c-record-type-identifiers
7190 (c-record-ref-id (cons (match-beginning 1) kwd-end
)))
7192 ;; Find the label end.
7195 (if (and (c-syntactic-re-search-forward
7196 ;; Stop on chars that aren't allowed in expressions,
7197 ;; and on operator chars that would be meaningless
7198 ;; there. FIXME: This doesn't cope with ?: operators.
7199 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7201 (match-beginning 2))
7203 (progn ; there's a proper :
7204 (goto-char (match-beginning 2)) ; just after the :
7205 (c-put-c-type-property (1- (point)) 'c-decl-end
)
7208 ;; It's an unfinished label. We consider the keyword enough
7209 ;; to recognize it as a label, so that it gets fontified.
7210 ;; Leave the point at the end of it, but don't put any
7211 ;; `c-decl-end' marker.
7215 ;; @private, @protected, @public, in Objective C, or similar.
7216 ((and c-opt-extra-label-key
7217 (looking-at c-opt-extra-label-key
))
7218 ;; For a `c-opt-extra-label-key' match, we record the whole
7219 ;; thing for fontification. That's to get the leading '@' in
7220 ;; Objective-C protection labels fontified.
7221 (goto-char (match-end 1))
7222 (when c-record-type-identifiers
7223 (c-record-ref-id (cons (match-beginning 1) (point))))
7224 (c-put-c-type-property (1- (point)) 'c-decl-end
)
7225 (setq label-type t
))
7227 ;; All other cases of labels.
7228 ((and c-recognize-colon-labels
; nil for AWK and IDL, otherwise t.
7230 ;; A colon label must have something before the colon.
7231 (not (eq (char-after) ?
:))
7233 ;; Check that we're not after a token that can't precede a label.
7235 ;; Trivially succeeds when there's no preceding token.
7236 ;; Succeeds when we're at a virtual semicolon.
7237 (if preceding-token-end
7238 (<= preceding-token-end
(point-min))
7240 (c-backward-syntactic-ws)
7241 (setq preceding-token-end
(point))
7245 ;; Check if we're after a label, if we're after a closing
7246 ;; paren that belong to statement, and with
7247 ;; `c-label-prefix-re'. It's done in different order
7248 ;; depending on `assume-markup' since the checks have
7249 ;; different expensiveness.
7252 (eq (c-get-char-property (1- preceding-token-end
) 'c-type
)
7256 (goto-char (1- preceding-token-end
))
7257 (c-beginning-of-current-token)
7258 (or (looking-at c-label-prefix-re
)
7259 (looking-at c-block-stmt-1-key
)))
7261 (and (eq (char-before preceding-token-end
) ?\
))
7262 (c-after-conditional)))
7266 (goto-char (1- preceding-token-end
))
7267 (c-beginning-of-current-token)
7268 (or (looking-at c-label-prefix-re
)
7269 (looking-at c-block-stmt-1-key
)))
7272 ((eq (char-before preceding-token-end
) ?\
))
7273 (c-after-conditional))
7275 ((eq (char-before preceding-token-end
) ?
:)
7276 ;; Might be after another label, so check it recursively.
7279 (goto-char (1- preceding-token-end
))
7280 ;; Essentially the same as the
7281 ;; `c-syntactic-re-search-forward' regexp below.
7283 (save-excursion (and (c-beginning-of-macro)
7285 (if macro-start
(narrow-to-region macro-start
(point-max)))
7286 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t
)
7287 ;; Note: the following should work instead of the
7288 ;; narrow-to-region above. Investigate why not,
7289 ;; sometime. ACM, 2006-03-31.
7290 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7293 ;; If the caller turned on recording for us,
7294 ;; it shouldn't apply when we check the
7296 c-record-type-identifiers
)
7297 ;; A label can't start at a cpp directive. Check for
7298 ;; this, since c-forward-syntactic-ws would foul up on it.
7299 (unless (and c-opt-cpp-prefix
(looking-at c-opt-cpp-prefix
))
7300 (c-forward-syntactic-ws)
7301 (c-forward-label nil pte start
))))))))))
7303 ;; Point is still at the beginning of the possible label construct.
7305 ;; Check that the next nonsymbol token is ":", or that we're in one
7306 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7307 ;; arguments. FIXME: Should build this regexp from the language
7310 ;; public: protected: private:
7312 (c-major-mode-is 'c
++-mode
)
7313 (search-forward-regexp
7314 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t
)
7315 (progn (backward-char)
7316 (c-forward-syntactic-ws limit
)
7317 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7319 (setq label-type t
))
7320 ;; QT double keyword like "protected slots:" or goto target.
7321 ((progn (goto-char start
) nil
))
7322 ((when (c-syntactic-re-search-forward
7323 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t
) ; not at EOB
7325 (setq label-end
(point))
7327 (and (c-major-mode-is 'c
++-mode
)
7329 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7330 (buffer-substring start
(point)))))
7331 (c-forward-syntactic-ws limit
)
7333 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7336 (if (or (string= "signals" ; Special QT macro
7337 (setq kwd
(buffer-substring-no-properties start label-end
)))
7338 (string= "Q_SIGNALS" kwd
))
7342 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t
)
7343 (progn (c-forward-syntactic-ws limit
)
7344 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7346 (setq label-type
'qt-2kwds-colon
)))))))
7349 (narrow-to-region start
(point))
7351 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7355 (when (looking-at c-nonlabel-token-key
)
7357 (setq label-type nil
)
7358 (throw 'check-label nil
))
7359 (and (c-safe (c-forward-sexp)
7360 (c-forward-syntactic-ws)
7364 ;; Record the identifiers in the label for fontification, unless
7365 ;; it begins with `c-label-kwds' in which case the following
7366 ;; identifiers are part of a (constant) expression that
7367 ;; shouldn't be fontified.
7368 (when (and c-record-type-identifiers
7369 (progn (goto-char start
)
7370 (not (looking-at c-label-kwds-regexp
))))
7371 (while (c-syntactic-re-search-forward c-symbol-key nil t
)
7372 (c-record-ref-id (cons (match-beginning 0)
7375 (c-put-c-type-property (1- (point-max)) 'c-decl-end
)
7376 (goto-char (point-max)))))
7383 (defun c-forward-objc-directive ()
7384 ;; Assuming the point is at the beginning of a token, try to move
7385 ;; forward to the end of the Objective-C directive that starts
7386 ;; there. Return t if a directive was fully recognized, otherwise
7387 ;; the point is moved as far as one could be successfully parsed and
7390 ;; This function records identifier ranges on
7391 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7392 ;; `c-record-type-identifiers' is non-nil.
7394 ;; This function might do hidden buffer changes.
7396 (let ((start (point))
7398 (c-promote-possible-types t
)
7400 ;; Turn off recognition of angle bracket arglists while parsing
7401 ;; types here since the protocol reference list might then be
7402 ;; considered part of the preceding name or superclass-name.
7403 c-recognize-
<>-arglists
)
7408 (c-make-keywords-re t
7409 (append (c-lang-const c-protection-kwds objc
)
7412 (goto-char (match-end 1))
7418 (c-make-keywords-re t
7419 '("@interface" "@implementation" "@protocol")
7422 ;; Handle the name of the class itself.
7424 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7426 (goto-char (match-end 0))
7432 ;; Look for ": superclass-name" or "( category-name )".
7433 (when (looking-at "[:\(]")
7434 (setq start-char
(char-after))
7436 (c-forward-syntactic-ws)
7437 (unless (c-forward-type) (throw 'break nil
))
7438 (when (eq start-char ?\
()
7439 (unless (eq (char-after) ?\
)) (throw 'break nil
))
7441 (c-forward-syntactic-ws)))
7443 ;; Look for a protocol reference list.
7444 (if (eq (char-after) ?
<)
7445 (let ((c-recognize-<>-arglists t
)
7446 (c-parse-and-markup-<>-arglists t
)
7447 c-restricted-
<>-arglists
)
7448 (c-forward-<>-arglist t
))
7452 (c-backward-syntactic-ws lim
)
7453 (c-clear-c-type-property start
(1- (point)) 'c-decl-end
)
7454 (c-put-c-type-property (1- (point)) 'c-decl-end
)
7457 (c-clear-c-type-property start
(point) 'c-decl-end
)
7460 (defun c-beginning-of-inheritance-list (&optional lim
)
7461 ;; Go to the first non-whitespace after the colon that starts a
7462 ;; multiple inheritance introduction. Optional LIM is the farthest
7463 ;; back we should search.
7465 ;; This function might do hidden buffer changes.
7466 (c-with-syntax-table c
++-template-syntax-table
7467 (c-backward-token-2 0 t lim
)
7468 (while (and (or (looking-at c-symbol-start
)
7469 (looking-at "[<,]\\|::"))
7470 (zerop (c-backward-token-2 1 t lim
))))))
7472 (defun c-in-method-def-p ()
7473 ;; Return nil if we aren't in a method definition, otherwise the
7474 ;; position of the initial [+-].
7476 ;; This function might do hidden buffer changes.
7479 (and c-opt-method-key
7480 (looking-at c-opt-method-key
)
7484 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7485 (defun c-in-gcc-asm-p ()
7486 ;; Return non-nil if point is within a gcc \"asm\" block.
7488 ;; This should be called with point inside an argument list.
7490 ;; Only one level of enclosing parentheses is considered, so for
7491 ;; instance `nil' is returned when in a function call within an asm
7494 ;; This function might do hidden buffer changes.
7496 (and c-opt-asm-stmt-key
7499 (backward-up-list 1)
7500 (c-beginning-of-statement-1 (point-min) nil t
)
7501 (looking-at c-opt-asm-stmt-key
))))
7503 (defun c-at-toplevel-p ()
7504 "Return a determination as to whether point is \"at the top level\".
7505 Informally, \"at the top level\" is anywhere where you can write
7508 More precisely, being at the top-level means that point is either
7509 outside any enclosing block (such as a function definition), or
7510 directly inside a class, namespace or other block that contains
7511 another declaration level.
7513 If point is not at the top-level (e.g. it is inside a method
7514 definition), then nil is returned. Otherwise, if point is at a
7515 top-level not enclosed within a class definition, t is returned.
7516 Otherwise, a 2-vector is returned where the zeroth element is the
7517 buffer position of the start of the class declaration, and the first
7518 element is the buffer position of the enclosing class's opening
7521 Note that this function might do hidden buffer changes. See the
7522 comment at the start of cc-engine.el for more info."
7523 (let ((paren-state (c-parse-state)))
7524 (or (not (c-most-enclosing-brace paren-state
))
7525 (c-search-uplist-for-classkey paren-state
))))
7527 (defun c-just-after-func-arglist-p (&optional lim
)
7528 ;; Return non-nil if the point is in the region after the argument
7529 ;; list of a function and its opening brace (or semicolon in case it
7530 ;; got no body). If there are K&R style argument declarations in
7531 ;; that region, the point has to be inside the first one for this
7532 ;; function to recognize it.
7534 ;; If successful, the point is moved to the first token after the
7535 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7536 ;; the position of the opening paren of the function arglist is
7539 ;; The point is clobbered if not successful.
7541 ;; LIM is used as bound for backward buffer searches.
7543 ;; This function might do hidden buffer changes.
7545 (let ((beg (point)) end id-start
)
7547 (eq (c-beginning-of-statement-1 lim
) 'same
)
7549 (not (or (c-major-mode-is 'objc-mode
)
7550 (c-forward-objc-directive)))
7553 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
)))
7556 ;; There should not be a '=' or ',' between beg and the
7557 ;; start of the declaration since that means we were in the
7558 ;; "expression part" of the declaration.
7560 (not (looking-at "[=,]")))
7563 ;; Check that there's an arglist paren in the
7565 (goto-char id-start
)
7566 (cond ((eq (char-after) ?\
()
7567 ;; The declarator is a paren expression, so skip past it
7568 ;; so that we don't get stuck on that instead of the
7569 ;; function arglist.
7571 ((and c-opt-op-identifier-prefix
7572 (looking-at c-opt-op-identifier-prefix
))
7573 ;; Don't trip up on "operator ()".
7574 (c-forward-token-2 2 t
)))
7575 (and (< (point) beg
)
7576 (c-syntactic-re-search-forward "(" beg t t
)
7579 (defun c-in-knr-argdecl (&optional lim
)
7580 ;; Return the position of the first argument declaration if point is
7581 ;; inside a K&R style argument declaration list, nil otherwise.
7582 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7583 ;; position that bounds the backward search for the argument list.
7585 ;; Point must be within a possible K&R region, e.g. just before a top-level
7586 ;; "{". It must be outside of parens and brackets. The test can return
7587 ;; false positives otherwise.
7589 ;; This function might do hidden buffer changes.
7593 ;; If we're in a macro, our search range is restricted to it. Narrow to
7594 ;; the searchable range.
7595 (let* ((macro-start (c-query-macro-start))
7596 (lim (max (or lim
(point-min)) (or macro-start
(point-min))))
7597 before-lparen after-rparen
7598 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
7599 (narrow-to-region lim
(c-point 'eol
))
7601 ;; Search backwards for the defun's argument list. We give up if we
7602 ;; encounter a "}" (end of a previous defun) or BOB.
7604 ;; The criterion for a paren structure being the arg list is:
7605 ;; o - there is non-WS stuff after it but before any "{"; AND
7606 ;; o - the token after it isn't a ";" AND
7607 ;; o - it is preceded by either an identifier (the function name) or
7608 ;; a macro expansion like "DEFUN (...)"; AND
7609 ;; o - its content is a non-empty comma-separated list of identifiers
7610 ;; (an empty arg list won't have a knr region).
7612 ;; The following snippet illustrates these rules:
7613 ;; int foo (bar, baz, yuk)
7615 ;; int (*baz) (my_type) ;
7616 ;; int (*) (void) (*yuk) (void) ;
7620 (while (> pp-count-out
0) ; go back one paren/bracket pair each time.
7621 (setq pp-count-out
(1- pp-count-out
))
7622 (c-syntactic-skip-backward "^)]}")
7623 (cond ((eq (char-before) ?\
))
7624 (setq after-rparen
(point)))
7625 ((eq (char-before) ?\
])
7626 (setq after-rparen nil
))
7627 (t ; either } (hit previous defun) or no more parens/brackets
7631 ;; We're inside a paren. Could it be our argument list....?
7635 (goto-char after-rparen
)
7636 (unless (c-go-list-backward) (throw 'knr nil
)) ;
7637 ;; FIXME!!! What about macros between the parens? 2007/01/20
7638 (setq before-lparen
(point)))
7640 ;; It can't be the arg list if next token is ; or {
7641 (progn (goto-char after-rparen
)
7642 (c-forward-syntactic-ws)
7643 (not (memq (char-after) '(?\
; ?\{))))
7645 ;; Is the thing preceding the list an identifier (the
7646 ;; function name), or a macro expansion?
7648 (goto-char before-lparen
)
7649 (eq (c-backward-token-2) 0)
7650 (or (c-on-identifier)
7651 (and (eq (char-after) ?\
))
7652 (c-go-up-list-backward)
7653 (eq (c-backward-token-2) 0)
7654 (c-on-identifier))))
7656 ;; Have we got a non-empty list of comma-separated
7659 (goto-char before-lparen
)
7660 (c-forward-token-2) ; to first token inside parens
7665 (while (eq (char-after) ?\
,)
7667 (unless (c-on-identifier) (throw 'id-list nil
))
7668 (c-forward-token-2))
7669 (eq (char-after) ?\
))))))
7671 ;; ...Yes. We've identified the function's argument list.
7673 (progn (goto-char after-rparen
)
7674 (c-forward-syntactic-ws)
7677 ;; ...No. The current parens aren't the function's arg list.
7678 (goto-char before-lparen
))
7680 (or (c-go-list-backward) ; backwards over [ .... ]
7681 (throw 'knr nil
)))))))))
7683 (defun c-skip-conditional ()
7684 ;; skip forward over conditional at point, including any predicate
7685 ;; statements in parentheses. No error checking is performed.
7687 ;; This function might do hidden buffer changes.
7688 (c-forward-sexp (cond
7690 ((looking-at (concat "\\<else"
7691 "\\([ \t\n]\\|\\\\\n\\)+"
7692 "if\\>\\([^_]\\|$\\)"))
7694 ;; do, else, try, finally
7695 ((looking-at (concat "\\<\\("
7696 "do\\|else\\|try\\|finally"
7697 "\\)\\>\\([^_]\\|$\\)"))
7699 ;; for, if, while, switch, catch, synchronized, foreach
7702 (defun c-after-conditional (&optional lim
)
7703 ;; If looking at the token after a conditional then return the
7704 ;; position of its start, otherwise return nil.
7706 ;; This function might do hidden buffer changes.
7708 (and (zerop (c-backward-token-2 1 t lim
))
7709 (or (looking-at c-block-stmt-1-key
)
7710 (and (eq (char-after) ?\
()
7711 (zerop (c-backward-token-2 1 t lim
))
7712 (looking-at c-block-stmt-2-key
)))
7715 (defun c-after-special-operator-id (&optional lim
)
7716 ;; If the point is after an operator identifier that isn't handled
7717 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7718 ;; position of the start of that identifier is returned. nil is
7719 ;; returned otherwise. The point may be anywhere in the syntactic
7720 ;; whitespace after the last token of the operator identifier.
7722 ;; This function might do hidden buffer changes.
7724 (and c-overloadable-operators-regexp
7725 (zerop (c-backward-token-2 1 nil lim
))
7726 (looking-at c-overloadable-operators-regexp
)
7727 (or (not c-opt-op-identifier-prefix
)
7729 (zerop (c-backward-token-2 1 nil lim
))
7730 (looking-at c-opt-op-identifier-prefix
)))
7733 (defsubst c-backward-to-block-anchor
(&optional lim
)
7734 ;; Assuming point is at a brace that opens a statement block of some
7735 ;; kind, move to the proper anchor point for that block. It might
7736 ;; need to be adjusted further by c-add-stmt-syntax, but the
7737 ;; position at return is suitable as start position for that
7740 ;; This function might do hidden buffer changes.
7741 (unless (= (point) (c-point 'boi
))
7742 (let ((start (c-after-conditional lim
)))
7744 (goto-char start
)))))
7746 (defsubst c-backward-to-decl-anchor
(&optional lim
)
7747 ;; Assuming point is at a brace that opens the block of a top level
7748 ;; declaration of some kind, move to the proper anchor point for
7751 ;; This function might do hidden buffer changes.
7752 (unless (= (point) (c-point 'boi
))
7753 (c-beginning-of-statement-1 lim
)))
7755 (defun c-search-decl-header-end ()
7756 ;; Search forward for the end of the "header" of the current
7757 ;; declaration. That's the position where the definition body
7758 ;; starts, or the first variable initializer, or the ending
7759 ;; semicolon. I.e. search forward for the closest following
7760 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7761 ;; _after_ the first found token, or at point-max if none is found.
7763 ;; This function might do hidden buffer changes.
7765 (let ((base (point)))
7766 (if (c-major-mode-is 'c
++-mode
)
7768 ;; In C++ we need to take special care to handle operator
7769 ;; tokens and those pesky template brackets.
7771 (c-syntactic-re-search-forward "[;{<=]" nil
'move t t
)
7773 (c-end-of-current-token base
)
7774 ;; Handle operator identifiers, i.e. ignore any
7775 ;; operator token preceded by "operator".
7777 (and (c-safe (c-backward-sexp) t
)
7778 (looking-at c-opt-op-identifier-prefix
)))
7779 (and (eq (char-before) ?
<)
7780 (c-with-syntax-table c
++-template-syntax-table
7781 (if (c-safe (goto-char (c-up-list-forward (point))))
7783 (goto-char (point-max))
7785 (setq base
(point)))
7788 (c-syntactic-re-search-forward "[;{=]" nil
'move t t
)
7789 (c-end-of-current-token base
))
7790 (setq base
(point))))))
7792 (defun c-beginning-of-decl-1 (&optional lim
)
7793 ;; Go to the beginning of the current declaration, or the beginning
7794 ;; of the previous one if already at the start of it. Point won't
7795 ;; be moved out of any surrounding paren. Return a cons cell of the
7796 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7797 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7798 ;; style argument declarations (and they are to be recognized) then
7799 ;; KNR-POS is set to the start of the first such argument
7800 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7801 ;; position that bounds the backward search.
7803 ;; NB: Cases where the declaration continues after the block, as in
7804 ;; "struct foo { ... } bar;", are currently recognized as two
7805 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7807 ;; This function might do hidden buffer changes.
7809 (let* ((start (point))
7810 (last-stmt-start (point))
7811 (move (c-beginning-of-statement-1 lim nil t
)))
7813 ;; `c-beginning-of-statement-1' stops at a block start, but we
7814 ;; want to continue if the block doesn't begin a top level
7815 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7816 ;; or an open paren.
7817 (let ((beg (point)) tentative-move
)
7818 ;; Go back one "statement" each time round the loop until we're just
7819 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7820 ;; an ObjC method. This will move over a multiple declaration whose
7821 ;; components are comma separated.
7823 ;; Must check with c-opt-method-key in ObjC mode.
7824 (not (and c-opt-method-key
7825 (looking-at c-opt-method-key
)))
7826 (/= last-stmt-start
(point))
7828 (c-backward-syntactic-ws lim
)
7829 (not (memq (char-before) '(?\
; ?} ?: nil))))
7832 (not (looking-at "\\s(")))
7833 ;; Check that we don't move from the first thing in a
7834 ;; macro to its header.
7835 (not (eq (setq tentative-move
7836 (c-beginning-of-statement-1 lim nil t
))
7838 (setq last-stmt-start beg
7840 move tentative-move
))
7843 (when c-recognize-knr-p
7844 (let ((fallback-pos (point)) knr-argdecl-start
)
7845 ;; Handle K&R argdecls. Back up after the "statement" jumped
7846 ;; over by `c-beginning-of-statement-1', unless it was the
7847 ;; function body, in which case we're sitting on the opening
7848 ;; brace now. Then test if we're in a K&R argdecl region and
7849 ;; that we started at the other side of the first argdecl in
7851 (unless (eq (char-after) ?
{)
7852 (goto-char last-stmt-start
))
7853 (if (and (setq knr-argdecl-start
(c-in-knr-argdecl lim
))
7854 (< knr-argdecl-start start
)
7856 (goto-char knr-argdecl-start
)
7857 (not (eq (c-beginning-of-statement-1 lim nil t
) 'macro
))))
7859 (cons (if (eq (char-after fallback-pos
) ?
{)
7863 (goto-char fallback-pos
))))
7865 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7866 ;; statement, so the result will be 'previous if we've moved over any.
7867 ;; So change our result back to 'same if necessary.
7869 ;; If they were brace list initializers we might not have moved over a
7870 ;; declaration boundary though, so change it to 'same if we've moved
7871 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7872 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7873 ;; potentially can search over a large amount of text.). Take special
7874 ;; pains not to get mislead by C++'s "operator=", and the like.
7875 (if (and (eq move
'previous
)
7876 (c-with-syntax-table (if (c-major-mode-is 'c
++-mode
)
7877 c
++-template-syntax-table
7882 (while ; keep going back to "[;={"s until we either find
7883 ; no more, or get to one which isn't an "operator ="
7884 (and (c-syntactic-re-search-forward "[;={]" start t t t
)
7885 (eq (char-before) ?
=)
7886 c-overloadable-operators-regexp
7887 c-opt-op-identifier-prefix
7889 (eq (c-backward-token-2) 0)
7890 (looking-at c-overloadable-operators-regexp
)
7891 (eq (c-backward-token-2) 0)
7892 (looking-at c-opt-op-identifier-prefix
))))
7893 (eq (char-before) ?
=))
7894 (c-syntactic-re-search-forward "[;{]" start t t
)
7895 (eq (char-before) ?
{)
7896 (c-safe (goto-char (c-up-list-forward (point))) t
)
7897 (not (c-syntactic-re-search-forward ";" start t t
))))))
7901 (defun c-end-of-decl-1 ()
7902 ;; Assuming point is at the start of a declaration (as detected by
7903 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7904 ;; `c-beginning-of-decl-1', this function handles the case when a
7905 ;; block is followed by identifiers in e.g. struct declarations in C
7906 ;; or C++. If a proper end was found then t is returned, otherwise
7907 ;; point is moved as far as possible within the current sexp and nil
7908 ;; is returned. This function doesn't handle macros; use
7909 ;; `c-end-of-macro' instead in those cases.
7911 ;; This function might do hidden buffer changes.
7912 (let ((start (point))
7913 (decl-syntax-table (if (c-major-mode-is 'c
++-mode
)
7914 c
++-template-syntax-table
7917 (c-search-decl-header-end)
7919 (when (and c-recognize-knr-p
7920 (eq (char-before) ?\
;)
7921 (c-in-knr-argdecl start
))
7922 ;; Stopped at the ';' in a K&R argdecl section which is
7923 ;; detected using the same criteria as in
7924 ;; `c-beginning-of-decl-1'. Move to the following block
7926 (c-syntactic-re-search-forward "{" nil
'move t
))
7928 (when (eq (char-before) ?
{)
7929 ;; Encountered a block in the declaration. Jump over it.
7931 (goto-char (c-up-list-forward (point)))
7932 (error (goto-char (point-max))
7933 (throw 'return nil
)))
7934 (if (or (not c-opt-block-decls-with-vars-key
)
7936 (c-with-syntax-table decl-syntax-table
7937 (let ((lim (point)))
7940 ;; Check for `c-opt-block-decls-with-vars-key'
7941 ;; before the first paren.
7942 (c-syntactic-re-search-forward
7943 (concat "[;=\(\[{]\\|\\("
7944 c-opt-block-decls-with-vars-key
7948 (not (eq (char-before) ?_
))
7949 ;; Check that the first following paren is
7951 (c-syntactic-re-search-forward "[;=\(\[{]"
7953 (eq (char-before) ?
{)))))))
7954 ;; The declaration doesn't have any of the
7955 ;; `c-opt-block-decls-with-vars' keywords in the
7956 ;; beginning, so it ends here at the end of the block.
7959 (c-with-syntax-table decl-syntax-table
7961 (if (eq (char-before) ?\
;)
7963 (c-syntactic-re-search-forward ";" nil
'move t
))))
7966 (defun c-looking-at-decl-block (containing-sexp goto-start
&optional limit
)
7967 ;; Assuming the point is at an open brace, check if it starts a
7968 ;; block that contains another declaration level, i.e. that isn't a
7969 ;; statement block or a brace list, and if so return non-nil.
7971 ;; If the check is successful, the return value is the start of the
7972 ;; keyword that tells what kind of construct it is, i.e. typically
7973 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7974 ;; the point will be at the start of the construct, before any
7975 ;; leading specifiers, otherwise it's at the returned position.
7977 ;; The point is clobbered if the check is unsuccessful.
7979 ;; CONTAINING-SEXP is the position of the open of the surrounding
7980 ;; paren, or nil if none.
7982 ;; The optional LIMIT limits the backward search for the start of
7983 ;; the construct. It's assumed to be at a syntactically relevant
7986 ;; If any template arglists are found in the searched region before
7987 ;; the open brace, they get marked with paren syntax.
7989 ;; This function might do hidden buffer changes.
7991 (let ((open-brace (point)) kwd-start first-specifier-pos
)
7992 (c-syntactic-skip-backward c-block-prefix-charset limit t
)
7994 (when (and c-recognize-
<>-arglists
7995 (eq (char-before) ?
>))
7996 ;; Could be at the end of a template arglist.
7997 (let ((c-parse-and-markup-<>-arglists t
)
7998 (c-disallow-comma-in-<>-arglists
7999 (and containing-sexp
8000 (not (eq (char-after containing-sexp
) ?
{)))))
8002 (c-backward-<>-arglist nil limit
)
8004 (c-syntactic-skip-backward c-block-prefix-charset limit t
)
8005 (eq (char-before) ?
>))))))
8007 ;; Note: Can't get bogus hits inside template arglists below since they
8008 ;; have gotten paren syntax above.
8010 ;; If `goto-start' is set we begin by searching for the
8011 ;; first possible position of a leading specifier list.
8012 ;; The `c-decl-block-key' search continues from there since
8013 ;; we know it can't match earlier.
8015 (when (c-syntactic-re-search-forward c-symbol-start
8017 (goto-char (setq first-specifier-pos
(match-beginning 0)))
8022 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t
)
8023 (goto-char (setq kwd-start
(match-beginning 0)))
8026 ;; Found a keyword that can't be a type?
8029 ;; Can be a type too, in which case it's the return type of a
8030 ;; function (under the assumption that no declaration level
8031 ;; block construct starts with a type).
8032 (not (c-forward-type))
8034 ;; Jumped over a type, but it could be a declaration keyword
8035 ;; followed by the declared identifier that we've jumped over
8036 ;; instead (e.g. in "class Foo {"). If it indeed is a type
8037 ;; then we should be at the declarator now, so check for a
8038 ;; valid declarator start.
8040 ;; Note: This doesn't cope with the case when a declared
8041 ;; identifier is followed by e.g. '(' in a language where '('
8042 ;; also might be part of a declarator expression. Currently
8043 ;; there's no such language.
8044 (not (or (looking-at c-symbol-start
)
8045 (looking-at c-type-decl-prefix-key
)))))
8047 ;; In Pike a list of modifiers may be followed by a brace
8048 ;; to make them apply to many identifiers. Note that the
8049 ;; match data will be empty on return in this case.
8050 ((and (c-major-mode-is 'pike-mode
)
8052 (goto-char open-brace
)
8053 (= (c-backward-token-2) 0))
8054 (looking-at c-specifier-key
)
8055 ;; Use this variant to avoid yet another special regexp.
8056 (c-keyword-member (c-keyword-sym (match-string 1))
8058 (setq kwd-start
(point))
8064 ;; Back up over any preceding specifiers and their clauses
8065 ;; by going forward from `first-specifier-pos', which is the
8066 ;; earliest possible position where the specifier list can
8069 (goto-char first-specifier-pos
)
8071 (while (< (point) kwd-start
)
8072 (if (looking-at c-symbol-key
)
8073 ;; Accept any plain symbol token on the ground that
8074 ;; it's a specifier masked through a macro (just
8075 ;; like `c-forward-decl-or-cast-1' skip forward over
8078 ;; Could be more restrictive wrt invalid keywords,
8079 ;; but that'd only occur in invalid code so there's
8080 ;; no use spending effort on it.
8081 (let ((end (match-end 0)))
8082 (unless (c-forward-keyword-clause 0)
8084 (c-forward-syntactic-ws)))
8086 ;; Can't parse a declaration preamble and is still
8087 ;; before `kwd-start'. That means `first-specifier-pos'
8088 ;; was in some earlier construct. Search again.
8089 (if (c-syntactic-re-search-forward c-symbol-start
8091 (goto-char (setq first-specifier-pos
(match-beginning 0)))
8092 ;; Got no preamble before the block declaration keyword.
8093 (setq first-specifier-pos kwd-start
))))
8095 (goto-char first-specifier-pos
))
8096 (goto-char kwd-start
))
8100 (defun c-search-uplist-for-classkey (paren-state)
8101 ;; Check if the closest containing paren sexp is a declaration
8102 ;; block, returning a 2 element vector in that case. Aref 0
8103 ;; contains the bufpos at boi of the class key line, and aref 1
8104 ;; contains the bufpos of the open brace. This function is an
8105 ;; obsolete wrapper for `c-looking-at-decl-block'.
8107 ;; This function might do hidden buffer changes.
8108 (let ((open-paren-pos (c-most-enclosing-brace paren-state
)))
8109 (when open-paren-pos
8111 (goto-char open-paren-pos
)
8112 (when (and (eq (char-after) ?
{)
8113 (c-looking-at-decl-block
8114 (c-safe-position open-paren-pos paren-state
)
8116 (back-to-indentation)
8117 (vector (point) open-paren-pos
))))))
8119 (defmacro c-pull-open-brace
(ps)
8120 ;; Pull the next open brace from PS (which has the form of paren-state),
8121 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
8123 (while (consp (car ,ps
))
8124 (setq ,ps
(cdr ,ps
)))
8126 (setq ,ps
(cdr ,ps
)))))
8128 (defun c-most-enclosing-decl-block (paren-state)
8129 ;; Return the buffer position of the most enclosing decl-block brace (in the
8130 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
8132 (let* ((open-brace (c-pull-open-brace paren-state
))
8133 (next-open-brace (c-pull-open-brace paren-state
)))
8134 (while (and open-brace
8136 (goto-char open-brace
)
8137 (not (c-looking-at-decl-block next-open-brace nil
))))
8138 (setq open-brace next-open-brace
8139 next-open-brace
(c-pull-open-brace paren-state
)))
8142 (defun c-cheap-inside-bracelist-p (paren-state)
8143 ;; Return the position of the L-brace if point is inside a brace list
8144 ;; initialization of an array, etc. This is an approximate function,
8145 ;; designed for speed over accuracy. It will not find every bracelist, but
8146 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
8147 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
8148 ;; is everywhere else.
8152 (and (setq b-pos
(c-pull-open-brace paren-state
))
8153 (progn (goto-char b-pos
)
8155 (c-backward-token-2)
8156 (not (looking-at "=")))))
8159 (defun c-inside-bracelist-p (containing-sexp paren-state
)
8160 ;; return the buffer position of the beginning of the brace list
8161 ;; statement if we're inside a brace list, otherwise return nil.
8162 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
8163 ;; paren. PAREN-STATE is the remainder of the state of enclosing
8166 ;; N.B.: This algorithm can potentially get confused by cpp macros
8167 ;; placed in inconvenient locations. It's a trade-off we make for
8170 ;; This function might do hidden buffer changes.
8172 ;; This will pick up brace list declarations.
8175 (goto-char containing-sexp
)
8178 (if (and (or (looking-at c-brace-list-key
)
8179 (progn (c-forward-sexp -
1)
8180 (looking-at c-brace-list-key
)))
8181 (setq bracepos
(c-down-list-forward (point)))
8182 (not (c-crosses-statement-barrier-p (point)
8185 ;; this will pick up array/aggregate init lists, even if they are nested.
8188 ;; Pike can have class definitions anywhere, so we must
8189 ;; check for the class key here.
8190 (and (c-major-mode-is 'pike-mode
)
8192 bufpos braceassignp lim next-containing
)
8193 (while (and (not bufpos
)
8196 (if (consp (car paren-state
))
8197 (setq lim
(cdr (car paren-state
))
8198 paren-state
(cdr paren-state
))
8199 (setq lim
(car paren-state
)))
8201 (setq next-containing
(car paren-state
)
8202 paren-state
(cdr paren-state
))))
8203 (goto-char containing-sexp
)
8204 (if (c-looking-at-inexpr-block next-containing next-containing
)
8205 ;; We're in an in-expression block of some kind. Do not
8206 ;; check nesting. We deliberately set the limit to the
8207 ;; containing sexp, so that c-looking-at-inexpr-block
8208 ;; doesn't check for an identifier before it.
8209 (setq containing-sexp nil
)
8210 ;; see if the open brace is preceded by = or [...] in
8211 ;; this statement, but watch out for operator=
8212 (setq braceassignp
'dontknow
)
8213 (c-backward-token-2 1 t lim
)
8214 ;; Checks to do only on the first sexp before the brace.
8215 (when (and c-opt-inexpr-brace-list-key
8216 (eq (char-after) ?\
[))
8217 ;; In Java, an initialization brace list may follow
8218 ;; directly after "new Foo[]", so check for a "new"
8220 (while (eq braceassignp
'dontknow
)
8222 (cond ((/= (c-backward-token-2 1 t lim
) 0) nil
)
8223 ((looking-at c-opt-inexpr-brace-list-key
) t
)
8224 ((looking-at "\\sw\\|\\s_\\|[.[]")
8225 ;; Carry on looking if this is an
8226 ;; identifier (may contain "." in Java)
8227 ;; or another "[]" sexp.
8230 ;; Checks to do on all sexps before the brace, up to the
8231 ;; beginning of the statement.
8232 (while (eq braceassignp
'dontknow
)
8233 (cond ((eq (char-after) ?\
;)
8234 (setq braceassignp nil
))
8236 (looking-at class-key
))
8237 (setq braceassignp nil
))
8238 ((eq (char-after) ?
=)
8239 ;; We've seen a =, but must check earlier tokens so
8240 ;; that it isn't something that should be ignored.
8241 (setq braceassignp
'maybe
)
8242 (while (and (eq braceassignp
'maybe
)
8243 (zerop (c-backward-token-2 1 t lim
)))
8246 ;; Check for operator =
8247 ((and c-opt-op-identifier-prefix
8248 (looking-at c-opt-op-identifier-prefix
))
8250 ;; Check for `<opchar>= in Pike.
8251 ((and (c-major-mode-is 'pike-mode
)
8252 (or (eq (char-after) ?
`)
8253 ;; Special case for Pikes
8254 ;; `[]=, since '[' is not in
8255 ;; the punctuation class.
8256 (and (eq (char-after) ?\
[)
8257 (eq (char-before) ?
`))))
8259 ((looking-at "\\s.") 'maybe
)
8260 ;; make sure we're not in a C++ template
8261 ;; argument assignment
8263 (c-major-mode-is 'c
++-mode
)
8265 (let ((here (point))
8267 (skip-chars-backward "^<>")
8269 (and (eq (char-before) ?
<)
8270 (not (c-crosses-statement-barrier-p
8272 (not (c-in-literal))
8276 (if (and (eq braceassignp
'dontknow
)
8277 (/= (c-backward-token-2 1 t lim
) 0))
8278 (setq braceassignp nil
)))
8279 (if (not braceassignp
)
8280 (if (eq (char-after) ?\
;)
8281 ;; Brace lists can't contain a semicolon, so we're done.
8282 (setq containing-sexp nil
)
8284 (setq containing-sexp next-containing
8286 next-containing nil
))
8287 ;; we've hit the beginning of the aggregate list
8288 (c-beginning-of-statement-1
8289 (c-most-enclosing-brace paren-state
))
8290 (setq bufpos
(point))))
8295 (defun c-looking-at-special-brace-list (&optional lim
)
8296 ;; If we're looking at the start of a pike-style list, ie `({Â })',
8297 ;; `([Â ])', `(<Â >)' etc, a cons of a cons of its starting and ending
8298 ;; positions and its entry in c-special-brace-lists is returned, nil
8299 ;; otherwise. The ending position is nil if the list is still open.
8300 ;; LIM is the limit for forward search. The point may either be at
8301 ;; the `(' or at the following paren character. Tries to check the
8302 ;; matching closer, but assumes it's correct if no balanced paren is
8303 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8304 ;; a special brace list).
8306 ;; This function might do hidden buffer changes.
8307 (if c-special-brace-lists
8312 (c-forward-syntactic-ws)
8313 (if (eq (char-after) ?\
()
8316 (c-forward-syntactic-ws)
8317 (setq inner-beg
(point))
8318 (setq type
(assq (char-after) c-special-brace-lists
)))
8319 (if (setq type
(assq (char-after) c-special-brace-lists
))
8321 (setq inner-beg
(point))
8322 (c-backward-syntactic-ws)
8324 (setq beg
(if (eq (char-after) ?\
()
8332 (= (char-before) ?\
)))
8334 (goto-char inner-beg
)
8335 (if (looking-at "\\s(")
8336 ;; Check balancing of the inner paren
8341 ;; If the inner char isn't a paren then
8342 ;; we can't check balancing, so just
8343 ;; check the char before the outer
8347 (c-backward-syntactic-ws)
8348 (= (char-before) (cdr type
)))))
8349 (if (or (/= (char-syntax (char-before)) ?\
))
8351 (c-forward-syntactic-ws)
8354 (cons (cons beg end
) type
))
8355 (cons (list beg
) type
)))))
8358 (defun c-looking-at-bos (&optional lim
)
8359 ;; Return non-nil if between two statements or declarations, assuming
8360 ;; point is not inside a literal or comment.
8362 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8363 ;; are recommended instead.
8365 ;; This function might do hidden buffer changes.
8366 (c-at-statement-start-p))
8367 (make-obsolete 'c-looking-at-bos
'c-at-statement-start-p
"22.1")
8369 (defun c-looking-at-inexpr-block (lim containing-sexp
&optional check-at-end
)
8370 ;; Return non-nil if we're looking at the beginning of a block
8371 ;; inside an expression. The value returned is actually a cons of
8372 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8373 ;; position of the beginning of the construct.
8375 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8376 ;; position of the closest containing list. If it's nil, the
8377 ;; containing paren isn't used to decide whether we're inside an
8378 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8379 ;; needs to be farther back.
8381 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8382 ;; brace block might be done. It should only be used when the
8383 ;; construct can be assumed to be complete, i.e. when the original
8384 ;; starting position was further down than that.
8386 ;; This function might do hidden buffer changes.
8389 (let ((res 'maybe
) passed-paren
8390 (closest-lim (or containing-sexp lim
(point-min)))
8391 ;; Look at the character after point only as a last resort
8392 ;; when we can't disambiguate.
8393 (block-follows (and (eq (char-after) ?
{) (point))))
8395 (while (and (eq res
'maybe
)
8396 (progn (c-backward-syntactic-ws)
8397 (> (point) closest-lim
))
8399 (progn (backward-char)
8400 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8401 (c-safe (forward-char)
8402 (goto-char (scan-sexps (point) -
1))))
8405 (if (looking-at c-keywords-regexp
)
8406 (let ((kw-sym (c-keyword-sym (match-string 1))))
8409 (c-keyword-member kw-sym
'c-inexpr-class-kwds
))
8410 (and (not (eq passed-paren ?\
[))
8411 (or (not (looking-at c-class-key
))
8412 ;; If the class definition is at the start of
8413 ;; a statement, we don't consider it an
8414 ;; in-expression class.
8415 (let ((prev (point)))
8417 (= (c-backward-token-2 1 nil closest-lim
) 0)
8418 (eq (char-syntax (char-after)) ?w
))
8419 (setq prev
(point)))
8421 (not (c-at-statement-start-p)))
8422 ;; Also, in Pike we treat it as an
8423 ;; in-expression class if it's used in an
8424 ;; object clone expression.
8427 (c-major-mode-is 'pike-mode
)
8428 (progn (goto-char block-follows
)
8429 (zerop (c-forward-token-2 1 t
)))
8430 (eq (char-after) ?\
())))
8431 (cons 'inexpr-class
(point))))
8432 ((c-keyword-member kw-sym
'c-inexpr-block-kwds
)
8433 (when (not passed-paren
)
8434 (cons 'inexpr-statement
(point))))
8435 ((c-keyword-member kw-sym
'c-lambda-kwds
)
8436 (when (or (not passed-paren
)
8437 (eq passed-paren ?\
())
8438 (cons 'inlambda
(point))))
8439 ((c-keyword-member kw-sym
'c-block-stmt-kwds
)
8444 (if (looking-at "\\s(")
8446 (if (and (eq passed-paren ?\
[)
8447 (eq (char-after) ?\
[))
8448 ;; Accept several square bracket sexps for
8449 ;; Java array initializations.
8451 (setq passed-paren
(char-after))
8456 (when (and c-recognize-paren-inexpr-blocks
8459 (eq (char-after containing-sexp
) ?\
())
8460 (goto-char containing-sexp
)
8461 (if (or (save-excursion
8462 (c-backward-syntactic-ws lim
)
8463 (and (> (point) (or lim
(point-min)))
8465 (and c-special-brace-lists
8466 (c-looking-at-special-brace-list)))
8468 (cons 'inexpr-statement
(point))))
8472 (defun c-looking-at-inexpr-block-backward (paren-state)
8473 ;; Returns non-nil if we're looking at the end of an in-expression
8474 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8475 ;; PAREN-STATE is the paren state relevant at the current position.
8477 ;; This function might do hidden buffer changes.
8479 ;; We currently only recognize a block.
8480 (let ((here (point))
8481 (elem (car-safe paren-state
))
8483 (when (and (consp elem
)
8484 (progn (goto-char (cdr elem
))
8485 (c-forward-syntactic-ws here
)
8487 (goto-char (car elem
))
8488 (if (setq paren-state
(cdr paren-state
))
8489 (setq containing-sexp
(car-safe paren-state
)))
8490 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8492 containing-sexp
)))))
8494 (defun c-at-macro-vsemi-p (&optional pos
)
8495 ;; Is there a "virtual semicolon" at POS or point?
8496 ;; (See cc-defs.el for full details of "virtual semicolons".)
8498 ;; This is true when point is at the last non syntactic WS position on the
8499 ;; line, there is a macro call last on the line, and this particular macro's
8500 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
8509 c-macro-with-semi-re
8510 (not (c-in-literal))
8511 (eq (skip-chars-backward " \t") 0)
8513 ;; Check we've got nothing after this except comments and empty lines
8514 ;; joined by escaped EOLs.
8515 (skip-chars-forward " \t") ; always returns non-nil.
8517 (while ; go over 1 block comment per iteration.
8519 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
8520 (goto-char (match-end 0))
8522 ((looking-at c-block-comment-start-regexp
)
8523 (and (forward-comment 1)
8524 (skip-chars-forward " \t"))) ; always returns non-nil
8525 ((looking-at c-line-comment-start-regexp
)
8532 (progn (c-backward-syntactic-ws)
8535 ;; Check for one of the listed macros being before point.
8536 (or (not (eq (char-before) ?\
)))
8537 (when (c-go-list-backward)
8538 (c-backward-syntactic-ws)
8540 (c-simple-skip-symbol-backward)
8541 (looking-at c-macro-with-semi-re
)))))
8543 (defun c-macro-vsemi-status-unknown-p () t
) ; See cc-defs.el.
8546 ;; `c-guess-basic-syntax' and the functions that precedes it below
8547 ;; implements the main decision tree for determining the syntactic
8548 ;; analysis of the current line of code.
8550 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8551 ;; auto newline analysis.
8552 (defvar c-auto-newline-analysis nil
)
8554 (defun c-brace-anchor-point (bracepos)
8555 ;; BRACEPOS is the position of a brace in a construct like "namespace
8556 ;; Bar {". Return the anchor point in this construct; this is the
8557 ;; earliest symbol on the brace's line which isn't earlier than
8560 ;; Currently (2007-08-17), "like namespace" means "matches
8561 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8562 ;; or anything like that.
8564 (let ((boi (c-point 'boi bracepos
)))
8565 (goto-char bracepos
)
8566 (while (and (> (point) boi
)
8567 (not (looking-at c-other-decl-block-key
)))
8568 (c-backward-token-2))
8569 (if (> (point) boi
) (point) boi
))))
8571 (defsubst c-add-syntax
(symbol &rest args
)
8572 ;; A simple function to prepend a new syntax element to
8573 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8574 ;; should always be dynamically bound but since we read it first
8575 ;; we'll fail properly anyway if this function is misused.
8576 (setq c-syntactic-context
(cons (cons symbol args
)
8577 c-syntactic-context
)))
8579 (defsubst c-append-syntax
(symbol &rest args
)
8580 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8581 ;; (Normally not necessary.)
8582 (setq c-syntactic-context
(nconc c-syntactic-context
8583 (list (cons symbol args
)))))
8585 (defun c-add-stmt-syntax (syntax-symbol
8590 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8591 ;; needed with further syntax elements of the types `substatement',
8592 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8593 ;; `defun-block-intro'.
8595 ;; Do the generic processing to anchor the given syntax symbol on
8596 ;; the preceding statement: Skip over any labels and containing
8597 ;; statements on the same line, and then search backward until we
8598 ;; find a statement or block start that begins at boi without a
8599 ;; label or comment.
8601 ;; Point is assumed to be at the prospective anchor point for the
8602 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8603 ;; skip past open parens and containing statements. Most of the added
8604 ;; syntax elements will get the same anchor point - the exception is
8605 ;; for an anchor in a construct like "namespace"[*] - this is as early
8606 ;; as possible in the construct but on the same line as the {.
8608 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8610 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8611 ;; syntax symbol. They are appended after the anchor point.
8613 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8614 ;; if the current statement starts there.
8616 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8617 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8619 ;; This function might do hidden buffer changes.
8621 (if (= (point) (c-point 'boi
))
8622 ;; This is by far the most common case, so let's give it special
8624 (apply 'c-add-syntax syntax-symbol
(point) syntax-extra-args
)
8626 (let ((syntax-last c-syntactic-context
)
8627 (boi (c-point 'boi
))
8628 ;; Set when we're on a label, so that we don't stop there.
8629 ;; FIXME: To be complete we should check if we're on a label
8630 ;; now at the start.
8633 ;; Use point as the anchor point for "namespace", "extern", etc.
8634 (apply 'c-add-syntax syntax-symbol
8635 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist
)
8639 ;; Loop while we have to back out of containing blocks.
8642 (catch 'back-up-block
8644 ;; Loop while we have to back up statements.
8645 (while (or (/= (point) boi
)
8647 (looking-at c-comment-start-regexp
))
8649 ;; Skip past any comments that stands between the
8650 ;; statement start and boi.
8651 (let ((savepos (point)))
8652 (while (and (/= savepos boi
)
8653 (c-backward-single-comment))
8654 (setq savepos
(point)
8655 boi
(c-point 'boi
)))
8656 (goto-char savepos
))
8658 ;; Skip to the beginning of this statement or backward
8660 (let ((old-pos (point))
8662 (step-type (c-beginning-of-statement-1 containing-sexp
)))
8663 (setq boi
(c-point 'boi
)
8664 on-label
(eq step-type
'label
))
8666 (cond ((= (point) old-pos
)
8667 ;; If we didn't move we're at the start of a block and
8668 ;; have to continue outside it.
8669 (throw 'back-up-block t
))
8671 ((and (eq step-type
'up
)
8672 (>= (point) old-boi
)
8673 (looking-at "else\\>[^_]")
8676 (looking-at "if\\>[^_]")))
8677 ;; Special case to avoid deeper and deeper indentation
8678 ;; of "else if" clauses.
8681 ((and (not stop-at-boi-only
)
8682 (/= old-pos old-boi
)
8683 (memq step-type
'(up previous
)))
8684 ;; If stop-at-boi-only is nil, we shouldn't back up
8685 ;; over previous or containing statements to try to
8686 ;; reach boi, so go back to the last position and
8689 (throw 'back-up-block nil
))
8692 (if (and (not stop-at-boi-only
)
8693 (memq step-type
'(up previous beginning
)))
8694 ;; If we've moved into another statement then we
8695 ;; should no longer try to stop in the middle of a
8697 (setq stop-at-boi-only t
))
8699 ;; Record this as a substatement if we skipped up one
8701 (when (eq step-type
'up
)
8702 (c-add-syntax 'substatement nil
))))
8707 ;; Now we have to go out of this block.
8708 (goto-char containing-sexp
)
8710 ;; Don't stop in the middle of a special brace list opener
8712 (when c-special-brace-lists
8713 (let ((special-list (c-looking-at-special-brace-list)))
8714 (when (and special-list
8715 (< (car (car special-list
)) (point)))
8716 (setq containing-sexp
(car (car special-list
)))
8717 (goto-char containing-sexp
))))
8719 (setq paren-state
(c-whack-state-after containing-sexp paren-state
)
8720 containing-sexp
(c-most-enclosing-brace paren-state
)
8723 ;; Analyze the construct in front of the block we've stepped out
8724 ;; from and add the right syntactic element for it.
8725 (let ((paren-pos (point))
8726 (paren-char (char-after))
8729 (if (eq paren-char ?\
()
8730 ;; Stepped out of a parenthesis block, so we're in an
8733 (when (/= paren-pos boi
)
8734 (if (and c-recognize-paren-inexpr-blocks
8736 (c-backward-syntactic-ws containing-sexp
)
8737 (or (not (looking-at "\\>"))
8738 (not (c-on-identifier))))
8740 (goto-char (1+ paren-pos
))
8741 (c-forward-syntactic-ws)
8742 (eq (char-after) ?
{)))
8743 ;; Stepped out of an in-expression statement. This
8744 ;; syntactic element won't get an anchor pos.
8745 (c-add-syntax 'inexpr-statement
)
8747 ;; A parenthesis normally belongs to an arglist.
8748 (c-add-syntax 'arglist-cont-nonempty nil paren-pos
)))
8752 (1+ containing-sexp
)
8754 (setq step-type
'same
8757 ;; Stepped out of a brace block.
8758 (setq step-type
(c-beginning-of-statement-1 containing-sexp
)
8759 on-label
(eq step-type
'label
))
8761 (if (and (eq step-type
'same
)
8762 (/= paren-pos
(point)))
8766 (goto-char paren-pos
)
8767 (setq inexpr
(c-looking-at-inexpr-block
8768 (c-safe-position containing-sexp paren-state
)
8770 (c-add-syntax (if (eq (car inexpr
) 'inlambda
)
8772 'statement-block-intro
)
8774 ((looking-at c-other-decl-block-key
)
8776 (cdr (assoc (match-string 1)
8777 c-other-decl-block-key-in-symbols-alist
))
8778 (max (c-point 'boi paren-pos
) (point))))
8779 (t (c-add-syntax 'defun-block-intro nil
))))
8781 (c-add-syntax 'statement-block-intro nil
)))
8783 (if (= paren-pos boi
)
8784 ;; Always done if the open brace was at boi. The
8785 ;; c-beginning-of-statement-1 call above is necessary
8786 ;; anyway, to decide the type of block-intro to add.
8787 (goto-char paren-pos
)
8788 (setq boi
(c-point 'boi
)))
8791 ;; Fill in the current point as the anchor for all the symbols
8793 (let ((p c-syntactic-context
) q
)
8794 (while (not (eq p syntax-last
))
8795 (setq q
(cdr (car p
))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8803 (defun c-add-class-syntax (symbol
8804 containing-decl-open
8805 containing-decl-start
8808 ;; The inclass and class-close syntactic symbols are added in
8809 ;; several places and some work is needed to fix everything.
8810 ;; Therefore it's collected here.
8812 ;; This function might do hidden buffer changes.
8813 (goto-char containing-decl-open
)
8814 (if (and (eq symbol
'inclass
) (= (point) (c-point 'boi
)))
8816 (c-add-syntax symbol containing-decl-open
)
8817 containing-decl-open
)
8818 (goto-char containing-decl-start
)
8819 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8820 ;; here, but we have to do like this for compatibility.
8821 (back-to-indentation)
8822 (c-add-syntax symbol
(point))
8823 (if (and (c-keyword-member containing-decl-kwd
8824 'c-inexpr-class-kwds
)
8825 (/= containing-decl-start
(c-point 'boi containing-decl-start
)))
8826 (c-add-syntax 'inexpr-class
))
8829 (defun c-guess-continued-construct (indent-point
8831 beg-of-same-or-containing-stmt
8834 ;; This function contains the decision tree reached through both
8835 ;; cases 18 and 10. It's a continued statement or top level
8836 ;; construct of some kind.
8838 ;; This function might do hidden buffer changes.
8840 (let (special-brace-list placeholder
)
8841 (goto-char indent-point
)
8842 (skip-chars-forward " \t")
8845 ;; (CASE A removed.)
8846 ;; CASE B: open braces for class or brace-lists
8847 ((setq special-brace-list
8848 (or (and c-special-brace-lists
8849 (c-looking-at-special-brace-list))
8850 (eq char-after-ip ?
{)))
8853 ;; CASE B.1: class-open
8855 (and (eq (char-after) ?
{)
8856 (c-looking-at-decl-block containing-sexp t
)
8857 (setq beg-of-same-or-containing-stmt
(point))))
8858 (c-add-syntax 'class-open beg-of-same-or-containing-stmt
))
8860 ;; CASE B.2: brace-list-open
8861 ((or (consp special-brace-list
)
8863 (goto-char beg-of-same-or-containing-stmt
)
8864 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8865 indent-point t t t
)))
8866 ;; The most semantically accurate symbol here is
8867 ;; brace-list-open, but we normally report it simply as a
8868 ;; statement-cont. The reason is that one normally adjusts
8869 ;; brace-list-open for brace lists as top-level constructs,
8870 ;; and brace lists inside statements is a completely different
8871 ;; context. C.f. case 5A.3.
8872 (c-beginning-of-statement-1 containing-sexp
)
8873 (c-add-stmt-syntax (if c-auto-newline-analysis
8874 ;; Turn off the dwim above when we're
8875 ;; analyzing the nature of the brace
8876 ;; for the auto newline feature.
8880 containing-sexp paren-state
))
8882 ;; CASE B.3: The body of a function declared inside a normal
8883 ;; block. Can occur e.g. in Pike and when using gcc
8884 ;; extensions, but watch out for macros followed by blocks.
8885 ;; C.f. cases E, 16F and 17G.
8886 ((and (not (c-at-statement-start-p))
8887 (eq (c-beginning-of-statement-1 containing-sexp nil nil t
)
8890 (let ((c-recognize-typeless-decls nil
))
8891 ;; Turn off recognition of constructs that lacks a
8892 ;; type in this case, since that's more likely to be
8893 ;; a macro followed by a block.
8894 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
8895 (c-add-stmt-syntax 'defun-open nil t
8896 containing-sexp paren-state
))
8898 ;; CASE B.4: Continued statement with block open. The most
8899 ;; accurate analysis is perhaps `statement-cont' together with
8900 ;; `block-open' but we play DWIM and use `substatement-open'
8901 ;; instead. The rationale is that this typically is a macro
8902 ;; followed by a block which makes it very similar to a
8903 ;; statement with a substatement block.
8905 (c-add-stmt-syntax 'substatement-open nil nil
8906 containing-sexp paren-state
))
8909 ;; CASE C: iostream insertion or extraction operator
8910 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8912 (goto-char beg-of-same-or-containing-stmt
)
8913 ;; If there is no preceding streamop in the statement
8914 ;; then indent this line as a normal statement-cont.
8915 (when (c-syntactic-re-search-forward
8916 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point
'move t t
)
8917 (c-add-syntax 'stream-op
(c-point 'boi
))
8920 ;; CASE E: In the "K&R region" of a function declared inside a
8921 ;; normal block. C.f. case B.3.
8922 ((and (save-excursion
8923 ;; Check that the next token is a '{'. This works as
8924 ;; long as no language that allows nested function
8925 ;; definitions allows stuff like member init lists, K&R
8926 ;; declarations or throws clauses there.
8928 ;; Note that we do a forward search for something ahead
8929 ;; of the indentation line here. That's not good since
8930 ;; the user might not have typed it yet. Unfortunately
8931 ;; it's exceedingly tricky to recognize a function
8932 ;; prototype in a code block without resorting to this.
8933 (c-forward-syntactic-ws)
8934 (eq (char-after) ?
{))
8935 (not (c-at-statement-start-p))
8936 (eq (c-beginning-of-statement-1 containing-sexp nil nil t
)
8939 (let ((c-recognize-typeless-decls nil
))
8940 ;; Turn off recognition of constructs that lacks a
8941 ;; type in this case, since that's more likely to be
8942 ;; a macro followed by a block.
8943 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
8944 (c-add-stmt-syntax 'func-decl-cont nil t
8945 containing-sexp paren-state
))
8947 ;;CASE F: continued statement and the only preceding items are
8949 ((and (c-major-mode-is 'java-mode
)
8950 (setq placeholder
(point))
8951 (c-beginning-of-statement-1)
8953 (while (and (c-forward-annotation)
8954 (< (point) placeholder
))
8955 (c-forward-syntactic-ws))
8958 (>= (point) placeholder
)
8959 (goto-char placeholder
)))
8960 (c-beginning-of-statement-1 containing-sexp
)
8961 (c-add-syntax 'annotation-var-cont
(point)))
8963 ;; CASE G: a template list continuation?
8964 ;; Mostly a duplication of case 5D.3 to fix templates-19:
8965 ((and (c-major-mode-is 'c
++-mode
)
8967 (goto-char indent-point
)
8968 (c-with-syntax-table c
++-template-syntax-table
8969 (setq placeholder
(c-up-list-backward)))
8971 (eq (char-after placeholder
) ?
<)
8972 (/= (char-before placeholder
) ?
<)
8974 (goto-char (1+ placeholder
))
8975 (not (looking-at c-
<-op-cont-regexp
))))))
8976 (c-with-syntax-table c
++-template-syntax-table
8977 (goto-char placeholder
)
8978 (c-beginning-of-statement-1 containing-sexp t
)
8980 (c-backward-syntactic-ws containing-sexp
)
8981 (eq (char-before) ?
<))
8982 ;; In a nested template arglist.
8984 (goto-char placeholder
)
8985 (c-syntactic-skip-backward "^,;" containing-sexp t
)
8986 (c-forward-syntactic-ws))
8987 (back-to-indentation)))
8988 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8990 (c-add-syntax 'template-args-cont
(point) placeholder
))
8992 ;; CASE D: continued statement.
8994 (c-beginning-of-statement-1 containing-sexp
)
8995 (c-add-stmt-syntax 'statement-cont nil nil
8996 containing-sexp paren-state
))
8999 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
9002 (defun c-guess-basic-syntax ()
9003 "Return the syntactic context of the current line."
9006 (c-save-buffer-state
9007 ((indent-point (point))
9008 (case-fold-search nil
)
9009 ;; A whole ugly bunch of various temporary variables. Have
9010 ;; to declare them here since it's not possible to declare
9011 ;; a variable with only the scope of a cond test and the
9012 ;; following result clauses, and most of this function is a
9013 ;; single gigantic cond. :P
9014 literal char-before-ip before-ws-ip char-after-ip macro-start
9015 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
9016 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
9018 ;; The following record some positions for the containing
9019 ;; declaration block if we're directly within one:
9020 ;; `containing-decl-open' is the position of the open
9021 ;; brace. `containing-decl-start' is the start of the
9022 ;; declaration. `containing-decl-kwd' is the keyword
9023 ;; symbol of the keyword that tells what kind of block it
9025 containing-decl-open
9026 containing-decl-start
9028 ;; The open paren of the closest surrounding sexp or nil if
9031 ;; The position after the closest preceding brace sexp
9032 ;; (nested sexps are ignored), or the position after
9033 ;; `containing-sexp' if there is none, or (point-min) if
9034 ;; `containing-sexp' is nil.
9036 ;; The paren state outside `containing-sexp', or at
9037 ;; `indent-point' if `containing-sexp' is nil.
9038 (paren-state (c-parse-state))
9039 ;; There's always at most one syntactic element which got
9040 ;; an anchor pos. It's stored in syntactic-relpos.
9042 (c-stmt-delim-chars c-stmt-delim-chars
))
9044 ;; Check if we're directly inside an enclosing declaration
9046 (when (and (setq containing-sexp
9047 (c-most-enclosing-brace paren-state
))
9049 (goto-char containing-sexp
)
9050 (eq (char-after) ?
{))
9052 (c-looking-at-decl-block
9053 (c-most-enclosing-brace paren-state
9056 (setq containing-decl-open containing-sexp
9057 containing-decl-start
(point)
9058 containing-sexp nil
)
9059 (goto-char placeholder
)
9060 (setq containing-decl-kwd
(and (looking-at c-keywords-regexp
)
9061 (c-keyword-sym (match-string 1)))))
9063 ;; Init some position variables.
9066 (setq containing-sexp
(car paren-state
)
9067 paren-state
(cdr paren-state
))
9068 (if (consp containing-sexp
)
9070 (setq lim
(cdr containing-sexp
))
9071 (if (cdr c-state-cache
)
9072 ;; Ignore balanced paren. The next entry
9073 ;; can't be another one.
9074 (setq containing-sexp
(car (cdr c-state-cache
))
9075 paren-state
(cdr paren-state
))
9076 ;; If there is no surrounding open paren then
9077 ;; put the last balanced pair back on paren-state.
9078 (setq paren-state
(cons containing-sexp paren-state
)
9079 containing-sexp nil
)))
9080 (setq lim
(1+ containing-sexp
))))
9081 (setq lim
(point-min)))
9083 ;; If we're in a parenthesis list then ',' delimits the
9084 ;; "statements" rather than being an operator (with the
9085 ;; exception of the "for" clause). This difference is
9086 ;; typically only noticeable when statements are used in macro
9088 (when (and containing-sexp
9089 (eq (char-after containing-sexp
) ?\
())
9090 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma
))
9091 ;; cache char before and after indent point, and move point to
9092 ;; the most likely position to perform the majority of tests
9093 (goto-char indent-point
)
9094 (c-backward-syntactic-ws lim
)
9095 (setq before-ws-ip
(point)
9096 char-before-ip
(char-before))
9097 (goto-char indent-point
)
9098 (skip-chars-forward " \t")
9099 (setq char-after-ip
(char-after))
9101 ;; are we in a literal?
9102 (setq literal
(c-in-literal lim
))
9104 ;; now figure out syntactic qualities of the current line
9107 ;; CASE 1: in a string.
9108 ((eq literal
'string
)
9109 (c-add-syntax 'string
(c-point 'bopl
)))
9111 ;; CASE 2: in a C or C++ style comment.
9112 ((and (memq literal
'(c c
++))
9113 ;; This is a kludge for XEmacs where we use
9114 ;; `buffer-syntactic-context', which doesn't correctly
9115 ;; recognize "\*/" to end a block comment.
9116 ;; `parse-partial-sexp' which is used by
9117 ;; `c-literal-limits' will however do that in most
9118 ;; versions, which results in that we get nil from
9119 ;; `c-literal-limits' even when `c-in-literal' claims
9120 ;; we're inside a comment.
9121 (setq placeholder
(c-literal-limits lim
)))
9122 (c-add-syntax literal
(car placeholder
)))
9124 ;; CASE 3: in a cpp preprocessor macro continuation.
9125 ((and (save-excursion
9126 (when (c-beginning-of-macro)
9127 (setq macro-start
(point))))
9128 (/= macro-start
(c-point 'boi
))
9130 (setq tmpsymbol
'cpp-macro-cont
)
9131 (or (not c-syntactic-indentation-in-macros
)
9133 (goto-char macro-start
)
9134 ;; If at the beginning of the body of a #define
9135 ;; directive then analyze as cpp-define-intro
9136 ;; only. Go on with the syntactic analysis
9137 ;; otherwise. in-macro-expr is set if we're in a
9138 ;; cpp expression, i.e. before the #define body
9139 ;; or anywhere in a non-#define directive.
9140 (if (c-forward-to-cpp-define-body)
9141 (let ((indent-boi (c-point 'boi indent-point
)))
9142 (setq in-macro-expr
(> (point) indent-boi
)
9143 tmpsymbol
'cpp-define-intro
)
9144 (= (point) indent-boi
))
9145 (setq in-macro-expr t
)
9147 (c-add-syntax tmpsymbol macro-start
)
9148 (setq macro-start nil
))
9150 ;; CASE 11: an else clause?
9151 ((looking-at "else\\>[^_]")
9152 (c-beginning-of-statement-1 containing-sexp
)
9153 (c-add-stmt-syntax 'else-clause nil t
9154 containing-sexp paren-state
))
9156 ;; CASE 12: while closure of a do/while construct?
9157 ((and (looking-at "while\\>[^_]")
9159 (prog1 (eq (c-beginning-of-statement-1 containing-sexp
)
9161 (setq placeholder
(point)))))
9162 (goto-char placeholder
)
9163 (c-add-stmt-syntax 'do-while-closure nil t
9164 containing-sexp paren-state
))
9166 ;; CASE 13: A catch or finally clause? This case is simpler
9167 ;; than if-else and do-while, because a block is required
9168 ;; after every try, catch and finally.
9170 (and (cond ((c-major-mode-is 'c
++-mode
)
9171 (looking-at "catch\\>[^_]"))
9172 ((c-major-mode-is 'java-mode
)
9173 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
9174 (and (c-safe (c-backward-syntactic-ws)
9177 (eq (char-after) ?
{)
9178 (c-safe (c-backward-syntactic-ws)
9181 (if (eq (char-after) ?\
()
9182 (c-safe (c-backward-sexp) t
)
9184 (looking-at "\\(try\\|catch\\)\\>[^_]")
9185 (setq placeholder
(point))))
9186 (goto-char placeholder
)
9187 (c-add-stmt-syntax 'catch-clause nil t
9188 containing-sexp paren-state
))
9190 ;; CASE 18: A substatement we can recognize by keyword.
9192 (and c-opt-block-stmt-key
9193 (not (eq char-before-ip ?\
;))
9194 (not (c-at-vsemi-p before-ws-ip
))
9195 (not (memq char-after-ip
'(?\
) ?\
] ?
,)))
9196 (or (not (eq char-before-ip ?
}))
9197 (c-looking-at-inexpr-block-backward c-state-cache
))
9200 ;; Ought to cache the result from the
9201 ;; c-beginning-of-statement-1 calls here.
9202 (setq placeholder
(point))
9203 (while (eq (setq step-type
9204 (c-beginning-of-statement-1 lim
))
9206 (if (eq step-type
'previous
)
9207 (goto-char placeholder
)
9208 (setq placeholder
(point))
9209 (if (and (eq step-type
'same
)
9210 (not (looking-at c-opt-block-stmt-key
)))
9211 ;; Step up to the containing statement if we
9212 ;; stayed in the same one.
9216 (c-beginning-of-statement-1 lim
))
9219 (setq placeholder
(point))
9220 ;; There was no containing statement after all.
9221 (goto-char placeholder
)))))
9223 (if (looking-at c-block-stmt-2-key
)
9224 ;; Require a parenthesis after these keywords.
9225 ;; Necessary to catch e.g. synchronized in Java,
9226 ;; which can be used both as statement and
9228 (and (zerop (c-forward-token-2 1 nil
))
9229 (eq (char-after) ?\
())
9230 (looking-at c-opt-block-stmt-key
))))
9232 (if (eq step-type
'up
)
9233 ;; CASE 18A: Simple substatement.
9235 (goto-char placeholder
)
9237 ((eq char-after-ip ?
{)
9238 (c-add-stmt-syntax 'substatement-open nil nil
9239 containing-sexp paren-state
))
9241 (goto-char indent-point
)
9242 (back-to-indentation)
9244 (c-add-stmt-syntax 'substatement-label nil nil
9245 containing-sexp paren-state
))
9247 (c-add-stmt-syntax 'substatement nil nil
9248 containing-sexp paren-state
))))
9250 ;; CASE 18B: Some other substatement. This is shared
9252 (c-guess-continued-construct indent-point
9258 ;; CASE 14: A case or default label
9259 ((looking-at c-label-kwds-regexp
)
9262 (goto-char containing-sexp
)
9263 (setq lim
(c-most-enclosing-brace c-state-cache
9265 (c-backward-to-block-anchor lim
)
9266 (c-add-stmt-syntax 'case-label nil t lim paren-state
))
9267 ;; Got a bogus label at the top level. In lack of better
9268 ;; alternatives, anchor it on (point-min).
9269 (c-add-syntax 'case-label
(point-min))))
9271 ;; CASE 15: any other label
9273 (back-to-indentation)
9274 (and (not (looking-at c-syntactic-ws-start
))
9276 (cond (containing-decl-open
9277 (setq placeholder
(c-add-class-syntax 'inclass
9278 containing-decl-open
9279 containing-decl-start
9282 ;; Append access-label with the same anchor point as
9284 (c-append-syntax 'access-label placeholder
))
9287 (goto-char containing-sexp
)
9288 (setq lim
(c-most-enclosing-brace c-state-cache
9292 (if (and (eq (c-beginning-of-statement-1 lim
) 'up
)
9293 (looking-at "switch\\>[^_]"))
9294 ;; If the surrounding statement is a switch then
9295 ;; let's analyze all labels as switch labels, so
9296 ;; that they get lined up consistently.
9299 (c-backward-to-block-anchor lim
)
9300 (c-add-stmt-syntax tmpsymbol nil t lim paren-state
))
9303 ;; A label on the top level. Treat it as a class
9304 ;; context. (point-min) is the closest we get to the
9305 ;; class open brace.
9306 (c-add-syntax 'access-label
(point-min)))))
9308 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9310 ((setq placeholder
(c-looking-at-inexpr-block
9311 (c-safe-position containing-sexp paren-state
)
9313 ;; Have to turn on the heuristics after
9314 ;; the point even though it doesn't work
9315 ;; very well. C.f. test case class-16.pike.
9317 (setq tmpsymbol
(assq (car placeholder
)
9318 '((inexpr-class . class-open
)
9319 (inexpr-statement . block-open
))))
9321 ;; It's a statement block or an anonymous class.
9322 (setq tmpsymbol
(cdr tmpsymbol
))
9323 ;; It's a Pike lambda. Check whether we are between the
9324 ;; lambda keyword and the argument list or at the defun
9326 (setq tmpsymbol
(if (eq char-after-ip ?
{)
9328 'lambda-intro-cont
)))
9329 (goto-char (cdr placeholder
))
9330 (back-to-indentation)
9331 (c-add-stmt-syntax tmpsymbol nil t
9332 (c-most-enclosing-brace c-state-cache
(point))
9334 (unless (eq (point) (cdr placeholder
))
9335 (c-add-syntax (car placeholder
))))
9337 ;; CASE 5: Line is inside a declaration level block or at top level.
9338 ((or containing-decl-open
(null containing-sexp
))
9341 ;; CASE 5A: we are looking at a defun, brace list, class,
9342 ;; or inline-inclass method opening brace
9343 ((setq special-brace-list
9344 (or (and c-special-brace-lists
9345 (c-looking-at-special-brace-list))
9346 (eq char-after-ip ?
{)))
9349 ;; CASE 5A.1: Non-class declaration block open.
9352 (and (eq char-after-ip ?
{)
9353 (setq tmp
(c-looking-at-decl-block containing-sexp t
))
9355 (setq placeholder
(point))
9357 (looking-at c-symbol-key
))
9359 (c-keyword-sym (setq keyword
(match-string 0)))
9360 'c-other-block-decl-kwds
))))
9361 (goto-char placeholder
)
9363 (if (string-equal keyword
"extern")
9364 ;; Special case for extern-lang-open.
9366 (intern (concat keyword
"-open")))
9367 nil t containing-sexp paren-state
))
9369 ;; CASE 5A.2: we are looking at a class opening brace
9371 (goto-char indent-point
)
9372 (skip-chars-forward " \t")
9373 (and (eq (char-after) ?
{)
9374 (c-looking-at-decl-block containing-sexp t
)
9375 (setq placeholder
(point))))
9376 (c-add-syntax 'class-open placeholder
))
9378 ;; CASE 5A.3: brace list open
9380 (c-beginning-of-decl-1 lim
)
9381 (while (looking-at c-specifier-key
)
9382 (goto-char (match-end 1))
9383 (c-forward-syntactic-ws indent-point
))
9384 (setq placeholder
(c-point 'boi
))
9385 (or (consp special-brace-list
)
9386 (and (or (save-excursion
9387 (goto-char indent-point
)
9388 (setq tmpsymbol nil
)
9389 (while (and (> (point) placeholder
)
9390 (zerop (c-backward-token-2 1 t
))
9391 (/= (char-after) ?
=))
9392 (and c-opt-inexpr-brace-list-key
9394 (looking-at c-opt-inexpr-brace-list-key
)
9395 (setq tmpsymbol
'topmost-intro-cont
)))
9396 (eq (char-after) ?
=))
9397 (looking-at c-brace-list-key
))
9399 (while (and (< (point) indent-point
)
9400 (zerop (c-forward-token-2 1 t
))
9401 (not (memq (char-after) '(?\
; ?\()))))
9402 (not (memq (char-after) '(?\
; ?\()))
9404 (if (and (not c-auto-newline-analysis
)
9405 (c-major-mode-is 'java-mode
)
9406 (eq tmpsymbol
'topmost-intro-cont
))
9407 ;; We're in Java and have found that the open brace
9408 ;; belongs to a "new Foo[]" initialization list,
9409 ;; which means the brace list is part of an
9410 ;; expression and not a top level definition. We
9411 ;; therefore treat it as any topmost continuation
9412 ;; even though the semantically correct symbol still
9413 ;; is brace-list-open, on the same grounds as in
9416 (c-beginning-of-statement-1 lim
)
9417 (c-add-syntax 'topmost-intro-cont
(c-point 'boi
)))
9418 (c-add-syntax 'brace-list-open placeholder
)))
9420 ;; CASE 5A.4: inline defun open
9421 ((and containing-decl-open
9422 (not (c-keyword-member containing-decl-kwd
9423 'c-other-block-decl-kwds
)))
9424 (c-add-syntax 'inline-open
)
9425 (c-add-class-syntax 'inclass
9426 containing-decl-open
9427 containing-decl-start
9431 ;; CASE 5A.5: ordinary defun open
9434 (c-beginning-of-decl-1 lim
)
9435 (while (looking-at c-specifier-key
)
9436 (goto-char (match-end 1))
9437 (c-forward-syntactic-ws indent-point
))
9438 (c-add-syntax 'defun-open
(c-point 'boi
))
9439 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9443 ;; CASE 5B: After a function header but before the body (or
9444 ;; the ending semicolon if there's no body).
9446 (when (setq placeholder
(c-just-after-func-arglist-p lim
))
9447 (setq tmp-pos
(point))))
9450 ;; CASE 5B.1: Member init list.
9451 ((eq (char-after tmp-pos
) ?
:)
9452 (if (or (>= tmp-pos indent-point
)
9453 (= (c-point 'bosws
) (1+ tmp-pos
)))
9455 ;; There is no preceding member init clause.
9456 ;; Indent relative to the beginning of indentation
9457 ;; for the topmost-intro line that contains the
9458 ;; prototype's open paren.
9459 (goto-char placeholder
)
9460 (c-add-syntax 'member-init-intro
(c-point 'boi
)))
9461 ;; Indent relative to the first member init clause.
9462 (goto-char (1+ tmp-pos
))
9463 (c-forward-syntactic-ws)
9464 (c-add-syntax 'member-init-cont
(point))))
9466 ;; CASE 5B.2: K&R arg decl intro
9467 ((and c-recognize-knr-p
9468 (c-in-knr-argdecl lim
))
9469 (c-beginning-of-statement-1 lim
)
9470 (c-add-syntax 'knr-argdecl-intro
(c-point 'boi
))
9471 (if containing-decl-open
9472 (c-add-class-syntax 'inclass
9473 containing-decl-open
9474 containing-decl-start
9478 ;; CASE 5B.4: Nether region after a C++ or Java func
9479 ;; decl, which could include a `throws' declaration.
9481 (c-beginning-of-statement-1 lim
)
9482 (c-add-syntax 'func-decl-cont
(c-point 'boi
))
9485 ;; CASE 5C: inheritance line. could be first inheritance
9486 ;; line, or continuation of a multiple inheritance
9487 ((or (and (c-major-mode-is 'c
++-mode
)
9489 (when (eq char-after-ip ?
,)
9490 (skip-chars-forward " \t")
9492 (looking-at c-opt-postfix-decl-spec-key
)))
9493 (and (or (eq char-before-ip ?
:)
9494 ;; watch out for scope operator
9496 (and (eq char-after-ip ?
:)
9497 (c-safe (forward-char 1) t
)
9498 (not (eq (char-after) ?
:))
9501 (c-backward-syntactic-ws lim
)
9502 (if (eq char-before-ip ?
:)
9505 (c-backward-syntactic-ws lim
)))
9506 (back-to-indentation)
9507 (looking-at c-class-key
)))
9509 (and (c-major-mode-is 'java-mode
)
9510 (let ((fence (save-excursion
9511 (c-beginning-of-statement-1 lim
)
9516 (cond ((looking-at c-opt-postfix-decl-spec-key
)
9517 (setq injava-inher
(cons cont
(point))
9519 ((or (not (c-safe (c-forward-sexp -
1) t
))
9525 (not (c-crosses-statement-barrier-p (cdr injava-inher
)
9530 ;; CASE 5C.1: non-hanging colon on an inher intro
9531 ((eq char-after-ip ?
:)
9532 (c-beginning-of-statement-1 lim
)
9533 (c-add-syntax 'inher-intro
(c-point 'boi
))
9534 ;; don't add inclass symbol since relative point already
9535 ;; contains any class offset
9538 ;; CASE 5C.2: hanging colon on an inher intro
9539 ((eq char-before-ip ?
:)
9540 (c-beginning-of-statement-1 lim
)
9541 (c-add-syntax 'inher-intro
(c-point 'boi
))
9542 (if containing-decl-open
9543 (c-add-class-syntax 'inclass
9544 containing-decl-open
9545 containing-decl-start
9549 ;; CASE 5C.3: in a Java implements/extends
9551 (let ((where (cdr injava-inher
))
9552 (cont (car injava-inher
)))
9554 (cond ((looking-at "throws\\>[^_]")
9555 (c-add-syntax 'func-decl-cont
9556 (progn (c-beginning-of-statement-1 lim
)
9558 (cont (c-add-syntax 'inher-cont where
))
9559 (t (c-add-syntax 'inher-intro
9560 (progn (goto-char (cdr injava-inher
))
9561 (c-beginning-of-statement-1 lim
)
9565 ;; CASE 5C.4: a continued inheritance line
9567 (c-beginning-of-inheritance-list lim
)
9568 (c-add-syntax 'inher-cont
(point))
9569 ;; don't add inclass symbol since relative point already
9570 ;; contains any class offset
9573 ;; CASE 5D: this could be a top-level initialization, a
9574 ;; member init list continuation, or a template argument
9575 ;; list continuation.
9577 ;; Note: We use the fact that lim is always after any
9578 ;; preceding brace sexp.
9579 (if c-recognize-
<>-arglists
9582 (c-syntactic-skip-backward "^;,=<>" lim t
)
9585 (when c-overloadable-operators-regexp
9586 (when (setq placeholder
(c-after-special-operator-id lim
))
9587 (goto-char placeholder
)
9590 ((eq (char-before) ?
>)
9591 (or (c-backward-<>-arglist nil lim
)
9594 ((eq (char-before) ?
<)
9597 (c-forward-<>-arglist nil
))
9598 (progn (forward-char)
9602 ;; NB: No c-after-special-operator-id stuff in this
9603 ;; clause - we assume only C++ needs it.
9604 (c-syntactic-skip-backward "^;,=" lim t
))
9605 (memq (char-before) '(?
, ?
= ?
<)))
9608 ;; CASE 5D.3: perhaps a template list continuation?
9609 ((and (c-major-mode-is 'c
++-mode
)
9612 (c-with-syntax-table c
++-template-syntax-table
9613 (goto-char indent-point
)
9614 (setq placeholder
(c-up-list-backward))
9616 (eq (char-after placeholder
) ?
<))))))
9617 (c-with-syntax-table c
++-template-syntax-table
9618 (goto-char placeholder
)
9619 (c-beginning-of-statement-1 lim t
)
9621 (c-backward-syntactic-ws lim
)
9622 (eq (char-before) ?
<))
9623 ;; In a nested template arglist.
9625 (goto-char placeholder
)
9626 (c-syntactic-skip-backward "^,;" lim t
)
9627 (c-forward-syntactic-ws))
9628 (back-to-indentation)))
9629 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9631 (c-add-syntax 'template-args-cont
(point) placeholder
))
9633 ;; CASE 5D.4: perhaps a multiple inheritance line?
9634 ((and (c-major-mode-is 'c
++-mode
)
9636 (c-beginning-of-statement-1 lim
)
9637 (setq placeholder
(point))
9638 (if (looking-at "static\\>[^_]")
9639 (c-forward-token-2 1 nil indent-point
))
9640 (and (looking-at c-class-key
)
9641 (zerop (c-forward-token-2 2 nil indent-point
))
9642 (if (eq (char-after) ?
<)
9643 (c-with-syntax-table c
++-template-syntax-table
9644 (zerop (c-forward-token-2 1 t indent-point
)))
9646 (eq (char-after) ?
:))))
9647 (goto-char placeholder
)
9648 (c-add-syntax 'inher-cont
(c-point 'boi
)))
9650 ;; CASE 5D.5: Continuation of the "expression part" of a
9651 ;; top level construct. Or, perhaps, an unrecognized construct.
9653 (while (and (setq placeholder
(point))
9654 (eq (car (c-beginning-of-decl-1 containing-sexp
))
9657 (c-backward-syntactic-ws)
9658 (eq (char-before) ?
}))
9659 (< (point) placeholder
)))
9662 ((eq (point) placeholder
) 'statement
) ; unrecognized construct
9663 ;; A preceding comma at the top level means that a
9664 ;; new variable declaration starts here. Use
9665 ;; topmost-intro-cont for it, for consistency with
9666 ;; the first variable declaration. C.f. case 5N.
9667 ((eq char-before-ip ?
,) 'topmost-intro-cont
)
9668 (t 'statement-cont
))
9669 nil nil containing-sexp paren-state
))
9672 ;; CASE 5F: Close of a non-class declaration level block.
9673 ((and (eq char-after-ip ?
})
9674 (c-keyword-member containing-decl-kwd
9675 'c-other-block-decl-kwds
))
9676 ;; This is inconsistent: Should use `containing-decl-open'
9677 ;; here if it's at boi, like in case 5J.
9678 (goto-char containing-decl-start
)
9680 (if (string-equal (symbol-name containing-decl-kwd
) "extern")
9681 ;; Special case for compatibility with the
9682 ;; extern-lang syntactic symbols.
9684 (intern (concat (symbol-name containing-decl-kwd
)
9687 (c-most-enclosing-brace paren-state
(point))
9690 ;; CASE 5G: we are looking at the brace which closes the
9691 ;; enclosing nested class decl
9692 ((and containing-sexp
9693 (eq char-after-ip ?
})
9694 (eq containing-decl-open containing-sexp
))
9695 (c-add-class-syntax 'class-close
9696 containing-decl-open
9697 containing-decl-start
9701 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9702 ((and c-recognize-knr-p
9703 (not containing-sexp
) ; can't be knr inside braces.
9704 (not (eq char-before-ip ?
}))
9706 (setq placeholder
(cdr (c-beginning-of-decl-1 lim
)))
9708 ;; Do an extra check to avoid tripping up on
9709 ;; statements that occur in invalid contexts
9710 ;; (e.g. in macro bodies where we don't really
9711 ;; know the context of what we're looking at).
9712 (not (and c-opt-block-stmt-key
9713 (looking-at c-opt-block-stmt-key
)))))
9714 (< placeholder indent-point
))
9715 (goto-char placeholder
)
9716 (c-add-syntax 'knr-argdecl
(point)))
9718 ;; CASE 5I: ObjC method definition.
9719 ((and c-opt-method-key
9720 (looking-at c-opt-method-key
))
9721 (c-beginning-of-statement-1 nil t
)
9722 (if (= (point) indent-point
)
9723 ;; Handle the case when it's the first (non-comment)
9724 ;; thing in the buffer. Can't look for a 'same return
9725 ;; value from cbos1 since ObjC directives currently
9726 ;; aren't recognized fully, so that we get 'same
9727 ;; instead of 'previous if it moved over a preceding
9729 (goto-char (point-min)))
9730 (c-add-syntax 'objc-method-intro
(c-point 'boi
)))
9732 ;; CASE 5P: AWK pattern or function or continuation
9734 ((c-major-mode-is 'awk-mode
)
9735 (setq placeholder
(point))
9737 (if (and (eq (c-beginning-of-statement-1) 'same
)
9738 (/= (point) placeholder
))
9742 containing-sexp paren-state
))
9744 ;; CASE 5N: At a variable declaration that follows a class
9745 ;; definition or some other block declaration that doesn't
9746 ;; end at the closing '}'. C.f. case 5D.5.
9748 (c-backward-syntactic-ws lim
)
9749 (and (eq (char-before) ?
})
9751 (let ((start (point)))
9752 (if (and c-state-cache
9753 (consp (car c-state-cache
))
9754 (eq (cdar c-state-cache
) (point)))
9755 ;; Speed up the backward search a bit.
9756 (goto-char (caar c-state-cache
)))
9757 (c-beginning-of-decl-1 containing-sexp
)
9758 (setq placeholder
(point))
9759 (if (= start
(point))
9760 ;; The '}' is unbalanced.
9763 (>= (point) indent-point
))))))
9764 (goto-char placeholder
)
9765 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9766 containing-sexp paren-state
))
9768 ;; NOTE: The point is at the end of the previous token here.
9770 ;; CASE 5J: we are at the topmost level, make
9771 ;; sure we skip back past any access specifiers
9773 ;; A macro continuation line is never at top level.
9774 (not (and macro-start
9775 (> indent-point macro-start
)))
9777 (setq placeholder
(point))
9778 (or (memq char-before-ip
'(?\
; ?{ ?} nil))
9779 (c-at-vsemi-p before-ws-ip
)
9780 (when (and (eq char-before-ip ?
:)
9781 (eq (c-beginning-of-statement-1 lim
)
9783 (c-backward-syntactic-ws lim
)
9784 (setq placeholder
(point)))
9785 (and (c-major-mode-is 'objc-mode
)
9786 (catch 'not-in-directive
9787 (c-beginning-of-statement-1 lim
)
9788 (setq placeholder
(point))
9789 (while (and (c-forward-objc-directive)
9790 (< (point) indent-point
))
9791 (c-forward-syntactic-ws)
9792 (if (>= (point) indent-point
)
9793 (throw 'not-in-directive t
))
9794 (setq placeholder
(point)))
9796 ;; For historic reasons we anchor at bol of the last
9797 ;; line of the previous declaration. That's clearly
9798 ;; highly bogus and useless, and it makes our lives hard
9799 ;; to remain compatible. :P
9800 (goto-char placeholder
)
9801 (c-add-syntax 'topmost-intro
(c-point 'bol
))
9802 (if containing-decl-open
9803 (if (c-keyword-member containing-decl-kwd
9804 'c-other-block-decl-kwds
)
9806 (goto-char (c-brace-anchor-point containing-decl-open
))
9808 (if (string-equal (symbol-name containing-decl-kwd
)
9810 ;; Special case for compatibility with the
9811 ;; extern-lang syntactic symbols.
9813 (intern (concat "in"
9814 (symbol-name containing-decl-kwd
))))
9816 (c-most-enclosing-brace paren-state
(point))
9818 (c-add-class-syntax 'inclass
9819 containing-decl-open
9820 containing-decl-start
9823 (when (and c-syntactic-indentation-in-macros
9825 (/= macro-start
(c-point 'boi indent-point
)))
9826 (c-add-syntax 'cpp-define-intro
)
9827 (setq macro-start nil
)))
9829 ;; CASE 5K: we are at an ObjC method definition
9830 ;; continuation line.
9831 ((and c-opt-method-key
9833 (c-beginning-of-statement-1 lim
)
9835 (when (looking-at c-opt-method-key
)
9836 (setq placeholder
(point)))))
9837 (c-add-syntax 'objc-method-args-cont placeholder
))
9839 ;; CASE 5L: we are at the first argument of a template
9840 ;; arglist that begins on the previous line.
9841 ((and c-recognize-
<>-arglists
9842 (eq (char-before) ?
<)
9843 (not (and c-overloadable-operators-regexp
9844 (c-after-special-operator-id lim
))))
9845 (c-beginning-of-statement-1 (c-safe-position (point) paren-state
))
9846 (c-add-syntax 'template-args-cont
(c-point 'boi
)))
9848 ;; CASE 5Q: we are at a statement within a macro.
9850 (c-beginning-of-statement-1 containing-sexp
)
9851 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state
))
9853 ;;CASE 5N: We are at a topmost continuation line and the only
9854 ;;preceding items are annotations.
9855 ((and (c-major-mode-is 'java-mode
)
9856 (setq placeholder
(point))
9857 (c-beginning-of-statement-1)
9859 (while (and (c-forward-annotation))
9860 (c-forward-syntactic-ws))
9863 (>= (point) placeholder
)
9864 (goto-char placeholder
)))
9865 (c-add-syntax 'annotation-top-cont
(c-point 'boi
)))
9867 ;; CASE 5M: we are at a topmost continuation line
9869 (c-beginning-of-statement-1 (c-safe-position (point) paren-state
))
9870 (when (c-major-mode-is 'objc-mode
)
9871 (setq placeholder
(point))
9872 (while (and (c-forward-objc-directive)
9873 (< (point) indent-point
))
9874 (c-forward-syntactic-ws)
9875 (setq placeholder
(point)))
9876 (goto-char placeholder
))
9877 (c-add-syntax 'topmost-intro-cont
(c-point 'boi
)))
9881 ;; (CASE 6 has been removed.)
9883 ;; CASE 7: line is an expression, not a statement. Most
9884 ;; likely we are either in a function prototype or a function
9885 ;; call argument list
9886 ((not (or (and c-special-brace-lists
9888 (goto-char containing-sexp
)
9889 (c-looking-at-special-brace-list)))
9890 (eq (char-after containing-sexp
) ?
{)))
9893 ;; CASE 7A: we are looking at the arglist closing paren.
9895 ((memq char-after-ip
'(?\
) ?\
]))
9896 (goto-char containing-sexp
)
9897 (setq placeholder
(c-point 'boi
))
9898 (if (and (c-safe (backward-up-list 1) t
)
9899 (>= (point) placeholder
))
9902 (skip-chars-forward " \t"))
9903 (goto-char placeholder
))
9904 (c-add-stmt-syntax 'arglist-close
(list containing-sexp
) t
9905 (c-most-enclosing-brace paren-state
(point))
9908 ;; CASE 7B: Looking at the opening brace of an
9909 ;; in-expression block or brace list. C.f. cases 4, 16A
9911 ((and (eq char-after-ip ?
{)
9913 (setq placeholder
(c-inside-bracelist-p (point)
9916 (setq tmpsymbol
'(brace-list-open . inexpr-class
))
9917 (setq tmpsymbol
'(block-open . inexpr-statement
)
9919 (cdr-safe (c-looking-at-inexpr-block
9920 (c-safe-position containing-sexp
9923 ;; placeholder is nil if it's a block directly in
9924 ;; a function arglist. That makes us skip out of
9927 (goto-char placeholder
)
9928 (back-to-indentation)
9929 (c-add-stmt-syntax (car tmpsymbol
) nil t
9930 (c-most-enclosing-brace paren-state
(point))
9932 (if (/= (point) placeholder
)
9933 (c-add-syntax (cdr tmpsymbol
))))
9935 ;; CASE 7C: we are looking at the first argument in an empty
9936 ;; argument list. Use arglist-close if we're actually
9937 ;; looking at a close paren or bracket.
9938 ((memq char-before-ip
'(?\
( ?\
[))
9939 (goto-char containing-sexp
)
9940 (setq placeholder
(c-point 'boi
))
9941 (if (and (c-safe (backward-up-list 1) t
)
9942 (>= (point) placeholder
))
9945 (skip-chars-forward " \t"))
9946 (goto-char placeholder
))
9947 (c-add-stmt-syntax 'arglist-intro
(list containing-sexp
) t
9948 (c-most-enclosing-brace paren-state
(point))
9951 ;; CASE 7D: we are inside a conditional test clause. treat
9952 ;; these things as statements
9954 (goto-char containing-sexp
)
9955 (and (c-safe (c-forward-sexp -
1) t
)
9956 (looking-at "\\<for\\>[^_]")))
9957 (goto-char (1+ containing-sexp
))
9958 (c-forward-syntactic-ws indent-point
)
9959 (if (eq char-before-ip ?\
;)
9960 (c-add-syntax 'statement
(point))
9961 (c-add-syntax 'statement-cont
(point))
9964 ;; CASE 7E: maybe a continued ObjC method call. This is the
9965 ;; case when we are inside a [] bracketed exp, and what
9966 ;; precede the opening bracket is not an identifier.
9967 ((and c-opt-method-key
9968 (eq (char-after containing-sexp
) ?\
[)
9970 (goto-char (1- containing-sexp
))
9971 (c-backward-syntactic-ws (c-point 'bod
))
9972 (if (not (looking-at c-symbol-key
))
9973 (c-add-syntax 'objc-method-call-cont containing-sexp
))
9976 ;; CASE 7F: we are looking at an arglist continuation line,
9977 ;; but the preceding argument is on the same line as the
9978 ;; opening paren. This case includes multi-line
9979 ;; mathematical paren groupings, but we could be on a
9980 ;; for-list continuation line. C.f. case 7A.
9982 (goto-char (1+ containing-sexp
))
9984 (c-forward-syntactic-ws)
9987 (goto-char containing-sexp
) ; paren opening the arglist
9988 (setq placeholder
(c-point 'boi
))
9989 (if (and (c-safe (backward-up-list 1) t
)
9990 (>= (point) placeholder
))
9993 (skip-chars-forward " \t"))
9994 (goto-char placeholder
))
9995 (c-add-stmt-syntax 'arglist-cont-nonempty
(list containing-sexp
) t
9996 (c-most-enclosing-brace c-state-cache
(point))
9999 ;; CASE 7G: we are looking at just a normal arglist
10000 ;; continuation line
10001 (t (c-forward-syntactic-ws indent-point
)
10002 (c-add-syntax 'arglist-cont
(c-point 'boi
)))
10005 ;; CASE 8: func-local multi-inheritance line
10006 ((and (c-major-mode-is 'c
++-mode
)
10008 (goto-char indent-point
)
10009 (skip-chars-forward " \t")
10010 (looking-at c-opt-postfix-decl-spec-key
)))
10011 (goto-char indent-point
)
10012 (skip-chars-forward " \t")
10015 ;; CASE 8A: non-hanging colon on an inher intro
10016 ((eq char-after-ip ?
:)
10017 (c-backward-syntactic-ws lim
)
10018 (c-add-syntax 'inher-intro
(c-point 'boi
)))
10020 ;; CASE 8B: hanging colon on an inher intro
10021 ((eq char-before-ip ?
:)
10022 (c-add-syntax 'inher-intro
(c-point 'boi
)))
10024 ;; CASE 8C: a continued inheritance line
10026 (c-beginning-of-inheritance-list lim
)
10027 (c-add-syntax 'inher-cont
(point))
10030 ;; CASE 9: we are inside a brace-list
10031 ((and (not (c-major-mode-is 'awk-mode
)) ; Maybe this isn't needed (ACM, 2002/3/29)
10032 (setq special-brace-list
10033 (or (and c-special-brace-lists
;;;; ALWAYS NIL FOR AWK!!
10035 (goto-char containing-sexp
)
10036 (c-looking-at-special-brace-list)))
10037 (c-inside-bracelist-p containing-sexp paren-state
))))
10040 ;; CASE 9A: In the middle of a special brace list opener.
10041 ((and (consp special-brace-list
)
10043 (goto-char containing-sexp
)
10044 (eq (char-after) ?\
())
10045 (eq char-after-ip
(car (cdr special-brace-list
))))
10046 (goto-char (car (car special-brace-list
)))
10047 (skip-chars-backward " \t")
10049 (assoc 'statement-cont
10050 (setq placeholder
(c-guess-basic-syntax))))
10051 (setq c-syntactic-context placeholder
)
10052 (c-beginning-of-statement-1
10053 (c-safe-position (1- containing-sexp
) paren-state
))
10054 (c-forward-token-2 0)
10055 (while (looking-at c-specifier-key
)
10056 (goto-char (match-end 1))
10057 (c-forward-syntactic-ws))
10058 (c-add-syntax 'brace-list-open
(c-point 'boi
))))
10060 ;; CASE 9B: brace-list-close brace
10061 ((if (consp special-brace-list
)
10062 ;; Check special brace list closer.
10064 (goto-char (car (car special-brace-list
)))
10066 (goto-char indent-point
)
10067 (back-to-indentation)
10069 ;; We were between the special close char and the `)'.
10070 (and (eq (char-after) ?\
))
10071 (eq (1+ (point)) (cdr (car special-brace-list
))))
10072 ;; We were before the special close char.
10073 (and (eq (char-after) (cdr (cdr special-brace-list
)))
10074 (zerop (c-forward-token-2))
10075 (eq (1+ (point)) (cdr (car special-brace-list
)))))))
10076 ;; Normal brace list check.
10077 (and (eq char-after-ip ?
})
10078 (c-safe (goto-char (c-up-list-backward (point))) t
)
10079 (= (point) containing-sexp
)))
10080 (if (eq (point) (c-point 'boi
))
10081 (c-add-syntax 'brace-list-close
(point))
10082 (setq lim
(c-most-enclosing-brace c-state-cache
(point)))
10083 (c-beginning-of-statement-1 lim
)
10084 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state
)))
10087 ;; Prepare for the rest of the cases below by going to the
10088 ;; token following the opening brace
10089 (if (consp special-brace-list
)
10091 (goto-char (car (car special-brace-list
)))
10092 (c-forward-token-2 1 nil indent-point
))
10093 (goto-char containing-sexp
))
10095 (let ((start (point)))
10096 (c-forward-syntactic-ws indent-point
)
10097 (goto-char (max start
(c-point 'bol
))))
10098 (c-skip-ws-forward indent-point
)
10101 ;; CASE 9C: we're looking at the first line in a brace-list
10102 ((= (point) indent-point
)
10103 (if (consp special-brace-list
)
10104 (goto-char (car (car special-brace-list
)))
10105 (goto-char containing-sexp
))
10106 (if (eq (point) (c-point 'boi
))
10107 (c-add-syntax 'brace-list-intro
(point))
10108 (setq lim
(c-most-enclosing-brace c-state-cache
(point)))
10109 (c-beginning-of-statement-1 lim
)
10110 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state
)))
10112 ;; CASE 9D: this is just a later brace-list-entry or
10113 ;; brace-entry-open
10114 (t (if (or (eq char-after-ip ?
{)
10115 (and c-special-brace-lists
10117 (goto-char indent-point
)
10118 (c-forward-syntactic-ws (c-point 'eol
))
10119 (c-looking-at-special-brace-list (point)))))
10120 (c-add-syntax 'brace-entry-open
(point))
10121 (c-add-syntax 'brace-list-entry
(point))
10125 ;; CASE 10: A continued statement or top level construct.
10126 ((and (not (memq char-before-ip
'(?\
; ?:)))
10127 (not (c-at-vsemi-p before-ws-ip
))
10128 (or (not (eq char-before-ip ?
}))
10129 (c-looking-at-inexpr-block-backward c-state-cache
))
10132 (c-beginning-of-statement-1 containing-sexp
)
10133 (setq placeholder
(point))))
10134 (/= placeholder containing-sexp
))
10135 ;; This is shared with case 18.
10136 (c-guess-continued-construct indent-point
10142 ;; CASE 16: block close brace, possibly closing the defun or
10144 ((eq char-after-ip ?
})
10145 ;; From here on we have the next containing sexp in lim.
10146 (setq lim
(c-most-enclosing-brace paren-state
))
10147 (goto-char containing-sexp
)
10150 ;; CASE 16E: Closing a statement block? This catches
10151 ;; cases where it's preceded by a statement keyword,
10152 ;; which works even when used in an "invalid" context,
10153 ;; e.g. a macro argument.
10154 ((c-after-conditional)
10155 (c-backward-to-block-anchor lim
)
10156 (c-add-stmt-syntax 'block-close nil t lim paren-state
))
10158 ;; CASE 16A: closing a lambda defun or an in-expression
10159 ;; block? C.f. cases 4, 7B and 17E.
10160 ((setq placeholder
(c-looking-at-inexpr-block
10161 (c-safe-position containing-sexp paren-state
)
10163 (setq tmpsymbol
(if (eq (car placeholder
) 'inlambda
)
10166 (goto-char containing-sexp
)
10167 (back-to-indentation)
10168 (if (= containing-sexp
(point))
10169 (c-add-syntax tmpsymbol
(point))
10170 (goto-char (cdr placeholder
))
10171 (back-to-indentation)
10172 (c-add-stmt-syntax tmpsymbol nil t
10173 (c-most-enclosing-brace paren-state
(point))
10175 (if (/= (point) (cdr placeholder
))
10176 (c-add-syntax (car placeholder
)))))
10178 ;; CASE 16B: does this close an inline or a function in
10179 ;; a non-class declaration level block?
10184 (c-looking-at-decl-block
10185 (c-most-enclosing-brace paren-state lim
)
10187 (setq placeholder
(point))))
10188 (c-backward-to-decl-anchor lim
)
10189 (back-to-indentation)
10190 (if (save-excursion
10191 (goto-char placeholder
)
10192 (looking-at c-other-decl-block-key
))
10193 (c-add-syntax 'defun-close
(point))
10194 (c-add-syntax 'inline-close
(point))))
10196 ;; CASE 16F: Can be a defun-close of a function declared
10197 ;; in a statement block, e.g. in Pike or when using gcc
10198 ;; extensions, but watch out for macros followed by
10199 ;; blocks. Let it through to be handled below.
10200 ;; C.f. cases B.3 and 17G.
10202 (and (not (c-at-statement-start-p))
10203 (eq (c-beginning-of-statement-1 lim nil nil t
) 'same
)
10204 (setq placeholder
(point))
10205 (let ((c-recognize-typeless-decls nil
))
10206 ;; Turn off recognition of constructs that
10207 ;; lacks a type in this case, since that's more
10208 ;; likely to be a macro followed by a block.
10209 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
10210 (back-to-indentation)
10211 (if (/= (point) containing-sexp
)
10212 (goto-char placeholder
))
10213 (c-add-stmt-syntax 'defun-close nil t lim paren-state
))
10215 ;; CASE 16C: If there is an enclosing brace then this is
10216 ;; a block close since defun closes inside declaration
10217 ;; level blocks have been handled above.
10219 ;; If the block is preceded by a case/switch label on
10220 ;; the same line, we anchor at the first preceding label
10221 ;; at boi. The default handling in c-add-stmt-syntax
10222 ;; really fixes it better, but we do like this to keep
10223 ;; the indentation compatible with version 5.28 and
10224 ;; earlier. C.f. case 17H.
10225 (while (and (/= (setq placeholder
(point)) (c-point 'boi
))
10226 (eq (c-beginning-of-statement-1 lim
) 'label
)))
10227 (goto-char placeholder
)
10228 (if (looking-at c-label-kwds-regexp
)
10229 (c-add-syntax 'block-close
(point))
10230 (goto-char containing-sexp
)
10231 ;; c-backward-to-block-anchor not necessary here; those
10232 ;; situations are handled in case 16E above.
10233 (c-add-stmt-syntax 'block-close nil t lim paren-state
)))
10235 ;; CASE 16D: Only top level defun close left.
10237 (goto-char containing-sexp
)
10238 (c-backward-to-decl-anchor lim
)
10239 (c-add-stmt-syntax 'defun-close nil nil
10240 (c-most-enclosing-brace paren-state
)
10244 ;; CASE 19: line is an expression, not a statement, and is directly
10245 ;; contained by a template delimiter. Most likely, we are in a
10246 ;; template arglist within a statement. This case is based on CASE
10247 ;; 7. At some point in the future, we may wish to create more
10248 ;; syntactic symbols such as `template-intro',
10249 ;; `template-cont-nonempty', etc., and distinguish between them as we
10250 ;; do for `arglist-intro' etc. (2009-12-07).
10251 ((and c-recognize-
<>-arglists
10252 (setq containing-
< (c-up-list-backward indent-point containing-sexp
))
10253 (eq (char-after containing-
<) ?\
<))
10254 (setq placeholder
(c-point 'boi containing-
<))
10255 (goto-char containing-sexp
) ; Most nested Lbrace/Lparen (but not
10256 ; '<') before indent-point.
10257 (if (>= (point) placeholder
)
10260 (skip-chars-forward " \t"))
10261 (goto-char placeholder
))
10262 (c-add-stmt-syntax 'template-args-cont
(list containing-
<) t
10263 (c-most-enclosing-brace c-state-cache
(point))
10266 ;; CASE 17: Statement or defun catchall.
10268 (goto-char indent-point
)
10269 ;; Back up statements until we find one that starts at boi.
10270 (while (let* ((prev-point (point))
10271 (last-step-type (c-beginning-of-statement-1
10273 (if (= (point) prev-point
)
10275 (setq step-type
(or step-type last-step-type
))
10277 (setq step-type last-step-type
)
10278 (/= (point) (c-point 'boi
)))))
10281 ;; CASE 17B: continued statement
10282 ((and (eq step-type
'same
)
10283 (/= (point) indent-point
))
10284 (c-add-stmt-syntax 'statement-cont nil nil
10285 containing-sexp paren-state
))
10287 ;; CASE 17A: After a case/default label?
10289 (while (and (eq step-type
'label
)
10290 (not (looking-at c-label-kwds-regexp
)))
10292 (c-beginning-of-statement-1 containing-sexp
)))
10293 (eq step-type
'label
))
10294 (c-add-stmt-syntax (if (eq char-after-ip ?
{)
10295 'statement-case-open
10296 'statement-case-intro
)
10297 nil t containing-sexp paren-state
))
10299 ;; CASE 17D: any old statement
10301 (while (eq step-type
'label
)
10303 (c-beginning-of-statement-1 containing-sexp
)))
10304 (eq step-type
'previous
))
10305 (c-add-stmt-syntax 'statement nil t
10306 containing-sexp paren-state
)
10307 (if (eq char-after-ip ?
{)
10308 (c-add-syntax 'block-open
)))
10310 ;; CASE 17I: Inside a substatement block.
10312 ;; The following tests are all based on containing-sexp.
10313 (goto-char containing-sexp
)
10314 ;; From here on we have the next containing sexp in lim.
10315 (setq lim
(c-most-enclosing-brace paren-state containing-sexp
))
10316 (c-after-conditional))
10317 (c-backward-to-block-anchor lim
)
10318 (c-add-stmt-syntax 'statement-block-intro nil t
10320 (if (eq char-after-ip ?
{)
10321 (c-add-syntax 'block-open
)))
10323 ;; CASE 17E: first statement in an in-expression block.
10324 ;; C.f. cases 4, 7B and 16A.
10325 ((setq placeholder
(c-looking-at-inexpr-block
10326 (c-safe-position containing-sexp paren-state
)
10328 (setq tmpsymbol
(if (eq (car placeholder
) 'inlambda
)
10330 'statement-block-intro
))
10331 (back-to-indentation)
10332 (if (= containing-sexp
(point))
10333 (c-add-syntax tmpsymbol
(point))
10334 (goto-char (cdr placeholder
))
10335 (back-to-indentation)
10336 (c-add-stmt-syntax tmpsymbol nil t
10337 (c-most-enclosing-brace c-state-cache
(point))
10339 (if (/= (point) (cdr placeholder
))
10340 (c-add-syntax (car placeholder
))))
10341 (if (eq char-after-ip ?
{)
10342 (c-add-syntax 'block-open
)))
10344 ;; CASE 17F: first statement in an inline, or first
10345 ;; statement in a top-level defun. we can tell this is it
10346 ;; if there are no enclosing braces that haven't been
10347 ;; narrowed out by a class (i.e. don't use bod here).
10349 (or (not (setq placeholder
(c-most-enclosing-brace
10352 (goto-char placeholder
)
10353 (eq (char-after) ?
{))
10354 (c-looking-at-decl-block (c-most-enclosing-brace
10355 paren-state
(point))
10357 (c-backward-to-decl-anchor lim
)
10358 (back-to-indentation)
10359 (c-add-syntax 'defun-block-intro
(point)))
10361 ;; CASE 17G: First statement in a function declared inside
10362 ;; a normal block. This can occur in Pike and with
10363 ;; e.g. the gcc extensions, but watch out for macros
10364 ;; followed by blocks. C.f. cases B.3 and 16F.
10366 (and (not (c-at-statement-start-p))
10367 (eq (c-beginning-of-statement-1 lim nil nil t
) 'same
)
10368 (setq placeholder
(point))
10369 (let ((c-recognize-typeless-decls nil
))
10370 ;; Turn off recognition of constructs that lacks
10371 ;; a type in this case, since that's more likely
10372 ;; to be a macro followed by a block.
10373 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
10374 (back-to-indentation)
10375 (if (/= (point) containing-sexp
)
10376 (goto-char placeholder
))
10377 (c-add-stmt-syntax 'defun-block-intro nil t
10380 ;; CASE 17H: First statement in a block.
10382 ;; If the block is preceded by a case/switch label on the
10383 ;; same line, we anchor at the first preceding label at
10384 ;; boi. The default handling in c-add-stmt-syntax is
10385 ;; really fixes it better, but we do like this to keep the
10386 ;; indentation compatible with version 5.28 and earlier.
10388 (while (and (/= (setq placeholder
(point)) (c-point 'boi
))
10389 (eq (c-beginning-of-statement-1 lim
) 'label
)))
10390 (goto-char placeholder
)
10391 (if (looking-at c-label-kwds-regexp
)
10392 (c-add-syntax 'statement-block-intro
(point))
10393 (goto-char containing-sexp
)
10394 ;; c-backward-to-block-anchor not necessary here; those
10395 ;; situations are handled in case 17I above.
10396 (c-add-stmt-syntax 'statement-block-intro nil t
10398 (if (eq char-after-ip ?
{)
10399 (c-add-syntax 'block-open
)))
10403 ;; now we need to look at any modifiers
10404 (goto-char indent-point
)
10405 (skip-chars-forward " \t")
10407 ;; are we looking at a comment only line?
10408 (when (and (looking-at c-comment-start-regexp
)
10409 (/= (c-forward-token-2 0 nil
(c-point 'eol
)) 0))
10410 (c-append-syntax 'comment-intro
))
10412 ;; we might want to give additional offset to friends (in C++).
10413 (when (and c-opt-friend-key
10414 (looking-at c-opt-friend-key
))
10415 (c-append-syntax 'friend
))
10417 ;; Set syntactic-relpos.
10418 (let ((p c-syntactic-context
))
10420 (if (integerp (c-langelem-pos (car p
)))
10422 (setq syntactic-relpos
(c-langelem-pos (car p
)))
10427 ;; Start of or a continuation of a preprocessor directive?
10428 (if (and macro-start
10429 (eq macro-start
(c-point 'boi
))
10430 (not (and (c-major-mode-is 'pike-mode
)
10431 (eq (char-after (1+ macro-start
)) ?
\"))))
10432 (c-append-syntax 'cpp-macro
)
10433 (when (and c-syntactic-indentation-in-macros macro-start
)
10436 (< syntactic-relpos macro-start
)
10438 (assq 'arglist-intro c-syntactic-context
)
10439 (assq 'arglist-cont c-syntactic-context
)
10440 (assq 'arglist-cont-nonempty c-syntactic-context
)
10441 (assq 'arglist-close c-syntactic-context
))))
10442 ;; If inside a cpp expression, i.e. anywhere in a
10443 ;; cpp directive except a #define body, we only let
10444 ;; through the syntactic analysis that is internal
10445 ;; in the expression. That means the arglist
10446 ;; elements, if they are anchored inside the cpp
10448 (setq c-syntactic-context nil
)
10449 (c-add-syntax 'cpp-macro-cont macro-start
))
10450 (when (and (eq macro-start syntactic-relpos
)
10451 (not (assq 'cpp-define-intro c-syntactic-context
))
10453 (goto-char macro-start
)
10454 (or (not (c-forward-to-cpp-define-body))
10455 (<= (point) (c-point 'boi indent-point
)))))
10456 ;; Inside a #define body and the syntactic analysis is
10457 ;; anchored on the start of the #define. In this case
10458 ;; we add cpp-define-intro to get the extra
10459 ;; indentation of the #define body.
10460 (c-add-syntax 'cpp-define-intro
)))))
10462 ;; return the syntax
10463 c-syntactic-context
)))
10466 ;; Indentation calculation.
10468 (defun c-evaluate-offset (offset langelem symbol
)
10469 ;; offset can be a number, a function, a variable, a list, or one of
10470 ;; the symbols + or -
10472 ;; This function might do hidden buffer changes.
10475 ((numberp offset
) offset
)
10476 ((vectorp offset
) offset
)
10477 ((null offset
) nil
)
10479 ((eq offset
'+) c-basic-offset
)
10480 ((eq offset
'-
) (- c-basic-offset
))
10481 ((eq offset
'++) (* 2 c-basic-offset
))
10482 ((eq offset
'--
) (* 2 (- c-basic-offset
)))
10483 ((eq offset
'*) (/ c-basic-offset
2))
10484 ((eq offset
'/) (/ (- c-basic-offset
) 2))
10486 ((functionp offset
)
10489 (cons (c-langelem-sym langelem
)
10490 (c-langelem-pos langelem
)))
10495 ((eq (car offset
) 'quote
)
10496 (c-benign-error "The offset %S for %s was mistakenly quoted"
10500 ((memq (car offset
) '(min max
))
10501 (let (res val
(method (car offset
)))
10502 (setq offset
(cdr offset
))
10504 (setq val
(c-evaluate-offset (car offset
) langelem symbol
))
10512 Error evaluating offset %S for %s: \
10513 Cannot combine absolute offset %S with relative %S in `%s' method"
10514 (car offset
) symbol res val method
)
10515 (setq res
(funcall method res val
))))
10519 Error evaluating offset %S for %s: \
10520 Cannot combine relative offset %S with absolute %S in `%s' method"
10521 (car offset
) symbol res val method
)
10522 (setq res
(vector (funcall method
(aref res
0)
10524 (setq offset
(cdr offset
)))
10527 ((eq (car offset
) 'add
)
10529 (setq offset
(cdr offset
))
10531 (setq val
(c-evaluate-offset (car offset
) langelem symbol
))
10538 (setq res
(vector (+ (aref res
0) val
)))
10539 (setq res
(+ res val
))))
10543 Error evaluating offset %S for %s: \
10544 Cannot combine absolute offsets %S and %S in `add' method"
10545 (car offset
) symbol res val
)
10546 (setq res val
)))) ; Override.
10547 (setq offset
(cdr offset
)))
10552 (when (eq (car offset
) 'first
)
10553 (setq offset
(cdr offset
)))
10554 (while (and (not res
) offset
)
10555 (setq res
(c-evaluate-offset (car offset
) langelem symbol
)
10556 offset
(cdr offset
)))
10559 ((and (symbolp offset
) (boundp offset
))
10560 (symbol-value offset
))
10563 (c-benign-error "Unknown offset format %S for %s" offset symbol
)
10566 (if (or (null res
) (integerp res
)
10567 (and (vectorp res
) (= (length res
) 1) (integerp (aref res
0))))
10569 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10573 (defun c-calc-offset (langelem)
10574 ;; Get offset from LANGELEM which is a list beginning with the
10575 ;; syntactic symbol and followed by any analysis data it provides.
10576 ;; That data may be zero or more elements, but if at least one is
10577 ;; given then the first is the anchor position (or nil). The symbol
10578 ;; is matched against `c-offsets-alist' and the offset calculated
10579 ;; from that is returned.
10581 ;; This function might do hidden buffer changes.
10582 (let* ((symbol (c-langelem-sym langelem
))
10583 (match (assq symbol c-offsets-alist
))
10584 (offset (cdr-safe match
)))
10586 (setq offset
(c-evaluate-offset offset langelem symbol
))
10587 (if c-strict-syntax-p
10588 (c-benign-error "No offset found for syntactic symbol %s" symbol
))
10590 (if (vectorp offset
)
10592 (or (and (numberp offset
) offset
)
10593 (and (symbolp offset
) (symbol-value offset
))
10597 (defun c-get-offset (langelem)
10598 ;; This is a compatibility wrapper for `c-calc-offset' in case
10599 ;; someone is calling it directly. It takes an old style syntactic
10600 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10603 ;; This function might do hidden buffer changes.
10604 (if (c-langelem-pos langelem
)
10605 (c-calc-offset (list (c-langelem-sym langelem
)
10606 (c-langelem-pos langelem
)))
10607 (c-calc-offset langelem
)))
10609 (defun c-get-syntactic-indentation (langelems)
10610 ;; Calculate the syntactic indentation from a syntactic description
10611 ;; as returned by `c-guess-syntax'.
10613 ;; Note that topmost-intro always has an anchor position at bol, for
10614 ;; historical reasons. It's often used together with other symbols
10615 ;; that has more sane positions. Since we always use the first
10616 ;; found anchor position, we rely on that these other symbols always
10617 ;; precede topmost-intro in the LANGELEMS list.
10619 ;; This function might do hidden buffer changes.
10620 (let ((indent 0) anchor
)
10623 (let* ((c-syntactic-element (car langelems
))
10624 (res (c-calc-offset c-syntactic-element
)))
10627 ;; Got an absolute column that overrides any indentation
10628 ;; we've collected so far, but not the relative
10629 ;; indentation we might get for the nested structures
10630 ;; further down the langelems list.
10631 (setq indent
(elt res
0)
10632 anchor
(point-min)) ; A position at column 0.
10634 ;; Got a relative change of the current calculated
10636 (setq indent
(+ indent res
))
10638 ;; Use the anchor position from the first syntactic
10639 ;; element with one.
10641 (setq anchor
(c-langelem-pos (car langelems
)))))
10643 (setq langelems
(cdr langelems
))))
10646 (+ indent
(save-excursion
10652 (cc-provide 'cc-engine
)
10654 ;;; cc-engine.el ends here