DTRT for c-beginning/end-of-defun in nested declaration scopes.
[emacs.git] / lisp / progmodes / cc-engine.el
blobbc42e1032ab972caa30d75fc469ed93c333b0ffb
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 c-stmt-delim-chars)
1158 lit-range)
1159 (save-excursion
1160 (catch 'done
1161 (goto-char from)
1162 (while (progn (skip-chars-forward skip-chars to)
1163 (< (point) to))
1164 (cond
1165 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1166 (goto-char (cdr lit-range)))
1167 ((eq (char-after) ?:)
1168 (forward-char)
1169 (if (and (eq (char-after) ?:)
1170 (< (point) to))
1171 ;; Ignore scope operators.
1172 (forward-char)
1173 (setq c-maybe-labelp (1- (point)))))
1174 ((eq (char-after) ??)
1175 ;; A question mark. Can't be a label, so stop
1176 ;; looking for more : and ?.
1177 (setq c-maybe-labelp nil
1178 skip-chars (substring c-stmt-delim-chars 0 -2)))
1179 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1180 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1181 (backward-char))
1182 (skip-chars-backward " \t" from)
1183 (if (c-at-vsemi-p)
1184 (throw 'done (point))
1185 (forward-line)))
1186 (t (throw 'done (point)))))
1187 ;; In trailing space after an as yet undetected virtual semicolon?
1188 (c-backward-syntactic-ws from)
1189 (if (and (< (point) to)
1190 (c-at-vsemi-p))
1191 (point)
1192 nil)))))
1194 (defun c-at-statement-start-p ()
1195 "Return non-nil if the point is at the first token in a statement
1196 or somewhere in the syntactic whitespace before it.
1198 A \"statement\" here is not restricted to those inside code blocks.
1199 Any kind of declaration-like construct that occur outside function
1200 bodies is also considered a \"statement\".
1202 Note that this function might do hidden buffer changes. See the
1203 comment at the start of cc-engine.el for more info."
1205 (save-excursion
1206 (let ((end (point))
1207 c-maybe-labelp)
1208 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1209 (or (bobp)
1210 (eq (char-before) ?})
1211 (and (eq (char-before) ?{)
1212 (not (and c-special-brace-lists
1213 (progn (backward-char)
1214 (c-looking-at-special-brace-list)))))
1215 (c-crosses-statement-barrier-p (point) end)))))
1217 (defun c-at-expression-start-p ()
1218 "Return non-nil if the point is at the first token in an expression or
1219 statement, or somewhere in the syntactic whitespace before it.
1221 An \"expression\" here is a bit different from the normal language
1222 grammar sense: It's any sequence of expression tokens except commas,
1223 unless they are enclosed inside parentheses of some kind. Also, an
1224 expression never continues past an enclosing parenthesis, but it might
1225 contain parenthesis pairs of any sort except braces.
1227 Since expressions never cross statement boundaries, this function also
1228 recognizes statement beginnings, just like `c-at-statement-start-p'.
1230 Note that this function might do hidden buffer changes. See the
1231 comment at the start of cc-engine.el for more info."
1233 (save-excursion
1234 (let ((end (point))
1235 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1236 c-maybe-labelp)
1237 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1238 (or (bobp)
1239 (memq (char-before) '(?{ ?}))
1240 (save-excursion (backward-char)
1241 (looking-at "\\s("))
1242 (c-crosses-statement-barrier-p (point) end)))))
1245 ;; A set of functions that covers various idiosyncrasies in
1246 ;; implementations of `forward-comment'.
1248 ;; Note: Some emacsen considers incorrectly that any line comment
1249 ;; ending with a backslash continues to the next line. I can't think
1250 ;; of any way to work around that in a reliable way without changing
1251 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1252 ;; changing the syntax for backslash doesn't work since we must treat
1253 ;; escapes in string literals correctly.)
1255 (defun c-forward-single-comment ()
1256 "Move forward past whitespace and the closest following comment, if any.
1257 Return t if a comment was found, nil otherwise. In either case, the
1258 point is moved past the following whitespace. Line continuations,
1259 i.e. a backslashes followed by line breaks, are treated as whitespace.
1260 The line breaks that end line comments are considered to be the
1261 comment enders, so the point will be put on the beginning of the next
1262 line if it moved past a line comment.
1264 This function does not do any hidden buffer changes."
1266 (let ((start (point)))
1267 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1268 (goto-char (match-end 0)))
1270 (when (forward-comment 1)
1271 (if (eobp)
1272 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1273 ;; forwards at eob.
1276 ;; Emacs includes the ending newline in a b-style (c++)
1277 ;; comment, but XEmacs doesn't. We depend on the Emacs
1278 ;; behavior (which also is symmetric).
1279 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1280 (condition-case nil (forward-char 1)))
1282 t))))
1284 (defsubst c-forward-comments ()
1285 "Move forward past all following whitespace and comments.
1286 Line continuations, i.e. a backslashes followed by line breaks, are
1287 treated as whitespace.
1289 Note that this function might do hidden buffer changes. See the
1290 comment at the start of cc-engine.el for more info."
1292 (while (or
1293 ;; If forward-comment in at least XEmacs 21 is given a large
1294 ;; positive value, it'll loop all the way through if it hits
1295 ;; eob.
1296 (and (forward-comment 5)
1297 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1298 ;; forwards at eob.
1299 (not (eobp)))
1301 (when (looking-at "\\\\[\n\r]")
1302 (forward-char 2)
1303 t))))
1305 (defun c-backward-single-comment ()
1306 "Move backward past whitespace and the closest preceding comment, if any.
1307 Return t if a comment was found, nil otherwise. In either case, the
1308 point is moved past the preceding whitespace. Line continuations,
1309 i.e. a backslashes followed by line breaks, are treated as whitespace.
1310 The line breaks that end line comments are considered to be the
1311 comment enders, so the point cannot be at the end of the same line to
1312 move over a line comment.
1314 This function does not do any hidden buffer changes."
1316 (let ((start (point)))
1317 ;; When we got newline terminated comments, forward-comment in all
1318 ;; supported emacsen so far will stop at eol of each line not
1319 ;; ending with a comment when moving backwards. This corrects for
1320 ;; that, and at the same time handles line continuations.
1321 (while (progn
1322 (skip-chars-backward " \t\n\r\f\v")
1323 (and (looking-at "[\n\r]")
1324 (eq (char-before) ?\\)))
1325 (backward-char))
1327 (if (bobp)
1328 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1329 ;; backwards at bob.
1332 ;; Leave point after the closest following newline if we've
1333 ;; backed up over any above, since forward-comment won't move
1334 ;; backward over a line comment if point is at the end of the
1335 ;; same line.
1336 (re-search-forward "\\=\\s *[\n\r]" start t)
1338 (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
1339 (if (eolp)
1340 ;; If forward-comment above succeeded and we're at eol
1341 ;; then the newline we moved over above didn't end a
1342 ;; line comment, so we give it another go.
1343 (let (open-paren-in-column-0-is-defun-start)
1344 (forward-comment -1))
1347 ;; Emacs <= 20 and XEmacs move back over the closer of a
1348 ;; block comment that lacks an opener.
1349 (if (looking-at "\\*/")
1350 (progn (forward-char 2) nil)
1351 t)))))
1353 (defsubst c-backward-comments ()
1354 "Move backward past all preceding whitespace and comments.
1355 Line continuations, i.e. a backslashes followed by line breaks, are
1356 treated as whitespace. The line breaks that end line comments are
1357 considered to be the comment enders, so the point cannot be at the end
1358 of the same line to move over a line comment. Unlike
1359 c-backward-syntactic-ws, this function doesn't move back over
1360 preprocessor directives.
1362 Note that this function might do hidden buffer changes. See the
1363 comment at the start of cc-engine.el for more info."
1365 (let ((start (point)))
1366 (while (and
1367 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1368 ;; return t when moving backwards at bob.
1369 (not (bobp))
1371 (if (let (open-paren-in-column-0-is-defun-start)
1372 (forward-comment -1))
1373 (if (looking-at "\\*/")
1374 ;; Emacs <= 20 and XEmacs move back over the
1375 ;; closer of a block comment that lacks an opener.
1376 (progn (forward-char 2) nil)
1379 ;; XEmacs treats line continuations as whitespace but
1380 ;; only in the backward direction, which seems a bit
1381 ;; odd. Anyway, this is necessary for Emacs.
1382 (when (and (looking-at "[\n\r]")
1383 (eq (char-before) ?\\)
1384 (< (point) start))
1385 (backward-char)
1386 t))))))
1389 ;; Tools for skipping over syntactic whitespace.
1391 ;; The following functions use text properties to cache searches over
1392 ;; large regions of syntactic whitespace. It works as follows:
1394 ;; o If a syntactic whitespace region contains anything but simple
1395 ;; whitespace (i.e. space, tab and line breaks), the text property
1396 ;; `c-in-sws' is put over it. At places where we have stopped
1397 ;; within that region there's also a `c-is-sws' text property.
1398 ;; That since there typically are nested whitespace inside that
1399 ;; must be handled separately, e.g. whitespace inside a comment or
1400 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1401 ;; to jump to another point with that property within the same
1402 ;; `c-in-sws' region. It can be likened to a ladder where
1403 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1405 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1406 ;; a "rung position" and also maybe on the first following char.
1407 ;; As many characters as can be conveniently found in this range
1408 ;; are marked, but no assumption can be made that the whole range
1409 ;; is marked (it could be clobbered by later changes, for
1410 ;; instance).
1412 ;; Note that some part of the beginning of a sequence of simple
1413 ;; whitespace might be part of the end of a preceding line comment
1414 ;; or cpp directive and must not be considered part of the "rung".
1415 ;; Such whitespace is some amount of horizontal whitespace followed
1416 ;; by a newline. In the case of cpp directives it could also be
1417 ;; two newlines with horizontal whitespace between them.
1419 ;; The reason to include the first following char is to cope with
1420 ;; "rung positions" that doesn't have any ordinary whitespace. If
1421 ;; `c-is-sws' is put on a token character it does not have
1422 ;; `c-in-sws' set simultaneously. That's the only case when that
1423 ;; can occur, and the reason for not extending the `c-in-sws'
1424 ;; region to cover it is that the `c-in-sws' region could then be
1425 ;; accidentally merged with a following one if the token is only
1426 ;; one character long.
1428 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1429 ;; removed in the changed region. If the change was inside
1430 ;; syntactic whitespace that means that the "ladder" is broken, but
1431 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1432 ;; parts on either side and use an ordinary search only to "repair"
1433 ;; the gap.
1435 ;; Special care needs to be taken if a region is removed: If there
1436 ;; are `c-in-sws' on both sides of it which do not connect inside
1437 ;; the region then they can't be joined. If e.g. a marked macro is
1438 ;; broken, syntactic whitespace inside the new text might be
1439 ;; marked. If those marks would become connected with the old
1440 ;; `c-in-sws' range around the macro then we could get a ladder
1441 ;; with one end outside the macro and the other at some whitespace
1442 ;; within it.
1444 ;; The main motivation for this system is to increase the speed in
1445 ;; skipping over the large whitespace regions that can occur at the
1446 ;; top level in e.g. header files that contain a lot of comments and
1447 ;; cpp directives. For small comments inside code it's probably
1448 ;; slower than using `forward-comment' straightforwardly, but speed is
1449 ;; not a significant factor there anyway.
1451 ; (defface c-debug-is-sws-face
1452 ; '((t (:background "GreenYellow")))
1453 ; "Debug face to mark the `c-is-sws' property.")
1454 ; (defface c-debug-in-sws-face
1455 ; '((t (:underline t)))
1456 ; "Debug face to mark the `c-in-sws' property.")
1458 ; (defun c-debug-put-sws-faces ()
1459 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1460 ; ;; properties in the buffer.
1461 ; (interactive)
1462 ; (save-excursion
1463 ; (c-save-buffer-state (in-face)
1464 ; (goto-char (point-min))
1465 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1466 ; (point)))
1467 ; (while (progn
1468 ; (goto-char (next-single-property-change
1469 ; (point) 'c-is-sws nil (point-max)))
1470 ; (if in-face
1471 ; (progn
1472 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1473 ; (setq in-face nil))
1474 ; (setq in-face (point)))
1475 ; (not (eobp))))
1476 ; (goto-char (point-min))
1477 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1478 ; (point)))
1479 ; (while (progn
1480 ; (goto-char (next-single-property-change
1481 ; (point) 'c-in-sws nil (point-max)))
1482 ; (if in-face
1483 ; (progn
1484 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1485 ; (setq in-face nil))
1486 ; (setq in-face (point)))
1487 ; (not (eobp)))))))
1489 (defmacro c-debug-sws-msg (&rest args)
1490 ;;`(message ,@args)
1493 (defmacro c-put-is-sws (beg end)
1494 ;; This macro does a hidden buffer change.
1495 `(let ((beg ,beg) (end ,end))
1496 (put-text-property beg end 'c-is-sws t)
1497 ,@(when (facep 'c-debug-is-sws-face)
1498 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1500 (defmacro c-put-in-sws (beg end)
1501 ;; This macro does a hidden buffer change.
1502 `(let ((beg ,beg) (end ,end))
1503 (put-text-property beg end 'c-in-sws t)
1504 ,@(when (facep 'c-debug-is-sws-face)
1505 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1507 (defmacro c-remove-is-sws (beg end)
1508 ;; This macro does a hidden buffer change.
1509 `(let ((beg ,beg) (end ,end))
1510 (remove-text-properties beg end '(c-is-sws nil))
1511 ,@(when (facep 'c-debug-is-sws-face)
1512 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1514 (defmacro c-remove-in-sws (beg end)
1515 ;; This macro does a hidden buffer change.
1516 `(let ((beg ,beg) (end ,end))
1517 (remove-text-properties beg end '(c-in-sws nil))
1518 ,@(when (facep 'c-debug-is-sws-face)
1519 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1521 (defmacro c-remove-is-and-in-sws (beg end)
1522 ;; This macro does a hidden buffer change.
1523 `(let ((beg ,beg) (end ,end))
1524 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1525 ,@(when (facep 'c-debug-is-sws-face)
1526 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1527 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1529 (defsubst c-invalidate-sws-region-after (beg end)
1530 ;; Called from `after-change-functions'. Note that if
1531 ;; `c-forward-sws' or `c-backward-sws' are used outside
1532 ;; `c-save-buffer-state' or similar then this will remove the cache
1533 ;; properties right after they're added.
1535 ;; This function does hidden buffer changes.
1537 (save-excursion
1538 ;; Adjust the end to remove the properties in any following simple
1539 ;; ws up to and including the next line break, if there is any
1540 ;; after the changed region. This is necessary e.g. when a rung
1541 ;; marked empty line is converted to a line comment by inserting
1542 ;; "//" before the line break. In that case the line break would
1543 ;; keep the rung mark which could make a later `c-backward-sws'
1544 ;; move into the line comment instead of over it.
1545 (goto-char end)
1546 (skip-chars-forward " \t\f\v")
1547 (when (and (eolp) (not (eobp)))
1548 (setq end (1+ (point)))))
1550 (when (and (= beg end)
1551 (get-text-property beg 'c-in-sws)
1552 (> beg (point-min))
1553 (get-text-property (1- beg) 'c-in-sws))
1554 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1555 ;; safe to keep a range that was continuous before the change. E.g:
1557 ;; #define foo
1558 ;; \
1559 ;; bar
1561 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1562 ;; after "foo" is removed then "bar" will become part of the cpp
1563 ;; directive instead of a syntactically relevant token. In that
1564 ;; case there's no longer syntactic ws from "#" to "b".
1565 (setq beg (1- beg)))
1567 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1568 (c-remove-is-and-in-sws beg end))
1570 (defun c-forward-sws ()
1571 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1573 ;; This function might do hidden buffer changes.
1575 (let (;; `rung-pos' is set to a position as early as possible in the
1576 ;; unmarked part of the simple ws region.
1577 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1578 rung-is-marked next-rung-is-marked simple-ws-end
1579 ;; `safe-start' is set when it's safe to cache the start position.
1580 ;; It's not set if we've initially skipped over comments and line
1581 ;; continuations since we might have gone out through the end of a
1582 ;; macro then. This provision makes `c-forward-sws' not populate the
1583 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1584 ;; more common.
1585 safe-start)
1587 ;; Skip simple ws and do a quick check on the following character to see
1588 ;; if it's anything that can't start syntactic ws, so we can bail out
1589 ;; early in the majority of cases when there just are a few ws chars.
1590 (skip-chars-forward " \t\n\r\f\v")
1591 (when (looking-at c-syntactic-ws-start)
1593 (setq rung-end-pos (min (1+ (point)) (point-max)))
1594 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1595 'c-is-sws t))
1596 ;; Find the last rung position to avoid setting properties in all
1597 ;; the cases when the marked rung is complete.
1598 ;; (`next-single-property-change' is certain to move at least one
1599 ;; step forward.)
1600 (setq rung-pos (1- (next-single-property-change
1601 rung-is-marked 'c-is-sws nil rung-end-pos)))
1602 ;; Got no marked rung here. Since the simple ws might have started
1603 ;; inside a line comment or cpp directive we must set `rung-pos' as
1604 ;; high as possible.
1605 (setq rung-pos (point)))
1607 (while
1608 (progn
1609 (while
1610 (when (and rung-is-marked
1611 (get-text-property (point) 'c-in-sws))
1613 ;; The following search is the main reason that `c-in-sws'
1614 ;; and `c-is-sws' aren't combined to one property.
1615 (goto-char (next-single-property-change
1616 (point) 'c-in-sws nil (point-max)))
1617 (unless (get-text-property (point) 'c-is-sws)
1618 ;; If the `c-in-sws' region extended past the last
1619 ;; `c-is-sws' char we have to go back a bit.
1620 (or (get-text-property (1- (point)) 'c-is-sws)
1621 (goto-char (previous-single-property-change
1622 (point) 'c-is-sws)))
1623 (backward-char))
1625 (c-debug-sws-msg
1626 "c-forward-sws cached move %s -> %s (max %s)"
1627 rung-pos (point) (point-max))
1629 (setq rung-pos (point))
1630 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1631 (not (eobp))))
1633 ;; We'll loop here if there is simple ws after the last rung.
1634 ;; That means that there's been some change in it and it's
1635 ;; possible that we've stepped into another ladder, so extend
1636 ;; the previous one to join with it if there is one, and try to
1637 ;; use the cache again.
1638 (c-debug-sws-msg
1639 "c-forward-sws extending rung with [%s..%s] (max %s)"
1640 (1+ rung-pos) (1+ (point)) (point-max))
1641 (unless (get-text-property (point) 'c-is-sws)
1642 ;; Remove any `c-in-sws' property from the last char of
1643 ;; the rung before we mark it with `c-is-sws', so that we
1644 ;; won't connect with the remains of a broken "ladder".
1645 (c-remove-in-sws (point) (1+ (point))))
1646 (c-put-is-sws (1+ rung-pos)
1647 (1+ (point)))
1648 (c-put-in-sws rung-pos
1649 (setq rung-pos (point)
1650 last-put-in-sws-pos rung-pos)))
1652 (setq simple-ws-end (point))
1653 (c-forward-comments)
1655 (cond
1656 ((/= (point) simple-ws-end)
1657 ;; Skipped over comments. Don't cache at eob in case the buffer
1658 ;; is narrowed.
1659 (not (eobp)))
1661 ((save-excursion
1662 (and c-opt-cpp-prefix
1663 (looking-at c-opt-cpp-start)
1664 (progn (skip-chars-backward " \t")
1665 (bolp))
1666 (or (bobp)
1667 (progn (backward-char)
1668 (not (eq (char-before) ?\\))))))
1669 ;; Skip a preprocessor directive.
1670 (end-of-line)
1671 (while (and (eq (char-before) ?\\)
1672 (= (forward-line 1) 0))
1673 (end-of-line))
1674 (forward-line 1)
1675 (setq safe-start t)
1676 ;; Don't cache at eob in case the buffer is narrowed.
1677 (not (eobp)))))
1679 ;; We've searched over a piece of non-white syntactic ws. See if this
1680 ;; can be cached.
1681 (setq next-rung-pos (point))
1682 (skip-chars-forward " \t\n\r\f\v")
1683 (setq rung-end-pos (min (1+ (point)) (point-max)))
1685 (if (or
1686 ;; Cache if we haven't skipped comments only, and if we started
1687 ;; either from a marked rung or from a completely uncached
1688 ;; position.
1689 (and safe-start
1690 (or rung-is-marked
1691 (not (get-text-property simple-ws-end 'c-in-sws))))
1693 ;; See if there's a marked rung in the encountered simple ws. If
1694 ;; so then we can cache, unless `safe-start' is nil. Even then
1695 ;; we need to do this to check if the cache can be used for the
1696 ;; next step.
1697 (and (setq next-rung-is-marked
1698 (text-property-any next-rung-pos rung-end-pos
1699 'c-is-sws t))
1700 safe-start))
1702 (progn
1703 (c-debug-sws-msg
1704 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1705 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1706 (point-max))
1708 ;; Remove the properties for any nested ws that might be cached.
1709 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1710 ;; anyway.
1711 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1712 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1713 (c-put-is-sws rung-pos
1714 (1+ simple-ws-end))
1715 (setq rung-is-marked t))
1716 (c-put-in-sws rung-pos
1717 (setq rung-pos (point)
1718 last-put-in-sws-pos rung-pos))
1719 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1720 ;; Remove any `c-in-sws' property from the last char of
1721 ;; the rung before we mark it with `c-is-sws', so that we
1722 ;; won't connect with the remains of a broken "ladder".
1723 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1724 (c-put-is-sws next-rung-pos
1725 rung-end-pos))
1727 (c-debug-sws-msg
1728 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1729 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1730 (point-max))
1732 ;; Set `rung-pos' for the next rung. It's the same thing here as
1733 ;; initially, except that the rung position is set as early as
1734 ;; possible since we can't be in the ending ws of a line comment or
1735 ;; cpp directive now.
1736 (if (setq rung-is-marked next-rung-is-marked)
1737 (setq rung-pos (1- (next-single-property-change
1738 rung-is-marked 'c-is-sws nil rung-end-pos)))
1739 (setq rung-pos next-rung-pos))
1740 (setq safe-start t)))
1742 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1743 ;; another one after the point (which might occur when editing inside a
1744 ;; comment or macro).
1745 (when (eq last-put-in-sws-pos (point))
1746 (cond ((< last-put-in-sws-pos (point-max))
1747 (c-debug-sws-msg
1748 "c-forward-sws clearing at %s for cache separation"
1749 last-put-in-sws-pos)
1750 (c-remove-in-sws last-put-in-sws-pos
1751 (1+ last-put-in-sws-pos)))
1753 ;; If at eob we have to clear the last character before the end
1754 ;; instead since the buffer might be narrowed and there might
1755 ;; be a `c-in-sws' after (point-max). In this case it's
1756 ;; necessary to clear both properties.
1757 (c-debug-sws-msg
1758 "c-forward-sws clearing thoroughly at %s for cache separation"
1759 (1- last-put-in-sws-pos))
1760 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1761 last-put-in-sws-pos))))
1764 (defun c-backward-sws ()
1765 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1767 ;; This function might do hidden buffer changes.
1769 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1770 ;; part of the simple ws region.
1771 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1772 rung-is-marked simple-ws-beg cmt-skip-pos)
1774 ;; Skip simple horizontal ws and do a quick check on the preceding
1775 ;; character to see if it's anying that can't end syntactic ws, so we can
1776 ;; bail out early in the majority of cases when there just are a few ws
1777 ;; chars. Newlines are complicated in the backward direction, so we can't
1778 ;; skip over them.
1779 (skip-chars-backward " \t\f")
1780 (when (and (not (bobp))
1781 (save-excursion
1782 (backward-char)
1783 (looking-at c-syntactic-ws-end)))
1785 ;; Try to find a rung position in the simple ws preceding point, so that
1786 ;; we can get a cache hit even if the last bit of the simple ws has
1787 ;; changed recently.
1788 (setq simple-ws-beg (point))
1789 (skip-chars-backward " \t\n\r\f\v")
1790 (if (setq rung-is-marked (text-property-any
1791 (point) (min (1+ rung-pos) (point-max))
1792 'c-is-sws t))
1793 ;; `rung-pos' will be the earliest marked position, which means that
1794 ;; there might be later unmarked parts in the simple ws region.
1795 ;; It's not worth the effort to fix that; the last part of the
1796 ;; simple ws is also typically edited often, so it could be wasted.
1797 (goto-char (setq rung-pos rung-is-marked))
1798 (goto-char simple-ws-beg))
1800 (while
1801 (progn
1802 (while
1803 (when (and rung-is-marked
1804 (not (bobp))
1805 (get-text-property (1- (point)) 'c-in-sws))
1807 ;; The following search is the main reason that `c-in-sws'
1808 ;; and `c-is-sws' aren't combined to one property.
1809 (goto-char (previous-single-property-change
1810 (point) 'c-in-sws nil (point-min)))
1811 (unless (get-text-property (point) 'c-is-sws)
1812 ;; If the `c-in-sws' region extended past the first
1813 ;; `c-is-sws' char we have to go forward a bit.
1814 (goto-char (next-single-property-change
1815 (point) 'c-is-sws)))
1817 (c-debug-sws-msg
1818 "c-backward-sws cached move %s <- %s (min %s)"
1819 (point) rung-pos (point-min))
1821 (setq rung-pos (point))
1822 (if (and (< (min (skip-chars-backward " \t\f\v")
1823 (progn
1824 (setq simple-ws-beg (point))
1825 (skip-chars-backward " \t\n\r\f\v")))
1827 (setq rung-is-marked
1828 (text-property-any (point) rung-pos
1829 'c-is-sws t)))
1831 (goto-char simple-ws-beg)
1832 nil))
1834 ;; We'll loop here if there is simple ws before the first rung.
1835 ;; That means that there's been some change in it and it's
1836 ;; possible that we've stepped into another ladder, so extend
1837 ;; the previous one to join with it if there is one, and try to
1838 ;; use the cache again.
1839 (c-debug-sws-msg
1840 "c-backward-sws extending rung with [%s..%s] (min %s)"
1841 rung-is-marked rung-pos (point-min))
1842 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1843 ;; Remove any `c-in-sws' property from the last char of
1844 ;; the rung before we mark it with `c-is-sws', so that we
1845 ;; won't connect with the remains of a broken "ladder".
1846 (c-remove-in-sws (1- rung-pos) rung-pos))
1847 (c-put-is-sws rung-is-marked
1848 rung-pos)
1849 (c-put-in-sws rung-is-marked
1850 (1- rung-pos))
1851 (setq rung-pos rung-is-marked
1852 last-put-in-sws-pos rung-pos))
1854 (c-backward-comments)
1855 (setq cmt-skip-pos (point))
1857 (cond
1858 ((and c-opt-cpp-prefix
1859 (/= cmt-skip-pos simple-ws-beg)
1860 (c-beginning-of-macro))
1861 ;; Inside a cpp directive. See if it should be skipped over.
1862 (let ((cpp-beg (point)))
1864 ;; Move back over all line continuations in the region skipped
1865 ;; over by `c-backward-comments'. If we go past it then we
1866 ;; started inside the cpp directive.
1867 (goto-char simple-ws-beg)
1868 (beginning-of-line)
1869 (while (and (> (point) cmt-skip-pos)
1870 (progn (backward-char)
1871 (eq (char-before) ?\\)))
1872 (beginning-of-line))
1874 (if (< (point) cmt-skip-pos)
1875 ;; Don't move past the cpp directive if we began inside
1876 ;; it. Note that the position at the end of the last line
1877 ;; of the macro is also considered to be within it.
1878 (progn (goto-char cmt-skip-pos)
1879 nil)
1881 ;; It's worthwhile to spend a little bit of effort on finding
1882 ;; the end of the macro, to get a good `simple-ws-beg'
1883 ;; position for the cache. Note that `c-backward-comments'
1884 ;; could have stepped over some comments before going into
1885 ;; the macro, and then `simple-ws-beg' must be kept on the
1886 ;; same side of those comments.
1887 (goto-char simple-ws-beg)
1888 (skip-chars-backward " \t\n\r\f\v")
1889 (if (eq (char-before) ?\\)
1890 (forward-char))
1891 (forward-line 1)
1892 (if (< (point) simple-ws-beg)
1893 ;; Might happen if comments after the macro were skipped
1894 ;; over.
1895 (setq simple-ws-beg (point)))
1897 (goto-char cpp-beg)
1898 t)))
1900 ((/= (save-excursion
1901 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1902 (setq next-rung-pos (point)))
1903 simple-ws-beg)
1904 ;; Skipped over comments. Must put point at the end of
1905 ;; the simple ws at point since we might be after a line
1906 ;; comment or cpp directive that's been partially
1907 ;; narrowed out, and we can't risk marking the simple ws
1908 ;; at the end of it.
1909 (goto-char next-rung-pos)
1910 t)))
1912 ;; We've searched over a piece of non-white syntactic ws. See if this
1913 ;; can be cached.
1914 (setq next-rung-pos (point))
1915 (skip-chars-backward " \t\f\v")
1917 (if (or
1918 ;; Cache if we started either from a marked rung or from a
1919 ;; completely uncached position.
1920 rung-is-marked
1921 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1923 ;; Cache if there's a marked rung in the encountered simple ws.
1924 (save-excursion
1925 (skip-chars-backward " \t\n\r\f\v")
1926 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1927 'c-is-sws t)))
1929 (progn
1930 (c-debug-sws-msg
1931 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1932 (point) (1+ next-rung-pos)
1933 simple-ws-beg (min (1+ rung-pos) (point-max))
1934 (point-min))
1936 ;; Remove the properties for any nested ws that might be cached.
1937 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1938 ;; anyway.
1939 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1940 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1941 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1942 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1943 ;; Remove any `c-in-sws' property from the last char of
1944 ;; the rung before we mark it with `c-is-sws', so that we
1945 ;; won't connect with the remains of a broken "ladder".
1946 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1947 (c-put-is-sws simple-ws-beg
1948 rung-end-pos)
1949 (setq rung-is-marked t)))
1950 (c-put-in-sws (setq simple-ws-beg (point)
1951 last-put-in-sws-pos simple-ws-beg)
1952 rung-pos)
1953 (c-put-is-sws (setq rung-pos simple-ws-beg)
1954 (1+ next-rung-pos)))
1956 (c-debug-sws-msg
1957 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1958 (point) (1+ next-rung-pos)
1959 simple-ws-beg (min (1+ rung-pos) (point-max))
1960 (point-min))
1961 (setq rung-pos next-rung-pos
1962 simple-ws-beg (point))
1965 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1966 ;; another one before the point (which might occur when editing inside a
1967 ;; comment or macro).
1968 (when (eq last-put-in-sws-pos (point))
1969 (cond ((< (point-min) last-put-in-sws-pos)
1970 (c-debug-sws-msg
1971 "c-backward-sws clearing at %s for cache separation"
1972 (1- last-put-in-sws-pos))
1973 (c-remove-in-sws (1- last-put-in-sws-pos)
1974 last-put-in-sws-pos))
1975 ((> (point-min) 1)
1976 ;; If at bob and the buffer is narrowed, we have to clear the
1977 ;; character we're standing on instead since there might be a
1978 ;; `c-in-sws' before (point-min). In this case it's necessary
1979 ;; to clear both properties.
1980 (c-debug-sws-msg
1981 "c-backward-sws clearing thoroughly at %s for cache separation"
1982 last-put-in-sws-pos)
1983 (c-remove-is-and-in-sws last-put-in-sws-pos
1984 (1+ last-put-in-sws-pos)))))
1988 ;; Other whitespace tools
1989 (defun c-partial-ws-p (beg end)
1990 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1991 ;; region? This is a "heuristic" function. .....
1993 ;; The motivation for the second bit is to check whether removing this
1994 ;; region would coalesce two symbols.
1996 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1997 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1998 (save-excursion
1999 (let ((end+1 (min (1+ end) (point-max))))
2000 (or (progn (goto-char (max (point-min) (1- beg)))
2001 (c-skip-ws-forward end)
2002 (eq (point) end))
2003 (progn (goto-char beg)
2004 (c-skip-ws-forward end+1)
2005 (eq (point) end+1))))))
2007 ;; A system for finding noteworthy parens before the point.
2009 (defconst c-state-cache-too-far 5000)
2010 ;; A maximum comfortable scanning distance, e.g. between
2011 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2012 ;; this distance is exceeded, we take "emergency meausures", e.g. by clearing
2013 ;; the cache and starting again from point-min or a beginning of defun. This
2014 ;; value can be tuned for efficiency or set to a lower value for testing.
2016 (defvar c-state-cache nil)
2017 (make-variable-buffer-local 'c-state-cache)
2018 ;; The state cache used by `c-parse-state' to cut down the amount of
2019 ;; searching. It's the result from some earlier `c-parse-state' call. See
2020 ;; `c-parse-state''s doc string for details of its structure.
2022 ;; The use of the cached info is more effective if the next
2023 ;; `c-parse-state' call is on a line close by the one the cached state
2024 ;; was made at; the cache can actually slow down a little if the
2025 ;; cached state was made very far back in the buffer. The cache is
2026 ;; most effective if `c-parse-state' is used on each line while moving
2027 ;; forward.
2029 (defvar c-state-cache-good-pos 1)
2030 (make-variable-buffer-local 'c-state-cache-good-pos)
2031 ;; This is a position where `c-state-cache' is known to be correct, or
2032 ;; nil (see below). It's a position inside one of the recorded unclosed
2033 ;; parens or the top level, but not further nested inside any literal or
2034 ;; subparen that is closed before the last recorded position.
2036 ;; The exact position is chosen to try to be close to yet earlier than
2037 ;; the position where `c-state-cache' will be called next. Right now
2038 ;; the heuristic is to set it to the position after the last found
2039 ;; closing paren (of any type) before the line on which
2040 ;; `c-parse-state' was called. That is chosen primarily to work well
2041 ;; with refontification of the current line.
2043 ;; 2009-07-28: When `c-state-point-min' and the last position where
2044 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2045 ;; both in the same literal, there is no such "good position", and
2046 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2047 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2049 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2050 ;; the middle of the desert, as long as it is not within a brace pair
2051 ;; recorded in `c-state-cache' or a paren/bracket pair.
2054 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2055 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2056 ;; speed up testing for non-literality.
2057 (defconst c-state-nonlit-pos-interval 10000)
2058 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2060 (defvar c-state-nonlit-pos-cache nil)
2061 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2062 ;; A list of buffer positions which are known not to be in a literal or a cpp
2063 ;; construct. This is ordered with higher positions at the front of the list.
2064 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2066 (defvar c-state-nonlit-pos-cache-limit 1)
2067 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2068 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2069 ;; reduced by buffer changes, and increased by invocations of
2070 ;; `c-state-literal-at'.
2072 (defsubst c-state-pp-to-literal (from to)
2073 ;; Do a parse-partial-sexp from FROM to TO, returning the bounds of any
2074 ;; literal at TO as a cons, otherwise NIL.
2075 ;; FROM must not be in a literal, and the buffer should already be wide
2076 ;; enough.
2077 (save-excursion
2078 (let ((s (parse-partial-sexp from to)))
2079 (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
2080 (parse-partial-sexp (point) (point-max)
2081 nil ; TARGETDEPTH
2082 nil ; STOPBEFORE
2083 s ; OLDSTATE
2084 'syntax-table) ; stop at end of literal
2085 (cons (nth 8 s) (point))))))
2087 (defun c-state-literal-at (here)
2088 ;; If position HERE is inside a literal, return (START . END), the
2089 ;; boundaries of the literal (which may be outside the accessible bit of the
2090 ;; buffer). Otherwise, return nil.
2092 ;; This function is almost the same as `c-literal-limits'. It differs in
2093 ;; that it is a lower level function, and that it rigourously follows the
2094 ;; syntax from BOB, whereas `c-literal-limits' uses a "local" safe position.
2096 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2097 ;; MAY NOT contain any positions within macros, since macros are frequently
2098 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2099 ;; We cannot rely on this mechanism whilst determining a cache pos since
2100 ;; this function is also called from outwith `c-parse-state'.
2101 (save-restriction
2102 (widen)
2103 (save-excursion
2104 (let ((c c-state-nonlit-pos-cache)
2105 pos npos lit)
2106 ;; Trim the cache to take account of buffer changes.
2107 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2108 (setq c (cdr c)))
2109 (setq c-state-nonlit-pos-cache c)
2111 (while (and c (> (car c) here))
2112 (setq c (cdr c)))
2113 (setq pos (or (car c) (point-min)))
2115 (while (<= (setq npos (+ pos c-state-nonlit-pos-interval))
2116 here)
2117 (setq lit (c-state-pp-to-literal pos npos))
2118 (setq pos (or (cdr lit) npos)) ; end of literal containing npos.
2119 (goto-char pos)
2120 (when (and (c-beginning-of-macro) (/= (point) pos))
2121 (c-syntactic-end-of-macro)
2122 (or (eobp) (forward-char))
2123 (setq pos (point)))
2124 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2126 (if (> pos c-state-nonlit-pos-cache-limit)
2127 (setq c-state-nonlit-pos-cache-limit pos))
2128 (if (< pos here)
2129 (setq lit (c-state-pp-to-literal pos here)))
2130 lit))))
2132 (defsubst c-state-lit-beg (pos)
2133 ;; Return the start of the literal containing POS, or POS itself.
2134 (or (car (c-state-literal-at pos))
2135 pos))
2137 (defsubst c-state-cache-non-literal-place (pos state)
2138 ;; Return a position outside of a string/comment/macro at or before POS.
2139 ;; STATE is the parse-partial-sexp state at POS.
2140 (let ((res (if (or (nth 3 state) ; in a string?
2141 (nth 4 state)) ; in a comment?
2142 (nth 8 state)
2143 pos)))
2144 (save-excursion
2145 (goto-char res)
2146 (if (c-beginning-of-macro)
2147 (point)
2148 res))))
2150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2151 ;; Stuff to do with point-min, and coping with any literal there.
2152 (defvar c-state-point-min 1)
2153 (make-variable-buffer-local 'c-state-point-min)
2154 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2155 ;; narrowing is likely to affect the parens that are visible before the point.
2157 (defvar c-state-point-min-lit-type nil)
2158 (make-variable-buffer-local 'c-state-point-min-lit-type)
2159 (defvar c-state-point-min-lit-start nil)
2160 (make-variable-buffer-local 'c-state-point-min-lit-start)
2161 ;; These two variables define the literal, if any, containing point-min.
2162 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2163 ;; literal. If there's no literal there, they're both nil.
2165 (defvar c-state-min-scan-pos 1)
2166 (make-variable-buffer-local 'c-state-min-scan-pos)
2167 ;; This is the earliest buffer-pos from which scanning can be done. It is
2168 ;; either the end of the literal containing point-min, or point-min itself.
2169 ;; It becomes nil if the buffer is changed earlier than this point.
2170 (defun c-state-get-min-scan-pos ()
2171 ;; Return the lowest valid scanning pos. This will be the end of the
2172 ;; literal enclosing point-min, or point-min itself.
2173 (or c-state-min-scan-pos
2174 (save-restriction
2175 (save-excursion
2176 (widen)
2177 (goto-char c-state-point-min-lit-start)
2178 (if (eq c-state-point-min-lit-type 'string)
2179 (forward-sexp)
2180 (forward-comment 1))
2181 (setq c-state-min-scan-pos (point))))))
2183 (defun c-state-mark-point-min-literal ()
2184 ;; Determine the properties of any literal containing POINT-MIN, setting the
2185 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2186 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2187 (let ((p-min (point-min))
2188 lit)
2189 (save-restriction
2190 (widen)
2191 (setq lit (c-state-literal-at p-min))
2192 (if lit
2193 (setq c-state-point-min-lit-type
2194 (save-excursion
2195 (goto-char (car lit))
2196 (cond
2197 ((looking-at c-block-comment-start-regexp) 'c)
2198 ((looking-at c-line-comment-starter) 'c++)
2199 (t 'string)))
2200 c-state-point-min-lit-start (car lit)
2201 c-state-min-scan-pos (cdr lit))
2202 (setq c-state-point-min-lit-type nil
2203 c-state-point-min-lit-start nil
2204 c-state-min-scan-pos p-min)))))
2207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2208 ;; A variable which signals a brace dessert - helpful for reducing the number
2209 ;; of fruitless backward scans.
2210 (defvar c-state-brace-pair-desert nil)
2211 (make-variable-buffer-local 'c-state-brace-pair-desert)
2212 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2213 ;; that defun has searched backwards for a brace pair and not found one. Its
2214 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2215 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2216 ;; nil when at top level) and FROM is where the backward search started. It
2217 ;; is reset to nil in `c-invalidate-state-cache'.
2220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2221 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2222 ;; list of like structure.
2223 (defmacro c-state-cache-top-lparen (&optional cache)
2224 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2225 ;; (default `c-state-cache') (or nil).
2226 (let ((cash (or cache 'c-state-cache)))
2227 `(if (consp (car ,cash))
2228 (caar ,cash)
2229 (car ,cash))))
2231 (defmacro c-state-cache-top-paren (&optional cache)
2232 ;; Return the address of the latest brace/bracket/paren (whether left or
2233 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2234 (let ((cash (or cache 'c-state-cache)))
2235 `(if (consp (car ,cash))
2236 (cdar ,cash)
2237 (car ,cash))))
2239 (defmacro c-state-cache-after-top-paren (&optional cache)
2240 ;; Return the position just after the latest brace/bracket/paren (whether
2241 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2242 (let ((cash (or cache 'c-state-cache)))
2243 `(if (consp (car ,cash))
2244 (cdar ,cash)
2245 (and (car ,cash)
2246 (1+ (car ,cash))))))
2248 (defun c-get-cache-scan-pos (here)
2249 ;; From the state-cache, determine the buffer position from which we might
2250 ;; scan forward to HERE to update this cache. This position will be just
2251 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2252 ;; return the earliest position in the accessible region which isn't within
2253 ;; a literal. If the visible portion of the buffer is entirely within a
2254 ;; literal, return NIL.
2255 (let ((c c-state-cache) elt)
2256 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2257 (while (and c
2258 (>= (c-state-cache-top-lparen c) here))
2259 (setq c (cdr c)))
2261 (setq elt (car c))
2262 (cond
2263 ((consp elt)
2264 (if (> (cdr elt) here)
2265 (1+ (car elt))
2266 (cdr elt)))
2267 (elt (1+ elt))
2268 ((<= (c-state-get-min-scan-pos) here)
2269 (c-state-get-min-scan-pos))
2270 (t nil))))
2272 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2273 ;; Variables which keep track of preprocessor constructs.
2274 (defvar c-state-old-cpp-beg nil)
2275 (make-variable-buffer-local 'c-state-old-cpp-beg)
2276 (defvar c-state-old-cpp-end nil)
2277 (make-variable-buffer-local 'c-state-old-cpp-end)
2278 ;; These are the limits of the macro containing point at the previous call of
2279 ;; `c-parse-state', or nil.
2281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2282 ;; Defuns which analyse the buffer, yet don't change `c-state-cache'.
2283 (defun c-get-fallback-scan-pos (here)
2284 ;; Return a start position for building `c-state-cache' from
2285 ;; scratch. This will be at the top level, 2 defuns back.
2286 (save-excursion
2287 ;; Go back 2 bods, but ignore any bogus positions returned by
2288 ;; beginning-of-defun (i.e. open paren in column zero).
2289 (goto-char here)
2290 (let ((cnt 2))
2291 (while (not (or (bobp) (zerop cnt)))
2292 (c-beginning-of-defun-1) ; Pure elisp BOD.
2293 (if (eq (char-after) ?\{)
2294 (setq cnt (1- cnt)))))
2295 (point)))
2297 (defun c-state-balance-parens-backwards (here- here+ top)
2298 ;; Return the position of the opening paren/brace/bracket before HERE- which
2299 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2300 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2302 ;; ............................................
2303 ;; | |
2304 ;; ( [ ( .........#macro.. ) ( ) ] )
2305 ;; ^ ^ ^ ^
2306 ;; | | | |
2307 ;; return HERE- HERE+ TOP
2309 ;; If there aren't enough opening paren/brace/brackets, return the position
2310 ;; of the outermost one found, or HERE- if there are none. If there are no
2311 ;; closeing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2312 ;; must not be inside literals. Only the accessible portion of the buffer
2313 ;; will be scanned.
2315 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2316 ;; `here'. Go round the next loop each time we pass over such a ")". These
2317 ;; probably match "("s before `here-'.
2318 (let (pos pa ren+1 lonely-rens)
2319 (save-excursion
2320 (save-restriction
2321 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2322 (setq pos here+)
2323 (c-safe
2324 (while
2325 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2326 (setq lonely-rens (cons ren+1 lonely-rens)
2327 pos ren+1)))))
2329 ;; PART 2: Scan back before `here-' searching for the "("s
2330 ;; matching/mismatching the ")"s found above. We only need to direct the
2331 ;; caller to scan when we've encountered unmatched right parens.
2332 (setq pos here-)
2333 (when lonely-rens
2334 (c-safe
2335 (while
2336 (and lonely-rens ; actual values aren't used.
2337 (setq pa (scan-lists pos -1 1)))
2338 (setq pos pa)
2339 (setq lonely-rens (cdr lonely-rens)))))
2340 pos))
2342 (defun c-parse-state-get-strategy (here good-pos)
2343 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2344 ;; to minimise the amount of scanning. HERE is the pertinent position in
2345 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2346 ;; its head trimmed) is known to be good, or nil if there is no such
2347 ;; position.
2349 ;; The return value is a list, one of the following:
2351 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2352 ;; which is not less than CACHE-POS.
2353 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2354 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2355 ;; top level.
2356 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2357 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2358 ;; or below HERE.
2359 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2360 BOD-pos ; position of 2nd BOD before HERE.
2361 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2362 start-point
2363 how-far) ; putative scanning distance.
2364 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2365 (cond
2366 ((< here (c-state-get-min-scan-pos))
2367 (setq strategy 'IN-LIT
2368 start-point nil
2369 cache-pos nil
2370 how-far 0))
2371 ((<= good-pos here)
2372 (setq strategy 'forward
2373 start-point (max good-pos cache-pos)
2374 how-far (- here start-point)))
2375 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2376 (setq strategy 'backward
2377 how-far (- good-pos here)))
2379 (setq strategy 'forward
2380 how-far (- here cache-pos)
2381 start-point cache-pos)))
2383 ;; Might we be better off starting from the top level, two defuns back,
2384 ;; instead?
2385 (when (> how-far c-state-cache-too-far)
2386 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2387 (if (< (- here BOD-pos) how-far)
2388 (setq strategy 'BOD
2389 start-point BOD-pos)))
2391 (list
2392 strategy
2393 (and (memq strategy '(forward backward)) cache-pos)
2394 (and (memq strategy '(forward BOD)) start-point))))
2397 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2398 ;; Routines which change `c-state-cache' and associated values.
2399 (defun c-renarrow-state-cache ()
2400 ;; The region (more precisely, point-min) has changed since we
2401 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2402 (if (< (point-min) c-state-point-min)
2403 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2404 ;; It would be possible to do a better job here and recalculate the top
2405 ;; only.
2406 (progn
2407 (c-state-mark-point-min-literal)
2408 (setq c-state-cache nil
2409 c-state-cache-good-pos c-state-min-scan-pos
2410 c-state-brace-pair-desert nil))
2412 ;; point-min has MOVED FORWARD.
2414 ;; Is the new point-min inside a (different) literal?
2415 (unless (and c-state-point-min-lit-start ; at prev. point-min
2416 (< (point-min) (c-state-get-min-scan-pos)))
2417 (c-state-mark-point-min-literal))
2419 ;; Cut off a bit of the tail from `c-state-cache'.
2420 (let ((ptr (cons nil c-state-cache))
2422 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2423 (>= pa (point-min)))
2424 (setq ptr (cdr ptr)))
2426 (when (consp ptr)
2427 (if (eq (cdr ptr) c-state-cache)
2428 (setq c-state-cache nil
2429 c-state-cache-good-pos c-state-min-scan-pos)
2430 (setcdr ptr nil)
2431 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2434 (setq c-state-point-min (point-min)))
2436 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2437 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2438 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2439 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2440 ;; the highest brace pair whose "}" is below UPPER-LIM.
2442 ;; Return non-nil when this has been done.
2444 ;; This routine should be fast. Since it can get called a LOT, we maintain
2445 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2446 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2447 (save-excursion
2448 (save-restriction
2449 (let ((bra from) ce ; Positions of "{" and "}".
2450 new-cons
2451 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2452 (macro-start-or-from
2453 (progn (goto-char from)
2454 (c-beginning-of-macro)
2455 (point))))
2456 (or upper-lim (setq upper-lim from))
2458 ;; If we're essentially repeating a fruitless search, just give up.
2459 (unless (and c-state-brace-pair-desert
2460 (eq cache-pos (car c-state-brace-pair-desert))
2461 (<= from (cdr c-state-brace-pair-desert)))
2462 ;; Only search what we absolutely need to:
2463 (if (and c-state-brace-pair-desert
2464 (eq cache-pos (car c-state-brace-pair-desert)))
2465 (narrow-to-region (cdr c-state-brace-pair-desert) (point-max)))
2467 ;; In the next pair of nested loops, the inner one moves back past a
2468 ;; pair of (mis-)matching parens or brackets; the outer one moves
2469 ;; back over a sequence of unmatched close brace/paren/bracket each
2470 ;; time round.
2471 (while
2472 (progn
2473 (c-safe
2474 (while
2475 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2476 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2477 (or (> ce upper-lim)
2478 (not (eq (char-after bra) ?\{))
2479 (and (goto-char bra)
2480 (c-beginning-of-macro)
2481 (< (point) macro-start-or-from))))))
2482 (and ce (< ce bra)))
2483 (setq bra ce)) ; If we just backed over an unbalanced closing
2484 ; brace, ignore it.
2486 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2487 ;; We've found the desired brace-pair.
2488 (progn
2489 (setq new-cons (cons bra (1+ ce)))
2490 (cond
2491 ((consp (car c-state-cache))
2492 (setcar c-state-cache new-cons))
2493 ((and (numberp (car c-state-cache)) ; probably never happens
2494 (< ce (car c-state-cache)))
2495 (setcdr c-state-cache
2496 (cons new-cons (cdr c-state-cache))))
2497 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2499 ;; We haven't found a brace pair. Record this.
2500 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2502 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2503 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2504 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2505 ;; "push" "a" brace pair onto `c-state-cache'.
2507 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2508 ;; otherwise push it normally.
2510 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2511 ;; latter is inside a macro, not being a macro containing
2512 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2513 ;; base pair. This latter case is assumed to be rare.
2515 ;; Note: POINT is not preserved in this routine.
2516 (if bra+1
2517 (if (or (> bra+1 macro-start-or-here)
2518 (progn (goto-char bra+1)
2519 (not (c-beginning-of-macro))))
2520 (setq c-state-cache
2521 (cons (cons (1- bra+1)
2522 (scan-lists bra+1 1 1))
2523 (if (consp (car c-state-cache))
2524 (cdr c-state-cache)
2525 c-state-cache)))
2526 ;; N.B. This defsubst codes one method for the simple, normal case,
2527 ;; and a more sophisticated, slower way for the general case. Don't
2528 ;; eliminate this defsubst - it's a speed optimisation.
2529 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2531 (defun c-append-to-state-cache (from)
2532 ;; Scan the buffer from FROM to (point-max), adding elements into
2533 ;; `c-state-cache' for braces etc. Return a candidate for
2534 ;; `c-state-cache-good-pos'.
2536 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2537 ;; any. Typically, it is immediately after it. It must not be inside a
2538 ;; literal.
2539 (let ((here-bol (c-point 'bol (point-max)))
2540 (macro-start-or-here
2541 (save-excursion (goto-char (point-max))
2542 (if (c-beginning-of-macro)
2543 (point)
2544 (point-max))))
2545 pa+1 ; pos just after an opening PAren (or brace).
2546 (ren+1 from) ; usually a pos just after an closing paREN etc.
2547 ; Is actually the pos. to scan for a (/{/[ from,
2548 ; which sometimes is after a silly )/}/].
2549 paren+1 ; Pos after some opening or closing paren.
2550 paren+1s ; A list of `paren+1's; used to determine a
2551 ; good-pos.
2552 bra+1 ce+1 ; just after L/R bra-ces.
2553 bra+1s ; list of OLD values of bra+1.
2554 mstart) ; start of a macro.
2556 (save-excursion
2557 ;; Each time round the following loop, we enter a succesively deeper
2558 ;; level of brace/paren nesting. (Except sometimes we "continue at
2559 ;; the existing level".) `pa+1' is a pos inside an opening
2560 ;; brace/paren/bracket, usually just after it.
2561 (while
2562 (progn
2563 ;; Each time round the next loop moves forward over an opening then
2564 ;; a closing brace/bracket/paren. This loop is white hot, so it
2565 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2566 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2567 ;; call of `scan-lists' signals an error, which happens when there
2568 ;; are no more b/b/p's to scan.
2569 (c-safe
2570 (while t
2571 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2572 paren+1s (cons pa+1 paren+1s))
2573 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2574 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2575 (setq bra+1 pa+1))
2576 (setcar paren+1s ren+1)))
2578 (if (and pa+1 (> pa+1 ren+1))
2579 ;; We've just entered a deeper nesting level.
2580 (progn
2581 ;; Insert the brace pair (if present) and the single open
2582 ;; paren/brace/bracket into `c-state-cache' It cannot be
2583 ;; inside a macro, except one around point, because of what
2584 ;; `c-neutralize-syntax-in-CPP' has done.
2585 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2586 ;; Insert the opening brace/bracket/paren position.
2587 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2588 ;; Clear admin stuff for the next more nested part of the scan.
2589 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2590 t) ; Carry on the loop
2592 ;; All open p/b/b's at this nesting level, if any, have probably
2593 ;; been closed by matching/mismatching ones. We're probably
2594 ;; finished - we just need to check for having found an
2595 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2596 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2597 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2599 ;; Record the final, innermost, brace-pair if there is one.
2600 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2602 ;; Determine a good pos
2603 (while (and (setq paren+1 (car paren+1s))
2604 (> (if (> paren+1 macro-start-or-here)
2605 paren+1
2606 (goto-char paren+1)
2607 (setq mstart (and (c-beginning-of-macro)
2608 (point)))
2609 (or mstart paren+1))
2610 here-bol))
2611 (setq paren+1s (cdr paren+1s)))
2612 (cond
2613 ((and paren+1 mstart)
2614 (min paren+1 mstart))
2615 (paren+1)
2616 (t from)))))
2618 (defun c-remove-stale-state-cache (good-pos pps-point)
2619 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2620 ;; not be in it when it is amended for position (point-max).
2621 ;; Additionally, the "outermost" open-brace entry before (point-max)
2622 ;; will be converted to a cons if the matching close-brace is scanned.
2624 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2625 ;; parens/braces/brackets between GOOD-POS and (point-max).
2627 ;; As a second thing, calculate the result of parse-partial-sexp at
2628 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2629 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2630 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2631 ;; needs to be FAST).
2633 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2634 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2635 ;; to be good (we aim for this to be as high as possible);
2636 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2637 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2638 ;; position to scan backwards from.
2639 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2640 (save-restriction
2641 (narrow-to-region 1 (point-max))
2642 (save-excursion
2643 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2644 (save-excursion
2645 (goto-char (point-max))
2646 (and (c-beginning-of-macro)
2647 (point))))
2648 (good-pos-actual-macro-start ; Start of macro containing good-pos
2649 ; or nil
2650 (and (< good-pos (point-max))
2651 (save-excursion
2652 (goto-char good-pos)
2653 (and (c-beginning-of-macro)
2654 (point)))))
2655 (good-pos-actual-macro-end ; End of this macro, (maybe
2656 ; (point-max)), or nil.
2657 (and good-pos-actual-macro-start
2658 (save-excursion
2659 (goto-char good-pos-actual-macro-start)
2660 (c-end-of-macro)
2661 (point))))
2662 pps-state ; Will be 9 or 10 elements long.
2664 upper-lim ; ,beyond which `c-state-cache' entries are removed
2665 scan-back-pos
2666 pair-beg pps-point-state target-depth)
2668 ;; Remove entries beyond (point-max). Also remove any entries inside
2669 ;; a macro, unless (point-max) is in the same macro.
2670 (setq upper-lim
2671 (if (or (null c-state-old-cpp-beg)
2672 (and (> (point-max) c-state-old-cpp-beg)
2673 (< (point-max) c-state-old-cpp-end)))
2674 (point-max)
2675 (min (point-max) c-state-old-cpp-beg)))
2676 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2677 (setq c-state-cache (cdr c-state-cache)))
2678 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2679 ;; RBrace and indicate we'll need to search backwards for a previous
2680 ;; brace pair.
2681 (when (and c-state-cache
2682 (consp (car c-state-cache))
2683 (> (cdar c-state-cache) upper-lim))
2684 (setcar c-state-cache (caar c-state-cache))
2685 (setq scan-back-pos (car c-state-cache)))
2687 ;; The next loop jumps forward out of a nested level of parens each
2688 ;; time round; the corresponding elements in `c-state-cache' are
2689 ;; removed. `pos' is just after the brace-pair or the open paren at
2690 ;; (car c-state-cache). There can be no open parens/braces/brackets
2691 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2692 ;; due to the interface spec to this function.
2693 (setq pos (if (and good-pos-actual-macro-end
2694 (not (eq good-pos-actual-macro-start
2695 in-macro-start)))
2696 (1+ good-pos-actual-macro-end) ; get outside the macro as
2697 ; marked by a `category' text property.
2698 good-pos))
2699 (goto-char pos)
2700 (while (and c-state-cache
2701 (< (point) (point-max)))
2702 (cond
2703 ((null pps-state) ; first time through
2704 (setq target-depth -1))
2705 ((eq (car pps-state) target-depth) ; found closing ),},]
2706 (setq target-depth (1- (car pps-state))))
2707 ;; Do nothing when we've merely reached pps-point.
2710 ;; Scan!
2711 (setq pps-state
2712 (parse-partial-sexp
2713 (point) (if (< (point) pps-point) pps-point (point-max))
2714 target-depth
2715 nil pps-state))
2717 (if (= (point) pps-point)
2718 (setq pps-point-state pps-state))
2720 (when (eq (car pps-state) target-depth)
2721 (setq pos (point)) ; POS is now just after an R-paren/brace.
2722 (cond
2723 ((and (consp (car c-state-cache))
2724 (eq (point) (cdar c-state-cache)))
2725 ;; We've just moved out of the paren pair containing the brace-pair
2726 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2727 ;; and is potentially where the open brace of a cons in
2728 ;; c-state-cache will be.
2729 (setq pair-beg (car-safe (cdr c-state-cache))
2730 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2731 ((numberp (car c-state-cache))
2732 (setq pair-beg (car c-state-cache)
2733 c-state-cache (cdr c-state-cache))) ; remove this
2734 ; containing Lparen
2735 ((numberp (cadr c-state-cache))
2736 (setq pair-beg (cadr c-state-cache)
2737 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2738 ; together with enclosed brace pair.
2739 ;; (t nil) ; Ignore an unmated Rparen.
2742 (if (< (point) pps-point)
2743 (setq pps-state (parse-partial-sexp (point) pps-point
2744 nil nil ; TARGETDEPTH, STOPBEFORE
2745 pps-state)))
2747 ;; If the last paren pair we moved out of was actually a brace pair,
2748 ;; insert it into `c-state-cache'.
2749 (when (and pair-beg (eq (char-after pair-beg) ?{))
2750 (if (consp (car-safe c-state-cache))
2751 (setq c-state-cache (cdr c-state-cache)))
2752 (setq c-state-cache (cons (cons pair-beg pos)
2753 c-state-cache)))
2755 (list pos scan-back-pos pps-state)))))
2757 (defun c-remove-stale-state-cache-backwards (here cache-pos)
2758 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2759 ;; buffer, and inform the caller of the scenario detected.
2761 ;; HERE is the position we're setting `c-state-cache' for.
2762 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2763 ;; before HERE, or a position at or near point-min which isn't in a
2764 ;; literal.
2766 ;; This function must only be called only when (> `c-state-cache-good-pos'
2767 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2768 ;; optimised to eliminate (or minimise) scanning between these two
2769 ;; positions.
2771 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2772 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2773 ;; could become so after missing elements are inserted into
2774 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2775 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2776 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2777 ;; before `here''s line, or the start of the literal containing it.
2778 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2779 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2780 ;; to scan backwards from.
2781 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2782 ;; POS and HERE which aren't recorded in `c-state-cache'.
2784 ;; The comments in this defun use "paren" to mean parenthesis or square
2785 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2787 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2788 ;; | | | | | |
2789 ;; CP E here D C good
2790 (let ((pos c-state-cache-good-pos)
2791 pa ren ; positions of "(" and ")"
2792 dropped-cons ; whether the last element dropped from `c-state-cache'
2793 ; was a cons (representing a brace-pair)
2794 good-pos ; see above.
2795 lit ; (START . END) of a literal containing some point.
2796 here-lit-start here-lit-end ; bounds of literal containing `here'
2797 ; or `here' itself.
2798 here- here+ ; start/end of macro around HERE, or HERE
2799 (here-bol (c-point 'bol here))
2800 (too-far-back (max (- here c-state-cache-too-far) 1)))
2802 ;; Remove completely irrelevant entries from `c-state-cache'.
2803 (while (and c-state-cache
2804 (>= (setq pa (c-state-cache-top-lparen)) here))
2805 (setq dropped-cons (consp (car c-state-cache)))
2806 (setq c-state-cache (cdr c-state-cache))
2807 (setq pos pa))
2808 ;; At this stage, (> pos here);
2809 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2811 (cond
2812 ((and (consp (car c-state-cache))
2813 (> (cdar c-state-cache) here))
2814 ;; CASE 1: The top of the cache is a brace pair which now encloses
2815 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2816 ;; knowledge of what's inside these braces, we have no alternative but
2817 ;; to direct the caller to scan the buffer from the opening brace.
2818 (setq pos (caar c-state-cache))
2819 (setcar c-state-cache pos)
2820 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
2821 ; entry into a { entry, so the caller needs to
2822 ; search for a brace pair before the {.
2824 ;; `here' might be inside a literal. Check for this.
2825 ((progn
2826 (setq lit (c-state-literal-at here)
2827 here-lit-start (or (car lit) here)
2828 here-lit-end (or (cdr lit) here))
2829 ;; Has `here' just "newly entered" a macro?
2830 (save-excursion
2831 (goto-char here-lit-start)
2832 (if (and (c-beginning-of-macro)
2833 (or (null c-state-old-cpp-beg)
2834 (not (= (point) c-state-old-cpp-beg))))
2835 (progn
2836 (setq here- (point))
2837 (c-end-of-macro)
2838 (setq here+ (point)))
2839 (setq here- here-lit-start
2840 here+ here-lit-end)))
2842 ;; `here' might be nested inside any depth of parens (or brackets but
2843 ;; not braces). Scan backwards to find the outermost such opening
2844 ;; paren, if there is one. This will be the scan position to return.
2845 (save-restriction
2846 (narrow-to-region cache-pos (point-max))
2847 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
2848 nil)) ; for the cond
2850 ((< pos here-lit-start)
2851 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2852 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2853 ;; a brace pair preceding this, it will already be in `c-state-cache',
2854 ;; unless there was a brace pair after it, i.e. there'll only be one to
2855 ;; scan for if we've just deleted one.
2856 (list pos (and dropped-cons pos) t)) ; Return value.
2858 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2859 ;; Further forward scanning isn't needed, but we still need to find a
2860 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2861 ((progn
2862 (save-restriction
2863 (narrow-to-region here-bol (point-max))
2864 (setq pos here-lit-start)
2865 (c-safe (while (setq pa (scan-lists pos -1 1))
2866 (setq pos pa)))) ; might signal
2867 nil)) ; for the cond
2869 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
2870 ;; CASE 3: After a }/)/] before `here''s BOL.
2871 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
2874 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2875 ;; literal containing it.
2876 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
2877 (list good-pos (and dropped-cons good-pos) nil)))))
2880 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2881 ;; Externally visible routines.
2883 (defun c-state-cache-init ()
2884 (setq c-state-cache nil
2885 c-state-cache-good-pos 1
2886 c-state-nonlit-pos-cache nil
2887 c-state-nonlit-pos-cache-limit 1
2888 c-state-brace-pair-desert nil
2889 c-state-point-min 1
2890 c-state-point-min-lit-type nil
2891 c-state-point-min-lit-start nil
2892 c-state-min-scan-pos 1
2893 c-state-old-cpp-beg nil
2894 c-state-old-cpp-end nil)
2895 (c-state-mark-point-min-literal))
2897 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2898 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
2899 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
2900 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
2901 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
2902 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
2903 ;; (defun c-state-dump ()
2904 ;; ;; For debugging.
2905 ;; ;(message
2906 ;; (concat
2907 ;; (c-sc-qde c-state-cache)
2908 ;; (c-sc-de c-state-cache-good-pos)
2909 ;; (c-sc-qde c-state-nonlit-pos-cache)
2910 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
2911 ;; (c-sc-qde c-state-brace-pair-desert)
2912 ;; (c-sc-de c-state-point-min)
2913 ;; (c-sc-de c-state-point-min-lit-type)
2914 ;; (c-sc-de c-state-point-min-lit-start)
2915 ;; (c-sc-de c-state-min-scan-pos)
2916 ;; (c-sc-de c-state-old-cpp-beg)
2917 ;; (c-sc-de c-state-old-cpp-end)))
2918 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2920 (defun c-invalidate-state-cache-1 (here)
2921 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2922 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2923 ;; left in a consistent state.
2925 ;; This is much like `c-whack-state-after', but it never changes a paren
2926 ;; pair element into an open paren element. Doing that would mean that the
2927 ;; new open paren wouldn't have the required preceding paren pair element.
2929 ;; This function is called from c-after-change.
2931 ;; The cache of non-literals:
2932 (if (< here c-state-nonlit-pos-cache-limit)
2933 (setq c-state-nonlit-pos-cache-limit here))
2935 ;; `c-state-cache':
2936 ;; Case 1: if `here' is in a literal containing point-min, everything
2937 ;; becomes (or is already) nil.
2938 (if (or (null c-state-cache-good-pos)
2939 (< here (c-state-get-min-scan-pos)))
2940 (setq c-state-cache nil
2941 c-state-cache-good-pos nil
2942 c-state-min-scan-pos nil)
2944 ;;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value below
2945 ;;; `here'. To maintain its consistency, we may need to insert a new brace
2946 ;;; pair.
2947 (let ((here-bol (c-point 'bol here))
2948 too-high-pa ; recorded {/(/[ next above here, or nil.
2949 dropped-cons ; was the last removed element a brace pair?
2951 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
2952 (while (and c-state-cache
2953 (>= (setq pa (c-state-cache-top-paren)) here))
2954 (setq dropped-cons (consp (car c-state-cache))
2955 too-high-pa (c-state-cache-top-lparen)
2956 c-state-cache (cdr c-state-cache)))
2958 ;; Do we need to add in an earlier brace pair, having lopped one off?
2959 (if (and dropped-cons
2960 (< too-high-pa (+ here c-state-cache-too-far)))
2961 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
2962 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
2963 (c-state-get-min-scan-pos)))))
2965 ;; The brace-pair desert marker:
2966 (when (car c-state-brace-pair-desert)
2967 (if (< here (car c-state-brace-pair-desert))
2968 (setq c-state-brace-pair-desert nil)
2969 (if (< here (cdr c-state-brace-pair-desert))
2970 (setcdr c-state-brace-pair-desert here)))))
2972 (defun c-parse-state-1 ()
2973 ;; Find and record all noteworthy parens between some good point earlier in
2974 ;; the file and point. That good point is at least the beginning of the
2975 ;; top-level construct we are in, or the beginning of the preceding
2976 ;; top-level construct if we aren't in one.
2978 ;; The returned value is a list of the noteworthy parens with the last one
2979 ;; first. If an element in the list is an integer, it's the position of an
2980 ;; open paren (of any type) which has not been closed before the point. If
2981 ;; an element is a cons, it gives the position of a closed BRACE paren
2982 ;; pair[*]; the car is the start brace position and the cdr is the position
2983 ;; following the closing brace. Only the last closed brace paren pair
2984 ;; before each open paren and before the point is recorded, and thus the
2985 ;; state never contains two cons elements in succession. When a close brace
2986 ;; has no matching open brace (e.g., the matching brace is outside the
2987 ;; visible region), it is not represented in the returned value.
2989 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
2990 ;; This defun explicitly treats mismatching parens/braces/brackets as
2991 ;; matching. It is the open brace which makes it a "brace" pair.
2993 ;; If POINT is within a macro, open parens and brace pairs within
2994 ;; THIS macro MIGHT be recorded. This depends on whether their
2995 ;; syntactic properties have been suppressed by
2996 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
2998 ;; Currently no characters which are given paren syntax with the
2999 ;; syntax-table property are recorded, i.e. angle bracket arglist
3000 ;; parens are never present here. Note that this might change.
3002 ;; BUG: This function doesn't cope entirely well with unbalanced
3003 ;; parens in macros. (2008-12-11: this has probably been resolved
3004 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3005 ;; following case the brace before the macro isn't balanced with the
3006 ;; one after it:
3008 ;; {
3009 ;; #define X {
3010 ;; }
3012 ;; Note to maintainers: this function DOES get called with point
3013 ;; within comments and strings, so don't assume it doesn't!
3015 ;; This function might do hidden buffer changes.
3016 (let* ((here (point))
3017 (here-bopl (c-point 'bopl))
3018 strategy ; 'forward, 'backward etc..
3019 ;; Candidate positions to start scanning from:
3020 cache-pos ; highest position below HERE already existing in
3021 ; cache (or 1).
3022 good-pos
3023 start-point
3024 bopl-state
3026 scan-backward-pos scan-forward-p) ; used for 'backward.
3027 ;; If POINT-MIN has changed, adjust the cache
3028 (unless (= (point-min) c-state-point-min)
3029 (c-renarrow-state-cache))
3031 ;; Strategy?
3032 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3033 strategy (car res)
3034 cache-pos (cadr res)
3035 start-point (nth 2 res))
3037 (when (eq strategy 'BOD)
3038 (setq c-state-cache nil
3039 c-state-cache-good-pos start-point))
3041 ;; SCAN!
3042 (save-restriction
3043 (cond
3044 ((memq strategy '(forward BOD))
3045 (narrow-to-region (point-min) here)
3046 (setq res (c-remove-stale-state-cache start-point here-bopl))
3047 (setq cache-pos (car res)
3048 scan-backward-pos (cadr res)
3049 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
3050 ; start-point)
3051 (if scan-backward-pos
3052 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
3053 (setq good-pos
3054 (c-append-to-state-cache cache-pos))
3055 (setq c-state-cache-good-pos
3056 (if (and bopl-state
3057 (< good-pos (- here c-state-cache-too-far)))
3058 (c-state-cache-non-literal-place here-bopl bopl-state)
3059 good-pos)))
3061 ((eq strategy 'backward)
3062 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
3063 good-pos (car res)
3064 scan-backward-pos (cadr res)
3065 scan-forward-p (car (cddr res)))
3066 (if scan-backward-pos
3067 (c-append-lower-brace-pair-to-state-cache
3068 scan-backward-pos))
3069 (setq c-state-cache-good-pos
3070 (if scan-forward-p
3071 (progn (narrow-to-region (point-min) here)
3072 (c-append-to-state-cache good-pos))
3074 (c-get-cache-scan-pos good-pos))))
3076 (t ; (eq strategy 'IN-LIT)
3077 (setq c-state-cache nil
3078 c-state-cache-good-pos nil)))))
3080 c-state-cache)
3082 (defun c-invalidate-state-cache (here)
3083 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3085 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3086 ;; of all parens in preprocessor constructs, except for any such construct
3087 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3088 ;; worrying further about macros and template delimiters.
3089 (c-with-<->-as-parens-suppressed
3090 (if (and c-state-old-cpp-beg
3091 (< c-state-old-cpp-beg here))
3092 (c-with-all-but-one-cpps-commented-out
3093 c-state-old-cpp-beg
3094 (min c-state-old-cpp-end here)
3095 (c-invalidate-state-cache-1 here))
3096 (c-with-cpps-commented-out
3097 (c-invalidate-state-cache-1 here)))))
3099 (defun c-parse-state ()
3100 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3101 ;; description of the functionality and return value.
3103 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3104 ;; of all parens in preprocessor constructs, except for any such construct
3105 ;; containing point. We can then call `c-parse-state-1' without worrying
3106 ;; further about macros and template delimiters.
3107 (let (here-cpp-beg here-cpp-end)
3108 (save-excursion
3109 (when (c-beginning-of-macro)
3110 (setq here-cpp-beg (point))
3111 (unless
3112 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3113 here-cpp-beg)
3114 (setq here-cpp-beg nil here-cpp-end nil))))
3115 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3116 ;; subsystem.
3117 (prog1
3118 (c-with-<->-as-parens-suppressed
3119 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3120 (c-with-all-but-one-cpps-commented-out
3121 here-cpp-beg here-cpp-end
3122 (c-parse-state-1))
3123 (c-with-cpps-commented-out
3124 (c-parse-state-1))))
3125 (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
3126 c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
3129 ;; Debug tool to catch cache inconsistencies. This is called from
3130 ;; 000tests.el.
3131 (defvar c-debug-parse-state nil)
3132 (unless (fboundp 'c-real-parse-state)
3133 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3134 (cc-bytecomp-defun c-real-parse-state)
3136 (defvar c-parse-state-state nil)
3137 (defun c-record-parse-state-state ()
3138 (setq c-parse-state-state
3139 (mapcar
3140 (lambda (arg)
3141 (cons arg (symbol-value arg)))
3142 '(c-state-cache
3143 c-state-cache-good-pos
3144 c-state-nonlit-pos-cache
3145 c-state-nonlit-pos-cache-limit
3146 c-state-brace-pair-desert
3147 c-state-point-min
3148 c-state-point-min-lit-type
3149 c-state-point-min-lit-start
3150 c-state-min-scan-pos
3151 c-state-old-cpp-beg
3152 c-state-old-cpp-end))))
3153 (defun c-replay-parse-state-state ()
3154 (message
3155 (concat "(setq "
3156 (mapconcat
3157 (lambda (arg)
3158 (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
3159 c-parse-state-state " ")
3160 ")")))
3162 (defun c-debug-parse-state ()
3163 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3164 (let ((c-state-cache nil)
3165 (c-state-cache-good-pos 1)
3166 (c-state-nonlit-pos-cache nil)
3167 (c-state-nonlit-pos-cache-limit 1)
3168 (c-state-brace-pair-desert nil)
3169 (c-state-point-min 1)
3170 (c-state-point-min-lit-type nil)
3171 (c-state-point-min-lit-start nil)
3172 (c-state-min-scan-pos 1)
3173 (c-state-old-cpp-beg nil)
3174 (c-state-old-cpp-end nil))
3175 (setq res2 (c-real-parse-state)))
3176 (unless (equal res1 res2)
3177 ;; The cache can actually go further back due to the ad-hoc way
3178 ;; the first paren is found, so try to whack off a bit of its
3179 ;; start before complaining.
3180 ;; (save-excursion
3181 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3182 ;; (c-beginning-of-defun-1)
3183 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3184 ;; (c-beginning-of-defun-1))
3185 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3186 ;; (message (concat "c-parse-state inconsistency at %s: "
3187 ;; "using cache: %s, from scratch: %s")
3188 ;; here res1 res2)))
3189 (message (concat "c-parse-state inconsistency at %s: "
3190 "using cache: %s, from scratch: %s")
3191 here res1 res2)
3192 (message "Old state:")
3193 (c-replay-parse-state-state))
3194 (c-record-parse-state-state)
3195 res1))
3197 (defun c-toggle-parse-state-debug (&optional arg)
3198 (interactive "P")
3199 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3200 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3201 'c-debug-parse-state
3202 'c-real-parse-state)))
3203 (c-keep-region-active))
3204 (when c-debug-parse-state
3205 (c-toggle-parse-state-debug 1))
3208 (defun c-whack-state-before (bufpos paren-state)
3209 ;; Whack off any state information from PAREN-STATE which lies
3210 ;; before BUFPOS. Not destructive on PAREN-STATE.
3211 (let* ((newstate (list nil))
3212 (ptr newstate)
3213 car)
3214 (while paren-state
3215 (setq car (car paren-state)
3216 paren-state (cdr paren-state))
3217 (if (< (if (consp car) (car car) car) bufpos)
3218 (setq paren-state nil)
3219 (setcdr ptr (list car))
3220 (setq ptr (cdr ptr))))
3221 (cdr newstate)))
3223 (defun c-whack-state-after (bufpos paren-state)
3224 ;; Whack off any state information from PAREN-STATE which lies at or
3225 ;; after BUFPOS. Not destructive on PAREN-STATE.
3226 (catch 'done
3227 (while paren-state
3228 (let ((car (car paren-state)))
3229 (if (consp car)
3230 ;; just check the car, because in a balanced brace
3231 ;; expression, it must be impossible for the corresponding
3232 ;; close brace to be before point, but the open brace to
3233 ;; be after.
3234 (if (<= bufpos (car car))
3235 nil ; whack it off
3236 (if (< bufpos (cdr car))
3237 ;; its possible that the open brace is before
3238 ;; bufpos, but the close brace is after. In that
3239 ;; case, convert this to a non-cons element. The
3240 ;; rest of the state is before bufpos, so we're
3241 ;; done.
3242 (throw 'done (cons (car car) (cdr paren-state)))
3243 ;; we know that both the open and close braces are
3244 ;; before bufpos, so we also know that everything else
3245 ;; on state is before bufpos.
3246 (throw 'done paren-state)))
3247 (if (<= bufpos car)
3248 nil ; whack it off
3249 ;; it's before bufpos, so everything else should too.
3250 (throw 'done paren-state)))
3251 (setq paren-state (cdr paren-state)))
3252 nil)))
3254 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3255 ;; Return the bufpos of the innermost enclosing open paren before
3256 ;; bufpos, or nil if none was found.
3257 (let (enclosingp)
3258 (or bufpos (setq bufpos 134217727))
3259 (while paren-state
3260 (setq enclosingp (car paren-state)
3261 paren-state (cdr paren-state))
3262 (if (or (consp enclosingp)
3263 (>= enclosingp bufpos))
3264 (setq enclosingp nil)
3265 (setq paren-state nil)))
3266 enclosingp))
3268 (defun c-least-enclosing-brace (paren-state)
3269 ;; Return the bufpos of the outermost enclosing open paren, or nil
3270 ;; if none was found.
3271 (let (pos elem)
3272 (while paren-state
3273 (setq elem (car paren-state)
3274 paren-state (cdr paren-state))
3275 (if (integerp elem)
3276 (setq pos elem)))
3277 pos))
3279 (defun c-safe-position (bufpos paren-state)
3280 ;; Return the closest "safe" position recorded on PAREN-STATE that
3281 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3282 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3283 ;; find the closest limit before a given limit that might be nil.
3285 ;; A "safe" position is a position at or after a recorded open
3286 ;; paren, or after a recorded close paren. The returned position is
3287 ;; thus either the first position after a close brace, or the first
3288 ;; position after an enclosing paren, or at the enclosing paren in
3289 ;; case BUFPOS is immediately after it.
3290 (when bufpos
3291 (let (elem)
3292 (catch 'done
3293 (while paren-state
3294 (setq elem (car paren-state))
3295 (if (consp elem)
3296 (cond ((< (cdr elem) bufpos)
3297 (throw 'done (cdr elem)))
3298 ((< (car elem) bufpos)
3299 ;; See below.
3300 (throw 'done (min (1+ (car elem)) bufpos))))
3301 (if (< elem bufpos)
3302 ;; elem is the position at and not after the opening paren, so
3303 ;; we can go forward one more step unless it's equal to
3304 ;; bufpos. This is useful in some cases avoid an extra paren
3305 ;; level between the safe position and bufpos.
3306 (throw 'done (min (1+ elem) bufpos))))
3307 (setq paren-state (cdr paren-state)))))))
3309 (defun c-beginning-of-syntax ()
3310 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3311 ;; goes to the closest previous point that is known to be outside
3312 ;; any string literal or comment. `c-state-cache' is used if it has
3313 ;; a position in the vicinity.
3314 (let* ((paren-state c-state-cache)
3315 elem
3317 (pos (catch 'done
3318 ;; Note: Similar code in `c-safe-position'. The
3319 ;; difference is that we accept a safe position at
3320 ;; the point and don't bother to go forward past open
3321 ;; parens.
3322 (while paren-state
3323 (setq elem (car paren-state))
3324 (if (consp elem)
3325 (cond ((<= (cdr elem) (point))
3326 (throw 'done (cdr elem)))
3327 ((<= (car elem) (point))
3328 (throw 'done (car elem))))
3329 (if (<= elem (point))
3330 (throw 'done elem)))
3331 (setq paren-state (cdr paren-state)))
3332 (point-min))))
3334 (if (> pos (- (point) 4000))
3335 (goto-char pos)
3336 ;; The position is far back. Try `c-beginning-of-defun-1'
3337 ;; (although we can't be entirely sure it will go to a position
3338 ;; outside a comment or string in current emacsen). FIXME:
3339 ;; Consult `syntax-ppss' here.
3340 (c-beginning-of-defun-1)
3341 (if (< (point) pos)
3342 (goto-char pos)))))
3345 ;; Tools for scanning identifiers and other tokens.
3347 (defun c-on-identifier ()
3348 "Return non-nil if the point is on or directly after an identifier.
3349 Keywords are recognized and not considered identifiers. If an
3350 identifier is detected, the returned value is its starting position.
3351 If an identifier ends at the point and another begins at it \(can only
3352 happen in Pike) then the point for the preceding one is returned.
3354 Note that this function might do hidden buffer changes. See the
3355 comment at the start of cc-engine.el for more info."
3357 ;; FIXME: Shouldn't this function handle "operator" in C++?
3359 (save-excursion
3360 (skip-syntax-backward "w_")
3364 ;; Check for a normal (non-keyword) identifier.
3365 (and (looking-at c-symbol-start)
3366 (not (looking-at c-keywords-regexp))
3367 (point))
3369 (when (c-major-mode-is 'pike-mode)
3370 ;; Handle the `<operator> syntax in Pike.
3371 (let ((pos (point)))
3372 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3373 (and (if (< (skip-chars-backward "`") 0)
3375 (goto-char pos)
3376 (eq (char-after) ?\`))
3377 (looking-at c-symbol-key)
3378 (>= (match-end 0) pos)
3379 (point))))
3381 ;; Handle the "operator +" syntax in C++.
3382 (when (and c-overloadable-operators-regexp
3383 (= (c-backward-token-2 0) 0))
3385 (cond ((and (looking-at c-overloadable-operators-regexp)
3386 (or (not c-opt-op-identifier-prefix)
3387 (and (= (c-backward-token-2 1) 0)
3388 (looking-at c-opt-op-identifier-prefix))))
3389 (point))
3391 ((save-excursion
3392 (and c-opt-op-identifier-prefix
3393 (looking-at c-opt-op-identifier-prefix)
3394 (= (c-forward-token-2 1) 0)
3395 (looking-at c-overloadable-operators-regexp)))
3396 (point))))
3400 (defsubst c-simple-skip-symbol-backward ()
3401 ;; If the point is at the end of a symbol then skip backward to the
3402 ;; beginning of it. Don't move otherwise. Return non-nil if point
3403 ;; moved.
3405 ;; This function might do hidden buffer changes.
3406 (or (< (skip-syntax-backward "w_") 0)
3407 (and (c-major-mode-is 'pike-mode)
3408 ;; Handle the `<operator> syntax in Pike.
3409 (let ((pos (point)))
3410 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3411 (< (skip-chars-backward "`") 0)
3412 (looking-at c-symbol-key)
3413 (>= (match-end 0) pos))
3415 (goto-char pos)
3416 nil)))))
3418 (defun c-beginning-of-current-token (&optional back-limit)
3419 ;; Move to the beginning of the current token. Do not move if not
3420 ;; in the middle of one. BACK-LIMIT may be used to bound the
3421 ;; backward search; if given it's assumed to be at the boundary
3422 ;; between two tokens. Return non-nil if the point is moved, nil
3423 ;; otherwise.
3425 ;; This function might do hidden buffer changes.
3426 (let ((start (point)))
3427 (if (looking-at "\\w\\|\\s_")
3428 (skip-syntax-backward "w_" back-limit)
3429 (when (< (skip-syntax-backward ".()" back-limit) 0)
3430 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3431 (match-end 0))
3432 ;; `c-nonsymbol-token-regexp' should always match
3433 ;; since we've skipped backward over punctuator
3434 ;; or paren syntax, but consume one char in case
3435 ;; it doesn't so that we don't leave point before
3436 ;; some earlier incorrect token.
3437 (1+ (point)))))
3438 (if (<= pos start)
3439 (goto-char pos))))))
3440 (< (point) start)))
3442 (defun c-end-of-current-token (&optional back-limit)
3443 ;; Move to the end of the current token. Do not move if not in the
3444 ;; middle of one. BACK-LIMIT may be used to bound the backward
3445 ;; search; if given it's assumed to be at the boundary between two
3446 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3448 ;; This function might do hidden buffer changes.
3449 (let ((start (point)))
3450 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3451 (skip-syntax-forward "w_"))
3452 ((< (skip-syntax-backward ".()" back-limit) 0)
3453 (while (progn
3454 (if (looking-at c-nonsymbol-token-regexp)
3455 (goto-char (match-end 0))
3456 ;; `c-nonsymbol-token-regexp' should always match since
3457 ;; we've skipped backward over punctuator or paren
3458 ;; syntax, but move forward in case it doesn't so that
3459 ;; we don't leave point earlier than we started with.
3460 (forward-char))
3461 (< (point) start)))))
3462 (> (point) start)))
3464 (defconst c-jump-syntax-balanced
3465 (if (memq 'gen-string-delim c-emacs-features)
3466 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3467 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3469 (defconst c-jump-syntax-unbalanced
3470 (if (memq 'gen-string-delim c-emacs-features)
3471 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3472 "\\w\\|\\s_\\|\\s\""))
3474 (defun c-forward-token-2 (&optional count balanced limit)
3475 "Move forward by tokens.
3476 A token is defined as all symbols and identifiers which aren't
3477 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3478 treated properly). Point is always either left at the beginning of a
3479 token or not moved at all. COUNT specifies the number of tokens to
3480 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3481 moves to the next token beginning only if not already at one. If
3482 BALANCED is true, move over balanced parens, otherwise move into them.
3483 Also, if BALANCED is true, never move out of an enclosing paren.
3485 LIMIT sets the limit for the movement and defaults to the point limit.
3486 The case when LIMIT is set in the middle of a token, comment or macro
3487 is handled correctly, i.e. the point won't be left there.
3489 Return the number of tokens left to move \(positive or negative). If
3490 BALANCED is true, a move over a balanced paren counts as one. Note
3491 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3492 be returned. Thus, a return value of 0 guarantees that point is at
3493 the requested position and a return value less \(without signs) than
3494 COUNT guarantees that point is at the beginning of some token.
3496 Note that this function might do hidden buffer changes. See the
3497 comment at the start of cc-engine.el for more info."
3499 (or count (setq count 1))
3500 (if (< count 0)
3501 (- (c-backward-token-2 (- count) balanced limit))
3503 (let ((jump-syntax (if balanced
3504 c-jump-syntax-balanced
3505 c-jump-syntax-unbalanced))
3506 (last (point))
3507 (prev (point)))
3509 (if (zerop count)
3510 ;; If count is zero we should jump if in the middle of a token.
3511 (c-end-of-current-token))
3513 (save-restriction
3514 (if limit (narrow-to-region (point-min) limit))
3515 (if (/= (point)
3516 (progn (c-forward-syntactic-ws) (point)))
3517 ;; Skip whitespace. Count this as a move if we did in
3518 ;; fact move.
3519 (setq count (max (1- count) 0)))
3521 (if (eobp)
3522 ;; Moved out of bounds. Make sure the returned count isn't zero.
3523 (progn
3524 (if (zerop count) (setq count 1))
3525 (goto-char last))
3527 ;; Use `condition-case' to avoid having the limit tests
3528 ;; inside the loop.
3529 (condition-case nil
3530 (while (and
3531 (> count 0)
3532 (progn
3533 (setq last (point))
3534 (cond ((looking-at jump-syntax)
3535 (goto-char (scan-sexps (point) 1))
3537 ((looking-at c-nonsymbol-token-regexp)
3538 (goto-char (match-end 0))
3540 ;; `c-nonsymbol-token-regexp' above should always
3541 ;; match if there are correct tokens. Try to
3542 ;; widen to see if the limit was set in the
3543 ;; middle of one, else fall back to treating
3544 ;; the offending thing as a one character token.
3545 ((and limit
3546 (save-restriction
3547 (widen)
3548 (looking-at c-nonsymbol-token-regexp)))
3549 nil)
3551 (forward-char)
3552 t))))
3553 (c-forward-syntactic-ws)
3554 (setq prev last
3555 count (1- count)))
3556 (error (goto-char last)))
3558 (when (eobp)
3559 (goto-char prev)
3560 (setq count (1+ count)))))
3562 count)))
3564 (defun c-backward-token-2 (&optional count balanced limit)
3565 "Move backward by tokens.
3566 See `c-forward-token-2' for details."
3568 (or count (setq count 1))
3569 (if (< count 0)
3570 (- (c-forward-token-2 (- count) balanced limit))
3572 (or limit (setq limit (point-min)))
3573 (let ((jump-syntax (if balanced
3574 c-jump-syntax-balanced
3575 c-jump-syntax-unbalanced))
3576 (last (point)))
3578 (if (zerop count)
3579 ;; The count is zero so try to skip to the beginning of the
3580 ;; current token.
3581 (if (> (point)
3582 (progn (c-beginning-of-current-token) (point)))
3583 (if (< (point) limit)
3584 ;; The limit is inside the same token, so return 1.
3585 (setq count 1))
3587 ;; We're not in the middle of a token. If there's
3588 ;; whitespace after the point then we must move backward,
3589 ;; so set count to 1 in that case.
3590 (and (looking-at c-syntactic-ws-start)
3591 ;; If we're looking at a '#' that might start a cpp
3592 ;; directive then we have to do a more elaborate check.
3593 (or (/= (char-after) ?#)
3594 (not c-opt-cpp-prefix)
3595 (save-excursion
3596 (and (= (point)
3597 (progn (beginning-of-line)
3598 (looking-at "[ \t]*")
3599 (match-end 0)))
3600 (or (bobp)
3601 (progn (backward-char)
3602 (not (eq (char-before) ?\\)))))))
3603 (setq count 1))))
3605 ;; Use `condition-case' to avoid having to check for buffer
3606 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3607 (condition-case nil
3608 (while (and
3609 (> count 0)
3610 (progn
3611 (c-backward-syntactic-ws)
3612 (backward-char)
3613 (if (looking-at jump-syntax)
3614 (goto-char (scan-sexps (1+ (point)) -1))
3615 ;; This can be very inefficient if there's a long
3616 ;; sequence of operator tokens without any separation.
3617 ;; That doesn't happen in practice, anyway.
3618 (c-beginning-of-current-token))
3619 (>= (point) limit)))
3620 (setq last (point)
3621 count (1- count)))
3622 (error (goto-char last)))
3624 (if (< (point) limit)
3625 (goto-char last))
3627 count)))
3629 (defun c-forward-token-1 (&optional count balanced limit)
3630 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3631 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3632 characters are jumped over character by character. This function is
3633 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3634 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3635 (c-forward-token-2 count balanced limit)))
3637 (defun c-backward-token-1 (&optional count balanced limit)
3638 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3639 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3640 characters are jumped over character by character. This function is
3641 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3642 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3643 (c-backward-token-2 count balanced limit)))
3646 ;; Tools for doing searches restricted to syntactically relevant text.
3648 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3649 paren-level not-inside-token
3650 lookbehind-submatch)
3651 "Like `re-search-forward', but only report matches that are found
3652 in syntactically significant text. I.e. matches in comments, macros
3653 or string literals are ignored. The start point is assumed to be
3654 outside any comment, macro or string literal, or else the content of
3655 that region is taken as syntactically significant text.
3657 If PAREN-LEVEL is non-nil, an additional restriction is added to
3658 ignore matches in nested paren sexps. The search will also not go
3659 outside the current list sexp, which has the effect that if the point
3660 should be moved to BOUND when no match is found \(i.e. NOERROR is
3661 neither nil nor t), then it will be at the closing paren if the end of
3662 the current list sexp is encountered first.
3664 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3665 ignored. Things like multicharacter operators and special symbols
3666 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3667 constants.
3669 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3670 subexpression in REGEXP. The end of that submatch is used as the
3671 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3672 isn't used or if that subexpression didn't match then the start
3673 position of the whole match is used instead. The \"look behind\"
3674 subexpression is never tested before the starting position, so it
3675 might be a good idea to include \\=\\= as a match alternative in it.
3677 Optimization note: Matches might be missed if the \"look behind\"
3678 subexpression can match the end of nonwhite syntactic whitespace,
3679 i.e. the end of comments or cpp directives. This since the function
3680 skips over such things before resuming the search. It's on the other
3681 hand not safe to assume that the \"look behind\" subexpression never
3682 matches syntactic whitespace.
3684 Bug: Unbalanced parens inside cpp directives are currently not handled
3685 correctly \(i.e. they don't get ignored as they should) when
3686 PAREN-LEVEL is set.
3688 Note that this function might do hidden buffer changes. See the
3689 comment at the start of cc-engine.el for more info."
3691 (or bound (setq bound (point-max)))
3692 (if paren-level (setq paren-level -1))
3694 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3696 (let ((start (point))
3698 ;; Start position for the last search.
3699 search-pos
3700 ;; The `parse-partial-sexp' state between the start position
3701 ;; and the point.
3702 state
3703 ;; The current position after the last state update. The next
3704 ;; `parse-partial-sexp' continues from here.
3705 (state-pos (point))
3706 ;; The position at which to check the state and the state
3707 ;; there. This is separate from `state-pos' since we might
3708 ;; need to back up before doing the next search round.
3709 check-pos check-state
3710 ;; Last position known to end a token.
3711 (last-token-end-pos (point-min))
3712 ;; Set when a valid match is found.
3713 found)
3715 (condition-case err
3716 (while
3717 (and
3718 (progn
3719 (setq search-pos (point))
3720 (re-search-forward regexp bound noerror))
3722 (progn
3723 (setq state (parse-partial-sexp
3724 state-pos (match-beginning 0) paren-level nil state)
3725 state-pos (point))
3726 (if (setq check-pos (and lookbehind-submatch
3727 (or (not paren-level)
3728 (>= (car state) 0))
3729 (match-end lookbehind-submatch)))
3730 (setq check-state (parse-partial-sexp
3731 state-pos check-pos paren-level nil state))
3732 (setq check-pos state-pos
3733 check-state state))
3735 ;; NOTE: If we got a look behind subexpression and get
3736 ;; an insignificant match in something that isn't
3737 ;; syntactic whitespace (i.e. strings or in nested
3738 ;; parentheses), then we can never skip more than a
3739 ;; single character from the match start position
3740 ;; (i.e. `state-pos' here) before continuing the
3741 ;; search. That since the look behind subexpression
3742 ;; might match the end of the insignificant region in
3743 ;; the next search.
3745 (cond
3746 ((elt check-state 7)
3747 ;; Match inside a line comment. Skip to eol. Use
3748 ;; `re-search-forward' instead of `skip-chars-forward' to get
3749 ;; the right bound behavior.
3750 (re-search-forward "[\n\r]" bound noerror))
3752 ((elt check-state 4)
3753 ;; Match inside a block comment. Skip to the '*/'.
3754 (search-forward "*/" bound noerror))
3756 ((and (not (elt check-state 5))
3757 (eq (char-before check-pos) ?/)
3758 (not (c-get-char-property (1- check-pos) 'syntax-table))
3759 (memq (char-after check-pos) '(?/ ?*)))
3760 ;; Match in the middle of the opener of a block or line
3761 ;; comment.
3762 (if (= (char-after check-pos) ?/)
3763 (re-search-forward "[\n\r]" bound noerror)
3764 (search-forward "*/" bound noerror)))
3766 ;; The last `parse-partial-sexp' above might have
3767 ;; stopped short of the real check position if the end
3768 ;; of the current sexp was encountered in paren-level
3769 ;; mode. The checks above are always false in that
3770 ;; case, and since they can do better skipping in
3771 ;; lookbehind-submatch mode, we do them before
3772 ;; checking the paren level.
3774 ((and paren-level
3775 (/= (setq tmp (car check-state)) 0))
3776 ;; Check the paren level first since we're short of the
3777 ;; syntactic checking position if the end of the
3778 ;; current sexp was encountered by `parse-partial-sexp'.
3779 (if (> tmp 0)
3781 ;; Inside a nested paren sexp.
3782 (if lookbehind-submatch
3783 ;; See the NOTE above.
3784 (progn (goto-char state-pos) t)
3785 ;; Skip out of the paren quickly.
3786 (setq state (parse-partial-sexp state-pos bound 0 nil state)
3787 state-pos (point)))
3789 ;; Have exited the current paren sexp.
3790 (if noerror
3791 (progn
3792 ;; The last `parse-partial-sexp' call above
3793 ;; has left us just after the closing paren
3794 ;; in this case, so we can modify the bound
3795 ;; to leave the point at the right position
3796 ;; upon return.
3797 (setq bound (1- (point)))
3798 nil)
3799 (signal 'search-failed (list regexp)))))
3801 ((setq tmp (elt check-state 3))
3802 ;; Match inside a string.
3803 (if (or lookbehind-submatch
3804 (not (integerp tmp)))
3805 ;; See the NOTE above.
3806 (progn (goto-char state-pos) t)
3807 ;; Skip to the end of the string before continuing.
3808 (let ((ender (make-string 1 tmp)) (continue t))
3809 (while (if (search-forward ender bound noerror)
3810 (progn
3811 (setq state (parse-partial-sexp
3812 state-pos (point) nil nil state)
3813 state-pos (point))
3814 (elt state 3))
3815 (setq continue nil)))
3816 continue)))
3818 ((save-excursion
3819 (save-match-data
3820 (c-beginning-of-macro start)))
3821 ;; Match inside a macro. Skip to the end of it.
3822 (c-end-of-macro)
3823 (cond ((<= (point) bound) t)
3824 (noerror nil)
3825 (t (signal 'search-failed (list regexp)))))
3827 ((and not-inside-token
3828 (or (< check-pos last-token-end-pos)
3829 (< check-pos
3830 (save-excursion
3831 (goto-char check-pos)
3832 (save-match-data
3833 (c-end-of-current-token last-token-end-pos))
3834 (setq last-token-end-pos (point))))))
3835 ;; Inside a token.
3836 (if lookbehind-submatch
3837 ;; See the NOTE above.
3838 (goto-char state-pos)
3839 (goto-char (min last-token-end-pos bound))))
3842 ;; A real match.
3843 (setq found t)
3844 nil)))
3846 ;; Should loop to search again, but take care to avoid
3847 ;; looping on the same spot.
3848 (or (/= search-pos (point))
3849 (if (= (point) bound)
3850 (if noerror
3852 (signal 'search-failed (list regexp)))
3853 (forward-char)
3854 t))))
3856 (error
3857 (goto-char start)
3858 (signal (car err) (cdr err))))
3860 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3862 (if found
3863 (progn
3864 (goto-char (match-end 0))
3865 (match-end 0))
3867 ;; Search failed. Set point as appropriate.
3868 (if (eq noerror t)
3869 (goto-char start)
3870 (goto-char bound))
3871 nil)))
3873 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
3875 (defsubst c-ssb-lit-begin ()
3876 ;; Return the start of the literal point is in, or nil.
3877 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3878 ;; bound in the caller.
3880 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3881 ;; if it's outside comments and strings.
3882 (save-excursion
3883 (let ((pos (point)) safe-pos state pps-end-pos)
3884 ;; Pick a safe position as close to the point as possible.
3886 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3887 ;; position.
3889 (while (and safe-pos-list
3890 (> (car safe-pos-list) (point)))
3891 (setq safe-pos-list (cdr safe-pos-list)))
3892 (unless (setq safe-pos (car-safe safe-pos-list))
3893 (setq safe-pos (max (or (c-safe-position
3894 (point) (or c-state-cache
3895 (c-parse-state)))
3897 (point-min))
3898 safe-pos-list (list safe-pos)))
3900 ;; Cache positions along the way to use if we have to back up more. We
3901 ;; cache every closing paren on the same level. If the paren cache is
3902 ;; relevant in this region then we're typically already on the same
3903 ;; level as the target position. Note that we might cache positions
3904 ;; after opening parens in case safe-pos is in a nested list. That's
3905 ;; both uncommon and harmless.
3906 (while (progn
3907 (setq state (parse-partial-sexp
3908 safe-pos pos 0))
3909 (< (point) pos))
3910 (setq safe-pos (point)
3911 safe-pos-list (cons safe-pos safe-pos-list)))
3913 ;; If the state contains the start of the containing sexp we cache that
3914 ;; position too, so that parse-partial-sexp in the next run has a bigger
3915 ;; chance of starting at the same level as the target position and thus
3916 ;; will get more good safe positions into the list.
3917 (if (elt state 1)
3918 (setq safe-pos (1+ (elt state 1))
3919 safe-pos-list (cons safe-pos safe-pos-list)))
3921 (if (or (elt state 3) (elt state 4))
3922 ;; Inside string or comment. Continue search at the
3923 ;; beginning of it.
3924 (elt state 8)))))
3926 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3927 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3928 i.e. don't stop at positions inside syntactic whitespace or string
3929 literals. Preprocessor directives are also ignored, with the exception
3930 of the one that the point starts within, if any. If LIMIT is given,
3931 it's assumed to be at a syntactically relevant position.
3933 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3934 sexps, and the search will also not go outside the current paren sexp.
3935 However, if LIMIT or the buffer limit is reached inside a nested paren
3936 then the point will be left at the limit.
3938 Non-nil is returned if the point moved, nil otherwise.
3940 Note that this function might do hidden buffer changes. See the
3941 comment at the start of cc-engine.el for more info."
3943 (let ((start (point))
3944 state-2
3945 ;; A list of syntactically relevant positions in descending
3946 ;; order. It's used to avoid scanning repeatedly over
3947 ;; potentially large regions with `parse-partial-sexp' to verify
3948 ;; each position. Used in `c-ssb-lit-begin'
3949 safe-pos-list
3950 ;; The result from `c-beginning-of-macro' at the start position or the
3951 ;; start position itself if it isn't within a macro. Evaluated on
3952 ;; demand.
3953 start-macro-beg
3954 ;; The earliest position after the current one with the same paren
3955 ;; level. Used only when `paren-level' is set.
3956 lit-beg
3957 (paren-level-pos (point)))
3959 (while
3960 (progn
3961 ;; The next loop "tries" to find the end point each time round,
3962 ;; loops when it hasn't succeeded.
3963 (while
3964 (and
3965 (< (skip-chars-backward skip-chars limit) 0)
3967 (let ((pos (point)) state-2 pps-end-pos)
3969 (cond
3970 ;; Don't stop inside a literal
3971 ((setq lit-beg (c-ssb-lit-begin))
3972 (goto-char lit-beg)
3975 ((and paren-level
3976 (save-excursion
3977 (setq state-2 (parse-partial-sexp
3978 pos paren-level-pos -1)
3979 pps-end-pos (point))
3980 (/= (car state-2) 0)))
3981 ;; Not at the right level.
3983 (if (and (< (car state-2) 0)
3984 ;; We stop above if we go out of a paren.
3985 ;; Now check whether it precedes or is
3986 ;; nested in the starting sexp.
3987 (save-excursion
3988 (setq state-2
3989 (parse-partial-sexp
3990 pps-end-pos paren-level-pos
3991 nil nil state-2))
3992 (< (car state-2) 0)))
3994 ;; We've stopped short of the starting position
3995 ;; so the hit was inside a nested list. Go up
3996 ;; until we are at the right level.
3997 (condition-case nil
3998 (progn
3999 (goto-char (scan-lists pos -1
4000 (- (car state-2))))
4001 (setq paren-level-pos (point))
4002 (if (and limit (>= limit paren-level-pos))
4003 (progn
4004 (goto-char limit)
4005 nil)
4007 (error
4008 (goto-char (or limit (point-min)))
4009 nil))
4011 ;; The hit was outside the list at the start
4012 ;; position. Go to the start of the list and exit.
4013 (goto-char (1+ (elt state-2 1)))
4014 nil))
4016 ((c-beginning-of-macro limit)
4017 ;; Inside a macro.
4018 (if (< (point)
4019 (or start-macro-beg
4020 (setq start-macro-beg
4021 (save-excursion
4022 (goto-char start)
4023 (c-beginning-of-macro limit)
4024 (point)))))
4027 ;; It's inside the same macro we started in so it's
4028 ;; a relevant match.
4029 (goto-char pos)
4030 nil))))))
4032 (> (point)
4033 (progn
4034 ;; Skip syntactic ws afterwards so that we don't stop at the
4035 ;; end of a comment if `skip-chars' is something like "^/".
4036 (c-backward-syntactic-ws)
4037 (point)))))
4039 ;; We might want to extend this with more useful return values in
4040 ;; the future.
4041 (/= (point) start)))
4043 ;; The following is an alternative implementation of
4044 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4045 ;; track of the syntactic context. It turned out to be generally
4046 ;; slower than the one above which uses forward checks from earlier
4047 ;; safe positions.
4049 ;;(defconst c-ssb-stop-re
4050 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4051 ;; ;; stop at to avoid going into comments and literals.
4052 ;; (concat
4053 ;; ;; Match comment end syntax and string literal syntax. Also match
4054 ;; ;; '/' for block comment endings (not covered by comment end
4055 ;; ;; syntax).
4056 ;; "\\s>\\|/\\|\\s\""
4057 ;; (if (memq 'gen-string-delim c-emacs-features)
4058 ;; "\\|\\s|"
4059 ;; "")
4060 ;; (if (memq 'gen-comment-delim c-emacs-features)
4061 ;; "\\|\\s!"
4062 ;; "")))
4064 ;;(defconst c-ssb-stop-paren-re
4065 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4066 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4068 ;;(defconst c-ssb-sexp-end-re
4069 ;; ;; Regexp matching the ending syntax of a complex sexp.
4070 ;; (concat c-string-limit-regexp "\\|\\s)"))
4072 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4073 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4074 ;;i.e. don't stop at positions inside syntactic whitespace or string
4075 ;;literals. Preprocessor directives are also ignored. However, if the
4076 ;;point is within a comment, string literal or preprocessor directory to
4077 ;;begin with, its contents is treated as syntactically relevant chars.
4078 ;;If LIMIT is given, it limits the backward search and the point will be
4079 ;;left there if no earlier position is found.
4081 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4082 ;;sexps, and the search will also not go outside the current paren sexp.
4083 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4084 ;;then the point will be left at the limit.
4086 ;;Non-nil is returned if the point moved, nil otherwise.
4088 ;;Note that this function might do hidden buffer changes. See the
4089 ;;comment at the start of cc-engine.el for more info."
4091 ;; (save-restriction
4092 ;; (when limit
4093 ;; (narrow-to-region limit (point-max)))
4095 ;; (let ((start (point)))
4096 ;; (catch 'done
4097 ;; (while (let ((last-pos (point))
4098 ;; (stop-pos (progn
4099 ;; (skip-chars-backward skip-chars)
4100 ;; (point))))
4102 ;; ;; Skip back over the same region as
4103 ;; ;; `skip-chars-backward' above, but keep to
4104 ;; ;; syntactically relevant positions.
4105 ;; (goto-char last-pos)
4106 ;; (while (and
4107 ;; ;; `re-search-backward' with a single char regexp
4108 ;; ;; should be fast.
4109 ;; (re-search-backward
4110 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4111 ;; stop-pos 'move)
4113 ;; (progn
4114 ;; (cond
4115 ;; ((looking-at "\\s(")
4116 ;; ;; `paren-level' is set and we've found the
4117 ;; ;; start of the containing paren.
4118 ;; (forward-char)
4119 ;; (throw 'done t))
4121 ;; ((looking-at c-ssb-sexp-end-re)
4122 ;; ;; We're at the end of a string literal or paren
4123 ;; ;; sexp (if `paren-level' is set).
4124 ;; (forward-char)
4125 ;; (condition-case nil
4126 ;; (c-backward-sexp)
4127 ;; (error
4128 ;; (goto-char limit)
4129 ;; (throw 'done t))))
4131 ;; (t
4132 ;; (forward-char)
4133 ;; ;; At the end of some syntactic ws or possibly
4134 ;; ;; after a plain '/' operator.
4135 ;; (let ((pos (point)))
4136 ;; (c-backward-syntactic-ws)
4137 ;; (if (= pos (point))
4138 ;; ;; Was a plain '/' operator. Go past it.
4139 ;; (backward-char)))))
4141 ;; (> (point) stop-pos))))
4143 ;; ;; Now the point is either at `stop-pos' or at some
4144 ;; ;; position further back if `stop-pos' was at a
4145 ;; ;; syntactically irrelevant place.
4147 ;; ;; Skip additional syntactic ws so that we don't stop
4148 ;; ;; at the end of a comment if `skip-chars' is
4149 ;; ;; something like "^/".
4150 ;; (c-backward-syntactic-ws)
4152 ;; (< (point) stop-pos))))
4154 ;; ;; We might want to extend this with more useful return values
4155 ;; ;; in the future.
4156 ;; (/= (point) start))))
4159 ;; Tools for handling comments and string literals.
4161 (defun c-slow-in-literal (&optional lim detect-cpp)
4162 "Return the type of literal point is in, if any.
4163 The return value is `c' if in a C-style comment, `c++' if in a C++
4164 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4165 is non-nil and in a preprocessor line, or nil if somewhere else.
4166 Optional LIM is used as the backward limit of the search. If omitted,
4167 or nil, `c-beginning-of-defun' is used.
4169 The last point calculated is cached if the cache is enabled, i.e. if
4170 `c-in-literal-cache' is bound to a two element vector.
4172 Note that this function might do hidden buffer changes. See the
4173 comment at the start of cc-engine.el for more info."
4175 (if (and (vectorp c-in-literal-cache)
4176 (= (point) (aref c-in-literal-cache 0)))
4177 (aref c-in-literal-cache 1)
4178 (let ((rtn (save-excursion
4179 (let* ((pos (point))
4180 (lim (or lim (progn
4181 (c-beginning-of-syntax)
4182 (point))))
4183 (state (parse-partial-sexp lim pos)))
4184 (cond
4185 ((elt state 3) 'string)
4186 ((elt state 4) (if (elt state 7) 'c++ 'c))
4187 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
4188 (t nil))))))
4189 ;; cache this result if the cache is enabled
4190 (if (not c-in-literal-cache)
4191 (setq c-in-literal-cache (vector (point) rtn)))
4192 rtn)))
4194 ;; XEmacs has a built-in function that should make this much quicker.
4195 ;; I don't think we even need the cache, which makes our lives more
4196 ;; complicated anyway. In this case, lim is only used to detect
4197 ;; cpp directives.
4199 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
4200 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
4201 ;; Xemacs 21.4.4. It manifested itself thus:
4203 ;; Starting with an empty AWK Mode buffer, type
4204 ;; /regexp/ {<C-j>
4205 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
4207 ;; AWK Mode is designed such that when the first / is typed, it gets the
4208 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
4209 ;; are given the s-t property "string". However, buffer-syntactic-context
4210 ;; fails to take account of the change of the s-t property on the opening / to
4211 ;; "string", and reports that the { is within a string started by the second /.
4213 ;; The workaround for this is for the AWK Mode initialisation to switch the
4214 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
4215 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
4217 ;; (Alan Mackenzie, 2003/4/30).
4219 (defun c-fast-in-literal (&optional lim detect-cpp)
4220 ;; This function might do hidden buffer changes.
4221 (let ((context (buffer-syntactic-context)))
4222 (cond
4223 ((eq context 'string) 'string)
4224 ((eq context 'comment) 'c++)
4225 ((eq context 'block-comment) 'c)
4226 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
4228 (defalias 'c-in-literal
4229 (if (fboundp 'buffer-syntactic-context)
4230 'c-fast-in-literal ; XEmacs
4231 'c-slow-in-literal)) ; GNU Emacs
4233 ;; The defalias above isn't enough to shut up the byte compiler.
4234 (cc-bytecomp-defun c-in-literal)
4236 (defun c-literal-limits (&optional lim near not-in-delimiter)
4237 "Return a cons of the beginning and end positions of the comment or
4238 string surrounding point (including both delimiters), or nil if point
4239 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4240 to start parsing from. If NEAR is non-nil, then the limits of any
4241 literal next to point is returned. \"Next to\" means there's only
4242 spaces and tabs between point and the literal. The search for such a
4243 literal is done first in forward direction. If NOT-IN-DELIMITER is
4244 non-nil, the case when point is inside a starting delimiter won't be
4245 recognized. This only has effect for comments which have starting
4246 delimiters with more than one character.
4248 Note that this function might do hidden buffer changes. See the
4249 comment at the start of cc-engine.el for more info."
4251 (save-excursion
4252 (let* ((pos (point))
4253 (lim (or lim (progn
4254 (c-beginning-of-syntax)
4255 (point))))
4256 (state (parse-partial-sexp lim pos)))
4258 (cond ((elt state 3) ; String.
4259 (goto-char (elt state 8))
4260 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4261 (point-max))))
4263 ((elt state 4) ; Comment.
4264 (goto-char (elt state 8))
4265 (cons (point) (progn (c-forward-single-comment) (point))))
4267 ((and (not not-in-delimiter)
4268 (not (elt state 5))
4269 (eq (char-before) ?/)
4270 (looking-at "[/*]"))
4271 ;; We're standing in a comment starter.
4272 (backward-char 1)
4273 (cons (point) (progn (c-forward-single-comment) (point))))
4275 (near
4276 (goto-char pos)
4278 ;; Search forward for a literal.
4279 (skip-chars-forward " \t")
4281 (cond
4282 ((looking-at c-string-limit-regexp) ; String.
4283 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4284 (point-max))))
4286 ((looking-at c-comment-start-regexp) ; Line or block comment.
4287 (cons (point) (progn (c-forward-single-comment) (point))))
4290 ;; Search backward.
4291 (skip-chars-backward " \t")
4293 (let ((end (point)) beg)
4294 (cond
4295 ((save-excursion
4296 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4297 (setq beg (c-safe (c-backward-sexp 1) (point))))
4299 ((and (c-safe (forward-char -2) t)
4300 (looking-at "*/"))
4301 ;; Block comment. Due to the nature of line
4302 ;; comments, they will always be covered by the
4303 ;; normal case above.
4304 (goto-char end)
4305 (c-backward-single-comment)
4306 ;; If LIM is bogus, beg will be bogus.
4307 (setq beg (point))))
4309 (if beg (cons beg end))))))
4310 ))))
4312 ;; In case external callers use this; it did have a docstring.
4313 (defalias 'c-literal-limits-fast 'c-literal-limits)
4315 (defun c-collect-line-comments (range)
4316 "If the argument is a cons of two buffer positions (such as returned by
4317 `c-literal-limits'), and that range contains a C++ style line comment,
4318 then an extended range is returned that contains all adjacent line
4319 comments (i.e. all comments that starts in the same column with no
4320 empty lines or non-whitespace characters between them). Otherwise the
4321 argument is returned.
4323 Note that this function might do hidden buffer changes. See the
4324 comment at the start of cc-engine.el for more info."
4326 (save-excursion
4327 (condition-case nil
4328 (if (and (consp range) (progn
4329 (goto-char (car range))
4330 (looking-at c-line-comment-starter)))
4331 (let ((col (current-column))
4332 (beg (point))
4333 (bopl (c-point 'bopl))
4334 (end (cdr range)))
4335 ;; Got to take care in the backward direction to handle
4336 ;; comments which are preceded by code.
4337 (while (and (c-backward-single-comment)
4338 (>= (point) bopl)
4339 (looking-at c-line-comment-starter)
4340 (= col (current-column)))
4341 (setq beg (point)
4342 bopl (c-point 'bopl)))
4343 (goto-char end)
4344 (while (and (progn (skip-chars-forward " \t")
4345 (looking-at c-line-comment-starter))
4346 (= col (current-column))
4347 (prog1 (zerop (forward-line 1))
4348 (setq end (point)))))
4349 (cons beg end))
4350 range)
4351 (error range))))
4353 (defun c-literal-type (range)
4354 "Convenience function that given the result of `c-literal-limits',
4355 returns nil or the type of literal that the range surrounds, one
4356 of the symbols 'c, 'c++ or 'string. It's much faster than using
4357 `c-in-literal' and is intended to be used when you need both the
4358 type of a literal and its limits.
4360 Note that this function might do hidden buffer changes. See the
4361 comment at the start of cc-engine.el for more info."
4363 (if (consp range)
4364 (save-excursion
4365 (goto-char (car range))
4366 (cond ((looking-at c-string-limit-regexp) 'string)
4367 ((or (looking-at "//") ; c++ line comment
4368 (and (looking-at "\\s<") ; comment starter
4369 (looking-at "#"))) ; awk comment.
4370 'c++)
4371 (t 'c))) ; Assuming the range is valid.
4372 range))
4375 ;; `c-find-decl-spots' and accompanying stuff.
4377 ;; Variables used in `c-find-decl-spots' to cache the search done for
4378 ;; the first declaration in the last call. When that function starts,
4379 ;; it needs to back up over syntactic whitespace to look at the last
4380 ;; token before the region being searched. That can sometimes cause
4381 ;; moves back and forth over a quite large region of comments and
4382 ;; macros, which would be repeated for each changed character when
4383 ;; we're called during fontification, since font-lock refontifies the
4384 ;; current line for each change. Thus it's worthwhile to cache the
4385 ;; first match.
4387 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4388 ;; the syntactic whitespace less or equal to some start position.
4389 ;; There's no cached value if it's nil.
4391 ;; `c-find-decl-match-pos' is the match position if
4392 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4393 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4394 (defvar c-find-decl-syntactic-pos nil)
4395 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4396 (defvar c-find-decl-match-pos nil)
4397 (make-variable-buffer-local 'c-find-decl-match-pos)
4399 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4400 (and c-find-decl-syntactic-pos
4401 (< change-min-pos c-find-decl-syntactic-pos)
4402 (setq c-find-decl-syntactic-pos nil)))
4404 ; (defface c-debug-decl-spot-face
4405 ; '((t (:background "Turquoise")))
4406 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4407 ; (defface c-debug-decl-sws-face
4408 ; '((t (:background "Khaki")))
4409 ; "Debug face to mark the syntactic whitespace between the declaration
4410 ; spots and the preceding token end.")
4412 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4413 (when (facep 'c-debug-decl-spot-face)
4414 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4415 (c-debug-add-face (max match-pos (point-min)) decl-pos
4416 'c-debug-decl-sws-face)
4417 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4418 'c-debug-decl-spot-face))))
4419 (defmacro c-debug-remove-decl-spot-faces (beg end)
4420 (when (facep 'c-debug-decl-spot-face)
4421 `(c-save-buffer-state ()
4422 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4423 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4425 (defmacro c-find-decl-prefix-search ()
4426 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4427 ;; but it contains lots of free variables that refer to things
4428 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4429 ;; if there is a match, otherwise at `cfd-limit'.
4431 ;; This macro might do hidden buffer changes.
4433 '(progn
4434 ;; Find the next property match position if we haven't got one already.
4435 (unless cfd-prop-match
4436 (save-excursion
4437 (while (progn
4438 (goto-char (next-single-property-change
4439 (point) 'c-type nil cfd-limit))
4440 (and (< (point) cfd-limit)
4441 (not (eq (c-get-char-property (1- (point)) 'c-type)
4442 'c-decl-end)))))
4443 (setq cfd-prop-match (point))))
4445 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4446 ;; got one already.
4447 (unless cfd-re-match
4449 (if (> cfd-re-match-end (point))
4450 (goto-char cfd-re-match-end))
4452 (while (if (setq cfd-re-match-end
4453 (re-search-forward c-decl-prefix-or-start-re
4454 cfd-limit 'move))
4456 ;; Match. Check if it's inside a comment or string literal.
4457 (c-got-face-at
4458 (if (setq cfd-re-match (match-end 1))
4459 ;; Matched the end of a token preceding a decl spot.
4460 (progn
4461 (goto-char cfd-re-match)
4462 (1- cfd-re-match))
4463 ;; Matched a token that start a decl spot.
4464 (goto-char (match-beginning 0))
4465 (point))
4466 c-literal-faces)
4468 ;; No match. Finish up and exit the loop.
4469 (setq cfd-re-match cfd-limit)
4470 nil)
4472 ;; Skip out of comments and string literals.
4473 (while (progn
4474 (goto-char (next-single-property-change
4475 (point) 'face nil cfd-limit))
4476 (and (< (point) cfd-limit)
4477 (c-got-face-at (point) c-literal-faces)))))
4479 ;; If we matched at the decl start, we have to back up over the
4480 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4481 ;; any decl spots in the syntactic ws.
4482 (unless cfd-re-match
4483 (c-backward-syntactic-ws)
4484 (setq cfd-re-match (point))))
4486 ;; Choose whichever match is closer to the start.
4487 (if (< cfd-re-match cfd-prop-match)
4488 (setq cfd-match-pos cfd-re-match
4489 cfd-re-match nil)
4490 (setq cfd-match-pos cfd-prop-match
4491 cfd-prop-match nil))
4493 (goto-char cfd-match-pos)
4495 (when (< cfd-match-pos cfd-limit)
4496 ;; Skip forward past comments only so we don't skip macros.
4497 (c-forward-comments)
4498 ;; Set the position to continue at. We can avoid going over
4499 ;; the comments skipped above a second time, but it's possible
4500 ;; that the comment skipping has taken us past `cfd-prop-match'
4501 ;; since the property might be used inside comments.
4502 (setq cfd-continue-pos (if cfd-prop-match
4503 (min cfd-prop-match (point))
4504 (point))))))
4506 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4507 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4508 ;; label from the point to CFD-LIMIT.
4510 ;; CFD-FUN is called with point at the start of the spot. It's
4511 ;; passed two arguments: The first is the end position of the token
4512 ;; preceding the spot, or 0 for the implicit match at bob. The
4513 ;; second is a flag that is t when the match is inside a macro. If
4514 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
4515 ;; spot, it should return non-nil to ensure that the next search
4516 ;; will find them.
4518 ;; Such a spot is:
4519 ;; o The first token after bob.
4520 ;; o The first token after the end of submatch 1 in
4521 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4522 ;; o The start of each `c-decl-prefix-or-start-re' match when
4523 ;; submatch 1 doesn't match.
4524 ;; o The first token after the end of each occurrence of the
4525 ;; `c-type' text property with the value `c-decl-end', provided
4526 ;; `c-type-decl-end-used' is set.
4528 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4529 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4530 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4532 ;; If the match is inside a macro then the buffer is narrowed to the
4533 ;; end of it, so that CFD-FUN can investigate the following tokens
4534 ;; without matching something that begins inside a macro and ends
4535 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4536 ;; CFD-FACE-CHECKLIST checks exist.
4538 ;; The spots are visited approximately in order from top to bottom.
4539 ;; It's however the positions where `c-decl-prefix-or-start-re'
4540 ;; matches and where `c-decl-end' properties are found that are in
4541 ;; order. Since the spots often are at the following token, they
4542 ;; might be visited out of order insofar as more spots are reported
4543 ;; later on within the syntactic whitespace between the match
4544 ;; positions and their spots.
4546 ;; It's assumed that comments and strings are fontified in the
4547 ;; searched range.
4549 ;; This is mainly used in fontification, and so has an elaborate
4550 ;; cache to handle repeated calls from the same start position; see
4551 ;; the variables above.
4553 ;; All variables in this function begin with `cfd-' to avoid name
4554 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4556 ;; This function might do hidden buffer changes.
4558 (let ((cfd-start-pos (point))
4559 (cfd-buffer-end (point-max))
4560 ;; The end of the token preceding the decl spot last found
4561 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4562 ;; no match.
4563 cfd-re-match
4564 ;; The end position of the last `c-decl-prefix-or-start-re'
4565 ;; match. If this is greater than `cfd-continue-pos', the
4566 ;; next regexp search is started here instead.
4567 (cfd-re-match-end (point-min))
4568 ;; The end of the last `c-decl-end' found by
4569 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4570 ;; match. If searching for the property isn't needed then we
4571 ;; disable it by setting it to `cfd-limit' directly.
4572 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4573 ;; The end of the token preceding the decl spot last found by
4574 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4575 ;; bob. `cfd-limit' if there's no match. In other words,
4576 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4577 (cfd-match-pos cfd-limit)
4578 ;; The position to continue searching at.
4579 cfd-continue-pos
4580 ;; The position of the last "real" token we've stopped at.
4581 ;; This can be greater than `cfd-continue-pos' when we get
4582 ;; hits inside macros or at `c-decl-end' positions inside
4583 ;; comments.
4584 (cfd-token-pos 0)
4585 ;; The end position of the last entered macro.
4586 (cfd-macro-end 0))
4588 ;; Initialize by finding a syntactically relevant start position
4589 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4590 ;; search unless we're at bob.
4592 (let (start-in-literal start-in-macro syntactic-pos)
4593 ;; Must back up a bit since we look for the end of the previous
4594 ;; statement or declaration, which is earlier than the first
4595 ;; returned match.
4597 (cond
4598 ;; First we need to move to a syntactically relevant position.
4599 ;; Begin by backing out of comment or string literals.
4600 ((and
4601 (when (c-got-face-at (point) c-literal-faces)
4602 ;; Try to use the faces to back up to the start of the
4603 ;; literal. FIXME: What if the point is on a declaration
4604 ;; inside a comment?
4605 (while (and (not (bobp))
4606 (c-got-face-at (1- (point)) c-literal-faces))
4607 (goto-char (previous-single-property-change
4608 (point) 'face nil (point-min))))
4610 ;; XEmacs doesn't fontify the quotes surrounding string
4611 ;; literals.
4612 (and (featurep 'xemacs)
4613 (eq (get-text-property (point) 'face)
4614 'font-lock-string-face)
4615 (not (bobp))
4616 (progn (backward-char)
4617 (not (looking-at c-string-limit-regexp)))
4618 (forward-char))
4620 ;; Don't trust the literal to contain only literal faces
4621 ;; (the font lock package might not have fontified the
4622 ;; start of it at all, for instance) so check that we have
4623 ;; arrived at something that looks like a start or else
4624 ;; resort to `c-literal-limits'.
4625 (unless (looking-at c-literal-start-regexp)
4626 (let ((range (c-literal-limits)))
4627 (if range (goto-char (car range)))))
4629 (setq start-in-literal (point)))
4631 ;; The start is in a literal. If the limit is in the same
4632 ;; one we don't have to find a syntactic position etc. We
4633 ;; only check that if the limit is at or before bonl to save
4634 ;; time; it covers the by far most common case when font-lock
4635 ;; refontifies the current line only.
4636 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4637 (save-excursion
4638 (goto-char cfd-start-pos)
4639 (while (progn
4640 (goto-char (next-single-property-change
4641 (point) 'face nil cfd-limit))
4642 (and (< (point) cfd-limit)
4643 (c-got-face-at (point) c-literal-faces))))
4644 (= (point) cfd-limit)))
4646 ;; Completely inside a literal. Set up variables to trig the
4647 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4648 ;; find a suitable start position.
4649 (setq cfd-continue-pos start-in-literal))
4651 ;; Check if the region might be completely inside a macro, to
4652 ;; optimize that like the completely-inside-literal above.
4653 ((save-excursion
4654 (and (= (forward-line 1) 0)
4655 (bolp) ; forward-line has funny behavior at eob.
4656 (>= (point) cfd-limit)
4657 (progn (backward-char)
4658 (eq (char-before) ?\\))))
4659 ;; (Maybe) completely inside a macro. Only need to trig the
4660 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4661 ;; set things up.
4662 (setq cfd-continue-pos (1- cfd-start-pos)
4663 start-in-macro t))
4666 ;; Back out of any macro so we don't miss any declaration
4667 ;; that could follow after it.
4668 (when (c-beginning-of-macro)
4669 (setq start-in-macro t))
4671 ;; Now we're at a proper syntactically relevant position so we
4672 ;; can use the cache. But first clear it if it applied
4673 ;; further down.
4674 (c-invalidate-find-decl-cache cfd-start-pos)
4676 (setq syntactic-pos (point))
4677 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
4678 ;; Don't have to do this if the cache is relevant here,
4679 ;; typically if the same line is refontified again. If
4680 ;; we're just some syntactic whitespace further down we can
4681 ;; still use the cache to limit the skipping.
4682 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
4684 ;; If we hit `c-find-decl-syntactic-pos' and
4685 ;; `c-find-decl-match-pos' is set then we install the cached
4686 ;; values. If we hit `c-find-decl-syntactic-pos' and
4687 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4688 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4689 ;; and so we can continue the search from this point. If we
4690 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4691 ;; the right spot to begin searching anyway.
4692 (if (and (eq (point) c-find-decl-syntactic-pos)
4693 c-find-decl-match-pos)
4694 (setq cfd-match-pos c-find-decl-match-pos
4695 cfd-continue-pos syntactic-pos)
4697 (setq c-find-decl-syntactic-pos syntactic-pos)
4699 (when (if (bobp)
4700 ;; Always consider bob a match to get the first
4701 ;; declaration in the file. Do this separately instead of
4702 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4703 ;; regexp always can consume at least one character to
4704 ;; ensure that we won't get stuck in an infinite loop.
4705 (setq cfd-re-match 0)
4706 (backward-char)
4707 (c-beginning-of-current-token)
4708 (< (point) cfd-limit))
4709 ;; Do an initial search now. In the bob case above it's
4710 ;; only done to search for a `c-decl-end' spot.
4711 (c-find-decl-prefix-search))
4713 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
4714 cfd-match-pos)))))
4716 ;; Advance `cfd-continue-pos' if it's before the start position.
4717 ;; The closest continue position that might have effect at or
4718 ;; after the start depends on what we started in. This also
4719 ;; finds a suitable start position in the special cases when the
4720 ;; region is completely within a literal or macro.
4721 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
4723 (cond
4724 (start-in-macro
4725 ;; If we're in a macro then it's the closest preceding token
4726 ;; in the macro. Check this before `start-in-literal',
4727 ;; since if we're inside a literal in a macro, the preceding
4728 ;; token is earlier than any `c-decl-end' spot inside the
4729 ;; literal (comment).
4730 (goto-char (or start-in-literal cfd-start-pos))
4731 ;; The only syntactic ws in macros are comments.
4732 (c-backward-comments)
4733 (backward-char)
4734 (c-beginning-of-current-token))
4736 (start-in-literal
4737 ;; If we're in a comment it can only be the closest
4738 ;; preceding `c-decl-end' position within that comment, if
4739 ;; any. Go back to the beginning of such a property so that
4740 ;; `c-find-decl-prefix-search' will find the end of it.
4741 ;; (Can't stop at the end and install it directly on
4742 ;; `cfd-prop-match' since that variable might be cleared
4743 ;; after `cfd-fun' below.)
4745 ;; Note that if the literal is a string then the property
4746 ;; search will simply skip to the beginning of it right
4747 ;; away.
4748 (if (not c-type-decl-end-used)
4749 (goto-char start-in-literal)
4750 (goto-char cfd-start-pos)
4751 (while (progn
4752 (goto-char (previous-single-property-change
4753 (point) 'c-type nil start-in-literal))
4754 (and (> (point) start-in-literal)
4755 (not (eq (c-get-char-property (point) 'c-type)
4756 'c-decl-end))))))
4758 (when (= (point) start-in-literal)
4759 ;; Didn't find any property inside the comment, so we can
4760 ;; skip it entirely. (This won't skip past a string, but
4761 ;; that'll be handled quickly by the next
4762 ;; `c-find-decl-prefix-search' anyway.)
4763 (c-forward-single-comment)
4764 (if (> (point) cfd-limit)
4765 (goto-char cfd-limit))))
4768 ;; If we started in normal code, the only match that might
4769 ;; apply before the start is what we already got in
4770 ;; `cfd-match-pos' so we can continue at the start position.
4771 ;; (Note that we don't get here if the first match is below
4772 ;; it.)
4773 (goto-char cfd-start-pos)))
4775 ;; Delete found matches if they are before our new continue
4776 ;; position, so that `c-find-decl-prefix-search' won't back up
4777 ;; to them later on.
4778 (setq cfd-continue-pos (point))
4779 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
4780 (setq cfd-re-match nil))
4781 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
4782 (setq cfd-prop-match nil)))
4784 (if syntactic-pos
4785 ;; This is the normal case and we got a proper syntactic
4786 ;; position. If there's a match then it's always outside
4787 ;; macros and comments, so advance to the next token and set
4788 ;; `cfd-token-pos'. The loop below will later go back using
4789 ;; `cfd-continue-pos' to fix declarations inside the
4790 ;; syntactic ws.
4791 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
4792 (goto-char syntactic-pos)
4793 (c-forward-syntactic-ws)
4794 (and cfd-continue-pos
4795 (< cfd-continue-pos (point))
4796 (setq cfd-token-pos (point))))
4798 ;; Have one of the special cases when the region is completely
4799 ;; within a literal or macro. `cfd-continue-pos' is set to a
4800 ;; good start position for the search, so do it.
4801 (c-find-decl-prefix-search)))
4803 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4805 (while (progn
4806 (while (and
4807 (< cfd-match-pos cfd-limit)
4810 ;; Kludge to filter out matches on the "<" that
4811 ;; aren't open parens, for the sake of languages
4812 ;; that got `c-recognize-<>-arglists' set.
4813 (and (eq (char-before cfd-match-pos) ?<)
4814 (not (c-get-char-property (1- cfd-match-pos)
4815 'syntax-table)))
4817 ;; If `cfd-continue-pos' is less or equal to
4818 ;; `cfd-token-pos', we've got a hit inside a macro
4819 ;; that's in the syntactic whitespace before the last
4820 ;; "real" declaration we've checked. If they're equal
4821 ;; we've arrived at the declaration a second time, so
4822 ;; there's nothing to do.
4823 (= cfd-continue-pos cfd-token-pos)
4825 (progn
4826 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4827 ;; we're still searching for declarations embedded in
4828 ;; the syntactic whitespace. In that case we need
4829 ;; only to skip comments and not macros, since they
4830 ;; can't be nested, and that's already been done in
4831 ;; `c-find-decl-prefix-search'.
4832 (when (> cfd-continue-pos cfd-token-pos)
4833 (c-forward-syntactic-ws)
4834 (setq cfd-token-pos (point)))
4836 ;; Continue if the following token fails the
4837 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4838 (when (or (>= (point) cfd-limit)
4839 (not (looking-at cfd-decl-re))
4840 (and cfd-face-checklist
4841 (not (c-got-face-at
4842 (point) cfd-face-checklist))))
4843 (goto-char cfd-continue-pos)
4844 t)))
4846 (< (point) cfd-limit))
4847 (c-find-decl-prefix-search))
4849 (< (point) cfd-limit))
4851 (when (and
4852 (>= (point) cfd-start-pos)
4854 (progn
4855 ;; Narrow to the end of the macro if we got a hit inside
4856 ;; one, to avoid recognizing things that start inside the
4857 ;; macro and end outside it.
4858 (when (> cfd-match-pos cfd-macro-end)
4859 ;; Not in the same macro as in the previous round.
4860 (save-excursion
4861 (goto-char cfd-match-pos)
4862 (setq cfd-macro-end
4863 (if (save-excursion (and (c-beginning-of-macro)
4864 (< (point) cfd-match-pos)))
4865 (progn (c-end-of-macro)
4866 (point))
4867 0))))
4869 (if (zerop cfd-macro-end)
4871 (if (> cfd-macro-end (point))
4872 (progn (narrow-to-region (point-min) cfd-macro-end)
4874 ;; The matched token was the last thing in the macro,
4875 ;; so the whole match is bogus.
4876 (setq cfd-macro-end 0)
4877 nil))))
4879 (c-debug-put-decl-spot-faces cfd-match-pos (point))
4880 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
4881 (setq cfd-prop-match nil))
4883 (when (/= cfd-macro-end 0)
4884 ;; Restore limits if we did macro narrowment above.
4885 (narrow-to-region (point-min) cfd-buffer-end)))
4887 (goto-char cfd-continue-pos)
4888 (if (= cfd-continue-pos cfd-limit)
4889 (setq cfd-match-pos cfd-limit)
4890 (c-find-decl-prefix-search)))))
4893 ;; A cache for found types.
4895 ;; Buffer local variable that contains an obarray with the types we've
4896 ;; found. If a declaration is recognized somewhere we record the
4897 ;; fully qualified identifier in it to recognize it as a type
4898 ;; elsewhere in the file too. This is not accurate since we do not
4899 ;; bother with the scoping rules of the languages, but in practice the
4900 ;; same name is seldom used as both a type and something else in a
4901 ;; file, and we only use this as a last resort in ambiguous cases (see
4902 ;; `c-forward-decl-or-cast-1').
4904 ;; Not every type need be in this cache. However, things which have
4905 ;; ceased to be types must be removed from it.
4907 ;; Template types in C++ are added here too but with the template
4908 ;; arglist replaced with "<>" in references or "<" for the one in the
4909 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4910 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4911 ;; template specs can be fairly sized programs in themselves) and
4912 ;; improves the hit ratio (it's a type regardless of the template
4913 ;; args; it's just not the same type, but we're only interested in
4914 ;; recognizing types, not telling distinct types apart). Note that
4915 ;; template types in references are added here too; from the example
4916 ;; above there will also be an entry "Foo<".
4917 (defvar c-found-types nil)
4918 (make-variable-buffer-local 'c-found-types)
4920 (defsubst c-clear-found-types ()
4921 ;; Clears `c-found-types'.
4922 (setq c-found-types (make-vector 53 0)))
4924 (defun c-add-type (from to)
4925 ;; Add the given region as a type in `c-found-types'. If the region
4926 ;; doesn't match an existing type but there is a type which is equal
4927 ;; to the given one except that the last character is missing, then
4928 ;; the shorter type is removed. That's done to avoid adding all
4929 ;; prefixes of a type as it's being entered and font locked. This
4930 ;; doesn't cover cases like when characters are removed from a type
4931 ;; or added in the middle. We'd need the position of point when the
4932 ;; font locking is invoked to solve this well.
4934 ;; This function might do hidden buffer changes.
4935 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4936 (unless (intern-soft type c-found-types)
4937 (unintern (substring type 0 -1) c-found-types)
4938 (intern type c-found-types))))
4940 (defun c-unfind-type (name)
4941 ;; Remove the "NAME" from c-found-types, if present.
4942 (unintern name c-found-types))
4944 (defsubst c-check-type (from to)
4945 ;; Return non-nil if the given region contains a type in
4946 ;; `c-found-types'.
4948 ;; This function might do hidden buffer changes.
4949 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4950 c-found-types))
4952 (defun c-list-found-types ()
4953 ;; Return all the types in `c-found-types' as a sorted list of
4954 ;; strings.
4955 (let (type-list)
4956 (mapatoms (lambda (type)
4957 (setq type-list (cons (symbol-name type)
4958 type-list)))
4959 c-found-types)
4960 (sort type-list 'string-lessp)))
4962 ;; Shut up the byte compiler.
4963 (defvar c-maybe-stale-found-type)
4965 (defun c-trim-found-types (beg end old-len)
4966 ;; An after change function which, in conjunction with the info in
4967 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4968 ;; from `c-found-types', should this type have become stale. For
4969 ;; example, this happens to "foo" when "foo \n bar();" becomes
4970 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4971 ;; the fontification.
4973 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4974 ;; type?
4975 (when (> end beg)
4976 (save-excursion
4977 (when (< end (point-max))
4978 (goto-char end)
4979 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4980 (progn (goto-char end)
4981 (c-end-of-current-token)))
4982 (c-unfind-type (buffer-substring-no-properties
4983 end (point)))))
4984 (when (> beg (point-min))
4985 (goto-char beg)
4986 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4987 (progn (goto-char beg)
4988 (c-beginning-of-current-token)))
4989 (c-unfind-type (buffer-substring-no-properties
4990 (point) beg))))))
4992 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4993 (cond
4994 ;; Changing the amount of (already existing) whitespace - don't do anything.
4995 ((and (c-partial-ws-p beg end)
4996 (or (= beg end) ; removal of WS
4997 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4999 ;; The syntactic relationship which defined a "found type" has been
5000 ;; destroyed.
5001 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
5002 (c-unfind-type (cadr c-maybe-stale-found-type)))
5003 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5007 ;; Setting and removing syntax properties on < and > in languages (C++
5008 ;; and Java) where they can be template/generic delimiters as well as
5009 ;; their normal meaning of "less/greater than".
5011 ;; Normally, < and > have syntax 'punctuation'. When they are found to
5012 ;; be delimiters, they are marked as such with the category properties
5013 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5015 ;; STRATEGY:
5017 ;; It is impossible to determine with certainty whether a <..> pair in
5018 ;; C++ is two comparison operators or is template delimiters, unless
5019 ;; one duplicates a lot of a C++ compiler. For example, the following
5020 ;; code fragment:
5022 ;; foo (a < b, c > d) ;
5024 ;; could be a function call with two integer parameters (each a
5025 ;; relational expression), or it could be a constructor for class foo
5026 ;; taking one parameter d of templated type "a < b, c >". They are
5027 ;; somewhat easier to distinguish in Java.
5029 ;; The strategy now (2010-01) adopted is to mark and unmark < and
5030 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5031 ;; individually when their context so indicated. This gave rise to
5032 ;; intractible problems when one of a matching pair was deleted, or
5033 ;; pulled into a literal.]
5035 ;; At each buffer change, the syntax-table properties are removed in a
5036 ;; before-change function and reapplied, when needed, in an
5037 ;; after-change function. It is far more important that the
5038 ;; properties get removed when they they are spurious than that they
5039 ;; be present when wanted.
5040 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5041 (defun c-clear-<-pair-props (&optional pos)
5042 ;; POS (default point) is at a < character. If it is marked with
5043 ;; open paren syntax-table text property, remove the property,
5044 ;; together with the close paren property on the matching > (if
5045 ;; any).
5046 (save-excursion
5047 (if pos
5048 (goto-char pos)
5049 (setq pos (point)))
5050 (when (equal (c-get-char-property (point) 'syntax-table)
5051 c-<-as-paren-syntax)
5052 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5053 (c-go-list-forward))
5054 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5055 c->-as-paren-syntax) ; should always be true.
5056 (c-clear-char-property (1- (point)) 'category))
5057 (c-clear-char-property pos 'category))))
5059 (defun c-clear->-pair-props (&optional pos)
5060 ;; POS (default point) is at a > character. If it is marked with
5061 ;; close paren syntax-table property, remove the property, together
5062 ;; with the open paren property on the matching < (if any).
5063 (save-excursion
5064 (if pos
5065 (goto-char pos)
5066 (setq pos (point)))
5067 (when (equal (c-get-char-property (point) 'syntax-table)
5068 c->-as-paren-syntax)
5069 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5070 (c-go-up-list-backward))
5071 (when (equal (c-get-char-property (point) 'syntax-table)
5072 c-<-as-paren-syntax) ; should always be true.
5073 (c-clear-char-property (point) 'category))
5074 (c-clear-char-property pos 'category))))
5076 (defun c-clear-<>-pair-props (&optional pos)
5077 ;; POS (default point) is at a < or > character. If it has an
5078 ;; open/close paren syntax-table property, remove this property both
5079 ;; from the current character and its partner (which will also be
5080 ;; thusly marked).
5081 (cond
5082 ((eq (char-after) ?\<)
5083 (c-clear-<-pair-props pos))
5084 ((eq (char-after) ?\>)
5085 (c-clear->-pair-props pos))
5086 (t (c-benign-error
5087 "c-clear-<>-pair-props called from wrong position"))))
5089 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5090 ;; POS (default point) is at a < character. If it is both marked
5091 ;; with open/close paren syntax-table property, and has a matching >
5092 ;; (also marked) which is after LIM, remove the property both from
5093 ;; the current > and its partner. Return t when this happens, nil
5094 ;; when it doesn't.
5095 (save-excursion
5096 (if pos
5097 (goto-char pos)
5098 (setq pos (point)))
5099 (when (equal (c-get-char-property (point) 'syntax-table)
5100 c-<-as-paren-syntax)
5101 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5102 (c-go-list-forward))
5103 (when (and (>= (point) lim)
5104 (equal (c-get-char-property (1- (point)) 'syntax-table)
5105 c->-as-paren-syntax)) ; should always be true.
5106 (c-unmark-<->-as-paren (1- (point)))
5107 (c-unmark-<->-as-paren pos))
5108 t)))
5110 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
5111 ;; POS (default point) is at a > character. If it is both marked
5112 ;; with open/close paren syntax-table property, and has a matching <
5113 ;; (also marked) which is before LIM, remove the property both from
5114 ;; the current < and its partner. Return t when this happens, nil
5115 ;; when it doesn't.
5116 (save-excursion
5117 (if pos
5118 (goto-char pos)
5119 (setq pos (point)))
5120 (when (equal (c-get-char-property (point) 'syntax-table)
5121 c->-as-paren-syntax)
5122 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5123 (c-go-up-list-backward))
5124 (when (and (<= (point) lim)
5125 (equal (c-get-char-property (point) 'syntax-table)
5126 c-<-as-paren-syntax)) ; should always be true.
5127 (c-unmark-<->-as-paren (point))
5128 (c-unmark-<->-as-paren pos))
5129 t)))
5131 ;; Set by c-common-init in cc-mode.el.
5132 (defvar c-new-BEG)
5133 (defvar c-new-END)
5135 (defun c-before-change-check-<>-operators (beg end)
5136 ;; Unmark certain pairs of "< .... >" which are currently marked as
5137 ;; template/generic delimiters. (This marking is via syntax-table
5138 ;; text properties).
5140 ;; These pairs are those which are in the current "statement" (i.e.,
5141 ;; the region between the {, }, or ; before BEG and the one after
5142 ;; END), and which enclose any part of the interval (BEG END).
5144 ;; Note that in C++ (?and Java), template/generic parens cannot
5145 ;; enclose a brace or semicolon, so we use these as bounds on the
5146 ;; region we must work on.
5148 ;; This function is called from before-change-functions (via
5149 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5150 ;; and point is undefined, both at entry and exit.
5152 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5153 ;; 2010-01-29.
5154 (save-excursion
5155 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5156 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5157 new-beg new-end need-new-beg need-new-end)
5158 ;; Locate the barrier before the changed region
5159 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5160 (c-syntactic-skip-backward "^;{}" (max (- beg 2048) (point-min)))
5161 (setq new-beg (point))
5163 ;; Remove the syntax-table properties from each pertinent <...> pair.
5164 ;; Firsly, the ones with the < before beg and > after beg.
5165 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5166 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5167 (setq need-new-beg t)))
5169 ;; Locate the barrier after END.
5170 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5171 (c-syntactic-re-search-forward "[;{}]"
5172 (min (+ end 2048) (point-max)) 'end)
5173 (setq new-end (point))
5175 ;; Remove syntax-table properties from the remaining pertinent <...>
5176 ;; pairs, those with a > after end and < before end.
5177 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5178 (if (c-clear->-pair-props-if-match-before end)
5179 (setq need-new-end t)))
5181 ;; Extend the fontification region, if needed.
5182 (when need-new-beg
5183 (goto-char new-beg)
5184 (c-forward-syntactic-ws)
5185 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5187 (when need-new-end
5188 (and (> new-end c-new-END) (setq c-new-END new-end))))))
5192 (defun c-after-change-check-<>-operators (beg end)
5193 ;; This is called from `after-change-functions' when
5194 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5195 ;; chars with paren syntax become part of another operator like "<<"
5196 ;; or ">=".
5198 ;; This function might do hidden buffer changes.
5200 (save-excursion
5201 (goto-char beg)
5202 (when (or (looking-at "[<>]")
5203 (< (skip-chars-backward "<>") 0))
5205 (goto-char beg)
5206 (c-beginning-of-current-token)
5207 (when (and (< (point) beg)
5208 (looking-at c-<>-multichar-token-regexp)
5209 (< beg (setq beg (match-end 0))))
5210 (while (progn (skip-chars-forward "^<>" beg)
5211 (< (point) beg))
5212 (c-clear-<>-pair-props)
5213 (forward-char))))
5215 (when (< beg end)
5216 (goto-char end)
5217 (when (or (looking-at "[<>]")
5218 (< (skip-chars-backward "<>") 0))
5220 (goto-char end)
5221 (c-beginning-of-current-token)
5222 (when (and (< (point) end)
5223 (looking-at c-<>-multichar-token-regexp)
5224 (< end (setq end (match-end 0))))
5225 (while (progn (skip-chars-forward "^<>" end)
5226 (< (point) end))
5227 (c-clear-<>-pair-props)
5228 (forward-char)))))))
5232 ;; Handling of small scale constructs like types and names.
5234 ;; Dynamically bound variable that instructs `c-forward-type' to also
5235 ;; treat possible types (i.e. those that it normally returns 'maybe or
5236 ;; 'found for) as actual types (and always return 'found for them).
5237 ;; This means that it records them in `c-record-type-identifiers' if
5238 ;; that is set, and that it adds them to `c-found-types'.
5239 (defvar c-promote-possible-types nil)
5241 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5242 ;; mark up successfully parsed arglists with paren syntax properties on
5243 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5244 ;; `c-type' property of each argument separating comma.
5246 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5247 ;; all arglists for side effects (i.e. recording types), otherwise it
5248 ;; exploits any existing paren syntax properties to quickly jump to the
5249 ;; end of already parsed arglists.
5251 ;; Marking up the arglists is not the default since doing that correctly
5252 ;; depends on a proper value for `c-restricted-<>-arglists'.
5253 (defvar c-parse-and-markup-<>-arglists nil)
5255 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5256 ;; not accept arglists that contain binary operators.
5258 ;; This is primarily used to handle C++ template arglists. C++
5259 ;; disambiguates them by checking whether the preceding name is a
5260 ;; template or not. We can't do that, so we assume it is a template
5261 ;; if it can be parsed as one. That usually works well since
5262 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5263 ;; in almost all cases would be pointless.
5265 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5266 ;; should let the comma separate the function arguments instead. And
5267 ;; in a context where the value of the expression is taken, e.g. in
5268 ;; "if (a < b || c > d)", it's probably not a template.
5269 (defvar c-restricted-<>-arglists nil)
5271 ;; Dynamically bound variables that instructs
5272 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5273 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5274 ;; `c-forward-label' to record the ranges of all the type and
5275 ;; reference identifiers they encounter. They will build lists on
5276 ;; these variables where each element is a cons of the buffer
5277 ;; positions surrounding each identifier. This recording is only
5278 ;; activated when `c-record-type-identifiers' is non-nil.
5280 ;; All known types that can't be identifiers are recorded, and also
5281 ;; other possible types if `c-promote-possible-types' is set.
5282 ;; Recording is however disabled inside angle bracket arglists that
5283 ;; are encountered inside names and other angle bracket arglists.
5284 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5285 ;; instead.
5287 ;; Only the names in C++ template style references (e.g. "tmpl" in
5288 ;; "tmpl<a,b>::foo") are recorded as references, other references
5289 ;; aren't handled here.
5291 ;; `c-forward-label' records the label identifier(s) on
5292 ;; `c-record-ref-identifiers'.
5293 (defvar c-record-type-identifiers nil)
5294 (defvar c-record-ref-identifiers nil)
5296 ;; This variable will receive a cons cell of the range of the last
5297 ;; single identifier symbol stepped over by `c-forward-name' if it's
5298 ;; successful. This is the range that should be put on one of the
5299 ;; record lists above by the caller. It's assigned nil if there's no
5300 ;; such symbol in the name.
5301 (defvar c-last-identifier-range nil)
5303 (defmacro c-record-type-id (range)
5304 (if (eq (car-safe range) 'cons)
5305 ;; Always true.
5306 `(setq c-record-type-identifiers
5307 (cons ,range c-record-type-identifiers))
5308 `(let ((range ,range))
5309 (if range
5310 (setq c-record-type-identifiers
5311 (cons range c-record-type-identifiers))))))
5313 (defmacro c-record-ref-id (range)
5314 (if (eq (car-safe range) 'cons)
5315 ;; Always true.
5316 `(setq c-record-ref-identifiers
5317 (cons ,range c-record-ref-identifiers))
5318 `(let ((range ,range))
5319 (if range
5320 (setq c-record-ref-identifiers
5321 (cons range c-record-ref-identifiers))))))
5323 ;; Dynamically bound variable that instructs `c-forward-type' to
5324 ;; record the ranges of types that only are found. Behaves otherwise
5325 ;; like `c-record-type-identifiers'.
5326 (defvar c-record-found-types nil)
5328 (defmacro c-forward-keyword-prefixed-id (type)
5329 ;; Used internally in `c-forward-keyword-clause' to move forward
5330 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5331 ;; possibly is prefixed by keywords and their associated clauses.
5332 ;; Try with a type/name first to not trip up on those that begin
5333 ;; with a keyword. Return t if a known or found type is moved
5334 ;; over. The point is clobbered if nil is returned. If range
5335 ;; recording is enabled, the identifier is recorded on as a type
5336 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5338 ;; This macro might do hidden buffer changes.
5339 `(let (res)
5340 (while (if (setq res ,(if (eq type 'type)
5341 `(c-forward-type)
5342 `(c-forward-name)))
5344 (and (looking-at c-keywords-regexp)
5345 (c-forward-keyword-clause 1))))
5346 (when (memq res '(t known found prefix))
5347 ,(when (eq type 'ref)
5348 `(when c-record-type-identifiers
5349 (c-record-ref-id c-last-identifier-range)))
5350 t)))
5352 (defmacro c-forward-id-comma-list (type update-safe-pos)
5353 ;; Used internally in `c-forward-keyword-clause' to move forward
5354 ;; over a comma separated list of types or names using
5355 ;; `c-forward-keyword-prefixed-id'.
5357 ;; This macro might do hidden buffer changes.
5358 `(while (and (progn
5359 ,(when update-safe-pos
5360 `(setq safe-pos (point)))
5361 (eq (char-after) ?,))
5362 (progn
5363 (forward-char)
5364 (c-forward-syntactic-ws)
5365 (c-forward-keyword-prefixed-id ,type)))))
5367 (defun c-forward-keyword-clause (match)
5368 ;; Submatch MATCH in the current match data is assumed to surround a
5369 ;; token. If it's a keyword, move over it and any immediately
5370 ;; following clauses associated with it, stopping at the start of
5371 ;; the next token. t is returned in that case, otherwise the point
5372 ;; stays and nil is returned. The kind of clauses that are
5373 ;; recognized are those specified by `c-type-list-kwds',
5374 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5375 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5376 ;; and `c-<>-arglist-kwds'.
5378 ;; This function records identifier ranges on
5379 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5380 ;; `c-record-type-identifiers' is non-nil.
5382 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5383 ;; apply directly after the keyword, the type list is moved over
5384 ;; only when there is no unaccounted token before it (i.e. a token
5385 ;; that isn't moved over due to some other keyword list). The
5386 ;; identifier ranges in the list are still recorded if that should
5387 ;; be done, though.
5389 ;; This function might do hidden buffer changes.
5391 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5392 ;; The call to `c-forward-<>-arglist' below is made after
5393 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5394 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5395 ;; should therefore be nil.
5396 (c-parse-and-markup-<>-arglists t)
5397 c-restricted-<>-arglists)
5399 (when kwd-sym
5400 (goto-char (match-end match))
5401 (c-forward-syntactic-ws)
5402 (setq safe-pos (point))
5404 (cond
5405 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5406 (c-forward-keyword-prefixed-id type))
5407 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5408 (c-forward-id-comma-list type t))
5410 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5411 (c-forward-keyword-prefixed-id ref))
5412 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5413 (c-forward-id-comma-list ref t))
5415 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5416 (eq (char-after) ?\())
5417 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5419 (forward-char)
5420 (when (and (setq pos (c-up-list-forward))
5421 (eq (char-before pos) ?\)))
5422 (when (and c-record-type-identifiers
5423 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5424 ;; Use `c-forward-type' on every identifier we can find
5425 ;; inside the paren, to record the types.
5426 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5427 (goto-char (match-beginning 0))
5428 (unless (c-forward-type)
5429 (looking-at c-symbol-key) ; Always matches.
5430 (goto-char (match-end 0)))))
5432 (goto-char pos)
5433 (c-forward-syntactic-ws)
5434 (setq safe-pos (point))))
5436 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5437 (eq (char-after) ?<)
5438 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5439 (c-forward-syntactic-ws)
5440 (setq safe-pos (point)))
5442 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5443 (not (looking-at c-symbol-start))
5444 (c-safe (c-forward-sexp) t))
5445 (c-forward-syntactic-ws)
5446 (setq safe-pos (point))))
5448 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5449 (if (eq (char-after) ?:)
5450 ;; If we are at the colon already, we move over the type
5451 ;; list after it.
5452 (progn
5453 (forward-char)
5454 (c-forward-syntactic-ws)
5455 (when (c-forward-keyword-prefixed-id type)
5456 (c-forward-id-comma-list type t)))
5457 ;; Not at the colon, so stop here. But the identifier
5458 ;; ranges in the type list later on should still be
5459 ;; recorded.
5460 (and c-record-type-identifiers
5461 (progn
5462 ;; If a keyword matched both one of the types above and
5463 ;; this one, we match `c-colon-type-list-re' after the
5464 ;; clause matched above.
5465 (goto-char safe-pos)
5466 (looking-at c-colon-type-list-re))
5467 (progn
5468 (goto-char (match-end 0))
5469 (c-forward-syntactic-ws)
5470 (c-forward-keyword-prefixed-id type))
5471 ;; There's a type after the `c-colon-type-list-re' match
5472 ;; after a keyword in `c-colon-type-list-kwds'.
5473 (c-forward-id-comma-list type nil))))
5475 (goto-char safe-pos)
5476 t)))
5478 ;; cc-mode requires cc-fonts.
5479 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5481 (defun c-forward-<>-arglist (all-types)
5482 ;; The point is assumed to be at a "<". Try to treat it as the open
5483 ;; paren of an angle bracket arglist and move forward to the
5484 ;; corresponding ">". If successful, the point is left after the
5485 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5486 ;; returned. If ALL-TYPES is t then all encountered arguments in
5487 ;; the arglist that might be types are treated as found types.
5489 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5490 ;; function handles text properties on the angle brackets and argument
5491 ;; separating commas.
5493 ;; `c-restricted-<>-arglists' controls how lenient the template
5494 ;; arglist recognition should be.
5496 ;; This function records identifier ranges on
5497 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5498 ;; `c-record-type-identifiers' is non-nil.
5500 ;; This function might do hidden buffer changes.
5502 (let ((start (point))
5503 ;; If `c-record-type-identifiers' is set then activate
5504 ;; recording of any found types that constitute an argument in
5505 ;; the arglist.
5506 (c-record-found-types (if c-record-type-identifiers t)))
5507 (if (catch 'angle-bracket-arglist-escape
5508 (setq c-record-found-types
5509 (c-forward-<>-arglist-recur all-types)))
5510 (progn
5511 (when (consp c-record-found-types)
5512 (setq c-record-type-identifiers
5513 ;; `nconc' doesn't mind that the tail of
5514 ;; `c-record-found-types' is t.
5515 (nconc c-record-found-types c-record-type-identifiers)))
5516 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
5519 (goto-char start)
5520 nil)))
5522 (defun c-forward-<>-arglist-recur (all-types)
5523 ;; Recursive part of `c-forward-<>-arglist'.
5525 ;; This function might do hidden buffer changes.
5527 (let ((start (point)) res pos tmp
5528 ;; Cover this so that any recorded found type ranges are
5529 ;; automatically lost if it turns out to not be an angle
5530 ;; bracket arglist. It's propagated through the return value
5531 ;; on successful completion.
5532 (c-record-found-types c-record-found-types)
5533 ;; List that collects the positions after the argument
5534 ;; separating ',' in the arglist.
5535 arg-start-pos)
5536 ;; If the '<' has paren open syntax then we've marked it as an angle
5537 ;; bracket arglist before, so skip to the end.
5538 (if (and (not c-parse-and-markup-<>-arglists)
5539 (c-get-char-property (point) 'syntax-table))
5541 (progn
5542 (forward-char)
5543 (if (and (c-go-up-list-forward)
5544 (eq (char-before) ?>))
5546 ;; Got unmatched paren angle brackets. We don't clear the paren
5547 ;; syntax properties and retry, on the basis that it's very
5548 ;; unlikely that paren angle brackets become operators by code
5549 ;; manipulation. It's far more likely that it doesn't match due
5550 ;; to narrowing or some temporary change.
5551 (goto-char start)
5552 nil))
5554 (forward-char) ; Forward over the opening '<'.
5556 (unless (looking-at c-<-op-cont-regexp)
5557 ;; go forward one non-alphanumeric character (group) per iteration of
5558 ;; this loop.
5559 (while (and
5560 (progn
5561 (c-forward-syntactic-ws)
5562 (let ((orig-record-found-types c-record-found-types))
5563 (when (or (and c-record-type-identifiers all-types)
5564 (c-major-mode-is 'java-mode))
5565 ;; All encountered identifiers are types, so set the
5566 ;; promote flag and parse the type.
5567 (progn
5568 (c-forward-syntactic-ws)
5569 (if (looking-at "\\?")
5570 (forward-char)
5571 (when (looking-at c-identifier-start)
5572 (let ((c-promote-possible-types t)
5573 (c-record-found-types t))
5574 (c-forward-type))))
5576 (c-forward-syntactic-ws)
5578 (when (or (looking-at "extends")
5579 (looking-at "super"))
5580 (forward-word)
5581 (c-forward-syntactic-ws)
5582 (let ((c-promote-possible-types t)
5583 (c-record-found-types t))
5584 (c-forward-type)
5585 (c-forward-syntactic-ws))))))
5587 (setq pos (point)) ; e.g. first token inside the '<'
5589 ;; Note: These regexps exploit the match order in \| so
5590 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5591 (c-syntactic-re-search-forward
5592 ;; Stop on ',', '|', '&', '+' and '-' to catch
5593 ;; common binary operators that could be between
5594 ;; two comparison expressions "a<b" and "c>d".
5595 "[<;{},|+&-]\\|[>)]"
5596 nil t t))
5598 (cond
5599 ((eq (char-before) ?>)
5600 ;; Either an operator starting with '>' or the end of
5601 ;; the angle bracket arglist.
5603 (if (looking-at c->-op-cont-regexp)
5604 (progn
5605 (goto-char (match-end 0))
5606 t) ; Continue the loop.
5608 ;; The angle bracket arglist is finished.
5609 (when c-parse-and-markup-<>-arglists
5610 (while arg-start-pos
5611 (c-put-c-type-property (1- (car arg-start-pos))
5612 'c-<>-arg-sep)
5613 (setq arg-start-pos (cdr arg-start-pos)))
5614 (c-mark-<-as-paren start)
5615 (c-mark->-as-paren (1- (point))))
5616 (setq res t)
5617 nil)) ; Exit the loop.
5619 ((eq (char-before) ?<)
5620 ;; Either an operator starting with '<' or a nested arglist.
5621 (setq pos (point))
5622 (let (id-start id-end subres keyword-match)
5623 (cond
5624 ;; The '<' begins a multi-char operator.
5625 ((looking-at c-<-op-cont-regexp)
5626 (setq tmp (match-end 0))
5627 (goto-char (match-end 0)))
5628 ;; We're at a nested <.....>
5629 ((progn
5630 (setq tmp pos)
5631 (backward-char) ; to the '<'
5632 (and
5633 (save-excursion
5634 ;; There's always an identifier before an angle
5635 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5636 ;; or `c-<>-arglist-kwds'.
5637 (c-backward-syntactic-ws)
5638 (setq id-end (point))
5639 (c-simple-skip-symbol-backward)
5640 (when (or (setq keyword-match
5641 (looking-at c-opt-<>-sexp-key))
5642 (not (looking-at c-keywords-regexp)))
5643 (setq id-start (point))))
5644 (setq subres
5645 (let ((c-promote-possible-types t)
5646 (c-record-found-types t))
5647 (c-forward-<>-arglist-recur
5648 (and keyword-match
5649 (c-keyword-member
5650 (c-keyword-sym (match-string 1))
5651 'c-<>-type-kwds)))))))
5653 ;; It was an angle bracket arglist.
5654 (setq c-record-found-types subres)
5656 ;; Record the identifier before the template as a type
5657 ;; or reference depending on whether the arglist is last
5658 ;; in a qualified identifier.
5659 (when (and c-record-type-identifiers
5660 (not keyword-match))
5661 (if (and c-opt-identifier-concat-key
5662 (progn
5663 (c-forward-syntactic-ws)
5664 (looking-at c-opt-identifier-concat-key)))
5665 (c-record-ref-id (cons id-start id-end))
5666 (c-record-type-id (cons id-start id-end)))))
5668 ;; At a "less than" operator.
5670 (forward-char)
5672 t) ; carry on looping.
5674 ((and (not c-restricted-<>-arglists)
5675 (or (and (eq (char-before) ?&)
5676 (not (eq (char-after) ?&)))
5677 (eq (char-before) ?,)))
5678 ;; Just another argument. Record the position. The
5679 ;; type check stuff that made us stop at it is at
5680 ;; the top of the loop.
5681 (setq arg-start-pos (cons (point) arg-start-pos)))
5684 ;; Got a character that can't be in an angle bracket
5685 ;; arglist argument. Abort using `throw', since
5686 ;; it's useless to try to find a surrounding arglist
5687 ;; if we're nested.
5688 (throw 'angle-bracket-arglist-escape nil))))))
5689 (if res
5690 (or c-record-found-types t)))))
5692 (defun c-backward-<>-arglist (all-types &optional limit)
5693 ;; The point is assumed to be directly after a ">". Try to treat it
5694 ;; as the close paren of an angle bracket arglist and move back to
5695 ;; the corresponding "<". If successful, the point is left at
5696 ;; the "<" and t is returned, otherwise the point isn't moved and
5697 ;; nil is returned. ALL-TYPES is passed on to
5698 ;; `c-forward-<>-arglist'.
5700 ;; If the optional LIMIT is given, it bounds the backward search.
5701 ;; It's then assumed to be at a syntactically relevant position.
5703 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5704 ;; function for more details.
5706 (let ((start (point)))
5707 (backward-char)
5708 (if (and (not c-parse-and-markup-<>-arglists)
5709 (c-get-char-property (point) 'syntax-table))
5711 (if (and (c-go-up-list-backward)
5712 (eq (char-after) ?<))
5714 ;; See corresponding note in `c-forward-<>-arglist'.
5715 (goto-char start)
5716 nil)
5718 (while (progn
5719 (c-syntactic-skip-backward "^<;{}" limit t)
5721 (and
5722 (if (eq (char-before) ?<)
5724 ;; Stopped at bob or a char that isn't allowed in an
5725 ;; arglist, so we've failed.
5726 (goto-char start)
5727 nil)
5729 (if (> (point)
5730 (progn (c-beginning-of-current-token)
5731 (point)))
5732 ;; If we moved then the "<" was part of some
5733 ;; multicharacter token.
5736 (backward-char)
5737 (let ((beg-pos (point)))
5738 (if (c-forward-<>-arglist all-types)
5739 (cond ((= (point) start)
5740 ;; Matched the arglist. Break the while.
5741 (goto-char beg-pos)
5742 nil)
5743 ((> (point) start)
5744 ;; We started from a non-paren ">" inside an
5745 ;; arglist.
5746 (goto-char start)
5747 nil)
5749 ;; Matched a shorter arglist. Can be a nested
5750 ;; one so continue looking.
5751 (goto-char beg-pos)
5753 t))))))
5755 (/= (point) start))))
5757 (defun c-forward-name ()
5758 ;; Move forward over a complete name if at the beginning of one,
5759 ;; stopping at the next following token. A keyword, as such,
5760 ;; doesn't count as a name. If the point is not at something that
5761 ;; is recognized as a name then it stays put.
5763 ;; A name could be something as simple as "foo" in C or something as
5764 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5765 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5766 ;; int>::*volatile const" in C++ (this function is actually little
5767 ;; more than a `looking-at' call in all modes except those that,
5768 ;; like C++, have `c-recognize-<>-arglists' set).
5770 ;; Return
5771 ;; o - nil if no name is found;
5772 ;; o - 'template if it's an identifier ending with an angle bracket
5773 ;; arglist;
5774 ;; o - 'operator of it's an operator identifier;
5775 ;; o - t if it's some other kind of name.
5777 ;; This function records identifier ranges on
5778 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5779 ;; `c-record-type-identifiers' is non-nil.
5781 ;; This function might do hidden buffer changes.
5783 (let ((pos (point)) (start (point)) res id-start id-end
5784 ;; Turn off `c-promote-possible-types' here since we might
5785 ;; call `c-forward-<>-arglist' and we don't want it to promote
5786 ;; every suspect thing in the arglist to a type. We're
5787 ;; typically called from `c-forward-type' in this case, and
5788 ;; the caller only wants the top level type that it finds to
5789 ;; be promoted.
5790 c-promote-possible-types)
5791 (while
5792 (and
5793 (looking-at c-identifier-key)
5795 (progn
5796 ;; Check for keyword. We go to the last symbol in
5797 ;; `c-identifier-key' first.
5798 (goto-char (setq id-end (match-end 0)))
5799 (c-simple-skip-symbol-backward)
5800 (setq id-start (point))
5802 (if (looking-at c-keywords-regexp)
5803 (when (and (c-major-mode-is 'c++-mode)
5804 (looking-at
5805 (cc-eval-when-compile
5806 (concat "\\(operator\\|\\(template\\)\\)"
5807 "\\(" (c-lang-const c-nonsymbol-key c++)
5808 "\\|$\\)")))
5809 (if (match-beginning 2)
5810 ;; "template" is only valid inside an
5811 ;; identifier if preceded by "::".
5812 (save-excursion
5813 (c-backward-syntactic-ws)
5814 (and (c-safe (backward-char 2) t)
5815 (looking-at "::")))
5818 ;; Handle a C++ operator or template identifier.
5819 (goto-char id-end)
5820 (c-forward-syntactic-ws)
5821 (cond ((eq (char-before id-end) ?e)
5822 ;; Got "... ::template".
5823 (let ((subres (c-forward-name)))
5824 (when subres
5825 (setq pos (point)
5826 res subres))))
5828 ((looking-at c-identifier-start)
5829 ;; Got a cast operator.
5830 (when (c-forward-type)
5831 (setq pos (point)
5832 res 'operator)
5833 ;; Now we should match a sequence of either
5834 ;; '*', '&' or a name followed by ":: *",
5835 ;; where each can be followed by a sequence
5836 ;; of `c-opt-type-modifier-key'.
5837 (while (cond ((looking-at "[*&]")
5838 (goto-char (match-end 0))
5840 ((looking-at c-identifier-start)
5841 (and (c-forward-name)
5842 (looking-at "::")
5843 (progn
5844 (goto-char (match-end 0))
5845 (c-forward-syntactic-ws)
5846 (eq (char-after) ?*))
5847 (progn
5848 (forward-char)
5849 t))))
5850 (while (progn
5851 (c-forward-syntactic-ws)
5852 (setq pos (point))
5853 (looking-at c-opt-type-modifier-key))
5854 (goto-char (match-end 1))))))
5856 ((looking-at c-overloadable-operators-regexp)
5857 ;; Got some other operator.
5858 (setq c-last-identifier-range
5859 (cons (point) (match-end 0)))
5860 (goto-char (match-end 0))
5861 (c-forward-syntactic-ws)
5862 (setq pos (point)
5863 res 'operator)))
5865 nil)
5867 ;; `id-start' is equal to `id-end' if we've jumped over
5868 ;; an identifier that doesn't end with a symbol token.
5869 ;; That can occur e.g. for Java import directives on the
5870 ;; form "foo.bar.*".
5871 (when (and id-start (/= id-start id-end))
5872 (setq c-last-identifier-range
5873 (cons id-start id-end)))
5874 (goto-char id-end)
5875 (c-forward-syntactic-ws)
5876 (setq pos (point)
5877 res t)))
5879 (progn
5880 (goto-char pos)
5881 (when (or c-opt-identifier-concat-key
5882 c-recognize-<>-arglists)
5884 (cond
5885 ((and c-opt-identifier-concat-key
5886 (looking-at c-opt-identifier-concat-key))
5887 ;; Got a concatenated identifier. This handles the
5888 ;; cases with tricky syntactic whitespace that aren't
5889 ;; covered in `c-identifier-key'.
5890 (goto-char (match-end 0))
5891 (c-forward-syntactic-ws)
5894 ((and c-recognize-<>-arglists
5895 (eq (char-after) ?<))
5896 ;; Maybe an angle bracket arglist.
5897 (when (let ((c-record-type-identifiers t)
5898 (c-record-found-types t))
5899 (c-forward-<>-arglist nil))
5901 (c-add-type start (1+ pos))
5902 (c-forward-syntactic-ws)
5903 (setq pos (point)
5904 c-last-identifier-range nil)
5906 (if (and c-opt-identifier-concat-key
5907 (looking-at c-opt-identifier-concat-key))
5909 ;; Continue if there's an identifier concatenation
5910 ;; operator after the template argument.
5911 (progn
5912 (when (and c-record-type-identifiers id-start)
5913 (c-record-ref-id (cons id-start id-end)))
5914 (forward-char 2)
5915 (c-forward-syntactic-ws)
5918 (when (and c-record-type-identifiers id-start)
5919 (c-record-type-id (cons id-start id-end)))
5920 (setq res 'template)
5921 nil)))
5922 )))))
5924 (goto-char pos)
5925 res))
5927 (defun c-forward-type (&optional brace-block-too)
5928 ;; Move forward over a type spec if at the beginning of one,
5929 ;; stopping at the next following token. The keyword "typedef"
5930 ;; isn't part of a type spec here.
5932 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
5933 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
5934 ;; The current (2009-03-10) intention is to convert all uses of
5935 ;; `c-forward-type' to call with this parameter set, then to
5936 ;; eliminate it.
5938 ;; Return
5939 ;; o - t if it's a known type that can't be a name or other
5940 ;; expression;
5941 ;; o - 'known if it's an otherwise known type (according to
5942 ;; `*-font-lock-extra-types');
5943 ;; o - 'prefix if it's a known prefix of a type;
5944 ;; o - 'found if it's a type that matches one in `c-found-types';
5945 ;; o - 'maybe if it's an identfier that might be a type; or
5946 ;; o - nil if it can't be a type (the point isn't moved then).
5948 ;; The point is assumed to be at the beginning of a token.
5950 ;; Note that this function doesn't skip past the brace definition
5951 ;; that might be considered part of the type, e.g.
5952 ;; "enum {a, b, c} foo".
5954 ;; This function records identifier ranges on
5955 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5956 ;; `c-record-type-identifiers' is non-nil.
5958 ;; This function might do hidden buffer changes.
5959 (when (and c-recognize-<>-arglists
5960 (looking-at "<"))
5961 (c-forward-<>-arglist t)
5962 (c-forward-syntactic-ws))
5964 (let ((start (point)) pos res name-res id-start id-end id-range)
5966 ;; Skip leading type modifiers. If any are found we know it's a
5967 ;; prefix of a type.
5968 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
5969 (while (looking-at c-opt-type-modifier-key)
5970 (goto-char (match-end 1))
5971 (c-forward-syntactic-ws)
5972 (setq res 'prefix)))
5974 (cond
5975 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
5976 ; "typedef".
5977 (goto-char (match-end 1))
5978 (c-forward-syntactic-ws)
5979 (setq pos (point))
5981 (setq name-res (c-forward-name))
5982 (setq res (not (null name-res)))
5983 (when (eq name-res t)
5984 ;; In many languages the name can be used without the
5985 ;; prefix, so we add it to `c-found-types'.
5986 (c-add-type pos (point))
5987 (when (and c-record-type-identifiers
5988 c-last-identifier-range)
5989 (c-record-type-id c-last-identifier-range)))
5990 (when (and brace-block-too
5991 (memq res '(t nil))
5992 (eq (char-after) ?\{)
5993 (save-excursion
5994 (c-safe
5995 (progn (c-forward-sexp)
5996 (c-forward-syntactic-ws)
5997 (setq pos (point))))))
5998 (goto-char pos)
5999 (setq res t))
6000 (unless res (goto-char start))) ; invalid syntax
6002 ((progn
6003 (setq pos nil)
6004 (if (looking-at c-identifier-start)
6005 (save-excursion
6006 (setq id-start (point)
6007 name-res (c-forward-name))
6008 (when name-res
6009 (setq id-end (point)
6010 id-range c-last-identifier-range))))
6011 (and (cond ((looking-at c-primitive-type-key)
6012 (setq res t))
6013 ((c-with-syntax-table c-identifier-syntax-table
6014 (looking-at c-known-type-key))
6015 (setq res 'known)))
6016 (or (not id-end)
6017 (>= (save-excursion
6018 (save-match-data
6019 (goto-char (match-end 1))
6020 (c-forward-syntactic-ws)
6021 (setq pos (point))))
6022 id-end)
6023 (setq res nil))))
6024 ;; Looking at a primitive or known type identifier. We've
6025 ;; checked for a name first so that we don't go here if the
6026 ;; known type match only is a prefix of another name.
6028 (setq id-end (match-end 1))
6030 (when (and c-record-type-identifiers
6031 (or c-promote-possible-types (eq res t)))
6032 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6034 (if (and c-opt-type-component-key
6035 (save-match-data
6036 (looking-at c-opt-type-component-key)))
6037 ;; There might be more keywords for the type.
6038 (let (safe-pos)
6039 (c-forward-keyword-clause 1)
6040 (while (progn
6041 (setq safe-pos (point))
6042 (looking-at c-opt-type-component-key))
6043 (when (and c-record-type-identifiers
6044 (looking-at c-primitive-type-key))
6045 (c-record-type-id (cons (match-beginning 1)
6046 (match-end 1))))
6047 (c-forward-keyword-clause 1))
6048 (if (looking-at c-primitive-type-key)
6049 (progn
6050 (when c-record-type-identifiers
6051 (c-record-type-id (cons (match-beginning 1)
6052 (match-end 1))))
6053 (c-forward-keyword-clause 1)
6054 (setq res t))
6055 (goto-char safe-pos)
6056 (setq res 'prefix)))
6057 (unless (save-match-data (c-forward-keyword-clause 1))
6058 (if pos
6059 (goto-char pos)
6060 (goto-char (match-end 1))
6061 (c-forward-syntactic-ws)))))
6063 (name-res
6064 (cond ((eq name-res t)
6065 ;; A normal identifier.
6066 (goto-char id-end)
6067 (if (or res c-promote-possible-types)
6068 (progn
6069 (c-add-type id-start id-end)
6070 (when (and c-record-type-identifiers id-range)
6071 (c-record-type-id id-range))
6072 (unless res
6073 (setq res 'found)))
6074 (setq res (if (c-check-type id-start id-end)
6075 ;; It's an identifier that has been used as
6076 ;; a type somewhere else.
6077 'found
6078 ;; It's an identifier that might be a type.
6079 'maybe))))
6080 ((eq name-res 'template)
6081 ;; A template is a type.
6082 (goto-char id-end)
6083 (setq res t))
6085 ;; Otherwise it's an operator identifier, which is not a type.
6086 (goto-char start)
6087 (setq res nil)))))
6089 (when res
6090 ;; Skip trailing type modifiers. If any are found we know it's
6091 ;; a type.
6092 (when c-opt-type-modifier-key
6093 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
6094 (goto-char (match-end 1))
6095 (c-forward-syntactic-ws)
6096 (setq res t)))
6097 ;; Step over any type suffix operator. Do not let the existence
6098 ;; of these alter the classification of the found type, since
6099 ;; these operators typically are allowed in normal expressions
6100 ;; too.
6101 (when c-opt-type-suffix-key
6102 (while (looking-at c-opt-type-suffix-key)
6103 (goto-char (match-end 1))
6104 (c-forward-syntactic-ws)))
6106 (when c-opt-type-concat-key ; Only/mainly for pike.
6107 ;; Look for a trailing operator that concatenates the type
6108 ;; with a following one, and if so step past that one through
6109 ;; a recursive call. Note that we don't record concatenated
6110 ;; types in `c-found-types' - it's the component types that
6111 ;; are recorded when appropriate.
6112 (setq pos (point))
6113 (let* ((c-promote-possible-types (or (memq res '(t known))
6114 c-promote-possible-types))
6115 ;; If we can't promote then set `c-record-found-types' so that
6116 ;; we can merge in the types from the second part afterwards if
6117 ;; it turns out to be a known type there.
6118 (c-record-found-types (and c-record-type-identifiers
6119 (not c-promote-possible-types)))
6120 subres)
6121 (if (and (looking-at c-opt-type-concat-key)
6123 (progn
6124 (goto-char (match-end 1))
6125 (c-forward-syntactic-ws)
6126 (setq subres (c-forward-type))))
6128 (progn
6129 ;; If either operand certainly is a type then both are, but we
6130 ;; don't let the existence of the operator itself promote two
6131 ;; uncertain types to a certain one.
6132 (cond ((eq res t))
6133 ((eq subres t)
6134 (unless (eq name-res 'template)
6135 (c-add-type id-start id-end))
6136 (when (and c-record-type-identifiers id-range)
6137 (c-record-type-id id-range))
6138 (setq res t))
6139 ((eq res 'known))
6140 ((eq subres 'known)
6141 (setq res 'known))
6142 ((eq res 'found))
6143 ((eq subres 'found)
6144 (setq res 'found))
6146 (setq res 'maybe)))
6148 (when (and (eq res t)
6149 (consp c-record-found-types))
6150 ;; Merge in the ranges of any types found by the second
6151 ;; `c-forward-type'.
6152 (setq c-record-type-identifiers
6153 ;; `nconc' doesn't mind that the tail of
6154 ;; `c-record-found-types' is t.
6155 (nconc c-record-found-types
6156 c-record-type-identifiers))))
6158 (goto-char pos))))
6160 (when (and c-record-found-types (memq res '(known found)) id-range)
6161 (setq c-record-found-types
6162 (cons id-range c-record-found-types))))
6164 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6166 res))
6168 (defun c-forward-annotation ()
6169 ;; Used for Java code only at the moment. Assumes point is on the
6170 ;; @, moves forward an annotation. returns nil if there is no
6171 ;; annotation at point.
6172 (and (looking-at "@")
6173 (progn (forward-char) t)
6174 (c-forward-type)
6175 (progn (c-forward-syntactic-ws) t)
6176 (if (looking-at "(")
6177 (c-go-list-forward)
6178 t)))
6181 ;; Handling of large scale constructs like statements and declarations.
6183 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6184 ;; defsubst or perhaps even a defun, but it contains lots of free
6185 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6186 (defmacro c-fdoc-shift-type-backward (&optional short)
6187 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6188 ;; of types when parsing a declaration, which means that it
6189 ;; sometimes consumes the identifier in the declaration as a type.
6190 ;; This is used to "backtrack" and make the last type be treated as
6191 ;; an identifier instead.
6192 `(progn
6193 ,(unless short
6194 ;; These identifiers are bound only in the inner let.
6195 '(setq identifier-type at-type
6196 identifier-start type-start
6197 got-parens nil
6198 got-identifier t
6199 got-suffix t
6200 got-suffix-after-parens id-start
6201 paren-depth 0))
6203 (if (setq at-type (if (eq backup-at-type 'prefix)
6205 backup-at-type))
6206 (setq type-start backup-type-start
6207 id-start backup-id-start)
6208 (setq type-start start-pos
6209 id-start start-pos))
6211 ;; When these flags already are set we've found specifiers that
6212 ;; unconditionally signal these attributes - backtracking doesn't
6213 ;; change that. So keep them set in that case.
6214 (or at-type-decl
6215 (setq at-type-decl backup-at-type-decl))
6216 (or maybe-typeless
6217 (setq maybe-typeless backup-maybe-typeless))
6219 ,(unless short
6220 ;; This identifier is bound only in the inner let.
6221 '(setq start id-start))))
6223 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6224 ;; Move forward over a declaration or a cast if at the start of one.
6225 ;; The point is assumed to be at the start of some token. Nil is
6226 ;; returned if no declaration or cast is recognized, and the point
6227 ;; is clobbered in that case.
6229 ;; If a declaration is parsed:
6231 ;; The point is left at the first token after the first complete
6232 ;; declarator, if there is one. The return value is a cons where
6233 ;; the car is the position of the first token in the declarator. (See
6234 ;; below for the cdr.)
6235 ;; Some examples:
6237 ;; void foo (int a, char *b) stuff ...
6238 ;; car ^ ^ point
6239 ;; float (*a)[], b;
6240 ;; car ^ ^ point
6241 ;; unsigned int a = c_style_initializer, b;
6242 ;; car ^ ^ point
6243 ;; unsigned int a (cplusplus_style_initializer), b;
6244 ;; car ^ ^ point (might change)
6245 ;; class Foo : public Bar {}
6246 ;; car ^ ^ point
6247 ;; class PikeClass (int a, string b) stuff ...
6248 ;; car ^ ^ point
6249 ;; enum bool;
6250 ;; car ^ ^ point
6251 ;; enum bool flag;
6252 ;; car ^ ^ point
6253 ;; void cplusplus_function (int x) throw (Bad);
6254 ;; car ^ ^ point
6255 ;; Foo::Foo (int b) : Base (b) {}
6256 ;; car ^ ^ point
6258 ;; The cdr of the return value is non-nil when a
6259 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6260 ;; Specifically it is a dotted pair (A . B) where B is t when a
6261 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6262 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6263 ;; specifier is present. I.e., (some of) the declared
6264 ;; identifier(s) are types.
6266 ;; If a cast is parsed:
6268 ;; The point is left at the first token after the closing paren of
6269 ;; the cast. The return value is `cast'. Note that the start
6270 ;; position must be at the first token inside the cast parenthesis
6271 ;; to recognize it.
6273 ;; PRECEDING-TOKEN-END is the first position after the preceding
6274 ;; token, i.e. on the other side of the syntactic ws from the point.
6275 ;; Use a value less than or equal to (point-min) if the point is at
6276 ;; the first token in (the visible part of) the buffer.
6278 ;; CONTEXT is a symbol that describes the context at the point:
6279 ;; 'decl In a comma-separated declaration context (typically
6280 ;; inside a function declaration arglist).
6281 ;; '<> In an angle bracket arglist.
6282 ;; 'arglist Some other type of arglist.
6283 ;; nil Some other context or unknown context. Includes
6284 ;; within the parens of an if, for, ... construct.
6286 ;; LAST-CAST-END is the first token after the closing paren of a
6287 ;; preceding cast, or nil if none is known. If
6288 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6289 ;; the position after the closest preceding call where a cast was
6290 ;; matched. In that case it's used to discover chains of casts like
6291 ;; "(a) (b) c".
6293 ;; This function records identifier ranges on
6294 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6295 ;; `c-record-type-identifiers' is non-nil.
6297 ;; This function might do hidden buffer changes.
6299 (let (;; `start-pos' is used below to point to the start of the
6300 ;; first type, i.e. after any leading specifiers. It might
6301 ;; also point at the beginning of the preceding syntactic
6302 ;; whitespace.
6303 (start-pos (point))
6304 ;; Set to the result of `c-forward-type'.
6305 at-type
6306 ;; The position of the first token in what we currently
6307 ;; believe is the type in the declaration or cast, after any
6308 ;; specifiers and their associated clauses.
6309 type-start
6310 ;; The position of the first token in what we currently
6311 ;; believe is the declarator for the first identifier. Set
6312 ;; when the type is found, and moved forward over any
6313 ;; `c-decl-hangon-kwds' and their associated clauses that
6314 ;; occurs after the type.
6315 id-start
6316 ;; These store `at-type', `type-start' and `id-start' of the
6317 ;; identifier before the one in those variables. The previous
6318 ;; identifier might turn out to be the real type in a
6319 ;; declaration if the last one has to be the declarator in it.
6320 ;; If `backup-at-type' is nil then the other variables have
6321 ;; undefined values.
6322 backup-at-type backup-type-start backup-id-start
6323 ;; Set if we've found a specifier (apart from "typedef") that makes
6324 ;; the defined identifier(s) types.
6325 at-type-decl
6326 ;; Set if we've a "typedef" keyword.
6327 at-typedef
6328 ;; Set if we've found a specifier that can start a declaration
6329 ;; where there's no type.
6330 maybe-typeless
6331 ;; If a specifier is found that also can be a type prefix,
6332 ;; these flags are set instead of those above. If we need to
6333 ;; back up an identifier, they are copied to the real flag
6334 ;; variables. Thus they only take effect if we fail to
6335 ;; interpret it as a type.
6336 backup-at-type-decl backup-maybe-typeless
6337 ;; Whether we've found a declaration or a cast. We might know
6338 ;; this before we've found the type in it. It's 'ids if we've
6339 ;; found two consecutive identifiers (usually a sure sign, but
6340 ;; we should allow that in labels too), and t if we've found a
6341 ;; specifier keyword (a 100% sure sign).
6342 at-decl-or-cast
6343 ;; Set when we need to back up to parse this as a declaration
6344 ;; but not as a cast.
6345 backup-if-not-cast
6346 ;; For casts, the return position.
6347 cast-end
6348 ;; Save `c-record-type-identifiers' and
6349 ;; `c-record-ref-identifiers' since ranges are recorded
6350 ;; speculatively and should be thrown away if it turns out
6351 ;; that it isn't a declaration or cast.
6352 (save-rec-type-ids c-record-type-identifiers)
6353 (save-rec-ref-ids c-record-ref-identifiers))
6355 (while (c-forward-annotation)
6356 (c-forward-syntactic-ws))
6358 ;; Check for a type. Unknown symbols are treated as possible
6359 ;; types, but they could also be specifiers disguised through
6360 ;; macros like __INLINE__, so we recognize both types and known
6361 ;; specifiers after them too.
6362 (while
6363 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6365 ;; Look for a specifier keyword clause.
6366 (when (or (looking-at c-prefix-spec-kwds-re)
6367 (and (c-major-mode-is 'java-mode)
6368 (looking-at "@[A-Za-z0-9]+")))
6369 (if (looking-at c-typedef-key)
6370 (setq at-typedef t))
6371 (setq kwd-sym (c-keyword-sym (match-string 1)))
6372 (save-excursion
6373 (c-forward-keyword-clause 1)
6374 (setq kwd-clause-end (point))))
6376 (when (setq found-type (c-forward-type t)) ; brace-block-too
6377 ;; Found a known or possible type or a prefix of a known type.
6379 (when at-type
6380 ;; Got two identifiers with nothing but whitespace
6381 ;; between them. That can only happen in declarations.
6382 (setq at-decl-or-cast 'ids)
6384 (when (eq at-type 'found)
6385 ;; If the previous identifier is a found type we
6386 ;; record it as a real one; it might be some sort of
6387 ;; alias for a prefix like "unsigned".
6388 (save-excursion
6389 (goto-char type-start)
6390 (let ((c-promote-possible-types t))
6391 (c-forward-type)))))
6393 (setq backup-at-type at-type
6394 backup-type-start type-start
6395 backup-id-start id-start
6396 at-type found-type
6397 type-start start
6398 id-start (point)
6399 ;; The previous ambiguous specifier/type turned out
6400 ;; to be a type since we've parsed another one after
6401 ;; it, so clear these backup flags.
6402 backup-at-type-decl nil
6403 backup-maybe-typeless nil))
6405 (if kwd-sym
6406 (progn
6407 ;; Handle known specifier keywords and
6408 ;; `c-decl-hangon-kwds' which can occur after known
6409 ;; types.
6411 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6412 ;; It's a hang-on keyword that can occur anywhere.
6413 (progn
6414 (setq at-decl-or-cast t)
6415 (if at-type
6416 ;; Move the identifier start position if
6417 ;; we've passed a type.
6418 (setq id-start kwd-clause-end)
6419 ;; Otherwise treat this as a specifier and
6420 ;; move the fallback position.
6421 (setq start-pos kwd-clause-end))
6422 (goto-char kwd-clause-end))
6424 ;; It's an ordinary specifier so we know that
6425 ;; anything before this can't be the type.
6426 (setq backup-at-type nil
6427 start-pos kwd-clause-end)
6429 (if found-type
6430 ;; It's ambiguous whether this keyword is a
6431 ;; specifier or a type prefix, so set the backup
6432 ;; flags. (It's assumed that `c-forward-type'
6433 ;; moved further than `c-forward-keyword-clause'.)
6434 (progn
6435 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6436 (setq backup-at-type-decl t))
6437 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6438 (setq backup-maybe-typeless t)))
6440 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6441 ;; This test only happens after we've scanned a type.
6442 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6443 (setq at-type-decl t))
6444 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6445 (setq maybe-typeless t))
6447 ;; Haven't matched a type so it's an umambiguous
6448 ;; specifier keyword and we know we're in a
6449 ;; declaration.
6450 (setq at-decl-or-cast t)
6452 (goto-char kwd-clause-end))))
6454 ;; If the type isn't known we continue so that we'll jump
6455 ;; over all specifiers and type identifiers. The reason
6456 ;; to do this for a known type prefix is to make things
6457 ;; like "unsigned INT16" work.
6458 (and found-type (not (eq found-type t))))))
6460 (cond
6461 ((eq at-type t)
6462 ;; If a known type was found, we still need to skip over any
6463 ;; hangon keyword clauses after it. Otherwise it has already
6464 ;; been done in the loop above.
6465 (while (looking-at c-decl-hangon-key)
6466 (c-forward-keyword-clause 1))
6467 (setq id-start (point)))
6469 ((eq at-type 'prefix)
6470 ;; A prefix type is itself a primitive type when it's not
6471 ;; followed by another type.
6472 (setq at-type t))
6474 ((not at-type)
6475 ;; Got no type but set things up to continue anyway to handle
6476 ;; the various cases when a declaration doesn't start with a
6477 ;; type.
6478 (setq id-start start-pos))
6480 ((and (eq at-type 'maybe)
6481 (c-major-mode-is 'c++-mode))
6482 ;; If it's C++ then check if the last "type" ends on the form
6483 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6484 ;; (con|de)structor.
6485 (save-excursion
6486 (let (name end-2 end-1)
6487 (goto-char id-start)
6488 (c-backward-syntactic-ws)
6489 (setq end-2 (point))
6490 (when (and
6491 (c-simple-skip-symbol-backward)
6492 (progn
6493 (setq name
6494 (buffer-substring-no-properties (point) end-2))
6495 ;; Cheating in the handling of syntactic ws below.
6496 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6497 (progn
6498 (setq end-1 (point))
6499 (c-simple-skip-symbol-backward))
6500 (>= (point) type-start)
6501 (equal (buffer-substring-no-properties (point) end-1)
6502 name))
6503 ;; It is a (con|de)structor name. In that case the
6504 ;; declaration is typeless so zap out any preceding
6505 ;; identifier(s) that we might have taken as types.
6506 (goto-char type-start)
6507 (setq at-type nil
6508 backup-at-type nil
6509 id-start type-start))))))
6511 ;; Check for and step over a type decl expression after the thing
6512 ;; that is or might be a type. This can't be skipped since we
6513 ;; need the correct end position of the declarator for
6514 ;; `max-type-decl-end-*'.
6515 (let ((start (point)) (paren-depth 0) pos
6516 ;; True if there's a non-open-paren match of
6517 ;; `c-type-decl-prefix-key'.
6518 got-prefix
6519 ;; True if the declarator is surrounded by a parenthesis pair.
6520 got-parens
6521 ;; True if there is an identifier in the declarator.
6522 got-identifier
6523 ;; True if there's a non-close-paren match of
6524 ;; `c-type-decl-suffix-key'.
6525 got-suffix
6526 ;; True if there's a prefix match outside the outermost
6527 ;; paren pair that surrounds the declarator.
6528 got-prefix-before-parens
6529 ;; True if there's a suffix match outside the outermost
6530 ;; paren pair that surrounds the declarator. The value is
6531 ;; the position of the first suffix match.
6532 got-suffix-after-parens
6533 ;; True if we've parsed the type decl to a token that is
6534 ;; known to end declarations in this context.
6535 at-decl-end
6536 ;; The earlier values of `at-type' and `type-start' if we've
6537 ;; shifted the type backwards.
6538 identifier-type identifier-start
6539 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6540 ;; turn it off during the name skipping below to avoid
6541 ;; getting `c-type' properties that might be bogus. That
6542 ;; can happen since we don't know if
6543 ;; `c-restricted-<>-arglists' will be correct inside the
6544 ;; arglist paren that gets entered.
6545 c-parse-and-markup-<>-arglists)
6547 (goto-char id-start)
6549 ;; Skip over type decl prefix operators. (Note similar code in
6550 ;; `c-font-lock-declarators'.)
6551 (while (and (looking-at c-type-decl-prefix-key)
6552 (if (and (c-major-mode-is 'c++-mode)
6553 (match-beginning 3))
6554 ;; If the second submatch matches in C++ then
6555 ;; we're looking at an identifier that's a
6556 ;; prefix only if it specifies a member pointer.
6557 (when (setq got-identifier (c-forward-name))
6558 (if (looking-at "\\(::\\)")
6559 ;; We only check for a trailing "::" and
6560 ;; let the "*" that should follow be
6561 ;; matched in the next round.
6562 (progn (setq got-identifier nil) t)
6563 ;; It turned out to be the real identifier,
6564 ;; so stop.
6565 nil))
6568 (if (eq (char-after) ?\()
6569 (progn
6570 (setq paren-depth (1+ paren-depth))
6571 (forward-char))
6572 (unless got-prefix-before-parens
6573 (setq got-prefix-before-parens (= paren-depth 0)))
6574 (setq got-prefix t)
6575 (goto-char (match-end 1)))
6576 (c-forward-syntactic-ws))
6578 (setq got-parens (> paren-depth 0))
6580 ;; Skip over an identifier.
6581 (or got-identifier
6582 (and (looking-at c-identifier-start)
6583 (setq got-identifier (c-forward-name))))
6585 ;; Skip over type decl suffix operators.
6586 (while (if (looking-at c-type-decl-suffix-key)
6588 (if (eq (char-after) ?\))
6589 (when (> paren-depth 0)
6590 (setq paren-depth (1- paren-depth))
6591 (forward-char)
6593 (when (if (save-match-data (looking-at "\\s\("))
6594 (c-safe (c-forward-sexp 1) t)
6595 (goto-char (match-end 1))
6597 (when (and (not got-suffix-after-parens)
6598 (= paren-depth 0))
6599 (setq got-suffix-after-parens (match-beginning 0)))
6600 (setq got-suffix t)))
6602 ;; No suffix matched. We might have matched the
6603 ;; identifier as a type and the open paren of a
6604 ;; function arglist as a type decl prefix. In that
6605 ;; case we should "backtrack": Reinterpret the last
6606 ;; type as the identifier, move out of the arglist and
6607 ;; continue searching for suffix operators.
6609 ;; Do this even if there's no preceding type, to cope
6610 ;; with old style function declarations in K&R C,
6611 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6612 ;; style declarations. That isn't applicable in an
6613 ;; arglist context, though.
6614 (when (and (= paren-depth 1)
6615 (not got-prefix-before-parens)
6616 (not (eq at-type t))
6617 (or backup-at-type
6618 maybe-typeless
6619 backup-maybe-typeless
6620 (when c-recognize-typeless-decls
6621 (not context)))
6622 (setq pos (c-up-list-forward (point)))
6623 (eq (char-before pos) ?\)))
6624 (c-fdoc-shift-type-backward)
6625 (goto-char pos)
6628 (c-forward-syntactic-ws))
6630 (when (and (or maybe-typeless backup-maybe-typeless)
6631 (not got-identifier)
6632 (not got-prefix)
6633 at-type)
6634 ;; Have found no identifier but `c-typeless-decl-kwds' has
6635 ;; matched so we know we're inside a declaration. The
6636 ;; preceding type must be the identifier instead.
6637 (c-fdoc-shift-type-backward))
6639 (setq
6640 at-decl-or-cast
6641 (catch 'at-decl-or-cast
6643 ;; CASE 1
6644 (when (> paren-depth 0)
6645 ;; Encountered something inside parens that isn't matched by
6646 ;; the `c-type-decl-*' regexps, so it's not a type decl
6647 ;; expression. Try to skip out to the same paren depth to
6648 ;; not confuse the cast check below.
6649 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6650 ;; If we've found a specifier keyword then it's a
6651 ;; declaration regardless.
6652 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6654 (setq at-decl-end
6655 (looking-at (cond ((eq context '<>) "[,>]")
6656 (context "[,\)]")
6657 (t "[,;]"))))
6659 ;; Now we've collected info about various characteristics of
6660 ;; the construct we're looking at. Below follows a decision
6661 ;; tree based on that. It's ordered to check more certain
6662 ;; signs before less certain ones.
6664 (if got-identifier
6665 (progn
6667 ;; CASE 2
6668 (when (and (or at-type maybe-typeless)
6669 (not (or got-prefix got-parens)))
6670 ;; Got another identifier directly after the type, so it's a
6671 ;; declaration.
6672 (throw 'at-decl-or-cast t))
6674 (when (and got-parens
6675 (not got-prefix)
6676 (not got-suffix-after-parens)
6677 (or backup-at-type
6678 maybe-typeless
6679 backup-maybe-typeless))
6680 ;; Got a declaration of the form "foo bar (gnu);" where we've
6681 ;; recognized "bar" as the type and "gnu" as the declarator.
6682 ;; In this case it's however more likely that "bar" is the
6683 ;; declarator and "gnu" a function argument or initializer (if
6684 ;; `c-recognize-paren-inits' is set), since the parens around
6685 ;; "gnu" would be superfluous if it's a declarator. Shift the
6686 ;; type one step backward.
6687 (c-fdoc-shift-type-backward)))
6689 ;; Found no identifier.
6691 (if backup-at-type
6692 (progn
6695 ;; CASE 3
6696 (when (= (point) start)
6697 ;; Got a plain list of identifiers. If a colon follows it's
6698 ;; a valid label, or maybe a bitfield. Otherwise the last
6699 ;; one probably is the declared identifier and we should
6700 ;; back up to the previous type, providing it isn't a cast.
6701 (if (and (eq (char-after) ?:)
6702 (not (c-major-mode-is 'java-mode)))
6703 (cond
6704 ;; If we've found a specifier keyword then it's a
6705 ;; declaration regardless.
6706 ((eq at-decl-or-cast t)
6707 (throw 'at-decl-or-cast t))
6708 ((and c-has-bitfields
6709 (eq at-decl-or-cast 'ids)) ; bitfield.
6710 (setq backup-if-not-cast t)
6711 (throw 'at-decl-or-cast t)))
6713 (setq backup-if-not-cast t)
6714 (throw 'at-decl-or-cast t)))
6716 ;; CASE 4
6717 (when (and got-suffix
6718 (not got-prefix)
6719 (not got-parens))
6720 ;; Got a plain list of identifiers followed by some suffix.
6721 ;; If this isn't a cast then the last identifier probably is
6722 ;; the declared one and we should back up to the previous
6723 ;; type.
6724 (setq backup-if-not-cast t)
6725 (throw 'at-decl-or-cast t)))
6727 ;; CASE 5
6728 (when (eq at-type t)
6729 ;; If the type is known we know that there can't be any
6730 ;; identifier somewhere else, and it's only in declarations in
6731 ;; e.g. function prototypes and in casts that the identifier may
6732 ;; be left out.
6733 (throw 'at-decl-or-cast t))
6735 (when (= (point) start)
6736 ;; Only got a single identifier (parsed as a type so far).
6737 ;; CASE 6
6738 (if (and
6739 ;; Check that the identifier isn't at the start of an
6740 ;; expression.
6741 at-decl-end
6742 (cond
6743 ((eq context 'decl)
6744 ;; Inside an arglist that contains declarations. If K&R
6745 ;; style declarations and parenthesis style initializers
6746 ;; aren't allowed then the single identifier must be a
6747 ;; type, else we require that it's known or found
6748 ;; (primitive types are handled above).
6749 (or (and (not c-recognize-knr-p)
6750 (not c-recognize-paren-inits))
6751 (memq at-type '(known found))))
6752 ((eq context '<>)
6753 ;; Inside a template arglist. Accept known and found
6754 ;; types; other identifiers could just as well be
6755 ;; constants in C++.
6756 (memq at-type '(known found)))))
6757 (throw 'at-decl-or-cast t)
6758 ;; CASE 7
6759 ;; Can't be a valid declaration or cast, but if we've found a
6760 ;; specifier it can't be anything else either, so treat it as
6761 ;; an invalid/unfinished declaration or cast.
6762 (throw 'at-decl-or-cast at-decl-or-cast))))
6764 (if (and got-parens
6765 (not got-prefix)
6766 (not context)
6767 (not (eq at-type t))
6768 (or backup-at-type
6769 maybe-typeless
6770 backup-maybe-typeless
6771 (when c-recognize-typeless-decls
6772 (or (not got-suffix)
6773 (not (looking-at
6774 c-after-suffixed-type-maybe-decl-key))))))
6775 ;; Got an empty paren pair and a preceding type that probably
6776 ;; really is the identifier. Shift the type backwards to make
6777 ;; the last one the identifier. This is analogous to the
6778 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6779 ;; above.
6781 ;; Exception: In addition to the conditions in that
6782 ;; "backtracking" code, do not shift backward if we're not
6783 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6784 ;; Since there's no preceding type, the shift would mean that
6785 ;; the declaration is typeless. But if the regexp doesn't match
6786 ;; then we will simply fall through in the tests below and not
6787 ;; recognize it at all, so it's better to try it as an abstract
6788 ;; declarator instead.
6789 (c-fdoc-shift-type-backward)
6791 ;; Still no identifier.
6792 ;; CASE 8
6793 (when (and got-prefix (or got-parens got-suffix))
6794 ;; Require `got-prefix' together with either `got-parens' or
6795 ;; `got-suffix' to recognize it as an abstract declarator:
6796 ;; `got-parens' only is probably an empty function call.
6797 ;; `got-suffix' only can build an ordinary expression together
6798 ;; with the preceding identifier which we've taken as a type.
6799 ;; We could actually accept on `got-prefix' only, but that can
6800 ;; easily occur temporarily while writing an expression so we
6801 ;; avoid that case anyway. We could do a better job if we knew
6802 ;; the point when the fontification was invoked.
6803 (throw 'at-decl-or-cast t))
6805 ;; CASE 9
6806 (when (and at-type
6807 (not got-prefix)
6808 (not got-parens)
6809 got-suffix-after-parens
6810 (eq (char-after got-suffix-after-parens) ?\())
6811 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6812 ;; normal function call afterall (or perhaps a C++ style object
6813 ;; instantiation expression).
6814 (throw 'at-decl-or-cast nil))))
6816 ;; CASE 10
6817 (when at-decl-or-cast
6818 ;; By now we've located the type in the declaration that we know
6819 ;; we're in.
6820 (throw 'at-decl-or-cast t))
6822 ;; CASE 11
6823 (when (and got-identifier
6824 (not context)
6825 (looking-at c-after-suffixed-type-decl-key)
6826 (if (and got-parens
6827 (not got-prefix)
6828 (not got-suffix)
6829 (not (eq at-type t)))
6830 ;; Shift the type backward in the case that there's a
6831 ;; single identifier inside parens. That can only
6832 ;; occur in K&R style function declarations so it's
6833 ;; more likely that it really is a function call.
6834 ;; Therefore we only do this after
6835 ;; `c-after-suffixed-type-decl-key' has matched.
6836 (progn (c-fdoc-shift-type-backward) t)
6837 got-suffix-after-parens))
6838 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6839 (throw 'at-decl-or-cast t))
6841 ;; CASE 12
6842 (when (and (or got-prefix (not got-parens))
6843 (memq at-type '(t known)))
6844 ;; It's a declaration if a known type precedes it and it can't be a
6845 ;; function call.
6846 (throw 'at-decl-or-cast t))
6848 ;; If we get here we can't tell if this is a type decl or a normal
6849 ;; expression by looking at it alone. (That's under the assumption
6850 ;; that normal expressions always can look like type decl expressions,
6851 ;; which isn't really true but the cases where it doesn't hold are so
6852 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6853 ;; the effort to look for them.)
6855 (unless (or at-decl-end (looking-at "=[^=]"))
6856 ;; If this is a declaration it should end here or its initializer(*)
6857 ;; should start here, so check for allowed separation tokens. Note
6858 ;; that this rule doesn't work e.g. with a K&R arglist after a
6859 ;; function header.
6861 ;; *) Don't check for C++ style initializers using parens
6862 ;; since those already have been matched as suffixes.
6864 ;; If `at-decl-or-cast' is then we've found some other sign that
6865 ;; it's a declaration or cast, so then it's probably an
6866 ;; invalid/unfinished one.
6867 (throw 'at-decl-or-cast at-decl-or-cast))
6869 ;; Below are tests that only should be applied when we're certain to
6870 ;; not have parsed halfway through an expression.
6872 ;; CASE 14
6873 (when (memq at-type '(t known))
6874 ;; The expression starts with a known type so treat it as a
6875 ;; declaration.
6876 (throw 'at-decl-or-cast t))
6878 ;; CASE 15
6879 (when (and (c-major-mode-is 'c++-mode)
6880 ;; In C++ we check if the identifier is a known type, since
6881 ;; (con|de)structors use the class name as identifier.
6882 ;; We've always shifted over the identifier as a type and
6883 ;; then backed up again in this case.
6884 identifier-type
6885 (or (memq identifier-type '(found known))
6886 (and (eq (char-after identifier-start) ?~)
6887 ;; `at-type' probably won't be 'found for
6888 ;; destructors since the "~" is then part of the
6889 ;; type name being checked against the list of
6890 ;; known types, so do a check without that
6891 ;; operator.
6892 (or (save-excursion
6893 (goto-char (1+ identifier-start))
6894 (c-forward-syntactic-ws)
6895 (c-with-syntax-table
6896 c-identifier-syntax-table
6897 (looking-at c-known-type-key)))
6898 (save-excursion
6899 (goto-char (1+ identifier-start))
6900 ;; We have already parsed the type earlier,
6901 ;; so it'd be possible to cache the end
6902 ;; position instead of redoing it here, but
6903 ;; then we'd need to keep track of another
6904 ;; position everywhere.
6905 (c-check-type (point)
6906 (progn (c-forward-type)
6907 (point))))))))
6908 (throw 'at-decl-or-cast t))
6910 (if got-identifier
6911 (progn
6912 ;; CASE 16
6913 (when (and got-prefix-before-parens
6914 at-type
6915 (or at-decl-end (looking-at "=[^=]"))
6916 (not context)
6917 (not got-suffix))
6918 ;; Got something like "foo * bar;". Since we're not inside an
6919 ;; arglist it would be a meaningless expression because the
6920 ;; result isn't used. We therefore choose to recognize it as
6921 ;; a declaration. Do not allow a suffix since it could then
6922 ;; be a function call.
6923 (throw 'at-decl-or-cast t))
6925 ;; CASE 17
6926 (when (and (or got-suffix-after-parens
6927 (looking-at "=[^=]"))
6928 (eq at-type 'found)
6929 (not (eq context 'arglist)))
6930 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
6931 ;; be an odd expression or it could be a declaration. Treat
6932 ;; it as a declaration if "a" has been used as a type
6933 ;; somewhere else (if it's a known type we won't get here).
6934 (throw 'at-decl-or-cast t)))
6936 ;; CASE 18
6937 (when (and context
6938 (or got-prefix
6939 (and (eq context 'decl)
6940 (not c-recognize-paren-inits)
6941 (or got-parens got-suffix))))
6942 ;; Got a type followed by an abstract declarator. If `got-prefix'
6943 ;; is set it's something like "a *" without anything after it. If
6944 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
6945 ;; or similar, which we accept only if the context rules out
6946 ;; expressions.
6947 (throw 'at-decl-or-cast t)))
6949 ;; If we had a complete symbol table here (which rules out
6950 ;; `c-found-types') we should return t due to the disambiguation rule
6951 ;; (in at least C++) that anything that can be parsed as a declaration
6952 ;; is a declaration. Now we're being more defensive and prefer to
6953 ;; highlight things like "foo (bar);" as a declaration only if we're
6954 ;; inside an arglist that contains declarations.
6955 (eq context 'decl))))
6957 ;; The point is now after the type decl expression.
6959 (cond
6960 ;; Check for a cast.
6961 ((save-excursion
6962 (and
6963 c-cast-parens
6965 ;; Should be the first type/identifier in a cast paren.
6966 (> preceding-token-end (point-min))
6967 (memq (char-before preceding-token-end) c-cast-parens)
6969 ;; The closing paren should follow.
6970 (progn
6971 (c-forward-syntactic-ws)
6972 (looking-at "\\s\)"))
6974 ;; There should be a primary expression after it.
6975 (let (pos)
6976 (forward-char)
6977 (c-forward-syntactic-ws)
6978 (setq cast-end (point))
6979 (and (looking-at c-primary-expr-regexp)
6980 (progn
6981 (setq pos (match-end 0))
6983 ;; Check if the expression begins with a prefix keyword.
6984 (match-beginning 2)
6985 (if (match-beginning 1)
6986 ;; Expression begins with an ambiguous operator. Treat
6987 ;; it as a cast if it's a type decl or if we've
6988 ;; recognized the type somewhere else.
6989 (or at-decl-or-cast
6990 (memq at-type '(t known found)))
6991 ;; Unless it's a keyword, it's the beginning of a primary
6992 ;; expression.
6993 (not (looking-at c-keywords-regexp)))))
6994 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
6995 ;; that it matched a whole one so that we don't e.g. confuse
6996 ;; the operator '-' with '->'. It's ok if it matches further,
6997 ;; though, since it e.g. can match the float '.5' while the
6998 ;; operator regexp only matches '.'.
6999 (or (not (looking-at c-nonsymbol-token-regexp))
7000 (<= (match-end 0) pos))))
7002 ;; There should either be a cast before it or something that isn't an
7003 ;; identifier or close paren.
7004 (> preceding-token-end (point-min))
7005 (progn
7006 (goto-char (1- preceding-token-end))
7007 (or (eq (point) last-cast-end)
7008 (progn
7009 (c-backward-syntactic-ws)
7010 (if (< (skip-syntax-backward "w_") 0)
7011 ;; It's a symbol. Accept it only if it's one of the
7012 ;; keywords that can precede an expression (without
7013 ;; surrounding parens).
7014 (looking-at c-simple-stmt-key)
7015 (and
7016 ;; Check that it isn't a close paren (block close is ok,
7017 ;; though).
7018 (not (memq (char-before) '(?\) ?\])))
7019 ;; Check that it isn't a nonsymbol identifier.
7020 (not (c-on-identifier)))))))))
7022 ;; Handle the cast.
7023 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7024 (let ((c-promote-possible-types t))
7025 (goto-char type-start)
7026 (c-forward-type)))
7028 (goto-char cast-end)
7029 'cast)
7031 (at-decl-or-cast
7032 ;; We're at a declaration. Highlight the type and the following
7033 ;; declarators.
7035 (when backup-if-not-cast
7036 (c-fdoc-shift-type-backward t))
7038 (when (and (eq context 'decl) (looking-at ","))
7039 ;; Make sure to propagate the `c-decl-arg-start' property to
7040 ;; the next argument if it's set in this one, to cope with
7041 ;; interactive refontification.
7042 (c-put-c-type-property (point) 'c-decl-arg-start))
7044 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7045 (let ((c-promote-possible-types t))
7046 (save-excursion
7047 (goto-char type-start)
7048 (c-forward-type))))
7050 (cons id-start
7051 (and (or at-type-decl at-typedef)
7052 (cons at-type-decl at-typedef))))
7055 ;; False alarm. Restore the recorded ranges.
7056 (setq c-record-type-identifiers save-rec-type-ids
7057 c-record-ref-identifiers save-rec-ref-ids)
7058 nil))))
7060 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
7061 ;; Assuming that point is at the beginning of a token, check if it starts a
7062 ;; label and if so move over it and return non-nil (t in default situations,
7063 ;; specific symbols (see below) for interesting situations), otherwise don't
7064 ;; move and return nil. "Label" here means "most things with a colon".
7066 ;; More precisely, a "label" is regarded as one of:
7067 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7068 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7069 ;; bare "case", should the colon be missing. We return t;
7070 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7071 ;; return t;
7072 ;; (iv) One of QT's "extended" C++ variants of
7073 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7074 ;; Returns the symbol `qt-2kwds-colon'.
7075 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
7076 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
7077 ;; colon). Currently (2006-03), this applies only to Objective C's
7078 ;; keywords "@private", "@protected", and "@public". Returns t.
7080 ;; One of the things which will NOT be recognised as a label is a bit-field
7081 ;; element of a struct, something like "int foo:5".
7083 ;; The end of the label is taken to be just after the colon, or the end of
7084 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7085 ;; after the end on return. The terminating char gets marked with
7086 ;; `c-decl-end' to improve recognition of the following declaration or
7087 ;; statement.
7089 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7090 ;; label, if any, has already been marked up like that.
7092 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7093 ;; after the preceding token, i.e. on the other side of the
7094 ;; syntactic ws from the point. Use a value less than or equal to
7095 ;; (point-min) if the point is at the first token in (the visible
7096 ;; part of) the buffer.
7098 ;; The optional LIMIT limits the forward scan for the colon.
7100 ;; This function records the ranges of the label symbols on
7101 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7102 ;; non-nil.
7104 ;; This function might do hidden buffer changes.
7106 (let ((start (point))
7107 label-end
7108 qt-symbol-idx
7109 macro-start ; if we're in one.
7110 label-type
7111 kwd)
7112 (cond
7113 ;; "case" or "default" (Doesn't apply to AWK).
7114 ((looking-at c-label-kwds-regexp)
7115 (let ((kwd-end (match-end 1)))
7116 ;; Record only the keyword itself for fontification, since in
7117 ;; case labels the following is a constant expression and not
7118 ;; a label.
7119 (when c-record-type-identifiers
7120 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7122 ;; Find the label end.
7123 (goto-char kwd-end)
7124 (setq label-type
7125 (if (and (c-syntactic-re-search-forward
7126 ;; Stop on chars that aren't allowed in expressions,
7127 ;; and on operator chars that would be meaningless
7128 ;; there. FIXME: This doesn't cope with ?: operators.
7129 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7130 limit t t nil 1)
7131 (match-beginning 2))
7133 (progn ; there's a proper :
7134 (goto-char (match-beginning 2)) ; just after the :
7135 (c-put-c-type-property (1- (point)) 'c-decl-end)
7138 ;; It's an unfinished label. We consider the keyword enough
7139 ;; to recognize it as a label, so that it gets fontified.
7140 ;; Leave the point at the end of it, but don't put any
7141 ;; `c-decl-end' marker.
7142 (goto-char kwd-end)
7143 t))))
7145 ;; @private, @protected, @public, in Objective C, or similar.
7146 ((and c-opt-extra-label-key
7147 (looking-at c-opt-extra-label-key))
7148 ;; For a `c-opt-extra-label-key' match, we record the whole
7149 ;; thing for fontification. That's to get the leading '@' in
7150 ;; Objective-C protection labels fontified.
7151 (goto-char (match-end 1))
7152 (when c-record-type-identifiers
7153 (c-record-ref-id (cons (match-beginning 1) (point))))
7154 (c-put-c-type-property (1- (point)) 'c-decl-end)
7155 (setq label-type t))
7157 ;; All other cases of labels.
7158 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
7160 ;; A colon label must have something before the colon.
7161 (not (eq (char-after) ?:))
7163 ;; Check that we're not after a token that can't precede a label.
7165 ;; Trivially succeeds when there's no preceding token.
7166 (if preceding-token-end
7167 (<= preceding-token-end (point-min))
7168 (save-excursion
7169 (c-backward-syntactic-ws)
7170 (setq preceding-token-end (point))
7171 (bobp)))
7173 ;; Check if we're after a label, if we're after a closing
7174 ;; paren that belong to statement, and with
7175 ;; `c-label-prefix-re'. It's done in different order
7176 ;; depending on `assume-markup' since the checks have
7177 ;; different expensiveness.
7178 (if assume-markup
7180 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7181 'c-decl-end)
7183 (save-excursion
7184 (goto-char (1- preceding-token-end))
7185 (c-beginning-of-current-token)
7186 (or (looking-at c-label-prefix-re)
7187 (looking-at c-block-stmt-1-key)))
7189 (and (eq (char-before preceding-token-end) ?\))
7190 (c-after-conditional)))
7193 (save-excursion
7194 (goto-char (1- preceding-token-end))
7195 (c-beginning-of-current-token)
7196 (or (looking-at c-label-prefix-re)
7197 (looking-at c-block-stmt-1-key)))
7199 (cond
7200 ((eq (char-before preceding-token-end) ?\))
7201 (c-after-conditional))
7203 ((eq (char-before preceding-token-end) ?:)
7204 ;; Might be after another label, so check it recursively.
7205 (save-restriction
7206 (save-excursion
7207 (goto-char (1- preceding-token-end))
7208 ;; Essentially the same as the
7209 ;; `c-syntactic-re-search-forward' regexp below.
7210 (setq macro-start
7211 (save-excursion (and (c-beginning-of-macro)
7212 (point))))
7213 (if macro-start (narrow-to-region macro-start (point-max)))
7214 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7215 ;; Note: the following should work instead of the
7216 ;; narrow-to-region above. Investigate why not,
7217 ;; sometime. ACM, 2006-03-31.
7218 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7219 ;; macro-start t)
7220 (let ((pte (point))
7221 ;; If the caller turned on recording for us,
7222 ;; it shouldn't apply when we check the
7223 ;; preceding label.
7224 c-record-type-identifiers)
7225 ;; A label can't start at a cpp directive. Check for
7226 ;; this, since c-forward-syntactic-ws would foul up on it.
7227 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7228 (c-forward-syntactic-ws)
7229 (c-forward-label nil pte start))))))))))
7231 ;; Point is still at the beginning of the possible label construct.
7233 ;; Check that the next nonsymbol token is ":", or that we're in one
7234 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7235 ;; arguments. FIXME: Should build this regexp from the language
7236 ;; constants.
7237 (cond
7238 ;; public: protected: private:
7239 ((and
7240 (c-major-mode-is 'c++-mode)
7241 (search-forward-regexp
7242 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7243 (progn (backward-char)
7244 (c-forward-syntactic-ws limit)
7245 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7246 (forward-char)
7247 (setq label-type t))
7248 ;; QT double keyword like "protected slots:" or goto target.
7249 ((progn (goto-char start) nil))
7250 ((when (c-syntactic-re-search-forward
7251 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7252 (backward-char)
7253 (setq label-end (point))
7254 (setq qt-symbol-idx
7255 (and (c-major-mode-is 'c++-mode)
7256 (string-match
7257 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7258 (buffer-substring start (point)))))
7259 (c-forward-syntactic-ws limit)
7260 (cond
7261 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7262 (forward-char)
7263 (setq label-type
7264 (if (or (string= "signals" ; Special QT macro
7265 (setq kwd (buffer-substring-no-properties start label-end)))
7266 (string= "Q_SIGNALS" kwd))
7267 'qt-1kwd-colon
7268 'goto-target)))
7269 ((and qt-symbol-idx
7270 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7271 (progn (c-forward-syntactic-ws limit)
7272 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7273 (forward-char)
7274 (setq label-type 'qt-2kwds-colon)))))))
7276 (save-restriction
7277 (narrow-to-region start (point))
7279 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7280 (catch 'check-label
7281 (goto-char start)
7282 (while (progn
7283 (when (looking-at c-nonlabel-token-key)
7284 (goto-char start)
7285 (setq label-type nil)
7286 (throw 'check-label nil))
7287 (and (c-safe (c-forward-sexp)
7288 (c-forward-syntactic-ws)
7290 (not (eobp)))))
7292 ;; Record the identifiers in the label for fontification, unless
7293 ;; it begins with `c-label-kwds' in which case the following
7294 ;; identifiers are part of a (constant) expression that
7295 ;; shouldn't be fontified.
7296 (when (and c-record-type-identifiers
7297 (progn (goto-char start)
7298 (not (looking-at c-label-kwds-regexp))))
7299 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7300 (c-record-ref-id (cons (match-beginning 0)
7301 (match-end 0)))))
7303 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7304 (goto-char (point-max)))))
7307 ;; Not a label.
7308 (goto-char start)))
7309 label-type))
7311 (defun c-forward-objc-directive ()
7312 ;; Assuming the point is at the beginning of a token, try to move
7313 ;; forward to the end of the Objective-C directive that starts
7314 ;; there. Return t if a directive was fully recognized, otherwise
7315 ;; the point is moved as far as one could be successfully parsed and
7316 ;; nil is returned.
7318 ;; This function records identifier ranges on
7319 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7320 ;; `c-record-type-identifiers' is non-nil.
7322 ;; This function might do hidden buffer changes.
7324 (let ((start (point))
7325 start-char
7326 (c-promote-possible-types t)
7327 ;; Turn off recognition of angle bracket arglists while parsing
7328 ;; types here since the protocol reference list might then be
7329 ;; considered part of the preceding name or superclass-name.
7330 c-recognize-<>-arglists)
7332 (if (or
7333 (when (looking-at
7334 (eval-when-compile
7335 (c-make-keywords-re t
7336 (append (c-lang-const c-protection-kwds objc)
7337 '("@end"))
7338 'objc-mode)))
7339 (goto-char (match-end 1))
7342 (and
7343 (looking-at
7344 (eval-when-compile
7345 (c-make-keywords-re t
7346 '("@interface" "@implementation" "@protocol")
7347 'objc-mode)))
7349 ;; Handle the name of the class itself.
7350 (progn
7351 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7352 ; at EOB.
7353 (goto-char (match-end 0))
7354 (c-skip-ws-forward)
7355 (c-forward-type))
7357 (catch 'break
7358 ;; Look for ": superclass-name" or "( category-name )".
7359 (when (looking-at "[:\(]")
7360 (setq start-char (char-after))
7361 (forward-char)
7362 (c-forward-syntactic-ws)
7363 (unless (c-forward-type) (throw 'break nil))
7364 (when (eq start-char ?\()
7365 (unless (eq (char-after) ?\)) (throw 'break nil))
7366 (forward-char)
7367 (c-forward-syntactic-ws)))
7369 ;; Look for a protocol reference list.
7370 (if (eq (char-after) ?<)
7371 (let ((c-recognize-<>-arglists t)
7372 (c-parse-and-markup-<>-arglists t)
7373 c-restricted-<>-arglists)
7374 (c-forward-<>-arglist t))
7375 t))))
7377 (progn
7378 (c-backward-syntactic-ws)
7379 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7380 (c-put-c-type-property (1- (point)) 'c-decl-end)
7383 (c-clear-c-type-property start (point) 'c-decl-end)
7384 nil)))
7386 (defun c-beginning-of-inheritance-list (&optional lim)
7387 ;; Go to the first non-whitespace after the colon that starts a
7388 ;; multiple inheritance introduction. Optional LIM is the farthest
7389 ;; back we should search.
7391 ;; This function might do hidden buffer changes.
7392 (c-with-syntax-table c++-template-syntax-table
7393 (c-backward-token-2 0 t lim)
7394 (while (and (or (looking-at c-symbol-start)
7395 (looking-at "[<,]\\|::"))
7396 (zerop (c-backward-token-2 1 t lim))))))
7398 (defun c-in-method-def-p ()
7399 ;; Return nil if we aren't in a method definition, otherwise the
7400 ;; position of the initial [+-].
7402 ;; This function might do hidden buffer changes.
7403 (save-excursion
7404 (beginning-of-line)
7405 (and c-opt-method-key
7406 (looking-at c-opt-method-key)
7407 (point))
7410 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7411 (defun c-in-gcc-asm-p ()
7412 ;; Return non-nil if point is within a gcc \"asm\" block.
7414 ;; This should be called with point inside an argument list.
7416 ;; Only one level of enclosing parentheses is considered, so for
7417 ;; instance `nil' is returned when in a function call within an asm
7418 ;; operand.
7420 ;; This function might do hidden buffer changes.
7422 (and c-opt-asm-stmt-key
7423 (save-excursion
7424 (beginning-of-line)
7425 (backward-up-list 1)
7426 (c-beginning-of-statement-1 (point-min) nil t)
7427 (looking-at c-opt-asm-stmt-key))))
7429 (defun c-at-toplevel-p ()
7430 "Return a determination as to whether point is \"at the top level\".
7431 Informally, \"at the top level\" is anywhere where you can write
7432 a function.
7434 More precisely, being at the top-level means that point is either
7435 outside any enclosing block (such as a function definition), or
7436 directly inside a class, namespace or other block that contains
7437 another declaration level.
7439 If point is not at the top-level (e.g. it is inside a method
7440 definition), then nil is returned. Otherwise, if point is at a
7441 top-level not enclosed within a class definition, t is returned.
7442 Otherwise, a 2-vector is returned where the zeroth element is the
7443 buffer position of the start of the class declaration, and the first
7444 element is the buffer position of the enclosing class's opening
7445 brace.
7447 Note that this function might do hidden buffer changes. See the
7448 comment at the start of cc-engine.el for more info."
7449 (let ((paren-state (c-parse-state)))
7450 (or (not (c-most-enclosing-brace paren-state))
7451 (c-search-uplist-for-classkey paren-state))))
7453 (defun c-just-after-func-arglist-p (&optional lim)
7454 ;; Return non-nil if the point is in the region after the argument
7455 ;; list of a function and its opening brace (or semicolon in case it
7456 ;; got no body). If there are K&R style argument declarations in
7457 ;; that region, the point has to be inside the first one for this
7458 ;; function to recognize it.
7460 ;; If successful, the point is moved to the first token after the
7461 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7462 ;; the position of the opening paren of the function arglist is
7463 ;; returned.
7465 ;; The point is clobbered if not successful.
7467 ;; LIM is used as bound for backward buffer searches.
7469 ;; This function might do hidden buffer changes.
7471 (let ((beg (point)) end id-start)
7472 (and
7473 (eq (c-beginning-of-statement-1 lim) 'same)
7475 (not (or (c-major-mode-is 'objc-mode)
7476 (c-forward-objc-directive)))
7478 (setq id-start
7479 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7480 (< id-start beg)
7482 ;; There should not be a '=' or ',' between beg and the
7483 ;; start of the declaration since that means we were in the
7484 ;; "expression part" of the declaration.
7485 (or (> (point) beg)
7486 (not (looking-at "[=,]")))
7488 (save-excursion
7489 ;; Check that there's an arglist paren in the
7490 ;; declaration.
7491 (goto-char id-start)
7492 (cond ((eq (char-after) ?\()
7493 ;; The declarator is a paren expression, so skip past it
7494 ;; so that we don't get stuck on that instead of the
7495 ;; function arglist.
7496 (c-forward-sexp))
7497 ((and c-opt-op-identifier-prefix
7498 (looking-at c-opt-op-identifier-prefix))
7499 ;; Don't trip up on "operator ()".
7500 (c-forward-token-2 2 t)))
7501 (and (< (point) beg)
7502 (c-syntactic-re-search-forward "(" beg t t)
7503 (1- (point)))))))
7505 (defun c-in-knr-argdecl (&optional lim)
7506 ;; Return the position of the first argument declaration if point is
7507 ;; inside a K&R style argument declaration list, nil otherwise.
7508 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7509 ;; position that bounds the backward search for the argument list.
7511 ;; Point must be within a possible K&R region, e.g. just before a top-level
7512 ;; "{". It must be outside of parens and brackets. The test can return
7513 ;; false positives otherwise.
7515 ;; This function might do hidden buffer changes.
7517 (save-excursion
7518 (save-restriction
7519 ;; If we're in a macro, our search range is restricted to it. Narrow to
7520 ;; the searchable range.
7521 (let* ((macro-start (c-query-macro-start))
7522 (lim (max (or lim (point-min)) (or macro-start (point-min))))
7523 before-lparen after-rparen
7524 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
7525 (narrow-to-region lim (c-point 'eol))
7527 ;; Search backwards for the defun's argument list. We give up if we
7528 ;; encounter a "}" (end of a previous defun) or BOB.
7530 ;; The criterion for a paren structure being the arg list is:
7531 ;; o - there is non-WS stuff after it but before any "{"; AND
7532 ;; o - the token after it isn't a ";" AND
7533 ;; o - it is preceded by either an identifier (the function name) or
7534 ;; a macro expansion like "DEFUN (...)"; AND
7535 ;; o - its content is a non-empty comma-separated list of identifiers
7536 ;; (an empty arg list won't have a knr region).
7538 ;; The following snippet illustrates these rules:
7539 ;; int foo (bar, baz, yuk)
7540 ;; int bar [] ;
7541 ;; int (*baz) (my_type) ;
7542 ;; int (*) (void) (*yuk) (void) ;
7543 ;; {
7545 (catch 'knr
7546 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7547 (setq pp-count-out (1- pp-count-out))
7548 (c-syntactic-skip-backward "^)]}")
7549 (cond ((eq (char-before) ?\))
7550 (setq after-rparen (point)))
7551 ((eq (char-before) ?\])
7552 (setq after-rparen nil))
7553 (t ; either } (hit previous defun) or no more parens/brackets
7554 (throw 'knr nil)))
7556 (if after-rparen
7557 ;; We're inside a paren. Could it be our argument list....?
7559 (and
7560 (progn
7561 (goto-char after-rparen)
7562 (unless (c-go-list-backward) (throw 'knr nil)) ;
7563 ;; FIXME!!! What about macros between the parens? 2007/01/20
7564 (setq before-lparen (point)))
7566 ;; It can't be the arg list if next token is ; or {
7567 (progn (goto-char after-rparen)
7568 (c-forward-syntactic-ws)
7569 (not (memq (char-after) '(?\; ?\{))))
7571 ;; Is the thing preceding the list an identifier (the
7572 ;; function name), or a macro expansion?
7573 (progn
7574 (goto-char before-lparen)
7575 (eq (c-backward-token-2) 0)
7576 (or (c-on-identifier)
7577 (and (eq (char-after) ?\))
7578 (c-go-up-list-backward)
7579 (eq (c-backward-token-2) 0)
7580 (c-on-identifier))))
7582 ;; Have we got a non-empty list of comma-separated
7583 ;; identifiers?
7584 (progn
7585 (goto-char before-lparen)
7586 (c-forward-token-2) ; to first token inside parens
7587 (and
7588 (c-on-identifier)
7589 (c-forward-token-2)
7590 (catch 'id-list
7591 (while (eq (char-after) ?\,)
7592 (c-forward-token-2)
7593 (unless (c-on-identifier) (throw 'id-list nil))
7594 (c-forward-token-2))
7595 (eq (char-after) ?\))))))
7597 ;; ...Yes. We've identified the function's argument list.
7598 (throw 'knr
7599 (progn (goto-char after-rparen)
7600 (c-forward-syntactic-ws)
7601 (point)))
7603 ;; ...No. The current parens aren't the function's arg list.
7604 (goto-char before-lparen))
7606 (or (c-go-list-backward) ; backwards over [ .... ]
7607 (throw 'knr nil)))))))))
7609 (defun c-skip-conditional ()
7610 ;; skip forward over conditional at point, including any predicate
7611 ;; statements in parentheses. No error checking is performed.
7613 ;; This function might do hidden buffer changes.
7614 (c-forward-sexp (cond
7615 ;; else if()
7616 ((looking-at (concat "\\<else"
7617 "\\([ \t\n]\\|\\\\\n\\)+"
7618 "if\\>\\([^_]\\|$\\)"))
7620 ;; do, else, try, finally
7621 ((looking-at (concat "\\<\\("
7622 "do\\|else\\|try\\|finally"
7623 "\\)\\>\\([^_]\\|$\\)"))
7625 ;; for, if, while, switch, catch, synchronized, foreach
7626 (t 2))))
7628 (defun c-after-conditional (&optional lim)
7629 ;; If looking at the token after a conditional then return the
7630 ;; position of its start, otherwise return nil.
7632 ;; This function might do hidden buffer changes.
7633 (save-excursion
7634 (and (zerop (c-backward-token-2 1 t lim))
7635 (or (looking-at c-block-stmt-1-key)
7636 (and (eq (char-after) ?\()
7637 (zerop (c-backward-token-2 1 t lim))
7638 (looking-at c-block-stmt-2-key)))
7639 (point))))
7641 (defun c-after-special-operator-id (&optional lim)
7642 ;; If the point is after an operator identifier that isn't handled
7643 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7644 ;; position of the start of that identifier is returned. nil is
7645 ;; returned otherwise. The point may be anywhere in the syntactic
7646 ;; whitespace after the last token of the operator identifier.
7648 ;; This function might do hidden buffer changes.
7649 (save-excursion
7650 (and c-overloadable-operators-regexp
7651 (zerop (c-backward-token-2 1 nil lim))
7652 (looking-at c-overloadable-operators-regexp)
7653 (or (not c-opt-op-identifier-prefix)
7654 (and
7655 (zerop (c-backward-token-2 1 nil lim))
7656 (looking-at c-opt-op-identifier-prefix)))
7657 (point))))
7659 (defsubst c-backward-to-block-anchor (&optional lim)
7660 ;; Assuming point is at a brace that opens a statement block of some
7661 ;; kind, move to the proper anchor point for that block. It might
7662 ;; need to be adjusted further by c-add-stmt-syntax, but the
7663 ;; position at return is suitable as start position for that
7664 ;; function.
7666 ;; This function might do hidden buffer changes.
7667 (unless (= (point) (c-point 'boi))
7668 (let ((start (c-after-conditional lim)))
7669 (if start
7670 (goto-char start)))))
7672 (defsubst c-backward-to-decl-anchor (&optional lim)
7673 ;; Assuming point is at a brace that opens the block of a top level
7674 ;; declaration of some kind, move to the proper anchor point for
7675 ;; that block.
7677 ;; This function might do hidden buffer changes.
7678 (unless (= (point) (c-point 'boi))
7679 (c-beginning-of-statement-1 lim)))
7681 (defun c-search-decl-header-end ()
7682 ;; Search forward for the end of the "header" of the current
7683 ;; declaration. That's the position where the definition body
7684 ;; starts, or the first variable initializer, or the ending
7685 ;; semicolon. I.e. search forward for the closest following
7686 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7687 ;; _after_ the first found token, or at point-max if none is found.
7689 ;; This function might do hidden buffer changes.
7691 (let ((base (point)))
7692 (if (c-major-mode-is 'c++-mode)
7694 ;; In C++ we need to take special care to handle operator
7695 ;; tokens and those pesky template brackets.
7696 (while (and
7697 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
7699 (c-end-of-current-token base)
7700 ;; Handle operator identifiers, i.e. ignore any
7701 ;; operator token preceded by "operator".
7702 (save-excursion
7703 (and (c-safe (c-backward-sexp) t)
7704 (looking-at c-opt-op-identifier-prefix)))
7705 (and (eq (char-before) ?<)
7706 (c-with-syntax-table c++-template-syntax-table
7707 (if (c-safe (goto-char (c-up-list-forward (point))))
7709 (goto-char (point-max))
7710 nil)))))
7711 (setq base (point)))
7713 (while (and
7714 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
7715 (c-end-of-current-token base))
7716 (setq base (point))))))
7718 (defun c-beginning-of-decl-1 (&optional lim)
7719 ;; Go to the beginning of the current declaration, or the beginning
7720 ;; of the previous one if already at the start of it. Point won't
7721 ;; be moved out of any surrounding paren. Return a cons cell of the
7722 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7723 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7724 ;; style argument declarations (and they are to be recognized) then
7725 ;; KNR-POS is set to the start of the first such argument
7726 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7727 ;; position that bounds the backward search.
7729 ;; NB: Cases where the declaration continues after the block, as in
7730 ;; "struct foo { ... } bar;", are currently recognized as two
7731 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7733 ;; This function might do hidden buffer changes.
7734 (catch 'return
7735 (let* ((start (point))
7736 (last-stmt-start (point))
7737 (move (c-beginning-of-statement-1 lim nil t)))
7739 ;; `c-beginning-of-statement-1' stops at a block start, but we
7740 ;; want to continue if the block doesn't begin a top level
7741 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7742 ;; or an open paren.
7743 (let ((beg (point)) tentative-move)
7744 ;; Go back one "statement" each time round the loop until we're just
7745 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7746 ;; an ObjC method. This will move over a multiple declaration whose
7747 ;; components are comma separated.
7748 (while (and
7749 ;; Must check with c-opt-method-key in ObjC mode.
7750 (not (and c-opt-method-key
7751 (looking-at c-opt-method-key)))
7752 (/= last-stmt-start (point))
7753 (progn
7754 (c-backward-syntactic-ws lim)
7755 (not (memq (char-before) '(?\; ?} ?: nil))))
7756 (save-excursion
7757 (backward-char)
7758 (not (looking-at "\\s(")))
7759 ;; Check that we don't move from the first thing in a
7760 ;; macro to its header.
7761 (not (eq (setq tentative-move
7762 (c-beginning-of-statement-1 lim nil t))
7763 'macro)))
7764 (setq last-stmt-start beg
7765 beg (point)
7766 move tentative-move))
7767 (goto-char beg))
7769 (when c-recognize-knr-p
7770 (let ((fallback-pos (point)) knr-argdecl-start)
7771 ;; Handle K&R argdecls. Back up after the "statement" jumped
7772 ;; over by `c-beginning-of-statement-1', unless it was the
7773 ;; function body, in which case we're sitting on the opening
7774 ;; brace now. Then test if we're in a K&R argdecl region and
7775 ;; that we started at the other side of the first argdecl in
7776 ;; it.
7777 (unless (eq (char-after) ?{)
7778 (goto-char last-stmt-start))
7779 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
7780 (< knr-argdecl-start start)
7781 (progn
7782 (goto-char knr-argdecl-start)
7783 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
7784 (throw 'return
7785 (cons (if (eq (char-after fallback-pos) ?{)
7786 'previous
7787 'same)
7788 knr-argdecl-start))
7789 (goto-char fallback-pos))))
7791 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7792 ;; statement, so the result will be 'previous if we've moved over any.
7793 ;; So change our result back to 'same if necessary.
7795 ;; If they were brace list initializers we might not have moved over a
7796 ;; declaration boundary though, so change it to 'same if we've moved
7797 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7798 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7799 ;; potentially can search over a large amount of text.). Take special
7800 ;; pains not to get mislead by C++'s "operator=", and the like.
7801 (if (and (eq move 'previous)
7802 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
7803 c++-template-syntax-table
7804 (syntax-table))
7805 (save-excursion
7806 (and
7807 (progn
7808 (while ; keep going back to "[;={"s until we either find
7809 ; no more, or get to one which isn't an "operator ="
7810 (and (c-syntactic-re-search-forward "[;={]" start t t t)
7811 (eq (char-before) ?=)
7812 c-overloadable-operators-regexp
7813 c-opt-op-identifier-prefix
7814 (save-excursion
7815 (eq (c-backward-token-2) 0)
7816 (looking-at c-overloadable-operators-regexp)
7817 (eq (c-backward-token-2) 0)
7818 (looking-at c-opt-op-identifier-prefix))))
7819 (eq (char-before) ?=))
7820 (c-syntactic-re-search-forward "[;{]" start t t)
7821 (eq (char-before) ?{)
7822 (c-safe (goto-char (c-up-list-forward (point))) t)
7823 (not (c-syntactic-re-search-forward ";" start t t))))))
7824 (cons 'same nil)
7825 (cons move nil)))))
7827 (defun c-end-of-decl-1 ()
7828 ;; Assuming point is at the start of a declaration (as detected by
7829 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7830 ;; `c-beginning-of-decl-1', this function handles the case when a
7831 ;; block is followed by identifiers in e.g. struct declarations in C
7832 ;; or C++. If a proper end was found then t is returned, otherwise
7833 ;; point is moved as far as possible within the current sexp and nil
7834 ;; is returned. This function doesn't handle macros; use
7835 ;; `c-end-of-macro' instead in those cases.
7837 ;; This function might do hidden buffer changes.
7838 (let ((start (point))
7839 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
7840 c++-template-syntax-table
7841 (syntax-table))))
7842 (catch 'return
7843 (c-search-decl-header-end)
7845 (when (and c-recognize-knr-p
7846 (eq (char-before) ?\;)
7847 (c-in-knr-argdecl start))
7848 ;; Stopped at the ';' in a K&R argdecl section which is
7849 ;; detected using the same criteria as in
7850 ;; `c-beginning-of-decl-1'. Move to the following block
7851 ;; start.
7852 (c-syntactic-re-search-forward "{" nil 'move t))
7854 (when (eq (char-before) ?{)
7855 ;; Encountered a block in the declaration. Jump over it.
7856 (condition-case nil
7857 (goto-char (c-up-list-forward (point)))
7858 (error (goto-char (point-max))
7859 (throw 'return nil)))
7860 (if (or (not c-opt-block-decls-with-vars-key)
7861 (save-excursion
7862 (c-with-syntax-table decl-syntax-table
7863 (let ((lim (point)))
7864 (goto-char start)
7865 (not (and
7866 ;; Check for `c-opt-block-decls-with-vars-key'
7867 ;; before the first paren.
7868 (c-syntactic-re-search-forward
7869 (concat "[;=\(\[{]\\|\\("
7870 c-opt-block-decls-with-vars-key
7871 "\\)")
7872 lim t t t)
7873 (match-beginning 1)
7874 (not (eq (char-before) ?_))
7875 ;; Check that the first following paren is
7876 ;; the block.
7877 (c-syntactic-re-search-forward "[;=\(\[{]"
7878 lim t t t)
7879 (eq (char-before) ?{)))))))
7880 ;; The declaration doesn't have any of the
7881 ;; `c-opt-block-decls-with-vars' keywords in the
7882 ;; beginning, so it ends here at the end of the block.
7883 (throw 'return t)))
7885 (c-with-syntax-table decl-syntax-table
7886 (while (progn
7887 (if (eq (char-before) ?\;)
7888 (throw 'return t))
7889 (c-syntactic-re-search-forward ";" nil 'move t))))
7890 nil)))
7892 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
7893 ;; Assuming the point is at an open brace, check if it starts a
7894 ;; block that contains another declaration level, i.e. that isn't a
7895 ;; statement block or a brace list, and if so return non-nil.
7897 ;; If the check is successful, the return value is the start of the
7898 ;; keyword that tells what kind of construct it is, i.e. typically
7899 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7900 ;; the point will be at the start of the construct, before any
7901 ;; leading specifiers, otherwise it's at the returned position.
7903 ;; The point is clobbered if the check is unsuccessful.
7905 ;; CONTAINING-SEXP is the position of the open of the surrounding
7906 ;; paren, or nil if none.
7908 ;; The optional LIMIT limits the backward search for the start of
7909 ;; the construct. It's assumed to be at a syntactically relevant
7910 ;; position.
7912 ;; If any template arglists are found in the searched region before
7913 ;; the open brace, they get marked with paren syntax.
7915 ;; This function might do hidden buffer changes.
7917 (let ((open-brace (point)) kwd-start first-specifier-pos)
7918 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7920 (when (and c-recognize-<>-arglists
7921 (eq (char-before) ?>))
7922 ;; Could be at the end of a template arglist.
7923 (let ((c-parse-and-markup-<>-arglists t)
7924 (c-disallow-comma-in-<>-arglists
7925 (and containing-sexp
7926 (not (eq (char-after containing-sexp) ?{)))))
7927 (while (and
7928 (c-backward-<>-arglist nil limit)
7929 (progn
7930 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7931 (eq (char-before) ?>))))))
7933 ;; Note: Can't get bogus hits inside template arglists below since they
7934 ;; have gotten paren syntax above.
7935 (when (and
7936 ;; If `goto-start' is set we begin by searching for the
7937 ;; first possible position of a leading specifier list.
7938 ;; The `c-decl-block-key' search continues from there since
7939 ;; we know it can't match earlier.
7940 (if goto-start
7941 (when (c-syntactic-re-search-forward c-symbol-start
7942 open-brace t t)
7943 (goto-char (setq first-specifier-pos (match-beginning 0)))
7947 (cond
7948 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
7949 (goto-char (setq kwd-start (match-beginning 0)))
7952 ;; Found a keyword that can't be a type?
7953 (match-beginning 1)
7955 ;; Can be a type too, in which case it's the return type of a
7956 ;; function (under the assumption that no declaration level
7957 ;; block construct starts with a type).
7958 (not (c-forward-type))
7960 ;; Jumped over a type, but it could be a declaration keyword
7961 ;; followed by the declared identifier that we've jumped over
7962 ;; instead (e.g. in "class Foo {"). If it indeed is a type
7963 ;; then we should be at the declarator now, so check for a
7964 ;; valid declarator start.
7966 ;; Note: This doesn't cope with the case when a declared
7967 ;; identifier is followed by e.g. '(' in a language where '('
7968 ;; also might be part of a declarator expression. Currently
7969 ;; there's no such language.
7970 (not (or (looking-at c-symbol-start)
7971 (looking-at c-type-decl-prefix-key)))))
7973 ;; In Pike a list of modifiers may be followed by a brace
7974 ;; to make them apply to many identifiers. Note that the
7975 ;; match data will be empty on return in this case.
7976 ((and (c-major-mode-is 'pike-mode)
7977 (progn
7978 (goto-char open-brace)
7979 (= (c-backward-token-2) 0))
7980 (looking-at c-specifier-key)
7981 ;; Use this variant to avoid yet another special regexp.
7982 (c-keyword-member (c-keyword-sym (match-string 1))
7983 'c-modifier-kwds))
7984 (setq kwd-start (point))
7985 t)))
7987 ;; Got a match.
7989 (if goto-start
7990 ;; Back up over any preceding specifiers and their clauses
7991 ;; by going forward from `first-specifier-pos', which is the
7992 ;; earliest possible position where the specifier list can
7993 ;; start.
7994 (progn
7995 (goto-char first-specifier-pos)
7997 (while (< (point) kwd-start)
7998 (if (looking-at c-symbol-key)
7999 ;; Accept any plain symbol token on the ground that
8000 ;; it's a specifier masked through a macro (just
8001 ;; like `c-forward-decl-or-cast-1' skip forward over
8002 ;; such tokens).
8004 ;; Could be more restrictive wrt invalid keywords,
8005 ;; but that'd only occur in invalid code so there's
8006 ;; no use spending effort on it.
8007 (let ((end (match-end 0)))
8008 (unless (c-forward-keyword-clause 0)
8009 (goto-char end)
8010 (c-forward-syntactic-ws)))
8012 ;; Can't parse a declaration preamble and is still
8013 ;; before `kwd-start'. That means `first-specifier-pos'
8014 ;; was in some earlier construct. Search again.
8015 (if (c-syntactic-re-search-forward c-symbol-start
8016 kwd-start 'move t)
8017 (goto-char (setq first-specifier-pos (match-beginning 0)))
8018 ;; Got no preamble before the block declaration keyword.
8019 (setq first-specifier-pos kwd-start))))
8021 (goto-char first-specifier-pos))
8022 (goto-char kwd-start))
8024 kwd-start)))
8026 (defun c-search-uplist-for-classkey (paren-state)
8027 ;; Check if the closest containing paren sexp is a declaration
8028 ;; block, returning a 2 element vector in that case. Aref 0
8029 ;; contains the bufpos at boi of the class key line, and aref 1
8030 ;; contains the bufpos of the open brace. This function is an
8031 ;; obsolete wrapper for `c-looking-at-decl-block'.
8033 ;; This function might do hidden buffer changes.
8034 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
8035 (when open-paren-pos
8036 (save-excursion
8037 (goto-char open-paren-pos)
8038 (when (and (eq (char-after) ?{)
8039 (c-looking-at-decl-block
8040 (c-safe-position open-paren-pos paren-state)
8041 nil))
8042 (back-to-indentation)
8043 (vector (point) open-paren-pos))))))
8045 (defmacro c-pull-open-brace (ps)
8046 ;; Pull the next open brace from PS (which has the form of paren-state),
8047 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
8048 `(progn
8049 (while (consp (car ,ps))
8050 (setq ,ps (cdr ,ps)))
8051 (prog1 (car ,ps)
8052 (setq ,ps (cdr ,ps)))))
8054 (defun c-most-enclosing-decl-block (paren-state)
8055 ;; Return the buffer position of the most enclosing decl-block brace (in the
8056 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
8057 ;; none was found.
8058 (let* ((open-brace (c-pull-open-brace paren-state))
8059 (next-open-brace (c-pull-open-brace paren-state)))
8060 (while (and open-brace
8061 (save-excursion
8062 (goto-char open-brace)
8063 (not (c-looking-at-decl-block next-open-brace nil))))
8064 (setq open-brace next-open-brace
8065 next-open-brace (c-pull-open-brace paren-state)))
8066 open-brace))
8068 (defun c-inside-bracelist-p (containing-sexp paren-state)
8069 ;; return the buffer position of the beginning of the brace list
8070 ;; statement if we're inside a brace list, otherwise return nil.
8071 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
8072 ;; paren. PAREN-STATE is the remainder of the state of enclosing
8073 ;; braces
8075 ;; N.B.: This algorithm can potentially get confused by cpp macros
8076 ;; placed in inconvenient locations. It's a trade-off we make for
8077 ;; speed.
8079 ;; This function might do hidden buffer changes.
8081 ;; This will pick up brace list declarations.
8082 (c-safe
8083 (save-excursion
8084 (goto-char containing-sexp)
8085 (c-forward-sexp -1)
8086 (let (bracepos)
8087 (if (and (or (looking-at c-brace-list-key)
8088 (progn (c-forward-sexp -1)
8089 (looking-at c-brace-list-key)))
8090 (setq bracepos (c-down-list-forward (point)))
8091 (not (c-crosses-statement-barrier-p (point)
8092 (- bracepos 2))))
8093 (point)))))
8094 ;; this will pick up array/aggregate init lists, even if they are nested.
8095 (save-excursion
8096 (let ((class-key
8097 ;; Pike can have class definitions anywhere, so we must
8098 ;; check for the class key here.
8099 (and (c-major-mode-is 'pike-mode)
8100 c-decl-block-key))
8101 bufpos braceassignp lim next-containing)
8102 (while (and (not bufpos)
8103 containing-sexp)
8104 (when paren-state
8105 (if (consp (car paren-state))
8106 (setq lim (cdr (car paren-state))
8107 paren-state (cdr paren-state))
8108 (setq lim (car paren-state)))
8109 (when paren-state
8110 (setq next-containing (car paren-state)
8111 paren-state (cdr paren-state))))
8112 (goto-char containing-sexp)
8113 (if (c-looking-at-inexpr-block next-containing next-containing)
8114 ;; We're in an in-expression block of some kind. Do not
8115 ;; check nesting. We deliberately set the limit to the
8116 ;; containing sexp, so that c-looking-at-inexpr-block
8117 ;; doesn't check for an identifier before it.
8118 (setq containing-sexp nil)
8119 ;; see if the open brace is preceded by = or [...] in
8120 ;; this statement, but watch out for operator=
8121 (setq braceassignp 'dontknow)
8122 (c-backward-token-2 1 t lim)
8123 ;; Checks to do only on the first sexp before the brace.
8124 (when (and c-opt-inexpr-brace-list-key
8125 (eq (char-after) ?\[))
8126 ;; In Java, an initialization brace list may follow
8127 ;; directly after "new Foo[]", so check for a "new"
8128 ;; earlier.
8129 (while (eq braceassignp 'dontknow)
8130 (setq braceassignp
8131 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8132 ((looking-at c-opt-inexpr-brace-list-key) t)
8133 ((looking-at "\\sw\\|\\s_\\|[.[]")
8134 ;; Carry on looking if this is an
8135 ;; identifier (may contain "." in Java)
8136 ;; or another "[]" sexp.
8137 'dontknow)
8138 (t nil)))))
8139 ;; Checks to do on all sexps before the brace, up to the
8140 ;; beginning of the statement.
8141 (while (eq braceassignp 'dontknow)
8142 (cond ((eq (char-after) ?\;)
8143 (setq braceassignp nil))
8144 ((and class-key
8145 (looking-at class-key))
8146 (setq braceassignp nil))
8147 ((eq (char-after) ?=)
8148 ;; We've seen a =, but must check earlier tokens so
8149 ;; that it isn't something that should be ignored.
8150 (setq braceassignp 'maybe)
8151 (while (and (eq braceassignp 'maybe)
8152 (zerop (c-backward-token-2 1 t lim)))
8153 (setq braceassignp
8154 (cond
8155 ;; Check for operator =
8156 ((and c-opt-op-identifier-prefix
8157 (looking-at c-opt-op-identifier-prefix))
8158 nil)
8159 ;; Check for `<opchar>= in Pike.
8160 ((and (c-major-mode-is 'pike-mode)
8161 (or (eq (char-after) ?`)
8162 ;; Special case for Pikes
8163 ;; `[]=, since '[' is not in
8164 ;; the punctuation class.
8165 (and (eq (char-after) ?\[)
8166 (eq (char-before) ?`))))
8167 nil)
8168 ((looking-at "\\s.") 'maybe)
8169 ;; make sure we're not in a C++ template
8170 ;; argument assignment
8171 ((and
8172 (c-major-mode-is 'c++-mode)
8173 (save-excursion
8174 (let ((here (point))
8175 (pos< (progn
8176 (skip-chars-backward "^<>")
8177 (point))))
8178 (and (eq (char-before) ?<)
8179 (not (c-crosses-statement-barrier-p
8180 pos< here))
8181 (not (c-in-literal))
8182 ))))
8183 nil)
8184 (t t))))))
8185 (if (and (eq braceassignp 'dontknow)
8186 (/= (c-backward-token-2 1 t lim) 0))
8187 (setq braceassignp nil)))
8188 (if (not braceassignp)
8189 (if (eq (char-after) ?\;)
8190 ;; Brace lists can't contain a semicolon, so we're done.
8191 (setq containing-sexp nil)
8192 ;; Go up one level.
8193 (setq containing-sexp next-containing
8194 lim nil
8195 next-containing nil))
8196 ;; we've hit the beginning of the aggregate list
8197 (c-beginning-of-statement-1
8198 (c-most-enclosing-brace paren-state))
8199 (setq bufpos (point))))
8201 bufpos))
8204 (defun c-looking-at-special-brace-list (&optional lim)
8205 ;; If we're looking at the start of a pike-style list, ie `({ })',
8206 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
8207 ;; positions and its entry in c-special-brace-lists is returned, nil
8208 ;; otherwise. The ending position is nil if the list is still open.
8209 ;; LIM is the limit for forward search. The point may either be at
8210 ;; the `(' or at the following paren character. Tries to check the
8211 ;; matching closer, but assumes it's correct if no balanced paren is
8212 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8213 ;; a special brace list).
8215 ;; This function might do hidden buffer changes.
8216 (if c-special-brace-lists
8217 (condition-case ()
8218 (save-excursion
8219 (let ((beg (point))
8220 inner-beg end type)
8221 (c-forward-syntactic-ws)
8222 (if (eq (char-after) ?\()
8223 (progn
8224 (forward-char 1)
8225 (c-forward-syntactic-ws)
8226 (setq inner-beg (point))
8227 (setq type (assq (char-after) c-special-brace-lists)))
8228 (if (setq type (assq (char-after) c-special-brace-lists))
8229 (progn
8230 (setq inner-beg (point))
8231 (c-backward-syntactic-ws)
8232 (forward-char -1)
8233 (setq beg (if (eq (char-after) ?\()
8234 (point)
8235 nil)))))
8236 (if (and beg type)
8237 (if (and (c-safe
8238 (goto-char beg)
8239 (c-forward-sexp 1)
8240 (setq end (point))
8241 (= (char-before) ?\)))
8242 (c-safe
8243 (goto-char inner-beg)
8244 (if (looking-at "\\s(")
8245 ;; Check balancing of the inner paren
8246 ;; below.
8247 (progn
8248 (c-forward-sexp 1)
8250 ;; If the inner char isn't a paren then
8251 ;; we can't check balancing, so just
8252 ;; check the char before the outer
8253 ;; closing paren.
8254 (goto-char end)
8255 (backward-char)
8256 (c-backward-syntactic-ws)
8257 (= (char-before) (cdr type)))))
8258 (if (or (/= (char-syntax (char-before)) ?\))
8259 (= (progn
8260 (c-forward-syntactic-ws)
8261 (point))
8262 (1- end)))
8263 (cons (cons beg end) type))
8264 (cons (list beg) type)))))
8265 (error nil))))
8267 (defun c-looking-at-bos (&optional lim)
8268 ;; Return non-nil if between two statements or declarations, assuming
8269 ;; point is not inside a literal or comment.
8271 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8272 ;; are recommended instead.
8274 ;; This function might do hidden buffer changes.
8275 (c-at-statement-start-p))
8276 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8278 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8279 ;; Return non-nil if we're looking at the beginning of a block
8280 ;; inside an expression. The value returned is actually a cons of
8281 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8282 ;; position of the beginning of the construct.
8284 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8285 ;; position of the closest containing list. If it's nil, the
8286 ;; containing paren isn't used to decide whether we're inside an
8287 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8288 ;; needs to be farther back.
8290 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8291 ;; brace block might be done. It should only be used when the
8292 ;; construct can be assumed to be complete, i.e. when the original
8293 ;; starting position was further down than that.
8295 ;; This function might do hidden buffer changes.
8297 (save-excursion
8298 (let ((res 'maybe) passed-paren
8299 (closest-lim (or containing-sexp lim (point-min)))
8300 ;; Look at the character after point only as a last resort
8301 ;; when we can't disambiguate.
8302 (block-follows (and (eq (char-after) ?{) (point))))
8304 (while (and (eq res 'maybe)
8305 (progn (c-backward-syntactic-ws)
8306 (> (point) closest-lim))
8307 (not (bobp))
8308 (progn (backward-char)
8309 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8310 (c-safe (forward-char)
8311 (goto-char (scan-sexps (point) -1))))
8313 (setq res
8314 (if (looking-at c-keywords-regexp)
8315 (let ((kw-sym (c-keyword-sym (match-string 1))))
8316 (cond
8317 ((and block-follows
8318 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8319 (and (not (eq passed-paren ?\[))
8320 (or (not (looking-at c-class-key))
8321 ;; If the class definition is at the start of
8322 ;; a statement, we don't consider it an
8323 ;; in-expression class.
8324 (let ((prev (point)))
8325 (while (and
8326 (= (c-backward-token-2 1 nil closest-lim) 0)
8327 (eq (char-syntax (char-after)) ?w))
8328 (setq prev (point)))
8329 (goto-char prev)
8330 (not (c-at-statement-start-p)))
8331 ;; Also, in Pike we treat it as an
8332 ;; in-expression class if it's used in an
8333 ;; object clone expression.
8334 (save-excursion
8335 (and check-at-end
8336 (c-major-mode-is 'pike-mode)
8337 (progn (goto-char block-follows)
8338 (zerop (c-forward-token-2 1 t)))
8339 (eq (char-after) ?\())))
8340 (cons 'inexpr-class (point))))
8341 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8342 (when (not passed-paren)
8343 (cons 'inexpr-statement (point))))
8344 ((c-keyword-member kw-sym 'c-lambda-kwds)
8345 (when (or (not passed-paren)
8346 (eq passed-paren ?\())
8347 (cons 'inlambda (point))))
8348 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8349 nil)
8351 'maybe)))
8353 (if (looking-at "\\s(")
8354 (if passed-paren
8355 (if (and (eq passed-paren ?\[)
8356 (eq (char-after) ?\[))
8357 ;; Accept several square bracket sexps for
8358 ;; Java array initializations.
8359 'maybe)
8360 (setq passed-paren (char-after))
8361 'maybe)
8362 'maybe))))
8364 (if (eq res 'maybe)
8365 (when (and c-recognize-paren-inexpr-blocks
8366 block-follows
8367 containing-sexp
8368 (eq (char-after containing-sexp) ?\())
8369 (goto-char containing-sexp)
8370 (if (or (save-excursion
8371 (c-backward-syntactic-ws lim)
8372 (and (> (point) (or lim (point-min)))
8373 (c-on-identifier)))
8374 (and c-special-brace-lists
8375 (c-looking-at-special-brace-list)))
8377 (cons 'inexpr-statement (point))))
8379 res))))
8381 (defun c-looking-at-inexpr-block-backward (paren-state)
8382 ;; Returns non-nil if we're looking at the end of an in-expression
8383 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8384 ;; PAREN-STATE is the paren state relevant at the current position.
8386 ;; This function might do hidden buffer changes.
8387 (save-excursion
8388 ;; We currently only recognize a block.
8389 (let ((here (point))
8390 (elem (car-safe paren-state))
8391 containing-sexp)
8392 (when (and (consp elem)
8393 (progn (goto-char (cdr elem))
8394 (c-forward-syntactic-ws here)
8395 (= (point) here)))
8396 (goto-char (car elem))
8397 (if (setq paren-state (cdr paren-state))
8398 (setq containing-sexp (car-safe paren-state)))
8399 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8400 paren-state)
8401 containing-sexp)))))
8404 ;; `c-guess-basic-syntax' and the functions that precedes it below
8405 ;; implements the main decision tree for determining the syntactic
8406 ;; analysis of the current line of code.
8408 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8409 ;; auto newline analysis.
8410 (defvar c-auto-newline-analysis nil)
8412 (defun c-brace-anchor-point (bracepos)
8413 ;; BRACEPOS is the position of a brace in a construct like "namespace
8414 ;; Bar {". Return the anchor point in this construct; this is the
8415 ;; earliest symbol on the brace's line which isn't earlier than
8416 ;; "namespace".
8418 ;; Currently (2007-08-17), "like namespace" means "matches
8419 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8420 ;; or anything like that.
8421 (save-excursion
8422 (let ((boi (c-point 'boi bracepos)))
8423 (goto-char bracepos)
8424 (while (and (> (point) boi)
8425 (not (looking-at c-other-decl-block-key)))
8426 (c-backward-token-2))
8427 (if (> (point) boi) (point) boi))))
8429 (defsubst c-add-syntax (symbol &rest args)
8430 ;; A simple function to prepend a new syntax element to
8431 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8432 ;; should always be dynamically bound but since we read it first
8433 ;; we'll fail properly anyway if this function is misused.
8434 (setq c-syntactic-context (cons (cons symbol args)
8435 c-syntactic-context)))
8437 (defsubst c-append-syntax (symbol &rest args)
8438 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8439 ;; (Normally not necessary.)
8440 (setq c-syntactic-context (nconc c-syntactic-context
8441 (list (cons symbol args)))))
8443 (defun c-add-stmt-syntax (syntax-symbol
8444 syntax-extra-args
8445 stop-at-boi-only
8446 containing-sexp
8447 paren-state)
8448 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8449 ;; needed with further syntax elements of the types `substatement',
8450 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8451 ;; `defun-block-intro'.
8453 ;; Do the generic processing to anchor the given syntax symbol on
8454 ;; the preceding statement: Skip over any labels and containing
8455 ;; statements on the same line, and then search backward until we
8456 ;; find a statement or block start that begins at boi without a
8457 ;; label or comment.
8459 ;; Point is assumed to be at the prospective anchor point for the
8460 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8461 ;; skip past open parens and containing statements. Most of the added
8462 ;; syntax elements will get the same anchor point - the exception is
8463 ;; for an anchor in a construct like "namespace"[*] - this is as early
8464 ;; as possible in the construct but on the same line as the {.
8466 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8468 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8469 ;; syntax symbol. They are appended after the anchor point.
8471 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8472 ;; if the current statement starts there.
8474 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8475 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8477 ;; This function might do hidden buffer changes.
8479 (if (= (point) (c-point 'boi))
8480 ;; This is by far the most common case, so let's give it special
8481 ;; treatment.
8482 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
8484 (let ((syntax-last c-syntactic-context)
8485 (boi (c-point 'boi))
8486 ;; Set when we're on a label, so that we don't stop there.
8487 ;; FIXME: To be complete we should check if we're on a label
8488 ;; now at the start.
8489 on-label)
8491 ;; Use point as the anchor point for "namespace", "extern", etc.
8492 (apply 'c-add-syntax syntax-symbol
8493 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
8494 (point) nil)
8495 syntax-extra-args)
8497 ;; Loop while we have to back out of containing blocks.
8498 (while
8499 (and
8500 (catch 'back-up-block
8502 ;; Loop while we have to back up statements.
8503 (while (or (/= (point) boi)
8504 on-label
8505 (looking-at c-comment-start-regexp))
8507 ;; Skip past any comments that stands between the
8508 ;; statement start and boi.
8509 (let ((savepos (point)))
8510 (while (and (/= savepos boi)
8511 (c-backward-single-comment))
8512 (setq savepos (point)
8513 boi (c-point 'boi)))
8514 (goto-char savepos))
8516 ;; Skip to the beginning of this statement or backward
8517 ;; another one.
8518 (let ((old-pos (point))
8519 (old-boi boi)
8520 (step-type (c-beginning-of-statement-1 containing-sexp)))
8521 (setq boi (c-point 'boi)
8522 on-label (eq step-type 'label))
8524 (cond ((= (point) old-pos)
8525 ;; If we didn't move we're at the start of a block and
8526 ;; have to continue outside it.
8527 (throw 'back-up-block t))
8529 ((and (eq step-type 'up)
8530 (>= (point) old-boi)
8531 (looking-at "else\\>[^_]")
8532 (save-excursion
8533 (goto-char old-pos)
8534 (looking-at "if\\>[^_]")))
8535 ;; Special case to avoid deeper and deeper indentation
8536 ;; of "else if" clauses.
8539 ((and (not stop-at-boi-only)
8540 (/= old-pos old-boi)
8541 (memq step-type '(up previous)))
8542 ;; If stop-at-boi-only is nil, we shouldn't back up
8543 ;; over previous or containing statements to try to
8544 ;; reach boi, so go back to the last position and
8545 ;; exit.
8546 (goto-char old-pos)
8547 (throw 'back-up-block nil))
8550 (if (and (not stop-at-boi-only)
8551 (memq step-type '(up previous beginning)))
8552 ;; If we've moved into another statement then we
8553 ;; should no longer try to stop in the middle of a
8554 ;; line.
8555 (setq stop-at-boi-only t))
8557 ;; Record this as a substatement if we skipped up one
8558 ;; level.
8559 (when (eq step-type 'up)
8560 (c-add-syntax 'substatement nil))))
8563 containing-sexp)
8565 ;; Now we have to go out of this block.
8566 (goto-char containing-sexp)
8568 ;; Don't stop in the middle of a special brace list opener
8569 ;; like "({".
8570 (when c-special-brace-lists
8571 (let ((special-list (c-looking-at-special-brace-list)))
8572 (when (and special-list
8573 (< (car (car special-list)) (point)))
8574 (setq containing-sexp (car (car special-list)))
8575 (goto-char containing-sexp))))
8577 (setq paren-state (c-whack-state-after containing-sexp paren-state)
8578 containing-sexp (c-most-enclosing-brace paren-state)
8579 boi (c-point 'boi))
8581 ;; Analyze the construct in front of the block we've stepped out
8582 ;; from and add the right syntactic element for it.
8583 (let ((paren-pos (point))
8584 (paren-char (char-after))
8585 step-type)
8587 (if (eq paren-char ?\()
8588 ;; Stepped out of a parenthesis block, so we're in an
8589 ;; expression now.
8590 (progn
8591 (when (/= paren-pos boi)
8592 (if (and c-recognize-paren-inexpr-blocks
8593 (progn
8594 (c-backward-syntactic-ws containing-sexp)
8595 (or (not (looking-at "\\>"))
8596 (not (c-on-identifier))))
8597 (save-excursion
8598 (goto-char (1+ paren-pos))
8599 (c-forward-syntactic-ws)
8600 (eq (char-after) ?{)))
8601 ;; Stepped out of an in-expression statement. This
8602 ;; syntactic element won't get an anchor pos.
8603 (c-add-syntax 'inexpr-statement)
8605 ;; A parenthesis normally belongs to an arglist.
8606 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
8608 (goto-char (max boi
8609 (if containing-sexp
8610 (1+ containing-sexp)
8611 (point-min))))
8612 (setq step-type 'same
8613 on-label nil))
8615 ;; Stepped out of a brace block.
8616 (setq step-type (c-beginning-of-statement-1 containing-sexp)
8617 on-label (eq step-type 'label))
8619 (if (and (eq step-type 'same)
8620 (/= paren-pos (point)))
8621 (let (inexpr)
8622 (cond
8623 ((save-excursion
8624 (goto-char paren-pos)
8625 (setq inexpr (c-looking-at-inexpr-block
8626 (c-safe-position containing-sexp paren-state)
8627 containing-sexp)))
8628 (c-add-syntax (if (eq (car inexpr) 'inlambda)
8629 'defun-block-intro
8630 'statement-block-intro)
8631 nil))
8632 ((looking-at c-other-decl-block-key)
8633 (c-add-syntax
8634 (cdr (assoc (match-string 1)
8635 c-other-decl-block-key-in-symbols-alist))
8636 (max (c-point 'boi paren-pos) (point))))
8637 (t (c-add-syntax 'defun-block-intro nil))))
8639 (c-add-syntax 'statement-block-intro nil)))
8641 (if (= paren-pos boi)
8642 ;; Always done if the open brace was at boi. The
8643 ;; c-beginning-of-statement-1 call above is necessary
8644 ;; anyway, to decide the type of block-intro to add.
8645 (goto-char paren-pos)
8646 (setq boi (c-point 'boi)))
8649 ;; Fill in the current point as the anchor for all the symbols
8650 ;; added above.
8651 (let ((p c-syntactic-context) q)
8652 (while (not (eq p syntax-last))
8653 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8654 (while q
8655 (unless (car q)
8656 (setcar q (point)))
8657 (setq q (cdr q)))
8658 (setq p (cdr p))))
8661 (defun c-add-class-syntax (symbol
8662 containing-decl-open
8663 containing-decl-start
8664 containing-decl-kwd
8665 paren-state)
8666 ;; The inclass and class-close syntactic symbols are added in
8667 ;; several places and some work is needed to fix everything.
8668 ;; Therefore it's collected here.
8670 ;; This function might do hidden buffer changes.
8671 (goto-char containing-decl-open)
8672 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
8673 (progn
8674 (c-add-syntax symbol containing-decl-open)
8675 containing-decl-open)
8676 (goto-char containing-decl-start)
8677 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8678 ;; here, but we have to do like this for compatibility.
8679 (back-to-indentation)
8680 (c-add-syntax symbol (point))
8681 (if (and (c-keyword-member containing-decl-kwd
8682 'c-inexpr-class-kwds)
8683 (/= containing-decl-start (c-point 'boi containing-decl-start)))
8684 (c-add-syntax 'inexpr-class))
8685 (point)))
8687 (defun c-guess-continued-construct (indent-point
8688 char-after-ip
8689 beg-of-same-or-containing-stmt
8690 containing-sexp
8691 paren-state)
8692 ;; This function contains the decision tree reached through both
8693 ;; cases 18 and 10. It's a continued statement or top level
8694 ;; construct of some kind.
8696 ;; This function might do hidden buffer changes.
8698 (let (special-brace-list placeholder)
8699 (goto-char indent-point)
8700 (skip-chars-forward " \t")
8702 (cond
8703 ;; (CASE A removed.)
8704 ;; CASE B: open braces for class or brace-lists
8705 ((setq special-brace-list
8706 (or (and c-special-brace-lists
8707 (c-looking-at-special-brace-list))
8708 (eq char-after-ip ?{)))
8710 (cond
8711 ;; CASE B.1: class-open
8712 ((save-excursion
8713 (and (eq (char-after) ?{)
8714 (c-looking-at-decl-block containing-sexp t)
8715 (setq beg-of-same-or-containing-stmt (point))))
8716 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
8718 ;; CASE B.2: brace-list-open
8719 ((or (consp special-brace-list)
8720 (save-excursion
8721 (goto-char beg-of-same-or-containing-stmt)
8722 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8723 indent-point t t t)))
8724 ;; The most semantically accurate symbol here is
8725 ;; brace-list-open, but we normally report it simply as a
8726 ;; statement-cont. The reason is that one normally adjusts
8727 ;; brace-list-open for brace lists as top-level constructs,
8728 ;; and brace lists inside statements is a completely different
8729 ;; context. C.f. case 5A.3.
8730 (c-beginning-of-statement-1 containing-sexp)
8731 (c-add-stmt-syntax (if c-auto-newline-analysis
8732 ;; Turn off the dwim above when we're
8733 ;; analyzing the nature of the brace
8734 ;; for the auto newline feature.
8735 'brace-list-open
8736 'statement-cont)
8737 nil nil
8738 containing-sexp paren-state))
8740 ;; CASE B.3: The body of a function declared inside a normal
8741 ;; block. Can occur e.g. in Pike and when using gcc
8742 ;; extensions, but watch out for macros followed by blocks.
8743 ;; C.f. cases E, 16F and 17G.
8744 ((and (not (c-at-statement-start-p))
8745 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8746 'same)
8747 (save-excursion
8748 (let ((c-recognize-typeless-decls nil))
8749 ;; Turn off recognition of constructs that lacks a
8750 ;; type in this case, since that's more likely to be
8751 ;; a macro followed by a block.
8752 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8753 (c-add-stmt-syntax 'defun-open nil t
8754 containing-sexp paren-state))
8756 ;; CASE B.4: Continued statement with block open. The most
8757 ;; accurate analysis is perhaps `statement-cont' together with
8758 ;; `block-open' but we play DWIM and use `substatement-open'
8759 ;; instead. The rationaly is that this typically is a macro
8760 ;; followed by a block which makes it very similar to a
8761 ;; statement with a substatement block.
8763 (c-add-stmt-syntax 'substatement-open nil nil
8764 containing-sexp paren-state))
8767 ;; CASE C: iostream insertion or extraction operator
8768 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8769 (save-excursion
8770 (goto-char beg-of-same-or-containing-stmt)
8771 ;; If there is no preceding streamop in the statement
8772 ;; then indent this line as a normal statement-cont.
8773 (when (c-syntactic-re-search-forward
8774 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
8775 (c-add-syntax 'stream-op (c-point 'boi))
8776 t))))
8778 ;; CASE E: In the "K&R region" of a function declared inside a
8779 ;; normal block. C.f. case B.3.
8780 ((and (save-excursion
8781 ;; Check that the next token is a '{'. This works as
8782 ;; long as no language that allows nested function
8783 ;; definitions allows stuff like member init lists, K&R
8784 ;; declarations or throws clauses there.
8786 ;; Note that we do a forward search for something ahead
8787 ;; of the indentation line here. That's not good since
8788 ;; the user might not have typed it yet. Unfortunately
8789 ;; it's exceedingly tricky to recognize a function
8790 ;; prototype in a code block without resorting to this.
8791 (c-forward-syntactic-ws)
8792 (eq (char-after) ?{))
8793 (not (c-at-statement-start-p))
8794 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8795 'same)
8796 (save-excursion
8797 (let ((c-recognize-typeless-decls nil))
8798 ;; Turn off recognition of constructs that lacks a
8799 ;; type in this case, since that's more likely to be
8800 ;; a macro followed by a block.
8801 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8802 (c-add-stmt-syntax 'func-decl-cont nil t
8803 containing-sexp paren-state))
8805 ;;CASE F: continued statement and the only preceding items are
8806 ;;annotations.
8807 ((and (c-major-mode-is 'java-mode)
8808 (setq placeholder (point))
8809 (c-beginning-of-statement-1)
8810 (progn
8811 (while (and (c-forward-annotation)
8812 (< (point) placeholder))
8813 (c-forward-syntactic-ws))
8815 (prog1
8816 (>= (point) placeholder)
8817 (goto-char placeholder)))
8818 (c-beginning-of-statement-1 containing-sexp)
8819 (c-add-syntax 'annotation-var-cont (point)))
8821 ;; CASE G: a template list continuation?
8822 ;; Mostly a duplication of case 5D.3 to fix templates-19:
8823 ((and (c-major-mode-is 'c++-mode)
8824 (save-excursion
8825 (goto-char indent-point)
8826 (c-with-syntax-table c++-template-syntax-table
8827 (setq placeholder (c-up-list-backward)))
8828 (and placeholder
8829 (eq (char-after placeholder) ?<)
8830 (/= (char-before placeholder) ?<)
8831 (progn
8832 (goto-char (1+ placeholder))
8833 (not (looking-at c-<-op-cont-regexp))))))
8834 (c-with-syntax-table c++-template-syntax-table
8835 (goto-char placeholder)
8836 (c-beginning-of-statement-1 containing-sexp t)
8837 (if (save-excursion
8838 (c-backward-syntactic-ws containing-sexp)
8839 (eq (char-before) ?<))
8840 ;; In a nested template arglist.
8841 (progn
8842 (goto-char placeholder)
8843 (c-syntactic-skip-backward "^,;" containing-sexp t)
8844 (c-forward-syntactic-ws))
8845 (back-to-indentation)))
8846 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8847 ;; template aware.
8848 (c-add-syntax 'template-args-cont (point) placeholder))
8850 ;; CASE D: continued statement.
8852 (c-beginning-of-statement-1 containing-sexp)
8853 (c-add-stmt-syntax 'statement-cont nil nil
8854 containing-sexp paren-state))
8857 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
8858 ;; 2005/11/29).
8859 ;;;###autoload
8860 (defun c-guess-basic-syntax ()
8861 "Return the syntactic context of the current line."
8862 (save-excursion
8863 (beginning-of-line)
8864 (c-save-buffer-state
8865 ((indent-point (point))
8866 (case-fold-search nil)
8867 ;; A whole ugly bunch of various temporary variables. Have
8868 ;; to declare them here since it's not possible to declare
8869 ;; a variable with only the scope of a cond test and the
8870 ;; following result clauses, and most of this function is a
8871 ;; single gigantic cond. :P
8872 literal char-before-ip before-ws-ip char-after-ip macro-start
8873 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
8874 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
8875 containing-<
8876 ;; The following record some positions for the containing
8877 ;; declaration block if we're directly within one:
8878 ;; `containing-decl-open' is the position of the open
8879 ;; brace. `containing-decl-start' is the start of the
8880 ;; declaration. `containing-decl-kwd' is the keyword
8881 ;; symbol of the keyword that tells what kind of block it
8882 ;; is.
8883 containing-decl-open
8884 containing-decl-start
8885 containing-decl-kwd
8886 ;; The open paren of the closest surrounding sexp or nil if
8887 ;; there is none.
8888 containing-sexp
8889 ;; The position after the closest preceding brace sexp
8890 ;; (nested sexps are ignored), or the position after
8891 ;; `containing-sexp' if there is none, or (point-min) if
8892 ;; `containing-sexp' is nil.
8894 ;; The paren state outside `containing-sexp', or at
8895 ;; `indent-point' if `containing-sexp' is nil.
8896 (paren-state (c-parse-state))
8897 ;; There's always at most one syntactic element which got
8898 ;; an anchor pos. It's stored in syntactic-relpos.
8899 syntactic-relpos
8900 (c-stmt-delim-chars c-stmt-delim-chars))
8902 ;; Check if we're directly inside an enclosing declaration
8903 ;; level block.
8904 (when (and (setq containing-sexp
8905 (c-most-enclosing-brace paren-state))
8906 (progn
8907 (goto-char containing-sexp)
8908 (eq (char-after) ?{))
8909 (setq placeholder
8910 (c-looking-at-decl-block
8911 (c-most-enclosing-brace paren-state
8912 containing-sexp)
8913 t)))
8914 (setq containing-decl-open containing-sexp
8915 containing-decl-start (point)
8916 containing-sexp nil)
8917 (goto-char placeholder)
8918 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
8919 (c-keyword-sym (match-string 1)))))
8921 ;; Init some position variables.
8922 (if c-state-cache
8923 (progn
8924 (setq containing-sexp (car paren-state)
8925 paren-state (cdr paren-state))
8926 (if (consp containing-sexp)
8927 (progn
8928 (setq lim (cdr containing-sexp))
8929 (if (cdr c-state-cache)
8930 ;; Ignore balanced paren. The next entry
8931 ;; can't be another one.
8932 (setq containing-sexp (car (cdr c-state-cache))
8933 paren-state (cdr paren-state))
8934 ;; If there is no surrounding open paren then
8935 ;; put the last balanced pair back on paren-state.
8936 (setq paren-state (cons containing-sexp paren-state)
8937 containing-sexp nil)))
8938 (setq lim (1+ containing-sexp))))
8939 (setq lim (point-min)))
8941 ;; If we're in a parenthesis list then ',' delimits the
8942 ;; "statements" rather than being an operator (with the
8943 ;; exception of the "for" clause). This difference is
8944 ;; typically only noticeable when statements are used in macro
8945 ;; arglists.
8946 (when (and containing-sexp
8947 (eq (char-after containing-sexp) ?\())
8948 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
8949 ;; cache char before and after indent point, and move point to
8950 ;; the most likely position to perform the majority of tests
8951 (goto-char indent-point)
8952 (c-backward-syntactic-ws lim)
8953 (setq before-ws-ip (point)
8954 char-before-ip (char-before))
8955 (goto-char indent-point)
8956 (skip-chars-forward " \t")
8957 (setq char-after-ip (char-after))
8959 ;; are we in a literal?
8960 (setq literal (c-in-literal lim))
8962 ;; now figure out syntactic qualities of the current line
8963 (cond
8965 ;; CASE 1: in a string.
8966 ((eq literal 'string)
8967 (c-add-syntax 'string (c-point 'bopl)))
8969 ;; CASE 2: in a C or C++ style comment.
8970 ((and (memq literal '(c c++))
8971 ;; This is a kludge for XEmacs where we use
8972 ;; `buffer-syntactic-context', which doesn't correctly
8973 ;; recognize "\*/" to end a block comment.
8974 ;; `parse-partial-sexp' which is used by
8975 ;; `c-literal-limits' will however do that in most
8976 ;; versions, which results in that we get nil from
8977 ;; `c-literal-limits' even when `c-in-literal' claims
8978 ;; we're inside a comment.
8979 (setq placeholder (c-literal-limits lim)))
8980 (c-add-syntax literal (car placeholder)))
8982 ;; CASE 3: in a cpp preprocessor macro continuation.
8983 ((and (save-excursion
8984 (when (c-beginning-of-macro)
8985 (setq macro-start (point))))
8986 (/= macro-start (c-point 'boi))
8987 (progn
8988 (setq tmpsymbol 'cpp-macro-cont)
8989 (or (not c-syntactic-indentation-in-macros)
8990 (save-excursion
8991 (goto-char macro-start)
8992 ;; If at the beginning of the body of a #define
8993 ;; directive then analyze as cpp-define-intro
8994 ;; only. Go on with the syntactic analysis
8995 ;; otherwise. in-macro-expr is set if we're in a
8996 ;; cpp expression, i.e. before the #define body
8997 ;; or anywhere in a non-#define directive.
8998 (if (c-forward-to-cpp-define-body)
8999 (let ((indent-boi (c-point 'boi indent-point)))
9000 (setq in-macro-expr (> (point) indent-boi)
9001 tmpsymbol 'cpp-define-intro)
9002 (= (point) indent-boi))
9003 (setq in-macro-expr t)
9004 nil)))))
9005 (c-add-syntax tmpsymbol macro-start)
9006 (setq macro-start nil))
9008 ;; CASE 11: an else clause?
9009 ((looking-at "else\\>[^_]")
9010 (c-beginning-of-statement-1 containing-sexp)
9011 (c-add-stmt-syntax 'else-clause nil t
9012 containing-sexp paren-state))
9014 ;; CASE 12: while closure of a do/while construct?
9015 ((and (looking-at "while\\>[^_]")
9016 (save-excursion
9017 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
9018 'beginning)
9019 (setq placeholder (point)))))
9020 (goto-char placeholder)
9021 (c-add-stmt-syntax 'do-while-closure nil t
9022 containing-sexp paren-state))
9024 ;; CASE 13: A catch or finally clause? This case is simpler
9025 ;; than if-else and do-while, because a block is required
9026 ;; after every try, catch and finally.
9027 ((save-excursion
9028 (and (cond ((c-major-mode-is 'c++-mode)
9029 (looking-at "catch\\>[^_]"))
9030 ((c-major-mode-is 'java-mode)
9031 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
9032 (and (c-safe (c-backward-syntactic-ws)
9033 (c-backward-sexp)
9035 (eq (char-after) ?{)
9036 (c-safe (c-backward-syntactic-ws)
9037 (c-backward-sexp)
9039 (if (eq (char-after) ?\()
9040 (c-safe (c-backward-sexp) t)
9042 (looking-at "\\(try\\|catch\\)\\>[^_]")
9043 (setq placeholder (point))))
9044 (goto-char placeholder)
9045 (c-add-stmt-syntax 'catch-clause nil t
9046 containing-sexp paren-state))
9048 ;; CASE 18: A substatement we can recognize by keyword.
9049 ((save-excursion
9050 (and c-opt-block-stmt-key
9051 (not (eq char-before-ip ?\;))
9052 (not (c-at-vsemi-p before-ws-ip))
9053 (not (memq char-after-ip '(?\) ?\] ?,)))
9054 (or (not (eq char-before-ip ?}))
9055 (c-looking-at-inexpr-block-backward c-state-cache))
9056 (> (point)
9057 (progn
9058 ;; Ought to cache the result from the
9059 ;; c-beginning-of-statement-1 calls here.
9060 (setq placeholder (point))
9061 (while (eq (setq step-type
9062 (c-beginning-of-statement-1 lim))
9063 'label))
9064 (if (eq step-type 'previous)
9065 (goto-char placeholder)
9066 (setq placeholder (point))
9067 (if (and (eq step-type 'same)
9068 (not (looking-at c-opt-block-stmt-key)))
9069 ;; Step up to the containing statement if we
9070 ;; stayed in the same one.
9071 (let (step)
9072 (while (eq
9073 (setq step
9074 (c-beginning-of-statement-1 lim))
9075 'label))
9076 (if (eq step 'up)
9077 (setq placeholder (point))
9078 ;; There was no containing statement afterall.
9079 (goto-char placeholder)))))
9080 placeholder))
9081 (if (looking-at c-block-stmt-2-key)
9082 ;; Require a parenthesis after these keywords.
9083 ;; Necessary to catch e.g. synchronized in Java,
9084 ;; which can be used both as statement and
9085 ;; modifier.
9086 (and (zerop (c-forward-token-2 1 nil))
9087 (eq (char-after) ?\())
9088 (looking-at c-opt-block-stmt-key))))
9090 (if (eq step-type 'up)
9091 ;; CASE 18A: Simple substatement.
9092 (progn
9093 (goto-char placeholder)
9094 (cond
9095 ((eq char-after-ip ?{)
9096 (c-add-stmt-syntax 'substatement-open nil nil
9097 containing-sexp paren-state))
9098 ((save-excursion
9099 (goto-char indent-point)
9100 (back-to-indentation)
9101 (c-forward-label))
9102 (c-add-stmt-syntax 'substatement-label nil nil
9103 containing-sexp paren-state))
9105 (c-add-stmt-syntax 'substatement nil nil
9106 containing-sexp paren-state))))
9108 ;; CASE 18B: Some other substatement. This is shared
9109 ;; with case 10.
9110 (c-guess-continued-construct indent-point
9111 char-after-ip
9112 placeholder
9114 paren-state)))
9116 ;; CASE 14: A case or default label
9117 ((looking-at c-label-kwds-regexp)
9118 (if containing-sexp
9119 (progn
9120 (goto-char containing-sexp)
9121 (setq lim (c-most-enclosing-brace c-state-cache
9122 containing-sexp))
9123 (c-backward-to-block-anchor lim)
9124 (c-add-stmt-syntax 'case-label nil t lim paren-state))
9125 ;; Got a bogus label at the top level. In lack of better
9126 ;; alternatives, anchor it on (point-min).
9127 (c-add-syntax 'case-label (point-min))))
9129 ;; CASE 15: any other label
9130 ((save-excursion
9131 (back-to-indentation)
9132 (and (not (looking-at c-syntactic-ws-start))
9133 (c-forward-label)))
9134 (cond (containing-decl-open
9135 (setq placeholder (c-add-class-syntax 'inclass
9136 containing-decl-open
9137 containing-decl-start
9138 containing-decl-kwd
9139 paren-state))
9140 ;; Append access-label with the same anchor point as
9141 ;; inclass gets.
9142 (c-append-syntax 'access-label placeholder))
9144 (containing-sexp
9145 (goto-char containing-sexp)
9146 (setq lim (c-most-enclosing-brace c-state-cache
9147 containing-sexp))
9148 (save-excursion
9149 (setq tmpsymbol
9150 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
9151 (looking-at "switch\\>[^_]"))
9152 ;; If the surrounding statement is a switch then
9153 ;; let's analyze all labels as switch labels, so
9154 ;; that they get lined up consistently.
9155 'case-label
9156 'label)))
9157 (c-backward-to-block-anchor lim)
9158 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
9161 ;; A label on the top level. Treat it as a class
9162 ;; context. (point-min) is the closest we get to the
9163 ;; class open brace.
9164 (c-add-syntax 'access-label (point-min)))))
9166 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9167 ;; 17E.
9168 ((setq placeholder (c-looking-at-inexpr-block
9169 (c-safe-position containing-sexp paren-state)
9170 containing-sexp
9171 ;; Have to turn on the heuristics after
9172 ;; the point even though it doesn't work
9173 ;; very well. C.f. test case class-16.pike.
9175 (setq tmpsymbol (assq (car placeholder)
9176 '((inexpr-class . class-open)
9177 (inexpr-statement . block-open))))
9178 (if tmpsymbol
9179 ;; It's a statement block or an anonymous class.
9180 (setq tmpsymbol (cdr tmpsymbol))
9181 ;; It's a Pike lambda. Check whether we are between the
9182 ;; lambda keyword and the argument list or at the defun
9183 ;; opener.
9184 (setq tmpsymbol (if (eq char-after-ip ?{)
9185 'inline-open
9186 'lambda-intro-cont)))
9187 (goto-char (cdr placeholder))
9188 (back-to-indentation)
9189 (c-add-stmt-syntax tmpsymbol nil t
9190 (c-most-enclosing-brace c-state-cache (point))
9191 paren-state)
9192 (unless (eq (point) (cdr placeholder))
9193 (c-add-syntax (car placeholder))))
9195 ;; CASE 5: Line is inside a declaration level block or at top level.
9196 ((or containing-decl-open (null containing-sexp))
9197 (cond
9199 ;; CASE 5A: we are looking at a defun, brace list, class,
9200 ;; or inline-inclass method opening brace
9201 ((setq special-brace-list
9202 (or (and c-special-brace-lists
9203 (c-looking-at-special-brace-list))
9204 (eq char-after-ip ?{)))
9205 (cond
9207 ;; CASE 5A.1: Non-class declaration block open.
9208 ((save-excursion
9209 (let (tmp)
9210 (and (eq char-after-ip ?{)
9211 (setq tmp (c-looking-at-decl-block containing-sexp t))
9212 (progn
9213 (setq placeholder (point))
9214 (goto-char tmp)
9215 (looking-at c-symbol-key))
9216 (c-keyword-member
9217 (c-keyword-sym (setq keyword (match-string 0)))
9218 'c-other-block-decl-kwds))))
9219 (goto-char placeholder)
9220 (c-add-stmt-syntax
9221 (if (string-equal keyword "extern")
9222 ;; Special case for extern-lang-open.
9223 'extern-lang-open
9224 (intern (concat keyword "-open")))
9225 nil t containing-sexp paren-state))
9227 ;; CASE 5A.2: we are looking at a class opening brace
9228 ((save-excursion
9229 (goto-char indent-point)
9230 (skip-chars-forward " \t")
9231 (and (eq (char-after) ?{)
9232 (c-looking-at-decl-block containing-sexp t)
9233 (setq placeholder (point))))
9234 (c-add-syntax 'class-open placeholder))
9236 ;; CASE 5A.3: brace list open
9237 ((save-excursion
9238 (c-beginning-of-decl-1 lim)
9239 (while (looking-at c-specifier-key)
9240 (goto-char (match-end 1))
9241 (c-forward-syntactic-ws indent-point))
9242 (setq placeholder (c-point 'boi))
9243 (or (consp special-brace-list)
9244 (and (or (save-excursion
9245 (goto-char indent-point)
9246 (setq tmpsymbol nil)
9247 (while (and (> (point) placeholder)
9248 (zerop (c-backward-token-2 1 t))
9249 (/= (char-after) ?=))
9250 (and c-opt-inexpr-brace-list-key
9251 (not tmpsymbol)
9252 (looking-at c-opt-inexpr-brace-list-key)
9253 (setq tmpsymbol 'topmost-intro-cont)))
9254 (eq (char-after) ?=))
9255 (looking-at c-brace-list-key))
9256 (save-excursion
9257 (while (and (< (point) indent-point)
9258 (zerop (c-forward-token-2 1 t))
9259 (not (memq (char-after) '(?\; ?\()))))
9260 (not (memq (char-after) '(?\; ?\()))
9261 ))))
9262 (if (and (not c-auto-newline-analysis)
9263 (c-major-mode-is 'java-mode)
9264 (eq tmpsymbol 'topmost-intro-cont))
9265 ;; We're in Java and have found that the open brace
9266 ;; belongs to a "new Foo[]" initialization list,
9267 ;; which means the brace list is part of an
9268 ;; expression and not a top level definition. We
9269 ;; therefore treat it as any topmost continuation
9270 ;; even though the semantically correct symbol still
9271 ;; is brace-list-open, on the same grounds as in
9272 ;; case B.2.
9273 (progn
9274 (c-beginning-of-statement-1 lim)
9275 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9276 (c-add-syntax 'brace-list-open placeholder)))
9278 ;; CASE 5A.4: inline defun open
9279 ((and containing-decl-open
9280 (not (c-keyword-member containing-decl-kwd
9281 'c-other-block-decl-kwds)))
9282 (c-add-syntax 'inline-open)
9283 (c-add-class-syntax 'inclass
9284 containing-decl-open
9285 containing-decl-start
9286 containing-decl-kwd
9287 paren-state))
9289 ;; CASE 5A.5: ordinary defun open
9291 (save-excursion
9292 (c-beginning-of-decl-1 lim)
9293 (while (looking-at c-specifier-key)
9294 (goto-char (match-end 1))
9295 (c-forward-syntactic-ws indent-point))
9296 (c-add-syntax 'defun-open (c-point 'boi))
9297 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9298 ;; 2007-11-09)
9299 ))))
9301 ;; CASE 5B: After a function header but before the body (or
9302 ;; the ending semicolon if there's no body).
9303 ((save-excursion
9304 (when (setq placeholder (c-just-after-func-arglist-p lim))
9305 (setq tmp-pos (point))))
9306 (cond
9308 ;; CASE 5B.1: Member init list.
9309 ((eq (char-after tmp-pos) ?:)
9310 (if (or (> tmp-pos indent-point)
9311 (= (c-point 'bosws) (1+ tmp-pos)))
9312 (progn
9313 ;; There is no preceding member init clause.
9314 ;; Indent relative to the beginning of indentation
9315 ;; for the topmost-intro line that contains the
9316 ;; prototype's open paren.
9317 (goto-char placeholder)
9318 (c-add-syntax 'member-init-intro (c-point 'boi)))
9319 ;; Indent relative to the first member init clause.
9320 (goto-char (1+ tmp-pos))
9321 (c-forward-syntactic-ws)
9322 (c-add-syntax 'member-init-cont (point))))
9324 ;; CASE 5B.2: K&R arg decl intro
9325 ((and c-recognize-knr-p
9326 (c-in-knr-argdecl lim))
9327 (c-beginning-of-statement-1 lim)
9328 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9329 (if containing-decl-open
9330 (c-add-class-syntax 'inclass
9331 containing-decl-open
9332 containing-decl-start
9333 containing-decl-kwd
9334 paren-state)))
9336 ;; CASE 5B.4: Nether region after a C++ or Java func
9337 ;; decl, which could include a `throws' declaration.
9339 (c-beginning-of-statement-1 lim)
9340 (c-add-syntax 'func-decl-cont (c-point 'boi))
9343 ;; CASE 5C: inheritance line. could be first inheritance
9344 ;; line, or continuation of a multiple inheritance
9345 ((or (and (c-major-mode-is 'c++-mode)
9346 (progn
9347 (when (eq char-after-ip ?,)
9348 (skip-chars-forward " \t")
9349 (forward-char))
9350 (looking-at c-opt-postfix-decl-spec-key)))
9351 (and (or (eq char-before-ip ?:)
9352 ;; watch out for scope operator
9353 (save-excursion
9354 (and (eq char-after-ip ?:)
9355 (c-safe (forward-char 1) t)
9356 (not (eq (char-after) ?:))
9358 (save-excursion
9359 (c-backward-syntactic-ws lim)
9360 (if (eq char-before-ip ?:)
9361 (progn
9362 (forward-char -1)
9363 (c-backward-syntactic-ws lim)))
9364 (back-to-indentation)
9365 (looking-at c-class-key)))
9366 ;; for Java
9367 (and (c-major-mode-is 'java-mode)
9368 (let ((fence (save-excursion
9369 (c-beginning-of-statement-1 lim)
9370 (point)))
9371 cont done)
9372 (save-excursion
9373 (while (not done)
9374 (cond ((looking-at c-opt-postfix-decl-spec-key)
9375 (setq injava-inher (cons cont (point))
9376 done t))
9377 ((or (not (c-safe (c-forward-sexp -1) t))
9378 (<= (point) fence))
9379 (setq done t))
9381 (setq cont t)))
9382 injava-inher)
9383 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9384 (point)))
9386 (cond
9388 ;; CASE 5C.1: non-hanging colon on an inher intro
9389 ((eq char-after-ip ?:)
9390 (c-beginning-of-statement-1 lim)
9391 (c-add-syntax 'inher-intro (c-point 'boi))
9392 ;; don't add inclass symbol since relative point already
9393 ;; contains any class offset
9396 ;; CASE 5C.2: hanging colon on an inher intro
9397 ((eq char-before-ip ?:)
9398 (c-beginning-of-statement-1 lim)
9399 (c-add-syntax 'inher-intro (c-point 'boi))
9400 (if containing-decl-open
9401 (c-add-class-syntax 'inclass
9402 containing-decl-open
9403 containing-decl-start
9404 containing-decl-kwd
9405 paren-state)))
9407 ;; CASE 5C.3: in a Java implements/extends
9408 (injava-inher
9409 (let ((where (cdr injava-inher))
9410 (cont (car injava-inher)))
9411 (goto-char where)
9412 (cond ((looking-at "throws\\>[^_]")
9413 (c-add-syntax 'func-decl-cont
9414 (progn (c-beginning-of-statement-1 lim)
9415 (c-point 'boi))))
9416 (cont (c-add-syntax 'inher-cont where))
9417 (t (c-add-syntax 'inher-intro
9418 (progn (goto-char (cdr injava-inher))
9419 (c-beginning-of-statement-1 lim)
9420 (point))))
9423 ;; CASE 5C.4: a continued inheritance line
9425 (c-beginning-of-inheritance-list lim)
9426 (c-add-syntax 'inher-cont (point))
9427 ;; don't add inclass symbol since relative point already
9428 ;; contains any class offset
9431 ;; CASE 5D: this could be a top-level initialization, a
9432 ;; member init list continuation, or a template argument
9433 ;; list continuation.
9434 ((save-excursion
9435 ;; Note: We use the fact that lim is always after any
9436 ;; preceding brace sexp.
9437 (if c-recognize-<>-arglists
9438 (while (and
9439 (progn
9440 (c-syntactic-skip-backward "^;,=<>" lim t)
9441 (> (point) lim))
9443 (when c-overloadable-operators-regexp
9444 (when (setq placeholder (c-after-special-operator-id lim))
9445 (goto-char placeholder)
9447 (cond
9448 ((eq (char-before) ?>)
9449 (or (c-backward-<>-arglist nil lim)
9450 (backward-char))
9452 ((eq (char-before) ?<)
9453 (backward-char)
9454 (if (save-excursion
9455 (c-forward-<>-arglist nil))
9456 (progn (forward-char)
9457 nil)
9459 (t nil)))))
9460 ;; NB: No c-after-special-operator-id stuff in this
9461 ;; clause - we assume only C++ needs it.
9462 (c-syntactic-skip-backward "^;,=" lim t))
9463 (memq (char-before) '(?, ?= ?<)))
9464 (cond
9466 ;; CASE 5D.3: perhaps a template list continuation?
9467 ((and (c-major-mode-is 'c++-mode)
9468 (save-excursion
9469 (save-restriction
9470 (c-with-syntax-table c++-template-syntax-table
9471 (goto-char indent-point)
9472 (setq placeholder (c-up-list-backward))
9473 (and placeholder
9474 (eq (char-after placeholder) ?<))))))
9475 (c-with-syntax-table c++-template-syntax-table
9476 (goto-char placeholder)
9477 (c-beginning-of-statement-1 lim t)
9478 (if (save-excursion
9479 (c-backward-syntactic-ws lim)
9480 (eq (char-before) ?<))
9481 ;; In a nested template arglist.
9482 (progn
9483 (goto-char placeholder)
9484 (c-syntactic-skip-backward "^,;" lim t)
9485 (c-forward-syntactic-ws))
9486 (back-to-indentation)))
9487 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9488 ;; template aware.
9489 (c-add-syntax 'template-args-cont (point) placeholder))
9491 ;; CASE 5D.4: perhaps a multiple inheritance line?
9492 ((and (c-major-mode-is 'c++-mode)
9493 (save-excursion
9494 (c-beginning-of-statement-1 lim)
9495 (setq placeholder (point))
9496 (if (looking-at "static\\>[^_]")
9497 (c-forward-token-2 1 nil indent-point))
9498 (and (looking-at c-class-key)
9499 (zerop (c-forward-token-2 2 nil indent-point))
9500 (if (eq (char-after) ?<)
9501 (c-with-syntax-table c++-template-syntax-table
9502 (zerop (c-forward-token-2 1 t indent-point)))
9504 (eq (char-after) ?:))))
9505 (goto-char placeholder)
9506 (c-add-syntax 'inher-cont (c-point 'boi)))
9508 ;; CASE 5D.5: Continuation of the "expression part" of a
9509 ;; top level construct. Or, perhaps, an unrecognised construct.
9511 (while (and (setq placeholder (point))
9512 (eq (car (c-beginning-of-decl-1 containing-sexp))
9513 'same)
9514 (save-excursion
9515 (c-backward-syntactic-ws)
9516 (eq (char-before) ?}))
9517 (< (point) placeholder)))
9518 (c-add-stmt-syntax
9519 (cond
9520 ((eq (point) placeholder) 'statement) ; unrecognised construct
9521 ;; A preceding comma at the top level means that a
9522 ;; new variable declaration starts here. Use
9523 ;; topmost-intro-cont for it, for consistency with
9524 ;; the first variable declaration. C.f. case 5N.
9525 ((eq char-before-ip ?,) 'topmost-intro-cont)
9526 (t 'statement-cont))
9527 nil nil containing-sexp paren-state))
9530 ;; CASE 5F: Close of a non-class declaration level block.
9531 ((and (eq char-after-ip ?})
9532 (c-keyword-member containing-decl-kwd
9533 'c-other-block-decl-kwds))
9534 ;; This is inconsistent: Should use `containing-decl-open'
9535 ;; here if it's at boi, like in case 5J.
9536 (goto-char containing-decl-start)
9537 (c-add-stmt-syntax
9538 (if (string-equal (symbol-name containing-decl-kwd) "extern")
9539 ;; Special case for compatibility with the
9540 ;; extern-lang syntactic symbols.
9541 'extern-lang-close
9542 (intern (concat (symbol-name containing-decl-kwd)
9543 "-close")))
9544 nil t
9545 (c-most-enclosing-brace paren-state (point))
9546 paren-state))
9548 ;; CASE 5G: we are looking at the brace which closes the
9549 ;; enclosing nested class decl
9550 ((and containing-sexp
9551 (eq char-after-ip ?})
9552 (eq containing-decl-open containing-sexp))
9553 (c-add-class-syntax 'class-close
9554 containing-decl-open
9555 containing-decl-start
9556 containing-decl-kwd
9557 paren-state))
9559 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9560 ((and c-recognize-knr-p
9561 (not containing-sexp) ; can't be knr inside braces.
9562 (not (eq char-before-ip ?}))
9563 (save-excursion
9564 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
9565 (and placeholder
9566 ;; Do an extra check to avoid tripping up on
9567 ;; statements that occur in invalid contexts
9568 ;; (e.g. in macro bodies where we don't really
9569 ;; know the context of what we're looking at).
9570 (not (and c-opt-block-stmt-key
9571 (looking-at c-opt-block-stmt-key)))))
9572 (< placeholder indent-point))
9573 (goto-char placeholder)
9574 (c-add-syntax 'knr-argdecl (point)))
9576 ;; CASE 5I: ObjC method definition.
9577 ((and c-opt-method-key
9578 (looking-at c-opt-method-key))
9579 (c-beginning-of-statement-1 nil t)
9580 (if (= (point) indent-point)
9581 ;; Handle the case when it's the first (non-comment)
9582 ;; thing in the buffer. Can't look for a 'same return
9583 ;; value from cbos1 since ObjC directives currently
9584 ;; aren't recognized fully, so that we get 'same
9585 ;; instead of 'previous if it moved over a preceding
9586 ;; directive.
9587 (goto-char (point-min)))
9588 (c-add-syntax 'objc-method-intro (c-point 'boi)))
9590 ;; CASE 5P: AWK pattern or function or continuation
9591 ;; thereof.
9592 ((c-major-mode-is 'awk-mode)
9593 (setq placeholder (point))
9594 (c-add-stmt-syntax
9595 (if (and (eq (c-beginning-of-statement-1) 'same)
9596 (/= (point) placeholder))
9597 'topmost-intro-cont
9598 'topmost-intro)
9599 nil nil
9600 containing-sexp paren-state))
9602 ;; CASE 5N: At a variable declaration that follows a class
9603 ;; definition or some other block declaration that doesn't
9604 ;; end at the closing '}'. C.f. case 5D.5.
9605 ((progn
9606 (c-backward-syntactic-ws lim)
9607 (and (eq (char-before) ?})
9608 (save-excursion
9609 (let ((start (point)))
9610 (if (and c-state-cache
9611 (consp (car c-state-cache))
9612 (eq (cdar c-state-cache) (point)))
9613 ;; Speed up the backward search a bit.
9614 (goto-char (caar c-state-cache)))
9615 (c-beginning-of-decl-1 containing-sexp)
9616 (setq placeholder (point))
9617 (if (= start (point))
9618 ;; The '}' is unbalanced.
9620 (c-end-of-decl-1)
9621 (>= (point) indent-point))))))
9622 (goto-char placeholder)
9623 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9624 containing-sexp paren-state))
9626 ;; NOTE: The point is at the end of the previous token here.
9628 ;; CASE 5J: we are at the topmost level, make
9629 ;; sure we skip back past any access specifiers
9630 ((and
9631 ;; A macro continuation line is never at top level.
9632 (not (and macro-start
9633 (> indent-point macro-start)))
9634 (save-excursion
9635 (setq placeholder (point))
9636 (or (memq char-before-ip '(?\; ?{ ?} nil))
9637 (c-at-vsemi-p before-ws-ip)
9638 (when (and (eq char-before-ip ?:)
9639 (eq (c-beginning-of-statement-1 lim)
9640 'label))
9641 (c-backward-syntactic-ws lim)
9642 (setq placeholder (point)))
9643 (and (c-major-mode-is 'objc-mode)
9644 (catch 'not-in-directive
9645 (c-beginning-of-statement-1 lim)
9646 (setq placeholder (point))
9647 (while (and (c-forward-objc-directive)
9648 (< (point) indent-point))
9649 (c-forward-syntactic-ws)
9650 (if (>= (point) indent-point)
9651 (throw 'not-in-directive t))
9652 (setq placeholder (point)))
9653 nil)))))
9654 ;; For historic reasons we anchor at bol of the last
9655 ;; line of the previous declaration. That's clearly
9656 ;; highly bogus and useless, and it makes our lives hard
9657 ;; to remain compatible. :P
9658 (goto-char placeholder)
9659 (c-add-syntax 'topmost-intro (c-point 'bol))
9660 (if containing-decl-open
9661 (if (c-keyword-member containing-decl-kwd
9662 'c-other-block-decl-kwds)
9663 (progn
9664 (goto-char (c-brace-anchor-point containing-decl-open))
9665 (c-add-stmt-syntax
9666 (if (string-equal (symbol-name containing-decl-kwd)
9667 "extern")
9668 ;; Special case for compatibility with the
9669 ;; extern-lang syntactic symbols.
9670 'inextern-lang
9671 (intern (concat "in"
9672 (symbol-name containing-decl-kwd))))
9673 nil t
9674 (c-most-enclosing-brace paren-state (point))
9675 paren-state))
9676 (c-add-class-syntax 'inclass
9677 containing-decl-open
9678 containing-decl-start
9679 containing-decl-kwd
9680 paren-state)))
9681 (when (and c-syntactic-indentation-in-macros
9682 macro-start
9683 (/= macro-start (c-point 'boi indent-point)))
9684 (c-add-syntax 'cpp-define-intro)
9685 (setq macro-start nil)))
9687 ;; CASE 5K: we are at an ObjC method definition
9688 ;; continuation line.
9689 ((and c-opt-method-key
9690 (save-excursion
9691 (c-beginning-of-statement-1 lim)
9692 (beginning-of-line)
9693 (when (looking-at c-opt-method-key)
9694 (setq placeholder (point)))))
9695 (c-add-syntax 'objc-method-args-cont placeholder))
9697 ;; CASE 5L: we are at the first argument of a template
9698 ;; arglist that begins on the previous line.
9699 ((and c-recognize-<>-arglists
9700 (eq (char-before) ?<)
9701 (not (and c-overloadable-operators-regexp
9702 (c-after-special-operator-id lim))))
9703 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9704 (c-add-syntax 'template-args-cont (c-point 'boi)))
9706 ;; CASE 5Q: we are at a statement within a macro.
9707 (macro-start
9708 (c-beginning-of-statement-1 containing-sexp)
9709 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
9711 ;;CASE 5N: We are at a tompmost continuation line and the only
9712 ;;preceding items are annotations.
9713 ((and (c-major-mode-is 'java-mode)
9714 (setq placeholder (point))
9715 (c-beginning-of-statement-1)
9716 (progn
9717 (while (and (c-forward-annotation))
9718 (c-forward-syntactic-ws))
9720 (prog1
9721 (>= (point) placeholder)
9722 (goto-char placeholder)))
9723 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
9725 ;; CASE 5M: we are at a topmost continuation line
9727 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9728 (when (c-major-mode-is 'objc-mode)
9729 (setq placeholder (point))
9730 (while (and (c-forward-objc-directive)
9731 (< (point) indent-point))
9732 (c-forward-syntactic-ws)
9733 (setq placeholder (point)))
9734 (goto-char placeholder))
9735 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9739 ;; (CASE 6 has been removed.)
9741 ;; CASE 7: line is an expression, not a statement. Most
9742 ;; likely we are either in a function prototype or a function
9743 ;; call argument list
9744 ((not (or (and c-special-brace-lists
9745 (save-excursion
9746 (goto-char containing-sexp)
9747 (c-looking-at-special-brace-list)))
9748 (eq (char-after containing-sexp) ?{)))
9749 (cond
9751 ;; CASE 7A: we are looking at the arglist closing paren.
9752 ;; C.f. case 7F.
9753 ((memq char-after-ip '(?\) ?\]))
9754 (goto-char containing-sexp)
9755 (setq placeholder (c-point 'boi))
9756 (if (and (c-safe (backward-up-list 1) t)
9757 (>= (point) placeholder))
9758 (progn
9759 (forward-char)
9760 (skip-chars-forward " \t"))
9761 (goto-char placeholder))
9762 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
9763 (c-most-enclosing-brace paren-state (point))
9764 paren-state))
9766 ;; CASE 7B: Looking at the opening brace of an
9767 ;; in-expression block or brace list. C.f. cases 4, 16A
9768 ;; and 17E.
9769 ((and (eq char-after-ip ?{)
9770 (progn
9771 (setq placeholder (c-inside-bracelist-p (point)
9772 paren-state))
9773 (if placeholder
9774 (setq tmpsymbol '(brace-list-open . inexpr-class))
9775 (setq tmpsymbol '(block-open . inexpr-statement)
9776 placeholder
9777 (cdr-safe (c-looking-at-inexpr-block
9778 (c-safe-position containing-sexp
9779 paren-state)
9780 containing-sexp)))
9781 ;; placeholder is nil if it's a block directly in
9782 ;; a function arglist. That makes us skip out of
9783 ;; this case.
9785 (goto-char placeholder)
9786 (back-to-indentation)
9787 (c-add-stmt-syntax (car tmpsymbol) nil t
9788 (c-most-enclosing-brace paren-state (point))
9789 paren-state)
9790 (if (/= (point) placeholder)
9791 (c-add-syntax (cdr tmpsymbol))))
9793 ;; CASE 7C: we are looking at the first argument in an empty
9794 ;; argument list. Use arglist-close if we're actually
9795 ;; looking at a close paren or bracket.
9796 ((memq char-before-ip '(?\( ?\[))
9797 (goto-char containing-sexp)
9798 (setq placeholder (c-point 'boi))
9799 (if (and (c-safe (backward-up-list 1) t)
9800 (>= (point) placeholder))
9801 (progn
9802 (forward-char)
9803 (skip-chars-forward " \t"))
9804 (goto-char placeholder))
9805 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
9806 (c-most-enclosing-brace paren-state (point))
9807 paren-state))
9809 ;; CASE 7D: we are inside a conditional test clause. treat
9810 ;; these things as statements
9811 ((progn
9812 (goto-char containing-sexp)
9813 (and (c-safe (c-forward-sexp -1) t)
9814 (looking-at "\\<for\\>[^_]")))
9815 (goto-char (1+ containing-sexp))
9816 (c-forward-syntactic-ws indent-point)
9817 (if (eq char-before-ip ?\;)
9818 (c-add-syntax 'statement (point))
9819 (c-add-syntax 'statement-cont (point))
9822 ;; CASE 7E: maybe a continued ObjC method call. This is the
9823 ;; case when we are inside a [] bracketed exp, and what
9824 ;; precede the opening bracket is not an identifier.
9825 ((and c-opt-method-key
9826 (eq (char-after containing-sexp) ?\[)
9827 (progn
9828 (goto-char (1- containing-sexp))
9829 (c-backward-syntactic-ws (c-point 'bod))
9830 (if (not (looking-at c-symbol-key))
9831 (c-add-syntax 'objc-method-call-cont containing-sexp))
9834 ;; CASE 7F: we are looking at an arglist continuation line,
9835 ;; but the preceding argument is on the same line as the
9836 ;; opening paren. This case includes multi-line
9837 ;; mathematical paren groupings, but we could be on a
9838 ;; for-list continuation line. C.f. case 7A.
9839 ((progn
9840 (goto-char (1+ containing-sexp))
9841 (< (save-excursion
9842 (c-forward-syntactic-ws)
9843 (point))
9844 (c-point 'bonl)))
9845 (goto-char containing-sexp) ; paren opening the arglist
9846 (setq placeholder (c-point 'boi))
9847 (if (and (c-safe (backward-up-list 1) t)
9848 (>= (point) placeholder))
9849 (progn
9850 (forward-char)
9851 (skip-chars-forward " \t"))
9852 (goto-char placeholder))
9853 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
9854 (c-most-enclosing-brace c-state-cache (point))
9855 paren-state))
9857 ;; CASE 7G: we are looking at just a normal arglist
9858 ;; continuation line
9859 (t (c-forward-syntactic-ws indent-point)
9860 (c-add-syntax 'arglist-cont (c-point 'boi)))
9863 ;; CASE 8: func-local multi-inheritance line
9864 ((and (c-major-mode-is 'c++-mode)
9865 (save-excursion
9866 (goto-char indent-point)
9867 (skip-chars-forward " \t")
9868 (looking-at c-opt-postfix-decl-spec-key)))
9869 (goto-char indent-point)
9870 (skip-chars-forward " \t")
9871 (cond
9873 ;; CASE 8A: non-hanging colon on an inher intro
9874 ((eq char-after-ip ?:)
9875 (c-backward-syntactic-ws lim)
9876 (c-add-syntax 'inher-intro (c-point 'boi)))
9878 ;; CASE 8B: hanging colon on an inher intro
9879 ((eq char-before-ip ?:)
9880 (c-add-syntax 'inher-intro (c-point 'boi)))
9882 ;; CASE 8C: a continued inheritance line
9884 (c-beginning-of-inheritance-list lim)
9885 (c-add-syntax 'inher-cont (point))
9888 ;; CASE 9: we are inside a brace-list
9889 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
9890 (setq special-brace-list
9891 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
9892 (save-excursion
9893 (goto-char containing-sexp)
9894 (c-looking-at-special-brace-list)))
9895 (c-inside-bracelist-p containing-sexp paren-state))))
9896 (cond
9898 ;; CASE 9A: In the middle of a special brace list opener.
9899 ((and (consp special-brace-list)
9900 (save-excursion
9901 (goto-char containing-sexp)
9902 (eq (char-after) ?\())
9903 (eq char-after-ip (car (cdr special-brace-list))))
9904 (goto-char (car (car special-brace-list)))
9905 (skip-chars-backward " \t")
9906 (if (and (bolp)
9907 (assoc 'statement-cont
9908 (setq placeholder (c-guess-basic-syntax))))
9909 (setq c-syntactic-context placeholder)
9910 (c-beginning-of-statement-1
9911 (c-safe-position (1- containing-sexp) paren-state))
9912 (c-forward-token-2 0)
9913 (while (looking-at c-specifier-key)
9914 (goto-char (match-end 1))
9915 (c-forward-syntactic-ws))
9916 (c-add-syntax 'brace-list-open (c-point 'boi))))
9918 ;; CASE 9B: brace-list-close brace
9919 ((if (consp special-brace-list)
9920 ;; Check special brace list closer.
9921 (progn
9922 (goto-char (car (car special-brace-list)))
9923 (save-excursion
9924 (goto-char indent-point)
9925 (back-to-indentation)
9927 ;; We were between the special close char and the `)'.
9928 (and (eq (char-after) ?\))
9929 (eq (1+ (point)) (cdr (car special-brace-list))))
9930 ;; We were before the special close char.
9931 (and (eq (char-after) (cdr (cdr special-brace-list)))
9932 (zerop (c-forward-token-2))
9933 (eq (1+ (point)) (cdr (car special-brace-list)))))))
9934 ;; Normal brace list check.
9935 (and (eq char-after-ip ?})
9936 (c-safe (goto-char (c-up-list-backward (point))) t)
9937 (= (point) containing-sexp)))
9938 (if (eq (point) (c-point 'boi))
9939 (c-add-syntax 'brace-list-close (point))
9940 (setq lim (c-most-enclosing-brace c-state-cache (point)))
9941 (c-beginning-of-statement-1 lim)
9942 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
9945 ;; Prepare for the rest of the cases below by going to the
9946 ;; token following the opening brace
9947 (if (consp special-brace-list)
9948 (progn
9949 (goto-char (car (car special-brace-list)))
9950 (c-forward-token-2 1 nil indent-point))
9951 (goto-char containing-sexp))
9952 (forward-char)
9953 (let ((start (point)))
9954 (c-forward-syntactic-ws indent-point)
9955 (goto-char (max start (c-point 'bol))))
9956 (c-skip-ws-forward indent-point)
9957 (cond
9959 ;; CASE 9C: we're looking at the first line in a brace-list
9960 ((= (point) indent-point)
9961 (if (consp special-brace-list)
9962 (goto-char (car (car special-brace-list)))
9963 (goto-char containing-sexp))
9964 (if (eq (point) (c-point 'boi))
9965 (c-add-syntax 'brace-list-intro (point))
9966 (setq lim (c-most-enclosing-brace c-state-cache (point)))
9967 (c-beginning-of-statement-1 lim)
9968 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
9970 ;; CASE 9D: this is just a later brace-list-entry or
9971 ;; brace-entry-open
9972 (t (if (or (eq char-after-ip ?{)
9973 (and c-special-brace-lists
9974 (save-excursion
9975 (goto-char indent-point)
9976 (c-forward-syntactic-ws (c-point 'eol))
9977 (c-looking-at-special-brace-list (point)))))
9978 (c-add-syntax 'brace-entry-open (point))
9979 (c-add-syntax 'brace-list-entry (point))
9981 ))))
9983 ;; CASE 10: A continued statement or top level construct.
9984 ((and (not (memq char-before-ip '(?\; ?:)))
9985 (not (c-at-vsemi-p before-ws-ip))
9986 (or (not (eq char-before-ip ?}))
9987 (c-looking-at-inexpr-block-backward c-state-cache))
9988 (> (point)
9989 (save-excursion
9990 (c-beginning-of-statement-1 containing-sexp)
9991 (setq placeholder (point))))
9992 (/= placeholder containing-sexp))
9993 ;; This is shared with case 18.
9994 (c-guess-continued-construct indent-point
9995 char-after-ip
9996 placeholder
9997 containing-sexp
9998 paren-state))
10000 ;; CASE 16: block close brace, possibly closing the defun or
10001 ;; the class
10002 ((eq char-after-ip ?})
10003 ;; From here on we have the next containing sexp in lim.
10004 (setq lim (c-most-enclosing-brace paren-state))
10005 (goto-char containing-sexp)
10006 (cond
10008 ;; CASE 16E: Closing a statement block? This catches
10009 ;; cases where it's preceded by a statement keyword,
10010 ;; which works even when used in an "invalid" context,
10011 ;; e.g. a macro argument.
10012 ((c-after-conditional)
10013 (c-backward-to-block-anchor lim)
10014 (c-add-stmt-syntax 'block-close nil t lim paren-state))
10016 ;; CASE 16A: closing a lambda defun or an in-expression
10017 ;; block? C.f. cases 4, 7B and 17E.
10018 ((setq placeholder (c-looking-at-inexpr-block
10019 (c-safe-position containing-sexp paren-state)
10020 nil))
10021 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10022 'inline-close
10023 'block-close))
10024 (goto-char containing-sexp)
10025 (back-to-indentation)
10026 (if (= containing-sexp (point))
10027 (c-add-syntax tmpsymbol (point))
10028 (goto-char (cdr placeholder))
10029 (back-to-indentation)
10030 (c-add-stmt-syntax tmpsymbol nil t
10031 (c-most-enclosing-brace paren-state (point))
10032 paren-state)
10033 (if (/= (point) (cdr placeholder))
10034 (c-add-syntax (car placeholder)))))
10036 ;; CASE 16B: does this close an inline or a function in
10037 ;; a non-class declaration level block?
10038 ((save-excursion
10039 (and lim
10040 (progn
10041 (goto-char lim)
10042 (c-looking-at-decl-block
10043 (c-most-enclosing-brace paren-state lim)
10044 nil))
10045 (setq placeholder (point))))
10046 (c-backward-to-decl-anchor lim)
10047 (back-to-indentation)
10048 (if (save-excursion
10049 (goto-char placeholder)
10050 (looking-at c-other-decl-block-key))
10051 (c-add-syntax 'defun-close (point))
10052 (c-add-syntax 'inline-close (point))))
10054 ;; CASE 16F: Can be a defun-close of a function declared
10055 ;; in a statement block, e.g. in Pike or when using gcc
10056 ;; extensions, but watch out for macros followed by
10057 ;; blocks. Let it through to be handled below.
10058 ;; C.f. cases B.3 and 17G.
10059 ((save-excursion
10060 (and (not (c-at-statement-start-p))
10061 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10062 (setq placeholder (point))
10063 (let ((c-recognize-typeless-decls nil))
10064 ;; Turn off recognition of constructs that
10065 ;; lacks a type in this case, since that's more
10066 ;; likely to be a macro followed by a block.
10067 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10068 (back-to-indentation)
10069 (if (/= (point) containing-sexp)
10070 (goto-char placeholder))
10071 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
10073 ;; CASE 16C: If there is an enclosing brace then this is
10074 ;; a block close since defun closes inside declaration
10075 ;; level blocks have been handled above.
10076 (lim
10077 ;; If the block is preceded by a case/switch label on
10078 ;; the same line, we anchor at the first preceding label
10079 ;; at boi. The default handling in c-add-stmt-syntax
10080 ;; really fixes it better, but we do like this to keep
10081 ;; the indentation compatible with version 5.28 and
10082 ;; earlier. C.f. case 17H.
10083 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10084 (eq (c-beginning-of-statement-1 lim) 'label)))
10085 (goto-char placeholder)
10086 (if (looking-at c-label-kwds-regexp)
10087 (c-add-syntax 'block-close (point))
10088 (goto-char containing-sexp)
10089 ;; c-backward-to-block-anchor not necessary here; those
10090 ;; situations are handled in case 16E above.
10091 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
10093 ;; CASE 16D: Only top level defun close left.
10095 (goto-char containing-sexp)
10096 (c-backward-to-decl-anchor lim)
10097 (c-add-stmt-syntax 'defun-close nil nil
10098 (c-most-enclosing-brace paren-state)
10099 paren-state))
10102 ;; CASE 19: line is an expression, not a statement, and is directly
10103 ;; contained by a template delimiter. Most likely, we are in a
10104 ;; template arglist within a statement. This case is based on CASE
10105 ;; 7. At some point in the future, we may wish to create more
10106 ;; syntactic symbols such as `template-intro',
10107 ;; `template-cont-nonempty', etc., and distinguish between them as we
10108 ;; do for `arglist-intro' etc. (2009-12-07).
10109 ((and c-recognize-<>-arglists
10110 (setq containing-< (c-up-list-backward indent-point containing-sexp))
10111 (eq (char-after containing-<) ?\<))
10112 (setq placeholder (c-point 'boi containing-<))
10113 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
10114 ; '<') before indent-point.
10115 (if (>= (point) placeholder)
10116 (progn
10117 (forward-char)
10118 (skip-chars-forward " \t"))
10119 (goto-char placeholder))
10120 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
10121 (c-most-enclosing-brace c-state-cache (point))
10122 paren-state))
10124 ;; CASE 17: Statement or defun catchall.
10126 (goto-char indent-point)
10127 ;; Back up statements until we find one that starts at boi.
10128 (while (let* ((prev-point (point))
10129 (last-step-type (c-beginning-of-statement-1
10130 containing-sexp)))
10131 (if (= (point) prev-point)
10132 (progn
10133 (setq step-type (or step-type last-step-type))
10134 nil)
10135 (setq step-type last-step-type)
10136 (/= (point) (c-point 'boi)))))
10137 (cond
10139 ;; CASE 17B: continued statement
10140 ((and (eq step-type 'same)
10141 (/= (point) indent-point))
10142 (c-add-stmt-syntax 'statement-cont nil nil
10143 containing-sexp paren-state))
10145 ;; CASE 17A: After a case/default label?
10146 ((progn
10147 (while (and (eq step-type 'label)
10148 (not (looking-at c-label-kwds-regexp)))
10149 (setq step-type
10150 (c-beginning-of-statement-1 containing-sexp)))
10151 (eq step-type 'label))
10152 (c-add-stmt-syntax (if (eq char-after-ip ?{)
10153 'statement-case-open
10154 'statement-case-intro)
10155 nil t containing-sexp paren-state))
10157 ;; CASE 17D: any old statement
10158 ((progn
10159 (while (eq step-type 'label)
10160 (setq step-type
10161 (c-beginning-of-statement-1 containing-sexp)))
10162 (eq step-type 'previous))
10163 (c-add-stmt-syntax 'statement nil t
10164 containing-sexp paren-state)
10165 (if (eq char-after-ip ?{)
10166 (c-add-syntax 'block-open)))
10168 ;; CASE 17I: Inside a substatement block.
10169 ((progn
10170 ;; The following tests are all based on containing-sexp.
10171 (goto-char containing-sexp)
10172 ;; From here on we have the next containing sexp in lim.
10173 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
10174 (c-after-conditional))
10175 (c-backward-to-block-anchor lim)
10176 (c-add-stmt-syntax 'statement-block-intro nil t
10177 lim paren-state)
10178 (if (eq char-after-ip ?{)
10179 (c-add-syntax 'block-open)))
10181 ;; CASE 17E: first statement in an in-expression block.
10182 ;; C.f. cases 4, 7B and 16A.
10183 ((setq placeholder (c-looking-at-inexpr-block
10184 (c-safe-position containing-sexp paren-state)
10185 nil))
10186 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10187 'defun-block-intro
10188 'statement-block-intro))
10189 (back-to-indentation)
10190 (if (= containing-sexp (point))
10191 (c-add-syntax tmpsymbol (point))
10192 (goto-char (cdr placeholder))
10193 (back-to-indentation)
10194 (c-add-stmt-syntax tmpsymbol nil t
10195 (c-most-enclosing-brace c-state-cache (point))
10196 paren-state)
10197 (if (/= (point) (cdr placeholder))
10198 (c-add-syntax (car placeholder))))
10199 (if (eq char-after-ip ?{)
10200 (c-add-syntax 'block-open)))
10202 ;; CASE 17F: first statement in an inline, or first
10203 ;; statement in a top-level defun. we can tell this is it
10204 ;; if there are no enclosing braces that haven't been
10205 ;; narrowed out by a class (i.e. don't use bod here).
10206 ((save-excursion
10207 (or (not (setq placeholder (c-most-enclosing-brace
10208 paren-state)))
10209 (and (progn
10210 (goto-char placeholder)
10211 (eq (char-after) ?{))
10212 (c-looking-at-decl-block (c-most-enclosing-brace
10213 paren-state (point))
10214 nil))))
10215 (c-backward-to-decl-anchor lim)
10216 (back-to-indentation)
10217 (c-add-syntax 'defun-block-intro (point)))
10219 ;; CASE 17G: First statement in a function declared inside
10220 ;; a normal block. This can occur in Pike and with
10221 ;; e.g. the gcc extensions, but watch out for macros
10222 ;; followed by blocks. C.f. cases B.3 and 16F.
10223 ((save-excursion
10224 (and (not (c-at-statement-start-p))
10225 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10226 (setq placeholder (point))
10227 (let ((c-recognize-typeless-decls nil))
10228 ;; Turn off recognition of constructs that lacks
10229 ;; a type in this case, since that's more likely
10230 ;; to be a macro followed by a block.
10231 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10232 (back-to-indentation)
10233 (if (/= (point) containing-sexp)
10234 (goto-char placeholder))
10235 (c-add-stmt-syntax 'defun-block-intro nil t
10236 lim paren-state))
10238 ;; CASE 17H: First statement in a block.
10240 ;; If the block is preceded by a case/switch label on the
10241 ;; same line, we anchor at the first preceding label at
10242 ;; boi. The default handling in c-add-stmt-syntax is
10243 ;; really fixes it better, but we do like this to keep the
10244 ;; indentation compatible with version 5.28 and earlier.
10245 ;; C.f. case 16C.
10246 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10247 (eq (c-beginning-of-statement-1 lim) 'label)))
10248 (goto-char placeholder)
10249 (if (looking-at c-label-kwds-regexp)
10250 (c-add-syntax 'statement-block-intro (point))
10251 (goto-char containing-sexp)
10252 ;; c-backward-to-block-anchor not necessary here; those
10253 ;; situations are handled in case 17I above.
10254 (c-add-stmt-syntax 'statement-block-intro nil t
10255 lim paren-state))
10256 (if (eq char-after-ip ?{)
10257 (c-add-syntax 'block-open)))
10261 ;; now we need to look at any modifiers
10262 (goto-char indent-point)
10263 (skip-chars-forward " \t")
10265 ;; are we looking at a comment only line?
10266 (when (and (looking-at c-comment-start-regexp)
10267 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
10268 (c-append-syntax 'comment-intro))
10270 ;; we might want to give additional offset to friends (in C++).
10271 (when (and c-opt-friend-key
10272 (looking-at c-opt-friend-key))
10273 (c-append-syntax 'friend))
10275 ;; Set syntactic-relpos.
10276 (let ((p c-syntactic-context))
10277 (while (and p
10278 (if (integerp (c-langelem-pos (car p)))
10279 (progn
10280 (setq syntactic-relpos (c-langelem-pos (car p)))
10281 nil)
10283 (setq p (cdr p))))
10285 ;; Start of or a continuation of a preprocessor directive?
10286 (if (and macro-start
10287 (eq macro-start (c-point 'boi))
10288 (not (and (c-major-mode-is 'pike-mode)
10289 (eq (char-after (1+ macro-start)) ?\"))))
10290 (c-append-syntax 'cpp-macro)
10291 (when (and c-syntactic-indentation-in-macros macro-start)
10292 (if in-macro-expr
10293 (when (or
10294 (< syntactic-relpos macro-start)
10295 (not (or
10296 (assq 'arglist-intro c-syntactic-context)
10297 (assq 'arglist-cont c-syntactic-context)
10298 (assq 'arglist-cont-nonempty c-syntactic-context)
10299 (assq 'arglist-close c-syntactic-context))))
10300 ;; If inside a cpp expression, i.e. anywhere in a
10301 ;; cpp directive except a #define body, we only let
10302 ;; through the syntactic analysis that is internal
10303 ;; in the expression. That means the arglist
10304 ;; elements, if they are anchored inside the cpp
10305 ;; expression.
10306 (setq c-syntactic-context nil)
10307 (c-add-syntax 'cpp-macro-cont macro-start))
10308 (when (and (eq macro-start syntactic-relpos)
10309 (not (assq 'cpp-define-intro c-syntactic-context))
10310 (save-excursion
10311 (goto-char macro-start)
10312 (or (not (c-forward-to-cpp-define-body))
10313 (<= (point) (c-point 'boi indent-point)))))
10314 ;; Inside a #define body and the syntactic analysis is
10315 ;; anchored on the start of the #define. In this case
10316 ;; we add cpp-define-intro to get the extra
10317 ;; indentation of the #define body.
10318 (c-add-syntax 'cpp-define-intro)))))
10320 ;; return the syntax
10321 c-syntactic-context)))
10324 ;; Indentation calculation.
10326 (defun c-evaluate-offset (offset langelem symbol)
10327 ;; offset can be a number, a function, a variable, a list, or one of
10328 ;; the symbols + or -
10330 ;; This function might do hidden buffer changes.
10331 (let ((res
10332 (cond
10333 ((numberp offset) offset)
10334 ((vectorp offset) offset)
10335 ((null offset) nil)
10337 ((eq offset '+) c-basic-offset)
10338 ((eq offset '-) (- c-basic-offset))
10339 ((eq offset '++) (* 2 c-basic-offset))
10340 ((eq offset '--) (* 2 (- c-basic-offset)))
10341 ((eq offset '*) (/ c-basic-offset 2))
10342 ((eq offset '/) (/ (- c-basic-offset) 2))
10344 ((functionp offset)
10345 (c-evaluate-offset
10346 (funcall offset
10347 (cons (c-langelem-sym langelem)
10348 (c-langelem-pos langelem)))
10349 langelem symbol))
10351 ((listp offset)
10352 (cond
10353 ((eq (car offset) 'quote)
10354 (c-benign-error "The offset %S for %s was mistakenly quoted"
10355 offset symbol)
10356 nil)
10358 ((memq (car offset) '(min max))
10359 (let (res val (method (car offset)))
10360 (setq offset (cdr offset))
10361 (while offset
10362 (setq val (c-evaluate-offset (car offset) langelem symbol))
10363 (cond
10364 ((not val))
10365 ((not res)
10366 (setq res val))
10367 ((integerp val)
10368 (if (vectorp res)
10369 (c-benign-error "\
10370 Error evaluating offset %S for %s: \
10371 Cannot combine absolute offset %S with relative %S in `%s' method"
10372 (car offset) symbol res val method)
10373 (setq res (funcall method res val))))
10375 (if (integerp res)
10376 (c-benign-error "\
10377 Error evaluating offset %S for %s: \
10378 Cannot combine relative offset %S with absolute %S in `%s' method"
10379 (car offset) symbol res val method)
10380 (setq res (vector (funcall method (aref res 0)
10381 (aref val 0)))))))
10382 (setq offset (cdr offset)))
10383 res))
10385 ((eq (car offset) 'add)
10386 (let (res val)
10387 (setq offset (cdr offset))
10388 (while offset
10389 (setq val (c-evaluate-offset (car offset) langelem symbol))
10390 (cond
10391 ((not val))
10392 ((not res)
10393 (setq res val))
10394 ((integerp val)
10395 (if (vectorp res)
10396 (setq res (vector (+ (aref res 0) val)))
10397 (setq res (+ res val))))
10399 (if (vectorp res)
10400 (c-benign-error "\
10401 Error evaluating offset %S for %s: \
10402 Cannot combine absolute offsets %S and %S in `add' method"
10403 (car offset) symbol res val)
10404 (setq res val)))) ; Override.
10405 (setq offset (cdr offset)))
10406 res))
10409 (let (res)
10410 (when (eq (car offset) 'first)
10411 (setq offset (cdr offset)))
10412 (while (and (not res) offset)
10413 (setq res (c-evaluate-offset (car offset) langelem symbol)
10414 offset (cdr offset)))
10415 res))))
10417 ((and (symbolp offset) (boundp offset))
10418 (symbol-value offset))
10421 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10422 nil))))
10424 (if (or (null res) (integerp res)
10425 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10427 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10428 offset symbol res)
10429 nil)))
10431 (defun c-calc-offset (langelem)
10432 ;; Get offset from LANGELEM which is a list beginning with the
10433 ;; syntactic symbol and followed by any analysis data it provides.
10434 ;; That data may be zero or more elements, but if at least one is
10435 ;; given then the first is the anchor position (or nil). The symbol
10436 ;; is matched against `c-offsets-alist' and the offset calculated
10437 ;; from that is returned.
10439 ;; This function might do hidden buffer changes.
10440 (let* ((symbol (c-langelem-sym langelem))
10441 (match (assq symbol c-offsets-alist))
10442 (offset (cdr-safe match)))
10443 (if match
10444 (setq offset (c-evaluate-offset offset langelem symbol))
10445 (if c-strict-syntax-p
10446 (c-benign-error "No offset found for syntactic symbol %s" symbol))
10447 (setq offset 0))
10448 (if (vectorp offset)
10449 offset
10450 (or (and (numberp offset) offset)
10451 (and (symbolp offset) (symbol-value offset))
10455 (defun c-get-offset (langelem)
10456 ;; This is a compatibility wrapper for `c-calc-offset' in case
10457 ;; someone is calling it directly. It takes an old style syntactic
10458 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10459 ;; new list form.
10461 ;; This function might do hidden buffer changes.
10462 (if (c-langelem-pos langelem)
10463 (c-calc-offset (list (c-langelem-sym langelem)
10464 (c-langelem-pos langelem)))
10465 (c-calc-offset langelem)))
10467 (defun c-get-syntactic-indentation (langelems)
10468 ;; Calculate the syntactic indentation from a syntactic description
10469 ;; as returned by `c-guess-syntax'.
10471 ;; Note that topmost-intro always has an anchor position at bol, for
10472 ;; historical reasons. It's often used together with other symbols
10473 ;; that has more sane positions. Since we always use the first
10474 ;; found anchor position, we rely on that these other symbols always
10475 ;; precede topmost-intro in the LANGELEMS list.
10477 ;; This function might do hidden buffer changes.
10478 (let ((indent 0) anchor)
10480 (while langelems
10481 (let* ((c-syntactic-element (car langelems))
10482 (res (c-calc-offset c-syntactic-element)))
10484 (if (vectorp res)
10485 ;; Got an absolute column that overrides any indentation
10486 ;; we've collected so far, but not the relative
10487 ;; indentation we might get for the nested structures
10488 ;; further down the langelems list.
10489 (setq indent (elt res 0)
10490 anchor (point-min)) ; A position at column 0.
10492 ;; Got a relative change of the current calculated
10493 ;; indentation.
10494 (setq indent (+ indent res))
10496 ;; Use the anchor position from the first syntactic
10497 ;; element with one.
10498 (unless anchor
10499 (setq anchor (c-langelem-pos (car langelems)))))
10501 (setq langelems (cdr langelems))))
10503 (if anchor
10504 (+ indent (save-excursion
10505 (goto-char anchor)
10506 (current-column)))
10507 indent)))
10510 (cc-provide 'cc-engine)
10512 ;;; cc-engine.el ends here