Handle system default font and changing font parameters.
[emacs.git] / lisp / progmodes / cc-engine.el
blob2c71b360748179a0aa3290dd6517dbc5373f0659
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Authors: 2001- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs
11 ;; 1987 Stewart Clamen
12 ;; 1985 Richard M. Stallman
13 ;; Maintainer: bug-cc-mode@gnu.org
14 ;; Created: 22-Apr-1997 (split from cc-mode.el)
15 ;; Version: See cc-mode.el
16 ;; Keywords: c languages oop
18 ;; This file is part of GNU Emacs.
20 ;; GNU Emacs is free software: you can redistribute it and/or modify
21 ;; it under the terms of the GNU General Public License as published by
22 ;; the Free Software Foundation, either version 3 of the License, or
23 ;; (at your option) any later version.
25 ;; GNU Emacs is distributed in the hope that it will be useful,
26 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;; GNU General Public License for more details.
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
33 ;;; Commentary:
35 ;; The functions which have docstring documentation can be considered
36 ;; part of an API which other packages can use in CC Mode buffers.
37 ;; Otoh, undocumented functions and functions with the documentation
38 ;; in comments are considered purely internal and can change semantics
39 ;; or even disappear in the future.
41 ;; (This policy applies to CC Mode as a whole, not just this file. It
42 ;; probably also applies to many other Emacs packages, but here it's
43 ;; clearly spelled out.)
45 ;; Hidden buffer changes
47 ;; Various functions in CC Mode use text properties for caching and
48 ;; syntactic markup purposes, and those of them that might modify such
49 ;; properties but still don't modify the buffer in a visible way are
50 ;; said to do "hidden buffer changes". They should be used within
51 ;; `c-save-buffer-state' or a similar function that saves and restores
52 ;; buffer modifiedness, disables buffer change hooks, etc.
54 ;; Interactive functions are assumed to not do hidden buffer changes,
55 ;; except in the specific parts of them that do real changes.
57 ;; Lineup functions are assumed to do hidden buffer changes. They
58 ;; must not do real changes, though.
60 ;; All other functions that do hidden buffer changes have that noted
61 ;; in their doc string or comment.
63 ;; The intention with this system is to avoid wrapping every leaf
64 ;; function that do hidden buffer changes inside
65 ;; `c-save-buffer-state'. It should be used as near the top of the
66 ;; interactive functions as possible.
68 ;; Functions called during font locking are allowed to do hidden
69 ;; buffer changes since the font-lock package run them in a context
70 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
71 ;; inspired by `save-buffer-state' in the font-lock package).
73 ;; Use of text properties
75 ;; CC Mode uses several text properties internally to mark up various
76 ;; positions, e.g. to improve speed and to eliminate glitches in
77 ;; interactive refontification.
79 ;; Note: This doc is for internal use only. Other packages should not
80 ;; assume that these text properties are used as described here.
82 ;; 'syntax-table
83 ;; Used to modify the syntax of some characters. It is used to
84 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
85 ;; to "hide" obtrusive characters in preprocessor lines.
87 ;; This property is used on single characters and is therefore
88 ;; always treated as front and rear nonsticky (or start and end open
89 ;; in XEmacs vocabulary). It's therefore installed on
90 ;; `text-property-default-nonsticky' if that variable exists (Emacs
91 ;; >= 21).
93 ;; 'c-is-sws and 'c-in-sws
94 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
95 ;; speed them up. See the comment blurb before `c-put-is-sws'
96 ;; below for further details.
98 ;; 'c-type
99 ;; This property is used on single characters to mark positions with
100 ;; special syntactic relevance of various sorts. Its primary use is
101 ;; to avoid glitches when multiline constructs are refontified
102 ;; interactively (on font lock decoration level 3). It's cleared in
103 ;; a region before it's fontified and is then put on relevant chars
104 ;; in that region as they are encountered during the fontification.
105 ;; The value specifies the kind of position:
107 ;; 'c-decl-arg-start
108 ;; Put on the last char of the token preceding each declaration
109 ;; inside a declaration style arglist (typically in a function
110 ;; prototype).
112 ;; 'c-decl-end
113 ;; Put on the last char of the token preceding a declaration.
114 ;; This is used in cases where declaration boundaries can't be
115 ;; recognized simply by looking for a token like ";" or "}".
116 ;; `c-type-decl-end-used' must be set if this is used (see also
117 ;; `c-find-decl-spots').
119 ;; 'c-<>-arg-sep
120 ;; Put on the commas that separate arguments in angle bracket
121 ;; arglists like C++ template arglists.
123 ;; 'c-decl-id-start and 'c-decl-type-start
124 ;; Put on the last char of the token preceding each declarator
125 ;; in the declarator list of a declaration. They are also used
126 ;; between the identifiers cases like enum declarations.
127 ;; 'c-decl-type-start is used when the declarators are types,
128 ;; 'c-decl-id-start otherwise.
130 ;; 'c-awk-NL-prop
131 ;; Used in AWK mode to mark the various kinds of newlines. See
132 ;; cc-awk.el.
134 ;;; Code:
136 (eval-when-compile
137 (let ((load-path
138 (if (and (boundp 'byte-compile-dest-file)
139 (stringp byte-compile-dest-file))
140 (cons (file-name-directory byte-compile-dest-file) load-path)
141 load-path)))
142 (load "cc-bytecomp" nil t)))
144 (cc-require 'cc-defs)
145 (cc-require-when-compile 'cc-langs)
146 (cc-require 'cc-vars)
148 ;; Silence the compiler.
149 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
152 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
154 (defmacro c-declare-lang-variables ()
155 `(progn
156 ,@(apply 'nconc
157 (mapcar (lambda (init)
158 `(,(if (elt init 2)
159 `(defvar ,(car init) nil ,(elt init 2))
160 `(defvar ,(car init) nil))
161 (make-variable-buffer-local ',(car init))))
162 (cdr c-lang-variable-inits)))))
163 (c-declare-lang-variables)
166 ;;; Internal state variables.
168 ;; Internal state of hungry delete key feature
169 (defvar c-hungry-delete-key nil)
170 (make-variable-buffer-local 'c-hungry-delete-key)
172 ;; The electric flag (toggled by `c-toggle-electric-state').
173 ;; If t, electric actions (like automatic reindentation, and (if
174 ;; c-auto-newline is also set) auto newlining) will happen when an electric
175 ;; key like `{' is pressed (or an electric keyword like `else').
176 (defvar c-electric-flag t)
177 (make-variable-buffer-local 'c-electric-flag)
179 ;; Internal state of auto newline feature.
180 (defvar c-auto-newline nil)
181 (make-variable-buffer-local 'c-auto-newline)
183 ;; Included in the mode line to indicate the active submodes.
184 ;; (defvar c-submode-indicators nil)
185 ;; (make-variable-buffer-local 'c-submode-indicators)
187 (defun c-calculate-state (arg prevstate)
188 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
189 ;; arg is nil or zero, toggle the state. If arg is negative, turn
190 ;; the state off, and if arg is positive, turn the state on
191 (if (or (not arg)
192 (zerop (setq arg (prefix-numeric-value arg))))
193 (not prevstate)
194 (> arg 0)))
196 ;; Dynamically bound cache for `c-in-literal'.
197 (defvar c-in-literal-cache t)
200 ;; Basic handling of preprocessor directives.
202 ;; This is a dynamically bound cache used together with
203 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
204 ;; works as long as point doesn't cross a macro boundary.
205 (defvar c-macro-start 'unknown)
207 (defsubst c-query-and-set-macro-start ()
208 (if (symbolp c-macro-start)
209 (setq c-macro-start (save-excursion
210 (c-save-buffer-state ()
211 (and (c-beginning-of-macro)
212 (point)))))
213 c-macro-start))
215 (defsubst c-query-macro-start ()
216 (if (symbolp c-macro-start)
217 (save-excursion
218 (c-save-buffer-state ()
219 (and (c-beginning-of-macro)
220 (point))))
221 c-macro-start))
223 (defun c-beginning-of-macro (&optional lim)
224 "Go to the beginning of a preprocessor directive.
225 Leave point at the beginning of the directive and return t if in one,
226 otherwise return nil and leave point unchanged.
228 Note that this function might do hidden buffer changes. See the
229 comment at the start of cc-engine.el for more info."
230 (when c-opt-cpp-prefix
231 (let ((here (point)))
232 (save-restriction
233 (if lim (narrow-to-region lim (point-max)))
234 (beginning-of-line)
235 (while (eq (char-before (1- (point))) ?\\)
236 (forward-line -1))
237 (back-to-indentation)
238 (if (and (<= (point) here)
239 (looking-at c-opt-cpp-start))
241 (goto-char here)
242 nil)))))
244 (defun c-end-of-macro ()
245 "Go to the end of a preprocessor directive.
246 More accurately, move the point to the end of the closest following
247 line that doesn't end with a line continuation backslash - no check is
248 done that the point is inside a cpp directive to begin with.
250 Note that this function might do hidden buffer changes. See the
251 comment at the start of cc-engine.el for more info."
252 (while (progn
253 (end-of-line)
254 (when (and (eq (char-before) ?\\)
255 (not (eobp)))
256 (forward-char)
257 t))))
259 (defun c-forward-over-cpp-define-id ()
260 ;; Assuming point is at the "#" that introduces a preprocessor
261 ;; directive, it's moved forward to the end of the identifier which is
262 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
263 ;; is returned in this case, in all other cases nil is returned and
264 ;; point isn't moved.
266 ;; This function might do hidden buffer changes.
267 (when (and c-opt-cpp-macro-define-id
268 (looking-at c-opt-cpp-macro-define-id))
269 (goto-char (match-end 0))))
271 (defun c-forward-to-cpp-define-body ()
272 ;; Assuming point is at the "#" that introduces a preprocessor
273 ;; directive, it's moved forward to the start of the definition body
274 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
275 ;; specifies). Non-nil is returned in this case, in all other cases
276 ;; nil is returned and point isn't moved.
278 ;; This function might do hidden buffer changes.
279 (when (and c-opt-cpp-macro-define-start
280 (looking-at c-opt-cpp-macro-define-start)
281 (not (= (match-end 0) (c-point 'eol))))
282 (goto-char (match-end 0))))
285 ;;; Basic utility functions.
287 (defun c-syntactic-content (from to paren-level)
288 ;; Return the given region as a string where all syntactic
289 ;; whitespace is removed or, where necessary, replaced with a single
290 ;; space. If PAREN-LEVEL is given then all parens in the region are
291 ;; collapsed to "()", "[]" etc.
293 ;; This function might do hidden buffer changes.
295 (save-excursion
296 (save-restriction
297 (narrow-to-region from to)
298 (goto-char from)
299 (let* ((parts (list nil)) (tail parts) pos in-paren)
301 (while (re-search-forward c-syntactic-ws-start to t)
302 (goto-char (setq pos (match-beginning 0)))
303 (c-forward-syntactic-ws)
304 (if (= (point) pos)
305 (forward-char)
307 (when paren-level
308 (save-excursion
309 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
310 pos (point))))
312 (if (and (> pos from)
313 (< (point) to)
314 (looking-at "\\w\\|\\s_")
315 (save-excursion
316 (goto-char (1- pos))
317 (looking-at "\\w\\|\\s_")))
318 (progn
319 (setcdr tail (list (buffer-substring-no-properties from pos)
320 " "))
321 (setq tail (cddr tail)))
322 (setcdr tail (list (buffer-substring-no-properties from pos)))
323 (setq tail (cdr tail)))
325 (when in-paren
326 (when (= (car (parse-partial-sexp pos to -1)) -1)
327 (setcdr tail (list (buffer-substring-no-properties
328 (1- (point)) (point))))
329 (setq tail (cdr tail))))
331 (setq from (point))))
333 (setcdr tail (list (buffer-substring-no-properties from to)))
334 (apply 'concat (cdr parts))))))
336 (defun c-shift-line-indentation (shift-amt)
337 ;; Shift the indentation of the current line with the specified
338 ;; amount (positive inwards). The buffer is modified only if
339 ;; SHIFT-AMT isn't equal to zero.
340 (let ((pos (- (point-max) (point)))
341 (c-macro-start c-macro-start)
342 tmp-char-inserted)
343 (if (zerop shift-amt)
345 ;; If we're on an empty line inside a macro, we take the point
346 ;; to be at the current indentation and shift it to the
347 ;; appropriate column. This way we don't treat the extra
348 ;; whitespace out to the line continuation as indentation.
349 (when (and (c-query-and-set-macro-start)
350 (looking-at "[ \t]*\\\\$")
351 (save-excursion
352 (skip-chars-backward " \t")
353 (bolp)))
354 (insert ?x)
355 (backward-char)
356 (setq tmp-char-inserted t))
357 (unwind-protect
358 (let ((col (current-indentation)))
359 (delete-region (c-point 'bol) (c-point 'boi))
360 (beginning-of-line)
361 (indent-to (+ col shift-amt)))
362 (when tmp-char-inserted
363 (delete-char 1))))
364 ;; If initial point was within line's indentation and we're not on
365 ;; a line with a line continuation in a macro, position after the
366 ;; indentation. Else stay at same point in text.
367 (if (and (< (point) (c-point 'boi))
368 (not tmp-char-inserted))
369 (back-to-indentation)
370 (if (> (- (point-max) pos) (point))
371 (goto-char (- (point-max) pos))))))
373 (defsubst c-keyword-sym (keyword)
374 ;; Return non-nil if the string KEYWORD is a known keyword. More
375 ;; precisely, the value is the symbol for the keyword in
376 ;; `c-keywords-obarray'.
377 (intern-soft keyword c-keywords-obarray))
379 (defsubst c-keyword-member (keyword-sym lang-constant)
380 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
381 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
382 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
383 ;; nil then the result is nil.
384 (get keyword-sym lang-constant))
386 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
387 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
388 "\"|"
389 "\""))
391 ;; Regexp matching string limit syntax.
392 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
393 "\\s\"\\|\\s|"
394 "\\s\""))
396 ;; Regexp matching WS followed by string limit syntax.
397 (defconst c-ws*-string-limit-regexp
398 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
400 ;; Holds formatted error strings for the few cases where parse errors
401 ;; are reported.
402 (defvar c-parsing-error nil)
403 (make-variable-buffer-local 'c-parsing-error)
405 (defun c-echo-parsing-error (&optional quiet)
406 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
407 (c-benign-error "%s" c-parsing-error))
408 c-parsing-error)
410 ;; Faces given to comments and string literals. This is used in some
411 ;; situations to speed up recognition; it isn't mandatory that font
412 ;; locking is in use. This variable is extended with the face in
413 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
414 (defvar c-literal-faces
415 (append '(font-lock-comment-face font-lock-string-face)
416 (when (facep 'font-lock-comment-delimiter-face)
417 ;; New in Emacs 22.
418 '(font-lock-comment-delimiter-face))))
420 (defsubst c-put-c-type-property (pos value)
421 ;; Put a c-type property with the given value at POS.
422 (c-put-char-property pos 'c-type value))
424 (defun c-clear-c-type-property (from to value)
425 ;; Remove all occurences of the c-type property that has the given
426 ;; value in the region between FROM and TO. VALUE is assumed to not
427 ;; be nil.
429 ;; Note: This assumes that c-type is put on single chars only; it's
430 ;; very inefficient if matching properties cover large regions.
431 (save-excursion
432 (goto-char from)
433 (while (progn
434 (when (eq (get-text-property (point) 'c-type) value)
435 (c-clear-char-property (point) 'c-type))
436 (goto-char (next-single-property-change (point) 'c-type nil to))
437 (< (point) to)))))
440 ;; Some debug tools to visualize various special positions. This
441 ;; debug code isn't as portable as the rest of CC Mode.
443 (cc-bytecomp-defun overlays-in)
444 (cc-bytecomp-defun overlay-get)
445 (cc-bytecomp-defun overlay-start)
446 (cc-bytecomp-defun overlay-end)
447 (cc-bytecomp-defun delete-overlay)
448 (cc-bytecomp-defun overlay-put)
449 (cc-bytecomp-defun make-overlay)
451 (defun c-debug-add-face (beg end face)
452 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
453 (while overlays
454 (setq overlay (car overlays)
455 overlays (cdr overlays))
456 (when (eq (overlay-get overlay 'face) face)
457 (setq beg (min beg (overlay-start overlay))
458 end (max end (overlay-end overlay)))
459 (delete-overlay overlay)))
460 (overlay-put (make-overlay beg end) 'face face)))
462 (defun c-debug-remove-face (beg end face)
463 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
464 (ol-beg beg) (ol-end end))
465 (while overlays
466 (setq overlay (car overlays)
467 overlays (cdr overlays))
468 (when (eq (overlay-get overlay 'face) face)
469 (setq ol-beg (min ol-beg (overlay-start overlay))
470 ol-end (max ol-end (overlay-end overlay)))
471 (delete-overlay overlay)))
472 (when (< ol-beg beg)
473 (overlay-put (make-overlay ol-beg beg) 'face face))
474 (when (> ol-end end)
475 (overlay-put (make-overlay end ol-end) 'face face))))
478 ;; `c-beginning-of-statement-1' and accompanying stuff.
480 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
481 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
482 ;; better way should be implemented, but this will at least shut up
483 ;; the byte compiler.
484 (defvar c-maybe-labelp)
486 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
488 ;; Macros used internally in c-beginning-of-statement-1 for the
489 ;; automaton actions.
490 (defmacro c-bos-push-state ()
491 '(setq stack (cons (cons state saved-pos)
492 stack)))
493 (defmacro c-bos-pop-state (&optional do-if-done)
494 `(if (setq state (car (car stack))
495 saved-pos (cdr (car stack))
496 stack (cdr stack))
498 ,do-if-done
499 (throw 'loop nil)))
500 (defmacro c-bos-pop-state-and-retry ()
501 '(throw 'loop (setq state (car (car stack))
502 saved-pos (cdr (car stack))
503 ;; Throw nil if stack is empty, else throw non-nil.
504 stack (cdr stack))))
505 (defmacro c-bos-save-pos ()
506 '(setq saved-pos (vector pos tok ptok pptok)))
507 (defmacro c-bos-restore-pos ()
508 '(unless (eq (elt saved-pos 0) start)
509 (setq pos (elt saved-pos 0)
510 tok (elt saved-pos 1)
511 ptok (elt saved-pos 2)
512 pptok (elt saved-pos 3))
513 (goto-char pos)
514 (setq sym nil)))
515 (defmacro c-bos-save-error-info (missing got)
516 `(setq saved-pos (vector pos ,missing ,got)))
517 (defmacro c-bos-report-error ()
518 '(unless noerror
519 (setq c-parsing-error
520 (format "No matching `%s' found for `%s' on line %d"
521 (elt saved-pos 1)
522 (elt saved-pos 2)
523 (1+ (count-lines (point-min)
524 (c-point 'bol (elt saved-pos 0))))))))
526 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
527 noerror comma-delim)
528 "Move to the start of the current statement or declaration, or to
529 the previous one if already at the beginning of one. Only
530 statements/declarations on the same level are considered, i.e. don't
531 move into or out of sexps (not even normal expression parentheses).
533 If point is already at the earliest statment within braces or parens,
534 this function doesn't move back into any whitespace preceding it; it
535 returns 'same in this case.
537 Stop at statement continuation tokens like \"else\", \"catch\",
538 \"finally\" and the \"while\" in \"do ... while\" if the start point
539 is within the continuation. If starting at such a token, move to the
540 corresponding statement start. If at the beginning of a statement,
541 move to the closest containing statement if there is any. This might
542 also stop at a continuation clause.
544 Labels are treated as part of the following statements if
545 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
546 statement start keyword.) Otherwise, each label is treated as a
547 separate statement.
549 Macros are ignored \(i.e. skipped over) unless point is within one, in
550 which case the content of the macro is treated as normal code. Aside
551 from any normal statement starts found in it, stop at the first token
552 of the content in the macro, i.e. the expression of an \"#if\" or the
553 start of the definition in a \"#define\". Also stop at start of
554 macros before leaving them.
556 Return:
557 'label if stopped at a label or \"case...:\" or \"default:\";
558 'same if stopped at the beginning of the current statement;
559 'up if stepped to a containing statement;
560 'previous if stepped to a preceding statement;
561 'beginning if stepped from a statement continuation clause to
562 its start clause; or
563 'macro if stepped to a macro start.
564 Note that 'same and not 'label is returned if stopped at the same
565 label without crossing the colon character.
567 LIM may be given to limit the search. If the search hits the limit,
568 point will be left at the closest following token, or at the start
569 position if that is less ('same is returned in this case).
571 NOERROR turns off error logging to `c-parsing-error'.
573 Normally only ';' and virtual semicolons are considered to delimit
574 statements, but if COMMA-DELIM is non-nil then ',' is treated
575 as a delimiter too.
577 Note that this function might do hidden buffer changes. See the
578 comment at the start of cc-engine.el for more info."
580 ;; The bulk of this function is a pushdown automaton that looks at statement
581 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
582 ;; purpose is to keep track of nested statements, ensuring that such
583 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
584 ;; does with nested braces/brackets/parentheses).
586 ;; Note: The position of a boundary is the following token.
588 ;; Beginning with the current token (the one following point), move back one
589 ;; sexp at a time (where a sexp is, more or less, either a token or the
590 ;; entire contents of a brace/bracket/paren pair). Each time a statement
591 ;; boundary is crossed or a "while"-like token is found, update the state of
592 ;; the PDA. Stop at the beginning of a statement when the stack (holding
593 ;; nested statement info) is empty and the position has been moved.
595 ;; The following variables constitute the PDA:
597 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
598 ;; scanned back over, 'boundary if we've just gone back over a
599 ;; statement boundary, or nil otherwise.
600 ;; state: takes one of the values (nil else else-boundary while
601 ;; while-boundary catch catch-boundary).
602 ;; nil means "no "while"-like token yet scanned".
603 ;; 'else, for example, means "just gone back over an else".
604 ;; 'else-boundary means "just gone back over a statement boundary
605 ;; immediately after having gone back over an else".
606 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
607 ;; of error reporting information.
608 ;; stack: The stack onto which the PDA pushes its state. Each entry
609 ;; consists of a saved value of state and saved-pos. An entry is
610 ;; pushed when we move back over a "continuation" token (e.g. else)
611 ;; and popped when we encounter the corresponding opening token
612 ;; (e.g. if).
615 ;; The following diagram briefly outlines the PDA.
617 ;; Common state:
618 ;; "else": Push state, goto state `else'.
619 ;; "while": Push state, goto state `while'.
620 ;; "catch" or "finally": Push state, goto state `catch'.
621 ;; boundary: Pop state.
622 ;; other: Do nothing special.
624 ;; State `else':
625 ;; boundary: Goto state `else-boundary'.
626 ;; other: Error, pop state, retry token.
628 ;; State `else-boundary':
629 ;; "if": Pop state.
630 ;; boundary: Error, pop state.
631 ;; other: See common state.
633 ;; State `while':
634 ;; boundary: Save position, goto state `while-boundary'.
635 ;; other: Pop state, retry token.
637 ;; State `while-boundary':
638 ;; "do": Pop state.
639 ;; boundary: Restore position if it's not at start, pop state. [*see below]
640 ;; other: See common state.
642 ;; State `catch':
643 ;; boundary: Goto state `catch-boundary'.
644 ;; other: Error, pop state, retry token.
646 ;; State `catch-boundary':
647 ;; "try": Pop state.
648 ;; "catch": Goto state `catch'.
649 ;; boundary: Error, pop state.
650 ;; other: See common state.
652 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
653 ;; searching for a "do" which would have opened a do-while. If we didn't
654 ;; find it, we discard the analysis done since the "while", go back to this
655 ;; token in the buffer and restart the scanning there, this time WITHOUT
656 ;; pushing the 'while state onto the stack.
658 ;; In addition to the above there is some special handling of labels
659 ;; and macros.
661 (let ((case-fold-search nil)
662 (start (point))
663 macro-start
664 (delims (if comma-delim '(?\; ?,) '(?\;)))
665 (c-stmt-delim-chars (if comma-delim
666 c-stmt-delim-chars-with-comma
667 c-stmt-delim-chars))
668 c-in-literal-cache c-maybe-labelp after-case:-pos saved
669 ;; Current position.
671 ;; Position of last stmt boundary character (e.g. ;).
672 boundary-pos
673 ;; The position of the last sexp or bound that follows the
674 ;; first found colon, i.e. the start of the nonlabel part of
675 ;; the statement. It's `start' if a colon is found just after
676 ;; the start.
677 after-labels-pos
678 ;; Like `after-labels-pos', but the first such position inside
679 ;; a label, i.e. the start of the last label before the start
680 ;; of the nonlabel part of the statement.
681 last-label-pos
682 ;; The last position where a label is possible provided the
683 ;; statement started there. It's nil as long as no invalid
684 ;; label content has been found (according to
685 ;; `c-nonlabel-token-key'. It's `start' if no valid label
686 ;; content was found in the label. Note that we might still
687 ;; regard it a label if it starts with `c-label-kwds'.
688 label-good-pos
689 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
690 ;; See above.
692 ;; Current state in the automaton. See above.
693 state
694 ;; Current saved positions. See above.
695 saved-pos
696 ;; Stack of conses (state . saved-pos).
697 stack
698 ;; Regexp which matches "for", "if", etc.
699 (cond-key (or c-opt-block-stmt-key
700 "\\<\\>")) ; Matches nothing.
701 ;; Return value.
702 (ret 'same)
703 ;; Positions of the last three sexps or bounds we've stopped at.
704 tok ptok pptok)
706 (save-restriction
707 (if lim (narrow-to-region lim (point-max)))
709 (if (save-excursion
710 (and (c-beginning-of-macro)
711 (/= (point) start)))
712 (setq macro-start (point)))
714 ;; Try to skip back over unary operator characters, to register
715 ;; that we've moved.
716 (while (progn
717 (setq pos (point))
718 (c-backward-syntactic-ws)
719 ;; Protect post-++/-- operators just before a virtual semicolon.
720 (and (not (c-at-vsemi-p))
721 (/= (skip-chars-backward "-+!*&~@`#") 0))))
723 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
724 ;; done. Later on we ignore the boundaries for statements that don't
725 ;; contain any sexp. The only thing that is affected is that the error
726 ;; checking is a little less strict, and we really don't bother.
727 (if (and (memq (char-before) delims)
728 (progn (forward-char -1)
729 (setq saved (point))
730 (c-backward-syntactic-ws)
731 (or (memq (char-before) delims)
732 (memq (char-before) '(?: nil))
733 (eq (char-syntax (char-before)) ?\()
734 (c-at-vsemi-p))))
735 (setq ret 'previous
736 pos saved)
738 ;; Begin at start and not pos to detect macros if we stand
739 ;; directly after the #.
740 (goto-char start)
741 (if (looking-at "\\<\\|\\W")
742 ;; Record this as the first token if not starting inside it.
743 (setq tok start))
745 ;; The following while loop goes back one sexp (balanced parens,
746 ;; etc. with contents, or symbol or suchlike) each iteration. This
747 ;; movement is accomplished with a call to scan-sexps approx 130 lines
748 ;; below.
749 (while
750 (catch 'loop ;; Throw nil to break, non-nil to continue.
751 (cond
752 ((save-excursion
753 (and macro-start ; Always NIL for AWK.
754 (progn (skip-chars-backward " \t")
755 (eq (char-before) ?#))
756 (progn (setq saved (1- (point)))
757 (beginning-of-line)
758 (not (eq (char-before (1- (point))) ?\\)))
759 (looking-at c-opt-cpp-start)
760 (progn (skip-chars-forward " \t")
761 (eq (point) saved))))
762 (goto-char saved)
763 (if (and (c-forward-to-cpp-define-body)
764 (progn (c-forward-syntactic-ws start)
765 (< (point) start)))
766 ;; Stop at the first token in the content of the macro.
767 (setq pos (point)
768 ignore-labels t) ; Avoid the label check on exit.
769 (setq pos saved
770 ret 'macro
771 ignore-labels t))
772 (throw 'loop nil))
774 ;; Do a round through the automaton if we've just passed a
775 ;; statement boundary or passed a "while"-like token.
776 ((or sym
777 (and (looking-at cond-key)
778 (setq sym (intern (match-string 1)))))
780 (when (and (< pos start) (null stack))
781 (throw 'loop nil))
783 ;; The PDA state handling.
785 ;; Refer to the description of the PDA in the opening
786 ;; comments. In the following OR form, the first leaf
787 ;; attempts to handles one of the specific actions detailed
788 ;; (e.g., finding token "if" whilst in state `else-boundary').
789 ;; We drop through to the second leaf (which handles common
790 ;; state) if no specific handler is found in the first cond.
791 ;; If a parsing error is detected (e.g. an "else" with no
792 ;; preceding "if"), we throw to the enclosing catch.
794 ;; Note that the (eq state 'else) means
795 ;; "we've just passed an else", NOT "we're looking for an
796 ;; else".
797 (or (cond
798 ((eq state 'else)
799 (if (eq sym 'boundary)
800 (setq state 'else-boundary)
801 (c-bos-report-error)
802 (c-bos-pop-state-and-retry)))
804 ((eq state 'else-boundary)
805 (cond ((eq sym 'if)
806 (c-bos-pop-state (setq ret 'beginning)))
807 ((eq sym 'boundary)
808 (c-bos-report-error)
809 (c-bos-pop-state))))
811 ((eq state 'while)
812 (if (and (eq sym 'boundary)
813 ;; Since this can cause backtracking we do a
814 ;; little more careful analysis to avoid it:
815 ;; If there's a label in front of the while
816 ;; it can't be part of a do-while.
817 (not after-labels-pos))
818 (progn (c-bos-save-pos)
819 (setq state 'while-boundary))
820 (c-bos-pop-state-and-retry))) ; Can't be a do-while
822 ((eq state 'while-boundary)
823 (cond ((eq sym 'do)
824 (c-bos-pop-state (setq ret 'beginning)))
825 ((eq sym 'boundary) ; isn't a do-while
826 (c-bos-restore-pos) ; the position of the while
827 (c-bos-pop-state)))) ; no longer searching for do.
829 ((eq state 'catch)
830 (if (eq sym 'boundary)
831 (setq state 'catch-boundary)
832 (c-bos-report-error)
833 (c-bos-pop-state-and-retry)))
835 ((eq state 'catch-boundary)
836 (cond
837 ((eq sym 'try)
838 (c-bos-pop-state (setq ret 'beginning)))
839 ((eq sym 'catch)
840 (setq state 'catch))
841 ((eq sym 'boundary)
842 (c-bos-report-error)
843 (c-bos-pop-state)))))
845 ;; This is state common. We get here when the previous
846 ;; cond statement found no particular state handler.
847 (cond ((eq sym 'boundary)
848 ;; If we have a boundary at the start
849 ;; position we push a frame to go to the
850 ;; previous statement.
851 (if (>= pos start)
852 (c-bos-push-state)
853 (c-bos-pop-state)))
854 ((eq sym 'else)
855 (c-bos-push-state)
856 (c-bos-save-error-info 'if 'else)
857 (setq state 'else))
858 ((eq sym 'while)
859 ;; Is this a real while, or a do-while?
860 ;; The next `when' triggers unless we are SURE that
861 ;; the `while' is not the tailend of a `do-while'.
862 (when (or (not pptok)
863 (memq (char-after pptok) delims)
864 ;; The following kludge is to prevent
865 ;; infinite recursion when called from
866 ;; c-awk-after-if-for-while-condition-p,
867 ;; or the like.
868 (and (eq (point) start)
869 (c-vsemi-status-unknown-p))
870 (c-at-vsemi-p pptok))
871 ;; Since this can cause backtracking we do a
872 ;; little more careful analysis to avoid it: If
873 ;; the while isn't followed by a (possibly
874 ;; virtual) semicolon it can't be a do-while.
875 (c-bos-push-state)
876 (setq state 'while)))
877 ((memq sym '(catch finally))
878 (c-bos-push-state)
879 (c-bos-save-error-info 'try sym)
880 (setq state 'catch))))
882 (when c-maybe-labelp
883 ;; We're either past a statement boundary or at the
884 ;; start of a statement, so throw away any label data
885 ;; for the previous one.
886 (setq after-labels-pos nil
887 last-label-pos nil
888 c-maybe-labelp nil))))
890 ;; Step to the previous sexp, but not if we crossed a
891 ;; boundary, since that doesn't consume an sexp.
892 (if (eq sym 'boundary)
893 (setq ret 'previous)
895 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
896 ;; BACKWARDS THROUGH THE SOURCE.
898 ;; This is typically fast with the caching done by
899 ;; c-(backward|forward)-sws.
900 (c-backward-syntactic-ws)
902 (let ((before-sws-pos (point))
903 ;; Set as long as we have to continue jumping by sexps.
904 ;; It's the position to use as end in the next round.
905 sexp-loop-continue-pos
906 ;; The end position of the area to search for statement
907 ;; barriers in this round.
908 (sexp-loop-end-pos pos))
910 ;; The following while goes back one sexp per iteration.
911 (while
912 (progn
913 (unless (c-safe (c-backward-sexp) t)
914 ;; Give up if we hit an unbalanced block. Since the
915 ;; stack won't be empty the code below will report a
916 ;; suitable error.
917 (throw 'loop nil))
919 ;; Check if the sexp movement crossed a statement or
920 ;; declaration boundary. But first modify the point
921 ;; so that `c-crosses-statement-barrier-p' only looks
922 ;; at the non-sexp chars following the sexp.
923 (save-excursion
924 (when (setq
925 boundary-pos
926 (cond
927 ((if macro-start
929 (save-excursion
930 (when (c-beginning-of-macro)
931 ;; Set continuation position in case
932 ;; `c-crosses-statement-barrier-p'
933 ;; doesn't detect anything below.
934 (setq sexp-loop-continue-pos (point)))))
935 ;; If the sexp movement took us into a
936 ;; macro then there were only some non-sexp
937 ;; chars after it. Skip out of the macro
938 ;; to analyze them but not the non-sexp
939 ;; chars that might be inside the macro.
940 (c-end-of-macro)
941 (c-crosses-statement-barrier-p
942 (point) sexp-loop-end-pos))
944 ((and
945 (eq (char-after) ?{)
946 (not (c-looking-at-inexpr-block lim nil t)))
947 ;; Passed a block sexp. That's a boundary
948 ;; alright.
949 (point))
951 ((looking-at "\\s\(")
952 ;; Passed some other paren. Only analyze
953 ;; the non-sexp chars after it.
954 (goto-char (1+ (c-down-list-backward
955 before-sws-pos)))
956 ;; We're at a valid token start position
957 ;; (outside the `save-excursion') if
958 ;; `c-crosses-statement-barrier-p' failed.
959 (c-crosses-statement-barrier-p
960 (point) sexp-loop-end-pos))
963 ;; Passed a symbol sexp or line
964 ;; continuation. It doesn't matter that
965 ;; it's included in the analyzed region.
966 (if (c-crosses-statement-barrier-p
967 (point) sexp-loop-end-pos)
969 ;; If it was a line continuation then we
970 ;; have to continue looping.
971 (if (looking-at "\\\\$")
972 (setq sexp-loop-continue-pos (point)))
973 nil))))
975 (setq pptok ptok
976 ptok tok
977 tok boundary-pos
978 sym 'boundary)
979 ;; Like a C "continue". Analyze the next sexp.
980 (throw 'loop t)))
982 sexp-loop-continue-pos) ; End of "go back a sexp" loop condition.
983 (goto-char sexp-loop-continue-pos)
984 (setq sexp-loop-end-pos sexp-loop-continue-pos
985 sexp-loop-continue-pos nil))))
987 ;; ObjC method def?
988 (when (and c-opt-method-key
989 (setq saved (c-in-method-def-p)))
990 (setq pos saved
991 ignore-labels t) ; Avoid the label check on exit.
992 (throw 'loop nil))
994 ;; Handle labels.
995 (unless (eq ignore-labels t)
996 (when (numberp c-maybe-labelp)
997 ;; `c-crosses-statement-barrier-p' has found a colon, so we
998 ;; might be in a label now. Have we got a real label
999 ;; (including a case label) or something like C++'s "public:"?
1000 ;; A case label might use an expression rather than a token.
1001 (setq after-case:-pos (or tok start))
1002 (if (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1003 (setq c-maybe-labelp nil)
1004 (if after-labels-pos ; Have we already encountered a label?
1005 (if (not last-label-pos)
1006 (setq last-label-pos (or tok start)))
1007 (setq after-labels-pos (or tok start)))
1008 (setq c-maybe-labelp t
1009 label-good-pos nil))) ; bogus "label"
1011 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1012 ; been found.
1013 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1014 ;; We're in a potential label and it's the first
1015 ;; time we've found something that isn't allowed in
1016 ;; one.
1017 (setq label-good-pos (or tok start))))
1019 ;; We've moved back by a sexp, so update the token positions.
1020 (setq sym nil
1021 pptok ptok
1022 ptok tok
1023 tok (point)
1024 pos tok))) ; Not nil (for the while loop).
1026 ;; If the stack isn't empty there might be errors to report.
1027 (while stack
1028 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1029 (c-bos-report-error))
1030 (setq saved-pos (cdr (car stack))
1031 stack (cdr stack)))
1033 (when (and (eq ret 'same)
1034 (not (memq sym '(boundary ignore nil))))
1035 ;; Need to investigate closer whether we've crossed
1036 ;; between a substatement and its containing statement.
1037 (if (setq saved (if (looking-at c-block-stmt-1-key)
1038 ptok
1039 pptok))
1040 (cond ((> start saved) (setq pos saved))
1041 ((= start saved) (setq ret 'up)))))
1043 (when (and (not ignore-labels)
1044 (eq c-maybe-labelp t)
1045 (not (eq ret 'beginning))
1046 after-labels-pos
1047 (or (not label-good-pos)
1048 (<= label-good-pos pos)
1049 (progn
1050 (goto-char (if (and last-label-pos
1051 (< last-label-pos start))
1052 last-label-pos
1053 pos))
1054 (looking-at c-label-kwds-regexp))))
1055 ;; We're in a label. Maybe we should step to the statement
1056 ;; after it.
1057 (if (< after-labels-pos start)
1058 (setq pos after-labels-pos)
1059 (setq ret 'label)
1060 (if (and last-label-pos (< last-label-pos start))
1061 ;; Might have jumped over several labels. Go to the last one.
1062 (setq pos last-label-pos)))))
1064 ;; Have we got "case <expression>:"?
1065 (goto-char pos)
1066 (when (and after-case:-pos
1067 (not (eq ret 'beginning))
1068 (looking-at c-case-kwds-regexp))
1069 (if (< after-case:-pos start)
1070 (setq pos after-case:-pos))
1071 (if (eq ret 'same)
1072 (setq ret 'label)))
1074 ;; Skip over the unary operators that can start the statement.
1075 (while (progn
1076 (c-backward-syntactic-ws)
1077 ;; protect AWK post-inc/decrement operators, etc.
1078 (and (not (c-at-vsemi-p (point)))
1079 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1080 (setq pos (point)))
1081 (goto-char pos)
1082 ret)))
1084 (defun c-crosses-statement-barrier-p (from to)
1085 "Return non-nil if buffer positions FROM to TO cross one or more
1086 statement or declaration boundaries. The returned value is actually
1087 the position of the earliest boundary char. FROM must not be within
1088 a string or comment.
1090 The variable `c-maybe-labelp' is set to the position of the first `:' that
1091 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1092 single `?' is found, then `c-maybe-labelp' is cleared.
1094 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1095 regarded as having a \"virtual semicolon\" immediately after the last token on
1096 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1098 Note that this function might do hidden buffer changes. See the
1099 comment at the start of cc-engine.el for more info."
1100 (let ((skip-chars c-stmt-delim-chars)
1101 lit-range)
1102 (save-excursion
1103 (catch 'done
1104 (goto-char from)
1105 (while (progn (skip-chars-forward skip-chars to)
1106 (< (point) to))
1107 (cond
1108 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1109 (goto-char (cdr lit-range)))
1110 ((eq (char-after) ?:)
1111 (forward-char)
1112 (if (and (eq (char-after) ?:)
1113 (< (point) to))
1114 ;; Ignore scope operators.
1115 (forward-char)
1116 (setq c-maybe-labelp (1- (point)))))
1117 ((eq (char-after) ??)
1118 ;; A question mark. Can't be a label, so stop
1119 ;; looking for more : and ?.
1120 (setq c-maybe-labelp nil
1121 skip-chars (substring c-stmt-delim-chars 0 -2)))
1122 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1123 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1124 (backward-char))
1125 (skip-chars-backward " \t" from)
1126 (if (c-at-vsemi-p)
1127 (throw 'done (point))
1128 (forward-line)))
1129 (t (throw 'done (point)))))
1130 ;; In trailing space after an as yet undetected virtual semicolon?
1131 (c-backward-syntactic-ws from)
1132 (if (and (< (point) to)
1133 (c-at-vsemi-p))
1134 (point)
1135 nil)))))
1137 (defun c-at-statement-start-p ()
1138 "Return non-nil if the point is at the first token in a statement
1139 or somewhere in the syntactic whitespace before it.
1141 A \"statement\" here is not restricted to those inside code blocks.
1142 Any kind of declaration-like construct that occur outside function
1143 bodies is also considered a \"statement\".
1145 Note that this function might do hidden buffer changes. See the
1146 comment at the start of cc-engine.el for more info."
1148 (save-excursion
1149 (let ((end (point))
1150 c-maybe-labelp)
1151 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1152 (or (bobp)
1153 (eq (char-before) ?})
1154 (and (eq (char-before) ?{)
1155 (not (and c-special-brace-lists
1156 (progn (backward-char)
1157 (c-looking-at-special-brace-list)))))
1158 (c-crosses-statement-barrier-p (point) end)))))
1160 (defun c-at-expression-start-p ()
1161 "Return non-nil if the point is at the first token in an expression or
1162 statement, or somewhere in the syntactic whitespace before it.
1164 An \"expression\" here is a bit different from the normal language
1165 grammar sense: It's any sequence of expression tokens except commas,
1166 unless they are enclosed inside parentheses of some kind. Also, an
1167 expression never continues past an enclosing parenthesis, but it might
1168 contain parenthesis pairs of any sort except braces.
1170 Since expressions never cross statement boundaries, this function also
1171 recognizes statement beginnings, just like `c-at-statement-start-p'.
1173 Note that this function might do hidden buffer changes. See the
1174 comment at the start of cc-engine.el for more info."
1176 (save-excursion
1177 (let ((end (point))
1178 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1179 c-maybe-labelp)
1180 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1181 (or (bobp)
1182 (memq (char-before) '(?{ ?}))
1183 (save-excursion (backward-char)
1184 (looking-at "\\s("))
1185 (c-crosses-statement-barrier-p (point) end)))))
1188 ;; A set of functions that covers various idiosyncrasies in
1189 ;; implementations of `forward-comment'.
1191 ;; Note: Some emacsen considers incorrectly that any line comment
1192 ;; ending with a backslash continues to the next line. I can't think
1193 ;; of any way to work around that in a reliable way without changing
1194 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1195 ;; changing the syntax for backslash doesn't work since we must treat
1196 ;; escapes in string literals correctly.)
1198 (defun c-forward-single-comment ()
1199 "Move forward past whitespace and the closest following comment, if any.
1200 Return t if a comment was found, nil otherwise. In either case, the
1201 point is moved past the following whitespace. Line continuations,
1202 i.e. a backslashes followed by line breaks, are treated as whitespace.
1203 The line breaks that end line comments are considered to be the
1204 comment enders, so the point will be put on the beginning of the next
1205 line if it moved past a line comment.
1207 This function does not do any hidden buffer changes."
1209 (let ((start (point)))
1210 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1211 (goto-char (match-end 0)))
1213 (when (forward-comment 1)
1214 (if (eobp)
1215 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1216 ;; forwards at eob.
1219 ;; Emacs includes the ending newline in a b-style (c++)
1220 ;; comment, but XEmacs doesn't. We depend on the Emacs
1221 ;; behavior (which also is symmetric).
1222 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1223 (condition-case nil (forward-char 1)))
1225 t))))
1227 (defsubst c-forward-comments ()
1228 "Move forward past all following whitespace and comments.
1229 Line continuations, i.e. a backslashes followed by line breaks, are
1230 treated as whitespace.
1232 Note that this function might do hidden buffer changes. See the
1233 comment at the start of cc-engine.el for more info."
1235 (while (or
1236 ;; If forward-comment in at least XEmacs 21 is given a large
1237 ;; positive value, it'll loop all the way through if it hits
1238 ;; eob.
1239 (and (forward-comment 5)
1240 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1241 ;; forwards at eob.
1242 (not (eobp)))
1244 (when (looking-at "\\\\[\n\r]")
1245 (forward-char 2)
1246 t))))
1248 (defun c-backward-single-comment ()
1249 "Move backward past whitespace and the closest preceding comment, if any.
1250 Return t if a comment was found, nil otherwise. In either case, the
1251 point is moved past the preceding whitespace. Line continuations,
1252 i.e. a backslashes followed by line breaks, are treated as whitespace.
1253 The line breaks that end line comments are considered to be the
1254 comment enders, so the point cannot be at the end of the same line to
1255 move over a line comment.
1257 This function does not do any hidden buffer changes."
1259 (let ((start (point)))
1260 ;; When we got newline terminated comments, forward-comment in all
1261 ;; supported emacsen so far will stop at eol of each line not
1262 ;; ending with a comment when moving backwards. This corrects for
1263 ;; that, and at the same time handles line continuations.
1264 (while (progn
1265 (skip-chars-backward " \t\n\r\f\v")
1266 (and (looking-at "[\n\r]")
1267 (eq (char-before) ?\\)))
1268 (backward-char))
1270 (if (bobp)
1271 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1272 ;; backwards at bob.
1275 ;; Leave point after the closest following newline if we've
1276 ;; backed up over any above, since forward-comment won't move
1277 ;; backward over a line comment if point is at the end of the
1278 ;; same line.
1279 (re-search-forward "\\=\\s *[\n\r]" start t)
1281 (if (if (forward-comment -1)
1282 (if (eolp)
1283 ;; If forward-comment above succeeded and we're at eol
1284 ;; then the newline we moved over above didn't end a
1285 ;; line comment, so we give it another go.
1286 (forward-comment -1)
1289 ;; Emacs <= 20 and XEmacs move back over the closer of a
1290 ;; block comment that lacks an opener.
1291 (if (looking-at "\\*/")
1292 (progn (forward-char 2) nil)
1293 t)))))
1295 (defsubst c-backward-comments ()
1296 "Move backward past all preceding whitespace and comments.
1297 Line continuations, i.e. a backslashes followed by line breaks, are
1298 treated as whitespace. The line breaks that end line comments are
1299 considered to be the comment enders, so the point cannot be at the end
1300 of the same line to move over a line comment. Unlike
1301 c-backward-syntactic-ws, this function doesn't move back over
1302 preprocessor directives.
1304 Note that this function might do hidden buffer changes. See the
1305 comment at the start of cc-engine.el for more info."
1307 (let ((start (point)))
1308 (while (and
1309 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1310 ;; return t when moving backwards at bob.
1311 (not (bobp))
1313 (if (forward-comment -1)
1314 (if (looking-at "\\*/")
1315 ;; Emacs <= 20 and XEmacs move back over the
1316 ;; closer of a block comment that lacks an opener.
1317 (progn (forward-char 2) nil)
1320 ;; XEmacs treats line continuations as whitespace but
1321 ;; only in the backward direction, which seems a bit
1322 ;; odd. Anyway, this is necessary for Emacs.
1323 (when (and (looking-at "[\n\r]")
1324 (eq (char-before) ?\\)
1325 (< (point) start))
1326 (backward-char)
1327 t))))))
1330 ;; Tools for skipping over syntactic whitespace.
1332 ;; The following functions use text properties to cache searches over
1333 ;; large regions of syntactic whitespace. It works as follows:
1335 ;; o If a syntactic whitespace region contains anything but simple
1336 ;; whitespace (i.e. space, tab and line breaks), the text property
1337 ;; `c-in-sws' is put over it. At places where we have stopped
1338 ;; within that region there's also a `c-is-sws' text property.
1339 ;; That since there typically are nested whitespace inside that
1340 ;; must be handled separately, e.g. whitespace inside a comment or
1341 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1342 ;; to jump to another point with that property within the same
1343 ;; `c-in-sws' region. It can be likened to a ladder where
1344 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1346 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1347 ;; a "rung position" and also maybe on the first following char.
1348 ;; As many characters as can be conveniently found in this range
1349 ;; are marked, but no assumption can be made that the whole range
1350 ;; is marked (it could be clobbered by later changes, for
1351 ;; instance).
1353 ;; Note that some part of the beginning of a sequence of simple
1354 ;; whitespace might be part of the end of a preceding line comment
1355 ;; or cpp directive and must not be considered part of the "rung".
1356 ;; Such whitespace is some amount of horizontal whitespace followed
1357 ;; by a newline. In the case of cpp directives it could also be
1358 ;; two newlines with horizontal whitespace between them.
1360 ;; The reason to include the first following char is to cope with
1361 ;; "rung positions" that doesn't have any ordinary whitespace. If
1362 ;; `c-is-sws' is put on a token character it does not have
1363 ;; `c-in-sws' set simultaneously. That's the only case when that
1364 ;; can occur, and the reason for not extending the `c-in-sws'
1365 ;; region to cover it is that the `c-in-sws' region could then be
1366 ;; accidentally merged with a following one if the token is only
1367 ;; one character long.
1369 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1370 ;; removed in the changed region. If the change was inside
1371 ;; syntactic whitespace that means that the "ladder" is broken, but
1372 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1373 ;; parts on either side and use an ordinary search only to "repair"
1374 ;; the gap.
1376 ;; Special care needs to be taken if a region is removed: If there
1377 ;; are `c-in-sws' on both sides of it which do not connect inside
1378 ;; the region then they can't be joined. If e.g. a marked macro is
1379 ;; broken, syntactic whitespace inside the new text might be
1380 ;; marked. If those marks would become connected with the old
1381 ;; `c-in-sws' range around the macro then we could get a ladder
1382 ;; with one end outside the macro and the other at some whitespace
1383 ;; within it.
1385 ;; The main motivation for this system is to increase the speed in
1386 ;; skipping over the large whitespace regions that can occur at the
1387 ;; top level in e.g. header files that contain a lot of comments and
1388 ;; cpp directives. For small comments inside code it's probably
1389 ;; slower than using `forward-comment' straightforwardly, but speed is
1390 ;; not a significant factor there anyway.
1392 ; (defface c-debug-is-sws-face
1393 ; '((t (:background "GreenYellow")))
1394 ; "Debug face to mark the `c-is-sws' property.")
1395 ; (defface c-debug-in-sws-face
1396 ; '((t (:underline t)))
1397 ; "Debug face to mark the `c-in-sws' property.")
1399 ; (defun c-debug-put-sws-faces ()
1400 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1401 ; ;; properties in the buffer.
1402 ; (interactive)
1403 ; (save-excursion
1404 ; (c-save-buffer-state (in-face)
1405 ; (goto-char (point-min))
1406 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1407 ; (point)))
1408 ; (while (progn
1409 ; (goto-char (next-single-property-change
1410 ; (point) 'c-is-sws nil (point-max)))
1411 ; (if in-face
1412 ; (progn
1413 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1414 ; (setq in-face nil))
1415 ; (setq in-face (point)))
1416 ; (not (eobp))))
1417 ; (goto-char (point-min))
1418 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1419 ; (point)))
1420 ; (while (progn
1421 ; (goto-char (next-single-property-change
1422 ; (point) 'c-in-sws nil (point-max)))
1423 ; (if in-face
1424 ; (progn
1425 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1426 ; (setq in-face nil))
1427 ; (setq in-face (point)))
1428 ; (not (eobp)))))))
1430 (defmacro c-debug-sws-msg (&rest args)
1431 ;;`(message ,@args)
1434 (defmacro c-put-is-sws (beg end)
1435 ;; This macro does a hidden buffer change.
1436 `(let ((beg ,beg) (end ,end))
1437 (put-text-property beg end 'c-is-sws t)
1438 ,@(when (facep 'c-debug-is-sws-face)
1439 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1441 (defmacro c-put-in-sws (beg end)
1442 ;; This macro does a hidden buffer change.
1443 `(let ((beg ,beg) (end ,end))
1444 (put-text-property beg end 'c-in-sws t)
1445 ,@(when (facep 'c-debug-is-sws-face)
1446 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1448 (defmacro c-remove-is-sws (beg end)
1449 ;; This macro does a hidden buffer change.
1450 `(let ((beg ,beg) (end ,end))
1451 (remove-text-properties beg end '(c-is-sws nil))
1452 ,@(when (facep 'c-debug-is-sws-face)
1453 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1455 (defmacro c-remove-in-sws (beg end)
1456 ;; This macro does a hidden buffer change.
1457 `(let ((beg ,beg) (end ,end))
1458 (remove-text-properties beg end '(c-in-sws nil))
1459 ,@(when (facep 'c-debug-is-sws-face)
1460 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1462 (defmacro c-remove-is-and-in-sws (beg end)
1463 ;; This macro does a hidden buffer change.
1464 `(let ((beg ,beg) (end ,end))
1465 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1466 ,@(when (facep 'c-debug-is-sws-face)
1467 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1468 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1470 (defsubst c-invalidate-sws-region-after (beg end)
1471 ;; Called from `after-change-functions'. Note that if
1472 ;; `c-forward-sws' or `c-backward-sws' are used outside
1473 ;; `c-save-buffer-state' or similar then this will remove the cache
1474 ;; properties right after they're added.
1476 ;; This function does hidden buffer changes.
1478 (save-excursion
1479 ;; Adjust the end to remove the properties in any following simple
1480 ;; ws up to and including the next line break, if there is any
1481 ;; after the changed region. This is necessary e.g. when a rung
1482 ;; marked empty line is converted to a line comment by inserting
1483 ;; "//" before the line break. In that case the line break would
1484 ;; keep the rung mark which could make a later `c-backward-sws'
1485 ;; move into the line comment instead of over it.
1486 (goto-char end)
1487 (skip-chars-forward " \t\f\v")
1488 (when (and (eolp) (not (eobp)))
1489 (setq end (1+ (point)))))
1491 (when (and (= beg end)
1492 (get-text-property beg 'c-in-sws)
1493 (> beg (point-min))
1494 (get-text-property (1- beg) 'c-in-sws))
1495 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1496 ;; safe to keep a range that was continuous before the change. E.g:
1498 ;; #define foo
1499 ;; \
1500 ;; bar
1502 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1503 ;; after "foo" is removed then "bar" will become part of the cpp
1504 ;; directive instead of a syntactically relevant token. In that
1505 ;; case there's no longer syntactic ws from "#" to "b".
1506 (setq beg (1- beg)))
1508 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1509 (c-remove-is-and-in-sws beg end))
1511 (defun c-forward-sws ()
1512 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1514 ;; This function might do hidden buffer changes.
1516 (let (;; `rung-pos' is set to a position as early as possible in the
1517 ;; unmarked part of the simple ws region.
1518 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1519 rung-is-marked next-rung-is-marked simple-ws-end
1520 ;; `safe-start' is set when it's safe to cache the start position.
1521 ;; It's not set if we've initially skipped over comments and line
1522 ;; continuations since we might have gone out through the end of a
1523 ;; macro then. This provision makes `c-forward-sws' not populate the
1524 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1525 ;; more common.
1526 safe-start)
1528 ;; Skip simple ws and do a quick check on the following character to see
1529 ;; if it's anything that can't start syntactic ws, so we can bail out
1530 ;; early in the majority of cases when there just are a few ws chars.
1531 (skip-chars-forward " \t\n\r\f\v")
1532 (when (looking-at c-syntactic-ws-start)
1534 (setq rung-end-pos (min (1+ (point)) (point-max)))
1535 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1536 'c-is-sws t))
1537 ;; Find the last rung position to avoid setting properties in all
1538 ;; the cases when the marked rung is complete.
1539 ;; (`next-single-property-change' is certain to move at least one
1540 ;; step forward.)
1541 (setq rung-pos (1- (next-single-property-change
1542 rung-is-marked 'c-is-sws nil rung-end-pos)))
1543 ;; Got no marked rung here. Since the simple ws might have started
1544 ;; inside a line comment or cpp directive we must set `rung-pos' as
1545 ;; high as possible.
1546 (setq rung-pos (point)))
1548 (while
1549 (progn
1550 (while
1551 (when (and rung-is-marked
1552 (get-text-property (point) 'c-in-sws))
1554 ;; The following search is the main reason that `c-in-sws'
1555 ;; and `c-is-sws' aren't combined to one property.
1556 (goto-char (next-single-property-change
1557 (point) 'c-in-sws nil (point-max)))
1558 (unless (get-text-property (point) 'c-is-sws)
1559 ;; If the `c-in-sws' region extended past the last
1560 ;; `c-is-sws' char we have to go back a bit.
1561 (or (get-text-property (1- (point)) 'c-is-sws)
1562 (goto-char (previous-single-property-change
1563 (point) 'c-is-sws)))
1564 (backward-char))
1566 (c-debug-sws-msg
1567 "c-forward-sws cached move %s -> %s (max %s)"
1568 rung-pos (point) (point-max))
1570 (setq rung-pos (point))
1571 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1572 (not (eobp))))
1574 ;; We'll loop here if there is simple ws after the last rung.
1575 ;; That means that there's been some change in it and it's
1576 ;; possible that we've stepped into another ladder, so extend
1577 ;; the previous one to join with it if there is one, and try to
1578 ;; use the cache again.
1579 (c-debug-sws-msg
1580 "c-forward-sws extending rung with [%s..%s] (max %s)"
1581 (1+ rung-pos) (1+ (point)) (point-max))
1582 (unless (get-text-property (point) 'c-is-sws)
1583 ;; Remove any `c-in-sws' property from the last char of
1584 ;; the rung before we mark it with `c-is-sws', so that we
1585 ;; won't connect with the remains of a broken "ladder".
1586 (c-remove-in-sws (point) (1+ (point))))
1587 (c-put-is-sws (1+ rung-pos)
1588 (1+ (point)))
1589 (c-put-in-sws rung-pos
1590 (setq rung-pos (point)
1591 last-put-in-sws-pos rung-pos)))
1593 (setq simple-ws-end (point))
1594 (c-forward-comments)
1596 (cond
1597 ((/= (point) simple-ws-end)
1598 ;; Skipped over comments. Don't cache at eob in case the buffer
1599 ;; is narrowed.
1600 (not (eobp)))
1602 ((save-excursion
1603 (and c-opt-cpp-prefix
1604 (looking-at c-opt-cpp-start)
1605 (progn (skip-chars-backward " \t")
1606 (bolp))
1607 (or (bobp)
1608 (progn (backward-char)
1609 (not (eq (char-before) ?\\))))))
1610 ;; Skip a preprocessor directive.
1611 (end-of-line)
1612 (while (and (eq (char-before) ?\\)
1613 (= (forward-line 1) 0))
1614 (end-of-line))
1615 (forward-line 1)
1616 (setq safe-start t)
1617 ;; Don't cache at eob in case the buffer is narrowed.
1618 (not (eobp)))))
1620 ;; We've searched over a piece of non-white syntactic ws. See if this
1621 ;; can be cached.
1622 (setq next-rung-pos (point))
1623 (skip-chars-forward " \t\n\r\f\v")
1624 (setq rung-end-pos (min (1+ (point)) (point-max)))
1626 (if (or
1627 ;; Cache if we haven't skipped comments only, and if we started
1628 ;; either from a marked rung or from a completely uncached
1629 ;; position.
1630 (and safe-start
1631 (or rung-is-marked
1632 (not (get-text-property simple-ws-end 'c-in-sws))))
1634 ;; See if there's a marked rung in the encountered simple ws. If
1635 ;; so then we can cache, unless `safe-start' is nil. Even then
1636 ;; we need to do this to check if the cache can be used for the
1637 ;; next step.
1638 (and (setq next-rung-is-marked
1639 (text-property-any next-rung-pos rung-end-pos
1640 'c-is-sws t))
1641 safe-start))
1643 (progn
1644 (c-debug-sws-msg
1645 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1646 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1647 (point-max))
1649 ;; Remove the properties for any nested ws that might be cached.
1650 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1651 ;; anyway.
1652 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1653 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1654 (c-put-is-sws rung-pos
1655 (1+ simple-ws-end))
1656 (setq rung-is-marked t))
1657 (c-put-in-sws rung-pos
1658 (setq rung-pos (point)
1659 last-put-in-sws-pos rung-pos))
1660 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1661 ;; Remove any `c-in-sws' property from the last char of
1662 ;; the rung before we mark it with `c-is-sws', so that we
1663 ;; won't connect with the remains of a broken "ladder".
1664 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1665 (c-put-is-sws next-rung-pos
1666 rung-end-pos))
1668 (c-debug-sws-msg
1669 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1670 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1671 (point-max))
1673 ;; Set `rung-pos' for the next rung. It's the same thing here as
1674 ;; initially, except that the rung position is set as early as
1675 ;; possible since we can't be in the ending ws of a line comment or
1676 ;; cpp directive now.
1677 (if (setq rung-is-marked next-rung-is-marked)
1678 (setq rung-pos (1- (next-single-property-change
1679 rung-is-marked 'c-is-sws nil rung-end-pos)))
1680 (setq rung-pos next-rung-pos))
1681 (setq safe-start t)))
1683 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1684 ;; another one after the point (which might occur when editing inside a
1685 ;; comment or macro).
1686 (when (eq last-put-in-sws-pos (point))
1687 (cond ((< last-put-in-sws-pos (point-max))
1688 (c-debug-sws-msg
1689 "c-forward-sws clearing at %s for cache separation"
1690 last-put-in-sws-pos)
1691 (c-remove-in-sws last-put-in-sws-pos
1692 (1+ last-put-in-sws-pos)))
1694 ;; If at eob we have to clear the last character before the end
1695 ;; instead since the buffer might be narrowed and there might
1696 ;; be a `c-in-sws' after (point-max). In this case it's
1697 ;; necessary to clear both properties.
1698 (c-debug-sws-msg
1699 "c-forward-sws clearing thoroughly at %s for cache separation"
1700 (1- last-put-in-sws-pos))
1701 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1702 last-put-in-sws-pos))))
1705 (defun c-backward-sws ()
1706 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1708 ;; This function might do hidden buffer changes.
1710 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1711 ;; part of the simple ws region.
1712 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1713 rung-is-marked simple-ws-beg cmt-skip-pos)
1715 ;; Skip simple horizontal ws and do a quick check on the preceding
1716 ;; character to see if it's anying that can't end syntactic ws, so we can
1717 ;; bail out early in the majority of cases when there just are a few ws
1718 ;; chars. Newlines are complicated in the backward direction, so we can't
1719 ;; skip over them.
1720 (skip-chars-backward " \t\f")
1721 (when (and (not (bobp))
1722 (save-excursion
1723 (backward-char)
1724 (looking-at c-syntactic-ws-end)))
1726 ;; Try to find a rung position in the simple ws preceding point, so that
1727 ;; we can get a cache hit even if the last bit of the simple ws has
1728 ;; changed recently.
1729 (setq simple-ws-beg (point))
1730 (skip-chars-backward " \t\n\r\f\v")
1731 (if (setq rung-is-marked (text-property-any
1732 (point) (min (1+ rung-pos) (point-max))
1733 'c-is-sws t))
1734 ;; `rung-pos' will be the earliest marked position, which means that
1735 ;; there might be later unmarked parts in the simple ws region.
1736 ;; It's not worth the effort to fix that; the last part of the
1737 ;; simple ws is also typically edited often, so it could be wasted.
1738 (goto-char (setq rung-pos rung-is-marked))
1739 (goto-char simple-ws-beg))
1741 (while
1742 (progn
1743 (while
1744 (when (and rung-is-marked
1745 (not (bobp))
1746 (get-text-property (1- (point)) 'c-in-sws))
1748 ;; The following search is the main reason that `c-in-sws'
1749 ;; and `c-is-sws' aren't combined to one property.
1750 (goto-char (previous-single-property-change
1751 (point) 'c-in-sws nil (point-min)))
1752 (unless (get-text-property (point) 'c-is-sws)
1753 ;; If the `c-in-sws' region extended past the first
1754 ;; `c-is-sws' char we have to go forward a bit.
1755 (goto-char (next-single-property-change
1756 (point) 'c-is-sws)))
1758 (c-debug-sws-msg
1759 "c-backward-sws cached move %s <- %s (min %s)"
1760 (point) rung-pos (point-min))
1762 (setq rung-pos (point))
1763 (if (and (< (min (skip-chars-backward " \t\f\v")
1764 (progn
1765 (setq simple-ws-beg (point))
1766 (skip-chars-backward " \t\n\r\f\v")))
1768 (setq rung-is-marked
1769 (text-property-any (point) rung-pos
1770 'c-is-sws t)))
1772 (goto-char simple-ws-beg)
1773 nil))
1775 ;; We'll loop here if there is simple ws before the first rung.
1776 ;; That means that there's been some change in it and it's
1777 ;; possible that we've stepped into another ladder, so extend
1778 ;; the previous one to join with it if there is one, and try to
1779 ;; use the cache again.
1780 (c-debug-sws-msg
1781 "c-backward-sws extending rung with [%s..%s] (min %s)"
1782 rung-is-marked rung-pos (point-min))
1783 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1784 ;; Remove any `c-in-sws' property from the last char of
1785 ;; the rung before we mark it with `c-is-sws', so that we
1786 ;; won't connect with the remains of a broken "ladder".
1787 (c-remove-in-sws (1- rung-pos) rung-pos))
1788 (c-put-is-sws rung-is-marked
1789 rung-pos)
1790 (c-put-in-sws rung-is-marked
1791 (1- rung-pos))
1792 (setq rung-pos rung-is-marked
1793 last-put-in-sws-pos rung-pos))
1795 (c-backward-comments)
1796 (setq cmt-skip-pos (point))
1798 (cond
1799 ((and c-opt-cpp-prefix
1800 (/= cmt-skip-pos simple-ws-beg)
1801 (c-beginning-of-macro))
1802 ;; Inside a cpp directive. See if it should be skipped over.
1803 (let ((cpp-beg (point)))
1805 ;; Move back over all line continuations in the region skipped
1806 ;; over by `c-backward-comments'. If we go past it then we
1807 ;; started inside the cpp directive.
1808 (goto-char simple-ws-beg)
1809 (beginning-of-line)
1810 (while (and (> (point) cmt-skip-pos)
1811 (progn (backward-char)
1812 (eq (char-before) ?\\)))
1813 (beginning-of-line))
1815 (if (< (point) cmt-skip-pos)
1816 ;; Don't move past the cpp directive if we began inside
1817 ;; it. Note that the position at the end of the last line
1818 ;; of the macro is also considered to be within it.
1819 (progn (goto-char cmt-skip-pos)
1820 nil)
1822 ;; It's worthwhile to spend a little bit of effort on finding
1823 ;; the end of the macro, to get a good `simple-ws-beg'
1824 ;; position for the cache. Note that `c-backward-comments'
1825 ;; could have stepped over some comments before going into
1826 ;; the macro, and then `simple-ws-beg' must be kept on the
1827 ;; same side of those comments.
1828 (goto-char simple-ws-beg)
1829 (skip-chars-backward " \t\n\r\f\v")
1830 (if (eq (char-before) ?\\)
1831 (forward-char))
1832 (forward-line 1)
1833 (if (< (point) simple-ws-beg)
1834 ;; Might happen if comments after the macro were skipped
1835 ;; over.
1836 (setq simple-ws-beg (point)))
1838 (goto-char cpp-beg)
1839 t)))
1841 ((/= (save-excursion
1842 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1843 (setq next-rung-pos (point)))
1844 simple-ws-beg)
1845 ;; Skipped over comments. Must put point at the end of
1846 ;; the simple ws at point since we might be after a line
1847 ;; comment or cpp directive that's been partially
1848 ;; narrowed out, and we can't risk marking the simple ws
1849 ;; at the end of it.
1850 (goto-char next-rung-pos)
1851 t)))
1853 ;; We've searched over a piece of non-white syntactic ws. See if this
1854 ;; can be cached.
1855 (setq next-rung-pos (point))
1856 (skip-chars-backward " \t\f\v")
1858 (if (or
1859 ;; Cache if we started either from a marked rung or from a
1860 ;; completely uncached position.
1861 rung-is-marked
1862 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1864 ;; Cache if there's a marked rung in the encountered simple ws.
1865 (save-excursion
1866 (skip-chars-backward " \t\n\r\f\v")
1867 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1868 'c-is-sws t)))
1870 (progn
1871 (c-debug-sws-msg
1872 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1873 (point) (1+ next-rung-pos)
1874 simple-ws-beg (min (1+ rung-pos) (point-max))
1875 (point-min))
1877 ;; Remove the properties for any nested ws that might be cached.
1878 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1879 ;; anyway.
1880 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1881 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1882 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1883 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1884 ;; Remove any `c-in-sws' property from the last char of
1885 ;; the rung before we mark it with `c-is-sws', so that we
1886 ;; won't connect with the remains of a broken "ladder".
1887 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1888 (c-put-is-sws simple-ws-beg
1889 rung-end-pos)
1890 (setq rung-is-marked t)))
1891 (c-put-in-sws (setq simple-ws-beg (point)
1892 last-put-in-sws-pos simple-ws-beg)
1893 rung-pos)
1894 (c-put-is-sws (setq rung-pos simple-ws-beg)
1895 (1+ next-rung-pos)))
1897 (c-debug-sws-msg
1898 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1899 (point) (1+ next-rung-pos)
1900 simple-ws-beg (min (1+ rung-pos) (point-max))
1901 (point-min))
1902 (setq rung-pos next-rung-pos
1903 simple-ws-beg (point))
1906 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1907 ;; another one before the point (which might occur when editing inside a
1908 ;; comment or macro).
1909 (when (eq last-put-in-sws-pos (point))
1910 (cond ((< (point-min) last-put-in-sws-pos)
1911 (c-debug-sws-msg
1912 "c-backward-sws clearing at %s for cache separation"
1913 (1- last-put-in-sws-pos))
1914 (c-remove-in-sws (1- last-put-in-sws-pos)
1915 last-put-in-sws-pos))
1916 ((> (point-min) 1)
1917 ;; If at bob and the buffer is narrowed, we have to clear the
1918 ;; character we're standing on instead since there might be a
1919 ;; `c-in-sws' before (point-min). In this case it's necessary
1920 ;; to clear both properties.
1921 (c-debug-sws-msg
1922 "c-backward-sws clearing thoroughly at %s for cache separation"
1923 last-put-in-sws-pos)
1924 (c-remove-is-and-in-sws last-put-in-sws-pos
1925 (1+ last-put-in-sws-pos)))))
1929 ;; Other whitespace tools
1930 (defun c-partial-ws-p (beg end)
1931 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1932 ;; region? This is a "heuristic" function. .....
1934 ;; The motivation for the second bit is to check whether removing this
1935 ;; region would coalesce two symbols.
1937 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1938 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1939 (save-excursion
1940 (let ((end+1 (min (1+ end) (point-max))))
1941 (or (progn (goto-char (max (point-min) (1- beg)))
1942 (c-skip-ws-forward end)
1943 (eq (point) end))
1944 (progn (goto-char beg)
1945 (c-skip-ws-forward end+1)
1946 (eq (point) end+1))))))
1948 ;; A system for finding noteworthy parens before the point.
1950 (defvar c-state-cache nil)
1951 (make-variable-buffer-local 'c-state-cache)
1952 ;; The state cache used by `c-parse-state' to cut down the amount of
1953 ;; searching. It's the result from some earlier `c-parse-state' call.
1955 ;; The use of the cached info is more effective if the next
1956 ;; `c-parse-state' call is on a line close by the one the cached state
1957 ;; was made at; the cache can actually slow down a little if the
1958 ;; cached state was made very far back in the buffer. The cache is
1959 ;; most effective if `c-parse-state' is used on each line while moving
1960 ;; forward.
1962 (defvar c-state-cache-start 1)
1963 (make-variable-buffer-local 'c-state-cache-start)
1964 ;; This is (point-min) when `c-state-cache' was calculated, since a
1965 ;; change of narrowing is likely to affect the parens that are visible
1966 ;; before the point.
1968 (defvar c-state-cache-good-pos 1)
1969 (make-variable-buffer-local 'c-state-cache-good-pos)
1970 ;; This is a position where `c-state-cache' is known to be correct.
1971 ;; It's a position inside one of the recorded unclosed parens or the
1972 ;; top level, but not further nested inside any literal or subparen
1973 ;; that is closed before the last recorded position.
1975 ;; The exact position is chosen to try to be close to yet earlier than
1976 ;; the position where `c-state-cache' will be called next. Right now
1977 ;; the heuristic is to set it to the position after the last found
1978 ;; closing paren (of any type) before the line on which
1979 ;; `c-parse-state' was called. That is chosen primarily to work well
1980 ;; with refontification of the current line.
1982 (defsubst c-invalidate-state-cache (pos)
1983 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1984 ;; at POS or higher. This is much like `c-whack-state-after', but
1985 ;; it never changes a paren pair element into an open paren element.
1986 ;; Doing that would mean that the new open paren wouldn't have the
1987 ;; required preceding paren pair element.
1988 (while (and (or c-state-cache
1989 (when (< pos c-state-cache-good-pos)
1990 (setq c-state-cache-good-pos 1)
1991 nil))
1992 (let ((elem (car c-state-cache)))
1993 (if (consp elem)
1994 (or (< pos (cdr elem))
1995 (when (< pos c-state-cache-good-pos)
1996 (setq c-state-cache-good-pos (cdr elem))
1997 nil))
1998 (or (<= pos elem)
1999 (when (< pos c-state-cache-good-pos)
2000 (setq c-state-cache-good-pos (1+ elem))
2001 nil)))))
2002 (setq c-state-cache (cdr c-state-cache))))
2004 (defun c-get-fallback-start-pos (here)
2005 ;; Return the start position for building `c-state-cache' from
2006 ;; scratch.
2007 (save-excursion
2008 ;; Go back 2 bods, but ignore any bogus positions returned by
2009 ;; beginning-of-defun (i.e. open paren in column zero).
2010 (goto-char here)
2011 (let ((cnt 2))
2012 (while (not (or (bobp) (zerop cnt)))
2013 (c-beginning-of-defun-1)
2014 (if (eq (char-after) ?\{)
2015 (setq cnt (1- cnt)))))
2016 (point)))
2018 (defun c-parse-state ()
2019 ;; Find and record all noteworthy parens between some good point
2020 ;; earlier in the file and point. That good point is at least the
2021 ;; beginning of the top-level construct we are in, or the beginning
2022 ;; of the preceding top-level construct if we aren't in one.
2024 ;; The returned value is a list of the noteworthy parens with the
2025 ;; last one first. If an element in the list is an integer, it's
2026 ;; the position of an open paren which has not been closed before
2027 ;; the point. If an element is a cons, it gives the position of a
2028 ;; closed brace paren pair; the car is the start paren position and
2029 ;; the cdr is the position following the closing paren. Only the
2030 ;; last closed brace paren pair before each open paren and before
2031 ;; the point is recorded, and thus the state never contains two cons
2032 ;; elements in succession.
2034 ;; Currently no characters which are given paren syntax with the
2035 ;; syntax-table property are recorded, i.e. angle bracket arglist
2036 ;; parens are never present here. Note that this might change.
2038 ;; BUG: This function doesn't cope entirely well with unbalanced
2039 ;; parens in macros. E.g. in the following case the brace before
2040 ;; the macro isn't balanced with the one after it:
2042 ;; {
2043 ;; #define X {
2044 ;; }
2046 ;; This function might do hidden buffer changes.
2048 (save-restriction
2049 (let* ((here (point))
2050 (here-bol (c-point 'bol))
2051 (c-macro-start (c-query-macro-start))
2052 (in-macro-start (or c-macro-start (point)))
2053 old-state last-pos brace-pair-open brace-pair-close
2054 pos save-pos)
2055 (c-invalidate-state-cache here)
2057 ;; If the minimum position has changed due to narrowing then we
2058 ;; have to fix the tail of `c-state-cache' accordingly.
2059 (unless (= c-state-cache-start (point-min))
2060 (if (> (point-min) c-state-cache-start)
2061 ;; If point-min has moved forward then we just need to cut
2062 ;; off a bit of the tail.
2063 (let ((ptr (cons nil c-state-cache)) elem)
2064 (while (and (setq elem (car-safe (cdr ptr)))
2065 (>= (if (consp elem) (car elem) elem)
2066 (point-min)))
2067 (setq ptr (cdr ptr)))
2068 (when (consp ptr)
2069 (if (eq (cdr ptr) c-state-cache)
2070 (setq c-state-cache nil
2071 c-state-cache-good-pos 1)
2072 (setcdr ptr nil))))
2073 ;; If point-min has moved backward then we drop the state
2074 ;; completely. It's possible to do a better job here and
2075 ;; recalculate the top only.
2076 (setq c-state-cache nil
2077 c-state-cache-good-pos 1))
2078 (setq c-state-cache-start (point-min)))
2080 ;; Get the latest position we know are directly inside the
2081 ;; closest containing paren of the cached state.
2082 (setq last-pos (and c-state-cache
2083 (if (consp (car c-state-cache))
2084 (cdr (car c-state-cache))
2085 (1+ (car c-state-cache)))))
2086 (if (or (not last-pos)
2087 (< last-pos c-state-cache-good-pos))
2088 (setq last-pos c-state-cache-good-pos)
2089 ;; Take the opportunity to move the cached good position
2090 ;; further down.
2091 (if (< last-pos here-bol)
2092 (setq c-state-cache-good-pos last-pos)))
2094 ;; Check if `last-pos' is in a macro. If it is, and we're not
2095 ;; in the same macro, we must discard everything on
2096 ;; `c-state-cache' that is inside the macro before using it.
2097 (save-excursion
2098 (goto-char last-pos)
2099 (when (and (c-beginning-of-macro)
2100 (/= (point) in-macro-start))
2101 (c-invalidate-state-cache (point))
2102 ;; Set `last-pos' again just like above except that there's
2103 ;; no use looking at `c-state-cache-good-pos' here.
2104 (setq last-pos (if c-state-cache
2105 (if (consp (car c-state-cache))
2106 (cdr (car c-state-cache))
2107 (1+ (car c-state-cache)))
2108 1))))
2110 ;; If we've moved very far from the last cached position then
2111 ;; it's probably better to redo it from scratch, otherwise we
2112 ;; might spend a lot of time searching from `last-pos' down to
2113 ;; here.
2114 (when (< last-pos (- here 20000))
2115 ;; First get the fallback start position. If it turns out
2116 ;; that it's so far back that the cached state is closer then
2117 ;; we'll keep it afterall.
2118 (setq pos (c-get-fallback-start-pos here))
2119 (if (<= pos last-pos)
2120 (setq pos nil)
2121 (setq last-pos nil
2122 c-state-cache nil
2123 c-state-cache-good-pos 1)))
2125 ;; Find the start position for the forward search. (Can't
2126 ;; search in the backward direction since the point might be in
2127 ;; some kind of literal.)
2129 (unless pos
2130 (setq old-state c-state-cache)
2132 ;; There's a cached state with a containing paren. Pop off
2133 ;; the stale containing sexps from it by going forward out of
2134 ;; parens as far as possible.
2135 (narrow-to-region (point-min) here)
2136 (let (placeholder pair-beg)
2137 (while (and c-state-cache
2138 (setq placeholder
2139 (c-up-list-forward last-pos)))
2140 (setq last-pos placeholder)
2141 (if (consp (car c-state-cache))
2142 (setq pair-beg (car-safe (cdr c-state-cache))
2143 c-state-cache (cdr-safe (cdr c-state-cache)))
2144 (setq pair-beg (car c-state-cache)
2145 c-state-cache (cdr c-state-cache))))
2147 (when (and pair-beg (eq (char-after pair-beg) ?{))
2148 ;; The last paren pair we moved out from was a brace
2149 ;; pair. Modify the state to record this as a closed
2150 ;; pair now.
2151 (if (consp (car-safe c-state-cache))
2152 (setq c-state-cache (cdr c-state-cache)))
2153 (setq c-state-cache (cons (cons pair-beg last-pos)
2154 c-state-cache))))
2156 ;; Check if the preceding balanced paren is within a
2157 ;; macro; it should be ignored if we're outside the
2158 ;; macro. There's no need to check any further upwards;
2159 ;; if the macro contains an unbalanced opening paren then
2160 ;; we're smoked anyway.
2161 (when (and (<= (point) in-macro-start)
2162 (consp (car c-state-cache)))
2163 (save-excursion
2164 (goto-char (car (car c-state-cache)))
2165 (when (c-beginning-of-macro)
2166 (setq here (point)
2167 c-state-cache (cdr c-state-cache)))))
2169 (unless (eq c-state-cache old-state)
2170 ;; Have to adjust the cached good position if state has been
2171 ;; popped off.
2172 (setq c-state-cache-good-pos
2173 (if c-state-cache
2174 (if (consp (car c-state-cache))
2175 (cdr (car c-state-cache))
2176 (1+ (car c-state-cache)))
2178 old-state c-state-cache))
2180 (when c-state-cache
2181 (setq pos last-pos)))
2183 ;; Get the fallback start position.
2184 (unless pos
2185 (setq pos (c-get-fallback-start-pos here)
2186 c-state-cache nil
2187 c-state-cache-good-pos 1))
2189 (narrow-to-region (point-min) here)
2191 (while pos
2192 (setq save-pos pos
2193 brace-pair-open nil)
2195 ;; Find the balanced brace pairs. This loop is hot, so it
2196 ;; does ugly tricks to go faster.
2197 (c-safe
2198 (let (set-good-pos set-brace-pair)
2199 (while t
2200 (setq last-pos nil
2201 last-pos (scan-lists pos 1 -1)) ; Might signal.
2202 (setq pos (scan-lists last-pos 1 1) ; Might signal.
2203 set-good-pos (< pos here-bol)
2204 set-brace-pair (eq (char-before last-pos) ?{))
2206 ;; Update the cached good position and record the brace
2207 ;; pair, whichever is applicable for the paren we've
2208 ;; just jumped over. But first check that it isn't
2209 ;; inside a macro and the point isn't inside the same
2210 ;; one.
2211 (when (and (or set-good-pos set-brace-pair)
2212 (or (>= pos in-macro-start)
2213 (save-excursion
2214 (goto-char pos)
2215 (not (c-beginning-of-macro)))))
2216 (if set-good-pos
2217 (setq c-state-cache-good-pos pos))
2218 (if set-brace-pair
2219 (setq brace-pair-open last-pos
2220 brace-pair-close pos))))))
2222 ;; Record the last brace pair.
2223 (when brace-pair-open
2224 (let ((head (car-safe c-state-cache)))
2225 (if (consp head)
2226 (progn
2227 (setcar head (1- brace-pair-open))
2228 (setcdr head brace-pair-close))
2229 (setq c-state-cache (cons (cons (1- brace-pair-open)
2230 brace-pair-close)
2231 c-state-cache)))))
2233 (if last-pos
2234 ;; Prepare to loop, but record the open paren only if it's
2235 ;; outside a macro or within the same macro as point, and
2236 ;; if it is a legitimate open paren and not some character
2237 ;; that got an open paren syntax-table property.
2238 (progn
2239 (setq pos last-pos)
2240 (when (and (or (>= last-pos in-macro-start)
2241 (save-excursion
2242 (goto-char last-pos)
2243 (not (c-beginning-of-macro))))
2244 ;; Check for known types of parens that we
2245 ;; want to record. The syntax table is not to
2246 ;; be trusted here since the caller might be
2247 ;; using e.g. `c++-template-syntax-table'.
2248 (memq (char-before last-pos) '(?{ ?\( ?\[)))
2249 (if (< last-pos here-bol)
2250 (setq c-state-cache-good-pos last-pos))
2251 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
2253 (if (setq last-pos (c-up-list-forward pos))
2254 ;; Found a close paren without a corresponding opening
2255 ;; one. Maybe we didn't go back far enough, so try to
2256 ;; scan backward for the start paren and then start over.
2257 (progn
2258 (setq pos (c-up-list-backward pos)
2259 c-state-cache nil
2260 c-state-cache-good-pos c-state-cache-start)
2261 (when (or (not pos)
2262 ;; Emacs (up to at least 21.2) can get confused by
2263 ;; open parens in column zero inside comments: The
2264 ;; sexp functions can then misbehave and bring us
2265 ;; back to the same point again. Check this so that
2266 ;; we don't get an infinite loop.
2267 (>= pos save-pos))
2268 (setq pos last-pos
2269 c-parsing-error
2270 (format "Unbalanced close paren at line %d"
2271 (1+ (count-lines (point-min)
2272 (c-point 'bol last-pos)))))))
2273 (setq pos nil))))
2275 ;;(message "c-parse-state: %S end: %S" c-state-cache c-state-cache-good-pos)
2276 c-state-cache)))
2278 ;; Debug tool to catch cache inconsistencies.
2279 (defvar c-debug-parse-state nil)
2280 (unless (fboundp 'c-real-parse-state)
2281 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
2282 (cc-bytecomp-defun c-real-parse-state)
2283 (defun c-debug-parse-state ()
2284 (let ((res1 (c-real-parse-state)) res2)
2285 (let ((c-state-cache nil)
2286 (c-state-cache-start 1)
2287 (c-state-cache-good-pos 1))
2288 (setq res2 (c-real-parse-state)))
2289 (unless (equal res1 res2)
2290 ;; The cache can actually go further back due to the ad-hoc way
2291 ;; the first paren is found, so try to whack off a bit of its
2292 ;; start before complaining.
2293 (save-excursion
2294 (goto-char (or (c-least-enclosing-brace res2) (point)))
2295 (c-beginning-of-defun-1)
2296 (while (not (or (bobp) (eq (char-after) ?{)))
2297 (c-beginning-of-defun-1))
2298 (unless (equal (c-whack-state-before (point) res1) res2)
2299 (message (concat "c-parse-state inconsistency: "
2300 "using cache: %s, from scratch: %s")
2301 res1 res2))))
2302 res1))
2303 (defun c-toggle-parse-state-debug (&optional arg)
2304 (interactive "P")
2305 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
2306 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
2307 'c-debug-parse-state
2308 'c-real-parse-state)))
2309 (c-keep-region-active))
2310 (when c-debug-parse-state
2311 (c-toggle-parse-state-debug 1))
2313 (defun c-whack-state-before (bufpos paren-state)
2314 ;; Whack off any state information from PAREN-STATE which lies
2315 ;; before BUFPOS. Not destructive on PAREN-STATE.
2316 (let* ((newstate (list nil))
2317 (ptr newstate)
2318 car)
2319 (while paren-state
2320 (setq car (car paren-state)
2321 paren-state (cdr paren-state))
2322 (if (< (if (consp car) (car car) car) bufpos)
2323 (setq paren-state nil)
2324 (setcdr ptr (list car))
2325 (setq ptr (cdr ptr))))
2326 (cdr newstate)))
2328 (defun c-whack-state-after (bufpos paren-state)
2329 ;; Whack off any state information from PAREN-STATE which lies at or
2330 ;; after BUFPOS. Not destructive on PAREN-STATE.
2331 (catch 'done
2332 (while paren-state
2333 (let ((car (car paren-state)))
2334 (if (consp car)
2335 ;; just check the car, because in a balanced brace
2336 ;; expression, it must be impossible for the corresponding
2337 ;; close brace to be before point, but the open brace to
2338 ;; be after.
2339 (if (<= bufpos (car car))
2340 nil ; whack it off
2341 (if (< bufpos (cdr car))
2342 ;; its possible that the open brace is before
2343 ;; bufpos, but the close brace is after. In that
2344 ;; case, convert this to a non-cons element. The
2345 ;; rest of the state is before bufpos, so we're
2346 ;; done.
2347 (throw 'done (cons (car car) (cdr paren-state)))
2348 ;; we know that both the open and close braces are
2349 ;; before bufpos, so we also know that everything else
2350 ;; on state is before bufpos.
2351 (throw 'done paren-state)))
2352 (if (<= bufpos car)
2353 nil ; whack it off
2354 ;; it's before bufpos, so everything else should too.
2355 (throw 'done paren-state)))
2356 (setq paren-state (cdr paren-state)))
2357 nil)))
2359 (defun c-most-enclosing-brace (paren-state &optional bufpos)
2360 ;; Return the bufpos of the innermost enclosing open paren before
2361 ;; bufpos, or nil if none was found.
2362 (let (enclosingp)
2363 (or bufpos (setq bufpos 134217727))
2364 (while paren-state
2365 (setq enclosingp (car paren-state)
2366 paren-state (cdr paren-state))
2367 (if (or (consp enclosingp)
2368 (>= enclosingp bufpos))
2369 (setq enclosingp nil)
2370 (setq paren-state nil)))
2371 enclosingp))
2373 (defun c-least-enclosing-brace (paren-state)
2374 ;; Return the bufpos of the outermost enclosing open paren, or nil
2375 ;; if none was found.
2376 (let (pos elem)
2377 (while paren-state
2378 (setq elem (car paren-state)
2379 paren-state (cdr paren-state))
2380 (if (integerp elem)
2381 (setq pos elem)))
2382 pos))
2384 (defun c-safe-position (bufpos paren-state)
2385 ;; Return the closest "safe" position recorded on PAREN-STATE that
2386 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
2387 ;; contain any. Return nil if BUFPOS is nil, which is useful to
2388 ;; find the closest limit before a given limit that might be nil.
2390 ;; A "safe" position is a position at or after a recorded open
2391 ;; paren, or after a recorded close paren. The returned position is
2392 ;; thus either the first position after a close brace, or the first
2393 ;; position after an enclosing paren, or at the enclosing paren in
2394 ;; case BUFPOS is immediately after it.
2395 (when bufpos
2396 (let (elem)
2397 (catch 'done
2398 (while paren-state
2399 (setq elem (car paren-state))
2400 (if (consp elem)
2401 (cond ((< (cdr elem) bufpos)
2402 (throw 'done (cdr elem)))
2403 ((< (car elem) bufpos)
2404 ;; See below.
2405 (throw 'done (min (1+ (car elem)) bufpos))))
2406 (if (< elem bufpos)
2407 ;; elem is the position at and not after the opening paren, so
2408 ;; we can go forward one more step unless it's equal to
2409 ;; bufpos. This is useful in some cases avoid an extra paren
2410 ;; level between the safe position and bufpos.
2411 (throw 'done (min (1+ elem) bufpos))))
2412 (setq paren-state (cdr paren-state)))))))
2414 (defun c-beginning-of-syntax ()
2415 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2416 ;; goes to the closest previous point that is known to be outside
2417 ;; any string literal or comment. `c-state-cache' is used if it has
2418 ;; a position in the vicinity.
2419 (let* ((paren-state c-state-cache)
2420 elem
2422 (pos (catch 'done
2423 ;; Note: Similar code in `c-safe-position'. The
2424 ;; difference is that we accept a safe position at
2425 ;; the point and don't bother to go forward past open
2426 ;; parens.
2427 (while paren-state
2428 (setq elem (car paren-state))
2429 (if (consp elem)
2430 (cond ((<= (cdr elem) (point))
2431 (throw 'done (cdr elem)))
2432 ((<= (car elem) (point))
2433 (throw 'done (car elem))))
2434 (if (<= elem (point))
2435 (throw 'done elem)))
2436 (setq paren-state (cdr paren-state)))
2437 (point-min))))
2439 (if (> pos (- (point) 4000))
2440 (goto-char pos)
2441 ;; The position is far back. Try `c-beginning-of-defun-1'
2442 ;; (although we can't be entirely sure it will go to a position
2443 ;; outside a comment or string in current emacsen). FIXME:
2444 ;; Consult `syntax-ppss' here.
2445 (c-beginning-of-defun-1)
2446 (if (< (point) pos)
2447 (goto-char pos)))))
2450 ;; Tools for scanning identifiers and other tokens.
2452 (defun c-on-identifier ()
2453 "Return non-nil if the point is on or directly after an identifier.
2454 Keywords are recognized and not considered identifiers. If an
2455 identifier is detected, the returned value is its starting position.
2456 If an identifier ends at the point and another begins at it \(can only
2457 happen in Pike) then the point for the preceding one is returned.
2459 Note that this function might do hidden buffer changes. See the
2460 comment at the start of cc-engine.el for more info."
2462 ;; FIXME: Shouldn't this function handle "operator" in C++?
2464 (save-excursion
2465 (skip-syntax-backward "w_")
2469 ;; Check for a normal (non-keyword) identifier.
2470 (and (looking-at c-symbol-start)
2471 (not (looking-at c-keywords-regexp))
2472 (point))
2474 (when (c-major-mode-is 'pike-mode)
2475 ;; Handle the `<operator> syntax in Pike.
2476 (let ((pos (point)))
2477 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2478 (and (if (< (skip-chars-backward "`") 0)
2480 (goto-char pos)
2481 (eq (char-after) ?\`))
2482 (looking-at c-symbol-key)
2483 (>= (match-end 0) pos)
2484 (point))))
2486 ;; Handle the "operator +" syntax in C++.
2487 (when (and c-overloadable-operators-regexp
2488 (= (c-backward-token-2 0) 0))
2490 (cond ((and (looking-at c-overloadable-operators-regexp)
2491 (or (not c-opt-op-identifier-prefix)
2492 (and (= (c-backward-token-2 1) 0)
2493 (looking-at c-opt-op-identifier-prefix))))
2494 (point))
2496 ((save-excursion
2497 (and c-opt-op-identifier-prefix
2498 (looking-at c-opt-op-identifier-prefix)
2499 (= (c-forward-token-2 1) 0)
2500 (looking-at c-overloadable-operators-regexp)))
2501 (point))))
2505 (defsubst c-simple-skip-symbol-backward ()
2506 ;; If the point is at the end of a symbol then skip backward to the
2507 ;; beginning of it. Don't move otherwise. Return non-nil if point
2508 ;; moved.
2510 ;; This function might do hidden buffer changes.
2511 (or (< (skip-syntax-backward "w_") 0)
2512 (and (c-major-mode-is 'pike-mode)
2513 ;; Handle the `<operator> syntax in Pike.
2514 (let ((pos (point)))
2515 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2516 (< (skip-chars-backward "`") 0)
2517 (looking-at c-symbol-key)
2518 (>= (match-end 0) pos))
2520 (goto-char pos)
2521 nil)))))
2523 (defun c-beginning-of-current-token (&optional back-limit)
2524 ;; Move to the beginning of the current token. Do not move if not
2525 ;; in the middle of one. BACK-LIMIT may be used to bound the
2526 ;; backward search; if given it's assumed to be at the boundary
2527 ;; between two tokens. Return non-nil if the point is moved, nil
2528 ;; otherwise.
2530 ;; This function might do hidden buffer changes.
2531 (let ((start (point)))
2532 (if (looking-at "\\w\\|\\s_")
2533 (skip-syntax-backward "w_" back-limit)
2534 (when (< (skip-syntax-backward ".()" back-limit) 0)
2535 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2536 (match-end 0))
2537 ;; `c-nonsymbol-token-regexp' should always match
2538 ;; since we've skipped backward over punctuator
2539 ;; or paren syntax, but consume one char in case
2540 ;; it doesn't so that we don't leave point before
2541 ;; some earlier incorrect token.
2542 (1+ (point)))))
2543 (if (<= pos start)
2544 (goto-char pos))))))
2545 (< (point) start)))
2547 (defun c-end-of-current-token (&optional back-limit)
2548 ;; Move to the end of the current token. Do not move if not in the
2549 ;; middle of one. BACK-LIMIT may be used to bound the backward
2550 ;; search; if given it's assumed to be at the boundary between two
2551 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2553 ;; This function might do hidden buffer changes.
2554 (let ((start (point)))
2555 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2556 (skip-syntax-forward "w_"))
2557 ((< (skip-syntax-backward ".()" back-limit) 0)
2558 (while (progn
2559 (if (looking-at c-nonsymbol-token-regexp)
2560 (goto-char (match-end 0))
2561 ;; `c-nonsymbol-token-regexp' should always match since
2562 ;; we've skipped backward over punctuator or paren
2563 ;; syntax, but move forward in case it doesn't so that
2564 ;; we don't leave point earlier than we started with.
2565 (forward-char))
2566 (< (point) start)))))
2567 (> (point) start)))
2569 (defconst c-jump-syntax-balanced
2570 (if (memq 'gen-string-delim c-emacs-features)
2571 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2572 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2574 (defconst c-jump-syntax-unbalanced
2575 (if (memq 'gen-string-delim c-emacs-features)
2576 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2577 "\\w\\|\\s_\\|\\s\""))
2579 (defun c-forward-token-2 (&optional count balanced limit)
2580 "Move forward by tokens.
2581 A token is defined as all symbols and identifiers which aren't
2582 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2583 treated properly). Point is always either left at the beginning of a
2584 token or not moved at all. COUNT specifies the number of tokens to
2585 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2586 moves to the next token beginning only if not already at one. If
2587 BALANCED is true, move over balanced parens, otherwise move into them.
2588 Also, if BALANCED is true, never move out of an enclosing paren.
2590 LIMIT sets the limit for the movement and defaults to the point limit.
2591 The case when LIMIT is set in the middle of a token, comment or macro
2592 is handled correctly, i.e. the point won't be left there.
2594 Return the number of tokens left to move \(positive or negative). If
2595 BALANCED is true, a move over a balanced paren counts as one. Note
2596 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2597 be returned. Thus, a return value of 0 guarantees that point is at
2598 the requested position and a return value less \(without signs) than
2599 COUNT guarantees that point is at the beginning of some token.
2601 Note that this function might do hidden buffer changes. See the
2602 comment at the start of cc-engine.el for more info."
2604 (or count (setq count 1))
2605 (if (< count 0)
2606 (- (c-backward-token-2 (- count) balanced limit))
2608 (let ((jump-syntax (if balanced
2609 c-jump-syntax-balanced
2610 c-jump-syntax-unbalanced))
2611 (last (point))
2612 (prev (point)))
2614 (if (zerop count)
2615 ;; If count is zero we should jump if in the middle of a token.
2616 (c-end-of-current-token))
2618 (save-restriction
2619 (if limit (narrow-to-region (point-min) limit))
2620 (if (/= (point)
2621 (progn (c-forward-syntactic-ws) (point)))
2622 ;; Skip whitespace. Count this as a move if we did in
2623 ;; fact move.
2624 (setq count (max (1- count) 0)))
2626 (if (eobp)
2627 ;; Moved out of bounds. Make sure the returned count isn't zero.
2628 (progn
2629 (if (zerop count) (setq count 1))
2630 (goto-char last))
2632 ;; Use `condition-case' to avoid having the limit tests
2633 ;; inside the loop.
2634 (condition-case nil
2635 (while (and
2636 (> count 0)
2637 (progn
2638 (setq last (point))
2639 (cond ((looking-at jump-syntax)
2640 (goto-char (scan-sexps (point) 1))
2642 ((looking-at c-nonsymbol-token-regexp)
2643 (goto-char (match-end 0))
2645 ;; `c-nonsymbol-token-regexp' above should always
2646 ;; match if there are correct tokens. Try to
2647 ;; widen to see if the limit was set in the
2648 ;; middle of one, else fall back to treating
2649 ;; the offending thing as a one character token.
2650 ((and limit
2651 (save-restriction
2652 (widen)
2653 (looking-at c-nonsymbol-token-regexp)))
2654 nil)
2656 (forward-char)
2657 t))))
2658 (c-forward-syntactic-ws)
2659 (setq prev last
2660 count (1- count)))
2661 (error (goto-char last)))
2663 (when (eobp)
2664 (goto-char prev)
2665 (setq count (1+ count)))))
2667 count)))
2669 (defun c-backward-token-2 (&optional count balanced limit)
2670 "Move backward by tokens.
2671 See `c-forward-token-2' for details."
2673 (or count (setq count 1))
2674 (if (< count 0)
2675 (- (c-forward-token-2 (- count) balanced limit))
2677 (or limit (setq limit (point-min)))
2678 (let ((jump-syntax (if balanced
2679 c-jump-syntax-balanced
2680 c-jump-syntax-unbalanced))
2681 (last (point)))
2683 (if (zerop count)
2684 ;; The count is zero so try to skip to the beginning of the
2685 ;; current token.
2686 (if (> (point)
2687 (progn (c-beginning-of-current-token) (point)))
2688 (if (< (point) limit)
2689 ;; The limit is inside the same token, so return 1.
2690 (setq count 1))
2692 ;; We're not in the middle of a token. If there's
2693 ;; whitespace after the point then we must move backward,
2694 ;; so set count to 1 in that case.
2695 (and (looking-at c-syntactic-ws-start)
2696 ;; If we're looking at a '#' that might start a cpp
2697 ;; directive then we have to do a more elaborate check.
2698 (or (/= (char-after) ?#)
2699 (not c-opt-cpp-prefix)
2700 (save-excursion
2701 (and (= (point)
2702 (progn (beginning-of-line)
2703 (looking-at "[ \t]*")
2704 (match-end 0)))
2705 (or (bobp)
2706 (progn (backward-char)
2707 (not (eq (char-before) ?\\)))))))
2708 (setq count 1))))
2710 ;; Use `condition-case' to avoid having to check for buffer
2711 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2712 (condition-case nil
2713 (while (and
2714 (> count 0)
2715 (progn
2716 (c-backward-syntactic-ws)
2717 (backward-char)
2718 (if (looking-at jump-syntax)
2719 (goto-char (scan-sexps (1+ (point)) -1))
2720 ;; This can be very inefficient if there's a long
2721 ;; sequence of operator tokens without any separation.
2722 ;; That doesn't happen in practice, anyway.
2723 (c-beginning-of-current-token))
2724 (>= (point) limit)))
2725 (setq last (point)
2726 count (1- count)))
2727 (error (goto-char last)))
2729 (if (< (point) limit)
2730 (goto-char last))
2732 count)))
2734 (defun c-forward-token-1 (&optional count balanced limit)
2735 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2736 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2737 characters are jumped over character by character. This function is
2738 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2739 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2740 (c-forward-token-2 count balanced limit)))
2742 (defun c-backward-token-1 (&optional count balanced limit)
2743 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2744 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2745 characters are jumped over character by character. This function is
2746 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2747 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2748 (c-backward-token-2 count balanced limit)))
2751 ;; Tools for doing searches restricted to syntactically relevant text.
2753 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2754 paren-level not-inside-token
2755 lookbehind-submatch)
2756 "Like `re-search-forward', but only report matches that are found
2757 in syntactically significant text. I.e. matches in comments, macros
2758 or string literals are ignored. The start point is assumed to be
2759 outside any comment, macro or string literal, or else the content of
2760 that region is taken as syntactically significant text.
2762 If PAREN-LEVEL is non-nil, an additional restriction is added to
2763 ignore matches in nested paren sexps. The search will also not go
2764 outside the current list sexp, which has the effect that if the point
2765 should be moved to BOUND when no match is found \(i.e. NOERROR is
2766 neither nil nor t), then it will be at the closing paren if the end of
2767 the current list sexp is encountered first.
2769 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2770 ignored. Things like multicharacter operators and special symbols
2771 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2772 constants.
2774 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2775 subexpression in REGEXP. The end of that submatch is used as the
2776 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2777 isn't used or if that subexpression didn't match then the start
2778 position of the whole match is used instead. The \"look behind\"
2779 subexpression is never tested before the starting position, so it
2780 might be a good idea to include \\=\\= as a match alternative in it.
2782 Optimization note: Matches might be missed if the \"look behind\"
2783 subexpression can match the end of nonwhite syntactic whitespace,
2784 i.e. the end of comments or cpp directives. This since the function
2785 skips over such things before resuming the search. It's on the other
2786 hand not safe to assume that the \"look behind\" subexpression never
2787 matches syntactic whitespace.
2789 Bug: Unbalanced parens inside cpp directives are currently not handled
2790 correctly \(i.e. they don't get ignored as they should) when
2791 PAREN-LEVEL is set.
2793 Note that this function might do hidden buffer changes. See the
2794 comment at the start of cc-engine.el for more info."
2796 (or bound (setq bound (point-max)))
2797 (if paren-level (setq paren-level -1))
2799 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2801 (let ((start (point))
2803 ;; Start position for the last search.
2804 search-pos
2805 ;; The `parse-partial-sexp' state between the start position
2806 ;; and the point.
2807 state
2808 ;; The current position after the last state update. The next
2809 ;; `parse-partial-sexp' continues from here.
2810 (state-pos (point))
2811 ;; The position at which to check the state and the state
2812 ;; there. This is separate from `state-pos' since we might
2813 ;; need to back up before doing the next search round.
2814 check-pos check-state
2815 ;; Last position known to end a token.
2816 (last-token-end-pos (point-min))
2817 ;; Set when a valid match is found.
2818 found)
2820 (condition-case err
2821 (while
2822 (and
2823 (progn
2824 (setq search-pos (point))
2825 (re-search-forward regexp bound noerror))
2827 (progn
2828 (setq state (parse-partial-sexp
2829 state-pos (match-beginning 0) paren-level nil state)
2830 state-pos (point))
2831 (if (setq check-pos (and lookbehind-submatch
2832 (or (not paren-level)
2833 (>= (car state) 0))
2834 (match-end lookbehind-submatch)))
2835 (setq check-state (parse-partial-sexp
2836 state-pos check-pos paren-level nil state))
2837 (setq check-pos state-pos
2838 check-state state))
2840 ;; NOTE: If we got a look behind subexpression and get
2841 ;; an insignificant match in something that isn't
2842 ;; syntactic whitespace (i.e. strings or in nested
2843 ;; parentheses), then we can never skip more than a
2844 ;; single character from the match start position
2845 ;; (i.e. `state-pos' here) before continuing the
2846 ;; search. That since the look behind subexpression
2847 ;; might match the end of the insignificant region in
2848 ;; the next search.
2850 (cond
2851 ((elt check-state 7)
2852 ;; Match inside a line comment. Skip to eol. Use
2853 ;; `re-search-forward' instead of `skip-chars-forward' to get
2854 ;; the right bound behavior.
2855 (re-search-forward "[\n\r]" bound noerror))
2857 ((elt check-state 4)
2858 ;; Match inside a block comment. Skip to the '*/'.
2859 (search-forward "*/" bound noerror))
2861 ((and (not (elt check-state 5))
2862 (eq (char-before check-pos) ?/)
2863 (not (c-get-char-property (1- check-pos) 'syntax-table))
2864 (memq (char-after check-pos) '(?/ ?*)))
2865 ;; Match in the middle of the opener of a block or line
2866 ;; comment.
2867 (if (= (char-after check-pos) ?/)
2868 (re-search-forward "[\n\r]" bound noerror)
2869 (search-forward "*/" bound noerror)))
2871 ;; The last `parse-partial-sexp' above might have
2872 ;; stopped short of the real check position if the end
2873 ;; of the current sexp was encountered in paren-level
2874 ;; mode. The checks above are always false in that
2875 ;; case, and since they can do better skipping in
2876 ;; lookbehind-submatch mode, we do them before
2877 ;; checking the paren level.
2879 ((and paren-level
2880 (/= (setq tmp (car check-state)) 0))
2881 ;; Check the paren level first since we're short of the
2882 ;; syntactic checking position if the end of the
2883 ;; current sexp was encountered by `parse-partial-sexp'.
2884 (if (> tmp 0)
2886 ;; Inside a nested paren sexp.
2887 (if lookbehind-submatch
2888 ;; See the NOTE above.
2889 (progn (goto-char state-pos) t)
2890 ;; Skip out of the paren quickly.
2891 (setq state (parse-partial-sexp state-pos bound 0 nil state)
2892 state-pos (point)))
2894 ;; Have exited the current paren sexp.
2895 (if noerror
2896 (progn
2897 ;; The last `parse-partial-sexp' call above
2898 ;; has left us just after the closing paren
2899 ;; in this case, so we can modify the bound
2900 ;; to leave the point at the right position
2901 ;; upon return.
2902 (setq bound (1- (point)))
2903 nil)
2904 (signal 'search-failed (list regexp)))))
2906 ((setq tmp (elt check-state 3))
2907 ;; Match inside a string.
2908 (if (or lookbehind-submatch
2909 (not (integerp tmp)))
2910 ;; See the NOTE above.
2911 (progn (goto-char state-pos) t)
2912 ;; Skip to the end of the string before continuing.
2913 (let ((ender (make-string 1 tmp)) (continue t))
2914 (while (if (search-forward ender bound noerror)
2915 (progn
2916 (setq state (parse-partial-sexp
2917 state-pos (point) nil nil state)
2918 state-pos (point))
2919 (elt state 3))
2920 (setq continue nil)))
2921 continue)))
2923 ((save-excursion
2924 (save-match-data
2925 (c-beginning-of-macro start)))
2926 ;; Match inside a macro. Skip to the end of it.
2927 (c-end-of-macro)
2928 (cond ((<= (point) bound) t)
2929 (noerror nil)
2930 (t (signal 'search-failed (list regexp)))))
2932 ((and not-inside-token
2933 (or (< check-pos last-token-end-pos)
2934 (< check-pos
2935 (save-excursion
2936 (goto-char check-pos)
2937 (save-match-data
2938 (c-end-of-current-token last-token-end-pos))
2939 (setq last-token-end-pos (point))))))
2940 ;; Inside a token.
2941 (if lookbehind-submatch
2942 ;; See the NOTE above.
2943 (goto-char state-pos)
2944 (goto-char (min last-token-end-pos bound))))
2947 ;; A real match.
2948 (setq found t)
2949 nil)))
2951 ;; Should loop to search again, but take care to avoid
2952 ;; looping on the same spot.
2953 (or (/= search-pos (point))
2954 (if (= (point) bound)
2955 (if noerror
2957 (signal 'search-failed (list regexp)))
2958 (forward-char)
2959 t))))
2961 (error
2962 (goto-char start)
2963 (signal (car err) (cdr err))))
2965 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2967 (if found
2968 (progn
2969 (goto-char (match-end 0))
2970 (match-end 0))
2972 ;; Search failed. Set point as appropriate.
2973 (if (eq noerror t)
2974 (goto-char start)
2975 (goto-char bound))
2976 nil)))
2978 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
2979 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2980 i.e. don't stop at positions inside syntactic whitespace or string
2981 literals. Preprocessor directives are also ignored, with the exception
2982 of the one that the point starts within, if any. If LIMIT is given,
2983 it's assumed to be at a syntactically relevant position.
2985 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
2986 sexps, and the search will also not go outside the current paren sexp.
2987 However, if LIMIT or the buffer limit is reached inside a nested paren
2988 then the point will be left at the limit.
2990 Non-nil is returned if the point moved, nil otherwise.
2992 Note that this function might do hidden buffer changes. See the
2993 comment at the start of cc-engine.el for more info."
2995 (let ((start (point))
2996 state
2997 ;; A list of syntactically relevant positions in descending
2998 ;; order. It's used to avoid scanning repeatedly over
2999 ;; potentially large regions with `parse-partial-sexp' to verify
3000 ;; each position.
3001 safe-pos-list
3002 ;; The position at the beginning of `safe-pos-list'.
3003 safe-pos
3004 ;; The result from `c-beginning-of-macro' at the start position or the
3005 ;; start position itself if it isn't within a macro. Evaluated on
3006 ;; demand.
3007 start-macro-beg
3008 ;; The earliest position after the current one with the same paren
3009 ;; level. Used only when `paren-level' is set.
3010 (paren-level-pos (point)))
3012 (while (progn
3013 (while (and
3014 (< (skip-chars-backward skip-chars limit) 0)
3016 ;; Use `parse-partial-sexp' from a safe position down to
3017 ;; the point to check if it's outside comments and
3018 ;; strings.
3019 (let ((pos (point)) state-2 pps-end-pos)
3020 ;; Pick a safe position as close to the point as
3021 ;; possible.
3023 ;; FIXME: Consult `syntax-ppss' here if our
3024 ;; cache doesn't give a good position.
3025 (while (and safe-pos-list
3026 (> (car safe-pos-list) (point)))
3027 (setq safe-pos-list (cdr safe-pos-list)))
3028 (unless (setq safe-pos (car-safe safe-pos-list))
3029 (setq safe-pos (max (or (c-safe-position
3030 (point) (or c-state-cache
3031 (c-parse-state)))
3033 (point-min))
3034 safe-pos-list (list safe-pos)))
3036 ;; Cache positions along the way to use if we have to
3037 ;; back up more. We cache every closing paren on the
3038 ;; same level. If the paren cache is relevant in this
3039 ;; region then we're typically already on the same
3040 ;; level as the target position. Note that we might
3041 ;; cache positions after opening parens in case
3042 ;; safe-pos is in a nested list. That's both uncommon
3043 ;; and harmless.
3044 (while (progn
3045 (setq state (parse-partial-sexp
3046 safe-pos pos 0))
3047 (< (point) pos))
3048 (setq safe-pos (point)
3049 safe-pos-list (cons safe-pos safe-pos-list)))
3051 (cond
3052 ((or (elt state 3) (elt state 4))
3053 ;; Inside string or comment. Continue search at the
3054 ;; beginning of it.
3055 (goto-char (elt state 8))
3058 ((and paren-level
3059 (save-excursion
3060 (setq state-2 (parse-partial-sexp
3061 pos paren-level-pos -1)
3062 pps-end-pos (point))
3063 (/= (car state-2) 0)))
3064 ;; Not at the right level.
3066 (if (and (< (car state-2) 0)
3067 ;; We stop above if we go out of a paren.
3068 ;; Now check whether it precedes or is
3069 ;; nested in the starting sexp.
3070 (save-excursion
3071 (setq state-2
3072 (parse-partial-sexp
3073 pps-end-pos paren-level-pos
3074 nil nil state-2))
3075 (< (car state-2) 0)))
3077 ;; We've stopped short of the starting position
3078 ;; so the hit was inside a nested list. Go up
3079 ;; until we are at the right level.
3080 (condition-case nil
3081 (progn
3082 (goto-char (scan-lists pos -1
3083 (- (car state-2))))
3084 (setq paren-level-pos (point))
3085 (if (and limit (>= limit paren-level-pos))
3086 (progn
3087 (goto-char limit)
3088 nil)
3090 (error
3091 (goto-char (or limit (point-min)))
3092 nil))
3094 ;; The hit was outside the list at the start
3095 ;; position. Go to the start of the list and exit.
3096 (goto-char (1+ (elt state-2 1)))
3097 nil))
3099 ((c-beginning-of-macro limit)
3100 ;; Inside a macro.
3101 (if (< (point)
3102 (or start-macro-beg
3103 (setq start-macro-beg
3104 (save-excursion
3105 (goto-char start)
3106 (c-beginning-of-macro limit)
3107 (point)))))
3110 ;; It's inside the same macro we started in so it's
3111 ;; a relevant match.
3112 (goto-char pos)
3113 nil)))))
3115 ;; If the state contains the start of the containing sexp we
3116 ;; cache that position too, so that parse-partial-sexp in the
3117 ;; next run has a bigger chance of starting at the same level
3118 ;; as the target position and thus will get more good safe
3119 ;; positions into the list.
3120 (if (elt state 1)
3121 (setq safe-pos (1+ (elt state 1))
3122 safe-pos-list (cons safe-pos safe-pos-list))))
3124 (> (point)
3125 (progn
3126 ;; Skip syntactic ws afterwards so that we don't stop at the
3127 ;; end of a comment if `skip-chars' is something like "^/".
3128 (c-backward-syntactic-ws)
3129 (point)))))
3131 ;; We might want to extend this with more useful return values in
3132 ;; the future.
3133 (/= (point) start)))
3135 ;; The following is an alternative implementation of
3136 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3137 ;; track of the syntactic context. It turned out to be generally
3138 ;; slower than the one above which uses forward checks from earlier
3139 ;; safe positions.
3141 ;;(defconst c-ssb-stop-re
3142 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3143 ;; ;; stop at to avoid going into comments and literals.
3144 ;; (concat
3145 ;; ;; Match comment end syntax and string literal syntax. Also match
3146 ;; ;; '/' for block comment endings (not covered by comment end
3147 ;; ;; syntax).
3148 ;; "\\s>\\|/\\|\\s\""
3149 ;; (if (memq 'gen-string-delim c-emacs-features)
3150 ;; "\\|\\s|"
3151 ;; "")
3152 ;; (if (memq 'gen-comment-delim c-emacs-features)
3153 ;; "\\|\\s!"
3154 ;; "")))
3156 ;;(defconst c-ssb-stop-paren-re
3157 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3158 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3160 ;;(defconst c-ssb-sexp-end-re
3161 ;; ;; Regexp matching the ending syntax of a complex sexp.
3162 ;; (concat c-string-limit-regexp "\\|\\s)"))
3164 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3165 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3166 ;;i.e. don't stop at positions inside syntactic whitespace or string
3167 ;;literals. Preprocessor directives are also ignored. However, if the
3168 ;;point is within a comment, string literal or preprocessor directory to
3169 ;;begin with, its contents is treated as syntactically relevant chars.
3170 ;;If LIMIT is given, it limits the backward search and the point will be
3171 ;;left there if no earlier position is found.
3173 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3174 ;;sexps, and the search will also not go outside the current paren sexp.
3175 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3176 ;;then the point will be left at the limit.
3178 ;;Non-nil is returned if the point moved, nil otherwise.
3180 ;;Note that this function might do hidden buffer changes. See the
3181 ;;comment at the start of cc-engine.el for more info."
3183 ;; (save-restriction
3184 ;; (when limit
3185 ;; (narrow-to-region limit (point-max)))
3187 ;; (let ((start (point)))
3188 ;; (catch 'done
3189 ;; (while (let ((last-pos (point))
3190 ;; (stop-pos (progn
3191 ;; (skip-chars-backward skip-chars)
3192 ;; (point))))
3194 ;; ;; Skip back over the same region as
3195 ;; ;; `skip-chars-backward' above, but keep to
3196 ;; ;; syntactically relevant positions.
3197 ;; (goto-char last-pos)
3198 ;; (while (and
3199 ;; ;; `re-search-backward' with a single char regexp
3200 ;; ;; should be fast.
3201 ;; (re-search-backward
3202 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3203 ;; stop-pos 'move)
3205 ;; (progn
3206 ;; (cond
3207 ;; ((looking-at "\\s(")
3208 ;; ;; `paren-level' is set and we've found the
3209 ;; ;; start of the containing paren.
3210 ;; (forward-char)
3211 ;; (throw 'done t))
3213 ;; ((looking-at c-ssb-sexp-end-re)
3214 ;; ;; We're at the end of a string literal or paren
3215 ;; ;; sexp (if `paren-level' is set).
3216 ;; (forward-char)
3217 ;; (condition-case nil
3218 ;; (c-backward-sexp)
3219 ;; (error
3220 ;; (goto-char limit)
3221 ;; (throw 'done t))))
3223 ;; (t
3224 ;; (forward-char)
3225 ;; ;; At the end of some syntactic ws or possibly
3226 ;; ;; after a plain '/' operator.
3227 ;; (let ((pos (point)))
3228 ;; (c-backward-syntactic-ws)
3229 ;; (if (= pos (point))
3230 ;; ;; Was a plain '/' operator. Go past it.
3231 ;; (backward-char)))))
3233 ;; (> (point) stop-pos))))
3235 ;; ;; Now the point is either at `stop-pos' or at some
3236 ;; ;; position further back if `stop-pos' was at a
3237 ;; ;; syntactically irrelevant place.
3239 ;; ;; Skip additional syntactic ws so that we don't stop
3240 ;; ;; at the end of a comment if `skip-chars' is
3241 ;; ;; something like "^/".
3242 ;; (c-backward-syntactic-ws)
3244 ;; (< (point) stop-pos))))
3246 ;; ;; We might want to extend this with more useful return values
3247 ;; ;; in the future.
3248 ;; (/= (point) start))))
3251 ;; Tools for handling comments and string literals.
3253 (defun c-slow-in-literal (&optional lim detect-cpp)
3254 "Return the type of literal point is in, if any.
3255 The return value is `c' if in a C-style comment, `c++' if in a C++
3256 style comment, `string' if in a string literal, `pound' if DETECT-CPP
3257 is non-nil and in a preprocessor line, or nil if somewhere else.
3258 Optional LIM is used as the backward limit of the search. If omitted,
3259 or nil, `c-beginning-of-defun' is used.
3261 The last point calculated is cached if the cache is enabled, i.e. if
3262 `c-in-literal-cache' is bound to a two element vector.
3264 Note that this function might do hidden buffer changes. See the
3265 comment at the start of cc-engine.el for more info."
3267 (if (and (vectorp c-in-literal-cache)
3268 (= (point) (aref c-in-literal-cache 0)))
3269 (aref c-in-literal-cache 1)
3270 (let ((rtn (save-excursion
3271 (let* ((pos (point))
3272 (lim (or lim (progn
3273 (c-beginning-of-syntax)
3274 (point))))
3275 (state (parse-partial-sexp lim pos)))
3276 (cond
3277 ((elt state 3) 'string)
3278 ((elt state 4) (if (elt state 7) 'c++ 'c))
3279 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
3280 (t nil))))))
3281 ;; cache this result if the cache is enabled
3282 (if (not c-in-literal-cache)
3283 (setq c-in-literal-cache (vector (point) rtn)))
3284 rtn)))
3286 ;; XEmacs has a built-in function that should make this much quicker.
3287 ;; I don't think we even need the cache, which makes our lives more
3288 ;; complicated anyway. In this case, lim is only used to detect
3289 ;; cpp directives.
3291 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
3292 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
3293 ;; Xemacs 21.4.4. It manifested itself thus:
3295 ;; Starting with an empty AWK Mode buffer, type
3296 ;; /regexp/ {<C-j>
3297 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
3299 ;; AWK Mode is designed such that when the first / is typed, it gets the
3300 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
3301 ;; are given the s-t property "string". However, buffer-syntactic-context
3302 ;; fails to take account of the change of the s-t property on the opening / to
3303 ;; "string", and reports that the { is within a string started by the second /.
3305 ;; The workaround for this is for the AWK Mode initialisation to switch the
3306 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
3307 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
3309 ;; (Alan Mackenzie, 2003/4/30).
3311 (defun c-fast-in-literal (&optional lim detect-cpp)
3312 ;; This function might do hidden buffer changes.
3313 (let ((context (buffer-syntactic-context)))
3314 (cond
3315 ((eq context 'string) 'string)
3316 ((eq context 'comment) 'c++)
3317 ((eq context 'block-comment) 'c)
3318 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
3320 (defalias 'c-in-literal
3321 (if (fboundp 'buffer-syntactic-context)
3322 'c-fast-in-literal ; XEmacs
3323 'c-slow-in-literal)) ; GNU Emacs
3325 ;; The defalias above isn't enough to shut up the byte compiler.
3326 (cc-bytecomp-defun c-in-literal)
3328 (defun c-literal-limits (&optional lim near not-in-delimiter)
3329 "Return a cons of the beginning and end positions of the comment or
3330 string surrounding point (including both delimiters), or nil if point
3331 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
3332 to start parsing from. If NEAR is non-nil, then the limits of any
3333 literal next to point is returned. \"Next to\" means there's only
3334 spaces and tabs between point and the literal. The search for such a
3335 literal is done first in forward direction. If NOT-IN-DELIMITER is
3336 non-nil, the case when point is inside a starting delimiter won't be
3337 recognized. This only has effect for comments which have starting
3338 delimiters with more than one character.
3340 Note that this function might do hidden buffer changes. See the
3341 comment at the start of cc-engine.el for more info."
3343 (save-excursion
3344 (let* ((pos (point))
3345 (lim (or lim (progn
3346 (c-beginning-of-syntax)
3347 (point))))
3348 (state (parse-partial-sexp lim pos)))
3350 (cond ((elt state 3) ; String.
3351 (goto-char (elt state 8))
3352 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3353 (point-max))))
3355 ((elt state 4) ; Comment.
3356 (goto-char (elt state 8))
3357 (cons (point) (progn (c-forward-single-comment) (point))))
3359 ((and (not not-in-delimiter)
3360 (not (elt state 5))
3361 (eq (char-before) ?/)
3362 (looking-at "[/*]"))
3363 ;; We're standing in a comment starter.
3364 (backward-char 1)
3365 (cons (point) (progn (c-forward-single-comment) (point))))
3367 (near
3368 (goto-char pos)
3370 ;; Search forward for a literal.
3371 (skip-chars-forward " \t")
3373 (cond
3374 ((looking-at c-string-limit-regexp) ; String.
3375 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3376 (point-max))))
3378 ((looking-at c-comment-start-regexp) ; Line or block comment.
3379 (cons (point) (progn (c-forward-single-comment) (point))))
3382 ;; Search backward.
3383 (skip-chars-backward " \t")
3385 (let ((end (point)) beg)
3386 (cond
3387 ((save-excursion
3388 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
3389 (setq beg (c-safe (c-backward-sexp 1) (point))))
3391 ((and (c-safe (forward-char -2) t)
3392 (looking-at "*/"))
3393 ;; Block comment. Due to the nature of line
3394 ;; comments, they will always be covered by the
3395 ;; normal case above.
3396 (goto-char end)
3397 (c-backward-single-comment)
3398 ;; If LIM is bogus, beg will be bogus.
3399 (setq beg (point))))
3401 (if beg (cons beg end))))))
3402 ))))
3404 ;; In case external callers use this; it did have a docstring.
3405 (defalias 'c-literal-limits-fast 'c-literal-limits)
3407 (defun c-collect-line-comments (range)
3408 "If the argument is a cons of two buffer positions (such as returned by
3409 `c-literal-limits'), and that range contains a C++ style line comment,
3410 then an extended range is returned that contains all adjacent line
3411 comments (i.e. all comments that starts in the same column with no
3412 empty lines or non-whitespace characters between them). Otherwise the
3413 argument is returned.
3415 Note that this function might do hidden buffer changes. See the
3416 comment at the start of cc-engine.el for more info."
3418 (save-excursion
3419 (condition-case nil
3420 (if (and (consp range) (progn
3421 (goto-char (car range))
3422 (looking-at c-line-comment-starter)))
3423 (let ((col (current-column))
3424 (beg (point))
3425 (bopl (c-point 'bopl))
3426 (end (cdr range)))
3427 ;; Got to take care in the backward direction to handle
3428 ;; comments which are preceded by code.
3429 (while (and (c-backward-single-comment)
3430 (>= (point) bopl)
3431 (looking-at c-line-comment-starter)
3432 (= col (current-column)))
3433 (setq beg (point)
3434 bopl (c-point 'bopl)))
3435 (goto-char end)
3436 (while (and (progn (skip-chars-forward " \t")
3437 (looking-at c-line-comment-starter))
3438 (= col (current-column))
3439 (prog1 (zerop (forward-line 1))
3440 (setq end (point)))))
3441 (cons beg end))
3442 range)
3443 (error range))))
3445 (defun c-literal-type (range)
3446 "Convenience function that given the result of `c-literal-limits',
3447 returns nil or the type of literal that the range surrounds, one
3448 of the symbols 'c, 'c++ or 'string. It's much faster than using
3449 `c-in-literal' and is intended to be used when you need both the
3450 type of a literal and its limits.
3452 Note that this function might do hidden buffer changes. See the
3453 comment at the start of cc-engine.el for more info."
3455 (if (consp range)
3456 (save-excursion
3457 (goto-char (car range))
3458 (cond ((looking-at c-string-limit-regexp) 'string)
3459 ((or (looking-at "//") ; c++ line comment
3460 (and (looking-at "\\s<") ; comment starter
3461 (looking-at "#"))) ; awk comment.
3462 'c++)
3463 (t 'c))) ; Assuming the range is valid.
3464 range))
3467 ;; `c-find-decl-spots' and accompanying stuff.
3469 ;; Variables used in `c-find-decl-spots' to cache the search done for
3470 ;; the first declaration in the last call. When that function starts,
3471 ;; it needs to back up over syntactic whitespace to look at the last
3472 ;; token before the region being searched. That can sometimes cause
3473 ;; moves back and forth over a quite large region of comments and
3474 ;; macros, which would be repeated for each changed character when
3475 ;; we're called during fontification, since font-lock refontifies the
3476 ;; current line for each change. Thus it's worthwhile to cache the
3477 ;; first match.
3479 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3480 ;; the syntactic whitespace less or equal to some start position.
3481 ;; There's no cached value if it's nil.
3483 ;; `c-find-decl-match-pos' is the match position if
3484 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3485 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3486 (defvar c-find-decl-syntactic-pos nil)
3487 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
3488 (defvar c-find-decl-match-pos nil)
3489 (make-variable-buffer-local 'c-find-decl-match-pos)
3491 (defsubst c-invalidate-find-decl-cache (change-min-pos)
3492 (and c-find-decl-syntactic-pos
3493 (< change-min-pos c-find-decl-syntactic-pos)
3494 (setq c-find-decl-syntactic-pos nil)))
3496 ; (defface c-debug-decl-spot-face
3497 ; '((t (:background "Turquoise")))
3498 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3499 ; (defface c-debug-decl-sws-face
3500 ; '((t (:background "Khaki")))
3501 ; "Debug face to mark the syntactic whitespace between the declaration
3502 ; spots and the preceding token end.")
3504 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
3505 (when (facep 'c-debug-decl-spot-face)
3506 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
3507 (c-debug-add-face (max match-pos (point-min)) decl-pos
3508 'c-debug-decl-sws-face)
3509 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
3510 'c-debug-decl-spot-face))))
3511 (defmacro c-debug-remove-decl-spot-faces (beg end)
3512 (when (facep 'c-debug-decl-spot-face)
3513 `(c-save-buffer-state ()
3514 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
3515 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
3517 (defmacro c-find-decl-prefix-search ()
3518 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3519 ;; but it contains lots of free variables that refer to things
3520 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3521 ;; if there is a match, otherwise at `cfd-limit'.
3523 ;; This macro might do hidden buffer changes.
3525 '(progn
3526 ;; Find the next property match position if we haven't got one already.
3527 (unless cfd-prop-match
3528 (save-excursion
3529 (while (progn
3530 (goto-char (next-single-property-change
3531 (point) 'c-type nil cfd-limit))
3532 (and (< (point) cfd-limit)
3533 (not (eq (c-get-char-property (1- (point)) 'c-type)
3534 'c-decl-end)))))
3535 (setq cfd-prop-match (point))))
3537 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
3538 ;; got one already.
3539 (unless cfd-re-match
3541 (if (> cfd-re-match-end (point))
3542 (goto-char cfd-re-match-end))
3544 (while (if (setq cfd-re-match-end
3545 (re-search-forward c-decl-prefix-or-start-re
3546 cfd-limit 'move))
3548 ;; Match. Check if it's inside a comment or string literal.
3549 (c-got-face-at
3550 (if (setq cfd-re-match (match-end 1))
3551 ;; Matched the end of a token preceding a decl spot.
3552 (progn
3553 (goto-char cfd-re-match)
3554 (1- cfd-re-match))
3555 ;; Matched a token that start a decl spot.
3556 (goto-char (match-beginning 0))
3557 (point))
3558 c-literal-faces)
3560 ;; No match. Finish up and exit the loop.
3561 (setq cfd-re-match cfd-limit)
3562 nil)
3564 ;; Skip out of comments and string literals.
3565 (while (progn
3566 (goto-char (next-single-property-change
3567 (point) 'face nil cfd-limit))
3568 (and (< (point) cfd-limit)
3569 (c-got-face-at (point) c-literal-faces)))))
3571 ;; If we matched at the decl start, we have to back up over the
3572 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
3573 ;; any decl spots in the syntactic ws.
3574 (unless cfd-re-match
3575 (c-backward-syntactic-ws)
3576 (setq cfd-re-match (point))))
3578 ;; Choose whichever match is closer to the start.
3579 (if (< cfd-re-match cfd-prop-match)
3580 (setq cfd-match-pos cfd-re-match
3581 cfd-re-match nil)
3582 (setq cfd-match-pos cfd-prop-match
3583 cfd-prop-match nil))
3585 (goto-char cfd-match-pos)
3587 (when (< cfd-match-pos cfd-limit)
3588 ;; Skip forward past comments only so we don't skip macros.
3589 (c-forward-comments)
3590 ;; Set the position to continue at. We can avoid going over
3591 ;; the comments skipped above a second time, but it's possible
3592 ;; that the comment skipping has taken us past `cfd-prop-match'
3593 ;; since the property might be used inside comments.
3594 (setq cfd-continue-pos (if cfd-prop-match
3595 (min cfd-prop-match (point))
3596 (point))))))
3598 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3599 ;; Call CFD-FUN for each possible spot for a declaration, cast or
3600 ;; label from the point to CFD-LIMIT.
3602 ;; CFD-FUN is called with point at the start of the spot. It's
3603 ;; passed two arguments: The first is the end position of the token
3604 ;; preceding the spot, or 0 for the implicit match at bob. The
3605 ;; second is a flag that is t when the match is inside a macro. If
3606 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
3607 ;; spot, it should return non-nil to ensure that the next search
3608 ;; will find them.
3610 ;; Such a spot is:
3611 ;; o The first token after bob.
3612 ;; o The first token after the end of submatch 1 in
3613 ;; `c-decl-prefix-or-start-re' when that submatch matches.
3614 ;; o The start of each `c-decl-prefix-or-start-re' match when
3615 ;; submatch 1 doesn't match.
3616 ;; o The first token after the end of each occurence of the
3617 ;; `c-type' text property with the value `c-decl-end', provided
3618 ;; `c-type-decl-end-used' is set.
3620 ;; Only a spot that match CFD-DECL-RE and whose face is in the
3621 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
3622 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
3624 ;; If the match is inside a macro then the buffer is narrowed to the
3625 ;; end of it, so that CFD-FUN can investigate the following tokens
3626 ;; without matching something that begins inside a macro and ends
3627 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3628 ;; CFD-FACE-CHECKLIST checks exist.
3630 ;; The spots are visited approximately in order from top to bottom.
3631 ;; It's however the positions where `c-decl-prefix-or-start-re'
3632 ;; matches and where `c-decl-end' properties are found that are in
3633 ;; order. Since the spots often are at the following token, they
3634 ;; might be visited out of order insofar as more spots are reported
3635 ;; later on within the syntactic whitespace between the match
3636 ;; positions and their spots.
3638 ;; It's assumed that comments and strings are fontified in the
3639 ;; searched range.
3641 ;; This is mainly used in fontification, and so has an elaborate
3642 ;; cache to handle repeated calls from the same start position; see
3643 ;; the variables above.
3645 ;; All variables in this function begin with `cfd-' to avoid name
3646 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3648 ;; This function might do hidden buffer changes.
3650 (let ((cfd-start-pos (point))
3651 (cfd-buffer-end (point-max))
3652 ;; The end of the token preceding the decl spot last found
3653 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
3654 ;; no match.
3655 cfd-re-match
3656 ;; The end position of the last `c-decl-prefix-or-start-re'
3657 ;; match. If this is greater than `cfd-continue-pos', the
3658 ;; next regexp search is started here instead.
3659 (cfd-re-match-end (point-min))
3660 ;; The end of the last `c-decl-end' found by
3661 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
3662 ;; match. If searching for the property isn't needed then we
3663 ;; disable it by setting it to `cfd-limit' directly.
3664 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3665 ;; The end of the token preceding the decl spot last found by
3666 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
3667 ;; bob. `cfd-limit' if there's no match. In other words,
3668 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
3669 (cfd-match-pos cfd-limit)
3670 ;; The position to continue searching at.
3671 cfd-continue-pos
3672 ;; The position of the last "real" token we've stopped at.
3673 ;; This can be greater than `cfd-continue-pos' when we get
3674 ;; hits inside macros or at `c-decl-end' positions inside
3675 ;; comments.
3676 (cfd-token-pos 0)
3677 ;; The end position of the last entered macro.
3678 (cfd-macro-end 0))
3680 ;; Initialize by finding a syntactically relevant start position
3681 ;; before the point, and do the first `c-decl-prefix-or-start-re'
3682 ;; search unless we're at bob.
3684 (let (start-in-literal start-in-macro syntactic-pos)
3685 ;; Must back up a bit since we look for the end of the previous
3686 ;; statement or declaration, which is earlier than the first
3687 ;; returned match.
3689 (cond
3690 ;; First we need to move to a syntactically relevant position.
3691 ;; Begin by backing out of comment or string literals.
3692 ((and
3693 (when (c-got-face-at (point) c-literal-faces)
3694 ;; Try to use the faces to back up to the start of the
3695 ;; literal. FIXME: What if the point is on a declaration
3696 ;; inside a comment?
3697 (while (and (not (bobp))
3698 (c-got-face-at (1- (point)) c-literal-faces))
3699 (goto-char (previous-single-property-change
3700 (point) 'face nil (point-min))))
3702 ;; XEmacs doesn't fontify the quotes surrounding string
3703 ;; literals.
3704 (and (featurep 'xemacs)
3705 (eq (get-text-property (point) 'face)
3706 'font-lock-string-face)
3707 (not (bobp))
3708 (progn (backward-char)
3709 (not (looking-at c-string-limit-regexp)))
3710 (forward-char))
3712 ;; Don't trust the literal to contain only literal faces
3713 ;; (the font lock package might not have fontified the
3714 ;; start of it at all, for instance) so check that we have
3715 ;; arrived at something that looks like a start or else
3716 ;; resort to `c-literal-limits'.
3717 (unless (looking-at c-literal-start-regexp)
3718 (let ((range (c-literal-limits)))
3719 (if range (goto-char (car range)))))
3721 (setq start-in-literal (point)))
3723 ;; The start is in a literal. If the limit is in the same
3724 ;; one we don't have to find a syntactic position etc. We
3725 ;; only check that if the limit is at or before bonl to save
3726 ;; time; it covers the by far most common case when font-lock
3727 ;; refontifies the current line only.
3728 (<= cfd-limit (c-point 'bonl cfd-start-pos))
3729 (save-excursion
3730 (goto-char cfd-start-pos)
3731 (while (progn
3732 (goto-char (next-single-property-change
3733 (point) 'face nil cfd-limit))
3734 (and (< (point) cfd-limit)
3735 (c-got-face-at (point) c-literal-faces))))
3736 (= (point) cfd-limit)))
3738 ;; Completely inside a literal. Set up variables to trig the
3739 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
3740 ;; find a suitable start position.
3741 (setq cfd-continue-pos start-in-literal))
3743 ;; Check if the region might be completely inside a macro, to
3744 ;; optimize that like the completely-inside-literal above.
3745 ((save-excursion
3746 (and (= (forward-line 1) 0)
3747 (bolp) ; forward-line has funny behavior at eob.
3748 (>= (point) cfd-limit)
3749 (progn (backward-char)
3750 (eq (char-before) ?\\))))
3751 ;; (Maybe) completely inside a macro. Only need to trig the
3752 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
3753 ;; set things up.
3754 (setq cfd-continue-pos (1- cfd-start-pos)
3755 start-in-macro t))
3758 ;; Back out of any macro so we don't miss any declaration
3759 ;; that could follow after it.
3760 (when (c-beginning-of-macro)
3761 (setq start-in-macro t))
3763 ;; Now we're at a proper syntactically relevant position so we
3764 ;; can use the cache. But first clear it if it applied
3765 ;; further down.
3766 (c-invalidate-find-decl-cache cfd-start-pos)
3768 (setq syntactic-pos (point))
3769 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
3770 ;; Don't have to do this if the cache is relevant here,
3771 ;; typically if the same line is refontified again. If
3772 ;; we're just some syntactic whitespace further down we can
3773 ;; still use the cache to limit the skipping.
3774 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
3776 ;; If we hit `c-find-decl-syntactic-pos' and
3777 ;; `c-find-decl-match-pos' is set then we install the cached
3778 ;; values. If we hit `c-find-decl-syntactic-pos' and
3779 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3780 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3781 ;; and so we can continue the search from this point. If we
3782 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
3783 ;; the right spot to begin searching anyway.
3784 (if (and (eq (point) c-find-decl-syntactic-pos)
3785 c-find-decl-match-pos)
3786 (setq cfd-match-pos c-find-decl-match-pos
3787 cfd-continue-pos syntactic-pos)
3789 (setq c-find-decl-syntactic-pos syntactic-pos)
3791 (when (if (bobp)
3792 ;; Always consider bob a match to get the first
3793 ;; declaration in the file. Do this separately instead of
3794 ;; letting `c-decl-prefix-or-start-re' match bob, so that
3795 ;; regexp always can consume at least one character to
3796 ;; ensure that we won't get stuck in an infinite loop.
3797 (setq cfd-re-match 0)
3798 (backward-char)
3799 (c-beginning-of-current-token)
3800 (< (point) cfd-limit))
3801 ;; Do an initial search now. In the bob case above it's
3802 ;; only done to search for a `c-decl-end' spot.
3803 (c-find-decl-prefix-search))
3805 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
3806 cfd-match-pos)))))
3808 ;; Advance `cfd-continue-pos' if it's before the start position.
3809 ;; The closest continue position that might have effect at or
3810 ;; after the start depends on what we started in. This also
3811 ;; finds a suitable start position in the special cases when the
3812 ;; region is completely within a literal or macro.
3813 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
3815 (cond
3816 (start-in-macro
3817 ;; If we're in a macro then it's the closest preceding token
3818 ;; in the macro. Check this before `start-in-literal',
3819 ;; since if we're inside a literal in a macro, the preceding
3820 ;; token is earlier than any `c-decl-end' spot inside the
3821 ;; literal (comment).
3822 (goto-char (or start-in-literal cfd-start-pos))
3823 ;; The only syntactic ws in macros are comments.
3824 (c-backward-comments)
3825 (backward-char)
3826 (c-beginning-of-current-token))
3828 (start-in-literal
3829 ;; If we're in a comment it can only be the closest
3830 ;; preceding `c-decl-end' position within that comment, if
3831 ;; any. Go back to the beginning of such a property so that
3832 ;; `c-find-decl-prefix-search' will find the end of it.
3833 ;; (Can't stop at the end and install it directly on
3834 ;; `cfd-prop-match' since that variable might be cleared
3835 ;; after `cfd-fun' below.)
3837 ;; Note that if the literal is a string then the property
3838 ;; search will simply skip to the beginning of it right
3839 ;; away.
3840 (if (not c-type-decl-end-used)
3841 (goto-char start-in-literal)
3842 (goto-char cfd-start-pos)
3843 (while (progn
3844 (goto-char (previous-single-property-change
3845 (point) 'c-type nil start-in-literal))
3846 (and (> (point) start-in-literal)
3847 (not (eq (c-get-char-property (point) 'c-type)
3848 'c-decl-end))))))
3850 (when (= (point) start-in-literal)
3851 ;; Didn't find any property inside the comment, so we can
3852 ;; skip it entirely. (This won't skip past a string, but
3853 ;; that'll be handled quickly by the next
3854 ;; `c-find-decl-prefix-search' anyway.)
3855 (c-forward-single-comment)
3856 (if (> (point) cfd-limit)
3857 (goto-char cfd-limit))))
3860 ;; If we started in normal code, the only match that might
3861 ;; apply before the start is what we already got in
3862 ;; `cfd-match-pos' so we can continue at the start position.
3863 ;; (Note that we don't get here if the first match is below
3864 ;; it.)
3865 (goto-char cfd-start-pos)))
3867 ;; Delete found matches if they are before our new continue
3868 ;; position, so that `c-find-decl-prefix-search' won't back up
3869 ;; to them later on.
3870 (setq cfd-continue-pos (point))
3871 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
3872 (setq cfd-re-match nil))
3873 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
3874 (setq cfd-prop-match nil)))
3876 (if syntactic-pos
3877 ;; This is the normal case and we got a proper syntactic
3878 ;; position. If there's a match then it's always outside
3879 ;; macros and comments, so advance to the next token and set
3880 ;; `cfd-token-pos'. The loop below will later go back using
3881 ;; `cfd-continue-pos' to fix declarations inside the
3882 ;; syntactic ws.
3883 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
3884 (goto-char syntactic-pos)
3885 (c-forward-syntactic-ws)
3886 (and cfd-continue-pos
3887 (< cfd-continue-pos (point))
3888 (setq cfd-token-pos (point))))
3890 ;; Have one of the special cases when the region is completely
3891 ;; within a literal or macro. `cfd-continue-pos' is set to a
3892 ;; good start position for the search, so do it.
3893 (c-find-decl-prefix-search)))
3895 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
3897 (while (progn
3898 (while (and
3899 (< cfd-match-pos cfd-limit)
3902 ;; Kludge to filter out matches on the "<" that
3903 ;; aren't open parens, for the sake of languages
3904 ;; that got `c-recognize-<>-arglists' set.
3905 (and (eq (char-before cfd-match-pos) ?<)
3906 (not (c-get-char-property (1- cfd-match-pos)
3907 'syntax-table)))
3909 ;; If `cfd-continue-pos' is less or equal to
3910 ;; `cfd-token-pos', we've got a hit inside a macro
3911 ;; that's in the syntactic whitespace before the last
3912 ;; "real" declaration we've checked. If they're equal
3913 ;; we've arrived at the declaration a second time, so
3914 ;; there's nothing to do.
3915 (= cfd-continue-pos cfd-token-pos)
3917 (progn
3918 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3919 ;; we're still searching for declarations embedded in
3920 ;; the syntactic whitespace. In that case we need
3921 ;; only to skip comments and not macros, since they
3922 ;; can't be nested, and that's already been done in
3923 ;; `c-find-decl-prefix-search'.
3924 (when (> cfd-continue-pos cfd-token-pos)
3925 (c-forward-syntactic-ws)
3926 (setq cfd-token-pos (point)))
3928 ;; Continue if the following token fails the
3929 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3930 (when (or (>= (point) cfd-limit)
3931 (not (looking-at cfd-decl-re))
3932 (and cfd-face-checklist
3933 (not (c-got-face-at
3934 (point) cfd-face-checklist))))
3935 (goto-char cfd-continue-pos)
3936 t)))
3938 (< (point) cfd-limit))
3939 (c-find-decl-prefix-search))
3941 (< (point) cfd-limit))
3943 (when (and
3944 (>= (point) cfd-start-pos)
3946 (progn
3947 ;; Narrow to the end of the macro if we got a hit inside
3948 ;; one, to avoid recognizing things that start inside the
3949 ;; macro and end outside it.
3950 (when (> cfd-match-pos cfd-macro-end)
3951 ;; Not in the same macro as in the previous round.
3952 (save-excursion
3953 (goto-char cfd-match-pos)
3954 (setq cfd-macro-end
3955 (if (save-excursion (and (c-beginning-of-macro)
3956 (< (point) cfd-match-pos)))
3957 (progn (c-end-of-macro)
3958 (point))
3959 0))))
3961 (if (zerop cfd-macro-end)
3963 (if (> cfd-macro-end (point))
3964 (progn (narrow-to-region (point-min) cfd-macro-end)
3966 ;; The matched token was the last thing in the macro,
3967 ;; so the whole match is bogus.
3968 (setq cfd-macro-end 0)
3969 nil))))
3971 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3972 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3973 (setq cfd-prop-match nil))
3975 (when (/= cfd-macro-end 0)
3976 ;; Restore limits if we did macro narrowment above.
3977 (narrow-to-region (point-min) cfd-buffer-end)))
3979 (goto-char cfd-continue-pos)
3980 (if (= cfd-continue-pos cfd-limit)
3981 (setq cfd-match-pos cfd-limit)
3982 (c-find-decl-prefix-search)))))
3985 ;; A cache for found types.
3987 ;; Buffer local variable that contains an obarray with the types we've
3988 ;; found. If a declaration is recognized somewhere we record the
3989 ;; fully qualified identifier in it to recognize it as a type
3990 ;; elsewhere in the file too. This is not accurate since we do not
3991 ;; bother with the scoping rules of the languages, but in practice the
3992 ;; same name is seldom used as both a type and something else in a
3993 ;; file, and we only use this as a last resort in ambiguous cases (see
3994 ;; `c-forward-decl-or-cast-1').
3996 ;; Not every type need be in this cache. However, things which have
3997 ;; ceased to be types must be removed from it.
3999 ;; Template types in C++ are added here too but with the template
4000 ;; arglist replaced with "<>" in references or "<" for the one in the
4001 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4002 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4003 ;; template specs can be fairly sized programs in themselves) and
4004 ;; improves the hit ratio (it's a type regardless of the template
4005 ;; args; it's just not the same type, but we're only interested in
4006 ;; recognizing types, not telling distinct types apart). Note that
4007 ;; template types in references are added here too; from the example
4008 ;; above there will also be an entry "Foo<".
4009 (defvar c-found-types nil)
4010 (make-variable-buffer-local 'c-found-types)
4012 (defsubst c-clear-found-types ()
4013 ;; Clears `c-found-types'.
4014 (setq c-found-types (make-vector 53 0)))
4016 (defun c-add-type (from to)
4017 ;; Add the given region as a type in `c-found-types'. If the region
4018 ;; doesn't match an existing type but there is a type which is equal
4019 ;; to the given one except that the last character is missing, then
4020 ;; the shorter type is removed. That's done to avoid adding all
4021 ;; prefixes of a type as it's being entered and font locked. This
4022 ;; doesn't cover cases like when characters are removed from a type
4023 ;; or added in the middle. We'd need the position of point when the
4024 ;; font locking is invoked to solve this well.
4026 ;; This function might do hidden buffer changes.
4027 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4028 (unless (intern-soft type c-found-types)
4029 (unintern (substring type 0 -1) c-found-types)
4030 (intern type c-found-types))))
4032 (defun c-unfind-type (name)
4033 ;; Remove the "NAME" from c-found-types, if present.
4034 (unintern name c-found-types))
4036 (defsubst c-check-type (from to)
4037 ;; Return non-nil if the given region contains a type in
4038 ;; `c-found-types'.
4040 ;; This function might do hidden buffer changes.
4041 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4042 c-found-types))
4044 (defun c-list-found-types ()
4045 ;; Return all the types in `c-found-types' as a sorted list of
4046 ;; strings.
4047 (let (type-list)
4048 (mapatoms (lambda (type)
4049 (setq type-list (cons (symbol-name type)
4050 type-list)))
4051 c-found-types)
4052 (sort type-list 'string-lessp)))
4054 ;; Shut up the byte compiler.
4055 (defvar c-maybe-stale-found-type)
4057 (defun c-trim-found-types (beg end old-len)
4058 ;; An after change function which, in conjunction with the info in
4059 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4060 ;; from `c-found-types', should this type have become stale. For
4061 ;; example, this happens to "foo" when "foo \n bar();" becomes
4062 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4063 ;; the fontification.
4065 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4066 ;; type?
4067 (when (> end beg)
4068 (save-excursion
4069 (when (< end (point-max))
4070 (goto-char end)
4071 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4072 (progn (goto-char end)
4073 (c-end-of-current-token)))
4074 (c-unfind-type (buffer-substring-no-properties
4075 end (point)))))
4076 (when (> beg (point-min))
4077 (goto-char beg)
4078 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4079 (progn (goto-char beg)
4080 (c-beginning-of-current-token)))
4081 (c-unfind-type (buffer-substring-no-properties
4082 (point) beg))))))
4084 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4085 (cond
4086 ;; Changing the amount of (already existing) whitespace - don't do anything.
4087 ((and (c-partial-ws-p beg end)
4088 (or (= beg end) ; removal of WS
4089 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4091 ;; The syntactic relationship which defined a "found type" has been
4092 ;; destroyed.
4093 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4094 (c-unfind-type (cadr c-maybe-stale-found-type)))
4095 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4099 ;; Handling of small scale constructs like types and names.
4101 (defun c-after-change-check-<>-operators (beg end)
4102 ;; This is called from `after-change-functions' when
4103 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
4104 ;; chars with paren syntax become part of another operator like "<<"
4105 ;; or ">=".
4107 ;; This function might do hidden buffer changes.
4109 (save-excursion
4110 (goto-char beg)
4111 (when (or (looking-at "[<>]")
4112 (< (skip-chars-backward "<>") 0))
4114 (goto-char beg)
4115 (c-beginning-of-current-token)
4116 (when (and (< (point) beg)
4117 (looking-at c-<>-multichar-token-regexp)
4118 (< beg (setq beg (match-end 0))))
4119 (while (progn (skip-chars-forward "^<>" beg)
4120 (< (point) beg))
4121 (c-clear-char-property (point) 'syntax-table)
4122 (forward-char))))
4124 (when (< beg end)
4125 (goto-char end)
4126 (when (or (looking-at "[<>]")
4127 (< (skip-chars-backward "<>") 0))
4129 (goto-char end)
4130 (c-beginning-of-current-token)
4131 (when (and (< (point) end)
4132 (looking-at c-<>-multichar-token-regexp)
4133 (< end (setq end (match-end 0))))
4134 (while (progn (skip-chars-forward "^<>" end)
4135 (< (point) end))
4136 (c-clear-char-property (point) 'syntax-table)
4137 (forward-char)))))))
4139 ;; Dynamically bound variable that instructs `c-forward-type' to also
4140 ;; treat possible types (i.e. those that it normally returns 'maybe or
4141 ;; 'found for) as actual types (and always return 'found for them).
4142 ;; This means that it records them in `c-record-type-identifiers' if
4143 ;; that is set, and that it adds them to `c-found-types'.
4144 (defvar c-promote-possible-types nil)
4146 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4147 ;; mark up successfully parsed arglists with paren syntax properties on
4148 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
4149 ;; `c-type' property of each argument separating comma.
4151 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
4152 ;; all arglists for side effects (i.e. recording types), otherwise it
4153 ;; exploits any existing paren syntax properties to quickly jump to the
4154 ;; end of already parsed arglists.
4156 ;; Marking up the arglists is not the default since doing that correctly
4157 ;; depends on a proper value for `c-restricted-<>-arglists'.
4158 (defvar c-parse-and-markup-<>-arglists nil)
4160 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4161 ;; not accept arglists that contain binary operators.
4163 ;; This is primarily used to handle C++ template arglists. C++
4164 ;; disambiguates them by checking whether the preceding name is a
4165 ;; template or not. We can't do that, so we assume it is a template
4166 ;; if it can be parsed as one. That usually works well since
4167 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
4168 ;; in almost all cases would be pointless.
4170 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
4171 ;; should let the comma separate the function arguments instead. And
4172 ;; in a context where the value of the expression is taken, e.g. in
4173 ;; "if (a < b || c > d)", it's probably not a template.
4174 (defvar c-restricted-<>-arglists nil)
4176 ;; Dynamically bound variables that instructs
4177 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
4178 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
4179 ;; `c-forward-label' to record the ranges of all the type and
4180 ;; reference identifiers they encounter. They will build lists on
4181 ;; these variables where each element is a cons of the buffer
4182 ;; positions surrounding each identifier. This recording is only
4183 ;; activated when `c-record-type-identifiers' is non-nil.
4185 ;; All known types that can't be identifiers are recorded, and also
4186 ;; other possible types if `c-promote-possible-types' is set.
4187 ;; Recording is however disabled inside angle bracket arglists that
4188 ;; are encountered inside names and other angle bracket arglists.
4189 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
4190 ;; instead.
4192 ;; Only the names in C++ template style references (e.g. "tmpl" in
4193 ;; "tmpl<a,b>::foo") are recorded as references, other references
4194 ;; aren't handled here.
4196 ;; `c-forward-label' records the label identifier(s) on
4197 ;; `c-record-ref-identifiers'.
4198 (defvar c-record-type-identifiers nil)
4199 (defvar c-record-ref-identifiers nil)
4201 ;; This variable will receive a cons cell of the range of the last
4202 ;; single identifier symbol stepped over by `c-forward-name' if it's
4203 ;; successful. This is the range that should be put on one of the
4204 ;; record lists above by the caller. It's assigned nil if there's no
4205 ;; such symbol in the name.
4206 (defvar c-last-identifier-range nil)
4208 (defmacro c-record-type-id (range)
4209 (if (eq (car-safe range) 'cons)
4210 ;; Always true.
4211 `(setq c-record-type-identifiers
4212 (cons ,range c-record-type-identifiers))
4213 `(let ((range ,range))
4214 (if range
4215 (setq c-record-type-identifiers
4216 (cons range c-record-type-identifiers))))))
4218 (defmacro c-record-ref-id (range)
4219 (if (eq (car-safe range) 'cons)
4220 ;; Always true.
4221 `(setq c-record-ref-identifiers
4222 (cons ,range c-record-ref-identifiers))
4223 `(let ((range ,range))
4224 (if range
4225 (setq c-record-ref-identifiers
4226 (cons range c-record-ref-identifiers))))))
4228 ;; Dynamically bound variable that instructs `c-forward-type' to
4229 ;; record the ranges of types that only are found. Behaves otherwise
4230 ;; like `c-record-type-identifiers'.
4231 (defvar c-record-found-types nil)
4233 (defmacro c-forward-keyword-prefixed-id (type)
4234 ;; Used internally in `c-forward-keyword-clause' to move forward
4235 ;; over a type (if TYPE is 'type) or a name (otherwise) which
4236 ;; possibly is prefixed by keywords and their associated clauses.
4237 ;; Try with a type/name first to not trip up on those that begin
4238 ;; with a keyword. Return t if a known or found type is moved
4239 ;; over. The point is clobbered if nil is returned. If range
4240 ;; recording is enabled, the identifier is recorded on as a type
4241 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
4243 ;; This macro might do hidden buffer changes.
4244 `(let (res)
4245 (while (if (setq res ,(if (eq type 'type)
4246 `(c-forward-type)
4247 `(c-forward-name)))
4249 (and (looking-at c-keywords-regexp)
4250 (c-forward-keyword-clause 1))))
4251 (when (memq res '(t known found prefix))
4252 ,(when (eq type 'ref)
4253 `(when c-record-type-identifiers
4254 (c-record-ref-id c-last-identifier-range)))
4255 t)))
4257 (defmacro c-forward-id-comma-list (type update-safe-pos)
4258 ;; Used internally in `c-forward-keyword-clause' to move forward
4259 ;; over a comma separated list of types or names using
4260 ;; `c-forward-keyword-prefixed-id'.
4262 ;; This macro might do hidden buffer changes.
4263 `(while (and (progn
4264 ,(when update-safe-pos
4265 `(setq safe-pos (point)))
4266 (eq (char-after) ?,))
4267 (progn
4268 (forward-char)
4269 (c-forward-syntactic-ws)
4270 (c-forward-keyword-prefixed-id ,type)))))
4272 (defun c-forward-keyword-clause (match)
4273 ;; Submatch MATCH in the current match data is assumed to surround a
4274 ;; token. If it's a keyword, move over it and any immediately
4275 ;; following clauses associated with it, stopping at the start of
4276 ;; the next token. t is returned in that case, otherwise the point
4277 ;; stays and nil is returned. The kind of clauses that are
4278 ;; recognized are those specified by `c-type-list-kwds',
4279 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
4280 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
4281 ;; and `c-<>-arglist-kwds'.
4283 ;; This function records identifier ranges on
4284 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4285 ;; `c-record-type-identifiers' is non-nil.
4287 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
4288 ;; apply directly after the keyword, the type list is moved over
4289 ;; only when there is no unaccounted token before it (i.e. a token
4290 ;; that isn't moved over due to some other keyword list). The
4291 ;; identifier ranges in the list are still recorded if that should
4292 ;; be done, though.
4294 ;; This function might do hidden buffer changes.
4296 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
4297 ;; The call to `c-forward-<>-arglist' below is made after
4298 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
4299 ;; are angle bracket arglists and `c-restricted-<>-arglists'
4300 ;; should therefore be nil.
4301 (c-parse-and-markup-<>-arglists t)
4302 c-restricted-<>-arglists)
4304 (when kwd-sym
4305 (goto-char (match-end match))
4306 (c-forward-syntactic-ws)
4307 (setq safe-pos (point))
4309 (cond
4310 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
4311 (c-forward-keyword-prefixed-id type))
4312 ;; There's a type directly after a keyword in `c-type-list-kwds'.
4313 (c-forward-id-comma-list type t))
4315 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
4316 (c-forward-keyword-prefixed-id ref))
4317 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
4318 (c-forward-id-comma-list ref t))
4320 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
4321 (eq (char-after) ?\())
4322 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
4324 (forward-char)
4325 (when (and (setq pos (c-up-list-forward))
4326 (eq (char-before pos) ?\)))
4327 (when (and c-record-type-identifiers
4328 (c-keyword-member kwd-sym 'c-paren-type-kwds))
4329 ;; Use `c-forward-type' on every identifier we can find
4330 ;; inside the paren, to record the types.
4331 (while (c-syntactic-re-search-forward c-symbol-start pos t)
4332 (goto-char (match-beginning 0))
4333 (unless (c-forward-type)
4334 (looking-at c-symbol-key) ; Always matches.
4335 (goto-char (match-end 0)))))
4337 (goto-char pos)
4338 (c-forward-syntactic-ws)
4339 (setq safe-pos (point))))
4341 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
4342 (eq (char-after) ?<)
4343 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
4344 (c-forward-syntactic-ws)
4345 (setq safe-pos (point)))
4347 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
4348 (not (looking-at c-symbol-start))
4349 (c-safe (c-forward-sexp) t))
4350 (c-forward-syntactic-ws)
4351 (setq safe-pos (point))))
4353 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
4354 (if (eq (char-after) ?:)
4355 ;; If we are at the colon already, we move over the type
4356 ;; list after it.
4357 (progn
4358 (forward-char)
4359 (c-forward-syntactic-ws)
4360 (when (c-forward-keyword-prefixed-id type)
4361 (c-forward-id-comma-list type t)))
4362 ;; Not at the colon, so stop here. But the identifier
4363 ;; ranges in the type list later on should still be
4364 ;; recorded.
4365 (and c-record-type-identifiers
4366 (progn
4367 ;; If a keyword matched both one of the types above and
4368 ;; this one, we match `c-colon-type-list-re' after the
4369 ;; clause matched above.
4370 (goto-char safe-pos)
4371 (looking-at c-colon-type-list-re))
4372 (progn
4373 (goto-char (match-end 0))
4374 (c-forward-syntactic-ws)
4375 (c-forward-keyword-prefixed-id type))
4376 ;; There's a type after the `c-colon-type-list-re' match
4377 ;; after a keyword in `c-colon-type-list-kwds'.
4378 (c-forward-id-comma-list type nil))))
4380 (goto-char safe-pos)
4381 t)))
4383 (defun c-forward-<>-arglist (all-types)
4384 ;; The point is assumed to be at a "<". Try to treat it as the open
4385 ;; paren of an angle bracket arglist and move forward to the
4386 ;; corresponding ">". If successful, the point is left after the
4387 ;; ">" and t is returned, otherwise the point isn't moved and nil is
4388 ;; returned. If ALL-TYPES is t then all encountered arguments in
4389 ;; the arglist that might be types are treated as found types.
4391 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
4392 ;; function handles text properties on the angle brackets and argument
4393 ;; separating commas.
4395 ;; `c-restricted-<>-arglists' controls how lenient the template
4396 ;; arglist recognition should be.
4398 ;; This function records identifier ranges on
4399 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4400 ;; `c-record-type-identifiers' is non-nil.
4402 ;; This function might do hidden buffer changes.
4404 (let ((start (point))
4405 ;; If `c-record-type-identifiers' is set then activate
4406 ;; recording of any found types that constitute an argument in
4407 ;; the arglist.
4408 (c-record-found-types (if c-record-type-identifiers t)))
4409 (if (catch 'angle-bracket-arglist-escape
4410 (setq c-record-found-types
4411 (c-forward-<>-arglist-recur all-types)))
4412 (progn
4413 (when (consp c-record-found-types)
4414 (setq c-record-type-identifiers
4415 ;; `nconc' doesn't mind that the tail of
4416 ;; `c-record-found-types' is t.
4417 (nconc c-record-found-types c-record-type-identifiers)))
4420 (goto-char start)
4421 nil)))
4423 (defun c-forward-<>-arglist-recur (all-types)
4424 ;; Recursive part of `c-forward-<>-arglist'.
4426 ;; This function might do hidden buffer changes.
4428 (let ((start (point)) res pos tmp
4429 ;; Cover this so that any recorded found type ranges are
4430 ;; automatically lost if it turns out to not be an angle
4431 ;; bracket arglist. It's propagated through the return value
4432 ;; on successful completion.
4433 (c-record-found-types c-record-found-types)
4434 ;; List that collects the positions after the argument
4435 ;; separating ',' in the arglist.
4436 arg-start-pos)
4438 ;; If the '<' has paren open syntax then we've marked it as an angle
4439 ;; bracket arglist before, so skip to the end.
4440 (if (and (not c-parse-and-markup-<>-arglists)
4441 (c-get-char-property (point) 'syntax-table))
4443 (progn
4444 (forward-char)
4445 (if (and (c-go-up-list-forward)
4446 (eq (char-before) ?>))
4449 ;; Got unmatched paren angle brackets. We don't clear the paren
4450 ;; syntax properties and retry, on the basis that it's very
4451 ;; unlikely that paren angle brackets become operators by code
4452 ;; manipulation. It's far more likely that it doesn't match due
4453 ;; to narrowing or some temporary change.
4454 (goto-char start)
4455 nil))
4457 (forward-char)
4458 (unless (looking-at c-<-op-cont-regexp)
4459 (while (and
4460 (progn
4462 (when c-record-type-identifiers
4463 (if all-types
4465 ;; All encountered identifiers are types, so set the
4466 ;; promote flag and parse the type.
4467 (progn
4468 (c-forward-syntactic-ws)
4469 (when (looking-at c-identifier-start)
4470 (let ((c-promote-possible-types t))
4471 (c-forward-type))))
4473 ;; Check if this arglist argument is a sole type. If
4474 ;; it's known then it's recorded in
4475 ;; `c-record-type-identifiers'. If it only is found
4476 ;; then it's recorded in `c-record-found-types' which we
4477 ;; might roll back if it turns out that this isn't an
4478 ;; angle bracket arglist afterall.
4479 (when (memq (char-before) '(?, ?<))
4480 (let ((orig-record-found-types c-record-found-types))
4481 (c-forward-syntactic-ws)
4482 (and (memq (c-forward-type) '(known found))
4483 (not (looking-at "[,>]"))
4484 ;; A found type was recorded but it's not the
4485 ;; only thing in the arglist argument, so reset
4486 ;; `c-record-found-types'.
4487 (setq c-record-found-types
4488 orig-record-found-types))))))
4490 (setq pos (point))
4491 (or (when (eq (char-after) ?>)
4492 ;; Must check for '>' at the very start separately,
4493 ;; since the regexp below has to avoid ">>" without
4494 ;; using \\=.
4495 (forward-char)
4498 ;; Note: These regexps exploit the match order in \| so
4499 ;; that "<>" is matched by "<" rather than "[^>:-]>".
4500 (c-syntactic-re-search-forward
4501 (if c-restricted-<>-arglists
4502 ;; Stop on ',', '|', '&', '+' and '-' to catch
4503 ;; common binary operators that could be between
4504 ;; two comparison expressions "a<b" and "c>d".
4505 "[<;{},|&+-]\\|\\([^>:-]>\\)"
4506 ;; Otherwise we still stop on ',' to find the
4507 ;; argument start positions.
4508 "[<;{},]\\|\\([^>:-]>\\)")
4509 nil 'move t t 1)
4511 ;; If the arglist starter has lost its open paren
4512 ;; syntax but not the closer, we won't find the
4513 ;; closer above since we only search in the
4514 ;; balanced sexp. In that case we stop just short
4515 ;; of it so check if the following char is the closer.
4516 (when (eq (char-after) ?>)
4517 (forward-char)
4518 t)))
4520 (cond
4521 ((eq (char-before) ?>)
4522 ;; Either an operator starting with '>' or the end of
4523 ;; the angle bracket arglist.
4525 (if (looking-at c->-op-cont-regexp)
4526 (progn
4527 (goto-char (match-end 0))
4528 t) ; Continue the loop.
4530 ;; The angle bracket arglist is finished.
4531 (when c-parse-and-markup-<>-arglists
4532 (while arg-start-pos
4533 (c-put-c-type-property (1- (car arg-start-pos))
4534 'c-<>-arg-sep)
4535 (setq arg-start-pos (cdr arg-start-pos)))
4536 (c-mark-<-as-paren start)
4537 (c-mark->-as-paren (1- (point))))
4538 (setq res t)
4539 nil)) ; Exit the loop.
4541 ((eq (char-before) ?<)
4542 ;; Either an operator starting with '<' or a nested arglist.
4544 (setq pos (point))
4545 (let (id-start id-end subres keyword-match)
4546 (if (if (looking-at c-<-op-cont-regexp)
4547 (setq tmp (match-end 0))
4548 (setq tmp pos)
4549 (backward-char)
4550 (not
4551 (and
4553 (save-excursion
4554 ;; There's always an identifier before an angle
4555 ;; bracket arglist, or a keyword in
4556 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
4557 (c-backward-syntactic-ws)
4558 (setq id-end (point))
4559 (c-simple-skip-symbol-backward)
4560 (when (or (setq keyword-match
4561 (looking-at c-opt-<>-sexp-key))
4562 (not (looking-at c-keywords-regexp)))
4563 (setq id-start (point))))
4565 (setq subres
4566 (let ((c-record-type-identifiers nil)
4567 (c-record-found-types nil))
4568 (c-forward-<>-arglist-recur
4569 (and keyword-match
4570 (c-keyword-member
4571 (c-keyword-sym (match-string 1))
4572 'c-<>-type-kwds)))))
4575 ;; It was not an angle bracket arglist.
4576 (goto-char tmp)
4578 ;; It was an angle bracket arglist.
4579 (setq c-record-found-types subres)
4581 ;; Record the identifier before the template as a type
4582 ;; or reference depending on whether the arglist is last
4583 ;; in a qualified identifier.
4584 (when (and c-record-type-identifiers
4585 (not keyword-match))
4586 (if (and c-opt-identifier-concat-key
4587 (progn
4588 (c-forward-syntactic-ws)
4589 (looking-at c-opt-identifier-concat-key)))
4590 (c-record-ref-id (cons id-start id-end))
4591 (c-record-type-id (cons id-start id-end))))))
4594 ((and (eq (char-before) ?,)
4595 (not c-restricted-<>-arglists))
4596 ;; Just another argument. Record the position. The
4597 ;; type check stuff that made us stop at it is at
4598 ;; the top of the loop.
4599 (setq arg-start-pos (cons (point) arg-start-pos)))
4602 ;; Got a character that can't be in an angle bracket
4603 ;; arglist argument. Abort using `throw', since
4604 ;; it's useless to try to find a surrounding arglist
4605 ;; if we're nested.
4606 (throw 'angle-bracket-arglist-escape nil))))))
4608 (if res
4609 (or c-record-found-types t)))))
4611 (defun c-backward-<>-arglist (all-types &optional limit)
4612 ;; The point is assumed to be directly after a ">". Try to treat it
4613 ;; as the close paren of an angle bracket arglist and move back to
4614 ;; the corresponding "<". If successful, the point is left at
4615 ;; the "<" and t is returned, otherwise the point isn't moved and
4616 ;; nil is returned. ALL-TYPES is passed on to
4617 ;; `c-forward-<>-arglist'.
4619 ;; If the optional LIMIT is given, it bounds the backward search.
4620 ;; It's then assumed to be at a syntactically relevant position.
4622 ;; This is a wrapper around `c-forward-<>-arglist'. See that
4623 ;; function for more details.
4625 (let ((start (point)))
4626 (backward-char)
4627 (if (and (not c-parse-and-markup-<>-arglists)
4628 (c-get-char-property (point) 'syntax-table))
4630 (if (and (c-go-up-list-backward)
4631 (eq (char-after) ?<))
4633 ;; See corresponding note in `c-forward-<>-arglist'.
4634 (goto-char start)
4635 nil)
4637 (while (progn
4638 (c-syntactic-skip-backward "^<;{}" limit t)
4640 (and
4641 (if (eq (char-before) ?<)
4643 ;; Stopped at bob or a char that isn't allowed in an
4644 ;; arglist, so we've failed.
4645 (goto-char start)
4646 nil)
4648 (if (> (point)
4649 (progn (c-beginning-of-current-token)
4650 (point)))
4651 ;; If we moved then the "<" was part of some
4652 ;; multicharacter token.
4655 (backward-char)
4656 (let ((beg-pos (point)))
4657 (if (c-forward-<>-arglist all-types)
4658 (cond ((= (point) start)
4659 ;; Matched the arglist. Break the while.
4660 (goto-char beg-pos)
4661 nil)
4662 ((> (point) start)
4663 ;; We started from a non-paren ">" inside an
4664 ;; arglist.
4665 (goto-char start)
4666 nil)
4668 ;; Matched a shorter arglist. Can be a nested
4669 ;; one so continue looking.
4670 (goto-char beg-pos)
4672 t))))))
4674 (/= (point) start))))
4676 (defun c-forward-name ()
4677 ;; Move forward over a complete name if at the beginning of one,
4678 ;; stopping at the next following token. If the point is not at
4679 ;; something that are recognized as name then it stays put. A name
4680 ;; could be something as simple as "foo" in C or something as
4681 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
4682 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
4683 ;; int>::*volatile const" in C++ (this function is actually little
4684 ;; more than a `looking-at' call in all modes except those that,
4685 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
4686 ;; name is found, 'template if it's an identifier ending with an
4687 ;; angle bracket arglist, 'operator of it's an operator identifier,
4688 ;; or t if it's some other kind of name.
4690 ;; This function records identifier ranges on
4691 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4692 ;; `c-record-type-identifiers' is non-nil.
4694 ;; This function might do hidden buffer changes.
4696 (let ((pos (point)) (start (point)) res id-start id-end
4697 ;; Turn off `c-promote-possible-types' here since we might
4698 ;; call `c-forward-<>-arglist' and we don't want it to promote
4699 ;; every suspect thing in the arglist to a type. We're
4700 ;; typically called from `c-forward-type' in this case, and
4701 ;; the caller only wants the top level type that it finds to
4702 ;; be promoted.
4703 c-promote-possible-types)
4704 (while
4705 (and
4706 (looking-at c-identifier-key)
4708 (progn
4709 ;; Check for keyword. We go to the last symbol in
4710 ;; `c-identifier-key' first.
4711 (goto-char (setq id-end (match-end 0)))
4712 (c-simple-skip-symbol-backward)
4713 (setq id-start (point))
4715 (if (looking-at c-keywords-regexp)
4716 (when (and (c-major-mode-is 'c++-mode)
4717 (looking-at
4718 (cc-eval-when-compile
4719 (concat "\\(operator\\|\\(template\\)\\)"
4720 "\\(" (c-lang-const c-nonsymbol-key c++)
4721 "\\|$\\)")))
4722 (if (match-beginning 2)
4723 ;; "template" is only valid inside an
4724 ;; identifier if preceded by "::".
4725 (save-excursion
4726 (c-backward-syntactic-ws)
4727 (and (c-safe (backward-char 2) t)
4728 (looking-at "::")))
4731 ;; Handle a C++ operator or template identifier.
4732 (goto-char id-end)
4733 (c-forward-syntactic-ws)
4734 (cond ((eq (char-before id-end) ?e)
4735 ;; Got "... ::template".
4736 (let ((subres (c-forward-name)))
4737 (when subres
4738 (setq pos (point)
4739 res subres))))
4741 ((looking-at c-identifier-start)
4742 ;; Got a cast operator.
4743 (when (c-forward-type)
4744 (setq pos (point)
4745 res 'operator)
4746 ;; Now we should match a sequence of either
4747 ;; '*', '&' or a name followed by ":: *",
4748 ;; where each can be followed by a sequence
4749 ;; of `c-opt-type-modifier-key'.
4750 (while (cond ((looking-at "[*&]")
4751 (goto-char (match-end 0))
4753 ((looking-at c-identifier-start)
4754 (and (c-forward-name)
4755 (looking-at "::")
4756 (progn
4757 (goto-char (match-end 0))
4758 (c-forward-syntactic-ws)
4759 (eq (char-after) ?*))
4760 (progn
4761 (forward-char)
4762 t))))
4763 (while (progn
4764 (c-forward-syntactic-ws)
4765 (setq pos (point))
4766 (looking-at c-opt-type-modifier-key))
4767 (goto-char (match-end 1))))))
4769 ((looking-at c-overloadable-operators-regexp)
4770 ;; Got some other operator.
4771 (setq c-last-identifier-range
4772 (cons (point) (match-end 0)))
4773 (goto-char (match-end 0))
4774 (c-forward-syntactic-ws)
4775 (setq pos (point)
4776 res 'operator)))
4778 nil)
4780 ;; `id-start' is equal to `id-end' if we've jumped over
4781 ;; an identifier that doesn't end with a symbol token.
4782 ;; That can occur e.g. for Java import directives on the
4783 ;; form "foo.bar.*".
4784 (when (and id-start (/= id-start id-end))
4785 (setq c-last-identifier-range
4786 (cons id-start id-end)))
4787 (goto-char id-end)
4788 (c-forward-syntactic-ws)
4789 (setq pos (point)
4790 res t)))
4792 (progn
4793 (goto-char pos)
4794 (when (or c-opt-identifier-concat-key
4795 c-recognize-<>-arglists)
4797 (cond
4798 ((and c-opt-identifier-concat-key
4799 (looking-at c-opt-identifier-concat-key))
4800 ;; Got a concatenated identifier. This handles the
4801 ;; cases with tricky syntactic whitespace that aren't
4802 ;; covered in `c-identifier-key'.
4803 (goto-char (match-end 0))
4804 (c-forward-syntactic-ws)
4807 ((and c-recognize-<>-arglists
4808 (eq (char-after) ?<))
4809 ;; Maybe an angle bracket arglist.
4811 (when (let (c-record-type-identifiers
4812 c-record-found-types)
4813 (c-forward-<>-arglist nil))
4815 (c-add-type start (1+ pos))
4816 (c-forward-syntactic-ws)
4817 (setq pos (point)
4818 c-last-identifier-range nil)
4820 (if (and c-opt-identifier-concat-key
4821 (looking-at c-opt-identifier-concat-key))
4823 ;; Continue if there's an identifier concatenation
4824 ;; operator after the template argument.
4825 (progn
4826 (when (and c-record-type-identifiers id-start)
4827 (c-record-ref-id (cons id-start id-end)))
4828 (forward-char 2)
4829 (c-forward-syntactic-ws)
4832 (when (and c-record-type-identifiers id-start)
4833 (c-record-type-id (cons id-start id-end)))
4834 (setq res 'template)
4835 nil)))
4836 )))))
4838 (goto-char pos)
4839 res))
4841 (defun c-forward-type ()
4842 ;; Move forward over a type spec if at the beginning of one,
4843 ;; stopping at the next following token. Return t if it's a known
4844 ;; type that can't be a name or other expression, 'known if it's an
4845 ;; otherwise known type (according to `*-font-lock-extra-types'),
4846 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4847 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4848 ;; that might be a type, or nil if it can't be a type (the point
4849 ;; isn't moved then). The point is assumed to be at the beginning
4850 ;; of a token.
4852 ;; Note that this function doesn't skip past the brace definition
4853 ;; that might be considered part of the type, e.g.
4854 ;; "enum {a, b, c} foo".
4856 ;; This function records identifier ranges on
4857 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4858 ;; `c-record-type-identifiers' is non-nil.
4860 ;; This function might do hidden buffer changes.
4862 (let ((start (point)) pos res name-res id-start id-end id-range)
4864 ;; Skip leading type modifiers. If any are found we know it's a
4865 ;; prefix of a type.
4866 (when c-opt-type-modifier-key
4867 (while (looking-at c-opt-type-modifier-key)
4868 (goto-char (match-end 1))
4869 (c-forward-syntactic-ws)
4870 (setq res 'prefix)))
4872 (cond
4873 ((looking-at c-type-prefix-key)
4874 ;; Looking at a keyword that prefixes a type identifier,
4875 ;; e.g. "class".
4876 (goto-char (match-end 1))
4877 (c-forward-syntactic-ws)
4878 (setq pos (point))
4879 (if (memq (setq name-res (c-forward-name)) '(t template))
4880 (progn
4881 (when (eq name-res t)
4882 ;; In many languages the name can be used without the
4883 ;; prefix, so we add it to `c-found-types'.
4884 (c-add-type pos (point))
4885 (when (and c-record-type-identifiers
4886 c-last-identifier-range)
4887 (c-record-type-id c-last-identifier-range)))
4888 (setq res t))
4889 ;; Invalid syntax.
4890 (goto-char start)
4891 (setq res nil)))
4893 ((progn
4894 (setq pos nil)
4895 (if (looking-at c-identifier-start)
4896 (save-excursion
4897 (setq id-start (point)
4898 name-res (c-forward-name))
4899 (when name-res
4900 (setq id-end (point)
4901 id-range c-last-identifier-range))))
4902 (and (cond ((looking-at c-primitive-type-key)
4903 (setq res t))
4904 ((c-with-syntax-table c-identifier-syntax-table
4905 (looking-at c-known-type-key))
4906 (setq res 'known)))
4907 (or (not id-end)
4908 (>= (save-excursion
4909 (save-match-data
4910 (goto-char (match-end 1))
4911 (c-forward-syntactic-ws)
4912 (setq pos (point))))
4913 id-end)
4914 (setq res nil))))
4915 ;; Looking at a primitive or known type identifier. We've
4916 ;; checked for a name first so that we don't go here if the
4917 ;; known type match only is a prefix of another name.
4919 (setq id-end (match-end 1))
4921 (when (and c-record-type-identifiers
4922 (or c-promote-possible-types (eq res t)))
4923 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4925 (if (and c-opt-type-component-key
4926 (save-match-data
4927 (looking-at c-opt-type-component-key)))
4928 ;; There might be more keywords for the type.
4929 (let (safe-pos)
4930 (c-forward-keyword-clause 1)
4931 (while (progn
4932 (setq safe-pos (point))
4933 (looking-at c-opt-type-component-key))
4934 (when (and c-record-type-identifiers
4935 (looking-at c-primitive-type-key))
4936 (c-record-type-id (cons (match-beginning 1)
4937 (match-end 1))))
4938 (c-forward-keyword-clause 1))
4939 (if (looking-at c-primitive-type-key)
4940 (progn
4941 (when c-record-type-identifiers
4942 (c-record-type-id (cons (match-beginning 1)
4943 (match-end 1))))
4944 (c-forward-keyword-clause 1)
4945 (setq res t))
4946 (goto-char safe-pos)
4947 (setq res 'prefix)))
4948 (unless (save-match-data (c-forward-keyword-clause 1))
4949 (if pos
4950 (goto-char pos)
4951 (goto-char (match-end 1))
4952 (c-forward-syntactic-ws)))))
4954 (name-res
4955 (cond ((eq name-res t)
4956 ;; A normal identifier.
4957 (goto-char id-end)
4958 (if (or res c-promote-possible-types)
4959 (progn
4960 (c-add-type id-start id-end)
4961 (when (and c-record-type-identifiers id-range)
4962 (c-record-type-id id-range))
4963 (unless res
4964 (setq res 'found)))
4965 (setq res (if (c-check-type id-start id-end)
4966 ;; It's an identifier that has been used as
4967 ;; a type somewhere else.
4968 'found
4969 ;; It's an identifier that might be a type.
4970 'maybe))))
4971 ((eq name-res 'template)
4972 ;; A template is a type.
4973 (goto-char id-end)
4974 (setq res t))
4976 ;; Otherwise it's an operator identifier, which is not a type.
4977 (goto-char start)
4978 (setq res nil)))))
4980 (when res
4981 ;; Skip trailing type modifiers. If any are found we know it's
4982 ;; a type.
4983 (when c-opt-type-modifier-key
4984 (while (looking-at c-opt-type-modifier-key)
4985 (goto-char (match-end 1))
4986 (c-forward-syntactic-ws)
4987 (setq res t)))
4989 ;; Step over any type suffix operator. Do not let the existence
4990 ;; of these alter the classification of the found type, since
4991 ;; these operators typically are allowed in normal expressions
4992 ;; too.
4993 (when c-opt-type-suffix-key
4994 (while (looking-at c-opt-type-suffix-key)
4995 (goto-char (match-end 1))
4996 (c-forward-syntactic-ws)))
4998 (when c-opt-type-concat-key
4999 ;; Look for a trailing operator that concatenates the type
5000 ;; with a following one, and if so step past that one through
5001 ;; a recursive call. Note that we don't record concatenated
5002 ;; types in `c-found-types' - it's the component types that
5003 ;; are recorded when appropriate.
5004 (setq pos (point))
5005 (let* ((c-promote-possible-types (or (memq res '(t known))
5006 c-promote-possible-types))
5007 ;; If we can't promote then set `c-record-found-types' so that
5008 ;; we can merge in the types from the second part afterwards if
5009 ;; it turns out to be a known type there.
5010 (c-record-found-types (and c-record-type-identifiers
5011 (not c-promote-possible-types)))
5012 subres)
5013 (if (and (looking-at c-opt-type-concat-key)
5015 (progn
5016 (goto-char (match-end 1))
5017 (c-forward-syntactic-ws)
5018 (setq subres (c-forward-type))))
5020 (progn
5021 ;; If either operand certainly is a type then both are, but we
5022 ;; don't let the existence of the operator itself promote two
5023 ;; uncertain types to a certain one.
5024 (cond ((eq res t))
5025 ((eq subres t)
5026 (unless (eq name-res 'template)
5027 (c-add-type id-start id-end))
5028 (when (and c-record-type-identifiers id-range)
5029 (c-record-type-id id-range))
5030 (setq res t))
5031 ((eq res 'known))
5032 ((eq subres 'known)
5033 (setq res 'known))
5034 ((eq res 'found))
5035 ((eq subres 'found)
5036 (setq res 'found))
5038 (setq res 'maybe)))
5040 (when (and (eq res t)
5041 (consp c-record-found-types))
5042 ;; Merge in the ranges of any types found by the second
5043 ;; `c-forward-type'.
5044 (setq c-record-type-identifiers
5045 ;; `nconc' doesn't mind that the tail of
5046 ;; `c-record-found-types' is t.
5047 (nconc c-record-found-types
5048 c-record-type-identifiers))))
5050 (goto-char pos))))
5052 (when (and c-record-found-types (memq res '(known found)) id-range)
5053 (setq c-record-found-types
5054 (cons id-range c-record-found-types))))
5056 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
5058 res))
5061 ;; Handling of large scale constructs like statements and declarations.
5063 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
5064 ;; defsubst or perhaps even a defun, but it contains lots of free
5065 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
5066 (defmacro c-fdoc-shift-type-backward (&optional short)
5067 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
5068 ;; of types when parsing a declaration, which means that it
5069 ;; sometimes consumes the identifier in the declaration as a type.
5070 ;; This is used to "backtrack" and make the last type be treated as
5071 ;; an identifier instead.
5072 `(progn
5073 ,(unless short
5074 ;; These identifiers are bound only in the inner let.
5075 '(setq identifier-type at-type
5076 identifier-start type-start
5077 got-parens nil
5078 got-identifier t
5079 got-suffix t
5080 got-suffix-after-parens id-start
5081 paren-depth 0))
5083 (if (setq at-type (if (eq backup-at-type 'prefix)
5085 backup-at-type))
5086 (setq type-start backup-type-start
5087 id-start backup-id-start)
5088 (setq type-start start-pos
5089 id-start start-pos))
5091 ;; When these flags already are set we've found specifiers that
5092 ;; unconditionally signal these attributes - backtracking doesn't
5093 ;; change that. So keep them set in that case.
5094 (or at-type-decl
5095 (setq at-type-decl backup-at-type-decl))
5096 (or maybe-typeless
5097 (setq maybe-typeless backup-maybe-typeless))
5099 ,(unless short
5100 ;; This identifier is bound only in the inner let.
5101 '(setq start id-start))))
5103 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
5104 ;; Move forward over a declaration or a cast if at the start of one.
5105 ;; The point is assumed to be at the start of some token. Nil is
5106 ;; returned if no declaration or cast is recognized, and the point
5107 ;; is clobbered in that case.
5109 ;; If a declaration is parsed:
5111 ;; The point is left at the first token after the first complete
5112 ;; declarator, if there is one. The return value is a cons where
5113 ;; the car is the position of the first token in the declarator. (See
5114 ;; below for the cdr.)
5115 ;; Some examples:
5117 ;; void foo (int a, char *b) stuff ...
5118 ;; car ^ ^ point
5119 ;; float (*a)[], b;
5120 ;; car ^ ^ point
5121 ;; unsigned int a = c_style_initializer, b;
5122 ;; car ^ ^ point
5123 ;; unsigned int a (cplusplus_style_initializer), b;
5124 ;; car ^ ^ point (might change)
5125 ;; class Foo : public Bar {}
5126 ;; car ^ ^ point
5127 ;; class PikeClass (int a, string b) stuff ...
5128 ;; car ^ ^ point
5129 ;; enum bool;
5130 ;; car ^ ^ point
5131 ;; enum bool flag;
5132 ;; car ^ ^ point
5133 ;; void cplusplus_function (int x) throw (Bad);
5134 ;; car ^ ^ point
5135 ;; Foo::Foo (int b) : Base (b) {}
5136 ;; car ^ ^ point
5138 ;; The cdr of the return value is non-nil iff a `c-typedef-decl-kwds'
5139 ;; specifier (e.g. class, struct, enum, typedef) is found in the
5140 ;; declaration, i.e. the declared identifier(s) are types.
5142 ;; If a cast is parsed:
5144 ;; The point is left at the first token after the closing paren of
5145 ;; the cast. The return value is `cast'. Note that the start
5146 ;; position must be at the first token inside the cast parenthesis
5147 ;; to recognize it.
5149 ;; PRECEDING-TOKEN-END is the first position after the preceding
5150 ;; token, i.e. on the other side of the syntactic ws from the point.
5151 ;; Use a value less than or equal to (point-min) if the point is at
5152 ;; the first token in (the visible part of) the buffer.
5154 ;; CONTEXT is a symbol that describes the context at the point:
5155 ;; 'decl In a comma-separated declaration context (typically
5156 ;; inside a function declaration arglist).
5157 ;; '<> In an angle bracket arglist.
5158 ;; 'arglist Some other type of arglist.
5159 ;; nil Some other context or unknown context. Includes
5160 ;; within the parens of an if, for, ... construct.
5162 ;; LAST-CAST-END is the first token after the closing paren of a
5163 ;; preceding cast, or nil if none is known. If
5164 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
5165 ;; the position after the closest preceding call where a cast was
5166 ;; matched. In that case it's used to discover chains of casts like
5167 ;; "(a) (b) c".
5169 ;; This function records identifier ranges on
5170 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5171 ;; `c-record-type-identifiers' is non-nil.
5173 ;; This function might do hidden buffer changes.
5175 (let (;; `start-pos' is used below to point to the start of the
5176 ;; first type, i.e. after any leading specifiers. It might
5177 ;; also point at the beginning of the preceding syntactic
5178 ;; whitespace.
5179 (start-pos (point))
5180 ;; Set to the result of `c-forward-type'.
5181 at-type
5182 ;; The position of the first token in what we currently
5183 ;; believe is the type in the declaration or cast, after any
5184 ;; specifiers and their associated clauses.
5185 type-start
5186 ;; The position of the first token in what we currently
5187 ;; believe is the declarator for the first identifier. Set
5188 ;; when the type is found, and moved forward over any
5189 ;; `c-decl-hangon-kwds' and their associated clauses that
5190 ;; occurs after the type.
5191 id-start
5192 ;; These store `at-type', `type-start' and `id-start' of the
5193 ;; identifier before the one in those variables. The previous
5194 ;; identifier might turn out to be the real type in a
5195 ;; declaration if the last one has to be the declarator in it.
5196 ;; If `backup-at-type' is nil then the other variables have
5197 ;; undefined values.
5198 backup-at-type backup-type-start backup-id-start
5199 ;; Set if we've found a specifier that makes the defined
5200 ;; identifier(s) types.
5201 at-type-decl
5202 ;; Set if we've found a specifier that can start a declaration
5203 ;; where there's no type.
5204 maybe-typeless
5205 ;; If a specifier is found that also can be a type prefix,
5206 ;; these flags are set instead of those above. If we need to
5207 ;; back up an identifier, they are copied to the real flag
5208 ;; variables. Thus they only take effect if we fail to
5209 ;; interpret it as a type.
5210 backup-at-type-decl backup-maybe-typeless
5211 ;; Whether we've found a declaration or a cast. We might know
5212 ;; this before we've found the type in it. It's 'ids if we've
5213 ;; found two consecutive identifiers (usually a sure sign, but
5214 ;; we should allow that in labels too), and t if we've found a
5215 ;; specifier keyword (a 100% sure sign).
5216 at-decl-or-cast
5217 ;; Set when we need to back up to parse this as a declaration
5218 ;; but not as a cast.
5219 backup-if-not-cast
5220 ;; For casts, the return position.
5221 cast-end
5222 ;; Save `c-record-type-identifiers' and
5223 ;; `c-record-ref-identifiers' since ranges are recorded
5224 ;; speculatively and should be thrown away if it turns out
5225 ;; that it isn't a declaration or cast.
5226 (save-rec-type-ids c-record-type-identifiers)
5227 (save-rec-ref-ids c-record-ref-identifiers))
5229 ;; Check for a type. Unknown symbols are treated as possible
5230 ;; types, but they could also be specifiers disguised through
5231 ;; macros like __INLINE__, so we recognize both types and known
5232 ;; specifiers after them too.
5233 (while
5234 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
5236 ;; Look for a specifier keyword clause.
5237 (when (looking-at c-prefix-spec-kwds-re)
5238 (setq kwd-sym (c-keyword-sym (match-string 1)))
5239 (save-excursion
5240 (c-forward-keyword-clause 1)
5241 (setq kwd-clause-end (point))))
5243 (when (setq found-type (c-forward-type))
5244 ;; Found a known or possible type or a prefix of a known type.
5246 (when at-type
5247 ;; Got two identifiers with nothing but whitespace
5248 ;; between them. That can only happen in declarations.
5249 (setq at-decl-or-cast 'ids)
5251 (when (eq at-type 'found)
5252 ;; If the previous identifier is a found type we
5253 ;; record it as a real one; it might be some sort of
5254 ;; alias for a prefix like "unsigned".
5255 (save-excursion
5256 (goto-char type-start)
5257 (let ((c-promote-possible-types t))
5258 (c-forward-type)))))
5260 (setq backup-at-type at-type
5261 backup-type-start type-start
5262 backup-id-start id-start
5263 at-type found-type
5264 type-start start
5265 id-start (point)
5266 ;; The previous ambiguous specifier/type turned out
5267 ;; to be a type since we've parsed another one after
5268 ;; it, so clear these backup flags.
5269 backup-at-type-decl nil
5270 backup-maybe-typeless nil))
5272 (if kwd-sym
5273 (progn
5274 ;; Handle known specifier keywords and
5275 ;; `c-decl-hangon-kwds' which can occur after known
5276 ;; types.
5278 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
5279 ;; It's a hang-on keyword that can occur anywhere.
5280 (progn
5281 (setq at-decl-or-cast t)
5282 (if at-type
5283 ;; Move the identifier start position if
5284 ;; we've passed a type.
5285 (setq id-start kwd-clause-end)
5286 ;; Otherwise treat this as a specifier and
5287 ;; move the fallback position.
5288 (setq start-pos kwd-clause-end))
5289 (goto-char kwd-clause-end))
5291 ;; It's an ordinary specifier so we know that
5292 ;; anything before this can't be the type.
5293 (setq backup-at-type nil
5294 start-pos kwd-clause-end)
5296 (if found-type
5297 ;; It's ambiguous whether this keyword is a
5298 ;; specifier or a type prefix, so set the backup
5299 ;; flags. (It's assumed that `c-forward-type'
5300 ;; moved further than `c-forward-keyword-clause'.)
5301 (progn
5302 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5303 (setq backup-at-type-decl t))
5304 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5305 (setq backup-maybe-typeless t)))
5307 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5308 (setq at-type-decl t))
5309 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5310 (setq maybe-typeless t))
5312 ;; Haven't matched a type so it's an umambiguous
5313 ;; specifier keyword and we know we're in a
5314 ;; declaration.
5315 (setq at-decl-or-cast t)
5317 (goto-char kwd-clause-end))))
5319 ;; If the type isn't known we continue so that we'll jump
5320 ;; over all specifiers and type identifiers. The reason
5321 ;; to do this for a known type prefix is to make things
5322 ;; like "unsigned INT16" work.
5323 (and found-type (not (eq found-type t))))))
5325 (cond
5326 ((eq at-type t)
5327 ;; If a known type was found, we still need to skip over any
5328 ;; hangon keyword clauses after it. Otherwise it has already
5329 ;; been done in the loop above.
5330 (while (looking-at c-decl-hangon-key)
5331 (c-forward-keyword-clause 1))
5332 (setq id-start (point)))
5334 ((eq at-type 'prefix)
5335 ;; A prefix type is itself a primitive type when it's not
5336 ;; followed by another type.
5337 (setq at-type t))
5339 ((not at-type)
5340 ;; Got no type but set things up to continue anyway to handle
5341 ;; the various cases when a declaration doesn't start with a
5342 ;; type.
5343 (setq id-start start-pos))
5345 ((and (eq at-type 'maybe)
5346 (c-major-mode-is 'c++-mode))
5347 ;; If it's C++ then check if the last "type" ends on the form
5348 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
5349 ;; (con|de)structor.
5350 (save-excursion
5351 (let (name end-2 end-1)
5352 (goto-char id-start)
5353 (c-backward-syntactic-ws)
5354 (setq end-2 (point))
5355 (when (and
5356 (c-simple-skip-symbol-backward)
5357 (progn
5358 (setq name
5359 (buffer-substring-no-properties (point) end-2))
5360 ;; Cheating in the handling of syntactic ws below.
5361 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
5362 (progn
5363 (setq end-1 (point))
5364 (c-simple-skip-symbol-backward))
5365 (>= (point) type-start)
5366 (equal (buffer-substring-no-properties (point) end-1)
5367 name))
5368 ;; It is a (con|de)structor name. In that case the
5369 ;; declaration is typeless so zap out any preceding
5370 ;; identifier(s) that we might have taken as types.
5371 (goto-char type-start)
5372 (setq at-type nil
5373 backup-at-type nil
5374 id-start type-start))))))
5376 ;; Check for and step over a type decl expression after the thing
5377 ;; that is or might be a type. This can't be skipped since we
5378 ;; need the correct end position of the declarator for
5379 ;; `max-type-decl-end-*'.
5380 (let ((start (point)) (paren-depth 0) pos
5381 ;; True if there's a non-open-paren match of
5382 ;; `c-type-decl-prefix-key'.
5383 got-prefix
5384 ;; True if the declarator is surrounded by a parenthesis pair.
5385 got-parens
5386 ;; True if there is an identifier in the declarator.
5387 got-identifier
5388 ;; True if there's a non-close-paren match of
5389 ;; `c-type-decl-suffix-key'.
5390 got-suffix
5391 ;; True if there's a prefix match outside the outermost
5392 ;; paren pair that surrounds the declarator.
5393 got-prefix-before-parens
5394 ;; True if there's a suffix match outside the outermost
5395 ;; paren pair that surrounds the declarator. The value is
5396 ;; the position of the first suffix match.
5397 got-suffix-after-parens
5398 ;; True if we've parsed the type decl to a token that is
5399 ;; known to end declarations in this context.
5400 at-decl-end
5401 ;; The earlier values of `at-type' and `type-start' if we've
5402 ;; shifted the type backwards.
5403 identifier-type identifier-start
5404 ;; If `c-parse-and-markup-<>-arglists' is set we need to
5405 ;; turn it off during the name skipping below to avoid
5406 ;; getting `c-type' properties that might be bogus. That
5407 ;; can happen since we don't know if
5408 ;; `c-restricted-<>-arglists' will be correct inside the
5409 ;; arglist paren that gets entered.
5410 c-parse-and-markup-<>-arglists)
5412 (goto-char id-start)
5414 ;; Skip over type decl prefix operators. (Note similar code in
5415 ;; `c-font-lock-declarators'.)
5416 (while (and (looking-at c-type-decl-prefix-key)
5417 (if (and (c-major-mode-is 'c++-mode)
5418 (match-beginning 2))
5419 ;; If the second submatch matches in C++ then
5420 ;; we're looking at an identifier that's a
5421 ;; prefix only if it specifies a member pointer.
5422 (when (setq got-identifier (c-forward-name))
5423 (if (looking-at "\\(::\\)")
5424 ;; We only check for a trailing "::" and
5425 ;; let the "*" that should follow be
5426 ;; matched in the next round.
5427 (progn (setq got-identifier nil) t)
5428 ;; It turned out to be the real identifier,
5429 ;; so stop.
5430 nil))
5433 (if (eq (char-after) ?\()
5434 (progn
5435 (setq paren-depth (1+ paren-depth))
5436 (forward-char))
5437 (unless got-prefix-before-parens
5438 (setq got-prefix-before-parens (= paren-depth 0)))
5439 (setq got-prefix t)
5440 (goto-char (match-end 1)))
5441 (c-forward-syntactic-ws))
5443 (setq got-parens (> paren-depth 0))
5445 ;; Skip over an identifier.
5446 (or got-identifier
5447 (and (looking-at c-identifier-start)
5448 (setq got-identifier (c-forward-name))))
5450 ;; Skip over type decl suffix operators.
5451 (while (if (looking-at c-type-decl-suffix-key)
5453 (if (eq (char-after) ?\))
5454 (when (> paren-depth 0)
5455 (setq paren-depth (1- paren-depth))
5456 (forward-char)
5458 (when (if (save-match-data (looking-at "\\s\("))
5459 (c-safe (c-forward-sexp 1) t)
5460 (goto-char (match-end 1))
5462 (when (and (not got-suffix-after-parens)
5463 (= paren-depth 0))
5464 (setq got-suffix-after-parens (match-beginning 0)))
5465 (setq got-suffix t)))
5467 ;; No suffix matched. We might have matched the
5468 ;; identifier as a type and the open paren of a
5469 ;; function arglist as a type decl prefix. In that
5470 ;; case we should "backtrack": Reinterpret the last
5471 ;; type as the identifier, move out of the arglist and
5472 ;; continue searching for suffix operators.
5474 ;; Do this even if there's no preceding type, to cope
5475 ;; with old style function declarations in K&R C,
5476 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
5477 ;; style declarations. That isn't applicable in an
5478 ;; arglist context, though.
5479 (when (and (= paren-depth 1)
5480 (not got-prefix-before-parens)
5481 (not (eq at-type t))
5482 (or backup-at-type
5483 maybe-typeless
5484 backup-maybe-typeless
5485 (when c-recognize-typeless-decls
5486 (not context)))
5487 (setq pos (c-up-list-forward (point)))
5488 (eq (char-before pos) ?\)))
5489 (c-fdoc-shift-type-backward)
5490 (goto-char pos)
5493 (c-forward-syntactic-ws))
5495 (when (and (or maybe-typeless backup-maybe-typeless)
5496 (not got-identifier)
5497 (not got-prefix)
5498 at-type)
5499 ;; Have found no identifier but `c-typeless-decl-kwds' has
5500 ;; matched so we know we're inside a declaration. The
5501 ;; preceding type must be the identifier instead.
5502 (c-fdoc-shift-type-backward))
5504 (setq
5505 at-decl-or-cast
5506 (catch 'at-decl-or-cast
5508 ;; CASE 1
5509 (when (> paren-depth 0)
5510 ;; Encountered something inside parens that isn't matched by
5511 ;; the `c-type-decl-*' regexps, so it's not a type decl
5512 ;; expression. Try to skip out to the same paren depth to
5513 ;; not confuse the cast check below.
5514 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
5515 ;; If we've found a specifier keyword then it's a
5516 ;; declaration regardless.
5517 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
5519 (setq at-decl-end
5520 (looking-at (cond ((eq context '<>) "[,>]")
5521 (context "[,\)]")
5522 (t "[,;]"))))
5524 ;; Now we've collected info about various characteristics of
5525 ;; the construct we're looking at. Below follows a decision
5526 ;; tree based on that. It's ordered to check more certain
5527 ;; signs before less certain ones.
5529 (if got-identifier
5530 (progn
5532 ;; CASE 2
5533 (when (and (or at-type maybe-typeless)
5534 (not (or got-prefix got-parens)))
5535 ;; Got another identifier directly after the type, so it's a
5536 ;; declaration.
5537 (throw 'at-decl-or-cast t))
5539 (when (and got-parens
5540 (not got-prefix)
5541 (not got-suffix-after-parens)
5542 (or backup-at-type
5543 maybe-typeless
5544 backup-maybe-typeless))
5545 ;; Got a declaration of the form "foo bar (gnu);" where we've
5546 ;; recognized "bar" as the type and "gnu" as the declarator.
5547 ;; In this case it's however more likely that "bar" is the
5548 ;; declarator and "gnu" a function argument or initializer (if
5549 ;; `c-recognize-paren-inits' is set), since the parens around
5550 ;; "gnu" would be superfluous if it's a declarator. Shift the
5551 ;; type one step backward.
5552 (c-fdoc-shift-type-backward)))
5554 ;; Found no identifier.
5556 (if backup-at-type
5557 (progn
5559 ;; CASE 3
5560 (when (= (point) start)
5561 ;; Got a plain list of identifiers. If a colon follows it's
5562 ;; a valid label. Otherwise the last one probably is the
5563 ;; declared identifier and we should back up to the previous
5564 ;; type, providing it isn't a cast.
5565 (if (eq (char-after) ?:)
5566 ;; If we've found a specifier keyword then it's a
5567 ;; declaration regardless.
5568 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
5569 (setq backup-if-not-cast t)
5570 (throw 'at-decl-or-cast t)))
5572 ;; CASE 4
5573 (when (and got-suffix
5574 (not got-prefix)
5575 (not got-parens))
5576 ;; Got a plain list of identifiers followed by some suffix.
5577 ;; If this isn't a cast then the last identifier probably is
5578 ;; the declared one and we should back up to the previous
5579 ;; type.
5580 (setq backup-if-not-cast t)
5581 (throw 'at-decl-or-cast t)))
5583 ;; CASE 5
5584 (when (eq at-type t)
5585 ;; If the type is known we know that there can't be any
5586 ;; identifier somewhere else, and it's only in declarations in
5587 ;; e.g. function prototypes and in casts that the identifier may
5588 ;; be left out.
5589 (throw 'at-decl-or-cast t))
5591 (when (= (point) start)
5592 ;; Only got a single identifier (parsed as a type so far).
5593 ;; CASE 6
5594 (if (and
5595 ;; Check that the identifier isn't at the start of an
5596 ;; expression.
5597 at-decl-end
5598 (cond
5599 ((eq context 'decl)
5600 ;; Inside an arglist that contains declarations. If K&R
5601 ;; style declarations and parenthesis style initializers
5602 ;; aren't allowed then the single identifier must be a
5603 ;; type, else we require that it's known or found
5604 ;; (primitive types are handled above).
5605 (or (and (not c-recognize-knr-p)
5606 (not c-recognize-paren-inits))
5607 (memq at-type '(known found))))
5608 ((eq context '<>)
5609 ;; Inside a template arglist. Accept known and found
5610 ;; types; other identifiers could just as well be
5611 ;; constants in C++.
5612 (memq at-type '(known found)))))
5613 (throw 'at-decl-or-cast t)
5614 ;; CASE 7
5615 ;; Can't be a valid declaration or cast, but if we've found a
5616 ;; specifier it can't be anything else either, so treat it as
5617 ;; an invalid/unfinished declaration or cast.
5618 (throw 'at-decl-or-cast at-decl-or-cast))))
5620 (if (and got-parens
5621 (not got-prefix)
5622 (not context)
5623 (not (eq at-type t))
5624 (or backup-at-type
5625 maybe-typeless
5626 backup-maybe-typeless
5627 (when c-recognize-typeless-decls
5628 (or (not got-suffix)
5629 (not (looking-at
5630 c-after-suffixed-type-maybe-decl-key))))))
5631 ;; Got an empty paren pair and a preceding type that probably
5632 ;; really is the identifier. Shift the type backwards to make
5633 ;; the last one the identifier. This is analogous to the
5634 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
5635 ;; above.
5637 ;; Exception: In addition to the conditions in that
5638 ;; "backtracking" code, do not shift backward if we're not
5639 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
5640 ;; Since there's no preceding type, the shift would mean that
5641 ;; the declaration is typeless. But if the regexp doesn't match
5642 ;; then we will simply fall through in the tests below and not
5643 ;; recognize it at all, so it's better to try it as an abstract
5644 ;; declarator instead.
5645 (c-fdoc-shift-type-backward)
5647 ;; Still no identifier.
5648 ;; CASE 8
5649 (when (and got-prefix (or got-parens got-suffix))
5650 ;; Require `got-prefix' together with either `got-parens' or
5651 ;; `got-suffix' to recognize it as an abstract declarator:
5652 ;; `got-parens' only is probably an empty function call.
5653 ;; `got-suffix' only can build an ordinary expression together
5654 ;; with the preceding identifier which we've taken as a type.
5655 ;; We could actually accept on `got-prefix' only, but that can
5656 ;; easily occur temporarily while writing an expression so we
5657 ;; avoid that case anyway. We could do a better job if we knew
5658 ;; the point when the fontification was invoked.
5659 (throw 'at-decl-or-cast t))
5661 ;; CASE 9
5662 (when (and at-type
5663 (not got-prefix)
5664 (not got-parens)
5665 got-suffix-after-parens
5666 (eq (char-after got-suffix-after-parens) ?\())
5667 ;; Got a type, no declarator but a paren suffix. I.e. it's a
5668 ;; normal function call afterall (or perhaps a C++ style object
5669 ;; instantiation expression).
5670 (throw 'at-decl-or-cast nil))))
5672 ;; CASE 10
5673 (when at-decl-or-cast
5674 ;; By now we've located the type in the declaration that we know
5675 ;; we're in.
5676 (throw 'at-decl-or-cast t))
5678 ;; CASE 11
5679 (when (and got-identifier
5680 (not context)
5681 (looking-at c-after-suffixed-type-decl-key)
5682 (if (and got-parens
5683 (not got-prefix)
5684 (not got-suffix)
5685 (not (eq at-type t)))
5686 ;; Shift the type backward in the case that there's a
5687 ;; single identifier inside parens. That can only
5688 ;; occur in K&R style function declarations so it's
5689 ;; more likely that it really is a function call.
5690 ;; Therefore we only do this after
5691 ;; `c-after-suffixed-type-decl-key' has matched.
5692 (progn (c-fdoc-shift-type-backward) t)
5693 got-suffix-after-parens))
5694 ;; A declaration according to `c-after-suffixed-type-decl-key'.
5695 (throw 'at-decl-or-cast t))
5697 ;; CASE 12
5698 (when (and (or got-prefix (not got-parens))
5699 (memq at-type '(t known)))
5700 ;; It's a declaration if a known type precedes it and it can't be a
5701 ;; function call.
5702 (throw 'at-decl-or-cast t))
5704 ;; If we get here we can't tell if this is a type decl or a normal
5705 ;; expression by looking at it alone. (That's under the assumption
5706 ;; that normal expressions always can look like type decl expressions,
5707 ;; which isn't really true but the cases where it doesn't hold are so
5708 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
5709 ;; the effort to look for them.)
5711 (unless (or at-decl-end (looking-at "=[^=]"))
5712 ;; If this is a declaration it should end here or its initializer(*)
5713 ;; should start here, so check for allowed separation tokens. Note
5714 ;; that this rule doesn't work e.g. with a K&R arglist after a
5715 ;; function header.
5717 ;; *) Don't check for C++ style initializers using parens
5718 ;; since those already have been matched as suffixes.
5720 ;; If `at-decl-or-cast' is then we've found some other sign that
5721 ;; it's a declaration or cast, so then it's probably an
5722 ;; invalid/unfinished one.
5723 (throw 'at-decl-or-cast at-decl-or-cast))
5725 ;; Below are tests that only should be applied when we're certain to
5726 ;; not have parsed halfway through an expression.
5728 ;; CASE 14
5729 (when (memq at-type '(t known))
5730 ;; The expression starts with a known type so treat it as a
5731 ;; declaration.
5732 (throw 'at-decl-or-cast t))
5734 ;; CASE 15
5735 (when (and (c-major-mode-is 'c++-mode)
5736 ;; In C++ we check if the identifier is a known type, since
5737 ;; (con|de)structors use the class name as identifier.
5738 ;; We've always shifted over the identifier as a type and
5739 ;; then backed up again in this case.
5740 identifier-type
5741 (or (memq identifier-type '(found known))
5742 (and (eq (char-after identifier-start) ?~)
5743 ;; `at-type' probably won't be 'found for
5744 ;; destructors since the "~" is then part of the
5745 ;; type name being checked against the list of
5746 ;; known types, so do a check without that
5747 ;; operator.
5748 (or (save-excursion
5749 (goto-char (1+ identifier-start))
5750 (c-forward-syntactic-ws)
5751 (c-with-syntax-table
5752 c-identifier-syntax-table
5753 (looking-at c-known-type-key)))
5754 (save-excursion
5755 (goto-char (1+ identifier-start))
5756 ;; We have already parsed the type earlier,
5757 ;; so it'd be possible to cache the end
5758 ;; position instead of redoing it here, but
5759 ;; then we'd need to keep track of another
5760 ;; position everywhere.
5761 (c-check-type (point)
5762 (progn (c-forward-type)
5763 (point))))))))
5764 (throw 'at-decl-or-cast t))
5766 (if got-identifier
5767 (progn
5768 ;; CASE 16
5769 (when (and got-prefix-before-parens
5770 at-type
5771 (or at-decl-end (looking-at "=[^=]"))
5772 (not context)
5773 (not got-suffix))
5774 ;; Got something like "foo * bar;". Since we're not inside an
5775 ;; arglist it would be a meaningless expression because the
5776 ;; result isn't used. We therefore choose to recognize it as
5777 ;; a declaration. Do not allow a suffix since it could then
5778 ;; be a function call.
5779 (throw 'at-decl-or-cast t))
5781 ;; CASE 17
5782 (when (and (or got-suffix-after-parens
5783 (looking-at "=[^=]"))
5784 (eq at-type 'found)
5785 (not (eq context 'arglist)))
5786 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
5787 ;; be an odd expression or it could be a declaration. Treat
5788 ;; it as a declaration if "a" has been used as a type
5789 ;; somewhere else (if it's a known type we won't get here).
5790 (throw 'at-decl-or-cast t)))
5792 ;; CASE 18
5793 (when (and context
5794 (or got-prefix
5795 (and (eq context 'decl)
5796 (not c-recognize-paren-inits)
5797 (or got-parens got-suffix))))
5798 ;; Got a type followed by an abstract declarator. If `got-prefix'
5799 ;; is set it's something like "a *" without anything after it. If
5800 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
5801 ;; or similar, which we accept only if the context rules out
5802 ;; expressions.
5803 (throw 'at-decl-or-cast t)))
5805 ;; If we had a complete symbol table here (which rules out
5806 ;; `c-found-types') we should return t due to the disambiguation rule
5807 ;; (in at least C++) that anything that can be parsed as a declaration
5808 ;; is a declaration. Now we're being more defensive and prefer to
5809 ;; highlight things like "foo (bar);" as a declaration only if we're
5810 ;; inside an arglist that contains declarations.
5811 (eq context 'decl))))
5813 ;; The point is now after the type decl expression.
5815 (cond
5816 ;; Check for a cast.
5817 ((save-excursion
5818 (and
5819 c-cast-parens
5821 ;; Should be the first type/identifier in a cast paren.
5822 (> preceding-token-end (point-min))
5823 (memq (char-before preceding-token-end) c-cast-parens)
5825 ;; The closing paren should follow.
5826 (progn
5827 (c-forward-syntactic-ws)
5828 (looking-at "\\s\)"))
5830 ;; There should be a primary expression after it.
5831 (let (pos)
5832 (forward-char)
5833 (c-forward-syntactic-ws)
5834 (setq cast-end (point))
5835 (and (looking-at c-primary-expr-regexp)
5836 (progn
5837 (setq pos (match-end 0))
5839 ;; Check if the expression begins with a prefix keyword.
5840 (match-beginning 2)
5841 (if (match-beginning 1)
5842 ;; Expression begins with an ambiguous operator. Treat
5843 ;; it as a cast if it's a type decl or if we've
5844 ;; recognized the type somewhere else.
5845 (or at-decl-or-cast
5846 (memq at-type '(t known found)))
5847 ;; Unless it's a keyword, it's the beginning of a primary
5848 ;; expression.
5849 (not (looking-at c-keywords-regexp)))))
5850 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
5851 ;; that it matched a whole one so that we don't e.g. confuse
5852 ;; the operator '-' with '->'. It's ok if it matches further,
5853 ;; though, since it e.g. can match the float '.5' while the
5854 ;; operator regexp only matches '.'.
5855 (or (not (looking-at c-nonsymbol-token-regexp))
5856 (<= (match-end 0) pos))))
5858 ;; There should either be a cast before it or something that isn't an
5859 ;; identifier or close paren.
5860 (> preceding-token-end (point-min))
5861 (progn
5862 (goto-char (1- preceding-token-end))
5863 (or (eq (point) last-cast-end)
5864 (progn
5865 (c-backward-syntactic-ws)
5866 (if (< (skip-syntax-backward "w_") 0)
5867 ;; It's a symbol. Accept it only if it's one of the
5868 ;; keywords that can precede an expression (without
5869 ;; surrounding parens).
5870 (looking-at c-simple-stmt-key)
5871 (and
5872 ;; Check that it isn't a close paren (block close is ok,
5873 ;; though).
5874 (not (memq (char-before) '(?\) ?\])))
5875 ;; Check that it isn't a nonsymbol identifier.
5876 (not (c-on-identifier)))))))))
5878 ;; Handle the cast.
5879 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5880 (let ((c-promote-possible-types t))
5881 (goto-char type-start)
5882 (c-forward-type)))
5884 (goto-char cast-end)
5885 'cast)
5887 (at-decl-or-cast
5888 ;; We're at a declaration. Highlight the type and the following
5889 ;; declarators.
5891 (when backup-if-not-cast
5892 (c-fdoc-shift-type-backward t))
5894 (when (and (eq context 'decl) (looking-at ","))
5895 ;; Make sure to propagate the `c-decl-arg-start' property to
5896 ;; the next argument if it's set in this one, to cope with
5897 ;; interactive refontification.
5898 (c-put-c-type-property (point) 'c-decl-arg-start))
5900 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5901 (let ((c-promote-possible-types t))
5902 (save-excursion
5903 (goto-char type-start)
5904 (c-forward-type))))
5906 (cons id-start at-type-decl))
5909 ;; False alarm. Restore the recorded ranges.
5910 (setq c-record-type-identifiers save-rec-type-ids
5911 c-record-ref-identifiers save-rec-ref-ids)
5912 nil))))
5914 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
5915 ;; Assuming that point is at the beginning of a token, check if it starts a
5916 ;; label and if so move over it and return non-nil (t in default situations,
5917 ;; specific symbols (see below) for interesting situations), otherwise don't
5918 ;; move and return nil. "Label" here means "most things with a colon".
5920 ;; More precisely, a "label" is regarded as one of:
5921 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
5922 ;; (ii) A case label - either the entire construct "case FOO:", or just the
5923 ;; bare "case", should the colon be missing. We return t;
5924 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
5925 ;; return t;
5926 ;; (iv) One of QT's "extended" C++ variants of
5927 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
5928 ;; Returns the symbol `qt-2kwds-colon'.
5929 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
5930 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
5931 ;; colon). Currently (2006-03), this applies only to Objective C's
5932 ;; keywords "@private", "@protected", and "@public". Returns t.
5934 ;; One of the things which will NOT be recognised as a label is a bit-field
5935 ;; element of a struct, something like "int foo:5".
5937 ;; The end of the label is taken to be just after the colon, or the end of
5938 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
5939 ;; after the end on return. The terminating char gets marked with
5940 ;; `c-decl-end' to improve recognition of the following declaration or
5941 ;; statement.
5943 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
5944 ;; label, if any, has already been marked up like that.
5946 ;; If PRECEDING-TOKEN-END is given, it should be the first position
5947 ;; after the preceding token, i.e. on the other side of the
5948 ;; syntactic ws from the point. Use a value less than or equal to
5949 ;; (point-min) if the point is at the first token in (the visible
5950 ;; part of) the buffer.
5952 ;; The optional LIMIT limits the forward scan for the colon.
5954 ;; This function records the ranges of the label symbols on
5955 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
5956 ;; non-nil.
5958 ;; This function might do hidden buffer changes.
5960 (let ((start (point))
5961 label-end
5962 qt-symbol-idx
5963 macro-start ; if we're in one.
5964 label-type
5965 kwd)
5966 (cond
5967 ;; "case" or "default" (Doesn't apply to AWK).
5968 ((looking-at c-label-kwds-regexp)
5969 (let ((kwd-end (match-end 1)))
5970 ;; Record only the keyword itself for fontification, since in
5971 ;; case labels the following is a constant expression and not
5972 ;; a label.
5973 (when c-record-type-identifiers
5974 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
5976 ;; Find the label end.
5977 (goto-char kwd-end)
5978 (setq label-type
5979 (if (and (c-syntactic-re-search-forward
5980 ;; Stop on chars that aren't allowed in expressions,
5981 ;; and on operator chars that would be meaningless
5982 ;; there. FIXME: This doesn't cope with ?: operators.
5983 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
5984 limit t t nil 1)
5985 (match-beginning 2))
5987 (progn ; there's a proper :
5988 (goto-char (match-beginning 2)) ; just after the :
5989 (c-put-c-type-property (1- (point)) 'c-decl-end)
5992 ;; It's an unfinished label. We consider the keyword enough
5993 ;; to recognize it as a label, so that it gets fontified.
5994 ;; Leave the point at the end of it, but don't put any
5995 ;; `c-decl-end' marker.
5996 (goto-char kwd-end)
5997 t))))
5999 ;; @private, @protected, @public, in Objective C, or similar.
6000 ((and c-opt-extra-label-key
6001 (looking-at c-opt-extra-label-key))
6002 ;; For a `c-opt-extra-label-key' match, we record the whole
6003 ;; thing for fontification. That's to get the leading '@' in
6004 ;; Objective-C protection labels fontified.
6005 (goto-char (match-end 1))
6006 (when c-record-type-identifiers
6007 (c-record-ref-id (cons (match-beginning 1) (point))))
6008 (c-put-c-type-property (1- (point)) 'c-decl-end)
6009 (setq label-type t))
6011 ;; All other cases of labels.
6012 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
6014 ;; A colon label must have something before the colon.
6015 (not (eq (char-after) ?:))
6017 ;; Check that we're not after a token that can't precede a label.
6019 ;; Trivially succeeds when there's no preceding token.
6020 (if preceding-token-end
6021 (<= preceding-token-end (point-min))
6022 (save-excursion
6023 (c-backward-syntactic-ws)
6024 (setq preceding-token-end (point))
6025 (bobp)))
6027 ;; Check if we're after a label, if we're after a closing
6028 ;; paren that belong to statement, and with
6029 ;; `c-label-prefix-re'. It's done in different order
6030 ;; depending on `assume-markup' since the checks have
6031 ;; different expensiveness.
6032 (if assume-markup
6034 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
6035 'c-decl-end)
6037 (save-excursion
6038 (goto-char (1- preceding-token-end))
6039 (c-beginning-of-current-token)
6040 (or (looking-at c-label-prefix-re)
6041 (looking-at c-block-stmt-1-key)))
6043 (and (eq (char-before preceding-token-end) ?\))
6044 (c-after-conditional)))
6047 (save-excursion
6048 (goto-char (1- preceding-token-end))
6049 (c-beginning-of-current-token)
6050 (or (looking-at c-label-prefix-re)
6051 (looking-at c-block-stmt-1-key)))
6053 (cond
6054 ((eq (char-before preceding-token-end) ?\))
6055 (c-after-conditional))
6057 ((eq (char-before preceding-token-end) ?:)
6058 ;; Might be after another label, so check it recursively.
6059 (save-restriction
6060 (save-excursion
6061 (goto-char (1- preceding-token-end))
6062 ;; Essentially the same as the
6063 ;; `c-syntactic-re-search-forward' regexp below.
6064 (setq macro-start
6065 (save-excursion (and (c-beginning-of-macro)
6066 (point))))
6067 (if macro-start (narrow-to-region macro-start (point-max)))
6068 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
6069 ;; Note: the following should work instead of the
6070 ;; narrow-to-region above. Investigate why not,
6071 ;; sometime. ACM, 2006-03-31.
6072 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
6073 ;; macro-start t)
6074 (let ((pte (point))
6075 ;; If the caller turned on recording for us,
6076 ;; it shouldn't apply when we check the
6077 ;; preceding label.
6078 c-record-type-identifiers)
6079 ;; A label can't start at a cpp directive. Check for
6080 ;; this, since c-forward-syntactic-ws would foul up on it.
6081 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
6082 (c-forward-syntactic-ws)
6083 (c-forward-label nil pte start))))))))))
6085 ;; Point is still at the beginning of the possible label construct.
6087 ;; Check that the next nonsymbol token is ":", or that we're in one
6088 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
6089 ;; arguments. FIXME: Should build this regexp from the language
6090 ;; constants.
6091 (cond
6092 ;; public: protected: private:
6093 ((and
6094 (c-major-mode-is 'c++-mode)
6095 (search-forward-regexp
6096 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
6097 (progn (backward-char)
6098 (c-forward-syntactic-ws limit)
6099 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
6100 (forward-char)
6101 (setq label-type t))
6102 ;; QT double keyword like "protected slots:" or goto target.
6103 ((progn (goto-char start) nil))
6104 ((when (c-syntactic-re-search-forward
6105 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
6106 (backward-char)
6107 (setq label-end (point))
6108 (setq qt-symbol-idx
6109 (and (c-major-mode-is 'c++-mode)
6110 (string-match
6111 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
6112 (buffer-substring start (point)))))
6113 (c-forward-syntactic-ws limit)
6114 (cond
6115 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
6116 (forward-char)
6117 (setq label-type
6118 (if (or (string= "signals" ; Special QT macro
6119 (setq kwd (buffer-substring-no-properties start label-end)))
6120 (string= "Q_SIGNALS" kwd))
6121 'qt-1kwd-colon
6122 'goto-target)))
6123 ((and qt-symbol-idx
6124 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
6125 (progn (c-forward-syntactic-ws limit)
6126 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
6127 (forward-char)
6128 (setq label-type 'qt-2kwds-colon)))))))
6130 (save-restriction
6131 (narrow-to-region start (point))
6133 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
6134 (catch 'check-label
6135 (goto-char start)
6136 (while (progn
6137 (when (looking-at c-nonlabel-token-key)
6138 (goto-char start)
6139 (setq label-type nil)
6140 (throw 'check-label nil))
6141 (and (c-safe (c-forward-sexp)
6142 (c-forward-syntactic-ws)
6144 (not (eobp)))))
6146 ;; Record the identifiers in the label for fontification, unless
6147 ;; it begins with `c-label-kwds' in which case the following
6148 ;; identifiers are part of a (constant) expression that
6149 ;; shouldn't be fontified.
6150 (when (and c-record-type-identifiers
6151 (progn (goto-char start)
6152 (not (looking-at c-label-kwds-regexp))))
6153 (while (c-syntactic-re-search-forward c-symbol-key nil t)
6154 (c-record-ref-id (cons (match-beginning 0)
6155 (match-end 0)))))
6157 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
6158 (goto-char (point-max)))))
6161 ;; Not a label.
6162 (goto-char start)))
6163 label-type))
6165 (defun c-forward-objc-directive ()
6166 ;; Assuming the point is at the beginning of a token, try to move
6167 ;; forward to the end of the Objective-C directive that starts
6168 ;; there. Return t if a directive was fully recognized, otherwise
6169 ;; the point is moved as far as one could be successfully parsed and
6170 ;; nil is returned.
6172 ;; This function records identifier ranges on
6173 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6174 ;; `c-record-type-identifiers' is non-nil.
6176 ;; This function might do hidden buffer changes.
6178 (let ((start (point))
6179 start-char
6180 (c-promote-possible-types t)
6181 ;; Turn off recognition of angle bracket arglists while parsing
6182 ;; types here since the protocol reference list might then be
6183 ;; considered part of the preceding name or superclass-name.
6184 c-recognize-<>-arglists)
6186 (if (or
6187 (when (looking-at
6188 (eval-when-compile
6189 (c-make-keywords-re t
6190 (append (c-lang-const c-protection-kwds objc)
6191 '("@end"))
6192 'objc-mode)))
6193 (goto-char (match-end 1))
6196 (and
6197 (looking-at
6198 (eval-when-compile
6199 (c-make-keywords-re t
6200 '("@interface" "@implementation" "@protocol")
6201 'objc-mode)))
6203 ;; Handle the name of the class itself.
6204 (progn
6205 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
6206 ; at EOB.
6207 (goto-char (match-end 0))
6208 (c-skip-ws-forward)
6209 (c-forward-type))
6211 (catch 'break
6212 ;; Look for ": superclass-name" or "( category-name )".
6213 (when (looking-at "[:\(]")
6214 (setq start-char (char-after))
6215 (forward-char)
6216 (c-forward-syntactic-ws)
6217 (unless (c-forward-type) (throw 'break nil))
6218 (when (eq start-char ?\()
6219 (unless (eq (char-after) ?\)) (throw 'break nil))
6220 (forward-char)
6221 (c-forward-syntactic-ws)))
6223 ;; Look for a protocol reference list.
6224 (if (eq (char-after) ?<)
6225 (let ((c-recognize-<>-arglists t)
6226 (c-parse-and-markup-<>-arglists t)
6227 c-restricted-<>-arglists)
6228 (c-forward-<>-arglist t))
6229 t))))
6231 (progn
6232 (c-backward-syntactic-ws)
6233 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
6234 (c-put-c-type-property (1- (point)) 'c-decl-end)
6237 (c-clear-c-type-property start (point) 'c-decl-end)
6238 nil)))
6240 (defun c-beginning-of-inheritance-list (&optional lim)
6241 ;; Go to the first non-whitespace after the colon that starts a
6242 ;; multiple inheritance introduction. Optional LIM is the farthest
6243 ;; back we should search.
6245 ;; This function might do hidden buffer changes.
6246 (c-with-syntax-table c++-template-syntax-table
6247 (c-backward-token-2 0 t lim)
6248 (while (and (or (looking-at c-symbol-start)
6249 (looking-at "[<,]\\|::"))
6250 (zerop (c-backward-token-2 1 t lim))))))
6252 (defun c-in-method-def-p ()
6253 ;; Return nil if we aren't in a method definition, otherwise the
6254 ;; position of the initial [+-].
6256 ;; This function might do hidden buffer changes.
6257 (save-excursion
6258 (beginning-of-line)
6259 (and c-opt-method-key
6260 (looking-at c-opt-method-key)
6261 (point))
6264 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
6265 (defun c-in-gcc-asm-p ()
6266 ;; Return non-nil if point is within a gcc \"asm\" block.
6268 ;; This should be called with point inside an argument list.
6270 ;; Only one level of enclosing parentheses is considered, so for
6271 ;; instance `nil' is returned when in a function call within an asm
6272 ;; operand.
6274 ;; This function might do hidden buffer changes.
6276 (and c-opt-asm-stmt-key
6277 (save-excursion
6278 (beginning-of-line)
6279 (backward-up-list 1)
6280 (c-beginning-of-statement-1 (point-min) nil t)
6281 (looking-at c-opt-asm-stmt-key))))
6283 (defun c-at-toplevel-p ()
6284 "Return a determination as to whether point is \"at the top level\".
6285 Informally, \"at the top level\" is anywhere where you can write
6286 a function.
6288 More precisely, being at the top-level means that point is either
6289 outside any enclosing block (such as a function definition), or
6290 directly inside a class, namespace or other block that contains
6291 another declaration level.
6293 If point is not at the top-level (e.g. it is inside a method
6294 definition), then nil is returned. Otherwise, if point is at a
6295 top-level not enclosed within a class definition, t is returned.
6296 Otherwise, a 2-vector is returned where the zeroth element is the
6297 buffer position of the start of the class declaration, and the first
6298 element is the buffer position of the enclosing class's opening
6299 brace.
6301 Note that this function might do hidden buffer changes. See the
6302 comment at the start of cc-engine.el for more info."
6303 (let ((paren-state (c-parse-state)))
6304 (or (not (c-most-enclosing-brace paren-state))
6305 (c-search-uplist-for-classkey paren-state))))
6307 (defun c-just-after-func-arglist-p (&optional lim)
6308 ;; Return non-nil if the point is in the region after the argument
6309 ;; list of a function and its opening brace (or semicolon in case it
6310 ;; got no body). If there are K&R style argument declarations in
6311 ;; that region, the point has to be inside the first one for this
6312 ;; function to recognize it.
6314 ;; If successful, the point is moved to the first token after the
6315 ;; function header (see `c-forward-decl-or-cast-1' for details) and
6316 ;; the position of the opening paren of the function arglist is
6317 ;; returned.
6319 ;; The point is clobbered if not successful.
6321 ;; LIM is used as bound for backward buffer searches.
6323 ;; This function might do hidden buffer changes.
6325 (let ((beg (point)) end id-start)
6326 (and
6327 (eq (c-beginning-of-statement-1 lim) 'same)
6329 (not (or (c-major-mode-is 'objc-mode)
6330 (c-forward-objc-directive)))
6332 (setq id-start
6333 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
6334 (< id-start beg)
6336 ;; There should not be a '=' or ',' between beg and the
6337 ;; start of the declaration since that means we were in the
6338 ;; "expression part" of the declaration.
6339 (or (> (point) beg)
6340 (not (looking-at "[=,]")))
6342 (save-excursion
6343 ;; Check that there's an arglist paren in the
6344 ;; declaration.
6345 (goto-char id-start)
6346 (cond ((eq (char-after) ?\()
6347 ;; The declarator is a paren expression, so skip past it
6348 ;; so that we don't get stuck on that instead of the
6349 ;; function arglist.
6350 (c-forward-sexp))
6351 ((and c-opt-op-identifier-prefix
6352 (looking-at c-opt-op-identifier-prefix))
6353 ;; Don't trip up on "operator ()".
6354 (c-forward-token-2 2 t)))
6355 (and (< (point) beg)
6356 (c-syntactic-re-search-forward "(" beg t t)
6357 (1- (point)))))))
6359 (defun c-in-knr-argdecl (&optional lim)
6360 ;; Return the position of the first argument declaration if point is
6361 ;; inside a K&R style argument declaration list, nil otherwise.
6362 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
6363 ;; position that bounds the backward search for the argument list.
6365 ;; Point must be within a possible K&R region, e.g. just before a top-level
6366 ;; "{". It must be outside of parens and brackets. The test can return
6367 ;; false positives otherwise.
6369 ;; This function might do hidden buffer changes.
6371 (save-excursion
6372 (save-restriction
6373 ;; If we're in a macro, our search range is restricted to it. Narrow to
6374 ;; the searchable range.
6375 (let* ((macro-start (c-query-macro-start))
6376 (lim (max (or lim (point-min)) (or macro-start (point-min))))
6377 before-lparen after-rparen
6378 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
6379 (narrow-to-region lim (c-point 'eol))
6381 ;; Search backwards for the defun's argument list. We give up if we
6382 ;; encounter a "}" (end of a previous defun) or BOB.
6384 ;; The criterion for a paren structure being the arg list is:
6385 ;; o - there is non-WS stuff after it but before any "{"; AND
6386 ;; o - the token after it isn't a ";" AND
6387 ;; o - it is preceded by either an identifier (the function name) or
6388 ;; a macro expansion like "DEFUN (...)"; AND
6389 ;; o - its content is a non-empty comma-separated list of identifiers
6390 ;; (an empty arg list won't have a knr region).
6392 ;; The following snippet illustrates these rules:
6393 ;; int foo (bar, baz, yuk)
6394 ;; int bar [] ;
6395 ;; int (*baz) (my_type) ;
6396 ;; int (*) (void) (*yuk) (void) ;
6397 ;; {
6399 (catch 'knr
6400 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
6401 (setq pp-count-out (1- pp-count-out))
6402 (c-syntactic-skip-backward "^)]}")
6403 (cond ((eq (char-before) ?\))
6404 (setq after-rparen (point)))
6405 ((eq (char-before) ?\])
6406 (setq after-rparen nil))
6407 (t ; either } (hit previous defun) or no more parens/brackets
6408 (throw 'knr nil)))
6410 (if after-rparen
6411 ;; We're inside a paren. Could it be our argument list....?
6413 (and
6414 (progn
6415 (goto-char after-rparen)
6416 (unless (c-go-list-backward) (throw 'knr nil)) ;
6417 ;; FIXME!!! What about macros between the parens? 2007/01/20
6418 (setq before-lparen (point)))
6420 ;; It can't be the arg list if next token is ; or {
6421 (progn (goto-char after-rparen)
6422 (c-forward-syntactic-ws)
6423 (not (memq (char-after) '(?\; ?\{))))
6425 ;; Is the thing preceding the list an identifier (the
6426 ;; function name), or a macro expansion?
6427 (progn
6428 (goto-char before-lparen)
6429 (eq (c-backward-token-2) 0)
6430 (or (c-on-identifier)
6431 (and (eq (char-after) ?\))
6432 (c-go-up-list-backward)
6433 (eq (c-backward-token-2) 0)
6434 (c-on-identifier))))
6436 ;; Have we got a non-empty list of comma-separated
6437 ;; identifiers?
6438 (progn
6439 (goto-char before-lparen)
6440 (c-forward-token-2) ; to first token inside parens
6441 (and
6442 (c-on-identifier)
6443 (c-forward-token-2)
6444 (catch 'id-list
6445 (while (eq (char-after) ?\,)
6446 (c-forward-token-2)
6447 (unless (c-on-identifier) (throw 'id-list nil))
6448 (c-forward-token-2))
6449 (eq (char-after) ?\))))))
6451 ;; ...Yes. We've identified the function's argument list.
6452 (throw 'knr
6453 (progn (goto-char after-rparen)
6454 (c-forward-syntactic-ws)
6455 (point)))
6457 ;; ...No. The current parens aren't the function's arg list.
6458 (goto-char before-lparen))
6460 (or (c-go-list-backward) ; backwards over [ .... ]
6461 (throw 'knr nil)))))))))
6463 (defun c-skip-conditional ()
6464 ;; skip forward over conditional at point, including any predicate
6465 ;; statements in parentheses. No error checking is performed.
6467 ;; This function might do hidden buffer changes.
6468 (c-forward-sexp (cond
6469 ;; else if()
6470 ((looking-at (concat "\\<else"
6471 "\\([ \t\n]\\|\\\\\n\\)+"
6472 "if\\>\\([^_]\\|$\\)"))
6474 ;; do, else, try, finally
6475 ((looking-at (concat "\\<\\("
6476 "do\\|else\\|try\\|finally"
6477 "\\)\\>\\([^_]\\|$\\)"))
6479 ;; for, if, while, switch, catch, synchronized, foreach
6480 (t 2))))
6482 (defun c-after-conditional (&optional lim)
6483 ;; If looking at the token after a conditional then return the
6484 ;; position of its start, otherwise return nil.
6486 ;; This function might do hidden buffer changes.
6487 (save-excursion
6488 (and (zerop (c-backward-token-2 1 t lim))
6489 (or (looking-at c-block-stmt-1-key)
6490 (and (eq (char-after) ?\()
6491 (zerop (c-backward-token-2 1 t lim))
6492 (looking-at c-block-stmt-2-key)))
6493 (point))))
6495 (defun c-after-special-operator-id (&optional lim)
6496 ;; If the point is after an operator identifier that isn't handled
6497 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
6498 ;; position of the start of that identifier is returned. nil is
6499 ;; returned otherwise. The point may be anywhere in the syntactic
6500 ;; whitespace after the last token of the operator identifier.
6502 ;; This function might do hidden buffer changes.
6503 (save-excursion
6504 (and c-overloadable-operators-regexp
6505 (zerop (c-backward-token-2 1 nil lim))
6506 (looking-at c-overloadable-operators-regexp)
6507 (or (not c-opt-op-identifier-prefix)
6508 (and
6509 (zerop (c-backward-token-2 1 nil lim))
6510 (looking-at c-opt-op-identifier-prefix)))
6511 (point))))
6513 (defsubst c-backward-to-block-anchor (&optional lim)
6514 ;; Assuming point is at a brace that opens a statement block of some
6515 ;; kind, move to the proper anchor point for that block. It might
6516 ;; need to be adjusted further by c-add-stmt-syntax, but the
6517 ;; position at return is suitable as start position for that
6518 ;; function.
6520 ;; This function might do hidden buffer changes.
6521 (unless (= (point) (c-point 'boi))
6522 (let ((start (c-after-conditional lim)))
6523 (if start
6524 (goto-char start)))))
6526 (defsubst c-backward-to-decl-anchor (&optional lim)
6527 ;; Assuming point is at a brace that opens the block of a top level
6528 ;; declaration of some kind, move to the proper anchor point for
6529 ;; that block.
6531 ;; This function might do hidden buffer changes.
6532 (unless (= (point) (c-point 'boi))
6533 (c-beginning-of-statement-1 lim)))
6535 (defun c-search-decl-header-end ()
6536 ;; Search forward for the end of the "header" of the current
6537 ;; declaration. That's the position where the definition body
6538 ;; starts, or the first variable initializer, or the ending
6539 ;; semicolon. I.e. search forward for the closest following
6540 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
6541 ;; _after_ the first found token, or at point-max if none is found.
6543 ;; This function might do hidden buffer changes.
6545 (let ((base (point)))
6546 (if (c-major-mode-is 'c++-mode)
6548 ;; In C++ we need to take special care to handle operator
6549 ;; tokens and those pesky template brackets.
6550 (while (and
6551 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
6553 (c-end-of-current-token base)
6554 ;; Handle operator identifiers, i.e. ignore any
6555 ;; operator token preceded by "operator".
6556 (save-excursion
6557 (and (c-safe (c-backward-sexp) t)
6558 (looking-at c-opt-op-identifier-prefix)))
6559 (and (eq (char-before) ?<)
6560 (c-with-syntax-table c++-template-syntax-table
6561 (if (c-safe (goto-char (c-up-list-forward (point))))
6563 (goto-char (point-max))
6564 nil)))))
6565 (setq base (point)))
6567 (while (and
6568 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
6569 (c-end-of-current-token base))
6570 (setq base (point))))))
6572 (defun c-beginning-of-decl-1 (&optional lim)
6573 ;; Go to the beginning of the current declaration, or the beginning
6574 ;; of the previous one if already at the start of it. Point won't
6575 ;; be moved out of any surrounding paren. Return a cons cell of the
6576 ;; form (MOVE . KNR-POS). MOVE is like the return value from
6577 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
6578 ;; style argument declarations (and they are to be recognized) then
6579 ;; KNR-POS is set to the start of the first such argument
6580 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
6581 ;; position that bounds the backward search.
6583 ;; NB: Cases where the declaration continues after the block, as in
6584 ;; "struct foo { ... } bar;", are currently recognized as two
6585 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
6587 ;; This function might do hidden buffer changes.
6588 (catch 'return
6589 (let* ((start (point))
6590 (last-stmt-start (point))
6591 (move (c-beginning-of-statement-1 lim nil t)))
6593 ;; `c-beginning-of-statement-1' stops at a block start, but we
6594 ;; want to continue if the block doesn't begin a top level
6595 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
6596 ;; or an open paren.
6597 (let ((beg (point)) tentative-move)
6598 ;; Go back one "statement" each time round the loop until we're just
6599 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
6600 ;; an ObjC method. This will move over a multiple declaration whose
6601 ;; components are comma separated.
6602 (while (and
6603 ;; Must check with c-opt-method-key in ObjC mode.
6604 (not (and c-opt-method-key
6605 (looking-at c-opt-method-key)))
6606 (/= last-stmt-start (point))
6607 (progn
6608 (c-backward-syntactic-ws lim)
6609 (not (memq (char-before) '(?\; ?} ?: nil))))
6610 (save-excursion
6611 (backward-char)
6612 (not (looking-at "\\s(")))
6613 ;; Check that we don't move from the first thing in a
6614 ;; macro to its header.
6615 (not (eq (setq tentative-move
6616 (c-beginning-of-statement-1 lim nil t))
6617 'macro)))
6618 (setq last-stmt-start beg
6619 beg (point)
6620 move tentative-move))
6621 (goto-char beg))
6623 (when c-recognize-knr-p
6624 (let ((fallback-pos (point)) knr-argdecl-start)
6625 ;; Handle K&R argdecls. Back up after the "statement" jumped
6626 ;; over by `c-beginning-of-statement-1', unless it was the
6627 ;; function body, in which case we're sitting on the opening
6628 ;; brace now. Then test if we're in a K&R argdecl region and
6629 ;; that we started at the other side of the first argdecl in
6630 ;; it.
6631 (unless (eq (char-after) ?{)
6632 (goto-char last-stmt-start))
6633 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
6634 (< knr-argdecl-start start)
6635 (progn
6636 (goto-char knr-argdecl-start)
6637 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
6638 (throw 'return
6639 (cons (if (eq (char-after fallback-pos) ?{)
6640 'previous
6641 'same)
6642 knr-argdecl-start))
6643 (goto-char fallback-pos))))
6645 ;; `c-beginning-of-statement-1' counts each brace block as a separate
6646 ;; statement, so the result will be 'previous if we've moved over any.
6647 ;; So change our result back to 'same if necessary.
6649 ;; If they were brace list initializers we might not have moved over a
6650 ;; declaration boundary though, so change it to 'same if we've moved
6651 ;; past a '=' before '{', but not ';'. (This ought to be integrated
6652 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
6653 ;; potentially can search over a large amount of text.). Take special
6654 ;; pains not to get mislead by C++'s "operator=", and the like.
6655 (if (and (eq move 'previous)
6656 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
6657 c++-template-syntax-table
6658 (syntax-table))
6659 (save-excursion
6660 (and
6661 (progn
6662 (while ; keep going back to "[;={"s until we either find
6663 ; no more, or get to one which isn't an "operator ="
6664 (and (c-syntactic-re-search-forward "[;={]" start t t t)
6665 (eq (char-before) ?=)
6666 c-overloadable-operators-regexp
6667 c-opt-op-identifier-prefix
6668 (save-excursion
6669 (eq (c-backward-token-2) 0)
6670 (looking-at c-overloadable-operators-regexp)
6671 (eq (c-backward-token-2) 0)
6672 (looking-at c-opt-op-identifier-prefix))))
6673 (eq (char-before) ?=))
6674 (c-syntactic-re-search-forward "[;{]" start t t)
6675 (eq (char-before) ?{)
6676 (c-safe (goto-char (c-up-list-forward (point))) t)
6677 (not (c-syntactic-re-search-forward ";" start t t))))))
6678 (cons 'same nil)
6679 (cons move nil)))))
6681 (defun c-end-of-decl-1 ()
6682 ;; Assuming point is at the start of a declaration (as detected by
6683 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
6684 ;; `c-beginning-of-decl-1', this function handles the case when a
6685 ;; block is followed by identifiers in e.g. struct declarations in C
6686 ;; or C++. If a proper end was found then t is returned, otherwise
6687 ;; point is moved as far as possible within the current sexp and nil
6688 ;; is returned. This function doesn't handle macros; use
6689 ;; `c-end-of-macro' instead in those cases.
6691 ;; This function might do hidden buffer changes.
6692 (let ((start (point))
6693 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
6694 c++-template-syntax-table
6695 (syntax-table))))
6696 (catch 'return
6697 (c-search-decl-header-end)
6699 (when (and c-recognize-knr-p
6700 (eq (char-before) ?\;)
6701 (c-in-knr-argdecl start))
6702 ;; Stopped at the ';' in a K&R argdecl section which is
6703 ;; detected using the same criteria as in
6704 ;; `c-beginning-of-decl-1'. Move to the following block
6705 ;; start.
6706 (c-syntactic-re-search-forward "{" nil 'move t))
6708 (when (eq (char-before) ?{)
6709 ;; Encountered a block in the declaration. Jump over it.
6710 (condition-case nil
6711 (goto-char (c-up-list-forward (point)))
6712 (error (goto-char (point-max))
6713 (throw 'return nil)))
6714 (if (or (not c-opt-block-decls-with-vars-key)
6715 (save-excursion
6716 (c-with-syntax-table decl-syntax-table
6717 (let ((lim (point)))
6718 (goto-char start)
6719 (not (and
6720 ;; Check for `c-opt-block-decls-with-vars-key'
6721 ;; before the first paren.
6722 (c-syntactic-re-search-forward
6723 (concat "[;=\(\[{]\\|\\("
6724 c-opt-block-decls-with-vars-key
6725 "\\)")
6726 lim t t t)
6727 (match-beginning 1)
6728 (not (eq (char-before) ?_))
6729 ;; Check that the first following paren is
6730 ;; the block.
6731 (c-syntactic-re-search-forward "[;=\(\[{]"
6732 lim t t t)
6733 (eq (char-before) ?{)))))))
6734 ;; The declaration doesn't have any of the
6735 ;; `c-opt-block-decls-with-vars' keywords in the
6736 ;; beginning, so it ends here at the end of the block.
6737 (throw 'return t)))
6739 (c-with-syntax-table decl-syntax-table
6740 (while (progn
6741 (if (eq (char-before) ?\;)
6742 (throw 'return t))
6743 (c-syntactic-re-search-forward ";" nil 'move t))))
6744 nil)))
6746 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
6747 ;; Assuming the point is at an open brace, check if it starts a
6748 ;; block that contains another declaration level, i.e. that isn't a
6749 ;; statement block or a brace list, and if so return non-nil.
6751 ;; If the check is successful, the return value is the start of the
6752 ;; keyword that tells what kind of construct it is, i.e. typically
6753 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
6754 ;; the point will be at the start of the construct, before any
6755 ;; leading specifiers, otherwise it's at the returned position.
6757 ;; The point is clobbered if the check is unsuccessful.
6759 ;; CONTAINING-SEXP is the position of the open of the surrounding
6760 ;; paren, or nil if none.
6762 ;; The optional LIMIT limits the backward search for the start of
6763 ;; the construct. It's assumed to be at a syntactically relevant
6764 ;; position.
6766 ;; If any template arglists are found in the searched region before
6767 ;; the open brace, they get marked with paren syntax.
6769 ;; This function might do hidden buffer changes.
6771 (let ((open-brace (point)) kwd-start first-specifier-pos)
6772 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6774 (when (and c-recognize-<>-arglists
6775 (eq (char-before) ?>))
6776 ;; Could be at the end of a template arglist.
6777 (let ((c-parse-and-markup-<>-arglists t)
6778 (c-disallow-comma-in-<>-arglists
6779 (and containing-sexp
6780 (not (eq (char-after containing-sexp) ?{)))))
6781 (while (and
6782 (c-backward-<>-arglist nil limit)
6783 (progn
6784 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6785 (eq (char-before) ?>))))))
6787 ;; Note: Can't get bogus hits inside template arglists below since they
6788 ;; have gotten paren syntax above.
6789 (when (and
6790 ;; If `goto-start' is set we begin by searching for the
6791 ;; first possible position of a leading specifier list.
6792 ;; The `c-decl-block-key' search continues from there since
6793 ;; we know it can't match earlier.
6794 (if goto-start
6795 (when (c-syntactic-re-search-forward c-symbol-start
6796 open-brace t t)
6797 (goto-char (setq first-specifier-pos (match-beginning 0)))
6801 (cond
6802 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
6803 (goto-char (setq kwd-start (match-beginning 0)))
6806 ;; Found a keyword that can't be a type?
6807 (match-beginning 1)
6809 ;; Can be a type too, in which case it's the return type of a
6810 ;; function (under the assumption that no declaration level
6811 ;; block construct starts with a type).
6812 (not (c-forward-type))
6814 ;; Jumped over a type, but it could be a declaration keyword
6815 ;; followed by the declared identifier that we've jumped over
6816 ;; instead (e.g. in "class Foo {"). If it indeed is a type
6817 ;; then we should be at the declarator now, so check for a
6818 ;; valid declarator start.
6820 ;; Note: This doesn't cope with the case when a declared
6821 ;; identifier is followed by e.g. '(' in a language where '('
6822 ;; also might be part of a declarator expression. Currently
6823 ;; there's no such language.
6824 (not (or (looking-at c-symbol-start)
6825 (looking-at c-type-decl-prefix-key)))))
6827 ;; In Pike a list of modifiers may be followed by a brace
6828 ;; to make them apply to many identifiers. Note that the
6829 ;; match data will be empty on return in this case.
6830 ((and (c-major-mode-is 'pike-mode)
6831 (progn
6832 (goto-char open-brace)
6833 (= (c-backward-token-2) 0))
6834 (looking-at c-specifier-key)
6835 ;; Use this variant to avoid yet another special regexp.
6836 (c-keyword-member (c-keyword-sym (match-string 1))
6837 'c-modifier-kwds))
6838 (setq kwd-start (point))
6839 t)))
6841 ;; Got a match.
6843 (if goto-start
6844 ;; Back up over any preceding specifiers and their clauses
6845 ;; by going forward from `first-specifier-pos', which is the
6846 ;; earliest possible position where the specifier list can
6847 ;; start.
6848 (progn
6849 (goto-char first-specifier-pos)
6851 (while (< (point) kwd-start)
6852 (if (looking-at c-symbol-key)
6853 ;; Accept any plain symbol token on the ground that
6854 ;; it's a specifier masked through a macro (just
6855 ;; like `c-forward-decl-or-cast-1' skip forward over
6856 ;; such tokens).
6858 ;; Could be more restrictive wrt invalid keywords,
6859 ;; but that'd only occur in invalid code so there's
6860 ;; no use spending effort on it.
6861 (let ((end (match-end 0)))
6862 (unless (c-forward-keyword-clause 0)
6863 (goto-char end)
6864 (c-forward-syntactic-ws)))
6866 ;; Can't parse a declaration preamble and is still
6867 ;; before `kwd-start'. That means `first-specifier-pos'
6868 ;; was in some earlier construct. Search again.
6869 (if (c-syntactic-re-search-forward c-symbol-start
6870 kwd-start 'move t)
6871 (goto-char (setq first-specifier-pos (match-beginning 0)))
6872 ;; Got no preamble before the block declaration keyword.
6873 (setq first-specifier-pos kwd-start))))
6875 (goto-char first-specifier-pos))
6876 (goto-char kwd-start))
6878 kwd-start)))
6880 (defun c-search-uplist-for-classkey (paren-state)
6881 ;; Check if the closest containing paren sexp is a declaration
6882 ;; block, returning a 2 element vector in that case. Aref 0
6883 ;; contains the bufpos at boi of the class key line, and aref 1
6884 ;; contains the bufpos of the open brace. This function is an
6885 ;; obsolete wrapper for `c-looking-at-decl-block'.
6887 ;; This function might do hidden buffer changes.
6888 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
6889 (when open-paren-pos
6890 (save-excursion
6891 (goto-char open-paren-pos)
6892 (when (and (eq (char-after) ?{)
6893 (c-looking-at-decl-block
6894 (c-safe-position open-paren-pos paren-state)
6895 nil))
6896 (back-to-indentation)
6897 (vector (point) open-paren-pos))))))
6899 (defun c-inside-bracelist-p (containing-sexp paren-state)
6900 ;; return the buffer position of the beginning of the brace list
6901 ;; statement if we're inside a brace list, otherwise return nil.
6902 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
6903 ;; paren. PAREN-STATE is the remainder of the state of enclosing
6904 ;; braces
6906 ;; N.B.: This algorithm can potentially get confused by cpp macros
6907 ;; placed in inconvenient locations. It's a trade-off we make for
6908 ;; speed.
6910 ;; This function might do hidden buffer changes.
6912 ;; This will pick up brace list declarations.
6913 (c-safe
6914 (save-excursion
6915 (goto-char containing-sexp)
6916 (c-forward-sexp -1)
6917 (let (bracepos)
6918 (if (and (or (looking-at c-brace-list-key)
6919 (progn (c-forward-sexp -1)
6920 (looking-at c-brace-list-key)))
6921 (setq bracepos (c-down-list-forward (point)))
6922 (not (c-crosses-statement-barrier-p (point)
6923 (- bracepos 2))))
6924 (point)))))
6925 ;; this will pick up array/aggregate init lists, even if they are nested.
6926 (save-excursion
6927 (let ((class-key
6928 ;; Pike can have class definitions anywhere, so we must
6929 ;; check for the class key here.
6930 (and (c-major-mode-is 'pike-mode)
6931 c-decl-block-key))
6932 bufpos braceassignp lim next-containing)
6933 (while (and (not bufpos)
6934 containing-sexp)
6935 (when paren-state
6936 (if (consp (car paren-state))
6937 (setq lim (cdr (car paren-state))
6938 paren-state (cdr paren-state))
6939 (setq lim (car paren-state)))
6940 (when paren-state
6941 (setq next-containing (car paren-state)
6942 paren-state (cdr paren-state))))
6943 (goto-char containing-sexp)
6944 (if (c-looking-at-inexpr-block next-containing next-containing)
6945 ;; We're in an in-expression block of some kind. Do not
6946 ;; check nesting. We deliberately set the limit to the
6947 ;; containing sexp, so that c-looking-at-inexpr-block
6948 ;; doesn't check for an identifier before it.
6949 (setq containing-sexp nil)
6950 ;; see if the open brace is preceded by = or [...] in
6951 ;; this statement, but watch out for operator=
6952 (setq braceassignp 'dontknow)
6953 (c-backward-token-2 1 t lim)
6954 ;; Checks to do only on the first sexp before the brace.
6955 (when (and c-opt-inexpr-brace-list-key
6956 (eq (char-after) ?\[))
6957 ;; In Java, an initialization brace list may follow
6958 ;; directly after "new Foo[]", so check for a "new"
6959 ;; earlier.
6960 (while (eq braceassignp 'dontknow)
6961 (setq braceassignp
6962 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
6963 ((looking-at c-opt-inexpr-brace-list-key) t)
6964 ((looking-at "\\sw\\|\\s_\\|[.[]")
6965 ;; Carry on looking if this is an
6966 ;; identifier (may contain "." in Java)
6967 ;; or another "[]" sexp.
6968 'dontknow)
6969 (t nil)))))
6970 ;; Checks to do on all sexps before the brace, up to the
6971 ;; beginning of the statement.
6972 (while (eq braceassignp 'dontknow)
6973 (cond ((eq (char-after) ?\;)
6974 (setq braceassignp nil))
6975 ((and class-key
6976 (looking-at class-key))
6977 (setq braceassignp nil))
6978 ((eq (char-after) ?=)
6979 ;; We've seen a =, but must check earlier tokens so
6980 ;; that it isn't something that should be ignored.
6981 (setq braceassignp 'maybe)
6982 (while (and (eq braceassignp 'maybe)
6983 (zerop (c-backward-token-2 1 t lim)))
6984 (setq braceassignp
6985 (cond
6986 ;; Check for operator =
6987 ((and c-opt-op-identifier-prefix
6988 (looking-at c-opt-op-identifier-prefix))
6989 nil)
6990 ;; Check for `<opchar>= in Pike.
6991 ((and (c-major-mode-is 'pike-mode)
6992 (or (eq (char-after) ?`)
6993 ;; Special case for Pikes
6994 ;; `[]=, since '[' is not in
6995 ;; the punctuation class.
6996 (and (eq (char-after) ?\[)
6997 (eq (char-before) ?`))))
6998 nil)
6999 ((looking-at "\\s.") 'maybe)
7000 ;; make sure we're not in a C++ template
7001 ;; argument assignment
7002 ((and
7003 (c-major-mode-is 'c++-mode)
7004 (save-excursion
7005 (let ((here (point))
7006 (pos< (progn
7007 (skip-chars-backward "^<>")
7008 (point))))
7009 (and (eq (char-before) ?<)
7010 (not (c-crosses-statement-barrier-p
7011 pos< here))
7012 (not (c-in-literal))
7013 ))))
7014 nil)
7015 (t t))))))
7016 (if (and (eq braceassignp 'dontknow)
7017 (/= (c-backward-token-2 1 t lim) 0))
7018 (setq braceassignp nil)))
7019 (if (not braceassignp)
7020 (if (eq (char-after) ?\;)
7021 ;; Brace lists can't contain a semicolon, so we're done.
7022 (setq containing-sexp nil)
7023 ;; Go up one level.
7024 (setq containing-sexp next-containing
7025 lim nil
7026 next-containing nil))
7027 ;; we've hit the beginning of the aggregate list
7028 (c-beginning-of-statement-1
7029 (c-most-enclosing-brace paren-state))
7030 (setq bufpos (point))))
7032 bufpos))
7035 (defun c-looking-at-special-brace-list (&optional lim)
7036 ;; If we're looking at the start of a pike-style list, ie `({ })',
7037 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
7038 ;; positions and its entry in c-special-brace-lists is returned, nil
7039 ;; otherwise. The ending position is nil if the list is still open.
7040 ;; LIM is the limit for forward search. The point may either be at
7041 ;; the `(' or at the following paren character. Tries to check the
7042 ;; matching closer, but assumes it's correct if no balanced paren is
7043 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
7044 ;; a special brace list).
7046 ;; This function might do hidden buffer changes.
7047 (if c-special-brace-lists
7048 (condition-case ()
7049 (save-excursion
7050 (let ((beg (point))
7051 inner-beg end type)
7052 (c-forward-syntactic-ws)
7053 (if (eq (char-after) ?\()
7054 (progn
7055 (forward-char 1)
7056 (c-forward-syntactic-ws)
7057 (setq inner-beg (point))
7058 (setq type (assq (char-after) c-special-brace-lists)))
7059 (if (setq type (assq (char-after) c-special-brace-lists))
7060 (progn
7061 (setq inner-beg (point))
7062 (c-backward-syntactic-ws)
7063 (forward-char -1)
7064 (setq beg (if (eq (char-after) ?\()
7065 (point)
7066 nil)))))
7067 (if (and beg type)
7068 (if (and (c-safe
7069 (goto-char beg)
7070 (c-forward-sexp 1)
7071 (setq end (point))
7072 (= (char-before) ?\)))
7073 (c-safe
7074 (goto-char inner-beg)
7075 (if (looking-at "\\s(")
7076 ;; Check balancing of the inner paren
7077 ;; below.
7078 (progn
7079 (c-forward-sexp 1)
7081 ;; If the inner char isn't a paren then
7082 ;; we can't check balancing, so just
7083 ;; check the char before the outer
7084 ;; closing paren.
7085 (goto-char end)
7086 (backward-char)
7087 (c-backward-syntactic-ws)
7088 (= (char-before) (cdr type)))))
7089 (if (or (/= (char-syntax (char-before)) ?\))
7090 (= (progn
7091 (c-forward-syntactic-ws)
7092 (point))
7093 (1- end)))
7094 (cons (cons beg end) type))
7095 (cons (list beg) type)))))
7096 (error nil))))
7098 (defun c-looking-at-bos (&optional lim)
7099 ;; Return non-nil if between two statements or declarations, assuming
7100 ;; point is not inside a literal or comment.
7102 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
7103 ;; are recommended instead.
7105 ;; This function might do hidden buffer changes.
7106 (c-at-statement-start-p))
7107 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
7109 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
7110 ;; Return non-nil if we're looking at the beginning of a block
7111 ;; inside an expression. The value returned is actually a cons of
7112 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
7113 ;; position of the beginning of the construct.
7115 ;; LIM limits the backward search. CONTAINING-SEXP is the start
7116 ;; position of the closest containing list. If it's nil, the
7117 ;; containing paren isn't used to decide whether we're inside an
7118 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
7119 ;; needs to be farther back.
7121 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
7122 ;; brace block might be done. It should only be used when the
7123 ;; construct can be assumed to be complete, i.e. when the original
7124 ;; starting position was further down than that.
7126 ;; This function might do hidden buffer changes.
7128 (save-excursion
7129 (let ((res 'maybe) passed-paren
7130 (closest-lim (or containing-sexp lim (point-min)))
7131 ;; Look at the character after point only as a last resort
7132 ;; when we can't disambiguate.
7133 (block-follows (and (eq (char-after) ?{) (point))))
7135 (while (and (eq res 'maybe)
7136 (progn (c-backward-syntactic-ws)
7137 (> (point) closest-lim))
7138 (not (bobp))
7139 (progn (backward-char)
7140 (looking-at "[\]\).]\\|\\w\\|\\s_"))
7141 (c-safe (forward-char)
7142 (goto-char (scan-sexps (point) -1))))
7144 (setq res
7145 (if (looking-at c-keywords-regexp)
7146 (let ((kw-sym (c-keyword-sym (match-string 1))))
7147 (cond
7148 ((and block-follows
7149 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
7150 (and (not (eq passed-paren ?\[))
7151 (or (not (looking-at c-class-key))
7152 ;; If the class definition is at the start of
7153 ;; a statement, we don't consider it an
7154 ;; in-expression class.
7155 (let ((prev (point)))
7156 (while (and
7157 (= (c-backward-token-2 1 nil closest-lim) 0)
7158 (eq (char-syntax (char-after)) ?w))
7159 (setq prev (point)))
7160 (goto-char prev)
7161 (not (c-at-statement-start-p)))
7162 ;; Also, in Pike we treat it as an
7163 ;; in-expression class if it's used in an
7164 ;; object clone expression.
7165 (save-excursion
7166 (and check-at-end
7167 (c-major-mode-is 'pike-mode)
7168 (progn (goto-char block-follows)
7169 (zerop (c-forward-token-2 1 t)))
7170 (eq (char-after) ?\())))
7171 (cons 'inexpr-class (point))))
7172 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
7173 (when (not passed-paren)
7174 (cons 'inexpr-statement (point))))
7175 ((c-keyword-member kw-sym 'c-lambda-kwds)
7176 (when (or (not passed-paren)
7177 (eq passed-paren ?\())
7178 (cons 'inlambda (point))))
7179 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
7180 nil)
7182 'maybe)))
7184 (if (looking-at "\\s(")
7185 (if passed-paren
7186 (if (and (eq passed-paren ?\[)
7187 (eq (char-after) ?\[))
7188 ;; Accept several square bracket sexps for
7189 ;; Java array initializations.
7190 'maybe)
7191 (setq passed-paren (char-after))
7192 'maybe)
7193 'maybe))))
7195 (if (eq res 'maybe)
7196 (when (and c-recognize-paren-inexpr-blocks
7197 block-follows
7198 containing-sexp
7199 (eq (char-after containing-sexp) ?\())
7200 (goto-char containing-sexp)
7201 (if (or (save-excursion
7202 (c-backward-syntactic-ws lim)
7203 (and (> (point) (or lim (point-min)))
7204 (c-on-identifier)))
7205 (and c-special-brace-lists
7206 (c-looking-at-special-brace-list)))
7208 (cons 'inexpr-statement (point))))
7210 res))))
7212 (defun c-looking-at-inexpr-block-backward (paren-state)
7213 ;; Returns non-nil if we're looking at the end of an in-expression
7214 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
7215 ;; PAREN-STATE is the paren state relevant at the current position.
7217 ;; This function might do hidden buffer changes.
7218 (save-excursion
7219 ;; We currently only recognize a block.
7220 (let ((here (point))
7221 (elem (car-safe paren-state))
7222 containing-sexp)
7223 (when (and (consp elem)
7224 (progn (goto-char (cdr elem))
7225 (c-forward-syntactic-ws here)
7226 (= (point) here)))
7227 (goto-char (car elem))
7228 (if (setq paren-state (cdr paren-state))
7229 (setq containing-sexp (car-safe paren-state)))
7230 (c-looking-at-inexpr-block (c-safe-position containing-sexp
7231 paren-state)
7232 containing-sexp)))))
7235 ;; `c-guess-basic-syntax' and the functions that precedes it below
7236 ;; implements the main decision tree for determining the syntactic
7237 ;; analysis of the current line of code.
7239 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
7240 ;; auto newline analysis.
7241 (defvar c-auto-newline-analysis nil)
7243 (defun c-brace-anchor-point (bracepos)
7244 ;; BRACEPOS is the position of a brace in a construct like "namespace
7245 ;; Bar {". Return the anchor point in this construct; this is the
7246 ;; earliest symbol on the brace's line which isn't earlier than
7247 ;; "namespace".
7249 ;; Currently (2007-08-17), "like namespace" means "matches
7250 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
7251 ;; or anything like that.
7252 (save-excursion
7253 (let ((boi (c-point 'boi bracepos)))
7254 (goto-char bracepos)
7255 (while (and (> (point) boi)
7256 (not (looking-at c-other-decl-block-key)))
7257 (c-backward-token-2))
7258 (if (> (point) boi) (point) boi))))
7260 (defsubst c-add-syntax (symbol &rest args)
7261 ;; A simple function to prepend a new syntax element to
7262 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
7263 ;; should always be dynamically bound but since we read it first
7264 ;; we'll fail properly anyway if this function is misused.
7265 (setq c-syntactic-context (cons (cons symbol args)
7266 c-syntactic-context)))
7268 (defsubst c-append-syntax (symbol &rest args)
7269 ;; Like `c-add-syntax' but appends to the end of the syntax list.
7270 ;; (Normally not necessary.)
7271 (setq c-syntactic-context (nconc c-syntactic-context
7272 (list (cons symbol args)))))
7274 (defun c-add-stmt-syntax (syntax-symbol
7275 syntax-extra-args
7276 stop-at-boi-only
7277 containing-sexp
7278 paren-state)
7279 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
7280 ;; needed with further syntax elements of the types `substatement',
7281 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
7282 ;; `defun-block-intro'.
7284 ;; Do the generic processing to anchor the given syntax symbol on
7285 ;; the preceding statement: Skip over any labels and containing
7286 ;; statements on the same line, and then search backward until we
7287 ;; find a statement or block start that begins at boi without a
7288 ;; label or comment.
7290 ;; Point is assumed to be at the prospective anchor point for the
7291 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
7292 ;; skip past open parens and containing statements. Most of the added
7293 ;; syntax elements will get the same anchor point - the exception is
7294 ;; for an anchor in a construct like "namespace"[*] - this is as early
7295 ;; as possible in the construct but on the same line as the {.
7297 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
7299 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
7300 ;; syntax symbol. They are appended after the anchor point.
7302 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
7303 ;; if the current statement starts there.
7305 ;; Note: It's not a problem if PAREN-STATE "overshoots"
7306 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
7308 ;; This function might do hidden buffer changes.
7310 (if (= (point) (c-point 'boi))
7311 ;; This is by far the most common case, so let's give it special
7312 ;; treatment.
7313 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
7315 (let ((syntax-last c-syntactic-context)
7316 (boi (c-point 'boi))
7317 ;; Set when we're on a label, so that we don't stop there.
7318 ;; FIXME: To be complete we should check if we're on a label
7319 ;; now at the start.
7320 on-label)
7322 ;; Use point as the anchor point for "namespace", "extern", etc.
7323 (apply 'c-add-syntax syntax-symbol
7324 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
7325 (point) nil)
7326 syntax-extra-args)
7328 ;; Loop while we have to back out of containing blocks.
7329 (while
7330 (and
7331 (catch 'back-up-block
7333 ;; Loop while we have to back up statements.
7334 (while (or (/= (point) boi)
7335 on-label
7336 (looking-at c-comment-start-regexp))
7338 ;; Skip past any comments that stands between the
7339 ;; statement start and boi.
7340 (let ((savepos (point)))
7341 (while (and (/= savepos boi)
7342 (c-backward-single-comment))
7343 (setq savepos (point)
7344 boi (c-point 'boi)))
7345 (goto-char savepos))
7347 ;; Skip to the beginning of this statement or backward
7348 ;; another one.
7349 (let ((old-pos (point))
7350 (old-boi boi)
7351 (step-type (c-beginning-of-statement-1 containing-sexp)))
7352 (setq boi (c-point 'boi)
7353 on-label (eq step-type 'label))
7355 (cond ((= (point) old-pos)
7356 ;; If we didn't move we're at the start of a block and
7357 ;; have to continue outside it.
7358 (throw 'back-up-block t))
7360 ((and (eq step-type 'up)
7361 (>= (point) old-boi)
7362 (looking-at "else\\>[^_]")
7363 (save-excursion
7364 (goto-char old-pos)
7365 (looking-at "if\\>[^_]")))
7366 ;; Special case to avoid deeper and deeper indentation
7367 ;; of "else if" clauses.
7370 ((and (not stop-at-boi-only)
7371 (/= old-pos old-boi)
7372 (memq step-type '(up previous)))
7373 ;; If stop-at-boi-only is nil, we shouldn't back up
7374 ;; over previous or containing statements to try to
7375 ;; reach boi, so go back to the last position and
7376 ;; exit.
7377 (goto-char old-pos)
7378 (throw 'back-up-block nil))
7381 (if (and (not stop-at-boi-only)
7382 (memq step-type '(up previous beginning)))
7383 ;; If we've moved into another statement then we
7384 ;; should no longer try to stop in the middle of a
7385 ;; line.
7386 (setq stop-at-boi-only t))
7388 ;; Record this as a substatement if we skipped up one
7389 ;; level.
7390 (when (eq step-type 'up)
7391 (c-add-syntax 'substatement nil))))
7394 containing-sexp)
7396 ;; Now we have to go out of this block.
7397 (goto-char containing-sexp)
7399 ;; Don't stop in the middle of a special brace list opener
7400 ;; like "({".
7401 (when c-special-brace-lists
7402 (let ((special-list (c-looking-at-special-brace-list)))
7403 (when (and special-list
7404 (< (car (car special-list)) (point)))
7405 (setq containing-sexp (car (car special-list)))
7406 (goto-char containing-sexp))))
7408 (setq paren-state (c-whack-state-after containing-sexp paren-state)
7409 containing-sexp (c-most-enclosing-brace paren-state)
7410 boi (c-point 'boi))
7412 ;; Analyze the construct in front of the block we've stepped out
7413 ;; from and add the right syntactic element for it.
7414 (let ((paren-pos (point))
7415 (paren-char (char-after))
7416 step-type)
7418 (if (eq paren-char ?\()
7419 ;; Stepped out of a parenthesis block, so we're in an
7420 ;; expression now.
7421 (progn
7422 (when (/= paren-pos boi)
7423 (if (and c-recognize-paren-inexpr-blocks
7424 (progn
7425 (c-backward-syntactic-ws containing-sexp)
7426 (or (not (looking-at "\\>"))
7427 (not (c-on-identifier))))
7428 (save-excursion
7429 (goto-char (1+ paren-pos))
7430 (c-forward-syntactic-ws)
7431 (eq (char-after) ?{)))
7432 ;; Stepped out of an in-expression statement. This
7433 ;; syntactic element won't get an anchor pos.
7434 (c-add-syntax 'inexpr-statement)
7436 ;; A parenthesis normally belongs to an arglist.
7437 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
7439 (goto-char (max boi
7440 (if containing-sexp
7441 (1+ containing-sexp)
7442 (point-min))))
7443 (setq step-type 'same
7444 on-label nil))
7446 ;; Stepped out of a brace block.
7447 (setq step-type (c-beginning-of-statement-1 containing-sexp)
7448 on-label (eq step-type 'label))
7450 (if (and (eq step-type 'same)
7451 (/= paren-pos (point)))
7452 (let (inexpr)
7453 (cond
7454 ((save-excursion
7455 (goto-char paren-pos)
7456 (setq inexpr (c-looking-at-inexpr-block
7457 (c-safe-position containing-sexp paren-state)
7458 containing-sexp)))
7459 (c-add-syntax (if (eq (car inexpr) 'inlambda)
7460 'defun-block-intro
7461 'statement-block-intro)
7462 nil))
7463 ((looking-at c-other-decl-block-key)
7464 (c-add-syntax
7465 (cdr (assoc (match-string 1)
7466 c-other-decl-block-key-in-symbols-alist))
7467 (max (c-point 'boi paren-pos) (point))))
7468 (t (c-add-syntax 'defun-block-intro nil))))
7470 (c-add-syntax 'statement-block-intro nil)))
7472 (if (= paren-pos boi)
7473 ;; Always done if the open brace was at boi. The
7474 ;; c-beginning-of-statement-1 call above is necessary
7475 ;; anyway, to decide the type of block-intro to add.
7476 (goto-char paren-pos)
7477 (setq boi (c-point 'boi)))
7480 ;; Fill in the current point as the anchor for all the symbols
7481 ;; added above.
7482 (let ((p c-syntactic-context) q)
7483 (while (not (eq p syntax-last))
7484 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
7485 (while q
7486 (unless (car q)
7487 (setcar q (point)))
7488 (setq q (cdr q)))
7489 (setq p (cdr p))))
7492 (defun c-add-class-syntax (symbol
7493 containing-decl-open
7494 containing-decl-start
7495 containing-decl-kwd
7496 paren-state)
7497 ;; The inclass and class-close syntactic symbols are added in
7498 ;; several places and some work is needed to fix everything.
7499 ;; Therefore it's collected here.
7501 ;; This function might do hidden buffer changes.
7502 (goto-char containing-decl-open)
7503 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
7504 (progn
7505 (c-add-syntax symbol containing-decl-open)
7506 containing-decl-open)
7507 (goto-char containing-decl-start)
7508 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
7509 ;; here, but we have to do like this for compatibility.
7510 (back-to-indentation)
7511 (c-add-syntax symbol (point))
7512 (if (and (c-keyword-member containing-decl-kwd
7513 'c-inexpr-class-kwds)
7514 (/= containing-decl-start (c-point 'boi containing-decl-start)))
7515 (c-add-syntax 'inexpr-class))
7516 (point)))
7518 (defun c-guess-continued-construct (indent-point
7519 char-after-ip
7520 beg-of-same-or-containing-stmt
7521 containing-sexp
7522 paren-state)
7523 ;; This function contains the decision tree reached through both
7524 ;; cases 18 and 10. It's a continued statement or top level
7525 ;; construct of some kind.
7527 ;; This function might do hidden buffer changes.
7529 (let (special-brace-list)
7530 (goto-char indent-point)
7531 (skip-chars-forward " \t")
7533 (cond
7534 ;; (CASE A removed.)
7535 ;; CASE B: open braces for class or brace-lists
7536 ((setq special-brace-list
7537 (or (and c-special-brace-lists
7538 (c-looking-at-special-brace-list))
7539 (eq char-after-ip ?{)))
7541 (cond
7542 ;; CASE B.1: class-open
7543 ((save-excursion
7544 (and (eq (char-after) ?{)
7545 (c-looking-at-decl-block containing-sexp t)
7546 (setq beg-of-same-or-containing-stmt (point))))
7547 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
7549 ;; CASE B.2: brace-list-open
7550 ((or (consp special-brace-list)
7551 (save-excursion
7552 (goto-char beg-of-same-or-containing-stmt)
7553 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
7554 indent-point t t t)))
7555 ;; The most semantically accurate symbol here is
7556 ;; brace-list-open, but we normally report it simply as a
7557 ;; statement-cont. The reason is that one normally adjusts
7558 ;; brace-list-open for brace lists as top-level constructs,
7559 ;; and brace lists inside statements is a completely different
7560 ;; context. C.f. case 5A.3.
7561 (c-beginning-of-statement-1 containing-sexp)
7562 (c-add-stmt-syntax (if c-auto-newline-analysis
7563 ;; Turn off the dwim above when we're
7564 ;; analyzing the nature of the brace
7565 ;; for the auto newline feature.
7566 'brace-list-open
7567 'statement-cont)
7568 nil nil
7569 containing-sexp paren-state))
7571 ;; CASE B.3: The body of a function declared inside a normal
7572 ;; block. Can occur e.g. in Pike and when using gcc
7573 ;; extensions, but watch out for macros followed by blocks.
7574 ;; C.f. cases E, 16F and 17G.
7575 ((and (not (c-at-statement-start-p))
7576 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7577 'same)
7578 (save-excursion
7579 (let ((c-recognize-typeless-decls nil))
7580 ;; Turn off recognition of constructs that lacks a
7581 ;; type in this case, since that's more likely to be
7582 ;; a macro followed by a block.
7583 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7584 (c-add-stmt-syntax 'defun-open nil t
7585 containing-sexp paren-state))
7587 ;; CASE B.4: Continued statement with block open. The most
7588 ;; accurate analysis is perhaps `statement-cont' together with
7589 ;; `block-open' but we play DWIM and use `substatement-open'
7590 ;; instead. The rationaly is that this typically is a macro
7591 ;; followed by a block which makes it very similar to a
7592 ;; statement with a substatement block.
7594 (c-add-stmt-syntax 'substatement-open nil nil
7595 containing-sexp paren-state))
7598 ;; CASE C: iostream insertion or extraction operator
7599 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
7600 (save-excursion
7601 (goto-char beg-of-same-or-containing-stmt)
7602 ;; If there is no preceding streamop in the statement
7603 ;; then indent this line as a normal statement-cont.
7604 (when (c-syntactic-re-search-forward
7605 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
7606 (c-add-syntax 'stream-op (c-point 'boi))
7607 t))))
7609 ;; CASE E: In the "K&R region" of a function declared inside a
7610 ;; normal block. C.f. case B.3.
7611 ((and (save-excursion
7612 ;; Check that the next token is a '{'. This works as
7613 ;; long as no language that allows nested function
7614 ;; definitions allows stuff like member init lists, K&R
7615 ;; declarations or throws clauses there.
7617 ;; Note that we do a forward search for something ahead
7618 ;; of the indentation line here. That's not good since
7619 ;; the user might not have typed it yet. Unfortunately
7620 ;; it's exceedingly tricky to recognize a function
7621 ;; prototype in a code block without resorting to this.
7622 (c-forward-syntactic-ws)
7623 (eq (char-after) ?{))
7624 (not (c-at-statement-start-p))
7625 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7626 'same)
7627 (save-excursion
7628 (let ((c-recognize-typeless-decls nil))
7629 ;; Turn off recognition of constructs that lacks a
7630 ;; type in this case, since that's more likely to be
7631 ;; a macro followed by a block.
7632 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7633 (c-add-stmt-syntax 'func-decl-cont nil t
7634 containing-sexp paren-state))
7636 ;; CASE D: continued statement.
7638 (c-beginning-of-statement-1 containing-sexp)
7639 (c-add-stmt-syntax 'statement-cont nil nil
7640 containing-sexp paren-state))
7643 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
7644 ;; 2005/11/29).
7645 ;;;###autoload
7646 (defun c-guess-basic-syntax ()
7647 "Return the syntactic context of the current line."
7648 (save-excursion
7649 (beginning-of-line)
7650 (c-save-buffer-state
7651 ((indent-point (point))
7652 (case-fold-search nil)
7653 ;; A whole ugly bunch of various temporary variables. Have
7654 ;; to declare them here since it's not possible to declare
7655 ;; a variable with only the scope of a cond test and the
7656 ;; following result clauses, and most of this function is a
7657 ;; single gigantic cond. :P
7658 literal char-before-ip before-ws-ip char-after-ip macro-start
7659 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
7660 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
7661 ;; The following record some positions for the containing
7662 ;; declaration block if we're directly within one:
7663 ;; `containing-decl-open' is the position of the open
7664 ;; brace. `containing-decl-start' is the start of the
7665 ;; declaration. `containing-decl-kwd' is the keyword
7666 ;; symbol of the keyword that tells what kind of block it
7667 ;; is.
7668 containing-decl-open
7669 containing-decl-start
7670 containing-decl-kwd
7671 ;; The open paren of the closest surrounding sexp or nil if
7672 ;; there is none.
7673 containing-sexp
7674 ;; The position after the closest preceding brace sexp
7675 ;; (nested sexps are ignored), or the position after
7676 ;; `containing-sexp' if there is none, or (point-min) if
7677 ;; `containing-sexp' is nil.
7679 ;; The paren state outside `containing-sexp', or at
7680 ;; `indent-point' if `containing-sexp' is nil.
7681 (paren-state (c-parse-state))
7682 ;; There's always at most one syntactic element which got
7683 ;; an anchor pos. It's stored in syntactic-relpos.
7684 syntactic-relpos
7685 (c-stmt-delim-chars c-stmt-delim-chars))
7687 ;; Check if we're directly inside an enclosing declaration
7688 ;; level block.
7689 (when (and (setq containing-sexp
7690 (c-most-enclosing-brace paren-state))
7691 (progn
7692 (goto-char containing-sexp)
7693 (eq (char-after) ?{))
7694 (setq placeholder
7695 (c-looking-at-decl-block
7696 (c-most-enclosing-brace paren-state
7697 containing-sexp)
7698 t)))
7699 (setq containing-decl-open containing-sexp
7700 containing-decl-start (point)
7701 containing-sexp nil)
7702 (goto-char placeholder)
7703 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
7704 (c-keyword-sym (match-string 1)))))
7706 ;; Init some position variables.
7707 (if c-state-cache
7708 (progn
7709 (setq containing-sexp (car paren-state)
7710 paren-state (cdr paren-state))
7711 (if (consp containing-sexp)
7712 (progn
7713 (setq lim (cdr containing-sexp))
7714 (if (cdr c-state-cache)
7715 ;; Ignore balanced paren. The next entry
7716 ;; can't be another one.
7717 (setq containing-sexp (car (cdr c-state-cache))
7718 paren-state (cdr paren-state))
7719 ;; If there is no surrounding open paren then
7720 ;; put the last balanced pair back on paren-state.
7721 (setq paren-state (cons containing-sexp paren-state)
7722 containing-sexp nil)))
7723 (setq lim (1+ containing-sexp))))
7724 (setq lim (point-min)))
7726 ;; If we're in a parenthesis list then ',' delimits the
7727 ;; "statements" rather than being an operator (with the
7728 ;; exception of the "for" clause). This difference is
7729 ;; typically only noticeable when statements are used in macro
7730 ;; arglists.
7731 (when (and containing-sexp
7732 (eq (char-after containing-sexp) ?\())
7733 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
7735 ;; cache char before and after indent point, and move point to
7736 ;; the most likely position to perform the majority of tests
7737 (goto-char indent-point)
7738 (c-backward-syntactic-ws lim)
7739 (setq before-ws-ip (point)
7740 char-before-ip (char-before))
7741 (goto-char indent-point)
7742 (skip-chars-forward " \t")
7743 (setq char-after-ip (char-after))
7745 ;; are we in a literal?
7746 (setq literal (c-in-literal lim))
7748 ;; now figure out syntactic qualities of the current line
7749 (cond
7751 ;; CASE 1: in a string.
7752 ((eq literal 'string)
7753 (c-add-syntax 'string (c-point 'bopl)))
7755 ;; CASE 2: in a C or C++ style comment.
7756 ((and (memq literal '(c c++))
7757 ;; This is a kludge for XEmacs where we use
7758 ;; `buffer-syntactic-context', which doesn't correctly
7759 ;; recognize "\*/" to end a block comment.
7760 ;; `parse-partial-sexp' which is used by
7761 ;; `c-literal-limits' will however do that in most
7762 ;; versions, which results in that we get nil from
7763 ;; `c-literal-limits' even when `c-in-literal' claims
7764 ;; we're inside a comment.
7765 (setq placeholder (c-literal-limits lim)))
7766 (c-add-syntax literal (car placeholder)))
7768 ;; CASE 3: in a cpp preprocessor macro continuation.
7769 ((and (save-excursion
7770 (when (c-beginning-of-macro)
7771 (setq macro-start (point))))
7772 (/= macro-start (c-point 'boi))
7773 (progn
7774 (setq tmpsymbol 'cpp-macro-cont)
7775 (or (not c-syntactic-indentation-in-macros)
7776 (save-excursion
7777 (goto-char macro-start)
7778 ;; If at the beginning of the body of a #define
7779 ;; directive then analyze as cpp-define-intro
7780 ;; only. Go on with the syntactic analysis
7781 ;; otherwise. in-macro-expr is set if we're in a
7782 ;; cpp expression, i.e. before the #define body
7783 ;; or anywhere in a non-#define directive.
7784 (if (c-forward-to-cpp-define-body)
7785 (let ((indent-boi (c-point 'boi indent-point)))
7786 (setq in-macro-expr (> (point) indent-boi)
7787 tmpsymbol 'cpp-define-intro)
7788 (= (point) indent-boi))
7789 (setq in-macro-expr t)
7790 nil)))))
7791 (c-add-syntax tmpsymbol macro-start)
7792 (setq macro-start nil))
7794 ;; CASE 11: an else clause?
7795 ((looking-at "else\\>[^_]")
7796 (c-beginning-of-statement-1 containing-sexp)
7797 (c-add-stmt-syntax 'else-clause nil t
7798 containing-sexp paren-state))
7800 ;; CASE 12: while closure of a do/while construct?
7801 ((and (looking-at "while\\>[^_]")
7802 (save-excursion
7803 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
7804 'beginning)
7805 (setq placeholder (point)))))
7806 (goto-char placeholder)
7807 (c-add-stmt-syntax 'do-while-closure nil t
7808 containing-sexp paren-state))
7810 ;; CASE 13: A catch or finally clause? This case is simpler
7811 ;; than if-else and do-while, because a block is required
7812 ;; after every try, catch and finally.
7813 ((save-excursion
7814 (and (cond ((c-major-mode-is 'c++-mode)
7815 (looking-at "catch\\>[^_]"))
7816 ((c-major-mode-is 'java-mode)
7817 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
7818 (and (c-safe (c-backward-syntactic-ws)
7819 (c-backward-sexp)
7821 (eq (char-after) ?{)
7822 (c-safe (c-backward-syntactic-ws)
7823 (c-backward-sexp)
7825 (if (eq (char-after) ?\()
7826 (c-safe (c-backward-sexp) t)
7828 (looking-at "\\(try\\|catch\\)\\>[^_]")
7829 (setq placeholder (point))))
7830 (goto-char placeholder)
7831 (c-add-stmt-syntax 'catch-clause nil t
7832 containing-sexp paren-state))
7834 ;; CASE 18: A substatement we can recognize by keyword.
7835 ((save-excursion
7836 (and c-opt-block-stmt-key
7837 (not (eq char-before-ip ?\;))
7838 (not (c-at-vsemi-p before-ws-ip))
7839 (not (memq char-after-ip '(?\) ?\] ?,)))
7840 (or (not (eq char-before-ip ?}))
7841 (c-looking-at-inexpr-block-backward c-state-cache))
7842 (> (point)
7843 (progn
7844 ;; Ought to cache the result from the
7845 ;; c-beginning-of-statement-1 calls here.
7846 (setq placeholder (point))
7847 (while (eq (setq step-type
7848 (c-beginning-of-statement-1 lim))
7849 'label))
7850 (if (eq step-type 'previous)
7851 (goto-char placeholder)
7852 (setq placeholder (point))
7853 (if (and (eq step-type 'same)
7854 (not (looking-at c-opt-block-stmt-key)))
7855 ;; Step up to the containing statement if we
7856 ;; stayed in the same one.
7857 (let (step)
7858 (while (eq
7859 (setq step
7860 (c-beginning-of-statement-1 lim))
7861 'label))
7862 (if (eq step 'up)
7863 (setq placeholder (point))
7864 ;; There was no containing statement afterall.
7865 (goto-char placeholder)))))
7866 placeholder))
7867 (if (looking-at c-block-stmt-2-key)
7868 ;; Require a parenthesis after these keywords.
7869 ;; Necessary to catch e.g. synchronized in Java,
7870 ;; which can be used both as statement and
7871 ;; modifier.
7872 (and (zerop (c-forward-token-2 1 nil))
7873 (eq (char-after) ?\())
7874 (looking-at c-opt-block-stmt-key))))
7876 (if (eq step-type 'up)
7877 ;; CASE 18A: Simple substatement.
7878 (progn
7879 (goto-char placeholder)
7880 (cond
7881 ((eq char-after-ip ?{)
7882 (c-add-stmt-syntax 'substatement-open nil nil
7883 containing-sexp paren-state))
7884 ((save-excursion
7885 (goto-char indent-point)
7886 (back-to-indentation)
7887 (c-forward-label))
7888 (c-add-stmt-syntax 'substatement-label nil nil
7889 containing-sexp paren-state))
7891 (c-add-stmt-syntax 'substatement nil nil
7892 containing-sexp paren-state))))
7894 ;; CASE 18B: Some other substatement. This is shared
7895 ;; with case 10.
7896 (c-guess-continued-construct indent-point
7897 char-after-ip
7898 placeholder
7900 paren-state)))
7902 ;; CASE 14: A case or default label
7903 ((looking-at c-label-kwds-regexp)
7904 (if containing-sexp
7905 (progn
7906 (goto-char containing-sexp)
7907 (setq lim (c-most-enclosing-brace c-state-cache
7908 containing-sexp))
7909 (c-backward-to-block-anchor lim)
7910 (c-add-stmt-syntax 'case-label nil t lim paren-state))
7911 ;; Got a bogus label at the top level. In lack of better
7912 ;; alternatives, anchor it on (point-min).
7913 (c-add-syntax 'case-label (point-min))))
7915 ;; CASE 15: any other label
7916 ((save-excursion
7917 (back-to-indentation)
7918 (and (not (looking-at c-syntactic-ws-start))
7919 (c-forward-label)))
7920 (cond (containing-decl-open
7921 (setq placeholder (c-add-class-syntax 'inclass
7922 containing-decl-open
7923 containing-decl-start
7924 containing-decl-kwd
7925 paren-state))
7926 ;; Append access-label with the same anchor point as
7927 ;; inclass gets.
7928 (c-append-syntax 'access-label placeholder))
7930 (containing-sexp
7931 (goto-char containing-sexp)
7932 (setq lim (c-most-enclosing-brace c-state-cache
7933 containing-sexp))
7934 (save-excursion
7935 (setq tmpsymbol
7936 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
7937 (looking-at "switch\\>[^_]"))
7938 ;; If the surrounding statement is a switch then
7939 ;; let's analyze all labels as switch labels, so
7940 ;; that they get lined up consistently.
7941 'case-label
7942 'label)))
7943 (c-backward-to-block-anchor lim)
7944 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
7947 ;; A label on the top level. Treat it as a class
7948 ;; context. (point-min) is the closest we get to the
7949 ;; class open brace.
7950 (c-add-syntax 'access-label (point-min)))))
7952 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
7953 ;; 17E.
7954 ((setq placeholder (c-looking-at-inexpr-block
7955 (c-safe-position containing-sexp paren-state)
7956 containing-sexp
7957 ;; Have to turn on the heuristics after
7958 ;; the point even though it doesn't work
7959 ;; very well. C.f. test case class-16.pike.
7961 (setq tmpsymbol (assq (car placeholder)
7962 '((inexpr-class . class-open)
7963 (inexpr-statement . block-open))))
7964 (if tmpsymbol
7965 ;; It's a statement block or an anonymous class.
7966 (setq tmpsymbol (cdr tmpsymbol))
7967 ;; It's a Pike lambda. Check whether we are between the
7968 ;; lambda keyword and the argument list or at the defun
7969 ;; opener.
7970 (setq tmpsymbol (if (eq char-after-ip ?{)
7971 'inline-open
7972 'lambda-intro-cont)))
7973 (goto-char (cdr placeholder))
7974 (back-to-indentation)
7975 (c-add-stmt-syntax tmpsymbol nil t
7976 (c-most-enclosing-brace c-state-cache (point))
7977 paren-state)
7978 (unless (eq (point) (cdr placeholder))
7979 (c-add-syntax (car placeholder))))
7981 ;; CASE 5: Line is inside a declaration level block or at top level.
7982 ((or containing-decl-open (null containing-sexp))
7983 (cond
7985 ;; CASE 5A: we are looking at a defun, brace list, class,
7986 ;; or inline-inclass method opening brace
7987 ((setq special-brace-list
7988 (or (and c-special-brace-lists
7989 (c-looking-at-special-brace-list))
7990 (eq char-after-ip ?{)))
7991 (cond
7993 ;; CASE 5A.1: Non-class declaration block open.
7994 ((save-excursion
7995 (let (tmp)
7996 (and (eq char-after-ip ?{)
7997 (setq tmp (c-looking-at-decl-block containing-sexp t))
7998 (progn
7999 (setq placeholder (point))
8000 (goto-char tmp)
8001 (looking-at c-symbol-key))
8002 (c-keyword-member
8003 (c-keyword-sym (setq keyword (match-string 0)))
8004 'c-other-block-decl-kwds))))
8005 (goto-char placeholder)
8006 (c-add-stmt-syntax
8007 (if (string-equal keyword "extern")
8008 ;; Special case for extern-lang-open.
8009 'extern-lang-open
8010 (intern (concat keyword "-open")))
8011 nil t containing-sexp paren-state))
8013 ;; CASE 5A.2: we are looking at a class opening brace
8014 ((save-excursion
8015 (goto-char indent-point)
8016 (skip-chars-forward " \t")
8017 (and (eq (char-after) ?{)
8018 (c-looking-at-decl-block containing-sexp t)
8019 (setq placeholder (point))))
8020 (c-add-syntax 'class-open placeholder))
8022 ;; CASE 5A.3: brace list open
8023 ((save-excursion
8024 (c-beginning-of-decl-1 lim)
8025 (while (looking-at c-specifier-key)
8026 (goto-char (match-end 1))
8027 (c-forward-syntactic-ws indent-point))
8028 (setq placeholder (c-point 'boi))
8029 (or (consp special-brace-list)
8030 (and (or (save-excursion
8031 (goto-char indent-point)
8032 (setq tmpsymbol nil)
8033 (while (and (> (point) placeholder)
8034 (zerop (c-backward-token-2 1 t))
8035 (/= (char-after) ?=))
8036 (and c-opt-inexpr-brace-list-key
8037 (not tmpsymbol)
8038 (looking-at c-opt-inexpr-brace-list-key)
8039 (setq tmpsymbol 'topmost-intro-cont)))
8040 (eq (char-after) ?=))
8041 (looking-at c-brace-list-key))
8042 (save-excursion
8043 (while (and (< (point) indent-point)
8044 (zerop (c-forward-token-2 1 t))
8045 (not (memq (char-after) '(?\; ?\()))))
8046 (not (memq (char-after) '(?\; ?\()))
8047 ))))
8048 (if (and (not c-auto-newline-analysis)
8049 (c-major-mode-is 'java-mode)
8050 (eq tmpsymbol 'topmost-intro-cont))
8051 ;; We're in Java and have found that the open brace
8052 ;; belongs to a "new Foo[]" initialization list,
8053 ;; which means the brace list is part of an
8054 ;; expression and not a top level definition. We
8055 ;; therefore treat it as any topmost continuation
8056 ;; even though the semantically correct symbol still
8057 ;; is brace-list-open, on the same grounds as in
8058 ;; case B.2.
8059 (progn
8060 (c-beginning-of-statement-1 lim)
8061 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8062 (c-add-syntax 'brace-list-open placeholder)))
8064 ;; CASE 5A.4: inline defun open
8065 ((and containing-decl-open
8066 (not (c-keyword-member containing-decl-kwd
8067 'c-other-block-decl-kwds)))
8068 (c-add-syntax 'inline-open)
8069 (c-add-class-syntax 'inclass
8070 containing-decl-open
8071 containing-decl-start
8072 containing-decl-kwd
8073 paren-state))
8075 ;; CASE 5A.5: ordinary defun open
8077 (save-excursion
8078 (c-beginning-of-decl-1 lim)
8079 (while (looking-at c-specifier-key)
8080 (goto-char (match-end 1))
8081 (c-forward-syntactic-ws indent-point))
8082 (c-add-syntax 'defun-open (c-point 'boi))
8083 ;; Bogus to use bol here, but it's the legacy. (Resolved,
8084 ;; 2007-11-09)
8085 ))))
8087 ;; CASE 5B: After a function header but before the body (or
8088 ;; the ending semicolon if there's no body).
8089 ((save-excursion
8090 (when (setq placeholder (c-just-after-func-arglist-p lim))
8091 (setq tmp-pos (point))))
8092 (cond
8094 ;; CASE 5B.1: Member init list.
8095 ((eq (char-after tmp-pos) ?:)
8096 (if (or (> tmp-pos indent-point)
8097 (= (c-point 'bosws) (1+ tmp-pos)))
8098 (progn
8099 ;; There is no preceding member init clause.
8100 ;; Indent relative to the beginning of indentation
8101 ;; for the topmost-intro line that contains the
8102 ;; prototype's open paren.
8103 (goto-char placeholder)
8104 (c-add-syntax 'member-init-intro (c-point 'boi)))
8105 ;; Indent relative to the first member init clause.
8106 (goto-char (1+ tmp-pos))
8107 (c-forward-syntactic-ws)
8108 (c-add-syntax 'member-init-cont (point))))
8110 ;; CASE 5B.2: K&R arg decl intro
8111 ((and c-recognize-knr-p
8112 (c-in-knr-argdecl lim))
8113 (c-beginning-of-statement-1 lim)
8114 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
8115 (if containing-decl-open
8116 (c-add-class-syntax 'inclass
8117 containing-decl-open
8118 containing-decl-start
8119 containing-decl-kwd
8120 paren-state)))
8122 ;; CASE 5B.4: Nether region after a C++ or Java func
8123 ;; decl, which could include a `throws' declaration.
8125 (c-beginning-of-statement-1 lim)
8126 (c-add-syntax 'func-decl-cont (c-point 'boi))
8129 ;; CASE 5C: inheritance line. could be first inheritance
8130 ;; line, or continuation of a multiple inheritance
8131 ((or (and (c-major-mode-is 'c++-mode)
8132 (progn
8133 (when (eq char-after-ip ?,)
8134 (skip-chars-forward " \t")
8135 (forward-char))
8136 (looking-at c-opt-postfix-decl-spec-key)))
8137 (and (or (eq char-before-ip ?:)
8138 ;; watch out for scope operator
8139 (save-excursion
8140 (and (eq char-after-ip ?:)
8141 (c-safe (forward-char 1) t)
8142 (not (eq (char-after) ?:))
8144 (save-excursion
8145 (c-backward-syntactic-ws lim)
8146 (if (eq char-before-ip ?:)
8147 (progn
8148 (forward-char -1)
8149 (c-backward-syntactic-ws lim)))
8150 (back-to-indentation)
8151 (looking-at c-class-key)))
8152 ;; for Java
8153 (and (c-major-mode-is 'java-mode)
8154 (let ((fence (save-excursion
8155 (c-beginning-of-statement-1 lim)
8156 (point)))
8157 cont done)
8158 (save-excursion
8159 (while (not done)
8160 (cond ((looking-at c-opt-postfix-decl-spec-key)
8161 (setq injava-inher (cons cont (point))
8162 done t))
8163 ((or (not (c-safe (c-forward-sexp -1) t))
8164 (<= (point) fence))
8165 (setq done t))
8167 (setq cont t)))
8168 injava-inher)
8169 (not (c-crosses-statement-barrier-p (cdr injava-inher)
8170 (point)))
8172 (cond
8174 ;; CASE 5C.1: non-hanging colon on an inher intro
8175 ((eq char-after-ip ?:)
8176 (c-beginning-of-statement-1 lim)
8177 (c-add-syntax 'inher-intro (c-point 'boi))
8178 ;; don't add inclass symbol since relative point already
8179 ;; contains any class offset
8182 ;; CASE 5C.2: hanging colon on an inher intro
8183 ((eq char-before-ip ?:)
8184 (c-beginning-of-statement-1 lim)
8185 (c-add-syntax 'inher-intro (c-point 'boi))
8186 (if containing-decl-open
8187 (c-add-class-syntax 'inclass
8188 containing-decl-open
8189 containing-decl-start
8190 containing-decl-kwd
8191 paren-state)))
8193 ;; CASE 5C.3: in a Java implements/extends
8194 (injava-inher
8195 (let ((where (cdr injava-inher))
8196 (cont (car injava-inher)))
8197 (goto-char where)
8198 (cond ((looking-at "throws\\>[^_]")
8199 (c-add-syntax 'func-decl-cont
8200 (progn (c-beginning-of-statement-1 lim)
8201 (c-point 'boi))))
8202 (cont (c-add-syntax 'inher-cont where))
8203 (t (c-add-syntax 'inher-intro
8204 (progn (goto-char (cdr injava-inher))
8205 (c-beginning-of-statement-1 lim)
8206 (point))))
8209 ;; CASE 5C.4: a continued inheritance line
8211 (c-beginning-of-inheritance-list lim)
8212 (c-add-syntax 'inher-cont (point))
8213 ;; don't add inclass symbol since relative point already
8214 ;; contains any class offset
8217 ;; CASE 5D: this could be a top-level initialization, a
8218 ;; member init list continuation, or a template argument
8219 ;; list continuation.
8220 ((save-excursion
8221 ;; Note: We use the fact that lim is always after any
8222 ;; preceding brace sexp.
8223 (if c-recognize-<>-arglists
8224 (while (and
8225 (progn
8226 (c-syntactic-skip-backward "^;,=<>" lim t)
8227 (> (point) lim))
8229 (when c-overloadable-operators-regexp
8230 (when (setq placeholder (c-after-special-operator-id lim))
8231 (goto-char placeholder)
8233 (cond
8234 ((eq (char-before) ?>)
8235 (or (c-backward-<>-arglist nil lim)
8236 (backward-char))
8238 ((eq (char-before) ?<)
8239 (backward-char)
8240 (if (save-excursion
8241 (c-forward-<>-arglist nil))
8242 (progn (forward-char)
8243 nil)
8245 (t nil)))))
8246 ;; NB: No c-after-special-operator-id stuff in this
8247 ;; clause - we assume only C++ needs it.
8248 (c-syntactic-skip-backward "^;,=" lim t))
8249 (memq (char-before) '(?, ?= ?<)))
8250 (cond
8252 ;; CASE 5D.3: perhaps a template list continuation?
8253 ((and (c-major-mode-is 'c++-mode)
8254 (save-excursion
8255 (save-restriction
8256 (c-with-syntax-table c++-template-syntax-table
8257 (goto-char indent-point)
8258 (setq placeholder (c-up-list-backward))
8259 (and placeholder
8260 (eq (char-after placeholder) ?<))))))
8261 (c-with-syntax-table c++-template-syntax-table
8262 (goto-char placeholder)
8263 (c-beginning-of-statement-1 lim t)
8264 (if (save-excursion
8265 (c-backward-syntactic-ws lim)
8266 (eq (char-before) ?<))
8267 ;; In a nested template arglist.
8268 (progn
8269 (goto-char placeholder)
8270 (c-syntactic-skip-backward "^,;" lim t)
8271 (c-forward-syntactic-ws))
8272 (back-to-indentation)))
8273 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8274 ;; template aware.
8275 (c-add-syntax 'template-args-cont (point)))
8277 ;; CASE 5D.4: perhaps a multiple inheritance line?
8278 ((and (c-major-mode-is 'c++-mode)
8279 (save-excursion
8280 (c-beginning-of-statement-1 lim)
8281 (setq placeholder (point))
8282 (if (looking-at "static\\>[^_]")
8283 (c-forward-token-2 1 nil indent-point))
8284 (and (looking-at c-class-key)
8285 (zerop (c-forward-token-2 2 nil indent-point))
8286 (if (eq (char-after) ?<)
8287 (c-with-syntax-table c++-template-syntax-table
8288 (zerop (c-forward-token-2 1 t indent-point)))
8290 (eq (char-after) ?:))))
8291 (goto-char placeholder)
8292 (c-add-syntax 'inher-cont (c-point 'boi)))
8294 ;; CASE 5D.5: Continuation of the "expression part" of a
8295 ;; top level construct. Or, perhaps, an unrecognised construct.
8297 (while (and (setq placeholder (point))
8298 (eq (car (c-beginning-of-decl-1 containing-sexp))
8299 'same)
8300 (save-excursion
8301 (c-backward-syntactic-ws)
8302 (eq (char-before) ?}))
8303 (< (point) placeholder)))
8304 (c-add-stmt-syntax
8305 (cond
8306 ((eq (point) placeholder) 'statement) ; unrecognised construct
8307 ;; A preceding comma at the top level means that a
8308 ;; new variable declaration starts here. Use
8309 ;; topmost-intro-cont for it, for consistency with
8310 ;; the first variable declaration. C.f. case 5N.
8311 ((eq char-before-ip ?,) 'topmost-intro-cont)
8312 (t 'statement-cont))
8313 nil nil containing-sexp paren-state))
8316 ;; CASE 5F: Close of a non-class declaration level block.
8317 ((and (eq char-after-ip ?})
8318 (c-keyword-member containing-decl-kwd
8319 'c-other-block-decl-kwds))
8320 ;; This is inconsistent: Should use `containing-decl-open'
8321 ;; here if it's at boi, like in case 5J.
8322 (goto-char containing-decl-start)
8323 (c-add-stmt-syntax
8324 (if (string-equal (symbol-name containing-decl-kwd) "extern")
8325 ;; Special case for compatibility with the
8326 ;; extern-lang syntactic symbols.
8327 'extern-lang-close
8328 (intern (concat (symbol-name containing-decl-kwd)
8329 "-close")))
8330 nil t
8331 (c-most-enclosing-brace paren-state (point))
8332 paren-state))
8334 ;; CASE 5G: we are looking at the brace which closes the
8335 ;; enclosing nested class decl
8336 ((and containing-sexp
8337 (eq char-after-ip ?})
8338 (eq containing-decl-open containing-sexp))
8339 (c-add-class-syntax 'class-close
8340 containing-decl-open
8341 containing-decl-start
8342 containing-decl-kwd
8343 paren-state))
8345 ;; CASE 5H: we could be looking at subsequent knr-argdecls
8346 ((and c-recognize-knr-p
8347 (not containing-sexp) ; can't be knr inside braces.
8348 (not (eq char-before-ip ?}))
8349 (save-excursion
8350 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
8351 (and placeholder
8352 ;; Do an extra check to avoid tripping up on
8353 ;; statements that occur in invalid contexts
8354 ;; (e.g. in macro bodies where we don't really
8355 ;; know the context of what we're looking at).
8356 (not (and c-opt-block-stmt-key
8357 (looking-at c-opt-block-stmt-key)))))
8358 (< placeholder indent-point))
8359 (goto-char placeholder)
8360 (c-add-syntax 'knr-argdecl (point)))
8362 ;; CASE 5I: ObjC method definition.
8363 ((and c-opt-method-key
8364 (looking-at c-opt-method-key))
8365 (c-beginning-of-statement-1 nil t)
8366 (if (= (point) indent-point)
8367 ;; Handle the case when it's the first (non-comment)
8368 ;; thing in the buffer. Can't look for a 'same return
8369 ;; value from cbos1 since ObjC directives currently
8370 ;; aren't recognized fully, so that we get 'same
8371 ;; instead of 'previous if it moved over a preceding
8372 ;; directive.
8373 (goto-char (point-min)))
8374 (c-add-syntax 'objc-method-intro (c-point 'boi)))
8376 ;; CASE 5P: AWK pattern or function or continuation
8377 ;; thereof.
8378 ((c-major-mode-is 'awk-mode)
8379 (setq placeholder (point))
8380 (c-add-stmt-syntax
8381 (if (and (eq (c-beginning-of-statement-1) 'same)
8382 (/= (point) placeholder))
8383 'topmost-intro-cont
8384 'topmost-intro)
8385 nil nil
8386 containing-sexp paren-state))
8388 ;; CASE 5N: At a variable declaration that follows a class
8389 ;; definition or some other block declaration that doesn't
8390 ;; end at the closing '}'. C.f. case 5D.5.
8391 ((progn
8392 (c-backward-syntactic-ws lim)
8393 (and (eq (char-before) ?})
8394 (save-excursion
8395 (let ((start (point)))
8396 (if (and c-state-cache
8397 (consp (car c-state-cache))
8398 (eq (cdar c-state-cache) (point)))
8399 ;; Speed up the backward search a bit.
8400 (goto-char (caar c-state-cache)))
8401 (c-beginning-of-decl-1 containing-sexp)
8402 (setq placeholder (point))
8403 (if (= start (point))
8404 ;; The '}' is unbalanced.
8406 (c-end-of-decl-1)
8407 (>= (point) indent-point))))))
8408 (goto-char placeholder)
8409 (c-add-stmt-syntax 'topmost-intro-cont nil nil
8410 containing-sexp paren-state))
8412 ;; NOTE: The point is at the end of the previous token here.
8414 ;; CASE 5J: we are at the topmost level, make
8415 ;; sure we skip back past any access specifiers
8416 ((and
8417 ;; A macro continuation line is never at top level.
8418 (not (and macro-start
8419 (> indent-point macro-start)))
8420 (save-excursion
8421 (setq placeholder (point))
8422 (or (memq char-before-ip '(?\; ?{ ?} nil))
8423 (c-at-vsemi-p before-ws-ip)
8424 (when (and (eq char-before-ip ?:)
8425 (eq (c-beginning-of-statement-1 lim)
8426 'label))
8427 (c-backward-syntactic-ws lim)
8428 (setq placeholder (point)))
8429 (and (c-major-mode-is 'objc-mode)
8430 (catch 'not-in-directive
8431 (c-beginning-of-statement-1 lim)
8432 (setq placeholder (point))
8433 (while (and (c-forward-objc-directive)
8434 (< (point) indent-point))
8435 (c-forward-syntactic-ws)
8436 (if (>= (point) indent-point)
8437 (throw 'not-in-directive t))
8438 (setq placeholder (point)))
8439 nil)))))
8440 ;; For historic reasons we anchor at bol of the last
8441 ;; line of the previous declaration. That's clearly
8442 ;; highly bogus and useless, and it makes our lives hard
8443 ;; to remain compatible. :P
8444 (goto-char placeholder)
8445 (c-add-syntax 'topmost-intro (c-point 'bol))
8446 (if containing-decl-open
8447 (if (c-keyword-member containing-decl-kwd
8448 'c-other-block-decl-kwds)
8449 (progn
8450 (goto-char (c-brace-anchor-point containing-decl-open))
8451 (c-add-stmt-syntax
8452 (if (string-equal (symbol-name containing-decl-kwd)
8453 "extern")
8454 ;; Special case for compatibility with the
8455 ;; extern-lang syntactic symbols.
8456 'inextern-lang
8457 (intern (concat "in"
8458 (symbol-name containing-decl-kwd))))
8459 nil t
8460 (c-most-enclosing-brace paren-state (point))
8461 paren-state))
8462 (c-add-class-syntax 'inclass
8463 containing-decl-open
8464 containing-decl-start
8465 containing-decl-kwd
8466 paren-state)))
8467 (when (and c-syntactic-indentation-in-macros
8468 macro-start
8469 (/= macro-start (c-point 'boi indent-point)))
8470 (c-add-syntax 'cpp-define-intro)
8471 (setq macro-start nil)))
8473 ;; CASE 5K: we are at an ObjC method definition
8474 ;; continuation line.
8475 ((and c-opt-method-key
8476 (save-excursion
8477 (c-beginning-of-statement-1 lim)
8478 (beginning-of-line)
8479 (when (looking-at c-opt-method-key)
8480 (setq placeholder (point)))))
8481 (c-add-syntax 'objc-method-args-cont placeholder))
8483 ;; CASE 5L: we are at the first argument of a template
8484 ;; arglist that begins on the previous line.
8485 ((and c-recognize-<>-arglists
8486 (eq (char-before) ?<)
8487 (not (and c-overloadable-operators-regexp
8488 (c-after-special-operator-id lim))))
8489 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8490 (c-add-syntax 'template-args-cont (c-point 'boi)))
8492 ;; CASE 5Q: we are at a statement within a macro.
8493 (macro-start
8494 (c-beginning-of-statement-1 containing-sexp)
8495 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
8497 ;; CASE 5M: we are at a topmost continuation line
8499 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8500 (when (c-major-mode-is 'objc-mode)
8501 (setq placeholder (point))
8502 (while (and (c-forward-objc-directive)
8503 (< (point) indent-point))
8504 (c-forward-syntactic-ws)
8505 (setq placeholder (point)))
8506 (goto-char placeholder))
8507 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8510 ;; (CASE 6 has been removed.)
8512 ;; CASE 7: line is an expression, not a statement. Most
8513 ;; likely we are either in a function prototype or a function
8514 ;; call argument list
8515 ((not (or (and c-special-brace-lists
8516 (save-excursion
8517 (goto-char containing-sexp)
8518 (c-looking-at-special-brace-list)))
8519 (eq (char-after containing-sexp) ?{)))
8520 (cond
8522 ;; CASE 7A: we are looking at the arglist closing paren.
8523 ;; C.f. case 7F.
8524 ((memq char-after-ip '(?\) ?\]))
8525 (goto-char containing-sexp)
8526 (setq placeholder (c-point 'boi))
8527 (if (and (c-safe (backward-up-list 1) t)
8528 (>= (point) placeholder))
8529 (progn
8530 (forward-char)
8531 (skip-chars-forward " \t"))
8532 (goto-char placeholder))
8533 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
8534 (c-most-enclosing-brace paren-state (point))
8535 paren-state))
8537 ;; CASE 7B: Looking at the opening brace of an
8538 ;; in-expression block or brace list. C.f. cases 4, 16A
8539 ;; and 17E.
8540 ((and (eq char-after-ip ?{)
8541 (progn
8542 (setq placeholder (c-inside-bracelist-p (point)
8543 paren-state))
8544 (if placeholder
8545 (setq tmpsymbol '(brace-list-open . inexpr-class))
8546 (setq tmpsymbol '(block-open . inexpr-statement)
8547 placeholder
8548 (cdr-safe (c-looking-at-inexpr-block
8549 (c-safe-position containing-sexp
8550 paren-state)
8551 containing-sexp)))
8552 ;; placeholder is nil if it's a block directly in
8553 ;; a function arglist. That makes us skip out of
8554 ;; this case.
8556 (goto-char placeholder)
8557 (back-to-indentation)
8558 (c-add-stmt-syntax (car tmpsymbol) nil t
8559 (c-most-enclosing-brace paren-state (point))
8560 paren-state)
8561 (if (/= (point) placeholder)
8562 (c-add-syntax (cdr tmpsymbol))))
8564 ;; CASE 7C: we are looking at the first argument in an empty
8565 ;; argument list. Use arglist-close if we're actually
8566 ;; looking at a close paren or bracket.
8567 ((memq char-before-ip '(?\( ?\[))
8568 (goto-char containing-sexp)
8569 (setq placeholder (c-point 'boi))
8570 (if (and (c-safe (backward-up-list 1) t)
8571 (>= (point) placeholder))
8572 (progn
8573 (forward-char)
8574 (skip-chars-forward " \t"))
8575 (goto-char placeholder))
8576 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
8577 (c-most-enclosing-brace paren-state (point))
8578 paren-state))
8580 ;; CASE 7D: we are inside a conditional test clause. treat
8581 ;; these things as statements
8582 ((progn
8583 (goto-char containing-sexp)
8584 (and (c-safe (c-forward-sexp -1) t)
8585 (looking-at "\\<for\\>[^_]")))
8586 (goto-char (1+ containing-sexp))
8587 (c-forward-syntactic-ws indent-point)
8588 (if (eq char-before-ip ?\;)
8589 (c-add-syntax 'statement (point))
8590 (c-add-syntax 'statement-cont (point))
8593 ;; CASE 7E: maybe a continued ObjC method call. This is the
8594 ;; case when we are inside a [] bracketed exp, and what
8595 ;; precede the opening bracket is not an identifier.
8596 ((and c-opt-method-key
8597 (eq (char-after containing-sexp) ?\[)
8598 (progn
8599 (goto-char (1- containing-sexp))
8600 (c-backward-syntactic-ws (c-point 'bod))
8601 (if (not (looking-at c-symbol-key))
8602 (c-add-syntax 'objc-method-call-cont containing-sexp))
8605 ;; CASE 7F: we are looking at an arglist continuation line,
8606 ;; but the preceding argument is on the same line as the
8607 ;; opening paren. This case includes multi-line
8608 ;; mathematical paren groupings, but we could be on a
8609 ;; for-list continuation line. C.f. case 7A.
8610 ((progn
8611 (goto-char (1+ containing-sexp))
8612 (< (save-excursion
8613 (c-forward-syntactic-ws)
8614 (point))
8615 (c-point 'bonl)))
8616 (goto-char containing-sexp)
8617 (setq placeholder (c-point 'boi))
8618 (if (and (c-safe (backward-up-list 1) t)
8619 (>= (point) placeholder))
8620 (progn
8621 (forward-char)
8622 (skip-chars-forward " \t"))
8623 (goto-char placeholder))
8624 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
8625 (c-most-enclosing-brace c-state-cache (point))
8626 paren-state))
8628 ;; CASE 7G: we are looking at just a normal arglist
8629 ;; continuation line
8630 (t (c-forward-syntactic-ws indent-point)
8631 (c-add-syntax 'arglist-cont (c-point 'boi)))
8634 ;; CASE 8: func-local multi-inheritance line
8635 ((and (c-major-mode-is 'c++-mode)
8636 (save-excursion
8637 (goto-char indent-point)
8638 (skip-chars-forward " \t")
8639 (looking-at c-opt-postfix-decl-spec-key)))
8640 (goto-char indent-point)
8641 (skip-chars-forward " \t")
8642 (cond
8644 ;; CASE 8A: non-hanging colon on an inher intro
8645 ((eq char-after-ip ?:)
8646 (c-backward-syntactic-ws lim)
8647 (c-add-syntax 'inher-intro (c-point 'boi)))
8649 ;; CASE 8B: hanging colon on an inher intro
8650 ((eq char-before-ip ?:)
8651 (c-add-syntax 'inher-intro (c-point 'boi)))
8653 ;; CASE 8C: a continued inheritance line
8655 (c-beginning-of-inheritance-list lim)
8656 (c-add-syntax 'inher-cont (point))
8659 ;; CASE 9: we are inside a brace-list
8660 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
8661 (setq special-brace-list
8662 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
8663 (save-excursion
8664 (goto-char containing-sexp)
8665 (c-looking-at-special-brace-list)))
8666 (c-inside-bracelist-p containing-sexp paren-state))))
8667 (cond
8669 ;; CASE 9A: In the middle of a special brace list opener.
8670 ((and (consp special-brace-list)
8671 (save-excursion
8672 (goto-char containing-sexp)
8673 (eq (char-after) ?\())
8674 (eq char-after-ip (car (cdr special-brace-list))))
8675 (goto-char (car (car special-brace-list)))
8676 (skip-chars-backward " \t")
8677 (if (and (bolp)
8678 (assoc 'statement-cont
8679 (setq placeholder (c-guess-basic-syntax))))
8680 (setq c-syntactic-context placeholder)
8681 (c-beginning-of-statement-1
8682 (c-safe-position (1- containing-sexp) paren-state))
8683 (c-forward-token-2 0)
8684 (while (looking-at c-specifier-key)
8685 (goto-char (match-end 1))
8686 (c-forward-syntactic-ws))
8687 (c-add-syntax 'brace-list-open (c-point 'boi))))
8689 ;; CASE 9B: brace-list-close brace
8690 ((if (consp special-brace-list)
8691 ;; Check special brace list closer.
8692 (progn
8693 (goto-char (car (car special-brace-list)))
8694 (save-excursion
8695 (goto-char indent-point)
8696 (back-to-indentation)
8698 ;; We were between the special close char and the `)'.
8699 (and (eq (char-after) ?\))
8700 (eq (1+ (point)) (cdr (car special-brace-list))))
8701 ;; We were before the special close char.
8702 (and (eq (char-after) (cdr (cdr special-brace-list)))
8703 (zerop (c-forward-token-2))
8704 (eq (1+ (point)) (cdr (car special-brace-list)))))))
8705 ;; Normal brace list check.
8706 (and (eq char-after-ip ?})
8707 (c-safe (goto-char (c-up-list-backward (point))) t)
8708 (= (point) containing-sexp)))
8709 (if (eq (point) (c-point 'boi))
8710 (c-add-syntax 'brace-list-close (point))
8711 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8712 (c-beginning-of-statement-1 lim)
8713 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
8716 ;; Prepare for the rest of the cases below by going to the
8717 ;; token following the opening brace
8718 (if (consp special-brace-list)
8719 (progn
8720 (goto-char (car (car special-brace-list)))
8721 (c-forward-token-2 1 nil indent-point))
8722 (goto-char containing-sexp))
8723 (forward-char)
8724 (let ((start (point)))
8725 (c-forward-syntactic-ws indent-point)
8726 (goto-char (max start (c-point 'bol))))
8727 (c-skip-ws-forward indent-point)
8728 (cond
8730 ;; CASE 9C: we're looking at the first line in a brace-list
8731 ((= (point) indent-point)
8732 (if (consp special-brace-list)
8733 (goto-char (car (car special-brace-list)))
8734 (goto-char containing-sexp))
8735 (if (eq (point) (c-point 'boi))
8736 (c-add-syntax 'brace-list-intro (point))
8737 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8738 (c-beginning-of-statement-1 lim)
8739 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
8741 ;; CASE 9D: this is just a later brace-list-entry or
8742 ;; brace-entry-open
8743 (t (if (or (eq char-after-ip ?{)
8744 (and c-special-brace-lists
8745 (save-excursion
8746 (goto-char indent-point)
8747 (c-forward-syntactic-ws (c-point 'eol))
8748 (c-looking-at-special-brace-list (point)))))
8749 (c-add-syntax 'brace-entry-open (point))
8750 (c-add-syntax 'brace-list-entry (point))
8752 ))))
8754 ;; CASE 10: A continued statement or top level construct.
8755 ((and (not (memq char-before-ip '(?\; ?:)))
8756 (not (c-at-vsemi-p before-ws-ip))
8757 (or (not (eq char-before-ip ?}))
8758 (c-looking-at-inexpr-block-backward c-state-cache))
8759 (> (point)
8760 (save-excursion
8761 (c-beginning-of-statement-1 containing-sexp)
8762 (setq placeholder (point))))
8763 (/= placeholder containing-sexp))
8764 ;; This is shared with case 18.
8765 (c-guess-continued-construct indent-point
8766 char-after-ip
8767 placeholder
8768 containing-sexp
8769 paren-state))
8771 ;; CASE 16: block close brace, possibly closing the defun or
8772 ;; the class
8773 ((eq char-after-ip ?})
8774 ;; From here on we have the next containing sexp in lim.
8775 (setq lim (c-most-enclosing-brace paren-state))
8776 (goto-char containing-sexp)
8777 (cond
8779 ;; CASE 16E: Closing a statement block? This catches
8780 ;; cases where it's preceded by a statement keyword,
8781 ;; which works even when used in an "invalid" context,
8782 ;; e.g. a macro argument.
8783 ((c-after-conditional)
8784 (c-backward-to-block-anchor lim)
8785 (c-add-stmt-syntax 'block-close nil t lim paren-state))
8787 ;; CASE 16A: closing a lambda defun or an in-expression
8788 ;; block? C.f. cases 4, 7B and 17E.
8789 ((setq placeholder (c-looking-at-inexpr-block
8790 (c-safe-position containing-sexp paren-state)
8791 nil))
8792 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8793 'inline-close
8794 'block-close))
8795 (goto-char containing-sexp)
8796 (back-to-indentation)
8797 (if (= containing-sexp (point))
8798 (c-add-syntax tmpsymbol (point))
8799 (goto-char (cdr placeholder))
8800 (back-to-indentation)
8801 (c-add-stmt-syntax tmpsymbol nil t
8802 (c-most-enclosing-brace paren-state (point))
8803 paren-state)
8804 (if (/= (point) (cdr placeholder))
8805 (c-add-syntax (car placeholder)))))
8807 ;; CASE 16B: does this close an inline or a function in
8808 ;; a non-class declaration level block?
8809 ((save-excursion
8810 (and lim
8811 (progn
8812 (goto-char lim)
8813 (c-looking-at-decl-block
8814 (c-most-enclosing-brace paren-state lim)
8815 nil))
8816 (setq placeholder (point))))
8817 (c-backward-to-decl-anchor lim)
8818 (back-to-indentation)
8819 (if (save-excursion
8820 (goto-char placeholder)
8821 (looking-at c-other-decl-block-key))
8822 (c-add-syntax 'defun-close (point))
8823 (c-add-syntax 'inline-close (point))))
8825 ;; CASE 16F: Can be a defun-close of a function declared
8826 ;; in a statement block, e.g. in Pike or when using gcc
8827 ;; extensions, but watch out for macros followed by
8828 ;; blocks. Let it through to be handled below.
8829 ;; C.f. cases B.3 and 17G.
8830 ((save-excursion
8831 (and (not (c-at-statement-start-p))
8832 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8833 (setq placeholder (point))
8834 (let ((c-recognize-typeless-decls nil))
8835 ;; Turn off recognition of constructs that
8836 ;; lacks a type in this case, since that's more
8837 ;; likely to be a macro followed by a block.
8838 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8839 (back-to-indentation)
8840 (if (/= (point) containing-sexp)
8841 (goto-char placeholder))
8842 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
8844 ;; CASE 16C: If there is an enclosing brace then this is
8845 ;; a block close since defun closes inside declaration
8846 ;; level blocks have been handled above.
8847 (lim
8848 ;; If the block is preceded by a case/switch label on
8849 ;; the same line, we anchor at the first preceding label
8850 ;; at boi. The default handling in c-add-stmt-syntax
8851 ;; really fixes it better, but we do like this to keep
8852 ;; the indentation compatible with version 5.28 and
8853 ;; earlier. C.f. case 17H.
8854 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8855 (eq (c-beginning-of-statement-1 lim) 'label)))
8856 (goto-char placeholder)
8857 (if (looking-at c-label-kwds-regexp)
8858 (c-add-syntax 'block-close (point))
8859 (goto-char containing-sexp)
8860 ;; c-backward-to-block-anchor not necessary here; those
8861 ;; situations are handled in case 16E above.
8862 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
8864 ;; CASE 16D: Only top level defun close left.
8866 (goto-char containing-sexp)
8867 (c-backward-to-decl-anchor lim)
8868 (c-add-stmt-syntax 'defun-close nil nil
8869 (c-most-enclosing-brace paren-state)
8870 paren-state))
8873 ;; CASE 17: Statement or defun catchall.
8875 (goto-char indent-point)
8876 ;; Back up statements until we find one that starts at boi.
8877 (while (let* ((prev-point (point))
8878 (last-step-type (c-beginning-of-statement-1
8879 containing-sexp)))
8880 (if (= (point) prev-point)
8881 (progn
8882 (setq step-type (or step-type last-step-type))
8883 nil)
8884 (setq step-type last-step-type)
8885 (/= (point) (c-point 'boi)))))
8886 (cond
8888 ;; CASE 17B: continued statement
8889 ((and (eq step-type 'same)
8890 (/= (point) indent-point))
8891 (c-add-stmt-syntax 'statement-cont nil nil
8892 containing-sexp paren-state))
8894 ;; CASE 17A: After a case/default label?
8895 ((progn
8896 (while (and (eq step-type 'label)
8897 (not (looking-at c-label-kwds-regexp)))
8898 (setq step-type
8899 (c-beginning-of-statement-1 containing-sexp)))
8900 (eq step-type 'label))
8901 (c-add-stmt-syntax (if (eq char-after-ip ?{)
8902 'statement-case-open
8903 'statement-case-intro)
8904 nil t containing-sexp paren-state))
8906 ;; CASE 17D: any old statement
8907 ((progn
8908 (while (eq step-type 'label)
8909 (setq step-type
8910 (c-beginning-of-statement-1 containing-sexp)))
8911 (eq step-type 'previous))
8912 (c-add-stmt-syntax 'statement nil t
8913 containing-sexp paren-state)
8914 (if (eq char-after-ip ?{)
8915 (c-add-syntax 'block-open)))
8917 ;; CASE 17I: Inside a substatement block.
8918 ((progn
8919 ;; The following tests are all based on containing-sexp.
8920 (goto-char containing-sexp)
8921 ;; From here on we have the next containing sexp in lim.
8922 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
8923 (c-after-conditional))
8924 (c-backward-to-block-anchor lim)
8925 (c-add-stmt-syntax 'statement-block-intro nil t
8926 lim paren-state)
8927 (if (eq char-after-ip ?{)
8928 (c-add-syntax 'block-open)))
8930 ;; CASE 17E: first statement in an in-expression block.
8931 ;; C.f. cases 4, 7B and 16A.
8932 ((setq placeholder (c-looking-at-inexpr-block
8933 (c-safe-position containing-sexp paren-state)
8934 nil))
8935 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8936 'defun-block-intro
8937 'statement-block-intro))
8938 (back-to-indentation)
8939 (if (= containing-sexp (point))
8940 (c-add-syntax tmpsymbol (point))
8941 (goto-char (cdr placeholder))
8942 (back-to-indentation)
8943 (c-add-stmt-syntax tmpsymbol nil t
8944 (c-most-enclosing-brace c-state-cache (point))
8945 paren-state)
8946 (if (/= (point) (cdr placeholder))
8947 (c-add-syntax (car placeholder))))
8948 (if (eq char-after-ip ?{)
8949 (c-add-syntax 'block-open)))
8951 ;; CASE 17F: first statement in an inline, or first
8952 ;; statement in a top-level defun. we can tell this is it
8953 ;; if there are no enclosing braces that haven't been
8954 ;; narrowed out by a class (i.e. don't use bod here).
8955 ((save-excursion
8956 (or (not (setq placeholder (c-most-enclosing-brace
8957 paren-state)))
8958 (and (progn
8959 (goto-char placeholder)
8960 (eq (char-after) ?{))
8961 (c-looking-at-decl-block (c-most-enclosing-brace
8962 paren-state (point))
8963 nil))))
8964 (c-backward-to-decl-anchor lim)
8965 (back-to-indentation)
8966 (c-add-syntax 'defun-block-intro (point)))
8968 ;; CASE 17G: First statement in a function declared inside
8969 ;; a normal block. This can occur in Pike and with
8970 ;; e.g. the gcc extensions, but watch out for macros
8971 ;; followed by blocks. C.f. cases B.3 and 16F.
8972 ((save-excursion
8973 (and (not (c-at-statement-start-p))
8974 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8975 (setq placeholder (point))
8976 (let ((c-recognize-typeless-decls nil))
8977 ;; Turn off recognition of constructs that lacks
8978 ;; a type in this case, since that's more likely
8979 ;; to be a macro followed by a block.
8980 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8981 (back-to-indentation)
8982 (if (/= (point) containing-sexp)
8983 (goto-char placeholder))
8984 (c-add-stmt-syntax 'defun-block-intro nil t
8985 lim paren-state))
8987 ;; CASE 17H: First statement in a block.
8989 ;; If the block is preceded by a case/switch label on the
8990 ;; same line, we anchor at the first preceding label at
8991 ;; boi. The default handling in c-add-stmt-syntax is
8992 ;; really fixes it better, but we do like this to keep the
8993 ;; indentation compatible with version 5.28 and earlier.
8994 ;; C.f. case 16C.
8995 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8996 (eq (c-beginning-of-statement-1 lim) 'label)))
8997 (goto-char placeholder)
8998 (if (looking-at c-label-kwds-regexp)
8999 (c-add-syntax 'statement-block-intro (point))
9000 (goto-char containing-sexp)
9001 ;; c-backward-to-block-anchor not necessary here; those
9002 ;; situations are handled in case 17I above.
9003 (c-add-stmt-syntax 'statement-block-intro nil t
9004 lim paren-state))
9005 (if (eq char-after-ip ?{)
9006 (c-add-syntax 'block-open)))
9010 ;; now we need to look at any modifiers
9011 (goto-char indent-point)
9012 (skip-chars-forward " \t")
9014 ;; are we looking at a comment only line?
9015 (when (and (looking-at c-comment-start-regexp)
9016 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
9017 (c-append-syntax 'comment-intro))
9019 ;; we might want to give additional offset to friends (in C++).
9020 (when (and c-opt-friend-key
9021 (looking-at c-opt-friend-key))
9022 (c-append-syntax 'friend))
9024 ;; Set syntactic-relpos.
9025 (let ((p c-syntactic-context))
9026 (while (and p
9027 (if (integerp (c-langelem-pos (car p)))
9028 (progn
9029 (setq syntactic-relpos (c-langelem-pos (car p)))
9030 nil)
9032 (setq p (cdr p))))
9034 ;; Start of or a continuation of a preprocessor directive?
9035 (if (and macro-start
9036 (eq macro-start (c-point 'boi))
9037 (not (and (c-major-mode-is 'pike-mode)
9038 (eq (char-after (1+ macro-start)) ?\"))))
9039 (c-append-syntax 'cpp-macro)
9040 (when (and c-syntactic-indentation-in-macros macro-start)
9041 (if in-macro-expr
9042 (when (or
9043 (< syntactic-relpos macro-start)
9044 (not (or
9045 (assq 'arglist-intro c-syntactic-context)
9046 (assq 'arglist-cont c-syntactic-context)
9047 (assq 'arglist-cont-nonempty c-syntactic-context)
9048 (assq 'arglist-close c-syntactic-context))))
9049 ;; If inside a cpp expression, i.e. anywhere in a
9050 ;; cpp directive except a #define body, we only let
9051 ;; through the syntactic analysis that is internal
9052 ;; in the expression. That means the arglist
9053 ;; elements, if they are anchored inside the cpp
9054 ;; expression.
9055 (setq c-syntactic-context nil)
9056 (c-add-syntax 'cpp-macro-cont macro-start))
9057 (when (and (eq macro-start syntactic-relpos)
9058 (not (assq 'cpp-define-intro c-syntactic-context))
9059 (save-excursion
9060 (goto-char macro-start)
9061 (or (not (c-forward-to-cpp-define-body))
9062 (<= (point) (c-point 'boi indent-point)))))
9063 ;; Inside a #define body and the syntactic analysis is
9064 ;; anchored on the start of the #define. In this case
9065 ;; we add cpp-define-intro to get the extra
9066 ;; indentation of the #define body.
9067 (c-add-syntax 'cpp-define-intro)))))
9069 ;; return the syntax
9070 c-syntactic-context)))
9073 ;; Indentation calculation.
9075 (defun c-evaluate-offset (offset langelem symbol)
9076 ;; offset can be a number, a function, a variable, a list, or one of
9077 ;; the symbols + or -
9079 ;; This function might do hidden buffer changes.
9080 (let ((res
9081 (cond
9082 ((numberp offset) offset)
9083 ((vectorp offset) offset)
9084 ((null offset) nil)
9086 ((eq offset '+) c-basic-offset)
9087 ((eq offset '-) (- c-basic-offset))
9088 ((eq offset '++) (* 2 c-basic-offset))
9089 ((eq offset '--) (* 2 (- c-basic-offset)))
9090 ((eq offset '*) (/ c-basic-offset 2))
9091 ((eq offset '/) (/ (- c-basic-offset) 2))
9093 ((functionp offset)
9094 (c-evaluate-offset
9095 (funcall offset
9096 (cons (c-langelem-sym langelem)
9097 (c-langelem-pos langelem)))
9098 langelem symbol))
9100 ((listp offset)
9101 (cond
9102 ((eq (car offset) 'quote)
9103 (c-benign-error "The offset %S for %s was mistakenly quoted"
9104 offset symbol)
9105 nil)
9107 ((memq (car offset) '(min max))
9108 (let (res val (method (car offset)))
9109 (setq offset (cdr offset))
9110 (while offset
9111 (setq val (c-evaluate-offset (car offset) langelem symbol))
9112 (cond
9113 ((not val))
9114 ((not res)
9115 (setq res val))
9116 ((integerp val)
9117 (if (vectorp res)
9118 (c-benign-error "\
9119 Error evaluating offset %S for %s: \
9120 Cannot combine absolute offset %S with relative %S in `%s' method"
9121 (car offset) symbol res val method)
9122 (setq res (funcall method res val))))
9124 (if (integerp res)
9125 (c-benign-error "\
9126 Error evaluating offset %S for %s: \
9127 Cannot combine relative offset %S with absolute %S in `%s' method"
9128 (car offset) symbol res val method)
9129 (setq res (vector (funcall method (aref res 0)
9130 (aref val 0)))))))
9131 (setq offset (cdr offset)))
9132 res))
9134 ((eq (car offset) 'add)
9135 (let (res val)
9136 (setq offset (cdr offset))
9137 (while offset
9138 (setq val (c-evaluate-offset (car offset) langelem symbol))
9139 (cond
9140 ((not val))
9141 ((not res)
9142 (setq res val))
9143 ((integerp val)
9144 (if (vectorp res)
9145 (setq res (vector (+ (aref res 0) val)))
9146 (setq res (+ res val))))
9148 (if (vectorp res)
9149 (c-benign-error "\
9150 Error evaluating offset %S for %s: \
9151 Cannot combine absolute offsets %S and %S in `add' method"
9152 (car offset) symbol res val)
9153 (setq res val)))) ; Override.
9154 (setq offset (cdr offset)))
9155 res))
9158 (let (res)
9159 (when (eq (car offset) 'first)
9160 (setq offset (cdr offset)))
9161 (while (and (not res) offset)
9162 (setq res (c-evaluate-offset (car offset) langelem symbol)
9163 offset (cdr offset)))
9164 res))))
9166 ((and (symbolp offset) (boundp offset))
9167 (symbol-value offset))
9170 (c-benign-error "Unknown offset format %S for %s" offset symbol)
9171 nil))))
9173 (if (or (null res) (integerp res)
9174 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
9176 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
9177 offset symbol res)
9178 nil)))
9180 (defun c-calc-offset (langelem)
9181 ;; Get offset from LANGELEM which is a list beginning with the
9182 ;; syntactic symbol and followed by any analysis data it provides.
9183 ;; That data may be zero or more elements, but if at least one is
9184 ;; given then the first is the anchor position (or nil). The symbol
9185 ;; is matched against `c-offsets-alist' and the offset calculated
9186 ;; from that is returned.
9188 ;; This function might do hidden buffer changes.
9189 (let* ((symbol (c-langelem-sym langelem))
9190 (match (assq symbol c-offsets-alist))
9191 (offset (cdr-safe match)))
9192 (if match
9193 (setq offset (c-evaluate-offset offset langelem symbol))
9194 (if c-strict-syntax-p
9195 (c-benign-error "No offset found for syntactic symbol %s" symbol))
9196 (setq offset 0))
9197 (if (vectorp offset)
9198 offset
9199 (or (and (numberp offset) offset)
9200 (and (symbolp offset) (symbol-value offset))
9204 (defun c-get-offset (langelem)
9205 ;; This is a compatibility wrapper for `c-calc-offset' in case
9206 ;; someone is calling it directly. It takes an old style syntactic
9207 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
9208 ;; new list form.
9210 ;; This function might do hidden buffer changes.
9211 (if (c-langelem-pos langelem)
9212 (c-calc-offset (list (c-langelem-sym langelem)
9213 (c-langelem-pos langelem)))
9214 (c-calc-offset langelem)))
9216 (defun c-get-syntactic-indentation (langelems)
9217 ;; Calculate the syntactic indentation from a syntactic description
9218 ;; as returned by `c-guess-syntax'.
9220 ;; Note that topmost-intro always has an anchor position at bol, for
9221 ;; historical reasons. It's often used together with other symbols
9222 ;; that has more sane positions. Since we always use the first
9223 ;; found anchor position, we rely on that these other symbols always
9224 ;; precede topmost-intro in the LANGELEMS list.
9226 ;; This function might do hidden buffer changes.
9227 (let ((indent 0) anchor)
9229 (while langelems
9230 (let* ((c-syntactic-element (car langelems))
9231 (res (c-calc-offset c-syntactic-element)))
9233 (if (vectorp res)
9234 ;; Got an absolute column that overrides any indentation
9235 ;; we've collected so far, but not the relative
9236 ;; indentation we might get for the nested structures
9237 ;; further down the langelems list.
9238 (setq indent (elt res 0)
9239 anchor (point-min)) ; A position at column 0.
9241 ;; Got a relative change of the current calculated
9242 ;; indentation.
9243 (setq indent (+ indent res))
9245 ;; Use the anchor position from the first syntactic
9246 ;; element with one.
9247 (unless anchor
9248 (setq anchor (c-langelem-pos (car langelems)))))
9250 (setq langelems (cdr langelems))))
9252 (if anchor
9253 (+ indent (save-excursion
9254 (goto-char anchor)
9255 (current-column)))
9256 indent)))
9259 (cc-provide 'cc-engine)
9261 ;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
9262 ;;; cc-engine.el ends here