Spelling fixes.
[emacs.git] / lisp / progmodes / cc-engine.el
blobfd32e5e956c40a5220c28c9e02833769e9cf18ad
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 (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1039 ;; Catch C++'s inheritance construct "class foo : bar".
1040 (save-excursion
1041 (and
1042 (c-safe (c-backward-sexp) t)
1043 (looking-at c-nonlabel-token-2-key))))
1044 (setq c-maybe-labelp nil)
1045 (if after-labels-pos ; Have we already encountered a label?
1046 (if (not last-label-pos)
1047 (setq last-label-pos (or tok start)))
1048 (setq after-labels-pos (or tok start)))
1049 (setq c-maybe-labelp t
1050 label-good-pos nil))) ; bogus "label"
1052 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1053 ; been found.
1054 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1055 ;; We're in a potential label and it's the first
1056 ;; time we've found something that isn't allowed in
1057 ;; one.
1058 (setq label-good-pos (or tok start))))
1060 ;; We've moved back by a sexp, so update the token positions.
1061 (setq sym nil
1062 pptok ptok
1063 ptok tok
1064 tok (point)
1065 pos tok) ; always non-nil
1066 ) ; end of (catch loop ....)
1067 ) ; end of sexp-at-a-time (while ....)
1069 ;; If the stack isn't empty there might be errors to report.
1070 (while stack
1071 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1072 (c-bos-report-error))
1073 (setq saved-pos (cdr (car stack))
1074 stack (cdr stack)))
1076 (when (and (eq ret 'same)
1077 (not (memq sym '(boundary ignore nil))))
1078 ;; Need to investigate closer whether we've crossed
1079 ;; between a substatement and its containing statement.
1080 (if (setq saved (if (looking-at c-block-stmt-1-key)
1081 ptok
1082 pptok))
1083 (cond ((> start saved) (setq pos saved))
1084 ((= start saved) (setq ret 'up)))))
1086 (when (and (not ignore-labels)
1087 (eq c-maybe-labelp t)
1088 (not (eq ret 'beginning))
1089 after-labels-pos
1090 (not bitfield-type-pos) ; Bitfields take precedence over labels.
1091 (or (not label-good-pos)
1092 (<= label-good-pos pos)
1093 (progn
1094 (goto-char (if (and last-label-pos
1095 (< last-label-pos start))
1096 last-label-pos
1097 pos))
1098 (looking-at c-label-kwds-regexp))))
1099 ;; We're in a label. Maybe we should step to the statement
1100 ;; after it.
1101 (if (< after-labels-pos start)
1102 (setq pos after-labels-pos)
1103 (setq ret 'label)
1104 (if (and last-label-pos (< last-label-pos start))
1105 ;; Might have jumped over several labels. Go to the last one.
1106 (setq pos last-label-pos)))))
1108 ;; Have we got "case <expression>:"?
1109 (goto-char pos)
1110 (when (and after-case:-pos
1111 (not (eq ret 'beginning))
1112 (looking-at c-case-kwds-regexp))
1113 (if (< after-case:-pos start)
1114 (setq pos after-case:-pos))
1115 (if (eq ret 'same)
1116 (setq ret 'label)))
1118 ;; Skip over the unary operators that can start the statement.
1119 (while (progn
1120 (c-backward-syntactic-ws)
1121 ;; protect AWK post-inc/decrement operators, etc.
1122 (and (not (c-at-vsemi-p (point)))
1123 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1124 (setq pos (point)))
1125 (goto-char pos)
1126 ret)))
1128 (defun c-punctuation-in (from to)
1129 "Return non-nil if there is a non-comment non-macro punctuation character
1130 between FROM and TO. FROM must not be in a string or comment. The returned
1131 value is the position of the first such character."
1132 (save-excursion
1133 (goto-char from)
1134 (let ((pos (point)))
1135 (while (progn (skip-chars-forward c-symbol-chars to)
1136 (c-forward-syntactic-ws to)
1137 (> (point) pos))
1138 (setq pos (point))))
1139 (and (< (point) to) (point))))
1141 (defun c-crosses-statement-barrier-p (from to)
1142 "Return non-nil if buffer positions FROM to TO cross one or more
1143 statement or declaration boundaries. The returned value is actually
1144 the position of the earliest boundary char. FROM must not be within
1145 a string or comment.
1147 The variable `c-maybe-labelp' is set to the position of the first `:' that
1148 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1149 single `?' is found, then `c-maybe-labelp' is cleared.
1151 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1152 regarded as having a \"virtual semicolon\" immediately after the last token on
1153 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1155 Note that this function might do hidden buffer changes. See the
1156 comment at the start of cc-engine.el for more info."
1157 (let* ((skip-chars
1158 ;; If the current language has CPP macros, insert # into skip-chars.
1159 (if c-opt-cpp-symbol
1160 (concat (substring c-stmt-delim-chars 0 1) ; "^"
1161 c-opt-cpp-symbol ; usually "#"
1162 (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
1163 c-stmt-delim-chars))
1164 (non-skip-list
1165 (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1166 lit-range vsemi-pos)
1167 (save-restriction
1168 (widen)
1169 (save-excursion
1170 (catch 'done
1171 (goto-char from)
1172 (while (progn (skip-chars-forward
1173 skip-chars
1174 (min to (c-point 'bonl)))
1175 (< (point) to))
1176 (cond
1177 ;; Virtual semicolon?
1178 ((and (bolp)
1179 (save-excursion
1180 (progn
1181 (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1182 (goto-char (car lit-range)))
1183 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1184 (setq vsemi-pos (point))
1185 (c-at-vsemi-p))))
1186 (throw 'done vsemi-pos))
1187 ;; In a string/comment?
1188 ((setq lit-range (c-literal-limits))
1189 (goto-char (cdr lit-range)))
1190 ((eq (char-after) ?:)
1191 (forward-char)
1192 (if (and (eq (char-after) ?:)
1193 (< (point) to))
1194 ;; Ignore scope operators.
1195 (forward-char)
1196 (setq c-maybe-labelp (1- (point)))))
1197 ((eq (char-after) ??)
1198 ;; A question mark. Can't be a label, so stop
1199 ;; looking for more : and ?.
1200 (setq c-maybe-labelp nil
1201 skip-chars (substring c-stmt-delim-chars 0 -2)))
1202 ;; At a CPP construct?
1203 ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol)
1204 (save-excursion
1205 (forward-line 0)
1206 (looking-at c-opt-cpp-prefix)))
1207 (c-end-of-macro))
1208 ((memq (char-after) non-skip-list)
1209 (throw 'done (point)))))
1210 ;; In trailing space after an as yet undetected virtual semicolon?
1211 (c-backward-syntactic-ws from)
1212 (if (and (< (point) to)
1213 (c-at-vsemi-p))
1214 (point)
1215 nil))))))
1217 (defun c-at-statement-start-p ()
1218 "Return non-nil if the point is at the first token in a statement
1219 or somewhere in the syntactic whitespace before it.
1221 A \"statement\" here is not restricted to those inside code blocks.
1222 Any kind of declaration-like construct that occur outside function
1223 bodies is also considered a \"statement\".
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-maybe-labelp)
1231 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1232 (or (bobp)
1233 (eq (char-before) ?})
1234 (and (eq (char-before) ?{)
1235 (not (and c-special-brace-lists
1236 (progn (backward-char)
1237 (c-looking-at-special-brace-list)))))
1238 (c-crosses-statement-barrier-p (point) end)))))
1240 (defun c-at-expression-start-p ()
1241 "Return non-nil if the point is at the first token in an expression or
1242 statement, or somewhere in the syntactic whitespace before it.
1244 An \"expression\" here is a bit different from the normal language
1245 grammar sense: It's any sequence of expression tokens except commas,
1246 unless they are enclosed inside parentheses of some kind. Also, an
1247 expression never continues past an enclosing parenthesis, but it might
1248 contain parenthesis pairs of any sort except braces.
1250 Since expressions never cross statement boundaries, this function also
1251 recognizes statement beginnings, just like `c-at-statement-start-p'.
1253 Note that this function might do hidden buffer changes. See the
1254 comment at the start of cc-engine.el for more info."
1256 (save-excursion
1257 (let ((end (point))
1258 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1259 c-maybe-labelp)
1260 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1261 (or (bobp)
1262 (memq (char-before) '(?{ ?}))
1263 (save-excursion (backward-char)
1264 (looking-at "\\s("))
1265 (c-crosses-statement-barrier-p (point) end)))))
1268 ;; A set of functions that covers various idiosyncrasies in
1269 ;; implementations of `forward-comment'.
1271 ;; Note: Some emacsen considers incorrectly that any line comment
1272 ;; ending with a backslash continues to the next line. I can't think
1273 ;; of any way to work around that in a reliable way without changing
1274 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1275 ;; changing the syntax for backslash doesn't work since we must treat
1276 ;; escapes in string literals correctly.)
1278 (defun c-forward-single-comment ()
1279 "Move forward past whitespace and the closest following comment, if any.
1280 Return t if a comment was found, nil otherwise. In either case, the
1281 point is moved past the following whitespace. Line continuations,
1282 i.e. a backslashes followed by line breaks, are treated as whitespace.
1283 The line breaks that end line comments are considered to be the
1284 comment enders, so the point will be put on the beginning of the next
1285 line if it moved past a line comment.
1287 This function does not do any hidden buffer changes."
1289 (let ((start (point)))
1290 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1291 (goto-char (match-end 0)))
1293 (when (forward-comment 1)
1294 (if (eobp)
1295 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1296 ;; forwards at eob.
1299 ;; Emacs includes the ending newline in a b-style (c++)
1300 ;; comment, but XEmacs doesn't. We depend on the Emacs
1301 ;; behavior (which also is symmetric).
1302 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1303 (condition-case nil (forward-char 1)))
1305 t))))
1307 (defsubst c-forward-comments ()
1308 "Move forward past all following whitespace and comments.
1309 Line continuations, i.e. a backslashes followed by line breaks, are
1310 treated as whitespace.
1312 Note that this function might do hidden buffer changes. See the
1313 comment at the start of cc-engine.el for more info."
1315 (while (or
1316 ;; If forward-comment in at least XEmacs 21 is given a large
1317 ;; positive value, it'll loop all the way through if it hits
1318 ;; eob.
1319 (and (forward-comment 5)
1320 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1321 ;; forwards at eob.
1322 (not (eobp)))
1324 (when (looking-at "\\\\[\n\r]")
1325 (forward-char 2)
1326 t))))
1328 (defun c-backward-single-comment ()
1329 "Move backward past whitespace and the closest preceding comment, if any.
1330 Return t if a comment was found, nil otherwise. In either case, the
1331 point is moved past the preceding whitespace. Line continuations,
1332 i.e. a backslashes followed by line breaks, are treated as whitespace.
1333 The line breaks that end line comments are considered to be the
1334 comment enders, so the point cannot be at the end of the same line to
1335 move over a line comment.
1337 This function does not do any hidden buffer changes."
1339 (let ((start (point)))
1340 ;; When we got newline terminated comments, forward-comment in all
1341 ;; supported emacsen so far will stop at eol of each line not
1342 ;; ending with a comment when moving backwards. This corrects for
1343 ;; that, and at the same time handles line continuations.
1344 (while (progn
1345 (skip-chars-backward " \t\n\r\f\v")
1346 (and (looking-at "[\n\r]")
1347 (eq (char-before) ?\\)))
1348 (backward-char))
1350 (if (bobp)
1351 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1352 ;; backwards at bob.
1355 ;; Leave point after the closest following newline if we've
1356 ;; backed up over any above, since forward-comment won't move
1357 ;; backward over a line comment if point is at the end of the
1358 ;; same line.
1359 (re-search-forward "\\=\\s *[\n\r]" start t)
1361 (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
1362 (if (eolp)
1363 ;; If forward-comment above succeeded and we're at eol
1364 ;; then the newline we moved over above didn't end a
1365 ;; line comment, so we give it another go.
1366 (let (open-paren-in-column-0-is-defun-start)
1367 (forward-comment -1))
1370 ;; Emacs <= 20 and XEmacs move back over the closer of a
1371 ;; block comment that lacks an opener.
1372 (if (looking-at "\\*/")
1373 (progn (forward-char 2) nil)
1374 t)))))
1376 (defsubst c-backward-comments ()
1377 "Move backward past all preceding whitespace and comments.
1378 Line continuations, i.e. a backslashes followed by line breaks, are
1379 treated as whitespace. The line breaks that end line comments are
1380 considered to be the comment enders, so the point cannot be at the end
1381 of the same line to move over a line comment. Unlike
1382 c-backward-syntactic-ws, this function doesn't move back over
1383 preprocessor directives.
1385 Note that this function might do hidden buffer changes. See the
1386 comment at the start of cc-engine.el for more info."
1388 (let ((start (point)))
1389 (while (and
1390 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1391 ;; return t when moving backwards at bob.
1392 (not (bobp))
1394 (if (let (open-paren-in-column-0-is-defun-start)
1395 (forward-comment -1))
1396 (if (looking-at "\\*/")
1397 ;; Emacs <= 20 and XEmacs move back over the
1398 ;; closer of a block comment that lacks an opener.
1399 (progn (forward-char 2) nil)
1402 ;; XEmacs treats line continuations as whitespace but
1403 ;; only in the backward direction, which seems a bit
1404 ;; odd. Anyway, this is necessary for Emacs.
1405 (when (and (looking-at "[\n\r]")
1406 (eq (char-before) ?\\)
1407 (< (point) start))
1408 (backward-char)
1409 t))))))
1412 ;; Tools for skipping over syntactic whitespace.
1414 ;; The following functions use text properties to cache searches over
1415 ;; large regions of syntactic whitespace. It works as follows:
1417 ;; o If a syntactic whitespace region contains anything but simple
1418 ;; whitespace (i.e. space, tab and line breaks), the text property
1419 ;; `c-in-sws' is put over it. At places where we have stopped
1420 ;; within that region there's also a `c-is-sws' text property.
1421 ;; That since there typically are nested whitespace inside that
1422 ;; must be handled separately, e.g. whitespace inside a comment or
1423 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1424 ;; to jump to another point with that property within the same
1425 ;; `c-in-sws' region. It can be likened to a ladder where
1426 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1428 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1429 ;; a "rung position" and also maybe on the first following char.
1430 ;; As many characters as can be conveniently found in this range
1431 ;; are marked, but no assumption can be made that the whole range
1432 ;; is marked (it could be clobbered by later changes, for
1433 ;; instance).
1435 ;; Note that some part of the beginning of a sequence of simple
1436 ;; whitespace might be part of the end of a preceding line comment
1437 ;; or cpp directive and must not be considered part of the "rung".
1438 ;; Such whitespace is some amount of horizontal whitespace followed
1439 ;; by a newline. In the case of cpp directives it could also be
1440 ;; two newlines with horizontal whitespace between them.
1442 ;; The reason to include the first following char is to cope with
1443 ;; "rung positions" that doesn't have any ordinary whitespace. If
1444 ;; `c-is-sws' is put on a token character it does not have
1445 ;; `c-in-sws' set simultaneously. That's the only case when that
1446 ;; can occur, and the reason for not extending the `c-in-sws'
1447 ;; region to cover it is that the `c-in-sws' region could then be
1448 ;; accidentally merged with a following one if the token is only
1449 ;; one character long.
1451 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1452 ;; removed in the changed region. If the change was inside
1453 ;; syntactic whitespace that means that the "ladder" is broken, but
1454 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1455 ;; parts on either side and use an ordinary search only to "repair"
1456 ;; the gap.
1458 ;; Special care needs to be taken if a region is removed: If there
1459 ;; are `c-in-sws' on both sides of it which do not connect inside
1460 ;; the region then they can't be joined. If e.g. a marked macro is
1461 ;; broken, syntactic whitespace inside the new text might be
1462 ;; marked. If those marks would become connected with the old
1463 ;; `c-in-sws' range around the macro then we could get a ladder
1464 ;; with one end outside the macro and the other at some whitespace
1465 ;; within it.
1467 ;; The main motivation for this system is to increase the speed in
1468 ;; skipping over the large whitespace regions that can occur at the
1469 ;; top level in e.g. header files that contain a lot of comments and
1470 ;; cpp directives. For small comments inside code it's probably
1471 ;; slower than using `forward-comment' straightforwardly, but speed is
1472 ;; not a significant factor there anyway.
1474 ; (defface c-debug-is-sws-face
1475 ; '((t (:background "GreenYellow")))
1476 ; "Debug face to mark the `c-is-sws' property.")
1477 ; (defface c-debug-in-sws-face
1478 ; '((t (:underline t)))
1479 ; "Debug face to mark the `c-in-sws' property.")
1481 ; (defun c-debug-put-sws-faces ()
1482 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1483 ; ;; properties in the buffer.
1484 ; (interactive)
1485 ; (save-excursion
1486 ; (c-save-buffer-state (in-face)
1487 ; (goto-char (point-min))
1488 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1489 ; (point)))
1490 ; (while (progn
1491 ; (goto-char (next-single-property-change
1492 ; (point) 'c-is-sws nil (point-max)))
1493 ; (if in-face
1494 ; (progn
1495 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1496 ; (setq in-face nil))
1497 ; (setq in-face (point)))
1498 ; (not (eobp))))
1499 ; (goto-char (point-min))
1500 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1501 ; (point)))
1502 ; (while (progn
1503 ; (goto-char (next-single-property-change
1504 ; (point) 'c-in-sws nil (point-max)))
1505 ; (if in-face
1506 ; (progn
1507 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1508 ; (setq in-face nil))
1509 ; (setq in-face (point)))
1510 ; (not (eobp)))))))
1512 (defmacro c-debug-sws-msg (&rest args)
1513 ;;`(message ,@args)
1516 (defmacro c-put-is-sws (beg end)
1517 ;; This macro does a hidden buffer change.
1518 `(let ((beg ,beg) (end ,end))
1519 (put-text-property beg end 'c-is-sws t)
1520 ,@(when (facep 'c-debug-is-sws-face)
1521 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1523 (defmacro c-put-in-sws (beg end)
1524 ;; This macro does a hidden buffer change.
1525 `(let ((beg ,beg) (end ,end))
1526 (put-text-property beg end 'c-in-sws t)
1527 ,@(when (facep 'c-debug-is-sws-face)
1528 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1530 (defmacro c-remove-is-sws (beg end)
1531 ;; This macro does a hidden buffer change.
1532 `(let ((beg ,beg) (end ,end))
1533 (remove-text-properties beg end '(c-is-sws nil))
1534 ,@(when (facep 'c-debug-is-sws-face)
1535 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1537 (defmacro c-remove-in-sws (beg end)
1538 ;; This macro does a hidden buffer change.
1539 `(let ((beg ,beg) (end ,end))
1540 (remove-text-properties beg end '(c-in-sws nil))
1541 ,@(when (facep 'c-debug-is-sws-face)
1542 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1544 (defmacro c-remove-is-and-in-sws (beg end)
1545 ;; This macro does a hidden buffer change.
1546 `(let ((beg ,beg) (end ,end))
1547 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1548 ,@(when (facep 'c-debug-is-sws-face)
1549 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1550 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1552 (defsubst c-invalidate-sws-region-after (beg end)
1553 ;; Called from `after-change-functions'. Note that if
1554 ;; `c-forward-sws' or `c-backward-sws' are used outside
1555 ;; `c-save-buffer-state' or similar then this will remove the cache
1556 ;; properties right after they're added.
1558 ;; This function does hidden buffer changes.
1560 (save-excursion
1561 ;; Adjust the end to remove the properties in any following simple
1562 ;; ws up to and including the next line break, if there is any
1563 ;; after the changed region. This is necessary e.g. when a rung
1564 ;; marked empty line is converted to a line comment by inserting
1565 ;; "//" before the line break. In that case the line break would
1566 ;; keep the rung mark which could make a later `c-backward-sws'
1567 ;; move into the line comment instead of over it.
1568 (goto-char end)
1569 (skip-chars-forward " \t\f\v")
1570 (when (and (eolp) (not (eobp)))
1571 (setq end (1+ (point)))))
1573 (when (and (= beg end)
1574 (get-text-property beg 'c-in-sws)
1575 (> beg (point-min))
1576 (get-text-property (1- beg) 'c-in-sws))
1577 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1578 ;; safe to keep a range that was continuous before the change. E.g:
1580 ;; #define foo
1581 ;; \
1582 ;; bar
1584 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1585 ;; after "foo" is removed then "bar" will become part of the cpp
1586 ;; directive instead of a syntactically relevant token. In that
1587 ;; case there's no longer syntactic ws from "#" to "b".
1588 (setq beg (1- beg)))
1590 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1591 (c-remove-is-and-in-sws beg end))
1593 (defun c-forward-sws ()
1594 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1596 ;; This function might do hidden buffer changes.
1598 (let (;; `rung-pos' is set to a position as early as possible in the
1599 ;; unmarked part of the simple ws region.
1600 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1601 rung-is-marked next-rung-is-marked simple-ws-end
1602 ;; `safe-start' is set when it's safe to cache the start position.
1603 ;; It's not set if we've initially skipped over comments and line
1604 ;; continuations since we might have gone out through the end of a
1605 ;; macro then. This provision makes `c-forward-sws' not populate the
1606 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1607 ;; more common.
1608 safe-start)
1610 ;; Skip simple ws and do a quick check on the following character to see
1611 ;; if it's anything that can't start syntactic ws, so we can bail out
1612 ;; early in the majority of cases when there just are a few ws chars.
1613 (skip-chars-forward " \t\n\r\f\v")
1614 (when (looking-at c-syntactic-ws-start)
1616 (setq rung-end-pos (min (1+ (point)) (point-max)))
1617 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1618 'c-is-sws t))
1619 ;; Find the last rung position to avoid setting properties in all
1620 ;; the cases when the marked rung is complete.
1621 ;; (`next-single-property-change' is certain to move at least one
1622 ;; step forward.)
1623 (setq rung-pos (1- (next-single-property-change
1624 rung-is-marked 'c-is-sws nil rung-end-pos)))
1625 ;; Got no marked rung here. Since the simple ws might have started
1626 ;; inside a line comment or cpp directive we must set `rung-pos' as
1627 ;; high as possible.
1628 (setq rung-pos (point)))
1630 (while
1631 (progn
1632 (while
1633 (when (and rung-is-marked
1634 (get-text-property (point) 'c-in-sws))
1636 ;; The following search is the main reason that `c-in-sws'
1637 ;; and `c-is-sws' aren't combined to one property.
1638 (goto-char (next-single-property-change
1639 (point) 'c-in-sws nil (point-max)))
1640 (unless (get-text-property (point) 'c-is-sws)
1641 ;; If the `c-in-sws' region extended past the last
1642 ;; `c-is-sws' char we have to go back a bit.
1643 (or (get-text-property (1- (point)) 'c-is-sws)
1644 (goto-char (previous-single-property-change
1645 (point) 'c-is-sws)))
1646 (backward-char))
1648 (c-debug-sws-msg
1649 "c-forward-sws cached move %s -> %s (max %s)"
1650 rung-pos (point) (point-max))
1652 (setq rung-pos (point))
1653 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1654 (not (eobp))))
1656 ;; We'll loop here if there is simple ws after the last rung.
1657 ;; That means that there's been some change in it and it's
1658 ;; possible that we've stepped into another ladder, so extend
1659 ;; the previous one to join with it if there is one, and try to
1660 ;; use the cache again.
1661 (c-debug-sws-msg
1662 "c-forward-sws extending rung with [%s..%s] (max %s)"
1663 (1+ rung-pos) (1+ (point)) (point-max))
1664 (unless (get-text-property (point) 'c-is-sws)
1665 ;; Remove any `c-in-sws' property from the last char of
1666 ;; the rung before we mark it with `c-is-sws', so that we
1667 ;; won't connect with the remains of a broken "ladder".
1668 (c-remove-in-sws (point) (1+ (point))))
1669 (c-put-is-sws (1+ rung-pos)
1670 (1+ (point)))
1671 (c-put-in-sws rung-pos
1672 (setq rung-pos (point)
1673 last-put-in-sws-pos rung-pos)))
1675 (setq simple-ws-end (point))
1676 (c-forward-comments)
1678 (cond
1679 ((/= (point) simple-ws-end)
1680 ;; Skipped over comments. Don't cache at eob in case the buffer
1681 ;; is narrowed.
1682 (not (eobp)))
1684 ((save-excursion
1685 (and c-opt-cpp-prefix
1686 (looking-at c-opt-cpp-start)
1687 (progn (skip-chars-backward " \t")
1688 (bolp))
1689 (or (bobp)
1690 (progn (backward-char)
1691 (not (eq (char-before) ?\\))))))
1692 ;; Skip a preprocessor directive.
1693 (end-of-line)
1694 (while (and (eq (char-before) ?\\)
1695 (= (forward-line 1) 0))
1696 (end-of-line))
1697 (forward-line 1)
1698 (setq safe-start t)
1699 ;; Don't cache at eob in case the buffer is narrowed.
1700 (not (eobp)))))
1702 ;; We've searched over a piece of non-white syntactic ws. See if this
1703 ;; can be cached.
1704 (setq next-rung-pos (point))
1705 (skip-chars-forward " \t\n\r\f\v")
1706 (setq rung-end-pos (min (1+ (point)) (point-max)))
1708 (if (or
1709 ;; Cache if we haven't skipped comments only, and if we started
1710 ;; either from a marked rung or from a completely uncached
1711 ;; position.
1712 (and safe-start
1713 (or rung-is-marked
1714 (not (get-text-property simple-ws-end 'c-in-sws))))
1716 ;; See if there's a marked rung in the encountered simple ws. If
1717 ;; so then we can cache, unless `safe-start' is nil. Even then
1718 ;; we need to do this to check if the cache can be used for the
1719 ;; next step.
1720 (and (setq next-rung-is-marked
1721 (text-property-any next-rung-pos rung-end-pos
1722 'c-is-sws t))
1723 safe-start))
1725 (progn
1726 (c-debug-sws-msg
1727 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1728 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1729 (point-max))
1731 ;; Remove the properties for any nested ws that might be cached.
1732 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1733 ;; anyway.
1734 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1735 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1736 (c-put-is-sws rung-pos
1737 (1+ simple-ws-end))
1738 (setq rung-is-marked t))
1739 (c-put-in-sws rung-pos
1740 (setq rung-pos (point)
1741 last-put-in-sws-pos rung-pos))
1742 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1743 ;; Remove any `c-in-sws' property from the last char of
1744 ;; the rung before we mark it with `c-is-sws', so that we
1745 ;; won't connect with the remains of a broken "ladder".
1746 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1747 (c-put-is-sws next-rung-pos
1748 rung-end-pos))
1750 (c-debug-sws-msg
1751 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1752 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1753 (point-max))
1755 ;; Set `rung-pos' for the next rung. It's the same thing here as
1756 ;; initially, except that the rung position is set as early as
1757 ;; possible since we can't be in the ending ws of a line comment or
1758 ;; cpp directive now.
1759 (if (setq rung-is-marked next-rung-is-marked)
1760 (setq rung-pos (1- (next-single-property-change
1761 rung-is-marked 'c-is-sws nil rung-end-pos)))
1762 (setq rung-pos next-rung-pos))
1763 (setq safe-start t)))
1765 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1766 ;; another one after the point (which might occur when editing inside a
1767 ;; comment or macro).
1768 (when (eq last-put-in-sws-pos (point))
1769 (cond ((< last-put-in-sws-pos (point-max))
1770 (c-debug-sws-msg
1771 "c-forward-sws clearing at %s for cache separation"
1772 last-put-in-sws-pos)
1773 (c-remove-in-sws last-put-in-sws-pos
1774 (1+ last-put-in-sws-pos)))
1776 ;; If at eob we have to clear the last character before the end
1777 ;; instead since the buffer might be narrowed and there might
1778 ;; be a `c-in-sws' after (point-max). In this case it's
1779 ;; necessary to clear both properties.
1780 (c-debug-sws-msg
1781 "c-forward-sws clearing thoroughly at %s for cache separation"
1782 (1- last-put-in-sws-pos))
1783 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1784 last-put-in-sws-pos))))
1787 (defun c-backward-sws ()
1788 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1790 ;; This function might do hidden buffer changes.
1792 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1793 ;; part of the simple ws region.
1794 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1795 rung-is-marked simple-ws-beg cmt-skip-pos)
1797 ;; Skip simple horizontal ws and do a quick check on the preceding
1798 ;; character to see if it's anything that can't end syntactic ws, so we can
1799 ;; bail out early in the majority of cases when there just are a few ws
1800 ;; chars. Newlines are complicated in the backward direction, so we can't
1801 ;; skip over them.
1802 (skip-chars-backward " \t\f")
1803 (when (and (not (bobp))
1804 (save-excursion
1805 (backward-char)
1806 (looking-at c-syntactic-ws-end)))
1808 ;; Try to find a rung position in the simple ws preceding point, so that
1809 ;; we can get a cache hit even if the last bit of the simple ws has
1810 ;; changed recently.
1811 (setq simple-ws-beg (point))
1812 (skip-chars-backward " \t\n\r\f\v")
1813 (if (setq rung-is-marked (text-property-any
1814 (point) (min (1+ rung-pos) (point-max))
1815 'c-is-sws t))
1816 ;; `rung-pos' will be the earliest marked position, which means that
1817 ;; there might be later unmarked parts in the simple ws region.
1818 ;; It's not worth the effort to fix that; the last part of the
1819 ;; simple ws is also typically edited often, so it could be wasted.
1820 (goto-char (setq rung-pos rung-is-marked))
1821 (goto-char simple-ws-beg))
1823 (while
1824 (progn
1825 (while
1826 (when (and rung-is-marked
1827 (not (bobp))
1828 (get-text-property (1- (point)) 'c-in-sws))
1830 ;; The following search is the main reason that `c-in-sws'
1831 ;; and `c-is-sws' aren't combined to one property.
1832 (goto-char (previous-single-property-change
1833 (point) 'c-in-sws nil (point-min)))
1834 (unless (get-text-property (point) 'c-is-sws)
1835 ;; If the `c-in-sws' region extended past the first
1836 ;; `c-is-sws' char we have to go forward a bit.
1837 (goto-char (next-single-property-change
1838 (point) 'c-is-sws)))
1840 (c-debug-sws-msg
1841 "c-backward-sws cached move %s <- %s (min %s)"
1842 (point) rung-pos (point-min))
1844 (setq rung-pos (point))
1845 (if (and (< (min (skip-chars-backward " \t\f\v")
1846 (progn
1847 (setq simple-ws-beg (point))
1848 (skip-chars-backward " \t\n\r\f\v")))
1850 (setq rung-is-marked
1851 (text-property-any (point) rung-pos
1852 'c-is-sws t)))
1854 (goto-char simple-ws-beg)
1855 nil))
1857 ;; We'll loop here if there is simple ws before the first rung.
1858 ;; That means that there's been some change in it and it's
1859 ;; possible that we've stepped into another ladder, so extend
1860 ;; the previous one to join with it if there is one, and try to
1861 ;; use the cache again.
1862 (c-debug-sws-msg
1863 "c-backward-sws extending rung with [%s..%s] (min %s)"
1864 rung-is-marked rung-pos (point-min))
1865 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1866 ;; Remove any `c-in-sws' property from the last char of
1867 ;; the rung before we mark it with `c-is-sws', so that we
1868 ;; won't connect with the remains of a broken "ladder".
1869 (c-remove-in-sws (1- rung-pos) rung-pos))
1870 (c-put-is-sws rung-is-marked
1871 rung-pos)
1872 (c-put-in-sws rung-is-marked
1873 (1- rung-pos))
1874 (setq rung-pos rung-is-marked
1875 last-put-in-sws-pos rung-pos))
1877 (c-backward-comments)
1878 (setq cmt-skip-pos (point))
1880 (cond
1881 ((and c-opt-cpp-prefix
1882 (/= cmt-skip-pos simple-ws-beg)
1883 (c-beginning-of-macro))
1884 ;; Inside a cpp directive. See if it should be skipped over.
1885 (let ((cpp-beg (point)))
1887 ;; Move back over all line continuations in the region skipped
1888 ;; over by `c-backward-comments'. If we go past it then we
1889 ;; started inside the cpp directive.
1890 (goto-char simple-ws-beg)
1891 (beginning-of-line)
1892 (while (and (> (point) cmt-skip-pos)
1893 (progn (backward-char)
1894 (eq (char-before) ?\\)))
1895 (beginning-of-line))
1897 (if (< (point) cmt-skip-pos)
1898 ;; Don't move past the cpp directive if we began inside
1899 ;; it. Note that the position at the end of the last line
1900 ;; of the macro is also considered to be within it.
1901 (progn (goto-char cmt-skip-pos)
1902 nil)
1904 ;; It's worthwhile to spend a little bit of effort on finding
1905 ;; the end of the macro, to get a good `simple-ws-beg'
1906 ;; position for the cache. Note that `c-backward-comments'
1907 ;; could have stepped over some comments before going into
1908 ;; the macro, and then `simple-ws-beg' must be kept on the
1909 ;; same side of those comments.
1910 (goto-char simple-ws-beg)
1911 (skip-chars-backward " \t\n\r\f\v")
1912 (if (eq (char-before) ?\\)
1913 (forward-char))
1914 (forward-line 1)
1915 (if (< (point) simple-ws-beg)
1916 ;; Might happen if comments after the macro were skipped
1917 ;; over.
1918 (setq simple-ws-beg (point)))
1920 (goto-char cpp-beg)
1921 t)))
1923 ((/= (save-excursion
1924 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1925 (setq next-rung-pos (point)))
1926 simple-ws-beg)
1927 ;; Skipped over comments. Must put point at the end of
1928 ;; the simple ws at point since we might be after a line
1929 ;; comment or cpp directive that's been partially
1930 ;; narrowed out, and we can't risk marking the simple ws
1931 ;; at the end of it.
1932 (goto-char next-rung-pos)
1933 t)))
1935 ;; We've searched over a piece of non-white syntactic ws. See if this
1936 ;; can be cached.
1937 (setq next-rung-pos (point))
1938 (skip-chars-backward " \t\f\v")
1940 (if (or
1941 ;; Cache if we started either from a marked rung or from a
1942 ;; completely uncached position.
1943 rung-is-marked
1944 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1946 ;; Cache if there's a marked rung in the encountered simple ws.
1947 (save-excursion
1948 (skip-chars-backward " \t\n\r\f\v")
1949 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1950 'c-is-sws t)))
1952 (progn
1953 (c-debug-sws-msg
1954 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1955 (point) (1+ next-rung-pos)
1956 simple-ws-beg (min (1+ rung-pos) (point-max))
1957 (point-min))
1959 ;; Remove the properties for any nested ws that might be cached.
1960 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1961 ;; anyway.
1962 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1963 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1964 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1965 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1966 ;; Remove any `c-in-sws' property from the last char of
1967 ;; the rung before we mark it with `c-is-sws', so that we
1968 ;; won't connect with the remains of a broken "ladder".
1969 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1970 (c-put-is-sws simple-ws-beg
1971 rung-end-pos)
1972 (setq rung-is-marked t)))
1973 (c-put-in-sws (setq simple-ws-beg (point)
1974 last-put-in-sws-pos simple-ws-beg)
1975 rung-pos)
1976 (c-put-is-sws (setq rung-pos simple-ws-beg)
1977 (1+ next-rung-pos)))
1979 (c-debug-sws-msg
1980 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1981 (point) (1+ next-rung-pos)
1982 simple-ws-beg (min (1+ rung-pos) (point-max))
1983 (point-min))
1984 (setq rung-pos next-rung-pos
1985 simple-ws-beg (point))
1988 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1989 ;; another one before the point (which might occur when editing inside a
1990 ;; comment or macro).
1991 (when (eq last-put-in-sws-pos (point))
1992 (cond ((< (point-min) last-put-in-sws-pos)
1993 (c-debug-sws-msg
1994 "c-backward-sws clearing at %s for cache separation"
1995 (1- last-put-in-sws-pos))
1996 (c-remove-in-sws (1- last-put-in-sws-pos)
1997 last-put-in-sws-pos))
1998 ((> (point-min) 1)
1999 ;; If at bob and the buffer is narrowed, we have to clear the
2000 ;; character we're standing on instead since there might be a
2001 ;; `c-in-sws' before (point-min). In this case it's necessary
2002 ;; to clear both properties.
2003 (c-debug-sws-msg
2004 "c-backward-sws clearing thoroughly at %s for cache separation"
2005 last-put-in-sws-pos)
2006 (c-remove-is-and-in-sws last-put-in-sws-pos
2007 (1+ last-put-in-sws-pos)))))
2011 ;; Other whitespace tools
2012 (defun c-partial-ws-p (beg end)
2013 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2014 ;; region? This is a "heuristic" function. .....
2016 ;; The motivation for the second bit is to check whether removing this
2017 ;; region would coalesce two symbols.
2019 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2020 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2021 (save-excursion
2022 (let ((end+1 (min (1+ end) (point-max))))
2023 (or (progn (goto-char (max (point-min) (1- beg)))
2024 (c-skip-ws-forward end)
2025 (eq (point) end))
2026 (progn (goto-char beg)
2027 (c-skip-ws-forward end+1)
2028 (eq (point) end+1))))))
2030 ;; A system for finding noteworthy parens before the point.
2032 (defconst c-state-cache-too-far 5000)
2033 ;; A maximum comfortable scanning distance, e.g. between
2034 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2035 ;; this distance is exceeded, we take "emergency meausures", e.g. by clearing
2036 ;; the cache and starting again from point-min or a beginning of defun. This
2037 ;; value can be tuned for efficiency or set to a lower value for testing.
2039 (defvar c-state-cache nil)
2040 (make-variable-buffer-local 'c-state-cache)
2041 ;; The state cache used by `c-parse-state' to cut down the amount of
2042 ;; searching. It's the result from some earlier `c-parse-state' call. See
2043 ;; `c-parse-state''s doc string for details of its structure.
2045 ;; The use of the cached info is more effective if the next
2046 ;; `c-parse-state' call is on a line close by the one the cached state
2047 ;; was made at; the cache can actually slow down a little if the
2048 ;; cached state was made very far back in the buffer. The cache is
2049 ;; most effective if `c-parse-state' is used on each line while moving
2050 ;; forward.
2052 (defvar c-state-cache-good-pos 1)
2053 (make-variable-buffer-local 'c-state-cache-good-pos)
2054 ;; This is a position where `c-state-cache' is known to be correct, or
2055 ;; nil (see below). It's a position inside one of the recorded unclosed
2056 ;; parens or the top level, but not further nested inside any literal or
2057 ;; subparen that is closed before the last recorded position.
2059 ;; The exact position is chosen to try to be close to yet earlier than
2060 ;; the position where `c-state-cache' will be called next. Right now
2061 ;; the heuristic is to set it to the position after the last found
2062 ;; closing paren (of any type) before the line on which
2063 ;; `c-parse-state' was called. That is chosen primarily to work well
2064 ;; with refontification of the current line.
2066 ;; 2009-07-28: When `c-state-point-min' and the last position where
2067 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2068 ;; both in the same literal, there is no such "good position", and
2069 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2070 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2072 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2073 ;; the middle of the desert, as long as it is not within a brace pair
2074 ;; recorded in `c-state-cache' or a paren/bracket pair.
2077 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2078 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2079 ;; speed up testing for non-literality.
2080 (defconst c-state-nonlit-pos-interval 10000)
2081 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2083 (defvar c-state-nonlit-pos-cache nil)
2084 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2085 ;; A list of buffer positions which are known not to be in a literal or a cpp
2086 ;; construct. This is ordered with higher positions at the front of the list.
2087 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2089 (defvar c-state-nonlit-pos-cache-limit 1)
2090 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2091 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2092 ;; reduced by buffer changes, and increased by invocations of
2093 ;; `c-state-literal-at'.
2095 (defsubst c-state-pp-to-literal (from to)
2096 ;; Do a parse-partial-sexp from FROM to TO, returning the bounds of any
2097 ;; literal at TO as a cons, otherwise NIL.
2098 ;; FROM must not be in a literal, and the buffer should already be wide
2099 ;; enough.
2100 (save-excursion
2101 (let ((s (parse-partial-sexp from to)))
2102 (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
2103 (parse-partial-sexp (point) (point-max)
2104 nil ; TARGETDEPTH
2105 nil ; STOPBEFORE
2106 s ; OLDSTATE
2107 'syntax-table) ; stop at end of literal
2108 (cons (nth 8 s) (point))))))
2110 (defun c-state-literal-at (here)
2111 ;; If position HERE is inside a literal, return (START . END), the
2112 ;; boundaries of the literal (which may be outside the accessible bit of the
2113 ;; buffer). Otherwise, return nil.
2115 ;; This function is almost the same as `c-literal-limits'. It differs in
2116 ;; that it is a lower level function, and that it rigourously follows the
2117 ;; syntax from BOB, whereas `c-literal-limits' uses a "local" safe position.
2119 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2120 ;; MAY NOT contain any positions within macros, since macros are frequently
2121 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2122 ;; We cannot rely on this mechanism whilst determining a cache pos since
2123 ;; this function is also called from outwith `c-parse-state'.
2124 (save-restriction
2125 (widen)
2126 (save-excursion
2127 (let ((c c-state-nonlit-pos-cache)
2128 pos npos lit)
2129 ;; Trim the cache to take account of buffer changes.
2130 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2131 (setq c (cdr c)))
2132 (setq c-state-nonlit-pos-cache c)
2134 (while (and c (> (car c) here))
2135 (setq c (cdr c)))
2136 (setq pos (or (car c) (point-min)))
2138 (while (<= (setq npos (+ pos c-state-nonlit-pos-interval))
2139 here)
2140 (setq lit (c-state-pp-to-literal pos npos))
2141 (setq pos (or (cdr lit) npos)) ; end of literal containing npos.
2142 (goto-char pos)
2143 (when (and (c-beginning-of-macro) (/= (point) pos))
2144 (c-syntactic-end-of-macro)
2145 (or (eobp) (forward-char))
2146 (setq pos (point)))
2147 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2149 (if (> pos c-state-nonlit-pos-cache-limit)
2150 (setq c-state-nonlit-pos-cache-limit pos))
2151 (if (< pos here)
2152 (setq lit (c-state-pp-to-literal pos here)))
2153 lit))))
2155 (defsubst c-state-lit-beg (pos)
2156 ;; Return the start of the literal containing POS, or POS itself.
2157 (or (car (c-state-literal-at pos))
2158 pos))
2160 (defsubst c-state-cache-non-literal-place (pos state)
2161 ;; Return a position outside of a string/comment/macro at or before POS.
2162 ;; STATE is the parse-partial-sexp state at POS.
2163 (let ((res (if (or (nth 3 state) ; in a string?
2164 (nth 4 state)) ; in a comment?
2165 (nth 8 state)
2166 pos)))
2167 (save-excursion
2168 (goto-char res)
2169 (if (c-beginning-of-macro)
2170 (point)
2171 res))))
2173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2174 ;; Stuff to do with point-min, and coping with any literal there.
2175 (defvar c-state-point-min 1)
2176 (make-variable-buffer-local 'c-state-point-min)
2177 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2178 ;; narrowing is likely to affect the parens that are visible before the point.
2180 (defvar c-state-point-min-lit-type nil)
2181 (make-variable-buffer-local 'c-state-point-min-lit-type)
2182 (defvar c-state-point-min-lit-start nil)
2183 (make-variable-buffer-local 'c-state-point-min-lit-start)
2184 ;; These two variables define the literal, if any, containing point-min.
2185 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2186 ;; literal. If there's no literal there, they're both nil.
2188 (defvar c-state-min-scan-pos 1)
2189 (make-variable-buffer-local 'c-state-min-scan-pos)
2190 ;; This is the earliest buffer-pos from which scanning can be done. It is
2191 ;; either the end of the literal containing point-min, or point-min itself.
2192 ;; It becomes nil if the buffer is changed earlier than this point.
2193 (defun c-state-get-min-scan-pos ()
2194 ;; Return the lowest valid scanning pos. This will be the end of the
2195 ;; literal enclosing point-min, or point-min itself.
2196 (or c-state-min-scan-pos
2197 (save-restriction
2198 (save-excursion
2199 (widen)
2200 (goto-char c-state-point-min-lit-start)
2201 (if (eq c-state-point-min-lit-type 'string)
2202 (forward-sexp)
2203 (forward-comment 1))
2204 (setq c-state-min-scan-pos (point))))))
2206 (defun c-state-mark-point-min-literal ()
2207 ;; Determine the properties of any literal containing POINT-MIN, setting the
2208 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2209 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2210 (let ((p-min (point-min))
2211 lit)
2212 (save-restriction
2213 (widen)
2214 (setq lit (c-state-literal-at p-min))
2215 (if lit
2216 (setq c-state-point-min-lit-type
2217 (save-excursion
2218 (goto-char (car lit))
2219 (cond
2220 ((looking-at c-block-comment-start-regexp) 'c)
2221 ((looking-at c-line-comment-starter) 'c++)
2222 (t 'string)))
2223 c-state-point-min-lit-start (car lit)
2224 c-state-min-scan-pos (cdr lit))
2225 (setq c-state-point-min-lit-type nil
2226 c-state-point-min-lit-start nil
2227 c-state-min-scan-pos p-min)))))
2230 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2231 ;; A variable which signals a brace dessert - helpful for reducing the number
2232 ;; of fruitless backward scans.
2233 (defvar c-state-brace-pair-desert nil)
2234 (make-variable-buffer-local 'c-state-brace-pair-desert)
2235 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2236 ;; that defun has searched backwards for a brace pair and not found one. Its
2237 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2238 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2239 ;; nil when at top level) and FROM is where the backward search started. It
2240 ;; is reset to nil in `c-invalidate-state-cache'.
2243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2244 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2245 ;; list of like structure.
2246 (defmacro c-state-cache-top-lparen (&optional cache)
2247 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2248 ;; (default `c-state-cache') (or nil).
2249 (let ((cash (or cache 'c-state-cache)))
2250 `(if (consp (car ,cash))
2251 (caar ,cash)
2252 (car ,cash))))
2254 (defmacro c-state-cache-top-paren (&optional cache)
2255 ;; Return the address of the latest brace/bracket/paren (whether left or
2256 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2257 (let ((cash (or cache 'c-state-cache)))
2258 `(if (consp (car ,cash))
2259 (cdar ,cash)
2260 (car ,cash))))
2262 (defmacro c-state-cache-after-top-paren (&optional cache)
2263 ;; Return the position just after the latest brace/bracket/paren (whether
2264 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2265 (let ((cash (or cache 'c-state-cache)))
2266 `(if (consp (car ,cash))
2267 (cdar ,cash)
2268 (and (car ,cash)
2269 (1+ (car ,cash))))))
2271 (defun c-get-cache-scan-pos (here)
2272 ;; From the state-cache, determine the buffer position from which we might
2273 ;; scan forward to HERE to update this cache. This position will be just
2274 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2275 ;; return the earliest position in the accessible region which isn't within
2276 ;; a literal. If the visible portion of the buffer is entirely within a
2277 ;; literal, return NIL.
2278 (let ((c c-state-cache) elt)
2279 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2280 (while (and c
2281 (>= (c-state-cache-top-lparen c) here))
2282 (setq c (cdr c)))
2284 (setq elt (car c))
2285 (cond
2286 ((consp elt)
2287 (if (> (cdr elt) here)
2288 (1+ (car elt))
2289 (cdr elt)))
2290 (elt (1+ elt))
2291 ((<= (c-state-get-min-scan-pos) here)
2292 (c-state-get-min-scan-pos))
2293 (t nil))))
2295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2296 ;; Variables which keep track of preprocessor constructs.
2297 (defvar c-state-old-cpp-beg nil)
2298 (make-variable-buffer-local 'c-state-old-cpp-beg)
2299 (defvar c-state-old-cpp-end nil)
2300 (make-variable-buffer-local 'c-state-old-cpp-end)
2301 ;; These are the limits of the macro containing point at the previous call of
2302 ;; `c-parse-state', or nil.
2304 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2305 ;; Defuns which analyse the buffer, yet don't change `c-state-cache'.
2306 (defun c-get-fallback-scan-pos (here)
2307 ;; Return a start position for building `c-state-cache' from
2308 ;; scratch. This will be at the top level, 2 defuns back.
2309 (save-excursion
2310 ;; Go back 2 bods, but ignore any bogus positions returned by
2311 ;; beginning-of-defun (i.e. open paren in column zero).
2312 (goto-char here)
2313 (let ((cnt 2))
2314 (while (not (or (bobp) (zerop cnt)))
2315 (c-beginning-of-defun-1) ; Pure elisp BOD.
2316 (if (eq (char-after) ?\{)
2317 (setq cnt (1- cnt)))))
2318 (point)))
2320 (defun c-state-balance-parens-backwards (here- here+ top)
2321 ;; Return the position of the opening paren/brace/bracket before HERE- which
2322 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2323 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2325 ;; ............................................
2326 ;; | |
2327 ;; ( [ ( .........#macro.. ) ( ) ] )
2328 ;; ^ ^ ^ ^
2329 ;; | | | |
2330 ;; return HERE- HERE+ TOP
2332 ;; If there aren't enough opening paren/brace/brackets, return the position
2333 ;; of the outermost one found, or HERE- if there are none. If there are no
2334 ;; closeing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2335 ;; must not be inside literals. Only the accessible portion of the buffer
2336 ;; will be scanned.
2338 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2339 ;; `here'. Go round the next loop each time we pass over such a ")". These
2340 ;; probably match "("s before `here-'.
2341 (let (pos pa ren+1 lonely-rens)
2342 (save-excursion
2343 (save-restriction
2344 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2345 (setq pos here+)
2346 (c-safe
2347 (while
2348 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2349 (setq lonely-rens (cons ren+1 lonely-rens)
2350 pos ren+1)))))
2352 ;; PART 2: Scan back before `here-' searching for the "("s
2353 ;; matching/mismatching the ")"s found above. We only need to direct the
2354 ;; caller to scan when we've encountered unmatched right parens.
2355 (setq pos here-)
2356 (when lonely-rens
2357 (c-safe
2358 (while
2359 (and lonely-rens ; actual values aren't used.
2360 (setq pa (scan-lists pos -1 1)))
2361 (setq pos pa)
2362 (setq lonely-rens (cdr lonely-rens)))))
2363 pos))
2365 (defun c-parse-state-get-strategy (here good-pos)
2366 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2367 ;; to minimise the amount of scanning. HERE is the pertinent position in
2368 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2369 ;; its head trimmed) is known to be good, or nil if there is no such
2370 ;; position.
2372 ;; The return value is a list, one of the following:
2374 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2375 ;; which is not less than CACHE-POS.
2376 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2377 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2378 ;; top level.
2379 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2380 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2381 ;; or below HERE.
2382 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2383 BOD-pos ; position of 2nd BOD before HERE.
2384 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2385 start-point
2386 how-far) ; putative scanning distance.
2387 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2388 (cond
2389 ((< here (c-state-get-min-scan-pos))
2390 (setq strategy 'IN-LIT
2391 start-point nil
2392 cache-pos nil
2393 how-far 0))
2394 ((<= good-pos here)
2395 (setq strategy 'forward
2396 start-point (max good-pos cache-pos)
2397 how-far (- here start-point)))
2398 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2399 (setq strategy 'backward
2400 how-far (- good-pos here)))
2402 (setq strategy 'forward
2403 how-far (- here cache-pos)
2404 start-point cache-pos)))
2406 ;; Might we be better off starting from the top level, two defuns back,
2407 ;; instead?
2408 (when (> how-far c-state-cache-too-far)
2409 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2410 (if (< (- here BOD-pos) how-far)
2411 (setq strategy 'BOD
2412 start-point BOD-pos)))
2414 (list
2415 strategy
2416 (and (memq strategy '(forward backward)) cache-pos)
2417 (and (memq strategy '(forward BOD)) start-point))))
2420 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2421 ;; Routines which change `c-state-cache' and associated values.
2422 (defun c-renarrow-state-cache ()
2423 ;; The region (more precisely, point-min) has changed since we
2424 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2425 (if (< (point-min) c-state-point-min)
2426 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2427 ;; It would be possible to do a better job here and recalculate the top
2428 ;; only.
2429 (progn
2430 (c-state-mark-point-min-literal)
2431 (setq c-state-cache nil
2432 c-state-cache-good-pos c-state-min-scan-pos
2433 c-state-brace-pair-desert nil))
2435 ;; point-min has MOVED FORWARD.
2437 ;; Is the new point-min inside a (different) literal?
2438 (unless (and c-state-point-min-lit-start ; at prev. point-min
2439 (< (point-min) (c-state-get-min-scan-pos)))
2440 (c-state-mark-point-min-literal))
2442 ;; Cut off a bit of the tail from `c-state-cache'.
2443 (let ((ptr (cons nil c-state-cache))
2445 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2446 (>= pa (point-min)))
2447 (setq ptr (cdr ptr)))
2449 (when (consp ptr)
2450 (if (eq (cdr ptr) c-state-cache)
2451 (setq c-state-cache nil
2452 c-state-cache-good-pos c-state-min-scan-pos)
2453 (setcdr ptr nil)
2454 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2457 (setq c-state-point-min (point-min)))
2459 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2460 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2461 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2462 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2463 ;; the highest brace pair whose "}" is below UPPER-LIM.
2465 ;; Return non-nil when this has been done.
2467 ;; This routine should be fast. Since it can get called a LOT, we maintain
2468 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2469 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2470 (save-excursion
2471 (save-restriction
2472 (let ((bra from) ce ; Positions of "{" and "}".
2473 new-cons
2474 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2475 (macro-start-or-from
2476 (progn (goto-char from)
2477 (c-beginning-of-macro)
2478 (point))))
2479 (or upper-lim (setq upper-lim from))
2481 ;; If we're essentially repeating a fruitless search, just give up.
2482 (unless (and c-state-brace-pair-desert
2483 (eq cache-pos (car c-state-brace-pair-desert))
2484 (<= from (cdr c-state-brace-pair-desert)))
2485 ;; Only search what we absolutely need to:
2486 (if (and c-state-brace-pair-desert
2487 (eq cache-pos (car c-state-brace-pair-desert)))
2488 (narrow-to-region (cdr c-state-brace-pair-desert) (point-max)))
2490 ;; In the next pair of nested loops, the inner one moves back past a
2491 ;; pair of (mis-)matching parens or brackets; the outer one moves
2492 ;; back over a sequence of unmatched close brace/paren/bracket each
2493 ;; time round.
2494 (while
2495 (progn
2496 (c-safe
2497 (while
2498 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2499 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2500 (or (> ce upper-lim)
2501 (not (eq (char-after bra) ?\{))
2502 (and (goto-char bra)
2503 (c-beginning-of-macro)
2504 (< (point) macro-start-or-from))))))
2505 (and ce (< ce bra)))
2506 (setq bra ce)) ; If we just backed over an unbalanced closing
2507 ; brace, ignore it.
2509 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2510 ;; We've found the desired brace-pair.
2511 (progn
2512 (setq new-cons (cons bra (1+ ce)))
2513 (cond
2514 ((consp (car c-state-cache))
2515 (setcar c-state-cache new-cons))
2516 ((and (numberp (car c-state-cache)) ; probably never happens
2517 (< ce (car c-state-cache)))
2518 (setcdr c-state-cache
2519 (cons new-cons (cdr c-state-cache))))
2520 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2522 ;; We haven't found a brace pair. Record this.
2523 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2525 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2526 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2527 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2528 ;; "push" "a" brace pair onto `c-state-cache'.
2530 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2531 ;; otherwise push it normally.
2533 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2534 ;; latter is inside a macro, not being a macro containing
2535 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2536 ;; base pair. This latter case is assumed to be rare.
2538 ;; Note: POINT is not preserved in this routine.
2539 (if bra+1
2540 (if (or (> bra+1 macro-start-or-here)
2541 (progn (goto-char bra+1)
2542 (not (c-beginning-of-macro))))
2543 (setq c-state-cache
2544 (cons (cons (1- bra+1)
2545 (scan-lists bra+1 1 1))
2546 (if (consp (car c-state-cache))
2547 (cdr c-state-cache)
2548 c-state-cache)))
2549 ;; N.B. This defsubst codes one method for the simple, normal case,
2550 ;; and a more sophisticated, slower way for the general case. Don't
2551 ;; eliminate this defsubst - it's a speed optimisation.
2552 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2554 (defun c-append-to-state-cache (from)
2555 ;; Scan the buffer from FROM to (point-max), adding elements into
2556 ;; `c-state-cache' for braces etc. Return a candidate for
2557 ;; `c-state-cache-good-pos'.
2559 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2560 ;; any. Typically, it is immediately after it. It must not be inside a
2561 ;; literal.
2562 (let ((here-bol (c-point 'bol (point-max)))
2563 (macro-start-or-here
2564 (save-excursion (goto-char (point-max))
2565 (if (c-beginning-of-macro)
2566 (point)
2567 (point-max))))
2568 pa+1 ; pos just after an opening PAren (or brace).
2569 (ren+1 from) ; usually a pos just after an closing paREN etc.
2570 ; Is actually the pos. to scan for a (/{/[ from,
2571 ; which sometimes is after a silly )/}/].
2572 paren+1 ; Pos after some opening or closing paren.
2573 paren+1s ; A list of `paren+1's; used to determine a
2574 ; good-pos.
2575 bra+1 ce+1 ; just after L/R bra-ces.
2576 bra+1s ; list of OLD values of bra+1.
2577 mstart) ; start of a macro.
2579 (save-excursion
2580 ;; Each time round the following loop, we enter a succesively deeper
2581 ;; level of brace/paren nesting. (Except sometimes we "continue at
2582 ;; the existing level".) `pa+1' is a pos inside an opening
2583 ;; brace/paren/bracket, usually just after it.
2584 (while
2585 (progn
2586 ;; Each time round the next loop moves forward over an opening then
2587 ;; a closing brace/bracket/paren. This loop is white hot, so it
2588 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2589 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2590 ;; call of `scan-lists' signals an error, which happens when there
2591 ;; are no more b/b/p's to scan.
2592 (c-safe
2593 (while t
2594 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2595 paren+1s (cons pa+1 paren+1s))
2596 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2597 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2598 (setq bra+1 pa+1))
2599 (setcar paren+1s ren+1)))
2601 (if (and pa+1 (> pa+1 ren+1))
2602 ;; We've just entered a deeper nesting level.
2603 (progn
2604 ;; Insert the brace pair (if present) and the single open
2605 ;; paren/brace/bracket into `c-state-cache' It cannot be
2606 ;; inside a macro, except one around point, because of what
2607 ;; `c-neutralize-syntax-in-CPP' has done.
2608 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2609 ;; Insert the opening brace/bracket/paren position.
2610 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2611 ;; Clear admin stuff for the next more nested part of the scan.
2612 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2613 t) ; Carry on the loop
2615 ;; All open p/b/b's at this nesting level, if any, have probably
2616 ;; been closed by matching/mismatching ones. We're probably
2617 ;; finished - we just need to check for having found an
2618 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2619 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2620 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2622 ;; Record the final, innermost, brace-pair if there is one.
2623 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2625 ;; Determine a good pos
2626 (while (and (setq paren+1 (car paren+1s))
2627 (> (if (> paren+1 macro-start-or-here)
2628 paren+1
2629 (goto-char paren+1)
2630 (setq mstart (and (c-beginning-of-macro)
2631 (point)))
2632 (or mstart paren+1))
2633 here-bol))
2634 (setq paren+1s (cdr paren+1s)))
2635 (cond
2636 ((and paren+1 mstart)
2637 (min paren+1 mstart))
2638 (paren+1)
2639 (t from)))))
2641 (defun c-remove-stale-state-cache (good-pos pps-point)
2642 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2643 ;; not be in it when it is amended for position (point-max).
2644 ;; Additionally, the "outermost" open-brace entry before (point-max)
2645 ;; will be converted to a cons if the matching close-brace is scanned.
2647 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2648 ;; parens/braces/brackets between GOOD-POS and (point-max).
2650 ;; As a second thing, calculate the result of parse-partial-sexp at
2651 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2652 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2653 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2654 ;; needs to be FAST).
2656 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2657 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2658 ;; to be good (we aim for this to be as high as possible);
2659 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2660 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2661 ;; position to scan backwards from.
2662 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2663 (save-restriction
2664 (narrow-to-region 1 (point-max))
2665 (save-excursion
2666 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2667 (save-excursion
2668 (goto-char (point-max))
2669 (and (c-beginning-of-macro)
2670 (point))))
2671 (good-pos-actual-macro-start ; Start of macro containing good-pos
2672 ; or nil
2673 (and (< good-pos (point-max))
2674 (save-excursion
2675 (goto-char good-pos)
2676 (and (c-beginning-of-macro)
2677 (point)))))
2678 (good-pos-actual-macro-end ; End of this macro, (maybe
2679 ; (point-max)), or nil.
2680 (and good-pos-actual-macro-start
2681 (save-excursion
2682 (goto-char good-pos-actual-macro-start)
2683 (c-end-of-macro)
2684 (point))))
2685 pps-state ; Will be 9 or 10 elements long.
2687 upper-lim ; ,beyond which `c-state-cache' entries are removed
2688 scan-back-pos
2689 pair-beg pps-point-state target-depth)
2691 ;; Remove entries beyond (point-max). Also remove any entries inside
2692 ;; a macro, unless (point-max) is in the same macro.
2693 (setq upper-lim
2694 (if (or (null c-state-old-cpp-beg)
2695 (and (> (point-max) c-state-old-cpp-beg)
2696 (< (point-max) c-state-old-cpp-end)))
2697 (point-max)
2698 (min (point-max) c-state-old-cpp-beg)))
2699 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2700 (setq c-state-cache (cdr c-state-cache)))
2701 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2702 ;; RBrace and indicate we'll need to search backwards for a previous
2703 ;; brace pair.
2704 (when (and c-state-cache
2705 (consp (car c-state-cache))
2706 (> (cdar c-state-cache) upper-lim))
2707 (setcar c-state-cache (caar c-state-cache))
2708 (setq scan-back-pos (car c-state-cache)))
2710 ;; The next loop jumps forward out of a nested level of parens each
2711 ;; time round; the corresponding elements in `c-state-cache' are
2712 ;; removed. `pos' is just after the brace-pair or the open paren at
2713 ;; (car c-state-cache). There can be no open parens/braces/brackets
2714 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2715 ;; due to the interface spec to this function.
2716 (setq pos (if (and good-pos-actual-macro-end
2717 (not (eq good-pos-actual-macro-start
2718 in-macro-start)))
2719 (1+ good-pos-actual-macro-end) ; get outside the macro as
2720 ; marked by a `category' text property.
2721 good-pos))
2722 (goto-char pos)
2723 (while (and c-state-cache
2724 (< (point) (point-max)))
2725 (cond
2726 ((null pps-state) ; first time through
2727 (setq target-depth -1))
2728 ((eq (car pps-state) target-depth) ; found closing ),},]
2729 (setq target-depth (1- (car pps-state))))
2730 ;; Do nothing when we've merely reached pps-point.
2733 ;; Scan!
2734 (setq pps-state
2735 (parse-partial-sexp
2736 (point) (if (< (point) pps-point) pps-point (point-max))
2737 target-depth
2738 nil pps-state))
2740 (if (= (point) pps-point)
2741 (setq pps-point-state pps-state))
2743 (when (eq (car pps-state) target-depth)
2744 (setq pos (point)) ; POS is now just after an R-paren/brace.
2745 (cond
2746 ((and (consp (car c-state-cache))
2747 (eq (point) (cdar c-state-cache)))
2748 ;; We've just moved out of the paren pair containing the brace-pair
2749 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2750 ;; and is potentially where the open brace of a cons in
2751 ;; c-state-cache will be.
2752 (setq pair-beg (car-safe (cdr c-state-cache))
2753 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2754 ((numberp (car c-state-cache))
2755 (setq pair-beg (car c-state-cache)
2756 c-state-cache (cdr c-state-cache))) ; remove this
2757 ; containing Lparen
2758 ((numberp (cadr c-state-cache))
2759 (setq pair-beg (cadr c-state-cache)
2760 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2761 ; together with enclosed brace pair.
2762 ;; (t nil) ; Ignore an unmated Rparen.
2765 (if (< (point) pps-point)
2766 (setq pps-state (parse-partial-sexp (point) pps-point
2767 nil nil ; TARGETDEPTH, STOPBEFORE
2768 pps-state)))
2770 ;; If the last paren pair we moved out of was actually a brace pair,
2771 ;; insert it into `c-state-cache'.
2772 (when (and pair-beg (eq (char-after pair-beg) ?{))
2773 (if (consp (car-safe c-state-cache))
2774 (setq c-state-cache (cdr c-state-cache)))
2775 (setq c-state-cache (cons (cons pair-beg pos)
2776 c-state-cache)))
2778 (list pos scan-back-pos pps-state)))))
2780 (defun c-remove-stale-state-cache-backwards (here cache-pos)
2781 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2782 ;; buffer, and inform the caller of the scenario detected.
2784 ;; HERE is the position we're setting `c-state-cache' for.
2785 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2786 ;; before HERE, or a position at or near point-min which isn't in a
2787 ;; literal.
2789 ;; This function must only be called only when (> `c-state-cache-good-pos'
2790 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2791 ;; optimised to eliminate (or minimise) scanning between these two
2792 ;; positions.
2794 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2795 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2796 ;; could become so after missing elements are inserted into
2797 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2798 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2799 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2800 ;; before `here''s line, or the start of the literal containing it.
2801 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2802 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2803 ;; to scan backwards from.
2804 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2805 ;; POS and HERE which aren't recorded in `c-state-cache'.
2807 ;; The comments in this defun use "paren" to mean parenthesis or square
2808 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2810 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2811 ;; | | | | | |
2812 ;; CP E here D C good
2813 (let ((pos c-state-cache-good-pos)
2814 pa ren ; positions of "(" and ")"
2815 dropped-cons ; whether the last element dropped from `c-state-cache'
2816 ; was a cons (representing a brace-pair)
2817 good-pos ; see above.
2818 lit ; (START . END) of a literal containing some point.
2819 here-lit-start here-lit-end ; bounds of literal containing `here'
2820 ; or `here' itself.
2821 here- here+ ; start/end of macro around HERE, or HERE
2822 (here-bol (c-point 'bol here))
2823 (too-far-back (max (- here c-state-cache-too-far) 1)))
2825 ;; Remove completely irrelevant entries from `c-state-cache'.
2826 (while (and c-state-cache
2827 (>= (setq pa (c-state-cache-top-lparen)) here))
2828 (setq dropped-cons (consp (car c-state-cache)))
2829 (setq c-state-cache (cdr c-state-cache))
2830 (setq pos pa))
2831 ;; At this stage, (> pos here);
2832 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2834 (cond
2835 ((and (consp (car c-state-cache))
2836 (> (cdar c-state-cache) here))
2837 ;; CASE 1: The top of the cache is a brace pair which now encloses
2838 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2839 ;; knowledge of what's inside these braces, we have no alternative but
2840 ;; to direct the caller to scan the buffer from the opening brace.
2841 (setq pos (caar c-state-cache))
2842 (setcar c-state-cache pos)
2843 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
2844 ; entry into a { entry, so the caller needs to
2845 ; search for a brace pair before the {.
2847 ;; `here' might be inside a literal. Check for this.
2848 ((progn
2849 (setq lit (c-state-literal-at here)
2850 here-lit-start (or (car lit) here)
2851 here-lit-end (or (cdr lit) here))
2852 ;; Has `here' just "newly entered" a macro?
2853 (save-excursion
2854 (goto-char here-lit-start)
2855 (if (and (c-beginning-of-macro)
2856 (or (null c-state-old-cpp-beg)
2857 (not (= (point) c-state-old-cpp-beg))))
2858 (progn
2859 (setq here- (point))
2860 (c-end-of-macro)
2861 (setq here+ (point)))
2862 (setq here- here-lit-start
2863 here+ here-lit-end)))
2865 ;; `here' might be nested inside any depth of parens (or brackets but
2866 ;; not braces). Scan backwards to find the outermost such opening
2867 ;; paren, if there is one. This will be the scan position to return.
2868 (save-restriction
2869 (narrow-to-region cache-pos (point-max))
2870 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
2871 nil)) ; for the cond
2873 ((< pos here-lit-start)
2874 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2875 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2876 ;; a brace pair preceding this, it will already be in `c-state-cache',
2877 ;; unless there was a brace pair after it, i.e. there'll only be one to
2878 ;; scan for if we've just deleted one.
2879 (list pos (and dropped-cons pos) t)) ; Return value.
2881 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2882 ;; Further forward scanning isn't needed, but we still need to find a
2883 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2884 ((progn
2885 (save-restriction
2886 (narrow-to-region here-bol (point-max))
2887 (setq pos here-lit-start)
2888 (c-safe (while (setq pa (scan-lists pos -1 1))
2889 (setq pos pa)))) ; might signal
2890 nil)) ; for the cond
2892 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
2893 ;; CASE 3: After a }/)/] before `here''s BOL.
2894 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
2897 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2898 ;; literal containing it.
2899 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
2900 (list good-pos (and dropped-cons good-pos) nil)))))
2903 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2904 ;; Externally visible routines.
2906 (defun c-state-cache-init ()
2907 (setq c-state-cache nil
2908 c-state-cache-good-pos 1
2909 c-state-nonlit-pos-cache nil
2910 c-state-nonlit-pos-cache-limit 1
2911 c-state-brace-pair-desert nil
2912 c-state-point-min 1
2913 c-state-point-min-lit-type nil
2914 c-state-point-min-lit-start nil
2915 c-state-min-scan-pos 1
2916 c-state-old-cpp-beg nil
2917 c-state-old-cpp-end nil)
2918 (c-state-mark-point-min-literal))
2920 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2921 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
2922 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
2923 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
2924 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
2925 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
2926 ;; (defun c-state-dump ()
2927 ;; ;; For debugging.
2928 ;; ;(message
2929 ;; (concat
2930 ;; (c-sc-qde c-state-cache)
2931 ;; (c-sc-de c-state-cache-good-pos)
2932 ;; (c-sc-qde c-state-nonlit-pos-cache)
2933 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
2934 ;; (c-sc-qde c-state-brace-pair-desert)
2935 ;; (c-sc-de c-state-point-min)
2936 ;; (c-sc-de c-state-point-min-lit-type)
2937 ;; (c-sc-de c-state-point-min-lit-start)
2938 ;; (c-sc-de c-state-min-scan-pos)
2939 ;; (c-sc-de c-state-old-cpp-beg)
2940 ;; (c-sc-de c-state-old-cpp-end)))
2941 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2943 (defun c-invalidate-state-cache-1 (here)
2944 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2945 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2946 ;; left in a consistent state.
2948 ;; This is much like `c-whack-state-after', but it never changes a paren
2949 ;; pair element into an open paren element. Doing that would mean that the
2950 ;; new open paren wouldn't have the required preceding paren pair element.
2952 ;; This function is called from c-after-change.
2954 ;; The cache of non-literals:
2955 (if (< here c-state-nonlit-pos-cache-limit)
2956 (setq c-state-nonlit-pos-cache-limit here))
2958 ;; `c-state-cache':
2959 ;; Case 1: if `here' is in a literal containing point-min, everything
2960 ;; becomes (or is already) nil.
2961 (if (or (null c-state-cache-good-pos)
2962 (< here (c-state-get-min-scan-pos)))
2963 (setq c-state-cache nil
2964 c-state-cache-good-pos nil
2965 c-state-min-scan-pos nil)
2967 ;;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value below
2968 ;;; `here'. To maintain its consistency, we may need to insert a new brace
2969 ;;; pair.
2970 (let ((here-bol (c-point 'bol here))
2971 too-high-pa ; recorded {/(/[ next above here, or nil.
2972 dropped-cons ; was the last removed element a brace pair?
2974 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
2975 (while (and c-state-cache
2976 (>= (setq pa (c-state-cache-top-paren)) here))
2977 (setq dropped-cons (consp (car c-state-cache))
2978 too-high-pa (c-state-cache-top-lparen)
2979 c-state-cache (cdr c-state-cache)))
2981 ;; Do we need to add in an earlier brace pair, having lopped one off?
2982 (if (and dropped-cons
2983 (< too-high-pa (+ here c-state-cache-too-far)))
2984 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
2985 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
2986 (c-state-get-min-scan-pos)))))
2988 ;; The brace-pair desert marker:
2989 (when (car c-state-brace-pair-desert)
2990 (if (< here (car c-state-brace-pair-desert))
2991 (setq c-state-brace-pair-desert nil)
2992 (if (< here (cdr c-state-brace-pair-desert))
2993 (setcdr c-state-brace-pair-desert here)))))
2995 (defun c-parse-state-1 ()
2996 ;; Find and record all noteworthy parens between some good point earlier in
2997 ;; the file and point. That good point is at least the beginning of the
2998 ;; top-level construct we are in, or the beginning of the preceding
2999 ;; top-level construct if we aren't in one.
3001 ;; The returned value is a list of the noteworthy parens with the last one
3002 ;; first. If an element in the list is an integer, it's the position of an
3003 ;; open paren (of any type) which has not been closed before the point. If
3004 ;; an element is a cons, it gives the position of a closed BRACE paren
3005 ;; pair[*]; the car is the start brace position and the cdr is the position
3006 ;; following the closing brace. Only the last closed brace paren pair
3007 ;; before each open paren and before the point is recorded, and thus the
3008 ;; state never contains two cons elements in succession. When a close brace
3009 ;; has no matching open brace (e.g., the matching brace is outside the
3010 ;; visible region), it is not represented in the returned value.
3012 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3013 ;; This defun explicitly treats mismatching parens/braces/brackets as
3014 ;; matching. It is the open brace which makes it a "brace" pair.
3016 ;; If POINT is within a macro, open parens and brace pairs within
3017 ;; THIS macro MIGHT be recorded. This depends on whether their
3018 ;; syntactic properties have been suppressed by
3019 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3021 ;; Currently no characters which are given paren syntax with the
3022 ;; syntax-table property are recorded, i.e. angle bracket arglist
3023 ;; parens are never present here. Note that this might change.
3025 ;; BUG: This function doesn't cope entirely well with unbalanced
3026 ;; parens in macros. (2008-12-11: this has probably been resolved
3027 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3028 ;; following case the brace before the macro isn't balanced with the
3029 ;; one after it:
3031 ;; {
3032 ;; #define X {
3033 ;; }
3035 ;; Note to maintainers: this function DOES get called with point
3036 ;; within comments and strings, so don't assume it doesn't!
3038 ;; This function might do hidden buffer changes.
3039 (let* ((here (point))
3040 (here-bopl (c-point 'bopl))
3041 strategy ; 'forward, 'backward etc..
3042 ;; Candidate positions to start scanning from:
3043 cache-pos ; highest position below HERE already existing in
3044 ; cache (or 1).
3045 good-pos
3046 start-point
3047 bopl-state
3049 scan-backward-pos scan-forward-p) ; used for 'backward.
3050 ;; If POINT-MIN has changed, adjust the cache
3051 (unless (= (point-min) c-state-point-min)
3052 (c-renarrow-state-cache))
3054 ;; Strategy?
3055 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3056 strategy (car res)
3057 cache-pos (cadr res)
3058 start-point (nth 2 res))
3060 (when (eq strategy 'BOD)
3061 (setq c-state-cache nil
3062 c-state-cache-good-pos start-point))
3064 ;; SCAN!
3065 (save-restriction
3066 (cond
3067 ((memq strategy '(forward BOD))
3068 (narrow-to-region (point-min) here)
3069 (setq res (c-remove-stale-state-cache start-point here-bopl))
3070 (setq cache-pos (car res)
3071 scan-backward-pos (cadr res)
3072 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
3073 ; start-point)
3074 (if scan-backward-pos
3075 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
3076 (setq good-pos
3077 (c-append-to-state-cache cache-pos))
3078 (setq c-state-cache-good-pos
3079 (if (and bopl-state
3080 (< good-pos (- here c-state-cache-too-far)))
3081 (c-state-cache-non-literal-place here-bopl bopl-state)
3082 good-pos)))
3084 ((eq strategy 'backward)
3085 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
3086 good-pos (car res)
3087 scan-backward-pos (cadr res)
3088 scan-forward-p (car (cddr res)))
3089 (if scan-backward-pos
3090 (c-append-lower-brace-pair-to-state-cache
3091 scan-backward-pos))
3092 (setq c-state-cache-good-pos
3093 (if scan-forward-p
3094 (progn (narrow-to-region (point-min) here)
3095 (c-append-to-state-cache good-pos))
3097 (c-get-cache-scan-pos good-pos))))
3099 (t ; (eq strategy 'IN-LIT)
3100 (setq c-state-cache nil
3101 c-state-cache-good-pos nil)))))
3103 c-state-cache)
3105 (defun c-invalidate-state-cache (here)
3106 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3108 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3109 ;; of all parens in preprocessor constructs, except for any such construct
3110 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3111 ;; worrying further about macros and template delimiters.
3112 (c-with-<->-as-parens-suppressed
3113 (if (and c-state-old-cpp-beg
3114 (< c-state-old-cpp-beg here))
3115 (c-with-all-but-one-cpps-commented-out
3116 c-state-old-cpp-beg
3117 (min c-state-old-cpp-end here)
3118 (c-invalidate-state-cache-1 here))
3119 (c-with-cpps-commented-out
3120 (c-invalidate-state-cache-1 here)))))
3122 (defun c-parse-state ()
3123 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3124 ;; description of the functionality and return value.
3126 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3127 ;; of all parens in preprocessor constructs, except for any such construct
3128 ;; containing point. We can then call `c-parse-state-1' without worrying
3129 ;; further about macros and template delimiters.
3130 (let (here-cpp-beg here-cpp-end)
3131 (save-excursion
3132 (when (c-beginning-of-macro)
3133 (setq here-cpp-beg (point))
3134 (unless
3135 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3136 here-cpp-beg)
3137 (setq here-cpp-beg nil here-cpp-end nil))))
3138 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3139 ;; subsystem.
3140 (prog1
3141 (c-with-<->-as-parens-suppressed
3142 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3143 (c-with-all-but-one-cpps-commented-out
3144 here-cpp-beg here-cpp-end
3145 (c-parse-state-1))
3146 (c-with-cpps-commented-out
3147 (c-parse-state-1))))
3148 (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
3149 c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
3152 ;; Debug tool to catch cache inconsistencies. This is called from
3153 ;; 000tests.el.
3154 (defvar c-debug-parse-state nil)
3155 (unless (fboundp 'c-real-parse-state)
3156 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3157 (cc-bytecomp-defun c-real-parse-state)
3159 (defvar c-parse-state-state nil)
3160 (defun c-record-parse-state-state ()
3161 (setq c-parse-state-state
3162 (mapcar
3163 (lambda (arg)
3164 (cons arg (symbol-value arg)))
3165 '(c-state-cache
3166 c-state-cache-good-pos
3167 c-state-nonlit-pos-cache
3168 c-state-nonlit-pos-cache-limit
3169 c-state-brace-pair-desert
3170 c-state-point-min
3171 c-state-point-min-lit-type
3172 c-state-point-min-lit-start
3173 c-state-min-scan-pos
3174 c-state-old-cpp-beg
3175 c-state-old-cpp-end))))
3176 (defun c-replay-parse-state-state ()
3177 (message
3178 (concat "(setq "
3179 (mapconcat
3180 (lambda (arg)
3181 (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
3182 c-parse-state-state " ")
3183 ")")))
3185 (defun c-debug-parse-state ()
3186 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3187 (let ((c-state-cache nil)
3188 (c-state-cache-good-pos 1)
3189 (c-state-nonlit-pos-cache nil)
3190 (c-state-nonlit-pos-cache-limit 1)
3191 (c-state-brace-pair-desert nil)
3192 (c-state-point-min 1)
3193 (c-state-point-min-lit-type nil)
3194 (c-state-point-min-lit-start nil)
3195 (c-state-min-scan-pos 1)
3196 (c-state-old-cpp-beg nil)
3197 (c-state-old-cpp-end nil))
3198 (setq res2 (c-real-parse-state)))
3199 (unless (equal res1 res2)
3200 ;; The cache can actually go further back due to the ad-hoc way
3201 ;; the first paren is found, so try to whack off a bit of its
3202 ;; start before complaining.
3203 ;; (save-excursion
3204 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3205 ;; (c-beginning-of-defun-1)
3206 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3207 ;; (c-beginning-of-defun-1))
3208 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3209 ;; (message (concat "c-parse-state inconsistency at %s: "
3210 ;; "using cache: %s, from scratch: %s")
3211 ;; here res1 res2)))
3212 (message (concat "c-parse-state inconsistency at %s: "
3213 "using cache: %s, from scratch: %s")
3214 here res1 res2)
3215 (message "Old state:")
3216 (c-replay-parse-state-state))
3217 (c-record-parse-state-state)
3218 res1))
3220 (defun c-toggle-parse-state-debug (&optional arg)
3221 (interactive "P")
3222 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3223 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3224 'c-debug-parse-state
3225 'c-real-parse-state)))
3226 (c-keep-region-active))
3227 (when c-debug-parse-state
3228 (c-toggle-parse-state-debug 1))
3231 (defun c-whack-state-before (bufpos paren-state)
3232 ;; Whack off any state information from PAREN-STATE which lies
3233 ;; before BUFPOS. Not destructive on PAREN-STATE.
3234 (let* ((newstate (list nil))
3235 (ptr newstate)
3236 car)
3237 (while paren-state
3238 (setq car (car paren-state)
3239 paren-state (cdr paren-state))
3240 (if (< (if (consp car) (car car) car) bufpos)
3241 (setq paren-state nil)
3242 (setcdr ptr (list car))
3243 (setq ptr (cdr ptr))))
3244 (cdr newstate)))
3246 (defun c-whack-state-after (bufpos paren-state)
3247 ;; Whack off any state information from PAREN-STATE which lies at or
3248 ;; after BUFPOS. Not destructive on PAREN-STATE.
3249 (catch 'done
3250 (while paren-state
3251 (let ((car (car paren-state)))
3252 (if (consp car)
3253 ;; just check the car, because in a balanced brace
3254 ;; expression, it must be impossible for the corresponding
3255 ;; close brace to be before point, but the open brace to
3256 ;; be after.
3257 (if (<= bufpos (car car))
3258 nil ; whack it off
3259 (if (< bufpos (cdr car))
3260 ;; its possible that the open brace is before
3261 ;; bufpos, but the close brace is after. In that
3262 ;; case, convert this to a non-cons element. The
3263 ;; rest of the state is before bufpos, so we're
3264 ;; done.
3265 (throw 'done (cons (car car) (cdr paren-state)))
3266 ;; we know that both the open and close braces are
3267 ;; before bufpos, so we also know that everything else
3268 ;; on state is before bufpos.
3269 (throw 'done paren-state)))
3270 (if (<= bufpos car)
3271 nil ; whack it off
3272 ;; it's before bufpos, so everything else should too.
3273 (throw 'done paren-state)))
3274 (setq paren-state (cdr paren-state)))
3275 nil)))
3277 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3278 ;; Return the bufpos of the innermost enclosing open paren before
3279 ;; bufpos, or nil if none was found.
3280 (let (enclosingp)
3281 (or bufpos (setq bufpos 134217727))
3282 (while paren-state
3283 (setq enclosingp (car paren-state)
3284 paren-state (cdr paren-state))
3285 (if (or (consp enclosingp)
3286 (>= enclosingp bufpos))
3287 (setq enclosingp nil)
3288 (setq paren-state nil)))
3289 enclosingp))
3291 (defun c-least-enclosing-brace (paren-state)
3292 ;; Return the bufpos of the outermost enclosing open paren, or nil
3293 ;; if none was found.
3294 (let (pos elem)
3295 (while paren-state
3296 (setq elem (car paren-state)
3297 paren-state (cdr paren-state))
3298 (if (integerp elem)
3299 (setq pos elem)))
3300 pos))
3302 (defun c-safe-position (bufpos paren-state)
3303 ;; Return the closest "safe" position recorded on PAREN-STATE that
3304 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3305 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3306 ;; find the closest limit before a given limit that might be nil.
3308 ;; A "safe" position is a position at or after a recorded open
3309 ;; paren, or after a recorded close paren. The returned position is
3310 ;; thus either the first position after a close brace, or the first
3311 ;; position after an enclosing paren, or at the enclosing paren in
3312 ;; case BUFPOS is immediately after it.
3313 (when bufpos
3314 (let (elem)
3315 (catch 'done
3316 (while paren-state
3317 (setq elem (car paren-state))
3318 (if (consp elem)
3319 (cond ((< (cdr elem) bufpos)
3320 (throw 'done (cdr elem)))
3321 ((< (car elem) bufpos)
3322 ;; See below.
3323 (throw 'done (min (1+ (car elem)) bufpos))))
3324 (if (< elem bufpos)
3325 ;; elem is the position at and not after the opening paren, so
3326 ;; we can go forward one more step unless it's equal to
3327 ;; bufpos. This is useful in some cases avoid an extra paren
3328 ;; level between the safe position and bufpos.
3329 (throw 'done (min (1+ elem) bufpos))))
3330 (setq paren-state (cdr paren-state)))))))
3332 (defun c-beginning-of-syntax ()
3333 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3334 ;; goes to the closest previous point that is known to be outside
3335 ;; any string literal or comment. `c-state-cache' is used if it has
3336 ;; a position in the vicinity.
3337 (let* ((paren-state c-state-cache)
3338 elem
3340 (pos (catch 'done
3341 ;; Note: Similar code in `c-safe-position'. The
3342 ;; difference is that we accept a safe position at
3343 ;; the point and don't bother to go forward past open
3344 ;; parens.
3345 (while paren-state
3346 (setq elem (car paren-state))
3347 (if (consp elem)
3348 (cond ((<= (cdr elem) (point))
3349 (throw 'done (cdr elem)))
3350 ((<= (car elem) (point))
3351 (throw 'done (car elem))))
3352 (if (<= elem (point))
3353 (throw 'done elem)))
3354 (setq paren-state (cdr paren-state)))
3355 (point-min))))
3357 (if (> pos (- (point) 4000))
3358 (goto-char pos)
3359 ;; The position is far back. Try `c-beginning-of-defun-1'
3360 ;; (although we can't be entirely sure it will go to a position
3361 ;; outside a comment or string in current emacsen). FIXME:
3362 ;; Consult `syntax-ppss' here.
3363 (c-beginning-of-defun-1)
3364 (if (< (point) pos)
3365 (goto-char pos)))))
3368 ;; Tools for scanning identifiers and other tokens.
3370 (defun c-on-identifier ()
3371 "Return non-nil if the point is on or directly after an identifier.
3372 Keywords are recognized and not considered identifiers. If an
3373 identifier is detected, the returned value is its starting position.
3374 If an identifier ends at the point and another begins at it \(can only
3375 happen in Pike) then the point for the preceding one is returned.
3377 Note that this function might do hidden buffer changes. See the
3378 comment at the start of cc-engine.el for more info."
3380 ;; FIXME: Shouldn't this function handle "operator" in C++?
3382 (save-excursion
3383 (skip-syntax-backward "w_")
3387 ;; Check for a normal (non-keyword) identifier.
3388 (and (looking-at c-symbol-start)
3389 (not (looking-at c-keywords-regexp))
3390 (point))
3392 (when (c-major-mode-is 'pike-mode)
3393 ;; Handle the `<operator> syntax in Pike.
3394 (let ((pos (point)))
3395 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3396 (and (if (< (skip-chars-backward "`") 0)
3398 (goto-char pos)
3399 (eq (char-after) ?\`))
3400 (looking-at c-symbol-key)
3401 (>= (match-end 0) pos)
3402 (point))))
3404 ;; Handle the "operator +" syntax in C++.
3405 (when (and c-overloadable-operators-regexp
3406 (= (c-backward-token-2 0) 0))
3408 (cond ((and (looking-at c-overloadable-operators-regexp)
3409 (or (not c-opt-op-identifier-prefix)
3410 (and (= (c-backward-token-2 1) 0)
3411 (looking-at c-opt-op-identifier-prefix))))
3412 (point))
3414 ((save-excursion
3415 (and c-opt-op-identifier-prefix
3416 (looking-at c-opt-op-identifier-prefix)
3417 (= (c-forward-token-2 1) 0)
3418 (looking-at c-overloadable-operators-regexp)))
3419 (point))))
3423 (defsubst c-simple-skip-symbol-backward ()
3424 ;; If the point is at the end of a symbol then skip backward to the
3425 ;; beginning of it. Don't move otherwise. Return non-nil if point
3426 ;; moved.
3428 ;; This function might do hidden buffer changes.
3429 (or (< (skip-syntax-backward "w_") 0)
3430 (and (c-major-mode-is 'pike-mode)
3431 ;; Handle the `<operator> syntax in Pike.
3432 (let ((pos (point)))
3433 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3434 (< (skip-chars-backward "`") 0)
3435 (looking-at c-symbol-key)
3436 (>= (match-end 0) pos))
3438 (goto-char pos)
3439 nil)))))
3441 (defun c-beginning-of-current-token (&optional back-limit)
3442 ;; Move to the beginning of the current token. Do not move if not
3443 ;; in the middle of one. BACK-LIMIT may be used to bound the
3444 ;; backward search; if given it's assumed to be at the boundary
3445 ;; between two tokens. Return non-nil if the point is moved, nil
3446 ;; otherwise.
3448 ;; This function might do hidden buffer changes.
3449 (let ((start (point)))
3450 (if (looking-at "\\w\\|\\s_")
3451 (skip-syntax-backward "w_" back-limit)
3452 (when (< (skip-syntax-backward ".()" back-limit) 0)
3453 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3454 (match-end 0))
3455 ;; `c-nonsymbol-token-regexp' should always match
3456 ;; since we've skipped backward over punctuator
3457 ;; or paren syntax, but consume one char in case
3458 ;; it doesn't so that we don't leave point before
3459 ;; some earlier incorrect token.
3460 (1+ (point)))))
3461 (if (<= pos start)
3462 (goto-char pos))))))
3463 (< (point) start)))
3465 (defun c-end-of-current-token (&optional back-limit)
3466 ;; Move to the end of the current token. Do not move if not in the
3467 ;; middle of one. BACK-LIMIT may be used to bound the backward
3468 ;; search; if given it's assumed to be at the boundary between two
3469 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3471 ;; This function might do hidden buffer changes.
3472 (let ((start (point)))
3473 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3474 (skip-syntax-forward "w_"))
3475 ((< (skip-syntax-backward ".()" back-limit) 0)
3476 (while (progn
3477 (if (looking-at c-nonsymbol-token-regexp)
3478 (goto-char (match-end 0))
3479 ;; `c-nonsymbol-token-regexp' should always match since
3480 ;; we've skipped backward over punctuator or paren
3481 ;; syntax, but move forward in case it doesn't so that
3482 ;; we don't leave point earlier than we started with.
3483 (forward-char))
3484 (< (point) start)))))
3485 (> (point) start)))
3487 (defconst c-jump-syntax-balanced
3488 (if (memq 'gen-string-delim c-emacs-features)
3489 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3490 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3492 (defconst c-jump-syntax-unbalanced
3493 (if (memq 'gen-string-delim c-emacs-features)
3494 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3495 "\\w\\|\\s_\\|\\s\""))
3497 (defun c-forward-token-2 (&optional count balanced limit)
3498 "Move forward by tokens.
3499 A token is defined as all symbols and identifiers which aren't
3500 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3501 treated properly). Point is always either left at the beginning of a
3502 token or not moved at all. COUNT specifies the number of tokens to
3503 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3504 moves to the next token beginning only if not already at one. If
3505 BALANCED is true, move over balanced parens, otherwise move into them.
3506 Also, if BALANCED is true, never move out of an enclosing paren.
3508 LIMIT sets the limit for the movement and defaults to the point limit.
3509 The case when LIMIT is set in the middle of a token, comment or macro
3510 is handled correctly, i.e. the point won't be left there.
3512 Return the number of tokens left to move \(positive or negative). If
3513 BALANCED is true, a move over a balanced paren counts as one. Note
3514 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3515 be returned. Thus, a return value of 0 guarantees that point is at
3516 the requested position and a return value less \(without signs) than
3517 COUNT guarantees that point is at the beginning of some token.
3519 Note that this function might do hidden buffer changes. See the
3520 comment at the start of cc-engine.el for more info."
3522 (or count (setq count 1))
3523 (if (< count 0)
3524 (- (c-backward-token-2 (- count) balanced limit))
3526 (let ((jump-syntax (if balanced
3527 c-jump-syntax-balanced
3528 c-jump-syntax-unbalanced))
3529 (last (point))
3530 (prev (point)))
3532 (if (zerop count)
3533 ;; If count is zero we should jump if in the middle of a token.
3534 (c-end-of-current-token))
3536 (save-restriction
3537 (if limit (narrow-to-region (point-min) limit))
3538 (if (/= (point)
3539 (progn (c-forward-syntactic-ws) (point)))
3540 ;; Skip whitespace. Count this as a move if we did in
3541 ;; fact move.
3542 (setq count (max (1- count) 0)))
3544 (if (eobp)
3545 ;; Moved out of bounds. Make sure the returned count isn't zero.
3546 (progn
3547 (if (zerop count) (setq count 1))
3548 (goto-char last))
3550 ;; Use `condition-case' to avoid having the limit tests
3551 ;; inside the loop.
3552 (condition-case nil
3553 (while (and
3554 (> count 0)
3555 (progn
3556 (setq last (point))
3557 (cond ((looking-at jump-syntax)
3558 (goto-char (scan-sexps (point) 1))
3560 ((looking-at c-nonsymbol-token-regexp)
3561 (goto-char (match-end 0))
3563 ;; `c-nonsymbol-token-regexp' above should always
3564 ;; match if there are correct tokens. Try to
3565 ;; widen to see if the limit was set in the
3566 ;; middle of one, else fall back to treating
3567 ;; the offending thing as a one character token.
3568 ((and limit
3569 (save-restriction
3570 (widen)
3571 (looking-at c-nonsymbol-token-regexp)))
3572 nil)
3574 (forward-char)
3575 t))))
3576 (c-forward-syntactic-ws)
3577 (setq prev last
3578 count (1- count)))
3579 (error (goto-char last)))
3581 (when (eobp)
3582 (goto-char prev)
3583 (setq count (1+ count)))))
3585 count)))
3587 (defun c-backward-token-2 (&optional count balanced limit)
3588 "Move backward by tokens.
3589 See `c-forward-token-2' for details."
3591 (or count (setq count 1))
3592 (if (< count 0)
3593 (- (c-forward-token-2 (- count) balanced limit))
3595 (or limit (setq limit (point-min)))
3596 (let ((jump-syntax (if balanced
3597 c-jump-syntax-balanced
3598 c-jump-syntax-unbalanced))
3599 (last (point)))
3601 (if (zerop count)
3602 ;; The count is zero so try to skip to the beginning of the
3603 ;; current token.
3604 (if (> (point)
3605 (progn (c-beginning-of-current-token) (point)))
3606 (if (< (point) limit)
3607 ;; The limit is inside the same token, so return 1.
3608 (setq count 1))
3610 ;; We're not in the middle of a token. If there's
3611 ;; whitespace after the point then we must move backward,
3612 ;; so set count to 1 in that case.
3613 (and (looking-at c-syntactic-ws-start)
3614 ;; If we're looking at a '#' that might start a cpp
3615 ;; directive then we have to do a more elaborate check.
3616 (or (/= (char-after) ?#)
3617 (not c-opt-cpp-prefix)
3618 (save-excursion
3619 (and (= (point)
3620 (progn (beginning-of-line)
3621 (looking-at "[ \t]*")
3622 (match-end 0)))
3623 (or (bobp)
3624 (progn (backward-char)
3625 (not (eq (char-before) ?\\)))))))
3626 (setq count 1))))
3628 ;; Use `condition-case' to avoid having to check for buffer
3629 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3630 (condition-case nil
3631 (while (and
3632 (> count 0)
3633 (progn
3634 (c-backward-syntactic-ws)
3635 (backward-char)
3636 (if (looking-at jump-syntax)
3637 (goto-char (scan-sexps (1+ (point)) -1))
3638 ;; This can be very inefficient if there's a long
3639 ;; sequence of operator tokens without any separation.
3640 ;; That doesn't happen in practice, anyway.
3641 (c-beginning-of-current-token))
3642 (>= (point) limit)))
3643 (setq last (point)
3644 count (1- count)))
3645 (error (goto-char last)))
3647 (if (< (point) limit)
3648 (goto-char last))
3650 count)))
3652 (defun c-forward-token-1 (&optional count balanced limit)
3653 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3654 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3655 characters are jumped over character by character. This function is
3656 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3657 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3658 (c-forward-token-2 count balanced limit)))
3660 (defun c-backward-token-1 (&optional count balanced limit)
3661 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3662 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3663 characters are jumped over character by character. This function is
3664 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3665 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3666 (c-backward-token-2 count balanced limit)))
3669 ;; Tools for doing searches restricted to syntactically relevant text.
3671 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3672 paren-level not-inside-token
3673 lookbehind-submatch)
3674 "Like `re-search-forward', but only report matches that are found
3675 in syntactically significant text. I.e. matches in comments, macros
3676 or string literals are ignored. The start point is assumed to be
3677 outside any comment, macro or string literal, or else the content of
3678 that region is taken as syntactically significant text.
3680 If PAREN-LEVEL is non-nil, an additional restriction is added to
3681 ignore matches in nested paren sexps. The search will also not go
3682 outside the current list sexp, which has the effect that if the point
3683 should be moved to BOUND when no match is found \(i.e. NOERROR is
3684 neither nil nor t), then it will be at the closing paren if the end of
3685 the current list sexp is encountered first.
3687 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3688 ignored. Things like multicharacter operators and special symbols
3689 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3690 constants.
3692 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3693 subexpression in REGEXP. The end of that submatch is used as the
3694 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3695 isn't used or if that subexpression didn't match then the start
3696 position of the whole match is used instead. The \"look behind\"
3697 subexpression is never tested before the starting position, so it
3698 might be a good idea to include \\=\\= as a match alternative in it.
3700 Optimization note: Matches might be missed if the \"look behind\"
3701 subexpression can match the end of nonwhite syntactic whitespace,
3702 i.e. the end of comments or cpp directives. This since the function
3703 skips over such things before resuming the search. It's on the other
3704 hand not safe to assume that the \"look behind\" subexpression never
3705 matches syntactic whitespace.
3707 Bug: Unbalanced parens inside cpp directives are currently not handled
3708 correctly \(i.e. they don't get ignored as they should) when
3709 PAREN-LEVEL is set.
3711 Note that this function might do hidden buffer changes. See the
3712 comment at the start of cc-engine.el for more info."
3714 (or bound (setq bound (point-max)))
3715 (if paren-level (setq paren-level -1))
3717 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3719 (let ((start (point))
3721 ;; Start position for the last search.
3722 search-pos
3723 ;; The `parse-partial-sexp' state between the start position
3724 ;; and the point.
3725 state
3726 ;; The current position after the last state update. The next
3727 ;; `parse-partial-sexp' continues from here.
3728 (state-pos (point))
3729 ;; The position at which to check the state and the state
3730 ;; there. This is separate from `state-pos' since we might
3731 ;; need to back up before doing the next search round.
3732 check-pos check-state
3733 ;; Last position known to end a token.
3734 (last-token-end-pos (point-min))
3735 ;; Set when a valid match is found.
3736 found)
3738 (condition-case err
3739 (while
3740 (and
3741 (progn
3742 (setq search-pos (point))
3743 (re-search-forward regexp bound noerror))
3745 (progn
3746 (setq state (parse-partial-sexp
3747 state-pos (match-beginning 0) paren-level nil state)
3748 state-pos (point))
3749 (if (setq check-pos (and lookbehind-submatch
3750 (or (not paren-level)
3751 (>= (car state) 0))
3752 (match-end lookbehind-submatch)))
3753 (setq check-state (parse-partial-sexp
3754 state-pos check-pos paren-level nil state))
3755 (setq check-pos state-pos
3756 check-state state))
3758 ;; NOTE: If we got a look behind subexpression and get
3759 ;; an insignificant match in something that isn't
3760 ;; syntactic whitespace (i.e. strings or in nested
3761 ;; parentheses), then we can never skip more than a
3762 ;; single character from the match start position
3763 ;; (i.e. `state-pos' here) before continuing the
3764 ;; search. That since the look behind subexpression
3765 ;; might match the end of the insignificant region in
3766 ;; the next search.
3768 (cond
3769 ((elt check-state 7)
3770 ;; Match inside a line comment. Skip to eol. Use
3771 ;; `re-search-forward' instead of `skip-chars-forward' to get
3772 ;; the right bound behavior.
3773 (re-search-forward "[\n\r]" bound noerror))
3775 ((elt check-state 4)
3776 ;; Match inside a block comment. Skip to the '*/'.
3777 (search-forward "*/" bound noerror))
3779 ((and (not (elt check-state 5))
3780 (eq (char-before check-pos) ?/)
3781 (not (c-get-char-property (1- check-pos) 'syntax-table))
3782 (memq (char-after check-pos) '(?/ ?*)))
3783 ;; Match in the middle of the opener of a block or line
3784 ;; comment.
3785 (if (= (char-after check-pos) ?/)
3786 (re-search-forward "[\n\r]" bound noerror)
3787 (search-forward "*/" bound noerror)))
3789 ;; The last `parse-partial-sexp' above might have
3790 ;; stopped short of the real check position if the end
3791 ;; of the current sexp was encountered in paren-level
3792 ;; mode. The checks above are always false in that
3793 ;; case, and since they can do better skipping in
3794 ;; lookbehind-submatch mode, we do them before
3795 ;; checking the paren level.
3797 ((and paren-level
3798 (/= (setq tmp (car check-state)) 0))
3799 ;; Check the paren level first since we're short of the
3800 ;; syntactic checking position if the end of the
3801 ;; current sexp was encountered by `parse-partial-sexp'.
3802 (if (> tmp 0)
3804 ;; Inside a nested paren sexp.
3805 (if lookbehind-submatch
3806 ;; See the NOTE above.
3807 (progn (goto-char state-pos) t)
3808 ;; Skip out of the paren quickly.
3809 (setq state (parse-partial-sexp state-pos bound 0 nil state)
3810 state-pos (point)))
3812 ;; Have exited the current paren sexp.
3813 (if noerror
3814 (progn
3815 ;; The last `parse-partial-sexp' call above
3816 ;; has left us just after the closing paren
3817 ;; in this case, so we can modify the bound
3818 ;; to leave the point at the right position
3819 ;; upon return.
3820 (setq bound (1- (point)))
3821 nil)
3822 (signal 'search-failed (list regexp)))))
3824 ((setq tmp (elt check-state 3))
3825 ;; Match inside a string.
3826 (if (or lookbehind-submatch
3827 (not (integerp tmp)))
3828 ;; See the NOTE above.
3829 (progn (goto-char state-pos) t)
3830 ;; Skip to the end of the string before continuing.
3831 (let ((ender (make-string 1 tmp)) (continue t))
3832 (while (if (search-forward ender bound noerror)
3833 (progn
3834 (setq state (parse-partial-sexp
3835 state-pos (point) nil nil state)
3836 state-pos (point))
3837 (elt state 3))
3838 (setq continue nil)))
3839 continue)))
3841 ((save-excursion
3842 (save-match-data
3843 (c-beginning-of-macro start)))
3844 ;; Match inside a macro. Skip to the end of it.
3845 (c-end-of-macro)
3846 (cond ((<= (point) bound) t)
3847 (noerror nil)
3848 (t (signal 'search-failed (list regexp)))))
3850 ((and not-inside-token
3851 (or (< check-pos last-token-end-pos)
3852 (< check-pos
3853 (save-excursion
3854 (goto-char check-pos)
3855 (save-match-data
3856 (c-end-of-current-token last-token-end-pos))
3857 (setq last-token-end-pos (point))))))
3858 ;; Inside a token.
3859 (if lookbehind-submatch
3860 ;; See the NOTE above.
3861 (goto-char state-pos)
3862 (goto-char (min last-token-end-pos bound))))
3865 ;; A real match.
3866 (setq found t)
3867 nil)))
3869 ;; Should loop to search again, but take care to avoid
3870 ;; looping on the same spot.
3871 (or (/= search-pos (point))
3872 (if (= (point) bound)
3873 (if noerror
3875 (signal 'search-failed (list regexp)))
3876 (forward-char)
3877 t))))
3879 (error
3880 (goto-char start)
3881 (signal (car err) (cdr err))))
3883 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3885 (if found
3886 (progn
3887 (goto-char (match-end 0))
3888 (match-end 0))
3890 ;; Search failed. Set point as appropriate.
3891 (if (eq noerror t)
3892 (goto-char start)
3893 (goto-char bound))
3894 nil)))
3896 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
3898 (defsubst c-ssb-lit-begin ()
3899 ;; Return the start of the literal point is in, or nil.
3900 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3901 ;; bound in the caller.
3903 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3904 ;; if it's outside comments and strings.
3905 (save-excursion
3906 (let ((pos (point)) safe-pos state pps-end-pos)
3907 ;; Pick a safe position as close to the point as possible.
3909 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3910 ;; position.
3912 (while (and safe-pos-list
3913 (> (car safe-pos-list) (point)))
3914 (setq safe-pos-list (cdr safe-pos-list)))
3915 (unless (setq safe-pos (car-safe safe-pos-list))
3916 (setq safe-pos (max (or (c-safe-position
3917 (point) (or c-state-cache
3918 (c-parse-state)))
3920 (point-min))
3921 safe-pos-list (list safe-pos)))
3923 ;; Cache positions along the way to use if we have to back up more. We
3924 ;; cache every closing paren on the same level. If the paren cache is
3925 ;; relevant in this region then we're typically already on the same
3926 ;; level as the target position. Note that we might cache positions
3927 ;; after opening parens in case safe-pos is in a nested list. That's
3928 ;; both uncommon and harmless.
3929 (while (progn
3930 (setq state (parse-partial-sexp
3931 safe-pos pos 0))
3932 (< (point) pos))
3933 (setq safe-pos (point)
3934 safe-pos-list (cons safe-pos safe-pos-list)))
3936 ;; If the state contains the start of the containing sexp we cache that
3937 ;; position too, so that parse-partial-sexp in the next run has a bigger
3938 ;; chance of starting at the same level as the target position and thus
3939 ;; will get more good safe positions into the list.
3940 (if (elt state 1)
3941 (setq safe-pos (1+ (elt state 1))
3942 safe-pos-list (cons safe-pos safe-pos-list)))
3944 (if (or (elt state 3) (elt state 4))
3945 ;; Inside string or comment. Continue search at the
3946 ;; beginning of it.
3947 (elt state 8)))))
3949 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3950 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3951 i.e. don't stop at positions inside syntactic whitespace or string
3952 literals. Preprocessor directives are also ignored, with the exception
3953 of the one that the point starts within, if any. If LIMIT is given,
3954 it's assumed to be at a syntactically relevant position.
3956 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3957 sexps, and the search will also not go outside the current paren sexp.
3958 However, if LIMIT or the buffer limit is reached inside a nested paren
3959 then the point will be left at the limit.
3961 Non-nil is returned if the point moved, nil otherwise.
3963 Note that this function might do hidden buffer changes. See the
3964 comment at the start of cc-engine.el for more info."
3966 (let ((start (point))
3967 state-2
3968 ;; A list of syntactically relevant positions in descending
3969 ;; order. It's used to avoid scanning repeatedly over
3970 ;; potentially large regions with `parse-partial-sexp' to verify
3971 ;; each position. Used in `c-ssb-lit-begin'
3972 safe-pos-list
3973 ;; The result from `c-beginning-of-macro' at the start position or the
3974 ;; start position itself if it isn't within a macro. Evaluated on
3975 ;; demand.
3976 start-macro-beg
3977 ;; The earliest position after the current one with the same paren
3978 ;; level. Used only when `paren-level' is set.
3979 lit-beg
3980 (paren-level-pos (point)))
3982 (while
3983 (progn
3984 ;; The next loop "tries" to find the end point each time round,
3985 ;; loops when it hasn't succeeded.
3986 (while
3987 (and
3988 (< (skip-chars-backward skip-chars limit) 0)
3990 (let ((pos (point)) state-2 pps-end-pos)
3992 (cond
3993 ;; Don't stop inside a literal
3994 ((setq lit-beg (c-ssb-lit-begin))
3995 (goto-char lit-beg)
3998 ((and paren-level
3999 (save-excursion
4000 (setq state-2 (parse-partial-sexp
4001 pos paren-level-pos -1)
4002 pps-end-pos (point))
4003 (/= (car state-2) 0)))
4004 ;; Not at the right level.
4006 (if (and (< (car state-2) 0)
4007 ;; We stop above if we go out of a paren.
4008 ;; Now check whether it precedes or is
4009 ;; nested in the starting sexp.
4010 (save-excursion
4011 (setq state-2
4012 (parse-partial-sexp
4013 pps-end-pos paren-level-pos
4014 nil nil state-2))
4015 (< (car state-2) 0)))
4017 ;; We've stopped short of the starting position
4018 ;; so the hit was inside a nested list. Go up
4019 ;; until we are at the right level.
4020 (condition-case nil
4021 (progn
4022 (goto-char (scan-lists pos -1
4023 (- (car state-2))))
4024 (setq paren-level-pos (point))
4025 (if (and limit (>= limit paren-level-pos))
4026 (progn
4027 (goto-char limit)
4028 nil)
4030 (error
4031 (goto-char (or limit (point-min)))
4032 nil))
4034 ;; The hit was outside the list at the start
4035 ;; position. Go to the start of the list and exit.
4036 (goto-char (1+ (elt state-2 1)))
4037 nil))
4039 ((c-beginning-of-macro limit)
4040 ;; Inside a macro.
4041 (if (< (point)
4042 (or start-macro-beg
4043 (setq start-macro-beg
4044 (save-excursion
4045 (goto-char start)
4046 (c-beginning-of-macro limit)
4047 (point)))))
4050 ;; It's inside the same macro we started in so it's
4051 ;; a relevant match.
4052 (goto-char pos)
4053 nil))))))
4055 (> (point)
4056 (progn
4057 ;; Skip syntactic ws afterwards so that we don't stop at the
4058 ;; end of a comment if `skip-chars' is something like "^/".
4059 (c-backward-syntactic-ws)
4060 (point)))))
4062 ;; We might want to extend this with more useful return values in
4063 ;; the future.
4064 (/= (point) start)))
4066 ;; The following is an alternative implementation of
4067 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4068 ;; track of the syntactic context. It turned out to be generally
4069 ;; slower than the one above which uses forward checks from earlier
4070 ;; safe positions.
4072 ;;(defconst c-ssb-stop-re
4073 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4074 ;; ;; stop at to avoid going into comments and literals.
4075 ;; (concat
4076 ;; ;; Match comment end syntax and string literal syntax. Also match
4077 ;; ;; '/' for block comment endings (not covered by comment end
4078 ;; ;; syntax).
4079 ;; "\\s>\\|/\\|\\s\""
4080 ;; (if (memq 'gen-string-delim c-emacs-features)
4081 ;; "\\|\\s|"
4082 ;; "")
4083 ;; (if (memq 'gen-comment-delim c-emacs-features)
4084 ;; "\\|\\s!"
4085 ;; "")))
4087 ;;(defconst c-ssb-stop-paren-re
4088 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4089 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4091 ;;(defconst c-ssb-sexp-end-re
4092 ;; ;; Regexp matching the ending syntax of a complex sexp.
4093 ;; (concat c-string-limit-regexp "\\|\\s)"))
4095 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4096 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4097 ;;i.e. don't stop at positions inside syntactic whitespace or string
4098 ;;literals. Preprocessor directives are also ignored. However, if the
4099 ;;point is within a comment, string literal or preprocessor directory to
4100 ;;begin with, its contents is treated as syntactically relevant chars.
4101 ;;If LIMIT is given, it limits the backward search and the point will be
4102 ;;left there if no earlier position is found.
4104 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4105 ;;sexps, and the search will also not go outside the current paren sexp.
4106 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4107 ;;then the point will be left at the limit.
4109 ;;Non-nil is returned if the point moved, nil otherwise.
4111 ;;Note that this function might do hidden buffer changes. See the
4112 ;;comment at the start of cc-engine.el for more info."
4114 ;; (save-restriction
4115 ;; (when limit
4116 ;; (narrow-to-region limit (point-max)))
4118 ;; (let ((start (point)))
4119 ;; (catch 'done
4120 ;; (while (let ((last-pos (point))
4121 ;; (stop-pos (progn
4122 ;; (skip-chars-backward skip-chars)
4123 ;; (point))))
4125 ;; ;; Skip back over the same region as
4126 ;; ;; `skip-chars-backward' above, but keep to
4127 ;; ;; syntactically relevant positions.
4128 ;; (goto-char last-pos)
4129 ;; (while (and
4130 ;; ;; `re-search-backward' with a single char regexp
4131 ;; ;; should be fast.
4132 ;; (re-search-backward
4133 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4134 ;; stop-pos 'move)
4136 ;; (progn
4137 ;; (cond
4138 ;; ((looking-at "\\s(")
4139 ;; ;; `paren-level' is set and we've found the
4140 ;; ;; start of the containing paren.
4141 ;; (forward-char)
4142 ;; (throw 'done t))
4144 ;; ((looking-at c-ssb-sexp-end-re)
4145 ;; ;; We're at the end of a string literal or paren
4146 ;; ;; sexp (if `paren-level' is set).
4147 ;; (forward-char)
4148 ;; (condition-case nil
4149 ;; (c-backward-sexp)
4150 ;; (error
4151 ;; (goto-char limit)
4152 ;; (throw 'done t))))
4154 ;; (t
4155 ;; (forward-char)
4156 ;; ;; At the end of some syntactic ws or possibly
4157 ;; ;; after a plain '/' operator.
4158 ;; (let ((pos (point)))
4159 ;; (c-backward-syntactic-ws)
4160 ;; (if (= pos (point))
4161 ;; ;; Was a plain '/' operator. Go past it.
4162 ;; (backward-char)))))
4164 ;; (> (point) stop-pos))))
4166 ;; ;; Now the point is either at `stop-pos' or at some
4167 ;; ;; position further back if `stop-pos' was at a
4168 ;; ;; syntactically irrelevant place.
4170 ;; ;; Skip additional syntactic ws so that we don't stop
4171 ;; ;; at the end of a comment if `skip-chars' is
4172 ;; ;; something like "^/".
4173 ;; (c-backward-syntactic-ws)
4175 ;; (< (point) stop-pos))))
4177 ;; ;; We might want to extend this with more useful return values
4178 ;; ;; in the future.
4179 ;; (/= (point) start))))
4182 ;; Tools for handling comments and string literals.
4184 (defun c-slow-in-literal (&optional lim detect-cpp)
4185 "Return the type of literal point is in, if any.
4186 The return value is `c' if in a C-style comment, `c++' if in a C++
4187 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4188 is non-nil and in a preprocessor line, or nil if somewhere else.
4189 Optional LIM is used as the backward limit of the search. If omitted,
4190 or nil, `c-beginning-of-defun' is used.
4192 The last point calculated is cached if the cache is enabled, i.e. if
4193 `c-in-literal-cache' is bound to a two element vector.
4195 Note that this function might do hidden buffer changes. See the
4196 comment at the start of cc-engine.el for more info."
4198 (if (and (vectorp c-in-literal-cache)
4199 (= (point) (aref c-in-literal-cache 0)))
4200 (aref c-in-literal-cache 1)
4201 (let ((rtn (save-excursion
4202 (let* ((pos (point))
4203 (lim (or lim (progn
4204 (c-beginning-of-syntax)
4205 (point))))
4206 (state (parse-partial-sexp lim pos)))
4207 (cond
4208 ((elt state 3) 'string)
4209 ((elt state 4) (if (elt state 7) 'c++ 'c))
4210 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
4211 (t nil))))))
4212 ;; cache this result if the cache is enabled
4213 (if (not c-in-literal-cache)
4214 (setq c-in-literal-cache (vector (point) rtn)))
4215 rtn)))
4217 ;; XEmacs has a built-in function that should make this much quicker.
4218 ;; I don't think we even need the cache, which makes our lives more
4219 ;; complicated anyway. In this case, lim is only used to detect
4220 ;; cpp directives.
4222 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
4223 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
4224 ;; Xemacs 21.4.4. It manifested itself thus:
4226 ;; Starting with an empty AWK Mode buffer, type
4227 ;; /regexp/ {<C-j>
4228 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
4230 ;; AWK Mode is designed such that when the first / is typed, it gets the
4231 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
4232 ;; are given the s-t property "string". However, buffer-syntactic-context
4233 ;; fails to take account of the change of the s-t property on the opening / to
4234 ;; "string", and reports that the { is within a string started by the second /.
4236 ;; The workaround for this is for the AWK Mode initialisation to switch the
4237 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
4238 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
4240 ;; (Alan Mackenzie, 2003/4/30).
4242 (defun c-fast-in-literal (&optional lim detect-cpp)
4243 ;; This function might do hidden buffer changes.
4244 (let ((context (buffer-syntactic-context)))
4245 (cond
4246 ((eq context 'string) 'string)
4247 ((eq context 'comment) 'c++)
4248 ((eq context 'block-comment) 'c)
4249 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
4251 (defalias 'c-in-literal
4252 (if (fboundp 'buffer-syntactic-context)
4253 'c-fast-in-literal ; XEmacs
4254 'c-slow-in-literal)) ; GNU Emacs
4256 ;; The defalias above isn't enough to shut up the byte compiler.
4257 (cc-bytecomp-defun c-in-literal)
4259 (defun c-literal-limits (&optional lim near not-in-delimiter)
4260 "Return a cons of the beginning and end positions of the comment or
4261 string surrounding point (including both delimiters), or nil if point
4262 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4263 to start parsing from. If NEAR is non-nil, then the limits of any
4264 literal next to point is returned. \"Next to\" means there's only
4265 spaces and tabs between point and the literal. The search for such a
4266 literal is done first in forward direction. If NOT-IN-DELIMITER is
4267 non-nil, the case when point is inside a starting delimiter won't be
4268 recognized. This only has effect for comments which have starting
4269 delimiters with more than one character.
4271 Note that this function might do hidden buffer changes. See the
4272 comment at the start of cc-engine.el for more info."
4274 (save-excursion
4275 (let* ((pos (point))
4276 (lim (or lim (progn
4277 (c-beginning-of-syntax)
4278 (point))))
4279 (state (parse-partial-sexp lim pos)))
4281 (cond ((elt state 3) ; String.
4282 (goto-char (elt state 8))
4283 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4284 (point-max))))
4286 ((elt state 4) ; Comment.
4287 (goto-char (elt state 8))
4288 (cons (point) (progn (c-forward-single-comment) (point))))
4290 ((and (not not-in-delimiter)
4291 (not (elt state 5))
4292 (eq (char-before) ?/)
4293 (looking-at "[/*]"))
4294 ;; We're standing in a comment starter.
4295 (backward-char 1)
4296 (cons (point) (progn (c-forward-single-comment) (point))))
4298 (near
4299 (goto-char pos)
4301 ;; Search forward for a literal.
4302 (skip-chars-forward " \t")
4304 (cond
4305 ((looking-at c-string-limit-regexp) ; String.
4306 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4307 (point-max))))
4309 ((looking-at c-comment-start-regexp) ; Line or block comment.
4310 (cons (point) (progn (c-forward-single-comment) (point))))
4313 ;; Search backward.
4314 (skip-chars-backward " \t")
4316 (let ((end (point)) beg)
4317 (cond
4318 ((save-excursion
4319 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4320 (setq beg (c-safe (c-backward-sexp 1) (point))))
4322 ((and (c-safe (forward-char -2) t)
4323 (looking-at "*/"))
4324 ;; Block comment. Due to the nature of line
4325 ;; comments, they will always be covered by the
4326 ;; normal case above.
4327 (goto-char end)
4328 (c-backward-single-comment)
4329 ;; If LIM is bogus, beg will be bogus.
4330 (setq beg (point))))
4332 (if beg (cons beg end))))))
4333 ))))
4335 ;; In case external callers use this; it did have a docstring.
4336 (defalias 'c-literal-limits-fast 'c-literal-limits)
4338 (defun c-collect-line-comments (range)
4339 "If the argument is a cons of two buffer positions (such as returned by
4340 `c-literal-limits'), and that range contains a C++ style line comment,
4341 then an extended range is returned that contains all adjacent line
4342 comments (i.e. all comments that starts in the same column with no
4343 empty lines or non-whitespace characters between them). Otherwise the
4344 argument is returned.
4346 Note that this function might do hidden buffer changes. See the
4347 comment at the start of cc-engine.el for more info."
4349 (save-excursion
4350 (condition-case nil
4351 (if (and (consp range) (progn
4352 (goto-char (car range))
4353 (looking-at c-line-comment-starter)))
4354 (let ((col (current-column))
4355 (beg (point))
4356 (bopl (c-point 'bopl))
4357 (end (cdr range)))
4358 ;; Got to take care in the backward direction to handle
4359 ;; comments which are preceded by code.
4360 (while (and (c-backward-single-comment)
4361 (>= (point) bopl)
4362 (looking-at c-line-comment-starter)
4363 (= col (current-column)))
4364 (setq beg (point)
4365 bopl (c-point 'bopl)))
4366 (goto-char end)
4367 (while (and (progn (skip-chars-forward " \t")
4368 (looking-at c-line-comment-starter))
4369 (= col (current-column))
4370 (prog1 (zerop (forward-line 1))
4371 (setq end (point)))))
4372 (cons beg end))
4373 range)
4374 (error range))))
4376 (defun c-literal-type (range)
4377 "Convenience function that given the result of `c-literal-limits',
4378 returns nil or the type of literal that the range surrounds, one
4379 of the symbols 'c, 'c++ or 'string. It's much faster than using
4380 `c-in-literal' and is intended to be used when you need both the
4381 type of a literal and its limits.
4383 Note that this function might do hidden buffer changes. See the
4384 comment at the start of cc-engine.el for more info."
4386 (if (consp range)
4387 (save-excursion
4388 (goto-char (car range))
4389 (cond ((looking-at c-string-limit-regexp) 'string)
4390 ((or (looking-at "//") ; c++ line comment
4391 (and (looking-at "\\s<") ; comment starter
4392 (looking-at "#"))) ; awk comment.
4393 'c++)
4394 (t 'c))) ; Assuming the range is valid.
4395 range))
4398 ;; `c-find-decl-spots' and accompanying stuff.
4400 ;; Variables used in `c-find-decl-spots' to cache the search done for
4401 ;; the first declaration in the last call. When that function starts,
4402 ;; it needs to back up over syntactic whitespace to look at the last
4403 ;; token before the region being searched. That can sometimes cause
4404 ;; moves back and forth over a quite large region of comments and
4405 ;; macros, which would be repeated for each changed character when
4406 ;; we're called during fontification, since font-lock refontifies the
4407 ;; current line for each change. Thus it's worthwhile to cache the
4408 ;; first match.
4410 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4411 ;; the syntactic whitespace less or equal to some start position.
4412 ;; There's no cached value if it's nil.
4414 ;; `c-find-decl-match-pos' is the match position if
4415 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4416 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4417 (defvar c-find-decl-syntactic-pos nil)
4418 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4419 (defvar c-find-decl-match-pos nil)
4420 (make-variable-buffer-local 'c-find-decl-match-pos)
4422 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4423 (and c-find-decl-syntactic-pos
4424 (< change-min-pos c-find-decl-syntactic-pos)
4425 (setq c-find-decl-syntactic-pos nil)))
4427 ; (defface c-debug-decl-spot-face
4428 ; '((t (:background "Turquoise")))
4429 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4430 ; (defface c-debug-decl-sws-face
4431 ; '((t (:background "Khaki")))
4432 ; "Debug face to mark the syntactic whitespace between the declaration
4433 ; spots and the preceding token end.")
4435 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4436 (when (facep 'c-debug-decl-spot-face)
4437 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4438 (c-debug-add-face (max match-pos (point-min)) decl-pos
4439 'c-debug-decl-sws-face)
4440 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4441 'c-debug-decl-spot-face))))
4442 (defmacro c-debug-remove-decl-spot-faces (beg end)
4443 (when (facep 'c-debug-decl-spot-face)
4444 `(c-save-buffer-state ()
4445 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4446 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4448 (defmacro c-find-decl-prefix-search ()
4449 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4450 ;; but it contains lots of free variables that refer to things
4451 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4452 ;; if there is a match, otherwise at `cfd-limit'.
4454 ;; This macro might do hidden buffer changes.
4456 '(progn
4457 ;; Find the next property match position if we haven't got one already.
4458 (unless cfd-prop-match
4459 (save-excursion
4460 (while (progn
4461 (goto-char (next-single-property-change
4462 (point) 'c-type nil cfd-limit))
4463 (and (< (point) cfd-limit)
4464 (not (eq (c-get-char-property (1- (point)) 'c-type)
4465 'c-decl-end)))))
4466 (setq cfd-prop-match (point))))
4468 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4469 ;; got one already.
4470 (unless cfd-re-match
4472 (if (> cfd-re-match-end (point))
4473 (goto-char cfd-re-match-end))
4475 (while (if (setq cfd-re-match-end
4476 (re-search-forward c-decl-prefix-or-start-re
4477 cfd-limit 'move))
4479 ;; Match. Check if it's inside a comment or string literal.
4480 (c-got-face-at
4481 (if (setq cfd-re-match (match-end 1))
4482 ;; Matched the end of a token preceding a decl spot.
4483 (progn
4484 (goto-char cfd-re-match)
4485 (1- cfd-re-match))
4486 ;; Matched a token that start a decl spot.
4487 (goto-char (match-beginning 0))
4488 (point))
4489 c-literal-faces)
4491 ;; No match. Finish up and exit the loop.
4492 (setq cfd-re-match cfd-limit)
4493 nil)
4495 ;; Skip out of comments and string literals.
4496 (while (progn
4497 (goto-char (next-single-property-change
4498 (point) 'face nil cfd-limit))
4499 (and (< (point) cfd-limit)
4500 (c-got-face-at (point) c-literal-faces)))))
4502 ;; If we matched at the decl start, we have to back up over the
4503 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4504 ;; any decl spots in the syntactic ws.
4505 (unless cfd-re-match
4506 (c-backward-syntactic-ws)
4507 (setq cfd-re-match (point))))
4509 ;; Choose whichever match is closer to the start.
4510 (if (< cfd-re-match cfd-prop-match)
4511 (setq cfd-match-pos cfd-re-match
4512 cfd-re-match nil)
4513 (setq cfd-match-pos cfd-prop-match
4514 cfd-prop-match nil))
4516 (goto-char cfd-match-pos)
4518 (when (< cfd-match-pos cfd-limit)
4519 ;; Skip forward past comments only so we don't skip macros.
4520 (c-forward-comments)
4521 ;; Set the position to continue at. We can avoid going over
4522 ;; the comments skipped above a second time, but it's possible
4523 ;; that the comment skipping has taken us past `cfd-prop-match'
4524 ;; since the property might be used inside comments.
4525 (setq cfd-continue-pos (if cfd-prop-match
4526 (min cfd-prop-match (point))
4527 (point))))))
4529 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4530 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4531 ;; label from the point to CFD-LIMIT.
4533 ;; CFD-FUN is called with point at the start of the spot. It's
4534 ;; passed two arguments: The first is the end position of the token
4535 ;; preceding the spot, or 0 for the implicit match at bob. The
4536 ;; second is a flag that is t when the match is inside a macro. If
4537 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
4538 ;; spot, it should return non-nil to ensure that the next search
4539 ;; will find them.
4541 ;; Such a spot is:
4542 ;; o The first token after bob.
4543 ;; o The first token after the end of submatch 1 in
4544 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4545 ;; o The start of each `c-decl-prefix-or-start-re' match when
4546 ;; submatch 1 doesn't match.
4547 ;; o The first token after the end of each occurrence of the
4548 ;; `c-type' text property with the value `c-decl-end', provided
4549 ;; `c-type-decl-end-used' is set.
4551 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4552 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4553 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4555 ;; If the match is inside a macro then the buffer is narrowed to the
4556 ;; end of it, so that CFD-FUN can investigate the following tokens
4557 ;; without matching something that begins inside a macro and ends
4558 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4559 ;; CFD-FACE-CHECKLIST checks exist.
4561 ;; The spots are visited approximately in order from top to bottom.
4562 ;; It's however the positions where `c-decl-prefix-or-start-re'
4563 ;; matches and where `c-decl-end' properties are found that are in
4564 ;; order. Since the spots often are at the following token, they
4565 ;; might be visited out of order insofar as more spots are reported
4566 ;; later on within the syntactic whitespace between the match
4567 ;; positions and their spots.
4569 ;; It's assumed that comments and strings are fontified in the
4570 ;; searched range.
4572 ;; This is mainly used in fontification, and so has an elaborate
4573 ;; cache to handle repeated calls from the same start position; see
4574 ;; the variables above.
4576 ;; All variables in this function begin with `cfd-' to avoid name
4577 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4579 ;; This function might do hidden buffer changes.
4581 (let ((cfd-start-pos (point))
4582 (cfd-buffer-end (point-max))
4583 ;; The end of the token preceding the decl spot last found
4584 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4585 ;; no match.
4586 cfd-re-match
4587 ;; The end position of the last `c-decl-prefix-or-start-re'
4588 ;; match. If this is greater than `cfd-continue-pos', the
4589 ;; next regexp search is started here instead.
4590 (cfd-re-match-end (point-min))
4591 ;; The end of the last `c-decl-end' found by
4592 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4593 ;; match. If searching for the property isn't needed then we
4594 ;; disable it by setting it to `cfd-limit' directly.
4595 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4596 ;; The end of the token preceding the decl spot last found by
4597 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4598 ;; bob. `cfd-limit' if there's no match. In other words,
4599 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4600 (cfd-match-pos cfd-limit)
4601 ;; The position to continue searching at.
4602 cfd-continue-pos
4603 ;; The position of the last "real" token we've stopped at.
4604 ;; This can be greater than `cfd-continue-pos' when we get
4605 ;; hits inside macros or at `c-decl-end' positions inside
4606 ;; comments.
4607 (cfd-token-pos 0)
4608 ;; The end position of the last entered macro.
4609 (cfd-macro-end 0))
4611 ;; Initialize by finding a syntactically relevant start position
4612 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4613 ;; search unless we're at bob.
4615 (let (start-in-literal start-in-macro syntactic-pos)
4616 ;; Must back up a bit since we look for the end of the previous
4617 ;; statement or declaration, which is earlier than the first
4618 ;; returned match.
4620 (cond
4621 ;; First we need to move to a syntactically relevant position.
4622 ;; Begin by backing out of comment or string literals.
4623 ((and
4624 (when (c-got-face-at (point) c-literal-faces)
4625 ;; Try to use the faces to back up to the start of the
4626 ;; literal. FIXME: What if the point is on a declaration
4627 ;; inside a comment?
4628 (while (and (not (bobp))
4629 (c-got-face-at (1- (point)) c-literal-faces))
4630 (goto-char (previous-single-property-change
4631 (point) 'face nil (point-min))))
4633 ;; XEmacs doesn't fontify the quotes surrounding string
4634 ;; literals.
4635 (and (featurep 'xemacs)
4636 (eq (get-text-property (point) 'face)
4637 'font-lock-string-face)
4638 (not (bobp))
4639 (progn (backward-char)
4640 (not (looking-at c-string-limit-regexp)))
4641 (forward-char))
4643 ;; Don't trust the literal to contain only literal faces
4644 ;; (the font lock package might not have fontified the
4645 ;; start of it at all, for instance) so check that we have
4646 ;; arrived at something that looks like a start or else
4647 ;; resort to `c-literal-limits'.
4648 (unless (looking-at c-literal-start-regexp)
4649 (let ((range (c-literal-limits)))
4650 (if range (goto-char (car range)))))
4652 (setq start-in-literal (point)))
4654 ;; The start is in a literal. If the limit is in the same
4655 ;; one we don't have to find a syntactic position etc. We
4656 ;; only check that if the limit is at or before bonl to save
4657 ;; time; it covers the by far most common case when font-lock
4658 ;; refontifies the current line only.
4659 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4660 (save-excursion
4661 (goto-char cfd-start-pos)
4662 (while (progn
4663 (goto-char (next-single-property-change
4664 (point) 'face nil cfd-limit))
4665 (and (< (point) cfd-limit)
4666 (c-got-face-at (point) c-literal-faces))))
4667 (= (point) cfd-limit)))
4669 ;; Completely inside a literal. Set up variables to trig the
4670 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4671 ;; find a suitable start position.
4672 (setq cfd-continue-pos start-in-literal))
4674 ;; Check if the region might be completely inside a macro, to
4675 ;; optimize that like the completely-inside-literal above.
4676 ((save-excursion
4677 (and (= (forward-line 1) 0)
4678 (bolp) ; forward-line has funny behavior at eob.
4679 (>= (point) cfd-limit)
4680 (progn (backward-char)
4681 (eq (char-before) ?\\))))
4682 ;; (Maybe) completely inside a macro. Only need to trig the
4683 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4684 ;; set things up.
4685 (setq cfd-continue-pos (1- cfd-start-pos)
4686 start-in-macro t))
4689 ;; Back out of any macro so we don't miss any declaration
4690 ;; that could follow after it.
4691 (when (c-beginning-of-macro)
4692 (setq start-in-macro t))
4694 ;; Now we're at a proper syntactically relevant position so we
4695 ;; can use the cache. But first clear it if it applied
4696 ;; further down.
4697 (c-invalidate-find-decl-cache cfd-start-pos)
4699 (setq syntactic-pos (point))
4700 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
4701 ;; Don't have to do this if the cache is relevant here,
4702 ;; typically if the same line is refontified again. If
4703 ;; we're just some syntactic whitespace further down we can
4704 ;; still use the cache to limit the skipping.
4705 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
4707 ;; If we hit `c-find-decl-syntactic-pos' and
4708 ;; `c-find-decl-match-pos' is set then we install the cached
4709 ;; values. If we hit `c-find-decl-syntactic-pos' and
4710 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4711 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4712 ;; and so we can continue the search from this point. If we
4713 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4714 ;; the right spot to begin searching anyway.
4715 (if (and (eq (point) c-find-decl-syntactic-pos)
4716 c-find-decl-match-pos)
4717 (setq cfd-match-pos c-find-decl-match-pos
4718 cfd-continue-pos syntactic-pos)
4720 (setq c-find-decl-syntactic-pos syntactic-pos)
4722 (when (if (bobp)
4723 ;; Always consider bob a match to get the first
4724 ;; declaration in the file. Do this separately instead of
4725 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4726 ;; regexp always can consume at least one character to
4727 ;; ensure that we won't get stuck in an infinite loop.
4728 (setq cfd-re-match 0)
4729 (backward-char)
4730 (c-beginning-of-current-token)
4731 (< (point) cfd-limit))
4732 ;; Do an initial search now. In the bob case above it's
4733 ;; only done to search for a `c-decl-end' spot.
4734 (c-find-decl-prefix-search))
4736 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
4737 cfd-match-pos)))))
4739 ;; Advance `cfd-continue-pos' if it's before the start position.
4740 ;; The closest continue position that might have effect at or
4741 ;; after the start depends on what we started in. This also
4742 ;; finds a suitable start position in the special cases when the
4743 ;; region is completely within a literal or macro.
4744 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
4746 (cond
4747 (start-in-macro
4748 ;; If we're in a macro then it's the closest preceding token
4749 ;; in the macro. Check this before `start-in-literal',
4750 ;; since if we're inside a literal in a macro, the preceding
4751 ;; token is earlier than any `c-decl-end' spot inside the
4752 ;; literal (comment).
4753 (goto-char (or start-in-literal cfd-start-pos))
4754 ;; The only syntactic ws in macros are comments.
4755 (c-backward-comments)
4756 (backward-char)
4757 (c-beginning-of-current-token))
4759 (start-in-literal
4760 ;; If we're in a comment it can only be the closest
4761 ;; preceding `c-decl-end' position within that comment, if
4762 ;; any. Go back to the beginning of such a property so that
4763 ;; `c-find-decl-prefix-search' will find the end of it.
4764 ;; (Can't stop at the end and install it directly on
4765 ;; `cfd-prop-match' since that variable might be cleared
4766 ;; after `cfd-fun' below.)
4768 ;; Note that if the literal is a string then the property
4769 ;; search will simply skip to the beginning of it right
4770 ;; away.
4771 (if (not c-type-decl-end-used)
4772 (goto-char start-in-literal)
4773 (goto-char cfd-start-pos)
4774 (while (progn
4775 (goto-char (previous-single-property-change
4776 (point) 'c-type nil start-in-literal))
4777 (and (> (point) start-in-literal)
4778 (not (eq (c-get-char-property (point) 'c-type)
4779 'c-decl-end))))))
4781 (when (= (point) start-in-literal)
4782 ;; Didn't find any property inside the comment, so we can
4783 ;; skip it entirely. (This won't skip past a string, but
4784 ;; that'll be handled quickly by the next
4785 ;; `c-find-decl-prefix-search' anyway.)
4786 (c-forward-single-comment)
4787 (if (> (point) cfd-limit)
4788 (goto-char cfd-limit))))
4791 ;; If we started in normal code, the only match that might
4792 ;; apply before the start is what we already got in
4793 ;; `cfd-match-pos' so we can continue at the start position.
4794 ;; (Note that we don't get here if the first match is below
4795 ;; it.)
4796 (goto-char cfd-start-pos)))
4798 ;; Delete found matches if they are before our new continue
4799 ;; position, so that `c-find-decl-prefix-search' won't back up
4800 ;; to them later on.
4801 (setq cfd-continue-pos (point))
4802 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
4803 (setq cfd-re-match nil))
4804 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
4805 (setq cfd-prop-match nil)))
4807 (if syntactic-pos
4808 ;; This is the normal case and we got a proper syntactic
4809 ;; position. If there's a match then it's always outside
4810 ;; macros and comments, so advance to the next token and set
4811 ;; `cfd-token-pos'. The loop below will later go back using
4812 ;; `cfd-continue-pos' to fix declarations inside the
4813 ;; syntactic ws.
4814 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
4815 (goto-char syntactic-pos)
4816 (c-forward-syntactic-ws)
4817 (and cfd-continue-pos
4818 (< cfd-continue-pos (point))
4819 (setq cfd-token-pos (point))))
4821 ;; Have one of the special cases when the region is completely
4822 ;; within a literal or macro. `cfd-continue-pos' is set to a
4823 ;; good start position for the search, so do it.
4824 (c-find-decl-prefix-search)))
4826 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4828 (while (progn
4829 (while (and
4830 (< cfd-match-pos cfd-limit)
4833 ;; Kludge to filter out matches on the "<" that
4834 ;; aren't open parens, for the sake of languages
4835 ;; that got `c-recognize-<>-arglists' set.
4836 (and (eq (char-before cfd-match-pos) ?<)
4837 (not (c-get-char-property (1- cfd-match-pos)
4838 'syntax-table)))
4840 ;; If `cfd-continue-pos' is less or equal to
4841 ;; `cfd-token-pos', we've got a hit inside a macro
4842 ;; that's in the syntactic whitespace before the last
4843 ;; "real" declaration we've checked. If they're equal
4844 ;; we've arrived at the declaration a second time, so
4845 ;; there's nothing to do.
4846 (= cfd-continue-pos cfd-token-pos)
4848 (progn
4849 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4850 ;; we're still searching for declarations embedded in
4851 ;; the syntactic whitespace. In that case we need
4852 ;; only to skip comments and not macros, since they
4853 ;; can't be nested, and that's already been done in
4854 ;; `c-find-decl-prefix-search'.
4855 (when (> cfd-continue-pos cfd-token-pos)
4856 (c-forward-syntactic-ws)
4857 (setq cfd-token-pos (point)))
4859 ;; Continue if the following token fails the
4860 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4861 (when (or (>= (point) cfd-limit)
4862 (not (looking-at cfd-decl-re))
4863 (and cfd-face-checklist
4864 (not (c-got-face-at
4865 (point) cfd-face-checklist))))
4866 (goto-char cfd-continue-pos)
4867 t)))
4869 (< (point) cfd-limit))
4870 (c-find-decl-prefix-search))
4872 (< (point) cfd-limit))
4874 (when (and
4875 (>= (point) cfd-start-pos)
4877 (progn
4878 ;; Narrow to the end of the macro if we got a hit inside
4879 ;; one, to avoid recognizing things that start inside the
4880 ;; macro and end outside it.
4881 (when (> cfd-match-pos cfd-macro-end)
4882 ;; Not in the same macro as in the previous round.
4883 (save-excursion
4884 (goto-char cfd-match-pos)
4885 (setq cfd-macro-end
4886 (if (save-excursion (and (c-beginning-of-macro)
4887 (< (point) cfd-match-pos)))
4888 (progn (c-end-of-macro)
4889 (point))
4890 0))))
4892 (if (zerop cfd-macro-end)
4894 (if (> cfd-macro-end (point))
4895 (progn (narrow-to-region (point-min) cfd-macro-end)
4897 ;; The matched token was the last thing in the macro,
4898 ;; so the whole match is bogus.
4899 (setq cfd-macro-end 0)
4900 nil))))
4902 (c-debug-put-decl-spot-faces cfd-match-pos (point))
4903 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
4904 (setq cfd-prop-match nil))
4906 (when (/= cfd-macro-end 0)
4907 ;; Restore limits if we did macro narrowment above.
4908 (narrow-to-region (point-min) cfd-buffer-end)))
4910 (goto-char cfd-continue-pos)
4911 (if (= cfd-continue-pos cfd-limit)
4912 (setq cfd-match-pos cfd-limit)
4913 (c-find-decl-prefix-search)))))
4916 ;; A cache for found types.
4918 ;; Buffer local variable that contains an obarray with the types we've
4919 ;; found. If a declaration is recognized somewhere we record the
4920 ;; fully qualified identifier in it to recognize it as a type
4921 ;; elsewhere in the file too. This is not accurate since we do not
4922 ;; bother with the scoping rules of the languages, but in practice the
4923 ;; same name is seldom used as both a type and something else in a
4924 ;; file, and we only use this as a last resort in ambiguous cases (see
4925 ;; `c-forward-decl-or-cast-1').
4927 ;; Not every type need be in this cache. However, things which have
4928 ;; ceased to be types must be removed from it.
4930 ;; Template types in C++ are added here too but with the template
4931 ;; arglist replaced with "<>" in references or "<" for the one in the
4932 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4933 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4934 ;; template specs can be fairly sized programs in themselves) and
4935 ;; improves the hit ratio (it's a type regardless of the template
4936 ;; args; it's just not the same type, but we're only interested in
4937 ;; recognizing types, not telling distinct types apart). Note that
4938 ;; template types in references are added here too; from the example
4939 ;; above there will also be an entry "Foo<".
4940 (defvar c-found-types nil)
4941 (make-variable-buffer-local 'c-found-types)
4943 (defsubst c-clear-found-types ()
4944 ;; Clears `c-found-types'.
4945 (setq c-found-types (make-vector 53 0)))
4947 (defun c-add-type (from to)
4948 ;; Add the given region as a type in `c-found-types'. If the region
4949 ;; doesn't match an existing type but there is a type which is equal
4950 ;; to the given one except that the last character is missing, then
4951 ;; the shorter type is removed. That's done to avoid adding all
4952 ;; prefixes of a type as it's being entered and font locked. This
4953 ;; doesn't cover cases like when characters are removed from a type
4954 ;; or added in the middle. We'd need the position of point when the
4955 ;; font locking is invoked to solve this well.
4957 ;; This function might do hidden buffer changes.
4958 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4959 (unless (intern-soft type c-found-types)
4960 (unintern (substring type 0 -1) c-found-types)
4961 (intern type c-found-types))))
4963 (defun c-unfind-type (name)
4964 ;; Remove the "NAME" from c-found-types, if present.
4965 (unintern name c-found-types))
4967 (defsubst c-check-type (from to)
4968 ;; Return non-nil if the given region contains a type in
4969 ;; `c-found-types'.
4971 ;; This function might do hidden buffer changes.
4972 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4973 c-found-types))
4975 (defun c-list-found-types ()
4976 ;; Return all the types in `c-found-types' as a sorted list of
4977 ;; strings.
4978 (let (type-list)
4979 (mapatoms (lambda (type)
4980 (setq type-list (cons (symbol-name type)
4981 type-list)))
4982 c-found-types)
4983 (sort type-list 'string-lessp)))
4985 ;; Shut up the byte compiler.
4986 (defvar c-maybe-stale-found-type)
4988 (defun c-trim-found-types (beg end old-len)
4989 ;; An after change function which, in conjunction with the info in
4990 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4991 ;; from `c-found-types', should this type have become stale. For
4992 ;; example, this happens to "foo" when "foo \n bar();" becomes
4993 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4994 ;; the fontification.
4996 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4997 ;; type?
4998 (when (> end beg)
4999 (save-excursion
5000 (when (< end (point-max))
5001 (goto-char end)
5002 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
5003 (progn (goto-char end)
5004 (c-end-of-current-token)))
5005 (c-unfind-type (buffer-substring-no-properties
5006 end (point)))))
5007 (when (> beg (point-min))
5008 (goto-char beg)
5009 (if (and (c-end-of-current-token) ; only moves when we started in the middle
5010 (progn (goto-char beg)
5011 (c-beginning-of-current-token)))
5012 (c-unfind-type (buffer-substring-no-properties
5013 (point) beg))))))
5015 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
5016 (cond
5017 ;; Changing the amount of (already existing) whitespace - don't do anything.
5018 ((and (c-partial-ws-p beg end)
5019 (or (= beg end) ; removal of WS
5020 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
5022 ;; The syntactic relationship which defined a "found type" has been
5023 ;; destroyed.
5024 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
5025 (c-unfind-type (cadr c-maybe-stale-found-type)))
5026 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5030 ;; Setting and removing syntax properties on < and > in languages (C++
5031 ;; and Java) where they can be template/generic delimiters as well as
5032 ;; their normal meaning of "less/greater than".
5034 ;; Normally, < and > have syntax 'punctuation'. When they are found to
5035 ;; be delimiters, they are marked as such with the category properties
5036 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5038 ;; STRATEGY:
5040 ;; It is impossible to determine with certainty whether a <..> pair in
5041 ;; C++ is two comparison operators or is template delimiters, unless
5042 ;; one duplicates a lot of a C++ compiler. For example, the following
5043 ;; code fragment:
5045 ;; foo (a < b, c > d) ;
5047 ;; could be a function call with two integer parameters (each a
5048 ;; relational expression), or it could be a constructor for class foo
5049 ;; taking one parameter d of templated type "a < b, c >". They are
5050 ;; somewhat easier to distinguish in Java.
5052 ;; The strategy now (2010-01) adopted is to mark and unmark < and
5053 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5054 ;; individually when their context so indicated. This gave rise to
5055 ;; intractible problems when one of a matching pair was deleted, or
5056 ;; pulled into a literal.]
5058 ;; At each buffer change, the syntax-table properties are removed in a
5059 ;; before-change function and reapplied, when needed, in an
5060 ;; after-change function. It is far more important that the
5061 ;; properties get removed when they they are spurious than that they
5062 ;; be present when wanted.
5063 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5064 (defun c-clear-<-pair-props (&optional pos)
5065 ;; POS (default point) is at a < character. If it is marked with
5066 ;; open paren syntax-table text property, remove the property,
5067 ;; together with the close paren property on the matching > (if
5068 ;; any).
5069 (save-excursion
5070 (if pos
5071 (goto-char pos)
5072 (setq pos (point)))
5073 (when (equal (c-get-char-property (point) 'syntax-table)
5074 c-<-as-paren-syntax)
5075 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5076 (c-go-list-forward))
5077 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5078 c->-as-paren-syntax) ; should always be true.
5079 (c-clear-char-property (1- (point)) 'category))
5080 (c-clear-char-property pos 'category))))
5082 (defun c-clear->-pair-props (&optional pos)
5083 ;; POS (default point) is at a > character. If it is marked with
5084 ;; close paren syntax-table property, remove the property, together
5085 ;; with the open paren property on the matching < (if any).
5086 (save-excursion
5087 (if pos
5088 (goto-char pos)
5089 (setq pos (point)))
5090 (when (equal (c-get-char-property (point) 'syntax-table)
5091 c->-as-paren-syntax)
5092 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5093 (c-go-up-list-backward))
5094 (when (equal (c-get-char-property (point) 'syntax-table)
5095 c-<-as-paren-syntax) ; should always be true.
5096 (c-clear-char-property (point) 'category))
5097 (c-clear-char-property pos 'category))))
5099 (defun c-clear-<>-pair-props (&optional pos)
5100 ;; POS (default point) is at a < or > character. If it has an
5101 ;; open/close paren syntax-table property, remove this property both
5102 ;; from the current character and its partner (which will also be
5103 ;; thusly marked).
5104 (cond
5105 ((eq (char-after) ?\<)
5106 (c-clear-<-pair-props pos))
5107 ((eq (char-after) ?\>)
5108 (c-clear->-pair-props pos))
5109 (t (c-benign-error
5110 "c-clear-<>-pair-props called from wrong position"))))
5112 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5113 ;; POS (default point) is at a < character. If it is both marked
5114 ;; with open/close paren syntax-table property, and has a matching >
5115 ;; (also marked) which is after LIM, remove the property both from
5116 ;; the current > and its partner. Return t when this happens, nil
5117 ;; when it doesn't.
5118 (save-excursion
5119 (if pos
5120 (goto-char pos)
5121 (setq pos (point)))
5122 (when (equal (c-get-char-property (point) 'syntax-table)
5123 c-<-as-paren-syntax)
5124 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5125 (c-go-list-forward))
5126 (when (and (>= (point) lim)
5127 (equal (c-get-char-property (1- (point)) 'syntax-table)
5128 c->-as-paren-syntax)) ; should always be true.
5129 (c-unmark-<->-as-paren (1- (point)))
5130 (c-unmark-<->-as-paren pos))
5131 t)))
5133 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
5134 ;; POS (default point) is at a > character. If it is both marked
5135 ;; with open/close paren syntax-table property, and has a matching <
5136 ;; (also marked) which is before LIM, remove the property both from
5137 ;; the current < and its partner. Return t when this happens, nil
5138 ;; when it doesn't.
5139 (save-excursion
5140 (if pos
5141 (goto-char pos)
5142 (setq pos (point)))
5143 (when (equal (c-get-char-property (point) 'syntax-table)
5144 c->-as-paren-syntax)
5145 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5146 (c-go-up-list-backward))
5147 (when (and (<= (point) lim)
5148 (equal (c-get-char-property (point) 'syntax-table)
5149 c-<-as-paren-syntax)) ; should always be true.
5150 (c-unmark-<->-as-paren (point))
5151 (c-unmark-<->-as-paren pos))
5152 t)))
5154 ;; Set by c-common-init in cc-mode.el.
5155 (defvar c-new-BEG)
5156 (defvar c-new-END)
5158 (defun c-before-change-check-<>-operators (beg end)
5159 ;; Unmark certain pairs of "< .... >" which are currently marked as
5160 ;; template/generic delimiters. (This marking is via syntax-table
5161 ;; text properties).
5163 ;; These pairs are those which are in the current "statement" (i.e.,
5164 ;; the region between the {, }, or ; before BEG and the one after
5165 ;; END), and which enclose any part of the interval (BEG END).
5167 ;; Note that in C++ (?and Java), template/generic parens cannot
5168 ;; enclose a brace or semicolon, so we use these as bounds on the
5169 ;; region we must work on.
5171 ;; This function is called from before-change-functions (via
5172 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5173 ;; and point is undefined, both at entry and exit.
5175 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5176 ;; 2010-01-29.
5177 (save-excursion
5178 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5179 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5180 new-beg new-end need-new-beg need-new-end)
5181 ;; Locate the barrier before the changed region
5182 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5183 (c-syntactic-skip-backward "^;{}" (max (- beg 2048) (point-min)))
5184 (setq new-beg (point))
5186 ;; Remove the syntax-table properties from each pertinent <...> pair.
5187 ;; Firsly, the ones with the < before beg and > after beg.
5188 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5189 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5190 (setq need-new-beg t)))
5192 ;; Locate the barrier after END.
5193 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5194 (c-syntactic-re-search-forward "[;{}]"
5195 (min (+ end 2048) (point-max)) 'end)
5196 (setq new-end (point))
5198 ;; Remove syntax-table properties from the remaining pertinent <...>
5199 ;; pairs, those with a > after end and < before end.
5200 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5201 (if (c-clear->-pair-props-if-match-before end)
5202 (setq need-new-end t)))
5204 ;; Extend the fontification region, if needed.
5205 (when need-new-beg
5206 (goto-char new-beg)
5207 (c-forward-syntactic-ws)
5208 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5210 (when need-new-end
5211 (and (> new-end c-new-END) (setq c-new-END new-end))))))
5215 (defun c-after-change-check-<>-operators (beg end)
5216 ;; This is called from `after-change-functions' when
5217 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5218 ;; chars with paren syntax become part of another operator like "<<"
5219 ;; or ">=".
5221 ;; This function might do hidden buffer changes.
5223 (save-excursion
5224 (goto-char beg)
5225 (when (or (looking-at "[<>]")
5226 (< (skip-chars-backward "<>") 0))
5228 (goto-char beg)
5229 (c-beginning-of-current-token)
5230 (when (and (< (point) beg)
5231 (looking-at c-<>-multichar-token-regexp)
5232 (< beg (setq beg (match-end 0))))
5233 (while (progn (skip-chars-forward "^<>" beg)
5234 (< (point) beg))
5235 (c-clear-<>-pair-props)
5236 (forward-char))))
5238 (when (< beg end)
5239 (goto-char end)
5240 (when (or (looking-at "[<>]")
5241 (< (skip-chars-backward "<>") 0))
5243 (goto-char end)
5244 (c-beginning-of-current-token)
5245 (when (and (< (point) end)
5246 (looking-at c-<>-multichar-token-regexp)
5247 (< end (setq end (match-end 0))))
5248 (while (progn (skip-chars-forward "^<>" end)
5249 (< (point) end))
5250 (c-clear-<>-pair-props)
5251 (forward-char)))))))
5255 ;; Handling of small scale constructs like types and names.
5257 ;; Dynamically bound variable that instructs `c-forward-type' to also
5258 ;; treat possible types (i.e. those that it normally returns 'maybe or
5259 ;; 'found for) as actual types (and always return 'found for them).
5260 ;; This means that it records them in `c-record-type-identifiers' if
5261 ;; that is set, and that it adds them to `c-found-types'.
5262 (defvar c-promote-possible-types nil)
5264 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5265 ;; mark up successfully parsed arglists with paren syntax properties on
5266 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5267 ;; `c-type' property of each argument separating comma.
5269 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5270 ;; all arglists for side effects (i.e. recording types), otherwise it
5271 ;; exploits any existing paren syntax properties to quickly jump to the
5272 ;; end of already parsed arglists.
5274 ;; Marking up the arglists is not the default since doing that correctly
5275 ;; depends on a proper value for `c-restricted-<>-arglists'.
5276 (defvar c-parse-and-markup-<>-arglists nil)
5278 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5279 ;; not accept arglists that contain binary operators.
5281 ;; This is primarily used to handle C++ template arglists. C++
5282 ;; disambiguates them by checking whether the preceding name is a
5283 ;; template or not. We can't do that, so we assume it is a template
5284 ;; if it can be parsed as one. That usually works well since
5285 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5286 ;; in almost all cases would be pointless.
5288 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5289 ;; should let the comma separate the function arguments instead. And
5290 ;; in a context where the value of the expression is taken, e.g. in
5291 ;; "if (a < b || c > d)", it's probably not a template.
5292 (defvar c-restricted-<>-arglists nil)
5294 ;; Dynamically bound variables that instructs
5295 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5296 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5297 ;; `c-forward-label' to record the ranges of all the type and
5298 ;; reference identifiers they encounter. They will build lists on
5299 ;; these variables where each element is a cons of the buffer
5300 ;; positions surrounding each identifier. This recording is only
5301 ;; activated when `c-record-type-identifiers' is non-nil.
5303 ;; All known types that can't be identifiers are recorded, and also
5304 ;; other possible types if `c-promote-possible-types' is set.
5305 ;; Recording is however disabled inside angle bracket arglists that
5306 ;; are encountered inside names and other angle bracket arglists.
5307 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5308 ;; instead.
5310 ;; Only the names in C++ template style references (e.g. "tmpl" in
5311 ;; "tmpl<a,b>::foo") are recorded as references, other references
5312 ;; aren't handled here.
5314 ;; `c-forward-label' records the label identifier(s) on
5315 ;; `c-record-ref-identifiers'.
5316 (defvar c-record-type-identifiers nil)
5317 (defvar c-record-ref-identifiers nil)
5319 ;; This variable will receive a cons cell of the range of the last
5320 ;; single identifier symbol stepped over by `c-forward-name' if it's
5321 ;; successful. This is the range that should be put on one of the
5322 ;; record lists above by the caller. It's assigned nil if there's no
5323 ;; such symbol in the name.
5324 (defvar c-last-identifier-range nil)
5326 (defmacro c-record-type-id (range)
5327 (if (eq (car-safe range) 'cons)
5328 ;; Always true.
5329 `(setq c-record-type-identifiers
5330 (cons ,range c-record-type-identifiers))
5331 `(let ((range ,range))
5332 (if range
5333 (setq c-record-type-identifiers
5334 (cons range c-record-type-identifiers))))))
5336 (defmacro c-record-ref-id (range)
5337 (if (eq (car-safe range) 'cons)
5338 ;; Always true.
5339 `(setq c-record-ref-identifiers
5340 (cons ,range c-record-ref-identifiers))
5341 `(let ((range ,range))
5342 (if range
5343 (setq c-record-ref-identifiers
5344 (cons range c-record-ref-identifiers))))))
5346 ;; Dynamically bound variable that instructs `c-forward-type' to
5347 ;; record the ranges of types that only are found. Behaves otherwise
5348 ;; like `c-record-type-identifiers'.
5349 (defvar c-record-found-types nil)
5351 (defmacro c-forward-keyword-prefixed-id (type)
5352 ;; Used internally in `c-forward-keyword-clause' to move forward
5353 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5354 ;; possibly is prefixed by keywords and their associated clauses.
5355 ;; Try with a type/name first to not trip up on those that begin
5356 ;; with a keyword. Return t if a known or found type is moved
5357 ;; over. The point is clobbered if nil is returned. If range
5358 ;; recording is enabled, the identifier is recorded on as a type
5359 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5361 ;; This macro might do hidden buffer changes.
5362 `(let (res)
5363 (while (if (setq res ,(if (eq type 'type)
5364 `(c-forward-type)
5365 `(c-forward-name)))
5367 (and (looking-at c-keywords-regexp)
5368 (c-forward-keyword-clause 1))))
5369 (when (memq res '(t known found prefix))
5370 ,(when (eq type 'ref)
5371 `(when c-record-type-identifiers
5372 (c-record-ref-id c-last-identifier-range)))
5373 t)))
5375 (defmacro c-forward-id-comma-list (type update-safe-pos)
5376 ;; Used internally in `c-forward-keyword-clause' to move forward
5377 ;; over a comma separated list of types or names using
5378 ;; `c-forward-keyword-prefixed-id'.
5380 ;; This macro might do hidden buffer changes.
5381 `(while (and (progn
5382 ,(when update-safe-pos
5383 `(setq safe-pos (point)))
5384 (eq (char-after) ?,))
5385 (progn
5386 (forward-char)
5387 (c-forward-syntactic-ws)
5388 (c-forward-keyword-prefixed-id ,type)))))
5390 (defun c-forward-keyword-clause (match)
5391 ;; Submatch MATCH in the current match data is assumed to surround a
5392 ;; token. If it's a keyword, move over it and any immediately
5393 ;; following clauses associated with it, stopping at the start of
5394 ;; the next token. t is returned in that case, otherwise the point
5395 ;; stays and nil is returned. The kind of clauses that are
5396 ;; recognized are those specified by `c-type-list-kwds',
5397 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5398 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5399 ;; and `c-<>-arglist-kwds'.
5401 ;; This function records identifier ranges on
5402 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5403 ;; `c-record-type-identifiers' is non-nil.
5405 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5406 ;; apply directly after the keyword, the type list is moved over
5407 ;; only when there is no unaccounted token before it (i.e. a token
5408 ;; that isn't moved over due to some other keyword list). The
5409 ;; identifier ranges in the list are still recorded if that should
5410 ;; be done, though.
5412 ;; This function might do hidden buffer changes.
5414 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5415 ;; The call to `c-forward-<>-arglist' below is made after
5416 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5417 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5418 ;; should therefore be nil.
5419 (c-parse-and-markup-<>-arglists t)
5420 c-restricted-<>-arglists)
5422 (when kwd-sym
5423 (goto-char (match-end match))
5424 (c-forward-syntactic-ws)
5425 (setq safe-pos (point))
5427 (cond
5428 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5429 (c-forward-keyword-prefixed-id type))
5430 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5431 (c-forward-id-comma-list type t))
5433 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5434 (c-forward-keyword-prefixed-id ref))
5435 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5436 (c-forward-id-comma-list ref t))
5438 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5439 (eq (char-after) ?\())
5440 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5442 (forward-char)
5443 (when (and (setq pos (c-up-list-forward))
5444 (eq (char-before pos) ?\)))
5445 (when (and c-record-type-identifiers
5446 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5447 ;; Use `c-forward-type' on every identifier we can find
5448 ;; inside the paren, to record the types.
5449 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5450 (goto-char (match-beginning 0))
5451 (unless (c-forward-type)
5452 (looking-at c-symbol-key) ; Always matches.
5453 (goto-char (match-end 0)))))
5455 (goto-char pos)
5456 (c-forward-syntactic-ws)
5457 (setq safe-pos (point))))
5459 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5460 (eq (char-after) ?<)
5461 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5462 (c-forward-syntactic-ws)
5463 (setq safe-pos (point)))
5465 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5466 (not (looking-at c-symbol-start))
5467 (c-safe (c-forward-sexp) t))
5468 (c-forward-syntactic-ws)
5469 (setq safe-pos (point))))
5471 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5472 (if (eq (char-after) ?:)
5473 ;; If we are at the colon already, we move over the type
5474 ;; list after it.
5475 (progn
5476 (forward-char)
5477 (c-forward-syntactic-ws)
5478 (when (c-forward-keyword-prefixed-id type)
5479 (c-forward-id-comma-list type t)))
5480 ;; Not at the colon, so stop here. But the identifier
5481 ;; ranges in the type list later on should still be
5482 ;; recorded.
5483 (and c-record-type-identifiers
5484 (progn
5485 ;; If a keyword matched both one of the types above and
5486 ;; this one, we match `c-colon-type-list-re' after the
5487 ;; clause matched above.
5488 (goto-char safe-pos)
5489 (looking-at c-colon-type-list-re))
5490 (progn
5491 (goto-char (match-end 0))
5492 (c-forward-syntactic-ws)
5493 (c-forward-keyword-prefixed-id type))
5494 ;; There's a type after the `c-colon-type-list-re' match
5495 ;; after a keyword in `c-colon-type-list-kwds'.
5496 (c-forward-id-comma-list type nil))))
5498 (goto-char safe-pos)
5499 t)))
5501 ;; cc-mode requires cc-fonts.
5502 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5504 (defun c-forward-<>-arglist (all-types)
5505 ;; The point is assumed to be at a "<". Try to treat it as the open
5506 ;; paren of an angle bracket arglist and move forward to the
5507 ;; corresponding ">". If successful, the point is left after the
5508 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5509 ;; returned. If ALL-TYPES is t then all encountered arguments in
5510 ;; the arglist that might be types are treated as found types.
5512 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5513 ;; function handles text properties on the angle brackets and argument
5514 ;; separating commas.
5516 ;; `c-restricted-<>-arglists' controls how lenient the template
5517 ;; arglist recognition should be.
5519 ;; This function records identifier ranges on
5520 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5521 ;; `c-record-type-identifiers' is non-nil.
5523 ;; This function might do hidden buffer changes.
5525 (let ((start (point))
5526 ;; If `c-record-type-identifiers' is set then activate
5527 ;; recording of any found types that constitute an argument in
5528 ;; the arglist.
5529 (c-record-found-types (if c-record-type-identifiers t)))
5530 (if (catch 'angle-bracket-arglist-escape
5531 (setq c-record-found-types
5532 (c-forward-<>-arglist-recur all-types)))
5533 (progn
5534 (when (consp c-record-found-types)
5535 (setq c-record-type-identifiers
5536 ;; `nconc' doesn't mind that the tail of
5537 ;; `c-record-found-types' is t.
5538 (nconc c-record-found-types c-record-type-identifiers)))
5539 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
5542 (goto-char start)
5543 nil)))
5545 (defun c-forward-<>-arglist-recur (all-types)
5546 ;; Recursive part of `c-forward-<>-arglist'.
5548 ;; This function might do hidden buffer changes.
5550 (let ((start (point)) res pos tmp
5551 ;; Cover this so that any recorded found type ranges are
5552 ;; automatically lost if it turns out to not be an angle
5553 ;; bracket arglist. It's propagated through the return value
5554 ;; on successful completion.
5555 (c-record-found-types c-record-found-types)
5556 ;; List that collects the positions after the argument
5557 ;; separating ',' in the arglist.
5558 arg-start-pos)
5559 ;; If the '<' has paren open syntax then we've marked it as an angle
5560 ;; bracket arglist before, so skip to the end.
5561 (if (and (not c-parse-and-markup-<>-arglists)
5562 (c-get-char-property (point) 'syntax-table))
5564 (progn
5565 (forward-char)
5566 (if (and (c-go-up-list-forward)
5567 (eq (char-before) ?>))
5569 ;; Got unmatched paren angle brackets. We don't clear the paren
5570 ;; syntax properties and retry, on the basis that it's very
5571 ;; unlikely that paren angle brackets become operators by code
5572 ;; manipulation. It's far more likely that it doesn't match due
5573 ;; to narrowing or some temporary change.
5574 (goto-char start)
5575 nil))
5577 (forward-char) ; Forward over the opening '<'.
5579 (unless (looking-at c-<-op-cont-regexp)
5580 ;; go forward one non-alphanumeric character (group) per iteration of
5581 ;; this loop.
5582 (while (and
5583 (progn
5584 (c-forward-syntactic-ws)
5585 (let ((orig-record-found-types c-record-found-types))
5586 (when (or (and c-record-type-identifiers all-types)
5587 (c-major-mode-is 'java-mode))
5588 ;; All encountered identifiers are types, so set the
5589 ;; promote flag and parse the type.
5590 (progn
5591 (c-forward-syntactic-ws)
5592 (if (looking-at "\\?")
5593 (forward-char)
5594 (when (looking-at c-identifier-start)
5595 (let ((c-promote-possible-types t)
5596 (c-record-found-types t))
5597 (c-forward-type))))
5599 (c-forward-syntactic-ws)
5601 (when (or (looking-at "extends")
5602 (looking-at "super"))
5603 (forward-word)
5604 (c-forward-syntactic-ws)
5605 (let ((c-promote-possible-types t)
5606 (c-record-found-types t))
5607 (c-forward-type)
5608 (c-forward-syntactic-ws))))))
5610 (setq pos (point)) ; e.g. first token inside the '<'
5612 ;; Note: These regexps exploit the match order in \| so
5613 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5614 (c-syntactic-re-search-forward
5615 ;; Stop on ',', '|', '&', '+' and '-' to catch
5616 ;; common binary operators that could be between
5617 ;; two comparison expressions "a<b" and "c>d".
5618 "[<;{},|+&-]\\|[>)]"
5619 nil t t))
5621 (cond
5622 ((eq (char-before) ?>)
5623 ;; Either an operator starting with '>' or the end of
5624 ;; the angle bracket arglist.
5626 (if (looking-at c->-op-cont-regexp)
5627 (progn
5628 (goto-char (match-end 0))
5629 t) ; Continue the loop.
5631 ;; The angle bracket arglist is finished.
5632 (when c-parse-and-markup-<>-arglists
5633 (while arg-start-pos
5634 (c-put-c-type-property (1- (car arg-start-pos))
5635 'c-<>-arg-sep)
5636 (setq arg-start-pos (cdr arg-start-pos)))
5637 (c-mark-<-as-paren start)
5638 (c-mark->-as-paren (1- (point))))
5639 (setq res t)
5640 nil)) ; Exit the loop.
5642 ((eq (char-before) ?<)
5643 ;; Either an operator starting with '<' or a nested arglist.
5644 (setq pos (point))
5645 (let (id-start id-end subres keyword-match)
5646 (cond
5647 ;; The '<' begins a multi-char operator.
5648 ((looking-at c-<-op-cont-regexp)
5649 (setq tmp (match-end 0))
5650 (goto-char (match-end 0)))
5651 ;; We're at a nested <.....>
5652 ((progn
5653 (setq tmp pos)
5654 (backward-char) ; to the '<'
5655 (and
5656 (save-excursion
5657 ;; There's always an identifier before an angle
5658 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5659 ;; or `c-<>-arglist-kwds'.
5660 (c-backward-syntactic-ws)
5661 (setq id-end (point))
5662 (c-simple-skip-symbol-backward)
5663 (when (or (setq keyword-match
5664 (looking-at c-opt-<>-sexp-key))
5665 (not (looking-at c-keywords-regexp)))
5666 (setq id-start (point))))
5667 (setq subres
5668 (let ((c-promote-possible-types t)
5669 (c-record-found-types t))
5670 (c-forward-<>-arglist-recur
5671 (and keyword-match
5672 (c-keyword-member
5673 (c-keyword-sym (match-string 1))
5674 'c-<>-type-kwds)))))))
5676 ;; It was an angle bracket arglist.
5677 (setq c-record-found-types subres)
5679 ;; Record the identifier before the template as a type
5680 ;; or reference depending on whether the arglist is last
5681 ;; in a qualified identifier.
5682 (when (and c-record-type-identifiers
5683 (not keyword-match))
5684 (if (and c-opt-identifier-concat-key
5685 (progn
5686 (c-forward-syntactic-ws)
5687 (looking-at c-opt-identifier-concat-key)))
5688 (c-record-ref-id (cons id-start id-end))
5689 (c-record-type-id (cons id-start id-end)))))
5691 ;; At a "less than" operator.
5693 (forward-char)
5695 t) ; carry on looping.
5697 ((and (not c-restricted-<>-arglists)
5698 (or (and (eq (char-before) ?&)
5699 (not (eq (char-after) ?&)))
5700 (eq (char-before) ?,)))
5701 ;; Just another argument. Record the position. The
5702 ;; type check stuff that made us stop at it is at
5703 ;; the top of the loop.
5704 (setq arg-start-pos (cons (point) arg-start-pos)))
5707 ;; Got a character that can't be in an angle bracket
5708 ;; arglist argument. Abort using `throw', since
5709 ;; it's useless to try to find a surrounding arglist
5710 ;; if we're nested.
5711 (throw 'angle-bracket-arglist-escape nil))))))
5712 (if res
5713 (or c-record-found-types t)))))
5715 (defun c-backward-<>-arglist (all-types &optional limit)
5716 ;; The point is assumed to be directly after a ">". Try to treat it
5717 ;; as the close paren of an angle bracket arglist and move back to
5718 ;; the corresponding "<". If successful, the point is left at
5719 ;; the "<" and t is returned, otherwise the point isn't moved and
5720 ;; nil is returned. ALL-TYPES is passed on to
5721 ;; `c-forward-<>-arglist'.
5723 ;; If the optional LIMIT is given, it bounds the backward search.
5724 ;; It's then assumed to be at a syntactically relevant position.
5726 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5727 ;; function for more details.
5729 (let ((start (point)))
5730 (backward-char)
5731 (if (and (not c-parse-and-markup-<>-arglists)
5732 (c-get-char-property (point) 'syntax-table))
5734 (if (and (c-go-up-list-backward)
5735 (eq (char-after) ?<))
5737 ;; See corresponding note in `c-forward-<>-arglist'.
5738 (goto-char start)
5739 nil)
5741 (while (progn
5742 (c-syntactic-skip-backward "^<;{}" limit t)
5744 (and
5745 (if (eq (char-before) ?<)
5747 ;; Stopped at bob or a char that isn't allowed in an
5748 ;; arglist, so we've failed.
5749 (goto-char start)
5750 nil)
5752 (if (> (point)
5753 (progn (c-beginning-of-current-token)
5754 (point)))
5755 ;; If we moved then the "<" was part of some
5756 ;; multicharacter token.
5759 (backward-char)
5760 (let ((beg-pos (point)))
5761 (if (c-forward-<>-arglist all-types)
5762 (cond ((= (point) start)
5763 ;; Matched the arglist. Break the while.
5764 (goto-char beg-pos)
5765 nil)
5766 ((> (point) start)
5767 ;; We started from a non-paren ">" inside an
5768 ;; arglist.
5769 (goto-char start)
5770 nil)
5772 ;; Matched a shorter arglist. Can be a nested
5773 ;; one so continue looking.
5774 (goto-char beg-pos)
5776 t))))))
5778 (/= (point) start))))
5780 (defun c-forward-name ()
5781 ;; Move forward over a complete name if at the beginning of one,
5782 ;; stopping at the next following token. A keyword, as such,
5783 ;; doesn't count as a name. If the point is not at something that
5784 ;; is recognized as a name then it stays put.
5786 ;; A name could be something as simple as "foo" in C or something as
5787 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5788 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5789 ;; int>::*volatile const" in C++ (this function is actually little
5790 ;; more than a `looking-at' call in all modes except those that,
5791 ;; like C++, have `c-recognize-<>-arglists' set).
5793 ;; Return
5794 ;; o - nil if no name is found;
5795 ;; o - 'template if it's an identifier ending with an angle bracket
5796 ;; arglist;
5797 ;; o - 'operator of it's an operator identifier;
5798 ;; o - t if it's some other kind of name.
5800 ;; This function records identifier ranges on
5801 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5802 ;; `c-record-type-identifiers' is non-nil.
5804 ;; This function might do hidden buffer changes.
5806 (let ((pos (point)) (start (point)) res id-start id-end
5807 ;; Turn off `c-promote-possible-types' here since we might
5808 ;; call `c-forward-<>-arglist' and we don't want it to promote
5809 ;; every suspect thing in the arglist to a type. We're
5810 ;; typically called from `c-forward-type' in this case, and
5811 ;; the caller only wants the top level type that it finds to
5812 ;; be promoted.
5813 c-promote-possible-types)
5814 (while
5815 (and
5816 (looking-at c-identifier-key)
5818 (progn
5819 ;; Check for keyword. We go to the last symbol in
5820 ;; `c-identifier-key' first.
5821 (goto-char (setq id-end (match-end 0)))
5822 (c-simple-skip-symbol-backward)
5823 (setq id-start (point))
5825 (if (looking-at c-keywords-regexp)
5826 (when (and (c-major-mode-is 'c++-mode)
5827 (looking-at
5828 (cc-eval-when-compile
5829 (concat "\\(operator\\|\\(template\\)\\)"
5830 "\\(" (c-lang-const c-nonsymbol-key c++)
5831 "\\|$\\)")))
5832 (if (match-beginning 2)
5833 ;; "template" is only valid inside an
5834 ;; identifier if preceded by "::".
5835 (save-excursion
5836 (c-backward-syntactic-ws)
5837 (and (c-safe (backward-char 2) t)
5838 (looking-at "::")))
5841 ;; Handle a C++ operator or template identifier.
5842 (goto-char id-end)
5843 (c-forward-syntactic-ws)
5844 (cond ((eq (char-before id-end) ?e)
5845 ;; Got "... ::template".
5846 (let ((subres (c-forward-name)))
5847 (when subres
5848 (setq pos (point)
5849 res subres))))
5851 ((looking-at c-identifier-start)
5852 ;; Got a cast operator.
5853 (when (c-forward-type)
5854 (setq pos (point)
5855 res 'operator)
5856 ;; Now we should match a sequence of either
5857 ;; '*', '&' or a name followed by ":: *",
5858 ;; where each can be followed by a sequence
5859 ;; of `c-opt-type-modifier-key'.
5860 (while (cond ((looking-at "[*&]")
5861 (goto-char (match-end 0))
5863 ((looking-at c-identifier-start)
5864 (and (c-forward-name)
5865 (looking-at "::")
5866 (progn
5867 (goto-char (match-end 0))
5868 (c-forward-syntactic-ws)
5869 (eq (char-after) ?*))
5870 (progn
5871 (forward-char)
5872 t))))
5873 (while (progn
5874 (c-forward-syntactic-ws)
5875 (setq pos (point))
5876 (looking-at c-opt-type-modifier-key))
5877 (goto-char (match-end 1))))))
5879 ((looking-at c-overloadable-operators-regexp)
5880 ;; Got some other operator.
5881 (setq c-last-identifier-range
5882 (cons (point) (match-end 0)))
5883 (goto-char (match-end 0))
5884 (c-forward-syntactic-ws)
5885 (setq pos (point)
5886 res 'operator)))
5888 nil)
5890 ;; `id-start' is equal to `id-end' if we've jumped over
5891 ;; an identifier that doesn't end with a symbol token.
5892 ;; That can occur e.g. for Java import directives on the
5893 ;; form "foo.bar.*".
5894 (when (and id-start (/= id-start id-end))
5895 (setq c-last-identifier-range
5896 (cons id-start id-end)))
5897 (goto-char id-end)
5898 (c-forward-syntactic-ws)
5899 (setq pos (point)
5900 res t)))
5902 (progn
5903 (goto-char pos)
5904 (when (or c-opt-identifier-concat-key
5905 c-recognize-<>-arglists)
5907 (cond
5908 ((and c-opt-identifier-concat-key
5909 (looking-at c-opt-identifier-concat-key))
5910 ;; Got a concatenated identifier. This handles the
5911 ;; cases with tricky syntactic whitespace that aren't
5912 ;; covered in `c-identifier-key'.
5913 (goto-char (match-end 0))
5914 (c-forward-syntactic-ws)
5917 ((and c-recognize-<>-arglists
5918 (eq (char-after) ?<))
5919 ;; Maybe an angle bracket arglist.
5920 (when (let ((c-record-type-identifiers t)
5921 (c-record-found-types t))
5922 (c-forward-<>-arglist nil))
5924 (c-add-type start (1+ pos))
5925 (c-forward-syntactic-ws)
5926 (setq pos (point)
5927 c-last-identifier-range nil)
5929 (if (and c-opt-identifier-concat-key
5930 (looking-at c-opt-identifier-concat-key))
5932 ;; Continue if there's an identifier concatenation
5933 ;; operator after the template argument.
5934 (progn
5935 (when (and c-record-type-identifiers id-start)
5936 (c-record-ref-id (cons id-start id-end)))
5937 (forward-char 2)
5938 (c-forward-syntactic-ws)
5941 (when (and c-record-type-identifiers id-start)
5942 (c-record-type-id (cons id-start id-end)))
5943 (setq res 'template)
5944 nil)))
5945 )))))
5947 (goto-char pos)
5948 res))
5950 (defun c-forward-type (&optional brace-block-too)
5951 ;; Move forward over a type spec if at the beginning of one,
5952 ;; stopping at the next following token. The keyword "typedef"
5953 ;; isn't part of a type spec here.
5955 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
5956 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
5957 ;; The current (2009-03-10) intention is to convert all uses of
5958 ;; `c-forward-type' to call with this parameter set, then to
5959 ;; eliminate it.
5961 ;; Return
5962 ;; o - t if it's a known type that can't be a name or other
5963 ;; expression;
5964 ;; o - 'known if it's an otherwise known type (according to
5965 ;; `*-font-lock-extra-types');
5966 ;; o - 'prefix if it's a known prefix of a type;
5967 ;; o - 'found if it's a type that matches one in `c-found-types';
5968 ;; o - 'maybe if it's an identfier that might be a type; or
5969 ;; o - nil if it can't be a type (the point isn't moved then).
5971 ;; The point is assumed to be at the beginning of a token.
5973 ;; Note that this function doesn't skip past the brace definition
5974 ;; that might be considered part of the type, e.g.
5975 ;; "enum {a, b, c} foo".
5977 ;; This function records identifier ranges on
5978 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5979 ;; `c-record-type-identifiers' is non-nil.
5981 ;; This function might do hidden buffer changes.
5982 (when (and c-recognize-<>-arglists
5983 (looking-at "<"))
5984 (c-forward-<>-arglist t)
5985 (c-forward-syntactic-ws))
5987 (let ((start (point)) pos res name-res id-start id-end id-range)
5989 ;; Skip leading type modifiers. If any are found we know it's a
5990 ;; prefix of a type.
5991 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
5992 (while (looking-at c-opt-type-modifier-key)
5993 (goto-char (match-end 1))
5994 (c-forward-syntactic-ws)
5995 (setq res 'prefix)))
5997 (cond
5998 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
5999 ; "typedef".
6000 (goto-char (match-end 1))
6001 (c-forward-syntactic-ws)
6002 (setq pos (point))
6004 (setq name-res (c-forward-name))
6005 (setq res (not (null name-res)))
6006 (when (eq name-res t)
6007 ;; In many languages the name can be used without the
6008 ;; prefix, so we add it to `c-found-types'.
6009 (c-add-type pos (point))
6010 (when (and c-record-type-identifiers
6011 c-last-identifier-range)
6012 (c-record-type-id c-last-identifier-range)))
6013 (when (and brace-block-too
6014 (memq res '(t nil))
6015 (eq (char-after) ?\{)
6016 (save-excursion
6017 (c-safe
6018 (progn (c-forward-sexp)
6019 (c-forward-syntactic-ws)
6020 (setq pos (point))))))
6021 (goto-char pos)
6022 (setq res t))
6023 (unless res (goto-char start))) ; invalid syntax
6025 ((progn
6026 (setq pos nil)
6027 (if (looking-at c-identifier-start)
6028 (save-excursion
6029 (setq id-start (point)
6030 name-res (c-forward-name))
6031 (when name-res
6032 (setq id-end (point)
6033 id-range c-last-identifier-range))))
6034 (and (cond ((looking-at c-primitive-type-key)
6035 (setq res t))
6036 ((c-with-syntax-table c-identifier-syntax-table
6037 (looking-at c-known-type-key))
6038 (setq res 'known)))
6039 (or (not id-end)
6040 (>= (save-excursion
6041 (save-match-data
6042 (goto-char (match-end 1))
6043 (c-forward-syntactic-ws)
6044 (setq pos (point))))
6045 id-end)
6046 (setq res nil))))
6047 ;; Looking at a primitive or known type identifier. We've
6048 ;; checked for a name first so that we don't go here if the
6049 ;; known type match only is a prefix of another name.
6051 (setq id-end (match-end 1))
6053 (when (and c-record-type-identifiers
6054 (or c-promote-possible-types (eq res t)))
6055 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6057 (if (and c-opt-type-component-key
6058 (save-match-data
6059 (looking-at c-opt-type-component-key)))
6060 ;; There might be more keywords for the type.
6061 (let (safe-pos)
6062 (c-forward-keyword-clause 1)
6063 (while (progn
6064 (setq safe-pos (point))
6065 (looking-at c-opt-type-component-key))
6066 (when (and c-record-type-identifiers
6067 (looking-at c-primitive-type-key))
6068 (c-record-type-id (cons (match-beginning 1)
6069 (match-end 1))))
6070 (c-forward-keyword-clause 1))
6071 (if (looking-at c-primitive-type-key)
6072 (progn
6073 (when c-record-type-identifiers
6074 (c-record-type-id (cons (match-beginning 1)
6075 (match-end 1))))
6076 (c-forward-keyword-clause 1)
6077 (setq res t))
6078 (goto-char safe-pos)
6079 (setq res 'prefix)))
6080 (unless (save-match-data (c-forward-keyword-clause 1))
6081 (if pos
6082 (goto-char pos)
6083 (goto-char (match-end 1))
6084 (c-forward-syntactic-ws)))))
6086 (name-res
6087 (cond ((eq name-res t)
6088 ;; A normal identifier.
6089 (goto-char id-end)
6090 (if (or res c-promote-possible-types)
6091 (progn
6092 (c-add-type id-start id-end)
6093 (when (and c-record-type-identifiers id-range)
6094 (c-record-type-id id-range))
6095 (unless res
6096 (setq res 'found)))
6097 (setq res (if (c-check-type id-start id-end)
6098 ;; It's an identifier that has been used as
6099 ;; a type somewhere else.
6100 'found
6101 ;; It's an identifier that might be a type.
6102 'maybe))))
6103 ((eq name-res 'template)
6104 ;; A template is a type.
6105 (goto-char id-end)
6106 (setq res t))
6108 ;; Otherwise it's an operator identifier, which is not a type.
6109 (goto-char start)
6110 (setq res nil)))))
6112 (when res
6113 ;; Skip trailing type modifiers. If any are found we know it's
6114 ;; a type.
6115 (when c-opt-type-modifier-key
6116 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
6117 (goto-char (match-end 1))
6118 (c-forward-syntactic-ws)
6119 (setq res t)))
6120 ;; Step over any type suffix operator. Do not let the existence
6121 ;; of these alter the classification of the found type, since
6122 ;; these operators typically are allowed in normal expressions
6123 ;; too.
6124 (when c-opt-type-suffix-key
6125 (while (looking-at c-opt-type-suffix-key)
6126 (goto-char (match-end 1))
6127 (c-forward-syntactic-ws)))
6129 (when c-opt-type-concat-key ; Only/mainly for pike.
6130 ;; Look for a trailing operator that concatenates the type
6131 ;; with a following one, and if so step past that one through
6132 ;; a recursive call. Note that we don't record concatenated
6133 ;; types in `c-found-types' - it's the component types that
6134 ;; are recorded when appropriate.
6135 (setq pos (point))
6136 (let* ((c-promote-possible-types (or (memq res '(t known))
6137 c-promote-possible-types))
6138 ;; If we can't promote then set `c-record-found-types' so that
6139 ;; we can merge in the types from the second part afterwards if
6140 ;; it turns out to be a known type there.
6141 (c-record-found-types (and c-record-type-identifiers
6142 (not c-promote-possible-types)))
6143 subres)
6144 (if (and (looking-at c-opt-type-concat-key)
6146 (progn
6147 (goto-char (match-end 1))
6148 (c-forward-syntactic-ws)
6149 (setq subres (c-forward-type))))
6151 (progn
6152 ;; If either operand certainly is a type then both are, but we
6153 ;; don't let the existence of the operator itself promote two
6154 ;; uncertain types to a certain one.
6155 (cond ((eq res t))
6156 ((eq subres t)
6157 (unless (eq name-res 'template)
6158 (c-add-type id-start id-end))
6159 (when (and c-record-type-identifiers id-range)
6160 (c-record-type-id id-range))
6161 (setq res t))
6162 ((eq res 'known))
6163 ((eq subres 'known)
6164 (setq res 'known))
6165 ((eq res 'found))
6166 ((eq subres 'found)
6167 (setq res 'found))
6169 (setq res 'maybe)))
6171 (when (and (eq res t)
6172 (consp c-record-found-types))
6173 ;; Merge in the ranges of any types found by the second
6174 ;; `c-forward-type'.
6175 (setq c-record-type-identifiers
6176 ;; `nconc' doesn't mind that the tail of
6177 ;; `c-record-found-types' is t.
6178 (nconc c-record-found-types
6179 c-record-type-identifiers))))
6181 (goto-char pos))))
6183 (when (and c-record-found-types (memq res '(known found)) id-range)
6184 (setq c-record-found-types
6185 (cons id-range c-record-found-types))))
6187 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6189 res))
6191 (defun c-forward-annotation ()
6192 ;; Used for Java code only at the moment. Assumes point is on the
6193 ;; @, moves forward an annotation. returns nil if there is no
6194 ;; annotation at point.
6195 (and (looking-at "@")
6196 (progn (forward-char) t)
6197 (c-forward-type)
6198 (progn (c-forward-syntactic-ws) t)
6199 (if (looking-at "(")
6200 (c-go-list-forward)
6201 t)))
6204 ;; Handling of large scale constructs like statements and declarations.
6206 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6207 ;; defsubst or perhaps even a defun, but it contains lots of free
6208 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6209 (defmacro c-fdoc-shift-type-backward (&optional short)
6210 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6211 ;; of types when parsing a declaration, which means that it
6212 ;; sometimes consumes the identifier in the declaration as a type.
6213 ;; This is used to "backtrack" and make the last type be treated as
6214 ;; an identifier instead.
6215 `(progn
6216 ,(unless short
6217 ;; These identifiers are bound only in the inner let.
6218 '(setq identifier-type at-type
6219 identifier-start type-start
6220 got-parens nil
6221 got-identifier t
6222 got-suffix t
6223 got-suffix-after-parens id-start
6224 paren-depth 0))
6226 (if (setq at-type (if (eq backup-at-type 'prefix)
6228 backup-at-type))
6229 (setq type-start backup-type-start
6230 id-start backup-id-start)
6231 (setq type-start start-pos
6232 id-start start-pos))
6234 ;; When these flags already are set we've found specifiers that
6235 ;; unconditionally signal these attributes - backtracking doesn't
6236 ;; change that. So keep them set in that case.
6237 (or at-type-decl
6238 (setq at-type-decl backup-at-type-decl))
6239 (or maybe-typeless
6240 (setq maybe-typeless backup-maybe-typeless))
6242 ,(unless short
6243 ;; This identifier is bound only in the inner let.
6244 '(setq start id-start))))
6246 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6247 ;; Move forward over a declaration or a cast if at the start of one.
6248 ;; The point is assumed to be at the start of some token. Nil is
6249 ;; returned if no declaration or cast is recognized, and the point
6250 ;; is clobbered in that case.
6252 ;; If a declaration is parsed:
6254 ;; The point is left at the first token after the first complete
6255 ;; declarator, if there is one. The return value is a cons where
6256 ;; the car is the position of the first token in the declarator. (See
6257 ;; below for the cdr.)
6258 ;; Some examples:
6260 ;; void foo (int a, char *b) stuff ...
6261 ;; car ^ ^ point
6262 ;; float (*a)[], b;
6263 ;; car ^ ^ point
6264 ;; unsigned int a = c_style_initializer, b;
6265 ;; car ^ ^ point
6266 ;; unsigned int a (cplusplus_style_initializer), b;
6267 ;; car ^ ^ point (might change)
6268 ;; class Foo : public Bar {}
6269 ;; car ^ ^ point
6270 ;; class PikeClass (int a, string b) stuff ...
6271 ;; car ^ ^ point
6272 ;; enum bool;
6273 ;; car ^ ^ point
6274 ;; enum bool flag;
6275 ;; car ^ ^ point
6276 ;; void cplusplus_function (int x) throw (Bad);
6277 ;; car ^ ^ point
6278 ;; Foo::Foo (int b) : Base (b) {}
6279 ;; car ^ ^ point
6281 ;; The cdr of the return value is non-nil when a
6282 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6283 ;; Specifically it is a dotted pair (A . B) where B is t when a
6284 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6285 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6286 ;; specifier is present. I.e., (some of) the declared
6287 ;; identifier(s) are types.
6289 ;; If a cast is parsed:
6291 ;; The point is left at the first token after the closing paren of
6292 ;; the cast. The return value is `cast'. Note that the start
6293 ;; position must be at the first token inside the cast parenthesis
6294 ;; to recognize it.
6296 ;; PRECEDING-TOKEN-END is the first position after the preceding
6297 ;; token, i.e. on the other side of the syntactic ws from the point.
6298 ;; Use a value less than or equal to (point-min) if the point is at
6299 ;; the first token in (the visible part of) the buffer.
6301 ;; CONTEXT is a symbol that describes the context at the point:
6302 ;; 'decl In a comma-separated declaration context (typically
6303 ;; inside a function declaration arglist).
6304 ;; '<> In an angle bracket arglist.
6305 ;; 'arglist Some other type of arglist.
6306 ;; nil Some other context or unknown context. Includes
6307 ;; within the parens of an if, for, ... construct.
6309 ;; LAST-CAST-END is the first token after the closing paren of a
6310 ;; preceding cast, or nil if none is known. If
6311 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6312 ;; the position after the closest preceding call where a cast was
6313 ;; matched. In that case it's used to discover chains of casts like
6314 ;; "(a) (b) c".
6316 ;; This function records identifier ranges on
6317 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6318 ;; `c-record-type-identifiers' is non-nil.
6320 ;; This function might do hidden buffer changes.
6322 (let (;; `start-pos' is used below to point to the start of the
6323 ;; first type, i.e. after any leading specifiers. It might
6324 ;; also point at the beginning of the preceding syntactic
6325 ;; whitespace.
6326 (start-pos (point))
6327 ;; Set to the result of `c-forward-type'.
6328 at-type
6329 ;; The position of the first token in what we currently
6330 ;; believe is the type in the declaration or cast, after any
6331 ;; specifiers and their associated clauses.
6332 type-start
6333 ;; The position of the first token in what we currently
6334 ;; believe is the declarator for the first identifier. Set
6335 ;; when the type is found, and moved forward over any
6336 ;; `c-decl-hangon-kwds' and their associated clauses that
6337 ;; occurs after the type.
6338 id-start
6339 ;; These store `at-type', `type-start' and `id-start' of the
6340 ;; identifier before the one in those variables. The previous
6341 ;; identifier might turn out to be the real type in a
6342 ;; declaration if the last one has to be the declarator in it.
6343 ;; If `backup-at-type' is nil then the other variables have
6344 ;; undefined values.
6345 backup-at-type backup-type-start backup-id-start
6346 ;; Set if we've found a specifier (apart from "typedef") that makes
6347 ;; the defined identifier(s) types.
6348 at-type-decl
6349 ;; Set if we've a "typedef" keyword.
6350 at-typedef
6351 ;; Set if we've found a specifier that can start a declaration
6352 ;; where there's no type.
6353 maybe-typeless
6354 ;; If a specifier is found that also can be a type prefix,
6355 ;; these flags are set instead of those above. If we need to
6356 ;; back up an identifier, they are copied to the real flag
6357 ;; variables. Thus they only take effect if we fail to
6358 ;; interpret it as a type.
6359 backup-at-type-decl backup-maybe-typeless
6360 ;; Whether we've found a declaration or a cast. We might know
6361 ;; this before we've found the type in it. It's 'ids if we've
6362 ;; found two consecutive identifiers (usually a sure sign, but
6363 ;; we should allow that in labels too), and t if we've found a
6364 ;; specifier keyword (a 100% sure sign).
6365 at-decl-or-cast
6366 ;; Set when we need to back up to parse this as a declaration
6367 ;; but not as a cast.
6368 backup-if-not-cast
6369 ;; For casts, the return position.
6370 cast-end
6371 ;; Save `c-record-type-identifiers' and
6372 ;; `c-record-ref-identifiers' since ranges are recorded
6373 ;; speculatively and should be thrown away if it turns out
6374 ;; that it isn't a declaration or cast.
6375 (save-rec-type-ids c-record-type-identifiers)
6376 (save-rec-ref-ids c-record-ref-identifiers))
6378 (while (c-forward-annotation)
6379 (c-forward-syntactic-ws))
6381 ;; Check for a type. Unknown symbols are treated as possible
6382 ;; types, but they could also be specifiers disguised through
6383 ;; macros like __INLINE__, so we recognize both types and known
6384 ;; specifiers after them too.
6385 (while
6386 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6388 ;; Look for a specifier keyword clause.
6389 (when (or (looking-at c-prefix-spec-kwds-re)
6390 (and (c-major-mode-is 'java-mode)
6391 (looking-at "@[A-Za-z0-9]+")))
6392 (if (looking-at c-typedef-key)
6393 (setq at-typedef t))
6394 (setq kwd-sym (c-keyword-sym (match-string 1)))
6395 (save-excursion
6396 (c-forward-keyword-clause 1)
6397 (setq kwd-clause-end (point))))
6399 (when (setq found-type (c-forward-type t)) ; brace-block-too
6400 ;; Found a known or possible type or a prefix of a known type.
6402 (when at-type
6403 ;; Got two identifiers with nothing but whitespace
6404 ;; between them. That can only happen in declarations.
6405 (setq at-decl-or-cast 'ids)
6407 (when (eq at-type 'found)
6408 ;; If the previous identifier is a found type we
6409 ;; record it as a real one; it might be some sort of
6410 ;; alias for a prefix like "unsigned".
6411 (save-excursion
6412 (goto-char type-start)
6413 (let ((c-promote-possible-types t))
6414 (c-forward-type)))))
6416 (setq backup-at-type at-type
6417 backup-type-start type-start
6418 backup-id-start id-start
6419 at-type found-type
6420 type-start start
6421 id-start (point)
6422 ;; The previous ambiguous specifier/type turned out
6423 ;; to be a type since we've parsed another one after
6424 ;; it, so clear these backup flags.
6425 backup-at-type-decl nil
6426 backup-maybe-typeless nil))
6428 (if kwd-sym
6429 (progn
6430 ;; Handle known specifier keywords and
6431 ;; `c-decl-hangon-kwds' which can occur after known
6432 ;; types.
6434 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6435 ;; It's a hang-on keyword that can occur anywhere.
6436 (progn
6437 (setq at-decl-or-cast t)
6438 (if at-type
6439 ;; Move the identifier start position if
6440 ;; we've passed a type.
6441 (setq id-start kwd-clause-end)
6442 ;; Otherwise treat this as a specifier and
6443 ;; move the fallback position.
6444 (setq start-pos kwd-clause-end))
6445 (goto-char kwd-clause-end))
6447 ;; It's an ordinary specifier so we know that
6448 ;; anything before this can't be the type.
6449 (setq backup-at-type nil
6450 start-pos kwd-clause-end)
6452 (if found-type
6453 ;; It's ambiguous whether this keyword is a
6454 ;; specifier or a type prefix, so set the backup
6455 ;; flags. (It's assumed that `c-forward-type'
6456 ;; moved further than `c-forward-keyword-clause'.)
6457 (progn
6458 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6459 (setq backup-at-type-decl t))
6460 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6461 (setq backup-maybe-typeless t)))
6463 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6464 ;; This test only happens after we've scanned a type.
6465 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6466 (setq at-type-decl t))
6467 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6468 (setq maybe-typeless t))
6470 ;; Haven't matched a type so it's an umambiguous
6471 ;; specifier keyword and we know we're in a
6472 ;; declaration.
6473 (setq at-decl-or-cast t)
6475 (goto-char kwd-clause-end))))
6477 ;; If the type isn't known we continue so that we'll jump
6478 ;; over all specifiers and type identifiers. The reason
6479 ;; to do this for a known type prefix is to make things
6480 ;; like "unsigned INT16" work.
6481 (and found-type (not (eq found-type t))))))
6483 (cond
6484 ((eq at-type t)
6485 ;; If a known type was found, we still need to skip over any
6486 ;; hangon keyword clauses after it. Otherwise it has already
6487 ;; been done in the loop above.
6488 (while (looking-at c-decl-hangon-key)
6489 (c-forward-keyword-clause 1))
6490 (setq id-start (point)))
6492 ((eq at-type 'prefix)
6493 ;; A prefix type is itself a primitive type when it's not
6494 ;; followed by another type.
6495 (setq at-type t))
6497 ((not at-type)
6498 ;; Got no type but set things up to continue anyway to handle
6499 ;; the various cases when a declaration doesn't start with a
6500 ;; type.
6501 (setq id-start start-pos))
6503 ((and (eq at-type 'maybe)
6504 (c-major-mode-is 'c++-mode))
6505 ;; If it's C++ then check if the last "type" ends on the form
6506 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6507 ;; (con|de)structor.
6508 (save-excursion
6509 (let (name end-2 end-1)
6510 (goto-char id-start)
6511 (c-backward-syntactic-ws)
6512 (setq end-2 (point))
6513 (when (and
6514 (c-simple-skip-symbol-backward)
6515 (progn
6516 (setq name
6517 (buffer-substring-no-properties (point) end-2))
6518 ;; Cheating in the handling of syntactic ws below.
6519 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6520 (progn
6521 (setq end-1 (point))
6522 (c-simple-skip-symbol-backward))
6523 (>= (point) type-start)
6524 (equal (buffer-substring-no-properties (point) end-1)
6525 name))
6526 ;; It is a (con|de)structor name. In that case the
6527 ;; declaration is typeless so zap out any preceding
6528 ;; identifier(s) that we might have taken as types.
6529 (goto-char type-start)
6530 (setq at-type nil
6531 backup-at-type nil
6532 id-start type-start))))))
6534 ;; Check for and step over a type decl expression after the thing
6535 ;; that is or might be a type. This can't be skipped since we
6536 ;; need the correct end position of the declarator for
6537 ;; `max-type-decl-end-*'.
6538 (let ((start (point)) (paren-depth 0) pos
6539 ;; True if there's a non-open-paren match of
6540 ;; `c-type-decl-prefix-key'.
6541 got-prefix
6542 ;; True if the declarator is surrounded by a parenthesis pair.
6543 got-parens
6544 ;; True if there is an identifier in the declarator.
6545 got-identifier
6546 ;; True if there's a non-close-paren match of
6547 ;; `c-type-decl-suffix-key'.
6548 got-suffix
6549 ;; True if there's a prefix match outside the outermost
6550 ;; paren pair that surrounds the declarator.
6551 got-prefix-before-parens
6552 ;; True if there's a suffix match outside the outermost
6553 ;; paren pair that surrounds the declarator. The value is
6554 ;; the position of the first suffix match.
6555 got-suffix-after-parens
6556 ;; True if we've parsed the type decl to a token that is
6557 ;; known to end declarations in this context.
6558 at-decl-end
6559 ;; The earlier values of `at-type' and `type-start' if we've
6560 ;; shifted the type backwards.
6561 identifier-type identifier-start
6562 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6563 ;; turn it off during the name skipping below to avoid
6564 ;; getting `c-type' properties that might be bogus. That
6565 ;; can happen since we don't know if
6566 ;; `c-restricted-<>-arglists' will be correct inside the
6567 ;; arglist paren that gets entered.
6568 c-parse-and-markup-<>-arglists)
6570 (goto-char id-start)
6572 ;; Skip over type decl prefix operators. (Note similar code in
6573 ;; `c-font-lock-declarators'.)
6574 (while (and (looking-at c-type-decl-prefix-key)
6575 (if (and (c-major-mode-is 'c++-mode)
6576 (match-beginning 3))
6577 ;; If the second submatch matches in C++ then
6578 ;; we're looking at an identifier that's a
6579 ;; prefix only if it specifies a member pointer.
6580 (when (setq got-identifier (c-forward-name))
6581 (if (looking-at "\\(::\\)")
6582 ;; We only check for a trailing "::" and
6583 ;; let the "*" that should follow be
6584 ;; matched in the next round.
6585 (progn (setq got-identifier nil) t)
6586 ;; It turned out to be the real identifier,
6587 ;; so stop.
6588 nil))
6591 (if (eq (char-after) ?\()
6592 (progn
6593 (setq paren-depth (1+ paren-depth))
6594 (forward-char))
6595 (unless got-prefix-before-parens
6596 (setq got-prefix-before-parens (= paren-depth 0)))
6597 (setq got-prefix t)
6598 (goto-char (match-end 1)))
6599 (c-forward-syntactic-ws))
6601 (setq got-parens (> paren-depth 0))
6603 ;; Skip over an identifier.
6604 (or got-identifier
6605 (and (looking-at c-identifier-start)
6606 (setq got-identifier (c-forward-name))))
6608 ;; Skip over type decl suffix operators.
6609 (while (if (looking-at c-type-decl-suffix-key)
6611 (if (eq (char-after) ?\))
6612 (when (> paren-depth 0)
6613 (setq paren-depth (1- paren-depth))
6614 (forward-char)
6616 (when (if (save-match-data (looking-at "\\s\("))
6617 (c-safe (c-forward-sexp 1) t)
6618 (goto-char (match-end 1))
6620 (when (and (not got-suffix-after-parens)
6621 (= paren-depth 0))
6622 (setq got-suffix-after-parens (match-beginning 0)))
6623 (setq got-suffix t)))
6625 ;; No suffix matched. We might have matched the
6626 ;; identifier as a type and the open paren of a
6627 ;; function arglist as a type decl prefix. In that
6628 ;; case we should "backtrack": Reinterpret the last
6629 ;; type as the identifier, move out of the arglist and
6630 ;; continue searching for suffix operators.
6632 ;; Do this even if there's no preceding type, to cope
6633 ;; with old style function declarations in K&R C,
6634 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6635 ;; style declarations. That isn't applicable in an
6636 ;; arglist context, though.
6637 (when (and (= paren-depth 1)
6638 (not got-prefix-before-parens)
6639 (not (eq at-type t))
6640 (or backup-at-type
6641 maybe-typeless
6642 backup-maybe-typeless
6643 (when c-recognize-typeless-decls
6644 (not context)))
6645 (setq pos (c-up-list-forward (point)))
6646 (eq (char-before pos) ?\)))
6647 (c-fdoc-shift-type-backward)
6648 (goto-char pos)
6651 (c-forward-syntactic-ws))
6653 (when (and (or maybe-typeless backup-maybe-typeless)
6654 (not got-identifier)
6655 (not got-prefix)
6656 at-type)
6657 ;; Have found no identifier but `c-typeless-decl-kwds' has
6658 ;; matched so we know we're inside a declaration. The
6659 ;; preceding type must be the identifier instead.
6660 (c-fdoc-shift-type-backward))
6662 (setq
6663 at-decl-or-cast
6664 (catch 'at-decl-or-cast
6666 ;; CASE 1
6667 (when (> paren-depth 0)
6668 ;; Encountered something inside parens that isn't matched by
6669 ;; the `c-type-decl-*' regexps, so it's not a type decl
6670 ;; expression. Try to skip out to the same paren depth to
6671 ;; not confuse the cast check below.
6672 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6673 ;; If we've found a specifier keyword then it's a
6674 ;; declaration regardless.
6675 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6677 (setq at-decl-end
6678 (looking-at (cond ((eq context '<>) "[,>]")
6679 (context "[,\)]")
6680 (t "[,;]"))))
6682 ;; Now we've collected info about various characteristics of
6683 ;; the construct we're looking at. Below follows a decision
6684 ;; tree based on that. It's ordered to check more certain
6685 ;; signs before less certain ones.
6687 (if got-identifier
6688 (progn
6690 ;; CASE 2
6691 (when (and (or at-type maybe-typeless)
6692 (not (or got-prefix got-parens)))
6693 ;; Got another identifier directly after the type, so it's a
6694 ;; declaration.
6695 (throw 'at-decl-or-cast t))
6697 (when (and got-parens
6698 (not got-prefix)
6699 (not got-suffix-after-parens)
6700 (or backup-at-type
6701 maybe-typeless
6702 backup-maybe-typeless))
6703 ;; Got a declaration of the form "foo bar (gnu);" where we've
6704 ;; recognized "bar" as the type and "gnu" as the declarator.
6705 ;; In this case it's however more likely that "bar" is the
6706 ;; declarator and "gnu" a function argument or initializer (if
6707 ;; `c-recognize-paren-inits' is set), since the parens around
6708 ;; "gnu" would be superfluous if it's a declarator. Shift the
6709 ;; type one step backward.
6710 (c-fdoc-shift-type-backward)))
6712 ;; Found no identifier.
6714 (if backup-at-type
6715 (progn
6718 ;; CASE 3
6719 (when (= (point) start)
6720 ;; Got a plain list of identifiers. If a colon follows it's
6721 ;; a valid label, or maybe a bitfield. Otherwise the last
6722 ;; one probably is the declared identifier and we should
6723 ;; back up to the previous type, providing it isn't a cast.
6724 (if (and (eq (char-after) ?:)
6725 (not (c-major-mode-is 'java-mode)))
6726 (cond
6727 ;; If we've found a specifier keyword then it's a
6728 ;; declaration regardless.
6729 ((eq at-decl-or-cast t)
6730 (throw 'at-decl-or-cast t))
6731 ((and c-has-bitfields
6732 (eq at-decl-or-cast 'ids)) ; bitfield.
6733 (setq backup-if-not-cast t)
6734 (throw 'at-decl-or-cast t)))
6736 (setq backup-if-not-cast t)
6737 (throw 'at-decl-or-cast t)))
6739 ;; CASE 4
6740 (when (and got-suffix
6741 (not got-prefix)
6742 (not got-parens))
6743 ;; Got a plain list of identifiers followed by some suffix.
6744 ;; If this isn't a cast then the last identifier probably is
6745 ;; the declared one and we should back up to the previous
6746 ;; type.
6747 (setq backup-if-not-cast t)
6748 (throw 'at-decl-or-cast t)))
6750 ;; CASE 5
6751 (when (eq at-type t)
6752 ;; If the type is known we know that there can't be any
6753 ;; identifier somewhere else, and it's only in declarations in
6754 ;; e.g. function prototypes and in casts that the identifier may
6755 ;; be left out.
6756 (throw 'at-decl-or-cast t))
6758 (when (= (point) start)
6759 ;; Only got a single identifier (parsed as a type so far).
6760 ;; CASE 6
6761 (if (and
6762 ;; Check that the identifier isn't at the start of an
6763 ;; expression.
6764 at-decl-end
6765 (cond
6766 ((eq context 'decl)
6767 ;; Inside an arglist that contains declarations. If K&R
6768 ;; style declarations and parenthesis style initializers
6769 ;; aren't allowed then the single identifier must be a
6770 ;; type, else we require that it's known or found
6771 ;; (primitive types are handled above).
6772 (or (and (not c-recognize-knr-p)
6773 (not c-recognize-paren-inits))
6774 (memq at-type '(known found))))
6775 ((eq context '<>)
6776 ;; Inside a template arglist. Accept known and found
6777 ;; types; other identifiers could just as well be
6778 ;; constants in C++.
6779 (memq at-type '(known found)))))
6780 (throw 'at-decl-or-cast t)
6781 ;; CASE 7
6782 ;; Can't be a valid declaration or cast, but if we've found a
6783 ;; specifier it can't be anything else either, so treat it as
6784 ;; an invalid/unfinished declaration or cast.
6785 (throw 'at-decl-or-cast at-decl-or-cast))))
6787 (if (and got-parens
6788 (not got-prefix)
6789 (not context)
6790 (not (eq at-type t))
6791 (or backup-at-type
6792 maybe-typeless
6793 backup-maybe-typeless
6794 (when c-recognize-typeless-decls
6795 (or (not got-suffix)
6796 (not (looking-at
6797 c-after-suffixed-type-maybe-decl-key))))))
6798 ;; Got an empty paren pair and a preceding type that probably
6799 ;; really is the identifier. Shift the type backwards to make
6800 ;; the last one the identifier. This is analogous to the
6801 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6802 ;; above.
6804 ;; Exception: In addition to the conditions in that
6805 ;; "backtracking" code, do not shift backward if we're not
6806 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6807 ;; Since there's no preceding type, the shift would mean that
6808 ;; the declaration is typeless. But if the regexp doesn't match
6809 ;; then we will simply fall through in the tests below and not
6810 ;; recognize it at all, so it's better to try it as an abstract
6811 ;; declarator instead.
6812 (c-fdoc-shift-type-backward)
6814 ;; Still no identifier.
6815 ;; CASE 8
6816 (when (and got-prefix (or got-parens got-suffix))
6817 ;; Require `got-prefix' together with either `got-parens' or
6818 ;; `got-suffix' to recognize it as an abstract declarator:
6819 ;; `got-parens' only is probably an empty function call.
6820 ;; `got-suffix' only can build an ordinary expression together
6821 ;; with the preceding identifier which we've taken as a type.
6822 ;; We could actually accept on `got-prefix' only, but that can
6823 ;; easily occur temporarily while writing an expression so we
6824 ;; avoid that case anyway. We could do a better job if we knew
6825 ;; the point when the fontification was invoked.
6826 (throw 'at-decl-or-cast t))
6828 ;; CASE 9
6829 (when (and at-type
6830 (not got-prefix)
6831 (not got-parens)
6832 got-suffix-after-parens
6833 (eq (char-after got-suffix-after-parens) ?\())
6834 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6835 ;; normal function call afterall (or perhaps a C++ style object
6836 ;; instantiation expression).
6837 (throw 'at-decl-or-cast nil))))
6839 ;; CASE 10
6840 (when at-decl-or-cast
6841 ;; By now we've located the type in the declaration that we know
6842 ;; we're in.
6843 (throw 'at-decl-or-cast t))
6845 ;; CASE 11
6846 (when (and got-identifier
6847 (not context)
6848 (looking-at c-after-suffixed-type-decl-key)
6849 (if (and got-parens
6850 (not got-prefix)
6851 (not got-suffix)
6852 (not (eq at-type t)))
6853 ;; Shift the type backward in the case that there's a
6854 ;; single identifier inside parens. That can only
6855 ;; occur in K&R style function declarations so it's
6856 ;; more likely that it really is a function call.
6857 ;; Therefore we only do this after
6858 ;; `c-after-suffixed-type-decl-key' has matched.
6859 (progn (c-fdoc-shift-type-backward) t)
6860 got-suffix-after-parens))
6861 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6862 (throw 'at-decl-or-cast t))
6864 ;; CASE 12
6865 (when (and (or got-prefix (not got-parens))
6866 (memq at-type '(t known)))
6867 ;; It's a declaration if a known type precedes it and it can't be a
6868 ;; function call.
6869 (throw 'at-decl-or-cast t))
6871 ;; If we get here we can't tell if this is a type decl or a normal
6872 ;; expression by looking at it alone. (That's under the assumption
6873 ;; that normal expressions always can look like type decl expressions,
6874 ;; which isn't really true but the cases where it doesn't hold are so
6875 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6876 ;; the effort to look for them.)
6878 (unless (or at-decl-end (looking-at "=[^=]"))
6879 ;; If this is a declaration it should end here or its initializer(*)
6880 ;; should start here, so check for allowed separation tokens. Note
6881 ;; that this rule doesn't work e.g. with a K&R arglist after a
6882 ;; function header.
6884 ;; *) Don't check for C++ style initializers using parens
6885 ;; since those already have been matched as suffixes.
6887 ;; If `at-decl-or-cast' is then we've found some other sign that
6888 ;; it's a declaration or cast, so then it's probably an
6889 ;; invalid/unfinished one.
6890 (throw 'at-decl-or-cast at-decl-or-cast))
6892 ;; Below are tests that only should be applied when we're certain to
6893 ;; not have parsed halfway through an expression.
6895 ;; CASE 14
6896 (when (memq at-type '(t known))
6897 ;; The expression starts with a known type so treat it as a
6898 ;; declaration.
6899 (throw 'at-decl-or-cast t))
6901 ;; CASE 15
6902 (when (and (c-major-mode-is 'c++-mode)
6903 ;; In C++ we check if the identifier is a known type, since
6904 ;; (con|de)structors use the class name as identifier.
6905 ;; We've always shifted over the identifier as a type and
6906 ;; then backed up again in this case.
6907 identifier-type
6908 (or (memq identifier-type '(found known))
6909 (and (eq (char-after identifier-start) ?~)
6910 ;; `at-type' probably won't be 'found for
6911 ;; destructors since the "~" is then part of the
6912 ;; type name being checked against the list of
6913 ;; known types, so do a check without that
6914 ;; operator.
6915 (or (save-excursion
6916 (goto-char (1+ identifier-start))
6917 (c-forward-syntactic-ws)
6918 (c-with-syntax-table
6919 c-identifier-syntax-table
6920 (looking-at c-known-type-key)))
6921 (save-excursion
6922 (goto-char (1+ identifier-start))
6923 ;; We have already parsed the type earlier,
6924 ;; so it'd be possible to cache the end
6925 ;; position instead of redoing it here, but
6926 ;; then we'd need to keep track of another
6927 ;; position everywhere.
6928 (c-check-type (point)
6929 (progn (c-forward-type)
6930 (point))))))))
6931 (throw 'at-decl-or-cast t))
6933 (if got-identifier
6934 (progn
6935 ;; CASE 16
6936 (when (and got-prefix-before-parens
6937 at-type
6938 (or at-decl-end (looking-at "=[^=]"))
6939 (not context)
6940 (not got-suffix))
6941 ;; Got something like "foo * bar;". Since we're not inside an
6942 ;; arglist it would be a meaningless expression because the
6943 ;; result isn't used. We therefore choose to recognize it as
6944 ;; a declaration. Do not allow a suffix since it could then
6945 ;; be a function call.
6946 (throw 'at-decl-or-cast t))
6948 ;; CASE 17
6949 (when (and (or got-suffix-after-parens
6950 (looking-at "=[^=]"))
6951 (eq at-type 'found)
6952 (not (eq context 'arglist)))
6953 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
6954 ;; be an odd expression or it could be a declaration. Treat
6955 ;; it as a declaration if "a" has been used as a type
6956 ;; somewhere else (if it's a known type we won't get here).
6957 (throw 'at-decl-or-cast t)))
6959 ;; CASE 18
6960 (when (and context
6961 (or got-prefix
6962 (and (eq context 'decl)
6963 (not c-recognize-paren-inits)
6964 (or got-parens got-suffix))))
6965 ;; Got a type followed by an abstract declarator. If `got-prefix'
6966 ;; is set it's something like "a *" without anything after it. If
6967 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
6968 ;; or similar, which we accept only if the context rules out
6969 ;; expressions.
6970 (throw 'at-decl-or-cast t)))
6972 ;; If we had a complete symbol table here (which rules out
6973 ;; `c-found-types') we should return t due to the disambiguation rule
6974 ;; (in at least C++) that anything that can be parsed as a declaration
6975 ;; is a declaration. Now we're being more defensive and prefer to
6976 ;; highlight things like "foo (bar);" as a declaration only if we're
6977 ;; inside an arglist that contains declarations.
6978 (eq context 'decl))))
6980 ;; The point is now after the type decl expression.
6982 (cond
6983 ;; Check for a cast.
6984 ((save-excursion
6985 (and
6986 c-cast-parens
6988 ;; Should be the first type/identifier in a cast paren.
6989 (> preceding-token-end (point-min))
6990 (memq (char-before preceding-token-end) c-cast-parens)
6992 ;; The closing paren should follow.
6993 (progn
6994 (c-forward-syntactic-ws)
6995 (looking-at "\\s\)"))
6997 ;; There should be a primary expression after it.
6998 (let (pos)
6999 (forward-char)
7000 (c-forward-syntactic-ws)
7001 (setq cast-end (point))
7002 (and (looking-at c-primary-expr-regexp)
7003 (progn
7004 (setq pos (match-end 0))
7006 ;; Check if the expression begins with a prefix keyword.
7007 (match-beginning 2)
7008 (if (match-beginning 1)
7009 ;; Expression begins with an ambiguous operator. Treat
7010 ;; it as a cast if it's a type decl or if we've
7011 ;; recognized the type somewhere else.
7012 (or at-decl-or-cast
7013 (memq at-type '(t known found)))
7014 ;; Unless it's a keyword, it's the beginning of a primary
7015 ;; expression.
7016 (not (looking-at c-keywords-regexp)))))
7017 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
7018 ;; that it matched a whole one so that we don't e.g. confuse
7019 ;; the operator '-' with '->'. It's ok if it matches further,
7020 ;; though, since it e.g. can match the float '.5' while the
7021 ;; operator regexp only matches '.'.
7022 (or (not (looking-at c-nonsymbol-token-regexp))
7023 (<= (match-end 0) pos))))
7025 ;; There should either be a cast before it or something that isn't an
7026 ;; identifier or close paren.
7027 (> preceding-token-end (point-min))
7028 (progn
7029 (goto-char (1- preceding-token-end))
7030 (or (eq (point) last-cast-end)
7031 (progn
7032 (c-backward-syntactic-ws)
7033 (if (< (skip-syntax-backward "w_") 0)
7034 ;; It's a symbol. Accept it only if it's one of the
7035 ;; keywords that can precede an expression (without
7036 ;; surrounding parens).
7037 (looking-at c-simple-stmt-key)
7038 (and
7039 ;; Check that it isn't a close paren (block close is ok,
7040 ;; though).
7041 (not (memq (char-before) '(?\) ?\])))
7042 ;; Check that it isn't a nonsymbol identifier.
7043 (not (c-on-identifier)))))))))
7045 ;; Handle the cast.
7046 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7047 (let ((c-promote-possible-types t))
7048 (goto-char type-start)
7049 (c-forward-type)))
7051 (goto-char cast-end)
7052 'cast)
7054 (at-decl-or-cast
7055 ;; We're at a declaration. Highlight the type and the following
7056 ;; declarators.
7058 (when backup-if-not-cast
7059 (c-fdoc-shift-type-backward t))
7061 (when (and (eq context 'decl) (looking-at ","))
7062 ;; Make sure to propagate the `c-decl-arg-start' property to
7063 ;; the next argument if it's set in this one, to cope with
7064 ;; interactive refontification.
7065 (c-put-c-type-property (point) 'c-decl-arg-start))
7067 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7068 (let ((c-promote-possible-types t))
7069 (save-excursion
7070 (goto-char type-start)
7071 (c-forward-type))))
7073 (cons id-start
7074 (and (or at-type-decl at-typedef)
7075 (cons at-type-decl at-typedef))))
7078 ;; False alarm. Restore the recorded ranges.
7079 (setq c-record-type-identifiers save-rec-type-ids
7080 c-record-ref-identifiers save-rec-ref-ids)
7081 nil))))
7083 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
7084 ;; Assuming that point is at the beginning of a token, check if it starts a
7085 ;; label and if so move over it and return non-nil (t in default situations,
7086 ;; specific symbols (see below) for interesting situations), otherwise don't
7087 ;; move and return nil. "Label" here means "most things with a colon".
7089 ;; More precisely, a "label" is regarded as one of:
7090 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7091 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7092 ;; bare "case", should the colon be missing. We return t;
7093 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7094 ;; return t;
7095 ;; (iv) One of QT's "extended" C++ variants of
7096 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7097 ;; Returns the symbol `qt-2kwds-colon'.
7098 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
7099 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
7100 ;; colon). Currently (2006-03), this applies only to Objective C's
7101 ;; keywords "@private", "@protected", and "@public". Returns t.
7103 ;; One of the things which will NOT be recognised as a label is a bit-field
7104 ;; element of a struct, something like "int foo:5".
7106 ;; The end of the label is taken to be just after the colon, or the end of
7107 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7108 ;; after the end on return. The terminating char gets marked with
7109 ;; `c-decl-end' to improve recognition of the following declaration or
7110 ;; statement.
7112 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7113 ;; label, if any, has already been marked up like that.
7115 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7116 ;; after the preceding token, i.e. on the other side of the
7117 ;; syntactic ws from the point. Use a value less than or equal to
7118 ;; (point-min) if the point is at the first token in (the visible
7119 ;; part of) the buffer.
7121 ;; The optional LIMIT limits the forward scan for the colon.
7123 ;; This function records the ranges of the label symbols on
7124 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7125 ;; non-nil.
7127 ;; This function might do hidden buffer changes.
7129 (let ((start (point))
7130 label-end
7131 qt-symbol-idx
7132 macro-start ; if we're in one.
7133 label-type
7134 kwd)
7135 (cond
7136 ;; "case" or "default" (Doesn't apply to AWK).
7137 ((looking-at c-label-kwds-regexp)
7138 (let ((kwd-end (match-end 1)))
7139 ;; Record only the keyword itself for fontification, since in
7140 ;; case labels the following is a constant expression and not
7141 ;; a label.
7142 (when c-record-type-identifiers
7143 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7145 ;; Find the label end.
7146 (goto-char kwd-end)
7147 (setq label-type
7148 (if (and (c-syntactic-re-search-forward
7149 ;; Stop on chars that aren't allowed in expressions,
7150 ;; and on operator chars that would be meaningless
7151 ;; there. FIXME: This doesn't cope with ?: operators.
7152 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7153 limit t t nil 1)
7154 (match-beginning 2))
7156 (progn ; there's a proper :
7157 (goto-char (match-beginning 2)) ; just after the :
7158 (c-put-c-type-property (1- (point)) 'c-decl-end)
7161 ;; It's an unfinished label. We consider the keyword enough
7162 ;; to recognize it as a label, so that it gets fontified.
7163 ;; Leave the point at the end of it, but don't put any
7164 ;; `c-decl-end' marker.
7165 (goto-char kwd-end)
7166 t))))
7168 ;; @private, @protected, @public, in Objective C, or similar.
7169 ((and c-opt-extra-label-key
7170 (looking-at c-opt-extra-label-key))
7171 ;; For a `c-opt-extra-label-key' match, we record the whole
7172 ;; thing for fontification. That's to get the leading '@' in
7173 ;; Objective-C protection labels fontified.
7174 (goto-char (match-end 1))
7175 (when c-record-type-identifiers
7176 (c-record-ref-id (cons (match-beginning 1) (point))))
7177 (c-put-c-type-property (1- (point)) 'c-decl-end)
7178 (setq label-type t))
7180 ;; All other cases of labels.
7181 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
7183 ;; A colon label must have something before the colon.
7184 (not (eq (char-after) ?:))
7186 ;; Check that we're not after a token that can't precede a label.
7188 ;; Trivially succeeds when there's no preceding token.
7189 ;; Succeeds when we're at a virtual semicolon.
7190 (if preceding-token-end
7191 (<= preceding-token-end (point-min))
7192 (save-excursion
7193 (c-backward-syntactic-ws)
7194 (setq preceding-token-end (point))
7195 (or (bobp)
7196 (c-at-vsemi-p))))
7198 ;; Check if we're after a label, if we're after a closing
7199 ;; paren that belong to statement, and with
7200 ;; `c-label-prefix-re'. It's done in different order
7201 ;; depending on `assume-markup' since the checks have
7202 ;; different expensiveness.
7203 (if assume-markup
7205 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7206 'c-decl-end)
7208 (save-excursion
7209 (goto-char (1- preceding-token-end))
7210 (c-beginning-of-current-token)
7211 (or (looking-at c-label-prefix-re)
7212 (looking-at c-block-stmt-1-key)))
7214 (and (eq (char-before preceding-token-end) ?\))
7215 (c-after-conditional)))
7218 (save-excursion
7219 (goto-char (1- preceding-token-end))
7220 (c-beginning-of-current-token)
7221 (or (looking-at c-label-prefix-re)
7222 (looking-at c-block-stmt-1-key)))
7224 (cond
7225 ((eq (char-before preceding-token-end) ?\))
7226 (c-after-conditional))
7228 ((eq (char-before preceding-token-end) ?:)
7229 ;; Might be after another label, so check it recursively.
7230 (save-restriction
7231 (save-excursion
7232 (goto-char (1- preceding-token-end))
7233 ;; Essentially the same as the
7234 ;; `c-syntactic-re-search-forward' regexp below.
7235 (setq macro-start
7236 (save-excursion (and (c-beginning-of-macro)
7237 (point))))
7238 (if macro-start (narrow-to-region macro-start (point-max)))
7239 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7240 ;; Note: the following should work instead of the
7241 ;; narrow-to-region above. Investigate why not,
7242 ;; sometime. ACM, 2006-03-31.
7243 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7244 ;; macro-start t)
7245 (let ((pte (point))
7246 ;; If the caller turned on recording for us,
7247 ;; it shouldn't apply when we check the
7248 ;; preceding label.
7249 c-record-type-identifiers)
7250 ;; A label can't start at a cpp directive. Check for
7251 ;; this, since c-forward-syntactic-ws would foul up on it.
7252 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7253 (c-forward-syntactic-ws)
7254 (c-forward-label nil pte start))))))))))
7256 ;; Point is still at the beginning of the possible label construct.
7258 ;; Check that the next nonsymbol token is ":", or that we're in one
7259 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7260 ;; arguments. FIXME: Should build this regexp from the language
7261 ;; constants.
7262 (cond
7263 ;; public: protected: private:
7264 ((and
7265 (c-major-mode-is 'c++-mode)
7266 (search-forward-regexp
7267 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7268 (progn (backward-char)
7269 (c-forward-syntactic-ws limit)
7270 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7271 (forward-char)
7272 (setq label-type t))
7273 ;; QT double keyword like "protected slots:" or goto target.
7274 ((progn (goto-char start) nil))
7275 ((when (c-syntactic-re-search-forward
7276 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7277 (backward-char)
7278 (setq label-end (point))
7279 (setq qt-symbol-idx
7280 (and (c-major-mode-is 'c++-mode)
7281 (string-match
7282 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7283 (buffer-substring start (point)))))
7284 (c-forward-syntactic-ws limit)
7285 (cond
7286 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7287 (forward-char)
7288 (setq label-type
7289 (if (or (string= "signals" ; Special QT macro
7290 (setq kwd (buffer-substring-no-properties start label-end)))
7291 (string= "Q_SIGNALS" kwd))
7292 'qt-1kwd-colon
7293 'goto-target)))
7294 ((and qt-symbol-idx
7295 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7296 (progn (c-forward-syntactic-ws limit)
7297 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7298 (forward-char)
7299 (setq label-type 'qt-2kwds-colon)))))))
7301 (save-restriction
7302 (narrow-to-region start (point))
7304 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7305 (catch 'check-label
7306 (goto-char start)
7307 (while (progn
7308 (when (looking-at c-nonlabel-token-key)
7309 (goto-char start)
7310 (setq label-type nil)
7311 (throw 'check-label nil))
7312 (and (c-safe (c-forward-sexp)
7313 (c-forward-syntactic-ws)
7315 (not (eobp)))))
7317 ;; Record the identifiers in the label for fontification, unless
7318 ;; it begins with `c-label-kwds' in which case the following
7319 ;; identifiers are part of a (constant) expression that
7320 ;; shouldn't be fontified.
7321 (when (and c-record-type-identifiers
7322 (progn (goto-char start)
7323 (not (looking-at c-label-kwds-regexp))))
7324 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7325 (c-record-ref-id (cons (match-beginning 0)
7326 (match-end 0)))))
7328 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7329 (goto-char (point-max)))))
7332 ;; Not a label.
7333 (goto-char start)))
7334 label-type))
7336 (defun c-forward-objc-directive ()
7337 ;; Assuming the point is at the beginning of a token, try to move
7338 ;; forward to the end of the Objective-C directive that starts
7339 ;; there. Return t if a directive was fully recognized, otherwise
7340 ;; the point is moved as far as one could be successfully parsed and
7341 ;; nil is returned.
7343 ;; This function records identifier ranges on
7344 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7345 ;; `c-record-type-identifiers' is non-nil.
7347 ;; This function might do hidden buffer changes.
7349 (let ((start (point))
7350 start-char
7351 (c-promote-possible-types t)
7352 ;; Turn off recognition of angle bracket arglists while parsing
7353 ;; types here since the protocol reference list might then be
7354 ;; considered part of the preceding name or superclass-name.
7355 c-recognize-<>-arglists)
7357 (if (or
7358 (when (looking-at
7359 (eval-when-compile
7360 (c-make-keywords-re t
7361 (append (c-lang-const c-protection-kwds objc)
7362 '("@end"))
7363 'objc-mode)))
7364 (goto-char (match-end 1))
7367 (and
7368 (looking-at
7369 (eval-when-compile
7370 (c-make-keywords-re t
7371 '("@interface" "@implementation" "@protocol")
7372 'objc-mode)))
7374 ;; Handle the name of the class itself.
7375 (progn
7376 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7377 ; at EOB.
7378 (goto-char (match-end 0))
7379 (c-skip-ws-forward)
7380 (c-forward-type))
7382 (catch 'break
7383 ;; Look for ": superclass-name" or "( category-name )".
7384 (when (looking-at "[:\(]")
7385 (setq start-char (char-after))
7386 (forward-char)
7387 (c-forward-syntactic-ws)
7388 (unless (c-forward-type) (throw 'break nil))
7389 (when (eq start-char ?\()
7390 (unless (eq (char-after) ?\)) (throw 'break nil))
7391 (forward-char)
7392 (c-forward-syntactic-ws)))
7394 ;; Look for a protocol reference list.
7395 (if (eq (char-after) ?<)
7396 (let ((c-recognize-<>-arglists t)
7397 (c-parse-and-markup-<>-arglists t)
7398 c-restricted-<>-arglists)
7399 (c-forward-<>-arglist t))
7400 t))))
7402 (progn
7403 (c-backward-syntactic-ws)
7404 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7405 (c-put-c-type-property (1- (point)) 'c-decl-end)
7408 (c-clear-c-type-property start (point) 'c-decl-end)
7409 nil)))
7411 (defun c-beginning-of-inheritance-list (&optional lim)
7412 ;; Go to the first non-whitespace after the colon that starts a
7413 ;; multiple inheritance introduction. Optional LIM is the farthest
7414 ;; back we should search.
7416 ;; This function might do hidden buffer changes.
7417 (c-with-syntax-table c++-template-syntax-table
7418 (c-backward-token-2 0 t lim)
7419 (while (and (or (looking-at c-symbol-start)
7420 (looking-at "[<,]\\|::"))
7421 (zerop (c-backward-token-2 1 t lim))))))
7423 (defun c-in-method-def-p ()
7424 ;; Return nil if we aren't in a method definition, otherwise the
7425 ;; position of the initial [+-].
7427 ;; This function might do hidden buffer changes.
7428 (save-excursion
7429 (beginning-of-line)
7430 (and c-opt-method-key
7431 (looking-at c-opt-method-key)
7432 (point))
7435 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7436 (defun c-in-gcc-asm-p ()
7437 ;; Return non-nil if point is within a gcc \"asm\" block.
7439 ;; This should be called with point inside an argument list.
7441 ;; Only one level of enclosing parentheses is considered, so for
7442 ;; instance `nil' is returned when in a function call within an asm
7443 ;; operand.
7445 ;; This function might do hidden buffer changes.
7447 (and c-opt-asm-stmt-key
7448 (save-excursion
7449 (beginning-of-line)
7450 (backward-up-list 1)
7451 (c-beginning-of-statement-1 (point-min) nil t)
7452 (looking-at c-opt-asm-stmt-key))))
7454 (defun c-at-toplevel-p ()
7455 "Return a determination as to whether point is \"at the top level\".
7456 Informally, \"at the top level\" is anywhere where you can write
7457 a function.
7459 More precisely, being at the top-level means that point is either
7460 outside any enclosing block (such as a function definition), or
7461 directly inside a class, namespace or other block that contains
7462 another declaration level.
7464 If point is not at the top-level (e.g. it is inside a method
7465 definition), then nil is returned. Otherwise, if point is at a
7466 top-level not enclosed within a class definition, t is returned.
7467 Otherwise, a 2-vector is returned where the zeroth element is the
7468 buffer position of the start of the class declaration, and the first
7469 element is the buffer position of the enclosing class's opening
7470 brace.
7472 Note that this function might do hidden buffer changes. See the
7473 comment at the start of cc-engine.el for more info."
7474 (let ((paren-state (c-parse-state)))
7475 (or (not (c-most-enclosing-brace paren-state))
7476 (c-search-uplist-for-classkey paren-state))))
7478 (defun c-just-after-func-arglist-p (&optional lim)
7479 ;; Return non-nil if the point is in the region after the argument
7480 ;; list of a function and its opening brace (or semicolon in case it
7481 ;; got no body). If there are K&R style argument declarations in
7482 ;; that region, the point has to be inside the first one for this
7483 ;; function to recognize it.
7485 ;; If successful, the point is moved to the first token after the
7486 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7487 ;; the position of the opening paren of the function arglist is
7488 ;; returned.
7490 ;; The point is clobbered if not successful.
7492 ;; LIM is used as bound for backward buffer searches.
7494 ;; This function might do hidden buffer changes.
7496 (let ((beg (point)) end id-start)
7497 (and
7498 (eq (c-beginning-of-statement-1 lim) 'same)
7500 (not (or (c-major-mode-is 'objc-mode)
7501 (c-forward-objc-directive)))
7503 (setq id-start
7504 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7505 (< id-start beg)
7507 ;; There should not be a '=' or ',' between beg and the
7508 ;; start of the declaration since that means we were in the
7509 ;; "expression part" of the declaration.
7510 (or (> (point) beg)
7511 (not (looking-at "[=,]")))
7513 (save-excursion
7514 ;; Check that there's an arglist paren in the
7515 ;; declaration.
7516 (goto-char id-start)
7517 (cond ((eq (char-after) ?\()
7518 ;; The declarator is a paren expression, so skip past it
7519 ;; so that we don't get stuck on that instead of the
7520 ;; function arglist.
7521 (c-forward-sexp))
7522 ((and c-opt-op-identifier-prefix
7523 (looking-at c-opt-op-identifier-prefix))
7524 ;; Don't trip up on "operator ()".
7525 (c-forward-token-2 2 t)))
7526 (and (< (point) beg)
7527 (c-syntactic-re-search-forward "(" beg t t)
7528 (1- (point)))))))
7530 (defun c-in-knr-argdecl (&optional lim)
7531 ;; Return the position of the first argument declaration if point is
7532 ;; inside a K&R style argument declaration list, nil otherwise.
7533 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7534 ;; position that bounds the backward search for the argument list.
7536 ;; Point must be within a possible K&R region, e.g. just before a top-level
7537 ;; "{". It must be outside of parens and brackets. The test can return
7538 ;; false positives otherwise.
7540 ;; This function might do hidden buffer changes.
7542 (save-excursion
7543 (save-restriction
7544 ;; If we're in a macro, our search range is restricted to it. Narrow to
7545 ;; the searchable range.
7546 (let* ((macro-start (c-query-macro-start))
7547 (lim (max (or lim (point-min)) (or macro-start (point-min))))
7548 before-lparen after-rparen
7549 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
7550 (narrow-to-region lim (c-point 'eol))
7552 ;; Search backwards for the defun's argument list. We give up if we
7553 ;; encounter a "}" (end of a previous defun) or BOB.
7555 ;; The criterion for a paren structure being the arg list is:
7556 ;; o - there is non-WS stuff after it but before any "{"; AND
7557 ;; o - the token after it isn't a ";" AND
7558 ;; o - it is preceded by either an identifier (the function name) or
7559 ;; a macro expansion like "DEFUN (...)"; AND
7560 ;; o - its content is a non-empty comma-separated list of identifiers
7561 ;; (an empty arg list won't have a knr region).
7563 ;; The following snippet illustrates these rules:
7564 ;; int foo (bar, baz, yuk)
7565 ;; int bar [] ;
7566 ;; int (*baz) (my_type) ;
7567 ;; int (*) (void) (*yuk) (void) ;
7568 ;; {
7570 (catch 'knr
7571 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7572 (setq pp-count-out (1- pp-count-out))
7573 (c-syntactic-skip-backward "^)]}")
7574 (cond ((eq (char-before) ?\))
7575 (setq after-rparen (point)))
7576 ((eq (char-before) ?\])
7577 (setq after-rparen nil))
7578 (t ; either } (hit previous defun) or no more parens/brackets
7579 (throw 'knr nil)))
7581 (if after-rparen
7582 ;; We're inside a paren. Could it be our argument list....?
7584 (and
7585 (progn
7586 (goto-char after-rparen)
7587 (unless (c-go-list-backward) (throw 'knr nil)) ;
7588 ;; FIXME!!! What about macros between the parens? 2007/01/20
7589 (setq before-lparen (point)))
7591 ;; It can't be the arg list if next token is ; or {
7592 (progn (goto-char after-rparen)
7593 (c-forward-syntactic-ws)
7594 (not (memq (char-after) '(?\; ?\{))))
7596 ;; Is the thing preceding the list an identifier (the
7597 ;; function name), or a macro expansion?
7598 (progn
7599 (goto-char before-lparen)
7600 (eq (c-backward-token-2) 0)
7601 (or (c-on-identifier)
7602 (and (eq (char-after) ?\))
7603 (c-go-up-list-backward)
7604 (eq (c-backward-token-2) 0)
7605 (c-on-identifier))))
7607 ;; Have we got a non-empty list of comma-separated
7608 ;; identifiers?
7609 (progn
7610 (goto-char before-lparen)
7611 (c-forward-token-2) ; to first token inside parens
7612 (and
7613 (c-on-identifier)
7614 (c-forward-token-2)
7615 (catch 'id-list
7616 (while (eq (char-after) ?\,)
7617 (c-forward-token-2)
7618 (unless (c-on-identifier) (throw 'id-list nil))
7619 (c-forward-token-2))
7620 (eq (char-after) ?\))))))
7622 ;; ...Yes. We've identified the function's argument list.
7623 (throw 'knr
7624 (progn (goto-char after-rparen)
7625 (c-forward-syntactic-ws)
7626 (point)))
7628 ;; ...No. The current parens aren't the function's arg list.
7629 (goto-char before-lparen))
7631 (or (c-go-list-backward) ; backwards over [ .... ]
7632 (throw 'knr nil)))))))))
7634 (defun c-skip-conditional ()
7635 ;; skip forward over conditional at point, including any predicate
7636 ;; statements in parentheses. No error checking is performed.
7638 ;; This function might do hidden buffer changes.
7639 (c-forward-sexp (cond
7640 ;; else if()
7641 ((looking-at (concat "\\<else"
7642 "\\([ \t\n]\\|\\\\\n\\)+"
7643 "if\\>\\([^_]\\|$\\)"))
7645 ;; do, else, try, finally
7646 ((looking-at (concat "\\<\\("
7647 "do\\|else\\|try\\|finally"
7648 "\\)\\>\\([^_]\\|$\\)"))
7650 ;; for, if, while, switch, catch, synchronized, foreach
7651 (t 2))))
7653 (defun c-after-conditional (&optional lim)
7654 ;; If looking at the token after a conditional then return the
7655 ;; position of its start, otherwise return nil.
7657 ;; This function might do hidden buffer changes.
7658 (save-excursion
7659 (and (zerop (c-backward-token-2 1 t lim))
7660 (or (looking-at c-block-stmt-1-key)
7661 (and (eq (char-after) ?\()
7662 (zerop (c-backward-token-2 1 t lim))
7663 (looking-at c-block-stmt-2-key)))
7664 (point))))
7666 (defun c-after-special-operator-id (&optional lim)
7667 ;; If the point is after an operator identifier that isn't handled
7668 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7669 ;; position of the start of that identifier is returned. nil is
7670 ;; returned otherwise. The point may be anywhere in the syntactic
7671 ;; whitespace after the last token of the operator identifier.
7673 ;; This function might do hidden buffer changes.
7674 (save-excursion
7675 (and c-overloadable-operators-regexp
7676 (zerop (c-backward-token-2 1 nil lim))
7677 (looking-at c-overloadable-operators-regexp)
7678 (or (not c-opt-op-identifier-prefix)
7679 (and
7680 (zerop (c-backward-token-2 1 nil lim))
7681 (looking-at c-opt-op-identifier-prefix)))
7682 (point))))
7684 (defsubst c-backward-to-block-anchor (&optional lim)
7685 ;; Assuming point is at a brace that opens a statement block of some
7686 ;; kind, move to the proper anchor point for that block. It might
7687 ;; need to be adjusted further by c-add-stmt-syntax, but the
7688 ;; position at return is suitable as start position for that
7689 ;; function.
7691 ;; This function might do hidden buffer changes.
7692 (unless (= (point) (c-point 'boi))
7693 (let ((start (c-after-conditional lim)))
7694 (if start
7695 (goto-char start)))))
7697 (defsubst c-backward-to-decl-anchor (&optional lim)
7698 ;; Assuming point is at a brace that opens the block of a top level
7699 ;; declaration of some kind, move to the proper anchor point for
7700 ;; that block.
7702 ;; This function might do hidden buffer changes.
7703 (unless (= (point) (c-point 'boi))
7704 (c-beginning-of-statement-1 lim)))
7706 (defun c-search-decl-header-end ()
7707 ;; Search forward for the end of the "header" of the current
7708 ;; declaration. That's the position where the definition body
7709 ;; starts, or the first variable initializer, or the ending
7710 ;; semicolon. I.e. search forward for the closest following
7711 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7712 ;; _after_ the first found token, or at point-max if none is found.
7714 ;; This function might do hidden buffer changes.
7716 (let ((base (point)))
7717 (if (c-major-mode-is 'c++-mode)
7719 ;; In C++ we need to take special care to handle operator
7720 ;; tokens and those pesky template brackets.
7721 (while (and
7722 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
7724 (c-end-of-current-token base)
7725 ;; Handle operator identifiers, i.e. ignore any
7726 ;; operator token preceded by "operator".
7727 (save-excursion
7728 (and (c-safe (c-backward-sexp) t)
7729 (looking-at c-opt-op-identifier-prefix)))
7730 (and (eq (char-before) ?<)
7731 (c-with-syntax-table c++-template-syntax-table
7732 (if (c-safe (goto-char (c-up-list-forward (point))))
7734 (goto-char (point-max))
7735 nil)))))
7736 (setq base (point)))
7738 (while (and
7739 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
7740 (c-end-of-current-token base))
7741 (setq base (point))))))
7743 (defun c-beginning-of-decl-1 (&optional lim)
7744 ;; Go to the beginning of the current declaration, or the beginning
7745 ;; of the previous one if already at the start of it. Point won't
7746 ;; be moved out of any surrounding paren. Return a cons cell of the
7747 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7748 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7749 ;; style argument declarations (and they are to be recognized) then
7750 ;; KNR-POS is set to the start of the first such argument
7751 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7752 ;; position that bounds the backward search.
7754 ;; NB: Cases where the declaration continues after the block, as in
7755 ;; "struct foo { ... } bar;", are currently recognized as two
7756 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7758 ;; This function might do hidden buffer changes.
7759 (catch 'return
7760 (let* ((start (point))
7761 (last-stmt-start (point))
7762 (move (c-beginning-of-statement-1 lim nil t)))
7764 ;; `c-beginning-of-statement-1' stops at a block start, but we
7765 ;; want to continue if the block doesn't begin a top level
7766 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7767 ;; or an open paren.
7768 (let ((beg (point)) tentative-move)
7769 ;; Go back one "statement" each time round the loop until we're just
7770 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7771 ;; an ObjC method. This will move over a multiple declaration whose
7772 ;; components are comma separated.
7773 (while (and
7774 ;; Must check with c-opt-method-key in ObjC mode.
7775 (not (and c-opt-method-key
7776 (looking-at c-opt-method-key)))
7777 (/= last-stmt-start (point))
7778 (progn
7779 (c-backward-syntactic-ws lim)
7780 (not (memq (char-before) '(?\; ?} ?: nil))))
7781 (save-excursion
7782 (backward-char)
7783 (not (looking-at "\\s(")))
7784 ;; Check that we don't move from the first thing in a
7785 ;; macro to its header.
7786 (not (eq (setq tentative-move
7787 (c-beginning-of-statement-1 lim nil t))
7788 'macro)))
7789 (setq last-stmt-start beg
7790 beg (point)
7791 move tentative-move))
7792 (goto-char beg))
7794 (when c-recognize-knr-p
7795 (let ((fallback-pos (point)) knr-argdecl-start)
7796 ;; Handle K&R argdecls. Back up after the "statement" jumped
7797 ;; over by `c-beginning-of-statement-1', unless it was the
7798 ;; function body, in which case we're sitting on the opening
7799 ;; brace now. Then test if we're in a K&R argdecl region and
7800 ;; that we started at the other side of the first argdecl in
7801 ;; it.
7802 (unless (eq (char-after) ?{)
7803 (goto-char last-stmt-start))
7804 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
7805 (< knr-argdecl-start start)
7806 (progn
7807 (goto-char knr-argdecl-start)
7808 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
7809 (throw 'return
7810 (cons (if (eq (char-after fallback-pos) ?{)
7811 'previous
7812 'same)
7813 knr-argdecl-start))
7814 (goto-char fallback-pos))))
7816 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7817 ;; statement, so the result will be 'previous if we've moved over any.
7818 ;; So change our result back to 'same if necessary.
7820 ;; If they were brace list initializers we might not have moved over a
7821 ;; declaration boundary though, so change it to 'same if we've moved
7822 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7823 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7824 ;; potentially can search over a large amount of text.). Take special
7825 ;; pains not to get mislead by C++'s "operator=", and the like.
7826 (if (and (eq move 'previous)
7827 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
7828 c++-template-syntax-table
7829 (syntax-table))
7830 (save-excursion
7831 (and
7832 (progn
7833 (while ; keep going back to "[;={"s until we either find
7834 ; no more, or get to one which isn't an "operator ="
7835 (and (c-syntactic-re-search-forward "[;={]" start t t t)
7836 (eq (char-before) ?=)
7837 c-overloadable-operators-regexp
7838 c-opt-op-identifier-prefix
7839 (save-excursion
7840 (eq (c-backward-token-2) 0)
7841 (looking-at c-overloadable-operators-regexp)
7842 (eq (c-backward-token-2) 0)
7843 (looking-at c-opt-op-identifier-prefix))))
7844 (eq (char-before) ?=))
7845 (c-syntactic-re-search-forward "[;{]" start t t)
7846 (eq (char-before) ?{)
7847 (c-safe (goto-char (c-up-list-forward (point))) t)
7848 (not (c-syntactic-re-search-forward ";" start t t))))))
7849 (cons 'same nil)
7850 (cons move nil)))))
7852 (defun c-end-of-decl-1 ()
7853 ;; Assuming point is at the start of a declaration (as detected by
7854 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7855 ;; `c-beginning-of-decl-1', this function handles the case when a
7856 ;; block is followed by identifiers in e.g. struct declarations in C
7857 ;; or C++. If a proper end was found then t is returned, otherwise
7858 ;; point is moved as far as possible within the current sexp and nil
7859 ;; is returned. This function doesn't handle macros; use
7860 ;; `c-end-of-macro' instead in those cases.
7862 ;; This function might do hidden buffer changes.
7863 (let ((start (point))
7864 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
7865 c++-template-syntax-table
7866 (syntax-table))))
7867 (catch 'return
7868 (c-search-decl-header-end)
7870 (when (and c-recognize-knr-p
7871 (eq (char-before) ?\;)
7872 (c-in-knr-argdecl start))
7873 ;; Stopped at the ';' in a K&R argdecl section which is
7874 ;; detected using the same criteria as in
7875 ;; `c-beginning-of-decl-1'. Move to the following block
7876 ;; start.
7877 (c-syntactic-re-search-forward "{" nil 'move t))
7879 (when (eq (char-before) ?{)
7880 ;; Encountered a block in the declaration. Jump over it.
7881 (condition-case nil
7882 (goto-char (c-up-list-forward (point)))
7883 (error (goto-char (point-max))
7884 (throw 'return nil)))
7885 (if (or (not c-opt-block-decls-with-vars-key)
7886 (save-excursion
7887 (c-with-syntax-table decl-syntax-table
7888 (let ((lim (point)))
7889 (goto-char start)
7890 (not (and
7891 ;; Check for `c-opt-block-decls-with-vars-key'
7892 ;; before the first paren.
7893 (c-syntactic-re-search-forward
7894 (concat "[;=\(\[{]\\|\\("
7895 c-opt-block-decls-with-vars-key
7896 "\\)")
7897 lim t t t)
7898 (match-beginning 1)
7899 (not (eq (char-before) ?_))
7900 ;; Check that the first following paren is
7901 ;; the block.
7902 (c-syntactic-re-search-forward "[;=\(\[{]"
7903 lim t t t)
7904 (eq (char-before) ?{)))))))
7905 ;; The declaration doesn't have any of the
7906 ;; `c-opt-block-decls-with-vars' keywords in the
7907 ;; beginning, so it ends here at the end of the block.
7908 (throw 'return t)))
7910 (c-with-syntax-table decl-syntax-table
7911 (while (progn
7912 (if (eq (char-before) ?\;)
7913 (throw 'return t))
7914 (c-syntactic-re-search-forward ";" nil 'move t))))
7915 nil)))
7917 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
7918 ;; Assuming the point is at an open brace, check if it starts a
7919 ;; block that contains another declaration level, i.e. that isn't a
7920 ;; statement block or a brace list, and if so return non-nil.
7922 ;; If the check is successful, the return value is the start of the
7923 ;; keyword that tells what kind of construct it is, i.e. typically
7924 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7925 ;; the point will be at the start of the construct, before any
7926 ;; leading specifiers, otherwise it's at the returned position.
7928 ;; The point is clobbered if the check is unsuccessful.
7930 ;; CONTAINING-SEXP is the position of the open of the surrounding
7931 ;; paren, or nil if none.
7933 ;; The optional LIMIT limits the backward search for the start of
7934 ;; the construct. It's assumed to be at a syntactically relevant
7935 ;; position.
7937 ;; If any template arglists are found in the searched region before
7938 ;; the open brace, they get marked with paren syntax.
7940 ;; This function might do hidden buffer changes.
7942 (let ((open-brace (point)) kwd-start first-specifier-pos)
7943 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7945 (when (and c-recognize-<>-arglists
7946 (eq (char-before) ?>))
7947 ;; Could be at the end of a template arglist.
7948 (let ((c-parse-and-markup-<>-arglists t)
7949 (c-disallow-comma-in-<>-arglists
7950 (and containing-sexp
7951 (not (eq (char-after containing-sexp) ?{)))))
7952 (while (and
7953 (c-backward-<>-arglist nil limit)
7954 (progn
7955 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7956 (eq (char-before) ?>))))))
7958 ;; Note: Can't get bogus hits inside template arglists below since they
7959 ;; have gotten paren syntax above.
7960 (when (and
7961 ;; If `goto-start' is set we begin by searching for the
7962 ;; first possible position of a leading specifier list.
7963 ;; The `c-decl-block-key' search continues from there since
7964 ;; we know it can't match earlier.
7965 (if goto-start
7966 (when (c-syntactic-re-search-forward c-symbol-start
7967 open-brace t t)
7968 (goto-char (setq first-specifier-pos (match-beginning 0)))
7972 (cond
7973 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
7974 (goto-char (setq kwd-start (match-beginning 0)))
7977 ;; Found a keyword that can't be a type?
7978 (match-beginning 1)
7980 ;; Can be a type too, in which case it's the return type of a
7981 ;; function (under the assumption that no declaration level
7982 ;; block construct starts with a type).
7983 (not (c-forward-type))
7985 ;; Jumped over a type, but it could be a declaration keyword
7986 ;; followed by the declared identifier that we've jumped over
7987 ;; instead (e.g. in "class Foo {"). If it indeed is a type
7988 ;; then we should be at the declarator now, so check for a
7989 ;; valid declarator start.
7991 ;; Note: This doesn't cope with the case when a declared
7992 ;; identifier is followed by e.g. '(' in a language where '('
7993 ;; also might be part of a declarator expression. Currently
7994 ;; there's no such language.
7995 (not (or (looking-at c-symbol-start)
7996 (looking-at c-type-decl-prefix-key)))))
7998 ;; In Pike a list of modifiers may be followed by a brace
7999 ;; to make them apply to many identifiers. Note that the
8000 ;; match data will be empty on return in this case.
8001 ((and (c-major-mode-is 'pike-mode)
8002 (progn
8003 (goto-char open-brace)
8004 (= (c-backward-token-2) 0))
8005 (looking-at c-specifier-key)
8006 ;; Use this variant to avoid yet another special regexp.
8007 (c-keyword-member (c-keyword-sym (match-string 1))
8008 'c-modifier-kwds))
8009 (setq kwd-start (point))
8010 t)))
8012 ;; Got a match.
8014 (if goto-start
8015 ;; Back up over any preceding specifiers and their clauses
8016 ;; by going forward from `first-specifier-pos', which is the
8017 ;; earliest possible position where the specifier list can
8018 ;; start.
8019 (progn
8020 (goto-char first-specifier-pos)
8022 (while (< (point) kwd-start)
8023 (if (looking-at c-symbol-key)
8024 ;; Accept any plain symbol token on the ground that
8025 ;; it's a specifier masked through a macro (just
8026 ;; like `c-forward-decl-or-cast-1' skip forward over
8027 ;; such tokens).
8029 ;; Could be more restrictive wrt invalid keywords,
8030 ;; but that'd only occur in invalid code so there's
8031 ;; no use spending effort on it.
8032 (let ((end (match-end 0)))
8033 (unless (c-forward-keyword-clause 0)
8034 (goto-char end)
8035 (c-forward-syntactic-ws)))
8037 ;; Can't parse a declaration preamble and is still
8038 ;; before `kwd-start'. That means `first-specifier-pos'
8039 ;; was in some earlier construct. Search again.
8040 (if (c-syntactic-re-search-forward c-symbol-start
8041 kwd-start 'move t)
8042 (goto-char (setq first-specifier-pos (match-beginning 0)))
8043 ;; Got no preamble before the block declaration keyword.
8044 (setq first-specifier-pos kwd-start))))
8046 (goto-char first-specifier-pos))
8047 (goto-char kwd-start))
8049 kwd-start)))
8051 (defun c-search-uplist-for-classkey (paren-state)
8052 ;; Check if the closest containing paren sexp is a declaration
8053 ;; block, returning a 2 element vector in that case. Aref 0
8054 ;; contains the bufpos at boi of the class key line, and aref 1
8055 ;; contains the bufpos of the open brace. This function is an
8056 ;; obsolete wrapper for `c-looking-at-decl-block'.
8058 ;; This function might do hidden buffer changes.
8059 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
8060 (when open-paren-pos
8061 (save-excursion
8062 (goto-char open-paren-pos)
8063 (when (and (eq (char-after) ?{)
8064 (c-looking-at-decl-block
8065 (c-safe-position open-paren-pos paren-state)
8066 nil))
8067 (back-to-indentation)
8068 (vector (point) open-paren-pos))))))
8070 (defmacro c-pull-open-brace (ps)
8071 ;; Pull the next open brace from PS (which has the form of paren-state),
8072 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
8073 `(progn
8074 (while (consp (car ,ps))
8075 (setq ,ps (cdr ,ps)))
8076 (prog1 (car ,ps)
8077 (setq ,ps (cdr ,ps)))))
8079 (defun c-most-enclosing-decl-block (paren-state)
8080 ;; Return the buffer position of the most enclosing decl-block brace (in the
8081 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
8082 ;; none was found.
8083 (let* ((open-brace (c-pull-open-brace paren-state))
8084 (next-open-brace (c-pull-open-brace paren-state)))
8085 (while (and open-brace
8086 (save-excursion
8087 (goto-char open-brace)
8088 (not (c-looking-at-decl-block next-open-brace nil))))
8089 (setq open-brace next-open-brace
8090 next-open-brace (c-pull-open-brace paren-state)))
8091 open-brace))
8093 (defun c-inside-bracelist-p (containing-sexp paren-state)
8094 ;; return the buffer position of the beginning of the brace list
8095 ;; statement if we're inside a brace list, otherwise return nil.
8096 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
8097 ;; paren. PAREN-STATE is the remainder of the state of enclosing
8098 ;; braces
8100 ;; N.B.: This algorithm can potentially get confused by cpp macros
8101 ;; placed in inconvenient locations. It's a trade-off we make for
8102 ;; speed.
8104 ;; This function might do hidden buffer changes.
8106 ;; This will pick up brace list declarations.
8107 (c-safe
8108 (save-excursion
8109 (goto-char containing-sexp)
8110 (c-forward-sexp -1)
8111 (let (bracepos)
8112 (if (and (or (looking-at c-brace-list-key)
8113 (progn (c-forward-sexp -1)
8114 (looking-at c-brace-list-key)))
8115 (setq bracepos (c-down-list-forward (point)))
8116 (not (c-crosses-statement-barrier-p (point)
8117 (- bracepos 2))))
8118 (point)))))
8119 ;; this will pick up array/aggregate init lists, even if they are nested.
8120 (save-excursion
8121 (let ((class-key
8122 ;; Pike can have class definitions anywhere, so we must
8123 ;; check for the class key here.
8124 (and (c-major-mode-is 'pike-mode)
8125 c-decl-block-key))
8126 bufpos braceassignp lim next-containing)
8127 (while (and (not bufpos)
8128 containing-sexp)
8129 (when paren-state
8130 (if (consp (car paren-state))
8131 (setq lim (cdr (car paren-state))
8132 paren-state (cdr paren-state))
8133 (setq lim (car paren-state)))
8134 (when paren-state
8135 (setq next-containing (car paren-state)
8136 paren-state (cdr paren-state))))
8137 (goto-char containing-sexp)
8138 (if (c-looking-at-inexpr-block next-containing next-containing)
8139 ;; We're in an in-expression block of some kind. Do not
8140 ;; check nesting. We deliberately set the limit to the
8141 ;; containing sexp, so that c-looking-at-inexpr-block
8142 ;; doesn't check for an identifier before it.
8143 (setq containing-sexp nil)
8144 ;; see if the open brace is preceded by = or [...] in
8145 ;; this statement, but watch out for operator=
8146 (setq braceassignp 'dontknow)
8147 (c-backward-token-2 1 t lim)
8148 ;; Checks to do only on the first sexp before the brace.
8149 (when (and c-opt-inexpr-brace-list-key
8150 (eq (char-after) ?\[))
8151 ;; In Java, an initialization brace list may follow
8152 ;; directly after "new Foo[]", so check for a "new"
8153 ;; earlier.
8154 (while (eq braceassignp 'dontknow)
8155 (setq braceassignp
8156 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8157 ((looking-at c-opt-inexpr-brace-list-key) t)
8158 ((looking-at "\\sw\\|\\s_\\|[.[]")
8159 ;; Carry on looking if this is an
8160 ;; identifier (may contain "." in Java)
8161 ;; or another "[]" sexp.
8162 'dontknow)
8163 (t nil)))))
8164 ;; Checks to do on all sexps before the brace, up to the
8165 ;; beginning of the statement.
8166 (while (eq braceassignp 'dontknow)
8167 (cond ((eq (char-after) ?\;)
8168 (setq braceassignp nil))
8169 ((and class-key
8170 (looking-at class-key))
8171 (setq braceassignp nil))
8172 ((eq (char-after) ?=)
8173 ;; We've seen a =, but must check earlier tokens so
8174 ;; that it isn't something that should be ignored.
8175 (setq braceassignp 'maybe)
8176 (while (and (eq braceassignp 'maybe)
8177 (zerop (c-backward-token-2 1 t lim)))
8178 (setq braceassignp
8179 (cond
8180 ;; Check for operator =
8181 ((and c-opt-op-identifier-prefix
8182 (looking-at c-opt-op-identifier-prefix))
8183 nil)
8184 ;; Check for `<opchar>= in Pike.
8185 ((and (c-major-mode-is 'pike-mode)
8186 (or (eq (char-after) ?`)
8187 ;; Special case for Pikes
8188 ;; `[]=, since '[' is not in
8189 ;; the punctuation class.
8190 (and (eq (char-after) ?\[)
8191 (eq (char-before) ?`))))
8192 nil)
8193 ((looking-at "\\s.") 'maybe)
8194 ;; make sure we're not in a C++ template
8195 ;; argument assignment
8196 ((and
8197 (c-major-mode-is 'c++-mode)
8198 (save-excursion
8199 (let ((here (point))
8200 (pos< (progn
8201 (skip-chars-backward "^<>")
8202 (point))))
8203 (and (eq (char-before) ?<)
8204 (not (c-crosses-statement-barrier-p
8205 pos< here))
8206 (not (c-in-literal))
8207 ))))
8208 nil)
8209 (t t))))))
8210 (if (and (eq braceassignp 'dontknow)
8211 (/= (c-backward-token-2 1 t lim) 0))
8212 (setq braceassignp nil)))
8213 (if (not braceassignp)
8214 (if (eq (char-after) ?\;)
8215 ;; Brace lists can't contain a semicolon, so we're done.
8216 (setq containing-sexp nil)
8217 ;; Go up one level.
8218 (setq containing-sexp next-containing
8219 lim nil
8220 next-containing nil))
8221 ;; we've hit the beginning of the aggregate list
8222 (c-beginning-of-statement-1
8223 (c-most-enclosing-brace paren-state))
8224 (setq bufpos (point))))
8226 bufpos))
8229 (defun c-looking-at-special-brace-list (&optional lim)
8230 ;; If we're looking at the start of a pike-style list, ie `({ })',
8231 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
8232 ;; positions and its entry in c-special-brace-lists is returned, nil
8233 ;; otherwise. The ending position is nil if the list is still open.
8234 ;; LIM is the limit for forward search. The point may either be at
8235 ;; the `(' or at the following paren character. Tries to check the
8236 ;; matching closer, but assumes it's correct if no balanced paren is
8237 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8238 ;; a special brace list).
8240 ;; This function might do hidden buffer changes.
8241 (if c-special-brace-lists
8242 (condition-case ()
8243 (save-excursion
8244 (let ((beg (point))
8245 inner-beg end type)
8246 (c-forward-syntactic-ws)
8247 (if (eq (char-after) ?\()
8248 (progn
8249 (forward-char 1)
8250 (c-forward-syntactic-ws)
8251 (setq inner-beg (point))
8252 (setq type (assq (char-after) c-special-brace-lists)))
8253 (if (setq type (assq (char-after) c-special-brace-lists))
8254 (progn
8255 (setq inner-beg (point))
8256 (c-backward-syntactic-ws)
8257 (forward-char -1)
8258 (setq beg (if (eq (char-after) ?\()
8259 (point)
8260 nil)))))
8261 (if (and beg type)
8262 (if (and (c-safe
8263 (goto-char beg)
8264 (c-forward-sexp 1)
8265 (setq end (point))
8266 (= (char-before) ?\)))
8267 (c-safe
8268 (goto-char inner-beg)
8269 (if (looking-at "\\s(")
8270 ;; Check balancing of the inner paren
8271 ;; below.
8272 (progn
8273 (c-forward-sexp 1)
8275 ;; If the inner char isn't a paren then
8276 ;; we can't check balancing, so just
8277 ;; check the char before the outer
8278 ;; closing paren.
8279 (goto-char end)
8280 (backward-char)
8281 (c-backward-syntactic-ws)
8282 (= (char-before) (cdr type)))))
8283 (if (or (/= (char-syntax (char-before)) ?\))
8284 (= (progn
8285 (c-forward-syntactic-ws)
8286 (point))
8287 (1- end)))
8288 (cons (cons beg end) type))
8289 (cons (list beg) type)))))
8290 (error nil))))
8292 (defun c-looking-at-bos (&optional lim)
8293 ;; Return non-nil if between two statements or declarations, assuming
8294 ;; point is not inside a literal or comment.
8296 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8297 ;; are recommended instead.
8299 ;; This function might do hidden buffer changes.
8300 (c-at-statement-start-p))
8301 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8303 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8304 ;; Return non-nil if we're looking at the beginning of a block
8305 ;; inside an expression. The value returned is actually a cons of
8306 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8307 ;; position of the beginning of the construct.
8309 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8310 ;; position of the closest containing list. If it's nil, the
8311 ;; containing paren isn't used to decide whether we're inside an
8312 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8313 ;; needs to be farther back.
8315 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8316 ;; brace block might be done. It should only be used when the
8317 ;; construct can be assumed to be complete, i.e. when the original
8318 ;; starting position was further down than that.
8320 ;; This function might do hidden buffer changes.
8322 (save-excursion
8323 (let ((res 'maybe) passed-paren
8324 (closest-lim (or containing-sexp lim (point-min)))
8325 ;; Look at the character after point only as a last resort
8326 ;; when we can't disambiguate.
8327 (block-follows (and (eq (char-after) ?{) (point))))
8329 (while (and (eq res 'maybe)
8330 (progn (c-backward-syntactic-ws)
8331 (> (point) closest-lim))
8332 (not (bobp))
8333 (progn (backward-char)
8334 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8335 (c-safe (forward-char)
8336 (goto-char (scan-sexps (point) -1))))
8338 (setq res
8339 (if (looking-at c-keywords-regexp)
8340 (let ((kw-sym (c-keyword-sym (match-string 1))))
8341 (cond
8342 ((and block-follows
8343 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8344 (and (not (eq passed-paren ?\[))
8345 (or (not (looking-at c-class-key))
8346 ;; If the class definition is at the start of
8347 ;; a statement, we don't consider it an
8348 ;; in-expression class.
8349 (let ((prev (point)))
8350 (while (and
8351 (= (c-backward-token-2 1 nil closest-lim) 0)
8352 (eq (char-syntax (char-after)) ?w))
8353 (setq prev (point)))
8354 (goto-char prev)
8355 (not (c-at-statement-start-p)))
8356 ;; Also, in Pike we treat it as an
8357 ;; in-expression class if it's used in an
8358 ;; object clone expression.
8359 (save-excursion
8360 (and check-at-end
8361 (c-major-mode-is 'pike-mode)
8362 (progn (goto-char block-follows)
8363 (zerop (c-forward-token-2 1 t)))
8364 (eq (char-after) ?\())))
8365 (cons 'inexpr-class (point))))
8366 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8367 (when (not passed-paren)
8368 (cons 'inexpr-statement (point))))
8369 ((c-keyword-member kw-sym 'c-lambda-kwds)
8370 (when (or (not passed-paren)
8371 (eq passed-paren ?\())
8372 (cons 'inlambda (point))))
8373 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8374 nil)
8376 'maybe)))
8378 (if (looking-at "\\s(")
8379 (if passed-paren
8380 (if (and (eq passed-paren ?\[)
8381 (eq (char-after) ?\[))
8382 ;; Accept several square bracket sexps for
8383 ;; Java array initializations.
8384 'maybe)
8385 (setq passed-paren (char-after))
8386 'maybe)
8387 'maybe))))
8389 (if (eq res 'maybe)
8390 (when (and c-recognize-paren-inexpr-blocks
8391 block-follows
8392 containing-sexp
8393 (eq (char-after containing-sexp) ?\())
8394 (goto-char containing-sexp)
8395 (if (or (save-excursion
8396 (c-backward-syntactic-ws lim)
8397 (and (> (point) (or lim (point-min)))
8398 (c-on-identifier)))
8399 (and c-special-brace-lists
8400 (c-looking-at-special-brace-list)))
8402 (cons 'inexpr-statement (point))))
8404 res))))
8406 (defun c-looking-at-inexpr-block-backward (paren-state)
8407 ;; Returns non-nil if we're looking at the end of an in-expression
8408 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8409 ;; PAREN-STATE is the paren state relevant at the current position.
8411 ;; This function might do hidden buffer changes.
8412 (save-excursion
8413 ;; We currently only recognize a block.
8414 (let ((here (point))
8415 (elem (car-safe paren-state))
8416 containing-sexp)
8417 (when (and (consp elem)
8418 (progn (goto-char (cdr elem))
8419 (c-forward-syntactic-ws here)
8420 (= (point) here)))
8421 (goto-char (car elem))
8422 (if (setq paren-state (cdr paren-state))
8423 (setq containing-sexp (car-safe paren-state)))
8424 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8425 paren-state)
8426 containing-sexp)))))
8428 (defun c-at-macro-vsemi-p (&optional pos)
8429 ;; Is there a "virtual semicolon" at POS or point?
8430 ;; (See cc-defs.el for full details of "virtual semicolons".)
8432 ;; This is true when point is at the last non syntactic WS position on the
8433 ;; line, there is a macro call last on the line, and this particular macro's
8434 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
8435 ;; semicolon.
8436 (save-excursion
8437 (save-restriction
8438 (widen)
8439 (if pos
8440 (goto-char pos)
8441 (setq pos (point)))
8442 (and
8443 c-macro-with-semi-re
8444 (not (c-in-literal))
8445 (eq (skip-chars-backward " \t") 0)
8447 ;; Check we've got nothing after this except comments and empty lines
8448 ;; joined by escaped EOLs.
8449 (skip-chars-forward " \t") ; always returns non-nil.
8450 (progn
8451 (while ; go over 1 block comment per iteration.
8452 (and
8453 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
8454 (goto-char (match-end 0))
8455 (cond
8456 ((looking-at c-block-comment-start-regexp)
8457 (and (forward-comment 1)
8458 (skip-chars-forward " \t"))) ; always returns non-nil
8459 ((looking-at c-line-comment-start-regexp)
8460 (end-of-line)
8461 nil)
8462 (t nil))))
8463 (eolp))
8465 (goto-char pos)
8466 (progn (c-backward-syntactic-ws)
8467 (eq (point) pos))
8469 ;; Check for one of the listed macros being before point.
8470 (or (not (eq (char-before) ?\)))
8471 (when (c-go-list-backward)
8472 (c-backward-syntactic-ws)
8474 (c-simple-skip-symbol-backward)
8475 (looking-at c-macro-with-semi-re)))))
8477 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
8480 ;; `c-guess-basic-syntax' and the functions that precedes it below
8481 ;; implements the main decision tree for determining the syntactic
8482 ;; analysis of the current line of code.
8484 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8485 ;; auto newline analysis.
8486 (defvar c-auto-newline-analysis nil)
8488 (defun c-brace-anchor-point (bracepos)
8489 ;; BRACEPOS is the position of a brace in a construct like "namespace
8490 ;; Bar {". Return the anchor point in this construct; this is the
8491 ;; earliest symbol on the brace's line which isn't earlier than
8492 ;; "namespace".
8494 ;; Currently (2007-08-17), "like namespace" means "matches
8495 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8496 ;; or anything like that.
8497 (save-excursion
8498 (let ((boi (c-point 'boi bracepos)))
8499 (goto-char bracepos)
8500 (while (and (> (point) boi)
8501 (not (looking-at c-other-decl-block-key)))
8502 (c-backward-token-2))
8503 (if (> (point) boi) (point) boi))))
8505 (defsubst c-add-syntax (symbol &rest args)
8506 ;; A simple function to prepend a new syntax element to
8507 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8508 ;; should always be dynamically bound but since we read it first
8509 ;; we'll fail properly anyway if this function is misused.
8510 (setq c-syntactic-context (cons (cons symbol args)
8511 c-syntactic-context)))
8513 (defsubst c-append-syntax (symbol &rest args)
8514 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8515 ;; (Normally not necessary.)
8516 (setq c-syntactic-context (nconc c-syntactic-context
8517 (list (cons symbol args)))))
8519 (defun c-add-stmt-syntax (syntax-symbol
8520 syntax-extra-args
8521 stop-at-boi-only
8522 containing-sexp
8523 paren-state)
8524 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8525 ;; needed with further syntax elements of the types `substatement',
8526 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8527 ;; `defun-block-intro'.
8529 ;; Do the generic processing to anchor the given syntax symbol on
8530 ;; the preceding statement: Skip over any labels and containing
8531 ;; statements on the same line, and then search backward until we
8532 ;; find a statement or block start that begins at boi without a
8533 ;; label or comment.
8535 ;; Point is assumed to be at the prospective anchor point for the
8536 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8537 ;; skip past open parens and containing statements. Most of the added
8538 ;; syntax elements will get the same anchor point - the exception is
8539 ;; for an anchor in a construct like "namespace"[*] - this is as early
8540 ;; as possible in the construct but on the same line as the {.
8542 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8544 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8545 ;; syntax symbol. They are appended after the anchor point.
8547 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8548 ;; if the current statement starts there.
8550 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8551 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8553 ;; This function might do hidden buffer changes.
8555 (if (= (point) (c-point 'boi))
8556 ;; This is by far the most common case, so let's give it special
8557 ;; treatment.
8558 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
8560 (let ((syntax-last c-syntactic-context)
8561 (boi (c-point 'boi))
8562 ;; Set when we're on a label, so that we don't stop there.
8563 ;; FIXME: To be complete we should check if we're on a label
8564 ;; now at the start.
8565 on-label)
8567 ;; Use point as the anchor point for "namespace", "extern", etc.
8568 (apply 'c-add-syntax syntax-symbol
8569 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
8570 (point) nil)
8571 syntax-extra-args)
8573 ;; Loop while we have to back out of containing blocks.
8574 (while
8575 (and
8576 (catch 'back-up-block
8578 ;; Loop while we have to back up statements.
8579 (while (or (/= (point) boi)
8580 on-label
8581 (looking-at c-comment-start-regexp))
8583 ;; Skip past any comments that stands between the
8584 ;; statement start and boi.
8585 (let ((savepos (point)))
8586 (while (and (/= savepos boi)
8587 (c-backward-single-comment))
8588 (setq savepos (point)
8589 boi (c-point 'boi)))
8590 (goto-char savepos))
8592 ;; Skip to the beginning of this statement or backward
8593 ;; another one.
8594 (let ((old-pos (point))
8595 (old-boi boi)
8596 (step-type (c-beginning-of-statement-1 containing-sexp)))
8597 (setq boi (c-point 'boi)
8598 on-label (eq step-type 'label))
8600 (cond ((= (point) old-pos)
8601 ;; If we didn't move we're at the start of a block and
8602 ;; have to continue outside it.
8603 (throw 'back-up-block t))
8605 ((and (eq step-type 'up)
8606 (>= (point) old-boi)
8607 (looking-at "else\\>[^_]")
8608 (save-excursion
8609 (goto-char old-pos)
8610 (looking-at "if\\>[^_]")))
8611 ;; Special case to avoid deeper and deeper indentation
8612 ;; of "else if" clauses.
8615 ((and (not stop-at-boi-only)
8616 (/= old-pos old-boi)
8617 (memq step-type '(up previous)))
8618 ;; If stop-at-boi-only is nil, we shouldn't back up
8619 ;; over previous or containing statements to try to
8620 ;; reach boi, so go back to the last position and
8621 ;; exit.
8622 (goto-char old-pos)
8623 (throw 'back-up-block nil))
8626 (if (and (not stop-at-boi-only)
8627 (memq step-type '(up previous beginning)))
8628 ;; If we've moved into another statement then we
8629 ;; should no longer try to stop in the middle of a
8630 ;; line.
8631 (setq stop-at-boi-only t))
8633 ;; Record this as a substatement if we skipped up one
8634 ;; level.
8635 (when (eq step-type 'up)
8636 (c-add-syntax 'substatement nil))))
8639 containing-sexp)
8641 ;; Now we have to go out of this block.
8642 (goto-char containing-sexp)
8644 ;; Don't stop in the middle of a special brace list opener
8645 ;; like "({".
8646 (when c-special-brace-lists
8647 (let ((special-list (c-looking-at-special-brace-list)))
8648 (when (and special-list
8649 (< (car (car special-list)) (point)))
8650 (setq containing-sexp (car (car special-list)))
8651 (goto-char containing-sexp))))
8653 (setq paren-state (c-whack-state-after containing-sexp paren-state)
8654 containing-sexp (c-most-enclosing-brace paren-state)
8655 boi (c-point 'boi))
8657 ;; Analyze the construct in front of the block we've stepped out
8658 ;; from and add the right syntactic element for it.
8659 (let ((paren-pos (point))
8660 (paren-char (char-after))
8661 step-type)
8663 (if (eq paren-char ?\()
8664 ;; Stepped out of a parenthesis block, so we're in an
8665 ;; expression now.
8666 (progn
8667 (when (/= paren-pos boi)
8668 (if (and c-recognize-paren-inexpr-blocks
8669 (progn
8670 (c-backward-syntactic-ws containing-sexp)
8671 (or (not (looking-at "\\>"))
8672 (not (c-on-identifier))))
8673 (save-excursion
8674 (goto-char (1+ paren-pos))
8675 (c-forward-syntactic-ws)
8676 (eq (char-after) ?{)))
8677 ;; Stepped out of an in-expression statement. This
8678 ;; syntactic element won't get an anchor pos.
8679 (c-add-syntax 'inexpr-statement)
8681 ;; A parenthesis normally belongs to an arglist.
8682 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
8684 (goto-char (max boi
8685 (if containing-sexp
8686 (1+ containing-sexp)
8687 (point-min))))
8688 (setq step-type 'same
8689 on-label nil))
8691 ;; Stepped out of a brace block.
8692 (setq step-type (c-beginning-of-statement-1 containing-sexp)
8693 on-label (eq step-type 'label))
8695 (if (and (eq step-type 'same)
8696 (/= paren-pos (point)))
8697 (let (inexpr)
8698 (cond
8699 ((save-excursion
8700 (goto-char paren-pos)
8701 (setq inexpr (c-looking-at-inexpr-block
8702 (c-safe-position containing-sexp paren-state)
8703 containing-sexp)))
8704 (c-add-syntax (if (eq (car inexpr) 'inlambda)
8705 'defun-block-intro
8706 'statement-block-intro)
8707 nil))
8708 ((looking-at c-other-decl-block-key)
8709 (c-add-syntax
8710 (cdr (assoc (match-string 1)
8711 c-other-decl-block-key-in-symbols-alist))
8712 (max (c-point 'boi paren-pos) (point))))
8713 (t (c-add-syntax 'defun-block-intro nil))))
8715 (c-add-syntax 'statement-block-intro nil)))
8717 (if (= paren-pos boi)
8718 ;; Always done if the open brace was at boi. The
8719 ;; c-beginning-of-statement-1 call above is necessary
8720 ;; anyway, to decide the type of block-intro to add.
8721 (goto-char paren-pos)
8722 (setq boi (c-point 'boi)))
8725 ;; Fill in the current point as the anchor for all the symbols
8726 ;; added above.
8727 (let ((p c-syntactic-context) q)
8728 (while (not (eq p syntax-last))
8729 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8730 (while q
8731 (unless (car q)
8732 (setcar q (point)))
8733 (setq q (cdr q)))
8734 (setq p (cdr p))))
8737 (defun c-add-class-syntax (symbol
8738 containing-decl-open
8739 containing-decl-start
8740 containing-decl-kwd
8741 paren-state)
8742 ;; The inclass and class-close syntactic symbols are added in
8743 ;; several places and some work is needed to fix everything.
8744 ;; Therefore it's collected here.
8746 ;; This function might do hidden buffer changes.
8747 (goto-char containing-decl-open)
8748 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
8749 (progn
8750 (c-add-syntax symbol containing-decl-open)
8751 containing-decl-open)
8752 (goto-char containing-decl-start)
8753 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8754 ;; here, but we have to do like this for compatibility.
8755 (back-to-indentation)
8756 (c-add-syntax symbol (point))
8757 (if (and (c-keyword-member containing-decl-kwd
8758 'c-inexpr-class-kwds)
8759 (/= containing-decl-start (c-point 'boi containing-decl-start)))
8760 (c-add-syntax 'inexpr-class))
8761 (point)))
8763 (defun c-guess-continued-construct (indent-point
8764 char-after-ip
8765 beg-of-same-or-containing-stmt
8766 containing-sexp
8767 paren-state)
8768 ;; This function contains the decision tree reached through both
8769 ;; cases 18 and 10. It's a continued statement or top level
8770 ;; construct of some kind.
8772 ;; This function might do hidden buffer changes.
8774 (let (special-brace-list placeholder)
8775 (goto-char indent-point)
8776 (skip-chars-forward " \t")
8778 (cond
8779 ;; (CASE A removed.)
8780 ;; CASE B: open braces for class or brace-lists
8781 ((setq special-brace-list
8782 (or (and c-special-brace-lists
8783 (c-looking-at-special-brace-list))
8784 (eq char-after-ip ?{)))
8786 (cond
8787 ;; CASE B.1: class-open
8788 ((save-excursion
8789 (and (eq (char-after) ?{)
8790 (c-looking-at-decl-block containing-sexp t)
8791 (setq beg-of-same-or-containing-stmt (point))))
8792 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
8794 ;; CASE B.2: brace-list-open
8795 ((or (consp special-brace-list)
8796 (save-excursion
8797 (goto-char beg-of-same-or-containing-stmt)
8798 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8799 indent-point t t t)))
8800 ;; The most semantically accurate symbol here is
8801 ;; brace-list-open, but we normally report it simply as a
8802 ;; statement-cont. The reason is that one normally adjusts
8803 ;; brace-list-open for brace lists as top-level constructs,
8804 ;; and brace lists inside statements is a completely different
8805 ;; context. C.f. case 5A.3.
8806 (c-beginning-of-statement-1 containing-sexp)
8807 (c-add-stmt-syntax (if c-auto-newline-analysis
8808 ;; Turn off the dwim above when we're
8809 ;; analyzing the nature of the brace
8810 ;; for the auto newline feature.
8811 'brace-list-open
8812 'statement-cont)
8813 nil nil
8814 containing-sexp paren-state))
8816 ;; CASE B.3: The body of a function declared inside a normal
8817 ;; block. Can occur e.g. in Pike and when using gcc
8818 ;; extensions, but watch out for macros followed by blocks.
8819 ;; C.f. cases E, 16F and 17G.
8820 ((and (not (c-at-statement-start-p))
8821 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8822 'same)
8823 (save-excursion
8824 (let ((c-recognize-typeless-decls nil))
8825 ;; Turn off recognition of constructs that lacks a
8826 ;; type in this case, since that's more likely to be
8827 ;; a macro followed by a block.
8828 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8829 (c-add-stmt-syntax 'defun-open nil t
8830 containing-sexp paren-state))
8832 ;; CASE B.4: Continued statement with block open. The most
8833 ;; accurate analysis is perhaps `statement-cont' together with
8834 ;; `block-open' but we play DWIM and use `substatement-open'
8835 ;; instead. The rationaly is that this typically is a macro
8836 ;; followed by a block which makes it very similar to a
8837 ;; statement with a substatement block.
8839 (c-add-stmt-syntax 'substatement-open nil nil
8840 containing-sexp paren-state))
8843 ;; CASE C: iostream insertion or extraction operator
8844 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8845 (save-excursion
8846 (goto-char beg-of-same-or-containing-stmt)
8847 ;; If there is no preceding streamop in the statement
8848 ;; then indent this line as a normal statement-cont.
8849 (when (c-syntactic-re-search-forward
8850 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
8851 (c-add-syntax 'stream-op (c-point 'boi))
8852 t))))
8854 ;; CASE E: In the "K&R region" of a function declared inside a
8855 ;; normal block. C.f. case B.3.
8856 ((and (save-excursion
8857 ;; Check that the next token is a '{'. This works as
8858 ;; long as no language that allows nested function
8859 ;; definitions allows stuff like member init lists, K&R
8860 ;; declarations or throws clauses there.
8862 ;; Note that we do a forward search for something ahead
8863 ;; of the indentation line here. That's not good since
8864 ;; the user might not have typed it yet. Unfortunately
8865 ;; it's exceedingly tricky to recognize a function
8866 ;; prototype in a code block without resorting to this.
8867 (c-forward-syntactic-ws)
8868 (eq (char-after) ?{))
8869 (not (c-at-statement-start-p))
8870 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8871 'same)
8872 (save-excursion
8873 (let ((c-recognize-typeless-decls nil))
8874 ;; Turn off recognition of constructs that lacks a
8875 ;; type in this case, since that's more likely to be
8876 ;; a macro followed by a block.
8877 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8878 (c-add-stmt-syntax 'func-decl-cont nil t
8879 containing-sexp paren-state))
8881 ;;CASE F: continued statement and the only preceding items are
8882 ;;annotations.
8883 ((and (c-major-mode-is 'java-mode)
8884 (setq placeholder (point))
8885 (c-beginning-of-statement-1)
8886 (progn
8887 (while (and (c-forward-annotation)
8888 (< (point) placeholder))
8889 (c-forward-syntactic-ws))
8891 (prog1
8892 (>= (point) placeholder)
8893 (goto-char placeholder)))
8894 (c-beginning-of-statement-1 containing-sexp)
8895 (c-add-syntax 'annotation-var-cont (point)))
8897 ;; CASE G: a template list continuation?
8898 ;; Mostly a duplication of case 5D.3 to fix templates-19:
8899 ((and (c-major-mode-is 'c++-mode)
8900 (save-excursion
8901 (goto-char indent-point)
8902 (c-with-syntax-table c++-template-syntax-table
8903 (setq placeholder (c-up-list-backward)))
8904 (and placeholder
8905 (eq (char-after placeholder) ?<)
8906 (/= (char-before placeholder) ?<)
8907 (progn
8908 (goto-char (1+ placeholder))
8909 (not (looking-at c-<-op-cont-regexp))))))
8910 (c-with-syntax-table c++-template-syntax-table
8911 (goto-char placeholder)
8912 (c-beginning-of-statement-1 containing-sexp t)
8913 (if (save-excursion
8914 (c-backward-syntactic-ws containing-sexp)
8915 (eq (char-before) ?<))
8916 ;; In a nested template arglist.
8917 (progn
8918 (goto-char placeholder)
8919 (c-syntactic-skip-backward "^,;" containing-sexp t)
8920 (c-forward-syntactic-ws))
8921 (back-to-indentation)))
8922 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8923 ;; template aware.
8924 (c-add-syntax 'template-args-cont (point) placeholder))
8926 ;; CASE D: continued statement.
8928 (c-beginning-of-statement-1 containing-sexp)
8929 (c-add-stmt-syntax 'statement-cont nil nil
8930 containing-sexp paren-state))
8933 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
8934 ;; 2005/11/29).
8935 ;;;###autoload
8936 (defun c-guess-basic-syntax ()
8937 "Return the syntactic context of the current line."
8938 (save-excursion
8939 (beginning-of-line)
8940 (c-save-buffer-state
8941 ((indent-point (point))
8942 (case-fold-search nil)
8943 ;; A whole ugly bunch of various temporary variables. Have
8944 ;; to declare them here since it's not possible to declare
8945 ;; a variable with only the scope of a cond test and the
8946 ;; following result clauses, and most of this function is a
8947 ;; single gigantic cond. :P
8948 literal char-before-ip before-ws-ip char-after-ip macro-start
8949 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
8950 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
8951 containing-<
8952 ;; The following record some positions for the containing
8953 ;; declaration block if we're directly within one:
8954 ;; `containing-decl-open' is the position of the open
8955 ;; brace. `containing-decl-start' is the start of the
8956 ;; declaration. `containing-decl-kwd' is the keyword
8957 ;; symbol of the keyword that tells what kind of block it
8958 ;; is.
8959 containing-decl-open
8960 containing-decl-start
8961 containing-decl-kwd
8962 ;; The open paren of the closest surrounding sexp or nil if
8963 ;; there is none.
8964 containing-sexp
8965 ;; The position after the closest preceding brace sexp
8966 ;; (nested sexps are ignored), or the position after
8967 ;; `containing-sexp' if there is none, or (point-min) if
8968 ;; `containing-sexp' is nil.
8970 ;; The paren state outside `containing-sexp', or at
8971 ;; `indent-point' if `containing-sexp' is nil.
8972 (paren-state (c-parse-state))
8973 ;; There's always at most one syntactic element which got
8974 ;; an anchor pos. It's stored in syntactic-relpos.
8975 syntactic-relpos
8976 (c-stmt-delim-chars c-stmt-delim-chars))
8978 ;; Check if we're directly inside an enclosing declaration
8979 ;; level block.
8980 (when (and (setq containing-sexp
8981 (c-most-enclosing-brace paren-state))
8982 (progn
8983 (goto-char containing-sexp)
8984 (eq (char-after) ?{))
8985 (setq placeholder
8986 (c-looking-at-decl-block
8987 (c-most-enclosing-brace paren-state
8988 containing-sexp)
8989 t)))
8990 (setq containing-decl-open containing-sexp
8991 containing-decl-start (point)
8992 containing-sexp nil)
8993 (goto-char placeholder)
8994 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
8995 (c-keyword-sym (match-string 1)))))
8997 ;; Init some position variables.
8998 (if c-state-cache
8999 (progn
9000 (setq containing-sexp (car paren-state)
9001 paren-state (cdr paren-state))
9002 (if (consp containing-sexp)
9003 (progn
9004 (setq lim (cdr containing-sexp))
9005 (if (cdr c-state-cache)
9006 ;; Ignore balanced paren. The next entry
9007 ;; can't be another one.
9008 (setq containing-sexp (car (cdr c-state-cache))
9009 paren-state (cdr paren-state))
9010 ;; If there is no surrounding open paren then
9011 ;; put the last balanced pair back on paren-state.
9012 (setq paren-state (cons containing-sexp paren-state)
9013 containing-sexp nil)))
9014 (setq lim (1+ containing-sexp))))
9015 (setq lim (point-min)))
9017 ;; If we're in a parenthesis list then ',' delimits the
9018 ;; "statements" rather than being an operator (with the
9019 ;; exception of the "for" clause). This difference is
9020 ;; typically only noticeable when statements are used in macro
9021 ;; arglists.
9022 (when (and containing-sexp
9023 (eq (char-after containing-sexp) ?\())
9024 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
9025 ;; cache char before and after indent point, and move point to
9026 ;; the most likely position to perform the majority of tests
9027 (goto-char indent-point)
9028 (c-backward-syntactic-ws lim)
9029 (setq before-ws-ip (point)
9030 char-before-ip (char-before))
9031 (goto-char indent-point)
9032 (skip-chars-forward " \t")
9033 (setq char-after-ip (char-after))
9035 ;; are we in a literal?
9036 (setq literal (c-in-literal lim))
9038 ;; now figure out syntactic qualities of the current line
9039 (cond
9041 ;; CASE 1: in a string.
9042 ((eq literal 'string)
9043 (c-add-syntax 'string (c-point 'bopl)))
9045 ;; CASE 2: in a C or C++ style comment.
9046 ((and (memq literal '(c c++))
9047 ;; This is a kludge for XEmacs where we use
9048 ;; `buffer-syntactic-context', which doesn't correctly
9049 ;; recognize "\*/" to end a block comment.
9050 ;; `parse-partial-sexp' which is used by
9051 ;; `c-literal-limits' will however do that in most
9052 ;; versions, which results in that we get nil from
9053 ;; `c-literal-limits' even when `c-in-literal' claims
9054 ;; we're inside a comment.
9055 (setq placeholder (c-literal-limits lim)))
9056 (c-add-syntax literal (car placeholder)))
9058 ;; CASE 3: in a cpp preprocessor macro continuation.
9059 ((and (save-excursion
9060 (when (c-beginning-of-macro)
9061 (setq macro-start (point))))
9062 (/= macro-start (c-point 'boi))
9063 (progn
9064 (setq tmpsymbol 'cpp-macro-cont)
9065 (or (not c-syntactic-indentation-in-macros)
9066 (save-excursion
9067 (goto-char macro-start)
9068 ;; If at the beginning of the body of a #define
9069 ;; directive then analyze as cpp-define-intro
9070 ;; only. Go on with the syntactic analysis
9071 ;; otherwise. in-macro-expr is set if we're in a
9072 ;; cpp expression, i.e. before the #define body
9073 ;; or anywhere in a non-#define directive.
9074 (if (c-forward-to-cpp-define-body)
9075 (let ((indent-boi (c-point 'boi indent-point)))
9076 (setq in-macro-expr (> (point) indent-boi)
9077 tmpsymbol 'cpp-define-intro)
9078 (= (point) indent-boi))
9079 (setq in-macro-expr t)
9080 nil)))))
9081 (c-add-syntax tmpsymbol macro-start)
9082 (setq macro-start nil))
9084 ;; CASE 11: an else clause?
9085 ((looking-at "else\\>[^_]")
9086 (c-beginning-of-statement-1 containing-sexp)
9087 (c-add-stmt-syntax 'else-clause nil t
9088 containing-sexp paren-state))
9090 ;; CASE 12: while closure of a do/while construct?
9091 ((and (looking-at "while\\>[^_]")
9092 (save-excursion
9093 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
9094 'beginning)
9095 (setq placeholder (point)))))
9096 (goto-char placeholder)
9097 (c-add-stmt-syntax 'do-while-closure nil t
9098 containing-sexp paren-state))
9100 ;; CASE 13: A catch or finally clause? This case is simpler
9101 ;; than if-else and do-while, because a block is required
9102 ;; after every try, catch and finally.
9103 ((save-excursion
9104 (and (cond ((c-major-mode-is 'c++-mode)
9105 (looking-at "catch\\>[^_]"))
9106 ((c-major-mode-is 'java-mode)
9107 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
9108 (and (c-safe (c-backward-syntactic-ws)
9109 (c-backward-sexp)
9111 (eq (char-after) ?{)
9112 (c-safe (c-backward-syntactic-ws)
9113 (c-backward-sexp)
9115 (if (eq (char-after) ?\()
9116 (c-safe (c-backward-sexp) t)
9118 (looking-at "\\(try\\|catch\\)\\>[^_]")
9119 (setq placeholder (point))))
9120 (goto-char placeholder)
9121 (c-add-stmt-syntax 'catch-clause nil t
9122 containing-sexp paren-state))
9124 ;; CASE 18: A substatement we can recognize by keyword.
9125 ((save-excursion
9126 (and c-opt-block-stmt-key
9127 (not (eq char-before-ip ?\;))
9128 (not (c-at-vsemi-p before-ws-ip))
9129 (not (memq char-after-ip '(?\) ?\] ?,)))
9130 (or (not (eq char-before-ip ?}))
9131 (c-looking-at-inexpr-block-backward c-state-cache))
9132 (> (point)
9133 (progn
9134 ;; Ought to cache the result from the
9135 ;; c-beginning-of-statement-1 calls here.
9136 (setq placeholder (point))
9137 (while (eq (setq step-type
9138 (c-beginning-of-statement-1 lim))
9139 'label))
9140 (if (eq step-type 'previous)
9141 (goto-char placeholder)
9142 (setq placeholder (point))
9143 (if (and (eq step-type 'same)
9144 (not (looking-at c-opt-block-stmt-key)))
9145 ;; Step up to the containing statement if we
9146 ;; stayed in the same one.
9147 (let (step)
9148 (while (eq
9149 (setq step
9150 (c-beginning-of-statement-1 lim))
9151 'label))
9152 (if (eq step 'up)
9153 (setq placeholder (point))
9154 ;; There was no containing statement afterall.
9155 (goto-char placeholder)))))
9156 placeholder))
9157 (if (looking-at c-block-stmt-2-key)
9158 ;; Require a parenthesis after these keywords.
9159 ;; Necessary to catch e.g. synchronized in Java,
9160 ;; which can be used both as statement and
9161 ;; modifier.
9162 (and (zerop (c-forward-token-2 1 nil))
9163 (eq (char-after) ?\())
9164 (looking-at c-opt-block-stmt-key))))
9166 (if (eq step-type 'up)
9167 ;; CASE 18A: Simple substatement.
9168 (progn
9169 (goto-char placeholder)
9170 (cond
9171 ((eq char-after-ip ?{)
9172 (c-add-stmt-syntax 'substatement-open nil nil
9173 containing-sexp paren-state))
9174 ((save-excursion
9175 (goto-char indent-point)
9176 (back-to-indentation)
9177 (c-forward-label))
9178 (c-add-stmt-syntax 'substatement-label nil nil
9179 containing-sexp paren-state))
9181 (c-add-stmt-syntax 'substatement nil nil
9182 containing-sexp paren-state))))
9184 ;; CASE 18B: Some other substatement. This is shared
9185 ;; with case 10.
9186 (c-guess-continued-construct indent-point
9187 char-after-ip
9188 placeholder
9190 paren-state)))
9192 ;; CASE 14: A case or default label
9193 ((looking-at c-label-kwds-regexp)
9194 (if containing-sexp
9195 (progn
9196 (goto-char containing-sexp)
9197 (setq lim (c-most-enclosing-brace c-state-cache
9198 containing-sexp))
9199 (c-backward-to-block-anchor lim)
9200 (c-add-stmt-syntax 'case-label nil t lim paren-state))
9201 ;; Got a bogus label at the top level. In lack of better
9202 ;; alternatives, anchor it on (point-min).
9203 (c-add-syntax 'case-label (point-min))))
9205 ;; CASE 15: any other label
9206 ((save-excursion
9207 (back-to-indentation)
9208 (and (not (looking-at c-syntactic-ws-start))
9209 (c-forward-label)))
9210 (cond (containing-decl-open
9211 (setq placeholder (c-add-class-syntax 'inclass
9212 containing-decl-open
9213 containing-decl-start
9214 containing-decl-kwd
9215 paren-state))
9216 ;; Append access-label with the same anchor point as
9217 ;; inclass gets.
9218 (c-append-syntax 'access-label placeholder))
9220 (containing-sexp
9221 (goto-char containing-sexp)
9222 (setq lim (c-most-enclosing-brace c-state-cache
9223 containing-sexp))
9224 (save-excursion
9225 (setq tmpsymbol
9226 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
9227 (looking-at "switch\\>[^_]"))
9228 ;; If the surrounding statement is a switch then
9229 ;; let's analyze all labels as switch labels, so
9230 ;; that they get lined up consistently.
9231 'case-label
9232 'label)))
9233 (c-backward-to-block-anchor lim)
9234 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
9237 ;; A label on the top level. Treat it as a class
9238 ;; context. (point-min) is the closest we get to the
9239 ;; class open brace.
9240 (c-add-syntax 'access-label (point-min)))))
9242 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9243 ;; 17E.
9244 ((setq placeholder (c-looking-at-inexpr-block
9245 (c-safe-position containing-sexp paren-state)
9246 containing-sexp
9247 ;; Have to turn on the heuristics after
9248 ;; the point even though it doesn't work
9249 ;; very well. C.f. test case class-16.pike.
9251 (setq tmpsymbol (assq (car placeholder)
9252 '((inexpr-class . class-open)
9253 (inexpr-statement . block-open))))
9254 (if tmpsymbol
9255 ;; It's a statement block or an anonymous class.
9256 (setq tmpsymbol (cdr tmpsymbol))
9257 ;; It's a Pike lambda. Check whether we are between the
9258 ;; lambda keyword and the argument list or at the defun
9259 ;; opener.
9260 (setq tmpsymbol (if (eq char-after-ip ?{)
9261 'inline-open
9262 'lambda-intro-cont)))
9263 (goto-char (cdr placeholder))
9264 (back-to-indentation)
9265 (c-add-stmt-syntax tmpsymbol nil t
9266 (c-most-enclosing-brace c-state-cache (point))
9267 paren-state)
9268 (unless (eq (point) (cdr placeholder))
9269 (c-add-syntax (car placeholder))))
9271 ;; CASE 5: Line is inside a declaration level block or at top level.
9272 ((or containing-decl-open (null containing-sexp))
9273 (cond
9275 ;; CASE 5A: we are looking at a defun, brace list, class,
9276 ;; or inline-inclass method opening brace
9277 ((setq special-brace-list
9278 (or (and c-special-brace-lists
9279 (c-looking-at-special-brace-list))
9280 (eq char-after-ip ?{)))
9281 (cond
9283 ;; CASE 5A.1: Non-class declaration block open.
9284 ((save-excursion
9285 (let (tmp)
9286 (and (eq char-after-ip ?{)
9287 (setq tmp (c-looking-at-decl-block containing-sexp t))
9288 (progn
9289 (setq placeholder (point))
9290 (goto-char tmp)
9291 (looking-at c-symbol-key))
9292 (c-keyword-member
9293 (c-keyword-sym (setq keyword (match-string 0)))
9294 'c-other-block-decl-kwds))))
9295 (goto-char placeholder)
9296 (c-add-stmt-syntax
9297 (if (string-equal keyword "extern")
9298 ;; Special case for extern-lang-open.
9299 'extern-lang-open
9300 (intern (concat keyword "-open")))
9301 nil t containing-sexp paren-state))
9303 ;; CASE 5A.2: we are looking at a class opening brace
9304 ((save-excursion
9305 (goto-char indent-point)
9306 (skip-chars-forward " \t")
9307 (and (eq (char-after) ?{)
9308 (c-looking-at-decl-block containing-sexp t)
9309 (setq placeholder (point))))
9310 (c-add-syntax 'class-open placeholder))
9312 ;; CASE 5A.3: brace list open
9313 ((save-excursion
9314 (c-beginning-of-decl-1 lim)
9315 (while (looking-at c-specifier-key)
9316 (goto-char (match-end 1))
9317 (c-forward-syntactic-ws indent-point))
9318 (setq placeholder (c-point 'boi))
9319 (or (consp special-brace-list)
9320 (and (or (save-excursion
9321 (goto-char indent-point)
9322 (setq tmpsymbol nil)
9323 (while (and (> (point) placeholder)
9324 (zerop (c-backward-token-2 1 t))
9325 (/= (char-after) ?=))
9326 (and c-opt-inexpr-brace-list-key
9327 (not tmpsymbol)
9328 (looking-at c-opt-inexpr-brace-list-key)
9329 (setq tmpsymbol 'topmost-intro-cont)))
9330 (eq (char-after) ?=))
9331 (looking-at c-brace-list-key))
9332 (save-excursion
9333 (while (and (< (point) indent-point)
9334 (zerop (c-forward-token-2 1 t))
9335 (not (memq (char-after) '(?\; ?\()))))
9336 (not (memq (char-after) '(?\; ?\()))
9337 ))))
9338 (if (and (not c-auto-newline-analysis)
9339 (c-major-mode-is 'java-mode)
9340 (eq tmpsymbol 'topmost-intro-cont))
9341 ;; We're in Java and have found that the open brace
9342 ;; belongs to a "new Foo[]" initialization list,
9343 ;; which means the brace list is part of an
9344 ;; expression and not a top level definition. We
9345 ;; therefore treat it as any topmost continuation
9346 ;; even though the semantically correct symbol still
9347 ;; is brace-list-open, on the same grounds as in
9348 ;; case B.2.
9349 (progn
9350 (c-beginning-of-statement-1 lim)
9351 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9352 (c-add-syntax 'brace-list-open placeholder)))
9354 ;; CASE 5A.4: inline defun open
9355 ((and containing-decl-open
9356 (not (c-keyword-member containing-decl-kwd
9357 'c-other-block-decl-kwds)))
9358 (c-add-syntax 'inline-open)
9359 (c-add-class-syntax 'inclass
9360 containing-decl-open
9361 containing-decl-start
9362 containing-decl-kwd
9363 paren-state))
9365 ;; CASE 5A.5: ordinary defun open
9367 (save-excursion
9368 (c-beginning-of-decl-1 lim)
9369 (while (looking-at c-specifier-key)
9370 (goto-char (match-end 1))
9371 (c-forward-syntactic-ws indent-point))
9372 (c-add-syntax 'defun-open (c-point 'boi))
9373 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9374 ;; 2007-11-09)
9375 ))))
9377 ;; CASE 5B: After a function header but before the body (or
9378 ;; the ending semicolon if there's no body).
9379 ((save-excursion
9380 (when (setq placeholder (c-just-after-func-arglist-p lim))
9381 (setq tmp-pos (point))))
9382 (cond
9384 ;; CASE 5B.1: Member init list.
9385 ((eq (char-after tmp-pos) ?:)
9386 (if (or (> tmp-pos indent-point)
9387 (= (c-point 'bosws) (1+ tmp-pos)))
9388 (progn
9389 ;; There is no preceding member init clause.
9390 ;; Indent relative to the beginning of indentation
9391 ;; for the topmost-intro line that contains the
9392 ;; prototype's open paren.
9393 (goto-char placeholder)
9394 (c-add-syntax 'member-init-intro (c-point 'boi)))
9395 ;; Indent relative to the first member init clause.
9396 (goto-char (1+ tmp-pos))
9397 (c-forward-syntactic-ws)
9398 (c-add-syntax 'member-init-cont (point))))
9400 ;; CASE 5B.2: K&R arg decl intro
9401 ((and c-recognize-knr-p
9402 (c-in-knr-argdecl lim))
9403 (c-beginning-of-statement-1 lim)
9404 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9405 (if containing-decl-open
9406 (c-add-class-syntax 'inclass
9407 containing-decl-open
9408 containing-decl-start
9409 containing-decl-kwd
9410 paren-state)))
9412 ;; CASE 5B.4: Nether region after a C++ or Java func
9413 ;; decl, which could include a `throws' declaration.
9415 (c-beginning-of-statement-1 lim)
9416 (c-add-syntax 'func-decl-cont (c-point 'boi))
9419 ;; CASE 5C: inheritance line. could be first inheritance
9420 ;; line, or continuation of a multiple inheritance
9421 ((or (and (c-major-mode-is 'c++-mode)
9422 (progn
9423 (when (eq char-after-ip ?,)
9424 (skip-chars-forward " \t")
9425 (forward-char))
9426 (looking-at c-opt-postfix-decl-spec-key)))
9427 (and (or (eq char-before-ip ?:)
9428 ;; watch out for scope operator
9429 (save-excursion
9430 (and (eq char-after-ip ?:)
9431 (c-safe (forward-char 1) t)
9432 (not (eq (char-after) ?:))
9434 (save-excursion
9435 (c-backward-syntactic-ws lim)
9436 (if (eq char-before-ip ?:)
9437 (progn
9438 (forward-char -1)
9439 (c-backward-syntactic-ws lim)))
9440 (back-to-indentation)
9441 (looking-at c-class-key)))
9442 ;; for Java
9443 (and (c-major-mode-is 'java-mode)
9444 (let ((fence (save-excursion
9445 (c-beginning-of-statement-1 lim)
9446 (point)))
9447 cont done)
9448 (save-excursion
9449 (while (not done)
9450 (cond ((looking-at c-opt-postfix-decl-spec-key)
9451 (setq injava-inher (cons cont (point))
9452 done t))
9453 ((or (not (c-safe (c-forward-sexp -1) t))
9454 (<= (point) fence))
9455 (setq done t))
9457 (setq cont t)))
9458 injava-inher)
9459 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9460 (point)))
9462 (cond
9464 ;; CASE 5C.1: non-hanging colon on an inher intro
9465 ((eq char-after-ip ?:)
9466 (c-beginning-of-statement-1 lim)
9467 (c-add-syntax 'inher-intro (c-point 'boi))
9468 ;; don't add inclass symbol since relative point already
9469 ;; contains any class offset
9472 ;; CASE 5C.2: hanging colon on an inher intro
9473 ((eq char-before-ip ?:)
9474 (c-beginning-of-statement-1 lim)
9475 (c-add-syntax 'inher-intro (c-point 'boi))
9476 (if containing-decl-open
9477 (c-add-class-syntax 'inclass
9478 containing-decl-open
9479 containing-decl-start
9480 containing-decl-kwd
9481 paren-state)))
9483 ;; CASE 5C.3: in a Java implements/extends
9484 (injava-inher
9485 (let ((where (cdr injava-inher))
9486 (cont (car injava-inher)))
9487 (goto-char where)
9488 (cond ((looking-at "throws\\>[^_]")
9489 (c-add-syntax 'func-decl-cont
9490 (progn (c-beginning-of-statement-1 lim)
9491 (c-point 'boi))))
9492 (cont (c-add-syntax 'inher-cont where))
9493 (t (c-add-syntax 'inher-intro
9494 (progn (goto-char (cdr injava-inher))
9495 (c-beginning-of-statement-1 lim)
9496 (point))))
9499 ;; CASE 5C.4: a continued inheritance line
9501 (c-beginning-of-inheritance-list lim)
9502 (c-add-syntax 'inher-cont (point))
9503 ;; don't add inclass symbol since relative point already
9504 ;; contains any class offset
9507 ;; CASE 5D: this could be a top-level initialization, a
9508 ;; member init list continuation, or a template argument
9509 ;; list continuation.
9510 ((save-excursion
9511 ;; Note: We use the fact that lim is always after any
9512 ;; preceding brace sexp.
9513 (if c-recognize-<>-arglists
9514 (while (and
9515 (progn
9516 (c-syntactic-skip-backward "^;,=<>" lim t)
9517 (> (point) lim))
9519 (when c-overloadable-operators-regexp
9520 (when (setq placeholder (c-after-special-operator-id lim))
9521 (goto-char placeholder)
9523 (cond
9524 ((eq (char-before) ?>)
9525 (or (c-backward-<>-arglist nil lim)
9526 (backward-char))
9528 ((eq (char-before) ?<)
9529 (backward-char)
9530 (if (save-excursion
9531 (c-forward-<>-arglist nil))
9532 (progn (forward-char)
9533 nil)
9535 (t nil)))))
9536 ;; NB: No c-after-special-operator-id stuff in this
9537 ;; clause - we assume only C++ needs it.
9538 (c-syntactic-skip-backward "^;,=" lim t))
9539 (memq (char-before) '(?, ?= ?<)))
9540 (cond
9542 ;; CASE 5D.3: perhaps a template list continuation?
9543 ((and (c-major-mode-is 'c++-mode)
9544 (save-excursion
9545 (save-restriction
9546 (c-with-syntax-table c++-template-syntax-table
9547 (goto-char indent-point)
9548 (setq placeholder (c-up-list-backward))
9549 (and placeholder
9550 (eq (char-after placeholder) ?<))))))
9551 (c-with-syntax-table c++-template-syntax-table
9552 (goto-char placeholder)
9553 (c-beginning-of-statement-1 lim t)
9554 (if (save-excursion
9555 (c-backward-syntactic-ws lim)
9556 (eq (char-before) ?<))
9557 ;; In a nested template arglist.
9558 (progn
9559 (goto-char placeholder)
9560 (c-syntactic-skip-backward "^,;" lim t)
9561 (c-forward-syntactic-ws))
9562 (back-to-indentation)))
9563 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9564 ;; template aware.
9565 (c-add-syntax 'template-args-cont (point) placeholder))
9567 ;; CASE 5D.4: perhaps a multiple inheritance line?
9568 ((and (c-major-mode-is 'c++-mode)
9569 (save-excursion
9570 (c-beginning-of-statement-1 lim)
9571 (setq placeholder (point))
9572 (if (looking-at "static\\>[^_]")
9573 (c-forward-token-2 1 nil indent-point))
9574 (and (looking-at c-class-key)
9575 (zerop (c-forward-token-2 2 nil indent-point))
9576 (if (eq (char-after) ?<)
9577 (c-with-syntax-table c++-template-syntax-table
9578 (zerop (c-forward-token-2 1 t indent-point)))
9580 (eq (char-after) ?:))))
9581 (goto-char placeholder)
9582 (c-add-syntax 'inher-cont (c-point 'boi)))
9584 ;; CASE 5D.5: Continuation of the "expression part" of a
9585 ;; top level construct. Or, perhaps, an unrecognised construct.
9587 (while (and (setq placeholder (point))
9588 (eq (car (c-beginning-of-decl-1 containing-sexp))
9589 'same)
9590 (save-excursion
9591 (c-backward-syntactic-ws)
9592 (eq (char-before) ?}))
9593 (< (point) placeholder)))
9594 (c-add-stmt-syntax
9595 (cond
9596 ((eq (point) placeholder) 'statement) ; unrecognised construct
9597 ;; A preceding comma at the top level means that a
9598 ;; new variable declaration starts here. Use
9599 ;; topmost-intro-cont for it, for consistency with
9600 ;; the first variable declaration. C.f. case 5N.
9601 ((eq char-before-ip ?,) 'topmost-intro-cont)
9602 (t 'statement-cont))
9603 nil nil containing-sexp paren-state))
9606 ;; CASE 5F: Close of a non-class declaration level block.
9607 ((and (eq char-after-ip ?})
9608 (c-keyword-member containing-decl-kwd
9609 'c-other-block-decl-kwds))
9610 ;; This is inconsistent: Should use `containing-decl-open'
9611 ;; here if it's at boi, like in case 5J.
9612 (goto-char containing-decl-start)
9613 (c-add-stmt-syntax
9614 (if (string-equal (symbol-name containing-decl-kwd) "extern")
9615 ;; Special case for compatibility with the
9616 ;; extern-lang syntactic symbols.
9617 'extern-lang-close
9618 (intern (concat (symbol-name containing-decl-kwd)
9619 "-close")))
9620 nil t
9621 (c-most-enclosing-brace paren-state (point))
9622 paren-state))
9624 ;; CASE 5G: we are looking at the brace which closes the
9625 ;; enclosing nested class decl
9626 ((and containing-sexp
9627 (eq char-after-ip ?})
9628 (eq containing-decl-open containing-sexp))
9629 (c-add-class-syntax 'class-close
9630 containing-decl-open
9631 containing-decl-start
9632 containing-decl-kwd
9633 paren-state))
9635 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9636 ((and c-recognize-knr-p
9637 (not containing-sexp) ; can't be knr inside braces.
9638 (not (eq char-before-ip ?}))
9639 (save-excursion
9640 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
9641 (and placeholder
9642 ;; Do an extra check to avoid tripping up on
9643 ;; statements that occur in invalid contexts
9644 ;; (e.g. in macro bodies where we don't really
9645 ;; know the context of what we're looking at).
9646 (not (and c-opt-block-stmt-key
9647 (looking-at c-opt-block-stmt-key)))))
9648 (< placeholder indent-point))
9649 (goto-char placeholder)
9650 (c-add-syntax 'knr-argdecl (point)))
9652 ;; CASE 5I: ObjC method definition.
9653 ((and c-opt-method-key
9654 (looking-at c-opt-method-key))
9655 (c-beginning-of-statement-1 nil t)
9656 (if (= (point) indent-point)
9657 ;; Handle the case when it's the first (non-comment)
9658 ;; thing in the buffer. Can't look for a 'same return
9659 ;; value from cbos1 since ObjC directives currently
9660 ;; aren't recognized fully, so that we get 'same
9661 ;; instead of 'previous if it moved over a preceding
9662 ;; directive.
9663 (goto-char (point-min)))
9664 (c-add-syntax 'objc-method-intro (c-point 'boi)))
9666 ;; CASE 5P: AWK pattern or function or continuation
9667 ;; thereof.
9668 ((c-major-mode-is 'awk-mode)
9669 (setq placeholder (point))
9670 (c-add-stmt-syntax
9671 (if (and (eq (c-beginning-of-statement-1) 'same)
9672 (/= (point) placeholder))
9673 'topmost-intro-cont
9674 'topmost-intro)
9675 nil nil
9676 containing-sexp paren-state))
9678 ;; CASE 5N: At a variable declaration that follows a class
9679 ;; definition or some other block declaration that doesn't
9680 ;; end at the closing '}'. C.f. case 5D.5.
9681 ((progn
9682 (c-backward-syntactic-ws lim)
9683 (and (eq (char-before) ?})
9684 (save-excursion
9685 (let ((start (point)))
9686 (if (and c-state-cache
9687 (consp (car c-state-cache))
9688 (eq (cdar c-state-cache) (point)))
9689 ;; Speed up the backward search a bit.
9690 (goto-char (caar c-state-cache)))
9691 (c-beginning-of-decl-1 containing-sexp)
9692 (setq placeholder (point))
9693 (if (= start (point))
9694 ;; The '}' is unbalanced.
9696 (c-end-of-decl-1)
9697 (>= (point) indent-point))))))
9698 (goto-char placeholder)
9699 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9700 containing-sexp paren-state))
9702 ;; NOTE: The point is at the end of the previous token here.
9704 ;; CASE 5J: we are at the topmost level, make
9705 ;; sure we skip back past any access specifiers
9706 ((and
9707 ;; A macro continuation line is never at top level.
9708 (not (and macro-start
9709 (> indent-point macro-start)))
9710 (save-excursion
9711 (setq placeholder (point))
9712 (or (memq char-before-ip '(?\; ?{ ?} nil))
9713 (c-at-vsemi-p before-ws-ip)
9714 (when (and (eq char-before-ip ?:)
9715 (eq (c-beginning-of-statement-1 lim)
9716 'label))
9717 (c-backward-syntactic-ws lim)
9718 (setq placeholder (point)))
9719 (and (c-major-mode-is 'objc-mode)
9720 (catch 'not-in-directive
9721 (c-beginning-of-statement-1 lim)
9722 (setq placeholder (point))
9723 (while (and (c-forward-objc-directive)
9724 (< (point) indent-point))
9725 (c-forward-syntactic-ws)
9726 (if (>= (point) indent-point)
9727 (throw 'not-in-directive t))
9728 (setq placeholder (point)))
9729 nil)))))
9730 ;; For historic reasons we anchor at bol of the last
9731 ;; line of the previous declaration. That's clearly
9732 ;; highly bogus and useless, and it makes our lives hard
9733 ;; to remain compatible. :P
9734 (goto-char placeholder)
9735 (c-add-syntax 'topmost-intro (c-point 'bol))
9736 (if containing-decl-open
9737 (if (c-keyword-member containing-decl-kwd
9738 'c-other-block-decl-kwds)
9739 (progn
9740 (goto-char (c-brace-anchor-point containing-decl-open))
9741 (c-add-stmt-syntax
9742 (if (string-equal (symbol-name containing-decl-kwd)
9743 "extern")
9744 ;; Special case for compatibility with the
9745 ;; extern-lang syntactic symbols.
9746 'inextern-lang
9747 (intern (concat "in"
9748 (symbol-name containing-decl-kwd))))
9749 nil t
9750 (c-most-enclosing-brace paren-state (point))
9751 paren-state))
9752 (c-add-class-syntax 'inclass
9753 containing-decl-open
9754 containing-decl-start
9755 containing-decl-kwd
9756 paren-state)))
9757 (when (and c-syntactic-indentation-in-macros
9758 macro-start
9759 (/= macro-start (c-point 'boi indent-point)))
9760 (c-add-syntax 'cpp-define-intro)
9761 (setq macro-start nil)))
9763 ;; CASE 5K: we are at an ObjC method definition
9764 ;; continuation line.
9765 ((and c-opt-method-key
9766 (save-excursion
9767 (c-beginning-of-statement-1 lim)
9768 (beginning-of-line)
9769 (when (looking-at c-opt-method-key)
9770 (setq placeholder (point)))))
9771 (c-add-syntax 'objc-method-args-cont placeholder))
9773 ;; CASE 5L: we are at the first argument of a template
9774 ;; arglist that begins on the previous line.
9775 ((and c-recognize-<>-arglists
9776 (eq (char-before) ?<)
9777 (not (and c-overloadable-operators-regexp
9778 (c-after-special-operator-id lim))))
9779 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9780 (c-add-syntax 'template-args-cont (c-point 'boi)))
9782 ;; CASE 5Q: we are at a statement within a macro.
9783 (macro-start
9784 (c-beginning-of-statement-1 containing-sexp)
9785 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
9787 ;;CASE 5N: We are at a tompmost continuation line and the only
9788 ;;preceding items are annotations.
9789 ((and (c-major-mode-is 'java-mode)
9790 (setq placeholder (point))
9791 (c-beginning-of-statement-1)
9792 (progn
9793 (while (and (c-forward-annotation))
9794 (c-forward-syntactic-ws))
9796 (prog1
9797 (>= (point) placeholder)
9798 (goto-char placeholder)))
9799 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
9801 ;; CASE 5M: we are at a topmost continuation line
9803 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9804 (when (c-major-mode-is 'objc-mode)
9805 (setq placeholder (point))
9806 (while (and (c-forward-objc-directive)
9807 (< (point) indent-point))
9808 (c-forward-syntactic-ws)
9809 (setq placeholder (point)))
9810 (goto-char placeholder))
9811 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9815 ;; (CASE 6 has been removed.)
9817 ;; CASE 7: line is an expression, not a statement. Most
9818 ;; likely we are either in a function prototype or a function
9819 ;; call argument list
9820 ((not (or (and c-special-brace-lists
9821 (save-excursion
9822 (goto-char containing-sexp)
9823 (c-looking-at-special-brace-list)))
9824 (eq (char-after containing-sexp) ?{)))
9825 (cond
9827 ;; CASE 7A: we are looking at the arglist closing paren.
9828 ;; C.f. case 7F.
9829 ((memq char-after-ip '(?\) ?\]))
9830 (goto-char containing-sexp)
9831 (setq placeholder (c-point 'boi))
9832 (if (and (c-safe (backward-up-list 1) t)
9833 (>= (point) placeholder))
9834 (progn
9835 (forward-char)
9836 (skip-chars-forward " \t"))
9837 (goto-char placeholder))
9838 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
9839 (c-most-enclosing-brace paren-state (point))
9840 paren-state))
9842 ;; CASE 7B: Looking at the opening brace of an
9843 ;; in-expression block or brace list. C.f. cases 4, 16A
9844 ;; and 17E.
9845 ((and (eq char-after-ip ?{)
9846 (progn
9847 (setq placeholder (c-inside-bracelist-p (point)
9848 paren-state))
9849 (if placeholder
9850 (setq tmpsymbol '(brace-list-open . inexpr-class))
9851 (setq tmpsymbol '(block-open . inexpr-statement)
9852 placeholder
9853 (cdr-safe (c-looking-at-inexpr-block
9854 (c-safe-position containing-sexp
9855 paren-state)
9856 containing-sexp)))
9857 ;; placeholder is nil if it's a block directly in
9858 ;; a function arglist. That makes us skip out of
9859 ;; this case.
9861 (goto-char placeholder)
9862 (back-to-indentation)
9863 (c-add-stmt-syntax (car tmpsymbol) nil t
9864 (c-most-enclosing-brace paren-state (point))
9865 paren-state)
9866 (if (/= (point) placeholder)
9867 (c-add-syntax (cdr tmpsymbol))))
9869 ;; CASE 7C: we are looking at the first argument in an empty
9870 ;; argument list. Use arglist-close if we're actually
9871 ;; looking at a close paren or bracket.
9872 ((memq char-before-ip '(?\( ?\[))
9873 (goto-char containing-sexp)
9874 (setq placeholder (c-point 'boi))
9875 (if (and (c-safe (backward-up-list 1) t)
9876 (>= (point) placeholder))
9877 (progn
9878 (forward-char)
9879 (skip-chars-forward " \t"))
9880 (goto-char placeholder))
9881 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
9882 (c-most-enclosing-brace paren-state (point))
9883 paren-state))
9885 ;; CASE 7D: we are inside a conditional test clause. treat
9886 ;; these things as statements
9887 ((progn
9888 (goto-char containing-sexp)
9889 (and (c-safe (c-forward-sexp -1) t)
9890 (looking-at "\\<for\\>[^_]")))
9891 (goto-char (1+ containing-sexp))
9892 (c-forward-syntactic-ws indent-point)
9893 (if (eq char-before-ip ?\;)
9894 (c-add-syntax 'statement (point))
9895 (c-add-syntax 'statement-cont (point))
9898 ;; CASE 7E: maybe a continued ObjC method call. This is the
9899 ;; case when we are inside a [] bracketed exp, and what
9900 ;; precede the opening bracket is not an identifier.
9901 ((and c-opt-method-key
9902 (eq (char-after containing-sexp) ?\[)
9903 (progn
9904 (goto-char (1- containing-sexp))
9905 (c-backward-syntactic-ws (c-point 'bod))
9906 (if (not (looking-at c-symbol-key))
9907 (c-add-syntax 'objc-method-call-cont containing-sexp))
9910 ;; CASE 7F: we are looking at an arglist continuation line,
9911 ;; but the preceding argument is on the same line as the
9912 ;; opening paren. This case includes multi-line
9913 ;; mathematical paren groupings, but we could be on a
9914 ;; for-list continuation line. C.f. case 7A.
9915 ((progn
9916 (goto-char (1+ containing-sexp))
9917 (< (save-excursion
9918 (c-forward-syntactic-ws)
9919 (point))
9920 (c-point 'bonl)))
9921 (goto-char containing-sexp) ; paren opening the arglist
9922 (setq placeholder (c-point 'boi))
9923 (if (and (c-safe (backward-up-list 1) t)
9924 (>= (point) placeholder))
9925 (progn
9926 (forward-char)
9927 (skip-chars-forward " \t"))
9928 (goto-char placeholder))
9929 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
9930 (c-most-enclosing-brace c-state-cache (point))
9931 paren-state))
9933 ;; CASE 7G: we are looking at just a normal arglist
9934 ;; continuation line
9935 (t (c-forward-syntactic-ws indent-point)
9936 (c-add-syntax 'arglist-cont (c-point 'boi)))
9939 ;; CASE 8: func-local multi-inheritance line
9940 ((and (c-major-mode-is 'c++-mode)
9941 (save-excursion
9942 (goto-char indent-point)
9943 (skip-chars-forward " \t")
9944 (looking-at c-opt-postfix-decl-spec-key)))
9945 (goto-char indent-point)
9946 (skip-chars-forward " \t")
9947 (cond
9949 ;; CASE 8A: non-hanging colon on an inher intro
9950 ((eq char-after-ip ?:)
9951 (c-backward-syntactic-ws lim)
9952 (c-add-syntax 'inher-intro (c-point 'boi)))
9954 ;; CASE 8B: hanging colon on an inher intro
9955 ((eq char-before-ip ?:)
9956 (c-add-syntax 'inher-intro (c-point 'boi)))
9958 ;; CASE 8C: a continued inheritance line
9960 (c-beginning-of-inheritance-list lim)
9961 (c-add-syntax 'inher-cont (point))
9964 ;; CASE 9: we are inside a brace-list
9965 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
9966 (setq special-brace-list
9967 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
9968 (save-excursion
9969 (goto-char containing-sexp)
9970 (c-looking-at-special-brace-list)))
9971 (c-inside-bracelist-p containing-sexp paren-state))))
9972 (cond
9974 ;; CASE 9A: In the middle of a special brace list opener.
9975 ((and (consp special-brace-list)
9976 (save-excursion
9977 (goto-char containing-sexp)
9978 (eq (char-after) ?\())
9979 (eq char-after-ip (car (cdr special-brace-list))))
9980 (goto-char (car (car special-brace-list)))
9981 (skip-chars-backward " \t")
9982 (if (and (bolp)
9983 (assoc 'statement-cont
9984 (setq placeholder (c-guess-basic-syntax))))
9985 (setq c-syntactic-context placeholder)
9986 (c-beginning-of-statement-1
9987 (c-safe-position (1- containing-sexp) paren-state))
9988 (c-forward-token-2 0)
9989 (while (looking-at c-specifier-key)
9990 (goto-char (match-end 1))
9991 (c-forward-syntactic-ws))
9992 (c-add-syntax 'brace-list-open (c-point 'boi))))
9994 ;; CASE 9B: brace-list-close brace
9995 ((if (consp special-brace-list)
9996 ;; Check special brace list closer.
9997 (progn
9998 (goto-char (car (car special-brace-list)))
9999 (save-excursion
10000 (goto-char indent-point)
10001 (back-to-indentation)
10003 ;; We were between the special close char and the `)'.
10004 (and (eq (char-after) ?\))
10005 (eq (1+ (point)) (cdr (car special-brace-list))))
10006 ;; We were before the special close char.
10007 (and (eq (char-after) (cdr (cdr special-brace-list)))
10008 (zerop (c-forward-token-2))
10009 (eq (1+ (point)) (cdr (car special-brace-list)))))))
10010 ;; Normal brace list check.
10011 (and (eq char-after-ip ?})
10012 (c-safe (goto-char (c-up-list-backward (point))) t)
10013 (= (point) containing-sexp)))
10014 (if (eq (point) (c-point 'boi))
10015 (c-add-syntax 'brace-list-close (point))
10016 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10017 (c-beginning-of-statement-1 lim)
10018 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
10021 ;; Prepare for the rest of the cases below by going to the
10022 ;; token following the opening brace
10023 (if (consp special-brace-list)
10024 (progn
10025 (goto-char (car (car special-brace-list)))
10026 (c-forward-token-2 1 nil indent-point))
10027 (goto-char containing-sexp))
10028 (forward-char)
10029 (let ((start (point)))
10030 (c-forward-syntactic-ws indent-point)
10031 (goto-char (max start (c-point 'bol))))
10032 (c-skip-ws-forward indent-point)
10033 (cond
10035 ;; CASE 9C: we're looking at the first line in a brace-list
10036 ((= (point) indent-point)
10037 (if (consp special-brace-list)
10038 (goto-char (car (car special-brace-list)))
10039 (goto-char containing-sexp))
10040 (if (eq (point) (c-point 'boi))
10041 (c-add-syntax 'brace-list-intro (point))
10042 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10043 (c-beginning-of-statement-1 lim)
10044 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
10046 ;; CASE 9D: this is just a later brace-list-entry or
10047 ;; brace-entry-open
10048 (t (if (or (eq char-after-ip ?{)
10049 (and c-special-brace-lists
10050 (save-excursion
10051 (goto-char indent-point)
10052 (c-forward-syntactic-ws (c-point 'eol))
10053 (c-looking-at-special-brace-list (point)))))
10054 (c-add-syntax 'brace-entry-open (point))
10055 (c-add-syntax 'brace-list-entry (point))
10057 ))))
10059 ;; CASE 10: A continued statement or top level construct.
10060 ((and (not (memq char-before-ip '(?\; ?:)))
10061 (not (c-at-vsemi-p before-ws-ip))
10062 (or (not (eq char-before-ip ?}))
10063 (c-looking-at-inexpr-block-backward c-state-cache))
10064 (> (point)
10065 (save-excursion
10066 (c-beginning-of-statement-1 containing-sexp)
10067 (setq placeholder (point))))
10068 (/= placeholder containing-sexp))
10069 ;; This is shared with case 18.
10070 (c-guess-continued-construct indent-point
10071 char-after-ip
10072 placeholder
10073 containing-sexp
10074 paren-state))
10076 ;; CASE 16: block close brace, possibly closing the defun or
10077 ;; the class
10078 ((eq char-after-ip ?})
10079 ;; From here on we have the next containing sexp in lim.
10080 (setq lim (c-most-enclosing-brace paren-state))
10081 (goto-char containing-sexp)
10082 (cond
10084 ;; CASE 16E: Closing a statement block? This catches
10085 ;; cases where it's preceded by a statement keyword,
10086 ;; which works even when used in an "invalid" context,
10087 ;; e.g. a macro argument.
10088 ((c-after-conditional)
10089 (c-backward-to-block-anchor lim)
10090 (c-add-stmt-syntax 'block-close nil t lim paren-state))
10092 ;; CASE 16A: closing a lambda defun or an in-expression
10093 ;; block? C.f. cases 4, 7B and 17E.
10094 ((setq placeholder (c-looking-at-inexpr-block
10095 (c-safe-position containing-sexp paren-state)
10096 nil))
10097 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10098 'inline-close
10099 'block-close))
10100 (goto-char containing-sexp)
10101 (back-to-indentation)
10102 (if (= containing-sexp (point))
10103 (c-add-syntax tmpsymbol (point))
10104 (goto-char (cdr placeholder))
10105 (back-to-indentation)
10106 (c-add-stmt-syntax tmpsymbol nil t
10107 (c-most-enclosing-brace paren-state (point))
10108 paren-state)
10109 (if (/= (point) (cdr placeholder))
10110 (c-add-syntax (car placeholder)))))
10112 ;; CASE 16B: does this close an inline or a function in
10113 ;; a non-class declaration level block?
10114 ((save-excursion
10115 (and lim
10116 (progn
10117 (goto-char lim)
10118 (c-looking-at-decl-block
10119 (c-most-enclosing-brace paren-state lim)
10120 nil))
10121 (setq placeholder (point))))
10122 (c-backward-to-decl-anchor lim)
10123 (back-to-indentation)
10124 (if (save-excursion
10125 (goto-char placeholder)
10126 (looking-at c-other-decl-block-key))
10127 (c-add-syntax 'defun-close (point))
10128 (c-add-syntax 'inline-close (point))))
10130 ;; CASE 16F: Can be a defun-close of a function declared
10131 ;; in a statement block, e.g. in Pike or when using gcc
10132 ;; extensions, but watch out for macros followed by
10133 ;; blocks. Let it through to be handled below.
10134 ;; C.f. cases B.3 and 17G.
10135 ((save-excursion
10136 (and (not (c-at-statement-start-p))
10137 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10138 (setq placeholder (point))
10139 (let ((c-recognize-typeless-decls nil))
10140 ;; Turn off recognition of constructs that
10141 ;; lacks a type in this case, since that's more
10142 ;; likely to be a macro followed by a block.
10143 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10144 (back-to-indentation)
10145 (if (/= (point) containing-sexp)
10146 (goto-char placeholder))
10147 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
10149 ;; CASE 16C: If there is an enclosing brace then this is
10150 ;; a block close since defun closes inside declaration
10151 ;; level blocks have been handled above.
10152 (lim
10153 ;; If the block is preceded by a case/switch label on
10154 ;; the same line, we anchor at the first preceding label
10155 ;; at boi. The default handling in c-add-stmt-syntax
10156 ;; really fixes it better, but we do like this to keep
10157 ;; the indentation compatible with version 5.28 and
10158 ;; earlier. C.f. case 17H.
10159 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10160 (eq (c-beginning-of-statement-1 lim) 'label)))
10161 (goto-char placeholder)
10162 (if (looking-at c-label-kwds-regexp)
10163 (c-add-syntax 'block-close (point))
10164 (goto-char containing-sexp)
10165 ;; c-backward-to-block-anchor not necessary here; those
10166 ;; situations are handled in case 16E above.
10167 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
10169 ;; CASE 16D: Only top level defun close left.
10171 (goto-char containing-sexp)
10172 (c-backward-to-decl-anchor lim)
10173 (c-add-stmt-syntax 'defun-close nil nil
10174 (c-most-enclosing-brace paren-state)
10175 paren-state))
10178 ;; CASE 19: line is an expression, not a statement, and is directly
10179 ;; contained by a template delimiter. Most likely, we are in a
10180 ;; template arglist within a statement. This case is based on CASE
10181 ;; 7. At some point in the future, we may wish to create more
10182 ;; syntactic symbols such as `template-intro',
10183 ;; `template-cont-nonempty', etc., and distinguish between them as we
10184 ;; do for `arglist-intro' etc. (2009-12-07).
10185 ((and c-recognize-<>-arglists
10186 (setq containing-< (c-up-list-backward indent-point containing-sexp))
10187 (eq (char-after containing-<) ?\<))
10188 (setq placeholder (c-point 'boi containing-<))
10189 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
10190 ; '<') before indent-point.
10191 (if (>= (point) placeholder)
10192 (progn
10193 (forward-char)
10194 (skip-chars-forward " \t"))
10195 (goto-char placeholder))
10196 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
10197 (c-most-enclosing-brace c-state-cache (point))
10198 paren-state))
10200 ;; CASE 17: Statement or defun catchall.
10202 (goto-char indent-point)
10203 ;; Back up statements until we find one that starts at boi.
10204 (while (let* ((prev-point (point))
10205 (last-step-type (c-beginning-of-statement-1
10206 containing-sexp)))
10207 (if (= (point) prev-point)
10208 (progn
10209 (setq step-type (or step-type last-step-type))
10210 nil)
10211 (setq step-type last-step-type)
10212 (/= (point) (c-point 'boi)))))
10213 (cond
10215 ;; CASE 17B: continued statement
10216 ((and (eq step-type 'same)
10217 (/= (point) indent-point))
10218 (c-add-stmt-syntax 'statement-cont nil nil
10219 containing-sexp paren-state))
10221 ;; CASE 17A: After a case/default label?
10222 ((progn
10223 (while (and (eq step-type 'label)
10224 (not (looking-at c-label-kwds-regexp)))
10225 (setq step-type
10226 (c-beginning-of-statement-1 containing-sexp)))
10227 (eq step-type 'label))
10228 (c-add-stmt-syntax (if (eq char-after-ip ?{)
10229 'statement-case-open
10230 'statement-case-intro)
10231 nil t containing-sexp paren-state))
10233 ;; CASE 17D: any old statement
10234 ((progn
10235 (while (eq step-type 'label)
10236 (setq step-type
10237 (c-beginning-of-statement-1 containing-sexp)))
10238 (eq step-type 'previous))
10239 (c-add-stmt-syntax 'statement nil t
10240 containing-sexp paren-state)
10241 (if (eq char-after-ip ?{)
10242 (c-add-syntax 'block-open)))
10244 ;; CASE 17I: Inside a substatement block.
10245 ((progn
10246 ;; The following tests are all based on containing-sexp.
10247 (goto-char containing-sexp)
10248 ;; From here on we have the next containing sexp in lim.
10249 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
10250 (c-after-conditional))
10251 (c-backward-to-block-anchor lim)
10252 (c-add-stmt-syntax 'statement-block-intro nil t
10253 lim paren-state)
10254 (if (eq char-after-ip ?{)
10255 (c-add-syntax 'block-open)))
10257 ;; CASE 17E: first statement in an in-expression block.
10258 ;; C.f. cases 4, 7B and 16A.
10259 ((setq placeholder (c-looking-at-inexpr-block
10260 (c-safe-position containing-sexp paren-state)
10261 nil))
10262 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10263 'defun-block-intro
10264 'statement-block-intro))
10265 (back-to-indentation)
10266 (if (= containing-sexp (point))
10267 (c-add-syntax tmpsymbol (point))
10268 (goto-char (cdr placeholder))
10269 (back-to-indentation)
10270 (c-add-stmt-syntax tmpsymbol nil t
10271 (c-most-enclosing-brace c-state-cache (point))
10272 paren-state)
10273 (if (/= (point) (cdr placeholder))
10274 (c-add-syntax (car placeholder))))
10275 (if (eq char-after-ip ?{)
10276 (c-add-syntax 'block-open)))
10278 ;; CASE 17F: first statement in an inline, or first
10279 ;; statement in a top-level defun. we can tell this is it
10280 ;; if there are no enclosing braces that haven't been
10281 ;; narrowed out by a class (i.e. don't use bod here).
10282 ((save-excursion
10283 (or (not (setq placeholder (c-most-enclosing-brace
10284 paren-state)))
10285 (and (progn
10286 (goto-char placeholder)
10287 (eq (char-after) ?{))
10288 (c-looking-at-decl-block (c-most-enclosing-brace
10289 paren-state (point))
10290 nil))))
10291 (c-backward-to-decl-anchor lim)
10292 (back-to-indentation)
10293 (c-add-syntax 'defun-block-intro (point)))
10295 ;; CASE 17G: First statement in a function declared inside
10296 ;; a normal block. This can occur in Pike and with
10297 ;; e.g. the gcc extensions, but watch out for macros
10298 ;; followed by blocks. C.f. cases B.3 and 16F.
10299 ((save-excursion
10300 (and (not (c-at-statement-start-p))
10301 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10302 (setq placeholder (point))
10303 (let ((c-recognize-typeless-decls nil))
10304 ;; Turn off recognition of constructs that lacks
10305 ;; a type in this case, since that's more likely
10306 ;; to be a macro followed by a block.
10307 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10308 (back-to-indentation)
10309 (if (/= (point) containing-sexp)
10310 (goto-char placeholder))
10311 (c-add-stmt-syntax 'defun-block-intro nil t
10312 lim paren-state))
10314 ;; CASE 17H: First statement in a block.
10316 ;; If the block is preceded by a case/switch label on the
10317 ;; same line, we anchor at the first preceding label at
10318 ;; boi. The default handling in c-add-stmt-syntax is
10319 ;; really fixes it better, but we do like this to keep the
10320 ;; indentation compatible with version 5.28 and earlier.
10321 ;; C.f. case 16C.
10322 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10323 (eq (c-beginning-of-statement-1 lim) 'label)))
10324 (goto-char placeholder)
10325 (if (looking-at c-label-kwds-regexp)
10326 (c-add-syntax 'statement-block-intro (point))
10327 (goto-char containing-sexp)
10328 ;; c-backward-to-block-anchor not necessary here; those
10329 ;; situations are handled in case 17I above.
10330 (c-add-stmt-syntax 'statement-block-intro nil t
10331 lim paren-state))
10332 (if (eq char-after-ip ?{)
10333 (c-add-syntax 'block-open)))
10337 ;; now we need to look at any modifiers
10338 (goto-char indent-point)
10339 (skip-chars-forward " \t")
10341 ;; are we looking at a comment only line?
10342 (when (and (looking-at c-comment-start-regexp)
10343 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
10344 (c-append-syntax 'comment-intro))
10346 ;; we might want to give additional offset to friends (in C++).
10347 (when (and c-opt-friend-key
10348 (looking-at c-opt-friend-key))
10349 (c-append-syntax 'friend))
10351 ;; Set syntactic-relpos.
10352 (let ((p c-syntactic-context))
10353 (while (and p
10354 (if (integerp (c-langelem-pos (car p)))
10355 (progn
10356 (setq syntactic-relpos (c-langelem-pos (car p)))
10357 nil)
10359 (setq p (cdr p))))
10361 ;; Start of or a continuation of a preprocessor directive?
10362 (if (and macro-start
10363 (eq macro-start (c-point 'boi))
10364 (not (and (c-major-mode-is 'pike-mode)
10365 (eq (char-after (1+ macro-start)) ?\"))))
10366 (c-append-syntax 'cpp-macro)
10367 (when (and c-syntactic-indentation-in-macros macro-start)
10368 (if in-macro-expr
10369 (when (or
10370 (< syntactic-relpos macro-start)
10371 (not (or
10372 (assq 'arglist-intro c-syntactic-context)
10373 (assq 'arglist-cont c-syntactic-context)
10374 (assq 'arglist-cont-nonempty c-syntactic-context)
10375 (assq 'arglist-close c-syntactic-context))))
10376 ;; If inside a cpp expression, i.e. anywhere in a
10377 ;; cpp directive except a #define body, we only let
10378 ;; through the syntactic analysis that is internal
10379 ;; in the expression. That means the arglist
10380 ;; elements, if they are anchored inside the cpp
10381 ;; expression.
10382 (setq c-syntactic-context nil)
10383 (c-add-syntax 'cpp-macro-cont macro-start))
10384 (when (and (eq macro-start syntactic-relpos)
10385 (not (assq 'cpp-define-intro c-syntactic-context))
10386 (save-excursion
10387 (goto-char macro-start)
10388 (or (not (c-forward-to-cpp-define-body))
10389 (<= (point) (c-point 'boi indent-point)))))
10390 ;; Inside a #define body and the syntactic analysis is
10391 ;; anchored on the start of the #define. In this case
10392 ;; we add cpp-define-intro to get the extra
10393 ;; indentation of the #define body.
10394 (c-add-syntax 'cpp-define-intro)))))
10396 ;; return the syntax
10397 c-syntactic-context)))
10400 ;; Indentation calculation.
10402 (defun c-evaluate-offset (offset langelem symbol)
10403 ;; offset can be a number, a function, a variable, a list, or one of
10404 ;; the symbols + or -
10406 ;; This function might do hidden buffer changes.
10407 (let ((res
10408 (cond
10409 ((numberp offset) offset)
10410 ((vectorp offset) offset)
10411 ((null offset) nil)
10413 ((eq offset '+) c-basic-offset)
10414 ((eq offset '-) (- c-basic-offset))
10415 ((eq offset '++) (* 2 c-basic-offset))
10416 ((eq offset '--) (* 2 (- c-basic-offset)))
10417 ((eq offset '*) (/ c-basic-offset 2))
10418 ((eq offset '/) (/ (- c-basic-offset) 2))
10420 ((functionp offset)
10421 (c-evaluate-offset
10422 (funcall offset
10423 (cons (c-langelem-sym langelem)
10424 (c-langelem-pos langelem)))
10425 langelem symbol))
10427 ((listp offset)
10428 (cond
10429 ((eq (car offset) 'quote)
10430 (c-benign-error "The offset %S for %s was mistakenly quoted"
10431 offset symbol)
10432 nil)
10434 ((memq (car offset) '(min max))
10435 (let (res val (method (car offset)))
10436 (setq offset (cdr offset))
10437 (while offset
10438 (setq val (c-evaluate-offset (car offset) langelem symbol))
10439 (cond
10440 ((not val))
10441 ((not res)
10442 (setq res val))
10443 ((integerp val)
10444 (if (vectorp res)
10445 (c-benign-error "\
10446 Error evaluating offset %S for %s: \
10447 Cannot combine absolute offset %S with relative %S in `%s' method"
10448 (car offset) symbol res val method)
10449 (setq res (funcall method res val))))
10451 (if (integerp res)
10452 (c-benign-error "\
10453 Error evaluating offset %S for %s: \
10454 Cannot combine relative offset %S with absolute %S in `%s' method"
10455 (car offset) symbol res val method)
10456 (setq res (vector (funcall method (aref res 0)
10457 (aref val 0)))))))
10458 (setq offset (cdr offset)))
10459 res))
10461 ((eq (car offset) 'add)
10462 (let (res val)
10463 (setq offset (cdr offset))
10464 (while offset
10465 (setq val (c-evaluate-offset (car offset) langelem symbol))
10466 (cond
10467 ((not val))
10468 ((not res)
10469 (setq res val))
10470 ((integerp val)
10471 (if (vectorp res)
10472 (setq res (vector (+ (aref res 0) val)))
10473 (setq res (+ res val))))
10475 (if (vectorp res)
10476 (c-benign-error "\
10477 Error evaluating offset %S for %s: \
10478 Cannot combine absolute offsets %S and %S in `add' method"
10479 (car offset) symbol res val)
10480 (setq res val)))) ; Override.
10481 (setq offset (cdr offset)))
10482 res))
10485 (let (res)
10486 (when (eq (car offset) 'first)
10487 (setq offset (cdr offset)))
10488 (while (and (not res) offset)
10489 (setq res (c-evaluate-offset (car offset) langelem symbol)
10490 offset (cdr offset)))
10491 res))))
10493 ((and (symbolp offset) (boundp offset))
10494 (symbol-value offset))
10497 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10498 nil))))
10500 (if (or (null res) (integerp res)
10501 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10503 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10504 offset symbol res)
10505 nil)))
10507 (defun c-calc-offset (langelem)
10508 ;; Get offset from LANGELEM which is a list beginning with the
10509 ;; syntactic symbol and followed by any analysis data it provides.
10510 ;; That data may be zero or more elements, but if at least one is
10511 ;; given then the first is the anchor position (or nil). The symbol
10512 ;; is matched against `c-offsets-alist' and the offset calculated
10513 ;; from that is returned.
10515 ;; This function might do hidden buffer changes.
10516 (let* ((symbol (c-langelem-sym langelem))
10517 (match (assq symbol c-offsets-alist))
10518 (offset (cdr-safe match)))
10519 (if match
10520 (setq offset (c-evaluate-offset offset langelem symbol))
10521 (if c-strict-syntax-p
10522 (c-benign-error "No offset found for syntactic symbol %s" symbol))
10523 (setq offset 0))
10524 (if (vectorp offset)
10525 offset
10526 (or (and (numberp offset) offset)
10527 (and (symbolp offset) (symbol-value offset))
10531 (defun c-get-offset (langelem)
10532 ;; This is a compatibility wrapper for `c-calc-offset' in case
10533 ;; someone is calling it directly. It takes an old style syntactic
10534 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10535 ;; new list form.
10537 ;; This function might do hidden buffer changes.
10538 (if (c-langelem-pos langelem)
10539 (c-calc-offset (list (c-langelem-sym langelem)
10540 (c-langelem-pos langelem)))
10541 (c-calc-offset langelem)))
10543 (defun c-get-syntactic-indentation (langelems)
10544 ;; Calculate the syntactic indentation from a syntactic description
10545 ;; as returned by `c-guess-syntax'.
10547 ;; Note that topmost-intro always has an anchor position at bol, for
10548 ;; historical reasons. It's often used together with other symbols
10549 ;; that has more sane positions. Since we always use the first
10550 ;; found anchor position, we rely on that these other symbols always
10551 ;; precede topmost-intro in the LANGELEMS list.
10553 ;; This function might do hidden buffer changes.
10554 (let ((indent 0) anchor)
10556 (while langelems
10557 (let* ((c-syntactic-element (car langelems))
10558 (res (c-calc-offset c-syntactic-element)))
10560 (if (vectorp res)
10561 ;; Got an absolute column that overrides any indentation
10562 ;; we've collected so far, but not the relative
10563 ;; indentation we might get for the nested structures
10564 ;; further down the langelems list.
10565 (setq indent (elt res 0)
10566 anchor (point-min)) ; A position at column 0.
10568 ;; Got a relative change of the current calculated
10569 ;; indentation.
10570 (setq indent (+ indent res))
10572 ;; Use the anchor position from the first syntactic
10573 ;; element with one.
10574 (unless anchor
10575 (setq anchor (c-langelem-pos (car langelems)))))
10577 (setq langelems (cdr langelems))))
10579 (if anchor
10580 (+ indent (save-excursion
10581 (goto-char anchor)
10582 (current-column)))
10583 indent)))
10586 (cc-provide 'cc-engine)
10588 ;;; cc-engine.el ends here