1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Authors: 2001- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
11 ;; 1987 Stewart Clamen
12 ;; 1985 Richard M. Stallman
13 ;; Maintainer: bug-cc-mode@gnu.org
14 ;; Created: 22-Apr-1997 (split from cc-mode.el)
15 ;; Version: See cc-mode.el
16 ;; Keywords: c languages oop
18 ;; This file is part of GNU Emacs.
20 ;; GNU Emacs is free software: you can redistribute it and/or modify
21 ;; it under the terms of the GNU General Public License as published by
22 ;; the Free Software Foundation, either version 3 of the License, or
23 ;; (at your option) any later version.
25 ;; GNU Emacs is distributed in the hope that it will be useful,
26 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;; GNU General Public License for more details.
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
35 ;; The functions which have docstring documentation can be considered
36 ;; part of an API which other packages can use in CC Mode buffers.
37 ;; Otoh, undocumented functions and functions with the documentation
38 ;; in comments are considered purely internal and can change semantics
39 ;; or even disappear in the future.
41 ;; (This policy applies to CC Mode as a whole, not just this file. It
42 ;; probably also applies to many other Emacs packages, but here it's
43 ;; clearly spelled out.)
45 ;; Hidden buffer changes
47 ;; Various functions in CC Mode use text properties for caching and
48 ;; syntactic markup purposes, and those of them that might modify such
49 ;; properties but still don't modify the buffer in a visible way are
50 ;; said to do "hidden buffer changes". They should be used within
51 ;; `c-save-buffer-state' or a similar function that saves and restores
52 ;; buffer modifiedness, disables buffer change hooks, etc.
54 ;; Interactive functions are assumed to not do hidden buffer changes,
55 ;; except in the specific parts of them that do real changes.
57 ;; Lineup functions are assumed to do hidden buffer changes. They
58 ;; must not do real changes, though.
60 ;; All other functions that do hidden buffer changes have that noted
61 ;; in their doc string or comment.
63 ;; The intention with this system is to avoid wrapping every leaf
64 ;; function that do hidden buffer changes inside
65 ;; `c-save-buffer-state'. It should be used as near the top of the
66 ;; interactive functions as possible.
68 ;; Functions called during font locking are allowed to do hidden
69 ;; buffer changes since the font-lock package run them in a context
70 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
71 ;; inspired by `save-buffer-state' in the font-lock package).
73 ;; Use of text properties
75 ;; CC Mode uses several text properties internally to mark up various
76 ;; positions, e.g. to improve speed and to eliminate glitches in
77 ;; interactive refontification.
79 ;; Note: This doc is for internal use only. Other packages should not
80 ;; assume that these text properties are used as described here.
83 ;; Used to modify the syntax of some characters. It is used to
84 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
85 ;; to "hide" obtrusive characters in preprocessor lines.
87 ;; This property is used on single characters and is therefore
88 ;; always treated as front and rear nonsticky (or start and end open
89 ;; in XEmacs vocabulary). It's therefore installed on
90 ;; `text-property-default-nonsticky' if that variable exists (Emacs
93 ;; 'c-is-sws and 'c-in-sws
94 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
95 ;; speed them up. See the comment blurb before `c-put-is-sws'
96 ;; below for further details.
99 ;; This property is used on single characters to mark positions with
100 ;; special syntactic relevance of various sorts. Its primary use is
101 ;; to avoid glitches when multiline constructs are refontified
102 ;; interactively (on font lock decoration level 3). It's cleared in
103 ;; a region before it's fontified and is then put on relevant chars
104 ;; in that region as they are encountered during the fontification.
105 ;; The value specifies the kind of position:
108 ;; Put on the last char of the token preceding each declaration
109 ;; inside a declaration style arglist (typically in a function
113 ;; Put on the last char of the token preceding a declaration.
114 ;; This is used in cases where declaration boundaries can't be
115 ;; recognized simply by looking for a token like ";" or "}".
116 ;; `c-type-decl-end-used' must be set if this is used (see also
117 ;; `c-find-decl-spots').
120 ;; Put on the commas that separate arguments in angle bracket
121 ;; arglists like C++ template arglists.
123 ;; 'c-decl-id-start and 'c-decl-type-start
124 ;; Put on the last char of the token preceding each declarator
125 ;; in the declarator list of a declaration. They are also used
126 ;; between the identifiers cases like enum declarations.
127 ;; 'c-decl-type-start is used when the declarators are types,
128 ;; 'c-decl-id-start otherwise.
131 ;; Used in AWK mode to mark the various kinds of newlines. See
138 (if (and (boundp 'byte-compile-dest-file
)
139 (stringp byte-compile-dest-file
))
140 (cons (file-name-directory byte-compile-dest-file
) load-path
)
142 (load "cc-bytecomp" nil t
)))
144 (cc-require 'cc-defs
)
145 (cc-require-when-compile 'cc-langs
)
146 (cc-require 'cc-vars
)
148 ;; Silence the compiler.
149 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
152 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
154 (defmacro c-declare-lang-variables
()
157 (mapcar (lambda (init)
159 `(defvar ,(car init
) nil
,(elt init
2))
160 `(defvar ,(car init
) nil
))
161 (make-variable-buffer-local ',(car init
))))
162 (cdr c-lang-variable-inits
)))))
163 (c-declare-lang-variables)
166 ;;; Internal state variables.
168 ;; Internal state of hungry delete key feature
169 (defvar c-hungry-delete-key nil
)
170 (make-variable-buffer-local 'c-hungry-delete-key
)
172 ;; The electric flag (toggled by `c-toggle-electric-state').
173 ;; If t, electric actions (like automatic reindentation, and (if
174 ;; c-auto-newline is also set) auto newlining) will happen when an electric
175 ;; key like `{' is pressed (or an electric keyword like `else').
176 (defvar c-electric-flag t
)
177 (make-variable-buffer-local 'c-electric-flag
)
179 ;; Internal state of auto newline feature.
180 (defvar c-auto-newline nil
)
181 (make-variable-buffer-local 'c-auto-newline
)
183 ;; Included in the mode line to indicate the active submodes.
184 ;; (defvar c-submode-indicators nil)
185 ;; (make-variable-buffer-local 'c-submode-indicators)
187 (defun c-calculate-state (arg prevstate
)
188 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
189 ;; arg is nil or zero, toggle the state. If arg is negative, turn
190 ;; the state off, and if arg is positive, turn the state on
192 (zerop (setq arg
(prefix-numeric-value arg
))))
196 ;; Dynamically bound cache for `c-in-literal'.
197 (defvar c-in-literal-cache t
)
200 ;; Basic handling of preprocessor directives.
202 ;; This is a dynamically bound cache used together with
203 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
204 ;; works as long as point doesn't cross a macro boundary.
205 (defvar c-macro-start
'unknown
)
207 (defsubst c-query-and-set-macro-start
()
208 (if (symbolp c-macro-start
)
209 (setq c-macro-start
(save-excursion
210 (c-save-buffer-state ()
211 (and (c-beginning-of-macro)
215 (defsubst c-query-macro-start
()
216 (if (symbolp c-macro-start
)
218 (c-save-buffer-state ()
219 (and (c-beginning-of-macro)
223 (defun c-beginning-of-macro (&optional lim
)
224 "Go to the beginning of a preprocessor directive.
225 Leave point at the beginning of the directive and return t if in one,
226 otherwise return nil and leave point unchanged.
228 Note that this function might do hidden buffer changes. See the
229 comment at the start of cc-engine.el for more info."
230 (when c-opt-cpp-prefix
231 (let ((here (point)))
233 (if lim
(narrow-to-region lim
(point-max)))
235 (while (eq (char-before (1- (point))) ?
\\)
237 (back-to-indentation)
238 (if (and (<= (point) here
)
239 (looking-at c-opt-cpp-start
))
244 (defun c-end-of-macro ()
245 "Go to the end of a preprocessor directive.
246 More accurately, move the point to the end of the closest following
247 line that doesn't end with a line continuation backslash - no check is
248 done that the point is inside a cpp directive to begin with.
250 Note that this function might do hidden buffer changes. See the
251 comment at the start of cc-engine.el for more info."
254 (when (and (eq (char-before) ?
\\)
259 (defun c-forward-over-cpp-define-id ()
260 ;; Assuming point is at the "#" that introduces a preprocessor
261 ;; directive, it's moved forward to the end of the identifier which is
262 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
263 ;; is returned in this case, in all other cases nil is returned and
264 ;; point isn't moved.
266 ;; This function might do hidden buffer changes.
267 (when (and c-opt-cpp-macro-define-id
268 (looking-at c-opt-cpp-macro-define-id
))
269 (goto-char (match-end 0))))
271 (defun c-forward-to-cpp-define-body ()
272 ;; Assuming point is at the "#" that introduces a preprocessor
273 ;; directive, it's moved forward to the start of the definition body
274 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
275 ;; specifies). Non-nil is returned in this case, in all other cases
276 ;; nil is returned and point isn't moved.
278 ;; This function might do hidden buffer changes.
279 (when (and c-opt-cpp-macro-define-start
280 (looking-at c-opt-cpp-macro-define-start
)
281 (not (= (match-end 0) (c-point 'eol
))))
282 (goto-char (match-end 0))))
285 ;;; Basic utility functions.
287 (defun c-syntactic-content (from to paren-level
)
288 ;; Return the given region as a string where all syntactic
289 ;; whitespace is removed or, where necessary, replaced with a single
290 ;; space. If PAREN-LEVEL is given then all parens in the region are
291 ;; collapsed to "()", "[]" etc.
293 ;; This function might do hidden buffer changes.
297 (narrow-to-region from to
)
299 (let* ((parts (list nil
)) (tail parts
) pos in-paren
)
301 (while (re-search-forward c-syntactic-ws-start to t
)
302 (goto-char (setq pos
(match-beginning 0)))
303 (c-forward-syntactic-ws)
309 (setq in-paren
(= (car (parse-partial-sexp from pos
1)) 1)
312 (if (and (> pos from
)
314 (looking-at "\\w\\|\\s_")
317 (looking-at "\\w\\|\\s_")))
319 (setcdr tail
(list (buffer-substring-no-properties from pos
)
321 (setq tail
(cddr tail
)))
322 (setcdr tail
(list (buffer-substring-no-properties from pos
)))
323 (setq tail
(cdr tail
)))
326 (when (= (car (parse-partial-sexp pos to -
1)) -
1)
327 (setcdr tail
(list (buffer-substring-no-properties
328 (1- (point)) (point))))
329 (setq tail
(cdr tail
))))
331 (setq from
(point))))
333 (setcdr tail
(list (buffer-substring-no-properties from to
)))
334 (apply 'concat
(cdr parts
))))))
336 (defun c-shift-line-indentation (shift-amt)
337 ;; Shift the indentation of the current line with the specified
338 ;; amount (positive inwards). The buffer is modified only if
339 ;; SHIFT-AMT isn't equal to zero.
340 (let ((pos (- (point-max) (point)))
341 (c-macro-start c-macro-start
)
343 (if (zerop shift-amt
)
345 ;; If we're on an empty line inside a macro, we take the point
346 ;; to be at the current indentation and shift it to the
347 ;; appropriate column. This way we don't treat the extra
348 ;; whitespace out to the line continuation as indentation.
349 (when (and (c-query-and-set-macro-start)
350 (looking-at "[ \t]*\\\\$")
352 (skip-chars-backward " \t")
356 (setq tmp-char-inserted t
))
358 (let ((col (current-indentation)))
359 (delete-region (c-point 'bol
) (c-point 'boi
))
361 (indent-to (+ col shift-amt
)))
362 (when tmp-char-inserted
364 ;; If initial point was within line's indentation and we're not on
365 ;; a line with a line continuation in a macro, position after the
366 ;; indentation. Else stay at same point in text.
367 (if (and (< (point) (c-point 'boi
))
368 (not tmp-char-inserted
))
369 (back-to-indentation)
370 (if (> (- (point-max) pos
) (point))
371 (goto-char (- (point-max) pos
))))))
373 (defsubst c-keyword-sym
(keyword)
374 ;; Return non-nil if the string KEYWORD is a known keyword. More
375 ;; precisely, the value is the symbol for the keyword in
376 ;; `c-keywords-obarray'.
377 (intern-soft keyword c-keywords-obarray
))
379 (defsubst c-keyword-member
(keyword-sym lang-constant
)
380 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
381 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
382 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
383 ;; nil then the result is nil.
384 (get keyword-sym lang-constant
))
386 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
387 (defconst c-string-syntax
(if (memq 'gen-string-delim c-emacs-features
)
391 ;; Regexp matching string limit syntax.
392 (defconst c-string-limit-regexp
(if (memq 'gen-string-delim c-emacs-features
)
396 ;; Regexp matching WS followed by string limit syntax.
397 (defconst c-ws
*-string-limit-regexp
398 (concat "[ \t]*\\(" c-string-limit-regexp
"\\)"))
400 ;; Holds formatted error strings for the few cases where parse errors
402 (defvar c-parsing-error nil
)
403 (make-variable-buffer-local 'c-parsing-error
)
405 (defun c-echo-parsing-error (&optional quiet
)
406 (when (and c-report-syntactic-errors c-parsing-error
(not quiet
))
407 (c-benign-error "%s" c-parsing-error
))
410 ;; Faces given to comments and string literals. This is used in some
411 ;; situations to speed up recognition; it isn't mandatory that font
412 ;; locking is in use. This variable is extended with the face in
413 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
414 (defvar c-literal-faces
415 (append '(font-lock-comment-face font-lock-string-face
)
416 (when (facep 'font-lock-comment-delimiter-face
)
418 '(font-lock-comment-delimiter-face))))
420 (defsubst c-put-c-type-property
(pos value
)
421 ;; Put a c-type property with the given value at POS.
422 (c-put-char-property pos
'c-type value
))
424 (defun c-clear-c-type-property (from to value
)
425 ;; Remove all occurences of the c-type property that has the given
426 ;; value in the region between FROM and TO. VALUE is assumed to not
429 ;; Note: This assumes that c-type is put on single chars only; it's
430 ;; very inefficient if matching properties cover large regions.
434 (when (eq (get-text-property (point) 'c-type
) value
)
435 (c-clear-char-property (point) 'c-type
))
436 (goto-char (next-single-property-change (point) 'c-type nil to
))
440 ;; Some debug tools to visualize various special positions. This
441 ;; debug code isn't as portable as the rest of CC Mode.
443 (cc-bytecomp-defun overlays-in)
444 (cc-bytecomp-defun overlay-get)
445 (cc-bytecomp-defun overlay-start)
446 (cc-bytecomp-defun overlay-end)
447 (cc-bytecomp-defun delete-overlay)
448 (cc-bytecomp-defun overlay-put)
449 (cc-bytecomp-defun make-overlay)
451 (defun c-debug-add-face (beg end face
)
452 (c-save-buffer-state ((overlays (overlays-in beg end
)) overlay
)
454 (setq overlay
(car overlays
)
455 overlays
(cdr overlays
))
456 (when (eq (overlay-get overlay
'face
) face
)
457 (setq beg
(min beg
(overlay-start overlay
))
458 end
(max end
(overlay-end overlay
)))
459 (delete-overlay overlay
)))
460 (overlay-put (make-overlay beg end
) 'face face
)))
462 (defun c-debug-remove-face (beg end face
)
463 (c-save-buffer-state ((overlays (overlays-in beg end
)) overlay
464 (ol-beg beg
) (ol-end end
))
466 (setq overlay
(car overlays
)
467 overlays
(cdr overlays
))
468 (when (eq (overlay-get overlay
'face
) face
)
469 (setq ol-beg
(min ol-beg
(overlay-start overlay
))
470 ol-end
(max ol-end
(overlay-end overlay
)))
471 (delete-overlay overlay
)))
473 (overlay-put (make-overlay ol-beg beg
) 'face face
))
475 (overlay-put (make-overlay end ol-end
) 'face face
))))
478 ;; `c-beginning-of-statement-1' and accompanying stuff.
480 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
481 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
482 ;; better way should be implemented, but this will at least shut up
483 ;; the byte compiler.
484 (defvar c-maybe-labelp
)
486 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
488 ;; Macros used internally in c-beginning-of-statement-1 for the
489 ;; automaton actions.
490 (defmacro c-bos-push-state
()
491 '(setq stack
(cons (cons state saved-pos
)
493 (defmacro c-bos-pop-state
(&optional do-if-done
)
494 `(if (setq state
(car (car stack
))
495 saved-pos
(cdr (car stack
))
500 (defmacro c-bos-pop-state-and-retry
()
501 '(throw 'loop
(setq state
(car (car stack
))
502 saved-pos
(cdr (car stack
))
503 ;; Throw nil if stack is empty, else throw non-nil.
505 (defmacro c-bos-save-pos
()
506 '(setq saved-pos
(vector pos tok ptok pptok
)))
507 (defmacro c-bos-restore-pos
()
508 '(unless (eq (elt saved-pos
0) start
)
509 (setq pos
(elt saved-pos
0)
510 tok
(elt saved-pos
1)
511 ptok
(elt saved-pos
2)
512 pptok
(elt saved-pos
3))
515 (defmacro c-bos-save-error-info
(missing got
)
516 `(setq saved-pos
(vector pos
,missing
,got
)))
517 (defmacro c-bos-report-error
()
519 (setq c-parsing-error
520 (format "No matching `%s' found for `%s' on line %d"
523 (1+ (count-lines (point-min)
524 (c-point 'bol
(elt saved-pos
0))))))))
526 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
528 "Move to the start of the current statement or declaration, or to
529 the previous one if already at the beginning of one. Only
530 statements/declarations on the same level are considered, i.e. don't
531 move into or out of sexps (not even normal expression parentheses).
533 If point is already at the earliest statment within braces or parens,
534 this function doesn't move back into any whitespace preceding it; it
535 returns 'same in this case.
537 Stop at statement continuation tokens like \"else\", \"catch\",
538 \"finally\" and the \"while\" in \"do ... while\" if the start point
539 is within the continuation. If starting at such a token, move to the
540 corresponding statement start. If at the beginning of a statement,
541 move to the closest containing statement if there is any. This might
542 also stop at a continuation clause.
544 Labels are treated as part of the following statements if
545 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
546 statement start keyword.) Otherwise, each label is treated as a
549 Macros are ignored \(i.e. skipped over) unless point is within one, in
550 which case the content of the macro is treated as normal code. Aside
551 from any normal statement starts found in it, stop at the first token
552 of the content in the macro, i.e. the expression of an \"#if\" or the
553 start of the definition in a \"#define\". Also stop at start of
554 macros before leaving them.
557 'label if stopped at a label or \"case...:\" or \"default:\";
558 'same if stopped at the beginning of the current statement;
559 'up if stepped to a containing statement;
560 'previous if stepped to a preceding statement;
561 'beginning if stepped from a statement continuation clause to
563 'macro if stepped to a macro start.
564 Note that 'same and not 'label is returned if stopped at the same
565 label without crossing the colon character.
567 LIM may be given to limit the search. If the search hits the limit,
568 point will be left at the closest following token, or at the start
569 position if that is less ('same is returned in this case).
571 NOERROR turns off error logging to `c-parsing-error'.
573 Normally only ';' and virtual semicolons are considered to delimit
574 statements, but if COMMA-DELIM is non-nil then ',' is treated
577 Note that this function might do hidden buffer changes. See the
578 comment at the start of cc-engine.el for more info."
580 ;; The bulk of this function is a pushdown automaton that looks at statement
581 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
582 ;; purpose is to keep track of nested statements, ensuring that such
583 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
584 ;; does with nested braces/brackets/parentheses).
586 ;; Note: The position of a boundary is the following token.
588 ;; Beginning with the current token (the one following point), move back one
589 ;; sexp at a time (where a sexp is, more or less, either a token or the
590 ;; entire contents of a brace/bracket/paren pair). Each time a statement
591 ;; boundary is crossed or a "while"-like token is found, update the state of
592 ;; the PDA. Stop at the beginning of a statement when the stack (holding
593 ;; nested statement info) is empty and the position has been moved.
595 ;; The following variables constitute the PDA:
597 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
598 ;; scanned back over, 'boundary if we've just gone back over a
599 ;; statement boundary, or nil otherwise.
600 ;; state: takes one of the values (nil else else-boundary while
601 ;; while-boundary catch catch-boundary).
602 ;; nil means "no "while"-like token yet scanned".
603 ;; 'else, for example, means "just gone back over an else".
604 ;; 'else-boundary means "just gone back over a statement boundary
605 ;; immediately after having gone back over an else".
606 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
607 ;; of error reporting information.
608 ;; stack: The stack onto which the PDA pushes its state. Each entry
609 ;; consists of a saved value of state and saved-pos. An entry is
610 ;; pushed when we move back over a "continuation" token (e.g. else)
611 ;; and popped when we encounter the corresponding opening token
615 ;; The following diagram briefly outlines the PDA.
618 ;; "else": Push state, goto state `else'.
619 ;; "while": Push state, goto state `while'.
620 ;; "catch" or "finally": Push state, goto state `catch'.
621 ;; boundary: Pop state.
622 ;; other: Do nothing special.
625 ;; boundary: Goto state `else-boundary'.
626 ;; other: Error, pop state, retry token.
628 ;; State `else-boundary':
630 ;; boundary: Error, pop state.
631 ;; other: See common state.
634 ;; boundary: Save position, goto state `while-boundary'.
635 ;; other: Pop state, retry token.
637 ;; State `while-boundary':
639 ;; boundary: Restore position if it's not at start, pop state. [*see below]
640 ;; other: See common state.
643 ;; boundary: Goto state `catch-boundary'.
644 ;; other: Error, pop state, retry token.
646 ;; State `catch-boundary':
648 ;; "catch": Goto state `catch'.
649 ;; boundary: Error, pop state.
650 ;; other: See common state.
652 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
653 ;; searching for a "do" which would have opened a do-while. If we didn't
654 ;; find it, we discard the analysis done since the "while", go back to this
655 ;; token in the buffer and restart the scanning there, this time WITHOUT
656 ;; pushing the 'while state onto the stack.
658 ;; In addition to the above there is some special handling of labels
661 (let ((case-fold-search nil
)
664 (delims (if comma-delim
'(?\
; ?,) '(?\;)))
665 (c-stmt-delim-chars (if comma-delim
666 c-stmt-delim-chars-with-comma
668 c-in-literal-cache c-maybe-labelp after-case
:-pos saved
671 ;; Position of last stmt boundary character (e.g. ;).
673 ;; The position of the last sexp or bound that follows the
674 ;; first found colon, i.e. the start of the nonlabel part of
675 ;; the statement. It's `start' if a colon is found just after
678 ;; Like `after-labels-pos', but the first such position inside
679 ;; a label, i.e. the start of the last label before the start
680 ;; of the nonlabel part of the statement.
682 ;; The last position where a label is possible provided the
683 ;; statement started there. It's nil as long as no invalid
684 ;; label content has been found (according to
685 ;; `c-nonlabel-token-key'. It's `start' if no valid label
686 ;; content was found in the label. Note that we might still
687 ;; regard it a label if it starts with `c-label-kwds'.
689 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
692 ;; Current state in the automaton. See above.
694 ;; Current saved positions. See above.
696 ;; Stack of conses (state . saved-pos).
698 ;; Regexp which matches "for", "if", etc.
699 (cond-key (or c-opt-block-stmt-key
700 "\\<\\>")) ; Matches nothing.
703 ;; Positions of the last three sexps or bounds we've stopped at.
707 (if lim
(narrow-to-region lim
(point-max)))
710 (and (c-beginning-of-macro)
712 (setq macro-start
(point)))
714 ;; Try to skip back over unary operator characters, to register
718 (c-backward-syntactic-ws)
719 ;; Protect post-++/-- operators just before a virtual semicolon.
720 (and (not (c-at-vsemi-p))
721 (/= (skip-chars-backward "-+!*&~@`#") 0))))
723 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
724 ;; done. Later on we ignore the boundaries for statements that don't
725 ;; contain any sexp. The only thing that is affected is that the error
726 ;; checking is a little less strict, and we really don't bother.
727 (if (and (memq (char-before) delims
)
728 (progn (forward-char -
1)
730 (c-backward-syntactic-ws)
731 (or (memq (char-before) delims
)
732 (memq (char-before) '(?
: nil
))
733 (eq (char-syntax (char-before)) ?\
()
738 ;; Begin at start and not pos to detect macros if we stand
739 ;; directly after the #.
741 (if (looking-at "\\<\\|\\W")
742 ;; Record this as the first token if not starting inside it.
745 ;; The following while loop goes back one sexp (balanced parens,
746 ;; etc. with contents, or symbol or suchlike) each iteration. This
747 ;; movement is accomplished with a call to scan-sexps approx 130 lines
750 (catch 'loop
;; Throw nil to break, non-nil to continue.
753 (and macro-start
; Always NIL for AWK.
754 (progn (skip-chars-backward " \t")
755 (eq (char-before) ?
#))
756 (progn (setq saved
(1- (point)))
758 (not (eq (char-before (1- (point))) ?
\\)))
759 (looking-at c-opt-cpp-start
)
760 (progn (skip-chars-forward " \t")
761 (eq (point) saved
))))
763 (if (and (c-forward-to-cpp-define-body)
764 (progn (c-forward-syntactic-ws start
)
766 ;; Stop at the first token in the content of the macro.
768 ignore-labels t
) ; Avoid the label check on exit.
774 ;; Do a round through the automaton if we've just passed a
775 ;; statement boundary or passed a "while"-like token.
777 (and (looking-at cond-key
)
778 (setq sym
(intern (match-string 1)))))
780 (when (and (< pos start
) (null stack
))
783 ;; The PDA state handling.
785 ;; Refer to the description of the PDA in the opening
786 ;; comments. In the following OR form, the first leaf
787 ;; attempts to handles one of the specific actions detailed
788 ;; (e.g., finding token "if" whilst in state `else-boundary').
789 ;; We drop through to the second leaf (which handles common
790 ;; state) if no specific handler is found in the first cond.
791 ;; If a parsing error is detected (e.g. an "else" with no
792 ;; preceding "if"), we throw to the enclosing catch.
794 ;; Note that the (eq state 'else) means
795 ;; "we've just passed an else", NOT "we're looking for an
799 (if (eq sym
'boundary
)
800 (setq state
'else-boundary
)
802 (c-bos-pop-state-and-retry)))
804 ((eq state
'else-boundary
)
806 (c-bos-pop-state (setq ret
'beginning
)))
812 (if (and (eq sym
'boundary
)
813 ;; Since this can cause backtracking we do a
814 ;; little more careful analysis to avoid it:
815 ;; If there's a label in front of the while
816 ;; it can't be part of a do-while.
817 (not after-labels-pos
))
818 (progn (c-bos-save-pos)
819 (setq state
'while-boundary
))
820 (c-bos-pop-state-and-retry))) ; Can't be a do-while
822 ((eq state
'while-boundary
)
824 (c-bos-pop-state (setq ret
'beginning
)))
825 ((eq sym
'boundary
) ; isn't a do-while
826 (c-bos-restore-pos) ; the position of the while
827 (c-bos-pop-state)))) ; no longer searching for do.
830 (if (eq sym
'boundary
)
831 (setq state
'catch-boundary
)
833 (c-bos-pop-state-and-retry)))
835 ((eq state
'catch-boundary
)
838 (c-bos-pop-state (setq ret
'beginning
)))
843 (c-bos-pop-state)))))
845 ;; This is state common. We get here when the previous
846 ;; cond statement found no particular state handler.
847 (cond ((eq sym
'boundary
)
848 ;; If we have a boundary at the start
849 ;; position we push a frame to go to the
850 ;; previous statement.
856 (c-bos-save-error-info 'if
'else
)
859 ;; Is this a real while, or a do-while?
860 ;; The next `when' triggers unless we are SURE that
861 ;; the `while' is not the tailend of a `do-while'.
862 (when (or (not pptok
)
863 (memq (char-after pptok
) delims
)
864 ;; The following kludge is to prevent
865 ;; infinite recursion when called from
866 ;; c-awk-after-if-for-while-condition-p,
868 (and (eq (point) start
)
869 (c-vsemi-status-unknown-p))
870 (c-at-vsemi-p pptok
))
871 ;; Since this can cause backtracking we do a
872 ;; little more careful analysis to avoid it: If
873 ;; the while isn't followed by a (possibly
874 ;; virtual) semicolon it can't be a do-while.
876 (setq state
'while
)))
877 ((memq sym
'(catch finally
))
879 (c-bos-save-error-info 'try sym
)
880 (setq state
'catch
))))
883 ;; We're either past a statement boundary or at the
884 ;; start of a statement, so throw away any label data
885 ;; for the previous one.
886 (setq after-labels-pos nil
888 c-maybe-labelp nil
))))
890 ;; Step to the previous sexp, but not if we crossed a
891 ;; boundary, since that doesn't consume an sexp.
892 (if (eq sym
'boundary
)
895 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
896 ;; BACKWARDS THROUGH THE SOURCE.
898 ;; This is typically fast with the caching done by
899 ;; c-(backward|forward)-sws.
900 (c-backward-syntactic-ws)
902 (let ((before-sws-pos (point))
903 ;; Set as long as we have to continue jumping by sexps.
904 ;; It's the position to use as end in the next round.
905 sexp-loop-continue-pos
906 ;; The end position of the area to search for statement
907 ;; barriers in this round.
908 (sexp-loop-end-pos pos
))
910 ;; The following while goes back one sexp per iteration.
913 (unless (c-safe (c-backward-sexp) t
)
914 ;; Give up if we hit an unbalanced block. Since the
915 ;; stack won't be empty the code below will report a
919 ;; Check if the sexp movement crossed a statement or
920 ;; declaration boundary. But first modify the point
921 ;; so that `c-crosses-statement-barrier-p' only looks
922 ;; at the non-sexp chars following the sexp.
930 (when (c-beginning-of-macro)
931 ;; Set continuation position in case
932 ;; `c-crosses-statement-barrier-p'
933 ;; doesn't detect anything below.
934 (setq sexp-loop-continue-pos
(point)))))
935 ;; If the sexp movement took us into a
936 ;; macro then there were only some non-sexp
937 ;; chars after it. Skip out of the macro
938 ;; to analyze them but not the non-sexp
939 ;; chars that might be inside the macro.
941 (c-crosses-statement-barrier-p
942 (point) sexp-loop-end-pos
))
946 (not (c-looking-at-inexpr-block lim nil t
)))
947 ;; Passed a block sexp. That's a boundary
951 ((looking-at "\\s\(")
952 ;; Passed some other paren. Only analyze
953 ;; the non-sexp chars after it.
954 (goto-char (1+ (c-down-list-backward
956 ;; We're at a valid token start position
957 ;; (outside the `save-excursion') if
958 ;; `c-crosses-statement-barrier-p' failed.
959 (c-crosses-statement-barrier-p
960 (point) sexp-loop-end-pos
))
963 ;; Passed a symbol sexp or line
964 ;; continuation. It doesn't matter that
965 ;; it's included in the analyzed region.
966 (if (c-crosses-statement-barrier-p
967 (point) sexp-loop-end-pos
)
969 ;; If it was a line continuation then we
970 ;; have to continue looping.
971 (if (looking-at "\\\\$")
972 (setq sexp-loop-continue-pos
(point)))
979 ;; Like a C "continue". Analyze the next sexp.
982 sexp-loop-continue-pos
) ; End of "go back a sexp" loop condition.
983 (goto-char sexp-loop-continue-pos
)
984 (setq sexp-loop-end-pos sexp-loop-continue-pos
985 sexp-loop-continue-pos nil
))))
988 (when (and c-opt-method-key
989 (setq saved
(c-in-method-def-p)))
991 ignore-labels t
) ; Avoid the label check on exit.
995 (unless (eq ignore-labels t
)
996 (when (numberp c-maybe-labelp
)
997 ;; `c-crosses-statement-barrier-p' has found a colon, so we
998 ;; might be in a label now. Have we got a real label
999 ;; (including a case label) or something like C++'s "public:"?
1000 ;; A case label might use an expression rather than a token.
1001 (setq after-case
:-pos
(or tok start
))
1002 (if (looking-at c-nonlabel-token-key
) ; e.g. "while" or "'a'"
1003 (setq c-maybe-labelp nil
)
1004 (if after-labels-pos
; Have we already encountered a label?
1005 (if (not last-label-pos
)
1006 (setq last-label-pos
(or tok start
)))
1007 (setq after-labels-pos
(or tok start
)))
1008 (setq c-maybe-labelp t
1009 label-good-pos nil
))) ; bogus "label"
1011 (when (and (not label-good-pos
) ; i.e. no invalid "label"'s yet
1013 (looking-at c-nonlabel-token-key
)) ; e.g. "while :"
1014 ;; We're in a potential label and it's the first
1015 ;; time we've found something that isn't allowed in
1017 (setq label-good-pos
(or tok start
))))
1019 ;; We've moved back by a sexp, so update the token positions.
1024 pos tok
))) ; Not nil (for the while loop).
1026 ;; If the stack isn't empty there might be errors to report.
1028 (if (and (vectorp saved-pos
) (eq (length saved-pos
) 3))
1029 (c-bos-report-error))
1030 (setq saved-pos
(cdr (car stack
))
1033 (when (and (eq ret
'same
)
1034 (not (memq sym
'(boundary ignore nil
))))
1035 ;; Need to investigate closer whether we've crossed
1036 ;; between a substatement and its containing statement.
1037 (if (setq saved
(if (looking-at c-block-stmt-1-key
)
1040 (cond ((> start saved
) (setq pos saved
))
1041 ((= start saved
) (setq ret
'up
)))))
1043 (when (and (not ignore-labels
)
1044 (eq c-maybe-labelp t
)
1045 (not (eq ret
'beginning
))
1047 (or (not label-good-pos
)
1048 (<= label-good-pos pos
)
1050 (goto-char (if (and last-label-pos
1051 (< last-label-pos start
))
1054 (looking-at c-label-kwds-regexp
))))
1055 ;; We're in a label. Maybe we should step to the statement
1057 (if (< after-labels-pos start
)
1058 (setq pos after-labels-pos
)
1060 (if (and last-label-pos
(< last-label-pos start
))
1061 ;; Might have jumped over several labels. Go to the last one.
1062 (setq pos last-label-pos
)))))
1064 ;; Have we got "case <expression>:"?
1066 (when (and after-case
:-pos
1067 (not (eq ret
'beginning
))
1068 (looking-at c-case-kwds-regexp
))
1069 (if (< after-case
:-pos start
)
1070 (setq pos after-case
:-pos
)
1073 ;; Skip over the unary operators that can start the statement.
1075 (c-backward-syntactic-ws)
1076 ;; protect AWK post-inc/decrement operators, etc.
1077 (and (not (c-at-vsemi-p (point)))
1078 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1083 (defun c-crosses-statement-barrier-p (from to
)
1084 "Return non-nil if buffer positions FROM to TO cross one or more
1085 statement or declaration boundaries. The returned value is actually
1086 the position of the earliest boundary char. FROM must not be within
1087 a string or comment.
1089 The variable `c-maybe-labelp' is set to the position of the first `:' that
1090 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1091 single `?' is found, then `c-maybe-labelp' is cleared.
1093 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1094 regarded as having a \"virtual semicolon\" immediately after the last token on
1095 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1097 Note that this function might do hidden buffer changes. See the
1098 comment at the start of cc-engine.el for more info."
1099 (let ((skip-chars c-stmt-delim-chars
)
1104 (while (progn (skip-chars-forward skip-chars to
)
1107 ((setq lit-range
(c-literal-limits from
)) ; Have we landed in a string/comment?
1108 (goto-char (cdr lit-range
)))
1109 ((eq (char-after) ?
:)
1111 (if (and (eq (char-after) ?
:)
1113 ;; Ignore scope operators.
1115 (setq c-maybe-labelp
(1- (point)))))
1116 ((eq (char-after) ??
)
1117 ;; A question mark. Can't be a label, so stop
1118 ;; looking for more : and ?.
1119 (setq c-maybe-labelp nil
1120 skip-chars
(substring c-stmt-delim-chars
0 -
2)))
1121 ((memq (char-after) '(?
# ?
\n ?
\r)) ; A virtual semicolon?
1122 (if (and (eq (char-before) ?
\\) (memq (char-after) '(?
\n ?
\r)))
1124 (skip-chars-backward " \t" from
)
1126 (throw 'done
(point))
1128 (t (throw 'done
(point)))))
1129 ;; In trailing space after an as yet undetected virtual semicolon?
1130 (c-backward-syntactic-ws from
)
1131 (if (and (< (point) to
)
1136 (defun c-at-statement-start-p ()
1137 "Return non-nil if the point is at the first token in a statement
1138 or somewhere in the syntactic whitespace before it.
1140 A \"statement\" here is not restricted to those inside code blocks.
1141 Any kind of declaration-like construct that occur outside function
1142 bodies is also considered a \"statement\".
1144 Note that this function might do hidden buffer changes. See the
1145 comment at the start of cc-engine.el for more info."
1150 (c-syntactic-skip-backward (substring c-stmt-delim-chars
1) nil t
)
1152 (eq (char-before) ?
})
1153 (and (eq (char-before) ?
{)
1154 (not (and c-special-brace-lists
1155 (progn (backward-char)
1156 (c-looking-at-special-brace-list)))))
1157 (c-crosses-statement-barrier-p (point) end
)))))
1159 (defun c-at-expression-start-p ()
1160 "Return non-nil if the point is at the first token in an expression or
1161 statement, or somewhere in the syntactic whitespace before it.
1163 An \"expression\" here is a bit different from the normal language
1164 grammar sense: It's any sequence of expression tokens except commas,
1165 unless they are enclosed inside parentheses of some kind. Also, an
1166 expression never continues past an enclosing parenthesis, but it might
1167 contain parenthesis pairs of any sort except braces.
1169 Since expressions never cross statement boundaries, this function also
1170 recognizes statement beginnings, just like `c-at-statement-start-p'.
1172 Note that this function might do hidden buffer changes. See the
1173 comment at the start of cc-engine.el for more info."
1177 (c-stmt-delim-chars c-stmt-delim-chars-with-comma
)
1179 (c-syntactic-skip-backward (substring c-stmt-delim-chars
1) nil t
)
1181 (memq (char-before) '(?
{ ?
}))
1182 (save-excursion (backward-char)
1183 (looking-at "\\s("))
1184 (c-crosses-statement-barrier-p (point) end
)))))
1187 ;; A set of functions that covers various idiosyncrasies in
1188 ;; implementations of `forward-comment'.
1190 ;; Note: Some emacsen considers incorrectly that any line comment
1191 ;; ending with a backslash continues to the next line. I can't think
1192 ;; of any way to work around that in a reliable way without changing
1193 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1194 ;; changing the syntax for backslash doesn't work since we must treat
1195 ;; escapes in string literals correctly.)
1197 (defun c-forward-single-comment ()
1198 "Move forward past whitespace and the closest following comment, if any.
1199 Return t if a comment was found, nil otherwise. In either case, the
1200 point is moved past the following whitespace. Line continuations,
1201 i.e. a backslashes followed by line breaks, are treated as whitespace.
1202 The line breaks that end line comments are considered to be the
1203 comment enders, so the point will be put on the beginning of the next
1204 line if it moved past a line comment.
1206 This function does not do any hidden buffer changes."
1208 (let ((start (point)))
1209 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1210 (goto-char (match-end 0)))
1212 (when (forward-comment 1)
1214 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1218 ;; Emacs includes the ending newline in a b-style (c++)
1219 ;; comment, but XEmacs doesn't. We depend on the Emacs
1220 ;; behavior (which also is symmetric).
1221 (if (and (eolp) (elt (parse-partial-sexp start
(point)) 7))
1222 (condition-case nil
(forward-char 1)))
1226 (defsubst c-forward-comments
()
1227 "Move forward past all following whitespace and comments.
1228 Line continuations, i.e. a backslashes followed by line breaks, are
1229 treated as whitespace.
1231 Note that this function might do hidden buffer changes. See the
1232 comment at the start of cc-engine.el for more info."
1235 ;; If forward-comment in at least XEmacs 21 is given a large
1236 ;; positive value, it'll loop all the way through if it hits
1238 (and (forward-comment 5)
1239 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1243 (when (looking-at "\\\\[\n\r]")
1247 (defun c-backward-single-comment ()
1248 "Move backward past whitespace and the closest preceding comment, if any.
1249 Return t if a comment was found, nil otherwise. In either case, the
1250 point is moved past the preceding whitespace. Line continuations,
1251 i.e. a backslashes followed by line breaks, are treated as whitespace.
1252 The line breaks that end line comments are considered to be the
1253 comment enders, so the point cannot be at the end of the same line to
1254 move over a line comment.
1256 This function does not do any hidden buffer changes."
1258 (let ((start (point)))
1259 ;; When we got newline terminated comments, forward-comment in all
1260 ;; supported emacsen so far will stop at eol of each line not
1261 ;; ending with a comment when moving backwards. This corrects for
1262 ;; that, and at the same time handles line continuations.
1264 (skip-chars-backward " \t\n\r\f\v")
1265 (and (looking-at "[\n\r]")
1266 (eq (char-before) ?
\\)))
1270 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1271 ;; backwards at bob.
1274 ;; Leave point after the closest following newline if we've
1275 ;; backed up over any above, since forward-comment won't move
1276 ;; backward over a line comment if point is at the end of the
1278 (re-search-forward "\\=\\s *[\n\r]" start t
)
1280 (if (if (forward-comment -
1)
1282 ;; If forward-comment above succeeded and we're at eol
1283 ;; then the newline we moved over above didn't end a
1284 ;; line comment, so we give it another go.
1285 (forward-comment -
1)
1288 ;; Emacs <= 20 and XEmacs move back over the closer of a
1289 ;; block comment that lacks an opener.
1290 (if (looking-at "\\*/")
1291 (progn (forward-char 2) nil
)
1294 (defsubst c-backward-comments
()
1295 "Move backward past all preceding whitespace and comments.
1296 Line continuations, i.e. a backslashes followed by line breaks, are
1297 treated as whitespace. The line breaks that end line comments are
1298 considered to be the comment enders, so the point cannot be at the end
1299 of the same line to move over a line comment. Unlike
1300 c-backward-syntactic-ws, this function doesn't move back over
1301 preprocessor directives.
1303 Note that this function might do hidden buffer changes. See the
1304 comment at the start of cc-engine.el for more info."
1306 (let ((start (point)))
1308 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1309 ;; return t when moving backwards at bob.
1312 (if (forward-comment -
1)
1313 (if (looking-at "\\*/")
1314 ;; Emacs <= 20 and XEmacs move back over the
1315 ;; closer of a block comment that lacks an opener.
1316 (progn (forward-char 2) nil
)
1319 ;; XEmacs treats line continuations as whitespace but
1320 ;; only in the backward direction, which seems a bit
1321 ;; odd. Anyway, this is necessary for Emacs.
1322 (when (and (looking-at "[\n\r]")
1323 (eq (char-before) ?
\\)
1329 ;; Tools for skipping over syntactic whitespace.
1331 ;; The following functions use text properties to cache searches over
1332 ;; large regions of syntactic whitespace. It works as follows:
1334 ;; o If a syntactic whitespace region contains anything but simple
1335 ;; whitespace (i.e. space, tab and line breaks), the text property
1336 ;; `c-in-sws' is put over it. At places where we have stopped
1337 ;; within that region there's also a `c-is-sws' text property.
1338 ;; That since there typically are nested whitespace inside that
1339 ;; must be handled separately, e.g. whitespace inside a comment or
1340 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1341 ;; to jump to another point with that property within the same
1342 ;; `c-in-sws' region. It can be likened to a ladder where
1343 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1345 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1346 ;; a "rung position" and also maybe on the first following char.
1347 ;; As many characters as can be conveniently found in this range
1348 ;; are marked, but no assumption can be made that the whole range
1349 ;; is marked (it could be clobbered by later changes, for
1352 ;; Note that some part of the beginning of a sequence of simple
1353 ;; whitespace might be part of the end of a preceding line comment
1354 ;; or cpp directive and must not be considered part of the "rung".
1355 ;; Such whitespace is some amount of horizontal whitespace followed
1356 ;; by a newline. In the case of cpp directives it could also be
1357 ;; two newlines with horizontal whitespace between them.
1359 ;; The reason to include the first following char is to cope with
1360 ;; "rung positions" that doesn't have any ordinary whitespace. If
1361 ;; `c-is-sws' is put on a token character it does not have
1362 ;; `c-in-sws' set simultaneously. That's the only case when that
1363 ;; can occur, and the reason for not extending the `c-in-sws'
1364 ;; region to cover it is that the `c-in-sws' region could then be
1365 ;; accidentally merged with a following one if the token is only
1366 ;; one character long.
1368 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1369 ;; removed in the changed region. If the change was inside
1370 ;; syntactic whitespace that means that the "ladder" is broken, but
1371 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1372 ;; parts on either side and use an ordinary search only to "repair"
1375 ;; Special care needs to be taken if a region is removed: If there
1376 ;; are `c-in-sws' on both sides of it which do not connect inside
1377 ;; the region then they can't be joined. If e.g. a marked macro is
1378 ;; broken, syntactic whitespace inside the new text might be
1379 ;; marked. If those marks would become connected with the old
1380 ;; `c-in-sws' range around the macro then we could get a ladder
1381 ;; with one end outside the macro and the other at some whitespace
1384 ;; The main motivation for this system is to increase the speed in
1385 ;; skipping over the large whitespace regions that can occur at the
1386 ;; top level in e.g. header files that contain a lot of comments and
1387 ;; cpp directives. For small comments inside code it's probably
1388 ;; slower than using `forward-comment' straightforwardly, but speed is
1389 ;; not a significant factor there anyway.
1391 ; (defface c-debug-is-sws-face
1392 ; '((t (:background "GreenYellow")))
1393 ; "Debug face to mark the `c-is-sws' property.")
1394 ; (defface c-debug-in-sws-face
1395 ; '((t (:underline t)))
1396 ; "Debug face to mark the `c-in-sws' property.")
1398 ; (defun c-debug-put-sws-faces ()
1399 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1400 ; ;; properties in the buffer.
1403 ; (c-save-buffer-state (in-face)
1404 ; (goto-char (point-min))
1405 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1408 ; (goto-char (next-single-property-change
1409 ; (point) 'c-is-sws nil (point-max)))
1412 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1413 ; (setq in-face nil))
1414 ; (setq in-face (point)))
1416 ; (goto-char (point-min))
1417 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1420 ; (goto-char (next-single-property-change
1421 ; (point) 'c-in-sws nil (point-max)))
1424 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1425 ; (setq in-face nil))
1426 ; (setq in-face (point)))
1429 (defmacro c-debug-sws-msg
(&rest args
)
1433 (defmacro c-put-is-sws
(beg end
)
1434 ;; This macro does a hidden buffer change.
1435 `(let ((beg ,beg
) (end ,end
))
1436 (put-text-property beg end
'c-is-sws t
)
1437 ,@(when (facep 'c-debug-is-sws-face
)
1438 `((c-debug-add-face beg end
'c-debug-is-sws-face
)))))
1440 (defmacro c-put-in-sws
(beg end
)
1441 ;; This macro does a hidden buffer change.
1442 `(let ((beg ,beg
) (end ,end
))
1443 (put-text-property beg end
'c-in-sws t
)
1444 ,@(when (facep 'c-debug-is-sws-face
)
1445 `((c-debug-add-face beg end
'c-debug-in-sws-face
)))))
1447 (defmacro c-remove-is-sws
(beg end
)
1448 ;; This macro does a hidden buffer change.
1449 `(let ((beg ,beg
) (end ,end
))
1450 (remove-text-properties beg end
'(c-is-sws nil
))
1451 ,@(when (facep 'c-debug-is-sws-face
)
1452 `((c-debug-remove-face beg end
'c-debug-is-sws-face
)))))
1454 (defmacro c-remove-in-sws
(beg end
)
1455 ;; This macro does a hidden buffer change.
1456 `(let ((beg ,beg
) (end ,end
))
1457 (remove-text-properties beg end
'(c-in-sws nil
))
1458 ,@(when (facep 'c-debug-is-sws-face
)
1459 `((c-debug-remove-face beg end
'c-debug-in-sws-face
)))))
1461 (defmacro c-remove-is-and-in-sws
(beg end
)
1462 ;; This macro does a hidden buffer change.
1463 `(let ((beg ,beg
) (end ,end
))
1464 (remove-text-properties beg end
'(c-is-sws nil c-in-sws nil
))
1465 ,@(when (facep 'c-debug-is-sws-face
)
1466 `((c-debug-remove-face beg end
'c-debug-is-sws-face
)
1467 (c-debug-remove-face beg end
'c-debug-in-sws-face
)))))
1469 (defsubst c-invalidate-sws-region-after
(beg end
)
1470 ;; Called from `after-change-functions'. Note that if
1471 ;; `c-forward-sws' or `c-backward-sws' are used outside
1472 ;; `c-save-buffer-state' or similar then this will remove the cache
1473 ;; properties right after they're added.
1475 ;; This function does hidden buffer changes.
1478 ;; Adjust the end to remove the properties in any following simple
1479 ;; ws up to and including the next line break, if there is any
1480 ;; after the changed region. This is necessary e.g. when a rung
1481 ;; marked empty line is converted to a line comment by inserting
1482 ;; "//" before the line break. In that case the line break would
1483 ;; keep the rung mark which could make a later `c-backward-sws'
1484 ;; move into the line comment instead of over it.
1486 (skip-chars-forward " \t\f\v")
1487 (when (and (eolp) (not (eobp)))
1488 (setq end
(1+ (point)))))
1490 (when (and (= beg end
)
1491 (get-text-property beg
'c-in-sws
)
1493 (get-text-property (1- beg
) 'c-in-sws
))
1494 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1495 ;; safe to keep a range that was continuous before the change. E.g:
1501 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1502 ;; after "foo" is removed then "bar" will become part of the cpp
1503 ;; directive instead of a syntactically relevant token. In that
1504 ;; case there's no longer syntactic ws from "#" to "b".
1505 (setq beg
(1- beg
)))
1507 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end
)
1508 (c-remove-is-and-in-sws beg end
))
1510 (defun c-forward-sws ()
1511 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1513 ;; This function might do hidden buffer changes.
1515 (let (;; `rung-pos' is set to a position as early as possible in the
1516 ;; unmarked part of the simple ws region.
1517 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1518 rung-is-marked next-rung-is-marked simple-ws-end
1519 ;; `safe-start' is set when it's safe to cache the start position.
1520 ;; It's not set if we've initially skipped over comments and line
1521 ;; continuations since we might have gone out through the end of a
1522 ;; macro then. This provision makes `c-forward-sws' not populate the
1523 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1527 ;; Skip simple ws and do a quick check on the following character to see
1528 ;; if it's anything that can't start syntactic ws, so we can bail out
1529 ;; early in the majority of cases when there just are a few ws chars.
1530 (skip-chars-forward " \t\n\r\f\v")
1531 (when (looking-at c-syntactic-ws-start
)
1533 (setq rung-end-pos
(min (1+ (point)) (point-max)))
1534 (if (setq rung-is-marked
(text-property-any rung-pos rung-end-pos
1536 ;; Find the last rung position to avoid setting properties in all
1537 ;; the cases when the marked rung is complete.
1538 ;; (`next-single-property-change' is certain to move at least one
1540 (setq rung-pos
(1- (next-single-property-change
1541 rung-is-marked
'c-is-sws nil rung-end-pos
)))
1542 ;; Got no marked rung here. Since the simple ws might have started
1543 ;; inside a line comment or cpp directive we must set `rung-pos' as
1544 ;; high as possible.
1545 (setq rung-pos
(point)))
1550 (when (and rung-is-marked
1551 (get-text-property (point) 'c-in-sws
))
1553 ;; The following search is the main reason that `c-in-sws'
1554 ;; and `c-is-sws' aren't combined to one property.
1555 (goto-char (next-single-property-change
1556 (point) 'c-in-sws nil
(point-max)))
1557 (unless (get-text-property (point) 'c-is-sws
)
1558 ;; If the `c-in-sws' region extended past the last
1559 ;; `c-is-sws' char we have to go back a bit.
1560 (or (get-text-property (1- (point)) 'c-is-sws
)
1561 (goto-char (previous-single-property-change
1562 (point) 'c-is-sws
)))
1566 "c-forward-sws cached move %s -> %s (max %s)"
1567 rung-pos
(point) (point-max))
1569 (setq rung-pos
(point))
1570 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1573 ;; We'll loop here if there is simple ws after the last rung.
1574 ;; That means that there's been some change in it and it's
1575 ;; possible that we've stepped into another ladder, so extend
1576 ;; the previous one to join with it if there is one, and try to
1577 ;; use the cache again.
1579 "c-forward-sws extending rung with [%s..%s] (max %s)"
1580 (1+ rung-pos
) (1+ (point)) (point-max))
1581 (unless (get-text-property (point) 'c-is-sws
)
1582 ;; Remove any `c-in-sws' property from the last char of
1583 ;; the rung before we mark it with `c-is-sws', so that we
1584 ;; won't connect with the remains of a broken "ladder".
1585 (c-remove-in-sws (point) (1+ (point))))
1586 (c-put-is-sws (1+ rung-pos
)
1588 (c-put-in-sws rung-pos
1589 (setq rung-pos
(point)
1590 last-put-in-sws-pos rung-pos
)))
1592 (setq simple-ws-end
(point))
1593 (c-forward-comments)
1596 ((/= (point) simple-ws-end
)
1597 ;; Skipped over comments. Don't cache at eob in case the buffer
1602 (and c-opt-cpp-prefix
1603 (looking-at c-opt-cpp-start
)
1604 (progn (skip-chars-backward " \t")
1607 (progn (backward-char)
1608 (not (eq (char-before) ?
\\))))))
1609 ;; Skip a preprocessor directive.
1611 (while (and (eq (char-before) ?
\\)
1612 (= (forward-line 1) 0))
1616 ;; Don't cache at eob in case the buffer is narrowed.
1619 ;; We've searched over a piece of non-white syntactic ws. See if this
1621 (setq next-rung-pos
(point))
1622 (skip-chars-forward " \t\n\r\f\v")
1623 (setq rung-end-pos
(min (1+ (point)) (point-max)))
1626 ;; Cache if we haven't skipped comments only, and if we started
1627 ;; either from a marked rung or from a completely uncached
1631 (not (get-text-property simple-ws-end
'c-in-sws
))))
1633 ;; See if there's a marked rung in the encountered simple ws. If
1634 ;; so then we can cache, unless `safe-start' is nil. Even then
1635 ;; we need to do this to check if the cache can be used for the
1637 (and (setq next-rung-is-marked
1638 (text-property-any next-rung-pos rung-end-pos
1644 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1645 rung-pos
(1+ simple-ws-end
) next-rung-pos rung-end-pos
1648 ;; Remove the properties for any nested ws that might be cached.
1649 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1651 (c-remove-is-sws (1+ simple-ws-end
) next-rung-pos
)
1652 (unless (and rung-is-marked
(= rung-pos simple-ws-end
))
1653 (c-put-is-sws rung-pos
1655 (setq rung-is-marked t
))
1656 (c-put-in-sws rung-pos
1657 (setq rung-pos
(point)
1658 last-put-in-sws-pos rung-pos
))
1659 (unless (get-text-property (1- rung-end-pos
) 'c-is-sws
)
1660 ;; Remove any `c-in-sws' property from the last char of
1661 ;; the rung before we mark it with `c-is-sws', so that we
1662 ;; won't connect with the remains of a broken "ladder".
1663 (c-remove-in-sws (1- rung-end-pos
) rung-end-pos
))
1664 (c-put-is-sws next-rung-pos
1668 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1669 rung-pos
(1+ simple-ws-end
) next-rung-pos rung-end-pos
1672 ;; Set `rung-pos' for the next rung. It's the same thing here as
1673 ;; initially, except that the rung position is set as early as
1674 ;; possible since we can't be in the ending ws of a line comment or
1675 ;; cpp directive now.
1676 (if (setq rung-is-marked next-rung-is-marked
)
1677 (setq rung-pos
(1- (next-single-property-change
1678 rung-is-marked
'c-is-sws nil rung-end-pos
)))
1679 (setq rung-pos next-rung-pos
))
1680 (setq safe-start t
)))
1682 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1683 ;; another one after the point (which might occur when editing inside a
1684 ;; comment or macro).
1685 (when (eq last-put-in-sws-pos
(point))
1686 (cond ((< last-put-in-sws-pos
(point-max))
1688 "c-forward-sws clearing at %s for cache separation"
1689 last-put-in-sws-pos
)
1690 (c-remove-in-sws last-put-in-sws-pos
1691 (1+ last-put-in-sws-pos
)))
1693 ;; If at eob we have to clear the last character before the end
1694 ;; instead since the buffer might be narrowed and there might
1695 ;; be a `c-in-sws' after (point-max). In this case it's
1696 ;; necessary to clear both properties.
1698 "c-forward-sws clearing thoroughly at %s for cache separation"
1699 (1- last-put-in-sws-pos
))
1700 (c-remove-is-and-in-sws (1- last-put-in-sws-pos
)
1701 last-put-in-sws-pos
))))
1704 (defun c-backward-sws ()
1705 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1707 ;; This function might do hidden buffer changes.
1709 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1710 ;; part of the simple ws region.
1711 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1712 rung-is-marked simple-ws-beg cmt-skip-pos
)
1714 ;; Skip simple horizontal ws and do a quick check on the preceding
1715 ;; character to see if it's anying that can't end syntactic ws, so we can
1716 ;; bail out early in the majority of cases when there just are a few ws
1717 ;; chars. Newlines are complicated in the backward direction, so we can't
1719 (skip-chars-backward " \t\f")
1720 (when (and (not (bobp))
1723 (looking-at c-syntactic-ws-end
)))
1725 ;; Try to find a rung position in the simple ws preceding point, so that
1726 ;; we can get a cache hit even if the last bit of the simple ws has
1727 ;; changed recently.
1728 (setq simple-ws-beg
(point))
1729 (skip-chars-backward " \t\n\r\f\v")
1730 (if (setq rung-is-marked
(text-property-any
1731 (point) (min (1+ rung-pos
) (point-max))
1733 ;; `rung-pos' will be the earliest marked position, which means that
1734 ;; there might be later unmarked parts in the simple ws region.
1735 ;; It's not worth the effort to fix that; the last part of the
1736 ;; simple ws is also typically edited often, so it could be wasted.
1737 (goto-char (setq rung-pos rung-is-marked
))
1738 (goto-char simple-ws-beg
))
1743 (when (and rung-is-marked
1745 (get-text-property (1- (point)) 'c-in-sws
))
1747 ;; The following search is the main reason that `c-in-sws'
1748 ;; and `c-is-sws' aren't combined to one property.
1749 (goto-char (previous-single-property-change
1750 (point) 'c-in-sws nil
(point-min)))
1751 (unless (get-text-property (point) 'c-is-sws
)
1752 ;; If the `c-in-sws' region extended past the first
1753 ;; `c-is-sws' char we have to go forward a bit.
1754 (goto-char (next-single-property-change
1755 (point) 'c-is-sws
)))
1758 "c-backward-sws cached move %s <- %s (min %s)"
1759 (point) rung-pos
(point-min))
1761 (setq rung-pos
(point))
1762 (if (and (< (min (skip-chars-backward " \t\f\v")
1764 (setq simple-ws-beg
(point))
1765 (skip-chars-backward " \t\n\r\f\v")))
1767 (setq rung-is-marked
1768 (text-property-any (point) rung-pos
1771 (goto-char simple-ws-beg
)
1774 ;; We'll loop here if there is simple ws before the first rung.
1775 ;; That means that there's been some change in it and it's
1776 ;; possible that we've stepped into another ladder, so extend
1777 ;; the previous one to join with it if there is one, and try to
1778 ;; use the cache again.
1780 "c-backward-sws extending rung with [%s..%s] (min %s)"
1781 rung-is-marked rung-pos
(point-min))
1782 (unless (get-text-property (1- rung-pos
) 'c-is-sws
)
1783 ;; Remove any `c-in-sws' property from the last char of
1784 ;; the rung before we mark it with `c-is-sws', so that we
1785 ;; won't connect with the remains of a broken "ladder".
1786 (c-remove-in-sws (1- rung-pos
) rung-pos
))
1787 (c-put-is-sws rung-is-marked
1789 (c-put-in-sws rung-is-marked
1791 (setq rung-pos rung-is-marked
1792 last-put-in-sws-pos rung-pos
))
1794 (c-backward-comments)
1795 (setq cmt-skip-pos
(point))
1798 ((and c-opt-cpp-prefix
1799 (/= cmt-skip-pos simple-ws-beg
)
1800 (c-beginning-of-macro))
1801 ;; Inside a cpp directive. See if it should be skipped over.
1802 (let ((cpp-beg (point)))
1804 ;; Move back over all line continuations in the region skipped
1805 ;; over by `c-backward-comments'. If we go past it then we
1806 ;; started inside the cpp directive.
1807 (goto-char simple-ws-beg
)
1809 (while (and (> (point) cmt-skip-pos
)
1810 (progn (backward-char)
1811 (eq (char-before) ?
\\)))
1812 (beginning-of-line))
1814 (if (< (point) cmt-skip-pos
)
1815 ;; Don't move past the cpp directive if we began inside
1816 ;; it. Note that the position at the end of the last line
1817 ;; of the macro is also considered to be within it.
1818 (progn (goto-char cmt-skip-pos
)
1821 ;; It's worthwhile to spend a little bit of effort on finding
1822 ;; the end of the macro, to get a good `simple-ws-beg'
1823 ;; position for the cache. Note that `c-backward-comments'
1824 ;; could have stepped over some comments before going into
1825 ;; the macro, and then `simple-ws-beg' must be kept on the
1826 ;; same side of those comments.
1827 (goto-char simple-ws-beg
)
1828 (skip-chars-backward " \t\n\r\f\v")
1829 (if (eq (char-before) ?
\\)
1832 (if (< (point) simple-ws-beg
)
1833 ;; Might happen if comments after the macro were skipped
1835 (setq simple-ws-beg
(point)))
1840 ((/= (save-excursion
1841 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg
)
1842 (setq next-rung-pos
(point)))
1844 ;; Skipped over comments. Must put point at the end of
1845 ;; the simple ws at point since we might be after a line
1846 ;; comment or cpp directive that's been partially
1847 ;; narrowed out, and we can't risk marking the simple ws
1848 ;; at the end of it.
1849 (goto-char next-rung-pos
)
1852 ;; We've searched over a piece of non-white syntactic ws. See if this
1854 (setq next-rung-pos
(point))
1855 (skip-chars-backward " \t\f\v")
1858 ;; Cache if we started either from a marked rung or from a
1859 ;; completely uncached position.
1861 (not (get-text-property (1- simple-ws-beg
) 'c-in-sws
))
1863 ;; Cache if there's a marked rung in the encountered simple ws.
1865 (skip-chars-backward " \t\n\r\f\v")
1866 (text-property-any (point) (min (1+ next-rung-pos
) (point-max))
1871 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1872 (point) (1+ next-rung-pos
)
1873 simple-ws-beg
(min (1+ rung-pos
) (point-max))
1876 ;; Remove the properties for any nested ws that might be cached.
1877 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1879 (c-remove-is-sws (1+ next-rung-pos
) simple-ws-beg
)
1880 (unless (and rung-is-marked
(= simple-ws-beg rung-pos
))
1881 (let ((rung-end-pos (min (1+ rung-pos
) (point-max))))
1882 (unless (get-text-property (1- rung-end-pos
) 'c-is-sws
)
1883 ;; Remove any `c-in-sws' property from the last char of
1884 ;; the rung before we mark it with `c-is-sws', so that we
1885 ;; won't connect with the remains of a broken "ladder".
1886 (c-remove-in-sws (1- rung-end-pos
) rung-end-pos
))
1887 (c-put-is-sws simple-ws-beg
1889 (setq rung-is-marked t
)))
1890 (c-put-in-sws (setq simple-ws-beg
(point)
1891 last-put-in-sws-pos simple-ws-beg
)
1893 (c-put-is-sws (setq rung-pos simple-ws-beg
)
1894 (1+ next-rung-pos
)))
1897 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1898 (point) (1+ next-rung-pos
)
1899 simple-ws-beg
(min (1+ rung-pos
) (point-max))
1901 (setq rung-pos next-rung-pos
1902 simple-ws-beg
(point))
1905 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1906 ;; another one before the point (which might occur when editing inside a
1907 ;; comment or macro).
1908 (when (eq last-put-in-sws-pos
(point))
1909 (cond ((< (point-min) last-put-in-sws-pos
)
1911 "c-backward-sws clearing at %s for cache separation"
1912 (1- last-put-in-sws-pos
))
1913 (c-remove-in-sws (1- last-put-in-sws-pos
)
1914 last-put-in-sws-pos
))
1916 ;; If at bob and the buffer is narrowed, we have to clear the
1917 ;; character we're standing on instead since there might be a
1918 ;; `c-in-sws' before (point-min). In this case it's necessary
1919 ;; to clear both properties.
1921 "c-backward-sws clearing thoroughly at %s for cache separation"
1922 last-put-in-sws-pos
)
1923 (c-remove-is-and-in-sws last-put-in-sws-pos
1924 (1+ last-put-in-sws-pos
)))))
1928 ;; Other whitespace tools
1929 (defun c-partial-ws-p (beg end
)
1930 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1931 ;; region? This is a "heuristic" function. .....
1933 ;; The motivation for the second bit is to check whether removing this
1934 ;; region would coalesce two symbols.
1936 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1937 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1939 (let ((end+1 (min (1+ end
) (point-max))))
1940 (or (progn (goto-char (max (point-min) (1- beg
)))
1941 (c-skip-ws-forward end
)
1943 (progn (goto-char beg
)
1944 (c-skip-ws-forward end
+1)
1945 (eq (point) end
+1))))))
1947 ;; A system for finding noteworthy parens before the point.
1949 (defvar c-state-cache nil
)
1950 (make-variable-buffer-local 'c-state-cache
)
1951 ;; The state cache used by `c-parse-state' to cut down the amount of
1952 ;; searching. It's the result from some earlier `c-parse-state' call.
1954 ;; The use of the cached info is more effective if the next
1955 ;; `c-parse-state' call is on a line close by the one the cached state
1956 ;; was made at; the cache can actually slow down a little if the
1957 ;; cached state was made very far back in the buffer. The cache is
1958 ;; most effective if `c-parse-state' is used on each line while moving
1961 (defvar c-state-cache-start
1)
1962 (make-variable-buffer-local 'c-state-cache-start
)
1963 ;; This is (point-min) when `c-state-cache' was calculated, since a
1964 ;; change of narrowing is likely to affect the parens that are visible
1965 ;; before the point.
1967 (defvar c-state-cache-good-pos
1)
1968 (make-variable-buffer-local 'c-state-cache-good-pos
)
1969 ;; This is a position where `c-state-cache' is known to be correct.
1970 ;; It's a position inside one of the recorded unclosed parens or the
1971 ;; top level, but not further nested inside any literal or subparen
1972 ;; that is closed before the last recorded position.
1974 ;; The exact position is chosen to try to be close to yet earlier than
1975 ;; the position where `c-state-cache' will be called next. Right now
1976 ;; the heuristic is to set it to the position after the last found
1977 ;; closing paren (of any type) before the line on which
1978 ;; `c-parse-state' was called. That is chosen primarily to work well
1979 ;; with refontification of the current line.
1981 (defsubst c-invalidate-state-cache
(pos)
1982 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1983 ;; at POS or higher. This is much like `c-whack-state-after', but
1984 ;; it never changes a paren pair element into an open paren element.
1985 ;; Doing that would mean that the new open paren wouldn't have the
1986 ;; required preceding paren pair element.
1987 (while (and (or c-state-cache
1988 (when (< pos c-state-cache-good-pos
)
1989 (setq c-state-cache-good-pos
1)
1991 (let ((elem (car c-state-cache
)))
1993 (or (< pos
(cdr elem
))
1994 (when (< pos c-state-cache-good-pos
)
1995 (setq c-state-cache-good-pos
(cdr elem
))
1998 (when (< pos c-state-cache-good-pos
)
1999 (setq c-state-cache-good-pos
(1+ elem
))
2001 (setq c-state-cache
(cdr c-state-cache
))))
2003 (defun c-get-fallback-start-pos (here)
2004 ;; Return the start position for building `c-state-cache' from
2007 ;; Go back 2 bods, but ignore any bogus positions returned by
2008 ;; beginning-of-defun (i.e. open paren in column zero).
2011 (while (not (or (bobp) (zerop cnt
)))
2012 (c-beginning-of-defun-1)
2013 (if (eq (char-after) ?\
{)
2014 (setq cnt
(1- cnt
)))))
2017 (defun c-parse-state ()
2018 ;; Find and record all noteworthy parens between some good point
2019 ;; earlier in the file and point. That good point is at least the
2020 ;; beginning of the top-level construct we are in, or the beginning
2021 ;; of the preceding top-level construct if we aren't in one.
2023 ;; The returned value is a list of the noteworthy parens with the
2024 ;; last one first. If an element in the list is an integer, it's
2025 ;; the position of an open paren which has not been closed before
2026 ;; the point. If an element is a cons, it gives the position of a
2027 ;; closed brace paren pair; the car is the start paren position and
2028 ;; the cdr is the position following the closing paren. Only the
2029 ;; last closed brace paren pair before each open paren and before
2030 ;; the point is recorded, and thus the state never contains two cons
2031 ;; elements in succession.
2033 ;; Currently no characters which are given paren syntax with the
2034 ;; syntax-table property are recorded, i.e. angle bracket arglist
2035 ;; parens are never present here. Note that this might change.
2037 ;; BUG: This function doesn't cope entirely well with unbalanced
2038 ;; parens in macros. E.g. in the following case the brace before
2039 ;; the macro isn't balanced with the one after it:
2045 ;; This function might do hidden buffer changes.
2048 (let* ((here (point))
2049 (here-bol (c-point 'bol
))
2050 (c-macro-start (c-query-macro-start))
2051 (in-macro-start (or c-macro-start
(point)))
2052 old-state last-pos brace-pair-open brace-pair-close
2054 (c-invalidate-state-cache here
)
2056 ;; If the minimum position has changed due to narrowing then we
2057 ;; have to fix the tail of `c-state-cache' accordingly.
2058 (unless (= c-state-cache-start
(point-min))
2059 (if (> (point-min) c-state-cache-start
)
2060 ;; If point-min has moved forward then we just need to cut
2061 ;; off a bit of the tail.
2062 (let ((ptr (cons nil c-state-cache
)) elem
)
2063 (while (and (setq elem
(car-safe (cdr ptr
)))
2064 (>= (if (consp elem
) (car elem
) elem
)
2066 (setq ptr
(cdr ptr
)))
2068 (if (eq (cdr ptr
) c-state-cache
)
2069 (setq c-state-cache nil
2070 c-state-cache-good-pos
1)
2072 ;; If point-min has moved backward then we drop the state
2073 ;; completely. It's possible to do a better job here and
2074 ;; recalculate the top only.
2075 (setq c-state-cache nil
2076 c-state-cache-good-pos
1))
2077 (setq c-state-cache-start
(point-min)))
2079 ;; Get the latest position we know are directly inside the
2080 ;; closest containing paren of the cached state.
2081 (setq last-pos
(and c-state-cache
2082 (if (consp (car c-state-cache
))
2083 (cdr (car c-state-cache
))
2084 (1+ (car c-state-cache
)))))
2085 (if (or (not last-pos
)
2086 (< last-pos c-state-cache-good-pos
))
2087 (setq last-pos c-state-cache-good-pos
)
2088 ;; Take the opportunity to move the cached good position
2090 (if (< last-pos here-bol
)
2091 (setq c-state-cache-good-pos last-pos
)))
2093 ;; Check if `last-pos' is in a macro. If it is, and we're not
2094 ;; in the same macro, we must discard everything on
2095 ;; `c-state-cache' that is inside the macro before using it.
2097 (goto-char last-pos
)
2098 (when (and (c-beginning-of-macro)
2099 (/= (point) in-macro-start
))
2100 (c-invalidate-state-cache (point))
2101 ;; Set `last-pos' again just like above except that there's
2102 ;; no use looking at `c-state-cache-good-pos' here.
2103 (setq last-pos
(if c-state-cache
2104 (if (consp (car c-state-cache
))
2105 (cdr (car c-state-cache
))
2106 (1+ (car c-state-cache
)))
2109 ;; If we've moved very far from the last cached position then
2110 ;; it's probably better to redo it from scratch, otherwise we
2111 ;; might spend a lot of time searching from `last-pos' down to
2113 (when (< last-pos
(- here
20000))
2114 ;; First get the fallback start position. If it turns out
2115 ;; that it's so far back that the cached state is closer then
2116 ;; we'll keep it afterall.
2117 (setq pos
(c-get-fallback-start-pos here
))
2118 (if (<= pos last-pos
)
2122 c-state-cache-good-pos
1)))
2124 ;; Find the start position for the forward search. (Can't
2125 ;; search in the backward direction since the point might be in
2126 ;; some kind of literal.)
2129 (setq old-state c-state-cache
)
2131 ;; There's a cached state with a containing paren. Pop off
2132 ;; the stale containing sexps from it by going forward out of
2133 ;; parens as far as possible.
2134 (narrow-to-region (point-min) here
)
2135 (let (placeholder pair-beg
)
2136 (while (and c-state-cache
2138 (c-up-list-forward last-pos
)))
2139 (setq last-pos placeholder
)
2140 (if (consp (car c-state-cache
))
2141 (setq pair-beg
(car-safe (cdr c-state-cache
))
2142 c-state-cache
(cdr-safe (cdr c-state-cache
)))
2143 (setq pair-beg
(car c-state-cache
)
2144 c-state-cache
(cdr c-state-cache
))))
2146 (when (and pair-beg
(eq (char-after pair-beg
) ?
{))
2147 ;; The last paren pair we moved out from was a brace
2148 ;; pair. Modify the state to record this as a closed
2150 (if (consp (car-safe c-state-cache
))
2151 (setq c-state-cache
(cdr c-state-cache
)))
2152 (setq c-state-cache
(cons (cons pair-beg last-pos
)
2155 ;; Check if the preceding balanced paren is within a
2156 ;; macro; it should be ignored if we're outside the
2157 ;; macro. There's no need to check any further upwards;
2158 ;; if the macro contains an unbalanced opening paren then
2159 ;; we're smoked anyway.
2160 (when (and (<= (point) in-macro-start
)
2161 (consp (car c-state-cache
)))
2163 (goto-char (car (car c-state-cache
)))
2164 (when (c-beginning-of-macro)
2166 c-state-cache
(cdr c-state-cache
)))))
2168 (unless (eq c-state-cache old-state
)
2169 ;; Have to adjust the cached good position if state has been
2171 (setq c-state-cache-good-pos
2173 (if (consp (car c-state-cache
))
2174 (cdr (car c-state-cache
))
2175 (1+ (car c-state-cache
)))
2177 old-state c-state-cache
))
2180 (setq pos last-pos
)))
2182 ;; Get the fallback start position.
2184 (setq pos
(c-get-fallback-start-pos here
)
2186 c-state-cache-good-pos
1))
2188 (narrow-to-region (point-min) here
)
2192 brace-pair-open nil
)
2194 ;; Find the balanced brace pairs. This loop is hot, so it
2195 ;; does ugly tricks to go faster.
2197 (let (set-good-pos set-brace-pair
)
2200 last-pos
(scan-lists pos
1 -
1)) ; Might signal.
2201 (setq pos
(scan-lists last-pos
1 1) ; Might signal.
2202 set-good-pos
(< pos here-bol
)
2203 set-brace-pair
(eq (char-before last-pos
) ?
{))
2205 ;; Update the cached good position and record the brace
2206 ;; pair, whichever is applicable for the paren we've
2207 ;; just jumped over. But first check that it isn't
2208 ;; inside a macro and the point isn't inside the same
2210 (when (and (or set-good-pos set-brace-pair
)
2211 (or (>= pos in-macro-start
)
2214 (not (c-beginning-of-macro)))))
2216 (setq c-state-cache-good-pos pos
))
2218 (setq brace-pair-open last-pos
2219 brace-pair-close pos
))))))
2221 ;; Record the last brace pair.
2222 (when brace-pair-open
2223 (let ((head (car-safe c-state-cache
)))
2226 (setcar head
(1- brace-pair-open
))
2227 (setcdr head brace-pair-close
))
2228 (setq c-state-cache
(cons (cons (1- brace-pair-open
)
2233 ;; Prepare to loop, but record the open paren only if it's
2234 ;; outside a macro or within the same macro as point, and
2235 ;; if it is a legitimate open paren and not some character
2236 ;; that got an open paren syntax-table property.
2239 (when (and (or (>= last-pos in-macro-start
)
2241 (goto-char last-pos
)
2242 (not (c-beginning-of-macro))))
2243 ;; Check for known types of parens that we
2244 ;; want to record. The syntax table is not to
2245 ;; be trusted here since the caller might be
2246 ;; using e.g. `c++-template-syntax-table'.
2247 (memq (char-before last-pos
) '(?
{ ?\
( ?\
[)))
2248 (if (< last-pos here-bol
)
2249 (setq c-state-cache-good-pos last-pos
))
2250 (setq c-state-cache
(cons (1- last-pos
) c-state-cache
))))
2252 (if (setq last-pos
(c-up-list-forward pos
))
2253 ;; Found a close paren without a corresponding opening
2254 ;; one. Maybe we didn't go back far enough, so try to
2255 ;; scan backward for the start paren and then start over.
2257 (setq pos
(c-up-list-backward pos
)
2259 c-state-cache-good-pos c-state-cache-start
)
2261 ;; Emacs (up to at least 21.2) can get confused by
2262 ;; open parens in column zero inside comments: The
2263 ;; sexp functions can then misbehave and bring us
2264 ;; back to the same point again. Check this so that
2265 ;; we don't get an infinite loop.
2269 (format "Unbalanced close paren at line %d"
2270 (1+ (count-lines (point-min)
2271 (c-point 'bol last-pos
)))))))
2274 ;;(message "c-parse-state: %S end: %S" c-state-cache c-state-cache-good-pos)
2277 ;; Debug tool to catch cache inconsistencies.
2278 (defvar c-debug-parse-state nil
)
2279 (unless (fboundp 'c-real-parse-state
)
2280 (fset 'c-real-parse-state
(symbol-function 'c-parse-state
)))
2281 (cc-bytecomp-defun c-real-parse-state)
2282 (defun c-debug-parse-state ()
2283 (let ((res1 (c-real-parse-state)) res2
)
2284 (let ((c-state-cache nil
)
2285 (c-state-cache-start 1)
2286 (c-state-cache-good-pos 1))
2287 (setq res2
(c-real-parse-state)))
2288 (unless (equal res1 res2
)
2289 ;; The cache can actually go further back due to the ad-hoc way
2290 ;; the first paren is found, so try to whack off a bit of its
2291 ;; start before complaining.
2293 (goto-char (or (c-least-enclosing-brace res2
) (point)))
2294 (c-beginning-of-defun-1)
2295 (while (not (or (bobp) (eq (char-after) ?
{)))
2296 (c-beginning-of-defun-1))
2297 (unless (equal (c-whack-state-before (point) res1
) res2
)
2298 (message (concat "c-parse-state inconsistency: "
2299 "using cache: %s, from scratch: %s")
2302 (defun c-toggle-parse-state-debug (&optional arg
)
2304 (setq c-debug-parse-state
(c-calculate-state arg c-debug-parse-state
))
2305 (fset 'c-parse-state
(symbol-function (if c-debug-parse-state
2306 'c-debug-parse-state
2307 'c-real-parse-state
)))
2308 (c-keep-region-active))
2309 (when c-debug-parse-state
2310 (c-toggle-parse-state-debug 1))
2312 (defun c-whack-state-before (bufpos paren-state
)
2313 ;; Whack off any state information from PAREN-STATE which lies
2314 ;; before BUFPOS. Not destructive on PAREN-STATE.
2315 (let* ((newstate (list nil
))
2319 (setq car
(car paren-state
)
2320 paren-state
(cdr paren-state
))
2321 (if (< (if (consp car
) (car car
) car
) bufpos
)
2322 (setq paren-state nil
)
2323 (setcdr ptr
(list car
))
2324 (setq ptr
(cdr ptr
))))
2327 (defun c-whack-state-after (bufpos paren-state
)
2328 ;; Whack off any state information from PAREN-STATE which lies at or
2329 ;; after BUFPOS. Not destructive on PAREN-STATE.
2332 (let ((car (car paren-state
)))
2334 ;; just check the car, because in a balanced brace
2335 ;; expression, it must be impossible for the corresponding
2336 ;; close brace to be before point, but the open brace to
2338 (if (<= bufpos
(car car
))
2340 (if (< bufpos
(cdr car
))
2341 ;; its possible that the open brace is before
2342 ;; bufpos, but the close brace is after. In that
2343 ;; case, convert this to a non-cons element. The
2344 ;; rest of the state is before bufpos, so we're
2346 (throw 'done
(cons (car car
) (cdr paren-state
)))
2347 ;; we know that both the open and close braces are
2348 ;; before bufpos, so we also know that everything else
2349 ;; on state is before bufpos.
2350 (throw 'done paren-state
)))
2353 ;; it's before bufpos, so everything else should too.
2354 (throw 'done paren-state
)))
2355 (setq paren-state
(cdr paren-state
)))
2358 (defun c-most-enclosing-brace (paren-state &optional bufpos
)
2359 ;; Return the bufpos of the innermost enclosing open paren before
2360 ;; bufpos, or nil if none was found.
2362 (or bufpos
(setq bufpos
134217727))
2364 (setq enclosingp
(car paren-state
)
2365 paren-state
(cdr paren-state
))
2366 (if (or (consp enclosingp
)
2367 (>= enclosingp bufpos
))
2368 (setq enclosingp nil
)
2369 (setq paren-state nil
)))
2372 (defun c-least-enclosing-brace (paren-state)
2373 ;; Return the bufpos of the outermost enclosing open paren, or nil
2374 ;; if none was found.
2377 (setq elem
(car paren-state
)
2378 paren-state
(cdr paren-state
))
2383 (defun c-safe-position (bufpos paren-state
)
2384 ;; Return the closest "safe" position recorded on PAREN-STATE that
2385 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
2386 ;; contain any. Return nil if BUFPOS is nil, which is useful to
2387 ;; find the closest limit before a given limit that might be nil.
2389 ;; A "safe" position is a position at or after a recorded open
2390 ;; paren, or after a recorded close paren. The returned position is
2391 ;; thus either the first position after a close brace, or the first
2392 ;; position after an enclosing paren, or at the enclosing paren in
2393 ;; case BUFPOS is immediately after it.
2398 (setq elem
(car paren-state
))
2400 (cond ((< (cdr elem
) bufpos
)
2401 (throw 'done
(cdr elem
)))
2402 ((< (car elem
) bufpos
)
2404 (throw 'done
(min (1+ (car elem
)) bufpos
))))
2406 ;; elem is the position at and not after the opening paren, so
2407 ;; we can go forward one more step unless it's equal to
2408 ;; bufpos. This is useful in some cases avoid an extra paren
2409 ;; level between the safe position and bufpos.
2410 (throw 'done
(min (1+ elem
) bufpos
))))
2411 (setq paren-state
(cdr paren-state
)))))))
2413 (defun c-beginning-of-syntax ()
2414 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2415 ;; goes to the closest previous point that is known to be outside
2416 ;; any string literal or comment. `c-state-cache' is used if it has
2417 ;; a position in the vicinity.
2418 (let* ((paren-state c-state-cache
)
2422 ;; Note: Similar code in `c-safe-position'. The
2423 ;; difference is that we accept a safe position at
2424 ;; the point and don't bother to go forward past open
2427 (setq elem
(car paren-state
))
2429 (cond ((<= (cdr elem
) (point))
2430 (throw 'done
(cdr elem
)))
2431 ((<= (car elem
) (point))
2432 (throw 'done
(car elem
))))
2433 (if (<= elem
(point))
2434 (throw 'done elem
)))
2435 (setq paren-state
(cdr paren-state
)))
2438 (if (> pos
(- (point) 4000))
2440 ;; The position is far back. Try `c-beginning-of-defun-1'
2441 ;; (although we can't be entirely sure it will go to a position
2442 ;; outside a comment or string in current emacsen). FIXME:
2443 ;; Consult `syntax-ppss' here.
2444 (c-beginning-of-defun-1)
2449 ;; Tools for scanning identifiers and other tokens.
2451 (defun c-on-identifier ()
2452 "Return non-nil if the point is on or directly after an identifier.
2453 Keywords are recognized and not considered identifiers. If an
2454 identifier is detected, the returned value is its starting position.
2455 If an identifier ends at the point and another begins at it \(can only
2456 happen in Pike) then the point for the preceding one is returned.
2458 Note that this function might do hidden buffer changes. See the
2459 comment at the start of cc-engine.el for more info."
2461 ;; FIXME: Shouldn't this function handle "operator" in C++?
2464 (skip-syntax-backward "w_")
2468 ;; Check for a normal (non-keyword) identifier.
2469 (and (looking-at c-symbol-start
)
2470 (not (looking-at c-keywords-regexp
))
2473 (when (c-major-mode-is 'pike-mode
)
2474 ;; Handle the `<operator> syntax in Pike.
2475 (let ((pos (point)))
2476 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2477 (and (if (< (skip-chars-backward "`") 0)
2480 (eq (char-after) ?\
`))
2481 (looking-at c-symbol-key
)
2482 (>= (match-end 0) pos
)
2485 ;; Handle the "operator +" syntax in C++.
2486 (when (and c-overloadable-operators-regexp
2487 (= (c-backward-token-2 0) 0))
2489 (cond ((and (looking-at c-overloadable-operators-regexp
)
2490 (or (not c-opt-op-identifier-prefix
)
2491 (and (= (c-backward-token-2 1) 0)
2492 (looking-at c-opt-op-identifier-prefix
))))
2496 (and c-opt-op-identifier-prefix
2497 (looking-at c-opt-op-identifier-prefix
)
2498 (= (c-forward-token-2 1) 0)
2499 (looking-at c-overloadable-operators-regexp
)))
2504 (defsubst c-simple-skip-symbol-backward
()
2505 ;; If the point is at the end of a symbol then skip backward to the
2506 ;; beginning of it. Don't move otherwise. Return non-nil if point
2509 ;; This function might do hidden buffer changes.
2510 (or (< (skip-syntax-backward "w_") 0)
2511 (and (c-major-mode-is 'pike-mode
)
2512 ;; Handle the `<operator> syntax in Pike.
2513 (let ((pos (point)))
2514 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2515 (< (skip-chars-backward "`") 0)
2516 (looking-at c-symbol-key
)
2517 (>= (match-end 0) pos
))
2522 (defun c-beginning-of-current-token (&optional back-limit
)
2523 ;; Move to the beginning of the current token. Do not move if not
2524 ;; in the middle of one. BACK-LIMIT may be used to bound the
2525 ;; backward search; if given it's assumed to be at the boundary
2526 ;; between two tokens. Return non-nil if the point is moved, nil
2529 ;; This function might do hidden buffer changes.
2530 (let ((start (point)))
2531 (if (looking-at "\\w\\|\\s_")
2532 (skip-syntax-backward "w_" back-limit
)
2533 (when (< (skip-syntax-backward ".()" back-limit
) 0)
2534 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp
)
2536 ;; `c-nonsymbol-token-regexp' should always match
2537 ;; since we've skipped backward over punctuator
2538 ;; or paren syntax, but consume one char in case
2539 ;; it doesn't so that we don't leave point before
2540 ;; some earlier incorrect token.
2543 (goto-char pos
))))))
2546 (defun c-end-of-current-token (&optional back-limit
)
2547 ;; Move to the end of the current token. Do not move if not in the
2548 ;; middle of one. BACK-LIMIT may be used to bound the backward
2549 ;; search; if given it's assumed to be at the boundary between two
2550 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2552 ;; This function might do hidden buffer changes.
2553 (let ((start (point)))
2554 (cond ((< (skip-syntax-backward "w_" (1- start
)) 0)
2555 (skip-syntax-forward "w_"))
2556 ((< (skip-syntax-backward ".()" back-limit
) 0)
2558 (if (looking-at c-nonsymbol-token-regexp
)
2559 (goto-char (match-end 0))
2560 ;; `c-nonsymbol-token-regexp' should always match since
2561 ;; we've skipped backward over punctuator or paren
2562 ;; syntax, but move forward in case it doesn't so that
2563 ;; we don't leave point earlier than we started with.
2565 (< (point) start
)))))
2568 (defconst c-jump-syntax-balanced
2569 (if (memq 'gen-string-delim c-emacs-features
)
2570 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2571 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2573 (defconst c-jump-syntax-unbalanced
2574 (if (memq 'gen-string-delim c-emacs-features
)
2575 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2576 "\\w\\|\\s_\\|\\s\""))
2578 (defun c-forward-token-2 (&optional count balanced limit
)
2579 "Move forward by tokens.
2580 A token is defined as all symbols and identifiers which aren't
2581 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2582 treated properly). Point is always either left at the beginning of a
2583 token or not moved at all. COUNT specifies the number of tokens to
2584 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2585 moves to the next token beginning only if not already at one. If
2586 BALANCED is true, move over balanced parens, otherwise move into them.
2587 Also, if BALANCED is true, never move out of an enclosing paren.
2589 LIMIT sets the limit for the movement and defaults to the point limit.
2590 The case when LIMIT is set in the middle of a token, comment or macro
2591 is handled correctly, i.e. the point won't be left there.
2593 Return the number of tokens left to move \(positive or negative). If
2594 BALANCED is true, a move over a balanced paren counts as one. Note
2595 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2596 be returned. Thus, a return value of 0 guarantees that point is at
2597 the requested position and a return value less \(without signs) than
2598 COUNT guarantees that point is at the beginning of some token.
2600 Note that this function might do hidden buffer changes. See the
2601 comment at the start of cc-engine.el for more info."
2603 (or count
(setq count
1))
2605 (- (c-backward-token-2 (- count
) balanced limit
))
2607 (let ((jump-syntax (if balanced
2608 c-jump-syntax-balanced
2609 c-jump-syntax-unbalanced
))
2614 ;; If count is zero we should jump if in the middle of a token.
2615 (c-end-of-current-token))
2618 (if limit
(narrow-to-region (point-min) limit
))
2620 (progn (c-forward-syntactic-ws) (point)))
2621 ;; Skip whitespace. Count this as a move if we did in
2623 (setq count
(max (1- count
) 0)))
2626 ;; Moved out of bounds. Make sure the returned count isn't zero.
2628 (if (zerop count
) (setq count
1))
2631 ;; Use `condition-case' to avoid having the limit tests
2638 (cond ((looking-at jump-syntax
)
2639 (goto-char (scan-sexps (point) 1))
2641 ((looking-at c-nonsymbol-token-regexp
)
2642 (goto-char (match-end 0))
2644 ;; `c-nonsymbol-token-regexp' above should always
2645 ;; match if there are correct tokens. Try to
2646 ;; widen to see if the limit was set in the
2647 ;; middle of one, else fall back to treating
2648 ;; the offending thing as a one character token.
2652 (looking-at c-nonsymbol-token-regexp
)))
2657 (c-forward-syntactic-ws)
2660 (error (goto-char last
)))
2664 (setq count
(1+ count
)))))
2668 (defun c-backward-token-2 (&optional count balanced limit
)
2669 "Move backward by tokens.
2670 See `c-forward-token-2' for details."
2672 (or count
(setq count
1))
2674 (- (c-forward-token-2 (- count
) balanced limit
))
2676 (or limit
(setq limit
(point-min)))
2677 (let ((jump-syntax (if balanced
2678 c-jump-syntax-balanced
2679 c-jump-syntax-unbalanced
))
2683 ;; The count is zero so try to skip to the beginning of the
2686 (progn (c-beginning-of-current-token) (point)))
2687 (if (< (point) limit
)
2688 ;; The limit is inside the same token, so return 1.
2691 ;; We're not in the middle of a token. If there's
2692 ;; whitespace after the point then we must move backward,
2693 ;; so set count to 1 in that case.
2694 (and (looking-at c-syntactic-ws-start
)
2695 ;; If we're looking at a '#' that might start a cpp
2696 ;; directive then we have to do a more elaborate check.
2697 (or (/= (char-after) ?
#)
2698 (not c-opt-cpp-prefix
)
2701 (progn (beginning-of-line)
2702 (looking-at "[ \t]*")
2705 (progn (backward-char)
2706 (not (eq (char-before) ?
\\)))))))
2709 ;; Use `condition-case' to avoid having to check for buffer
2710 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2715 (c-backward-syntactic-ws)
2717 (if (looking-at jump-syntax
)
2718 (goto-char (scan-sexps (1+ (point)) -
1))
2719 ;; This can be very inefficient if there's a long
2720 ;; sequence of operator tokens without any separation.
2721 ;; That doesn't happen in practice, anyway.
2722 (c-beginning-of-current-token))
2723 (>= (point) limit
)))
2726 (error (goto-char last
)))
2728 (if (< (point) limit
)
2733 (defun c-forward-token-1 (&optional count balanced limit
)
2734 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2735 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2736 characters are jumped over character by character. This function is
2737 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2738 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2739 (c-forward-token-2 count balanced limit
)))
2741 (defun c-backward-token-1 (&optional count balanced limit
)
2742 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2743 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2744 characters are jumped over character by character. This function is
2745 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2746 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2747 (c-backward-token-2 count balanced limit
)))
2750 ;; Tools for doing searches restricted to syntactically relevant text.
2752 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2753 paren-level not-inside-token
2754 lookbehind-submatch
)
2755 "Like `re-search-forward', but only report matches that are found
2756 in syntactically significant text. I.e. matches in comments, macros
2757 or string literals are ignored. The start point is assumed to be
2758 outside any comment, macro or string literal, or else the content of
2759 that region is taken as syntactically significant text.
2761 If PAREN-LEVEL is non-nil, an additional restriction is added to
2762 ignore matches in nested paren sexps. The search will also not go
2763 outside the current list sexp, which has the effect that if the point
2764 should be moved to BOUND when no match is found \(i.e. NOERROR is
2765 neither nil nor t), then it will be at the closing paren if the end of
2766 the current list sexp is encountered first.
2768 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2769 ignored. Things like multicharacter operators and special symbols
2770 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2773 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2774 subexpression in REGEXP. The end of that submatch is used as the
2775 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2776 isn't used or if that subexpression didn't match then the start
2777 position of the whole match is used instead. The \"look behind\"
2778 subexpression is never tested before the starting position, so it
2779 might be a good idea to include \\=\\= as a match alternative in it.
2781 Optimization note: Matches might be missed if the \"look behind\"
2782 subexpression can match the end of nonwhite syntactic whitespace,
2783 i.e. the end of comments or cpp directives. This since the function
2784 skips over such things before resuming the search. It's on the other
2785 hand not safe to assume that the \"look behind\" subexpression never
2786 matches syntactic whitespace.
2788 Bug: Unbalanced parens inside cpp directives are currently not handled
2789 correctly \(i.e. they don't get ignored as they should) when
2792 Note that this function might do hidden buffer changes. See the
2793 comment at the start of cc-engine.el for more info."
2795 (or bound
(setq bound
(point-max)))
2796 (if paren-level
(setq paren-level -
1))
2798 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2800 (let ((start (point))
2802 ;; Start position for the last search.
2804 ;; The `parse-partial-sexp' state between the start position
2807 ;; The current position after the last state update. The next
2808 ;; `parse-partial-sexp' continues from here.
2810 ;; The position at which to check the state and the state
2811 ;; there. This is separate from `state-pos' since we might
2812 ;; need to back up before doing the next search round.
2813 check-pos check-state
2814 ;; Last position known to end a token.
2815 (last-token-end-pos (point-min))
2816 ;; Set when a valid match is found.
2823 (setq search-pos
(point))
2824 (re-search-forward regexp bound noerror
))
2827 (setq state
(parse-partial-sexp
2828 state-pos
(match-beginning 0) paren-level nil state
)
2830 (if (setq check-pos
(and lookbehind-submatch
2831 (or (not paren-level
)
2833 (match-end lookbehind-submatch
)))
2834 (setq check-state
(parse-partial-sexp
2835 state-pos check-pos paren-level nil state
))
2836 (setq check-pos state-pos
2839 ;; NOTE: If we got a look behind subexpression and get
2840 ;; an insignificant match in something that isn't
2841 ;; syntactic whitespace (i.e. strings or in nested
2842 ;; parentheses), then we can never skip more than a
2843 ;; single character from the match start position
2844 ;; (i.e. `state-pos' here) before continuing the
2845 ;; search. That since the look behind subexpression
2846 ;; might match the end of the insignificant region in
2850 ((elt check-state
7)
2851 ;; Match inside a line comment. Skip to eol. Use
2852 ;; `re-search-forward' instead of `skip-chars-forward' to get
2853 ;; the right bound behavior.
2854 (re-search-forward "[\n\r]" bound noerror
))
2856 ((elt check-state
4)
2857 ;; Match inside a block comment. Skip to the '*/'.
2858 (search-forward "*/" bound noerror
))
2860 ((and (not (elt check-state
5))
2861 (eq (char-before check-pos
) ?
/)
2862 (not (c-get-char-property (1- check-pos
) 'syntax-table
))
2863 (memq (char-after check-pos
) '(?
/ ?
*)))
2864 ;; Match in the middle of the opener of a block or line
2866 (if (= (char-after check-pos
) ?
/)
2867 (re-search-forward "[\n\r]" bound noerror
)
2868 (search-forward "*/" bound noerror
)))
2870 ;; The last `parse-partial-sexp' above might have
2871 ;; stopped short of the real check position if the end
2872 ;; of the current sexp was encountered in paren-level
2873 ;; mode. The checks above are always false in that
2874 ;; case, and since they can do better skipping in
2875 ;; lookbehind-submatch mode, we do them before
2876 ;; checking the paren level.
2879 (/= (setq tmp
(car check-state
)) 0))
2880 ;; Check the paren level first since we're short of the
2881 ;; syntactic checking position if the end of the
2882 ;; current sexp was encountered by `parse-partial-sexp'.
2885 ;; Inside a nested paren sexp.
2886 (if lookbehind-submatch
2887 ;; See the NOTE above.
2888 (progn (goto-char state-pos
) t
)
2889 ;; Skip out of the paren quickly.
2890 (setq state
(parse-partial-sexp state-pos bound
0 nil state
)
2893 ;; Have exited the current paren sexp.
2896 ;; The last `parse-partial-sexp' call above
2897 ;; has left us just after the closing paren
2898 ;; in this case, so we can modify the bound
2899 ;; to leave the point at the right position
2901 (setq bound
(1- (point)))
2903 (signal 'search-failed
(list regexp
)))))
2905 ((setq tmp
(elt check-state
3))
2906 ;; Match inside a string.
2907 (if (or lookbehind-submatch
2908 (not (integerp tmp
)))
2909 ;; See the NOTE above.
2910 (progn (goto-char state-pos
) t
)
2911 ;; Skip to the end of the string before continuing.
2912 (let ((ender (make-string 1 tmp
)) (continue t
))
2913 (while (if (search-forward ender bound noerror
)
2915 (setq state
(parse-partial-sexp
2916 state-pos
(point) nil nil state
)
2919 (setq continue nil
)))
2924 (c-beginning-of-macro start
)))
2925 ;; Match inside a macro. Skip to the end of it.
2927 (cond ((<= (point) bound
) t
)
2929 (t (signal 'search-failed
(list regexp
)))))
2931 ((and not-inside-token
2932 (or (< check-pos last-token-end-pos
)
2935 (goto-char check-pos
)
2937 (c-end-of-current-token last-token-end-pos
))
2938 (setq last-token-end-pos
(point))))))
2940 (if lookbehind-submatch
2941 ;; See the NOTE above.
2942 (goto-char state-pos
)
2943 (goto-char (min last-token-end-pos bound
))))
2950 ;; Should loop to search again, but take care to avoid
2951 ;; looping on the same spot.
2952 (or (/= search-pos
(point))
2953 (if (= (point) bound
)
2956 (signal 'search-failed
(list regexp
)))
2962 (signal (car err
) (cdr err
))))
2964 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2968 (goto-char (match-end 0))
2971 ;; Search failed. Set point as appropriate.
2977 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level
)
2978 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2979 i.e. don't stop at positions inside syntactic whitespace or string
2980 literals. Preprocessor directives are also ignored, with the exception
2981 of the one that the point starts within, if any. If LIMIT is given,
2982 it's assumed to be at a syntactically relevant position.
2984 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
2985 sexps, and the search will also not go outside the current paren sexp.
2986 However, if LIMIT or the buffer limit is reached inside a nested paren
2987 then the point will be left at the limit.
2989 Non-nil is returned if the point moved, nil otherwise.
2991 Note that this function might do hidden buffer changes. See the
2992 comment at the start of cc-engine.el for more info."
2994 (let ((start (point))
2996 ;; A list of syntactically relevant positions in descending
2997 ;; order. It's used to avoid scanning repeatedly over
2998 ;; potentially large regions with `parse-partial-sexp' to verify
3001 ;; The position at the beginning of `safe-pos-list'.
3003 ;; The result from `c-beginning-of-macro' at the start position or the
3004 ;; start position itself if it isn't within a macro. Evaluated on
3007 ;; The earliest position after the current one with the same paren
3008 ;; level. Used only when `paren-level' is set.
3009 (paren-level-pos (point)))
3013 (< (skip-chars-backward skip-chars limit
) 0)
3015 ;; Use `parse-partial-sexp' from a safe position down to
3016 ;; the point to check if it's outside comments and
3018 (let ((pos (point)) state-2 pps-end-pos
)
3019 ;; Pick a safe position as close to the point as
3022 ;; FIXME: Consult `syntax-ppss' here if our
3023 ;; cache doesn't give a good position.
3024 (while (and safe-pos-list
3025 (> (car safe-pos-list
) (point)))
3026 (setq safe-pos-list
(cdr safe-pos-list
)))
3027 (unless (setq safe-pos
(car-safe safe-pos-list
))
3028 (setq safe-pos
(max (or (c-safe-position
3029 (point) (or c-state-cache
3033 safe-pos-list
(list safe-pos
)))
3035 ;; Cache positions along the way to use if we have to
3036 ;; back up more. We cache every closing paren on the
3037 ;; same level. If the paren cache is relevant in this
3038 ;; region then we're typically already on the same
3039 ;; level as the target position. Note that we might
3040 ;; cache positions after opening parens in case
3041 ;; safe-pos is in a nested list. That's both uncommon
3044 (setq state
(parse-partial-sexp
3047 (setq safe-pos
(point)
3048 safe-pos-list
(cons safe-pos safe-pos-list
)))
3051 ((or (elt state
3) (elt state
4))
3052 ;; Inside string or comment. Continue search at the
3054 (goto-char (elt state
8))
3059 (setq state-2
(parse-partial-sexp
3060 pos paren-level-pos -
1)
3061 pps-end-pos
(point))
3062 (/= (car state-2
) 0)))
3063 ;; Not at the right level.
3065 (if (and (< (car state-2
) 0)
3066 ;; We stop above if we go out of a paren.
3067 ;; Now check whether it precedes or is
3068 ;; nested in the starting sexp.
3072 pps-end-pos paren-level-pos
3074 (< (car state-2
) 0)))
3076 ;; We've stopped short of the starting position
3077 ;; so the hit was inside a nested list. Go up
3078 ;; until we are at the right level.
3081 (goto-char (scan-lists pos -
1
3083 (setq paren-level-pos
(point))
3084 (if (and limit
(>= limit paren-level-pos
))
3090 (goto-char (or limit
(point-min)))
3093 ;; The hit was outside the list at the start
3094 ;; position. Go to the start of the list and exit.
3095 (goto-char (1+ (elt state-2
1)))
3098 ((c-beginning-of-macro limit
)
3102 (setq start-macro-beg
3105 (c-beginning-of-macro limit
)
3109 ;; It's inside the same macro we started in so it's
3110 ;; a relevant match.
3114 ;; If the state contains the start of the containing sexp we
3115 ;; cache that position too, so that parse-partial-sexp in the
3116 ;; next run has a bigger chance of starting at the same level
3117 ;; as the target position and thus will get more good safe
3118 ;; positions into the list.
3120 (setq safe-pos
(1+ (elt state
1))
3121 safe-pos-list
(cons safe-pos safe-pos-list
))))
3125 ;; Skip syntactic ws afterwards so that we don't stop at the
3126 ;; end of a comment if `skip-chars' is something like "^/".
3127 (c-backward-syntactic-ws)
3130 ;; We might want to extend this with more useful return values in
3132 (/= (point) start
)))
3134 ;; The following is an alternative implementation of
3135 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3136 ;; track of the syntactic context. It turned out to be generally
3137 ;; slower than the one above which uses forward checks from earlier
3140 ;;(defconst c-ssb-stop-re
3141 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3142 ;; ;; stop at to avoid going into comments and literals.
3144 ;; ;; Match comment end syntax and string literal syntax. Also match
3145 ;; ;; '/' for block comment endings (not covered by comment end
3147 ;; "\\s>\\|/\\|\\s\""
3148 ;; (if (memq 'gen-string-delim c-emacs-features)
3151 ;; (if (memq 'gen-comment-delim c-emacs-features)
3155 ;;(defconst c-ssb-stop-paren-re
3156 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3157 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3159 ;;(defconst c-ssb-sexp-end-re
3160 ;; ;; Regexp matching the ending syntax of a complex sexp.
3161 ;; (concat c-string-limit-regexp "\\|\\s)"))
3163 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3164 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3165 ;;i.e. don't stop at positions inside syntactic whitespace or string
3166 ;;literals. Preprocessor directives are also ignored. However, if the
3167 ;;point is within a comment, string literal or preprocessor directory to
3168 ;;begin with, its contents is treated as syntactically relevant chars.
3169 ;;If LIMIT is given, it limits the backward search and the point will be
3170 ;;left there if no earlier position is found.
3172 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3173 ;;sexps, and the search will also not go outside the current paren sexp.
3174 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3175 ;;then the point will be left at the limit.
3177 ;;Non-nil is returned if the point moved, nil otherwise.
3179 ;;Note that this function might do hidden buffer changes. See the
3180 ;;comment at the start of cc-engine.el for more info."
3182 ;; (save-restriction
3184 ;; (narrow-to-region limit (point-max)))
3186 ;; (let ((start (point)))
3188 ;; (while (let ((last-pos (point))
3190 ;; (skip-chars-backward skip-chars)
3193 ;; ;; Skip back over the same region as
3194 ;; ;; `skip-chars-backward' above, but keep to
3195 ;; ;; syntactically relevant positions.
3196 ;; (goto-char last-pos)
3198 ;; ;; `re-search-backward' with a single char regexp
3199 ;; ;; should be fast.
3200 ;; (re-search-backward
3201 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3206 ;; ((looking-at "\\s(")
3207 ;; ;; `paren-level' is set and we've found the
3208 ;; ;; start of the containing paren.
3212 ;; ((looking-at c-ssb-sexp-end-re)
3213 ;; ;; We're at the end of a string literal or paren
3214 ;; ;; sexp (if `paren-level' is set).
3216 ;; (condition-case nil
3217 ;; (c-backward-sexp)
3219 ;; (goto-char limit)
3220 ;; (throw 'done t))))
3224 ;; ;; At the end of some syntactic ws or possibly
3225 ;; ;; after a plain '/' operator.
3226 ;; (let ((pos (point)))
3227 ;; (c-backward-syntactic-ws)
3228 ;; (if (= pos (point))
3229 ;; ;; Was a plain '/' operator. Go past it.
3230 ;; (backward-char)))))
3232 ;; (> (point) stop-pos))))
3234 ;; ;; Now the point is either at `stop-pos' or at some
3235 ;; ;; position further back if `stop-pos' was at a
3236 ;; ;; syntactically irrelevant place.
3238 ;; ;; Skip additional syntactic ws so that we don't stop
3239 ;; ;; at the end of a comment if `skip-chars' is
3240 ;; ;; something like "^/".
3241 ;; (c-backward-syntactic-ws)
3243 ;; (< (point) stop-pos))))
3245 ;; ;; We might want to extend this with more useful return values
3246 ;; ;; in the future.
3247 ;; (/= (point) start))))
3250 ;; Tools for handling comments and string literals.
3252 (defun c-slow-in-literal (&optional lim detect-cpp
)
3253 "Return the type of literal point is in, if any.
3254 The return value is `c' if in a C-style comment, `c++' if in a C++
3255 style comment, `string' if in a string literal, `pound' if DETECT-CPP
3256 is non-nil and in a preprocessor line, or nil if somewhere else.
3257 Optional LIM is used as the backward limit of the search. If omitted,
3258 or nil, `c-beginning-of-defun' is used.
3260 The last point calculated is cached if the cache is enabled, i.e. if
3261 `c-in-literal-cache' is bound to a two element vector.
3263 Note that this function might do hidden buffer changes. See the
3264 comment at the start of cc-engine.el for more info."
3266 (if (and (vectorp c-in-literal-cache
)
3267 (= (point) (aref c-in-literal-cache
0)))
3268 (aref c-in-literal-cache
1)
3269 (let ((rtn (save-excursion
3270 (let* ((pos (point))
3272 (c-beginning-of-syntax)
3274 (state (parse-partial-sexp lim pos
)))
3276 ((elt state
3) 'string
)
3277 ((elt state
4) (if (elt state
7) 'c
++ 'c
))
3278 ((and detect-cpp
(c-beginning-of-macro lim
)) 'pound
)
3280 ;; cache this result if the cache is enabled
3281 (if (not c-in-literal-cache
)
3282 (setq c-in-literal-cache
(vector (point) rtn
)))
3285 ;; XEmacs has a built-in function that should make this much quicker.
3286 ;; I don't think we even need the cache, which makes our lives more
3287 ;; complicated anyway. In this case, lim is only used to detect
3290 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
3291 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
3292 ;; Xemacs 21.4.4. It manifested itself thus:
3294 ;; Starting with an empty AWK Mode buffer, type
3296 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
3298 ;; AWK Mode is designed such that when the first / is typed, it gets the
3299 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
3300 ;; are given the s-t property "string". However, buffer-syntactic-context
3301 ;; fails to take account of the change of the s-t property on the opening / to
3302 ;; "string", and reports that the { is within a string started by the second /.
3304 ;; The workaround for this is for the AWK Mode initialisation to switch the
3305 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
3306 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
3308 ;; (Alan Mackenzie, 2003/4/30).
3310 (defun c-fast-in-literal (&optional lim detect-cpp
)
3311 ;; This function might do hidden buffer changes.
3312 (let ((context (buffer-syntactic-context)))
3314 ((eq context
'string
) 'string
)
3315 ((eq context
'comment
) 'c
++)
3316 ((eq context
'block-comment
) 'c
)
3317 ((and detect-cpp
(save-excursion (c-beginning-of-macro lim
))) 'pound
))))
3319 (defalias 'c-in-literal
3320 (if (fboundp 'buffer-syntactic-context
)
3321 'c-fast-in-literal
; XEmacs
3322 'c-slow-in-literal
)) ; GNU Emacs
3324 ;; The defalias above isn't enough to shut up the byte compiler.
3325 (cc-bytecomp-defun c-in-literal)
3327 (defun c-literal-limits (&optional lim near not-in-delimiter
)
3328 "Return a cons of the beginning and end positions of the comment or
3329 string surrounding point (including both delimiters), or nil if point
3330 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
3331 to start parsing from. If NEAR is non-nil, then the limits of any
3332 literal next to point is returned. \"Next to\" means there's only
3333 spaces and tabs between point and the literal. The search for such a
3334 literal is done first in forward direction. If NOT-IN-DELIMITER is
3335 non-nil, the case when point is inside a starting delimiter won't be
3336 recognized. This only has effect for comments which have starting
3337 delimiters with more than one character.
3339 Note that this function might do hidden buffer changes. See the
3340 comment at the start of cc-engine.el for more info."
3343 (let* ((pos (point))
3345 (c-beginning-of-syntax)
3347 (state (parse-partial-sexp lim pos
)))
3349 (cond ((elt state
3) ; String.
3350 (goto-char (elt state
8))
3351 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3354 ((elt state
4) ; Comment.
3355 (goto-char (elt state
8))
3356 (cons (point) (progn (c-forward-single-comment) (point))))
3358 ((and (not not-in-delimiter
)
3360 (eq (char-before) ?
/)
3361 (looking-at "[/*]"))
3362 ;; We're standing in a comment starter.
3364 (cons (point) (progn (c-forward-single-comment) (point))))
3369 ;; Search forward for a literal.
3370 (skip-chars-forward " \t")
3373 ((looking-at c-string-limit-regexp
) ; String.
3374 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3377 ((looking-at c-comment-start-regexp
) ; Line or block comment.
3378 (cons (point) (progn (c-forward-single-comment) (point))))
3382 (skip-chars-backward " \t")
3384 (let ((end (point)) beg
)
3387 (< (skip-syntax-backward c-string-syntax
) 0)) ; String.
3388 (setq beg
(c-safe (c-backward-sexp 1) (point))))
3390 ((and (c-safe (forward-char -
2) t
)
3392 ;; Block comment. Due to the nature of line
3393 ;; comments, they will always be covered by the
3394 ;; normal case above.
3396 (c-backward-single-comment)
3397 ;; If LIM is bogus, beg will be bogus.
3398 (setq beg
(point))))
3400 (if beg
(cons beg end
))))))
3403 ;; In case external callers use this; it did have a docstring.
3404 (defalias 'c-literal-limits-fast
'c-literal-limits
)
3406 (defun c-collect-line-comments (range)
3407 "If the argument is a cons of two buffer positions (such as returned by
3408 `c-literal-limits'), and that range contains a C++ style line comment,
3409 then an extended range is returned that contains all adjacent line
3410 comments (i.e. all comments that starts in the same column with no
3411 empty lines or non-whitespace characters between them). Otherwise the
3412 argument is returned.
3414 Note that this function might do hidden buffer changes. See the
3415 comment at the start of cc-engine.el for more info."
3419 (if (and (consp range
) (progn
3420 (goto-char (car range
))
3421 (looking-at c-line-comment-starter
)))
3422 (let ((col (current-column))
3424 (bopl (c-point 'bopl
))
3426 ;; Got to take care in the backward direction to handle
3427 ;; comments which are preceded by code.
3428 (while (and (c-backward-single-comment)
3430 (looking-at c-line-comment-starter
)
3431 (= col
(current-column)))
3433 bopl
(c-point 'bopl
)))
3435 (while (and (progn (skip-chars-forward " \t")
3436 (looking-at c-line-comment-starter
))
3437 (= col
(current-column))
3438 (prog1 (zerop (forward-line 1))
3439 (setq end
(point)))))
3444 (defun c-literal-type (range)
3445 "Convenience function that given the result of `c-literal-limits',
3446 returns nil or the type of literal that the range surrounds, one
3447 of the symbols 'c, 'c++ or 'string. It's much faster than using
3448 `c-in-literal' and is intended to be used when you need both the
3449 type of a literal and its limits.
3451 Note that this function might do hidden buffer changes. See the
3452 comment at the start of cc-engine.el for more info."
3456 (goto-char (car range
))
3457 (cond ((looking-at c-string-limit-regexp
) 'string
)
3458 ((or (looking-at "//") ; c++ line comment
3459 (and (looking-at "\\s<") ; comment starter
3460 (looking-at "#"))) ; awk comment.
3462 (t 'c
))) ; Assuming the range is valid.
3466 ;; `c-find-decl-spots' and accompanying stuff.
3468 ;; Variables used in `c-find-decl-spots' to cache the search done for
3469 ;; the first declaration in the last call. When that function starts,
3470 ;; it needs to back up over syntactic whitespace to look at the last
3471 ;; token before the region being searched. That can sometimes cause
3472 ;; moves back and forth over a quite large region of comments and
3473 ;; macros, which would be repeated for each changed character when
3474 ;; we're called during fontification, since font-lock refontifies the
3475 ;; current line for each change. Thus it's worthwhile to cache the
3478 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3479 ;; the syntactic whitespace less or equal to some start position.
3480 ;; There's no cached value if it's nil.
3482 ;; `c-find-decl-match-pos' is the match position if
3483 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3484 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3485 (defvar c-find-decl-syntactic-pos nil
)
3486 (make-variable-buffer-local 'c-find-decl-syntactic-pos
)
3487 (defvar c-find-decl-match-pos nil
)
3488 (make-variable-buffer-local 'c-find-decl-match-pos
)
3490 (defsubst c-invalidate-find-decl-cache
(change-min-pos)
3491 (and c-find-decl-syntactic-pos
3492 (< change-min-pos c-find-decl-syntactic-pos
)
3493 (setq c-find-decl-syntactic-pos nil
)))
3495 ; (defface c-debug-decl-spot-face
3496 ; '((t (:background "Turquoise")))
3497 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3498 ; (defface c-debug-decl-sws-face
3499 ; '((t (:background "Khaki")))
3500 ; "Debug face to mark the syntactic whitespace between the declaration
3501 ; spots and the preceding token end.")
3503 (defmacro c-debug-put-decl-spot-faces
(match-pos decl-pos
)
3504 (when (facep 'c-debug-decl-spot-face
)
3505 `(c-save-buffer-state ((match-pos ,match-pos
) (decl-pos ,decl-pos
))
3506 (c-debug-add-face (max match-pos
(point-min)) decl-pos
3507 'c-debug-decl-sws-face
)
3508 (c-debug-add-face decl-pos
(min (1+ decl-pos
) (point-max))
3509 'c-debug-decl-spot-face
))))
3510 (defmacro c-debug-remove-decl-spot-faces
(beg end
)
3511 (when (facep 'c-debug-decl-spot-face
)
3512 `(c-save-buffer-state ()
3513 (c-debug-remove-face ,beg
,end
'c-debug-decl-spot-face
)
3514 (c-debug-remove-face ,beg
,end
'c-debug-decl-sws-face
))))
3516 (defmacro c-find-decl-prefix-search
()
3517 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3518 ;; but it contains lots of free variables that refer to things
3519 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3520 ;; if there is a match, otherwise at `cfd-limit'.
3522 ;; This macro might do hidden buffer changes.
3525 ;; Find the next property match position if we haven't got one already.
3526 (unless cfd-prop-match
3529 (goto-char (next-single-property-change
3530 (point) 'c-type nil cfd-limit
))
3531 (and (< (point) cfd-limit
)
3532 (not (eq (c-get-char-property (1- (point)) 'c-type
)
3534 (setq cfd-prop-match
(point))))
3536 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
3538 (unless cfd-re-match
3540 (if (> cfd-re-match-end
(point))
3541 (goto-char cfd-re-match-end
))
3543 (while (if (setq cfd-re-match-end
3544 (re-search-forward c-decl-prefix-or-start-re
3547 ;; Match. Check if it's inside a comment or string literal.
3549 (if (setq cfd-re-match
(match-end 1))
3550 ;; Matched the end of a token preceding a decl spot.
3552 (goto-char cfd-re-match
)
3554 ;; Matched a token that start a decl spot.
3555 (goto-char (match-beginning 0))
3559 ;; No match. Finish up and exit the loop.
3560 (setq cfd-re-match cfd-limit
)
3563 ;; Skip out of comments and string literals.
3565 (goto-char (next-single-property-change
3566 (point) 'face nil cfd-limit
))
3567 (and (< (point) cfd-limit
)
3568 (c-got-face-at (point) c-literal-faces
)))))
3570 ;; If we matched at the decl start, we have to back up over the
3571 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
3572 ;; any decl spots in the syntactic ws.
3573 (unless cfd-re-match
3574 (c-backward-syntactic-ws)
3575 (setq cfd-re-match
(point))))
3577 ;; Choose whichever match is closer to the start.
3578 (if (< cfd-re-match cfd-prop-match
)
3579 (setq cfd-match-pos cfd-re-match
3581 (setq cfd-match-pos cfd-prop-match
3582 cfd-prop-match nil
))
3584 (goto-char cfd-match-pos
)
3586 (when (< cfd-match-pos cfd-limit
)
3587 ;; Skip forward past comments only so we don't skip macros.
3588 (c-forward-comments)
3589 ;; Set the position to continue at. We can avoid going over
3590 ;; the comments skipped above a second time, but it's possible
3591 ;; that the comment skipping has taken us past `cfd-prop-match'
3592 ;; since the property might be used inside comments.
3593 (setq cfd-continue-pos
(if cfd-prop-match
3594 (min cfd-prop-match
(point))
3597 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun
)
3598 ;; Call CFD-FUN for each possible spot for a declaration, cast or
3599 ;; label from the point to CFD-LIMIT.
3601 ;; CFD-FUN is called with point at the start of the spot. It's
3602 ;; passed two arguments: The first is the end position of the token
3603 ;; preceding the spot, or 0 for the implicit match at bob. The
3604 ;; second is a flag that is t when the match is inside a macro. If
3605 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
3606 ;; spot, it should return non-nil to ensure that the next search
3610 ;; o The first token after bob.
3611 ;; o The first token after the end of submatch 1 in
3612 ;; `c-decl-prefix-or-start-re' when that submatch matches.
3613 ;; o The start of each `c-decl-prefix-or-start-re' match when
3614 ;; submatch 1 doesn't match.
3615 ;; o The first token after the end of each occurence of the
3616 ;; `c-type' text property with the value `c-decl-end', provided
3617 ;; `c-type-decl-end-used' is set.
3619 ;; Only a spot that match CFD-DECL-RE and whose face is in the
3620 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
3621 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
3623 ;; If the match is inside a macro then the buffer is narrowed to the
3624 ;; end of it, so that CFD-FUN can investigate the following tokens
3625 ;; without matching something that begins inside a macro and ends
3626 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3627 ;; CFD-FACE-CHECKLIST checks exist.
3629 ;; The spots are visited approximately in order from top to bottom.
3630 ;; It's however the positions where `c-decl-prefix-or-start-re'
3631 ;; matches and where `c-decl-end' properties are found that are in
3632 ;; order. Since the spots often are at the following token, they
3633 ;; might be visited out of order insofar as more spots are reported
3634 ;; later on within the syntactic whitespace between the match
3635 ;; positions and their spots.
3637 ;; It's assumed that comments and strings are fontified in the
3640 ;; This is mainly used in fontification, and so has an elaborate
3641 ;; cache to handle repeated calls from the same start position; see
3642 ;; the variables above.
3644 ;; All variables in this function begin with `cfd-' to avoid name
3645 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3647 ;; This function might do hidden buffer changes.
3649 (let ((cfd-start-pos (point))
3650 (cfd-buffer-end (point-max))
3651 ;; The end of the token preceding the decl spot last found
3652 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
3655 ;; The end position of the last `c-decl-prefix-or-start-re'
3656 ;; match. If this is greater than `cfd-continue-pos', the
3657 ;; next regexp search is started here instead.
3658 (cfd-re-match-end (point-min))
3659 ;; The end of the last `c-decl-end' found by
3660 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
3661 ;; match. If searching for the property isn't needed then we
3662 ;; disable it by setting it to `cfd-limit' directly.
3663 (cfd-prop-match (unless c-type-decl-end-used cfd-limit
))
3664 ;; The end of the token preceding the decl spot last found by
3665 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
3666 ;; bob. `cfd-limit' if there's no match. In other words,
3667 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
3668 (cfd-match-pos cfd-limit
)
3669 ;; The position to continue searching at.
3671 ;; The position of the last "real" token we've stopped at.
3672 ;; This can be greater than `cfd-continue-pos' when we get
3673 ;; hits inside macros or at `c-decl-end' positions inside
3676 ;; The end position of the last entered macro.
3679 ;; Initialize by finding a syntactically relevant start position
3680 ;; before the point, and do the first `c-decl-prefix-or-start-re'
3681 ;; search unless we're at bob.
3683 (let (start-in-literal start-in-macro syntactic-pos
)
3684 ;; Must back up a bit since we look for the end of the previous
3685 ;; statement or declaration, which is earlier than the first
3689 ;; First we need to move to a syntactically relevant position.
3690 ;; Begin by backing out of comment or string literals.
3692 (when (c-got-face-at (point) c-literal-faces
)
3693 ;; Try to use the faces to back up to the start of the
3694 ;; literal. FIXME: What if the point is on a declaration
3695 ;; inside a comment?
3696 (while (and (not (bobp))
3697 (c-got-face-at (1- (point)) c-literal-faces
))
3698 (goto-char (previous-single-property-change
3699 (point) 'face nil
(point-min))))
3701 ;; XEmacs doesn't fontify the quotes surrounding string
3703 (and (featurep 'xemacs
)
3704 (eq (get-text-property (point) 'face
)
3705 'font-lock-string-face
)
3707 (progn (backward-char)
3708 (not (looking-at c-string-limit-regexp
)))
3711 ;; Don't trust the literal to contain only literal faces
3712 ;; (the font lock package might not have fontified the
3713 ;; start of it at all, for instance) so check that we have
3714 ;; arrived at something that looks like a start or else
3715 ;; resort to `c-literal-limits'.
3716 (unless (looking-at c-literal-start-regexp
)
3717 (let ((range (c-literal-limits)))
3718 (if range
(goto-char (car range
)))))
3720 (setq start-in-literal
(point)))
3722 ;; The start is in a literal. If the limit is in the same
3723 ;; one we don't have to find a syntactic position etc. We
3724 ;; only check that if the limit is at or before bonl to save
3725 ;; time; it covers the by far most common case when font-lock
3726 ;; refontifies the current line only.
3727 (<= cfd-limit
(c-point 'bonl cfd-start-pos
))
3729 (goto-char cfd-start-pos
)
3731 (goto-char (next-single-property-change
3732 (point) 'face nil cfd-limit
))
3733 (and (< (point) cfd-limit
)
3734 (c-got-face-at (point) c-literal-faces
))))
3735 (= (point) cfd-limit
)))
3737 ;; Completely inside a literal. Set up variables to trig the
3738 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
3739 ;; find a suitable start position.
3740 (setq cfd-continue-pos start-in-literal
))
3742 ;; Check if the region might be completely inside a macro, to
3743 ;; optimize that like the completely-inside-literal above.
3745 (and (= (forward-line 1) 0)
3746 (bolp) ; forward-line has funny behavior at eob.
3747 (>= (point) cfd-limit
)
3748 (progn (backward-char)
3749 (eq (char-before) ?
\\))))
3750 ;; (Maybe) completely inside a macro. Only need to trig the
3751 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
3753 (setq cfd-continue-pos
(1- cfd-start-pos
)
3757 ;; Back out of any macro so we don't miss any declaration
3758 ;; that could follow after it.
3759 (when (c-beginning-of-macro)
3760 (setq start-in-macro t
))
3762 ;; Now we're at a proper syntactically relevant position so we
3763 ;; can use the cache. But first clear it if it applied
3765 (c-invalidate-find-decl-cache cfd-start-pos
)
3767 (setq syntactic-pos
(point))
3768 (unless (eq syntactic-pos c-find-decl-syntactic-pos
)
3769 ;; Don't have to do this if the cache is relevant here,
3770 ;; typically if the same line is refontified again. If
3771 ;; we're just some syntactic whitespace further down we can
3772 ;; still use the cache to limit the skipping.
3773 (c-backward-syntactic-ws c-find-decl-syntactic-pos
))
3775 ;; If we hit `c-find-decl-syntactic-pos' and
3776 ;; `c-find-decl-match-pos' is set then we install the cached
3777 ;; values. If we hit `c-find-decl-syntactic-pos' and
3778 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3779 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3780 ;; and so we can continue the search from this point. If we
3781 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
3782 ;; the right spot to begin searching anyway.
3783 (if (and (eq (point) c-find-decl-syntactic-pos
)
3784 c-find-decl-match-pos
)
3785 (setq cfd-match-pos c-find-decl-match-pos
3786 cfd-continue-pos syntactic-pos
)
3788 (setq c-find-decl-syntactic-pos syntactic-pos
)
3791 ;; Always consider bob a match to get the first
3792 ;; declaration in the file. Do this separately instead of
3793 ;; letting `c-decl-prefix-or-start-re' match bob, so that
3794 ;; regexp always can consume at least one character to
3795 ;; ensure that we won't get stuck in an infinite loop.
3796 (setq cfd-re-match
0)
3798 (c-beginning-of-current-token)
3799 (< (point) cfd-limit
))
3800 ;; Do an initial search now. In the bob case above it's
3801 ;; only done to search for a `c-decl-end' spot.
3802 (c-find-decl-prefix-search))
3804 (setq c-find-decl-match-pos
(and (< cfd-match-pos cfd-start-pos
)
3807 ;; Advance `cfd-continue-pos' if it's before the start position.
3808 ;; The closest continue position that might have effect at or
3809 ;; after the start depends on what we started in. This also
3810 ;; finds a suitable start position in the special cases when the
3811 ;; region is completely within a literal or macro.
3812 (when (and cfd-continue-pos
(< cfd-continue-pos cfd-start-pos
))
3816 ;; If we're in a macro then it's the closest preceding token
3817 ;; in the macro. Check this before `start-in-literal',
3818 ;; since if we're inside a literal in a macro, the preceding
3819 ;; token is earlier than any `c-decl-end' spot inside the
3820 ;; literal (comment).
3821 (goto-char (or start-in-literal cfd-start-pos
))
3822 ;; The only syntactic ws in macros are comments.
3823 (c-backward-comments)
3825 (c-beginning-of-current-token))
3828 ;; If we're in a comment it can only be the closest
3829 ;; preceding `c-decl-end' position within that comment, if
3830 ;; any. Go back to the beginning of such a property so that
3831 ;; `c-find-decl-prefix-search' will find the end of it.
3832 ;; (Can't stop at the end and install it directly on
3833 ;; `cfd-prop-match' since that variable might be cleared
3834 ;; after `cfd-fun' below.)
3836 ;; Note that if the literal is a string then the property
3837 ;; search will simply skip to the beginning of it right
3839 (if (not c-type-decl-end-used
)
3840 (goto-char start-in-literal
)
3841 (goto-char cfd-start-pos
)
3843 (goto-char (previous-single-property-change
3844 (point) 'c-type nil start-in-literal
))
3845 (and (> (point) start-in-literal
)
3846 (not (eq (c-get-char-property (point) 'c-type
)
3849 (when (= (point) start-in-literal
)
3850 ;; Didn't find any property inside the comment, so we can
3851 ;; skip it entirely. (This won't skip past a string, but
3852 ;; that'll be handled quickly by the next
3853 ;; `c-find-decl-prefix-search' anyway.)
3854 (c-forward-single-comment)
3855 (if (> (point) cfd-limit
)
3856 (goto-char cfd-limit
))))
3859 ;; If we started in normal code, the only match that might
3860 ;; apply before the start is what we already got in
3861 ;; `cfd-match-pos' so we can continue at the start position.
3862 ;; (Note that we don't get here if the first match is below
3864 (goto-char cfd-start-pos
)))
3866 ;; Delete found matches if they are before our new continue
3867 ;; position, so that `c-find-decl-prefix-search' won't back up
3868 ;; to them later on.
3869 (setq cfd-continue-pos
(point))
3870 (when (and cfd-re-match
(< cfd-re-match cfd-continue-pos
))
3871 (setq cfd-re-match nil
))
3872 (when (and cfd-prop-match
(< cfd-prop-match cfd-continue-pos
))
3873 (setq cfd-prop-match nil
)))
3876 ;; This is the normal case and we got a proper syntactic
3877 ;; position. If there's a match then it's always outside
3878 ;; macros and comments, so advance to the next token and set
3879 ;; `cfd-token-pos'. The loop below will later go back using
3880 ;; `cfd-continue-pos' to fix declarations inside the
3882 (when (and cfd-match-pos
(< cfd-match-pos syntactic-pos
))
3883 (goto-char syntactic-pos
)
3884 (c-forward-syntactic-ws)
3885 (and cfd-continue-pos
3886 (< cfd-continue-pos
(point))
3887 (setq cfd-token-pos
(point))))
3889 ;; Have one of the special cases when the region is completely
3890 ;; within a literal or macro. `cfd-continue-pos' is set to a
3891 ;; good start position for the search, so do it.
3892 (c-find-decl-prefix-search)))
3894 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
3898 (< cfd-match-pos cfd-limit
)
3901 ;; Kludge to filter out matches on the "<" that
3902 ;; aren't open parens, for the sake of languages
3903 ;; that got `c-recognize-<>-arglists' set.
3904 (and (eq (char-before cfd-match-pos
) ?
<)
3905 (not (c-get-char-property (1- cfd-match-pos
)
3908 ;; If `cfd-continue-pos' is less or equal to
3909 ;; `cfd-token-pos', we've got a hit inside a macro
3910 ;; that's in the syntactic whitespace before the last
3911 ;; "real" declaration we've checked. If they're equal
3912 ;; we've arrived at the declaration a second time, so
3913 ;; there's nothing to do.
3914 (= cfd-continue-pos cfd-token-pos
)
3917 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3918 ;; we're still searching for declarations embedded in
3919 ;; the syntactic whitespace. In that case we need
3920 ;; only to skip comments and not macros, since they
3921 ;; can't be nested, and that's already been done in
3922 ;; `c-find-decl-prefix-search'.
3923 (when (> cfd-continue-pos cfd-token-pos
)
3924 (c-forward-syntactic-ws)
3925 (setq cfd-token-pos
(point)))
3927 ;; Continue if the following token fails the
3928 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3929 (when (or (>= (point) cfd-limit
)
3930 (not (looking-at cfd-decl-re
))
3931 (and cfd-face-checklist
3933 (point) cfd-face-checklist
))))
3934 (goto-char cfd-continue-pos
)
3937 (< (point) cfd-limit
))
3938 (c-find-decl-prefix-search))
3940 (< (point) cfd-limit
))
3943 (>= (point) cfd-start-pos
)
3946 ;; Narrow to the end of the macro if we got a hit inside
3947 ;; one, to avoid recognizing things that start inside the
3948 ;; macro and end outside it.
3949 (when (> cfd-match-pos cfd-macro-end
)
3950 ;; Not in the same macro as in the previous round.
3952 (goto-char cfd-match-pos
)
3954 (if (save-excursion (and (c-beginning-of-macro)
3955 (< (point) cfd-match-pos
)))
3956 (progn (c-end-of-macro)
3960 (if (zerop cfd-macro-end
)
3962 (if (> cfd-macro-end
(point))
3963 (progn (narrow-to-region (point-min) cfd-macro-end
)
3965 ;; The matched token was the last thing in the macro,
3966 ;; so the whole match is bogus.
3967 (setq cfd-macro-end
0)
3970 (c-debug-put-decl-spot-faces cfd-match-pos
(point))
3971 (if (funcall cfd-fun cfd-match-pos
(/= cfd-macro-end
0))
3972 (setq cfd-prop-match nil
))
3974 (when (/= cfd-macro-end
0)
3975 ;; Restore limits if we did macro narrowment above.
3976 (narrow-to-region (point-min) cfd-buffer-end
)))
3978 (goto-char cfd-continue-pos
)
3979 (if (= cfd-continue-pos cfd-limit
)
3980 (setq cfd-match-pos cfd-limit
)
3981 (c-find-decl-prefix-search)))))
3984 ;; A cache for found types.
3986 ;; Buffer local variable that contains an obarray with the types we've
3987 ;; found. If a declaration is recognized somewhere we record the
3988 ;; fully qualified identifier in it to recognize it as a type
3989 ;; elsewhere in the file too. This is not accurate since we do not
3990 ;; bother with the scoping rules of the languages, but in practice the
3991 ;; same name is seldom used as both a type and something else in a
3992 ;; file, and we only use this as a last resort in ambiguous cases (see
3993 ;; `c-forward-decl-or-cast-1').
3995 ;; Not every type need be in this cache. However, things which have
3996 ;; ceased to be types must be removed from it.
3998 ;; Template types in C++ are added here too but with the template
3999 ;; arglist replaced with "<>" in references or "<" for the one in the
4000 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4001 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4002 ;; template specs can be fairly sized programs in themselves) and
4003 ;; improves the hit ratio (it's a type regardless of the template
4004 ;; args; it's just not the same type, but we're only interested in
4005 ;; recognizing types, not telling distinct types apart). Note that
4006 ;; template types in references are added here too; from the example
4007 ;; above there will also be an entry "Foo<".
4008 (defvar c-found-types nil
)
4009 (make-variable-buffer-local 'c-found-types
)
4011 (defsubst c-clear-found-types
()
4012 ;; Clears `c-found-types'.
4013 (setq c-found-types
(make-vector 53 0)))
4015 (defun c-add-type (from to
)
4016 ;; Add the given region as a type in `c-found-types'. If the region
4017 ;; doesn't match an existing type but there is a type which is equal
4018 ;; to the given one except that the last character is missing, then
4019 ;; the shorter type is removed. That's done to avoid adding all
4020 ;; prefixes of a type as it's being entered and font locked. This
4021 ;; doesn't cover cases like when characters are removed from a type
4022 ;; or added in the middle. We'd need the position of point when the
4023 ;; font locking is invoked to solve this well.
4025 ;; This function might do hidden buffer changes.
4026 (let ((type (c-syntactic-content from to c-recognize-
<>-arglists
)))
4027 (unless (intern-soft type c-found-types
)
4028 (unintern (substring type
0 -
1) c-found-types
)
4029 (intern type c-found-types
))))
4031 (defun c-unfind-type (name)
4032 ;; Remove the "NAME" from c-found-types, if present.
4033 (unintern name c-found-types
))
4035 (defsubst c-check-type
(from to
)
4036 ;; Return non-nil if the given region contains a type in
4039 ;; This function might do hidden buffer changes.
4040 (intern-soft (c-syntactic-content from to c-recognize-
<>-arglists
)
4043 (defun c-list-found-types ()
4044 ;; Return all the types in `c-found-types' as a sorted list of
4047 (mapatoms (lambda (type)
4048 (setq type-list
(cons (symbol-name type
)
4051 (sort type-list
'string-lessp
)))
4053 ;; Shut up the byte compiler.
4054 (defvar c-maybe-stale-found-type
)
4056 (defun c-trim-found-types (beg end old-len
)
4057 ;; An after change function which, in conjunction with the info in
4058 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4059 ;; from `c-found-types', should this type have become stale. For
4060 ;; example, this happens to "foo" when "foo \n bar();" becomes
4061 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4062 ;; the fontification.
4064 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4068 (when (< end
(point-max))
4070 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4071 (progn (goto-char end
)
4072 (c-end-of-current-token)))
4073 (c-unfind-type (buffer-substring-no-properties
4075 (when (> beg
(point-min))
4077 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4078 (progn (goto-char beg
)
4079 (c-beginning-of-current-token)))
4080 (c-unfind-type (buffer-substring-no-properties
4083 (if c-maybe-stale-found-type
; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4085 ;; Changing the amount of (already existing) whitespace - don't do anything.
4086 ((and (c-partial-ws-p beg end
)
4087 (or (= beg end
) ; removal of WS
4088 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type
)))))
4090 ;; The syntactic relationship which defined a "found type" has been
4092 ((eq (car c-maybe-stale-found-type
) 'c-decl-id-start
)
4093 (c-unfind-type (cadr c-maybe-stale-found-type
)))
4094 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4098 ;; Handling of small scale constructs like types and names.
4100 (defun c-after-change-check-<>-operators
(beg end
)
4101 ;; This is called from `after-change-functions' when
4102 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
4103 ;; chars with paren syntax become part of another operator like "<<"
4106 ;; This function might do hidden buffer changes.
4110 (when (or (looking-at "[<>]")
4111 (< (skip-chars-backward "<>") 0))
4114 (c-beginning-of-current-token)
4115 (when (and (< (point) beg
)
4116 (looking-at c-
<>-multichar-token-regexp
)
4117 (< beg
(setq beg
(match-end 0))))
4118 (while (progn (skip-chars-forward "^<>" beg
)
4120 (c-clear-char-property (point) 'syntax-table
)
4125 (when (or (looking-at "[<>]")
4126 (< (skip-chars-backward "<>") 0))
4129 (c-beginning-of-current-token)
4130 (when (and (< (point) end
)
4131 (looking-at c-
<>-multichar-token-regexp
)
4132 (< end
(setq end
(match-end 0))))
4133 (while (progn (skip-chars-forward "^<>" end
)
4135 (c-clear-char-property (point) 'syntax-table
)
4136 (forward-char)))))))
4138 ;; Dynamically bound variable that instructs `c-forward-type' to also
4139 ;; treat possible types (i.e. those that it normally returns 'maybe or
4140 ;; 'found for) as actual types (and always return 'found for them).
4141 ;; This means that it records them in `c-record-type-identifiers' if
4142 ;; that is set, and that it adds them to `c-found-types'.
4143 (defvar c-promote-possible-types nil
)
4145 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4146 ;; mark up successfully parsed arglists with paren syntax properties on
4147 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
4148 ;; `c-type' property of each argument separating comma.
4150 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
4151 ;; all arglists for side effects (i.e. recording types), otherwise it
4152 ;; exploits any existing paren syntax properties to quickly jump to the
4153 ;; end of already parsed arglists.
4155 ;; Marking up the arglists is not the default since doing that correctly
4156 ;; depends on a proper value for `c-restricted-<>-arglists'.
4157 (defvar c-parse-and-markup-
<>-arglists nil
)
4159 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4160 ;; not accept arglists that contain binary operators.
4162 ;; This is primarily used to handle C++ template arglists. C++
4163 ;; disambiguates them by checking whether the preceding name is a
4164 ;; template or not. We can't do that, so we assume it is a template
4165 ;; if it can be parsed as one. That usually works well since
4166 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
4167 ;; in almost all cases would be pointless.
4169 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
4170 ;; should let the comma separate the function arguments instead. And
4171 ;; in a context where the value of the expression is taken, e.g. in
4172 ;; "if (a < b || c > d)", it's probably not a template.
4173 (defvar c-restricted-
<>-arglists nil
)
4175 ;; Dynamically bound variables that instructs
4176 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
4177 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
4178 ;; `c-forward-label' to record the ranges of all the type and
4179 ;; reference identifiers they encounter. They will build lists on
4180 ;; these variables where each element is a cons of the buffer
4181 ;; positions surrounding each identifier. This recording is only
4182 ;; activated when `c-record-type-identifiers' is non-nil.
4184 ;; All known types that can't be identifiers are recorded, and also
4185 ;; other possible types if `c-promote-possible-types' is set.
4186 ;; Recording is however disabled inside angle bracket arglists that
4187 ;; are encountered inside names and other angle bracket arglists.
4188 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
4191 ;; Only the names in C++ template style references (e.g. "tmpl" in
4192 ;; "tmpl<a,b>::foo") are recorded as references, other references
4193 ;; aren't handled here.
4195 ;; `c-forward-label' records the label identifier(s) on
4196 ;; `c-record-ref-identifiers'.
4197 (defvar c-record-type-identifiers nil
)
4198 (defvar c-record-ref-identifiers nil
)
4200 ;; This variable will receive a cons cell of the range of the last
4201 ;; single identifier symbol stepped over by `c-forward-name' if it's
4202 ;; successful. This is the range that should be put on one of the
4203 ;; record lists above by the caller. It's assigned nil if there's no
4204 ;; such symbol in the name.
4205 (defvar c-last-identifier-range nil
)
4207 (defmacro c-record-type-id
(range)
4208 (if (eq (car-safe range
) 'cons
)
4210 `(setq c-record-type-identifiers
4211 (cons ,range c-record-type-identifiers
))
4212 `(let ((range ,range
))
4214 (setq c-record-type-identifiers
4215 (cons range c-record-type-identifiers
))))))
4217 (defmacro c-record-ref-id
(range)
4218 (if (eq (car-safe range
) 'cons
)
4220 `(setq c-record-ref-identifiers
4221 (cons ,range c-record-ref-identifiers
))
4222 `(let ((range ,range
))
4224 (setq c-record-ref-identifiers
4225 (cons range c-record-ref-identifiers
))))))
4227 ;; Dynamically bound variable that instructs `c-forward-type' to
4228 ;; record the ranges of types that only are found. Behaves otherwise
4229 ;; like `c-record-type-identifiers'.
4230 (defvar c-record-found-types nil
)
4232 (defmacro c-forward-keyword-prefixed-id
(type)
4233 ;; Used internally in `c-forward-keyword-clause' to move forward
4234 ;; over a type (if TYPE is 'type) or a name (otherwise) which
4235 ;; possibly is prefixed by keywords and their associated clauses.
4236 ;; Try with a type/name first to not trip up on those that begin
4237 ;; with a keyword. Return t if a known or found type is moved
4238 ;; over. The point is clobbered if nil is returned. If range
4239 ;; recording is enabled, the identifier is recorded on as a type
4240 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
4242 ;; This macro might do hidden buffer changes.
4244 (while (if (setq res
,(if (eq type
'type
)
4248 (and (looking-at c-keywords-regexp
)
4249 (c-forward-keyword-clause 1))))
4250 (when (memq res
'(t known found prefix
))
4251 ,(when (eq type
'ref
)
4252 `(when c-record-type-identifiers
4253 (c-record-ref-id c-last-identifier-range
)))
4256 (defmacro c-forward-id-comma-list
(type update-safe-pos
)
4257 ;; Used internally in `c-forward-keyword-clause' to move forward
4258 ;; over a comma separated list of types or names using
4259 ;; `c-forward-keyword-prefixed-id'.
4261 ;; This macro might do hidden buffer changes.
4263 ,(when update-safe-pos
4264 `(setq safe-pos
(point)))
4265 (eq (char-after) ?
,))
4268 (c-forward-syntactic-ws)
4269 (c-forward-keyword-prefixed-id ,type
)))))
4271 (defun c-forward-keyword-clause (match)
4272 ;; Submatch MATCH in the current match data is assumed to surround a
4273 ;; token. If it's a keyword, move over it and any immediately
4274 ;; following clauses associated with it, stopping at the start of
4275 ;; the next token. t is returned in that case, otherwise the point
4276 ;; stays and nil is returned. The kind of clauses that are
4277 ;; recognized are those specified by `c-type-list-kwds',
4278 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
4279 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
4280 ;; and `c-<>-arglist-kwds'.
4282 ;; This function records identifier ranges on
4283 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4284 ;; `c-record-type-identifiers' is non-nil.
4286 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
4287 ;; apply directly after the keyword, the type list is moved over
4288 ;; only when there is no unaccounted token before it (i.e. a token
4289 ;; that isn't moved over due to some other keyword list). The
4290 ;; identifier ranges in the list are still recorded if that should
4293 ;; This function might do hidden buffer changes.
4295 (let ((kwd-sym (c-keyword-sym (match-string match
))) safe-pos pos
4296 ;; The call to `c-forward-<>-arglist' below is made after
4297 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
4298 ;; are angle bracket arglists and `c-restricted-<>-arglists'
4299 ;; should therefore be nil.
4300 (c-parse-and-markup-<>-arglists t
)
4301 c-restricted-
<>-arglists
)
4304 (goto-char (match-end match
))
4305 (c-forward-syntactic-ws)
4306 (setq safe-pos
(point))
4309 ((and (c-keyword-member kwd-sym
'c-type-list-kwds
)
4310 (c-forward-keyword-prefixed-id type
))
4311 ;; There's a type directly after a keyword in `c-type-list-kwds'.
4312 (c-forward-id-comma-list type t
))
4314 ((and (c-keyword-member kwd-sym
'c-ref-list-kwds
)
4315 (c-forward-keyword-prefixed-id ref
))
4316 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
4317 (c-forward-id-comma-list ref t
))
4319 ((and (c-keyword-member kwd-sym
'c-paren-any-kwds
)
4320 (eq (char-after) ?\
())
4321 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
4324 (when (and (setq pos
(c-up-list-forward))
4325 (eq (char-before pos
) ?\
)))
4326 (when (and c-record-type-identifiers
4327 (c-keyword-member kwd-sym
'c-paren-type-kwds
))
4328 ;; Use `c-forward-type' on every identifier we can find
4329 ;; inside the paren, to record the types.
4330 (while (c-syntactic-re-search-forward c-symbol-start pos t
)
4331 (goto-char (match-beginning 0))
4332 (unless (c-forward-type)
4333 (looking-at c-symbol-key
) ; Always matches.
4334 (goto-char (match-end 0)))))
4337 (c-forward-syntactic-ws)
4338 (setq safe-pos
(point))))
4340 ((and (c-keyword-member kwd-sym
'c-
<>-sexp-kwds
)
4341 (eq (char-after) ?
<)
4342 (c-forward-<>-arglist
(c-keyword-member kwd-sym
'c-
<>-type-kwds
)))
4343 (c-forward-syntactic-ws)
4344 (setq safe-pos
(point)))
4346 ((and (c-keyword-member kwd-sym
'c-nonsymbol-sexp-kwds
)
4347 (not (looking-at c-symbol-start
))
4348 (c-safe (c-forward-sexp) t
))
4349 (c-forward-syntactic-ws)
4350 (setq safe-pos
(point))))
4352 (when (c-keyword-member kwd-sym
'c-colon-type-list-kwds
)
4353 (if (eq (char-after) ?
:)
4354 ;; If we are at the colon already, we move over the type
4358 (c-forward-syntactic-ws)
4359 (when (c-forward-keyword-prefixed-id type
)
4360 (c-forward-id-comma-list type t
)))
4361 ;; Not at the colon, so stop here. But the identifier
4362 ;; ranges in the type list later on should still be
4364 (and c-record-type-identifiers
4366 ;; If a keyword matched both one of the types above and
4367 ;; this one, we match `c-colon-type-list-re' after the
4368 ;; clause matched above.
4369 (goto-char safe-pos
)
4370 (looking-at c-colon-type-list-re
))
4372 (goto-char (match-end 0))
4373 (c-forward-syntactic-ws)
4374 (c-forward-keyword-prefixed-id type
))
4375 ;; There's a type after the `c-colon-type-list-re' match
4376 ;; after a keyword in `c-colon-type-list-kwds'.
4377 (c-forward-id-comma-list type nil
))))
4379 (goto-char safe-pos
)
4382 (defun c-forward-<>-arglist
(all-types)
4383 ;; The point is assumed to be at a "<". Try to treat it as the open
4384 ;; paren of an angle bracket arglist and move forward to the the
4385 ;; corresponding ">". If successful, the point is left after the
4386 ;; ">" and t is returned, otherwise the point isn't moved and nil is
4387 ;; returned. If ALL-TYPES is t then all encountered arguments in
4388 ;; the arglist that might be types are treated as found types.
4390 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
4391 ;; function handles text properties on the angle brackets and argument
4392 ;; separating commas.
4394 ;; `c-restricted-<>-arglists' controls how lenient the template
4395 ;; arglist recognition should be.
4397 ;; This function records identifier ranges on
4398 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4399 ;; `c-record-type-identifiers' is non-nil.
4401 ;; This function might do hidden buffer changes.
4403 (let ((start (point))
4404 ;; If `c-record-type-identifiers' is set then activate
4405 ;; recording of any found types that constitute an argument in
4407 (c-record-found-types (if c-record-type-identifiers t
)))
4408 (if (catch 'angle-bracket-arglist-escape
4409 (setq c-record-found-types
4410 (c-forward-<>-arglist-recur all-types
)))
4412 (when (consp c-record-found-types
)
4413 (setq c-record-type-identifiers
4414 ;; `nconc' doesn't mind that the tail of
4415 ;; `c-record-found-types' is t.
4416 (nconc c-record-found-types c-record-type-identifiers
)))
4422 (defun c-forward-<>-arglist-recur
(all-types)
4423 ;; Recursive part of `c-forward-<>-arglist'.
4425 ;; This function might do hidden buffer changes.
4427 (let ((start (point)) res pos tmp
4428 ;; Cover this so that any recorded found type ranges are
4429 ;; automatically lost if it turns out to not be an angle
4430 ;; bracket arglist. It's propagated through the return value
4431 ;; on successful completion.
4432 (c-record-found-types c-record-found-types
)
4433 ;; List that collects the positions after the argument
4434 ;; separating ',' in the arglist.
4437 ;; If the '<' has paren open syntax then we've marked it as an angle
4438 ;; bracket arglist before, so skip to the end.
4439 (if (and (not c-parse-and-markup-
<>-arglists
)
4440 (c-get-char-property (point) 'syntax-table
))
4444 (if (and (c-go-up-list-forward)
4445 (eq (char-before) ?
>))
4448 ;; Got unmatched paren angle brackets. We don't clear the paren
4449 ;; syntax properties and retry, on the basis that it's very
4450 ;; unlikely that paren angle brackets become operators by code
4451 ;; manipulation. It's far more likely that it doesn't match due
4452 ;; to narrowing or some temporary change.
4457 (unless (looking-at c-
<-op-cont-regexp
)
4461 (when c-record-type-identifiers
4464 ;; All encountered identifiers are types, so set the
4465 ;; promote flag and parse the type.
4467 (c-forward-syntactic-ws)
4468 (when (looking-at c-identifier-start
)
4469 (let ((c-promote-possible-types t
))
4472 ;; Check if this arglist argument is a sole type. If
4473 ;; it's known then it's recorded in
4474 ;; `c-record-type-identifiers'. If it only is found
4475 ;; then it's recorded in `c-record-found-types' which we
4476 ;; might roll back if it turns out that this isn't an
4477 ;; angle bracket arglist afterall.
4478 (when (memq (char-before) '(?
, ?
<))
4479 (let ((orig-record-found-types c-record-found-types
))
4480 (c-forward-syntactic-ws)
4481 (and (memq (c-forward-type) '(known found
))
4482 (not (looking-at "[,>]"))
4483 ;; A found type was recorded but it's not the
4484 ;; only thing in the arglist argument, so reset
4485 ;; `c-record-found-types'.
4486 (setq c-record-found-types
4487 orig-record-found-types
))))))
4490 (or (when (eq (char-after) ?
>)
4491 ;; Must check for '>' at the very start separately,
4492 ;; since the regexp below has to avoid ">>" without
4497 ;; Note: These regexps exploit the match order in \| so
4498 ;; that "<>" is matched by "<" rather than "[^>:-]>".
4499 (c-syntactic-re-search-forward
4500 (if c-restricted-
<>-arglists
4501 ;; Stop on ',', '|', '&', '+' and '-' to catch
4502 ;; common binary operators that could be between
4503 ;; two comparison expressions "a<b" and "c>d".
4504 "[<;{},|&+-]\\|\\([^>:-]>\\)"
4505 ;; Otherwise we still stop on ',' to find the
4506 ;; argument start positions.
4507 "[<;{},]\\|\\([^>:-]>\\)")
4510 ;; If the arglist starter has lost its open paren
4511 ;; syntax but not the closer, we won't find the
4512 ;; closer above since we only search in the
4513 ;; balanced sexp. In that case we stop just short
4514 ;; of it so check if the following char is the closer.
4515 (when (eq (char-after) ?
>)
4520 ((eq (char-before) ?
>)
4521 ;; Either an operator starting with '>' or the end of
4522 ;; the angle bracket arglist.
4524 (if (looking-at c-
>-op-cont-regexp
)
4526 (goto-char (match-end 0))
4527 t
) ; Continue the loop.
4529 ;; The angle bracket arglist is finished.
4530 (when c-parse-and-markup-
<>-arglists
4531 (while arg-start-pos
4532 (c-put-c-type-property (1- (car arg-start-pos
))
4534 (setq arg-start-pos
(cdr arg-start-pos
)))
4535 (c-mark-<-as-paren start
)
4536 (c-mark->-as-paren
(1- (point))))
4538 nil
)) ; Exit the loop.
4540 ((eq (char-before) ?
<)
4541 ;; Either an operator starting with '<' or a nested arglist.
4544 (let (id-start id-end subres keyword-match
)
4545 (if (if (looking-at c-
<-op-cont-regexp
)
4546 (setq tmp
(match-end 0))
4553 ;; There's always an identifier before an angle
4554 ;; bracket arglist, or a keyword in
4555 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
4556 (c-backward-syntactic-ws)
4557 (setq id-end
(point))
4558 (c-simple-skip-symbol-backward)
4559 (when (or (setq keyword-match
4560 (looking-at c-opt-
<>-sexp-key
))
4561 (not (looking-at c-keywords-regexp
)))
4562 (setq id-start
(point))))
4565 (let ((c-record-type-identifiers nil
)
4566 (c-record-found-types nil
))
4567 (c-forward-<>-arglist-recur
4570 (c-keyword-sym (match-string 1))
4571 'c-
<>-type-kwds
)))))
4574 ;; It was not an angle bracket arglist.
4577 ;; It was an angle bracket arglist.
4578 (setq c-record-found-types subres
)
4580 ;; Record the identifier before the template as a type
4581 ;; or reference depending on whether the arglist is last
4582 ;; in a qualified identifier.
4583 (when (and c-record-type-identifiers
4584 (not keyword-match
))
4585 (if (and c-opt-identifier-concat-key
4587 (c-forward-syntactic-ws)
4588 (looking-at c-opt-identifier-concat-key
)))
4589 (c-record-ref-id (cons id-start id-end
))
4590 (c-record-type-id (cons id-start id-end
))))))
4593 ((and (eq (char-before) ?
,)
4594 (not c-restricted-
<>-arglists
))
4595 ;; Just another argument. Record the position. The
4596 ;; type check stuff that made us stop at it is at
4597 ;; the top of the loop.
4598 (setq arg-start-pos
(cons (point) arg-start-pos
)))
4601 ;; Got a character that can't be in an angle bracket
4602 ;; arglist argument. Abort using `throw', since
4603 ;; it's useless to try to find a surrounding arglist
4605 (throw 'angle-bracket-arglist-escape nil
))))))
4608 (or c-record-found-types t
)))))
4610 (defun c-backward-<>-arglist
(all-types &optional limit
)
4611 ;; The point is assumed to be directly after a ">". Try to treat it
4612 ;; as the close paren of an angle bracket arglist and move back to
4613 ;; the corresponding "<". If successful, the point is left at
4614 ;; the "<" and t is returned, otherwise the point isn't moved and
4615 ;; nil is returned. ALL-TYPES is passed on to
4616 ;; `c-forward-<>-arglist'.
4618 ;; If the optional LIMIT is given, it bounds the backward search.
4619 ;; It's then assumed to be at a syntactically relevant position.
4621 ;; This is a wrapper around `c-forward-<>-arglist'. See that
4622 ;; function for more details.
4624 (let ((start (point)))
4626 (if (and (not c-parse-and-markup-
<>-arglists
)
4627 (c-get-char-property (point) 'syntax-table
))
4629 (if (and (c-go-up-list-backward)
4630 (eq (char-after) ?
<))
4632 ;; See corresponding note in `c-forward-<>-arglist'.
4637 (c-syntactic-skip-backward "^<;{}" limit t
)
4640 (if (eq (char-before) ?
<)
4642 ;; Stopped at bob or a char that isn't allowed in an
4643 ;; arglist, so we've failed.
4648 (progn (c-beginning-of-current-token)
4650 ;; If we moved then the "<" was part of some
4651 ;; multicharacter token.
4655 (let ((beg-pos (point)))
4656 (if (c-forward-<>-arglist all-types
)
4657 (cond ((= (point) start
)
4658 ;; Matched the arglist. Break the while.
4662 ;; We started from a non-paren ">" inside an
4667 ;; Matched a shorter arglist. Can be a nested
4668 ;; one so continue looking.
4673 (/= (point) start
))))
4675 (defun c-forward-name ()
4676 ;; Move forward over a complete name if at the beginning of one,
4677 ;; stopping at the next following token. If the point is not at
4678 ;; something that are recognized as name then it stays put. A name
4679 ;; could be something as simple as "foo" in C or something as
4680 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
4681 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
4682 ;; int>::*volatile const" in C++ (this function is actually little
4683 ;; more than a `looking-at' call in all modes except those that,
4684 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
4685 ;; name is found, 'template if it's an identifier ending with an
4686 ;; angle bracket arglist, 'operator of it's an operator identifier,
4687 ;; or t if it's some other kind of name.
4689 ;; This function records identifier ranges on
4690 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4691 ;; `c-record-type-identifiers' is non-nil.
4693 ;; This function might do hidden buffer changes.
4695 (let ((pos (point)) (start (point)) res id-start id-end
4696 ;; Turn off `c-promote-possible-types' here since we might
4697 ;; call `c-forward-<>-arglist' and we don't want it to promote
4698 ;; every suspect thing in the arglist to a type. We're
4699 ;; typically called from `c-forward-type' in this case, and
4700 ;; the caller only wants the top level type that it finds to
4702 c-promote-possible-types
)
4705 (looking-at c-identifier-key
)
4708 ;; Check for keyword. We go to the last symbol in
4709 ;; `c-identifier-key' first.
4710 (goto-char (setq id-end
(match-end 0)))
4711 (c-simple-skip-symbol-backward)
4712 (setq id-start
(point))
4714 (if (looking-at c-keywords-regexp
)
4715 (when (and (c-major-mode-is 'c
++-mode
)
4717 (cc-eval-when-compile
4718 (concat "\\(operator\\|\\(template\\)\\)"
4719 "\\(" (c-lang-const c-nonsymbol-key c
++)
4721 (if (match-beginning 2)
4722 ;; "template" is only valid inside an
4723 ;; identifier if preceded by "::".
4725 (c-backward-syntactic-ws)
4726 (and (c-safe (backward-char 2) t
)
4730 ;; Handle a C++ operator or template identifier.
4732 (c-forward-syntactic-ws)
4733 (cond ((eq (char-before id-end
) ?e
)
4734 ;; Got "... ::template".
4735 (let ((subres (c-forward-name)))
4740 ((looking-at c-identifier-start
)
4741 ;; Got a cast operator.
4742 (when (c-forward-type)
4745 ;; Now we should match a sequence of either
4746 ;; '*', '&' or a name followed by ":: *",
4747 ;; where each can be followed by a sequence
4748 ;; of `c-opt-type-modifier-key'.
4749 (while (cond ((looking-at "[*&]")
4750 (goto-char (match-end 0))
4752 ((looking-at c-identifier-start
)
4753 (and (c-forward-name)
4756 (goto-char (match-end 0))
4757 (c-forward-syntactic-ws)
4758 (eq (char-after) ?
*))
4763 (c-forward-syntactic-ws)
4765 (looking-at c-opt-type-modifier-key
))
4766 (goto-char (match-end 1))))))
4768 ((looking-at c-overloadable-operators-regexp
)
4769 ;; Got some other operator.
4770 (setq c-last-identifier-range
4771 (cons (point) (match-end 0)))
4772 (goto-char (match-end 0))
4773 (c-forward-syntactic-ws)
4779 ;; `id-start' is equal to `id-end' if we've jumped over
4780 ;; an identifier that doesn't end with a symbol token.
4781 ;; That can occur e.g. for Java import directives on the
4782 ;; form "foo.bar.*".
4783 (when (and id-start
(/= id-start id-end
))
4784 (setq c-last-identifier-range
4785 (cons id-start id-end
)))
4787 (c-forward-syntactic-ws)
4793 (when (or c-opt-identifier-concat-key
4794 c-recognize-
<>-arglists
)
4797 ((and c-opt-identifier-concat-key
4798 (looking-at c-opt-identifier-concat-key
))
4799 ;; Got a concatenated identifier. This handles the
4800 ;; cases with tricky syntactic whitespace that aren't
4801 ;; covered in `c-identifier-key'.
4802 (goto-char (match-end 0))
4803 (c-forward-syntactic-ws)
4806 ((and c-recognize-
<>-arglists
4807 (eq (char-after) ?
<))
4808 ;; Maybe an angle bracket arglist.
4810 (when (let (c-record-type-identifiers
4811 c-record-found-types
)
4812 (c-forward-<>-arglist nil
))
4814 (c-add-type start
(1+ pos
))
4815 (c-forward-syntactic-ws)
4817 c-last-identifier-range nil
)
4819 (if (and c-opt-identifier-concat-key
4820 (looking-at c-opt-identifier-concat-key
))
4822 ;; Continue if there's an identifier concatenation
4823 ;; operator after the template argument.
4825 (when (and c-record-type-identifiers id-start
)
4826 (c-record-ref-id (cons id-start id-end
)))
4828 (c-forward-syntactic-ws)
4831 (when (and c-record-type-identifiers id-start
)
4832 (c-record-type-id (cons id-start id-end
)))
4833 (setq res
'template
)
4840 (defun c-forward-type ()
4841 ;; Move forward over a type spec if at the beginning of one,
4842 ;; stopping at the next following token. Return t if it's a known
4843 ;; type that can't be a name or other expression, 'known if it's an
4844 ;; otherwise known type (according to `*-font-lock-extra-types'),
4845 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4846 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4847 ;; that might be a type, or nil if it can't be a type (the point
4848 ;; isn't moved then). The point is assumed to be at the beginning
4851 ;; Note that this function doesn't skip past the brace definition
4852 ;; that might be considered part of the type, e.g.
4853 ;; "enum {a, b, c} foo".
4855 ;; This function records identifier ranges on
4856 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4857 ;; `c-record-type-identifiers' is non-nil.
4859 ;; This function might do hidden buffer changes.
4861 (let ((start (point)) pos res name-res id-start id-end id-range
)
4863 ;; Skip leading type modifiers. If any are found we know it's a
4864 ;; prefix of a type.
4865 (when c-opt-type-modifier-key
4866 (while (looking-at c-opt-type-modifier-key
)
4867 (goto-char (match-end 1))
4868 (c-forward-syntactic-ws)
4869 (setq res
'prefix
)))
4872 ((looking-at c-type-prefix-key
)
4873 ;; Looking at a keyword that prefixes a type identifier,
4875 (goto-char (match-end 1))
4876 (c-forward-syntactic-ws)
4878 (if (memq (setq name-res
(c-forward-name)) '(t template
))
4880 (when (eq name-res t
)
4881 ;; In many languages the name can be used without the
4882 ;; prefix, so we add it to `c-found-types'.
4883 (c-add-type pos
(point))
4884 (when (and c-record-type-identifiers
4885 c-last-identifier-range
)
4886 (c-record-type-id c-last-identifier-range
)))
4894 (if (looking-at c-identifier-start
)
4896 (setq id-start
(point)
4897 name-res
(c-forward-name))
4899 (setq id-end
(point)
4900 id-range c-last-identifier-range
))))
4901 (and (cond ((looking-at c-primitive-type-key
)
4903 ((c-with-syntax-table c-identifier-syntax-table
4904 (looking-at c-known-type-key
))
4909 (goto-char (match-end 1))
4910 (c-forward-syntactic-ws)
4911 (setq pos
(point))))
4914 ;; Looking at a primitive or known type identifier. We've
4915 ;; checked for a name first so that we don't go here if the
4916 ;; known type match only is a prefix of another name.
4918 (setq id-end
(match-end 1))
4920 (when (and c-record-type-identifiers
4921 (or c-promote-possible-types
(eq res t
)))
4922 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4924 (if (and c-opt-type-component-key
4926 (looking-at c-opt-type-component-key
)))
4927 ;; There might be more keywords for the type.
4929 (c-forward-keyword-clause 1)
4931 (setq safe-pos
(point))
4932 (looking-at c-opt-type-component-key
))
4933 (when (and c-record-type-identifiers
4934 (looking-at c-primitive-type-key
))
4935 (c-record-type-id (cons (match-beginning 1)
4937 (c-forward-keyword-clause 1))
4938 (if (looking-at c-primitive-type-key
)
4940 (when c-record-type-identifiers
4941 (c-record-type-id (cons (match-beginning 1)
4943 (c-forward-keyword-clause 1)
4945 (goto-char safe-pos
)
4946 (setq res
'prefix
)))
4947 (unless (save-match-data (c-forward-keyword-clause 1))
4950 (goto-char (match-end 1))
4951 (c-forward-syntactic-ws)))))
4954 (cond ((eq name-res t
)
4955 ;; A normal identifier.
4957 (if (or res c-promote-possible-types
)
4959 (c-add-type id-start id-end
)
4960 (when (and c-record-type-identifiers id-range
)
4961 (c-record-type-id id-range
))
4964 (setq res
(if (c-check-type id-start id-end
)
4965 ;; It's an identifier that has been used as
4966 ;; a type somewhere else.
4968 ;; It's an identifier that might be a type.
4970 ((eq name-res
'template
)
4971 ;; A template is a type.
4975 ;; Otherwise it's an operator identifier, which is not a type.
4980 ;; Skip trailing type modifiers. If any are found we know it's
4982 (when c-opt-type-modifier-key
4983 (while (looking-at c-opt-type-modifier-key
)
4984 (goto-char (match-end 1))
4985 (c-forward-syntactic-ws)
4988 ;; Step over any type suffix operator. Do not let the existence
4989 ;; of these alter the classification of the found type, since
4990 ;; these operators typically are allowed in normal expressions
4992 (when c-opt-type-suffix-key
4993 (while (looking-at c-opt-type-suffix-key
)
4994 (goto-char (match-end 1))
4995 (c-forward-syntactic-ws)))
4997 (when c-opt-type-concat-key
4998 ;; Look for a trailing operator that concatenates the type
4999 ;; with a following one, and if so step past that one through
5000 ;; a recursive call. Note that we don't record concatenated
5001 ;; types in `c-found-types' - it's the component types that
5002 ;; are recorded when appropriate.
5004 (let* ((c-promote-possible-types (or (memq res
'(t known
))
5005 c-promote-possible-types
))
5006 ;; If we can't promote then set `c-record-found-types' so that
5007 ;; we can merge in the types from the second part afterwards if
5008 ;; it turns out to be a known type there.
5009 (c-record-found-types (and c-record-type-identifiers
5010 (not c-promote-possible-types
)))
5012 (if (and (looking-at c-opt-type-concat-key
)
5015 (goto-char (match-end 1))
5016 (c-forward-syntactic-ws)
5017 (setq subres
(c-forward-type))))
5020 ;; If either operand certainly is a type then both are, but we
5021 ;; don't let the existence of the operator itself promote two
5022 ;; uncertain types to a certain one.
5025 (unless (eq name-res
'template
)
5026 (c-add-type id-start id-end
))
5027 (when (and c-record-type-identifiers id-range
)
5028 (c-record-type-id id-range
))
5039 (when (and (eq res t
)
5040 (consp c-record-found-types
))
5041 ;; Merge in the ranges of any types found by the second
5042 ;; `c-forward-type'.
5043 (setq c-record-type-identifiers
5044 ;; `nconc' doesn't mind that the tail of
5045 ;; `c-record-found-types' is t.
5046 (nconc c-record-found-types
5047 c-record-type-identifiers
))))
5051 (when (and c-record-found-types
(memq res
'(known found
)) id-range
)
5052 (setq c-record-found-types
5053 (cons id-range c-record-found-types
))))
5055 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
5060 ;; Handling of large scale constructs like statements and declarations.
5062 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
5063 ;; defsubst or perhaps even a defun, but it contains lots of free
5064 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
5065 (defmacro c-fdoc-shift-type-backward
(&optional short
)
5066 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
5067 ;; of types when parsing a declaration, which means that it
5068 ;; sometimes consumes the identifier in the declaration as a type.
5069 ;; This is used to "backtrack" and make the last type be treated as
5070 ;; an identifier instead.
5073 ;; These identifiers are bound only in the inner let.
5074 '(setq identifier-type at-type
5075 identifier-start type-start
5079 got-suffix-after-parens id-start
5082 (if (setq at-type
(if (eq backup-at-type
'prefix
)
5085 (setq type-start backup-type-start
5086 id-start backup-id-start
)
5087 (setq type-start start-pos
5088 id-start start-pos
))
5090 ;; When these flags already are set we've found specifiers that
5091 ;; unconditionally signal these attributes - backtracking doesn't
5092 ;; change that. So keep them set in that case.
5094 (setq at-type-decl backup-at-type-decl
))
5096 (setq maybe-typeless backup-maybe-typeless
))
5099 ;; This identifier is bound only in the inner let.
5100 '(setq start id-start
))))
5102 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end
)
5103 ;; Move forward over a declaration or a cast if at the start of one.
5104 ;; The point is assumed to be at the start of some token. Nil is
5105 ;; returned if no declaration or cast is recognized, and the point
5106 ;; is clobbered in that case.
5108 ;; If a declaration is parsed:
5110 ;; The point is left at the first token after the first complete
5111 ;; declarator, if there is one. The return value is a cons where
5112 ;; the car is the position of the first token in the declarator. (See
5113 ;; below for the cdr.)
5116 ;; void foo (int a, char *b) stuff ...
5120 ;; unsigned int a = c_style_initializer, b;
5122 ;; unsigned int a (cplusplus_style_initializer), b;
5123 ;; car ^ ^ point (might change)
5124 ;; class Foo : public Bar {}
5126 ;; class PikeClass (int a, string b) stuff ...
5132 ;; void cplusplus_function (int x) throw (Bad);
5134 ;; Foo::Foo (int b) : Base (b) {}
5137 ;; The cdr of the return value is non-nil iff a `c-typedef-decl-kwds'
5138 ;; specifier (e.g. class, struct, enum, typedef) is found in the
5139 ;; declaration, i.e. the declared identifier(s) are types.
5141 ;; If a cast is parsed:
5143 ;; The point is left at the first token after the closing paren of
5144 ;; the cast. The return value is `cast'. Note that the start
5145 ;; position must be at the first token inside the cast parenthesis
5148 ;; PRECEDING-TOKEN-END is the first position after the preceding
5149 ;; token, i.e. on the other side of the syntactic ws from the point.
5150 ;; Use a value less than or equal to (point-min) if the point is at
5151 ;; the first token in (the visible part of) the buffer.
5153 ;; CONTEXT is a symbol that describes the context at the point:
5154 ;; 'decl In a comma-separated declaration context (typically
5155 ;; inside a function declaration arglist).
5156 ;; '<> In an angle bracket arglist.
5157 ;; 'arglist Some other type of arglist.
5158 ;; nil Some other context or unknown context. Includes
5159 ;; within the parens of an if, for, ... construct.
5161 ;; LAST-CAST-END is the first token after the closing paren of a
5162 ;; preceding cast, or nil if none is known. If
5163 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
5164 ;; the position after the closest preceding call where a cast was
5165 ;; matched. In that case it's used to discover chains of casts like
5168 ;; This function records identifier ranges on
5169 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5170 ;; `c-record-type-identifiers' is non-nil.
5172 ;; This function might do hidden buffer changes.
5174 (let (;; `start-pos' is used below to point to the start of the
5175 ;; first type, i.e. after any leading specifiers. It might
5176 ;; also point at the beginning of the preceding syntactic
5179 ;; Set to the result of `c-forward-type'.
5181 ;; The position of the first token in what we currently
5182 ;; believe is the type in the declaration or cast, after any
5183 ;; specifiers and their associated clauses.
5185 ;; The position of the first token in what we currently
5186 ;; believe is the declarator for the first identifier. Set
5187 ;; when the type is found, and moved forward over any
5188 ;; `c-decl-hangon-kwds' and their associated clauses that
5189 ;; occurs after the type.
5191 ;; These store `at-type', `type-start' and `id-start' of the
5192 ;; identifier before the one in those variables. The previous
5193 ;; identifier might turn out to be the real type in a
5194 ;; declaration if the last one has to be the declarator in it.
5195 ;; If `backup-at-type' is nil then the other variables have
5196 ;; undefined values.
5197 backup-at-type backup-type-start backup-id-start
5198 ;; Set if we've found a specifier that makes the defined
5199 ;; identifier(s) types.
5201 ;; Set if we've found a specifier that can start a declaration
5202 ;; where there's no type.
5204 ;; If a specifier is found that also can be a type prefix,
5205 ;; these flags are set instead of those above. If we need to
5206 ;; back up an identifier, they are copied to the real flag
5207 ;; variables. Thus they only take effect if we fail to
5208 ;; interpret it as a type.
5209 backup-at-type-decl backup-maybe-typeless
5210 ;; Whether we've found a declaration or a cast. We might know
5211 ;; this before we've found the type in it. It's 'ids if we've
5212 ;; found two consecutive identifiers (usually a sure sign, but
5213 ;; we should allow that in labels too), and t if we've found a
5214 ;; specifier keyword (a 100% sure sign).
5216 ;; Set when we need to back up to parse this as a declaration
5217 ;; but not as a cast.
5219 ;; For casts, the return position.
5221 ;; Save `c-record-type-identifiers' and
5222 ;; `c-record-ref-identifiers' since ranges are recorded
5223 ;; speculatively and should be thrown away if it turns out
5224 ;; that it isn't a declaration or cast.
5225 (save-rec-type-ids c-record-type-identifiers
)
5226 (save-rec-ref-ids c-record-ref-identifiers
))
5228 ;; Check for a type. Unknown symbols are treated as possible
5229 ;; types, but they could also be specifiers disguised through
5230 ;; macros like __INLINE__, so we recognize both types and known
5231 ;; specifiers after them too.
5233 (let* ((start (point)) kwd-sym kwd-clause-end found-type
)
5235 ;; Look for a specifier keyword clause.
5236 (when (looking-at c-prefix-spec-kwds-re
)
5237 (setq kwd-sym
(c-keyword-sym (match-string 1)))
5239 (c-forward-keyword-clause 1)
5240 (setq kwd-clause-end
(point))))
5242 (when (setq found-type
(c-forward-type))
5243 ;; Found a known or possible type or a prefix of a known type.
5246 ;; Got two identifiers with nothing but whitespace
5247 ;; between them. That can only happen in declarations.
5248 (setq at-decl-or-cast
'ids
)
5250 (when (eq at-type
'found
)
5251 ;; If the previous identifier is a found type we
5252 ;; record it as a real one; it might be some sort of
5253 ;; alias for a prefix like "unsigned".
5255 (goto-char type-start
)
5256 (let ((c-promote-possible-types t
))
5257 (c-forward-type)))))
5259 (setq backup-at-type at-type
5260 backup-type-start type-start
5261 backup-id-start id-start
5265 ;; The previous ambiguous specifier/type turned out
5266 ;; to be a type since we've parsed another one after
5267 ;; it, so clear these backup flags.
5268 backup-at-type-decl nil
5269 backup-maybe-typeless nil
))
5273 ;; Handle known specifier keywords and
5274 ;; `c-decl-hangon-kwds' which can occur after known
5277 (if (c-keyword-member kwd-sym
'c-decl-hangon-kwds
)
5278 ;; It's a hang-on keyword that can occur anywhere.
5280 (setq at-decl-or-cast t
)
5282 ;; Move the identifier start position if
5283 ;; we've passed a type.
5284 (setq id-start kwd-clause-end
)
5285 ;; Otherwise treat this as a specifier and
5286 ;; move the fallback position.
5287 (setq start-pos kwd-clause-end
))
5288 (goto-char kwd-clause-end
))
5290 ;; It's an ordinary specifier so we know that
5291 ;; anything before this can't be the type.
5292 (setq backup-at-type nil
5293 start-pos kwd-clause-end
)
5296 ;; It's ambiguous whether this keyword is a
5297 ;; specifier or a type prefix, so set the backup
5298 ;; flags. (It's assumed that `c-forward-type'
5299 ;; moved further than `c-forward-keyword-clause'.)
5301 (when (c-keyword-member kwd-sym
'c-typedef-decl-kwds
)
5302 (setq backup-at-type-decl t
))
5303 (when (c-keyword-member kwd-sym
'c-typeless-decl-kwds
)
5304 (setq backup-maybe-typeless t
)))
5306 (when (c-keyword-member kwd-sym
'c-typedef-decl-kwds
)
5307 (setq at-type-decl t
))
5308 (when (c-keyword-member kwd-sym
'c-typeless-decl-kwds
)
5309 (setq maybe-typeless t
))
5311 ;; Haven't matched a type so it's an umambiguous
5312 ;; specifier keyword and we know we're in a
5314 (setq at-decl-or-cast t
)
5316 (goto-char kwd-clause-end
))))
5318 ;; If the type isn't known we continue so that we'll jump
5319 ;; over all specifiers and type identifiers. The reason
5320 ;; to do this for a known type prefix is to make things
5321 ;; like "unsigned INT16" work.
5322 (and found-type
(not (eq found-type t
))))))
5326 ;; If a known type was found, we still need to skip over any
5327 ;; hangon keyword clauses after it. Otherwise it has already
5328 ;; been done in the loop above.
5329 (while (looking-at c-decl-hangon-key
)
5330 (c-forward-keyword-clause 1))
5331 (setq id-start
(point)))
5333 ((eq at-type
'prefix
)
5334 ;; A prefix type is itself a primitive type when it's not
5335 ;; followed by another type.
5339 ;; Got no type but set things up to continue anyway to handle
5340 ;; the various cases when a declaration doesn't start with a
5342 (setq id-start start-pos
))
5344 ((and (eq at-type
'maybe
)
5345 (c-major-mode-is 'c
++-mode
))
5346 ;; If it's C++ then check if the last "type" ends on the form
5347 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
5348 ;; (con|de)structor.
5350 (let (name end-2 end-1
)
5351 (goto-char id-start
)
5352 (c-backward-syntactic-ws)
5353 (setq end-2
(point))
5355 (c-simple-skip-symbol-backward)
5358 (buffer-substring-no-properties (point) end-2
))
5359 ;; Cheating in the handling of syntactic ws below.
5360 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
5362 (setq end-1
(point))
5363 (c-simple-skip-symbol-backward))
5364 (>= (point) type-start
)
5365 (equal (buffer-substring-no-properties (point) end-1
)
5367 ;; It is a (con|de)structor name. In that case the
5368 ;; declaration is typeless so zap out any preceding
5369 ;; identifier(s) that we might have taken as types.
5370 (goto-char type-start
)
5373 id-start type-start
))))))
5375 ;; Check for and step over a type decl expression after the thing
5376 ;; that is or might be a type. This can't be skipped since we
5377 ;; need the correct end position of the declarator for
5378 ;; `max-type-decl-end-*'.
5379 (let ((start (point)) (paren-depth 0) pos
5380 ;; True if there's a non-open-paren match of
5381 ;; `c-type-decl-prefix-key'.
5383 ;; True if the declarator is surrounded by a parenthesis pair.
5385 ;; True if there is an identifier in the declarator.
5387 ;; True if there's a non-close-paren match of
5388 ;; `c-type-decl-suffix-key'.
5390 ;; True if there's a prefix match outside the outermost
5391 ;; paren pair that surrounds the declarator.
5392 got-prefix-before-parens
5393 ;; True if there's a suffix match outside the outermost
5394 ;; paren pair that surrounds the declarator. The value is
5395 ;; the position of the first suffix match.
5396 got-suffix-after-parens
5397 ;; True if we've parsed the type decl to a token that is
5398 ;; known to end declarations in this context.
5400 ;; The earlier values of `at-type' and `type-start' if we've
5401 ;; shifted the type backwards.
5402 identifier-type identifier-start
5403 ;; If `c-parse-and-markup-<>-arglists' is set we need to
5404 ;; turn it off during the name skipping below to avoid
5405 ;; getting `c-type' properties that might be bogus. That
5406 ;; can happen since we don't know if
5407 ;; `c-restricted-<>-arglists' will be correct inside the
5408 ;; arglist paren that gets entered.
5409 c-parse-and-markup-
<>-arglists
)
5411 (goto-char id-start
)
5413 ;; Skip over type decl prefix operators. (Note similar code in
5414 ;; `c-font-lock-declarators'.)
5415 (while (and (looking-at c-type-decl-prefix-key
)
5416 (if (and (c-major-mode-is 'c
++-mode
)
5417 (match-beginning 2))
5418 ;; If the second submatch matches in C++ then
5419 ;; we're looking at an identifier that's a
5420 ;; prefix only if it specifies a member pointer.
5421 (when (setq got-identifier
(c-forward-name))
5422 (if (looking-at "\\(::\\)")
5423 ;; We only check for a trailing "::" and
5424 ;; let the "*" that should follow be
5425 ;; matched in the next round.
5426 (progn (setq got-identifier nil
) t
)
5427 ;; It turned out to be the real identifier,
5432 (if (eq (char-after) ?\
()
5434 (setq paren-depth
(1+ paren-depth
))
5436 (unless got-prefix-before-parens
5437 (setq got-prefix-before-parens
(= paren-depth
0)))
5439 (goto-char (match-end 1)))
5440 (c-forward-syntactic-ws))
5442 (setq got-parens
(> paren-depth
0))
5444 ;; Skip over an identifier.
5446 (and (looking-at c-identifier-start
)
5447 (setq got-identifier
(c-forward-name))))
5449 ;; Skip over type decl suffix operators.
5450 (while (if (looking-at c-type-decl-suffix-key
)
5452 (if (eq (char-after) ?\
))
5453 (when (> paren-depth
0)
5454 (setq paren-depth
(1- paren-depth
))
5457 (when (if (save-match-data (looking-at "\\s\("))
5458 (c-safe (c-forward-sexp 1) t
)
5459 (goto-char (match-end 1))
5461 (when (and (not got-suffix-after-parens
)
5463 (setq got-suffix-after-parens
(match-beginning 0)))
5464 (setq got-suffix t
)))
5466 ;; No suffix matched. We might have matched the
5467 ;; identifier as a type and the open paren of a
5468 ;; function arglist as a type decl prefix. In that
5469 ;; case we should "backtrack": Reinterpret the last
5470 ;; type as the identifier, move out of the arglist and
5471 ;; continue searching for suffix operators.
5473 ;; Do this even if there's no preceding type, to cope
5474 ;; with old style function declarations in K&R C,
5475 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
5476 ;; style declarations. That isn't applicable in an
5477 ;; arglist context, though.
5478 (when (and (= paren-depth
1)
5479 (not got-prefix-before-parens
)
5480 (not (eq at-type t
))
5483 backup-maybe-typeless
5484 (when c-recognize-typeless-decls
5486 (setq pos
(c-up-list-forward (point)))
5487 (eq (char-before pos
) ?\
)))
5488 (c-fdoc-shift-type-backward)
5492 (c-forward-syntactic-ws))
5494 (when (and (or maybe-typeless backup-maybe-typeless
)
5495 (not got-identifier
)
5498 ;; Have found no identifier but `c-typeless-decl-kwds' has
5499 ;; matched so we know we're inside a declaration. The
5500 ;; preceding type must be the identifier instead.
5501 (c-fdoc-shift-type-backward))
5505 (catch 'at-decl-or-cast
5508 (when (> paren-depth
0)
5509 ;; Encountered something inside parens that isn't matched by
5510 ;; the `c-type-decl-*' regexps, so it's not a type decl
5511 ;; expression. Try to skip out to the same paren depth to
5512 ;; not confuse the cast check below.
5513 (c-safe (goto-char (scan-lists (point) 1 paren-depth
)))
5514 ;; If we've found a specifier keyword then it's a
5515 ;; declaration regardless.
5516 (throw 'at-decl-or-cast
(eq at-decl-or-cast t
)))
5519 (looking-at (cond ((eq context
'<>) "[,>]")
5523 ;; Now we've collected info about various characteristics of
5524 ;; the construct we're looking at. Below follows a decision
5525 ;; tree based on that. It's ordered to check more certain
5526 ;; signs before less certain ones.
5532 (when (and (or at-type maybe-typeless
)
5533 (not (or got-prefix got-parens
)))
5534 ;; Got another identifier directly after the type, so it's a
5536 (throw 'at-decl-or-cast t
))
5538 (when (and got-parens
5540 (not got-suffix-after-parens
)
5543 backup-maybe-typeless
))
5544 ;; Got a declaration of the form "foo bar (gnu);" where we've
5545 ;; recognized "bar" as the type and "gnu" as the declarator.
5546 ;; In this case it's however more likely that "bar" is the
5547 ;; declarator and "gnu" a function argument or initializer (if
5548 ;; `c-recognize-paren-inits' is set), since the parens around
5549 ;; "gnu" would be superfluous if it's a declarator. Shift the
5550 ;; type one step backward.
5551 (c-fdoc-shift-type-backward)))
5553 ;; Found no identifier.
5559 (when (= (point) start
)
5560 ;; Got a plain list of identifiers. If a colon follows it's
5561 ;; a valid label. Otherwise the last one probably is the
5562 ;; declared identifier and we should back up to the previous
5563 ;; type, providing it isn't a cast.
5564 (if (eq (char-after) ?
:)
5565 ;; If we've found a specifier keyword then it's a
5566 ;; declaration regardless.
5567 (throw 'at-decl-or-cast
(eq at-decl-or-cast t
))
5568 (setq backup-if-not-cast t
)
5569 (throw 'at-decl-or-cast t
)))
5572 (when (and got-suffix
5575 ;; Got a plain list of identifiers followed by some suffix.
5576 ;; If this isn't a cast then the last identifier probably is
5577 ;; the declared one and we should back up to the previous
5579 (setq backup-if-not-cast t
)
5580 (throw 'at-decl-or-cast t
)))
5583 (when (eq at-type t
)
5584 ;; If the type is known we know that there can't be any
5585 ;; identifier somewhere else, and it's only in declarations in
5586 ;; e.g. function prototypes and in casts that the identifier may
5588 (throw 'at-decl-or-cast t
))
5590 (when (= (point) start
)
5591 ;; Only got a single identifier (parsed as a type so far).
5594 ;; Check that the identifier isn't at the start of an
5599 ;; Inside an arglist that contains declarations. If K&R
5600 ;; style declarations and parenthesis style initializers
5601 ;; aren't allowed then the single identifier must be a
5602 ;; type, else we require that it's known or found
5603 ;; (primitive types are handled above).
5604 (or (and (not c-recognize-knr-p
)
5605 (not c-recognize-paren-inits
))
5606 (memq at-type
'(known found
))))
5608 ;; Inside a template arglist. Accept known and found
5609 ;; types; other identifiers could just as well be
5610 ;; constants in C++.
5611 (memq at-type
'(known found
)))))
5612 (throw 'at-decl-or-cast t
)
5614 ;; Can't be a valid declaration or cast, but if we've found a
5615 ;; specifier it can't be anything else either, so treat it as
5616 ;; an invalid/unfinished declaration or cast.
5617 (throw 'at-decl-or-cast at-decl-or-cast
))))
5622 (not (eq at-type t
))
5625 backup-maybe-typeless
5626 (when c-recognize-typeless-decls
5627 (or (not got-suffix
)
5629 c-after-suffixed-type-maybe-decl-key
))))))
5630 ;; Got an empty paren pair and a preceding type that probably
5631 ;; really is the identifier. Shift the type backwards to make
5632 ;; the last one the identifier. This is analogous to the
5633 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
5636 ;; Exception: In addition to the conditions in that
5637 ;; "backtracking" code, do not shift backward if we're not
5638 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
5639 ;; Since there's no preceding type, the shift would mean that
5640 ;; the declaration is typeless. But if the regexp doesn't match
5641 ;; then we will simply fall through in the tests below and not
5642 ;; recognize it at all, so it's better to try it as an abstract
5643 ;; declarator instead.
5644 (c-fdoc-shift-type-backward)
5646 ;; Still no identifier.
5648 (when (and got-prefix
(or got-parens got-suffix
))
5649 ;; Require `got-prefix' together with either `got-parens' or
5650 ;; `got-suffix' to recognize it as an abstract declarator:
5651 ;; `got-parens' only is probably an empty function call.
5652 ;; `got-suffix' only can build an ordinary expression together
5653 ;; with the preceding identifier which we've taken as a type.
5654 ;; We could actually accept on `got-prefix' only, but that can
5655 ;; easily occur temporarily while writing an expression so we
5656 ;; avoid that case anyway. We could do a better job if we knew
5657 ;; the point when the fontification was invoked.
5658 (throw 'at-decl-or-cast t
))
5664 got-suffix-after-parens
5665 (eq (char-after got-suffix-after-parens
) ?\
())
5666 ;; Got a type, no declarator but a paren suffix. I.e. it's a
5667 ;; normal function call afterall (or perhaps a C++ style object
5668 ;; instantiation expression).
5669 (throw 'at-decl-or-cast nil
))))
5672 (when at-decl-or-cast
5673 ;; By now we've located the type in the declaration that we know
5675 (throw 'at-decl-or-cast t
))
5678 (when (and got-identifier
5680 (looking-at c-after-suffixed-type-decl-key
)
5684 (not (eq at-type t
)))
5685 ;; Shift the type backward in the case that there's a
5686 ;; single identifier inside parens. That can only
5687 ;; occur in K&R style function declarations so it's
5688 ;; more likely that it really is a function call.
5689 ;; Therefore we only do this after
5690 ;; `c-after-suffixed-type-decl-key' has matched.
5691 (progn (c-fdoc-shift-type-backward) t
)
5692 got-suffix-after-parens
))
5693 ;; A declaration according to `c-after-suffixed-type-decl-key'.
5694 (throw 'at-decl-or-cast t
))
5697 (when (and (or got-prefix
(not got-parens
))
5698 (memq at-type
'(t known
)))
5699 ;; It's a declaration if a known type precedes it and it can't be a
5701 (throw 'at-decl-or-cast t
))
5703 ;; If we get here we can't tell if this is a type decl or a normal
5704 ;; expression by looking at it alone. (That's under the assumption
5705 ;; that normal expressions always can look like type decl expressions,
5706 ;; which isn't really true but the cases where it doesn't hold are so
5707 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
5708 ;; the effort to look for them.)
5710 (unless (or at-decl-end
(looking-at "=[^=]"))
5711 ;; If this is a declaration it should end here or its initializer(*)
5712 ;; should start here, so check for allowed separation tokens. Note
5713 ;; that this rule doesn't work e.g. with a K&R arglist after a
5716 ;; *) Don't check for C++ style initializers using parens
5717 ;; since those already have been matched as suffixes.
5719 ;; If `at-decl-or-cast' is then we've found some other sign that
5720 ;; it's a declaration or cast, so then it's probably an
5721 ;; invalid/unfinished one.
5722 (throw 'at-decl-or-cast at-decl-or-cast
))
5724 ;; Below are tests that only should be applied when we're certain to
5725 ;; not have parsed halfway through an expression.
5728 (when (memq at-type
'(t known
))
5729 ;; The expression starts with a known type so treat it as a
5731 (throw 'at-decl-or-cast t
))
5734 (when (and (c-major-mode-is 'c
++-mode
)
5735 ;; In C++ we check if the identifier is a known type, since
5736 ;; (con|de)structors use the class name as identifier.
5737 ;; We've always shifted over the identifier as a type and
5738 ;; then backed up again in this case.
5740 (or (memq identifier-type
'(found known
))
5741 (and (eq (char-after identifier-start
) ?~
)
5742 ;; `at-type' probably won't be 'found for
5743 ;; destructors since the "~" is then part of the
5744 ;; type name being checked against the list of
5745 ;; known types, so do a check without that
5748 (goto-char (1+ identifier-start
))
5749 (c-forward-syntactic-ws)
5750 (c-with-syntax-table
5751 c-identifier-syntax-table
5752 (looking-at c-known-type-key
)))
5754 (goto-char (1+ identifier-start
))
5755 ;; We have already parsed the type earlier,
5756 ;; so it'd be possible to cache the end
5757 ;; position instead of redoing it here, but
5758 ;; then we'd need to keep track of another
5759 ;; position everywhere.
5760 (c-check-type (point)
5761 (progn (c-forward-type)
5763 (throw 'at-decl-or-cast t
))
5768 (when (and got-prefix-before-parens
5770 (or at-decl-end
(looking-at "=[^=]"))
5773 ;; Got something like "foo * bar;". Since we're not inside an
5774 ;; arglist it would be a meaningless expression because the
5775 ;; result isn't used. We therefore choose to recognize it as
5776 ;; a declaration. Do not allow a suffix since it could then
5777 ;; be a function call.
5778 (throw 'at-decl-or-cast t
))
5781 (when (and (or got-suffix-after-parens
5782 (looking-at "=[^=]"))
5784 (not (eq context
'arglist
)))
5785 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
5786 ;; be an odd expression or it could be a declaration. Treat
5787 ;; it as a declaration if "a" has been used as a type
5788 ;; somewhere else (if it's a known type we won't get here).
5789 (throw 'at-decl-or-cast t
)))
5794 (and (eq context
'decl
)
5795 (not c-recognize-paren-inits
)
5796 (or got-parens got-suffix
))))
5797 ;; Got a type followed by an abstract declarator. If `got-prefix'
5798 ;; is set it's something like "a *" without anything after it. If
5799 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
5800 ;; or similar, which we accept only if the context rules out
5802 (throw 'at-decl-or-cast t
)))
5804 ;; If we had a complete symbol table here (which rules out
5805 ;; `c-found-types') we should return t due to the disambiguation rule
5806 ;; (in at least C++) that anything that can be parsed as a declaration
5807 ;; is a declaration. Now we're being more defensive and prefer to
5808 ;; highlight things like "foo (bar);" as a declaration only if we're
5809 ;; inside an arglist that contains declarations.
5810 (eq context
'decl
))))
5812 ;; The point is now after the type decl expression.
5815 ;; Check for a cast.
5820 ;; Should be the first type/identifier in a cast paren.
5821 (> preceding-token-end
(point-min))
5822 (memq (char-before preceding-token-end
) c-cast-parens
)
5824 ;; The closing paren should follow.
5826 (c-forward-syntactic-ws)
5827 (looking-at "\\s\)"))
5829 ;; There should be a primary expression after it.
5832 (c-forward-syntactic-ws)
5833 (setq cast-end
(point))
5834 (and (looking-at c-primary-expr-regexp
)
5836 (setq pos
(match-end 0))
5838 ;; Check if the expression begins with a prefix keyword.
5840 (if (match-beginning 1)
5841 ;; Expression begins with an ambiguous operator. Treat
5842 ;; it as a cast if it's a type decl or if we've
5843 ;; recognized the type somewhere else.
5845 (memq at-type
'(t known found
)))
5846 ;; Unless it's a keyword, it's the beginning of a primary
5848 (not (looking-at c-keywords-regexp
)))))
5849 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
5850 ;; that it matched a whole one so that we don't e.g. confuse
5851 ;; the operator '-' with '->'. It's ok if it matches further,
5852 ;; though, since it e.g. can match the float '.5' while the
5853 ;; operator regexp only matches '.'.
5854 (or (not (looking-at c-nonsymbol-token-regexp
))
5855 (<= (match-end 0) pos
))))
5857 ;; There should either be a cast before it or something that isn't an
5858 ;; identifier or close paren.
5859 (> preceding-token-end
(point-min))
5861 (goto-char (1- preceding-token-end
))
5862 (or (eq (point) last-cast-end
)
5864 (c-backward-syntactic-ws)
5865 (if (< (skip-syntax-backward "w_") 0)
5866 ;; It's a symbol. Accept it only if it's one of the
5867 ;; keywords that can precede an expression (without
5868 ;; surrounding parens).
5869 (looking-at c-simple-stmt-key
)
5871 ;; Check that it isn't a close paren (block close is ok,
5873 (not (memq (char-before) '(?\
) ?\
])))
5874 ;; Check that it isn't a nonsymbol identifier.
5875 (not (c-on-identifier)))))))))
5878 (when (and c-record-type-identifiers at-type
(not (eq at-type t
)))
5879 (let ((c-promote-possible-types t
))
5880 (goto-char type-start
)
5883 (goto-char cast-end
)
5887 ;; We're at a declaration. Highlight the type and the following
5890 (when backup-if-not-cast
5891 (c-fdoc-shift-type-backward t
))
5893 (when (and (eq context
'decl
) (looking-at ","))
5894 ;; Make sure to propagate the `c-decl-arg-start' property to
5895 ;; the next argument if it's set in this one, to cope with
5896 ;; interactive refontification.
5897 (c-put-c-type-property (point) 'c-decl-arg-start
))
5899 (when (and c-record-type-identifiers at-type
(not (eq at-type t
)))
5900 (let ((c-promote-possible-types t
))
5902 (goto-char type-start
)
5905 (cons id-start at-type-decl
))
5908 ;; False alarm. Restore the recorded ranges.
5909 (setq c-record-type-identifiers save-rec-type-ids
5910 c-record-ref-identifiers save-rec-ref-ids
)
5913 (defun c-forward-label (&optional assume-markup preceding-token-end limit
)
5914 ;; Assuming that point is at the beginning of a token, check if it starts a
5915 ;; label and if so move over it and return non-nil (t in default situations,
5916 ;; specific symbols (see below) for interesting situations), otherwise don't
5917 ;; move and return nil. "Label" here means "most things with a colon".
5919 ;; More precisely, a "label" is regarded as one of:
5920 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
5921 ;; (ii) A case label - either the entire construct "case FOO:", or just the
5922 ;; bare "case", should the colon be missing. We return t;
5923 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
5925 ;; (iv) One of QT's "extended" C++ variants of
5926 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
5927 ;; Returns the symbol `qt-2kwds-colon'.
5928 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
5929 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
5930 ;; colon). Currently (2006-03), this applies only to Objective C's
5931 ;; keywords "@private", "@protected", and "@public". Returns t.
5933 ;; One of the things which will NOT be recognised as a label is a bit-field
5934 ;; element of a struct, something like "int foo:5".
5936 ;; The end of the label is taken to be just after the colon, or the end of
5937 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
5938 ;; after the end on return. The terminating char gets marked with
5939 ;; `c-decl-end' to improve recognition of the following declaration or
5942 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
5943 ;; label, if any, has already been marked up like that.
5945 ;; If PRECEDING-TOKEN-END is given, it should be the first position
5946 ;; after the preceding token, i.e. on the other side of the
5947 ;; syntactic ws from the point. Use a value less than or equal to
5948 ;; (point-min) if the point is at the first token in (the visible
5949 ;; part of) the buffer.
5951 ;; The optional LIMIT limits the forward scan for the colon.
5953 ;; This function records the ranges of the label symbols on
5954 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
5957 ;; This function might do hidden buffer changes.
5959 (let ((start (point))
5962 macro-start
; if we're in one.
5966 ;; "case" or "default" (Doesn't apply to AWK).
5967 ((looking-at c-label-kwds-regexp
)
5968 (let ((kwd-end (match-end 1)))
5969 ;; Record only the keyword itself for fontification, since in
5970 ;; case labels the following is a constant expression and not
5972 (when c-record-type-identifiers
5973 (c-record-ref-id (cons (match-beginning 1) kwd-end
)))
5975 ;; Find the label end.
5978 (if (and (c-syntactic-re-search-forward
5979 ;; Stop on chars that aren't allowed in expressions,
5980 ;; and on operator chars that would be meaningless
5981 ;; there. FIXME: This doesn't cope with ?: operators.
5982 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
5984 (match-beginning 2))
5986 (progn ; there's a proper :
5987 (goto-char (match-beginning 2)) ; just after the :
5988 (c-put-c-type-property (1- (point)) 'c-decl-end
)
5991 ;; It's an unfinished label. We consider the keyword enough
5992 ;; to recognize it as a label, so that it gets fontified.
5993 ;; Leave the point at the end of it, but don't put any
5994 ;; `c-decl-end' marker.
5998 ;; @private, @protected, @public, in Objective C, or similar.
5999 ((and c-opt-extra-label-key
6000 (looking-at c-opt-extra-label-key
))
6001 ;; For a `c-opt-extra-label-key' match, we record the whole
6002 ;; thing for fontification. That's to get the leading '@' in
6003 ;; Objective-C protection labels fontified.
6004 (goto-char (match-end 1))
6005 (when c-record-type-identifiers
6006 (c-record-ref-id (cons (match-beginning 1) (point))))
6007 (c-put-c-type-property (1- (point)) 'c-decl-end
)
6008 (setq label-type t
))
6010 ;; All other cases of labels.
6011 ((and c-recognize-colon-labels
; nil for AWK and IDL, otherwise t.
6013 ;; A colon label must have something before the colon.
6014 (not (eq (char-after) ?
:))
6016 ;; Check that we're not after a token that can't precede a label.
6018 ;; Trivially succeeds when there's no preceding token.
6019 (if preceding-token-end
6020 (<= preceding-token-end
(point-min))
6022 (c-backward-syntactic-ws)
6023 (setq preceding-token-end
(point))
6026 ;; Check if we're after a label, if we're after a closing
6027 ;; paren that belong to statement, and with
6028 ;; `c-label-prefix-re'. It's done in different order
6029 ;; depending on `assume-markup' since the checks have
6030 ;; different expensiveness.
6033 (eq (c-get-char-property (1- preceding-token-end
) 'c-type
)
6037 (goto-char (1- preceding-token-end
))
6038 (c-beginning-of-current-token)
6039 (or (looking-at c-label-prefix-re
)
6040 (looking-at c-block-stmt-1-key
)))
6042 (and (eq (char-before preceding-token-end
) ?\
))
6043 (c-after-conditional)))
6047 (goto-char (1- preceding-token-end
))
6048 (c-beginning-of-current-token)
6049 (or (looking-at c-label-prefix-re
)
6050 (looking-at c-block-stmt-1-key
)))
6053 ((eq (char-before preceding-token-end
) ?\
))
6054 (c-after-conditional))
6056 ((eq (char-before preceding-token-end
) ?
:)
6057 ;; Might be after another label, so check it recursively.
6060 (goto-char (1- preceding-token-end
))
6061 ;; Essentially the same as the
6062 ;; `c-syntactic-re-search-forward' regexp below.
6064 (save-excursion (and (c-beginning-of-macro)
6066 (if macro-start
(narrow-to-region macro-start
(point-max)))
6067 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t
)
6068 ;; Note: the following should work instead of the
6069 ;; narrow-to-region above. Investigate why not,
6070 ;; sometime. ACM, 2006-03-31.
6071 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
6074 ;; If the caller turned on recording for us,
6075 ;; it shouldn't apply when we check the
6077 c-record-type-identifiers
)
6078 ;; A label can't start at a cpp directive. Check for
6079 ;; this, since c-forward-syntactic-ws would foul up on it.
6080 (unless (and c-opt-cpp-prefix
(looking-at c-opt-cpp-prefix
))
6081 (c-forward-syntactic-ws)
6082 (c-forward-label nil pte start
))))))))))
6084 ;; Point is still at the beginning of the possible label construct.
6086 ;; Check that the next nonsymbol token is ":", or that we're in one
6087 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
6088 ;; arguments. FIXME: Should build this regexp from the language
6091 ;; public: protected: private:
6093 (c-major-mode-is 'c
++-mode
)
6094 (search-forward-regexp
6095 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t
)
6096 (progn (backward-char)
6097 (c-forward-syntactic-ws limit
)
6098 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
6100 (setq label-type t
))
6101 ;; QT double keyword like "protected slots:" or goto target.
6102 ((progn (goto-char start
) nil
))
6103 ((when (c-syntactic-re-search-forward
6104 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t
) ; not at EOB
6106 (setq label-end
(point))
6108 (and (c-major-mode-is 'c
++-mode
)
6110 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
6111 (buffer-substring start
(point)))))
6112 (c-forward-syntactic-ws limit
)
6114 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
6117 (if (or (string= "signals" ; Special QT macro
6118 (setq kwd
(buffer-substring-no-properties start label-end
)))
6119 (string= "Q_SIGNALS" kwd
))
6123 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t
)
6124 (progn (c-forward-syntactic-ws limit
)
6125 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
6127 (setq label-type
'qt-2kwds-colon
)))))))
6130 (narrow-to-region start
(point))
6132 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
6136 (when (looking-at c-nonlabel-token-key
)
6138 (setq label-type nil
)
6139 (throw 'check-label nil
))
6140 (and (c-safe (c-forward-sexp)
6141 (c-forward-syntactic-ws)
6145 ;; Record the identifiers in the label for fontification, unless
6146 ;; it begins with `c-label-kwds' in which case the following
6147 ;; identifiers are part of a (constant) expression that
6148 ;; shouldn't be fontified.
6149 (when (and c-record-type-identifiers
6150 (progn (goto-char start
)
6151 (not (looking-at c-label-kwds-regexp
))))
6152 (while (c-syntactic-re-search-forward c-symbol-key nil t
)
6153 (c-record-ref-id (cons (match-beginning 0)
6156 (c-put-c-type-property (1- (point-max)) 'c-decl-end
)
6157 (goto-char (point-max)))))
6164 (defun c-forward-objc-directive ()
6165 ;; Assuming the point is at the beginning of a token, try to move
6166 ;; forward to the end of the Objective-C directive that starts
6167 ;; there. Return t if a directive was fully recognized, otherwise
6168 ;; the point is moved as far as one could be successfully parsed and
6171 ;; This function records identifier ranges on
6172 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6173 ;; `c-record-type-identifiers' is non-nil.
6175 ;; This function might do hidden buffer changes.
6177 (let ((start (point))
6179 (c-promote-possible-types t
)
6180 ;; Turn off recognition of angle bracket arglists while parsing
6181 ;; types here since the protocol reference list might then be
6182 ;; considered part of the preceding name or superclass-name.
6183 c-recognize-
<>-arglists
)
6188 (c-make-keywords-re t
6189 (append (c-lang-const c-protection-kwds objc
)
6192 (goto-char (match-end 1))
6198 (c-make-keywords-re t
6199 '("@interface" "@implementation" "@protocol")
6202 ;; Handle the name of the class itself.
6204 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
6206 (goto-char (match-end 0))
6211 ;; Look for ": superclass-name" or "( category-name )".
6212 (when (looking-at "[:\(]")
6213 (setq start-char
(char-after))
6215 (c-forward-syntactic-ws)
6216 (unless (c-forward-type) (throw 'break nil
))
6217 (when (eq start-char ?\
()
6218 (unless (eq (char-after) ?\
)) (throw 'break nil
))
6220 (c-forward-syntactic-ws)))
6222 ;; Look for a protocol reference list.
6223 (if (eq (char-after) ?
<)
6224 (let ((c-recognize-<>-arglists t
)
6225 (c-parse-and-markup-<>-arglists t
)
6226 c-restricted-
<>-arglists
)
6227 (c-forward-<>-arglist t
))
6231 (c-backward-syntactic-ws)
6232 (c-clear-c-type-property start
(1- (point)) 'c-decl-end
)
6233 (c-put-c-type-property (1- (point)) 'c-decl-end
)
6236 (c-clear-c-type-property start
(point) 'c-decl-end
)
6239 (defun c-beginning-of-inheritance-list (&optional lim
)
6240 ;; Go to the first non-whitespace after the colon that starts a
6241 ;; multiple inheritance introduction. Optional LIM is the farthest
6242 ;; back we should search.
6244 ;; This function might do hidden buffer changes.
6245 (c-with-syntax-table c
++-template-syntax-table
6246 (c-backward-token-2 0 t lim
)
6247 (while (and (or (looking-at c-symbol-start
)
6248 (looking-at "[<,]\\|::"))
6249 (zerop (c-backward-token-2 1 t lim
))))))
6251 (defun c-in-method-def-p ()
6252 ;; Return nil if we aren't in a method definition, otherwise the
6253 ;; position of the initial [+-].
6255 ;; This function might do hidden buffer changes.
6258 (and c-opt-method-key
6259 (looking-at c-opt-method-key
)
6263 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
6264 (defun c-in-gcc-asm-p ()
6265 ;; Return non-nil if point is within a gcc \"asm\" block.
6267 ;; This should be called with point inside an argument list.
6269 ;; Only one level of enclosing parentheses is considered, so for
6270 ;; instance `nil' is returned when in a function call within an asm
6273 ;; This function might do hidden buffer changes.
6275 (and c-opt-asm-stmt-key
6278 (backward-up-list 1)
6279 (c-beginning-of-statement-1 (point-min) nil t
)
6280 (looking-at c-opt-asm-stmt-key
))))
6282 (defun c-at-toplevel-p ()
6283 "Return a determination as to whether point is \"at the top level\".
6284 Informally, \"at the top level\" is anywhere where you can write
6287 More precisely, being at the top-level means that point is either
6288 outside any enclosing block (such as a function definition), or
6289 directly inside a class, namespace or other block that contains
6290 another declaration level.
6292 If point is not at the top-level (e.g. it is inside a method
6293 definition), then nil is returned. Otherwise, if point is at a
6294 top-level not enclosed within a class definition, t is returned.
6295 Otherwise, a 2-vector is returned where the zeroth element is the
6296 buffer position of the start of the class declaration, and the first
6297 element is the buffer position of the enclosing class's opening
6300 Note that this function might do hidden buffer changes. See the
6301 comment at the start of cc-engine.el for more info."
6302 (let ((paren-state (c-parse-state)))
6303 (or (not (c-most-enclosing-brace paren-state
))
6304 (c-search-uplist-for-classkey paren-state
))))
6306 (defun c-just-after-func-arglist-p (&optional lim
)
6307 ;; Return non-nil if the point is in the region after the argument
6308 ;; list of a function and its opening brace (or semicolon in case it
6309 ;; got no body). If there are K&R style argument declarations in
6310 ;; that region, the point has to be inside the first one for this
6311 ;; function to recognize it.
6313 ;; If successful, the point is moved to the first token after the
6314 ;; function header (see `c-forward-decl-or-cast-1' for details) and
6315 ;; the position of the opening paren of the function arglist is
6318 ;; The point is clobbered if not successful.
6320 ;; LIM is used as bound for backward buffer searches.
6322 ;; This function might do hidden buffer changes.
6324 (let ((beg (point)) end id-start
)
6326 (eq (c-beginning-of-statement-1 lim
) 'same
)
6328 (not (or (c-major-mode-is 'objc-mode
)
6329 (c-forward-objc-directive)))
6332 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
)))
6335 ;; There should not be a '=' or ',' between beg and the
6336 ;; start of the declaration since that means we were in the
6337 ;; "expression part" of the declaration.
6339 (not (looking-at "[=,]")))
6342 ;; Check that there's an arglist paren in the
6344 (goto-char id-start
)
6345 (cond ((eq (char-after) ?\
()
6346 ;; The declarator is a paren expression, so skip past it
6347 ;; so that we don't get stuck on that instead of the
6348 ;; function arglist.
6350 ((and c-opt-op-identifier-prefix
6351 (looking-at c-opt-op-identifier-prefix
))
6352 ;; Don't trip up on "operator ()".
6353 (c-forward-token-2 2 t
)))
6354 (and (< (point) beg
)
6355 (c-syntactic-re-search-forward "(" beg t t
)
6358 (defun c-in-knr-argdecl (&optional lim
)
6359 ;; Return the position of the first argument declaration if point is
6360 ;; inside a K&R style argument declaration list, nil otherwise.
6361 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
6362 ;; position that bounds the backward search for the argument list.
6364 ;; Point must be within a possible K&R region, e.g. just before a top-level
6365 ;; "{". It must be outside of parens and brackets. The test can return
6366 ;; false positives otherwise.
6368 ;; This function might do hidden buffer changes.
6372 ;; If we're in a macro, our search range is restricted to it. Narrow to
6373 ;; the searchable range.
6374 (let* ((macro-start (c-query-macro-start))
6375 (lim (max (or lim
(point-min)) (or macro-start
(point-min))))
6376 before-lparen after-rparen
6377 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
6378 (narrow-to-region lim
(c-point 'eol
))
6380 ;; Search backwards for the defun's argument list. We give up if we
6381 ;; encounter a "}" (end of a previous defun) or BOB.
6383 ;; The criterion for a paren structure being the arg list is:
6384 ;; o - there is non-WS stuff after it but before any "{"; AND
6385 ;; o - the token after it isn't a ";" AND
6386 ;; o - it is preceded by either an identifier (the function name) or
6387 ;; a macro expansion like "DEFUN (...)"; AND
6388 ;; o - its content is a non-empty comma-separated list of identifiers
6389 ;; (an empty arg list won't have a knr region).
6391 ;; The following snippet illustrates these rules:
6392 ;; int foo (bar, baz, yuk)
6394 ;; int (*baz) (my_type) ;
6395 ;; int (*) (void) (*yuk) (void) ;
6399 (while (> pp-count-out
0) ; go back one paren/bracket pair each time.
6400 (setq pp-count-out
(1- pp-count-out
))
6401 (c-syntactic-skip-backward "^)]}")
6402 (cond ((eq (char-before) ?\
))
6403 (setq after-rparen
(point)))
6404 ((eq (char-before) ?\
])
6405 (setq after-rparen nil
))
6406 (t ; either } (hit previous defun) or no more parens/brackets
6410 ;; We're inside a paren. Could it be our argument list....?
6414 (goto-char after-rparen
)
6415 (unless (c-go-list-backward) (throw 'knr nil
)) ;
6416 ;; FIXME!!! What about macros between the parens? 2007/01/20
6417 (setq before-lparen
(point)))
6419 ;; It can't be the arg list if next token is ; or {
6420 (progn (goto-char after-rparen
)
6421 (c-forward-syntactic-ws)
6422 (not (memq (char-after) '(?\
; ?\{))))
6424 ;; Is the thing preceding the list an identifier (the
6425 ;; function name), or a macro expansion?
6427 (goto-char before-lparen
)
6428 (eq (c-backward-token-2) 0)
6429 (or (c-on-identifier)
6430 (and (eq (char-after) ?\
))
6431 (c-go-up-list-backward)
6432 (eq (c-backward-token-2) 0)
6433 (c-on-identifier))))
6435 ;; Have we got a non-empty list of comma-separated
6438 (goto-char before-lparen
)
6439 (c-forward-token-2) ; to first token inside parens
6444 (while (eq (char-after) ?\
,)
6446 (unless (c-on-identifier) (throw 'id-list nil
))
6447 (c-forward-token-2))
6448 (eq (char-after) ?\
))))))
6450 ;; ...Yes. We've identified the function's argument list.
6452 (progn (goto-char after-rparen
)
6453 (c-forward-syntactic-ws)
6456 ;; ...No. The current parens aren't the function's arg list.
6457 (goto-char before-lparen
))
6459 (or (c-go-list-backward) ; backwards over [ .... ]
6460 (throw 'knr nil
)))))))))
6462 (defun c-skip-conditional ()
6463 ;; skip forward over conditional at point, including any predicate
6464 ;; statements in parentheses. No error checking is performed.
6466 ;; This function might do hidden buffer changes.
6467 (c-forward-sexp (cond
6469 ((looking-at (concat "\\<else"
6470 "\\([ \t\n]\\|\\\\\n\\)+"
6471 "if\\>\\([^_]\\|$\\)"))
6473 ;; do, else, try, finally
6474 ((looking-at (concat "\\<\\("
6475 "do\\|else\\|try\\|finally"
6476 "\\)\\>\\([^_]\\|$\\)"))
6478 ;; for, if, while, switch, catch, synchronized, foreach
6481 (defun c-after-conditional (&optional lim
)
6482 ;; If looking at the token after a conditional then return the
6483 ;; position of its start, otherwise return nil.
6485 ;; This function might do hidden buffer changes.
6487 (and (zerop (c-backward-token-2 1 t lim
))
6488 (or (looking-at c-block-stmt-1-key
)
6489 (and (eq (char-after) ?\
()
6490 (zerop (c-backward-token-2 1 t lim
))
6491 (looking-at c-block-stmt-2-key
)))
6494 (defun c-after-special-operator-id (&optional lim
)
6495 ;; If the point is after an operator identifier that isn't handled
6496 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
6497 ;; position of the start of that identifier is returned. nil is
6498 ;; returned otherwise. The point may be anywhere in the syntactic
6499 ;; whitespace after the last token of the operator identifier.
6501 ;; This function might do hidden buffer changes.
6503 (and c-overloadable-operators-regexp
6504 (zerop (c-backward-token-2 1 nil lim
))
6505 (looking-at c-overloadable-operators-regexp
)
6506 (or (not c-opt-op-identifier-prefix
)
6508 (zerop (c-backward-token-2 1 nil lim
))
6509 (looking-at c-opt-op-identifier-prefix
)))
6512 (defsubst c-backward-to-block-anchor
(&optional lim
)
6513 ;; Assuming point is at a brace that opens a statement block of some
6514 ;; kind, move to the proper anchor point for that block. It might
6515 ;; need to be adjusted further by c-add-stmt-syntax, but the
6516 ;; position at return is suitable as start position for that
6519 ;; This function might do hidden buffer changes.
6520 (unless (= (point) (c-point 'boi
))
6521 (let ((start (c-after-conditional lim
)))
6523 (goto-char start
)))))
6525 (defsubst c-backward-to-decl-anchor
(&optional lim
)
6526 ;; Assuming point is at a brace that opens the block of a top level
6527 ;; declaration of some kind, move to the proper anchor point for
6530 ;; This function might do hidden buffer changes.
6531 (unless (= (point) (c-point 'boi
))
6532 (c-beginning-of-statement-1 lim
)))
6534 (defun c-search-decl-header-end ()
6535 ;; Search forward for the end of the "header" of the current
6536 ;; declaration. That's the position where the definition body
6537 ;; starts, or the first variable initializer, or the ending
6538 ;; semicolon. I.e. search forward for the closest following
6539 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
6540 ;; _after_ the first found token, or at point-max if none is found.
6542 ;; This function might do hidden buffer changes.
6544 (let ((base (point)))
6545 (if (c-major-mode-is 'c
++-mode
)
6547 ;; In C++ we need to take special care to handle operator
6548 ;; tokens and those pesky template brackets.
6550 (c-syntactic-re-search-forward "[;{<=]" nil
'move t t
)
6552 (c-end-of-current-token base
)
6553 ;; Handle operator identifiers, i.e. ignore any
6554 ;; operator token preceded by "operator".
6556 (and (c-safe (c-backward-sexp) t
)
6557 (looking-at c-opt-op-identifier-prefix
)))
6558 (and (eq (char-before) ?
<)
6559 (c-with-syntax-table c
++-template-syntax-table
6560 (if (c-safe (goto-char (c-up-list-forward (point))))
6562 (goto-char (point-max))
6564 (setq base
(point)))
6567 (c-syntactic-re-search-forward "[;{=]" nil
'move t t
)
6568 (c-end-of-current-token base
))
6569 (setq base
(point))))))
6571 (defun c-beginning-of-decl-1 (&optional lim
)
6572 ;; Go to the beginning of the current declaration, or the beginning
6573 ;; of the previous one if already at the start of it. Point won't
6574 ;; be moved out of any surrounding paren. Return a cons cell of the
6575 ;; form (MOVE . KNR-POS). MOVE is like the return value from
6576 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
6577 ;; style argument declarations (and they are to be recognized) then
6578 ;; KNR-POS is set to the start of the first such argument
6579 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
6580 ;; position that bounds the backward search.
6582 ;; NB: Cases where the declaration continues after the block, as in
6583 ;; "struct foo { ... } bar;", are currently recognized as two
6584 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
6586 ;; This function might do hidden buffer changes.
6588 (let* ((start (point))
6589 (last-stmt-start (point))
6590 (move (c-beginning-of-statement-1 lim nil t
)))
6592 ;; `c-beginning-of-statement-1' stops at a block start, but we
6593 ;; want to continue if the block doesn't begin a top level
6594 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
6595 ;; or an open paren.
6596 (let ((beg (point)) tentative-move
)
6597 ;; Go back one "statement" each time round the loop until we're just
6598 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
6599 ;; an ObjC method. This will move over a multiple declaration whose
6600 ;; components are comma separated.
6602 ;; Must check with c-opt-method-key in ObjC mode.
6603 (not (and c-opt-method-key
6604 (looking-at c-opt-method-key
)))
6605 (/= last-stmt-start
(point))
6607 (c-backward-syntactic-ws lim
)
6608 (not (memq (char-before) '(?\
; ?} ?: nil))))
6611 (not (looking-at "\\s(")))
6612 ;; Check that we don't move from the first thing in a
6613 ;; macro to its header.
6614 (not (eq (setq tentative-move
6615 (c-beginning-of-statement-1 lim nil t
))
6617 (setq last-stmt-start beg
6619 move tentative-move
))
6622 (when c-recognize-knr-p
6623 (let ((fallback-pos (point)) knr-argdecl-start
)
6624 ;; Handle K&R argdecls. Back up after the "statement" jumped
6625 ;; over by `c-beginning-of-statement-1', unless it was the
6626 ;; function body, in which case we're sitting on the opening
6627 ;; brace now. Then test if we're in a K&R argdecl region and
6628 ;; that we started at the other side of the first argdecl in
6630 (unless (eq (char-after) ?
{)
6631 (goto-char last-stmt-start
))
6632 (if (and (setq knr-argdecl-start
(c-in-knr-argdecl lim
))
6633 (< knr-argdecl-start start
)
6635 (goto-char knr-argdecl-start
)
6636 (not (eq (c-beginning-of-statement-1 lim nil t
) 'macro
))))
6638 (cons (if (eq (char-after fallback-pos
) ?
{)
6642 (goto-char fallback-pos
))))
6644 ;; `c-beginning-of-statement-1' counts each brace block as a separate
6645 ;; statement, so the result will be 'previous if we've moved over any.
6646 ;; So change our result back to 'same if necessary.
6648 ;; If they were brace list initializers we might not have moved over a
6649 ;; declaration boundary though, so change it to 'same if we've moved
6650 ;; past a '=' before '{', but not ';'. (This ought to be integrated
6651 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
6652 ;; potentially can search over a large amount of text.). Take special
6653 ;; pains not to get mislead by C++'s "operator=", and the like.
6654 (if (and (eq move
'previous
)
6655 (c-with-syntax-table (if (c-major-mode-is 'c
++-mode
)
6656 c
++-template-syntax-table
6661 (while ; keep going back to "[;={"s until we either find
6662 ; no more, or get to one which isn't an "operator ="
6663 (and (c-syntactic-re-search-forward "[;={]" start t t t
)
6664 (eq (char-before) ?
=)
6665 c-overloadable-operators-regexp
6666 c-opt-op-identifier-prefix
6668 (eq (c-backward-token-2) 0)
6669 (looking-at c-overloadable-operators-regexp
)
6670 (eq (c-backward-token-2) 0)
6671 (looking-at c-opt-op-identifier-prefix
))))
6672 (eq (char-before) ?
=))
6673 (c-syntactic-re-search-forward "[;{]" start t t
)
6674 (eq (char-before) ?
{)
6675 (c-safe (goto-char (c-up-list-forward (point))) t
)
6676 (not (c-syntactic-re-search-forward ";" start t t
))))))
6680 (defun c-end-of-decl-1 ()
6681 ;; Assuming point is at the start of a declaration (as detected by
6682 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
6683 ;; `c-beginning-of-decl-1', this function handles the case when a
6684 ;; block is followed by identifiers in e.g. struct declarations in C
6685 ;; or C++. If a proper end was found then t is returned, otherwise
6686 ;; point is moved as far as possible within the current sexp and nil
6687 ;; is returned. This function doesn't handle macros; use
6688 ;; `c-end-of-macro' instead in those cases.
6690 ;; This function might do hidden buffer changes.
6691 (let ((start (point))
6692 (decl-syntax-table (if (c-major-mode-is 'c
++-mode
)
6693 c
++-template-syntax-table
6696 (c-search-decl-header-end)
6698 (when (and c-recognize-knr-p
6699 (eq (char-before) ?\
;)
6700 (c-in-knr-argdecl start
))
6701 ;; Stopped at the ';' in a K&R argdecl section which is
6702 ;; detected using the same criteria as in
6703 ;; `c-beginning-of-decl-1'. Move to the following block
6705 (c-syntactic-re-search-forward "{" nil
'move t
))
6707 (when (eq (char-before) ?
{)
6708 ;; Encountered a block in the declaration. Jump over it.
6710 (goto-char (c-up-list-forward (point)))
6711 (error (goto-char (point-max))
6712 (throw 'return nil
)))
6713 (if (or (not c-opt-block-decls-with-vars-key
)
6715 (c-with-syntax-table decl-syntax-table
6716 (let ((lim (point)))
6719 ;; Check for `c-opt-block-decls-with-vars-key'
6720 ;; before the first paren.
6721 (c-syntactic-re-search-forward
6722 (concat "[;=\(\[{]\\|\\("
6723 c-opt-block-decls-with-vars-key
6727 (not (eq (char-before) ?_
))
6728 ;; Check that the first following paren is
6730 (c-syntactic-re-search-forward "[;=\(\[{]"
6732 (eq (char-before) ?
{)))))))
6733 ;; The declaration doesn't have any of the
6734 ;; `c-opt-block-decls-with-vars' keywords in the
6735 ;; beginning, so it ends here at the end of the block.
6738 (c-with-syntax-table decl-syntax-table
6740 (if (eq (char-before) ?\
;)
6742 (c-syntactic-re-search-forward ";" nil
'move t
))))
6745 (defun c-looking-at-decl-block (containing-sexp goto-start
&optional limit
)
6746 ;; Assuming the point is at an open brace, check if it starts a
6747 ;; block that contains another declaration level, i.e. that isn't a
6748 ;; statement block or a brace list, and if so return non-nil.
6750 ;; If the check is successful, the return value is the start of the
6751 ;; keyword that tells what kind of construct it is, i.e. typically
6752 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
6753 ;; the point will be at the start of the construct, before any
6754 ;; leading specifiers, otherwise it's at the returned position.
6756 ;; The point is clobbered if the check is unsuccessful.
6758 ;; CONTAINING-SEXP is the position of the open of the surrounding
6759 ;; paren, or nil if none.
6761 ;; The optional LIMIT limits the backward search for the start of
6762 ;; the construct. It's assumed to be at a syntactically relevant
6765 ;; If any template arglists are found in the searched region before
6766 ;; the open brace, they get marked with paren syntax.
6768 ;; This function might do hidden buffer changes.
6770 (let ((open-brace (point)) kwd-start first-specifier-pos
)
6771 (c-syntactic-skip-backward c-block-prefix-charset limit t
)
6773 (when (and c-recognize-
<>-arglists
6774 (eq (char-before) ?
>))
6775 ;; Could be at the end of a template arglist.
6776 (let ((c-parse-and-markup-<>-arglists t
)
6777 (c-disallow-comma-in-<>-arglists
6778 (and containing-sexp
6779 (not (eq (char-after containing-sexp
) ?
{)))))
6781 (c-backward-<>-arglist nil limit
)
6783 (c-syntactic-skip-backward c-block-prefix-charset limit t
)
6784 (eq (char-before) ?
>))))))
6786 ;; Note: Can't get bogus hits inside template arglists below since they
6787 ;; have gotten paren syntax above.
6789 ;; If `goto-start' is set we begin by searching for the
6790 ;; first possible position of a leading specifier list.
6791 ;; The `c-decl-block-key' search continues from there since
6792 ;; we know it can't match earlier.
6794 (when (c-syntactic-re-search-forward c-symbol-start
6796 (goto-char (setq first-specifier-pos
(match-beginning 0)))
6801 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t
)
6802 (goto-char (setq kwd-start
(match-beginning 0)))
6805 ;; Found a keyword that can't be a type?
6808 ;; Can be a type too, in which case it's the return type of a
6809 ;; function (under the assumption that no declaration level
6810 ;; block construct starts with a type).
6811 (not (c-forward-type))
6813 ;; Jumped over a type, but it could be a declaration keyword
6814 ;; followed by the declared identifier that we've jumped over
6815 ;; instead (e.g. in "class Foo {"). If it indeed is a type
6816 ;; then we should be at the declarator now, so check for a
6817 ;; valid declarator start.
6819 ;; Note: This doesn't cope with the case when a declared
6820 ;; identifier is followed by e.g. '(' in a language where '('
6821 ;; also might be part of a declarator expression. Currently
6822 ;; there's no such language.
6823 (not (or (looking-at c-symbol-start
)
6824 (looking-at c-type-decl-prefix-key
)))))
6826 ;; In Pike a list of modifiers may be followed by a brace
6827 ;; to make them apply to many identifiers. Note that the
6828 ;; match data will be empty on return in this case.
6829 ((and (c-major-mode-is 'pike-mode
)
6831 (goto-char open-brace
)
6832 (= (c-backward-token-2) 0))
6833 (looking-at c-specifier-key
)
6834 ;; Use this variant to avoid yet another special regexp.
6835 (c-keyword-member (c-keyword-sym (match-string 1))
6837 (setq kwd-start
(point))
6843 ;; Back up over any preceding specifiers and their clauses
6844 ;; by going forward from `first-specifier-pos', which is the
6845 ;; earliest possible position where the specifier list can
6848 (goto-char first-specifier-pos
)
6850 (while (< (point) kwd-start
)
6851 (if (looking-at c-symbol-key
)
6852 ;; Accept any plain symbol token on the ground that
6853 ;; it's a specifier masked through a macro (just
6854 ;; like `c-forward-decl-or-cast-1' skip forward over
6857 ;; Could be more restrictive wrt invalid keywords,
6858 ;; but that'd only occur in invalid code so there's
6859 ;; no use spending effort on it.
6860 (let ((end (match-end 0)))
6861 (unless (c-forward-keyword-clause 0)
6863 (c-forward-syntactic-ws)))
6865 ;; Can't parse a declaration preamble and is still
6866 ;; before `kwd-start'. That means `first-specifier-pos'
6867 ;; was in some earlier construct. Search again.
6868 (if (c-syntactic-re-search-forward c-symbol-start
6870 (goto-char (setq first-specifier-pos
(match-beginning 0)))
6871 ;; Got no preamble before the block declaration keyword.
6872 (setq first-specifier-pos kwd-start
))))
6874 (goto-char first-specifier-pos
))
6875 (goto-char kwd-start
))
6879 (defun c-search-uplist-for-classkey (paren-state)
6880 ;; Check if the closest containing paren sexp is a declaration
6881 ;; block, returning a 2 element vector in that case. Aref 0
6882 ;; contains the bufpos at boi of the class key line, and aref 1
6883 ;; contains the bufpos of the open brace. This function is an
6884 ;; obsolete wrapper for `c-looking-at-decl-block'.
6886 ;; This function might do hidden buffer changes.
6887 (let ((open-paren-pos (c-most-enclosing-brace paren-state
)))
6888 (when open-paren-pos
6890 (goto-char open-paren-pos
)
6891 (when (and (eq (char-after) ?
{)
6892 (c-looking-at-decl-block
6893 (c-safe-position open-paren-pos paren-state
)
6895 (back-to-indentation)
6896 (vector (point) open-paren-pos
))))))
6898 (defun c-inside-bracelist-p (containing-sexp paren-state
)
6899 ;; return the buffer position of the beginning of the brace list
6900 ;; statement if we're inside a brace list, otherwise return nil.
6901 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
6902 ;; paren. PAREN-STATE is the remainder of the state of enclosing
6905 ;; N.B.: This algorithm can potentially get confused by cpp macros
6906 ;; placed in inconvenient locations. It's a trade-off we make for
6909 ;; This function might do hidden buffer changes.
6911 ;; This will pick up brace list declarations.
6914 (goto-char containing-sexp
)
6917 (if (and (or (looking-at c-brace-list-key
)
6918 (progn (c-forward-sexp -
1)
6919 (looking-at c-brace-list-key
)))
6920 (setq bracepos
(c-down-list-forward (point)))
6921 (not (c-crosses-statement-barrier-p (point)
6924 ;; this will pick up array/aggregate init lists, even if they are nested.
6927 ;; Pike can have class definitions anywhere, so we must
6928 ;; check for the class key here.
6929 (and (c-major-mode-is 'pike-mode
)
6931 bufpos braceassignp lim next-containing
)
6932 (while (and (not bufpos
)
6935 (if (consp (car paren-state
))
6936 (setq lim
(cdr (car paren-state
))
6937 paren-state
(cdr paren-state
))
6938 (setq lim
(car paren-state
)))
6940 (setq next-containing
(car paren-state
)
6941 paren-state
(cdr paren-state
))))
6942 (goto-char containing-sexp
)
6943 (if (c-looking-at-inexpr-block next-containing next-containing
)
6944 ;; We're in an in-expression block of some kind. Do not
6945 ;; check nesting. We deliberately set the limit to the
6946 ;; containing sexp, so that c-looking-at-inexpr-block
6947 ;; doesn't check for an identifier before it.
6948 (setq containing-sexp nil
)
6949 ;; see if the open brace is preceded by = or [...] in
6950 ;; this statement, but watch out for operator=
6951 (setq braceassignp
'dontknow
)
6952 (c-backward-token-2 1 t lim
)
6953 ;; Checks to do only on the first sexp before the brace.
6954 (when (and c-opt-inexpr-brace-list-key
6955 (eq (char-after) ?\
[))
6956 ;; In Java, an initialization brace list may follow
6957 ;; directly after "new Foo[]", so check for a "new"
6959 (while (eq braceassignp
'dontknow
)
6961 (cond ((/= (c-backward-token-2 1 t lim
) 0) nil
)
6962 ((looking-at c-opt-inexpr-brace-list-key
) t
)
6963 ((looking-at "\\sw\\|\\s_\\|[.[]")
6964 ;; Carry on looking if this is an
6965 ;; identifier (may contain "." in Java)
6966 ;; or another "[]" sexp.
6969 ;; Checks to do on all sexps before the brace, up to the
6970 ;; beginning of the statement.
6971 (while (eq braceassignp
'dontknow
)
6972 (cond ((eq (char-after) ?\
;)
6973 (setq braceassignp nil
))
6975 (looking-at class-key
))
6976 (setq braceassignp nil
))
6977 ((eq (char-after) ?
=)
6978 ;; We've seen a =, but must check earlier tokens so
6979 ;; that it isn't something that should be ignored.
6980 (setq braceassignp
'maybe
)
6981 (while (and (eq braceassignp
'maybe
)
6982 (zerop (c-backward-token-2 1 t lim
)))
6985 ;; Check for operator =
6986 ((and c-opt-op-identifier-prefix
6987 (looking-at c-opt-op-identifier-prefix
))
6989 ;; Check for `<opchar>= in Pike.
6990 ((and (c-major-mode-is 'pike-mode
)
6991 (or (eq (char-after) ?
`)
6992 ;; Special case for Pikes
6993 ;; `[]=, since '[' is not in
6994 ;; the punctuation class.
6995 (and (eq (char-after) ?\
[)
6996 (eq (char-before) ?
`))))
6998 ((looking-at "\\s.") 'maybe
)
6999 ;; make sure we're not in a C++ template
7000 ;; argument assignment
7002 (c-major-mode-is 'c
++-mode
)
7004 (let ((here (point))
7006 (skip-chars-backward "^<>")
7008 (and (eq (char-before) ?
<)
7009 (not (c-crosses-statement-barrier-p
7011 (not (c-in-literal))
7015 (if (and (eq braceassignp
'dontknow
)
7016 (/= (c-backward-token-2 1 t lim
) 0))
7017 (setq braceassignp nil
)))
7018 (if (not braceassignp
)
7019 (if (eq (char-after) ?\
;)
7020 ;; Brace lists can't contain a semicolon, so we're done.
7021 (setq containing-sexp nil
)
7023 (setq containing-sexp next-containing
7025 next-containing nil
))
7026 ;; we've hit the beginning of the aggregate list
7027 (c-beginning-of-statement-1
7028 (c-most-enclosing-brace paren-state
))
7029 (setq bufpos
(point))))
7034 (defun c-looking-at-special-brace-list (&optional lim
)
7035 ;; If we're looking at the start of a pike-style list, ie `({Â })',
7036 ;; `([Â ])', `(<Â >)' etc, a cons of a cons of its starting and ending
7037 ;; positions and its entry in c-special-brace-lists is returned, nil
7038 ;; otherwise. The ending position is nil if the list is still open.
7039 ;; LIM is the limit for forward search. The point may either be at
7040 ;; the `(' or at the following paren character. Tries to check the
7041 ;; matching closer, but assumes it's correct if no balanced paren is
7042 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
7043 ;; a special brace list).
7045 ;; This function might do hidden buffer changes.
7046 (if c-special-brace-lists
7051 (c-forward-syntactic-ws)
7052 (if (eq (char-after) ?\
()
7055 (c-forward-syntactic-ws)
7056 (setq inner-beg
(point))
7057 (setq type
(assq (char-after) c-special-brace-lists
)))
7058 (if (setq type
(assq (char-after) c-special-brace-lists
))
7060 (setq inner-beg
(point))
7061 (c-backward-syntactic-ws)
7063 (setq beg
(if (eq (char-after) ?\
()
7071 (= (char-before) ?\
)))
7073 (goto-char inner-beg
)
7074 (if (looking-at "\\s(")
7075 ;; Check balancing of the inner paren
7080 ;; If the inner char isn't a paren then
7081 ;; we can't check balancing, so just
7082 ;; check the char before the outer
7086 (c-backward-syntactic-ws)
7087 (= (char-before) (cdr type
)))))
7088 (if (or (/= (char-syntax (char-before)) ?\
))
7090 (c-forward-syntactic-ws)
7093 (cons (cons beg end
) type
))
7094 (cons (list beg
) type
)))))
7097 (defun c-looking-at-bos (&optional lim
)
7098 ;; Return non-nil if between two statements or declarations, assuming
7099 ;; point is not inside a literal or comment.
7101 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
7102 ;; are recommended instead.
7104 ;; This function might do hidden buffer changes.
7105 (c-at-statement-start-p))
7106 (make-obsolete 'c-looking-at-bos
'c-at-statement-start-p
)
7108 (defun c-looking-at-inexpr-block (lim containing-sexp
&optional check-at-end
)
7109 ;; Return non-nil if we're looking at the beginning of a block
7110 ;; inside an expression. The value returned is actually a cons of
7111 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
7112 ;; position of the beginning of the construct.
7114 ;; LIM limits the backward search. CONTAINING-SEXP is the start
7115 ;; position of the closest containing list. If it's nil, the
7116 ;; containing paren isn't used to decide whether we're inside an
7117 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
7118 ;; needs to be farther back.
7120 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
7121 ;; brace block might be done. It should only be used when the
7122 ;; construct can be assumed to be complete, i.e. when the original
7123 ;; starting position was further down than that.
7125 ;; This function might do hidden buffer changes.
7128 (let ((res 'maybe
) passed-paren
7129 (closest-lim (or containing-sexp lim
(point-min)))
7130 ;; Look at the character after point only as a last resort
7131 ;; when we can't disambiguate.
7132 (block-follows (and (eq (char-after) ?
{) (point))))
7134 (while (and (eq res
'maybe
)
7135 (progn (c-backward-syntactic-ws)
7136 (> (point) closest-lim
))
7138 (progn (backward-char)
7139 (looking-at "[\]\).]\\|\\w\\|\\s_"))
7140 (c-safe (forward-char)
7141 (goto-char (scan-sexps (point) -
1))))
7144 (if (looking-at c-keywords-regexp
)
7145 (let ((kw-sym (c-keyword-sym (match-string 1))))
7148 (c-keyword-member kw-sym
'c-inexpr-class-kwds
))
7149 (and (not (eq passed-paren ?\
[))
7150 (or (not (looking-at c-class-key
))
7151 ;; If the class definition is at the start of
7152 ;; a statement, we don't consider it an
7153 ;; in-expression class.
7154 (let ((prev (point)))
7156 (= (c-backward-token-2 1 nil closest-lim
) 0)
7157 (eq (char-syntax (char-after)) ?w
))
7158 (setq prev
(point)))
7160 (not (c-at-statement-start-p)))
7161 ;; Also, in Pike we treat it as an
7162 ;; in-expression class if it's used in an
7163 ;; object clone expression.
7166 (c-major-mode-is 'pike-mode
)
7167 (progn (goto-char block-follows
)
7168 (zerop (c-forward-token-2 1 t
)))
7169 (eq (char-after) ?\
())))
7170 (cons 'inexpr-class
(point))))
7171 ((c-keyword-member kw-sym
'c-inexpr-block-kwds
)
7172 (when (not passed-paren
)
7173 (cons 'inexpr-statement
(point))))
7174 ((c-keyword-member kw-sym
'c-lambda-kwds
)
7175 (when (or (not passed-paren
)
7176 (eq passed-paren ?\
())
7177 (cons 'inlambda
(point))))
7178 ((c-keyword-member kw-sym
'c-block-stmt-kwds
)
7183 (if (looking-at "\\s(")
7185 (if (and (eq passed-paren ?\
[)
7186 (eq (char-after) ?\
[))
7187 ;; Accept several square bracket sexps for
7188 ;; Java array initializations.
7190 (setq passed-paren
(char-after))
7195 (when (and c-recognize-paren-inexpr-blocks
7198 (eq (char-after containing-sexp
) ?\
())
7199 (goto-char containing-sexp
)
7200 (if (or (save-excursion
7201 (c-backward-syntactic-ws lim
)
7202 (and (> (point) (or lim
(point-min)))
7204 (and c-special-brace-lists
7205 (c-looking-at-special-brace-list)))
7207 (cons 'inexpr-statement
(point))))
7211 (defun c-looking-at-inexpr-block-backward (paren-state)
7212 ;; Returns non-nil if we're looking at the end of an in-expression
7213 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
7214 ;; PAREN-STATE is the paren state relevant at the current position.
7216 ;; This function might do hidden buffer changes.
7218 ;; We currently only recognize a block.
7219 (let ((here (point))
7220 (elem (car-safe paren-state
))
7222 (when (and (consp elem
)
7223 (progn (goto-char (cdr elem
))
7224 (c-forward-syntactic-ws here
)
7226 (goto-char (car elem
))
7227 (if (setq paren-state
(cdr paren-state
))
7228 (setq containing-sexp
(car-safe paren-state
)))
7229 (c-looking-at-inexpr-block (c-safe-position containing-sexp
7231 containing-sexp
)))))
7234 ;; `c-guess-basic-syntax' and the functions that precedes it below
7235 ;; implements the main decision tree for determining the syntactic
7236 ;; analysis of the current line of code.
7238 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
7239 ;; auto newline analysis.
7240 (defvar c-auto-newline-analysis nil
)
7242 (defun c-brace-anchor-point (bracepos)
7243 ;; BRACEPOS is the position of a brace in a construct like "namespace
7244 ;; Bar {". Return the anchor point in this construct; this is the
7245 ;; earliest symbol on the brace's line which isn't earlier than
7248 ;; Currently (2007-08-17), "like namespace" means "matches
7249 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
7250 ;; or anything like that.
7252 (let ((boi (c-point 'boi bracepos
)))
7253 (goto-char bracepos
)
7254 (while (and (> (point) boi
)
7255 (not (looking-at c-other-decl-block-key
)))
7256 (c-backward-token-2))
7257 (if (> (point) boi
) (point) boi
))))
7259 (defsubst c-add-syntax
(symbol &rest args
)
7260 ;; A simple function to prepend a new syntax element to
7261 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
7262 ;; should always be dynamically bound but since we read it first
7263 ;; we'll fail properly anyway if this function is misused.
7264 (setq c-syntactic-context
(cons (cons symbol args
)
7265 c-syntactic-context
)))
7267 (defsubst c-append-syntax
(symbol &rest args
)
7268 ;; Like `c-add-syntax' but appends to the end of the syntax list.
7269 ;; (Normally not necessary.)
7270 (setq c-syntactic-context
(nconc c-syntactic-context
7271 (list (cons symbol args
)))))
7273 (defun c-add-stmt-syntax (syntax-symbol
7278 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
7279 ;; needed with further syntax elements of the types `substatement',
7280 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
7281 ;; `defun-block-intro'.
7283 ;; Do the generic processing to anchor the given syntax symbol on
7284 ;; the preceding statement: Skip over any labels and containing
7285 ;; statements on the same line, and then search backward until we
7286 ;; find a statement or block start that begins at boi without a
7287 ;; label or comment.
7289 ;; Point is assumed to be at the prospective anchor point for the
7290 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
7291 ;; skip past open parens and containing statements. Most of the added
7292 ;; syntax elements will get the same anchor point - the exception is
7293 ;; for an anchor in a construct like "namespace"[*] - this is as early
7294 ;; as possible in the construct but on the same line as the {.
7296 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
7298 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
7299 ;; syntax symbol. They are appended after the anchor point.
7301 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
7302 ;; if the current statement starts there.
7304 ;; Note: It's not a problem if PAREN-STATE "overshoots"
7305 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
7307 ;; This function might do hidden buffer changes.
7309 (if (= (point) (c-point 'boi
))
7310 ;; This is by far the most common case, so let's give it special
7312 (apply 'c-add-syntax syntax-symbol
(point) syntax-extra-args
)
7314 (let ((syntax-last c-syntactic-context
)
7315 (boi (c-point 'boi
))
7316 ;; Set when we're on a label, so that we don't stop there.
7317 ;; FIXME: To be complete we should check if we're on a label
7318 ;; now at the start.
7321 ;; Use point as the anchor point for "namespace", "extern", etc.
7322 (apply 'c-add-syntax syntax-symbol
7323 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist
)
7327 ;; Loop while we have to back out of containing blocks.
7330 (catch 'back-up-block
7332 ;; Loop while we have to back up statements.
7333 (while (or (/= (point) boi
)
7335 (looking-at c-comment-start-regexp
))
7337 ;; Skip past any comments that stands between the
7338 ;; statement start and boi.
7339 (let ((savepos (point)))
7340 (while (and (/= savepos boi
)
7341 (c-backward-single-comment))
7342 (setq savepos
(point)
7343 boi
(c-point 'boi
)))
7344 (goto-char savepos
))
7346 ;; Skip to the beginning of this statement or backward
7348 (let ((old-pos (point))
7350 (step-type (c-beginning-of-statement-1 containing-sexp
)))
7351 (setq boi
(c-point 'boi
)
7352 on-label
(eq step-type
'label
))
7354 (cond ((= (point) old-pos
)
7355 ;; If we didn't move we're at the start of a block and
7356 ;; have to continue outside it.
7357 (throw 'back-up-block t
))
7359 ((and (eq step-type
'up
)
7360 (>= (point) old-boi
)
7361 (looking-at "else\\>[^_]")
7364 (looking-at "if\\>[^_]")))
7365 ;; Special case to avoid deeper and deeper indentation
7366 ;; of "else if" clauses.
7369 ((and (not stop-at-boi-only
)
7370 (/= old-pos old-boi
)
7371 (memq step-type
'(up previous
)))
7372 ;; If stop-at-boi-only is nil, we shouldn't back up
7373 ;; over previous or containing statements to try to
7374 ;; reach boi, so go back to the last position and
7377 (throw 'back-up-block nil
))
7380 (if (and (not stop-at-boi-only
)
7381 (memq step-type
'(up previous beginning
)))
7382 ;; If we've moved into another statement then we
7383 ;; should no longer try to stop in the middle of a
7385 (setq stop-at-boi-only t
))
7387 ;; Record this as a substatement if we skipped up one
7389 (when (eq step-type
'up
)
7390 (c-add-syntax 'substatement nil
))))
7395 ;; Now we have to go out of this block.
7396 (goto-char containing-sexp
)
7398 ;; Don't stop in the middle of a special brace list opener
7400 (when c-special-brace-lists
7401 (let ((special-list (c-looking-at-special-brace-list)))
7402 (when (and special-list
7403 (< (car (car special-list
)) (point)))
7404 (setq containing-sexp
(car (car special-list
)))
7405 (goto-char containing-sexp
))))
7407 (setq paren-state
(c-whack-state-after containing-sexp paren-state
)
7408 containing-sexp
(c-most-enclosing-brace paren-state
)
7411 ;; Analyze the construct in front of the block we've stepped out
7412 ;; from and add the right syntactic element for it.
7413 (let ((paren-pos (point))
7414 (paren-char (char-after))
7417 (if (eq paren-char ?\
()
7418 ;; Stepped out of a parenthesis block, so we're in an
7421 (when (/= paren-pos boi
)
7422 (if (and c-recognize-paren-inexpr-blocks
7424 (c-backward-syntactic-ws containing-sexp
)
7425 (or (not (looking-at "\\>"))
7426 (not (c-on-identifier))))
7428 (goto-char (1+ paren-pos
))
7429 (c-forward-syntactic-ws)
7430 (eq (char-after) ?
{)))
7431 ;; Stepped out of an in-expression statement. This
7432 ;; syntactic element won't get an anchor pos.
7433 (c-add-syntax 'inexpr-statement
)
7435 ;; A parenthesis normally belongs to an arglist.
7436 (c-add-syntax 'arglist-cont-nonempty nil paren-pos
)))
7440 (1+ containing-sexp
)
7442 (setq step-type
'same
7445 ;; Stepped out of a brace block.
7446 (setq step-type
(c-beginning-of-statement-1 containing-sexp
)
7447 on-label
(eq step-type
'label
))
7449 (if (and (eq step-type
'same
)
7450 (/= paren-pos
(point)))
7454 (goto-char paren-pos
)
7455 (setq inexpr
(c-looking-at-inexpr-block
7456 (c-safe-position containing-sexp paren-state
)
7458 (c-add-syntax (if (eq (car inexpr
) 'inlambda
)
7460 'statement-block-intro
)
7462 ((looking-at c-other-decl-block-key
)
7464 (cdr (assoc (match-string 1)
7465 c-other-decl-block-key-in-symbols-alist
))
7466 (max (c-point 'boi paren-pos
) (point))))
7467 (t (c-add-syntax 'defun-block-intro nil
))))
7469 (c-add-syntax 'statement-block-intro nil
)))
7471 (if (= paren-pos boi
)
7472 ;; Always done if the open brace was at boi. The
7473 ;; c-beginning-of-statement-1 call above is necessary
7474 ;; anyway, to decide the type of block-intro to add.
7475 (goto-char paren-pos
)
7476 (setq boi
(c-point 'boi
)))
7479 ;; Fill in the current point as the anchor for all the symbols
7481 (let ((p c-syntactic-context
) q
)
7482 (while (not (eq p syntax-last
))
7483 (setq q
(cdr (car p
))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
7491 (defun c-add-class-syntax (symbol
7492 containing-decl-open
7493 containing-decl-start
7496 ;; The inclass and class-close syntactic symbols are added in
7497 ;; several places and some work is needed to fix everything.
7498 ;; Therefore it's collected here.
7500 ;; This function might do hidden buffer changes.
7501 (goto-char containing-decl-open
)
7502 (if (and (eq symbol
'inclass
) (= (point) (c-point 'boi
)))
7504 (c-add-syntax symbol containing-decl-open
)
7505 containing-decl-open
)
7506 (goto-char containing-decl-start
)
7507 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
7508 ;; here, but we have to do like this for compatibility.
7509 (back-to-indentation)
7510 (c-add-syntax symbol
(point))
7511 (if (and (c-keyword-member containing-decl-kwd
7512 'c-inexpr-class-kwds
)
7513 (/= containing-decl-start
(c-point 'boi containing-decl-start
)))
7514 (c-add-syntax 'inexpr-class
))
7517 (defun c-guess-continued-construct (indent-point
7519 beg-of-same-or-containing-stmt
7522 ;; This function contains the decision tree reached through both
7523 ;; cases 18 and 10. It's a continued statement or top level
7524 ;; construct of some kind.
7526 ;; This function might do hidden buffer changes.
7528 (let (special-brace-list)
7529 (goto-char indent-point
)
7530 (skip-chars-forward " \t")
7533 ;; (CASE A removed.)
7534 ;; CASE B: open braces for class or brace-lists
7535 ((setq special-brace-list
7536 (or (and c-special-brace-lists
7537 (c-looking-at-special-brace-list))
7538 (eq char-after-ip ?
{)))
7541 ;; CASE B.1: class-open
7543 (and (eq (char-after) ?
{)
7544 (c-looking-at-decl-block containing-sexp t
)
7545 (setq beg-of-same-or-containing-stmt
(point))))
7546 (c-add-syntax 'class-open beg-of-same-or-containing-stmt
))
7548 ;; CASE B.2: brace-list-open
7549 ((or (consp special-brace-list
)
7551 (goto-char beg-of-same-or-containing-stmt
)
7552 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
7553 indent-point t t t
)))
7554 ;; The most semantically accurate symbol here is
7555 ;; brace-list-open, but we normally report it simply as a
7556 ;; statement-cont. The reason is that one normally adjusts
7557 ;; brace-list-open for brace lists as top-level constructs,
7558 ;; and brace lists inside statements is a completely different
7559 ;; context. C.f. case 5A.3.
7560 (c-beginning-of-statement-1 containing-sexp
)
7561 (c-add-stmt-syntax (if c-auto-newline-analysis
7562 ;; Turn off the dwim above when we're
7563 ;; analyzing the nature of the brace
7564 ;; for the auto newline feature.
7568 containing-sexp paren-state
))
7570 ;; CASE B.3: The body of a function declared inside a normal
7571 ;; block. Can occur e.g. in Pike and when using gcc
7572 ;; extensions, but watch out for macros followed by blocks.
7573 ;; C.f. cases E, 16F and 17G.
7574 ((and (not (c-at-statement-start-p))
7575 (eq (c-beginning-of-statement-1 containing-sexp nil nil t
)
7578 (let ((c-recognize-typeless-decls nil
))
7579 ;; Turn off recognition of constructs that lacks a
7580 ;; type in this case, since that's more likely to be
7581 ;; a macro followed by a block.
7582 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
7583 (c-add-stmt-syntax 'defun-open nil t
7584 containing-sexp paren-state
))
7586 ;; CASE B.4: Continued statement with block open. The most
7587 ;; accurate analysis is perhaps `statement-cont' together with
7588 ;; `block-open' but we play DWIM and use `substatement-open'
7589 ;; instead. The rationaly is that this typically is a macro
7590 ;; followed by a block which makes it very similar to a
7591 ;; statement with a substatement block.
7593 (c-add-stmt-syntax 'substatement-open nil nil
7594 containing-sexp paren-state
))
7597 ;; CASE C: iostream insertion or extraction operator
7598 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
7600 (goto-char beg-of-same-or-containing-stmt
)
7601 ;; If there is no preceding streamop in the statement
7602 ;; then indent this line as a normal statement-cont.
7603 (when (c-syntactic-re-search-forward
7604 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point
'move t t
)
7605 (c-add-syntax 'stream-op
(c-point 'boi
))
7608 ;; CASE E: In the "K&R region" of a function declared inside a
7609 ;; normal block. C.f. case B.3.
7610 ((and (save-excursion
7611 ;; Check that the next token is a '{'. This works as
7612 ;; long as no language that allows nested function
7613 ;; definitions allows stuff like member init lists, K&R
7614 ;; declarations or throws clauses there.
7616 ;; Note that we do a forward search for something ahead
7617 ;; of the indentation line here. That's not good since
7618 ;; the user might not have typed it yet. Unfortunately
7619 ;; it's exceedingly tricky to recognize a function
7620 ;; prototype in a code block without resorting to this.
7621 (c-forward-syntactic-ws)
7622 (eq (char-after) ?
{))
7623 (not (c-at-statement-start-p))
7624 (eq (c-beginning-of-statement-1 containing-sexp nil nil t
)
7627 (let ((c-recognize-typeless-decls nil
))
7628 ;; Turn off recognition of constructs that lacks a
7629 ;; type in this case, since that's more likely to be
7630 ;; a macro followed by a block.
7631 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
7632 (c-add-stmt-syntax 'func-decl-cont nil t
7633 containing-sexp paren-state
))
7635 ;; CASE D: continued statement.
7637 (c-beginning-of-statement-1 containing-sexp
)
7638 (c-add-stmt-syntax 'statement-cont nil nil
7639 containing-sexp paren-state
))
7642 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
7645 (defun c-guess-basic-syntax ()
7646 "Return the syntactic context of the current line."
7649 (c-save-buffer-state
7650 ((indent-point (point))
7651 (case-fold-search nil
)
7652 ;; A whole ugly bunch of various temporary variables. Have
7653 ;; to declare them here since it's not possible to declare
7654 ;; a variable with only the scope of a cond test and the
7655 ;; following result clauses, and most of this function is a
7656 ;; single gigantic cond. :P
7657 literal char-before-ip before-ws-ip char-after-ip macro-start
7658 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
7659 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
7660 ;; The following record some positions for the containing
7661 ;; declaration block if we're directly within one:
7662 ;; `containing-decl-open' is the position of the open
7663 ;; brace. `containing-decl-start' is the start of the
7664 ;; declaration. `containing-decl-kwd' is the keyword
7665 ;; symbol of the keyword that tells what kind of block it
7667 containing-decl-open
7668 containing-decl-start
7670 ;; The open paren of the closest surrounding sexp or nil if
7673 ;; The position after the closest preceding brace sexp
7674 ;; (nested sexps are ignored), or the position after
7675 ;; `containing-sexp' if there is none, or (point-min) if
7676 ;; `containing-sexp' is nil.
7678 ;; The paren state outside `containing-sexp', or at
7679 ;; `indent-point' if `containing-sexp' is nil.
7680 (paren-state (c-parse-state))
7681 ;; There's always at most one syntactic element which got
7682 ;; an anchor pos. It's stored in syntactic-relpos.
7684 (c-stmt-delim-chars c-stmt-delim-chars
))
7686 ;; Check if we're directly inside an enclosing declaration
7688 (when (and (setq containing-sexp
7689 (c-most-enclosing-brace paren-state
))
7691 (goto-char containing-sexp
)
7692 (eq (char-after) ?
{))
7694 (c-looking-at-decl-block
7695 (c-most-enclosing-brace paren-state
7698 (setq containing-decl-open containing-sexp
7699 containing-decl-start
(point)
7700 containing-sexp nil
)
7701 (goto-char placeholder
)
7702 (setq containing-decl-kwd
(and (looking-at c-keywords-regexp
)
7703 (c-keyword-sym (match-string 1)))))
7705 ;; Init some position variables.
7708 (setq containing-sexp
(car paren-state
)
7709 paren-state
(cdr paren-state
))
7710 (if (consp containing-sexp
)
7712 (setq lim
(cdr containing-sexp
))
7713 (if (cdr c-state-cache
)
7714 ;; Ignore balanced paren. The next entry
7715 ;; can't be another one.
7716 (setq containing-sexp
(car (cdr c-state-cache
))
7717 paren-state
(cdr paren-state
))
7718 ;; If there is no surrounding open paren then
7719 ;; put the last balanced pair back on paren-state.
7720 (setq paren-state
(cons containing-sexp paren-state
)
7721 containing-sexp nil
)))
7722 (setq lim
(1+ containing-sexp
))))
7723 (setq lim
(point-min)))
7725 ;; If we're in a parenthesis list then ',' delimits the
7726 ;; "statements" rather than being an operator (with the
7727 ;; exception of the "for" clause). This difference is
7728 ;; typically only noticeable when statements are used in macro
7730 (when (and containing-sexp
7731 (eq (char-after containing-sexp
) ?\
())
7732 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma
))
7734 ;; cache char before and after indent point, and move point to
7735 ;; the most likely position to perform the majority of tests
7736 (goto-char indent-point
)
7737 (c-backward-syntactic-ws lim
)
7738 (setq before-ws-ip
(point)
7739 char-before-ip
(char-before))
7740 (goto-char indent-point
)
7741 (skip-chars-forward " \t")
7742 (setq char-after-ip
(char-after))
7744 ;; are we in a literal?
7745 (setq literal
(c-in-literal lim
))
7747 ;; now figure out syntactic qualities of the current line
7750 ;; CASE 1: in a string.
7751 ((eq literal
'string
)
7752 (c-add-syntax 'string
(c-point 'bopl
)))
7754 ;; CASE 2: in a C or C++ style comment.
7755 ((and (memq literal
'(c c
++))
7756 ;; This is a kludge for XEmacs where we use
7757 ;; `buffer-syntactic-context', which doesn't correctly
7758 ;; recognize "\*/" to end a block comment.
7759 ;; `parse-partial-sexp' which is used by
7760 ;; `c-literal-limits' will however do that in most
7761 ;; versions, which results in that we get nil from
7762 ;; `c-literal-limits' even when `c-in-literal' claims
7763 ;; we're inside a comment.
7764 (setq placeholder
(c-literal-limits lim
)))
7765 (c-add-syntax literal
(car placeholder
)))
7767 ;; CASE 3: in a cpp preprocessor macro continuation.
7768 ((and (save-excursion
7769 (when (c-beginning-of-macro)
7770 (setq macro-start
(point))))
7771 (/= macro-start
(c-point 'boi
))
7773 (setq tmpsymbol
'cpp-macro-cont
)
7774 (or (not c-syntactic-indentation-in-macros
)
7776 (goto-char macro-start
)
7777 ;; If at the beginning of the body of a #define
7778 ;; directive then analyze as cpp-define-intro
7779 ;; only. Go on with the syntactic analysis
7780 ;; otherwise. in-macro-expr is set if we're in a
7781 ;; cpp expression, i.e. before the #define body
7782 ;; or anywhere in a non-#define directive.
7783 (if (c-forward-to-cpp-define-body)
7784 (let ((indent-boi (c-point 'boi indent-point
)))
7785 (setq in-macro-expr
(> (point) indent-boi
)
7786 tmpsymbol
'cpp-define-intro
)
7787 (= (point) indent-boi
))
7788 (setq in-macro-expr t
)
7790 (c-add-syntax tmpsymbol macro-start
)
7791 (setq macro-start nil
))
7793 ;; CASE 11: an else clause?
7794 ((looking-at "else\\>[^_]")
7795 (c-beginning-of-statement-1 containing-sexp
)
7796 (c-add-stmt-syntax 'else-clause nil t
7797 containing-sexp paren-state
))
7799 ;; CASE 12: while closure of a do/while construct?
7800 ((and (looking-at "while\\>[^_]")
7802 (prog1 (eq (c-beginning-of-statement-1 containing-sexp
)
7804 (setq placeholder
(point)))))
7805 (goto-char placeholder
)
7806 (c-add-stmt-syntax 'do-while-closure nil t
7807 containing-sexp paren-state
))
7809 ;; CASE 13: A catch or finally clause? This case is simpler
7810 ;; than if-else and do-while, because a block is required
7811 ;; after every try, catch and finally.
7813 (and (cond ((c-major-mode-is 'c
++-mode
)
7814 (looking-at "catch\\>[^_]"))
7815 ((c-major-mode-is 'java-mode
)
7816 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
7817 (and (c-safe (c-backward-syntactic-ws)
7820 (eq (char-after) ?
{)
7821 (c-safe (c-backward-syntactic-ws)
7824 (if (eq (char-after) ?\
()
7825 (c-safe (c-backward-sexp) t
)
7827 (looking-at "\\(try\\|catch\\)\\>[^_]")
7828 (setq placeholder
(point))))
7829 (goto-char placeholder
)
7830 (c-add-stmt-syntax 'catch-clause nil t
7831 containing-sexp paren-state
))
7833 ;; CASE 18: A substatement we can recognize by keyword.
7835 (and c-opt-block-stmt-key
7836 (not (eq char-before-ip ?\
;))
7837 (not (c-at-vsemi-p before-ws-ip
))
7838 (not (memq char-after-ip
'(?\
) ?\
] ?
,)))
7839 (or (not (eq char-before-ip ?
}))
7840 (c-looking-at-inexpr-block-backward c-state-cache
))
7843 ;; Ought to cache the result from the
7844 ;; c-beginning-of-statement-1 calls here.
7845 (setq placeholder
(point))
7846 (while (eq (setq step-type
7847 (c-beginning-of-statement-1 lim
))
7849 (if (eq step-type
'previous
)
7850 (goto-char placeholder
)
7851 (setq placeholder
(point))
7852 (if (and (eq step-type
'same
)
7853 (not (looking-at c-opt-block-stmt-key
)))
7854 ;; Step up to the containing statement if we
7855 ;; stayed in the same one.
7859 (c-beginning-of-statement-1 lim
))
7862 (setq placeholder
(point))
7863 ;; There was no containing statement afterall.
7864 (goto-char placeholder
)))))
7866 (if (looking-at c-block-stmt-2-key
)
7867 ;; Require a parenthesis after these keywords.
7868 ;; Necessary to catch e.g. synchronized in Java,
7869 ;; which can be used both as statement and
7871 (and (zerop (c-forward-token-2 1 nil
))
7872 (eq (char-after) ?\
())
7873 (looking-at c-opt-block-stmt-key
))))
7875 (if (eq step-type
'up
)
7876 ;; CASE 18A: Simple substatement.
7878 (goto-char placeholder
)
7880 ((eq char-after-ip ?
{)
7881 (c-add-stmt-syntax 'substatement-open nil nil
7882 containing-sexp paren-state
))
7884 (goto-char indent-point
)
7885 (back-to-indentation)
7887 (c-add-stmt-syntax 'substatement-label nil nil
7888 containing-sexp paren-state
))
7890 (c-add-stmt-syntax 'substatement nil nil
7891 containing-sexp paren-state
))))
7893 ;; CASE 18B: Some other substatement. This is shared
7895 (c-guess-continued-construct indent-point
7901 ;; CASE 14: A case or default label
7902 ((looking-at c-label-kwds-regexp
)
7905 (goto-char containing-sexp
)
7906 (setq lim
(c-most-enclosing-brace c-state-cache
7908 (c-backward-to-block-anchor lim
)
7909 (c-add-stmt-syntax 'case-label nil t lim paren-state
))
7910 ;; Got a bogus label at the top level. In lack of better
7911 ;; alternatives, anchor it on (point-min).
7912 (c-add-syntax 'case-label
(point-min))))
7914 ;; CASE 15: any other label
7916 (back-to-indentation)
7917 (and (not (looking-at c-syntactic-ws-start
))
7919 (cond (containing-decl-open
7920 (setq placeholder
(c-add-class-syntax 'inclass
7921 containing-decl-open
7922 containing-decl-start
7925 ;; Append access-label with the same anchor point as
7927 (c-append-syntax 'access-label placeholder
))
7930 (goto-char containing-sexp
)
7931 (setq lim
(c-most-enclosing-brace c-state-cache
7935 (if (and (eq (c-beginning-of-statement-1 lim
) 'up
)
7936 (looking-at "switch\\>[^_]"))
7937 ;; If the surrounding statement is a switch then
7938 ;; let's analyze all labels as switch labels, so
7939 ;; that they get lined up consistently.
7942 (c-backward-to-block-anchor lim
)
7943 (c-add-stmt-syntax tmpsymbol nil t lim paren-state
))
7946 ;; A label on the top level. Treat it as a class
7947 ;; context. (point-min) is the closest we get to the
7948 ;; class open brace.
7949 (c-add-syntax 'access-label
(point-min)))))
7951 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
7953 ((setq placeholder
(c-looking-at-inexpr-block
7954 (c-safe-position containing-sexp paren-state
)
7956 ;; Have to turn on the heuristics after
7957 ;; the point even though it doesn't work
7958 ;; very well. C.f. test case class-16.pike.
7960 (setq tmpsymbol
(assq (car placeholder
)
7961 '((inexpr-class . class-open
)
7962 (inexpr-statement . block-open
))))
7964 ;; It's a statement block or an anonymous class.
7965 (setq tmpsymbol
(cdr tmpsymbol
))
7966 ;; It's a Pike lambda. Check whether we are between the
7967 ;; lambda keyword and the argument list or at the defun
7969 (setq tmpsymbol
(if (eq char-after-ip ?
{)
7971 'lambda-intro-cont
)))
7972 (goto-char (cdr placeholder
))
7973 (back-to-indentation)
7974 (c-add-stmt-syntax tmpsymbol nil t
7975 (c-most-enclosing-brace c-state-cache
(point))
7977 (unless (eq (point) (cdr placeholder
))
7978 (c-add-syntax (car placeholder
))))
7980 ;; CASE 5: Line is inside a declaration level block or at top level.
7981 ((or containing-decl-open
(null containing-sexp
))
7984 ;; CASE 5A: we are looking at a defun, brace list, class,
7985 ;; or inline-inclass method opening brace
7986 ((setq special-brace-list
7987 (or (and c-special-brace-lists
7988 (c-looking-at-special-brace-list))
7989 (eq char-after-ip ?
{)))
7992 ;; CASE 5A.1: Non-class declaration block open.
7995 (and (eq char-after-ip ?
{)
7996 (setq tmp
(c-looking-at-decl-block containing-sexp t
))
7998 (setq placeholder
(point))
8000 (looking-at c-symbol-key
))
8002 (c-keyword-sym (setq keyword
(match-string 0)))
8003 'c-other-block-decl-kwds
))))
8004 (goto-char placeholder
)
8006 (if (string-equal keyword
"extern")
8007 ;; Special case for extern-lang-open.
8009 (intern (concat keyword
"-open")))
8010 nil t containing-sexp paren-state
))
8012 ;; CASE 5A.2: we are looking at a class opening brace
8014 (goto-char indent-point
)
8015 (skip-chars-forward " \t")
8016 (and (eq (char-after) ?
{)
8017 (c-looking-at-decl-block containing-sexp t
)
8018 (setq placeholder
(point))))
8019 (c-add-syntax 'class-open placeholder
))
8021 ;; CASE 5A.3: brace list open
8023 (c-beginning-of-decl-1 lim
)
8024 (while (looking-at c-specifier-key
)
8025 (goto-char (match-end 1))
8026 (c-forward-syntactic-ws indent-point
))
8027 (setq placeholder
(c-point 'boi
))
8028 (or (consp special-brace-list
)
8029 (and (or (save-excursion
8030 (goto-char indent-point
)
8031 (setq tmpsymbol nil
)
8032 (while (and (> (point) placeholder
)
8033 (zerop (c-backward-token-2 1 t
))
8034 (/= (char-after) ?
=))
8035 (and c-opt-inexpr-brace-list-key
8037 (looking-at c-opt-inexpr-brace-list-key
)
8038 (setq tmpsymbol
'topmost-intro-cont
)))
8039 (eq (char-after) ?
=))
8040 (looking-at c-brace-list-key
))
8042 (while (and (< (point) indent-point
)
8043 (zerop (c-forward-token-2 1 t
))
8044 (not (memq (char-after) '(?\
; ?\()))))
8045 (not (memq (char-after) '(?\
; ?\()))
8047 (if (and (not c-auto-newline-analysis
)
8048 (c-major-mode-is 'java-mode
)
8049 (eq tmpsymbol
'topmost-intro-cont
))
8050 ;; We're in Java and have found that the open brace
8051 ;; belongs to a "new Foo[]" initialization list,
8052 ;; which means the brace list is part of an
8053 ;; expression and not a top level definition. We
8054 ;; therefore treat it as any topmost continuation
8055 ;; even though the semantically correct symbol still
8056 ;; is brace-list-open, on the same grounds as in
8059 (c-beginning-of-statement-1 lim
)
8060 (c-add-syntax 'topmost-intro-cont
(c-point 'boi
)))
8061 (c-add-syntax 'brace-list-open placeholder
)))
8063 ;; CASE 5A.4: inline defun open
8064 ((and containing-decl-open
8065 (not (c-keyword-member containing-decl-kwd
8066 'c-other-block-decl-kwds
)))
8067 (c-add-syntax 'inline-open
)
8068 (c-add-class-syntax 'inclass
8069 containing-decl-open
8070 containing-decl-start
8074 ;; CASE 5A.5: ordinary defun open
8077 (c-beginning-of-decl-1 lim
)
8078 (while (looking-at c-specifier-key
)
8079 (goto-char (match-end 1))
8080 (c-forward-syntactic-ws indent-point
))
8081 (c-add-syntax 'defun-open
(c-point 'boi
))
8082 ;; Bogus to use bol here, but it's the legacy. (Resolved,
8086 ;; CASE 5B: After a function header but before the body (or
8087 ;; the ending semicolon if there's no body).
8089 (when (setq placeholder
(c-just-after-func-arglist-p lim
))
8090 (setq tmp-pos
(point))))
8093 ;; CASE 5B.1: Member init list.
8094 ((eq (char-after tmp-pos
) ?
:)
8095 (if (or (> tmp-pos indent-point
)
8096 (= (c-point 'bosws
) (1+ tmp-pos
)))
8098 ;; There is no preceding member init clause.
8099 ;; Indent relative to the beginning of indentation
8100 ;; for the topmost-intro line that contains the
8101 ;; prototype's open paren.
8102 (goto-char placeholder
)
8103 (c-add-syntax 'member-init-intro
(c-point 'boi
)))
8104 ;; Indent relative to the first member init clause.
8105 (goto-char (1+ tmp-pos
))
8106 (c-forward-syntactic-ws)
8107 (c-add-syntax 'member-init-cont
(point))))
8109 ;; CASE 5B.2: K&R arg decl intro
8110 ((and c-recognize-knr-p
8111 (c-in-knr-argdecl lim
))
8112 (c-beginning-of-statement-1 lim
)
8113 (c-add-syntax 'knr-argdecl-intro
(c-point 'boi
))
8114 (if containing-decl-open
8115 (c-add-class-syntax 'inclass
8116 containing-decl-open
8117 containing-decl-start
8121 ;; CASE 5B.4: Nether region after a C++ or Java func
8122 ;; decl, which could include a `throws' declaration.
8124 (c-beginning-of-statement-1 lim
)
8125 (c-add-syntax 'func-decl-cont
(c-point 'boi
))
8128 ;; CASE 5C: inheritance line. could be first inheritance
8129 ;; line, or continuation of a multiple inheritance
8130 ((or (and (c-major-mode-is 'c
++-mode
)
8132 (when (eq char-after-ip ?
,)
8133 (skip-chars-forward " \t")
8135 (looking-at c-opt-postfix-decl-spec-key
)))
8136 (and (or (eq char-before-ip ?
:)
8137 ;; watch out for scope operator
8139 (and (eq char-after-ip ?
:)
8140 (c-safe (forward-char 1) t
)
8141 (not (eq (char-after) ?
:))
8144 (c-backward-syntactic-ws lim
)
8145 (if (eq char-before-ip ?
:)
8148 (c-backward-syntactic-ws lim
)))
8149 (back-to-indentation)
8150 (looking-at c-class-key
)))
8152 (and (c-major-mode-is 'java-mode
)
8153 (let ((fence (save-excursion
8154 (c-beginning-of-statement-1 lim
)
8159 (cond ((looking-at c-opt-postfix-decl-spec-key
)
8160 (setq injava-inher
(cons cont
(point))
8162 ((or (not (c-safe (c-forward-sexp -
1) t
))
8168 (not (c-crosses-statement-barrier-p (cdr injava-inher
)
8173 ;; CASE 5C.1: non-hanging colon on an inher intro
8174 ((eq char-after-ip ?
:)
8175 (c-beginning-of-statement-1 lim
)
8176 (c-add-syntax 'inher-intro
(c-point 'boi
))
8177 ;; don't add inclass symbol since relative point already
8178 ;; contains any class offset
8181 ;; CASE 5C.2: hanging colon on an inher intro
8182 ((eq char-before-ip ?
:)
8183 (c-beginning-of-statement-1 lim
)
8184 (c-add-syntax 'inher-intro
(c-point 'boi
))
8185 (if containing-decl-open
8186 (c-add-class-syntax 'inclass
8187 containing-decl-open
8188 containing-decl-start
8192 ;; CASE 5C.3: in a Java implements/extends
8194 (let ((where (cdr injava-inher
))
8195 (cont (car injava-inher
)))
8197 (cond ((looking-at "throws\\>[^_]")
8198 (c-add-syntax 'func-decl-cont
8199 (progn (c-beginning-of-statement-1 lim
)
8201 (cont (c-add-syntax 'inher-cont where
))
8202 (t (c-add-syntax 'inher-intro
8203 (progn (goto-char (cdr injava-inher
))
8204 (c-beginning-of-statement-1 lim
)
8208 ;; CASE 5C.4: a continued inheritance line
8210 (c-beginning-of-inheritance-list lim
)
8211 (c-add-syntax 'inher-cont
(point))
8212 ;; don't add inclass symbol since relative point already
8213 ;; contains any class offset
8216 ;; CASE 5D: this could be a top-level initialization, a
8217 ;; member init list continuation, or a template argument
8218 ;; list continuation.
8220 ;; Note: We use the fact that lim is always after any
8221 ;; preceding brace sexp.
8222 (if c-recognize-
<>-arglists
8225 (c-syntactic-skip-backward "^;,=<>" lim t
)
8228 (when c-overloadable-operators-regexp
8229 (when (setq placeholder
(c-after-special-operator-id lim
))
8230 (goto-char placeholder
)
8233 ((eq (char-before) ?
>)
8234 (or (c-backward-<>-arglist nil lim
)
8237 ((eq (char-before) ?
<)
8240 (c-forward-<>-arglist nil
))
8241 (progn (forward-char)
8245 ;; NB: No c-after-special-operator-id stuff in this
8246 ;; clause - we assume only C++ needs it.
8247 (c-syntactic-skip-backward "^;,=" lim t
))
8248 (memq (char-before) '(?
, ?
= ?
<)))
8251 ;; CASE 5D.3: perhaps a template list continuation?
8252 ((and (c-major-mode-is 'c
++-mode
)
8255 (c-with-syntax-table c
++-template-syntax-table
8256 (goto-char indent-point
)
8257 (setq placeholder
(c-up-list-backward))
8259 (eq (char-after placeholder
) ?
<))))))
8260 (c-with-syntax-table c
++-template-syntax-table
8261 (goto-char placeholder
)
8262 (c-beginning-of-statement-1 lim t
)
8264 (c-backward-syntactic-ws lim
)
8265 (eq (char-before) ?
<))
8266 ;; In a nested template arglist.
8268 (goto-char placeholder
)
8269 (c-syntactic-skip-backward "^,;" lim t
)
8270 (c-forward-syntactic-ws))
8271 (back-to-indentation)))
8272 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8274 (c-add-syntax 'template-args-cont
(point)))
8276 ;; CASE 5D.4: perhaps a multiple inheritance line?
8277 ((and (c-major-mode-is 'c
++-mode
)
8279 (c-beginning-of-statement-1 lim
)
8280 (setq placeholder
(point))
8281 (if (looking-at "static\\>[^_]")
8282 (c-forward-token-2 1 nil indent-point
))
8283 (and (looking-at c-class-key
)
8284 (zerop (c-forward-token-2 2 nil indent-point
))
8285 (if (eq (char-after) ?
<)
8286 (c-with-syntax-table c
++-template-syntax-table
8287 (zerop (c-forward-token-2 1 t indent-point
)))
8289 (eq (char-after) ?
:))))
8290 (goto-char placeholder
)
8291 (c-add-syntax 'inher-cont
(c-point 'boi
)))
8293 ;; CASE 5D.5: Continuation of the "expression part" of a
8294 ;; top level construct. Or, perhaps, an unrecognised construct.
8296 (while (and (setq placeholder
(point))
8297 (eq (car (c-beginning-of-decl-1 containing-sexp
))
8300 (c-backward-syntactic-ws)
8301 (eq (char-before) ?
}))
8302 (< (point) placeholder
)))
8305 ((eq (point) placeholder
) 'statement
) ; unrecognised construct
8306 ;; A preceding comma at the top level means that a
8307 ;; new variable declaration starts here. Use
8308 ;; topmost-intro-cont for it, for consistency with
8309 ;; the first variable declaration. C.f. case 5N.
8310 ((eq char-before-ip ?
,) 'topmost-intro-cont
)
8311 (t 'statement-cont
))
8312 nil nil containing-sexp paren-state
))
8315 ;; CASE 5F: Close of a non-class declaration level block.
8316 ((and (eq char-after-ip ?
})
8317 (c-keyword-member containing-decl-kwd
8318 'c-other-block-decl-kwds
))
8319 ;; This is inconsistent: Should use `containing-decl-open'
8320 ;; here if it's at boi, like in case 5J.
8321 (goto-char containing-decl-start
)
8323 (if (string-equal (symbol-name containing-decl-kwd
) "extern")
8324 ;; Special case for compatibility with the
8325 ;; extern-lang syntactic symbols.
8327 (intern (concat (symbol-name containing-decl-kwd
)
8330 (c-most-enclosing-brace paren-state
(point))
8333 ;; CASE 5G: we are looking at the brace which closes the
8334 ;; enclosing nested class decl
8335 ((and containing-sexp
8336 (eq char-after-ip ?
})
8337 (eq containing-decl-open containing-sexp
))
8338 (c-add-class-syntax 'class-close
8339 containing-decl-open
8340 containing-decl-start
8344 ;; CASE 5H: we could be looking at subsequent knr-argdecls
8345 ((and c-recognize-knr-p
8346 (not containing-sexp
) ; can't be knr inside braces.
8347 (not (eq char-before-ip ?
}))
8349 (setq placeholder
(cdr (c-beginning-of-decl-1 lim
)))
8351 ;; Do an extra check to avoid tripping up on
8352 ;; statements that occur in invalid contexts
8353 ;; (e.g. in macro bodies where we don't really
8354 ;; know the context of what we're looking at).
8355 (not (and c-opt-block-stmt-key
8356 (looking-at c-opt-block-stmt-key
)))))
8357 (< placeholder indent-point
))
8358 (goto-char placeholder
)
8359 (c-add-syntax 'knr-argdecl
(point)))
8361 ;; CASE 5I: ObjC method definition.
8362 ((and c-opt-method-key
8363 (looking-at c-opt-method-key
))
8364 (c-beginning-of-statement-1 nil t
)
8365 (if (= (point) indent-point
)
8366 ;; Handle the case when it's the first (non-comment)
8367 ;; thing in the buffer. Can't look for a 'same return
8368 ;; value from cbos1 since ObjC directives currently
8369 ;; aren't recognized fully, so that we get 'same
8370 ;; instead of 'previous if it moved over a preceding
8372 (goto-char (point-min)))
8373 (c-add-syntax 'objc-method-intro
(c-point 'boi
)))
8375 ;; CASE 5P: AWK pattern or function or continuation
8377 ((c-major-mode-is 'awk-mode
)
8378 (setq placeholder
(point))
8380 (if (and (eq (c-beginning-of-statement-1) 'same
)
8381 (/= (point) placeholder
))
8385 containing-sexp paren-state
))
8387 ;; CASE 5N: At a variable declaration that follows a class
8388 ;; definition or some other block declaration that doesn't
8389 ;; end at the closing '}'. C.f. case 5D.5.
8391 (c-backward-syntactic-ws lim
)
8392 (and (eq (char-before) ?
})
8394 (let ((start (point)))
8395 (if (and c-state-cache
8396 (consp (car c-state-cache
))
8397 (eq (cdar c-state-cache
) (point)))
8398 ;; Speed up the backward search a bit.
8399 (goto-char (caar c-state-cache
)))
8400 (c-beginning-of-decl-1 containing-sexp
)
8401 (setq placeholder
(point))
8402 (if (= start
(point))
8403 ;; The '}' is unbalanced.
8406 (>= (point) indent-point
))))))
8407 (goto-char placeholder
)
8408 (c-add-stmt-syntax 'topmost-intro-cont nil nil
8409 containing-sexp paren-state
))
8411 ;; NOTE: The point is at the end of the previous token here.
8413 ;; CASE 5J: we are at the topmost level, make
8414 ;; sure we skip back past any access specifiers
8416 ;; A macro continuation line is never at top level.
8417 (not (and macro-start
8418 (> indent-point macro-start
)))
8420 (setq placeholder
(point))
8421 (or (memq char-before-ip
'(?\
; ?{ ?} nil))
8422 (c-at-vsemi-p before-ws-ip
)
8423 (when (and (eq char-before-ip ?
:)
8424 (eq (c-beginning-of-statement-1 lim
)
8426 (c-backward-syntactic-ws lim
)
8427 (setq placeholder
(point)))
8428 (and (c-major-mode-is 'objc-mode
)
8429 (catch 'not-in-directive
8430 (c-beginning-of-statement-1 lim
)
8431 (setq placeholder
(point))
8432 (while (and (c-forward-objc-directive)
8433 (< (point) indent-point
))
8434 (c-forward-syntactic-ws)
8435 (if (>= (point) indent-point
)
8436 (throw 'not-in-directive t
))
8437 (setq placeholder
(point)))
8439 ;; For historic reasons we anchor at bol of the last
8440 ;; line of the previous declaration. That's clearly
8441 ;; highly bogus and useless, and it makes our lives hard
8442 ;; to remain compatible. :P
8443 (goto-char placeholder
)
8444 (c-add-syntax 'topmost-intro
(c-point 'bol
))
8445 (if containing-decl-open
8446 (if (c-keyword-member containing-decl-kwd
8447 'c-other-block-decl-kwds
)
8449 (goto-char (c-brace-anchor-point containing-decl-open
))
8451 (if (string-equal (symbol-name containing-decl-kwd
)
8453 ;; Special case for compatibility with the
8454 ;; extern-lang syntactic symbols.
8456 (intern (concat "in"
8457 (symbol-name containing-decl-kwd
))))
8459 (c-most-enclosing-brace paren-state
(point))
8461 (c-add-class-syntax 'inclass
8462 containing-decl-open
8463 containing-decl-start
8466 (when (and c-syntactic-indentation-in-macros
8468 (/= macro-start
(c-point 'boi indent-point
)))
8469 (c-add-syntax 'cpp-define-intro
)
8470 (setq macro-start nil
)))
8472 ;; CASE 5K: we are at an ObjC method definition
8473 ;; continuation line.
8474 ((and c-opt-method-key
8476 (c-beginning-of-statement-1 lim
)
8478 (when (looking-at c-opt-method-key
)
8479 (setq placeholder
(point)))))
8480 (c-add-syntax 'objc-method-args-cont placeholder
))
8482 ;; CASE 5L: we are at the first argument of a template
8483 ;; arglist that begins on the previous line.
8484 ((and c-recognize-
<>-arglists
8485 (eq (char-before) ?
<)
8486 (not (and c-overloadable-operators-regexp
8487 (c-after-special-operator-id lim
))))
8488 (c-beginning-of-statement-1 (c-safe-position (point) paren-state
))
8489 (c-add-syntax 'template-args-cont
(c-point 'boi
)))
8491 ;; CASE 5Q: we are at a statement within a macro.
8493 (c-beginning-of-statement-1 containing-sexp
)
8494 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state
))
8496 ;; CASE 5M: we are at a topmost continuation line
8498 (c-beginning-of-statement-1 (c-safe-position (point) paren-state
))
8499 (when (c-major-mode-is 'objc-mode
)
8500 (setq placeholder
(point))
8501 (while (and (c-forward-objc-directive)
8502 (< (point) indent-point
))
8503 (c-forward-syntactic-ws)
8504 (setq placeholder
(point)))
8505 (goto-char placeholder
))
8506 (c-add-syntax 'topmost-intro-cont
(c-point 'boi
)))
8509 ;; (CASE 6 has been removed.)
8511 ;; CASE 7: line is an expression, not a statement. Most
8512 ;; likely we are either in a function prototype or a function
8513 ;; call argument list
8514 ((not (or (and c-special-brace-lists
8516 (goto-char containing-sexp
)
8517 (c-looking-at-special-brace-list)))
8518 (eq (char-after containing-sexp
) ?
{)))
8521 ;; CASE 7A: we are looking at the arglist closing paren.
8523 ((memq char-after-ip
'(?\
) ?\
]))
8524 (goto-char containing-sexp
)
8525 (setq placeholder
(c-point 'boi
))
8526 (if (and (c-safe (backward-up-list 1) t
)
8527 (>= (point) placeholder
))
8530 (skip-chars-forward " \t"))
8531 (goto-char placeholder
))
8532 (c-add-stmt-syntax 'arglist-close
(list containing-sexp
) t
8533 (c-most-enclosing-brace paren-state
(point))
8536 ;; CASE 7B: Looking at the opening brace of an
8537 ;; in-expression block or brace list. C.f. cases 4, 16A
8539 ((and (eq char-after-ip ?
{)
8541 (setq placeholder
(c-inside-bracelist-p (point)
8544 (setq tmpsymbol
'(brace-list-open . inexpr-class
))
8545 (setq tmpsymbol
'(block-open . inexpr-statement
)
8547 (cdr-safe (c-looking-at-inexpr-block
8548 (c-safe-position containing-sexp
8551 ;; placeholder is nil if it's a block directly in
8552 ;; a function arglist. That makes us skip out of
8555 (goto-char placeholder
)
8556 (back-to-indentation)
8557 (c-add-stmt-syntax (car tmpsymbol
) nil t
8558 (c-most-enclosing-brace paren-state
(point))
8560 (if (/= (point) placeholder
)
8561 (c-add-syntax (cdr tmpsymbol
))))
8563 ;; CASE 7C: we are looking at the first argument in an empty
8564 ;; argument list. Use arglist-close if we're actually
8565 ;; looking at a close paren or bracket.
8566 ((memq char-before-ip
'(?\
( ?\
[))
8567 (goto-char containing-sexp
)
8568 (setq placeholder
(c-point 'boi
))
8569 (if (and (c-safe (backward-up-list 1) t
)
8570 (>= (point) placeholder
))
8573 (skip-chars-forward " \t"))
8574 (goto-char placeholder
))
8575 (c-add-stmt-syntax 'arglist-intro
(list containing-sexp
) t
8576 (c-most-enclosing-brace paren-state
(point))
8579 ;; CASE 7D: we are inside a conditional test clause. treat
8580 ;; these things as statements
8582 (goto-char containing-sexp
)
8583 (and (c-safe (c-forward-sexp -
1) t
)
8584 (looking-at "\\<for\\>[^_]")))
8585 (goto-char (1+ containing-sexp
))
8586 (c-forward-syntactic-ws indent-point
)
8587 (if (eq char-before-ip ?\
;)
8588 (c-add-syntax 'statement
(point))
8589 (c-add-syntax 'statement-cont
(point))
8592 ;; CASE 7E: maybe a continued ObjC method call. This is the
8593 ;; case when we are inside a [] bracketed exp, and what
8594 ;; precede the opening bracket is not an identifier.
8595 ((and c-opt-method-key
8596 (eq (char-after containing-sexp
) ?\
[)
8598 (goto-char (1- containing-sexp
))
8599 (c-backward-syntactic-ws (c-point 'bod
))
8600 (if (not (looking-at c-symbol-key
))
8601 (c-add-syntax 'objc-method-call-cont containing-sexp
))
8604 ;; CASE 7F: we are looking at an arglist continuation line,
8605 ;; but the preceding argument is on the same line as the
8606 ;; opening paren. This case includes multi-line
8607 ;; mathematical paren groupings, but we could be on a
8608 ;; for-list continuation line. C.f. case 7A.
8610 (goto-char (1+ containing-sexp
))
8612 (c-forward-syntactic-ws)
8615 (goto-char containing-sexp
)
8616 (setq placeholder
(c-point 'boi
))
8617 (if (and (c-safe (backward-up-list 1) t
)
8618 (>= (point) placeholder
))
8621 (skip-chars-forward " \t"))
8622 (goto-char placeholder
))
8623 (c-add-stmt-syntax 'arglist-cont-nonempty
(list containing-sexp
) t
8624 (c-most-enclosing-brace c-state-cache
(point))
8627 ;; CASE 7G: we are looking at just a normal arglist
8628 ;; continuation line
8629 (t (c-forward-syntactic-ws indent-point
)
8630 (c-add-syntax 'arglist-cont
(c-point 'boi
)))
8633 ;; CASE 8: func-local multi-inheritance line
8634 ((and (c-major-mode-is 'c
++-mode
)
8636 (goto-char indent-point
)
8637 (skip-chars-forward " \t")
8638 (looking-at c-opt-postfix-decl-spec-key
)))
8639 (goto-char indent-point
)
8640 (skip-chars-forward " \t")
8643 ;; CASE 8A: non-hanging colon on an inher intro
8644 ((eq char-after-ip ?
:)
8645 (c-backward-syntactic-ws lim
)
8646 (c-add-syntax 'inher-intro
(c-point 'boi
)))
8648 ;; CASE 8B: hanging colon on an inher intro
8649 ((eq char-before-ip ?
:)
8650 (c-add-syntax 'inher-intro
(c-point 'boi
)))
8652 ;; CASE 8C: a continued inheritance line
8654 (c-beginning-of-inheritance-list lim
)
8655 (c-add-syntax 'inher-cont
(point))
8658 ;; CASE 9: we are inside a brace-list
8659 ((and (not (c-major-mode-is 'awk-mode
)) ; Maybe this isn't needed (ACM, 2002/3/29)
8660 (setq special-brace-list
8661 (or (and c-special-brace-lists
;;;; ALWAYS NIL FOR AWK!!
8663 (goto-char containing-sexp
)
8664 (c-looking-at-special-brace-list)))
8665 (c-inside-bracelist-p containing-sexp paren-state
))))
8668 ;; CASE 9A: In the middle of a special brace list opener.
8669 ((and (consp special-brace-list
)
8671 (goto-char containing-sexp
)
8672 (eq (char-after) ?\
())
8673 (eq char-after-ip
(car (cdr special-brace-list
))))
8674 (goto-char (car (car special-brace-list
)))
8675 (skip-chars-backward " \t")
8677 (assoc 'statement-cont
8678 (setq placeholder
(c-guess-basic-syntax))))
8679 (setq c-syntactic-context placeholder
)
8680 (c-beginning-of-statement-1
8681 (c-safe-position (1- containing-sexp
) paren-state
))
8682 (c-forward-token-2 0)
8683 (while (looking-at c-specifier-key
)
8684 (goto-char (match-end 1))
8685 (c-forward-syntactic-ws))
8686 (c-add-syntax 'brace-list-open
(c-point 'boi
))))
8688 ;; CASE 9B: brace-list-close brace
8689 ((if (consp special-brace-list
)
8690 ;; Check special brace list closer.
8692 (goto-char (car (car special-brace-list
)))
8694 (goto-char indent-point
)
8695 (back-to-indentation)
8697 ;; We were between the special close char and the `)'.
8698 (and (eq (char-after) ?\
))
8699 (eq (1+ (point)) (cdr (car special-brace-list
))))
8700 ;; We were before the special close char.
8701 (and (eq (char-after) (cdr (cdr special-brace-list
)))
8702 (zerop (c-forward-token-2))
8703 (eq (1+ (point)) (cdr (car special-brace-list
)))))))
8704 ;; Normal brace list check.
8705 (and (eq char-after-ip ?
})
8706 (c-safe (goto-char (c-up-list-backward (point))) t
)
8707 (= (point) containing-sexp
)))
8708 (if (eq (point) (c-point 'boi
))
8709 (c-add-syntax 'brace-list-close
(point))
8710 (setq lim
(c-most-enclosing-brace c-state-cache
(point)))
8711 (c-beginning-of-statement-1 lim
)
8712 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state
)))
8715 ;; Prepare for the rest of the cases below by going to the
8716 ;; token following the opening brace
8717 (if (consp special-brace-list
)
8719 (goto-char (car (car special-brace-list
)))
8720 (c-forward-token-2 1 nil indent-point
))
8721 (goto-char containing-sexp
))
8723 (let ((start (point)))
8724 (c-forward-syntactic-ws indent-point
)
8725 (goto-char (max start
(c-point 'bol
))))
8726 (c-skip-ws-forward indent-point
)
8729 ;; CASE 9C: we're looking at the first line in a brace-list
8730 ((= (point) indent-point
)
8731 (if (consp special-brace-list
)
8732 (goto-char (car (car special-brace-list
)))
8733 (goto-char containing-sexp
))
8734 (if (eq (point) (c-point 'boi
))
8735 (c-add-syntax 'brace-list-intro
(point))
8736 (setq lim
(c-most-enclosing-brace c-state-cache
(point)))
8737 (c-beginning-of-statement-1 lim
)
8738 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state
)))
8740 ;; CASE 9D: this is just a later brace-list-entry or
8742 (t (if (or (eq char-after-ip ?
{)
8743 (and c-special-brace-lists
8745 (goto-char indent-point
)
8746 (c-forward-syntactic-ws (c-point 'eol
))
8747 (c-looking-at-special-brace-list (point)))))
8748 (c-add-syntax 'brace-entry-open
(point))
8749 (c-add-syntax 'brace-list-entry
(point))
8753 ;; CASE 10: A continued statement or top level construct.
8754 ((and (not (memq char-before-ip
'(?\
; ?:)))
8755 (not (c-at-vsemi-p before-ws-ip
))
8756 (or (not (eq char-before-ip ?
}))
8757 (c-looking-at-inexpr-block-backward c-state-cache
))
8760 (c-beginning-of-statement-1 containing-sexp
)
8761 (setq placeholder
(point))))
8762 (/= placeholder containing-sexp
))
8763 ;; This is shared with case 18.
8764 (c-guess-continued-construct indent-point
8770 ;; CASE 16: block close brace, possibly closing the defun or
8772 ((eq char-after-ip ?
})
8773 ;; From here on we have the next containing sexp in lim.
8774 (setq lim
(c-most-enclosing-brace paren-state
))
8775 (goto-char containing-sexp
)
8778 ;; CASE 16E: Closing a statement block? This catches
8779 ;; cases where it's preceded by a statement keyword,
8780 ;; which works even when used in an "invalid" context,
8781 ;; e.g. a macro argument.
8782 ((c-after-conditional)
8783 (c-backward-to-block-anchor lim
)
8784 (c-add-stmt-syntax 'block-close nil t lim paren-state
))
8786 ;; CASE 16A: closing a lambda defun or an in-expression
8787 ;; block? C.f. cases 4, 7B and 17E.
8788 ((setq placeholder
(c-looking-at-inexpr-block
8789 (c-safe-position containing-sexp paren-state
)
8791 (setq tmpsymbol
(if (eq (car placeholder
) 'inlambda
)
8794 (goto-char containing-sexp
)
8795 (back-to-indentation)
8796 (if (= containing-sexp
(point))
8797 (c-add-syntax tmpsymbol
(point))
8798 (goto-char (cdr placeholder
))
8799 (back-to-indentation)
8800 (c-add-stmt-syntax tmpsymbol nil t
8801 (c-most-enclosing-brace paren-state
(point))
8803 (if (/= (point) (cdr placeholder
))
8804 (c-add-syntax (car placeholder
)))))
8806 ;; CASE 16B: does this close an inline or a function in
8807 ;; a non-class declaration level block?
8812 (c-looking-at-decl-block
8813 (c-most-enclosing-brace paren-state lim
)
8815 (setq placeholder
(point))))
8816 (c-backward-to-decl-anchor lim
)
8817 (back-to-indentation)
8819 (goto-char placeholder
)
8820 (looking-at c-other-decl-block-key
))
8821 (c-add-syntax 'defun-close
(point))
8822 (c-add-syntax 'inline-close
(point))))
8824 ;; CASE 16F: Can be a defun-close of a function declared
8825 ;; in a statement block, e.g. in Pike or when using gcc
8826 ;; extensions, but watch out for macros followed by
8827 ;; blocks. Let it through to be handled below.
8828 ;; C.f. cases B.3 and 17G.
8830 (and (not (c-at-statement-start-p))
8831 (eq (c-beginning-of-statement-1 lim nil nil t
) 'same
)
8832 (setq placeholder
(point))
8833 (let ((c-recognize-typeless-decls nil
))
8834 ;; Turn off recognition of constructs that
8835 ;; lacks a type in this case, since that's more
8836 ;; likely to be a macro followed by a block.
8837 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
8838 (back-to-indentation)
8839 (if (/= (point) containing-sexp
)
8840 (goto-char placeholder
))
8841 (c-add-stmt-syntax 'defun-close nil t lim paren-state
))
8843 ;; CASE 16C: If there is an enclosing brace then this is
8844 ;; a block close since defun closes inside declaration
8845 ;; level blocks have been handled above.
8847 ;; If the block is preceded by a case/switch label on
8848 ;; the same line, we anchor at the first preceding label
8849 ;; at boi. The default handling in c-add-stmt-syntax
8850 ;; really fixes it better, but we do like this to keep
8851 ;; the indentation compatible with version 5.28 and
8852 ;; earlier. C.f. case 17H.
8853 (while (and (/= (setq placeholder
(point)) (c-point 'boi
))
8854 (eq (c-beginning-of-statement-1 lim
) 'label
)))
8855 (goto-char placeholder
)
8856 (if (looking-at c-label-kwds-regexp
)
8857 (c-add-syntax 'block-close
(point))
8858 (goto-char containing-sexp
)
8859 ;; c-backward-to-block-anchor not necessary here; those
8860 ;; situations are handled in case 16E above.
8861 (c-add-stmt-syntax 'block-close nil t lim paren-state
)))
8863 ;; CASE 16D: Only top level defun close left.
8865 (goto-char containing-sexp
)
8866 (c-backward-to-decl-anchor lim
)
8867 (c-add-stmt-syntax 'defun-close nil nil
8868 (c-most-enclosing-brace paren-state
)
8872 ;; CASE 17: Statement or defun catchall.
8874 (goto-char indent-point
)
8875 ;; Back up statements until we find one that starts at boi.
8876 (while (let* ((prev-point (point))
8877 (last-step-type (c-beginning-of-statement-1
8879 (if (= (point) prev-point
)
8881 (setq step-type
(or step-type last-step-type
))
8883 (setq step-type last-step-type
)
8884 (/= (point) (c-point 'boi
)))))
8887 ;; CASE 17B: continued statement
8888 ((and (eq step-type
'same
)
8889 (/= (point) indent-point
))
8890 (c-add-stmt-syntax 'statement-cont nil nil
8891 containing-sexp paren-state
))
8893 ;; CASE 17A: After a case/default label?
8895 (while (and (eq step-type
'label
)
8896 (not (looking-at c-label-kwds-regexp
)))
8898 (c-beginning-of-statement-1 containing-sexp
)))
8899 (eq step-type
'label
))
8900 (c-add-stmt-syntax (if (eq char-after-ip ?
{)
8901 'statement-case-open
8902 'statement-case-intro
)
8903 nil t containing-sexp paren-state
))
8905 ;; CASE 17D: any old statement
8907 (while (eq step-type
'label
)
8909 (c-beginning-of-statement-1 containing-sexp
)))
8910 (eq step-type
'previous
))
8911 (c-add-stmt-syntax 'statement nil t
8912 containing-sexp paren-state
)
8913 (if (eq char-after-ip ?
{)
8914 (c-add-syntax 'block-open
)))
8916 ;; CASE 17I: Inside a substatement block.
8918 ;; The following tests are all based on containing-sexp.
8919 (goto-char containing-sexp
)
8920 ;; From here on we have the next containing sexp in lim.
8921 (setq lim
(c-most-enclosing-brace paren-state containing-sexp
))
8922 (c-after-conditional))
8923 (c-backward-to-block-anchor lim
)
8924 (c-add-stmt-syntax 'statement-block-intro nil t
8926 (if (eq char-after-ip ?
{)
8927 (c-add-syntax 'block-open
)))
8929 ;; CASE 17E: first statement in an in-expression block.
8930 ;; C.f. cases 4, 7B and 16A.
8931 ((setq placeholder
(c-looking-at-inexpr-block
8932 (c-safe-position containing-sexp paren-state
)
8934 (setq tmpsymbol
(if (eq (car placeholder
) 'inlambda
)
8936 'statement-block-intro
))
8937 (back-to-indentation)
8938 (if (= containing-sexp
(point))
8939 (c-add-syntax tmpsymbol
(point))
8940 (goto-char (cdr placeholder
))
8941 (back-to-indentation)
8942 (c-add-stmt-syntax tmpsymbol nil t
8943 (c-most-enclosing-brace c-state-cache
(point))
8945 (if (/= (point) (cdr placeholder
))
8946 (c-add-syntax (car placeholder
))))
8947 (if (eq char-after-ip ?
{)
8948 (c-add-syntax 'block-open
)))
8950 ;; CASE 17F: first statement in an inline, or first
8951 ;; statement in a top-level defun. we can tell this is it
8952 ;; if there are no enclosing braces that haven't been
8953 ;; narrowed out by a class (i.e. don't use bod here).
8955 (or (not (setq placeholder
(c-most-enclosing-brace
8958 (goto-char placeholder
)
8959 (eq (char-after) ?
{))
8960 (c-looking-at-decl-block (c-most-enclosing-brace
8961 paren-state
(point))
8963 (c-backward-to-decl-anchor lim
)
8964 (back-to-indentation)
8965 (c-add-syntax 'defun-block-intro
(point)))
8967 ;; CASE 17G: First statement in a function declared inside
8968 ;; a normal block. This can occur in Pike and with
8969 ;; e.g. the gcc extensions, but watch out for macros
8970 ;; followed by blocks. C.f. cases B.3 and 16F.
8972 (and (not (c-at-statement-start-p))
8973 (eq (c-beginning-of-statement-1 lim nil nil t
) 'same
)
8974 (setq placeholder
(point))
8975 (let ((c-recognize-typeless-decls nil
))
8976 ;; Turn off recognition of constructs that lacks
8977 ;; a type in this case, since that's more likely
8978 ;; to be a macro followed by a block.
8979 (c-forward-decl-or-cast-1 (c-point 'bosws
) nil nil
))))
8980 (back-to-indentation)
8981 (if (/= (point) containing-sexp
)
8982 (goto-char placeholder
))
8983 (c-add-stmt-syntax 'defun-block-intro nil t
8986 ;; CASE 17H: First statement in a block.
8988 ;; If the block is preceded by a case/switch label on the
8989 ;; same line, we anchor at the first preceding label at
8990 ;; boi. The default handling in c-add-stmt-syntax is
8991 ;; really fixes it better, but we do like this to keep the
8992 ;; indentation compatible with version 5.28 and earlier.
8994 (while (and (/= (setq placeholder
(point)) (c-point 'boi
))
8995 (eq (c-beginning-of-statement-1 lim
) 'label
)))
8996 (goto-char placeholder
)
8997 (if (looking-at c-label-kwds-regexp
)
8998 (c-add-syntax 'statement-block-intro
(point))
8999 (goto-char containing-sexp
)
9000 ;; c-backward-to-block-anchor not necessary here; those
9001 ;; situations are handled in case 17I above.
9002 (c-add-stmt-syntax 'statement-block-intro nil t
9004 (if (eq char-after-ip ?
{)
9005 (c-add-syntax 'block-open
)))
9009 ;; now we need to look at any modifiers
9010 (goto-char indent-point
)
9011 (skip-chars-forward " \t")
9013 ;; are we looking at a comment only line?
9014 (when (and (looking-at c-comment-start-regexp
)
9015 (/= (c-forward-token-2 0 nil
(c-point 'eol
)) 0))
9016 (c-append-syntax 'comment-intro
))
9018 ;; we might want to give additional offset to friends (in C++).
9019 (when (and c-opt-friend-key
9020 (looking-at c-opt-friend-key
))
9021 (c-append-syntax 'friend
))
9023 ;; Set syntactic-relpos.
9024 (let ((p c-syntactic-context
))
9026 (if (integerp (c-langelem-pos (car p
)))
9028 (setq syntactic-relpos
(c-langelem-pos (car p
)))
9033 ;; Start of or a continuation of a preprocessor directive?
9034 (if (and macro-start
9035 (eq macro-start
(c-point 'boi
))
9036 (not (and (c-major-mode-is 'pike-mode
)
9037 (eq (char-after (1+ macro-start
)) ?
\"))))
9038 (c-append-syntax 'cpp-macro
)
9039 (when (and c-syntactic-indentation-in-macros macro-start
)
9042 (< syntactic-relpos macro-start
)
9044 (assq 'arglist-intro c-syntactic-context
)
9045 (assq 'arglist-cont c-syntactic-context
)
9046 (assq 'arglist-cont-nonempty c-syntactic-context
)
9047 (assq 'arglist-close c-syntactic-context
))))
9048 ;; If inside a cpp expression, i.e. anywhere in a
9049 ;; cpp directive except a #define body, we only let
9050 ;; through the syntactic analysis that is internal
9051 ;; in the expression. That means the arglist
9052 ;; elements, if they are anchored inside the cpp
9054 (setq c-syntactic-context nil
)
9055 (c-add-syntax 'cpp-macro-cont macro-start
))
9056 (when (and (eq macro-start syntactic-relpos
)
9057 (not (assq 'cpp-define-intro c-syntactic-context
))
9059 (goto-char macro-start
)
9060 (or (not (c-forward-to-cpp-define-body))
9061 (<= (point) (c-point 'boi indent-point
)))))
9062 ;; Inside a #define body and the syntactic analysis is
9063 ;; anchored on the start of the #define. In this case
9064 ;; we add cpp-define-intro to get the extra
9065 ;; indentation of the #define body.
9066 (c-add-syntax 'cpp-define-intro
)))))
9068 ;; return the syntax
9069 c-syntactic-context
)))
9072 ;; Indentation calculation.
9074 (defun c-evaluate-offset (offset langelem symbol
)
9075 ;; offset can be a number, a function, a variable, a list, or one of
9076 ;; the symbols + or -
9078 ;; This function might do hidden buffer changes.
9081 ((numberp offset
) offset
)
9082 ((vectorp offset
) offset
)
9085 ((eq offset
'+) c-basic-offset
)
9086 ((eq offset
'-
) (- c-basic-offset
))
9087 ((eq offset
'++) (* 2 c-basic-offset
))
9088 ((eq offset
'--
) (* 2 (- c-basic-offset
)))
9089 ((eq offset
'*) (/ c-basic-offset
2))
9090 ((eq offset
'/) (/ (- c-basic-offset
) 2))
9095 (cons (c-langelem-sym langelem
)
9096 (c-langelem-pos langelem
)))
9101 ((eq (car offset
) 'quote
)
9102 (c-benign-error "The offset %S for %s was mistakenly quoted"
9106 ((memq (car offset
) '(min max
))
9107 (let (res val
(method (car offset
)))
9108 (setq offset
(cdr offset
))
9110 (setq val
(c-evaluate-offset (car offset
) langelem symbol
))
9118 Error evaluating offset %S for %s: \
9119 Cannot combine absolute offset %S with relative %S in `%s' method"
9120 (car offset
) symbol res val method
)
9121 (setq res
(funcall method res val
))))
9125 Error evaluating offset %S for %s: \
9126 Cannot combine relative offset %S with absolute %S in `%s' method"
9127 (car offset
) symbol res val method
)
9128 (setq res
(vector (funcall method
(aref res
0)
9130 (setq offset
(cdr offset
)))
9133 ((eq (car offset
) 'add
)
9135 (setq offset
(cdr offset
))
9137 (setq val
(c-evaluate-offset (car offset
) langelem symbol
))
9144 (setq res
(vector (+ (aref res
0) val
)))
9145 (setq res
(+ res val
))))
9149 Error evaluating offset %S for %s: \
9150 Cannot combine absolute offsets %S and %S in `add' method"
9151 (car offset
) symbol res val
)
9152 (setq res val
)))) ; Override.
9153 (setq offset
(cdr offset
)))
9158 (when (eq (car offset
) 'first
)
9159 (setq offset
(cdr offset
)))
9160 (while (and (not res
) offset
)
9161 (setq res
(c-evaluate-offset (car offset
) langelem symbol
)
9162 offset
(cdr offset
)))
9165 ((and (symbolp offset
) (boundp offset
))
9166 (symbol-value offset
))
9169 (c-benign-error "Unknown offset format %S for %s" offset symbol
)
9172 (if (or (null res
) (integerp res
)
9173 (and (vectorp res
) (= (length res
) 1) (integerp (aref res
0))))
9175 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
9179 (defun c-calc-offset (langelem)
9180 ;; Get offset from LANGELEM which is a list beginning with the
9181 ;; syntactic symbol and followed by any analysis data it provides.
9182 ;; That data may be zero or more elements, but if at least one is
9183 ;; given then the first is the anchor position (or nil). The symbol
9184 ;; is matched against `c-offsets-alist' and the offset calculated
9185 ;; from that is returned.
9187 ;; This function might do hidden buffer changes.
9188 (let* ((symbol (c-langelem-sym langelem
))
9189 (match (assq symbol c-offsets-alist
))
9190 (offset (cdr-safe match
)))
9192 (setq offset
(c-evaluate-offset offset langelem symbol
))
9193 (if c-strict-syntax-p
9194 (c-benign-error "No offset found for syntactic symbol %s" symbol
))
9196 (if (vectorp offset
)
9198 (or (and (numberp offset
) offset
)
9199 (and (symbolp offset
) (symbol-value offset
))
9203 (defun c-get-offset (langelem)
9204 ;; This is a compatibility wrapper for `c-calc-offset' in case
9205 ;; someone is calling it directly. It takes an old style syntactic
9206 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
9209 ;; This function might do hidden buffer changes.
9210 (if (c-langelem-pos langelem
)
9211 (c-calc-offset (list (c-langelem-sym langelem
)
9212 (c-langelem-pos langelem
)))
9213 (c-calc-offset langelem
)))
9215 (defun c-get-syntactic-indentation (langelems)
9216 ;; Calculate the syntactic indentation from a syntactic description
9217 ;; as returned by `c-guess-syntax'.
9219 ;; Note that topmost-intro always has an anchor position at bol, for
9220 ;; historical reasons. It's often used together with other symbols
9221 ;; that has more sane positions. Since we always use the first
9222 ;; found anchor position, we rely on that these other symbols always
9223 ;; precede topmost-intro in the LANGELEMS list.
9225 ;; This function might do hidden buffer changes.
9226 (let ((indent 0) anchor
)
9229 (let* ((c-syntactic-element (car langelems
))
9230 (res (c-calc-offset c-syntactic-element
)))
9233 ;; Got an absolute column that overrides any indentation
9234 ;; we've collected so far, but not the relative
9235 ;; indentation we might get for the nested structures
9236 ;; further down the langelems list.
9237 (setq indent
(elt res
0)
9238 anchor
(point-min)) ; A position at column 0.
9240 ;; Got a relative change of the current calculated
9242 (setq indent
(+ indent res
))
9244 ;; Use the anchor position from the first syntactic
9245 ;; element with one.
9247 (setq anchor
(c-langelem-pos (car langelems
)))))
9249 (setq langelems
(cdr langelems
))))
9252 (+ indent
(save-excursion
9258 (cc-provide 'cc-engine
)
9260 ;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
9261 ;;; cc-engine.el ends here