Update for MH-E release 8.0.2.
[emacs.git] / lisp / progmodes / cc-engine.el
blob32c82eb1c1d2345ebc99eb12056152f047196e02
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985,1987,1992-2003, 2004, 2005, 2006 Free Software Foundation,
4 ;; Inc.
6 ;; Authors: 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs and Stewart Clamen
9 ;; 1985 Richard M. Stallman
10 ;; Maintainer: bug-cc-mode@gnu.org
11 ;; Created: 22-Apr-1997 (split from cc-mode.el)
12 ;; Version: See cc-mode.el
13 ;; Keywords: c languages oop
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with this program; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
32 ;;; Commentary:
34 ;; The functions which have docstring documentation can be considered
35 ;; part of an API which other packages can use in CC Mode buffers.
36 ;; Otoh, undocumented functions and functions with the documentation
37 ;; in comments are considered purely internal and can change semantics
38 ;; or even disappear in the future.
40 ;; (This policy applies to CC Mode as a whole, not just this file. It
41 ;; probably also applies to many other Emacs packages, but here it's
42 ;; clearly spelled out.)
44 ;; Hidden buffer changes
46 ;; Various functions in CC Mode use text properties for caching and
47 ;; syntactic markup purposes, and those of them that might modify such
48 ;; properties but still don't modify the buffer in a visible way are
49 ;; said to do "hidden buffer changes". They should be used within
50 ;; `c-save-buffer-state' or a similar function that saves and restores
51 ;; buffer modifiedness, disables buffer change hooks, etc.
53 ;; Interactive functions are assumed to not do hidden buffer changes,
54 ;; except in the specific parts of them that do real changes.
56 ;; Lineup functions are assumed to do hidden buffer changes. They
57 ;; must not do real changes, though.
59 ;; All other functions that do hidden buffer changes have that noted
60 ;; in their doc string or comment.
62 ;; The intention with this system is to avoid wrapping every leaf
63 ;; function that do hidden buffer changes inside
64 ;; `c-save-buffer-state'. It should be used as near the top of the
65 ;; interactive functions as possible.
67 ;; Functions called during font locking are allowed to do hidden
68 ;; buffer changes since the font-lock package run them in a context
69 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
70 ;; inspired by `save-buffer-state' in the font-lock package).
72 ;; Use of text properties
74 ;; CC Mode uses several text properties internally to mark up various
75 ;; positions, e.g. to improve speed and to eliminate glitches in
76 ;; interactive refontification.
78 ;; Note: This doc is for internal use only. Other packages should not
79 ;; assume that these text properties are used as described here.
81 ;; 'syntax-table
82 ;; Used to modify the syntax of some characters. Currently used to
83 ;; mark the "<" and ">" of angle bracket parens with paren syntax.
85 ;; This property is used on single characters and is therefore
86 ;; always treated as front and rear nonsticky (or start and end open
87 ;; in XEmacs vocabulary). It's therefore installed on
88 ;; `text-property-default-nonsticky' if that variable exists (Emacs
89 ;; >= 21).
91 ;; 'c-is-sws and 'c-in-sws
92 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
93 ;; speed them up. See the comment blurb before `c-put-is-sws'
94 ;; below for further details.
96 ;; 'c-type
97 ;; This property is used on single characters to mark positions with
98 ;; special syntactic relevance of various sorts. Its primary use is
99 ;; to avoid glitches when multiline constructs are refontified
100 ;; interactively (on font lock decoration level 3). It's cleared in
101 ;; a region before it's fontified and is then put on relevant chars
102 ;; in that region as they are encountered during the fontification.
103 ;; The value specifies the kind of position:
105 ;; 'c-decl-arg-start
106 ;; Put on the last char of the token preceding each declaration
107 ;; inside a declaration style arglist (typically in a function
108 ;; prototype).
110 ;; 'c-decl-end
111 ;; Put on the last char of the token preceding a declaration.
112 ;; This is used in cases where declaration boundaries can't be
113 ;; recognized simply by looking for a token like ";" or "}".
114 ;; `c-type-decl-end-used' must be set if this is used (see also
115 ;; `c-find-decl-spots').
117 ;; 'c-<>-arg-sep
118 ;; Put on the commas that separate arguments in angle bracket
119 ;; arglists like C++ template arglists.
121 ;; 'c-decl-id-start and 'c-decl-type-start
122 ;; Put on the last char of the token preceding each declarator
123 ;; in the declarator list of a declaration. They are also used
124 ;; between the identifiers cases like enum declarations.
125 ;; 'c-decl-type-start is used when the declarators are types,
126 ;; 'c-decl-id-start otherwise.
128 ;; 'c-awk-NL-prop
129 ;; Used in AWK mode to mark the various kinds of newlines. See
130 ;; cc-awk.el.
132 ;;; Code:
134 (eval-when-compile
135 (let ((load-path
136 (if (and (boundp 'byte-compile-dest-file)
137 (stringp byte-compile-dest-file))
138 (cons (file-name-directory byte-compile-dest-file) load-path)
139 load-path)))
140 (load "cc-bytecomp" nil t)))
142 (cc-require 'cc-defs)
143 (cc-require-when-compile 'cc-langs)
144 (cc-require 'cc-vars)
146 ;; Silence the compiler.
147 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
150 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
152 (defmacro c-declare-lang-variables ()
153 `(progn
154 ,@(apply 'nconc
155 (mapcar (lambda (init)
156 `(,(if (elt init 2)
157 `(defvar ,(car init) nil ,(elt init 2))
158 `(defvar ,(car init) nil))
159 (make-variable-buffer-local ',(car init))))
160 (cdr c-lang-variable-inits)))))
161 (c-declare-lang-variables)
164 ;;; Internal state variables.
166 ;; Internal state of hungry delete key feature
167 (defvar c-hungry-delete-key nil)
168 (make-variable-buffer-local 'c-hungry-delete-key)
170 ;; The electric flag (toggled by `c-toggle-electric-state').
171 ;; If t, electric actions (like automatic reindentation, and (if
172 ;; c-auto-newline is also set) auto newlining) will happen when an electric
173 ;; key like `{' is pressed (or an electric keyword like `else').
174 (defvar c-electric-flag t)
175 (make-variable-buffer-local 'c-electric-flag)
177 ;; Internal state of auto newline feature.
178 (defvar c-auto-newline nil)
179 (make-variable-buffer-local 'c-auto-newline)
181 ;; Included in the mode line to indicate the active submodes.
182 ;; (defvar c-submode-indicators nil)
183 ;; (make-variable-buffer-local 'c-submode-indicators)
185 (defun c-calculate-state (arg prevstate)
186 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
187 ;; arg is nil or zero, toggle the state. If arg is negative, turn
188 ;; the state off, and if arg is positive, turn the state on
189 (if (or (not arg)
190 (zerop (setq arg (prefix-numeric-value arg))))
191 (not prevstate)
192 (> arg 0)))
194 ;; Dynamically bound cache for `c-in-literal'.
195 (defvar c-in-literal-cache t)
198 ;; Basic handling of preprocessor directives.
200 ;; This is a dynamically bound cache used together with
201 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
202 ;; works as long as point doesn't cross a macro boundary.
203 (defvar c-macro-start 'unknown)
205 (defsubst c-query-and-set-macro-start ()
206 (if (symbolp c-macro-start)
207 (setq c-macro-start (save-excursion
208 (c-save-buffer-state ()
209 (and (c-beginning-of-macro)
210 (point)))))
211 c-macro-start))
213 (defsubst c-query-macro-start ()
214 (if (symbolp c-macro-start)
215 (save-excursion
216 (c-save-buffer-state ()
217 (and (c-beginning-of-macro)
218 (point))))
219 c-macro-start))
221 (defun c-beginning-of-macro (&optional lim)
222 "Go to the beginning of a preprocessor directive.
223 Leave point at the beginning of the directive and return t if in one,
224 otherwise return nil and leave point unchanged.
226 Note that this function might do hidden buffer changes. See the
227 comment at the start of cc-engine.el for more info."
228 (when c-opt-cpp-prefix
229 (let ((here (point)))
230 (save-restriction
231 (if lim (narrow-to-region lim (point-max)))
232 (beginning-of-line)
233 (while (eq (char-before (1- (point))) ?\\)
234 (forward-line -1))
235 (back-to-indentation)
236 (if (and (<= (point) here)
237 (looking-at c-opt-cpp-start))
239 (goto-char here)
240 nil)))))
242 (defun c-end-of-macro ()
243 "Go to the end of a preprocessor directive.
244 More accurately, move the point to the end of the closest following
245 line that doesn't end with a line continuation backslash - no check is
246 done that the point is inside a cpp directive to begin with.
248 Note that this function might do hidden buffer changes. See the
249 comment at the start of cc-engine.el for more info."
250 (while (progn
251 (end-of-line)
252 (when (and (eq (char-before) ?\\)
253 (not (eobp)))
254 (forward-char)
255 t))))
257 (defun c-forward-to-cpp-define-body ()
258 ;; Assuming point is at the "#" that introduces a preprocessor
259 ;; directive, it's moved forward to the start of the definition body
260 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
261 ;; specifies). Non-nil is returned in this case, in all other cases
262 ;; nil is returned and point isn't moved.
264 ;; This function might do hidden buffer changes.
265 (when (and c-opt-cpp-macro-define-start
266 (looking-at c-opt-cpp-macro-define-start)
267 (not (= (match-end 0) (c-point 'eol))))
268 (goto-char (match-end 0))))
271 ;;; Basic utility functions.
273 (defun c-syntactic-content (from to paren-level)
274 ;; Return the given region as a string where all syntactic
275 ;; whitespace is removed or, where necessary, replaced with a single
276 ;; space. If PAREN-LEVEL is given then all parens in the region are
277 ;; collapsed to "()", "[]" etc.
279 ;; This function might do hidden buffer changes.
281 (save-excursion
282 (save-restriction
283 (narrow-to-region from to)
284 (goto-char from)
285 (let* ((parts (list nil)) (tail parts) pos in-paren)
287 (while (re-search-forward c-syntactic-ws-start to t)
288 (goto-char (setq pos (match-beginning 0)))
289 (c-forward-syntactic-ws)
290 (if (= (point) pos)
291 (forward-char)
293 (when paren-level
294 (save-excursion
295 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
296 pos (point))))
298 (if (and (> pos from)
299 (< (point) to)
300 (looking-at "\\w\\|\\s_")
301 (save-excursion
302 (goto-char (1- pos))
303 (looking-at "\\w\\|\\s_")))
304 (progn
305 (setcdr tail (list (buffer-substring-no-properties from pos)
306 " "))
307 (setq tail (cddr tail)))
308 (setcdr tail (list (buffer-substring-no-properties from pos)))
309 (setq tail (cdr tail)))
311 (when in-paren
312 (when (= (car (parse-partial-sexp pos to -1)) -1)
313 (setcdr tail (list (buffer-substring-no-properties
314 (1- (point)) (point))))
315 (setq tail (cdr tail))))
317 (setq from (point))))
319 (setcdr tail (list (buffer-substring-no-properties from to)))
320 (apply 'concat (cdr parts))))))
322 (defun c-shift-line-indentation (shift-amt)
323 ;; Shift the indentation of the current line with the specified
324 ;; amount (positive inwards). The buffer is modified only if
325 ;; SHIFT-AMT isn't equal to zero.
326 (let ((pos (- (point-max) (point)))
327 (c-macro-start c-macro-start)
328 tmp-char-inserted)
329 (if (zerop shift-amt)
331 ;; If we're on an empty line inside a macro, we take the point
332 ;; to be at the current indentation and shift it to the
333 ;; appropriate column. This way we don't treat the extra
334 ;; whitespace out to the line continuation as indentation.
335 (when (and (c-query-and-set-macro-start)
336 (looking-at "[ \t]*\\\\$")
337 (save-excursion
338 (skip-chars-backward " \t")
339 (bolp)))
340 (insert ?x)
341 (backward-char)
342 (setq tmp-char-inserted t))
343 (unwind-protect
344 (let ((col (current-indentation)))
345 (delete-region (c-point 'bol) (c-point 'boi))
346 (beginning-of-line)
347 (indent-to (+ col shift-amt)))
348 (when tmp-char-inserted
349 (delete-char 1))))
350 ;; If initial point was within line's indentation and we're not on
351 ;; a line with a line continuation in a macro, position after the
352 ;; indentation. Else stay at same point in text.
353 (if (and (< (point) (c-point 'boi))
354 (not tmp-char-inserted))
355 (back-to-indentation)
356 (if (> (- (point-max) pos) (point))
357 (goto-char (- (point-max) pos))))))
359 (defsubst c-keyword-sym (keyword)
360 ;; Return non-nil if the string KEYWORD is a known keyword. More
361 ;; precisely, the value is the symbol for the keyword in
362 ;; `c-keywords-obarray'.
363 (intern-soft keyword c-keywords-obarray))
365 (defsubst c-keyword-member (keyword-sym lang-constant)
366 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
367 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
368 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
369 ;; nil then the result is nil.
370 (get keyword-sym lang-constant))
372 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
373 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
374 "\"|"
375 "\""))
377 ;; Regexp matching string limit syntax.
378 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
379 "\\s\"\\|\\s|"
380 "\\s\""))
382 ;; Regexp matching WS followed by string limit syntax.
383 (defconst c-ws*-string-limit-regexp
384 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
386 ;; Holds formatted error strings for the few cases where parse errors
387 ;; are reported.
388 (defvar c-parsing-error nil)
389 (make-variable-buffer-local 'c-parsing-error)
391 (defun c-echo-parsing-error (&optional quiet)
392 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
393 (c-benign-error "%s" c-parsing-error))
394 c-parsing-error)
396 ;; Faces given to comments and string literals. This is used in some
397 ;; situations to speed up recognition; it isn't mandatory that font
398 ;; locking is in use. This variable is extended with the face in
399 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
400 (defvar c-literal-faces
401 (append '(font-lock-comment-face font-lock-string-face)
402 (when (facep 'font-lock-comment-delimiter-face)
403 ;; New in Emacs 22.
404 '(font-lock-comment-delimiter-face))))
406 (defsubst c-put-c-type-property (pos value)
407 ;; Put a c-type property with the given value at POS.
408 (c-put-char-property pos 'c-type value))
410 (defun c-clear-c-type-property (from to value)
411 ;; Remove all occurences of the c-type property that has the given
412 ;; value in the region between FROM and TO. VALUE is assumed to not
413 ;; be nil.
415 ;; Note: This assumes that c-type is put on single chars only; it's
416 ;; very inefficient if matching properties cover large regions.
417 (save-excursion
418 (goto-char from)
419 (while (progn
420 (when (eq (get-text-property (point) 'c-type) value)
421 (c-clear-char-property (point) 'c-type))
422 (goto-char (next-single-property-change (point) 'c-type nil to))
423 (< (point) to)))))
426 ;; Some debug tools to visualize various special positions. This
427 ;; debug code isn't as portable as the rest of CC Mode.
429 (cc-bytecomp-defun overlays-in)
430 (cc-bytecomp-defun overlay-get)
431 (cc-bytecomp-defun overlay-start)
432 (cc-bytecomp-defun overlay-end)
433 (cc-bytecomp-defun delete-overlay)
434 (cc-bytecomp-defun overlay-put)
435 (cc-bytecomp-defun make-overlay)
437 (defun c-debug-add-face (beg end face)
438 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
439 (while overlays
440 (setq overlay (car overlays)
441 overlays (cdr overlays))
442 (when (eq (overlay-get overlay 'face) face)
443 (setq beg (min beg (overlay-start overlay))
444 end (max end (overlay-end overlay)))
445 (delete-overlay overlay)))
446 (overlay-put (make-overlay beg end) 'face face)))
448 (defun c-debug-remove-face (beg end face)
449 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
450 (ol-beg beg) (ol-end end))
451 (while overlays
452 (setq overlay (car overlays)
453 overlays (cdr overlays))
454 (when (eq (overlay-get overlay 'face) face)
455 (setq ol-beg (min ol-beg (overlay-start overlay))
456 ol-end (max ol-end (overlay-end overlay)))
457 (delete-overlay overlay)))
458 (when (< ol-beg beg)
459 (overlay-put (make-overlay ol-beg beg) 'face face))
460 (when (> ol-end end)
461 (overlay-put (make-overlay end ol-end) 'face face))))
464 ;; `c-beginning-of-statement-1' and accompanying stuff.
466 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
467 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
468 ;; better way should be implemented, but this will at least shut up
469 ;; the byte compiler.
470 (defvar c-maybe-labelp)
472 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
474 ;; Macros used internally in c-beginning-of-statement-1 for the
475 ;; automaton actions.
476 (defmacro c-bos-push-state ()
477 '(setq stack (cons (cons state saved-pos)
478 stack)))
479 (defmacro c-bos-pop-state (&optional do-if-done)
480 `(if (setq state (car (car stack))
481 saved-pos (cdr (car stack))
482 stack (cdr stack))
484 ,do-if-done
485 (throw 'loop nil)))
486 (defmacro c-bos-pop-state-and-retry ()
487 '(throw 'loop (setq state (car (car stack))
488 saved-pos (cdr (car stack))
489 ;; Throw nil if stack is empty, else throw non-nil.
490 stack (cdr stack))))
491 (defmacro c-bos-save-pos ()
492 '(setq saved-pos (vector pos tok ptok pptok)))
493 (defmacro c-bos-restore-pos ()
494 '(unless (eq (elt saved-pos 0) start)
495 (setq pos (elt saved-pos 0)
496 tok (elt saved-pos 1)
497 ptok (elt saved-pos 2)
498 pptok (elt saved-pos 3))
499 (goto-char pos)
500 (setq sym nil)))
501 (defmacro c-bos-save-error-info (missing got)
502 `(setq saved-pos (vector pos ,missing ,got)))
503 (defmacro c-bos-report-error ()
504 '(unless noerror
505 (setq c-parsing-error
506 (format "No matching `%s' found for `%s' on line %d"
507 (elt saved-pos 1)
508 (elt saved-pos 2)
509 (1+ (count-lines (point-min)
510 (c-point 'bol (elt saved-pos 0))))))))
512 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
513 noerror comma-delim)
514 "Move to the start of the current statement or declaration, or to
515 the previous one if already at the beginning of one. Only
516 statements/declarations on the same level are considered, i.e. don't
517 move into or out of sexps (not even normal expression parentheses).
519 Stop at statement continuation tokens like \"else\", \"catch\",
520 \"finally\" and the \"while\" in \"do ... while\" if the start point
521 is within the continuation. If starting at such a token, move to the
522 corresponding statement start. If at the beginning of a statement,
523 move to the closest containing statement if there is any. This might
524 also stop at a continuation clause.
526 Labels are treated as part of the following statements if
527 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
528 statement start keyword.) Otherwise, each label is treated as a
529 separate statement.
531 Macros are ignored \(i.e. skipped over) unless point is within one, in
532 which case the content of the macro is treated as normal code. Aside
533 from any normal statement starts found in it, stop at the first token
534 of the content in the macro, i.e. the expression of an \"#if\" or the
535 start of the definition in a \"#define\". Also stop at start of
536 macros before leaving them.
538 Return 'label if stopped at a label, 'same if stopped at the beginning
539 of the current statement, 'up if stepped to a containing statement,
540 'previous if stepped to a preceding statement, 'beginning if stepped
541 from a statement continuation clause to its start clause, or 'macro if
542 stepped to a macro start. Note that 'same and not 'label is returned
543 if stopped at the same label without crossing the colon character.
545 LIM may be given to limit the search. If the search hits the limit,
546 point will be left at the closest following token, or at the start
547 position if that is less ('same is returned in this case).
549 NOERROR turns off error logging to `c-parsing-error'.
551 Normally only ';' and virtual semicolons are considered to delimit
552 statements, but if COMMA-DELIM is non-nil then ',' is treated
553 as a delimiter too.
555 Note that this function might do hidden buffer changes. See the
556 comment at the start of cc-engine.el for more info."
558 ;; The bulk of this function is a pushdown automaton that looks at statement
559 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
560 ;; purpose is to keep track of nested statements, ensuring that such
561 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
562 ;; does with nested braces/brackets/parentheses).
564 ;; Note: The position of a boundary is the following token.
566 ;; Beginning with the current token (the one following point), move back one
567 ;; sexp at a time (where a sexp is, more or less, either a token or the
568 ;; entire contents of a brace/bracket/paren pair). Each time a statement
569 ;; boundary is crossed or a "while"-like token is found, update the state of
570 ;; the PDA. Stop at the beginning of a statement when the stack (holding
571 ;; nested statement info) is empty and the position has been moved.
573 ;; The following variables constitute the PDA:
575 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
576 ;; scanned back over, 'boundary if we've just gone back over a
577 ;; statement boundary, or nil otherwise.
578 ;; state: takes one of the values (nil else else-boundary while
579 ;; while-boundary catch catch-boundary).
580 ;; nil means "no "while"-like token yet scanned".
581 ;; 'else, for example, means "just gone back over an else".
582 ;; 'else-boundary means "just gone back over a statement boundary
583 ;; immediately after having gone back over an else".
584 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
585 ;; of error reporting information.
586 ;; stack: The stack onto which the PDA pushes its state. Each entry
587 ;; consists of a saved value of state and saved-pos. An entry is
588 ;; pushed when we move back over a "continuation" token (e.g. else)
589 ;; and popped when we encounter the corresponding opening token
590 ;; (e.g. if).
593 ;; The following diagram briefly outlines the PDA.
595 ;; Common state:
596 ;; "else": Push state, goto state `else'.
597 ;; "while": Push state, goto state `while'.
598 ;; "catch" or "finally": Push state, goto state `catch'.
599 ;; boundary: Pop state.
600 ;; other: Do nothing special.
602 ;; State `else':
603 ;; boundary: Goto state `else-boundary'.
604 ;; other: Error, pop state, retry token.
606 ;; State `else-boundary':
607 ;; "if": Pop state.
608 ;; boundary: Error, pop state.
609 ;; other: See common state.
611 ;; State `while':
612 ;; boundary: Save position, goto state `while-boundary'.
613 ;; other: Pop state, retry token.
615 ;; State `while-boundary':
616 ;; "do": Pop state.
617 ;; boundary: Restore position if it's not at start, pop state. [*see below]
618 ;; other: See common state.
620 ;; State `catch':
621 ;; boundary: Goto state `catch-boundary'.
622 ;; other: Error, pop state, retry token.
624 ;; State `catch-boundary':
625 ;; "try": Pop state.
626 ;; "catch": Goto state `catch'.
627 ;; boundary: Error, pop state.
628 ;; other: See common state.
630 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
631 ;; searching for a "do" which would have opened a do-while. If we didn't
632 ;; find it, we discard the analysis done since the "while", go back to this
633 ;; token in the buffer and restart the scanning there, this time WITHOUT
634 ;; pushing the 'while state onto the stack.
636 ;; In addition to the above there is some special handling of labels
637 ;; and macros.
639 (let ((case-fold-search nil)
640 (start (point))
641 macro-start
642 (delims (if comma-delim '(?\; ?,) '(?\;)))
643 (c-stmt-delim-chars (if comma-delim
644 c-stmt-delim-chars-with-comma
645 c-stmt-delim-chars))
646 c-in-literal-cache c-maybe-labelp saved
647 ;; Current position.
649 ;; Position of last stmt boundary character (e.g. ;).
650 boundary-pos
651 ;; The position of the last sexp or bound that follows the
652 ;; first found colon, i.e. the start of the nonlabel part of
653 ;; the statement. It's `start' if a colon is found just after
654 ;; the start.
655 after-labels-pos
656 ;; Like `after-labels-pos', but the first such position inside
657 ;; a label, i.e. the start of the last label before the start
658 ;; of the nonlabel part of the statement.
659 last-label-pos
660 ;; The last position where a label is possible provided the
661 ;; statement started there. It's nil as long as no invalid
662 ;; label content has been found (according to
663 ;; `c-nonlabel-token-key'. It's `start' if no valid label
664 ;; content was found in the label. Note that we might still
665 ;; regard it a label if it starts with `c-label-kwds'.
666 label-good-pos
667 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
668 ;; See above.
670 ;; Current state in the automaton. See above.
671 state
672 ;; Current saved positions. See above.
673 saved-pos
674 ;; Stack of conses (state . saved-pos).
675 stack
676 ;; Regexp which matches "for", "if", etc.
677 (cond-key (or c-opt-block-stmt-key
678 "\\<\\>")) ; Matches nothing.
679 ;; Return value.
680 (ret 'same)
681 ;; Positions of the last three sexps or bounds we've stopped at.
682 tok ptok pptok)
684 (save-restriction
685 (if lim (narrow-to-region lim (point-max)))
687 (if (save-excursion
688 (and (c-beginning-of-macro)
689 (/= (point) start)))
690 (setq macro-start (point)))
692 ;; Try to skip back over unary operator characters, to register
693 ;; that we've moved.
694 (while (progn
695 (setq pos (point))
696 (c-backward-syntactic-ws)
697 ;; Protect post-++/-- operators just before a virtual semicolon.
698 (and (not (c-at-vsemi-p))
699 (/= (skip-chars-backward "-+!*&~@`#") 0))))
701 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
702 ;; done. Later on we ignore the boundaries for statements that don't
703 ;; contain any sexp. The only thing that is affected is that the error
704 ;; checking is a little less strict, and we really don't bother.
705 (if (and (memq (char-before) delims)
706 (progn (forward-char -1)
707 (setq saved (point))
708 (c-backward-syntactic-ws)
709 (or (memq (char-before) delims)
710 (memq (char-before) '(?: nil))
711 (eq (char-syntax (char-before)) ?\()
712 (c-at-vsemi-p))))
713 (setq ret 'previous
714 pos saved)
716 ;; Begin at start and not pos to detect macros if we stand
717 ;; directly after the #.
718 (goto-char start)
719 (if (looking-at "\\<\\|\\W")
720 ;; Record this as the first token if not starting inside it.
721 (setq tok start))
723 ;; The following while loop goes back one sexp (balanced parens,
724 ;; etc. with contents, or symbol or suchlike) each iteration. This
725 ;; movement is accomplished with a call to scan-sexps approx 130 lines
726 ;; below.
727 (while
728 (catch 'loop ;; Throw nil to break, non-nil to continue.
729 (cond
730 ((save-excursion
731 (and macro-start ; Always NIL for AWK.
732 (progn (skip-chars-backward " \t")
733 (eq (char-before) ?#))
734 (progn (setq saved (1- (point)))
735 (beginning-of-line)
736 (not (eq (char-before (1- (point))) ?\\)))
737 (looking-at c-opt-cpp-start)
738 (progn (skip-chars-forward " \t")
739 (eq (point) saved))))
740 (goto-char saved)
741 (if (and (c-forward-to-cpp-define-body)
742 (progn (c-forward-syntactic-ws start)
743 (< (point) start)))
744 ;; Stop at the first token in the content of the macro.
745 (setq pos (point)
746 ignore-labels t) ; Avoid the label check on exit.
747 (setq pos saved
748 ret 'macro
749 ignore-labels t))
750 (throw 'loop nil))
752 ;; Do a round through the automaton if we've just passed a
753 ;; statement boundary or passed a "while"-like token.
754 ((or sym
755 (and (looking-at cond-key)
756 (setq sym (intern (match-string 1)))))
758 (when (and (< pos start) (null stack))
759 (throw 'loop nil))
761 ;; The PDA state handling.
763 ;; Refer to the description of the PDA in the opening
764 ;; comments. In the following OR form, the first leaf
765 ;; attempts to handles one of the specific actions detailed
766 ;; (e.g., finding token "if" whilst in state `else-boundary').
767 ;; We drop through to the second leaf (which handles common
768 ;; state) if no specific handler is found in the first cond.
769 ;; If a parsing error is detected (e.g. an "else" with no
770 ;; preceding "if"), we throw to the enclosing catch.
772 ;; Note that the (eq state 'else) means
773 ;; "we've just passed an else", NOT "we're looking for an
774 ;; else".
775 (or (cond
776 ((eq state 'else)
777 (if (eq sym 'boundary)
778 (setq state 'else-boundary)
779 (c-bos-report-error)
780 (c-bos-pop-state-and-retry)))
782 ((eq state 'else-boundary)
783 (cond ((eq sym 'if)
784 (c-bos-pop-state (setq ret 'beginning)))
785 ((eq sym 'boundary)
786 (c-bos-report-error)
787 (c-bos-pop-state))))
789 ((eq state 'while)
790 (if (and (eq sym 'boundary)
791 ;; Since this can cause backtracking we do a
792 ;; little more careful analysis to avoid it:
793 ;; If there's a label in front of the while
794 ;; it can't be part of a do-while.
795 (not after-labels-pos))
796 (progn (c-bos-save-pos)
797 (setq state 'while-boundary))
798 (c-bos-pop-state-and-retry))) ; Can't be a do-while
800 ((eq state 'while-boundary)
801 (cond ((eq sym 'do)
802 (c-bos-pop-state (setq ret 'beginning)))
803 ((eq sym 'boundary) ; isn't a do-while
804 (c-bos-restore-pos) ; the position of the while
805 (c-bos-pop-state)))) ; no longer searching for do.
807 ((eq state 'catch)
808 (if (eq sym 'boundary)
809 (setq state 'catch-boundary)
810 (c-bos-report-error)
811 (c-bos-pop-state-and-retry)))
813 ((eq state 'catch-boundary)
814 (cond
815 ((eq sym 'try)
816 (c-bos-pop-state (setq ret 'beginning)))
817 ((eq sym 'catch)
818 (setq state 'catch))
819 ((eq sym 'boundary)
820 (c-bos-report-error)
821 (c-bos-pop-state)))))
823 ;; This is state common. We get here when the previous
824 ;; cond statement found no particular state handler.
825 (cond ((eq sym 'boundary)
826 ;; If we have a boundary at the start
827 ;; position we push a frame to go to the
828 ;; previous statement.
829 (if (>= pos start)
830 (c-bos-push-state)
831 (c-bos-pop-state)))
832 ((eq sym 'else)
833 (c-bos-push-state)
834 (c-bos-save-error-info 'if 'else)
835 (setq state 'else))
836 ((eq sym 'while)
837 ;; Is this a real while, or a do-while?
838 ;; The next `when' triggers unless we are SURE that
839 ;; the `while' is not the tailend of a `do-while'.
840 (when (or (not pptok)
841 (memq (char-after pptok) delims)
842 ;; The following kludge is to prevent
843 ;; infinite recursion when called from
844 ;; c-awk-after-if-for-while-condition-p,
845 ;; or the like.
846 (and (eq (point) start)
847 (c-vsemi-status-unknown-p))
848 (c-at-vsemi-p pptok))
849 ;; Since this can cause backtracking we do a
850 ;; little more careful analysis to avoid it: If
851 ;; the while isn't followed by a (possibly
852 ;; virtual) semicolon it can't be a do-while.
853 (c-bos-push-state)
854 (setq state 'while)))
855 ((memq sym '(catch finally))
856 (c-bos-push-state)
857 (c-bos-save-error-info 'try sym)
858 (setq state 'catch))))
860 (when c-maybe-labelp
861 ;; We're either past a statement boundary or at the
862 ;; start of a statement, so throw away any label data
863 ;; for the previous one.
864 (setq after-labels-pos nil
865 last-label-pos nil
866 c-maybe-labelp nil))))
868 ;; Step to the previous sexp, but not if we crossed a
869 ;; boundary, since that doesn't consume an sexp.
870 (if (eq sym 'boundary)
871 (setq ret 'previous)
873 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
874 ;; BACKWARDS THROUGH THE SOURCE.
876 ;; This is typically fast with the caching done by
877 ;; c-(backward|forward)-sws.
878 (c-backward-syntactic-ws)
880 (let ((before-sws-pos (point))
881 ;; Set as long as we have to continue jumping by sexps.
882 ;; It's the position to use as end in the next round.
883 sexp-loop-continue-pos
884 ;; The end position of the area to search for statement
885 ;; barriers in this round.
886 (sexp-loop-end-pos pos))
888 ;; The following while goes back one sexp per iteration.
889 (while
890 (progn
891 (unless (c-safe (c-backward-sexp) t)
892 ;; Give up if we hit an unbalanced block. Since the
893 ;; stack won't be empty the code below will report a
894 ;; suitable error.
895 (throw 'loop nil))
897 ;; Check if the sexp movement crossed a statement or
898 ;; declaration boundary. But first modify the point
899 ;; so that `c-crosses-statement-barrier-p' only looks
900 ;; at the non-sexp chars following the sexp.
901 (save-excursion
902 (when (setq
903 boundary-pos
904 (cond
905 ((if macro-start
907 (save-excursion
908 (when (c-beginning-of-macro)
909 ;; Set continuation position in case
910 ;; `c-crosses-statement-barrier-p'
911 ;; doesn't detect anything below.
912 (setq sexp-loop-continue-pos (point)))))
913 ;; If the sexp movement took us into a
914 ;; macro then there were only some non-sexp
915 ;; chars after it. Skip out of the macro
916 ;; to analyze them but not the non-sexp
917 ;; chars that might be inside the macro.
918 (c-end-of-macro)
919 (c-crosses-statement-barrier-p
920 (point) sexp-loop-end-pos))
922 ((and
923 (eq (char-after) ?{)
924 (not (c-looking-at-inexpr-block lim nil t)))
925 ;; Passed a block sexp. That's a boundary
926 ;; alright.
927 (point))
929 ((looking-at "\\s\(")
930 ;; Passed some other paren. Only analyze
931 ;; the non-sexp chars after it.
932 (goto-char (1+ (c-down-list-backward
933 before-sws-pos)))
934 ;; We're at a valid token start position
935 ;; (outside the `save-excursion') if
936 ;; `c-crosses-statement-barrier-p' failed.
937 (c-crosses-statement-barrier-p
938 (point) sexp-loop-end-pos))
941 ;; Passed a symbol sexp or line
942 ;; continuation. It doesn't matter that
943 ;; it's included in the analyzed region.
944 (if (c-crosses-statement-barrier-p
945 (point) sexp-loop-end-pos)
947 ;; If it was a line continuation then we
948 ;; have to continue looping.
949 (if (looking-at "\\\\$")
950 (setq sexp-loop-continue-pos (point)))
951 nil))))
953 (setq pptok ptok
954 ptok tok
955 tok boundary-pos
956 sym 'boundary)
957 ;; Like a C "continue". Analyze the next sexp.
958 (throw 'loop t)))
960 sexp-loop-continue-pos) ; End of "go back a sexp" loop.
961 (goto-char sexp-loop-continue-pos)
962 (setq sexp-loop-end-pos sexp-loop-continue-pos
963 sexp-loop-continue-pos nil))))
965 ;; ObjC method def?
966 (when (and c-opt-method-key
967 (setq saved (c-in-method-def-p)))
968 (setq pos saved
969 ignore-labels t) ; Avoid the label check on exit.
970 (throw 'loop nil))
972 ;; Handle labels.
973 (unless (eq ignore-labels t)
974 (when (numberp c-maybe-labelp)
975 ;; `c-crosses-statement-barrier-p' has found a colon, so we
976 ;; might be in a label now. Have we got a real label
977 ;; (including a case label) or something like C++'s "public:"?
978 (if (or (not (looking-at c-nonlabel-token-key)) ; proper label
979 (save-excursion ; e.g. "case 'a':" ?
980 (and (c-safe (c-backward-sexp) t)
981 (looking-at "\\<case\\>")))) ; FIXME!!! this is
982 ; wrong for AWK. 2006/1/14.
983 (progn
984 (if after-labels-pos ; Have we already encountered a label?
985 (if (not last-label-pos)
986 (setq last-label-pos (or tok start)))
987 (setq after-labels-pos (or tok start)))
988 (setq c-maybe-labelp t
989 label-good-pos nil))
990 (setq c-maybe-labelp nil))) ; bogus "label"
992 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
993 ; been found.
994 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
995 ;; We're in a potential label and it's the first
996 ;; time we've found something that isn't allowed in
997 ;; one.
998 (setq label-good-pos (or tok start))))
1000 ;; We've moved back by a sexp, so update the token positions.
1001 (setq sym nil
1002 pptok ptok
1003 ptok tok
1004 tok (point)
1005 pos tok))) ; Not nil (for the while loop).
1007 ;; If the stack isn't empty there might be errors to report.
1008 (while stack
1009 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1010 (c-bos-report-error))
1011 (setq saved-pos (cdr (car stack))
1012 stack (cdr stack)))
1014 (when (and (eq ret 'same)
1015 (not (memq sym '(boundary ignore nil))))
1016 ;; Need to investigate closer whether we've crossed
1017 ;; between a substatement and its containing statement.
1018 (if (setq saved (if (looking-at c-block-stmt-1-key)
1019 ptok
1020 pptok))
1021 (cond ((> start saved) (setq pos saved))
1022 ((= start saved) (setq ret 'up)))))
1024 (when (and (not ignore-labels)
1025 (eq c-maybe-labelp t)
1026 (not (eq ret 'beginning))
1027 after-labels-pos
1028 (or (not label-good-pos)
1029 (<= label-good-pos pos)
1030 (progn
1031 (goto-char (if (and last-label-pos
1032 (< last-label-pos start))
1033 last-label-pos
1034 pos))
1035 (looking-at c-label-kwds-regexp))))
1036 ;; We're in a label. Maybe we should step to the statement
1037 ;; after it.
1038 (if (< after-labels-pos start)
1039 (setq pos after-labels-pos)
1040 (setq ret 'label)
1041 (if (and last-label-pos (< last-label-pos start))
1042 ;; Might have jumped over several labels. Go to the last one.
1043 (setq pos last-label-pos)))))
1045 ;; Skip over the unary operators that can start the statement.
1046 (goto-char pos)
1047 (while (progn
1048 (c-backward-syntactic-ws)
1049 ;; protect AWK post-inc/decrement operators, etc.
1050 (and (not (c-at-vsemi-p (point)))
1051 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1052 (setq pos (point)))
1053 (goto-char pos)
1054 ret)))
1056 (defun c-crosses-statement-barrier-p (from to)
1057 "Return non-nil if buffer positions FROM to TO cross one or more
1058 statement or declaration boundaries. The returned value is actually
1059 the position of the earliest boundary char. FROM must not be within
1060 a string or comment.
1062 The variable `c-maybe-labelp' is set to the position of the first `:' that
1063 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1064 single `?' is found, then `c-maybe-labelp' is cleared.
1066 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1067 regarded as having a \"virtual semicolon\" immediately after the last token on
1068 the line. If this virtual semicolon is _at_ from, the function recognises it.
1070 Note that this function might do hidden buffer changes. See the
1071 comment at the start of cc-engine.el for more info."
1072 (let ((skip-chars c-stmt-delim-chars)
1073 lit-range)
1074 (save-excursion
1075 (catch 'done
1076 (goto-char from)
1077 (while (progn (skip-chars-forward skip-chars to)
1078 (< (point) to))
1079 (cond
1080 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1081 (goto-char (cdr lit-range)))
1082 ((eq (char-after) ?:)
1083 (forward-char)
1084 (if (and (eq (char-after) ?:)
1085 (< (point) to))
1086 ;; Ignore scope operators.
1087 (forward-char)
1088 (setq c-maybe-labelp (1- (point)))))
1089 ((eq (char-after) ??)
1090 ;; A question mark. Can't be a label, so stop
1091 ;; looking for more : and ?.
1092 (setq c-maybe-labelp nil
1093 skip-chars (substring c-stmt-delim-chars 0 -2)))
1094 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1095 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1096 (backward-char))
1097 (skip-chars-backward " \t" from)
1098 (if (c-at-vsemi-p)
1099 (throw 'done (point))
1100 (forward-line)))
1101 (t (throw 'done (point)))))
1102 ;; In trailing space after an as yet undetected virtual semicolon?
1103 (c-backward-syntactic-ws from)
1104 (if (and (< (point) to)
1105 (c-at-vsemi-p))
1106 (point)
1107 nil)))))
1109 (defun c-at-statement-start-p ()
1110 "Return non-nil if the point is at the first token in a statement
1111 or somewhere in the syntactic whitespace before it.
1113 A \"statement\" here is not restricted to those inside code blocks.
1114 Any kind of declaration-like construct that occur outside function
1115 bodies is also considered a \"statement\".
1117 Note that this function might do hidden buffer changes. See the
1118 comment at the start of cc-engine.el for more info."
1120 (save-excursion
1121 (let ((end (point))
1122 c-maybe-labelp)
1123 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1124 (or (bobp)
1125 (eq (char-before) ?})
1126 (and (eq (char-before) ?{)
1127 (not (and c-special-brace-lists
1128 (progn (backward-char)
1129 (c-looking-at-special-brace-list)))))
1130 (c-crosses-statement-barrier-p (point) end)))))
1132 (defun c-at-expression-start-p ()
1133 "Return non-nil if the point is at the first token in an expression or
1134 statement, or somewhere in the syntactic whitespace before it.
1136 An \"expression\" here is a bit different from the normal language
1137 grammar sense: It's any sequence of expression tokens except commas,
1138 unless they are enclosed inside parentheses of some kind. Also, an
1139 expression never continues past an enclosing parenthesis, but it might
1140 contain parenthesis pairs of any sort except braces.
1142 Since expressions never cross statement boundaries, this function also
1143 recognizes statement beginnings, just like `c-at-statement-start-p'.
1145 Note that this function might do hidden buffer changes. See the
1146 comment at the start of cc-engine.el for more info."
1148 (save-excursion
1149 (let ((end (point))
1150 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1151 c-maybe-labelp)
1152 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1153 (or (bobp)
1154 (memq (char-before) '(?{ ?}))
1155 (save-excursion (backward-char)
1156 (looking-at "\\s("))
1157 (c-crosses-statement-barrier-p (point) end)))))
1160 ;; A set of functions that covers various idiosyncrasies in
1161 ;; implementations of `forward-comment'.
1163 ;; Note: Some emacsen considers incorrectly that any line comment
1164 ;; ending with a backslash continues to the next line. I can't think
1165 ;; of any way to work around that in a reliable way without changing
1166 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1167 ;; changing the syntax for backslash doesn't work since we must treat
1168 ;; escapes in string literals correctly.)
1170 (defun c-forward-single-comment ()
1171 "Move forward past whitespace and the closest following comment, if any.
1172 Return t if a comment was found, nil otherwise. In either case, the
1173 point is moved past the following whitespace. Line continuations,
1174 i.e. a backslashes followed by line breaks, are treated as whitespace.
1175 The line breaks that end line comments are considered to be the
1176 comment enders, so the point will be put on the beginning of the next
1177 line if it moved past a line comment.
1179 This function does not do any hidden buffer changes."
1181 (let ((start (point)))
1182 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1183 (goto-char (match-end 0)))
1185 (when (forward-comment 1)
1186 (if (eobp)
1187 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1188 ;; forwards at eob.
1191 ;; Emacs includes the ending newline in a b-style (c++)
1192 ;; comment, but XEmacs doesn't. We depend on the Emacs
1193 ;; behavior (which also is symmetric).
1194 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1195 (condition-case nil (forward-char 1)))
1197 t))))
1199 (defsubst c-forward-comments ()
1200 "Move forward past all following whitespace and comments.
1201 Line continuations, i.e. a backslashes followed by line breaks, are
1202 treated as whitespace.
1204 Note that this function might do hidden buffer changes. See the
1205 comment at the start of cc-engine.el for more info."
1207 (while (or
1208 ;; If forward-comment in at least XEmacs 21 is given a large
1209 ;; positive value, it'll loop all the way through if it hits
1210 ;; eob.
1211 (and (forward-comment 5)
1212 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1213 ;; forwards at eob.
1214 (not (eobp)))
1216 (when (looking-at "\\\\[\n\r]")
1217 (forward-char 2)
1218 t))))
1220 (defun c-backward-single-comment ()
1221 "Move backward past whitespace and the closest preceding comment, if any.
1222 Return t if a comment was found, nil otherwise. In either case, the
1223 point is moved past the preceding whitespace. Line continuations,
1224 i.e. a backslashes followed by line breaks, are treated as whitespace.
1225 The line breaks that end line comments are considered to be the
1226 comment enders, so the point cannot be at the end of the same line to
1227 move over a line comment.
1229 This function does not do any hidden buffer changes."
1231 (let ((start (point)))
1232 ;; When we got newline terminated comments, forward-comment in all
1233 ;; supported emacsen so far will stop at eol of each line not
1234 ;; ending with a comment when moving backwards. This corrects for
1235 ;; that, and at the same time handles line continuations.
1236 (while (progn
1237 (skip-chars-backward " \t\n\r\f\v")
1238 (and (looking-at "[\n\r]")
1239 (eq (char-before) ?\\)))
1240 (backward-char))
1242 (if (bobp)
1243 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1244 ;; backwards at bob.
1247 ;; Leave point after the closest following newline if we've
1248 ;; backed up over any above, since forward-comment won't move
1249 ;; backward over a line comment if point is at the end of the
1250 ;; same line.
1251 (re-search-forward "\\=\\s *[\n\r]" start t)
1253 (if (if (forward-comment -1)
1254 (if (eolp)
1255 ;; If forward-comment above succeeded and we're at eol
1256 ;; then the newline we moved over above didn't end a
1257 ;; line comment, so we give it another go.
1258 (forward-comment -1)
1261 ;; Emacs <= 20 and XEmacs move back over the closer of a
1262 ;; block comment that lacks an opener.
1263 (if (looking-at "\\*/")
1264 (progn (forward-char 2) nil)
1265 t)))))
1267 (defsubst c-backward-comments ()
1268 "Move backward past all preceding whitespace and comments.
1269 Line continuations, i.e. a backslashes followed by line breaks, are
1270 treated as whitespace. The line breaks that end line comments are
1271 considered to be the comment enders, so the point cannot be at the end
1272 of the same line to move over a line comment. Unlike
1273 c-backward-syntactic-ws, this function doesn't move back over
1274 preprocessor directives.
1276 Note that this function might do hidden buffer changes. See the
1277 comment at the start of cc-engine.el for more info."
1279 (let ((start (point)))
1280 (while (and
1281 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1282 ;; return t when moving backwards at bob.
1283 (not (bobp))
1285 (if (forward-comment -1)
1286 (if (looking-at "\\*/")
1287 ;; Emacs <= 20 and XEmacs move back over the
1288 ;; closer of a block comment that lacks an opener.
1289 (progn (forward-char 2) nil)
1292 ;; XEmacs treats line continuations as whitespace but
1293 ;; only in the backward direction, which seems a bit
1294 ;; odd. Anyway, this is necessary for Emacs.
1295 (when (and (looking-at "[\n\r]")
1296 (eq (char-before) ?\\)
1297 (< (point) start))
1298 (backward-char)
1299 t))))))
1302 ;; Tools for skipping over syntactic whitespace.
1304 ;; The following functions use text properties to cache searches over
1305 ;; large regions of syntactic whitespace. It works as follows:
1307 ;; o If a syntactic whitespace region contains anything but simple
1308 ;; whitespace (i.e. space, tab and line breaks), the text property
1309 ;; `c-in-sws' is put over it. At places where we have stopped
1310 ;; within that region there's also a `c-is-sws' text property.
1311 ;; That since there typically are nested whitespace inside that
1312 ;; must be handled separately, e.g. whitespace inside a comment or
1313 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1314 ;; to jump to another point with that property within the same
1315 ;; `c-in-sws' region. It can be likened to a ladder where
1316 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1318 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1319 ;; a "rung position" and also maybe on the first following char.
1320 ;; As many characters as can be conveniently found in this range
1321 ;; are marked, but no assumption can be made that the whole range
1322 ;; is marked (it could be clobbered by later changes, for
1323 ;; instance).
1325 ;; Note that some part of the beginning of a sequence of simple
1326 ;; whitespace might be part of the end of a preceding line comment
1327 ;; or cpp directive and must not be considered part of the "rung".
1328 ;; Such whitespace is some amount of horizontal whitespace followed
1329 ;; by a newline. In the case of cpp directives it could also be
1330 ;; two newlines with horizontal whitespace between them.
1332 ;; The reason to include the first following char is to cope with
1333 ;; "rung positions" that doesn't have any ordinary whitespace. If
1334 ;; `c-is-sws' is put on a token character it does not have
1335 ;; `c-in-sws' set simultaneously. That's the only case when that
1336 ;; can occur, and the reason for not extending the `c-in-sws'
1337 ;; region to cover it is that the `c-in-sws' region could then be
1338 ;; accidentally merged with a following one if the token is only
1339 ;; one character long.
1341 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1342 ;; removed in the changed region. If the change was inside
1343 ;; syntactic whitespace that means that the "ladder" is broken, but
1344 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1345 ;; parts on either side and use an ordinary search only to "repair"
1346 ;; the gap.
1348 ;; Special care needs to be taken if a region is removed: If there
1349 ;; are `c-in-sws' on both sides of it which do not connect inside
1350 ;; the region then they can't be joined. If e.g. a marked macro is
1351 ;; broken, syntactic whitespace inside the new text might be
1352 ;; marked. If those marks would become connected with the old
1353 ;; `c-in-sws' range around the macro then we could get a ladder
1354 ;; with one end outside the macro and the other at some whitespace
1355 ;; within it.
1357 ;; The main motivation for this system is to increase the speed in
1358 ;; skipping over the large whitespace regions that can occur at the
1359 ;; top level in e.g. header files that contain a lot of comments and
1360 ;; cpp directives. For small comments inside code it's probably
1361 ;; slower than using `forward-comment' straightforwardly, but speed is
1362 ;; not a significant factor there anyway.
1364 ; (defface c-debug-is-sws-face
1365 ; '((t (:background "GreenYellow")))
1366 ; "Debug face to mark the `c-is-sws' property.")
1367 ; (defface c-debug-in-sws-face
1368 ; '((t (:underline t)))
1369 ; "Debug face to mark the `c-in-sws' property.")
1371 ; (defun c-debug-put-sws-faces ()
1372 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1373 ; ;; properties in the buffer.
1374 ; (interactive)
1375 ; (save-excursion
1376 ; (c-save-buffer-state (in-face)
1377 ; (goto-char (point-min))
1378 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1379 ; (point)))
1380 ; (while (progn
1381 ; (goto-char (next-single-property-change
1382 ; (point) 'c-is-sws nil (point-max)))
1383 ; (if in-face
1384 ; (progn
1385 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1386 ; (setq in-face nil))
1387 ; (setq in-face (point)))
1388 ; (not (eobp))))
1389 ; (goto-char (point-min))
1390 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1391 ; (point)))
1392 ; (while (progn
1393 ; (goto-char (next-single-property-change
1394 ; (point) 'c-in-sws nil (point-max)))
1395 ; (if in-face
1396 ; (progn
1397 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1398 ; (setq in-face nil))
1399 ; (setq in-face (point)))
1400 ; (not (eobp)))))))
1402 (defmacro c-debug-sws-msg (&rest args)
1403 ;;`(message ,@args)
1406 (defmacro c-put-is-sws (beg end)
1407 ;; This macro does a hidden buffer change.
1408 `(let ((beg ,beg) (end ,end))
1409 (put-text-property beg end 'c-is-sws t)
1410 ,@(when (facep 'c-debug-is-sws-face)
1411 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1413 (defmacro c-put-in-sws (beg end)
1414 ;; This macro does a hidden buffer change.
1415 `(let ((beg ,beg) (end ,end))
1416 (put-text-property beg end 'c-in-sws t)
1417 ,@(when (facep 'c-debug-is-sws-face)
1418 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1420 (defmacro c-remove-is-sws (beg end)
1421 ;; This macro does a hidden buffer change.
1422 `(let ((beg ,beg) (end ,end))
1423 (remove-text-properties beg end '(c-is-sws nil))
1424 ,@(when (facep 'c-debug-is-sws-face)
1425 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1427 (defmacro c-remove-in-sws (beg end)
1428 ;; This macro does a hidden buffer change.
1429 `(let ((beg ,beg) (end ,end))
1430 (remove-text-properties beg end '(c-in-sws nil))
1431 ,@(when (facep 'c-debug-is-sws-face)
1432 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1434 (defmacro c-remove-is-and-in-sws (beg end)
1435 ;; This macro does a hidden buffer change.
1436 `(let ((beg ,beg) (end ,end))
1437 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1438 ,@(when (facep 'c-debug-is-sws-face)
1439 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1440 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1442 (defsubst c-invalidate-sws-region-after (beg end)
1443 ;; Called from `after-change-functions'. Note that if
1444 ;; `c-forward-sws' or `c-backward-sws' are used outside
1445 ;; `c-save-buffer-state' or similar then this will remove the cache
1446 ;; properties right after they're added.
1448 ;; This function does hidden buffer changes.
1450 (save-excursion
1451 ;; Adjust the end to remove the properties in any following simple
1452 ;; ws up to and including the next line break, if there is any
1453 ;; after the changed region. This is necessary e.g. when a rung
1454 ;; marked empty line is converted to a line comment by inserting
1455 ;; "//" before the line break. In that case the line break would
1456 ;; keep the rung mark which could make a later `c-backward-sws'
1457 ;; move into the line comment instead of over it.
1458 (goto-char end)
1459 (skip-chars-forward " \t\f\v")
1460 (when (and (eolp) (not (eobp)))
1461 (setq end (1+ (point)))))
1463 (when (and (= beg end)
1464 (get-text-property beg 'c-in-sws)
1465 (> beg (point-min))
1466 (get-text-property (1- beg) 'c-in-sws))
1467 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1468 ;; safe to keep a range that was continuous before the change. E.g:
1470 ;; #define foo
1471 ;; \
1472 ;; bar
1474 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1475 ;; after "foo" is removed then "bar" will become part of the cpp
1476 ;; directive instead of a syntactically relevant token. In that
1477 ;; case there's no longer syntactic ws from "#" to "b".
1478 (setq beg (1- beg)))
1480 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1481 (c-remove-is-and-in-sws beg end))
1483 (defun c-forward-sws ()
1484 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1486 ;; This function might do hidden buffer changes.
1488 (let (;; `rung-pos' is set to a position as early as possible in the
1489 ;; unmarked part of the simple ws region.
1490 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1491 rung-is-marked next-rung-is-marked simple-ws-end
1492 ;; `safe-start' is set when it's safe to cache the start position.
1493 ;; It's not set if we've initially skipped over comments and line
1494 ;; continuations since we might have gone out through the end of a
1495 ;; macro then. This provision makes `c-forward-sws' not populate the
1496 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1497 ;; more common.
1498 safe-start)
1500 ;; Skip simple ws and do a quick check on the following character to see
1501 ;; if it's anything that can't start syntactic ws, so we can bail out
1502 ;; early in the majority of cases when there just are a few ws chars.
1503 (skip-chars-forward " \t\n\r\f\v")
1504 (when (looking-at c-syntactic-ws-start)
1506 (setq rung-end-pos (min (1+ (point)) (point-max)))
1507 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1508 'c-is-sws t))
1509 ;; Find the last rung position to avoid setting properties in all
1510 ;; the cases when the marked rung is complete.
1511 ;; (`next-single-property-change' is certain to move at least one
1512 ;; step forward.)
1513 (setq rung-pos (1- (next-single-property-change
1514 rung-is-marked 'c-is-sws nil rung-end-pos)))
1515 ;; Got no marked rung here. Since the simple ws might have started
1516 ;; inside a line comment or cpp directive we must set `rung-pos' as
1517 ;; high as possible.
1518 (setq rung-pos (point)))
1520 (while
1521 (progn
1522 (while
1523 (when (and rung-is-marked
1524 (get-text-property (point) 'c-in-sws))
1526 ;; The following search is the main reason that `c-in-sws'
1527 ;; and `c-is-sws' aren't combined to one property.
1528 (goto-char (next-single-property-change
1529 (point) 'c-in-sws nil (point-max)))
1530 (unless (get-text-property (point) 'c-is-sws)
1531 ;; If the `c-in-sws' region extended past the last
1532 ;; `c-is-sws' char we have to go back a bit.
1533 (or (get-text-property (1- (point)) 'c-is-sws)
1534 (goto-char (previous-single-property-change
1535 (point) 'c-is-sws)))
1536 (backward-char))
1538 (c-debug-sws-msg
1539 "c-forward-sws cached move %s -> %s (max %s)"
1540 rung-pos (point) (point-max))
1542 (setq rung-pos (point))
1543 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1544 (not (eobp))))
1546 ;; We'll loop here if there is simple ws after the last rung.
1547 ;; That means that there's been some change in it and it's
1548 ;; possible that we've stepped into another ladder, so extend
1549 ;; the previous one to join with it if there is one, and try to
1550 ;; use the cache again.
1551 (c-debug-sws-msg
1552 "c-forward-sws extending rung with [%s..%s] (max %s)"
1553 (1+ rung-pos) (1+ (point)) (point-max))
1554 (unless (get-text-property (point) 'c-is-sws)
1555 ;; Remove any `c-in-sws' property from the last char of
1556 ;; the rung before we mark it with `c-is-sws', so that we
1557 ;; won't connect with the remains of a broken "ladder".
1558 (c-remove-in-sws (point) (1+ (point))))
1559 (c-put-is-sws (1+ rung-pos)
1560 (1+ (point)))
1561 (c-put-in-sws rung-pos
1562 (setq rung-pos (point)
1563 last-put-in-sws-pos rung-pos)))
1565 (setq simple-ws-end (point))
1566 (c-forward-comments)
1568 (cond
1569 ((/= (point) simple-ws-end)
1570 ;; Skipped over comments. Don't cache at eob in case the buffer
1571 ;; is narrowed.
1572 (not (eobp)))
1574 ((save-excursion
1575 (and c-opt-cpp-prefix
1576 (looking-at c-opt-cpp-start)
1577 (progn (skip-chars-backward " \t")
1578 (bolp))
1579 (or (bobp)
1580 (progn (backward-char)
1581 (not (eq (char-before) ?\\))))))
1582 ;; Skip a preprocessor directive.
1583 (end-of-line)
1584 (while (and (eq (char-before) ?\\)
1585 (= (forward-line 1) 0))
1586 (end-of-line))
1587 (forward-line 1)
1588 (setq safe-start t)
1589 ;; Don't cache at eob in case the buffer is narrowed.
1590 (not (eobp)))))
1592 ;; We've searched over a piece of non-white syntactic ws. See if this
1593 ;; can be cached.
1594 (setq next-rung-pos (point))
1595 (skip-chars-forward " \t\n\r\f\v")
1596 (setq rung-end-pos (min (1+ (point)) (point-max)))
1598 (if (or
1599 ;; Cache if we haven't skipped comments only, and if we started
1600 ;; either from a marked rung or from a completely uncached
1601 ;; position.
1602 (and safe-start
1603 (or rung-is-marked
1604 (not (get-text-property simple-ws-end 'c-in-sws))))
1606 ;; See if there's a marked rung in the encountered simple ws. If
1607 ;; so then we can cache, unless `safe-start' is nil. Even then
1608 ;; we need to do this to check if the cache can be used for the
1609 ;; next step.
1610 (and (setq next-rung-is-marked
1611 (text-property-any next-rung-pos rung-end-pos
1612 'c-is-sws t))
1613 safe-start))
1615 (progn
1616 (c-debug-sws-msg
1617 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1618 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1619 (point-max))
1621 ;; Remove the properties for any nested ws that might be cached.
1622 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1623 ;; anyway.
1624 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1625 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1626 (c-put-is-sws rung-pos
1627 (1+ simple-ws-end))
1628 (setq rung-is-marked t))
1629 (c-put-in-sws rung-pos
1630 (setq rung-pos (point)
1631 last-put-in-sws-pos rung-pos))
1632 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1633 ;; Remove any `c-in-sws' property from the last char of
1634 ;; the rung before we mark it with `c-is-sws', so that we
1635 ;; won't connect with the remains of a broken "ladder".
1636 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1637 (c-put-is-sws next-rung-pos
1638 rung-end-pos))
1640 (c-debug-sws-msg
1641 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1642 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1643 (point-max))
1645 ;; Set `rung-pos' for the next rung. It's the same thing here as
1646 ;; initially, except that the rung position is set as early as
1647 ;; possible since we can't be in the ending ws of a line comment or
1648 ;; cpp directive now.
1649 (if (setq rung-is-marked next-rung-is-marked)
1650 (setq rung-pos (1- (next-single-property-change
1651 rung-is-marked 'c-is-sws nil rung-end-pos)))
1652 (setq rung-pos next-rung-pos))
1653 (setq safe-start t)))
1655 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1656 ;; another one after the point (which might occur when editing inside a
1657 ;; comment or macro).
1658 (when (eq last-put-in-sws-pos (point))
1659 (cond ((< last-put-in-sws-pos (point-max))
1660 (c-debug-sws-msg
1661 "c-forward-sws clearing at %s for cache separation"
1662 last-put-in-sws-pos)
1663 (c-remove-in-sws last-put-in-sws-pos
1664 (1+ last-put-in-sws-pos)))
1666 ;; If at eob we have to clear the last character before the end
1667 ;; instead since the buffer might be narrowed and there might
1668 ;; be a `c-in-sws' after (point-max). In this case it's
1669 ;; necessary to clear both properties.
1670 (c-debug-sws-msg
1671 "c-forward-sws clearing thoroughly at %s for cache separation"
1672 (1- last-put-in-sws-pos))
1673 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1674 last-put-in-sws-pos))))
1677 (defun c-backward-sws ()
1678 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1680 ;; This function might do hidden buffer changes.
1682 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1683 ;; part of the simple ws region.
1684 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1685 rung-is-marked simple-ws-beg cmt-skip-pos)
1687 ;; Skip simple horizontal ws and do a quick check on the preceding
1688 ;; character to see if it's anying that can't end syntactic ws, so we can
1689 ;; bail out early in the majority of cases when there just are a few ws
1690 ;; chars. Newlines are complicated in the backward direction, so we can't
1691 ;; skip over them.
1692 (skip-chars-backward " \t\f")
1693 (when (and (not (bobp))
1694 (save-excursion
1695 (backward-char)
1696 (looking-at c-syntactic-ws-end)))
1698 ;; Try to find a rung position in the simple ws preceding point, so that
1699 ;; we can get a cache hit even if the last bit of the simple ws has
1700 ;; changed recently.
1701 (setq simple-ws-beg (point))
1702 (skip-chars-backward " \t\n\r\f\v")
1703 (if (setq rung-is-marked (text-property-any
1704 (point) (min (1+ rung-pos) (point-max))
1705 'c-is-sws t))
1706 ;; `rung-pos' will be the earliest marked position, which means that
1707 ;; there might be later unmarked parts in the simple ws region.
1708 ;; It's not worth the effort to fix that; the last part of the
1709 ;; simple ws is also typically edited often, so it could be wasted.
1710 (goto-char (setq rung-pos rung-is-marked))
1711 (goto-char simple-ws-beg))
1713 (while
1714 (progn
1715 (while
1716 (when (and rung-is-marked
1717 (not (bobp))
1718 (get-text-property (1- (point)) 'c-in-sws))
1720 ;; The following search is the main reason that `c-in-sws'
1721 ;; and `c-is-sws' aren't combined to one property.
1722 (goto-char (previous-single-property-change
1723 (point) 'c-in-sws nil (point-min)))
1724 (unless (get-text-property (point) 'c-is-sws)
1725 ;; If the `c-in-sws' region extended past the first
1726 ;; `c-is-sws' char we have to go forward a bit.
1727 (goto-char (next-single-property-change
1728 (point) 'c-is-sws)))
1730 (c-debug-sws-msg
1731 "c-backward-sws cached move %s <- %s (min %s)"
1732 (point) rung-pos (point-min))
1734 (setq rung-pos (point))
1735 (if (and (< (min (skip-chars-backward " \t\f\v")
1736 (progn
1737 (setq simple-ws-beg (point))
1738 (skip-chars-backward " \t\n\r\f\v")))
1740 (setq rung-is-marked
1741 (text-property-any (point) rung-pos
1742 'c-is-sws t)))
1744 (goto-char simple-ws-beg)
1745 nil))
1747 ;; We'll loop here if there is simple ws before the first rung.
1748 ;; That means that there's been some change in it and it's
1749 ;; possible that we've stepped into another ladder, so extend
1750 ;; the previous one to join with it if there is one, and try to
1751 ;; use the cache again.
1752 (c-debug-sws-msg
1753 "c-backward-sws extending rung with [%s..%s] (min %s)"
1754 rung-is-marked rung-pos (point-min))
1755 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1756 ;; Remove any `c-in-sws' property from the last char of
1757 ;; the rung before we mark it with `c-is-sws', so that we
1758 ;; won't connect with the remains of a broken "ladder".
1759 (c-remove-in-sws (1- rung-pos) rung-pos))
1760 (c-put-is-sws rung-is-marked
1761 rung-pos)
1762 (c-put-in-sws rung-is-marked
1763 (1- rung-pos))
1764 (setq rung-pos rung-is-marked
1765 last-put-in-sws-pos rung-pos))
1767 (c-backward-comments)
1768 (setq cmt-skip-pos (point))
1770 (cond
1771 ((and c-opt-cpp-prefix
1772 (/= cmt-skip-pos simple-ws-beg)
1773 (c-beginning-of-macro))
1774 ;; Inside a cpp directive. See if it should be skipped over.
1775 (let ((cpp-beg (point)))
1777 ;; Move back over all line continuations in the region skipped
1778 ;; over by `c-backward-comments'. If we go past it then we
1779 ;; started inside the cpp directive.
1780 (goto-char simple-ws-beg)
1781 (beginning-of-line)
1782 (while (and (> (point) cmt-skip-pos)
1783 (progn (backward-char)
1784 (eq (char-before) ?\\)))
1785 (beginning-of-line))
1787 (if (< (point) cmt-skip-pos)
1788 ;; Don't move past the cpp directive if we began inside
1789 ;; it. Note that the position at the end of the last line
1790 ;; of the macro is also considered to be within it.
1791 (progn (goto-char cmt-skip-pos)
1792 nil)
1794 ;; It's worthwhile to spend a little bit of effort on finding
1795 ;; the end of the macro, to get a good `simple-ws-beg'
1796 ;; position for the cache. Note that `c-backward-comments'
1797 ;; could have stepped over some comments before going into
1798 ;; the macro, and then `simple-ws-beg' must be kept on the
1799 ;; same side of those comments.
1800 (goto-char simple-ws-beg)
1801 (skip-chars-backward " \t\n\r\f\v")
1802 (if (eq (char-before) ?\\)
1803 (forward-char))
1804 (forward-line 1)
1805 (if (< (point) simple-ws-beg)
1806 ;; Might happen if comments after the macro were skipped
1807 ;; over.
1808 (setq simple-ws-beg (point)))
1810 (goto-char cpp-beg)
1811 t)))
1813 ((/= (save-excursion
1814 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1815 (setq next-rung-pos (point)))
1816 simple-ws-beg)
1817 ;; Skipped over comments. Must put point at the end of
1818 ;; the simple ws at point since we might be after a line
1819 ;; comment or cpp directive that's been partially
1820 ;; narrowed out, and we can't risk marking the simple ws
1821 ;; at the end of it.
1822 (goto-char next-rung-pos)
1823 t)))
1825 ;; We've searched over a piece of non-white syntactic ws. See if this
1826 ;; can be cached.
1827 (setq next-rung-pos (point))
1828 (skip-chars-backward " \t\f\v")
1830 (if (or
1831 ;; Cache if we started either from a marked rung or from a
1832 ;; completely uncached position.
1833 rung-is-marked
1834 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1836 ;; Cache if there's a marked rung in the encountered simple ws.
1837 (save-excursion
1838 (skip-chars-backward " \t\n\r\f\v")
1839 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1840 'c-is-sws t)))
1842 (progn
1843 (c-debug-sws-msg
1844 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1845 (point) (1+ next-rung-pos)
1846 simple-ws-beg (min (1+ rung-pos) (point-max))
1847 (point-min))
1849 ;; Remove the properties for any nested ws that might be cached.
1850 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1851 ;; anyway.
1852 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1853 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1854 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1855 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1856 ;; Remove any `c-in-sws' property from the last char of
1857 ;; the rung before we mark it with `c-is-sws', so that we
1858 ;; won't connect with the remains of a broken "ladder".
1859 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1860 (c-put-is-sws simple-ws-beg
1861 rung-end-pos)
1862 (setq rung-is-marked t)))
1863 (c-put-in-sws (setq simple-ws-beg (point)
1864 last-put-in-sws-pos simple-ws-beg)
1865 rung-pos)
1866 (c-put-is-sws (setq rung-pos simple-ws-beg)
1867 (1+ next-rung-pos)))
1869 (c-debug-sws-msg
1870 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1871 (point) (1+ next-rung-pos)
1872 simple-ws-beg (min (1+ rung-pos) (point-max))
1873 (point-min))
1874 (setq rung-pos next-rung-pos
1875 simple-ws-beg (point))
1878 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1879 ;; another one before the point (which might occur when editing inside a
1880 ;; comment or macro).
1881 (when (eq last-put-in-sws-pos (point))
1882 (cond ((< (point-min) last-put-in-sws-pos)
1883 (c-debug-sws-msg
1884 "c-backward-sws clearing at %s for cache separation"
1885 (1- last-put-in-sws-pos))
1886 (c-remove-in-sws (1- last-put-in-sws-pos)
1887 last-put-in-sws-pos))
1888 ((> (point-min) 1)
1889 ;; If at bob and the buffer is narrowed, we have to clear the
1890 ;; character we're standing on instead since there might be a
1891 ;; `c-in-sws' before (point-min). In this case it's necessary
1892 ;; to clear both properties.
1893 (c-debug-sws-msg
1894 "c-backward-sws clearing thoroughly at %s for cache separation"
1895 last-put-in-sws-pos)
1896 (c-remove-is-and-in-sws last-put-in-sws-pos
1897 (1+ last-put-in-sws-pos)))))
1901 ;; A system for finding noteworthy parens before the point.
1903 (defvar c-state-cache nil)
1904 (make-variable-buffer-local 'c-state-cache)
1905 ;; The state cache used by `c-parse-state' to cut down the amount of
1906 ;; searching. It's the result from some earlier `c-parse-state' call.
1908 ;; The use of the cached info is more effective if the next
1909 ;; `c-parse-state' call is on a line close by the one the cached state
1910 ;; was made at; the cache can actually slow down a little if the
1911 ;; cached state was made very far back in the buffer. The cache is
1912 ;; most effective if `c-parse-state' is used on each line while moving
1913 ;; forward.
1915 (defvar c-state-cache-start 1)
1916 (make-variable-buffer-local 'c-state-cache-start)
1917 ;; This is (point-min) when `c-state-cache' was calculated, since a
1918 ;; change of narrowing is likely to affect the parens that are visible
1919 ;; before the point.
1921 (defvar c-state-cache-good-pos 1)
1922 (make-variable-buffer-local 'c-state-cache-good-pos)
1923 ;; This is a position where `c-state-cache' is known to be correct.
1924 ;; It's a position inside one of the recorded unclosed parens or the
1925 ;; top level, but not further nested inside any literal or subparen
1926 ;; that is closed before the last recorded position.
1928 ;; The exact position is chosen to try to be close to yet earlier than
1929 ;; the position where `c-state-cache' will be called next. Right now
1930 ;; the heuristic is to set it to the position after the last found
1931 ;; closing paren (of any type) before the line on which
1932 ;; `c-parse-state' was called. That is chosen primarily to work well
1933 ;; with refontification of the current line.
1935 (defsubst c-invalidate-state-cache (pos)
1936 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1937 ;; at POS or higher. This is much like `c-whack-state-after', but
1938 ;; it never changes a paren pair element into an open paren element.
1939 ;; Doing that would mean that the new open paren wouldn't have the
1940 ;; required preceding paren pair element.
1941 (while (and (or c-state-cache
1942 (when (< pos c-state-cache-good-pos)
1943 (setq c-state-cache-good-pos 1)
1944 nil))
1945 (let ((elem (car c-state-cache)))
1946 (if (consp elem)
1947 (or (< pos (cdr elem))
1948 (when (< pos c-state-cache-good-pos)
1949 (setq c-state-cache-good-pos (cdr elem))
1950 nil))
1951 (or (<= pos elem)
1952 (when (< pos c-state-cache-good-pos)
1953 (setq c-state-cache-good-pos (1+ elem))
1954 nil)))))
1955 (setq c-state-cache (cdr c-state-cache))))
1957 (defun c-get-fallback-start-pos (here)
1958 ;; Return the start position for building `c-state-cache' from
1959 ;; scratch.
1960 (save-excursion
1961 ;; Go back 2 bods, but ignore any bogus positions returned by
1962 ;; beginning-of-defun (i.e. open paren in column zero).
1963 (goto-char here)
1964 (let ((cnt 2))
1965 (while (not (or (bobp) (zerop cnt)))
1966 (c-beginning-of-defun-1)
1967 (if (eq (char-after) ?\{)
1968 (setq cnt (1- cnt)))))
1969 (point)))
1971 (defun c-parse-state ()
1972 ;; Find and record all noteworthy parens between some good point
1973 ;; earlier in the file and point. That good point is at least the
1974 ;; beginning of the top-level construct we are in, or the beginning
1975 ;; of the preceding top-level construct if we aren't in one.
1977 ;; The returned value is a list of the noteworthy parens with the
1978 ;; last one first. If an element in the list is an integer, it's
1979 ;; the position of an open paren which has not been closed before
1980 ;; the point. If an element is a cons, it gives the position of a
1981 ;; closed brace paren pair; the car is the start paren position and
1982 ;; the cdr is the position following the closing paren. Only the
1983 ;; last closed brace paren pair before each open paren and before
1984 ;; the point is recorded, and thus the state never contains two cons
1985 ;; elements in succession.
1987 ;; Currently no characters which are given paren syntax with the
1988 ;; syntax-table property are recorded, i.e. angle bracket arglist
1989 ;; parens are never present here. Note that this might change.
1991 ;; BUG: This function doesn't cope entirely well with unbalanced
1992 ;; parens in macros. E.g. in the following case the brace before
1993 ;; the macro isn't balanced with the one after it:
1995 ;; {
1996 ;; #define X {
1997 ;; }
1999 ;; This function might do hidden buffer changes.
2001 (save-restriction
2002 (let* ((here (point))
2003 (here-bol (c-point 'bol))
2004 (c-macro-start (c-query-macro-start))
2005 (in-macro-start (or c-macro-start (point)))
2006 old-state last-pos brace-pair-open brace-pair-close
2007 pos save-pos)
2008 (c-invalidate-state-cache here)
2010 ;; If the minimum position has changed due to narrowing then we
2011 ;; have to fix the tail of `c-state-cache' accordingly.
2012 (unless (= c-state-cache-start (point-min))
2013 (if (> (point-min) c-state-cache-start)
2014 ;; If point-min has moved forward then we just need to cut
2015 ;; off a bit of the tail.
2016 (let ((ptr (cons nil c-state-cache)) elem)
2017 (while (and (setq elem (car-safe (cdr ptr)))
2018 (>= (if (consp elem) (car elem) elem)
2019 (point-min)))
2020 (setq ptr (cdr ptr)))
2021 (when (consp ptr)
2022 (if (eq (cdr ptr) c-state-cache)
2023 (setq c-state-cache nil
2024 c-state-cache-good-pos 1)
2025 (setcdr ptr nil))))
2026 ;; If point-min has moved backward then we drop the state
2027 ;; completely. It's possible to do a better job here and
2028 ;; recalculate the top only.
2029 (setq c-state-cache nil
2030 c-state-cache-good-pos 1))
2031 (setq c-state-cache-start (point-min)))
2033 ;; Get the latest position we know are directly inside the
2034 ;; closest containing paren of the cached state.
2035 (setq last-pos (and c-state-cache
2036 (if (consp (car c-state-cache))
2037 (cdr (car c-state-cache))
2038 (1+ (car c-state-cache)))))
2039 (if (or (not last-pos)
2040 (< last-pos c-state-cache-good-pos))
2041 (setq last-pos c-state-cache-good-pos)
2042 ;; Take the opportunity to move the cached good position
2043 ;; further down.
2044 (if (< last-pos here-bol)
2045 (setq c-state-cache-good-pos last-pos)))
2047 ;; Check if `last-pos' is in a macro. If it is, and we're not
2048 ;; in the same macro, we must discard everything on
2049 ;; `c-state-cache' that is inside the macro before using it.
2050 (save-excursion
2051 (goto-char last-pos)
2052 (when (and (c-beginning-of-macro)
2053 (/= (point) in-macro-start))
2054 (c-invalidate-state-cache (point))
2055 ;; Set `last-pos' again just like above except that there's
2056 ;; no use looking at `c-state-cache-good-pos' here.
2057 (setq last-pos (if c-state-cache
2058 (if (consp (car c-state-cache))
2059 (cdr (car c-state-cache))
2060 (1+ (car c-state-cache)))
2061 1))))
2063 ;; If we've moved very far from the last cached position then
2064 ;; it's probably better to redo it from scratch, otherwise we
2065 ;; might spend a lot of time searching from `last-pos' down to
2066 ;; here.
2067 (when (< last-pos (- here 20000))
2068 ;; First get the fallback start position. If it turns out
2069 ;; that it's so far back that the cached state is closer then
2070 ;; we'll keep it afterall.
2071 (setq pos (c-get-fallback-start-pos here))
2072 (if (<= pos last-pos)
2073 (setq pos nil)
2074 (setq last-pos nil
2075 c-state-cache nil
2076 c-state-cache-good-pos 1)))
2078 ;; Find the start position for the forward search. (Can't
2079 ;; search in the backward direction since the point might be in
2080 ;; some kind of literal.)
2082 (unless pos
2083 (setq old-state c-state-cache)
2085 ;; There's a cached state with a containing paren. Pop off
2086 ;; the stale containing sexps from it by going forward out of
2087 ;; parens as far as possible.
2088 (narrow-to-region (point-min) here)
2089 (let (placeholder pair-beg)
2090 (while (and c-state-cache
2091 (setq placeholder
2092 (c-up-list-forward last-pos)))
2093 (setq last-pos placeholder)
2094 (if (consp (car c-state-cache))
2095 (setq pair-beg (car-safe (cdr c-state-cache))
2096 c-state-cache (cdr-safe (cdr c-state-cache)))
2097 (setq pair-beg (car c-state-cache)
2098 c-state-cache (cdr c-state-cache))))
2100 (when (and pair-beg (eq (char-after pair-beg) ?{))
2101 ;; The last paren pair we moved out from was a brace
2102 ;; pair. Modify the state to record this as a closed
2103 ;; pair now.
2104 (if (consp (car-safe c-state-cache))
2105 (setq c-state-cache (cdr c-state-cache)))
2106 (setq c-state-cache (cons (cons pair-beg last-pos)
2107 c-state-cache))))
2109 ;; Check if the preceding balanced paren is within a
2110 ;; macro; it should be ignored if we're outside the
2111 ;; macro. There's no need to check any further upwards;
2112 ;; if the macro contains an unbalanced opening paren then
2113 ;; we're smoked anyway.
2114 (when (and (<= (point) in-macro-start)
2115 (consp (car c-state-cache)))
2116 (save-excursion
2117 (goto-char (car (car c-state-cache)))
2118 (when (c-beginning-of-macro)
2119 (setq here (point)
2120 c-state-cache (cdr c-state-cache)))))
2122 (unless (eq c-state-cache old-state)
2123 ;; Have to adjust the cached good position if state has been
2124 ;; popped off.
2125 (setq c-state-cache-good-pos
2126 (if c-state-cache
2127 (if (consp (car c-state-cache))
2128 (cdr (car c-state-cache))
2129 (1+ (car c-state-cache)))
2131 old-state c-state-cache))
2133 (when c-state-cache
2134 (setq pos last-pos)))
2136 ;; Get the fallback start position.
2137 (unless pos
2138 (setq pos (c-get-fallback-start-pos here)
2139 c-state-cache nil
2140 c-state-cache-good-pos 1))
2142 (narrow-to-region (point-min) here)
2144 (while pos
2145 (setq save-pos pos
2146 brace-pair-open nil)
2148 ;; Find the balanced brace pairs. This loop is hot, so it
2149 ;; does ugly tricks to go faster.
2150 (c-safe
2151 (let (set-good-pos set-brace-pair)
2152 (while t
2153 (setq last-pos nil
2154 last-pos (scan-lists pos 1 -1)) ; Might signal.
2155 (setq pos (scan-lists last-pos 1 1) ; Might signal.
2156 set-good-pos (< pos here-bol)
2157 set-brace-pair (eq (char-before last-pos) ?{))
2159 ;; Update the cached good position and record the brace
2160 ;; pair, whichever is applicable for the paren we've
2161 ;; just jumped over. But first check that it isn't
2162 ;; inside a macro and the point isn't inside the same
2163 ;; one.
2164 (when (and (or set-good-pos set-brace-pair)
2165 (or (>= pos in-macro-start)
2166 (save-excursion
2167 (goto-char pos)
2168 (not (c-beginning-of-macro)))))
2169 (if set-good-pos
2170 (setq c-state-cache-good-pos pos))
2171 (if set-brace-pair
2172 (setq brace-pair-open last-pos
2173 brace-pair-close pos))))))
2175 ;; Record the last brace pair.
2176 (when brace-pair-open
2177 (let ((head (car-safe c-state-cache)))
2178 (if (consp head)
2179 (progn
2180 (setcar head (1- brace-pair-open))
2181 (setcdr head brace-pair-close))
2182 (setq c-state-cache (cons (cons (1- brace-pair-open)
2183 brace-pair-close)
2184 c-state-cache)))))
2186 (if last-pos
2187 ;; Prepare to loop, but record the open paren only if it's
2188 ;; outside a macro or within the same macro as point, and
2189 ;; if it is a legitimate open paren and not some character
2190 ;; that got an open paren syntax-table property.
2191 (progn
2192 (setq pos last-pos)
2193 (when (and (or (>= last-pos in-macro-start)
2194 (save-excursion
2195 (goto-char last-pos)
2196 (not (c-beginning-of-macro))))
2197 ;; Check for known types of parens that we
2198 ;; want to record. The syntax table is not to
2199 ;; be trusted here since the caller might be
2200 ;; using e.g. `c++-template-syntax-table'.
2201 (memq (char-before last-pos) '(?{ ?\( ?\[)))
2202 (if (< last-pos here-bol)
2203 (setq c-state-cache-good-pos last-pos))
2204 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
2206 (if (setq last-pos (c-up-list-forward pos))
2207 ;; Found a close paren without a corresponding opening
2208 ;; one. Maybe we didn't go back far enough, so try to
2209 ;; scan backward for the start paren and then start over.
2210 (progn
2211 (setq pos (c-up-list-backward pos)
2212 c-state-cache nil
2213 c-state-cache-good-pos c-state-cache-start)
2214 (when (or (not pos)
2215 ;; Emacs (up to at least 21.2) can get confused by
2216 ;; open parens in column zero inside comments: The
2217 ;; sexp functions can then misbehave and bring us
2218 ;; back to the same point again. Check this so that
2219 ;; we don't get an infinite loop.
2220 (>= pos save-pos))
2221 (setq pos last-pos
2222 c-parsing-error
2223 (format "Unbalanced close paren at line %d"
2224 (1+ (count-lines (point-min)
2225 (c-point 'bol last-pos)))))))
2226 (setq pos nil))))
2228 ;;(message "c-parse-state: %S end: %S" c-state-cache c-state-cache-good-pos)
2229 c-state-cache)))
2231 ;; Debug tool to catch cache inconsistencies.
2232 (defvar c-debug-parse-state nil)
2233 (unless (fboundp 'c-real-parse-state)
2234 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
2235 (cc-bytecomp-defun c-real-parse-state)
2236 (defun c-debug-parse-state ()
2237 (let ((res1 (c-real-parse-state)) res2)
2238 (let ((c-state-cache nil)
2239 (c-state-cache-start 1)
2240 (c-state-cache-good-pos 1))
2241 (setq res2 (c-real-parse-state)))
2242 (unless (equal res1 res2)
2243 ;; The cache can actually go further back due to the ad-hoc way
2244 ;; the first paren is found, so try to whack off a bit of its
2245 ;; start before complaining.
2246 (save-excursion
2247 (goto-char (or (c-least-enclosing-brace res2) (point)))
2248 (c-beginning-of-defun-1)
2249 (while (not (or (bobp) (eq (char-after) ?{)))
2250 (c-beginning-of-defun-1))
2251 (unless (equal (c-whack-state-before (point) res1) res2)
2252 (message (concat "c-parse-state inconsistency: "
2253 "using cache: %s, from scratch: %s")
2254 res1 res2))))
2255 res1))
2256 (defun c-toggle-parse-state-debug (&optional arg)
2257 (interactive "P")
2258 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
2259 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
2260 'c-debug-parse-state
2261 'c-real-parse-state)))
2262 (c-keep-region-active))
2263 (when c-debug-parse-state
2264 (c-toggle-parse-state-debug 1))
2266 (defun c-whack-state-before (bufpos paren-state)
2267 ;; Whack off any state information from PAREN-STATE which lies
2268 ;; before BUFPOS. Not destructive on PAREN-STATE.
2269 (let* ((newstate (list nil))
2270 (ptr newstate)
2271 car)
2272 (while paren-state
2273 (setq car (car paren-state)
2274 paren-state (cdr paren-state))
2275 (if (< (if (consp car) (car car) car) bufpos)
2276 (setq paren-state nil)
2277 (setcdr ptr (list car))
2278 (setq ptr (cdr ptr))))
2279 (cdr newstate)))
2281 (defun c-whack-state-after (bufpos paren-state)
2282 ;; Whack off any state information from PAREN-STATE which lies at or
2283 ;; after BUFPOS. Not destructive on PAREN-STATE.
2284 (catch 'done
2285 (while paren-state
2286 (let ((car (car paren-state)))
2287 (if (consp car)
2288 ;; just check the car, because in a balanced brace
2289 ;; expression, it must be impossible for the corresponding
2290 ;; close brace to be before point, but the open brace to
2291 ;; be after.
2292 (if (<= bufpos (car car))
2293 nil ; whack it off
2294 (if (< bufpos (cdr car))
2295 ;; its possible that the open brace is before
2296 ;; bufpos, but the close brace is after. In that
2297 ;; case, convert this to a non-cons element. The
2298 ;; rest of the state is before bufpos, so we're
2299 ;; done.
2300 (throw 'done (cons (car car) (cdr paren-state)))
2301 ;; we know that both the open and close braces are
2302 ;; before bufpos, so we also know that everything else
2303 ;; on state is before bufpos.
2304 (throw 'done paren-state)))
2305 (if (<= bufpos car)
2306 nil ; whack it off
2307 ;; it's before bufpos, so everything else should too.
2308 (throw 'done paren-state)))
2309 (setq paren-state (cdr paren-state)))
2310 nil)))
2312 (defun c-most-enclosing-brace (paren-state &optional bufpos)
2313 ;; Return the bufpos of the innermost enclosing open paren before
2314 ;; bufpos, or nil if none was found.
2315 (let (enclosingp)
2316 (or bufpos (setq bufpos 134217727))
2317 (while paren-state
2318 (setq enclosingp (car paren-state)
2319 paren-state (cdr paren-state))
2320 (if (or (consp enclosingp)
2321 (>= enclosingp bufpos))
2322 (setq enclosingp nil)
2323 (setq paren-state nil)))
2324 enclosingp))
2326 (defun c-least-enclosing-brace (paren-state)
2327 ;; Return the bufpos of the outermost enclosing open paren, or nil
2328 ;; if none was found.
2329 (let (pos elem)
2330 (while paren-state
2331 (setq elem (car paren-state)
2332 paren-state (cdr paren-state))
2333 (if (integerp elem)
2334 (setq pos elem)))
2335 pos))
2337 (defun c-safe-position (bufpos paren-state)
2338 ;; Return the closest "safe" position recorded on PAREN-STATE that
2339 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
2340 ;; contain any. Return nil if BUFPOS is nil, which is useful to
2341 ;; find the closest limit before a given limit that might be nil.
2343 ;; A "safe" position is a position at or after a recorded open
2344 ;; paren, or after a recorded close paren. The returned position is
2345 ;; thus either the first position after a close brace, or the first
2346 ;; position after an enclosing paren, or at the enclosing paren in
2347 ;; case BUFPOS is immediately after it.
2348 (when bufpos
2349 (let (elem)
2350 (catch 'done
2351 (while paren-state
2352 (setq elem (car paren-state))
2353 (if (consp elem)
2354 (cond ((< (cdr elem) bufpos)
2355 (throw 'done (cdr elem)))
2356 ((< (car elem) bufpos)
2357 ;; See below.
2358 (throw 'done (min (1+ (car elem)) bufpos))))
2359 (if (< elem bufpos)
2360 ;; elem is the position at and not after the opening paren, so
2361 ;; we can go forward one more step unless it's equal to
2362 ;; bufpos. This is useful in some cases avoid an extra paren
2363 ;; level between the safe position and bufpos.
2364 (throw 'done (min (1+ elem) bufpos))))
2365 (setq paren-state (cdr paren-state)))))))
2367 (defun c-beginning-of-syntax ()
2368 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2369 ;; goes to the closest previous point that is known to be outside
2370 ;; any string literal or comment. `c-state-cache' is used if it has
2371 ;; a position in the vicinity.
2372 (let* ((paren-state c-state-cache)
2373 elem
2375 (pos (catch 'done
2376 ;; Note: Similar code in `c-safe-position'. The
2377 ;; difference is that we accept a safe position at
2378 ;; the point and don't bother to go forward past open
2379 ;; parens.
2380 (while paren-state
2381 (setq elem (car paren-state))
2382 (if (consp elem)
2383 (cond ((<= (cdr elem) (point))
2384 (throw 'done (cdr elem)))
2385 ((<= (car elem) (point))
2386 (throw 'done (car elem))))
2387 (if (<= elem (point))
2388 (throw 'done elem)))
2389 (setq paren-state (cdr paren-state)))
2390 (point-min))))
2392 (if (> pos (- (point) 4000))
2393 (goto-char pos)
2394 ;; The position is far back. Try `c-beginning-of-defun-1'
2395 ;; (although we can't be entirely sure it will go to a position
2396 ;; outside a comment or string in current emacsen). FIXME:
2397 ;; Consult `syntax-ppss' here.
2398 (c-beginning-of-defun-1)
2399 (if (< (point) pos)
2400 (goto-char pos)))))
2403 ;; Tools for scanning identifiers and other tokens.
2405 (defun c-on-identifier ()
2406 "Return non-nil if the point is on or directly after an identifier.
2407 Keywords are recognized and not considered identifiers. If an
2408 identifier is detected, the returned value is its starting position.
2409 If an identifier ends at the point and another begins at it \(can only
2410 happen in Pike) then the point for the preceding one is returned.
2412 Note that this function might do hidden buffer changes. See the
2413 comment at the start of cc-engine.el for more info."
2415 ;; FIXME: Shouldn't this function handle "operator" in C++?
2417 (save-excursion
2418 (skip-syntax-backward "w_")
2422 ;; Check for a normal (non-keyword) identifier.
2423 (and (looking-at c-symbol-start)
2424 (not (looking-at c-keywords-regexp))
2425 (point))
2427 (when (c-major-mode-is 'pike-mode)
2428 ;; Handle the `<operator> syntax in Pike.
2429 (let ((pos (point)))
2430 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2431 (and (if (< (skip-chars-backward "`") 0)
2433 (goto-char pos)
2434 (eq (char-after) ?\`))
2435 (looking-at c-symbol-key)
2436 (>= (match-end 0) pos)
2437 (point))))
2439 ;; Handle the "operator +" syntax in C++.
2440 (when (and c-overloadable-operators-regexp
2441 (= (c-backward-token-2 0) 0))
2443 (cond ((and (looking-at c-overloadable-operators-regexp)
2444 (or (not c-opt-op-identitier-prefix)
2445 (and (= (c-backward-token-2 1) 0)
2446 (looking-at c-opt-op-identitier-prefix))))
2447 (point))
2449 ((save-excursion
2450 (and c-opt-op-identitier-prefix
2451 (looking-at c-opt-op-identitier-prefix)
2452 (= (c-forward-token-2 1) 0)
2453 (looking-at c-overloadable-operators-regexp)))
2454 (point))))
2458 (defsubst c-simple-skip-symbol-backward ()
2459 ;; If the point is at the end of a symbol then skip backward to the
2460 ;; beginning of it. Don't move otherwise. Return non-nil if point
2461 ;; moved.
2463 ;; This function might do hidden buffer changes.
2464 (or (< (skip-syntax-backward "w_") 0)
2465 (and (c-major-mode-is 'pike-mode)
2466 ;; Handle the `<operator> syntax in Pike.
2467 (let ((pos (point)))
2468 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2469 (< (skip-chars-backward "`") 0)
2470 (looking-at c-symbol-key)
2471 (>= (match-end 0) pos))
2473 (goto-char pos)
2474 nil)))))
2476 (defun c-beginning-of-current-token (&optional back-limit)
2477 ;; Move to the beginning of the current token. Do not move if not
2478 ;; in the middle of one. BACK-LIMIT may be used to bound the
2479 ;; backward search; if given it's assumed to be at the boundary
2480 ;; between two tokens.
2482 ;; This function might do hidden buffer changes.
2483 (if (looking-at "\\w\\|\\s_")
2484 (skip-syntax-backward "w_" back-limit)
2485 (let ((start (point)))
2486 (when (< (skip-syntax-backward ".()" back-limit) 0)
2487 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2488 (match-end 0))
2489 ;; `c-nonsymbol-token-regexp' should always match
2490 ;; since we've skipped backward over punctuator
2491 ;; or paren syntax, but consume one char in case
2492 ;; it doesn't so that we don't leave point before
2493 ;; some earlier incorrect token.
2494 (1+ (point)))))
2495 (if (<= pos start)
2496 (goto-char pos))
2497 (< pos start)))))))
2499 (defun c-end-of-current-token (&optional back-limit)
2500 ;; Move to the end of the current token. Do not move if not in the
2501 ;; middle of one. BACK-LIMIT may be used to bound the backward
2502 ;; search; if given it's assumed to be at the boundary between two
2503 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2505 ;; This function might do hidden buffer changes.
2506 (let ((start (point)))
2507 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2508 (skip-syntax-forward "w_"))
2509 ((< (skip-syntax-backward ".()" back-limit) 0)
2510 (while (progn
2511 (if (looking-at c-nonsymbol-token-regexp)
2512 (goto-char (match-end 0))
2513 ;; `c-nonsymbol-token-regexp' should always match since
2514 ;; we've skipped backward over punctuator or paren
2515 ;; syntax, but move forward in case it doesn't so that
2516 ;; we don't leave point earlier than we started with.
2517 (forward-char))
2518 (< (point) start)))))
2519 (> (point) start)))
2521 (defconst c-jump-syntax-balanced
2522 (if (memq 'gen-string-delim c-emacs-features)
2523 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2524 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2526 (defconst c-jump-syntax-unbalanced
2527 (if (memq 'gen-string-delim c-emacs-features)
2528 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2529 "\\w\\|\\s_\\|\\s\""))
2531 (defun c-forward-token-2 (&optional count balanced limit)
2532 "Move forward by tokens.
2533 A token is defined as all symbols and identifiers which aren't
2534 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2535 treated properly). Point is always either left at the beginning of a
2536 token or not moved at all. COUNT specifies the number of tokens to
2537 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2538 moves to the next token beginning only if not already at one. If
2539 BALANCED is true, move over balanced parens, otherwise move into them.
2540 Also, if BALANCED is true, never move out of an enclosing paren.
2542 LIMIT sets the limit for the movement and defaults to the point limit.
2543 The case when LIMIT is set in the middle of a token, comment or macro
2544 is handled correctly, i.e. the point won't be left there.
2546 Return the number of tokens left to move \(positive or negative). If
2547 BALANCED is true, a move over a balanced paren counts as one. Note
2548 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2549 be returned. Thus, a return value of 0 guarantees that point is at
2550 the requested position and a return value less \(without signs) than
2551 COUNT guarantees that point is at the beginning of some token.
2553 Note that this function might do hidden buffer changes. See the
2554 comment at the start of cc-engine.el for more info."
2556 (or count (setq count 1))
2557 (if (< count 0)
2558 (- (c-backward-token-2 (- count) balanced limit))
2560 (let ((jump-syntax (if balanced
2561 c-jump-syntax-balanced
2562 c-jump-syntax-unbalanced))
2563 (last (point))
2564 (prev (point)))
2566 (if (zerop count)
2567 ;; If count is zero we should jump if in the middle of a token.
2568 (c-end-of-current-token))
2570 (save-restriction
2571 (if limit (narrow-to-region (point-min) limit))
2572 (if (/= (point)
2573 (progn (c-forward-syntactic-ws) (point)))
2574 ;; Skip whitespace. Count this as a move if we did in
2575 ;; fact move.
2576 (setq count (max (1- count) 0)))
2578 (if (eobp)
2579 ;; Moved out of bounds. Make sure the returned count isn't zero.
2580 (progn
2581 (if (zerop count) (setq count 1))
2582 (goto-char last))
2584 ;; Use `condition-case' to avoid having the limit tests
2585 ;; inside the loop.
2586 (condition-case nil
2587 (while (and
2588 (> count 0)
2589 (progn
2590 (setq last (point))
2591 (cond ((looking-at jump-syntax)
2592 (goto-char (scan-sexps (point) 1))
2594 ((looking-at c-nonsymbol-token-regexp)
2595 (goto-char (match-end 0))
2597 ;; `c-nonsymbol-token-regexp' above should always
2598 ;; match if there are correct tokens. Try to
2599 ;; widen to see if the limit was set in the
2600 ;; middle of one, else fall back to treating
2601 ;; the offending thing as a one character token.
2602 ((and limit
2603 (save-restriction
2604 (widen)
2605 (looking-at c-nonsymbol-token-regexp)))
2606 nil)
2608 (forward-char)
2609 t))))
2610 (c-forward-syntactic-ws)
2611 (setq prev last
2612 count (1- count)))
2613 (error (goto-char last)))
2615 (when (eobp)
2616 (goto-char prev)
2617 (setq count (1+ count)))))
2619 count)))
2621 (defun c-backward-token-2 (&optional count balanced limit)
2622 "Move backward by tokens.
2623 See `c-forward-token-2' for details."
2625 (or count (setq count 1))
2626 (if (< count 0)
2627 (- (c-forward-token-2 (- count) balanced limit))
2629 (or limit (setq limit (point-min)))
2630 (let ((jump-syntax (if balanced
2631 c-jump-syntax-balanced
2632 c-jump-syntax-unbalanced))
2633 (last (point)))
2635 (if (zerop count)
2636 ;; The count is zero so try to skip to the beginning of the
2637 ;; current token.
2638 (if (> (point)
2639 (progn (c-beginning-of-current-token) (point)))
2640 (if (< (point) limit)
2641 ;; The limit is inside the same token, so return 1.
2642 (setq count 1))
2644 ;; We're not in the middle of a token. If there's
2645 ;; whitespace after the point then we must move backward,
2646 ;; so set count to 1 in that case.
2647 (and (looking-at c-syntactic-ws-start)
2648 ;; If we're looking at a '#' that might start a cpp
2649 ;; directive then we have to do a more elaborate check.
2650 (or (/= (char-after) ?#)
2651 (not c-opt-cpp-prefix)
2652 (save-excursion
2653 (and (= (point)
2654 (progn (beginning-of-line)
2655 (looking-at "[ \t]*")
2656 (match-end 0)))
2657 (or (bobp)
2658 (progn (backward-char)
2659 (not (eq (char-before) ?\\)))))))
2660 (setq count 1))))
2662 ;; Use `condition-case' to avoid having to check for buffer
2663 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2664 (condition-case nil
2665 (while (and
2666 (> count 0)
2667 (progn
2668 (c-backward-syntactic-ws)
2669 (backward-char)
2670 (if (looking-at jump-syntax)
2671 (goto-char (scan-sexps (1+ (point)) -1))
2672 ;; This can be very inefficient if there's a long
2673 ;; sequence of operator tokens without any separation.
2674 ;; That doesn't happen in practice, anyway.
2675 (c-beginning-of-current-token))
2676 (>= (point) limit)))
2677 (setq last (point)
2678 count (1- count)))
2679 (error (goto-char last)))
2681 (if (< (point) limit)
2682 (goto-char last))
2684 count)))
2686 (defun c-forward-token-1 (&optional count balanced limit)
2687 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2688 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2689 characters are jumped over character by character. This function is
2690 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2691 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2692 (c-forward-token-2 count balanced limit)))
2694 (defun c-backward-token-1 (&optional count balanced limit)
2695 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2696 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2697 characters are jumped over character by character. This function is
2698 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2699 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2700 (c-backward-token-2 count balanced limit)))
2703 ;; Tools for doing searches restricted to syntactically relevant text.
2705 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2706 paren-level not-inside-token
2707 lookbehind-submatch)
2708 "Like `re-search-forward', but only report matches that are found
2709 in syntactically significant text. I.e. matches in comments, macros
2710 or string literals are ignored. The start point is assumed to be
2711 outside any comment, macro or string literal, or else the content of
2712 that region is taken as syntactically significant text.
2714 If PAREN-LEVEL is non-nil, an additional restriction is added to
2715 ignore matches in nested paren sexps. The search will also not go
2716 outside the current list sexp, which has the effect that if the point
2717 should be moved to BOUND when no match is found \(i.e. NOERROR is
2718 neither nil nor t), then it will be at the closing paren if the end of
2719 the current list sexp is encountered first.
2721 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2722 ignored. Things like multicharacter operators and special symbols
2723 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2724 constants.
2726 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2727 subexpression in REGEXP. The end of that submatch is used as the
2728 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2729 isn't used or if that subexpression didn't match then the start
2730 position of the whole match is used instead. The \"look behind\"
2731 subexpression is never tested before the starting position, so it
2732 might be a good idea to include \\=\\= as a match alternative in it.
2734 Optimization note: Matches might be missed if the \"look behind\"
2735 subexpression can match the end of nonwhite syntactic whitespace,
2736 i.e. the end of comments or cpp directives. This since the function
2737 skips over such things before resuming the search. It's on the other
2738 hand not safe to assume that the \"look behind\" subexpression never
2739 matches syntactic whitespace.
2741 Bug: Unbalanced parens inside cpp directives are currently not handled
2742 correctly \(i.e. they don't get ignored as they should) when
2743 PAREN-LEVEL is set.
2745 Note that this function might do hidden buffer changes. See the
2746 comment at the start of cc-engine.el for more info."
2748 (or bound (setq bound (point-max)))
2749 (if paren-level (setq paren-level -1))
2751 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2753 (let ((start (point))
2755 ;; Start position for the last search.
2756 search-pos
2757 ;; The `parse-partial-sexp' state between the start position
2758 ;; and the point.
2759 state
2760 ;; The current position after the last state update. The next
2761 ;; `parse-partial-sexp' continues from here.
2762 (state-pos (point))
2763 ;; The position at which to check the state and the state
2764 ;; there. This is separate from `state-pos' since we might
2765 ;; need to back up before doing the next search round.
2766 check-pos check-state
2767 ;; Last position known to end a token.
2768 (last-token-end-pos (point-min))
2769 ;; Set when a valid match is found.
2770 found)
2772 (condition-case err
2773 (while
2774 (and
2775 (progn
2776 (setq search-pos (point))
2777 (re-search-forward regexp bound noerror))
2779 (progn
2780 (setq state (parse-partial-sexp
2781 state-pos (match-beginning 0) paren-level nil state)
2782 state-pos (point))
2783 (if (setq check-pos (and lookbehind-submatch
2784 (or (not paren-level)
2785 (>= (car state) 0))
2786 (match-end lookbehind-submatch)))
2787 (setq check-state (parse-partial-sexp
2788 state-pos check-pos paren-level nil state))
2789 (setq check-pos state-pos
2790 check-state state))
2792 ;; NOTE: If we got a look behind subexpression and get
2793 ;; an insignificant match in something that isn't
2794 ;; syntactic whitespace (i.e. strings or in nested
2795 ;; parentheses), then we can never skip more than a
2796 ;; single character from the match start position
2797 ;; (i.e. `state-pos' here) before continuing the
2798 ;; search. That since the look behind subexpression
2799 ;; might match the end of the insignificant region in
2800 ;; the next search.
2802 (cond
2803 ((elt check-state 7)
2804 ;; Match inside a line comment. Skip to eol. Use
2805 ;; `re-search-forward' instead of `skip-chars-forward' to get
2806 ;; the right bound behavior.
2807 (re-search-forward "[\n\r]" bound noerror))
2809 ((elt check-state 4)
2810 ;; Match inside a block comment. Skip to the '*/'.
2811 (search-forward "*/" bound noerror))
2813 ((and (not (elt check-state 5))
2814 (eq (char-before check-pos) ?/)
2815 (not (c-get-char-property (1- check-pos) 'syntax-table))
2816 (memq (char-after check-pos) '(?/ ?*)))
2817 ;; Match in the middle of the opener of a block or line
2818 ;; comment.
2819 (if (= (char-after check-pos) ?/)
2820 (re-search-forward "[\n\r]" bound noerror)
2821 (search-forward "*/" bound noerror)))
2823 ;; The last `parse-partial-sexp' above might have
2824 ;; stopped short of the real check position if the end
2825 ;; of the current sexp was encountered in paren-level
2826 ;; mode. The checks above are always false in that
2827 ;; case, and since they can do better skipping in
2828 ;; lookbehind-submatch mode, we do them before
2829 ;; checking the paren level.
2831 ((and paren-level
2832 (/= (setq tmp (car check-state)) 0))
2833 ;; Check the paren level first since we're short of the
2834 ;; syntactic checking position if the end of the
2835 ;; current sexp was encountered by `parse-partial-sexp'.
2836 (if (> tmp 0)
2838 ;; Inside a nested paren sexp.
2839 (if lookbehind-submatch
2840 ;; See the NOTE above.
2841 (progn (goto-char state-pos) t)
2842 ;; Skip out of the paren quickly.
2843 (setq state (parse-partial-sexp state-pos bound 0 nil state)
2844 state-pos (point)))
2846 ;; Have exited the current paren sexp.
2847 (if noerror
2848 (progn
2849 ;; The last `parse-partial-sexp' call above
2850 ;; has left us just after the closing paren
2851 ;; in this case, so we can modify the bound
2852 ;; to leave the point at the right position
2853 ;; upon return.
2854 (setq bound (1- (point)))
2855 nil)
2856 (signal 'search-failed (list regexp)))))
2858 ((setq tmp (elt check-state 3))
2859 ;; Match inside a string.
2860 (if (or lookbehind-submatch
2861 (not (integerp tmp)))
2862 ;; See the NOTE above.
2863 (progn (goto-char state-pos) t)
2864 ;; Skip to the end of the string before continuing.
2865 (let ((ender (make-string 1 tmp)) (continue t))
2866 (while (if (search-forward ender bound noerror)
2867 (progn
2868 (setq state (parse-partial-sexp
2869 state-pos (point) nil nil state)
2870 state-pos (point))
2871 (elt state 3))
2872 (setq continue nil)))
2873 continue)))
2875 ((save-excursion
2876 (save-match-data
2877 (c-beginning-of-macro start)))
2878 ;; Match inside a macro. Skip to the end of it.
2879 (c-end-of-macro)
2880 (cond ((<= (point) bound) t)
2881 (noerror nil)
2882 (t (signal 'search-failed (list regexp)))))
2884 ((and not-inside-token
2885 (or (< check-pos last-token-end-pos)
2886 (< check-pos
2887 (save-excursion
2888 (goto-char check-pos)
2889 (save-match-data
2890 (c-end-of-current-token last-token-end-pos))
2891 (setq last-token-end-pos (point))))))
2892 ;; Inside a token.
2893 (if lookbehind-submatch
2894 ;; See the NOTE above.
2895 (goto-char state-pos)
2896 (goto-char (min last-token-end-pos bound))))
2899 ;; A real match.
2900 (setq found t)
2901 nil)))
2903 ;; Should loop to search again, but take care to avoid
2904 ;; looping on the same spot.
2905 (or (/= search-pos (point))
2906 (if (= (point) bound)
2907 (if noerror
2909 (signal 'search-failed (list regexp)))
2910 (forward-char)
2911 t))))
2913 (error
2914 (goto-char start)
2915 (signal (car err) (cdr err))))
2917 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2919 (if found
2920 (progn
2921 (goto-char (match-end 0))
2922 (match-end 0))
2924 ;; Search failed. Set point as appropriate.
2925 (if (eq noerror t)
2926 (goto-char start)
2927 (goto-char bound))
2928 nil)))
2930 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
2931 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2932 i.e. don't stop at positions inside syntactic whitespace or string
2933 literals. Preprocessor directives are also ignored, with the exception
2934 of the one that the point starts within, if any. If LIMIT is given,
2935 it's assumed to be at a syntactically relevant position.
2937 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
2938 sexps, and the search will also not go outside the current paren sexp.
2939 However, if LIMIT or the buffer limit is reached inside a nested paren
2940 then the point will be left at the limit.
2942 Non-nil is returned if the point moved, nil otherwise.
2944 Note that this function might do hidden buffer changes. See the
2945 comment at the start of cc-engine.el for more info."
2947 (let ((start (point))
2948 state
2949 ;; A list of syntactically relevant positions in descending
2950 ;; order. It's used to avoid scanning repeatedly over
2951 ;; potentially large regions with `parse-partial-sexp' to verify
2952 ;; each position.
2953 safe-pos-list
2954 ;; The position at the beginning of `safe-pos-list'.
2955 safe-pos
2956 ;; The result from `c-beginning-of-macro' at the start position or the
2957 ;; start position itself if it isn't within a macro. Evaluated on
2958 ;; demand.
2959 start-macro-beg
2960 ;; The earliest position after the current one with the same paren
2961 ;; level. Used only when `paren-level' is set.
2962 (paren-level-pos (point)))
2964 (while (progn
2965 (while (and
2966 (< (skip-chars-backward skip-chars limit) 0)
2968 ;; Use `parse-partial-sexp' from a safe position down to
2969 ;; the point to check if it's outside comments and
2970 ;; strings.
2971 (let ((pos (point)) state-2 pps-end-pos)
2972 ;; Pick a safe position as close to the point as
2973 ;; possible.
2975 ;; FIXME: Consult `syntax-ppss' here if our
2976 ;; cache doesn't give a good position.
2977 (while (and safe-pos-list
2978 (> (car safe-pos-list) (point)))
2979 (setq safe-pos-list (cdr safe-pos-list)))
2980 (unless (setq safe-pos (car-safe safe-pos-list))
2981 (setq safe-pos (max (or (c-safe-position
2982 (point) (or c-state-cache
2983 (c-parse-state)))
2985 (point-min))
2986 safe-pos-list (list safe-pos)))
2988 ;; Cache positions along the way to use if we have to
2989 ;; back up more. We cache every closing paren on the
2990 ;; same level. If the paren cache is relevant in this
2991 ;; region then we're typically already on the same
2992 ;; level as the target position. Note that we might
2993 ;; cache positions after opening parens in case
2994 ;; safe-pos is in a nested list. That's both uncommon
2995 ;; and harmless.
2996 (while (progn
2997 (setq state (parse-partial-sexp
2998 safe-pos pos 0))
2999 (< (point) pos))
3000 (setq safe-pos (point)
3001 safe-pos-list (cons safe-pos safe-pos-list)))
3003 (cond
3004 ((or (elt state 3) (elt state 4))
3005 ;; Inside string or comment. Continue search at the
3006 ;; beginning of it.
3007 (goto-char (elt state 8))
3010 ((and paren-level
3011 (save-excursion
3012 (setq state-2 (parse-partial-sexp
3013 pos paren-level-pos -1)
3014 pps-end-pos (point))
3015 (/= (car state-2) 0)))
3016 ;; Not at the right level.
3018 (if (and (< (car state-2) 0)
3019 ;; We stop above if we go out of a paren.
3020 ;; Now check whether it precedes or is
3021 ;; nested in the starting sexp.
3022 (save-excursion
3023 (setq state-2
3024 (parse-partial-sexp
3025 pps-end-pos paren-level-pos
3026 nil nil state-2))
3027 (< (car state-2) 0)))
3029 ;; We've stopped short of the starting position
3030 ;; so the hit was inside a nested list. Go up
3031 ;; until we are at the right level.
3032 (condition-case nil
3033 (progn
3034 (goto-char (scan-lists pos -1
3035 (- (car state-2))))
3036 (setq paren-level-pos (point))
3037 (if (and limit (>= limit paren-level-pos))
3038 (progn
3039 (goto-char limit)
3040 nil)
3042 (error
3043 (goto-char (or limit (point-min)))
3044 nil))
3046 ;; The hit was outside the list at the start
3047 ;; position. Go to the start of the list and exit.
3048 (goto-char (1+ (elt state-2 1)))
3049 nil))
3051 ((c-beginning-of-macro limit)
3052 ;; Inside a macro.
3053 (if (< (point)
3054 (or start-macro-beg
3055 (setq start-macro-beg
3056 (save-excursion
3057 (goto-char start)
3058 (c-beginning-of-macro limit)
3059 (point)))))
3062 ;; It's inside the same macro we started in so it's
3063 ;; a relevant match.
3064 (goto-char pos)
3065 nil)))))
3067 ;; If the state contains the start of the containing sexp we
3068 ;; cache that position too, so that parse-partial-sexp in the
3069 ;; next run has a bigger chance of starting at the same level
3070 ;; as the target position and thus will get more good safe
3071 ;; positions into the list.
3072 (if (elt state 1)
3073 (setq safe-pos (1+ (elt state 1))
3074 safe-pos-list (cons safe-pos safe-pos-list))))
3076 (> (point)
3077 (progn
3078 ;; Skip syntactic ws afterwards so that we don't stop at the
3079 ;; end of a comment if `skip-chars' is something like "^/".
3080 (c-backward-syntactic-ws)
3081 (point)))))
3083 ;; We might want to extend this with more useful return values in
3084 ;; the future.
3085 (/= (point) start)))
3087 ;; The following is an alternative implementation of
3088 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3089 ;; track of the syntactic context. It turned out to be generally
3090 ;; slower than the one above which uses forward checks from earlier
3091 ;; safe positions.
3093 ;;(defconst c-ssb-stop-re
3094 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3095 ;; ;; stop at to avoid going into comments and literals.
3096 ;; (concat
3097 ;; ;; Match comment end syntax and string literal syntax. Also match
3098 ;; ;; '/' for block comment endings (not covered by comment end
3099 ;; ;; syntax).
3100 ;; "\\s>\\|/\\|\\s\""
3101 ;; (if (memq 'gen-string-delim c-emacs-features)
3102 ;; "\\|\\s|"
3103 ;; "")
3104 ;; (if (memq 'gen-comment-delim c-emacs-features)
3105 ;; "\\|\\s!"
3106 ;; "")))
3108 ;;(defconst c-ssb-stop-paren-re
3109 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3110 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3112 ;;(defconst c-ssb-sexp-end-re
3113 ;; ;; Regexp matching the ending syntax of a complex sexp.
3114 ;; (concat c-string-limit-regexp "\\|\\s)"))
3116 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3117 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3118 ;;i.e. don't stop at positions inside syntactic whitespace or string
3119 ;;literals. Preprocessor directives are also ignored. However, if the
3120 ;;point is within a comment, string literal or preprocessor directory to
3121 ;;begin with, its contents is treated as syntactically relevant chars.
3122 ;;If LIMIT is given, it limits the backward search and the point will be
3123 ;;left there if no earlier position is found.
3125 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3126 ;;sexps, and the search will also not go outside the current paren sexp.
3127 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3128 ;;then the point will be left at the limit.
3130 ;;Non-nil is returned if the point moved, nil otherwise.
3132 ;;Note that this function might do hidden buffer changes. See the
3133 ;;comment at the start of cc-engine.el for more info."
3135 ;; (save-restriction
3136 ;; (when limit
3137 ;; (narrow-to-region limit (point-max)))
3139 ;; (let ((start (point)))
3140 ;; (catch 'done
3141 ;; (while (let ((last-pos (point))
3142 ;; (stop-pos (progn
3143 ;; (skip-chars-backward skip-chars)
3144 ;; (point))))
3146 ;; ;; Skip back over the same region as
3147 ;; ;; `skip-chars-backward' above, but keep to
3148 ;; ;; syntactically relevant positions.
3149 ;; (goto-char last-pos)
3150 ;; (while (and
3151 ;; ;; `re-search-backward' with a single char regexp
3152 ;; ;; should be fast.
3153 ;; (re-search-backward
3154 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3155 ;; stop-pos 'move)
3157 ;; (progn
3158 ;; (cond
3159 ;; ((looking-at "\\s(")
3160 ;; ;; `paren-level' is set and we've found the
3161 ;; ;; start of the containing paren.
3162 ;; (forward-char)
3163 ;; (throw 'done t))
3165 ;; ((looking-at c-ssb-sexp-end-re)
3166 ;; ;; We're at the end of a string literal or paren
3167 ;; ;; sexp (if `paren-level' is set).
3168 ;; (forward-char)
3169 ;; (condition-case nil
3170 ;; (c-backward-sexp)
3171 ;; (error
3172 ;; (goto-char limit)
3173 ;; (throw 'done t))))
3175 ;; (t
3176 ;; (forward-char)
3177 ;; ;; At the end of some syntactic ws or possibly
3178 ;; ;; after a plain '/' operator.
3179 ;; (let ((pos (point)))
3180 ;; (c-backward-syntactic-ws)
3181 ;; (if (= pos (point))
3182 ;; ;; Was a plain '/' operator. Go past it.
3183 ;; (backward-char)))))
3185 ;; (> (point) stop-pos))))
3187 ;; ;; Now the point is either at `stop-pos' or at some
3188 ;; ;; position further back if `stop-pos' was at a
3189 ;; ;; syntactically irrelevant place.
3191 ;; ;; Skip additional syntactic ws so that we don't stop
3192 ;; ;; at the end of a comment if `skip-chars' is
3193 ;; ;; something like "^/".
3194 ;; (c-backward-syntactic-ws)
3196 ;; (< (point) stop-pos))))
3198 ;; ;; We might want to extend this with more useful return values
3199 ;; ;; in the future.
3200 ;; (/= (point) start))))
3203 ;; Tools for handling comments and string literals.
3205 (defun c-slow-in-literal (&optional lim detect-cpp)
3206 "Return the type of literal point is in, if any.
3207 The return value is `c' if in a C-style comment, `c++' if in a C++
3208 style comment, `string' if in a string literal, `pound' if DETECT-CPP
3209 is non-nil and in a preprocessor line, or nil if somewhere else.
3210 Optional LIM is used as the backward limit of the search. If omitted,
3211 or nil, `c-beginning-of-defun' is used.
3213 The last point calculated is cached if the cache is enabled, i.e. if
3214 `c-in-literal-cache' is bound to a two element vector.
3216 Note that this function might do hidden buffer changes. See the
3217 comment at the start of cc-engine.el for more info."
3219 (if (and (vectorp c-in-literal-cache)
3220 (= (point) (aref c-in-literal-cache 0)))
3221 (aref c-in-literal-cache 1)
3222 (let ((rtn (save-excursion
3223 (let* ((pos (point))
3224 (lim (or lim (progn
3225 (c-beginning-of-syntax)
3226 (point))))
3227 (state (parse-partial-sexp lim pos)))
3228 (cond
3229 ((elt state 3) 'string)
3230 ((elt state 4) (if (elt state 7) 'c++ 'c))
3231 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
3232 (t nil))))))
3233 ;; cache this result if the cache is enabled
3234 (if (not c-in-literal-cache)
3235 (setq c-in-literal-cache (vector (point) rtn)))
3236 rtn)))
3238 ;; XEmacs has a built-in function that should make this much quicker.
3239 ;; I don't think we even need the cache, which makes our lives more
3240 ;; complicated anyway. In this case, lim is only used to detect
3241 ;; cpp directives.
3243 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
3244 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
3245 ;; Xemacs 21.4.4. It manifested itself thus:
3247 ;; Starting with an empty AWK Mode buffer, type
3248 ;; /regexp/ {<C-j>
3249 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
3251 ;; AWK Mode is designed such that when the first / is typed, it gets the
3252 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
3253 ;; are given the s-t property "string". However, buffer-syntactic-context
3254 ;; fails to take account of the change of the s-t property on the opening / to
3255 ;; "string", and reports that the { is within a string started by the second /.
3257 ;; The workaround for this is for the AWK Mode initialisation to switch the
3258 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
3259 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
3261 ;; (Alan Mackenzie, 2003/4/30).
3263 (defun c-fast-in-literal (&optional lim detect-cpp)
3264 ;; This function might do hidden buffer changes.
3265 (let ((context (buffer-syntactic-context)))
3266 (cond
3267 ((eq context 'string) 'string)
3268 ((eq context 'comment) 'c++)
3269 ((eq context 'block-comment) 'c)
3270 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
3272 (defalias 'c-in-literal
3273 (if (fboundp 'buffer-syntactic-context)
3274 'c-fast-in-literal ; XEmacs
3275 'c-slow-in-literal)) ; GNU Emacs
3277 ;; The defalias above isn't enough to shut up the byte compiler.
3278 (cc-bytecomp-defun c-in-literal)
3280 (defun c-literal-limits (&optional lim near not-in-delimiter)
3281 "Return a cons of the beginning and end positions of the comment or
3282 string surrounding point (including both delimiters), or nil if point
3283 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
3284 to start parsing from. If NEAR is non-nil, then the limits of any
3285 literal next to point is returned. \"Next to\" means there's only
3286 spaces and tabs between point and the literal. The search for such a
3287 literal is done first in forward direction. If NOT-IN-DELIMITER is
3288 non-nil, the case when point is inside a starting delimiter won't be
3289 recognized. This only has effect for comments, which have starting
3290 delimiters with more than one character.
3292 Note that this function might do hidden buffer changes. See the
3293 comment at the start of cc-engine.el for more info."
3295 (save-excursion
3296 (let* ((pos (point))
3297 (lim (or lim (progn
3298 (c-beginning-of-syntax)
3299 (point))))
3300 (state (parse-partial-sexp lim pos)))
3302 (cond ((elt state 3) ; String.
3303 (goto-char (elt state 8))
3304 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3305 (point-max))))
3307 ((elt state 4) ; Comment.
3308 (goto-char (elt state 8))
3309 (cons (point) (progn (c-forward-single-comment) (point))))
3311 ((and (not not-in-delimiter)
3312 (not (elt state 5))
3313 (eq (char-before) ?/)
3314 (looking-at "[/*]"))
3315 ;; We're standing in a comment starter.
3316 (backward-char 1)
3317 (cons (point) (progn (c-forward-single-comment) (point))))
3319 (near
3320 (goto-char pos)
3322 ;; Search forward for a literal.
3323 (skip-chars-forward " \t")
3325 (cond
3326 ((looking-at c-string-limit-regexp) ; String.
3327 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3328 (point-max))))
3330 ((looking-at c-comment-start-regexp) ; Line or block comment.
3331 (cons (point) (progn (c-forward-single-comment) (point))))
3334 ;; Search backward.
3335 (skip-chars-backward " \t")
3337 (let ((end (point)) beg)
3338 (cond
3339 ((save-excursion
3340 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
3341 (setq beg (c-safe (c-backward-sexp 1) (point))))
3343 ((and (c-safe (forward-char -2) t)
3344 (looking-at "*/"))
3345 ;; Block comment. Due to the nature of line
3346 ;; comments, they will always be covered by the
3347 ;; normal case above.
3348 (goto-char end)
3349 (c-backward-single-comment)
3350 ;; If LIM is bogus, beg will be bogus.
3351 (setq beg (point))))
3353 (if beg (cons beg end))))))
3354 ))))
3356 ;; In case external callers use this; it did have a docstring.
3357 (defalias 'c-literal-limits-fast 'c-literal-limits)
3359 (defun c-collect-line-comments (range)
3360 "If the argument is a cons of two buffer positions (such as returned by
3361 `c-literal-limits'), and that range contains a C++ style line comment,
3362 then an extended range is returned that contains all adjacent line
3363 comments (i.e. all comments that starts in the same column with no
3364 empty lines or non-whitespace characters between them). Otherwise the
3365 argument is returned.
3367 Note that this function might do hidden buffer changes. See the
3368 comment at the start of cc-engine.el for more info."
3370 (save-excursion
3371 (condition-case nil
3372 (if (and (consp range) (progn
3373 (goto-char (car range))
3374 (looking-at c-line-comment-starter)))
3375 (let ((col (current-column))
3376 (beg (point))
3377 (bopl (c-point 'bopl))
3378 (end (cdr range)))
3379 ;; Got to take care in the backward direction to handle
3380 ;; comments which are preceded by code.
3381 (while (and (c-backward-single-comment)
3382 (>= (point) bopl)
3383 (looking-at c-line-comment-starter)
3384 (= col (current-column)))
3385 (setq beg (point)
3386 bopl (c-point 'bopl)))
3387 (goto-char end)
3388 (while (and (progn (skip-chars-forward " \t")
3389 (looking-at c-line-comment-starter))
3390 (= col (current-column))
3391 (prog1 (zerop (forward-line 1))
3392 (setq end (point)))))
3393 (cons beg end))
3394 range)
3395 (error range))))
3397 (defun c-literal-type (range)
3398 "Convenience function that given the result of `c-literal-limits',
3399 returns nil or the type of literal that the range surrounds. It's
3400 much faster than using `c-in-literal' and is intended to be used when
3401 you need both the type of a literal and its limits.
3403 Note that this function might do hidden buffer changes. See the
3404 comment at the start of cc-engine.el for more info."
3406 (if (consp range)
3407 (save-excursion
3408 (goto-char (car range))
3409 (cond ((looking-at c-string-limit-regexp) 'string)
3410 ((or (looking-at "//") ; c++ line comment
3411 (and (looking-at "\\s<") ; comment starter
3412 (looking-at "#"))) ; awk comment.
3413 'c++)
3414 (t 'c))) ; Assuming the range is valid.
3415 range))
3418 ;; `c-find-decl-spots' and accompanying stuff.
3420 ;; Variables used in `c-find-decl-spots' to cache the search done for
3421 ;; the first declaration in the last call. When that function starts,
3422 ;; it needs to back up over syntactic whitespace to look at the last
3423 ;; token before the region being searched. That can sometimes cause
3424 ;; moves back and forth over a quite large region of comments and
3425 ;; macros, which would be repeated for each changed character when
3426 ;; we're called during fontification, since font-lock refontifies the
3427 ;; current line for each change. Thus it's worthwhile to cache the
3428 ;; first match.
3430 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3431 ;; the syntactic whitespace less or equal to some start position.
3432 ;; There's no cached value if it's nil.
3434 ;; `c-find-decl-match-pos' is the match position if
3435 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3436 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3437 (defvar c-find-decl-syntactic-pos nil)
3438 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
3439 (defvar c-find-decl-match-pos nil)
3440 (make-variable-buffer-local 'c-find-decl-match-pos)
3442 (defsubst c-invalidate-find-decl-cache (change-min-pos)
3443 (and c-find-decl-syntactic-pos
3444 (< change-min-pos c-find-decl-syntactic-pos)
3445 (setq c-find-decl-syntactic-pos nil)))
3447 ; (defface c-debug-decl-spot-face
3448 ; '((t (:background "Turquoise")))
3449 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3450 ; (defface c-debug-decl-sws-face
3451 ; '((t (:background "Khaki")))
3452 ; "Debug face to mark the syntactic whitespace between the declaration
3453 ; spots and the preceding token end.")
3455 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
3456 (when (facep 'c-debug-decl-spot-face)
3457 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
3458 (c-debug-add-face (max match-pos (point-min)) decl-pos
3459 'c-debug-decl-sws-face)
3460 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
3461 'c-debug-decl-spot-face))))
3462 (defmacro c-debug-remove-decl-spot-faces (beg end)
3463 (when (facep 'c-debug-decl-spot-face)
3464 `(c-save-buffer-state ()
3465 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
3466 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
3468 (defmacro c-find-decl-prefix-search ()
3469 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3470 ;; but it contains lots of free variables that refer to things
3471 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3472 ;; if there is a match, otherwise at `cfd-limit'.
3474 ;; This macro might do hidden buffer changes.
3476 '(progn
3477 ;; Find the next property match position if we haven't got one already.
3478 (unless cfd-prop-match
3479 (save-excursion
3480 (while (progn
3481 (goto-char (next-single-property-change
3482 (point) 'c-type nil cfd-limit))
3483 (and (< (point) cfd-limit)
3484 (not (eq (c-get-char-property (1- (point)) 'c-type)
3485 'c-decl-end)))))
3486 (setq cfd-prop-match (point))))
3488 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
3489 ;; got one already.
3490 (unless cfd-re-match
3492 (if (> cfd-re-match-end (point))
3493 (goto-char cfd-re-match-end))
3495 (while (if (setq cfd-re-match-end
3496 (re-search-forward c-decl-prefix-or-start-re
3497 cfd-limit 'move))
3499 ;; Match. Check if it's inside a comment or string literal.
3500 (c-got-face-at
3501 (if (setq cfd-re-match (match-end 1))
3502 ;; Matched the end of a token preceding a decl spot.
3503 (progn
3504 (goto-char cfd-re-match)
3505 (1- cfd-re-match))
3506 ;; Matched a token that start a decl spot.
3507 (goto-char (match-beginning 0))
3508 (point))
3509 c-literal-faces)
3511 ;; No match. Finish up and exit the loop.
3512 (setq cfd-re-match cfd-limit)
3513 nil)
3515 ;; Skip out of comments and string literals.
3516 (while (progn
3517 (goto-char (next-single-property-change
3518 (point) 'face nil cfd-limit))
3519 (and (< (point) cfd-limit)
3520 (c-got-face-at (point) c-literal-faces)))))
3522 ;; If we matched at the decl start, we have to back up over the
3523 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
3524 ;; any decl spots in the syntactic ws.
3525 (unless cfd-re-match
3526 (c-backward-syntactic-ws)
3527 (setq cfd-re-match (point))))
3529 ;; Choose whichever match is closer to the start.
3530 (if (< cfd-re-match cfd-prop-match)
3531 (setq cfd-match-pos cfd-re-match
3532 cfd-re-match nil)
3533 (setq cfd-match-pos cfd-prop-match
3534 cfd-prop-match nil))
3536 (goto-char cfd-match-pos)
3538 (when (< cfd-match-pos cfd-limit)
3539 ;; Skip forward past comments only so we don't skip macros.
3540 (c-forward-comments)
3541 ;; Set the position to continue at. We can avoid going over
3542 ;; the comments skipped above a second time, but it's possible
3543 ;; that the comment skipping has taken us past `cfd-prop-match'
3544 ;; since the property might be used inside comments.
3545 (setq cfd-continue-pos (if cfd-prop-match
3546 (min cfd-prop-match (point))
3547 (point))))))
3549 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3550 ;; Call CFD-FUN for each possible spot for a declaration, cast or
3551 ;; label from the point to CFD-LIMIT. Such a spot is:
3553 ;; o The first token after bob.
3554 ;; o The first token after the end of submatch 1 in
3555 ;; `c-decl-prefix-or-start-re' when that submatch matches.
3556 ;; o The start of each `c-decl-prefix-or-start-re' match when
3557 ;; submatch 1 doesn't match.
3558 ;; o The first token after the end of each occurence of the
3559 ;; `c-type' text property with the value `c-decl-end', provided
3560 ;; `c-type-decl-end-used' is set.
3562 ;; Only a spot that match CFD-DECL-RE and whose face is in the
3563 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
3564 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
3566 ;; If the match is inside a macro then the buffer is narrowed to the
3567 ;; end of it, so that CFD-FUN can investigate the following tokens
3568 ;; without matching something that begins inside a macro and ends
3569 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3570 ;; CFD-FACE-CHECKLIST checks exist.
3572 ;; CFD-FUN is called with point at the start of the spot. It's
3573 ;; passed two arguments: The first is the end position of the token
3574 ;; preceding the spot, or 0 for the implicit match at bob. The
3575 ;; second is a flag that is t when the match is inside a macro. If
3576 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
3577 ;; spot, it should return non-nil to ensure that the next search
3578 ;; will find them.
3580 ;; The spots are visited approximately in order from top to bottom.
3581 ;; It's however the positions where `c-decl-prefix-or-start-re'
3582 ;; matches and where `c-decl-end' properties are found that are in
3583 ;; order. Since the spots often are at the following token, they
3584 ;; might be visited out of order insofar as more spots are reported
3585 ;; later on within the syntactic whitespace between the match
3586 ;; positions and their spots.
3588 ;; It's assumed that comments and strings are fontified in the
3589 ;; searched range.
3591 ;; This is mainly used in fontification, and so has an elaborate
3592 ;; cache to handle repeated calls from the same start position; see
3593 ;; the variables above.
3595 ;; All variables in this function begin with `cfd-' to avoid name
3596 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3598 ;; This function might do hidden buffer changes.
3600 (let ((cfd-start-pos (point))
3601 (cfd-buffer-end (point-max))
3602 ;; The end of the token preceding the decl spot last found
3603 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
3604 ;; no match.
3605 cfd-re-match
3606 ;; The end position of the last `c-decl-prefix-or-start-re'
3607 ;; match. If this is greater than `cfd-continue-pos', the
3608 ;; next regexp search is started here instead.
3609 (cfd-re-match-end (point-min))
3610 ;; The end of the last `c-decl-end' found by
3611 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
3612 ;; match. If searching for the property isn't needed then we
3613 ;; disable it by setting it to `cfd-limit' directly.
3614 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3615 ;; The end of the token preceding the decl spot last found by
3616 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
3617 ;; bob. `cfd-limit' if there's no match. In other words,
3618 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
3619 (cfd-match-pos cfd-limit)
3620 ;; The position to continue searching at.
3621 cfd-continue-pos
3622 ;; The position of the last "real" token we've stopped at.
3623 ;; This can be greater than `cfd-continue-pos' when we get
3624 ;; hits inside macros or at `c-decl-end' positions inside
3625 ;; comments.
3626 (cfd-token-pos 0)
3627 ;; The end position of the last entered macro.
3628 (cfd-macro-end 0))
3630 ;; Initialize by finding a syntactically relevant start position
3631 ;; before the point, and do the first `c-decl-prefix-or-start-re'
3632 ;; search unless we're at bob.
3634 (let (start-in-literal start-in-macro syntactic-pos)
3635 ;; Must back up a bit since we look for the end of the previous
3636 ;; statement or declaration, which is earlier than the first
3637 ;; returned match.
3639 (cond
3640 ;; First we need to move to a syntactically relevant position.
3641 ;; Begin by backing out of comment or string literals.
3642 ((and
3643 (when (c-got-face-at (point) c-literal-faces)
3644 ;; Try to use the faces to back up to the start of the
3645 ;; literal. FIXME: What if the point is on a declaration
3646 ;; inside a comment?
3647 (while (and (not (bobp))
3648 (c-got-face-at (1- (point)) c-literal-faces))
3649 (goto-char (previous-single-property-change
3650 (point) 'face nil (point-min))))
3652 ;; XEmacs doesn't fontify the quotes surrounding string
3653 ;; literals.
3654 (and (featurep 'xemacs)
3655 (eq (get-text-property (point) 'face)
3656 'font-lock-string-face)
3657 (not (bobp))
3658 (progn (backward-char)
3659 (not (looking-at c-string-limit-regexp)))
3660 (forward-char))
3662 ;; Don't trust the literal to contain only literal faces
3663 ;; (the font lock package might not have fontified the
3664 ;; start of it at all, for instance) so check that we have
3665 ;; arrived at something that looks like a start or else
3666 ;; resort to `c-literal-limits'.
3667 (unless (looking-at c-literal-start-regexp)
3668 (let ((range (c-literal-limits)))
3669 (if range (goto-char (car range)))))
3671 (setq start-in-literal (point)))
3673 ;; The start is in a literal. If the limit is in the same
3674 ;; one we don't have to find a syntactic position etc. We
3675 ;; only check that if the limit is at or before bonl to save
3676 ;; time; it covers the by far most common case when font-lock
3677 ;; refontifies the current line only.
3678 (<= cfd-limit (c-point 'bonl cfd-start-pos))
3679 (save-excursion
3680 (goto-char cfd-start-pos)
3681 (while (progn
3682 (goto-char (next-single-property-change
3683 (point) 'face nil cfd-limit))
3684 (and (< (point) cfd-limit)
3685 (c-got-face-at (point) c-literal-faces))))
3686 (= (point) cfd-limit)))
3688 ;; Completely inside a literal. Set up variables to trig the
3689 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
3690 ;; find a suitable start position.
3691 (setq cfd-continue-pos start-in-literal))
3693 ;; Check if the region might be completely inside a macro, to
3694 ;; optimize that like the completely-inside-literal above.
3695 ((save-excursion
3696 (and (= (forward-line 1) 0)
3697 (bolp) ; forward-line has funny behavior at eob.
3698 (>= (point) cfd-limit)
3699 (progn (backward-char)
3700 (eq (char-before) ?\\))))
3701 ;; (Maybe) completely inside a macro. Only need to trig the
3702 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
3703 ;; set things up.
3704 (setq cfd-continue-pos (1- cfd-start-pos)
3705 start-in-macro t))
3708 ;; Back out of any macro so we don't miss any declaration
3709 ;; that could follow after it.
3710 (when (c-beginning-of-macro)
3711 (setq start-in-macro t))
3713 ;; Now we're at a proper syntactically relevant position so we
3714 ;; can use the cache. But first clear it if it applied
3715 ;; further down.
3716 (c-invalidate-find-decl-cache cfd-start-pos)
3718 (setq syntactic-pos (point))
3719 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
3720 ;; Don't have to do this if the cache is relevant here,
3721 ;; typically if the same line is refontified again. If
3722 ;; we're just some syntactic whitespace further down we can
3723 ;; still use the cache to limit the skipping.
3724 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
3726 ;; If we hit `c-find-decl-syntactic-pos' and
3727 ;; `c-find-decl-match-pos' is set then we install the cached
3728 ;; values. If we hit `c-find-decl-syntactic-pos' and
3729 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3730 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3731 ;; and so we can continue the search from this point. If we
3732 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
3733 ;; the right spot to begin searching anyway.
3734 (if (and (eq (point) c-find-decl-syntactic-pos)
3735 c-find-decl-match-pos)
3736 (setq cfd-match-pos c-find-decl-match-pos
3737 cfd-continue-pos syntactic-pos)
3739 (setq c-find-decl-syntactic-pos syntactic-pos)
3741 (when (if (bobp)
3742 ;; Always consider bob a match to get the first
3743 ;; declaration in the file. Do this separately instead of
3744 ;; letting `c-decl-prefix-or-start-re' match bob, so that
3745 ;; regexp always can consume at least one character to
3746 ;; ensure that we won't get stuck in an infinite loop.
3747 (setq cfd-re-match 0)
3748 (backward-char)
3749 (c-beginning-of-current-token)
3750 (< (point) cfd-limit))
3751 ;; Do an initial search now. In the bob case above it's
3752 ;; only done to search for a `c-decl-end' spot.
3753 (c-find-decl-prefix-search))
3755 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
3756 cfd-match-pos)))))
3758 ;; Advance `cfd-continue-pos' if it's before the start position.
3759 ;; The closest continue position that might have effect at or
3760 ;; after the start depends on what we started in. This also
3761 ;; finds a suitable start position in the special cases when the
3762 ;; region is completely within a literal or macro.
3763 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
3765 (cond
3766 (start-in-macro
3767 ;; If we're in a macro then it's the closest preceding token
3768 ;; in the macro. Check this before `start-in-literal',
3769 ;; since if we're inside a literal in a macro, the preceding
3770 ;; token is earlier than any `c-decl-end' spot inside the
3771 ;; literal (comment).
3772 (goto-char (or start-in-literal cfd-start-pos))
3773 ;; The only syntactic ws in macros are comments.
3774 (c-backward-comments)
3775 (backward-char)
3776 (c-beginning-of-current-token))
3778 (start-in-literal
3779 ;; If we're in a comment it can only be the closest
3780 ;; preceding `c-decl-end' position within that comment, if
3781 ;; any. Go back to the beginning of such a property so that
3782 ;; `c-find-decl-prefix-search' will find the end of it.
3783 ;; (Can't stop at the end and install it directly on
3784 ;; `cfd-prop-match' since that variable might be cleared
3785 ;; after `cfd-fun' below.)
3787 ;; Note that if the literal is a string then the property
3788 ;; search will simply skip to the beginning of it right
3789 ;; away.
3790 (if (not c-type-decl-end-used)
3791 (goto-char start-in-literal)
3792 (goto-char cfd-start-pos)
3793 (while (progn
3794 (goto-char (previous-single-property-change
3795 (point) 'c-type nil start-in-literal))
3796 (and (> (point) start-in-literal)
3797 (not (eq (c-get-char-property (point) 'c-type)
3798 'c-decl-end))))))
3800 (when (= (point) start-in-literal)
3801 ;; Didn't find any property inside the comment, so we can
3802 ;; skip it entirely. (This won't skip past a string, but
3803 ;; that'll be handled quickly by the next
3804 ;; `c-find-decl-prefix-search' anyway.)
3805 (c-forward-single-comment)
3806 (if (> (point) cfd-limit)
3807 (goto-char cfd-limit))))
3810 ;; If we started in normal code, the only match that might
3811 ;; apply before the start is what we already got in
3812 ;; `cfd-match-pos' so we can continue at the start position.
3813 ;; (Note that we don't get here if the first match is below
3814 ;; it.)
3815 (goto-char cfd-start-pos)))
3817 ;; Delete found matches if they are before our new continue
3818 ;; position, so that `c-find-decl-prefix-search' won't back up
3819 ;; to them later on.
3820 (setq cfd-continue-pos (point))
3821 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
3822 (setq cfd-re-match nil))
3823 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
3824 (setq cfd-prop-match nil)))
3826 (if syntactic-pos
3827 ;; This is the normal case and we got a proper syntactic
3828 ;; position. If there's a match then it's always outside
3829 ;; macros and comments, so advance to the next token and set
3830 ;; `cfd-token-pos'. The loop below will later go back using
3831 ;; `cfd-continue-pos' to fix declarations inside the
3832 ;; syntactic ws.
3833 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
3834 (goto-char syntactic-pos)
3835 (c-forward-syntactic-ws)
3836 (and cfd-continue-pos
3837 (< cfd-continue-pos (point))
3838 (setq cfd-token-pos (point))))
3840 ;; Have one of the special cases when the region is completely
3841 ;; within a literal or macro. `cfd-continue-pos' is set to a
3842 ;; good start position for the search, so do it.
3843 (c-find-decl-prefix-search)))
3845 ;; Now loop. We already got the first match.
3847 (while (progn
3848 (while (and
3849 (< cfd-match-pos cfd-limit)
3852 ;; Kludge to filter out matches on the "<" that
3853 ;; aren't open parens, for the sake of languages
3854 ;; that got `c-recognize-<>-arglists' set.
3855 (and (eq (char-before cfd-match-pos) ?<)
3856 (not (c-get-char-property (1- cfd-match-pos)
3857 'syntax-table)))
3859 ;; If `cfd-continue-pos' is less or equal to
3860 ;; `cfd-token-pos', we've got a hit inside a macro
3861 ;; that's in the syntactic whitespace before the last
3862 ;; "real" declaration we've checked. If they're equal
3863 ;; we've arrived at the declaration a second time, so
3864 ;; there's nothing to do.
3865 (= cfd-continue-pos cfd-token-pos)
3867 (progn
3868 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3869 ;; we're still searching for declarations embedded in
3870 ;; the syntactic whitespace. In that case we need
3871 ;; only to skip comments and not macros, since they
3872 ;; can't be nested, and that's already been done in
3873 ;; `c-find-decl-prefix-search'.
3874 (when (> cfd-continue-pos cfd-token-pos)
3875 (c-forward-syntactic-ws)
3876 (setq cfd-token-pos (point)))
3878 ;; Continue if the following token fails the
3879 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3880 (when (or (>= (point) cfd-limit)
3881 (not (looking-at cfd-decl-re))
3882 (and cfd-face-checklist
3883 (not (c-got-face-at
3884 (point) cfd-face-checklist))))
3885 (goto-char cfd-continue-pos)
3886 t)))
3888 (< (point) cfd-limit))
3889 (c-find-decl-prefix-search))
3891 (< (point) cfd-limit))
3893 (when (and
3894 (>= (point) cfd-start-pos)
3896 (progn
3897 ;; Narrow to the end of the macro if we got a hit inside
3898 ;; one, to avoid recognizing things that start inside the
3899 ;; macro and end outside it.
3900 (when (> cfd-match-pos cfd-macro-end)
3901 ;; Not in the same macro as in the previous round.
3902 (save-excursion
3903 (goto-char cfd-match-pos)
3904 (setq cfd-macro-end
3905 (if (save-excursion (and (c-beginning-of-macro)
3906 (< (point) cfd-match-pos)))
3907 (progn (c-end-of-macro)
3908 (point))
3909 0))))
3911 (if (zerop cfd-macro-end)
3913 (if (> cfd-macro-end (point))
3914 (progn (narrow-to-region (point-min) cfd-macro-end)
3916 ;; The matched token was the last thing in the macro,
3917 ;; so the whole match is bogus.
3918 (setq cfd-macro-end 0)
3919 nil))))
3921 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3922 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3923 (setq cfd-prop-match nil))
3925 (when (/= cfd-macro-end 0)
3926 ;; Restore limits if we did macro narrowment above.
3927 (narrow-to-region (point-min) cfd-buffer-end)))
3929 (goto-char cfd-continue-pos)
3930 (if (= cfd-continue-pos cfd-limit)
3931 (setq cfd-match-pos cfd-limit)
3932 (c-find-decl-prefix-search)))))
3935 ;; A cache for found types.
3937 ;; Buffer local variable that contains an obarray with the types we've
3938 ;; found. If a declaration is recognized somewhere we record the
3939 ;; fully qualified identifier in it to recognize it as a type
3940 ;; elsewhere in the file too. This is not accurate since we do not
3941 ;; bother with the scoping rules of the languages, but in practice the
3942 ;; same name is seldom used as both a type and something else in a
3943 ;; file, and we only use this as a last resort in ambiguous cases (see
3944 ;; `c-forward-decl-or-cast-1').
3946 ;; Template types in C++ are added here too but with the template
3947 ;; arglist replaced with "<>" in references or "<" for the one in the
3948 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
3949 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
3950 ;; template specs can be fairly sized programs in themselves) and
3951 ;; improves the hit ratio (it's a type regardless of the template
3952 ;; args; it's just not the same type, but we're only interested in
3953 ;; recognizing types, not telling distinct types apart). Note that
3954 ;; template types in references are added here too; from the example
3955 ;; above there will also be an entry "Foo<".
3956 (defvar c-found-types nil)
3957 (make-variable-buffer-local 'c-found-types)
3959 (defsubst c-clear-found-types ()
3960 ;; Clears `c-found-types'.
3961 (setq c-found-types (make-vector 53 0)))
3963 (defun c-add-type (from to)
3964 ;; Add the given region as a type in `c-found-types'. If the region
3965 ;; doesn't match an existing type but there is a type which is equal
3966 ;; to the given one except that the last character is missing, then
3967 ;; the shorter type is removed. That's done to avoid adding all
3968 ;; prefixes of a type as it's being entered and font locked. This
3969 ;; doesn't cover cases like when characters are removed from a type
3970 ;; or added in the middle. We'd need the position of point when the
3971 ;; font locking is invoked to solve this well.
3973 ;; This function might do hidden buffer changes.
3974 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
3975 (unless (intern-soft type c-found-types)
3976 (unintern (substring type 0 -1) c-found-types)
3977 (intern type c-found-types))))
3979 (defsubst c-check-type (from to)
3980 ;; Return non-nil if the given region contains a type in
3981 ;; `c-found-types'.
3983 ;; This function might do hidden buffer changes.
3984 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
3985 c-found-types))
3987 (defun c-list-found-types ()
3988 ;; Return all the types in `c-found-types' as a sorted list of
3989 ;; strings.
3990 (let (type-list)
3991 (mapatoms (lambda (type)
3992 (setq type-list (cons (symbol-name type)
3993 type-list)))
3994 c-found-types)
3995 (sort type-list 'string-lessp)))
3998 ;; Handling of small scale constructs like types and names.
4000 (defun c-after-change-check-<>-operators (beg end)
4001 ;; This is called from `after-change-functions' when
4002 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
4003 ;; chars with paren syntax become part of another operator like "<<"
4004 ;; or ">=".
4006 ;; This function might do hidden buffer changes.
4008 (save-excursion
4009 (goto-char beg)
4010 (when (or (looking-at "[<>]")
4011 (< (skip-chars-backward "<>") 0))
4013 (goto-char beg)
4014 (c-beginning-of-current-token)
4015 (when (and (< (point) beg)
4016 (looking-at c-<>-multichar-token-regexp)
4017 (< beg (setq beg (match-end 0))))
4018 (while (progn (skip-chars-forward "^<>" beg)
4019 (< (point) beg))
4020 (c-clear-char-property (point) 'syntax-table)
4021 (forward-char))))
4023 (when (< beg end)
4024 (goto-char end)
4025 (when (or (looking-at "[<>]")
4026 (< (skip-chars-backward "<>") 0))
4028 (goto-char end)
4029 (c-beginning-of-current-token)
4030 (when (and (< (point) end)
4031 (looking-at c-<>-multichar-token-regexp)
4032 (< end (setq end (match-end 0))))
4033 (while (progn (skip-chars-forward "^<>" end)
4034 (< (point) end))
4035 (c-clear-char-property (point) 'syntax-table)
4036 (forward-char)))))))
4038 ;; Dynamically bound variable that instructs `c-forward-type' to also
4039 ;; treat possible types (i.e. those that it normally returns 'maybe or
4040 ;; 'found for) as actual types (and always return 'found for them).
4041 ;; This means that it records them in `c-record-type-identifiers' if
4042 ;; that is set, and that it adds them to `c-found-types'.
4043 (defvar c-promote-possible-types nil)
4045 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4046 ;; mark up successfully parsed arglists with paren syntax properties on
4047 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
4048 ;; `c-type' property of each argument separating comma.
4050 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
4051 ;; all arglists for side effects (i.e. recording types), otherwise it
4052 ;; exploits any existing paren syntax properties to quickly jump to the
4053 ;; end of already parsed arglists.
4055 ;; Marking up the arglists is not the default since doing that correctly
4056 ;; depends on a proper value for `c-restricted-<>-arglists'.
4057 (defvar c-parse-and-markup-<>-arglists nil)
4059 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4060 ;; not accept arglists that contain binary operators.
4062 ;; This is primarily used to handle C++ template arglists. C++
4063 ;; disambiguates them by checking whether the preceding name is a
4064 ;; template or not. We can't do that, so we assume it is a template
4065 ;; if it can be parsed as one. That usually works well since
4066 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
4067 ;; in almost all cases would be pointless.
4069 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
4070 ;; should let the comma separate the function arguments instead. And
4071 ;; in a context where the value of the expression is taken, e.g. in
4072 ;; "if (a < b || c > d)", it's probably not a template.
4073 (defvar c-restricted-<>-arglists nil)
4075 ;; Dynamically bound variables that instructs
4076 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
4077 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
4078 ;; `c-forward-label' to record the ranges of all the type and
4079 ;; reference identifiers they encounter. They will build lists on
4080 ;; these variables where each element is a cons of the buffer
4081 ;; positions surrounding each identifier. This recording is only
4082 ;; activated when `c-record-type-identifiers' is non-nil.
4084 ;; All known types that can't be identifiers are recorded, and also
4085 ;; other possible types if `c-promote-possible-types' is set.
4086 ;; Recording is however disabled inside angle bracket arglists that
4087 ;; are encountered inside names and other angle bracket arglists.
4088 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
4089 ;; instead.
4091 ;; Only the names in C++ template style references (e.g. "tmpl" in
4092 ;; "tmpl<a,b>::foo") are recorded as references, other references
4093 ;; aren't handled here.
4095 ;; `c-forward-label' records the label identifier(s) on
4096 ;; `c-record-ref-identifiers'.
4097 (defvar c-record-type-identifiers nil)
4098 (defvar c-record-ref-identifiers nil)
4100 ;; This variable will receive a cons cell of the range of the last
4101 ;; single identifier symbol stepped over by `c-forward-name' if it's
4102 ;; successful. This is the range that should be put on one of the
4103 ;; record lists above by the caller. It's assigned nil if there's no
4104 ;; such symbol in the name.
4105 (defvar c-last-identifier-range nil)
4107 (defmacro c-record-type-id (range)
4108 (if (eq (car-safe range) 'cons)
4109 ;; Always true.
4110 `(setq c-record-type-identifiers
4111 (cons ,range c-record-type-identifiers))
4112 `(let ((range ,range))
4113 (if range
4114 (setq c-record-type-identifiers
4115 (cons range c-record-type-identifiers))))))
4117 (defmacro c-record-ref-id (range)
4118 (if (eq (car-safe range) 'cons)
4119 ;; Always true.
4120 `(setq c-record-ref-identifiers
4121 (cons ,range c-record-ref-identifiers))
4122 `(let ((range ,range))
4123 (if range
4124 (setq c-record-ref-identifiers
4125 (cons range c-record-ref-identifiers))))))
4127 ;; Dynamically bound variable that instructs `c-forward-type' to
4128 ;; record the ranges of types that only are found. Behaves otherwise
4129 ;; like `c-record-type-identifiers'.
4130 (defvar c-record-found-types nil)
4132 (defmacro c-forward-keyword-prefixed-id (type)
4133 ;; Used internally in `c-forward-keyword-clause' to move forward
4134 ;; over a type (if TYPE is 'type) or a name (otherwise) which
4135 ;; possibly is prefixed by keywords and their associated clauses.
4136 ;; Try with a type/name first to not trip up on those that begin
4137 ;; with a keyword. Return t if a known or found type is moved
4138 ;; over. The point is clobbered if nil is returned. If range
4139 ;; recording is enabled, the identifier is recorded on as a type
4140 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
4142 ;; This macro might do hidden buffer changes.
4143 `(let (res)
4144 (while (if (setq res ,(if (eq type 'type)
4145 `(c-forward-type)
4146 `(c-forward-name)))
4148 (and (looking-at c-keywords-regexp)
4149 (c-forward-keyword-clause 1))))
4150 (when (memq res '(t known found prefix))
4151 ,(when (eq type 'ref)
4152 `(when c-record-type-identifiers
4153 (c-record-ref-id c-last-identifier-range)))
4154 t)))
4156 (defmacro c-forward-id-comma-list (type update-safe-pos)
4157 ;; Used internally in `c-forward-keyword-clause' to move forward
4158 ;; over a comma separated list of types or names using
4159 ;; `c-forward-keyword-prefixed-id'.
4161 ;; This macro might do hidden buffer changes.
4162 `(while (and (progn
4163 ,(when update-safe-pos
4164 `(setq safe-pos (point)))
4165 (eq (char-after) ?,))
4166 (progn
4167 (forward-char)
4168 (c-forward-syntactic-ws)
4169 (c-forward-keyword-prefixed-id ,type)))))
4171 (defun c-forward-keyword-clause (match)
4172 ;; Submatch MATCH in the current match data is assumed to surround a
4173 ;; token. If it's a keyword, move over it and any immediately
4174 ;; following clauses associated with it, stopping at the start of
4175 ;; the next token. t is returned in that case, otherwise the point
4176 ;; stays and nil is returned. The kind of clauses that are
4177 ;; recognized are those specified by `c-type-list-kwds',
4178 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
4179 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
4180 ;; and `c-<>-arglist-kwds'.
4182 ;; This function records identifier ranges on
4183 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4184 ;; `c-record-type-identifiers' is non-nil.
4186 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
4187 ;; apply directly after the keyword, the type list is moved over
4188 ;; only when there is no unaccounted token before it (i.e. a token
4189 ;; that isn't moved over due to some other keyword list). The
4190 ;; identifier ranges in the list are still recorded if that should
4191 ;; be done, though.
4193 ;; This function might do hidden buffer changes.
4195 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
4196 ;; The call to `c-forward-<>-arglist' below is made after
4197 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
4198 ;; are angle bracket arglists and `c-restricted-<>-arglists'
4199 ;; should therefore be nil.
4200 (c-parse-and-markup-<>-arglists t)
4201 c-restricted-<>-arglists)
4203 (when kwd-sym
4204 (goto-char (match-end match))
4205 (c-forward-syntactic-ws)
4206 (setq safe-pos (point))
4208 (cond
4209 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
4210 (c-forward-keyword-prefixed-id type))
4211 ;; There's a type directly after a keyword in `c-type-list-kwds'.
4212 (c-forward-id-comma-list type t))
4214 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
4215 (c-forward-keyword-prefixed-id ref))
4216 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
4217 (c-forward-id-comma-list ref t))
4219 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
4220 (eq (char-after) ?\())
4221 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
4223 (forward-char)
4224 (when (and (setq pos (c-up-list-forward))
4225 (eq (char-before pos) ?\)))
4226 (when (and c-record-type-identifiers
4227 (c-keyword-member kwd-sym 'c-paren-type-kwds))
4228 ;; Use `c-forward-type' on every identifier we can find
4229 ;; inside the paren, to record the types.
4230 (while (c-syntactic-re-search-forward c-symbol-start pos t)
4231 (goto-char (match-beginning 0))
4232 (unless (c-forward-type)
4233 (looking-at c-symbol-key) ; Always matches.
4234 (goto-char (match-end 0)))))
4236 (goto-char pos)
4237 (c-forward-syntactic-ws)
4238 (setq safe-pos (point))))
4240 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
4241 (eq (char-after) ?<)
4242 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
4243 (c-forward-syntactic-ws)
4244 (setq safe-pos (point)))
4246 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
4247 (not (looking-at c-symbol-start))
4248 (c-safe (c-forward-sexp) t))
4249 (c-forward-syntactic-ws)
4250 (setq safe-pos (point))))
4252 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
4253 (if (eq (char-after) ?:)
4254 ;; If we are at the colon already, we move over the type
4255 ;; list after it.
4256 (progn
4257 (forward-char)
4258 (c-forward-syntactic-ws)
4259 (when (c-forward-keyword-prefixed-id type)
4260 (c-forward-id-comma-list type t)))
4261 ;; Not at the colon, so stop here. But the identifier
4262 ;; ranges in the type list later on should still be
4263 ;; recorded.
4264 (and c-record-type-identifiers
4265 (progn
4266 ;; If a keyword matched both one of the types above and
4267 ;; this one, we match `c-colon-type-list-re' after the
4268 ;; clause matched above.
4269 (goto-char safe-pos)
4270 (looking-at c-colon-type-list-re))
4271 (progn
4272 (goto-char (match-end 0))
4273 (c-forward-syntactic-ws)
4274 (c-forward-keyword-prefixed-id type))
4275 ;; There's a type after the `c-colon-type-list-re' match
4276 ;; after a keyword in `c-colon-type-list-kwds'.
4277 (c-forward-id-comma-list type nil))))
4279 (goto-char safe-pos)
4280 t)))
4282 (defun c-forward-<>-arglist (all-types)
4283 ;; The point is assumed to be at a "<". Try to treat it as the open
4284 ;; paren of an angle bracket arglist and move forward to the the
4285 ;; corresponding ">". If successful, the point is left after the
4286 ;; ">" and t is returned, otherwise the point isn't moved and nil is
4287 ;; returned. If ALL-TYPES is t then all encountered arguments in
4288 ;; the arglist that might be types are treated as found types.
4290 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
4291 ;; function handles text properties on the angle brackets and argument
4292 ;; separating commas.
4294 ;; `c-restricted-<>-arglists' controls how lenient the template
4295 ;; arglist recognition should be.
4297 ;; This function records identifier ranges on
4298 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4299 ;; `c-record-type-identifiers' is non-nil.
4301 ;; This function might do hidden buffer changes.
4303 (let ((start (point))
4304 ;; If `c-record-type-identifiers' is set then activate
4305 ;; recording of any found types that constitute an argument in
4306 ;; the arglist.
4307 (c-record-found-types (if c-record-type-identifiers t)))
4308 (if (catch 'angle-bracket-arglist-escape
4309 (setq c-record-found-types
4310 (c-forward-<>-arglist-recur all-types)))
4311 (progn
4312 (when (consp c-record-found-types)
4313 (setq c-record-type-identifiers
4314 ;; `nconc' doesn't mind that the tail of
4315 ;; `c-record-found-types' is t.
4316 (nconc c-record-found-types c-record-type-identifiers)))
4319 (goto-char start)
4320 nil)))
4322 (defun c-forward-<>-arglist-recur (all-types)
4323 ;; Recursive part of `c-forward-<>-arglist'.
4325 ;; This function might do hidden buffer changes.
4327 (let ((start (point)) res pos tmp
4328 ;; Cover this so that any recorded found type ranges are
4329 ;; automatically lost if it turns out to not be an angle
4330 ;; bracket arglist. It's propagated through the return value
4331 ;; on successful completion.
4332 (c-record-found-types c-record-found-types)
4333 ;; List that collects the positions after the argument
4334 ;; separating ',' in the arglist.
4335 arg-start-pos)
4337 ;; If the '<' has paren open syntax then we've marked it as an angle
4338 ;; bracket arglist before, so skip to the end.
4339 (if (and (not c-parse-and-markup-<>-arglists)
4340 (c-get-char-property (point) 'syntax-table))
4342 (progn
4343 (forward-char)
4344 (if (and (c-go-up-list-forward)
4345 (eq (char-before) ?>))
4348 ;; Got unmatched paren angle brackets. We don't clear the paren
4349 ;; syntax properties and retry, on the basis that it's very
4350 ;; unlikely that paren angle brackets become operators by code
4351 ;; manipulation. It's far more likely that it doesn't match due
4352 ;; to narrowing or some temporary change.
4353 (goto-char start)
4354 nil))
4356 (forward-char)
4357 (unless (looking-at c-<-op-cont-regexp)
4358 (while (and
4359 (progn
4361 (when c-record-type-identifiers
4362 (if all-types
4364 ;; All encountered identifiers are types, so set the
4365 ;; promote flag and parse the type.
4366 (progn
4367 (c-forward-syntactic-ws)
4368 (when (looking-at c-identifier-start)
4369 (let ((c-promote-possible-types t))
4370 (c-forward-type))))
4372 ;; Check if this arglist argument is a sole type. If
4373 ;; it's known then it's recorded in
4374 ;; `c-record-type-identifiers'. If it only is found
4375 ;; then it's recorded in `c-record-found-types' which we
4376 ;; might roll back if it turns out that this isn't an
4377 ;; angle bracket arglist afterall.
4378 (when (memq (char-before) '(?, ?<))
4379 (let ((orig-record-found-types c-record-found-types))
4380 (c-forward-syntactic-ws)
4381 (and (memq (c-forward-type) '(known found))
4382 (not (looking-at "[,>]"))
4383 ;; A found type was recorded but it's not the
4384 ;; only thing in the arglist argument, so reset
4385 ;; `c-record-found-types'.
4386 (setq c-record-found-types
4387 orig-record-found-types))))))
4389 (setq pos (point))
4390 (or (when (eq (char-after) ?>)
4391 ;; Must check for '>' at the very start separately,
4392 ;; since the regexp below has to avoid ">>" without
4393 ;; using \\=.
4394 (forward-char)
4397 ;; Note: These regexps exploit the match order in \| so
4398 ;; that "<>" is matched by "<" rather than "[^>:-]>".
4399 (c-syntactic-re-search-forward
4400 (if c-restricted-<>-arglists
4401 ;; Stop on ',', '|', '&', '+' and '-' to catch
4402 ;; common binary operators that could be between
4403 ;; two comparison expressions "a<b" and "c>d".
4404 "[<;{},|&+-]\\|\\([^>:-]>\\)"
4405 ;; Otherwise we still stop on ',' to find the
4406 ;; argument start positions.
4407 "[<;{},]\\|\\([^>:-]>\\)")
4408 nil 'move t t 1)
4410 ;; If the arglist starter has lost its open paren
4411 ;; syntax but not the closer, we won't find the
4412 ;; closer above since we only search in the
4413 ;; balanced sexp. In that case we stop just short
4414 ;; of it so check if the following char is the closer.
4415 (when (eq (char-after) ?>)
4416 (forward-char)
4417 t)))
4419 (cond
4420 ((eq (char-before) ?>)
4421 ;; Either an operator starting with '>' or the end of
4422 ;; the angle bracket arglist.
4424 (if (looking-at c->-op-cont-regexp)
4425 (progn
4426 (goto-char (match-end 0))
4427 t) ; Continue the loop.
4429 ;; The angle bracket arglist is finished.
4430 (when c-parse-and-markup-<>-arglists
4431 (while arg-start-pos
4432 (c-put-c-type-property (1- (car arg-start-pos))
4433 'c-<>-arg-sep)
4434 (setq arg-start-pos (cdr arg-start-pos)))
4435 (c-mark-<-as-paren start)
4436 (c-mark->-as-paren (1- (point))))
4437 (setq res t)
4438 nil)) ; Exit the loop.
4440 ((eq (char-before) ?<)
4441 ;; Either an operator starting with '<' or a nested arglist.
4443 (setq pos (point))
4444 (let (id-start id-end subres keyword-match)
4445 (if (if (looking-at c-<-op-cont-regexp)
4446 (setq tmp (match-end 0))
4447 (setq tmp pos)
4448 (backward-char)
4449 (not
4450 (and
4452 (save-excursion
4453 ;; There's always an identifier before an angle
4454 ;; bracket arglist, or a keyword in
4455 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
4456 (c-backward-syntactic-ws)
4457 (setq id-end (point))
4458 (c-simple-skip-symbol-backward)
4459 (when (or (setq keyword-match
4460 (looking-at c-opt-<>-sexp-key))
4461 (not (looking-at c-keywords-regexp)))
4462 (setq id-start (point))))
4464 (setq subres
4465 (let ((c-record-type-identifiers nil)
4466 (c-record-found-types nil))
4467 (c-forward-<>-arglist-recur
4468 (and keyword-match
4469 (c-keyword-member
4470 (c-keyword-sym (match-string 1))
4471 'c-<>-type-kwds)))))
4474 ;; It was not an angle bracket arglist.
4475 (goto-char tmp)
4477 ;; It was an angle bracket arglist.
4478 (setq c-record-found-types subres)
4480 ;; Record the identifier before the template as a type
4481 ;; or reference depending on whether the arglist is last
4482 ;; in a qualified identifier.
4483 (when (and c-record-type-identifiers
4484 (not keyword-match))
4485 (if (and c-opt-identifier-concat-key
4486 (progn
4487 (c-forward-syntactic-ws)
4488 (looking-at c-opt-identifier-concat-key)))
4489 (c-record-ref-id (cons id-start id-end))
4490 (c-record-type-id (cons id-start id-end))))))
4493 ((and (eq (char-before) ?,)
4494 (not c-restricted-<>-arglists))
4495 ;; Just another argument. Record the position. The
4496 ;; type check stuff that made us stop at it is at
4497 ;; the top of the loop.
4498 (setq arg-start-pos (cons (point) arg-start-pos)))
4501 ;; Got a character that can't be in an angle bracket
4502 ;; arglist argument. Abort using `throw', since
4503 ;; it's useless to try to find a surrounding arglist
4504 ;; if we're nested.
4505 (throw 'angle-bracket-arglist-escape nil))))))
4507 (if res
4508 (or c-record-found-types t)))))
4510 (defun c-backward-<>-arglist (all-types &optional limit)
4511 ;; The point is assumed to be directly after a ">". Try to treat it
4512 ;; as the close paren of an angle bracket arglist and move back to
4513 ;; the corresponding "<". If successful, the point is left at
4514 ;; the "<" and t is returned, otherwise the point isn't moved and
4515 ;; nil is returned. ALL-TYPES is passed on to
4516 ;; `c-forward-<>-arglist'.
4518 ;; If the optional LIMIT is given, it bounds the backward search.
4519 ;; It's then assumed to be at a syntactically relevant position.
4521 ;; This is a wrapper around `c-forward-<>-arglist'. See that
4522 ;; function for more details.
4524 (let ((start (point)))
4525 (backward-char)
4526 (if (and (not c-parse-and-markup-<>-arglists)
4527 (c-get-char-property (point) 'syntax-table))
4529 (if (and (c-go-up-list-backward)
4530 (eq (char-after) ?<))
4532 ;; See corresponding note in `c-forward-<>-arglist'.
4533 (goto-char start)
4534 nil)
4536 (while (and
4537 (c-syntactic-skip-backward "^<;{}" limit t)
4539 (if (eq (char-before) ?<)
4541 ;; Stopped at bob or a char that isn't allowed in an
4542 ;; arglist, so we've failed.
4543 (goto-char start)
4544 nil)
4546 (if (> (point)
4547 (progn (c-beginning-of-current-token)
4548 (point)))
4549 ;; If we moved then the "<" was part of some
4550 ;; multicharacter token.
4553 (backward-char)
4554 (let ((beg-pos (point)))
4555 (if (c-forward-<>-arglist all-types)
4556 (cond ((= (point) start)
4557 ;; Matched the arglist. Break the while.
4558 (goto-char beg-pos)
4559 nil)
4560 ((> (point) start)
4561 ;; We started from a non-paren ">" inside an
4562 ;; arglist.
4563 (goto-char start)
4564 nil)
4566 ;; Matched a shorter arglist. Can be a nested
4567 ;; one so continue looking.
4568 (goto-char beg-pos)
4570 t)))))
4572 (/= (point) start))))
4574 (defun c-forward-name ()
4575 ;; Move forward over a complete name if at the beginning of one,
4576 ;; stopping at the next following token. If the point is not at
4577 ;; something that are recognized as name then it stays put. A name
4578 ;; could be something as simple as "foo" in C or something as
4579 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
4580 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
4581 ;; int>::*volatile const" in C++ (this function is actually little
4582 ;; more than a `looking-at' call in all modes except those that,
4583 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
4584 ;; name is found, 'template if it's an identifier ending with an
4585 ;; angle bracket arglist, 'operator of it's an operator identifier,
4586 ;; or t if it's some other kind of name.
4588 ;; This function records identifier ranges on
4589 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4590 ;; `c-record-type-identifiers' is non-nil.
4592 ;; This function might do hidden buffer changes.
4594 (let ((pos (point)) (start (point)) res id-start id-end
4595 ;; Turn off `c-promote-possible-types' here since we might
4596 ;; call `c-forward-<>-arglist' and we don't want it to promote
4597 ;; every suspect thing in the arglist to a type. We're
4598 ;; typically called from `c-forward-type' in this case, and
4599 ;; the caller only wants the top level type that it finds to
4600 ;; be promoted.
4601 c-promote-possible-types)
4602 (while
4603 (and
4604 (looking-at c-identifier-key)
4606 (progn
4607 ;; Check for keyword. We go to the last symbol in
4608 ;; `c-identifier-key' first.
4609 (goto-char (setq id-end (match-end 0)))
4610 (c-simple-skip-symbol-backward)
4611 (setq id-start (point))
4613 (if (looking-at c-keywords-regexp)
4614 (when (and (c-major-mode-is 'c++-mode)
4615 (looking-at
4616 (cc-eval-when-compile
4617 (concat "\\(operator\\|\\(template\\)\\)"
4618 "\\(" (c-lang-const c-nonsymbol-key c++)
4619 "\\|$\\)")))
4620 (if (match-beginning 2)
4621 ;; "template" is only valid inside an
4622 ;; identifier if preceded by "::".
4623 (save-excursion
4624 (c-backward-syntactic-ws)
4625 (and (c-safe (backward-char 2) t)
4626 (looking-at "::")))
4629 ;; Handle a C++ operator or template identifier.
4630 (goto-char id-end)
4631 (c-forward-syntactic-ws)
4632 (cond ((eq (char-before id-end) ?e)
4633 ;; Got "... ::template".
4634 (let ((subres (c-forward-name)))
4635 (when subres
4636 (setq pos (point)
4637 res subres))))
4639 ((looking-at c-identifier-start)
4640 ;; Got a cast operator.
4641 (when (c-forward-type)
4642 (setq pos (point)
4643 res 'operator)
4644 ;; Now we should match a sequence of either
4645 ;; '*', '&' or a name followed by ":: *",
4646 ;; where each can be followed by a sequence
4647 ;; of `c-opt-type-modifier-key'.
4648 (while (cond ((looking-at "[*&]")
4649 (goto-char (match-end 0))
4651 ((looking-at c-identifier-start)
4652 (and (c-forward-name)
4653 (looking-at "::")
4654 (progn
4655 (goto-char (match-end 0))
4656 (c-forward-syntactic-ws)
4657 (eq (char-after) ?*))
4658 (progn
4659 (forward-char)
4660 t))))
4661 (while (progn
4662 (c-forward-syntactic-ws)
4663 (setq pos (point))
4664 (looking-at c-opt-type-modifier-key))
4665 (goto-char (match-end 1))))))
4667 ((looking-at c-overloadable-operators-regexp)
4668 ;; Got some other operator.
4669 (setq c-last-identifier-range
4670 (cons (point) (match-end 0)))
4671 (goto-char (match-end 0))
4672 (c-forward-syntactic-ws)
4673 (setq pos (point)
4674 res 'operator)))
4676 nil)
4678 ;; `id-start' is equal to `id-end' if we've jumped over
4679 ;; an identifier that doesn't end with a symbol token.
4680 ;; That can occur e.g. for Java import directives on the
4681 ;; form "foo.bar.*".
4682 (when (and id-start (/= id-start id-end))
4683 (setq c-last-identifier-range
4684 (cons id-start id-end)))
4685 (goto-char id-end)
4686 (c-forward-syntactic-ws)
4687 (setq pos (point)
4688 res t)))
4690 (progn
4691 (goto-char pos)
4692 (when (or c-opt-identifier-concat-key
4693 c-recognize-<>-arglists)
4695 (cond
4696 ((and c-opt-identifier-concat-key
4697 (looking-at c-opt-identifier-concat-key))
4698 ;; Got a concatenated identifier. This handles the
4699 ;; cases with tricky syntactic whitespace that aren't
4700 ;; covered in `c-identifier-key'.
4701 (goto-char (match-end 0))
4702 (c-forward-syntactic-ws)
4705 ((and c-recognize-<>-arglists
4706 (eq (char-after) ?<))
4707 ;; Maybe an angle bracket arglist.
4709 (when (let (c-record-type-identifiers
4710 c-record-found-types)
4711 (c-forward-<>-arglist nil))
4713 (c-add-type start (1+ pos))
4714 (c-forward-syntactic-ws)
4715 (setq pos (point)
4716 c-last-identifier-range nil)
4718 (if (and c-opt-identifier-concat-key
4719 (looking-at c-opt-identifier-concat-key))
4721 ;; Continue if there's an identifier concatenation
4722 ;; operator after the template argument.
4723 (progn
4724 (when (and c-record-type-identifiers id-start)
4725 (c-record-ref-id (cons id-start id-end)))
4726 (forward-char 2)
4727 (c-forward-syntactic-ws)
4730 (when (and c-record-type-identifiers id-start)
4731 (c-record-type-id (cons id-start id-end)))
4732 (setq res 'template)
4733 nil)))
4734 )))))
4736 (goto-char pos)
4737 res))
4739 (defun c-forward-type ()
4740 ;; Move forward over a type spec if at the beginning of one,
4741 ;; stopping at the next following token. Return t if it's a known
4742 ;; type that can't be a name or other expression, 'known if it's an
4743 ;; otherwise known type (according to `*-font-lock-extra-types'),
4744 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4745 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4746 ;; that might be a type, or nil if it can't be a type (the point
4747 ;; isn't moved then). The point is assumed to be at the beginning
4748 ;; of a token.
4750 ;; Note that this function doesn't skip past the brace definition
4751 ;; that might be considered part of the type, e.g.
4752 ;; "enum {a, b, c} foo".
4754 ;; This function records identifier ranges on
4755 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4756 ;; `c-record-type-identifiers' is non-nil.
4758 ;; This function might do hidden buffer changes.
4760 (let ((start (point)) pos res name-res id-start id-end id-range)
4762 ;; Skip leading type modifiers. If any are found we know it's a
4763 ;; prefix of a type.
4764 (when c-opt-type-modifier-key
4765 (while (looking-at c-opt-type-modifier-key)
4766 (goto-char (match-end 1))
4767 (c-forward-syntactic-ws)
4768 (setq res 'prefix)))
4770 (cond
4771 ((looking-at c-type-prefix-key)
4772 ;; Looking at a keyword that prefixes a type identifier,
4773 ;; e.g. "class".
4774 (goto-char (match-end 1))
4775 (c-forward-syntactic-ws)
4776 (setq pos (point))
4777 (if (memq (setq name-res (c-forward-name)) '(t template))
4778 (progn
4779 (when (eq name-res t)
4780 ;; In many languages the name can be used without the
4781 ;; prefix, so we add it to `c-found-types'.
4782 (c-add-type pos (point))
4783 (when (and c-record-type-identifiers
4784 c-last-identifier-range)
4785 (c-record-type-id c-last-identifier-range)))
4786 (setq res t))
4787 ;; Invalid syntax.
4788 (goto-char start)
4789 (setq res nil)))
4791 ((progn
4792 (setq pos nil)
4793 (if (looking-at c-identifier-start)
4794 (save-excursion
4795 (setq id-start (point)
4796 name-res (c-forward-name))
4797 (when name-res
4798 (setq id-end (point)
4799 id-range c-last-identifier-range))))
4800 (and (cond ((looking-at c-primitive-type-key)
4801 (setq res t))
4802 ((c-with-syntax-table c-identifier-syntax-table
4803 (looking-at c-known-type-key))
4804 (setq res 'known)))
4805 (or (not id-end)
4806 (>= (save-excursion
4807 (save-match-data
4808 (goto-char (match-end 1))
4809 (c-forward-syntactic-ws)
4810 (setq pos (point))))
4811 id-end)
4812 (setq res nil))))
4813 ;; Looking at a primitive or known type identifier. We've
4814 ;; checked for a name first so that we don't go here if the
4815 ;; known type match only is a prefix of another name.
4817 (setq id-end (match-end 1))
4819 (when (and c-record-type-identifiers
4820 (or c-promote-possible-types (eq res t)))
4821 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4823 (if (and c-opt-type-component-key
4824 (save-match-data
4825 (looking-at c-opt-type-component-key)))
4826 ;; There might be more keywords for the type.
4827 (let (safe-pos)
4828 (c-forward-keyword-clause 1)
4829 (while (progn
4830 (setq safe-pos (point))
4831 (looking-at c-opt-type-component-key))
4832 (when (and c-record-type-identifiers
4833 (looking-at c-primitive-type-key))
4834 (c-record-type-id (cons (match-beginning 1)
4835 (match-end 1))))
4836 (c-forward-keyword-clause 1))
4837 (if (looking-at c-primitive-type-key)
4838 (progn
4839 (when c-record-type-identifiers
4840 (c-record-type-id (cons (match-beginning 1)
4841 (match-end 1))))
4842 (c-forward-keyword-clause 1)
4843 (setq res t))
4844 (goto-char safe-pos)
4845 (setq res 'prefix)))
4846 (unless (save-match-data (c-forward-keyword-clause 1))
4847 (if pos
4848 (goto-char pos)
4849 (goto-char (match-end 1))
4850 (c-forward-syntactic-ws)))))
4852 (name-res
4853 (cond ((eq name-res t)
4854 ;; A normal identifier.
4855 (goto-char id-end)
4856 (if (or res c-promote-possible-types)
4857 (progn
4858 (c-add-type id-start id-end)
4859 (when (and c-record-type-identifiers id-range)
4860 (c-record-type-id id-range))
4861 (unless res
4862 (setq res 'found)))
4863 (setq res (if (c-check-type id-start id-end)
4864 ;; It's an identifier that has been used as
4865 ;; a type somewhere else.
4866 'found
4867 ;; It's an identifier that might be a type.
4868 'maybe))))
4869 ((eq name-res 'template)
4870 ;; A template is a type.
4871 (goto-char id-end)
4872 (setq res t))
4874 ;; Otherwise it's an operator identifier, which is not a type.
4875 (goto-char start)
4876 (setq res nil)))))
4878 (when res
4879 ;; Skip trailing type modifiers. If any are found we know it's
4880 ;; a type.
4881 (when c-opt-type-modifier-key
4882 (while (looking-at c-opt-type-modifier-key)
4883 (goto-char (match-end 1))
4884 (c-forward-syntactic-ws)
4885 (setq res t)))
4887 ;; Step over any type suffix operator. Do not let the existence
4888 ;; of these alter the classification of the found type, since
4889 ;; these operators typically are allowed in normal expressions
4890 ;; too.
4891 (when c-opt-type-suffix-key
4892 (while (looking-at c-opt-type-suffix-key)
4893 (goto-char (match-end 1))
4894 (c-forward-syntactic-ws)))
4896 (when c-opt-type-concat-key
4897 ;; Look for a trailing operator that concatenates the type
4898 ;; with a following one, and if so step past that one through
4899 ;; a recursive call. Note that we don't record concatenated
4900 ;; types in `c-found-types' - it's the component types that
4901 ;; are recorded when appropriate.
4902 (setq pos (point))
4903 (let* ((c-promote-possible-types (or (memq res '(t known))
4904 c-promote-possible-types))
4905 ;; If we can't promote then set `c-record-found-types' so that
4906 ;; we can merge in the types from the second part afterwards if
4907 ;; it turns out to be a known type there.
4908 (c-record-found-types (and c-record-type-identifiers
4909 (not c-promote-possible-types)))
4910 subres)
4911 (if (and (looking-at c-opt-type-concat-key)
4913 (progn
4914 (goto-char (match-end 1))
4915 (c-forward-syntactic-ws)
4916 (setq subres (c-forward-type))))
4918 (progn
4919 ;; If either operand certainly is a type then both are, but we
4920 ;; don't let the existence of the operator itself promote two
4921 ;; uncertain types to a certain one.
4922 (cond ((eq res t))
4923 ((eq subres t)
4924 (unless (eq name-res 'template)
4925 (c-add-type id-start id-end))
4926 (when (and c-record-type-identifiers id-range)
4927 (c-record-type-id id-range))
4928 (setq res t))
4929 ((eq res 'known))
4930 ((eq subres 'known)
4931 (setq res 'known))
4932 ((eq res 'found))
4933 ((eq subres 'found)
4934 (setq res 'found))
4936 (setq res 'maybe)))
4938 (when (and (eq res t)
4939 (consp c-record-found-types))
4940 ;; Merge in the ranges of any types found by the second
4941 ;; `c-forward-type'.
4942 (setq c-record-type-identifiers
4943 ;; `nconc' doesn't mind that the tail of
4944 ;; `c-record-found-types' is t.
4945 (nconc c-record-found-types
4946 c-record-type-identifiers))))
4948 (goto-char pos))))
4950 (when (and c-record-found-types (memq res '(known found)) id-range)
4951 (setq c-record-found-types
4952 (cons id-range c-record-found-types))))
4954 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
4956 res))
4959 ;; Handling of large scale constructs like statements and declarations.
4961 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
4962 ;; defsubst or perhaps even a defun, but it contains lots of free
4963 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
4964 (defmacro c-fdoc-shift-type-backward (&optional short)
4965 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
4966 ;; of types when parsing a declaration, which means that it
4967 ;; sometimes consumes the identifier in the declaration as a type.
4968 ;; This is used to "backtrack" and make the last type be treated as
4969 ;; an identifier instead.
4970 `(progn
4971 ,(unless short
4972 ;; These identifiers are bound only in the inner let.
4973 '(setq identifier-type at-type
4974 identifier-start type-start
4975 got-parens nil
4976 got-identifier t
4977 got-suffix t
4978 got-suffix-after-parens id-start
4979 paren-depth 0))
4981 (if (setq at-type (if (eq backup-at-type 'prefix)
4983 backup-at-type))
4984 (setq type-start backup-type-start
4985 id-start backup-id-start)
4986 (setq type-start start-pos
4987 id-start start-pos))
4989 ;; When these flags already are set we've found specifiers that
4990 ;; unconditionally signal these attributes - backtracking doesn't
4991 ;; change that. So keep them set in that case.
4992 (or at-type-decl
4993 (setq at-type-decl backup-at-type-decl))
4994 (or maybe-typeless
4995 (setq maybe-typeless backup-maybe-typeless))
4997 ,(unless short
4998 ;; This identifier is bound only in the inner let.
4999 '(setq start id-start))))
5001 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
5002 ;; Move forward over a declaration or a cast if at the start of one.
5003 ;; The point is assumed to be at the start of some token. Nil is
5004 ;; returned if no declaration or cast is recognized, and the point
5005 ;; is clobbered in that case.
5007 ;; If a declaration is parsed:
5009 ;; The point is left at the first token after the first complete
5010 ;; declarator, if there is one. The return value is a cons where
5011 ;; the car is the position of the first token in the declarator.
5012 ;; Some examples:
5014 ;; void foo (int a, char *b) stuff ...
5015 ;; car ^ ^ point
5016 ;; float (*a)[], b;
5017 ;; car ^ ^ point
5018 ;; unsigned int a = c_style_initializer, b;
5019 ;; car ^ ^ point
5020 ;; unsigned int a (cplusplus_style_initializer), b;
5021 ;; car ^ ^ point (might change)
5022 ;; class Foo : public Bar {}
5023 ;; car ^ ^ point
5024 ;; class PikeClass (int a, string b) stuff ...
5025 ;; car ^ ^ point
5026 ;; enum bool;
5027 ;; car ^ ^ point
5028 ;; enum bool flag;
5029 ;; car ^ ^ point
5030 ;; void cplusplus_function (int x) throw (Bad);
5031 ;; car ^ ^ point
5032 ;; Foo::Foo (int b) : Base (b) {}
5033 ;; car ^ ^ point
5035 ;; The cdr of the return value is non-nil iff a
5036 ;; `c-typedef-decl-kwds' specifier is found in the declaration,
5037 ;; i.e. the declared identifier(s) are types.
5039 ;; If a cast is parsed:
5041 ;; The point is left at the first token after the closing paren of
5042 ;; the cast. The return value is `cast'. Note that the start
5043 ;; position must be at the first token inside the cast parenthesis
5044 ;; to recognize it.
5046 ;; PRECEDING-TOKEN-END is the first position after the preceding
5047 ;; token, i.e. on the other side of the syntactic ws from the point.
5048 ;; Use a value less than or equal to (point-min) if the point is at
5049 ;; the first token in (the visible part of) the buffer.
5051 ;; CONTEXT is a symbol that describes the context at the point:
5052 ;; 'decl In a comma-separatded declaration context (typically
5053 ;; inside a function declaration arglist).
5054 ;; '<> In an angle bracket arglist.
5055 ;; 'arglist Some other type of arglist.
5056 ;; nil Some other context or unknown context.
5058 ;; LAST-CAST-END is the first token after the closing paren of a
5059 ;; preceding cast, or nil if none is known. If
5060 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
5061 ;; the position after the closest preceding call where a cast was
5062 ;; matched. In that case it's used to discover chains of casts like
5063 ;; "(a) (b) c".
5065 ;; This function records identifier ranges on
5066 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5067 ;; `c-record-type-identifiers' is non-nil.
5069 ;; This function might do hidden buffer changes.
5071 (let (;; `start-pos' is used below to point to the start of the
5072 ;; first type, i.e. after any leading specifiers. It might
5073 ;; also point at the beginning of the preceding syntactic
5074 ;; whitespace.
5075 (start-pos (point))
5076 ;; Set to the result of `c-forward-type'.
5077 at-type
5078 ;; The position of the first token in what we currently
5079 ;; believe is the type in the declaration or cast, after any
5080 ;; specifiers and their associated clauses.
5081 type-start
5082 ;; The position of the first token in what we currently
5083 ;; believe is the declarator for the first identifier. Set
5084 ;; when the type is found, and moved forward over any
5085 ;; `c-decl-hangon-kwds' and their associated clauses that
5086 ;; occurs after the type.
5087 id-start
5088 ;; These store `at-type', `type-start' and `id-start' of the
5089 ;; identifier before the one in those variables. The previous
5090 ;; identifier might turn out to be the real type in a
5091 ;; declaration if the last one has to be the declarator in it.
5092 ;; If `backup-at-type' is nil then the other variables have
5093 ;; undefined values.
5094 backup-at-type backup-type-start backup-id-start
5095 ;; Set if we've found a specifier that makes the defined
5096 ;; identifier(s) types.
5097 at-type-decl
5098 ;; Set if we've found a specifier that can start a declaration
5099 ;; where there's no type.
5100 maybe-typeless
5101 ;; If a specifier is found that also can be a type prefix,
5102 ;; these flags are set instead of those above. If we need to
5103 ;; back up an identifier, they are copied to the real flag
5104 ;; variables. Thus they only take effect if we fail to
5105 ;; interpret it as a type.
5106 backup-at-type-decl backup-maybe-typeless
5107 ;; Whether we've found a declaration or a cast. We might know
5108 ;; this before we've found the type in it. It's 'ids if we've
5109 ;; found two consecutive identifiers (usually a sure sign, but
5110 ;; we should allow that in labels too), and t if we've found a
5111 ;; specifier keyword (a 100% sure sign).
5112 at-decl-or-cast
5113 ;; Set when we need to back up to parse this as a declaration
5114 ;; but not as a cast.
5115 backup-if-not-cast
5116 ;; For casts, the return position.
5117 cast-end
5118 ;; Save `c-record-type-identifiers' and
5119 ;; `c-record-ref-identifiers' since ranges are recorded
5120 ;; speculatively and should be thrown away if it turns out
5121 ;; that it isn't a declaration or cast.
5122 (save-rec-type-ids c-record-type-identifiers)
5123 (save-rec-ref-ids c-record-ref-identifiers))
5125 ;; Check for a type. Unknown symbols are treated as possible
5126 ;; types, but they could also be specifiers disguised through
5127 ;; macros like __INLINE__, so we recognize both types and known
5128 ;; specifiers after them too.
5129 (while
5130 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
5132 ;; Look for a specifier keyword clause.
5133 (when (looking-at c-prefix-spec-kwds-re)
5134 (setq kwd-sym (c-keyword-sym (match-string 1)))
5135 (save-excursion
5136 (c-forward-keyword-clause 1)
5137 (setq kwd-clause-end (point))))
5139 (when (setq found-type (c-forward-type))
5140 ;; Found a known or possible type or a prefix of a known type.
5142 (when at-type
5143 ;; Got two identifiers with nothing but whitespace
5144 ;; between them. That can only happen in declarations.
5145 (setq at-decl-or-cast 'ids)
5147 (when (eq at-type 'found)
5148 ;; If the previous identifier is a found type we
5149 ;; record it as a real one; it might be some sort of
5150 ;; alias for a prefix like "unsigned".
5151 (save-excursion
5152 (goto-char type-start)
5153 (let ((c-promote-possible-types t))
5154 (c-forward-type)))))
5156 (setq backup-at-type at-type
5157 backup-type-start type-start
5158 backup-id-start id-start
5159 at-type found-type
5160 type-start start
5161 id-start (point)
5162 ;; The previous ambiguous specifier/type turned out
5163 ;; to be a type since we've parsed another one after
5164 ;; it, so clear these backup flags.
5165 backup-at-type-decl nil
5166 backup-maybe-typeless nil))
5168 (if kwd-sym
5169 (progn
5170 ;; Handle known specifier keywords and
5171 ;; `c-decl-hangon-kwds' which can occur after known
5172 ;; types.
5174 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
5175 ;; It's a hang-on keyword that can occur anywhere.
5176 (progn
5177 (setq at-decl-or-cast t)
5178 (if at-type
5179 ;; Move the identifier start position if
5180 ;; we've passed a type.
5181 (setq id-start kwd-clause-end)
5182 ;; Otherwise treat this as a specifier and
5183 ;; move the fallback position.
5184 (setq start-pos kwd-clause-end))
5185 (goto-char kwd-clause-end))
5187 ;; It's an ordinary specifier so we know that
5188 ;; anything before this can't be the type.
5189 (setq backup-at-type nil
5190 start-pos kwd-clause-end)
5192 (if found-type
5193 ;; It's ambiguous whether this keyword is a
5194 ;; specifier or a type prefix, so set the backup
5195 ;; flags. (It's assumed that `c-forward-type'
5196 ;; moved further than `c-forward-keyword-clause'.)
5197 (progn
5198 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5199 (setq backup-at-type-decl t))
5200 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5201 (setq backup-maybe-typeless t)))
5203 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5204 (setq at-type-decl t))
5205 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5206 (setq maybe-typeless t))
5208 ;; Haven't matched a type so it's an umambiguous
5209 ;; specifier keyword and we know we're in a
5210 ;; declaration.
5211 (setq at-decl-or-cast t)
5213 (goto-char kwd-clause-end))))
5215 ;; If the type isn't known we continue so that we'll jump
5216 ;; over all specifiers and type identifiers. The reason
5217 ;; to do this for a known type prefix is to make things
5218 ;; like "unsigned INT16" work.
5219 (and found-type (not (eq found-type t))))))
5221 (cond
5222 ((eq at-type t)
5223 ;; If a known type was found, we still need to skip over any
5224 ;; hangon keyword clauses after it. Otherwise it has already
5225 ;; been done in the loop above.
5226 (while (looking-at c-decl-hangon-key)
5227 (c-forward-keyword-clause 1))
5228 (setq id-start (point)))
5230 ((eq at-type 'prefix)
5231 ;; A prefix type is itself a primitive type when it's not
5232 ;; followed by another type.
5233 (setq at-type t))
5235 ((not at-type)
5236 ;; Got no type but set things up to continue anyway to handle
5237 ;; the various cases when a declaration doesn't start with a
5238 ;; type.
5239 (setq id-start start-pos))
5241 ((and (eq at-type 'maybe)
5242 (c-major-mode-is 'c++-mode))
5243 ;; If it's C++ then check if the last "type" ends on the form
5244 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
5245 ;; (con|de)structor.
5246 (save-excursion
5247 (let (name end-2 end-1)
5248 (goto-char id-start)
5249 (c-backward-syntactic-ws)
5250 (setq end-2 (point))
5251 (when (and
5252 (c-simple-skip-symbol-backward)
5253 (progn
5254 (setq name
5255 (buffer-substring-no-properties (point) end-2))
5256 ;; Cheating in the handling of syntactic ws below.
5257 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
5258 (progn
5259 (setq end-1 (point))
5260 (c-simple-skip-symbol-backward))
5261 (>= (point) type-start)
5262 (equal (buffer-substring-no-properties (point) end-1)
5263 name))
5264 ;; It is a (con|de)structor name. In that case the
5265 ;; declaration is typeless so zap out any preceding
5266 ;; identifier(s) that we might have taken as types.
5267 (goto-char type-start)
5268 (setq at-type nil
5269 backup-at-type nil
5270 id-start type-start))))))
5272 ;; Check for and step over a type decl expression after the thing
5273 ;; that is or might be a type. This can't be skipped since we
5274 ;; need the correct end position of the declarator for
5275 ;; `max-type-decl-end-*'.
5276 (let ((start (point)) (paren-depth 0) pos
5277 ;; True if there's a non-open-paren match of
5278 ;; `c-type-decl-prefix-key'.
5279 got-prefix
5280 ;; True if the declarator is surrounded by a parenthesis pair.
5281 got-parens
5282 ;; True if there is an identifier in the declarator.
5283 got-identifier
5284 ;; True if there's a non-close-paren match of
5285 ;; `c-type-decl-suffix-key'.
5286 got-suffix
5287 ;; True if there's a prefix match outside the outermost
5288 ;; paren pair that surrounds the declarator.
5289 got-prefix-before-parens
5290 y ;; True if there's a suffix match outside the outermost
5291 ;; paren pair that surrounds the declarator. The value is
5292 ;; the position of the first suffix match.
5293 got-suffix-after-parens
5294 ;; True if we've parsed the type decl to a token that is
5295 ;; known to end declarations in this context.
5296 at-decl-end
5297 ;; The earlier values of `at-type' and `type-start' if we've
5298 ;; shifted the type backwards.
5299 identifier-type identifier-start
5300 ;; If `c-parse-and-markup-<>-arglists' is set we need to
5301 ;; turn it off during the name skipping below to avoid
5302 ;; getting `c-type' properties that might be bogus. That
5303 ;; can happen since we don't know if
5304 ;; `c-restricted-<>-arglists' will be correct inside the
5305 ;; arglist paren that gets entered.
5306 c-parse-and-markup-<>-arglists)
5308 (goto-char id-start)
5310 ;; Skip over type decl prefix operators. (Note similar code in
5311 ;; `c-font-lock-declarators'.)
5312 (while (and (looking-at c-type-decl-prefix-key)
5313 (if (and (c-major-mode-is 'c++-mode)
5314 (match-beginning 2))
5315 ;; If the second submatch matches in C++ then
5316 ;; we're looking at an identifier that's a
5317 ;; prefix only if it specifies a member pointer.
5318 (when (setq got-identifier (c-forward-name))
5319 (if (looking-at "\\(::\\)")
5320 ;; We only check for a trailing "::" and
5321 ;; let the "*" that should follow be
5322 ;; matched in the next round.
5323 (progn (setq got-identifier nil) t)
5324 ;; It turned out to be the real identifier,
5325 ;; so stop.
5326 nil))
5329 (if (eq (char-after) ?\()
5330 (progn
5331 (setq paren-depth (1+ paren-depth))
5332 (forward-char))
5333 (unless got-prefix-before-parens
5334 (setq got-prefix-before-parens (= paren-depth 0)))
5335 (setq got-prefix t)
5336 (goto-char (match-end 1)))
5337 (c-forward-syntactic-ws))
5339 (setq got-parens (> paren-depth 0))
5341 ;; Skip over an identifier.
5342 (or got-identifier
5343 (and (looking-at c-identifier-start)
5344 (setq got-identifier (c-forward-name))))
5346 ;; Skip over type decl suffix operators.
5347 (while (if (looking-at c-type-decl-suffix-key)
5349 (if (eq (char-after) ?\))
5350 (when (> paren-depth 0)
5351 (setq paren-depth (1- paren-depth))
5352 (forward-char)
5354 (when (if (save-match-data (looking-at "\\s\("))
5355 (c-safe (c-forward-sexp 1) t)
5356 (goto-char (match-end 1))
5358 (when (and (not got-suffix-after-parens)
5359 (= paren-depth 0))
5360 (setq got-suffix-after-parens (match-beginning 0)))
5361 (setq got-suffix t)))
5363 ;; No suffix matched. We might have matched the
5364 ;; identifier as a type and the open paren of a
5365 ;; function arglist as a type decl prefix. In that
5366 ;; case we should "backtrack": Reinterpret the last
5367 ;; type as the identifier, move out of the arglist and
5368 ;; continue searching for suffix operators.
5370 ;; Do this even if there's no preceding type, to cope
5371 ;; with old style function declarations in K&R C,
5372 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
5373 ;; style declarations. That isn't applicable in an
5374 ;; arglist context, though.
5375 (when (and (= paren-depth 1)
5376 (not got-prefix-before-parens)
5377 (not (eq at-type t))
5378 (or backup-at-type
5379 maybe-typeless
5380 backup-maybe-typeless
5381 (when c-recognize-typeless-decls
5382 (not context)))
5383 (setq pos (c-up-list-forward (point)))
5384 (eq (char-before pos) ?\)))
5385 (c-fdoc-shift-type-backward)
5386 (goto-char pos)
5389 (c-forward-syntactic-ws))
5391 (when (and (or maybe-typeless backup-maybe-typeless)
5392 (not got-identifier)
5393 (not got-prefix)
5394 at-type)
5395 ;; Have found no identifier but `c-typeless-decl-kwds' has
5396 ;; matched so we know we're inside a declaration. The
5397 ;; preceding type must be the identifier instead.
5398 (c-fdoc-shift-type-backward))
5400 (setq
5401 at-decl-or-cast
5402 (catch 'at-decl-or-cast
5404 (when (> paren-depth 0)
5405 ;; Encountered something inside parens that isn't matched by
5406 ;; the `c-type-decl-*' regexps, so it's not a type decl
5407 ;; expression. Try to skip out to the same paren depth to
5408 ;; not confuse the cast check below.
5409 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
5410 ;; If we've found a specifier keyword then it's a
5411 ;; declaration regardless.
5412 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
5414 (setq at-decl-end
5415 (looking-at (cond ((eq context '<>) "[,>]")
5416 (context "[,\)]")
5417 (t "[,;]"))))
5419 ;; Now we've collected info about various characteristics of
5420 ;; the construct we're looking at. Below follows a decision
5421 ;; tree based on that. It's ordered to check more certain
5422 ;; signs before less certain ones.
5424 (if got-identifier
5425 (progn
5427 (when (and (or at-type maybe-typeless)
5428 (not (or got-prefix got-parens)))
5429 ;; Got another identifier directly after the type, so it's a
5430 ;; declaration.
5431 (throw 'at-decl-or-cast t))
5433 (when (and got-parens
5434 (not got-prefix)
5435 (not got-suffix-after-parens)
5436 (or backup-at-type
5437 maybe-typeless
5438 backup-maybe-typeless))
5439 ;; Got a declaration of the form "foo bar (gnu);" where we've
5440 ;; recognized "bar" as the type and "gnu" as the declarator.
5441 ;; In this case it's however more likely that "bar" is the
5442 ;; declarator and "gnu" a function argument or initializer (if
5443 ;; `c-recognize-paren-inits' is set), since the parens around
5444 ;; "gnu" would be superfluous if it's a declarator. Shift the
5445 ;; type one step backward.
5446 (c-fdoc-shift-type-backward)))
5448 ;; Found no identifier.
5450 (if backup-at-type
5451 (progn
5453 (when (= (point) start)
5454 ;; Got a plain list of identifiers. If a colon follows it's
5455 ;; a valid label. Otherwise the last one probably is the
5456 ;; declared identifier and we should back up to the previous
5457 ;; type, providing it isn't a cast.
5458 (if (eq (char-after) ?:)
5459 ;; If we've found a specifier keyword then it's a
5460 ;; declaration regardless.
5461 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
5462 (setq backup-if-not-cast t)
5463 (throw 'at-decl-or-cast t)))
5465 (when (and got-suffix
5466 (not got-prefix)
5467 (not got-parens))
5468 ;; Got a plain list of identifiers followed by some suffix.
5469 ;; If this isn't a cast then the last identifier probably is
5470 ;; the declared one and we should back up to the previous
5471 ;; type.
5472 (setq backup-if-not-cast t)
5473 (throw 'at-decl-or-cast t)))
5475 (when (eq at-type t)
5476 ;; If the type is known we know that there can't be any
5477 ;; identifier somewhere else, and it's only in declarations in
5478 ;; e.g. function prototypes and in casts that the identifier may
5479 ;; be left out.
5480 (throw 'at-decl-or-cast t))
5482 (when (= (point) start)
5483 ;; Only got a single identifier (parsed as a type so far).
5484 (if (and
5485 ;; Check that the identifier isn't at the start of an
5486 ;; expression.
5487 at-decl-end
5488 (cond
5489 ((eq context 'decl)
5490 ;; Inside an arglist that contains declarations. If K&R
5491 ;; style declarations and parenthesis style initializers
5492 ;; aren't allowed then the single identifier must be a
5493 ;; type, else we require that it's known or found
5494 ;; (primitive types are handled above).
5495 (or (and (not c-recognize-knr-p)
5496 (not c-recognize-paren-inits))
5497 (memq at-type '(known found))))
5498 ((eq context '<>)
5499 ;; Inside a template arglist. Accept known and found
5500 ;; types; other identifiers could just as well be
5501 ;; constants in C++.
5502 (memq at-type '(known found)))))
5503 (throw 'at-decl-or-cast t)
5504 ;; Can't be a valid declaration or cast, but if we've found a
5505 ;; specifier it can't be anything else either, so treat it as
5506 ;; an invalid/unfinished declaration or cast.
5507 (throw 'at-decl-or-cast at-decl-or-cast))))
5509 (if (and got-parens
5510 (not got-prefix)
5511 (not context)
5512 (not (eq at-type t))
5513 (or backup-at-type
5514 maybe-typeless
5515 backup-maybe-typeless
5516 (when c-recognize-typeless-decls
5517 (or (not got-suffix)
5518 (not (looking-at
5519 c-after-suffixed-type-maybe-decl-key))))))
5520 ;; Got an empty paren pair and a preceding type that probably
5521 ;; really is the identifier. Shift the type backwards to make
5522 ;; the last one the identifier. This is analogous to the
5523 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
5524 ;; above.
5526 ;; Exception: In addition to the conditions in that
5527 ;; "backtracking" code, do not shift backward if we're not
5528 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
5529 ;; Since there's no preceding type, the shift would mean that
5530 ;; the declaration is typeless. But if the regexp doesn't match
5531 ;; then we will simply fall through in the tests below and not
5532 ;; recognize it at all, so it's better to try it as an abstract
5533 ;; declarator instead.
5534 (c-fdoc-shift-type-backward)
5536 ;; Still no identifier.
5538 (when (and got-prefix (or got-parens got-suffix))
5539 ;; Require `got-prefix' together with either `got-parens' or
5540 ;; `got-suffix' to recognize it as an abstract declarator:
5541 ;; `got-parens' only is probably an empty function call.
5542 ;; `got-suffix' only can build an ordinary expression together
5543 ;; with the preceding identifier which we've taken as a type.
5544 ;; We could actually accept on `got-prefix' only, but that can
5545 ;; easily occur temporarily while writing an expression so we
5546 ;; avoid that case anyway. We could do a better job if we knew
5547 ;; the point when the fontification was invoked.
5548 (throw 'at-decl-or-cast t))
5550 (when (and at-type
5551 (not got-prefix)
5552 (not got-parens)
5553 got-suffix-after-parens
5554 (eq (char-after got-suffix-after-parens) ?\())
5555 ;; Got a type, no declarator but a paren suffix. I.e. it's a
5556 ;; normal function call afterall (or perhaps a C++ style object
5557 ;; instantiation expression).
5558 (throw 'at-decl-or-cast nil))))
5560 (when at-decl-or-cast
5561 ;; By now we've located the type in the declaration that we know
5562 ;; we're in.
5563 (throw 'at-decl-or-cast t))
5565 (when (and got-identifier
5566 (not context)
5567 (looking-at c-after-suffixed-type-decl-key)
5568 (if (and got-parens
5569 (not got-prefix)
5570 (not got-suffix)
5571 (not (eq at-type t)))
5572 ;; Shift the type backward in the case that there's a
5573 ;; single identifier inside parens. That can only
5574 ;; occur in K&R style function declarations so it's
5575 ;; more likely that it really is a function call.
5576 ;; Therefore we only do this after
5577 ;; `c-after-suffixed-type-decl-key' has matched.
5578 (progn (c-fdoc-shift-type-backward) t)
5579 got-suffix-after-parens))
5580 ;; A declaration according to `c-after-suffixed-type-decl-key'.
5581 (throw 'at-decl-or-cast t))
5583 (when (and (or got-prefix (not got-parens))
5584 (memq at-type '(t known)))
5585 ;; It's a declaration if a known type precedes it and it can't be a
5586 ;; function call.
5587 (throw 'at-decl-or-cast t))
5589 ;; If we get here we can't tell if this is a type decl or a normal
5590 ;; expression by looking at it alone. (That's under the assumption
5591 ;; that normal expressions always can look like type decl expressions,
5592 ;; which isn't really true but the cases where it doesn't hold are so
5593 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
5594 ;; the effort to look for them.)
5596 (unless (or at-decl-end (looking-at "=[^=]"))
5597 ;; If this is a declaration it should end here or its initializer(*)
5598 ;; should start here, so check for allowed separation tokens. Note
5599 ;; that this rule doesn't work e.g. with a K&R arglist after a
5600 ;; function header.
5602 ;; *) Don't check for C++ style initializers using parens
5603 ;; since those already have been matched as suffixes.
5605 ;; If `at-decl-or-cast' is then we've found some other sign that
5606 ;; it's a declaration or cast, so then it's probably an
5607 ;; invalid/unfinished one.
5608 (throw 'at-decl-or-cast at-decl-or-cast))
5610 ;; Below are tests that only should be applied when we're certain to
5611 ;; not have parsed halfway through an expression.
5613 (when (memq at-type '(t known))
5614 ;; The expression starts with a known type so treat it as a
5615 ;; declaration.
5616 (throw 'at-decl-or-cast t))
5618 (when (and (c-major-mode-is 'c++-mode)
5619 ;; In C++ we check if the identifier is a known type, since
5620 ;; (con|de)structors use the class name as identifier.
5621 ;; We've always shifted over the identifier as a type and
5622 ;; then backed up again in this case.
5623 identifier-type
5624 (or (memq identifier-type '(found known))
5625 (and (eq (char-after identifier-start) ?~)
5626 ;; `at-type' probably won't be 'found for
5627 ;; destructors since the "~" is then part of the
5628 ;; type name being checked against the list of
5629 ;; known types, so do a check without that
5630 ;; operator.
5631 (or (save-excursion
5632 (goto-char (1+ identifier-start))
5633 (c-forward-syntactic-ws)
5634 (c-with-syntax-table
5635 c-identifier-syntax-table
5636 (looking-at c-known-type-key)))
5637 (save-excursion
5638 (goto-char (1+ identifier-start))
5639 ;; We have already parsed the type earlier,
5640 ;; so it'd be possible to cache the end
5641 ;; position instead of redoing it here, but
5642 ;; then we'd need to keep track of another
5643 ;; position everywhere.
5644 (c-check-type (point)
5645 (progn (c-forward-type)
5646 (point))))))))
5647 (throw 'at-decl-or-cast t))
5649 (if got-identifier
5650 (progn
5651 (when (and got-prefix-before-parens
5652 at-type
5653 (or at-decl-end (looking-at "=[^=]"))
5654 (not context)
5655 (not got-suffix))
5656 ;; Got something like "foo * bar;". Since we're not inside an
5657 ;; arglist it would be a meaningless expression because the
5658 ;; result isn't used. We therefore choose to recognize it as
5659 ;; a declaration. Do not allow a suffix since it could then
5660 ;; be a function call.
5661 (throw 'at-decl-or-cast t))
5663 (when (and (or got-suffix-after-parens
5664 (looking-at "=[^=]"))
5665 (eq at-type 'found)
5666 (not (eq context 'arglist)))
5667 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
5668 ;; be an odd expression or it could be a declaration. Treat
5669 ;; it as a declaration if "a" has been used as a type
5670 ;; somewhere else (if it's a known type we won't get here).
5671 (throw 'at-decl-or-cast t)))
5673 (when (and context
5674 (or got-prefix
5675 (and (eq context 'decl)
5676 (not c-recognize-paren-inits)
5677 (or got-parens got-suffix))))
5678 ;; Got a type followed by an abstract declarator. If `got-prefix'
5679 ;; is set it's something like "a *" without anything after it. If
5680 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
5681 ;; or similar, which we accept only if the context rules out
5682 ;; expressions.
5683 (throw 'at-decl-or-cast t)))
5685 ;; If we had a complete symbol table here (which rules out
5686 ;; `c-found-types') we should return t due to the disambiguation rule
5687 ;; (in at least C++) that anything that can be parsed as a declaration
5688 ;; is a declaration. Now we're being more defensive and prefer to
5689 ;; highlight things like "foo (bar);" as a declaration only if we're
5690 ;; inside an arglist that contains declarations.
5691 (eq context 'decl))))
5693 ;; The point is now after the type decl expression.
5695 (cond
5696 ;; Check for a cast.
5697 ((save-excursion
5698 (and
5699 c-cast-parens
5701 ;; Should be the first type/identifier in a cast paren.
5702 (> preceding-token-end (point-min))
5703 (memq (char-before preceding-token-end) c-cast-parens)
5705 ;; The closing paren should follow.
5706 (progn
5707 (c-forward-syntactic-ws)
5708 (looking-at "\\s\)"))
5710 ;; There should be a primary expression after it.
5711 (let (pos)
5712 (forward-char)
5713 (c-forward-syntactic-ws)
5714 (setq cast-end (point))
5715 (and (looking-at c-primary-expr-regexp)
5716 (progn
5717 (setq pos (match-end 0))
5719 ;; Check if the expression begins with a prefix keyword.
5720 (match-beginning 2)
5721 (if (match-beginning 1)
5722 ;; Expression begins with an ambiguous operator. Treat
5723 ;; it as a cast if it's a type decl or if we've
5724 ;; recognized the type somewhere else.
5725 (or at-decl-or-cast
5726 (memq at-type '(t known found)))
5727 ;; Unless it's a keyword, it's the beginning of a primary
5728 ;; expression.
5729 (not (looking-at c-keywords-regexp)))))
5730 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
5731 ;; that it matched a whole one so that we don't e.g. confuse
5732 ;; the operator '-' with '->'. It's ok if it matches further,
5733 ;; though, since it e.g. can match the float '.5' while the
5734 ;; operator regexp only matches '.'.
5735 (or (not (looking-at c-nonsymbol-token-regexp))
5736 (<= (match-end 0) pos))))
5738 ;; There should either be a cast before it or something that isn't an
5739 ;; identifier or close paren.
5740 (> preceding-token-end (point-min))
5741 (progn
5742 (goto-char (1- preceding-token-end))
5743 (or (eq (point) last-cast-end)
5744 (progn
5745 (c-backward-syntactic-ws)
5746 (if (< (skip-syntax-backward "w_") 0)
5747 ;; It's a symbol. Accept it only if it's one of the
5748 ;; keywords that can precede an expression (without
5749 ;; surrounding parens).
5750 (looking-at c-simple-stmt-key)
5751 (and
5752 ;; Check that it isn't a close paren (block close is ok,
5753 ;; though).
5754 (not (memq (char-before) '(?\) ?\])))
5755 ;; Check that it isn't a nonsymbol identifier.
5756 (not (c-on-identifier)))))))))
5758 ;; Handle the cast.
5759 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5760 (let ((c-promote-possible-types t))
5761 (goto-char type-start)
5762 (c-forward-type)))
5764 (goto-char cast-end)
5765 'cast)
5767 (at-decl-or-cast
5768 ;; We're at a declaration. Highlight the type and the following
5769 ;; declarators.
5771 (when backup-if-not-cast
5772 (c-fdoc-shift-type-backward t))
5774 (when (and (eq context 'decl) (looking-at ","))
5775 ;; Make sure to propagate the `c-decl-arg-start' property to
5776 ;; the next argument if it's set in this one, to cope with
5777 ;; interactive refontification.
5778 (c-put-c-type-property (point) 'c-decl-arg-start))
5780 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5781 (let ((c-promote-possible-types t))
5782 (save-excursion
5783 (goto-char type-start)
5784 (c-forward-type))))
5786 (cons id-start at-type-decl))
5789 ;; False alarm. Restore the recorded ranges.
5790 (setq c-record-type-identifiers save-rec-type-ids
5791 c-record-ref-identifiers save-rec-ref-ids)
5792 nil))))
5794 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
5795 ;; Assuming the point is at the beginning of a token, check if it
5796 ;; starts a label and if so move over it and return t, otherwise
5797 ;; don't move and return nil. The end of the label is taken to be
5798 ;; the end of the first submatch in `c-opt-extra-label-key' if it
5799 ;; matched, otherwise it's the colon. The point is directly after
5800 ;; the end on return. The terminating char is marked with
5801 ;; `c-decl-end' to improve recognition of the following declaration
5802 ;; or statement.
5804 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
5805 ;; label, if any, has been marked up like that.
5807 ;; If PRECEDING-TOKEN-END is given, it should be the first position
5808 ;; after the preceding token, i.e. on the other side of the
5809 ;; syntactic ws from the point. Use a value less than or equal to
5810 ;; (point-min) if the point is at the first token in (the visible
5811 ;; part of) the buffer.
5813 ;; The optional LIMIT limits the forward scan for the colon.
5815 ;; This function records the ranges of the label symbols on
5816 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
5817 ;; non-nil.
5819 ;; This function might do hidden buffer changes.
5821 (let ((start (point)))
5822 (cond
5823 ((looking-at c-label-kwds-regexp)
5824 (let ((kwd-end (match-end 1)))
5825 ;; Record only the keyword itself for fontification, since in
5826 ;; case labels the following is a constant expression and not
5827 ;; a label.
5828 (when c-record-type-identifiers
5829 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
5831 ;; Find the label end.
5832 (goto-char kwd-end)
5833 (if (and (c-syntactic-re-search-forward
5834 ;; Stop on chars that aren't allowed in expressions,
5835 ;; and on operator chars that would be meaningless
5836 ;; there. FIXME: This doesn't cope with ?: operators.
5837 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
5838 limit t t nil 1)
5839 (match-beginning 2))
5841 (progn
5842 (goto-char (match-beginning 2))
5843 (c-put-c-type-property (1- (point)) 'c-decl-end)
5846 ;; It's an unfinished label. We consider the keyword enough
5847 ;; to recognize it as a label, so that it gets fontified.
5848 ;; Leave the point at the end of it, but don't put any
5849 ;; `c-decl-end' marker.
5850 (goto-char kwd-end)
5851 t)))
5853 ((and c-opt-extra-label-key
5854 (looking-at c-opt-extra-label-key))
5855 ;; For a `c-opt-extra-label-key' match, we record the whole
5856 ;; thing for fontification. That's to get the leading '@' in
5857 ;; Objective-C protection labels fontified.
5858 (goto-char (match-end 1))
5859 (when c-record-type-identifiers
5860 (c-record-ref-id (cons (match-beginning 1) (point))))
5861 (c-put-c-type-property (1- (point)) 'c-decl-end)
5864 ((and c-recognize-colon-labels
5866 ;; A colon label must have something before the colon.
5867 (not (eq (char-after) ?:))
5869 ;; Check that we're not after a token that can't precede a label.
5871 ;; Trivially succeeds when there's no preceding token.
5872 (if preceding-token-end
5873 (<= preceding-token-end (point-min))
5874 (save-excursion
5875 (c-backward-syntactic-ws)
5876 (setq preceding-token-end (point))
5877 (bobp)))
5879 ;; Check if we're after a label, if we're after a closing
5880 ;; paren that belong to statement, and with
5881 ;; `c-label-prefix-re'. It's done in different order
5882 ;; depending on `assume-markup' since the checks have
5883 ;; different expensiveness.
5884 (if assume-markup
5886 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
5887 'c-decl-end)
5889 (save-excursion
5890 (goto-char (1- preceding-token-end))
5891 (c-beginning-of-current-token)
5892 (looking-at c-label-prefix-re))
5894 (and (eq (char-before preceding-token-end) ?\))
5895 (c-after-conditional)))
5898 (save-excursion
5899 (goto-char (1- preceding-token-end))
5900 (c-beginning-of-current-token)
5901 (looking-at c-label-prefix-re))
5903 (cond
5904 ((eq (char-before preceding-token-end) ?\))
5905 (c-after-conditional))
5907 ((eq (char-before preceding-token-end) ?:)
5908 ;; Might be after another label, so check it recursively.
5909 (save-excursion
5910 (goto-char (1- preceding-token-end))
5911 ;; Essentially the same as the
5912 ;; `c-syntactic-re-search-forward' regexp below.
5913 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
5914 (let ((pte (point))
5915 ;; If the caller turned on recording for us,
5916 ;; it shouldn't apply when we check the
5917 ;; preceding label.
5918 c-record-type-identifiers)
5919 (c-forward-syntactic-ws)
5920 (c-forward-label nil pte start))))))))
5922 ;; Check that the next nonsymbol token is ":". Allow '('
5923 ;; for the sake of macro arguments. FIXME: Should build
5924 ;; this regexp from the language constants.
5925 (c-syntactic-re-search-forward
5926 "[[:?;{=*/%&|,<>!@+-]" limit t t)
5927 (eq (char-before) ?:)
5928 (not (eq (char-after) ?:)))
5930 (save-restriction
5931 (narrow-to-region start (point))
5933 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
5934 (catch 'check-label
5935 (goto-char start)
5936 (while (progn
5937 (when (looking-at c-nonlabel-token-key)
5938 (goto-char start)
5939 (throw 'check-label nil))
5940 (and (c-safe (c-forward-sexp)
5941 (c-forward-syntactic-ws)
5943 (not (eobp)))))
5945 ;; Record the identifiers in the label for fontification, unless
5946 ;; it begins with `c-label-kwds' in which case the following
5947 ;; identifiers are part of a (constant) expression that
5948 ;; shouldn't be fontified.
5949 (when (and c-record-type-identifiers
5950 (progn (goto-char start)
5951 (not (looking-at c-label-kwds-regexp))))
5952 (while (c-syntactic-re-search-forward c-symbol-key nil t)
5953 (c-record-ref-id (cons (match-beginning 0)
5954 (match-end 0)))))
5956 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
5957 (goto-char (point-max))
5958 t)))
5961 ;; Not a label.
5962 (goto-char start)
5963 nil))))
5965 (defun c-forward-objc-directive ()
5966 ;; Assuming the point is at the beginning of a token, try to move
5967 ;; forward to the end of the Objective-C directive that starts
5968 ;; there. Return t if a directive was fully recognized, otherwise
5969 ;; the point is moved as far as one could be successfully parsed and
5970 ;; nil is returned.
5972 ;; This function records identifier ranges on
5973 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5974 ;; `c-record-type-identifiers' is non-nil.
5976 ;; This function might do hidden buffer changes.
5978 (let ((start (point))
5979 start-char
5980 (c-promote-possible-types t)
5981 ;; Turn off recognition of angle bracket arglists while parsing
5982 ;; types here since the protocol reference list might then be
5983 ;; considered part of the preceding name or superclass-name.
5984 c-recognize-<>-arglists)
5986 (if (or
5987 (when (looking-at
5988 (eval-when-compile
5989 (c-make-keywords-re t
5990 (append (c-lang-const c-protection-kwds objc)
5991 '("@end"))
5992 'objc-mode)))
5993 (goto-char (match-end 1))
5996 (and
5997 (looking-at
5998 (eval-when-compile
5999 (c-make-keywords-re t
6000 '("@interface" "@implementation" "@protocol")
6001 'objc-mode)))
6003 ;; Handle the name of the class itself.
6004 (progn
6005 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
6006 ; at EOB.
6007 (goto-char (match-end 0))
6008 (c-skip-ws-forward)
6009 (c-forward-type))
6011 (catch 'break
6012 ;; Look for ": superclass-name" or "( category-name )".
6013 (when (looking-at "[:\(]")
6014 (setq start-char (char-after))
6015 (forward-char)
6016 (c-forward-syntactic-ws)
6017 (unless (c-forward-type) (throw 'break nil))
6018 (when (eq start-char ?\()
6019 (unless (eq (char-after) ?\)) (throw 'break nil))
6020 (forward-char)
6021 (c-forward-syntactic-ws)))
6023 ;; Look for a protocol reference list.
6024 (if (eq (char-after) ?<)
6025 (let ((c-recognize-<>-arglists t)
6026 (c-parse-and-markup-<>-arglists t)
6027 c-restricted-<>-arglists)
6028 (c-forward-<>-arglist t))
6029 t))))
6031 (progn
6032 (c-backward-syntactic-ws)
6033 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
6034 (c-put-c-type-property (1- (point)) 'c-decl-end)
6037 (c-clear-c-type-property start (point) 'c-decl-end)
6038 nil)))
6040 (defun c-beginning-of-inheritance-list (&optional lim)
6041 ;; Go to the first non-whitespace after the colon that starts a
6042 ;; multiple inheritance introduction. Optional LIM is the farthest
6043 ;; back we should search.
6045 ;; This function might do hidden buffer changes.
6046 (c-with-syntax-table c++-template-syntax-table
6047 (c-backward-token-2 0 t lim)
6048 (while (and (or (looking-at c-symbol-start)
6049 (looking-at "[<,]\\|::"))
6050 (zerop (c-backward-token-2 1 t lim))))))
6052 (defun c-in-method-def-p ()
6053 ;; Return nil if we aren't in a method definition, otherwise the
6054 ;; position of the initial [+-].
6056 ;; This function might do hidden buffer changes.
6057 (save-excursion
6058 (beginning-of-line)
6059 (and c-opt-method-key
6060 (looking-at c-opt-method-key)
6061 (point))
6064 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
6065 (defun c-in-gcc-asm-p ()
6066 ;; Return non-nil if point is within a gcc \"asm\" block.
6068 ;; This should be called with point inside an argument list.
6070 ;; Only one level of enclosing parentheses is considered, so for
6071 ;; instance `nil' is returned when in a function call within an asm
6072 ;; operand.
6074 ;; This function might do hidden buffer changes.
6076 (and c-opt-asm-stmt-key
6077 (save-excursion
6078 (beginning-of-line)
6079 (backward-up-list 1)
6080 (c-beginning-of-statement-1 (point-min) nil t)
6081 (looking-at c-opt-asm-stmt-key))))
6083 (defun c-at-toplevel-p ()
6084 "Return a determination as to whether point is at the `top-level'.
6085 Being at the top-level means that point is either outside any
6086 enclosing block (such function definition), or only inside a class,
6087 namespace or other block that contains another declaration level.
6089 If point is not at the top-level (e.g. it is inside a method
6090 definition), then nil is returned. Otherwise, if point is at a
6091 top-level not enclosed within a class definition, t is returned.
6092 Otherwise, a 2-vector is returned where the zeroth element is the
6093 buffer position of the start of the class declaration, and the first
6094 element is the buffer position of the enclosing class's opening
6095 brace.
6097 Note that this function might do hidden buffer changes. See the
6098 comment at the start of cc-engine.el for more info."
6099 (let ((paren-state (c-parse-state)))
6100 (or (not (c-most-enclosing-brace paren-state))
6101 (c-search-uplist-for-classkey paren-state))))
6103 (defun c-just-after-func-arglist-p (&optional lim)
6104 ;; Return non-nil if the point is in the region after the argument
6105 ;; list of a function and its opening brace (or semicolon in case it
6106 ;; got no body). If there are K&R style argument declarations in
6107 ;; that region, the point has to be inside the first one for this
6108 ;; function to recognize it.
6110 ;; If successful, the point is moved to the first token after the
6111 ;; function header (see `c-forward-decl-or-cast-1' for details) and
6112 ;; the position of the opening paren of the function arglist is
6113 ;; returned.
6115 ;; The point is clobbered if not successful.
6117 ;; LIM is used as bound for backward buffer searches.
6119 ;; This function might do hidden buffer changes.
6121 (let ((beg (point)) end id-start)
6122 (and
6123 (eq (c-beginning-of-statement-1 lim) 'same)
6125 (not (or (c-major-mode-is 'objc-mode)
6126 (c-forward-objc-directive)))
6128 (setq id-start
6129 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
6130 (< id-start beg)
6132 ;; There should not be a '=' or ',' between beg and the
6133 ;; start of the declaration since that means we were in the
6134 ;; "expression part" of the declaration.
6135 (or (> (point) beg)
6136 (not (looking-at "[=,]")))
6138 (save-excursion
6139 ;; Check that there's an arglist paren in the
6140 ;; declaration.
6141 (goto-char id-start)
6142 (cond ((eq (char-after) ?\()
6143 ;; The declarator is a paren expression, so skip past it
6144 ;; so that we don't get stuck on that instead of the
6145 ;; function arglist.
6146 (c-forward-sexp))
6147 ((and c-opt-op-identitier-prefix
6148 (looking-at c-opt-op-identitier-prefix))
6149 ;; Don't trip up on "operator ()".
6150 (c-forward-token-2 2 t)))
6151 (and (< (point) beg)
6152 (c-syntactic-re-search-forward "(" beg t t)
6153 (1- (point)))))))
6155 (defun c-in-knr-argdecl (&optional lim)
6156 ;; Return the position of the first argument declaration if point is
6157 ;; inside a K&R style argument declaration list, nil otherwise.
6158 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
6159 ;; position that bounds the backward search for the argument list.
6161 ;; Note: A declaration level context is assumed; the test can return
6162 ;; false positives for statements.
6164 ;; This function might do hidden buffer changes.
6166 (save-excursion
6167 (save-restriction
6169 ;; Go back to the closest preceding normal parenthesis sexp. We
6170 ;; take that as the argument list in the function header. Then
6171 ;; check that it's followed by some symbol before the next ';'
6172 ;; or '{'. If it does, it's the header of the K&R argdecl we're
6173 ;; in.
6174 (if lim (narrow-to-region lim (c-point 'eol)))
6175 (let ((outside-macro (not (c-query-macro-start)))
6176 paren-end)
6178 (catch 'done
6179 (while (if (and (setq paren-end (c-down-list-backward (point)))
6180 (eq (char-after paren-end) ?\)))
6181 (progn
6182 (goto-char (1+ paren-end))
6183 (if outside-macro
6184 (c-beginning-of-macro)))
6185 (throw 'done nil))))
6187 (and (progn
6188 (c-forward-syntactic-ws)
6189 (looking-at "\\w\\|\\s_"))
6191 (save-excursion
6192 ;; The function header in a K&R declaration should only
6193 ;; contain identifiers separated by comma. It should
6194 ;; also contain at least one identifier since there
6195 ;; wouldn't be anything to declare in the K&R region
6196 ;; otherwise.
6197 (when (c-go-up-list-backward paren-end)
6198 (forward-char)
6199 (catch 'knr-ok
6200 (while t
6201 (c-forward-syntactic-ws)
6202 (if (or (looking-at c-known-type-key)
6203 (looking-at c-keywords-regexp))
6204 (throw 'knr-ok nil))
6205 (c-forward-token-2)
6206 (if (eq (char-after) ?,)
6207 (forward-char)
6208 (throw 'knr-ok (and (eq (char-after) ?\))
6209 (= (point) paren-end))))))))
6211 (save-excursion
6212 ;; If it's a K&R declaration then we're now at the
6213 ;; beginning of the function arglist. Check that there
6214 ;; isn't a '=' before it in this statement since that
6215 ;; means it some kind of initialization instead.
6216 (c-syntactic-skip-backward "^;=}{")
6217 (not (eq (char-before) ?=)))
6219 (point))))))
6221 (defun c-skip-conditional ()
6222 ;; skip forward over conditional at point, including any predicate
6223 ;; statements in parentheses. No error checking is performed.
6225 ;; This function might do hidden buffer changes.
6226 (c-forward-sexp (cond
6227 ;; else if()
6228 ((looking-at (concat "\\<else"
6229 "\\([ \t\n]\\|\\\\\n\\)+"
6230 "if\\>\\([^_]\\|$\\)"))
6232 ;; do, else, try, finally
6233 ((looking-at (concat "\\<\\("
6234 "do\\|else\\|try\\|finally"
6235 "\\)\\>\\([^_]\\|$\\)"))
6237 ;; for, if, while, switch, catch, synchronized, foreach
6238 (t 2))))
6240 (defun c-after-conditional (&optional lim)
6241 ;; If looking at the token after a conditional then return the
6242 ;; position of its start, otherwise return nil.
6244 ;; This function might do hidden buffer changes.
6245 (save-excursion
6246 (and (zerop (c-backward-token-2 1 t lim))
6247 (or (looking-at c-block-stmt-1-key)
6248 (and (eq (char-after) ?\()
6249 (zerop (c-backward-token-2 1 t lim))
6250 (looking-at c-block-stmt-2-key)))
6251 (point))))
6253 (defun c-after-special-operator-id (&optional lim)
6254 ;; If the point is after an operator identifier that isn't handled
6255 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
6256 ;; position of the start of that identifier is returned. nil is
6257 ;; returned otherwise. The point may be anywhere in the syntactic
6258 ;; whitespace after the last token of the operator identifier.
6260 ;; This function might do hidden buffer changes.
6261 (save-excursion
6262 (and c-overloadable-operators-regexp
6263 (zerop (c-backward-token-2 1 nil lim))
6264 (looking-at c-overloadable-operators-regexp)
6265 (or (not c-opt-op-identitier-prefix)
6266 (and
6267 (zerop (c-backward-token-2 1 nil lim))
6268 (looking-at c-opt-op-identitier-prefix)))
6269 (point))))
6271 (defsubst c-backward-to-block-anchor (&optional lim)
6272 ;; Assuming point is at a brace that opens a statement block of some
6273 ;; kind, move to the proper anchor point for that block. It might
6274 ;; need to be adjusted further by c-add-stmt-syntax, but the
6275 ;; position at return is suitable as start position for that
6276 ;; function.
6278 ;; This function might do hidden buffer changes.
6279 (unless (= (point) (c-point 'boi))
6280 (let ((start (c-after-conditional lim)))
6281 (if start
6282 (goto-char start)))))
6284 (defsubst c-backward-to-decl-anchor (&optional lim)
6285 ;; Assuming point is at a brace that opens the block of a top level
6286 ;; declaration of some kind, move to the proper anchor point for
6287 ;; that block.
6289 ;; This function might do hidden buffer changes.
6290 (unless (= (point) (c-point 'boi))
6291 (c-beginning-of-statement-1 lim)))
6293 (defun c-search-decl-header-end ()
6294 ;; Search forward for the end of the "header" of the current
6295 ;; declaration. That's the position where the definition body
6296 ;; starts, or the first variable initializer, or the ending
6297 ;; semicolon. I.e. search forward for the closest following
6298 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
6299 ;; _after_ the first found token, or at point-max if none is found.
6301 ;; This function might do hidden buffer changes.
6303 (let ((base (point)))
6304 (if (c-major-mode-is 'c++-mode)
6306 ;; In C++ we need to take special care to handle operator
6307 ;; tokens and those pesky template brackets.
6308 (while (and
6309 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
6311 (c-end-of-current-token base)
6312 ;; Handle operator identifiers, i.e. ignore any
6313 ;; operator token preceded by "operator".
6314 (save-excursion
6315 (and (c-safe (c-backward-sexp) t)
6316 (looking-at c-opt-op-identitier-prefix)))
6317 (and (eq (char-before) ?<)
6318 (c-with-syntax-table c++-template-syntax-table
6319 (if (c-safe (goto-char (c-up-list-forward (point))))
6321 (goto-char (point-max))
6322 nil)))))
6323 (setq base (point)))
6325 (while (and
6326 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
6327 (c-end-of-current-token base))
6328 (setq base (point))))))
6330 (defun c-beginning-of-decl-1 (&optional lim)
6331 ;; Go to the beginning of the current declaration, or the beginning
6332 ;; of the previous one if already at the start of it. Point won't
6333 ;; be moved out of any surrounding paren. Return a cons cell of the
6334 ;; form (MOVE . KNR-POS). MOVE is like the return value from
6335 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
6336 ;; style argument declarations (and they are to be recognized) then
6337 ;; KNR-POS is set to the start of the first such argument
6338 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
6339 ;; position that bounds the backward search.
6341 ;; NB: Cases where the declaration continues after the block, as in
6342 ;; "struct foo { ... } bar;", are currently recognized as two
6343 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
6345 ;; This function might do hidden buffer changes.
6346 (catch 'return
6347 (let* ((start (point))
6348 (last-stmt-start (point))
6349 (move (c-beginning-of-statement-1 lim nil t)))
6351 ;; `c-beginning-of-statement-1' stops at a block start, but we
6352 ;; want to continue if the block doesn't begin a top level
6353 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
6354 ;; or an open paren.
6355 (let ((beg (point)) tentative-move)
6356 (while (and
6357 ;; Must check with c-opt-method-key in ObjC mode.
6358 (not (and c-opt-method-key
6359 (looking-at c-opt-method-key)))
6360 (/= last-stmt-start (point))
6361 (progn
6362 (c-backward-syntactic-ws lim)
6363 (not (memq (char-before) '(?\; ?} ?: nil))))
6364 (save-excursion
6365 (backward-char)
6366 (not (looking-at "\\s(")))
6367 ;; Check that we don't move from the first thing in a
6368 ;; macro to its header.
6369 (not (eq (setq tentative-move
6370 (c-beginning-of-statement-1 lim nil t))
6371 'macro)))
6372 (setq last-stmt-start beg
6373 beg (point)
6374 move tentative-move))
6375 (goto-char beg))
6377 (when c-recognize-knr-p
6378 (let ((fallback-pos (point)) knr-argdecl-start)
6379 ;; Handle K&R argdecls. Back up after the "statement" jumped
6380 ;; over by `c-beginning-of-statement-1', unless it was the
6381 ;; function body, in which case we're sitting on the opening
6382 ;; brace now. Then test if we're in a K&R argdecl region and
6383 ;; that we started at the other side of the first argdecl in
6384 ;; it.
6385 (unless (eq (char-after) ?{)
6386 (goto-char last-stmt-start))
6387 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
6388 (< knr-argdecl-start start)
6389 (progn
6390 (goto-char knr-argdecl-start)
6391 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
6392 (throw 'return
6393 (cons (if (eq (char-after fallback-pos) ?{)
6394 'previous
6395 'same)
6396 knr-argdecl-start))
6397 (goto-char fallback-pos))))
6399 ;; `c-beginning-of-statement-1' counts each brace block as a
6400 ;; separate statement, so the result will be 'previous if we've
6401 ;; moved over any. If they were brace list initializers we might
6402 ;; not have moved over a declaration boundary though, so change it
6403 ;; to 'same if we've moved past a '=' before '{', but not ';'.
6404 ;; (This ought to be integrated into `c-beginning-of-statement-1',
6405 ;; so we avoid this extra pass which potentially can search over a
6406 ;; large amount of text.)
6407 (if (and (eq move 'previous)
6408 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
6409 c++-template-syntax-table
6410 (syntax-table))
6411 (save-excursion
6412 (and (c-syntactic-re-search-forward "[;={]" start t t t)
6413 (eq (char-before) ?=)
6414 (c-syntactic-re-search-forward "[;{]" start t t)
6415 (eq (char-before) ?{)
6416 (c-safe (goto-char (c-up-list-forward (point))) t)
6417 (not (c-syntactic-re-search-forward ";" start t t))))))
6418 (cons 'same nil)
6419 (cons move nil)))))
6421 (defun c-end-of-decl-1 ()
6422 ;; Assuming point is at the start of a declaration (as detected by
6423 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
6424 ;; `c-beginning-of-decl-1', this function handles the case when a
6425 ;; block is followed by identifiers in e.g. struct declarations in C
6426 ;; or C++. If a proper end was found then t is returned, otherwise
6427 ;; point is moved as far as possible within the current sexp and nil
6428 ;; is returned. This function doesn't handle macros; use
6429 ;; `c-end-of-macro' instead in those cases.
6431 ;; This function might do hidden buffer changes.
6432 (let ((start (point))
6433 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
6434 c++-template-syntax-table
6435 (syntax-table))))
6436 (catch 'return
6437 (c-search-decl-header-end)
6439 (when (and c-recognize-knr-p
6440 (eq (char-before) ?\;)
6441 (c-in-knr-argdecl start))
6442 ;; Stopped at the ';' in a K&R argdecl section which is
6443 ;; detected using the same criteria as in
6444 ;; `c-beginning-of-decl-1'. Move to the following block
6445 ;; start.
6446 (c-syntactic-re-search-forward "{" nil 'move t))
6448 (when (eq (char-before) ?{)
6449 ;; Encountered a block in the declaration. Jump over it.
6450 (condition-case nil
6451 (goto-char (c-up-list-forward (point)))
6452 (error (goto-char (point-max))
6453 (throw 'return nil)))
6454 (if (or (not c-opt-block-decls-with-vars-key)
6455 (save-excursion
6456 (c-with-syntax-table decl-syntax-table
6457 (let ((lim (point)))
6458 (goto-char start)
6459 (not (and
6460 ;; Check for `c-opt-block-decls-with-vars-key'
6461 ;; before the first paren.
6462 (c-syntactic-re-search-forward
6463 (concat "[;=\(\[{]\\|\\("
6464 c-opt-block-decls-with-vars-key
6465 "\\)")
6466 lim t t t)
6467 (match-beginning 1)
6468 (not (eq (char-before) ?_))
6469 ;; Check that the first following paren is
6470 ;; the block.
6471 (c-syntactic-re-search-forward "[;=\(\[{]"
6472 lim t t t)
6473 (eq (char-before) ?{)))))))
6474 ;; The declaration doesn't have any of the
6475 ;; `c-opt-block-decls-with-vars' keywords in the
6476 ;; beginning, so it ends here at the end of the block.
6477 (throw 'return t)))
6479 (c-with-syntax-table decl-syntax-table
6480 (while (progn
6481 (if (eq (char-before) ?\;)
6482 (throw 'return t))
6483 (c-syntactic-re-search-forward ";" nil 'move t))))
6484 nil)))
6486 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
6487 ;; Assuming the point is at an open brace, check if it starts a
6488 ;; block that contains another declaration level, i.e. that isn't a
6489 ;; statement block or a brace list, and if so return non-nil.
6491 ;; If the check is successful, the return value is the start of the
6492 ;; keyword that tells what kind of construct it is, i.e. typically
6493 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
6494 ;; the point will be at the start of the construct, before any
6495 ;; leading specifiers, otherwise it's at the returned position.
6497 ;; The point is clobbered if the check is unsuccessful.
6499 ;; CONTAINING-SEXP is the position of the open of the surrounding
6500 ;; paren, or nil if none.
6502 ;; The optional LIMIT limits the backward search for the start of
6503 ;; the construct. It's assumed to be at a syntactically relevant
6504 ;; position.
6506 ;; If any template arglists are found in the searched region before
6507 ;; the open brace, they get marked with paren syntax.
6509 ;; This function might do hidden buffer changes.
6511 (let ((open-brace (point)) kwd-start first-specifier-pos)
6512 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6514 (when (and c-recognize-<>-arglists
6515 (eq (char-before) ?>))
6516 ;; Could be at the end of a template arglist.
6517 (let ((c-parse-and-markup-<>-arglists t)
6518 (c-disallow-comma-in-<>-arglists
6519 (and containing-sexp
6520 (not (eq (char-after containing-sexp) ?{)))))
6521 (while (and
6522 (c-backward-<>-arglist nil limit)
6523 (progn
6524 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6525 (eq (char-before) ?>))))))
6527 ;; Note: Can't get bogus hits inside template arglists below since they
6528 ;; have gotten paren syntax above.
6529 (when (and
6530 ;; If `goto-start' is set we begin by searching for the
6531 ;; first possible position of a leading specifier list.
6532 ;; The `c-decl-block-key' search continues from there since
6533 ;; we know it can't match earlier.
6534 (if goto-start
6535 (when (c-syntactic-re-search-forward c-symbol-start
6536 open-brace t t)
6537 (goto-char (setq first-specifier-pos (match-beginning 0)))
6541 (cond
6542 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
6543 (goto-char (setq kwd-start (match-beginning 0)))
6546 ;; Found a keyword that can't be a type?
6547 (match-beginning 1)
6549 ;; Can be a type too, in which case it's the return type of a
6550 ;; function (under the assumption that no declaration level
6551 ;; block construct starts with a type).
6552 (not (c-forward-type))
6554 ;; Jumped over a type, but it could be a declaration keyword
6555 ;; followed by the declared identifier that we've jumped over
6556 ;; instead (e.g. in "class Foo {"). If it indeed is a type
6557 ;; then we should be at the declarator now, so check for a
6558 ;; valid declarator start.
6560 ;; Note: This doesn't cope with the case when a declared
6561 ;; identifier is followed by e.g. '(' in a language where '('
6562 ;; also might be part of a declarator expression. Currently
6563 ;; there's no such language.
6564 (not (or (looking-at c-symbol-start)
6565 (looking-at c-type-decl-prefix-key)))))
6567 ;; In Pike a list of modifiers may be followed by a brace
6568 ;; to make them apply to many identifiers. Note that the
6569 ;; match data will be empty on return in this case.
6570 ((and (c-major-mode-is 'pike-mode)
6571 (progn
6572 (goto-char open-brace)
6573 (= (c-backward-token-2) 0))
6574 (looking-at c-specifier-key)
6575 ;; Use this variant to avoid yet another special regexp.
6576 (c-keyword-member (c-keyword-sym (match-string 1))
6577 'c-modifier-kwds))
6578 (setq kwd-start (point))
6579 t)))
6581 ;; Got a match.
6583 (if goto-start
6584 ;; Back up over any preceding specifiers and their clauses
6585 ;; by going forward from `first-specifier-pos', which is the
6586 ;; earliest possible position where the specifier list can
6587 ;; start.
6588 (progn
6589 (goto-char first-specifier-pos)
6591 (while (< (point) kwd-start)
6592 (if (looking-at c-symbol-key)
6593 ;; Accept any plain symbol token on the ground that
6594 ;; it's a specifier masked through a macro (just
6595 ;; like `c-forward-decl-or-cast-1' skip forward over
6596 ;; such tokens).
6598 ;; Could be more restrictive wrt invalid keywords,
6599 ;; but that'd only occur in invalid code so there's
6600 ;; no use spending effort on it.
6601 (let ((end (match-end 0)))
6602 (unless (c-forward-keyword-clause 0)
6603 (goto-char end)
6604 (c-forward-syntactic-ws)))
6606 ;; Can't parse a declaration preamble and is still
6607 ;; before `kwd-start'. That means `first-specifier-pos'
6608 ;; was in some earlier construct. Search again.
6609 (if (c-syntactic-re-search-forward c-symbol-start
6610 kwd-start 'move t)
6611 (goto-char (setq first-specifier-pos (match-beginning 0)))
6612 ;; Got no preamble before the block declaration keyword.
6613 (setq first-specifier-pos kwd-start))))
6615 (goto-char first-specifier-pos))
6616 (goto-char kwd-start))
6618 kwd-start)))
6620 (defun c-search-uplist-for-classkey (paren-state)
6621 ;; Check if the closest containing paren sexp is a declaration
6622 ;; block, returning a 2 element vector in that case. Aref 0
6623 ;; contains the bufpos at boi of the class key line, and aref 1
6624 ;; contains the bufpos of the open brace. This function is an
6625 ;; obsolete wrapper for `c-looking-at-decl-block'.
6627 ;; This function might do hidden buffer changes.
6628 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
6629 (when open-paren-pos
6630 (save-excursion
6631 (goto-char open-paren-pos)
6632 (when (and (eq (char-after) ?{)
6633 (c-looking-at-decl-block
6634 (c-safe-position open-paren-pos paren-state)
6635 nil))
6636 (back-to-indentation)
6637 (vector (point) open-paren-pos))))))
6639 (defun c-inside-bracelist-p (containing-sexp paren-state)
6640 ;; return the buffer position of the beginning of the brace list
6641 ;; statement if we're inside a brace list, otherwise return nil.
6642 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
6643 ;; paren. PAREN-STATE is the remainder of the state of enclosing
6644 ;; braces
6646 ;; N.B.: This algorithm can potentially get confused by cpp macros
6647 ;; placed in inconvenient locations. It's a trade-off we make for
6648 ;; speed.
6650 ;; This function might do hidden buffer changes.
6652 ;; This will pick up brace list declarations.
6653 (c-safe
6654 (save-excursion
6655 (goto-char containing-sexp)
6656 (c-forward-sexp -1)
6657 (let (bracepos)
6658 (if (and (or (looking-at c-brace-list-key)
6659 (progn (c-forward-sexp -1)
6660 (looking-at c-brace-list-key)))
6661 (setq bracepos (c-down-list-forward (point)))
6662 (not (c-crosses-statement-barrier-p (point)
6663 (- bracepos 2))))
6664 (point)))))
6665 ;; this will pick up array/aggregate init lists, even if they are nested.
6666 (save-excursion
6667 (let ((class-key
6668 ;; Pike can have class definitions anywhere, so we must
6669 ;; check for the class key here.
6670 (and (c-major-mode-is 'pike-mode)
6671 c-decl-block-key))
6672 bufpos braceassignp lim next-containing)
6673 (while (and (not bufpos)
6674 containing-sexp)
6675 (when paren-state
6676 (if (consp (car paren-state))
6677 (setq lim (cdr (car paren-state))
6678 paren-state (cdr paren-state))
6679 (setq lim (car paren-state)))
6680 (when paren-state
6681 (setq next-containing (car paren-state)
6682 paren-state (cdr paren-state))))
6683 (goto-char containing-sexp)
6684 (if (c-looking-at-inexpr-block next-containing next-containing)
6685 ;; We're in an in-expression block of some kind. Do not
6686 ;; check nesting. We deliberately set the limit to the
6687 ;; containing sexp, so that c-looking-at-inexpr-block
6688 ;; doesn't check for an identifier before it.
6689 (setq containing-sexp nil)
6690 ;; see if the open brace is preceded by = or [...] in
6691 ;; this statement, but watch out for operator=
6692 (setq braceassignp 'dontknow)
6693 (c-backward-token-2 1 t lim)
6694 ;; Checks to do only on the first sexp before the brace.
6695 (when (and c-opt-inexpr-brace-list-key
6696 (eq (char-after) ?\[))
6697 ;; In Java, an initialization brace list may follow
6698 ;; directly after "new Foo[]", so check for a "new"
6699 ;; earlier.
6700 (while (eq braceassignp 'dontknow)
6701 (setq braceassignp
6702 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
6703 ((looking-at c-opt-inexpr-brace-list-key) t)
6704 ((looking-at "\\sw\\|\\s_\\|[.[]")
6705 ;; Carry on looking if this is an
6706 ;; identifier (may contain "." in Java)
6707 ;; or another "[]" sexp.
6708 'dontknow)
6709 (t nil)))))
6710 ;; Checks to do on all sexps before the brace, up to the
6711 ;; beginning of the statement.
6712 (while (eq braceassignp 'dontknow)
6713 (cond ((eq (char-after) ?\;)
6714 (setq braceassignp nil))
6715 ((and class-key
6716 (looking-at class-key))
6717 (setq braceassignp nil))
6718 ((eq (char-after) ?=)
6719 ;; We've seen a =, but must check earlier tokens so
6720 ;; that it isn't something that should be ignored.
6721 (setq braceassignp 'maybe)
6722 (while (and (eq braceassignp 'maybe)
6723 (zerop (c-backward-token-2 1 t lim)))
6724 (setq braceassignp
6725 (cond
6726 ;; Check for operator =
6727 ((and c-opt-op-identitier-prefix
6728 (looking-at c-opt-op-identitier-prefix))
6729 nil)
6730 ;; Check for `<opchar>= in Pike.
6731 ((and (c-major-mode-is 'pike-mode)
6732 (or (eq (char-after) ?`)
6733 ;; Special case for Pikes
6734 ;; `[]=, since '[' is not in
6735 ;; the punctuation class.
6736 (and (eq (char-after) ?\[)
6737 (eq (char-before) ?`))))
6738 nil)
6739 ((looking-at "\\s.") 'maybe)
6740 ;; make sure we're not in a C++ template
6741 ;; argument assignment
6742 ((and
6743 (c-major-mode-is 'c++-mode)
6744 (save-excursion
6745 (let ((here (point))
6746 (pos< (progn
6747 (skip-chars-backward "^<>")
6748 (point))))
6749 (and (eq (char-before) ?<)
6750 (not (c-crosses-statement-barrier-p
6751 pos< here))
6752 (not (c-in-literal))
6753 ))))
6754 nil)
6755 (t t))))))
6756 (if (and (eq braceassignp 'dontknow)
6757 (/= (c-backward-token-2 1 t lim) 0))
6758 (setq braceassignp nil)))
6759 (if (not braceassignp)
6760 (if (eq (char-after) ?\;)
6761 ;; Brace lists can't contain a semicolon, so we're done.
6762 (setq containing-sexp nil)
6763 ;; Go up one level.
6764 (setq containing-sexp next-containing
6765 lim nil
6766 next-containing nil))
6767 ;; we've hit the beginning of the aggregate list
6768 (c-beginning-of-statement-1
6769 (c-most-enclosing-brace paren-state))
6770 (setq bufpos (point))))
6772 bufpos))
6775 (defun c-looking-at-special-brace-list (&optional lim)
6776 ;; If we're looking at the start of a pike-style list, ie `({ })',
6777 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
6778 ;; positions and its entry in c-special-brace-lists is returned, nil
6779 ;; otherwise. The ending position is nil if the list is still open.
6780 ;; LIM is the limit for forward search. The point may either be at
6781 ;; the `(' or at the following paren character. Tries to check the
6782 ;; matching closer, but assumes it's correct if no balanced paren is
6783 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
6784 ;; a special brace list).
6786 ;; This function might do hidden buffer changes.
6787 (if c-special-brace-lists
6788 (condition-case ()
6789 (save-excursion
6790 (let ((beg (point))
6791 inner-beg end type)
6792 (c-forward-syntactic-ws)
6793 (if (eq (char-after) ?\()
6794 (progn
6795 (forward-char 1)
6796 (c-forward-syntactic-ws)
6797 (setq inner-beg (point))
6798 (setq type (assq (char-after) c-special-brace-lists)))
6799 (if (setq type (assq (char-after) c-special-brace-lists))
6800 (progn
6801 (setq inner-beg (point))
6802 (c-backward-syntactic-ws)
6803 (forward-char -1)
6804 (setq beg (if (eq (char-after) ?\()
6805 (point)
6806 nil)))))
6807 (if (and beg type)
6808 (if (and (c-safe
6809 (goto-char beg)
6810 (c-forward-sexp 1)
6811 (setq end (point))
6812 (= (char-before) ?\)))
6813 (c-safe
6814 (goto-char inner-beg)
6815 (if (looking-at "\\s(")
6816 ;; Check balancing of the inner paren
6817 ;; below.
6818 (progn
6819 (c-forward-sexp 1)
6821 ;; If the inner char isn't a paren then
6822 ;; we can't check balancing, so just
6823 ;; check the char before the outer
6824 ;; closing paren.
6825 (goto-char end)
6826 (backward-char)
6827 (c-backward-syntactic-ws)
6828 (= (char-before) (cdr type)))))
6829 (if (or (/= (char-syntax (char-before)) ?\))
6830 (= (progn
6831 (c-forward-syntactic-ws)
6832 (point))
6833 (1- end)))
6834 (cons (cons beg end) type))
6835 (cons (list beg) type)))))
6836 (error nil))))
6838 (defun c-looking-at-bos (&optional lim)
6839 ;; Return non-nil if between two statements or declarations, assuming
6840 ;; point is not inside a literal or comment.
6842 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
6843 ;; are recommended instead.
6845 ;; This function might do hidden buffer changes.
6846 (c-at-statement-start-p))
6847 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p)
6849 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
6850 ;; Return non-nil if we're looking at the beginning of a block
6851 ;; inside an expression. The value returned is actually a cons of
6852 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
6853 ;; position of the beginning of the construct.
6855 ;; LIM limits the backward search. CONTAINING-SEXP is the start
6856 ;; position of the closest containing list. If it's nil, the
6857 ;; containing paren isn't used to decide whether we're inside an
6858 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
6859 ;; needs to be farther back.
6861 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
6862 ;; brace block might be done. It should only be used when the
6863 ;; construct can be assumed to be complete, i.e. when the original
6864 ;; starting position was further down than that.
6866 ;; This function might do hidden buffer changes.
6868 (save-excursion
6869 (let ((res 'maybe) passed-paren
6870 (closest-lim (or containing-sexp lim (point-min)))
6871 ;; Look at the character after point only as a last resort
6872 ;; when we can't disambiguate.
6873 (block-follows (and (eq (char-after) ?{) (point))))
6875 (while (and (eq res 'maybe)
6876 (progn (c-backward-syntactic-ws)
6877 (> (point) closest-lim))
6878 (not (bobp))
6879 (progn (backward-char)
6880 (looking-at "[\]\).]\\|\\w\\|\\s_"))
6881 (c-safe (forward-char)
6882 (goto-char (scan-sexps (point) -1))))
6884 (setq res
6885 (if (looking-at c-keywords-regexp)
6886 (let ((kw-sym (c-keyword-sym (match-string 1))))
6887 (cond
6888 ((and block-follows
6889 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
6890 (and (not (eq passed-paren ?\[))
6891 (or (not (looking-at c-class-key))
6892 ;; If the class definition is at the start of
6893 ;; a statement, we don't consider it an
6894 ;; in-expression class.
6895 (let ((prev (point)))
6896 (while (and
6897 (= (c-backward-token-2 1 nil closest-lim) 0)
6898 (eq (char-syntax (char-after)) ?w))
6899 (setq prev (point)))
6900 (goto-char prev)
6901 (not (c-at-statement-start-p)))
6902 ;; Also, in Pike we treat it as an
6903 ;; in-expression class if it's used in an
6904 ;; object clone expression.
6905 (save-excursion
6906 (and check-at-end
6907 (c-major-mode-is 'pike-mode)
6908 (progn (goto-char block-follows)
6909 (zerop (c-forward-token-2 1 t)))
6910 (eq (char-after) ?\())))
6911 (cons 'inexpr-class (point))))
6912 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
6913 (when (not passed-paren)
6914 (cons 'inexpr-statement (point))))
6915 ((c-keyword-member kw-sym 'c-lambda-kwds)
6916 (when (or (not passed-paren)
6917 (eq passed-paren ?\())
6918 (cons 'inlambda (point))))
6919 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
6920 nil)
6922 'maybe)))
6924 (if (looking-at "\\s(")
6925 (if passed-paren
6926 (if (and (eq passed-paren ?\[)
6927 (eq (char-after) ?\[))
6928 ;; Accept several square bracket sexps for
6929 ;; Java array initializations.
6930 'maybe)
6931 (setq passed-paren (char-after))
6932 'maybe)
6933 'maybe))))
6935 (if (eq res 'maybe)
6936 (when (and c-recognize-paren-inexpr-blocks
6937 block-follows
6938 containing-sexp
6939 (eq (char-after containing-sexp) ?\())
6940 (goto-char containing-sexp)
6941 (if (or (save-excursion
6942 (c-backward-syntactic-ws lim)
6943 (and (> (point) (or lim (point-min)))
6944 (c-on-identifier)))
6945 (and c-special-brace-lists
6946 (c-looking-at-special-brace-list)))
6948 (cons 'inexpr-statement (point))))
6950 res))))
6952 (defun c-looking-at-inexpr-block-backward (paren-state)
6953 ;; Returns non-nil if we're looking at the end of an in-expression
6954 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
6955 ;; PAREN-STATE is the paren state relevant at the current position.
6957 ;; This function might do hidden buffer changes.
6958 (save-excursion
6959 ;; We currently only recognize a block.
6960 (let ((here (point))
6961 (elem (car-safe paren-state))
6962 containing-sexp)
6963 (when (and (consp elem)
6964 (progn (goto-char (cdr elem))
6965 (c-forward-syntactic-ws here)
6966 (= (point) here)))
6967 (goto-char (car elem))
6968 (if (setq paren-state (cdr paren-state))
6969 (setq containing-sexp (car-safe paren-state)))
6970 (c-looking-at-inexpr-block (c-safe-position containing-sexp
6971 paren-state)
6972 containing-sexp)))))
6975 ;; `c-guess-basic-syntax' and the functions that precedes it below
6976 ;; implements the main decision tree for determining the syntactic
6977 ;; analysis of the current line of code.
6979 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
6980 ;; auto newline analysis.
6981 (defvar c-auto-newline-analysis nil)
6983 (defsubst c-add-syntax (symbol &rest args)
6984 ;; A simple function to prepend a new syntax element to
6985 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
6986 ;; should always be dynamically bound but since we read it first
6987 ;; we'll fail properly anyway if this function is misused.
6988 (setq c-syntactic-context (cons (cons symbol args)
6989 c-syntactic-context)))
6991 (defsubst c-append-syntax (symbol &rest args)
6992 ;; Like `c-add-syntax' but appends to the end of the syntax list.
6993 ;; (Normally not necessary.)
6994 (setq c-syntactic-context (nconc c-syntactic-context
6995 (list (cons symbol args)))))
6997 (defun c-add-stmt-syntax (syntax-symbol
6998 syntax-extra-args
6999 stop-at-boi-only
7000 containing-sexp
7001 paren-state)
7002 ;; Do the generic processing to anchor the given syntax symbol on
7003 ;; the preceding statement: Skip over any labels and containing
7004 ;; statements on the same line, and then search backward until we
7005 ;; find a statement or block start that begins at boi without a
7006 ;; label or comment.
7008 ;; Point is assumed to be at the prospective anchor point for the
7009 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
7010 ;; skip past open parens and containing statements. All the added
7011 ;; syntax elements will get the same anchor point.
7013 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
7014 ;; syntax symbol. They are appended after the anchor point.
7016 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
7017 ;; if the current statement starts there.
7019 ;; Note: It's not a problem if PAREN-STATE "overshoots"
7020 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
7022 ;; This function might do hidden buffer changes.
7024 (if (= (point) (c-point 'boi))
7025 ;; This is by far the most common case, so let's give it special
7026 ;; treatment.
7027 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
7029 (let ((syntax-last c-syntactic-context)
7030 (boi (c-point 'boi))
7031 ;; Set when we're on a label, so that we don't stop there.
7032 ;; FIXME: To be complete we should check if we're on a label
7033 ;; now at the start.
7034 on-label)
7036 (apply 'c-add-syntax syntax-symbol nil syntax-extra-args)
7038 ;; Loop while we have to back out of containing blocks.
7039 (while
7040 (and
7041 (catch 'back-up-block
7043 ;; Loop while we have to back up statements.
7044 (while (or (/= (point) boi)
7045 on-label
7046 (looking-at c-comment-start-regexp))
7048 ;; Skip past any comments that stands between the
7049 ;; statement start and boi.
7050 (let ((savepos (point)))
7051 (while (and (/= savepos boi)
7052 (c-backward-single-comment))
7053 (setq savepos (point)
7054 boi (c-point 'boi)))
7055 (goto-char savepos))
7057 ;; Skip to the beginning of this statement or backward
7058 ;; another one.
7059 (let ((old-pos (point))
7060 (old-boi boi)
7061 (step-type (c-beginning-of-statement-1 containing-sexp)))
7062 (setq boi (c-point 'boi)
7063 on-label (eq step-type 'label))
7065 (cond ((= (point) old-pos)
7066 ;; If we didn't move we're at the start of a block and
7067 ;; have to continue outside it.
7068 (throw 'back-up-block t))
7070 ((and (eq step-type 'up)
7071 (>= (point) old-boi)
7072 (looking-at "else\\>[^_]")
7073 (save-excursion
7074 (goto-char old-pos)
7075 (looking-at "if\\>[^_]")))
7076 ;; Special case to avoid deeper and deeper indentation
7077 ;; of "else if" clauses.
7080 ((and (not stop-at-boi-only)
7081 (/= old-pos old-boi)
7082 (memq step-type '(up previous)))
7083 ;; If stop-at-boi-only is nil, we shouldn't back up
7084 ;; over previous or containing statements to try to
7085 ;; reach boi, so go back to the last position and
7086 ;; exit.
7087 (goto-char old-pos)
7088 (throw 'back-up-block nil))
7091 (if (and (not stop-at-boi-only)
7092 (memq step-type '(up previous beginning)))
7093 ;; If we've moved into another statement then we
7094 ;; should no longer try to stop in the middle of a
7095 ;; line.
7096 (setq stop-at-boi-only t))
7098 ;; Record this as a substatement if we skipped up one
7099 ;; level.
7100 (when (eq step-type 'up)
7101 (c-add-syntax 'substatement nil))))
7104 containing-sexp)
7106 ;; Now we have to go out of this block.
7107 (goto-char containing-sexp)
7109 ;; Don't stop in the middle of a special brace list opener
7110 ;; like "({".
7111 (when c-special-brace-lists
7112 (let ((special-list (c-looking-at-special-brace-list)))
7113 (when (and special-list
7114 (< (car (car special-list)) (point)))
7115 (setq containing-sexp (car (car special-list)))
7116 (goto-char containing-sexp))))
7118 (setq paren-state (c-whack-state-after containing-sexp paren-state)
7119 containing-sexp (c-most-enclosing-brace paren-state)
7120 boi (c-point 'boi))
7122 ;; Analyze the construct in front of the block we've stepped out
7123 ;; from and add the right syntactic element for it.
7124 (let ((paren-pos (point))
7125 (paren-char (char-after))
7126 step-type)
7128 (if (eq paren-char ?\()
7129 ;; Stepped out of a parenthesis block, so we're in an
7130 ;; expression now.
7131 (progn
7132 (when (/= paren-pos boi)
7133 (if (and c-recognize-paren-inexpr-blocks
7134 (progn
7135 (c-backward-syntactic-ws containing-sexp)
7136 (or (not (looking-at "\\>"))
7137 (not (c-on-identifier))))
7138 (save-excursion
7139 (goto-char (1+ paren-pos))
7140 (c-forward-syntactic-ws)
7141 (eq (char-after) ?{)))
7142 ;; Stepped out of an in-expression statement. This
7143 ;; syntactic element won't get an anchor pos.
7144 (c-add-syntax 'inexpr-statement)
7146 ;; A parenthesis normally belongs to an arglist.
7147 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
7149 (goto-char (max boi
7150 (if containing-sexp
7151 (1+ containing-sexp)
7152 (point-min))))
7153 (setq step-type 'same
7154 on-label nil))
7156 (setq step-type (c-beginning-of-statement-1 containing-sexp)
7157 on-label (eq step-type 'label))
7159 (if (and (eq step-type 'same)
7160 (/= paren-pos (point)))
7161 (save-excursion
7162 (goto-char paren-pos)
7163 (let ((inexpr (c-looking-at-inexpr-block
7164 (c-safe-position containing-sexp
7165 paren-state)
7166 containing-sexp)))
7167 (if (and inexpr
7168 (not (eq (car inexpr) 'inlambda)))
7169 (c-add-syntax 'statement-block-intro nil)
7170 (c-add-syntax 'defun-block-intro nil))))
7171 (c-add-syntax 'statement-block-intro nil)))
7173 (if (= paren-pos boi)
7174 ;; Always done if the open brace was at boi. The
7175 ;; c-beginning-of-statement-1 call above is necessary
7176 ;; anyway, to decide the type of block-intro to add.
7177 (goto-char paren-pos)
7178 (setq boi (c-point 'boi)))
7181 ;; Fill in the current point as the anchor for all the symbols
7182 ;; added above.
7183 (let ((p c-syntactic-context))
7184 (while (not (eq p syntax-last))
7185 (if (cdr (car p))
7186 (setcar (cdr (car p)) (point)))
7187 (setq p (cdr p))))
7190 (defun c-add-class-syntax (symbol
7191 containing-decl-open
7192 containing-decl-start
7193 containing-decl-kwd
7194 paren-state)
7195 ;; The inclass and class-close syntactic symbols are added in
7196 ;; several places and some work is needed to fix everything.
7197 ;; Therefore it's collected here.
7199 ;; This function might do hidden buffer changes.
7200 (goto-char containing-decl-open)
7201 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
7202 (progn
7203 (c-add-syntax symbol containing-decl-open)
7204 containing-decl-open)
7205 (goto-char containing-decl-start)
7206 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
7207 ;; here, but we have to do like this for compatibility.
7208 (back-to-indentation)
7209 (c-add-syntax symbol (point))
7210 (if (and (c-keyword-member containing-decl-kwd
7211 'c-inexpr-class-kwds)
7212 (/= containing-decl-start (c-point 'boi containing-decl-start)))
7213 (c-add-syntax 'inexpr-class))
7214 (point)))
7216 (defun c-guess-continued-construct (indent-point
7217 char-after-ip
7218 beg-of-same-or-containing-stmt
7219 containing-sexp
7220 paren-state)
7221 ;; This function contains the decision tree reached through both
7222 ;; cases 18 and 10. It's a continued statement or top level
7223 ;; construct of some kind.
7225 ;; This function might do hidden buffer changes.
7227 (let (special-brace-list)
7228 (goto-char indent-point)
7229 (skip-chars-forward " \t")
7231 (cond
7232 ;; (CASE A removed.)
7233 ;; CASE B: open braces for class or brace-lists
7234 ((setq special-brace-list
7235 (or (and c-special-brace-lists
7236 (c-looking-at-special-brace-list))
7237 (eq char-after-ip ?{)))
7239 (cond
7240 ;; CASE B.1: class-open
7241 ((save-excursion
7242 (and (eq (char-after) ?{)
7243 (c-looking-at-decl-block containing-sexp t)
7244 (setq beg-of-same-or-containing-stmt (point))))
7245 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
7247 ;; CASE B.2: brace-list-open
7248 ((or (consp special-brace-list)
7249 (save-excursion
7250 (goto-char beg-of-same-or-containing-stmt)
7251 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
7252 indent-point t t t)))
7253 ;; The most semantically accurate symbol here is
7254 ;; brace-list-open, but we normally report it simply as a
7255 ;; statement-cont. The reason is that one normally adjusts
7256 ;; brace-list-open for brace lists as top-level constructs,
7257 ;; and brace lists inside statements is a completely different
7258 ;; context. C.f. case 5A.3.
7259 (c-beginning-of-statement-1 containing-sexp)
7260 (c-add-stmt-syntax (if c-auto-newline-analysis
7261 ;; Turn off the dwim above when we're
7262 ;; analyzing the nature of the brace
7263 ;; for the auto newline feature.
7264 'brace-list-open
7265 'statement-cont)
7266 nil nil
7267 containing-sexp paren-state))
7269 ;; CASE B.3: The body of a function declared inside a normal
7270 ;; block. Can occur e.g. in Pike and when using gcc
7271 ;; extensions, but watch out for macros followed by blocks.
7272 ;; C.f. cases E, 16F and 17G.
7273 ((and (not (c-at-statement-start-p))
7274 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7275 'same)
7276 (save-excursion
7277 (let ((c-recognize-typeless-decls nil))
7278 ;; Turn off recognition of constructs that lacks a
7279 ;; type in this case, since that's more likely to be
7280 ;; a macro followed by a block.
7281 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7282 (c-add-stmt-syntax 'defun-open nil t
7283 containing-sexp paren-state))
7285 ;; CASE B.4: Continued statement with block open. The most
7286 ;; accurate analysis is perhaps `statement-cont' together with
7287 ;; `block-open' but we play DWIM and use `substatement-open'
7288 ;; instead. The rationaly is that this typically is a macro
7289 ;; followed by a block which makes it very similar to a
7290 ;; statement with a substatement block.
7292 (c-add-stmt-syntax 'substatement-open nil nil
7293 containing-sexp paren-state))
7296 ;; CASE C: iostream insertion or extraction operator
7297 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
7298 (save-excursion
7299 (goto-char beg-of-same-or-containing-stmt)
7300 ;; If there is no preceding streamop in the statement
7301 ;; then indent this line as a normal statement-cont.
7302 (when (c-syntactic-re-search-forward
7303 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
7304 (c-add-syntax 'stream-op (c-point 'boi))
7305 t))))
7307 ;; CASE E: In the "K&R region" of a function declared inside a
7308 ;; normal block. C.f. case B.3.
7309 ((and (save-excursion
7310 ;; Check that the next token is a '{'. This works as
7311 ;; long as no language that allows nested function
7312 ;; definitions allows stuff like member init lists, K&R
7313 ;; declarations or throws clauses there.
7315 ;; Note that we do a forward search for something ahead
7316 ;; of the indentation line here. That's not good since
7317 ;; the user might not have typed it yet. Unfortunately
7318 ;; it's exceedingly tricky to recognize a function
7319 ;; prototype in a code block without resorting to this.
7320 (c-forward-syntactic-ws)
7321 (eq (char-after) ?{))
7322 (not (c-at-statement-start-p))
7323 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7324 'same)
7325 (save-excursion
7326 (let ((c-recognize-typeless-decls nil))
7327 ;; Turn off recognition of constructs that lacks a
7328 ;; type in this case, since that's more likely to be
7329 ;; a macro followed by a block.
7330 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7331 (c-add-stmt-syntax 'func-decl-cont nil t
7332 containing-sexp paren-state))
7334 ;; CASE D: continued statement.
7336 (c-beginning-of-statement-1 containing-sexp)
7337 (c-add-stmt-syntax 'statement-cont nil nil
7338 containing-sexp paren-state))
7341 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
7342 ;; 2005/11/29).
7343 ;;;###autoload
7344 (defun c-guess-basic-syntax ()
7345 "Return the syntactic context of the current line."
7346 (save-excursion
7347 (beginning-of-line)
7348 (c-save-buffer-state
7349 ((indent-point (point))
7350 (case-fold-search nil)
7351 ;; A whole ugly bunch of various temporary variables. Have
7352 ;; to declare them here since it's not possible to declare
7353 ;; a variable with only the scope of a cond test and the
7354 ;; following result clauses, and most of this function is a
7355 ;; single gigantic cond. :P
7356 literal char-before-ip before-ws-ip char-after-ip macro-start
7357 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
7358 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
7359 ;; The following record some positions for the containing
7360 ;; declaration block if we're directly within one:
7361 ;; `containing-decl-open' is the position of the open
7362 ;; brace. `containing-decl-start' is the start of the
7363 ;; declaration. `containing-decl-kwd' is the keyword
7364 ;; symbol of the keyword that tells what kind of block it
7365 ;; is.
7366 containing-decl-open
7367 containing-decl-start
7368 containing-decl-kwd
7369 ;; The open paren of the closest surrounding sexp or nil if
7370 ;; there is none.
7371 containing-sexp
7372 ;; The position after the closest preceding brace sexp
7373 ;; (nested sexps are ignored), or the position after
7374 ;; `containing-sexp' if there is none, or (point-min) if
7375 ;; `containing-sexp' is nil.
7377 ;; The paren state outside `containing-sexp', or at
7378 ;; `indent-point' if `containing-sexp' is nil.
7379 (paren-state (c-parse-state))
7380 ;; There's always at most one syntactic element which got
7381 ;; an anchor pos. It's stored in syntactic-relpos.
7382 syntactic-relpos
7383 (c-stmt-delim-chars c-stmt-delim-chars))
7385 ;; Check if we're directly inside an enclosing declaration
7386 ;; level block.
7387 (when (and (setq containing-sexp
7388 (c-most-enclosing-brace paren-state))
7389 (progn
7390 (goto-char containing-sexp)
7391 (eq (char-after) ?{))
7392 (setq placeholder
7393 (c-looking-at-decl-block
7394 (c-most-enclosing-brace paren-state
7395 containing-sexp)
7396 t)))
7397 (setq containing-decl-open containing-sexp
7398 containing-decl-start (point)
7399 containing-sexp nil)
7400 (goto-char placeholder)
7401 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
7402 (c-keyword-sym (match-string 1)))))
7404 ;; Init some position variables.
7405 (if c-state-cache
7406 (progn
7407 (setq containing-sexp (car paren-state)
7408 paren-state (cdr paren-state))
7409 (if (consp containing-sexp)
7410 (progn
7411 (setq lim (cdr containing-sexp))
7412 (if (cdr c-state-cache)
7413 ;; Ignore balanced paren. The next entry
7414 ;; can't be another one.
7415 (setq containing-sexp (car (cdr c-state-cache))
7416 paren-state (cdr paren-state))
7417 ;; If there is no surrounding open paren then
7418 ;; put the last balanced pair back on paren-state.
7419 (setq paren-state (cons containing-sexp paren-state)
7420 containing-sexp nil)))
7421 (setq lim (1+ containing-sexp))))
7422 (setq lim (point-min)))
7424 ;; If we're in a parenthesis list then ',' delimits the
7425 ;; "statements" rather than being an operator (with the
7426 ;; exception of the "for" clause). This difference is
7427 ;; typically only noticeable when statements are used in macro
7428 ;; arglists.
7429 (when (and containing-sexp
7430 (eq (char-after containing-sexp) ?\())
7431 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
7433 ;; cache char before and after indent point, and move point to
7434 ;; the most likely position to perform the majority of tests
7435 (goto-char indent-point)
7436 (c-backward-syntactic-ws lim)
7437 (setq before-ws-ip (point)
7438 char-before-ip (char-before))
7439 (goto-char indent-point)
7440 (skip-chars-forward " \t")
7441 (setq char-after-ip (char-after))
7443 ;; are we in a literal?
7444 (setq literal (c-in-literal lim))
7446 ;; now figure out syntactic qualities of the current line
7447 (cond
7449 ;; CASE 1: in a string.
7450 ((eq literal 'string)
7451 (c-add-syntax 'string (c-point 'bopl)))
7453 ;; CASE 2: in a C or C++ style comment.
7454 ((and (memq literal '(c c++))
7455 ;; This is a kludge for XEmacs where we use
7456 ;; `buffer-syntactic-context', which doesn't correctly
7457 ;; recognize "\*/" to end a block comment.
7458 ;; `parse-partial-sexp' which is used by
7459 ;; `c-literal-limits' will however do that in most
7460 ;; versions, which results in that we get nil from
7461 ;; `c-literal-limits' even when `c-in-literal' claims
7462 ;; we're inside a comment.
7463 (setq placeholder (c-literal-limits lim)))
7464 (c-add-syntax literal (car placeholder)))
7466 ;; CASE 3: in a cpp preprocessor macro continuation.
7467 ((and (save-excursion
7468 (when (c-beginning-of-macro)
7469 (setq macro-start (point))))
7470 (/= macro-start (c-point 'boi))
7471 (progn
7472 (setq tmpsymbol 'cpp-macro-cont)
7473 (or (not c-syntactic-indentation-in-macros)
7474 (save-excursion
7475 (goto-char macro-start)
7476 ;; If at the beginning of the body of a #define
7477 ;; directive then analyze as cpp-define-intro
7478 ;; only. Go on with the syntactic analysis
7479 ;; otherwise. in-macro-expr is set if we're in a
7480 ;; cpp expression, i.e. before the #define body
7481 ;; or anywhere in a non-#define directive.
7482 (if (c-forward-to-cpp-define-body)
7483 (let ((indent-boi (c-point 'boi indent-point)))
7484 (setq in-macro-expr (> (point) indent-boi)
7485 tmpsymbol 'cpp-define-intro)
7486 (= (point) indent-boi))
7487 (setq in-macro-expr t)
7488 nil)))))
7489 (c-add-syntax tmpsymbol macro-start)
7490 (setq macro-start nil))
7492 ;; CASE 11: an else clause?
7493 ((looking-at "else\\>[^_]")
7494 (c-beginning-of-statement-1 containing-sexp)
7495 (c-add-stmt-syntax 'else-clause nil t
7496 containing-sexp paren-state))
7498 ;; CASE 12: while closure of a do/while construct?
7499 ((and (looking-at "while\\>[^_]")
7500 (save-excursion
7501 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
7502 'beginning)
7503 (setq placeholder (point)))))
7504 (goto-char placeholder)
7505 (c-add-stmt-syntax 'do-while-closure nil t
7506 containing-sexp paren-state))
7508 ;; CASE 13: A catch or finally clause? This case is simpler
7509 ;; than if-else and do-while, because a block is required
7510 ;; after every try, catch and finally.
7511 ((save-excursion
7512 (and (cond ((c-major-mode-is 'c++-mode)
7513 (looking-at "catch\\>[^_]"))
7514 ((c-major-mode-is 'java-mode)
7515 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
7516 (and (c-safe (c-backward-syntactic-ws)
7517 (c-backward-sexp)
7519 (eq (char-after) ?{)
7520 (c-safe (c-backward-syntactic-ws)
7521 (c-backward-sexp)
7523 (if (eq (char-after) ?\()
7524 (c-safe (c-backward-sexp) t)
7526 (looking-at "\\(try\\|catch\\)\\>[^_]")
7527 (setq placeholder (point))))
7528 (goto-char placeholder)
7529 (c-add-stmt-syntax 'catch-clause nil t
7530 containing-sexp paren-state))
7532 ;; CASE 18: A substatement we can recognize by keyword.
7533 ((save-excursion
7534 (and c-opt-block-stmt-key
7535 (not (eq char-before-ip ?\;))
7536 (not (c-at-vsemi-p before-ws-ip))
7537 (not (memq char-after-ip '(?\) ?\] ?,)))
7538 (or (not (eq char-before-ip ?}))
7539 (c-looking-at-inexpr-block-backward c-state-cache))
7540 (> (point)
7541 (progn
7542 ;; Ought to cache the result from the
7543 ;; c-beginning-of-statement-1 calls here.
7544 (setq placeholder (point))
7545 (while (eq (setq step-type
7546 (c-beginning-of-statement-1 lim))
7547 'label))
7548 (if (eq step-type 'previous)
7549 (goto-char placeholder)
7550 (setq placeholder (point))
7551 (if (and (eq step-type 'same)
7552 (not (looking-at c-opt-block-stmt-key)))
7553 ;; Step up to the containing statement if we
7554 ;; stayed in the same one.
7555 (let (step)
7556 (while (eq
7557 (setq step
7558 (c-beginning-of-statement-1 lim))
7559 'label))
7560 (if (eq step 'up)
7561 (setq placeholder (point))
7562 ;; There was no containing statement afterall.
7563 (goto-char placeholder)))))
7564 placeholder))
7565 (if (looking-at c-block-stmt-2-key)
7566 ;; Require a parenthesis after these keywords.
7567 ;; Necessary to catch e.g. synchronized in Java,
7568 ;; which can be used both as statement and
7569 ;; modifier.
7570 (and (zerop (c-forward-token-2 1 nil))
7571 (eq (char-after) ?\())
7572 (looking-at c-opt-block-stmt-key))))
7574 (if (eq step-type 'up)
7575 ;; CASE 18A: Simple substatement.
7576 (progn
7577 (goto-char placeholder)
7578 (cond
7579 ((eq char-after-ip ?{)
7580 (c-add-stmt-syntax 'substatement-open nil nil
7581 containing-sexp paren-state))
7582 ((save-excursion
7583 (goto-char indent-point)
7584 (back-to-indentation)
7585 (c-forward-label))
7586 (c-add-stmt-syntax 'substatement-label nil nil
7587 containing-sexp paren-state))
7589 (c-add-stmt-syntax 'substatement nil nil
7590 containing-sexp paren-state))))
7592 ;; CASE 18B: Some other substatement. This is shared
7593 ;; with case 10.
7594 (c-guess-continued-construct indent-point
7595 char-after-ip
7596 placeholder
7598 paren-state)))
7600 ;; CASE 14: A case or default label
7601 ((looking-at c-label-kwds-regexp)
7602 (if containing-sexp
7603 (progn
7604 (goto-char containing-sexp)
7605 (setq lim (c-most-enclosing-brace c-state-cache
7606 containing-sexp))
7607 (c-backward-to-block-anchor lim)
7608 (c-add-stmt-syntax 'case-label nil t lim paren-state))
7609 ;; Got a bogus label at the top level. In lack of better
7610 ;; alternatives, anchor it on (point-min).
7611 (c-add-syntax 'case-label (point-min))))
7613 ;; CASE 15: any other label
7614 ((save-excursion
7615 (back-to-indentation)
7616 (and (not (looking-at c-syntactic-ws-start))
7617 (c-forward-label)))
7618 (cond (containing-decl-open
7619 (setq placeholder (c-add-class-syntax 'inclass
7620 containing-decl-open
7621 containing-decl-start
7622 containing-decl-kwd
7623 paren-state))
7624 ;; Append access-label with the same anchor point as
7625 ;; inclass gets.
7626 (c-append-syntax 'access-label placeholder))
7628 (containing-sexp
7629 (goto-char containing-sexp)
7630 (setq lim (c-most-enclosing-brace c-state-cache
7631 containing-sexp))
7632 (save-excursion
7633 (setq tmpsymbol
7634 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
7635 (looking-at "switch\\>[^_]"))
7636 ;; If the surrounding statement is a switch then
7637 ;; let's analyze all labels as switch labels, so
7638 ;; that they get lined up consistently.
7639 'case-label
7640 'label)))
7641 (c-backward-to-block-anchor lim)
7642 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
7645 ;; A label on the top level. Treat it as a class
7646 ;; context. (point-min) is the closest we get to the
7647 ;; class open brace.
7648 (c-add-syntax 'access-label (point-min)))))
7650 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
7651 ;; 17E.
7652 ((setq placeholder (c-looking-at-inexpr-block
7653 (c-safe-position containing-sexp paren-state)
7654 containing-sexp
7655 ;; Have to turn on the heuristics after
7656 ;; the point even though it doesn't work
7657 ;; very well. C.f. test case class-16.pike.
7659 (setq tmpsymbol (assq (car placeholder)
7660 '((inexpr-class . class-open)
7661 (inexpr-statement . block-open))))
7662 (if tmpsymbol
7663 ;; It's a statement block or an anonymous class.
7664 (setq tmpsymbol (cdr tmpsymbol))
7665 ;; It's a Pike lambda. Check whether we are between the
7666 ;; lambda keyword and the argument list or at the defun
7667 ;; opener.
7668 (setq tmpsymbol (if (eq char-after-ip ?{)
7669 'inline-open
7670 'lambda-intro-cont)))
7671 (goto-char (cdr placeholder))
7672 (back-to-indentation)
7673 (c-add-stmt-syntax tmpsymbol nil t
7674 (c-most-enclosing-brace c-state-cache (point))
7675 paren-state)
7676 (unless (eq (point) (cdr placeholder))
7677 (c-add-syntax (car placeholder))))
7679 ;; CASE 5: Line is inside a declaration level block or at top level.
7680 ((or containing-decl-open (null containing-sexp))
7681 (cond
7683 ;; CASE 5A: we are looking at a defun, brace list, class,
7684 ;; or inline-inclass method opening brace
7685 ((setq special-brace-list
7686 (or (and c-special-brace-lists
7687 (c-looking-at-special-brace-list))
7688 (eq char-after-ip ?{)))
7689 (cond
7691 ;; CASE 5A.1: Non-class declaration block open.
7692 ((save-excursion
7693 (let (tmp)
7694 (and (eq char-after-ip ?{)
7695 (setq tmp (c-looking-at-decl-block containing-sexp t))
7696 (progn
7697 (setq placeholder (point))
7698 (goto-char tmp)
7699 (looking-at c-symbol-key))
7700 (c-keyword-member
7701 (c-keyword-sym (setq keyword (match-string 0)))
7702 'c-other-block-decl-kwds))))
7703 (goto-char placeholder)
7704 (c-add-stmt-syntax
7705 (if (string-equal keyword "extern")
7706 ;; Special case for extern-lang-open.
7707 'extern-lang-open
7708 (intern (concat keyword "-open")))
7709 nil t containing-sexp paren-state))
7711 ;; CASE 5A.2: we are looking at a class opening brace
7712 ((save-excursion
7713 (goto-char indent-point)
7714 (skip-chars-forward " \t")
7715 (and (eq (char-after) ?{)
7716 (c-looking-at-decl-block containing-sexp t)
7717 (setq placeholder (point))))
7718 (c-add-syntax 'class-open placeholder))
7720 ;; CASE 5A.3: brace list open
7721 ((save-excursion
7722 (c-beginning-of-decl-1 lim)
7723 (while (looking-at c-specifier-key)
7724 (goto-char (match-end 1))
7725 (c-forward-syntactic-ws indent-point))
7726 (setq placeholder (c-point 'boi))
7727 (or (consp special-brace-list)
7728 (and (or (save-excursion
7729 (goto-char indent-point)
7730 (setq tmpsymbol nil)
7731 (while (and (> (point) placeholder)
7732 (zerop (c-backward-token-2 1 t))
7733 (/= (char-after) ?=))
7734 (and c-opt-inexpr-brace-list-key
7735 (not tmpsymbol)
7736 (looking-at c-opt-inexpr-brace-list-key)
7737 (setq tmpsymbol 'topmost-intro-cont)))
7738 (eq (char-after) ?=))
7739 (looking-at c-brace-list-key))
7740 (save-excursion
7741 (while (and (< (point) indent-point)
7742 (zerop (c-forward-token-2 1 t))
7743 (not (memq (char-after) '(?\; ?\()))))
7744 (not (memq (char-after) '(?\; ?\()))
7745 ))))
7746 (if (and (not c-auto-newline-analysis)
7747 (c-major-mode-is 'java-mode)
7748 (eq tmpsymbol 'topmost-intro-cont))
7749 ;; We're in Java and have found that the open brace
7750 ;; belongs to a "new Foo[]" initialization list,
7751 ;; which means the brace list is part of an
7752 ;; expression and not a top level definition. We
7753 ;; therefore treat it as any topmost continuation
7754 ;; even though the semantically correct symbol still
7755 ;; is brace-list-open, on the same grounds as in
7756 ;; case B.2.
7757 (progn
7758 (c-beginning-of-statement-1 lim)
7759 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
7760 (c-add-syntax 'brace-list-open placeholder)))
7762 ;; CASE 5A.4: inline defun open
7763 ((and containing-decl-open
7764 (not (c-keyword-member containing-decl-kwd
7765 'c-other-block-decl-kwds)))
7766 (c-add-syntax 'inline-open)
7767 (c-add-class-syntax 'inclass
7768 containing-decl-open
7769 containing-decl-start
7770 containing-decl-kwd
7771 paren-state))
7773 ;; CASE 5A.5: ordinary defun open
7775 (goto-char placeholder)
7776 (if (or containing-decl-open macro-start)
7777 (c-add-syntax 'defun-open (c-point 'boi))
7778 ;; Bogus to use bol here, but it's the legacy.
7779 (c-add-syntax 'defun-open (c-point 'bol)))
7782 ;; CASE 5B: After a function header but before the body (or
7783 ;; the ending semicolon if there's no body).
7784 ((save-excursion
7785 (when (setq placeholder (c-just-after-func-arglist-p lim))
7786 (setq tmp-pos (point))))
7787 (cond
7789 ;; CASE 5B.1: Member init list.
7790 ((eq (char-after tmp-pos) ?:)
7791 (if (or (> tmp-pos indent-point)
7792 (= (c-point 'bosws) (1+ tmp-pos)))
7793 (progn
7794 ;; There is no preceding member init clause.
7795 ;; Indent relative to the beginning of indentation
7796 ;; for the topmost-intro line that contains the
7797 ;; prototype's open paren.
7798 (goto-char placeholder)
7799 (c-add-syntax 'member-init-intro (c-point 'boi)))
7800 ;; Indent relative to the first member init clause.
7801 (goto-char (1+ tmp-pos))
7802 (c-forward-syntactic-ws)
7803 (c-add-syntax 'member-init-cont (point))))
7805 ;; CASE 5B.2: K&R arg decl intro
7806 ((and c-recognize-knr-p
7807 (c-in-knr-argdecl lim))
7808 (c-beginning-of-statement-1 lim)
7809 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
7810 (if containing-decl-open
7811 (c-add-class-syntax 'inclass
7812 containing-decl-open
7813 containing-decl-start
7814 containing-decl-kwd
7815 paren-state)))
7817 ;; CASE 5B.4: Nether region after a C++ or Java func
7818 ;; decl, which could include a `throws' declaration.
7820 (c-beginning-of-statement-1 lim)
7821 (c-add-syntax 'func-decl-cont (c-point 'boi))
7824 ;; CASE 5C: inheritance line. could be first inheritance
7825 ;; line, or continuation of a multiple inheritance
7826 ((or (and (c-major-mode-is 'c++-mode)
7827 (progn
7828 (when (eq char-after-ip ?,)
7829 (skip-chars-forward " \t")
7830 (forward-char))
7831 (looking-at c-opt-postfix-decl-spec-key)))
7832 (and (or (eq char-before-ip ?:)
7833 ;; watch out for scope operator
7834 (save-excursion
7835 (and (eq char-after-ip ?:)
7836 (c-safe (forward-char 1) t)
7837 (not (eq (char-after) ?:))
7839 (save-excursion
7840 (c-backward-syntactic-ws lim)
7841 (if (eq char-before-ip ?:)
7842 (progn
7843 (forward-char -1)
7844 (c-backward-syntactic-ws lim)))
7845 (back-to-indentation)
7846 (looking-at c-class-key)))
7847 ;; for Java
7848 (and (c-major-mode-is 'java-mode)
7849 (let ((fence (save-excursion
7850 (c-beginning-of-statement-1 lim)
7851 (point)))
7852 cont done)
7853 (save-excursion
7854 (while (not done)
7855 (cond ((looking-at c-opt-postfix-decl-spec-key)
7856 (setq injava-inher (cons cont (point))
7857 done t))
7858 ((or (not (c-safe (c-forward-sexp -1) t))
7859 (<= (point) fence))
7860 (setq done t))
7862 (setq cont t)))
7863 injava-inher)
7864 (not (c-crosses-statement-barrier-p (cdr injava-inher)
7865 (point)))
7867 (cond
7869 ;; CASE 5C.1: non-hanging colon on an inher intro
7870 ((eq char-after-ip ?:)
7871 (c-beginning-of-statement-1 lim)
7872 (c-add-syntax 'inher-intro (c-point 'boi))
7873 ;; don't add inclass symbol since relative point already
7874 ;; contains any class offset
7877 ;; CASE 5C.2: hanging colon on an inher intro
7878 ((eq char-before-ip ?:)
7879 (c-beginning-of-statement-1 lim)
7880 (c-add-syntax 'inher-intro (c-point 'boi))
7881 (if containing-decl-open
7882 (c-add-class-syntax 'inclass
7883 containing-decl-open
7884 containing-decl-start
7885 containing-decl-kwd
7886 paren-state)))
7888 ;; CASE 5C.3: in a Java implements/extends
7889 (injava-inher
7890 (let ((where (cdr injava-inher))
7891 (cont (car injava-inher)))
7892 (goto-char where)
7893 (cond ((looking-at "throws\\>[^_]")
7894 (c-add-syntax 'func-decl-cont
7895 (progn (c-beginning-of-statement-1 lim)
7896 (c-point 'boi))))
7897 (cont (c-add-syntax 'inher-cont where))
7898 (t (c-add-syntax 'inher-intro
7899 (progn (goto-char (cdr injava-inher))
7900 (c-beginning-of-statement-1 lim)
7901 (point))))
7904 ;; CASE 5C.4: a continued inheritance line
7906 (c-beginning-of-inheritance-list lim)
7907 (c-add-syntax 'inher-cont (point))
7908 ;; don't add inclass symbol since relative point already
7909 ;; contains any class offset
7912 ;; CASE 5D: this could be a top-level initialization, a
7913 ;; member init list continuation, or a template argument
7914 ;; list continuation.
7915 ((save-excursion
7916 ;; Note: We use the fact that lim always is after any
7917 ;; preceding brace sexp.
7918 (if c-recognize-<>-arglists
7919 (while (and
7920 (progn
7921 (c-syntactic-skip-backward "^;,=<>" lim t)
7922 (> (point) lim))
7924 (when c-overloadable-operators-regexp
7925 (when (setq placeholder (c-after-special-operator-id lim))
7926 (goto-char placeholder)
7928 (cond
7929 ((eq (char-before) ?>)
7930 (or (c-backward-<>-arglist nil lim)
7931 (backward-char))
7933 ((eq (char-before) ?<)
7934 (backward-char)
7935 (if (save-excursion
7936 (c-forward-<>-arglist nil))
7937 (progn (forward-char)
7938 nil)
7940 (t nil)))))
7941 ;; NB: No c-after-special-operator-id stuff in this
7942 ;; clause - we assume only C++ needs it.
7943 (c-syntactic-skip-backward "^;,=" lim t))
7944 (memq (char-before) '(?, ?= ?<)))
7945 (cond
7947 ;; CASE 5D.3: perhaps a template list continuation?
7948 ((and (c-major-mode-is 'c++-mode)
7949 (save-excursion
7950 (save-restriction
7951 (c-with-syntax-table c++-template-syntax-table
7952 (goto-char indent-point)
7953 (setq placeholder (c-up-list-backward))
7954 (and placeholder
7955 (eq (char-after placeholder) ?<))))))
7956 (c-with-syntax-table c++-template-syntax-table
7957 (goto-char placeholder)
7958 (c-beginning-of-statement-1 lim t)
7959 (if (save-excursion
7960 (c-backward-syntactic-ws lim)
7961 (eq (char-before) ?<))
7962 ;; In a nested template arglist.
7963 (progn
7964 (goto-char placeholder)
7965 (c-syntactic-skip-backward "^,;" lim t)
7966 (c-forward-syntactic-ws))
7967 (back-to-indentation)))
7968 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
7969 ;; template aware.
7970 (c-add-syntax 'template-args-cont (point)))
7972 ;; CASE 5D.4: perhaps a multiple inheritance line?
7973 ((and (c-major-mode-is 'c++-mode)
7974 (save-excursion
7975 (c-beginning-of-statement-1 lim)
7976 (setq placeholder (point))
7977 (if (looking-at "static\\>[^_]")
7978 (c-forward-token-2 1 nil indent-point))
7979 (and (looking-at c-class-key)
7980 (zerop (c-forward-token-2 2 nil indent-point))
7981 (if (eq (char-after) ?<)
7982 (c-with-syntax-table c++-template-syntax-table
7983 (zerop (c-forward-token-2 1 t indent-point)))
7985 (eq (char-after) ?:))))
7986 (goto-char placeholder)
7987 (c-add-syntax 'inher-cont (c-point 'boi)))
7989 ;; CASE 5D.5: Continuation of the "expression part" of a
7990 ;; top level construct.
7992 (while (and (eq (car (c-beginning-of-decl-1 containing-sexp))
7993 'same)
7994 (save-excursion
7995 (c-backward-syntactic-ws)
7996 (eq (char-before) ?}))))
7997 (c-add-stmt-syntax
7998 (if (eq char-before-ip ?,)
7999 ;; A preceding comma at the top level means that a
8000 ;; new variable declaration starts here. Use
8001 ;; topmost-intro-cont for it, for consistency with
8002 ;; the first variable declaration. C.f. case 5N.
8003 'topmost-intro-cont
8004 'statement-cont)
8005 nil nil containing-sexp paren-state))
8008 ;; CASE 5F: Close of a non-class declaration level block.
8009 ((and (eq char-after-ip ?})
8010 (c-keyword-member containing-decl-kwd
8011 'c-other-block-decl-kwds))
8012 ;; This is inconsistent: Should use `containing-decl-open'
8013 ;; here if it's at boi, like in case 5J.
8014 (goto-char containing-decl-start)
8015 (c-add-stmt-syntax
8016 (if (string-equal (symbol-name containing-decl-kwd) "extern")
8017 ;; Special case for compatibility with the
8018 ;; extern-lang syntactic symbols.
8019 'extern-lang-close
8020 (intern (concat (symbol-name containing-decl-kwd)
8021 "-close")))
8022 nil t
8023 (c-most-enclosing-brace paren-state (point))
8024 paren-state))
8026 ;; CASE 5G: we are looking at the brace which closes the
8027 ;; enclosing nested class decl
8028 ((and containing-sexp
8029 (eq char-after-ip ?})
8030 (eq containing-decl-open containing-sexp))
8031 (c-add-class-syntax 'class-close
8032 containing-decl-open
8033 containing-decl-start
8034 containing-decl-kwd
8035 paren-state))
8037 ;; CASE 5H: we could be looking at subsequent knr-argdecls
8038 ((and c-recognize-knr-p
8039 (not (eq char-before-ip ?}))
8040 (save-excursion
8041 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
8042 (and placeholder
8043 ;; Do an extra check to avoid tripping up on
8044 ;; statements that occur in invalid contexts
8045 ;; (e.g. in macro bodies where we don't really
8046 ;; know the context of what we're looking at).
8047 (not (and c-opt-block-stmt-key
8048 (looking-at c-opt-block-stmt-key)))))
8049 (< placeholder indent-point))
8050 (goto-char placeholder)
8051 (c-add-syntax 'knr-argdecl (point)))
8053 ;; CASE 5I: ObjC method definition.
8054 ((and c-opt-method-key
8055 (looking-at c-opt-method-key))
8056 (c-beginning-of-statement-1 nil t)
8057 (if (= (point) indent-point)
8058 ;; Handle the case when it's the first (non-comment)
8059 ;; thing in the buffer. Can't look for a 'same return
8060 ;; value from cbos1 since ObjC directives currently
8061 ;; aren't recognized fully, so that we get 'same
8062 ;; instead of 'previous if it moved over a preceding
8063 ;; directive.
8064 (goto-char (point-min)))
8065 (c-add-syntax 'objc-method-intro (c-point 'boi)))
8067 ;; CASE 5P: AWK pattern or function or continuation
8068 ;; thereof.
8069 ((c-major-mode-is 'awk-mode)
8070 (setq placeholder (point))
8071 (c-add-stmt-syntax
8072 (if (and (eq (c-beginning-of-statement-1) 'same)
8073 (/= (point) placeholder))
8074 'topmost-intro-cont
8075 'topmost-intro)
8076 nil nil
8077 containing-sexp paren-state))
8079 ;; CASE 5N: At a variable declaration that follows a class
8080 ;; definition or some other block declaration that doesn't
8081 ;; end at the closing '}'. C.f. case 5D.5.
8082 ((progn
8083 (c-backward-syntactic-ws lim)
8084 (and (eq (char-before) ?})
8085 (save-excursion
8086 (let ((start (point)))
8087 (if c-state-cache
8088 ;; Speed up the backward search a bit.
8089 (goto-char (caar c-state-cache)))
8090 (c-beginning-of-decl-1 containing-sexp)
8091 (setq placeholder (point))
8092 (if (= start (point))
8093 ;; The '}' is unbalanced.
8095 (c-end-of-decl-1)
8096 (>= (point) indent-point))))))
8097 (goto-char placeholder)
8098 (c-add-stmt-syntax 'topmost-intro-cont nil nil
8099 containing-sexp paren-state))
8101 ;; NOTE: The point is at the end of the previous token here.
8103 ;; CASE 5J: we are at the topmost level, make
8104 ;; sure we skip back past any access specifiers
8105 ((save-excursion
8106 (setq placeholder (point))
8107 (or (memq char-before-ip '(?\; ?{ ?} nil))
8108 (c-at-vsemi-p before-ws-ip)
8109 (when (and (eq char-before-ip ?:)
8110 (eq (c-beginning-of-statement-1 lim)
8111 'label))
8112 (c-backward-syntactic-ws lim)
8113 (setq placeholder (point)))
8114 (and (c-major-mode-is 'objc-mode)
8115 (catch 'not-in-directive
8116 (c-beginning-of-statement-1 lim)
8117 (setq placeholder (point))
8118 (while (and (c-forward-objc-directive)
8119 (< (point) indent-point))
8120 (c-forward-syntactic-ws)
8121 (if (>= (point) indent-point)
8122 (throw 'not-in-directive t))
8123 (setq placeholder (point)))
8124 nil))))
8125 ;; For historic reasons we anchor at bol of the last
8126 ;; line of the previous declaration. That's clearly
8127 ;; highly bogus and useless, and it makes our lives hard
8128 ;; to remain compatible. :P
8129 (goto-char placeholder)
8130 (c-add-syntax 'topmost-intro (c-point 'bol))
8131 (if containing-decl-open
8132 (if (c-keyword-member containing-decl-kwd
8133 'c-other-block-decl-kwds)
8134 (progn
8135 (goto-char containing-decl-open)
8136 (unless (= (point) (c-point 'boi))
8137 (goto-char containing-decl-start))
8138 (c-add-stmt-syntax
8139 (if (string-equal (symbol-name containing-decl-kwd)
8140 "extern")
8141 ;; Special case for compatibility with the
8142 ;; extern-lang syntactic symbols.
8143 'inextern-lang
8144 (intern (concat "in"
8145 (symbol-name containing-decl-kwd))))
8146 nil t
8147 (c-most-enclosing-brace paren-state (point))
8148 paren-state))
8149 (c-add-class-syntax 'inclass
8150 containing-decl-open
8151 containing-decl-start
8152 containing-decl-kwd
8153 paren-state)))
8154 (when (and c-syntactic-indentation-in-macros
8155 macro-start
8156 (/= macro-start (c-point 'boi indent-point)))
8157 (c-add-syntax 'cpp-define-intro)
8158 (setq macro-start nil)))
8160 ;; CASE 5K: we are at an ObjC method definition
8161 ;; continuation line.
8162 ((and c-opt-method-key
8163 (save-excursion
8164 (c-beginning-of-statement-1 lim)
8165 (beginning-of-line)
8166 (when (looking-at c-opt-method-key)
8167 (setq placeholder (point)))))
8168 (c-add-syntax 'objc-method-args-cont placeholder))
8170 ;; CASE 5L: we are at the first argument of a template
8171 ;; arglist that begins on the previous line.
8172 ((and c-recognize-<>-arglists
8173 (eq (char-before) ?<)
8174 (not (and c-overloadable-operators-regexp
8175 (c-after-special-operator-id lim))))
8176 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8177 (c-add-syntax 'template-args-cont (c-point 'boi)))
8179 ;; CASE 5M: we are at a topmost continuation line
8181 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8182 (when (c-major-mode-is 'objc-mode)
8183 (setq placeholder (point))
8184 (while (and (c-forward-objc-directive)
8185 (< (point) indent-point))
8186 (c-forward-syntactic-ws)
8187 (setq placeholder (point)))
8188 (goto-char placeholder))
8189 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8192 ;; (CASE 6 has been removed.)
8194 ;; CASE 7: line is an expression, not a statement. Most
8195 ;; likely we are either in a function prototype or a function
8196 ;; call argument list
8197 ((not (or (and c-special-brace-lists
8198 (save-excursion
8199 (goto-char containing-sexp)
8200 (c-looking-at-special-brace-list)))
8201 (eq (char-after containing-sexp) ?{)))
8202 (cond
8204 ;; CASE 7A: we are looking at the arglist closing paren.
8205 ;; C.f. case 7F.
8206 ((memq char-after-ip '(?\) ?\]))
8207 (goto-char containing-sexp)
8208 (setq placeholder (c-point 'boi))
8209 (if (and (c-safe (backward-up-list 1) t)
8210 (>= (point) placeholder))
8211 (progn
8212 (forward-char)
8213 (skip-chars-forward " \t"))
8214 (goto-char placeholder))
8215 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
8216 (c-most-enclosing-brace paren-state (point))
8217 paren-state))
8219 ;; CASE 7B: Looking at the opening brace of an
8220 ;; in-expression block or brace list. C.f. cases 4, 16A
8221 ;; and 17E.
8222 ((and (eq char-after-ip ?{)
8223 (progn
8224 (setq placeholder (c-inside-bracelist-p (point)
8225 paren-state))
8226 (if placeholder
8227 (setq tmpsymbol '(brace-list-open . inexpr-class))
8228 (setq tmpsymbol '(block-open . inexpr-statement)
8229 placeholder
8230 (cdr-safe (c-looking-at-inexpr-block
8231 (c-safe-position containing-sexp
8232 paren-state)
8233 containing-sexp)))
8234 ;; placeholder is nil if it's a block directly in
8235 ;; a function arglist. That makes us skip out of
8236 ;; this case.
8238 (goto-char placeholder)
8239 (back-to-indentation)
8240 (c-add-stmt-syntax (car tmpsymbol) nil t
8241 (c-most-enclosing-brace paren-state (point))
8242 paren-state)
8243 (if (/= (point) placeholder)
8244 (c-add-syntax (cdr tmpsymbol))))
8246 ;; CASE 7C: we are looking at the first argument in an empty
8247 ;; argument list. Use arglist-close if we're actually
8248 ;; looking at a close paren or bracket.
8249 ((memq char-before-ip '(?\( ?\[))
8250 (goto-char containing-sexp)
8251 (setq placeholder (c-point 'boi))
8252 (if (and (c-safe (backward-up-list 1) t)
8253 (>= (point) placeholder))
8254 (progn
8255 (forward-char)
8256 (skip-chars-forward " \t"))
8257 (goto-char placeholder))
8258 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
8259 (c-most-enclosing-brace paren-state (point))
8260 paren-state))
8262 ;; CASE 7D: we are inside a conditional test clause. treat
8263 ;; these things as statements
8264 ((progn
8265 (goto-char containing-sexp)
8266 (and (c-safe (c-forward-sexp -1) t)
8267 (looking-at "\\<for\\>[^_]")))
8268 (goto-char (1+ containing-sexp))
8269 (c-forward-syntactic-ws indent-point)
8270 (if (eq char-before-ip ?\;)
8271 (c-add-syntax 'statement (point))
8272 (c-add-syntax 'statement-cont (point))
8275 ;; CASE 7E: maybe a continued ObjC method call. This is the
8276 ;; case when we are inside a [] bracketed exp, and what
8277 ;; precede the opening bracket is not an identifier.
8278 ((and c-opt-method-key
8279 (eq (char-after containing-sexp) ?\[)
8280 (progn
8281 (goto-char (1- containing-sexp))
8282 (c-backward-syntactic-ws (c-point 'bod))
8283 (if (not (looking-at c-symbol-key))
8284 (c-add-syntax 'objc-method-call-cont containing-sexp))
8287 ;; CASE 7F: we are looking at an arglist continuation line,
8288 ;; but the preceding argument is on the same line as the
8289 ;; opening paren. This case includes multi-line
8290 ;; mathematical paren groupings, but we could be on a
8291 ;; for-list continuation line. C.f. case 7A.
8292 ((progn
8293 (goto-char (1+ containing-sexp))
8294 (< (save-excursion
8295 (c-forward-syntactic-ws)
8296 (point))
8297 (c-point 'bonl)))
8298 (goto-char containing-sexp)
8299 (setq placeholder (c-point 'boi))
8300 (if (and (c-safe (backward-up-list 1) t)
8301 (>= (point) placeholder))
8302 (progn
8303 (forward-char)
8304 (skip-chars-forward " \t"))
8305 (goto-char placeholder))
8306 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
8307 (c-most-enclosing-brace c-state-cache (point))
8308 paren-state))
8310 ;; CASE 7G: we are looking at just a normal arglist
8311 ;; continuation line
8312 (t (c-forward-syntactic-ws indent-point)
8313 (c-add-syntax 'arglist-cont (c-point 'boi)))
8316 ;; CASE 8: func-local multi-inheritance line
8317 ((and (c-major-mode-is 'c++-mode)
8318 (save-excursion
8319 (goto-char indent-point)
8320 (skip-chars-forward " \t")
8321 (looking-at c-opt-postfix-decl-spec-key)))
8322 (goto-char indent-point)
8323 (skip-chars-forward " \t")
8324 (cond
8326 ;; CASE 8A: non-hanging colon on an inher intro
8327 ((eq char-after-ip ?:)
8328 (c-backward-syntactic-ws lim)
8329 (c-add-syntax 'inher-intro (c-point 'boi)))
8331 ;; CASE 8B: hanging colon on an inher intro
8332 ((eq char-before-ip ?:)
8333 (c-add-syntax 'inher-intro (c-point 'boi)))
8335 ;; CASE 8C: a continued inheritance line
8337 (c-beginning-of-inheritance-list lim)
8338 (c-add-syntax 'inher-cont (point))
8341 ;; CASE 9: we are inside a brace-list
8342 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
8343 (setq special-brace-list
8344 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
8345 (save-excursion
8346 (goto-char containing-sexp)
8347 (c-looking-at-special-brace-list)))
8348 (c-inside-bracelist-p containing-sexp paren-state))))
8349 (cond
8351 ;; CASE 9A: In the middle of a special brace list opener.
8352 ((and (consp special-brace-list)
8353 (save-excursion
8354 (goto-char containing-sexp)
8355 (eq (char-after) ?\())
8356 (eq char-after-ip (car (cdr special-brace-list))))
8357 (goto-char (car (car special-brace-list)))
8358 (skip-chars-backward " \t")
8359 (if (and (bolp)
8360 (assoc 'statement-cont
8361 (setq placeholder (c-guess-basic-syntax))))
8362 (setq c-syntactic-context placeholder)
8363 (c-beginning-of-statement-1
8364 (c-safe-position (1- containing-sexp) paren-state))
8365 (c-forward-token-2 0)
8366 (while (looking-at c-specifier-key)
8367 (goto-char (match-end 1))
8368 (c-forward-syntactic-ws))
8369 (c-add-syntax 'brace-list-open (c-point 'boi))))
8371 ;; CASE 9B: brace-list-close brace
8372 ((if (consp special-brace-list)
8373 ;; Check special brace list closer.
8374 (progn
8375 (goto-char (car (car special-brace-list)))
8376 (save-excursion
8377 (goto-char indent-point)
8378 (back-to-indentation)
8380 ;; We were between the special close char and the `)'.
8381 (and (eq (char-after) ?\))
8382 (eq (1+ (point)) (cdr (car special-brace-list))))
8383 ;; We were before the special close char.
8384 (and (eq (char-after) (cdr (cdr special-brace-list)))
8385 (zerop (c-forward-token-2))
8386 (eq (1+ (point)) (cdr (car special-brace-list)))))))
8387 ;; Normal brace list check.
8388 (and (eq char-after-ip ?})
8389 (c-safe (goto-char (c-up-list-backward (point))) t)
8390 (= (point) containing-sexp)))
8391 (if (eq (point) (c-point 'boi))
8392 (c-add-syntax 'brace-list-close (point))
8393 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8394 (c-beginning-of-statement-1 lim)
8395 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
8398 ;; Prepare for the rest of the cases below by going to the
8399 ;; token following the opening brace
8400 (if (consp special-brace-list)
8401 (progn
8402 (goto-char (car (car special-brace-list)))
8403 (c-forward-token-2 1 nil indent-point))
8404 (goto-char containing-sexp))
8405 (forward-char)
8406 (let ((start (point)))
8407 (c-forward-syntactic-ws indent-point)
8408 (goto-char (max start (c-point 'bol))))
8409 (c-skip-ws-forward indent-point)
8410 (cond
8412 ;; CASE 9C: we're looking at the first line in a brace-list
8413 ((= (point) indent-point)
8414 (if (consp special-brace-list)
8415 (goto-char (car (car special-brace-list)))
8416 (goto-char containing-sexp))
8417 (if (eq (point) (c-point 'boi))
8418 (c-add-syntax 'brace-list-intro (point))
8419 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8420 (c-beginning-of-statement-1 lim)
8421 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
8423 ;; CASE 9D: this is just a later brace-list-entry or
8424 ;; brace-entry-open
8425 (t (if (or (eq char-after-ip ?{)
8426 (and c-special-brace-lists
8427 (save-excursion
8428 (goto-char indent-point)
8429 (c-forward-syntactic-ws (c-point 'eol))
8430 (c-looking-at-special-brace-list (point)))))
8431 (c-add-syntax 'brace-entry-open (point))
8432 (c-add-syntax 'brace-list-entry (point))
8434 ))))
8436 ;; CASE 10: A continued statement or top level construct.
8437 ((and (not (memq char-before-ip '(?\; ?:)))
8438 (not (c-at-vsemi-p before-ws-ip))
8439 (or (not (eq char-before-ip ?}))
8440 (c-looking-at-inexpr-block-backward c-state-cache))
8441 (> (point)
8442 (save-excursion
8443 (c-beginning-of-statement-1 containing-sexp)
8444 (setq placeholder (point))))
8445 (/= placeholder containing-sexp))
8446 ;; This is shared with case 18.
8447 (c-guess-continued-construct indent-point
8448 char-after-ip
8449 placeholder
8450 containing-sexp
8451 paren-state))
8453 ;; CASE 16: block close brace, possibly closing the defun or
8454 ;; the class
8455 ((eq char-after-ip ?})
8456 ;; From here on we have the next containing sexp in lim.
8457 (setq lim (c-most-enclosing-brace paren-state))
8458 (goto-char containing-sexp)
8459 (cond
8461 ;; CASE 16E: Closing a statement block? This catches
8462 ;; cases where it's preceded by a statement keyword,
8463 ;; which works even when used in an "invalid" context,
8464 ;; e.g. a macro argument.
8465 ((c-after-conditional)
8466 (c-backward-to-block-anchor lim)
8467 (c-add-stmt-syntax 'block-close nil t lim paren-state))
8469 ;; CASE 16A: closing a lambda defun or an in-expression
8470 ;; block? C.f. cases 4, 7B and 17E.
8471 ((setq placeholder (c-looking-at-inexpr-block
8472 (c-safe-position containing-sexp paren-state)
8473 nil))
8474 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8475 'inline-close
8476 'block-close))
8477 (goto-char containing-sexp)
8478 (back-to-indentation)
8479 (if (= containing-sexp (point))
8480 (c-add-syntax tmpsymbol (point))
8481 (goto-char (cdr placeholder))
8482 (back-to-indentation)
8483 (c-add-stmt-syntax tmpsymbol nil t
8484 (c-most-enclosing-brace paren-state (point))
8485 paren-state)
8486 (if (/= (point) (cdr placeholder))
8487 (c-add-syntax (car placeholder)))))
8489 ;; CASE 16B: does this close an inline or a function in
8490 ;; a non-class declaration level block?
8491 ((save-excursion
8492 (and lim
8493 (progn
8494 (goto-char lim)
8495 (c-looking-at-decl-block
8496 (c-most-enclosing-brace paren-state lim)
8497 nil))
8498 (setq placeholder (point))))
8499 (c-backward-to-decl-anchor lim)
8500 (back-to-indentation)
8501 (if (save-excursion
8502 (goto-char placeholder)
8503 (looking-at c-other-decl-block-key))
8504 (c-add-syntax 'defun-close (point))
8505 (c-add-syntax 'inline-close (point))))
8507 ;; CASE 16F: Can be a defun-close of a function declared
8508 ;; in a statement block, e.g. in Pike or when using gcc
8509 ;; extensions, but watch out for macros followed by
8510 ;; blocks. Let it through to be handled below.
8511 ;; C.f. cases B.3 and 17G.
8512 ((save-excursion
8513 (and (not (c-at-statement-start-p))
8514 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8515 (setq placeholder (point))
8516 (let ((c-recognize-typeless-decls nil))
8517 ;; Turn off recognition of constructs that
8518 ;; lacks a type in this case, since that's more
8519 ;; likely to be a macro followed by a block.
8520 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8521 (back-to-indentation)
8522 (if (/= (point) containing-sexp)
8523 (goto-char placeholder))
8524 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
8526 ;; CASE 16C: If there is an enclosing brace then this is
8527 ;; a block close since defun closes inside declaration
8528 ;; level blocks have been handled above.
8529 (lim
8530 ;; If the block is preceded by a case/switch label on
8531 ;; the same line, we anchor at the first preceding label
8532 ;; at boi. The default handling in c-add-stmt-syntax
8533 ;; really fixes it better, but we do like this to keep
8534 ;; the indentation compatible with version 5.28 and
8535 ;; earlier. C.f. case 17H.
8536 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8537 (eq (c-beginning-of-statement-1 lim) 'label)))
8538 (goto-char placeholder)
8539 (if (looking-at c-label-kwds-regexp)
8540 (c-add-syntax 'block-close (point))
8541 (goto-char containing-sexp)
8542 ;; c-backward-to-block-anchor not necessary here; those
8543 ;; situations are handled in case 16E above.
8544 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
8546 ;; CASE 16D: Only top level defun close left.
8548 (goto-char containing-sexp)
8549 (c-backward-to-decl-anchor lim)
8550 (c-add-stmt-syntax 'defun-close nil nil
8551 (c-most-enclosing-brace paren-state)
8552 paren-state))
8555 ;; CASE 17: Statement or defun catchall.
8557 (goto-char indent-point)
8558 ;; Back up statements until we find one that starts at boi.
8559 (while (let* ((prev-point (point))
8560 (last-step-type (c-beginning-of-statement-1
8561 containing-sexp)))
8562 (if (= (point) prev-point)
8563 (progn
8564 (setq step-type (or step-type last-step-type))
8565 nil)
8566 (setq step-type last-step-type)
8567 (/= (point) (c-point 'boi)))))
8568 (cond
8570 ;; CASE 17B: continued statement
8571 ((and (eq step-type 'same)
8572 (/= (point) indent-point))
8573 (c-add-stmt-syntax 'statement-cont nil nil
8574 containing-sexp paren-state))
8576 ;; CASE 17A: After a case/default label?
8577 ((progn
8578 (while (and (eq step-type 'label)
8579 (not (looking-at c-label-kwds-regexp)))
8580 (setq step-type
8581 (c-beginning-of-statement-1 containing-sexp)))
8582 (eq step-type 'label))
8583 (c-add-stmt-syntax (if (eq char-after-ip ?{)
8584 'statement-case-open
8585 'statement-case-intro)
8586 nil t containing-sexp paren-state))
8588 ;; CASE 17D: any old statement
8589 ((progn
8590 (while (eq step-type 'label)
8591 (setq step-type
8592 (c-beginning-of-statement-1 containing-sexp)))
8593 (eq step-type 'previous))
8594 (c-add-stmt-syntax 'statement nil t
8595 containing-sexp paren-state)
8596 (if (eq char-after-ip ?{)
8597 (c-add-syntax 'block-open)))
8599 ;; CASE 17I: Inside a substatement block.
8600 ((progn
8601 ;; The following tests are all based on containing-sexp.
8602 (goto-char containing-sexp)
8603 ;; From here on we have the next containing sexp in lim.
8604 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
8605 (c-after-conditional))
8606 (c-backward-to-block-anchor lim)
8607 (c-add-stmt-syntax 'statement-block-intro nil t
8608 lim paren-state)
8609 (if (eq char-after-ip ?{)
8610 (c-add-syntax 'block-open)))
8612 ;; CASE 17E: first statement in an in-expression block.
8613 ;; C.f. cases 4, 7B and 16A.
8614 ((setq placeholder (c-looking-at-inexpr-block
8615 (c-safe-position containing-sexp paren-state)
8616 nil))
8617 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8618 'defun-block-intro
8619 'statement-block-intro))
8620 (back-to-indentation)
8621 (if (= containing-sexp (point))
8622 (c-add-syntax tmpsymbol (point))
8623 (goto-char (cdr placeholder))
8624 (back-to-indentation)
8625 (c-add-stmt-syntax tmpsymbol nil t
8626 (c-most-enclosing-brace c-state-cache (point))
8627 paren-state)
8628 (if (/= (point) (cdr placeholder))
8629 (c-add-syntax (car placeholder))))
8630 (if (eq char-after-ip ?{)
8631 (c-add-syntax 'block-open)))
8633 ;; CASE 17F: first statement in an inline, or first
8634 ;; statement in a top-level defun. we can tell this is it
8635 ;; if there are no enclosing braces that haven't been
8636 ;; narrowed out by a class (i.e. don't use bod here).
8637 ((save-excursion
8638 (or (not (setq placeholder (c-most-enclosing-brace
8639 paren-state)))
8640 (and (progn
8641 (goto-char placeholder)
8642 (eq (char-after) ?{))
8643 (c-looking-at-decl-block (c-most-enclosing-brace
8644 paren-state (point))
8645 nil))))
8646 (c-backward-to-decl-anchor lim)
8647 (back-to-indentation)
8648 (c-add-syntax 'defun-block-intro (point)))
8650 ;; CASE 17G: First statement in a function declared inside
8651 ;; a normal block. This can occur in Pike and with
8652 ;; e.g. the gcc extensions, but watch out for macros
8653 ;; followed by blocks. C.f. cases B.3 and 16F.
8654 ((save-excursion
8655 (and (not (c-at-statement-start-p))
8656 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8657 (setq placeholder (point))
8658 (let ((c-recognize-typeless-decls nil))
8659 ;; Turn off recognition of constructs that lacks
8660 ;; a type in this case, since that's more likely
8661 ;; to be a macro followed by a block.
8662 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8663 (back-to-indentation)
8664 (if (/= (point) containing-sexp)
8665 (goto-char placeholder))
8666 (c-add-stmt-syntax 'defun-block-intro nil t
8667 lim paren-state))
8669 ;; CASE 17H: First statement in a block.
8671 ;; If the block is preceded by a case/switch label on the
8672 ;; same line, we anchor at the first preceding label at
8673 ;; boi. The default handling in c-add-stmt-syntax is
8674 ;; really fixes it better, but we do like this to keep the
8675 ;; indentation compatible with version 5.28 and earlier.
8676 ;; C.f. case 16C.
8677 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8678 (eq (c-beginning-of-statement-1 lim) 'label)))
8679 (goto-char placeholder)
8680 (if (looking-at c-label-kwds-regexp)
8681 (c-add-syntax 'statement-block-intro (point))
8682 (goto-char containing-sexp)
8683 ;; c-backward-to-block-anchor not necessary here; those
8684 ;; situations are handled in case 17I above.
8685 (c-add-stmt-syntax 'statement-block-intro nil t
8686 lim paren-state))
8687 (if (eq char-after-ip ?{)
8688 (c-add-syntax 'block-open)))
8692 ;; now we need to look at any modifiers
8693 (goto-char indent-point)
8694 (skip-chars-forward " \t")
8696 ;; are we looking at a comment only line?
8697 (when (and (looking-at c-comment-start-regexp)
8698 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
8699 (c-append-syntax 'comment-intro))
8701 ;; we might want to give additional offset to friends (in C++).
8702 (when (and c-opt-friend-key
8703 (looking-at c-opt-friend-key))
8704 (c-append-syntax 'friend))
8706 ;; Set syntactic-relpos.
8707 (let ((p c-syntactic-context))
8708 (while (and p
8709 (if (integerp (c-langelem-pos (car p)))
8710 (progn
8711 (setq syntactic-relpos (c-langelem-pos (car p)))
8712 nil)
8714 (setq p (cdr p))))
8716 ;; Start of or a continuation of a preprocessor directive?
8717 (if (and macro-start
8718 (eq macro-start (c-point 'boi))
8719 (not (and (c-major-mode-is 'pike-mode)
8720 (eq (char-after (1+ macro-start)) ?\"))))
8721 (c-append-syntax 'cpp-macro)
8722 (when (and c-syntactic-indentation-in-macros macro-start)
8723 (if in-macro-expr
8724 (when (or
8725 (< syntactic-relpos macro-start)
8726 (not (or
8727 (assq 'arglist-intro c-syntactic-context)
8728 (assq 'arglist-cont c-syntactic-context)
8729 (assq 'arglist-cont-nonempty c-syntactic-context)
8730 (assq 'arglist-close c-syntactic-context))))
8731 ;; If inside a cpp expression, i.e. anywhere in a
8732 ;; cpp directive except a #define body, we only let
8733 ;; through the syntactic analysis that is internal
8734 ;; in the expression. That means the arglist
8735 ;; elements, if they are anchored inside the cpp
8736 ;; expression.
8737 (setq c-syntactic-context nil)
8738 (c-add-syntax 'cpp-macro-cont macro-start))
8739 (when (and (eq macro-start syntactic-relpos)
8740 (not (assq 'cpp-define-intro c-syntactic-context))
8741 (save-excursion
8742 (goto-char macro-start)
8743 (or (not (c-forward-to-cpp-define-body))
8744 (<= (point) (c-point 'boi indent-point)))))
8745 ;; Inside a #define body and the syntactic analysis is
8746 ;; anchored on the start of the #define. In this case
8747 ;; we add cpp-define-intro to get the extra
8748 ;; indentation of the #define body.
8749 (c-add-syntax 'cpp-define-intro)))))
8751 ;; return the syntax
8752 c-syntactic-context)))
8755 ;; Indentation calculation.
8757 (defun c-evaluate-offset (offset langelem symbol)
8758 ;; offset can be a number, a function, a variable, a list, or one of
8759 ;; the symbols + or -
8761 ;; This function might do hidden buffer changes.
8762 (let ((res
8763 (cond
8764 ((numberp offset) offset)
8765 ((vectorp offset) offset)
8766 ((null offset) nil)
8768 ((eq offset '+) c-basic-offset)
8769 ((eq offset '-) (- c-basic-offset))
8770 ((eq offset '++) (* 2 c-basic-offset))
8771 ((eq offset '--) (* 2 (- c-basic-offset)))
8772 ((eq offset '*) (/ c-basic-offset 2))
8773 ((eq offset '/) (/ (- c-basic-offset) 2))
8775 ((functionp offset)
8776 (c-evaluate-offset
8777 (funcall offset
8778 (cons (c-langelem-sym langelem)
8779 (c-langelem-pos langelem)))
8780 langelem symbol))
8782 ((listp offset)
8783 (cond
8784 ((eq (car offset) 'quote)
8785 (c-benign-error "The offset %S for %s was mistakenly quoted"
8786 offset symbol)
8787 nil)
8789 ((memq (car offset) '(min max))
8790 (let (res val (method (car offset)))
8791 (setq offset (cdr offset))
8792 (while offset
8793 (setq val (c-evaluate-offset (car offset) langelem symbol))
8794 (cond
8795 ((not val))
8796 ((not res)
8797 (setq res val))
8798 ((integerp val)
8799 (if (vectorp res)
8800 (c-benign-error "\
8801 Error evaluating offset %S for %s: \
8802 Cannot combine absolute offset %S with relative %S in `%s' method"
8803 (car offset) symbol res val method)
8804 (setq res (funcall method res val))))
8806 (if (integerp res)
8807 (c-benign-error "\
8808 Error evaluating offset %S for %s: \
8809 Cannot combine relative offset %S with absolute %S in `%s' method"
8810 (car offset) symbol res val method)
8811 (setq res (vector (funcall method (aref res 0)
8812 (aref val 0)))))))
8813 (setq offset (cdr offset)))
8814 res))
8816 ((eq (car offset) 'add)
8817 (let (res val)
8818 (setq offset (cdr offset))
8819 (while offset
8820 (setq val (c-evaluate-offset (car offset) langelem symbol))
8821 (cond
8822 ((not val))
8823 ((not res)
8824 (setq res val))
8825 ((integerp val)
8826 (if (vectorp res)
8827 (setq res (vector (+ (aref res 0) val)))
8828 (setq res (+ res val))))
8830 (if (vectorp res)
8831 (c-benign-error "\
8832 Error evaluating offset %S for %s: \
8833 Cannot combine absolute offsets %S and %S in `add' method"
8834 (car offset) symbol res val)
8835 (setq res val)))) ; Override.
8836 (setq offset (cdr offset)))
8837 res))
8840 (let (res)
8841 (when (eq (car offset) 'first)
8842 (setq offset (cdr offset)))
8843 (while (and (not res) offset)
8844 (setq res (c-evaluate-offset (car offset) langelem symbol)
8845 offset (cdr offset)))
8846 res))))
8848 ((and (symbolp offset) (boundp offset))
8849 (symbol-value offset))
8852 (c-benign-error "Unknown offset format %S for %s" offset symbol)
8853 nil))))
8855 (if (or (null res) (integerp res)
8856 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
8858 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
8859 offset symbol res)
8860 nil)))
8862 (defun c-calc-offset (langelem)
8863 ;; Get offset from LANGELEM which is a list beginning with the
8864 ;; syntactic symbol and followed by any analysis data it provides.
8865 ;; That data may be zero or more elements, but if at least one is
8866 ;; given then the first is the anchor position (or nil). The symbol
8867 ;; is matched against `c-offsets-alist' and the offset calculated
8868 ;; from that is returned.
8870 ;; This function might do hidden buffer changes.
8871 (let* ((symbol (c-langelem-sym langelem))
8872 (match (assq symbol c-offsets-alist))
8873 (offset (cdr-safe match)))
8874 (if match
8875 (setq offset (c-evaluate-offset offset langelem symbol))
8876 (if c-strict-syntax-p
8877 (c-benign-error "No offset found for syntactic symbol %s" symbol))
8878 (setq offset 0))
8879 (if (vectorp offset)
8880 offset
8881 (or (and (numberp offset) offset)
8882 (and (symbolp offset) (symbol-value offset))
8886 (defun c-get-offset (langelem)
8887 ;; This is a compatibility wrapper for `c-calc-offset' in case
8888 ;; someone is calling it directly. It takes an old style syntactic
8889 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
8890 ;; new list form.
8892 ;; This function might do hidden buffer changes.
8893 (if (c-langelem-pos langelem)
8894 (c-calc-offset (list (c-langelem-sym langelem)
8895 (c-langelem-pos langelem)))
8896 (c-calc-offset langelem)))
8898 (defun c-get-syntactic-indentation (langelems)
8899 ;; Calculate the syntactic indentation from a syntactic description
8900 ;; as returned by `c-guess-syntax'.
8902 ;; Note that topmost-intro always has an anchor position at bol, for
8903 ;; historical reasons. It's often used together with other symbols
8904 ;; that has more sane positions. Since we always use the first
8905 ;; found anchor position, we rely on that these other symbols always
8906 ;; precede topmost-intro in the LANGELEMS list.
8908 ;; This function might do hidden buffer changes.
8909 (let ((indent 0) anchor)
8911 (while langelems
8912 (let* ((c-syntactic-element (car langelems))
8913 (res (c-calc-offset c-syntactic-element)))
8915 (if (vectorp res)
8916 ;; Got an absolute column that overrides any indentation
8917 ;; we've collected so far, but not the relative
8918 ;; indentation we might get for the nested structures
8919 ;; further down the langelems list.
8920 (setq indent (elt res 0)
8921 anchor (point-min)) ; A position at column 0.
8923 ;; Got a relative change of the current calculated
8924 ;; indentation.
8925 (setq indent (+ indent res))
8927 ;; Use the anchor position from the first syntactic
8928 ;; element with one.
8929 (unless anchor
8930 (setq anchor (c-langelem-pos (car langelems)))))
8932 (setq langelems (cdr langelems))))
8934 (if anchor
8935 (+ indent (save-excursion
8936 (goto-char anchor)
8937 (current-column)))
8938 indent)))
8941 (cc-provide 'cc-engine)
8943 ;;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
8944 ;;; cc-engine.el ends here