lisp/startup.el (argi): Declare as global variable (bug#9275).
[emacs.git] / lisp / progmodes / cc-engine.el
blob0236a2be296b5072dd25eb47a957e94e5e699f42
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985, 1987, 1992-2011 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)))
198 ;; Dynamically bound cache for `c-in-literal'.
199 (defvar c-in-literal-cache t)
202 ;; Basic handling of preprocessor directives.
204 ;; This is a dynamically bound cache used together with
205 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
206 ;; works as long as point doesn't cross a macro boundary.
207 (defvar c-macro-start 'unknown)
209 (defsubst c-query-and-set-macro-start ()
210 (if (symbolp c-macro-start)
211 (setq c-macro-start (save-excursion
212 (c-save-buffer-state ()
213 (and (c-beginning-of-macro)
214 (point)))))
215 c-macro-start))
217 (defsubst c-query-macro-start ()
218 (if (symbolp c-macro-start)
219 (save-excursion
220 (c-save-buffer-state ()
221 (and (c-beginning-of-macro)
222 (point))))
223 c-macro-start))
225 (defun c-beginning-of-macro (&optional lim)
226 "Go to the beginning of a preprocessor directive.
227 Leave point at the beginning of the directive and return t if in one,
228 otherwise return nil and leave point unchanged.
230 Note that this function might do hidden buffer changes. See the
231 comment at the start of cc-engine.el for more info."
232 (when c-opt-cpp-prefix
233 (let ((here (point)))
234 (save-restriction
235 (if lim (narrow-to-region lim (point-max)))
236 (beginning-of-line)
237 (while (eq (char-before (1- (point))) ?\\)
238 (forward-line -1))
239 (back-to-indentation)
240 (if (and (<= (point) here)
241 (looking-at c-opt-cpp-start))
243 (goto-char here)
244 nil)))))
246 (defun c-end-of-macro ()
247 "Go to the end of a preprocessor directive.
248 More accurately, move the point to the end of the closest following
249 line that doesn't end with a line continuation backslash - no check is
250 done that the point is inside a cpp directive to begin with.
252 Note that this function might do hidden buffer changes. See the
253 comment at the start of cc-engine.el for more info."
254 (while (progn
255 (end-of-line)
256 (when (and (eq (char-before) ?\\)
257 (not (eobp)))
258 (forward-char)
259 t))))
261 (defun c-syntactic-end-of-macro ()
262 ;; Go to the end of a CPP directive, or a "safe" pos just before.
264 ;; This is normally the end of the next non-escaped line. A "safe"
265 ;; position is one not within a string or comment. (The EOL on a line
266 ;; comment is NOT "safe").
268 ;; This function must only be called from the beginning of a CPP construct.
270 ;; Note that this function might do hidden buffer changes. See the comment
271 ;; at the start of cc-engine.el for more info.
272 (let* ((here (point))
273 (there (progn (c-end-of-macro) (point)))
274 (s (parse-partial-sexp here there)))
275 (while (and (or (nth 3 s) ; in a string
276 (nth 4 s)) ; in a comment (maybe at end of line comment)
277 (> there here)) ; No infinite loops, please.
278 (setq there (1- (nth 8 s)))
279 (setq s (parse-partial-sexp here there)))
280 (point)))
282 (defun c-forward-over-cpp-define-id ()
283 ;; Assuming point is at the "#" that introduces a preprocessor
284 ;; directive, it's moved forward to the end of the identifier which is
285 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
286 ;; is returned in this case, in all other cases nil is returned and
287 ;; point isn't moved.
289 ;; This function might do hidden buffer changes.
290 (when (and c-opt-cpp-macro-define-id
291 (looking-at c-opt-cpp-macro-define-id))
292 (goto-char (match-end 0))))
294 (defun c-forward-to-cpp-define-body ()
295 ;; Assuming point is at the "#" that introduces a preprocessor
296 ;; directive, it's moved forward to the start of the definition body
297 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
298 ;; specifies). Non-nil is returned in this case, in all other cases
299 ;; nil is returned and point isn't moved.
301 ;; This function might do hidden buffer changes.
302 (when (and c-opt-cpp-macro-define-start
303 (looking-at c-opt-cpp-macro-define-start)
304 (not (= (match-end 0) (c-point 'eol))))
305 (goto-char (match-end 0))))
308 ;;; Basic utility functions.
310 (defun c-syntactic-content (from to paren-level)
311 ;; Return the given region as a string where all syntactic
312 ;; whitespace is removed or, where necessary, replaced with a single
313 ;; space. If PAREN-LEVEL is given then all parens in the region are
314 ;; collapsed to "()", "[]" etc.
316 ;; This function might do hidden buffer changes.
318 (save-excursion
319 (save-restriction
320 (narrow-to-region from to)
321 (goto-char from)
322 (let* ((parts (list nil)) (tail parts) pos in-paren)
324 (while (re-search-forward c-syntactic-ws-start to t)
325 (goto-char (setq pos (match-beginning 0)))
326 (c-forward-syntactic-ws)
327 (if (= (point) pos)
328 (forward-char)
330 (when paren-level
331 (save-excursion
332 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
333 pos (point))))
335 (if (and (> pos from)
336 (< (point) to)
337 (looking-at "\\w\\|\\s_")
338 (save-excursion
339 (goto-char (1- pos))
340 (looking-at "\\w\\|\\s_")))
341 (progn
342 (setcdr tail (list (buffer-substring-no-properties from pos)
343 " "))
344 (setq tail (cddr tail)))
345 (setcdr tail (list (buffer-substring-no-properties from pos)))
346 (setq tail (cdr tail)))
348 (when in-paren
349 (when (= (car (parse-partial-sexp pos to -1)) -1)
350 (setcdr tail (list (buffer-substring-no-properties
351 (1- (point)) (point))))
352 (setq tail (cdr tail))))
354 (setq from (point))))
356 (setcdr tail (list (buffer-substring-no-properties from to)))
357 (apply 'concat (cdr parts))))))
359 (defun c-shift-line-indentation (shift-amt)
360 ;; Shift the indentation of the current line with the specified
361 ;; amount (positive inwards). The buffer is modified only if
362 ;; SHIFT-AMT isn't equal to zero.
363 (let ((pos (- (point-max) (point)))
364 (c-macro-start c-macro-start)
365 tmp-char-inserted)
366 (if (zerop shift-amt)
368 ;; If we're on an empty line inside a macro, we take the point
369 ;; to be at the current indentation and shift it to the
370 ;; appropriate column. This way we don't treat the extra
371 ;; whitespace out to the line continuation as indentation.
372 (when (and (c-query-and-set-macro-start)
373 (looking-at "[ \t]*\\\\$")
374 (save-excursion
375 (skip-chars-backward " \t")
376 (bolp)))
377 (insert ?x)
378 (backward-char)
379 (setq tmp-char-inserted t))
380 (unwind-protect
381 (let ((col (current-indentation)))
382 (delete-region (c-point 'bol) (c-point 'boi))
383 (beginning-of-line)
384 (indent-to (+ col shift-amt)))
385 (when tmp-char-inserted
386 (delete-char 1))))
387 ;; If initial point was within line's indentation and we're not on
388 ;; a line with a line continuation in a macro, position after the
389 ;; indentation. Else stay at same point in text.
390 (if (and (< (point) (c-point 'boi))
391 (not tmp-char-inserted))
392 (back-to-indentation)
393 (if (> (- (point-max) pos) (point))
394 (goto-char (- (point-max) pos))))))
396 (defsubst c-keyword-sym (keyword)
397 ;; Return non-nil if the string KEYWORD is a known keyword. More
398 ;; precisely, the value is the symbol for the keyword in
399 ;; `c-keywords-obarray'.
400 (intern-soft keyword c-keywords-obarray))
402 (defsubst c-keyword-member (keyword-sym lang-constant)
403 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
404 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
405 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
406 ;; nil then the result is nil.
407 (get keyword-sym lang-constant))
409 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
410 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
411 "\"|"
412 "\""))
414 ;; Regexp matching string limit syntax.
415 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
416 "\\s\"\\|\\s|"
417 "\\s\""))
419 ;; Regexp matching WS followed by string limit syntax.
420 (defconst c-ws*-string-limit-regexp
421 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
423 ;; Holds formatted error strings for the few cases where parse errors
424 ;; are reported.
425 (defvar c-parsing-error nil)
426 (make-variable-buffer-local 'c-parsing-error)
428 (defun c-echo-parsing-error (&optional quiet)
429 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
430 (c-benign-error "%s" c-parsing-error))
431 c-parsing-error)
433 ;; Faces given to comments and string literals. This is used in some
434 ;; situations to speed up recognition; it isn't mandatory that font
435 ;; locking is in use. This variable is extended with the face in
436 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
437 (defvar c-literal-faces
438 (append '(font-lock-comment-face font-lock-string-face)
439 (when (facep 'font-lock-comment-delimiter-face)
440 ;; New in Emacs 22.
441 '(font-lock-comment-delimiter-face))))
443 (defsubst c-put-c-type-property (pos value)
444 ;; Put a c-type property with the given value at POS.
445 (c-put-char-property pos 'c-type value))
447 (defun c-clear-c-type-property (from to value)
448 ;; Remove all occurrences of the c-type property that has the given
449 ;; value in the region between FROM and TO. VALUE is assumed to not
450 ;; be nil.
452 ;; Note: This assumes that c-type is put on single chars only; it's
453 ;; very inefficient if matching properties cover large regions.
454 (save-excursion
455 (goto-char from)
456 (while (progn
457 (when (eq (get-text-property (point) 'c-type) value)
458 (c-clear-char-property (point) 'c-type))
459 (goto-char (next-single-property-change (point) 'c-type nil to))
460 (< (point) to)))))
463 ;; Some debug tools to visualize various special positions. This
464 ;; debug code isn't as portable as the rest of CC Mode.
466 (cc-bytecomp-defun overlays-in)
467 (cc-bytecomp-defun overlay-get)
468 (cc-bytecomp-defun overlay-start)
469 (cc-bytecomp-defun overlay-end)
470 (cc-bytecomp-defun delete-overlay)
471 (cc-bytecomp-defun overlay-put)
472 (cc-bytecomp-defun make-overlay)
474 (defun c-debug-add-face (beg end face)
475 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
476 (while overlays
477 (setq overlay (car overlays)
478 overlays (cdr overlays))
479 (when (eq (overlay-get overlay 'face) face)
480 (setq beg (min beg (overlay-start overlay))
481 end (max end (overlay-end overlay)))
482 (delete-overlay overlay)))
483 (overlay-put (make-overlay beg end) 'face face)))
485 (defun c-debug-remove-face (beg end face)
486 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
487 (ol-beg beg) (ol-end end))
488 (while overlays
489 (setq overlay (car overlays)
490 overlays (cdr overlays))
491 (when (eq (overlay-get overlay 'face) face)
492 (setq ol-beg (min ol-beg (overlay-start overlay))
493 ol-end (max ol-end (overlay-end overlay)))
494 (delete-overlay overlay)))
495 (when (< ol-beg beg)
496 (overlay-put (make-overlay ol-beg beg) 'face face))
497 (when (> ol-end end)
498 (overlay-put (make-overlay end ol-end) 'face face))))
501 ;; `c-beginning-of-statement-1' and accompanying stuff.
503 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
504 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
505 ;; better way should be implemented, but this will at least shut up
506 ;; the byte compiler.
507 (defvar c-maybe-labelp)
509 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
511 ;; Macros used internally in c-beginning-of-statement-1 for the
512 ;; automaton actions.
513 (defmacro c-bos-push-state ()
514 '(setq stack (cons (cons state saved-pos)
515 stack)))
516 (defmacro c-bos-pop-state (&optional do-if-done)
517 `(if (setq state (car (car stack))
518 saved-pos (cdr (car stack))
519 stack (cdr stack))
521 ,do-if-done
522 (throw 'loop nil)))
523 (defmacro c-bos-pop-state-and-retry ()
524 '(throw 'loop (setq state (car (car stack))
525 saved-pos (cdr (car stack))
526 ;; Throw nil if stack is empty, else throw non-nil.
527 stack (cdr stack))))
528 (defmacro c-bos-save-pos ()
529 '(setq saved-pos (vector pos tok ptok pptok)))
530 (defmacro c-bos-restore-pos ()
531 '(unless (eq (elt saved-pos 0) start)
532 (setq pos (elt saved-pos 0)
533 tok (elt saved-pos 1)
534 ptok (elt saved-pos 2)
535 pptok (elt saved-pos 3))
536 (goto-char pos)
537 (setq sym nil)))
538 (defmacro c-bos-save-error-info (missing got)
539 `(setq saved-pos (vector pos ,missing ,got)))
540 (defmacro c-bos-report-error ()
541 '(unless noerror
542 (setq c-parsing-error
543 (format "No matching `%s' found for `%s' on line %d"
544 (elt saved-pos 1)
545 (elt saved-pos 2)
546 (1+ (count-lines (point-min)
547 (c-point 'bol (elt saved-pos 0))))))))
549 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
550 noerror comma-delim)
551 "Move to the start of the current statement or declaration, or to
552 the previous one if already at the beginning of one. Only
553 statements/declarations on the same level are considered, i.e. don't
554 move into or out of sexps (not even normal expression parentheses).
556 If point is already at the earliest statement within braces or parens,
557 this function doesn't move back into any whitespace preceding it; it
558 returns 'same in this case.
560 Stop at statement continuation tokens like \"else\", \"catch\",
561 \"finally\" and the \"while\" in \"do ... while\" if the start point
562 is within the continuation. If starting at such a token, move to the
563 corresponding statement start. If at the beginning of a statement,
564 move to the closest containing statement if there is any. This might
565 also stop at a continuation clause.
567 Labels are treated as part of the following statements if
568 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
569 statement start keyword.) Otherwise, each label is treated as a
570 separate statement.
572 Macros are ignored \(i.e. skipped over) unless point is within one, in
573 which case the content of the macro is treated as normal code. Aside
574 from any normal statement starts found in it, stop at the first token
575 of the content in the macro, i.e. the expression of an \"#if\" or the
576 start of the definition in a \"#define\". Also stop at start of
577 macros before leaving them.
579 Return:
580 'label if stopped at a label or \"case...:\" or \"default:\";
581 'same if stopped at the beginning of the current statement;
582 'up if stepped to a containing statement;
583 'previous if stepped to a preceding statement;
584 'beginning if stepped from a statement continuation clause to
585 its start clause; or
586 'macro if stepped to a macro start.
587 Note that 'same and not 'label is returned if stopped at the same
588 label without crossing the colon character.
590 LIM may be given to limit the search. If the search hits the limit,
591 point will be left at the closest following token, or at the start
592 position if that is less ('same is returned in this case).
594 NOERROR turns off error logging to `c-parsing-error'.
596 Normally only ';' and virtual semicolons are considered to delimit
597 statements, but if COMMA-DELIM is non-nil then ',' is treated
598 as a delimiter too.
600 Note that this function might do hidden buffer changes. See the
601 comment at the start of cc-engine.el for more info."
603 ;; The bulk of this function is a pushdown automaton that looks at statement
604 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
605 ;; purpose is to keep track of nested statements, ensuring that such
606 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
607 ;; does with nested braces/brackets/parentheses).
609 ;; Note: The position of a boundary is the following token.
611 ;; Beginning with the current token (the one following point), move back one
612 ;; sexp at a time (where a sexp is, more or less, either a token or the
613 ;; entire contents of a brace/bracket/paren pair). Each time a statement
614 ;; boundary is crossed or a "while"-like token is found, update the state of
615 ;; the PDA. Stop at the beginning of a statement when the stack (holding
616 ;; nested statement info) is empty and the position has been moved.
618 ;; The following variables constitute the PDA:
620 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
621 ;; scanned back over, 'boundary if we've just gone back over a
622 ;; statement boundary, or nil otherwise.
623 ;; state: takes one of the values (nil else else-boundary while
624 ;; while-boundary catch catch-boundary).
625 ;; nil means "no "while"-like token yet scanned".
626 ;; 'else, for example, means "just gone back over an else".
627 ;; 'else-boundary means "just gone back over a statement boundary
628 ;; immediately after having gone back over an else".
629 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
630 ;; of error reporting information.
631 ;; stack: The stack onto which the PDA pushes its state. Each entry
632 ;; consists of a saved value of state and saved-pos. An entry is
633 ;; pushed when we move back over a "continuation" token (e.g. else)
634 ;; and popped when we encounter the corresponding opening token
635 ;; (e.g. if).
638 ;; The following diagram briefly outlines the PDA.
640 ;; Common state:
641 ;; "else": Push state, goto state `else'.
642 ;; "while": Push state, goto state `while'.
643 ;; "catch" or "finally": Push state, goto state `catch'.
644 ;; boundary: Pop state.
645 ;; other: Do nothing special.
647 ;; State `else':
648 ;; boundary: Goto state `else-boundary'.
649 ;; other: Error, pop state, retry token.
651 ;; State `else-boundary':
652 ;; "if": Pop state.
653 ;; boundary: Error, pop state.
654 ;; other: See common state.
656 ;; State `while':
657 ;; boundary: Save position, goto state `while-boundary'.
658 ;; other: Pop state, retry token.
660 ;; State `while-boundary':
661 ;; "do": Pop state.
662 ;; boundary: Restore position if it's not at start, pop state. [*see below]
663 ;; other: See common state.
665 ;; State `catch':
666 ;; boundary: Goto state `catch-boundary'.
667 ;; other: Error, pop state, retry token.
669 ;; State `catch-boundary':
670 ;; "try": Pop state.
671 ;; "catch": Goto state `catch'.
672 ;; boundary: Error, pop state.
673 ;; other: See common state.
675 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
676 ;; searching for a "do" which would have opened a do-while. If we didn't
677 ;; find it, we discard the analysis done since the "while", go back to this
678 ;; token in the buffer and restart the scanning there, this time WITHOUT
679 ;; pushing the 'while state onto the stack.
681 ;; In addition to the above there is some special handling of labels
682 ;; and macros.
684 (let ((case-fold-search nil)
685 (start (point))
686 macro-start
687 (delims (if comma-delim '(?\; ?,) '(?\;)))
688 (c-stmt-delim-chars (if comma-delim
689 c-stmt-delim-chars-with-comma
690 c-stmt-delim-chars))
691 c-in-literal-cache c-maybe-labelp after-case:-pos saved
692 ;; Current position.
694 ;; Position of last stmt boundary character (e.g. ;).
695 boundary-pos
696 ;; The position of the last sexp or bound that follows the
697 ;; first found colon, i.e. the start of the nonlabel part of
698 ;; the statement. It's `start' if a colon is found just after
699 ;; the start.
700 after-labels-pos
701 ;; Like `after-labels-pos', but the first such position inside
702 ;; a label, i.e. the start of the last label before the start
703 ;; of the nonlabel part of the statement.
704 last-label-pos
705 ;; The last position where a label is possible provided the
706 ;; statement started there. It's nil as long as no invalid
707 ;; label content has been found (according to
708 ;; `c-nonlabel-token-key'. It's `start' if no valid label
709 ;; content was found in the label. Note that we might still
710 ;; regard it a label if it starts with `c-label-kwds'.
711 label-good-pos
712 ;; Putative positions of the components of a bitfield declaration,
713 ;; e.g. "int foo : NUM_FOO_BITS ;"
714 bitfield-type-pos bitfield-id-pos bitfield-size-pos
715 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
716 ;; See above.
718 ;; Current state in the automaton. See above.
719 state
720 ;; Current saved positions. See above.
721 saved-pos
722 ;; Stack of conses (state . saved-pos).
723 stack
724 ;; Regexp which matches "for", "if", etc.
725 (cond-key (or c-opt-block-stmt-key
726 "\\<\\>")) ; Matches nothing.
727 ;; Return value.
728 (ret 'same)
729 ;; Positions of the last three sexps or bounds we've stopped at.
730 tok ptok pptok)
732 (save-restriction
733 (if lim (narrow-to-region lim (point-max)))
735 (if (save-excursion
736 (and (c-beginning-of-macro)
737 (/= (point) start)))
738 (setq macro-start (point)))
740 ;; Try to skip back over unary operator characters, to register
741 ;; that we've moved.
742 (while (progn
743 (setq pos (point))
744 (c-backward-syntactic-ws)
745 ;; Protect post-++/-- operators just before a virtual semicolon.
746 (and (not (c-at-vsemi-p))
747 (/= (skip-chars-backward "-+!*&~@`#") 0))))
749 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
750 ;; done. Later on we ignore the boundaries for statements that don't
751 ;; contain any sexp. The only thing that is affected is that the error
752 ;; checking is a little less strict, and we really don't bother.
753 (if (and (memq (char-before) delims)
754 (progn (forward-char -1)
755 (setq saved (point))
756 (c-backward-syntactic-ws)
757 (or (memq (char-before) delims)
758 (memq (char-before) '(?: nil))
759 (eq (char-syntax (char-before)) ?\()
760 (c-at-vsemi-p))))
761 (setq ret 'previous
762 pos saved)
764 ;; Begin at start and not pos to detect macros if we stand
765 ;; directly after the #.
766 (goto-char start)
767 (if (looking-at "\\<\\|\\W")
768 ;; Record this as the first token if not starting inside it.
769 (setq tok start))
772 ;; The following while loop goes back one sexp (balanced parens,
773 ;; etc. with contents, or symbol or suchlike) each iteration. This
774 ;; movement is accomplished with a call to c-backward-sexp approx 170
775 ;; lines below.
777 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
778 ;; 1. On reaching the start of a macro;
779 ;; 2. On having passed a stmt boundary with the PDA stack empty;
780 ;; 3. On reaching the start of an Objective C method def;
781 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
782 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
783 (while
784 (catch 'loop ;; Throw nil to break, non-nil to continue.
785 (cond
786 ;; Are we in a macro, just after the opening #?
787 ((save-excursion
788 (and macro-start ; Always NIL for AWK.
789 (progn (skip-chars-backward " \t")
790 (eq (char-before) ?#))
791 (progn (setq saved (1- (point)))
792 (beginning-of-line)
793 (not (eq (char-before (1- (point))) ?\\)))
794 (looking-at c-opt-cpp-start)
795 (progn (skip-chars-forward " \t")
796 (eq (point) saved))))
797 (goto-char saved)
798 (if (and (c-forward-to-cpp-define-body)
799 (progn (c-forward-syntactic-ws start)
800 (< (point) start)))
801 ;; Stop at the first token in the content of the macro.
802 (setq pos (point)
803 ignore-labels t) ; Avoid the label check on exit.
804 (setq pos saved
805 ret 'macro
806 ignore-labels t))
807 (throw 'loop nil)) ; 1. Start of macro.
809 ;; Do a round through the automaton if we've just passed a
810 ;; statement boundary or passed a "while"-like token.
811 ((or sym
812 (and (looking-at cond-key)
813 (setq sym (intern (match-string 1)))))
815 (when (and (< pos start) (null stack))
816 (throw 'loop nil)) ; 2. Statement boundary.
818 ;; The PDA state handling.
820 ;; Refer to the description of the PDA in the opening
821 ;; comments. In the following OR form, the first leaf
822 ;; attempts to handles one of the specific actions detailed
823 ;; (e.g., finding token "if" whilst in state `else-boundary').
824 ;; We drop through to the second leaf (which handles common
825 ;; state) if no specific handler is found in the first cond.
826 ;; If a parsing error is detected (e.g. an "else" with no
827 ;; preceding "if"), we throw to the enclosing catch.
829 ;; Note that the (eq state 'else) means
830 ;; "we've just passed an else", NOT "we're looking for an
831 ;; else".
832 (or (cond
833 ((eq state 'else)
834 (if (eq sym 'boundary)
835 (setq state 'else-boundary)
836 (c-bos-report-error)
837 (c-bos-pop-state-and-retry)))
839 ((eq state 'else-boundary)
840 (cond ((eq sym 'if)
841 (c-bos-pop-state (setq ret 'beginning)))
842 ((eq sym 'boundary)
843 (c-bos-report-error)
844 (c-bos-pop-state))))
846 ((eq state 'while)
847 (if (and (eq sym 'boundary)
848 ;; Since this can cause backtracking we do a
849 ;; little more careful analysis to avoid it:
850 ;; If there's a label in front of the while
851 ;; it can't be part of a do-while.
852 (not after-labels-pos))
853 (progn (c-bos-save-pos)
854 (setq state 'while-boundary))
855 (c-bos-pop-state-and-retry))) ; Can't be a do-while
857 ((eq state 'while-boundary)
858 (cond ((eq sym 'do)
859 (c-bos-pop-state (setq ret 'beginning)))
860 ((eq sym 'boundary) ; isn't a do-while
861 (c-bos-restore-pos) ; the position of the while
862 (c-bos-pop-state)))) ; no longer searching for do.
864 ((eq state 'catch)
865 (if (eq sym 'boundary)
866 (setq state 'catch-boundary)
867 (c-bos-report-error)
868 (c-bos-pop-state-and-retry)))
870 ((eq state 'catch-boundary)
871 (cond
872 ((eq sym 'try)
873 (c-bos-pop-state (setq ret 'beginning)))
874 ((eq sym 'catch)
875 (setq state 'catch))
876 ((eq sym 'boundary)
877 (c-bos-report-error)
878 (c-bos-pop-state)))))
880 ;; This is state common. We get here when the previous
881 ;; cond statement found no particular state handler.
882 (cond ((eq sym 'boundary)
883 ;; If we have a boundary at the start
884 ;; position we push a frame to go to the
885 ;; previous statement.
886 (if (>= pos start)
887 (c-bos-push-state)
888 (c-bos-pop-state)))
889 ((eq sym 'else)
890 (c-bos-push-state)
891 (c-bos-save-error-info 'if 'else)
892 (setq state 'else))
893 ((eq sym 'while)
894 ;; Is this a real while, or a do-while?
895 ;; The next `when' triggers unless we are SURE that
896 ;; the `while' is not the tailend of a `do-while'.
897 (when (or (not pptok)
898 (memq (char-after pptok) delims)
899 ;; The following kludge is to prevent
900 ;; infinite recursion when called from
901 ;; c-awk-after-if-for-while-condition-p,
902 ;; or the like.
903 (and (eq (point) start)
904 (c-vsemi-status-unknown-p))
905 (c-at-vsemi-p pptok))
906 ;; Since this can cause backtracking we do a
907 ;; little more careful analysis to avoid it: If
908 ;; the while isn't followed by a (possibly
909 ;; virtual) semicolon it can't be a do-while.
910 (c-bos-push-state)
911 (setq state 'while)))
912 ((memq sym '(catch finally))
913 (c-bos-push-state)
914 (c-bos-save-error-info 'try sym)
915 (setq state 'catch))))
917 (when c-maybe-labelp
918 ;; We're either past a statement boundary or at the
919 ;; start of a statement, so throw away any label data
920 ;; for the previous one.
921 (setq after-labels-pos nil
922 last-label-pos nil
923 c-maybe-labelp nil))))
925 ;; Step to the previous sexp, but not if we crossed a
926 ;; boundary, since that doesn't consume an sexp.
927 (if (eq sym 'boundary)
928 (setq ret 'previous)
930 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
931 ;; BACKWARDS THROUGH THE SOURCE.
933 (c-backward-syntactic-ws)
934 (let ((before-sws-pos (point))
935 ;; The end position of the area to search for statement
936 ;; barriers in this round.
937 (maybe-after-boundary-pos pos))
939 ;; Go back over exactly one logical sexp, taking proper
940 ;; account of macros and escaped EOLs.
941 (while
942 (progn
943 (unless (c-safe (c-backward-sexp) t)
944 ;; Give up if we hit an unbalanced block. Since the
945 ;; stack won't be empty the code below will report a
946 ;; suitable error.
947 (throw 'loop nil))
948 (cond
949 ;; Have we moved into a macro?
950 ((and (not macro-start)
951 (c-beginning-of-macro))
952 ;; Have we crossed a statement boundary? If not,
953 ;; keep going back until we find one or a "real" sexp.
954 (and
955 (save-excursion
956 (c-end-of-macro)
957 (not (c-crosses-statement-barrier-p
958 (point) maybe-after-boundary-pos)))
959 (setq maybe-after-boundary-pos (point))))
960 ;; Have we just gone back over an escaped NL? This
961 ;; doesn't count as a sexp.
962 ((looking-at "\\\\$")))))
964 ;; Have we crossed a statement boundary?
965 (setq boundary-pos
966 (cond
967 ;; Are we at a macro beginning?
968 ((and (not macro-start)
969 c-opt-cpp-prefix
970 (looking-at c-opt-cpp-prefix))
971 (save-excursion
972 (c-end-of-macro)
973 (c-crosses-statement-barrier-p
974 (point) maybe-after-boundary-pos)))
975 ;; Just gone back over a brace block?
976 ((and
977 (eq (char-after) ?{)
978 (not (c-looking-at-inexpr-block lim nil t)))
979 (save-excursion
980 (c-forward-sexp) (point)))
981 ;; Just gone back over some paren block?
982 ((looking-at "\\s\(")
983 (save-excursion
984 (goto-char (1+ (c-down-list-backward
985 before-sws-pos)))
986 (c-crosses-statement-barrier-p
987 (point) maybe-after-boundary-pos)))
988 ;; Just gone back over an ordinary symbol of some sort?
989 (t (c-crosses-statement-barrier-p
990 (point) maybe-after-boundary-pos))))
992 (when boundary-pos
993 (setq pptok ptok
994 ptok tok
995 tok boundary-pos
996 sym 'boundary)
997 ;; Like a C "continue". Analyze the next sexp.
998 (throw 'loop t))))
1000 ;; ObjC method def?
1001 (when (and c-opt-method-key
1002 (setq saved (c-in-method-def-p)))
1003 (setq pos saved
1004 ignore-labels t) ; Avoid the label check on exit.
1005 (throw 'loop nil)) ; 3. ObjC method def.
1007 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1008 (if c-has-bitfields
1009 (cond
1010 ;; The : <size> and <id> fields?
1011 ((and (numberp c-maybe-labelp)
1012 (not bitfield-size-pos)
1013 (save-excursion
1014 (goto-char (or tok start))
1015 (not (looking-at c-keywords-regexp)))
1016 (not (looking-at c-keywords-regexp))
1017 (not (c-punctuation-in (point) c-maybe-labelp)))
1018 (setq bitfield-size-pos (or tok start)
1019 bitfield-id-pos (point)))
1020 ;; The <type> field?
1021 ((and bitfield-id-pos
1022 (not bitfield-type-pos))
1023 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1024 (not (looking-at c-not-primitive-type-keywords-regexp))
1025 (not (c-punctuation-in (point) tok)))
1026 (setq bitfield-type-pos (point))
1027 (setq bitfield-size-pos nil
1028 bitfield-id-pos nil)))))
1030 ;; Handle labels.
1031 (unless (eq ignore-labels t)
1032 (when (numberp c-maybe-labelp)
1033 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1034 ;; might be in a label now. Have we got a real label
1035 ;; (including a case label) or something like C++'s "public:"?
1036 ;; A case label might use an expression rather than a token.
1037 (setq after-case:-pos (or tok start))
1038 (if (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1039 (setq c-maybe-labelp nil)
1040 (if after-labels-pos ; Have we already encountered a label?
1041 (if (not last-label-pos)
1042 (setq last-label-pos (or tok start)))
1043 (setq after-labels-pos (or tok start)))
1044 (setq c-maybe-labelp t
1045 label-good-pos nil))) ; bogus "label"
1047 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1048 ; been found.
1049 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1050 ;; We're in a potential label and it's the first
1051 ;; time we've found something that isn't allowed in
1052 ;; one.
1053 (setq label-good-pos (or tok start))))
1055 ;; We've moved back by a sexp, so update the token positions.
1056 (setq sym nil
1057 pptok ptok
1058 ptok tok
1059 tok (point)
1060 pos tok) ; always non-nil
1061 ) ; end of (catch loop ....)
1062 ) ; end of sexp-at-a-time (while ....)
1064 ;; If the stack isn't empty there might be errors to report.
1065 (while stack
1066 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1067 (c-bos-report-error))
1068 (setq saved-pos (cdr (car stack))
1069 stack (cdr stack)))
1071 (when (and (eq ret 'same)
1072 (not (memq sym '(boundary ignore nil))))
1073 ;; Need to investigate closer whether we've crossed
1074 ;; between a substatement and its containing statement.
1075 (if (setq saved (if (looking-at c-block-stmt-1-key)
1076 ptok
1077 pptok))
1078 (cond ((> start saved) (setq pos saved))
1079 ((= start saved) (setq ret 'up)))))
1081 (when (and (not ignore-labels)
1082 (eq c-maybe-labelp t)
1083 (not (eq ret 'beginning))
1084 after-labels-pos
1085 (not bitfield-type-pos) ; Bitfields take precedence over labels.
1086 (or (not label-good-pos)
1087 (<= label-good-pos pos)
1088 (progn
1089 (goto-char (if (and last-label-pos
1090 (< last-label-pos start))
1091 last-label-pos
1092 pos))
1093 (looking-at c-label-kwds-regexp))))
1094 ;; We're in a label. Maybe we should step to the statement
1095 ;; after it.
1096 (if (< after-labels-pos start)
1097 (setq pos after-labels-pos)
1098 (setq ret 'label)
1099 (if (and last-label-pos (< last-label-pos start))
1100 ;; Might have jumped over several labels. Go to the last one.
1101 (setq pos last-label-pos)))))
1103 ;; Have we got "case <expression>:"?
1104 (goto-char pos)
1105 (when (and after-case:-pos
1106 (not (eq ret 'beginning))
1107 (looking-at c-case-kwds-regexp))
1108 (if (< after-case:-pos start)
1109 (setq pos after-case:-pos))
1110 (if (eq ret 'same)
1111 (setq ret 'label)))
1113 ;; Skip over the unary operators that can start the statement.
1114 (while (progn
1115 (c-backward-syntactic-ws)
1116 ;; protect AWK post-inc/decrement operators, etc.
1117 (and (not (c-at-vsemi-p (point)))
1118 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1119 (setq pos (point)))
1120 (goto-char pos)
1121 ret)))
1123 (defun c-punctuation-in (from to)
1124 "Return non-nil if there is a non-comment non-macro punctuation character
1125 between FROM and TO. FROM must not be in a string or comment. The returned
1126 value is the position of the first such character."
1127 (save-excursion
1128 (goto-char from)
1129 (let ((pos (point)))
1130 (while (progn (skip-chars-forward c-symbol-chars to)
1131 (c-forward-syntactic-ws to)
1132 (> (point) pos))
1133 (setq pos (point))))
1134 (and (< (point) to) (point))))
1136 (defun c-crosses-statement-barrier-p (from to)
1137 "Return non-nil if buffer positions FROM to TO cross one or more
1138 statement or declaration boundaries. The returned value is actually
1139 the position of the earliest boundary char. FROM must not be within
1140 a string or comment.
1142 The variable `c-maybe-labelp' is set to the position of the first `:' that
1143 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1144 single `?' is found, then `c-maybe-labelp' is cleared.
1146 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1147 regarded as having a \"virtual semicolon\" immediately after the last token on
1148 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1150 Note that this function might do hidden buffer changes. See the
1151 comment at the start of cc-engine.el for more info."
1152 (let ((skip-chars c-stmt-delim-chars)
1153 lit-range)
1154 (save-excursion
1155 (catch 'done
1156 (goto-char from)
1157 (while (progn (skip-chars-forward skip-chars to)
1158 (< (point) to))
1159 (cond
1160 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1161 (goto-char (cdr lit-range)))
1162 ((eq (char-after) ?:)
1163 (forward-char)
1164 (if (and (eq (char-after) ?:)
1165 (< (point) to))
1166 ;; Ignore scope operators.
1167 (forward-char)
1168 (setq c-maybe-labelp (1- (point)))))
1169 ((eq (char-after) ??)
1170 ;; A question mark. Can't be a label, so stop
1171 ;; looking for more : and ?.
1172 (setq c-maybe-labelp nil
1173 skip-chars (substring c-stmt-delim-chars 0 -2)))
1174 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1175 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1176 (backward-char))
1177 (skip-chars-backward " \t" from)
1178 (if (c-at-vsemi-p)
1179 (throw 'done (point))
1180 (forward-line)))
1181 (t (throw 'done (point)))))
1182 ;; In trailing space after an as yet undetected virtual semicolon?
1183 (c-backward-syntactic-ws from)
1184 (if (and (< (point) to)
1185 (c-at-vsemi-p))
1186 (point)
1187 nil)))))
1189 (defun c-at-statement-start-p ()
1190 "Return non-nil if the point is at the first token in a statement
1191 or somewhere in the syntactic whitespace before it.
1193 A \"statement\" here is not restricted to those inside code blocks.
1194 Any kind of declaration-like construct that occur outside function
1195 bodies is also considered a \"statement\".
1197 Note that this function might do hidden buffer changes. See the
1198 comment at the start of cc-engine.el for more info."
1200 (save-excursion
1201 (let ((end (point))
1202 c-maybe-labelp)
1203 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1204 (or (bobp)
1205 (eq (char-before) ?})
1206 (and (eq (char-before) ?{)
1207 (not (and c-special-brace-lists
1208 (progn (backward-char)
1209 (c-looking-at-special-brace-list)))))
1210 (c-crosses-statement-barrier-p (point) end)))))
1212 (defun c-at-expression-start-p ()
1213 "Return non-nil if the point is at the first token in an expression or
1214 statement, or somewhere in the syntactic whitespace before it.
1216 An \"expression\" here is a bit different from the normal language
1217 grammar sense: It's any sequence of expression tokens except commas,
1218 unless they are enclosed inside parentheses of some kind. Also, an
1219 expression never continues past an enclosing parenthesis, but it might
1220 contain parenthesis pairs of any sort except braces.
1222 Since expressions never cross statement boundaries, this function also
1223 recognizes statement beginnings, just like `c-at-statement-start-p'.
1225 Note that this function might do hidden buffer changes. See the
1226 comment at the start of cc-engine.el for more info."
1228 (save-excursion
1229 (let ((end (point))
1230 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1231 c-maybe-labelp)
1232 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1233 (or (bobp)
1234 (memq (char-before) '(?{ ?}))
1235 (save-excursion (backward-char)
1236 (looking-at "\\s("))
1237 (c-crosses-statement-barrier-p (point) end)))))
1240 ;; A set of functions that covers various idiosyncrasies in
1241 ;; implementations of `forward-comment'.
1243 ;; Note: Some emacsen considers incorrectly that any line comment
1244 ;; ending with a backslash continues to the next line. I can't think
1245 ;; of any way to work around that in a reliable way without changing
1246 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1247 ;; changing the syntax for backslash doesn't work since we must treat
1248 ;; escapes in string literals correctly.)
1250 (defun c-forward-single-comment ()
1251 "Move forward past whitespace and the closest following comment, if any.
1252 Return t if a comment was found, nil otherwise. In either case, the
1253 point is moved past the following whitespace. Line continuations,
1254 i.e. a backslashes followed by line breaks, are treated as whitespace.
1255 The line breaks that end line comments are considered to be the
1256 comment enders, so the point will be put on the beginning of the next
1257 line if it moved past a line comment.
1259 This function does not do any hidden buffer changes."
1261 (let ((start (point)))
1262 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1263 (goto-char (match-end 0)))
1265 (when (forward-comment 1)
1266 (if (eobp)
1267 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1268 ;; forwards at eob.
1271 ;; Emacs includes the ending newline in a b-style (c++)
1272 ;; comment, but XEmacs doesn't. We depend on the Emacs
1273 ;; behavior (which also is symmetric).
1274 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1275 (condition-case nil (forward-char 1)))
1277 t))))
1279 (defsubst c-forward-comments ()
1280 "Move forward past all following whitespace and comments.
1281 Line continuations, i.e. a backslashes followed by line breaks, are
1282 treated as whitespace.
1284 Note that this function might do hidden buffer changes. See the
1285 comment at the start of cc-engine.el for more info."
1287 (while (or
1288 ;; If forward-comment in at least XEmacs 21 is given a large
1289 ;; positive value, it'll loop all the way through if it hits
1290 ;; eob.
1291 (and (forward-comment 5)
1292 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1293 ;; forwards at eob.
1294 (not (eobp)))
1296 (when (looking-at "\\\\[\n\r]")
1297 (forward-char 2)
1298 t))))
1300 (defun c-backward-single-comment ()
1301 "Move backward past whitespace and the closest preceding comment, if any.
1302 Return t if a comment was found, nil otherwise. In either case, the
1303 point is moved past the preceding whitespace. Line continuations,
1304 i.e. a backslashes followed by line breaks, are treated as whitespace.
1305 The line breaks that end line comments are considered to be the
1306 comment enders, so the point cannot be at the end of the same line to
1307 move over a line comment.
1309 This function does not do any hidden buffer changes."
1311 (let ((start (point)))
1312 ;; When we got newline terminated comments, forward-comment in all
1313 ;; supported emacsen so far will stop at eol of each line not
1314 ;; ending with a comment when moving backwards. This corrects for
1315 ;; that, and at the same time handles line continuations.
1316 (while (progn
1317 (skip-chars-backward " \t\n\r\f\v")
1318 (and (looking-at "[\n\r]")
1319 (eq (char-before) ?\\)))
1320 (backward-char))
1322 (if (bobp)
1323 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1324 ;; backwards at bob.
1327 ;; Leave point after the closest following newline if we've
1328 ;; backed up over any above, since forward-comment won't move
1329 ;; backward over a line comment if point is at the end of the
1330 ;; same line.
1331 (re-search-forward "\\=\\s *[\n\r]" start t)
1333 (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
1334 (if (eolp)
1335 ;; If forward-comment above succeeded and we're at eol
1336 ;; then the newline we moved over above didn't end a
1337 ;; line comment, so we give it another go.
1338 (let (open-paren-in-column-0-is-defun-start)
1339 (forward-comment -1))
1342 ;; Emacs <= 20 and XEmacs move back over the closer of a
1343 ;; block comment that lacks an opener.
1344 (if (looking-at "\\*/")
1345 (progn (forward-char 2) nil)
1346 t)))))
1348 (defsubst c-backward-comments ()
1349 "Move backward past all preceding whitespace and comments.
1350 Line continuations, i.e. a backslashes followed by line breaks, are
1351 treated as whitespace. The line breaks that end line comments are
1352 considered to be the comment enders, so the point cannot be at the end
1353 of the same line to move over a line comment. Unlike
1354 c-backward-syntactic-ws, this function doesn't move back over
1355 preprocessor directives.
1357 Note that this function might do hidden buffer changes. See the
1358 comment at the start of cc-engine.el for more info."
1360 (let ((start (point)))
1361 (while (and
1362 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1363 ;; return t when moving backwards at bob.
1364 (not (bobp))
1366 (if (let (open-paren-in-column-0-is-defun-start)
1367 (forward-comment -1))
1368 (if (looking-at "\\*/")
1369 ;; Emacs <= 20 and XEmacs move back over the
1370 ;; closer of a block comment that lacks an opener.
1371 (progn (forward-char 2) nil)
1374 ;; XEmacs treats line continuations as whitespace but
1375 ;; only in the backward direction, which seems a bit
1376 ;; odd. Anyway, this is necessary for Emacs.
1377 (when (and (looking-at "[\n\r]")
1378 (eq (char-before) ?\\)
1379 (< (point) start))
1380 (backward-char)
1381 t))))))
1384 ;; Tools for skipping over syntactic whitespace.
1386 ;; The following functions use text properties to cache searches over
1387 ;; large regions of syntactic whitespace. It works as follows:
1389 ;; o If a syntactic whitespace region contains anything but simple
1390 ;; whitespace (i.e. space, tab and line breaks), the text property
1391 ;; `c-in-sws' is put over it. At places where we have stopped
1392 ;; within that region there's also a `c-is-sws' text property.
1393 ;; That since there typically are nested whitespace inside that
1394 ;; must be handled separately, e.g. whitespace inside a comment or
1395 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1396 ;; to jump to another point with that property within the same
1397 ;; `c-in-sws' region. It can be likened to a ladder where
1398 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1400 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1401 ;; a "rung position" and also maybe on the first following char.
1402 ;; As many characters as can be conveniently found in this range
1403 ;; are marked, but no assumption can be made that the whole range
1404 ;; is marked (it could be clobbered by later changes, for
1405 ;; instance).
1407 ;; Note that some part of the beginning of a sequence of simple
1408 ;; whitespace might be part of the end of a preceding line comment
1409 ;; or cpp directive and must not be considered part of the "rung".
1410 ;; Such whitespace is some amount of horizontal whitespace followed
1411 ;; by a newline. In the case of cpp directives it could also be
1412 ;; two newlines with horizontal whitespace between them.
1414 ;; The reason to include the first following char is to cope with
1415 ;; "rung positions" that doesn't have any ordinary whitespace. If
1416 ;; `c-is-sws' is put on a token character it does not have
1417 ;; `c-in-sws' set simultaneously. That's the only case when that
1418 ;; can occur, and the reason for not extending the `c-in-sws'
1419 ;; region to cover it is that the `c-in-sws' region could then be
1420 ;; accidentally merged with a following one if the token is only
1421 ;; one character long.
1423 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1424 ;; removed in the changed region. If the change was inside
1425 ;; syntactic whitespace that means that the "ladder" is broken, but
1426 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1427 ;; parts on either side and use an ordinary search only to "repair"
1428 ;; the gap.
1430 ;; Special care needs to be taken if a region is removed: If there
1431 ;; are `c-in-sws' on both sides of it which do not connect inside
1432 ;; the region then they can't be joined. If e.g. a marked macro is
1433 ;; broken, syntactic whitespace inside the new text might be
1434 ;; marked. If those marks would become connected with the old
1435 ;; `c-in-sws' range around the macro then we could get a ladder
1436 ;; with one end outside the macro and the other at some whitespace
1437 ;; within it.
1439 ;; The main motivation for this system is to increase the speed in
1440 ;; skipping over the large whitespace regions that can occur at the
1441 ;; top level in e.g. header files that contain a lot of comments and
1442 ;; cpp directives. For small comments inside code it's probably
1443 ;; slower than using `forward-comment' straightforwardly, but speed is
1444 ;; not a significant factor there anyway.
1446 ; (defface c-debug-is-sws-face
1447 ; '((t (:background "GreenYellow")))
1448 ; "Debug face to mark the `c-is-sws' property.")
1449 ; (defface c-debug-in-sws-face
1450 ; '((t (:underline t)))
1451 ; "Debug face to mark the `c-in-sws' property.")
1453 ; (defun c-debug-put-sws-faces ()
1454 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1455 ; ;; properties in the buffer.
1456 ; (interactive)
1457 ; (save-excursion
1458 ; (c-save-buffer-state (in-face)
1459 ; (goto-char (point-min))
1460 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1461 ; (point)))
1462 ; (while (progn
1463 ; (goto-char (next-single-property-change
1464 ; (point) 'c-is-sws nil (point-max)))
1465 ; (if in-face
1466 ; (progn
1467 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1468 ; (setq in-face nil))
1469 ; (setq in-face (point)))
1470 ; (not (eobp))))
1471 ; (goto-char (point-min))
1472 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1473 ; (point)))
1474 ; (while (progn
1475 ; (goto-char (next-single-property-change
1476 ; (point) 'c-in-sws nil (point-max)))
1477 ; (if in-face
1478 ; (progn
1479 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1480 ; (setq in-face nil))
1481 ; (setq in-face (point)))
1482 ; (not (eobp)))))))
1484 (defmacro c-debug-sws-msg (&rest args)
1485 ;;`(message ,@args)
1488 (defmacro c-put-is-sws (beg end)
1489 ;; This macro does a hidden buffer change.
1490 `(let ((beg ,beg) (end ,end))
1491 (put-text-property beg end 'c-is-sws t)
1492 ,@(when (facep 'c-debug-is-sws-face)
1493 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1495 (defmacro c-put-in-sws (beg end)
1496 ;; This macro does a hidden buffer change.
1497 `(let ((beg ,beg) (end ,end))
1498 (put-text-property beg end 'c-in-sws t)
1499 ,@(when (facep 'c-debug-is-sws-face)
1500 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1502 (defmacro c-remove-is-sws (beg end)
1503 ;; This macro does a hidden buffer change.
1504 `(let ((beg ,beg) (end ,end))
1505 (remove-text-properties beg end '(c-is-sws nil))
1506 ,@(when (facep 'c-debug-is-sws-face)
1507 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1509 (defmacro c-remove-in-sws (beg end)
1510 ;; This macro does a hidden buffer change.
1511 `(let ((beg ,beg) (end ,end))
1512 (remove-text-properties beg end '(c-in-sws nil))
1513 ,@(when (facep 'c-debug-is-sws-face)
1514 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1516 (defmacro c-remove-is-and-in-sws (beg end)
1517 ;; This macro does a hidden buffer change.
1518 `(let ((beg ,beg) (end ,end))
1519 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1520 ,@(when (facep 'c-debug-is-sws-face)
1521 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1522 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1524 (defsubst c-invalidate-sws-region-after (beg end)
1525 ;; Called from `after-change-functions'. Note that if
1526 ;; `c-forward-sws' or `c-backward-sws' are used outside
1527 ;; `c-save-buffer-state' or similar then this will remove the cache
1528 ;; properties right after they're added.
1530 ;; This function does hidden buffer changes.
1532 (save-excursion
1533 ;; Adjust the end to remove the properties in any following simple
1534 ;; ws up to and including the next line break, if there is any
1535 ;; after the changed region. This is necessary e.g. when a rung
1536 ;; marked empty line is converted to a line comment by inserting
1537 ;; "//" before the line break. In that case the line break would
1538 ;; keep the rung mark which could make a later `c-backward-sws'
1539 ;; move into the line comment instead of over it.
1540 (goto-char end)
1541 (skip-chars-forward " \t\f\v")
1542 (when (and (eolp) (not (eobp)))
1543 (setq end (1+ (point)))))
1545 (when (and (= beg end)
1546 (get-text-property beg 'c-in-sws)
1547 (> beg (point-min))
1548 (get-text-property (1- beg) 'c-in-sws))
1549 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1550 ;; safe to keep a range that was continuous before the change. E.g:
1552 ;; #define foo
1553 ;; \
1554 ;; bar
1556 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1557 ;; after "foo" is removed then "bar" will become part of the cpp
1558 ;; directive instead of a syntactically relevant token. In that
1559 ;; case there's no longer syntactic ws from "#" to "b".
1560 (setq beg (1- beg)))
1562 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1563 (c-remove-is-and-in-sws beg end))
1565 (defun c-forward-sws ()
1566 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1568 ;; This function might do hidden buffer changes.
1570 (let (;; `rung-pos' is set to a position as early as possible in the
1571 ;; unmarked part of the simple ws region.
1572 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1573 rung-is-marked next-rung-is-marked simple-ws-end
1574 ;; `safe-start' is set when it's safe to cache the start position.
1575 ;; It's not set if we've initially skipped over comments and line
1576 ;; continuations since we might have gone out through the end of a
1577 ;; macro then. This provision makes `c-forward-sws' not populate the
1578 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1579 ;; more common.
1580 safe-start)
1582 ;; Skip simple ws and do a quick check on the following character to see
1583 ;; if it's anything that can't start syntactic ws, so we can bail out
1584 ;; early in the majority of cases when there just are a few ws chars.
1585 (skip-chars-forward " \t\n\r\f\v")
1586 (when (looking-at c-syntactic-ws-start)
1588 (setq rung-end-pos (min (1+ (point)) (point-max)))
1589 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1590 'c-is-sws t))
1591 ;; Find the last rung position to avoid setting properties in all
1592 ;; the cases when the marked rung is complete.
1593 ;; (`next-single-property-change' is certain to move at least one
1594 ;; step forward.)
1595 (setq rung-pos (1- (next-single-property-change
1596 rung-is-marked 'c-is-sws nil rung-end-pos)))
1597 ;; Got no marked rung here. Since the simple ws might have started
1598 ;; inside a line comment or cpp directive we must set `rung-pos' as
1599 ;; high as possible.
1600 (setq rung-pos (point)))
1602 (while
1603 (progn
1604 (while
1605 (when (and rung-is-marked
1606 (get-text-property (point) 'c-in-sws))
1608 ;; The following search is the main reason that `c-in-sws'
1609 ;; and `c-is-sws' aren't combined to one property.
1610 (goto-char (next-single-property-change
1611 (point) 'c-in-sws nil (point-max)))
1612 (unless (get-text-property (point) 'c-is-sws)
1613 ;; If the `c-in-sws' region extended past the last
1614 ;; `c-is-sws' char we have to go back a bit.
1615 (or (get-text-property (1- (point)) 'c-is-sws)
1616 (goto-char (previous-single-property-change
1617 (point) 'c-is-sws)))
1618 (backward-char))
1620 (c-debug-sws-msg
1621 "c-forward-sws cached move %s -> %s (max %s)"
1622 rung-pos (point) (point-max))
1624 (setq rung-pos (point))
1625 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1626 (not (eobp))))
1628 ;; We'll loop here if there is simple ws after the last rung.
1629 ;; That means that there's been some change in it and it's
1630 ;; possible that we've stepped into another ladder, so extend
1631 ;; the previous one to join with it if there is one, and try to
1632 ;; use the cache again.
1633 (c-debug-sws-msg
1634 "c-forward-sws extending rung with [%s..%s] (max %s)"
1635 (1+ rung-pos) (1+ (point)) (point-max))
1636 (unless (get-text-property (point) 'c-is-sws)
1637 ;; Remove any `c-in-sws' property from the last char of
1638 ;; the rung before we mark it with `c-is-sws', so that we
1639 ;; won't connect with the remains of a broken "ladder".
1640 (c-remove-in-sws (point) (1+ (point))))
1641 (c-put-is-sws (1+ rung-pos)
1642 (1+ (point)))
1643 (c-put-in-sws rung-pos
1644 (setq rung-pos (point)
1645 last-put-in-sws-pos rung-pos)))
1647 (setq simple-ws-end (point))
1648 (c-forward-comments)
1650 (cond
1651 ((/= (point) simple-ws-end)
1652 ;; Skipped over comments. Don't cache at eob in case the buffer
1653 ;; is narrowed.
1654 (not (eobp)))
1656 ((save-excursion
1657 (and c-opt-cpp-prefix
1658 (looking-at c-opt-cpp-start)
1659 (progn (skip-chars-backward " \t")
1660 (bolp))
1661 (or (bobp)
1662 (progn (backward-char)
1663 (not (eq (char-before) ?\\))))))
1664 ;; Skip a preprocessor directive.
1665 (end-of-line)
1666 (while (and (eq (char-before) ?\\)
1667 (= (forward-line 1) 0))
1668 (end-of-line))
1669 (forward-line 1)
1670 (setq safe-start t)
1671 ;; Don't cache at eob in case the buffer is narrowed.
1672 (not (eobp)))))
1674 ;; We've searched over a piece of non-white syntactic ws. See if this
1675 ;; can be cached.
1676 (setq next-rung-pos (point))
1677 (skip-chars-forward " \t\n\r\f\v")
1678 (setq rung-end-pos (min (1+ (point)) (point-max)))
1680 (if (or
1681 ;; Cache if we haven't skipped comments only, and if we started
1682 ;; either from a marked rung or from a completely uncached
1683 ;; position.
1684 (and safe-start
1685 (or rung-is-marked
1686 (not (get-text-property simple-ws-end 'c-in-sws))))
1688 ;; See if there's a marked rung in the encountered simple ws. If
1689 ;; so then we can cache, unless `safe-start' is nil. Even then
1690 ;; we need to do this to check if the cache can be used for the
1691 ;; next step.
1692 (and (setq next-rung-is-marked
1693 (text-property-any next-rung-pos rung-end-pos
1694 'c-is-sws t))
1695 safe-start))
1697 (progn
1698 (c-debug-sws-msg
1699 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1700 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1701 (point-max))
1703 ;; Remove the properties for any nested ws that might be cached.
1704 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1705 ;; anyway.
1706 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1707 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1708 (c-put-is-sws rung-pos
1709 (1+ simple-ws-end))
1710 (setq rung-is-marked t))
1711 (c-put-in-sws rung-pos
1712 (setq rung-pos (point)
1713 last-put-in-sws-pos rung-pos))
1714 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1715 ;; Remove any `c-in-sws' property from the last char of
1716 ;; the rung before we mark it with `c-is-sws', so that we
1717 ;; won't connect with the remains of a broken "ladder".
1718 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1719 (c-put-is-sws next-rung-pos
1720 rung-end-pos))
1722 (c-debug-sws-msg
1723 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1724 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1725 (point-max))
1727 ;; Set `rung-pos' for the next rung. It's the same thing here as
1728 ;; initially, except that the rung position is set as early as
1729 ;; possible since we can't be in the ending ws of a line comment or
1730 ;; cpp directive now.
1731 (if (setq rung-is-marked next-rung-is-marked)
1732 (setq rung-pos (1- (next-single-property-change
1733 rung-is-marked 'c-is-sws nil rung-end-pos)))
1734 (setq rung-pos next-rung-pos))
1735 (setq safe-start t)))
1737 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1738 ;; another one after the point (which might occur when editing inside a
1739 ;; comment or macro).
1740 (when (eq last-put-in-sws-pos (point))
1741 (cond ((< last-put-in-sws-pos (point-max))
1742 (c-debug-sws-msg
1743 "c-forward-sws clearing at %s for cache separation"
1744 last-put-in-sws-pos)
1745 (c-remove-in-sws last-put-in-sws-pos
1746 (1+ last-put-in-sws-pos)))
1748 ;; If at eob we have to clear the last character before the end
1749 ;; instead since the buffer might be narrowed and there might
1750 ;; be a `c-in-sws' after (point-max). In this case it's
1751 ;; necessary to clear both properties.
1752 (c-debug-sws-msg
1753 "c-forward-sws clearing thoroughly at %s for cache separation"
1754 (1- last-put-in-sws-pos))
1755 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1756 last-put-in-sws-pos))))
1759 (defun c-backward-sws ()
1760 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1762 ;; This function might do hidden buffer changes.
1764 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1765 ;; part of the simple ws region.
1766 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1767 rung-is-marked simple-ws-beg cmt-skip-pos)
1769 ;; Skip simple horizontal ws and do a quick check on the preceding
1770 ;; character to see if it's anying that can't end syntactic ws, so we can
1771 ;; bail out early in the majority of cases when there just are a few ws
1772 ;; chars. Newlines are complicated in the backward direction, so we can't
1773 ;; skip over them.
1774 (skip-chars-backward " \t\f")
1775 (when (and (not (bobp))
1776 (save-excursion
1777 (backward-char)
1778 (looking-at c-syntactic-ws-end)))
1780 ;; Try to find a rung position in the simple ws preceding point, so that
1781 ;; we can get a cache hit even if the last bit of the simple ws has
1782 ;; changed recently.
1783 (setq simple-ws-beg (point))
1784 (skip-chars-backward " \t\n\r\f\v")
1785 (if (setq rung-is-marked (text-property-any
1786 (point) (min (1+ rung-pos) (point-max))
1787 'c-is-sws t))
1788 ;; `rung-pos' will be the earliest marked position, which means that
1789 ;; there might be later unmarked parts in the simple ws region.
1790 ;; It's not worth the effort to fix that; the last part of the
1791 ;; simple ws is also typically edited often, so it could be wasted.
1792 (goto-char (setq rung-pos rung-is-marked))
1793 (goto-char simple-ws-beg))
1795 (while
1796 (progn
1797 (while
1798 (when (and rung-is-marked
1799 (not (bobp))
1800 (get-text-property (1- (point)) 'c-in-sws))
1802 ;; The following search is the main reason that `c-in-sws'
1803 ;; and `c-is-sws' aren't combined to one property.
1804 (goto-char (previous-single-property-change
1805 (point) 'c-in-sws nil (point-min)))
1806 (unless (get-text-property (point) 'c-is-sws)
1807 ;; If the `c-in-sws' region extended past the first
1808 ;; `c-is-sws' char we have to go forward a bit.
1809 (goto-char (next-single-property-change
1810 (point) 'c-is-sws)))
1812 (c-debug-sws-msg
1813 "c-backward-sws cached move %s <- %s (min %s)"
1814 (point) rung-pos (point-min))
1816 (setq rung-pos (point))
1817 (if (and (< (min (skip-chars-backward " \t\f\v")
1818 (progn
1819 (setq simple-ws-beg (point))
1820 (skip-chars-backward " \t\n\r\f\v")))
1822 (setq rung-is-marked
1823 (text-property-any (point) rung-pos
1824 'c-is-sws t)))
1826 (goto-char simple-ws-beg)
1827 nil))
1829 ;; We'll loop here if there is simple ws before the first rung.
1830 ;; That means that there's been some change in it and it's
1831 ;; possible that we've stepped into another ladder, so extend
1832 ;; the previous one to join with it if there is one, and try to
1833 ;; use the cache again.
1834 (c-debug-sws-msg
1835 "c-backward-sws extending rung with [%s..%s] (min %s)"
1836 rung-is-marked rung-pos (point-min))
1837 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1838 ;; Remove any `c-in-sws' property from the last char of
1839 ;; the rung before we mark it with `c-is-sws', so that we
1840 ;; won't connect with the remains of a broken "ladder".
1841 (c-remove-in-sws (1- rung-pos) rung-pos))
1842 (c-put-is-sws rung-is-marked
1843 rung-pos)
1844 (c-put-in-sws rung-is-marked
1845 (1- rung-pos))
1846 (setq rung-pos rung-is-marked
1847 last-put-in-sws-pos rung-pos))
1849 (c-backward-comments)
1850 (setq cmt-skip-pos (point))
1852 (cond
1853 ((and c-opt-cpp-prefix
1854 (/= cmt-skip-pos simple-ws-beg)
1855 (c-beginning-of-macro))
1856 ;; Inside a cpp directive. See if it should be skipped over.
1857 (let ((cpp-beg (point)))
1859 ;; Move back over all line continuations in the region skipped
1860 ;; over by `c-backward-comments'. If we go past it then we
1861 ;; started inside the cpp directive.
1862 (goto-char simple-ws-beg)
1863 (beginning-of-line)
1864 (while (and (> (point) cmt-skip-pos)
1865 (progn (backward-char)
1866 (eq (char-before) ?\\)))
1867 (beginning-of-line))
1869 (if (< (point) cmt-skip-pos)
1870 ;; Don't move past the cpp directive if we began inside
1871 ;; it. Note that the position at the end of the last line
1872 ;; of the macro is also considered to be within it.
1873 (progn (goto-char cmt-skip-pos)
1874 nil)
1876 ;; It's worthwhile to spend a little bit of effort on finding
1877 ;; the end of the macro, to get a good `simple-ws-beg'
1878 ;; position for the cache. Note that `c-backward-comments'
1879 ;; could have stepped over some comments before going into
1880 ;; the macro, and then `simple-ws-beg' must be kept on the
1881 ;; same side of those comments.
1882 (goto-char simple-ws-beg)
1883 (skip-chars-backward " \t\n\r\f\v")
1884 (if (eq (char-before) ?\\)
1885 (forward-char))
1886 (forward-line 1)
1887 (if (< (point) simple-ws-beg)
1888 ;; Might happen if comments after the macro were skipped
1889 ;; over.
1890 (setq simple-ws-beg (point)))
1892 (goto-char cpp-beg)
1893 t)))
1895 ((/= (save-excursion
1896 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1897 (setq next-rung-pos (point)))
1898 simple-ws-beg)
1899 ;; Skipped over comments. Must put point at the end of
1900 ;; the simple ws at point since we might be after a line
1901 ;; comment or cpp directive that's been partially
1902 ;; narrowed out, and we can't risk marking the simple ws
1903 ;; at the end of it.
1904 (goto-char next-rung-pos)
1905 t)))
1907 ;; We've searched over a piece of non-white syntactic ws. See if this
1908 ;; can be cached.
1909 (setq next-rung-pos (point))
1910 (skip-chars-backward " \t\f\v")
1912 (if (or
1913 ;; Cache if we started either from a marked rung or from a
1914 ;; completely uncached position.
1915 rung-is-marked
1916 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1918 ;; Cache if there's a marked rung in the encountered simple ws.
1919 (save-excursion
1920 (skip-chars-backward " \t\n\r\f\v")
1921 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1922 'c-is-sws t)))
1924 (progn
1925 (c-debug-sws-msg
1926 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1927 (point) (1+ next-rung-pos)
1928 simple-ws-beg (min (1+ rung-pos) (point-max))
1929 (point-min))
1931 ;; Remove the properties for any nested ws that might be cached.
1932 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1933 ;; anyway.
1934 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1935 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1936 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1937 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1938 ;; Remove any `c-in-sws' property from the last char of
1939 ;; the rung before we mark it with `c-is-sws', so that we
1940 ;; won't connect with the remains of a broken "ladder".
1941 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1942 (c-put-is-sws simple-ws-beg
1943 rung-end-pos)
1944 (setq rung-is-marked t)))
1945 (c-put-in-sws (setq simple-ws-beg (point)
1946 last-put-in-sws-pos simple-ws-beg)
1947 rung-pos)
1948 (c-put-is-sws (setq rung-pos simple-ws-beg)
1949 (1+ next-rung-pos)))
1951 (c-debug-sws-msg
1952 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1953 (point) (1+ next-rung-pos)
1954 simple-ws-beg (min (1+ rung-pos) (point-max))
1955 (point-min))
1956 (setq rung-pos next-rung-pos
1957 simple-ws-beg (point))
1960 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1961 ;; another one before the point (which might occur when editing inside a
1962 ;; comment or macro).
1963 (when (eq last-put-in-sws-pos (point))
1964 (cond ((< (point-min) last-put-in-sws-pos)
1965 (c-debug-sws-msg
1966 "c-backward-sws clearing at %s for cache separation"
1967 (1- last-put-in-sws-pos))
1968 (c-remove-in-sws (1- last-put-in-sws-pos)
1969 last-put-in-sws-pos))
1970 ((> (point-min) 1)
1971 ;; If at bob and the buffer is narrowed, we have to clear the
1972 ;; character we're standing on instead since there might be a
1973 ;; `c-in-sws' before (point-min). In this case it's necessary
1974 ;; to clear both properties.
1975 (c-debug-sws-msg
1976 "c-backward-sws clearing thoroughly at %s for cache separation"
1977 last-put-in-sws-pos)
1978 (c-remove-is-and-in-sws last-put-in-sws-pos
1979 (1+ last-put-in-sws-pos)))))
1983 ;; Other whitespace tools
1984 (defun c-partial-ws-p (beg end)
1985 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1986 ;; region? This is a "heuristic" function. .....
1988 ;; The motivation for the second bit is to check whether removing this
1989 ;; region would coalesce two symbols.
1991 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1992 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1993 (save-excursion
1994 (let ((end+1 (min (1+ end) (point-max))))
1995 (or (progn (goto-char (max (point-min) (1- beg)))
1996 (c-skip-ws-forward end)
1997 (eq (point) end))
1998 (progn (goto-char beg)
1999 (c-skip-ws-forward end+1)
2000 (eq (point) end+1))))))
2002 ;; A system for finding noteworthy parens before the point.
2004 (defconst c-state-cache-too-far 5000)
2005 ;; A maximum comfortable scanning distance, e.g. between
2006 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2007 ;; this distance is exceeded, we take "emergency meausures", e.g. by clearing
2008 ;; the cache and starting again from point-min or a beginning of defun. This
2009 ;; value can be tuned for efficiency or set to a lower value for testing.
2011 (defvar c-state-cache nil)
2012 (make-variable-buffer-local 'c-state-cache)
2013 ;; The state cache used by `c-parse-state' to cut down the amount of
2014 ;; searching. It's the result from some earlier `c-parse-state' call. See
2015 ;; `c-parse-state''s doc string for details of its structure.
2017 ;; The use of the cached info is more effective if the next
2018 ;; `c-parse-state' call is on a line close by the one the cached state
2019 ;; was made at; the cache can actually slow down a little if the
2020 ;; cached state was made very far back in the buffer. The cache is
2021 ;; most effective if `c-parse-state' is used on each line while moving
2022 ;; forward.
2024 (defvar c-state-cache-good-pos 1)
2025 (make-variable-buffer-local 'c-state-cache-good-pos)
2026 ;; This is a position where `c-state-cache' is known to be correct, or
2027 ;; nil (see below). It's a position inside one of the recorded unclosed
2028 ;; parens or the top level, but not further nested inside any literal or
2029 ;; subparen that is closed before the last recorded position.
2031 ;; The exact position is chosen to try to be close to yet earlier than
2032 ;; the position where `c-state-cache' will be called next. Right now
2033 ;; the heuristic is to set it to the position after the last found
2034 ;; closing paren (of any type) before the line on which
2035 ;; `c-parse-state' was called. That is chosen primarily to work well
2036 ;; with refontification of the current line.
2038 ;; 2009-07-28: When `c-state-point-min' and the last position where
2039 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2040 ;; both in the same literal, there is no such "good position", and
2041 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2042 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2044 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2045 ;; the middle of the desert, as long as it is not within a brace pair
2046 ;; recorded in `c-state-cache' or a paren/bracket pair.
2049 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2050 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2051 ;; speed up testing for non-literality.
2052 (defconst c-state-nonlit-pos-interval 10000)
2053 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2055 (defvar c-state-nonlit-pos-cache nil)
2056 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2057 ;; A list of buffer positions which are known not to be in a literal or a cpp
2058 ;; construct. This is ordered with higher positions at the front of the list.
2059 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2061 (defvar c-state-nonlit-pos-cache-limit 1)
2062 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2063 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2064 ;; reduced by buffer changes, and increased by invocations of
2065 ;; `c-state-literal-at'.
2067 (defsubst c-state-pp-to-literal (from to)
2068 ;; Do a parse-partial-sexp from FROM to TO, returning the bounds of any
2069 ;; literal at TO as a cons, otherwise NIL.
2070 ;; FROM must not be in a literal, and the buffer should already be wide
2071 ;; enough.
2072 (save-excursion
2073 (let ((s (parse-partial-sexp from to)))
2074 (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
2075 (parse-partial-sexp (point) (point-max)
2076 nil ; TARGETDEPTH
2077 nil ; STOPBEFORE
2078 s ; OLDSTATE
2079 'syntax-table) ; stop at end of literal
2080 (cons (nth 8 s) (point))))))
2082 (defun c-state-literal-at (here)
2083 ;; If position HERE is inside a literal, return (START . END), the
2084 ;; boundaries of the literal (which may be outside the accessible bit of the
2085 ;; buffer). Otherwise, return nil.
2087 ;; This function is almost the same as `c-literal-limits'. It differs in
2088 ;; that it is a lower level function, and that it rigourously follows the
2089 ;; syntax from BOB, whereas `c-literal-limits' uses a "local" safe position.
2091 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2092 ;; MAY NOT contain any positions within macros, since macros are frequently
2093 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2094 ;; We cannot rely on this mechanism whilst determining a cache pos since
2095 ;; this function is also called from outwith `c-parse-state'.
2096 (save-restriction
2097 (widen)
2098 (save-excursion
2099 (let ((c c-state-nonlit-pos-cache)
2100 pos npos lit)
2101 ;; Trim the cache to take account of buffer changes.
2102 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2103 (setq c (cdr c)))
2104 (setq c-state-nonlit-pos-cache c)
2106 (while (and c (> (car c) here))
2107 (setq c (cdr c)))
2108 (setq pos (or (car c) (point-min)))
2110 (while (<= (setq npos (+ pos c-state-nonlit-pos-interval))
2111 here)
2112 (setq lit (c-state-pp-to-literal pos npos))
2113 (setq pos (or (cdr lit) npos)) ; end of literal containing npos.
2114 (goto-char pos)
2115 (when (and (c-beginning-of-macro) (/= (point) pos))
2116 (c-syntactic-end-of-macro)
2117 (or (eobp) (forward-char))
2118 (setq pos (point)))
2119 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2121 (if (> pos c-state-nonlit-pos-cache-limit)
2122 (setq c-state-nonlit-pos-cache-limit pos))
2123 (if (< pos here)
2124 (setq lit (c-state-pp-to-literal pos here)))
2125 lit))))
2127 (defsubst c-state-lit-beg (pos)
2128 ;; Return the start of the literal containing POS, or POS itself.
2129 (or (car (c-state-literal-at pos))
2130 pos))
2132 (defsubst c-state-cache-non-literal-place (pos state)
2133 ;; Return a position outside of a string/comment at or before POS.
2134 ;; STATE is the parse-partial-sexp state at POS.
2135 (if (or (nth 3 state) ; in a string?
2136 (nth 4 state)) ; in a comment?
2137 (nth 8 state)
2138 pos))
2141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2142 ;; Stuff to do with point-min, and coping with any literal there.
2143 (defvar c-state-point-min 1)
2144 (make-variable-buffer-local 'c-state-point-min)
2145 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2146 ;; narrowing is likely to affect the parens that are visible before the point.
2148 (defvar c-state-point-min-lit-type nil)
2149 (make-variable-buffer-local 'c-state-point-min-lit-type)
2150 (defvar c-state-point-min-lit-start nil)
2151 (make-variable-buffer-local 'c-state-point-min-lit-start)
2152 ;; These two variables define the literal, if any, containing point-min.
2153 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2154 ;; literal. If there's no literal there, they're both nil.
2156 (defvar c-state-min-scan-pos 1)
2157 (make-variable-buffer-local 'c-state-min-scan-pos)
2158 ;; This is the earliest buffer-pos from which scanning can be done. It is
2159 ;; either the end of the literal containing point-min, or point-min itself.
2160 ;; It becomes nil if the buffer is changed earlier than this point.
2161 (defun c-state-get-min-scan-pos ()
2162 ;; Return the lowest valid scanning pos. This will be the end of the
2163 ;; literal enclosing point-min, or point-min itself.
2164 (or c-state-min-scan-pos
2165 (save-restriction
2166 (save-excursion
2167 (widen)
2168 (goto-char c-state-point-min-lit-start)
2169 (if (eq c-state-point-min-lit-type 'string)
2170 (forward-sexp)
2171 (forward-comment 1))
2172 (setq c-state-min-scan-pos (point))))))
2174 (defun c-state-mark-point-min-literal ()
2175 ;; Determine the properties of any literal containing POINT-MIN, setting the
2176 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2177 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2178 (let ((p-min (point-min))
2179 lit)
2180 (save-restriction
2181 (widen)
2182 (setq lit (c-state-literal-at p-min))
2183 (if lit
2184 (setq c-state-point-min-lit-type
2185 (save-excursion
2186 (goto-char (car lit))
2187 (cond
2188 ((looking-at c-block-comment-start-regexp) 'c)
2189 ((looking-at c-line-comment-starter) 'c++)
2190 (t 'string)))
2191 c-state-point-min-lit-start (car lit)
2192 c-state-min-scan-pos (cdr lit))
2193 (setq c-state-point-min-lit-type nil
2194 c-state-point-min-lit-start nil
2195 c-state-min-scan-pos p-min)))))
2198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2199 ;; A variable which signals a brace dessert - helpful for reducing the number
2200 ;; of fruitless backward scans.
2201 (defvar c-state-brace-pair-desert nil)
2202 (make-variable-buffer-local 'c-state-brace-pair-desert)
2203 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2204 ;; that defun has searched backwards for a brace pair and not found one. Its
2205 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2206 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2207 ;; nil when at top level) and FROM is where the backward search started. It
2208 ;; is reset to nil in `c-invalidate-state-cache'.
2211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2212 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2213 ;; list of like structure.
2214 (defmacro c-state-cache-top-lparen (&optional cache)
2215 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2216 ;; (default `c-state-cache') (or nil).
2217 (let ((cash (or cache 'c-state-cache)))
2218 `(if (consp (car ,cash))
2219 (caar ,cash)
2220 (car ,cash))))
2222 (defmacro c-state-cache-top-paren (&optional cache)
2223 ;; Return the address of the latest brace/bracket/paren (whether left or
2224 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2225 (let ((cash (or cache 'c-state-cache)))
2226 `(if (consp (car ,cash))
2227 (cdar ,cash)
2228 (car ,cash))))
2230 (defmacro c-state-cache-after-top-paren (&optional cache)
2231 ;; Return the position just after the latest brace/bracket/paren (whether
2232 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2233 (let ((cash (or cache 'c-state-cache)))
2234 `(if (consp (car ,cash))
2235 (cdar ,cash)
2236 (and (car ,cash)
2237 (1+ (car ,cash))))))
2239 (defun c-get-cache-scan-pos (here)
2240 ;; From the state-cache, determine the buffer position from which we might
2241 ;; scan forward to HERE to update this cache. This position will be just
2242 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2243 ;; return the earliest position in the accessible region which isn't within
2244 ;; a literal. If the visible portion of the buffer is entirely within a
2245 ;; literal, return NIL.
2246 (let ((c c-state-cache) elt)
2247 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2248 (while (and c
2249 (>= (c-state-cache-top-lparen c) here))
2250 (setq c (cdr c)))
2252 (setq elt (car c))
2253 (cond
2254 ((consp elt)
2255 (if (> (cdr elt) here)
2256 (1+ (car elt))
2257 (cdr elt)))
2258 (elt (1+ elt))
2259 ((<= (c-state-get-min-scan-pos) here)
2260 (c-state-get-min-scan-pos))
2261 (t nil))))
2263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2264 ;; Variables which keep track of preprocessor constructs.
2265 (defvar c-state-old-cpp-beg nil)
2266 (make-variable-buffer-local 'c-state-old-cpp-beg)
2267 (defvar c-state-old-cpp-end nil)
2268 (make-variable-buffer-local 'c-state-old-cpp-end)
2269 ;; These are the limits of the macro containing point at the previous call of
2270 ;; `c-parse-state', or nil.
2272 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2273 ;; Defuns which analyse the buffer, yet don't change `c-state-cache'.
2274 (defun c-get-fallback-scan-pos (here)
2275 ;; Return a start position for building `c-state-cache' from
2276 ;; scratch. This will be at the top level, 2 defuns back.
2277 (save-excursion
2278 ;; Go back 2 bods, but ignore any bogus positions returned by
2279 ;; beginning-of-defun (i.e. open paren in column zero).
2280 (goto-char here)
2281 (let ((cnt 2))
2282 (while (not (or (bobp) (zerop cnt)))
2283 (c-beginning-of-defun-1) ; Pure elisp BOD.
2284 (if (eq (char-after) ?\{)
2285 (setq cnt (1- cnt)))))
2286 (point)))
2288 (defun c-state-balance-parens-backwards (here- here+ top)
2289 ;; Return the position of the opening paren/brace/bracket before HERE- which
2290 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2291 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2293 ;; ............................................
2294 ;; | |
2295 ;; ( [ ( .........#macro.. ) ( ) ] )
2296 ;; ^ ^ ^ ^
2297 ;; | | | |
2298 ;; return HERE- HERE+ TOP
2300 ;; If there aren't enough opening paren/brace/brackets, return the position
2301 ;; of the outermost one found, or HERE- if there are none. If there are no
2302 ;; closeing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2303 ;; must not be inside literals. Only the accessible portion of the buffer
2304 ;; will be scanned.
2306 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2307 ;; `here'. Go round the next loop each time we pass over such a ")". These
2308 ;; probably match "("s before `here-'.
2309 (let (pos pa ren+1 lonely-rens)
2310 (save-excursion
2311 (save-restriction
2312 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2313 (setq pos here+)
2314 (c-safe
2315 (while
2316 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2317 (setq lonely-rens (cons ren+1 lonely-rens)
2318 pos ren+1)))))
2320 ;; PART 2: Scan back before `here-' searching for the "("s
2321 ;; matching/mismatching the ")"s found above. We only need to direct the
2322 ;; caller to scan when we've encountered unmatched right parens.
2323 (setq pos here-)
2324 (when lonely-rens
2325 (c-safe
2326 (while
2327 (and lonely-rens ; actual values aren't used.
2328 (setq pa (scan-lists pos -1 1)))
2329 (setq pos pa)
2330 (setq lonely-rens (cdr lonely-rens)))))
2331 pos))
2333 (defun c-parse-state-get-strategy (here good-pos)
2334 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2335 ;; to minimise the amount of scanning. HERE is the pertinent position in
2336 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2337 ;; its head trimmed) is known to be good, or nil if there is no such
2338 ;; position.
2340 ;; The return value is a list, one of the following:
2342 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2343 ;; which is not less than CACHE-POS.
2344 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2345 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2346 ;; top level.
2347 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2348 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2349 ;; or below HERE.
2350 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2351 BOD-pos ; position of 2nd BOD before HERE.
2352 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2353 start-point
2354 how-far) ; putative scanning distance.
2355 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2356 (cond
2357 ((< here (c-state-get-min-scan-pos))
2358 (setq strategy 'IN-LIT
2359 start-point nil
2360 cache-pos nil
2361 how-far 0))
2362 ((<= good-pos here)
2363 (setq strategy 'forward
2364 start-point (max good-pos cache-pos)
2365 how-far (- here start-point)))
2366 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2367 (setq strategy 'backward
2368 how-far (- good-pos here)))
2370 (setq strategy 'forward
2371 how-far (- here cache-pos)
2372 start-point cache-pos)))
2374 ;; Might we be better off starting from the top level, two defuns back,
2375 ;; instead?
2376 (when (> how-far c-state-cache-too-far)
2377 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2378 (if (< (- here BOD-pos) how-far)
2379 (setq strategy 'BOD
2380 start-point BOD-pos)))
2382 (list
2383 strategy
2384 (and (memq strategy '(forward backward)) cache-pos)
2385 (and (memq strategy '(forward BOD)) start-point))))
2388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2389 ;; Routines which change `c-state-cache' and associated values.
2390 (defun c-renarrow-state-cache ()
2391 ;; The region (more precisely, point-min) has changed since we
2392 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2393 (if (< (point-min) c-state-point-min)
2394 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2395 ;; It would be possible to do a better job here and recalculate the top
2396 ;; only.
2397 (progn
2398 (c-state-mark-point-min-literal)
2399 (setq c-state-cache nil
2400 c-state-cache-good-pos c-state-min-scan-pos
2401 c-state-brace-pair-desert nil))
2403 ;; point-min has MOVED FORWARD.
2405 ;; Is the new point-min inside a (different) literal?
2406 (unless (and c-state-point-min-lit-start ; at prev. point-min
2407 (< (point-min) (c-state-get-min-scan-pos)))
2408 (c-state-mark-point-min-literal))
2410 ;; Cut off a bit of the tail from `c-state-cache'.
2411 (let ((ptr (cons nil c-state-cache))
2413 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2414 (>= pa (point-min)))
2415 (setq ptr (cdr ptr)))
2417 (when (consp ptr)
2418 (if (eq (cdr ptr) c-state-cache)
2419 (setq c-state-cache nil
2420 c-state-cache-good-pos c-state-min-scan-pos)
2421 (setcdr ptr nil)
2422 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2425 (setq c-state-point-min (point-min)))
2427 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2428 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2429 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2430 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2431 ;; the highest brace pair whose "}" is below UPPER-LIM.
2433 ;; Return non-nil when this has been done.
2435 ;; This routine should be fast. Since it can get called a LOT, we maintain
2436 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2437 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2438 (save-excursion
2439 (save-restriction
2440 (let ((bra from) ce ; Positions of "{" and "}".
2441 new-cons
2442 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2443 (macro-start-or-from
2444 (progn (goto-char from)
2445 (c-beginning-of-macro)
2446 (point))))
2447 (or upper-lim (setq upper-lim from))
2449 ;; If we're essentially repeating a fruitless search, just give up.
2450 (unless (and c-state-brace-pair-desert
2451 (eq cache-pos (car c-state-brace-pair-desert))
2452 (<= from (cdr c-state-brace-pair-desert)))
2453 ;; Only search what we absolutely need to:
2454 (if (and c-state-brace-pair-desert
2455 (> from (cdr c-state-brace-pair-desert)))
2456 (narrow-to-region (cdr c-state-brace-pair-desert) (point-max)))
2458 ;; In the next pair of nested loops, the inner one moves back past a
2459 ;; pair of (mis-)matching parens or brackets; the outer one moves
2460 ;; back over a sequence of unmatched close brace/paren/bracket each
2461 ;; time round.
2462 (while
2463 (progn
2464 (c-safe
2465 (while
2466 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2467 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2468 (or (> ce upper-lim)
2469 (not (eq (char-after bra) ?\{))
2470 (and (goto-char bra)
2471 (c-beginning-of-macro)
2472 (< (point) macro-start-or-from))))))
2473 (and ce (< ce bra)))
2474 (setq bra ce)) ; If we just backed over an unbalanced closing
2475 ; brace, ignore it.
2477 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2478 ;; We've found the desired brace-pair.
2479 (progn
2480 (setq new-cons (cons bra (1+ ce)))
2481 (cond
2482 ((consp (car c-state-cache))
2483 (setcar c-state-cache new-cons))
2484 ((and (numberp (car c-state-cache)) ; probably never happens
2485 (< ce (car c-state-cache)))
2486 (setcdr c-state-cache
2487 (cons new-cons (cdr c-state-cache))))
2488 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2490 ;; We haven't found a brace pair. Record this.
2491 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2493 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2494 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2495 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2496 ;; "push" "a" brace pair onto `c-state-cache'.
2498 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2499 ;; otherwise push it normally.
2501 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2502 ;; latter is inside a macro, not being a macro containing
2503 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2504 ;; base pair. This latter case is assumed to be rare.
2506 ;; Note: POINT is not preserved in this routine.
2507 (if bra+1
2508 (if (or (> bra+1 macro-start-or-here)
2509 (progn (goto-char bra+1)
2510 (not (c-beginning-of-macro))))
2511 (setq c-state-cache
2512 (cons (cons (1- bra+1)
2513 (scan-lists bra+1 1 1))
2514 (if (consp (car c-state-cache))
2515 (cdr c-state-cache)
2516 c-state-cache)))
2517 ;; N.B. This defsubst codes one method for the simple, normal case,
2518 ;; and a more sophisticated, slower way for the general case. Don't
2519 ;; eliminate this defsubst - it's a speed optimisation.
2520 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2522 (defun c-append-to-state-cache (from)
2523 ;; Scan the buffer from FROM to (point-max), adding elements into
2524 ;; `c-state-cache' for braces etc. Return a candidate for
2525 ;; `c-state-cache-good-pos'.
2527 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2528 ;; any. Typically, it is immediately after it. It must not be inside a
2529 ;; literal.
2530 (let ((here-bol (c-point 'bol (point-max)))
2531 (macro-start-or-here
2532 (save-excursion (goto-char (point-max))
2533 (if (c-beginning-of-macro)
2534 (point)
2535 (point-max))))
2536 pa+1 ; pos just after an opening PAren (or brace).
2537 (ren+1 from) ; usually a pos just after an closing paREN etc.
2538 ; Is actually the pos. to scan for a (/{/[ from,
2539 ; which sometimes is after a silly )/}/].
2540 paren+1 ; Pos after some opening or closing paren.
2541 paren+1s ; A list of `paren+1's; used to determine a
2542 ; good-pos.
2543 bra+1 ce+1 ; just after L/R bra-ces.
2544 bra+1s ; list of OLD values of bra+1.
2545 mstart) ; start of a macro.
2547 (save-excursion
2548 ;; Each time round the following loop, we enter a succesively deeper
2549 ;; level of brace/paren nesting. (Except sometimes we "continue at
2550 ;; the existing level".) `pa+1' is a pos inside an opening
2551 ;; brace/paren/bracket, usually just after it.
2552 (while
2553 (progn
2554 ;; Each time round the next loop moves forward over an opening then
2555 ;; a closing brace/bracket/paren. This loop is white hot, so it
2556 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2557 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2558 ;; call of `scan-lists' signals an error, which happens when there
2559 ;; are no more b/b/p's to scan.
2560 (c-safe
2561 (while t
2562 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2563 paren+1s (cons pa+1 paren+1s))
2564 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2565 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2566 (setq bra+1 pa+1))
2567 (setcar paren+1s ren+1)))
2569 (if (and pa+1 (> pa+1 ren+1))
2570 ;; We've just entered a deeper nesting level.
2571 (progn
2572 ;; Insert the brace pair (if present) and the single open
2573 ;; paren/brace/bracket into `c-state-cache' It cannot be
2574 ;; inside a macro, except one around point, because of what
2575 ;; `c-neutralize-syntax-in-CPP' has done.
2576 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2577 ;; Insert the opening brace/bracket/paren position.
2578 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2579 ;; Clear admin stuff for the next more nested part of the scan.
2580 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2581 t) ; Carry on the loop
2583 ;; All open p/b/b's at this nesting level, if any, have probably
2584 ;; been closed by matching/mismatching ones. We're probably
2585 ;; finished - we just need to check for having found an
2586 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2587 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2588 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2590 ;; Record the final, innermost, brace-pair if there is one.
2591 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2593 ;; Determine a good pos
2594 (while (and (setq paren+1 (car paren+1s))
2595 (> (if (> paren+1 macro-start-or-here)
2596 paren+1
2597 (goto-char paren+1)
2598 (setq mstart (and (c-beginning-of-macro)
2599 (point)))
2600 (or mstart paren+1))
2601 here-bol))
2602 (setq paren+1s (cdr paren+1s)))
2603 (cond
2604 ((and paren+1 mstart)
2605 (min paren+1 mstart))
2606 (paren+1)
2607 (t from)))))
2609 (defun c-remove-stale-state-cache (good-pos pps-point)
2610 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2611 ;; not be in it when it is amended for position (point-max).
2612 ;; Additionally, the "outermost" open-brace entry before (point-max)
2613 ;; will be converted to a cons if the matching close-brace is scanned.
2615 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2616 ;; parens/braces/brackets between GOOD-POS and (point-max).
2618 ;; As a second thing, calculate the result of parse-partial-sexp at
2619 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2620 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2621 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2622 ;; needs to be FAST).
2624 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2625 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2626 ;; to be good (we aim for this to be as high as possible);
2627 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2628 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2629 ;; position to scan backwards from.
2630 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2631 (save-restriction
2632 (narrow-to-region 1 (point-max))
2633 (save-excursion
2634 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2635 (save-excursion
2636 (goto-char (point-max))
2637 (and (c-beginning-of-macro)
2638 (point))))
2639 (good-pos-actual-macro-start ; Start of macro containing good-pos
2640 ; or nil
2641 (and (< good-pos (point-max))
2642 (save-excursion
2643 (goto-char good-pos)
2644 (and (c-beginning-of-macro)
2645 (point)))))
2646 (good-pos-actual-macro-end ; End of this macro, (maybe
2647 ; (point-max)), or nil.
2648 (and good-pos-actual-macro-start
2649 (save-excursion
2650 (goto-char good-pos-actual-macro-start)
2651 (c-end-of-macro)
2652 (point))))
2653 pps-state ; Will be 9 or 10 elements long.
2655 upper-lim ; ,beyond which `c-state-cache' entries are removed
2656 scan-back-pos
2657 pair-beg pps-point-state target-depth)
2659 ;; Remove entries beyond (point-max). Also remove any entries inside
2660 ;; a macro, unless (point-max) is in the same macro.
2661 (setq upper-lim
2662 (if (or (null c-state-old-cpp-beg)
2663 (and (> (point-max) c-state-old-cpp-beg)
2664 (< (point-max) c-state-old-cpp-end)))
2665 (point-max)
2666 (min (point-max) c-state-old-cpp-beg)))
2667 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2668 (setq c-state-cache (cdr c-state-cache)))
2669 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2670 ;; RBrace and indicate we'll need to search backwards for a previous
2671 ;; brace pair.
2672 (when (and c-state-cache
2673 (consp (car c-state-cache))
2674 (> (cdar c-state-cache) upper-lim))
2675 (setcar c-state-cache (caar c-state-cache))
2676 (setq scan-back-pos (car c-state-cache)))
2678 ;; The next loop jumps forward out of a nested level of parens each
2679 ;; time round; the corresponding elements in `c-state-cache' are
2680 ;; removed. `pos' is just after the brace-pair or the open paren at
2681 ;; (car c-state-cache). There can be no open parens/braces/brackets
2682 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2683 ;; due to the interface spec to this function.
2684 (setq pos (if (and good-pos-actual-macro-end
2685 (not (eq good-pos-actual-macro-start
2686 in-macro-start)))
2687 (1+ good-pos-actual-macro-end) ; get outside the macro as
2688 ; marked by a `category' text property.
2689 good-pos))
2690 (goto-char pos)
2691 (while (and c-state-cache
2692 (< (point) (point-max)))
2693 (cond
2694 ((null pps-state) ; first time through
2695 (setq target-depth -1))
2696 ((eq (car pps-state) target-depth) ; found closing ),},]
2697 (setq target-depth (1- (car pps-state))))
2698 ;; Do nothing when we've merely reached pps-point.
2701 ;; Scan!
2702 (setq pps-state
2703 (parse-partial-sexp
2704 (point) (if (< (point) pps-point) pps-point (point-max))
2705 target-depth
2706 nil pps-state))
2708 (if (= (point) pps-point)
2709 (setq pps-point-state pps-state))
2711 (when (eq (car pps-state) target-depth)
2712 (setq pos (point)) ; POS is now just after an R-paren/brace.
2713 (cond
2714 ((and (consp (car c-state-cache))
2715 (eq (point) (cdar c-state-cache)))
2716 ;; We've just moved out of the paren pair containing the brace-pair
2717 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2718 ;; and is potentially where the open brace of a cons in
2719 ;; c-state-cache will be.
2720 (setq pair-beg (car-safe (cdr c-state-cache))
2721 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2722 ((numberp (car c-state-cache))
2723 (setq pair-beg (car c-state-cache)
2724 c-state-cache (cdr c-state-cache))) ; remove this
2725 ; containing Lparen
2726 ((numberp (cadr c-state-cache))
2727 (setq pair-beg (cadr c-state-cache)
2728 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2729 ; together with enclosed brace pair.
2730 ;; (t nil) ; Ignore an unmated Rparen.
2733 (if (< (point) pps-point)
2734 (setq pps-state (parse-partial-sexp (point) pps-point
2735 nil nil ; TARGETDEPTH, STOPBEFORE
2736 pps-state)))
2738 ;; If the last paren pair we moved out of was actually a brace pair,
2739 ;; insert it into `c-state-cache'.
2740 (when (and pair-beg (eq (char-after pair-beg) ?{))
2741 (if (consp (car-safe c-state-cache))
2742 (setq c-state-cache (cdr c-state-cache)))
2743 (setq c-state-cache (cons (cons pair-beg pos)
2744 c-state-cache)))
2746 (list pos scan-back-pos pps-state)))))
2748 (defun c-remove-stale-state-cache-backwards (here cache-pos)
2749 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2750 ;; buffer, and inform the caller of the scenario detected.
2752 ;; HERE is the position we're setting `c-state-cache' for.
2753 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2754 ;; before HERE, or a position at or near point-min which isn't in a
2755 ;; literal.
2757 ;; This function must only be called only when (> `c-state-cache-good-pos'
2758 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2759 ;; optimised to eliminate (or minimise) scanning between these two
2760 ;; positions.
2762 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2763 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2764 ;; could become so after missing elements are inserted into
2765 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2766 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2767 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2768 ;; before `here''s line, or the start of the literal containing it.
2769 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2770 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2771 ;; to scan backwards from.
2772 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2773 ;; POS and HERE which aren't recorded in `c-state-cache'.
2775 ;; The comments in this defun use "paren" to mean parenthesis or square
2776 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2778 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2779 ;; | | | | | |
2780 ;; CP E here D C good
2781 (let ((pos c-state-cache-good-pos)
2782 pa ren ; positions of "(" and ")"
2783 dropped-cons ; whether the last element dropped from `c-state-cache'
2784 ; was a cons (representing a brace-pair)
2785 good-pos ; see above.
2786 lit ; (START . END) of a literal containing some point.
2787 here-lit-start here-lit-end ; bounds of literal containing `here'
2788 ; or `here' itself.
2789 here- here+ ; start/end of macro around HERE, or HERE
2790 (here-bol (c-point 'bol here))
2791 (too-far-back (max (- here c-state-cache-too-far) 1)))
2793 ;; Remove completely irrelevant entries from `c-state-cache'.
2794 (while (and c-state-cache
2795 (>= (setq pa (c-state-cache-top-lparen)) here))
2796 (setq dropped-cons (consp (car c-state-cache)))
2797 (setq c-state-cache (cdr c-state-cache))
2798 (setq pos pa))
2799 ;; At this stage, (> pos here);
2800 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2802 (cond
2803 ((and (consp (car c-state-cache))
2804 (> (cdar c-state-cache) here))
2805 ;; CASE 1: The top of the cache is a brace pair which now encloses
2806 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2807 ;; knowledge of what's inside these braces, we have no alternative but
2808 ;; to direct the caller to scan the buffer from the opening brace.
2809 (setq pos (caar c-state-cache))
2810 (setcar c-state-cache pos)
2811 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
2812 ; entry into a { entry, so the caller needs to
2813 ; search for a brace pair before the {.
2815 ;; `here' might be inside a literal. Check for this.
2816 ((progn
2817 (setq lit (c-state-literal-at here)
2818 here-lit-start (or (car lit) here)
2819 here-lit-end (or (cdr lit) here))
2820 ;; Has `here' just "newly entered" a macro?
2821 (save-excursion
2822 (goto-char here-lit-start)
2823 (if (and (c-beginning-of-macro)
2824 (or (null c-state-old-cpp-beg)
2825 (not (= (point) c-state-old-cpp-beg))))
2826 (progn
2827 (setq here- (point))
2828 (c-end-of-macro)
2829 (setq here+ (point)))
2830 (setq here- here-lit-start
2831 here+ here-lit-end)))
2833 ;; `here' might be nested inside any depth of parens (or brackets but
2834 ;; not braces). Scan backwards to find the outermost such opening
2835 ;; paren, if there is one. This will be the scan position to return.
2836 (save-restriction
2837 (narrow-to-region cache-pos (point-max))
2838 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
2839 nil)) ; for the cond
2841 ((< pos here-lit-start)
2842 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2843 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2844 ;; a brace pair preceding this, it will already be in `c-state-cache',
2845 ;; unless there was a brace pair after it, i.e. there'll only be one to
2846 ;; scan for if we've just deleted one.
2847 (list pos (and dropped-cons pos) t)) ; Return value.
2849 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2850 ;; Further forward scanning isn't needed, but we still need to find a
2851 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2852 ((progn
2853 (save-restriction
2854 (narrow-to-region here-bol (point-max))
2855 (setq pos here-lit-start)
2856 (c-safe (while (setq pa (scan-lists pos -1 1))
2857 (setq pos pa)))) ; might signal
2858 nil)) ; for the cond
2860 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
2861 ;; CASE 3: After a }/)/] before `here''s BOL.
2862 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
2865 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2866 ;; literal containing it.
2867 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
2868 (list good-pos (and dropped-cons good-pos) nil)))))
2871 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2872 ;; Externally visible routines.
2874 (defun c-state-cache-init ()
2875 (setq c-state-cache nil
2876 c-state-cache-good-pos 1
2877 c-state-nonlit-pos-cache nil
2878 c-state-nonlit-pos-cache-limit 1
2879 c-state-brace-pair-desert nil
2880 c-state-point-min 1
2881 c-state-point-min-lit-type nil
2882 c-state-point-min-lit-start nil
2883 c-state-min-scan-pos 1
2884 c-state-old-cpp-beg nil
2885 c-state-old-cpp-end nil)
2886 (c-state-mark-point-min-literal))
2888 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2889 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
2890 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
2891 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
2892 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
2893 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
2894 ;; (defun c-state-dump ()
2895 ;; ;; For debugging.
2896 ;; ;(message
2897 ;; (concat
2898 ;; (c-sc-qde c-state-cache)
2899 ;; (c-sc-de c-state-cache-good-pos)
2900 ;; (c-sc-qde c-state-nonlit-pos-cache)
2901 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
2902 ;; (c-sc-qde c-state-brace-pair-desert)
2903 ;; (c-sc-de c-state-point-min)
2904 ;; (c-sc-de c-state-point-min-lit-type)
2905 ;; (c-sc-de c-state-point-min-lit-start)
2906 ;; (c-sc-de c-state-min-scan-pos)
2907 ;; (c-sc-de c-state-old-cpp-beg)
2908 ;; (c-sc-de c-state-old-cpp-end)))
2909 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2911 (defun c-invalidate-state-cache-1 (here)
2912 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2913 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2914 ;; left in a consistent state.
2916 ;; This is much like `c-whack-state-after', but it never changes a paren
2917 ;; pair element into an open paren element. Doing that would mean that the
2918 ;; new open paren wouldn't have the required preceding paren pair element.
2920 ;; This function is called from c-after-change.
2922 ;; The cache of non-literals:
2923 (if (< here c-state-nonlit-pos-cache-limit)
2924 (setq c-state-nonlit-pos-cache-limit here))
2926 ;; `c-state-cache':
2927 ;; Case 1: if `here' is in a literal containing point-min, everything
2928 ;; becomes (or is already) nil.
2929 (if (or (null c-state-cache-good-pos)
2930 (< here (c-state-get-min-scan-pos)))
2931 (setq c-state-cache nil
2932 c-state-cache-good-pos nil
2933 c-state-min-scan-pos nil)
2935 ;;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value below
2936 ;;; `here'. To maintain its consistency, we may need to insert a new brace
2937 ;;; pair.
2938 (let ((here-bol (c-point 'bol here))
2939 too-high-pa ; recorded {/(/[ next above here, or nil.
2940 dropped-cons ; was the last removed element a brace pair?
2942 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
2943 (while (and c-state-cache
2944 (>= (setq pa (c-state-cache-top-paren)) here))
2945 (setq dropped-cons (consp (car c-state-cache))
2946 too-high-pa (c-state-cache-top-lparen)
2947 c-state-cache (cdr c-state-cache)))
2949 ;; Do we need to add in an earlier brace pair, having lopped one off?
2950 (if (and dropped-cons
2951 (< too-high-pa (+ here c-state-cache-too-far)))
2952 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
2953 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
2954 (c-state-get-min-scan-pos)))))
2956 ;; The brace-pair desert marker:
2957 (when (car c-state-brace-pair-desert)
2958 (if (< here (car c-state-brace-pair-desert))
2959 (setq c-state-brace-pair-desert nil)
2960 (if (< here (cdr c-state-brace-pair-desert))
2961 (setcdr c-state-brace-pair-desert here)))))
2963 (defun c-parse-state-1 ()
2964 ;; Find and record all noteworthy parens between some good point earlier in
2965 ;; the file and point. That good point is at least the beginning of the
2966 ;; top-level construct we are in, or the beginning of the preceding
2967 ;; top-level construct if we aren't in one.
2969 ;; The returned value is a list of the noteworthy parens with the last one
2970 ;; first. If an element in the list is an integer, it's the position of an
2971 ;; open paren (of any type) which has not been closed before the point. If
2972 ;; an element is a cons, it gives the position of a closed BRACE paren
2973 ;; pair[*]; the car is the start brace position and the cdr is the position
2974 ;; following the closing brace. Only the last closed brace paren pair
2975 ;; before each open paren and before the point is recorded, and thus the
2976 ;; state never contains two cons elements in succession. When a close brace
2977 ;; has no matching open brace (e.g., the matching brace is outside the
2978 ;; visible region), it is not represented in the returned value.
2980 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
2981 ;; This defun explicitly treats mismatching parens/braces/brackets as
2982 ;; matching. It is the open brace which makes it a "brace" pair.
2984 ;; If POINT is within a macro, open parens and brace pairs within
2985 ;; THIS macro MIGHT be recorded. This depends on whether their
2986 ;; syntactic properties have been suppressed by
2987 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
2989 ;; Currently no characters which are given paren syntax with the
2990 ;; syntax-table property are recorded, i.e. angle bracket arglist
2991 ;; parens are never present here. Note that this might change.
2993 ;; BUG: This function doesn't cope entirely well with unbalanced
2994 ;; parens in macros. (2008-12-11: this has probably been resolved
2995 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
2996 ;; following case the brace before the macro isn't balanced with the
2997 ;; one after it:
2999 ;; {
3000 ;; #define X {
3001 ;; }
3003 ;; Note to maintainers: this function DOES get called with point
3004 ;; within comments and strings, so don't assume it doesn't!
3006 ;; This function might do hidden buffer changes.
3007 (let* ((here (point))
3008 (here-bopl (c-point 'bopl))
3009 strategy ; 'forward, 'backward etc..
3010 ;; Candidate positions to start scanning from:
3011 cache-pos ; highest position below HERE already existing in
3012 ; cache (or 1).
3013 good-pos
3014 start-point
3015 bopl-state
3017 scan-backward-pos scan-forward-p) ; used for 'backward.
3018 ;; If POINT-MIN has changed, adjust the cache
3019 (unless (= (point-min) c-state-point-min)
3020 (c-renarrow-state-cache))
3022 ;; Strategy?
3023 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3024 strategy (car res)
3025 cache-pos (cadr res)
3026 start-point (nth 2 res))
3028 (when (eq strategy 'BOD)
3029 (setq c-state-cache nil
3030 c-state-cache-good-pos start-point))
3032 ;; SCAN!
3033 (save-restriction
3034 (cond
3035 ((memq strategy '(forward BOD))
3036 (narrow-to-region (point-min) here)
3037 (setq res (c-remove-stale-state-cache start-point here-bopl))
3038 (setq cache-pos (car res)
3039 scan-backward-pos (cadr res)
3040 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
3041 ; start-point)
3042 (if scan-backward-pos
3043 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
3044 (setq good-pos
3045 (c-append-to-state-cache cache-pos))
3046 (setq c-state-cache-good-pos
3047 (if (and bopl-state
3048 (< good-pos (- here c-state-cache-too-far)))
3049 (c-state-cache-non-literal-place here-bopl bopl-state)
3050 good-pos)))
3052 ((eq strategy 'backward)
3053 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
3054 good-pos (car res)
3055 scan-backward-pos (cadr res)
3056 scan-forward-p (car (cddr res)))
3057 (if scan-backward-pos
3058 (c-append-lower-brace-pair-to-state-cache
3059 scan-backward-pos))
3060 (setq c-state-cache-good-pos
3061 (if scan-forward-p
3062 (progn (narrow-to-region (point-min) here)
3063 (c-append-to-state-cache good-pos))
3065 (c-get-cache-scan-pos good-pos))))
3067 (t ; (eq strategy 'IN-LIT)
3068 (setq c-state-cache nil
3069 c-state-cache-good-pos nil)))))
3071 c-state-cache)
3073 (defun c-invalidate-state-cache (here)
3074 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3076 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3077 ;; of all parens in preprocessor constructs, except for any such construct
3078 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3079 ;; worrying further about macros and template delimiters.
3080 (c-with-<->-as-parens-suppressed
3081 (if (and c-state-old-cpp-beg
3082 (< c-state-old-cpp-beg here))
3083 (c-with-all-but-one-cpps-commented-out
3084 c-state-old-cpp-beg
3085 (min c-state-old-cpp-end here)
3086 (c-invalidate-state-cache-1 here))
3087 (c-with-cpps-commented-out
3088 (c-invalidate-state-cache-1 here)))))
3090 (defun c-parse-state ()
3091 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3092 ;; description of the functionality and return value.
3094 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3095 ;; of all parens in preprocessor constructs, except for any such construct
3096 ;; containing point. We can then call `c-parse-state-1' without worrying
3097 ;; further about macros and template delimiters.
3098 (let (here-cpp-beg here-cpp-end)
3099 (save-excursion
3100 (when (c-beginning-of-macro)
3101 (setq here-cpp-beg (point))
3102 (unless
3103 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3104 here-cpp-beg)
3105 (setq here-cpp-beg nil here-cpp-end nil))))
3106 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3107 ;; subsystem.
3108 (prog1
3109 (c-with-<->-as-parens-suppressed
3110 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3111 (c-with-all-but-one-cpps-commented-out
3112 here-cpp-beg here-cpp-end
3113 (c-parse-state-1))
3114 (c-with-cpps-commented-out
3115 (c-parse-state-1))))
3116 (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
3117 c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
3120 ;; Debug tool to catch cache inconsistencies. This is called from
3121 ;; 000tests.el.
3122 (defvar c-debug-parse-state nil)
3123 (unless (fboundp 'c-real-parse-state)
3124 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3125 (cc-bytecomp-defun c-real-parse-state)
3126 (defun c-debug-parse-state ()
3127 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3128 (let ((c-state-cache nil)
3129 (c-state-cache-good-pos 1)
3130 (c-state-nonlit-pos-cache nil)
3131 (c-state-nonlit-pos-cache-limit 1)
3132 (c-state-brace-pair-desert nil)
3133 (c-state-point-min 1)
3134 (c-state-point-min-lit-type nil)
3135 (c-state-point-min-lit-start nil)
3136 (c-state-min-scan-pos 1)
3137 (c-state-old-cpp-beg nil)
3138 (c-state-old-cpp-end nil))
3139 (setq res2 (c-real-parse-state)))
3140 (unless (equal res1 res2)
3141 ;; The cache can actually go further back due to the ad-hoc way
3142 ;; the first paren is found, so try to whack off a bit of its
3143 ;; start before complaining.
3144 (save-excursion
3145 (goto-char (or (c-least-enclosing-brace res2) (point)))
3146 (c-beginning-of-defun-1)
3147 (while (not (or (bobp) (eq (char-after) ?{)))
3148 (c-beginning-of-defun-1))
3149 (unless (equal (c-whack-state-before (point) res1) res2)
3150 (message (concat "c-parse-state inconsistency at %s: "
3151 "using cache: %s, from scratch: %s")
3152 here res1 res2))))
3153 res1))
3155 (defun c-toggle-parse-state-debug (&optional arg)
3156 (interactive "P")
3157 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3158 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3159 'c-debug-parse-state
3160 'c-real-parse-state)))
3161 (c-keep-region-active))
3162 (when c-debug-parse-state
3163 (c-toggle-parse-state-debug 1))
3166 (defun c-whack-state-before (bufpos paren-state)
3167 ;; Whack off any state information from PAREN-STATE which lies
3168 ;; before BUFPOS. Not destructive on PAREN-STATE.
3169 (let* ((newstate (list nil))
3170 (ptr newstate)
3171 car)
3172 (while paren-state
3173 (setq car (car paren-state)
3174 paren-state (cdr paren-state))
3175 (if (< (if (consp car) (car car) car) bufpos)
3176 (setq paren-state nil)
3177 (setcdr ptr (list car))
3178 (setq ptr (cdr ptr))))
3179 (cdr newstate)))
3181 (defun c-whack-state-after (bufpos paren-state)
3182 ;; Whack off any state information from PAREN-STATE which lies at or
3183 ;; after BUFPOS. Not destructive on PAREN-STATE.
3184 (catch 'done
3185 (while paren-state
3186 (let ((car (car paren-state)))
3187 (if (consp car)
3188 ;; just check the car, because in a balanced brace
3189 ;; expression, it must be impossible for the corresponding
3190 ;; close brace to be before point, but the open brace to
3191 ;; be after.
3192 (if (<= bufpos (car car))
3193 nil ; whack it off
3194 (if (< bufpos (cdr car))
3195 ;; its possible that the open brace is before
3196 ;; bufpos, but the close brace is after. In that
3197 ;; case, convert this to a non-cons element. The
3198 ;; rest of the state is before bufpos, so we're
3199 ;; done.
3200 (throw 'done (cons (car car) (cdr paren-state)))
3201 ;; we know that both the open and close braces are
3202 ;; before bufpos, so we also know that everything else
3203 ;; on state is before bufpos.
3204 (throw 'done paren-state)))
3205 (if (<= bufpos car)
3206 nil ; whack it off
3207 ;; it's before bufpos, so everything else should too.
3208 (throw 'done paren-state)))
3209 (setq paren-state (cdr paren-state)))
3210 nil)))
3212 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3213 ;; Return the bufpos of the innermost enclosing open paren before
3214 ;; bufpos, or nil if none was found.
3215 (let (enclosingp)
3216 (or bufpos (setq bufpos 134217727))
3217 (while paren-state
3218 (setq enclosingp (car paren-state)
3219 paren-state (cdr paren-state))
3220 (if (or (consp enclosingp)
3221 (>= enclosingp bufpos))
3222 (setq enclosingp nil)
3223 (setq paren-state nil)))
3224 enclosingp))
3226 (defun c-least-enclosing-brace (paren-state)
3227 ;; Return the bufpos of the outermost enclosing open paren, or nil
3228 ;; if none was found.
3229 (let (pos elem)
3230 (while paren-state
3231 (setq elem (car paren-state)
3232 paren-state (cdr paren-state))
3233 (if (integerp elem)
3234 (setq pos elem)))
3235 pos))
3237 (defun c-safe-position (bufpos paren-state)
3238 ;; Return the closest "safe" position recorded on PAREN-STATE that
3239 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3240 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3241 ;; find the closest limit before a given limit that might be nil.
3243 ;; A "safe" position is a position at or after a recorded open
3244 ;; paren, or after a recorded close paren. The returned position is
3245 ;; thus either the first position after a close brace, or the first
3246 ;; position after an enclosing paren, or at the enclosing paren in
3247 ;; case BUFPOS is immediately after it.
3248 (when bufpos
3249 (let (elem)
3250 (catch 'done
3251 (while paren-state
3252 (setq elem (car paren-state))
3253 (if (consp elem)
3254 (cond ((< (cdr elem) bufpos)
3255 (throw 'done (cdr elem)))
3256 ((< (car elem) bufpos)
3257 ;; See below.
3258 (throw 'done (min (1+ (car elem)) bufpos))))
3259 (if (< elem bufpos)
3260 ;; elem is the position at and not after the opening paren, so
3261 ;; we can go forward one more step unless it's equal to
3262 ;; bufpos. This is useful in some cases avoid an extra paren
3263 ;; level between the safe position and bufpos.
3264 (throw 'done (min (1+ elem) bufpos))))
3265 (setq paren-state (cdr paren-state)))))))
3267 (defun c-beginning-of-syntax ()
3268 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3269 ;; goes to the closest previous point that is known to be outside
3270 ;; any string literal or comment. `c-state-cache' is used if it has
3271 ;; a position in the vicinity.
3272 (let* ((paren-state c-state-cache)
3273 elem
3275 (pos (catch 'done
3276 ;; Note: Similar code in `c-safe-position'. The
3277 ;; difference is that we accept a safe position at
3278 ;; the point and don't bother to go forward past open
3279 ;; parens.
3280 (while paren-state
3281 (setq elem (car paren-state))
3282 (if (consp elem)
3283 (cond ((<= (cdr elem) (point))
3284 (throw 'done (cdr elem)))
3285 ((<= (car elem) (point))
3286 (throw 'done (car elem))))
3287 (if (<= elem (point))
3288 (throw 'done elem)))
3289 (setq paren-state (cdr paren-state)))
3290 (point-min))))
3292 (if (> pos (- (point) 4000))
3293 (goto-char pos)
3294 ;; The position is far back. Try `c-beginning-of-defun-1'
3295 ;; (although we can't be entirely sure it will go to a position
3296 ;; outside a comment or string in current emacsen). FIXME:
3297 ;; Consult `syntax-ppss' here.
3298 (c-beginning-of-defun-1)
3299 (if (< (point) pos)
3300 (goto-char pos)))))
3303 ;; Tools for scanning identifiers and other tokens.
3305 (defun c-on-identifier ()
3306 "Return non-nil if the point is on or directly after an identifier.
3307 Keywords are recognized and not considered identifiers. If an
3308 identifier is detected, the returned value is its starting position.
3309 If an identifier ends at the point and another begins at it \(can only
3310 happen in Pike) then the point for the preceding one is returned.
3312 Note that this function might do hidden buffer changes. See the
3313 comment at the start of cc-engine.el for more info."
3315 ;; FIXME: Shouldn't this function handle "operator" in C++?
3317 (save-excursion
3318 (skip-syntax-backward "w_")
3322 ;; Check for a normal (non-keyword) identifier.
3323 (and (looking-at c-symbol-start)
3324 (not (looking-at c-keywords-regexp))
3325 (point))
3327 (when (c-major-mode-is 'pike-mode)
3328 ;; Handle the `<operator> syntax in Pike.
3329 (let ((pos (point)))
3330 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3331 (and (if (< (skip-chars-backward "`") 0)
3333 (goto-char pos)
3334 (eq (char-after) ?\`))
3335 (looking-at c-symbol-key)
3336 (>= (match-end 0) pos)
3337 (point))))
3339 ;; Handle the "operator +" syntax in C++.
3340 (when (and c-overloadable-operators-regexp
3341 (= (c-backward-token-2 0) 0))
3343 (cond ((and (looking-at c-overloadable-operators-regexp)
3344 (or (not c-opt-op-identifier-prefix)
3345 (and (= (c-backward-token-2 1) 0)
3346 (looking-at c-opt-op-identifier-prefix))))
3347 (point))
3349 ((save-excursion
3350 (and c-opt-op-identifier-prefix
3351 (looking-at c-opt-op-identifier-prefix)
3352 (= (c-forward-token-2 1) 0)
3353 (looking-at c-overloadable-operators-regexp)))
3354 (point))))
3358 (defsubst c-simple-skip-symbol-backward ()
3359 ;; If the point is at the end of a symbol then skip backward to the
3360 ;; beginning of it. Don't move otherwise. Return non-nil if point
3361 ;; moved.
3363 ;; This function might do hidden buffer changes.
3364 (or (< (skip-syntax-backward "w_") 0)
3365 (and (c-major-mode-is 'pike-mode)
3366 ;; Handle the `<operator> syntax in Pike.
3367 (let ((pos (point)))
3368 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3369 (< (skip-chars-backward "`") 0)
3370 (looking-at c-symbol-key)
3371 (>= (match-end 0) pos))
3373 (goto-char pos)
3374 nil)))))
3376 (defun c-beginning-of-current-token (&optional back-limit)
3377 ;; Move to the beginning of the current token. Do not move if not
3378 ;; in the middle of one. BACK-LIMIT may be used to bound the
3379 ;; backward search; if given it's assumed to be at the boundary
3380 ;; between two tokens. Return non-nil if the point is moved, nil
3381 ;; otherwise.
3383 ;; This function might do hidden buffer changes.
3384 (let ((start (point)))
3385 (if (looking-at "\\w\\|\\s_")
3386 (skip-syntax-backward "w_" back-limit)
3387 (when (< (skip-syntax-backward ".()" back-limit) 0)
3388 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3389 (match-end 0))
3390 ;; `c-nonsymbol-token-regexp' should always match
3391 ;; since we've skipped backward over punctuator
3392 ;; or paren syntax, but consume one char in case
3393 ;; it doesn't so that we don't leave point before
3394 ;; some earlier incorrect token.
3395 (1+ (point)))))
3396 (if (<= pos start)
3397 (goto-char pos))))))
3398 (< (point) start)))
3400 (defun c-end-of-current-token (&optional back-limit)
3401 ;; Move to the end of the current token. Do not move if not in the
3402 ;; middle of one. BACK-LIMIT may be used to bound the backward
3403 ;; search; if given it's assumed to be at the boundary between two
3404 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3406 ;; This function might do hidden buffer changes.
3407 (let ((start (point)))
3408 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3409 (skip-syntax-forward "w_"))
3410 ((< (skip-syntax-backward ".()" back-limit) 0)
3411 (while (progn
3412 (if (looking-at c-nonsymbol-token-regexp)
3413 (goto-char (match-end 0))
3414 ;; `c-nonsymbol-token-regexp' should always match since
3415 ;; we've skipped backward over punctuator or paren
3416 ;; syntax, but move forward in case it doesn't so that
3417 ;; we don't leave point earlier than we started with.
3418 (forward-char))
3419 (< (point) start)))))
3420 (> (point) start)))
3422 (defconst c-jump-syntax-balanced
3423 (if (memq 'gen-string-delim c-emacs-features)
3424 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3425 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3427 (defconst c-jump-syntax-unbalanced
3428 (if (memq 'gen-string-delim c-emacs-features)
3429 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3430 "\\w\\|\\s_\\|\\s\""))
3432 (defun c-forward-token-2 (&optional count balanced limit)
3433 "Move forward by tokens.
3434 A token is defined as all symbols and identifiers which aren't
3435 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3436 treated properly). Point is always either left at the beginning of a
3437 token or not moved at all. COUNT specifies the number of tokens to
3438 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3439 moves to the next token beginning only if not already at one. If
3440 BALANCED is true, move over balanced parens, otherwise move into them.
3441 Also, if BALANCED is true, never move out of an enclosing paren.
3443 LIMIT sets the limit for the movement and defaults to the point limit.
3444 The case when LIMIT is set in the middle of a token, comment or macro
3445 is handled correctly, i.e. the point won't be left there.
3447 Return the number of tokens left to move \(positive or negative). If
3448 BALANCED is true, a move over a balanced paren counts as one. Note
3449 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3450 be returned. Thus, a return value of 0 guarantees that point is at
3451 the requested position and a return value less \(without signs) than
3452 COUNT guarantees that point is at the beginning of some token.
3454 Note that this function might do hidden buffer changes. See the
3455 comment at the start of cc-engine.el for more info."
3457 (or count (setq count 1))
3458 (if (< count 0)
3459 (- (c-backward-token-2 (- count) balanced limit))
3461 (let ((jump-syntax (if balanced
3462 c-jump-syntax-balanced
3463 c-jump-syntax-unbalanced))
3464 (last (point))
3465 (prev (point)))
3467 (if (zerop count)
3468 ;; If count is zero we should jump if in the middle of a token.
3469 (c-end-of-current-token))
3471 (save-restriction
3472 (if limit (narrow-to-region (point-min) limit))
3473 (if (/= (point)
3474 (progn (c-forward-syntactic-ws) (point)))
3475 ;; Skip whitespace. Count this as a move if we did in
3476 ;; fact move.
3477 (setq count (max (1- count) 0)))
3479 (if (eobp)
3480 ;; Moved out of bounds. Make sure the returned count isn't zero.
3481 (progn
3482 (if (zerop count) (setq count 1))
3483 (goto-char last))
3485 ;; Use `condition-case' to avoid having the limit tests
3486 ;; inside the loop.
3487 (condition-case nil
3488 (while (and
3489 (> count 0)
3490 (progn
3491 (setq last (point))
3492 (cond ((looking-at jump-syntax)
3493 (goto-char (scan-sexps (point) 1))
3495 ((looking-at c-nonsymbol-token-regexp)
3496 (goto-char (match-end 0))
3498 ;; `c-nonsymbol-token-regexp' above should always
3499 ;; match if there are correct tokens. Try to
3500 ;; widen to see if the limit was set in the
3501 ;; middle of one, else fall back to treating
3502 ;; the offending thing as a one character token.
3503 ((and limit
3504 (save-restriction
3505 (widen)
3506 (looking-at c-nonsymbol-token-regexp)))
3507 nil)
3509 (forward-char)
3510 t))))
3511 (c-forward-syntactic-ws)
3512 (setq prev last
3513 count (1- count)))
3514 (error (goto-char last)))
3516 (when (eobp)
3517 (goto-char prev)
3518 (setq count (1+ count)))))
3520 count)))
3522 (defun c-backward-token-2 (&optional count balanced limit)
3523 "Move backward by tokens.
3524 See `c-forward-token-2' for details."
3526 (or count (setq count 1))
3527 (if (< count 0)
3528 (- (c-forward-token-2 (- count) balanced limit))
3530 (or limit (setq limit (point-min)))
3531 (let ((jump-syntax (if balanced
3532 c-jump-syntax-balanced
3533 c-jump-syntax-unbalanced))
3534 (last (point)))
3536 (if (zerop count)
3537 ;; The count is zero so try to skip to the beginning of the
3538 ;; current token.
3539 (if (> (point)
3540 (progn (c-beginning-of-current-token) (point)))
3541 (if (< (point) limit)
3542 ;; The limit is inside the same token, so return 1.
3543 (setq count 1))
3545 ;; We're not in the middle of a token. If there's
3546 ;; whitespace after the point then we must move backward,
3547 ;; so set count to 1 in that case.
3548 (and (looking-at c-syntactic-ws-start)
3549 ;; If we're looking at a '#' that might start a cpp
3550 ;; directive then we have to do a more elaborate check.
3551 (or (/= (char-after) ?#)
3552 (not c-opt-cpp-prefix)
3553 (save-excursion
3554 (and (= (point)
3555 (progn (beginning-of-line)
3556 (looking-at "[ \t]*")
3557 (match-end 0)))
3558 (or (bobp)
3559 (progn (backward-char)
3560 (not (eq (char-before) ?\\)))))))
3561 (setq count 1))))
3563 ;; Use `condition-case' to avoid having to check for buffer
3564 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3565 (condition-case nil
3566 (while (and
3567 (> count 0)
3568 (progn
3569 (c-backward-syntactic-ws)
3570 (backward-char)
3571 (if (looking-at jump-syntax)
3572 (goto-char (scan-sexps (1+ (point)) -1))
3573 ;; This can be very inefficient if there's a long
3574 ;; sequence of operator tokens without any separation.
3575 ;; That doesn't happen in practice, anyway.
3576 (c-beginning-of-current-token))
3577 (>= (point) limit)))
3578 (setq last (point)
3579 count (1- count)))
3580 (error (goto-char last)))
3582 (if (< (point) limit)
3583 (goto-char last))
3585 count)))
3587 (defun c-forward-token-1 (&optional count balanced limit)
3588 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3589 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3590 characters are jumped over character by character. This function is
3591 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3592 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3593 (c-forward-token-2 count balanced limit)))
3595 (defun c-backward-token-1 (&optional count balanced limit)
3596 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3597 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3598 characters are jumped over character by character. This function is
3599 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3600 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3601 (c-backward-token-2 count balanced limit)))
3604 ;; Tools for doing searches restricted to syntactically relevant text.
3606 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3607 paren-level not-inside-token
3608 lookbehind-submatch)
3609 "Like `re-search-forward', but only report matches that are found
3610 in syntactically significant text. I.e. matches in comments, macros
3611 or string literals are ignored. The start point is assumed to be
3612 outside any comment, macro or string literal, or else the content of
3613 that region is taken as syntactically significant text.
3615 If PAREN-LEVEL is non-nil, an additional restriction is added to
3616 ignore matches in nested paren sexps. The search will also not go
3617 outside the current list sexp, which has the effect that if the point
3618 should be moved to BOUND when no match is found \(i.e. NOERROR is
3619 neither nil nor t), then it will be at the closing paren if the end of
3620 the current list sexp is encountered first.
3622 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3623 ignored. Things like multicharacter operators and special symbols
3624 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3625 constants.
3627 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3628 subexpression in REGEXP. The end of that submatch is used as the
3629 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3630 isn't used or if that subexpression didn't match then the start
3631 position of the whole match is used instead. The \"look behind\"
3632 subexpression is never tested before the starting position, so it
3633 might be a good idea to include \\=\\= as a match alternative in it.
3635 Optimization note: Matches might be missed if the \"look behind\"
3636 subexpression can match the end of nonwhite syntactic whitespace,
3637 i.e. the end of comments or cpp directives. This since the function
3638 skips over such things before resuming the search. It's on the other
3639 hand not safe to assume that the \"look behind\" subexpression never
3640 matches syntactic whitespace.
3642 Bug: Unbalanced parens inside cpp directives are currently not handled
3643 correctly \(i.e. they don't get ignored as they should) when
3644 PAREN-LEVEL is set.
3646 Note that this function might do hidden buffer changes. See the
3647 comment at the start of cc-engine.el for more info."
3649 (or bound (setq bound (point-max)))
3650 (if paren-level (setq paren-level -1))
3652 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3654 (let ((start (point))
3656 ;; Start position for the last search.
3657 search-pos
3658 ;; The `parse-partial-sexp' state between the start position
3659 ;; and the point.
3660 state
3661 ;; The current position after the last state update. The next
3662 ;; `parse-partial-sexp' continues from here.
3663 (state-pos (point))
3664 ;; The position at which to check the state and the state
3665 ;; there. This is separate from `state-pos' since we might
3666 ;; need to back up before doing the next search round.
3667 check-pos check-state
3668 ;; Last position known to end a token.
3669 (last-token-end-pos (point-min))
3670 ;; Set when a valid match is found.
3671 found)
3673 (condition-case err
3674 (while
3675 (and
3676 (progn
3677 (setq search-pos (point))
3678 (re-search-forward regexp bound noerror))
3680 (progn
3681 (setq state (parse-partial-sexp
3682 state-pos (match-beginning 0) paren-level nil state)
3683 state-pos (point))
3684 (if (setq check-pos (and lookbehind-submatch
3685 (or (not paren-level)
3686 (>= (car state) 0))
3687 (match-end lookbehind-submatch)))
3688 (setq check-state (parse-partial-sexp
3689 state-pos check-pos paren-level nil state))
3690 (setq check-pos state-pos
3691 check-state state))
3693 ;; NOTE: If we got a look behind subexpression and get
3694 ;; an insignificant match in something that isn't
3695 ;; syntactic whitespace (i.e. strings or in nested
3696 ;; parentheses), then we can never skip more than a
3697 ;; single character from the match start position
3698 ;; (i.e. `state-pos' here) before continuing the
3699 ;; search. That since the look behind subexpression
3700 ;; might match the end of the insignificant region in
3701 ;; the next search.
3703 (cond
3704 ((elt check-state 7)
3705 ;; Match inside a line comment. Skip to eol. Use
3706 ;; `re-search-forward' instead of `skip-chars-forward' to get
3707 ;; the right bound behavior.
3708 (re-search-forward "[\n\r]" bound noerror))
3710 ((elt check-state 4)
3711 ;; Match inside a block comment. Skip to the '*/'.
3712 (search-forward "*/" bound noerror))
3714 ((and (not (elt check-state 5))
3715 (eq (char-before check-pos) ?/)
3716 (not (c-get-char-property (1- check-pos) 'syntax-table))
3717 (memq (char-after check-pos) '(?/ ?*)))
3718 ;; Match in the middle of the opener of a block or line
3719 ;; comment.
3720 (if (= (char-after check-pos) ?/)
3721 (re-search-forward "[\n\r]" bound noerror)
3722 (search-forward "*/" bound noerror)))
3724 ;; The last `parse-partial-sexp' above might have
3725 ;; stopped short of the real check position if the end
3726 ;; of the current sexp was encountered in paren-level
3727 ;; mode. The checks above are always false in that
3728 ;; case, and since they can do better skipping in
3729 ;; lookbehind-submatch mode, we do them before
3730 ;; checking the paren level.
3732 ((and paren-level
3733 (/= (setq tmp (car check-state)) 0))
3734 ;; Check the paren level first since we're short of the
3735 ;; syntactic checking position if the end of the
3736 ;; current sexp was encountered by `parse-partial-sexp'.
3737 (if (> tmp 0)
3739 ;; Inside a nested paren sexp.
3740 (if lookbehind-submatch
3741 ;; See the NOTE above.
3742 (progn (goto-char state-pos) t)
3743 ;; Skip out of the paren quickly.
3744 (setq state (parse-partial-sexp state-pos bound 0 nil state)
3745 state-pos (point)))
3747 ;; Have exited the current paren sexp.
3748 (if noerror
3749 (progn
3750 ;; The last `parse-partial-sexp' call above
3751 ;; has left us just after the closing paren
3752 ;; in this case, so we can modify the bound
3753 ;; to leave the point at the right position
3754 ;; upon return.
3755 (setq bound (1- (point)))
3756 nil)
3757 (signal 'search-failed (list regexp)))))
3759 ((setq tmp (elt check-state 3))
3760 ;; Match inside a string.
3761 (if (or lookbehind-submatch
3762 (not (integerp tmp)))
3763 ;; See the NOTE above.
3764 (progn (goto-char state-pos) t)
3765 ;; Skip to the end of the string before continuing.
3766 (let ((ender (make-string 1 tmp)) (continue t))
3767 (while (if (search-forward ender bound noerror)
3768 (progn
3769 (setq state (parse-partial-sexp
3770 state-pos (point) nil nil state)
3771 state-pos (point))
3772 (elt state 3))
3773 (setq continue nil)))
3774 continue)))
3776 ((save-excursion
3777 (save-match-data
3778 (c-beginning-of-macro start)))
3779 ;; Match inside a macro. Skip to the end of it.
3780 (c-end-of-macro)
3781 (cond ((<= (point) bound) t)
3782 (noerror nil)
3783 (t (signal 'search-failed (list regexp)))))
3785 ((and not-inside-token
3786 (or (< check-pos last-token-end-pos)
3787 (< check-pos
3788 (save-excursion
3789 (goto-char check-pos)
3790 (save-match-data
3791 (c-end-of-current-token last-token-end-pos))
3792 (setq last-token-end-pos (point))))))
3793 ;; Inside a token.
3794 (if lookbehind-submatch
3795 ;; See the NOTE above.
3796 (goto-char state-pos)
3797 (goto-char (min last-token-end-pos bound))))
3800 ;; A real match.
3801 (setq found t)
3802 nil)))
3804 ;; Should loop to search again, but take care to avoid
3805 ;; looping on the same spot.
3806 (or (/= search-pos (point))
3807 (if (= (point) bound)
3808 (if noerror
3810 (signal 'search-failed (list regexp)))
3811 (forward-char)
3812 t))))
3814 (error
3815 (goto-char start)
3816 (signal (car err) (cdr err))))
3818 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3820 (if found
3821 (progn
3822 (goto-char (match-end 0))
3823 (match-end 0))
3825 ;; Search failed. Set point as appropriate.
3826 (if (eq noerror t)
3827 (goto-char start)
3828 (goto-char bound))
3829 nil)))
3831 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
3833 (defsubst c-ssb-lit-begin ()
3834 ;; Return the start of the literal point is in, or nil.
3835 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3836 ;; bound in the caller.
3838 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3839 ;; if it's outside comments and strings.
3840 (save-excursion
3841 (let ((pos (point)) safe-pos state pps-end-pos)
3842 ;; Pick a safe position as close to the point as possible.
3844 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3845 ;; position.
3847 (while (and safe-pos-list
3848 (> (car safe-pos-list) (point)))
3849 (setq safe-pos-list (cdr safe-pos-list)))
3850 (unless (setq safe-pos (car-safe safe-pos-list))
3851 (setq safe-pos (max (or (c-safe-position
3852 (point) (or c-state-cache
3853 (c-parse-state)))
3855 (point-min))
3856 safe-pos-list (list safe-pos)))
3858 ;; Cache positions along the way to use if we have to back up more. We
3859 ;; cache every closing paren on the same level. If the paren cache is
3860 ;; relevant in this region then we're typically already on the same
3861 ;; level as the target position. Note that we might cache positions
3862 ;; after opening parens in case safe-pos is in a nested list. That's
3863 ;; both uncommon and harmless.
3864 (while (progn
3865 (setq state (parse-partial-sexp
3866 safe-pos pos 0))
3867 (< (point) pos))
3868 (setq safe-pos (point)
3869 safe-pos-list (cons safe-pos safe-pos-list)))
3871 ;; If the state contains the start of the containing sexp we cache that
3872 ;; position too, so that parse-partial-sexp in the next run has a bigger
3873 ;; chance of starting at the same level as the target position and thus
3874 ;; will get more good safe positions into the list.
3875 (if (elt state 1)
3876 (setq safe-pos (1+ (elt state 1))
3877 safe-pos-list (cons safe-pos safe-pos-list)))
3879 (if (or (elt state 3) (elt state 4))
3880 ;; Inside string or comment. Continue search at the
3881 ;; beginning of it.
3882 (elt state 8)))))
3884 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3885 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3886 i.e. don't stop at positions inside syntactic whitespace or string
3887 literals. Preprocessor directives are also ignored, with the exception
3888 of the one that the point starts within, if any. If LIMIT is given,
3889 it's assumed to be at a syntactically relevant position.
3891 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3892 sexps, and the search will also not go outside the current paren sexp.
3893 However, if LIMIT or the buffer limit is reached inside a nested paren
3894 then the point will be left at the limit.
3896 Non-nil is returned if the point moved, nil otherwise.
3898 Note that this function might do hidden buffer changes. See the
3899 comment at the start of cc-engine.el for more info."
3901 (let ((start (point))
3902 state-2
3903 ;; A list of syntactically relevant positions in descending
3904 ;; order. It's used to avoid scanning repeatedly over
3905 ;; potentially large regions with `parse-partial-sexp' to verify
3906 ;; each position. Used in `c-ssb-lit-begin'
3907 safe-pos-list
3908 ;; The result from `c-beginning-of-macro' at the start position or the
3909 ;; start position itself if it isn't within a macro. Evaluated on
3910 ;; demand.
3911 start-macro-beg
3912 ;; The earliest position after the current one with the same paren
3913 ;; level. Used only when `paren-level' is set.
3914 lit-beg
3915 (paren-level-pos (point)))
3917 (while
3918 (progn
3919 ;; The next loop "tries" to find the end point each time round,
3920 ;; loops when it hasn't succeeded.
3921 (while
3922 (and
3923 (< (skip-chars-backward skip-chars limit) 0)
3925 (let ((pos (point)) state-2 pps-end-pos)
3927 (cond
3928 ;; Don't stop inside a literal
3929 ((setq lit-beg (c-ssb-lit-begin))
3930 (goto-char lit-beg)
3933 ((and paren-level
3934 (save-excursion
3935 (setq state-2 (parse-partial-sexp
3936 pos paren-level-pos -1)
3937 pps-end-pos (point))
3938 (/= (car state-2) 0)))
3939 ;; Not at the right level.
3941 (if (and (< (car state-2) 0)
3942 ;; We stop above if we go out of a paren.
3943 ;; Now check whether it precedes or is
3944 ;; nested in the starting sexp.
3945 (save-excursion
3946 (setq state-2
3947 (parse-partial-sexp
3948 pps-end-pos paren-level-pos
3949 nil nil state-2))
3950 (< (car state-2) 0)))
3952 ;; We've stopped short of the starting position
3953 ;; so the hit was inside a nested list. Go up
3954 ;; until we are at the right level.
3955 (condition-case nil
3956 (progn
3957 (goto-char (scan-lists pos -1
3958 (- (car state-2))))
3959 (setq paren-level-pos (point))
3960 (if (and limit (>= limit paren-level-pos))
3961 (progn
3962 (goto-char limit)
3963 nil)
3965 (error
3966 (goto-char (or limit (point-min)))
3967 nil))
3969 ;; The hit was outside the list at the start
3970 ;; position. Go to the start of the list and exit.
3971 (goto-char (1+ (elt state-2 1)))
3972 nil))
3974 ((c-beginning-of-macro limit)
3975 ;; Inside a macro.
3976 (if (< (point)
3977 (or start-macro-beg
3978 (setq start-macro-beg
3979 (save-excursion
3980 (goto-char start)
3981 (c-beginning-of-macro limit)
3982 (point)))))
3985 ;; It's inside the same macro we started in so it's
3986 ;; a relevant match.
3987 (goto-char pos)
3988 nil))))))
3990 (> (point)
3991 (progn
3992 ;; Skip syntactic ws afterwards so that we don't stop at the
3993 ;; end of a comment if `skip-chars' is something like "^/".
3994 (c-backward-syntactic-ws)
3995 (point)))))
3997 ;; We might want to extend this with more useful return values in
3998 ;; the future.
3999 (/= (point) start)))
4001 ;; The following is an alternative implementation of
4002 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4003 ;; track of the syntactic context. It turned out to be generally
4004 ;; slower than the one above which uses forward checks from earlier
4005 ;; safe positions.
4007 ;;(defconst c-ssb-stop-re
4008 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4009 ;; ;; stop at to avoid going into comments and literals.
4010 ;; (concat
4011 ;; ;; Match comment end syntax and string literal syntax. Also match
4012 ;; ;; '/' for block comment endings (not covered by comment end
4013 ;; ;; syntax).
4014 ;; "\\s>\\|/\\|\\s\""
4015 ;; (if (memq 'gen-string-delim c-emacs-features)
4016 ;; "\\|\\s|"
4017 ;; "")
4018 ;; (if (memq 'gen-comment-delim c-emacs-features)
4019 ;; "\\|\\s!"
4020 ;; "")))
4022 ;;(defconst c-ssb-stop-paren-re
4023 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4024 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4026 ;;(defconst c-ssb-sexp-end-re
4027 ;; ;; Regexp matching the ending syntax of a complex sexp.
4028 ;; (concat c-string-limit-regexp "\\|\\s)"))
4030 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4031 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4032 ;;i.e. don't stop at positions inside syntactic whitespace or string
4033 ;;literals. Preprocessor directives are also ignored. However, if the
4034 ;;point is within a comment, string literal or preprocessor directory to
4035 ;;begin with, its contents is treated as syntactically relevant chars.
4036 ;;If LIMIT is given, it limits the backward search and the point will be
4037 ;;left there if no earlier position is found.
4039 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4040 ;;sexps, and the search will also not go outside the current paren sexp.
4041 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4042 ;;then the point will be left at the limit.
4044 ;;Non-nil is returned if the point moved, nil otherwise.
4046 ;;Note that this function might do hidden buffer changes. See the
4047 ;;comment at the start of cc-engine.el for more info."
4049 ;; (save-restriction
4050 ;; (when limit
4051 ;; (narrow-to-region limit (point-max)))
4053 ;; (let ((start (point)))
4054 ;; (catch 'done
4055 ;; (while (let ((last-pos (point))
4056 ;; (stop-pos (progn
4057 ;; (skip-chars-backward skip-chars)
4058 ;; (point))))
4060 ;; ;; Skip back over the same region as
4061 ;; ;; `skip-chars-backward' above, but keep to
4062 ;; ;; syntactically relevant positions.
4063 ;; (goto-char last-pos)
4064 ;; (while (and
4065 ;; ;; `re-search-backward' with a single char regexp
4066 ;; ;; should be fast.
4067 ;; (re-search-backward
4068 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4069 ;; stop-pos 'move)
4071 ;; (progn
4072 ;; (cond
4073 ;; ((looking-at "\\s(")
4074 ;; ;; `paren-level' is set and we've found the
4075 ;; ;; start of the containing paren.
4076 ;; (forward-char)
4077 ;; (throw 'done t))
4079 ;; ((looking-at c-ssb-sexp-end-re)
4080 ;; ;; We're at the end of a string literal or paren
4081 ;; ;; sexp (if `paren-level' is set).
4082 ;; (forward-char)
4083 ;; (condition-case nil
4084 ;; (c-backward-sexp)
4085 ;; (error
4086 ;; (goto-char limit)
4087 ;; (throw 'done t))))
4089 ;; (t
4090 ;; (forward-char)
4091 ;; ;; At the end of some syntactic ws or possibly
4092 ;; ;; after a plain '/' operator.
4093 ;; (let ((pos (point)))
4094 ;; (c-backward-syntactic-ws)
4095 ;; (if (= pos (point))
4096 ;; ;; Was a plain '/' operator. Go past it.
4097 ;; (backward-char)))))
4099 ;; (> (point) stop-pos))))
4101 ;; ;; Now the point is either at `stop-pos' or at some
4102 ;; ;; position further back if `stop-pos' was at a
4103 ;; ;; syntactically irrelevant place.
4105 ;; ;; Skip additional syntactic ws so that we don't stop
4106 ;; ;; at the end of a comment if `skip-chars' is
4107 ;; ;; something like "^/".
4108 ;; (c-backward-syntactic-ws)
4110 ;; (< (point) stop-pos))))
4112 ;; ;; We might want to extend this with more useful return values
4113 ;; ;; in the future.
4114 ;; (/= (point) start))))
4117 ;; Tools for handling comments and string literals.
4119 (defun c-slow-in-literal (&optional lim detect-cpp)
4120 "Return the type of literal point is in, if any.
4121 The return value is `c' if in a C-style comment, `c++' if in a C++
4122 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4123 is non-nil and in a preprocessor line, or nil if somewhere else.
4124 Optional LIM is used as the backward limit of the search. If omitted,
4125 or nil, `c-beginning-of-defun' is used.
4127 The last point calculated is cached if the cache is enabled, i.e. if
4128 `c-in-literal-cache' is bound to a two element vector.
4130 Note that this function might do hidden buffer changes. See the
4131 comment at the start of cc-engine.el for more info."
4133 (if (and (vectorp c-in-literal-cache)
4134 (= (point) (aref c-in-literal-cache 0)))
4135 (aref c-in-literal-cache 1)
4136 (let ((rtn (save-excursion
4137 (let* ((pos (point))
4138 (lim (or lim (progn
4139 (c-beginning-of-syntax)
4140 (point))))
4141 (state (parse-partial-sexp lim pos)))
4142 (cond
4143 ((elt state 3) 'string)
4144 ((elt state 4) (if (elt state 7) 'c++ 'c))
4145 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
4146 (t nil))))))
4147 ;; cache this result if the cache is enabled
4148 (if (not c-in-literal-cache)
4149 (setq c-in-literal-cache (vector (point) rtn)))
4150 rtn)))
4152 ;; XEmacs has a built-in function that should make this much quicker.
4153 ;; I don't think we even need the cache, which makes our lives more
4154 ;; complicated anyway. In this case, lim is only used to detect
4155 ;; cpp directives.
4157 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
4158 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
4159 ;; Xemacs 21.4.4. It manifested itself thus:
4161 ;; Starting with an empty AWK Mode buffer, type
4162 ;; /regexp/ {<C-j>
4163 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
4165 ;; AWK Mode is designed such that when the first / is typed, it gets the
4166 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
4167 ;; are given the s-t property "string". However, buffer-syntactic-context
4168 ;; fails to take account of the change of the s-t property on the opening / to
4169 ;; "string", and reports that the { is within a string started by the second /.
4171 ;; The workaround for this is for the AWK Mode initialisation to switch the
4172 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
4173 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
4175 ;; (Alan Mackenzie, 2003/4/30).
4177 (defun c-fast-in-literal (&optional lim detect-cpp)
4178 ;; This function might do hidden buffer changes.
4179 (let ((context (buffer-syntactic-context)))
4180 (cond
4181 ((eq context 'string) 'string)
4182 ((eq context 'comment) 'c++)
4183 ((eq context 'block-comment) 'c)
4184 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
4186 (defalias 'c-in-literal
4187 (if (fboundp 'buffer-syntactic-context)
4188 'c-fast-in-literal ; XEmacs
4189 'c-slow-in-literal)) ; GNU Emacs
4191 ;; The defalias above isn't enough to shut up the byte compiler.
4192 (cc-bytecomp-defun c-in-literal)
4194 (defun c-literal-limits (&optional lim near not-in-delimiter)
4195 "Return a cons of the beginning and end positions of the comment or
4196 string surrounding point (including both delimiters), or nil if point
4197 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4198 to start parsing from. If NEAR is non-nil, then the limits of any
4199 literal next to point is returned. \"Next to\" means there's only
4200 spaces and tabs between point and the literal. The search for such a
4201 literal is done first in forward direction. If NOT-IN-DELIMITER is
4202 non-nil, the case when point is inside a starting delimiter won't be
4203 recognized. This only has effect for comments which have starting
4204 delimiters with more than one character.
4206 Note that this function might do hidden buffer changes. See the
4207 comment at the start of cc-engine.el for more info."
4209 (save-excursion
4210 (let* ((pos (point))
4211 (lim (or lim (progn
4212 (c-beginning-of-syntax)
4213 (point))))
4214 (state (parse-partial-sexp lim pos)))
4216 (cond ((elt state 3) ; String.
4217 (goto-char (elt state 8))
4218 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4219 (point-max))))
4221 ((elt state 4) ; Comment.
4222 (goto-char (elt state 8))
4223 (cons (point) (progn (c-forward-single-comment) (point))))
4225 ((and (not not-in-delimiter)
4226 (not (elt state 5))
4227 (eq (char-before) ?/)
4228 (looking-at "[/*]"))
4229 ;; We're standing in a comment starter.
4230 (backward-char 1)
4231 (cons (point) (progn (c-forward-single-comment) (point))))
4233 (near
4234 (goto-char pos)
4236 ;; Search forward for a literal.
4237 (skip-chars-forward " \t")
4239 (cond
4240 ((looking-at c-string-limit-regexp) ; String.
4241 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4242 (point-max))))
4244 ((looking-at c-comment-start-regexp) ; Line or block comment.
4245 (cons (point) (progn (c-forward-single-comment) (point))))
4248 ;; Search backward.
4249 (skip-chars-backward " \t")
4251 (let ((end (point)) beg)
4252 (cond
4253 ((save-excursion
4254 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4255 (setq beg (c-safe (c-backward-sexp 1) (point))))
4257 ((and (c-safe (forward-char -2) t)
4258 (looking-at "*/"))
4259 ;; Block comment. Due to the nature of line
4260 ;; comments, they will always be covered by the
4261 ;; normal case above.
4262 (goto-char end)
4263 (c-backward-single-comment)
4264 ;; If LIM is bogus, beg will be bogus.
4265 (setq beg (point))))
4267 (if beg (cons beg end))))))
4268 ))))
4270 ;; In case external callers use this; it did have a docstring.
4271 (defalias 'c-literal-limits-fast 'c-literal-limits)
4273 (defun c-collect-line-comments (range)
4274 "If the argument is a cons of two buffer positions (such as returned by
4275 `c-literal-limits'), and that range contains a C++ style line comment,
4276 then an extended range is returned that contains all adjacent line
4277 comments (i.e. all comments that starts in the same column with no
4278 empty lines or non-whitespace characters between them). Otherwise the
4279 argument is returned.
4281 Note that this function might do hidden buffer changes. See the
4282 comment at the start of cc-engine.el for more info."
4284 (save-excursion
4285 (condition-case nil
4286 (if (and (consp range) (progn
4287 (goto-char (car range))
4288 (looking-at c-line-comment-starter)))
4289 (let ((col (current-column))
4290 (beg (point))
4291 (bopl (c-point 'bopl))
4292 (end (cdr range)))
4293 ;; Got to take care in the backward direction to handle
4294 ;; comments which are preceded by code.
4295 (while (and (c-backward-single-comment)
4296 (>= (point) bopl)
4297 (looking-at c-line-comment-starter)
4298 (= col (current-column)))
4299 (setq beg (point)
4300 bopl (c-point 'bopl)))
4301 (goto-char end)
4302 (while (and (progn (skip-chars-forward " \t")
4303 (looking-at c-line-comment-starter))
4304 (= col (current-column))
4305 (prog1 (zerop (forward-line 1))
4306 (setq end (point)))))
4307 (cons beg end))
4308 range)
4309 (error range))))
4311 (defun c-literal-type (range)
4312 "Convenience function that given the result of `c-literal-limits',
4313 returns nil or the type of literal that the range surrounds, one
4314 of the symbols 'c, 'c++ or 'string. It's much faster than using
4315 `c-in-literal' and is intended to be used when you need both the
4316 type of a literal and its limits.
4318 Note that this function might do hidden buffer changes. See the
4319 comment at the start of cc-engine.el for more info."
4321 (if (consp range)
4322 (save-excursion
4323 (goto-char (car range))
4324 (cond ((looking-at c-string-limit-regexp) 'string)
4325 ((or (looking-at "//") ; c++ line comment
4326 (and (looking-at "\\s<") ; comment starter
4327 (looking-at "#"))) ; awk comment.
4328 'c++)
4329 (t 'c))) ; Assuming the range is valid.
4330 range))
4333 ;; `c-find-decl-spots' and accompanying stuff.
4335 ;; Variables used in `c-find-decl-spots' to cache the search done for
4336 ;; the first declaration in the last call. When that function starts,
4337 ;; it needs to back up over syntactic whitespace to look at the last
4338 ;; token before the region being searched. That can sometimes cause
4339 ;; moves back and forth over a quite large region of comments and
4340 ;; macros, which would be repeated for each changed character when
4341 ;; we're called during fontification, since font-lock refontifies the
4342 ;; current line for each change. Thus it's worthwhile to cache the
4343 ;; first match.
4345 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4346 ;; the syntactic whitespace less or equal to some start position.
4347 ;; There's no cached value if it's nil.
4349 ;; `c-find-decl-match-pos' is the match position if
4350 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4351 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4352 (defvar c-find-decl-syntactic-pos nil)
4353 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4354 (defvar c-find-decl-match-pos nil)
4355 (make-variable-buffer-local 'c-find-decl-match-pos)
4357 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4358 (and c-find-decl-syntactic-pos
4359 (< change-min-pos c-find-decl-syntactic-pos)
4360 (setq c-find-decl-syntactic-pos nil)))
4362 ; (defface c-debug-decl-spot-face
4363 ; '((t (:background "Turquoise")))
4364 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4365 ; (defface c-debug-decl-sws-face
4366 ; '((t (:background "Khaki")))
4367 ; "Debug face to mark the syntactic whitespace between the declaration
4368 ; spots and the preceding token end.")
4370 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4371 (when (facep 'c-debug-decl-spot-face)
4372 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4373 (c-debug-add-face (max match-pos (point-min)) decl-pos
4374 'c-debug-decl-sws-face)
4375 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4376 'c-debug-decl-spot-face))))
4377 (defmacro c-debug-remove-decl-spot-faces (beg end)
4378 (when (facep 'c-debug-decl-spot-face)
4379 `(c-save-buffer-state ()
4380 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4381 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4383 (defmacro c-find-decl-prefix-search ()
4384 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4385 ;; but it contains lots of free variables that refer to things
4386 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4387 ;; if there is a match, otherwise at `cfd-limit'.
4389 ;; This macro might do hidden buffer changes.
4391 '(progn
4392 ;; Find the next property match position if we haven't got one already.
4393 (unless cfd-prop-match
4394 (save-excursion
4395 (while (progn
4396 (goto-char (next-single-property-change
4397 (point) 'c-type nil cfd-limit))
4398 (and (< (point) cfd-limit)
4399 (not (eq (c-get-char-property (1- (point)) 'c-type)
4400 'c-decl-end)))))
4401 (setq cfd-prop-match (point))))
4403 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4404 ;; got one already.
4405 (unless cfd-re-match
4407 (if (> cfd-re-match-end (point))
4408 (goto-char cfd-re-match-end))
4410 (while (if (setq cfd-re-match-end
4411 (re-search-forward c-decl-prefix-or-start-re
4412 cfd-limit 'move))
4414 ;; Match. Check if it's inside a comment or string literal.
4415 (c-got-face-at
4416 (if (setq cfd-re-match (match-end 1))
4417 ;; Matched the end of a token preceding a decl spot.
4418 (progn
4419 (goto-char cfd-re-match)
4420 (1- cfd-re-match))
4421 ;; Matched a token that start a decl spot.
4422 (goto-char (match-beginning 0))
4423 (point))
4424 c-literal-faces)
4426 ;; No match. Finish up and exit the loop.
4427 (setq cfd-re-match cfd-limit)
4428 nil)
4430 ;; Skip out of comments and string literals.
4431 (while (progn
4432 (goto-char (next-single-property-change
4433 (point) 'face nil cfd-limit))
4434 (and (< (point) cfd-limit)
4435 (c-got-face-at (point) c-literal-faces)))))
4437 ;; If we matched at the decl start, we have to back up over the
4438 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4439 ;; any decl spots in the syntactic ws.
4440 (unless cfd-re-match
4441 (c-backward-syntactic-ws)
4442 (setq cfd-re-match (point))))
4444 ;; Choose whichever match is closer to the start.
4445 (if (< cfd-re-match cfd-prop-match)
4446 (setq cfd-match-pos cfd-re-match
4447 cfd-re-match nil)
4448 (setq cfd-match-pos cfd-prop-match
4449 cfd-prop-match nil))
4451 (goto-char cfd-match-pos)
4453 (when (< cfd-match-pos cfd-limit)
4454 ;; Skip forward past comments only so we don't skip macros.
4455 (c-forward-comments)
4456 ;; Set the position to continue at. We can avoid going over
4457 ;; the comments skipped above a second time, but it's possible
4458 ;; that the comment skipping has taken us past `cfd-prop-match'
4459 ;; since the property might be used inside comments.
4460 (setq cfd-continue-pos (if cfd-prop-match
4461 (min cfd-prop-match (point))
4462 (point))))))
4464 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4465 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4466 ;; label from the point to CFD-LIMIT.
4468 ;; CFD-FUN is called with point at the start of the spot. It's
4469 ;; passed two arguments: The first is the end position of the token
4470 ;; preceding the spot, or 0 for the implicit match at bob. The
4471 ;; second is a flag that is t when the match is inside a macro. If
4472 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
4473 ;; spot, it should return non-nil to ensure that the next search
4474 ;; will find them.
4476 ;; Such a spot is:
4477 ;; o The first token after bob.
4478 ;; o The first token after the end of submatch 1 in
4479 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4480 ;; o The start of each `c-decl-prefix-or-start-re' match when
4481 ;; submatch 1 doesn't match.
4482 ;; o The first token after the end of each occurrence of the
4483 ;; `c-type' text property with the value `c-decl-end', provided
4484 ;; `c-type-decl-end-used' is set.
4486 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4487 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4488 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4490 ;; If the match is inside a macro then the buffer is narrowed to the
4491 ;; end of it, so that CFD-FUN can investigate the following tokens
4492 ;; without matching something that begins inside a macro and ends
4493 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4494 ;; CFD-FACE-CHECKLIST checks exist.
4496 ;; The spots are visited approximately in order from top to bottom.
4497 ;; It's however the positions where `c-decl-prefix-or-start-re'
4498 ;; matches and where `c-decl-end' properties are found that are in
4499 ;; order. Since the spots often are at the following token, they
4500 ;; might be visited out of order insofar as more spots are reported
4501 ;; later on within the syntactic whitespace between the match
4502 ;; positions and their spots.
4504 ;; It's assumed that comments and strings are fontified in the
4505 ;; searched range.
4507 ;; This is mainly used in fontification, and so has an elaborate
4508 ;; cache to handle repeated calls from the same start position; see
4509 ;; the variables above.
4511 ;; All variables in this function begin with `cfd-' to avoid name
4512 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4514 ;; This function might do hidden buffer changes.
4516 (let ((cfd-start-pos (point))
4517 (cfd-buffer-end (point-max))
4518 ;; The end of the token preceding the decl spot last found
4519 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4520 ;; no match.
4521 cfd-re-match
4522 ;; The end position of the last `c-decl-prefix-or-start-re'
4523 ;; match. If this is greater than `cfd-continue-pos', the
4524 ;; next regexp search is started here instead.
4525 (cfd-re-match-end (point-min))
4526 ;; The end of the last `c-decl-end' found by
4527 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4528 ;; match. If searching for the property isn't needed then we
4529 ;; disable it by setting it to `cfd-limit' directly.
4530 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4531 ;; The end of the token preceding the decl spot last found by
4532 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4533 ;; bob. `cfd-limit' if there's no match. In other words,
4534 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4535 (cfd-match-pos cfd-limit)
4536 ;; The position to continue searching at.
4537 cfd-continue-pos
4538 ;; The position of the last "real" token we've stopped at.
4539 ;; This can be greater than `cfd-continue-pos' when we get
4540 ;; hits inside macros or at `c-decl-end' positions inside
4541 ;; comments.
4542 (cfd-token-pos 0)
4543 ;; The end position of the last entered macro.
4544 (cfd-macro-end 0))
4546 ;; Initialize by finding a syntactically relevant start position
4547 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4548 ;; search unless we're at bob.
4550 (let (start-in-literal start-in-macro syntactic-pos)
4551 ;; Must back up a bit since we look for the end of the previous
4552 ;; statement or declaration, which is earlier than the first
4553 ;; returned match.
4555 (cond
4556 ;; First we need to move to a syntactically relevant position.
4557 ;; Begin by backing out of comment or string literals.
4558 ((and
4559 (when (c-got-face-at (point) c-literal-faces)
4560 ;; Try to use the faces to back up to the start of the
4561 ;; literal. FIXME: What if the point is on a declaration
4562 ;; inside a comment?
4563 (while (and (not (bobp))
4564 (c-got-face-at (1- (point)) c-literal-faces))
4565 (goto-char (previous-single-property-change
4566 (point) 'face nil (point-min))))
4568 ;; XEmacs doesn't fontify the quotes surrounding string
4569 ;; literals.
4570 (and (featurep 'xemacs)
4571 (eq (get-text-property (point) 'face)
4572 'font-lock-string-face)
4573 (not (bobp))
4574 (progn (backward-char)
4575 (not (looking-at c-string-limit-regexp)))
4576 (forward-char))
4578 ;; Don't trust the literal to contain only literal faces
4579 ;; (the font lock package might not have fontified the
4580 ;; start of it at all, for instance) so check that we have
4581 ;; arrived at something that looks like a start or else
4582 ;; resort to `c-literal-limits'.
4583 (unless (looking-at c-literal-start-regexp)
4584 (let ((range (c-literal-limits)))
4585 (if range (goto-char (car range)))))
4587 (setq start-in-literal (point)))
4589 ;; The start is in a literal. If the limit is in the same
4590 ;; one we don't have to find a syntactic position etc. We
4591 ;; only check that if the limit is at or before bonl to save
4592 ;; time; it covers the by far most common case when font-lock
4593 ;; refontifies the current line only.
4594 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4595 (save-excursion
4596 (goto-char cfd-start-pos)
4597 (while (progn
4598 (goto-char (next-single-property-change
4599 (point) 'face nil cfd-limit))
4600 (and (< (point) cfd-limit)
4601 (c-got-face-at (point) c-literal-faces))))
4602 (= (point) cfd-limit)))
4604 ;; Completely inside a literal. Set up variables to trig the
4605 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4606 ;; find a suitable start position.
4607 (setq cfd-continue-pos start-in-literal))
4609 ;; Check if the region might be completely inside a macro, to
4610 ;; optimize that like the completely-inside-literal above.
4611 ((save-excursion
4612 (and (= (forward-line 1) 0)
4613 (bolp) ; forward-line has funny behavior at eob.
4614 (>= (point) cfd-limit)
4615 (progn (backward-char)
4616 (eq (char-before) ?\\))))
4617 ;; (Maybe) completely inside a macro. Only need to trig the
4618 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4619 ;; set things up.
4620 (setq cfd-continue-pos (1- cfd-start-pos)
4621 start-in-macro t))
4624 ;; Back out of any macro so we don't miss any declaration
4625 ;; that could follow after it.
4626 (when (c-beginning-of-macro)
4627 (setq start-in-macro t))
4629 ;; Now we're at a proper syntactically relevant position so we
4630 ;; can use the cache. But first clear it if it applied
4631 ;; further down.
4632 (c-invalidate-find-decl-cache cfd-start-pos)
4634 (setq syntactic-pos (point))
4635 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
4636 ;; Don't have to do this if the cache is relevant here,
4637 ;; typically if the same line is refontified again. If
4638 ;; we're just some syntactic whitespace further down we can
4639 ;; still use the cache to limit the skipping.
4640 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
4642 ;; If we hit `c-find-decl-syntactic-pos' and
4643 ;; `c-find-decl-match-pos' is set then we install the cached
4644 ;; values. If we hit `c-find-decl-syntactic-pos' and
4645 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4646 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4647 ;; and so we can continue the search from this point. If we
4648 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4649 ;; the right spot to begin searching anyway.
4650 (if (and (eq (point) c-find-decl-syntactic-pos)
4651 c-find-decl-match-pos)
4652 (setq cfd-match-pos c-find-decl-match-pos
4653 cfd-continue-pos syntactic-pos)
4655 (setq c-find-decl-syntactic-pos syntactic-pos)
4657 (when (if (bobp)
4658 ;; Always consider bob a match to get the first
4659 ;; declaration in the file. Do this separately instead of
4660 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4661 ;; regexp always can consume at least one character to
4662 ;; ensure that we won't get stuck in an infinite loop.
4663 (setq cfd-re-match 0)
4664 (backward-char)
4665 (c-beginning-of-current-token)
4666 (< (point) cfd-limit))
4667 ;; Do an initial search now. In the bob case above it's
4668 ;; only done to search for a `c-decl-end' spot.
4669 (c-find-decl-prefix-search))
4671 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
4672 cfd-match-pos)))))
4674 ;; Advance `cfd-continue-pos' if it's before the start position.
4675 ;; The closest continue position that might have effect at or
4676 ;; after the start depends on what we started in. This also
4677 ;; finds a suitable start position in the special cases when the
4678 ;; region is completely within a literal or macro.
4679 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
4681 (cond
4682 (start-in-macro
4683 ;; If we're in a macro then it's the closest preceding token
4684 ;; in the macro. Check this before `start-in-literal',
4685 ;; since if we're inside a literal in a macro, the preceding
4686 ;; token is earlier than any `c-decl-end' spot inside the
4687 ;; literal (comment).
4688 (goto-char (or start-in-literal cfd-start-pos))
4689 ;; The only syntactic ws in macros are comments.
4690 (c-backward-comments)
4691 (backward-char)
4692 (c-beginning-of-current-token))
4694 (start-in-literal
4695 ;; If we're in a comment it can only be the closest
4696 ;; preceding `c-decl-end' position within that comment, if
4697 ;; any. Go back to the beginning of such a property so that
4698 ;; `c-find-decl-prefix-search' will find the end of it.
4699 ;; (Can't stop at the end and install it directly on
4700 ;; `cfd-prop-match' since that variable might be cleared
4701 ;; after `cfd-fun' below.)
4703 ;; Note that if the literal is a string then the property
4704 ;; search will simply skip to the beginning of it right
4705 ;; away.
4706 (if (not c-type-decl-end-used)
4707 (goto-char start-in-literal)
4708 (goto-char cfd-start-pos)
4709 (while (progn
4710 (goto-char (previous-single-property-change
4711 (point) 'c-type nil start-in-literal))
4712 (and (> (point) start-in-literal)
4713 (not (eq (c-get-char-property (point) 'c-type)
4714 'c-decl-end))))))
4716 (when (= (point) start-in-literal)
4717 ;; Didn't find any property inside the comment, so we can
4718 ;; skip it entirely. (This won't skip past a string, but
4719 ;; that'll be handled quickly by the next
4720 ;; `c-find-decl-prefix-search' anyway.)
4721 (c-forward-single-comment)
4722 (if (> (point) cfd-limit)
4723 (goto-char cfd-limit))))
4726 ;; If we started in normal code, the only match that might
4727 ;; apply before the start is what we already got in
4728 ;; `cfd-match-pos' so we can continue at the start position.
4729 ;; (Note that we don't get here if the first match is below
4730 ;; it.)
4731 (goto-char cfd-start-pos)))
4733 ;; Delete found matches if they are before our new continue
4734 ;; position, so that `c-find-decl-prefix-search' won't back up
4735 ;; to them later on.
4736 (setq cfd-continue-pos (point))
4737 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
4738 (setq cfd-re-match nil))
4739 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
4740 (setq cfd-prop-match nil)))
4742 (if syntactic-pos
4743 ;; This is the normal case and we got a proper syntactic
4744 ;; position. If there's a match then it's always outside
4745 ;; macros and comments, so advance to the next token and set
4746 ;; `cfd-token-pos'. The loop below will later go back using
4747 ;; `cfd-continue-pos' to fix declarations inside the
4748 ;; syntactic ws.
4749 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
4750 (goto-char syntactic-pos)
4751 (c-forward-syntactic-ws)
4752 (and cfd-continue-pos
4753 (< cfd-continue-pos (point))
4754 (setq cfd-token-pos (point))))
4756 ;; Have one of the special cases when the region is completely
4757 ;; within a literal or macro. `cfd-continue-pos' is set to a
4758 ;; good start position for the search, so do it.
4759 (c-find-decl-prefix-search)))
4761 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4763 (while (progn
4764 (while (and
4765 (< cfd-match-pos cfd-limit)
4768 ;; Kludge to filter out matches on the "<" that
4769 ;; aren't open parens, for the sake of languages
4770 ;; that got `c-recognize-<>-arglists' set.
4771 (and (eq (char-before cfd-match-pos) ?<)
4772 (not (c-get-char-property (1- cfd-match-pos)
4773 'syntax-table)))
4775 ;; If `cfd-continue-pos' is less or equal to
4776 ;; `cfd-token-pos', we've got a hit inside a macro
4777 ;; that's in the syntactic whitespace before the last
4778 ;; "real" declaration we've checked. If they're equal
4779 ;; we've arrived at the declaration a second time, so
4780 ;; there's nothing to do.
4781 (= cfd-continue-pos cfd-token-pos)
4783 (progn
4784 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4785 ;; we're still searching for declarations embedded in
4786 ;; the syntactic whitespace. In that case we need
4787 ;; only to skip comments and not macros, since they
4788 ;; can't be nested, and that's already been done in
4789 ;; `c-find-decl-prefix-search'.
4790 (when (> cfd-continue-pos cfd-token-pos)
4791 (c-forward-syntactic-ws)
4792 (setq cfd-token-pos (point)))
4794 ;; Continue if the following token fails the
4795 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4796 (when (or (>= (point) cfd-limit)
4797 (not (looking-at cfd-decl-re))
4798 (and cfd-face-checklist
4799 (not (c-got-face-at
4800 (point) cfd-face-checklist))))
4801 (goto-char cfd-continue-pos)
4802 t)))
4804 (< (point) cfd-limit))
4805 (c-find-decl-prefix-search))
4807 (< (point) cfd-limit))
4809 (when (and
4810 (>= (point) cfd-start-pos)
4812 (progn
4813 ;; Narrow to the end of the macro if we got a hit inside
4814 ;; one, to avoid recognizing things that start inside the
4815 ;; macro and end outside it.
4816 (when (> cfd-match-pos cfd-macro-end)
4817 ;; Not in the same macro as in the previous round.
4818 (save-excursion
4819 (goto-char cfd-match-pos)
4820 (setq cfd-macro-end
4821 (if (save-excursion (and (c-beginning-of-macro)
4822 (< (point) cfd-match-pos)))
4823 (progn (c-end-of-macro)
4824 (point))
4825 0))))
4827 (if (zerop cfd-macro-end)
4829 (if (> cfd-macro-end (point))
4830 (progn (narrow-to-region (point-min) cfd-macro-end)
4832 ;; The matched token was the last thing in the macro,
4833 ;; so the whole match is bogus.
4834 (setq cfd-macro-end 0)
4835 nil))))
4837 (c-debug-put-decl-spot-faces cfd-match-pos (point))
4838 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
4839 (setq cfd-prop-match nil))
4841 (when (/= cfd-macro-end 0)
4842 ;; Restore limits if we did macro narrowment above.
4843 (narrow-to-region (point-min) cfd-buffer-end)))
4845 (goto-char cfd-continue-pos)
4846 (if (= cfd-continue-pos cfd-limit)
4847 (setq cfd-match-pos cfd-limit)
4848 (c-find-decl-prefix-search)))))
4851 ;; A cache for found types.
4853 ;; Buffer local variable that contains an obarray with the types we've
4854 ;; found. If a declaration is recognized somewhere we record the
4855 ;; fully qualified identifier in it to recognize it as a type
4856 ;; elsewhere in the file too. This is not accurate since we do not
4857 ;; bother with the scoping rules of the languages, but in practice the
4858 ;; same name is seldom used as both a type and something else in a
4859 ;; file, and we only use this as a last resort in ambiguous cases (see
4860 ;; `c-forward-decl-or-cast-1').
4862 ;; Not every type need be in this cache. However, things which have
4863 ;; ceased to be types must be removed from it.
4865 ;; Template types in C++ are added here too but with the template
4866 ;; arglist replaced with "<>" in references or "<" for the one in the
4867 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4868 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4869 ;; template specs can be fairly sized programs in themselves) and
4870 ;; improves the hit ratio (it's a type regardless of the template
4871 ;; args; it's just not the same type, but we're only interested in
4872 ;; recognizing types, not telling distinct types apart). Note that
4873 ;; template types in references are added here too; from the example
4874 ;; above there will also be an entry "Foo<".
4875 (defvar c-found-types nil)
4876 (make-variable-buffer-local 'c-found-types)
4878 (defsubst c-clear-found-types ()
4879 ;; Clears `c-found-types'.
4880 (setq c-found-types (make-vector 53 0)))
4882 (defun c-add-type (from to)
4883 ;; Add the given region as a type in `c-found-types'. If the region
4884 ;; doesn't match an existing type but there is a type which is equal
4885 ;; to the given one except that the last character is missing, then
4886 ;; the shorter type is removed. That's done to avoid adding all
4887 ;; prefixes of a type as it's being entered and font locked. This
4888 ;; doesn't cover cases like when characters are removed from a type
4889 ;; or added in the middle. We'd need the position of point when the
4890 ;; font locking is invoked to solve this well.
4892 ;; This function might do hidden buffer changes.
4893 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4894 (unless (intern-soft type c-found-types)
4895 (unintern (substring type 0 -1) c-found-types)
4896 (intern type c-found-types))))
4898 (defun c-unfind-type (name)
4899 ;; Remove the "NAME" from c-found-types, if present.
4900 (unintern name c-found-types))
4902 (defsubst c-check-type (from to)
4903 ;; Return non-nil if the given region contains a type in
4904 ;; `c-found-types'.
4906 ;; This function might do hidden buffer changes.
4907 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4908 c-found-types))
4910 (defun c-list-found-types ()
4911 ;; Return all the types in `c-found-types' as a sorted list of
4912 ;; strings.
4913 (let (type-list)
4914 (mapatoms (lambda (type)
4915 (setq type-list (cons (symbol-name type)
4916 type-list)))
4917 c-found-types)
4918 (sort type-list 'string-lessp)))
4920 ;; Shut up the byte compiler.
4921 (defvar c-maybe-stale-found-type)
4923 (defun c-trim-found-types (beg end old-len)
4924 ;; An after change function which, in conjunction with the info in
4925 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4926 ;; from `c-found-types', should this type have become stale. For
4927 ;; example, this happens to "foo" when "foo \n bar();" becomes
4928 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4929 ;; the fontification.
4931 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4932 ;; type?
4933 (when (> end beg)
4934 (save-excursion
4935 (when (< end (point-max))
4936 (goto-char end)
4937 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4938 (progn (goto-char end)
4939 (c-end-of-current-token)))
4940 (c-unfind-type (buffer-substring-no-properties
4941 end (point)))))
4942 (when (> beg (point-min))
4943 (goto-char beg)
4944 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4945 (progn (goto-char beg)
4946 (c-beginning-of-current-token)))
4947 (c-unfind-type (buffer-substring-no-properties
4948 (point) beg))))))
4950 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4951 (cond
4952 ;; Changing the amount of (already existing) whitespace - don't do anything.
4953 ((and (c-partial-ws-p beg end)
4954 (or (= beg end) ; removal of WS
4955 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4957 ;; The syntactic relationship which defined a "found type" has been
4958 ;; destroyed.
4959 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4960 (c-unfind-type (cadr c-maybe-stale-found-type)))
4961 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4965 ;; Setting and removing syntax properties on < and > in languages (C++
4966 ;; and Java) where they can be template/generic delimiters as well as
4967 ;; their normal meaning of "less/greater than".
4969 ;; Normally, < and > have syntax 'punctuation'. When they are found to
4970 ;; be delimiters, they are marked as such with the category properties
4971 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
4973 ;; STRATEGY:
4975 ;; It is impossible to determine with certainty whether a <..> pair in
4976 ;; C++ is two comparison operators or is template delimiters, unless
4977 ;; one duplicates a lot of a C++ compiler. For example, the following
4978 ;; code fragment:
4980 ;; foo (a < b, c > d) ;
4982 ;; could be a function call with two integer parameters (each a
4983 ;; relational expression), or it could be a constructor for class foo
4984 ;; taking one parameter d of templated type "a < b, c >". They are
4985 ;; somewhat easier to distinguish in Java.
4987 ;; The strategy now (2010-01) adopted is to mark and unmark < and
4988 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
4989 ;; individually when their context so indicated. This gave rise to
4990 ;; intractible problems when one of a matching pair was deleted, or
4991 ;; pulled into a literal.]
4993 ;; At each buffer change, the syntax-table properties are removed in a
4994 ;; before-change function and reapplied, when needed, in an
4995 ;; after-change function. It is far more important that the
4996 ;; properties get removed when they they are spurious than that they
4997 ;; be present when wanted.
4998 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4999 (defun c-clear-<-pair-props (&optional pos)
5000 ;; POS (default point) is at a < character. If it is marked with
5001 ;; open paren syntax-table text property, remove the property,
5002 ;; together with the close paren property on the matching > (if
5003 ;; any).
5004 (save-excursion
5005 (if pos
5006 (goto-char pos)
5007 (setq pos (point)))
5008 (when (equal (c-get-char-property (point) 'syntax-table)
5009 c-<-as-paren-syntax)
5010 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5011 (c-go-list-forward))
5012 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5013 c->-as-paren-syntax) ; should always be true.
5014 (c-clear-char-property (1- (point)) 'category))
5015 (c-clear-char-property pos 'category))))
5017 (defun c-clear->-pair-props (&optional pos)
5018 ;; POS (default point) is at a > character. If it is marked with
5019 ;; close paren syntax-table property, remove the property, together
5020 ;; with the open paren property on the matching < (if any).
5021 (save-excursion
5022 (if pos
5023 (goto-char pos)
5024 (setq pos (point)))
5025 (when (equal (c-get-char-property (point) 'syntax-table)
5026 c->-as-paren-syntax)
5027 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5028 (c-go-up-list-backward))
5029 (when (equal (c-get-char-property (point) 'syntax-table)
5030 c-<-as-paren-syntax) ; should always be true.
5031 (c-clear-char-property (point) 'category))
5032 (c-clear-char-property pos 'category))))
5034 (defun c-clear-<>-pair-props (&optional pos)
5035 ;; POS (default point) is at a < or > character. If it has an
5036 ;; open/close paren syntax-table property, remove this property both
5037 ;; from the current character and its partner (which will also be
5038 ;; thusly marked).
5039 (cond
5040 ((eq (char-after) ?\<)
5041 (c-clear-<-pair-props pos))
5042 ((eq (char-after) ?\>)
5043 (c-clear->-pair-props pos))
5044 (t (c-benign-error
5045 "c-clear-<>-pair-props called from wrong position"))))
5047 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5048 ;; POS (default point) is at a < character. If it is both marked
5049 ;; with open/close paren syntax-table property, and has a matching >
5050 ;; (also marked) which is after LIM, remove the property both from
5051 ;; the current > and its partner. Return t when this happens, nil
5052 ;; when it doesn't.
5053 (save-excursion
5054 (if pos
5055 (goto-char pos)
5056 (setq pos (point)))
5057 (when (equal (c-get-char-property (point) 'syntax-table)
5058 c-<-as-paren-syntax)
5059 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5060 (c-go-list-forward))
5061 (when (and (>= (point) lim)
5062 (equal (c-get-char-property (1- (point)) 'syntax-table)
5063 c->-as-paren-syntax)) ; should always be true.
5064 (c-unmark-<->-as-paren (1- (point)))
5065 (c-unmark-<->-as-paren pos))
5066 t)))
5068 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
5069 ;; POS (default point) is at a > character. If it is both marked
5070 ;; with open/close paren syntax-table property, and has a matching <
5071 ;; (also marked) which is before LIM, remove the property both from
5072 ;; the current < and its partner. Return t when this happens, nil
5073 ;; when it doesn't.
5074 (save-excursion
5075 (if pos
5076 (goto-char pos)
5077 (setq pos (point)))
5078 (when (equal (c-get-char-property (point) 'syntax-table)
5079 c->-as-paren-syntax)
5080 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5081 (c-go-up-list-backward))
5082 (when (and (<= (point) lim)
5083 (equal (c-get-char-property (point) 'syntax-table)
5084 c-<-as-paren-syntax)) ; should always be true.
5085 (c-unmark-<->-as-paren (point))
5086 (c-unmark-<->-as-paren pos))
5087 t)))
5089 ;; Set by c-common-init in cc-mode.el.
5090 (defvar c-new-BEG)
5091 (defvar c-new-END)
5093 (defun c-before-change-check-<>-operators (beg end)
5094 ;; Unmark certain pairs of "< .... >" which are currently marked as
5095 ;; template/generic delimiters. (This marking is via syntax-table
5096 ;; text properties).
5098 ;; These pairs are those which are in the current "statement" (i.e.,
5099 ;; the region between the {, }, or ; before BEG and the one after
5100 ;; END), and which enclose any part of the interval (BEG END).
5102 ;; Note that in C++ (?and Java), template/generic parens cannot
5103 ;; enclose a brace or semicolon, so we use these as bounds on the
5104 ;; region we must work on.
5106 ;; This function is called from before-change-functions (via
5107 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5108 ;; and point is undefined, both at entry and exit.
5110 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5111 ;; 2010-01-29.
5112 (save-excursion
5113 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5114 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5115 new-beg new-end need-new-beg need-new-end)
5116 ;; Locate the barrier before the changed region
5117 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5118 (c-syntactic-skip-backward "^;{}" (max (- beg 2048) (point-min)))
5119 (setq new-beg (point))
5121 ;; Remove the syntax-table properties from each pertinent <...> pair.
5122 ;; Firsly, the ones with the < before beg and > after beg.
5123 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5124 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5125 (setq need-new-beg t)))
5127 ;; Locate the barrier after END.
5128 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5129 (c-syntactic-re-search-forward "[;{}]"
5130 (min (+ end 2048) (point-max)) 'end)
5131 (setq new-end (point))
5133 ;; Remove syntax-table properties from the remaining pertinent <...>
5134 ;; pairs, those with a > after end and < before end.
5135 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5136 (if (c-clear->-pair-props-if-match-before end)
5137 (setq need-new-end t)))
5139 ;; Extend the fontification region, if needed.
5140 (when need-new-beg
5141 (goto-char new-beg)
5142 (c-forward-syntactic-ws)
5143 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5145 (when need-new-end
5146 (and (> new-end c-new-END) (setq c-new-END new-end))))))
5150 (defun c-after-change-check-<>-operators (beg end)
5151 ;; This is called from `after-change-functions' when
5152 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5153 ;; chars with paren syntax become part of another operator like "<<"
5154 ;; or ">=".
5156 ;; This function might do hidden buffer changes.
5158 (save-excursion
5159 (goto-char beg)
5160 (when (or (looking-at "[<>]")
5161 (< (skip-chars-backward "<>") 0))
5163 (goto-char beg)
5164 (c-beginning-of-current-token)
5165 (when (and (< (point) beg)
5166 (looking-at c-<>-multichar-token-regexp)
5167 (< beg (setq beg (match-end 0))))
5168 (while (progn (skip-chars-forward "^<>" beg)
5169 (< (point) beg))
5170 (c-clear-<>-pair-props)
5171 (forward-char))))
5173 (when (< beg end)
5174 (goto-char end)
5175 (when (or (looking-at "[<>]")
5176 (< (skip-chars-backward "<>") 0))
5178 (goto-char end)
5179 (c-beginning-of-current-token)
5180 (when (and (< (point) end)
5181 (looking-at c-<>-multichar-token-regexp)
5182 (< end (setq end (match-end 0))))
5183 (while (progn (skip-chars-forward "^<>" end)
5184 (< (point) end))
5185 (c-clear-<>-pair-props)
5186 (forward-char)))))))
5190 ;; Handling of small scale constructs like types and names.
5192 ;; Dynamically bound variable that instructs `c-forward-type' to also
5193 ;; treat possible types (i.e. those that it normally returns 'maybe or
5194 ;; 'found for) as actual types (and always return 'found for them).
5195 ;; This means that it records them in `c-record-type-identifiers' if
5196 ;; that is set, and that it adds them to `c-found-types'.
5197 (defvar c-promote-possible-types nil)
5199 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5200 ;; mark up successfully parsed arglists with paren syntax properties on
5201 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5202 ;; `c-type' property of each argument separating comma.
5204 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5205 ;; all arglists for side effects (i.e. recording types), otherwise it
5206 ;; exploits any existing paren syntax properties to quickly jump to the
5207 ;; end of already parsed arglists.
5209 ;; Marking up the arglists is not the default since doing that correctly
5210 ;; depends on a proper value for `c-restricted-<>-arglists'.
5211 (defvar c-parse-and-markup-<>-arglists nil)
5213 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5214 ;; not accept arglists that contain binary operators.
5216 ;; This is primarily used to handle C++ template arglists. C++
5217 ;; disambiguates them by checking whether the preceding name is a
5218 ;; template or not. We can't do that, so we assume it is a template
5219 ;; if it can be parsed as one. That usually works well since
5220 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5221 ;; in almost all cases would be pointless.
5223 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5224 ;; should let the comma separate the function arguments instead. And
5225 ;; in a context where the value of the expression is taken, e.g. in
5226 ;; "if (a < b || c > d)", it's probably not a template.
5227 (defvar c-restricted-<>-arglists nil)
5229 ;; Dynamically bound variables that instructs
5230 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5231 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5232 ;; `c-forward-label' to record the ranges of all the type and
5233 ;; reference identifiers they encounter. They will build lists on
5234 ;; these variables where each element is a cons of the buffer
5235 ;; positions surrounding each identifier. This recording is only
5236 ;; activated when `c-record-type-identifiers' is non-nil.
5238 ;; All known types that can't be identifiers are recorded, and also
5239 ;; other possible types if `c-promote-possible-types' is set.
5240 ;; Recording is however disabled inside angle bracket arglists that
5241 ;; are encountered inside names and other angle bracket arglists.
5242 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5243 ;; instead.
5245 ;; Only the names in C++ template style references (e.g. "tmpl" in
5246 ;; "tmpl<a,b>::foo") are recorded as references, other references
5247 ;; aren't handled here.
5249 ;; `c-forward-label' records the label identifier(s) on
5250 ;; `c-record-ref-identifiers'.
5251 (defvar c-record-type-identifiers nil)
5252 (defvar c-record-ref-identifiers nil)
5254 ;; This variable will receive a cons cell of the range of the last
5255 ;; single identifier symbol stepped over by `c-forward-name' if it's
5256 ;; successful. This is the range that should be put on one of the
5257 ;; record lists above by the caller. It's assigned nil if there's no
5258 ;; such symbol in the name.
5259 (defvar c-last-identifier-range nil)
5261 (defmacro c-record-type-id (range)
5262 (if (eq (car-safe range) 'cons)
5263 ;; Always true.
5264 `(setq c-record-type-identifiers
5265 (cons ,range c-record-type-identifiers))
5266 `(let ((range ,range))
5267 (if range
5268 (setq c-record-type-identifiers
5269 (cons range c-record-type-identifiers))))))
5271 (defmacro c-record-ref-id (range)
5272 (if (eq (car-safe range) 'cons)
5273 ;; Always true.
5274 `(setq c-record-ref-identifiers
5275 (cons ,range c-record-ref-identifiers))
5276 `(let ((range ,range))
5277 (if range
5278 (setq c-record-ref-identifiers
5279 (cons range c-record-ref-identifiers))))))
5281 ;; Dynamically bound variable that instructs `c-forward-type' to
5282 ;; record the ranges of types that only are found. Behaves otherwise
5283 ;; like `c-record-type-identifiers'.
5284 (defvar c-record-found-types nil)
5286 (defmacro c-forward-keyword-prefixed-id (type)
5287 ;; Used internally in `c-forward-keyword-clause' to move forward
5288 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5289 ;; possibly is prefixed by keywords and their associated clauses.
5290 ;; Try with a type/name first to not trip up on those that begin
5291 ;; with a keyword. Return t if a known or found type is moved
5292 ;; over. The point is clobbered if nil is returned. If range
5293 ;; recording is enabled, the identifier is recorded on as a type
5294 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5296 ;; This macro might do hidden buffer changes.
5297 `(let (res)
5298 (while (if (setq res ,(if (eq type 'type)
5299 `(c-forward-type)
5300 `(c-forward-name)))
5302 (and (looking-at c-keywords-regexp)
5303 (c-forward-keyword-clause 1))))
5304 (when (memq res '(t known found prefix))
5305 ,(when (eq type 'ref)
5306 `(when c-record-type-identifiers
5307 (c-record-ref-id c-last-identifier-range)))
5308 t)))
5310 (defmacro c-forward-id-comma-list (type update-safe-pos)
5311 ;; Used internally in `c-forward-keyword-clause' to move forward
5312 ;; over a comma separated list of types or names using
5313 ;; `c-forward-keyword-prefixed-id'.
5315 ;; This macro might do hidden buffer changes.
5316 `(while (and (progn
5317 ,(when update-safe-pos
5318 `(setq safe-pos (point)))
5319 (eq (char-after) ?,))
5320 (progn
5321 (forward-char)
5322 (c-forward-syntactic-ws)
5323 (c-forward-keyword-prefixed-id ,type)))))
5325 (defun c-forward-keyword-clause (match)
5326 ;; Submatch MATCH in the current match data is assumed to surround a
5327 ;; token. If it's a keyword, move over it and any immediately
5328 ;; following clauses associated with it, stopping at the start of
5329 ;; the next token. t is returned in that case, otherwise the point
5330 ;; stays and nil is returned. The kind of clauses that are
5331 ;; recognized are those specified by `c-type-list-kwds',
5332 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5333 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5334 ;; and `c-<>-arglist-kwds'.
5336 ;; This function records identifier ranges on
5337 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5338 ;; `c-record-type-identifiers' is non-nil.
5340 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5341 ;; apply directly after the keyword, the type list is moved over
5342 ;; only when there is no unaccounted token before it (i.e. a token
5343 ;; that isn't moved over due to some other keyword list). The
5344 ;; identifier ranges in the list are still recorded if that should
5345 ;; be done, though.
5347 ;; This function might do hidden buffer changes.
5349 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5350 ;; The call to `c-forward-<>-arglist' below is made after
5351 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5352 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5353 ;; should therefore be nil.
5354 (c-parse-and-markup-<>-arglists t)
5355 c-restricted-<>-arglists)
5357 (when kwd-sym
5358 (goto-char (match-end match))
5359 (c-forward-syntactic-ws)
5360 (setq safe-pos (point))
5362 (cond
5363 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5364 (c-forward-keyword-prefixed-id type))
5365 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5366 (c-forward-id-comma-list type t))
5368 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5369 (c-forward-keyword-prefixed-id ref))
5370 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5371 (c-forward-id-comma-list ref t))
5373 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5374 (eq (char-after) ?\())
5375 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5377 (forward-char)
5378 (when (and (setq pos (c-up-list-forward))
5379 (eq (char-before pos) ?\)))
5380 (when (and c-record-type-identifiers
5381 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5382 ;; Use `c-forward-type' on every identifier we can find
5383 ;; inside the paren, to record the types.
5384 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5385 (goto-char (match-beginning 0))
5386 (unless (c-forward-type)
5387 (looking-at c-symbol-key) ; Always matches.
5388 (goto-char (match-end 0)))))
5390 (goto-char pos)
5391 (c-forward-syntactic-ws)
5392 (setq safe-pos (point))))
5394 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5395 (eq (char-after) ?<)
5396 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5397 (c-forward-syntactic-ws)
5398 (setq safe-pos (point)))
5400 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5401 (not (looking-at c-symbol-start))
5402 (c-safe (c-forward-sexp) t))
5403 (c-forward-syntactic-ws)
5404 (setq safe-pos (point))))
5406 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5407 (if (eq (char-after) ?:)
5408 ;; If we are at the colon already, we move over the type
5409 ;; list after it.
5410 (progn
5411 (forward-char)
5412 (c-forward-syntactic-ws)
5413 (when (c-forward-keyword-prefixed-id type)
5414 (c-forward-id-comma-list type t)))
5415 ;; Not at the colon, so stop here. But the identifier
5416 ;; ranges in the type list later on should still be
5417 ;; recorded.
5418 (and c-record-type-identifiers
5419 (progn
5420 ;; If a keyword matched both one of the types above and
5421 ;; this one, we match `c-colon-type-list-re' after the
5422 ;; clause matched above.
5423 (goto-char safe-pos)
5424 (looking-at c-colon-type-list-re))
5425 (progn
5426 (goto-char (match-end 0))
5427 (c-forward-syntactic-ws)
5428 (c-forward-keyword-prefixed-id type))
5429 ;; There's a type after the `c-colon-type-list-re' match
5430 ;; after a keyword in `c-colon-type-list-kwds'.
5431 (c-forward-id-comma-list type nil))))
5433 (goto-char safe-pos)
5434 t)))
5436 ;; cc-mode requires cc-fonts.
5437 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5439 (defun c-forward-<>-arglist (all-types)
5440 ;; The point is assumed to be at a "<". Try to treat it as the open
5441 ;; paren of an angle bracket arglist and move forward to the
5442 ;; corresponding ">". If successful, the point is left after the
5443 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5444 ;; returned. If ALL-TYPES is t then all encountered arguments in
5445 ;; the arglist that might be types are treated as found types.
5447 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5448 ;; function handles text properties on the angle brackets and argument
5449 ;; separating commas.
5451 ;; `c-restricted-<>-arglists' controls how lenient the template
5452 ;; arglist recognition should be.
5454 ;; This function records identifier ranges on
5455 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5456 ;; `c-record-type-identifiers' is non-nil.
5458 ;; This function might do hidden buffer changes.
5460 (let ((start (point))
5461 ;; If `c-record-type-identifiers' is set then activate
5462 ;; recording of any found types that constitute an argument in
5463 ;; the arglist.
5464 (c-record-found-types (if c-record-type-identifiers t)))
5465 (if (catch 'angle-bracket-arglist-escape
5466 (setq c-record-found-types
5467 (c-forward-<>-arglist-recur all-types)))
5468 (progn
5469 (when (consp c-record-found-types)
5470 (setq c-record-type-identifiers
5471 ;; `nconc' doesn't mind that the tail of
5472 ;; `c-record-found-types' is t.
5473 (nconc c-record-found-types c-record-type-identifiers)))
5474 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
5477 (goto-char start)
5478 nil)))
5480 (defun c-forward-<>-arglist-recur (all-types)
5481 ;; Recursive part of `c-forward-<>-arglist'.
5483 ;; This function might do hidden buffer changes.
5485 (let ((start (point)) res pos tmp
5486 ;; Cover this so that any recorded found type ranges are
5487 ;; automatically lost if it turns out to not be an angle
5488 ;; bracket arglist. It's propagated through the return value
5489 ;; on successful completion.
5490 (c-record-found-types c-record-found-types)
5491 ;; List that collects the positions after the argument
5492 ;; separating ',' in the arglist.
5493 arg-start-pos)
5494 ;; If the '<' has paren open syntax then we've marked it as an angle
5495 ;; bracket arglist before, so skip to the end.
5496 (if (and (not c-parse-and-markup-<>-arglists)
5497 (c-get-char-property (point) 'syntax-table))
5499 (progn
5500 (forward-char)
5501 (if (and (c-go-up-list-forward)
5502 (eq (char-before) ?>))
5504 ;; Got unmatched paren angle brackets. We don't clear the paren
5505 ;; syntax properties and retry, on the basis that it's very
5506 ;; unlikely that paren angle brackets become operators by code
5507 ;; manipulation. It's far more likely that it doesn't match due
5508 ;; to narrowing or some temporary change.
5509 (goto-char start)
5510 nil))
5512 (forward-char) ; Forward over the opening '<'.
5514 (unless (looking-at c-<-op-cont-regexp)
5515 ;; go forward one non-alphanumeric character (group) per iteration of
5516 ;; this loop.
5517 (while (and
5518 (progn
5519 (c-forward-syntactic-ws)
5520 (let ((orig-record-found-types c-record-found-types))
5521 (when (or (and c-record-type-identifiers all-types)
5522 (c-major-mode-is 'java-mode))
5523 ;; All encountered identifiers are types, so set the
5524 ;; promote flag and parse the type.
5525 (progn
5526 (c-forward-syntactic-ws)
5527 (if (looking-at "\\?")
5528 (forward-char)
5529 (when (looking-at c-identifier-start)
5530 (let ((c-promote-possible-types t)
5531 (c-record-found-types t))
5532 (c-forward-type))))
5534 (c-forward-syntactic-ws)
5536 (when (or (looking-at "extends")
5537 (looking-at "super"))
5538 (forward-word)
5539 (c-forward-syntactic-ws)
5540 (let ((c-promote-possible-types t)
5541 (c-record-found-types t))
5542 (c-forward-type)
5543 (c-forward-syntactic-ws))))))
5545 (setq pos (point)) ; e.g. first token inside the '<'
5547 ;; Note: These regexps exploit the match order in \| so
5548 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5549 (c-syntactic-re-search-forward
5550 ;; Stop on ',', '|', '&', '+' and '-' to catch
5551 ;; common binary operators that could be between
5552 ;; two comparison expressions "a<b" and "c>d".
5553 "[<;{},|+&-]\\|[>)]"
5554 nil t t))
5556 (cond
5557 ((eq (char-before) ?>)
5558 ;; Either an operator starting with '>' or the end of
5559 ;; the angle bracket arglist.
5561 (if (looking-at c->-op-cont-regexp)
5562 (progn
5563 (goto-char (match-end 0))
5564 t) ; Continue the loop.
5566 ;; The angle bracket arglist is finished.
5567 (when c-parse-and-markup-<>-arglists
5568 (while arg-start-pos
5569 (c-put-c-type-property (1- (car arg-start-pos))
5570 'c-<>-arg-sep)
5571 (setq arg-start-pos (cdr arg-start-pos)))
5572 (c-mark-<-as-paren start)
5573 (c-mark->-as-paren (1- (point))))
5574 (setq res t)
5575 nil)) ; Exit the loop.
5577 ((eq (char-before) ?<)
5578 ;; Either an operator starting with '<' or a nested arglist.
5579 (setq pos (point))
5580 (let (id-start id-end subres keyword-match)
5581 (cond
5582 ;; The '<' begins a multi-char operator.
5583 ((looking-at c-<-op-cont-regexp)
5584 (setq tmp (match-end 0))
5585 (goto-char (match-end 0)))
5586 ;; We're at a nested <.....>
5587 ((progn
5588 (setq tmp pos)
5589 (backward-char) ; to the '<'
5590 (and
5591 (save-excursion
5592 ;; There's always an identifier before an angle
5593 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5594 ;; or `c-<>-arglist-kwds'.
5595 (c-backward-syntactic-ws)
5596 (setq id-end (point))
5597 (c-simple-skip-symbol-backward)
5598 (when (or (setq keyword-match
5599 (looking-at c-opt-<>-sexp-key))
5600 (not (looking-at c-keywords-regexp)))
5601 (setq id-start (point))))
5602 (setq subres
5603 (let ((c-promote-possible-types t)
5604 (c-record-found-types t))
5605 (c-forward-<>-arglist-recur
5606 (and keyword-match
5607 (c-keyword-member
5608 (c-keyword-sym (match-string 1))
5609 'c-<>-type-kwds)))))))
5611 ;; It was an angle bracket arglist.
5612 (setq c-record-found-types subres)
5614 ;; Record the identifier before the template as a type
5615 ;; or reference depending on whether the arglist is last
5616 ;; in a qualified identifier.
5617 (when (and c-record-type-identifiers
5618 (not keyword-match))
5619 (if (and c-opt-identifier-concat-key
5620 (progn
5621 (c-forward-syntactic-ws)
5622 (looking-at c-opt-identifier-concat-key)))
5623 (c-record-ref-id (cons id-start id-end))
5624 (c-record-type-id (cons id-start id-end)))))
5626 ;; At a "less than" operator.
5628 (forward-char)
5630 t) ; carry on looping.
5632 ((and (not c-restricted-<>-arglists)
5633 (or (and (eq (char-before) ?&)
5634 (not (eq (char-after) ?&)))
5635 (eq (char-before) ?,)))
5636 ;; Just another argument. Record the position. The
5637 ;; type check stuff that made us stop at it is at
5638 ;; the top of the loop.
5639 (setq arg-start-pos (cons (point) arg-start-pos)))
5642 ;; Got a character that can't be in an angle bracket
5643 ;; arglist argument. Abort using `throw', since
5644 ;; it's useless to try to find a surrounding arglist
5645 ;; if we're nested.
5646 (throw 'angle-bracket-arglist-escape nil))))))
5647 (if res
5648 (or c-record-found-types t)))))
5650 (defun c-backward-<>-arglist (all-types &optional limit)
5651 ;; The point is assumed to be directly after a ">". Try to treat it
5652 ;; as the close paren of an angle bracket arglist and move back to
5653 ;; the corresponding "<". If successful, the point is left at
5654 ;; the "<" and t is returned, otherwise the point isn't moved and
5655 ;; nil is returned. ALL-TYPES is passed on to
5656 ;; `c-forward-<>-arglist'.
5658 ;; If the optional LIMIT is given, it bounds the backward search.
5659 ;; It's then assumed to be at a syntactically relevant position.
5661 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5662 ;; function for more details.
5664 (let ((start (point)))
5665 (backward-char)
5666 (if (and (not c-parse-and-markup-<>-arglists)
5667 (c-get-char-property (point) 'syntax-table))
5669 (if (and (c-go-up-list-backward)
5670 (eq (char-after) ?<))
5672 ;; See corresponding note in `c-forward-<>-arglist'.
5673 (goto-char start)
5674 nil)
5676 (while (progn
5677 (c-syntactic-skip-backward "^<;{}" limit t)
5679 (and
5680 (if (eq (char-before) ?<)
5682 ;; Stopped at bob or a char that isn't allowed in an
5683 ;; arglist, so we've failed.
5684 (goto-char start)
5685 nil)
5687 (if (> (point)
5688 (progn (c-beginning-of-current-token)
5689 (point)))
5690 ;; If we moved then the "<" was part of some
5691 ;; multicharacter token.
5694 (backward-char)
5695 (let ((beg-pos (point)))
5696 (if (c-forward-<>-arglist all-types)
5697 (cond ((= (point) start)
5698 ;; Matched the arglist. Break the while.
5699 (goto-char beg-pos)
5700 nil)
5701 ((> (point) start)
5702 ;; We started from a non-paren ">" inside an
5703 ;; arglist.
5704 (goto-char start)
5705 nil)
5707 ;; Matched a shorter arglist. Can be a nested
5708 ;; one so continue looking.
5709 (goto-char beg-pos)
5711 t))))))
5713 (/= (point) start))))
5715 (defun c-forward-name ()
5716 ;; Move forward over a complete name if at the beginning of one,
5717 ;; stopping at the next following token. A keyword, as such,
5718 ;; doesn't count as a name. If the point is not at something that
5719 ;; is recognized as a name then it stays put.
5721 ;; A name could be something as simple as "foo" in C or something as
5722 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5723 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5724 ;; int>::*volatile const" in C++ (this function is actually little
5725 ;; more than a `looking-at' call in all modes except those that,
5726 ;; like C++, have `c-recognize-<>-arglists' set).
5728 ;; Return
5729 ;; o - nil if no name is found;
5730 ;; o - 'template if it's an identifier ending with an angle bracket
5731 ;; arglist;
5732 ;; o - 'operator of it's an operator identifier;
5733 ;; o - t if it's some other kind of name.
5735 ;; This function records identifier ranges on
5736 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5737 ;; `c-record-type-identifiers' is non-nil.
5739 ;; This function might do hidden buffer changes.
5741 (let ((pos (point)) (start (point)) res id-start id-end
5742 ;; Turn off `c-promote-possible-types' here since we might
5743 ;; call `c-forward-<>-arglist' and we don't want it to promote
5744 ;; every suspect thing in the arglist to a type. We're
5745 ;; typically called from `c-forward-type' in this case, and
5746 ;; the caller only wants the top level type that it finds to
5747 ;; be promoted.
5748 c-promote-possible-types)
5749 (while
5750 (and
5751 (looking-at c-identifier-key)
5753 (progn
5754 ;; Check for keyword. We go to the last symbol in
5755 ;; `c-identifier-key' first.
5756 (goto-char (setq id-end (match-end 0)))
5757 (c-simple-skip-symbol-backward)
5758 (setq id-start (point))
5760 (if (looking-at c-keywords-regexp)
5761 (when (and (c-major-mode-is 'c++-mode)
5762 (looking-at
5763 (cc-eval-when-compile
5764 (concat "\\(operator\\|\\(template\\)\\)"
5765 "\\(" (c-lang-const c-nonsymbol-key c++)
5766 "\\|$\\)")))
5767 (if (match-beginning 2)
5768 ;; "template" is only valid inside an
5769 ;; identifier if preceded by "::".
5770 (save-excursion
5771 (c-backward-syntactic-ws)
5772 (and (c-safe (backward-char 2) t)
5773 (looking-at "::")))
5776 ;; Handle a C++ operator or template identifier.
5777 (goto-char id-end)
5778 (c-forward-syntactic-ws)
5779 (cond ((eq (char-before id-end) ?e)
5780 ;; Got "... ::template".
5781 (let ((subres (c-forward-name)))
5782 (when subres
5783 (setq pos (point)
5784 res subres))))
5786 ((looking-at c-identifier-start)
5787 ;; Got a cast operator.
5788 (when (c-forward-type)
5789 (setq pos (point)
5790 res 'operator)
5791 ;; Now we should match a sequence of either
5792 ;; '*', '&' or a name followed by ":: *",
5793 ;; where each can be followed by a sequence
5794 ;; of `c-opt-type-modifier-key'.
5795 (while (cond ((looking-at "[*&]")
5796 (goto-char (match-end 0))
5798 ((looking-at c-identifier-start)
5799 (and (c-forward-name)
5800 (looking-at "::")
5801 (progn
5802 (goto-char (match-end 0))
5803 (c-forward-syntactic-ws)
5804 (eq (char-after) ?*))
5805 (progn
5806 (forward-char)
5807 t))))
5808 (while (progn
5809 (c-forward-syntactic-ws)
5810 (setq pos (point))
5811 (looking-at c-opt-type-modifier-key))
5812 (goto-char (match-end 1))))))
5814 ((looking-at c-overloadable-operators-regexp)
5815 ;; Got some other operator.
5816 (setq c-last-identifier-range
5817 (cons (point) (match-end 0)))
5818 (goto-char (match-end 0))
5819 (c-forward-syntactic-ws)
5820 (setq pos (point)
5821 res 'operator)))
5823 nil)
5825 ;; `id-start' is equal to `id-end' if we've jumped over
5826 ;; an identifier that doesn't end with a symbol token.
5827 ;; That can occur e.g. for Java import directives on the
5828 ;; form "foo.bar.*".
5829 (when (and id-start (/= id-start id-end))
5830 (setq c-last-identifier-range
5831 (cons id-start id-end)))
5832 (goto-char id-end)
5833 (c-forward-syntactic-ws)
5834 (setq pos (point)
5835 res t)))
5837 (progn
5838 (goto-char pos)
5839 (when (or c-opt-identifier-concat-key
5840 c-recognize-<>-arglists)
5842 (cond
5843 ((and c-opt-identifier-concat-key
5844 (looking-at c-opt-identifier-concat-key))
5845 ;; Got a concatenated identifier. This handles the
5846 ;; cases with tricky syntactic whitespace that aren't
5847 ;; covered in `c-identifier-key'.
5848 (goto-char (match-end 0))
5849 (c-forward-syntactic-ws)
5852 ((and c-recognize-<>-arglists
5853 (eq (char-after) ?<))
5854 ;; Maybe an angle bracket arglist.
5855 (when (let ((c-record-type-identifiers t)
5856 (c-record-found-types t))
5857 (c-forward-<>-arglist nil))
5859 (c-add-type start (1+ pos))
5860 (c-forward-syntactic-ws)
5861 (setq pos (point)
5862 c-last-identifier-range nil)
5864 (if (and c-opt-identifier-concat-key
5865 (looking-at c-opt-identifier-concat-key))
5867 ;; Continue if there's an identifier concatenation
5868 ;; operator after the template argument.
5869 (progn
5870 (when (and c-record-type-identifiers id-start)
5871 (c-record-ref-id (cons id-start id-end)))
5872 (forward-char 2)
5873 (c-forward-syntactic-ws)
5876 (when (and c-record-type-identifiers id-start)
5877 (c-record-type-id (cons id-start id-end)))
5878 (setq res 'template)
5879 nil)))
5880 )))))
5882 (goto-char pos)
5883 res))
5885 (defun c-forward-type (&optional brace-block-too)
5886 ;; Move forward over a type spec if at the beginning of one,
5887 ;; stopping at the next following token. The keyword "typedef"
5888 ;; isn't part of a type spec here.
5890 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
5891 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
5892 ;; The current (2009-03-10) intention is to convert all uses of
5893 ;; `c-forward-type' to call with this parameter set, then to
5894 ;; eliminate it.
5896 ;; Return
5897 ;; o - t if it's a known type that can't be a name or other
5898 ;; expression;
5899 ;; o - 'known if it's an otherwise known type (according to
5900 ;; `*-font-lock-extra-types');
5901 ;; o - 'prefix if it's a known prefix of a type;
5902 ;; o - 'found if it's a type that matches one in `c-found-types';
5903 ;; o - 'maybe if it's an identfier that might be a type; or
5904 ;; o - nil if it can't be a type (the point isn't moved then).
5906 ;; The point is assumed to be at the beginning of a token.
5908 ;; Note that this function doesn't skip past the brace definition
5909 ;; that might be considered part of the type, e.g.
5910 ;; "enum {a, b, c} foo".
5912 ;; This function records identifier ranges on
5913 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5914 ;; `c-record-type-identifiers' is non-nil.
5916 ;; This function might do hidden buffer changes.
5917 (when (and c-recognize-<>-arglists
5918 (looking-at "<"))
5919 (c-forward-<>-arglist t)
5920 (c-forward-syntactic-ws))
5922 (let ((start (point)) pos res name-res id-start id-end id-range)
5924 ;; Skip leading type modifiers. If any are found we know it's a
5925 ;; prefix of a type.
5926 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
5927 (while (looking-at c-opt-type-modifier-key)
5928 (goto-char (match-end 1))
5929 (c-forward-syntactic-ws)
5930 (setq res 'prefix)))
5932 (cond
5933 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
5934 ; "typedef".
5935 (goto-char (match-end 1))
5936 (c-forward-syntactic-ws)
5937 (setq pos (point))
5939 (setq name-res (c-forward-name))
5940 (setq res (not (null name-res)))
5941 (when (eq name-res t)
5942 ;; In many languages the name can be used without the
5943 ;; prefix, so we add it to `c-found-types'.
5944 (c-add-type pos (point))
5945 (when (and c-record-type-identifiers
5946 c-last-identifier-range)
5947 (c-record-type-id c-last-identifier-range)))
5948 (when (and brace-block-too
5949 (memq res '(t nil))
5950 (eq (char-after) ?\{)
5951 (save-excursion
5952 (c-safe
5953 (progn (c-forward-sexp)
5954 (c-forward-syntactic-ws)
5955 (setq pos (point))))))
5956 (goto-char pos)
5957 (setq res t))
5958 (unless res (goto-char start))) ; invalid syntax
5960 ((progn
5961 (setq pos nil)
5962 (if (looking-at c-identifier-start)
5963 (save-excursion
5964 (setq id-start (point)
5965 name-res (c-forward-name))
5966 (when name-res
5967 (setq id-end (point)
5968 id-range c-last-identifier-range))))
5969 (and (cond ((looking-at c-primitive-type-key)
5970 (setq res t))
5971 ((c-with-syntax-table c-identifier-syntax-table
5972 (looking-at c-known-type-key))
5973 (setq res 'known)))
5974 (or (not id-end)
5975 (>= (save-excursion
5976 (save-match-data
5977 (goto-char (match-end 1))
5978 (c-forward-syntactic-ws)
5979 (setq pos (point))))
5980 id-end)
5981 (setq res nil))))
5982 ;; Looking at a primitive or known type identifier. We've
5983 ;; checked for a name first so that we don't go here if the
5984 ;; known type match only is a prefix of another name.
5986 (setq id-end (match-end 1))
5988 (when (and c-record-type-identifiers
5989 (or c-promote-possible-types (eq res t)))
5990 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
5992 (if (and c-opt-type-component-key
5993 (save-match-data
5994 (looking-at c-opt-type-component-key)))
5995 ;; There might be more keywords for the type.
5996 (let (safe-pos)
5997 (c-forward-keyword-clause 1)
5998 (while (progn
5999 (setq safe-pos (point))
6000 (looking-at c-opt-type-component-key))
6001 (when (and c-record-type-identifiers
6002 (looking-at c-primitive-type-key))
6003 (c-record-type-id (cons (match-beginning 1)
6004 (match-end 1))))
6005 (c-forward-keyword-clause 1))
6006 (if (looking-at c-primitive-type-key)
6007 (progn
6008 (when c-record-type-identifiers
6009 (c-record-type-id (cons (match-beginning 1)
6010 (match-end 1))))
6011 (c-forward-keyword-clause 1)
6012 (setq res t))
6013 (goto-char safe-pos)
6014 (setq res 'prefix)))
6015 (unless (save-match-data (c-forward-keyword-clause 1))
6016 (if pos
6017 (goto-char pos)
6018 (goto-char (match-end 1))
6019 (c-forward-syntactic-ws)))))
6021 (name-res
6022 (cond ((eq name-res t)
6023 ;; A normal identifier.
6024 (goto-char id-end)
6025 (if (or res c-promote-possible-types)
6026 (progn
6027 (c-add-type id-start id-end)
6028 (when (and c-record-type-identifiers id-range)
6029 (c-record-type-id id-range))
6030 (unless res
6031 (setq res 'found)))
6032 (setq res (if (c-check-type id-start id-end)
6033 ;; It's an identifier that has been used as
6034 ;; a type somewhere else.
6035 'found
6036 ;; It's an identifier that might be a type.
6037 'maybe))))
6038 ((eq name-res 'template)
6039 ;; A template is a type.
6040 (goto-char id-end)
6041 (setq res t))
6043 ;; Otherwise it's an operator identifier, which is not a type.
6044 (goto-char start)
6045 (setq res nil)))))
6047 (when res
6048 ;; Skip trailing type modifiers. If any are found we know it's
6049 ;; a type.
6050 (when c-opt-type-modifier-key
6051 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
6052 (goto-char (match-end 1))
6053 (c-forward-syntactic-ws)
6054 (setq res t)))
6055 ;; Step over any type suffix operator. Do not let the existence
6056 ;; of these alter the classification of the found type, since
6057 ;; these operators typically are allowed in normal expressions
6058 ;; too.
6059 (when c-opt-type-suffix-key
6060 (while (looking-at c-opt-type-suffix-key)
6061 (goto-char (match-end 1))
6062 (c-forward-syntactic-ws)))
6064 (when c-opt-type-concat-key ; Only/mainly for pike.
6065 ;; Look for a trailing operator that concatenates the type
6066 ;; with a following one, and if so step past that one through
6067 ;; a recursive call. Note that we don't record concatenated
6068 ;; types in `c-found-types' - it's the component types that
6069 ;; are recorded when appropriate.
6070 (setq pos (point))
6071 (let* ((c-promote-possible-types (or (memq res '(t known))
6072 c-promote-possible-types))
6073 ;; If we can't promote then set `c-record-found-types' so that
6074 ;; we can merge in the types from the second part afterwards if
6075 ;; it turns out to be a known type there.
6076 (c-record-found-types (and c-record-type-identifiers
6077 (not c-promote-possible-types)))
6078 subres)
6079 (if (and (looking-at c-opt-type-concat-key)
6081 (progn
6082 (goto-char (match-end 1))
6083 (c-forward-syntactic-ws)
6084 (setq subres (c-forward-type))))
6086 (progn
6087 ;; If either operand certainly is a type then both are, but we
6088 ;; don't let the existence of the operator itself promote two
6089 ;; uncertain types to a certain one.
6090 (cond ((eq res t))
6091 ((eq subres t)
6092 (unless (eq name-res 'template)
6093 (c-add-type id-start id-end))
6094 (when (and c-record-type-identifiers id-range)
6095 (c-record-type-id id-range))
6096 (setq res t))
6097 ((eq res 'known))
6098 ((eq subres 'known)
6099 (setq res 'known))
6100 ((eq res 'found))
6101 ((eq subres 'found)
6102 (setq res 'found))
6104 (setq res 'maybe)))
6106 (when (and (eq res t)
6107 (consp c-record-found-types))
6108 ;; Merge in the ranges of any types found by the second
6109 ;; `c-forward-type'.
6110 (setq c-record-type-identifiers
6111 ;; `nconc' doesn't mind that the tail of
6112 ;; `c-record-found-types' is t.
6113 (nconc c-record-found-types
6114 c-record-type-identifiers))))
6116 (goto-char pos))))
6118 (when (and c-record-found-types (memq res '(known found)) id-range)
6119 (setq c-record-found-types
6120 (cons id-range c-record-found-types))))
6122 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6124 res))
6126 (defun c-forward-annotation ()
6127 ;; Used for Java code only at the moment. Assumes point is on the
6128 ;; @, moves forward an annotation. returns nil if there is no
6129 ;; annotation at point.
6130 (and (looking-at "@")
6131 (progn (forward-char) t)
6132 (c-forward-type)
6133 (progn (c-forward-syntactic-ws) t)
6134 (if (looking-at "(")
6135 (c-go-list-forward)
6136 t)))
6139 ;; Handling of large scale constructs like statements and declarations.
6141 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6142 ;; defsubst or perhaps even a defun, but it contains lots of free
6143 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6144 (defmacro c-fdoc-shift-type-backward (&optional short)
6145 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6146 ;; of types when parsing a declaration, which means that it
6147 ;; sometimes consumes the identifier in the declaration as a type.
6148 ;; This is used to "backtrack" and make the last type be treated as
6149 ;; an identifier instead.
6150 `(progn
6151 ,(unless short
6152 ;; These identifiers are bound only in the inner let.
6153 '(setq identifier-type at-type
6154 identifier-start type-start
6155 got-parens nil
6156 got-identifier t
6157 got-suffix t
6158 got-suffix-after-parens id-start
6159 paren-depth 0))
6161 (if (setq at-type (if (eq backup-at-type 'prefix)
6163 backup-at-type))
6164 (setq type-start backup-type-start
6165 id-start backup-id-start)
6166 (setq type-start start-pos
6167 id-start start-pos))
6169 ;; When these flags already are set we've found specifiers that
6170 ;; unconditionally signal these attributes - backtracking doesn't
6171 ;; change that. So keep them set in that case.
6172 (or at-type-decl
6173 (setq at-type-decl backup-at-type-decl))
6174 (or maybe-typeless
6175 (setq maybe-typeless backup-maybe-typeless))
6177 ,(unless short
6178 ;; This identifier is bound only in the inner let.
6179 '(setq start id-start))))
6181 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6182 ;; Move forward over a declaration or a cast if at the start of one.
6183 ;; The point is assumed to be at the start of some token. Nil is
6184 ;; returned if no declaration or cast is recognized, and the point
6185 ;; is clobbered in that case.
6187 ;; If a declaration is parsed:
6189 ;; The point is left at the first token after the first complete
6190 ;; declarator, if there is one. The return value is a cons where
6191 ;; the car is the position of the first token in the declarator. (See
6192 ;; below for the cdr.)
6193 ;; Some examples:
6195 ;; void foo (int a, char *b) stuff ...
6196 ;; car ^ ^ point
6197 ;; float (*a)[], b;
6198 ;; car ^ ^ point
6199 ;; unsigned int a = c_style_initializer, b;
6200 ;; car ^ ^ point
6201 ;; unsigned int a (cplusplus_style_initializer), b;
6202 ;; car ^ ^ point (might change)
6203 ;; class Foo : public Bar {}
6204 ;; car ^ ^ point
6205 ;; class PikeClass (int a, string b) stuff ...
6206 ;; car ^ ^ point
6207 ;; enum bool;
6208 ;; car ^ ^ point
6209 ;; enum bool flag;
6210 ;; car ^ ^ point
6211 ;; void cplusplus_function (int x) throw (Bad);
6212 ;; car ^ ^ point
6213 ;; Foo::Foo (int b) : Base (b) {}
6214 ;; car ^ ^ point
6216 ;; The cdr of the return value is non-nil when a
6217 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6218 ;; Specifically it is a dotted pair (A . B) where B is t when a
6219 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6220 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6221 ;; specifier is present. I.e., (some of) the declared
6222 ;; identifier(s) are types.
6224 ;; If a cast is parsed:
6226 ;; The point is left at the first token after the closing paren of
6227 ;; the cast. The return value is `cast'. Note that the start
6228 ;; position must be at the first token inside the cast parenthesis
6229 ;; to recognize it.
6231 ;; PRECEDING-TOKEN-END is the first position after the preceding
6232 ;; token, i.e. on the other side of the syntactic ws from the point.
6233 ;; Use a value less than or equal to (point-min) if the point is at
6234 ;; the first token in (the visible part of) the buffer.
6236 ;; CONTEXT is a symbol that describes the context at the point:
6237 ;; 'decl In a comma-separated declaration context (typically
6238 ;; inside a function declaration arglist).
6239 ;; '<> In an angle bracket arglist.
6240 ;; 'arglist Some other type of arglist.
6241 ;; nil Some other context or unknown context. Includes
6242 ;; within the parens of an if, for, ... construct.
6244 ;; LAST-CAST-END is the first token after the closing paren of a
6245 ;; preceding cast, or nil if none is known. If
6246 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6247 ;; the position after the closest preceding call where a cast was
6248 ;; matched. In that case it's used to discover chains of casts like
6249 ;; "(a) (b) c".
6251 ;; This function records identifier ranges on
6252 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6253 ;; `c-record-type-identifiers' is non-nil.
6255 ;; This function might do hidden buffer changes.
6257 (let (;; `start-pos' is used below to point to the start of the
6258 ;; first type, i.e. after any leading specifiers. It might
6259 ;; also point at the beginning of the preceding syntactic
6260 ;; whitespace.
6261 (start-pos (point))
6262 ;; Set to the result of `c-forward-type'.
6263 at-type
6264 ;; The position of the first token in what we currently
6265 ;; believe is the type in the declaration or cast, after any
6266 ;; specifiers and their associated clauses.
6267 type-start
6268 ;; The position of the first token in what we currently
6269 ;; believe is the declarator for the first identifier. Set
6270 ;; when the type is found, and moved forward over any
6271 ;; `c-decl-hangon-kwds' and their associated clauses that
6272 ;; occurs after the type.
6273 id-start
6274 ;; These store `at-type', `type-start' and `id-start' of the
6275 ;; identifier before the one in those variables. The previous
6276 ;; identifier might turn out to be the real type in a
6277 ;; declaration if the last one has to be the declarator in it.
6278 ;; If `backup-at-type' is nil then the other variables have
6279 ;; undefined values.
6280 backup-at-type backup-type-start backup-id-start
6281 ;; Set if we've found a specifier (apart from "typedef") that makes
6282 ;; the defined identifier(s) types.
6283 at-type-decl
6284 ;; Set if we've a "typedef" keyword.
6285 at-typedef
6286 ;; Set if we've found a specifier that can start a declaration
6287 ;; where there's no type.
6288 maybe-typeless
6289 ;; If a specifier is found that also can be a type prefix,
6290 ;; these flags are set instead of those above. If we need to
6291 ;; back up an identifier, they are copied to the real flag
6292 ;; variables. Thus they only take effect if we fail to
6293 ;; interpret it as a type.
6294 backup-at-type-decl backup-maybe-typeless
6295 ;; Whether we've found a declaration or a cast. We might know
6296 ;; this before we've found the type in it. It's 'ids if we've
6297 ;; found two consecutive identifiers (usually a sure sign, but
6298 ;; we should allow that in labels too), and t if we've found a
6299 ;; specifier keyword (a 100% sure sign).
6300 at-decl-or-cast
6301 ;; Set when we need to back up to parse this as a declaration
6302 ;; but not as a cast.
6303 backup-if-not-cast
6304 ;; For casts, the return position.
6305 cast-end
6306 ;; Save `c-record-type-identifiers' and
6307 ;; `c-record-ref-identifiers' since ranges are recorded
6308 ;; speculatively and should be thrown away if it turns out
6309 ;; that it isn't a declaration or cast.
6310 (save-rec-type-ids c-record-type-identifiers)
6311 (save-rec-ref-ids c-record-ref-identifiers))
6313 (while (c-forward-annotation)
6314 (c-forward-syntactic-ws))
6316 ;; Check for a type. Unknown symbols are treated as possible
6317 ;; types, but they could also be specifiers disguised through
6318 ;; macros like __INLINE__, so we recognize both types and known
6319 ;; specifiers after them too.
6320 (while
6321 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6323 ;; Look for a specifier keyword clause.
6324 (when (looking-at c-prefix-spec-kwds-re)
6325 (if (looking-at c-typedef-key)
6326 (setq at-typedef t))
6327 (setq kwd-sym (c-keyword-sym (match-string 1)))
6328 (save-excursion
6329 (c-forward-keyword-clause 1)
6330 (setq kwd-clause-end (point))))
6332 (when (setq found-type (c-forward-type t)) ; brace-block-too
6333 ;; Found a known or possible type or a prefix of a known type.
6335 (when at-type
6336 ;; Got two identifiers with nothing but whitespace
6337 ;; between them. That can only happen in declarations.
6338 (setq at-decl-or-cast 'ids)
6340 (when (eq at-type 'found)
6341 ;; If the previous identifier is a found type we
6342 ;; record it as a real one; it might be some sort of
6343 ;; alias for a prefix like "unsigned".
6344 (save-excursion
6345 (goto-char type-start)
6346 (let ((c-promote-possible-types t))
6347 (c-forward-type)))))
6349 (setq backup-at-type at-type
6350 backup-type-start type-start
6351 backup-id-start id-start
6352 at-type found-type
6353 type-start start
6354 id-start (point)
6355 ;; The previous ambiguous specifier/type turned out
6356 ;; to be a type since we've parsed another one after
6357 ;; it, so clear these backup flags.
6358 backup-at-type-decl nil
6359 backup-maybe-typeless nil))
6361 (if kwd-sym
6362 (progn
6363 ;; Handle known specifier keywords and
6364 ;; `c-decl-hangon-kwds' which can occur after known
6365 ;; types.
6367 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6368 ;; It's a hang-on keyword that can occur anywhere.
6369 (progn
6370 (setq at-decl-or-cast t)
6371 (if at-type
6372 ;; Move the identifier start position if
6373 ;; we've passed a type.
6374 (setq id-start kwd-clause-end)
6375 ;; Otherwise treat this as a specifier and
6376 ;; move the fallback position.
6377 (setq start-pos kwd-clause-end))
6378 (goto-char kwd-clause-end))
6380 ;; It's an ordinary specifier so we know that
6381 ;; anything before this can't be the type.
6382 (setq backup-at-type nil
6383 start-pos kwd-clause-end)
6385 (if found-type
6386 ;; It's ambiguous whether this keyword is a
6387 ;; specifier or a type prefix, so set the backup
6388 ;; flags. (It's assumed that `c-forward-type'
6389 ;; moved further than `c-forward-keyword-clause'.)
6390 (progn
6391 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6392 (setq backup-at-type-decl t))
6393 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6394 (setq backup-maybe-typeless t)))
6396 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6397 ;; This test only happens after we've scanned a type.
6398 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6399 (setq at-type-decl t))
6400 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6401 (setq maybe-typeless t))
6403 ;; Haven't matched a type so it's an umambiguous
6404 ;; specifier keyword and we know we're in a
6405 ;; declaration.
6406 (setq at-decl-or-cast t)
6408 (goto-char kwd-clause-end))))
6410 ;; If the type isn't known we continue so that we'll jump
6411 ;; over all specifiers and type identifiers. The reason
6412 ;; to do this for a known type prefix is to make things
6413 ;; like "unsigned INT16" work.
6414 (and found-type (not (eq found-type t))))))
6416 (cond
6417 ((eq at-type t)
6418 ;; If a known type was found, we still need to skip over any
6419 ;; hangon keyword clauses after it. Otherwise it has already
6420 ;; been done in the loop above.
6421 (while (looking-at c-decl-hangon-key)
6422 (c-forward-keyword-clause 1))
6423 (setq id-start (point)))
6425 ((eq at-type 'prefix)
6426 ;; A prefix type is itself a primitive type when it's not
6427 ;; followed by another type.
6428 (setq at-type t))
6430 ((not at-type)
6431 ;; Got no type but set things up to continue anyway to handle
6432 ;; the various cases when a declaration doesn't start with a
6433 ;; type.
6434 (setq id-start start-pos))
6436 ((and (eq at-type 'maybe)
6437 (c-major-mode-is 'c++-mode))
6438 ;; If it's C++ then check if the last "type" ends on the form
6439 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6440 ;; (con|de)structor.
6441 (save-excursion
6442 (let (name end-2 end-1)
6443 (goto-char id-start)
6444 (c-backward-syntactic-ws)
6445 (setq end-2 (point))
6446 (when (and
6447 (c-simple-skip-symbol-backward)
6448 (progn
6449 (setq name
6450 (buffer-substring-no-properties (point) end-2))
6451 ;; Cheating in the handling of syntactic ws below.
6452 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6453 (progn
6454 (setq end-1 (point))
6455 (c-simple-skip-symbol-backward))
6456 (>= (point) type-start)
6457 (equal (buffer-substring-no-properties (point) end-1)
6458 name))
6459 ;; It is a (con|de)structor name. In that case the
6460 ;; declaration is typeless so zap out any preceding
6461 ;; identifier(s) that we might have taken as types.
6462 (goto-char type-start)
6463 (setq at-type nil
6464 backup-at-type nil
6465 id-start type-start))))))
6467 ;; Check for and step over a type decl expression after the thing
6468 ;; that is or might be a type. This can't be skipped since we
6469 ;; need the correct end position of the declarator for
6470 ;; `max-type-decl-end-*'.
6471 (let ((start (point)) (paren-depth 0) pos
6472 ;; True if there's a non-open-paren match of
6473 ;; `c-type-decl-prefix-key'.
6474 got-prefix
6475 ;; True if the declarator is surrounded by a parenthesis pair.
6476 got-parens
6477 ;; True if there is an identifier in the declarator.
6478 got-identifier
6479 ;; True if there's a non-close-paren match of
6480 ;; `c-type-decl-suffix-key'.
6481 got-suffix
6482 ;; True if there's a prefix match outside the outermost
6483 ;; paren pair that surrounds the declarator.
6484 got-prefix-before-parens
6485 ;; True if there's a suffix match outside the outermost
6486 ;; paren pair that surrounds the declarator. The value is
6487 ;; the position of the first suffix match.
6488 got-suffix-after-parens
6489 ;; True if we've parsed the type decl to a token that is
6490 ;; known to end declarations in this context.
6491 at-decl-end
6492 ;; The earlier values of `at-type' and `type-start' if we've
6493 ;; shifted the type backwards.
6494 identifier-type identifier-start
6495 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6496 ;; turn it off during the name skipping below to avoid
6497 ;; getting `c-type' properties that might be bogus. That
6498 ;; can happen since we don't know if
6499 ;; `c-restricted-<>-arglists' will be correct inside the
6500 ;; arglist paren that gets entered.
6501 c-parse-and-markup-<>-arglists)
6503 (goto-char id-start)
6505 ;; Skip over type decl prefix operators. (Note similar code in
6506 ;; `c-font-lock-declarators'.)
6507 (while (and (looking-at c-type-decl-prefix-key)
6508 (if (and (c-major-mode-is 'c++-mode)
6509 (match-beginning 3))
6510 ;; If the second submatch matches in C++ then
6511 ;; we're looking at an identifier that's a
6512 ;; prefix only if it specifies a member pointer.
6513 (when (setq got-identifier (c-forward-name))
6514 (if (looking-at "\\(::\\)")
6515 ;; We only check for a trailing "::" and
6516 ;; let the "*" that should follow be
6517 ;; matched in the next round.
6518 (progn (setq got-identifier nil) t)
6519 ;; It turned out to be the real identifier,
6520 ;; so stop.
6521 nil))
6524 (if (eq (char-after) ?\()
6525 (progn
6526 (setq paren-depth (1+ paren-depth))
6527 (forward-char))
6528 (unless got-prefix-before-parens
6529 (setq got-prefix-before-parens (= paren-depth 0)))
6530 (setq got-prefix t)
6531 (goto-char (match-end 1)))
6532 (c-forward-syntactic-ws))
6534 (setq got-parens (> paren-depth 0))
6536 ;; Skip over an identifier.
6537 (or got-identifier
6538 (and (looking-at c-identifier-start)
6539 (setq got-identifier (c-forward-name))))
6541 ;; Skip over type decl suffix operators.
6542 (while (if (looking-at c-type-decl-suffix-key)
6544 (if (eq (char-after) ?\))
6545 (when (> paren-depth 0)
6546 (setq paren-depth (1- paren-depth))
6547 (forward-char)
6549 (when (if (save-match-data (looking-at "\\s\("))
6550 (c-safe (c-forward-sexp 1) t)
6551 (goto-char (match-end 1))
6553 (when (and (not got-suffix-after-parens)
6554 (= paren-depth 0))
6555 (setq got-suffix-after-parens (match-beginning 0)))
6556 (setq got-suffix t)))
6558 ;; No suffix matched. We might have matched the
6559 ;; identifier as a type and the open paren of a
6560 ;; function arglist as a type decl prefix. In that
6561 ;; case we should "backtrack": Reinterpret the last
6562 ;; type as the identifier, move out of the arglist and
6563 ;; continue searching for suffix operators.
6565 ;; Do this even if there's no preceding type, to cope
6566 ;; with old style function declarations in K&R C,
6567 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6568 ;; style declarations. That isn't applicable in an
6569 ;; arglist context, though.
6570 (when (and (= paren-depth 1)
6571 (not got-prefix-before-parens)
6572 (not (eq at-type t))
6573 (or backup-at-type
6574 maybe-typeless
6575 backup-maybe-typeless
6576 (when c-recognize-typeless-decls
6577 (not context)))
6578 (setq pos (c-up-list-forward (point)))
6579 (eq (char-before pos) ?\)))
6580 (c-fdoc-shift-type-backward)
6581 (goto-char pos)
6584 (c-forward-syntactic-ws))
6586 (when (and (or maybe-typeless backup-maybe-typeless)
6587 (not got-identifier)
6588 (not got-prefix)
6589 at-type)
6590 ;; Have found no identifier but `c-typeless-decl-kwds' has
6591 ;; matched so we know we're inside a declaration. The
6592 ;; preceding type must be the identifier instead.
6593 (c-fdoc-shift-type-backward))
6595 (setq
6596 at-decl-or-cast
6597 (catch 'at-decl-or-cast
6599 ;; CASE 1
6600 (when (> paren-depth 0)
6601 ;; Encountered something inside parens that isn't matched by
6602 ;; the `c-type-decl-*' regexps, so it's not a type decl
6603 ;; expression. Try to skip out to the same paren depth to
6604 ;; not confuse the cast check below.
6605 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6606 ;; If we've found a specifier keyword then it's a
6607 ;; declaration regardless.
6608 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6610 (setq at-decl-end
6611 (looking-at (cond ((eq context '<>) "[,>]")
6612 (context "[,\)]")
6613 (t "[,;]"))))
6615 ;; Now we've collected info about various characteristics of
6616 ;; the construct we're looking at. Below follows a decision
6617 ;; tree based on that. It's ordered to check more certain
6618 ;; signs before less certain ones.
6620 (if got-identifier
6621 (progn
6623 ;; CASE 2
6624 (when (and (or at-type maybe-typeless)
6625 (not (or got-prefix got-parens)))
6626 ;; Got another identifier directly after the type, so it's a
6627 ;; declaration.
6628 (throw 'at-decl-or-cast t))
6630 (when (and got-parens
6631 (not got-prefix)
6632 (not got-suffix-after-parens)
6633 (or backup-at-type
6634 maybe-typeless
6635 backup-maybe-typeless))
6636 ;; Got a declaration of the form "foo bar (gnu);" where we've
6637 ;; recognized "bar" as the type and "gnu" as the declarator.
6638 ;; In this case it's however more likely that "bar" is the
6639 ;; declarator and "gnu" a function argument or initializer (if
6640 ;; `c-recognize-paren-inits' is set), since the parens around
6641 ;; "gnu" would be superfluous if it's a declarator. Shift the
6642 ;; type one step backward.
6643 (c-fdoc-shift-type-backward)))
6645 ;; Found no identifier.
6647 (if backup-at-type
6648 (progn
6651 ;; CASE 3
6652 (when (= (point) start)
6653 ;; Got a plain list of identifiers. If a colon follows it's
6654 ;; a valid label, or maybe a bitfield. Otherwise the last
6655 ;; one probably is the declared identifier and we should
6656 ;; back up to the previous type, providing it isn't a cast.
6657 (if (and (eq (char-after) ?:)
6658 (not (c-major-mode-is 'java-mode)))
6659 (cond
6660 ;; If we've found a specifier keyword then it's a
6661 ;; declaration regardless.
6662 ((eq at-decl-or-cast t)
6663 (throw 'at-decl-or-cast t))
6664 ((and c-has-bitfields
6665 (eq at-decl-or-cast 'ids)) ; bitfield.
6666 (setq backup-if-not-cast t)
6667 (throw 'at-decl-or-cast t)))
6669 (setq backup-if-not-cast t)
6670 (throw 'at-decl-or-cast t)))
6672 ;; CASE 4
6673 (when (and got-suffix
6674 (not got-prefix)
6675 (not got-parens))
6676 ;; Got a plain list of identifiers followed by some suffix.
6677 ;; If this isn't a cast then the last identifier probably is
6678 ;; the declared one and we should back up to the previous
6679 ;; type.
6680 (setq backup-if-not-cast t)
6681 (throw 'at-decl-or-cast t)))
6683 ;; CASE 5
6684 (when (eq at-type t)
6685 ;; If the type is known we know that there can't be any
6686 ;; identifier somewhere else, and it's only in declarations in
6687 ;; e.g. function prototypes and in casts that the identifier may
6688 ;; be left out.
6689 (throw 'at-decl-or-cast t))
6691 (when (= (point) start)
6692 ;; Only got a single identifier (parsed as a type so far).
6693 ;; CASE 6
6694 (if (and
6695 ;; Check that the identifier isn't at the start of an
6696 ;; expression.
6697 at-decl-end
6698 (cond
6699 ((eq context 'decl)
6700 ;; Inside an arglist that contains declarations. If K&R
6701 ;; style declarations and parenthesis style initializers
6702 ;; aren't allowed then the single identifier must be a
6703 ;; type, else we require that it's known or found
6704 ;; (primitive types are handled above).
6705 (or (and (not c-recognize-knr-p)
6706 (not c-recognize-paren-inits))
6707 (memq at-type '(known found))))
6708 ((eq context '<>)
6709 ;; Inside a template arglist. Accept known and found
6710 ;; types; other identifiers could just as well be
6711 ;; constants in C++.
6712 (memq at-type '(known found)))))
6713 (throw 'at-decl-or-cast t)
6714 ;; CASE 7
6715 ;; Can't be a valid declaration or cast, but if we've found a
6716 ;; specifier it can't be anything else either, so treat it as
6717 ;; an invalid/unfinished declaration or cast.
6718 (throw 'at-decl-or-cast at-decl-or-cast))))
6720 (if (and got-parens
6721 (not got-prefix)
6722 (not context)
6723 (not (eq at-type t))
6724 (or backup-at-type
6725 maybe-typeless
6726 backup-maybe-typeless
6727 (when c-recognize-typeless-decls
6728 (or (not got-suffix)
6729 (not (looking-at
6730 c-after-suffixed-type-maybe-decl-key))))))
6731 ;; Got an empty paren pair and a preceding type that probably
6732 ;; really is the identifier. Shift the type backwards to make
6733 ;; the last one the identifier. This is analogous to the
6734 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6735 ;; above.
6737 ;; Exception: In addition to the conditions in that
6738 ;; "backtracking" code, do not shift backward if we're not
6739 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6740 ;; Since there's no preceding type, the shift would mean that
6741 ;; the declaration is typeless. But if the regexp doesn't match
6742 ;; then we will simply fall through in the tests below and not
6743 ;; recognize it at all, so it's better to try it as an abstract
6744 ;; declarator instead.
6745 (c-fdoc-shift-type-backward)
6747 ;; Still no identifier.
6748 ;; CASE 8
6749 (when (and got-prefix (or got-parens got-suffix))
6750 ;; Require `got-prefix' together with either `got-parens' or
6751 ;; `got-suffix' to recognize it as an abstract declarator:
6752 ;; `got-parens' only is probably an empty function call.
6753 ;; `got-suffix' only can build an ordinary expression together
6754 ;; with the preceding identifier which we've taken as a type.
6755 ;; We could actually accept on `got-prefix' only, but that can
6756 ;; easily occur temporarily while writing an expression so we
6757 ;; avoid that case anyway. We could do a better job if we knew
6758 ;; the point when the fontification was invoked.
6759 (throw 'at-decl-or-cast t))
6761 ;; CASE 9
6762 (when (and at-type
6763 (not got-prefix)
6764 (not got-parens)
6765 got-suffix-after-parens
6766 (eq (char-after got-suffix-after-parens) ?\())
6767 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6768 ;; normal function call afterall (or perhaps a C++ style object
6769 ;; instantiation expression).
6770 (throw 'at-decl-or-cast nil))))
6772 ;; CASE 10
6773 (when at-decl-or-cast
6774 ;; By now we've located the type in the declaration that we know
6775 ;; we're in.
6776 (throw 'at-decl-or-cast t))
6778 ;; CASE 11
6779 (when (and got-identifier
6780 (not context)
6781 (looking-at c-after-suffixed-type-decl-key)
6782 (if (and got-parens
6783 (not got-prefix)
6784 (not got-suffix)
6785 (not (eq at-type t)))
6786 ;; Shift the type backward in the case that there's a
6787 ;; single identifier inside parens. That can only
6788 ;; occur in K&R style function declarations so it's
6789 ;; more likely that it really is a function call.
6790 ;; Therefore we only do this after
6791 ;; `c-after-suffixed-type-decl-key' has matched.
6792 (progn (c-fdoc-shift-type-backward) t)
6793 got-suffix-after-parens))
6794 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6795 (throw 'at-decl-or-cast t))
6797 ;; CASE 12
6798 (when (and (or got-prefix (not got-parens))
6799 (memq at-type '(t known)))
6800 ;; It's a declaration if a known type precedes it and it can't be a
6801 ;; function call.
6802 (throw 'at-decl-or-cast t))
6804 ;; If we get here we can't tell if this is a type decl or a normal
6805 ;; expression by looking at it alone. (That's under the assumption
6806 ;; that normal expressions always can look like type decl expressions,
6807 ;; which isn't really true but the cases where it doesn't hold are so
6808 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6809 ;; the effort to look for them.)
6811 (unless (or at-decl-end (looking-at "=[^=]"))
6812 ;; If this is a declaration it should end here or its initializer(*)
6813 ;; should start here, so check for allowed separation tokens. Note
6814 ;; that this rule doesn't work e.g. with a K&R arglist after a
6815 ;; function header.
6817 ;; *) Don't check for C++ style initializers using parens
6818 ;; since those already have been matched as suffixes.
6820 ;; If `at-decl-or-cast' is then we've found some other sign that
6821 ;; it's a declaration or cast, so then it's probably an
6822 ;; invalid/unfinished one.
6823 (throw 'at-decl-or-cast at-decl-or-cast))
6825 ;; Below are tests that only should be applied when we're certain to
6826 ;; not have parsed halfway through an expression.
6828 ;; CASE 14
6829 (when (memq at-type '(t known))
6830 ;; The expression starts with a known type so treat it as a
6831 ;; declaration.
6832 (throw 'at-decl-or-cast t))
6834 ;; CASE 15
6835 (when (and (c-major-mode-is 'c++-mode)
6836 ;; In C++ we check if the identifier is a known type, since
6837 ;; (con|de)structors use the class name as identifier.
6838 ;; We've always shifted over the identifier as a type and
6839 ;; then backed up again in this case.
6840 identifier-type
6841 (or (memq identifier-type '(found known))
6842 (and (eq (char-after identifier-start) ?~)
6843 ;; `at-type' probably won't be 'found for
6844 ;; destructors since the "~" is then part of the
6845 ;; type name being checked against the list of
6846 ;; known types, so do a check without that
6847 ;; operator.
6848 (or (save-excursion
6849 (goto-char (1+ identifier-start))
6850 (c-forward-syntactic-ws)
6851 (c-with-syntax-table
6852 c-identifier-syntax-table
6853 (looking-at c-known-type-key)))
6854 (save-excursion
6855 (goto-char (1+ identifier-start))
6856 ;; We have already parsed the type earlier,
6857 ;; so it'd be possible to cache the end
6858 ;; position instead of redoing it here, but
6859 ;; then we'd need to keep track of another
6860 ;; position everywhere.
6861 (c-check-type (point)
6862 (progn (c-forward-type)
6863 (point))))))))
6864 (throw 'at-decl-or-cast t))
6866 (if got-identifier
6867 (progn
6868 ;; CASE 16
6869 (when (and got-prefix-before-parens
6870 at-type
6871 (or at-decl-end (looking-at "=[^=]"))
6872 (not context)
6873 (not got-suffix))
6874 ;; Got something like "foo * bar;". Since we're not inside an
6875 ;; arglist it would be a meaningless expression because the
6876 ;; result isn't used. We therefore choose to recognize it as
6877 ;; a declaration. Do not allow a suffix since it could then
6878 ;; be a function call.
6879 (throw 'at-decl-or-cast t))
6881 ;; CASE 17
6882 (when (and (or got-suffix-after-parens
6883 (looking-at "=[^=]"))
6884 (eq at-type 'found)
6885 (not (eq context 'arglist)))
6886 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
6887 ;; be an odd expression or it could be a declaration. Treat
6888 ;; it as a declaration if "a" has been used as a type
6889 ;; somewhere else (if it's a known type we won't get here).
6890 (throw 'at-decl-or-cast t)))
6892 ;; CASE 18
6893 (when (and context
6894 (or got-prefix
6895 (and (eq context 'decl)
6896 (not c-recognize-paren-inits)
6897 (or got-parens got-suffix))))
6898 ;; Got a type followed by an abstract declarator. If `got-prefix'
6899 ;; is set it's something like "a *" without anything after it. If
6900 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
6901 ;; or similar, which we accept only if the context rules out
6902 ;; expressions.
6903 (throw 'at-decl-or-cast t)))
6905 ;; If we had a complete symbol table here (which rules out
6906 ;; `c-found-types') we should return t due to the disambiguation rule
6907 ;; (in at least C++) that anything that can be parsed as a declaration
6908 ;; is a declaration. Now we're being more defensive and prefer to
6909 ;; highlight things like "foo (bar);" as a declaration only if we're
6910 ;; inside an arglist that contains declarations.
6911 (eq context 'decl))))
6913 ;; The point is now after the type decl expression.
6915 (cond
6916 ;; Check for a cast.
6917 ((save-excursion
6918 (and
6919 c-cast-parens
6921 ;; Should be the first type/identifier in a cast paren.
6922 (> preceding-token-end (point-min))
6923 (memq (char-before preceding-token-end) c-cast-parens)
6925 ;; The closing paren should follow.
6926 (progn
6927 (c-forward-syntactic-ws)
6928 (looking-at "\\s\)"))
6930 ;; There should be a primary expression after it.
6931 (let (pos)
6932 (forward-char)
6933 (c-forward-syntactic-ws)
6934 (setq cast-end (point))
6935 (and (looking-at c-primary-expr-regexp)
6936 (progn
6937 (setq pos (match-end 0))
6939 ;; Check if the expression begins with a prefix keyword.
6940 (match-beginning 2)
6941 (if (match-beginning 1)
6942 ;; Expression begins with an ambiguous operator. Treat
6943 ;; it as a cast if it's a type decl or if we've
6944 ;; recognized the type somewhere else.
6945 (or at-decl-or-cast
6946 (memq at-type '(t known found)))
6947 ;; Unless it's a keyword, it's the beginning of a primary
6948 ;; expression.
6949 (not (looking-at c-keywords-regexp)))))
6950 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
6951 ;; that it matched a whole one so that we don't e.g. confuse
6952 ;; the operator '-' with '->'. It's ok if it matches further,
6953 ;; though, since it e.g. can match the float '.5' while the
6954 ;; operator regexp only matches '.'.
6955 (or (not (looking-at c-nonsymbol-token-regexp))
6956 (<= (match-end 0) pos))))
6958 ;; There should either be a cast before it or something that isn't an
6959 ;; identifier or close paren.
6960 (> preceding-token-end (point-min))
6961 (progn
6962 (goto-char (1- preceding-token-end))
6963 (or (eq (point) last-cast-end)
6964 (progn
6965 (c-backward-syntactic-ws)
6966 (if (< (skip-syntax-backward "w_") 0)
6967 ;; It's a symbol. Accept it only if it's one of the
6968 ;; keywords that can precede an expression (without
6969 ;; surrounding parens).
6970 (looking-at c-simple-stmt-key)
6971 (and
6972 ;; Check that it isn't a close paren (block close is ok,
6973 ;; though).
6974 (not (memq (char-before) '(?\) ?\])))
6975 ;; Check that it isn't a nonsymbol identifier.
6976 (not (c-on-identifier)))))))))
6978 ;; Handle the cast.
6979 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
6980 (let ((c-promote-possible-types t))
6981 (goto-char type-start)
6982 (c-forward-type)))
6984 (goto-char cast-end)
6985 'cast)
6987 (at-decl-or-cast
6988 ;; We're at a declaration. Highlight the type and the following
6989 ;; declarators.
6991 (when backup-if-not-cast
6992 (c-fdoc-shift-type-backward t))
6994 (when (and (eq context 'decl) (looking-at ","))
6995 ;; Make sure to propagate the `c-decl-arg-start' property to
6996 ;; the next argument if it's set in this one, to cope with
6997 ;; interactive refontification.
6998 (c-put-c-type-property (point) 'c-decl-arg-start))
7000 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7001 (let ((c-promote-possible-types t))
7002 (save-excursion
7003 (goto-char type-start)
7004 (c-forward-type))))
7006 (cons id-start
7007 (and (or at-type-decl at-typedef)
7008 (cons at-type-decl at-typedef))))
7011 ;; False alarm. Restore the recorded ranges.
7012 (setq c-record-type-identifiers save-rec-type-ids
7013 c-record-ref-identifiers save-rec-ref-ids)
7014 nil))))
7016 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
7017 ;; Assuming that point is at the beginning of a token, check if it starts a
7018 ;; label and if so move over it and return non-nil (t in default situations,
7019 ;; specific symbols (see below) for interesting situations), otherwise don't
7020 ;; move and return nil. "Label" here means "most things with a colon".
7022 ;; More precisely, a "label" is regarded as one of:
7023 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7024 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7025 ;; bare "case", should the colon be missing. We return t;
7026 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7027 ;; return t;
7028 ;; (iv) One of QT's "extended" C++ variants of
7029 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7030 ;; Returns the symbol `qt-2kwds-colon'.
7031 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
7032 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
7033 ;; colon). Currently (2006-03), this applies only to Objective C's
7034 ;; keywords "@private", "@protected", and "@public". Returns t.
7036 ;; One of the things which will NOT be recognised as a label is a bit-field
7037 ;; element of a struct, something like "int foo:5".
7039 ;; The end of the label is taken to be just after the colon, or the end of
7040 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7041 ;; after the end on return. The terminating char gets marked with
7042 ;; `c-decl-end' to improve recognition of the following declaration or
7043 ;; statement.
7045 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7046 ;; label, if any, has already been marked up like that.
7048 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7049 ;; after the preceding token, i.e. on the other side of the
7050 ;; syntactic ws from the point. Use a value less than or equal to
7051 ;; (point-min) if the point is at the first token in (the visible
7052 ;; part of) the buffer.
7054 ;; The optional LIMIT limits the forward scan for the colon.
7056 ;; This function records the ranges of the label symbols on
7057 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7058 ;; non-nil.
7060 ;; This function might do hidden buffer changes.
7062 (let ((start (point))
7063 label-end
7064 qt-symbol-idx
7065 macro-start ; if we're in one.
7066 label-type
7067 kwd)
7068 (cond
7069 ;; "case" or "default" (Doesn't apply to AWK).
7070 ((looking-at c-label-kwds-regexp)
7071 (let ((kwd-end (match-end 1)))
7072 ;; Record only the keyword itself for fontification, since in
7073 ;; case labels the following is a constant expression and not
7074 ;; a label.
7075 (when c-record-type-identifiers
7076 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7078 ;; Find the label end.
7079 (goto-char kwd-end)
7080 (setq label-type
7081 (if (and (c-syntactic-re-search-forward
7082 ;; Stop on chars that aren't allowed in expressions,
7083 ;; and on operator chars that would be meaningless
7084 ;; there. FIXME: This doesn't cope with ?: operators.
7085 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7086 limit t t nil 1)
7087 (match-beginning 2))
7089 (progn ; there's a proper :
7090 (goto-char (match-beginning 2)) ; just after the :
7091 (c-put-c-type-property (1- (point)) 'c-decl-end)
7094 ;; It's an unfinished label. We consider the keyword enough
7095 ;; to recognize it as a label, so that it gets fontified.
7096 ;; Leave the point at the end of it, but don't put any
7097 ;; `c-decl-end' marker.
7098 (goto-char kwd-end)
7099 t))))
7101 ;; @private, @protected, @public, in Objective C, or similar.
7102 ((and c-opt-extra-label-key
7103 (looking-at c-opt-extra-label-key))
7104 ;; For a `c-opt-extra-label-key' match, we record the whole
7105 ;; thing for fontification. That's to get the leading '@' in
7106 ;; Objective-C protection labels fontified.
7107 (goto-char (match-end 1))
7108 (when c-record-type-identifiers
7109 (c-record-ref-id (cons (match-beginning 1) (point))))
7110 (c-put-c-type-property (1- (point)) 'c-decl-end)
7111 (setq label-type t))
7113 ;; All other cases of labels.
7114 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
7116 ;; A colon label must have something before the colon.
7117 (not (eq (char-after) ?:))
7119 ;; Check that we're not after a token that can't precede a label.
7121 ;; Trivially succeeds when there's no preceding token.
7122 (if preceding-token-end
7123 (<= preceding-token-end (point-min))
7124 (save-excursion
7125 (c-backward-syntactic-ws)
7126 (setq preceding-token-end (point))
7127 (bobp)))
7129 ;; Check if we're after a label, if we're after a closing
7130 ;; paren that belong to statement, and with
7131 ;; `c-label-prefix-re'. It's done in different order
7132 ;; depending on `assume-markup' since the checks have
7133 ;; different expensiveness.
7134 (if assume-markup
7136 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7137 'c-decl-end)
7139 (save-excursion
7140 (goto-char (1- preceding-token-end))
7141 (c-beginning-of-current-token)
7142 (or (looking-at c-label-prefix-re)
7143 (looking-at c-block-stmt-1-key)))
7145 (and (eq (char-before preceding-token-end) ?\))
7146 (c-after-conditional)))
7149 (save-excursion
7150 (goto-char (1- preceding-token-end))
7151 (c-beginning-of-current-token)
7152 (or (looking-at c-label-prefix-re)
7153 (looking-at c-block-stmt-1-key)))
7155 (cond
7156 ((eq (char-before preceding-token-end) ?\))
7157 (c-after-conditional))
7159 ((eq (char-before preceding-token-end) ?:)
7160 ;; Might be after another label, so check it recursively.
7161 (save-restriction
7162 (save-excursion
7163 (goto-char (1- preceding-token-end))
7164 ;; Essentially the same as the
7165 ;; `c-syntactic-re-search-forward' regexp below.
7166 (setq macro-start
7167 (save-excursion (and (c-beginning-of-macro)
7168 (point))))
7169 (if macro-start (narrow-to-region macro-start (point-max)))
7170 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7171 ;; Note: the following should work instead of the
7172 ;; narrow-to-region above. Investigate why not,
7173 ;; sometime. ACM, 2006-03-31.
7174 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7175 ;; macro-start t)
7176 (let ((pte (point))
7177 ;; If the caller turned on recording for us,
7178 ;; it shouldn't apply when we check the
7179 ;; preceding label.
7180 c-record-type-identifiers)
7181 ;; A label can't start at a cpp directive. Check for
7182 ;; this, since c-forward-syntactic-ws would foul up on it.
7183 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7184 (c-forward-syntactic-ws)
7185 (c-forward-label nil pte start))))))))))
7187 ;; Point is still at the beginning of the possible label construct.
7189 ;; Check that the next nonsymbol token is ":", or that we're in one
7190 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7191 ;; arguments. FIXME: Should build this regexp from the language
7192 ;; constants.
7193 (cond
7194 ;; public: protected: private:
7195 ((and
7196 (c-major-mode-is 'c++-mode)
7197 (search-forward-regexp
7198 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7199 (progn (backward-char)
7200 (c-forward-syntactic-ws limit)
7201 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7202 (forward-char)
7203 (setq label-type t))
7204 ;; QT double keyword like "protected slots:" or goto target.
7205 ((progn (goto-char start) nil))
7206 ((when (c-syntactic-re-search-forward
7207 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7208 (backward-char)
7209 (setq label-end (point))
7210 (setq qt-symbol-idx
7211 (and (c-major-mode-is 'c++-mode)
7212 (string-match
7213 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7214 (buffer-substring start (point)))))
7215 (c-forward-syntactic-ws limit)
7216 (cond
7217 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7218 (forward-char)
7219 (setq label-type
7220 (if (or (string= "signals" ; Special QT macro
7221 (setq kwd (buffer-substring-no-properties start label-end)))
7222 (string= "Q_SIGNALS" kwd))
7223 'qt-1kwd-colon
7224 'goto-target)))
7225 ((and qt-symbol-idx
7226 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7227 (progn (c-forward-syntactic-ws limit)
7228 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7229 (forward-char)
7230 (setq label-type 'qt-2kwds-colon)))))))
7232 (save-restriction
7233 (narrow-to-region start (point))
7235 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7236 (catch 'check-label
7237 (goto-char start)
7238 (while (progn
7239 (when (looking-at c-nonlabel-token-key)
7240 (goto-char start)
7241 (setq label-type nil)
7242 (throw 'check-label nil))
7243 (and (c-safe (c-forward-sexp)
7244 (c-forward-syntactic-ws)
7246 (not (eobp)))))
7248 ;; Record the identifiers in the label for fontification, unless
7249 ;; it begins with `c-label-kwds' in which case the following
7250 ;; identifiers are part of a (constant) expression that
7251 ;; shouldn't be fontified.
7252 (when (and c-record-type-identifiers
7253 (progn (goto-char start)
7254 (not (looking-at c-label-kwds-regexp))))
7255 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7256 (c-record-ref-id (cons (match-beginning 0)
7257 (match-end 0)))))
7259 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7260 (goto-char (point-max)))))
7263 ;; Not a label.
7264 (goto-char start)))
7265 label-type))
7267 (defun c-forward-objc-directive ()
7268 ;; Assuming the point is at the beginning of a token, try to move
7269 ;; forward to the end of the Objective-C directive that starts
7270 ;; there. Return t if a directive was fully recognized, otherwise
7271 ;; the point is moved as far as one could be successfully parsed and
7272 ;; nil is returned.
7274 ;; This function records identifier ranges on
7275 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7276 ;; `c-record-type-identifiers' is non-nil.
7278 ;; This function might do hidden buffer changes.
7280 (let ((start (point))
7281 start-char
7282 (c-promote-possible-types t)
7283 ;; Turn off recognition of angle bracket arglists while parsing
7284 ;; types here since the protocol reference list might then be
7285 ;; considered part of the preceding name or superclass-name.
7286 c-recognize-<>-arglists)
7288 (if (or
7289 (when (looking-at
7290 (eval-when-compile
7291 (c-make-keywords-re t
7292 (append (c-lang-const c-protection-kwds objc)
7293 '("@end"))
7294 'objc-mode)))
7295 (goto-char (match-end 1))
7298 (and
7299 (looking-at
7300 (eval-when-compile
7301 (c-make-keywords-re t
7302 '("@interface" "@implementation" "@protocol")
7303 'objc-mode)))
7305 ;; Handle the name of the class itself.
7306 (progn
7307 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7308 ; at EOB.
7309 (goto-char (match-end 0))
7310 (c-skip-ws-forward)
7311 (c-forward-type))
7313 (catch 'break
7314 ;; Look for ": superclass-name" or "( category-name )".
7315 (when (looking-at "[:\(]")
7316 (setq start-char (char-after))
7317 (forward-char)
7318 (c-forward-syntactic-ws)
7319 (unless (c-forward-type) (throw 'break nil))
7320 (when (eq start-char ?\()
7321 (unless (eq (char-after) ?\)) (throw 'break nil))
7322 (forward-char)
7323 (c-forward-syntactic-ws)))
7325 ;; Look for a protocol reference list.
7326 (if (eq (char-after) ?<)
7327 (let ((c-recognize-<>-arglists t)
7328 (c-parse-and-markup-<>-arglists t)
7329 c-restricted-<>-arglists)
7330 (c-forward-<>-arglist t))
7331 t))))
7333 (progn
7334 (c-backward-syntactic-ws)
7335 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7336 (c-put-c-type-property (1- (point)) 'c-decl-end)
7339 (c-clear-c-type-property start (point) 'c-decl-end)
7340 nil)))
7342 (defun c-beginning-of-inheritance-list (&optional lim)
7343 ;; Go to the first non-whitespace after the colon that starts a
7344 ;; multiple inheritance introduction. Optional LIM is the farthest
7345 ;; back we should search.
7347 ;; This function might do hidden buffer changes.
7348 (c-with-syntax-table c++-template-syntax-table
7349 (c-backward-token-2 0 t lim)
7350 (while (and (or (looking-at c-symbol-start)
7351 (looking-at "[<,]\\|::"))
7352 (zerop (c-backward-token-2 1 t lim))))))
7354 (defun c-in-method-def-p ()
7355 ;; Return nil if we aren't in a method definition, otherwise the
7356 ;; position of the initial [+-].
7358 ;; This function might do hidden buffer changes.
7359 (save-excursion
7360 (beginning-of-line)
7361 (and c-opt-method-key
7362 (looking-at c-opt-method-key)
7363 (point))
7366 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7367 (defun c-in-gcc-asm-p ()
7368 ;; Return non-nil if point is within a gcc \"asm\" block.
7370 ;; This should be called with point inside an argument list.
7372 ;; Only one level of enclosing parentheses is considered, so for
7373 ;; instance `nil' is returned when in a function call within an asm
7374 ;; operand.
7376 ;; This function might do hidden buffer changes.
7378 (and c-opt-asm-stmt-key
7379 (save-excursion
7380 (beginning-of-line)
7381 (backward-up-list 1)
7382 (c-beginning-of-statement-1 (point-min) nil t)
7383 (looking-at c-opt-asm-stmt-key))))
7385 (defun c-at-toplevel-p ()
7386 "Return a determination as to whether point is \"at the top level\".
7387 Informally, \"at the top level\" is anywhere where you can write
7388 a function.
7390 More precisely, being at the top-level means that point is either
7391 outside any enclosing block (such as a function definition), or
7392 directly inside a class, namespace or other block that contains
7393 another declaration level.
7395 If point is not at the top-level (e.g. it is inside a method
7396 definition), then nil is returned. Otherwise, if point is at a
7397 top-level not enclosed within a class definition, t is returned.
7398 Otherwise, a 2-vector is returned where the zeroth element is the
7399 buffer position of the start of the class declaration, and the first
7400 element is the buffer position of the enclosing class's opening
7401 brace.
7403 Note that this function might do hidden buffer changes. See the
7404 comment at the start of cc-engine.el for more info."
7405 (let ((paren-state (c-parse-state)))
7406 (or (not (c-most-enclosing-brace paren-state))
7407 (c-search-uplist-for-classkey paren-state))))
7409 (defun c-just-after-func-arglist-p (&optional lim)
7410 ;; Return non-nil if the point is in the region after the argument
7411 ;; list of a function and its opening brace (or semicolon in case it
7412 ;; got no body). If there are K&R style argument declarations in
7413 ;; that region, the point has to be inside the first one for this
7414 ;; function to recognize it.
7416 ;; If successful, the point is moved to the first token after the
7417 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7418 ;; the position of the opening paren of the function arglist is
7419 ;; returned.
7421 ;; The point is clobbered if not successful.
7423 ;; LIM is used as bound for backward buffer searches.
7425 ;; This function might do hidden buffer changes.
7427 (let ((beg (point)) end id-start)
7428 (and
7429 (eq (c-beginning-of-statement-1 lim) 'same)
7431 (not (or (c-major-mode-is 'objc-mode)
7432 (c-forward-objc-directive)))
7434 (setq id-start
7435 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7436 (< id-start beg)
7438 ;; There should not be a '=' or ',' between beg and the
7439 ;; start of the declaration since that means we were in the
7440 ;; "expression part" of the declaration.
7441 (or (> (point) beg)
7442 (not (looking-at "[=,]")))
7444 (save-excursion
7445 ;; Check that there's an arglist paren in the
7446 ;; declaration.
7447 (goto-char id-start)
7448 (cond ((eq (char-after) ?\()
7449 ;; The declarator is a paren expression, so skip past it
7450 ;; so that we don't get stuck on that instead of the
7451 ;; function arglist.
7452 (c-forward-sexp))
7453 ((and c-opt-op-identifier-prefix
7454 (looking-at c-opt-op-identifier-prefix))
7455 ;; Don't trip up on "operator ()".
7456 (c-forward-token-2 2 t)))
7457 (and (< (point) beg)
7458 (c-syntactic-re-search-forward "(" beg t t)
7459 (1- (point)))))))
7461 (defun c-in-knr-argdecl (&optional lim)
7462 ;; Return the position of the first argument declaration if point is
7463 ;; inside a K&R style argument declaration list, nil otherwise.
7464 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7465 ;; position that bounds the backward search for the argument list.
7467 ;; Point must be within a possible K&R region, e.g. just before a top-level
7468 ;; "{". It must be outside of parens and brackets. The test can return
7469 ;; false positives otherwise.
7471 ;; This function might do hidden buffer changes.
7473 (save-excursion
7474 (save-restriction
7475 ;; If we're in a macro, our search range is restricted to it. Narrow to
7476 ;; the searchable range.
7477 (let* ((macro-start (c-query-macro-start))
7478 (lim (max (or lim (point-min)) (or macro-start (point-min))))
7479 before-lparen after-rparen
7480 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
7481 (narrow-to-region lim (c-point 'eol))
7483 ;; Search backwards for the defun's argument list. We give up if we
7484 ;; encounter a "}" (end of a previous defun) or BOB.
7486 ;; The criterion for a paren structure being the arg list is:
7487 ;; o - there is non-WS stuff after it but before any "{"; AND
7488 ;; o - the token after it isn't a ";" AND
7489 ;; o - it is preceded by either an identifier (the function name) or
7490 ;; a macro expansion like "DEFUN (...)"; AND
7491 ;; o - its content is a non-empty comma-separated list of identifiers
7492 ;; (an empty arg list won't have a knr region).
7494 ;; The following snippet illustrates these rules:
7495 ;; int foo (bar, baz, yuk)
7496 ;; int bar [] ;
7497 ;; int (*baz) (my_type) ;
7498 ;; int (*) (void) (*yuk) (void) ;
7499 ;; {
7501 (catch 'knr
7502 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7503 (setq pp-count-out (1- pp-count-out))
7504 (c-syntactic-skip-backward "^)]}")
7505 (cond ((eq (char-before) ?\))
7506 (setq after-rparen (point)))
7507 ((eq (char-before) ?\])
7508 (setq after-rparen nil))
7509 (t ; either } (hit previous defun) or no more parens/brackets
7510 (throw 'knr nil)))
7512 (if after-rparen
7513 ;; We're inside a paren. Could it be our argument list....?
7515 (and
7516 (progn
7517 (goto-char after-rparen)
7518 (unless (c-go-list-backward) (throw 'knr nil)) ;
7519 ;; FIXME!!! What about macros between the parens? 2007/01/20
7520 (setq before-lparen (point)))
7522 ;; It can't be the arg list if next token is ; or {
7523 (progn (goto-char after-rparen)
7524 (c-forward-syntactic-ws)
7525 (not (memq (char-after) '(?\; ?\{))))
7527 ;; Is the thing preceding the list an identifier (the
7528 ;; function name), or a macro expansion?
7529 (progn
7530 (goto-char before-lparen)
7531 (eq (c-backward-token-2) 0)
7532 (or (c-on-identifier)
7533 (and (eq (char-after) ?\))
7534 (c-go-up-list-backward)
7535 (eq (c-backward-token-2) 0)
7536 (c-on-identifier))))
7538 ;; Have we got a non-empty list of comma-separated
7539 ;; identifiers?
7540 (progn
7541 (goto-char before-lparen)
7542 (c-forward-token-2) ; to first token inside parens
7543 (and
7544 (c-on-identifier)
7545 (c-forward-token-2)
7546 (catch 'id-list
7547 (while (eq (char-after) ?\,)
7548 (c-forward-token-2)
7549 (unless (c-on-identifier) (throw 'id-list nil))
7550 (c-forward-token-2))
7551 (eq (char-after) ?\))))))
7553 ;; ...Yes. We've identified the function's argument list.
7554 (throw 'knr
7555 (progn (goto-char after-rparen)
7556 (c-forward-syntactic-ws)
7557 (point)))
7559 ;; ...No. The current parens aren't the function's arg list.
7560 (goto-char before-lparen))
7562 (or (c-go-list-backward) ; backwards over [ .... ]
7563 (throw 'knr nil)))))))))
7565 (defun c-skip-conditional ()
7566 ;; skip forward over conditional at point, including any predicate
7567 ;; statements in parentheses. No error checking is performed.
7569 ;; This function might do hidden buffer changes.
7570 (c-forward-sexp (cond
7571 ;; else if()
7572 ((looking-at (concat "\\<else"
7573 "\\([ \t\n]\\|\\\\\n\\)+"
7574 "if\\>\\([^_]\\|$\\)"))
7576 ;; do, else, try, finally
7577 ((looking-at (concat "\\<\\("
7578 "do\\|else\\|try\\|finally"
7579 "\\)\\>\\([^_]\\|$\\)"))
7581 ;; for, if, while, switch, catch, synchronized, foreach
7582 (t 2))))
7584 (defun c-after-conditional (&optional lim)
7585 ;; If looking at the token after a conditional then return the
7586 ;; position of its start, otherwise return nil.
7588 ;; This function might do hidden buffer changes.
7589 (save-excursion
7590 (and (zerop (c-backward-token-2 1 t lim))
7591 (or (looking-at c-block-stmt-1-key)
7592 (and (eq (char-after) ?\()
7593 (zerop (c-backward-token-2 1 t lim))
7594 (looking-at c-block-stmt-2-key)))
7595 (point))))
7597 (defun c-after-special-operator-id (&optional lim)
7598 ;; If the point is after an operator identifier that isn't handled
7599 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7600 ;; position of the start of that identifier is returned. nil is
7601 ;; returned otherwise. The point may be anywhere in the syntactic
7602 ;; whitespace after the last token of the operator identifier.
7604 ;; This function might do hidden buffer changes.
7605 (save-excursion
7606 (and c-overloadable-operators-regexp
7607 (zerop (c-backward-token-2 1 nil lim))
7608 (looking-at c-overloadable-operators-regexp)
7609 (or (not c-opt-op-identifier-prefix)
7610 (and
7611 (zerop (c-backward-token-2 1 nil lim))
7612 (looking-at c-opt-op-identifier-prefix)))
7613 (point))))
7615 (defsubst c-backward-to-block-anchor (&optional lim)
7616 ;; Assuming point is at a brace that opens a statement block of some
7617 ;; kind, move to the proper anchor point for that block. It might
7618 ;; need to be adjusted further by c-add-stmt-syntax, but the
7619 ;; position at return is suitable as start position for that
7620 ;; function.
7622 ;; This function might do hidden buffer changes.
7623 (unless (= (point) (c-point 'boi))
7624 (let ((start (c-after-conditional lim)))
7625 (if start
7626 (goto-char start)))))
7628 (defsubst c-backward-to-decl-anchor (&optional lim)
7629 ;; Assuming point is at a brace that opens the block of a top level
7630 ;; declaration of some kind, move to the proper anchor point for
7631 ;; that block.
7633 ;; This function might do hidden buffer changes.
7634 (unless (= (point) (c-point 'boi))
7635 (c-beginning-of-statement-1 lim)))
7637 (defun c-search-decl-header-end ()
7638 ;; Search forward for the end of the "header" of the current
7639 ;; declaration. That's the position where the definition body
7640 ;; starts, or the first variable initializer, or the ending
7641 ;; semicolon. I.e. search forward for the closest following
7642 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7643 ;; _after_ the first found token, or at point-max if none is found.
7645 ;; This function might do hidden buffer changes.
7647 (let ((base (point)))
7648 (if (c-major-mode-is 'c++-mode)
7650 ;; In C++ we need to take special care to handle operator
7651 ;; tokens and those pesky template brackets.
7652 (while (and
7653 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
7655 (c-end-of-current-token base)
7656 ;; Handle operator identifiers, i.e. ignore any
7657 ;; operator token preceded by "operator".
7658 (save-excursion
7659 (and (c-safe (c-backward-sexp) t)
7660 (looking-at c-opt-op-identifier-prefix)))
7661 (and (eq (char-before) ?<)
7662 (c-with-syntax-table c++-template-syntax-table
7663 (if (c-safe (goto-char (c-up-list-forward (point))))
7665 (goto-char (point-max))
7666 nil)))))
7667 (setq base (point)))
7669 (while (and
7670 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
7671 (c-end-of-current-token base))
7672 (setq base (point))))))
7674 (defun c-beginning-of-decl-1 (&optional lim)
7675 ;; Go to the beginning of the current declaration, or the beginning
7676 ;; of the previous one if already at the start of it. Point won't
7677 ;; be moved out of any surrounding paren. Return a cons cell of the
7678 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7679 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7680 ;; style argument declarations (and they are to be recognized) then
7681 ;; KNR-POS is set to the start of the first such argument
7682 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7683 ;; position that bounds the backward search.
7685 ;; NB: Cases where the declaration continues after the block, as in
7686 ;; "struct foo { ... } bar;", are currently recognized as two
7687 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7689 ;; This function might do hidden buffer changes.
7690 (catch 'return
7691 (let* ((start (point))
7692 (last-stmt-start (point))
7693 (move (c-beginning-of-statement-1 lim nil t)))
7695 ;; `c-beginning-of-statement-1' stops at a block start, but we
7696 ;; want to continue if the block doesn't begin a top level
7697 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7698 ;; or an open paren.
7699 (let ((beg (point)) tentative-move)
7700 ;; Go back one "statement" each time round the loop until we're just
7701 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7702 ;; an ObjC method. This will move over a multiple declaration whose
7703 ;; components are comma separated.
7704 (while (and
7705 ;; Must check with c-opt-method-key in ObjC mode.
7706 (not (and c-opt-method-key
7707 (looking-at c-opt-method-key)))
7708 (/= last-stmt-start (point))
7709 (progn
7710 (c-backward-syntactic-ws lim)
7711 (not (memq (char-before) '(?\; ?} ?: nil))))
7712 (save-excursion
7713 (backward-char)
7714 (not (looking-at "\\s(")))
7715 ;; Check that we don't move from the first thing in a
7716 ;; macro to its header.
7717 (not (eq (setq tentative-move
7718 (c-beginning-of-statement-1 lim nil t))
7719 'macro)))
7720 (setq last-stmt-start beg
7721 beg (point)
7722 move tentative-move))
7723 (goto-char beg))
7725 (when c-recognize-knr-p
7726 (let ((fallback-pos (point)) knr-argdecl-start)
7727 ;; Handle K&R argdecls. Back up after the "statement" jumped
7728 ;; over by `c-beginning-of-statement-1', unless it was the
7729 ;; function body, in which case we're sitting on the opening
7730 ;; brace now. Then test if we're in a K&R argdecl region and
7731 ;; that we started at the other side of the first argdecl in
7732 ;; it.
7733 (unless (eq (char-after) ?{)
7734 (goto-char last-stmt-start))
7735 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
7736 (< knr-argdecl-start start)
7737 (progn
7738 (goto-char knr-argdecl-start)
7739 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
7740 (throw 'return
7741 (cons (if (eq (char-after fallback-pos) ?{)
7742 'previous
7743 'same)
7744 knr-argdecl-start))
7745 (goto-char fallback-pos))))
7747 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7748 ;; statement, so the result will be 'previous if we've moved over any.
7749 ;; So change our result back to 'same if necessary.
7751 ;; If they were brace list initializers we might not have moved over a
7752 ;; declaration boundary though, so change it to 'same if we've moved
7753 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7754 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7755 ;; potentially can search over a large amount of text.). Take special
7756 ;; pains not to get mislead by C++'s "operator=", and the like.
7757 (if (and (eq move 'previous)
7758 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
7759 c++-template-syntax-table
7760 (syntax-table))
7761 (save-excursion
7762 (and
7763 (progn
7764 (while ; keep going back to "[;={"s until we either find
7765 ; no more, or get to one which isn't an "operator ="
7766 (and (c-syntactic-re-search-forward "[;={]" start t t t)
7767 (eq (char-before) ?=)
7768 c-overloadable-operators-regexp
7769 c-opt-op-identifier-prefix
7770 (save-excursion
7771 (eq (c-backward-token-2) 0)
7772 (looking-at c-overloadable-operators-regexp)
7773 (eq (c-backward-token-2) 0)
7774 (looking-at c-opt-op-identifier-prefix))))
7775 (eq (char-before) ?=))
7776 (c-syntactic-re-search-forward "[;{]" start t t)
7777 (eq (char-before) ?{)
7778 (c-safe (goto-char (c-up-list-forward (point))) t)
7779 (not (c-syntactic-re-search-forward ";" start t t))))))
7780 (cons 'same nil)
7781 (cons move nil)))))
7783 (defun c-end-of-decl-1 ()
7784 ;; Assuming point is at the start of a declaration (as detected by
7785 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7786 ;; `c-beginning-of-decl-1', this function handles the case when a
7787 ;; block is followed by identifiers in e.g. struct declarations in C
7788 ;; or C++. If a proper end was found then t is returned, otherwise
7789 ;; point is moved as far as possible within the current sexp and nil
7790 ;; is returned. This function doesn't handle macros; use
7791 ;; `c-end-of-macro' instead in those cases.
7793 ;; This function might do hidden buffer changes.
7794 (let ((start (point))
7795 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
7796 c++-template-syntax-table
7797 (syntax-table))))
7798 (catch 'return
7799 (c-search-decl-header-end)
7801 (when (and c-recognize-knr-p
7802 (eq (char-before) ?\;)
7803 (c-in-knr-argdecl start))
7804 ;; Stopped at the ';' in a K&R argdecl section which is
7805 ;; detected using the same criteria as in
7806 ;; `c-beginning-of-decl-1'. Move to the following block
7807 ;; start.
7808 (c-syntactic-re-search-forward "{" nil 'move t))
7810 (when (eq (char-before) ?{)
7811 ;; Encountered a block in the declaration. Jump over it.
7812 (condition-case nil
7813 (goto-char (c-up-list-forward (point)))
7814 (error (goto-char (point-max))
7815 (throw 'return nil)))
7816 (if (or (not c-opt-block-decls-with-vars-key)
7817 (save-excursion
7818 (c-with-syntax-table decl-syntax-table
7819 (let ((lim (point)))
7820 (goto-char start)
7821 (not (and
7822 ;; Check for `c-opt-block-decls-with-vars-key'
7823 ;; before the first paren.
7824 (c-syntactic-re-search-forward
7825 (concat "[;=\(\[{]\\|\\("
7826 c-opt-block-decls-with-vars-key
7827 "\\)")
7828 lim t t t)
7829 (match-beginning 1)
7830 (not (eq (char-before) ?_))
7831 ;; Check that the first following paren is
7832 ;; the block.
7833 (c-syntactic-re-search-forward "[;=\(\[{]"
7834 lim t t t)
7835 (eq (char-before) ?{)))))))
7836 ;; The declaration doesn't have any of the
7837 ;; `c-opt-block-decls-with-vars' keywords in the
7838 ;; beginning, so it ends here at the end of the block.
7839 (throw 'return t)))
7841 (c-with-syntax-table decl-syntax-table
7842 (while (progn
7843 (if (eq (char-before) ?\;)
7844 (throw 'return t))
7845 (c-syntactic-re-search-forward ";" nil 'move t))))
7846 nil)))
7848 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
7849 ;; Assuming the point is at an open brace, check if it starts a
7850 ;; block that contains another declaration level, i.e. that isn't a
7851 ;; statement block or a brace list, and if so return non-nil.
7853 ;; If the check is successful, the return value is the start of the
7854 ;; keyword that tells what kind of construct it is, i.e. typically
7855 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7856 ;; the point will be at the start of the construct, before any
7857 ;; leading specifiers, otherwise it's at the returned position.
7859 ;; The point is clobbered if the check is unsuccessful.
7861 ;; CONTAINING-SEXP is the position of the open of the surrounding
7862 ;; paren, or nil if none.
7864 ;; The optional LIMIT limits the backward search for the start of
7865 ;; the construct. It's assumed to be at a syntactically relevant
7866 ;; position.
7868 ;; If any template arglists are found in the searched region before
7869 ;; the open brace, they get marked with paren syntax.
7871 ;; This function might do hidden buffer changes.
7873 (let ((open-brace (point)) kwd-start first-specifier-pos)
7874 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7876 (when (and c-recognize-<>-arglists
7877 (eq (char-before) ?>))
7878 ;; Could be at the end of a template arglist.
7879 (let ((c-parse-and-markup-<>-arglists t)
7880 (c-disallow-comma-in-<>-arglists
7881 (and containing-sexp
7882 (not (eq (char-after containing-sexp) ?{)))))
7883 (while (and
7884 (c-backward-<>-arglist nil limit)
7885 (progn
7886 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7887 (eq (char-before) ?>))))))
7889 ;; Note: Can't get bogus hits inside template arglists below since they
7890 ;; have gotten paren syntax above.
7891 (when (and
7892 ;; If `goto-start' is set we begin by searching for the
7893 ;; first possible position of a leading specifier list.
7894 ;; The `c-decl-block-key' search continues from there since
7895 ;; we know it can't match earlier.
7896 (if goto-start
7897 (when (c-syntactic-re-search-forward c-symbol-start
7898 open-brace t t)
7899 (goto-char (setq first-specifier-pos (match-beginning 0)))
7903 (cond
7904 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
7905 (goto-char (setq kwd-start (match-beginning 0)))
7908 ;; Found a keyword that can't be a type?
7909 (match-beginning 1)
7911 ;; Can be a type too, in which case it's the return type of a
7912 ;; function (under the assumption that no declaration level
7913 ;; block construct starts with a type).
7914 (not (c-forward-type))
7916 ;; Jumped over a type, but it could be a declaration keyword
7917 ;; followed by the declared identifier that we've jumped over
7918 ;; instead (e.g. in "class Foo {"). If it indeed is a type
7919 ;; then we should be at the declarator now, so check for a
7920 ;; valid declarator start.
7922 ;; Note: This doesn't cope with the case when a declared
7923 ;; identifier is followed by e.g. '(' in a language where '('
7924 ;; also might be part of a declarator expression. Currently
7925 ;; there's no such language.
7926 (not (or (looking-at c-symbol-start)
7927 (looking-at c-type-decl-prefix-key)))))
7929 ;; In Pike a list of modifiers may be followed by a brace
7930 ;; to make them apply to many identifiers. Note that the
7931 ;; match data will be empty on return in this case.
7932 ((and (c-major-mode-is 'pike-mode)
7933 (progn
7934 (goto-char open-brace)
7935 (= (c-backward-token-2) 0))
7936 (looking-at c-specifier-key)
7937 ;; Use this variant to avoid yet another special regexp.
7938 (c-keyword-member (c-keyword-sym (match-string 1))
7939 'c-modifier-kwds))
7940 (setq kwd-start (point))
7941 t)))
7943 ;; Got a match.
7945 (if goto-start
7946 ;; Back up over any preceding specifiers and their clauses
7947 ;; by going forward from `first-specifier-pos', which is the
7948 ;; earliest possible position where the specifier list can
7949 ;; start.
7950 (progn
7951 (goto-char first-specifier-pos)
7953 (while (< (point) kwd-start)
7954 (if (looking-at c-symbol-key)
7955 ;; Accept any plain symbol token on the ground that
7956 ;; it's a specifier masked through a macro (just
7957 ;; like `c-forward-decl-or-cast-1' skip forward over
7958 ;; such tokens).
7960 ;; Could be more restrictive wrt invalid keywords,
7961 ;; but that'd only occur in invalid code so there's
7962 ;; no use spending effort on it.
7963 (let ((end (match-end 0)))
7964 (unless (c-forward-keyword-clause 0)
7965 (goto-char end)
7966 (c-forward-syntactic-ws)))
7968 ;; Can't parse a declaration preamble and is still
7969 ;; before `kwd-start'. That means `first-specifier-pos'
7970 ;; was in some earlier construct. Search again.
7971 (if (c-syntactic-re-search-forward c-symbol-start
7972 kwd-start 'move t)
7973 (goto-char (setq first-specifier-pos (match-beginning 0)))
7974 ;; Got no preamble before the block declaration keyword.
7975 (setq first-specifier-pos kwd-start))))
7977 (goto-char first-specifier-pos))
7978 (goto-char kwd-start))
7980 kwd-start)))
7982 (defun c-search-uplist-for-classkey (paren-state)
7983 ;; Check if the closest containing paren sexp is a declaration
7984 ;; block, returning a 2 element vector in that case. Aref 0
7985 ;; contains the bufpos at boi of the class key line, and aref 1
7986 ;; contains the bufpos of the open brace. This function is an
7987 ;; obsolete wrapper for `c-looking-at-decl-block'.
7989 ;; This function might do hidden buffer changes.
7990 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
7991 (when open-paren-pos
7992 (save-excursion
7993 (goto-char open-paren-pos)
7994 (when (and (eq (char-after) ?{)
7995 (c-looking-at-decl-block
7996 (c-safe-position open-paren-pos paren-state)
7997 nil))
7998 (back-to-indentation)
7999 (vector (point) open-paren-pos))))))
8001 (defun c-inside-bracelist-p (containing-sexp paren-state)
8002 ;; return the buffer position of the beginning of the brace list
8003 ;; statement if we're inside a brace list, otherwise return nil.
8004 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
8005 ;; paren. PAREN-STATE is the remainder of the state of enclosing
8006 ;; braces
8008 ;; N.B.: This algorithm can potentially get confused by cpp macros
8009 ;; placed in inconvenient locations. It's a trade-off we make for
8010 ;; speed.
8012 ;; This function might do hidden buffer changes.
8014 ;; This will pick up brace list declarations.
8015 (c-safe
8016 (save-excursion
8017 (goto-char containing-sexp)
8018 (c-forward-sexp -1)
8019 (let (bracepos)
8020 (if (and (or (looking-at c-brace-list-key)
8021 (progn (c-forward-sexp -1)
8022 (looking-at c-brace-list-key)))
8023 (setq bracepos (c-down-list-forward (point)))
8024 (not (c-crosses-statement-barrier-p (point)
8025 (- bracepos 2))))
8026 (point)))))
8027 ;; this will pick up array/aggregate init lists, even if they are nested.
8028 (save-excursion
8029 (let ((class-key
8030 ;; Pike can have class definitions anywhere, so we must
8031 ;; check for the class key here.
8032 (and (c-major-mode-is 'pike-mode)
8033 c-decl-block-key))
8034 bufpos braceassignp lim next-containing)
8035 (while (and (not bufpos)
8036 containing-sexp)
8037 (when paren-state
8038 (if (consp (car paren-state))
8039 (setq lim (cdr (car paren-state))
8040 paren-state (cdr paren-state))
8041 (setq lim (car paren-state)))
8042 (when paren-state
8043 (setq next-containing (car paren-state)
8044 paren-state (cdr paren-state))))
8045 (goto-char containing-sexp)
8046 (if (c-looking-at-inexpr-block next-containing next-containing)
8047 ;; We're in an in-expression block of some kind. Do not
8048 ;; check nesting. We deliberately set the limit to the
8049 ;; containing sexp, so that c-looking-at-inexpr-block
8050 ;; doesn't check for an identifier before it.
8051 (setq containing-sexp nil)
8052 ;; see if the open brace is preceded by = or [...] in
8053 ;; this statement, but watch out for operator=
8054 (setq braceassignp 'dontknow)
8055 (c-backward-token-2 1 t lim)
8056 ;; Checks to do only on the first sexp before the brace.
8057 (when (and c-opt-inexpr-brace-list-key
8058 (eq (char-after) ?\[))
8059 ;; In Java, an initialization brace list may follow
8060 ;; directly after "new Foo[]", so check for a "new"
8061 ;; earlier.
8062 (while (eq braceassignp 'dontknow)
8063 (setq braceassignp
8064 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8065 ((looking-at c-opt-inexpr-brace-list-key) t)
8066 ((looking-at "\\sw\\|\\s_\\|[.[]")
8067 ;; Carry on looking if this is an
8068 ;; identifier (may contain "." in Java)
8069 ;; or another "[]" sexp.
8070 'dontknow)
8071 (t nil)))))
8072 ;; Checks to do on all sexps before the brace, up to the
8073 ;; beginning of the statement.
8074 (while (eq braceassignp 'dontknow)
8075 (cond ((eq (char-after) ?\;)
8076 (setq braceassignp nil))
8077 ((and class-key
8078 (looking-at class-key))
8079 (setq braceassignp nil))
8080 ((eq (char-after) ?=)
8081 ;; We've seen a =, but must check earlier tokens so
8082 ;; that it isn't something that should be ignored.
8083 (setq braceassignp 'maybe)
8084 (while (and (eq braceassignp 'maybe)
8085 (zerop (c-backward-token-2 1 t lim)))
8086 (setq braceassignp
8087 (cond
8088 ;; Check for operator =
8089 ((and c-opt-op-identifier-prefix
8090 (looking-at c-opt-op-identifier-prefix))
8091 nil)
8092 ;; Check for `<opchar>= in Pike.
8093 ((and (c-major-mode-is 'pike-mode)
8094 (or (eq (char-after) ?`)
8095 ;; Special case for Pikes
8096 ;; `[]=, since '[' is not in
8097 ;; the punctuation class.
8098 (and (eq (char-after) ?\[)
8099 (eq (char-before) ?`))))
8100 nil)
8101 ((looking-at "\\s.") 'maybe)
8102 ;; make sure we're not in a C++ template
8103 ;; argument assignment
8104 ((and
8105 (c-major-mode-is 'c++-mode)
8106 (save-excursion
8107 (let ((here (point))
8108 (pos< (progn
8109 (skip-chars-backward "^<>")
8110 (point))))
8111 (and (eq (char-before) ?<)
8112 (not (c-crosses-statement-barrier-p
8113 pos< here))
8114 (not (c-in-literal))
8115 ))))
8116 nil)
8117 (t t))))))
8118 (if (and (eq braceassignp 'dontknow)
8119 (/= (c-backward-token-2 1 t lim) 0))
8120 (setq braceassignp nil)))
8121 (if (not braceassignp)
8122 (if (eq (char-after) ?\;)
8123 ;; Brace lists can't contain a semicolon, so we're done.
8124 (setq containing-sexp nil)
8125 ;; Go up one level.
8126 (setq containing-sexp next-containing
8127 lim nil
8128 next-containing nil))
8129 ;; we've hit the beginning of the aggregate list
8130 (c-beginning-of-statement-1
8131 (c-most-enclosing-brace paren-state))
8132 (setq bufpos (point))))
8134 bufpos))
8137 (defun c-looking-at-special-brace-list (&optional lim)
8138 ;; If we're looking at the start of a pike-style list, ie `({ })',
8139 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
8140 ;; positions and its entry in c-special-brace-lists is returned, nil
8141 ;; otherwise. The ending position is nil if the list is still open.
8142 ;; LIM is the limit for forward search. The point may either be at
8143 ;; the `(' or at the following paren character. Tries to check the
8144 ;; matching closer, but assumes it's correct if no balanced paren is
8145 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8146 ;; a special brace list).
8148 ;; This function might do hidden buffer changes.
8149 (if c-special-brace-lists
8150 (condition-case ()
8151 (save-excursion
8152 (let ((beg (point))
8153 inner-beg end type)
8154 (c-forward-syntactic-ws)
8155 (if (eq (char-after) ?\()
8156 (progn
8157 (forward-char 1)
8158 (c-forward-syntactic-ws)
8159 (setq inner-beg (point))
8160 (setq type (assq (char-after) c-special-brace-lists)))
8161 (if (setq type (assq (char-after) c-special-brace-lists))
8162 (progn
8163 (setq inner-beg (point))
8164 (c-backward-syntactic-ws)
8165 (forward-char -1)
8166 (setq beg (if (eq (char-after) ?\()
8167 (point)
8168 nil)))))
8169 (if (and beg type)
8170 (if (and (c-safe
8171 (goto-char beg)
8172 (c-forward-sexp 1)
8173 (setq end (point))
8174 (= (char-before) ?\)))
8175 (c-safe
8176 (goto-char inner-beg)
8177 (if (looking-at "\\s(")
8178 ;; Check balancing of the inner paren
8179 ;; below.
8180 (progn
8181 (c-forward-sexp 1)
8183 ;; If the inner char isn't a paren then
8184 ;; we can't check balancing, so just
8185 ;; check the char before the outer
8186 ;; closing paren.
8187 (goto-char end)
8188 (backward-char)
8189 (c-backward-syntactic-ws)
8190 (= (char-before) (cdr type)))))
8191 (if (or (/= (char-syntax (char-before)) ?\))
8192 (= (progn
8193 (c-forward-syntactic-ws)
8194 (point))
8195 (1- end)))
8196 (cons (cons beg end) type))
8197 (cons (list beg) type)))))
8198 (error nil))))
8200 (defun c-looking-at-bos (&optional lim)
8201 ;; Return non-nil if between two statements or declarations, assuming
8202 ;; point is not inside a literal or comment.
8204 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8205 ;; are recommended instead.
8207 ;; This function might do hidden buffer changes.
8208 (c-at-statement-start-p))
8209 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8211 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8212 ;; Return non-nil if we're looking at the beginning of a block
8213 ;; inside an expression. The value returned is actually a cons of
8214 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8215 ;; position of the beginning of the construct.
8217 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8218 ;; position of the closest containing list. If it's nil, the
8219 ;; containing paren isn't used to decide whether we're inside an
8220 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8221 ;; needs to be farther back.
8223 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8224 ;; brace block might be done. It should only be used when the
8225 ;; construct can be assumed to be complete, i.e. when the original
8226 ;; starting position was further down than that.
8228 ;; This function might do hidden buffer changes.
8230 (save-excursion
8231 (let ((res 'maybe) passed-paren
8232 (closest-lim (or containing-sexp lim (point-min)))
8233 ;; Look at the character after point only as a last resort
8234 ;; when we can't disambiguate.
8235 (block-follows (and (eq (char-after) ?{) (point))))
8237 (while (and (eq res 'maybe)
8238 (progn (c-backward-syntactic-ws)
8239 (> (point) closest-lim))
8240 (not (bobp))
8241 (progn (backward-char)
8242 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8243 (c-safe (forward-char)
8244 (goto-char (scan-sexps (point) -1))))
8246 (setq res
8247 (if (looking-at c-keywords-regexp)
8248 (let ((kw-sym (c-keyword-sym (match-string 1))))
8249 (cond
8250 ((and block-follows
8251 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8252 (and (not (eq passed-paren ?\[))
8253 (or (not (looking-at c-class-key))
8254 ;; If the class definition is at the start of
8255 ;; a statement, we don't consider it an
8256 ;; in-expression class.
8257 (let ((prev (point)))
8258 (while (and
8259 (= (c-backward-token-2 1 nil closest-lim) 0)
8260 (eq (char-syntax (char-after)) ?w))
8261 (setq prev (point)))
8262 (goto-char prev)
8263 (not (c-at-statement-start-p)))
8264 ;; Also, in Pike we treat it as an
8265 ;; in-expression class if it's used in an
8266 ;; object clone expression.
8267 (save-excursion
8268 (and check-at-end
8269 (c-major-mode-is 'pike-mode)
8270 (progn (goto-char block-follows)
8271 (zerop (c-forward-token-2 1 t)))
8272 (eq (char-after) ?\())))
8273 (cons 'inexpr-class (point))))
8274 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8275 (when (not passed-paren)
8276 (cons 'inexpr-statement (point))))
8277 ((c-keyword-member kw-sym 'c-lambda-kwds)
8278 (when (or (not passed-paren)
8279 (eq passed-paren ?\())
8280 (cons 'inlambda (point))))
8281 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8282 nil)
8284 'maybe)))
8286 (if (looking-at "\\s(")
8287 (if passed-paren
8288 (if (and (eq passed-paren ?\[)
8289 (eq (char-after) ?\[))
8290 ;; Accept several square bracket sexps for
8291 ;; Java array initializations.
8292 'maybe)
8293 (setq passed-paren (char-after))
8294 'maybe)
8295 'maybe))))
8297 (if (eq res 'maybe)
8298 (when (and c-recognize-paren-inexpr-blocks
8299 block-follows
8300 containing-sexp
8301 (eq (char-after containing-sexp) ?\())
8302 (goto-char containing-sexp)
8303 (if (or (save-excursion
8304 (c-backward-syntactic-ws lim)
8305 (and (> (point) (or lim (point-min)))
8306 (c-on-identifier)))
8307 (and c-special-brace-lists
8308 (c-looking-at-special-brace-list)))
8310 (cons 'inexpr-statement (point))))
8312 res))))
8314 (defun c-looking-at-inexpr-block-backward (paren-state)
8315 ;; Returns non-nil if we're looking at the end of an in-expression
8316 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8317 ;; PAREN-STATE is the paren state relevant at the current position.
8319 ;; This function might do hidden buffer changes.
8320 (save-excursion
8321 ;; We currently only recognize a block.
8322 (let ((here (point))
8323 (elem (car-safe paren-state))
8324 containing-sexp)
8325 (when (and (consp elem)
8326 (progn (goto-char (cdr elem))
8327 (c-forward-syntactic-ws here)
8328 (= (point) here)))
8329 (goto-char (car elem))
8330 (if (setq paren-state (cdr paren-state))
8331 (setq containing-sexp (car-safe paren-state)))
8332 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8333 paren-state)
8334 containing-sexp)))))
8337 ;; `c-guess-basic-syntax' and the functions that precedes it below
8338 ;; implements the main decision tree for determining the syntactic
8339 ;; analysis of the current line of code.
8341 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8342 ;; auto newline analysis.
8343 (defvar c-auto-newline-analysis nil)
8345 (defun c-brace-anchor-point (bracepos)
8346 ;; BRACEPOS is the position of a brace in a construct like "namespace
8347 ;; Bar {". Return the anchor point in this construct; this is the
8348 ;; earliest symbol on the brace's line which isn't earlier than
8349 ;; "namespace".
8351 ;; Currently (2007-08-17), "like namespace" means "matches
8352 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8353 ;; or anything like that.
8354 (save-excursion
8355 (let ((boi (c-point 'boi bracepos)))
8356 (goto-char bracepos)
8357 (while (and (> (point) boi)
8358 (not (looking-at c-other-decl-block-key)))
8359 (c-backward-token-2))
8360 (if (> (point) boi) (point) boi))))
8362 (defsubst c-add-syntax (symbol &rest args)
8363 ;; A simple function to prepend a new syntax element to
8364 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8365 ;; should always be dynamically bound but since we read it first
8366 ;; we'll fail properly anyway if this function is misused.
8367 (setq c-syntactic-context (cons (cons symbol args)
8368 c-syntactic-context)))
8370 (defsubst c-append-syntax (symbol &rest args)
8371 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8372 ;; (Normally not necessary.)
8373 (setq c-syntactic-context (nconc c-syntactic-context
8374 (list (cons symbol args)))))
8376 (defun c-add-stmt-syntax (syntax-symbol
8377 syntax-extra-args
8378 stop-at-boi-only
8379 containing-sexp
8380 paren-state)
8381 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8382 ;; needed with further syntax elements of the types `substatement',
8383 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8384 ;; `defun-block-intro'.
8386 ;; Do the generic processing to anchor the given syntax symbol on
8387 ;; the preceding statement: Skip over any labels and containing
8388 ;; statements on the same line, and then search backward until we
8389 ;; find a statement or block start that begins at boi without a
8390 ;; label or comment.
8392 ;; Point is assumed to be at the prospective anchor point for the
8393 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8394 ;; skip past open parens and containing statements. Most of the added
8395 ;; syntax elements will get the same anchor point - the exception is
8396 ;; for an anchor in a construct like "namespace"[*] - this is as early
8397 ;; as possible in the construct but on the same line as the {.
8399 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8401 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8402 ;; syntax symbol. They are appended after the anchor point.
8404 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8405 ;; if the current statement starts there.
8407 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8408 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8410 ;; This function might do hidden buffer changes.
8412 (if (= (point) (c-point 'boi))
8413 ;; This is by far the most common case, so let's give it special
8414 ;; treatment.
8415 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
8417 (let ((syntax-last c-syntactic-context)
8418 (boi (c-point 'boi))
8419 ;; Set when we're on a label, so that we don't stop there.
8420 ;; FIXME: To be complete we should check if we're on a label
8421 ;; now at the start.
8422 on-label)
8424 ;; Use point as the anchor point for "namespace", "extern", etc.
8425 (apply 'c-add-syntax syntax-symbol
8426 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
8427 (point) nil)
8428 syntax-extra-args)
8430 ;; Loop while we have to back out of containing blocks.
8431 (while
8432 (and
8433 (catch 'back-up-block
8435 ;; Loop while we have to back up statements.
8436 (while (or (/= (point) boi)
8437 on-label
8438 (looking-at c-comment-start-regexp))
8440 ;; Skip past any comments that stands between the
8441 ;; statement start and boi.
8442 (let ((savepos (point)))
8443 (while (and (/= savepos boi)
8444 (c-backward-single-comment))
8445 (setq savepos (point)
8446 boi (c-point 'boi)))
8447 (goto-char savepos))
8449 ;; Skip to the beginning of this statement or backward
8450 ;; another one.
8451 (let ((old-pos (point))
8452 (old-boi boi)
8453 (step-type (c-beginning-of-statement-1 containing-sexp)))
8454 (setq boi (c-point 'boi)
8455 on-label (eq step-type 'label))
8457 (cond ((= (point) old-pos)
8458 ;; If we didn't move we're at the start of a block and
8459 ;; have to continue outside it.
8460 (throw 'back-up-block t))
8462 ((and (eq step-type 'up)
8463 (>= (point) old-boi)
8464 (looking-at "else\\>[^_]")
8465 (save-excursion
8466 (goto-char old-pos)
8467 (looking-at "if\\>[^_]")))
8468 ;; Special case to avoid deeper and deeper indentation
8469 ;; of "else if" clauses.
8472 ((and (not stop-at-boi-only)
8473 (/= old-pos old-boi)
8474 (memq step-type '(up previous)))
8475 ;; If stop-at-boi-only is nil, we shouldn't back up
8476 ;; over previous or containing statements to try to
8477 ;; reach boi, so go back to the last position and
8478 ;; exit.
8479 (goto-char old-pos)
8480 (throw 'back-up-block nil))
8483 (if (and (not stop-at-boi-only)
8484 (memq step-type '(up previous beginning)))
8485 ;; If we've moved into another statement then we
8486 ;; should no longer try to stop in the middle of a
8487 ;; line.
8488 (setq stop-at-boi-only t))
8490 ;; Record this as a substatement if we skipped up one
8491 ;; level.
8492 (when (eq step-type 'up)
8493 (c-add-syntax 'substatement nil))))
8496 containing-sexp)
8498 ;; Now we have to go out of this block.
8499 (goto-char containing-sexp)
8501 ;; Don't stop in the middle of a special brace list opener
8502 ;; like "({".
8503 (when c-special-brace-lists
8504 (let ((special-list (c-looking-at-special-brace-list)))
8505 (when (and special-list
8506 (< (car (car special-list)) (point)))
8507 (setq containing-sexp (car (car special-list)))
8508 (goto-char containing-sexp))))
8510 (setq paren-state (c-whack-state-after containing-sexp paren-state)
8511 containing-sexp (c-most-enclosing-brace paren-state)
8512 boi (c-point 'boi))
8514 ;; Analyze the construct in front of the block we've stepped out
8515 ;; from and add the right syntactic element for it.
8516 (let ((paren-pos (point))
8517 (paren-char (char-after))
8518 step-type)
8520 (if (eq paren-char ?\()
8521 ;; Stepped out of a parenthesis block, so we're in an
8522 ;; expression now.
8523 (progn
8524 (when (/= paren-pos boi)
8525 (if (and c-recognize-paren-inexpr-blocks
8526 (progn
8527 (c-backward-syntactic-ws containing-sexp)
8528 (or (not (looking-at "\\>"))
8529 (not (c-on-identifier))))
8530 (save-excursion
8531 (goto-char (1+ paren-pos))
8532 (c-forward-syntactic-ws)
8533 (eq (char-after) ?{)))
8534 ;; Stepped out of an in-expression statement. This
8535 ;; syntactic element won't get an anchor pos.
8536 (c-add-syntax 'inexpr-statement)
8538 ;; A parenthesis normally belongs to an arglist.
8539 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
8541 (goto-char (max boi
8542 (if containing-sexp
8543 (1+ containing-sexp)
8544 (point-min))))
8545 (setq step-type 'same
8546 on-label nil))
8548 ;; Stepped out of a brace block.
8549 (setq step-type (c-beginning-of-statement-1 containing-sexp)
8550 on-label (eq step-type 'label))
8552 (if (and (eq step-type 'same)
8553 (/= paren-pos (point)))
8554 (let (inexpr)
8555 (cond
8556 ((save-excursion
8557 (goto-char paren-pos)
8558 (setq inexpr (c-looking-at-inexpr-block
8559 (c-safe-position containing-sexp paren-state)
8560 containing-sexp)))
8561 (c-add-syntax (if (eq (car inexpr) 'inlambda)
8562 'defun-block-intro
8563 'statement-block-intro)
8564 nil))
8565 ((looking-at c-other-decl-block-key)
8566 (c-add-syntax
8567 (cdr (assoc (match-string 1)
8568 c-other-decl-block-key-in-symbols-alist))
8569 (max (c-point 'boi paren-pos) (point))))
8570 (t (c-add-syntax 'defun-block-intro nil))))
8572 (c-add-syntax 'statement-block-intro nil)))
8574 (if (= paren-pos boi)
8575 ;; Always done if the open brace was at boi. The
8576 ;; c-beginning-of-statement-1 call above is necessary
8577 ;; anyway, to decide the type of block-intro to add.
8578 (goto-char paren-pos)
8579 (setq boi (c-point 'boi)))
8582 ;; Fill in the current point as the anchor for all the symbols
8583 ;; added above.
8584 (let ((p c-syntactic-context) q)
8585 (while (not (eq p syntax-last))
8586 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8587 (while q
8588 (unless (car q)
8589 (setcar q (point)))
8590 (setq q (cdr q)))
8591 (setq p (cdr p))))
8594 (defun c-add-class-syntax (symbol
8595 containing-decl-open
8596 containing-decl-start
8597 containing-decl-kwd
8598 paren-state)
8599 ;; The inclass and class-close syntactic symbols are added in
8600 ;; several places and some work is needed to fix everything.
8601 ;; Therefore it's collected here.
8603 ;; This function might do hidden buffer changes.
8604 (goto-char containing-decl-open)
8605 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
8606 (progn
8607 (c-add-syntax symbol containing-decl-open)
8608 containing-decl-open)
8609 (goto-char containing-decl-start)
8610 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8611 ;; here, but we have to do like this for compatibility.
8612 (back-to-indentation)
8613 (c-add-syntax symbol (point))
8614 (if (and (c-keyword-member containing-decl-kwd
8615 'c-inexpr-class-kwds)
8616 (/= containing-decl-start (c-point 'boi containing-decl-start)))
8617 (c-add-syntax 'inexpr-class))
8618 (point)))
8620 (defun c-guess-continued-construct (indent-point
8621 char-after-ip
8622 beg-of-same-or-containing-stmt
8623 containing-sexp
8624 paren-state)
8625 ;; This function contains the decision tree reached through both
8626 ;; cases 18 and 10. It's a continued statement or top level
8627 ;; construct of some kind.
8629 ;; This function might do hidden buffer changes.
8631 (let (special-brace-list placeholder)
8632 (goto-char indent-point)
8633 (skip-chars-forward " \t")
8635 (cond
8636 ;; (CASE A removed.)
8637 ;; CASE B: open braces for class or brace-lists
8638 ((setq special-brace-list
8639 (or (and c-special-brace-lists
8640 (c-looking-at-special-brace-list))
8641 (eq char-after-ip ?{)))
8643 (cond
8644 ;; CASE B.1: class-open
8645 ((save-excursion
8646 (and (eq (char-after) ?{)
8647 (c-looking-at-decl-block containing-sexp t)
8648 (setq beg-of-same-or-containing-stmt (point))))
8649 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
8651 ;; CASE B.2: brace-list-open
8652 ((or (consp special-brace-list)
8653 (save-excursion
8654 (goto-char beg-of-same-or-containing-stmt)
8655 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8656 indent-point t t t)))
8657 ;; The most semantically accurate symbol here is
8658 ;; brace-list-open, but we normally report it simply as a
8659 ;; statement-cont. The reason is that one normally adjusts
8660 ;; brace-list-open for brace lists as top-level constructs,
8661 ;; and brace lists inside statements is a completely different
8662 ;; context. C.f. case 5A.3.
8663 (c-beginning-of-statement-1 containing-sexp)
8664 (c-add-stmt-syntax (if c-auto-newline-analysis
8665 ;; Turn off the dwim above when we're
8666 ;; analyzing the nature of the brace
8667 ;; for the auto newline feature.
8668 'brace-list-open
8669 'statement-cont)
8670 nil nil
8671 containing-sexp paren-state))
8673 ;; CASE B.3: The body of a function declared inside a normal
8674 ;; block. Can occur e.g. in Pike and when using gcc
8675 ;; extensions, but watch out for macros followed by blocks.
8676 ;; C.f. cases E, 16F and 17G.
8677 ((and (not (c-at-statement-start-p))
8678 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8679 'same)
8680 (save-excursion
8681 (let ((c-recognize-typeless-decls nil))
8682 ;; Turn off recognition of constructs that lacks a
8683 ;; type in this case, since that's more likely to be
8684 ;; a macro followed by a block.
8685 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8686 (c-add-stmt-syntax 'defun-open nil t
8687 containing-sexp paren-state))
8689 ;; CASE B.4: Continued statement with block open. The most
8690 ;; accurate analysis is perhaps `statement-cont' together with
8691 ;; `block-open' but we play DWIM and use `substatement-open'
8692 ;; instead. The rationaly is that this typically is a macro
8693 ;; followed by a block which makes it very similar to a
8694 ;; statement with a substatement block.
8696 (c-add-stmt-syntax 'substatement-open nil nil
8697 containing-sexp paren-state))
8700 ;; CASE C: iostream insertion or extraction operator
8701 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8702 (save-excursion
8703 (goto-char beg-of-same-or-containing-stmt)
8704 ;; If there is no preceding streamop in the statement
8705 ;; then indent this line as a normal statement-cont.
8706 (when (c-syntactic-re-search-forward
8707 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
8708 (c-add-syntax 'stream-op (c-point 'boi))
8709 t))))
8711 ;; CASE E: In the "K&R region" of a function declared inside a
8712 ;; normal block. C.f. case B.3.
8713 ((and (save-excursion
8714 ;; Check that the next token is a '{'. This works as
8715 ;; long as no language that allows nested function
8716 ;; definitions allows stuff like member init lists, K&R
8717 ;; declarations or throws clauses there.
8719 ;; Note that we do a forward search for something ahead
8720 ;; of the indentation line here. That's not good since
8721 ;; the user might not have typed it yet. Unfortunately
8722 ;; it's exceedingly tricky to recognize a function
8723 ;; prototype in a code block without resorting to this.
8724 (c-forward-syntactic-ws)
8725 (eq (char-after) ?{))
8726 (not (c-at-statement-start-p))
8727 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8728 'same)
8729 (save-excursion
8730 (let ((c-recognize-typeless-decls nil))
8731 ;; Turn off recognition of constructs that lacks a
8732 ;; type in this case, since that's more likely to be
8733 ;; a macro followed by a block.
8734 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8735 (c-add-stmt-syntax 'func-decl-cont nil t
8736 containing-sexp paren-state))
8738 ;;CASE F: continued statement and the only preceding items are
8739 ;;annotations.
8740 ((and (c-major-mode-is 'java-mode)
8741 (setq placeholder (point))
8742 (c-beginning-of-statement-1)
8743 (progn
8744 (while (and (c-forward-annotation)
8745 (< (point) placeholder))
8746 (c-forward-syntactic-ws))
8748 (prog1
8749 (>= (point) placeholder)
8750 (goto-char placeholder)))
8751 (c-beginning-of-statement-1 containing-sexp)
8752 (c-add-syntax 'annotation-var-cont (point)))
8754 ;; CASE G: a template list continuation?
8755 ;; Mostly a duplication of case 5D.3 to fix templates-19:
8756 ((and (c-major-mode-is 'c++-mode)
8757 (save-excursion
8758 (goto-char indent-point)
8759 (c-with-syntax-table c++-template-syntax-table
8760 (setq placeholder (c-up-list-backward)))
8761 (and placeholder
8762 (eq (char-after placeholder) ?<)
8763 (/= (char-before placeholder) ?<)
8764 (progn
8765 (goto-char (1+ placeholder))
8766 (not (looking-at c-<-op-cont-regexp))))))
8767 (c-with-syntax-table c++-template-syntax-table
8768 (goto-char placeholder)
8769 (c-beginning-of-statement-1 containing-sexp t)
8770 (if (save-excursion
8771 (c-backward-syntactic-ws containing-sexp)
8772 (eq (char-before) ?<))
8773 ;; In a nested template arglist.
8774 (progn
8775 (goto-char placeholder)
8776 (c-syntactic-skip-backward "^,;" containing-sexp t)
8777 (c-forward-syntactic-ws))
8778 (back-to-indentation)))
8779 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8780 ;; template aware.
8781 (c-add-syntax 'template-args-cont (point) placeholder))
8783 ;; CASE D: continued statement.
8785 (c-beginning-of-statement-1 containing-sexp)
8786 (c-add-stmt-syntax 'statement-cont nil nil
8787 containing-sexp paren-state))
8790 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
8791 ;; 2005/11/29).
8792 ;;;###autoload
8793 (defun c-guess-basic-syntax ()
8794 "Return the syntactic context of the current line."
8795 (save-excursion
8796 (beginning-of-line)
8797 (c-save-buffer-state
8798 ((indent-point (point))
8799 (case-fold-search nil)
8800 ;; A whole ugly bunch of various temporary variables. Have
8801 ;; to declare them here since it's not possible to declare
8802 ;; a variable with only the scope of a cond test and the
8803 ;; following result clauses, and most of this function is a
8804 ;; single gigantic cond. :P
8805 literal char-before-ip before-ws-ip char-after-ip macro-start
8806 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
8807 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
8808 containing-<
8809 ;; The following record some positions for the containing
8810 ;; declaration block if we're directly within one:
8811 ;; `containing-decl-open' is the position of the open
8812 ;; brace. `containing-decl-start' is the start of the
8813 ;; declaration. `containing-decl-kwd' is the keyword
8814 ;; symbol of the keyword that tells what kind of block it
8815 ;; is.
8816 containing-decl-open
8817 containing-decl-start
8818 containing-decl-kwd
8819 ;; The open paren of the closest surrounding sexp or nil if
8820 ;; there is none.
8821 containing-sexp
8822 ;; The position after the closest preceding brace sexp
8823 ;; (nested sexps are ignored), or the position after
8824 ;; `containing-sexp' if there is none, or (point-min) if
8825 ;; `containing-sexp' is nil.
8827 ;; The paren state outside `containing-sexp', or at
8828 ;; `indent-point' if `containing-sexp' is nil.
8829 (paren-state (c-parse-state))
8830 ;; There's always at most one syntactic element which got
8831 ;; an anchor pos. It's stored in syntactic-relpos.
8832 syntactic-relpos
8833 (c-stmt-delim-chars c-stmt-delim-chars))
8835 ;; Check if we're directly inside an enclosing declaration
8836 ;; level block.
8837 (when (and (setq containing-sexp
8838 (c-most-enclosing-brace paren-state))
8839 (progn
8840 (goto-char containing-sexp)
8841 (eq (char-after) ?{))
8842 (setq placeholder
8843 (c-looking-at-decl-block
8844 (c-most-enclosing-brace paren-state
8845 containing-sexp)
8846 t)))
8847 (setq containing-decl-open containing-sexp
8848 containing-decl-start (point)
8849 containing-sexp nil)
8850 (goto-char placeholder)
8851 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
8852 (c-keyword-sym (match-string 1)))))
8854 ;; Init some position variables.
8855 (if c-state-cache
8856 (progn
8857 (setq containing-sexp (car paren-state)
8858 paren-state (cdr paren-state))
8859 (if (consp containing-sexp)
8860 (progn
8861 (setq lim (cdr containing-sexp))
8862 (if (cdr c-state-cache)
8863 ;; Ignore balanced paren. The next entry
8864 ;; can't be another one.
8865 (setq containing-sexp (car (cdr c-state-cache))
8866 paren-state (cdr paren-state))
8867 ;; If there is no surrounding open paren then
8868 ;; put the last balanced pair back on paren-state.
8869 (setq paren-state (cons containing-sexp paren-state)
8870 containing-sexp nil)))
8871 (setq lim (1+ containing-sexp))))
8872 (setq lim (point-min)))
8874 ;; If we're in a parenthesis list then ',' delimits the
8875 ;; "statements" rather than being an operator (with the
8876 ;; exception of the "for" clause). This difference is
8877 ;; typically only noticeable when statements are used in macro
8878 ;; arglists.
8879 (when (and containing-sexp
8880 (eq (char-after containing-sexp) ?\())
8881 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
8882 ;; cache char before and after indent point, and move point to
8883 ;; the most likely position to perform the majority of tests
8884 (goto-char indent-point)
8885 (c-backward-syntactic-ws lim)
8886 (setq before-ws-ip (point)
8887 char-before-ip (char-before))
8888 (goto-char indent-point)
8889 (skip-chars-forward " \t")
8890 (setq char-after-ip (char-after))
8892 ;; are we in a literal?
8893 (setq literal (c-in-literal lim))
8895 ;; now figure out syntactic qualities of the current line
8896 (cond
8898 ;; CASE 1: in a string.
8899 ((eq literal 'string)
8900 (c-add-syntax 'string (c-point 'bopl)))
8902 ;; CASE 2: in a C or C++ style comment.
8903 ((and (memq literal '(c c++))
8904 ;; This is a kludge for XEmacs where we use
8905 ;; `buffer-syntactic-context', which doesn't correctly
8906 ;; recognize "\*/" to end a block comment.
8907 ;; `parse-partial-sexp' which is used by
8908 ;; `c-literal-limits' will however do that in most
8909 ;; versions, which results in that we get nil from
8910 ;; `c-literal-limits' even when `c-in-literal' claims
8911 ;; we're inside a comment.
8912 (setq placeholder (c-literal-limits lim)))
8913 (c-add-syntax literal (car placeholder)))
8915 ;; CASE 3: in a cpp preprocessor macro continuation.
8916 ((and (save-excursion
8917 (when (c-beginning-of-macro)
8918 (setq macro-start (point))))
8919 (/= macro-start (c-point 'boi))
8920 (progn
8921 (setq tmpsymbol 'cpp-macro-cont)
8922 (or (not c-syntactic-indentation-in-macros)
8923 (save-excursion
8924 (goto-char macro-start)
8925 ;; If at the beginning of the body of a #define
8926 ;; directive then analyze as cpp-define-intro
8927 ;; only. Go on with the syntactic analysis
8928 ;; otherwise. in-macro-expr is set if we're in a
8929 ;; cpp expression, i.e. before the #define body
8930 ;; or anywhere in a non-#define directive.
8931 (if (c-forward-to-cpp-define-body)
8932 (let ((indent-boi (c-point 'boi indent-point)))
8933 (setq in-macro-expr (> (point) indent-boi)
8934 tmpsymbol 'cpp-define-intro)
8935 (= (point) indent-boi))
8936 (setq in-macro-expr t)
8937 nil)))))
8938 (c-add-syntax tmpsymbol macro-start)
8939 (setq macro-start nil))
8941 ;; CASE 11: an else clause?
8942 ((looking-at "else\\>[^_]")
8943 (c-beginning-of-statement-1 containing-sexp)
8944 (c-add-stmt-syntax 'else-clause nil t
8945 containing-sexp paren-state))
8947 ;; CASE 12: while closure of a do/while construct?
8948 ((and (looking-at "while\\>[^_]")
8949 (save-excursion
8950 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
8951 'beginning)
8952 (setq placeholder (point)))))
8953 (goto-char placeholder)
8954 (c-add-stmt-syntax 'do-while-closure nil t
8955 containing-sexp paren-state))
8957 ;; CASE 13: A catch or finally clause? This case is simpler
8958 ;; than if-else and do-while, because a block is required
8959 ;; after every try, catch and finally.
8960 ((save-excursion
8961 (and (cond ((c-major-mode-is 'c++-mode)
8962 (looking-at "catch\\>[^_]"))
8963 ((c-major-mode-is 'java-mode)
8964 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
8965 (and (c-safe (c-backward-syntactic-ws)
8966 (c-backward-sexp)
8968 (eq (char-after) ?{)
8969 (c-safe (c-backward-syntactic-ws)
8970 (c-backward-sexp)
8972 (if (eq (char-after) ?\()
8973 (c-safe (c-backward-sexp) t)
8975 (looking-at "\\(try\\|catch\\)\\>[^_]")
8976 (setq placeholder (point))))
8977 (goto-char placeholder)
8978 (c-add-stmt-syntax 'catch-clause nil t
8979 containing-sexp paren-state))
8981 ;; CASE 18: A substatement we can recognize by keyword.
8982 ((save-excursion
8983 (and c-opt-block-stmt-key
8984 (not (eq char-before-ip ?\;))
8985 (not (c-at-vsemi-p before-ws-ip))
8986 (not (memq char-after-ip '(?\) ?\] ?,)))
8987 (or (not (eq char-before-ip ?}))
8988 (c-looking-at-inexpr-block-backward c-state-cache))
8989 (> (point)
8990 (progn
8991 ;; Ought to cache the result from the
8992 ;; c-beginning-of-statement-1 calls here.
8993 (setq placeholder (point))
8994 (while (eq (setq step-type
8995 (c-beginning-of-statement-1 lim))
8996 'label))
8997 (if (eq step-type 'previous)
8998 (goto-char placeholder)
8999 (setq placeholder (point))
9000 (if (and (eq step-type 'same)
9001 (not (looking-at c-opt-block-stmt-key)))
9002 ;; Step up to the containing statement if we
9003 ;; stayed in the same one.
9004 (let (step)
9005 (while (eq
9006 (setq step
9007 (c-beginning-of-statement-1 lim))
9008 'label))
9009 (if (eq step 'up)
9010 (setq placeholder (point))
9011 ;; There was no containing statement afterall.
9012 (goto-char placeholder)))))
9013 placeholder))
9014 (if (looking-at c-block-stmt-2-key)
9015 ;; Require a parenthesis after these keywords.
9016 ;; Necessary to catch e.g. synchronized in Java,
9017 ;; which can be used both as statement and
9018 ;; modifier.
9019 (and (zerop (c-forward-token-2 1 nil))
9020 (eq (char-after) ?\())
9021 (looking-at c-opt-block-stmt-key))))
9023 (if (eq step-type 'up)
9024 ;; CASE 18A: Simple substatement.
9025 (progn
9026 (goto-char placeholder)
9027 (cond
9028 ((eq char-after-ip ?{)
9029 (c-add-stmt-syntax 'substatement-open nil nil
9030 containing-sexp paren-state))
9031 ((save-excursion
9032 (goto-char indent-point)
9033 (back-to-indentation)
9034 (c-forward-label))
9035 (c-add-stmt-syntax 'substatement-label nil nil
9036 containing-sexp paren-state))
9038 (c-add-stmt-syntax 'substatement nil nil
9039 containing-sexp paren-state))))
9041 ;; CASE 18B: Some other substatement. This is shared
9042 ;; with case 10.
9043 (c-guess-continued-construct indent-point
9044 char-after-ip
9045 placeholder
9047 paren-state)))
9049 ;; CASE 14: A case or default label
9050 ((looking-at c-label-kwds-regexp)
9051 (if containing-sexp
9052 (progn
9053 (goto-char containing-sexp)
9054 (setq lim (c-most-enclosing-brace c-state-cache
9055 containing-sexp))
9056 (c-backward-to-block-anchor lim)
9057 (c-add-stmt-syntax 'case-label nil t lim paren-state))
9058 ;; Got a bogus label at the top level. In lack of better
9059 ;; alternatives, anchor it on (point-min).
9060 (c-add-syntax 'case-label (point-min))))
9062 ;; CASE 15: any other label
9063 ((save-excursion
9064 (back-to-indentation)
9065 (and (not (looking-at c-syntactic-ws-start))
9066 (c-forward-label)))
9067 (cond (containing-decl-open
9068 (setq placeholder (c-add-class-syntax 'inclass
9069 containing-decl-open
9070 containing-decl-start
9071 containing-decl-kwd
9072 paren-state))
9073 ;; Append access-label with the same anchor point as
9074 ;; inclass gets.
9075 (c-append-syntax 'access-label placeholder))
9077 (containing-sexp
9078 (goto-char containing-sexp)
9079 (setq lim (c-most-enclosing-brace c-state-cache
9080 containing-sexp))
9081 (save-excursion
9082 (setq tmpsymbol
9083 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
9084 (looking-at "switch\\>[^_]"))
9085 ;; If the surrounding statement is a switch then
9086 ;; let's analyze all labels as switch labels, so
9087 ;; that they get lined up consistently.
9088 'case-label
9089 'label)))
9090 (c-backward-to-block-anchor lim)
9091 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
9094 ;; A label on the top level. Treat it as a class
9095 ;; context. (point-min) is the closest we get to the
9096 ;; class open brace.
9097 (c-add-syntax 'access-label (point-min)))))
9099 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9100 ;; 17E.
9101 ((setq placeholder (c-looking-at-inexpr-block
9102 (c-safe-position containing-sexp paren-state)
9103 containing-sexp
9104 ;; Have to turn on the heuristics after
9105 ;; the point even though it doesn't work
9106 ;; very well. C.f. test case class-16.pike.
9108 (setq tmpsymbol (assq (car placeholder)
9109 '((inexpr-class . class-open)
9110 (inexpr-statement . block-open))))
9111 (if tmpsymbol
9112 ;; It's a statement block or an anonymous class.
9113 (setq tmpsymbol (cdr tmpsymbol))
9114 ;; It's a Pike lambda. Check whether we are between the
9115 ;; lambda keyword and the argument list or at the defun
9116 ;; opener.
9117 (setq tmpsymbol (if (eq char-after-ip ?{)
9118 'inline-open
9119 'lambda-intro-cont)))
9120 (goto-char (cdr placeholder))
9121 (back-to-indentation)
9122 (c-add-stmt-syntax tmpsymbol nil t
9123 (c-most-enclosing-brace c-state-cache (point))
9124 paren-state)
9125 (unless (eq (point) (cdr placeholder))
9126 (c-add-syntax (car placeholder))))
9128 ;; CASE 5: Line is inside a declaration level block or at top level.
9129 ((or containing-decl-open (null containing-sexp))
9130 (cond
9132 ;; CASE 5A: we are looking at a defun, brace list, class,
9133 ;; or inline-inclass method opening brace
9134 ((setq special-brace-list
9135 (or (and c-special-brace-lists
9136 (c-looking-at-special-brace-list))
9137 (eq char-after-ip ?{)))
9138 (cond
9140 ;; CASE 5A.1: Non-class declaration block open.
9141 ((save-excursion
9142 (let (tmp)
9143 (and (eq char-after-ip ?{)
9144 (setq tmp (c-looking-at-decl-block containing-sexp t))
9145 (progn
9146 (setq placeholder (point))
9147 (goto-char tmp)
9148 (looking-at c-symbol-key))
9149 (c-keyword-member
9150 (c-keyword-sym (setq keyword (match-string 0)))
9151 'c-other-block-decl-kwds))))
9152 (goto-char placeholder)
9153 (c-add-stmt-syntax
9154 (if (string-equal keyword "extern")
9155 ;; Special case for extern-lang-open.
9156 'extern-lang-open
9157 (intern (concat keyword "-open")))
9158 nil t containing-sexp paren-state))
9160 ;; CASE 5A.2: we are looking at a class opening brace
9161 ((save-excursion
9162 (goto-char indent-point)
9163 (skip-chars-forward " \t")
9164 (and (eq (char-after) ?{)
9165 (c-looking-at-decl-block containing-sexp t)
9166 (setq placeholder (point))))
9167 (c-add-syntax 'class-open placeholder))
9169 ;; CASE 5A.3: brace list open
9170 ((save-excursion
9171 (c-beginning-of-decl-1 lim)
9172 (while (looking-at c-specifier-key)
9173 (goto-char (match-end 1))
9174 (c-forward-syntactic-ws indent-point))
9175 (setq placeholder (c-point 'boi))
9176 (or (consp special-brace-list)
9177 (and (or (save-excursion
9178 (goto-char indent-point)
9179 (setq tmpsymbol nil)
9180 (while (and (> (point) placeholder)
9181 (zerop (c-backward-token-2 1 t))
9182 (/= (char-after) ?=))
9183 (and c-opt-inexpr-brace-list-key
9184 (not tmpsymbol)
9185 (looking-at c-opt-inexpr-brace-list-key)
9186 (setq tmpsymbol 'topmost-intro-cont)))
9187 (eq (char-after) ?=))
9188 (looking-at c-brace-list-key))
9189 (save-excursion
9190 (while (and (< (point) indent-point)
9191 (zerop (c-forward-token-2 1 t))
9192 (not (memq (char-after) '(?\; ?\()))))
9193 (not (memq (char-after) '(?\; ?\()))
9194 ))))
9195 (if (and (not c-auto-newline-analysis)
9196 (c-major-mode-is 'java-mode)
9197 (eq tmpsymbol 'topmost-intro-cont))
9198 ;; We're in Java and have found that the open brace
9199 ;; belongs to a "new Foo[]" initialization list,
9200 ;; which means the brace list is part of an
9201 ;; expression and not a top level definition. We
9202 ;; therefore treat it as any topmost continuation
9203 ;; even though the semantically correct symbol still
9204 ;; is brace-list-open, on the same grounds as in
9205 ;; case B.2.
9206 (progn
9207 (c-beginning-of-statement-1 lim)
9208 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9209 (c-add-syntax 'brace-list-open placeholder)))
9211 ;; CASE 5A.4: inline defun open
9212 ((and containing-decl-open
9213 (not (c-keyword-member containing-decl-kwd
9214 'c-other-block-decl-kwds)))
9215 (c-add-syntax 'inline-open)
9216 (c-add-class-syntax 'inclass
9217 containing-decl-open
9218 containing-decl-start
9219 containing-decl-kwd
9220 paren-state))
9222 ;; CASE 5A.5: ordinary defun open
9224 (save-excursion
9225 (c-beginning-of-decl-1 lim)
9226 (while (looking-at c-specifier-key)
9227 (goto-char (match-end 1))
9228 (c-forward-syntactic-ws indent-point))
9229 (c-add-syntax 'defun-open (c-point 'boi))
9230 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9231 ;; 2007-11-09)
9232 ))))
9234 ;; CASE 5B: After a function header but before the body (or
9235 ;; the ending semicolon if there's no body).
9236 ((save-excursion
9237 (when (setq placeholder (c-just-after-func-arglist-p lim))
9238 (setq tmp-pos (point))))
9239 (cond
9241 ;; CASE 5B.1: Member init list.
9242 ((eq (char-after tmp-pos) ?:)
9243 (if (or (> tmp-pos indent-point)
9244 (= (c-point 'bosws) (1+ tmp-pos)))
9245 (progn
9246 ;; There is no preceding member init clause.
9247 ;; Indent relative to the beginning of indentation
9248 ;; for the topmost-intro line that contains the
9249 ;; prototype's open paren.
9250 (goto-char placeholder)
9251 (c-add-syntax 'member-init-intro (c-point 'boi)))
9252 ;; Indent relative to the first member init clause.
9253 (goto-char (1+ tmp-pos))
9254 (c-forward-syntactic-ws)
9255 (c-add-syntax 'member-init-cont (point))))
9257 ;; CASE 5B.2: K&R arg decl intro
9258 ((and c-recognize-knr-p
9259 (c-in-knr-argdecl lim))
9260 (c-beginning-of-statement-1 lim)
9261 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9262 (if containing-decl-open
9263 (c-add-class-syntax 'inclass
9264 containing-decl-open
9265 containing-decl-start
9266 containing-decl-kwd
9267 paren-state)))
9269 ;; CASE 5B.4: Nether region after a C++ or Java func
9270 ;; decl, which could include a `throws' declaration.
9272 (c-beginning-of-statement-1 lim)
9273 (c-add-syntax 'func-decl-cont (c-point 'boi))
9276 ;; CASE 5C: inheritance line. could be first inheritance
9277 ;; line, or continuation of a multiple inheritance
9278 ((or (and (c-major-mode-is 'c++-mode)
9279 (progn
9280 (when (eq char-after-ip ?,)
9281 (skip-chars-forward " \t")
9282 (forward-char))
9283 (looking-at c-opt-postfix-decl-spec-key)))
9284 (and (or (eq char-before-ip ?:)
9285 ;; watch out for scope operator
9286 (save-excursion
9287 (and (eq char-after-ip ?:)
9288 (c-safe (forward-char 1) t)
9289 (not (eq (char-after) ?:))
9291 (save-excursion
9292 (c-backward-syntactic-ws lim)
9293 (if (eq char-before-ip ?:)
9294 (progn
9295 (forward-char -1)
9296 (c-backward-syntactic-ws lim)))
9297 (back-to-indentation)
9298 (looking-at c-class-key)))
9299 ;; for Java
9300 (and (c-major-mode-is 'java-mode)
9301 (let ((fence (save-excursion
9302 (c-beginning-of-statement-1 lim)
9303 (point)))
9304 cont done)
9305 (save-excursion
9306 (while (not done)
9307 (cond ((looking-at c-opt-postfix-decl-spec-key)
9308 (setq injava-inher (cons cont (point))
9309 done t))
9310 ((or (not (c-safe (c-forward-sexp -1) t))
9311 (<= (point) fence))
9312 (setq done t))
9314 (setq cont t)))
9315 injava-inher)
9316 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9317 (point)))
9319 (cond
9321 ;; CASE 5C.1: non-hanging colon on an inher intro
9322 ((eq char-after-ip ?:)
9323 (c-beginning-of-statement-1 lim)
9324 (c-add-syntax 'inher-intro (c-point 'boi))
9325 ;; don't add inclass symbol since relative point already
9326 ;; contains any class offset
9329 ;; CASE 5C.2: hanging colon on an inher intro
9330 ((eq char-before-ip ?:)
9331 (c-beginning-of-statement-1 lim)
9332 (c-add-syntax 'inher-intro (c-point 'boi))
9333 (if containing-decl-open
9334 (c-add-class-syntax 'inclass
9335 containing-decl-open
9336 containing-decl-start
9337 containing-decl-kwd
9338 paren-state)))
9340 ;; CASE 5C.3: in a Java implements/extends
9341 (injava-inher
9342 (let ((where (cdr injava-inher))
9343 (cont (car injava-inher)))
9344 (goto-char where)
9345 (cond ((looking-at "throws\\>[^_]")
9346 (c-add-syntax 'func-decl-cont
9347 (progn (c-beginning-of-statement-1 lim)
9348 (c-point 'boi))))
9349 (cont (c-add-syntax 'inher-cont where))
9350 (t (c-add-syntax 'inher-intro
9351 (progn (goto-char (cdr injava-inher))
9352 (c-beginning-of-statement-1 lim)
9353 (point))))
9356 ;; CASE 5C.4: a continued inheritance line
9358 (c-beginning-of-inheritance-list lim)
9359 (c-add-syntax 'inher-cont (point))
9360 ;; don't add inclass symbol since relative point already
9361 ;; contains any class offset
9364 ;; CASE 5D: this could be a top-level initialization, a
9365 ;; member init list continuation, or a template argument
9366 ;; list continuation.
9367 ((save-excursion
9368 ;; Note: We use the fact that lim is always after any
9369 ;; preceding brace sexp.
9370 (if c-recognize-<>-arglists
9371 (while (and
9372 (progn
9373 (c-syntactic-skip-backward "^;,=<>" lim t)
9374 (> (point) lim))
9376 (when c-overloadable-operators-regexp
9377 (when (setq placeholder (c-after-special-operator-id lim))
9378 (goto-char placeholder)
9380 (cond
9381 ((eq (char-before) ?>)
9382 (or (c-backward-<>-arglist nil lim)
9383 (backward-char))
9385 ((eq (char-before) ?<)
9386 (backward-char)
9387 (if (save-excursion
9388 (c-forward-<>-arglist nil))
9389 (progn (forward-char)
9390 nil)
9392 (t nil)))))
9393 ;; NB: No c-after-special-operator-id stuff in this
9394 ;; clause - we assume only C++ needs it.
9395 (c-syntactic-skip-backward "^;,=" lim t))
9396 (memq (char-before) '(?, ?= ?<)))
9397 (cond
9399 ;; CASE 5D.3: perhaps a template list continuation?
9400 ((and (c-major-mode-is 'c++-mode)
9401 (save-excursion
9402 (save-restriction
9403 (c-with-syntax-table c++-template-syntax-table
9404 (goto-char indent-point)
9405 (setq placeholder (c-up-list-backward))
9406 (and placeholder
9407 (eq (char-after placeholder) ?<))))))
9408 (c-with-syntax-table c++-template-syntax-table
9409 (goto-char placeholder)
9410 (c-beginning-of-statement-1 lim t)
9411 (if (save-excursion
9412 (c-backward-syntactic-ws lim)
9413 (eq (char-before) ?<))
9414 ;; In a nested template arglist.
9415 (progn
9416 (goto-char placeholder)
9417 (c-syntactic-skip-backward "^,;" lim t)
9418 (c-forward-syntactic-ws))
9419 (back-to-indentation)))
9420 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9421 ;; template aware.
9422 (c-add-syntax 'template-args-cont (point) placeholder))
9424 ;; CASE 5D.4: perhaps a multiple inheritance line?
9425 ((and (c-major-mode-is 'c++-mode)
9426 (save-excursion
9427 (c-beginning-of-statement-1 lim)
9428 (setq placeholder (point))
9429 (if (looking-at "static\\>[^_]")
9430 (c-forward-token-2 1 nil indent-point))
9431 (and (looking-at c-class-key)
9432 (zerop (c-forward-token-2 2 nil indent-point))
9433 (if (eq (char-after) ?<)
9434 (c-with-syntax-table c++-template-syntax-table
9435 (zerop (c-forward-token-2 1 t indent-point)))
9437 (eq (char-after) ?:))))
9438 (goto-char placeholder)
9439 (c-add-syntax 'inher-cont (c-point 'boi)))
9441 ;; CASE 5D.5: Continuation of the "expression part" of a
9442 ;; top level construct. Or, perhaps, an unrecognised construct.
9444 (while (and (setq placeholder (point))
9445 (eq (car (c-beginning-of-decl-1 containing-sexp))
9446 'same)
9447 (save-excursion
9448 (c-backward-syntactic-ws)
9449 (eq (char-before) ?}))
9450 (< (point) placeholder)))
9451 (c-add-stmt-syntax
9452 (cond
9453 ((eq (point) placeholder) 'statement) ; unrecognised construct
9454 ;; A preceding comma at the top level means that a
9455 ;; new variable declaration starts here. Use
9456 ;; topmost-intro-cont for it, for consistency with
9457 ;; the first variable declaration. C.f. case 5N.
9458 ((eq char-before-ip ?,) 'topmost-intro-cont)
9459 (t 'statement-cont))
9460 nil nil containing-sexp paren-state))
9463 ;; CASE 5F: Close of a non-class declaration level block.
9464 ((and (eq char-after-ip ?})
9465 (c-keyword-member containing-decl-kwd
9466 'c-other-block-decl-kwds))
9467 ;; This is inconsistent: Should use `containing-decl-open'
9468 ;; here if it's at boi, like in case 5J.
9469 (goto-char containing-decl-start)
9470 (c-add-stmt-syntax
9471 (if (string-equal (symbol-name containing-decl-kwd) "extern")
9472 ;; Special case for compatibility with the
9473 ;; extern-lang syntactic symbols.
9474 'extern-lang-close
9475 (intern (concat (symbol-name containing-decl-kwd)
9476 "-close")))
9477 nil t
9478 (c-most-enclosing-brace paren-state (point))
9479 paren-state))
9481 ;; CASE 5G: we are looking at the brace which closes the
9482 ;; enclosing nested class decl
9483 ((and containing-sexp
9484 (eq char-after-ip ?})
9485 (eq containing-decl-open containing-sexp))
9486 (c-add-class-syntax 'class-close
9487 containing-decl-open
9488 containing-decl-start
9489 containing-decl-kwd
9490 paren-state))
9492 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9493 ((and c-recognize-knr-p
9494 (not containing-sexp) ; can't be knr inside braces.
9495 (not (eq char-before-ip ?}))
9496 (save-excursion
9497 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
9498 (and placeholder
9499 ;; Do an extra check to avoid tripping up on
9500 ;; statements that occur in invalid contexts
9501 ;; (e.g. in macro bodies where we don't really
9502 ;; know the context of what we're looking at).
9503 (not (and c-opt-block-stmt-key
9504 (looking-at c-opt-block-stmt-key)))))
9505 (< placeholder indent-point))
9506 (goto-char placeholder)
9507 (c-add-syntax 'knr-argdecl (point)))
9509 ;; CASE 5I: ObjC method definition.
9510 ((and c-opt-method-key
9511 (looking-at c-opt-method-key))
9512 (c-beginning-of-statement-1 nil t)
9513 (if (= (point) indent-point)
9514 ;; Handle the case when it's the first (non-comment)
9515 ;; thing in the buffer. Can't look for a 'same return
9516 ;; value from cbos1 since ObjC directives currently
9517 ;; aren't recognized fully, so that we get 'same
9518 ;; instead of 'previous if it moved over a preceding
9519 ;; directive.
9520 (goto-char (point-min)))
9521 (c-add-syntax 'objc-method-intro (c-point 'boi)))
9523 ;; CASE 5P: AWK pattern or function or continuation
9524 ;; thereof.
9525 ((c-major-mode-is 'awk-mode)
9526 (setq placeholder (point))
9527 (c-add-stmt-syntax
9528 (if (and (eq (c-beginning-of-statement-1) 'same)
9529 (/= (point) placeholder))
9530 'topmost-intro-cont
9531 'topmost-intro)
9532 nil nil
9533 containing-sexp paren-state))
9535 ;; CASE 5N: At a variable declaration that follows a class
9536 ;; definition or some other block declaration that doesn't
9537 ;; end at the closing '}'. C.f. case 5D.5.
9538 ((progn
9539 (c-backward-syntactic-ws lim)
9540 (and (eq (char-before) ?})
9541 (save-excursion
9542 (let ((start (point)))
9543 (if (and c-state-cache
9544 (consp (car c-state-cache))
9545 (eq (cdar c-state-cache) (point)))
9546 ;; Speed up the backward search a bit.
9547 (goto-char (caar c-state-cache)))
9548 (c-beginning-of-decl-1 containing-sexp)
9549 (setq placeholder (point))
9550 (if (= start (point))
9551 ;; The '}' is unbalanced.
9553 (c-end-of-decl-1)
9554 (>= (point) indent-point))))))
9555 (goto-char placeholder)
9556 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9557 containing-sexp paren-state))
9559 ;; NOTE: The point is at the end of the previous token here.
9561 ;; CASE 5J: we are at the topmost level, make
9562 ;; sure we skip back past any access specifiers
9563 ((and
9564 ;; A macro continuation line is never at top level.
9565 (not (and macro-start
9566 (> indent-point macro-start)))
9567 (save-excursion
9568 (setq placeholder (point))
9569 (or (memq char-before-ip '(?\; ?{ ?} nil))
9570 (c-at-vsemi-p before-ws-ip)
9571 (when (and (eq char-before-ip ?:)
9572 (eq (c-beginning-of-statement-1 lim)
9573 'label))
9574 (c-backward-syntactic-ws lim)
9575 (setq placeholder (point)))
9576 (and (c-major-mode-is 'objc-mode)
9577 (catch 'not-in-directive
9578 (c-beginning-of-statement-1 lim)
9579 (setq placeholder (point))
9580 (while (and (c-forward-objc-directive)
9581 (< (point) indent-point))
9582 (c-forward-syntactic-ws)
9583 (if (>= (point) indent-point)
9584 (throw 'not-in-directive t))
9585 (setq placeholder (point)))
9586 nil)))))
9587 ;; For historic reasons we anchor at bol of the last
9588 ;; line of the previous declaration. That's clearly
9589 ;; highly bogus and useless, and it makes our lives hard
9590 ;; to remain compatible. :P
9591 (goto-char placeholder)
9592 (c-add-syntax 'topmost-intro (c-point 'bol))
9593 (if containing-decl-open
9594 (if (c-keyword-member containing-decl-kwd
9595 'c-other-block-decl-kwds)
9596 (progn
9597 (goto-char (c-brace-anchor-point containing-decl-open))
9598 (c-add-stmt-syntax
9599 (if (string-equal (symbol-name containing-decl-kwd)
9600 "extern")
9601 ;; Special case for compatibility with the
9602 ;; extern-lang syntactic symbols.
9603 'inextern-lang
9604 (intern (concat "in"
9605 (symbol-name containing-decl-kwd))))
9606 nil t
9607 (c-most-enclosing-brace paren-state (point))
9608 paren-state))
9609 (c-add-class-syntax 'inclass
9610 containing-decl-open
9611 containing-decl-start
9612 containing-decl-kwd
9613 paren-state)))
9614 (when (and c-syntactic-indentation-in-macros
9615 macro-start
9616 (/= macro-start (c-point 'boi indent-point)))
9617 (c-add-syntax 'cpp-define-intro)
9618 (setq macro-start nil)))
9620 ;; CASE 5K: we are at an ObjC method definition
9621 ;; continuation line.
9622 ((and c-opt-method-key
9623 (save-excursion
9624 (c-beginning-of-statement-1 lim)
9625 (beginning-of-line)
9626 (when (looking-at c-opt-method-key)
9627 (setq placeholder (point)))))
9628 (c-add-syntax 'objc-method-args-cont placeholder))
9630 ;; CASE 5L: we are at the first argument of a template
9631 ;; arglist that begins on the previous line.
9632 ((and c-recognize-<>-arglists
9633 (eq (char-before) ?<)
9634 (not (and c-overloadable-operators-regexp
9635 (c-after-special-operator-id lim))))
9636 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9637 (c-add-syntax 'template-args-cont (c-point 'boi)))
9639 ;; CASE 5Q: we are at a statement within a macro.
9640 (macro-start
9641 (c-beginning-of-statement-1 containing-sexp)
9642 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
9644 ;;CASE 5N: We are at a tompmost continuation line and the only
9645 ;;preceding items are annotations.
9646 ((and (c-major-mode-is 'java-mode)
9647 (setq placeholder (point))
9648 (c-beginning-of-statement-1)
9649 (progn
9650 (while (and (c-forward-annotation))
9651 (c-forward-syntactic-ws))
9653 (prog1
9654 (>= (point) placeholder)
9655 (goto-char placeholder)))
9656 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
9658 ;; CASE 5M: we are at a topmost continuation line
9660 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9661 (when (c-major-mode-is 'objc-mode)
9662 (setq placeholder (point))
9663 (while (and (c-forward-objc-directive)
9664 (< (point) indent-point))
9665 (c-forward-syntactic-ws)
9666 (setq placeholder (point)))
9667 (goto-char placeholder))
9668 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9672 ;; (CASE 6 has been removed.)
9674 ;; CASE 7: line is an expression, not a statement. Most
9675 ;; likely we are either in a function prototype or a function
9676 ;; call argument list
9677 ((not (or (and c-special-brace-lists
9678 (save-excursion
9679 (goto-char containing-sexp)
9680 (c-looking-at-special-brace-list)))
9681 (eq (char-after containing-sexp) ?{)))
9682 (cond
9684 ;; CASE 7A: we are looking at the arglist closing paren.
9685 ;; C.f. case 7F.
9686 ((memq char-after-ip '(?\) ?\]))
9687 (goto-char containing-sexp)
9688 (setq placeholder (c-point 'boi))
9689 (if (and (c-safe (backward-up-list 1) t)
9690 (>= (point) placeholder))
9691 (progn
9692 (forward-char)
9693 (skip-chars-forward " \t"))
9694 (goto-char placeholder))
9695 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
9696 (c-most-enclosing-brace paren-state (point))
9697 paren-state))
9699 ;; CASE 7B: Looking at the opening brace of an
9700 ;; in-expression block or brace list. C.f. cases 4, 16A
9701 ;; and 17E.
9702 ((and (eq char-after-ip ?{)
9703 (progn
9704 (setq placeholder (c-inside-bracelist-p (point)
9705 paren-state))
9706 (if placeholder
9707 (setq tmpsymbol '(brace-list-open . inexpr-class))
9708 (setq tmpsymbol '(block-open . inexpr-statement)
9709 placeholder
9710 (cdr-safe (c-looking-at-inexpr-block
9711 (c-safe-position containing-sexp
9712 paren-state)
9713 containing-sexp)))
9714 ;; placeholder is nil if it's a block directly in
9715 ;; a function arglist. That makes us skip out of
9716 ;; this case.
9718 (goto-char placeholder)
9719 (back-to-indentation)
9720 (c-add-stmt-syntax (car tmpsymbol) nil t
9721 (c-most-enclosing-brace paren-state (point))
9722 paren-state)
9723 (if (/= (point) placeholder)
9724 (c-add-syntax (cdr tmpsymbol))))
9726 ;; CASE 7C: we are looking at the first argument in an empty
9727 ;; argument list. Use arglist-close if we're actually
9728 ;; looking at a close paren or bracket.
9729 ((memq char-before-ip '(?\( ?\[))
9730 (goto-char containing-sexp)
9731 (setq placeholder (c-point 'boi))
9732 (if (and (c-safe (backward-up-list 1) t)
9733 (>= (point) placeholder))
9734 (progn
9735 (forward-char)
9736 (skip-chars-forward " \t"))
9737 (goto-char placeholder))
9738 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
9739 (c-most-enclosing-brace paren-state (point))
9740 paren-state))
9742 ;; CASE 7D: we are inside a conditional test clause. treat
9743 ;; these things as statements
9744 ((progn
9745 (goto-char containing-sexp)
9746 (and (c-safe (c-forward-sexp -1) t)
9747 (looking-at "\\<for\\>[^_]")))
9748 (goto-char (1+ containing-sexp))
9749 (c-forward-syntactic-ws indent-point)
9750 (if (eq char-before-ip ?\;)
9751 (c-add-syntax 'statement (point))
9752 (c-add-syntax 'statement-cont (point))
9755 ;; CASE 7E: maybe a continued ObjC method call. This is the
9756 ;; case when we are inside a [] bracketed exp, and what
9757 ;; precede the opening bracket is not an identifier.
9758 ((and c-opt-method-key
9759 (eq (char-after containing-sexp) ?\[)
9760 (progn
9761 (goto-char (1- containing-sexp))
9762 (c-backward-syntactic-ws (c-point 'bod))
9763 (if (not (looking-at c-symbol-key))
9764 (c-add-syntax 'objc-method-call-cont containing-sexp))
9767 ;; CASE 7F: we are looking at an arglist continuation line,
9768 ;; but the preceding argument is on the same line as the
9769 ;; opening paren. This case includes multi-line
9770 ;; mathematical paren groupings, but we could be on a
9771 ;; for-list continuation line. C.f. case 7A.
9772 ((progn
9773 (goto-char (1+ containing-sexp))
9774 (< (save-excursion
9775 (c-forward-syntactic-ws)
9776 (point))
9777 (c-point 'bonl)))
9778 (goto-char containing-sexp) ; paren opening the arglist
9779 (setq placeholder (c-point 'boi))
9780 (if (and (c-safe (backward-up-list 1) t)
9781 (>= (point) placeholder))
9782 (progn
9783 (forward-char)
9784 (skip-chars-forward " \t"))
9785 (goto-char placeholder))
9786 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
9787 (c-most-enclosing-brace c-state-cache (point))
9788 paren-state))
9790 ;; CASE 7G: we are looking at just a normal arglist
9791 ;; continuation line
9792 (t (c-forward-syntactic-ws indent-point)
9793 (c-add-syntax 'arglist-cont (c-point 'boi)))
9796 ;; CASE 8: func-local multi-inheritance line
9797 ((and (c-major-mode-is 'c++-mode)
9798 (save-excursion
9799 (goto-char indent-point)
9800 (skip-chars-forward " \t")
9801 (looking-at c-opt-postfix-decl-spec-key)))
9802 (goto-char indent-point)
9803 (skip-chars-forward " \t")
9804 (cond
9806 ;; CASE 8A: non-hanging colon on an inher intro
9807 ((eq char-after-ip ?:)
9808 (c-backward-syntactic-ws lim)
9809 (c-add-syntax 'inher-intro (c-point 'boi)))
9811 ;; CASE 8B: hanging colon on an inher intro
9812 ((eq char-before-ip ?:)
9813 (c-add-syntax 'inher-intro (c-point 'boi)))
9815 ;; CASE 8C: a continued inheritance line
9817 (c-beginning-of-inheritance-list lim)
9818 (c-add-syntax 'inher-cont (point))
9821 ;; CASE 9: we are inside a brace-list
9822 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
9823 (setq special-brace-list
9824 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
9825 (save-excursion
9826 (goto-char containing-sexp)
9827 (c-looking-at-special-brace-list)))
9828 (c-inside-bracelist-p containing-sexp paren-state))))
9829 (cond
9831 ;; CASE 9A: In the middle of a special brace list opener.
9832 ((and (consp special-brace-list)
9833 (save-excursion
9834 (goto-char containing-sexp)
9835 (eq (char-after) ?\())
9836 (eq char-after-ip (car (cdr special-brace-list))))
9837 (goto-char (car (car special-brace-list)))
9838 (skip-chars-backward " \t")
9839 (if (and (bolp)
9840 (assoc 'statement-cont
9841 (setq placeholder (c-guess-basic-syntax))))
9842 (setq c-syntactic-context placeholder)
9843 (c-beginning-of-statement-1
9844 (c-safe-position (1- containing-sexp) paren-state))
9845 (c-forward-token-2 0)
9846 (while (looking-at c-specifier-key)
9847 (goto-char (match-end 1))
9848 (c-forward-syntactic-ws))
9849 (c-add-syntax 'brace-list-open (c-point 'boi))))
9851 ;; CASE 9B: brace-list-close brace
9852 ((if (consp special-brace-list)
9853 ;; Check special brace list closer.
9854 (progn
9855 (goto-char (car (car special-brace-list)))
9856 (save-excursion
9857 (goto-char indent-point)
9858 (back-to-indentation)
9860 ;; We were between the special close char and the `)'.
9861 (and (eq (char-after) ?\))
9862 (eq (1+ (point)) (cdr (car special-brace-list))))
9863 ;; We were before the special close char.
9864 (and (eq (char-after) (cdr (cdr special-brace-list)))
9865 (zerop (c-forward-token-2))
9866 (eq (1+ (point)) (cdr (car special-brace-list)))))))
9867 ;; Normal brace list check.
9868 (and (eq char-after-ip ?})
9869 (c-safe (goto-char (c-up-list-backward (point))) t)
9870 (= (point) containing-sexp)))
9871 (if (eq (point) (c-point 'boi))
9872 (c-add-syntax 'brace-list-close (point))
9873 (setq lim (c-most-enclosing-brace c-state-cache (point)))
9874 (c-beginning-of-statement-1 lim)
9875 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
9878 ;; Prepare for the rest of the cases below by going to the
9879 ;; token following the opening brace
9880 (if (consp special-brace-list)
9881 (progn
9882 (goto-char (car (car special-brace-list)))
9883 (c-forward-token-2 1 nil indent-point))
9884 (goto-char containing-sexp))
9885 (forward-char)
9886 (let ((start (point)))
9887 (c-forward-syntactic-ws indent-point)
9888 (goto-char (max start (c-point 'bol))))
9889 (c-skip-ws-forward indent-point)
9890 (cond
9892 ;; CASE 9C: we're looking at the first line in a brace-list
9893 ((= (point) indent-point)
9894 (if (consp special-brace-list)
9895 (goto-char (car (car special-brace-list)))
9896 (goto-char containing-sexp))
9897 (if (eq (point) (c-point 'boi))
9898 (c-add-syntax 'brace-list-intro (point))
9899 (setq lim (c-most-enclosing-brace c-state-cache (point)))
9900 (c-beginning-of-statement-1 lim)
9901 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
9903 ;; CASE 9D: this is just a later brace-list-entry or
9904 ;; brace-entry-open
9905 (t (if (or (eq char-after-ip ?{)
9906 (and c-special-brace-lists
9907 (save-excursion
9908 (goto-char indent-point)
9909 (c-forward-syntactic-ws (c-point 'eol))
9910 (c-looking-at-special-brace-list (point)))))
9911 (c-add-syntax 'brace-entry-open (point))
9912 (c-add-syntax 'brace-list-entry (point))
9914 ))))
9916 ;; CASE 10: A continued statement or top level construct.
9917 ((and (not (memq char-before-ip '(?\; ?:)))
9918 (not (c-at-vsemi-p before-ws-ip))
9919 (or (not (eq char-before-ip ?}))
9920 (c-looking-at-inexpr-block-backward c-state-cache))
9921 (> (point)
9922 (save-excursion
9923 (c-beginning-of-statement-1 containing-sexp)
9924 (setq placeholder (point))))
9925 (/= placeholder containing-sexp))
9926 ;; This is shared with case 18.
9927 (c-guess-continued-construct indent-point
9928 char-after-ip
9929 placeholder
9930 containing-sexp
9931 paren-state))
9933 ;; CASE 16: block close brace, possibly closing the defun or
9934 ;; the class
9935 ((eq char-after-ip ?})
9936 ;; From here on we have the next containing sexp in lim.
9937 (setq lim (c-most-enclosing-brace paren-state))
9938 (goto-char containing-sexp)
9939 (cond
9941 ;; CASE 16E: Closing a statement block? This catches
9942 ;; cases where it's preceded by a statement keyword,
9943 ;; which works even when used in an "invalid" context,
9944 ;; e.g. a macro argument.
9945 ((c-after-conditional)
9946 (c-backward-to-block-anchor lim)
9947 (c-add-stmt-syntax 'block-close nil t lim paren-state))
9949 ;; CASE 16A: closing a lambda defun or an in-expression
9950 ;; block? C.f. cases 4, 7B and 17E.
9951 ((setq placeholder (c-looking-at-inexpr-block
9952 (c-safe-position containing-sexp paren-state)
9953 nil))
9954 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
9955 'inline-close
9956 'block-close))
9957 (goto-char containing-sexp)
9958 (back-to-indentation)
9959 (if (= containing-sexp (point))
9960 (c-add-syntax tmpsymbol (point))
9961 (goto-char (cdr placeholder))
9962 (back-to-indentation)
9963 (c-add-stmt-syntax tmpsymbol nil t
9964 (c-most-enclosing-brace paren-state (point))
9965 paren-state)
9966 (if (/= (point) (cdr placeholder))
9967 (c-add-syntax (car placeholder)))))
9969 ;; CASE 16B: does this close an inline or a function in
9970 ;; a non-class declaration level block?
9971 ((save-excursion
9972 (and lim
9973 (progn
9974 (goto-char lim)
9975 (c-looking-at-decl-block
9976 (c-most-enclosing-brace paren-state lim)
9977 nil))
9978 (setq placeholder (point))))
9979 (c-backward-to-decl-anchor lim)
9980 (back-to-indentation)
9981 (if (save-excursion
9982 (goto-char placeholder)
9983 (looking-at c-other-decl-block-key))
9984 (c-add-syntax 'defun-close (point))
9985 (c-add-syntax 'inline-close (point))))
9987 ;; CASE 16F: Can be a defun-close of a function declared
9988 ;; in a statement block, e.g. in Pike or when using gcc
9989 ;; extensions, but watch out for macros followed by
9990 ;; blocks. Let it through to be handled below.
9991 ;; C.f. cases B.3 and 17G.
9992 ((save-excursion
9993 (and (not (c-at-statement-start-p))
9994 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
9995 (setq placeholder (point))
9996 (let ((c-recognize-typeless-decls nil))
9997 ;; Turn off recognition of constructs that
9998 ;; lacks a type in this case, since that's more
9999 ;; likely to be a macro followed by a block.
10000 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10001 (back-to-indentation)
10002 (if (/= (point) containing-sexp)
10003 (goto-char placeholder))
10004 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
10006 ;; CASE 16C: If there is an enclosing brace then this is
10007 ;; a block close since defun closes inside declaration
10008 ;; level blocks have been handled above.
10009 (lim
10010 ;; If the block is preceded by a case/switch label on
10011 ;; the same line, we anchor at the first preceding label
10012 ;; at boi. The default handling in c-add-stmt-syntax
10013 ;; really fixes it better, but we do like this to keep
10014 ;; the indentation compatible with version 5.28 and
10015 ;; earlier. C.f. case 17H.
10016 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10017 (eq (c-beginning-of-statement-1 lim) 'label)))
10018 (goto-char placeholder)
10019 (if (looking-at c-label-kwds-regexp)
10020 (c-add-syntax 'block-close (point))
10021 (goto-char containing-sexp)
10022 ;; c-backward-to-block-anchor not necessary here; those
10023 ;; situations are handled in case 16E above.
10024 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
10026 ;; CASE 16D: Only top level defun close left.
10028 (goto-char containing-sexp)
10029 (c-backward-to-decl-anchor lim)
10030 (c-add-stmt-syntax 'defun-close nil nil
10031 (c-most-enclosing-brace paren-state)
10032 paren-state))
10035 ;; CASE 19: line is an expression, not a statement, and is directly
10036 ;; contained by a template delimiter. Most likely, we are in a
10037 ;; template arglist within a statement. This case is based on CASE
10038 ;; 7. At some point in the future, we may wish to create more
10039 ;; syntactic symbols such as `template-intro',
10040 ;; `template-cont-nonempty', etc., and distinguish between them as we
10041 ;; do for `arglist-intro' etc. (2009-12-07).
10042 ((and c-recognize-<>-arglists
10043 (setq containing-< (c-up-list-backward indent-point containing-sexp))
10044 (eq (char-after containing-<) ?\<))
10045 (setq placeholder (c-point 'boi containing-<))
10046 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
10047 ; '<') before indent-point.
10048 (if (>= (point) placeholder)
10049 (progn
10050 (forward-char)
10051 (skip-chars-forward " \t"))
10052 (goto-char placeholder))
10053 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
10054 (c-most-enclosing-brace c-state-cache (point))
10055 paren-state))
10057 ;; CASE 17: Statement or defun catchall.
10059 (goto-char indent-point)
10060 ;; Back up statements until we find one that starts at boi.
10061 (while (let* ((prev-point (point))
10062 (last-step-type (c-beginning-of-statement-1
10063 containing-sexp)))
10064 (if (= (point) prev-point)
10065 (progn
10066 (setq step-type (or step-type last-step-type))
10067 nil)
10068 (setq step-type last-step-type)
10069 (/= (point) (c-point 'boi)))))
10070 (cond
10072 ;; CASE 17B: continued statement
10073 ((and (eq step-type 'same)
10074 (/= (point) indent-point))
10075 (c-add-stmt-syntax 'statement-cont nil nil
10076 containing-sexp paren-state))
10078 ;; CASE 17A: After a case/default label?
10079 ((progn
10080 (while (and (eq step-type 'label)
10081 (not (looking-at c-label-kwds-regexp)))
10082 (setq step-type
10083 (c-beginning-of-statement-1 containing-sexp)))
10084 (eq step-type 'label))
10085 (c-add-stmt-syntax (if (eq char-after-ip ?{)
10086 'statement-case-open
10087 'statement-case-intro)
10088 nil t containing-sexp paren-state))
10090 ;; CASE 17D: any old statement
10091 ((progn
10092 (while (eq step-type 'label)
10093 (setq step-type
10094 (c-beginning-of-statement-1 containing-sexp)))
10095 (eq step-type 'previous))
10096 (c-add-stmt-syntax 'statement nil t
10097 containing-sexp paren-state)
10098 (if (eq char-after-ip ?{)
10099 (c-add-syntax 'block-open)))
10101 ;; CASE 17I: Inside a substatement block.
10102 ((progn
10103 ;; The following tests are all based on containing-sexp.
10104 (goto-char containing-sexp)
10105 ;; From here on we have the next containing sexp in lim.
10106 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
10107 (c-after-conditional))
10108 (c-backward-to-block-anchor lim)
10109 (c-add-stmt-syntax 'statement-block-intro nil t
10110 lim paren-state)
10111 (if (eq char-after-ip ?{)
10112 (c-add-syntax 'block-open)))
10114 ;; CASE 17E: first statement in an in-expression block.
10115 ;; C.f. cases 4, 7B and 16A.
10116 ((setq placeholder (c-looking-at-inexpr-block
10117 (c-safe-position containing-sexp paren-state)
10118 nil))
10119 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10120 'defun-block-intro
10121 'statement-block-intro))
10122 (back-to-indentation)
10123 (if (= containing-sexp (point))
10124 (c-add-syntax tmpsymbol (point))
10125 (goto-char (cdr placeholder))
10126 (back-to-indentation)
10127 (c-add-stmt-syntax tmpsymbol nil t
10128 (c-most-enclosing-brace c-state-cache (point))
10129 paren-state)
10130 (if (/= (point) (cdr placeholder))
10131 (c-add-syntax (car placeholder))))
10132 (if (eq char-after-ip ?{)
10133 (c-add-syntax 'block-open)))
10135 ;; CASE 17F: first statement in an inline, or first
10136 ;; statement in a top-level defun. we can tell this is it
10137 ;; if there are no enclosing braces that haven't been
10138 ;; narrowed out by a class (i.e. don't use bod here).
10139 ((save-excursion
10140 (or (not (setq placeholder (c-most-enclosing-brace
10141 paren-state)))
10142 (and (progn
10143 (goto-char placeholder)
10144 (eq (char-after) ?{))
10145 (c-looking-at-decl-block (c-most-enclosing-brace
10146 paren-state (point))
10147 nil))))
10148 (c-backward-to-decl-anchor lim)
10149 (back-to-indentation)
10150 (c-add-syntax 'defun-block-intro (point)))
10152 ;; CASE 17G: First statement in a function declared inside
10153 ;; a normal block. This can occur in Pike and with
10154 ;; e.g. the gcc extensions, but watch out for macros
10155 ;; followed by blocks. C.f. cases B.3 and 16F.
10156 ((save-excursion
10157 (and (not (c-at-statement-start-p))
10158 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10159 (setq placeholder (point))
10160 (let ((c-recognize-typeless-decls nil))
10161 ;; Turn off recognition of constructs that lacks
10162 ;; a type in this case, since that's more likely
10163 ;; to be a macro followed by a block.
10164 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10165 (back-to-indentation)
10166 (if (/= (point) containing-sexp)
10167 (goto-char placeholder))
10168 (c-add-stmt-syntax 'defun-block-intro nil t
10169 lim paren-state))
10171 ;; CASE 17H: First statement in a block.
10173 ;; If the block is preceded by a case/switch label on the
10174 ;; same line, we anchor at the first preceding label at
10175 ;; boi. The default handling in c-add-stmt-syntax is
10176 ;; really fixes it better, but we do like this to keep the
10177 ;; indentation compatible with version 5.28 and earlier.
10178 ;; C.f. case 16C.
10179 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10180 (eq (c-beginning-of-statement-1 lim) 'label)))
10181 (goto-char placeholder)
10182 (if (looking-at c-label-kwds-regexp)
10183 (c-add-syntax 'statement-block-intro (point))
10184 (goto-char containing-sexp)
10185 ;; c-backward-to-block-anchor not necessary here; those
10186 ;; situations are handled in case 17I above.
10187 (c-add-stmt-syntax 'statement-block-intro nil t
10188 lim paren-state))
10189 (if (eq char-after-ip ?{)
10190 (c-add-syntax 'block-open)))
10194 ;; now we need to look at any modifiers
10195 (goto-char indent-point)
10196 (skip-chars-forward " \t")
10198 ;; are we looking at a comment only line?
10199 (when (and (looking-at c-comment-start-regexp)
10200 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
10201 (c-append-syntax 'comment-intro))
10203 ;; we might want to give additional offset to friends (in C++).
10204 (when (and c-opt-friend-key
10205 (looking-at c-opt-friend-key))
10206 (c-append-syntax 'friend))
10208 ;; Set syntactic-relpos.
10209 (let ((p c-syntactic-context))
10210 (while (and p
10211 (if (integerp (c-langelem-pos (car p)))
10212 (progn
10213 (setq syntactic-relpos (c-langelem-pos (car p)))
10214 nil)
10216 (setq p (cdr p))))
10218 ;; Start of or a continuation of a preprocessor directive?
10219 (if (and macro-start
10220 (eq macro-start (c-point 'boi))
10221 (not (and (c-major-mode-is 'pike-mode)
10222 (eq (char-after (1+ macro-start)) ?\"))))
10223 (c-append-syntax 'cpp-macro)
10224 (when (and c-syntactic-indentation-in-macros macro-start)
10225 (if in-macro-expr
10226 (when (or
10227 (< syntactic-relpos macro-start)
10228 (not (or
10229 (assq 'arglist-intro c-syntactic-context)
10230 (assq 'arglist-cont c-syntactic-context)
10231 (assq 'arglist-cont-nonempty c-syntactic-context)
10232 (assq 'arglist-close c-syntactic-context))))
10233 ;; If inside a cpp expression, i.e. anywhere in a
10234 ;; cpp directive except a #define body, we only let
10235 ;; through the syntactic analysis that is internal
10236 ;; in the expression. That means the arglist
10237 ;; elements, if they are anchored inside the cpp
10238 ;; expression.
10239 (setq c-syntactic-context nil)
10240 (c-add-syntax 'cpp-macro-cont macro-start))
10241 (when (and (eq macro-start syntactic-relpos)
10242 (not (assq 'cpp-define-intro c-syntactic-context))
10243 (save-excursion
10244 (goto-char macro-start)
10245 (or (not (c-forward-to-cpp-define-body))
10246 (<= (point) (c-point 'boi indent-point)))))
10247 ;; Inside a #define body and the syntactic analysis is
10248 ;; anchored on the start of the #define. In this case
10249 ;; we add cpp-define-intro to get the extra
10250 ;; indentation of the #define body.
10251 (c-add-syntax 'cpp-define-intro)))))
10253 ;; return the syntax
10254 c-syntactic-context)))
10257 ;; Indentation calculation.
10259 (defun c-evaluate-offset (offset langelem symbol)
10260 ;; offset can be a number, a function, a variable, a list, or one of
10261 ;; the symbols + or -
10263 ;; This function might do hidden buffer changes.
10264 (let ((res
10265 (cond
10266 ((numberp offset) offset)
10267 ((vectorp offset) offset)
10268 ((null offset) nil)
10270 ((eq offset '+) c-basic-offset)
10271 ((eq offset '-) (- c-basic-offset))
10272 ((eq offset '++) (* 2 c-basic-offset))
10273 ((eq offset '--) (* 2 (- c-basic-offset)))
10274 ((eq offset '*) (/ c-basic-offset 2))
10275 ((eq offset '/) (/ (- c-basic-offset) 2))
10277 ((functionp offset)
10278 (c-evaluate-offset
10279 (funcall offset
10280 (cons (c-langelem-sym langelem)
10281 (c-langelem-pos langelem)))
10282 langelem symbol))
10284 ((listp offset)
10285 (cond
10286 ((eq (car offset) 'quote)
10287 (c-benign-error "The offset %S for %s was mistakenly quoted"
10288 offset symbol)
10289 nil)
10291 ((memq (car offset) '(min max))
10292 (let (res val (method (car offset)))
10293 (setq offset (cdr offset))
10294 (while offset
10295 (setq val (c-evaluate-offset (car offset) langelem symbol))
10296 (cond
10297 ((not val))
10298 ((not res)
10299 (setq res val))
10300 ((integerp val)
10301 (if (vectorp res)
10302 (c-benign-error "\
10303 Error evaluating offset %S for %s: \
10304 Cannot combine absolute offset %S with relative %S in `%s' method"
10305 (car offset) symbol res val method)
10306 (setq res (funcall method res val))))
10308 (if (integerp res)
10309 (c-benign-error "\
10310 Error evaluating offset %S for %s: \
10311 Cannot combine relative offset %S with absolute %S in `%s' method"
10312 (car offset) symbol res val method)
10313 (setq res (vector (funcall method (aref res 0)
10314 (aref val 0)))))))
10315 (setq offset (cdr offset)))
10316 res))
10318 ((eq (car offset) 'add)
10319 (let (res val)
10320 (setq offset (cdr offset))
10321 (while offset
10322 (setq val (c-evaluate-offset (car offset) langelem symbol))
10323 (cond
10324 ((not val))
10325 ((not res)
10326 (setq res val))
10327 ((integerp val)
10328 (if (vectorp res)
10329 (setq res (vector (+ (aref res 0) val)))
10330 (setq res (+ res val))))
10332 (if (vectorp res)
10333 (c-benign-error "\
10334 Error evaluating offset %S for %s: \
10335 Cannot combine absolute offsets %S and %S in `add' method"
10336 (car offset) symbol res val)
10337 (setq res val)))) ; Override.
10338 (setq offset (cdr offset)))
10339 res))
10342 (let (res)
10343 (when (eq (car offset) 'first)
10344 (setq offset (cdr offset)))
10345 (while (and (not res) offset)
10346 (setq res (c-evaluate-offset (car offset) langelem symbol)
10347 offset (cdr offset)))
10348 res))))
10350 ((and (symbolp offset) (boundp offset))
10351 (symbol-value offset))
10354 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10355 nil))))
10357 (if (or (null res) (integerp res)
10358 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10360 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10361 offset symbol res)
10362 nil)))
10364 (defun c-calc-offset (langelem)
10365 ;; Get offset from LANGELEM which is a list beginning with the
10366 ;; syntactic symbol and followed by any analysis data it provides.
10367 ;; That data may be zero or more elements, but if at least one is
10368 ;; given then the first is the anchor position (or nil). The symbol
10369 ;; is matched against `c-offsets-alist' and the offset calculated
10370 ;; from that is returned.
10372 ;; This function might do hidden buffer changes.
10373 (let* ((symbol (c-langelem-sym langelem))
10374 (match (assq symbol c-offsets-alist))
10375 (offset (cdr-safe match)))
10376 (if match
10377 (setq offset (c-evaluate-offset offset langelem symbol))
10378 (if c-strict-syntax-p
10379 (c-benign-error "No offset found for syntactic symbol %s" symbol))
10380 (setq offset 0))
10381 (if (vectorp offset)
10382 offset
10383 (or (and (numberp offset) offset)
10384 (and (symbolp offset) (symbol-value offset))
10388 (defun c-get-offset (langelem)
10389 ;; This is a compatibility wrapper for `c-calc-offset' in case
10390 ;; someone is calling it directly. It takes an old style syntactic
10391 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10392 ;; new list form.
10394 ;; This function might do hidden buffer changes.
10395 (if (c-langelem-pos langelem)
10396 (c-calc-offset (list (c-langelem-sym langelem)
10397 (c-langelem-pos langelem)))
10398 (c-calc-offset langelem)))
10400 (defun c-get-syntactic-indentation (langelems)
10401 ;; Calculate the syntactic indentation from a syntactic description
10402 ;; as returned by `c-guess-syntax'.
10404 ;; Note that topmost-intro always has an anchor position at bol, for
10405 ;; historical reasons. It's often used together with other symbols
10406 ;; that has more sane positions. Since we always use the first
10407 ;; found anchor position, we rely on that these other symbols always
10408 ;; precede topmost-intro in the LANGELEMS list.
10410 ;; This function might do hidden buffer changes.
10411 (let ((indent 0) anchor)
10413 (while langelems
10414 (let* ((c-syntactic-element (car langelems))
10415 (res (c-calc-offset c-syntactic-element)))
10417 (if (vectorp res)
10418 ;; Got an absolute column that overrides any indentation
10419 ;; we've collected so far, but not the relative
10420 ;; indentation we might get for the nested structures
10421 ;; further down the langelems list.
10422 (setq indent (elt res 0)
10423 anchor (point-min)) ; A position at column 0.
10425 ;; Got a relative change of the current calculated
10426 ;; indentation.
10427 (setq indent (+ indent res))
10429 ;; Use the anchor position from the first syntactic
10430 ;; element with one.
10431 (unless anchor
10432 (setq anchor (c-langelem-pos (car langelems)))))
10434 (setq langelems (cdr langelems))))
10436 (if anchor
10437 (+ indent (save-excursion
10438 (goto-char anchor)
10439 (current-column)))
10440 indent)))
10443 (cc-provide 'cc-engine)
10445 ;;; cc-engine.el ends here