Merge from emacs-23; up to 2010-06-29T18:17:31Z!cyd@stupidchicken.com.
[emacs.git] / lisp / progmodes / cc-engine.el
blobbe0f86ddd7e78a17be96f2baaac55dc8aab0da62
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)
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)
2146 (progn
2147 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2148 (cond
2149 ((null lit)
2150 (setq pos npos)
2152 ((<= (cdr lit) here)
2153 (setq pos (cdr lit))
2156 (setq pos (car lit))
2157 nil))))
2159 (goto-char pos)
2160 (when (and (c-beginning-of-macro) (/= (point) pos))
2161 (setq macro-beg (point))
2162 (c-syntactic-end-of-macro)
2163 (or (eobp) (forward-char))
2164 (setq pos (if (<= (point) here)
2165 (point)
2166 macro-beg)))
2167 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2169 (if (> pos c-state-nonlit-pos-cache-limit)
2170 (setq c-state-nonlit-pos-cache-limit pos))
2171 pos))))
2173 (defun c-state-literal-at (here)
2174 ;; If position HERE is inside a literal, return (START . END), the
2175 ;; boundaries of the literal (which may be outside the accessible bit of the
2176 ;; buffer). Otherwise, return nil.
2178 ;; This function is almost the same as `c-literal-limits'. Previously, it
2179 ;; differed in that it was a lower level function, and that it rigorously
2180 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2181 ;; virtually identical to this function.
2182 (save-restriction
2183 (widen)
2184 (save-excursion
2185 (let ((pos (c-state-safe-place here)))
2186 (car (cddr (c-state-pp-to-literal pos here)))))))
2188 (defsubst c-state-lit-beg (pos)
2189 ;; Return the start of the literal containing POS, or POS itself.
2190 (or (car (c-state-literal-at pos))
2191 pos))
2193 (defsubst c-state-cache-non-literal-place (pos state)
2194 ;; Return a position outside of a string/comment/macro at or before POS.
2195 ;; STATE is the parse-partial-sexp state at POS.
2196 (let ((res (if (or (nth 3 state) ; in a string?
2197 (nth 4 state)) ; in a comment?
2198 (nth 8 state)
2199 pos)))
2200 (save-excursion
2201 (goto-char res)
2202 (if (c-beginning-of-macro)
2203 (point)
2204 res))))
2206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2207 ;; Stuff to do with point-min, and coping with any literal there.
2208 (defvar c-state-point-min 1)
2209 (make-variable-buffer-local 'c-state-point-min)
2210 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2211 ;; narrowing is likely to affect the parens that are visible before the point.
2213 (defvar c-state-point-min-lit-type nil)
2214 (make-variable-buffer-local 'c-state-point-min-lit-type)
2215 (defvar c-state-point-min-lit-start nil)
2216 (make-variable-buffer-local 'c-state-point-min-lit-start)
2217 ;; These two variables define the literal, if any, containing point-min.
2218 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2219 ;; literal. If there's no literal there, they're both nil.
2221 (defvar c-state-min-scan-pos 1)
2222 (make-variable-buffer-local 'c-state-min-scan-pos)
2223 ;; This is the earliest buffer-pos from which scanning can be done. It is
2224 ;; either the end of the literal containing point-min, or point-min itself.
2225 ;; It becomes nil if the buffer is changed earlier than this point.
2226 (defun c-state-get-min-scan-pos ()
2227 ;; Return the lowest valid scanning pos. This will be the end of the
2228 ;; literal enclosing point-min, or point-min itself.
2229 (or c-state-min-scan-pos
2230 (save-restriction
2231 (save-excursion
2232 (widen)
2233 (goto-char c-state-point-min-lit-start)
2234 (if (eq c-state-point-min-lit-type 'string)
2235 (forward-sexp)
2236 (forward-comment 1))
2237 (setq c-state-min-scan-pos (point))))))
2239 (defun c-state-mark-point-min-literal ()
2240 ;; Determine the properties of any literal containing POINT-MIN, setting the
2241 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2242 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2243 (let ((p-min (point-min))
2244 lit)
2245 (save-restriction
2246 (widen)
2247 (setq lit (c-state-literal-at p-min))
2248 (if lit
2249 (setq c-state-point-min-lit-type
2250 (save-excursion
2251 (goto-char (car lit))
2252 (cond
2253 ((looking-at c-block-comment-start-regexp) 'c)
2254 ((looking-at c-line-comment-starter) 'c++)
2255 (t 'string)))
2256 c-state-point-min-lit-start (car lit)
2257 c-state-min-scan-pos (cdr lit))
2258 (setq c-state-point-min-lit-type nil
2259 c-state-point-min-lit-start nil
2260 c-state-min-scan-pos p-min)))))
2263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2264 ;; A variable which signals a brace dessert - helpful for reducing the number
2265 ;; of fruitless backward scans.
2266 (defvar c-state-brace-pair-desert nil)
2267 (make-variable-buffer-local 'c-state-brace-pair-desert)
2268 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2269 ;; that defun has searched backwards for a brace pair and not found one. Its
2270 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2271 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2272 ;; nil when at top level) and FROM is where the backward search started. It
2273 ;; is reset to nil in `c-invalidate-state-cache'.
2276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2277 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2278 ;; list of like structure.
2279 (defmacro c-state-cache-top-lparen (&optional cache)
2280 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2281 ;; (default `c-state-cache') (or nil).
2282 (let ((cash (or cache 'c-state-cache)))
2283 `(if (consp (car ,cash))
2284 (caar ,cash)
2285 (car ,cash))))
2287 (defmacro c-state-cache-top-paren (&optional cache)
2288 ;; Return the address of the latest brace/bracket/paren (whether left or
2289 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2290 (let ((cash (or cache 'c-state-cache)))
2291 `(if (consp (car ,cash))
2292 (cdar ,cash)
2293 (car ,cash))))
2295 (defmacro c-state-cache-after-top-paren (&optional cache)
2296 ;; Return the position just after the latest brace/bracket/paren (whether
2297 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2298 (let ((cash (or cache 'c-state-cache)))
2299 `(if (consp (car ,cash))
2300 (cdar ,cash)
2301 (and (car ,cash)
2302 (1+ (car ,cash))))))
2304 (defun c-get-cache-scan-pos (here)
2305 ;; From the state-cache, determine the buffer position from which we might
2306 ;; scan forward to HERE to update this cache. This position will be just
2307 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2308 ;; return the earliest position in the accessible region which isn't within
2309 ;; a literal. If the visible portion of the buffer is entirely within a
2310 ;; literal, return NIL.
2311 (let ((c c-state-cache) elt)
2312 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2313 (while (and c
2314 (>= (c-state-cache-top-lparen c) here))
2315 (setq c (cdr c)))
2317 (setq elt (car c))
2318 (cond
2319 ((consp elt)
2320 (if (> (cdr elt) here)
2321 (1+ (car elt))
2322 (cdr elt)))
2323 (elt (1+ elt))
2324 ((<= (c-state-get-min-scan-pos) here)
2325 (c-state-get-min-scan-pos))
2326 (t nil))))
2328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2329 ;; Variables which keep track of preprocessor constructs.
2330 (defvar c-state-old-cpp-beg nil)
2331 (make-variable-buffer-local 'c-state-old-cpp-beg)
2332 (defvar c-state-old-cpp-end nil)
2333 (make-variable-buffer-local 'c-state-old-cpp-end)
2334 ;; These are the limits of the macro containing point at the previous call of
2335 ;; `c-parse-state', or nil.
2337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2338 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2339 (defun c-get-fallback-scan-pos (here)
2340 ;; Return a start position for building `c-state-cache' from
2341 ;; scratch. This will be at the top level, 2 defuns back.
2342 (save-excursion
2343 ;; Go back 2 bods, but ignore any bogus positions returned by
2344 ;; beginning-of-defun (i.e. open paren in column zero).
2345 (goto-char here)
2346 (let ((cnt 2))
2347 (while (not (or (bobp) (zerop cnt)))
2348 (c-beginning-of-defun-1) ; Pure elisp BOD.
2349 (if (eq (char-after) ?\{)
2350 (setq cnt (1- cnt)))))
2351 (point)))
2353 (defun c-state-balance-parens-backwards (here- here+ top)
2354 ;; Return the position of the opening paren/brace/bracket before HERE- which
2355 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2356 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2358 ;; ............................................
2359 ;; | |
2360 ;; ( [ ( .........#macro.. ) ( ) ] )
2361 ;; ^ ^ ^ ^
2362 ;; | | | |
2363 ;; return HERE- HERE+ TOP
2365 ;; If there aren't enough opening paren/brace/brackets, return the position
2366 ;; of the outermost one found, or HERE- if there are none. If there are no
2367 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2368 ;; must not be inside literals. Only the accessible portion of the buffer
2369 ;; will be scanned.
2371 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2372 ;; `here'. Go round the next loop each time we pass over such a ")". These
2373 ;; probably match "("s before `here-'.
2374 (let (pos pa ren+1 lonely-rens)
2375 (save-excursion
2376 (save-restriction
2377 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2378 (setq pos here+)
2379 (c-safe
2380 (while
2381 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2382 (setq lonely-rens (cons ren+1 lonely-rens)
2383 pos ren+1)))))
2385 ;; PART 2: Scan back before `here-' searching for the "("s
2386 ;; matching/mismatching the ")"s found above. We only need to direct the
2387 ;; caller to scan when we've encountered unmatched right parens.
2388 (setq pos here-)
2389 (when lonely-rens
2390 (c-safe
2391 (while
2392 (and lonely-rens ; actual values aren't used.
2393 (setq pa (scan-lists pos -1 1)))
2394 (setq pos pa)
2395 (setq lonely-rens (cdr lonely-rens)))))
2396 pos))
2398 (defun c-parse-state-get-strategy (here good-pos)
2399 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2400 ;; to minimize the amount of scanning. HERE is the pertinent position in
2401 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2402 ;; its head trimmed) is known to be good, or nil if there is no such
2403 ;; position.
2405 ;; The return value is a list, one of the following:
2407 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2408 ;; which is not less than CACHE-POS.
2409 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2410 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2411 ;; top level.
2412 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2413 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2414 ;; or below HERE.
2415 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2416 BOD-pos ; position of 2nd BOD before HERE.
2417 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2418 start-point
2419 how-far) ; putative scanning distance.
2420 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2421 (cond
2422 ((< here (c-state-get-min-scan-pos))
2423 (setq strategy 'IN-LIT
2424 start-point nil
2425 cache-pos nil
2426 how-far 0))
2427 ((<= good-pos here)
2428 (setq strategy 'forward
2429 start-point (max good-pos cache-pos)
2430 how-far (- here start-point)))
2431 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2432 (setq strategy 'backward
2433 how-far (- good-pos here)))
2435 (setq strategy 'forward
2436 how-far (- here cache-pos)
2437 start-point cache-pos)))
2439 ;; Might we be better off starting from the top level, two defuns back,
2440 ;; instead?
2441 (when (> how-far c-state-cache-too-far)
2442 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2443 (if (< (- here BOD-pos) how-far)
2444 (setq strategy 'BOD
2445 start-point BOD-pos)))
2447 (list
2448 strategy
2449 (and (memq strategy '(forward backward)) cache-pos)
2450 (and (memq strategy '(forward BOD)) start-point))))
2453 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2454 ;; Routines which change `c-state-cache' and associated values.
2455 (defun c-renarrow-state-cache ()
2456 ;; The region (more precisely, point-min) has changed since we
2457 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2458 (if (< (point-min) c-state-point-min)
2459 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2460 ;; It would be possible to do a better job here and recalculate the top
2461 ;; only.
2462 (progn
2463 (c-state-mark-point-min-literal)
2464 (setq c-state-cache nil
2465 c-state-cache-good-pos c-state-min-scan-pos
2466 c-state-brace-pair-desert nil))
2468 ;; point-min has MOVED FORWARD.
2470 ;; Is the new point-min inside a (different) literal?
2471 (unless (and c-state-point-min-lit-start ; at prev. point-min
2472 (< (point-min) (c-state-get-min-scan-pos)))
2473 (c-state-mark-point-min-literal))
2475 ;; Cut off a bit of the tail from `c-state-cache'.
2476 (let ((ptr (cons nil c-state-cache))
2478 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2479 (>= pa (point-min)))
2480 (setq ptr (cdr ptr)))
2482 (when (consp ptr)
2483 (if (eq (cdr ptr) c-state-cache)
2484 (setq c-state-cache nil
2485 c-state-cache-good-pos c-state-min-scan-pos)
2486 (setcdr ptr nil)
2487 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2490 (setq c-state-point-min (point-min)))
2492 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2493 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2494 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2495 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2496 ;; the highest brace pair whose "}" is below UPPER-LIM.
2498 ;; Return non-nil when this has been done.
2500 ;; This routine should be fast. Since it can get called a LOT, we maintain
2501 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2502 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2503 (save-excursion
2504 (save-restriction
2505 (let ((bra from) ce ; Positions of "{" and "}".
2506 new-cons
2507 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2508 (macro-start-or-from
2509 (progn (goto-char from)
2510 (c-beginning-of-macro)
2511 (point))))
2512 (or upper-lim (setq upper-lim from))
2514 ;; If we're essentially repeating a fruitless search, just give up.
2515 (unless (and c-state-brace-pair-desert
2516 (eq cache-pos (car c-state-brace-pair-desert))
2517 (<= from (cdr c-state-brace-pair-desert)))
2518 ;; Only search what we absolutely need to:
2519 (if (and c-state-brace-pair-desert
2520 (eq cache-pos (car c-state-brace-pair-desert)))
2521 (narrow-to-region (cdr c-state-brace-pair-desert) (point-max)))
2523 ;; In the next pair of nested loops, the inner one moves back past a
2524 ;; pair of (mis-)matching parens or brackets; the outer one moves
2525 ;; back over a sequence of unmatched close brace/paren/bracket each
2526 ;; time round.
2527 (while
2528 (progn
2529 (c-safe
2530 (while
2531 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2532 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2533 (or (> ce upper-lim)
2534 (not (eq (char-after bra) ?\{))
2535 (and (goto-char bra)
2536 (c-beginning-of-macro)
2537 (< (point) macro-start-or-from))))))
2538 (and ce (< ce bra)))
2539 (setq bra ce)) ; If we just backed over an unbalanced closing
2540 ; brace, ignore it.
2542 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2543 ;; We've found the desired brace-pair.
2544 (progn
2545 (setq new-cons (cons bra (1+ ce)))
2546 (cond
2547 ((consp (car c-state-cache))
2548 (setcar c-state-cache new-cons))
2549 ((and (numberp (car c-state-cache)) ; probably never happens
2550 (< ce (car c-state-cache)))
2551 (setcdr c-state-cache
2552 (cons new-cons (cdr c-state-cache))))
2553 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2555 ;; We haven't found a brace pair. Record this.
2556 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2558 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2559 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2560 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2561 ;; "push" "a" brace pair onto `c-state-cache'.
2563 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2564 ;; otherwise push it normally.
2566 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2567 ;; latter is inside a macro, not being a macro containing
2568 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2569 ;; base pair. This latter case is assumed to be rare.
2571 ;; Note: POINT is not preserved in this routine.
2572 (if bra+1
2573 (if (or (> bra+1 macro-start-or-here)
2574 (progn (goto-char bra+1)
2575 (not (c-beginning-of-macro))))
2576 (setq c-state-cache
2577 (cons (cons (1- bra+1)
2578 (scan-lists bra+1 1 1))
2579 (if (consp (car c-state-cache))
2580 (cdr c-state-cache)
2581 c-state-cache)))
2582 ;; N.B. This defsubst codes one method for the simple, normal case,
2583 ;; and a more sophisticated, slower way for the general case. Don't
2584 ;; eliminate this defsubst - it's a speed optimization.
2585 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2587 (defun c-append-to-state-cache (from)
2588 ;; Scan the buffer from FROM to (point-max), adding elements into
2589 ;; `c-state-cache' for braces etc. Return a candidate for
2590 ;; `c-state-cache-good-pos'.
2592 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2593 ;; any. Typically, it is immediately after it. It must not be inside a
2594 ;; literal.
2595 (let ((here-bol (c-point 'bol (point-max)))
2596 (macro-start-or-here
2597 (save-excursion (goto-char (point-max))
2598 (if (c-beginning-of-macro)
2599 (point)
2600 (point-max))))
2601 pa+1 ; pos just after an opening PAren (or brace).
2602 (ren+1 from) ; usually a pos just after an closing paREN etc.
2603 ; Is actually the pos. to scan for a (/{/[ from,
2604 ; which sometimes is after a silly )/}/].
2605 paren+1 ; Pos after some opening or closing paren.
2606 paren+1s ; A list of `paren+1's; used to determine a
2607 ; good-pos.
2608 bra+1 ce+1 ; just after L/R bra-ces.
2609 bra+1s ; list of OLD values of bra+1.
2610 mstart) ; start of a macro.
2612 (save-excursion
2613 ;; Each time round the following loop, we enter a successively deeper
2614 ;; level of brace/paren nesting. (Except sometimes we "continue at
2615 ;; the existing level".) `pa+1' is a pos inside an opening
2616 ;; brace/paren/bracket, usually just after it.
2617 (while
2618 (progn
2619 ;; Each time round the next loop moves forward over an opening then
2620 ;; a closing brace/bracket/paren. This loop is white hot, so it
2621 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2622 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2623 ;; call of `scan-lists' signals an error, which happens when there
2624 ;; are no more b/b/p's to scan.
2625 (c-safe
2626 (while t
2627 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2628 paren+1s (cons pa+1 paren+1s))
2629 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2630 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2631 (setq bra+1 pa+1))
2632 (setcar paren+1s ren+1)))
2634 (if (and pa+1 (> pa+1 ren+1))
2635 ;; We've just entered a deeper nesting level.
2636 (progn
2637 ;; Insert the brace pair (if present) and the single open
2638 ;; paren/brace/bracket into `c-state-cache' It cannot be
2639 ;; inside a macro, except one around point, because of what
2640 ;; `c-neutralize-syntax-in-CPP' has done.
2641 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2642 ;; Insert the opening brace/bracket/paren position.
2643 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2644 ;; Clear admin stuff for the next more nested part of the scan.
2645 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2646 t) ; Carry on the loop
2648 ;; All open p/b/b's at this nesting level, if any, have probably
2649 ;; been closed by matching/mismatching ones. We're probably
2650 ;; finished - we just need to check for having found an
2651 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2652 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2653 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2655 ;; Record the final, innermost, brace-pair if there is one.
2656 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2658 ;; Determine a good pos
2659 (while (and (setq paren+1 (car paren+1s))
2660 (> (if (> paren+1 macro-start-or-here)
2661 paren+1
2662 (goto-char paren+1)
2663 (setq mstart (and (c-beginning-of-macro)
2664 (point)))
2665 (or mstart paren+1))
2666 here-bol))
2667 (setq paren+1s (cdr paren+1s)))
2668 (cond
2669 ((and paren+1 mstart)
2670 (min paren+1 mstart))
2671 (paren+1)
2672 (t from)))))
2674 (defun c-remove-stale-state-cache (good-pos pps-point)
2675 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2676 ;; not be in it when it is amended for position (point-max).
2677 ;; Additionally, the "outermost" open-brace entry before (point-max)
2678 ;; will be converted to a cons if the matching close-brace is scanned.
2680 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2681 ;; parens/braces/brackets between GOOD-POS and (point-max).
2683 ;; As a second thing, calculate the result of parse-partial-sexp at
2684 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2685 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2686 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2687 ;; needs to be FAST).
2689 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2690 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2691 ;; to be good (we aim for this to be as high as possible);
2692 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2693 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2694 ;; position to scan backwards from.
2695 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2696 (save-restriction
2697 (narrow-to-region 1 (point-max))
2698 (save-excursion
2699 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2700 (save-excursion
2701 (goto-char (point-max))
2702 (and (c-beginning-of-macro)
2703 (point))))
2704 (good-pos-actual-macro-start ; Start of macro containing good-pos
2705 ; or nil
2706 (and (< good-pos (point-max))
2707 (save-excursion
2708 (goto-char good-pos)
2709 (and (c-beginning-of-macro)
2710 (point)))))
2711 (good-pos-actual-macro-end ; End of this macro, (maybe
2712 ; (point-max)), or nil.
2713 (and good-pos-actual-macro-start
2714 (save-excursion
2715 (goto-char good-pos-actual-macro-start)
2716 (c-end-of-macro)
2717 (point))))
2718 pps-state ; Will be 9 or 10 elements long.
2720 upper-lim ; ,beyond which `c-state-cache' entries are removed
2721 scan-back-pos
2722 pair-beg pps-point-state target-depth)
2724 ;; Remove entries beyond (point-max). Also remove any entries inside
2725 ;; a macro, unless (point-max) is in the same macro.
2726 (setq upper-lim
2727 (if (or (null c-state-old-cpp-beg)
2728 (and (> (point-max) c-state-old-cpp-beg)
2729 (< (point-max) c-state-old-cpp-end)))
2730 (point-max)
2731 (min (point-max) c-state-old-cpp-beg)))
2732 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2733 (setq c-state-cache (cdr c-state-cache)))
2734 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2735 ;; RBrace and indicate we'll need to search backwards for a previous
2736 ;; brace pair.
2737 (when (and c-state-cache
2738 (consp (car c-state-cache))
2739 (> (cdar c-state-cache) upper-lim))
2740 (setcar c-state-cache (caar c-state-cache))
2741 (setq scan-back-pos (car c-state-cache)))
2743 ;; The next loop jumps forward out of a nested level of parens each
2744 ;; time round; the corresponding elements in `c-state-cache' are
2745 ;; removed. `pos' is just after the brace-pair or the open paren at
2746 ;; (car c-state-cache). There can be no open parens/braces/brackets
2747 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2748 ;; due to the interface spec to this function.
2749 (setq pos (if (and good-pos-actual-macro-end
2750 (not (eq good-pos-actual-macro-start
2751 in-macro-start)))
2752 (1+ good-pos-actual-macro-end) ; get outside the macro as
2753 ; marked by a `category' text property.
2754 good-pos))
2755 (goto-char pos)
2756 (while (and c-state-cache
2757 (< (point) (point-max)))
2758 (cond
2759 ((null pps-state) ; first time through
2760 (setq target-depth -1))
2761 ((eq (car pps-state) target-depth) ; found closing ),},]
2762 (setq target-depth (1- (car pps-state))))
2763 ;; Do nothing when we've merely reached pps-point.
2766 ;; Scan!
2767 (setq pps-state
2768 (parse-partial-sexp
2769 (point) (if (< (point) pps-point) pps-point (point-max))
2770 target-depth
2771 nil pps-state))
2773 (if (= (point) pps-point)
2774 (setq pps-point-state pps-state))
2776 (when (eq (car pps-state) target-depth)
2777 (setq pos (point)) ; POS is now just after an R-paren/brace.
2778 (cond
2779 ((and (consp (car c-state-cache))
2780 (eq (point) (cdar c-state-cache)))
2781 ;; We've just moved out of the paren pair containing the brace-pair
2782 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2783 ;; and is potentially where the open brace of a cons in
2784 ;; c-state-cache will be.
2785 (setq pair-beg (car-safe (cdr c-state-cache))
2786 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2787 ((numberp (car c-state-cache))
2788 (setq pair-beg (car c-state-cache)
2789 c-state-cache (cdr c-state-cache))) ; remove this
2790 ; containing Lparen
2791 ((numberp (cadr c-state-cache))
2792 (setq pair-beg (cadr c-state-cache)
2793 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2794 ; together with enclosed brace pair.
2795 ;; (t nil) ; Ignore an unmated Rparen.
2798 (if (< (point) pps-point)
2799 (setq pps-state (parse-partial-sexp (point) pps-point
2800 nil nil ; TARGETDEPTH, STOPBEFORE
2801 pps-state)))
2803 ;; If the last paren pair we moved out of was actually a brace pair,
2804 ;; insert it into `c-state-cache'.
2805 (when (and pair-beg (eq (char-after pair-beg) ?{))
2806 (if (consp (car-safe c-state-cache))
2807 (setq c-state-cache (cdr c-state-cache)))
2808 (setq c-state-cache (cons (cons pair-beg pos)
2809 c-state-cache)))
2811 (list pos scan-back-pos pps-state)))))
2813 (defun c-remove-stale-state-cache-backwards (here cache-pos)
2814 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2815 ;; buffer, and inform the caller of the scenario detected.
2817 ;; HERE is the position we're setting `c-state-cache' for.
2818 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2819 ;; before HERE, or a position at or near point-min which isn't in a
2820 ;; literal.
2822 ;; This function must only be called only when (> `c-state-cache-good-pos'
2823 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2824 ;; optimized to eliminate (or minimize) scanning between these two
2825 ;; positions.
2827 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2828 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2829 ;; could become so after missing elements are inserted into
2830 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2831 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2832 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2833 ;; before `here''s line, or the start of the literal containing it.
2834 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2835 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2836 ;; to scan backwards from.
2837 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2838 ;; POS and HERE which aren't recorded in `c-state-cache'.
2840 ;; The comments in this defun use "paren" to mean parenthesis or square
2841 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2843 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2844 ;; | | | | | |
2845 ;; CP E here D C good
2846 (let ((pos c-state-cache-good-pos)
2847 pa ren ; positions of "(" and ")"
2848 dropped-cons ; whether the last element dropped from `c-state-cache'
2849 ; was a cons (representing a brace-pair)
2850 good-pos ; see above.
2851 lit ; (START . END) of a literal containing some point.
2852 here-lit-start here-lit-end ; bounds of literal containing `here'
2853 ; or `here' itself.
2854 here- here+ ; start/end of macro around HERE, or HERE
2855 (here-bol (c-point 'bol here))
2856 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
2858 ;; Remove completely irrelevant entries from `c-state-cache'.
2859 (while (and c-state-cache
2860 (>= (setq pa (c-state-cache-top-lparen)) here))
2861 (setq dropped-cons (consp (car c-state-cache)))
2862 (setq c-state-cache (cdr c-state-cache))
2863 (setq pos pa))
2864 ;; At this stage, (> pos here);
2865 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2867 (cond
2868 ((and (consp (car c-state-cache))
2869 (> (cdar c-state-cache) here))
2870 ;; CASE 1: The top of the cache is a brace pair which now encloses
2871 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2872 ;; knowledge of what's inside these braces, we have no alternative but
2873 ;; to direct the caller to scan the buffer from the opening brace.
2874 (setq pos (caar c-state-cache))
2875 (setcar c-state-cache pos)
2876 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
2877 ; entry into a { entry, so the caller needs to
2878 ; search for a brace pair before the {.
2880 ;; `here' might be inside a literal. Check for this.
2881 ((progn
2882 (setq lit (c-state-literal-at here)
2883 here-lit-start (or (car lit) here)
2884 here-lit-end (or (cdr lit) here))
2885 ;; Has `here' just "newly entered" a macro?
2886 (save-excursion
2887 (goto-char here-lit-start)
2888 (if (and (c-beginning-of-macro)
2889 (or (null c-state-old-cpp-beg)
2890 (not (= (point) c-state-old-cpp-beg))))
2891 (progn
2892 (setq here- (point))
2893 (c-end-of-macro)
2894 (setq here+ (point)))
2895 (setq here- here-lit-start
2896 here+ here-lit-end)))
2898 ;; `here' might be nested inside any depth of parens (or brackets but
2899 ;; not braces). Scan backwards to find the outermost such opening
2900 ;; paren, if there is one. This will be the scan position to return.
2901 (save-restriction
2902 (narrow-to-region cache-pos (point-max))
2903 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
2904 nil)) ; for the cond
2906 ((< pos here-lit-start)
2907 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2908 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2909 ;; a brace pair preceding this, it will already be in `c-state-cache',
2910 ;; unless there was a brace pair after it, i.e. there'll only be one to
2911 ;; scan for if we've just deleted one.
2912 (list pos (and dropped-cons pos) t)) ; Return value.
2914 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2915 ;; Further forward scanning isn't needed, but we still need to find a
2916 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2917 ((progn
2918 (save-restriction
2919 (narrow-to-region here-bol (point-max))
2920 (setq pos here-lit-start)
2921 (c-safe (while (setq pa (scan-lists pos -1 1))
2922 (setq pos pa)))) ; might signal
2923 nil)) ; for the cond
2925 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
2926 ;; CASE 3: After a }/)/] before `here''s BOL.
2927 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
2930 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2931 ;; literal containing it.
2932 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
2933 (list good-pos (and dropped-cons good-pos) nil)))))
2936 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2937 ;; Externally visible routines.
2939 (defun c-state-cache-init ()
2940 (setq c-state-cache nil
2941 c-state-cache-good-pos 1
2942 c-state-nonlit-pos-cache nil
2943 c-state-nonlit-pos-cache-limit 1
2944 c-state-brace-pair-desert nil
2945 c-state-point-min 1
2946 c-state-point-min-lit-type nil
2947 c-state-point-min-lit-start nil
2948 c-state-min-scan-pos 1
2949 c-state-old-cpp-beg nil
2950 c-state-old-cpp-end nil)
2951 (c-state-mark-point-min-literal))
2953 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2954 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
2955 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
2956 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
2957 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
2958 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
2959 ;; (defun c-state-dump ()
2960 ;; ;; For debugging.
2961 ;; ;(message
2962 ;; (concat
2963 ;; (c-sc-qde c-state-cache)
2964 ;; (c-sc-de c-state-cache-good-pos)
2965 ;; (c-sc-qde c-state-nonlit-pos-cache)
2966 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
2967 ;; (c-sc-qde c-state-brace-pair-desert)
2968 ;; (c-sc-de c-state-point-min)
2969 ;; (c-sc-de c-state-point-min-lit-type)
2970 ;; (c-sc-de c-state-point-min-lit-start)
2971 ;; (c-sc-de c-state-min-scan-pos)
2972 ;; (c-sc-de c-state-old-cpp-beg)
2973 ;; (c-sc-de c-state-old-cpp-end)))
2974 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2976 (defun c-invalidate-state-cache-1 (here)
2977 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2978 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2979 ;; left in a consistent state.
2981 ;; This is much like `c-whack-state-after', but it never changes a paren
2982 ;; pair element into an open paren element. Doing that would mean that the
2983 ;; new open paren wouldn't have the required preceding paren pair element.
2985 ;; This function is called from c-after-change.
2987 ;; The cache of non-literals:
2988 (if (< here c-state-nonlit-pos-cache-limit)
2989 (setq c-state-nonlit-pos-cache-limit here))
2991 ;; `c-state-cache':
2992 ;; Case 1: if `here' is in a literal containing point-min, everything
2993 ;; becomes (or is already) nil.
2994 (if (or (null c-state-cache-good-pos)
2995 (< here (c-state-get-min-scan-pos)))
2996 (setq c-state-cache nil
2997 c-state-cache-good-pos nil
2998 c-state-min-scan-pos nil)
3000 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3001 ;; below `here'. To maintain its consistency, we may need to insert a new
3002 ;; brace pair.
3003 (let ((here-bol (c-point 'bol here))
3004 too-high-pa ; recorded {/(/[ next above here, or nil.
3005 dropped-cons ; was the last removed element a brace pair?
3007 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3008 (while (and c-state-cache
3009 (>= (setq pa (c-state-cache-top-paren)) here))
3010 (setq dropped-cons (consp (car c-state-cache))
3011 too-high-pa (c-state-cache-top-lparen)
3012 c-state-cache (cdr c-state-cache)))
3014 ;; Do we need to add in an earlier brace pair, having lopped one off?
3015 (if (and dropped-cons
3016 (< too-high-pa (+ here c-state-cache-too-far)))
3017 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
3018 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3019 (c-state-get-min-scan-pos)))))
3021 ;; The brace-pair desert marker:
3022 (when (car c-state-brace-pair-desert)
3023 (if (< here (car c-state-brace-pair-desert))
3024 (setq c-state-brace-pair-desert nil)
3025 (if (< here (cdr c-state-brace-pair-desert))
3026 (setcdr c-state-brace-pair-desert here)))))
3028 (defun c-parse-state-1 ()
3029 ;; Find and record all noteworthy parens between some good point earlier in
3030 ;; the file and point. That good point is at least the beginning of the
3031 ;; top-level construct we are in, or the beginning of the preceding
3032 ;; top-level construct if we aren't in one.
3034 ;; The returned value is a list of the noteworthy parens with the last one
3035 ;; first. If an element in the list is an integer, it's the position of an
3036 ;; open paren (of any type) which has not been closed before the point. If
3037 ;; an element is a cons, it gives the position of a closed BRACE paren
3038 ;; pair[*]; the car is the start brace position and the cdr is the position
3039 ;; following the closing brace. Only the last closed brace paren pair
3040 ;; before each open paren and before the point is recorded, and thus the
3041 ;; state never contains two cons elements in succession. When a close brace
3042 ;; has no matching open brace (e.g., the matching brace is outside the
3043 ;; visible region), it is not represented in the returned value.
3045 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3046 ;; This defun explicitly treats mismatching parens/braces/brackets as
3047 ;; matching. It is the open brace which makes it a "brace" pair.
3049 ;; If POINT is within a macro, open parens and brace pairs within
3050 ;; THIS macro MIGHT be recorded. This depends on whether their
3051 ;; syntactic properties have been suppressed by
3052 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3054 ;; Currently no characters which are given paren syntax with the
3055 ;; syntax-table property are recorded, i.e. angle bracket arglist
3056 ;; parens are never present here. Note that this might change.
3058 ;; BUG: This function doesn't cope entirely well with unbalanced
3059 ;; parens in macros. (2008-12-11: this has probably been resolved
3060 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3061 ;; following case the brace before the macro isn't balanced with the
3062 ;; one after it:
3064 ;; {
3065 ;; #define X {
3066 ;; }
3068 ;; Note to maintainers: this function DOES get called with point
3069 ;; within comments and strings, so don't assume it doesn't!
3071 ;; This function might do hidden buffer changes.
3072 (let* ((here (point))
3073 (here-bopl (c-point 'bopl))
3074 strategy ; 'forward, 'backward etc..
3075 ;; Candidate positions to start scanning from:
3076 cache-pos ; highest position below HERE already existing in
3077 ; cache (or 1).
3078 good-pos
3079 start-point
3080 bopl-state
3082 scan-backward-pos scan-forward-p) ; used for 'backward.
3083 ;; If POINT-MIN has changed, adjust the cache
3084 (unless (= (point-min) c-state-point-min)
3085 (c-renarrow-state-cache))
3087 ;; Strategy?
3088 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3089 strategy (car res)
3090 cache-pos (cadr res)
3091 start-point (nth 2 res))
3093 (when (eq strategy 'BOD)
3094 (setq c-state-cache nil
3095 c-state-cache-good-pos start-point))
3097 ;; SCAN!
3098 (save-restriction
3099 (cond
3100 ((memq strategy '(forward BOD))
3101 (narrow-to-region (point-min) here)
3102 (setq res (c-remove-stale-state-cache start-point here-bopl))
3103 (setq cache-pos (car res)
3104 scan-backward-pos (cadr res)
3105 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
3106 ; start-point)
3107 (if scan-backward-pos
3108 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
3109 (setq good-pos
3110 (c-append-to-state-cache cache-pos))
3111 (setq c-state-cache-good-pos
3112 (if (and bopl-state
3113 (< good-pos (- here c-state-cache-too-far)))
3114 (c-state-cache-non-literal-place here-bopl bopl-state)
3115 good-pos)))
3117 ((eq strategy 'backward)
3118 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
3119 good-pos (car res)
3120 scan-backward-pos (cadr res)
3121 scan-forward-p (car (cddr res)))
3122 (if scan-backward-pos
3123 (c-append-lower-brace-pair-to-state-cache
3124 scan-backward-pos))
3125 (setq c-state-cache-good-pos
3126 (if scan-forward-p
3127 (progn (narrow-to-region (point-min) here)
3128 (c-append-to-state-cache good-pos))
3130 (c-get-cache-scan-pos good-pos))))
3132 (t ; (eq strategy 'IN-LIT)
3133 (setq c-state-cache nil
3134 c-state-cache-good-pos nil)))))
3136 c-state-cache)
3138 (defun c-invalidate-state-cache (here)
3139 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3141 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3142 ;; of all parens in preprocessor constructs, except for any such construct
3143 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3144 ;; worrying further about macros and template delimiters.
3145 (c-with-<->-as-parens-suppressed
3146 (if (and c-state-old-cpp-beg
3147 (< c-state-old-cpp-beg here))
3148 (c-with-all-but-one-cpps-commented-out
3149 c-state-old-cpp-beg
3150 (min c-state-old-cpp-end here)
3151 (c-invalidate-state-cache-1 here))
3152 (c-with-cpps-commented-out
3153 (c-invalidate-state-cache-1 here)))))
3155 (defun c-parse-state ()
3156 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3157 ;; description of the functionality and return value.
3159 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3160 ;; of all parens in preprocessor constructs, except for any such construct
3161 ;; containing point. We can then call `c-parse-state-1' without worrying
3162 ;; further about macros and template delimiters.
3163 (let (here-cpp-beg here-cpp-end)
3164 (save-excursion
3165 (when (c-beginning-of-macro)
3166 (setq here-cpp-beg (point))
3167 (unless
3168 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3169 here-cpp-beg)
3170 (setq here-cpp-beg nil here-cpp-end nil))))
3171 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3172 ;; subsystem.
3173 (prog1
3174 (c-with-<->-as-parens-suppressed
3175 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3176 (c-with-all-but-one-cpps-commented-out
3177 here-cpp-beg here-cpp-end
3178 (c-parse-state-1))
3179 (c-with-cpps-commented-out
3180 (c-parse-state-1))))
3181 (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
3182 c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
3185 ;; Debug tool to catch cache inconsistencies. This is called from
3186 ;; 000tests.el.
3187 (defvar c-debug-parse-state nil)
3188 (unless (fboundp 'c-real-parse-state)
3189 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3190 (cc-bytecomp-defun c-real-parse-state)
3192 (defvar c-parse-state-state nil)
3193 (defun c-record-parse-state-state ()
3194 (setq c-parse-state-state
3195 (mapcar
3196 (lambda (arg)
3197 (cons arg (symbol-value arg)))
3198 '(c-state-cache
3199 c-state-cache-good-pos
3200 c-state-nonlit-pos-cache
3201 c-state-nonlit-pos-cache-limit
3202 c-state-brace-pair-desert
3203 c-state-point-min
3204 c-state-point-min-lit-type
3205 c-state-point-min-lit-start
3206 c-state-min-scan-pos
3207 c-state-old-cpp-beg
3208 c-state-old-cpp-end))))
3209 (defun c-replay-parse-state-state ()
3210 (message
3211 (concat "(setq "
3212 (mapconcat
3213 (lambda (arg)
3214 (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
3215 c-parse-state-state " ")
3216 ")")))
3218 (defun c-debug-parse-state ()
3219 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3220 (let ((c-state-cache nil)
3221 (c-state-cache-good-pos 1)
3222 (c-state-nonlit-pos-cache nil)
3223 (c-state-nonlit-pos-cache-limit 1)
3224 (c-state-brace-pair-desert nil)
3225 (c-state-point-min 1)
3226 (c-state-point-min-lit-type nil)
3227 (c-state-point-min-lit-start nil)
3228 (c-state-min-scan-pos 1)
3229 (c-state-old-cpp-beg nil)
3230 (c-state-old-cpp-end nil))
3231 (setq res2 (c-real-parse-state)))
3232 (unless (equal res1 res2)
3233 ;; The cache can actually go further back due to the ad-hoc way
3234 ;; the first paren is found, so try to whack off a bit of its
3235 ;; start before complaining.
3236 ;; (save-excursion
3237 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3238 ;; (c-beginning-of-defun-1)
3239 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3240 ;; (c-beginning-of-defun-1))
3241 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3242 ;; (message (concat "c-parse-state inconsistency at %s: "
3243 ;; "using cache: %s, from scratch: %s")
3244 ;; here res1 res2)))
3245 (message (concat "c-parse-state inconsistency at %s: "
3246 "using cache: %s, from scratch: %s")
3247 here res1 res2)
3248 (message "Old state:")
3249 (c-replay-parse-state-state))
3250 (c-record-parse-state-state)
3251 res1))
3253 (defun c-toggle-parse-state-debug (&optional arg)
3254 (interactive "P")
3255 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3256 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3257 'c-debug-parse-state
3258 'c-real-parse-state)))
3259 (c-keep-region-active))
3260 (when c-debug-parse-state
3261 (c-toggle-parse-state-debug 1))
3264 (defun c-whack-state-before (bufpos paren-state)
3265 ;; Whack off any state information from PAREN-STATE which lies
3266 ;; before BUFPOS. Not destructive on PAREN-STATE.
3267 (let* ((newstate (list nil))
3268 (ptr newstate)
3269 car)
3270 (while paren-state
3271 (setq car (car paren-state)
3272 paren-state (cdr paren-state))
3273 (if (< (if (consp car) (car car) car) bufpos)
3274 (setq paren-state nil)
3275 (setcdr ptr (list car))
3276 (setq ptr (cdr ptr))))
3277 (cdr newstate)))
3279 (defun c-whack-state-after (bufpos paren-state)
3280 ;; Whack off any state information from PAREN-STATE which lies at or
3281 ;; after BUFPOS. Not destructive on PAREN-STATE.
3282 (catch 'done
3283 (while paren-state
3284 (let ((car (car paren-state)))
3285 (if (consp car)
3286 ;; just check the car, because in a balanced brace
3287 ;; expression, it must be impossible for the corresponding
3288 ;; close brace to be before point, but the open brace to
3289 ;; be after.
3290 (if (<= bufpos (car car))
3291 nil ; whack it off
3292 (if (< bufpos (cdr car))
3293 ;; its possible that the open brace is before
3294 ;; bufpos, but the close brace is after. In that
3295 ;; case, convert this to a non-cons element. The
3296 ;; rest of the state is before bufpos, so we're
3297 ;; done.
3298 (throw 'done (cons (car car) (cdr paren-state)))
3299 ;; we know that both the open and close braces are
3300 ;; before bufpos, so we also know that everything else
3301 ;; on state is before bufpos.
3302 (throw 'done paren-state)))
3303 (if (<= bufpos car)
3304 nil ; whack it off
3305 ;; it's before bufpos, so everything else should too.
3306 (throw 'done paren-state)))
3307 (setq paren-state (cdr paren-state)))
3308 nil)))
3310 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3311 ;; Return the bufpos of the innermost enclosing open paren before
3312 ;; bufpos, or nil if none was found.
3313 (let (enclosingp)
3314 (or bufpos (setq bufpos 134217727))
3315 (while paren-state
3316 (setq enclosingp (car paren-state)
3317 paren-state (cdr paren-state))
3318 (if (or (consp enclosingp)
3319 (>= enclosingp bufpos))
3320 (setq enclosingp nil)
3321 (setq paren-state nil)))
3322 enclosingp))
3324 (defun c-least-enclosing-brace (paren-state)
3325 ;; Return the bufpos of the outermost enclosing open paren, or nil
3326 ;; if none was found.
3327 (let (pos elem)
3328 (while paren-state
3329 (setq elem (car paren-state)
3330 paren-state (cdr paren-state))
3331 (if (integerp elem)
3332 (setq pos elem)))
3333 pos))
3335 (defun c-safe-position (bufpos paren-state)
3336 ;; Return the closest "safe" position recorded on PAREN-STATE that
3337 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3338 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3339 ;; find the closest limit before a given limit that might be nil.
3341 ;; A "safe" position is a position at or after a recorded open
3342 ;; paren, or after a recorded close paren. The returned position is
3343 ;; thus either the first position after a close brace, or the first
3344 ;; position after an enclosing paren, or at the enclosing paren in
3345 ;; case BUFPOS is immediately after it.
3346 (when bufpos
3347 (let (elem)
3348 (catch 'done
3349 (while paren-state
3350 (setq elem (car paren-state))
3351 (if (consp elem)
3352 (cond ((< (cdr elem) bufpos)
3353 (throw 'done (cdr elem)))
3354 ((< (car elem) bufpos)
3355 ;; See below.
3356 (throw 'done (min (1+ (car elem)) bufpos))))
3357 (if (< elem bufpos)
3358 ;; elem is the position at and not after the opening paren, so
3359 ;; we can go forward one more step unless it's equal to
3360 ;; bufpos. This is useful in some cases avoid an extra paren
3361 ;; level between the safe position and bufpos.
3362 (throw 'done (min (1+ elem) bufpos))))
3363 (setq paren-state (cdr paren-state)))))))
3365 (defun c-beginning-of-syntax ()
3366 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3367 ;; goes to the closest previous point that is known to be outside
3368 ;; any string literal or comment. `c-state-cache' is used if it has
3369 ;; a position in the vicinity.
3370 (let* ((paren-state c-state-cache)
3371 elem
3373 (pos (catch 'done
3374 ;; Note: Similar code in `c-safe-position'. The
3375 ;; difference is that we accept a safe position at
3376 ;; the point and don't bother to go forward past open
3377 ;; parens.
3378 (while paren-state
3379 (setq elem (car paren-state))
3380 (if (consp elem)
3381 (cond ((<= (cdr elem) (point))
3382 (throw 'done (cdr elem)))
3383 ((<= (car elem) (point))
3384 (throw 'done (car elem))))
3385 (if (<= elem (point))
3386 (throw 'done elem)))
3387 (setq paren-state (cdr paren-state)))
3388 (point-min))))
3390 (if (> pos (- (point) 4000))
3391 (goto-char pos)
3392 ;; The position is far back. Try `c-beginning-of-defun-1'
3393 ;; (although we can't be entirely sure it will go to a position
3394 ;; outside a comment or string in current emacsen). FIXME:
3395 ;; Consult `syntax-ppss' here.
3396 (c-beginning-of-defun-1)
3397 (if (< (point) pos)
3398 (goto-char pos)))))
3401 ;; Tools for scanning identifiers and other tokens.
3403 (defun c-on-identifier ()
3404 "Return non-nil if the point is on or directly after an identifier.
3405 Keywords are recognized and not considered identifiers. If an
3406 identifier is detected, the returned value is its starting position.
3407 If an identifier ends at the point and another begins at it \(can only
3408 happen in Pike) then the point for the preceding one is returned.
3410 Note that this function might do hidden buffer changes. See the
3411 comment at the start of cc-engine.el for more info."
3413 ;; FIXME: Shouldn't this function handle "operator" in C++?
3415 (save-excursion
3416 (skip-syntax-backward "w_")
3420 ;; Check for a normal (non-keyword) identifier.
3421 (and (looking-at c-symbol-start)
3422 (not (looking-at c-keywords-regexp))
3423 (point))
3425 (when (c-major-mode-is 'pike-mode)
3426 ;; Handle the `<operator> syntax in Pike.
3427 (let ((pos (point)))
3428 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3429 (and (if (< (skip-chars-backward "`") 0)
3431 (goto-char pos)
3432 (eq (char-after) ?\`))
3433 (looking-at c-symbol-key)
3434 (>= (match-end 0) pos)
3435 (point))))
3437 ;; Handle the "operator +" syntax in C++.
3438 (when (and c-overloadable-operators-regexp
3439 (= (c-backward-token-2 0) 0))
3441 (cond ((and (looking-at c-overloadable-operators-regexp)
3442 (or (not c-opt-op-identifier-prefix)
3443 (and (= (c-backward-token-2 1) 0)
3444 (looking-at c-opt-op-identifier-prefix))))
3445 (point))
3447 ((save-excursion
3448 (and c-opt-op-identifier-prefix
3449 (looking-at c-opt-op-identifier-prefix)
3450 (= (c-forward-token-2 1) 0)
3451 (looking-at c-overloadable-operators-regexp)))
3452 (point))))
3456 (defsubst c-simple-skip-symbol-backward ()
3457 ;; If the point is at the end of a symbol then skip backward to the
3458 ;; beginning of it. Don't move otherwise. Return non-nil if point
3459 ;; moved.
3461 ;; This function might do hidden buffer changes.
3462 (or (< (skip-syntax-backward "w_") 0)
3463 (and (c-major-mode-is 'pike-mode)
3464 ;; Handle the `<operator> syntax in Pike.
3465 (let ((pos (point)))
3466 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3467 (< (skip-chars-backward "`") 0)
3468 (looking-at c-symbol-key)
3469 (>= (match-end 0) pos))
3471 (goto-char pos)
3472 nil)))))
3474 (defun c-beginning-of-current-token (&optional back-limit)
3475 ;; Move to the beginning of the current token. Do not move if not
3476 ;; in the middle of one. BACK-LIMIT may be used to bound the
3477 ;; backward search; if given it's assumed to be at the boundary
3478 ;; between two tokens. Return non-nil if the point is moved, nil
3479 ;; otherwise.
3481 ;; This function might do hidden buffer changes.
3482 (let ((start (point)))
3483 (if (looking-at "\\w\\|\\s_")
3484 (skip-syntax-backward "w_" back-limit)
3485 (when (< (skip-syntax-backward ".()" back-limit) 0)
3486 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3487 (match-end 0))
3488 ;; `c-nonsymbol-token-regexp' should always match
3489 ;; since we've skipped backward over punctuator
3490 ;; or paren syntax, but consume one char in case
3491 ;; it doesn't so that we don't leave point before
3492 ;; some earlier incorrect token.
3493 (1+ (point)))))
3494 (if (<= pos start)
3495 (goto-char pos))))))
3496 (< (point) start)))
3498 (defun c-end-of-current-token (&optional back-limit)
3499 ;; Move to the end of the current token. Do not move if not in the
3500 ;; middle of one. BACK-LIMIT may be used to bound the backward
3501 ;; search; if given it's assumed to be at the boundary between two
3502 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3504 ;; This function might do hidden buffer changes.
3505 (let ((start (point)))
3506 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3507 (skip-syntax-forward "w_"))
3508 ((< (skip-syntax-backward ".()" back-limit) 0)
3509 (while (progn
3510 (if (looking-at c-nonsymbol-token-regexp)
3511 (goto-char (match-end 0))
3512 ;; `c-nonsymbol-token-regexp' should always match since
3513 ;; we've skipped backward over punctuator or paren
3514 ;; syntax, but move forward in case it doesn't so that
3515 ;; we don't leave point earlier than we started with.
3516 (forward-char))
3517 (< (point) start)))))
3518 (> (point) start)))
3520 (defconst c-jump-syntax-balanced
3521 (if (memq 'gen-string-delim c-emacs-features)
3522 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3523 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3525 (defconst c-jump-syntax-unbalanced
3526 (if (memq 'gen-string-delim c-emacs-features)
3527 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3528 "\\w\\|\\s_\\|\\s\""))
3530 (defun c-forward-token-2 (&optional count balanced limit)
3531 "Move forward by tokens.
3532 A token is defined as all symbols and identifiers which aren't
3533 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3534 treated properly). Point is always either left at the beginning of a
3535 token or not moved at all. COUNT specifies the number of tokens to
3536 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3537 moves to the next token beginning only if not already at one. If
3538 BALANCED is true, move over balanced parens, otherwise move into them.
3539 Also, if BALANCED is true, never move out of an enclosing paren.
3541 LIMIT sets the limit for the movement and defaults to the point limit.
3542 The case when LIMIT is set in the middle of a token, comment or macro
3543 is handled correctly, i.e. the point won't be left there.
3545 Return the number of tokens left to move \(positive or negative). If
3546 BALANCED is true, a move over a balanced paren counts as one. Note
3547 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3548 be returned. Thus, a return value of 0 guarantees that point is at
3549 the requested position and a return value less \(without signs) than
3550 COUNT guarantees that point is at the beginning of some token.
3552 Note that this function might do hidden buffer changes. See the
3553 comment at the start of cc-engine.el for more info."
3555 (or count (setq count 1))
3556 (if (< count 0)
3557 (- (c-backward-token-2 (- count) balanced limit))
3559 (let ((jump-syntax (if balanced
3560 c-jump-syntax-balanced
3561 c-jump-syntax-unbalanced))
3562 (last (point))
3563 (prev (point)))
3565 (if (zerop count)
3566 ;; If count is zero we should jump if in the middle of a token.
3567 (c-end-of-current-token))
3569 (save-restriction
3570 (if limit (narrow-to-region (point-min) limit))
3571 (if (/= (point)
3572 (progn (c-forward-syntactic-ws) (point)))
3573 ;; Skip whitespace. Count this as a move if we did in
3574 ;; fact move.
3575 (setq count (max (1- count) 0)))
3577 (if (eobp)
3578 ;; Moved out of bounds. Make sure the returned count isn't zero.
3579 (progn
3580 (if (zerop count) (setq count 1))
3581 (goto-char last))
3583 ;; Use `condition-case' to avoid having the limit tests
3584 ;; inside the loop.
3585 (condition-case nil
3586 (while (and
3587 (> count 0)
3588 (progn
3589 (setq last (point))
3590 (cond ((looking-at jump-syntax)
3591 (goto-char (scan-sexps (point) 1))
3593 ((looking-at c-nonsymbol-token-regexp)
3594 (goto-char (match-end 0))
3596 ;; `c-nonsymbol-token-regexp' above should always
3597 ;; match if there are correct tokens. Try to
3598 ;; widen to see if the limit was set in the
3599 ;; middle of one, else fall back to treating
3600 ;; the offending thing as a one character token.
3601 ((and limit
3602 (save-restriction
3603 (widen)
3604 (looking-at c-nonsymbol-token-regexp)))
3605 nil)
3607 (forward-char)
3608 t))))
3609 (c-forward-syntactic-ws)
3610 (setq prev last
3611 count (1- count)))
3612 (error (goto-char last)))
3614 (when (eobp)
3615 (goto-char prev)
3616 (setq count (1+ count)))))
3618 count)))
3620 (defun c-backward-token-2 (&optional count balanced limit)
3621 "Move backward by tokens.
3622 See `c-forward-token-2' for details."
3624 (or count (setq count 1))
3625 (if (< count 0)
3626 (- (c-forward-token-2 (- count) balanced limit))
3628 (or limit (setq limit (point-min)))
3629 (let ((jump-syntax (if balanced
3630 c-jump-syntax-balanced
3631 c-jump-syntax-unbalanced))
3632 (last (point)))
3634 (if (zerop count)
3635 ;; The count is zero so try to skip to the beginning of the
3636 ;; current token.
3637 (if (> (point)
3638 (progn (c-beginning-of-current-token) (point)))
3639 (if (< (point) limit)
3640 ;; The limit is inside the same token, so return 1.
3641 (setq count 1))
3643 ;; We're not in the middle of a token. If there's
3644 ;; whitespace after the point then we must move backward,
3645 ;; so set count to 1 in that case.
3646 (and (looking-at c-syntactic-ws-start)
3647 ;; If we're looking at a '#' that might start a cpp
3648 ;; directive then we have to do a more elaborate check.
3649 (or (/= (char-after) ?#)
3650 (not c-opt-cpp-prefix)
3651 (save-excursion
3652 (and (= (point)
3653 (progn (beginning-of-line)
3654 (looking-at "[ \t]*")
3655 (match-end 0)))
3656 (or (bobp)
3657 (progn (backward-char)
3658 (not (eq (char-before) ?\\)))))))
3659 (setq count 1))))
3661 ;; Use `condition-case' to avoid having to check for buffer
3662 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3663 (condition-case nil
3664 (while (and
3665 (> count 0)
3666 (progn
3667 (c-backward-syntactic-ws)
3668 (backward-char)
3669 (if (looking-at jump-syntax)
3670 (goto-char (scan-sexps (1+ (point)) -1))
3671 ;; This can be very inefficient if there's a long
3672 ;; sequence of operator tokens without any separation.
3673 ;; That doesn't happen in practice, anyway.
3674 (c-beginning-of-current-token))
3675 (>= (point) limit)))
3676 (setq last (point)
3677 count (1- count)))
3678 (error (goto-char last)))
3680 (if (< (point) limit)
3681 (goto-char last))
3683 count)))
3685 (defun c-forward-token-1 (&optional count balanced limit)
3686 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3687 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3688 characters are jumped over character by character. This function is
3689 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3690 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3691 (c-forward-token-2 count balanced limit)))
3693 (defun c-backward-token-1 (&optional count balanced limit)
3694 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3695 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3696 characters are jumped over character by character. This function is
3697 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3698 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3699 (c-backward-token-2 count balanced limit)))
3702 ;; Tools for doing searches restricted to syntactically relevant text.
3704 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3705 paren-level not-inside-token
3706 lookbehind-submatch)
3707 "Like `re-search-forward', but only report matches that are found
3708 in syntactically significant text. I.e. matches in comments, macros
3709 or string literals are ignored. The start point is assumed to be
3710 outside any comment, macro or string literal, or else the content of
3711 that region is taken as syntactically significant text.
3713 If PAREN-LEVEL is non-nil, an additional restriction is added to
3714 ignore matches in nested paren sexps. The search will also not go
3715 outside the current list sexp, which has the effect that if the point
3716 should be moved to BOUND when no match is found \(i.e. NOERROR is
3717 neither nil nor t), then it will be at the closing paren if the end of
3718 the current list sexp is encountered first.
3720 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3721 ignored. Things like multicharacter operators and special symbols
3722 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3723 constants.
3725 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3726 subexpression in REGEXP. The end of that submatch is used as the
3727 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3728 isn't used or if that subexpression didn't match then the start
3729 position of the whole match is used instead. The \"look behind\"
3730 subexpression is never tested before the starting position, so it
3731 might be a good idea to include \\=\\= as a match alternative in it.
3733 Optimization note: Matches might be missed if the \"look behind\"
3734 subexpression can match the end of nonwhite syntactic whitespace,
3735 i.e. the end of comments or cpp directives. This since the function
3736 skips over such things before resuming the search. It's on the other
3737 hand not safe to assume that the \"look behind\" subexpression never
3738 matches syntactic whitespace.
3740 Bug: Unbalanced parens inside cpp directives are currently not handled
3741 correctly \(i.e. they don't get ignored as they should) when
3742 PAREN-LEVEL is set.
3744 Note that this function might do hidden buffer changes. See the
3745 comment at the start of cc-engine.el for more info."
3747 (or bound (setq bound (point-max)))
3748 (if paren-level (setq paren-level -1))
3750 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3752 (let ((start (point))
3754 ;; Start position for the last search.
3755 search-pos
3756 ;; The `parse-partial-sexp' state between the start position
3757 ;; and the point.
3758 state
3759 ;; The current position after the last state update. The next
3760 ;; `parse-partial-sexp' continues from here.
3761 (state-pos (point))
3762 ;; The position at which to check the state and the state
3763 ;; there. This is separate from `state-pos' since we might
3764 ;; need to back up before doing the next search round.
3765 check-pos check-state
3766 ;; Last position known to end a token.
3767 (last-token-end-pos (point-min))
3768 ;; Set when a valid match is found.
3769 found)
3771 (condition-case err
3772 (while
3773 (and
3774 (progn
3775 (setq search-pos (point))
3776 (re-search-forward regexp bound noerror))
3778 (progn
3779 (setq state (parse-partial-sexp
3780 state-pos (match-beginning 0) paren-level nil state)
3781 state-pos (point))
3782 (if (setq check-pos (and lookbehind-submatch
3783 (or (not paren-level)
3784 (>= (car state) 0))
3785 (match-end lookbehind-submatch)))
3786 (setq check-state (parse-partial-sexp
3787 state-pos check-pos paren-level nil state))
3788 (setq check-pos state-pos
3789 check-state state))
3791 ;; NOTE: If we got a look behind subexpression and get
3792 ;; an insignificant match in something that isn't
3793 ;; syntactic whitespace (i.e. strings or in nested
3794 ;; parentheses), then we can never skip more than a
3795 ;; single character from the match start position
3796 ;; (i.e. `state-pos' here) before continuing the
3797 ;; search. That since the look behind subexpression
3798 ;; might match the end of the insignificant region in
3799 ;; the next search.
3801 (cond
3802 ((elt check-state 7)
3803 ;; Match inside a line comment. Skip to eol. Use
3804 ;; `re-search-forward' instead of `skip-chars-forward' to get
3805 ;; the right bound behavior.
3806 (re-search-forward "[\n\r]" bound noerror))
3808 ((elt check-state 4)
3809 ;; Match inside a block comment. Skip to the '*/'.
3810 (search-forward "*/" bound noerror))
3812 ((and (not (elt check-state 5))
3813 (eq (char-before check-pos) ?/)
3814 (not (c-get-char-property (1- check-pos) 'syntax-table))
3815 (memq (char-after check-pos) '(?/ ?*)))
3816 ;; Match in the middle of the opener of a block or line
3817 ;; comment.
3818 (if (= (char-after check-pos) ?/)
3819 (re-search-forward "[\n\r]" bound noerror)
3820 (search-forward "*/" bound noerror)))
3822 ;; The last `parse-partial-sexp' above might have
3823 ;; stopped short of the real check position if the end
3824 ;; of the current sexp was encountered in paren-level
3825 ;; mode. The checks above are always false in that
3826 ;; case, and since they can do better skipping in
3827 ;; lookbehind-submatch mode, we do them before
3828 ;; checking the paren level.
3830 ((and paren-level
3831 (/= (setq tmp (car check-state)) 0))
3832 ;; Check the paren level first since we're short of the
3833 ;; syntactic checking position if the end of the
3834 ;; current sexp was encountered by `parse-partial-sexp'.
3835 (if (> tmp 0)
3837 ;; Inside a nested paren sexp.
3838 (if lookbehind-submatch
3839 ;; See the NOTE above.
3840 (progn (goto-char state-pos) t)
3841 ;; Skip out of the paren quickly.
3842 (setq state (parse-partial-sexp state-pos bound 0 nil state)
3843 state-pos (point)))
3845 ;; Have exited the current paren sexp.
3846 (if noerror
3847 (progn
3848 ;; The last `parse-partial-sexp' call above
3849 ;; has left us just after the closing paren
3850 ;; in this case, so we can modify the bound
3851 ;; to leave the point at the right position
3852 ;; upon return.
3853 (setq bound (1- (point)))
3854 nil)
3855 (signal 'search-failed (list regexp)))))
3857 ((setq tmp (elt check-state 3))
3858 ;; Match inside a string.
3859 (if (or lookbehind-submatch
3860 (not (integerp tmp)))
3861 ;; See the NOTE above.
3862 (progn (goto-char state-pos) t)
3863 ;; Skip to the end of the string before continuing.
3864 (let ((ender (make-string 1 tmp)) (continue t))
3865 (while (if (search-forward ender bound noerror)
3866 (progn
3867 (setq state (parse-partial-sexp
3868 state-pos (point) nil nil state)
3869 state-pos (point))
3870 (elt state 3))
3871 (setq continue nil)))
3872 continue)))
3874 ((save-excursion
3875 (save-match-data
3876 (c-beginning-of-macro start)))
3877 ;; Match inside a macro. Skip to the end of it.
3878 (c-end-of-macro)
3879 (cond ((<= (point) bound) t)
3880 (noerror nil)
3881 (t (signal 'search-failed (list regexp)))))
3883 ((and not-inside-token
3884 (or (< check-pos last-token-end-pos)
3885 (< check-pos
3886 (save-excursion
3887 (goto-char check-pos)
3888 (save-match-data
3889 (c-end-of-current-token last-token-end-pos))
3890 (setq last-token-end-pos (point))))))
3891 ;; Inside a token.
3892 (if lookbehind-submatch
3893 ;; See the NOTE above.
3894 (goto-char state-pos)
3895 (goto-char (min last-token-end-pos bound))))
3898 ;; A real match.
3899 (setq found t)
3900 nil)))
3902 ;; Should loop to search again, but take care to avoid
3903 ;; looping on the same spot.
3904 (or (/= search-pos (point))
3905 (if (= (point) bound)
3906 (if noerror
3908 (signal 'search-failed (list regexp)))
3909 (forward-char)
3910 t))))
3912 (error
3913 (goto-char start)
3914 (signal (car err) (cdr err))))
3916 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3918 (if found
3919 (progn
3920 (goto-char (match-end 0))
3921 (match-end 0))
3923 ;; Search failed. Set point as appropriate.
3924 (if (eq noerror t)
3925 (goto-char start)
3926 (goto-char bound))
3927 nil)))
3929 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
3931 (defsubst c-ssb-lit-begin ()
3932 ;; Return the start of the literal point is in, or nil.
3933 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3934 ;; bound in the caller.
3936 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3937 ;; if it's outside comments and strings.
3938 (save-excursion
3939 (let ((pos (point)) safe-pos state pps-end-pos)
3940 ;; Pick a safe position as close to the point as possible.
3942 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3943 ;; position.
3945 (while (and safe-pos-list
3946 (> (car safe-pos-list) (point)))
3947 (setq safe-pos-list (cdr safe-pos-list)))
3948 (unless (setq safe-pos (car-safe safe-pos-list))
3949 (setq safe-pos (max (or (c-safe-position
3950 (point) (or c-state-cache
3951 (c-parse-state)))
3953 (point-min))
3954 safe-pos-list (list safe-pos)))
3956 ;; Cache positions along the way to use if we have to back up more. We
3957 ;; cache every closing paren on the same level. If the paren cache is
3958 ;; relevant in this region then we're typically already on the same
3959 ;; level as the target position. Note that we might cache positions
3960 ;; after opening parens in case safe-pos is in a nested list. That's
3961 ;; both uncommon and harmless.
3962 (while (progn
3963 (setq state (parse-partial-sexp
3964 safe-pos pos 0))
3965 (< (point) pos))
3966 (setq safe-pos (point)
3967 safe-pos-list (cons safe-pos safe-pos-list)))
3969 ;; If the state contains the start of the containing sexp we cache that
3970 ;; position too, so that parse-partial-sexp in the next run has a bigger
3971 ;; chance of starting at the same level as the target position and thus
3972 ;; will get more good safe positions into the list.
3973 (if (elt state 1)
3974 (setq safe-pos (1+ (elt state 1))
3975 safe-pos-list (cons safe-pos safe-pos-list)))
3977 (if (or (elt state 3) (elt state 4))
3978 ;; Inside string or comment. Continue search at the
3979 ;; beginning of it.
3980 (elt state 8)))))
3982 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3983 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3984 i.e. don't stop at positions inside syntactic whitespace or string
3985 literals. Preprocessor directives are also ignored, with the exception
3986 of the one that the point starts within, if any. If LIMIT is given,
3987 it's assumed to be at a syntactically relevant position.
3989 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3990 sexps, and the search will also not go outside the current paren sexp.
3991 However, if LIMIT or the buffer limit is reached inside a nested paren
3992 then the point will be left at the limit.
3994 Non-nil is returned if the point moved, nil otherwise.
3996 Note that this function might do hidden buffer changes. See the
3997 comment at the start of cc-engine.el for more info."
3999 (let ((start (point))
4000 state-2
4001 ;; A list of syntactically relevant positions in descending
4002 ;; order. It's used to avoid scanning repeatedly over
4003 ;; potentially large regions with `parse-partial-sexp' to verify
4004 ;; each position. Used in `c-ssb-lit-begin'
4005 safe-pos-list
4006 ;; The result from `c-beginning-of-macro' at the start position or the
4007 ;; start position itself if it isn't within a macro. Evaluated on
4008 ;; demand.
4009 start-macro-beg
4010 ;; The earliest position after the current one with the same paren
4011 ;; level. Used only when `paren-level' is set.
4012 lit-beg
4013 (paren-level-pos (point)))
4015 (while
4016 (progn
4017 ;; The next loop "tries" to find the end point each time round,
4018 ;; loops when it hasn't succeeded.
4019 (while
4020 (and
4021 (< (skip-chars-backward skip-chars limit) 0)
4023 (let ((pos (point)) state-2 pps-end-pos)
4025 (cond
4026 ;; Don't stop inside a literal
4027 ((setq lit-beg (c-ssb-lit-begin))
4028 (goto-char lit-beg)
4031 ((and paren-level
4032 (save-excursion
4033 (setq state-2 (parse-partial-sexp
4034 pos paren-level-pos -1)
4035 pps-end-pos (point))
4036 (/= (car state-2) 0)))
4037 ;; Not at the right level.
4039 (if (and (< (car state-2) 0)
4040 ;; We stop above if we go out of a paren.
4041 ;; Now check whether it precedes or is
4042 ;; nested in the starting sexp.
4043 (save-excursion
4044 (setq state-2
4045 (parse-partial-sexp
4046 pps-end-pos paren-level-pos
4047 nil nil state-2))
4048 (< (car state-2) 0)))
4050 ;; We've stopped short of the starting position
4051 ;; so the hit was inside a nested list. Go up
4052 ;; until we are at the right level.
4053 (condition-case nil
4054 (progn
4055 (goto-char (scan-lists pos -1
4056 (- (car state-2))))
4057 (setq paren-level-pos (point))
4058 (if (and limit (>= limit paren-level-pos))
4059 (progn
4060 (goto-char limit)
4061 nil)
4063 (error
4064 (goto-char (or limit (point-min)))
4065 nil))
4067 ;; The hit was outside the list at the start
4068 ;; position. Go to the start of the list and exit.
4069 (goto-char (1+ (elt state-2 1)))
4070 nil))
4072 ((c-beginning-of-macro limit)
4073 ;; Inside a macro.
4074 (if (< (point)
4075 (or start-macro-beg
4076 (setq start-macro-beg
4077 (save-excursion
4078 (goto-char start)
4079 (c-beginning-of-macro limit)
4080 (point)))))
4083 ;; It's inside the same macro we started in so it's
4084 ;; a relevant match.
4085 (goto-char pos)
4086 nil))))))
4088 (> (point)
4089 (progn
4090 ;; Skip syntactic ws afterwards so that we don't stop at the
4091 ;; end of a comment if `skip-chars' is something like "^/".
4092 (c-backward-syntactic-ws)
4093 (point)))))
4095 ;; We might want to extend this with more useful return values in
4096 ;; the future.
4097 (/= (point) start)))
4099 ;; The following is an alternative implementation of
4100 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4101 ;; track of the syntactic context. It turned out to be generally
4102 ;; slower than the one above which uses forward checks from earlier
4103 ;; safe positions.
4105 ;;(defconst c-ssb-stop-re
4106 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4107 ;; ;; stop at to avoid going into comments and literals.
4108 ;; (concat
4109 ;; ;; Match comment end syntax and string literal syntax. Also match
4110 ;; ;; '/' for block comment endings (not covered by comment end
4111 ;; ;; syntax).
4112 ;; "\\s>\\|/\\|\\s\""
4113 ;; (if (memq 'gen-string-delim c-emacs-features)
4114 ;; "\\|\\s|"
4115 ;; "")
4116 ;; (if (memq 'gen-comment-delim c-emacs-features)
4117 ;; "\\|\\s!"
4118 ;; "")))
4120 ;;(defconst c-ssb-stop-paren-re
4121 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4122 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4124 ;;(defconst c-ssb-sexp-end-re
4125 ;; ;; Regexp matching the ending syntax of a complex sexp.
4126 ;; (concat c-string-limit-regexp "\\|\\s)"))
4128 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4129 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4130 ;;i.e. don't stop at positions inside syntactic whitespace or string
4131 ;;literals. Preprocessor directives are also ignored. However, if the
4132 ;;point is within a comment, string literal or preprocessor directory to
4133 ;;begin with, its contents is treated as syntactically relevant chars.
4134 ;;If LIMIT is given, it limits the backward search and the point will be
4135 ;;left there if no earlier position is found.
4137 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4138 ;;sexps, and the search will also not go outside the current paren sexp.
4139 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4140 ;;then the point will be left at the limit.
4142 ;;Non-nil is returned if the point moved, nil otherwise.
4144 ;;Note that this function might do hidden buffer changes. See the
4145 ;;comment at the start of cc-engine.el for more info."
4147 ;; (save-restriction
4148 ;; (when limit
4149 ;; (narrow-to-region limit (point-max)))
4151 ;; (let ((start (point)))
4152 ;; (catch 'done
4153 ;; (while (let ((last-pos (point))
4154 ;; (stop-pos (progn
4155 ;; (skip-chars-backward skip-chars)
4156 ;; (point))))
4158 ;; ;; Skip back over the same region as
4159 ;; ;; `skip-chars-backward' above, but keep to
4160 ;; ;; syntactically relevant positions.
4161 ;; (goto-char last-pos)
4162 ;; (while (and
4163 ;; ;; `re-search-backward' with a single char regexp
4164 ;; ;; should be fast.
4165 ;; (re-search-backward
4166 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4167 ;; stop-pos 'move)
4169 ;; (progn
4170 ;; (cond
4171 ;; ((looking-at "\\s(")
4172 ;; ;; `paren-level' is set and we've found the
4173 ;; ;; start of the containing paren.
4174 ;; (forward-char)
4175 ;; (throw 'done t))
4177 ;; ((looking-at c-ssb-sexp-end-re)
4178 ;; ;; We're at the end of a string literal or paren
4179 ;; ;; sexp (if `paren-level' is set).
4180 ;; (forward-char)
4181 ;; (condition-case nil
4182 ;; (c-backward-sexp)
4183 ;; (error
4184 ;; (goto-char limit)
4185 ;; (throw 'done t))))
4187 ;; (t
4188 ;; (forward-char)
4189 ;; ;; At the end of some syntactic ws or possibly
4190 ;; ;; after a plain '/' operator.
4191 ;; (let ((pos (point)))
4192 ;; (c-backward-syntactic-ws)
4193 ;; (if (= pos (point))
4194 ;; ;; Was a plain '/' operator. Go past it.
4195 ;; (backward-char)))))
4197 ;; (> (point) stop-pos))))
4199 ;; ;; Now the point is either at `stop-pos' or at some
4200 ;; ;; position further back if `stop-pos' was at a
4201 ;; ;; syntactically irrelevant place.
4203 ;; ;; Skip additional syntactic ws so that we don't stop
4204 ;; ;; at the end of a comment if `skip-chars' is
4205 ;; ;; something like "^/".
4206 ;; (c-backward-syntactic-ws)
4208 ;; (< (point) stop-pos))))
4210 ;; ;; We might want to extend this with more useful return values
4211 ;; ;; in the future.
4212 ;; (/= (point) start))))
4215 ;; Tools for handling comments and string literals.
4217 (defun c-in-literal (&optional lim detect-cpp)
4218 "Return the type of literal point is in, if any.
4219 The return value is `c' if in a C-style comment, `c++' if in a C++
4220 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4221 is non-nil and in a preprocessor line, or nil if somewhere else.
4222 Optional LIM is used as the backward limit of the search. If omitted,
4223 or nil, `c-beginning-of-defun' is used.
4225 The last point calculated is cached if the cache is enabled, i.e. if
4226 `c-in-literal-cache' is bound to a two element vector.
4228 Note that this function might do hidden buffer changes. See the
4229 comment at the start of cc-engine.el for more info."
4230 (save-restriction
4231 (widen)
4232 (let* ((safe-place (c-state-safe-place (point)))
4233 (lit (c-state-pp-to-literal safe-place (point))))
4234 (or (cadr lit)
4235 (and detect-cpp
4236 (save-excursion (c-beginning-of-macro))
4237 'pound)))))
4239 (defun c-literal-limits (&optional lim near not-in-delimiter)
4240 "Return a cons of the beginning and end positions of the comment or
4241 string surrounding point (including both delimiters), or nil if point
4242 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4243 to start parsing from. If NEAR is non-nil, then the limits of any
4244 literal next to point is returned. \"Next to\" means there's only
4245 spaces and tabs between point and the literal. The search for such a
4246 literal is done first in forward direction. If NOT-IN-DELIMITER is
4247 non-nil, the case when point is inside a starting delimiter won't be
4248 recognized. This only has effect for comments which have starting
4249 delimiters with more than one character.
4251 Note that this function might do hidden buffer changes. See the
4252 comment at the start of cc-engine.el for more info."
4254 (save-excursion
4255 (let* ((pos (point))
4256 (lim (or lim (c-state-safe-place pos)))
4257 (pp-to-lit (save-restriction
4258 (widen)
4259 (c-state-pp-to-literal lim pos)))
4260 (state (car pp-to-lit))
4261 (lit-limits (car (cddr pp-to-lit))))
4263 (cond
4264 (lit-limits)
4265 ((and (not not-in-delimiter)
4266 (not (elt state 5))
4267 (eq (char-before) ?/)
4268 (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28.
4269 ;; We're standing in a comment starter.
4270 (backward-char 1)
4271 (cons (point) (progn (c-forward-single-comment) (point))))
4273 (near
4274 (goto-char pos)
4275 ;; Search forward for a literal.
4276 (skip-chars-forward " \t")
4277 (cond
4278 ((looking-at c-string-limit-regexp) ; String.
4279 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4280 (point-max))))
4282 ((looking-at c-comment-start-regexp) ; Line or block comment.
4283 (cons (point) (progn (c-forward-single-comment) (point))))
4286 ;; Search backward.
4287 (skip-chars-backward " \t")
4289 (let ((end (point)) beg)
4290 (cond
4291 ((save-excursion
4292 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4293 (setq beg (c-safe (c-backward-sexp 1) (point))))
4295 ((and (c-safe (forward-char -2) t)
4296 (looking-at "*/"))
4297 ;; Block comment. Due to the nature of line
4298 ;; comments, they will always be covered by the
4299 ;; normal case above.
4300 (goto-char end)
4301 (c-backward-single-comment)
4302 ;; If LIM is bogus, beg will be bogus.
4303 (setq beg (point))))
4305 (if beg (cons beg end))))))
4306 ))))
4308 ;; In case external callers use this; it did have a docstring.
4309 (defalias 'c-literal-limits-fast 'c-literal-limits)
4311 (defun c-collect-line-comments (range)
4312 "If the argument is a cons of two buffer positions (such as returned by
4313 `c-literal-limits'), and that range contains a C++ style line comment,
4314 then an extended range is returned that contains all adjacent line
4315 comments (i.e. all comments that starts in the same column with no
4316 empty lines or non-whitespace characters between them). Otherwise the
4317 argument is returned.
4319 Note that this function might do hidden buffer changes. See the
4320 comment at the start of cc-engine.el for more info."
4322 (save-excursion
4323 (condition-case nil
4324 (if (and (consp range) (progn
4325 (goto-char (car range))
4326 (looking-at c-line-comment-starter)))
4327 (let ((col (current-column))
4328 (beg (point))
4329 (bopl (c-point 'bopl))
4330 (end (cdr range)))
4331 ;; Got to take care in the backward direction to handle
4332 ;; comments which are preceded by code.
4333 (while (and (c-backward-single-comment)
4334 (>= (point) bopl)
4335 (looking-at c-line-comment-starter)
4336 (= col (current-column)))
4337 (setq beg (point)
4338 bopl (c-point 'bopl)))
4339 (goto-char end)
4340 (while (and (progn (skip-chars-forward " \t")
4341 (looking-at c-line-comment-starter))
4342 (= col (current-column))
4343 (prog1 (zerop (forward-line 1))
4344 (setq end (point)))))
4345 (cons beg end))
4346 range)
4347 (error range))))
4349 (defun c-literal-type (range)
4350 "Convenience function that given the result of `c-literal-limits',
4351 returns nil or the type of literal that the range surrounds, one
4352 of the symbols 'c, 'c++ or 'string. It's much faster than using
4353 `c-in-literal' and is intended to be used when you need both the
4354 type of a literal and its limits.
4356 Note that this function might do hidden buffer changes. See the
4357 comment at the start of cc-engine.el for more info."
4359 (if (consp range)
4360 (save-excursion
4361 (goto-char (car range))
4362 (cond ((looking-at c-string-limit-regexp) 'string)
4363 ((or (looking-at "//") ; c++ line comment
4364 (and (looking-at "\\s<") ; comment starter
4365 (looking-at "#"))) ; awk comment.
4366 'c++)
4367 (t 'c))) ; Assuming the range is valid.
4368 range))
4370 (defsubst c-determine-limit-get-base (start try-size)
4371 ;; Get a "safe place" approximately TRY-SIZE characters before START.
4372 ;; This doesn't preserve point.
4373 (let* ((pos (max (- start try-size) (point-min)))
4374 (base (c-state-safe-place pos))
4375 (s (parse-partial-sexp base pos)))
4376 (if (or (nth 4 s) (nth 3 s)) ; comment or string
4377 (nth 8 s)
4378 (point))))
4380 (defun c-determine-limit (how-far-back &optional start try-size)
4381 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
4382 ;; (default point). This is done by going back further in the buffer then
4383 ;; searching forward for literals. The position found won't be in a
4384 ;; literal. We start searching for the sought position TRY-SIZE (default
4385 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
4386 ;; :-)
4387 (save-excursion
4388 (let* ((start (or start (point)))
4389 (try-size (or try-size (* 2 how-far-back)))
4390 (base (c-determine-limit-get-base start try-size))
4391 (pos base)
4393 (s (parse-partial-sexp pos pos)) ; null state.
4394 stack elt size
4395 (count 0))
4396 (while (< pos start)
4397 ;; Move forward one literal each time round this loop.
4398 ;; Move forward to the start of a comment or string.
4399 (setq s (parse-partial-sexp
4401 start
4402 nil ; target-depth
4403 nil ; stop-before
4404 s ; state
4405 'syntax-table)) ; stop-comment
4407 ;; Gather details of the non-literal-bit - starting pos and size.
4408 (setq size (- (if (or (nth 4 s) (nth 3 s))
4409 (nth 8 s)
4410 (point))
4411 pos))
4412 (if (> size 0)
4413 (setq stack (cons (cons pos size) stack)))
4415 ;; Move forward to the end of the comment/string.
4416 (if (or (nth 4 s) (nth 3 s))
4417 (setq s (parse-partial-sexp
4418 (point)
4419 start
4420 nil ; target-depth
4421 nil ; stop-before
4422 s ; state
4423 'syntax-table))) ; stop-comment
4424 (setq pos (point)))
4426 ;; Now try and find enough non-literal characters recorded on the stack.
4427 ;; Go back one recorded literal each time round this loop.
4428 (while (and (< count how-far-back)
4429 stack)
4430 (setq elt (car stack)
4431 stack (cdr stack))
4432 (setq count (+ count (cdr elt))))
4434 ;; Have we found enough yet?
4435 (cond
4436 ((>= count how-far-back)
4437 (+ (car elt) (- count how-far-back)))
4438 ((eq base (point-min))
4439 (point-min))
4441 (c-determine-limit (- how-far-back count) base try-size))))))
4443 ;; `c-find-decl-spots' and accompanying stuff.
4445 ;; Variables used in `c-find-decl-spots' to cache the search done for
4446 ;; the first declaration in the last call. When that function starts,
4447 ;; it needs to back up over syntactic whitespace to look at the last
4448 ;; token before the region being searched. That can sometimes cause
4449 ;; moves back and forth over a quite large region of comments and
4450 ;; macros, which would be repeated for each changed character when
4451 ;; we're called during fontification, since font-lock refontifies the
4452 ;; current line for each change. Thus it's worthwhile to cache the
4453 ;; first match.
4455 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4456 ;; the syntactic whitespace less or equal to some start position.
4457 ;; There's no cached value if it's nil.
4459 ;; `c-find-decl-match-pos' is the match position if
4460 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4461 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4462 (defvar c-find-decl-syntactic-pos nil)
4463 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4464 (defvar c-find-decl-match-pos nil)
4465 (make-variable-buffer-local 'c-find-decl-match-pos)
4467 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4468 (and c-find-decl-syntactic-pos
4469 (< change-min-pos c-find-decl-syntactic-pos)
4470 (setq c-find-decl-syntactic-pos nil)))
4472 ; (defface c-debug-decl-spot-face
4473 ; '((t (:background "Turquoise")))
4474 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4475 ; (defface c-debug-decl-sws-face
4476 ; '((t (:background "Khaki")))
4477 ; "Debug face to mark the syntactic whitespace between the declaration
4478 ; spots and the preceding token end.")
4480 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4481 (when (facep 'c-debug-decl-spot-face)
4482 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4483 (c-debug-add-face (max match-pos (point-min)) decl-pos
4484 'c-debug-decl-sws-face)
4485 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4486 'c-debug-decl-spot-face))))
4487 (defmacro c-debug-remove-decl-spot-faces (beg end)
4488 (when (facep 'c-debug-decl-spot-face)
4489 `(c-save-buffer-state ()
4490 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4491 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4493 (defmacro c-find-decl-prefix-search ()
4494 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4495 ;; but it contains lots of free variables that refer to things
4496 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4497 ;; if there is a match, otherwise at `cfd-limit'.
4499 ;; This macro might do hidden buffer changes.
4501 '(progn
4502 ;; Find the next property match position if we haven't got one already.
4503 (unless cfd-prop-match
4504 (save-excursion
4505 (while (progn
4506 (goto-char (next-single-property-change
4507 (point) 'c-type nil cfd-limit))
4508 (and (< (point) cfd-limit)
4509 (not (eq (c-get-char-property (1- (point)) 'c-type)
4510 'c-decl-end)))))
4511 (setq cfd-prop-match (point))))
4513 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4514 ;; got one already.
4515 (unless cfd-re-match
4517 (if (> cfd-re-match-end (point))
4518 (goto-char cfd-re-match-end))
4520 (while (if (setq cfd-re-match-end
4521 (re-search-forward c-decl-prefix-or-start-re
4522 cfd-limit 'move))
4524 ;; Match. Check if it's inside a comment or string literal.
4525 (c-got-face-at
4526 (if (setq cfd-re-match (match-end 1))
4527 ;; Matched the end of a token preceding a decl spot.
4528 (progn
4529 (goto-char cfd-re-match)
4530 (1- cfd-re-match))
4531 ;; Matched a token that start a decl spot.
4532 (goto-char (match-beginning 0))
4533 (point))
4534 c-literal-faces)
4536 ;; No match. Finish up and exit the loop.
4537 (setq cfd-re-match cfd-limit)
4538 nil)
4540 ;; Skip out of comments and string literals.
4541 (while (progn
4542 (goto-char (next-single-property-change
4543 (point) 'face nil cfd-limit))
4544 (and (< (point) cfd-limit)
4545 (c-got-face-at (point) c-literal-faces)))))
4547 ;; If we matched at the decl start, we have to back up over the
4548 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4549 ;; any decl spots in the syntactic ws.
4550 (unless cfd-re-match
4551 (c-backward-syntactic-ws)
4552 (setq cfd-re-match (point))))
4554 ;; Choose whichever match is closer to the start.
4555 (if (< cfd-re-match cfd-prop-match)
4556 (setq cfd-match-pos cfd-re-match
4557 cfd-re-match nil)
4558 (setq cfd-match-pos cfd-prop-match
4559 cfd-prop-match nil))
4561 (goto-char cfd-match-pos)
4563 (when (< cfd-match-pos cfd-limit)
4564 ;; Skip forward past comments only so we don't skip macros.
4565 (c-forward-comments)
4566 ;; Set the position to continue at. We can avoid going over
4567 ;; the comments skipped above a second time, but it's possible
4568 ;; that the comment skipping has taken us past `cfd-prop-match'
4569 ;; since the property might be used inside comments.
4570 (setq cfd-continue-pos (if cfd-prop-match
4571 (min cfd-prop-match (point))
4572 (point))))))
4574 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4575 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4576 ;; label from the point to CFD-LIMIT.
4578 ;; CFD-FUN is called with point at the start of the spot. It's passed two
4579 ;; arguments: The first is the end position of the token preceding the spot,
4580 ;; or 0 for the implicit match at bob. The second is a flag that is t when
4581 ;; the match is inside a macro. Point should be moved forward by at least
4582 ;; one token.
4584 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
4585 ;; it should return non-nil to ensure that the next search will find them.
4587 ;; Such a spot is:
4588 ;; o The first token after bob.
4589 ;; o The first token after the end of submatch 1 in
4590 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4591 ;; o The start of each `c-decl-prefix-or-start-re' match when
4592 ;; submatch 1 doesn't match.
4593 ;; o The first token after the end of each occurrence of the
4594 ;; `c-type' text property with the value `c-decl-end', provided
4595 ;; `c-type-decl-end-used' is set.
4597 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4598 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4599 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4601 ;; If the match is inside a macro then the buffer is narrowed to the
4602 ;; end of it, so that CFD-FUN can investigate the following tokens
4603 ;; without matching something that begins inside a macro and ends
4604 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4605 ;; CFD-FACE-CHECKLIST checks exist.
4607 ;; The spots are visited approximately in order from top to bottom.
4608 ;; It's however the positions where `c-decl-prefix-or-start-re'
4609 ;; matches and where `c-decl-end' properties are found that are in
4610 ;; order. Since the spots often are at the following token, they
4611 ;; might be visited out of order insofar as more spots are reported
4612 ;; later on within the syntactic whitespace between the match
4613 ;; positions and their spots.
4615 ;; It's assumed that comments and strings are fontified in the
4616 ;; searched range.
4618 ;; This is mainly used in fontification, and so has an elaborate
4619 ;; cache to handle repeated calls from the same start position; see
4620 ;; the variables above.
4622 ;; All variables in this function begin with `cfd-' to avoid name
4623 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4625 ;; This function might do hidden buffer changes.
4627 (let ((cfd-start-pos (point))
4628 (cfd-buffer-end (point-max))
4629 ;; The end of the token preceding the decl spot last found
4630 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4631 ;; no match.
4632 cfd-re-match
4633 ;; The end position of the last `c-decl-prefix-or-start-re'
4634 ;; match. If this is greater than `cfd-continue-pos', the
4635 ;; next regexp search is started here instead.
4636 (cfd-re-match-end (point-min))
4637 ;; The end of the last `c-decl-end' found by
4638 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4639 ;; match. If searching for the property isn't needed then we
4640 ;; disable it by setting it to `cfd-limit' directly.
4641 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4642 ;; The end of the token preceding the decl spot last found by
4643 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4644 ;; bob. `cfd-limit' if there's no match. In other words,
4645 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4646 (cfd-match-pos cfd-limit)
4647 ;; The position to continue searching at.
4648 cfd-continue-pos
4649 ;; The position of the last "real" token we've stopped at.
4650 ;; This can be greater than `cfd-continue-pos' when we get
4651 ;; hits inside macros or at `c-decl-end' positions inside
4652 ;; comments.
4653 (cfd-token-pos 0)
4654 ;; The end position of the last entered macro.
4655 (cfd-macro-end 0))
4657 ;; Initialize by finding a syntactically relevant start position
4658 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4659 ;; search unless we're at bob.
4661 (let (start-in-literal start-in-macro syntactic-pos)
4662 ;; Must back up a bit since we look for the end of the previous
4663 ;; statement or declaration, which is earlier than the first
4664 ;; returned match.
4666 (cond
4667 ;; First we need to move to a syntactically relevant position.
4668 ;; Begin by backing out of comment or string literals.
4669 ((and
4670 (when (c-got-face-at (point) c-literal-faces)
4671 ;; Try to use the faces to back up to the start of the
4672 ;; literal. FIXME: What if the point is on a declaration
4673 ;; inside a comment?
4674 (while (and (not (bobp))
4675 (c-got-face-at (1- (point)) c-literal-faces))
4676 (goto-char (previous-single-property-change
4677 (point) 'face nil (point-min))))
4679 ;; XEmacs doesn't fontify the quotes surrounding string
4680 ;; literals.
4681 (and (featurep 'xemacs)
4682 (eq (get-text-property (point) 'face)
4683 'font-lock-string-face)
4684 (not (bobp))
4685 (progn (backward-char)
4686 (not (looking-at c-string-limit-regexp)))
4687 (forward-char))
4689 ;; Don't trust the literal to contain only literal faces
4690 ;; (the font lock package might not have fontified the
4691 ;; start of it at all, for instance) so check that we have
4692 ;; arrived at something that looks like a start or else
4693 ;; resort to `c-literal-limits'.
4694 (unless (looking-at c-literal-start-regexp)
4695 (let ((range (c-literal-limits)))
4696 (if range (goto-char (car range)))))
4698 (setq start-in-literal (point)))
4700 ;; The start is in a literal. If the limit is in the same
4701 ;; one we don't have to find a syntactic position etc. We
4702 ;; only check that if the limit is at or before bonl to save
4703 ;; time; it covers the by far most common case when font-lock
4704 ;; refontifies the current line only.
4705 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4706 (save-excursion
4707 (goto-char cfd-start-pos)
4708 (while (progn
4709 (goto-char (next-single-property-change
4710 (point) 'face nil cfd-limit))
4711 (and (< (point) cfd-limit)
4712 (c-got-face-at (point) c-literal-faces))))
4713 (= (point) cfd-limit)))
4715 ;; Completely inside a literal. Set up variables to trig the
4716 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4717 ;; find a suitable start position.
4718 (setq cfd-continue-pos start-in-literal))
4720 ;; Check if the region might be completely inside a macro, to
4721 ;; optimize that like the completely-inside-literal above.
4722 ((save-excursion
4723 (and (= (forward-line 1) 0)
4724 (bolp) ; forward-line has funny behavior at eob.
4725 (>= (point) cfd-limit)
4726 (progn (backward-char)
4727 (eq (char-before) ?\\))))
4728 ;; (Maybe) completely inside a macro. Only need to trig the
4729 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4730 ;; set things up.
4731 (setq cfd-continue-pos (1- cfd-start-pos)
4732 start-in-macro t))
4735 ;; Back out of any macro so we don't miss any declaration
4736 ;; that could follow after it.
4737 (when (c-beginning-of-macro)
4738 (setq start-in-macro t))
4740 ;; Now we're at a proper syntactically relevant position so we
4741 ;; can use the cache. But first clear it if it applied
4742 ;; further down.
4743 (c-invalidate-find-decl-cache cfd-start-pos)
4745 (setq syntactic-pos (point))
4746 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
4747 ;; Don't have to do this if the cache is relevant here,
4748 ;; typically if the same line is refontified again. If
4749 ;; we're just some syntactic whitespace further down we can
4750 ;; still use the cache to limit the skipping.
4751 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
4753 ;; If we hit `c-find-decl-syntactic-pos' and
4754 ;; `c-find-decl-match-pos' is set then we install the cached
4755 ;; values. If we hit `c-find-decl-syntactic-pos' and
4756 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4757 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4758 ;; and so we can continue the search from this point. If we
4759 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4760 ;; the right spot to begin searching anyway.
4761 (if (and (eq (point) c-find-decl-syntactic-pos)
4762 c-find-decl-match-pos)
4763 (setq cfd-match-pos c-find-decl-match-pos
4764 cfd-continue-pos syntactic-pos)
4766 (setq c-find-decl-syntactic-pos syntactic-pos)
4768 (when (if (bobp)
4769 ;; Always consider bob a match to get the first
4770 ;; declaration in the file. Do this separately instead of
4771 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4772 ;; regexp always can consume at least one character to
4773 ;; ensure that we won't get stuck in an infinite loop.
4774 (setq cfd-re-match 0)
4775 (backward-char)
4776 (c-beginning-of-current-token)
4777 (< (point) cfd-limit))
4778 ;; Do an initial search now. In the bob case above it's
4779 ;; only done to search for a `c-decl-end' spot.
4780 (c-find-decl-prefix-search))
4782 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
4783 cfd-match-pos)))))
4785 ;; Advance `cfd-continue-pos' if it's before the start position.
4786 ;; The closest continue position that might have effect at or
4787 ;; after the start depends on what we started in. This also
4788 ;; finds a suitable start position in the special cases when the
4789 ;; region is completely within a literal or macro.
4790 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
4792 (cond
4793 (start-in-macro
4794 ;; If we're in a macro then it's the closest preceding token
4795 ;; in the macro. Check this before `start-in-literal',
4796 ;; since if we're inside a literal in a macro, the preceding
4797 ;; token is earlier than any `c-decl-end' spot inside the
4798 ;; literal (comment).
4799 (goto-char (or start-in-literal cfd-start-pos))
4800 ;; The only syntactic ws in macros are comments.
4801 (c-backward-comments)
4802 (backward-char)
4803 (c-beginning-of-current-token))
4805 (start-in-literal
4806 ;; If we're in a comment it can only be the closest
4807 ;; preceding `c-decl-end' position within that comment, if
4808 ;; any. Go back to the beginning of such a property so that
4809 ;; `c-find-decl-prefix-search' will find the end of it.
4810 ;; (Can't stop at the end and install it directly on
4811 ;; `cfd-prop-match' since that variable might be cleared
4812 ;; after `cfd-fun' below.)
4814 ;; Note that if the literal is a string then the property
4815 ;; search will simply skip to the beginning of it right
4816 ;; away.
4817 (if (not c-type-decl-end-used)
4818 (goto-char start-in-literal)
4819 (goto-char cfd-start-pos)
4820 (while (progn
4821 (goto-char (previous-single-property-change
4822 (point) 'c-type nil start-in-literal))
4823 (and (> (point) start-in-literal)
4824 (not (eq (c-get-char-property (point) 'c-type)
4825 'c-decl-end))))))
4827 (when (= (point) start-in-literal)
4828 ;; Didn't find any property inside the comment, so we can
4829 ;; skip it entirely. (This won't skip past a string, but
4830 ;; that'll be handled quickly by the next
4831 ;; `c-find-decl-prefix-search' anyway.)
4832 (c-forward-single-comment)
4833 (if (> (point) cfd-limit)
4834 (goto-char cfd-limit))))
4837 ;; If we started in normal code, the only match that might
4838 ;; apply before the start is what we already got in
4839 ;; `cfd-match-pos' so we can continue at the start position.
4840 ;; (Note that we don't get here if the first match is below
4841 ;; it.)
4842 (goto-char cfd-start-pos)))
4844 ;; Delete found matches if they are before our new continue
4845 ;; position, so that `c-find-decl-prefix-search' won't back up
4846 ;; to them later on.
4847 (setq cfd-continue-pos (point))
4848 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
4849 (setq cfd-re-match nil))
4850 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
4851 (setq cfd-prop-match nil)))
4853 (if syntactic-pos
4854 ;; This is the normal case and we got a proper syntactic
4855 ;; position. If there's a match then it's always outside
4856 ;; macros and comments, so advance to the next token and set
4857 ;; `cfd-token-pos'. The loop below will later go back using
4858 ;; `cfd-continue-pos' to fix declarations inside the
4859 ;; syntactic ws.
4860 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
4861 (goto-char syntactic-pos)
4862 (c-forward-syntactic-ws)
4863 (and cfd-continue-pos
4864 (< cfd-continue-pos (point))
4865 (setq cfd-token-pos (point))))
4867 ;; Have one of the special cases when the region is completely
4868 ;; within a literal or macro. `cfd-continue-pos' is set to a
4869 ;; good start position for the search, so do it.
4870 (c-find-decl-prefix-search)))
4872 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4874 (while (progn
4875 (while (and
4876 (< cfd-match-pos cfd-limit)
4879 ;; Kludge to filter out matches on the "<" that
4880 ;; aren't open parens, for the sake of languages
4881 ;; that got `c-recognize-<>-arglists' set.
4882 (and (eq (char-before cfd-match-pos) ?<)
4883 (not (c-get-char-property (1- cfd-match-pos)
4884 'syntax-table)))
4886 ;; If `cfd-continue-pos' is less or equal to
4887 ;; `cfd-token-pos', we've got a hit inside a macro
4888 ;; that's in the syntactic whitespace before the last
4889 ;; "real" declaration we've checked. If they're equal
4890 ;; we've arrived at the declaration a second time, so
4891 ;; there's nothing to do.
4892 (= cfd-continue-pos cfd-token-pos)
4894 (progn
4895 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4896 ;; we're still searching for declarations embedded in
4897 ;; the syntactic whitespace. In that case we need
4898 ;; only to skip comments and not macros, since they
4899 ;; can't be nested, and that's already been done in
4900 ;; `c-find-decl-prefix-search'.
4901 (when (> cfd-continue-pos cfd-token-pos)
4902 (c-forward-syntactic-ws)
4903 (setq cfd-token-pos (point)))
4905 ;; Continue if the following token fails the
4906 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4907 (when (or (>= (point) cfd-limit)
4908 (not (looking-at cfd-decl-re))
4909 (and cfd-face-checklist
4910 (not (c-got-face-at
4911 (point) cfd-face-checklist))))
4912 (goto-char cfd-continue-pos)
4913 t)))
4915 (< (point) cfd-limit))
4916 (c-find-decl-prefix-search))
4918 (< (point) cfd-limit))
4920 (when (and
4921 (>= (point) cfd-start-pos)
4923 (progn
4924 ;; Narrow to the end of the macro if we got a hit inside
4925 ;; one, to avoid recognizing things that start inside the
4926 ;; macro and end outside it.
4927 (when (> cfd-match-pos cfd-macro-end)
4928 ;; Not in the same macro as in the previous round.
4929 (save-excursion
4930 (goto-char cfd-match-pos)
4931 (setq cfd-macro-end
4932 (if (save-excursion (and (c-beginning-of-macro)
4933 (< (point) cfd-match-pos)))
4934 (progn (c-end-of-macro)
4935 (point))
4936 0))))
4938 (if (zerop cfd-macro-end)
4940 (if (> cfd-macro-end (point))
4941 (progn (narrow-to-region (point-min) cfd-macro-end)
4943 ;; The matched token was the last thing in the macro,
4944 ;; so the whole match is bogus.
4945 (setq cfd-macro-end 0)
4946 nil))))
4948 (c-debug-put-decl-spot-faces cfd-match-pos (point))
4949 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
4950 (setq cfd-prop-match nil))
4952 (when (/= cfd-macro-end 0)
4953 ;; Restore limits if we did macro narrowing above.
4954 (narrow-to-region (point-min) cfd-buffer-end)))
4956 (goto-char cfd-continue-pos)
4957 (if (= cfd-continue-pos cfd-limit)
4958 (setq cfd-match-pos cfd-limit)
4959 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
4960 ; cfd-match-pos, etc.
4963 ;; A cache for found types.
4965 ;; Buffer local variable that contains an obarray with the types we've
4966 ;; found. If a declaration is recognized somewhere we record the
4967 ;; fully qualified identifier in it to recognize it as a type
4968 ;; elsewhere in the file too. This is not accurate since we do not
4969 ;; bother with the scoping rules of the languages, but in practice the
4970 ;; same name is seldom used as both a type and something else in a
4971 ;; file, and we only use this as a last resort in ambiguous cases (see
4972 ;; `c-forward-decl-or-cast-1').
4974 ;; Not every type need be in this cache. However, things which have
4975 ;; ceased to be types must be removed from it.
4977 ;; Template types in C++ are added here too but with the template
4978 ;; arglist replaced with "<>" in references or "<" for the one in the
4979 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4980 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4981 ;; template specs can be fairly sized programs in themselves) and
4982 ;; improves the hit ratio (it's a type regardless of the template
4983 ;; args; it's just not the same type, but we're only interested in
4984 ;; recognizing types, not telling distinct types apart). Note that
4985 ;; template types in references are added here too; from the example
4986 ;; above there will also be an entry "Foo<".
4987 (defvar c-found-types nil)
4988 (make-variable-buffer-local 'c-found-types)
4990 (defsubst c-clear-found-types ()
4991 ;; Clears `c-found-types'.
4992 (setq c-found-types (make-vector 53 0)))
4994 (defun c-add-type (from to)
4995 ;; Add the given region as a type in `c-found-types'. If the region
4996 ;; doesn't match an existing type but there is a type which is equal
4997 ;; to the given one except that the last character is missing, then
4998 ;; the shorter type is removed. That's done to avoid adding all
4999 ;; prefixes of a type as it's being entered and font locked. This
5000 ;; doesn't cover cases like when characters are removed from a type
5001 ;; or added in the middle. We'd need the position of point when the
5002 ;; font locking is invoked to solve this well.
5004 ;; This function might do hidden buffer changes.
5005 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
5006 (unless (intern-soft type c-found-types)
5007 (unintern (substring type 0 -1) c-found-types)
5008 (intern type c-found-types))))
5010 (defun c-unfind-type (name)
5011 ;; Remove the "NAME" from c-found-types, if present.
5012 (unintern name c-found-types))
5014 (defsubst c-check-type (from to)
5015 ;; Return non-nil if the given region contains a type in
5016 ;; `c-found-types'.
5018 ;; This function might do hidden buffer changes.
5019 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
5020 c-found-types))
5022 (defun c-list-found-types ()
5023 ;; Return all the types in `c-found-types' as a sorted list of
5024 ;; strings.
5025 (let (type-list)
5026 (mapatoms (lambda (type)
5027 (setq type-list (cons (symbol-name type)
5028 type-list)))
5029 c-found-types)
5030 (sort type-list 'string-lessp)))
5032 ;; Shut up the byte compiler.
5033 (defvar c-maybe-stale-found-type)
5035 (defun c-trim-found-types (beg end old-len)
5036 ;; An after change function which, in conjunction with the info in
5037 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
5038 ;; from `c-found-types', should this type have become stale. For
5039 ;; example, this happens to "foo" when "foo \n bar();" becomes
5040 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
5041 ;; the fontification.
5043 ;; Have we, perhaps, added non-ws characters to the front/back of a found
5044 ;; type?
5045 (when (> end beg)
5046 (save-excursion
5047 (when (< end (point-max))
5048 (goto-char end)
5049 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
5050 (progn (goto-char end)
5051 (c-end-of-current-token)))
5052 (c-unfind-type (buffer-substring-no-properties
5053 end (point)))))
5054 (when (> beg (point-min))
5055 (goto-char beg)
5056 (if (and (c-end-of-current-token) ; only moves when we started in the middle
5057 (progn (goto-char beg)
5058 (c-beginning-of-current-token)))
5059 (c-unfind-type (buffer-substring-no-properties
5060 (point) beg))))))
5062 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
5063 (cond
5064 ;; Changing the amount of (already existing) whitespace - don't do anything.
5065 ((and (c-partial-ws-p beg end)
5066 (or (= beg end) ; removal of WS
5067 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
5069 ;; The syntactic relationship which defined a "found type" has been
5070 ;; destroyed.
5071 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
5072 (c-unfind-type (cadr c-maybe-stale-found-type)))
5073 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5077 ;; Setting and removing syntax properties on < and > in languages (C++
5078 ;; and Java) where they can be template/generic delimiters as well as
5079 ;; their normal meaning of "less/greater than".
5081 ;; Normally, < and > have syntax 'punctuation'. When they are found to
5082 ;; be delimiters, they are marked as such with the category properties
5083 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5085 ;; STRATEGY:
5087 ;; It is impossible to determine with certainty whether a <..> pair in
5088 ;; C++ is two comparison operators or is template delimiters, unless
5089 ;; one duplicates a lot of a C++ compiler. For example, the following
5090 ;; code fragment:
5092 ;; foo (a < b, c > d) ;
5094 ;; could be a function call with two integer parameters (each a
5095 ;; relational expression), or it could be a constructor for class foo
5096 ;; taking one parameter d of templated type "a < b, c >". They are
5097 ;; somewhat easier to distinguish in Java.
5099 ;; The strategy now (2010-01) adopted is to mark and unmark < and
5100 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5101 ;; individually when their context so indicated. This gave rise to
5102 ;; intractable problems when one of a matching pair was deleted, or
5103 ;; pulled into a literal.]
5105 ;; At each buffer change, the syntax-table properties are removed in a
5106 ;; before-change function and reapplied, when needed, in an
5107 ;; after-change function. It is far more important that the
5108 ;; properties get removed when they they are spurious than that they
5109 ;; be present when wanted.
5110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5111 (defun c-clear-<-pair-props (&optional pos)
5112 ;; POS (default point) is at a < character. If it is marked with
5113 ;; open paren syntax-table text property, remove the property,
5114 ;; together with the close paren property on the matching > (if
5115 ;; any).
5116 (save-excursion
5117 (if pos
5118 (goto-char pos)
5119 (setq pos (point)))
5120 (when (equal (c-get-char-property (point) 'syntax-table)
5121 c-<-as-paren-syntax)
5122 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5123 (c-go-list-forward))
5124 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5125 c->-as-paren-syntax) ; should always be true.
5126 (c-clear-char-property (1- (point)) 'category))
5127 (c-clear-char-property pos 'category))))
5129 (defun c-clear->-pair-props (&optional pos)
5130 ;; POS (default point) is at a > character. If it is marked with
5131 ;; close paren syntax-table property, remove the property, together
5132 ;; with the open paren property on the matching < (if any).
5133 (save-excursion
5134 (if pos
5135 (goto-char pos)
5136 (setq pos (point)))
5137 (when (equal (c-get-char-property (point) 'syntax-table)
5138 c->-as-paren-syntax)
5139 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5140 (c-go-up-list-backward))
5141 (when (equal (c-get-char-property (point) 'syntax-table)
5142 c-<-as-paren-syntax) ; should always be true.
5143 (c-clear-char-property (point) 'category))
5144 (c-clear-char-property pos 'category))))
5146 (defun c-clear-<>-pair-props (&optional pos)
5147 ;; POS (default point) is at a < or > character. If it has an
5148 ;; open/close paren syntax-table property, remove this property both
5149 ;; from the current character and its partner (which will also be
5150 ;; thusly marked).
5151 (cond
5152 ((eq (char-after) ?\<)
5153 (c-clear-<-pair-props pos))
5154 ((eq (char-after) ?\>)
5155 (c-clear->-pair-props pos))
5156 (t (c-benign-error
5157 "c-clear-<>-pair-props called from wrong position"))))
5159 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5160 ;; POS (default point) is at a < character. If it is both marked
5161 ;; with open/close paren syntax-table property, and has a matching >
5162 ;; (also marked) which is after LIM, remove the property both from
5163 ;; the current > and its partner. Return t when this happens, nil
5164 ;; when it doesn't.
5165 (save-excursion
5166 (if pos
5167 (goto-char pos)
5168 (setq pos (point)))
5169 (when (equal (c-get-char-property (point) 'syntax-table)
5170 c-<-as-paren-syntax)
5171 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5172 (c-go-list-forward))
5173 (when (and (>= (point) lim)
5174 (equal (c-get-char-property (1- (point)) 'syntax-table)
5175 c->-as-paren-syntax)) ; should always be true.
5176 (c-unmark-<->-as-paren (1- (point)))
5177 (c-unmark-<->-as-paren pos))
5178 t)))
5180 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
5181 ;; POS (default point) is at a > character. If it is both marked
5182 ;; with open/close paren syntax-table property, and has a matching <
5183 ;; (also marked) which is before LIM, remove the property both from
5184 ;; the current < and its partner. Return t when this happens, nil
5185 ;; when it doesn't.
5186 (save-excursion
5187 (if pos
5188 (goto-char pos)
5189 (setq pos (point)))
5190 (when (equal (c-get-char-property (point) 'syntax-table)
5191 c->-as-paren-syntax)
5192 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5193 (c-go-up-list-backward))
5194 (when (and (<= (point) lim)
5195 (equal (c-get-char-property (point) 'syntax-table)
5196 c-<-as-paren-syntax)) ; should always be true.
5197 (c-unmark-<->-as-paren (point))
5198 (c-unmark-<->-as-paren pos))
5199 t)))
5201 ;; Set by c-common-init in cc-mode.el.
5202 (defvar c-new-BEG)
5203 (defvar c-new-END)
5205 (defun c-before-change-check-<>-operators (beg end)
5206 ;; Unmark certain pairs of "< .... >" which are currently marked as
5207 ;; template/generic delimiters. (This marking is via syntax-table
5208 ;; text properties).
5210 ;; These pairs are those which are in the current "statement" (i.e.,
5211 ;; the region between the {, }, or ; before BEG and the one after
5212 ;; END), and which enclose any part of the interval (BEG END).
5214 ;; Note that in C++ (?and Java), template/generic parens cannot
5215 ;; enclose a brace or semicolon, so we use these as bounds on the
5216 ;; region we must work on.
5218 ;; This function is called from before-change-functions (via
5219 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5220 ;; and point is undefined, both at entry and exit.
5222 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5223 ;; 2010-01-29.
5224 (save-excursion
5225 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5226 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5227 new-beg new-end need-new-beg need-new-end)
5228 ;; Locate the barrier before the changed region
5229 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5230 (c-syntactic-skip-backward "^;{}" (max (- beg 2048) (point-min)))
5231 (setq new-beg (point))
5233 ;; Remove the syntax-table properties from each pertinent <...> pair.
5234 ;; Firsly, the ones with the < before beg and > after beg.
5235 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5236 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5237 (setq need-new-beg t)))
5239 ;; Locate the barrier after END.
5240 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5241 (c-syntactic-re-search-forward "[;{}]"
5242 (min (+ end 2048) (point-max)) 'end)
5243 (setq new-end (point))
5245 ;; Remove syntax-table properties from the remaining pertinent <...>
5246 ;; pairs, those with a > after end and < before end.
5247 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5248 (if (c-clear->-pair-props-if-match-before end)
5249 (setq need-new-end t)))
5251 ;; Extend the fontification region, if needed.
5252 (when need-new-beg
5253 (goto-char new-beg)
5254 (c-forward-syntactic-ws)
5255 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5257 (when need-new-end
5258 (and (> new-end c-new-END) (setq c-new-END new-end))))))
5262 (defun c-after-change-check-<>-operators (beg end)
5263 ;; This is called from `after-change-functions' when
5264 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5265 ;; chars with paren syntax become part of another operator like "<<"
5266 ;; or ">=".
5268 ;; This function might do hidden buffer changes.
5270 (save-excursion
5271 (goto-char beg)
5272 (when (or (looking-at "[<>]")
5273 (< (skip-chars-backward "<>") 0))
5275 (goto-char beg)
5276 (c-beginning-of-current-token)
5277 (when (and (< (point) beg)
5278 (looking-at c-<>-multichar-token-regexp)
5279 (< beg (setq beg (match-end 0))))
5280 (while (progn (skip-chars-forward "^<>" beg)
5281 (< (point) beg))
5282 (c-clear-<>-pair-props)
5283 (forward-char))))
5285 (when (< beg end)
5286 (goto-char end)
5287 (when (or (looking-at "[<>]")
5288 (< (skip-chars-backward "<>") 0))
5290 (goto-char end)
5291 (c-beginning-of-current-token)
5292 (when (and (< (point) end)
5293 (looking-at c-<>-multichar-token-regexp)
5294 (< end (setq end (match-end 0))))
5295 (while (progn (skip-chars-forward "^<>" end)
5296 (< (point) end))
5297 (c-clear-<>-pair-props)
5298 (forward-char)))))))
5302 ;; Handling of small scale constructs like types and names.
5304 ;; Dynamically bound variable that instructs `c-forward-type' to also
5305 ;; treat possible types (i.e. those that it normally returns 'maybe or
5306 ;; 'found for) as actual types (and always return 'found for them).
5307 ;; This means that it records them in `c-record-type-identifiers' if
5308 ;; that is set, and that it adds them to `c-found-types'.
5309 (defvar c-promote-possible-types nil)
5311 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5312 ;; mark up successfully parsed arglists with paren syntax properties on
5313 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5314 ;; `c-type' property of each argument separating comma.
5316 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5317 ;; all arglists for side effects (i.e. recording types), otherwise it
5318 ;; exploits any existing paren syntax properties to quickly jump to the
5319 ;; end of already parsed arglists.
5321 ;; Marking up the arglists is not the default since doing that correctly
5322 ;; depends on a proper value for `c-restricted-<>-arglists'.
5323 (defvar c-parse-and-markup-<>-arglists nil)
5325 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5326 ;; not accept arglists that contain binary operators.
5328 ;; This is primarily used to handle C++ template arglists. C++
5329 ;; disambiguates them by checking whether the preceding name is a
5330 ;; template or not. We can't do that, so we assume it is a template
5331 ;; if it can be parsed as one. That usually works well since
5332 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5333 ;; in almost all cases would be pointless.
5335 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5336 ;; should let the comma separate the function arguments instead. And
5337 ;; in a context where the value of the expression is taken, e.g. in
5338 ;; "if (a < b || c > d)", it's probably not a template.
5339 (defvar c-restricted-<>-arglists nil)
5341 ;; Dynamically bound variables that instructs
5342 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5343 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5344 ;; `c-forward-label' to record the ranges of all the type and
5345 ;; reference identifiers they encounter. They will build lists on
5346 ;; these variables where each element is a cons of the buffer
5347 ;; positions surrounding each identifier. This recording is only
5348 ;; activated when `c-record-type-identifiers' is non-nil.
5350 ;; All known types that can't be identifiers are recorded, and also
5351 ;; other possible types if `c-promote-possible-types' is set.
5352 ;; Recording is however disabled inside angle bracket arglists that
5353 ;; are encountered inside names and other angle bracket arglists.
5354 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5355 ;; instead.
5357 ;; Only the names in C++ template style references (e.g. "tmpl" in
5358 ;; "tmpl<a,b>::foo") are recorded as references, other references
5359 ;; aren't handled here.
5361 ;; `c-forward-label' records the label identifier(s) on
5362 ;; `c-record-ref-identifiers'.
5363 (defvar c-record-type-identifiers nil)
5364 (defvar c-record-ref-identifiers nil)
5366 ;; This variable will receive a cons cell of the range of the last
5367 ;; single identifier symbol stepped over by `c-forward-name' if it's
5368 ;; successful. This is the range that should be put on one of the
5369 ;; record lists above by the caller. It's assigned nil if there's no
5370 ;; such symbol in the name.
5371 (defvar c-last-identifier-range nil)
5373 (defmacro c-record-type-id (range)
5374 (if (eq (car-safe range) 'cons)
5375 ;; Always true.
5376 `(setq c-record-type-identifiers
5377 (cons ,range c-record-type-identifiers))
5378 `(let ((range ,range))
5379 (if range
5380 (setq c-record-type-identifiers
5381 (cons range c-record-type-identifiers))))))
5383 (defmacro c-record-ref-id (range)
5384 (if (eq (car-safe range) 'cons)
5385 ;; Always true.
5386 `(setq c-record-ref-identifiers
5387 (cons ,range c-record-ref-identifiers))
5388 `(let ((range ,range))
5389 (if range
5390 (setq c-record-ref-identifiers
5391 (cons range c-record-ref-identifiers))))))
5393 ;; Dynamically bound variable that instructs `c-forward-type' to
5394 ;; record the ranges of types that only are found. Behaves otherwise
5395 ;; like `c-record-type-identifiers'.
5396 (defvar c-record-found-types nil)
5398 (defmacro c-forward-keyword-prefixed-id (type)
5399 ;; Used internally in `c-forward-keyword-clause' to move forward
5400 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5401 ;; possibly is prefixed by keywords and their associated clauses.
5402 ;; Try with a type/name first to not trip up on those that begin
5403 ;; with a keyword. Return t if a known or found type is moved
5404 ;; over. The point is clobbered if nil is returned. If range
5405 ;; recording is enabled, the identifier is recorded on as a type
5406 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5408 ;; This macro might do hidden buffer changes.
5409 `(let (res)
5410 (while (if (setq res ,(if (eq type 'type)
5411 `(c-forward-type)
5412 `(c-forward-name)))
5414 (and (looking-at c-keywords-regexp)
5415 (c-forward-keyword-clause 1))))
5416 (when (memq res '(t known found prefix))
5417 ,(when (eq type 'ref)
5418 `(when c-record-type-identifiers
5419 (c-record-ref-id c-last-identifier-range)))
5420 t)))
5422 (defmacro c-forward-id-comma-list (type update-safe-pos)
5423 ;; Used internally in `c-forward-keyword-clause' to move forward
5424 ;; over a comma separated list of types or names using
5425 ;; `c-forward-keyword-prefixed-id'.
5427 ;; This macro might do hidden buffer changes.
5428 `(while (and (progn
5429 ,(when update-safe-pos
5430 `(setq safe-pos (point)))
5431 (eq (char-after) ?,))
5432 (progn
5433 (forward-char)
5434 (c-forward-syntactic-ws)
5435 (c-forward-keyword-prefixed-id ,type)))))
5437 (defun c-forward-keyword-clause (match)
5438 ;; Submatch MATCH in the current match data is assumed to surround a
5439 ;; token. If it's a keyword, move over it and any immediately
5440 ;; following clauses associated with it, stopping at the start of
5441 ;; the next token. t is returned in that case, otherwise the point
5442 ;; stays and nil is returned. The kind of clauses that are
5443 ;; recognized are those specified by `c-type-list-kwds',
5444 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5445 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5446 ;; and `c-<>-arglist-kwds'.
5448 ;; This function records identifier ranges on
5449 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5450 ;; `c-record-type-identifiers' is non-nil.
5452 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5453 ;; apply directly after the keyword, the type list is moved over
5454 ;; only when there is no unaccounted token before it (i.e. a token
5455 ;; that isn't moved over due to some other keyword list). The
5456 ;; identifier ranges in the list are still recorded if that should
5457 ;; be done, though.
5459 ;; This function might do hidden buffer changes.
5461 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5462 ;; The call to `c-forward-<>-arglist' below is made after
5463 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5464 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5465 ;; should therefore be nil.
5466 (c-parse-and-markup-<>-arglists t)
5467 c-restricted-<>-arglists)
5469 (when kwd-sym
5470 (goto-char (match-end match))
5471 (c-forward-syntactic-ws)
5472 (setq safe-pos (point))
5474 (cond
5475 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5476 (c-forward-keyword-prefixed-id type))
5477 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5478 (c-forward-id-comma-list type t))
5480 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5481 (c-forward-keyword-prefixed-id ref))
5482 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5483 (c-forward-id-comma-list ref t))
5485 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5486 (eq (char-after) ?\())
5487 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5489 (forward-char)
5490 (when (and (setq pos (c-up-list-forward))
5491 (eq (char-before pos) ?\)))
5492 (when (and c-record-type-identifiers
5493 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5494 ;; Use `c-forward-type' on every identifier we can find
5495 ;; inside the paren, to record the types.
5496 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5497 (goto-char (match-beginning 0))
5498 (unless (c-forward-type)
5499 (looking-at c-symbol-key) ; Always matches.
5500 (goto-char (match-end 0)))))
5502 (goto-char pos)
5503 (c-forward-syntactic-ws)
5504 (setq safe-pos (point))))
5506 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5507 (eq (char-after) ?<)
5508 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5509 (c-forward-syntactic-ws)
5510 (setq safe-pos (point)))
5512 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5513 (not (looking-at c-symbol-start))
5514 (c-safe (c-forward-sexp) t))
5515 (c-forward-syntactic-ws)
5516 (setq safe-pos (point))))
5518 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5519 (if (eq (char-after) ?:)
5520 ;; If we are at the colon already, we move over the type
5521 ;; list after it.
5522 (progn
5523 (forward-char)
5524 (c-forward-syntactic-ws)
5525 (when (c-forward-keyword-prefixed-id type)
5526 (c-forward-id-comma-list type t)))
5527 ;; Not at the colon, so stop here. But the identifier
5528 ;; ranges in the type list later on should still be
5529 ;; recorded.
5530 (and c-record-type-identifiers
5531 (progn
5532 ;; If a keyword matched both one of the types above and
5533 ;; this one, we match `c-colon-type-list-re' after the
5534 ;; clause matched above.
5535 (goto-char safe-pos)
5536 (looking-at c-colon-type-list-re))
5537 (progn
5538 (goto-char (match-end 0))
5539 (c-forward-syntactic-ws)
5540 (c-forward-keyword-prefixed-id type))
5541 ;; There's a type after the `c-colon-type-list-re' match
5542 ;; after a keyword in `c-colon-type-list-kwds'.
5543 (c-forward-id-comma-list type nil))))
5545 (goto-char safe-pos)
5546 t)))
5548 ;; cc-mode requires cc-fonts.
5549 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5551 (defun c-forward-<>-arglist (all-types)
5552 ;; The point is assumed to be at a "<". Try to treat it as the open
5553 ;; paren of an angle bracket arglist and move forward to the
5554 ;; corresponding ">". If successful, the point is left after the
5555 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5556 ;; returned. If ALL-TYPES is t then all encountered arguments in
5557 ;; the arglist that might be types are treated as found types.
5559 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5560 ;; function handles text properties on the angle brackets and argument
5561 ;; separating commas.
5563 ;; `c-restricted-<>-arglists' controls how lenient the template
5564 ;; arglist recognition should be.
5566 ;; This function records identifier ranges on
5567 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5568 ;; `c-record-type-identifiers' is non-nil.
5570 ;; This function might do hidden buffer changes.
5572 (let ((start (point))
5573 ;; If `c-record-type-identifiers' is set then activate
5574 ;; recording of any found types that constitute an argument in
5575 ;; the arglist.
5576 (c-record-found-types (if c-record-type-identifiers t)))
5577 (if (catch 'angle-bracket-arglist-escape
5578 (setq c-record-found-types
5579 (c-forward-<>-arglist-recur all-types)))
5580 (progn
5581 (when (consp c-record-found-types)
5582 (setq c-record-type-identifiers
5583 ;; `nconc' doesn't mind that the tail of
5584 ;; `c-record-found-types' is t.
5585 (nconc c-record-found-types c-record-type-identifiers)))
5586 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
5589 (goto-char start)
5590 nil)))
5592 (defun c-forward-<>-arglist-recur (all-types)
5593 ;; Recursive part of `c-forward-<>-arglist'.
5595 ;; This function might do hidden buffer changes.
5597 (let ((start (point)) res pos tmp
5598 ;; Cover this so that any recorded found type ranges are
5599 ;; automatically lost if it turns out to not be an angle
5600 ;; bracket arglist. It's propagated through the return value
5601 ;; on successful completion.
5602 (c-record-found-types c-record-found-types)
5603 ;; List that collects the positions after the argument
5604 ;; separating ',' in the arglist.
5605 arg-start-pos)
5606 ;; If the '<' has paren open syntax then we've marked it as an angle
5607 ;; bracket arglist before, so skip to the end.
5608 (if (and (not c-parse-and-markup-<>-arglists)
5609 (c-get-char-property (point) 'syntax-table))
5611 (progn
5612 (forward-char)
5613 (if (and (c-go-up-list-forward)
5614 (eq (char-before) ?>))
5616 ;; Got unmatched paren angle brackets. We don't clear the paren
5617 ;; syntax properties and retry, on the basis that it's very
5618 ;; unlikely that paren angle brackets become operators by code
5619 ;; manipulation. It's far more likely that it doesn't match due
5620 ;; to narrowing or some temporary change.
5621 (goto-char start)
5622 nil))
5624 (forward-char) ; Forward over the opening '<'.
5626 (unless (looking-at c-<-op-cont-regexp)
5627 ;; go forward one non-alphanumeric character (group) per iteration of
5628 ;; this loop.
5629 (while (and
5630 (progn
5631 (c-forward-syntactic-ws)
5632 (let ((orig-record-found-types c-record-found-types))
5633 (when (or (and c-record-type-identifiers all-types)
5634 (c-major-mode-is 'java-mode))
5635 ;; All encountered identifiers are types, so set the
5636 ;; promote flag and parse the type.
5637 (progn
5638 (c-forward-syntactic-ws)
5639 (if (looking-at "\\?")
5640 (forward-char)
5641 (when (looking-at c-identifier-start)
5642 (let ((c-promote-possible-types t)
5643 (c-record-found-types t))
5644 (c-forward-type))))
5646 (c-forward-syntactic-ws)
5648 (when (or (looking-at "extends")
5649 (looking-at "super"))
5650 (forward-word)
5651 (c-forward-syntactic-ws)
5652 (let ((c-promote-possible-types t)
5653 (c-record-found-types t))
5654 (c-forward-type)
5655 (c-forward-syntactic-ws))))))
5657 (setq pos (point)) ; e.g. first token inside the '<'
5659 ;; Note: These regexps exploit the match order in \| so
5660 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5661 (c-syntactic-re-search-forward
5662 ;; Stop on ',', '|', '&', '+' and '-' to catch
5663 ;; common binary operators that could be between
5664 ;; two comparison expressions "a<b" and "c>d".
5665 "[<;{},|+&-]\\|[>)]"
5666 nil t t))
5668 (cond
5669 ((eq (char-before) ?>)
5670 ;; Either an operator starting with '>' or the end of
5671 ;; the angle bracket arglist.
5673 (if (looking-at c->-op-cont-regexp)
5674 (progn
5675 (goto-char (match-end 0))
5676 t) ; Continue the loop.
5678 ;; The angle bracket arglist is finished.
5679 (when c-parse-and-markup-<>-arglists
5680 (while arg-start-pos
5681 (c-put-c-type-property (1- (car arg-start-pos))
5682 'c-<>-arg-sep)
5683 (setq arg-start-pos (cdr arg-start-pos)))
5684 (c-mark-<-as-paren start)
5685 (c-mark->-as-paren (1- (point))))
5686 (setq res t)
5687 nil)) ; Exit the loop.
5689 ((eq (char-before) ?<)
5690 ;; Either an operator starting with '<' or a nested arglist.
5691 (setq pos (point))
5692 (let (id-start id-end subres keyword-match)
5693 (cond
5694 ;; The '<' begins a multi-char operator.
5695 ((looking-at c-<-op-cont-regexp)
5696 (setq tmp (match-end 0))
5697 (goto-char (match-end 0)))
5698 ;; We're at a nested <.....>
5699 ((progn
5700 (setq tmp pos)
5701 (backward-char) ; to the '<'
5702 (and
5703 (save-excursion
5704 ;; There's always an identifier before an angle
5705 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5706 ;; or `c-<>-arglist-kwds'.
5707 (c-backward-syntactic-ws)
5708 (setq id-end (point))
5709 (c-simple-skip-symbol-backward)
5710 (when (or (setq keyword-match
5711 (looking-at c-opt-<>-sexp-key))
5712 (not (looking-at c-keywords-regexp)))
5713 (setq id-start (point))))
5714 (setq subres
5715 (let ((c-promote-possible-types t)
5716 (c-record-found-types t))
5717 (c-forward-<>-arglist-recur
5718 (and keyword-match
5719 (c-keyword-member
5720 (c-keyword-sym (match-string 1))
5721 'c-<>-type-kwds)))))))
5723 ;; It was an angle bracket arglist.
5724 (setq c-record-found-types subres)
5726 ;; Record the identifier before the template as a type
5727 ;; or reference depending on whether the arglist is last
5728 ;; in a qualified identifier.
5729 (when (and c-record-type-identifiers
5730 (not keyword-match))
5731 (if (and c-opt-identifier-concat-key
5732 (progn
5733 (c-forward-syntactic-ws)
5734 (looking-at c-opt-identifier-concat-key)))
5735 (c-record-ref-id (cons id-start id-end))
5736 (c-record-type-id (cons id-start id-end)))))
5738 ;; At a "less than" operator.
5740 (forward-char)
5742 t) ; carry on looping.
5744 ((and (not c-restricted-<>-arglists)
5745 (or (and (eq (char-before) ?&)
5746 (not (eq (char-after) ?&)))
5747 (eq (char-before) ?,)))
5748 ;; Just another argument. Record the position. The
5749 ;; type check stuff that made us stop at it is at
5750 ;; the top of the loop.
5751 (setq arg-start-pos (cons (point) arg-start-pos)))
5754 ;; Got a character that can't be in an angle bracket
5755 ;; arglist argument. Abort using `throw', since
5756 ;; it's useless to try to find a surrounding arglist
5757 ;; if we're nested.
5758 (throw 'angle-bracket-arglist-escape nil))))))
5759 (if res
5760 (or c-record-found-types t)))))
5762 (defun c-backward-<>-arglist (all-types &optional limit)
5763 ;; The point is assumed to be directly after a ">". Try to treat it
5764 ;; as the close paren of an angle bracket arglist and move back to
5765 ;; the corresponding "<". If successful, the point is left at
5766 ;; the "<" and t is returned, otherwise the point isn't moved and
5767 ;; nil is returned. ALL-TYPES is passed on to
5768 ;; `c-forward-<>-arglist'.
5770 ;; If the optional LIMIT is given, it bounds the backward search.
5771 ;; It's then assumed to be at a syntactically relevant position.
5773 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5774 ;; function for more details.
5776 (let ((start (point)))
5777 (backward-char)
5778 (if (and (not c-parse-and-markup-<>-arglists)
5779 (c-get-char-property (point) 'syntax-table))
5781 (if (and (c-go-up-list-backward)
5782 (eq (char-after) ?<))
5784 ;; See corresponding note in `c-forward-<>-arglist'.
5785 (goto-char start)
5786 nil)
5788 (while (progn
5789 (c-syntactic-skip-backward "^<;{}" limit t)
5791 (and
5792 (if (eq (char-before) ?<)
5794 ;; Stopped at bob or a char that isn't allowed in an
5795 ;; arglist, so we've failed.
5796 (goto-char start)
5797 nil)
5799 (if (> (point)
5800 (progn (c-beginning-of-current-token)
5801 (point)))
5802 ;; If we moved then the "<" was part of some
5803 ;; multicharacter token.
5806 (backward-char)
5807 (let ((beg-pos (point)))
5808 (if (c-forward-<>-arglist all-types)
5809 (cond ((= (point) start)
5810 ;; Matched the arglist. Break the while.
5811 (goto-char beg-pos)
5812 nil)
5813 ((> (point) start)
5814 ;; We started from a non-paren ">" inside an
5815 ;; arglist.
5816 (goto-char start)
5817 nil)
5819 ;; Matched a shorter arglist. Can be a nested
5820 ;; one so continue looking.
5821 (goto-char beg-pos)
5823 t))))))
5825 (/= (point) start))))
5827 (defun c-forward-name ()
5828 ;; Move forward over a complete name if at the beginning of one,
5829 ;; stopping at the next following token. A keyword, as such,
5830 ;; doesn't count as a name. If the point is not at something that
5831 ;; is recognized as a name then it stays put.
5833 ;; A name could be something as simple as "foo" in C or something as
5834 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5835 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5836 ;; int>::*volatile const" in C++ (this function is actually little
5837 ;; more than a `looking-at' call in all modes except those that,
5838 ;; like C++, have `c-recognize-<>-arglists' set).
5840 ;; Return
5841 ;; o - nil if no name is found;
5842 ;; o - 'template if it's an identifier ending with an angle bracket
5843 ;; arglist;
5844 ;; o - 'operator of it's an operator identifier;
5845 ;; o - t if it's some other kind of name.
5847 ;; This function records identifier ranges on
5848 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5849 ;; `c-record-type-identifiers' is non-nil.
5851 ;; This function might do hidden buffer changes.
5853 (let ((pos (point)) (start (point)) res id-start id-end
5854 ;; Turn off `c-promote-possible-types' here since we might
5855 ;; call `c-forward-<>-arglist' and we don't want it to promote
5856 ;; every suspect thing in the arglist to a type. We're
5857 ;; typically called from `c-forward-type' in this case, and
5858 ;; the caller only wants the top level type that it finds to
5859 ;; be promoted.
5860 c-promote-possible-types)
5861 (while
5862 (and
5863 (looking-at c-identifier-key)
5865 (progn
5866 ;; Check for keyword. We go to the last symbol in
5867 ;; `c-identifier-key' first.
5868 (goto-char (setq id-end (match-end 0)))
5869 (c-simple-skip-symbol-backward)
5870 (setq id-start (point))
5872 (if (looking-at c-keywords-regexp)
5873 (when (and (c-major-mode-is 'c++-mode)
5874 (looking-at
5875 (cc-eval-when-compile
5876 (concat "\\(operator\\|\\(template\\)\\)"
5877 "\\(" (c-lang-const c-nonsymbol-key c++)
5878 "\\|$\\)")))
5879 (if (match-beginning 2)
5880 ;; "template" is only valid inside an
5881 ;; identifier if preceded by "::".
5882 (save-excursion
5883 (c-backward-syntactic-ws)
5884 (and (c-safe (backward-char 2) t)
5885 (looking-at "::")))
5888 ;; Handle a C++ operator or template identifier.
5889 (goto-char id-end)
5890 (c-forward-syntactic-ws)
5891 (cond ((eq (char-before id-end) ?e)
5892 ;; Got "... ::template".
5893 (let ((subres (c-forward-name)))
5894 (when subres
5895 (setq pos (point)
5896 res subres))))
5898 ((looking-at c-identifier-start)
5899 ;; Got a cast operator.
5900 (when (c-forward-type)
5901 (setq pos (point)
5902 res 'operator)
5903 ;; Now we should match a sequence of either
5904 ;; '*', '&' or a name followed by ":: *",
5905 ;; where each can be followed by a sequence
5906 ;; of `c-opt-type-modifier-key'.
5907 (while (cond ((looking-at "[*&]")
5908 (goto-char (match-end 0))
5910 ((looking-at c-identifier-start)
5911 (and (c-forward-name)
5912 (looking-at "::")
5913 (progn
5914 (goto-char (match-end 0))
5915 (c-forward-syntactic-ws)
5916 (eq (char-after) ?*))
5917 (progn
5918 (forward-char)
5919 t))))
5920 (while (progn
5921 (c-forward-syntactic-ws)
5922 (setq pos (point))
5923 (looking-at c-opt-type-modifier-key))
5924 (goto-char (match-end 1))))))
5926 ((looking-at c-overloadable-operators-regexp)
5927 ;; Got some other operator.
5928 (setq c-last-identifier-range
5929 (cons (point) (match-end 0)))
5930 (goto-char (match-end 0))
5931 (c-forward-syntactic-ws)
5932 (setq pos (point)
5933 res 'operator)))
5935 nil)
5937 ;; `id-start' is equal to `id-end' if we've jumped over
5938 ;; an identifier that doesn't end with a symbol token.
5939 ;; That can occur e.g. for Java import directives on the
5940 ;; form "foo.bar.*".
5941 (when (and id-start (/= id-start id-end))
5942 (setq c-last-identifier-range
5943 (cons id-start id-end)))
5944 (goto-char id-end)
5945 (c-forward-syntactic-ws)
5946 (setq pos (point)
5947 res t)))
5949 (progn
5950 (goto-char pos)
5951 (when (or c-opt-identifier-concat-key
5952 c-recognize-<>-arglists)
5954 (cond
5955 ((and c-opt-identifier-concat-key
5956 (looking-at c-opt-identifier-concat-key))
5957 ;; Got a concatenated identifier. This handles the
5958 ;; cases with tricky syntactic whitespace that aren't
5959 ;; covered in `c-identifier-key'.
5960 (goto-char (match-end 0))
5961 (c-forward-syntactic-ws)
5964 ((and c-recognize-<>-arglists
5965 (eq (char-after) ?<))
5966 ;; Maybe an angle bracket arglist.
5967 (when (let ((c-record-type-identifiers t)
5968 (c-record-found-types t))
5969 (c-forward-<>-arglist nil))
5971 (c-add-type start (1+ pos))
5972 (c-forward-syntactic-ws)
5973 (setq pos (point)
5974 c-last-identifier-range nil)
5976 (if (and c-opt-identifier-concat-key
5977 (looking-at c-opt-identifier-concat-key))
5979 ;; Continue if there's an identifier concatenation
5980 ;; operator after the template argument.
5981 (progn
5982 (when (and c-record-type-identifiers id-start)
5983 (c-record-ref-id (cons id-start id-end)))
5984 (forward-char 2)
5985 (c-forward-syntactic-ws)
5988 (when (and c-record-type-identifiers id-start)
5989 (c-record-type-id (cons id-start id-end)))
5990 (setq res 'template)
5991 nil)))
5992 )))))
5994 (goto-char pos)
5995 res))
5997 (defun c-forward-type (&optional brace-block-too)
5998 ;; Move forward over a type spec if at the beginning of one,
5999 ;; stopping at the next following token. The keyword "typedef"
6000 ;; isn't part of a type spec here.
6002 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
6003 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
6004 ;; The current (2009-03-10) intention is to convert all uses of
6005 ;; `c-forward-type' to call with this parameter set, then to
6006 ;; eliminate it.
6008 ;; Return
6009 ;; o - t if it's a known type that can't be a name or other
6010 ;; expression;
6011 ;; o - 'known if it's an otherwise known type (according to
6012 ;; `*-font-lock-extra-types');
6013 ;; o - 'prefix if it's a known prefix of a type;
6014 ;; o - 'found if it's a type that matches one in `c-found-types';
6015 ;; o - 'maybe if it's an identifier that might be a type; or
6016 ;; o - nil if it can't be a type (the point isn't moved then).
6018 ;; The point is assumed to be at the beginning of a token.
6020 ;; Note that this function doesn't skip past the brace definition
6021 ;; that might be considered part of the type, e.g.
6022 ;; "enum {a, b, c} foo".
6024 ;; This function records identifier ranges on
6025 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6026 ;; `c-record-type-identifiers' is non-nil.
6028 ;; This function might do hidden buffer changes.
6029 (when (and c-recognize-<>-arglists
6030 (looking-at "<"))
6031 (c-forward-<>-arglist t)
6032 (c-forward-syntactic-ws))
6034 (let ((start (point)) pos res name-res id-start id-end id-range)
6036 ;; Skip leading type modifiers. If any are found we know it's a
6037 ;; prefix of a type.
6038 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
6039 (while (looking-at c-opt-type-modifier-key)
6040 (goto-char (match-end 1))
6041 (c-forward-syntactic-ws)
6042 (setq res 'prefix)))
6044 (cond
6045 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
6046 ; "typedef".
6047 (goto-char (match-end 1))
6048 (c-forward-syntactic-ws)
6049 (setq pos (point))
6051 (setq name-res (c-forward-name))
6052 (setq res (not (null name-res)))
6053 (when (eq name-res t)
6054 ;; In many languages the name can be used without the
6055 ;; prefix, so we add it to `c-found-types'.
6056 (c-add-type pos (point))
6057 (when (and c-record-type-identifiers
6058 c-last-identifier-range)
6059 (c-record-type-id c-last-identifier-range)))
6060 (when (and brace-block-too
6061 (memq res '(t nil))
6062 (eq (char-after) ?\{)
6063 (save-excursion
6064 (c-safe
6065 (progn (c-forward-sexp)
6066 (c-forward-syntactic-ws)
6067 (setq pos (point))))))
6068 (goto-char pos)
6069 (setq res t))
6070 (unless res (goto-char start))) ; invalid syntax
6072 ((progn
6073 (setq pos nil)
6074 (if (looking-at c-identifier-start)
6075 (save-excursion
6076 (setq id-start (point)
6077 name-res (c-forward-name))
6078 (when name-res
6079 (setq id-end (point)
6080 id-range c-last-identifier-range))))
6081 (and (cond ((looking-at c-primitive-type-key)
6082 (setq res t))
6083 ((c-with-syntax-table c-identifier-syntax-table
6084 (looking-at c-known-type-key))
6085 (setq res 'known)))
6086 (or (not id-end)
6087 (>= (save-excursion
6088 (save-match-data
6089 (goto-char (match-end 1))
6090 (c-forward-syntactic-ws)
6091 (setq pos (point))))
6092 id-end)
6093 (setq res nil))))
6094 ;; Looking at a primitive or known type identifier. We've
6095 ;; checked for a name first so that we don't go here if the
6096 ;; known type match only is a prefix of another name.
6098 (setq id-end (match-end 1))
6100 (when (and c-record-type-identifiers
6101 (or c-promote-possible-types (eq res t)))
6102 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6104 (if (and c-opt-type-component-key
6105 (save-match-data
6106 (looking-at c-opt-type-component-key)))
6107 ;; There might be more keywords for the type.
6108 (let (safe-pos)
6109 (c-forward-keyword-clause 1)
6110 (while (progn
6111 (setq safe-pos (point))
6112 (looking-at c-opt-type-component-key))
6113 (when (and c-record-type-identifiers
6114 (looking-at c-primitive-type-key))
6115 (c-record-type-id (cons (match-beginning 1)
6116 (match-end 1))))
6117 (c-forward-keyword-clause 1))
6118 (if (looking-at c-primitive-type-key)
6119 (progn
6120 (when c-record-type-identifiers
6121 (c-record-type-id (cons (match-beginning 1)
6122 (match-end 1))))
6123 (c-forward-keyword-clause 1)
6124 (setq res t))
6125 (goto-char safe-pos)
6126 (setq res 'prefix)))
6127 (unless (save-match-data (c-forward-keyword-clause 1))
6128 (if pos
6129 (goto-char pos)
6130 (goto-char (match-end 1))
6131 (c-forward-syntactic-ws)))))
6133 (name-res
6134 (cond ((eq name-res t)
6135 ;; A normal identifier.
6136 (goto-char id-end)
6137 (if (or res c-promote-possible-types)
6138 (progn
6139 (c-add-type id-start id-end)
6140 (when (and c-record-type-identifiers id-range)
6141 (c-record-type-id id-range))
6142 (unless res
6143 (setq res 'found)))
6144 (setq res (if (c-check-type id-start id-end)
6145 ;; It's an identifier that has been used as
6146 ;; a type somewhere else.
6147 'found
6148 ;; It's an identifier that might be a type.
6149 'maybe))))
6150 ((eq name-res 'template)
6151 ;; A template is a type.
6152 (goto-char id-end)
6153 (setq res t))
6155 ;; Otherwise it's an operator identifier, which is not a type.
6156 (goto-char start)
6157 (setq res nil)))))
6159 (when res
6160 ;; Skip trailing type modifiers. If any are found we know it's
6161 ;; a type.
6162 (when c-opt-type-modifier-key
6163 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
6164 (goto-char (match-end 1))
6165 (c-forward-syntactic-ws)
6166 (setq res t)))
6167 ;; Step over any type suffix operator. Do not let the existence
6168 ;; of these alter the classification of the found type, since
6169 ;; these operators typically are allowed in normal expressions
6170 ;; too.
6171 (when c-opt-type-suffix-key
6172 (while (looking-at c-opt-type-suffix-key)
6173 (goto-char (match-end 1))
6174 (c-forward-syntactic-ws)))
6176 (when c-opt-type-concat-key ; Only/mainly for pike.
6177 ;; Look for a trailing operator that concatenates the type
6178 ;; with a following one, and if so step past that one through
6179 ;; a recursive call. Note that we don't record concatenated
6180 ;; types in `c-found-types' - it's the component types that
6181 ;; are recorded when appropriate.
6182 (setq pos (point))
6183 (let* ((c-promote-possible-types (or (memq res '(t known))
6184 c-promote-possible-types))
6185 ;; If we can't promote then set `c-record-found-types' so that
6186 ;; we can merge in the types from the second part afterwards if
6187 ;; it turns out to be a known type there.
6188 (c-record-found-types (and c-record-type-identifiers
6189 (not c-promote-possible-types)))
6190 subres)
6191 (if (and (looking-at c-opt-type-concat-key)
6193 (progn
6194 (goto-char (match-end 1))
6195 (c-forward-syntactic-ws)
6196 (setq subres (c-forward-type))))
6198 (progn
6199 ;; If either operand certainly is a type then both are, but we
6200 ;; don't let the existence of the operator itself promote two
6201 ;; uncertain types to a certain one.
6202 (cond ((eq res t))
6203 ((eq subres t)
6204 (unless (eq name-res 'template)
6205 (c-add-type id-start id-end))
6206 (when (and c-record-type-identifiers id-range)
6207 (c-record-type-id id-range))
6208 (setq res t))
6209 ((eq res 'known))
6210 ((eq subres 'known)
6211 (setq res 'known))
6212 ((eq res 'found))
6213 ((eq subres 'found)
6214 (setq res 'found))
6216 (setq res 'maybe)))
6218 (when (and (eq res t)
6219 (consp c-record-found-types))
6220 ;; Merge in the ranges of any types found by the second
6221 ;; `c-forward-type'.
6222 (setq c-record-type-identifiers
6223 ;; `nconc' doesn't mind that the tail of
6224 ;; `c-record-found-types' is t.
6225 (nconc c-record-found-types
6226 c-record-type-identifiers))))
6228 (goto-char pos))))
6230 (when (and c-record-found-types (memq res '(known found)) id-range)
6231 (setq c-record-found-types
6232 (cons id-range c-record-found-types))))
6234 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6236 res))
6238 (defun c-forward-annotation ()
6239 ;; Used for Java code only at the moment. Assumes point is on the
6240 ;; @, moves forward an annotation. returns nil if there is no
6241 ;; annotation at point.
6242 (and (looking-at "@")
6243 (progn (forward-char) t)
6244 (c-forward-type)
6245 (progn (c-forward-syntactic-ws) t)
6246 (if (looking-at "(")
6247 (c-go-list-forward)
6248 t)))
6251 ;; Handling of large scale constructs like statements and declarations.
6253 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6254 ;; defsubst or perhaps even a defun, but it contains lots of free
6255 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6256 (defmacro c-fdoc-shift-type-backward (&optional short)
6257 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6258 ;; of types when parsing a declaration, which means that it
6259 ;; sometimes consumes the identifier in the declaration as a type.
6260 ;; This is used to "backtrack" and make the last type be treated as
6261 ;; an identifier instead.
6262 `(progn
6263 ,(unless short
6264 ;; These identifiers are bound only in the inner let.
6265 '(setq identifier-type at-type
6266 identifier-start type-start
6267 got-parens nil
6268 got-identifier t
6269 got-suffix t
6270 got-suffix-after-parens id-start
6271 paren-depth 0))
6273 (if (setq at-type (if (eq backup-at-type 'prefix)
6275 backup-at-type))
6276 (setq type-start backup-type-start
6277 id-start backup-id-start)
6278 (setq type-start start-pos
6279 id-start start-pos))
6281 ;; When these flags already are set we've found specifiers that
6282 ;; unconditionally signal these attributes - backtracking doesn't
6283 ;; change that. So keep them set in that case.
6284 (or at-type-decl
6285 (setq at-type-decl backup-at-type-decl))
6286 (or maybe-typeless
6287 (setq maybe-typeless backup-maybe-typeless))
6289 ,(unless short
6290 ;; This identifier is bound only in the inner let.
6291 '(setq start id-start))))
6293 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6294 ;; Move forward over a declaration or a cast if at the start of one.
6295 ;; The point is assumed to be at the start of some token. Nil is
6296 ;; returned if no declaration or cast is recognized, and the point
6297 ;; is clobbered in that case.
6299 ;; If a declaration is parsed:
6301 ;; The point is left at the first token after the first complete
6302 ;; declarator, if there is one. The return value is a cons where
6303 ;; the car is the position of the first token in the declarator. (See
6304 ;; below for the cdr.)
6305 ;; Some examples:
6307 ;; void foo (int a, char *b) stuff ...
6308 ;; car ^ ^ point
6309 ;; float (*a)[], b;
6310 ;; car ^ ^ point
6311 ;; unsigned int a = c_style_initializer, b;
6312 ;; car ^ ^ point
6313 ;; unsigned int a (cplusplus_style_initializer), b;
6314 ;; car ^ ^ point (might change)
6315 ;; class Foo : public Bar {}
6316 ;; car ^ ^ point
6317 ;; class PikeClass (int a, string b) stuff ...
6318 ;; car ^ ^ point
6319 ;; enum bool;
6320 ;; car ^ ^ point
6321 ;; enum bool flag;
6322 ;; car ^ ^ point
6323 ;; void cplusplus_function (int x) throw (Bad);
6324 ;; car ^ ^ point
6325 ;; Foo::Foo (int b) : Base (b) {}
6326 ;; car ^ ^ point
6328 ;; The cdr of the return value is non-nil when a
6329 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6330 ;; Specifically it is a dotted pair (A . B) where B is t when a
6331 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6332 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6333 ;; specifier is present. I.e., (some of) the declared
6334 ;; identifier(s) are types.
6336 ;; If a cast is parsed:
6338 ;; The point is left at the first token after the closing paren of
6339 ;; the cast. The return value is `cast'. Note that the start
6340 ;; position must be at the first token inside the cast parenthesis
6341 ;; to recognize it.
6343 ;; PRECEDING-TOKEN-END is the first position after the preceding
6344 ;; token, i.e. on the other side of the syntactic ws from the point.
6345 ;; Use a value less than or equal to (point-min) if the point is at
6346 ;; the first token in (the visible part of) the buffer.
6348 ;; CONTEXT is a symbol that describes the context at the point:
6349 ;; 'decl In a comma-separated declaration context (typically
6350 ;; inside a function declaration arglist).
6351 ;; '<> In an angle bracket arglist.
6352 ;; 'arglist Some other type of arglist.
6353 ;; nil Some other context or unknown context. Includes
6354 ;; within the parens of an if, for, ... construct.
6356 ;; LAST-CAST-END is the first token after the closing paren of a
6357 ;; preceding cast, or nil if none is known. If
6358 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6359 ;; the position after the closest preceding call where a cast was
6360 ;; matched. In that case it's used to discover chains of casts like
6361 ;; "(a) (b) c".
6363 ;; This function records identifier ranges on
6364 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6365 ;; `c-record-type-identifiers' is non-nil.
6367 ;; This function might do hidden buffer changes.
6369 (let (;; `start-pos' is used below to point to the start of the
6370 ;; first type, i.e. after any leading specifiers. It might
6371 ;; also point at the beginning of the preceding syntactic
6372 ;; whitespace.
6373 (start-pos (point))
6374 ;; Set to the result of `c-forward-type'.
6375 at-type
6376 ;; The position of the first token in what we currently
6377 ;; believe is the type in the declaration or cast, after any
6378 ;; specifiers and their associated clauses.
6379 type-start
6380 ;; The position of the first token in what we currently
6381 ;; believe is the declarator for the first identifier. Set
6382 ;; when the type is found, and moved forward over any
6383 ;; `c-decl-hangon-kwds' and their associated clauses that
6384 ;; occurs after the type.
6385 id-start
6386 ;; These store `at-type', `type-start' and `id-start' of the
6387 ;; identifier before the one in those variables. The previous
6388 ;; identifier might turn out to be the real type in a
6389 ;; declaration if the last one has to be the declarator in it.
6390 ;; If `backup-at-type' is nil then the other variables have
6391 ;; undefined values.
6392 backup-at-type backup-type-start backup-id-start
6393 ;; Set if we've found a specifier (apart from "typedef") that makes
6394 ;; the defined identifier(s) types.
6395 at-type-decl
6396 ;; Set if we've a "typedef" keyword.
6397 at-typedef
6398 ;; Set if we've found a specifier that can start a declaration
6399 ;; where there's no type.
6400 maybe-typeless
6401 ;; If a specifier is found that also can be a type prefix,
6402 ;; these flags are set instead of those above. If we need to
6403 ;; back up an identifier, they are copied to the real flag
6404 ;; variables. Thus they only take effect if we fail to
6405 ;; interpret it as a type.
6406 backup-at-type-decl backup-maybe-typeless
6407 ;; Whether we've found a declaration or a cast. We might know
6408 ;; this before we've found the type in it. It's 'ids if we've
6409 ;; found two consecutive identifiers (usually a sure sign, but
6410 ;; we should allow that in labels too), and t if we've found a
6411 ;; specifier keyword (a 100% sure sign).
6412 at-decl-or-cast
6413 ;; Set when we need to back up to parse this as a declaration
6414 ;; but not as a cast.
6415 backup-if-not-cast
6416 ;; For casts, the return position.
6417 cast-end
6418 ;; Save `c-record-type-identifiers' and
6419 ;; `c-record-ref-identifiers' since ranges are recorded
6420 ;; speculatively and should be thrown away if it turns out
6421 ;; that it isn't a declaration or cast.
6422 (save-rec-type-ids c-record-type-identifiers)
6423 (save-rec-ref-ids c-record-ref-identifiers))
6425 (while (c-forward-annotation)
6426 (c-forward-syntactic-ws))
6428 ;; Check for a type. Unknown symbols are treated as possible
6429 ;; types, but they could also be specifiers disguised through
6430 ;; macros like __INLINE__, so we recognize both types and known
6431 ;; specifiers after them too.
6432 (while
6433 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6435 ;; Look for a specifier keyword clause.
6436 (when (or (looking-at c-prefix-spec-kwds-re)
6437 (and (c-major-mode-is 'java-mode)
6438 (looking-at "@[A-Za-z0-9]+")))
6439 (if (looking-at c-typedef-key)
6440 (setq at-typedef t))
6441 (setq kwd-sym (c-keyword-sym (match-string 1)))
6442 (save-excursion
6443 (c-forward-keyword-clause 1)
6444 (setq kwd-clause-end (point))))
6446 (when (setq found-type (c-forward-type t)) ; brace-block-too
6447 ;; Found a known or possible type or a prefix of a known type.
6449 (when at-type
6450 ;; Got two identifiers with nothing but whitespace
6451 ;; between them. That can only happen in declarations.
6452 (setq at-decl-or-cast 'ids)
6454 (when (eq at-type 'found)
6455 ;; If the previous identifier is a found type we
6456 ;; record it as a real one; it might be some sort of
6457 ;; alias for a prefix like "unsigned".
6458 (save-excursion
6459 (goto-char type-start)
6460 (let ((c-promote-possible-types t))
6461 (c-forward-type)))))
6463 (setq backup-at-type at-type
6464 backup-type-start type-start
6465 backup-id-start id-start
6466 at-type found-type
6467 type-start start
6468 id-start (point)
6469 ;; The previous ambiguous specifier/type turned out
6470 ;; to be a type since we've parsed another one after
6471 ;; it, so clear these backup flags.
6472 backup-at-type-decl nil
6473 backup-maybe-typeless nil))
6475 (if kwd-sym
6476 (progn
6477 ;; Handle known specifier keywords and
6478 ;; `c-decl-hangon-kwds' which can occur after known
6479 ;; types.
6481 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6482 ;; It's a hang-on keyword that can occur anywhere.
6483 (progn
6484 (setq at-decl-or-cast t)
6485 (if at-type
6486 ;; Move the identifier start position if
6487 ;; we've passed a type.
6488 (setq id-start kwd-clause-end)
6489 ;; Otherwise treat this as a specifier and
6490 ;; move the fallback position.
6491 (setq start-pos kwd-clause-end))
6492 (goto-char kwd-clause-end))
6494 ;; It's an ordinary specifier so we know that
6495 ;; anything before this can't be the type.
6496 (setq backup-at-type nil
6497 start-pos kwd-clause-end)
6499 (if found-type
6500 ;; It's ambiguous whether this keyword is a
6501 ;; specifier or a type prefix, so set the backup
6502 ;; flags. (It's assumed that `c-forward-type'
6503 ;; moved further than `c-forward-keyword-clause'.)
6504 (progn
6505 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6506 (setq backup-at-type-decl t))
6507 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6508 (setq backup-maybe-typeless t)))
6510 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6511 ;; This test only happens after we've scanned a type.
6512 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6513 (setq at-type-decl t))
6514 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6515 (setq maybe-typeless t))
6517 ;; Haven't matched a type so it's an unambiguous
6518 ;; specifier keyword and we know we're in a
6519 ;; declaration.
6520 (setq at-decl-or-cast t)
6522 (goto-char kwd-clause-end))))
6524 ;; If the type isn't known we continue so that we'll jump
6525 ;; over all specifiers and type identifiers. The reason
6526 ;; to do this for a known type prefix is to make things
6527 ;; like "unsigned INT16" work.
6528 (and found-type (not (eq found-type t))))))
6530 (cond
6531 ((eq at-type t)
6532 ;; If a known type was found, we still need to skip over any
6533 ;; hangon keyword clauses after it. Otherwise it has already
6534 ;; been done in the loop above.
6535 (while (looking-at c-decl-hangon-key)
6536 (c-forward-keyword-clause 1))
6537 (setq id-start (point)))
6539 ((eq at-type 'prefix)
6540 ;; A prefix type is itself a primitive type when it's not
6541 ;; followed by another type.
6542 (setq at-type t))
6544 ((not at-type)
6545 ;; Got no type but set things up to continue anyway to handle
6546 ;; the various cases when a declaration doesn't start with a
6547 ;; type.
6548 (setq id-start start-pos))
6550 ((and (eq at-type 'maybe)
6551 (c-major-mode-is 'c++-mode))
6552 ;; If it's C++ then check if the last "type" ends on the form
6553 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6554 ;; (con|de)structor.
6555 (save-excursion
6556 (let (name end-2 end-1)
6557 (goto-char id-start)
6558 (c-backward-syntactic-ws)
6559 (setq end-2 (point))
6560 (when (and
6561 (c-simple-skip-symbol-backward)
6562 (progn
6563 (setq name
6564 (buffer-substring-no-properties (point) end-2))
6565 ;; Cheating in the handling of syntactic ws below.
6566 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6567 (progn
6568 (setq end-1 (point))
6569 (c-simple-skip-symbol-backward))
6570 (>= (point) type-start)
6571 (equal (buffer-substring-no-properties (point) end-1)
6572 name))
6573 ;; It is a (con|de)structor name. In that case the
6574 ;; declaration is typeless so zap out any preceding
6575 ;; identifier(s) that we might have taken as types.
6576 (goto-char type-start)
6577 (setq at-type nil
6578 backup-at-type nil
6579 id-start type-start))))))
6581 ;; Check for and step over a type decl expression after the thing
6582 ;; that is or might be a type. This can't be skipped since we
6583 ;; need the correct end position of the declarator for
6584 ;; `max-type-decl-end-*'.
6585 (let ((start (point)) (paren-depth 0) pos
6586 ;; True if there's a non-open-paren match of
6587 ;; `c-type-decl-prefix-key'.
6588 got-prefix
6589 ;; True if the declarator is surrounded by a parenthesis pair.
6590 got-parens
6591 ;; True if there is an identifier in the declarator.
6592 got-identifier
6593 ;; True if there's a non-close-paren match of
6594 ;; `c-type-decl-suffix-key'.
6595 got-suffix
6596 ;; True if there's a prefix match outside the outermost
6597 ;; paren pair that surrounds the declarator.
6598 got-prefix-before-parens
6599 ;; True if there's a suffix match outside the outermost
6600 ;; paren pair that surrounds the declarator. The value is
6601 ;; the position of the first suffix match.
6602 got-suffix-after-parens
6603 ;; True if we've parsed the type decl to a token that is
6604 ;; known to end declarations in this context.
6605 at-decl-end
6606 ;; The earlier values of `at-type' and `type-start' if we've
6607 ;; shifted the type backwards.
6608 identifier-type identifier-start
6609 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6610 ;; turn it off during the name skipping below to avoid
6611 ;; getting `c-type' properties that might be bogus. That
6612 ;; can happen since we don't know if
6613 ;; `c-restricted-<>-arglists' will be correct inside the
6614 ;; arglist paren that gets entered.
6615 c-parse-and-markup-<>-arglists)
6617 (goto-char id-start)
6619 ;; Skip over type decl prefix operators. (Note similar code in
6620 ;; `c-font-lock-declarators'.)
6621 (while (and (looking-at c-type-decl-prefix-key)
6622 (if (and (c-major-mode-is 'c++-mode)
6623 (match-beginning 3))
6624 ;; If the second submatch matches in C++ then
6625 ;; we're looking at an identifier that's a
6626 ;; prefix only if it specifies a member pointer.
6627 (when (setq got-identifier (c-forward-name))
6628 (if (looking-at "\\(::\\)")
6629 ;; We only check for a trailing "::" and
6630 ;; let the "*" that should follow be
6631 ;; matched in the next round.
6632 (progn (setq got-identifier nil) t)
6633 ;; It turned out to be the real identifier,
6634 ;; so stop.
6635 nil))
6638 (if (eq (char-after) ?\()
6639 (progn
6640 (setq paren-depth (1+ paren-depth))
6641 (forward-char))
6642 (unless got-prefix-before-parens
6643 (setq got-prefix-before-parens (= paren-depth 0)))
6644 (setq got-prefix t)
6645 (goto-char (match-end 1)))
6646 (c-forward-syntactic-ws))
6648 (setq got-parens (> paren-depth 0))
6650 ;; Skip over an identifier.
6651 (or got-identifier
6652 (and (looking-at c-identifier-start)
6653 (setq got-identifier (c-forward-name))))
6655 ;; Skip over type decl suffix operators.
6656 (while (if (looking-at c-type-decl-suffix-key)
6658 (if (eq (char-after) ?\))
6659 (when (> paren-depth 0)
6660 (setq paren-depth (1- paren-depth))
6661 (forward-char)
6663 (when (if (save-match-data (looking-at "\\s\("))
6664 (c-safe (c-forward-sexp 1) t)
6665 (goto-char (match-end 1))
6667 (when (and (not got-suffix-after-parens)
6668 (= paren-depth 0))
6669 (setq got-suffix-after-parens (match-beginning 0)))
6670 (setq got-suffix t)))
6672 ;; No suffix matched. We might have matched the
6673 ;; identifier as a type and the open paren of a
6674 ;; function arglist as a type decl prefix. In that
6675 ;; case we should "backtrack": Reinterpret the last
6676 ;; type as the identifier, move out of the arglist and
6677 ;; continue searching for suffix operators.
6679 ;; Do this even if there's no preceding type, to cope
6680 ;; with old style function declarations in K&R C,
6681 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6682 ;; style declarations. That isn't applicable in an
6683 ;; arglist context, though.
6684 (when (and (= paren-depth 1)
6685 (not got-prefix-before-parens)
6686 (not (eq at-type t))
6687 (or backup-at-type
6688 maybe-typeless
6689 backup-maybe-typeless
6690 (when c-recognize-typeless-decls
6691 (not context)))
6692 (setq pos (c-up-list-forward (point)))
6693 (eq (char-before pos) ?\)))
6694 (c-fdoc-shift-type-backward)
6695 (goto-char pos)
6698 (c-forward-syntactic-ws))
6700 (when (and (or maybe-typeless backup-maybe-typeless)
6701 (not got-identifier)
6702 (not got-prefix)
6703 at-type)
6704 ;; Have found no identifier but `c-typeless-decl-kwds' has
6705 ;; matched so we know we're inside a declaration. The
6706 ;; preceding type must be the identifier instead.
6707 (c-fdoc-shift-type-backward))
6709 (setq
6710 at-decl-or-cast
6711 (catch 'at-decl-or-cast
6713 ;; CASE 1
6714 (when (> paren-depth 0)
6715 ;; Encountered something inside parens that isn't matched by
6716 ;; the `c-type-decl-*' regexps, so it's not a type decl
6717 ;; expression. Try to skip out to the same paren depth to
6718 ;; not confuse the cast check below.
6719 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6720 ;; If we've found a specifier keyword then it's a
6721 ;; declaration regardless.
6722 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6724 (setq at-decl-end
6725 (looking-at (cond ((eq context '<>) "[,>]")
6726 (context "[,\)]")
6727 (t "[,;]"))))
6729 ;; Now we've collected info about various characteristics of
6730 ;; the construct we're looking at. Below follows a decision
6731 ;; tree based on that. It's ordered to check more certain
6732 ;; signs before less certain ones.
6734 (if got-identifier
6735 (progn
6737 ;; CASE 2
6738 (when (and (or at-type maybe-typeless)
6739 (not (or got-prefix got-parens)))
6740 ;; Got another identifier directly after the type, so it's a
6741 ;; declaration.
6742 (throw 'at-decl-or-cast t))
6744 (when (and got-parens
6745 (not got-prefix)
6746 (not got-suffix-after-parens)
6747 (or backup-at-type
6748 maybe-typeless
6749 backup-maybe-typeless))
6750 ;; Got a declaration of the form "foo bar (gnu);" where we've
6751 ;; recognized "bar" as the type and "gnu" as the declarator.
6752 ;; In this case it's however more likely that "bar" is the
6753 ;; declarator and "gnu" a function argument or initializer (if
6754 ;; `c-recognize-paren-inits' is set), since the parens around
6755 ;; "gnu" would be superfluous if it's a declarator. Shift the
6756 ;; type one step backward.
6757 (c-fdoc-shift-type-backward)))
6759 ;; Found no identifier.
6761 (if backup-at-type
6762 (progn
6765 ;; CASE 3
6766 (when (= (point) start)
6767 ;; Got a plain list of identifiers. If a colon follows it's
6768 ;; a valid label, or maybe a bitfield. Otherwise the last
6769 ;; one probably is the declared identifier and we should
6770 ;; back up to the previous type, providing it isn't a cast.
6771 (if (and (eq (char-after) ?:)
6772 (not (c-major-mode-is 'java-mode)))
6773 (cond
6774 ;; If we've found a specifier keyword then it's a
6775 ;; declaration regardless.
6776 ((eq at-decl-or-cast t)
6777 (throw 'at-decl-or-cast t))
6778 ((and c-has-bitfields
6779 (eq at-decl-or-cast 'ids)) ; bitfield.
6780 (setq backup-if-not-cast t)
6781 (throw 'at-decl-or-cast t)))
6783 (setq backup-if-not-cast t)
6784 (throw 'at-decl-or-cast t)))
6786 ;; CASE 4
6787 (when (and got-suffix
6788 (not got-prefix)
6789 (not got-parens))
6790 ;; Got a plain list of identifiers followed by some suffix.
6791 ;; If this isn't a cast then the last identifier probably is
6792 ;; the declared one and we should back up to the previous
6793 ;; type.
6794 (setq backup-if-not-cast t)
6795 (throw 'at-decl-or-cast t)))
6797 ;; CASE 5
6798 (when (eq at-type t)
6799 ;; If the type is known we know that there can't be any
6800 ;; identifier somewhere else, and it's only in declarations in
6801 ;; e.g. function prototypes and in casts that the identifier may
6802 ;; be left out.
6803 (throw 'at-decl-or-cast t))
6805 (when (= (point) start)
6806 ;; Only got a single identifier (parsed as a type so far).
6807 ;; CASE 6
6808 (if (and
6809 ;; Check that the identifier isn't at the start of an
6810 ;; expression.
6811 at-decl-end
6812 (cond
6813 ((eq context 'decl)
6814 ;; Inside an arglist that contains declarations. If K&R
6815 ;; style declarations and parenthesis style initializers
6816 ;; aren't allowed then the single identifier must be a
6817 ;; type, else we require that it's known or found
6818 ;; (primitive types are handled above).
6819 (or (and (not c-recognize-knr-p)
6820 (not c-recognize-paren-inits))
6821 (memq at-type '(known found))))
6822 ((eq context '<>)
6823 ;; Inside a template arglist. Accept known and found
6824 ;; types; other identifiers could just as well be
6825 ;; constants in C++.
6826 (memq at-type '(known found)))))
6827 (throw 'at-decl-or-cast t)
6828 ;; CASE 7
6829 ;; Can't be a valid declaration or cast, but if we've found a
6830 ;; specifier it can't be anything else either, so treat it as
6831 ;; an invalid/unfinished declaration or cast.
6832 (throw 'at-decl-or-cast at-decl-or-cast))))
6834 (if (and got-parens
6835 (not got-prefix)
6836 (not context)
6837 (not (eq at-type t))
6838 (or backup-at-type
6839 maybe-typeless
6840 backup-maybe-typeless
6841 (when c-recognize-typeless-decls
6842 (or (not got-suffix)
6843 (not (looking-at
6844 c-after-suffixed-type-maybe-decl-key))))))
6845 ;; Got an empty paren pair and a preceding type that probably
6846 ;; really is the identifier. Shift the type backwards to make
6847 ;; the last one the identifier. This is analogous to the
6848 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6849 ;; above.
6851 ;; Exception: In addition to the conditions in that
6852 ;; "backtracking" code, do not shift backward if we're not
6853 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6854 ;; Since there's no preceding type, the shift would mean that
6855 ;; the declaration is typeless. But if the regexp doesn't match
6856 ;; then we will simply fall through in the tests below and not
6857 ;; recognize it at all, so it's better to try it as an abstract
6858 ;; declarator instead.
6859 (c-fdoc-shift-type-backward)
6861 ;; Still no identifier.
6862 ;; CASE 8
6863 (when (and got-prefix (or got-parens got-suffix))
6864 ;; Require `got-prefix' together with either `got-parens' or
6865 ;; `got-suffix' to recognize it as an abstract declarator:
6866 ;; `got-parens' only is probably an empty function call.
6867 ;; `got-suffix' only can build an ordinary expression together
6868 ;; with the preceding identifier which we've taken as a type.
6869 ;; We could actually accept on `got-prefix' only, but that can
6870 ;; easily occur temporarily while writing an expression so we
6871 ;; avoid that case anyway. We could do a better job if we knew
6872 ;; the point when the fontification was invoked.
6873 (throw 'at-decl-or-cast t))
6875 ;; CASE 9
6876 (when (and at-type
6877 (not got-prefix)
6878 (not got-parens)
6879 got-suffix-after-parens
6880 (eq (char-after got-suffix-after-parens) ?\())
6881 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6882 ;; normal function call after all (or perhaps a C++ style object
6883 ;; instantiation expression).
6884 (throw 'at-decl-or-cast nil))))
6886 ;; CASE 10
6887 (when at-decl-or-cast
6888 ;; By now we've located the type in the declaration that we know
6889 ;; we're in.
6890 (throw 'at-decl-or-cast t))
6892 ;; CASE 11
6893 (when (and got-identifier
6894 (not context)
6895 (looking-at c-after-suffixed-type-decl-key)
6896 (if (and got-parens
6897 (not got-prefix)
6898 (not got-suffix)
6899 (not (eq at-type t)))
6900 ;; Shift the type backward in the case that there's a
6901 ;; single identifier inside parens. That can only
6902 ;; occur in K&R style function declarations so it's
6903 ;; more likely that it really is a function call.
6904 ;; Therefore we only do this after
6905 ;; `c-after-suffixed-type-decl-key' has matched.
6906 (progn (c-fdoc-shift-type-backward) t)
6907 got-suffix-after-parens))
6908 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6909 (throw 'at-decl-or-cast t))
6911 ;; CASE 12
6912 (when (and (or got-prefix (not got-parens))
6913 (memq at-type '(t known)))
6914 ;; It's a declaration if a known type precedes it and it can't be a
6915 ;; function call.
6916 (throw 'at-decl-or-cast t))
6918 ;; If we get here we can't tell if this is a type decl or a normal
6919 ;; expression by looking at it alone. (That's under the assumption
6920 ;; that normal expressions always can look like type decl expressions,
6921 ;; which isn't really true but the cases where it doesn't hold are so
6922 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6923 ;; the effort to look for them.)
6925 (unless (or at-decl-end (looking-at "=[^=]"))
6926 ;; If this is a declaration it should end here or its initializer(*)
6927 ;; should start here, so check for allowed separation tokens. Note
6928 ;; that this rule doesn't work e.g. with a K&R arglist after a
6929 ;; function header.
6931 ;; *) Don't check for C++ style initializers using parens
6932 ;; since those already have been matched as suffixes.
6934 ;; If `at-decl-or-cast' is then we've found some other sign that
6935 ;; it's a declaration or cast, so then it's probably an
6936 ;; invalid/unfinished one.
6937 (throw 'at-decl-or-cast at-decl-or-cast))
6939 ;; Below are tests that only should be applied when we're certain to
6940 ;; not have parsed halfway through an expression.
6942 ;; CASE 14
6943 (when (memq at-type '(t known))
6944 ;; The expression starts with a known type so treat it as a
6945 ;; declaration.
6946 (throw 'at-decl-or-cast t))
6948 ;; CASE 15
6949 (when (and (c-major-mode-is 'c++-mode)
6950 ;; In C++ we check if the identifier is a known type, since
6951 ;; (con|de)structors use the class name as identifier.
6952 ;; We've always shifted over the identifier as a type and
6953 ;; then backed up again in this case.
6954 identifier-type
6955 (or (memq identifier-type '(found known))
6956 (and (eq (char-after identifier-start) ?~)
6957 ;; `at-type' probably won't be 'found for
6958 ;; destructors since the "~" is then part of the
6959 ;; type name being checked against the list of
6960 ;; known types, so do a check without that
6961 ;; operator.
6962 (or (save-excursion
6963 (goto-char (1+ identifier-start))
6964 (c-forward-syntactic-ws)
6965 (c-with-syntax-table
6966 c-identifier-syntax-table
6967 (looking-at c-known-type-key)))
6968 (save-excursion
6969 (goto-char (1+ identifier-start))
6970 ;; We have already parsed the type earlier,
6971 ;; so it'd be possible to cache the end
6972 ;; position instead of redoing it here, but
6973 ;; then we'd need to keep track of another
6974 ;; position everywhere.
6975 (c-check-type (point)
6976 (progn (c-forward-type)
6977 (point))))))))
6978 (throw 'at-decl-or-cast t))
6980 (if got-identifier
6981 (progn
6982 ;; CASE 16
6983 (when (and got-prefix-before-parens
6984 at-type
6985 (or at-decl-end (looking-at "=[^=]"))
6986 (not context)
6987 (not got-suffix))
6988 ;; Got something like "foo * bar;". Since we're not inside an
6989 ;; arglist it would be a meaningless expression because the
6990 ;; result isn't used. We therefore choose to recognize it as
6991 ;; a declaration. Do not allow a suffix since it could then
6992 ;; be a function call.
6993 (throw 'at-decl-or-cast t))
6995 ;; CASE 17
6996 (when (and (or got-suffix-after-parens
6997 (looking-at "=[^=]"))
6998 (eq at-type 'found)
6999 (not (eq context 'arglist)))
7000 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
7001 ;; be an odd expression or it could be a declaration. Treat
7002 ;; it as a declaration if "a" has been used as a type
7003 ;; somewhere else (if it's a known type we won't get here).
7004 (throw 'at-decl-or-cast t)))
7006 ;; CASE 18
7007 (when (and context
7008 (or got-prefix
7009 (and (eq context 'decl)
7010 (not c-recognize-paren-inits)
7011 (or got-parens got-suffix))))
7012 ;; Got a type followed by an abstract declarator. If `got-prefix'
7013 ;; is set it's something like "a *" without anything after it. If
7014 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
7015 ;; or similar, which we accept only if the context rules out
7016 ;; expressions.
7017 (throw 'at-decl-or-cast t)))
7019 ;; If we had a complete symbol table here (which rules out
7020 ;; `c-found-types') we should return t due to the disambiguation rule
7021 ;; (in at least C++) that anything that can be parsed as a declaration
7022 ;; is a declaration. Now we're being more defensive and prefer to
7023 ;; highlight things like "foo (bar);" as a declaration only if we're
7024 ;; inside an arglist that contains declarations.
7025 (eq context 'decl))))
7027 ;; The point is now after the type decl expression.
7029 (cond
7030 ;; Check for a cast.
7031 ((save-excursion
7032 (and
7033 c-cast-parens
7035 ;; Should be the first type/identifier in a cast paren.
7036 (> preceding-token-end (point-min))
7037 (memq (char-before preceding-token-end) c-cast-parens)
7039 ;; The closing paren should follow.
7040 (progn
7041 (c-forward-syntactic-ws)
7042 (looking-at "\\s\)"))
7044 ;; There should be a primary expression after it.
7045 (let (pos)
7046 (forward-char)
7047 (c-forward-syntactic-ws)
7048 (setq cast-end (point))
7049 (and (looking-at c-primary-expr-regexp)
7050 (progn
7051 (setq pos (match-end 0))
7053 ;; Check if the expression begins with a prefix keyword.
7054 (match-beginning 2)
7055 (if (match-beginning 1)
7056 ;; Expression begins with an ambiguous operator. Treat
7057 ;; it as a cast if it's a type decl or if we've
7058 ;; recognized the type somewhere else.
7059 (or at-decl-or-cast
7060 (memq at-type '(t known found)))
7061 ;; Unless it's a keyword, it's the beginning of a primary
7062 ;; expression.
7063 (not (looking-at c-keywords-regexp)))))
7064 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
7065 ;; that it matched a whole one so that we don't e.g. confuse
7066 ;; the operator '-' with '->'. It's ok if it matches further,
7067 ;; though, since it e.g. can match the float '.5' while the
7068 ;; operator regexp only matches '.'.
7069 (or (not (looking-at c-nonsymbol-token-regexp))
7070 (<= (match-end 0) pos))))
7072 ;; There should either be a cast before it or something that isn't an
7073 ;; identifier or close paren.
7074 (> preceding-token-end (point-min))
7075 (progn
7076 (goto-char (1- preceding-token-end))
7077 (or (eq (point) last-cast-end)
7078 (progn
7079 (c-backward-syntactic-ws)
7080 (if (< (skip-syntax-backward "w_") 0)
7081 ;; It's a symbol. Accept it only if it's one of the
7082 ;; keywords that can precede an expression (without
7083 ;; surrounding parens).
7084 (looking-at c-simple-stmt-key)
7085 (and
7086 ;; Check that it isn't a close paren (block close is ok,
7087 ;; though).
7088 (not (memq (char-before) '(?\) ?\])))
7089 ;; Check that it isn't a nonsymbol identifier.
7090 (not (c-on-identifier)))))))))
7092 ;; Handle the cast.
7093 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7094 (let ((c-promote-possible-types t))
7095 (goto-char type-start)
7096 (c-forward-type)))
7098 (goto-char cast-end)
7099 'cast)
7101 (at-decl-or-cast
7102 ;; We're at a declaration. Highlight the type and the following
7103 ;; declarators.
7105 (when backup-if-not-cast
7106 (c-fdoc-shift-type-backward t))
7108 (when (and (eq context 'decl) (looking-at ","))
7109 ;; Make sure to propagate the `c-decl-arg-start' property to
7110 ;; the next argument if it's set in this one, to cope with
7111 ;; interactive refontification.
7112 (c-put-c-type-property (point) 'c-decl-arg-start))
7114 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7115 (let ((c-promote-possible-types t))
7116 (save-excursion
7117 (goto-char type-start)
7118 (c-forward-type))))
7120 (cons id-start
7121 (and (or at-type-decl at-typedef)
7122 (cons at-type-decl at-typedef))))
7125 ;; False alarm. Restore the recorded ranges.
7126 (setq c-record-type-identifiers save-rec-type-ids
7127 c-record-ref-identifiers save-rec-ref-ids)
7128 nil))))
7130 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
7131 ;; Assuming that point is at the beginning of a token, check if it starts a
7132 ;; label and if so move over it and return non-nil (t in default situations,
7133 ;; specific symbols (see below) for interesting situations), otherwise don't
7134 ;; move and return nil. "Label" here means "most things with a colon".
7136 ;; More precisely, a "label" is regarded as one of:
7137 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7138 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7139 ;; bare "case", should the colon be missing. We return t;
7140 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7141 ;; return t;
7142 ;; (iv) One of QT's "extended" C++ variants of
7143 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7144 ;; Returns the symbol `qt-2kwds-colon'.
7145 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
7146 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
7147 ;; colon). Currently (2006-03), this applies only to Objective C's
7148 ;; keywords "@private", "@protected", and "@public". Returns t.
7150 ;; One of the things which will NOT be recognized as a label is a bit-field
7151 ;; element of a struct, something like "int foo:5".
7153 ;; The end of the label is taken to be just after the colon, or the end of
7154 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7155 ;; after the end on return. The terminating char gets marked with
7156 ;; `c-decl-end' to improve recognition of the following declaration or
7157 ;; statement.
7159 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7160 ;; label, if any, has already been marked up like that.
7162 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7163 ;; after the preceding token, i.e. on the other side of the
7164 ;; syntactic ws from the point. Use a value less than or equal to
7165 ;; (point-min) if the point is at the first token in (the visible
7166 ;; part of) the buffer.
7168 ;; The optional LIMIT limits the forward scan for the colon.
7170 ;; This function records the ranges of the label symbols on
7171 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7172 ;; non-nil.
7174 ;; This function might do hidden buffer changes.
7176 (let ((start (point))
7177 label-end
7178 qt-symbol-idx
7179 macro-start ; if we're in one.
7180 label-type
7181 kwd)
7182 (cond
7183 ;; "case" or "default" (Doesn't apply to AWK).
7184 ((looking-at c-label-kwds-regexp)
7185 (let ((kwd-end (match-end 1)))
7186 ;; Record only the keyword itself for fontification, since in
7187 ;; case labels the following is a constant expression and not
7188 ;; a label.
7189 (when c-record-type-identifiers
7190 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7192 ;; Find the label end.
7193 (goto-char kwd-end)
7194 (setq label-type
7195 (if (and (c-syntactic-re-search-forward
7196 ;; Stop on chars that aren't allowed in expressions,
7197 ;; and on operator chars that would be meaningless
7198 ;; there. FIXME: This doesn't cope with ?: operators.
7199 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7200 limit t t nil 1)
7201 (match-beginning 2))
7203 (progn ; there's a proper :
7204 (goto-char (match-beginning 2)) ; just after the :
7205 (c-put-c-type-property (1- (point)) 'c-decl-end)
7208 ;; It's an unfinished label. We consider the keyword enough
7209 ;; to recognize it as a label, so that it gets fontified.
7210 ;; Leave the point at the end of it, but don't put any
7211 ;; `c-decl-end' marker.
7212 (goto-char kwd-end)
7213 t))))
7215 ;; @private, @protected, @public, in Objective C, or similar.
7216 ((and c-opt-extra-label-key
7217 (looking-at c-opt-extra-label-key))
7218 ;; For a `c-opt-extra-label-key' match, we record the whole
7219 ;; thing for fontification. That's to get the leading '@' in
7220 ;; Objective-C protection labels fontified.
7221 (goto-char (match-end 1))
7222 (when c-record-type-identifiers
7223 (c-record-ref-id (cons (match-beginning 1) (point))))
7224 (c-put-c-type-property (1- (point)) 'c-decl-end)
7225 (setq label-type t))
7227 ;; All other cases of labels.
7228 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
7230 ;; A colon label must have something before the colon.
7231 (not (eq (char-after) ?:))
7233 ;; Check that we're not after a token that can't precede a label.
7235 ;; Trivially succeeds when there's no preceding token.
7236 ;; Succeeds when we're at a virtual semicolon.
7237 (if preceding-token-end
7238 (<= preceding-token-end (point-min))
7239 (save-excursion
7240 (c-backward-syntactic-ws)
7241 (setq preceding-token-end (point))
7242 (or (bobp)
7243 (c-at-vsemi-p))))
7245 ;; Check if we're after a label, if we're after a closing
7246 ;; paren that belong to statement, and with
7247 ;; `c-label-prefix-re'. It's done in different order
7248 ;; depending on `assume-markup' since the checks have
7249 ;; different expensiveness.
7250 (if assume-markup
7252 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7253 'c-decl-end)
7255 (save-excursion
7256 (goto-char (1- preceding-token-end))
7257 (c-beginning-of-current-token)
7258 (or (looking-at c-label-prefix-re)
7259 (looking-at c-block-stmt-1-key)))
7261 (and (eq (char-before preceding-token-end) ?\))
7262 (c-after-conditional)))
7265 (save-excursion
7266 (goto-char (1- preceding-token-end))
7267 (c-beginning-of-current-token)
7268 (or (looking-at c-label-prefix-re)
7269 (looking-at c-block-stmt-1-key)))
7271 (cond
7272 ((eq (char-before preceding-token-end) ?\))
7273 (c-after-conditional))
7275 ((eq (char-before preceding-token-end) ?:)
7276 ;; Might be after another label, so check it recursively.
7277 (save-restriction
7278 (save-excursion
7279 (goto-char (1- preceding-token-end))
7280 ;; Essentially the same as the
7281 ;; `c-syntactic-re-search-forward' regexp below.
7282 (setq macro-start
7283 (save-excursion (and (c-beginning-of-macro)
7284 (point))))
7285 (if macro-start (narrow-to-region macro-start (point-max)))
7286 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7287 ;; Note: the following should work instead of the
7288 ;; narrow-to-region above. Investigate why not,
7289 ;; sometime. ACM, 2006-03-31.
7290 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7291 ;; macro-start t)
7292 (let ((pte (point))
7293 ;; If the caller turned on recording for us,
7294 ;; it shouldn't apply when we check the
7295 ;; preceding label.
7296 c-record-type-identifiers)
7297 ;; A label can't start at a cpp directive. Check for
7298 ;; this, since c-forward-syntactic-ws would foul up on it.
7299 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7300 (c-forward-syntactic-ws)
7301 (c-forward-label nil pte start))))))))))
7303 ;; Point is still at the beginning of the possible label construct.
7305 ;; Check that the next nonsymbol token is ":", or that we're in one
7306 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7307 ;; arguments. FIXME: Should build this regexp from the language
7308 ;; constants.
7309 (cond
7310 ;; public: protected: private:
7311 ((and
7312 (c-major-mode-is 'c++-mode)
7313 (search-forward-regexp
7314 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7315 (progn (backward-char)
7316 (c-forward-syntactic-ws limit)
7317 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7318 (forward-char)
7319 (setq label-type t))
7320 ;; QT double keyword like "protected slots:" or goto target.
7321 ((progn (goto-char start) nil))
7322 ((when (c-syntactic-re-search-forward
7323 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7324 (backward-char)
7325 (setq label-end (point))
7326 (setq qt-symbol-idx
7327 (and (c-major-mode-is 'c++-mode)
7328 (string-match
7329 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7330 (buffer-substring start (point)))))
7331 (c-forward-syntactic-ws limit)
7332 (cond
7333 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7334 (forward-char)
7335 (setq label-type
7336 (if (or (string= "signals" ; Special QT macro
7337 (setq kwd (buffer-substring-no-properties start label-end)))
7338 (string= "Q_SIGNALS" kwd))
7339 'qt-1kwd-colon
7340 'goto-target)))
7341 ((and qt-symbol-idx
7342 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7343 (progn (c-forward-syntactic-ws limit)
7344 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7345 (forward-char)
7346 (setq label-type 'qt-2kwds-colon)))))))
7348 (save-restriction
7349 (narrow-to-region start (point))
7351 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7352 (catch 'check-label
7353 (goto-char start)
7354 (while (progn
7355 (when (looking-at c-nonlabel-token-key)
7356 (goto-char start)
7357 (setq label-type nil)
7358 (throw 'check-label nil))
7359 (and (c-safe (c-forward-sexp)
7360 (c-forward-syntactic-ws)
7362 (not (eobp)))))
7364 ;; Record the identifiers in the label for fontification, unless
7365 ;; it begins with `c-label-kwds' in which case the following
7366 ;; identifiers are part of a (constant) expression that
7367 ;; shouldn't be fontified.
7368 (when (and c-record-type-identifiers
7369 (progn (goto-char start)
7370 (not (looking-at c-label-kwds-regexp))))
7371 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7372 (c-record-ref-id (cons (match-beginning 0)
7373 (match-end 0)))))
7375 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7376 (goto-char (point-max)))))
7379 ;; Not a label.
7380 (goto-char start)))
7381 label-type))
7383 (defun c-forward-objc-directive ()
7384 ;; Assuming the point is at the beginning of a token, try to move
7385 ;; forward to the end of the Objective-C directive that starts
7386 ;; there. Return t if a directive was fully recognized, otherwise
7387 ;; the point is moved as far as one could be successfully parsed and
7388 ;; nil is returned.
7390 ;; This function records identifier ranges on
7391 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7392 ;; `c-record-type-identifiers' is non-nil.
7394 ;; This function might do hidden buffer changes.
7396 (let ((start (point))
7397 start-char
7398 (c-promote-possible-types t)
7400 ;; Turn off recognition of angle bracket arglists while parsing
7401 ;; types here since the protocol reference list might then be
7402 ;; considered part of the preceding name or superclass-name.
7403 c-recognize-<>-arglists)
7405 (if (or
7406 (when (looking-at
7407 (eval-when-compile
7408 (c-make-keywords-re t
7409 (append (c-lang-const c-protection-kwds objc)
7410 '("@end"))
7411 'objc-mode)))
7412 (goto-char (match-end 1))
7415 (and
7416 (looking-at
7417 (eval-when-compile
7418 (c-make-keywords-re t
7419 '("@interface" "@implementation" "@protocol")
7420 'objc-mode)))
7422 ;; Handle the name of the class itself.
7423 (progn
7424 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7425 ; at EOB.
7426 (goto-char (match-end 0))
7427 (setq lim (point))
7428 (c-skip-ws-forward)
7429 (c-forward-type))
7431 (catch 'break
7432 ;; Look for ": superclass-name" or "( category-name )".
7433 (when (looking-at "[:\(]")
7434 (setq start-char (char-after))
7435 (forward-char)
7436 (c-forward-syntactic-ws)
7437 (unless (c-forward-type) (throw 'break nil))
7438 (when (eq start-char ?\()
7439 (unless (eq (char-after) ?\)) (throw 'break nil))
7440 (forward-char)
7441 (c-forward-syntactic-ws)))
7443 ;; Look for a protocol reference list.
7444 (if (eq (char-after) ?<)
7445 (let ((c-recognize-<>-arglists t)
7446 (c-parse-and-markup-<>-arglists t)
7447 c-restricted-<>-arglists)
7448 (c-forward-<>-arglist t))
7449 t))))
7451 (progn
7452 (c-backward-syntactic-ws lim)
7453 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7454 (c-put-c-type-property (1- (point)) 'c-decl-end)
7457 (c-clear-c-type-property start (point) 'c-decl-end)
7458 nil)))
7460 (defun c-beginning-of-inheritance-list (&optional lim)
7461 ;; Go to the first non-whitespace after the colon that starts a
7462 ;; multiple inheritance introduction. Optional LIM is the farthest
7463 ;; back we should search.
7465 ;; This function might do hidden buffer changes.
7466 (c-with-syntax-table c++-template-syntax-table
7467 (c-backward-token-2 0 t lim)
7468 (while (and (or (looking-at c-symbol-start)
7469 (looking-at "[<,]\\|::"))
7470 (zerop (c-backward-token-2 1 t lim))))))
7472 (defun c-in-method-def-p ()
7473 ;; Return nil if we aren't in a method definition, otherwise the
7474 ;; position of the initial [+-].
7476 ;; This function might do hidden buffer changes.
7477 (save-excursion
7478 (beginning-of-line)
7479 (and c-opt-method-key
7480 (looking-at c-opt-method-key)
7481 (point))
7484 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7485 (defun c-in-gcc-asm-p ()
7486 ;; Return non-nil if point is within a gcc \"asm\" block.
7488 ;; This should be called with point inside an argument list.
7490 ;; Only one level of enclosing parentheses is considered, so for
7491 ;; instance `nil' is returned when in a function call within an asm
7492 ;; operand.
7494 ;; This function might do hidden buffer changes.
7496 (and c-opt-asm-stmt-key
7497 (save-excursion
7498 (beginning-of-line)
7499 (backward-up-list 1)
7500 (c-beginning-of-statement-1 (point-min) nil t)
7501 (looking-at c-opt-asm-stmt-key))))
7503 (defun c-at-toplevel-p ()
7504 "Return a determination as to whether point is \"at the top level\".
7505 Informally, \"at the top level\" is anywhere where you can write
7506 a function.
7508 More precisely, being at the top-level means that point is either
7509 outside any enclosing block (such as a function definition), or
7510 directly inside a class, namespace or other block that contains
7511 another declaration level.
7513 If point is not at the top-level (e.g. it is inside a method
7514 definition), then nil is returned. Otherwise, if point is at a
7515 top-level not enclosed within a class definition, t is returned.
7516 Otherwise, a 2-vector is returned where the zeroth element is the
7517 buffer position of the start of the class declaration, and the first
7518 element is the buffer position of the enclosing class's opening
7519 brace.
7521 Note that this function might do hidden buffer changes. See the
7522 comment at the start of cc-engine.el for more info."
7523 (let ((paren-state (c-parse-state)))
7524 (or (not (c-most-enclosing-brace paren-state))
7525 (c-search-uplist-for-classkey paren-state))))
7527 (defun c-just-after-func-arglist-p (&optional lim)
7528 ;; Return non-nil if the point is in the region after the argument
7529 ;; list of a function and its opening brace (or semicolon in case it
7530 ;; got no body). If there are K&R style argument declarations in
7531 ;; that region, the point has to be inside the first one for this
7532 ;; function to recognize it.
7534 ;; If successful, the point is moved to the first token after the
7535 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7536 ;; the position of the opening paren of the function arglist is
7537 ;; returned.
7539 ;; The point is clobbered if not successful.
7541 ;; LIM is used as bound for backward buffer searches.
7543 ;; This function might do hidden buffer changes.
7545 (let ((beg (point)) end id-start)
7546 (and
7547 (eq (c-beginning-of-statement-1 lim) 'same)
7549 (not (or (c-major-mode-is 'objc-mode)
7550 (c-forward-objc-directive)))
7552 (setq id-start
7553 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7554 (< id-start beg)
7556 ;; There should not be a '=' or ',' between beg and the
7557 ;; start of the declaration since that means we were in the
7558 ;; "expression part" of the declaration.
7559 (or (> (point) beg)
7560 (not (looking-at "[=,]")))
7562 (save-excursion
7563 ;; Check that there's an arglist paren in the
7564 ;; declaration.
7565 (goto-char id-start)
7566 (cond ((eq (char-after) ?\()
7567 ;; The declarator is a paren expression, so skip past it
7568 ;; so that we don't get stuck on that instead of the
7569 ;; function arglist.
7570 (c-forward-sexp))
7571 ((and c-opt-op-identifier-prefix
7572 (looking-at c-opt-op-identifier-prefix))
7573 ;; Don't trip up on "operator ()".
7574 (c-forward-token-2 2 t)))
7575 (and (< (point) beg)
7576 (c-syntactic-re-search-forward "(" beg t t)
7577 (1- (point)))))))
7579 (defun c-in-knr-argdecl (&optional lim)
7580 ;; Return the position of the first argument declaration if point is
7581 ;; inside a K&R style argument declaration list, nil otherwise.
7582 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7583 ;; position that bounds the backward search for the argument list.
7585 ;; Point must be within a possible K&R region, e.g. just before a top-level
7586 ;; "{". It must be outside of parens and brackets. The test can return
7587 ;; false positives otherwise.
7589 ;; This function might do hidden buffer changes.
7591 (save-excursion
7592 (save-restriction
7593 ;; If we're in a macro, our search range is restricted to it. Narrow to
7594 ;; the searchable range.
7595 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
7596 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
7597 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
7598 before-lparen after-rparen
7599 (pp-count-out 20)) ; Max number of paren/brace constructs before
7600 ; we give up
7601 (narrow-to-region low-lim (or macro-end (point-max)))
7603 ;; Search backwards for the defun's argument list. We give up if we
7604 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
7605 ;; a knr region) or BOB.
7607 ;; The criterion for a paren structure being the arg list is:
7608 ;; o - there is non-WS stuff after it but before any "{"; AND
7609 ;; o - the token after it isn't a ";" AND
7610 ;; o - it is preceded by either an identifier (the function name) or
7611 ;; a macro expansion like "DEFUN (...)"; AND
7612 ;; o - its content is a non-empty comma-separated list of identifiers
7613 ;; (an empty arg list won't have a knr region).
7615 ;; The following snippet illustrates these rules:
7616 ;; int foo (bar, baz, yuk)
7617 ;; int bar [] ;
7618 ;; int (*baz) (my_type) ;
7619 ;; int (*) (void) (*yuk) (void) ;
7620 ;; {
7622 (catch 'knr
7623 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7624 (setq pp-count-out (1- pp-count-out))
7625 (c-syntactic-skip-backward "^)]}=")
7626 (cond ((eq (char-before) ?\))
7627 (setq after-rparen (point)))
7628 ((eq (char-before) ?\])
7629 (setq after-rparen nil))
7630 (t ; either } (hit previous defun) or = or no more
7631 ; parens/brackets.
7632 (throw 'knr nil)))
7634 (if after-rparen
7635 ;; We're inside a paren. Could it be our argument list....?
7637 (and
7638 (progn
7639 (goto-char after-rparen)
7640 (unless (c-go-list-backward) (throw 'knr nil)) ;
7641 ;; FIXME!!! What about macros between the parens? 2007/01/20
7642 (setq before-lparen (point)))
7644 ;; It can't be the arg list if next token is ; or {
7645 (progn (goto-char after-rparen)
7646 (c-forward-syntactic-ws)
7647 (not (memq (char-after) '(?\; ?\{ ?\=))))
7649 ;; Is the thing preceding the list an identifier (the
7650 ;; function name), or a macro expansion?
7651 (progn
7652 (goto-char before-lparen)
7653 (eq (c-backward-token-2) 0)
7654 (or (eq (c-on-identifier) (point))
7655 (and (eq (char-after) ?\))
7656 (c-go-up-list-backward)
7657 (eq (c-backward-token-2) 0)
7658 (eq (c-on-identifier) (point)))))
7660 ;; Have we got a non-empty list of comma-separated
7661 ;; identifiers?
7662 (progn
7663 (goto-char before-lparen)
7664 (c-forward-token-2) ; to first token inside parens
7665 (and
7666 (c-on-identifier)
7667 (c-forward-token-2)
7668 (catch 'id-list
7669 (while (eq (char-after) ?\,)
7670 (c-forward-token-2)
7671 (unless (c-on-identifier) (throw 'id-list nil))
7672 (c-forward-token-2))
7673 (eq (char-after) ?\))))))
7675 ;; ...Yes. We've identified the function's argument list.
7676 (throw 'knr
7677 (progn (goto-char after-rparen)
7678 (c-forward-syntactic-ws)
7679 (point)))
7681 ;; ...No. The current parens aren't the function's arg list.
7682 (goto-char before-lparen))
7684 (or (c-go-list-backward) ; backwards over [ .... ]
7685 (throw 'knr nil)))))))))
7687 (defun c-skip-conditional ()
7688 ;; skip forward over conditional at point, including any predicate
7689 ;; statements in parentheses. No error checking is performed.
7691 ;; This function might do hidden buffer changes.
7692 (c-forward-sexp (cond
7693 ;; else if()
7694 ((looking-at (concat "\\<else"
7695 "\\([ \t\n]\\|\\\\\n\\)+"
7696 "if\\>\\([^_]\\|$\\)"))
7698 ;; do, else, try, finally
7699 ((looking-at (concat "\\<\\("
7700 "do\\|else\\|try\\|finally"
7701 "\\)\\>\\([^_]\\|$\\)"))
7703 ;; for, if, while, switch, catch, synchronized, foreach
7704 (t 2))))
7706 (defun c-after-conditional (&optional lim)
7707 ;; If looking at the token after a conditional then return the
7708 ;; position of its start, otherwise return nil.
7710 ;; This function might do hidden buffer changes.
7711 (save-excursion
7712 (and (zerop (c-backward-token-2 1 t lim))
7713 (or (looking-at c-block-stmt-1-key)
7714 (and (eq (char-after) ?\()
7715 (zerop (c-backward-token-2 1 t lim))
7716 (looking-at c-block-stmt-2-key)))
7717 (point))))
7719 (defun c-after-special-operator-id (&optional lim)
7720 ;; If the point is after an operator identifier that isn't handled
7721 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7722 ;; position of the start of that identifier is returned. nil is
7723 ;; returned otherwise. The point may be anywhere in the syntactic
7724 ;; whitespace after the last token of the operator identifier.
7726 ;; This function might do hidden buffer changes.
7727 (save-excursion
7728 (and c-overloadable-operators-regexp
7729 (zerop (c-backward-token-2 1 nil lim))
7730 (looking-at c-overloadable-operators-regexp)
7731 (or (not c-opt-op-identifier-prefix)
7732 (and
7733 (zerop (c-backward-token-2 1 nil lim))
7734 (looking-at c-opt-op-identifier-prefix)))
7735 (point))))
7737 (defsubst c-backward-to-block-anchor (&optional lim)
7738 ;; Assuming point is at a brace that opens a statement block of some
7739 ;; kind, move to the proper anchor point for that block. It might
7740 ;; need to be adjusted further by c-add-stmt-syntax, but the
7741 ;; position at return is suitable as start position for that
7742 ;; function.
7744 ;; This function might do hidden buffer changes.
7745 (unless (= (point) (c-point 'boi))
7746 (let ((start (c-after-conditional lim)))
7747 (if start
7748 (goto-char start)))))
7750 (defsubst c-backward-to-decl-anchor (&optional lim)
7751 ;; Assuming point is at a brace that opens the block of a top level
7752 ;; declaration of some kind, move to the proper anchor point for
7753 ;; that block.
7755 ;; This function might do hidden buffer changes.
7756 (unless (= (point) (c-point 'boi))
7757 (c-beginning-of-statement-1 lim)))
7759 (defun c-search-decl-header-end ()
7760 ;; Search forward for the end of the "header" of the current
7761 ;; declaration. That's the position where the definition body
7762 ;; starts, or the first variable initializer, or the ending
7763 ;; semicolon. I.e. search forward for the closest following
7764 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7765 ;; _after_ the first found token, or at point-max if none is found.
7767 ;; This function might do hidden buffer changes.
7769 (let ((base (point)))
7770 (if (c-major-mode-is 'c++-mode)
7772 ;; In C++ we need to take special care to handle operator
7773 ;; tokens and those pesky template brackets.
7774 (while (and
7775 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
7777 (c-end-of-current-token base)
7778 ;; Handle operator identifiers, i.e. ignore any
7779 ;; operator token preceded by "operator".
7780 (save-excursion
7781 (and (c-safe (c-backward-sexp) t)
7782 (looking-at c-opt-op-identifier-prefix)))
7783 (and (eq (char-before) ?<)
7784 (c-with-syntax-table c++-template-syntax-table
7785 (if (c-safe (goto-char (c-up-list-forward (point))))
7787 (goto-char (point-max))
7788 nil)))))
7789 (setq base (point)))
7791 (while (and
7792 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
7793 (c-end-of-current-token base))
7794 (setq base (point))))))
7796 (defun c-beginning-of-decl-1 (&optional lim)
7797 ;; Go to the beginning of the current declaration, or the beginning
7798 ;; of the previous one if already at the start of it. Point won't
7799 ;; be moved out of any surrounding paren. Return a cons cell of the
7800 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7801 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7802 ;; style argument declarations (and they are to be recognized) then
7803 ;; KNR-POS is set to the start of the first such argument
7804 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7805 ;; position that bounds the backward search.
7807 ;; NB: Cases where the declaration continues after the block, as in
7808 ;; "struct foo { ... } bar;", are currently recognized as two
7809 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7811 ;; This function might do hidden buffer changes.
7812 (catch 'return
7813 (let* ((start (point))
7814 (last-stmt-start (point))
7815 (move (c-beginning-of-statement-1 lim nil t)))
7817 ;; `c-beginning-of-statement-1' stops at a block start, but we
7818 ;; want to continue if the block doesn't begin a top level
7819 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7820 ;; or an open paren.
7821 (let ((beg (point)) tentative-move)
7822 ;; Go back one "statement" each time round the loop until we're just
7823 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7824 ;; an ObjC method. This will move over a multiple declaration whose
7825 ;; components are comma separated.
7826 (while (and
7827 ;; Must check with c-opt-method-key in ObjC mode.
7828 (not (and c-opt-method-key
7829 (looking-at c-opt-method-key)))
7830 (/= last-stmt-start (point))
7831 (progn
7832 (c-backward-syntactic-ws lim)
7833 (not (memq (char-before) '(?\; ?} ?: nil))))
7834 (save-excursion
7835 (backward-char)
7836 (not (looking-at "\\s(")))
7837 ;; Check that we don't move from the first thing in a
7838 ;; macro to its header.
7839 (not (eq (setq tentative-move
7840 (c-beginning-of-statement-1 lim nil t))
7841 'macro)))
7842 (setq last-stmt-start beg
7843 beg (point)
7844 move tentative-move))
7845 (goto-char beg))
7847 (when c-recognize-knr-p
7848 (let ((fallback-pos (point)) knr-argdecl-start)
7849 ;; Handle K&R argdecls. Back up after the "statement" jumped
7850 ;; over by `c-beginning-of-statement-1', unless it was the
7851 ;; function body, in which case we're sitting on the opening
7852 ;; brace now. Then test if we're in a K&R argdecl region and
7853 ;; that we started at the other side of the first argdecl in
7854 ;; it.
7855 (unless (eq (char-after) ?{)
7856 (goto-char last-stmt-start))
7857 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
7858 (< knr-argdecl-start start)
7859 (progn
7860 (goto-char knr-argdecl-start)
7861 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
7862 (throw 'return
7863 (cons (if (eq (char-after fallback-pos) ?{)
7864 'previous
7865 'same)
7866 knr-argdecl-start))
7867 (goto-char fallback-pos))))
7869 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7870 ;; statement, so the result will be 'previous if we've moved over any.
7871 ;; So change our result back to 'same if necessary.
7873 ;; If they were brace list initializers we might not have moved over a
7874 ;; declaration boundary though, so change it to 'same if we've moved
7875 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7876 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7877 ;; potentially can search over a large amount of text.). Take special
7878 ;; pains not to get mislead by C++'s "operator=", and the like.
7879 (if (and (eq move 'previous)
7880 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
7881 c++-template-syntax-table
7882 (syntax-table))
7883 (save-excursion
7884 (and
7885 (progn
7886 (while ; keep going back to "[;={"s until we either find
7887 ; no more, or get to one which isn't an "operator ="
7888 (and (c-syntactic-re-search-forward "[;={]" start t t t)
7889 (eq (char-before) ?=)
7890 c-overloadable-operators-regexp
7891 c-opt-op-identifier-prefix
7892 (save-excursion
7893 (eq (c-backward-token-2) 0)
7894 (looking-at c-overloadable-operators-regexp)
7895 (eq (c-backward-token-2) 0)
7896 (looking-at c-opt-op-identifier-prefix))))
7897 (eq (char-before) ?=))
7898 (c-syntactic-re-search-forward "[;{]" start t t)
7899 (eq (char-before) ?{)
7900 (c-safe (goto-char (c-up-list-forward (point))) t)
7901 (not (c-syntactic-re-search-forward ";" start t t))))))
7902 (cons 'same nil)
7903 (cons move nil)))))
7905 (defun c-end-of-decl-1 ()
7906 ;; Assuming point is at the start of a declaration (as detected by
7907 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7908 ;; `c-beginning-of-decl-1', this function handles the case when a
7909 ;; block is followed by identifiers in e.g. struct declarations in C
7910 ;; or C++. If a proper end was found then t is returned, otherwise
7911 ;; point is moved as far as possible within the current sexp and nil
7912 ;; is returned. This function doesn't handle macros; use
7913 ;; `c-end-of-macro' instead in those cases.
7915 ;; This function might do hidden buffer changes.
7916 (let ((start (point))
7917 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
7918 c++-template-syntax-table
7919 (syntax-table))))
7920 (catch 'return
7921 (c-search-decl-header-end)
7923 (when (and c-recognize-knr-p
7924 (eq (char-before) ?\;)
7925 (c-in-knr-argdecl start))
7926 ;; Stopped at the ';' in a K&R argdecl section which is
7927 ;; detected using the same criteria as in
7928 ;; `c-beginning-of-decl-1'. Move to the following block
7929 ;; start.
7930 (c-syntactic-re-search-forward "{" nil 'move t))
7932 (when (eq (char-before) ?{)
7933 ;; Encountered a block in the declaration. Jump over it.
7934 (condition-case nil
7935 (goto-char (c-up-list-forward (point)))
7936 (error (goto-char (point-max))
7937 (throw 'return nil)))
7938 (if (or (not c-opt-block-decls-with-vars-key)
7939 (save-excursion
7940 (c-with-syntax-table decl-syntax-table
7941 (let ((lim (point)))
7942 (goto-char start)
7943 (not (and
7944 ;; Check for `c-opt-block-decls-with-vars-key'
7945 ;; before the first paren.
7946 (c-syntactic-re-search-forward
7947 (concat "[;=\(\[{]\\|\\("
7948 c-opt-block-decls-with-vars-key
7949 "\\)")
7950 lim t t t)
7951 (match-beginning 1)
7952 (not (eq (char-before) ?_))
7953 ;; Check that the first following paren is
7954 ;; the block.
7955 (c-syntactic-re-search-forward "[;=\(\[{]"
7956 lim t t t)
7957 (eq (char-before) ?{)))))))
7958 ;; The declaration doesn't have any of the
7959 ;; `c-opt-block-decls-with-vars' keywords in the
7960 ;; beginning, so it ends here at the end of the block.
7961 (throw 'return t)))
7963 (c-with-syntax-table decl-syntax-table
7964 (while (progn
7965 (if (eq (char-before) ?\;)
7966 (throw 'return t))
7967 (c-syntactic-re-search-forward ";" nil 'move t))))
7968 nil)))
7970 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
7971 ;; Assuming the point is at an open brace, check if it starts a
7972 ;; block that contains another declaration level, i.e. that isn't a
7973 ;; statement block or a brace list, and if so return non-nil.
7975 ;; If the check is successful, the return value is the start of the
7976 ;; keyword that tells what kind of construct it is, i.e. typically
7977 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7978 ;; the point will be at the start of the construct, before any
7979 ;; leading specifiers, otherwise it's at the returned position.
7981 ;; The point is clobbered if the check is unsuccessful.
7983 ;; CONTAINING-SEXP is the position of the open of the surrounding
7984 ;; paren, or nil if none.
7986 ;; The optional LIMIT limits the backward search for the start of
7987 ;; the construct. It's assumed to be at a syntactically relevant
7988 ;; position.
7990 ;; If any template arglists are found in the searched region before
7991 ;; the open brace, they get marked with paren syntax.
7993 ;; This function might do hidden buffer changes.
7995 (let ((open-brace (point)) kwd-start first-specifier-pos)
7996 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7998 (when (and c-recognize-<>-arglists
7999 (eq (char-before) ?>))
8000 ;; Could be at the end of a template arglist.
8001 (let ((c-parse-and-markup-<>-arglists t)
8002 (c-disallow-comma-in-<>-arglists
8003 (and containing-sexp
8004 (not (eq (char-after containing-sexp) ?{)))))
8005 (while (and
8006 (c-backward-<>-arglist nil limit)
8007 (progn
8008 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8009 (eq (char-before) ?>))))))
8011 ;; Note: Can't get bogus hits inside template arglists below since they
8012 ;; have gotten paren syntax above.
8013 (when (and
8014 ;; If `goto-start' is set we begin by searching for the
8015 ;; first possible position of a leading specifier list.
8016 ;; The `c-decl-block-key' search continues from there since
8017 ;; we know it can't match earlier.
8018 (if goto-start
8019 (when (c-syntactic-re-search-forward c-symbol-start
8020 open-brace t t)
8021 (goto-char (setq first-specifier-pos (match-beginning 0)))
8025 (cond
8026 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
8027 (goto-char (setq kwd-start (match-beginning 0)))
8030 ;; Found a keyword that can't be a type?
8031 (match-beginning 1)
8033 ;; Can be a type too, in which case it's the return type of a
8034 ;; function (under the assumption that no declaration level
8035 ;; block construct starts with a type).
8036 (not (c-forward-type))
8038 ;; Jumped over a type, but it could be a declaration keyword
8039 ;; followed by the declared identifier that we've jumped over
8040 ;; instead (e.g. in "class Foo {"). If it indeed is a type
8041 ;; then we should be at the declarator now, so check for a
8042 ;; valid declarator start.
8044 ;; Note: This doesn't cope with the case when a declared
8045 ;; identifier is followed by e.g. '(' in a language where '('
8046 ;; also might be part of a declarator expression. Currently
8047 ;; there's no such language.
8048 (not (or (looking-at c-symbol-start)
8049 (looking-at c-type-decl-prefix-key)))))
8051 ;; In Pike a list of modifiers may be followed by a brace
8052 ;; to make them apply to many identifiers. Note that the
8053 ;; match data will be empty on return in this case.
8054 ((and (c-major-mode-is 'pike-mode)
8055 (progn
8056 (goto-char open-brace)
8057 (= (c-backward-token-2) 0))
8058 (looking-at c-specifier-key)
8059 ;; Use this variant to avoid yet another special regexp.
8060 (c-keyword-member (c-keyword-sym (match-string 1))
8061 'c-modifier-kwds))
8062 (setq kwd-start (point))
8063 t)))
8065 ;; Got a match.
8067 (if goto-start
8068 ;; Back up over any preceding specifiers and their clauses
8069 ;; by going forward from `first-specifier-pos', which is the
8070 ;; earliest possible position where the specifier list can
8071 ;; start.
8072 (progn
8073 (goto-char first-specifier-pos)
8075 (while (< (point) kwd-start)
8076 (if (looking-at c-symbol-key)
8077 ;; Accept any plain symbol token on the ground that
8078 ;; it's a specifier masked through a macro (just
8079 ;; like `c-forward-decl-or-cast-1' skip forward over
8080 ;; such tokens).
8082 ;; Could be more restrictive wrt invalid keywords,
8083 ;; but that'd only occur in invalid code so there's
8084 ;; no use spending effort on it.
8085 (let ((end (match-end 0)))
8086 (unless (c-forward-keyword-clause 0)
8087 (goto-char end)
8088 (c-forward-syntactic-ws)))
8090 ;; Can't parse a declaration preamble and is still
8091 ;; before `kwd-start'. That means `first-specifier-pos'
8092 ;; was in some earlier construct. Search again.
8093 (if (c-syntactic-re-search-forward c-symbol-start
8094 kwd-start 'move t)
8095 (goto-char (setq first-specifier-pos (match-beginning 0)))
8096 ;; Got no preamble before the block declaration keyword.
8097 (setq first-specifier-pos kwd-start))))
8099 (goto-char first-specifier-pos))
8100 (goto-char kwd-start))
8102 kwd-start)))
8104 (defun c-search-uplist-for-classkey (paren-state)
8105 ;; Check if the closest containing paren sexp is a declaration
8106 ;; block, returning a 2 element vector in that case. Aref 0
8107 ;; contains the bufpos at boi of the class key line, and aref 1
8108 ;; contains the bufpos of the open brace. This function is an
8109 ;; obsolete wrapper for `c-looking-at-decl-block'.
8111 ;; This function might do hidden buffer changes.
8112 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
8113 (when open-paren-pos
8114 (save-excursion
8115 (goto-char open-paren-pos)
8116 (when (and (eq (char-after) ?{)
8117 (c-looking-at-decl-block
8118 (c-safe-position open-paren-pos paren-state)
8119 nil))
8120 (back-to-indentation)
8121 (vector (point) open-paren-pos))))))
8123 (defmacro c-pull-open-brace (ps)
8124 ;; Pull the next open brace from PS (which has the form of paren-state),
8125 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
8126 `(progn
8127 (while (consp (car ,ps))
8128 (setq ,ps (cdr ,ps)))
8129 (prog1 (car ,ps)
8130 (setq ,ps (cdr ,ps)))))
8132 (defun c-most-enclosing-decl-block (paren-state)
8133 ;; Return the buffer position of the most enclosing decl-block brace (in the
8134 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
8135 ;; none was found.
8136 (let* ((open-brace (c-pull-open-brace paren-state))
8137 (next-open-brace (c-pull-open-brace paren-state)))
8138 (while (and open-brace
8139 (save-excursion
8140 (goto-char open-brace)
8141 (not (c-looking-at-decl-block next-open-brace nil))))
8142 (setq open-brace next-open-brace
8143 next-open-brace (c-pull-open-brace paren-state)))
8144 open-brace))
8146 (defun c-cheap-inside-bracelist-p (paren-state)
8147 ;; Return the position of the L-brace if point is inside a brace list
8148 ;; initialization of an array, etc. This is an approximate function,
8149 ;; designed for speed over accuracy. It will not find every bracelist, but
8150 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
8151 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
8152 ;; is everywhere else.
8153 (let (b-pos)
8154 (save-excursion
8155 (while
8156 (and (setq b-pos (c-pull-open-brace paren-state))
8157 (progn (goto-char b-pos)
8158 (c-backward-sws)
8159 (c-backward-token-2)
8160 (not (looking-at "=")))))
8161 b-pos)))
8163 (defun c-inside-bracelist-p (containing-sexp paren-state)
8164 ;; return the buffer position of the beginning of the brace list
8165 ;; statement if we're inside a brace list, otherwise return nil.
8166 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
8167 ;; paren. PAREN-STATE is the remainder of the state of enclosing
8168 ;; braces
8170 ;; N.B.: This algorithm can potentially get confused by cpp macros
8171 ;; placed in inconvenient locations. It's a trade-off we make for
8172 ;; speed.
8174 ;; This function might do hidden buffer changes.
8176 ;; This will pick up brace list declarations.
8177 (c-safe
8178 (save-excursion
8179 (goto-char containing-sexp)
8180 (c-forward-sexp -1)
8181 (let (bracepos)
8182 (if (and (or (looking-at c-brace-list-key)
8183 (progn (c-forward-sexp -1)
8184 (looking-at c-brace-list-key)))
8185 (setq bracepos (c-down-list-forward (point)))
8186 (not (c-crosses-statement-barrier-p (point)
8187 (- bracepos 2))))
8188 (point)))))
8189 ;; this will pick up array/aggregate init lists, even if they are nested.
8190 (save-excursion
8191 (let ((class-key
8192 ;; Pike can have class definitions anywhere, so we must
8193 ;; check for the class key here.
8194 (and (c-major-mode-is 'pike-mode)
8195 c-decl-block-key))
8196 bufpos braceassignp lim next-containing)
8197 (while (and (not bufpos)
8198 containing-sexp)
8199 (when paren-state
8200 (if (consp (car paren-state))
8201 (setq lim (cdr (car paren-state))
8202 paren-state (cdr paren-state))
8203 (setq lim (car paren-state)))
8204 (when paren-state
8205 (setq next-containing (car paren-state)
8206 paren-state (cdr paren-state))))
8207 (goto-char containing-sexp)
8208 (if (c-looking-at-inexpr-block next-containing next-containing)
8209 ;; We're in an in-expression block of some kind. Do not
8210 ;; check nesting. We deliberately set the limit to the
8211 ;; containing sexp, so that c-looking-at-inexpr-block
8212 ;; doesn't check for an identifier before it.
8213 (setq containing-sexp nil)
8214 ;; see if the open brace is preceded by = or [...] in
8215 ;; this statement, but watch out for operator=
8216 (setq braceassignp 'dontknow)
8217 (c-backward-token-2 1 t lim)
8218 ;; Checks to do only on the first sexp before the brace.
8219 (when (and c-opt-inexpr-brace-list-key
8220 (eq (char-after) ?\[))
8221 ;; In Java, an initialization brace list may follow
8222 ;; directly after "new Foo[]", so check for a "new"
8223 ;; earlier.
8224 (while (eq braceassignp 'dontknow)
8225 (setq braceassignp
8226 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8227 ((looking-at c-opt-inexpr-brace-list-key) t)
8228 ((looking-at "\\sw\\|\\s_\\|[.[]")
8229 ;; Carry on looking if this is an
8230 ;; identifier (may contain "." in Java)
8231 ;; or another "[]" sexp.
8232 'dontknow)
8233 (t nil)))))
8234 ;; Checks to do on all sexps before the brace, up to the
8235 ;; beginning of the statement.
8236 (while (eq braceassignp 'dontknow)
8237 (cond ((eq (char-after) ?\;)
8238 (setq braceassignp nil))
8239 ((and class-key
8240 (looking-at class-key))
8241 (setq braceassignp nil))
8242 ((eq (char-after) ?=)
8243 ;; We've seen a =, but must check earlier tokens so
8244 ;; that it isn't something that should be ignored.
8245 (setq braceassignp 'maybe)
8246 (while (and (eq braceassignp 'maybe)
8247 (zerop (c-backward-token-2 1 t lim)))
8248 (setq braceassignp
8249 (cond
8250 ;; Check for operator =
8251 ((and c-opt-op-identifier-prefix
8252 (looking-at c-opt-op-identifier-prefix))
8253 nil)
8254 ;; Check for `<opchar>= in Pike.
8255 ((and (c-major-mode-is 'pike-mode)
8256 (or (eq (char-after) ?`)
8257 ;; Special case for Pikes
8258 ;; `[]=, since '[' is not in
8259 ;; the punctuation class.
8260 (and (eq (char-after) ?\[)
8261 (eq (char-before) ?`))))
8262 nil)
8263 ((looking-at "\\s.") 'maybe)
8264 ;; make sure we're not in a C++ template
8265 ;; argument assignment
8266 ((and
8267 (c-major-mode-is 'c++-mode)
8268 (save-excursion
8269 (let ((here (point))
8270 (pos< (progn
8271 (skip-chars-backward "^<>")
8272 (point))))
8273 (and (eq (char-before) ?<)
8274 (not (c-crosses-statement-barrier-p
8275 pos< here))
8276 (not (c-in-literal))
8277 ))))
8278 nil)
8279 (t t))))))
8280 (if (and (eq braceassignp 'dontknow)
8281 (/= (c-backward-token-2 1 t lim) 0))
8282 (setq braceassignp nil)))
8283 (if (not braceassignp)
8284 (if (eq (char-after) ?\;)
8285 ;; Brace lists can't contain a semicolon, so we're done.
8286 (setq containing-sexp nil)
8287 ;; Go up one level.
8288 (setq containing-sexp next-containing
8289 lim nil
8290 next-containing nil))
8291 ;; we've hit the beginning of the aggregate list
8292 (c-beginning-of-statement-1
8293 (c-most-enclosing-brace paren-state))
8294 (setq bufpos (point))))
8296 bufpos))
8299 (defun c-looking-at-special-brace-list (&optional lim)
8300 ;; If we're looking at the start of a pike-style list, ie `({ })',
8301 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
8302 ;; positions and its entry in c-special-brace-lists is returned, nil
8303 ;; otherwise. The ending position is nil if the list is still open.
8304 ;; LIM is the limit for forward search. The point may either be at
8305 ;; the `(' or at the following paren character. Tries to check the
8306 ;; matching closer, but assumes it's correct if no balanced paren is
8307 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8308 ;; a special brace list).
8310 ;; This function might do hidden buffer changes.
8311 (if c-special-brace-lists
8312 (condition-case ()
8313 (save-excursion
8314 (let ((beg (point))
8315 inner-beg end type)
8316 (c-forward-syntactic-ws)
8317 (if (eq (char-after) ?\()
8318 (progn
8319 (forward-char 1)
8320 (c-forward-syntactic-ws)
8321 (setq inner-beg (point))
8322 (setq type (assq (char-after) c-special-brace-lists)))
8323 (if (setq type (assq (char-after) c-special-brace-lists))
8324 (progn
8325 (setq inner-beg (point))
8326 (c-backward-syntactic-ws)
8327 (forward-char -1)
8328 (setq beg (if (eq (char-after) ?\()
8329 (point)
8330 nil)))))
8331 (if (and beg type)
8332 (if (and (c-safe
8333 (goto-char beg)
8334 (c-forward-sexp 1)
8335 (setq end (point))
8336 (= (char-before) ?\)))
8337 (c-safe
8338 (goto-char inner-beg)
8339 (if (looking-at "\\s(")
8340 ;; Check balancing of the inner paren
8341 ;; below.
8342 (progn
8343 (c-forward-sexp 1)
8345 ;; If the inner char isn't a paren then
8346 ;; we can't check balancing, so just
8347 ;; check the char before the outer
8348 ;; closing paren.
8349 (goto-char end)
8350 (backward-char)
8351 (c-backward-syntactic-ws)
8352 (= (char-before) (cdr type)))))
8353 (if (or (/= (char-syntax (char-before)) ?\))
8354 (= (progn
8355 (c-forward-syntactic-ws)
8356 (point))
8357 (1- end)))
8358 (cons (cons beg end) type))
8359 (cons (list beg) type)))))
8360 (error nil))))
8362 (defun c-looking-at-bos (&optional lim)
8363 ;; Return non-nil if between two statements or declarations, assuming
8364 ;; point is not inside a literal or comment.
8366 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8367 ;; are recommended instead.
8369 ;; This function might do hidden buffer changes.
8370 (c-at-statement-start-p))
8371 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8373 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8374 ;; Return non-nil if we're looking at the beginning of a block
8375 ;; inside an expression. The value returned is actually a cons of
8376 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8377 ;; position of the beginning of the construct.
8379 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8380 ;; position of the closest containing list. If it's nil, the
8381 ;; containing paren isn't used to decide whether we're inside an
8382 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8383 ;; needs to be farther back.
8385 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8386 ;; brace block might be done. It should only be used when the
8387 ;; construct can be assumed to be complete, i.e. when the original
8388 ;; starting position was further down than that.
8390 ;; This function might do hidden buffer changes.
8392 (save-excursion
8393 (let ((res 'maybe) passed-paren
8394 (closest-lim (or containing-sexp lim (point-min)))
8395 ;; Look at the character after point only as a last resort
8396 ;; when we can't disambiguate.
8397 (block-follows (and (eq (char-after) ?{) (point))))
8399 (while (and (eq res 'maybe)
8400 (progn (c-backward-syntactic-ws)
8401 (> (point) closest-lim))
8402 (not (bobp))
8403 (progn (backward-char)
8404 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8405 (c-safe (forward-char)
8406 (goto-char (scan-sexps (point) -1))))
8408 (setq res
8409 (if (looking-at c-keywords-regexp)
8410 (let ((kw-sym (c-keyword-sym (match-string 1))))
8411 (cond
8412 ((and block-follows
8413 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8414 (and (not (eq passed-paren ?\[))
8415 (or (not (looking-at c-class-key))
8416 ;; If the class definition is at the start of
8417 ;; a statement, we don't consider it an
8418 ;; in-expression class.
8419 (let ((prev (point)))
8420 (while (and
8421 (= (c-backward-token-2 1 nil closest-lim) 0)
8422 (eq (char-syntax (char-after)) ?w))
8423 (setq prev (point)))
8424 (goto-char prev)
8425 (not (c-at-statement-start-p)))
8426 ;; Also, in Pike we treat it as an
8427 ;; in-expression class if it's used in an
8428 ;; object clone expression.
8429 (save-excursion
8430 (and check-at-end
8431 (c-major-mode-is 'pike-mode)
8432 (progn (goto-char block-follows)
8433 (zerop (c-forward-token-2 1 t)))
8434 (eq (char-after) ?\())))
8435 (cons 'inexpr-class (point))))
8436 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8437 (when (not passed-paren)
8438 (cons 'inexpr-statement (point))))
8439 ((c-keyword-member kw-sym 'c-lambda-kwds)
8440 (when (or (not passed-paren)
8441 (eq passed-paren ?\())
8442 (cons 'inlambda (point))))
8443 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8444 nil)
8446 'maybe)))
8448 (if (looking-at "\\s(")
8449 (if passed-paren
8450 (if (and (eq passed-paren ?\[)
8451 (eq (char-after) ?\[))
8452 ;; Accept several square bracket sexps for
8453 ;; Java array initializations.
8454 'maybe)
8455 (setq passed-paren (char-after))
8456 'maybe)
8457 'maybe))))
8459 (if (eq res 'maybe)
8460 (when (and c-recognize-paren-inexpr-blocks
8461 block-follows
8462 containing-sexp
8463 (eq (char-after containing-sexp) ?\())
8464 (goto-char containing-sexp)
8465 (if (or (save-excursion
8466 (c-backward-syntactic-ws lim)
8467 (and (> (point) (or lim (point-min)))
8468 (c-on-identifier)))
8469 (and c-special-brace-lists
8470 (c-looking-at-special-brace-list)))
8472 (cons 'inexpr-statement (point))))
8474 res))))
8476 (defun c-looking-at-inexpr-block-backward (paren-state)
8477 ;; Returns non-nil if we're looking at the end of an in-expression
8478 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8479 ;; PAREN-STATE is the paren state relevant at the current position.
8481 ;; This function might do hidden buffer changes.
8482 (save-excursion
8483 ;; We currently only recognize a block.
8484 (let ((here (point))
8485 (elem (car-safe paren-state))
8486 containing-sexp)
8487 (when (and (consp elem)
8488 (progn (goto-char (cdr elem))
8489 (c-forward-syntactic-ws here)
8490 (= (point) here)))
8491 (goto-char (car elem))
8492 (if (setq paren-state (cdr paren-state))
8493 (setq containing-sexp (car-safe paren-state)))
8494 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8495 paren-state)
8496 containing-sexp)))))
8498 (defun c-at-macro-vsemi-p (&optional pos)
8499 ;; Is there a "virtual semicolon" at POS or point?
8500 ;; (See cc-defs.el for full details of "virtual semicolons".)
8502 ;; This is true when point is at the last non syntactic WS position on the
8503 ;; line, there is a macro call last on the line, and this particular macro's
8504 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
8505 ;; semicolon.
8506 (save-excursion
8507 (save-restriction
8508 (widen)
8509 (if pos
8510 (goto-char pos)
8511 (setq pos (point)))
8512 (and
8513 c-macro-with-semi-re
8514 (not (c-in-literal))
8515 (eq (skip-chars-backward " \t") 0)
8517 ;; Check we've got nothing after this except comments and empty lines
8518 ;; joined by escaped EOLs.
8519 (skip-chars-forward " \t") ; always returns non-nil.
8520 (progn
8521 (while ; go over 1 block comment per iteration.
8522 (and
8523 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
8524 (goto-char (match-end 0))
8525 (cond
8526 ((looking-at c-block-comment-start-regexp)
8527 (and (forward-comment 1)
8528 (skip-chars-forward " \t"))) ; always returns non-nil
8529 ((looking-at c-line-comment-start-regexp)
8530 (end-of-line)
8531 nil)
8532 (t nil))))
8533 (eolp))
8535 (goto-char pos)
8536 (progn (c-backward-syntactic-ws)
8537 (eq (point) pos))
8539 ;; Check for one of the listed macros being before point.
8540 (or (not (eq (char-before) ?\)))
8541 (when (c-go-list-backward)
8542 (c-backward-syntactic-ws)
8544 (c-simple-skip-symbol-backward)
8545 (looking-at c-macro-with-semi-re)))))
8547 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
8550 ;; `c-guess-basic-syntax' and the functions that precedes it below
8551 ;; implements the main decision tree for determining the syntactic
8552 ;; analysis of the current line of code.
8554 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8555 ;; auto newline analysis.
8556 (defvar c-auto-newline-analysis nil)
8558 (defun c-brace-anchor-point (bracepos)
8559 ;; BRACEPOS is the position of a brace in a construct like "namespace
8560 ;; Bar {". Return the anchor point in this construct; this is the
8561 ;; earliest symbol on the brace's line which isn't earlier than
8562 ;; "namespace".
8564 ;; Currently (2007-08-17), "like namespace" means "matches
8565 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8566 ;; or anything like that.
8567 (save-excursion
8568 (let ((boi (c-point 'boi bracepos)))
8569 (goto-char bracepos)
8570 (while (and (> (point) boi)
8571 (not (looking-at c-other-decl-block-key)))
8572 (c-backward-token-2))
8573 (if (> (point) boi) (point) boi))))
8575 (defsubst c-add-syntax (symbol &rest args)
8576 ;; A simple function to prepend a new syntax element to
8577 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8578 ;; should always be dynamically bound but since we read it first
8579 ;; we'll fail properly anyway if this function is misused.
8580 (setq c-syntactic-context (cons (cons symbol args)
8581 c-syntactic-context)))
8583 (defsubst c-append-syntax (symbol &rest args)
8584 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8585 ;; (Normally not necessary.)
8586 (setq c-syntactic-context (nconc c-syntactic-context
8587 (list (cons symbol args)))))
8589 (defun c-add-stmt-syntax (syntax-symbol
8590 syntax-extra-args
8591 stop-at-boi-only
8592 containing-sexp
8593 paren-state)
8594 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8595 ;; needed with further syntax elements of the types `substatement',
8596 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8597 ;; `defun-block-intro'.
8599 ;; Do the generic processing to anchor the given syntax symbol on
8600 ;; the preceding statement: Skip over any labels and containing
8601 ;; statements on the same line, and then search backward until we
8602 ;; find a statement or block start that begins at boi without a
8603 ;; label or comment.
8605 ;; Point is assumed to be at the prospective anchor point for the
8606 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8607 ;; skip past open parens and containing statements. Most of the added
8608 ;; syntax elements will get the same anchor point - the exception is
8609 ;; for an anchor in a construct like "namespace"[*] - this is as early
8610 ;; as possible in the construct but on the same line as the {.
8612 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8614 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8615 ;; syntax symbol. They are appended after the anchor point.
8617 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8618 ;; if the current statement starts there.
8620 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8621 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8623 ;; This function might do hidden buffer changes.
8625 (if (= (point) (c-point 'boi))
8626 ;; This is by far the most common case, so let's give it special
8627 ;; treatment.
8628 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
8630 (let ((syntax-last c-syntactic-context)
8631 (boi (c-point 'boi))
8632 ;; Set when we're on a label, so that we don't stop there.
8633 ;; FIXME: To be complete we should check if we're on a label
8634 ;; now at the start.
8635 on-label)
8637 ;; Use point as the anchor point for "namespace", "extern", etc.
8638 (apply 'c-add-syntax syntax-symbol
8639 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
8640 (point) nil)
8641 syntax-extra-args)
8643 ;; Loop while we have to back out of containing blocks.
8644 (while
8645 (and
8646 (catch 'back-up-block
8648 ;; Loop while we have to back up statements.
8649 (while (or (/= (point) boi)
8650 on-label
8651 (looking-at c-comment-start-regexp))
8653 ;; Skip past any comments that stands between the
8654 ;; statement start and boi.
8655 (let ((savepos (point)))
8656 (while (and (/= savepos boi)
8657 (c-backward-single-comment))
8658 (setq savepos (point)
8659 boi (c-point 'boi)))
8660 (goto-char savepos))
8662 ;; Skip to the beginning of this statement or backward
8663 ;; another one.
8664 (let ((old-pos (point))
8665 (old-boi boi)
8666 (step-type (c-beginning-of-statement-1 containing-sexp)))
8667 (setq boi (c-point 'boi)
8668 on-label (eq step-type 'label))
8670 (cond ((= (point) old-pos)
8671 ;; If we didn't move we're at the start of a block and
8672 ;; have to continue outside it.
8673 (throw 'back-up-block t))
8675 ((and (eq step-type 'up)
8676 (>= (point) old-boi)
8677 (looking-at "else\\>[^_]")
8678 (save-excursion
8679 (goto-char old-pos)
8680 (looking-at "if\\>[^_]")))
8681 ;; Special case to avoid deeper and deeper indentation
8682 ;; of "else if" clauses.
8685 ((and (not stop-at-boi-only)
8686 (/= old-pos old-boi)
8687 (memq step-type '(up previous)))
8688 ;; If stop-at-boi-only is nil, we shouldn't back up
8689 ;; over previous or containing statements to try to
8690 ;; reach boi, so go back to the last position and
8691 ;; exit.
8692 (goto-char old-pos)
8693 (throw 'back-up-block nil))
8696 (if (and (not stop-at-boi-only)
8697 (memq step-type '(up previous beginning)))
8698 ;; If we've moved into another statement then we
8699 ;; should no longer try to stop in the middle of a
8700 ;; line.
8701 (setq stop-at-boi-only t))
8703 ;; Record this as a substatement if we skipped up one
8704 ;; level.
8705 (when (eq step-type 'up)
8706 (c-add-syntax 'substatement nil))))
8709 containing-sexp)
8711 ;; Now we have to go out of this block.
8712 (goto-char containing-sexp)
8714 ;; Don't stop in the middle of a special brace list opener
8715 ;; like "({".
8716 (when c-special-brace-lists
8717 (let ((special-list (c-looking-at-special-brace-list)))
8718 (when (and special-list
8719 (< (car (car special-list)) (point)))
8720 (setq containing-sexp (car (car special-list)))
8721 (goto-char containing-sexp))))
8723 (setq paren-state (c-whack-state-after containing-sexp paren-state)
8724 containing-sexp (c-most-enclosing-brace paren-state)
8725 boi (c-point 'boi))
8727 ;; Analyze the construct in front of the block we've stepped out
8728 ;; from and add the right syntactic element for it.
8729 (let ((paren-pos (point))
8730 (paren-char (char-after))
8731 step-type)
8733 (if (eq paren-char ?\()
8734 ;; Stepped out of a parenthesis block, so we're in an
8735 ;; expression now.
8736 (progn
8737 (when (/= paren-pos boi)
8738 (if (and c-recognize-paren-inexpr-blocks
8739 (progn
8740 (c-backward-syntactic-ws containing-sexp)
8741 (or (not (looking-at "\\>"))
8742 (not (c-on-identifier))))
8743 (save-excursion
8744 (goto-char (1+ paren-pos))
8745 (c-forward-syntactic-ws)
8746 (eq (char-after) ?{)))
8747 ;; Stepped out of an in-expression statement. This
8748 ;; syntactic element won't get an anchor pos.
8749 (c-add-syntax 'inexpr-statement)
8751 ;; A parenthesis normally belongs to an arglist.
8752 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
8754 (goto-char (max boi
8755 (if containing-sexp
8756 (1+ containing-sexp)
8757 (point-min))))
8758 (setq step-type 'same
8759 on-label nil))
8761 ;; Stepped out of a brace block.
8762 (setq step-type (c-beginning-of-statement-1 containing-sexp)
8763 on-label (eq step-type 'label))
8765 (if (and (eq step-type 'same)
8766 (/= paren-pos (point)))
8767 (let (inexpr)
8768 (cond
8769 ((save-excursion
8770 (goto-char paren-pos)
8771 (setq inexpr (c-looking-at-inexpr-block
8772 (c-safe-position containing-sexp paren-state)
8773 containing-sexp)))
8774 (c-add-syntax (if (eq (car inexpr) 'inlambda)
8775 'defun-block-intro
8776 'statement-block-intro)
8777 nil))
8778 ((looking-at c-other-decl-block-key)
8779 (c-add-syntax
8780 (cdr (assoc (match-string 1)
8781 c-other-decl-block-key-in-symbols-alist))
8782 (max (c-point 'boi paren-pos) (point))))
8783 (t (c-add-syntax 'defun-block-intro nil))))
8785 (c-add-syntax 'statement-block-intro nil)))
8787 (if (= paren-pos boi)
8788 ;; Always done if the open brace was at boi. The
8789 ;; c-beginning-of-statement-1 call above is necessary
8790 ;; anyway, to decide the type of block-intro to add.
8791 (goto-char paren-pos)
8792 (setq boi (c-point 'boi)))
8795 ;; Fill in the current point as the anchor for all the symbols
8796 ;; added above.
8797 (let ((p c-syntactic-context) q)
8798 (while (not (eq p syntax-last))
8799 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8800 (while q
8801 (unless (car q)
8802 (setcar q (point)))
8803 (setq q (cdr q)))
8804 (setq p (cdr p))))
8807 (defun c-add-class-syntax (symbol
8808 containing-decl-open
8809 containing-decl-start
8810 containing-decl-kwd
8811 paren-state)
8812 ;; The inclass and class-close syntactic symbols are added in
8813 ;; several places and some work is needed to fix everything.
8814 ;; Therefore it's collected here.
8816 ;; This function might do hidden buffer changes.
8817 (goto-char containing-decl-open)
8818 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
8819 (progn
8820 (c-add-syntax symbol containing-decl-open)
8821 containing-decl-open)
8822 (goto-char containing-decl-start)
8823 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8824 ;; here, but we have to do like this for compatibility.
8825 (back-to-indentation)
8826 (c-add-syntax symbol (point))
8827 (if (and (c-keyword-member containing-decl-kwd
8828 'c-inexpr-class-kwds)
8829 (/= containing-decl-start (c-point 'boi containing-decl-start)))
8830 (c-add-syntax 'inexpr-class))
8831 (point)))
8833 (defun c-guess-continued-construct (indent-point
8834 char-after-ip
8835 beg-of-same-or-containing-stmt
8836 containing-sexp
8837 paren-state)
8838 ;; This function contains the decision tree reached through both
8839 ;; cases 18 and 10. It's a continued statement or top level
8840 ;; construct of some kind.
8842 ;; This function might do hidden buffer changes.
8844 (let (special-brace-list placeholder)
8845 (goto-char indent-point)
8846 (skip-chars-forward " \t")
8848 (cond
8849 ;; (CASE A removed.)
8850 ;; CASE B: open braces for class or brace-lists
8851 ((setq special-brace-list
8852 (or (and c-special-brace-lists
8853 (c-looking-at-special-brace-list))
8854 (eq char-after-ip ?{)))
8856 (cond
8857 ;; CASE B.1: class-open
8858 ((save-excursion
8859 (and (eq (char-after) ?{)
8860 (c-looking-at-decl-block containing-sexp t)
8861 (setq beg-of-same-or-containing-stmt (point))))
8862 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
8864 ;; CASE B.2: brace-list-open
8865 ((or (consp special-brace-list)
8866 (save-excursion
8867 (goto-char beg-of-same-or-containing-stmt)
8868 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8869 indent-point t t t)))
8870 ;; The most semantically accurate symbol here is
8871 ;; brace-list-open, but we normally report it simply as a
8872 ;; statement-cont. The reason is that one normally adjusts
8873 ;; brace-list-open for brace lists as top-level constructs,
8874 ;; and brace lists inside statements is a completely different
8875 ;; context. C.f. case 5A.3.
8876 (c-beginning-of-statement-1 containing-sexp)
8877 (c-add-stmt-syntax (if c-auto-newline-analysis
8878 ;; Turn off the dwim above when we're
8879 ;; analyzing the nature of the brace
8880 ;; for the auto newline feature.
8881 'brace-list-open
8882 'statement-cont)
8883 nil nil
8884 containing-sexp paren-state))
8886 ;; CASE B.3: The body of a function declared inside a normal
8887 ;; block. Can occur e.g. in Pike and when using gcc
8888 ;; extensions, but watch out for macros followed by blocks.
8889 ;; C.f. cases E, 16F and 17G.
8890 ((and (not (c-at-statement-start-p))
8891 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8892 'same)
8893 (save-excursion
8894 (let ((c-recognize-typeless-decls nil))
8895 ;; Turn off recognition of constructs that lacks a
8896 ;; type in this case, since that's more likely to be
8897 ;; a macro followed by a block.
8898 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8899 (c-add-stmt-syntax 'defun-open nil t
8900 containing-sexp paren-state))
8902 ;; CASE B.4: Continued statement with block open. The most
8903 ;; accurate analysis is perhaps `statement-cont' together with
8904 ;; `block-open' but we play DWIM and use `substatement-open'
8905 ;; instead. The rationale is that this typically is a macro
8906 ;; followed by a block which makes it very similar to a
8907 ;; statement with a substatement block.
8909 (c-add-stmt-syntax 'substatement-open nil nil
8910 containing-sexp paren-state))
8913 ;; CASE C: iostream insertion or extraction operator
8914 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8915 (save-excursion
8916 (goto-char beg-of-same-or-containing-stmt)
8917 ;; If there is no preceding streamop in the statement
8918 ;; then indent this line as a normal statement-cont.
8919 (when (c-syntactic-re-search-forward
8920 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
8921 (c-add-syntax 'stream-op (c-point 'boi))
8922 t))))
8924 ;; CASE E: In the "K&R region" of a function declared inside a
8925 ;; normal block. C.f. case B.3.
8926 ((and (save-excursion
8927 ;; Check that the next token is a '{'. This works as
8928 ;; long as no language that allows nested function
8929 ;; definitions allows stuff like member init lists, K&R
8930 ;; declarations or throws clauses there.
8932 ;; Note that we do a forward search for something ahead
8933 ;; of the indentation line here. That's not good since
8934 ;; the user might not have typed it yet. Unfortunately
8935 ;; it's exceedingly tricky to recognize a function
8936 ;; prototype in a code block without resorting to this.
8937 (c-forward-syntactic-ws)
8938 (eq (char-after) ?{))
8939 (not (c-at-statement-start-p))
8940 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8941 'same)
8942 (save-excursion
8943 (let ((c-recognize-typeless-decls nil))
8944 ;; Turn off recognition of constructs that lacks a
8945 ;; type in this case, since that's more likely to be
8946 ;; a macro followed by a block.
8947 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8948 (c-add-stmt-syntax 'func-decl-cont nil t
8949 containing-sexp paren-state))
8951 ;;CASE F: continued statement and the only preceding items are
8952 ;;annotations.
8953 ((and (c-major-mode-is 'java-mode)
8954 (setq placeholder (point))
8955 (c-beginning-of-statement-1)
8956 (progn
8957 (while (and (c-forward-annotation)
8958 (< (point) placeholder))
8959 (c-forward-syntactic-ws))
8961 (prog1
8962 (>= (point) placeholder)
8963 (goto-char placeholder)))
8964 (c-beginning-of-statement-1 containing-sexp)
8965 (c-add-syntax 'annotation-var-cont (point)))
8967 ;; CASE G: a template list continuation?
8968 ;; Mostly a duplication of case 5D.3 to fix templates-19:
8969 ((and (c-major-mode-is 'c++-mode)
8970 (save-excursion
8971 (goto-char indent-point)
8972 (c-with-syntax-table c++-template-syntax-table
8973 (setq placeholder (c-up-list-backward)))
8974 (and placeholder
8975 (eq (char-after placeholder) ?<)
8976 (/= (char-before placeholder) ?<)
8977 (progn
8978 (goto-char (1+ placeholder))
8979 (not (looking-at c-<-op-cont-regexp))))))
8980 (c-with-syntax-table c++-template-syntax-table
8981 (goto-char placeholder)
8982 (c-beginning-of-statement-1 containing-sexp t)
8983 (if (save-excursion
8984 (c-backward-syntactic-ws containing-sexp)
8985 (eq (char-before) ?<))
8986 ;; In a nested template arglist.
8987 (progn
8988 (goto-char placeholder)
8989 (c-syntactic-skip-backward "^,;" containing-sexp t)
8990 (c-forward-syntactic-ws))
8991 (back-to-indentation)))
8992 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8993 ;; template aware.
8994 (c-add-syntax 'template-args-cont (point) placeholder))
8996 ;; CASE D: continued statement.
8998 (c-beginning-of-statement-1 containing-sexp)
8999 (c-add-stmt-syntax 'statement-cont nil nil
9000 containing-sexp paren-state))
9003 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
9004 ;; 2005/11/29).
9005 ;;;###autoload
9006 (defun c-guess-basic-syntax ()
9007 "Return the syntactic context of the current line."
9008 (save-excursion
9009 (beginning-of-line)
9010 (c-save-buffer-state
9011 ((indent-point (point))
9012 (case-fold-search nil)
9013 ;; A whole ugly bunch of various temporary variables. Have
9014 ;; to declare them here since it's not possible to declare
9015 ;; a variable with only the scope of a cond test and the
9016 ;; following result clauses, and most of this function is a
9017 ;; single gigantic cond. :P
9018 literal char-before-ip before-ws-ip char-after-ip macro-start
9019 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
9020 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
9021 containing-<
9022 ;; The following record some positions for the containing
9023 ;; declaration block if we're directly within one:
9024 ;; `containing-decl-open' is the position of the open
9025 ;; brace. `containing-decl-start' is the start of the
9026 ;; declaration. `containing-decl-kwd' is the keyword
9027 ;; symbol of the keyword that tells what kind of block it
9028 ;; is.
9029 containing-decl-open
9030 containing-decl-start
9031 containing-decl-kwd
9032 ;; The open paren of the closest surrounding sexp or nil if
9033 ;; there is none.
9034 containing-sexp
9035 ;; The position after the closest preceding brace sexp
9036 ;; (nested sexps are ignored), or the position after
9037 ;; `containing-sexp' if there is none, or (point-min) if
9038 ;; `containing-sexp' is nil.
9040 ;; The paren state outside `containing-sexp', or at
9041 ;; `indent-point' if `containing-sexp' is nil.
9042 (paren-state (c-parse-state))
9043 ;; There's always at most one syntactic element which got
9044 ;; an anchor pos. It's stored in syntactic-relpos.
9045 syntactic-relpos
9046 (c-stmt-delim-chars c-stmt-delim-chars))
9048 ;; Check if we're directly inside an enclosing declaration
9049 ;; level block.
9050 (when (and (setq containing-sexp
9051 (c-most-enclosing-brace paren-state))
9052 (progn
9053 (goto-char containing-sexp)
9054 (eq (char-after) ?{))
9055 (setq placeholder
9056 (c-looking-at-decl-block
9057 (c-most-enclosing-brace paren-state
9058 containing-sexp)
9059 t)))
9060 (setq containing-decl-open containing-sexp
9061 containing-decl-start (point)
9062 containing-sexp nil)
9063 (goto-char placeholder)
9064 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
9065 (c-keyword-sym (match-string 1)))))
9067 ;; Init some position variables.
9068 (if c-state-cache
9069 (progn
9070 (setq containing-sexp (car paren-state)
9071 paren-state (cdr paren-state))
9072 (if (consp containing-sexp)
9073 (progn
9074 (setq lim (cdr containing-sexp))
9075 (if (cdr c-state-cache)
9076 ;; Ignore balanced paren. The next entry
9077 ;; can't be another one.
9078 (setq containing-sexp (car (cdr c-state-cache))
9079 paren-state (cdr paren-state))
9080 ;; If there is no surrounding open paren then
9081 ;; put the last balanced pair back on paren-state.
9082 (setq paren-state (cons containing-sexp paren-state)
9083 containing-sexp nil)))
9084 (setq lim (1+ containing-sexp))))
9085 (setq lim (point-min)))
9087 ;; If we're in a parenthesis list then ',' delimits the
9088 ;; "statements" rather than being an operator (with the
9089 ;; exception of the "for" clause). This difference is
9090 ;; typically only noticeable when statements are used in macro
9091 ;; arglists.
9092 (when (and containing-sexp
9093 (eq (char-after containing-sexp) ?\())
9094 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
9095 ;; cache char before and after indent point, and move point to
9096 ;; the most likely position to perform the majority of tests
9097 (goto-char indent-point)
9098 (c-backward-syntactic-ws lim)
9099 (setq before-ws-ip (point)
9100 char-before-ip (char-before))
9101 (goto-char indent-point)
9102 (skip-chars-forward " \t")
9103 (setq char-after-ip (char-after))
9105 ;; are we in a literal?
9106 (setq literal (c-in-literal lim))
9108 ;; now figure out syntactic qualities of the current line
9109 (cond
9111 ;; CASE 1: in a string.
9112 ((eq literal 'string)
9113 (c-add-syntax 'string (c-point 'bopl)))
9115 ;; CASE 2: in a C or C++ style comment.
9116 ((and (memq literal '(c c++))
9117 ;; This is a kludge for XEmacs where we use
9118 ;; `buffer-syntactic-context', which doesn't correctly
9119 ;; recognize "\*/" to end a block comment.
9120 ;; `parse-partial-sexp' which is used by
9121 ;; `c-literal-limits' will however do that in most
9122 ;; versions, which results in that we get nil from
9123 ;; `c-literal-limits' even when `c-in-literal' claims
9124 ;; we're inside a comment.
9125 (setq placeholder (c-literal-limits lim)))
9126 (c-add-syntax literal (car placeholder)))
9128 ;; CASE 3: in a cpp preprocessor macro continuation.
9129 ((and (save-excursion
9130 (when (c-beginning-of-macro)
9131 (setq macro-start (point))))
9132 (/= macro-start (c-point 'boi))
9133 (progn
9134 (setq tmpsymbol 'cpp-macro-cont)
9135 (or (not c-syntactic-indentation-in-macros)
9136 (save-excursion
9137 (goto-char macro-start)
9138 ;; If at the beginning of the body of a #define
9139 ;; directive then analyze as cpp-define-intro
9140 ;; only. Go on with the syntactic analysis
9141 ;; otherwise. in-macro-expr is set if we're in a
9142 ;; cpp expression, i.e. before the #define body
9143 ;; or anywhere in a non-#define directive.
9144 (if (c-forward-to-cpp-define-body)
9145 (let ((indent-boi (c-point 'boi indent-point)))
9146 (setq in-macro-expr (> (point) indent-boi)
9147 tmpsymbol 'cpp-define-intro)
9148 (= (point) indent-boi))
9149 (setq in-macro-expr t)
9150 nil)))))
9151 (c-add-syntax tmpsymbol macro-start)
9152 (setq macro-start nil))
9154 ;; CASE 11: an else clause?
9155 ((looking-at "else\\>[^_]")
9156 (c-beginning-of-statement-1 containing-sexp)
9157 (c-add-stmt-syntax 'else-clause nil t
9158 containing-sexp paren-state))
9160 ;; CASE 12: while closure of a do/while construct?
9161 ((and (looking-at "while\\>[^_]")
9162 (save-excursion
9163 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
9164 'beginning)
9165 (setq placeholder (point)))))
9166 (goto-char placeholder)
9167 (c-add-stmt-syntax 'do-while-closure nil t
9168 containing-sexp paren-state))
9170 ;; CASE 13: A catch or finally clause? This case is simpler
9171 ;; than if-else and do-while, because a block is required
9172 ;; after every try, catch and finally.
9173 ((save-excursion
9174 (and (cond ((c-major-mode-is 'c++-mode)
9175 (looking-at "catch\\>[^_]"))
9176 ((c-major-mode-is 'java-mode)
9177 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
9178 (and (c-safe (c-backward-syntactic-ws)
9179 (c-backward-sexp)
9181 (eq (char-after) ?{)
9182 (c-safe (c-backward-syntactic-ws)
9183 (c-backward-sexp)
9185 (if (eq (char-after) ?\()
9186 (c-safe (c-backward-sexp) t)
9188 (looking-at "\\(try\\|catch\\)\\>[^_]")
9189 (setq placeholder (point))))
9190 (goto-char placeholder)
9191 (c-add-stmt-syntax 'catch-clause nil t
9192 containing-sexp paren-state))
9194 ;; CASE 18: A substatement we can recognize by keyword.
9195 ((save-excursion
9196 (and c-opt-block-stmt-key
9197 (not (eq char-before-ip ?\;))
9198 (not (c-at-vsemi-p before-ws-ip))
9199 (not (memq char-after-ip '(?\) ?\] ?,)))
9200 (or (not (eq char-before-ip ?}))
9201 (c-looking-at-inexpr-block-backward c-state-cache))
9202 (> (point)
9203 (progn
9204 ;; Ought to cache the result from the
9205 ;; c-beginning-of-statement-1 calls here.
9206 (setq placeholder (point))
9207 (while (eq (setq step-type
9208 (c-beginning-of-statement-1 lim))
9209 'label))
9210 (if (eq step-type 'previous)
9211 (goto-char placeholder)
9212 (setq placeholder (point))
9213 (if (and (eq step-type 'same)
9214 (not (looking-at c-opt-block-stmt-key)))
9215 ;; Step up to the containing statement if we
9216 ;; stayed in the same one.
9217 (let (step)
9218 (while (eq
9219 (setq step
9220 (c-beginning-of-statement-1 lim))
9221 'label))
9222 (if (eq step 'up)
9223 (setq placeholder (point))
9224 ;; There was no containing statement after all.
9225 (goto-char placeholder)))))
9226 placeholder))
9227 (if (looking-at c-block-stmt-2-key)
9228 ;; Require a parenthesis after these keywords.
9229 ;; Necessary to catch e.g. synchronized in Java,
9230 ;; which can be used both as statement and
9231 ;; modifier.
9232 (and (zerop (c-forward-token-2 1 nil))
9233 (eq (char-after) ?\())
9234 (looking-at c-opt-block-stmt-key))))
9236 (if (eq step-type 'up)
9237 ;; CASE 18A: Simple substatement.
9238 (progn
9239 (goto-char placeholder)
9240 (cond
9241 ((eq char-after-ip ?{)
9242 (c-add-stmt-syntax 'substatement-open nil nil
9243 containing-sexp paren-state))
9244 ((save-excursion
9245 (goto-char indent-point)
9246 (back-to-indentation)
9247 (c-forward-label))
9248 (c-add-stmt-syntax 'substatement-label nil nil
9249 containing-sexp paren-state))
9251 (c-add-stmt-syntax 'substatement nil nil
9252 containing-sexp paren-state))))
9254 ;; CASE 18B: Some other substatement. This is shared
9255 ;; with case 10.
9256 (c-guess-continued-construct indent-point
9257 char-after-ip
9258 placeholder
9260 paren-state)))
9262 ;; CASE 14: A case or default label
9263 ((looking-at c-label-kwds-regexp)
9264 (if containing-sexp
9265 (progn
9266 (goto-char containing-sexp)
9267 (setq lim (c-most-enclosing-brace c-state-cache
9268 containing-sexp))
9269 (c-backward-to-block-anchor lim)
9270 (c-add-stmt-syntax 'case-label nil t lim paren-state))
9271 ;; Got a bogus label at the top level. In lack of better
9272 ;; alternatives, anchor it on (point-min).
9273 (c-add-syntax 'case-label (point-min))))
9275 ;; CASE 15: any other label
9276 ((save-excursion
9277 (back-to-indentation)
9278 (and (not (looking-at c-syntactic-ws-start))
9279 (c-forward-label)))
9280 (cond (containing-decl-open
9281 (setq placeholder (c-add-class-syntax 'inclass
9282 containing-decl-open
9283 containing-decl-start
9284 containing-decl-kwd
9285 paren-state))
9286 ;; Append access-label with the same anchor point as
9287 ;; inclass gets.
9288 (c-append-syntax 'access-label placeholder))
9290 (containing-sexp
9291 (goto-char containing-sexp)
9292 (setq lim (c-most-enclosing-brace c-state-cache
9293 containing-sexp))
9294 (save-excursion
9295 (setq tmpsymbol
9296 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
9297 (looking-at "switch\\>[^_]"))
9298 ;; If the surrounding statement is a switch then
9299 ;; let's analyze all labels as switch labels, so
9300 ;; that they get lined up consistently.
9301 'case-label
9302 'label)))
9303 (c-backward-to-block-anchor lim)
9304 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
9307 ;; A label on the top level. Treat it as a class
9308 ;; context. (point-min) is the closest we get to the
9309 ;; class open brace.
9310 (c-add-syntax 'access-label (point-min)))))
9312 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9313 ;; 17E.
9314 ((setq placeholder (c-looking-at-inexpr-block
9315 (c-safe-position containing-sexp paren-state)
9316 containing-sexp
9317 ;; Have to turn on the heuristics after
9318 ;; the point even though it doesn't work
9319 ;; very well. C.f. test case class-16.pike.
9321 (setq tmpsymbol (assq (car placeholder)
9322 '((inexpr-class . class-open)
9323 (inexpr-statement . block-open))))
9324 (if tmpsymbol
9325 ;; It's a statement block or an anonymous class.
9326 (setq tmpsymbol (cdr tmpsymbol))
9327 ;; It's a Pike lambda. Check whether we are between the
9328 ;; lambda keyword and the argument list or at the defun
9329 ;; opener.
9330 (setq tmpsymbol (if (eq char-after-ip ?{)
9331 'inline-open
9332 'lambda-intro-cont)))
9333 (goto-char (cdr placeholder))
9334 (back-to-indentation)
9335 (c-add-stmt-syntax tmpsymbol nil t
9336 (c-most-enclosing-brace c-state-cache (point))
9337 paren-state)
9338 (unless (eq (point) (cdr placeholder))
9339 (c-add-syntax (car placeholder))))
9341 ;; CASE 5: Line is inside a declaration level block or at top level.
9342 ((or containing-decl-open (null containing-sexp))
9343 (cond
9345 ;; CASE 5A: we are looking at a defun, brace list, class,
9346 ;; or inline-inclass method opening brace
9347 ((setq special-brace-list
9348 (or (and c-special-brace-lists
9349 (c-looking-at-special-brace-list))
9350 (eq char-after-ip ?{)))
9351 (cond
9353 ;; CASE 5A.1: Non-class declaration block open.
9354 ((save-excursion
9355 (let (tmp)
9356 (and (eq char-after-ip ?{)
9357 (setq tmp (c-looking-at-decl-block containing-sexp t))
9358 (progn
9359 (setq placeholder (point))
9360 (goto-char tmp)
9361 (looking-at c-symbol-key))
9362 (c-keyword-member
9363 (c-keyword-sym (setq keyword (match-string 0)))
9364 'c-other-block-decl-kwds))))
9365 (goto-char placeholder)
9366 (c-add-stmt-syntax
9367 (if (string-equal keyword "extern")
9368 ;; Special case for extern-lang-open.
9369 'extern-lang-open
9370 (intern (concat keyword "-open")))
9371 nil t containing-sexp paren-state))
9373 ;; CASE 5A.2: we are looking at a class opening brace
9374 ((save-excursion
9375 (goto-char indent-point)
9376 (skip-chars-forward " \t")
9377 (and (eq (char-after) ?{)
9378 (c-looking-at-decl-block containing-sexp t)
9379 (setq placeholder (point))))
9380 (c-add-syntax 'class-open placeholder))
9382 ;; CASE 5A.3: brace list open
9383 ((save-excursion
9384 (c-beginning-of-decl-1 lim)
9385 (while (looking-at c-specifier-key)
9386 (goto-char (match-end 1))
9387 (c-forward-syntactic-ws indent-point))
9388 (setq placeholder (c-point 'boi))
9389 (or (consp special-brace-list)
9390 (and (or (save-excursion
9391 (goto-char indent-point)
9392 (setq tmpsymbol nil)
9393 (while (and (> (point) placeholder)
9394 (zerop (c-backward-token-2 1 t))
9395 (/= (char-after) ?=))
9396 (and c-opt-inexpr-brace-list-key
9397 (not tmpsymbol)
9398 (looking-at c-opt-inexpr-brace-list-key)
9399 (setq tmpsymbol 'topmost-intro-cont)))
9400 (eq (char-after) ?=))
9401 (looking-at c-brace-list-key))
9402 (save-excursion
9403 (while (and (< (point) indent-point)
9404 (zerop (c-forward-token-2 1 t))
9405 (not (memq (char-after) '(?\; ?\()))))
9406 (not (memq (char-after) '(?\; ?\()))
9407 ))))
9408 (if (and (not c-auto-newline-analysis)
9409 (c-major-mode-is 'java-mode)
9410 (eq tmpsymbol 'topmost-intro-cont))
9411 ;; We're in Java and have found that the open brace
9412 ;; belongs to a "new Foo[]" initialization list,
9413 ;; which means the brace list is part of an
9414 ;; expression and not a top level definition. We
9415 ;; therefore treat it as any topmost continuation
9416 ;; even though the semantically correct symbol still
9417 ;; is brace-list-open, on the same grounds as in
9418 ;; case B.2.
9419 (progn
9420 (c-beginning-of-statement-1 lim)
9421 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9422 (c-add-syntax 'brace-list-open placeholder)))
9424 ;; CASE 5A.4: inline defun open
9425 ((and containing-decl-open
9426 (not (c-keyword-member containing-decl-kwd
9427 'c-other-block-decl-kwds)))
9428 (c-add-syntax 'inline-open)
9429 (c-add-class-syntax 'inclass
9430 containing-decl-open
9431 containing-decl-start
9432 containing-decl-kwd
9433 paren-state))
9435 ;; CASE 5A.5: ordinary defun open
9437 (save-excursion
9438 (c-beginning-of-decl-1 lim)
9439 (while (looking-at c-specifier-key)
9440 (goto-char (match-end 1))
9441 (c-forward-syntactic-ws indent-point))
9442 (c-add-syntax 'defun-open (c-point 'boi))
9443 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9444 ;; 2007-11-09)
9445 ))))
9447 ;; CASE 5B: After a function header but before the body (or
9448 ;; the ending semicolon if there's no body).
9449 ((save-excursion
9450 (when (setq placeholder (c-just-after-func-arglist-p lim))
9451 (setq tmp-pos (point))))
9452 (cond
9454 ;; CASE 5B.1: Member init list.
9455 ((eq (char-after tmp-pos) ?:)
9456 (if (or (>= tmp-pos indent-point)
9457 (= (c-point 'bosws) (1+ tmp-pos)))
9458 (progn
9459 ;; There is no preceding member init clause.
9460 ;; Indent relative to the beginning of indentation
9461 ;; for the topmost-intro line that contains the
9462 ;; prototype's open paren.
9463 (goto-char placeholder)
9464 (c-add-syntax 'member-init-intro (c-point 'boi)))
9465 ;; Indent relative to the first member init clause.
9466 (goto-char (1+ tmp-pos))
9467 (c-forward-syntactic-ws)
9468 (c-add-syntax 'member-init-cont (point))))
9470 ;; CASE 5B.2: K&R arg decl intro
9471 ((and c-recognize-knr-p
9472 (c-in-knr-argdecl lim))
9473 (c-beginning-of-statement-1 lim)
9474 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9475 (if containing-decl-open
9476 (c-add-class-syntax 'inclass
9477 containing-decl-open
9478 containing-decl-start
9479 containing-decl-kwd
9480 paren-state)))
9482 ;; CASE 5B.4: Nether region after a C++ or Java func
9483 ;; decl, which could include a `throws' declaration.
9485 (c-beginning-of-statement-1 lim)
9486 (c-add-syntax 'func-decl-cont (c-point 'boi))
9489 ;; CASE 5C: inheritance line. could be first inheritance
9490 ;; line, or continuation of a multiple inheritance
9491 ((or (and (c-major-mode-is 'c++-mode)
9492 (progn
9493 (when (eq char-after-ip ?,)
9494 (skip-chars-forward " \t")
9495 (forward-char))
9496 (looking-at c-opt-postfix-decl-spec-key)))
9497 (and (or (eq char-before-ip ?:)
9498 ;; watch out for scope operator
9499 (save-excursion
9500 (and (eq char-after-ip ?:)
9501 (c-safe (forward-char 1) t)
9502 (not (eq (char-after) ?:))
9504 (save-excursion
9505 (c-backward-syntactic-ws lim)
9506 (if (eq char-before-ip ?:)
9507 (progn
9508 (forward-char -1)
9509 (c-backward-syntactic-ws lim)))
9510 (back-to-indentation)
9511 (looking-at c-class-key)))
9512 ;; for Java
9513 (and (c-major-mode-is 'java-mode)
9514 (let ((fence (save-excursion
9515 (c-beginning-of-statement-1 lim)
9516 (point)))
9517 cont done)
9518 (save-excursion
9519 (while (not done)
9520 (cond ((looking-at c-opt-postfix-decl-spec-key)
9521 (setq injava-inher (cons cont (point))
9522 done t))
9523 ((or (not (c-safe (c-forward-sexp -1) t))
9524 (<= (point) fence))
9525 (setq done t))
9527 (setq cont t)))
9528 injava-inher)
9529 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9530 (point)))
9532 (cond
9534 ;; CASE 5C.1: non-hanging colon on an inher intro
9535 ((eq char-after-ip ?:)
9536 (c-beginning-of-statement-1 lim)
9537 (c-add-syntax 'inher-intro (c-point 'boi))
9538 ;; don't add inclass symbol since relative point already
9539 ;; contains any class offset
9542 ;; CASE 5C.2: hanging colon on an inher intro
9543 ((eq char-before-ip ?:)
9544 (c-beginning-of-statement-1 lim)
9545 (c-add-syntax 'inher-intro (c-point 'boi))
9546 (if containing-decl-open
9547 (c-add-class-syntax 'inclass
9548 containing-decl-open
9549 containing-decl-start
9550 containing-decl-kwd
9551 paren-state)))
9553 ;; CASE 5C.3: in a Java implements/extends
9554 (injava-inher
9555 (let ((where (cdr injava-inher))
9556 (cont (car injava-inher)))
9557 (goto-char where)
9558 (cond ((looking-at "throws\\>[^_]")
9559 (c-add-syntax 'func-decl-cont
9560 (progn (c-beginning-of-statement-1 lim)
9561 (c-point 'boi))))
9562 (cont (c-add-syntax 'inher-cont where))
9563 (t (c-add-syntax 'inher-intro
9564 (progn (goto-char (cdr injava-inher))
9565 (c-beginning-of-statement-1 lim)
9566 (point))))
9569 ;; CASE 5C.4: a continued inheritance line
9571 (c-beginning-of-inheritance-list lim)
9572 (c-add-syntax 'inher-cont (point))
9573 ;; don't add inclass symbol since relative point already
9574 ;; contains any class offset
9577 ;; CASE 5D: this could be a top-level initialization, a
9578 ;; member init list continuation, or a template argument
9579 ;; list continuation.
9580 ((save-excursion
9581 ;; Note: We use the fact that lim is always after any
9582 ;; preceding brace sexp.
9583 (if c-recognize-<>-arglists
9584 (while (and
9585 (progn
9586 (c-syntactic-skip-backward "^;,=<>" lim t)
9587 (> (point) lim))
9589 (when c-overloadable-operators-regexp
9590 (when (setq placeholder (c-after-special-operator-id lim))
9591 (goto-char placeholder)
9593 (cond
9594 ((eq (char-before) ?>)
9595 (or (c-backward-<>-arglist nil lim)
9596 (backward-char))
9598 ((eq (char-before) ?<)
9599 (backward-char)
9600 (if (save-excursion
9601 (c-forward-<>-arglist nil))
9602 (progn (forward-char)
9603 nil)
9605 (t nil)))))
9606 ;; NB: No c-after-special-operator-id stuff in this
9607 ;; clause - we assume only C++ needs it.
9608 (c-syntactic-skip-backward "^;,=" lim t))
9609 (memq (char-before) '(?, ?= ?<)))
9610 (cond
9612 ;; CASE 5D.3: perhaps a template list continuation?
9613 ((and (c-major-mode-is 'c++-mode)
9614 (save-excursion
9615 (save-restriction
9616 (c-with-syntax-table c++-template-syntax-table
9617 (goto-char indent-point)
9618 (setq placeholder (c-up-list-backward))
9619 (and placeholder
9620 (eq (char-after placeholder) ?<))))))
9621 (c-with-syntax-table c++-template-syntax-table
9622 (goto-char placeholder)
9623 (c-beginning-of-statement-1 lim t)
9624 (if (save-excursion
9625 (c-backward-syntactic-ws lim)
9626 (eq (char-before) ?<))
9627 ;; In a nested template arglist.
9628 (progn
9629 (goto-char placeholder)
9630 (c-syntactic-skip-backward "^,;" lim t)
9631 (c-forward-syntactic-ws))
9632 (back-to-indentation)))
9633 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9634 ;; template aware.
9635 (c-add-syntax 'template-args-cont (point) placeholder))
9637 ;; CASE 5D.4: perhaps a multiple inheritance line?
9638 ((and (c-major-mode-is 'c++-mode)
9639 (save-excursion
9640 (c-beginning-of-statement-1 lim)
9641 (setq placeholder (point))
9642 (if (looking-at "static\\>[^_]")
9643 (c-forward-token-2 1 nil indent-point))
9644 (and (looking-at c-class-key)
9645 (zerop (c-forward-token-2 2 nil indent-point))
9646 (if (eq (char-after) ?<)
9647 (c-with-syntax-table c++-template-syntax-table
9648 (zerop (c-forward-token-2 1 t indent-point)))
9650 (eq (char-after) ?:))))
9651 (goto-char placeholder)
9652 (c-add-syntax 'inher-cont (c-point 'boi)))
9654 ;; CASE 5D.5: Continuation of the "expression part" of a
9655 ;; top level construct. Or, perhaps, an unrecognized construct.
9657 (while (and (setq placeholder (point))
9658 (eq (car (c-beginning-of-decl-1 containing-sexp))
9659 'same)
9660 (save-excursion
9661 (c-backward-syntactic-ws)
9662 (eq (char-before) ?}))
9663 (< (point) placeholder)))
9664 (c-add-stmt-syntax
9665 (cond
9666 ((eq (point) placeholder) 'statement) ; unrecognized construct
9667 ;; A preceding comma at the top level means that a
9668 ;; new variable declaration starts here. Use
9669 ;; topmost-intro-cont for it, for consistency with
9670 ;; the first variable declaration. C.f. case 5N.
9671 ((eq char-before-ip ?,) 'topmost-intro-cont)
9672 (t 'statement-cont))
9673 nil nil containing-sexp paren-state))
9676 ;; CASE 5F: Close of a non-class declaration level block.
9677 ((and (eq char-after-ip ?})
9678 (c-keyword-member containing-decl-kwd
9679 'c-other-block-decl-kwds))
9680 ;; This is inconsistent: Should use `containing-decl-open'
9681 ;; here if it's at boi, like in case 5J.
9682 (goto-char containing-decl-start)
9683 (c-add-stmt-syntax
9684 (if (string-equal (symbol-name containing-decl-kwd) "extern")
9685 ;; Special case for compatibility with the
9686 ;; extern-lang syntactic symbols.
9687 'extern-lang-close
9688 (intern (concat (symbol-name containing-decl-kwd)
9689 "-close")))
9690 nil t
9691 (c-most-enclosing-brace paren-state (point))
9692 paren-state))
9694 ;; CASE 5G: we are looking at the brace which closes the
9695 ;; enclosing nested class decl
9696 ((and containing-sexp
9697 (eq char-after-ip ?})
9698 (eq containing-decl-open containing-sexp))
9699 (c-add-class-syntax 'class-close
9700 containing-decl-open
9701 containing-decl-start
9702 containing-decl-kwd
9703 paren-state))
9705 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9706 ((and c-recognize-knr-p
9707 (not containing-sexp) ; can't be knr inside braces.
9708 (not (eq char-before-ip ?}))
9709 (save-excursion
9710 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
9711 (and placeholder
9712 ;; Do an extra check to avoid tripping up on
9713 ;; statements that occur in invalid contexts
9714 ;; (e.g. in macro bodies where we don't really
9715 ;; know the context of what we're looking at).
9716 (not (and c-opt-block-stmt-key
9717 (looking-at c-opt-block-stmt-key)))))
9718 (< placeholder indent-point))
9719 (goto-char placeholder)
9720 (c-add-syntax 'knr-argdecl (point)))
9722 ;; CASE 5I: ObjC method definition.
9723 ((and c-opt-method-key
9724 (looking-at c-opt-method-key))
9725 (c-beginning-of-statement-1 nil t)
9726 (if (= (point) indent-point)
9727 ;; Handle the case when it's the first (non-comment)
9728 ;; thing in the buffer. Can't look for a 'same return
9729 ;; value from cbos1 since ObjC directives currently
9730 ;; aren't recognized fully, so that we get 'same
9731 ;; instead of 'previous if it moved over a preceding
9732 ;; directive.
9733 (goto-char (point-min)))
9734 (c-add-syntax 'objc-method-intro (c-point 'boi)))
9736 ;; CASE 5P: AWK pattern or function or continuation
9737 ;; thereof.
9738 ((c-major-mode-is 'awk-mode)
9739 (setq placeholder (point))
9740 (c-add-stmt-syntax
9741 (if (and (eq (c-beginning-of-statement-1) 'same)
9742 (/= (point) placeholder))
9743 'topmost-intro-cont
9744 'topmost-intro)
9745 nil nil
9746 containing-sexp paren-state))
9748 ;; CASE 5N: At a variable declaration that follows a class
9749 ;; definition or some other block declaration that doesn't
9750 ;; end at the closing '}'. C.f. case 5D.5.
9751 ((progn
9752 (c-backward-syntactic-ws lim)
9753 (and (eq (char-before) ?})
9754 (save-excursion
9755 (let ((start (point)))
9756 (if (and c-state-cache
9757 (consp (car c-state-cache))
9758 (eq (cdar c-state-cache) (point)))
9759 ;; Speed up the backward search a bit.
9760 (goto-char (caar c-state-cache)))
9761 (c-beginning-of-decl-1 containing-sexp)
9762 (setq placeholder (point))
9763 (if (= start (point))
9764 ;; The '}' is unbalanced.
9766 (c-end-of-decl-1)
9767 (>= (point) indent-point))))))
9768 (goto-char placeholder)
9769 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9770 containing-sexp paren-state))
9772 ;; NOTE: The point is at the end of the previous token here.
9774 ;; CASE 5J: we are at the topmost level, make
9775 ;; sure we skip back past any access specifiers
9776 ((and
9777 ;; A macro continuation line is never at top level.
9778 (not (and macro-start
9779 (> indent-point macro-start)))
9780 (save-excursion
9781 (setq placeholder (point))
9782 (or (memq char-before-ip '(?\; ?{ ?} nil))
9783 (c-at-vsemi-p before-ws-ip)
9784 (when (and (eq char-before-ip ?:)
9785 (eq (c-beginning-of-statement-1 lim)
9786 'label))
9787 (c-backward-syntactic-ws lim)
9788 (setq placeholder (point)))
9789 (and (c-major-mode-is 'objc-mode)
9790 (catch 'not-in-directive
9791 (c-beginning-of-statement-1 lim)
9792 (setq placeholder (point))
9793 (while (and (c-forward-objc-directive)
9794 (< (point) indent-point))
9795 (c-forward-syntactic-ws)
9796 (if (>= (point) indent-point)
9797 (throw 'not-in-directive t))
9798 (setq placeholder (point)))
9799 nil)))))
9800 ;; For historic reasons we anchor at bol of the last
9801 ;; line of the previous declaration. That's clearly
9802 ;; highly bogus and useless, and it makes our lives hard
9803 ;; to remain compatible. :P
9804 (goto-char placeholder)
9805 (c-add-syntax 'topmost-intro (c-point 'bol))
9806 (if containing-decl-open
9807 (if (c-keyword-member containing-decl-kwd
9808 'c-other-block-decl-kwds)
9809 (progn
9810 (goto-char (c-brace-anchor-point containing-decl-open))
9811 (c-add-stmt-syntax
9812 (if (string-equal (symbol-name containing-decl-kwd)
9813 "extern")
9814 ;; Special case for compatibility with the
9815 ;; extern-lang syntactic symbols.
9816 'inextern-lang
9817 (intern (concat "in"
9818 (symbol-name containing-decl-kwd))))
9819 nil t
9820 (c-most-enclosing-brace paren-state (point))
9821 paren-state))
9822 (c-add-class-syntax 'inclass
9823 containing-decl-open
9824 containing-decl-start
9825 containing-decl-kwd
9826 paren-state)))
9827 (when (and c-syntactic-indentation-in-macros
9828 macro-start
9829 (/= macro-start (c-point 'boi indent-point)))
9830 (c-add-syntax 'cpp-define-intro)
9831 (setq macro-start nil)))
9833 ;; CASE 5K: we are at an ObjC method definition
9834 ;; continuation line.
9835 ((and c-opt-method-key
9836 (save-excursion
9837 (c-beginning-of-statement-1 lim)
9838 (beginning-of-line)
9839 (when (looking-at c-opt-method-key)
9840 (setq placeholder (point)))))
9841 (c-add-syntax 'objc-method-args-cont placeholder))
9843 ;; CASE 5L: we are at the first argument of a template
9844 ;; arglist that begins on the previous line.
9845 ((and c-recognize-<>-arglists
9846 (eq (char-before) ?<)
9847 (not (and c-overloadable-operators-regexp
9848 (c-after-special-operator-id lim))))
9849 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9850 (c-add-syntax 'template-args-cont (c-point 'boi)))
9852 ;; CASE 5Q: we are at a statement within a macro.
9853 (macro-start
9854 (c-beginning-of-statement-1 containing-sexp)
9855 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
9857 ;;CASE 5N: We are at a topmost continuation line and the only
9858 ;;preceding items are annotations.
9859 ((and (c-major-mode-is 'java-mode)
9860 (setq placeholder (point))
9861 (c-beginning-of-statement-1)
9862 (progn
9863 (while (and (c-forward-annotation))
9864 (c-forward-syntactic-ws))
9866 (prog1
9867 (>= (point) placeholder)
9868 (goto-char placeholder)))
9869 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
9871 ;; CASE 5M: we are at a topmost continuation line
9873 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9874 (when (c-major-mode-is 'objc-mode)
9875 (setq placeholder (point))
9876 (while (and (c-forward-objc-directive)
9877 (< (point) indent-point))
9878 (c-forward-syntactic-ws)
9879 (setq placeholder (point)))
9880 (goto-char placeholder))
9881 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9885 ;; (CASE 6 has been removed.)
9887 ;; CASE 7: line is an expression, not a statement. Most
9888 ;; likely we are either in a function prototype or a function
9889 ;; call argument list
9890 ((not (or (and c-special-brace-lists
9891 (save-excursion
9892 (goto-char containing-sexp)
9893 (c-looking-at-special-brace-list)))
9894 (eq (char-after containing-sexp) ?{)))
9895 (cond
9897 ;; CASE 7A: we are looking at the arglist closing paren.
9898 ;; C.f. case 7F.
9899 ((memq char-after-ip '(?\) ?\]))
9900 (goto-char containing-sexp)
9901 (setq placeholder (c-point 'boi))
9902 (if (and (c-safe (backward-up-list 1) t)
9903 (>= (point) placeholder))
9904 (progn
9905 (forward-char)
9906 (skip-chars-forward " \t"))
9907 (goto-char placeholder))
9908 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
9909 (c-most-enclosing-brace paren-state (point))
9910 paren-state))
9912 ;; CASE 7B: Looking at the opening brace of an
9913 ;; in-expression block or brace list. C.f. cases 4, 16A
9914 ;; and 17E.
9915 ((and (eq char-after-ip ?{)
9916 (progn
9917 (setq placeholder (c-inside-bracelist-p (point)
9918 paren-state))
9919 (if placeholder
9920 (setq tmpsymbol '(brace-list-open . inexpr-class))
9921 (setq tmpsymbol '(block-open . inexpr-statement)
9922 placeholder
9923 (cdr-safe (c-looking-at-inexpr-block
9924 (c-safe-position containing-sexp
9925 paren-state)
9926 containing-sexp)))
9927 ;; placeholder is nil if it's a block directly in
9928 ;; a function arglist. That makes us skip out of
9929 ;; this case.
9931 (goto-char placeholder)
9932 (back-to-indentation)
9933 (c-add-stmt-syntax (car tmpsymbol) nil t
9934 (c-most-enclosing-brace paren-state (point))
9935 paren-state)
9936 (if (/= (point) placeholder)
9937 (c-add-syntax (cdr tmpsymbol))))
9939 ;; CASE 7C: we are looking at the first argument in an empty
9940 ;; argument list. Use arglist-close if we're actually
9941 ;; looking at a close paren or bracket.
9942 ((memq char-before-ip '(?\( ?\[))
9943 (goto-char containing-sexp)
9944 (setq placeholder (c-point 'boi))
9945 (if (and (c-safe (backward-up-list 1) t)
9946 (>= (point) placeholder))
9947 (progn
9948 (forward-char)
9949 (skip-chars-forward " \t"))
9950 (goto-char placeholder))
9951 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
9952 (c-most-enclosing-brace paren-state (point))
9953 paren-state))
9955 ;; CASE 7D: we are inside a conditional test clause. treat
9956 ;; these things as statements
9957 ((progn
9958 (goto-char containing-sexp)
9959 (and (c-safe (c-forward-sexp -1) t)
9960 (looking-at "\\<for\\>[^_]")))
9961 (goto-char (1+ containing-sexp))
9962 (c-forward-syntactic-ws indent-point)
9963 (if (eq char-before-ip ?\;)
9964 (c-add-syntax 'statement (point))
9965 (c-add-syntax 'statement-cont (point))
9968 ;; CASE 7E: maybe a continued ObjC method call. This is the
9969 ;; case when we are inside a [] bracketed exp, and what
9970 ;; precede the opening bracket is not an identifier.
9971 ((and c-opt-method-key
9972 (eq (char-after containing-sexp) ?\[)
9973 (progn
9974 (goto-char (1- containing-sexp))
9975 (c-backward-syntactic-ws (c-point 'bod))
9976 (if (not (looking-at c-symbol-key))
9977 (c-add-syntax 'objc-method-call-cont containing-sexp))
9980 ;; CASE 7F: we are looking at an arglist continuation line,
9981 ;; but the preceding argument is on the same line as the
9982 ;; opening paren. This case includes multi-line
9983 ;; mathematical paren groupings, but we could be on a
9984 ;; for-list continuation line. C.f. case 7A.
9985 ((progn
9986 (goto-char (1+ containing-sexp))
9987 (< (save-excursion
9988 (c-forward-syntactic-ws)
9989 (point))
9990 (c-point 'bonl)))
9991 (goto-char containing-sexp) ; paren opening the arglist
9992 (setq placeholder (c-point 'boi))
9993 (if (and (c-safe (backward-up-list 1) t)
9994 (>= (point) placeholder))
9995 (progn
9996 (forward-char)
9997 (skip-chars-forward " \t"))
9998 (goto-char placeholder))
9999 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
10000 (c-most-enclosing-brace c-state-cache (point))
10001 paren-state))
10003 ;; CASE 7G: we are looking at just a normal arglist
10004 ;; continuation line
10005 (t (c-forward-syntactic-ws indent-point)
10006 (c-add-syntax 'arglist-cont (c-point 'boi)))
10009 ;; CASE 8: func-local multi-inheritance line
10010 ((and (c-major-mode-is 'c++-mode)
10011 (save-excursion
10012 (goto-char indent-point)
10013 (skip-chars-forward " \t")
10014 (looking-at c-opt-postfix-decl-spec-key)))
10015 (goto-char indent-point)
10016 (skip-chars-forward " \t")
10017 (cond
10019 ;; CASE 8A: non-hanging colon on an inher intro
10020 ((eq char-after-ip ?:)
10021 (c-backward-syntactic-ws lim)
10022 (c-add-syntax 'inher-intro (c-point 'boi)))
10024 ;; CASE 8B: hanging colon on an inher intro
10025 ((eq char-before-ip ?:)
10026 (c-add-syntax 'inher-intro (c-point 'boi)))
10028 ;; CASE 8C: a continued inheritance line
10030 (c-beginning-of-inheritance-list lim)
10031 (c-add-syntax 'inher-cont (point))
10034 ;; CASE 9: we are inside a brace-list
10035 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
10036 (setq special-brace-list
10037 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
10038 (save-excursion
10039 (goto-char containing-sexp)
10040 (c-looking-at-special-brace-list)))
10041 (c-inside-bracelist-p containing-sexp paren-state))))
10042 (cond
10044 ;; CASE 9A: In the middle of a special brace list opener.
10045 ((and (consp special-brace-list)
10046 (save-excursion
10047 (goto-char containing-sexp)
10048 (eq (char-after) ?\())
10049 (eq char-after-ip (car (cdr special-brace-list))))
10050 (goto-char (car (car special-brace-list)))
10051 (skip-chars-backward " \t")
10052 (if (and (bolp)
10053 (assoc 'statement-cont
10054 (setq placeholder (c-guess-basic-syntax))))
10055 (setq c-syntactic-context placeholder)
10056 (c-beginning-of-statement-1
10057 (c-safe-position (1- containing-sexp) paren-state))
10058 (c-forward-token-2 0)
10059 (while (looking-at c-specifier-key)
10060 (goto-char (match-end 1))
10061 (c-forward-syntactic-ws))
10062 (c-add-syntax 'brace-list-open (c-point 'boi))))
10064 ;; CASE 9B: brace-list-close brace
10065 ((if (consp special-brace-list)
10066 ;; Check special brace list closer.
10067 (progn
10068 (goto-char (car (car special-brace-list)))
10069 (save-excursion
10070 (goto-char indent-point)
10071 (back-to-indentation)
10073 ;; We were between the special close char and the `)'.
10074 (and (eq (char-after) ?\))
10075 (eq (1+ (point)) (cdr (car special-brace-list))))
10076 ;; We were before the special close char.
10077 (and (eq (char-after) (cdr (cdr special-brace-list)))
10078 (zerop (c-forward-token-2))
10079 (eq (1+ (point)) (cdr (car special-brace-list)))))))
10080 ;; Normal brace list check.
10081 (and (eq char-after-ip ?})
10082 (c-safe (goto-char (c-up-list-backward (point))) t)
10083 (= (point) containing-sexp)))
10084 (if (eq (point) (c-point 'boi))
10085 (c-add-syntax 'brace-list-close (point))
10086 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10087 (c-beginning-of-statement-1 lim)
10088 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
10091 ;; Prepare for the rest of the cases below by going to the
10092 ;; token following the opening brace
10093 (if (consp special-brace-list)
10094 (progn
10095 (goto-char (car (car special-brace-list)))
10096 (c-forward-token-2 1 nil indent-point))
10097 (goto-char containing-sexp))
10098 (forward-char)
10099 (let ((start (point)))
10100 (c-forward-syntactic-ws indent-point)
10101 (goto-char (max start (c-point 'bol))))
10102 (c-skip-ws-forward indent-point)
10103 (cond
10105 ;; CASE 9C: we're looking at the first line in a brace-list
10106 ((= (point) indent-point)
10107 (if (consp special-brace-list)
10108 (goto-char (car (car special-brace-list)))
10109 (goto-char containing-sexp))
10110 (if (eq (point) (c-point 'boi))
10111 (c-add-syntax 'brace-list-intro (point))
10112 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10113 (c-beginning-of-statement-1 lim)
10114 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
10116 ;; CASE 9D: this is just a later brace-list-entry or
10117 ;; brace-entry-open
10118 (t (if (or (eq char-after-ip ?{)
10119 (and c-special-brace-lists
10120 (save-excursion
10121 (goto-char indent-point)
10122 (c-forward-syntactic-ws (c-point 'eol))
10123 (c-looking-at-special-brace-list (point)))))
10124 (c-add-syntax 'brace-entry-open (point))
10125 (c-add-syntax 'brace-list-entry (point))
10127 ))))
10129 ;; CASE 10: A continued statement or top level construct.
10130 ((and (not (memq char-before-ip '(?\; ?:)))
10131 (not (c-at-vsemi-p before-ws-ip))
10132 (or (not (eq char-before-ip ?}))
10133 (c-looking-at-inexpr-block-backward c-state-cache))
10134 (> (point)
10135 (save-excursion
10136 (c-beginning-of-statement-1 containing-sexp)
10137 (setq placeholder (point))))
10138 (/= placeholder containing-sexp))
10139 ;; This is shared with case 18.
10140 (c-guess-continued-construct indent-point
10141 char-after-ip
10142 placeholder
10143 containing-sexp
10144 paren-state))
10146 ;; CASE 16: block close brace, possibly closing the defun or
10147 ;; the class
10148 ((eq char-after-ip ?})
10149 ;; From here on we have the next containing sexp in lim.
10150 (setq lim (c-most-enclosing-brace paren-state))
10151 (goto-char containing-sexp)
10152 (cond
10154 ;; CASE 16E: Closing a statement block? This catches
10155 ;; cases where it's preceded by a statement keyword,
10156 ;; which works even when used in an "invalid" context,
10157 ;; e.g. a macro argument.
10158 ((c-after-conditional)
10159 (c-backward-to-block-anchor lim)
10160 (c-add-stmt-syntax 'block-close nil t lim paren-state))
10162 ;; CASE 16A: closing a lambda defun or an in-expression
10163 ;; block? C.f. cases 4, 7B and 17E.
10164 ((setq placeholder (c-looking-at-inexpr-block
10165 (c-safe-position containing-sexp paren-state)
10166 nil))
10167 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10168 'inline-close
10169 'block-close))
10170 (goto-char containing-sexp)
10171 (back-to-indentation)
10172 (if (= containing-sexp (point))
10173 (c-add-syntax tmpsymbol (point))
10174 (goto-char (cdr placeholder))
10175 (back-to-indentation)
10176 (c-add-stmt-syntax tmpsymbol nil t
10177 (c-most-enclosing-brace paren-state (point))
10178 paren-state)
10179 (if (/= (point) (cdr placeholder))
10180 (c-add-syntax (car placeholder)))))
10182 ;; CASE 16B: does this close an inline or a function in
10183 ;; a non-class declaration level block?
10184 ((save-excursion
10185 (and lim
10186 (progn
10187 (goto-char lim)
10188 (c-looking-at-decl-block
10189 (c-most-enclosing-brace paren-state lim)
10190 nil))
10191 (setq placeholder (point))))
10192 (c-backward-to-decl-anchor lim)
10193 (back-to-indentation)
10194 (if (save-excursion
10195 (goto-char placeholder)
10196 (looking-at c-other-decl-block-key))
10197 (c-add-syntax 'defun-close (point))
10198 (c-add-syntax 'inline-close (point))))
10200 ;; CASE 16F: Can be a defun-close of a function declared
10201 ;; in a statement block, e.g. in Pike or when using gcc
10202 ;; extensions, but watch out for macros followed by
10203 ;; blocks. Let it through to be handled below.
10204 ;; C.f. cases B.3 and 17G.
10205 ((save-excursion
10206 (and (not (c-at-statement-start-p))
10207 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10208 (setq placeholder (point))
10209 (let ((c-recognize-typeless-decls nil))
10210 ;; Turn off recognition of constructs that
10211 ;; lacks a type in this case, since that's more
10212 ;; likely to be a macro followed by a block.
10213 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10214 (back-to-indentation)
10215 (if (/= (point) containing-sexp)
10216 (goto-char placeholder))
10217 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
10219 ;; CASE 16C: If there is an enclosing brace then this is
10220 ;; a block close since defun closes inside declaration
10221 ;; level blocks have been handled above.
10222 (lim
10223 ;; If the block is preceded by a case/switch label on
10224 ;; the same line, we anchor at the first preceding label
10225 ;; at boi. The default handling in c-add-stmt-syntax
10226 ;; really fixes it better, but we do like this to keep
10227 ;; the indentation compatible with version 5.28 and
10228 ;; earlier. C.f. case 17H.
10229 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10230 (eq (c-beginning-of-statement-1 lim) 'label)))
10231 (goto-char placeholder)
10232 (if (looking-at c-label-kwds-regexp)
10233 (c-add-syntax 'block-close (point))
10234 (goto-char containing-sexp)
10235 ;; c-backward-to-block-anchor not necessary here; those
10236 ;; situations are handled in case 16E above.
10237 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
10239 ;; CASE 16D: Only top level defun close left.
10241 (goto-char containing-sexp)
10242 (c-backward-to-decl-anchor lim)
10243 (c-add-stmt-syntax 'defun-close nil nil
10244 (c-most-enclosing-brace paren-state)
10245 paren-state))
10248 ;; CASE 19: line is an expression, not a statement, and is directly
10249 ;; contained by a template delimiter. Most likely, we are in a
10250 ;; template arglist within a statement. This case is based on CASE
10251 ;; 7. At some point in the future, we may wish to create more
10252 ;; syntactic symbols such as `template-intro',
10253 ;; `template-cont-nonempty', etc., and distinguish between them as we
10254 ;; do for `arglist-intro' etc. (2009-12-07).
10255 ((and c-recognize-<>-arglists
10256 (setq containing-< (c-up-list-backward indent-point containing-sexp))
10257 (eq (char-after containing-<) ?\<))
10258 (setq placeholder (c-point 'boi containing-<))
10259 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
10260 ; '<') before indent-point.
10261 (if (>= (point) placeholder)
10262 (progn
10263 (forward-char)
10264 (skip-chars-forward " \t"))
10265 (goto-char placeholder))
10266 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
10267 (c-most-enclosing-brace c-state-cache (point))
10268 paren-state))
10270 ;; CASE 17: Statement or defun catchall.
10272 (goto-char indent-point)
10273 ;; Back up statements until we find one that starts at boi.
10274 (while (let* ((prev-point (point))
10275 (last-step-type (c-beginning-of-statement-1
10276 containing-sexp)))
10277 (if (= (point) prev-point)
10278 (progn
10279 (setq step-type (or step-type last-step-type))
10280 nil)
10281 (setq step-type last-step-type)
10282 (/= (point) (c-point 'boi)))))
10283 (cond
10285 ;; CASE 17B: continued statement
10286 ((and (eq step-type 'same)
10287 (/= (point) indent-point))
10288 (c-add-stmt-syntax 'statement-cont nil nil
10289 containing-sexp paren-state))
10291 ;; CASE 17A: After a case/default label?
10292 ((progn
10293 (while (and (eq step-type 'label)
10294 (not (looking-at c-label-kwds-regexp)))
10295 (setq step-type
10296 (c-beginning-of-statement-1 containing-sexp)))
10297 (eq step-type 'label))
10298 (c-add-stmt-syntax (if (eq char-after-ip ?{)
10299 'statement-case-open
10300 'statement-case-intro)
10301 nil t containing-sexp paren-state))
10303 ;; CASE 17D: any old statement
10304 ((progn
10305 (while (eq step-type 'label)
10306 (setq step-type
10307 (c-beginning-of-statement-1 containing-sexp)))
10308 (eq step-type 'previous))
10309 (c-add-stmt-syntax 'statement nil t
10310 containing-sexp paren-state)
10311 (if (eq char-after-ip ?{)
10312 (c-add-syntax 'block-open)))
10314 ;; CASE 17I: Inside a substatement block.
10315 ((progn
10316 ;; The following tests are all based on containing-sexp.
10317 (goto-char containing-sexp)
10318 ;; From here on we have the next containing sexp in lim.
10319 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
10320 (c-after-conditional))
10321 (c-backward-to-block-anchor lim)
10322 (c-add-stmt-syntax 'statement-block-intro nil t
10323 lim paren-state)
10324 (if (eq char-after-ip ?{)
10325 (c-add-syntax 'block-open)))
10327 ;; CASE 17E: first statement in an in-expression block.
10328 ;; C.f. cases 4, 7B and 16A.
10329 ((setq placeholder (c-looking-at-inexpr-block
10330 (c-safe-position containing-sexp paren-state)
10331 nil))
10332 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10333 'defun-block-intro
10334 'statement-block-intro))
10335 (back-to-indentation)
10336 (if (= containing-sexp (point))
10337 (c-add-syntax tmpsymbol (point))
10338 (goto-char (cdr placeholder))
10339 (back-to-indentation)
10340 (c-add-stmt-syntax tmpsymbol nil t
10341 (c-most-enclosing-brace c-state-cache (point))
10342 paren-state)
10343 (if (/= (point) (cdr placeholder))
10344 (c-add-syntax (car placeholder))))
10345 (if (eq char-after-ip ?{)
10346 (c-add-syntax 'block-open)))
10348 ;; CASE 17F: first statement in an inline, or first
10349 ;; statement in a top-level defun. we can tell this is it
10350 ;; if there are no enclosing braces that haven't been
10351 ;; narrowed out by a class (i.e. don't use bod here).
10352 ((save-excursion
10353 (or (not (setq placeholder (c-most-enclosing-brace
10354 paren-state)))
10355 (and (progn
10356 (goto-char placeholder)
10357 (eq (char-after) ?{))
10358 (c-looking-at-decl-block (c-most-enclosing-brace
10359 paren-state (point))
10360 nil))))
10361 (c-backward-to-decl-anchor lim)
10362 (back-to-indentation)
10363 (c-add-syntax 'defun-block-intro (point)))
10365 ;; CASE 17G: First statement in a function declared inside
10366 ;; a normal block. This can occur in Pike and with
10367 ;; e.g. the gcc extensions, but watch out for macros
10368 ;; followed by blocks. C.f. cases B.3 and 16F.
10369 ((save-excursion
10370 (and (not (c-at-statement-start-p))
10371 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10372 (setq placeholder (point))
10373 (let ((c-recognize-typeless-decls nil))
10374 ;; Turn off recognition of constructs that lacks
10375 ;; a type in this case, since that's more likely
10376 ;; to be a macro followed by a block.
10377 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10378 (back-to-indentation)
10379 (if (/= (point) containing-sexp)
10380 (goto-char placeholder))
10381 (c-add-stmt-syntax 'defun-block-intro nil t
10382 lim paren-state))
10384 ;; CASE 17H: First statement in a block.
10386 ;; If the block is preceded by a case/switch label on the
10387 ;; same line, we anchor at the first preceding label at
10388 ;; boi. The default handling in c-add-stmt-syntax is
10389 ;; really fixes it better, but we do like this to keep the
10390 ;; indentation compatible with version 5.28 and earlier.
10391 ;; C.f. case 16C.
10392 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10393 (eq (c-beginning-of-statement-1 lim) 'label)))
10394 (goto-char placeholder)
10395 (if (looking-at c-label-kwds-regexp)
10396 (c-add-syntax 'statement-block-intro (point))
10397 (goto-char containing-sexp)
10398 ;; c-backward-to-block-anchor not necessary here; those
10399 ;; situations are handled in case 17I above.
10400 (c-add-stmt-syntax 'statement-block-intro nil t
10401 lim paren-state))
10402 (if (eq char-after-ip ?{)
10403 (c-add-syntax 'block-open)))
10407 ;; now we need to look at any modifiers
10408 (goto-char indent-point)
10409 (skip-chars-forward " \t")
10411 ;; are we looking at a comment only line?
10412 (when (and (looking-at c-comment-start-regexp)
10413 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
10414 (c-append-syntax 'comment-intro))
10416 ;; we might want to give additional offset to friends (in C++).
10417 (when (and c-opt-friend-key
10418 (looking-at c-opt-friend-key))
10419 (c-append-syntax 'friend))
10421 ;; Set syntactic-relpos.
10422 (let ((p c-syntactic-context))
10423 (while (and p
10424 (if (integerp (c-langelem-pos (car p)))
10425 (progn
10426 (setq syntactic-relpos (c-langelem-pos (car p)))
10427 nil)
10429 (setq p (cdr p))))
10431 ;; Start of or a continuation of a preprocessor directive?
10432 (if (and macro-start
10433 (eq macro-start (c-point 'boi))
10434 (not (and (c-major-mode-is 'pike-mode)
10435 (eq (char-after (1+ macro-start)) ?\"))))
10436 (c-append-syntax 'cpp-macro)
10437 (when (and c-syntactic-indentation-in-macros macro-start)
10438 (if in-macro-expr
10439 (when (or
10440 (< syntactic-relpos macro-start)
10441 (not (or
10442 (assq 'arglist-intro c-syntactic-context)
10443 (assq 'arglist-cont c-syntactic-context)
10444 (assq 'arglist-cont-nonempty c-syntactic-context)
10445 (assq 'arglist-close c-syntactic-context))))
10446 ;; If inside a cpp expression, i.e. anywhere in a
10447 ;; cpp directive except a #define body, we only let
10448 ;; through the syntactic analysis that is internal
10449 ;; in the expression. That means the arglist
10450 ;; elements, if they are anchored inside the cpp
10451 ;; expression.
10452 (setq c-syntactic-context nil)
10453 (c-add-syntax 'cpp-macro-cont macro-start))
10454 (when (and (eq macro-start syntactic-relpos)
10455 (not (assq 'cpp-define-intro c-syntactic-context))
10456 (save-excursion
10457 (goto-char macro-start)
10458 (or (not (c-forward-to-cpp-define-body))
10459 (<= (point) (c-point 'boi indent-point)))))
10460 ;; Inside a #define body and the syntactic analysis is
10461 ;; anchored on the start of the #define. In this case
10462 ;; we add cpp-define-intro to get the extra
10463 ;; indentation of the #define body.
10464 (c-add-syntax 'cpp-define-intro)))))
10466 ;; return the syntax
10467 c-syntactic-context)))
10470 ;; Indentation calculation.
10472 (defun c-evaluate-offset (offset langelem symbol)
10473 ;; offset can be a number, a function, a variable, a list, or one of
10474 ;; the symbols + or -
10476 ;; This function might do hidden buffer changes.
10477 (let ((res
10478 (cond
10479 ((numberp offset) offset)
10480 ((vectorp offset) offset)
10481 ((null offset) nil)
10483 ((eq offset '+) c-basic-offset)
10484 ((eq offset '-) (- c-basic-offset))
10485 ((eq offset '++) (* 2 c-basic-offset))
10486 ((eq offset '--) (* 2 (- c-basic-offset)))
10487 ((eq offset '*) (/ c-basic-offset 2))
10488 ((eq offset '/) (/ (- c-basic-offset) 2))
10490 ((functionp offset)
10491 (c-evaluate-offset
10492 (funcall offset
10493 (cons (c-langelem-sym langelem)
10494 (c-langelem-pos langelem)))
10495 langelem symbol))
10497 ((listp offset)
10498 (cond
10499 ((eq (car offset) 'quote)
10500 (c-benign-error "The offset %S for %s was mistakenly quoted"
10501 offset symbol)
10502 nil)
10504 ((memq (car offset) '(min max))
10505 (let (res val (method (car offset)))
10506 (setq offset (cdr offset))
10507 (while offset
10508 (setq val (c-evaluate-offset (car offset) langelem symbol))
10509 (cond
10510 ((not val))
10511 ((not res)
10512 (setq res val))
10513 ((integerp val)
10514 (if (vectorp res)
10515 (c-benign-error "\
10516 Error evaluating offset %S for %s: \
10517 Cannot combine absolute offset %S with relative %S in `%s' method"
10518 (car offset) symbol res val method)
10519 (setq res (funcall method res val))))
10521 (if (integerp res)
10522 (c-benign-error "\
10523 Error evaluating offset %S for %s: \
10524 Cannot combine relative offset %S with absolute %S in `%s' method"
10525 (car offset) symbol res val method)
10526 (setq res (vector (funcall method (aref res 0)
10527 (aref val 0)))))))
10528 (setq offset (cdr offset)))
10529 res))
10531 ((eq (car offset) 'add)
10532 (let (res val)
10533 (setq offset (cdr offset))
10534 (while offset
10535 (setq val (c-evaluate-offset (car offset) langelem symbol))
10536 (cond
10537 ((not val))
10538 ((not res)
10539 (setq res val))
10540 ((integerp val)
10541 (if (vectorp res)
10542 (setq res (vector (+ (aref res 0) val)))
10543 (setq res (+ res val))))
10545 (if (vectorp res)
10546 (c-benign-error "\
10547 Error evaluating offset %S for %s: \
10548 Cannot combine absolute offsets %S and %S in `add' method"
10549 (car offset) symbol res val)
10550 (setq res val)))) ; Override.
10551 (setq offset (cdr offset)))
10552 res))
10555 (let (res)
10556 (when (eq (car offset) 'first)
10557 (setq offset (cdr offset)))
10558 (while (and (not res) offset)
10559 (setq res (c-evaluate-offset (car offset) langelem symbol)
10560 offset (cdr offset)))
10561 res))))
10563 ((and (symbolp offset) (boundp offset))
10564 (symbol-value offset))
10567 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10568 nil))))
10570 (if (or (null res) (integerp res)
10571 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10573 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10574 offset symbol res)
10575 nil)))
10577 (defun c-calc-offset (langelem)
10578 ;; Get offset from LANGELEM which is a list beginning with the
10579 ;; syntactic symbol and followed by any analysis data it provides.
10580 ;; That data may be zero or more elements, but if at least one is
10581 ;; given then the first is the anchor position (or nil). The symbol
10582 ;; is matched against `c-offsets-alist' and the offset calculated
10583 ;; from that is returned.
10585 ;; This function might do hidden buffer changes.
10586 (let* ((symbol (c-langelem-sym langelem))
10587 (match (assq symbol c-offsets-alist))
10588 (offset (cdr-safe match)))
10589 (if match
10590 (setq offset (c-evaluate-offset offset langelem symbol))
10591 (if c-strict-syntax-p
10592 (c-benign-error "No offset found for syntactic symbol %s" symbol))
10593 (setq offset 0))
10594 (if (vectorp offset)
10595 offset
10596 (or (and (numberp offset) offset)
10597 (and (symbolp offset) (symbol-value offset))
10601 (defun c-get-offset (langelem)
10602 ;; This is a compatibility wrapper for `c-calc-offset' in case
10603 ;; someone is calling it directly. It takes an old style syntactic
10604 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10605 ;; new list form.
10607 ;; This function might do hidden buffer changes.
10608 (if (c-langelem-pos langelem)
10609 (c-calc-offset (list (c-langelem-sym langelem)
10610 (c-langelem-pos langelem)))
10611 (c-calc-offset langelem)))
10613 (defun c-get-syntactic-indentation (langelems)
10614 ;; Calculate the syntactic indentation from a syntactic description
10615 ;; as returned by `c-guess-syntax'.
10617 ;; Note that topmost-intro always has an anchor position at bol, for
10618 ;; historical reasons. It's often used together with other symbols
10619 ;; that has more sane positions. Since we always use the first
10620 ;; found anchor position, we rely on that these other symbols always
10621 ;; precede topmost-intro in the LANGELEMS list.
10623 ;; This function might do hidden buffer changes.
10624 (let ((indent 0) anchor)
10626 (while langelems
10627 (let* ((c-syntactic-element (car langelems))
10628 (res (c-calc-offset c-syntactic-element)))
10630 (if (vectorp res)
10631 ;; Got an absolute column that overrides any indentation
10632 ;; we've collected so far, but not the relative
10633 ;; indentation we might get for the nested structures
10634 ;; further down the langelems list.
10635 (setq indent (elt res 0)
10636 anchor (point-min)) ; A position at column 0.
10638 ;; Got a relative change of the current calculated
10639 ;; indentation.
10640 (setq indent (+ indent res))
10642 ;; Use the anchor position from the first syntactic
10643 ;; element with one.
10644 (unless anchor
10645 (setq anchor (c-langelem-pos (car langelems)))))
10647 (setq langelems (cdr langelems))))
10649 (if anchor
10650 (+ indent (save-excursion
10651 (goto-char anchor)
10652 (current-column)))
10653 indent)))
10656 (cc-provide 'cc-engine)
10658 ;;; cc-engine.el ends here