Fix infinite loop with long macros.
[emacs.git] / lisp / progmodes / cc-engine.el
blob95b43e763d505398ff77252941103e75423fe5c4
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985, 1987, 1992-2012 Free Software Foundation, Inc.
5 ;; Authors: 2001- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
14 ;; Package: cc-mode
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;;; Commentary:
33 ;; The functions which have docstring documentation can be considered
34 ;; part of an API which other packages can use in CC Mode buffers.
35 ;; Otoh, undocumented functions and functions with the documentation
36 ;; in comments are considered purely internal and can change semantics
37 ;; or even disappear in the future.
39 ;; (This policy applies to CC Mode as a whole, not just this file. It
40 ;; probably also applies to many other Emacs packages, but here it's
41 ;; clearly spelled out.)
43 ;; Hidden buffer changes
45 ;; Various functions in CC Mode use text properties for caching and
46 ;; syntactic markup purposes, and those of them that might modify such
47 ;; properties but still don't modify the buffer in a visible way are
48 ;; said to do "hidden buffer changes". They should be used within
49 ;; `c-save-buffer-state' or a similar function that saves and restores
50 ;; buffer modifiedness, disables buffer change hooks, etc.
52 ;; Interactive functions are assumed to not do hidden buffer changes,
53 ;; except in the specific parts of them that do real changes.
55 ;; Lineup functions are assumed to do hidden buffer changes. They
56 ;; must not do real changes, though.
58 ;; All other functions that do hidden buffer changes have that noted
59 ;; in their doc string or comment.
61 ;; The intention with this system is to avoid wrapping every leaf
62 ;; function that do hidden buffer changes inside
63 ;; `c-save-buffer-state'. It should be used as near the top of the
64 ;; interactive functions as possible.
66 ;; Functions called during font locking are allowed to do hidden
67 ;; buffer changes since the font-lock package run them in a context
68 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
69 ;; inspired by `save-buffer-state' in the font-lock package).
71 ;; Use of text properties
73 ;; CC Mode uses several text properties internally to mark up various
74 ;; positions, e.g. to improve speed and to eliminate glitches in
75 ;; interactive refontification.
77 ;; Note: This doc is for internal use only. Other packages should not
78 ;; assume that these text properties are used as described here.
80 ;; 'category
81 ;; Used for "indirection". With its help, some other property can
82 ;; be cheaply and easily switched on or off everywhere it occurs.
84 ;; 'syntax-table
85 ;; Used to modify the syntax of some characters. It is used to
86 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
87 ;; to "hide" obtrusive characters in preprocessor lines.
89 ;; This property is used on single characters and is therefore
90 ;; always treated as front and rear nonsticky (or start and end open
91 ;; in XEmacs vocabulary). It's therefore installed on
92 ;; `text-property-default-nonsticky' if that variable exists (Emacs
93 ;; >= 21).
95 ;; 'c-is-sws and 'c-in-sws
96 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
97 ;; speed them up. See the comment blurb before `c-put-is-sws'
98 ;; below for further details.
100 ;; 'c-type
101 ;; This property is used on single characters to mark positions with
102 ;; special syntactic relevance of various sorts. Its primary use is
103 ;; to avoid glitches when multiline constructs are refontified
104 ;; interactively (on font lock decoration level 3). It's cleared in
105 ;; a region before it's fontified and is then put on relevant chars
106 ;; in that region as they are encountered during the fontification.
107 ;; The value specifies the kind of position:
109 ;; 'c-decl-arg-start
110 ;; Put on the last char of the token preceding each declaration
111 ;; inside a declaration style arglist (typically in a function
112 ;; prototype).
114 ;; 'c-decl-end
115 ;; Put on the last char of the token preceding a declaration.
116 ;; This is used in cases where declaration boundaries can't be
117 ;; recognized simply by looking for a token like ";" or "}".
118 ;; `c-type-decl-end-used' must be set if this is used (see also
119 ;; `c-find-decl-spots').
121 ;; 'c-<>-arg-sep
122 ;; Put on the commas that separate arguments in angle bracket
123 ;; arglists like C++ template arglists.
125 ;; 'c-decl-id-start and 'c-decl-type-start
126 ;; Put on the last char of the token preceding each declarator
127 ;; in the declarator list of a declaration. They are also used
128 ;; between the identifiers cases like enum declarations.
129 ;; 'c-decl-type-start is used when the declarators are types,
130 ;; 'c-decl-id-start otherwise.
132 ;; 'c-awk-NL-prop
133 ;; Used in AWK mode to mark the various kinds of newlines. See
134 ;; cc-awk.el.
136 ;;; Code:
138 (eval-when-compile
139 (let ((load-path
140 (if (and (boundp 'byte-compile-dest-file)
141 (stringp byte-compile-dest-file))
142 (cons (file-name-directory byte-compile-dest-file) load-path)
143 load-path)))
144 (load "cc-bytecomp" nil t)))
146 (cc-require 'cc-defs)
147 (cc-require-when-compile 'cc-langs)
148 (cc-require 'cc-vars)
150 ;; Silence the compiler.
151 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
154 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
156 (defmacro c-declare-lang-variables ()
157 `(progn
158 ,@(apply 'nconc
159 (mapcar (lambda (init)
160 `(,(if (elt init 2)
161 `(defvar ,(car init) nil ,(elt init 2))
162 `(defvar ,(car init) nil))
163 (make-variable-buffer-local ',(car init))))
164 (cdr c-lang-variable-inits)))))
165 (c-declare-lang-variables)
168 ;;; Internal state variables.
170 ;; Internal state of hungry delete key feature
171 (defvar c-hungry-delete-key nil)
172 (make-variable-buffer-local 'c-hungry-delete-key)
174 ;; The electric flag (toggled by `c-toggle-electric-state').
175 ;; If t, electric actions (like automatic reindentation, and (if
176 ;; c-auto-newline is also set) auto newlining) will happen when an electric
177 ;; key like `{' is pressed (or an electric keyword like `else').
178 (defvar c-electric-flag t)
179 (make-variable-buffer-local 'c-electric-flag)
181 ;; Internal state of auto newline feature.
182 (defvar c-auto-newline nil)
183 (make-variable-buffer-local 'c-auto-newline)
185 ;; Included in the mode line to indicate the active submodes.
186 ;; (defvar c-submode-indicators nil)
187 ;; (make-variable-buffer-local 'c-submode-indicators)
189 (defun c-calculate-state (arg prevstate)
190 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
191 ;; arg is nil or zero, toggle the state. If arg is negative, turn
192 ;; the state off, and if arg is positive, turn the state on
193 (if (or (not arg)
194 (zerop (setq arg (prefix-numeric-value arg))))
195 (not prevstate)
196 (> arg 0)))
199 ;; Basic handling of preprocessor directives.
201 ;; This is a dynamically bound cache used together with
202 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
203 ;; works as long as point doesn't cross a macro boundary.
204 (defvar c-macro-start 'unknown)
206 (defsubst c-query-and-set-macro-start ()
207 (if (symbolp c-macro-start)
208 (setq c-macro-start (save-excursion
209 (c-save-buffer-state ()
210 (and (c-beginning-of-macro)
211 (point)))))
212 c-macro-start))
214 (defsubst c-query-macro-start ()
215 (if (symbolp c-macro-start)
216 (save-excursion
217 (c-save-buffer-state ()
218 (and (c-beginning-of-macro)
219 (point))))
220 c-macro-start))
222 (defun c-beginning-of-macro (&optional lim)
223 "Go to the beginning of a preprocessor directive.
224 Leave point at the beginning of the directive and return t if in one,
225 otherwise return nil and leave point unchanged.
227 Note that this function might do hidden buffer changes. See the
228 comment at the start of cc-engine.el for more info."
229 (when c-opt-cpp-prefix
230 (let ((here (point)))
231 (save-restriction
232 (if lim (narrow-to-region lim (point-max)))
233 (beginning-of-line)
234 (while (eq (char-before (1- (point))) ?\\)
235 (forward-line -1))
236 (back-to-indentation)
237 (if (and (<= (point) here)
238 (looking-at c-opt-cpp-start))
240 (goto-char here)
241 nil)))))
243 (defun c-end-of-macro ()
244 "Go to the end of a preprocessor directive.
245 More accurately, move the point to the end of the closest following
246 line that doesn't end with a line continuation backslash - no check is
247 done that the point is inside a cpp directive to begin with.
249 Note that this function might do hidden buffer changes. See the
250 comment at the start of cc-engine.el for more info."
251 (while (progn
252 (end-of-line)
253 (when (and (eq (char-before) ?\\)
254 (not (eobp)))
255 (forward-char)
256 t))))
258 (defun c-syntactic-end-of-macro ()
259 ;; Go to the end of a CPP directive, or a "safe" pos just before.
261 ;; This is normally the end of the next non-escaped line. A "safe"
262 ;; position is one not within a string or comment. (The EOL on a line
263 ;; comment is NOT "safe").
265 ;; This function must only be called from the beginning of a CPP construct.
267 ;; Note that this function might do hidden buffer changes. See the comment
268 ;; at the start of cc-engine.el for more info.
269 (let* ((here (point))
270 (there (progn (c-end-of-macro) (point)))
271 (s (parse-partial-sexp here there)))
272 (while (and (or (nth 3 s) ; in a string
273 (nth 4 s)) ; in a comment (maybe at end of line comment)
274 (> there here)) ; No infinite loops, please.
275 (setq there (1- (nth 8 s)))
276 (setq s (parse-partial-sexp here there)))
277 (point)))
279 (defun c-forward-over-cpp-define-id ()
280 ;; Assuming point is at the "#" that introduces a preprocessor
281 ;; directive, it's moved forward to the end of the identifier which is
282 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
283 ;; is returned in this case, in all other cases nil is returned and
284 ;; point isn't moved.
286 ;; This function might do hidden buffer changes.
287 (when (and c-opt-cpp-macro-define-id
288 (looking-at c-opt-cpp-macro-define-id))
289 (goto-char (match-end 0))))
291 (defun c-forward-to-cpp-define-body ()
292 ;; Assuming point is at the "#" that introduces a preprocessor
293 ;; directive, it's moved forward to the start of the definition body
294 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
295 ;; specifies). Non-nil is returned in this case, in all other cases
296 ;; nil is returned and point isn't moved.
298 ;; This function might do hidden buffer changes.
299 (when (and c-opt-cpp-macro-define-start
300 (looking-at c-opt-cpp-macro-define-start)
301 (not (= (match-end 0) (c-point 'eol))))
302 (goto-char (match-end 0))))
305 ;;; Basic utility functions.
307 (defun c-syntactic-content (from to paren-level)
308 ;; Return the given region as a string where all syntactic
309 ;; whitespace is removed or, where necessary, replaced with a single
310 ;; space. If PAREN-LEVEL is given then all parens in the region are
311 ;; collapsed to "()", "[]" etc.
313 ;; This function might do hidden buffer changes.
315 (save-excursion
316 (save-restriction
317 (narrow-to-region from to)
318 (goto-char from)
319 (let* ((parts (list nil)) (tail parts) pos in-paren)
321 (while (re-search-forward c-syntactic-ws-start to t)
322 (goto-char (setq pos (match-beginning 0)))
323 (c-forward-syntactic-ws)
324 (if (= (point) pos)
325 (forward-char)
327 (when paren-level
328 (save-excursion
329 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
330 pos (point))))
332 (if (and (> pos from)
333 (< (point) to)
334 (looking-at "\\w\\|\\s_")
335 (save-excursion
336 (goto-char (1- pos))
337 (looking-at "\\w\\|\\s_")))
338 (progn
339 (setcdr tail (list (buffer-substring-no-properties from pos)
340 " "))
341 (setq tail (cddr tail)))
342 (setcdr tail (list (buffer-substring-no-properties from pos)))
343 (setq tail (cdr tail)))
345 (when in-paren
346 (when (= (car (parse-partial-sexp pos to -1)) -1)
347 (setcdr tail (list (buffer-substring-no-properties
348 (1- (point)) (point))))
349 (setq tail (cdr tail))))
351 (setq from (point))))
353 (setcdr tail (list (buffer-substring-no-properties from to)))
354 (apply 'concat (cdr parts))))))
356 (defun c-shift-line-indentation (shift-amt)
357 ;; Shift the indentation of the current line with the specified
358 ;; amount (positive inwards). The buffer is modified only if
359 ;; SHIFT-AMT isn't equal to zero.
360 (let ((pos (- (point-max) (point)))
361 (c-macro-start c-macro-start)
362 tmp-char-inserted)
363 (if (zerop shift-amt)
365 ;; If we're on an empty line inside a macro, we take the point
366 ;; to be at the current indentation and shift it to the
367 ;; appropriate column. This way we don't treat the extra
368 ;; whitespace out to the line continuation as indentation.
369 (when (and (c-query-and-set-macro-start)
370 (looking-at "[ \t]*\\\\$")
371 (save-excursion
372 (skip-chars-backward " \t")
373 (bolp)))
374 (insert ?x)
375 (backward-char)
376 (setq tmp-char-inserted t))
377 (unwind-protect
378 (let ((col (current-indentation)))
379 (delete-region (c-point 'bol) (c-point 'boi))
380 (beginning-of-line)
381 (indent-to (+ col shift-amt)))
382 (when tmp-char-inserted
383 (delete-char 1))))
384 ;; If initial point was within line's indentation and we're not on
385 ;; a line with a line continuation in a macro, position after the
386 ;; indentation. Else stay at same point in text.
387 (if (and (< (point) (c-point 'boi))
388 (not tmp-char-inserted))
389 (back-to-indentation)
390 (if (> (- (point-max) pos) (point))
391 (goto-char (- (point-max) pos))))))
393 (defsubst c-keyword-sym (keyword)
394 ;; Return non-nil if the string KEYWORD is a known keyword. More
395 ;; precisely, the value is the symbol for the keyword in
396 ;; `c-keywords-obarray'.
397 (intern-soft keyword c-keywords-obarray))
399 (defsubst c-keyword-member (keyword-sym lang-constant)
400 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
401 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
402 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
403 ;; nil then the result is nil.
404 (get keyword-sym lang-constant))
406 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
407 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
408 "\"|"
409 "\""))
411 ;; Regexp matching string limit syntax.
412 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
413 "\\s\"\\|\\s|"
414 "\\s\""))
416 ;; Regexp matching WS followed by string limit syntax.
417 (defconst c-ws*-string-limit-regexp
418 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
420 ;; Holds formatted error strings for the few cases where parse errors
421 ;; are reported.
422 (defvar c-parsing-error nil)
423 (make-variable-buffer-local 'c-parsing-error)
425 (defun c-echo-parsing-error (&optional quiet)
426 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
427 (c-benign-error "%s" c-parsing-error))
428 c-parsing-error)
430 ;; Faces given to comments and string literals. This is used in some
431 ;; situations to speed up recognition; it isn't mandatory that font
432 ;; locking is in use. This variable is extended with the face in
433 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
434 (defvar c-literal-faces
435 (append '(font-lock-comment-face font-lock-string-face)
436 (when (facep 'font-lock-comment-delimiter-face)
437 ;; New in Emacs 22.
438 '(font-lock-comment-delimiter-face))))
440 (defsubst c-put-c-type-property (pos value)
441 ;; Put a c-type property with the given value at POS.
442 (c-put-char-property pos 'c-type value))
444 (defun c-clear-c-type-property (from to value)
445 ;; Remove all occurrences of the c-type property that has the given
446 ;; value in the region between FROM and TO. VALUE is assumed to not
447 ;; be nil.
449 ;; Note: This assumes that c-type is put on single chars only; it's
450 ;; very inefficient if matching properties cover large regions.
451 (save-excursion
452 (goto-char from)
453 (while (progn
454 (when (eq (get-text-property (point) 'c-type) value)
455 (c-clear-char-property (point) 'c-type))
456 (goto-char (next-single-property-change (point) 'c-type nil to))
457 (< (point) to)))))
460 ;; Some debug tools to visualize various special positions. This
461 ;; debug code isn't as portable as the rest of CC Mode.
463 (cc-bytecomp-defun overlays-in)
464 (cc-bytecomp-defun overlay-get)
465 (cc-bytecomp-defun overlay-start)
466 (cc-bytecomp-defun overlay-end)
467 (cc-bytecomp-defun delete-overlay)
468 (cc-bytecomp-defun overlay-put)
469 (cc-bytecomp-defun make-overlay)
471 (defun c-debug-add-face (beg end face)
472 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
473 (while overlays
474 (setq overlay (car overlays)
475 overlays (cdr overlays))
476 (when (eq (overlay-get overlay 'face) face)
477 (setq beg (min beg (overlay-start overlay))
478 end (max end (overlay-end overlay)))
479 (delete-overlay overlay)))
480 (overlay-put (make-overlay beg end) 'face face)))
482 (defun c-debug-remove-face (beg end face)
483 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
484 (ol-beg beg) (ol-end end))
485 (while overlays
486 (setq overlay (car overlays)
487 overlays (cdr overlays))
488 (when (eq (overlay-get overlay 'face) face)
489 (setq ol-beg (min ol-beg (overlay-start overlay))
490 ol-end (max ol-end (overlay-end overlay)))
491 (delete-overlay overlay)))
492 (when (< ol-beg beg)
493 (overlay-put (make-overlay ol-beg beg) 'face face))
494 (when (> ol-end end)
495 (overlay-put (make-overlay end ol-end) 'face face))))
498 ;; `c-beginning-of-statement-1' and accompanying stuff.
500 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
501 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
502 ;; better way should be implemented, but this will at least shut up
503 ;; the byte compiler.
504 (defvar c-maybe-labelp)
506 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
508 ;; Macros used internally in c-beginning-of-statement-1 for the
509 ;; automaton actions.
510 (defmacro c-bos-push-state ()
511 '(setq stack (cons (cons state saved-pos)
512 stack)))
513 (defmacro c-bos-pop-state (&optional do-if-done)
514 `(if (setq state (car (car stack))
515 saved-pos (cdr (car stack))
516 stack (cdr stack))
518 ,do-if-done
519 (throw 'loop nil)))
520 (defmacro c-bos-pop-state-and-retry ()
521 '(throw 'loop (setq state (car (car stack))
522 saved-pos (cdr (car stack))
523 ;; Throw nil if stack is empty, else throw non-nil.
524 stack (cdr stack))))
525 (defmacro c-bos-save-pos ()
526 '(setq saved-pos (vector pos tok ptok pptok)))
527 (defmacro c-bos-restore-pos ()
528 '(unless (eq (elt saved-pos 0) start)
529 (setq pos (elt saved-pos 0)
530 tok (elt saved-pos 1)
531 ptok (elt saved-pos 2)
532 pptok (elt saved-pos 3))
533 (goto-char pos)
534 (setq sym nil)))
535 (defmacro c-bos-save-error-info (missing got)
536 `(setq saved-pos (vector pos ,missing ,got)))
537 (defmacro c-bos-report-error ()
538 '(unless noerror
539 (setq c-parsing-error
540 (format "No matching `%s' found for `%s' on line %d"
541 (elt saved-pos 1)
542 (elt saved-pos 2)
543 (1+ (count-lines (point-min)
544 (c-point 'bol (elt saved-pos 0))))))))
546 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
547 noerror comma-delim)
548 "Move to the start of the current statement or declaration, or to
549 the previous one if already at the beginning of one. Only
550 statements/declarations on the same level are considered, i.e. don't
551 move into or out of sexps (not even normal expression parentheses).
553 If point is already at the earliest statement within braces or parens,
554 this function doesn't move back into any whitespace preceding it; it
555 returns 'same in this case.
557 Stop at statement continuation tokens like \"else\", \"catch\",
558 \"finally\" and the \"while\" in \"do ... while\" if the start point
559 is within the continuation. If starting at such a token, move to the
560 corresponding statement start. If at the beginning of a statement,
561 move to the closest containing statement if there is any. This might
562 also stop at a continuation clause.
564 Labels are treated as part of the following statements if
565 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
566 statement start keyword.) Otherwise, each label is treated as a
567 separate statement.
569 Macros are ignored \(i.e. skipped over) unless point is within one, in
570 which case the content of the macro is treated as normal code. Aside
571 from any normal statement starts found in it, stop at the first token
572 of the content in the macro, i.e. the expression of an \"#if\" or the
573 start of the definition in a \"#define\". Also stop at start of
574 macros before leaving them.
576 Return:
577 'label if stopped at a label or \"case...:\" or \"default:\";
578 'same if stopped at the beginning of the current statement;
579 'up if stepped to a containing statement;
580 'previous if stepped to a preceding statement;
581 'beginning if stepped from a statement continuation clause to
582 its start clause; or
583 'macro if stepped to a macro start.
584 Note that 'same and not 'label is returned if stopped at the same
585 label without crossing the colon character.
587 LIM may be given to limit the search. If the search hits the limit,
588 point will be left at the closest following token, or at the start
589 position if that is less ('same is returned in this case).
591 NOERROR turns off error logging to `c-parsing-error'.
593 Normally only ';' and virtual semicolons are considered to delimit
594 statements, but if COMMA-DELIM is non-nil then ',' is treated
595 as a delimiter too.
597 Note that this function might do hidden buffer changes. See the
598 comment at the start of cc-engine.el for more info."
600 ;; The bulk of this function is a pushdown automaton that looks at statement
601 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
602 ;; purpose is to keep track of nested statements, ensuring that such
603 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
604 ;; does with nested braces/brackets/parentheses).
606 ;; Note: The position of a boundary is the following token.
608 ;; Beginning with the current token (the one following point), move back one
609 ;; sexp at a time (where a sexp is, more or less, either a token or the
610 ;; entire contents of a brace/bracket/paren pair). Each time a statement
611 ;; boundary is crossed or a "while"-like token is found, update the state of
612 ;; the PDA. Stop at the beginning of a statement when the stack (holding
613 ;; nested statement info) is empty and the position has been moved.
615 ;; The following variables constitute the PDA:
617 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
618 ;; scanned back over, 'boundary if we've just gone back over a
619 ;; statement boundary, or nil otherwise.
620 ;; state: takes one of the values (nil else else-boundary while
621 ;; while-boundary catch catch-boundary).
622 ;; nil means "no "while"-like token yet scanned".
623 ;; 'else, for example, means "just gone back over an else".
624 ;; 'else-boundary means "just gone back over a statement boundary
625 ;; immediately after having gone back over an else".
626 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
627 ;; of error reporting information.
628 ;; stack: The stack onto which the PDA pushes its state. Each entry
629 ;; consists of a saved value of state and saved-pos. An entry is
630 ;; pushed when we move back over a "continuation" token (e.g. else)
631 ;; and popped when we encounter the corresponding opening token
632 ;; (e.g. if).
635 ;; The following diagram briefly outlines the PDA.
637 ;; Common state:
638 ;; "else": Push state, goto state `else'.
639 ;; "while": Push state, goto state `while'.
640 ;; "catch" or "finally": Push state, goto state `catch'.
641 ;; boundary: Pop state.
642 ;; other: Do nothing special.
644 ;; State `else':
645 ;; boundary: Goto state `else-boundary'.
646 ;; other: Error, pop state, retry token.
648 ;; State `else-boundary':
649 ;; "if": Pop state.
650 ;; boundary: Error, pop state.
651 ;; other: See common state.
653 ;; State `while':
654 ;; boundary: Save position, goto state `while-boundary'.
655 ;; other: Pop state, retry token.
657 ;; State `while-boundary':
658 ;; "do": Pop state.
659 ;; boundary: Restore position if it's not at start, pop state. [*see below]
660 ;; other: See common state.
662 ;; State `catch':
663 ;; boundary: Goto state `catch-boundary'.
664 ;; other: Error, pop state, retry token.
666 ;; State `catch-boundary':
667 ;; "try": Pop state.
668 ;; "catch": Goto state `catch'.
669 ;; boundary: Error, pop state.
670 ;; other: See common state.
672 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
673 ;; searching for a "do" which would have opened a do-while. If we didn't
674 ;; find it, we discard the analysis done since the "while", go back to this
675 ;; token in the buffer and restart the scanning there, this time WITHOUT
676 ;; pushing the 'while state onto the stack.
678 ;; In addition to the above there is some special handling of labels
679 ;; and macros.
681 (let ((case-fold-search nil)
682 (start (point))
683 macro-start
684 (delims (if comma-delim '(?\; ?,) '(?\;)))
685 (c-stmt-delim-chars (if comma-delim
686 c-stmt-delim-chars-with-comma
687 c-stmt-delim-chars))
688 c-in-literal-cache c-maybe-labelp after-case:-pos saved
689 ;; Current position.
691 ;; Position of last stmt boundary character (e.g. ;).
692 boundary-pos
693 ;; The position of the last sexp or bound that follows the
694 ;; first found colon, i.e. the start of the nonlabel part of
695 ;; the statement. It's `start' if a colon is found just after
696 ;; the start.
697 after-labels-pos
698 ;; Like `after-labels-pos', but the first such position inside
699 ;; a label, i.e. the start of the last label before the start
700 ;; of the nonlabel part of the statement.
701 last-label-pos
702 ;; The last position where a label is possible provided the
703 ;; statement started there. It's nil as long as no invalid
704 ;; label content has been found (according to
705 ;; `c-nonlabel-token-key'). It's `start' if no valid label
706 ;; content was found in the label. Note that we might still
707 ;; regard it a label if it starts with `c-label-kwds'.
708 label-good-pos
709 ;; Putative positions of the components of a bitfield declaration,
710 ;; e.g. "int foo : NUM_FOO_BITS ;"
711 bitfield-type-pos bitfield-id-pos bitfield-size-pos
712 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
713 ;; See above.
715 ;; Current state in the automaton. See above.
716 state
717 ;; Current saved positions. See above.
718 saved-pos
719 ;; Stack of conses (state . saved-pos).
720 stack
721 ;; Regexp which matches "for", "if", etc.
722 (cond-key (or c-opt-block-stmt-key
723 "\\<\\>")) ; Matches nothing.
724 ;; Return value.
725 (ret 'same)
726 ;; Positions of the last three sexps or bounds we've stopped at.
727 tok ptok pptok)
729 (save-restriction
730 (if lim (narrow-to-region lim (point-max)))
732 (if (save-excursion
733 (and (c-beginning-of-macro)
734 (/= (point) start)))
735 (setq macro-start (point)))
737 ;; Try to skip back over unary operator characters, to register
738 ;; that we've moved.
739 (while (progn
740 (setq pos (point))
741 (c-backward-syntactic-ws)
742 ;; Protect post-++/-- operators just before a virtual semicolon.
743 (and (not (c-at-vsemi-p))
744 (/= (skip-chars-backward "-+!*&~@`#") 0))))
746 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
747 ;; done. Later on we ignore the boundaries for statements that don't
748 ;; contain any sexp. The only thing that is affected is that the error
749 ;; checking is a little less strict, and we really don't bother.
750 (if (and (memq (char-before) delims)
751 (progn (forward-char -1)
752 (setq saved (point))
753 (c-backward-syntactic-ws)
754 (or (memq (char-before) delims)
755 (memq (char-before) '(?: nil))
756 (eq (char-syntax (char-before)) ?\()
757 (c-at-vsemi-p))))
758 (setq ret 'previous
759 pos saved)
761 ;; Begin at start and not pos to detect macros if we stand
762 ;; directly after the #.
763 (goto-char start)
764 (if (looking-at "\\<\\|\\W")
765 ;; Record this as the first token if not starting inside it.
766 (setq tok start))
769 ;; The following while loop goes back one sexp (balanced parens,
770 ;; etc. with contents, or symbol or suchlike) each iteration. This
771 ;; movement is accomplished with a call to c-backward-sexp approx 170
772 ;; lines below.
774 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
775 ;; 1. On reaching the start of a macro;
776 ;; 2. On having passed a stmt boundary with the PDA stack empty;
777 ;; 3. On reaching the start of an Objective C method def;
778 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
779 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
780 (while
781 (catch 'loop ;; Throw nil to break, non-nil to continue.
782 (cond
783 ;; Are we in a macro, just after the opening #?
784 ((save-excursion
785 (and macro-start ; Always NIL for AWK.
786 (progn (skip-chars-backward " \t")
787 (eq (char-before) ?#))
788 (progn (setq saved (1- (point)))
789 (beginning-of-line)
790 (not (eq (char-before (1- (point))) ?\\)))
791 (looking-at c-opt-cpp-start)
792 (progn (skip-chars-forward " \t")
793 (eq (point) saved))))
794 (goto-char saved)
795 (if (and (c-forward-to-cpp-define-body)
796 (progn (c-forward-syntactic-ws start)
797 (< (point) start)))
798 ;; Stop at the first token in the content of the macro.
799 (setq pos (point)
800 ignore-labels t) ; Avoid the label check on exit.
801 (setq pos saved
802 ret 'macro
803 ignore-labels t))
804 (throw 'loop nil)) ; 1. Start of macro.
806 ;; Do a round through the automaton if we've just passed a
807 ;; statement boundary or passed a "while"-like token.
808 ((or sym
809 (and (looking-at cond-key)
810 (setq sym (intern (match-string 1)))))
812 (when (and (< pos start) (null stack))
813 (throw 'loop nil)) ; 2. Statement boundary.
815 ;; The PDA state handling.
817 ;; Refer to the description of the PDA in the opening
818 ;; comments. In the following OR form, the first leaf
819 ;; attempts to handles one of the specific actions detailed
820 ;; (e.g., finding token "if" whilst in state `else-boundary').
821 ;; We drop through to the second leaf (which handles common
822 ;; state) if no specific handler is found in the first cond.
823 ;; If a parsing error is detected (e.g. an "else" with no
824 ;; preceding "if"), we throw to the enclosing catch.
826 ;; Note that the (eq state 'else) means
827 ;; "we've just passed an else", NOT "we're looking for an
828 ;; else".
829 (or (cond
830 ((eq state 'else)
831 (if (eq sym 'boundary)
832 (setq state 'else-boundary)
833 (c-bos-report-error)
834 (c-bos-pop-state-and-retry)))
836 ((eq state 'else-boundary)
837 (cond ((eq sym 'if)
838 (c-bos-pop-state (setq ret 'beginning)))
839 ((eq sym 'boundary)
840 (c-bos-report-error)
841 (c-bos-pop-state))))
843 ((eq state 'while)
844 (if (and (eq sym 'boundary)
845 ;; Since this can cause backtracking we do a
846 ;; little more careful analysis to avoid it:
847 ;; If there's a label in front of the while
848 ;; it can't be part of a do-while.
849 (not after-labels-pos))
850 (progn (c-bos-save-pos)
851 (setq state 'while-boundary))
852 (c-bos-pop-state-and-retry))) ; Can't be a do-while
854 ((eq state 'while-boundary)
855 (cond ((eq sym 'do)
856 (c-bos-pop-state (setq ret 'beginning)))
857 ((eq sym 'boundary) ; isn't a do-while
858 (c-bos-restore-pos) ; the position of the while
859 (c-bos-pop-state)))) ; no longer searching for do.
861 ((eq state 'catch)
862 (if (eq sym 'boundary)
863 (setq state 'catch-boundary)
864 (c-bos-report-error)
865 (c-bos-pop-state-and-retry)))
867 ((eq state 'catch-boundary)
868 (cond
869 ((eq sym 'try)
870 (c-bos-pop-state (setq ret 'beginning)))
871 ((eq sym 'catch)
872 (setq state 'catch))
873 ((eq sym 'boundary)
874 (c-bos-report-error)
875 (c-bos-pop-state)))))
877 ;; This is state common. We get here when the previous
878 ;; cond statement found no particular state handler.
879 (cond ((eq sym 'boundary)
880 ;; If we have a boundary at the start
881 ;; position we push a frame to go to the
882 ;; previous statement.
883 (if (>= pos start)
884 (c-bos-push-state)
885 (c-bos-pop-state)))
886 ((eq sym 'else)
887 (c-bos-push-state)
888 (c-bos-save-error-info 'if 'else)
889 (setq state 'else))
890 ((eq sym 'while)
891 ;; Is this a real while, or a do-while?
892 ;; The next `when' triggers unless we are SURE that
893 ;; the `while' is not the tail end of a `do-while'.
894 (when (or (not pptok)
895 (memq (char-after pptok) delims)
896 ;; The following kludge is to prevent
897 ;; infinite recursion when called from
898 ;; c-awk-after-if-for-while-condition-p,
899 ;; or the like.
900 (and (eq (point) start)
901 (c-vsemi-status-unknown-p))
902 (c-at-vsemi-p pptok))
903 ;; Since this can cause backtracking we do a
904 ;; little more careful analysis to avoid it: If
905 ;; the while isn't followed by a (possibly
906 ;; virtual) semicolon it can't be a do-while.
907 (c-bos-push-state)
908 (setq state 'while)))
909 ((memq sym '(catch finally))
910 (c-bos-push-state)
911 (c-bos-save-error-info 'try sym)
912 (setq state 'catch))))
914 (when c-maybe-labelp
915 ;; We're either past a statement boundary or at the
916 ;; start of a statement, so throw away any label data
917 ;; for the previous one.
918 (setq after-labels-pos nil
919 last-label-pos nil
920 c-maybe-labelp nil))))
922 ;; Step to the previous sexp, but not if we crossed a
923 ;; boundary, since that doesn't consume an sexp.
924 (if (eq sym 'boundary)
925 (setq ret 'previous)
927 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
928 ;; BACKWARDS THROUGH THE SOURCE.
930 (c-backward-syntactic-ws)
931 (let ((before-sws-pos (point))
932 ;; The end position of the area to search for statement
933 ;; barriers in this round.
934 (maybe-after-boundary-pos pos))
936 ;; Go back over exactly one logical sexp, taking proper
937 ;; account of macros and escaped EOLs.
938 (while
939 (progn
940 (unless (c-safe (c-backward-sexp) t)
941 ;; Give up if we hit an unbalanced block. Since the
942 ;; stack won't be empty the code below will report a
943 ;; suitable error.
944 (throw 'loop nil))
945 (cond
946 ;; Have we moved into a macro?
947 ((and (not macro-start)
948 (c-beginning-of-macro))
949 ;; Have we crossed a statement boundary? If not,
950 ;; keep going back until we find one or a "real" sexp.
951 (and
952 (save-excursion
953 (c-end-of-macro)
954 (not (c-crosses-statement-barrier-p
955 (point) maybe-after-boundary-pos)))
956 (setq maybe-after-boundary-pos (point))))
957 ;; Have we just gone back over an escaped NL? This
958 ;; doesn't count as a sexp.
959 ((looking-at "\\\\$")))))
961 ;; Have we crossed a statement boundary?
962 (setq boundary-pos
963 (cond
964 ;; Are we at a macro beginning?
965 ((and (not macro-start)
966 c-opt-cpp-prefix
967 (looking-at c-opt-cpp-prefix))
968 (save-excursion
969 (c-end-of-macro)
970 (c-crosses-statement-barrier-p
971 (point) maybe-after-boundary-pos)))
972 ;; Just gone back over a brace block?
973 ((and
974 (eq (char-after) ?{)
975 (not (c-looking-at-inexpr-block lim nil t)))
976 (save-excursion
977 (c-forward-sexp) (point)))
978 ;; Just gone back over some paren block?
979 ((looking-at "\\s\(")
980 (save-excursion
981 (goto-char (1+ (c-down-list-backward
982 before-sws-pos)))
983 (c-crosses-statement-barrier-p
984 (point) maybe-after-boundary-pos)))
985 ;; Just gone back over an ordinary symbol of some sort?
986 (t (c-crosses-statement-barrier-p
987 (point) maybe-after-boundary-pos))))
989 (when boundary-pos
990 (setq pptok ptok
991 ptok tok
992 tok boundary-pos
993 sym 'boundary)
994 ;; Like a C "continue". Analyze the next sexp.
995 (throw 'loop t))))
997 ;; ObjC method def?
998 (when (and c-opt-method-key
999 (setq saved (c-in-method-def-p)))
1000 (setq pos saved
1001 ignore-labels t) ; Avoid the label check on exit.
1002 (throw 'loop nil)) ; 3. ObjC method def.
1004 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1005 (if c-has-bitfields
1006 (cond
1007 ;; The : <size> and <id> fields?
1008 ((and (numberp c-maybe-labelp)
1009 (not bitfield-size-pos)
1010 (save-excursion
1011 (goto-char (or tok start))
1012 (not (looking-at c-keywords-regexp)))
1013 (not (looking-at c-keywords-regexp))
1014 (not (c-punctuation-in (point) c-maybe-labelp)))
1015 (setq bitfield-size-pos (or tok start)
1016 bitfield-id-pos (point)))
1017 ;; The <type> field?
1018 ((and bitfield-id-pos
1019 (not bitfield-type-pos))
1020 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1021 (not (looking-at c-not-primitive-type-keywords-regexp))
1022 (not (c-punctuation-in (point) tok)))
1023 (setq bitfield-type-pos (point))
1024 (setq bitfield-size-pos nil
1025 bitfield-id-pos nil)))))
1027 ;; Handle labels.
1028 (unless (eq ignore-labels t)
1029 (when (numberp c-maybe-labelp)
1030 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1031 ;; might be in a label now. Have we got a real label
1032 ;; (including a case label) or something like C++'s "public:"?
1033 ;; A case label might use an expression rather than a token.
1034 (setq after-case:-pos (or tok start))
1035 (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1036 ;; Catch C++'s inheritance construct "class foo : bar".
1037 (save-excursion
1038 (and
1039 (c-safe (c-backward-sexp) t)
1040 (looking-at c-nonlabel-token-2-key))))
1041 (setq c-maybe-labelp nil)
1042 (if after-labels-pos ; Have we already encountered a label?
1043 (if (not last-label-pos)
1044 (setq last-label-pos (or tok start)))
1045 (setq after-labels-pos (or tok start)))
1046 (setq c-maybe-labelp t
1047 label-good-pos nil))) ; bogus "label"
1049 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1050 ; been found.
1051 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1052 ;; We're in a potential label and it's the first
1053 ;; time we've found something that isn't allowed in
1054 ;; one.
1055 (setq label-good-pos (or tok start))))
1057 ;; We've moved back by a sexp, so update the token positions.
1058 (setq sym nil
1059 pptok ptok
1060 ptok tok
1061 tok (point)
1062 pos tok) ; always non-nil
1063 ) ; end of (catch loop ....)
1064 ) ; end of sexp-at-a-time (while ....)
1066 ;; If the stack isn't empty there might be errors to report.
1067 (while stack
1068 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1069 (c-bos-report-error))
1070 (setq saved-pos (cdr (car stack))
1071 stack (cdr stack)))
1073 (when (and (eq ret 'same)
1074 (not (memq sym '(boundary ignore nil))))
1075 ;; Need to investigate closer whether we've crossed
1076 ;; between a substatement and its containing statement.
1077 (if (setq saved (if (looking-at c-block-stmt-1-key)
1078 ptok
1079 pptok))
1080 (cond ((> start saved) (setq pos saved))
1081 ((= start saved) (setq ret 'up)))))
1083 (when (and (not ignore-labels)
1084 (eq c-maybe-labelp t)
1085 (not (eq ret 'beginning))
1086 after-labels-pos
1087 (not bitfield-type-pos) ; Bitfields take precedence over labels.
1088 (or (not label-good-pos)
1089 (<= label-good-pos pos)
1090 (progn
1091 (goto-char (if (and last-label-pos
1092 (< last-label-pos start))
1093 last-label-pos
1094 pos))
1095 (looking-at c-label-kwds-regexp))))
1096 ;; We're in a label. Maybe we should step to the statement
1097 ;; after it.
1098 (if (< after-labels-pos start)
1099 (setq pos after-labels-pos)
1100 (setq ret 'label)
1101 (if (and last-label-pos (< last-label-pos start))
1102 ;; Might have jumped over several labels. Go to the last one.
1103 (setq pos last-label-pos)))))
1105 ;; Have we got "case <expression>:"?
1106 (goto-char pos)
1107 (when (and after-case:-pos
1108 (not (eq ret 'beginning))
1109 (looking-at c-case-kwds-regexp))
1110 (if (< after-case:-pos start)
1111 (setq pos after-case:-pos))
1112 (if (eq ret 'same)
1113 (setq ret 'label)))
1115 ;; Skip over the unary operators that can start the statement.
1116 (while (progn
1117 (c-backward-syntactic-ws)
1118 ;; protect AWK post-inc/decrement operators, etc.
1119 (and (not (c-at-vsemi-p (point)))
1120 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1121 (setq pos (point)))
1122 (goto-char pos)
1123 ret)))
1125 (defun c-punctuation-in (from to)
1126 "Return non-nil if there is a non-comment non-macro punctuation character
1127 between FROM and TO. FROM must not be in a string or comment. The returned
1128 value is the position of the first such character."
1129 (save-excursion
1130 (goto-char from)
1131 (let ((pos (point)))
1132 (while (progn (skip-chars-forward c-symbol-chars to)
1133 (c-forward-syntactic-ws to)
1134 (> (point) pos))
1135 (setq pos (point))))
1136 (and (< (point) to) (point))))
1138 (defun c-crosses-statement-barrier-p (from to)
1139 "Return non-nil if buffer positions FROM to TO cross one or more
1140 statement or declaration boundaries. The returned value is actually
1141 the position of the earliest boundary char. FROM must not be within
1142 a string or comment.
1144 The variable `c-maybe-labelp' is set to the position of the first `:' that
1145 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1146 single `?' is found, then `c-maybe-labelp' is cleared.
1148 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1149 regarded as having a \"virtual semicolon\" immediately after the last token on
1150 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1152 Note that this function might do hidden buffer changes. See the
1153 comment at the start of cc-engine.el for more info."
1154 (let* ((skip-chars
1155 ;; If the current language has CPP macros, insert # into skip-chars.
1156 (if c-opt-cpp-symbol
1157 (concat (substring c-stmt-delim-chars 0 1) ; "^"
1158 c-opt-cpp-symbol ; usually "#"
1159 (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
1160 c-stmt-delim-chars))
1161 (non-skip-list
1162 (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1163 lit-range vsemi-pos)
1164 (save-restriction
1165 (widen)
1166 (save-excursion
1167 (catch 'done
1168 (goto-char from)
1169 (while (progn (skip-chars-forward
1170 skip-chars
1171 (min to (c-point 'bonl)))
1172 (< (point) to))
1173 (cond
1174 ;; Virtual semicolon?
1175 ((and (bolp)
1176 (save-excursion
1177 (progn
1178 (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1179 (goto-char (car lit-range)))
1180 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1181 (setq vsemi-pos (point))
1182 (c-at-vsemi-p))))
1183 (throw 'done vsemi-pos))
1184 ;; In a string/comment?
1185 ((setq lit-range (c-literal-limits))
1186 (goto-char (cdr lit-range)))
1187 ((eq (char-after) ?:)
1188 (forward-char)
1189 (if (and (eq (char-after) ?:)
1190 (< (point) to))
1191 ;; Ignore scope operators.
1192 (forward-char)
1193 (setq c-maybe-labelp (1- (point)))))
1194 ((eq (char-after) ??)
1195 ;; A question mark. Can't be a label, so stop
1196 ;; looking for more : and ?.
1197 (setq c-maybe-labelp nil
1198 skip-chars (substring c-stmt-delim-chars 0 -2)))
1199 ;; At a CPP construct?
1200 ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol)
1201 (save-excursion
1202 (forward-line 0)
1203 (looking-at c-opt-cpp-prefix)))
1204 (c-end-of-macro))
1205 ((memq (char-after) non-skip-list)
1206 (throw 'done (point)))))
1207 ;; In trailing space after an as yet undetected virtual semicolon?
1208 (c-backward-syntactic-ws from)
1209 (if (and (< (point) to)
1210 (c-at-vsemi-p))
1211 (point)
1212 nil))))))
1214 (defun c-at-statement-start-p ()
1215 "Return non-nil if the point is at the first token in a statement
1216 or somewhere in the syntactic whitespace before it.
1218 A \"statement\" here is not restricted to those inside code blocks.
1219 Any kind of declaration-like construct that occur outside function
1220 bodies is also considered a \"statement\".
1222 Note that this function might do hidden buffer changes. See the
1223 comment at the start of cc-engine.el for more info."
1225 (save-excursion
1226 (let ((end (point))
1227 c-maybe-labelp)
1228 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1229 (or (bobp)
1230 (eq (char-before) ?})
1231 (and (eq (char-before) ?{)
1232 (not (and c-special-brace-lists
1233 (progn (backward-char)
1234 (c-looking-at-special-brace-list)))))
1235 (c-crosses-statement-barrier-p (point) end)))))
1237 (defun c-at-expression-start-p ()
1238 "Return non-nil if the point is at the first token in an expression or
1239 statement, or somewhere in the syntactic whitespace before it.
1241 An \"expression\" here is a bit different from the normal language
1242 grammar sense: It's any sequence of expression tokens except commas,
1243 unless they are enclosed inside parentheses of some kind. Also, an
1244 expression never continues past an enclosing parenthesis, but it might
1245 contain parenthesis pairs of any sort except braces.
1247 Since expressions never cross statement boundaries, this function also
1248 recognizes statement beginnings, just like `c-at-statement-start-p'.
1250 Note that this function might do hidden buffer changes. See the
1251 comment at the start of cc-engine.el for more info."
1253 (save-excursion
1254 (let ((end (point))
1255 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1256 c-maybe-labelp)
1257 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1258 (or (bobp)
1259 (memq (char-before) '(?{ ?}))
1260 (save-excursion (backward-char)
1261 (looking-at "\\s("))
1262 (c-crosses-statement-barrier-p (point) end)))))
1265 ;; A set of functions that covers various idiosyncrasies in
1266 ;; implementations of `forward-comment'.
1268 ;; Note: Some emacsen considers incorrectly that any line comment
1269 ;; ending with a backslash continues to the next line. I can't think
1270 ;; of any way to work around that in a reliable way without changing
1271 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1272 ;; changing the syntax for backslash doesn't work since we must treat
1273 ;; escapes in string literals correctly.)
1275 (defun c-forward-single-comment ()
1276 "Move forward past whitespace and the closest following comment, if any.
1277 Return t if a comment was found, nil otherwise. In either case, the
1278 point is moved past the following whitespace. Line continuations,
1279 i.e. a backslashes followed by line breaks, are treated as whitespace.
1280 The line breaks that end line comments are considered to be the
1281 comment enders, so the point will be put on the beginning of the next
1282 line if it moved past a line comment.
1284 This function does not do any hidden buffer changes."
1286 (let ((start (point)))
1287 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1288 (goto-char (match-end 0)))
1290 (when (forward-comment 1)
1291 (if (eobp)
1292 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1293 ;; forwards at eob.
1296 ;; Emacs includes the ending newline in a b-style (c++)
1297 ;; comment, but XEmacs doesn't. We depend on the Emacs
1298 ;; behavior (which also is symmetric).
1299 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1300 (condition-case nil (forward-char 1)))
1302 t))))
1304 (defsubst c-forward-comments ()
1305 "Move forward past all following whitespace and comments.
1306 Line continuations, i.e. a backslashes followed by line breaks, are
1307 treated as whitespace.
1309 Note that this function might do hidden buffer changes. See the
1310 comment at the start of cc-engine.el for more info."
1312 (while (or
1313 ;; If forward-comment in at least XEmacs 21 is given a large
1314 ;; positive value, it'll loop all the way through if it hits
1315 ;; eob.
1316 (and (forward-comment 5)
1317 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1318 ;; forwards at eob.
1319 (not (eobp)))
1321 (when (looking-at "\\\\[\n\r]")
1322 (forward-char 2)
1323 t))))
1325 (defun c-backward-single-comment ()
1326 "Move backward past whitespace and the closest preceding comment, if any.
1327 Return t if a comment was found, nil otherwise. In either case, the
1328 point is moved past the preceding whitespace. Line continuations,
1329 i.e. a backslashes followed by line breaks, are treated as whitespace.
1330 The line breaks that end line comments are considered to be the
1331 comment enders, so the point cannot be at the end of the same line to
1332 move over a line comment.
1334 This function does not do any hidden buffer changes."
1336 (let ((start (point)))
1337 ;; When we got newline terminated comments, forward-comment in all
1338 ;; supported emacsen so far will stop at eol of each line not
1339 ;; ending with a comment when moving backwards. This corrects for
1340 ;; that, and at the same time handles line continuations.
1341 (while (progn
1342 (skip-chars-backward " \t\n\r\f\v")
1343 (and (looking-at "[\n\r]")
1344 (eq (char-before) ?\\)))
1345 (backward-char))
1347 (if (bobp)
1348 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1349 ;; backwards at bob.
1352 ;; Leave point after the closest following newline if we've
1353 ;; backed up over any above, since forward-comment won't move
1354 ;; backward over a line comment if point is at the end of the
1355 ;; same line.
1356 (re-search-forward "\\=\\s *[\n\r]" start t)
1358 (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
1359 (if (eolp)
1360 ;; If forward-comment above succeeded and we're at eol
1361 ;; then the newline we moved over above didn't end a
1362 ;; line comment, so we give it another go.
1363 (let (open-paren-in-column-0-is-defun-start)
1364 (forward-comment -1))
1367 ;; Emacs <= 20 and XEmacs move back over the closer of a
1368 ;; block comment that lacks an opener.
1369 (if (looking-at "\\*/")
1370 (progn (forward-char 2) nil)
1371 t)))))
1373 (defsubst c-backward-comments ()
1374 "Move backward past all preceding whitespace and comments.
1375 Line continuations, i.e. a backslashes followed by line breaks, are
1376 treated as whitespace. The line breaks that end line comments are
1377 considered to be the comment enders, so the point cannot be at the end
1378 of the same line to move over a line comment. Unlike
1379 c-backward-syntactic-ws, this function doesn't move back over
1380 preprocessor directives.
1382 Note that this function might do hidden buffer changes. See the
1383 comment at the start of cc-engine.el for more info."
1385 (let ((start (point)))
1386 (while (and
1387 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1388 ;; return t when moving backwards at bob.
1389 (not (bobp))
1391 (if (let (open-paren-in-column-0-is-defun-start)
1392 (forward-comment -1))
1393 (if (looking-at "\\*/")
1394 ;; Emacs <= 20 and XEmacs move back over the
1395 ;; closer of a block comment that lacks an opener.
1396 (progn (forward-char 2) nil)
1399 ;; XEmacs treats line continuations as whitespace but
1400 ;; only in the backward direction, which seems a bit
1401 ;; odd. Anyway, this is necessary for Emacs.
1402 (when (and (looking-at "[\n\r]")
1403 (eq (char-before) ?\\)
1404 (< (point) start))
1405 (backward-char)
1406 t))))))
1409 ;; Tools for skipping over syntactic whitespace.
1411 ;; The following functions use text properties to cache searches over
1412 ;; large regions of syntactic whitespace. It works as follows:
1414 ;; o If a syntactic whitespace region contains anything but simple
1415 ;; whitespace (i.e. space, tab and line breaks), the text property
1416 ;; `c-in-sws' is put over it. At places where we have stopped
1417 ;; within that region there's also a `c-is-sws' text property.
1418 ;; That since there typically are nested whitespace inside that
1419 ;; must be handled separately, e.g. whitespace inside a comment or
1420 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1421 ;; to jump to another point with that property within the same
1422 ;; `c-in-sws' region. It can be likened to a ladder where
1423 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1425 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1426 ;; a "rung position" and also maybe on the first following char.
1427 ;; As many characters as can be conveniently found in this range
1428 ;; are marked, but no assumption can be made that the whole range
1429 ;; is marked (it could be clobbered by later changes, for
1430 ;; instance).
1432 ;; Note that some part of the beginning of a sequence of simple
1433 ;; whitespace might be part of the end of a preceding line comment
1434 ;; or cpp directive and must not be considered part of the "rung".
1435 ;; Such whitespace is some amount of horizontal whitespace followed
1436 ;; by a newline. In the case of cpp directives it could also be
1437 ;; two newlines with horizontal whitespace between them.
1439 ;; The reason to include the first following char is to cope with
1440 ;; "rung positions" that doesn't have any ordinary whitespace. If
1441 ;; `c-is-sws' is put on a token character it does not have
1442 ;; `c-in-sws' set simultaneously. That's the only case when that
1443 ;; can occur, and the reason for not extending the `c-in-sws'
1444 ;; region to cover it is that the `c-in-sws' region could then be
1445 ;; accidentally merged with a following one if the token is only
1446 ;; one character long.
1448 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1449 ;; removed in the changed region. If the change was inside
1450 ;; syntactic whitespace that means that the "ladder" is broken, but
1451 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1452 ;; parts on either side and use an ordinary search only to "repair"
1453 ;; the gap.
1455 ;; Special care needs to be taken if a region is removed: If there
1456 ;; are `c-in-sws' on both sides of it which do not connect inside
1457 ;; the region then they can't be joined. If e.g. a marked macro is
1458 ;; broken, syntactic whitespace inside the new text might be
1459 ;; marked. If those marks would become connected with the old
1460 ;; `c-in-sws' range around the macro then we could get a ladder
1461 ;; with one end outside the macro and the other at some whitespace
1462 ;; within it.
1464 ;; The main motivation for this system is to increase the speed in
1465 ;; skipping over the large whitespace regions that can occur at the
1466 ;; top level in e.g. header files that contain a lot of comments and
1467 ;; cpp directives. For small comments inside code it's probably
1468 ;; slower than using `forward-comment' straightforwardly, but speed is
1469 ;; not a significant factor there anyway.
1471 ; (defface c-debug-is-sws-face
1472 ; '((t (:background "GreenYellow")))
1473 ; "Debug face to mark the `c-is-sws' property.")
1474 ; (defface c-debug-in-sws-face
1475 ; '((t (:underline t)))
1476 ; "Debug face to mark the `c-in-sws' property.")
1478 ; (defun c-debug-put-sws-faces ()
1479 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1480 ; ;; properties in the buffer.
1481 ; (interactive)
1482 ; (save-excursion
1483 ; (c-save-buffer-state (in-face)
1484 ; (goto-char (point-min))
1485 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1486 ; (point)))
1487 ; (while (progn
1488 ; (goto-char (next-single-property-change
1489 ; (point) 'c-is-sws nil (point-max)))
1490 ; (if in-face
1491 ; (progn
1492 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1493 ; (setq in-face nil))
1494 ; (setq in-face (point)))
1495 ; (not (eobp))))
1496 ; (goto-char (point-min))
1497 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1498 ; (point)))
1499 ; (while (progn
1500 ; (goto-char (next-single-property-change
1501 ; (point) 'c-in-sws nil (point-max)))
1502 ; (if in-face
1503 ; (progn
1504 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1505 ; (setq in-face nil))
1506 ; (setq in-face (point)))
1507 ; (not (eobp)))))))
1509 (defmacro c-debug-sws-msg (&rest args)
1510 ;;`(message ,@args)
1513 (defmacro c-put-is-sws (beg end)
1514 ;; This macro does a hidden buffer change.
1515 `(let ((beg ,beg) (end ,end))
1516 (put-text-property beg end 'c-is-sws t)
1517 ,@(when (facep 'c-debug-is-sws-face)
1518 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1520 (defmacro c-put-in-sws (beg end)
1521 ;; This macro does a hidden buffer change.
1522 `(let ((beg ,beg) (end ,end))
1523 (put-text-property beg end 'c-in-sws t)
1524 ,@(when (facep 'c-debug-is-sws-face)
1525 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1527 (defmacro c-remove-is-sws (beg end)
1528 ;; This macro does a hidden buffer change.
1529 `(let ((beg ,beg) (end ,end))
1530 (remove-text-properties beg end '(c-is-sws nil))
1531 ,@(when (facep 'c-debug-is-sws-face)
1532 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1534 (defmacro c-remove-in-sws (beg end)
1535 ;; This macro does a hidden buffer change.
1536 `(let ((beg ,beg) (end ,end))
1537 (remove-text-properties beg end '(c-in-sws nil))
1538 ,@(when (facep 'c-debug-is-sws-face)
1539 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1541 (defmacro c-remove-is-and-in-sws (beg end)
1542 ;; This macro does a hidden buffer change.
1543 `(let ((beg ,beg) (end ,end))
1544 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1545 ,@(when (facep 'c-debug-is-sws-face)
1546 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1547 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1549 (defsubst c-invalidate-sws-region-after (beg end)
1550 ;; Called from `after-change-functions'. Note that if
1551 ;; `c-forward-sws' or `c-backward-sws' are used outside
1552 ;; `c-save-buffer-state' or similar then this will remove the cache
1553 ;; properties right after they're added.
1555 ;; This function does hidden buffer changes.
1557 (save-excursion
1558 ;; Adjust the end to remove the properties in any following simple
1559 ;; ws up to and including the next line break, if there is any
1560 ;; after the changed region. This is necessary e.g. when a rung
1561 ;; marked empty line is converted to a line comment by inserting
1562 ;; "//" before the line break. In that case the line break would
1563 ;; keep the rung mark which could make a later `c-backward-sws'
1564 ;; move into the line comment instead of over it.
1565 (goto-char end)
1566 (skip-chars-forward " \t\f\v")
1567 (when (and (eolp) (not (eobp)))
1568 (setq end (1+ (point)))))
1570 (when (and (= beg end)
1571 (get-text-property beg 'c-in-sws)
1572 (> beg (point-min))
1573 (get-text-property (1- beg) 'c-in-sws))
1574 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1575 ;; safe to keep a range that was continuous before the change. E.g:
1577 ;; #define foo
1578 ;; \
1579 ;; bar
1581 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1582 ;; after "foo" is removed then "bar" will become part of the cpp
1583 ;; directive instead of a syntactically relevant token. In that
1584 ;; case there's no longer syntactic ws from "#" to "b".
1585 (setq beg (1- beg)))
1587 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1588 (c-remove-is-and-in-sws beg end))
1590 (defun c-forward-sws ()
1591 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1593 ;; This function might do hidden buffer changes.
1595 (let (;; `rung-pos' is set to a position as early as possible in the
1596 ;; unmarked part of the simple ws region.
1597 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1598 rung-is-marked next-rung-is-marked simple-ws-end
1599 ;; `safe-start' is set when it's safe to cache the start position.
1600 ;; It's not set if we've initially skipped over comments and line
1601 ;; continuations since we might have gone out through the end of a
1602 ;; macro then. This provision makes `c-forward-sws' not populate the
1603 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1604 ;; more common.
1605 safe-start)
1607 ;; Skip simple ws and do a quick check on the following character to see
1608 ;; if it's anything that can't start syntactic ws, so we can bail out
1609 ;; early in the majority of cases when there just are a few ws chars.
1610 (skip-chars-forward " \t\n\r\f\v")
1611 (when (looking-at c-syntactic-ws-start)
1613 (setq rung-end-pos (min (1+ (point)) (point-max)))
1614 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1615 'c-is-sws t))
1616 ;; Find the last rung position to avoid setting properties in all
1617 ;; the cases when the marked rung is complete.
1618 ;; (`next-single-property-change' is certain to move at least one
1619 ;; step forward.)
1620 (setq rung-pos (1- (next-single-property-change
1621 rung-is-marked 'c-is-sws nil rung-end-pos)))
1622 ;; Got no marked rung here. Since the simple ws might have started
1623 ;; inside a line comment or cpp directive we must set `rung-pos' as
1624 ;; high as possible.
1625 (setq rung-pos (point)))
1627 (while
1628 (progn
1629 (while
1630 (when (and rung-is-marked
1631 (get-text-property (point) 'c-in-sws))
1633 ;; The following search is the main reason that `c-in-sws'
1634 ;; and `c-is-sws' aren't combined to one property.
1635 (goto-char (next-single-property-change
1636 (point) 'c-in-sws nil (point-max)))
1637 (unless (get-text-property (point) 'c-is-sws)
1638 ;; If the `c-in-sws' region extended past the last
1639 ;; `c-is-sws' char we have to go back a bit.
1640 (or (get-text-property (1- (point)) 'c-is-sws)
1641 (goto-char (previous-single-property-change
1642 (point) 'c-is-sws)))
1643 (backward-char))
1645 (c-debug-sws-msg
1646 "c-forward-sws cached move %s -> %s (max %s)"
1647 rung-pos (point) (point-max))
1649 (setq rung-pos (point))
1650 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1651 (not (eobp))))
1653 ;; We'll loop here if there is simple ws after the last rung.
1654 ;; That means that there's been some change in it and it's
1655 ;; possible that we've stepped into another ladder, so extend
1656 ;; the previous one to join with it if there is one, and try to
1657 ;; use the cache again.
1658 (c-debug-sws-msg
1659 "c-forward-sws extending rung with [%s..%s] (max %s)"
1660 (1+ rung-pos) (1+ (point)) (point-max))
1661 (unless (get-text-property (point) 'c-is-sws)
1662 ;; Remove any `c-in-sws' property from the last char of
1663 ;; the rung before we mark it with `c-is-sws', so that we
1664 ;; won't connect with the remains of a broken "ladder".
1665 (c-remove-in-sws (point) (1+ (point))))
1666 (c-put-is-sws (1+ rung-pos)
1667 (1+ (point)))
1668 (c-put-in-sws rung-pos
1669 (setq rung-pos (point)
1670 last-put-in-sws-pos rung-pos)))
1672 (setq simple-ws-end (point))
1673 (c-forward-comments)
1675 (cond
1676 ((/= (point) simple-ws-end)
1677 ;; Skipped over comments. Don't cache at eob in case the buffer
1678 ;; is narrowed.
1679 (not (eobp)))
1681 ((save-excursion
1682 (and c-opt-cpp-prefix
1683 (looking-at c-opt-cpp-start)
1684 (progn (skip-chars-backward " \t")
1685 (bolp))
1686 (or (bobp)
1687 (progn (backward-char)
1688 (not (eq (char-before) ?\\))))))
1689 ;; Skip a preprocessor directive.
1690 (end-of-line)
1691 (while (and (eq (char-before) ?\\)
1692 (= (forward-line 1) 0))
1693 (end-of-line))
1694 (forward-line 1)
1695 (setq safe-start t)
1696 ;; Don't cache at eob in case the buffer is narrowed.
1697 (not (eobp)))))
1699 ;; We've searched over a piece of non-white syntactic ws. See if this
1700 ;; can be cached.
1701 (setq next-rung-pos (point))
1702 (skip-chars-forward " \t\n\r\f\v")
1703 (setq rung-end-pos (min (1+ (point)) (point-max)))
1705 (if (or
1706 ;; Cache if we haven't skipped comments only, and if we started
1707 ;; either from a marked rung or from a completely uncached
1708 ;; position.
1709 (and safe-start
1710 (or rung-is-marked
1711 (not (get-text-property simple-ws-end 'c-in-sws))))
1713 ;; See if there's a marked rung in the encountered simple ws. If
1714 ;; so then we can cache, unless `safe-start' is nil. Even then
1715 ;; we need to do this to check if the cache can be used for the
1716 ;; next step.
1717 (and (setq next-rung-is-marked
1718 (text-property-any next-rung-pos rung-end-pos
1719 'c-is-sws t))
1720 safe-start))
1722 (progn
1723 (c-debug-sws-msg
1724 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1725 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1726 (point-max))
1728 ;; Remove the properties for any nested ws that might be cached.
1729 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1730 ;; anyway.
1731 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1732 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1733 (c-put-is-sws rung-pos
1734 (1+ simple-ws-end))
1735 (setq rung-is-marked t))
1736 (c-put-in-sws rung-pos
1737 (setq rung-pos (point)
1738 last-put-in-sws-pos rung-pos))
1739 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1740 ;; Remove any `c-in-sws' property from the last char of
1741 ;; the rung before we mark it with `c-is-sws', so that we
1742 ;; won't connect with the remains of a broken "ladder".
1743 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1744 (c-put-is-sws next-rung-pos
1745 rung-end-pos))
1747 (c-debug-sws-msg
1748 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1749 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1750 (point-max))
1752 ;; Set `rung-pos' for the next rung. It's the same thing here as
1753 ;; initially, except that the rung position is set as early as
1754 ;; possible since we can't be in the ending ws of a line comment or
1755 ;; cpp directive now.
1756 (if (setq rung-is-marked next-rung-is-marked)
1757 (setq rung-pos (1- (next-single-property-change
1758 rung-is-marked 'c-is-sws nil rung-end-pos)))
1759 (setq rung-pos next-rung-pos))
1760 (setq safe-start t)))
1762 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1763 ;; another one after the point (which might occur when editing inside a
1764 ;; comment or macro).
1765 (when (eq last-put-in-sws-pos (point))
1766 (cond ((< last-put-in-sws-pos (point-max))
1767 (c-debug-sws-msg
1768 "c-forward-sws clearing at %s for cache separation"
1769 last-put-in-sws-pos)
1770 (c-remove-in-sws last-put-in-sws-pos
1771 (1+ last-put-in-sws-pos)))
1773 ;; If at eob we have to clear the last character before the end
1774 ;; instead since the buffer might be narrowed and there might
1775 ;; be a `c-in-sws' after (point-max). In this case it's
1776 ;; necessary to clear both properties.
1777 (c-debug-sws-msg
1778 "c-forward-sws clearing thoroughly at %s for cache separation"
1779 (1- last-put-in-sws-pos))
1780 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1781 last-put-in-sws-pos))))
1784 (defun c-backward-sws ()
1785 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1787 ;; This function might do hidden buffer changes.
1789 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1790 ;; part of the simple ws region.
1791 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1792 rung-is-marked simple-ws-beg cmt-skip-pos)
1794 ;; Skip simple horizontal ws and do a quick check on the preceding
1795 ;; character to see if it's anything that can't end syntactic ws, so we can
1796 ;; bail out early in the majority of cases when there just are a few ws
1797 ;; chars. Newlines are complicated in the backward direction, so we can't
1798 ;; skip over them.
1799 (skip-chars-backward " \t\f")
1800 (when (and (not (bobp))
1801 (save-excursion
1802 (backward-char)
1803 (looking-at c-syntactic-ws-end)))
1805 ;; Try to find a rung position in the simple ws preceding point, so that
1806 ;; we can get a cache hit even if the last bit of the simple ws has
1807 ;; changed recently.
1808 (setq simple-ws-beg (point))
1809 (skip-chars-backward " \t\n\r\f\v")
1810 (if (setq rung-is-marked (text-property-any
1811 (point) (min (1+ rung-pos) (point-max))
1812 'c-is-sws t))
1813 ;; `rung-pos' will be the earliest marked position, which means that
1814 ;; there might be later unmarked parts in the simple ws region.
1815 ;; It's not worth the effort to fix that; the last part of the
1816 ;; simple ws is also typically edited often, so it could be wasted.
1817 (goto-char (setq rung-pos rung-is-marked))
1818 (goto-char simple-ws-beg))
1820 (while
1821 (progn
1822 (while
1823 (when (and rung-is-marked
1824 (not (bobp))
1825 (get-text-property (1- (point)) 'c-in-sws))
1827 ;; The following search is the main reason that `c-in-sws'
1828 ;; and `c-is-sws' aren't combined to one property.
1829 (goto-char (previous-single-property-change
1830 (point) 'c-in-sws nil (point-min)))
1831 (unless (get-text-property (point) 'c-is-sws)
1832 ;; If the `c-in-sws' region extended past the first
1833 ;; `c-is-sws' char we have to go forward a bit.
1834 (goto-char (next-single-property-change
1835 (point) 'c-is-sws)))
1837 (c-debug-sws-msg
1838 "c-backward-sws cached move %s <- %s (min %s)"
1839 (point) rung-pos (point-min))
1841 (setq rung-pos (point))
1842 (if (and (< (min (skip-chars-backward " \t\f\v")
1843 (progn
1844 (setq simple-ws-beg (point))
1845 (skip-chars-backward " \t\n\r\f\v")))
1847 (setq rung-is-marked
1848 (text-property-any (point) rung-pos
1849 'c-is-sws t)))
1851 (goto-char simple-ws-beg)
1852 nil))
1854 ;; We'll loop here if there is simple ws before the first rung.
1855 ;; That means that there's been some change in it and it's
1856 ;; possible that we've stepped into another ladder, so extend
1857 ;; the previous one to join with it if there is one, and try to
1858 ;; use the cache again.
1859 (c-debug-sws-msg
1860 "c-backward-sws extending rung with [%s..%s] (min %s)"
1861 rung-is-marked rung-pos (point-min))
1862 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1863 ;; Remove any `c-in-sws' property from the last char of
1864 ;; the rung before we mark it with `c-is-sws', so that we
1865 ;; won't connect with the remains of a broken "ladder".
1866 (c-remove-in-sws (1- rung-pos) rung-pos))
1867 (c-put-is-sws rung-is-marked
1868 rung-pos)
1869 (c-put-in-sws rung-is-marked
1870 (1- rung-pos))
1871 (setq rung-pos rung-is-marked
1872 last-put-in-sws-pos rung-pos))
1874 (c-backward-comments)
1875 (setq cmt-skip-pos (point))
1877 (cond
1878 ((and c-opt-cpp-prefix
1879 (/= cmt-skip-pos simple-ws-beg)
1880 (c-beginning-of-macro))
1881 ;; Inside a cpp directive. See if it should be skipped over.
1882 (let ((cpp-beg (point)))
1884 ;; Move back over all line continuations in the region skipped
1885 ;; over by `c-backward-comments'. If we go past it then we
1886 ;; started inside the cpp directive.
1887 (goto-char simple-ws-beg)
1888 (beginning-of-line)
1889 (while (and (> (point) cmt-skip-pos)
1890 (progn (backward-char)
1891 (eq (char-before) ?\\)))
1892 (beginning-of-line))
1894 (if (< (point) cmt-skip-pos)
1895 ;; Don't move past the cpp directive if we began inside
1896 ;; it. Note that the position at the end of the last line
1897 ;; of the macro is also considered to be within it.
1898 (progn (goto-char cmt-skip-pos)
1899 nil)
1901 ;; It's worthwhile to spend a little bit of effort on finding
1902 ;; the end of the macro, to get a good `simple-ws-beg'
1903 ;; position for the cache. Note that `c-backward-comments'
1904 ;; could have stepped over some comments before going into
1905 ;; the macro, and then `simple-ws-beg' must be kept on the
1906 ;; same side of those comments.
1907 (goto-char simple-ws-beg)
1908 (skip-chars-backward " \t\n\r\f\v")
1909 (if (eq (char-before) ?\\)
1910 (forward-char))
1911 (forward-line 1)
1912 (if (< (point) simple-ws-beg)
1913 ;; Might happen if comments after the macro were skipped
1914 ;; over.
1915 (setq simple-ws-beg (point)))
1917 (goto-char cpp-beg)
1918 t)))
1920 ((/= (save-excursion
1921 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1922 (setq next-rung-pos (point)))
1923 simple-ws-beg)
1924 ;; Skipped over comments. Must put point at the end of
1925 ;; the simple ws at point since we might be after a line
1926 ;; comment or cpp directive that's been partially
1927 ;; narrowed out, and we can't risk marking the simple ws
1928 ;; at the end of it.
1929 (goto-char next-rung-pos)
1930 t)))
1932 ;; We've searched over a piece of non-white syntactic ws. See if this
1933 ;; can be cached.
1934 (setq next-rung-pos (point))
1935 (skip-chars-backward " \t\f\v")
1937 (if (or
1938 ;; Cache if we started either from a marked rung or from a
1939 ;; completely uncached position.
1940 rung-is-marked
1941 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1943 ;; Cache if there's a marked rung in the encountered simple ws.
1944 (save-excursion
1945 (skip-chars-backward " \t\n\r\f\v")
1946 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1947 'c-is-sws t)))
1949 (progn
1950 (c-debug-sws-msg
1951 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1952 (point) (1+ next-rung-pos)
1953 simple-ws-beg (min (1+ rung-pos) (point-max))
1954 (point-min))
1956 ;; Remove the properties for any nested ws that might be cached.
1957 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1958 ;; anyway.
1959 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1960 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1961 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1962 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1963 ;; Remove any `c-in-sws' property from the last char of
1964 ;; the rung before we mark it with `c-is-sws', so that we
1965 ;; won't connect with the remains of a broken "ladder".
1966 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1967 (c-put-is-sws simple-ws-beg
1968 rung-end-pos)
1969 (setq rung-is-marked t)))
1970 (c-put-in-sws (setq simple-ws-beg (point)
1971 last-put-in-sws-pos simple-ws-beg)
1972 rung-pos)
1973 (c-put-is-sws (setq rung-pos simple-ws-beg)
1974 (1+ next-rung-pos)))
1976 (c-debug-sws-msg
1977 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1978 (point) (1+ next-rung-pos)
1979 simple-ws-beg (min (1+ rung-pos) (point-max))
1980 (point-min))
1981 (setq rung-pos next-rung-pos
1982 simple-ws-beg (point))
1985 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1986 ;; another one before the point (which might occur when editing inside a
1987 ;; comment or macro).
1988 (when (eq last-put-in-sws-pos (point))
1989 (cond ((< (point-min) last-put-in-sws-pos)
1990 (c-debug-sws-msg
1991 "c-backward-sws clearing at %s for cache separation"
1992 (1- last-put-in-sws-pos))
1993 (c-remove-in-sws (1- last-put-in-sws-pos)
1994 last-put-in-sws-pos))
1995 ((> (point-min) 1)
1996 ;; If at bob and the buffer is narrowed, we have to clear the
1997 ;; character we're standing on instead since there might be a
1998 ;; `c-in-sws' before (point-min). In this case it's necessary
1999 ;; to clear both properties.
2000 (c-debug-sws-msg
2001 "c-backward-sws clearing thoroughly at %s for cache separation"
2002 last-put-in-sws-pos)
2003 (c-remove-is-and-in-sws last-put-in-sws-pos
2004 (1+ last-put-in-sws-pos)))))
2008 ;; Other whitespace tools
2009 (defun c-partial-ws-p (beg end)
2010 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2011 ;; region? This is a "heuristic" function. .....
2013 ;; The motivation for the second bit is to check whether removing this
2014 ;; region would coalesce two symbols.
2016 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2017 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2018 (save-excursion
2019 (let ((end+1 (min (1+ end) (point-max))))
2020 (or (progn (goto-char (max (point-min) (1- beg)))
2021 (c-skip-ws-forward end)
2022 (eq (point) end))
2023 (progn (goto-char beg)
2024 (c-skip-ws-forward end+1)
2025 (eq (point) end+1))))))
2027 ;; A system for finding noteworthy parens before the point.
2029 (defconst c-state-cache-too-far 5000)
2030 ;; A maximum comfortable scanning distance, e.g. between
2031 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2032 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2033 ;; the cache and starting again from point-min or a beginning of defun. This
2034 ;; value can be tuned for efficiency or set to a lower value for testing.
2036 (defvar c-state-cache nil)
2037 (make-variable-buffer-local 'c-state-cache)
2038 ;; The state cache used by `c-parse-state' to cut down the amount of
2039 ;; searching. It's the result from some earlier `c-parse-state' call. See
2040 ;; `c-parse-state''s doc string for details of its structure.
2042 ;; The use of the cached info is more effective if the next
2043 ;; `c-parse-state' call is on a line close by the one the cached state
2044 ;; was made at; the cache can actually slow down a little if the
2045 ;; cached state was made very far back in the buffer. The cache is
2046 ;; most effective if `c-parse-state' is used on each line while moving
2047 ;; forward.
2049 (defvar c-state-cache-good-pos 1)
2050 (make-variable-buffer-local 'c-state-cache-good-pos)
2051 ;; This is a position where `c-state-cache' is known to be correct, or
2052 ;; nil (see below). It's a position inside one of the recorded unclosed
2053 ;; parens or the top level, but not further nested inside any literal or
2054 ;; subparen that is closed before the last recorded position.
2056 ;; The exact position is chosen to try to be close to yet earlier than
2057 ;; the position where `c-state-cache' will be called next. Right now
2058 ;; the heuristic is to set it to the position after the last found
2059 ;; closing paren (of any type) before the line on which
2060 ;; `c-parse-state' was called. That is chosen primarily to work well
2061 ;; with refontification of the current line.
2063 ;; 2009-07-28: When `c-state-point-min' and the last position where
2064 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2065 ;; both in the same literal, there is no such "good position", and
2066 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2067 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2069 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2070 ;; the middle of the desert, as long as it is not within a brace pair
2071 ;; recorded in `c-state-cache' or a paren/bracket pair.
2074 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2075 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2076 ;; speed up testing for non-literality.
2077 (defconst c-state-nonlit-pos-interval 3000)
2078 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2080 (defvar c-state-nonlit-pos-cache nil)
2081 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2082 ;; A list of buffer positions which are known not to be in a literal or a cpp
2083 ;; construct. This is ordered with higher positions at the front of the list.
2084 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2086 (defvar c-state-nonlit-pos-cache-limit 1)
2087 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2088 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2089 ;; reduced by buffer changes, and increased by invocations of
2090 ;; `c-state-literal-at'.
2092 (defsubst c-state-pp-to-literal (from to)
2093 ;; Do a parse-partial-sexp from FROM to TO, returning either
2094 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2095 ;; (STATE) otherwise,
2096 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2097 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal.
2099 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2100 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2101 ;; STATE are valid.
2102 (save-excursion
2103 (let ((s (parse-partial-sexp from to))
2105 (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
2106 (setq ty (cond
2107 ((nth 3 s) 'string)
2108 ((eq (nth 7 s) t) 'c++)
2109 (t 'c)))
2110 (parse-partial-sexp (point) (point-max)
2111 nil ; TARGETDEPTH
2112 nil ; STOPBEFORE
2113 s ; OLDSTATE
2114 'syntax-table)) ; stop at end of literal
2115 (if ty
2116 `(,s ,ty (,(nth 8 s) . ,(point)))
2117 `(,s)))))
2119 (defun c-state-safe-place (here)
2120 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2121 ;; string, comment, or macro.
2123 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2124 ;; MAY NOT contain any positions within macros, since macros are frequently
2125 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2126 ;; We cannot rely on this mechanism whilst determining a cache pos since
2127 ;; this function is also called from outwith `c-parse-state'.
2128 (save-restriction
2129 (widen)
2130 (save-excursion
2131 (let ((c c-state-nonlit-pos-cache)
2132 pos npos lit macro-beg macro-end)
2133 ;; Trim the cache to take account of buffer changes.
2134 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2135 (setq c (cdr c)))
2136 (setq c-state-nonlit-pos-cache c)
2138 (while (and c (> (car c) here))
2139 (setq c (cdr c)))
2140 (setq pos (or (car c) (point-min)))
2142 (while
2143 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2144 (and
2145 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2147 ;; Test for being in a literal.
2148 (progn
2149 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2150 (or (null lit)
2151 (prog1 (<= (cdr lit) here)
2152 (setq npos (cdr lit)))))
2154 ;; Test for being in a macro.
2155 (progn
2156 (goto-char npos)
2157 (setq macro-beg
2158 (and (c-beginning-of-macro) (/= (point) npos) (point)))
2159 (when macro-beg
2160 (c-syntactic-end-of-macro)
2161 (or (eobp) (forward-char))
2162 (setq macro-end (point)))
2163 (or (null macro-beg)
2164 (prog1 (<= macro-end here)
2165 (setq npos macro-end)))))
2167 (setq pos npos)
2168 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2170 (if (> pos c-state-nonlit-pos-cache-limit)
2171 (setq c-state-nonlit-pos-cache-limit pos))
2172 pos))))
2174 (defun c-state-literal-at (here)
2175 ;; If position HERE is inside a literal, return (START . END), the
2176 ;; boundaries of the literal (which may be outside the accessible bit of the
2177 ;; buffer). Otherwise, return nil.
2179 ;; This function is almost the same as `c-literal-limits'. Previously, it
2180 ;; differed in that it was a lower level function, and that it rigorously
2181 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2182 ;; virtually identical to this function.
2183 (save-restriction
2184 (widen)
2185 (save-excursion
2186 (let ((pos (c-state-safe-place here)))
2187 (car (cddr (c-state-pp-to-literal pos here)))))))
2189 (defsubst c-state-lit-beg (pos)
2190 ;; Return the start of the literal containing POS, or POS itself.
2191 (or (car (c-state-literal-at pos))
2192 pos))
2194 (defsubst c-state-cache-non-literal-place (pos state)
2195 ;; Return a position outside of a string/comment/macro at or before POS.
2196 ;; STATE is the parse-partial-sexp state at POS.
2197 (let ((res (if (or (nth 3 state) ; in a string?
2198 (nth 4 state)) ; in a comment?
2199 (nth 8 state)
2200 pos)))
2201 (save-excursion
2202 (goto-char res)
2203 (if (c-beginning-of-macro)
2204 (point)
2205 res))))
2207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2208 ;; Stuff to do with point-min, and coping with any literal there.
2209 (defvar c-state-point-min 1)
2210 (make-variable-buffer-local 'c-state-point-min)
2211 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2212 ;; narrowing is likely to affect the parens that are visible before the point.
2214 (defvar c-state-point-min-lit-type nil)
2215 (make-variable-buffer-local 'c-state-point-min-lit-type)
2216 (defvar c-state-point-min-lit-start nil)
2217 (make-variable-buffer-local 'c-state-point-min-lit-start)
2218 ;; These two variables define the literal, if any, containing point-min.
2219 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2220 ;; literal. If there's no literal there, they're both nil.
2222 (defvar c-state-min-scan-pos 1)
2223 (make-variable-buffer-local 'c-state-min-scan-pos)
2224 ;; This is the earliest buffer-pos from which scanning can be done. It is
2225 ;; either the end of the literal containing point-min, or point-min itself.
2226 ;; It becomes nil if the buffer is changed earlier than this point.
2227 (defun c-state-get-min-scan-pos ()
2228 ;; Return the lowest valid scanning pos. This will be the end of the
2229 ;; literal enclosing point-min, or point-min itself.
2230 (or c-state-min-scan-pos
2231 (save-restriction
2232 (save-excursion
2233 (widen)
2234 (goto-char c-state-point-min-lit-start)
2235 (if (eq c-state-point-min-lit-type 'string)
2236 (forward-sexp)
2237 (forward-comment 1))
2238 (setq c-state-min-scan-pos (point))))))
2240 (defun c-state-mark-point-min-literal ()
2241 ;; Determine the properties of any literal containing POINT-MIN, setting the
2242 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2243 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2244 (let ((p-min (point-min))
2245 lit)
2246 (save-restriction
2247 (widen)
2248 (setq lit (c-state-literal-at p-min))
2249 (if lit
2250 (setq c-state-point-min-lit-type
2251 (save-excursion
2252 (goto-char (car lit))
2253 (cond
2254 ((looking-at c-block-comment-start-regexp) 'c)
2255 ((looking-at c-line-comment-starter) 'c++)
2256 (t 'string)))
2257 c-state-point-min-lit-start (car lit)
2258 c-state-min-scan-pos (cdr lit))
2259 (setq c-state-point-min-lit-type nil
2260 c-state-point-min-lit-start nil
2261 c-state-min-scan-pos p-min)))))
2264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2265 ;; A variable which signals a brace dessert - helpful for reducing the number
2266 ;; of fruitless backward scans.
2267 (defvar c-state-brace-pair-desert nil)
2268 (make-variable-buffer-local 'c-state-brace-pair-desert)
2269 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2270 ;; that defun has searched backwards for a brace pair and not found one. Its
2271 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2272 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2273 ;; nil when at top level) and FROM is where the backward search started. It
2274 ;; is reset to nil in `c-invalidate-state-cache'.
2277 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2278 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2279 ;; list of like structure.
2280 (defmacro c-state-cache-top-lparen (&optional cache)
2281 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2282 ;; (default `c-state-cache') (or nil).
2283 (let ((cash (or cache 'c-state-cache)))
2284 `(if (consp (car ,cash))
2285 (caar ,cash)
2286 (car ,cash))))
2288 (defmacro c-state-cache-top-paren (&optional cache)
2289 ;; Return the address of the latest brace/bracket/paren (whether left or
2290 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2291 (let ((cash (or cache 'c-state-cache)))
2292 `(if (consp (car ,cash))
2293 (cdar ,cash)
2294 (car ,cash))))
2296 (defmacro c-state-cache-after-top-paren (&optional cache)
2297 ;; Return the position just after the latest brace/bracket/paren (whether
2298 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2299 (let ((cash (or cache 'c-state-cache)))
2300 `(if (consp (car ,cash))
2301 (cdar ,cash)
2302 (and (car ,cash)
2303 (1+ (car ,cash))))))
2305 (defun c-get-cache-scan-pos (here)
2306 ;; From the state-cache, determine the buffer position from which we might
2307 ;; scan forward to HERE to update this cache. This position will be just
2308 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2309 ;; return the earliest position in the accessible region which isn't within
2310 ;; a literal. If the visible portion of the buffer is entirely within a
2311 ;; literal, return NIL.
2312 (let ((c c-state-cache) elt)
2313 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2314 (while (and c
2315 (>= (c-state-cache-top-lparen c) here))
2316 (setq c (cdr c)))
2318 (setq elt (car c))
2319 (cond
2320 ((consp elt)
2321 (if (> (cdr elt) here)
2322 (1+ (car elt))
2323 (cdr elt)))
2324 (elt (1+ elt))
2325 ((<= (c-state-get-min-scan-pos) here)
2326 (c-state-get-min-scan-pos))
2327 (t nil))))
2329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2330 ;; Variables which keep track of preprocessor constructs.
2331 (defvar c-state-old-cpp-beg nil)
2332 (make-variable-buffer-local 'c-state-old-cpp-beg)
2333 (defvar c-state-old-cpp-end nil)
2334 (make-variable-buffer-local 'c-state-old-cpp-end)
2335 ;; These are the limits of the macro containing point at the previous call of
2336 ;; `c-parse-state', or nil.
2338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2339 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2340 (defun c-get-fallback-scan-pos (here)
2341 ;; Return a start position for building `c-state-cache' from
2342 ;; scratch. This will be at the top level, 2 defuns back.
2343 (save-excursion
2344 ;; Go back 2 bods, but ignore any bogus positions returned by
2345 ;; beginning-of-defun (i.e. open paren in column zero).
2346 (goto-char here)
2347 (let ((cnt 2))
2348 (while (not (or (bobp) (zerop cnt)))
2349 (c-beginning-of-defun-1) ; Pure elisp BOD.
2350 (if (eq (char-after) ?\{)
2351 (setq cnt (1- cnt)))))
2352 (point)))
2354 (defun c-state-balance-parens-backwards (here- here+ top)
2355 ;; Return the position of the opening paren/brace/bracket before HERE- which
2356 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2357 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2359 ;; ............................................
2360 ;; | |
2361 ;; ( [ ( .........#macro.. ) ( ) ] )
2362 ;; ^ ^ ^ ^
2363 ;; | | | |
2364 ;; return HERE- HERE+ TOP
2366 ;; If there aren't enough opening paren/brace/brackets, return the position
2367 ;; of the outermost one found, or HERE- if there are none. If there are no
2368 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2369 ;; must not be inside literals. Only the accessible portion of the buffer
2370 ;; will be scanned.
2372 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2373 ;; `here'. Go round the next loop each time we pass over such a ")". These
2374 ;; probably match "("s before `here-'.
2375 (let (pos pa ren+1 lonely-rens)
2376 (save-excursion
2377 (save-restriction
2378 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2379 (setq pos here+)
2380 (c-safe
2381 (while
2382 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2383 (setq lonely-rens (cons ren+1 lonely-rens)
2384 pos ren+1)))))
2386 ;; PART 2: Scan back before `here-' searching for the "("s
2387 ;; matching/mismatching the ")"s found above. We only need to direct the
2388 ;; caller to scan when we've encountered unmatched right parens.
2389 (setq pos here-)
2390 (when lonely-rens
2391 (c-safe
2392 (while
2393 (and lonely-rens ; actual values aren't used.
2394 (setq pa (scan-lists pos -1 1)))
2395 (setq pos pa)
2396 (setq lonely-rens (cdr lonely-rens)))))
2397 pos))
2399 (defun c-parse-state-get-strategy (here good-pos)
2400 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2401 ;; to minimize the amount of scanning. HERE is the pertinent position in
2402 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2403 ;; its head trimmed) is known to be good, or nil if there is no such
2404 ;; position.
2406 ;; The return value is a list, one of the following:
2408 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2409 ;; which is not less than CACHE-POS.
2410 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2411 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2412 ;; top level.
2413 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2414 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2415 ;; or below HERE.
2416 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2417 BOD-pos ; position of 2nd BOD before HERE.
2418 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2419 start-point
2420 how-far) ; putative scanning distance.
2421 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2422 (cond
2423 ((< here (c-state-get-min-scan-pos))
2424 (setq strategy 'IN-LIT
2425 start-point nil
2426 cache-pos nil
2427 how-far 0))
2428 ((<= good-pos here)
2429 (setq strategy 'forward
2430 start-point (max good-pos cache-pos)
2431 how-far (- here start-point)))
2432 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2433 (setq strategy 'backward
2434 how-far (- good-pos here)))
2436 (setq strategy 'forward
2437 how-far (- here cache-pos)
2438 start-point cache-pos)))
2440 ;; Might we be better off starting from the top level, two defuns back,
2441 ;; instead?
2442 (when (> how-far c-state-cache-too-far)
2443 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2444 (if (< (- here BOD-pos) how-far)
2445 (setq strategy 'BOD
2446 start-point BOD-pos)))
2448 (list
2449 strategy
2450 (and (memq strategy '(forward backward)) cache-pos)
2451 (and (memq strategy '(forward BOD)) start-point))))
2454 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2455 ;; Routines which change `c-state-cache' and associated values.
2456 (defun c-renarrow-state-cache ()
2457 ;; The region (more precisely, point-min) has changed since we
2458 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2459 (if (< (point-min) c-state-point-min)
2460 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2461 ;; It would be possible to do a better job here and recalculate the top
2462 ;; only.
2463 (progn
2464 (c-state-mark-point-min-literal)
2465 (setq c-state-cache nil
2466 c-state-cache-good-pos c-state-min-scan-pos
2467 c-state-brace-pair-desert nil))
2469 ;; point-min has MOVED FORWARD.
2471 ;; Is the new point-min inside a (different) literal?
2472 (unless (and c-state-point-min-lit-start ; at prev. point-min
2473 (< (point-min) (c-state-get-min-scan-pos)))
2474 (c-state-mark-point-min-literal))
2476 ;; Cut off a bit of the tail from `c-state-cache'.
2477 (let ((ptr (cons nil c-state-cache))
2479 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2480 (>= pa (point-min)))
2481 (setq ptr (cdr ptr)))
2483 (when (consp ptr)
2484 (if (eq (cdr ptr) c-state-cache)
2485 (setq c-state-cache nil
2486 c-state-cache-good-pos c-state-min-scan-pos)
2487 (setcdr ptr nil)
2488 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2491 (setq c-state-point-min (point-min)))
2493 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2494 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2495 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2496 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2497 ;; the highest brace pair whose "}" is below UPPER-LIM.
2499 ;; Return non-nil when this has been done.
2501 ;; This routine should be fast. Since it can get called a LOT, we maintain
2502 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2503 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2504 (save-excursion
2505 (save-restriction
2506 (let ((bra from) ce ; Positions of "{" and "}".
2507 new-cons
2508 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2509 (macro-start-or-from
2510 (progn (goto-char from)
2511 (c-beginning-of-macro)
2512 (point))))
2513 (or upper-lim (setq upper-lim from))
2515 ;; If we're essentially repeating a fruitless search, just give up.
2516 (unless (and c-state-brace-pair-desert
2517 (eq cache-pos (car c-state-brace-pair-desert))
2518 (<= from (cdr c-state-brace-pair-desert)))
2519 ;; Only search what we absolutely need to:
2520 (if (and c-state-brace-pair-desert
2521 (eq cache-pos (car c-state-brace-pair-desert)))
2522 (narrow-to-region (cdr c-state-brace-pair-desert) (point-max)))
2524 ;; In the next pair of nested loops, the inner one moves back past a
2525 ;; pair of (mis-)matching parens or brackets; the outer one moves
2526 ;; back over a sequence of unmatched close brace/paren/bracket each
2527 ;; time round.
2528 (while
2529 (progn
2530 (c-safe
2531 (while
2532 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2533 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2534 (or (> ce upper-lim)
2535 (not (eq (char-after bra) ?\{))
2536 (and (goto-char bra)
2537 (c-beginning-of-macro)
2538 (< (point) macro-start-or-from))))))
2539 (and ce (< ce bra)))
2540 (setq bra ce)) ; If we just backed over an unbalanced closing
2541 ; brace, ignore it.
2543 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2544 ;; We've found the desired brace-pair.
2545 (progn
2546 (setq new-cons (cons bra (1+ ce)))
2547 (cond
2548 ((consp (car c-state-cache))
2549 (setcar c-state-cache new-cons))
2550 ((and (numberp (car c-state-cache)) ; probably never happens
2551 (< ce (car c-state-cache)))
2552 (setcdr c-state-cache
2553 (cons new-cons (cdr c-state-cache))))
2554 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2556 ;; We haven't found a brace pair. Record this.
2557 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2559 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2560 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2561 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2562 ;; "push" "a" brace pair onto `c-state-cache'.
2564 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2565 ;; otherwise push it normally.
2567 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2568 ;; latter is inside a macro, not being a macro containing
2569 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2570 ;; base pair. This latter case is assumed to be rare.
2572 ;; Note: POINT is not preserved in this routine.
2573 (if bra+1
2574 (if (or (> bra+1 macro-start-or-here)
2575 (progn (goto-char bra+1)
2576 (not (c-beginning-of-macro))))
2577 (setq c-state-cache
2578 (cons (cons (1- bra+1)
2579 (scan-lists bra+1 1 1))
2580 (if (consp (car c-state-cache))
2581 (cdr c-state-cache)
2582 c-state-cache)))
2583 ;; N.B. This defsubst codes one method for the simple, normal case,
2584 ;; and a more sophisticated, slower way for the general case. Don't
2585 ;; eliminate this defsubst - it's a speed optimization.
2586 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2588 (defun c-append-to-state-cache (from)
2589 ;; Scan the buffer from FROM to (point-max), adding elements into
2590 ;; `c-state-cache' for braces etc. Return a candidate for
2591 ;; `c-state-cache-good-pos'.
2593 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2594 ;; any. Typically, it is immediately after it. It must not be inside a
2595 ;; literal.
2596 (let ((here-bol (c-point 'bol (point-max)))
2597 (macro-start-or-here
2598 (save-excursion (goto-char (point-max))
2599 (if (c-beginning-of-macro)
2600 (point)
2601 (point-max))))
2602 pa+1 ; pos just after an opening PAren (or brace).
2603 (ren+1 from) ; usually a pos just after an closing paREN etc.
2604 ; Is actually the pos. to scan for a (/{/[ from,
2605 ; which sometimes is after a silly )/}/].
2606 paren+1 ; Pos after some opening or closing paren.
2607 paren+1s ; A list of `paren+1's; used to determine a
2608 ; good-pos.
2609 bra+1 ce+1 ; just after L/R bra-ces.
2610 bra+1s ; list of OLD values of bra+1.
2611 mstart) ; start of a macro.
2613 (save-excursion
2614 ;; Each time round the following loop, we enter a successively deeper
2615 ;; level of brace/paren nesting. (Except sometimes we "continue at
2616 ;; the existing level".) `pa+1' is a pos inside an opening
2617 ;; brace/paren/bracket, usually just after it.
2618 (while
2619 (progn
2620 ;; Each time round the next loop moves forward over an opening then
2621 ;; a closing brace/bracket/paren. This loop is white hot, so it
2622 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2623 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2624 ;; call of `scan-lists' signals an error, which happens when there
2625 ;; are no more b/b/p's to scan.
2626 (c-safe
2627 (while t
2628 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2629 paren+1s (cons pa+1 paren+1s))
2630 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2631 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2632 (setq bra+1 pa+1))
2633 (setcar paren+1s ren+1)))
2635 (if (and pa+1 (> pa+1 ren+1))
2636 ;; We've just entered a deeper nesting level.
2637 (progn
2638 ;; Insert the brace pair (if present) and the single open
2639 ;; paren/brace/bracket into `c-state-cache' It cannot be
2640 ;; inside a macro, except one around point, because of what
2641 ;; `c-neutralize-syntax-in-CPP' has done.
2642 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2643 ;; Insert the opening brace/bracket/paren position.
2644 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2645 ;; Clear admin stuff for the next more nested part of the scan.
2646 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2647 t) ; Carry on the loop
2649 ;; All open p/b/b's at this nesting level, if any, have probably
2650 ;; been closed by matching/mismatching ones. We're probably
2651 ;; finished - we just need to check for having found an
2652 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2653 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2654 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2656 ;; Record the final, innermost, brace-pair if there is one.
2657 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2659 ;; Determine a good pos
2660 (while (and (setq paren+1 (car paren+1s))
2661 (> (if (> paren+1 macro-start-or-here)
2662 paren+1
2663 (goto-char paren+1)
2664 (setq mstart (and (c-beginning-of-macro)
2665 (point)))
2666 (or mstart paren+1))
2667 here-bol))
2668 (setq paren+1s (cdr paren+1s)))
2669 (cond
2670 ((and paren+1 mstart)
2671 (min paren+1 mstart))
2672 (paren+1)
2673 (t from)))))
2675 (defun c-remove-stale-state-cache (good-pos pps-point)
2676 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2677 ;; not be in it when it is amended for position (point-max).
2678 ;; Additionally, the "outermost" open-brace entry before (point-max)
2679 ;; will be converted to a cons if the matching close-brace is scanned.
2681 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2682 ;; parens/braces/brackets between GOOD-POS and (point-max).
2684 ;; As a second thing, calculate the result of parse-partial-sexp at
2685 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2686 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2687 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2688 ;; needs to be FAST).
2690 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2691 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2692 ;; to be good (we aim for this to be as high as possible);
2693 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2694 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2695 ;; position to scan backwards from.
2696 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2697 (save-restriction
2698 (narrow-to-region 1 (point-max))
2699 (save-excursion
2700 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2701 (save-excursion
2702 (goto-char (point-max))
2703 (and (c-beginning-of-macro)
2704 (point))))
2705 (good-pos-actual-macro-start ; Start of macro containing good-pos
2706 ; or nil
2707 (and (< good-pos (point-max))
2708 (save-excursion
2709 (goto-char good-pos)
2710 (and (c-beginning-of-macro)
2711 (point)))))
2712 (good-pos-actual-macro-end ; End of this macro, (maybe
2713 ; (point-max)), or nil.
2714 (and good-pos-actual-macro-start
2715 (save-excursion
2716 (goto-char good-pos-actual-macro-start)
2717 (c-end-of-macro)
2718 (point))))
2719 pps-state ; Will be 9 or 10 elements long.
2721 upper-lim ; ,beyond which `c-state-cache' entries are removed
2722 scan-back-pos
2723 pair-beg pps-point-state target-depth)
2725 ;; Remove entries beyond (point-max). Also remove any entries inside
2726 ;; a macro, unless (point-max) is in the same macro.
2727 (setq upper-lim
2728 (if (or (null c-state-old-cpp-beg)
2729 (and (> (point-max) c-state-old-cpp-beg)
2730 (< (point-max) c-state-old-cpp-end)))
2731 (point-max)
2732 (min (point-max) c-state-old-cpp-beg)))
2733 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2734 (setq c-state-cache (cdr c-state-cache)))
2735 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2736 ;; RBrace and indicate we'll need to search backwards for a previous
2737 ;; brace pair.
2738 (when (and c-state-cache
2739 (consp (car c-state-cache))
2740 (> (cdar c-state-cache) upper-lim))
2741 (setcar c-state-cache (caar c-state-cache))
2742 (setq scan-back-pos (car c-state-cache)))
2744 ;; The next loop jumps forward out of a nested level of parens each
2745 ;; time round; the corresponding elements in `c-state-cache' are
2746 ;; removed. `pos' is just after the brace-pair or the open paren at
2747 ;; (car c-state-cache). There can be no open parens/braces/brackets
2748 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2749 ;; due to the interface spec to this function.
2750 (setq pos (if (and good-pos-actual-macro-end
2751 (not (eq good-pos-actual-macro-start
2752 in-macro-start)))
2753 (1+ good-pos-actual-macro-end) ; get outside the macro as
2754 ; marked by a `category' text property.
2755 good-pos))
2756 (goto-char pos)
2757 (while (and c-state-cache
2758 (< (point) (point-max)))
2759 (cond
2760 ((null pps-state) ; first time through
2761 (setq target-depth -1))
2762 ((eq (car pps-state) target-depth) ; found closing ),},]
2763 (setq target-depth (1- (car pps-state))))
2764 ;; Do nothing when we've merely reached pps-point.
2767 ;; Scan!
2768 (setq pps-state
2769 (parse-partial-sexp
2770 (point) (if (< (point) pps-point) pps-point (point-max))
2771 target-depth
2772 nil pps-state))
2774 (if (= (point) pps-point)
2775 (setq pps-point-state pps-state))
2777 (when (eq (car pps-state) target-depth)
2778 (setq pos (point)) ; POS is now just after an R-paren/brace.
2779 (cond
2780 ((and (consp (car c-state-cache))
2781 (eq (point) (cdar c-state-cache)))
2782 ;; We've just moved out of the paren pair containing the brace-pair
2783 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2784 ;; and is potentially where the open brace of a cons in
2785 ;; c-state-cache will be.
2786 (setq pair-beg (car-safe (cdr c-state-cache))
2787 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2788 ((numberp (car c-state-cache))
2789 (setq pair-beg (car c-state-cache)
2790 c-state-cache (cdr c-state-cache))) ; remove this
2791 ; containing Lparen
2792 ((numberp (cadr c-state-cache))
2793 (setq pair-beg (cadr c-state-cache)
2794 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2795 ; together with enclosed brace pair.
2796 ;; (t nil) ; Ignore an unmated Rparen.
2799 (if (< (point) pps-point)
2800 (setq pps-state (parse-partial-sexp (point) pps-point
2801 nil nil ; TARGETDEPTH, STOPBEFORE
2802 pps-state)))
2804 ;; If the last paren pair we moved out of was actually a brace pair,
2805 ;; insert it into `c-state-cache'.
2806 (when (and pair-beg (eq (char-after pair-beg) ?{))
2807 (if (consp (car-safe c-state-cache))
2808 (setq c-state-cache (cdr c-state-cache)))
2809 (setq c-state-cache (cons (cons pair-beg pos)
2810 c-state-cache)))
2812 (list pos scan-back-pos pps-state)))))
2814 (defun c-remove-stale-state-cache-backwards (here cache-pos)
2815 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2816 ;; buffer, and inform the caller of the scenario detected.
2818 ;; HERE is the position we're setting `c-state-cache' for.
2819 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2820 ;; before HERE, or a position at or near point-min which isn't in a
2821 ;; literal.
2823 ;; This function must only be called only when (> `c-state-cache-good-pos'
2824 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2825 ;; optimized to eliminate (or minimize) scanning between these two
2826 ;; positions.
2828 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2829 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2830 ;; could become so after missing elements are inserted into
2831 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2832 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2833 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2834 ;; before `here''s line, or the start of the literal containing it.
2835 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2836 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2837 ;; to scan backwards from.
2838 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2839 ;; POS and HERE which aren't recorded in `c-state-cache'.
2841 ;; The comments in this defun use "paren" to mean parenthesis or square
2842 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2844 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2845 ;; | | | | | |
2846 ;; CP E here D C good
2847 (let ((pos c-state-cache-good-pos)
2848 pa ren ; positions of "(" and ")"
2849 dropped-cons ; whether the last element dropped from `c-state-cache'
2850 ; was a cons (representing a brace-pair)
2851 good-pos ; see above.
2852 lit ; (START . END) of a literal containing some point.
2853 here-lit-start here-lit-end ; bounds of literal containing `here'
2854 ; or `here' itself.
2855 here- here+ ; start/end of macro around HERE, or HERE
2856 (here-bol (c-point 'bol here))
2857 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
2859 ;; Remove completely irrelevant entries from `c-state-cache'.
2860 (while (and c-state-cache
2861 (>= (setq pa (c-state-cache-top-lparen)) here))
2862 (setq dropped-cons (consp (car c-state-cache)))
2863 (setq c-state-cache (cdr c-state-cache))
2864 (setq pos pa))
2865 ;; At this stage, (> pos here);
2866 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2868 (cond
2869 ((and (consp (car c-state-cache))
2870 (> (cdar c-state-cache) here))
2871 ;; CASE 1: The top of the cache is a brace pair which now encloses
2872 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2873 ;; knowledge of what's inside these braces, we have no alternative but
2874 ;; to direct the caller to scan the buffer from the opening brace.
2875 (setq pos (caar c-state-cache))
2876 (setcar c-state-cache pos)
2877 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
2878 ; entry into a { entry, so the caller needs to
2879 ; search for a brace pair before the {.
2881 ;; `here' might be inside a literal. Check for this.
2882 ((progn
2883 (setq lit (c-state-literal-at here)
2884 here-lit-start (or (car lit) here)
2885 here-lit-end (or (cdr lit) here))
2886 ;; Has `here' just "newly entered" a macro?
2887 (save-excursion
2888 (goto-char here-lit-start)
2889 (if (and (c-beginning-of-macro)
2890 (or (null c-state-old-cpp-beg)
2891 (not (= (point) c-state-old-cpp-beg))))
2892 (progn
2893 (setq here- (point))
2894 (c-end-of-macro)
2895 (setq here+ (point)))
2896 (setq here- here-lit-start
2897 here+ here-lit-end)))
2899 ;; `here' might be nested inside any depth of parens (or brackets but
2900 ;; not braces). Scan backwards to find the outermost such opening
2901 ;; paren, if there is one. This will be the scan position to return.
2902 (save-restriction
2903 (narrow-to-region cache-pos (point-max))
2904 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
2905 nil)) ; for the cond
2907 ((< pos here-lit-start)
2908 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2909 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2910 ;; a brace pair preceding this, it will already be in `c-state-cache',
2911 ;; unless there was a brace pair after it, i.e. there'll only be one to
2912 ;; scan for if we've just deleted one.
2913 (list pos (and dropped-cons pos) t)) ; Return value.
2915 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2916 ;; Further forward scanning isn't needed, but we still need to find a
2917 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2918 ((progn
2919 (save-restriction
2920 (narrow-to-region here-bol (point-max))
2921 (setq pos here-lit-start)
2922 (c-safe (while (setq pa (scan-lists pos -1 1))
2923 (setq pos pa)))) ; might signal
2924 nil)) ; for the cond
2926 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
2927 ;; CASE 3: After a }/)/] before `here''s BOL.
2928 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
2931 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2932 ;; literal containing it.
2933 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
2934 (list good-pos (and dropped-cons good-pos) nil)))))
2937 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2938 ;; Externally visible routines.
2940 (defun c-state-cache-init ()
2941 (setq c-state-cache nil
2942 c-state-cache-good-pos 1
2943 c-state-nonlit-pos-cache nil
2944 c-state-nonlit-pos-cache-limit 1
2945 c-state-brace-pair-desert nil
2946 c-state-point-min 1
2947 c-state-point-min-lit-type nil
2948 c-state-point-min-lit-start nil
2949 c-state-min-scan-pos 1
2950 c-state-old-cpp-beg nil
2951 c-state-old-cpp-end nil)
2952 (c-state-mark-point-min-literal))
2954 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2955 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
2956 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
2957 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
2958 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
2959 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
2960 ;; (defun c-state-dump ()
2961 ;; ;; For debugging.
2962 ;; ;(message
2963 ;; (concat
2964 ;; (c-sc-qde c-state-cache)
2965 ;; (c-sc-de c-state-cache-good-pos)
2966 ;; (c-sc-qde c-state-nonlit-pos-cache)
2967 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
2968 ;; (c-sc-qde c-state-brace-pair-desert)
2969 ;; (c-sc-de c-state-point-min)
2970 ;; (c-sc-de c-state-point-min-lit-type)
2971 ;; (c-sc-de c-state-point-min-lit-start)
2972 ;; (c-sc-de c-state-min-scan-pos)
2973 ;; (c-sc-de c-state-old-cpp-beg)
2974 ;; (c-sc-de c-state-old-cpp-end)))
2975 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2977 (defun c-invalidate-state-cache-1 (here)
2978 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2979 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2980 ;; left in a consistent state.
2982 ;; This is much like `c-whack-state-after', but it never changes a paren
2983 ;; pair element into an open paren element. Doing that would mean that the
2984 ;; new open paren wouldn't have the required preceding paren pair element.
2986 ;; This function is called from c-after-change.
2988 ;; The cache of non-literals:
2989 (if (< here c-state-nonlit-pos-cache-limit)
2990 (setq c-state-nonlit-pos-cache-limit here))
2992 ;; `c-state-cache':
2993 ;; Case 1: if `here' is in a literal containing point-min, everything
2994 ;; becomes (or is already) nil.
2995 (if (or (null c-state-cache-good-pos)
2996 (< here (c-state-get-min-scan-pos)))
2997 (setq c-state-cache nil
2998 c-state-cache-good-pos nil
2999 c-state-min-scan-pos nil)
3001 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3002 ;; below `here'. To maintain its consistency, we may need to insert a new
3003 ;; brace pair.
3004 (let ((here-bol (c-point 'bol here))
3005 too-high-pa ; recorded {/(/[ next above here, or nil.
3006 dropped-cons ; was the last removed element a brace pair?
3008 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3009 (while (and c-state-cache
3010 (>= (setq pa (c-state-cache-top-paren)) here))
3011 (setq dropped-cons (consp (car c-state-cache))
3012 too-high-pa (c-state-cache-top-lparen)
3013 c-state-cache (cdr c-state-cache)))
3015 ;; Do we need to add in an earlier brace pair, having lopped one off?
3016 (if (and dropped-cons
3017 (< too-high-pa (+ here c-state-cache-too-far)))
3018 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
3019 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3020 (c-state-get-min-scan-pos)))))
3022 ;; The brace-pair desert marker:
3023 (when (car c-state-brace-pair-desert)
3024 (if (< here (car c-state-brace-pair-desert))
3025 (setq c-state-brace-pair-desert nil)
3026 (if (< here (cdr c-state-brace-pair-desert))
3027 (setcdr c-state-brace-pair-desert here)))))
3029 (defun c-parse-state-1 ()
3030 ;; Find and record all noteworthy parens between some good point earlier in
3031 ;; the file and point. That good point is at least the beginning of the
3032 ;; top-level construct we are in, or the beginning of the preceding
3033 ;; top-level construct if we aren't in one.
3035 ;; The returned value is a list of the noteworthy parens with the last one
3036 ;; first. If an element in the list is an integer, it's the position of an
3037 ;; open paren (of any type) which has not been closed before the point. If
3038 ;; an element is a cons, it gives the position of a closed BRACE paren
3039 ;; pair[*]; the car is the start brace position and the cdr is the position
3040 ;; following the closing brace. Only the last closed brace paren pair
3041 ;; before each open paren and before the point is recorded, and thus the
3042 ;; state never contains two cons elements in succession. When a close brace
3043 ;; has no matching open brace (e.g., the matching brace is outside the
3044 ;; visible region), it is not represented in the returned value.
3046 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3047 ;; This defun explicitly treats mismatching parens/braces/brackets as
3048 ;; matching. It is the open brace which makes it a "brace" pair.
3050 ;; If POINT is within a macro, open parens and brace pairs within
3051 ;; THIS macro MIGHT be recorded. This depends on whether their
3052 ;; syntactic properties have been suppressed by
3053 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3055 ;; Currently no characters which are given paren syntax with the
3056 ;; syntax-table property are recorded, i.e. angle bracket arglist
3057 ;; parens are never present here. Note that this might change.
3059 ;; BUG: This function doesn't cope entirely well with unbalanced
3060 ;; parens in macros. (2008-12-11: this has probably been resolved
3061 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3062 ;; following case the brace before the macro isn't balanced with the
3063 ;; one after it:
3065 ;; {
3066 ;; #define X {
3067 ;; }
3069 ;; Note to maintainers: this function DOES get called with point
3070 ;; within comments and strings, so don't assume it doesn't!
3072 ;; This function might do hidden buffer changes.
3073 (let* ((here (point))
3074 (here-bopl (c-point 'bopl))
3075 strategy ; 'forward, 'backward etc..
3076 ;; Candidate positions to start scanning from:
3077 cache-pos ; highest position below HERE already existing in
3078 ; cache (or 1).
3079 good-pos
3080 start-point
3081 bopl-state
3083 scan-backward-pos scan-forward-p) ; used for 'backward.
3084 ;; If POINT-MIN has changed, adjust the cache
3085 (unless (= (point-min) c-state-point-min)
3086 (c-renarrow-state-cache))
3088 ;; Strategy?
3089 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3090 strategy (car res)
3091 cache-pos (cadr res)
3092 start-point (nth 2 res))
3094 (when (eq strategy 'BOD)
3095 (setq c-state-cache nil
3096 c-state-cache-good-pos start-point))
3098 ;; SCAN!
3099 (save-restriction
3100 (cond
3101 ((memq strategy '(forward BOD))
3102 (narrow-to-region (point-min) here)
3103 (setq res (c-remove-stale-state-cache start-point here-bopl))
3104 (setq cache-pos (car res)
3105 scan-backward-pos (cadr res)
3106 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
3107 ; start-point)
3108 (if scan-backward-pos
3109 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
3110 (setq good-pos
3111 (c-append-to-state-cache cache-pos))
3112 (setq c-state-cache-good-pos
3113 (if (and bopl-state
3114 (< good-pos (- here c-state-cache-too-far)))
3115 (c-state-cache-non-literal-place here-bopl bopl-state)
3116 good-pos)))
3118 ((eq strategy 'backward)
3119 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
3120 good-pos (car res)
3121 scan-backward-pos (cadr res)
3122 scan-forward-p (car (cddr res)))
3123 (if scan-backward-pos
3124 (c-append-lower-brace-pair-to-state-cache
3125 scan-backward-pos))
3126 (setq c-state-cache-good-pos
3127 (if scan-forward-p
3128 (progn (narrow-to-region (point-min) here)
3129 (c-append-to-state-cache good-pos))
3131 (c-get-cache-scan-pos good-pos))))
3133 (t ; (eq strategy 'IN-LIT)
3134 (setq c-state-cache nil
3135 c-state-cache-good-pos nil)))))
3137 c-state-cache)
3139 (defun c-invalidate-state-cache (here)
3140 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3142 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3143 ;; of all parens in preprocessor constructs, except for any such construct
3144 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3145 ;; worrying further about macros and template delimiters.
3146 (c-with-<->-as-parens-suppressed
3147 (if (and c-state-old-cpp-beg
3148 (< c-state-old-cpp-beg here))
3149 (c-with-all-but-one-cpps-commented-out
3150 c-state-old-cpp-beg
3151 (min c-state-old-cpp-end here)
3152 (c-invalidate-state-cache-1 here))
3153 (c-with-cpps-commented-out
3154 (c-invalidate-state-cache-1 here)))))
3156 (defun c-parse-state ()
3157 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3158 ;; description of the functionality and return value.
3160 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3161 ;; of all parens in preprocessor constructs, except for any such construct
3162 ;; containing point. We can then call `c-parse-state-1' without worrying
3163 ;; further about macros and template delimiters.
3164 (let (here-cpp-beg here-cpp-end)
3165 (save-excursion
3166 (when (c-beginning-of-macro)
3167 (setq here-cpp-beg (point))
3168 (unless
3169 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3170 here-cpp-beg)
3171 (setq here-cpp-beg nil here-cpp-end nil))))
3172 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3173 ;; subsystem.
3174 (prog1
3175 (c-with-<->-as-parens-suppressed
3176 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3177 (c-with-all-but-one-cpps-commented-out
3178 here-cpp-beg here-cpp-end
3179 (c-parse-state-1))
3180 (c-with-cpps-commented-out
3181 (c-parse-state-1))))
3182 (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
3183 c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
3186 ;; Debug tool to catch cache inconsistencies. This is called from
3187 ;; 000tests.el.
3188 (defvar c-debug-parse-state nil)
3189 (unless (fboundp 'c-real-parse-state)
3190 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3191 (cc-bytecomp-defun c-real-parse-state)
3193 (defvar c-parse-state-state nil)
3194 (defun c-record-parse-state-state ()
3195 (setq c-parse-state-state
3196 (mapcar
3197 (lambda (arg)
3198 (cons arg (symbol-value arg)))
3199 '(c-state-cache
3200 c-state-cache-good-pos
3201 c-state-nonlit-pos-cache
3202 c-state-nonlit-pos-cache-limit
3203 c-state-brace-pair-desert
3204 c-state-point-min
3205 c-state-point-min-lit-type
3206 c-state-point-min-lit-start
3207 c-state-min-scan-pos
3208 c-state-old-cpp-beg
3209 c-state-old-cpp-end))))
3210 (defun c-replay-parse-state-state ()
3211 (message
3212 (concat "(setq "
3213 (mapconcat
3214 (lambda (arg)
3215 (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
3216 c-parse-state-state " ")
3217 ")")))
3219 (defun c-debug-parse-state ()
3220 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3221 (let ((c-state-cache nil)
3222 (c-state-cache-good-pos 1)
3223 (c-state-nonlit-pos-cache nil)
3224 (c-state-nonlit-pos-cache-limit 1)
3225 (c-state-brace-pair-desert nil)
3226 (c-state-point-min 1)
3227 (c-state-point-min-lit-type nil)
3228 (c-state-point-min-lit-start nil)
3229 (c-state-min-scan-pos 1)
3230 (c-state-old-cpp-beg nil)
3231 (c-state-old-cpp-end nil))
3232 (setq res2 (c-real-parse-state)))
3233 (unless (equal res1 res2)
3234 ;; The cache can actually go further back due to the ad-hoc way
3235 ;; the first paren is found, so try to whack off a bit of its
3236 ;; start before complaining.
3237 ;; (save-excursion
3238 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3239 ;; (c-beginning-of-defun-1)
3240 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3241 ;; (c-beginning-of-defun-1))
3242 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3243 ;; (message (concat "c-parse-state inconsistency at %s: "
3244 ;; "using cache: %s, from scratch: %s")
3245 ;; here res1 res2)))
3246 (message (concat "c-parse-state inconsistency at %s: "
3247 "using cache: %s, from scratch: %s")
3248 here res1 res2)
3249 (message "Old state:")
3250 (c-replay-parse-state-state))
3251 (c-record-parse-state-state)
3252 res1))
3254 (defun c-toggle-parse-state-debug (&optional arg)
3255 (interactive "P")
3256 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3257 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3258 'c-debug-parse-state
3259 'c-real-parse-state)))
3260 (c-keep-region-active))
3261 (when c-debug-parse-state
3262 (c-toggle-parse-state-debug 1))
3265 (defun c-whack-state-before (bufpos paren-state)
3266 ;; Whack off any state information from PAREN-STATE which lies
3267 ;; before BUFPOS. Not destructive on PAREN-STATE.
3268 (let* ((newstate (list nil))
3269 (ptr newstate)
3270 car)
3271 (while paren-state
3272 (setq car (car paren-state)
3273 paren-state (cdr paren-state))
3274 (if (< (if (consp car) (car car) car) bufpos)
3275 (setq paren-state nil)
3276 (setcdr ptr (list car))
3277 (setq ptr (cdr ptr))))
3278 (cdr newstate)))
3280 (defun c-whack-state-after (bufpos paren-state)
3281 ;; Whack off any state information from PAREN-STATE which lies at or
3282 ;; after BUFPOS. Not destructive on PAREN-STATE.
3283 (catch 'done
3284 (while paren-state
3285 (let ((car (car paren-state)))
3286 (if (consp car)
3287 ;; just check the car, because in a balanced brace
3288 ;; expression, it must be impossible for the corresponding
3289 ;; close brace to be before point, but the open brace to
3290 ;; be after.
3291 (if (<= bufpos (car car))
3292 nil ; whack it off
3293 (if (< bufpos (cdr car))
3294 ;; its possible that the open brace is before
3295 ;; bufpos, but the close brace is after. In that
3296 ;; case, convert this to a non-cons element. The
3297 ;; rest of the state is before bufpos, so we're
3298 ;; done.
3299 (throw 'done (cons (car car) (cdr paren-state)))
3300 ;; we know that both the open and close braces are
3301 ;; before bufpos, so we also know that everything else
3302 ;; on state is before bufpos.
3303 (throw 'done paren-state)))
3304 (if (<= bufpos car)
3305 nil ; whack it off
3306 ;; it's before bufpos, so everything else should too.
3307 (throw 'done paren-state)))
3308 (setq paren-state (cdr paren-state)))
3309 nil)))
3311 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3312 ;; Return the bufpos of the innermost enclosing open paren before
3313 ;; bufpos, or nil if none was found.
3314 (let (enclosingp)
3315 (or bufpos (setq bufpos 134217727))
3316 (while paren-state
3317 (setq enclosingp (car paren-state)
3318 paren-state (cdr paren-state))
3319 (if (or (consp enclosingp)
3320 (>= enclosingp bufpos))
3321 (setq enclosingp nil)
3322 (setq paren-state nil)))
3323 enclosingp))
3325 (defun c-least-enclosing-brace (paren-state)
3326 ;; Return the bufpos of the outermost enclosing open paren, or nil
3327 ;; if none was found.
3328 (let (pos elem)
3329 (while paren-state
3330 (setq elem (car paren-state)
3331 paren-state (cdr paren-state))
3332 (if (integerp elem)
3333 (setq pos elem)))
3334 pos))
3336 (defun c-safe-position (bufpos paren-state)
3337 ;; Return the closest "safe" position recorded on PAREN-STATE that
3338 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3339 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3340 ;; find the closest limit before a given limit that might be nil.
3342 ;; A "safe" position is a position at or after a recorded open
3343 ;; paren, or after a recorded close paren. The returned position is
3344 ;; thus either the first position after a close brace, or the first
3345 ;; position after an enclosing paren, or at the enclosing paren in
3346 ;; case BUFPOS is immediately after it.
3347 (when bufpos
3348 (let (elem)
3349 (catch 'done
3350 (while paren-state
3351 (setq elem (car paren-state))
3352 (if (consp elem)
3353 (cond ((< (cdr elem) bufpos)
3354 (throw 'done (cdr elem)))
3355 ((< (car elem) bufpos)
3356 ;; See below.
3357 (throw 'done (min (1+ (car elem)) bufpos))))
3358 (if (< elem bufpos)
3359 ;; elem is the position at and not after the opening paren, so
3360 ;; we can go forward one more step unless it's equal to
3361 ;; bufpos. This is useful in some cases avoid an extra paren
3362 ;; level between the safe position and bufpos.
3363 (throw 'done (min (1+ elem) bufpos))))
3364 (setq paren-state (cdr paren-state)))))))
3366 (defun c-beginning-of-syntax ()
3367 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3368 ;; goes to the closest previous point that is known to be outside
3369 ;; any string literal or comment. `c-state-cache' is used if it has
3370 ;; a position in the vicinity.
3371 (let* ((paren-state c-state-cache)
3372 elem
3374 (pos (catch 'done
3375 ;; Note: Similar code in `c-safe-position'. The
3376 ;; difference is that we accept a safe position at
3377 ;; the point and don't bother to go forward past open
3378 ;; parens.
3379 (while paren-state
3380 (setq elem (car paren-state))
3381 (if (consp elem)
3382 (cond ((<= (cdr elem) (point))
3383 (throw 'done (cdr elem)))
3384 ((<= (car elem) (point))
3385 (throw 'done (car elem))))
3386 (if (<= elem (point))
3387 (throw 'done elem)))
3388 (setq paren-state (cdr paren-state)))
3389 (point-min))))
3391 (if (> pos (- (point) 4000))
3392 (goto-char pos)
3393 ;; The position is far back. Try `c-beginning-of-defun-1'
3394 ;; (although we can't be entirely sure it will go to a position
3395 ;; outside a comment or string in current emacsen). FIXME:
3396 ;; Consult `syntax-ppss' here.
3397 (c-beginning-of-defun-1)
3398 (if (< (point) pos)
3399 (goto-char pos)))))
3402 ;; Tools for scanning identifiers and other tokens.
3404 (defun c-on-identifier ()
3405 "Return non-nil if the point is on or directly after an identifier.
3406 Keywords are recognized and not considered identifiers. If an
3407 identifier is detected, the returned value is its starting position.
3408 If an identifier ends at the point and another begins at it \(can only
3409 happen in Pike) then the point for the preceding one is returned.
3411 Note that this function might do hidden buffer changes. See the
3412 comment at the start of cc-engine.el for more info."
3414 ;; FIXME: Shouldn't this function handle "operator" in C++?
3416 (save-excursion
3417 (skip-syntax-backward "w_")
3421 ;; Check for a normal (non-keyword) identifier.
3422 (and (looking-at c-symbol-start)
3423 (not (looking-at c-keywords-regexp))
3424 (point))
3426 (when (c-major-mode-is 'pike-mode)
3427 ;; Handle the `<operator> syntax in Pike.
3428 (let ((pos (point)))
3429 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3430 (and (if (< (skip-chars-backward "`") 0)
3432 (goto-char pos)
3433 (eq (char-after) ?\`))
3434 (looking-at c-symbol-key)
3435 (>= (match-end 0) pos)
3436 (point))))
3438 ;; Handle the "operator +" syntax in C++.
3439 (when (and c-overloadable-operators-regexp
3440 (= (c-backward-token-2 0) 0))
3442 (cond ((and (looking-at c-overloadable-operators-regexp)
3443 (or (not c-opt-op-identifier-prefix)
3444 (and (= (c-backward-token-2 1) 0)
3445 (looking-at c-opt-op-identifier-prefix))))
3446 (point))
3448 ((save-excursion
3449 (and c-opt-op-identifier-prefix
3450 (looking-at c-opt-op-identifier-prefix)
3451 (= (c-forward-token-2 1) 0)
3452 (looking-at c-overloadable-operators-regexp)))
3453 (point))))
3457 (defsubst c-simple-skip-symbol-backward ()
3458 ;; If the point is at the end of a symbol then skip backward to the
3459 ;; beginning of it. Don't move otherwise. Return non-nil if point
3460 ;; moved.
3462 ;; This function might do hidden buffer changes.
3463 (or (< (skip-syntax-backward "w_") 0)
3464 (and (c-major-mode-is 'pike-mode)
3465 ;; Handle the `<operator> syntax in Pike.
3466 (let ((pos (point)))
3467 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3468 (< (skip-chars-backward "`") 0)
3469 (looking-at c-symbol-key)
3470 (>= (match-end 0) pos))
3472 (goto-char pos)
3473 nil)))))
3475 (defun c-beginning-of-current-token (&optional back-limit)
3476 ;; Move to the beginning of the current token. Do not move if not
3477 ;; in the middle of one. BACK-LIMIT may be used to bound the
3478 ;; backward search; if given it's assumed to be at the boundary
3479 ;; between two tokens. Return non-nil if the point is moved, nil
3480 ;; otherwise.
3482 ;; This function might do hidden buffer changes.
3483 (let ((start (point)))
3484 (if (looking-at "\\w\\|\\s_")
3485 (skip-syntax-backward "w_" back-limit)
3486 (when (< (skip-syntax-backward ".()" back-limit) 0)
3487 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3488 (match-end 0))
3489 ;; `c-nonsymbol-token-regexp' should always match
3490 ;; since we've skipped backward over punctuator
3491 ;; or paren syntax, but consume one char in case
3492 ;; it doesn't so that we don't leave point before
3493 ;; some earlier incorrect token.
3494 (1+ (point)))))
3495 (if (<= pos start)
3496 (goto-char pos))))))
3497 (< (point) start)))
3499 (defun c-end-of-current-token (&optional back-limit)
3500 ;; Move to the end of the current token. Do not move if not in the
3501 ;; middle of one. BACK-LIMIT may be used to bound the backward
3502 ;; search; if given it's assumed to be at the boundary between two
3503 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3505 ;; This function might do hidden buffer changes.
3506 (let ((start (point)))
3507 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3508 (skip-syntax-forward "w_"))
3509 ((< (skip-syntax-backward ".()" back-limit) 0)
3510 (while (progn
3511 (if (looking-at c-nonsymbol-token-regexp)
3512 (goto-char (match-end 0))
3513 ;; `c-nonsymbol-token-regexp' should always match since
3514 ;; we've skipped backward over punctuator or paren
3515 ;; syntax, but move forward in case it doesn't so that
3516 ;; we don't leave point earlier than we started with.
3517 (forward-char))
3518 (< (point) start)))))
3519 (> (point) start)))
3521 (defconst c-jump-syntax-balanced
3522 (if (memq 'gen-string-delim c-emacs-features)
3523 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3524 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3526 (defconst c-jump-syntax-unbalanced
3527 (if (memq 'gen-string-delim c-emacs-features)
3528 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3529 "\\w\\|\\s_\\|\\s\""))
3531 (defun c-forward-token-2 (&optional count balanced limit)
3532 "Move forward by tokens.
3533 A token is defined as all symbols and identifiers which aren't
3534 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3535 treated properly). Point is always either left at the beginning of a
3536 token or not moved at all. COUNT specifies the number of tokens to
3537 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3538 moves to the next token beginning only if not already at one. If
3539 BALANCED is true, move over balanced parens, otherwise move into them.
3540 Also, if BALANCED is true, never move out of an enclosing paren.
3542 LIMIT sets the limit for the movement and defaults to the point limit.
3543 The case when LIMIT is set in the middle of a token, comment or macro
3544 is handled correctly, i.e. the point won't be left there.
3546 Return the number of tokens left to move \(positive or negative). If
3547 BALANCED is true, a move over a balanced paren counts as one. Note
3548 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3549 be returned. Thus, a return value of 0 guarantees that point is at
3550 the requested position and a return value less \(without signs) than
3551 COUNT guarantees that point is at the beginning of some token.
3553 Note that this function might do hidden buffer changes. See the
3554 comment at the start of cc-engine.el for more info."
3556 (or count (setq count 1))
3557 (if (< count 0)
3558 (- (c-backward-token-2 (- count) balanced limit))
3560 (let ((jump-syntax (if balanced
3561 c-jump-syntax-balanced
3562 c-jump-syntax-unbalanced))
3563 (last (point))
3564 (prev (point)))
3566 (if (zerop count)
3567 ;; If count is zero we should jump if in the middle of a token.
3568 (c-end-of-current-token))
3570 (save-restriction
3571 (if limit (narrow-to-region (point-min) limit))
3572 (if (/= (point)
3573 (progn (c-forward-syntactic-ws) (point)))
3574 ;; Skip whitespace. Count this as a move if we did in
3575 ;; fact move.
3576 (setq count (max (1- count) 0)))
3578 (if (eobp)
3579 ;; Moved out of bounds. Make sure the returned count isn't zero.
3580 (progn
3581 (if (zerop count) (setq count 1))
3582 (goto-char last))
3584 ;; Use `condition-case' to avoid having the limit tests
3585 ;; inside the loop.
3586 (condition-case nil
3587 (while (and
3588 (> count 0)
3589 (progn
3590 (setq last (point))
3591 (cond ((looking-at jump-syntax)
3592 (goto-char (scan-sexps (point) 1))
3594 ((looking-at c-nonsymbol-token-regexp)
3595 (goto-char (match-end 0))
3597 ;; `c-nonsymbol-token-regexp' above should always
3598 ;; match if there are correct tokens. Try to
3599 ;; widen to see if the limit was set in the
3600 ;; middle of one, else fall back to treating
3601 ;; the offending thing as a one character token.
3602 ((and limit
3603 (save-restriction
3604 (widen)
3605 (looking-at c-nonsymbol-token-regexp)))
3606 nil)
3608 (forward-char)
3609 t))))
3610 (c-forward-syntactic-ws)
3611 (setq prev last
3612 count (1- count)))
3613 (error (goto-char last)))
3615 (when (eobp)
3616 (goto-char prev)
3617 (setq count (1+ count)))))
3619 count)))
3621 (defun c-backward-token-2 (&optional count balanced limit)
3622 "Move backward by tokens.
3623 See `c-forward-token-2' for details."
3625 (or count (setq count 1))
3626 (if (< count 0)
3627 (- (c-forward-token-2 (- count) balanced limit))
3629 (or limit (setq limit (point-min)))
3630 (let ((jump-syntax (if balanced
3631 c-jump-syntax-balanced
3632 c-jump-syntax-unbalanced))
3633 (last (point)))
3635 (if (zerop count)
3636 ;; The count is zero so try to skip to the beginning of the
3637 ;; current token.
3638 (if (> (point)
3639 (progn (c-beginning-of-current-token) (point)))
3640 (if (< (point) limit)
3641 ;; The limit is inside the same token, so return 1.
3642 (setq count 1))
3644 ;; We're not in the middle of a token. If there's
3645 ;; whitespace after the point then we must move backward,
3646 ;; so set count to 1 in that case.
3647 (and (looking-at c-syntactic-ws-start)
3648 ;; If we're looking at a '#' that might start a cpp
3649 ;; directive then we have to do a more elaborate check.
3650 (or (/= (char-after) ?#)
3651 (not c-opt-cpp-prefix)
3652 (save-excursion
3653 (and (= (point)
3654 (progn (beginning-of-line)
3655 (looking-at "[ \t]*")
3656 (match-end 0)))
3657 (or (bobp)
3658 (progn (backward-char)
3659 (not (eq (char-before) ?\\)))))))
3660 (setq count 1))))
3662 ;; Use `condition-case' to avoid having to check for buffer
3663 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3664 (condition-case nil
3665 (while (and
3666 (> count 0)
3667 (progn
3668 (c-backward-syntactic-ws)
3669 (backward-char)
3670 (if (looking-at jump-syntax)
3671 (goto-char (scan-sexps (1+ (point)) -1))
3672 ;; This can be very inefficient if there's a long
3673 ;; sequence of operator tokens without any separation.
3674 ;; That doesn't happen in practice, anyway.
3675 (c-beginning-of-current-token))
3676 (>= (point) limit)))
3677 (setq last (point)
3678 count (1- count)))
3679 (error (goto-char last)))
3681 (if (< (point) limit)
3682 (goto-char last))
3684 count)))
3686 (defun c-forward-token-1 (&optional count balanced limit)
3687 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3688 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3689 characters are jumped over character by character. This function is
3690 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3691 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3692 (c-forward-token-2 count balanced limit)))
3694 (defun c-backward-token-1 (&optional count balanced limit)
3695 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3696 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3697 characters are jumped over character by character. This function is
3698 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3699 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3700 (c-backward-token-2 count balanced limit)))
3703 ;; Tools for doing searches restricted to syntactically relevant text.
3705 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3706 paren-level not-inside-token
3707 lookbehind-submatch)
3708 "Like `re-search-forward', but only report matches that are found
3709 in syntactically significant text. I.e. matches in comments, macros
3710 or string literals are ignored. The start point is assumed to be
3711 outside any comment, macro or string literal, or else the content of
3712 that region is taken as syntactically significant text.
3714 If PAREN-LEVEL is non-nil, an additional restriction is added to
3715 ignore matches in nested paren sexps. The search will also not go
3716 outside the current list sexp, which has the effect that if the point
3717 should be moved to BOUND when no match is found \(i.e. NOERROR is
3718 neither nil nor t), then it will be at the closing paren if the end of
3719 the current list sexp is encountered first.
3721 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3722 ignored. Things like multicharacter operators and special symbols
3723 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3724 constants.
3726 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3727 subexpression in REGEXP. The end of that submatch is used as the
3728 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3729 isn't used or if that subexpression didn't match then the start
3730 position of the whole match is used instead. The \"look behind\"
3731 subexpression is never tested before the starting position, so it
3732 might be a good idea to include \\=\\= as a match alternative in it.
3734 Optimization note: Matches might be missed if the \"look behind\"
3735 subexpression can match the end of nonwhite syntactic whitespace,
3736 i.e. the end of comments or cpp directives. This since the function
3737 skips over such things before resuming the search. It's on the other
3738 hand not safe to assume that the \"look behind\" subexpression never
3739 matches syntactic whitespace.
3741 Bug: Unbalanced parens inside cpp directives are currently not handled
3742 correctly \(i.e. they don't get ignored as they should) when
3743 PAREN-LEVEL is set.
3745 Note that this function might do hidden buffer changes. See the
3746 comment at the start of cc-engine.el for more info."
3748 (or bound (setq bound (point-max)))
3749 (if paren-level (setq paren-level -1))
3751 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3753 (let ((start (point))
3755 ;; Start position for the last search.
3756 search-pos
3757 ;; The `parse-partial-sexp' state between the start position
3758 ;; and the point.
3759 state
3760 ;; The current position after the last state update. The next
3761 ;; `parse-partial-sexp' continues from here.
3762 (state-pos (point))
3763 ;; The position at which to check the state and the state
3764 ;; there. This is separate from `state-pos' since we might
3765 ;; need to back up before doing the next search round.
3766 check-pos check-state
3767 ;; Last position known to end a token.
3768 (last-token-end-pos (point-min))
3769 ;; Set when a valid match is found.
3770 found)
3772 (condition-case err
3773 (while
3774 (and
3775 (progn
3776 (setq search-pos (point))
3777 (re-search-forward regexp bound noerror))
3779 (progn
3780 (setq state (parse-partial-sexp
3781 state-pos (match-beginning 0) paren-level nil state)
3782 state-pos (point))
3783 (if (setq check-pos (and lookbehind-submatch
3784 (or (not paren-level)
3785 (>= (car state) 0))
3786 (match-end lookbehind-submatch)))
3787 (setq check-state (parse-partial-sexp
3788 state-pos check-pos paren-level nil state))
3789 (setq check-pos state-pos
3790 check-state state))
3792 ;; NOTE: If we got a look behind subexpression and get
3793 ;; an insignificant match in something that isn't
3794 ;; syntactic whitespace (i.e. strings or in nested
3795 ;; parentheses), then we can never skip more than a
3796 ;; single character from the match start position
3797 ;; (i.e. `state-pos' here) before continuing the
3798 ;; search. That since the look behind subexpression
3799 ;; might match the end of the insignificant region in
3800 ;; the next search.
3802 (cond
3803 ((elt check-state 7)
3804 ;; Match inside a line comment. Skip to eol. Use
3805 ;; `re-search-forward' instead of `skip-chars-forward' to get
3806 ;; the right bound behavior.
3807 (re-search-forward "[\n\r]" bound noerror))
3809 ((elt check-state 4)
3810 ;; Match inside a block comment. Skip to the '*/'.
3811 (search-forward "*/" bound noerror))
3813 ((and (not (elt check-state 5))
3814 (eq (char-before check-pos) ?/)
3815 (not (c-get-char-property (1- check-pos) 'syntax-table))
3816 (memq (char-after check-pos) '(?/ ?*)))
3817 ;; Match in the middle of the opener of a block or line
3818 ;; comment.
3819 (if (= (char-after check-pos) ?/)
3820 (re-search-forward "[\n\r]" bound noerror)
3821 (search-forward "*/" bound noerror)))
3823 ;; The last `parse-partial-sexp' above might have
3824 ;; stopped short of the real check position if the end
3825 ;; of the current sexp was encountered in paren-level
3826 ;; mode. The checks above are always false in that
3827 ;; case, and since they can do better skipping in
3828 ;; lookbehind-submatch mode, we do them before
3829 ;; checking the paren level.
3831 ((and paren-level
3832 (/= (setq tmp (car check-state)) 0))
3833 ;; Check the paren level first since we're short of the
3834 ;; syntactic checking position if the end of the
3835 ;; current sexp was encountered by `parse-partial-sexp'.
3836 (if (> tmp 0)
3838 ;; Inside a nested paren sexp.
3839 (if lookbehind-submatch
3840 ;; See the NOTE above.
3841 (progn (goto-char state-pos) t)
3842 ;; Skip out of the paren quickly.
3843 (setq state (parse-partial-sexp state-pos bound 0 nil state)
3844 state-pos (point)))
3846 ;; Have exited the current paren sexp.
3847 (if noerror
3848 (progn
3849 ;; The last `parse-partial-sexp' call above
3850 ;; has left us just after the closing paren
3851 ;; in this case, so we can modify the bound
3852 ;; to leave the point at the right position
3853 ;; upon return.
3854 (setq bound (1- (point)))
3855 nil)
3856 (signal 'search-failed (list regexp)))))
3858 ((setq tmp (elt check-state 3))
3859 ;; Match inside a string.
3860 (if (or lookbehind-submatch
3861 (not (integerp tmp)))
3862 ;; See the NOTE above.
3863 (progn (goto-char state-pos) t)
3864 ;; Skip to the end of the string before continuing.
3865 (let ((ender (make-string 1 tmp)) (continue t))
3866 (while (if (search-forward ender bound noerror)
3867 (progn
3868 (setq state (parse-partial-sexp
3869 state-pos (point) nil nil state)
3870 state-pos (point))
3871 (elt state 3))
3872 (setq continue nil)))
3873 continue)))
3875 ((save-excursion
3876 (save-match-data
3877 (c-beginning-of-macro start)))
3878 ;; Match inside a macro. Skip to the end of it.
3879 (c-end-of-macro)
3880 (cond ((<= (point) bound) t)
3881 (noerror nil)
3882 (t (signal 'search-failed (list regexp)))))
3884 ((and not-inside-token
3885 (or (< check-pos last-token-end-pos)
3886 (< check-pos
3887 (save-excursion
3888 (goto-char check-pos)
3889 (save-match-data
3890 (c-end-of-current-token last-token-end-pos))
3891 (setq last-token-end-pos (point))))))
3892 ;; Inside a token.
3893 (if lookbehind-submatch
3894 ;; See the NOTE above.
3895 (goto-char state-pos)
3896 (goto-char (min last-token-end-pos bound))))
3899 ;; A real match.
3900 (setq found t)
3901 nil)))
3903 ;; Should loop to search again, but take care to avoid
3904 ;; looping on the same spot.
3905 (or (/= search-pos (point))
3906 (if (= (point) bound)
3907 (if noerror
3909 (signal 'search-failed (list regexp)))
3910 (forward-char)
3911 t))))
3913 (error
3914 (goto-char start)
3915 (signal (car err) (cdr err))))
3917 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3919 (if found
3920 (progn
3921 (goto-char (match-end 0))
3922 (match-end 0))
3924 ;; Search failed. Set point as appropriate.
3925 (if (eq noerror t)
3926 (goto-char start)
3927 (goto-char bound))
3928 nil)))
3930 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
3932 (defsubst c-ssb-lit-begin ()
3933 ;; Return the start of the literal point is in, or nil.
3934 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3935 ;; bound in the caller.
3937 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3938 ;; if it's outside comments and strings.
3939 (save-excursion
3940 (let ((pos (point)) safe-pos state pps-end-pos)
3941 ;; Pick a safe position as close to the point as possible.
3943 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3944 ;; position.
3946 (while (and safe-pos-list
3947 (> (car safe-pos-list) (point)))
3948 (setq safe-pos-list (cdr safe-pos-list)))
3949 (unless (setq safe-pos (car-safe safe-pos-list))
3950 (setq safe-pos (max (or (c-safe-position
3951 (point) (or c-state-cache
3952 (c-parse-state)))
3954 (point-min))
3955 safe-pos-list (list safe-pos)))
3957 ;; Cache positions along the way to use if we have to back up more. We
3958 ;; cache every closing paren on the same level. If the paren cache is
3959 ;; relevant in this region then we're typically already on the same
3960 ;; level as the target position. Note that we might cache positions
3961 ;; after opening parens in case safe-pos is in a nested list. That's
3962 ;; both uncommon and harmless.
3963 (while (progn
3964 (setq state (parse-partial-sexp
3965 safe-pos pos 0))
3966 (< (point) pos))
3967 (setq safe-pos (point)
3968 safe-pos-list (cons safe-pos safe-pos-list)))
3970 ;; If the state contains the start of the containing sexp we cache that
3971 ;; position too, so that parse-partial-sexp in the next run has a bigger
3972 ;; chance of starting at the same level as the target position and thus
3973 ;; will get more good safe positions into the list.
3974 (if (elt state 1)
3975 (setq safe-pos (1+ (elt state 1))
3976 safe-pos-list (cons safe-pos safe-pos-list)))
3978 (if (or (elt state 3) (elt state 4))
3979 ;; Inside string or comment. Continue search at the
3980 ;; beginning of it.
3981 (elt state 8)))))
3983 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3984 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3985 i.e. don't stop at positions inside syntactic whitespace or string
3986 literals. Preprocessor directives are also ignored, with the exception
3987 of the one that the point starts within, if any. If LIMIT is given,
3988 it's assumed to be at a syntactically relevant position.
3990 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3991 sexps, and the search will also not go outside the current paren sexp.
3992 However, if LIMIT or the buffer limit is reached inside a nested paren
3993 then the point will be left at the limit.
3995 Non-nil is returned if the point moved, nil otherwise.
3997 Note that this function might do hidden buffer changes. See the
3998 comment at the start of cc-engine.el for more info."
4000 (let ((start (point))
4001 state-2
4002 ;; A list of syntactically relevant positions in descending
4003 ;; order. It's used to avoid scanning repeatedly over
4004 ;; potentially large regions with `parse-partial-sexp' to verify
4005 ;; each position. Used in `c-ssb-lit-begin'
4006 safe-pos-list
4007 ;; The result from `c-beginning-of-macro' at the start position or the
4008 ;; start position itself if it isn't within a macro. Evaluated on
4009 ;; demand.
4010 start-macro-beg
4011 ;; The earliest position after the current one with the same paren
4012 ;; level. Used only when `paren-level' is set.
4013 lit-beg
4014 (paren-level-pos (point)))
4016 (while
4017 (progn
4018 ;; The next loop "tries" to find the end point each time round,
4019 ;; loops when it hasn't succeeded.
4020 (while
4021 (and
4022 (< (skip-chars-backward skip-chars limit) 0)
4024 (let ((pos (point)) state-2 pps-end-pos)
4026 (cond
4027 ;; Don't stop inside a literal
4028 ((setq lit-beg (c-ssb-lit-begin))
4029 (goto-char lit-beg)
4032 ((and paren-level
4033 (save-excursion
4034 (setq state-2 (parse-partial-sexp
4035 pos paren-level-pos -1)
4036 pps-end-pos (point))
4037 (/= (car state-2) 0)))
4038 ;; Not at the right level.
4040 (if (and (< (car state-2) 0)
4041 ;; We stop above if we go out of a paren.
4042 ;; Now check whether it precedes or is
4043 ;; nested in the starting sexp.
4044 (save-excursion
4045 (setq state-2
4046 (parse-partial-sexp
4047 pps-end-pos paren-level-pos
4048 nil nil state-2))
4049 (< (car state-2) 0)))
4051 ;; We've stopped short of the starting position
4052 ;; so the hit was inside a nested list. Go up
4053 ;; until we are at the right level.
4054 (condition-case nil
4055 (progn
4056 (goto-char (scan-lists pos -1
4057 (- (car state-2))))
4058 (setq paren-level-pos (point))
4059 (if (and limit (>= limit paren-level-pos))
4060 (progn
4061 (goto-char limit)
4062 nil)
4064 (error
4065 (goto-char (or limit (point-min)))
4066 nil))
4068 ;; The hit was outside the list at the start
4069 ;; position. Go to the start of the list and exit.
4070 (goto-char (1+ (elt state-2 1)))
4071 nil))
4073 ((c-beginning-of-macro limit)
4074 ;; Inside a macro.
4075 (if (< (point)
4076 (or start-macro-beg
4077 (setq start-macro-beg
4078 (save-excursion
4079 (goto-char start)
4080 (c-beginning-of-macro limit)
4081 (point)))))
4084 ;; It's inside the same macro we started in so it's
4085 ;; a relevant match.
4086 (goto-char pos)
4087 nil))))))
4089 (> (point)
4090 (progn
4091 ;; Skip syntactic ws afterwards so that we don't stop at the
4092 ;; end of a comment if `skip-chars' is something like "^/".
4093 (c-backward-syntactic-ws)
4094 (point)))))
4096 ;; We might want to extend this with more useful return values in
4097 ;; the future.
4098 (/= (point) start)))
4100 ;; The following is an alternative implementation of
4101 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4102 ;; track of the syntactic context. It turned out to be generally
4103 ;; slower than the one above which uses forward checks from earlier
4104 ;; safe positions.
4106 ;;(defconst c-ssb-stop-re
4107 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4108 ;; ;; stop at to avoid going into comments and literals.
4109 ;; (concat
4110 ;; ;; Match comment end syntax and string literal syntax. Also match
4111 ;; ;; '/' for block comment endings (not covered by comment end
4112 ;; ;; syntax).
4113 ;; "\\s>\\|/\\|\\s\""
4114 ;; (if (memq 'gen-string-delim c-emacs-features)
4115 ;; "\\|\\s|"
4116 ;; "")
4117 ;; (if (memq 'gen-comment-delim c-emacs-features)
4118 ;; "\\|\\s!"
4119 ;; "")))
4121 ;;(defconst c-ssb-stop-paren-re
4122 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4123 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4125 ;;(defconst c-ssb-sexp-end-re
4126 ;; ;; Regexp matching the ending syntax of a complex sexp.
4127 ;; (concat c-string-limit-regexp "\\|\\s)"))
4129 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4130 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4131 ;;i.e. don't stop at positions inside syntactic whitespace or string
4132 ;;literals. Preprocessor directives are also ignored. However, if the
4133 ;;point is within a comment, string literal or preprocessor directory to
4134 ;;begin with, its contents is treated as syntactically relevant chars.
4135 ;;If LIMIT is given, it limits the backward search and the point will be
4136 ;;left there if no earlier position is found.
4138 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4139 ;;sexps, and the search will also not go outside the current paren sexp.
4140 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4141 ;;then the point will be left at the limit.
4143 ;;Non-nil is returned if the point moved, nil otherwise.
4145 ;;Note that this function might do hidden buffer changes. See the
4146 ;;comment at the start of cc-engine.el for more info."
4148 ;; (save-restriction
4149 ;; (when limit
4150 ;; (narrow-to-region limit (point-max)))
4152 ;; (let ((start (point)))
4153 ;; (catch 'done
4154 ;; (while (let ((last-pos (point))
4155 ;; (stop-pos (progn
4156 ;; (skip-chars-backward skip-chars)
4157 ;; (point))))
4159 ;; ;; Skip back over the same region as
4160 ;; ;; `skip-chars-backward' above, but keep to
4161 ;; ;; syntactically relevant positions.
4162 ;; (goto-char last-pos)
4163 ;; (while (and
4164 ;; ;; `re-search-backward' with a single char regexp
4165 ;; ;; should be fast.
4166 ;; (re-search-backward
4167 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4168 ;; stop-pos 'move)
4170 ;; (progn
4171 ;; (cond
4172 ;; ((looking-at "\\s(")
4173 ;; ;; `paren-level' is set and we've found the
4174 ;; ;; start of the containing paren.
4175 ;; (forward-char)
4176 ;; (throw 'done t))
4178 ;; ((looking-at c-ssb-sexp-end-re)
4179 ;; ;; We're at the end of a string literal or paren
4180 ;; ;; sexp (if `paren-level' is set).
4181 ;; (forward-char)
4182 ;; (condition-case nil
4183 ;; (c-backward-sexp)
4184 ;; (error
4185 ;; (goto-char limit)
4186 ;; (throw 'done t))))
4188 ;; (t
4189 ;; (forward-char)
4190 ;; ;; At the end of some syntactic ws or possibly
4191 ;; ;; after a plain '/' operator.
4192 ;; (let ((pos (point)))
4193 ;; (c-backward-syntactic-ws)
4194 ;; (if (= pos (point))
4195 ;; ;; Was a plain '/' operator. Go past it.
4196 ;; (backward-char)))))
4198 ;; (> (point) stop-pos))))
4200 ;; ;; Now the point is either at `stop-pos' or at some
4201 ;; ;; position further back if `stop-pos' was at a
4202 ;; ;; syntactically irrelevant place.
4204 ;; ;; Skip additional syntactic ws so that we don't stop
4205 ;; ;; at the end of a comment if `skip-chars' is
4206 ;; ;; something like "^/".
4207 ;; (c-backward-syntactic-ws)
4209 ;; (< (point) stop-pos))))
4211 ;; ;; We might want to extend this with more useful return values
4212 ;; ;; in the future.
4213 ;; (/= (point) start))))
4216 ;; Tools for handling comments and string literals.
4218 (defun c-in-literal (&optional lim detect-cpp)
4219 "Return the type of literal point is in, if any.
4220 The return value is `c' if in a C-style comment, `c++' if in a C++
4221 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4222 is non-nil and in a preprocessor line, or nil if somewhere else.
4223 Optional LIM is used as the backward limit of the search. If omitted,
4224 or nil, `c-beginning-of-defun' is used.
4226 The last point calculated is cached if the cache is enabled, i.e. if
4227 `c-in-literal-cache' is bound to a two element vector.
4229 Note that this function might do hidden buffer changes. See the
4230 comment at the start of cc-engine.el for more info."
4231 (save-restriction
4232 (widen)
4233 (let* ((safe-place (c-state-safe-place (point)))
4234 (lit (c-state-pp-to-literal safe-place (point))))
4235 (or (cadr lit)
4236 (and detect-cpp
4237 (save-excursion (c-beginning-of-macro))
4238 'pound)))))
4240 (defun c-literal-limits (&optional lim near not-in-delimiter)
4241 "Return a cons of the beginning and end positions of the comment or
4242 string surrounding point (including both delimiters), or nil if point
4243 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4244 to start parsing from. If NEAR is non-nil, then the limits of any
4245 literal next to point is returned. \"Next to\" means there's only
4246 spaces and tabs between point and the literal. The search for such a
4247 literal is done first in forward direction. If NOT-IN-DELIMITER is
4248 non-nil, the case when point is inside a starting delimiter won't be
4249 recognized. This only has effect for comments which have starting
4250 delimiters with more than one character.
4252 Note that this function might do hidden buffer changes. See the
4253 comment at the start of cc-engine.el for more info."
4255 (save-excursion
4256 (let* ((pos (point))
4257 (lim (or lim (c-state-safe-place pos)))
4258 (pp-to-lit (save-restriction
4259 (widen)
4260 (c-state-pp-to-literal lim pos)))
4261 (state (car pp-to-lit))
4262 (lit-limits (car (cddr pp-to-lit))))
4264 (cond
4265 (lit-limits)
4266 ((and (not not-in-delimiter)
4267 (not (elt state 5))
4268 (eq (char-before) ?/)
4269 (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28.
4270 ;; We're standing in a comment starter.
4271 (backward-char 1)
4272 (cons (point) (progn (c-forward-single-comment) (point))))
4274 (near
4275 (goto-char pos)
4276 ;; Search forward for a literal.
4277 (skip-chars-forward " \t")
4278 (cond
4279 ((looking-at c-string-limit-regexp) ; String.
4280 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4281 (point-max))))
4283 ((looking-at c-comment-start-regexp) ; Line or block comment.
4284 (cons (point) (progn (c-forward-single-comment) (point))))
4287 ;; Search backward.
4288 (skip-chars-backward " \t")
4290 (let ((end (point)) beg)
4291 (cond
4292 ((save-excursion
4293 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4294 (setq beg (c-safe (c-backward-sexp 1) (point))))
4296 ((and (c-safe (forward-char -2) t)
4297 (looking-at "*/"))
4298 ;; Block comment. Due to the nature of line
4299 ;; comments, they will always be covered by the
4300 ;; normal case above.
4301 (goto-char end)
4302 (c-backward-single-comment)
4303 ;; If LIM is bogus, beg will be bogus.
4304 (setq beg (point))))
4306 (if beg (cons beg end))))))
4307 ))))
4309 ;; In case external callers use this; it did have a docstring.
4310 (defalias 'c-literal-limits-fast 'c-literal-limits)
4312 (defun c-collect-line-comments (range)
4313 "If the argument is a cons of two buffer positions (such as returned by
4314 `c-literal-limits'), and that range contains a C++ style line comment,
4315 then an extended range is returned that contains all adjacent line
4316 comments (i.e. all comments that starts in the same column with no
4317 empty lines or non-whitespace characters between them). Otherwise the
4318 argument is returned.
4320 Note that this function might do hidden buffer changes. See the
4321 comment at the start of cc-engine.el for more info."
4323 (save-excursion
4324 (condition-case nil
4325 (if (and (consp range) (progn
4326 (goto-char (car range))
4327 (looking-at c-line-comment-starter)))
4328 (let ((col (current-column))
4329 (beg (point))
4330 (bopl (c-point 'bopl))
4331 (end (cdr range)))
4332 ;; Got to take care in the backward direction to handle
4333 ;; comments which are preceded by code.
4334 (while (and (c-backward-single-comment)
4335 (>= (point) bopl)
4336 (looking-at c-line-comment-starter)
4337 (= col (current-column)))
4338 (setq beg (point)
4339 bopl (c-point 'bopl)))
4340 (goto-char end)
4341 (while (and (progn (skip-chars-forward " \t")
4342 (looking-at c-line-comment-starter))
4343 (= col (current-column))
4344 (prog1 (zerop (forward-line 1))
4345 (setq end (point)))))
4346 (cons beg end))
4347 range)
4348 (error range))))
4350 (defun c-literal-type (range)
4351 "Convenience function that given the result of `c-literal-limits',
4352 returns nil or the type of literal that the range surrounds, one
4353 of the symbols 'c, 'c++ or 'string. It's much faster than using
4354 `c-in-literal' and is intended to be used when you need both the
4355 type of a literal and its limits.
4357 Note that this function might do hidden buffer changes. See the
4358 comment at the start of cc-engine.el for more info."
4360 (if (consp range)
4361 (save-excursion
4362 (goto-char (car range))
4363 (cond ((looking-at c-string-limit-regexp) 'string)
4364 ((or (looking-at "//") ; c++ line comment
4365 (and (looking-at "\\s<") ; comment starter
4366 (looking-at "#"))) ; awk comment.
4367 'c++)
4368 (t 'c))) ; Assuming the range is valid.
4369 range))
4371 (defsubst c-determine-limit-get-base (start try-size)
4372 ;; Get a "safe place" approximately TRY-SIZE characters before START.
4373 ;; This doesn't preserve point.
4374 (let* ((pos (max (- start try-size) (point-min)))
4375 (base (c-state-safe-place pos))
4376 (s (parse-partial-sexp base pos)))
4377 (if (or (nth 4 s) (nth 3 s)) ; comment or string
4378 (nth 8 s)
4379 (point))))
4381 (defun c-determine-limit (how-far-back &optional start try-size)
4382 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
4383 ;; (default point). This is done by going back further in the buffer then
4384 ;; searching forward for literals. The position found won't be in a
4385 ;; literal. We start searching for the sought position TRY-SIZE (default
4386 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
4387 ;; :-)
4388 (save-excursion
4389 (let* ((start (or start (point)))
4390 (try-size (or try-size (* 2 how-far-back)))
4391 (base (c-determine-limit-get-base start try-size))
4392 (pos base)
4394 (s (parse-partial-sexp pos pos)) ; null state.
4395 stack elt size
4396 (count 0))
4397 (while (< pos start)
4398 ;; Move forward one literal each time round this loop.
4399 ;; Move forward to the start of a comment or string.
4400 (setq s (parse-partial-sexp
4402 start
4403 nil ; target-depth
4404 nil ; stop-before
4405 s ; state
4406 'syntax-table)) ; stop-comment
4408 ;; Gather details of the non-literal-bit - starting pos and size.
4409 (setq size (- (if (or (nth 4 s) (nth 3 s))
4410 (nth 8 s)
4411 (point))
4412 pos))
4413 (if (> size 0)
4414 (setq stack (cons (cons pos size) stack)))
4416 ;; Move forward to the end of the comment/string.
4417 (if (or (nth 4 s) (nth 3 s))
4418 (setq s (parse-partial-sexp
4419 (point)
4420 start
4421 nil ; target-depth
4422 nil ; stop-before
4423 s ; state
4424 'syntax-table))) ; stop-comment
4425 (setq pos (point)))
4427 ;; Now try and find enough non-literal characters recorded on the stack.
4428 ;; Go back one recorded literal each time round this loop.
4429 (while (and (< count how-far-back)
4430 stack)
4431 (setq elt (car stack)
4432 stack (cdr stack))
4433 (setq count (+ count (cdr elt))))
4435 ;; Have we found enough yet?
4436 (cond
4437 ((>= count how-far-back)
4438 (+ (car elt) (- count how-far-back)))
4439 ((eq base (point-min))
4440 (point-min))
4442 (c-determine-limit (- how-far-back count) base try-size))))))
4444 ;; `c-find-decl-spots' and accompanying stuff.
4446 ;; Variables used in `c-find-decl-spots' to cache the search done for
4447 ;; the first declaration in the last call. When that function starts,
4448 ;; it needs to back up over syntactic whitespace to look at the last
4449 ;; token before the region being searched. That can sometimes cause
4450 ;; moves back and forth over a quite large region of comments and
4451 ;; macros, which would be repeated for each changed character when
4452 ;; we're called during fontification, since font-lock refontifies the
4453 ;; current line for each change. Thus it's worthwhile to cache the
4454 ;; first match.
4456 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4457 ;; the syntactic whitespace less or equal to some start position.
4458 ;; There's no cached value if it's nil.
4460 ;; `c-find-decl-match-pos' is the match position if
4461 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4462 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4463 (defvar c-find-decl-syntactic-pos nil)
4464 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4465 (defvar c-find-decl-match-pos nil)
4466 (make-variable-buffer-local 'c-find-decl-match-pos)
4468 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4469 (and c-find-decl-syntactic-pos
4470 (< change-min-pos c-find-decl-syntactic-pos)
4471 (setq c-find-decl-syntactic-pos nil)))
4473 ; (defface c-debug-decl-spot-face
4474 ; '((t (:background "Turquoise")))
4475 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4476 ; (defface c-debug-decl-sws-face
4477 ; '((t (:background "Khaki")))
4478 ; "Debug face to mark the syntactic whitespace between the declaration
4479 ; spots and the preceding token end.")
4481 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4482 (when (facep 'c-debug-decl-spot-face)
4483 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4484 (c-debug-add-face (max match-pos (point-min)) decl-pos
4485 'c-debug-decl-sws-face)
4486 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4487 'c-debug-decl-spot-face))))
4488 (defmacro c-debug-remove-decl-spot-faces (beg end)
4489 (when (facep 'c-debug-decl-spot-face)
4490 `(c-save-buffer-state ()
4491 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4492 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4494 (defmacro c-find-decl-prefix-search ()
4495 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4496 ;; but it contains lots of free variables that refer to things
4497 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4498 ;; if there is a match, otherwise at `cfd-limit'.
4500 ;; This macro might do hidden buffer changes.
4502 '(progn
4503 ;; Find the next property match position if we haven't got one already.
4504 (unless cfd-prop-match
4505 (save-excursion
4506 (while (progn
4507 (goto-char (next-single-property-change
4508 (point) 'c-type nil cfd-limit))
4509 (and (< (point) cfd-limit)
4510 (not (eq (c-get-char-property (1- (point)) 'c-type)
4511 'c-decl-end)))))
4512 (setq cfd-prop-match (point))))
4514 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4515 ;; got one already.
4516 (unless cfd-re-match
4518 (if (> cfd-re-match-end (point))
4519 (goto-char cfd-re-match-end))
4521 (while (if (setq cfd-re-match-end
4522 (re-search-forward c-decl-prefix-or-start-re
4523 cfd-limit 'move))
4525 ;; Match. Check if it's inside a comment or string literal.
4526 (c-got-face-at
4527 (if (setq cfd-re-match (match-end 1))
4528 ;; Matched the end of a token preceding a decl spot.
4529 (progn
4530 (goto-char cfd-re-match)
4531 (1- cfd-re-match))
4532 ;; Matched a token that start a decl spot.
4533 (goto-char (match-beginning 0))
4534 (point))
4535 c-literal-faces)
4537 ;; No match. Finish up and exit the loop.
4538 (setq cfd-re-match cfd-limit)
4539 nil)
4541 ;; Skip out of comments and string literals.
4542 (while (progn
4543 (goto-char (next-single-property-change
4544 (point) 'face nil cfd-limit))
4545 (and (< (point) cfd-limit)
4546 (c-got-face-at (point) c-literal-faces)))))
4548 ;; If we matched at the decl start, we have to back up over the
4549 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4550 ;; any decl spots in the syntactic ws.
4551 (unless cfd-re-match
4552 (c-backward-syntactic-ws)
4553 (setq cfd-re-match (point))))
4555 ;; Choose whichever match is closer to the start.
4556 (if (< cfd-re-match cfd-prop-match)
4557 (setq cfd-match-pos cfd-re-match
4558 cfd-re-match nil)
4559 (setq cfd-match-pos cfd-prop-match
4560 cfd-prop-match nil))
4562 (goto-char cfd-match-pos)
4564 (when (< cfd-match-pos cfd-limit)
4565 ;; Skip forward past comments only so we don't skip macros.
4566 (c-forward-comments)
4567 ;; Set the position to continue at. We can avoid going over
4568 ;; the comments skipped above a second time, but it's possible
4569 ;; that the comment skipping has taken us past `cfd-prop-match'
4570 ;; since the property might be used inside comments.
4571 (setq cfd-continue-pos (if cfd-prop-match
4572 (min cfd-prop-match (point))
4573 (point))))))
4575 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4576 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4577 ;; label from the point to CFD-LIMIT.
4579 ;; CFD-FUN is called with point at the start of the spot. It's passed two
4580 ;; arguments: The first is the end position of the token preceding the spot,
4581 ;; or 0 for the implicit match at bob. The second is a flag that is t when
4582 ;; the match is inside a macro. Point should be moved forward by at least
4583 ;; one token.
4585 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
4586 ;; it should return non-nil to ensure that the next search will find them.
4588 ;; Such a spot is:
4589 ;; o The first token after bob.
4590 ;; o The first token after the end of submatch 1 in
4591 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4592 ;; o The start of each `c-decl-prefix-or-start-re' match when
4593 ;; submatch 1 doesn't match.
4594 ;; o The first token after the end of each occurrence of the
4595 ;; `c-type' text property with the value `c-decl-end', provided
4596 ;; `c-type-decl-end-used' is set.
4598 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4599 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4600 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4602 ;; If the match is inside a macro then the buffer is narrowed to the
4603 ;; end of it, so that CFD-FUN can investigate the following tokens
4604 ;; without matching something that begins inside a macro and ends
4605 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4606 ;; CFD-FACE-CHECKLIST checks exist.
4608 ;; The spots are visited approximately in order from top to bottom.
4609 ;; It's however the positions where `c-decl-prefix-or-start-re'
4610 ;; matches and where `c-decl-end' properties are found that are in
4611 ;; order. Since the spots often are at the following token, they
4612 ;; might be visited out of order insofar as more spots are reported
4613 ;; later on within the syntactic whitespace between the match
4614 ;; positions and their spots.
4616 ;; It's assumed that comments and strings are fontified in the
4617 ;; searched range.
4619 ;; This is mainly used in fontification, and so has an elaborate
4620 ;; cache to handle repeated calls from the same start position; see
4621 ;; the variables above.
4623 ;; All variables in this function begin with `cfd-' to avoid name
4624 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4626 ;; This function might do hidden buffer changes.
4628 (let ((cfd-start-pos (point))
4629 (cfd-buffer-end (point-max))
4630 ;; The end of the token preceding the decl spot last found
4631 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4632 ;; no match.
4633 cfd-re-match
4634 ;; The end position of the last `c-decl-prefix-or-start-re'
4635 ;; match. If this is greater than `cfd-continue-pos', the
4636 ;; next regexp search is started here instead.
4637 (cfd-re-match-end (point-min))
4638 ;; The end of the last `c-decl-end' found by
4639 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4640 ;; match. If searching for the property isn't needed then we
4641 ;; disable it by setting it to `cfd-limit' directly.
4642 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4643 ;; The end of the token preceding the decl spot last found by
4644 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4645 ;; bob. `cfd-limit' if there's no match. In other words,
4646 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4647 (cfd-match-pos cfd-limit)
4648 ;; The position to continue searching at.
4649 cfd-continue-pos
4650 ;; The position of the last "real" token we've stopped at.
4651 ;; This can be greater than `cfd-continue-pos' when we get
4652 ;; hits inside macros or at `c-decl-end' positions inside
4653 ;; comments.
4654 (cfd-token-pos 0)
4655 ;; The end position of the last entered macro.
4656 (cfd-macro-end 0))
4658 ;; Initialize by finding a syntactically relevant start position
4659 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4660 ;; search unless we're at bob.
4662 (let (start-in-literal start-in-macro syntactic-pos)
4663 ;; Must back up a bit since we look for the end of the previous
4664 ;; statement or declaration, which is earlier than the first
4665 ;; returned match.
4667 (cond
4668 ;; First we need to move to a syntactically relevant position.
4669 ;; Begin by backing out of comment or string literals.
4670 ((and
4671 (when (c-got-face-at (point) c-literal-faces)
4672 ;; Try to use the faces to back up to the start of the
4673 ;; literal. FIXME: What if the point is on a declaration
4674 ;; inside a comment?
4675 (while (and (not (bobp))
4676 (c-got-face-at (1- (point)) c-literal-faces))
4677 (goto-char (previous-single-property-change
4678 (point) 'face nil (point-min))))
4680 ;; XEmacs doesn't fontify the quotes surrounding string
4681 ;; literals.
4682 (and (featurep 'xemacs)
4683 (eq (get-text-property (point) 'face)
4684 'font-lock-string-face)
4685 (not (bobp))
4686 (progn (backward-char)
4687 (not (looking-at c-string-limit-regexp)))
4688 (forward-char))
4690 ;; Don't trust the literal to contain only literal faces
4691 ;; (the font lock package might not have fontified the
4692 ;; start of it at all, for instance) so check that we have
4693 ;; arrived at something that looks like a start or else
4694 ;; resort to `c-literal-limits'.
4695 (unless (looking-at c-literal-start-regexp)
4696 (let ((range (c-literal-limits)))
4697 (if range (goto-char (car range)))))
4699 (setq start-in-literal (point)))
4701 ;; The start is in a literal. If the limit is in the same
4702 ;; one we don't have to find a syntactic position etc. We
4703 ;; only check that if the limit is at or before bonl to save
4704 ;; time; it covers the by far most common case when font-lock
4705 ;; refontifies the current line only.
4706 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4707 (save-excursion
4708 (goto-char cfd-start-pos)
4709 (while (progn
4710 (goto-char (next-single-property-change
4711 (point) 'face nil cfd-limit))
4712 (and (< (point) cfd-limit)
4713 (c-got-face-at (point) c-literal-faces))))
4714 (= (point) cfd-limit)))
4716 ;; Completely inside a literal. Set up variables to trig the
4717 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4718 ;; find a suitable start position.
4719 (setq cfd-continue-pos start-in-literal))
4721 ;; Check if the region might be completely inside a macro, to
4722 ;; optimize that like the completely-inside-literal above.
4723 ((save-excursion
4724 (and (= (forward-line 1) 0)
4725 (bolp) ; forward-line has funny behavior at eob.
4726 (>= (point) cfd-limit)
4727 (progn (backward-char)
4728 (eq (char-before) ?\\))))
4729 ;; (Maybe) completely inside a macro. Only need to trig the
4730 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4731 ;; set things up.
4732 (setq cfd-continue-pos (1- cfd-start-pos)
4733 start-in-macro t))
4736 ;; Back out of any macro so we don't miss any declaration
4737 ;; that could follow after it.
4738 (when (c-beginning-of-macro)
4739 (setq start-in-macro t))
4741 ;; Now we're at a proper syntactically relevant position so we
4742 ;; can use the cache. But first clear it if it applied
4743 ;; further down.
4744 (c-invalidate-find-decl-cache cfd-start-pos)
4746 (setq syntactic-pos (point))
4747 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
4748 ;; Don't have to do this if the cache is relevant here,
4749 ;; typically if the same line is refontified again. If
4750 ;; we're just some syntactic whitespace further down we can
4751 ;; still use the cache to limit the skipping.
4752 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
4754 ;; If we hit `c-find-decl-syntactic-pos' and
4755 ;; `c-find-decl-match-pos' is set then we install the cached
4756 ;; values. If we hit `c-find-decl-syntactic-pos' and
4757 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4758 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4759 ;; and so we can continue the search from this point. If we
4760 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4761 ;; the right spot to begin searching anyway.
4762 (if (and (eq (point) c-find-decl-syntactic-pos)
4763 c-find-decl-match-pos)
4764 (setq cfd-match-pos c-find-decl-match-pos
4765 cfd-continue-pos syntactic-pos)
4767 (setq c-find-decl-syntactic-pos syntactic-pos)
4769 (when (if (bobp)
4770 ;; Always consider bob a match to get the first
4771 ;; declaration in the file. Do this separately instead of
4772 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4773 ;; regexp always can consume at least one character to
4774 ;; ensure that we won't get stuck in an infinite loop.
4775 (setq cfd-re-match 0)
4776 (backward-char)
4777 (c-beginning-of-current-token)
4778 (< (point) cfd-limit))
4779 ;; Do an initial search now. In the bob case above it's
4780 ;; only done to search for a `c-decl-end' spot.
4781 (c-find-decl-prefix-search))
4783 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
4784 cfd-match-pos)))))
4786 ;; Advance `cfd-continue-pos' if it's before the start position.
4787 ;; The closest continue position that might have effect at or
4788 ;; after the start depends on what we started in. This also
4789 ;; finds a suitable start position in the special cases when the
4790 ;; region is completely within a literal or macro.
4791 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
4793 (cond
4794 (start-in-macro
4795 ;; If we're in a macro then it's the closest preceding token
4796 ;; in the macro. Check this before `start-in-literal',
4797 ;; since if we're inside a literal in a macro, the preceding
4798 ;; token is earlier than any `c-decl-end' spot inside the
4799 ;; literal (comment).
4800 (goto-char (or start-in-literal cfd-start-pos))
4801 ;; The only syntactic ws in macros are comments.
4802 (c-backward-comments)
4803 (backward-char)
4804 (c-beginning-of-current-token))
4806 (start-in-literal
4807 ;; If we're in a comment it can only be the closest
4808 ;; preceding `c-decl-end' position within that comment, if
4809 ;; any. Go back to the beginning of such a property so that
4810 ;; `c-find-decl-prefix-search' will find the end of it.
4811 ;; (Can't stop at the end and install it directly on
4812 ;; `cfd-prop-match' since that variable might be cleared
4813 ;; after `cfd-fun' below.)
4815 ;; Note that if the literal is a string then the property
4816 ;; search will simply skip to the beginning of it right
4817 ;; away.
4818 (if (not c-type-decl-end-used)
4819 (goto-char start-in-literal)
4820 (goto-char cfd-start-pos)
4821 (while (progn
4822 (goto-char (previous-single-property-change
4823 (point) 'c-type nil start-in-literal))
4824 (and (> (point) start-in-literal)
4825 (not (eq (c-get-char-property (point) 'c-type)
4826 'c-decl-end))))))
4828 (when (= (point) start-in-literal)
4829 ;; Didn't find any property inside the comment, so we can
4830 ;; skip it entirely. (This won't skip past a string, but
4831 ;; that'll be handled quickly by the next
4832 ;; `c-find-decl-prefix-search' anyway.)
4833 (c-forward-single-comment)
4834 (if (> (point) cfd-limit)
4835 (goto-char cfd-limit))))
4838 ;; If we started in normal code, the only match that might
4839 ;; apply before the start is what we already got in
4840 ;; `cfd-match-pos' so we can continue at the start position.
4841 ;; (Note that we don't get here if the first match is below
4842 ;; it.)
4843 (goto-char cfd-start-pos)))
4845 ;; Delete found matches if they are before our new continue
4846 ;; position, so that `c-find-decl-prefix-search' won't back up
4847 ;; to them later on.
4848 (setq cfd-continue-pos (point))
4849 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
4850 (setq cfd-re-match nil))
4851 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
4852 (setq cfd-prop-match nil)))
4854 (if syntactic-pos
4855 ;; This is the normal case and we got a proper syntactic
4856 ;; position. If there's a match then it's always outside
4857 ;; macros and comments, so advance to the next token and set
4858 ;; `cfd-token-pos'. The loop below will later go back using
4859 ;; `cfd-continue-pos' to fix declarations inside the
4860 ;; syntactic ws.
4861 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
4862 (goto-char syntactic-pos)
4863 (c-forward-syntactic-ws)
4864 (and cfd-continue-pos
4865 (< cfd-continue-pos (point))
4866 (setq cfd-token-pos (point))))
4868 ;; Have one of the special cases when the region is completely
4869 ;; within a literal or macro. `cfd-continue-pos' is set to a
4870 ;; good start position for the search, so do it.
4871 (c-find-decl-prefix-search)))
4873 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4875 (while (progn
4876 (while (and
4877 (< cfd-match-pos cfd-limit)
4880 ;; Kludge to filter out matches on the "<" that
4881 ;; aren't open parens, for the sake of languages
4882 ;; that got `c-recognize-<>-arglists' set.
4883 (and (eq (char-before cfd-match-pos) ?<)
4884 (not (c-get-char-property (1- cfd-match-pos)
4885 'syntax-table)))
4887 ;; If `cfd-continue-pos' is less or equal to
4888 ;; `cfd-token-pos', we've got a hit inside a macro
4889 ;; that's in the syntactic whitespace before the last
4890 ;; "real" declaration we've checked. If they're equal
4891 ;; we've arrived at the declaration a second time, so
4892 ;; there's nothing to do.
4893 (= cfd-continue-pos cfd-token-pos)
4895 (progn
4896 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4897 ;; we're still searching for declarations embedded in
4898 ;; the syntactic whitespace. In that case we need
4899 ;; only to skip comments and not macros, since they
4900 ;; can't be nested, and that's already been done in
4901 ;; `c-find-decl-prefix-search'.
4902 (when (> cfd-continue-pos cfd-token-pos)
4903 (c-forward-syntactic-ws)
4904 (setq cfd-token-pos (point)))
4906 ;; Continue if the following token fails the
4907 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4908 (when (or (>= (point) cfd-limit)
4909 (not (looking-at cfd-decl-re))
4910 (and cfd-face-checklist
4911 (not (c-got-face-at
4912 (point) cfd-face-checklist))))
4913 (goto-char cfd-continue-pos)
4914 t)))
4916 (< (point) cfd-limit))
4917 (c-find-decl-prefix-search))
4919 (< (point) cfd-limit))
4921 (when (and
4922 (>= (point) cfd-start-pos)
4924 (progn
4925 ;; Narrow to the end of the macro if we got a hit inside
4926 ;; one, to avoid recognizing things that start inside the
4927 ;; macro and end outside it.
4928 (when (> cfd-match-pos cfd-macro-end)
4929 ;; Not in the same macro as in the previous round.
4930 (save-excursion
4931 (goto-char cfd-match-pos)
4932 (setq cfd-macro-end
4933 (if (save-excursion (and (c-beginning-of-macro)
4934 (< (point) cfd-match-pos)))
4935 (progn (c-end-of-macro)
4936 (point))
4937 0))))
4939 (if (zerop cfd-macro-end)
4941 (if (> cfd-macro-end (point))
4942 (progn (narrow-to-region (point-min) cfd-macro-end)
4944 ;; The matched token was the last thing in the macro,
4945 ;; so the whole match is bogus.
4946 (setq cfd-macro-end 0)
4947 nil))))
4949 (c-debug-put-decl-spot-faces cfd-match-pos (point))
4950 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
4951 (setq cfd-prop-match nil))
4953 (when (/= cfd-macro-end 0)
4954 ;; Restore limits if we did macro narrowing above.
4955 (narrow-to-region (point-min) cfd-buffer-end)))
4957 (goto-char cfd-continue-pos)
4958 (if (= cfd-continue-pos cfd-limit)
4959 (setq cfd-match-pos cfd-limit)
4960 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
4961 ; cfd-match-pos, etc.
4964 ;; A cache for found types.
4966 ;; Buffer local variable that contains an obarray with the types we've
4967 ;; found. If a declaration is recognized somewhere we record the
4968 ;; fully qualified identifier in it to recognize it as a type
4969 ;; elsewhere in the file too. This is not accurate since we do not
4970 ;; bother with the scoping rules of the languages, but in practice the
4971 ;; same name is seldom used as both a type and something else in a
4972 ;; file, and we only use this as a last resort in ambiguous cases (see
4973 ;; `c-forward-decl-or-cast-1').
4975 ;; Not every type need be in this cache. However, things which have
4976 ;; ceased to be types must be removed from it.
4978 ;; Template types in C++ are added here too but with the template
4979 ;; arglist replaced with "<>" in references or "<" for the one in the
4980 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4981 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4982 ;; template specs can be fairly sized programs in themselves) and
4983 ;; improves the hit ratio (it's a type regardless of the template
4984 ;; args; it's just not the same type, but we're only interested in
4985 ;; recognizing types, not telling distinct types apart). Note that
4986 ;; template types in references are added here too; from the example
4987 ;; above there will also be an entry "Foo<".
4988 (defvar c-found-types nil)
4989 (make-variable-buffer-local 'c-found-types)
4991 (defsubst c-clear-found-types ()
4992 ;; Clears `c-found-types'.
4993 (setq c-found-types (make-vector 53 0)))
4995 (defun c-add-type (from to)
4996 ;; Add the given region as a type in `c-found-types'. If the region
4997 ;; doesn't match an existing type but there is a type which is equal
4998 ;; to the given one except that the last character is missing, then
4999 ;; the shorter type is removed. That's done to avoid adding all
5000 ;; prefixes of a type as it's being entered and font locked. This
5001 ;; doesn't cover cases like when characters are removed from a type
5002 ;; or added in the middle. We'd need the position of point when the
5003 ;; font locking is invoked to solve this well.
5005 ;; This function might do hidden buffer changes.
5006 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
5007 (unless (intern-soft type c-found-types)
5008 (unintern (substring type 0 -1) c-found-types)
5009 (intern type c-found-types))))
5011 (defun c-unfind-type (name)
5012 ;; Remove the "NAME" from c-found-types, if present.
5013 (unintern name c-found-types))
5015 (defsubst c-check-type (from to)
5016 ;; Return non-nil if the given region contains a type in
5017 ;; `c-found-types'.
5019 ;; This function might do hidden buffer changes.
5020 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
5021 c-found-types))
5023 (defun c-list-found-types ()
5024 ;; Return all the types in `c-found-types' as a sorted list of
5025 ;; strings.
5026 (let (type-list)
5027 (mapatoms (lambda (type)
5028 (setq type-list (cons (symbol-name type)
5029 type-list)))
5030 c-found-types)
5031 (sort type-list 'string-lessp)))
5033 ;; Shut up the byte compiler.
5034 (defvar c-maybe-stale-found-type)
5036 (defun c-trim-found-types (beg end old-len)
5037 ;; An after change function which, in conjunction with the info in
5038 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
5039 ;; from `c-found-types', should this type have become stale. For
5040 ;; example, this happens to "foo" when "foo \n bar();" becomes
5041 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
5042 ;; the fontification.
5044 ;; Have we, perhaps, added non-ws characters to the front/back of a found
5045 ;; type?
5046 (when (> end beg)
5047 (save-excursion
5048 (when (< end (point-max))
5049 (goto-char end)
5050 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
5051 (progn (goto-char end)
5052 (c-end-of-current-token)))
5053 (c-unfind-type (buffer-substring-no-properties
5054 end (point)))))
5055 (when (> beg (point-min))
5056 (goto-char beg)
5057 (if (and (c-end-of-current-token) ; only moves when we started in the middle
5058 (progn (goto-char beg)
5059 (c-beginning-of-current-token)))
5060 (c-unfind-type (buffer-substring-no-properties
5061 (point) beg))))))
5063 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
5064 (cond
5065 ;; Changing the amount of (already existing) whitespace - don't do anything.
5066 ((and (c-partial-ws-p beg end)
5067 (or (= beg end) ; removal of WS
5068 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
5070 ;; The syntactic relationship which defined a "found type" has been
5071 ;; destroyed.
5072 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
5073 (c-unfind-type (cadr c-maybe-stale-found-type)))
5074 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5078 ;; Setting and removing syntax properties on < and > in languages (C++
5079 ;; and Java) where they can be template/generic delimiters as well as
5080 ;; their normal meaning of "less/greater than".
5082 ;; Normally, < and > have syntax 'punctuation'. When they are found to
5083 ;; be delimiters, they are marked as such with the category properties
5084 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5086 ;; STRATEGY:
5088 ;; It is impossible to determine with certainty whether a <..> pair in
5089 ;; C++ is two comparison operators or is template delimiters, unless
5090 ;; one duplicates a lot of a C++ compiler. For example, the following
5091 ;; code fragment:
5093 ;; foo (a < b, c > d) ;
5095 ;; could be a function call with two integer parameters (each a
5096 ;; relational expression), or it could be a constructor for class foo
5097 ;; taking one parameter d of templated type "a < b, c >". They are
5098 ;; somewhat easier to distinguish in Java.
5100 ;; The strategy now (2010-01) adopted is to mark and unmark < and
5101 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5102 ;; individually when their context so indicated. This gave rise to
5103 ;; intractable problems when one of a matching pair was deleted, or
5104 ;; pulled into a literal.]
5106 ;; At each buffer change, the syntax-table properties are removed in a
5107 ;; before-change function and reapplied, when needed, in an
5108 ;; after-change function. It is far more important that the
5109 ;; properties get removed when they they are spurious than that they
5110 ;; be present when wanted.
5111 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5112 (defun c-clear-<-pair-props (&optional pos)
5113 ;; POS (default point) is at a < character. If it is marked with
5114 ;; open paren syntax-table text property, remove the property,
5115 ;; together with the close paren property on the matching > (if
5116 ;; any).
5117 (save-excursion
5118 (if pos
5119 (goto-char pos)
5120 (setq pos (point)))
5121 (when (equal (c-get-char-property (point) 'syntax-table)
5122 c-<-as-paren-syntax)
5123 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5124 (c-go-list-forward))
5125 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5126 c->-as-paren-syntax) ; should always be true.
5127 (c-clear-char-property (1- (point)) 'category))
5128 (c-clear-char-property pos 'category))))
5130 (defun c-clear->-pair-props (&optional pos)
5131 ;; POS (default point) is at a > character. If it is marked with
5132 ;; close paren syntax-table property, remove the property, together
5133 ;; with the open paren property on the matching < (if any).
5134 (save-excursion
5135 (if pos
5136 (goto-char pos)
5137 (setq pos (point)))
5138 (when (equal (c-get-char-property (point) 'syntax-table)
5139 c->-as-paren-syntax)
5140 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5141 (c-go-up-list-backward))
5142 (when (equal (c-get-char-property (point) 'syntax-table)
5143 c-<-as-paren-syntax) ; should always be true.
5144 (c-clear-char-property (point) 'category))
5145 (c-clear-char-property pos 'category))))
5147 (defun c-clear-<>-pair-props (&optional pos)
5148 ;; POS (default point) is at a < or > character. If it has an
5149 ;; open/close paren syntax-table property, remove this property both
5150 ;; from the current character and its partner (which will also be
5151 ;; thusly marked).
5152 (cond
5153 ((eq (char-after) ?\<)
5154 (c-clear-<-pair-props pos))
5155 ((eq (char-after) ?\>)
5156 (c-clear->-pair-props pos))
5157 (t (c-benign-error
5158 "c-clear-<>-pair-props called from wrong position"))))
5160 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5161 ;; POS (default point) is at a < character. If it is both marked
5162 ;; with open/close paren syntax-table property, and has a matching >
5163 ;; (also marked) which is after LIM, remove the property both from
5164 ;; the current > and its partner. Return t when this happens, nil
5165 ;; when it doesn't.
5166 (save-excursion
5167 (if pos
5168 (goto-char pos)
5169 (setq pos (point)))
5170 (when (equal (c-get-char-property (point) 'syntax-table)
5171 c-<-as-paren-syntax)
5172 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5173 (c-go-list-forward))
5174 (when (and (>= (point) lim)
5175 (equal (c-get-char-property (1- (point)) 'syntax-table)
5176 c->-as-paren-syntax)) ; should always be true.
5177 (c-unmark-<->-as-paren (1- (point)))
5178 (c-unmark-<->-as-paren pos))
5179 t)))
5181 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
5182 ;; POS (default point) is at a > character. If it is both marked
5183 ;; with open/close paren syntax-table property, and has a matching <
5184 ;; (also marked) which is before LIM, remove the property both from
5185 ;; the current < and its partner. Return t when this happens, nil
5186 ;; when it doesn't.
5187 (save-excursion
5188 (if pos
5189 (goto-char pos)
5190 (setq pos (point)))
5191 (when (equal (c-get-char-property (point) 'syntax-table)
5192 c->-as-paren-syntax)
5193 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5194 (c-go-up-list-backward))
5195 (when (and (<= (point) lim)
5196 (equal (c-get-char-property (point) 'syntax-table)
5197 c-<-as-paren-syntax)) ; should always be true.
5198 (c-unmark-<->-as-paren (point))
5199 (c-unmark-<->-as-paren pos))
5200 t)))
5202 ;; Set by c-common-init in cc-mode.el.
5203 (defvar c-new-BEG)
5204 (defvar c-new-END)
5206 (defun c-before-change-check-<>-operators (beg end)
5207 ;; Unmark certain pairs of "< .... >" which are currently marked as
5208 ;; template/generic delimiters. (This marking is via syntax-table
5209 ;; text properties).
5211 ;; These pairs are those which are in the current "statement" (i.e.,
5212 ;; the region between the {, }, or ; before BEG and the one after
5213 ;; END), and which enclose any part of the interval (BEG END).
5215 ;; Note that in C++ (?and Java), template/generic parens cannot
5216 ;; enclose a brace or semicolon, so we use these as bounds on the
5217 ;; region we must work on.
5219 ;; This function is called from before-change-functions (via
5220 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5221 ;; and point is undefined, both at entry and exit.
5223 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5224 ;; 2010-01-29.
5225 (save-excursion
5226 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5227 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5228 new-beg new-end need-new-beg need-new-end)
5229 ;; Locate the barrier before the changed region
5230 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5231 (c-syntactic-skip-backward "^;{}" (max (- beg 2048) (point-min)))
5232 (setq new-beg (point))
5234 ;; Remove the syntax-table properties from each pertinent <...> pair.
5235 ;; Firsly, the ones with the < before beg and > after beg.
5236 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5237 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5238 (setq need-new-beg t)))
5240 ;; Locate the barrier after END.
5241 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5242 (c-syntactic-re-search-forward "[;{}]"
5243 (min (+ end 2048) (point-max)) 'end)
5244 (setq new-end (point))
5246 ;; Remove syntax-table properties from the remaining pertinent <...>
5247 ;; pairs, those with a > after end and < before end.
5248 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5249 (if (c-clear->-pair-props-if-match-before end)
5250 (setq need-new-end t)))
5252 ;; Extend the fontification region, if needed.
5253 (when need-new-beg
5254 (goto-char new-beg)
5255 (c-forward-syntactic-ws)
5256 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5258 (when need-new-end
5259 (and (> new-end c-new-END) (setq c-new-END new-end))))))
5263 (defun c-after-change-check-<>-operators (beg end)
5264 ;; This is called from `after-change-functions' when
5265 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5266 ;; chars with paren syntax become part of another operator like "<<"
5267 ;; or ">=".
5269 ;; This function might do hidden buffer changes.
5271 (save-excursion
5272 (goto-char beg)
5273 (when (or (looking-at "[<>]")
5274 (< (skip-chars-backward "<>") 0))
5276 (goto-char beg)
5277 (c-beginning-of-current-token)
5278 (when (and (< (point) beg)
5279 (looking-at c-<>-multichar-token-regexp)
5280 (< beg (setq beg (match-end 0))))
5281 (while (progn (skip-chars-forward "^<>" beg)
5282 (< (point) beg))
5283 (c-clear-<>-pair-props)
5284 (forward-char))))
5286 (when (< beg end)
5287 (goto-char end)
5288 (when (or (looking-at "[<>]")
5289 (< (skip-chars-backward "<>") 0))
5291 (goto-char end)
5292 (c-beginning-of-current-token)
5293 (when (and (< (point) end)
5294 (looking-at c-<>-multichar-token-regexp)
5295 (< end (setq end (match-end 0))))
5296 (while (progn (skip-chars-forward "^<>" end)
5297 (< (point) end))
5298 (c-clear-<>-pair-props)
5299 (forward-char)))))))
5303 ;; Handling of small scale constructs like types and names.
5305 ;; Dynamically bound variable that instructs `c-forward-type' to also
5306 ;; treat possible types (i.e. those that it normally returns 'maybe or
5307 ;; 'found for) as actual types (and always return 'found for them).
5308 ;; This means that it records them in `c-record-type-identifiers' if
5309 ;; that is set, and that it adds them to `c-found-types'.
5310 (defvar c-promote-possible-types nil)
5312 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5313 ;; mark up successfully parsed arglists with paren syntax properties on
5314 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5315 ;; `c-type' property of each argument separating comma.
5317 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5318 ;; all arglists for side effects (i.e. recording types), otherwise it
5319 ;; exploits any existing paren syntax properties to quickly jump to the
5320 ;; end of already parsed arglists.
5322 ;; Marking up the arglists is not the default since doing that correctly
5323 ;; depends on a proper value for `c-restricted-<>-arglists'.
5324 (defvar c-parse-and-markup-<>-arglists nil)
5326 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5327 ;; not accept arglists that contain binary operators.
5329 ;; This is primarily used to handle C++ template arglists. C++
5330 ;; disambiguates them by checking whether the preceding name is a
5331 ;; template or not. We can't do that, so we assume it is a template
5332 ;; if it can be parsed as one. That usually works well since
5333 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5334 ;; in almost all cases would be pointless.
5336 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5337 ;; should let the comma separate the function arguments instead. And
5338 ;; in a context where the value of the expression is taken, e.g. in
5339 ;; "if (a < b || c > d)", it's probably not a template.
5340 (defvar c-restricted-<>-arglists nil)
5342 ;; Dynamically bound variables that instructs
5343 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5344 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5345 ;; `c-forward-label' to record the ranges of all the type and
5346 ;; reference identifiers they encounter. They will build lists on
5347 ;; these variables where each element is a cons of the buffer
5348 ;; positions surrounding each identifier. This recording is only
5349 ;; activated when `c-record-type-identifiers' is non-nil.
5351 ;; All known types that can't be identifiers are recorded, and also
5352 ;; other possible types if `c-promote-possible-types' is set.
5353 ;; Recording is however disabled inside angle bracket arglists that
5354 ;; are encountered inside names and other angle bracket arglists.
5355 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5356 ;; instead.
5358 ;; Only the names in C++ template style references (e.g. "tmpl" in
5359 ;; "tmpl<a,b>::foo") are recorded as references, other references
5360 ;; aren't handled here.
5362 ;; `c-forward-label' records the label identifier(s) on
5363 ;; `c-record-ref-identifiers'.
5364 (defvar c-record-type-identifiers nil)
5365 (defvar c-record-ref-identifiers nil)
5367 ;; This variable will receive a cons cell of the range of the last
5368 ;; single identifier symbol stepped over by `c-forward-name' if it's
5369 ;; successful. This is the range that should be put on one of the
5370 ;; record lists above by the caller. It's assigned nil if there's no
5371 ;; such symbol in the name.
5372 (defvar c-last-identifier-range nil)
5374 (defmacro c-record-type-id (range)
5375 (if (eq (car-safe range) 'cons)
5376 ;; Always true.
5377 `(setq c-record-type-identifiers
5378 (cons ,range c-record-type-identifiers))
5379 `(let ((range ,range))
5380 (if range
5381 (setq c-record-type-identifiers
5382 (cons range c-record-type-identifiers))))))
5384 (defmacro c-record-ref-id (range)
5385 (if (eq (car-safe range) 'cons)
5386 ;; Always true.
5387 `(setq c-record-ref-identifiers
5388 (cons ,range c-record-ref-identifiers))
5389 `(let ((range ,range))
5390 (if range
5391 (setq c-record-ref-identifiers
5392 (cons range c-record-ref-identifiers))))))
5394 ;; Dynamically bound variable that instructs `c-forward-type' to
5395 ;; record the ranges of types that only are found. Behaves otherwise
5396 ;; like `c-record-type-identifiers'.
5397 (defvar c-record-found-types nil)
5399 (defmacro c-forward-keyword-prefixed-id (type)
5400 ;; Used internally in `c-forward-keyword-clause' to move forward
5401 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5402 ;; possibly is prefixed by keywords and their associated clauses.
5403 ;; Try with a type/name first to not trip up on those that begin
5404 ;; with a keyword. Return t if a known or found type is moved
5405 ;; over. The point is clobbered if nil is returned. If range
5406 ;; recording is enabled, the identifier is recorded on as a type
5407 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5409 ;; This macro might do hidden buffer changes.
5410 `(let (res)
5411 (while (if (setq res ,(if (eq type 'type)
5412 `(c-forward-type)
5413 `(c-forward-name)))
5415 (and (looking-at c-keywords-regexp)
5416 (c-forward-keyword-clause 1))))
5417 (when (memq res '(t known found prefix))
5418 ,(when (eq type 'ref)
5419 `(when c-record-type-identifiers
5420 (c-record-ref-id c-last-identifier-range)))
5421 t)))
5423 (defmacro c-forward-id-comma-list (type update-safe-pos)
5424 ;; Used internally in `c-forward-keyword-clause' to move forward
5425 ;; over a comma separated list of types or names using
5426 ;; `c-forward-keyword-prefixed-id'.
5428 ;; This macro might do hidden buffer changes.
5429 `(while (and (progn
5430 ,(when update-safe-pos
5431 `(setq safe-pos (point)))
5432 (eq (char-after) ?,))
5433 (progn
5434 (forward-char)
5435 (c-forward-syntactic-ws)
5436 (c-forward-keyword-prefixed-id ,type)))))
5438 (defun c-forward-keyword-clause (match)
5439 ;; Submatch MATCH in the current match data is assumed to surround a
5440 ;; token. If it's a keyword, move over it and any immediately
5441 ;; following clauses associated with it, stopping at the start of
5442 ;; the next token. t is returned in that case, otherwise the point
5443 ;; stays and nil is returned. The kind of clauses that are
5444 ;; recognized are those specified by `c-type-list-kwds',
5445 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5446 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5447 ;; and `c-<>-arglist-kwds'.
5449 ;; This function records identifier ranges on
5450 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5451 ;; `c-record-type-identifiers' is non-nil.
5453 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5454 ;; apply directly after the keyword, the type list is moved over
5455 ;; only when there is no unaccounted token before it (i.e. a token
5456 ;; that isn't moved over due to some other keyword list). The
5457 ;; identifier ranges in the list are still recorded if that should
5458 ;; be done, though.
5460 ;; This function might do hidden buffer changes.
5462 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5463 ;; The call to `c-forward-<>-arglist' below is made after
5464 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5465 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5466 ;; should therefore be nil.
5467 (c-parse-and-markup-<>-arglists t)
5468 c-restricted-<>-arglists)
5470 (when kwd-sym
5471 (goto-char (match-end match))
5472 (c-forward-syntactic-ws)
5473 (setq safe-pos (point))
5475 (cond
5476 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5477 (c-forward-keyword-prefixed-id type))
5478 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5479 (c-forward-id-comma-list type t))
5481 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5482 (c-forward-keyword-prefixed-id ref))
5483 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5484 (c-forward-id-comma-list ref t))
5486 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5487 (eq (char-after) ?\())
5488 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5490 (forward-char)
5491 (when (and (setq pos (c-up-list-forward))
5492 (eq (char-before pos) ?\)))
5493 (when (and c-record-type-identifiers
5494 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5495 ;; Use `c-forward-type' on every identifier we can find
5496 ;; inside the paren, to record the types.
5497 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5498 (goto-char (match-beginning 0))
5499 (unless (c-forward-type)
5500 (looking-at c-symbol-key) ; Always matches.
5501 (goto-char (match-end 0)))))
5503 (goto-char pos)
5504 (c-forward-syntactic-ws)
5505 (setq safe-pos (point))))
5507 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5508 (eq (char-after) ?<)
5509 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5510 (c-forward-syntactic-ws)
5511 (setq safe-pos (point)))
5513 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5514 (not (looking-at c-symbol-start))
5515 (c-safe (c-forward-sexp) t))
5516 (c-forward-syntactic-ws)
5517 (setq safe-pos (point))))
5519 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5520 (if (eq (char-after) ?:)
5521 ;; If we are at the colon already, we move over the type
5522 ;; list after it.
5523 (progn
5524 (forward-char)
5525 (c-forward-syntactic-ws)
5526 (when (c-forward-keyword-prefixed-id type)
5527 (c-forward-id-comma-list type t)))
5528 ;; Not at the colon, so stop here. But the identifier
5529 ;; ranges in the type list later on should still be
5530 ;; recorded.
5531 (and c-record-type-identifiers
5532 (progn
5533 ;; If a keyword matched both one of the types above and
5534 ;; this one, we match `c-colon-type-list-re' after the
5535 ;; clause matched above.
5536 (goto-char safe-pos)
5537 (looking-at c-colon-type-list-re))
5538 (progn
5539 (goto-char (match-end 0))
5540 (c-forward-syntactic-ws)
5541 (c-forward-keyword-prefixed-id type))
5542 ;; There's a type after the `c-colon-type-list-re' match
5543 ;; after a keyword in `c-colon-type-list-kwds'.
5544 (c-forward-id-comma-list type nil))))
5546 (goto-char safe-pos)
5547 t)))
5549 ;; cc-mode requires cc-fonts.
5550 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5552 (defun c-forward-<>-arglist (all-types)
5553 ;; The point is assumed to be at a "<". Try to treat it as the open
5554 ;; paren of an angle bracket arglist and move forward to the
5555 ;; corresponding ">". If successful, the point is left after the
5556 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5557 ;; returned. If ALL-TYPES is t then all encountered arguments in
5558 ;; the arglist that might be types are treated as found types.
5560 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5561 ;; function handles text properties on the angle brackets and argument
5562 ;; separating commas.
5564 ;; `c-restricted-<>-arglists' controls how lenient the template
5565 ;; arglist recognition should be.
5567 ;; This function records identifier ranges on
5568 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5569 ;; `c-record-type-identifiers' is non-nil.
5571 ;; This function might do hidden buffer changes.
5573 (let ((start (point))
5574 ;; If `c-record-type-identifiers' is set then activate
5575 ;; recording of any found types that constitute an argument in
5576 ;; the arglist.
5577 (c-record-found-types (if c-record-type-identifiers t)))
5578 (if (catch 'angle-bracket-arglist-escape
5579 (setq c-record-found-types
5580 (c-forward-<>-arglist-recur all-types)))
5581 (progn
5582 (when (consp c-record-found-types)
5583 (setq c-record-type-identifiers
5584 ;; `nconc' doesn't mind that the tail of
5585 ;; `c-record-found-types' is t.
5586 (nconc c-record-found-types c-record-type-identifiers)))
5587 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
5590 (goto-char start)
5591 nil)))
5593 (defun c-forward-<>-arglist-recur (all-types)
5594 ;; Recursive part of `c-forward-<>-arglist'.
5596 ;; This function might do hidden buffer changes.
5598 (let ((start (point)) res pos tmp
5599 ;; Cover this so that any recorded found type ranges are
5600 ;; automatically lost if it turns out to not be an angle
5601 ;; bracket arglist. It's propagated through the return value
5602 ;; on successful completion.
5603 (c-record-found-types c-record-found-types)
5604 ;; List that collects the positions after the argument
5605 ;; separating ',' in the arglist.
5606 arg-start-pos)
5607 ;; If the '<' has paren open syntax then we've marked it as an angle
5608 ;; bracket arglist before, so skip to the end.
5609 (if (and (not c-parse-and-markup-<>-arglists)
5610 (c-get-char-property (point) 'syntax-table))
5612 (progn
5613 (forward-char)
5614 (if (and (c-go-up-list-forward)
5615 (eq (char-before) ?>))
5617 ;; Got unmatched paren angle brackets. We don't clear the paren
5618 ;; syntax properties and retry, on the basis that it's very
5619 ;; unlikely that paren angle brackets become operators by code
5620 ;; manipulation. It's far more likely that it doesn't match due
5621 ;; to narrowing or some temporary change.
5622 (goto-char start)
5623 nil))
5625 (forward-char) ; Forward over the opening '<'.
5627 (unless (looking-at c-<-op-cont-regexp)
5628 ;; go forward one non-alphanumeric character (group) per iteration of
5629 ;; this loop.
5630 (while (and
5631 (progn
5632 (c-forward-syntactic-ws)
5633 (let ((orig-record-found-types c-record-found-types))
5634 (when (or (and c-record-type-identifiers all-types)
5635 (c-major-mode-is 'java-mode))
5636 ;; All encountered identifiers are types, so set the
5637 ;; promote flag and parse the type.
5638 (progn
5639 (c-forward-syntactic-ws)
5640 (if (looking-at "\\?")
5641 (forward-char)
5642 (when (looking-at c-identifier-start)
5643 (let ((c-promote-possible-types t)
5644 (c-record-found-types t))
5645 (c-forward-type))))
5647 (c-forward-syntactic-ws)
5649 (when (or (looking-at "extends")
5650 (looking-at "super"))
5651 (forward-word)
5652 (c-forward-syntactic-ws)
5653 (let ((c-promote-possible-types t)
5654 (c-record-found-types t))
5655 (c-forward-type)
5656 (c-forward-syntactic-ws))))))
5658 (setq pos (point)) ; e.g. first token inside the '<'
5660 ;; Note: These regexps exploit the match order in \| so
5661 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5662 (c-syntactic-re-search-forward
5663 ;; Stop on ',', '|', '&', '+' and '-' to catch
5664 ;; common binary operators that could be between
5665 ;; two comparison expressions "a<b" and "c>d".
5666 "[<;{},|+&-]\\|[>)]"
5667 nil t t))
5669 (cond
5670 ((eq (char-before) ?>)
5671 ;; Either an operator starting with '>' or the end of
5672 ;; the angle bracket arglist.
5674 (if (looking-at c->-op-cont-regexp)
5675 (progn
5676 (goto-char (match-end 0))
5677 t) ; Continue the loop.
5679 ;; The angle bracket arglist is finished.
5680 (when c-parse-and-markup-<>-arglists
5681 (while arg-start-pos
5682 (c-put-c-type-property (1- (car arg-start-pos))
5683 'c-<>-arg-sep)
5684 (setq arg-start-pos (cdr arg-start-pos)))
5685 (c-mark-<-as-paren start)
5686 (c-mark->-as-paren (1- (point))))
5687 (setq res t)
5688 nil)) ; Exit the loop.
5690 ((eq (char-before) ?<)
5691 ;; Either an operator starting with '<' or a nested arglist.
5692 (setq pos (point))
5693 (let (id-start id-end subres keyword-match)
5694 (cond
5695 ;; The '<' begins a multi-char operator.
5696 ((looking-at c-<-op-cont-regexp)
5697 (setq tmp (match-end 0))
5698 (goto-char (match-end 0)))
5699 ;; We're at a nested <.....>
5700 ((progn
5701 (setq tmp pos)
5702 (backward-char) ; to the '<'
5703 (and
5704 (save-excursion
5705 ;; There's always an identifier before an angle
5706 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5707 ;; or `c-<>-arglist-kwds'.
5708 (c-backward-syntactic-ws)
5709 (setq id-end (point))
5710 (c-simple-skip-symbol-backward)
5711 (when (or (setq keyword-match
5712 (looking-at c-opt-<>-sexp-key))
5713 (not (looking-at c-keywords-regexp)))
5714 (setq id-start (point))))
5715 (setq subres
5716 (let ((c-promote-possible-types t)
5717 (c-record-found-types t))
5718 (c-forward-<>-arglist-recur
5719 (and keyword-match
5720 (c-keyword-member
5721 (c-keyword-sym (match-string 1))
5722 'c-<>-type-kwds)))))))
5724 ;; It was an angle bracket arglist.
5725 (setq c-record-found-types subres)
5727 ;; Record the identifier before the template as a type
5728 ;; or reference depending on whether the arglist is last
5729 ;; in a qualified identifier.
5730 (when (and c-record-type-identifiers
5731 (not keyword-match))
5732 (if (and c-opt-identifier-concat-key
5733 (progn
5734 (c-forward-syntactic-ws)
5735 (looking-at c-opt-identifier-concat-key)))
5736 (c-record-ref-id (cons id-start id-end))
5737 (c-record-type-id (cons id-start id-end)))))
5739 ;; At a "less than" operator.
5741 (forward-char)
5743 t) ; carry on looping.
5745 ((and (not c-restricted-<>-arglists)
5746 (or (and (eq (char-before) ?&)
5747 (not (eq (char-after) ?&)))
5748 (eq (char-before) ?,)))
5749 ;; Just another argument. Record the position. The
5750 ;; type check stuff that made us stop at it is at
5751 ;; the top of the loop.
5752 (setq arg-start-pos (cons (point) arg-start-pos)))
5755 ;; Got a character that can't be in an angle bracket
5756 ;; arglist argument. Abort using `throw', since
5757 ;; it's useless to try to find a surrounding arglist
5758 ;; if we're nested.
5759 (throw 'angle-bracket-arglist-escape nil))))))
5760 (if res
5761 (or c-record-found-types t)))))
5763 (defun c-backward-<>-arglist (all-types &optional limit)
5764 ;; The point is assumed to be directly after a ">". Try to treat it
5765 ;; as the close paren of an angle bracket arglist and move back to
5766 ;; the corresponding "<". If successful, the point is left at
5767 ;; the "<" and t is returned, otherwise the point isn't moved and
5768 ;; nil is returned. ALL-TYPES is passed on to
5769 ;; `c-forward-<>-arglist'.
5771 ;; If the optional LIMIT is given, it bounds the backward search.
5772 ;; It's then assumed to be at a syntactically relevant position.
5774 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5775 ;; function for more details.
5777 (let ((start (point)))
5778 (backward-char)
5779 (if (and (not c-parse-and-markup-<>-arglists)
5780 (c-get-char-property (point) 'syntax-table))
5782 (if (and (c-go-up-list-backward)
5783 (eq (char-after) ?<))
5785 ;; See corresponding note in `c-forward-<>-arglist'.
5786 (goto-char start)
5787 nil)
5789 (while (progn
5790 (c-syntactic-skip-backward "^<;{}" limit t)
5792 (and
5793 (if (eq (char-before) ?<)
5795 ;; Stopped at bob or a char that isn't allowed in an
5796 ;; arglist, so we've failed.
5797 (goto-char start)
5798 nil)
5800 (if (> (point)
5801 (progn (c-beginning-of-current-token)
5802 (point)))
5803 ;; If we moved then the "<" was part of some
5804 ;; multicharacter token.
5807 (backward-char)
5808 (let ((beg-pos (point)))
5809 (if (c-forward-<>-arglist all-types)
5810 (cond ((= (point) start)
5811 ;; Matched the arglist. Break the while.
5812 (goto-char beg-pos)
5813 nil)
5814 ((> (point) start)
5815 ;; We started from a non-paren ">" inside an
5816 ;; arglist.
5817 (goto-char start)
5818 nil)
5820 ;; Matched a shorter arglist. Can be a nested
5821 ;; one so continue looking.
5822 (goto-char beg-pos)
5824 t))))))
5826 (/= (point) start))))
5828 (defun c-forward-name ()
5829 ;; Move forward over a complete name if at the beginning of one,
5830 ;; stopping at the next following token. A keyword, as such,
5831 ;; doesn't count as a name. If the point is not at something that
5832 ;; is recognized as a name then it stays put.
5834 ;; A name could be something as simple as "foo" in C or something as
5835 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5836 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5837 ;; int>::*volatile const" in C++ (this function is actually little
5838 ;; more than a `looking-at' call in all modes except those that,
5839 ;; like C++, have `c-recognize-<>-arglists' set).
5841 ;; Return
5842 ;; o - nil if no name is found;
5843 ;; o - 'template if it's an identifier ending with an angle bracket
5844 ;; arglist;
5845 ;; o - 'operator of it's an operator identifier;
5846 ;; o - t if it's some other kind of name.
5848 ;; This function records identifier ranges on
5849 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5850 ;; `c-record-type-identifiers' is non-nil.
5852 ;; This function might do hidden buffer changes.
5854 (let ((pos (point)) (start (point)) res id-start id-end
5855 ;; Turn off `c-promote-possible-types' here since we might
5856 ;; call `c-forward-<>-arglist' and we don't want it to promote
5857 ;; every suspect thing in the arglist to a type. We're
5858 ;; typically called from `c-forward-type' in this case, and
5859 ;; the caller only wants the top level type that it finds to
5860 ;; be promoted.
5861 c-promote-possible-types)
5862 (while
5863 (and
5864 (looking-at c-identifier-key)
5866 (progn
5867 ;; Check for keyword. We go to the last symbol in
5868 ;; `c-identifier-key' first.
5869 (goto-char (setq id-end (match-end 0)))
5870 (c-simple-skip-symbol-backward)
5871 (setq id-start (point))
5873 (if (looking-at c-keywords-regexp)
5874 (when (and (c-major-mode-is 'c++-mode)
5875 (looking-at
5876 (cc-eval-when-compile
5877 (concat "\\(operator\\|\\(template\\)\\)"
5878 "\\(" (c-lang-const c-nonsymbol-key c++)
5879 "\\|$\\)")))
5880 (if (match-beginning 2)
5881 ;; "template" is only valid inside an
5882 ;; identifier if preceded by "::".
5883 (save-excursion
5884 (c-backward-syntactic-ws)
5885 (and (c-safe (backward-char 2) t)
5886 (looking-at "::")))
5889 ;; Handle a C++ operator or template identifier.
5890 (goto-char id-end)
5891 (c-forward-syntactic-ws)
5892 (cond ((eq (char-before id-end) ?e)
5893 ;; Got "... ::template".
5894 (let ((subres (c-forward-name)))
5895 (when subres
5896 (setq pos (point)
5897 res subres))))
5899 ((looking-at c-identifier-start)
5900 ;; Got a cast operator.
5901 (when (c-forward-type)
5902 (setq pos (point)
5903 res 'operator)
5904 ;; Now we should match a sequence of either
5905 ;; '*', '&' or a name followed by ":: *",
5906 ;; where each can be followed by a sequence
5907 ;; of `c-opt-type-modifier-key'.
5908 (while (cond ((looking-at "[*&]")
5909 (goto-char (match-end 0))
5911 ((looking-at c-identifier-start)
5912 (and (c-forward-name)
5913 (looking-at "::")
5914 (progn
5915 (goto-char (match-end 0))
5916 (c-forward-syntactic-ws)
5917 (eq (char-after) ?*))
5918 (progn
5919 (forward-char)
5920 t))))
5921 (while (progn
5922 (c-forward-syntactic-ws)
5923 (setq pos (point))
5924 (looking-at c-opt-type-modifier-key))
5925 (goto-char (match-end 1))))))
5927 ((looking-at c-overloadable-operators-regexp)
5928 ;; Got some other operator.
5929 (setq c-last-identifier-range
5930 (cons (point) (match-end 0)))
5931 (goto-char (match-end 0))
5932 (c-forward-syntactic-ws)
5933 (setq pos (point)
5934 res 'operator)))
5936 nil)
5938 ;; `id-start' is equal to `id-end' if we've jumped over
5939 ;; an identifier that doesn't end with a symbol token.
5940 ;; That can occur e.g. for Java import directives on the
5941 ;; form "foo.bar.*".
5942 (when (and id-start (/= id-start id-end))
5943 (setq c-last-identifier-range
5944 (cons id-start id-end)))
5945 (goto-char id-end)
5946 (c-forward-syntactic-ws)
5947 (setq pos (point)
5948 res t)))
5950 (progn
5951 (goto-char pos)
5952 (when (or c-opt-identifier-concat-key
5953 c-recognize-<>-arglists)
5955 (cond
5956 ((and c-opt-identifier-concat-key
5957 (looking-at c-opt-identifier-concat-key))
5958 ;; Got a concatenated identifier. This handles the
5959 ;; cases with tricky syntactic whitespace that aren't
5960 ;; covered in `c-identifier-key'.
5961 (goto-char (match-end 0))
5962 (c-forward-syntactic-ws)
5965 ((and c-recognize-<>-arglists
5966 (eq (char-after) ?<))
5967 ;; Maybe an angle bracket arglist.
5968 (when (let ((c-record-type-identifiers t)
5969 (c-record-found-types t))
5970 (c-forward-<>-arglist nil))
5972 (c-add-type start (1+ pos))
5973 (c-forward-syntactic-ws)
5974 (setq pos (point)
5975 c-last-identifier-range nil)
5977 (if (and c-opt-identifier-concat-key
5978 (looking-at c-opt-identifier-concat-key))
5980 ;; Continue if there's an identifier concatenation
5981 ;; operator after the template argument.
5982 (progn
5983 (when (and c-record-type-identifiers id-start)
5984 (c-record-ref-id (cons id-start id-end)))
5985 (forward-char 2)
5986 (c-forward-syntactic-ws)
5989 (when (and c-record-type-identifiers id-start)
5990 (c-record-type-id (cons id-start id-end)))
5991 (setq res 'template)
5992 nil)))
5993 )))))
5995 (goto-char pos)
5996 res))
5998 (defun c-forward-type (&optional brace-block-too)
5999 ;; Move forward over a type spec if at the beginning of one,
6000 ;; stopping at the next following token. The keyword "typedef"
6001 ;; isn't part of a type spec here.
6003 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
6004 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
6005 ;; The current (2009-03-10) intention is to convert all uses of
6006 ;; `c-forward-type' to call with this parameter set, then to
6007 ;; eliminate it.
6009 ;; Return
6010 ;; o - t if it's a known type that can't be a name or other
6011 ;; expression;
6012 ;; o - 'known if it's an otherwise known type (according to
6013 ;; `*-font-lock-extra-types');
6014 ;; o - 'prefix if it's a known prefix of a type;
6015 ;; o - 'found if it's a type that matches one in `c-found-types';
6016 ;; o - 'maybe if it's an identifier that might be a type; or
6017 ;; o - nil if it can't be a type (the point isn't moved then).
6019 ;; The point is assumed to be at the beginning of a token.
6021 ;; Note that this function doesn't skip past the brace definition
6022 ;; that might be considered part of the type, e.g.
6023 ;; "enum {a, b, c} foo".
6025 ;; This function records identifier ranges on
6026 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6027 ;; `c-record-type-identifiers' is non-nil.
6029 ;; This function might do hidden buffer changes.
6030 (when (and c-recognize-<>-arglists
6031 (looking-at "<"))
6032 (c-forward-<>-arglist t)
6033 (c-forward-syntactic-ws))
6035 (let ((start (point)) pos res name-res id-start id-end id-range)
6037 ;; Skip leading type modifiers. If any are found we know it's a
6038 ;; prefix of a type.
6039 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
6040 (while (looking-at c-opt-type-modifier-key)
6041 (goto-char (match-end 1))
6042 (c-forward-syntactic-ws)
6043 (setq res 'prefix)))
6045 (cond
6046 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
6047 ; "typedef".
6048 (goto-char (match-end 1))
6049 (c-forward-syntactic-ws)
6050 (setq pos (point))
6052 (setq name-res (c-forward-name))
6053 (setq res (not (null name-res)))
6054 (when (eq name-res t)
6055 ;; In many languages the name can be used without the
6056 ;; prefix, so we add it to `c-found-types'.
6057 (c-add-type pos (point))
6058 (when (and c-record-type-identifiers
6059 c-last-identifier-range)
6060 (c-record-type-id c-last-identifier-range)))
6061 (when (and brace-block-too
6062 (memq res '(t nil))
6063 (eq (char-after) ?\{)
6064 (save-excursion
6065 (c-safe
6066 (progn (c-forward-sexp)
6067 (c-forward-syntactic-ws)
6068 (setq pos (point))))))
6069 (goto-char pos)
6070 (setq res t))
6071 (unless res (goto-char start))) ; invalid syntax
6073 ((progn
6074 (setq pos nil)
6075 (if (looking-at c-identifier-start)
6076 (save-excursion
6077 (setq id-start (point)
6078 name-res (c-forward-name))
6079 (when name-res
6080 (setq id-end (point)
6081 id-range c-last-identifier-range))))
6082 (and (cond ((looking-at c-primitive-type-key)
6083 (setq res t))
6084 ((c-with-syntax-table c-identifier-syntax-table
6085 (looking-at c-known-type-key))
6086 (setq res 'known)))
6087 (or (not id-end)
6088 (>= (save-excursion
6089 (save-match-data
6090 (goto-char (match-end 1))
6091 (c-forward-syntactic-ws)
6092 (setq pos (point))))
6093 id-end)
6094 (setq res nil))))
6095 ;; Looking at a primitive or known type identifier. We've
6096 ;; checked for a name first so that we don't go here if the
6097 ;; known type match only is a prefix of another name.
6099 (setq id-end (match-end 1))
6101 (when (and c-record-type-identifiers
6102 (or c-promote-possible-types (eq res t)))
6103 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6105 (if (and c-opt-type-component-key
6106 (save-match-data
6107 (looking-at c-opt-type-component-key)))
6108 ;; There might be more keywords for the type.
6109 (let (safe-pos)
6110 (c-forward-keyword-clause 1)
6111 (while (progn
6112 (setq safe-pos (point))
6113 (looking-at c-opt-type-component-key))
6114 (when (and c-record-type-identifiers
6115 (looking-at c-primitive-type-key))
6116 (c-record-type-id (cons (match-beginning 1)
6117 (match-end 1))))
6118 (c-forward-keyword-clause 1))
6119 (if (looking-at c-primitive-type-key)
6120 (progn
6121 (when c-record-type-identifiers
6122 (c-record-type-id (cons (match-beginning 1)
6123 (match-end 1))))
6124 (c-forward-keyword-clause 1)
6125 (setq res t))
6126 (goto-char safe-pos)
6127 (setq res 'prefix)))
6128 (unless (save-match-data (c-forward-keyword-clause 1))
6129 (if pos
6130 (goto-char pos)
6131 (goto-char (match-end 1))
6132 (c-forward-syntactic-ws)))))
6134 (name-res
6135 (cond ((eq name-res t)
6136 ;; A normal identifier.
6137 (goto-char id-end)
6138 (if (or res c-promote-possible-types)
6139 (progn
6140 (c-add-type id-start id-end)
6141 (when (and c-record-type-identifiers id-range)
6142 (c-record-type-id id-range))
6143 (unless res
6144 (setq res 'found)))
6145 (setq res (if (c-check-type id-start id-end)
6146 ;; It's an identifier that has been used as
6147 ;; a type somewhere else.
6148 'found
6149 ;; It's an identifier that might be a type.
6150 'maybe))))
6151 ((eq name-res 'template)
6152 ;; A template is a type.
6153 (goto-char id-end)
6154 (setq res t))
6156 ;; Otherwise it's an operator identifier, which is not a type.
6157 (goto-char start)
6158 (setq res nil)))))
6160 (when res
6161 ;; Skip trailing type modifiers. If any are found we know it's
6162 ;; a type.
6163 (when c-opt-type-modifier-key
6164 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
6165 (goto-char (match-end 1))
6166 (c-forward-syntactic-ws)
6167 (setq res t)))
6168 ;; Step over any type suffix operator. Do not let the existence
6169 ;; of these alter the classification of the found type, since
6170 ;; these operators typically are allowed in normal expressions
6171 ;; too.
6172 (when c-opt-type-suffix-key
6173 (while (looking-at c-opt-type-suffix-key)
6174 (goto-char (match-end 1))
6175 (c-forward-syntactic-ws)))
6177 (when c-opt-type-concat-key ; Only/mainly for pike.
6178 ;; Look for a trailing operator that concatenates the type
6179 ;; with a following one, and if so step past that one through
6180 ;; a recursive call. Note that we don't record concatenated
6181 ;; types in `c-found-types' - it's the component types that
6182 ;; are recorded when appropriate.
6183 (setq pos (point))
6184 (let* ((c-promote-possible-types (or (memq res '(t known))
6185 c-promote-possible-types))
6186 ;; If we can't promote then set `c-record-found-types' so that
6187 ;; we can merge in the types from the second part afterwards if
6188 ;; it turns out to be a known type there.
6189 (c-record-found-types (and c-record-type-identifiers
6190 (not c-promote-possible-types)))
6191 subres)
6192 (if (and (looking-at c-opt-type-concat-key)
6194 (progn
6195 (goto-char (match-end 1))
6196 (c-forward-syntactic-ws)
6197 (setq subres (c-forward-type))))
6199 (progn
6200 ;; If either operand certainly is a type then both are, but we
6201 ;; don't let the existence of the operator itself promote two
6202 ;; uncertain types to a certain one.
6203 (cond ((eq res t))
6204 ((eq subres t)
6205 (unless (eq name-res 'template)
6206 (c-add-type id-start id-end))
6207 (when (and c-record-type-identifiers id-range)
6208 (c-record-type-id id-range))
6209 (setq res t))
6210 ((eq res 'known))
6211 ((eq subres 'known)
6212 (setq res 'known))
6213 ((eq res 'found))
6214 ((eq subres 'found)
6215 (setq res 'found))
6217 (setq res 'maybe)))
6219 (when (and (eq res t)
6220 (consp c-record-found-types))
6221 ;; Merge in the ranges of any types found by the second
6222 ;; `c-forward-type'.
6223 (setq c-record-type-identifiers
6224 ;; `nconc' doesn't mind that the tail of
6225 ;; `c-record-found-types' is t.
6226 (nconc c-record-found-types
6227 c-record-type-identifiers))))
6229 (goto-char pos))))
6231 (when (and c-record-found-types (memq res '(known found)) id-range)
6232 (setq c-record-found-types
6233 (cons id-range c-record-found-types))))
6235 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6237 res))
6239 (defun c-forward-annotation ()
6240 ;; Used for Java code only at the moment. Assumes point is on the
6241 ;; @, moves forward an annotation. returns nil if there is no
6242 ;; annotation at point.
6243 (and (looking-at "@")
6244 (progn (forward-char) t)
6245 (c-forward-type)
6246 (progn (c-forward-syntactic-ws) t)
6247 (if (looking-at "(")
6248 (c-go-list-forward)
6249 t)))
6252 ;; Handling of large scale constructs like statements and declarations.
6254 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6255 ;; defsubst or perhaps even a defun, but it contains lots of free
6256 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6257 (defmacro c-fdoc-shift-type-backward (&optional short)
6258 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6259 ;; of types when parsing a declaration, which means that it
6260 ;; sometimes consumes the identifier in the declaration as a type.
6261 ;; This is used to "backtrack" and make the last type be treated as
6262 ;; an identifier instead.
6263 `(progn
6264 ,(unless short
6265 ;; These identifiers are bound only in the inner let.
6266 '(setq identifier-type at-type
6267 identifier-start type-start
6268 got-parens nil
6269 got-identifier t
6270 got-suffix t
6271 got-suffix-after-parens id-start
6272 paren-depth 0))
6274 (if (setq at-type (if (eq backup-at-type 'prefix)
6276 backup-at-type))
6277 (setq type-start backup-type-start
6278 id-start backup-id-start)
6279 (setq type-start start-pos
6280 id-start start-pos))
6282 ;; When these flags already are set we've found specifiers that
6283 ;; unconditionally signal these attributes - backtracking doesn't
6284 ;; change that. So keep them set in that case.
6285 (or at-type-decl
6286 (setq at-type-decl backup-at-type-decl))
6287 (or maybe-typeless
6288 (setq maybe-typeless backup-maybe-typeless))
6290 ,(unless short
6291 ;; This identifier is bound only in the inner let.
6292 '(setq start id-start))))
6294 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6295 ;; Move forward over a declaration or a cast if at the start of one.
6296 ;; The point is assumed to be at the start of some token. Nil is
6297 ;; returned if no declaration or cast is recognized, and the point
6298 ;; is clobbered in that case.
6300 ;; If a declaration is parsed:
6302 ;; The point is left at the first token after the first complete
6303 ;; declarator, if there is one. The return value is a cons where
6304 ;; the car is the position of the first token in the declarator. (See
6305 ;; below for the cdr.)
6306 ;; Some examples:
6308 ;; void foo (int a, char *b) stuff ...
6309 ;; car ^ ^ point
6310 ;; float (*a)[], b;
6311 ;; car ^ ^ point
6312 ;; unsigned int a = c_style_initializer, b;
6313 ;; car ^ ^ point
6314 ;; unsigned int a (cplusplus_style_initializer), b;
6315 ;; car ^ ^ point (might change)
6316 ;; class Foo : public Bar {}
6317 ;; car ^ ^ point
6318 ;; class PikeClass (int a, string b) stuff ...
6319 ;; car ^ ^ point
6320 ;; enum bool;
6321 ;; car ^ ^ point
6322 ;; enum bool flag;
6323 ;; car ^ ^ point
6324 ;; void cplusplus_function (int x) throw (Bad);
6325 ;; car ^ ^ point
6326 ;; Foo::Foo (int b) : Base (b) {}
6327 ;; car ^ ^ point
6329 ;; The cdr of the return value is non-nil when a
6330 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6331 ;; Specifically it is a dotted pair (A . B) where B is t when a
6332 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6333 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6334 ;; specifier is present. I.e., (some of) the declared
6335 ;; identifier(s) are types.
6337 ;; If a cast is parsed:
6339 ;; The point is left at the first token after the closing paren of
6340 ;; the cast. The return value is `cast'. Note that the start
6341 ;; position must be at the first token inside the cast parenthesis
6342 ;; to recognize it.
6344 ;; PRECEDING-TOKEN-END is the first position after the preceding
6345 ;; token, i.e. on the other side of the syntactic ws from the point.
6346 ;; Use a value less than or equal to (point-min) if the point is at
6347 ;; the first token in (the visible part of) the buffer.
6349 ;; CONTEXT is a symbol that describes the context at the point:
6350 ;; 'decl In a comma-separated declaration context (typically
6351 ;; inside a function declaration arglist).
6352 ;; '<> In an angle bracket arglist.
6353 ;; 'arglist Some other type of arglist.
6354 ;; nil Some other context or unknown context. Includes
6355 ;; within the parens of an if, for, ... construct.
6357 ;; LAST-CAST-END is the first token after the closing paren of a
6358 ;; preceding cast, or nil if none is known. If
6359 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6360 ;; the position after the closest preceding call where a cast was
6361 ;; matched. In that case it's used to discover chains of casts like
6362 ;; "(a) (b) c".
6364 ;; This function records identifier ranges on
6365 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6366 ;; `c-record-type-identifiers' is non-nil.
6368 ;; This function might do hidden buffer changes.
6370 (let (;; `start-pos' is used below to point to the start of the
6371 ;; first type, i.e. after any leading specifiers. It might
6372 ;; also point at the beginning of the preceding syntactic
6373 ;; whitespace.
6374 (start-pos (point))
6375 ;; Set to the result of `c-forward-type'.
6376 at-type
6377 ;; The position of the first token in what we currently
6378 ;; believe is the type in the declaration or cast, after any
6379 ;; specifiers and their associated clauses.
6380 type-start
6381 ;; The position of the first token in what we currently
6382 ;; believe is the declarator for the first identifier. Set
6383 ;; when the type is found, and moved forward over any
6384 ;; `c-decl-hangon-kwds' and their associated clauses that
6385 ;; occurs after the type.
6386 id-start
6387 ;; These store `at-type', `type-start' and `id-start' of the
6388 ;; identifier before the one in those variables. The previous
6389 ;; identifier might turn out to be the real type in a
6390 ;; declaration if the last one has to be the declarator in it.
6391 ;; If `backup-at-type' is nil then the other variables have
6392 ;; undefined values.
6393 backup-at-type backup-type-start backup-id-start
6394 ;; Set if we've found a specifier (apart from "typedef") that makes
6395 ;; the defined identifier(s) types.
6396 at-type-decl
6397 ;; Set if we've a "typedef" keyword.
6398 at-typedef
6399 ;; Set if we've found a specifier that can start a declaration
6400 ;; where there's no type.
6401 maybe-typeless
6402 ;; If a specifier is found that also can be a type prefix,
6403 ;; these flags are set instead of those above. If we need to
6404 ;; back up an identifier, they are copied to the real flag
6405 ;; variables. Thus they only take effect if we fail to
6406 ;; interpret it as a type.
6407 backup-at-type-decl backup-maybe-typeless
6408 ;; Whether we've found a declaration or a cast. We might know
6409 ;; this before we've found the type in it. It's 'ids if we've
6410 ;; found two consecutive identifiers (usually a sure sign, but
6411 ;; we should allow that in labels too), and t if we've found a
6412 ;; specifier keyword (a 100% sure sign).
6413 at-decl-or-cast
6414 ;; Set when we need to back up to parse this as a declaration
6415 ;; but not as a cast.
6416 backup-if-not-cast
6417 ;; For casts, the return position.
6418 cast-end
6419 ;; Save `c-record-type-identifiers' and
6420 ;; `c-record-ref-identifiers' since ranges are recorded
6421 ;; speculatively and should be thrown away if it turns out
6422 ;; that it isn't a declaration or cast.
6423 (save-rec-type-ids c-record-type-identifiers)
6424 (save-rec-ref-ids c-record-ref-identifiers))
6426 (while (c-forward-annotation)
6427 (c-forward-syntactic-ws))
6429 ;; Check for a type. Unknown symbols are treated as possible
6430 ;; types, but they could also be specifiers disguised through
6431 ;; macros like __INLINE__, so we recognize both types and known
6432 ;; specifiers after them too.
6433 (while
6434 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6436 ;; Look for a specifier keyword clause.
6437 (when (or (looking-at c-prefix-spec-kwds-re)
6438 (and (c-major-mode-is 'java-mode)
6439 (looking-at "@[A-Za-z0-9]+")))
6440 (if (looking-at c-typedef-key)
6441 (setq at-typedef t))
6442 (setq kwd-sym (c-keyword-sym (match-string 1)))
6443 (save-excursion
6444 (c-forward-keyword-clause 1)
6445 (setq kwd-clause-end (point))))
6447 (when (setq found-type (c-forward-type t)) ; brace-block-too
6448 ;; Found a known or possible type or a prefix of a known type.
6450 (when at-type
6451 ;; Got two identifiers with nothing but whitespace
6452 ;; between them. That can only happen in declarations.
6453 (setq at-decl-or-cast 'ids)
6455 (when (eq at-type 'found)
6456 ;; If the previous identifier is a found type we
6457 ;; record it as a real one; it might be some sort of
6458 ;; alias for a prefix like "unsigned".
6459 (save-excursion
6460 (goto-char type-start)
6461 (let ((c-promote-possible-types t))
6462 (c-forward-type)))))
6464 (setq backup-at-type at-type
6465 backup-type-start type-start
6466 backup-id-start id-start
6467 at-type found-type
6468 type-start start
6469 id-start (point)
6470 ;; The previous ambiguous specifier/type turned out
6471 ;; to be a type since we've parsed another one after
6472 ;; it, so clear these backup flags.
6473 backup-at-type-decl nil
6474 backup-maybe-typeless nil))
6476 (if kwd-sym
6477 (progn
6478 ;; Handle known specifier keywords and
6479 ;; `c-decl-hangon-kwds' which can occur after known
6480 ;; types.
6482 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6483 ;; It's a hang-on keyword that can occur anywhere.
6484 (progn
6485 (setq at-decl-or-cast t)
6486 (if at-type
6487 ;; Move the identifier start position if
6488 ;; we've passed a type.
6489 (setq id-start kwd-clause-end)
6490 ;; Otherwise treat this as a specifier and
6491 ;; move the fallback position.
6492 (setq start-pos kwd-clause-end))
6493 (goto-char kwd-clause-end))
6495 ;; It's an ordinary specifier so we know that
6496 ;; anything before this can't be the type.
6497 (setq backup-at-type nil
6498 start-pos kwd-clause-end)
6500 (if found-type
6501 ;; It's ambiguous whether this keyword is a
6502 ;; specifier or a type prefix, so set the backup
6503 ;; flags. (It's assumed that `c-forward-type'
6504 ;; moved further than `c-forward-keyword-clause'.)
6505 (progn
6506 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6507 (setq backup-at-type-decl t))
6508 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6509 (setq backup-maybe-typeless t)))
6511 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6512 ;; This test only happens after we've scanned a type.
6513 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6514 (setq at-type-decl t))
6515 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6516 (setq maybe-typeless t))
6518 ;; Haven't matched a type so it's an unambiguous
6519 ;; specifier keyword and we know we're in a
6520 ;; declaration.
6521 (setq at-decl-or-cast t)
6523 (goto-char kwd-clause-end))))
6525 ;; If the type isn't known we continue so that we'll jump
6526 ;; over all specifiers and type identifiers. The reason
6527 ;; to do this for a known type prefix is to make things
6528 ;; like "unsigned INT16" work.
6529 (and found-type (not (eq found-type t))))))
6531 (cond
6532 ((eq at-type t)
6533 ;; If a known type was found, we still need to skip over any
6534 ;; hangon keyword clauses after it. Otherwise it has already
6535 ;; been done in the loop above.
6536 (while (looking-at c-decl-hangon-key)
6537 (c-forward-keyword-clause 1))
6538 (setq id-start (point)))
6540 ((eq at-type 'prefix)
6541 ;; A prefix type is itself a primitive type when it's not
6542 ;; followed by another type.
6543 (setq at-type t))
6545 ((not at-type)
6546 ;; Got no type but set things up to continue anyway to handle
6547 ;; the various cases when a declaration doesn't start with a
6548 ;; type.
6549 (setq id-start start-pos))
6551 ((and (eq at-type 'maybe)
6552 (c-major-mode-is 'c++-mode))
6553 ;; If it's C++ then check if the last "type" ends on the form
6554 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6555 ;; (con|de)structor.
6556 (save-excursion
6557 (let (name end-2 end-1)
6558 (goto-char id-start)
6559 (c-backward-syntactic-ws)
6560 (setq end-2 (point))
6561 (when (and
6562 (c-simple-skip-symbol-backward)
6563 (progn
6564 (setq name
6565 (buffer-substring-no-properties (point) end-2))
6566 ;; Cheating in the handling of syntactic ws below.
6567 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6568 (progn
6569 (setq end-1 (point))
6570 (c-simple-skip-symbol-backward))
6571 (>= (point) type-start)
6572 (equal (buffer-substring-no-properties (point) end-1)
6573 name))
6574 ;; It is a (con|de)structor name. In that case the
6575 ;; declaration is typeless so zap out any preceding
6576 ;; identifier(s) that we might have taken as types.
6577 (goto-char type-start)
6578 (setq at-type nil
6579 backup-at-type nil
6580 id-start type-start))))))
6582 ;; Check for and step over a type decl expression after the thing
6583 ;; that is or might be a type. This can't be skipped since we
6584 ;; need the correct end position of the declarator for
6585 ;; `max-type-decl-end-*'.
6586 (let ((start (point)) (paren-depth 0) pos
6587 ;; True if there's a non-open-paren match of
6588 ;; `c-type-decl-prefix-key'.
6589 got-prefix
6590 ;; True if the declarator is surrounded by a parenthesis pair.
6591 got-parens
6592 ;; True if there is an identifier in the declarator.
6593 got-identifier
6594 ;; True if there's a non-close-paren match of
6595 ;; `c-type-decl-suffix-key'.
6596 got-suffix
6597 ;; True if there's a prefix match outside the outermost
6598 ;; paren pair that surrounds the declarator.
6599 got-prefix-before-parens
6600 ;; True if there's a suffix match outside the outermost
6601 ;; paren pair that surrounds the declarator. The value is
6602 ;; the position of the first suffix match.
6603 got-suffix-after-parens
6604 ;; True if we've parsed the type decl to a token that is
6605 ;; known to end declarations in this context.
6606 at-decl-end
6607 ;; The earlier values of `at-type' and `type-start' if we've
6608 ;; shifted the type backwards.
6609 identifier-type identifier-start
6610 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6611 ;; turn it off during the name skipping below to avoid
6612 ;; getting `c-type' properties that might be bogus. That
6613 ;; can happen since we don't know if
6614 ;; `c-restricted-<>-arglists' will be correct inside the
6615 ;; arglist paren that gets entered.
6616 c-parse-and-markup-<>-arglists)
6618 (goto-char id-start)
6620 ;; Skip over type decl prefix operators. (Note similar code in
6621 ;; `c-font-lock-declarators'.)
6622 (while (and (looking-at c-type-decl-prefix-key)
6623 (if (and (c-major-mode-is 'c++-mode)
6624 (match-beginning 3))
6625 ;; If the second submatch matches in C++ then
6626 ;; we're looking at an identifier that's a
6627 ;; prefix only if it specifies a member pointer.
6628 (when (setq got-identifier (c-forward-name))
6629 (if (looking-at "\\(::\\)")
6630 ;; We only check for a trailing "::" and
6631 ;; let the "*" that should follow be
6632 ;; matched in the next round.
6633 (progn (setq got-identifier nil) t)
6634 ;; It turned out to be the real identifier,
6635 ;; so stop.
6636 nil))
6639 (if (eq (char-after) ?\()
6640 (progn
6641 (setq paren-depth (1+ paren-depth))
6642 (forward-char))
6643 (unless got-prefix-before-parens
6644 (setq got-prefix-before-parens (= paren-depth 0)))
6645 (setq got-prefix t)
6646 (goto-char (match-end 1)))
6647 (c-forward-syntactic-ws))
6649 (setq got-parens (> paren-depth 0))
6651 ;; Skip over an identifier.
6652 (or got-identifier
6653 (and (looking-at c-identifier-start)
6654 (setq got-identifier (c-forward-name))))
6656 ;; Skip over type decl suffix operators.
6657 (while (if (looking-at c-type-decl-suffix-key)
6659 (if (eq (char-after) ?\))
6660 (when (> paren-depth 0)
6661 (setq paren-depth (1- paren-depth))
6662 (forward-char)
6664 (when (if (save-match-data (looking-at "\\s\("))
6665 (c-safe (c-forward-sexp 1) t)
6666 (goto-char (match-end 1))
6668 (when (and (not got-suffix-after-parens)
6669 (= paren-depth 0))
6670 (setq got-suffix-after-parens (match-beginning 0)))
6671 (setq got-suffix t)))
6673 ;; No suffix matched. We might have matched the
6674 ;; identifier as a type and the open paren of a
6675 ;; function arglist as a type decl prefix. In that
6676 ;; case we should "backtrack": Reinterpret the last
6677 ;; type as the identifier, move out of the arglist and
6678 ;; continue searching for suffix operators.
6680 ;; Do this even if there's no preceding type, to cope
6681 ;; with old style function declarations in K&R C,
6682 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6683 ;; style declarations. That isn't applicable in an
6684 ;; arglist context, though.
6685 (when (and (= paren-depth 1)
6686 (not got-prefix-before-parens)
6687 (not (eq at-type t))
6688 (or backup-at-type
6689 maybe-typeless
6690 backup-maybe-typeless
6691 (when c-recognize-typeless-decls
6692 (not context)))
6693 (setq pos (c-up-list-forward (point)))
6694 (eq (char-before pos) ?\)))
6695 (c-fdoc-shift-type-backward)
6696 (goto-char pos)
6699 (c-forward-syntactic-ws))
6701 (when (and (or maybe-typeless backup-maybe-typeless)
6702 (not got-identifier)
6703 (not got-prefix)
6704 at-type)
6705 ;; Have found no identifier but `c-typeless-decl-kwds' has
6706 ;; matched so we know we're inside a declaration. The
6707 ;; preceding type must be the identifier instead.
6708 (c-fdoc-shift-type-backward))
6710 (setq
6711 at-decl-or-cast
6712 (catch 'at-decl-or-cast
6714 ;; CASE 1
6715 (when (> paren-depth 0)
6716 ;; Encountered something inside parens that isn't matched by
6717 ;; the `c-type-decl-*' regexps, so it's not a type decl
6718 ;; expression. Try to skip out to the same paren depth to
6719 ;; not confuse the cast check below.
6720 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6721 ;; If we've found a specifier keyword then it's a
6722 ;; declaration regardless.
6723 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6725 (setq at-decl-end
6726 (looking-at (cond ((eq context '<>) "[,>]")
6727 (context "[,\)]")
6728 (t "[,;]"))))
6730 ;; Now we've collected info about various characteristics of
6731 ;; the construct we're looking at. Below follows a decision
6732 ;; tree based on that. It's ordered to check more certain
6733 ;; signs before less certain ones.
6735 (if got-identifier
6736 (progn
6738 ;; CASE 2
6739 (when (and (or at-type maybe-typeless)
6740 (not (or got-prefix got-parens)))
6741 ;; Got another identifier directly after the type, so it's a
6742 ;; declaration.
6743 (throw 'at-decl-or-cast t))
6745 (when (and got-parens
6746 (not got-prefix)
6747 (not got-suffix-after-parens)
6748 (or backup-at-type
6749 maybe-typeless
6750 backup-maybe-typeless))
6751 ;; Got a declaration of the form "foo bar (gnu);" where we've
6752 ;; recognized "bar" as the type and "gnu" as the declarator.
6753 ;; In this case it's however more likely that "bar" is the
6754 ;; declarator and "gnu" a function argument or initializer (if
6755 ;; `c-recognize-paren-inits' is set), since the parens around
6756 ;; "gnu" would be superfluous if it's a declarator. Shift the
6757 ;; type one step backward.
6758 (c-fdoc-shift-type-backward)))
6760 ;; Found no identifier.
6762 (if backup-at-type
6763 (progn
6766 ;; CASE 3
6767 (when (= (point) start)
6768 ;; Got a plain list of identifiers. If a colon follows it's
6769 ;; a valid label, or maybe a bitfield. Otherwise the last
6770 ;; one probably is the declared identifier and we should
6771 ;; back up to the previous type, providing it isn't a cast.
6772 (if (and (eq (char-after) ?:)
6773 (not (c-major-mode-is 'java-mode)))
6774 (cond
6775 ;; If we've found a specifier keyword then it's a
6776 ;; declaration regardless.
6777 ((eq at-decl-or-cast t)
6778 (throw 'at-decl-or-cast t))
6779 ((and c-has-bitfields
6780 (eq at-decl-or-cast 'ids)) ; bitfield.
6781 (setq backup-if-not-cast t)
6782 (throw 'at-decl-or-cast t)))
6784 (setq backup-if-not-cast t)
6785 (throw 'at-decl-or-cast t)))
6787 ;; CASE 4
6788 (when (and got-suffix
6789 (not got-prefix)
6790 (not got-parens))
6791 ;; Got a plain list of identifiers followed by some suffix.
6792 ;; If this isn't a cast then the last identifier probably is
6793 ;; the declared one and we should back up to the previous
6794 ;; type.
6795 (setq backup-if-not-cast t)
6796 (throw 'at-decl-or-cast t)))
6798 ;; CASE 5
6799 (when (eq at-type t)
6800 ;; If the type is known we know that there can't be any
6801 ;; identifier somewhere else, and it's only in declarations in
6802 ;; e.g. function prototypes and in casts that the identifier may
6803 ;; be left out.
6804 (throw 'at-decl-or-cast t))
6806 (when (= (point) start)
6807 ;; Only got a single identifier (parsed as a type so far).
6808 ;; CASE 6
6809 (if (and
6810 ;; Check that the identifier isn't at the start of an
6811 ;; expression.
6812 at-decl-end
6813 (cond
6814 ((eq context 'decl)
6815 ;; Inside an arglist that contains declarations. If K&R
6816 ;; style declarations and parenthesis style initializers
6817 ;; aren't allowed then the single identifier must be a
6818 ;; type, else we require that it's known or found
6819 ;; (primitive types are handled above).
6820 (or (and (not c-recognize-knr-p)
6821 (not c-recognize-paren-inits))
6822 (memq at-type '(known found))))
6823 ((eq context '<>)
6824 ;; Inside a template arglist. Accept known and found
6825 ;; types; other identifiers could just as well be
6826 ;; constants in C++.
6827 (memq at-type '(known found)))))
6828 (throw 'at-decl-or-cast t)
6829 ;; CASE 7
6830 ;; Can't be a valid declaration or cast, but if we've found a
6831 ;; specifier it can't be anything else either, so treat it as
6832 ;; an invalid/unfinished declaration or cast.
6833 (throw 'at-decl-or-cast at-decl-or-cast))))
6835 (if (and got-parens
6836 (not got-prefix)
6837 (not context)
6838 (not (eq at-type t))
6839 (or backup-at-type
6840 maybe-typeless
6841 backup-maybe-typeless
6842 (when c-recognize-typeless-decls
6843 (or (not got-suffix)
6844 (not (looking-at
6845 c-after-suffixed-type-maybe-decl-key))))))
6846 ;; Got an empty paren pair and a preceding type that probably
6847 ;; really is the identifier. Shift the type backwards to make
6848 ;; the last one the identifier. This is analogous to the
6849 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6850 ;; above.
6852 ;; Exception: In addition to the conditions in that
6853 ;; "backtracking" code, do not shift backward if we're not
6854 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6855 ;; Since there's no preceding type, the shift would mean that
6856 ;; the declaration is typeless. But if the regexp doesn't match
6857 ;; then we will simply fall through in the tests below and not
6858 ;; recognize it at all, so it's better to try it as an abstract
6859 ;; declarator instead.
6860 (c-fdoc-shift-type-backward)
6862 ;; Still no identifier.
6863 ;; CASE 8
6864 (when (and got-prefix (or got-parens got-suffix))
6865 ;; Require `got-prefix' together with either `got-parens' or
6866 ;; `got-suffix' to recognize it as an abstract declarator:
6867 ;; `got-parens' only is probably an empty function call.
6868 ;; `got-suffix' only can build an ordinary expression together
6869 ;; with the preceding identifier which we've taken as a type.
6870 ;; We could actually accept on `got-prefix' only, but that can
6871 ;; easily occur temporarily while writing an expression so we
6872 ;; avoid that case anyway. We could do a better job if we knew
6873 ;; the point when the fontification was invoked.
6874 (throw 'at-decl-or-cast t))
6876 ;; CASE 9
6877 (when (and at-type
6878 (not got-prefix)
6879 (not got-parens)
6880 got-suffix-after-parens
6881 (eq (char-after got-suffix-after-parens) ?\())
6882 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6883 ;; normal function call after all (or perhaps a C++ style object
6884 ;; instantiation expression).
6885 (throw 'at-decl-or-cast nil))))
6887 ;; CASE 10
6888 (when at-decl-or-cast
6889 ;; By now we've located the type in the declaration that we know
6890 ;; we're in.
6891 (throw 'at-decl-or-cast t))
6893 ;; CASE 11
6894 (when (and got-identifier
6895 (not context)
6896 (looking-at c-after-suffixed-type-decl-key)
6897 (if (and got-parens
6898 (not got-prefix)
6899 (not got-suffix)
6900 (not (eq at-type t)))
6901 ;; Shift the type backward in the case that there's a
6902 ;; single identifier inside parens. That can only
6903 ;; occur in K&R style function declarations so it's
6904 ;; more likely that it really is a function call.
6905 ;; Therefore we only do this after
6906 ;; `c-after-suffixed-type-decl-key' has matched.
6907 (progn (c-fdoc-shift-type-backward) t)
6908 got-suffix-after-parens))
6909 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6910 (throw 'at-decl-or-cast t))
6912 ;; CASE 12
6913 (when (and (or got-prefix (not got-parens))
6914 (memq at-type '(t known)))
6915 ;; It's a declaration if a known type precedes it and it can't be a
6916 ;; function call.
6917 (throw 'at-decl-or-cast t))
6919 ;; If we get here we can't tell if this is a type decl or a normal
6920 ;; expression by looking at it alone. (That's under the assumption
6921 ;; that normal expressions always can look like type decl expressions,
6922 ;; which isn't really true but the cases where it doesn't hold are so
6923 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6924 ;; the effort to look for them.)
6926 (unless (or at-decl-end (looking-at "=[^=]"))
6927 ;; If this is a declaration it should end here or its initializer(*)
6928 ;; should start here, so check for allowed separation tokens. Note
6929 ;; that this rule doesn't work e.g. with a K&R arglist after a
6930 ;; function header.
6932 ;; *) Don't check for C++ style initializers using parens
6933 ;; since those already have been matched as suffixes.
6935 ;; If `at-decl-or-cast' is then we've found some other sign that
6936 ;; it's a declaration or cast, so then it's probably an
6937 ;; invalid/unfinished one.
6938 (throw 'at-decl-or-cast at-decl-or-cast))
6940 ;; Below are tests that only should be applied when we're certain to
6941 ;; not have parsed halfway through an expression.
6943 ;; CASE 14
6944 (when (memq at-type '(t known))
6945 ;; The expression starts with a known type so treat it as a
6946 ;; declaration.
6947 (throw 'at-decl-or-cast t))
6949 ;; CASE 15
6950 (when (and (c-major-mode-is 'c++-mode)
6951 ;; In C++ we check if the identifier is a known type, since
6952 ;; (con|de)structors use the class name as identifier.
6953 ;; We've always shifted over the identifier as a type and
6954 ;; then backed up again in this case.
6955 identifier-type
6956 (or (memq identifier-type '(found known))
6957 (and (eq (char-after identifier-start) ?~)
6958 ;; `at-type' probably won't be 'found for
6959 ;; destructors since the "~" is then part of the
6960 ;; type name being checked against the list of
6961 ;; known types, so do a check without that
6962 ;; operator.
6963 (or (save-excursion
6964 (goto-char (1+ identifier-start))
6965 (c-forward-syntactic-ws)
6966 (c-with-syntax-table
6967 c-identifier-syntax-table
6968 (looking-at c-known-type-key)))
6969 (save-excursion
6970 (goto-char (1+ identifier-start))
6971 ;; We have already parsed the type earlier,
6972 ;; so it'd be possible to cache the end
6973 ;; position instead of redoing it here, but
6974 ;; then we'd need to keep track of another
6975 ;; position everywhere.
6976 (c-check-type (point)
6977 (progn (c-forward-type)
6978 (point))))))))
6979 (throw 'at-decl-or-cast t))
6981 (if got-identifier
6982 (progn
6983 ;; CASE 16
6984 (when (and got-prefix-before-parens
6985 at-type
6986 (or at-decl-end (looking-at "=[^=]"))
6987 (not context)
6988 (not got-suffix))
6989 ;; Got something like "foo * bar;". Since we're not inside an
6990 ;; arglist it would be a meaningless expression because the
6991 ;; result isn't used. We therefore choose to recognize it as
6992 ;; a declaration. Do not allow a suffix since it could then
6993 ;; be a function call.
6994 (throw 'at-decl-or-cast t))
6996 ;; CASE 17
6997 (when (and (or got-suffix-after-parens
6998 (looking-at "=[^=]"))
6999 (eq at-type 'found)
7000 (not (eq context 'arglist)))
7001 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
7002 ;; be an odd expression or it could be a declaration. Treat
7003 ;; it as a declaration if "a" has been used as a type
7004 ;; somewhere else (if it's a known type we won't get here).
7005 (throw 'at-decl-or-cast t)))
7007 ;; CASE 18
7008 (when (and context
7009 (or got-prefix
7010 (and (eq context 'decl)
7011 (not c-recognize-paren-inits)
7012 (or got-parens got-suffix))))
7013 ;; Got a type followed by an abstract declarator. If `got-prefix'
7014 ;; is set it's something like "a *" without anything after it. If
7015 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
7016 ;; or similar, which we accept only if the context rules out
7017 ;; expressions.
7018 (throw 'at-decl-or-cast t)))
7020 ;; If we had a complete symbol table here (which rules out
7021 ;; `c-found-types') we should return t due to the disambiguation rule
7022 ;; (in at least C++) that anything that can be parsed as a declaration
7023 ;; is a declaration. Now we're being more defensive and prefer to
7024 ;; highlight things like "foo (bar);" as a declaration only if we're
7025 ;; inside an arglist that contains declarations.
7026 (eq context 'decl))))
7028 ;; The point is now after the type decl expression.
7030 (cond
7031 ;; Check for a cast.
7032 ((save-excursion
7033 (and
7034 c-cast-parens
7036 ;; Should be the first type/identifier in a cast paren.
7037 (> preceding-token-end (point-min))
7038 (memq (char-before preceding-token-end) c-cast-parens)
7040 ;; The closing paren should follow.
7041 (progn
7042 (c-forward-syntactic-ws)
7043 (looking-at "\\s\)"))
7045 ;; There should be a primary expression after it.
7046 (let (pos)
7047 (forward-char)
7048 (c-forward-syntactic-ws)
7049 (setq cast-end (point))
7050 (and (looking-at c-primary-expr-regexp)
7051 (progn
7052 (setq pos (match-end 0))
7054 ;; Check if the expression begins with a prefix keyword.
7055 (match-beginning 2)
7056 (if (match-beginning 1)
7057 ;; Expression begins with an ambiguous operator. Treat
7058 ;; it as a cast if it's a type decl or if we've
7059 ;; recognized the type somewhere else.
7060 (or at-decl-or-cast
7061 (memq at-type '(t known found)))
7062 ;; Unless it's a keyword, it's the beginning of a primary
7063 ;; expression.
7064 (not (looking-at c-keywords-regexp)))))
7065 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
7066 ;; that it matched a whole one so that we don't e.g. confuse
7067 ;; the operator '-' with '->'. It's ok if it matches further,
7068 ;; though, since it e.g. can match the float '.5' while the
7069 ;; operator regexp only matches '.'.
7070 (or (not (looking-at c-nonsymbol-token-regexp))
7071 (<= (match-end 0) pos))))
7073 ;; There should either be a cast before it or something that isn't an
7074 ;; identifier or close paren.
7075 (> preceding-token-end (point-min))
7076 (progn
7077 (goto-char (1- preceding-token-end))
7078 (or (eq (point) last-cast-end)
7079 (progn
7080 (c-backward-syntactic-ws)
7081 (if (< (skip-syntax-backward "w_") 0)
7082 ;; It's a symbol. Accept it only if it's one of the
7083 ;; keywords that can precede an expression (without
7084 ;; surrounding parens).
7085 (looking-at c-simple-stmt-key)
7086 (and
7087 ;; Check that it isn't a close paren (block close is ok,
7088 ;; though).
7089 (not (memq (char-before) '(?\) ?\])))
7090 ;; Check that it isn't a nonsymbol identifier.
7091 (not (c-on-identifier)))))))))
7093 ;; Handle the cast.
7094 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7095 (let ((c-promote-possible-types t))
7096 (goto-char type-start)
7097 (c-forward-type)))
7099 (goto-char cast-end)
7100 'cast)
7102 (at-decl-or-cast
7103 ;; We're at a declaration. Highlight the type and the following
7104 ;; declarators.
7106 (when backup-if-not-cast
7107 (c-fdoc-shift-type-backward t))
7109 (when (and (eq context 'decl) (looking-at ","))
7110 ;; Make sure to propagate the `c-decl-arg-start' property to
7111 ;; the next argument if it's set in this one, to cope with
7112 ;; interactive refontification.
7113 (c-put-c-type-property (point) 'c-decl-arg-start))
7115 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7116 (let ((c-promote-possible-types t))
7117 (save-excursion
7118 (goto-char type-start)
7119 (c-forward-type))))
7121 (cons id-start
7122 (and (or at-type-decl at-typedef)
7123 (cons at-type-decl at-typedef))))
7126 ;; False alarm. Restore the recorded ranges.
7127 (setq c-record-type-identifiers save-rec-type-ids
7128 c-record-ref-identifiers save-rec-ref-ids)
7129 nil))))
7131 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
7132 ;; Assuming that point is at the beginning of a token, check if it starts a
7133 ;; label and if so move over it and return non-nil (t in default situations,
7134 ;; specific symbols (see below) for interesting situations), otherwise don't
7135 ;; move and return nil. "Label" here means "most things with a colon".
7137 ;; More precisely, a "label" is regarded as one of:
7138 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7139 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7140 ;; bare "case", should the colon be missing. We return t;
7141 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7142 ;; return t;
7143 ;; (iv) One of QT's "extended" C++ variants of
7144 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7145 ;; Returns the symbol `qt-2kwds-colon'.
7146 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
7147 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
7148 ;; colon). Currently (2006-03), this applies only to Objective C's
7149 ;; keywords "@private", "@protected", and "@public". Returns t.
7151 ;; One of the things which will NOT be recognized as a label is a bit-field
7152 ;; element of a struct, something like "int foo:5".
7154 ;; The end of the label is taken to be just after the colon, or the end of
7155 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7156 ;; after the end on return. The terminating char gets marked with
7157 ;; `c-decl-end' to improve recognition of the following declaration or
7158 ;; statement.
7160 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7161 ;; label, if any, has already been marked up like that.
7163 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7164 ;; after the preceding token, i.e. on the other side of the
7165 ;; syntactic ws from the point. Use a value less than or equal to
7166 ;; (point-min) if the point is at the first token in (the visible
7167 ;; part of) the buffer.
7169 ;; The optional LIMIT limits the forward scan for the colon.
7171 ;; This function records the ranges of the label symbols on
7172 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7173 ;; non-nil.
7175 ;; This function might do hidden buffer changes.
7177 (let ((start (point))
7178 label-end
7179 qt-symbol-idx
7180 macro-start ; if we're in one.
7181 label-type
7182 kwd)
7183 (cond
7184 ;; "case" or "default" (Doesn't apply to AWK).
7185 ((looking-at c-label-kwds-regexp)
7186 (let ((kwd-end (match-end 1)))
7187 ;; Record only the keyword itself for fontification, since in
7188 ;; case labels the following is a constant expression and not
7189 ;; a label.
7190 (when c-record-type-identifiers
7191 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7193 ;; Find the label end.
7194 (goto-char kwd-end)
7195 (setq label-type
7196 (if (and (c-syntactic-re-search-forward
7197 ;; Stop on chars that aren't allowed in expressions,
7198 ;; and on operator chars that would be meaningless
7199 ;; there. FIXME: This doesn't cope with ?: operators.
7200 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7201 limit t t nil 1)
7202 (match-beginning 2))
7204 (progn ; there's a proper :
7205 (goto-char (match-beginning 2)) ; just after the :
7206 (c-put-c-type-property (1- (point)) 'c-decl-end)
7209 ;; It's an unfinished label. We consider the keyword enough
7210 ;; to recognize it as a label, so that it gets fontified.
7211 ;; Leave the point at the end of it, but don't put any
7212 ;; `c-decl-end' marker.
7213 (goto-char kwd-end)
7214 t))))
7216 ;; @private, @protected, @public, in Objective C, or similar.
7217 ((and c-opt-extra-label-key
7218 (looking-at c-opt-extra-label-key))
7219 ;; For a `c-opt-extra-label-key' match, we record the whole
7220 ;; thing for fontification. That's to get the leading '@' in
7221 ;; Objective-C protection labels fontified.
7222 (goto-char (match-end 1))
7223 (when c-record-type-identifiers
7224 (c-record-ref-id (cons (match-beginning 1) (point))))
7225 (c-put-c-type-property (1- (point)) 'c-decl-end)
7226 (setq label-type t))
7228 ;; All other cases of labels.
7229 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
7231 ;; A colon label must have something before the colon.
7232 (not (eq (char-after) ?:))
7234 ;; Check that we're not after a token that can't precede a label.
7236 ;; Trivially succeeds when there's no preceding token.
7237 ;; Succeeds when we're at a virtual semicolon.
7238 (if preceding-token-end
7239 (<= preceding-token-end (point-min))
7240 (save-excursion
7241 (c-backward-syntactic-ws)
7242 (setq preceding-token-end (point))
7243 (or (bobp)
7244 (c-at-vsemi-p))))
7246 ;; Check if we're after a label, if we're after a closing
7247 ;; paren that belong to statement, and with
7248 ;; `c-label-prefix-re'. It's done in different order
7249 ;; depending on `assume-markup' since the checks have
7250 ;; different expensiveness.
7251 (if assume-markup
7253 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7254 'c-decl-end)
7256 (save-excursion
7257 (goto-char (1- preceding-token-end))
7258 (c-beginning-of-current-token)
7259 (or (looking-at c-label-prefix-re)
7260 (looking-at c-block-stmt-1-key)))
7262 (and (eq (char-before preceding-token-end) ?\))
7263 (c-after-conditional)))
7266 (save-excursion
7267 (goto-char (1- preceding-token-end))
7268 (c-beginning-of-current-token)
7269 (or (looking-at c-label-prefix-re)
7270 (looking-at c-block-stmt-1-key)))
7272 (cond
7273 ((eq (char-before preceding-token-end) ?\))
7274 (c-after-conditional))
7276 ((eq (char-before preceding-token-end) ?:)
7277 ;; Might be after another label, so check it recursively.
7278 (save-restriction
7279 (save-excursion
7280 (goto-char (1- preceding-token-end))
7281 ;; Essentially the same as the
7282 ;; `c-syntactic-re-search-forward' regexp below.
7283 (setq macro-start
7284 (save-excursion (and (c-beginning-of-macro)
7285 (point))))
7286 (if macro-start (narrow-to-region macro-start (point-max)))
7287 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7288 ;; Note: the following should work instead of the
7289 ;; narrow-to-region above. Investigate why not,
7290 ;; sometime. ACM, 2006-03-31.
7291 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7292 ;; macro-start t)
7293 (let ((pte (point))
7294 ;; If the caller turned on recording for us,
7295 ;; it shouldn't apply when we check the
7296 ;; preceding label.
7297 c-record-type-identifiers)
7298 ;; A label can't start at a cpp directive. Check for
7299 ;; this, since c-forward-syntactic-ws would foul up on it.
7300 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7301 (c-forward-syntactic-ws)
7302 (c-forward-label nil pte start))))))))))
7304 ;; Point is still at the beginning of the possible label construct.
7306 ;; Check that the next nonsymbol token is ":", or that we're in one
7307 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7308 ;; arguments. FIXME: Should build this regexp from the language
7309 ;; constants.
7310 (cond
7311 ;; public: protected: private:
7312 ((and
7313 (c-major-mode-is 'c++-mode)
7314 (search-forward-regexp
7315 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7316 (progn (backward-char)
7317 (c-forward-syntactic-ws limit)
7318 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7319 (forward-char)
7320 (setq label-type t))
7321 ;; QT double keyword like "protected slots:" or goto target.
7322 ((progn (goto-char start) nil))
7323 ((when (c-syntactic-re-search-forward
7324 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7325 (backward-char)
7326 (setq label-end (point))
7327 (setq qt-symbol-idx
7328 (and (c-major-mode-is 'c++-mode)
7329 (string-match
7330 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7331 (buffer-substring start (point)))))
7332 (c-forward-syntactic-ws limit)
7333 (cond
7334 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7335 (forward-char)
7336 (setq label-type
7337 (if (or (string= "signals" ; Special QT macro
7338 (setq kwd (buffer-substring-no-properties start label-end)))
7339 (string= "Q_SIGNALS" kwd))
7340 'qt-1kwd-colon
7341 'goto-target)))
7342 ((and qt-symbol-idx
7343 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7344 (progn (c-forward-syntactic-ws limit)
7345 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7346 (forward-char)
7347 (setq label-type 'qt-2kwds-colon)))))))
7349 (save-restriction
7350 (narrow-to-region start (point))
7352 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7353 (catch 'check-label
7354 (goto-char start)
7355 (while (progn
7356 (when (looking-at c-nonlabel-token-key)
7357 (goto-char start)
7358 (setq label-type nil)
7359 (throw 'check-label nil))
7360 (and (c-safe (c-forward-sexp)
7361 (c-forward-syntactic-ws)
7363 (not (eobp)))))
7365 ;; Record the identifiers in the label for fontification, unless
7366 ;; it begins with `c-label-kwds' in which case the following
7367 ;; identifiers are part of a (constant) expression that
7368 ;; shouldn't be fontified.
7369 (when (and c-record-type-identifiers
7370 (progn (goto-char start)
7371 (not (looking-at c-label-kwds-regexp))))
7372 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7373 (c-record-ref-id (cons (match-beginning 0)
7374 (match-end 0)))))
7376 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7377 (goto-char (point-max)))))
7380 ;; Not a label.
7381 (goto-char start)))
7382 label-type))
7384 (defun c-forward-objc-directive ()
7385 ;; Assuming the point is at the beginning of a token, try to move
7386 ;; forward to the end of the Objective-C directive that starts
7387 ;; there. Return t if a directive was fully recognized, otherwise
7388 ;; the point is moved as far as one could be successfully parsed and
7389 ;; nil is returned.
7391 ;; This function records identifier ranges on
7392 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7393 ;; `c-record-type-identifiers' is non-nil.
7395 ;; This function might do hidden buffer changes.
7397 (let ((start (point))
7398 start-char
7399 (c-promote-possible-types t)
7401 ;; Turn off recognition of angle bracket arglists while parsing
7402 ;; types here since the protocol reference list might then be
7403 ;; considered part of the preceding name or superclass-name.
7404 c-recognize-<>-arglists)
7406 (if (or
7407 (when (looking-at
7408 (eval-when-compile
7409 (c-make-keywords-re t
7410 (append (c-lang-const c-protection-kwds objc)
7411 '("@end"))
7412 'objc-mode)))
7413 (goto-char (match-end 1))
7416 (and
7417 (looking-at
7418 (eval-when-compile
7419 (c-make-keywords-re t
7420 '("@interface" "@implementation" "@protocol")
7421 'objc-mode)))
7423 ;; Handle the name of the class itself.
7424 (progn
7425 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7426 ; at EOB.
7427 (goto-char (match-end 0))
7428 (setq lim (point))
7429 (c-skip-ws-forward)
7430 (c-forward-type))
7432 (catch 'break
7433 ;; Look for ": superclass-name" or "( category-name )".
7434 (when (looking-at "[:\(]")
7435 (setq start-char (char-after))
7436 (forward-char)
7437 (c-forward-syntactic-ws)
7438 (unless (c-forward-type) (throw 'break nil))
7439 (when (eq start-char ?\()
7440 (unless (eq (char-after) ?\)) (throw 'break nil))
7441 (forward-char)
7442 (c-forward-syntactic-ws)))
7444 ;; Look for a protocol reference list.
7445 (if (eq (char-after) ?<)
7446 (let ((c-recognize-<>-arglists t)
7447 (c-parse-and-markup-<>-arglists t)
7448 c-restricted-<>-arglists)
7449 (c-forward-<>-arglist t))
7450 t))))
7452 (progn
7453 (c-backward-syntactic-ws lim)
7454 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7455 (c-put-c-type-property (1- (point)) 'c-decl-end)
7458 (c-clear-c-type-property start (point) 'c-decl-end)
7459 nil)))
7461 (defun c-beginning-of-inheritance-list (&optional lim)
7462 ;; Go to the first non-whitespace after the colon that starts a
7463 ;; multiple inheritance introduction. Optional LIM is the farthest
7464 ;; back we should search.
7466 ;; This function might do hidden buffer changes.
7467 (c-with-syntax-table c++-template-syntax-table
7468 (c-backward-token-2 0 t lim)
7469 (while (and (or (looking-at c-symbol-start)
7470 (looking-at "[<,]\\|::"))
7471 (zerop (c-backward-token-2 1 t lim))))))
7473 (defun c-in-method-def-p ()
7474 ;; Return nil if we aren't in a method definition, otherwise the
7475 ;; position of the initial [+-].
7477 ;; This function might do hidden buffer changes.
7478 (save-excursion
7479 (beginning-of-line)
7480 (and c-opt-method-key
7481 (looking-at c-opt-method-key)
7482 (point))
7485 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7486 (defun c-in-gcc-asm-p ()
7487 ;; Return non-nil if point is within a gcc \"asm\" block.
7489 ;; This should be called with point inside an argument list.
7491 ;; Only one level of enclosing parentheses is considered, so for
7492 ;; instance `nil' is returned when in a function call within an asm
7493 ;; operand.
7495 ;; This function might do hidden buffer changes.
7497 (and c-opt-asm-stmt-key
7498 (save-excursion
7499 (beginning-of-line)
7500 (backward-up-list 1)
7501 (c-beginning-of-statement-1 (point-min) nil t)
7502 (looking-at c-opt-asm-stmt-key))))
7504 (defun c-at-toplevel-p ()
7505 "Return a determination as to whether point is \"at the top level\".
7506 Informally, \"at the top level\" is anywhere where you can write
7507 a function.
7509 More precisely, being at the top-level means that point is either
7510 outside any enclosing block (such as a function definition), or
7511 directly inside a class, namespace or other block that contains
7512 another declaration level.
7514 If point is not at the top-level (e.g. it is inside a method
7515 definition), then nil is returned. Otherwise, if point is at a
7516 top-level not enclosed within a class definition, t is returned.
7517 Otherwise, a 2-vector is returned where the zeroth element is the
7518 buffer position of the start of the class declaration, and the first
7519 element is the buffer position of the enclosing class's opening
7520 brace.
7522 Note that this function might do hidden buffer changes. See the
7523 comment at the start of cc-engine.el for more info."
7524 (let ((paren-state (c-parse-state)))
7525 (or (not (c-most-enclosing-brace paren-state))
7526 (c-search-uplist-for-classkey paren-state))))
7528 (defun c-just-after-func-arglist-p (&optional lim)
7529 ;; Return non-nil if the point is in the region after the argument
7530 ;; list of a function and its opening brace (or semicolon in case it
7531 ;; got no body). If there are K&R style argument declarations in
7532 ;; that region, the point has to be inside the first one for this
7533 ;; function to recognize it.
7535 ;; If successful, the point is moved to the first token after the
7536 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7537 ;; the position of the opening paren of the function arglist is
7538 ;; returned.
7540 ;; The point is clobbered if not successful.
7542 ;; LIM is used as bound for backward buffer searches.
7544 ;; This function might do hidden buffer changes.
7546 (let ((beg (point)) end id-start)
7547 (and
7548 (eq (c-beginning-of-statement-1 lim) 'same)
7550 (not (or (c-major-mode-is 'objc-mode)
7551 (c-forward-objc-directive)))
7553 (setq id-start
7554 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7555 (< id-start beg)
7557 ;; There should not be a '=' or ',' between beg and the
7558 ;; start of the declaration since that means we were in the
7559 ;; "expression part" of the declaration.
7560 (or (> (point) beg)
7561 (not (looking-at "[=,]")))
7563 (save-excursion
7564 ;; Check that there's an arglist paren in the
7565 ;; declaration.
7566 (goto-char id-start)
7567 (cond ((eq (char-after) ?\()
7568 ;; The declarator is a paren expression, so skip past it
7569 ;; so that we don't get stuck on that instead of the
7570 ;; function arglist.
7571 (c-forward-sexp))
7572 ((and c-opt-op-identifier-prefix
7573 (looking-at c-opt-op-identifier-prefix))
7574 ;; Don't trip up on "operator ()".
7575 (c-forward-token-2 2 t)))
7576 (and (< (point) beg)
7577 (c-syntactic-re-search-forward "(" beg t t)
7578 (1- (point)))))))
7580 (defun c-in-knr-argdecl (&optional lim)
7581 ;; Return the position of the first argument declaration if point is
7582 ;; inside a K&R style argument declaration list, nil otherwise.
7583 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7584 ;; position that bounds the backward search for the argument list.
7586 ;; Point must be within a possible K&R region, e.g. just before a top-level
7587 ;; "{". It must be outside of parens and brackets. The test can return
7588 ;; false positives otherwise.
7590 ;; This function might do hidden buffer changes.
7592 (save-excursion
7593 (save-restriction
7594 ;; If we're in a macro, our search range is restricted to it. Narrow to
7595 ;; the searchable range.
7596 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
7597 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
7598 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
7599 before-lparen after-rparen
7600 (pp-count-out 20)) ; Max number of paren/brace constructs before
7601 ; we give up
7602 (narrow-to-region low-lim (or macro-end (point-max)))
7604 ;; Search backwards for the defun's argument list. We give up if we
7605 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
7606 ;; a knr region) or BOB.
7608 ;; The criterion for a paren structure being the arg list is:
7609 ;; o - there is non-WS stuff after it but before any "{"; AND
7610 ;; o - the token after it isn't a ";" AND
7611 ;; o - it is preceded by either an identifier (the function name) or
7612 ;; a macro expansion like "DEFUN (...)"; AND
7613 ;; o - its content is a non-empty comma-separated list of identifiers
7614 ;; (an empty arg list won't have a knr region).
7616 ;; The following snippet illustrates these rules:
7617 ;; int foo (bar, baz, yuk)
7618 ;; int bar [] ;
7619 ;; int (*baz) (my_type) ;
7620 ;; int (*) (void) (*yuk) (void) ;
7621 ;; {
7623 (catch 'knr
7624 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7625 (setq pp-count-out (1- pp-count-out))
7626 (c-syntactic-skip-backward "^)]}=")
7627 (cond ((eq (char-before) ?\))
7628 (setq after-rparen (point)))
7629 ((eq (char-before) ?\])
7630 (setq after-rparen nil))
7631 (t ; either } (hit previous defun) or = or no more
7632 ; parens/brackets.
7633 (throw 'knr nil)))
7635 (if after-rparen
7636 ;; We're inside a paren. Could it be our argument list....?
7638 (and
7639 (progn
7640 (goto-char after-rparen)
7641 (unless (c-go-list-backward) (throw 'knr nil)) ;
7642 ;; FIXME!!! What about macros between the parens? 2007/01/20
7643 (setq before-lparen (point)))
7645 ;; It can't be the arg list if next token is ; or {
7646 (progn (goto-char after-rparen)
7647 (c-forward-syntactic-ws)
7648 (not (memq (char-after) '(?\; ?\{ ?\=))))
7650 ;; Is the thing preceding the list an identifier (the
7651 ;; function name), or a macro expansion?
7652 (progn
7653 (goto-char before-lparen)
7654 (eq (c-backward-token-2) 0)
7655 (or (eq (c-on-identifier) (point))
7656 (and (eq (char-after) ?\))
7657 (c-go-up-list-backward)
7658 (eq (c-backward-token-2) 0)
7659 (eq (c-on-identifier) (point)))))
7661 ;; Have we got a non-empty list of comma-separated
7662 ;; identifiers?
7663 (progn
7664 (goto-char before-lparen)
7665 (c-forward-token-2) ; to first token inside parens
7666 (and
7667 (c-on-identifier)
7668 (c-forward-token-2)
7669 (catch 'id-list
7670 (while (eq (char-after) ?\,)
7671 (c-forward-token-2)
7672 (unless (c-on-identifier) (throw 'id-list nil))
7673 (c-forward-token-2))
7674 (eq (char-after) ?\))))))
7676 ;; ...Yes. We've identified the function's argument list.
7677 (throw 'knr
7678 (progn (goto-char after-rparen)
7679 (c-forward-syntactic-ws)
7680 (point)))
7682 ;; ...No. The current parens aren't the function's arg list.
7683 (goto-char before-lparen))
7685 (or (c-go-list-backward) ; backwards over [ .... ]
7686 (throw 'knr nil)))))))))
7688 (defun c-skip-conditional ()
7689 ;; skip forward over conditional at point, including any predicate
7690 ;; statements in parentheses. No error checking is performed.
7692 ;; This function might do hidden buffer changes.
7693 (c-forward-sexp (cond
7694 ;; else if()
7695 ((looking-at (concat "\\<else"
7696 "\\([ \t\n]\\|\\\\\n\\)+"
7697 "if\\>\\([^_]\\|$\\)"))
7699 ;; do, else, try, finally
7700 ((looking-at (concat "\\<\\("
7701 "do\\|else\\|try\\|finally"
7702 "\\)\\>\\([^_]\\|$\\)"))
7704 ;; for, if, while, switch, catch, synchronized, foreach
7705 (t 2))))
7707 (defun c-after-conditional (&optional lim)
7708 ;; If looking at the token after a conditional then return the
7709 ;; position of its start, otherwise return nil.
7711 ;; This function might do hidden buffer changes.
7712 (save-excursion
7713 (and (zerop (c-backward-token-2 1 t lim))
7714 (or (looking-at c-block-stmt-1-key)
7715 (and (eq (char-after) ?\()
7716 (zerop (c-backward-token-2 1 t lim))
7717 (looking-at c-block-stmt-2-key)))
7718 (point))))
7720 (defun c-after-special-operator-id (&optional lim)
7721 ;; If the point is after an operator identifier that isn't handled
7722 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7723 ;; position of the start of that identifier is returned. nil is
7724 ;; returned otherwise. The point may be anywhere in the syntactic
7725 ;; whitespace after the last token of the operator identifier.
7727 ;; This function might do hidden buffer changes.
7728 (save-excursion
7729 (and c-overloadable-operators-regexp
7730 (zerop (c-backward-token-2 1 nil lim))
7731 (looking-at c-overloadable-operators-regexp)
7732 (or (not c-opt-op-identifier-prefix)
7733 (and
7734 (zerop (c-backward-token-2 1 nil lim))
7735 (looking-at c-opt-op-identifier-prefix)))
7736 (point))))
7738 (defsubst c-backward-to-block-anchor (&optional lim)
7739 ;; Assuming point is at a brace that opens a statement block of some
7740 ;; kind, move to the proper anchor point for that block. It might
7741 ;; need to be adjusted further by c-add-stmt-syntax, but the
7742 ;; position at return is suitable as start position for that
7743 ;; function.
7745 ;; This function might do hidden buffer changes.
7746 (unless (= (point) (c-point 'boi))
7747 (let ((start (c-after-conditional lim)))
7748 (if start
7749 (goto-char start)))))
7751 (defsubst c-backward-to-decl-anchor (&optional lim)
7752 ;; Assuming point is at a brace that opens the block of a top level
7753 ;; declaration of some kind, move to the proper anchor point for
7754 ;; that block.
7756 ;; This function might do hidden buffer changes.
7757 (unless (= (point) (c-point 'boi))
7758 (c-beginning-of-statement-1 lim)))
7760 (defun c-search-decl-header-end ()
7761 ;; Search forward for the end of the "header" of the current
7762 ;; declaration. That's the position where the definition body
7763 ;; starts, or the first variable initializer, or the ending
7764 ;; semicolon. I.e. search forward for the closest following
7765 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7766 ;; _after_ the first found token, or at point-max if none is found.
7768 ;; This function might do hidden buffer changes.
7770 (let ((base (point)))
7771 (if (c-major-mode-is 'c++-mode)
7773 ;; In C++ we need to take special care to handle operator
7774 ;; tokens and those pesky template brackets.
7775 (while (and
7776 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
7778 (c-end-of-current-token base)
7779 ;; Handle operator identifiers, i.e. ignore any
7780 ;; operator token preceded by "operator".
7781 (save-excursion
7782 (and (c-safe (c-backward-sexp) t)
7783 (looking-at c-opt-op-identifier-prefix)))
7784 (and (eq (char-before) ?<)
7785 (c-with-syntax-table c++-template-syntax-table
7786 (if (c-safe (goto-char (c-up-list-forward (point))))
7788 (goto-char (point-max))
7789 nil)))))
7790 (setq base (point)))
7792 (while (and
7793 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
7794 (c-end-of-current-token base))
7795 (setq base (point))))))
7797 (defun c-beginning-of-decl-1 (&optional lim)
7798 ;; Go to the beginning of the current declaration, or the beginning
7799 ;; of the previous one if already at the start of it. Point won't
7800 ;; be moved out of any surrounding paren. Return a cons cell of the
7801 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7802 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7803 ;; style argument declarations (and they are to be recognized) then
7804 ;; KNR-POS is set to the start of the first such argument
7805 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7806 ;; position that bounds the backward search.
7808 ;; NB: Cases where the declaration continues after the block, as in
7809 ;; "struct foo { ... } bar;", are currently recognized as two
7810 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7812 ;; This function might do hidden buffer changes.
7813 (catch 'return
7814 (let* ((start (point))
7815 (last-stmt-start (point))
7816 (move (c-beginning-of-statement-1 lim nil t)))
7818 ;; `c-beginning-of-statement-1' stops at a block start, but we
7819 ;; want to continue if the block doesn't begin a top level
7820 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7821 ;; or an open paren.
7822 (let ((beg (point)) tentative-move)
7823 ;; Go back one "statement" each time round the loop until we're just
7824 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7825 ;; an ObjC method. This will move over a multiple declaration whose
7826 ;; components are comma separated.
7827 (while (and
7828 ;; Must check with c-opt-method-key in ObjC mode.
7829 (not (and c-opt-method-key
7830 (looking-at c-opt-method-key)))
7831 (/= last-stmt-start (point))
7832 (progn
7833 (c-backward-syntactic-ws lim)
7834 (not (memq (char-before) '(?\; ?} ?: nil))))
7835 (save-excursion
7836 (backward-char)
7837 (not (looking-at "\\s(")))
7838 ;; Check that we don't move from the first thing in a
7839 ;; macro to its header.
7840 (not (eq (setq tentative-move
7841 (c-beginning-of-statement-1 lim nil t))
7842 'macro)))
7843 (setq last-stmt-start beg
7844 beg (point)
7845 move tentative-move))
7846 (goto-char beg))
7848 (when c-recognize-knr-p
7849 (let ((fallback-pos (point)) knr-argdecl-start)
7850 ;; Handle K&R argdecls. Back up after the "statement" jumped
7851 ;; over by `c-beginning-of-statement-1', unless it was the
7852 ;; function body, in which case we're sitting on the opening
7853 ;; brace now. Then test if we're in a K&R argdecl region and
7854 ;; that we started at the other side of the first argdecl in
7855 ;; it.
7856 (unless (eq (char-after) ?{)
7857 (goto-char last-stmt-start))
7858 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
7859 (< knr-argdecl-start start)
7860 (progn
7861 (goto-char knr-argdecl-start)
7862 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
7863 (throw 'return
7864 (cons (if (eq (char-after fallback-pos) ?{)
7865 'previous
7866 'same)
7867 knr-argdecl-start))
7868 (goto-char fallback-pos))))
7870 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7871 ;; statement, so the result will be 'previous if we've moved over any.
7872 ;; So change our result back to 'same if necessary.
7874 ;; If they were brace list initializers we might not have moved over a
7875 ;; declaration boundary though, so change it to 'same if we've moved
7876 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7877 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7878 ;; potentially can search over a large amount of text.). Take special
7879 ;; pains not to get mislead by C++'s "operator=", and the like.
7880 (if (and (eq move 'previous)
7881 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
7882 c++-template-syntax-table
7883 (syntax-table))
7884 (save-excursion
7885 (and
7886 (progn
7887 (while ; keep going back to "[;={"s until we either find
7888 ; no more, or get to one which isn't an "operator ="
7889 (and (c-syntactic-re-search-forward "[;={]" start t t t)
7890 (eq (char-before) ?=)
7891 c-overloadable-operators-regexp
7892 c-opt-op-identifier-prefix
7893 (save-excursion
7894 (eq (c-backward-token-2) 0)
7895 (looking-at c-overloadable-operators-regexp)
7896 (eq (c-backward-token-2) 0)
7897 (looking-at c-opt-op-identifier-prefix))))
7898 (eq (char-before) ?=))
7899 (c-syntactic-re-search-forward "[;{]" start t t)
7900 (eq (char-before) ?{)
7901 (c-safe (goto-char (c-up-list-forward (point))) t)
7902 (not (c-syntactic-re-search-forward ";" start t t))))))
7903 (cons 'same nil)
7904 (cons move nil)))))
7906 (defun c-end-of-decl-1 ()
7907 ;; Assuming point is at the start of a declaration (as detected by
7908 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7909 ;; `c-beginning-of-decl-1', this function handles the case when a
7910 ;; block is followed by identifiers in e.g. struct declarations in C
7911 ;; or C++. If a proper end was found then t is returned, otherwise
7912 ;; point is moved as far as possible within the current sexp and nil
7913 ;; is returned. This function doesn't handle macros; use
7914 ;; `c-end-of-macro' instead in those cases.
7916 ;; This function might do hidden buffer changes.
7917 (let ((start (point))
7918 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
7919 c++-template-syntax-table
7920 (syntax-table))))
7921 (catch 'return
7922 (c-search-decl-header-end)
7924 (when (and c-recognize-knr-p
7925 (eq (char-before) ?\;)
7926 (c-in-knr-argdecl start))
7927 ;; Stopped at the ';' in a K&R argdecl section which is
7928 ;; detected using the same criteria as in
7929 ;; `c-beginning-of-decl-1'. Move to the following block
7930 ;; start.
7931 (c-syntactic-re-search-forward "{" nil 'move t))
7933 (when (eq (char-before) ?{)
7934 ;; Encountered a block in the declaration. Jump over it.
7935 (condition-case nil
7936 (goto-char (c-up-list-forward (point)))
7937 (error (goto-char (point-max))
7938 (throw 'return nil)))
7939 (if (or (not c-opt-block-decls-with-vars-key)
7940 (save-excursion
7941 (c-with-syntax-table decl-syntax-table
7942 (let ((lim (point)))
7943 (goto-char start)
7944 (not (and
7945 ;; Check for `c-opt-block-decls-with-vars-key'
7946 ;; before the first paren.
7947 (c-syntactic-re-search-forward
7948 (concat "[;=\(\[{]\\|\\("
7949 c-opt-block-decls-with-vars-key
7950 "\\)")
7951 lim t t t)
7952 (match-beginning 1)
7953 (not (eq (char-before) ?_))
7954 ;; Check that the first following paren is
7955 ;; the block.
7956 (c-syntactic-re-search-forward "[;=\(\[{]"
7957 lim t t t)
7958 (eq (char-before) ?{)))))))
7959 ;; The declaration doesn't have any of the
7960 ;; `c-opt-block-decls-with-vars' keywords in the
7961 ;; beginning, so it ends here at the end of the block.
7962 (throw 'return t)))
7964 (c-with-syntax-table decl-syntax-table
7965 (while (progn
7966 (if (eq (char-before) ?\;)
7967 (throw 'return t))
7968 (c-syntactic-re-search-forward ";" nil 'move t))))
7969 nil)))
7971 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
7972 ;; Assuming the point is at an open brace, check if it starts a
7973 ;; block that contains another declaration level, i.e. that isn't a
7974 ;; statement block or a brace list, and if so return non-nil.
7976 ;; If the check is successful, the return value is the start of the
7977 ;; keyword that tells what kind of construct it is, i.e. typically
7978 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7979 ;; the point will be at the start of the construct, before any
7980 ;; leading specifiers, otherwise it's at the returned position.
7982 ;; The point is clobbered if the check is unsuccessful.
7984 ;; CONTAINING-SEXP is the position of the open of the surrounding
7985 ;; paren, or nil if none.
7987 ;; The optional LIMIT limits the backward search for the start of
7988 ;; the construct. It's assumed to be at a syntactically relevant
7989 ;; position.
7991 ;; If any template arglists are found in the searched region before
7992 ;; the open brace, they get marked with paren syntax.
7994 ;; This function might do hidden buffer changes.
7996 (let ((open-brace (point)) kwd-start first-specifier-pos)
7997 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7999 (when (and c-recognize-<>-arglists
8000 (eq (char-before) ?>))
8001 ;; Could be at the end of a template arglist.
8002 (let ((c-parse-and-markup-<>-arglists t)
8003 (c-disallow-comma-in-<>-arglists
8004 (and containing-sexp
8005 (not (eq (char-after containing-sexp) ?{)))))
8006 (while (and
8007 (c-backward-<>-arglist nil limit)
8008 (progn
8009 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8010 (eq (char-before) ?>))))))
8012 ;; Note: Can't get bogus hits inside template arglists below since they
8013 ;; have gotten paren syntax above.
8014 (when (and
8015 ;; If `goto-start' is set we begin by searching for the
8016 ;; first possible position of a leading specifier list.
8017 ;; The `c-decl-block-key' search continues from there since
8018 ;; we know it can't match earlier.
8019 (if goto-start
8020 (when (c-syntactic-re-search-forward c-symbol-start
8021 open-brace t t)
8022 (goto-char (setq first-specifier-pos (match-beginning 0)))
8026 (cond
8027 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
8028 (goto-char (setq kwd-start (match-beginning 0)))
8031 ;; Found a keyword that can't be a type?
8032 (match-beginning 1)
8034 ;; Can be a type too, in which case it's the return type of a
8035 ;; function (under the assumption that no declaration level
8036 ;; block construct starts with a type).
8037 (not (c-forward-type))
8039 ;; Jumped over a type, but it could be a declaration keyword
8040 ;; followed by the declared identifier that we've jumped over
8041 ;; instead (e.g. in "class Foo {"). If it indeed is a type
8042 ;; then we should be at the declarator now, so check for a
8043 ;; valid declarator start.
8045 ;; Note: This doesn't cope with the case when a declared
8046 ;; identifier is followed by e.g. '(' in a language where '('
8047 ;; also might be part of a declarator expression. Currently
8048 ;; there's no such language.
8049 (not (or (looking-at c-symbol-start)
8050 (looking-at c-type-decl-prefix-key)))))
8052 ;; In Pike a list of modifiers may be followed by a brace
8053 ;; to make them apply to many identifiers. Note that the
8054 ;; match data will be empty on return in this case.
8055 ((and (c-major-mode-is 'pike-mode)
8056 (progn
8057 (goto-char open-brace)
8058 (= (c-backward-token-2) 0))
8059 (looking-at c-specifier-key)
8060 ;; Use this variant to avoid yet another special regexp.
8061 (c-keyword-member (c-keyword-sym (match-string 1))
8062 'c-modifier-kwds))
8063 (setq kwd-start (point))
8064 t)))
8066 ;; Got a match.
8068 (if goto-start
8069 ;; Back up over any preceding specifiers and their clauses
8070 ;; by going forward from `first-specifier-pos', which is the
8071 ;; earliest possible position where the specifier list can
8072 ;; start.
8073 (progn
8074 (goto-char first-specifier-pos)
8076 (while (< (point) kwd-start)
8077 (if (looking-at c-symbol-key)
8078 ;; Accept any plain symbol token on the ground that
8079 ;; it's a specifier masked through a macro (just
8080 ;; like `c-forward-decl-or-cast-1' skip forward over
8081 ;; such tokens).
8083 ;; Could be more restrictive wrt invalid keywords,
8084 ;; but that'd only occur in invalid code so there's
8085 ;; no use spending effort on it.
8086 (let ((end (match-end 0)))
8087 (unless (c-forward-keyword-clause 0)
8088 (goto-char end)
8089 (c-forward-syntactic-ws)))
8091 ;; Can't parse a declaration preamble and is still
8092 ;; before `kwd-start'. That means `first-specifier-pos'
8093 ;; was in some earlier construct. Search again.
8094 (if (c-syntactic-re-search-forward c-symbol-start
8095 kwd-start 'move t)
8096 (goto-char (setq first-specifier-pos (match-beginning 0)))
8097 ;; Got no preamble before the block declaration keyword.
8098 (setq first-specifier-pos kwd-start))))
8100 (goto-char first-specifier-pos))
8101 (goto-char kwd-start))
8103 kwd-start)))
8105 (defun c-search-uplist-for-classkey (paren-state)
8106 ;; Check if the closest containing paren sexp is a declaration
8107 ;; block, returning a 2 element vector in that case. Aref 0
8108 ;; contains the bufpos at boi of the class key line, and aref 1
8109 ;; contains the bufpos of the open brace. This function is an
8110 ;; obsolete wrapper for `c-looking-at-decl-block'.
8112 ;; This function might do hidden buffer changes.
8113 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
8114 (when open-paren-pos
8115 (save-excursion
8116 (goto-char open-paren-pos)
8117 (when (and (eq (char-after) ?{)
8118 (c-looking-at-decl-block
8119 (c-safe-position open-paren-pos paren-state)
8120 nil))
8121 (back-to-indentation)
8122 (vector (point) open-paren-pos))))))
8124 (defmacro c-pull-open-brace (ps)
8125 ;; Pull the next open brace from PS (which has the form of paren-state),
8126 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
8127 `(progn
8128 (while (consp (car ,ps))
8129 (setq ,ps (cdr ,ps)))
8130 (prog1 (car ,ps)
8131 (setq ,ps (cdr ,ps)))))
8133 (defun c-most-enclosing-decl-block (paren-state)
8134 ;; Return the buffer position of the most enclosing decl-block brace (in the
8135 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
8136 ;; none was found.
8137 (let* ((open-brace (c-pull-open-brace paren-state))
8138 (next-open-brace (c-pull-open-brace paren-state)))
8139 (while (and open-brace
8140 (save-excursion
8141 (goto-char open-brace)
8142 (not (c-looking-at-decl-block next-open-brace nil))))
8143 (setq open-brace next-open-brace
8144 next-open-brace (c-pull-open-brace paren-state)))
8145 open-brace))
8147 (defun c-cheap-inside-bracelist-p (paren-state)
8148 ;; Return the position of the L-brace if point is inside a brace list
8149 ;; initialization of an array, etc. This is an approximate function,
8150 ;; designed for speed over accuracy. It will not find every bracelist, but
8151 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
8152 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
8153 ;; is everywhere else.
8154 (let (b-pos)
8155 (save-excursion
8156 (while
8157 (and (setq b-pos (c-pull-open-brace paren-state))
8158 (progn (goto-char b-pos)
8159 (c-backward-sws)
8160 (c-backward-token-2)
8161 (not (looking-at "=")))))
8162 b-pos)))
8164 (defun c-inside-bracelist-p (containing-sexp paren-state)
8165 ;; return the buffer position of the beginning of the brace list
8166 ;; statement if we're inside a brace list, otherwise return nil.
8167 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
8168 ;; paren. PAREN-STATE is the remainder of the state of enclosing
8169 ;; braces
8171 ;; N.B.: This algorithm can potentially get confused by cpp macros
8172 ;; placed in inconvenient locations. It's a trade-off we make for
8173 ;; speed.
8175 ;; This function might do hidden buffer changes.
8177 ;; This will pick up brace list declarations.
8178 (c-safe
8179 (save-excursion
8180 (goto-char containing-sexp)
8181 (c-forward-sexp -1)
8182 (let (bracepos)
8183 (if (and (or (looking-at c-brace-list-key)
8184 (progn (c-forward-sexp -1)
8185 (looking-at c-brace-list-key)))
8186 (setq bracepos (c-down-list-forward (point)))
8187 (not (c-crosses-statement-barrier-p (point)
8188 (- bracepos 2))))
8189 (point)))))
8190 ;; this will pick up array/aggregate init lists, even if they are nested.
8191 (save-excursion
8192 (let ((class-key
8193 ;; Pike can have class definitions anywhere, so we must
8194 ;; check for the class key here.
8195 (and (c-major-mode-is 'pike-mode)
8196 c-decl-block-key))
8197 bufpos braceassignp lim next-containing)
8198 (while (and (not bufpos)
8199 containing-sexp)
8200 (when paren-state
8201 (if (consp (car paren-state))
8202 (setq lim (cdr (car paren-state))
8203 paren-state (cdr paren-state))
8204 (setq lim (car paren-state)))
8205 (when paren-state
8206 (setq next-containing (car paren-state)
8207 paren-state (cdr paren-state))))
8208 (goto-char containing-sexp)
8209 (if (c-looking-at-inexpr-block next-containing next-containing)
8210 ;; We're in an in-expression block of some kind. Do not
8211 ;; check nesting. We deliberately set the limit to the
8212 ;; containing sexp, so that c-looking-at-inexpr-block
8213 ;; doesn't check for an identifier before it.
8214 (setq containing-sexp nil)
8215 ;; see if the open brace is preceded by = or [...] in
8216 ;; this statement, but watch out for operator=
8217 (setq braceassignp 'dontknow)
8218 (c-backward-token-2 1 t lim)
8219 ;; Checks to do only on the first sexp before the brace.
8220 (when (and c-opt-inexpr-brace-list-key
8221 (eq (char-after) ?\[))
8222 ;; In Java, an initialization brace list may follow
8223 ;; directly after "new Foo[]", so check for a "new"
8224 ;; earlier.
8225 (while (eq braceassignp 'dontknow)
8226 (setq braceassignp
8227 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8228 ((looking-at c-opt-inexpr-brace-list-key) t)
8229 ((looking-at "\\sw\\|\\s_\\|[.[]")
8230 ;; Carry on looking if this is an
8231 ;; identifier (may contain "." in Java)
8232 ;; or another "[]" sexp.
8233 'dontknow)
8234 (t nil)))))
8235 ;; Checks to do on all sexps before the brace, up to the
8236 ;; beginning of the statement.
8237 (while (eq braceassignp 'dontknow)
8238 (cond ((eq (char-after) ?\;)
8239 (setq braceassignp nil))
8240 ((and class-key
8241 (looking-at class-key))
8242 (setq braceassignp nil))
8243 ((eq (char-after) ?=)
8244 ;; We've seen a =, but must check earlier tokens so
8245 ;; that it isn't something that should be ignored.
8246 (setq braceassignp 'maybe)
8247 (while (and (eq braceassignp 'maybe)
8248 (zerop (c-backward-token-2 1 t lim)))
8249 (setq braceassignp
8250 (cond
8251 ;; Check for operator =
8252 ((and c-opt-op-identifier-prefix
8253 (looking-at c-opt-op-identifier-prefix))
8254 nil)
8255 ;; Check for `<opchar>= in Pike.
8256 ((and (c-major-mode-is 'pike-mode)
8257 (or (eq (char-after) ?`)
8258 ;; Special case for Pikes
8259 ;; `[]=, since '[' is not in
8260 ;; the punctuation class.
8261 (and (eq (char-after) ?\[)
8262 (eq (char-before) ?`))))
8263 nil)
8264 ((looking-at "\\s.") 'maybe)
8265 ;; make sure we're not in a C++ template
8266 ;; argument assignment
8267 ((and
8268 (c-major-mode-is 'c++-mode)
8269 (save-excursion
8270 (let ((here (point))
8271 (pos< (progn
8272 (skip-chars-backward "^<>")
8273 (point))))
8274 (and (eq (char-before) ?<)
8275 (not (c-crosses-statement-barrier-p
8276 pos< here))
8277 (not (c-in-literal))
8278 ))))
8279 nil)
8280 (t t))))))
8281 (if (and (eq braceassignp 'dontknow)
8282 (/= (c-backward-token-2 1 t lim) 0))
8283 (setq braceassignp nil)))
8284 (if (not braceassignp)
8285 (if (eq (char-after) ?\;)
8286 ;; Brace lists can't contain a semicolon, so we're done.
8287 (setq containing-sexp nil)
8288 ;; Go up one level.
8289 (setq containing-sexp next-containing
8290 lim nil
8291 next-containing nil))
8292 ;; we've hit the beginning of the aggregate list
8293 (c-beginning-of-statement-1
8294 (c-most-enclosing-brace paren-state))
8295 (setq bufpos (point))))
8297 bufpos))
8300 (defun c-looking-at-special-brace-list (&optional lim)
8301 ;; If we're looking at the start of a pike-style list, ie `({ })',
8302 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
8303 ;; positions and its entry in c-special-brace-lists is returned, nil
8304 ;; otherwise. The ending position is nil if the list is still open.
8305 ;; LIM is the limit for forward search. The point may either be at
8306 ;; the `(' or at the following paren character. Tries to check the
8307 ;; matching closer, but assumes it's correct if no balanced paren is
8308 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8309 ;; a special brace list).
8311 ;; This function might do hidden buffer changes.
8312 (if c-special-brace-lists
8313 (condition-case ()
8314 (save-excursion
8315 (let ((beg (point))
8316 inner-beg end type)
8317 (c-forward-syntactic-ws)
8318 (if (eq (char-after) ?\()
8319 (progn
8320 (forward-char 1)
8321 (c-forward-syntactic-ws)
8322 (setq inner-beg (point))
8323 (setq type (assq (char-after) c-special-brace-lists)))
8324 (if (setq type (assq (char-after) c-special-brace-lists))
8325 (progn
8326 (setq inner-beg (point))
8327 (c-backward-syntactic-ws)
8328 (forward-char -1)
8329 (setq beg (if (eq (char-after) ?\()
8330 (point)
8331 nil)))))
8332 (if (and beg type)
8333 (if (and (c-safe
8334 (goto-char beg)
8335 (c-forward-sexp 1)
8336 (setq end (point))
8337 (= (char-before) ?\)))
8338 (c-safe
8339 (goto-char inner-beg)
8340 (if (looking-at "\\s(")
8341 ;; Check balancing of the inner paren
8342 ;; below.
8343 (progn
8344 (c-forward-sexp 1)
8346 ;; If the inner char isn't a paren then
8347 ;; we can't check balancing, so just
8348 ;; check the char before the outer
8349 ;; closing paren.
8350 (goto-char end)
8351 (backward-char)
8352 (c-backward-syntactic-ws)
8353 (= (char-before) (cdr type)))))
8354 (if (or (/= (char-syntax (char-before)) ?\))
8355 (= (progn
8356 (c-forward-syntactic-ws)
8357 (point))
8358 (1- end)))
8359 (cons (cons beg end) type))
8360 (cons (list beg) type)))))
8361 (error nil))))
8363 (defun c-looking-at-bos (&optional lim)
8364 ;; Return non-nil if between two statements or declarations, assuming
8365 ;; point is not inside a literal or comment.
8367 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8368 ;; are recommended instead.
8370 ;; This function might do hidden buffer changes.
8371 (c-at-statement-start-p))
8372 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8374 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8375 ;; Return non-nil if we're looking at the beginning of a block
8376 ;; inside an expression. The value returned is actually a cons of
8377 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8378 ;; position of the beginning of the construct.
8380 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8381 ;; position of the closest containing list. If it's nil, the
8382 ;; containing paren isn't used to decide whether we're inside an
8383 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8384 ;; needs to be farther back.
8386 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8387 ;; brace block might be done. It should only be used when the
8388 ;; construct can be assumed to be complete, i.e. when the original
8389 ;; starting position was further down than that.
8391 ;; This function might do hidden buffer changes.
8393 (save-excursion
8394 (let ((res 'maybe) passed-paren
8395 (closest-lim (or containing-sexp lim (point-min)))
8396 ;; Look at the character after point only as a last resort
8397 ;; when we can't disambiguate.
8398 (block-follows (and (eq (char-after) ?{) (point))))
8400 (while (and (eq res 'maybe)
8401 (progn (c-backward-syntactic-ws)
8402 (> (point) closest-lim))
8403 (not (bobp))
8404 (progn (backward-char)
8405 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8406 (c-safe (forward-char)
8407 (goto-char (scan-sexps (point) -1))))
8409 (setq res
8410 (if (looking-at c-keywords-regexp)
8411 (let ((kw-sym (c-keyword-sym (match-string 1))))
8412 (cond
8413 ((and block-follows
8414 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8415 (and (not (eq passed-paren ?\[))
8416 (or (not (looking-at c-class-key))
8417 ;; If the class definition is at the start of
8418 ;; a statement, we don't consider it an
8419 ;; in-expression class.
8420 (let ((prev (point)))
8421 (while (and
8422 (= (c-backward-token-2 1 nil closest-lim) 0)
8423 (eq (char-syntax (char-after)) ?w))
8424 (setq prev (point)))
8425 (goto-char prev)
8426 (not (c-at-statement-start-p)))
8427 ;; Also, in Pike we treat it as an
8428 ;; in-expression class if it's used in an
8429 ;; object clone expression.
8430 (save-excursion
8431 (and check-at-end
8432 (c-major-mode-is 'pike-mode)
8433 (progn (goto-char block-follows)
8434 (zerop (c-forward-token-2 1 t)))
8435 (eq (char-after) ?\())))
8436 (cons 'inexpr-class (point))))
8437 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8438 (when (not passed-paren)
8439 (cons 'inexpr-statement (point))))
8440 ((c-keyword-member kw-sym 'c-lambda-kwds)
8441 (when (or (not passed-paren)
8442 (eq passed-paren ?\())
8443 (cons 'inlambda (point))))
8444 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8445 nil)
8447 'maybe)))
8449 (if (looking-at "\\s(")
8450 (if passed-paren
8451 (if (and (eq passed-paren ?\[)
8452 (eq (char-after) ?\[))
8453 ;; Accept several square bracket sexps for
8454 ;; Java array initializations.
8455 'maybe)
8456 (setq passed-paren (char-after))
8457 'maybe)
8458 'maybe))))
8460 (if (eq res 'maybe)
8461 (when (and c-recognize-paren-inexpr-blocks
8462 block-follows
8463 containing-sexp
8464 (eq (char-after containing-sexp) ?\())
8465 (goto-char containing-sexp)
8466 (if (or (save-excursion
8467 (c-backward-syntactic-ws lim)
8468 (and (> (point) (or lim (point-min)))
8469 (c-on-identifier)))
8470 (and c-special-brace-lists
8471 (c-looking-at-special-brace-list)))
8473 (cons 'inexpr-statement (point))))
8475 res))))
8477 (defun c-looking-at-inexpr-block-backward (paren-state)
8478 ;; Returns non-nil if we're looking at the end of an in-expression
8479 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8480 ;; PAREN-STATE is the paren state relevant at the current position.
8482 ;; This function might do hidden buffer changes.
8483 (save-excursion
8484 ;; We currently only recognize a block.
8485 (let ((here (point))
8486 (elem (car-safe paren-state))
8487 containing-sexp)
8488 (when (and (consp elem)
8489 (progn (goto-char (cdr elem))
8490 (c-forward-syntactic-ws here)
8491 (= (point) here)))
8492 (goto-char (car elem))
8493 (if (setq paren-state (cdr paren-state))
8494 (setq containing-sexp (car-safe paren-state)))
8495 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8496 paren-state)
8497 containing-sexp)))))
8499 (defun c-at-macro-vsemi-p (&optional pos)
8500 ;; Is there a "virtual semicolon" at POS or point?
8501 ;; (See cc-defs.el for full details of "virtual semicolons".)
8503 ;; This is true when point is at the last non syntactic WS position on the
8504 ;; line, there is a macro call last on the line, and this particular macro's
8505 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
8506 ;; semicolon.
8507 (save-excursion
8508 (save-restriction
8509 (widen)
8510 (if pos
8511 (goto-char pos)
8512 (setq pos (point)))
8513 (and
8514 c-macro-with-semi-re
8515 (not (c-in-literal))
8516 (eq (skip-chars-backward " \t") 0)
8518 ;; Check we've got nothing after this except comments and empty lines
8519 ;; joined by escaped EOLs.
8520 (skip-chars-forward " \t") ; always returns non-nil.
8521 (progn
8522 (while ; go over 1 block comment per iteration.
8523 (and
8524 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
8525 (goto-char (match-end 0))
8526 (cond
8527 ((looking-at c-block-comment-start-regexp)
8528 (and (forward-comment 1)
8529 (skip-chars-forward " \t"))) ; always returns non-nil
8530 ((looking-at c-line-comment-start-regexp)
8531 (end-of-line)
8532 nil)
8533 (t nil))))
8534 (eolp))
8536 (goto-char pos)
8537 (progn (c-backward-syntactic-ws)
8538 (eq (point) pos))
8540 ;; Check for one of the listed macros being before point.
8541 (or (not (eq (char-before) ?\)))
8542 (when (c-go-list-backward)
8543 (c-backward-syntactic-ws)
8545 (c-simple-skip-symbol-backward)
8546 (looking-at c-macro-with-semi-re)))))
8548 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
8551 ;; `c-guess-basic-syntax' and the functions that precedes it below
8552 ;; implements the main decision tree for determining the syntactic
8553 ;; analysis of the current line of code.
8555 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8556 ;; auto newline analysis.
8557 (defvar c-auto-newline-analysis nil)
8559 (defun c-brace-anchor-point (bracepos)
8560 ;; BRACEPOS is the position of a brace in a construct like "namespace
8561 ;; Bar {". Return the anchor point in this construct; this is the
8562 ;; earliest symbol on the brace's line which isn't earlier than
8563 ;; "namespace".
8565 ;; Currently (2007-08-17), "like namespace" means "matches
8566 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8567 ;; or anything like that.
8568 (save-excursion
8569 (let ((boi (c-point 'boi bracepos)))
8570 (goto-char bracepos)
8571 (while (and (> (point) boi)
8572 (not (looking-at c-other-decl-block-key)))
8573 (c-backward-token-2))
8574 (if (> (point) boi) (point) boi))))
8576 (defsubst c-add-syntax (symbol &rest args)
8577 ;; A simple function to prepend a new syntax element to
8578 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8579 ;; should always be dynamically bound but since we read it first
8580 ;; we'll fail properly anyway if this function is misused.
8581 (setq c-syntactic-context (cons (cons symbol args)
8582 c-syntactic-context)))
8584 (defsubst c-append-syntax (symbol &rest args)
8585 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8586 ;; (Normally not necessary.)
8587 (setq c-syntactic-context (nconc c-syntactic-context
8588 (list (cons symbol args)))))
8590 (defun c-add-stmt-syntax (syntax-symbol
8591 syntax-extra-args
8592 stop-at-boi-only
8593 containing-sexp
8594 paren-state)
8595 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8596 ;; needed with further syntax elements of the types `substatement',
8597 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8598 ;; `defun-block-intro'.
8600 ;; Do the generic processing to anchor the given syntax symbol on
8601 ;; the preceding statement: Skip over any labels and containing
8602 ;; statements on the same line, and then search backward until we
8603 ;; find a statement or block start that begins at boi without a
8604 ;; label or comment.
8606 ;; Point is assumed to be at the prospective anchor point for the
8607 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8608 ;; skip past open parens and containing statements. Most of the added
8609 ;; syntax elements will get the same anchor point - the exception is
8610 ;; for an anchor in a construct like "namespace"[*] - this is as early
8611 ;; as possible in the construct but on the same line as the {.
8613 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8615 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8616 ;; syntax symbol. They are appended after the anchor point.
8618 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8619 ;; if the current statement starts there.
8621 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8622 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8624 ;; This function might do hidden buffer changes.
8626 (if (= (point) (c-point 'boi))
8627 ;; This is by far the most common case, so let's give it special
8628 ;; treatment.
8629 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
8631 (let ((syntax-last c-syntactic-context)
8632 (boi (c-point 'boi))
8633 ;; Set when we're on a label, so that we don't stop there.
8634 ;; FIXME: To be complete we should check if we're on a label
8635 ;; now at the start.
8636 on-label)
8638 ;; Use point as the anchor point for "namespace", "extern", etc.
8639 (apply 'c-add-syntax syntax-symbol
8640 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
8641 (point) nil)
8642 syntax-extra-args)
8644 ;; Loop while we have to back out of containing blocks.
8645 (while
8646 (and
8647 (catch 'back-up-block
8649 ;; Loop while we have to back up statements.
8650 (while (or (/= (point) boi)
8651 on-label
8652 (looking-at c-comment-start-regexp))
8654 ;; Skip past any comments that stands between the
8655 ;; statement start and boi.
8656 (let ((savepos (point)))
8657 (while (and (/= savepos boi)
8658 (c-backward-single-comment))
8659 (setq savepos (point)
8660 boi (c-point 'boi)))
8661 (goto-char savepos))
8663 ;; Skip to the beginning of this statement or backward
8664 ;; another one.
8665 (let ((old-pos (point))
8666 (old-boi boi)
8667 (step-type (c-beginning-of-statement-1 containing-sexp)))
8668 (setq boi (c-point 'boi)
8669 on-label (eq step-type 'label))
8671 (cond ((= (point) old-pos)
8672 ;; If we didn't move we're at the start of a block and
8673 ;; have to continue outside it.
8674 (throw 'back-up-block t))
8676 ((and (eq step-type 'up)
8677 (>= (point) old-boi)
8678 (looking-at "else\\>[^_]")
8679 (save-excursion
8680 (goto-char old-pos)
8681 (looking-at "if\\>[^_]")))
8682 ;; Special case to avoid deeper and deeper indentation
8683 ;; of "else if" clauses.
8686 ((and (not stop-at-boi-only)
8687 (/= old-pos old-boi)
8688 (memq step-type '(up previous)))
8689 ;; If stop-at-boi-only is nil, we shouldn't back up
8690 ;; over previous or containing statements to try to
8691 ;; reach boi, so go back to the last position and
8692 ;; exit.
8693 (goto-char old-pos)
8694 (throw 'back-up-block nil))
8697 (if (and (not stop-at-boi-only)
8698 (memq step-type '(up previous beginning)))
8699 ;; If we've moved into another statement then we
8700 ;; should no longer try to stop in the middle of a
8701 ;; line.
8702 (setq stop-at-boi-only t))
8704 ;; Record this as a substatement if we skipped up one
8705 ;; level.
8706 (when (eq step-type 'up)
8707 (c-add-syntax 'substatement nil))))
8710 containing-sexp)
8712 ;; Now we have to go out of this block.
8713 (goto-char containing-sexp)
8715 ;; Don't stop in the middle of a special brace list opener
8716 ;; like "({".
8717 (when c-special-brace-lists
8718 (let ((special-list (c-looking-at-special-brace-list)))
8719 (when (and special-list
8720 (< (car (car special-list)) (point)))
8721 (setq containing-sexp (car (car special-list)))
8722 (goto-char containing-sexp))))
8724 (setq paren-state (c-whack-state-after containing-sexp paren-state)
8725 containing-sexp (c-most-enclosing-brace paren-state)
8726 boi (c-point 'boi))
8728 ;; Analyze the construct in front of the block we've stepped out
8729 ;; from and add the right syntactic element for it.
8730 (let ((paren-pos (point))
8731 (paren-char (char-after))
8732 step-type)
8734 (if (eq paren-char ?\()
8735 ;; Stepped out of a parenthesis block, so we're in an
8736 ;; expression now.
8737 (progn
8738 (when (/= paren-pos boi)
8739 (if (and c-recognize-paren-inexpr-blocks
8740 (progn
8741 (c-backward-syntactic-ws containing-sexp)
8742 (or (not (looking-at "\\>"))
8743 (not (c-on-identifier))))
8744 (save-excursion
8745 (goto-char (1+ paren-pos))
8746 (c-forward-syntactic-ws)
8747 (eq (char-after) ?{)))
8748 ;; Stepped out of an in-expression statement. This
8749 ;; syntactic element won't get an anchor pos.
8750 (c-add-syntax 'inexpr-statement)
8752 ;; A parenthesis normally belongs to an arglist.
8753 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
8755 (goto-char (max boi
8756 (if containing-sexp
8757 (1+ containing-sexp)
8758 (point-min))))
8759 (setq step-type 'same
8760 on-label nil))
8762 ;; Stepped out of a brace block.
8763 (setq step-type (c-beginning-of-statement-1 containing-sexp)
8764 on-label (eq step-type 'label))
8766 (if (and (eq step-type 'same)
8767 (/= paren-pos (point)))
8768 (let (inexpr)
8769 (cond
8770 ((save-excursion
8771 (goto-char paren-pos)
8772 (setq inexpr (c-looking-at-inexpr-block
8773 (c-safe-position containing-sexp paren-state)
8774 containing-sexp)))
8775 (c-add-syntax (if (eq (car inexpr) 'inlambda)
8776 'defun-block-intro
8777 'statement-block-intro)
8778 nil))
8779 ((looking-at c-other-decl-block-key)
8780 (c-add-syntax
8781 (cdr (assoc (match-string 1)
8782 c-other-decl-block-key-in-symbols-alist))
8783 (max (c-point 'boi paren-pos) (point))))
8784 (t (c-add-syntax 'defun-block-intro nil))))
8786 (c-add-syntax 'statement-block-intro nil)))
8788 (if (= paren-pos boi)
8789 ;; Always done if the open brace was at boi. The
8790 ;; c-beginning-of-statement-1 call above is necessary
8791 ;; anyway, to decide the type of block-intro to add.
8792 (goto-char paren-pos)
8793 (setq boi (c-point 'boi)))
8796 ;; Fill in the current point as the anchor for all the symbols
8797 ;; added above.
8798 (let ((p c-syntactic-context) q)
8799 (while (not (eq p syntax-last))
8800 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8801 (while q
8802 (unless (car q)
8803 (setcar q (point)))
8804 (setq q (cdr q)))
8805 (setq p (cdr p))))
8808 (defun c-add-class-syntax (symbol
8809 containing-decl-open
8810 containing-decl-start
8811 containing-decl-kwd
8812 paren-state)
8813 ;; The inclass and class-close syntactic symbols are added in
8814 ;; several places and some work is needed to fix everything.
8815 ;; Therefore it's collected here.
8817 ;; This function might do hidden buffer changes.
8818 (goto-char containing-decl-open)
8819 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
8820 (progn
8821 (c-add-syntax symbol containing-decl-open)
8822 containing-decl-open)
8823 (goto-char containing-decl-start)
8824 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8825 ;; here, but we have to do like this for compatibility.
8826 (back-to-indentation)
8827 (c-add-syntax symbol (point))
8828 (if (and (c-keyword-member containing-decl-kwd
8829 'c-inexpr-class-kwds)
8830 (/= containing-decl-start (c-point 'boi containing-decl-start)))
8831 (c-add-syntax 'inexpr-class))
8832 (point)))
8834 (defun c-guess-continued-construct (indent-point
8835 char-after-ip
8836 beg-of-same-or-containing-stmt
8837 containing-sexp
8838 paren-state)
8839 ;; This function contains the decision tree reached through both
8840 ;; cases 18 and 10. It's a continued statement or top level
8841 ;; construct of some kind.
8843 ;; This function might do hidden buffer changes.
8845 (let (special-brace-list placeholder)
8846 (goto-char indent-point)
8847 (skip-chars-forward " \t")
8849 (cond
8850 ;; (CASE A removed.)
8851 ;; CASE B: open braces for class or brace-lists
8852 ((setq special-brace-list
8853 (or (and c-special-brace-lists
8854 (c-looking-at-special-brace-list))
8855 (eq char-after-ip ?{)))
8857 (cond
8858 ;; CASE B.1: class-open
8859 ((save-excursion
8860 (and (eq (char-after) ?{)
8861 (c-looking-at-decl-block containing-sexp t)
8862 (setq beg-of-same-or-containing-stmt (point))))
8863 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
8865 ;; CASE B.2: brace-list-open
8866 ((or (consp special-brace-list)
8867 (save-excursion
8868 (goto-char beg-of-same-or-containing-stmt)
8869 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8870 indent-point t t t)))
8871 ;; The most semantically accurate symbol here is
8872 ;; brace-list-open, but we normally report it simply as a
8873 ;; statement-cont. The reason is that one normally adjusts
8874 ;; brace-list-open for brace lists as top-level constructs,
8875 ;; and brace lists inside statements is a completely different
8876 ;; context. C.f. case 5A.3.
8877 (c-beginning-of-statement-1 containing-sexp)
8878 (c-add-stmt-syntax (if c-auto-newline-analysis
8879 ;; Turn off the dwim above when we're
8880 ;; analyzing the nature of the brace
8881 ;; for the auto newline feature.
8882 'brace-list-open
8883 'statement-cont)
8884 nil nil
8885 containing-sexp paren-state))
8887 ;; CASE B.3: The body of a function declared inside a normal
8888 ;; block. Can occur e.g. in Pike and when using gcc
8889 ;; extensions, but watch out for macros followed by blocks.
8890 ;; C.f. cases E, 16F and 17G.
8891 ((and (not (c-at-statement-start-p))
8892 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8893 'same)
8894 (save-excursion
8895 (let ((c-recognize-typeless-decls nil))
8896 ;; Turn off recognition of constructs that lacks a
8897 ;; type in this case, since that's more likely to be
8898 ;; a macro followed by a block.
8899 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8900 (c-add-stmt-syntax 'defun-open nil t
8901 containing-sexp paren-state))
8903 ;; CASE B.4: Continued statement with block open. The most
8904 ;; accurate analysis is perhaps `statement-cont' together with
8905 ;; `block-open' but we play DWIM and use `substatement-open'
8906 ;; instead. The rationale is that this typically is a macro
8907 ;; followed by a block which makes it very similar to a
8908 ;; statement with a substatement block.
8910 (c-add-stmt-syntax 'substatement-open nil nil
8911 containing-sexp paren-state))
8914 ;; CASE C: iostream insertion or extraction operator
8915 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8916 (save-excursion
8917 (goto-char beg-of-same-or-containing-stmt)
8918 ;; If there is no preceding streamop in the statement
8919 ;; then indent this line as a normal statement-cont.
8920 (when (c-syntactic-re-search-forward
8921 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
8922 (c-add-syntax 'stream-op (c-point 'boi))
8923 t))))
8925 ;; CASE E: In the "K&R region" of a function declared inside a
8926 ;; normal block. C.f. case B.3.
8927 ((and (save-excursion
8928 ;; Check that the next token is a '{'. This works as
8929 ;; long as no language that allows nested function
8930 ;; definitions allows stuff like member init lists, K&R
8931 ;; declarations or throws clauses there.
8933 ;; Note that we do a forward search for something ahead
8934 ;; of the indentation line here. That's not good since
8935 ;; the user might not have typed it yet. Unfortunately
8936 ;; it's exceedingly tricky to recognize a function
8937 ;; prototype in a code block without resorting to this.
8938 (c-forward-syntactic-ws)
8939 (eq (char-after) ?{))
8940 (not (c-at-statement-start-p))
8941 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8942 'same)
8943 (save-excursion
8944 (let ((c-recognize-typeless-decls nil))
8945 ;; Turn off recognition of constructs that lacks a
8946 ;; type in this case, since that's more likely to be
8947 ;; a macro followed by a block.
8948 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8949 (c-add-stmt-syntax 'func-decl-cont nil t
8950 containing-sexp paren-state))
8952 ;;CASE F: continued statement and the only preceding items are
8953 ;;annotations.
8954 ((and (c-major-mode-is 'java-mode)
8955 (setq placeholder (point))
8956 (c-beginning-of-statement-1)
8957 (progn
8958 (while (and (c-forward-annotation)
8959 (< (point) placeholder))
8960 (c-forward-syntactic-ws))
8962 (prog1
8963 (>= (point) placeholder)
8964 (goto-char placeholder)))
8965 (c-beginning-of-statement-1 containing-sexp)
8966 (c-add-syntax 'annotation-var-cont (point)))
8968 ;; CASE G: a template list continuation?
8969 ;; Mostly a duplication of case 5D.3 to fix templates-19:
8970 ((and (c-major-mode-is 'c++-mode)
8971 (save-excursion
8972 (goto-char indent-point)
8973 (c-with-syntax-table c++-template-syntax-table
8974 (setq placeholder (c-up-list-backward)))
8975 (and placeholder
8976 (eq (char-after placeholder) ?<)
8977 (/= (char-before placeholder) ?<)
8978 (progn
8979 (goto-char (1+ placeholder))
8980 (not (looking-at c-<-op-cont-regexp))))))
8981 (c-with-syntax-table c++-template-syntax-table
8982 (goto-char placeholder)
8983 (c-beginning-of-statement-1 containing-sexp t)
8984 (if (save-excursion
8985 (c-backward-syntactic-ws containing-sexp)
8986 (eq (char-before) ?<))
8987 ;; In a nested template arglist.
8988 (progn
8989 (goto-char placeholder)
8990 (c-syntactic-skip-backward "^,;" containing-sexp t)
8991 (c-forward-syntactic-ws))
8992 (back-to-indentation)))
8993 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8994 ;; template aware.
8995 (c-add-syntax 'template-args-cont (point) placeholder))
8997 ;; CASE D: continued statement.
8999 (c-beginning-of-statement-1 containing-sexp)
9000 (c-add-stmt-syntax 'statement-cont nil nil
9001 containing-sexp paren-state))
9004 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
9005 ;; 2005/11/29).
9006 ;;;###autoload
9007 (defun c-guess-basic-syntax ()
9008 "Return the syntactic context of the current line."
9009 (save-excursion
9010 (beginning-of-line)
9011 (c-save-buffer-state
9012 ((indent-point (point))
9013 (case-fold-search nil)
9014 ;; A whole ugly bunch of various temporary variables. Have
9015 ;; to declare them here since it's not possible to declare
9016 ;; a variable with only the scope of a cond test and the
9017 ;; following result clauses, and most of this function is a
9018 ;; single gigantic cond. :P
9019 literal char-before-ip before-ws-ip char-after-ip macro-start
9020 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
9021 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
9022 containing-<
9023 ;; The following record some positions for the containing
9024 ;; declaration block if we're directly within one:
9025 ;; `containing-decl-open' is the position of the open
9026 ;; brace. `containing-decl-start' is the start of the
9027 ;; declaration. `containing-decl-kwd' is the keyword
9028 ;; symbol of the keyword that tells what kind of block it
9029 ;; is.
9030 containing-decl-open
9031 containing-decl-start
9032 containing-decl-kwd
9033 ;; The open paren of the closest surrounding sexp or nil if
9034 ;; there is none.
9035 containing-sexp
9036 ;; The position after the closest preceding brace sexp
9037 ;; (nested sexps are ignored), or the position after
9038 ;; `containing-sexp' if there is none, or (point-min) if
9039 ;; `containing-sexp' is nil.
9041 ;; The paren state outside `containing-sexp', or at
9042 ;; `indent-point' if `containing-sexp' is nil.
9043 (paren-state (c-parse-state))
9044 ;; There's always at most one syntactic element which got
9045 ;; an anchor pos. It's stored in syntactic-relpos.
9046 syntactic-relpos
9047 (c-stmt-delim-chars c-stmt-delim-chars))
9049 ;; Check if we're directly inside an enclosing declaration
9050 ;; level block.
9051 (when (and (setq containing-sexp
9052 (c-most-enclosing-brace paren-state))
9053 (progn
9054 (goto-char containing-sexp)
9055 (eq (char-after) ?{))
9056 (setq placeholder
9057 (c-looking-at-decl-block
9058 (c-most-enclosing-brace paren-state
9059 containing-sexp)
9060 t)))
9061 (setq containing-decl-open containing-sexp
9062 containing-decl-start (point)
9063 containing-sexp nil)
9064 (goto-char placeholder)
9065 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
9066 (c-keyword-sym (match-string 1)))))
9068 ;; Init some position variables.
9069 (if c-state-cache
9070 (progn
9071 (setq containing-sexp (car paren-state)
9072 paren-state (cdr paren-state))
9073 (if (consp containing-sexp)
9074 (progn
9075 (setq lim (cdr containing-sexp))
9076 (if (cdr c-state-cache)
9077 ;; Ignore balanced paren. The next entry
9078 ;; can't be another one.
9079 (setq containing-sexp (car (cdr c-state-cache))
9080 paren-state (cdr paren-state))
9081 ;; If there is no surrounding open paren then
9082 ;; put the last balanced pair back on paren-state.
9083 (setq paren-state (cons containing-sexp paren-state)
9084 containing-sexp nil)))
9085 (setq lim (1+ containing-sexp))))
9086 (setq lim (point-min)))
9088 ;; If we're in a parenthesis list then ',' delimits the
9089 ;; "statements" rather than being an operator (with the
9090 ;; exception of the "for" clause). This difference is
9091 ;; typically only noticeable when statements are used in macro
9092 ;; arglists.
9093 (when (and containing-sexp
9094 (eq (char-after containing-sexp) ?\())
9095 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
9096 ;; cache char before and after indent point, and move point to
9097 ;; the most likely position to perform the majority of tests
9098 (goto-char indent-point)
9099 (c-backward-syntactic-ws lim)
9100 (setq before-ws-ip (point)
9101 char-before-ip (char-before))
9102 (goto-char indent-point)
9103 (skip-chars-forward " \t")
9104 (setq char-after-ip (char-after))
9106 ;; are we in a literal?
9107 (setq literal (c-in-literal lim))
9109 ;; now figure out syntactic qualities of the current line
9110 (cond
9112 ;; CASE 1: in a string.
9113 ((eq literal 'string)
9114 (c-add-syntax 'string (c-point 'bopl)))
9116 ;; CASE 2: in a C or C++ style comment.
9117 ((and (memq literal '(c c++))
9118 ;; This is a kludge for XEmacs where we use
9119 ;; `buffer-syntactic-context', which doesn't correctly
9120 ;; recognize "\*/" to end a block comment.
9121 ;; `parse-partial-sexp' which is used by
9122 ;; `c-literal-limits' will however do that in most
9123 ;; versions, which results in that we get nil from
9124 ;; `c-literal-limits' even when `c-in-literal' claims
9125 ;; we're inside a comment.
9126 (setq placeholder (c-literal-limits lim)))
9127 (c-add-syntax literal (car placeholder)))
9129 ;; CASE 3: in a cpp preprocessor macro continuation.
9130 ((and (save-excursion
9131 (when (c-beginning-of-macro)
9132 (setq macro-start (point))))
9133 (/= macro-start (c-point 'boi))
9134 (progn
9135 (setq tmpsymbol 'cpp-macro-cont)
9136 (or (not c-syntactic-indentation-in-macros)
9137 (save-excursion
9138 (goto-char macro-start)
9139 ;; If at the beginning of the body of a #define
9140 ;; directive then analyze as cpp-define-intro
9141 ;; only. Go on with the syntactic analysis
9142 ;; otherwise. in-macro-expr is set if we're in a
9143 ;; cpp expression, i.e. before the #define body
9144 ;; or anywhere in a non-#define directive.
9145 (if (c-forward-to-cpp-define-body)
9146 (let ((indent-boi (c-point 'boi indent-point)))
9147 (setq in-macro-expr (> (point) indent-boi)
9148 tmpsymbol 'cpp-define-intro)
9149 (= (point) indent-boi))
9150 (setq in-macro-expr t)
9151 nil)))))
9152 (c-add-syntax tmpsymbol macro-start)
9153 (setq macro-start nil))
9155 ;; CASE 11: an else clause?
9156 ((looking-at "else\\>[^_]")
9157 (c-beginning-of-statement-1 containing-sexp)
9158 (c-add-stmt-syntax 'else-clause nil t
9159 containing-sexp paren-state))
9161 ;; CASE 12: while closure of a do/while construct?
9162 ((and (looking-at "while\\>[^_]")
9163 (save-excursion
9164 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
9165 'beginning)
9166 (setq placeholder (point)))))
9167 (goto-char placeholder)
9168 (c-add-stmt-syntax 'do-while-closure nil t
9169 containing-sexp paren-state))
9171 ;; CASE 13: A catch or finally clause? This case is simpler
9172 ;; than if-else and do-while, because a block is required
9173 ;; after every try, catch and finally.
9174 ((save-excursion
9175 (and (cond ((c-major-mode-is 'c++-mode)
9176 (looking-at "catch\\>[^_]"))
9177 ((c-major-mode-is 'java-mode)
9178 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
9179 (and (c-safe (c-backward-syntactic-ws)
9180 (c-backward-sexp)
9182 (eq (char-after) ?{)
9183 (c-safe (c-backward-syntactic-ws)
9184 (c-backward-sexp)
9186 (if (eq (char-after) ?\()
9187 (c-safe (c-backward-sexp) t)
9189 (looking-at "\\(try\\|catch\\)\\>[^_]")
9190 (setq placeholder (point))))
9191 (goto-char placeholder)
9192 (c-add-stmt-syntax 'catch-clause nil t
9193 containing-sexp paren-state))
9195 ;; CASE 18: A substatement we can recognize by keyword.
9196 ((save-excursion
9197 (and c-opt-block-stmt-key
9198 (not (eq char-before-ip ?\;))
9199 (not (c-at-vsemi-p before-ws-ip))
9200 (not (memq char-after-ip '(?\) ?\] ?,)))
9201 (or (not (eq char-before-ip ?}))
9202 (c-looking-at-inexpr-block-backward c-state-cache))
9203 (> (point)
9204 (progn
9205 ;; Ought to cache the result from the
9206 ;; c-beginning-of-statement-1 calls here.
9207 (setq placeholder (point))
9208 (while (eq (setq step-type
9209 (c-beginning-of-statement-1 lim))
9210 'label))
9211 (if (eq step-type 'previous)
9212 (goto-char placeholder)
9213 (setq placeholder (point))
9214 (if (and (eq step-type 'same)
9215 (not (looking-at c-opt-block-stmt-key)))
9216 ;; Step up to the containing statement if we
9217 ;; stayed in the same one.
9218 (let (step)
9219 (while (eq
9220 (setq step
9221 (c-beginning-of-statement-1 lim))
9222 'label))
9223 (if (eq step 'up)
9224 (setq placeholder (point))
9225 ;; There was no containing statement after all.
9226 (goto-char placeholder)))))
9227 placeholder))
9228 (if (looking-at c-block-stmt-2-key)
9229 ;; Require a parenthesis after these keywords.
9230 ;; Necessary to catch e.g. synchronized in Java,
9231 ;; which can be used both as statement and
9232 ;; modifier.
9233 (and (zerop (c-forward-token-2 1 nil))
9234 (eq (char-after) ?\())
9235 (looking-at c-opt-block-stmt-key))))
9237 (if (eq step-type 'up)
9238 ;; CASE 18A: Simple substatement.
9239 (progn
9240 (goto-char placeholder)
9241 (cond
9242 ((eq char-after-ip ?{)
9243 (c-add-stmt-syntax 'substatement-open nil nil
9244 containing-sexp paren-state))
9245 ((save-excursion
9246 (goto-char indent-point)
9247 (back-to-indentation)
9248 (c-forward-label))
9249 (c-add-stmt-syntax 'substatement-label nil nil
9250 containing-sexp paren-state))
9252 (c-add-stmt-syntax 'substatement nil nil
9253 containing-sexp paren-state))))
9255 ;; CASE 18B: Some other substatement. This is shared
9256 ;; with case 10.
9257 (c-guess-continued-construct indent-point
9258 char-after-ip
9259 placeholder
9261 paren-state)))
9263 ;; CASE 14: A case or default label
9264 ((looking-at c-label-kwds-regexp)
9265 (if containing-sexp
9266 (progn
9267 (goto-char containing-sexp)
9268 (setq lim (c-most-enclosing-brace c-state-cache
9269 containing-sexp))
9270 (c-backward-to-block-anchor lim)
9271 (c-add-stmt-syntax 'case-label nil t lim paren-state))
9272 ;; Got a bogus label at the top level. In lack of better
9273 ;; alternatives, anchor it on (point-min).
9274 (c-add-syntax 'case-label (point-min))))
9276 ;; CASE 15: any other label
9277 ((save-excursion
9278 (back-to-indentation)
9279 (and (not (looking-at c-syntactic-ws-start))
9280 (c-forward-label)))
9281 (cond (containing-decl-open
9282 (setq placeholder (c-add-class-syntax 'inclass
9283 containing-decl-open
9284 containing-decl-start
9285 containing-decl-kwd
9286 paren-state))
9287 ;; Append access-label with the same anchor point as
9288 ;; inclass gets.
9289 (c-append-syntax 'access-label placeholder))
9291 (containing-sexp
9292 (goto-char containing-sexp)
9293 (setq lim (c-most-enclosing-brace c-state-cache
9294 containing-sexp))
9295 (save-excursion
9296 (setq tmpsymbol
9297 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
9298 (looking-at "switch\\>[^_]"))
9299 ;; If the surrounding statement is a switch then
9300 ;; let's analyze all labels as switch labels, so
9301 ;; that they get lined up consistently.
9302 'case-label
9303 'label)))
9304 (c-backward-to-block-anchor lim)
9305 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
9308 ;; A label on the top level. Treat it as a class
9309 ;; context. (point-min) is the closest we get to the
9310 ;; class open brace.
9311 (c-add-syntax 'access-label (point-min)))))
9313 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9314 ;; 17E.
9315 ((setq placeholder (c-looking-at-inexpr-block
9316 (c-safe-position containing-sexp paren-state)
9317 containing-sexp
9318 ;; Have to turn on the heuristics after
9319 ;; the point even though it doesn't work
9320 ;; very well. C.f. test case class-16.pike.
9322 (setq tmpsymbol (assq (car placeholder)
9323 '((inexpr-class . class-open)
9324 (inexpr-statement . block-open))))
9325 (if tmpsymbol
9326 ;; It's a statement block or an anonymous class.
9327 (setq tmpsymbol (cdr tmpsymbol))
9328 ;; It's a Pike lambda. Check whether we are between the
9329 ;; lambda keyword and the argument list or at the defun
9330 ;; opener.
9331 (setq tmpsymbol (if (eq char-after-ip ?{)
9332 'inline-open
9333 'lambda-intro-cont)))
9334 (goto-char (cdr placeholder))
9335 (back-to-indentation)
9336 (c-add-stmt-syntax tmpsymbol nil t
9337 (c-most-enclosing-brace c-state-cache (point))
9338 paren-state)
9339 (unless (eq (point) (cdr placeholder))
9340 (c-add-syntax (car placeholder))))
9342 ;; CASE 5: Line is inside a declaration level block or at top level.
9343 ((or containing-decl-open (null containing-sexp))
9344 (cond
9346 ;; CASE 5A: we are looking at a defun, brace list, class,
9347 ;; or inline-inclass method opening brace
9348 ((setq special-brace-list
9349 (or (and c-special-brace-lists
9350 (c-looking-at-special-brace-list))
9351 (eq char-after-ip ?{)))
9352 (cond
9354 ;; CASE 5A.1: Non-class declaration block open.
9355 ((save-excursion
9356 (let (tmp)
9357 (and (eq char-after-ip ?{)
9358 (setq tmp (c-looking-at-decl-block containing-sexp t))
9359 (progn
9360 (setq placeholder (point))
9361 (goto-char tmp)
9362 (looking-at c-symbol-key))
9363 (c-keyword-member
9364 (c-keyword-sym (setq keyword (match-string 0)))
9365 'c-other-block-decl-kwds))))
9366 (goto-char placeholder)
9367 (c-add-stmt-syntax
9368 (if (string-equal keyword "extern")
9369 ;; Special case for extern-lang-open.
9370 'extern-lang-open
9371 (intern (concat keyword "-open")))
9372 nil t containing-sexp paren-state))
9374 ;; CASE 5A.2: we are looking at a class opening brace
9375 ((save-excursion
9376 (goto-char indent-point)
9377 (skip-chars-forward " \t")
9378 (and (eq (char-after) ?{)
9379 (c-looking-at-decl-block containing-sexp t)
9380 (setq placeholder (point))))
9381 (c-add-syntax 'class-open placeholder))
9383 ;; CASE 5A.3: brace list open
9384 ((save-excursion
9385 (c-beginning-of-decl-1 lim)
9386 (while (looking-at c-specifier-key)
9387 (goto-char (match-end 1))
9388 (c-forward-syntactic-ws indent-point))
9389 (setq placeholder (c-point 'boi))
9390 (or (consp special-brace-list)
9391 (and (or (save-excursion
9392 (goto-char indent-point)
9393 (setq tmpsymbol nil)
9394 (while (and (> (point) placeholder)
9395 (zerop (c-backward-token-2 1 t))
9396 (/= (char-after) ?=))
9397 (and c-opt-inexpr-brace-list-key
9398 (not tmpsymbol)
9399 (looking-at c-opt-inexpr-brace-list-key)
9400 (setq tmpsymbol 'topmost-intro-cont)))
9401 (eq (char-after) ?=))
9402 (looking-at c-brace-list-key))
9403 (save-excursion
9404 (while (and (< (point) indent-point)
9405 (zerop (c-forward-token-2 1 t))
9406 (not (memq (char-after) '(?\; ?\()))))
9407 (not (memq (char-after) '(?\; ?\()))
9408 ))))
9409 (if (and (not c-auto-newline-analysis)
9410 (c-major-mode-is 'java-mode)
9411 (eq tmpsymbol 'topmost-intro-cont))
9412 ;; We're in Java and have found that the open brace
9413 ;; belongs to a "new Foo[]" initialization list,
9414 ;; which means the brace list is part of an
9415 ;; expression and not a top level definition. We
9416 ;; therefore treat it as any topmost continuation
9417 ;; even though the semantically correct symbol still
9418 ;; is brace-list-open, on the same grounds as in
9419 ;; case B.2.
9420 (progn
9421 (c-beginning-of-statement-1 lim)
9422 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9423 (c-add-syntax 'brace-list-open placeholder)))
9425 ;; CASE 5A.4: inline defun open
9426 ((and containing-decl-open
9427 (not (c-keyword-member containing-decl-kwd
9428 'c-other-block-decl-kwds)))
9429 (c-add-syntax 'inline-open)
9430 (c-add-class-syntax 'inclass
9431 containing-decl-open
9432 containing-decl-start
9433 containing-decl-kwd
9434 paren-state))
9436 ;; CASE 5A.5: ordinary defun open
9438 (save-excursion
9439 (c-beginning-of-decl-1 lim)
9440 (while (looking-at c-specifier-key)
9441 (goto-char (match-end 1))
9442 (c-forward-syntactic-ws indent-point))
9443 (c-add-syntax 'defun-open (c-point 'boi))
9444 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9445 ;; 2007-11-09)
9446 ))))
9448 ;; CASE 5B: After a function header but before the body (or
9449 ;; the ending semicolon if there's no body).
9450 ((save-excursion
9451 (when (setq placeholder (c-just-after-func-arglist-p lim))
9452 (setq tmp-pos (point))))
9453 (cond
9455 ;; CASE 5B.1: Member init list.
9456 ((eq (char-after tmp-pos) ?:)
9457 (if (or (>= tmp-pos indent-point)
9458 (= (c-point 'bosws) (1+ tmp-pos)))
9459 (progn
9460 ;; There is no preceding member init clause.
9461 ;; Indent relative to the beginning of indentation
9462 ;; for the topmost-intro line that contains the
9463 ;; prototype's open paren.
9464 (goto-char placeholder)
9465 (c-add-syntax 'member-init-intro (c-point 'boi)))
9466 ;; Indent relative to the first member init clause.
9467 (goto-char (1+ tmp-pos))
9468 (c-forward-syntactic-ws)
9469 (c-add-syntax 'member-init-cont (point))))
9471 ;; CASE 5B.2: K&R arg decl intro
9472 ((and c-recognize-knr-p
9473 (c-in-knr-argdecl lim))
9474 (c-beginning-of-statement-1 lim)
9475 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9476 (if containing-decl-open
9477 (c-add-class-syntax 'inclass
9478 containing-decl-open
9479 containing-decl-start
9480 containing-decl-kwd
9481 paren-state)))
9483 ;; CASE 5B.4: Nether region after a C++ or Java func
9484 ;; decl, which could include a `throws' declaration.
9486 (c-beginning-of-statement-1 lim)
9487 (c-add-syntax 'func-decl-cont (c-point 'boi))
9490 ;; CASE 5C: inheritance line. could be first inheritance
9491 ;; line, or continuation of a multiple inheritance
9492 ((or (and (c-major-mode-is 'c++-mode)
9493 (progn
9494 (when (eq char-after-ip ?,)
9495 (skip-chars-forward " \t")
9496 (forward-char))
9497 (looking-at c-opt-postfix-decl-spec-key)))
9498 (and (or (eq char-before-ip ?:)
9499 ;; watch out for scope operator
9500 (save-excursion
9501 (and (eq char-after-ip ?:)
9502 (c-safe (forward-char 1) t)
9503 (not (eq (char-after) ?:))
9505 (save-excursion
9506 (c-backward-syntactic-ws lim)
9507 (if (eq char-before-ip ?:)
9508 (progn
9509 (forward-char -1)
9510 (c-backward-syntactic-ws lim)))
9511 (back-to-indentation)
9512 (looking-at c-class-key)))
9513 ;; for Java
9514 (and (c-major-mode-is 'java-mode)
9515 (let ((fence (save-excursion
9516 (c-beginning-of-statement-1 lim)
9517 (point)))
9518 cont done)
9519 (save-excursion
9520 (while (not done)
9521 (cond ((looking-at c-opt-postfix-decl-spec-key)
9522 (setq injava-inher (cons cont (point))
9523 done t))
9524 ((or (not (c-safe (c-forward-sexp -1) t))
9525 (<= (point) fence))
9526 (setq done t))
9528 (setq cont t)))
9529 injava-inher)
9530 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9531 (point)))
9533 (cond
9535 ;; CASE 5C.1: non-hanging colon on an inher intro
9536 ((eq char-after-ip ?:)
9537 (c-beginning-of-statement-1 lim)
9538 (c-add-syntax 'inher-intro (c-point 'boi))
9539 ;; don't add inclass symbol since relative point already
9540 ;; contains any class offset
9543 ;; CASE 5C.2: hanging colon on an inher intro
9544 ((eq char-before-ip ?:)
9545 (c-beginning-of-statement-1 lim)
9546 (c-add-syntax 'inher-intro (c-point 'boi))
9547 (if containing-decl-open
9548 (c-add-class-syntax 'inclass
9549 containing-decl-open
9550 containing-decl-start
9551 containing-decl-kwd
9552 paren-state)))
9554 ;; CASE 5C.3: in a Java implements/extends
9555 (injava-inher
9556 (let ((where (cdr injava-inher))
9557 (cont (car injava-inher)))
9558 (goto-char where)
9559 (cond ((looking-at "throws\\>[^_]")
9560 (c-add-syntax 'func-decl-cont
9561 (progn (c-beginning-of-statement-1 lim)
9562 (c-point 'boi))))
9563 (cont (c-add-syntax 'inher-cont where))
9564 (t (c-add-syntax 'inher-intro
9565 (progn (goto-char (cdr injava-inher))
9566 (c-beginning-of-statement-1 lim)
9567 (point))))
9570 ;; CASE 5C.4: a continued inheritance line
9572 (c-beginning-of-inheritance-list lim)
9573 (c-add-syntax 'inher-cont (point))
9574 ;; don't add inclass symbol since relative point already
9575 ;; contains any class offset
9578 ;; CASE 5D: this could be a top-level initialization, a
9579 ;; member init list continuation, or a template argument
9580 ;; list continuation.
9581 ((save-excursion
9582 ;; Note: We use the fact that lim is always after any
9583 ;; preceding brace sexp.
9584 (if c-recognize-<>-arglists
9585 (while (and
9586 (progn
9587 (c-syntactic-skip-backward "^;,=<>" lim t)
9588 (> (point) lim))
9590 (when c-overloadable-operators-regexp
9591 (when (setq placeholder (c-after-special-operator-id lim))
9592 (goto-char placeholder)
9594 (cond
9595 ((eq (char-before) ?>)
9596 (or (c-backward-<>-arglist nil lim)
9597 (backward-char))
9599 ((eq (char-before) ?<)
9600 (backward-char)
9601 (if (save-excursion
9602 (c-forward-<>-arglist nil))
9603 (progn (forward-char)
9604 nil)
9606 (t nil)))))
9607 ;; NB: No c-after-special-operator-id stuff in this
9608 ;; clause - we assume only C++ needs it.
9609 (c-syntactic-skip-backward "^;,=" lim t))
9610 (memq (char-before) '(?, ?= ?<)))
9611 (cond
9613 ;; CASE 5D.3: perhaps a template list continuation?
9614 ((and (c-major-mode-is 'c++-mode)
9615 (save-excursion
9616 (save-restriction
9617 (c-with-syntax-table c++-template-syntax-table
9618 (goto-char indent-point)
9619 (setq placeholder (c-up-list-backward))
9620 (and placeholder
9621 (eq (char-after placeholder) ?<))))))
9622 (c-with-syntax-table c++-template-syntax-table
9623 (goto-char placeholder)
9624 (c-beginning-of-statement-1 lim t)
9625 (if (save-excursion
9626 (c-backward-syntactic-ws lim)
9627 (eq (char-before) ?<))
9628 ;; In a nested template arglist.
9629 (progn
9630 (goto-char placeholder)
9631 (c-syntactic-skip-backward "^,;" lim t)
9632 (c-forward-syntactic-ws))
9633 (back-to-indentation)))
9634 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9635 ;; template aware.
9636 (c-add-syntax 'template-args-cont (point) placeholder))
9638 ;; CASE 5D.4: perhaps a multiple inheritance line?
9639 ((and (c-major-mode-is 'c++-mode)
9640 (save-excursion
9641 (c-beginning-of-statement-1 lim)
9642 (setq placeholder (point))
9643 (if (looking-at "static\\>[^_]")
9644 (c-forward-token-2 1 nil indent-point))
9645 (and (looking-at c-class-key)
9646 (zerop (c-forward-token-2 2 nil indent-point))
9647 (if (eq (char-after) ?<)
9648 (c-with-syntax-table c++-template-syntax-table
9649 (zerop (c-forward-token-2 1 t indent-point)))
9651 (eq (char-after) ?:))))
9652 (goto-char placeholder)
9653 (c-add-syntax 'inher-cont (c-point 'boi)))
9655 ;; CASE 5D.5: Continuation of the "expression part" of a
9656 ;; top level construct. Or, perhaps, an unrecognized construct.
9658 (while (and (setq placeholder (point))
9659 (eq (car (c-beginning-of-decl-1 containing-sexp))
9660 'same)
9661 (save-excursion
9662 (c-backward-syntactic-ws)
9663 (eq (char-before) ?}))
9664 (< (point) placeholder)))
9665 (c-add-stmt-syntax
9666 (cond
9667 ((eq (point) placeholder) 'statement) ; unrecognized construct
9668 ;; A preceding comma at the top level means that a
9669 ;; new variable declaration starts here. Use
9670 ;; topmost-intro-cont for it, for consistency with
9671 ;; the first variable declaration. C.f. case 5N.
9672 ((eq char-before-ip ?,) 'topmost-intro-cont)
9673 (t 'statement-cont))
9674 nil nil containing-sexp paren-state))
9677 ;; CASE 5F: Close of a non-class declaration level block.
9678 ((and (eq char-after-ip ?})
9679 (c-keyword-member containing-decl-kwd
9680 'c-other-block-decl-kwds))
9681 ;; This is inconsistent: Should use `containing-decl-open'
9682 ;; here if it's at boi, like in case 5J.
9683 (goto-char containing-decl-start)
9684 (c-add-stmt-syntax
9685 (if (string-equal (symbol-name containing-decl-kwd) "extern")
9686 ;; Special case for compatibility with the
9687 ;; extern-lang syntactic symbols.
9688 'extern-lang-close
9689 (intern (concat (symbol-name containing-decl-kwd)
9690 "-close")))
9691 nil t
9692 (c-most-enclosing-brace paren-state (point))
9693 paren-state))
9695 ;; CASE 5G: we are looking at the brace which closes the
9696 ;; enclosing nested class decl
9697 ((and containing-sexp
9698 (eq char-after-ip ?})
9699 (eq containing-decl-open containing-sexp))
9700 (c-add-class-syntax 'class-close
9701 containing-decl-open
9702 containing-decl-start
9703 containing-decl-kwd
9704 paren-state))
9706 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9707 ((and c-recognize-knr-p
9708 (not containing-sexp) ; can't be knr inside braces.
9709 (not (eq char-before-ip ?}))
9710 (save-excursion
9711 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
9712 (and placeholder
9713 ;; Do an extra check to avoid tripping up on
9714 ;; statements that occur in invalid contexts
9715 ;; (e.g. in macro bodies where we don't really
9716 ;; know the context of what we're looking at).
9717 (not (and c-opt-block-stmt-key
9718 (looking-at c-opt-block-stmt-key)))))
9719 (< placeholder indent-point))
9720 (goto-char placeholder)
9721 (c-add-syntax 'knr-argdecl (point)))
9723 ;; CASE 5I: ObjC method definition.
9724 ((and c-opt-method-key
9725 (looking-at c-opt-method-key))
9726 (c-beginning-of-statement-1 nil t)
9727 (if (= (point) indent-point)
9728 ;; Handle the case when it's the first (non-comment)
9729 ;; thing in the buffer. Can't look for a 'same return
9730 ;; value from cbos1 since ObjC directives currently
9731 ;; aren't recognized fully, so that we get 'same
9732 ;; instead of 'previous if it moved over a preceding
9733 ;; directive.
9734 (goto-char (point-min)))
9735 (c-add-syntax 'objc-method-intro (c-point 'boi)))
9737 ;; CASE 5P: AWK pattern or function or continuation
9738 ;; thereof.
9739 ((c-major-mode-is 'awk-mode)
9740 (setq placeholder (point))
9741 (c-add-stmt-syntax
9742 (if (and (eq (c-beginning-of-statement-1) 'same)
9743 (/= (point) placeholder))
9744 'topmost-intro-cont
9745 'topmost-intro)
9746 nil nil
9747 containing-sexp paren-state))
9749 ;; CASE 5N: At a variable declaration that follows a class
9750 ;; definition or some other block declaration that doesn't
9751 ;; end at the closing '}'. C.f. case 5D.5.
9752 ((progn
9753 (c-backward-syntactic-ws lim)
9754 (and (eq (char-before) ?})
9755 (save-excursion
9756 (let ((start (point)))
9757 (if (and c-state-cache
9758 (consp (car c-state-cache))
9759 (eq (cdar c-state-cache) (point)))
9760 ;; Speed up the backward search a bit.
9761 (goto-char (caar c-state-cache)))
9762 (c-beginning-of-decl-1 containing-sexp)
9763 (setq placeholder (point))
9764 (if (= start (point))
9765 ;; The '}' is unbalanced.
9767 (c-end-of-decl-1)
9768 (>= (point) indent-point))))))
9769 (goto-char placeholder)
9770 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9771 containing-sexp paren-state))
9773 ;; NOTE: The point is at the end of the previous token here.
9775 ;; CASE 5J: we are at the topmost level, make
9776 ;; sure we skip back past any access specifiers
9777 ((and
9778 ;; A macro continuation line is never at top level.
9779 (not (and macro-start
9780 (> indent-point macro-start)))
9781 (save-excursion
9782 (setq placeholder (point))
9783 (or (memq char-before-ip '(?\; ?{ ?} nil))
9784 (c-at-vsemi-p before-ws-ip)
9785 (when (and (eq char-before-ip ?:)
9786 (eq (c-beginning-of-statement-1 lim)
9787 'label))
9788 (c-backward-syntactic-ws lim)
9789 (setq placeholder (point)))
9790 (and (c-major-mode-is 'objc-mode)
9791 (catch 'not-in-directive
9792 (c-beginning-of-statement-1 lim)
9793 (setq placeholder (point))
9794 (while (and (c-forward-objc-directive)
9795 (< (point) indent-point))
9796 (c-forward-syntactic-ws)
9797 (if (>= (point) indent-point)
9798 (throw 'not-in-directive t))
9799 (setq placeholder (point)))
9800 nil)))))
9801 ;; For historic reasons we anchor at bol of the last
9802 ;; line of the previous declaration. That's clearly
9803 ;; highly bogus and useless, and it makes our lives hard
9804 ;; to remain compatible. :P
9805 (goto-char placeholder)
9806 (c-add-syntax 'topmost-intro (c-point 'bol))
9807 (if containing-decl-open
9808 (if (c-keyword-member containing-decl-kwd
9809 'c-other-block-decl-kwds)
9810 (progn
9811 (goto-char (c-brace-anchor-point containing-decl-open))
9812 (c-add-stmt-syntax
9813 (if (string-equal (symbol-name containing-decl-kwd)
9814 "extern")
9815 ;; Special case for compatibility with the
9816 ;; extern-lang syntactic symbols.
9817 'inextern-lang
9818 (intern (concat "in"
9819 (symbol-name containing-decl-kwd))))
9820 nil t
9821 (c-most-enclosing-brace paren-state (point))
9822 paren-state))
9823 (c-add-class-syntax 'inclass
9824 containing-decl-open
9825 containing-decl-start
9826 containing-decl-kwd
9827 paren-state)))
9828 (when (and c-syntactic-indentation-in-macros
9829 macro-start
9830 (/= macro-start (c-point 'boi indent-point)))
9831 (c-add-syntax 'cpp-define-intro)
9832 (setq macro-start nil)))
9834 ;; CASE 5K: we are at an ObjC method definition
9835 ;; continuation line.
9836 ((and c-opt-method-key
9837 (save-excursion
9838 (c-beginning-of-statement-1 lim)
9839 (beginning-of-line)
9840 (when (looking-at c-opt-method-key)
9841 (setq placeholder (point)))))
9842 (c-add-syntax 'objc-method-args-cont placeholder))
9844 ;; CASE 5L: we are at the first argument of a template
9845 ;; arglist that begins on the previous line.
9846 ((and c-recognize-<>-arglists
9847 (eq (char-before) ?<)
9848 (not (and c-overloadable-operators-regexp
9849 (c-after-special-operator-id lim))))
9850 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9851 (c-add-syntax 'template-args-cont (c-point 'boi)))
9853 ;; CASE 5Q: we are at a statement within a macro.
9854 (macro-start
9855 (c-beginning-of-statement-1 containing-sexp)
9856 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
9858 ;;CASE 5N: We are at a topmost continuation line and the only
9859 ;;preceding items are annotations.
9860 ((and (c-major-mode-is 'java-mode)
9861 (setq placeholder (point))
9862 (c-beginning-of-statement-1)
9863 (progn
9864 (while (and (c-forward-annotation))
9865 (c-forward-syntactic-ws))
9867 (prog1
9868 (>= (point) placeholder)
9869 (goto-char placeholder)))
9870 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
9872 ;; CASE 5M: we are at a topmost continuation line
9874 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9875 (when (c-major-mode-is 'objc-mode)
9876 (setq placeholder (point))
9877 (while (and (c-forward-objc-directive)
9878 (< (point) indent-point))
9879 (c-forward-syntactic-ws)
9880 (setq placeholder (point)))
9881 (goto-char placeholder))
9882 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9886 ;; (CASE 6 has been removed.)
9888 ;; CASE 7: line is an expression, not a statement. Most
9889 ;; likely we are either in a function prototype or a function
9890 ;; call argument list
9891 ((not (or (and c-special-brace-lists
9892 (save-excursion
9893 (goto-char containing-sexp)
9894 (c-looking-at-special-brace-list)))
9895 (eq (char-after containing-sexp) ?{)))
9896 (cond
9898 ;; CASE 7A: we are looking at the arglist closing paren.
9899 ;; C.f. case 7F.
9900 ((memq char-after-ip '(?\) ?\]))
9901 (goto-char containing-sexp)
9902 (setq placeholder (c-point 'boi))
9903 (if (and (c-safe (backward-up-list 1) t)
9904 (>= (point) placeholder))
9905 (progn
9906 (forward-char)
9907 (skip-chars-forward " \t"))
9908 (goto-char placeholder))
9909 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
9910 (c-most-enclosing-brace paren-state (point))
9911 paren-state))
9913 ;; CASE 7B: Looking at the opening brace of an
9914 ;; in-expression block or brace list. C.f. cases 4, 16A
9915 ;; and 17E.
9916 ((and (eq char-after-ip ?{)
9917 (progn
9918 (setq placeholder (c-inside-bracelist-p (point)
9919 paren-state))
9920 (if placeholder
9921 (setq tmpsymbol '(brace-list-open . inexpr-class))
9922 (setq tmpsymbol '(block-open . inexpr-statement)
9923 placeholder
9924 (cdr-safe (c-looking-at-inexpr-block
9925 (c-safe-position containing-sexp
9926 paren-state)
9927 containing-sexp)))
9928 ;; placeholder is nil if it's a block directly in
9929 ;; a function arglist. That makes us skip out of
9930 ;; this case.
9932 (goto-char placeholder)
9933 (back-to-indentation)
9934 (c-add-stmt-syntax (car tmpsymbol) nil t
9935 (c-most-enclosing-brace paren-state (point))
9936 paren-state)
9937 (if (/= (point) placeholder)
9938 (c-add-syntax (cdr tmpsymbol))))
9940 ;; CASE 7C: we are looking at the first argument in an empty
9941 ;; argument list. Use arglist-close if we're actually
9942 ;; looking at a close paren or bracket.
9943 ((memq char-before-ip '(?\( ?\[))
9944 (goto-char containing-sexp)
9945 (setq placeholder (c-point 'boi))
9946 (if (and (c-safe (backward-up-list 1) t)
9947 (>= (point) placeholder))
9948 (progn
9949 (forward-char)
9950 (skip-chars-forward " \t"))
9951 (goto-char placeholder))
9952 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
9953 (c-most-enclosing-brace paren-state (point))
9954 paren-state))
9956 ;; CASE 7D: we are inside a conditional test clause. treat
9957 ;; these things as statements
9958 ((progn
9959 (goto-char containing-sexp)
9960 (and (c-safe (c-forward-sexp -1) t)
9961 (looking-at "\\<for\\>[^_]")))
9962 (goto-char (1+ containing-sexp))
9963 (c-forward-syntactic-ws indent-point)
9964 (if (eq char-before-ip ?\;)
9965 (c-add-syntax 'statement (point))
9966 (c-add-syntax 'statement-cont (point))
9969 ;; CASE 7E: maybe a continued ObjC method call. This is the
9970 ;; case when we are inside a [] bracketed exp, and what
9971 ;; precede the opening bracket is not an identifier.
9972 ((and c-opt-method-key
9973 (eq (char-after containing-sexp) ?\[)
9974 (progn
9975 (goto-char (1- containing-sexp))
9976 (c-backward-syntactic-ws (c-point 'bod))
9977 (if (not (looking-at c-symbol-key))
9978 (c-add-syntax 'objc-method-call-cont containing-sexp))
9981 ;; CASE 7F: we are looking at an arglist continuation line,
9982 ;; but the preceding argument is on the same line as the
9983 ;; opening paren. This case includes multi-line
9984 ;; mathematical paren groupings, but we could be on a
9985 ;; for-list continuation line. C.f. case 7A.
9986 ((progn
9987 (goto-char (1+ containing-sexp))
9988 (< (save-excursion
9989 (c-forward-syntactic-ws)
9990 (point))
9991 (c-point 'bonl)))
9992 (goto-char containing-sexp) ; paren opening the arglist
9993 (setq placeholder (c-point 'boi))
9994 (if (and (c-safe (backward-up-list 1) t)
9995 (>= (point) placeholder))
9996 (progn
9997 (forward-char)
9998 (skip-chars-forward " \t"))
9999 (goto-char placeholder))
10000 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
10001 (c-most-enclosing-brace c-state-cache (point))
10002 paren-state))
10004 ;; CASE 7G: we are looking at just a normal arglist
10005 ;; continuation line
10006 (t (c-forward-syntactic-ws indent-point)
10007 (c-add-syntax 'arglist-cont (c-point 'boi)))
10010 ;; CASE 8: func-local multi-inheritance line
10011 ((and (c-major-mode-is 'c++-mode)
10012 (save-excursion
10013 (goto-char indent-point)
10014 (skip-chars-forward " \t")
10015 (looking-at c-opt-postfix-decl-spec-key)))
10016 (goto-char indent-point)
10017 (skip-chars-forward " \t")
10018 (cond
10020 ;; CASE 8A: non-hanging colon on an inher intro
10021 ((eq char-after-ip ?:)
10022 (c-backward-syntactic-ws lim)
10023 (c-add-syntax 'inher-intro (c-point 'boi)))
10025 ;; CASE 8B: hanging colon on an inher intro
10026 ((eq char-before-ip ?:)
10027 (c-add-syntax 'inher-intro (c-point 'boi)))
10029 ;; CASE 8C: a continued inheritance line
10031 (c-beginning-of-inheritance-list lim)
10032 (c-add-syntax 'inher-cont (point))
10035 ;; CASE 9: we are inside a brace-list
10036 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
10037 (setq special-brace-list
10038 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
10039 (save-excursion
10040 (goto-char containing-sexp)
10041 (c-looking-at-special-brace-list)))
10042 (c-inside-bracelist-p containing-sexp paren-state))))
10043 (cond
10045 ;; CASE 9A: In the middle of a special brace list opener.
10046 ((and (consp special-brace-list)
10047 (save-excursion
10048 (goto-char containing-sexp)
10049 (eq (char-after) ?\())
10050 (eq char-after-ip (car (cdr special-brace-list))))
10051 (goto-char (car (car special-brace-list)))
10052 (skip-chars-backward " \t")
10053 (if (and (bolp)
10054 (assoc 'statement-cont
10055 (setq placeholder (c-guess-basic-syntax))))
10056 (setq c-syntactic-context placeholder)
10057 (c-beginning-of-statement-1
10058 (c-safe-position (1- containing-sexp) paren-state))
10059 (c-forward-token-2 0)
10060 (while (looking-at c-specifier-key)
10061 (goto-char (match-end 1))
10062 (c-forward-syntactic-ws))
10063 (c-add-syntax 'brace-list-open (c-point 'boi))))
10065 ;; CASE 9B: brace-list-close brace
10066 ((if (consp special-brace-list)
10067 ;; Check special brace list closer.
10068 (progn
10069 (goto-char (car (car special-brace-list)))
10070 (save-excursion
10071 (goto-char indent-point)
10072 (back-to-indentation)
10074 ;; We were between the special close char and the `)'.
10075 (and (eq (char-after) ?\))
10076 (eq (1+ (point)) (cdr (car special-brace-list))))
10077 ;; We were before the special close char.
10078 (and (eq (char-after) (cdr (cdr special-brace-list)))
10079 (zerop (c-forward-token-2))
10080 (eq (1+ (point)) (cdr (car special-brace-list)))))))
10081 ;; Normal brace list check.
10082 (and (eq char-after-ip ?})
10083 (c-safe (goto-char (c-up-list-backward (point))) t)
10084 (= (point) containing-sexp)))
10085 (if (eq (point) (c-point 'boi))
10086 (c-add-syntax 'brace-list-close (point))
10087 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10088 (c-beginning-of-statement-1 lim)
10089 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
10092 ;; Prepare for the rest of the cases below by going to the
10093 ;; token following the opening brace
10094 (if (consp special-brace-list)
10095 (progn
10096 (goto-char (car (car special-brace-list)))
10097 (c-forward-token-2 1 nil indent-point))
10098 (goto-char containing-sexp))
10099 (forward-char)
10100 (let ((start (point)))
10101 (c-forward-syntactic-ws indent-point)
10102 (goto-char (max start (c-point 'bol))))
10103 (c-skip-ws-forward indent-point)
10104 (cond
10106 ;; CASE 9C: we're looking at the first line in a brace-list
10107 ((= (point) indent-point)
10108 (if (consp special-brace-list)
10109 (goto-char (car (car special-brace-list)))
10110 (goto-char containing-sexp))
10111 (if (eq (point) (c-point 'boi))
10112 (c-add-syntax 'brace-list-intro (point))
10113 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10114 (c-beginning-of-statement-1 lim)
10115 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
10117 ;; CASE 9D: this is just a later brace-list-entry or
10118 ;; brace-entry-open
10119 (t (if (or (eq char-after-ip ?{)
10120 (and c-special-brace-lists
10121 (save-excursion
10122 (goto-char indent-point)
10123 (c-forward-syntactic-ws (c-point 'eol))
10124 (c-looking-at-special-brace-list (point)))))
10125 (c-add-syntax 'brace-entry-open (point))
10126 (c-add-syntax 'brace-list-entry (point))
10128 ))))
10130 ;; CASE 10: A continued statement or top level construct.
10131 ((and (not (memq char-before-ip '(?\; ?:)))
10132 (not (c-at-vsemi-p before-ws-ip))
10133 (or (not (eq char-before-ip ?}))
10134 (c-looking-at-inexpr-block-backward c-state-cache))
10135 (> (point)
10136 (save-excursion
10137 (c-beginning-of-statement-1 containing-sexp)
10138 (setq placeholder (point))))
10139 (/= placeholder containing-sexp))
10140 ;; This is shared with case 18.
10141 (c-guess-continued-construct indent-point
10142 char-after-ip
10143 placeholder
10144 containing-sexp
10145 paren-state))
10147 ;; CASE 16: block close brace, possibly closing the defun or
10148 ;; the class
10149 ((eq char-after-ip ?})
10150 ;; From here on we have the next containing sexp in lim.
10151 (setq lim (c-most-enclosing-brace paren-state))
10152 (goto-char containing-sexp)
10153 (cond
10155 ;; CASE 16E: Closing a statement block? This catches
10156 ;; cases where it's preceded by a statement keyword,
10157 ;; which works even when used in an "invalid" context,
10158 ;; e.g. a macro argument.
10159 ((c-after-conditional)
10160 (c-backward-to-block-anchor lim)
10161 (c-add-stmt-syntax 'block-close nil t lim paren-state))
10163 ;; CASE 16A: closing a lambda defun or an in-expression
10164 ;; block? C.f. cases 4, 7B and 17E.
10165 ((setq placeholder (c-looking-at-inexpr-block
10166 (c-safe-position containing-sexp paren-state)
10167 nil))
10168 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10169 'inline-close
10170 'block-close))
10171 (goto-char containing-sexp)
10172 (back-to-indentation)
10173 (if (= containing-sexp (point))
10174 (c-add-syntax tmpsymbol (point))
10175 (goto-char (cdr placeholder))
10176 (back-to-indentation)
10177 (c-add-stmt-syntax tmpsymbol nil t
10178 (c-most-enclosing-brace paren-state (point))
10179 paren-state)
10180 (if (/= (point) (cdr placeholder))
10181 (c-add-syntax (car placeholder)))))
10183 ;; CASE 16B: does this close an inline or a function in
10184 ;; a non-class declaration level block?
10185 ((save-excursion
10186 (and lim
10187 (progn
10188 (goto-char lim)
10189 (c-looking-at-decl-block
10190 (c-most-enclosing-brace paren-state lim)
10191 nil))
10192 (setq placeholder (point))))
10193 (c-backward-to-decl-anchor lim)
10194 (back-to-indentation)
10195 (if (save-excursion
10196 (goto-char placeholder)
10197 (looking-at c-other-decl-block-key))
10198 (c-add-syntax 'defun-close (point))
10199 (c-add-syntax 'inline-close (point))))
10201 ;; CASE 16F: Can be a defun-close of a function declared
10202 ;; in a statement block, e.g. in Pike or when using gcc
10203 ;; extensions, but watch out for macros followed by
10204 ;; blocks. Let it through to be handled below.
10205 ;; C.f. cases B.3 and 17G.
10206 ((save-excursion
10207 (and (not (c-at-statement-start-p))
10208 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10209 (setq placeholder (point))
10210 (let ((c-recognize-typeless-decls nil))
10211 ;; Turn off recognition of constructs that
10212 ;; lacks a type in this case, since that's more
10213 ;; likely to be a macro followed by a block.
10214 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10215 (back-to-indentation)
10216 (if (/= (point) containing-sexp)
10217 (goto-char placeholder))
10218 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
10220 ;; CASE 16C: If there is an enclosing brace then this is
10221 ;; a block close since defun closes inside declaration
10222 ;; level blocks have been handled above.
10223 (lim
10224 ;; If the block is preceded by a case/switch label on
10225 ;; the same line, we anchor at the first preceding label
10226 ;; at boi. The default handling in c-add-stmt-syntax
10227 ;; really fixes it better, but we do like this to keep
10228 ;; the indentation compatible with version 5.28 and
10229 ;; earlier. C.f. case 17H.
10230 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10231 (eq (c-beginning-of-statement-1 lim) 'label)))
10232 (goto-char placeholder)
10233 (if (looking-at c-label-kwds-regexp)
10234 (c-add-syntax 'block-close (point))
10235 (goto-char containing-sexp)
10236 ;; c-backward-to-block-anchor not necessary here; those
10237 ;; situations are handled in case 16E above.
10238 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
10240 ;; CASE 16D: Only top level defun close left.
10242 (goto-char containing-sexp)
10243 (c-backward-to-decl-anchor lim)
10244 (c-add-stmt-syntax 'defun-close nil nil
10245 (c-most-enclosing-brace paren-state)
10246 paren-state))
10249 ;; CASE 19: line is an expression, not a statement, and is directly
10250 ;; contained by a template delimiter. Most likely, we are in a
10251 ;; template arglist within a statement. This case is based on CASE
10252 ;; 7. At some point in the future, we may wish to create more
10253 ;; syntactic symbols such as `template-intro',
10254 ;; `template-cont-nonempty', etc., and distinguish between them as we
10255 ;; do for `arglist-intro' etc. (2009-12-07).
10256 ((and c-recognize-<>-arglists
10257 (setq containing-< (c-up-list-backward indent-point containing-sexp))
10258 (eq (char-after containing-<) ?\<))
10259 (setq placeholder (c-point 'boi containing-<))
10260 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
10261 ; '<') before indent-point.
10262 (if (>= (point) placeholder)
10263 (progn
10264 (forward-char)
10265 (skip-chars-forward " \t"))
10266 (goto-char placeholder))
10267 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
10268 (c-most-enclosing-brace c-state-cache (point))
10269 paren-state))
10271 ;; CASE 17: Statement or defun catchall.
10273 (goto-char indent-point)
10274 ;; Back up statements until we find one that starts at boi.
10275 (while (let* ((prev-point (point))
10276 (last-step-type (c-beginning-of-statement-1
10277 containing-sexp)))
10278 (if (= (point) prev-point)
10279 (progn
10280 (setq step-type (or step-type last-step-type))
10281 nil)
10282 (setq step-type last-step-type)
10283 (/= (point) (c-point 'boi)))))
10284 (cond
10286 ;; CASE 17B: continued statement
10287 ((and (eq step-type 'same)
10288 (/= (point) indent-point))
10289 (c-add-stmt-syntax 'statement-cont nil nil
10290 containing-sexp paren-state))
10292 ;; CASE 17A: After a case/default label?
10293 ((progn
10294 (while (and (eq step-type 'label)
10295 (not (looking-at c-label-kwds-regexp)))
10296 (setq step-type
10297 (c-beginning-of-statement-1 containing-sexp)))
10298 (eq step-type 'label))
10299 (c-add-stmt-syntax (if (eq char-after-ip ?{)
10300 'statement-case-open
10301 'statement-case-intro)
10302 nil t containing-sexp paren-state))
10304 ;; CASE 17D: any old statement
10305 ((progn
10306 (while (eq step-type 'label)
10307 (setq step-type
10308 (c-beginning-of-statement-1 containing-sexp)))
10309 (eq step-type 'previous))
10310 (c-add-stmt-syntax 'statement nil t
10311 containing-sexp paren-state)
10312 (if (eq char-after-ip ?{)
10313 (c-add-syntax 'block-open)))
10315 ;; CASE 17I: Inside a substatement block.
10316 ((progn
10317 ;; The following tests are all based on containing-sexp.
10318 (goto-char containing-sexp)
10319 ;; From here on we have the next containing sexp in lim.
10320 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
10321 (c-after-conditional))
10322 (c-backward-to-block-anchor lim)
10323 (c-add-stmt-syntax 'statement-block-intro nil t
10324 lim paren-state)
10325 (if (eq char-after-ip ?{)
10326 (c-add-syntax 'block-open)))
10328 ;; CASE 17E: first statement in an in-expression block.
10329 ;; C.f. cases 4, 7B and 16A.
10330 ((setq placeholder (c-looking-at-inexpr-block
10331 (c-safe-position containing-sexp paren-state)
10332 nil))
10333 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10334 'defun-block-intro
10335 'statement-block-intro))
10336 (back-to-indentation)
10337 (if (= containing-sexp (point))
10338 (c-add-syntax tmpsymbol (point))
10339 (goto-char (cdr placeholder))
10340 (back-to-indentation)
10341 (c-add-stmt-syntax tmpsymbol nil t
10342 (c-most-enclosing-brace c-state-cache (point))
10343 paren-state)
10344 (if (/= (point) (cdr placeholder))
10345 (c-add-syntax (car placeholder))))
10346 (if (eq char-after-ip ?{)
10347 (c-add-syntax 'block-open)))
10349 ;; CASE 17F: first statement in an inline, or first
10350 ;; statement in a top-level defun. we can tell this is it
10351 ;; if there are no enclosing braces that haven't been
10352 ;; narrowed out by a class (i.e. don't use bod here).
10353 ((save-excursion
10354 (or (not (setq placeholder (c-most-enclosing-brace
10355 paren-state)))
10356 (and (progn
10357 (goto-char placeholder)
10358 (eq (char-after) ?{))
10359 (c-looking-at-decl-block (c-most-enclosing-brace
10360 paren-state (point))
10361 nil))))
10362 (c-backward-to-decl-anchor lim)
10363 (back-to-indentation)
10364 (c-add-syntax 'defun-block-intro (point)))
10366 ;; CASE 17G: First statement in a function declared inside
10367 ;; a normal block. This can occur in Pike and with
10368 ;; e.g. the gcc extensions, but watch out for macros
10369 ;; followed by blocks. C.f. cases B.3 and 16F.
10370 ((save-excursion
10371 (and (not (c-at-statement-start-p))
10372 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10373 (setq placeholder (point))
10374 (let ((c-recognize-typeless-decls nil))
10375 ;; Turn off recognition of constructs that lacks
10376 ;; a type in this case, since that's more likely
10377 ;; to be a macro followed by a block.
10378 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10379 (back-to-indentation)
10380 (if (/= (point) containing-sexp)
10381 (goto-char placeholder))
10382 (c-add-stmt-syntax 'defun-block-intro nil t
10383 lim paren-state))
10385 ;; CASE 17H: First statement in a block.
10387 ;; If the block is preceded by a case/switch label on the
10388 ;; same line, we anchor at the first preceding label at
10389 ;; boi. The default handling in c-add-stmt-syntax is
10390 ;; really fixes it better, but we do like this to keep the
10391 ;; indentation compatible with version 5.28 and earlier.
10392 ;; C.f. case 16C.
10393 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10394 (eq (c-beginning-of-statement-1 lim) 'label)))
10395 (goto-char placeholder)
10396 (if (looking-at c-label-kwds-regexp)
10397 (c-add-syntax 'statement-block-intro (point))
10398 (goto-char containing-sexp)
10399 ;; c-backward-to-block-anchor not necessary here; those
10400 ;; situations are handled in case 17I above.
10401 (c-add-stmt-syntax 'statement-block-intro nil t
10402 lim paren-state))
10403 (if (eq char-after-ip ?{)
10404 (c-add-syntax 'block-open)))
10408 ;; now we need to look at any modifiers
10409 (goto-char indent-point)
10410 (skip-chars-forward " \t")
10412 ;; are we looking at a comment only line?
10413 (when (and (looking-at c-comment-start-regexp)
10414 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
10415 (c-append-syntax 'comment-intro))
10417 ;; we might want to give additional offset to friends (in C++).
10418 (when (and c-opt-friend-key
10419 (looking-at c-opt-friend-key))
10420 (c-append-syntax 'friend))
10422 ;; Set syntactic-relpos.
10423 (let ((p c-syntactic-context))
10424 (while (and p
10425 (if (integerp (c-langelem-pos (car p)))
10426 (progn
10427 (setq syntactic-relpos (c-langelem-pos (car p)))
10428 nil)
10430 (setq p (cdr p))))
10432 ;; Start of or a continuation of a preprocessor directive?
10433 (if (and macro-start
10434 (eq macro-start (c-point 'boi))
10435 (not (and (c-major-mode-is 'pike-mode)
10436 (eq (char-after (1+ macro-start)) ?\"))))
10437 (c-append-syntax 'cpp-macro)
10438 (when (and c-syntactic-indentation-in-macros macro-start)
10439 (if in-macro-expr
10440 (when (or
10441 (< syntactic-relpos macro-start)
10442 (not (or
10443 (assq 'arglist-intro c-syntactic-context)
10444 (assq 'arglist-cont c-syntactic-context)
10445 (assq 'arglist-cont-nonempty c-syntactic-context)
10446 (assq 'arglist-close c-syntactic-context))))
10447 ;; If inside a cpp expression, i.e. anywhere in a
10448 ;; cpp directive except a #define body, we only let
10449 ;; through the syntactic analysis that is internal
10450 ;; in the expression. That means the arglist
10451 ;; elements, if they are anchored inside the cpp
10452 ;; expression.
10453 (setq c-syntactic-context nil)
10454 (c-add-syntax 'cpp-macro-cont macro-start))
10455 (when (and (eq macro-start syntactic-relpos)
10456 (not (assq 'cpp-define-intro c-syntactic-context))
10457 (save-excursion
10458 (goto-char macro-start)
10459 (or (not (c-forward-to-cpp-define-body))
10460 (<= (point) (c-point 'boi indent-point)))))
10461 ;; Inside a #define body and the syntactic analysis is
10462 ;; anchored on the start of the #define. In this case
10463 ;; we add cpp-define-intro to get the extra
10464 ;; indentation of the #define body.
10465 (c-add-syntax 'cpp-define-intro)))))
10467 ;; return the syntax
10468 c-syntactic-context)))
10471 ;; Indentation calculation.
10473 (defun c-evaluate-offset (offset langelem symbol)
10474 ;; offset can be a number, a function, a variable, a list, or one of
10475 ;; the symbols + or -
10477 ;; This function might do hidden buffer changes.
10478 (let ((res
10479 (cond
10480 ((numberp offset) offset)
10481 ((vectorp offset) offset)
10482 ((null offset) nil)
10484 ((eq offset '+) c-basic-offset)
10485 ((eq offset '-) (- c-basic-offset))
10486 ((eq offset '++) (* 2 c-basic-offset))
10487 ((eq offset '--) (* 2 (- c-basic-offset)))
10488 ((eq offset '*) (/ c-basic-offset 2))
10489 ((eq offset '/) (/ (- c-basic-offset) 2))
10491 ((functionp offset)
10492 (c-evaluate-offset
10493 (funcall offset
10494 (cons (c-langelem-sym langelem)
10495 (c-langelem-pos langelem)))
10496 langelem symbol))
10498 ((listp offset)
10499 (cond
10500 ((eq (car offset) 'quote)
10501 (c-benign-error "The offset %S for %s was mistakenly quoted"
10502 offset symbol)
10503 nil)
10505 ((memq (car offset) '(min max))
10506 (let (res val (method (car offset)))
10507 (setq offset (cdr offset))
10508 (while offset
10509 (setq val (c-evaluate-offset (car offset) langelem symbol))
10510 (cond
10511 ((not val))
10512 ((not res)
10513 (setq res val))
10514 ((integerp val)
10515 (if (vectorp res)
10516 (c-benign-error "\
10517 Error evaluating offset %S for %s: \
10518 Cannot combine absolute offset %S with relative %S in `%s' method"
10519 (car offset) symbol res val method)
10520 (setq res (funcall method res val))))
10522 (if (integerp res)
10523 (c-benign-error "\
10524 Error evaluating offset %S for %s: \
10525 Cannot combine relative offset %S with absolute %S in `%s' method"
10526 (car offset) symbol res val method)
10527 (setq res (vector (funcall method (aref res 0)
10528 (aref val 0)))))))
10529 (setq offset (cdr offset)))
10530 res))
10532 ((eq (car offset) 'add)
10533 (let (res val)
10534 (setq offset (cdr offset))
10535 (while offset
10536 (setq val (c-evaluate-offset (car offset) langelem symbol))
10537 (cond
10538 ((not val))
10539 ((not res)
10540 (setq res val))
10541 ((integerp val)
10542 (if (vectorp res)
10543 (setq res (vector (+ (aref res 0) val)))
10544 (setq res (+ res val))))
10546 (if (vectorp res)
10547 (c-benign-error "\
10548 Error evaluating offset %S for %s: \
10549 Cannot combine absolute offsets %S and %S in `add' method"
10550 (car offset) symbol res val)
10551 (setq res val)))) ; Override.
10552 (setq offset (cdr offset)))
10553 res))
10556 (let (res)
10557 (when (eq (car offset) 'first)
10558 (setq offset (cdr offset)))
10559 (while (and (not res) offset)
10560 (setq res (c-evaluate-offset (car offset) langelem symbol)
10561 offset (cdr offset)))
10562 res))))
10564 ((and (symbolp offset) (boundp offset))
10565 (symbol-value offset))
10568 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10569 nil))))
10571 (if (or (null res) (integerp res)
10572 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10574 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10575 offset symbol res)
10576 nil)))
10578 (defun c-calc-offset (langelem)
10579 ;; Get offset from LANGELEM which is a list beginning with the
10580 ;; syntactic symbol and followed by any analysis data it provides.
10581 ;; That data may be zero or more elements, but if at least one is
10582 ;; given then the first is the anchor position (or nil). The symbol
10583 ;; is matched against `c-offsets-alist' and the offset calculated
10584 ;; from that is returned.
10586 ;; This function might do hidden buffer changes.
10587 (let* ((symbol (c-langelem-sym langelem))
10588 (match (assq symbol c-offsets-alist))
10589 (offset (cdr-safe match)))
10590 (if match
10591 (setq offset (c-evaluate-offset offset langelem symbol))
10592 (if c-strict-syntax-p
10593 (c-benign-error "No offset found for syntactic symbol %s" symbol))
10594 (setq offset 0))
10595 (if (vectorp offset)
10596 offset
10597 (or (and (numberp offset) offset)
10598 (and (symbolp offset) (symbol-value offset))
10602 (defun c-get-offset (langelem)
10603 ;; This is a compatibility wrapper for `c-calc-offset' in case
10604 ;; someone is calling it directly. It takes an old style syntactic
10605 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10606 ;; new list form.
10608 ;; This function might do hidden buffer changes.
10609 (if (c-langelem-pos langelem)
10610 (c-calc-offset (list (c-langelem-sym langelem)
10611 (c-langelem-pos langelem)))
10612 (c-calc-offset langelem)))
10614 (defun c-get-syntactic-indentation (langelems)
10615 ;; Calculate the syntactic indentation from a syntactic description
10616 ;; as returned by `c-guess-syntax'.
10618 ;; Note that topmost-intro always has an anchor position at bol, for
10619 ;; historical reasons. It's often used together with other symbols
10620 ;; that has more sane positions. Since we always use the first
10621 ;; found anchor position, we rely on that these other symbols always
10622 ;; precede topmost-intro in the LANGELEMS list.
10624 ;; This function might do hidden buffer changes.
10625 (let ((indent 0) anchor)
10627 (while langelems
10628 (let* ((c-syntactic-element (car langelems))
10629 (res (c-calc-offset c-syntactic-element)))
10631 (if (vectorp res)
10632 ;; Got an absolute column that overrides any indentation
10633 ;; we've collected so far, but not the relative
10634 ;; indentation we might get for the nested structures
10635 ;; further down the langelems list.
10636 (setq indent (elt res 0)
10637 anchor (point-min)) ; A position at column 0.
10639 ;; Got a relative change of the current calculated
10640 ;; indentation.
10641 (setq indent (+ indent res))
10643 ;; Use the anchor position from the first syntactic
10644 ;; element with one.
10645 (unless anchor
10646 (setq anchor (c-langelem-pos (car langelems)))))
10648 (setq langelems (cdr langelems))))
10650 (if anchor
10651 (+ indent (save-excursion
10652 (goto-char anchor)
10653 (current-column)))
10654 indent)))
10657 (cc-provide 'cc-engine)
10659 ;;; cc-engine.el ends here