]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-engine.el
(c-emacs-features): Do parse-partial-sexp at point, in case of narrowing.
[gnu-emacs] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985,1987,1992-2003, 2004, 2005 Free Software Foundation,
4 ;; Inc.
5
6 ;; Authors: 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs and Stewart Clamen
9 ;; 1985 Richard M. Stallman
10 ;; Maintainer: bug-cc-mode@gnu.org
11 ;; Created: 22-Apr-1997 (split from cc-mode.el)
12 ;; Version: See cc-mode.el
13 ;; Keywords: c languages oop
14
15 ;; This file is part of GNU Emacs.
16
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; any later version.
21
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with this program; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
31
32 ;;; Commentary:
33
34 ;; The functions which have docstring documentation can be considered
35 ;; part of an API which other packages can use in CC Mode buffers.
36 ;; Otoh, undocumented functions and functions with the documentation
37 ;; in comments are considered purely internal and can change semantics
38 ;; or even disappear in the future.
39 ;;
40 ;; (This policy applies to CC Mode as a whole, not just this file. It
41 ;; probably also applies to many other Emacs packages, but here it's
42 ;; clearly spelled out.)
43
44 ;; Hidden buffer changes
45 ;;
46 ;; Various functions in CC Mode use text properties for caching and
47 ;; syntactic markup purposes, and those of them that might modify such
48 ;; properties but still don't modify the buffer in a visible way are
49 ;; said to do "hidden buffer changes". They should be used within
50 ;; `c-save-buffer-state' or a similar function that saves and restores
51 ;; buffer modifiedness, disables buffer change hooks, etc.
52 ;;
53 ;; Interactive functions are assumed to not do hidden buffer changes,
54 ;; except in the specific parts of them that do real changes.
55 ;;
56 ;; Lineup functions are assumed to do hidden buffer changes. They
57 ;; must not do real changes, though.
58 ;;
59 ;; All other functions that do hidden buffer changes have that noted
60 ;; in their doc string or comment.
61 ;;
62 ;; The intention with this system is to avoid wrapping every leaf
63 ;; function that do hidden buffer changes inside
64 ;; `c-save-buffer-state'. It should be used as near the top of the
65 ;; interactive functions as possible.
66 ;;
67 ;; Functions called during font locking are allowed to do hidden
68 ;; buffer changes since the font-lock package run them in a context
69 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
70 ;; inspired by `save-buffer-state' in the font-lock package).
71
72 ;; Use of text properties
73 ;;
74 ;; CC Mode uses several text properties internally to mark up various
75 ;; positions, e.g. to improve speed and to eliminate glitches in
76 ;; interactive refontification.
77 ;;
78 ;; Note: This doc is for internal use only. Other packages should not
79 ;; assume that these text properties are used as described here.
80 ;;
81 ;; 'syntax-table
82 ;; Used to modify the syntax of some characters. Currently used to
83 ;; mark the "<" and ">" of angle bracket parens with paren syntax.
84 ;;
85 ;; This property is used on single characters and is therefore
86 ;; always treated as front and rear nonsticky (or start and end open
87 ;; in XEmacs vocabulary). It's therefore installed on
88 ;; `text-property-default-nonsticky' if that variable exists (Emacs
89 ;; >= 21).
90 ;;
91 ;; 'c-is-sws and 'c-in-sws
92 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
93 ;; speed them up. See the comment blurb before `c-put-is-sws'
94 ;; below for further details.
95 ;;
96 ;; 'c-type
97 ;; This property is used on single characters to mark positions with
98 ;; special syntactic relevance of various sorts. Its primary use is
99 ;; to avoid glitches when multiline constructs are refontified
100 ;; interactively (on font lock decoration level 3). It's cleared in
101 ;; a region before it's fontified and is then put on relevant chars
102 ;; in that region as they are encountered during the fontification.
103 ;; The value specifies the kind of position:
104 ;;
105 ;; 'c-decl-arg-start
106 ;; Put on the last char of the token preceding each declaration
107 ;; inside a declaration style arglist (typically in a function
108 ;; prototype).
109 ;;
110 ;; 'c-decl-end
111 ;; Put on the last char of the token preceding a declaration.
112 ;; This is used in cases where declaration boundaries can't be
113 ;; recognized simply by looking for a token like ";" or "}".
114 ;; `c-type-decl-end-used' must be set if this is used (see also
115 ;; `c-find-decl-spots').
116 ;;
117 ;; 'c-<>-arg-sep
118 ;; Put on the commas that separate arguments in angle bracket
119 ;; arglists like C++ template arglists.
120 ;;
121 ;; 'c-decl-id-start and 'c-decl-type-start
122 ;; Put on the last char of the token preceding each declarator
123 ;; in the declarator list of a declaration. They are also used
124 ;; between the identifiers cases like enum declarations.
125 ;; 'c-decl-type-start is used when the declarators are types,
126 ;; 'c-decl-id-start otherwise.
127 ;;
128 ;; 'c-awk-NL-prop
129 ;; Used in AWK mode to mark the various kinds of newlines. See
130 ;; cc-awk.el.
131
132 ;;; Code:
133
134 (eval-when-compile
135 (let ((load-path
136 (if (and (boundp 'byte-compile-dest-file)
137 (stringp byte-compile-dest-file))
138 (cons (file-name-directory byte-compile-dest-file) load-path)
139 load-path)))
140 (load "cc-bytecomp" nil t)))
141
142 (cc-require 'cc-defs)
143 (cc-require-when-compile 'cc-langs)
144 (cc-require 'cc-vars)
145
146 ;; Silence the compiler.
147 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
148
149 \f
150 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
151
152 (defmacro c-declare-lang-variables ()
153 `(progn
154 ,@(apply 'nconc
155 (mapcar (lambda (init)
156 `(,(if (elt init 2)
157 `(defvar ,(car init) nil ,(elt init 2))
158 `(defvar ,(car init) nil))
159 (make-variable-buffer-local ',(car init))))
160 (cdr c-lang-variable-inits)))))
161 (c-declare-lang-variables)
162
163 \f
164 ;;; Internal state variables.
165
166 ;; Internal state of hungry delete key feature
167 (defvar c-hungry-delete-key nil)
168 (make-variable-buffer-local 'c-hungry-delete-key)
169
170 ;; The electric flag (toggled by `c-toggle-electric-state').
171 ;; If t, electric actions (like automatic reindentation, and (if
172 ;; c-auto-newline is also set) auto newlining) will happen when an electric
173 ;; key like `{' is pressed (or an electric keyword like `else').
174 (defvar c-electric-flag t)
175 (make-variable-buffer-local 'c-electric-flag)
176
177 ;; Internal state of auto newline feature.
178 (defvar c-auto-newline nil)
179 (make-variable-buffer-local 'c-auto-newline)
180
181 ;; Included in the mode line to indicate the active submodes.
182 (defvar c-submode-indicators nil)
183 (make-variable-buffer-local 'c-submode-indicators)
184
185 (defun c-calculate-state (arg prevstate)
186 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
187 ;; arg is nil or zero, toggle the state. If arg is negative, turn
188 ;; the state off, and if arg is positive, turn the state on
189 (if (or (not arg)
190 (zerop (setq arg (prefix-numeric-value arg))))
191 (not prevstate)
192 (> arg 0)))
193
194 ;; Dynamically bound cache for `c-in-literal'.
195 (defvar c-in-literal-cache t)
196
197 \f
198 ;; Basic handling of preprocessor directives.
199
200 ;; This is a dynamically bound cache used together with
201 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
202 ;; works as long as point doesn't cross a macro boundary.
203 (defvar c-macro-start 'unknown)
204
205 (defsubst c-query-and-set-macro-start ()
206 (if (symbolp c-macro-start)
207 (setq c-macro-start (save-excursion
208 (c-save-buffer-state ()
209 (and (c-beginning-of-macro)
210 (point)))))
211 c-macro-start))
212
213 (defsubst c-query-macro-start ()
214 (if (symbolp c-macro-start)
215 (save-excursion
216 (c-save-buffer-state ()
217 (and (c-beginning-of-macro)
218 (point))))
219 c-macro-start))
220
221 (defun c-beginning-of-macro (&optional lim)
222 "Go to the beginning of a preprocessor directive.
223 Leave point at the beginning of the directive and return t if in one,
224 otherwise return nil and leave point unchanged.
225
226 Note that this function might do hidden buffer changes. See the
227 comment at the start of cc-engine.el for more info."
228 (when c-opt-cpp-prefix
229 (let ((here (point)))
230 (save-restriction
231 (if lim (narrow-to-region lim (point-max)))
232 (beginning-of-line)
233 (while (eq (char-before (1- (point))) ?\\)
234 (forward-line -1))
235 (back-to-indentation)
236 (if (and (<= (point) here)
237 (looking-at c-opt-cpp-start))
238 t
239 (goto-char here)
240 nil)))))
241
242 (defun c-end-of-macro ()
243 "Go to the end of a preprocessor directive.
244 More accurately, move the point to the end of the closest following
245 line that doesn't end with a line continuation backslash - no check is
246 done that the point is inside a cpp directive to begin with.
247
248 Note that this function might do hidden buffer changes. See the
249 comment at the start of cc-engine.el for more info."
250 (while (progn
251 (end-of-line)
252 (when (and (eq (char-before) ?\\)
253 (not (eobp)))
254 (forward-char)
255 t))))
256
257 (defun c-forward-to-cpp-define-body ()
258 ;; Assuming point is at the "#" that introduces a preprocessor
259 ;; directive, it's moved forward to the start of the definition body
260 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
261 ;; specifies). Non-nil is returned in this case, in all other cases
262 ;; nil is returned and point isn't moved.
263 ;;
264 ;; This function might do hidden buffer changes.
265 (when (and c-opt-cpp-macro-define-start
266 (looking-at c-opt-cpp-macro-define-start)
267 (not (= (match-end 0) (c-point 'eol))))
268 (goto-char (match-end 0))))
269
270 \f
271 ;;; Basic utility functions.
272
273 (defun c-syntactic-content (from to paren-level)
274 ;; Return the given region as a string where all syntactic
275 ;; whitespace is removed or, where necessary, replaced with a single
276 ;; space. If PAREN-LEVEL is given then all parens in the region are
277 ;; collapsed to "()", "[]" etc.
278 ;;
279 ;; This function might do hidden buffer changes.
280
281 (save-excursion
282 (save-restriction
283 (narrow-to-region from to)
284 (goto-char from)
285 (let* ((parts (list nil)) (tail parts) pos in-paren)
286
287 (while (re-search-forward c-syntactic-ws-start to t)
288 (goto-char (setq pos (match-beginning 0)))
289 (c-forward-syntactic-ws)
290 (if (= (point) pos)
291 (forward-char)
292
293 (when paren-level
294 (save-excursion
295 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
296 pos (point))))
297
298 (if (and (> pos from)
299 (< (point) to)
300 (looking-at "\\w\\|\\s_")
301 (save-excursion
302 (goto-char (1- pos))
303 (looking-at "\\w\\|\\s_")))
304 (progn
305 (setcdr tail (list (buffer-substring-no-properties from pos)
306 " "))
307 (setq tail (cddr tail)))
308 (setcdr tail (list (buffer-substring-no-properties from pos)))
309 (setq tail (cdr tail)))
310
311 (when in-paren
312 (when (= (car (parse-partial-sexp pos to -1)) -1)
313 (setcdr tail (list (buffer-substring-no-properties
314 (1- (point)) (point))))
315 (setq tail (cdr tail))))
316
317 (setq from (point))))
318
319 (setcdr tail (list (buffer-substring-no-properties from to)))
320 (apply 'concat (cdr parts))))))
321
322 (defun c-shift-line-indentation (shift-amt)
323 ;; Shift the indentation of the current line with the specified
324 ;; amount (positive inwards). The buffer is modified only if
325 ;; SHIFT-AMT isn't equal to zero.
326 (let ((pos (- (point-max) (point)))
327 (c-macro-start c-macro-start)
328 tmp-char-inserted)
329 (if (zerop shift-amt)
330 nil
331 ;; If we're on an empty line inside a macro, we take the point
332 ;; to be at the current indentation and shift it to the
333 ;; appropriate column. This way we don't treat the extra
334 ;; whitespace out to the line continuation as indentation.
335 (when (and (c-query-and-set-macro-start)
336 (looking-at "[ \t]*\\\\$")
337 (save-excursion
338 (skip-chars-backward " \t")
339 (bolp)))
340 (insert ?x)
341 (backward-char)
342 (setq tmp-char-inserted t))
343 (unwind-protect
344 (let ((col (current-indentation)))
345 (delete-region (c-point 'bol) (c-point 'boi))
346 (beginning-of-line)
347 (indent-to (+ col shift-amt)))
348 (when tmp-char-inserted
349 (delete-char 1))))
350 ;; If initial point was within line's indentation and we're not on
351 ;; a line with a line continuation in a macro, position after the
352 ;; indentation. Else stay at same point in text.
353 (if (and (< (point) (c-point 'boi))
354 (not tmp-char-inserted))
355 (back-to-indentation)
356 (if (> (- (point-max) pos) (point))
357 (goto-char (- (point-max) pos))))))
358
359 (defsubst c-keyword-sym (keyword)
360 ;; Return non-nil if the string KEYWORD is a known keyword. More
361 ;; precisely, the value is the symbol for the keyword in
362 ;; `c-keywords-obarray'.
363 (intern-soft keyword c-keywords-obarray))
364
365 (defsubst c-keyword-member (keyword-sym lang-constant)
366 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
367 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
368 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
369 ;; nil then the result is nil.
370 (get keyword-sym lang-constant))
371
372 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
373 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
374 "\"|"
375 "\""))
376
377 ;; Regexp matching string limit syntax.
378 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
379 "\\s\"\\|\\s|"
380 "\\s\""))
381
382 ;; Regexp matching WS followed by string limit syntax.
383 (defconst c-ws*-string-limit-regexp
384 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
385
386 ;; Holds formatted error strings for the few cases where parse errors
387 ;; are reported.
388 (defvar c-parsing-error nil)
389 (make-variable-buffer-local 'c-parsing-error)
390
391 (defun c-echo-parsing-error (&optional quiet)
392 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
393 (c-benign-error "%s" c-parsing-error))
394 c-parsing-error)
395
396 ;; Faces given to comments and string literals. This is used in some
397 ;; situations to speed up recognition; it isn't mandatory that font
398 ;; locking is in use. This variable is extended with the face in
399 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
400 (defvar c-literal-faces
401 (append '(font-lock-comment-face font-lock-string-face)
402 (when (facep 'font-lock-comment-delimiter-face)
403 ;; New in Emacs 22.
404 '(font-lock-comment-delimiter-face))))
405
406 (defsubst c-put-c-type-property (pos value)
407 ;; Put a c-type property with the given value at POS.
408 (c-put-char-property pos 'c-type value))
409
410 (defun c-clear-c-type-property (from to value)
411 ;; Remove all occurences of the c-type property that has the given
412 ;; value in the region between FROM and TO. VALUE is assumed to not
413 ;; be nil.
414 ;;
415 ;; Note: This assumes that c-type is put on single chars only; it's
416 ;; very inefficient if matching properties cover large regions.
417 (save-excursion
418 (goto-char from)
419 (while (progn
420 (when (eq (get-text-property (point) 'c-type) value)
421 (c-clear-char-property (point) 'c-type))
422 (goto-char (next-single-property-change (point) 'c-type nil to))
423 (< (point) to)))))
424
425 \f
426 ;; Some debug tools to visualize various special positions. This
427 ;; debug code isn't as portable as the rest of CC Mode.
428
429 (cc-bytecomp-defun overlays-in)
430 (cc-bytecomp-defun overlay-get)
431 (cc-bytecomp-defun overlay-start)
432 (cc-bytecomp-defun overlay-end)
433 (cc-bytecomp-defun delete-overlay)
434 (cc-bytecomp-defun overlay-put)
435 (cc-bytecomp-defun make-overlay)
436
437 (defun c-debug-add-face (beg end face)
438 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
439 (while overlays
440 (setq overlay (car overlays)
441 overlays (cdr overlays))
442 (when (eq (overlay-get overlay 'face) face)
443 (setq beg (min beg (overlay-start overlay))
444 end (max end (overlay-end overlay)))
445 (delete-overlay overlay)))
446 (overlay-put (make-overlay beg end) 'face face)))
447
448 (defun c-debug-remove-face (beg end face)
449 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
450 (ol-beg beg) (ol-end end))
451 (while overlays
452 (setq overlay (car overlays)
453 overlays (cdr overlays))
454 (when (eq (overlay-get overlay 'face) face)
455 (setq ol-beg (min ol-beg (overlay-start overlay))
456 ol-end (max ol-end (overlay-end overlay)))
457 (delete-overlay overlay)))
458 (when (< ol-beg beg)
459 (overlay-put (make-overlay ol-beg beg) 'face face))
460 (when (> ol-end end)
461 (overlay-put (make-overlay end ol-end) 'face face))))
462
463 \f
464 ;; `c-beginning-of-statement-1' and accompanying stuff.
465
466 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
467 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
468 ;; better way should be implemented, but this will at least shut up
469 ;; the byte compiler.
470 (defvar c-maybe-labelp)
471
472 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
473
474 ;; Macros used internally in c-beginning-of-statement-1 for the
475 ;; automaton actions.
476 (defmacro c-bos-push-state ()
477 '(setq stack (cons (cons state saved-pos)
478 stack)))
479 (defmacro c-bos-pop-state (&optional do-if-done)
480 `(if (setq state (car (car stack))
481 saved-pos (cdr (car stack))
482 stack (cdr stack))
483 t
484 ,do-if-done
485 (throw 'loop nil)))
486 (defmacro c-bos-pop-state-and-retry ()
487 '(throw 'loop (setq state (car (car stack))
488 saved-pos (cdr (car stack))
489 ;; Throw nil if stack is empty, else throw non-nil.
490 stack (cdr stack))))
491 (defmacro c-bos-save-pos ()
492 '(setq saved-pos (vector pos tok ptok pptok)))
493 (defmacro c-bos-restore-pos ()
494 '(unless (eq (elt saved-pos 0) start)
495 (setq pos (elt saved-pos 0)
496 tok (elt saved-pos 1)
497 ptok (elt saved-pos 2)
498 pptok (elt saved-pos 3))
499 (goto-char pos)
500 (setq sym nil)))
501 (defmacro c-bos-save-error-info (missing got)
502 `(setq saved-pos (vector pos ,missing ,got)))
503 (defmacro c-bos-report-error ()
504 '(unless noerror
505 (setq c-parsing-error
506 (format "No matching `%s' found for `%s' on line %d"
507 (elt saved-pos 1)
508 (elt saved-pos 2)
509 (1+ (count-lines (point-min)
510 (c-point 'bol (elt saved-pos 0))))))))
511
512 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
513 noerror comma-delim)
514 "Move to the start of the current statement or declaration, or to
515 the previous one if already at the beginning of one. Only
516 statements/declarations on the same level are considered, i.e. don't
517 move into or out of sexps (not even normal expression parentheses).
518
519 Stop at statement continuation tokens like \"else\", \"catch\",
520 \"finally\" and the \"while\" in \"do ... while\" if the start point
521 is within the continuation. If starting at such a token, move to the
522 corresponding statement start. If at the beginning of a statement,
523 move to the closest containing statement if there is any. This might
524 also stop at a continuation clause.
525
526 Labels are treated as part of the following statements if
527 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
528 statement start keyword.)
529
530 Macros are ignored unless point is within one, in which case the
531 content of the macro is treated as normal code. Aside from any normal
532 statement starts found in it, stop at the first token of the content
533 in the macro, i.e. the expression of an \"#if\" or the start of the
534 definition in a \"#define\". Also stop at start of macros before
535 leaving them.
536
537 Return 'label if stopped at a label, 'same if stopped at the beginning
538 of the current statement, 'up if stepped to a containing statement,
539 'previous if stepped to a preceding statement, 'beginning if stepped
540 from a statement continuation clause to its start clause, or 'macro if
541 stepped to a macro start. Note that 'same and not 'label is returned
542 if stopped at the same label without crossing the colon character.
543
544 LIM may be given to limit the search. If the search hits the limit,
545 point will be left at the closest following token, or at the start
546 position if that is less ('same is returned in this case).
547
548 NOERROR turns off error logging to `c-parsing-error'.
549
550 Normally only ';' is considered to delimit statements, but if
551 COMMA-DELIM is non-nil then ',' is treated likewise.
552
553 Note that this function might do hidden buffer changes. See the
554 comment at the start of cc-engine.el for more info."
555
556 ;; The bulk of this function is a pushdown automaton that looks at statement
557 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
558 ;; purpose is to keep track of nested statements, ensuring that such
559 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
560 ;; does with nested braces/brackets/parentheses).
561 ;;
562 ;; Note: The position of a boundary is the following token.
563 ;;
564 ;; Beginning with the current token (the one following point), move back one
565 ;; sexp at a time (where a sexp is, more or less, either a token or the
566 ;; entire contents of a brace/bracket/paren pair). Each time a statement
567 ;; boundary is crossed or a "while"-like token is found, update the state of
568 ;; the PDA. Stop at the beginning of a statement when the stack (holding
569 ;; nested statement info) is empty and the position has been moved.
570 ;;
571 ;; The following variables constitute the PDA:
572 ;;
573 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
574 ;; scanned back over, 'boundary if we've just gone back over a
575 ;; statement boundary, or nil otherwise.
576 ;; state: takes one of the values (nil else else-boundary while
577 ;; while-boundary catch catch-boundary).
578 ;; nil means "no "while"-like token yet scanned".
579 ;; 'else, for example, means "just gone back over an else".
580 ;; 'else-boundary means "just gone back over a statement boundary
581 ;; immediately after having gone back over an else".
582 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
583 ;; of error reporting information.
584 ;; stack: The stack onto which the PDA pushes its state. Each entry
585 ;; consists of a saved value of state and saved-pos. An entry is
586 ;; pushed when we move back over a "continuation" token (e.g. else)
587 ;; and popped when we encounter the corresponding opening token
588 ;; (e.g. if).
589 ;;
590 ;;
591 ;; The following diagram briefly outlines the PDA.
592 ;;
593 ;; Common state:
594 ;; "else": Push state, goto state `else'.
595 ;; "while": Push state, goto state `while'.
596 ;; "catch" or "finally": Push state, goto state `catch'.
597 ;; boundary: Pop state.
598 ;; other: Do nothing special.
599 ;;
600 ;; State `else':
601 ;; boundary: Goto state `else-boundary'.
602 ;; other: Error, pop state, retry token.
603 ;;
604 ;; State `else-boundary':
605 ;; "if": Pop state.
606 ;; boundary: Error, pop state.
607 ;; other: See common state.
608 ;;
609 ;; State `while':
610 ;; boundary: Save position, goto state `while-boundary'.
611 ;; other: Pop state, retry token.
612 ;;
613 ;; State `while-boundary':
614 ;; "do": Pop state.
615 ;; boundary: Restore position if it's not at start, pop state. [*see below]
616 ;; other: See common state.
617 ;;
618 ;; State `catch':
619 ;; boundary: Goto state `catch-boundary'.
620 ;; other: Error, pop state, retry token.
621 ;;
622 ;; State `catch-boundary':
623 ;; "try": Pop state.
624 ;; "catch": Goto state `catch'.
625 ;; boundary: Error, pop state.
626 ;; other: See common state.
627 ;;
628 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
629 ;; searching for a "do" which would have opened a do-while. If we didn't
630 ;; find it, we discard the analysis done since the "while", go back to this
631 ;; token in the buffer and restart the scanning there, this time WITHOUT
632 ;; pushing the 'while state onto the stack.
633 ;;
634 ;; In addition to the above there is some special handling of labels
635 ;; and macros.
636
637 (let ((case-fold-search nil)
638 (start (point))
639 macro-start
640 (delims (if comma-delim '(?\; ?,) '(?\;)))
641 (c-stmt-delim-chars (if comma-delim
642 c-stmt-delim-chars-with-comma
643 c-stmt-delim-chars))
644 c-in-literal-cache c-maybe-labelp saved
645 ;; Current position.
646 pos
647 ;; Position of last stmt boundary character (e.g. ;).
648 boundary-pos
649 ;; The position of the last sexp or bound that follows the
650 ;; first found colon, i.e. the start of the nonlabel part of
651 ;; the statement. It's `start' if a colon is found just after
652 ;; the start.
653 after-labels-pos
654 ;; Like `after-labels-pos', but the first such position inside
655 ;; a label, i.e. the start of the last label before the start
656 ;; of the nonlabel part of the statement.
657 last-label-pos
658 ;; The last position where a label is possible provided the
659 ;; statement started there. It's nil as long as no invalid
660 ;; label content has been found (according to
661 ;; `c-nonlabel-token-key'. It's `start' if no valid label
662 ;; content was found in the label. Note that we might still
663 ;; regard it a label if it starts with `c-label-kwds'.
664 label-good-pos
665 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
666 ;; See above.
667 sym
668 ;; Current state in the automaton. See above.
669 state
670 ;; Current saved positions. See above.
671 saved-pos
672 ;; Stack of conses (state . saved-pos).
673 stack
674 ;; Regexp which matches "for", "if", etc.
675 (cond-key (or c-opt-block-stmt-key
676 "\\<\\>")) ; Matches nothing.
677 ;; Return value.
678 (ret 'same)
679 ;; Positions of the last three sexps or bounds we've stopped at.
680 tok ptok pptok)
681
682 (save-restriction
683 (if lim (narrow-to-region lim (point-max)))
684
685 (if (save-excursion
686 (and (c-beginning-of-macro)
687 (/= (point) start)))
688 (setq macro-start (point)))
689
690 ;; Try to skip back over unary operator characters, to register
691 ;; that we've moved.
692 (while (progn
693 (setq pos (point))
694 (c-backward-syntactic-ws)
695 ;; Protect post-++/-- operators just before a virtual semicolon.
696 (and (not (c-at-vsemi-p))
697 (/= (skip-chars-backward "-+!*&~@`#") 0))))
698
699 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
700 ;; done. Later on we ignore the boundaries for statements that don't
701 ;; contain any sexp. The only thing that is affected is that the error
702 ;; checking is a little less strict, and we really don't bother.
703 (if (and (memq (char-before) delims)
704 (progn (forward-char -1)
705 (setq saved (point))
706 (c-backward-syntactic-ws)
707 (or (memq (char-before) delims)
708 (memq (char-before) '(?: nil))
709 (eq (char-syntax (char-before)) ?\()
710 (c-at-vsemi-p))))
711 (setq ret 'previous
712 pos saved)
713
714 ;; Begin at start and not pos to detect macros if we stand
715 ;; directly after the #.
716 (goto-char start)
717 (if (looking-at "\\<\\|\\W")
718 ;; Record this as the first token if not starting inside it.
719 (setq tok start))
720
721 ;; The following while loop goes back one sexp (balanced parens,
722 ;; etc. with contents, or symbol or suchlike) each iteration. This
723 ;; movement is accomplished with a call to scan-sexps approx 130 lines
724 ;; below.
725 (while
726 (catch 'loop ;; Throw nil to break, non-nil to continue.
727 (cond
728 ((save-excursion
729 (and macro-start ; Always NIL for AWK.
730 (progn (skip-chars-backward " \t")
731 (eq (char-before) ?#))
732 (progn (setq saved (1- (point)))
733 (beginning-of-line)
734 (not (eq (char-before (1- (point))) ?\\)))
735 (looking-at c-opt-cpp-start)
736 (progn (skip-chars-forward " \t")
737 (eq (point) saved))))
738 (goto-char saved)
739 (if (and (c-forward-to-cpp-define-body)
740 (progn (c-forward-syntactic-ws start)
741 (< (point) start)))
742 ;; Stop at the first token in the content of the macro.
743 (setq pos (point)
744 ignore-labels t) ; Avoid the label check on exit.
745 (setq pos saved
746 ret 'macro
747 ignore-labels t))
748 (throw 'loop nil))
749
750 ;; Do a round through the automaton if we've just passed a
751 ;; statement boundary or passed a "while"-like token.
752 ((or sym
753 (and (looking-at cond-key)
754 (setq sym (intern (match-string 1)))))
755
756 (when (and (< pos start) (null stack))
757 (throw 'loop nil))
758
759 ;; The PDA state handling.
760 ;;
761 ;; Refer to the description of the PDA in the opening
762 ;; comments. In the following OR form, the first leaf
763 ;; attempts to handles one of the specific actions detailed
764 ;; (e.g., finding token "if" whilst in state `else-boundary').
765 ;; We drop through to the second leaf (which handles common
766 ;; state) if no specific handler is found in the first cond.
767 ;; If a parsing error is detected (e.g. an "else" with no
768 ;; preceding "if"), we throw to the enclosing catch.
769 ;;
770 ;; Note that the (eq state 'else) means
771 ;; "we've just passed an else", NOT "we're looking for an
772 ;; else".
773 (or (cond
774 ((eq state 'else)
775 (if (eq sym 'boundary)
776 (setq state 'else-boundary)
777 (c-bos-report-error)
778 (c-bos-pop-state-and-retry)))
779
780 ((eq state 'else-boundary)
781 (cond ((eq sym 'if)
782 (c-bos-pop-state (setq ret 'beginning)))
783 ((eq sym 'boundary)
784 (c-bos-report-error)
785 (c-bos-pop-state))))
786
787 ((eq state 'while)
788 (if (and (eq sym 'boundary)
789 ;; Since this can cause backtracking we do a
790 ;; little more careful analysis to avoid it:
791 ;; If there's a label in front of the while
792 ;; it can't be part of a do-while.
793 (not after-labels-pos))
794 (progn (c-bos-save-pos)
795 (setq state 'while-boundary))
796 (c-bos-pop-state-and-retry))) ; Can't be a do-while
797
798 ((eq state 'while-boundary)
799 (cond ((eq sym 'do)
800 (c-bos-pop-state (setq ret 'beginning)))
801 ((eq sym 'boundary) ; isn't a do-while
802 (c-bos-restore-pos) ; the position of the while
803 (c-bos-pop-state)))) ; no longer searching for do.
804
805 ((eq state 'catch)
806 (if (eq sym 'boundary)
807 (setq state 'catch-boundary)
808 (c-bos-report-error)
809 (c-bos-pop-state-and-retry)))
810
811 ((eq state 'catch-boundary)
812 (cond
813 ((eq sym 'try)
814 (c-bos-pop-state (setq ret 'beginning)))
815 ((eq sym 'catch)
816 (setq state 'catch))
817 ((eq sym 'boundary)
818 (c-bos-report-error)
819 (c-bos-pop-state)))))
820
821 ;; This is state common. We get here when the previous
822 ;; cond statement found no particular state handler.
823 (cond ((eq sym 'boundary)
824 ;; If we have a boundary at the start
825 ;; position we push a frame to go to the
826 ;; previous statement.
827 (if (>= pos start)
828 (c-bos-push-state)
829 (c-bos-pop-state)))
830 ((eq sym 'else)
831 (c-bos-push-state)
832 (c-bos-save-error-info 'if 'else)
833 (setq state 'else))
834 ((eq sym 'while)
835 ;; Is this a real while, or a do-while?
836 ;; The next `when' triggers unless we are SURE that
837 ;; the `while' is not the tailend of a `do-while'.
838 (when (or (not pptok)
839 (memq (char-after pptok) delims)
840 ;; The following kludge is to prevent
841 ;; infinite recursion when called from
842 ;; c-awk-after-if-for-while-condition-p,
843 ;; or the like.
844 (and (eq (point) start)
845 (c-vsemi-status-unknown-p))
846 (c-at-vsemi-p pptok))
847 ;; Since this can cause backtracking we do a
848 ;; little more careful analysis to avoid it: If
849 ;; the while isn't followed by a (possibly
850 ;; virtual) semicolon it can't be a do-while.
851 (c-bos-push-state)
852 (setq state 'while)))
853 ((memq sym '(catch finally))
854 (c-bos-push-state)
855 (c-bos-save-error-info 'try sym)
856 (setq state 'catch))))
857
858 (when c-maybe-labelp
859 ;; We're either past a statement boundary or at the
860 ;; start of a statement, so throw away any label data
861 ;; for the previous one.
862 (setq after-labels-pos nil
863 last-label-pos nil
864 c-maybe-labelp nil))))
865
866 ;; Step to the previous sexp, but not if we crossed a
867 ;; boundary, since that doesn't consume an sexp.
868 (if (eq sym 'boundary)
869 (setq ret 'previous)
870
871 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
872 ;; BACKWARDS THROUGH THE SOURCE.
873
874 ;; This is typically fast with the caching done by
875 ;; c-(backward|forward)-sws.
876 (c-backward-syntactic-ws)
877
878 (let ((before-sws-pos (point))
879 ;; Set as long as we have to continue jumping by sexps.
880 ;; It's the position to use as end in the next round.
881 sexp-loop-continue-pos
882 ;; The end position of the area to search for statement
883 ;; barriers in this round.
884 (sexp-loop-end-pos pos))
885
886 (while
887 (progn
888 (unless (c-safe (c-backward-sexp) t)
889 ;; Give up if we hit an unbalanced block. Since the
890 ;; stack won't be empty the code below will report a
891 ;; suitable error.
892 (throw 'loop nil))
893
894 ;; Check if the sexp movement crossed a statement or
895 ;; declaration boundary. But first modify the point
896 ;; so that `c-crosses-statement-barrier-p' only looks
897 ;; at the non-sexp chars following the sexp.
898 (save-excursion
899 (when (setq
900 boundary-pos
901 (cond
902 ((if macro-start
903 nil
904 (save-excursion
905 (when (c-beginning-of-macro)
906 ;; Set continuation position in case
907 ;; `c-crosses-statement-barrier-p'
908 ;; doesn't detect anything below.
909 (setq sexp-loop-continue-pos (point)))))
910 ;; If the sexp movement took us into a
911 ;; macro then there were only some non-sexp
912 ;; chars after it. Skip out of the macro
913 ;; to analyze them but not the non-sexp
914 ;; chars that might be inside the macro.
915 (c-end-of-macro)
916 (c-crosses-statement-barrier-p
917 (point) sexp-loop-end-pos))
918
919 ((and
920 (eq (char-after) ?{)
921 (not (c-looking-at-inexpr-block lim nil t)))
922 ;; Passed a block sexp. That's a boundary
923 ;; alright.
924 (point))
925
926 ((looking-at "\\s\(")
927 ;; Passed some other paren. Only analyze
928 ;; the non-sexp chars after it.
929 (goto-char (1+ (c-down-list-backward
930 before-sws-pos)))
931 ;; We're at a valid token start position
932 ;; (outside the `save-excursion') if
933 ;; `c-crosses-statement-barrier-p' failed.
934 (c-crosses-statement-barrier-p
935 (point) sexp-loop-end-pos))
936
937 (t
938 ;; Passed a symbol sexp or line
939 ;; continuation. It doesn't matter that
940 ;; it's included in the analyzed region.
941 (if (c-crosses-statement-barrier-p
942 (point) sexp-loop-end-pos)
943 t
944 ;; If it was a line continuation then we
945 ;; have to continue looping.
946 (if (looking-at "\\\\$")
947 (setq sexp-loop-continue-pos (point)))
948 nil))))
949
950 (setq pptok ptok
951 ptok tok
952 tok boundary-pos
953 sym 'boundary)
954 ;; Like a C "continue". Analyze the next sexp.
955 (throw 'loop t)))
956
957 sexp-loop-continue-pos)
958 (goto-char sexp-loop-continue-pos)
959 (setq sexp-loop-end-pos sexp-loop-continue-pos
960 sexp-loop-continue-pos nil))))
961
962 ;; ObjC method def?
963 (when (and c-opt-method-key
964 (setq saved (c-in-method-def-p)))
965 (setq pos saved
966 ignore-labels t) ; Avoid the label check on exit.
967 (throw 'loop nil))
968
969 ;; Handle labels.
970 (unless (eq ignore-labels t)
971 (when (numberp c-maybe-labelp)
972 ;; `c-crosses-statement-barrier-p' has found a
973 ;; colon, so we might be in a label now.
974 (if after-labels-pos
975 (if (not last-label-pos)
976 (setq last-label-pos (or tok start)))
977 (setq after-labels-pos (or tok start)))
978 (setq c-maybe-labelp t
979 label-good-pos nil))
980
981 (when (and (not label-good-pos)
982 (looking-at c-nonlabel-token-key))
983 ;; We're in a potential label and it's the first
984 ;; time we've found something that isn't allowed in
985 ;; one.
986 (setq label-good-pos (or tok start))))
987
988 ;; We've moved back by a sexp, so update the token positions.
989 (setq sym nil
990 pptok ptok
991 ptok tok
992 tok (point)
993 pos tok))) ; Not nil (for the while loop).
994
995 ;; If the stack isn't empty there might be errors to report.
996 (while stack
997 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
998 (c-bos-report-error))
999 (setq saved-pos (cdr (car stack))
1000 stack (cdr stack)))
1001
1002 (when (and (eq ret 'same)
1003 (not (memq sym '(boundary ignore nil))))
1004 ;; Need to investigate closer whether we've crossed
1005 ;; between a substatement and its containing statement.
1006 (if (setq saved (if (looking-at c-block-stmt-1-key)
1007 ptok
1008 pptok))
1009 (cond ((> start saved) (setq pos saved))
1010 ((= start saved) (setq ret 'up)))))
1011
1012 (when (and (not ignore-labels)
1013 (eq c-maybe-labelp t)
1014 (not (eq ret 'beginning))
1015 after-labels-pos
1016 (or (not label-good-pos)
1017 (<= label-good-pos pos)
1018 (progn
1019 (goto-char (if (and last-label-pos
1020 (< last-label-pos start))
1021 last-label-pos
1022 pos))
1023 (looking-at c-label-kwds-regexp))))
1024 ;; We're in a label. Maybe we should step to the statement
1025 ;; after it.
1026 (if (< after-labels-pos start)
1027 (setq pos after-labels-pos)
1028 (setq ret 'label)
1029 (if (and last-label-pos (< last-label-pos start))
1030 ;; Might have jumped over several labels. Go to the last one.
1031 (setq pos last-label-pos)))))
1032
1033 ;; Skip over the unary operators that can start the statement.
1034 (goto-char pos)
1035 (while (progn
1036 (c-backward-syntactic-ws)
1037 ;; protect AWK post-inc/decrement operators, etc.
1038 (and (not (c-at-vsemi-p (point)))
1039 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1040 (setq pos (point)))
1041 (goto-char pos)
1042 ret)))
1043
1044 (defun c-crosses-statement-barrier-p (from to)
1045 "Return non-nil if buffer positions FROM to TO cross one or more
1046 statement or declaration boundaries. The returned value is actually
1047 the position of the earliest boundary char. FROM must not be within
1048 a string or comment.
1049
1050 The variable `c-maybe-labelp' is set to the position of the first `:' that
1051 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1052 single `?' is found, then `c-maybe-labelp' is cleared.
1053
1054 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1055 regarded as having a \"virtual semicolon\" immediately after the last token on
1056 the line. If this virtual semicolon is _at_ from, the function recognises it.
1057
1058 Note that this function might do hidden buffer changes. See the
1059 comment at the start of cc-engine.el for more info."
1060 (let ((skip-chars c-stmt-delim-chars)
1061 lit-range)
1062 (save-excursion
1063 (catch 'done
1064 (goto-char from)
1065 (while (progn (skip-chars-forward skip-chars to)
1066 (< (point) to))
1067 (cond
1068 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1069 (goto-char (cdr lit-range)))
1070 ((eq (char-after) ?:)
1071 (forward-char)
1072 (if (and (eq (char-after) ?:)
1073 (< (point) to))
1074 ;; Ignore scope operators.
1075 (forward-char)
1076 (setq c-maybe-labelp (1- (point)))))
1077 ((eq (char-after) ??)
1078 ;; A question mark. Can't be a label, so stop
1079 ;; looking for more : and ?.
1080 (setq c-maybe-labelp nil
1081 skip-chars (substring c-stmt-delim-chars 0 -2)))
1082 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1083 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1084 (backward-char))
1085 (skip-chars-backward " \t" from)
1086 (if (c-at-vsemi-p)
1087 (throw 'done (point))
1088 (forward-line)))
1089 (t (throw 'done (point)))))
1090 ;; In trailing space after an as yet undetected virtual semicolon?
1091 (c-backward-syntactic-ws from)
1092 (if (and (< (point) to)
1093 (c-at-vsemi-p))
1094 (point)
1095 nil)))))
1096
1097 (defun c-at-statement-start-p ()
1098 "Return non-nil if the point is at the first token in a statement
1099 or somewhere in the syntactic whitespace before it.
1100
1101 A \"statement\" here is not restricted to those inside code blocks.
1102 Any kind of declaration-like construct that occur outside function
1103 bodies is also considered a \"statement\".
1104
1105 Note that this function might do hidden buffer changes. See the
1106 comment at the start of cc-engine.el for more info."
1107
1108 (save-excursion
1109 (let ((end (point))
1110 c-maybe-labelp)
1111 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1112 (or (bobp)
1113 (eq (char-before) ?})
1114 (and (eq (char-before) ?{)
1115 (not (and c-special-brace-lists
1116 (progn (backward-char)
1117 (c-looking-at-special-brace-list)))))
1118 (c-crosses-statement-barrier-p (point) end)))))
1119
1120 (defun c-at-expression-start-p ()
1121 "Return non-nil if the point is at the first token in an expression or
1122 statement, or somewhere in the syntactic whitespace before it.
1123
1124 An \"expression\" here is a bit different from the normal language
1125 grammar sense: It's any sequence of expression tokens except commas,
1126 unless they are enclosed inside parentheses of some kind. Also, an
1127 expression never continues past an enclosing parenthesis, but it might
1128 contain parenthesis pairs of any sort except braces.
1129
1130 Since expressions never cross statement boundaries, this function also
1131 recognizes statement beginnings, just like `c-at-statement-start-p'.
1132
1133 Note that this function might do hidden buffer changes. See the
1134 comment at the start of cc-engine.el for more info."
1135
1136 (save-excursion
1137 (let ((end (point))
1138 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1139 c-maybe-labelp)
1140 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1141 (or (bobp)
1142 (memq (char-before) '(?{ ?}))
1143 (save-excursion (backward-char)
1144 (looking-at "\\s("))
1145 (c-crosses-statement-barrier-p (point) end)))))
1146
1147 \f
1148 ;; A set of functions that covers various idiosyncrasies in
1149 ;; implementations of `forward-comment'.
1150
1151 ;; Note: Some emacsen considers incorrectly that any line comment
1152 ;; ending with a backslash continues to the next line. I can't think
1153 ;; of any way to work around that in a reliable way without changing
1154 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1155 ;; changing the syntax for backslash doesn't work since we must treat
1156 ;; escapes in string literals correctly.)
1157
1158 (defun c-forward-single-comment ()
1159 "Move forward past whitespace and the closest following comment, if any.
1160 Return t if a comment was found, nil otherwise. In either case, the
1161 point is moved past the following whitespace. Line continuations,
1162 i.e. a backslashes followed by line breaks, are treated as whitespace.
1163 The line breaks that end line comments are considered to be the
1164 comment enders, so the point will be put on the beginning of the next
1165 line if it moved past a line comment.
1166
1167 This function does not do any hidden buffer changes."
1168
1169 (let ((start (point)))
1170 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1171 (goto-char (match-end 0)))
1172
1173 (when (forward-comment 1)
1174 (if (eobp)
1175 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1176 ;; forwards at eob.
1177 nil
1178
1179 ;; Emacs includes the ending newline in a b-style (c++)
1180 ;; comment, but XEmacs doesn't. We depend on the Emacs
1181 ;; behavior (which also is symmetric).
1182 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1183 (condition-case nil (forward-char 1)))
1184
1185 t))))
1186
1187 (defsubst c-forward-comments ()
1188 "Move forward past all following whitespace and comments.
1189 Line continuations, i.e. a backslashes followed by line breaks, are
1190 treated as whitespace.
1191
1192 Note that this function might do hidden buffer changes. See the
1193 comment at the start of cc-engine.el for more info."
1194
1195 (while (or
1196 ;; If forward-comment in at least XEmacs 21 is given a large
1197 ;; positive value, it'll loop all the way through if it hits
1198 ;; eob.
1199 (and (forward-comment 5)
1200 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1201 ;; forwards at eob.
1202 (not (eobp)))
1203
1204 (when (looking-at "\\\\[\n\r]")
1205 (forward-char 2)
1206 t))))
1207
1208 (defun c-backward-single-comment ()
1209 "Move backward past whitespace and the closest preceding comment, if any.
1210 Return t if a comment was found, nil otherwise. In either case, the
1211 point is moved past the preceding whitespace. Line continuations,
1212 i.e. a backslashes followed by line breaks, are treated as whitespace.
1213 The line breaks that end line comments are considered to be the
1214 comment enders, so the point cannot be at the end of the same line to
1215 move over a line comment.
1216
1217 This function does not do any hidden buffer changes."
1218
1219 (let ((start (point)))
1220 ;; When we got newline terminated comments, forward-comment in all
1221 ;; supported emacsen so far will stop at eol of each line not
1222 ;; ending with a comment when moving backwards. This corrects for
1223 ;; that, and at the same time handles line continuations.
1224 (while (progn
1225 (skip-chars-backward " \t\n\r\f\v")
1226 (and (looking-at "[\n\r]")
1227 (eq (char-before) ?\\)))
1228 (backward-char))
1229
1230 (if (bobp)
1231 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1232 ;; backwards at bob.
1233 nil
1234
1235 ;; Leave point after the closest following newline if we've
1236 ;; backed up over any above, since forward-comment won't move
1237 ;; backward over a line comment if point is at the end of the
1238 ;; same line.
1239 (re-search-forward "\\=\\s *[\n\r]" start t)
1240
1241 (if (if (forward-comment -1)
1242 (if (eolp)
1243 ;; If forward-comment above succeeded and we're at eol
1244 ;; then the newline we moved over above didn't end a
1245 ;; line comment, so we give it another go.
1246 (forward-comment -1)
1247 t))
1248
1249 ;; Emacs <= 20 and XEmacs move back over the closer of a
1250 ;; block comment that lacks an opener.
1251 (if (looking-at "\\*/")
1252 (progn (forward-char 2) nil)
1253 t)))))
1254
1255 (defsubst c-backward-comments ()
1256 "Move backward past all preceding whitespace and comments.
1257 Line continuations, i.e. a backslashes followed by line breaks, are
1258 treated as whitespace. The line breaks that end line comments are
1259 considered to be the comment enders, so the point cannot be at the end
1260 of the same line to move over a line comment. Unlike
1261 c-backward-syntactic-ws, this function doesn't move back over
1262 preprocessor directives.
1263
1264 Note that this function might do hidden buffer changes. See the
1265 comment at the start of cc-engine.el for more info."
1266
1267 (let ((start (point)))
1268 (while (and
1269 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1270 ;; return t when moving backwards at bob.
1271 (not (bobp))
1272
1273 (if (forward-comment -1)
1274 (if (looking-at "\\*/")
1275 ;; Emacs <= 20 and XEmacs move back over the
1276 ;; closer of a block comment that lacks an opener.
1277 (progn (forward-char 2) nil)
1278 t)
1279
1280 ;; XEmacs treats line continuations as whitespace but
1281 ;; only in the backward direction, which seems a bit
1282 ;; odd. Anyway, this is necessary for Emacs.
1283 (when (and (looking-at "[\n\r]")
1284 (eq (char-before) ?\\)
1285 (< (point) start))
1286 (backward-char)
1287 t))))))
1288
1289 \f
1290 ;; Tools for skipping over syntactic whitespace.
1291
1292 ;; The following functions use text properties to cache searches over
1293 ;; large regions of syntactic whitespace. It works as follows:
1294 ;;
1295 ;; o If a syntactic whitespace region contains anything but simple
1296 ;; whitespace (i.e. space, tab and line breaks), the text property
1297 ;; `c-in-sws' is put over it. At places where we have stopped
1298 ;; within that region there's also a `c-is-sws' text property.
1299 ;; That since there typically are nested whitespace inside that
1300 ;; must be handled separately, e.g. whitespace inside a comment or
1301 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1302 ;; to jump to another point with that property within the same
1303 ;; `c-in-sws' region. It can be likened to a ladder where
1304 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1305 ;;
1306 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1307 ;; a "rung position" and also maybe on the first following char.
1308 ;; As many characters as can be conveniently found in this range
1309 ;; are marked, but no assumption can be made that the whole range
1310 ;; is marked (it could be clobbered by later changes, for
1311 ;; instance).
1312 ;;
1313 ;; Note that some part of the beginning of a sequence of simple
1314 ;; whitespace might be part of the end of a preceding line comment
1315 ;; or cpp directive and must not be considered part of the "rung".
1316 ;; Such whitespace is some amount of horizontal whitespace followed
1317 ;; by a newline. In the case of cpp directives it could also be
1318 ;; two newlines with horizontal whitespace between them.
1319 ;;
1320 ;; The reason to include the first following char is to cope with
1321 ;; "rung positions" that doesn't have any ordinary whitespace. If
1322 ;; `c-is-sws' is put on a token character it does not have
1323 ;; `c-in-sws' set simultaneously. That's the only case when that
1324 ;; can occur, and the reason for not extending the `c-in-sws'
1325 ;; region to cover it is that the `c-in-sws' region could then be
1326 ;; accidentally merged with a following one if the token is only
1327 ;; one character long.
1328 ;;
1329 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1330 ;; removed in the changed region. If the change was inside
1331 ;; syntactic whitespace that means that the "ladder" is broken, but
1332 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1333 ;; parts on either side and use an ordinary search only to "repair"
1334 ;; the gap.
1335 ;;
1336 ;; Special care needs to be taken if a region is removed: If there
1337 ;; are `c-in-sws' on both sides of it which do not connect inside
1338 ;; the region then they can't be joined. If e.g. a marked macro is
1339 ;; broken, syntactic whitespace inside the new text might be
1340 ;; marked. If those marks would become connected with the old
1341 ;; `c-in-sws' range around the macro then we could get a ladder
1342 ;; with one end outside the macro and the other at some whitespace
1343 ;; within it.
1344 ;;
1345 ;; The main motivation for this system is to increase the speed in
1346 ;; skipping over the large whitespace regions that can occur at the
1347 ;; top level in e.g. header files that contain a lot of comments and
1348 ;; cpp directives. For small comments inside code it's probably
1349 ;; slower than using `forward-comment' straightforwardly, but speed is
1350 ;; not a significant factor there anyway.
1351
1352 ; (defface c-debug-is-sws-face
1353 ; '((t (:background "GreenYellow")))
1354 ; "Debug face to mark the `c-is-sws' property.")
1355 ; (defface c-debug-in-sws-face
1356 ; '((t (:underline t)))
1357 ; "Debug face to mark the `c-in-sws' property.")
1358
1359 ; (defun c-debug-put-sws-faces ()
1360 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1361 ; ;; properties in the buffer.
1362 ; (interactive)
1363 ; (save-excursion
1364 ; (c-save-buffer-state (in-face)
1365 ; (goto-char (point-min))
1366 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1367 ; (point)))
1368 ; (while (progn
1369 ; (goto-char (next-single-property-change
1370 ; (point) 'c-is-sws nil (point-max)))
1371 ; (if in-face
1372 ; (progn
1373 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1374 ; (setq in-face nil))
1375 ; (setq in-face (point)))
1376 ; (not (eobp))))
1377 ; (goto-char (point-min))
1378 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1379 ; (point)))
1380 ; (while (progn
1381 ; (goto-char (next-single-property-change
1382 ; (point) 'c-in-sws nil (point-max)))
1383 ; (if in-face
1384 ; (progn
1385 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1386 ; (setq in-face nil))
1387 ; (setq in-face (point)))
1388 ; (not (eobp)))))))
1389
1390 (defmacro c-debug-sws-msg (&rest args)
1391 ;;`(message ,@args)
1392 )
1393
1394 (defmacro c-put-is-sws (beg end)
1395 ;; This macro does a hidden buffer change.
1396 `(let ((beg ,beg) (end ,end))
1397 (put-text-property beg end 'c-is-sws t)
1398 ,@(when (facep 'c-debug-is-sws-face)
1399 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1400
1401 (defmacro c-put-in-sws (beg end)
1402 ;; This macro does a hidden buffer change.
1403 `(let ((beg ,beg) (end ,end))
1404 (put-text-property beg end 'c-in-sws t)
1405 ,@(when (facep 'c-debug-is-sws-face)
1406 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1407
1408 (defmacro c-remove-is-sws (beg end)
1409 ;; This macro does a hidden buffer change.
1410 `(let ((beg ,beg) (end ,end))
1411 (remove-text-properties beg end '(c-is-sws nil))
1412 ,@(when (facep 'c-debug-is-sws-face)
1413 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1414
1415 (defmacro c-remove-in-sws (beg end)
1416 ;; This macro does a hidden buffer change.
1417 `(let ((beg ,beg) (end ,end))
1418 (remove-text-properties beg end '(c-in-sws nil))
1419 ,@(when (facep 'c-debug-is-sws-face)
1420 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1421
1422 (defmacro c-remove-is-and-in-sws (beg end)
1423 ;; This macro does a hidden buffer change.
1424 `(let ((beg ,beg) (end ,end))
1425 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1426 ,@(when (facep 'c-debug-is-sws-face)
1427 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1428 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1429
1430 (defsubst c-invalidate-sws-region-after (beg end)
1431 ;; Called from `after-change-functions'. Note that if
1432 ;; `c-forward-sws' or `c-backward-sws' are used outside
1433 ;; `c-save-buffer-state' or similar then this will remove the cache
1434 ;; properties right after they're added.
1435 ;;
1436 ;; This function does hidden buffer changes.
1437
1438 (save-excursion
1439 ;; Adjust the end to remove the properties in any following simple
1440 ;; ws up to and including the next line break, if there is any
1441 ;; after the changed region. This is necessary e.g. when a rung
1442 ;; marked empty line is converted to a line comment by inserting
1443 ;; "//" before the line break. In that case the line break would
1444 ;; keep the rung mark which could make a later `c-backward-sws'
1445 ;; move into the line comment instead of over it.
1446 (goto-char end)
1447 (skip-chars-forward " \t\f\v")
1448 (when (and (eolp) (not (eobp)))
1449 (setq end (1+ (point)))))
1450
1451 (when (and (= beg end)
1452 (get-text-property beg 'c-in-sws)
1453 (> beg (point-min))
1454 (get-text-property (1- beg) 'c-in-sws))
1455 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1456 ;; safe to keep a range that was continuous before the change. E.g:
1457 ;;
1458 ;; #define foo
1459 ;; \
1460 ;; bar
1461 ;;
1462 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1463 ;; after "foo" is removed then "bar" will become part of the cpp
1464 ;; directive instead of a syntactically relevant token. In that
1465 ;; case there's no longer syntactic ws from "#" to "b".
1466 (setq beg (1- beg)))
1467
1468 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1469 (c-remove-is-and-in-sws beg end))
1470
1471 (defun c-forward-sws ()
1472 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1473 ;;
1474 ;; This function might do hidden buffer changes.
1475
1476 (let (;; `rung-pos' is set to a position as early as possible in the
1477 ;; unmarked part of the simple ws region.
1478 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1479 rung-is-marked next-rung-is-marked simple-ws-end
1480 ;; `safe-start' is set when it's safe to cache the start position.
1481 ;; It's not set if we've initially skipped over comments and line
1482 ;; continuations since we might have gone out through the end of a
1483 ;; macro then. This provision makes `c-forward-sws' not populate the
1484 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1485 ;; more common.
1486 safe-start)
1487
1488 ;; Skip simple ws and do a quick check on the following character to see
1489 ;; if it's anything that can't start syntactic ws, so we can bail out
1490 ;; early in the majority of cases when there just are a few ws chars.
1491 (skip-chars-forward " \t\n\r\f\v")
1492 (when (looking-at c-syntactic-ws-start)
1493
1494 (setq rung-end-pos (min (1+ (point)) (point-max)))
1495 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1496 'c-is-sws t))
1497 ;; Find the last rung position to avoid setting properties in all
1498 ;; the cases when the marked rung is complete.
1499 ;; (`next-single-property-change' is certain to move at least one
1500 ;; step forward.)
1501 (setq rung-pos (1- (next-single-property-change
1502 rung-is-marked 'c-is-sws nil rung-end-pos)))
1503 ;; Got no marked rung here. Since the simple ws might have started
1504 ;; inside a line comment or cpp directive we must set `rung-pos' as
1505 ;; high as possible.
1506 (setq rung-pos (point)))
1507
1508 (while
1509 (progn
1510 (while
1511 (when (and rung-is-marked
1512 (get-text-property (point) 'c-in-sws))
1513
1514 ;; The following search is the main reason that `c-in-sws'
1515 ;; and `c-is-sws' aren't combined to one property.
1516 (goto-char (next-single-property-change
1517 (point) 'c-in-sws nil (point-max)))
1518 (unless (get-text-property (point) 'c-is-sws)
1519 ;; If the `c-in-sws' region extended past the last
1520 ;; `c-is-sws' char we have to go back a bit.
1521 (or (get-text-property (1- (point)) 'c-is-sws)
1522 (goto-char (previous-single-property-change
1523 (point) 'c-is-sws)))
1524 (backward-char))
1525
1526 (c-debug-sws-msg
1527 "c-forward-sws cached move %s -> %s (max %s)"
1528 rung-pos (point) (point-max))
1529
1530 (setq rung-pos (point))
1531 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1532 (not (eobp))))
1533
1534 ;; We'll loop here if there is simple ws after the last rung.
1535 ;; That means that there's been some change in it and it's
1536 ;; possible that we've stepped into another ladder, so extend
1537 ;; the previous one to join with it if there is one, and try to
1538 ;; use the cache again.
1539 (c-debug-sws-msg
1540 "c-forward-sws extending rung with [%s..%s] (max %s)"
1541 (1+ rung-pos) (1+ (point)) (point-max))
1542 (unless (get-text-property (point) 'c-is-sws)
1543 ;; Remove any `c-in-sws' property from the last char of
1544 ;; the rung before we mark it with `c-is-sws', so that we
1545 ;; won't connect with the remains of a broken "ladder".
1546 (c-remove-in-sws (point) (1+ (point))))
1547 (c-put-is-sws (1+ rung-pos)
1548 (1+ (point)))
1549 (c-put-in-sws rung-pos
1550 (setq rung-pos (point)
1551 last-put-in-sws-pos rung-pos)))
1552
1553 (setq simple-ws-end (point))
1554 (c-forward-comments)
1555
1556 (cond
1557 ((/= (point) simple-ws-end)
1558 ;; Skipped over comments. Don't cache at eob in case the buffer
1559 ;; is narrowed.
1560 (not (eobp)))
1561
1562 ((save-excursion
1563 (and c-opt-cpp-prefix
1564 (looking-at c-opt-cpp-start)
1565 (progn (skip-chars-backward " \t")
1566 (bolp))
1567 (or (bobp)
1568 (progn (backward-char)
1569 (not (eq (char-before) ?\\))))))
1570 ;; Skip a preprocessor directive.
1571 (end-of-line)
1572 (while (and (eq (char-before) ?\\)
1573 (= (forward-line 1) 0))
1574 (end-of-line))
1575 (forward-line 1)
1576 (setq safe-start t)
1577 ;; Don't cache at eob in case the buffer is narrowed.
1578 (not (eobp)))))
1579
1580 ;; We've searched over a piece of non-white syntactic ws. See if this
1581 ;; can be cached.
1582 (setq next-rung-pos (point))
1583 (skip-chars-forward " \t\n\r\f\v")
1584 (setq rung-end-pos (min (1+ (point)) (point-max)))
1585
1586 (if (or
1587 ;; Cache if we haven't skipped comments only, and if we started
1588 ;; either from a marked rung or from a completely uncached
1589 ;; position.
1590 (and safe-start
1591 (or rung-is-marked
1592 (not (get-text-property simple-ws-end 'c-in-sws))))
1593
1594 ;; See if there's a marked rung in the encountered simple ws. If
1595 ;; so then we can cache, unless `safe-start' is nil. Even then
1596 ;; we need to do this to check if the cache can be used for the
1597 ;; next step.
1598 (and (setq next-rung-is-marked
1599 (text-property-any next-rung-pos rung-end-pos
1600 'c-is-sws t))
1601 safe-start))
1602
1603 (progn
1604 (c-debug-sws-msg
1605 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1606 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1607 (point-max))
1608
1609 ;; Remove the properties for any nested ws that might be cached.
1610 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1611 ;; anyway.
1612 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1613 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1614 (c-put-is-sws rung-pos
1615 (1+ simple-ws-end))
1616 (setq rung-is-marked t))
1617 (c-put-in-sws rung-pos
1618 (setq rung-pos (point)
1619 last-put-in-sws-pos rung-pos))
1620 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1621 ;; Remove any `c-in-sws' property from the last char of
1622 ;; the rung before we mark it with `c-is-sws', so that we
1623 ;; won't connect with the remains of a broken "ladder".
1624 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1625 (c-put-is-sws next-rung-pos
1626 rung-end-pos))
1627
1628 (c-debug-sws-msg
1629 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1630 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1631 (point-max))
1632
1633 ;; Set `rung-pos' for the next rung. It's the same thing here as
1634 ;; initially, except that the rung position is set as early as
1635 ;; possible since we can't be in the ending ws of a line comment or
1636 ;; cpp directive now.
1637 (if (setq rung-is-marked next-rung-is-marked)
1638 (setq rung-pos (1- (next-single-property-change
1639 rung-is-marked 'c-is-sws nil rung-end-pos)))
1640 (setq rung-pos next-rung-pos))
1641 (setq safe-start t)))
1642
1643 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1644 ;; another one after the point (which might occur when editing inside a
1645 ;; comment or macro).
1646 (when (eq last-put-in-sws-pos (point))
1647 (cond ((< last-put-in-sws-pos (point-max))
1648 (c-debug-sws-msg
1649 "c-forward-sws clearing at %s for cache separation"
1650 last-put-in-sws-pos)
1651 (c-remove-in-sws last-put-in-sws-pos
1652 (1+ last-put-in-sws-pos)))
1653 (t
1654 ;; If at eob we have to clear the last character before the end
1655 ;; instead since the buffer might be narrowed and there might
1656 ;; be a `c-in-sws' after (point-max). In this case it's
1657 ;; necessary to clear both properties.
1658 (c-debug-sws-msg
1659 "c-forward-sws clearing thoroughly at %s for cache separation"
1660 (1- last-put-in-sws-pos))
1661 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1662 last-put-in-sws-pos))))
1663 )))
1664
1665 (defun c-backward-sws ()
1666 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1667 ;;
1668 ;; This function might do hidden buffer changes.
1669
1670 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1671 ;; part of the simple ws region.
1672 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1673 rung-is-marked simple-ws-beg cmt-skip-pos)
1674
1675 ;; Skip simple horizontal ws and do a quick check on the preceding
1676 ;; character to see if it's anying that can't end syntactic ws, so we can
1677 ;; bail out early in the majority of cases when there just are a few ws
1678 ;; chars. Newlines are complicated in the backward direction, so we can't
1679 ;; skip over them.
1680 (skip-chars-backward " \t\f")
1681 (when (and (not (bobp))
1682 (save-excursion
1683 (backward-char)
1684 (looking-at c-syntactic-ws-end)))
1685
1686 ;; Try to find a rung position in the simple ws preceding point, so that
1687 ;; we can get a cache hit even if the last bit of the simple ws has
1688 ;; changed recently.
1689 (setq simple-ws-beg (point))
1690 (skip-chars-backward " \t\n\r\f\v")
1691 (if (setq rung-is-marked (text-property-any
1692 (point) (min (1+ rung-pos) (point-max))
1693 'c-is-sws t))
1694 ;; `rung-pos' will be the earliest marked position, which means that
1695 ;; there might be later unmarked parts in the simple ws region.
1696 ;; It's not worth the effort to fix that; the last part of the
1697 ;; simple ws is also typically edited often, so it could be wasted.
1698 (goto-char (setq rung-pos rung-is-marked))
1699 (goto-char simple-ws-beg))
1700
1701 (while
1702 (progn
1703 (while
1704 (when (and rung-is-marked
1705 (not (bobp))
1706 (get-text-property (1- (point)) 'c-in-sws))
1707
1708 ;; The following search is the main reason that `c-in-sws'
1709 ;; and `c-is-sws' aren't combined to one property.
1710 (goto-char (previous-single-property-change
1711 (point) 'c-in-sws nil (point-min)))
1712 (unless (get-text-property (point) 'c-is-sws)
1713 ;; If the `c-in-sws' region extended past the first
1714 ;; `c-is-sws' char we have to go forward a bit.
1715 (goto-char (next-single-property-change
1716 (point) 'c-is-sws)))
1717
1718 (c-debug-sws-msg
1719 "c-backward-sws cached move %s <- %s (min %s)"
1720 (point) rung-pos (point-min))
1721
1722 (setq rung-pos (point))
1723 (if (and (< (min (skip-chars-backward " \t\f\v")
1724 (progn
1725 (setq simple-ws-beg (point))
1726 (skip-chars-backward " \t\n\r\f\v")))
1727 0)
1728 (setq rung-is-marked
1729 (text-property-any (point) rung-pos
1730 'c-is-sws t)))
1731 t
1732 (goto-char simple-ws-beg)
1733 nil))
1734
1735 ;; We'll loop here if there is simple ws before the first rung.
1736 ;; That means that there's been some change in it and it's
1737 ;; possible that we've stepped into another ladder, so extend
1738 ;; the previous one to join with it if there is one, and try to
1739 ;; use the cache again.
1740 (c-debug-sws-msg
1741 "c-backward-sws extending rung with [%s..%s] (min %s)"
1742 rung-is-marked rung-pos (point-min))
1743 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1744 ;; Remove any `c-in-sws' property from the last char of
1745 ;; the rung before we mark it with `c-is-sws', so that we
1746 ;; won't connect with the remains of a broken "ladder".
1747 (c-remove-in-sws (1- rung-pos) rung-pos))
1748 (c-put-is-sws rung-is-marked
1749 rung-pos)
1750 (c-put-in-sws rung-is-marked
1751 (1- rung-pos))
1752 (setq rung-pos rung-is-marked
1753 last-put-in-sws-pos rung-pos))
1754
1755 (c-backward-comments)
1756 (setq cmt-skip-pos (point))
1757
1758 (cond
1759 ((and c-opt-cpp-prefix
1760 (/= cmt-skip-pos simple-ws-beg)
1761 (c-beginning-of-macro))
1762 ;; Inside a cpp directive. See if it should be skipped over.
1763 (let ((cpp-beg (point)))
1764
1765 ;; Move back over all line continuations in the region skipped
1766 ;; over by `c-backward-comments'. If we go past it then we
1767 ;; started inside the cpp directive.
1768 (goto-char simple-ws-beg)
1769 (beginning-of-line)
1770 (while (and (> (point) cmt-skip-pos)
1771 (progn (backward-char)
1772 (eq (char-before) ?\\)))
1773 (beginning-of-line))
1774
1775 (if (< (point) cmt-skip-pos)
1776 ;; Don't move past the cpp directive if we began inside
1777 ;; it. Note that the position at the end of the last line
1778 ;; of the macro is also considered to be within it.
1779 (progn (goto-char cmt-skip-pos)
1780 nil)
1781
1782 ;; It's worthwhile to spend a little bit of effort on finding
1783 ;; the end of the macro, to get a good `simple-ws-beg'
1784 ;; position for the cache. Note that `c-backward-comments'
1785 ;; could have stepped over some comments before going into
1786 ;; the macro, and then `simple-ws-beg' must be kept on the
1787 ;; same side of those comments.
1788 (goto-char simple-ws-beg)
1789 (skip-chars-backward " \t\n\r\f\v")
1790 (if (eq (char-before) ?\\)
1791 (forward-char))
1792 (forward-line 1)
1793 (if (< (point) simple-ws-beg)
1794 ;; Might happen if comments after the macro were skipped
1795 ;; over.
1796 (setq simple-ws-beg (point)))
1797
1798 (goto-char cpp-beg)
1799 t)))
1800
1801 ((/= (save-excursion
1802 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1803 (setq next-rung-pos (point)))
1804 simple-ws-beg)
1805 ;; Skipped over comments. Must put point at the end of
1806 ;; the simple ws at point since we might be after a line
1807 ;; comment or cpp directive that's been partially
1808 ;; narrowed out, and we can't risk marking the simple ws
1809 ;; at the end of it.
1810 (goto-char next-rung-pos)
1811 t)))
1812
1813 ;; We've searched over a piece of non-white syntactic ws. See if this
1814 ;; can be cached.
1815 (setq next-rung-pos (point))
1816 (skip-chars-backward " \t\f\v")
1817
1818 (if (or
1819 ;; Cache if we started either from a marked rung or from a
1820 ;; completely uncached position.
1821 rung-is-marked
1822 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1823
1824 ;; Cache if there's a marked rung in the encountered simple ws.
1825 (save-excursion
1826 (skip-chars-backward " \t\n\r\f\v")
1827 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1828 'c-is-sws t)))
1829
1830 (progn
1831 (c-debug-sws-msg
1832 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1833 (point) (1+ next-rung-pos)
1834 simple-ws-beg (min (1+ rung-pos) (point-max))
1835 (point-min))
1836
1837 ;; Remove the properties for any nested ws that might be cached.
1838 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1839 ;; anyway.
1840 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1841 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1842 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1843 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1844 ;; Remove any `c-in-sws' property from the last char of
1845 ;; the rung before we mark it with `c-is-sws', so that we
1846 ;; won't connect with the remains of a broken "ladder".
1847 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1848 (c-put-is-sws simple-ws-beg
1849 rung-end-pos)
1850 (setq rung-is-marked t)))
1851 (c-put-in-sws (setq simple-ws-beg (point)
1852 last-put-in-sws-pos simple-ws-beg)
1853 rung-pos)
1854 (c-put-is-sws (setq rung-pos simple-ws-beg)
1855 (1+ next-rung-pos)))
1856
1857 (c-debug-sws-msg
1858 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1859 (point) (1+ next-rung-pos)
1860 simple-ws-beg (min (1+ rung-pos) (point-max))
1861 (point-min))
1862 (setq rung-pos next-rung-pos
1863 simple-ws-beg (point))
1864 ))
1865
1866 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1867 ;; another one before the point (which might occur when editing inside a
1868 ;; comment or macro).
1869 (when (eq last-put-in-sws-pos (point))
1870 (cond ((< (point-min) last-put-in-sws-pos)
1871 (c-debug-sws-msg
1872 "c-backward-sws clearing at %s for cache separation"
1873 (1- last-put-in-sws-pos))
1874 (c-remove-in-sws (1- last-put-in-sws-pos)
1875 last-put-in-sws-pos))
1876 ((> (point-min) 1)
1877 ;; If at bob and the buffer is narrowed, we have to clear the
1878 ;; character we're standing on instead since there might be a
1879 ;; `c-in-sws' before (point-min). In this case it's necessary
1880 ;; to clear both properties.
1881 (c-debug-sws-msg
1882 "c-backward-sws clearing thoroughly at %s for cache separation"
1883 last-put-in-sws-pos)
1884 (c-remove-is-and-in-sws last-put-in-sws-pos
1885 (1+ last-put-in-sws-pos)))))
1886 )))
1887
1888 \f
1889 ;; A system for finding noteworthy parens before the point.
1890
1891 (defvar c-state-cache nil)
1892 (make-variable-buffer-local 'c-state-cache)
1893 ;; The state cache used by `c-parse-state' to cut down the amount of
1894 ;; searching. It's the result from some earlier `c-parse-state' call.
1895 ;;
1896 ;; The use of the cached info is more effective if the next
1897 ;; `c-parse-state' call is on a line close by the one the cached state
1898 ;; was made at; the cache can actually slow down a little if the
1899 ;; cached state was made very far back in the buffer. The cache is
1900 ;; most effective if `c-parse-state' is used on each line while moving
1901 ;; forward.
1902
1903 (defvar c-state-cache-start 1)
1904 (make-variable-buffer-local 'c-state-cache-start)
1905 ;; This is (point-min) when `c-state-cache' was calculated, since a
1906 ;; change of narrowing is likely to affect the parens that are visible
1907 ;; before the point.
1908
1909 (defvar c-state-cache-good-pos 1)
1910 (make-variable-buffer-local 'c-state-cache-good-pos)
1911 ;; This is a position where `c-state-cache' is known to be correct.
1912 ;; It's a position inside one of the recorded unclosed parens or the
1913 ;; top level, but not further nested inside any literal or subparen
1914 ;; that is closed before the last recorded position.
1915 ;;
1916 ;; The exact position is chosen to try to be close to yet earlier than
1917 ;; the position where `c-state-cache' will be called next. Right now
1918 ;; the heuristic is to set it to the position after the last found
1919 ;; closing paren (of any type) before the line on which
1920 ;; `c-parse-state' was called. That is chosen primarily to work well
1921 ;; with refontification of the current line.
1922
1923 (defsubst c-invalidate-state-cache (pos)
1924 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1925 ;; at POS or higher. This is much like `c-whack-state-after', but
1926 ;; it never changes a paren pair element into an open paren element.
1927 ;; Doing that would mean that the new open paren wouldn't have the
1928 ;; required preceding paren pair element.
1929 (while (and (or c-state-cache
1930 (when (< pos c-state-cache-good-pos)
1931 (setq c-state-cache-good-pos 1)
1932 nil))
1933 (let ((elem (car c-state-cache)))
1934 (if (consp elem)
1935 (or (< pos (cdr elem))
1936 (when (< pos c-state-cache-good-pos)
1937 (setq c-state-cache-good-pos (cdr elem))
1938 nil))
1939 (or (<= pos elem)
1940 (when (< pos c-state-cache-good-pos)
1941 (setq c-state-cache-good-pos (1+ elem))
1942 nil)))))
1943 (setq c-state-cache (cdr c-state-cache))))
1944
1945 (defun c-get-fallback-start-pos (here)
1946 ;; Return the start position for building `c-state-cache' from
1947 ;; scratch.
1948 (save-excursion
1949 ;; Go back 2 bods, but ignore any bogus positions returned by
1950 ;; beginning-of-defun (i.e. open paren in column zero).
1951 (goto-char here)
1952 (let ((cnt 2))
1953 (while (not (or (bobp) (zerop cnt)))
1954 (c-beginning-of-defun-1)
1955 (if (eq (char-after) ?\{)
1956 (setq cnt (1- cnt)))))
1957 (point)))
1958
1959 (defun c-parse-state ()
1960 ;; Find and record all noteworthy parens between some good point
1961 ;; earlier in the file and point. That good point is at least the
1962 ;; beginning of the top-level construct we are in, or the beginning
1963 ;; of the preceding top-level construct if we aren't in one.
1964 ;;
1965 ;; The returned value is a list of the noteworthy parens with the
1966 ;; last one first. If an element in the list is an integer, it's
1967 ;; the position of an open paren which has not been closed before
1968 ;; the point. If an element is a cons, it gives the position of a
1969 ;; closed brace paren pair; the car is the start paren position and
1970 ;; the cdr is the position following the closing paren. Only the
1971 ;; last closed brace paren pair before each open paren and before
1972 ;; the point is recorded, and thus the state never contains two cons
1973 ;; elements in succession.
1974 ;;
1975 ;; Currently no characters which are given paren syntax with the
1976 ;; syntax-table property are recorded, i.e. angle bracket arglist
1977 ;; parens are never present here. Note that this might change.
1978 ;;
1979 ;; BUG: This function doesn't cope entirely well with unbalanced
1980 ;; parens in macros. E.g. in the following case the brace before
1981 ;; the macro isn't balanced with the one after it:
1982 ;;
1983 ;; {
1984 ;; #define X {
1985 ;; }
1986 ;;
1987 ;; This function might do hidden buffer changes.
1988
1989 (save-restriction
1990 (let* ((here (point))
1991 (here-bol (c-point 'bol))
1992 (c-macro-start (c-query-macro-start))
1993 (in-macro-start (or c-macro-start (point)))
1994 old-state last-pos brace-pair-open brace-pair-close
1995 pos save-pos)
1996 (c-invalidate-state-cache here)
1997
1998 ;; If the minimum position has changed due to narrowing then we
1999 ;; have to fix the tail of `c-state-cache' accordingly.
2000 (unless (= c-state-cache-start (point-min))
2001 (if (> (point-min) c-state-cache-start)
2002 ;; If point-min has moved forward then we just need to cut
2003 ;; off a bit of the tail.
2004 (let ((ptr (cons nil c-state-cache)) elem)
2005 (while (and (setq elem (car-safe (cdr ptr)))
2006 (>= (if (consp elem) (car elem) elem)
2007 (point-min)))
2008 (setq ptr (cdr ptr)))
2009 (when (consp ptr)
2010 (if (eq (cdr ptr) c-state-cache)
2011 (setq c-state-cache nil
2012 c-state-cache-good-pos 1)
2013 (setcdr ptr nil))))
2014 ;; If point-min has moved backward then we drop the state
2015 ;; completely. It's possible to do a better job here and
2016 ;; recalculate the top only.
2017 (setq c-state-cache nil
2018 c-state-cache-good-pos 1))
2019 (setq c-state-cache-start (point-min)))
2020
2021 ;; Get the latest position we know are directly inside the
2022 ;; closest containing paren of the cached state.
2023 (setq last-pos (and c-state-cache
2024 (if (consp (car c-state-cache))
2025 (cdr (car c-state-cache))
2026 (1+ (car c-state-cache)))))
2027 (if (or (not last-pos)
2028 (< last-pos c-state-cache-good-pos))
2029 (setq last-pos c-state-cache-good-pos)
2030 ;; Take the opportunity to move the cached good position
2031 ;; further down.
2032 (if (< last-pos here-bol)
2033 (setq c-state-cache-good-pos last-pos)))
2034
2035 ;; Check if `last-pos' is in a macro. If it is, and we're not
2036 ;; in the same macro, we must discard everything on
2037 ;; `c-state-cache' that is inside the macro before using it.
2038 (save-excursion
2039 (goto-char last-pos)
2040 (when (and (c-beginning-of-macro)
2041 (/= (point) in-macro-start))
2042 (c-invalidate-state-cache (point))
2043 ;; Set `last-pos' again just like above except that there's
2044 ;; no use looking at `c-state-cache-good-pos' here.
2045 (setq last-pos (if c-state-cache
2046 (if (consp (car c-state-cache))
2047 (cdr (car c-state-cache))
2048 (1+ (car c-state-cache)))
2049 1))))
2050
2051 ;; If we've moved very far from the last cached position then
2052 ;; it's probably better to redo it from scratch, otherwise we
2053 ;; might spend a lot of time searching from `last-pos' down to
2054 ;; here.
2055 (when (< last-pos (- here 20000))
2056 ;; First get the fallback start position. If it turns out
2057 ;; that it's so far back that the cached state is closer then
2058 ;; we'll keep it afterall.
2059 (setq pos (c-get-fallback-start-pos here))
2060 (if (<= pos last-pos)
2061 (setq pos nil)
2062 (setq last-pos nil
2063 c-state-cache nil
2064 c-state-cache-good-pos 1)))
2065
2066 ;; Find the start position for the forward search. (Can't
2067 ;; search in the backward direction since the point might be in
2068 ;; some kind of literal.)
2069
2070 (unless pos
2071 (setq old-state c-state-cache)
2072
2073 ;; There's a cached state with a containing paren. Pop off
2074 ;; the stale containing sexps from it by going forward out of
2075 ;; parens as far as possible.
2076 (narrow-to-region (point-min) here)
2077 (let (placeholder pair-beg)
2078 (while (and c-state-cache
2079 (setq placeholder
2080 (c-up-list-forward last-pos)))
2081 (setq last-pos placeholder)
2082 (if (consp (car c-state-cache))
2083 (setq pair-beg (car-safe (cdr c-state-cache))
2084 c-state-cache (cdr-safe (cdr c-state-cache)))
2085 (setq pair-beg (car c-state-cache)
2086 c-state-cache (cdr c-state-cache))))
2087
2088 (when (and pair-beg (eq (char-after pair-beg) ?{))
2089 ;; The last paren pair we moved out from was a brace
2090 ;; pair. Modify the state to record this as a closed
2091 ;; pair now.
2092 (if (consp (car-safe c-state-cache))
2093 (setq c-state-cache (cdr c-state-cache)))
2094 (setq c-state-cache (cons (cons pair-beg last-pos)
2095 c-state-cache))))
2096
2097 ;; Check if the preceding balanced paren is within a
2098 ;; macro; it should be ignored if we're outside the
2099 ;; macro. There's no need to check any further upwards;
2100 ;; if the macro contains an unbalanced opening paren then
2101 ;; we're smoked anyway.
2102 (when (and (<= (point) in-macro-start)
2103 (consp (car c-state-cache)))
2104 (save-excursion
2105 (goto-char (car (car c-state-cache)))
2106 (when (c-beginning-of-macro)
2107 (setq here (point)
2108 c-state-cache (cdr c-state-cache)))))
2109
2110 (unless (eq c-state-cache old-state)
2111 ;; Have to adjust the cached good position if state has been
2112 ;; popped off.
2113 (setq c-state-cache-good-pos
2114 (if c-state-cache
2115 (if (consp (car c-state-cache))
2116 (cdr (car c-state-cache))
2117 (1+ (car c-state-cache)))
2118 1)
2119 old-state c-state-cache))
2120
2121 (when c-state-cache
2122 (setq pos last-pos)))
2123
2124 ;; Get the fallback start position.
2125 (unless pos
2126 (setq pos (c-get-fallback-start-pos here)
2127 c-state-cache nil
2128 c-state-cache-good-pos 1))
2129
2130 (narrow-to-region (point-min) here)
2131
2132 (while pos
2133 (setq save-pos pos
2134 brace-pair-open nil)
2135
2136 ;; Find the balanced brace pairs. This loop is hot, so it
2137 ;; does ugly tricks to go faster.
2138 (c-safe
2139 (let (set-good-pos set-brace-pair)
2140 (while t
2141 (setq last-pos nil
2142 last-pos (scan-lists pos 1 -1)) ; Might signal.
2143 (setq pos (scan-lists last-pos 1 1) ; Might signal.
2144 set-good-pos (< pos here-bol)
2145 set-brace-pair (eq (char-before last-pos) ?{))
2146
2147 ;; Update the cached good position and record the brace
2148 ;; pair, whichever is applicable for the paren we've
2149 ;; just jumped over. But first check that it isn't
2150 ;; inside a macro and the point isn't inside the same
2151 ;; one.
2152 (when (and (or set-good-pos set-brace-pair)
2153 (or (>= pos in-macro-start)
2154 (save-excursion
2155 (goto-char pos)
2156 (not (c-beginning-of-macro)))))
2157 (if set-good-pos
2158 (setq c-state-cache-good-pos pos))
2159 (if set-brace-pair
2160 (setq brace-pair-open last-pos
2161 brace-pair-close pos))))))
2162
2163 ;; Record the last brace pair.
2164 (when brace-pair-open
2165 (let ((head (car-safe c-state-cache)))
2166 (if (consp head)
2167 (progn
2168 (setcar head (1- brace-pair-open))
2169 (setcdr head brace-pair-close))
2170 (setq c-state-cache (cons (cons (1- brace-pair-open)
2171 brace-pair-close)
2172 c-state-cache)))))
2173
2174 (if last-pos
2175 ;; Prepare to loop, but record the open paren only if it's
2176 ;; outside a macro or within the same macro as point, and
2177 ;; if it is a legitimate open paren and not some character
2178 ;; that got an open paren syntax-table property.
2179 (progn
2180 (setq pos last-pos)
2181 (when (and (or (>= last-pos in-macro-start)
2182 (save-excursion
2183 (goto-char last-pos)
2184 (not (c-beginning-of-macro))))
2185 ;; Check for known types of parens that we
2186 ;; want to record. The syntax table is not to
2187 ;; be trusted here since the caller might be
2188 ;; using e.g. `c++-template-syntax-table'.
2189 (memq (char-before last-pos) '(?{ ?\( ?\[)))
2190 (if (< last-pos here-bol)
2191 (setq c-state-cache-good-pos last-pos))
2192 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
2193
2194 (if (setq last-pos (c-up-list-forward pos))
2195 ;; Found a close paren without a corresponding opening
2196 ;; one. Maybe we didn't go back far enough, so try to
2197 ;; scan backward for the start paren and then start over.
2198 (progn
2199 (setq pos (c-up-list-backward pos)
2200 c-state-cache nil
2201 c-state-cache-good-pos c-state-cache-start)
2202 (when (or (not pos)
2203 ;; Emacs (up to at least 21.2) can get confused by
2204 ;; open parens in column zero inside comments: The
2205 ;; sexp functions can then misbehave and bring us
2206 ;; back to the same point again. Check this so that
2207 ;; we don't get an infinite loop.
2208 (>= pos save-pos))
2209 (setq pos last-pos
2210 c-parsing-error
2211 (format "Unbalanced close paren at line %d"
2212 (1+ (count-lines (point-min)
2213 (c-point 'bol last-pos)))))))
2214 (setq pos nil))))
2215
2216 ;;(message "c-parse-state: %S end: %S" c-state-cache c-state-cache-good-pos)
2217 c-state-cache)))
2218
2219 ;; Debug tool to catch cache inconsistencies.
2220 (defvar c-debug-parse-state nil)
2221 (unless (fboundp 'c-real-parse-state)
2222 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
2223 (cc-bytecomp-defun c-real-parse-state)
2224 (defun c-debug-parse-state ()
2225 (let ((res1 (c-real-parse-state)) res2)
2226 (let ((c-state-cache nil)
2227 (c-state-cache-start 1)
2228 (c-state-cache-good-pos 1))
2229 (setq res2 (c-real-parse-state)))
2230 (unless (equal res1 res2)
2231 ;; The cache can actually go further back due to the ad-hoc way
2232 ;; the first paren is found, so try to whack off a bit of its
2233 ;; start before complaining.
2234 (save-excursion
2235 (goto-char (or (c-least-enclosing-brace res2) (point)))
2236 (c-beginning-of-defun-1)
2237 (while (not (or (bobp) (eq (char-after) ?{)))
2238 (c-beginning-of-defun-1))
2239 (unless (equal (c-whack-state-before (point) res1) res2)
2240 (message (concat "c-parse-state inconsistency: "
2241 "using cache: %s, from scratch: %s")
2242 res1 res2))))
2243 res1))
2244 (defun c-toggle-parse-state-debug (&optional arg)
2245 (interactive "P")
2246 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
2247 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
2248 'c-debug-parse-state
2249 'c-real-parse-state)))
2250 (c-keep-region-active))
2251 (when c-debug-parse-state
2252 (c-toggle-parse-state-debug 1))
2253
2254 (defun c-whack-state-before (bufpos paren-state)
2255 ;; Whack off any state information from PAREN-STATE which lies
2256 ;; before BUFPOS. Not destructive on PAREN-STATE.
2257 (let* ((newstate (list nil))
2258 (ptr newstate)
2259 car)
2260 (while paren-state
2261 (setq car (car paren-state)
2262 paren-state (cdr paren-state))
2263 (if (< (if (consp car) (car car) car) bufpos)
2264 (setq paren-state nil)
2265 (setcdr ptr (list car))
2266 (setq ptr (cdr ptr))))
2267 (cdr newstate)))
2268
2269 (defun c-whack-state-after (bufpos paren-state)
2270 ;; Whack off any state information from PAREN-STATE which lies at or
2271 ;; after BUFPOS. Not destructive on PAREN-STATE.
2272 (catch 'done
2273 (while paren-state
2274 (let ((car (car paren-state)))
2275 (if (consp car)
2276 ;; just check the car, because in a balanced brace
2277 ;; expression, it must be impossible for the corresponding
2278 ;; close brace to be before point, but the open brace to
2279 ;; be after.
2280 (if (<= bufpos (car car))
2281 nil ; whack it off
2282 (if (< bufpos (cdr car))
2283 ;; its possible that the open brace is before
2284 ;; bufpos, but the close brace is after. In that
2285 ;; case, convert this to a non-cons element. The
2286 ;; rest of the state is before bufpos, so we're
2287 ;; done.
2288 (throw 'done (cons (car car) (cdr paren-state)))
2289 ;; we know that both the open and close braces are
2290 ;; before bufpos, so we also know that everything else
2291 ;; on state is before bufpos.
2292 (throw 'done paren-state)))
2293 (if (<= bufpos car)
2294 nil ; whack it off
2295 ;; it's before bufpos, so everything else should too.
2296 (throw 'done paren-state)))
2297 (setq paren-state (cdr paren-state)))
2298 nil)))
2299
2300 (defun c-most-enclosing-brace (paren-state &optional bufpos)
2301 ;; Return the bufpos of the innermost enclosing open paren before
2302 ;; bufpos, or nil if none was found.
2303 (let (enclosingp)
2304 (or bufpos (setq bufpos 134217727))
2305 (while paren-state
2306 (setq enclosingp (car paren-state)
2307 paren-state (cdr paren-state))
2308 (if (or (consp enclosingp)
2309 (>= enclosingp bufpos))
2310 (setq enclosingp nil)
2311 (setq paren-state nil)))
2312 enclosingp))
2313
2314 (defun c-least-enclosing-brace (paren-state)
2315 ;; Return the bufpos of the outermost enclosing open paren, or nil
2316 ;; if none was found.
2317 (let (pos elem)
2318 (while paren-state
2319 (setq elem (car paren-state)
2320 paren-state (cdr paren-state))
2321 (if (integerp elem)
2322 (setq pos elem)))
2323 pos))
2324
2325 (defun c-safe-position (bufpos paren-state)
2326 ;; Return the closest "safe" position recorded on PAREN-STATE that
2327 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
2328 ;; contain any. Return nil if BUFPOS is nil, which is useful to
2329 ;; find the closest limit before a given limit that might be nil.
2330 ;;
2331 ;; A "safe" position is a position at or after a recorded open
2332 ;; paren, or after a recorded close paren. The returned position is
2333 ;; thus either the first position after a close brace, or the first
2334 ;; position after an enclosing paren, or at the enclosing paren in
2335 ;; case BUFPOS is immediately after it.
2336 (when bufpos
2337 (let (elem)
2338 (catch 'done
2339 (while paren-state
2340 (setq elem (car paren-state))
2341 (if (consp elem)
2342 (cond ((< (cdr elem) bufpos)
2343 (throw 'done (cdr elem)))
2344 ((< (car elem) bufpos)
2345 ;; See below.
2346 (throw 'done (min (1+ (car elem)) bufpos))))
2347 (if (< elem bufpos)
2348 ;; elem is the position at and not after the opening paren, so
2349 ;; we can go forward one more step unless it's equal to
2350 ;; bufpos. This is useful in some cases avoid an extra paren
2351 ;; level between the safe position and bufpos.
2352 (throw 'done (min (1+ elem) bufpos))))
2353 (setq paren-state (cdr paren-state)))))))
2354
2355 (defun c-beginning-of-syntax ()
2356 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2357 ;; goes to the closest previous point that is known to be outside
2358 ;; any string literal or comment. `c-state-cache' is used if it has
2359 ;; a position in the vicinity.
2360 (let* ((paren-state c-state-cache)
2361 elem
2362
2363 (pos (catch 'done
2364 ;; Note: Similar code in `c-safe-position'. The
2365 ;; difference is that we accept a safe position at
2366 ;; the point and don't bother to go forward past open
2367 ;; parens.
2368 (while paren-state
2369 (setq elem (car paren-state))
2370 (if (consp elem)
2371 (cond ((<= (cdr elem) (point))
2372 (throw 'done (cdr elem)))
2373 ((<= (car elem) (point))
2374 (throw 'done (car elem))))
2375 (if (<= elem (point))
2376 (throw 'done elem)))
2377 (setq paren-state (cdr paren-state)))
2378 (point-min))))
2379
2380 (if (> pos (- (point) 4000))
2381 (goto-char pos)
2382 ;; The position is far back. Try `c-beginning-of-defun-1'
2383 ;; (although we can't be entirely sure it will go to a position
2384 ;; outside a comment or string in current emacsen). FIXME:
2385 ;; Consult `syntax-ppss' here.
2386 (c-beginning-of-defun-1)
2387 (if (< (point) pos)
2388 (goto-char pos)))))
2389
2390 \f
2391 ;; Tools for scanning identifiers and other tokens.
2392
2393 (defun c-on-identifier ()
2394 "Return non-nil if the point is on or directly after an identifier.
2395 Keywords are recognized and not considered identifiers. If an
2396 identifier is detected, the returned value is its starting position.
2397 If an identifier ends at the point and another begins at it \(can only
2398 happen in Pike) then the point for the preceding one is returned.
2399
2400 Note that this function might do hidden buffer changes. See the
2401 comment at the start of cc-engine.el for more info."
2402
2403 ;; FIXME: Shouldn't this function handle "operator" in C++?
2404
2405 (save-excursion
2406 (skip-syntax-backward "w_")
2407
2408 (or
2409
2410 ;; Check for a normal (non-keyword) identifier.
2411 (and (looking-at c-symbol-start)
2412 (not (looking-at c-keywords-regexp))
2413 (point))
2414
2415 (when (c-major-mode-is 'pike-mode)
2416 ;; Handle the `<operator> syntax in Pike.
2417 (let ((pos (point)))
2418 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2419 (and (if (< (skip-chars-backward "`") 0)
2420 t
2421 (goto-char pos)
2422 (eq (char-after) ?\`))
2423 (looking-at c-symbol-key)
2424 (>= (match-end 0) pos)
2425 (point))))
2426
2427 ;; Handle the "operator +" syntax in C++.
2428 (when (and c-overloadable-operators-regexp
2429 (= (c-backward-token-2 0) 0))
2430
2431 (cond ((and (looking-at c-overloadable-operators-regexp)
2432 (or (not c-opt-op-identitier-prefix)
2433 (and (= (c-backward-token-2 1) 0)
2434 (looking-at c-opt-op-identitier-prefix))))
2435 (point))
2436
2437 ((save-excursion
2438 (and c-opt-op-identitier-prefix
2439 (looking-at c-opt-op-identitier-prefix)
2440 (= (c-forward-token-2 1) 0)
2441 (looking-at c-overloadable-operators-regexp)))
2442 (point))))
2443
2444 )))
2445
2446 (defsubst c-simple-skip-symbol-backward ()
2447 ;; If the point is at the end of a symbol then skip backward to the
2448 ;; beginning of it. Don't move otherwise. Return non-nil if point
2449 ;; moved.
2450 ;;
2451 ;; This function might do hidden buffer changes.
2452 (or (< (skip-syntax-backward "w_") 0)
2453 (and (c-major-mode-is 'pike-mode)
2454 ;; Handle the `<operator> syntax in Pike.
2455 (let ((pos (point)))
2456 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2457 (< (skip-chars-backward "`") 0)
2458 (looking-at c-symbol-key)
2459 (>= (match-end 0) pos))
2460 t
2461 (goto-char pos)
2462 nil)))))
2463
2464 (defun c-beginning-of-current-token (&optional back-limit)
2465 ;; Move to the beginning of the current token. Do not move if not
2466 ;; in the middle of one. BACK-LIMIT may be used to bound the
2467 ;; backward search; if given it's assumed to be at the boundary
2468 ;; between two tokens.
2469 ;;
2470 ;; This function might do hidden buffer changes.
2471 (if (looking-at "\\w\\|\\s_")
2472 (skip-syntax-backward "w_" back-limit)
2473 (let ((start (point)))
2474 (when (< (skip-syntax-backward ".()" back-limit) 0)
2475 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2476 (match-end 0))
2477 ;; `c-nonsymbol-token-regexp' should always match
2478 ;; since we've skipped backward over punctuator
2479 ;; or paren syntax, but consume one char in case
2480 ;; it doesn't so that we don't leave point before
2481 ;; some earlier incorrect token.
2482 (1+ (point)))))
2483 (if (<= pos start)
2484 (goto-char pos))
2485 (< pos start)))))))
2486
2487 (defun c-end-of-current-token (&optional back-limit)
2488 ;; Move to the end of the current token. Do not move if not in the
2489 ;; middle of one. BACK-LIMIT may be used to bound the backward
2490 ;; search; if given it's assumed to be at the boundary between two
2491 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2492 ;;
2493 ;; This function might do hidden buffer changes.
2494 (let ((start (point)))
2495 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2496 (skip-syntax-forward "w_"))
2497 ((< (skip-syntax-backward ".()" back-limit) 0)
2498 (while (progn
2499 (if (looking-at c-nonsymbol-token-regexp)
2500 (goto-char (match-end 0))
2501 ;; `c-nonsymbol-token-regexp' should always match since
2502 ;; we've skipped backward over punctuator or paren
2503 ;; syntax, but move forward in case it doesn't so that
2504 ;; we don't leave point earlier than we started with.
2505 (forward-char))
2506 (< (point) start)))))
2507 (> (point) start)))
2508
2509 (defconst c-jump-syntax-balanced
2510 (if (memq 'gen-string-delim c-emacs-features)
2511 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2512 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2513
2514 (defconst c-jump-syntax-unbalanced
2515 (if (memq 'gen-string-delim c-emacs-features)
2516 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2517 "\\w\\|\\s_\\|\\s\""))
2518
2519 (defun c-forward-token-2 (&optional count balanced limit)
2520 "Move forward by tokens.
2521 A token is defined as all symbols and identifiers which aren't
2522 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2523 treated properly). Point is always either left at the beginning of a
2524 token or not moved at all. COUNT specifies the number of tokens to
2525 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2526 moves to the next token beginning only if not already at one. If
2527 BALANCED is true, move over balanced parens, otherwise move into them.
2528 Also, if BALANCED is true, never move out of an enclosing paren.
2529
2530 LIMIT sets the limit for the movement and defaults to the point limit.
2531 The case when LIMIT is set in the middle of a token, comment or macro
2532 is handled correctly, i.e. the point won't be left there.
2533
2534 Return the number of tokens left to move \(positive or negative). If
2535 BALANCED is true, a move over a balanced paren counts as one. Note
2536 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2537 be returned. Thus, a return value of 0 guarantees that point is at
2538 the requested position and a return value less \(without signs) than
2539 COUNT guarantees that point is at the beginning of some token.
2540
2541 Note that this function might do hidden buffer changes. See the
2542 comment at the start of cc-engine.el for more info."
2543
2544 (or count (setq count 1))
2545 (if (< count 0)
2546 (- (c-backward-token-2 (- count) balanced limit))
2547
2548 (let ((jump-syntax (if balanced
2549 c-jump-syntax-balanced
2550 c-jump-syntax-unbalanced))
2551 (last (point))
2552 (prev (point)))
2553
2554 (if (zerop count)
2555 ;; If count is zero we should jump if in the middle of a token.
2556 (c-end-of-current-token))
2557
2558 (save-restriction
2559 (if limit (narrow-to-region (point-min) limit))
2560 (if (/= (point)
2561 (progn (c-forward-syntactic-ws) (point)))
2562 ;; Skip whitespace. Count this as a move if we did in
2563 ;; fact move.
2564 (setq count (max (1- count) 0)))
2565
2566 (if (eobp)
2567 ;; Moved out of bounds. Make sure the returned count isn't zero.
2568 (progn
2569 (if (zerop count) (setq count 1))
2570 (goto-char last))
2571
2572 ;; Use `condition-case' to avoid having the limit tests
2573 ;; inside the loop.
2574 (condition-case nil
2575 (while (and
2576 (> count 0)
2577 (progn
2578 (setq last (point))
2579 (cond ((looking-at jump-syntax)
2580 (goto-char (scan-sexps (point) 1))
2581 t)
2582 ((looking-at c-nonsymbol-token-regexp)
2583 (goto-char (match-end 0))
2584 t)
2585 ;; `c-nonsymbol-token-regexp' above should always
2586 ;; match if there are correct tokens. Try to
2587 ;; widen to see if the limit was set in the
2588 ;; middle of one, else fall back to treating
2589 ;; the offending thing as a one character token.
2590 ((and limit
2591 (save-restriction
2592 (widen)
2593 (looking-at c-nonsymbol-token-regexp)))
2594 nil)
2595 (t
2596 (forward-char)
2597 t))))
2598 (c-forward-syntactic-ws)
2599 (setq prev last
2600 count (1- count)))
2601 (error (goto-char last)))
2602
2603 (when (eobp)
2604 (goto-char prev)
2605 (setq count (1+ count)))))
2606
2607 count)))
2608
2609 (defun c-backward-token-2 (&optional count balanced limit)
2610 "Move backward by tokens.
2611 See `c-forward-token-2' for details."
2612
2613 (or count (setq count 1))
2614 (if (< count 0)
2615 (- (c-forward-token-2 (- count) balanced limit))
2616
2617 (or limit (setq limit (point-min)))
2618 (let ((jump-syntax (if balanced
2619 c-jump-syntax-balanced
2620 c-jump-syntax-unbalanced))
2621 (last (point)))
2622
2623 (if (zerop count)
2624 ;; The count is zero so try to skip to the beginning of the
2625 ;; current token.
2626 (if (> (point)
2627 (progn (c-beginning-of-current-token) (point)))
2628 (if (< (point) limit)
2629 ;; The limit is inside the same token, so return 1.
2630 (setq count 1))
2631
2632 ;; We're not in the middle of a token. If there's
2633 ;; whitespace after the point then we must move backward,
2634 ;; so set count to 1 in that case.
2635 (and (looking-at c-syntactic-ws-start)
2636 ;; If we're looking at a '#' that might start a cpp
2637 ;; directive then we have to do a more elaborate check.
2638 (or (/= (char-after) ?#)
2639 (not c-opt-cpp-prefix)
2640 (save-excursion
2641 (and (= (point)
2642 (progn (beginning-of-line)
2643 (looking-at "[ \t]*")
2644 (match-end 0)))
2645 (or (bobp)
2646 (progn (backward-char)
2647 (not (eq (char-before) ?\\)))))))
2648 (setq count 1))))
2649
2650 ;; Use `condition-case' to avoid having to check for buffer
2651 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2652 (condition-case nil
2653 (while (and
2654 (> count 0)
2655 (progn
2656 (c-backward-syntactic-ws)
2657 (backward-char)
2658 (if (looking-at jump-syntax)
2659 (goto-char (scan-sexps (1+ (point)) -1))
2660 ;; This can be very inefficient if there's a long
2661 ;; sequence of operator tokens without any separation.
2662 ;; That doesn't happen in practice, anyway.
2663 (c-beginning-of-current-token))
2664 (>= (point) limit)))
2665 (setq last (point)
2666 count (1- count)))
2667 (error (goto-char last)))
2668
2669 (if (< (point) limit)
2670 (goto-char last))
2671
2672 count)))
2673
2674 (defun c-forward-token-1 (&optional count balanced limit)
2675 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2676 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2677 characters are jumped over character by character. This function is
2678 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2679 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2680 (c-forward-token-2 count balanced limit)))
2681
2682 (defun c-backward-token-1 (&optional count balanced limit)
2683 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2684 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2685 characters are jumped over character by character. This function is
2686 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2687 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2688 (c-backward-token-2 count balanced limit)))
2689
2690 \f
2691 ;; Tools for doing searches restricted to syntactically relevant text.
2692
2693 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2694 paren-level not-inside-token
2695 lookbehind-submatch)
2696 "Like `re-search-forward', but only report matches that are found
2697 in syntactically significant text. I.e. matches in comments, macros
2698 or string literals are ignored. The start point is assumed to be
2699 outside any comment, macro or string literal, or else the content of
2700 that region is taken as syntactically significant text.
2701
2702 If PAREN-LEVEL is non-nil, an additional restriction is added to
2703 ignore matches in nested paren sexps. The search will also not go
2704 outside the current list sexp, which has the effect that if the point
2705 should be moved to BOUND when no match is found \(i.e. NOERROR is
2706 neither nil nor t), then it will be at the closing paren if the end of
2707 the current list sexp is encountered first.
2708
2709 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2710 ignored. Things like multicharacter operators and special symbols
2711 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2712 constants.
2713
2714 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2715 subexpression in REGEXP. The end of that submatch is used as the
2716 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2717 isn't used or if that subexpression didn't match then the start
2718 position of the whole match is used instead. The \"look behind\"
2719 subexpression is never tested before the starting position, so it
2720 might be a good idea to include \\=\\= as a match alternative in it.
2721
2722 Optimization note: Matches might be missed if the \"look behind\"
2723 subexpression can match the end of nonwhite syntactic whitespace,
2724 i.e. the end of comments or cpp directives. This since the function
2725 skips over such things before resuming the search. It's on the other
2726 hand not safe to assume that the \"look behind\" subexpression never
2727 matches syntactic whitespace.
2728
2729 Bug: Unbalanced parens inside cpp directives are currently not handled
2730 correctly \(i.e. they don't get ignored as they should) when
2731 PAREN-LEVEL is set.
2732
2733 Note that this function might do hidden buffer changes. See the
2734 comment at the start of cc-engine.el for more info."
2735
2736 (or bound (setq bound (point-max)))
2737 (if paren-level (setq paren-level -1))
2738
2739 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2740
2741 (let ((start (point))
2742 tmp
2743 ;; Start position for the last search.
2744 search-pos
2745 ;; The `parse-partial-sexp' state between the start position
2746 ;; and the point.
2747 state
2748 ;; The current position after the last state update. The next
2749 ;; `parse-partial-sexp' continues from here.
2750 (state-pos (point))
2751 ;; The position at which to check the state and the state
2752 ;; there. This is separate from `state-pos' since we might
2753 ;; need to back up before doing the next search round.
2754 check-pos check-state
2755 ;; Last position known to end a token.
2756 (last-token-end-pos (point-min))
2757 ;; Set when a valid match is found.
2758 found)
2759
2760 (condition-case err
2761 (while
2762 (and
2763 (progn
2764 (setq search-pos (point))
2765 (re-search-forward regexp bound noerror))
2766
2767 (progn
2768 (setq state (parse-partial-sexp
2769 state-pos (match-beginning 0) paren-level nil state)
2770 state-pos (point))
2771 (if (setq check-pos (and lookbehind-submatch
2772 (or (not paren-level)
2773 (>= (car state) 0))
2774 (match-end lookbehind-submatch)))
2775 (setq check-state (parse-partial-sexp
2776 state-pos check-pos paren-level nil state))
2777 (setq check-pos state-pos
2778 check-state state))
2779
2780 ;; NOTE: If we got a look behind subexpression and get
2781 ;; an insignificant match in something that isn't
2782 ;; syntactic whitespace (i.e. strings or in nested
2783 ;; parentheses), then we can never skip more than a
2784 ;; single character from the match start position
2785 ;; (i.e. `state-pos' here) before continuing the
2786 ;; search. That since the look behind subexpression
2787 ;; might match the end of the insignificant region in
2788 ;; the next search.
2789
2790 (cond
2791 ((elt check-state 7)
2792 ;; Match inside a line comment. Skip to eol. Use
2793 ;; `re-search-forward' instead of `skip-chars-forward' to get
2794 ;; the right bound behavior.
2795 (re-search-forward "[\n\r]" bound noerror))
2796
2797 ((elt check-state 4)
2798 ;; Match inside a block comment. Skip to the '*/'.
2799 (search-forward "*/" bound noerror))
2800
2801 ((and (not (elt check-state 5))
2802 (eq (char-before check-pos) ?/)
2803 (not (c-get-char-property (1- check-pos) 'syntax-table))
2804 (memq (char-after check-pos) '(?/ ?*)))
2805 ;; Match in the middle of the opener of a block or line
2806 ;; comment.
2807 (if (= (char-after check-pos) ?/)
2808 (re-search-forward "[\n\r]" bound noerror)
2809 (search-forward "*/" bound noerror)))
2810
2811 ;; The last `parse-partial-sexp' above might have
2812 ;; stopped short of the real check position if the end
2813 ;; of the current sexp was encountered in paren-level
2814 ;; mode. The checks above are always false in that
2815 ;; case, and since they can do better skipping in
2816 ;; lookbehind-submatch mode, we do them before
2817 ;; checking the paren level.
2818
2819 ((and paren-level
2820 (/= (setq tmp (car check-state)) 0))
2821 ;; Check the paren level first since we're short of the
2822 ;; syntactic checking position if the end of the
2823 ;; current sexp was encountered by `parse-partial-sexp'.
2824 (if (> tmp 0)
2825
2826 ;; Inside a nested paren sexp.
2827 (if lookbehind-submatch
2828 ;; See the NOTE above.
2829 (progn (goto-char state-pos) t)
2830 ;; Skip out of the paren quickly.
2831 (setq state (parse-partial-sexp state-pos bound 0 nil state)
2832 state-pos (point)))
2833
2834 ;; Have exited the current paren sexp.
2835 (if noerror
2836 (progn
2837 ;; The last `parse-partial-sexp' call above
2838 ;; has left us just after the closing paren
2839 ;; in this case, so we can modify the bound
2840 ;; to leave the point at the right position
2841 ;; upon return.
2842 (setq bound (1- (point)))
2843 nil)
2844 (signal 'search-failed (list regexp)))))
2845
2846 ((setq tmp (elt check-state 3))
2847 ;; Match inside a string.
2848 (if (or lookbehind-submatch
2849 (not (integerp tmp)))
2850 ;; See the NOTE above.
2851 (progn (goto-char state-pos) t)
2852 ;; Skip to the end of the string before continuing.
2853 (let ((ender (make-string 1 tmp)) (continue t))
2854 (while (if (search-forward ender bound noerror)
2855 (progn
2856 (setq state (parse-partial-sexp
2857 state-pos (point) nil nil state)
2858 state-pos (point))
2859 (elt state 3))
2860 (setq continue nil)))
2861 continue)))
2862
2863 ((save-excursion
2864 (save-match-data
2865 (c-beginning-of-macro start)))
2866 ;; Match inside a macro. Skip to the end of it.
2867 (c-end-of-macro)
2868 (cond ((<= (point) bound) t)
2869 (noerror nil)
2870 (t (signal 'search-failed (list regexp)))))
2871
2872 ((and not-inside-token
2873 (or (< check-pos last-token-end-pos)
2874 (< check-pos
2875 (save-excursion
2876 (goto-char check-pos)
2877 (save-match-data
2878 (c-end-of-current-token last-token-end-pos))
2879 (setq last-token-end-pos (point))))))
2880 ;; Inside a token.
2881 (if lookbehind-submatch
2882 ;; See the NOTE above.
2883 (goto-char state-pos)
2884 (goto-char (min last-token-end-pos bound))))
2885
2886 (t
2887 ;; A real match.
2888 (setq found t)
2889 nil)))
2890
2891 ;; Should loop to search again, but take care to avoid
2892 ;; looping on the same spot.
2893 (or (/= search-pos (point))
2894 (if (= (point) bound)
2895 (if noerror
2896 nil
2897 (signal 'search-failed (list regexp)))
2898 (forward-char)
2899 t))))
2900
2901 (error
2902 (goto-char start)
2903 (signal (car err) (cdr err))))
2904
2905 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2906
2907 (if found
2908 (progn
2909 (goto-char (match-end 0))
2910 (match-end 0))
2911
2912 ;; Search failed. Set point as appropriate.
2913 (if (eq noerror t)
2914 (goto-char start)
2915 (goto-char bound))
2916 nil)))
2917
2918 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
2919 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2920 i.e. don't stop at positions inside syntactic whitespace or string
2921 literals. Preprocessor directives are also ignored, with the exception
2922 of the one that the point starts within, if any. If LIMIT is given,
2923 it's assumed to be at a syntactically relevant position.
2924
2925 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
2926 sexps, and the search will also not go outside the current paren sexp.
2927 However, if LIMIT or the buffer limit is reached inside a nested paren
2928 then the point will be left at the limit.
2929
2930 Non-nil is returned if the point moved, nil otherwise.
2931
2932 Note that this function might do hidden buffer changes. See the
2933 comment at the start of cc-engine.el for more info."
2934
2935 (let ((start (point))
2936 state
2937 ;; A list of syntactically relevant positions in descending
2938 ;; order. It's used to avoid scanning repeatedly over
2939 ;; potentially large regions with `parse-partial-sexp' to verify
2940 ;; each position.
2941 safe-pos-list
2942 ;; The position at the beginning of `safe-pos-list'.
2943 safe-pos
2944 ;; The result from `c-beginning-of-macro' at the start position or the
2945 ;; start position itself if it isn't within a macro. Evaluated on
2946 ;; demand.
2947 start-macro-beg
2948 ;; The earliest position after the current one with the same paren
2949 ;; level. Used only when `paren-level' is set.
2950 (paren-level-pos (point)))
2951
2952 (while (progn
2953 (while (and
2954 (< (skip-chars-backward skip-chars limit) 0)
2955
2956 ;; Use `parse-partial-sexp' from a safe position down to
2957 ;; the point to check if it's outside comments and
2958 ;; strings.
2959 (let ((pos (point)) state-2 pps-end-pos)
2960 ;; Pick a safe position as close to the point as
2961 ;; possible.
2962 ;;
2963 ;; FIXME: Consult `syntax-ppss' here if our
2964 ;; cache doesn't give a good position.
2965 (while (and safe-pos-list
2966 (> (car safe-pos-list) (point)))
2967 (setq safe-pos-list (cdr safe-pos-list)))
2968 (unless (setq safe-pos (car-safe safe-pos-list))
2969 (setq safe-pos (max (or (c-safe-position
2970 (point) (or c-state-cache
2971 (c-parse-state)))
2972 0)
2973 (point-min))
2974 safe-pos-list (list safe-pos)))
2975
2976 ;; Cache positions along the way to use if we have to
2977 ;; back up more. We cache every closing paren on the
2978 ;; same level. If the paren cache is relevant in this
2979 ;; region then we're typically already on the same
2980 ;; level as the target position. Note that we might
2981 ;; cache positions after opening parens in case
2982 ;; safe-pos is in a nested list. That's both uncommon
2983 ;; and harmless.
2984 (while (progn
2985 (setq state (parse-partial-sexp
2986 safe-pos pos 0))
2987 (< (point) pos))
2988 (setq safe-pos (point)
2989 safe-pos-list (cons safe-pos safe-pos-list)))
2990
2991 (cond
2992 ((or (elt state 3) (elt state 4))
2993 ;; Inside string or comment. Continue search at the
2994 ;; beginning of it.
2995 (goto-char (elt state 8))
2996 t)
2997
2998 ((and paren-level
2999 (save-excursion
3000 (setq state-2 (parse-partial-sexp
3001 pos paren-level-pos -1)
3002 pps-end-pos (point))
3003 (/= (car state-2) 0)))
3004 ;; Not at the right level.
3005
3006 (if (and (< (car state-2) 0)
3007 ;; We stop above if we go out of a paren.
3008 ;; Now check whether it precedes or is
3009 ;; nested in the starting sexp.
3010 (save-excursion
3011 (setq state-2
3012 (parse-partial-sexp
3013 pps-end-pos paren-level-pos
3014 nil nil state-2))
3015 (< (car state-2) 0)))
3016
3017 ;; We've stopped short of the starting position
3018 ;; so the hit was inside a nested list. Go up
3019 ;; until we are at the right level.
3020 (condition-case nil
3021 (progn
3022 (goto-char (scan-lists pos -1
3023 (- (car state-2))))
3024 (setq paren-level-pos (point))
3025 (if (and limit (>= limit paren-level-pos))
3026 (progn
3027 (goto-char limit)
3028 nil)
3029 t))
3030 (error
3031 (goto-char (or limit (point-min)))
3032 nil))
3033
3034 ;; The hit was outside the list at the start
3035 ;; position. Go to the start of the list and exit.
3036 (goto-char (1+ (elt state-2 1)))
3037 nil))
3038
3039 ((c-beginning-of-macro limit)
3040 ;; Inside a macro.
3041 (if (< (point)
3042 (or start-macro-beg
3043 (setq start-macro-beg
3044 (save-excursion
3045 (goto-char start)
3046 (c-beginning-of-macro limit)
3047 (point)))))
3048 t
3049
3050 ;; It's inside the same macro we started in so it's
3051 ;; a relevant match.
3052 (goto-char pos)
3053 nil)))))
3054
3055 ;; If the state contains the start of the containing sexp we
3056 ;; cache that position too, so that parse-partial-sexp in the
3057 ;; next run has a bigger chance of starting at the same level
3058 ;; as the target position and thus will get more good safe
3059 ;; positions into the list.
3060 (if (elt state 1)
3061 (setq safe-pos (1+ (elt state 1))
3062 safe-pos-list (cons safe-pos safe-pos-list))))
3063
3064 (> (point)
3065 (progn
3066 ;; Skip syntactic ws afterwards so that we don't stop at the
3067 ;; end of a comment if `skip-chars' is something like "^/".
3068 (c-backward-syntactic-ws)
3069 (point)))))
3070
3071 ;; We might want to extend this with more useful return values in
3072 ;; the future.
3073 (/= (point) start)))
3074
3075 ;; The following is an alternative implementation of
3076 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3077 ;; track of the syntactic context. It turned out to be generally
3078 ;; slower than the one above which uses forward checks from earlier
3079 ;; safe positions.
3080 ;;
3081 ;;(defconst c-ssb-stop-re
3082 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3083 ;; ;; stop at to avoid going into comments and literals.
3084 ;; (concat
3085 ;; ;; Match comment end syntax and string literal syntax. Also match
3086 ;; ;; '/' for block comment endings (not covered by comment end
3087 ;; ;; syntax).
3088 ;; "\\s>\\|/\\|\\s\""
3089 ;; (if (memq 'gen-string-delim c-emacs-features)
3090 ;; "\\|\\s|"
3091 ;; "")
3092 ;; (if (memq 'gen-comment-delim c-emacs-features)
3093 ;; "\\|\\s!"
3094 ;; "")))
3095 ;;
3096 ;;(defconst c-ssb-stop-paren-re
3097 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3098 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3099 ;;
3100 ;;(defconst c-ssb-sexp-end-re
3101 ;; ;; Regexp matching the ending syntax of a complex sexp.
3102 ;; (concat c-string-limit-regexp "\\|\\s)"))
3103 ;;
3104 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3105 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3106 ;;i.e. don't stop at positions inside syntactic whitespace or string
3107 ;;literals. Preprocessor directives are also ignored. However, if the
3108 ;;point is within a comment, string literal or preprocessor directory to
3109 ;;begin with, its contents is treated as syntactically relevant chars.
3110 ;;If LIMIT is given, it limits the backward search and the point will be
3111 ;;left there if no earlier position is found.
3112 ;;
3113 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3114 ;;sexps, and the search will also not go outside the current paren sexp.
3115 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3116 ;;then the point will be left at the limit.
3117 ;;
3118 ;;Non-nil is returned if the point moved, nil otherwise.
3119 ;;
3120 ;;Note that this function might do hidden buffer changes. See the
3121 ;;comment at the start of cc-engine.el for more info."
3122 ;;
3123 ;; (save-restriction
3124 ;; (when limit
3125 ;; (narrow-to-region limit (point-max)))
3126 ;;
3127 ;; (let ((start (point)))
3128 ;; (catch 'done
3129 ;; (while (let ((last-pos (point))
3130 ;; (stop-pos (progn
3131 ;; (skip-chars-backward skip-chars)
3132 ;; (point))))
3133 ;;
3134 ;; ;; Skip back over the same region as
3135 ;; ;; `skip-chars-backward' above, but keep to
3136 ;; ;; syntactically relevant positions.
3137 ;; (goto-char last-pos)
3138 ;; (while (and
3139 ;; ;; `re-search-backward' with a single char regexp
3140 ;; ;; should be fast.
3141 ;; (re-search-backward
3142 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3143 ;; stop-pos 'move)
3144 ;;
3145 ;; (progn
3146 ;; (cond
3147 ;; ((looking-at "\\s(")
3148 ;; ;; `paren-level' is set and we've found the
3149 ;; ;; start of the containing paren.
3150 ;; (forward-char)
3151 ;; (throw 'done t))
3152 ;;
3153 ;; ((looking-at c-ssb-sexp-end-re)
3154 ;; ;; We're at the end of a string literal or paren
3155 ;; ;; sexp (if `paren-level' is set).
3156 ;; (forward-char)
3157 ;; (condition-case nil
3158 ;; (c-backward-sexp)
3159 ;; (error
3160 ;; (goto-char limit)
3161 ;; (throw 'done t))))
3162 ;;
3163 ;; (t
3164 ;; (forward-char)
3165 ;; ;; At the end of some syntactic ws or possibly
3166 ;; ;; after a plain '/' operator.
3167 ;; (let ((pos (point)))
3168 ;; (c-backward-syntactic-ws)
3169 ;; (if (= pos (point))
3170 ;; ;; Was a plain '/' operator. Go past it.
3171 ;; (backward-char)))))
3172 ;;
3173 ;; (> (point) stop-pos))))
3174 ;;
3175 ;; ;; Now the point is either at `stop-pos' or at some
3176 ;; ;; position further back if `stop-pos' was at a
3177 ;; ;; syntactically irrelevant place.
3178 ;;
3179 ;; ;; Skip additional syntactic ws so that we don't stop
3180 ;; ;; at the end of a comment if `skip-chars' is
3181 ;; ;; something like "^/".
3182 ;; (c-backward-syntactic-ws)
3183 ;;
3184 ;; (< (point) stop-pos))))
3185 ;;
3186 ;; ;; We might want to extend this with more useful return values
3187 ;; ;; in the future.
3188 ;; (/= (point) start))))
3189
3190 \f
3191 ;; Tools for handling comments and string literals.
3192
3193 (defun c-slow-in-literal (&optional lim detect-cpp)
3194 "Return the type of literal point is in, if any.
3195 The return value is `c' if in a C-style comment, `c++' if in a C++
3196 style comment, `string' if in a string literal, `pound' if DETECT-CPP
3197 is non-nil and in a preprocessor line, or nil if somewhere else.
3198 Optional LIM is used as the backward limit of the search. If omitted,
3199 or nil, `c-beginning-of-defun' is used.
3200
3201 The last point calculated is cached if the cache is enabled, i.e. if
3202 `c-in-literal-cache' is bound to a two element vector.
3203
3204 Note that this function might do hidden buffer changes. See the
3205 comment at the start of cc-engine.el for more info."
3206
3207 (if (and (vectorp c-in-literal-cache)
3208 (= (point) (aref c-in-literal-cache 0)))
3209 (aref c-in-literal-cache 1)
3210 (let ((rtn (save-excursion
3211 (let* ((pos (point))
3212 (lim (or lim (progn
3213 (c-beginning-of-syntax)
3214 (point))))
3215 (state (parse-partial-sexp lim pos)))
3216 (cond
3217 ((elt state 3) 'string)
3218 ((elt state 4) (if (elt state 7) 'c++ 'c))
3219 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
3220 (t nil))))))
3221 ;; cache this result if the cache is enabled
3222 (if (not c-in-literal-cache)
3223 (setq c-in-literal-cache (vector (point) rtn)))
3224 rtn)))
3225
3226 ;; XEmacs has a built-in function that should make this much quicker.
3227 ;; I don't think we even need the cache, which makes our lives more
3228 ;; complicated anyway. In this case, lim is only used to detect
3229 ;; cpp directives.
3230 ;;
3231 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
3232 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
3233 ;; Xemacs 21.4.4. It manifested itself thus:
3234 ;;
3235 ;; Starting with an empty AWK Mode buffer, type
3236 ;; /regexp/ {<C-j>
3237 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
3238 ;;
3239 ;; AWK Mode is designed such that when the first / is typed, it gets the
3240 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
3241 ;; are given the s-t property "string". However, buffer-syntactic-context
3242 ;; fails to take account of the change of the s-t property on the opening / to
3243 ;; "string", and reports that the { is within a string started by the second /.
3244 ;;
3245 ;; The workaround for this is for the AWK Mode initialisation to switch the
3246 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
3247 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
3248 ;;
3249 ;; (Alan Mackenzie, 2003/4/30).
3250
3251 (defun c-fast-in-literal (&optional lim detect-cpp)
3252 ;; This function might do hidden buffer changes.
3253 (let ((context (buffer-syntactic-context)))
3254 (cond
3255 ((eq context 'string) 'string)
3256 ((eq context 'comment) 'c++)
3257 ((eq context 'block-comment) 'c)
3258 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
3259
3260 (defalias 'c-in-literal
3261 (if (fboundp 'buffer-syntactic-context)
3262 'c-fast-in-literal ; XEmacs
3263 'c-slow-in-literal)) ; GNU Emacs
3264
3265 ;; The defalias above isn't enough to shut up the byte compiler.
3266 (cc-bytecomp-defun c-in-literal)
3267
3268 (defun c-literal-limits (&optional lim near not-in-delimiter)
3269 "Return a cons of the beginning and end positions of the comment or
3270 string surrounding point (including both delimiters), or nil if point
3271 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
3272 to start parsing from. If NEAR is non-nil, then the limits of any
3273 literal next to point is returned. \"Next to\" means there's only
3274 spaces and tabs between point and the literal. The search for such a
3275 literal is done first in forward direction. If NOT-IN-DELIMITER is
3276 non-nil, the case when point is inside a starting delimiter won't be
3277 recognized. This only has effect for comments, which have starting
3278 delimiters with more than one character.
3279
3280 Note that this function might do hidden buffer changes. See the
3281 comment at the start of cc-engine.el for more info."
3282
3283 (save-excursion
3284 (let* ((pos (point))
3285 (lim (or lim (progn
3286 (c-beginning-of-syntax)
3287 (point))))
3288 (state (parse-partial-sexp lim pos)))
3289
3290 (cond ((elt state 3) ; String.
3291 (goto-char (elt state 8))
3292 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3293 (point-max))))
3294
3295 ((elt state 4) ; Comment.
3296 (goto-char (elt state 8))
3297 (cons (point) (progn (c-forward-single-comment) (point))))
3298
3299 ((and (not not-in-delimiter)
3300 (not (elt state 5))
3301 (eq (char-before) ?/)
3302 (looking-at "[/*]"))
3303 ;; We're standing in a comment starter.
3304 (backward-char 1)
3305 (cons (point) (progn (c-forward-single-comment) (point))))
3306
3307 (near
3308 (goto-char pos)
3309
3310 ;; Search forward for a literal.
3311 (skip-chars-forward " \t")
3312
3313 (cond
3314 ((looking-at c-string-limit-regexp) ; String.
3315 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3316 (point-max))))
3317
3318 ((looking-at c-comment-start-regexp) ; Line or block comment.
3319 (cons (point) (progn (c-forward-single-comment) (point))))
3320
3321 (t
3322 ;; Search backward.
3323 (skip-chars-backward " \t")
3324
3325 (let ((end (point)) beg)
3326 (cond
3327 ((save-excursion
3328 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
3329 (setq beg (c-safe (c-backward-sexp 1) (point))))
3330
3331 ((and (c-safe (forward-char -2) t)
3332 (looking-at "*/"))
3333 ;; Block comment. Due to the nature of line
3334 ;; comments, they will always be covered by the
3335 ;; normal case above.
3336 (goto-char end)
3337 (c-backward-single-comment)
3338 ;; If LIM is bogus, beg will be bogus.
3339 (setq beg (point))))
3340
3341 (if beg (cons beg end))))))
3342 ))))
3343
3344 ;; In case external callers use this; it did have a docstring.
3345 (defalias 'c-literal-limits-fast 'c-literal-limits)
3346
3347 (defun c-collect-line-comments (range)
3348 "If the argument is a cons of two buffer positions (such as returned by
3349 `c-literal-limits'), and that range contains a C++ style line comment,
3350 then an extended range is returned that contains all adjacent line
3351 comments (i.e. all comments that starts in the same column with no
3352 empty lines or non-whitespace characters between them). Otherwise the
3353 argument is returned.
3354
3355 Note that this function might do hidden buffer changes. See the
3356 comment at the start of cc-engine.el for more info."
3357
3358 (save-excursion
3359 (condition-case nil
3360 (if (and (consp range) (progn
3361 (goto-char (car range))
3362 (looking-at c-line-comment-starter)))
3363 (let ((col (current-column))
3364 (beg (point))
3365 (bopl (c-point 'bopl))
3366 (end (cdr range)))
3367 ;; Got to take care in the backward direction to handle
3368 ;; comments which are preceded by code.
3369 (while (and (c-backward-single-comment)
3370 (>= (point) bopl)
3371 (looking-at c-line-comment-starter)
3372 (= col (current-column)))
3373 (setq beg (point)
3374 bopl (c-point 'bopl)))
3375 (goto-char end)
3376 (while (and (progn (skip-chars-forward " \t")
3377 (looking-at c-line-comment-starter))
3378 (= col (current-column))
3379 (prog1 (zerop (forward-line 1))
3380 (setq end (point)))))
3381 (cons beg end))
3382 range)
3383 (error range))))
3384
3385 (defun c-literal-type (range)
3386 "Convenience function that given the result of `c-literal-limits',
3387 returns nil or the type of literal that the range surrounds. It's
3388 much faster than using `c-in-literal' and is intended to be used when
3389 you need both the type of a literal and its limits.
3390
3391 Note that this function might do hidden buffer changes. See the
3392 comment at the start of cc-engine.el for more info."
3393
3394 (if (consp range)
3395 (save-excursion
3396 (goto-char (car range))
3397 (cond ((looking-at c-string-limit-regexp) 'string)
3398 ((or (looking-at "//") ; c++ line comment
3399 (and (looking-at "\\s<") ; comment starter
3400 (looking-at "#"))) ; awk comment.
3401 'c++)
3402 (t 'c))) ; Assuming the range is valid.
3403 range))
3404
3405 \f
3406 ;; `c-find-decl-spots' and accompanying stuff.
3407
3408 ;; Variables used in `c-find-decl-spots' to cache the search done for
3409 ;; the first declaration in the last call. When that function starts,
3410 ;; it needs to back up over syntactic whitespace to look at the last
3411 ;; token before the region being searched. That can sometimes cause
3412 ;; moves back and forth over a quite large region of comments and
3413 ;; macros, which would be repeated for each changed character when
3414 ;; we're called during fontification, since font-lock refontifies the
3415 ;; current line for each change. Thus it's worthwhile to cache the
3416 ;; first match.
3417 ;;
3418 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3419 ;; the syntactic whitespace less or equal to some start position.
3420 ;; There's no cached value if it's nil.
3421 ;;
3422 ;; `c-find-decl-match-pos' is the match position if
3423 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3424 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3425 (defvar c-find-decl-syntactic-pos nil)
3426 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
3427 (defvar c-find-decl-match-pos nil)
3428 (make-variable-buffer-local 'c-find-decl-match-pos)
3429
3430 (defsubst c-invalidate-find-decl-cache (change-min-pos)
3431 (and c-find-decl-syntactic-pos
3432 (< change-min-pos c-find-decl-syntactic-pos)
3433 (setq c-find-decl-syntactic-pos nil)))
3434
3435 ; (defface c-debug-decl-spot-face
3436 ; '((t (:background "Turquoise")))
3437 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3438 ; (defface c-debug-decl-sws-face
3439 ; '((t (:background "Khaki")))
3440 ; "Debug face to mark the syntactic whitespace between the declaration
3441 ; spots and the preceding token end.")
3442
3443 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
3444 (when (facep 'c-debug-decl-spot-face)
3445 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
3446 (c-debug-add-face (max match-pos (point-min)) decl-pos
3447 'c-debug-decl-sws-face)
3448 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
3449 'c-debug-decl-spot-face))))
3450 (defmacro c-debug-remove-decl-spot-faces (beg end)
3451 (when (facep 'c-debug-decl-spot-face)
3452 `(c-save-buffer-state ()
3453 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
3454 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
3455
3456 (defmacro c-find-decl-prefix-search ()
3457 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3458 ;; but it contains lots of free variables that refer to things
3459 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3460 ;; if there is a match, otherwise at `cfd-limit'.
3461 ;;
3462 ;; This macro might do hidden buffer changes.
3463
3464 '(progn
3465 ;; Find the next property match position if we haven't got one already.
3466 (unless cfd-prop-match
3467 (save-excursion
3468 (while (progn
3469 (goto-char (next-single-property-change
3470 (point) 'c-type nil cfd-limit))
3471 (and (< (point) cfd-limit)
3472 (not (eq (c-get-char-property (1- (point)) 'c-type)
3473 'c-decl-end)))))
3474 (setq cfd-prop-match (point))))
3475
3476 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
3477 ;; got one already.
3478 (unless cfd-re-match
3479
3480 (if (> cfd-re-match-end (point))
3481 (goto-char cfd-re-match-end))
3482
3483 (while (if (setq cfd-re-match-end
3484 (re-search-forward c-decl-prefix-or-start-re
3485 cfd-limit 'move))
3486
3487 ;; Match. Check if it's inside a comment or string literal.
3488 (c-got-face-at
3489 (if (setq cfd-re-match (match-end 1))
3490 ;; Matched the end of a token preceding a decl spot.
3491 (progn
3492 (goto-char cfd-re-match)
3493 (1- cfd-re-match))
3494 ;; Matched a token that start a decl spot.
3495 (goto-char (match-beginning 0))
3496 (point))
3497 c-literal-faces)
3498
3499 ;; No match. Finish up and exit the loop.
3500 (setq cfd-re-match cfd-limit)
3501 nil)
3502
3503 ;; Skip out of comments and string literals.
3504 (while (progn
3505 (goto-char (next-single-property-change
3506 (point) 'face nil cfd-limit))
3507 (and (< (point) cfd-limit)
3508 (c-got-face-at (point) c-literal-faces)))))
3509
3510 ;; If we matched at the decl start, we have to back up over the
3511 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
3512 ;; any decl spots in the syntactic ws.
3513 (unless cfd-re-match
3514 (c-backward-syntactic-ws)
3515 (setq cfd-re-match (point))))
3516
3517 ;; Choose whichever match is closer to the start.
3518 (if (< cfd-re-match cfd-prop-match)
3519 (setq cfd-match-pos cfd-re-match
3520 cfd-re-match nil)
3521 (setq cfd-match-pos cfd-prop-match
3522 cfd-prop-match nil))
3523
3524 (goto-char cfd-match-pos)
3525
3526 (when (< cfd-match-pos cfd-limit)
3527 ;; Skip forward past comments only so we don't skip macros.
3528 (c-forward-comments)
3529 ;; Set the position to continue at. We can avoid going over
3530 ;; the comments skipped above a second time, but it's possible
3531 ;; that the comment skipping has taken us past `cfd-prop-match'
3532 ;; since the property might be used inside comments.
3533 (setq cfd-continue-pos (if cfd-prop-match
3534 (min cfd-prop-match (point))
3535 (point))))))
3536
3537 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3538 ;; Call CFD-FUN for each possible spot for a declaration, cast or
3539 ;; label from the point to CFD-LIMIT. Such a spot is:
3540 ;;
3541 ;; o The first token after bob.
3542 ;; o The first token after the end of submatch 1 in
3543 ;; `c-decl-prefix-or-start-re' when that submatch matches.
3544 ;; o The start of each `c-decl-prefix-or-start-re' match when
3545 ;; submatch 1 doesn't match.
3546 ;; o The first token after the end of each occurence of the
3547 ;; `c-type' text property with the value `c-decl-end', provided
3548 ;; `c-type-decl-end-used' is set.
3549 ;;
3550 ;; Only a spot that match CFD-DECL-RE and whose face is in the
3551 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
3552 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
3553 ;;
3554 ;; If the match is inside a macro then the buffer is narrowed to the
3555 ;; end of it, so that CFD-FUN can investigate the following tokens
3556 ;; without matching something that begins inside a macro and ends
3557 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3558 ;; CFD-FACE-CHECKLIST checks exist.
3559 ;;
3560 ;; CFD-FUN is called with point at the start of the spot. It's
3561 ;; passed two arguments: The first is the end position of the token
3562 ;; preceding the spot, or 0 for the implicit match at bob. The
3563 ;; second is a flag that is t when the match is inside a macro. If
3564 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
3565 ;; spot, it should return non-nil to ensure that the next search
3566 ;; will find them.
3567 ;;
3568 ;; The spots are visited approximately in order from top to bottom.
3569 ;; It's however the positions where `c-decl-prefix-or-start-re'
3570 ;; matches and where `c-decl-end' properties are found that are in
3571 ;; order. Since the spots often are at the following token, they
3572 ;; might be visited out of order insofar as more spots are reported
3573 ;; later on within the syntactic whitespace between the match
3574 ;; positions and their spots.
3575 ;;
3576 ;; It's assumed that comments and strings are fontified in the
3577 ;; searched range.
3578 ;;
3579 ;; This is mainly used in fontification, and so has an elaborate
3580 ;; cache to handle repeated calls from the same start position; see
3581 ;; the variables above.
3582 ;;
3583 ;; All variables in this function begin with `cfd-' to avoid name
3584 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3585 ;;
3586 ;; This function might do hidden buffer changes.
3587
3588 (let ((cfd-start-pos (point))
3589 (cfd-buffer-end (point-max))
3590 ;; The end of the token preceding the decl spot last found
3591 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
3592 ;; no match.
3593 cfd-re-match
3594 ;; The end position of the last `c-decl-prefix-or-start-re'
3595 ;; match. If this is greater than `cfd-continue-pos', the
3596 ;; next regexp search is started here instead.
3597 (cfd-re-match-end (point-min))
3598 ;; The end of the last `c-decl-end' found by
3599 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
3600 ;; match. If searching for the property isn't needed then we
3601 ;; disable it by setting it to `cfd-limit' directly.
3602 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3603 ;; The end of the token preceding the decl spot last found by
3604 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
3605 ;; bob. `cfd-limit' if there's no match. In other words,
3606 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
3607 (cfd-match-pos cfd-limit)
3608 ;; The position to continue searching at.
3609 cfd-continue-pos
3610 ;; The position of the last "real" token we've stopped at.
3611 ;; This can be greater than `cfd-continue-pos' when we get
3612 ;; hits inside macros or at `c-decl-end' positions inside
3613 ;; comments.
3614 (cfd-token-pos 0)
3615 ;; The end position of the last entered macro.
3616 (cfd-macro-end 0))
3617
3618 ;; Initialize by finding a syntactically relevant start position
3619 ;; before the point, and do the first `c-decl-prefix-or-start-re'
3620 ;; search unless we're at bob.
3621
3622 (let (start-in-literal start-in-macro syntactic-pos)
3623 ;; Must back up a bit since we look for the end of the previous
3624 ;; statement or declaration, which is earlier than the first
3625 ;; returned match.
3626
3627 (cond
3628 ;; First we need to move to a syntactically relevant position.
3629 ;; Begin by backing out of comment or string literals.
3630 ((and
3631 (when (c-got-face-at (point) c-literal-faces)
3632 ;; Try to use the faces to back up to the start of the
3633 ;; literal. FIXME: What if the point is on a declaration
3634 ;; inside a comment?
3635 (while (and (not (bobp))
3636 (c-got-face-at (1- (point)) c-literal-faces))
3637 (goto-char (previous-single-property-change
3638 (point) 'face nil (point-min))))
3639
3640 ;; XEmacs doesn't fontify the quotes surrounding string
3641 ;; literals.
3642 (and (featurep 'xemacs)
3643 (eq (get-text-property (point) 'face)
3644 'font-lock-string-face)
3645 (not (bobp))
3646 (progn (backward-char)
3647 (not (looking-at c-string-limit-regexp)))
3648 (forward-char))
3649
3650 ;; Don't trust the literal to contain only literal faces
3651 ;; (the font lock package might not have fontified the
3652 ;; start of it at all, for instance) so check that we have
3653 ;; arrived at something that looks like a start or else
3654 ;; resort to `c-literal-limits'.
3655 (unless (looking-at c-literal-start-regexp)
3656 (let ((range (c-literal-limits)))
3657 (if range (goto-char (car range)))))
3658
3659 (setq start-in-literal (point)))
3660
3661 ;; The start is in a literal. If the limit is in the same
3662 ;; one we don't have to find a syntactic position etc. We
3663 ;; only check that if the limit is at or before bonl to save
3664 ;; time; it covers the by far most common case when font-lock
3665 ;; refontifies the current line only.
3666 (<= cfd-limit (c-point 'bonl cfd-start-pos))
3667 (save-excursion
3668 (goto-char cfd-start-pos)
3669 (while (progn
3670 (goto-char (next-single-property-change
3671 (point) 'face nil cfd-limit))
3672 (and (< (point) cfd-limit)
3673 (c-got-face-at (point) c-literal-faces))))
3674 (= (point) cfd-limit)))
3675
3676 ;; Completely inside a literal. Set up variables to trig the
3677 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
3678 ;; find a suitable start position.
3679 (setq cfd-continue-pos start-in-literal))
3680
3681 ;; Check if the region might be completely inside a macro, to
3682 ;; optimize that like the completely-inside-literal above.
3683 ((save-excursion
3684 (and (= (forward-line 1) 0)
3685 (bolp) ; forward-line has funny behavior at eob.
3686 (>= (point) cfd-limit)
3687 (progn (backward-char)
3688 (eq (char-before) ?\\))))
3689 ;; (Maybe) completely inside a macro. Only need to trig the
3690 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
3691 ;; set things up.
3692 (setq cfd-continue-pos (1- cfd-start-pos)
3693 start-in-macro t))
3694
3695 (t
3696 ;; Back out of any macro so we don't miss any declaration
3697 ;; that could follow after it.
3698 (when (c-beginning-of-macro)
3699 (setq start-in-macro t))
3700
3701 ;; Now we're at a proper syntactically relevant position so we
3702 ;; can use the cache. But first clear it if it applied
3703 ;; further down.
3704 (c-invalidate-find-decl-cache cfd-start-pos)
3705
3706 (setq syntactic-pos (point))
3707 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
3708 ;; Don't have to do this if the cache is relevant here,
3709 ;; typically if the same line is refontified again. If
3710 ;; we're just some syntactic whitespace further down we can
3711 ;; still use the cache to limit the skipping.
3712 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
3713
3714 ;; If we hit `c-find-decl-syntactic-pos' and
3715 ;; `c-find-decl-match-pos' is set then we install the cached
3716 ;; values. If we hit `c-find-decl-syntactic-pos' and
3717 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3718 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3719 ;; and so we can continue the search from this point. If we
3720 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
3721 ;; the right spot to begin searching anyway.
3722 (if (and (eq (point) c-find-decl-syntactic-pos)
3723 c-find-decl-match-pos)
3724 (setq cfd-match-pos c-find-decl-match-pos
3725 cfd-continue-pos syntactic-pos)
3726
3727 (setq c-find-decl-syntactic-pos syntactic-pos)
3728
3729 (when (if (bobp)
3730 ;; Always consider bob a match to get the first
3731 ;; declaration in the file. Do this separately instead of
3732 ;; letting `c-decl-prefix-or-start-re' match bob, so that
3733 ;; regexp always can consume at least one character to
3734 ;; ensure that we won't get stuck in an infinite loop.
3735 (setq cfd-re-match 0)
3736 (backward-char)
3737 (c-beginning-of-current-token)
3738 (< (point) cfd-limit))
3739 ;; Do an initial search now. In the bob case above it's
3740 ;; only done to search for a `c-decl-end' spot.
3741 (c-find-decl-prefix-search))
3742
3743 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
3744 cfd-match-pos)))))
3745
3746 ;; Advance `cfd-continue-pos' if it's before the start position.
3747 ;; The closest continue position that might have effect at or
3748 ;; after the start depends on what we started in. This also
3749 ;; finds a suitable start position in the special cases when the
3750 ;; region is completely within a literal or macro.
3751 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
3752
3753 (cond
3754 (start-in-macro
3755 ;; If we're in a macro then it's the closest preceding token
3756 ;; in the macro. Check this before `start-in-literal',
3757 ;; since if we're inside a literal in a macro, the preceding
3758 ;; token is earlier than any `c-decl-end' spot inside the
3759 ;; literal (comment).
3760 (goto-char (or start-in-literal cfd-start-pos))
3761 ;; The only syntactic ws in macros are comments.
3762 (c-backward-comments)
3763 (backward-char)
3764 (c-beginning-of-current-token))
3765
3766 (start-in-literal
3767 ;; If we're in a comment it can only be the closest
3768 ;; preceding `c-decl-end' position within that comment, if
3769 ;; any. Go back to the beginning of such a property so that
3770 ;; `c-find-decl-prefix-search' will find the end of it.
3771 ;; (Can't stop at the end and install it directly on
3772 ;; `cfd-prop-match' since that variable might be cleared
3773 ;; after `cfd-fun' below.)
3774 ;;
3775 ;; Note that if the literal is a string then the property
3776 ;; search will simply skip to the beginning of it right
3777 ;; away.
3778 (if (not c-type-decl-end-used)
3779 (goto-char start-in-literal)
3780 (goto-char cfd-start-pos)
3781 (while (progn
3782 (goto-char (previous-single-property-change
3783 (point) 'c-type nil start-in-literal))
3784 (and (> (point) start-in-literal)
3785 (not (eq (c-get-char-property (point) 'c-type)
3786 'c-decl-end))))))
3787
3788 (when (= (point) start-in-literal)
3789 ;; Didn't find any property inside the comment, so we can
3790 ;; skip it entirely. (This won't skip past a string, but
3791 ;; that'll be handled quickly by the next
3792 ;; `c-find-decl-prefix-search' anyway.)
3793 (c-forward-single-comment)
3794 (if (> (point) cfd-limit)
3795 (goto-char cfd-limit))))
3796
3797 (t
3798 ;; If we started in normal code, the only match that might
3799 ;; apply before the start is what we already got in
3800 ;; `cfd-match-pos' so we can continue at the start position.
3801 ;; (Note that we don't get here if the first match is below
3802 ;; it.)
3803 (goto-char cfd-start-pos)))
3804
3805 ;; Delete found matches if they are before our new continue
3806 ;; position, so that `c-find-decl-prefix-search' won't back up
3807 ;; to them later on.
3808 (setq cfd-continue-pos (point))
3809 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
3810 (setq cfd-re-match nil))
3811 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
3812 (setq cfd-prop-match nil)))
3813
3814 (if syntactic-pos
3815 ;; This is the normal case and we got a proper syntactic
3816 ;; position. If there's a match then it's always outside
3817 ;; macros and comments, so advance to the next token and set
3818 ;; `cfd-token-pos'. The loop below will later go back using
3819 ;; `cfd-continue-pos' to fix declarations inside the
3820 ;; syntactic ws.
3821 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
3822 (goto-char syntactic-pos)
3823 (c-forward-syntactic-ws)
3824 (and cfd-continue-pos
3825 (< cfd-continue-pos (point))
3826 (setq cfd-token-pos (point))))
3827
3828 ;; Have one of the special cases when the region is completely
3829 ;; within a literal or macro. `cfd-continue-pos' is set to a
3830 ;; good start position for the search, so do it.
3831 (c-find-decl-prefix-search)))
3832
3833 ;; Now loop. We already got the first match.
3834
3835 (while (progn
3836 (while (and
3837 (< cfd-match-pos cfd-limit)
3838
3839 (or
3840 ;; Kludge to filter out matches on the "<" that
3841 ;; aren't open parens, for the sake of languages
3842 ;; that got `c-recognize-<>-arglists' set.
3843 (and (eq (char-before cfd-match-pos) ?<)
3844 (not (c-get-char-property (1- cfd-match-pos)
3845 'syntax-table)))
3846
3847 ;; If `cfd-continue-pos' is less or equal to
3848 ;; `cfd-token-pos', we've got a hit inside a macro
3849 ;; that's in the syntactic whitespace before the last
3850 ;; "real" declaration we've checked. If they're equal
3851 ;; we've arrived at the declaration a second time, so
3852 ;; there's nothing to do.
3853 (= cfd-continue-pos cfd-token-pos)
3854
3855 (progn
3856 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3857 ;; we're still searching for declarations embedded in
3858 ;; the syntactic whitespace. In that case we need
3859 ;; only to skip comments and not macros, since they
3860 ;; can't be nested, and that's already been done in
3861 ;; `c-find-decl-prefix-search'.
3862 (when (> cfd-continue-pos cfd-token-pos)
3863 (c-forward-syntactic-ws)
3864 (setq cfd-token-pos (point)))
3865
3866 ;; Continue if the following token fails the
3867 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3868 (when (or (>= (point) cfd-limit)
3869 (not (looking-at cfd-decl-re))
3870 (and cfd-face-checklist
3871 (not (c-got-face-at
3872 (point) cfd-face-checklist))))
3873 (goto-char cfd-continue-pos)
3874 t)))
3875
3876 (< (point) cfd-limit))
3877 (c-find-decl-prefix-search))
3878
3879 (< (point) cfd-limit))
3880
3881 (when (and
3882 (>= (point) cfd-start-pos)
3883
3884 (progn
3885 ;; Narrow to the end of the macro if we got a hit inside
3886 ;; one, to avoid recognizing things that start inside the
3887 ;; macro and end outside it.
3888 (when (> cfd-match-pos cfd-macro-end)
3889 ;; Not in the same macro as in the previous round.
3890 (save-excursion
3891 (goto-char cfd-match-pos)
3892 (setq cfd-macro-end
3893 (if (save-excursion (and (c-beginning-of-macro)
3894 (< (point) cfd-match-pos)))
3895 (progn (c-end-of-macro)
3896 (point))
3897 0))))
3898
3899 (if (zerop cfd-macro-end)
3900 t
3901 (if (> cfd-macro-end (point))
3902 (progn (narrow-to-region (point-min) cfd-macro-end)
3903 t)
3904 ;; The matched token was the last thing in the macro,
3905 ;; so the whole match is bogus.
3906 (setq cfd-macro-end 0)
3907 nil))))
3908
3909 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3910 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3911 (setq cfd-prop-match nil))
3912
3913 (when (/= cfd-macro-end 0)
3914 ;; Restore limits if we did macro narrowment above.
3915 (narrow-to-region (point-min) cfd-buffer-end)))
3916
3917 (goto-char cfd-continue-pos)
3918 (if (= cfd-continue-pos cfd-limit)
3919 (setq cfd-match-pos cfd-limit)
3920 (c-find-decl-prefix-search)))))
3921
3922 \f
3923 ;; A cache for found types.
3924
3925 ;; Buffer local variable that contains an obarray with the types we've
3926 ;; found. If a declaration is recognized somewhere we record the
3927 ;; fully qualified identifier in it to recognize it as a type
3928 ;; elsewhere in the file too. This is not accurate since we do not
3929 ;; bother with the scoping rules of the languages, but in practice the
3930 ;; same name is seldom used as both a type and something else in a
3931 ;; file, and we only use this as a last resort in ambiguous cases (see
3932 ;; `c-forward-decl-or-cast-1').
3933 ;;
3934 ;; Template types in C++ are added here too but with the template
3935 ;; arglist replaced with "<>" in references or "<" for the one in the
3936 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
3937 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
3938 ;; template specs can be fairly sized programs in themselves) and
3939 ;; improves the hit ratio (it's a type regardless of the template
3940 ;; args; it's just not the same type, but we're only interested in
3941 ;; recognizing types, not telling distinct types apart). Note that
3942 ;; template types in references are added here too; from the example
3943 ;; above there will also be an entry "Foo<".
3944 (defvar c-found-types nil)
3945 (make-variable-buffer-local 'c-found-types)
3946
3947 (defsubst c-clear-found-types ()
3948 ;; Clears `c-found-types'.
3949 (setq c-found-types (make-vector 53 0)))
3950
3951 (defun c-add-type (from to)
3952 ;; Add the given region as a type in `c-found-types'. If the region
3953 ;; doesn't match an existing type but there is a type which is equal
3954 ;; to the given one except that the last character is missing, then
3955 ;; the shorter type is removed. That's done to avoid adding all
3956 ;; prefixes of a type as it's being entered and font locked. This
3957 ;; doesn't cover cases like when characters are removed from a type
3958 ;; or added in the middle. We'd need the position of point when the
3959 ;; font locking is invoked to solve this well.
3960 ;;
3961 ;; This function might do hidden buffer changes.
3962 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
3963 (unless (intern-soft type c-found-types)
3964 (unintern (substring type 0 -1) c-found-types)
3965 (intern type c-found-types))))
3966
3967 (defsubst c-check-type (from to)
3968 ;; Return non-nil if the given region contains a type in
3969 ;; `c-found-types'.
3970 ;;
3971 ;; This function might do hidden buffer changes.
3972 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
3973 c-found-types))
3974
3975 (defun c-list-found-types ()
3976 ;; Return all the types in `c-found-types' as a sorted list of
3977 ;; strings.
3978 (let (type-list)
3979 (mapatoms (lambda (type)
3980 (setq type-list (cons (symbol-name type)
3981 type-list)))
3982 c-found-types)
3983 (sort type-list 'string-lessp)))
3984
3985 \f
3986 ;; Handling of small scale constructs like types and names.
3987
3988 (defun c-after-change-check-<>-operators (beg end)
3989 ;; This is called from `after-change-functions' when
3990 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
3991 ;; chars with paren syntax become part of another operator like "<<"
3992 ;; or ">=".
3993 ;;
3994 ;; This function might do hidden buffer changes.
3995
3996 (save-match-data
3997 (save-excursion
3998 (goto-char beg)
3999 (when (or (looking-at "[<>]")
4000 (< (skip-chars-backward "<>") 0))
4001
4002 (goto-char beg)
4003 (c-beginning-of-current-token)
4004 (when (and (< (point) beg)
4005 (looking-at c-<>-multichar-token-regexp)
4006 (< beg (setq beg (match-end 0))))
4007 (while (progn (skip-chars-forward "^<>" beg)
4008 (< (point) beg))
4009 (c-clear-char-property (point) 'syntax-table)
4010 (forward-char))))
4011
4012 (when (< beg end)
4013 (goto-char end)
4014 (when (or (looking-at "[<>]")
4015 (< (skip-chars-backward "<>") 0))
4016
4017 (goto-char end)
4018 (c-beginning-of-current-token)
4019 (when (and (< (point) end)
4020 (looking-at c-<>-multichar-token-regexp)
4021 (< end (setq end (match-end 0))))
4022 (while (progn (skip-chars-forward "^<>" end)
4023 (< (point) end))
4024 (c-clear-char-property (point) 'syntax-table)
4025 (forward-char))))))))
4026
4027 ;; Dynamically bound variable that instructs `c-forward-type' to also
4028 ;; treat possible types (i.e. those that it normally returns 'maybe or
4029 ;; 'found for) as actual types (and always return 'found for them).
4030 ;; This means that it records them in `c-record-type-identifiers' if
4031 ;; that is set, and that it adds them to `c-found-types'.
4032 (defvar c-promote-possible-types nil)
4033
4034 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4035 ;; mark up successfully parsed arglists with paren syntax properties on
4036 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
4037 ;; `c-type' property of each argument separating comma.
4038 ;;
4039 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
4040 ;; all arglists for side effects (i.e. recording types), otherwise it
4041 ;; exploits any existing paren syntax properties to quickly jump to the
4042 ;; end of already parsed arglists.
4043 ;;
4044 ;; Marking up the arglists is not the default since doing that correctly
4045 ;; depends on a proper value for `c-restricted-<>-arglists'.
4046 (defvar c-parse-and-markup-<>-arglists nil)
4047
4048 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4049 ;; not accept arglists that contain binary operators.
4050 ;;
4051 ;; This is primarily used to handle C++ template arglists. C++
4052 ;; disambiguates them by checking whether the preceding name is a
4053 ;; template or not. We can't do that, so we assume it is a template
4054 ;; if it can be parsed as one. That usually works well since
4055 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
4056 ;; in almost all cases would be pointless.
4057 ;;
4058 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
4059 ;; should let the comma separate the function arguments instead. And
4060 ;; in a context where the value of the expression is taken, e.g. in
4061 ;; "if (a < b || c > d)", it's probably not a template.
4062 (defvar c-restricted-<>-arglists nil)
4063
4064 ;; Dynamically bound variables that instructs
4065 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
4066 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
4067 ;; `c-forward-label' to record the ranges of all the type and
4068 ;; reference identifiers they encounter. They will build lists on
4069 ;; these variables where each element is a cons of the buffer
4070 ;; positions surrounding each identifier. This recording is only
4071 ;; activated when `c-record-type-identifiers' is non-nil.
4072 ;;
4073 ;; All known types that can't be identifiers are recorded, and also
4074 ;; other possible types if `c-promote-possible-types' is set.
4075 ;; Recording is however disabled inside angle bracket arglists that
4076 ;; are encountered inside names and other angle bracket arglists.
4077 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
4078 ;; instead.
4079 ;;
4080 ;; Only the names in C++ template style references (e.g. "tmpl" in
4081 ;; "tmpl<a,b>::foo") are recorded as references, other references
4082 ;; aren't handled here.
4083 ;;
4084 ;; `c-forward-label' records the label identifier(s) on
4085 ;; `c-record-ref-identifiers'.
4086 (defvar c-record-type-identifiers nil)
4087 (defvar c-record-ref-identifiers nil)
4088
4089 ;; This variable will receive a cons cell of the range of the last
4090 ;; single identifier symbol stepped over by `c-forward-name' if it's
4091 ;; successful. This is the range that should be put on one of the
4092 ;; record lists above by the caller. It's assigned nil if there's no
4093 ;; such symbol in the name.
4094 (defvar c-last-identifier-range nil)
4095
4096 (defmacro c-record-type-id (range)
4097 (if (eq (car-safe range) 'cons)
4098 ;; Always true.
4099 `(setq c-record-type-identifiers
4100 (cons ,range c-record-type-identifiers))
4101 `(let ((range ,range))
4102 (if range
4103 (setq c-record-type-identifiers
4104 (cons range c-record-type-identifiers))))))
4105
4106 (defmacro c-record-ref-id (range)
4107 (if (eq (car-safe range) 'cons)
4108 ;; Always true.
4109 `(setq c-record-ref-identifiers
4110 (cons ,range c-record-ref-identifiers))
4111 `(let ((range ,range))
4112 (if range
4113 (setq c-record-ref-identifiers
4114 (cons range c-record-ref-identifiers))))))
4115
4116 ;; Dynamically bound variable that instructs `c-forward-type' to
4117 ;; record the ranges of types that only are found. Behaves otherwise
4118 ;; like `c-record-type-identifiers'.
4119 (defvar c-record-found-types nil)
4120
4121 (defmacro c-forward-keyword-prefixed-id (type)
4122 ;; Used internally in `c-forward-keyword-clause' to move forward
4123 ;; over a type (if TYPE is 'type) or a name (otherwise) which
4124 ;; possibly is prefixed by keywords and their associated clauses.
4125 ;; Try with a type/name first to not trip up on those that begin
4126 ;; with a keyword. Return t if a known or found type is moved
4127 ;; over. The point is clobbered if nil is returned. If range
4128 ;; recording is enabled, the identifier is recorded on as a type
4129 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
4130 ;;
4131 ;; This macro might do hidden buffer changes.
4132 `(let (res)
4133 (while (if (setq res ,(if (eq type 'type)
4134 `(c-forward-type)
4135 `(c-forward-name)))
4136 nil
4137 (and (looking-at c-keywords-regexp)
4138 (c-forward-keyword-clause 1))))
4139 (when (memq res '(t known found prefix))
4140 ,(when (eq type 'ref)
4141 `(when c-record-type-identifiers
4142 (c-record-ref-id c-last-identifier-range)))
4143 t)))
4144
4145 (defmacro c-forward-id-comma-list (type update-safe-pos)
4146 ;; Used internally in `c-forward-keyword-clause' to move forward
4147 ;; over a comma separated list of types or names using
4148 ;; `c-forward-keyword-prefixed-id'.
4149 ;;
4150 ;; This macro might do hidden buffer changes.
4151 `(while (and (progn
4152 ,(when update-safe-pos
4153 `(setq safe-pos (point)))
4154 (eq (char-after) ?,))
4155 (progn
4156 (forward-char)
4157 (c-forward-syntactic-ws)
4158 (c-forward-keyword-prefixed-id ,type)))))
4159
4160 (defun c-forward-keyword-clause (match)
4161 ;; Submatch MATCH in the current match data is assumed to surround a
4162 ;; token. If it's a keyword, move over it and any immediately
4163 ;; following clauses associated with it, stopping at the start of
4164 ;; the next token. t is returned in that case, otherwise the point
4165 ;; stays and nil is returned. The kind of clauses that are
4166 ;; recognized are those specified by `c-type-list-kwds',
4167 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
4168 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
4169 ;; and `c-<>-arglist-kwds'.
4170 ;;
4171 ;; This function records identifier ranges on
4172 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4173 ;; `c-record-type-identifiers' is non-nil.
4174 ;;
4175 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
4176 ;; apply directly after the keyword, the type list is moved over
4177 ;; only when there is no unaccounted token before it (i.e. a token
4178 ;; that isn't moved over due to some other keyword list). The
4179 ;; identifier ranges in the list are still recorded if that should
4180 ;; be done, though.
4181 ;;
4182 ;; This function might do hidden buffer changes.
4183
4184 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
4185 ;; The call to `c-forward-<>-arglist' below is made after
4186 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
4187 ;; are angle bracket arglists and `c-restricted-<>-arglists'
4188 ;; should therefore be nil.
4189 (c-parse-and-markup-<>-arglists t)
4190 c-restricted-<>-arglists)
4191
4192 (when kwd-sym
4193 (goto-char (match-end match))
4194 (c-forward-syntactic-ws)
4195 (setq safe-pos (point))
4196
4197 (cond
4198 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
4199 (c-forward-keyword-prefixed-id type))
4200 ;; There's a type directly after a keyword in `c-type-list-kwds'.
4201 (c-forward-id-comma-list type t))
4202
4203 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
4204 (c-forward-keyword-prefixed-id ref))
4205 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
4206 (c-forward-id-comma-list ref t))
4207
4208 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
4209 (eq (char-after) ?\())
4210 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
4211
4212 (forward-char)
4213 (when (and (setq pos (c-up-list-forward))
4214 (eq (char-before pos) ?\)))
4215 (when (and c-record-type-identifiers
4216 (c-keyword-member kwd-sym 'c-paren-type-kwds))
4217 ;; Use `c-forward-type' on every identifier we can find
4218 ;; inside the paren, to record the types.
4219 (while (c-syntactic-re-search-forward c-symbol-start pos t)
4220 (goto-char (match-beginning 0))
4221 (unless (c-forward-type)
4222 (looking-at c-symbol-key) ; Always matches.
4223 (goto-char (match-end 0)))))
4224
4225 (goto-char pos)
4226 (c-forward-syntactic-ws)
4227 (setq safe-pos (point))))
4228
4229 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
4230 (eq (char-after) ?<)
4231 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
4232 (c-forward-syntactic-ws)
4233 (setq safe-pos (point)))
4234
4235 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
4236 (not (looking-at c-symbol-start))
4237 (c-safe (c-forward-sexp) t))
4238 (c-forward-syntactic-ws)
4239 (setq safe-pos (point))))
4240
4241 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
4242 (if (eq (char-after) ?:)
4243 ;; If we are at the colon already, we move over the type
4244 ;; list after it.
4245 (progn
4246 (forward-char)
4247 (c-forward-syntactic-ws)
4248 (when (c-forward-keyword-prefixed-id type)
4249 (c-forward-id-comma-list type t)))
4250 ;; Not at the colon, so stop here. But the identifier
4251 ;; ranges in the type list later on should still be
4252 ;; recorded.
4253 (and c-record-type-identifiers
4254 (progn
4255 ;; If a keyword matched both one of the types above and
4256 ;; this one, we match `c-colon-type-list-re' after the
4257 ;; clause matched above.
4258 (goto-char safe-pos)
4259 (looking-at c-colon-type-list-re))
4260 (progn
4261 (goto-char (match-end 0))
4262 (c-forward-syntactic-ws)
4263 (c-forward-keyword-prefixed-id type))
4264 ;; There's a type after the `c-colon-type-list-re' match
4265 ;; after a keyword in `c-colon-type-list-kwds'.
4266 (c-forward-id-comma-list type nil))))
4267
4268 (goto-char safe-pos)
4269 t)))
4270
4271 (defun c-forward-<>-arglist (all-types)
4272 ;; The point is assumed to be at a "<". Try to treat it as the open
4273 ;; paren of an angle bracket arglist and move forward to the the
4274 ;; corresponding ">". If successful, the point is left after the
4275 ;; ">" and t is returned, otherwise the point isn't moved and nil is
4276 ;; returned. If ALL-TYPES is t then all encountered arguments in
4277 ;; the arglist that might be types are treated as found types.
4278 ;;
4279 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
4280 ;; function handles text properties on the angle brackets and argument
4281 ;; separating commas.
4282 ;;
4283 ;; `c-restricted-<>-arglists' controls how lenient the template
4284 ;; arglist recognition should be.
4285 ;;
4286 ;; This function records identifier ranges on
4287 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4288 ;; `c-record-type-identifiers' is non-nil.
4289 ;;
4290 ;; This function might do hidden buffer changes.
4291
4292 (let ((start (point))
4293 ;; If `c-record-type-identifiers' is set then activate
4294 ;; recording of any found types that constitute an argument in
4295 ;; the arglist.
4296 (c-record-found-types (if c-record-type-identifiers t)))
4297 (if (catch 'angle-bracket-arglist-escape
4298 (setq c-record-found-types
4299 (c-forward-<>-arglist-recur all-types)))
4300 (progn
4301 (when (consp c-record-found-types)
4302 (setq c-record-type-identifiers
4303 ;; `nconc' doesn't mind that the tail of
4304 ;; `c-record-found-types' is t.
4305 (nconc c-record-found-types c-record-type-identifiers)))
4306 t)
4307
4308 (goto-char start)
4309 nil)))
4310
4311 (defun c-forward-<>-arglist-recur (all-types)
4312 ;; Recursive part of `c-forward-<>-arglist'.
4313 ;;
4314 ;; This function might do hidden buffer changes.
4315
4316 (let ((start (point)) res pos tmp
4317 ;; Cover this so that any recorded found type ranges are
4318 ;; automatically lost if it turns out to not be an angle
4319 ;; bracket arglist. It's propagated through the return value
4320 ;; on successful completion.
4321 (c-record-found-types c-record-found-types)
4322 ;; List that collects the positions after the argument
4323 ;; separating ',' in the arglist.
4324 arg-start-pos)
4325
4326 ;; If the '<' has paren open syntax then we've marked it as an angle
4327 ;; bracket arglist before, so skip to the end.
4328 (if (and (not c-parse-and-markup-<>-arglists)
4329 (c-get-char-property (point) 'syntax-table))
4330
4331 (progn
4332 (forward-char)
4333 (if (and (c-go-up-list-forward)
4334 (eq (char-before) ?>))
4335 t
4336
4337 ;; Got unmatched paren angle brackets. We don't clear the paren
4338 ;; syntax properties and retry, on the basis that it's very
4339 ;; unlikely that paren angle brackets become operators by code
4340 ;; manipulation. It's far more likely that it doesn't match due
4341 ;; to narrowing or some temporary change.
4342 (goto-char start)
4343 nil))
4344
4345 (forward-char)
4346 (unless (looking-at c-<-op-cont-regexp)
4347 (while (and
4348 (progn
4349
4350 (when c-record-type-identifiers
4351 (if all-types
4352
4353 ;; All encountered identifiers are types, so set the
4354 ;; promote flag and parse the type.
4355 (progn
4356 (c-forward-syntactic-ws)
4357 (when (looking-at c-identifier-start)
4358 (let ((c-promote-possible-types t))
4359 (c-forward-type))))
4360
4361 ;; Check if this arglist argument is a sole type. If
4362 ;; it's known then it's recorded in
4363 ;; `c-record-type-identifiers'. If it only is found
4364 ;; then it's recorded in `c-record-found-types' which we
4365 ;; might roll back if it turns out that this isn't an
4366 ;; angle bracket arglist afterall.
4367 (when (memq (char-before) '(?, ?<))
4368 (let ((orig-record-found-types c-record-found-types))
4369 (c-forward-syntactic-ws)
4370 (and (memq (c-forward-type) '(known found))
4371 (not (looking-at "[,>]"))
4372 ;; A found type was recorded but it's not the
4373 ;; only thing in the arglist argument, so reset
4374 ;; `c-record-found-types'.
4375 (setq c-record-found-types
4376 orig-record-found-types))))))
4377
4378 (setq pos (point))
4379 (or (when (eq (char-after) ?>)
4380 ;; Must check for '>' at the very start separately,
4381 ;; since the regexp below has to avoid ">>" without
4382 ;; using \\=.
4383 (forward-char)
4384 t)
4385
4386 ;; Note: These regexps exploit the match order in \| so
4387 ;; that "<>" is matched by "<" rather than "[^>:-]>".
4388 (c-syntactic-re-search-forward
4389 (if c-restricted-<>-arglists
4390 ;; Stop on ',', '|', '&', '+' and '-' to catch
4391 ;; common binary operators that could be between
4392 ;; two comparison expressions "a<b" and "c>d".
4393 "[<;{},|&+-]\\|\\([^>:-]>\\)"
4394 ;; Otherwise we still stop on ',' to find the
4395 ;; argument start positions.
4396 "[<;{},]\\|\\([^>:-]>\\)")
4397 nil 'move t t 1)
4398
4399 ;; If the arglist starter has lost its open paren
4400 ;; syntax but not the closer, we won't find the
4401 ;; closer above since we only search in the
4402 ;; balanced sexp. In that case we stop just short
4403 ;; of it so check if the following char is the closer.
4404 (when (eq (char-after) ?>)
4405 (forward-char)
4406 t)))
4407
4408 (cond
4409 ((eq (char-before) ?>)
4410 ;; Either an operator starting with '>' or the end of
4411 ;; the angle bracket arglist.
4412
4413 (if (looking-at c->-op-cont-regexp)
4414 (progn
4415 (goto-char (match-end 0))
4416 t) ; Continue the loop.
4417
4418 ;; The angle bracket arglist is finished.
4419 (when c-parse-and-markup-<>-arglists
4420 (while arg-start-pos
4421 (c-put-c-type-property (1- (car arg-start-pos))
4422 'c-<>-arg-sep)
4423 (setq arg-start-pos (cdr arg-start-pos)))
4424 (c-mark-<-as-paren start)
4425 (c-mark->-as-paren (1- (point))))
4426 (setq res t)
4427 nil)) ; Exit the loop.
4428
4429 ((eq (char-before) ?<)
4430 ;; Either an operator starting with '<' or a nested arglist.
4431
4432 (setq pos (point))
4433 (let (id-start id-end subres keyword-match)
4434 (if (if (looking-at c-<-op-cont-regexp)
4435 (setq tmp (match-end 0))
4436 (setq tmp pos)
4437 (backward-char)
4438 (not
4439 (and
4440
4441 (save-excursion
4442 ;; There's always an identifier before an angle
4443 ;; bracket arglist, or a keyword in
4444 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
4445 (c-backward-syntactic-ws)
4446 (setq id-end (point))
4447 (c-simple-skip-symbol-backward)
4448 (when (or (setq keyword-match
4449 (looking-at c-opt-<>-sexp-key))
4450 (not (looking-at c-keywords-regexp)))
4451 (setq id-start (point))))
4452
4453 (setq subres
4454 (let ((c-record-type-identifiers nil)
4455 (c-record-found-types nil))
4456 (c-forward-<>-arglist-recur
4457 (and keyword-match
4458 (c-keyword-member
4459 (c-keyword-sym (match-string 1))
4460 'c-<>-type-kwds)))))
4461 )))
4462
4463 ;; It was not an angle bracket arglist.
4464 (goto-char tmp)
4465
4466 ;; It was an angle bracket arglist.
4467 (setq c-record-found-types subres)
4468
4469 ;; Record the identifier before the template as a type
4470 ;; or reference depending on whether the arglist is last
4471 ;; in a qualified identifier.
4472 (when (and c-record-type-identifiers
4473 (not keyword-match))
4474 (if (and c-opt-identifier-concat-key
4475 (progn
4476 (c-forward-syntactic-ws)
4477 (looking-at c-opt-identifier-concat-key)))
4478 (c-record-ref-id (cons id-start id-end))
4479 (c-record-type-id (cons id-start id-end))))))
4480 t)
4481
4482 ((and (eq (char-before) ?,)
4483 (not c-restricted-<>-arglists))
4484 ;; Just another argument. Record the position. The
4485 ;; type check stuff that made us stop at it is at
4486 ;; the top of the loop.
4487 (setq arg-start-pos (cons (point) arg-start-pos)))
4488
4489 (t
4490 ;; Got a character that can't be in an angle bracket
4491 ;; arglist argument. Abort using `throw', since
4492 ;; it's useless to try to find a surrounding arglist
4493 ;; if we're nested.
4494 (throw 'angle-bracket-arglist-escape nil))))))
4495
4496 (if res
4497 (or c-record-found-types t)))))
4498
4499 (defun c-backward-<>-arglist (all-types &optional limit)
4500 ;; The point is assumed to be directly after a ">". Try to treat it
4501 ;; as the close paren of an angle bracket arglist and move back to
4502 ;; the corresponding "<". If successful, the point is left at
4503 ;; the "<" and t is returned, otherwise the point isn't moved and
4504 ;; nil is returned. ALL-TYPES is passed on to
4505 ;; `c-forward-<>-arglist'.
4506 ;;
4507 ;; If the optional LIMIT is given, it bounds the backward search.
4508 ;; It's then assumed to be at a syntactically relevant position.
4509 ;;
4510 ;; This is a wrapper around `c-forward-<>-arglist'. See that
4511 ;; function for more details.
4512
4513 (let ((start (point)))
4514 (backward-char)
4515 (if (and (not c-parse-and-markup-<>-arglists)
4516 (c-get-char-property (point) 'syntax-table))
4517
4518 (if (and (c-go-up-list-backward)
4519 (eq (char-after) ?<))
4520 t
4521 ;; See corresponding note in `c-forward-<>-arglist'.
4522 (goto-char start)
4523 nil)
4524
4525 (while (and
4526 (c-syntactic-skip-backward "^<;{}" limit t)
4527
4528 (if (eq (char-before) ?<)
4529 t
4530 ;; Stopped at bob or a char that isn't allowed in an
4531 ;; arglist, so we've failed.
4532 (goto-char start)
4533 nil)
4534
4535 (if (> (point)
4536 (progn (c-beginning-of-current-token)
4537 (point)))
4538 ;; If we moved then the "<" was part of some
4539 ;; multicharacter token.
4540 t
4541
4542 (backward-char)
4543 (let ((beg-pos (point)))
4544 (if (c-forward-<>-arglist all-types)
4545 (cond ((= (point) start)
4546 ;; Matched the arglist. Break the while.
4547 (goto-char beg-pos)
4548 nil)
4549 ((> (point) start)
4550 ;; We started from a non-paren ">" inside an
4551 ;; arglist.
4552 (goto-char start)
4553 nil)
4554 (t
4555 ;; Matched a shorter arglist. Can be a nested
4556 ;; one so continue looking.
4557 (goto-char beg-pos)
4558 t))
4559 t)))))
4560
4561 (/= (point) start))))
4562
4563 (defun c-forward-name ()
4564 ;; Move forward over a complete name if at the beginning of one,
4565 ;; stopping at the next following token. If the point is not at
4566 ;; something that are recognized as name then it stays put. A name
4567 ;; could be something as simple as "foo" in C or something as
4568 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
4569 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
4570 ;; int>::*volatile const" in C++ (this function is actually little
4571 ;; more than a `looking-at' call in all modes except those that,
4572 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
4573 ;; name is found, 'template if it's an identifier ending with an
4574 ;; angle bracket arglist, 'operator of it's an operator identifier,
4575 ;; or t if it's some other kind of name.
4576 ;;
4577 ;; This function records identifier ranges on
4578 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4579 ;; `c-record-type-identifiers' is non-nil.
4580 ;;
4581 ;; This function might do hidden buffer changes.
4582
4583 (let ((pos (point)) (start (point)) res id-start id-end
4584 ;; Turn off `c-promote-possible-types' here since we might
4585 ;; call `c-forward-<>-arglist' and we don't want it to promote
4586 ;; every suspect thing in the arglist to a type. We're
4587 ;; typically called from `c-forward-type' in this case, and
4588 ;; the caller only wants the top level type that it finds to
4589 ;; be promoted.
4590 c-promote-possible-types)
4591 (while
4592 (and
4593 (looking-at c-identifier-key)
4594
4595 (progn
4596 ;; Check for keyword. We go to the last symbol in
4597 ;; `c-identifier-key' first.
4598 (goto-char (setq id-end (match-end 0)))
4599 (c-simple-skip-symbol-backward)
4600 (setq id-start (point))
4601
4602 (if (looking-at c-keywords-regexp)
4603 (when (and (c-major-mode-is 'c++-mode)
4604 (looking-at
4605 (cc-eval-when-compile
4606 (concat "\\(operator\\|\\(template\\)\\)"
4607 "\\(" (c-lang-const c-nonsymbol-key c++)
4608 "\\|$\\)")))
4609 (if (match-beginning 2)
4610 ;; "template" is only valid inside an
4611 ;; identifier if preceded by "::".
4612 (save-excursion
4613 (c-backward-syntactic-ws)
4614 (and (c-safe (backward-char 2) t)
4615 (looking-at "::")))
4616 t))
4617
4618 ;; Handle a C++ operator or template identifier.
4619 (goto-char id-end)
4620 (c-forward-syntactic-ws)
4621 (cond ((eq (char-before id-end) ?e)
4622 ;; Got "... ::template".
4623 (let ((subres (c-forward-name)))
4624 (when subres
4625 (setq pos (point)
4626 res subres))))
4627
4628 ((looking-at c-identifier-start)
4629 ;; Got a cast operator.
4630 (when (c-forward-type)
4631 (setq pos (point)
4632 res 'operator)
4633 ;; Now we should match a sequence of either
4634 ;; '*', '&' or a name followed by ":: *",
4635 ;; where each can be followed by a sequence
4636 ;; of `c-opt-type-modifier-key'.
4637 (while (cond ((looking-at "[*&]")
4638 (goto-char (match-end 0))
4639 t)
4640 ((looking-at c-identifier-start)
4641 (and (c-forward-name)
4642 (looking-at "::")
4643 (progn
4644 (goto-char (match-end 0))
4645 (c-forward-syntactic-ws)
4646 (eq (char-after) ?*))
4647 (progn
4648 (forward-char)
4649 t))))
4650 (while (progn
4651 (c-forward-syntactic-ws)
4652 (setq pos (point))
4653 (looking-at c-opt-type-modifier-key))
4654 (goto-char (match-end 1))))))
4655
4656 ((looking-at c-overloadable-operators-regexp)
4657 ;; Got some other operator.
4658 (setq c-last-identifier-range
4659 (cons (point) (match-end 0)))
4660 (goto-char (match-end 0))
4661 (c-forward-syntactic-ws)
4662 (setq pos (point)
4663 res 'operator)))
4664
4665 nil)
4666
4667 ;; `id-start' is equal to `id-end' if we've jumped over
4668 ;; an identifier that doesn't end with a symbol token.
4669 ;; That can occur e.g. for Java import directives on the
4670 ;; form "foo.bar.*".
4671 (when (and id-start (/= id-start id-end))
4672 (setq c-last-identifier-range
4673 (cons id-start id-end)))
4674 (goto-char id-end)
4675 (c-forward-syntactic-ws)
4676 (setq pos (point)
4677 res t)))
4678
4679 (progn
4680 (goto-char pos)
4681 (when (or c-opt-identifier-concat-key
4682 c-recognize-<>-arglists)
4683
4684 (cond
4685 ((and c-opt-identifier-concat-key
4686 (looking-at c-opt-identifier-concat-key))
4687 ;; Got a concatenated identifier. This handles the
4688 ;; cases with tricky syntactic whitespace that aren't
4689 ;; covered in `c-identifier-key'.
4690 (goto-char (match-end 0))
4691 (c-forward-syntactic-ws)
4692 t)
4693
4694 ((and c-recognize-<>-arglists
4695 (eq (char-after) ?<))
4696 ;; Maybe an angle bracket arglist.
4697
4698 (when (let (c-record-type-identifiers
4699 c-record-found-types)
4700 (c-forward-<>-arglist nil))
4701
4702 (c-add-type start (1+ pos))
4703 (c-forward-syntactic-ws)
4704 (setq pos (point)
4705 c-last-identifier-range nil)
4706
4707 (if (and c-opt-identifier-concat-key
4708 (looking-at c-opt-identifier-concat-key))
4709
4710 ;; Continue if there's an identifier concatenation
4711 ;; operator after the template argument.
4712 (progn
4713 (when (and c-record-type-identifiers id-start)
4714 (c-record-ref-id (cons id-start id-end)))
4715 (forward-char 2)
4716 (c-forward-syntactic-ws)
4717 t)
4718
4719 (when (and c-record-type-identifiers id-start)
4720 (c-record-type-id (cons id-start id-end)))
4721 (setq res 'template)
4722 nil)))
4723 )))))
4724
4725 (goto-char pos)
4726 res))
4727
4728 (defun c-forward-type ()
4729 ;; Move forward over a type spec if at the beginning of one,
4730 ;; stopping at the next following token. Return t if it's a known
4731 ;; type that can't be a name or other expression, 'known if it's an
4732 ;; otherwise known type (according to `*-font-lock-extra-types'),
4733 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4734 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4735 ;; that might be a type, or nil if it can't be a type (the point
4736 ;; isn't moved then). The point is assumed to be at the beginning
4737 ;; of a token.
4738 ;;
4739 ;; Note that this function doesn't skip past the brace definition
4740 ;; that might be considered part of the type, e.g.
4741 ;; "enum {a, b, c} foo".
4742 ;;
4743 ;; This function records identifier ranges on
4744 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4745 ;; `c-record-type-identifiers' is non-nil.
4746 ;;
4747 ;; This function might do hidden buffer changes.
4748
4749 (let ((start (point)) pos res name-res id-start id-end id-range)
4750
4751 ;; Skip leading type modifiers. If any are found we know it's a
4752 ;; prefix of a type.
4753 (when c-opt-type-modifier-key
4754 (while (looking-at c-opt-type-modifier-key)
4755 (goto-char (match-end 1))
4756 (c-forward-syntactic-ws)
4757 (setq res 'prefix)))
4758
4759 (cond
4760 ((looking-at c-type-prefix-key)
4761 ;; Looking at a keyword that prefixes a type identifier,
4762 ;; e.g. "class".
4763 (goto-char (match-end 1))
4764 (c-forward-syntactic-ws)
4765 (setq pos (point))
4766 (if (memq (setq name-res (c-forward-name)) '(t template))
4767 (progn
4768 (when (eq name-res t)
4769 ;; In many languages the name can be used without the
4770 ;; prefix, so we add it to `c-found-types'.
4771 (c-add-type pos (point))
4772 (when (and c-record-type-identifiers
4773 c-last-identifier-range)
4774 (c-record-type-id c-last-identifier-range)))
4775 (setq res t))
4776 ;; Invalid syntax.
4777 (goto-char start)
4778 (setq res nil)))
4779
4780 ((progn
4781 (setq pos nil)
4782 (if (looking-at c-identifier-start)
4783 (save-excursion
4784 (setq id-start (point)
4785 name-res (c-forward-name))
4786 (when name-res
4787 (setq id-end (point)
4788 id-range c-last-identifier-range))))
4789 (and (cond ((looking-at c-primitive-type-key)
4790 (setq res t))
4791 ((c-with-syntax-table c-identifier-syntax-table
4792 (looking-at c-known-type-key))
4793 (setq res 'known)))
4794 (or (not id-end)
4795 (>= (save-excursion
4796 (save-match-data
4797 (goto-char (match-end 1))
4798 (c-forward-syntactic-ws)
4799 (setq pos (point))))
4800 id-end)
4801 (setq res nil))))
4802 ;; Looking at a primitive or known type identifier. We've
4803 ;; checked for a name first so that we don't go here if the
4804 ;; known type match only is a prefix of another name.
4805
4806 (setq id-end (match-end 1))
4807
4808 (when (and c-record-type-identifiers
4809 (or c-promote-possible-types (eq res t)))
4810 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4811
4812 (if (and c-opt-type-component-key
4813 (save-match-data
4814 (looking-at c-opt-type-component-key)))
4815 ;; There might be more keywords for the type.
4816 (let (safe-pos)
4817 (c-forward-keyword-clause 1)
4818 (while (progn
4819 (setq safe-pos (point))
4820 (looking-at c-opt-type-component-key))
4821 (when (and c-record-type-identifiers
4822 (looking-at c-primitive-type-key))
4823 (c-record-type-id (cons (match-beginning 1)
4824 (match-end 1))))
4825 (c-forward-keyword-clause 1))
4826 (if (looking-at c-primitive-type-key)
4827 (progn
4828 (when c-record-type-identifiers
4829 (c-record-type-id (cons (match-beginning 1)
4830 (match-end 1))))
4831 (c-forward-keyword-clause 1)
4832 (setq res t))
4833 (goto-char safe-pos)
4834 (setq res 'prefix)))
4835 (unless (save-match-data (c-forward-keyword-clause 1))
4836 (if pos
4837 (goto-char pos)
4838 (goto-char (match-end 1))
4839 (c-forward-syntactic-ws)))))
4840
4841 (name-res
4842 (cond ((eq name-res t)
4843 ;; A normal identifier.
4844 (goto-char id-end)
4845 (if (or res c-promote-possible-types)
4846 (progn
4847 (c-add-type id-start id-end)
4848 (when (and c-record-type-identifiers id-range)
4849 (c-record-type-id id-range))
4850 (unless res
4851 (setq res 'found)))
4852 (setq res (if (c-check-type id-start id-end)
4853 ;; It's an identifier that has been used as
4854 ;; a type somewhere else.
4855 'found
4856 ;; It's an identifier that might be a type.
4857 'maybe))))
4858 ((eq name-res 'template)
4859 ;; A template is a type.
4860 (goto-char id-end)
4861 (setq res t))
4862 (t
4863 ;; Otherwise it's an operator identifier, which is not a type.
4864 (goto-char start)
4865 (setq res nil)))))
4866
4867 (when res
4868 ;; Skip trailing type modifiers. If any are found we know it's
4869 ;; a type.
4870 (when c-opt-type-modifier-key
4871 (while (looking-at c-opt-type-modifier-key)
4872 (goto-char (match-end 1))
4873 (c-forward-syntactic-ws)
4874 (setq res t)))
4875
4876 ;; Step over any type suffix operator. Do not let the existence
4877 ;; of these alter the classification of the found type, since
4878 ;; these operators typically are allowed in normal expressions
4879 ;; too.
4880 (when c-opt-type-suffix-key
4881 (while (looking-at c-opt-type-suffix-key)
4882 (goto-char (match-end 1))
4883 (c-forward-syntactic-ws)))
4884
4885 (when c-opt-type-concat-key
4886 ;; Look for a trailing operator that concatenates the type
4887 ;; with a following one, and if so step past that one through
4888 ;; a recursive call. Note that we don't record concatenated
4889 ;; types in `c-found-types' - it's the component types that
4890 ;; are recorded when appropriate.
4891 (setq pos (point))
4892 (let* ((c-promote-possible-types (or (memq res '(t known))
4893 c-promote-possible-types))
4894 ;; If we can't promote then set `c-record-found-types' so that
4895 ;; we can merge in the types from the second part afterwards if
4896 ;; it turns out to be a known type there.
4897 (c-record-found-types (and c-record-type-identifiers
4898 (not c-promote-possible-types)))
4899 subres)
4900 (if (and (looking-at c-opt-type-concat-key)
4901
4902 (progn
4903 (goto-char (match-end 1))
4904 (c-forward-syntactic-ws)
4905 (setq subres (c-forward-type))))
4906
4907 (progn
4908 ;; If either operand certainly is a type then both are, but we
4909 ;; don't let the existence of the operator itself promote two
4910 ;; uncertain types to a certain one.
4911 (cond ((eq res t))
4912 ((eq subres t)
4913 (unless (eq name-res 'template)
4914 (c-add-type id-start id-end))
4915 (when (and c-record-type-identifiers id-range)
4916 (c-record-type-id id-range))
4917 (setq res t))
4918 ((eq res 'known))
4919 ((eq subres 'known)
4920 (setq res 'known))
4921 ((eq res 'found))
4922 ((eq subres 'found)
4923 (setq res 'found))
4924 (t
4925 (setq res 'maybe)))
4926
4927 (when (and (eq res t)
4928 (consp c-record-found-types))
4929 ;; Merge in the ranges of any types found by the second
4930 ;; `c-forward-type'.
4931 (setq c-record-type-identifiers
4932 ;; `nconc' doesn't mind that the tail of
4933 ;; `c-record-found-types' is t.
4934 (nconc c-record-found-types
4935 c-record-type-identifiers))))
4936
4937 (goto-char pos))))
4938
4939 (when (and c-record-found-types (memq res '(known found)) id-range)
4940 (setq c-record-found-types
4941 (cons id-range c-record-found-types))))
4942
4943 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
4944
4945 res))
4946
4947 \f
4948 ;; Handling of large scale constructs like statements and declarations.
4949
4950 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
4951 ;; defsubst or perhaps even a defun, but it contains lots of free
4952 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
4953 (defmacro c-fdoc-shift-type-backward (&optional short)
4954 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
4955 ;; of types when parsing a declaration, which means that it
4956 ;; sometimes consumes the identifier in the declaration as a type.
4957 ;; This is used to "backtrack" and make the last type be treated as
4958 ;; an identifier instead.
4959 `(progn
4960 ,(unless short
4961 ;; These identifiers are bound only in the inner let.
4962 '(setq identifier-type at-type
4963 identifier-start type-start
4964 got-parens nil
4965 got-identifier t
4966 got-suffix t
4967 got-suffix-after-parens id-start
4968 paren-depth 0))
4969
4970 (if (setq at-type (if (eq backup-at-type 'prefix)
4971 t
4972 backup-at-type))
4973 (setq type-start backup-type-start
4974 id-start backup-id-start)
4975 (setq type-start start-pos
4976 id-start start-pos))
4977
4978 ;; When these flags already are set we've found specifiers that
4979 ;; unconditionally signal these attributes - backtracking doesn't
4980 ;; change that. So keep them set in that case.
4981 (or at-type-decl
4982 (setq at-type-decl backup-at-type-decl))
4983 (or maybe-typeless
4984 (setq maybe-typeless backup-maybe-typeless))
4985
4986 ,(unless short
4987 ;; This identifier is bound only in the inner let.
4988 '(setq start id-start))))
4989
4990 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
4991 ;; Move forward over a declaration or a cast if at the start of one.
4992 ;; The point is assumed to be at the start of some token. Nil is
4993 ;; returned if no declaration or cast is recognized, and the point
4994 ;; is clobbered in that case.
4995 ;;
4996 ;; If a declaration is parsed:
4997 ;;
4998 ;; The point is left at the first token after the first complete
4999 ;; declarator, if there is one. The return value is a cons where
5000 ;; the car is the position of the first token in the declarator.
5001 ;; Some examples:
5002 ;;
5003 ;; void foo (int a, char *b) stuff ...
5004 ;; car ^ ^ point
5005 ;; float (*a)[], b;
5006 ;; car ^ ^ point
5007 ;; unsigned int a = c_style_initializer, b;
5008 ;; car ^ ^ point
5009 ;; unsigned int a (cplusplus_style_initializer), b;
5010 ;; car ^ ^ point (might change)
5011 ;; class Foo : public Bar {}
5012 ;; car ^ ^ point
5013 ;; class PikeClass (int a, string b) stuff ...
5014 ;; car ^ ^ point
5015 ;; enum bool;
5016 ;; car ^ ^ point
5017 ;; enum bool flag;
5018 ;; car ^ ^ point
5019 ;; void cplusplus_function (int x) throw (Bad);
5020 ;; car ^ ^ point
5021 ;; Foo::Foo (int b) : Base (b) {}
5022 ;; car ^ ^ point
5023 ;;
5024 ;; The cdr of the return value is non-nil iff a
5025 ;; `c-typedef-decl-kwds' specifier is found in the declaration,
5026 ;; i.e. the declared identifier(s) are types.
5027 ;;
5028 ;; If a cast is parsed:
5029 ;;
5030 ;; The point is left at the first token after the closing paren of
5031 ;; the cast. The return value is `cast'. Note that the start
5032 ;; position must be at the first token inside the cast parenthesis
5033 ;; to recognize it.
5034 ;;
5035 ;; PRECEDING-TOKEN-END is the first position after the preceding
5036 ;; token, i.e. on the other side of the syntactic ws from the point.
5037 ;; Use a value less than or equal to (point-min) if the point is at
5038 ;; the first token in (the visible part of) the buffer.
5039 ;;
5040 ;; CONTEXT is a symbol that describes the context at the point:
5041 ;; 'decl In a comma-separatded declaration context (typically
5042 ;; inside a function declaration arglist).
5043 ;; '<> In an angle bracket arglist.
5044 ;; 'arglist Some other type of arglist.
5045 ;; nil Some other context or unknown context.
5046 ;;
5047 ;; LAST-CAST-END is the first token after the closing paren of a
5048 ;; preceding cast, or nil if none is known. If
5049 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
5050 ;; the position after the closest preceding call where a cast was
5051 ;; matched. In that case it's used to discover chains of casts like
5052 ;; "(a) (b) c".
5053 ;;
5054 ;; This function records identifier ranges on
5055 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5056 ;; `c-record-type-identifiers' is non-nil.
5057 ;;
5058 ;; This function might do hidden buffer changes.
5059
5060 (let (;; `start-pos' is used below to point to the start of the
5061 ;; first type, i.e. after any leading specifiers. It might
5062 ;; also point at the beginning of the preceding syntactic
5063 ;; whitespace.
5064 (start-pos (point))
5065 ;; Set to the result of `c-forward-type'.
5066 at-type
5067 ;; The position of the first token in what we currently
5068 ;; believe is the type in the declaration or cast, after any
5069 ;; specifiers and their associated clauses.
5070 type-start
5071 ;; The position of the first token in what we currently
5072 ;; believe is the declarator for the first identifier. Set
5073 ;; when the type is found, and moved forward over any
5074 ;; `c-decl-hangon-kwds' and their associated clauses that
5075 ;; occurs after the type.
5076 id-start
5077 ;; These store `at-type', `type-start' and `id-start' of the
5078 ;; identifier before the one in those variables. The previous
5079 ;; identifier might turn out to be the real type in a
5080 ;; declaration if the last one has to be the declarator in it.
5081 ;; If `backup-at-type' is nil then the other variables have
5082 ;; undefined values.
5083 backup-at-type backup-type-start backup-id-start
5084 ;; Set if we've found a specifier that makes the defined
5085 ;; identifier(s) types.
5086 at-type-decl
5087 ;; Set if we've found a specifier that can start a declaration
5088 ;; where there's no type.
5089 maybe-typeless
5090 ;; If a specifier is found that also can be a type prefix,
5091 ;; these flags are set instead of those above. If we need to
5092 ;; back up an identifier, they are copied to the real flag
5093 ;; variables. Thus they only take effect if we fail to
5094 ;; interpret it as a type.
5095 backup-at-type-decl backup-maybe-typeless
5096 ;; Whether we've found a declaration or a cast. We might know
5097 ;; this before we've found the type in it. It's 'ids if we've
5098 ;; found two consecutive identifiers (usually a sure sign, but
5099 ;; we should allow that in labels too), and t if we've found a
5100 ;; specifier keyword (a 100% sure sign).
5101 at-decl-or-cast
5102 ;; Set when we need to back up to parse this as a declaration
5103 ;; but not as a cast.
5104 backup-if-not-cast
5105 ;; For casts, the return position.
5106 cast-end
5107 ;; Save `c-record-type-identifiers' and
5108 ;; `c-record-ref-identifiers' since ranges are recorded
5109 ;; speculatively and should be thrown away if it turns out
5110 ;; that it isn't a declaration or cast.
5111 (save-rec-type-ids c-record-type-identifiers)
5112 (save-rec-ref-ids c-record-ref-identifiers))
5113
5114 ;; Check for a type. Unknown symbols are treated as possible
5115 ;; types, but they could also be specifiers disguised through
5116 ;; macros like __INLINE__, so we recognize both types and known
5117 ;; specifiers after them too.
5118 (while
5119 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
5120
5121 ;; Look for a specifier keyword clause.
5122 (when (looking-at c-prefix-spec-kwds-re)
5123 (setq kwd-sym (c-keyword-sym (match-string 1)))
5124 (save-excursion
5125 (c-forward-keyword-clause 1)
5126 (setq kwd-clause-end (point))))
5127
5128 (when (setq found-type (c-forward-type))
5129 ;; Found a known or possible type or a prefix of a known type.
5130
5131 (when at-type
5132 ;; Got two identifiers with nothing but whitespace
5133 ;; between them. That can only happen in declarations.
5134 (setq at-decl-or-cast 'ids)
5135
5136 (when (eq at-type 'found)
5137 ;; If the previous identifier is a found type we
5138 ;; record it as a real one; it might be some sort of
5139 ;; alias for a prefix like "unsigned".
5140 (save-excursion
5141 (goto-char type-start)
5142 (let ((c-promote-possible-types t))
5143 (c-forward-type)))))
5144
5145 (setq backup-at-type at-type
5146 backup-type-start type-start
5147 backup-id-start id-start
5148 at-type found-type
5149 type-start start
5150 id-start (point)
5151 ;; The previous ambiguous specifier/type turned out
5152 ;; to be a type since we've parsed another one after
5153 ;; it, so clear these backup flags.
5154 backup-at-type-decl nil
5155 backup-maybe-typeless nil))
5156
5157 (if kwd-sym
5158 (progn
5159 ;; Handle known specifier keywords and
5160 ;; `c-decl-hangon-kwds' which can occur after known
5161 ;; types.
5162
5163 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
5164 ;; It's a hang-on keyword that can occur anywhere.
5165 (progn
5166 (setq at-decl-or-cast t)
5167 (if at-type
5168 ;; Move the identifier start position if
5169 ;; we've passed a type.
5170 (setq id-start kwd-clause-end)
5171 ;; Otherwise treat this as a specifier and
5172 ;; move the fallback position.
5173 (setq start-pos kwd-clause-end))
5174 (goto-char kwd-clause-end))
5175
5176 ;; It's an ordinary specifier so we know that
5177 ;; anything before this can't be the type.
5178 (setq backup-at-type nil
5179 start-pos kwd-clause-end)
5180
5181 (if found-type
5182 ;; It's ambiguous whether this keyword is a
5183 ;; specifier or a type prefix, so set the backup
5184 ;; flags. (It's assumed that `c-forward-type'
5185 ;; moved further than `c-forward-keyword-clause'.)
5186 (progn
5187 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5188 (setq backup-at-type-decl t))
5189 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5190 (setq backup-maybe-typeless t)))
5191
5192 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5193 (setq at-type-decl t))
5194 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5195 (setq maybe-typeless t))
5196
5197 ;; Haven't matched a type so it's an umambiguous
5198 ;; specifier keyword and we know we're in a
5199 ;; declaration.
5200 (setq at-decl-or-cast t)
5201
5202 (goto-char kwd-clause-end))))
5203
5204 ;; If the type isn't known we continue so that we'll jump
5205 ;; over all specifiers and type identifiers. The reason
5206 ;; to do this for a known type prefix is to make things
5207 ;; like "unsigned INT16" work.
5208 (and found-type (not (eq found-type t))))))
5209
5210 (cond
5211 ((eq at-type t)
5212 ;; If a known type was found, we still need to skip over any
5213 ;; hangon keyword clauses after it. Otherwise it has already
5214 ;; been done in the loop above.
5215 (while (looking-at c-decl-hangon-key)
5216 (c-forward-keyword-clause 1))
5217 (setq id-start (point)))
5218
5219 ((eq at-type 'prefix)
5220 ;; A prefix type is itself a primitive type when it's not
5221 ;; followed by another type.
5222 (setq at-type t))
5223
5224 ((not at-type)
5225 ;; Got no type but set things up to continue anyway to handle
5226 ;; the various cases when a declaration doesn't start with a
5227 ;; type.
5228 (setq id-start start-pos))
5229
5230 ((and (eq at-type 'maybe)
5231 (c-major-mode-is 'c++-mode))
5232 ;; If it's C++ then check if the last "type" ends on the form
5233 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
5234 ;; (con|de)structor.
5235 (save-excursion
5236 (let (name end-2 end-1)
5237 (goto-char id-start)
5238 (c-backward-syntactic-ws)
5239 (setq end-2 (point))
5240 (when (and
5241 (c-simple-skip-symbol-backward)
5242 (progn
5243 (setq name
5244 (buffer-substring-no-properties (point) end-2))
5245 ;; Cheating in the handling of syntactic ws below.
5246 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
5247 (progn
5248 (setq end-1 (point))
5249 (c-simple-skip-symbol-backward))
5250 (>= (point) type-start)
5251 (equal (buffer-substring-no-properties (point) end-1)
5252 name))
5253 ;; It is a (con|de)structor name. In that case the
5254 ;; declaration is typeless so zap out any preceding
5255 ;; identifier(s) that we might have taken as types.
5256 (goto-char type-start)
5257 (setq at-type nil
5258 backup-at-type nil
5259 id-start type-start))))))
5260
5261 ;; Check for and step over a type decl expression after the thing
5262 ;; that is or might be a type. This can't be skipped since we
5263 ;; need the correct end position of the declarator for
5264 ;; `max-type-decl-end-*'.
5265 (let ((start (point)) (paren-depth 0) pos
5266 ;; True if there's a non-open-paren match of
5267 ;; `c-type-decl-prefix-key'.
5268 got-prefix
5269 ;; True if the declarator is surrounded by a parenthesis pair.
5270 got-parens
5271 ;; True if there is an identifier in the declarator.
5272 got-identifier
5273 ;; True if there's a non-close-paren match of
5274 ;; `c-type-decl-suffix-key'.
5275 got-suffix
5276 ;; True if there's a prefix match outside the outermost
5277 ;; paren pair that surrounds the declarator.
5278 got-prefix-before-parens
5279 y ;; True if there's a suffix match outside the outermost
5280 ;; paren pair that surrounds the declarator. The value is
5281 ;; the position of the first suffix match.
5282 got-suffix-after-parens
5283 ;; True if we've parsed the type decl to a token that is
5284 ;; known to end declarations in this context.
5285 at-decl-end
5286 ;; The earlier values of `at-type' and `type-start' if we've
5287 ;; shifted the type backwards.
5288 identifier-type identifier-start
5289 ;; If `c-parse-and-markup-<>-arglists' is set we need to
5290 ;; turn it off during the name skipping below to avoid
5291 ;; getting `c-type' properties that might be bogus. That
5292 ;; can happen since we don't know if
5293 ;; `c-restricted-<>-arglists' will be correct inside the
5294 ;; arglist paren that gets entered.
5295 c-parse-and-markup-<>-arglists)
5296
5297 (goto-char id-start)
5298
5299 ;; Skip over type decl prefix operators. (Note similar code in
5300 ;; `c-font-lock-declarators'.)
5301 (while (and (looking-at c-type-decl-prefix-key)
5302 (if (and (c-major-mode-is 'c++-mode)
5303 (match-beginning 2))
5304 ;; If the second submatch matches in C++ then
5305 ;; we're looking at an identifier that's a
5306 ;; prefix only if it specifies a member pointer.
5307 (when (setq got-identifier (c-forward-name))
5308 (if (looking-at "\\(::\\)")
5309 ;; We only check for a trailing "::" and
5310 ;; let the "*" that should follow be
5311 ;; matched in the next round.
5312 (progn (setq got-identifier nil) t)
5313 ;; It turned out to be the real identifier,
5314 ;; so stop.
5315 nil))
5316 t))
5317
5318 (if (eq (char-after) ?\()
5319 (progn
5320 (setq paren-depth (1+ paren-depth))
5321 (forward-char))
5322 (unless got-prefix-before-parens
5323 (setq got-prefix-before-parens (= paren-depth 0)))
5324 (setq got-prefix t)
5325 (goto-char (match-end 1)))
5326 (c-forward-syntactic-ws))
5327
5328 (setq got-parens (> paren-depth 0))
5329
5330 ;; Skip over an identifier.
5331 (or got-identifier
5332 (and (looking-at c-identifier-start)
5333 (setq got-identifier (c-forward-name))))
5334
5335 ;; Skip over type decl suffix operators.
5336 (while (if (looking-at c-type-decl-suffix-key)
5337
5338 (if (eq (char-after) ?\))
5339 (when (> paren-depth 0)
5340 (setq paren-depth (1- paren-depth))
5341 (forward-char)
5342 t)
5343 (when (if (save-match-data (looking-at "\\s\("))
5344 (c-safe (c-forward-sexp 1) t)
5345 (goto-char (match-end 1))
5346 t)
5347 (when (and (not got-suffix-after-parens)
5348 (= paren-depth 0))
5349 (setq got-suffix-after-parens (match-beginning 0)))
5350 (setq got-suffix t)))
5351
5352 ;; No suffix matched. We might have matched the
5353 ;; identifier as a type and the open paren of a
5354 ;; function arglist as a type decl prefix. In that
5355 ;; case we should "backtrack": Reinterpret the last
5356 ;; type as the identifier, move out of the arglist and
5357 ;; continue searching for suffix operators.
5358 ;;
5359 ;; Do this even if there's no preceding type, to cope
5360 ;; with old style function declarations in K&R C,
5361 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
5362 ;; style declarations. That isn't applicable in an
5363 ;; arglist context, though.
5364 (when (and (= paren-depth 1)
5365 (not got-prefix-before-parens)
5366 (not (eq at-type t))
5367 (or backup-at-type
5368 maybe-typeless
5369 backup-maybe-typeless
5370 (when c-recognize-typeless-decls
5371 (not context)))
5372 (setq pos (c-up-list-forward (point)))
5373 (eq (char-before pos) ?\)))
5374 (c-fdoc-shift-type-backward)
5375 (goto-char pos)
5376 t))
5377
5378 (c-forward-syntactic-ws))
5379
5380 (when (and (or maybe-typeless backup-maybe-typeless)
5381 (not got-identifier)
5382 (not got-prefix)
5383 at-type)
5384 ;; Have found no identifier but `c-typeless-decl-kwds' has
5385 ;; matched so we know we're inside a declaration. The
5386 ;; preceding type must be the identifier instead.
5387 (c-fdoc-shift-type-backward))
5388
5389 (setq
5390 at-decl-or-cast
5391 (catch 'at-decl-or-cast
5392
5393 (when (> paren-depth 0)
5394 ;; Encountered something inside parens that isn't matched by
5395 ;; the `c-type-decl-*' regexps, so it's not a type decl
5396 ;; expression. Try to skip out to the same paren depth to
5397 ;; not confuse the cast check below.
5398 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
5399 ;; If we've found a specifier keyword then it's a
5400 ;; declaration regardless.
5401 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
5402
5403 (setq at-decl-end
5404 (looking-at (cond ((eq context '<>) "[,>]")
5405 (context "[,\)]")
5406 (t "[,;]"))))
5407
5408 ;; Now we've collected info about various characteristics of
5409 ;; the construct we're looking at. Below follows a decision
5410 ;; tree based on that. It's ordered to check more certain
5411 ;; signs before less certain ones.
5412
5413 (if got-identifier
5414 (progn
5415
5416 (when (and (or at-type maybe-typeless)
5417 (not (or got-prefix got-parens)))
5418 ;; Got another identifier directly after the type, so it's a
5419 ;; declaration.
5420 (throw 'at-decl-or-cast t))
5421
5422 (when (and got-parens
5423 (not got-prefix)
5424 (not got-suffix-after-parens)
5425 (or backup-at-type
5426 maybe-typeless
5427 backup-maybe-typeless))
5428 ;; Got a declaration of the form "foo bar (gnu);" where we've
5429 ;; recognized "bar" as the type and "gnu" as the declarator.
5430 ;; In this case it's however more likely that "bar" is the
5431 ;; declarator and "gnu" a function argument or initializer (if
5432 ;; `c-recognize-paren-inits' is set), since the parens around
5433 ;; "gnu" would be superfluous if it's a declarator. Shift the
5434 ;; type one step backward.
5435 (c-fdoc-shift-type-backward)))
5436
5437 ;; Found no identifier.
5438
5439 (if backup-at-type
5440 (progn
5441
5442 (when (= (point) start)
5443 ;; Got a plain list of identifiers. If a colon follows it's
5444 ;; a valid label. Otherwise the last one probably is the
5445 ;; declared identifier and we should back up to the previous
5446 ;; type, providing it isn't a cast.
5447 (if (eq (char-after) ?:)
5448 ;; If we've found a specifier keyword then it's a
5449 ;; declaration regardless.
5450 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
5451 (setq backup-if-not-cast t)
5452 (throw 'at-decl-or-cast t)))
5453
5454 (when (and got-suffix
5455 (not got-prefix)
5456 (not got-parens))
5457 ;; Got a plain list of identifiers followed by some suffix.
5458 ;; If this isn't a cast then the last identifier probably is
5459 ;; the declared one and we should back up to the previous
5460 ;; type.
5461 (setq backup-if-not-cast t)
5462 (throw 'at-decl-or-cast t)))
5463
5464 (when (eq at-type t)
5465 ;; If the type is known we know that there can't be any
5466 ;; identifier somewhere else, and it's only in declarations in
5467 ;; e.g. function prototypes and in casts that the identifier may
5468 ;; be left out.
5469 (throw 'at-decl-or-cast t))
5470
5471 (when (= (point) start)
5472 ;; Only got a single identifier (parsed as a type so far).
5473 (if (and
5474 ;; Check that the identifier isn't at the start of an
5475 ;; expression.
5476 at-decl-end
5477 (cond
5478 ((eq context 'decl)
5479 ;; Inside an arglist that contains declarations. If K&R
5480 ;; style declarations and parenthesis style initializers
5481 ;; aren't allowed then the single identifier must be a
5482 ;; type, else we require that it's known or found
5483 ;; (primitive types are handled above).
5484 (or (and (not c-recognize-knr-p)
5485 (not c-recognize-paren-inits))
5486 (memq at-type '(known found))))
5487 ((eq context '<>)
5488 ;; Inside a template arglist. Accept known and found
5489 ;; types; other identifiers could just as well be
5490 ;; constants in C++.
5491 (memq at-type '(known found)))))
5492 (throw 'at-decl-or-cast t)
5493 ;; Can't be a valid declaration or cast, but if we've found a
5494 ;; specifier it can't be anything else either, so treat it as
5495 ;; an invalid/unfinished declaration or cast.
5496 (throw 'at-decl-or-cast at-decl-or-cast))))
5497
5498 (if (and got-parens
5499 (not got-prefix)
5500 (not context)
5501 (not (eq at-type t))
5502 (or backup-at-type
5503 maybe-typeless
5504 backup-maybe-typeless
5505 (when c-recognize-typeless-decls
5506 (or (not got-suffix)
5507 (not (looking-at
5508 c-after-suffixed-type-maybe-decl-key))))))
5509 ;; Got an empty paren pair and a preceding type that probably
5510 ;; really is the identifier. Shift the type backwards to make
5511 ;; the last one the identifier. This is analogous to the
5512 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
5513 ;; above.
5514 ;;
5515 ;; Exception: In addition to the conditions in that
5516 ;; "backtracking" code, do not shift backward if we're not
5517 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
5518 ;; Since there's no preceding type, the shift would mean that
5519 ;; the declaration is typeless. But if the regexp doesn't match
5520 ;; then we will simply fall through in the tests below and not
5521 ;; recognize it at all, so it's better to try it as an abstract
5522 ;; declarator instead.
5523 (c-fdoc-shift-type-backward)
5524
5525 ;; Still no identifier.
5526
5527 (when (and got-prefix (or got-parens got-suffix))
5528 ;; Require `got-prefix' together with either `got-parens' or
5529 ;; `got-suffix' to recognize it as an abstract declarator:
5530 ;; `got-parens' only is probably an empty function call.
5531 ;; `got-suffix' only can build an ordinary expression together
5532 ;; with the preceding identifier which we've taken as a type.
5533 ;; We could actually accept on `got-prefix' only, but that can
5534 ;; easily occur temporarily while writing an expression so we
5535 ;; avoid that case anyway. We could do a better job if we knew
5536 ;; the point when the fontification was invoked.
5537 (throw 'at-decl-or-cast t))
5538
5539 (when (and at-type
5540 (not got-prefix)
5541 (not got-parens)
5542 got-suffix-after-parens
5543 (eq (char-after got-suffix-after-parens) ?\())
5544 ;; Got a type, no declarator but a paren suffix. I.e. it's a
5545 ;; normal function call afterall (or perhaps a C++ style object
5546 ;; instantiation expression).
5547 (throw 'at-decl-or-cast nil))))
5548
5549 (when at-decl-or-cast
5550 ;; By now we've located the type in the declaration that we know
5551 ;; we're in.
5552 (throw 'at-decl-or-cast t))
5553
5554 (when (and got-identifier
5555 (not context)
5556 (looking-at c-after-suffixed-type-decl-key)
5557 (if (and got-parens
5558 (not got-prefix)
5559 (not got-suffix)
5560 (not (eq at-type t)))
5561 ;; Shift the type backward in the case that there's a
5562 ;; single identifier inside parens. That can only
5563 ;; occur in K&R style function declarations so it's
5564 ;; more likely that it really is a function call.
5565 ;; Therefore we only do this after
5566 ;; `c-after-suffixed-type-decl-key' has matched.
5567 (progn (c-fdoc-shift-type-backward) t)
5568 got-suffix-after-parens))
5569 ;; A declaration according to `c-after-suffixed-type-decl-key'.
5570 (throw 'at-decl-or-cast t))
5571
5572 (when (and (or got-prefix (not got-parens))
5573 (memq at-type '(t known)))
5574 ;; It's a declaration if a known type precedes it and it can't be a
5575 ;; function call.
5576 (throw 'at-decl-or-cast t))
5577
5578 ;; If we get here we can't tell if this is a type decl or a normal
5579 ;; expression by looking at it alone. (That's under the assumption
5580 ;; that normal expressions always can look like type decl expressions,
5581 ;; which isn't really true but the cases where it doesn't hold are so
5582 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
5583 ;; the effort to look for them.)
5584
5585 (unless (or at-decl-end (looking-at "=[^=]"))
5586 ;; If this is a declaration it should end here or its initializer(*)
5587 ;; should start here, so check for allowed separation tokens. Note
5588 ;; that this rule doesn't work e.g. with a K&R arglist after a
5589 ;; function header.
5590 ;;
5591 ;; *) Don't check for C++ style initializers using parens
5592 ;; since those already have been matched as suffixes.
5593 ;;
5594 ;; If `at-decl-or-cast' is then we've found some other sign that
5595 ;; it's a declaration or cast, so then it's probably an
5596 ;; invalid/unfinished one.
5597 (throw 'at-decl-or-cast at-decl-or-cast))
5598
5599 ;; Below are tests that only should be applied when we're certain to
5600 ;; not have parsed halfway through an expression.
5601
5602 (when (memq at-type '(t known))
5603 ;; The expression starts with a known type so treat it as a
5604 ;; declaration.
5605 (throw 'at-decl-or-cast t))
5606
5607 (when (and (c-major-mode-is 'c++-mode)
5608 ;; In C++ we check if the identifier is a known type, since
5609 ;; (con|de)structors use the class name as identifier.
5610 ;; We've always shifted over the identifier as a type and
5611 ;; then backed up again in this case.
5612 identifier-type
5613 (or (memq identifier-type '(found known))
5614 (and (eq (char-after identifier-start) ?~)
5615 ;; `at-type' probably won't be 'found for
5616 ;; destructors since the "~" is then part of the
5617 ;; type name being checked against the list of
5618 ;; known types, so do a check without that
5619 ;; operator.
5620 (or (save-excursion
5621 (goto-char (1+ identifier-start))
5622 (c-forward-syntactic-ws)
5623 (c-with-syntax-table
5624 c-identifier-syntax-table
5625 (looking-at c-known-type-key)))
5626 (save-excursion
5627 (goto-char (1+ identifier-start))
5628 ;; We have already parsed the type earlier,
5629 ;; so it'd be possible to cache the end
5630 ;; position instead of redoing it here, but
5631 ;; then we'd need to keep track of another
5632 ;; position everywhere.
5633 (c-check-type (point)
5634 (progn (c-forward-type)
5635 (point))))))))
5636 (throw 'at-decl-or-cast t))
5637
5638 (if got-identifier
5639 (progn
5640 (when (and got-prefix-before-parens
5641 at-type
5642 (or at-decl-end (looking-at "=[^=]"))
5643 (not context)
5644 (not got-suffix))
5645 ;; Got something like "foo * bar;". Since we're not inside an
5646 ;; arglist it would be a meaningless expression because the
5647 ;; result isn't used. We therefore choose to recognize it as
5648 ;; a declaration. Do not allow a suffix since it could then
5649 ;; be a function call.
5650 (throw 'at-decl-or-cast t))
5651
5652 (when (and (or got-suffix-after-parens
5653 (looking-at "=[^=]"))
5654 (eq at-type 'found)
5655 (not (eq context 'arglist)))
5656 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
5657 ;; be an odd expression or it could be a declaration. Treat
5658 ;; it as a declaration if "a" has been used as a type
5659 ;; somewhere else (if it's a known type we won't get here).
5660 (throw 'at-decl-or-cast t)))
5661
5662 (when (and context
5663 (or got-prefix
5664 (and (eq context 'decl)
5665 (not c-recognize-paren-inits)
5666 (or got-parens got-suffix))))
5667 ;; Got a type followed by an abstract declarator. If `got-prefix'
5668 ;; is set it's something like "a *" without anything after it. If
5669 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
5670 ;; or similar, which we accept only if the context rules out
5671 ;; expressions.
5672 (throw 'at-decl-or-cast t)))
5673
5674 ;; If we had a complete symbol table here (which rules out
5675 ;; `c-found-types') we should return t due to the disambiguation rule
5676 ;; (in at least C++) that anything that can be parsed as a declaration
5677 ;; is a declaration. Now we're being more defensive and prefer to
5678 ;; highlight things like "foo (bar);" as a declaration only if we're
5679 ;; inside an arglist that contains declarations.
5680 (eq context 'decl))))
5681
5682 ;; The point is now after the type decl expression.
5683
5684 (cond
5685 ;; Check for a cast.
5686 ((save-excursion
5687 (and
5688 c-cast-parens
5689
5690 ;; Should be the first type/identifier in a cast paren.
5691 (> preceding-token-end (point-min))
5692 (memq (char-before preceding-token-end) c-cast-parens)
5693
5694 ;; The closing paren should follow.
5695 (progn
5696 (c-forward-syntactic-ws)
5697 (looking-at "\\s\)"))
5698
5699 ;; There should be a primary expression after it.
5700 (let (pos)
5701 (forward-char)
5702 (c-forward-syntactic-ws)
5703 (setq cast-end (point))
5704 (and (looking-at c-primary-expr-regexp)
5705 (progn
5706 (setq pos (match-end 0))
5707 (or
5708 ;; Check if the expression begins with a prefix keyword.
5709 (match-beginning 2)
5710 (if (match-beginning 1)
5711 ;; Expression begins with an ambiguous operator. Treat
5712 ;; it as a cast if it's a type decl or if we've
5713 ;; recognized the type somewhere else.
5714 (or at-decl-or-cast
5715 (memq at-type '(t known found)))
5716 ;; Unless it's a keyword, it's the beginning of a primary
5717 ;; expression.
5718 (not (looking-at c-keywords-regexp)))))
5719 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
5720 ;; that it matched a whole one so that we don't e.g. confuse
5721 ;; the operator '-' with '->'. It's ok if it matches further,
5722 ;; though, since it e.g. can match the float '.5' while the
5723 ;; operator regexp only matches '.'.
5724 (or (not (looking-at c-nonsymbol-token-regexp))
5725 (<= (match-end 0) pos))))
5726
5727 ;; There should either be a cast before it or something that isn't an
5728 ;; identifier or close paren.
5729 (> preceding-token-end (point-min))
5730 (progn
5731 (goto-char (1- preceding-token-end))
5732 (or (eq (point) last-cast-end)
5733 (progn
5734 (c-backward-syntactic-ws)
5735 (if (< (skip-syntax-backward "w_") 0)
5736 ;; It's a symbol. Accept it only if it's one of the
5737 ;; keywords that can precede an expression (without
5738 ;; surrounding parens).
5739 (looking-at c-simple-stmt-key)
5740 (and
5741 ;; Check that it isn't a close paren (block close is ok,
5742 ;; though).
5743 (not (memq (char-before) '(?\) ?\])))
5744 ;; Check that it isn't a nonsymbol identifier.
5745 (not (c-on-identifier)))))))))
5746
5747 ;; Handle the cast.
5748 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5749 (let ((c-promote-possible-types t))
5750 (goto-char type-start)
5751 (c-forward-type)))
5752
5753 (goto-char cast-end)
5754 'cast)
5755
5756 (at-decl-or-cast
5757 ;; We're at a declaration. Highlight the type and the following
5758 ;; declarators.
5759
5760 (when backup-if-not-cast
5761 (c-fdoc-shift-type-backward t))
5762
5763 (when (and (eq context 'decl) (looking-at ","))
5764 ;; Make sure to propagate the `c-decl-arg-start' property to
5765 ;; the next argument if it's set in this one, to cope with
5766 ;; interactive refontification.
5767 (c-put-c-type-property (point) 'c-decl-arg-start))
5768
5769 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5770 (let ((c-promote-possible-types t))
5771 (save-excursion
5772 (goto-char type-start)
5773 (c-forward-type))))
5774
5775 (cons id-start at-type-decl))
5776
5777 (t
5778 ;; False alarm. Restore the recorded ranges.
5779 (setq c-record-type-identifiers save-rec-type-ids
5780 c-record-ref-identifiers save-rec-ref-ids)
5781 nil))))
5782
5783 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
5784 ;; Assuming the point is at the beginning of a token, check if it
5785 ;; starts a label and if so move over it and return t, otherwise
5786 ;; don't move and return nil. The end of the label is taken to be
5787 ;; the end of the first submatch in `c-opt-extra-label-key' if it
5788 ;; matched, otherwise it's the colon. The point is directly after
5789 ;; the end on return. The terminating char is marked with
5790 ;; `c-decl-end' to improve recognition of the following declaration
5791 ;; or statement.
5792 ;;
5793 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
5794 ;; label, if any, has been marked up like that.
5795 ;;
5796 ;; If PRECEDING-TOKEN-END is given, it should be the first position
5797 ;; after the preceding token, i.e. on the other side of the
5798 ;; syntactic ws from the point. Use a value less than or equal to
5799 ;; (point-min) if the point is at the first token in (the visible
5800 ;; part of) the buffer.
5801 ;;
5802 ;; The optional LIMIT limits the forward scan for the colon.
5803 ;;
5804 ;; This function records the ranges of the label symbols on
5805 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
5806 ;; non-nil.
5807 ;;
5808 ;; This function might do hidden buffer changes.
5809
5810 (let ((start (point)))
5811 (cond
5812 ((looking-at c-label-kwds-regexp)
5813 (let ((kwd-end (match-end 1)))
5814 ;; Record only the keyword itself for fontification, since in
5815 ;; case labels the following is a constant expression and not
5816 ;; a label.
5817 (when c-record-type-identifiers
5818 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
5819
5820 ;; Find the label end.
5821 (goto-char kwd-end)
5822 (if (and (c-syntactic-re-search-forward
5823 ;; Stop on chars that aren't allowed in expressions,
5824 ;; and on operator chars that would be meaningless
5825 ;; there. FIXME: This doesn't cope with ?: operators.
5826 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
5827 limit t t nil 1)
5828 (match-beginning 2))
5829
5830 (progn
5831 (goto-char (match-beginning 2))
5832 (c-put-c-type-property (1- (point)) 'c-decl-end)
5833 t)
5834
5835 ;; It's an unfinished label. We consider the keyword enough
5836 ;; to recognize it as a label, so that it gets fontified.
5837 ;; Leave the point at the end of it, but don't put any
5838 ;; `c-decl-end' marker.
5839 (goto-char kwd-end)
5840 t)))
5841
5842 ((and c-opt-extra-label-key
5843 (looking-at c-opt-extra-label-key))
5844 ;; For a `c-opt-extra-label-key' match, we record the whole
5845 ;; thing for fontification. That's to get the leading '@' in
5846 ;; Objective-C protection labels fontified.
5847 (goto-char (match-end 1))
5848 (when c-record-type-identifiers
5849 (c-record-ref-id (cons (match-beginning 1) (point))))
5850 (c-put-c-type-property (1- (point)) 'c-decl-end)
5851 t)
5852
5853 ((and c-recognize-colon-labels
5854
5855 ;; A colon label must have something before the colon.
5856 (not (eq (char-after) ?:))
5857
5858 ;; Check that we're not after a token that can't precede a label.
5859 (or
5860 ;; Trivially succeeds when there's no preceding token.
5861 (if preceding-token-end
5862 (<= preceding-token-end (point-min))
5863 (save-excursion
5864 (c-backward-syntactic-ws)
5865 (setq preceding-token-end (point))
5866 (bobp)))
5867
5868 ;; Check if we're after a label, if we're after a closing
5869 ;; paren that belong to statement, and with
5870 ;; `c-label-prefix-re'. It's done in different order
5871 ;; depending on `assume-markup' since the checks have
5872 ;; different expensiveness.
5873 (if assume-markup
5874 (or
5875 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
5876 'c-decl-end)
5877
5878 (save-excursion
5879 (goto-char (1- preceding-token-end))
5880 (c-beginning-of-current-token)
5881 (looking-at c-label-prefix-re))
5882
5883 (and (eq (char-before preceding-token-end) ?\))
5884 (c-after-conditional)))
5885
5886 (or
5887 (save-excursion
5888 (goto-char (1- preceding-token-end))
5889 (c-beginning-of-current-token)
5890 (looking-at c-label-prefix-re))
5891
5892 (cond
5893 ((eq (char-before preceding-token-end) ?\))
5894 (c-after-conditional))
5895
5896 ((eq (char-before preceding-token-end) ?:)
5897 ;; Might be after another label, so check it recursively.
5898 (save-excursion
5899 (goto-char (1- preceding-token-end))
5900 ;; Essentially the same as the
5901 ;; `c-syntactic-re-search-forward' regexp below.
5902 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
5903 (let ((pte (point))
5904 ;; If the caller turned on recording for us,
5905 ;; it shouldn't apply when we check the
5906 ;; preceding label.
5907 c-record-type-identifiers)
5908 (c-forward-syntactic-ws)
5909 (c-forward-label nil pte start))))))))
5910
5911 ;; Check that the next nonsymbol token is ":". Allow '('
5912 ;; for the sake of macro arguments. FIXME: Should build
5913 ;; this regexp from the language constants.
5914 (c-syntactic-re-search-forward
5915 "[[:?;{=*/%&|,<>!@+-]" limit t t)
5916 (eq (char-before) ?:)
5917 (not (eq (char-after) ?:)))
5918
5919 (save-restriction
5920 (narrow-to-region start (point))
5921
5922 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
5923 (catch 'check-label
5924 (goto-char start)
5925 (while (progn
5926 (when (looking-at c-nonlabel-token-key)
5927 (goto-char start)
5928 (throw 'check-label nil))
5929 (and (c-safe (c-forward-sexp)
5930 (c-forward-syntactic-ws)
5931 t)
5932 (not (eobp)))))
5933
5934 ;; Record the identifiers in the label for fontification, unless
5935 ;; it begins with `c-label-kwds' in which case the following
5936 ;; identifiers are part of a (constant) expression that
5937 ;; shouldn't be fontified.
5938 (when (and c-record-type-identifiers
5939 (progn (goto-char start)
5940 (not (looking-at c-label-kwds-regexp))))
5941 (while (c-syntactic-re-search-forward c-symbol-key nil t)
5942 (c-record-ref-id (cons (match-beginning 0)
5943 (match-end 0)))))
5944
5945 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
5946 (goto-char (point-max))
5947 t)))
5948
5949 (t
5950 ;; Not a label.
5951 (goto-char start)
5952 nil))))
5953
5954 (defun c-forward-objc-directive ()
5955 ;; Assuming the point is at the beginning of a token, try to move
5956 ;; forward to the end of the Objective-C directive that starts
5957 ;; there. Return t if a directive was fully recognized, otherwise
5958 ;; the point is moved as far as one could be successfully parsed and
5959 ;; nil is returned.
5960 ;;
5961 ;; This function records identifier ranges on
5962 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5963 ;; `c-record-type-identifiers' is non-nil.
5964 ;;
5965 ;; This function might do hidden buffer changes.
5966
5967 (let ((start (point))
5968 start-char
5969 (c-promote-possible-types t)
5970 ;; Turn off recognition of angle bracket arglists while parsing
5971 ;; types here since the protocol reference list might then be
5972 ;; considered part of the preceding name or superclass-name.
5973 c-recognize-<>-arglists)
5974
5975 (if (or
5976 (when (looking-at
5977 (eval-when-compile
5978 (c-make-keywords-re t
5979 (append (c-lang-const c-protection-kwds objc)
5980 '("@end"))
5981 'objc-mode)))
5982 (goto-char (match-end 1))
5983 t)
5984
5985 (and
5986 (looking-at
5987 (eval-when-compile
5988 (c-make-keywords-re t
5989 '("@interface" "@implementation" "@protocol")
5990 'objc-mode)))
5991
5992 ;; Handle the name of the class itself.
5993 (progn
5994 (c-forward-token-2)
5995 (c-forward-type))
5996
5997 (catch 'break
5998 ;; Look for ": superclass-name" or "( category-name )".
5999 (when (looking-at "[:\(]")
6000 (setq start-char (char-after))
6001 (forward-char)
6002 (c-forward-syntactic-ws)
6003 (unless (c-forward-type) (throw 'break nil))
6004 (when (eq start-char ?\()
6005 (unless (eq (char-after) ?\)) (throw 'break nil))
6006 (forward-char)
6007 (c-forward-syntactic-ws)))
6008
6009 ;; Look for a protocol reference list.
6010 (if (eq (char-after) ?<)
6011 (let ((c-recognize-<>-arglists t)
6012 (c-parse-and-markup-<>-arglists t)
6013 c-restricted-<>-arglists)
6014 (c-forward-<>-arglist t))
6015 t))))
6016
6017 (progn
6018 (c-backward-syntactic-ws)
6019 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
6020 (c-put-c-type-property (1- (point)) 'c-decl-end)
6021 t)
6022
6023 (c-clear-c-type-property start (point) 'c-decl-end)
6024 nil)))
6025
6026 (defun c-beginning-of-inheritance-list (&optional lim)
6027 ;; Go to the first non-whitespace after the colon that starts a
6028 ;; multiple inheritance introduction. Optional LIM is the farthest
6029 ;; back we should search.
6030 ;;
6031 ;; This function might do hidden buffer changes.
6032 (c-with-syntax-table c++-template-syntax-table
6033 (c-backward-token-2 0 t lim)
6034 (while (and (or (looking-at c-symbol-start)
6035 (looking-at "[<,]\\|::"))
6036 (zerop (c-backward-token-2 1 t lim))))))
6037
6038 (defun c-in-method-def-p ()
6039 ;; Return nil if we aren't in a method definition, otherwise the
6040 ;; position of the initial [+-].
6041 ;;
6042 ;; This function might do hidden buffer changes.
6043 (save-excursion
6044 (beginning-of-line)
6045 (and c-opt-method-key
6046 (looking-at c-opt-method-key)
6047 (point))
6048 ))
6049
6050 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
6051 (defun c-in-gcc-asm-p ()
6052 ;; Return non-nil if point is within a gcc \"asm\" block.
6053 ;;
6054 ;; This should be called with point inside an argument list.
6055 ;;
6056 ;; Only one level of enclosing parentheses is considered, so for
6057 ;; instance `nil' is returned when in a function call within an asm
6058 ;; operand.
6059 ;;
6060 ;; This function might do hidden buffer changes.
6061
6062 (and c-opt-asm-stmt-key
6063 (save-excursion
6064 (beginning-of-line)
6065 (backward-up-list 1)
6066 (c-beginning-of-statement-1 (point-min) nil t)
6067 (looking-at c-opt-asm-stmt-key))))
6068
6069 (defun c-at-toplevel-p ()
6070 "Return a determination as to whether point is at the `top-level'.
6071 Being at the top-level means that point is either outside any
6072 enclosing block (such function definition), or only inside a class,
6073 namespace or other block that contains another declaration level.
6074
6075 If point is not at the top-level (e.g. it is inside a method
6076 definition), then nil is returned. Otherwise, if point is at a
6077 top-level not enclosed within a class definition, t is returned.
6078 Otherwise, a 2-vector is returned where the zeroth element is the
6079 buffer position of the start of the class declaration, and the first
6080 element is the buffer position of the enclosing class's opening
6081 brace.
6082
6083 Note that this function might do hidden buffer changes. See the
6084 comment at the start of cc-engine.el for more info."
6085 (let ((paren-state (c-parse-state)))
6086 (or (not (c-most-enclosing-brace paren-state))
6087 (c-search-uplist-for-classkey paren-state))))
6088
6089 (defun c-just-after-func-arglist-p (&optional lim)
6090 ;; Return non-nil if the point is in the region after the argument
6091 ;; list of a function and its opening brace (or semicolon in case it
6092 ;; got no body). If there are K&R style argument declarations in
6093 ;; that region, the point has to be inside the first one for this
6094 ;; function to recognize it.
6095 ;;
6096 ;; If successful, the point is moved to the first token after the
6097 ;; function header (see `c-forward-decl-or-cast-1' for details) and
6098 ;; the position of the opening paren of the function arglist is
6099 ;; returned.
6100 ;;
6101 ;; The point is clobbered if not successful.
6102 ;;
6103 ;; LIM is used as bound for backward buffer searches.
6104 ;;
6105 ;; This function might do hidden buffer changes.
6106
6107 (let ((beg (point)) end id-start)
6108 (and
6109 (eq (c-beginning-of-statement-1 lim) 'same)
6110
6111 (not (or (c-major-mode-is 'objc-mode)
6112 (c-forward-objc-directive)))
6113
6114 (setq id-start
6115 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
6116 (< id-start beg)
6117
6118 ;; There should not be a '=' or ',' between beg and the
6119 ;; start of the declaration since that means we were in the
6120 ;; "expression part" of the declaration.
6121 (or (> (point) beg)
6122 (not (looking-at "[=,]")))
6123
6124 (save-excursion
6125 ;; Check that there's an arglist paren in the
6126 ;; declaration.
6127 (goto-char id-start)
6128 (cond ((eq (char-after) ?\()
6129 ;; The declarator is a paren expression, so skip past it
6130 ;; so that we don't get stuck on that instead of the
6131 ;; function arglist.
6132 (c-forward-sexp))
6133 ((and c-opt-op-identitier-prefix
6134 (looking-at c-opt-op-identitier-prefix))
6135 ;; Don't trip up on "operator ()".
6136 (c-forward-token-2 2 t)))
6137 (and (< (point) beg)
6138 (c-syntactic-re-search-forward "(" beg t t)
6139 (1- (point)))))))
6140
6141 (defun c-in-knr-argdecl (&optional lim)
6142 ;; Return the position of the first argument declaration if point is
6143 ;; inside a K&R style argument declaration list, nil otherwise.
6144 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
6145 ;; position that bounds the backward search for the argument list.
6146 ;;
6147 ;; Note: A declaration level context is assumed; the test can return
6148 ;; false positives for statements.
6149 ;;
6150 ;; This function might do hidden buffer changes.
6151
6152 (save-excursion
6153 (save-restriction
6154
6155 ;; Go back to the closest preceding normal parenthesis sexp. We
6156 ;; take that as the argument list in the function header. Then
6157 ;; check that it's followed by some symbol before the next ';'
6158 ;; or '{'. If it does, it's the header of the K&R argdecl we're
6159 ;; in.
6160 (if lim (narrow-to-region lim (c-point 'eol)))
6161 (let ((outside-macro (not (c-query-macro-start)))
6162 paren-end)
6163
6164 (catch 'done
6165 (while (if (and (setq paren-end (c-down-list-backward (point)))
6166 (eq (char-after paren-end) ?\)))
6167 (progn
6168 (goto-char (1+ paren-end))
6169 (if outside-macro
6170 (c-beginning-of-macro)))
6171 (throw 'done nil))))
6172
6173 (and (progn
6174 (c-forward-syntactic-ws)
6175 (looking-at "\\w\\|\\s_"))
6176
6177 (save-excursion
6178 ;; The function header in a K&R declaration should only
6179 ;; contain identifiers separated by comma. It should
6180 ;; also contain at least one identifier since there
6181 ;; wouldn't be anything to declare in the K&R region
6182 ;; otherwise.
6183 (when (c-go-up-list-backward paren-end)
6184 (forward-char)
6185 (catch 'knr-ok
6186 (while t
6187 (c-forward-syntactic-ws)
6188 (if (or (looking-at c-known-type-key)
6189 (looking-at c-keywords-regexp))
6190 (throw 'knr-ok nil))
6191 (c-forward-token-2)
6192 (if (eq (char-after) ?,)
6193 (forward-char)
6194 (throw 'knr-ok (and (eq (char-after) ?\))
6195 (= (point) paren-end))))))))
6196
6197 (save-excursion
6198 ;; If it's a K&R declaration then we're now at the
6199 ;; beginning of the function arglist. Check that there
6200 ;; isn't a '=' before it in this statement since that
6201 ;; means it some kind of initialization instead.
6202 (c-syntactic-skip-backward "^;=}{")
6203 (not (eq (char-before) ?=)))
6204
6205 (point))))))
6206
6207 (defun c-skip-conditional ()
6208 ;; skip forward over conditional at point, including any predicate
6209 ;; statements in parentheses. No error checking is performed.
6210 ;;
6211 ;; This function might do hidden buffer changes.
6212 (c-forward-sexp (cond
6213 ;; else if()
6214 ((looking-at (concat "\\<else"
6215 "\\([ \t\n]\\|\\\\\n\\)+"
6216 "if\\>\\([^_]\\|$\\)"))
6217 3)
6218 ;; do, else, try, finally
6219 ((looking-at (concat "\\<\\("
6220 "do\\|else\\|try\\|finally"
6221 "\\)\\>\\([^_]\\|$\\)"))
6222 1)
6223 ;; for, if, while, switch, catch, synchronized, foreach
6224 (t 2))))
6225
6226 (defun c-after-conditional (&optional lim)
6227 ;; If looking at the token after a conditional then return the
6228 ;; position of its start, otherwise return nil.
6229 ;;
6230 ;; This function might do hidden buffer changes.
6231 (save-excursion
6232 (and (zerop (c-backward-token-2 1 t lim))
6233 (or (looking-at c-block-stmt-1-key)
6234 (and (eq (char-after) ?\()
6235 (zerop (c-backward-token-2 1 t lim))
6236 (looking-at c-block-stmt-2-key)))
6237 (point))))
6238
6239 (defun c-after-special-operator-id (&optional lim)
6240 ;; If the point is after an operator identifier that isn't handled
6241 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
6242 ;; position of the start of that identifier is returned. nil is
6243 ;; returned otherwise. The point may be anywhere in the syntactic
6244 ;; whitespace after the last token of the operator identifier.
6245 ;;
6246 ;; This function might do hidden buffer changes.
6247 (save-excursion
6248 (and c-overloadable-operators-regexp
6249 (zerop (c-backward-token-2 1 nil lim))
6250 (looking-at c-overloadable-operators-regexp)
6251 (or (not c-opt-op-identitier-prefix)
6252 (and
6253 (zerop (c-backward-token-2 1 nil lim))
6254 (looking-at c-opt-op-identitier-prefix)))
6255 (point))))
6256
6257 (defsubst c-backward-to-block-anchor (&optional lim)
6258 ;; Assuming point is at a brace that opens a statement block of some
6259 ;; kind, move to the proper anchor point for that block. It might
6260 ;; need to be adjusted further by c-add-stmt-syntax, but the
6261 ;; position at return is suitable as start position for that
6262 ;; function.
6263 ;;
6264 ;; This function might do hidden buffer changes.
6265 (unless (= (point) (c-point 'boi))
6266 (let ((start (c-after-conditional lim)))
6267 (if start
6268 (goto-char start)))))
6269
6270 (defsubst c-backward-to-decl-anchor (&optional lim)
6271 ;; Assuming point is at a brace that opens the block of a top level
6272 ;; declaration of some kind, move to the proper anchor point for
6273 ;; that block.
6274 ;;
6275 ;; This function might do hidden buffer changes.
6276 (unless (= (point) (c-point 'boi))
6277 (c-beginning-of-statement-1 lim)))
6278
6279 (defun c-search-decl-header-end ()
6280 ;; Search forward for the end of the "header" of the current
6281 ;; declaration. That's the position where the definition body
6282 ;; starts, or the first variable initializer, or the ending
6283 ;; semicolon. I.e. search forward for the closest following
6284 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
6285 ;; _after_ the first found token, or at point-max if none is found.
6286 ;;
6287 ;; This function might do hidden buffer changes.
6288
6289 (let ((base (point)))
6290 (if (c-major-mode-is 'c++-mode)
6291
6292 ;; In C++ we need to take special care to handle operator
6293 ;; tokens and those pesky template brackets.
6294 (while (and
6295 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
6296 (or
6297 (c-end-of-current-token base)
6298 ;; Handle operator identifiers, i.e. ignore any
6299 ;; operator token preceded by "operator".
6300 (save-excursion
6301 (and (c-safe (c-backward-sexp) t)
6302 (looking-at c-opt-op-identitier-prefix)))
6303 (and (eq (char-before) ?<)
6304 (c-with-syntax-table c++-template-syntax-table
6305 (if (c-safe (goto-char (c-up-list-forward (point))))
6306 t
6307 (goto-char (point-max))
6308 nil)))))
6309 (setq base (point)))
6310
6311 (while (and
6312 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
6313 (c-end-of-current-token base))
6314 (setq base (point))))))
6315
6316 (defun c-beginning-of-decl-1 (&optional lim)
6317 ;; Go to the beginning of the current declaration, or the beginning
6318 ;; of the previous one if already at the start of it. Point won't
6319 ;; be moved out of any surrounding paren. Return a cons cell of the
6320 ;; form (MOVE . KNR-POS). MOVE is like the return value from
6321 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
6322 ;; style argument declarations (and they are to be recognized) then
6323 ;; KNR-POS is set to the start of the first such argument
6324 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
6325 ;; position that bounds the backward search.
6326 ;;
6327 ;; NB: Cases where the declaration continues after the block, as in
6328 ;; "struct foo { ... } bar;", are currently recognized as two
6329 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
6330 ;;
6331 ;; This function might do hidden buffer changes.
6332 (catch 'return
6333 (let* ((start (point))
6334 (last-stmt-start (point))
6335 (move (c-beginning-of-statement-1 lim nil t)))
6336
6337 ;; `c-beginning-of-statement-1' stops at a block start, but we
6338 ;; want to continue if the block doesn't begin a top level
6339 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
6340 ;; or an open paren.
6341 (let ((beg (point)) tentative-move)
6342 (while (and
6343 ;; Must check with c-opt-method-key in ObjC mode.
6344 (not (and c-opt-method-key
6345 (looking-at c-opt-method-key)))
6346 (/= last-stmt-start (point))
6347 (progn
6348 (c-backward-syntactic-ws lim)
6349 (not (memq (char-before) '(?\; ?} ?: nil))))
6350 (save-excursion
6351 (backward-char)
6352 (not (looking-at "\\s(")))
6353 ;; Check that we don't move from the first thing in a
6354 ;; macro to its header.
6355 (not (eq (setq tentative-move
6356 (c-beginning-of-statement-1 lim nil t))
6357 'macro)))
6358 (setq last-stmt-start beg
6359 beg (point)
6360 move tentative-move))
6361 (goto-char beg))
6362
6363 (when c-recognize-knr-p
6364 (let ((fallback-pos (point)) knr-argdecl-start)
6365 ;; Handle K&R argdecls. Back up after the "statement" jumped
6366 ;; over by `c-beginning-of-statement-1', unless it was the
6367 ;; function body, in which case we're sitting on the opening
6368 ;; brace now. Then test if we're in a K&R argdecl region and
6369 ;; that we started at the other side of the first argdecl in
6370 ;; it.
6371 (unless (eq (char-after) ?{)
6372 (goto-char last-stmt-start))
6373 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
6374 (< knr-argdecl-start start)
6375 (progn
6376 (goto-char knr-argdecl-start)
6377 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
6378 (throw 'return
6379 (cons (if (eq (char-after fallback-pos) ?{)
6380 'previous
6381 'same)
6382 knr-argdecl-start))
6383 (goto-char fallback-pos))))
6384
6385 ;; `c-beginning-of-statement-1' counts each brace block as a
6386 ;; separate statement, so the result will be 'previous if we've
6387 ;; moved over any. If they were brace list initializers we might
6388 ;; not have moved over a declaration boundary though, so change it
6389 ;; to 'same if we've moved past a '=' before '{', but not ';'.
6390 ;; (This ought to be integrated into `c-beginning-of-statement-1',
6391 ;; so we avoid this extra pass which potentially can search over a
6392 ;; large amount of text.)
6393 (if (and (eq move 'previous)
6394 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
6395 c++-template-syntax-table
6396 (syntax-table))
6397 (save-excursion
6398 (and (c-syntactic-re-search-forward "[;={]" start t t t)
6399 (eq (char-before) ?=)
6400 (c-syntactic-re-search-forward "[;{]" start t t)
6401 (eq (char-before) ?{)
6402 (c-safe (goto-char (c-up-list-forward (point))) t)
6403 (not (c-syntactic-re-search-forward ";" start t t))))))
6404 (cons 'same nil)
6405 (cons move nil)))))
6406
6407 (defun c-end-of-decl-1 ()
6408 ;; Assuming point is at the start of a declaration (as detected by
6409 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
6410 ;; `c-beginning-of-decl-1', this function handles the case when a
6411 ;; block is followed by identifiers in e.g. struct declarations in C
6412 ;; or C++. If a proper end was found then t is returned, otherwise
6413 ;; point is moved as far as possible within the current sexp and nil
6414 ;; is returned. This function doesn't handle macros; use
6415 ;; `c-end-of-macro' instead in those cases.
6416 ;;
6417 ;; This function might do hidden buffer changes.
6418 (let ((start (point))
6419 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
6420 c++-template-syntax-table
6421 (syntax-table))))
6422 (catch 'return
6423 (c-search-decl-header-end)
6424
6425 (when (and c-recognize-knr-p
6426 (eq (char-before) ?\;)
6427 (c-in-knr-argdecl start))
6428 ;; Stopped at the ';' in a K&R argdecl section which is
6429 ;; detected using the same criteria as in
6430 ;; `c-beginning-of-decl-1'. Move to the following block
6431 ;; start.
6432 (c-syntactic-re-search-forward "{" nil 'move t))
6433
6434 (when (eq (char-before) ?{)
6435 ;; Encountered a block in the declaration. Jump over it.
6436 (condition-case nil
6437 (goto-char (c-up-list-forward (point)))
6438 (error (goto-char (point-max))
6439 (throw 'return nil)))
6440 (if (or (not c-opt-block-decls-with-vars-key)
6441 (save-excursion
6442 (c-with-syntax-table decl-syntax-table
6443 (let ((lim (point)))
6444 (goto-char start)
6445 (not (and
6446 ;; Check for `c-opt-block-decls-with-vars-key'
6447 ;; before the first paren.
6448 (c-syntactic-re-search-forward
6449 (concat "[;=\(\[{]\\|\\("
6450 c-opt-block-decls-with-vars-key
6451 "\\)")
6452 lim t t t)
6453 (match-beginning 1)
6454 (not (eq (char-before) ?_))
6455 ;; Check that the first following paren is
6456 ;; the block.
6457 (c-syntactic-re-search-forward "[;=\(\[{]"
6458 lim t t t)
6459 (eq (char-before) ?{)))))))
6460 ;; The declaration doesn't have any of the
6461 ;; `c-opt-block-decls-with-vars' keywords in the
6462 ;; beginning, so it ends here at the end of the block.
6463 (throw 'return t)))
6464
6465 (c-with-syntax-table decl-syntax-table
6466 (while (progn
6467 (if (eq (char-before) ?\;)
6468 (throw 'return t))
6469 (c-syntactic-re-search-forward ";" nil 'move t))))
6470 nil)))
6471
6472 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
6473 ;; Assuming the point is at an open brace, check if it starts a
6474 ;; block that contains another declaration level, i.e. that isn't a
6475 ;; statement block or a brace list, and if so return non-nil.
6476 ;;
6477 ;; If the check is successful, the return value is the start of the
6478 ;; keyword that tells what kind of construct it is, i.e. typically
6479 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
6480 ;; the point will be at the start of the construct, before any
6481 ;; leading specifiers, otherwise it's at the returned position.
6482 ;;
6483 ;; The point is clobbered if the check is unsuccessful.
6484 ;;
6485 ;; CONTAINING-SEXP is the position of the open of the surrounding
6486 ;; paren, or nil if none.
6487 ;;
6488 ;; The optional LIMIT limits the backward search for the start of
6489 ;; the construct. It's assumed to be at a syntactically relevant
6490 ;; position.
6491 ;;
6492 ;; If any template arglists are found in the searched region before
6493 ;; the open brace, they get marked with paren syntax.
6494 ;;
6495 ;; This function might do hidden buffer changes.
6496
6497 (let ((open-brace (point)) kwd-start first-specifier-pos)
6498 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6499
6500 (when (and c-recognize-<>-arglists
6501 (eq (char-before) ?>))
6502 ;; Could be at the end of a template arglist.
6503 (let ((c-parse-and-markup-<>-arglists t)
6504 (c-disallow-comma-in-<>-arglists
6505 (and containing-sexp
6506 (not (eq (char-after containing-sexp) ?{)))))
6507 (while (and
6508 (c-backward-<>-arglist nil limit)
6509 (progn
6510 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6511 (eq (char-before) ?>))))))
6512
6513 ;; Note: Can't get bogus hits inside template arglists below since they
6514 ;; have gotten paren syntax above.
6515 (when (and
6516 ;; If `goto-start' is set we begin by searching for the
6517 ;; first possible position of a leading specifier list.
6518 ;; The `c-decl-block-key' search continues from there since
6519 ;; we know it can't match earlier.
6520 (if goto-start
6521 (when (c-syntactic-re-search-forward c-symbol-start
6522 open-brace t t)
6523 (goto-char (setq first-specifier-pos (match-beginning 0)))
6524 t)
6525 t)
6526
6527 (cond
6528 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
6529 (goto-char (setq kwd-start (match-beginning 0)))
6530 (or
6531
6532 ;; Found a keyword that can't be a type?
6533 (match-beginning 1)
6534
6535 ;; Can be a type too, in which case it's the return type of a
6536 ;; function (under the assumption that no declaration level
6537 ;; block construct starts with a type).
6538 (not (c-forward-type))
6539
6540 ;; Jumped over a type, but it could be a declaration keyword
6541 ;; followed by the declared identifier that we've jumped over
6542 ;; instead (e.g. in "class Foo {"). If it indeed is a type
6543 ;; then we should be at the declarator now, so check for a
6544 ;; valid declarator start.
6545 ;;
6546 ;; Note: This doesn't cope with the case when a declared
6547 ;; identifier is followed by e.g. '(' in a language where '('
6548 ;; also might be part of a declarator expression. Currently
6549 ;; there's no such language.
6550 (not (or (looking-at c-symbol-start)
6551 (looking-at c-type-decl-prefix-key)))))
6552
6553 ;; In Pike a list of modifiers may be followed by a brace
6554 ;; to make them apply to many identifiers. Note that the
6555 ;; match data will be empty on return in this case.
6556 ((and (c-major-mode-is 'pike-mode)
6557 (progn
6558 (goto-char open-brace)
6559 (= (c-backward-token-2) 0))
6560 (looking-at c-specifier-key)
6561 ;; Use this variant to avoid yet another special regexp.
6562 (c-keyword-member (c-keyword-sym (match-string 1))
6563 'c-modifier-kwds))
6564 (setq kwd-start (point))
6565 t)))
6566
6567 ;; Got a match.
6568
6569 (if goto-start
6570 ;; Back up over any preceding specifiers and their clauses
6571 ;; by going forward from `first-specifier-pos', which is the
6572 ;; earliest possible position where the specifier list can
6573 ;; start.
6574 (progn
6575 (goto-char first-specifier-pos)
6576
6577 (while (< (point) kwd-start)
6578 (if (looking-at c-symbol-key)
6579 ;; Accept any plain symbol token on the ground that
6580 ;; it's a specifier masked through a macro (just
6581 ;; like `c-forward-decl-or-cast-1' skip forward over
6582 ;; such tokens).
6583 ;;
6584 ;; Could be more restrictive wrt invalid keywords,
6585 ;; but that'd only occur in invalid code so there's
6586 ;; no use spending effort on it.
6587 (let ((end (match-end 0)))
6588 (unless (c-forward-keyword-clause 0)
6589 (goto-char end)
6590 (c-forward-syntactic-ws)))
6591
6592 ;; Can't parse a declaration preamble and is still
6593 ;; before `kwd-start'. That means `first-specifier-pos'
6594 ;; was in some earlier construct. Search again.
6595 (if (c-syntactic-re-search-forward c-symbol-start
6596 kwd-start 'move t)
6597 (goto-char (setq first-specifier-pos (match-beginning 0)))
6598 ;; Got no preamble before the block declaration keyword.
6599 (setq first-specifier-pos kwd-start))))
6600
6601 (goto-char first-specifier-pos))
6602 (goto-char kwd-start))
6603
6604 kwd-start)))
6605
6606 (defun c-search-uplist-for-classkey (paren-state)
6607 ;; Check if the closest containing paren sexp is a declaration
6608 ;; block, returning a 2 element vector in that case. Aref 0
6609 ;; contains the bufpos at boi of the class key line, and aref 1
6610 ;; contains the bufpos of the open brace. This function is an
6611 ;; obsolete wrapper for `c-looking-at-decl-block'.
6612 ;;
6613 ;; This function might do hidden buffer changes.
6614 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
6615 (when open-paren-pos
6616 (save-excursion
6617 (goto-char open-paren-pos)
6618 (when (and (eq (char-after) ?{)
6619 (c-looking-at-decl-block
6620 (c-safe-position open-paren-pos paren-state)
6621 nil))
6622 (back-to-indentation)
6623 (vector (point) open-paren-pos))))))
6624
6625 (defun c-inside-bracelist-p (containing-sexp paren-state)
6626 ;; return the buffer position of the beginning of the brace list
6627 ;; statement if we're inside a brace list, otherwise return nil.
6628 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
6629 ;; paren. PAREN-STATE is the remainder of the state of enclosing
6630 ;; braces
6631 ;;
6632 ;; N.B.: This algorithm can potentially get confused by cpp macros
6633 ;; placed in inconvenient locations. It's a trade-off we make for
6634 ;; speed.
6635 ;;
6636 ;; This function might do hidden buffer changes.
6637 (or
6638 ;; This will pick up brace list declarations.
6639 (c-safe
6640 (save-excursion
6641 (goto-char containing-sexp)
6642 (c-forward-sexp -1)
6643 (let (bracepos)
6644 (if (and (or (looking-at c-brace-list-key)
6645 (progn (c-forward-sexp -1)
6646 (looking-at c-brace-list-key)))
6647 (setq bracepos (c-down-list-forward (point)))
6648 (not (c-crosses-statement-barrier-p (point)
6649 (- bracepos 2))))
6650 (point)))))
6651 ;; this will pick up array/aggregate init lists, even if they are nested.
6652 (save-excursion
6653 (let ((class-key
6654 ;; Pike can have class definitions anywhere, so we must
6655 ;; check for the class key here.
6656 (and (c-major-mode-is 'pike-mode)
6657 c-decl-block-key))
6658 bufpos braceassignp lim next-containing)
6659 (while (and (not bufpos)
6660 containing-sexp)
6661 (when paren-state
6662 (if (consp (car paren-state))
6663 (setq lim (cdr (car paren-state))
6664 paren-state (cdr paren-state))
6665 (setq lim (car paren-state)))
6666 (when paren-state
6667 (setq next-containing (car paren-state)
6668 paren-state (cdr paren-state))))
6669 (goto-char containing-sexp)
6670 (if (c-looking-at-inexpr-block next-containing next-containing)
6671 ;; We're in an in-expression block of some kind. Do not
6672 ;; check nesting. We deliberately set the limit to the
6673 ;; containing sexp, so that c-looking-at-inexpr-block
6674 ;; doesn't check for an identifier before it.
6675 (setq containing-sexp nil)
6676 ;; see if the open brace is preceded by = or [...] in
6677 ;; this statement, but watch out for operator=
6678 (setq braceassignp 'dontknow)
6679 (c-backward-token-2 1 t lim)
6680 ;; Checks to do only on the first sexp before the brace.
6681 (when (and c-opt-inexpr-brace-list-key
6682 (eq (char-after) ?\[))
6683 ;; In Java, an initialization brace list may follow
6684 ;; directly after "new Foo[]", so check for a "new"
6685 ;; earlier.
6686 (while (eq braceassignp 'dontknow)
6687 (setq braceassignp
6688 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
6689 ((looking-at c-opt-inexpr-brace-list-key) t)
6690 ((looking-at "\\sw\\|\\s_\\|[.[]")
6691 ;; Carry on looking if this is an
6692 ;; identifier (may contain "." in Java)
6693 ;; or another "[]" sexp.
6694 'dontknow)
6695 (t nil)))))
6696 ;; Checks to do on all sexps before the brace, up to the
6697 ;; beginning of the statement.
6698 (while (eq braceassignp 'dontknow)
6699 (cond ((eq (char-after) ?\;)
6700 (setq braceassignp nil))
6701 ((and class-key
6702 (looking-at class-key))
6703 (setq braceassignp nil))
6704 ((eq (char-after) ?=)
6705 ;; We've seen a =, but must check earlier tokens so
6706 ;; that it isn't something that should be ignored.
6707 (setq braceassignp 'maybe)
6708 (while (and (eq braceassignp 'maybe)
6709 (zerop (c-backward-token-2 1 t lim)))
6710 (setq braceassignp
6711 (cond
6712 ;; Check for operator =
6713 ((and c-opt-op-identitier-prefix
6714 (looking-at c-opt-op-identitier-prefix))
6715 nil)
6716 ;; Check for `<opchar>= in Pike.
6717 ((and (c-major-mode-is 'pike-mode)
6718 (or (eq (char-after) ?`)
6719 ;; Special case for Pikes
6720 ;; `[]=, since '[' is not in
6721 ;; the punctuation class.
6722 (and (eq (char-after) ?\[)
6723 (eq (char-before) ?`))))
6724 nil)
6725 ((looking-at "\\s.") 'maybe)
6726 ;; make sure we're not in a C++ template
6727 ;; argument assignment
6728 ((and
6729 (c-major-mode-is 'c++-mode)
6730 (save-excursion
6731 (let ((here (point))
6732 (pos< (progn
6733 (skip-chars-backward "^<>")
6734 (point))))
6735 (and (eq (char-before) ?<)
6736 (not (c-crosses-statement-barrier-p
6737 pos< here))
6738 (not (c-in-literal))
6739 ))))
6740 nil)
6741 (t t))))))
6742 (if (and (eq braceassignp 'dontknow)
6743 (/= (c-backward-token-2 1 t lim) 0))
6744 (setq braceassignp nil)))
6745 (if (not braceassignp)
6746 (if (eq (char-after) ?\;)
6747 ;; Brace lists can't contain a semicolon, so we're done.
6748 (setq containing-sexp nil)
6749 ;; Go up one level.
6750 (setq containing-sexp next-containing
6751 lim nil
6752 next-containing nil))
6753 ;; we've hit the beginning of the aggregate list
6754 (c-beginning-of-statement-1
6755 (c-most-enclosing-brace paren-state))
6756 (setq bufpos (point))))
6757 )
6758 bufpos))
6759 ))
6760
6761 (defun c-looking-at-special-brace-list (&optional lim)
6762 ;; If we're looking at the start of a pike-style list, ie `({ })',
6763 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
6764 ;; positions and its entry in c-special-brace-lists is returned, nil
6765 ;; otherwise. The ending position is nil if the list is still open.
6766 ;; LIM is the limit for forward search. The point may either be at
6767 ;; the `(' or at the following paren character. Tries to check the
6768 ;; matching closer, but assumes it's correct if no balanced paren is
6769 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
6770 ;; a special brace list).
6771 ;;
6772 ;; This function might do hidden buffer changes.
6773 (if c-special-brace-lists
6774 (condition-case ()
6775 (save-excursion
6776 (let ((beg (point))
6777 inner-beg end type)
6778 (c-forward-syntactic-ws)
6779 (if (eq (char-after) ?\()
6780 (progn
6781 (forward-char 1)
6782 (c-forward-syntactic-ws)
6783 (setq inner-beg (point))
6784 (setq type (assq (char-after) c-special-brace-lists)))
6785 (if (setq type (assq (char-after) c-special-brace-lists))
6786 (progn
6787 (setq inner-beg (point))
6788 (c-backward-syntactic-ws)
6789 (forward-char -1)
6790 (setq beg (if (eq (char-after) ?\()
6791 (point)
6792 nil)))))
6793 (if (and beg type)
6794 (if (and (c-safe
6795 (goto-char beg)
6796 (c-forward-sexp 1)
6797 (setq end (point))
6798 (= (char-before) ?\)))
6799 (c-safe
6800 (goto-char inner-beg)
6801 (if (looking-at "\\s(")
6802 ;; Check balancing of the inner paren
6803 ;; below.
6804 (progn
6805 (c-forward-sexp 1)
6806 t)
6807 ;; If the inner char isn't a paren then
6808 ;; we can't check balancing, so just
6809 ;; check the char before the outer
6810 ;; closing paren.
6811 (goto-char end)
6812 (backward-char)
6813 (c-backward-syntactic-ws)
6814 (= (char-before) (cdr type)))))
6815 (if (or (/= (char-syntax (char-before)) ?\))
6816 (= (progn
6817 (c-forward-syntactic-ws)
6818 (point))
6819 (1- end)))
6820 (cons (cons beg end) type))
6821 (cons (list beg) type)))))
6822 (error nil))))
6823
6824 (defun c-looking-at-bos (&optional lim)
6825 ;; Return non-nil if between two statements or declarations, assuming
6826 ;; point is not inside a literal or comment.
6827 ;;
6828 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
6829 ;; are recommended instead.
6830 ;;
6831 ;; This function might do hidden buffer changes.
6832 (c-at-statement-start-p))
6833 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p)
6834
6835 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
6836 ;; Return non-nil if we're looking at the beginning of a block
6837 ;; inside an expression. The value returned is actually a cons of
6838 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
6839 ;; position of the beginning of the construct.
6840 ;;
6841 ;; LIM limits the backward search. CONTAINING-SEXP is the start
6842 ;; position of the closest containing list. If it's nil, the
6843 ;; containing paren isn't used to decide whether we're inside an
6844 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
6845 ;; needs to be farther back.
6846 ;;
6847 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
6848 ;; brace block might be done. It should only be used when the
6849 ;; construct can be assumed to be complete, i.e. when the original
6850 ;; starting position was further down than that.
6851 ;;
6852 ;; This function might do hidden buffer changes.
6853
6854 (save-excursion
6855 (let ((res 'maybe) passed-paren
6856 (closest-lim (or containing-sexp lim (point-min)))
6857 ;; Look at the character after point only as a last resort
6858 ;; when we can't disambiguate.
6859 (block-follows (and (eq (char-after) ?{) (point))))
6860
6861 (while (and (eq res 'maybe)
6862 (progn (c-backward-syntactic-ws)
6863 (> (point) closest-lim))
6864 (not (bobp))
6865 (progn (backward-char)
6866 (looking-at "[\]\).]\\|\\w\\|\\s_"))
6867 (c-safe (forward-char)
6868 (goto-char (scan-sexps (point) -1))))
6869
6870 (setq res
6871 (if (looking-at c-keywords-regexp)
6872 (let ((kw-sym (c-keyword-sym (match-string 1))))
6873 (cond
6874 ((and block-follows
6875 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
6876 (and (not (eq passed-paren ?\[))
6877 (or (not (looking-at c-class-key))
6878 ;; If the class definition is at the start of
6879 ;; a statement, we don't consider it an
6880 ;; in-expression class.
6881 (let ((prev (point)))
6882 (while (and
6883 (= (c-backward-token-2 1 nil closest-lim) 0)
6884 (eq (char-syntax (char-after)) ?w))
6885 (setq prev (point)))
6886 (goto-char prev)
6887 (not (c-at-statement-start-p)))
6888 ;; Also, in Pike we treat it as an
6889 ;; in-expression class if it's used in an
6890 ;; object clone expression.
6891 (save-excursion
6892 (and check-at-end
6893 (c-major-mode-is 'pike-mode)
6894 (progn (goto-char block-follows)
6895 (zerop (c-forward-token-2 1 t)))
6896 (eq (char-after) ?\())))
6897 (cons 'inexpr-class (point))))
6898 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
6899 (when (not passed-paren)
6900 (cons 'inexpr-statement (point))))
6901 ((c-keyword-member kw-sym 'c-lambda-kwds)
6902 (when (or (not passed-paren)
6903 (eq passed-paren ?\())
6904 (cons 'inlambda (point))))
6905 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
6906 nil)
6907 (t
6908 'maybe)))
6909
6910 (if (looking-at "\\s(")
6911 (if passed-paren
6912 (if (and (eq passed-paren ?\[)
6913 (eq (char-after) ?\[))
6914 ;; Accept several square bracket sexps for
6915 ;; Java array initializations.
6916 'maybe)
6917 (setq passed-paren (char-after))
6918 'maybe)
6919 'maybe))))
6920
6921 (if (eq res 'maybe)
6922 (when (and c-recognize-paren-inexpr-blocks
6923 block-follows
6924 containing-sexp
6925 (eq (char-after containing-sexp) ?\())
6926 (goto-char containing-sexp)
6927 (if (or (save-excursion
6928 (c-backward-syntactic-ws lim)
6929 (and (> (point) (or lim (point-min)))
6930 (c-on-identifier)))
6931 (and c-special-brace-lists
6932 (c-looking-at-special-brace-list)))
6933 nil
6934 (cons 'inexpr-statement (point))))
6935
6936 res))))
6937
6938 (defun c-looking-at-inexpr-block-backward (paren-state)
6939 ;; Returns non-nil if we're looking at the end of an in-expression
6940 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
6941 ;; PAREN-STATE is the paren state relevant at the current position.
6942 ;;
6943 ;; This function might do hidden buffer changes.
6944 (save-excursion
6945 ;; We currently only recognize a block.
6946 (let ((here (point))
6947 (elem (car-safe paren-state))
6948 containing-sexp)
6949 (when (and (consp elem)
6950 (progn (goto-char (cdr elem))
6951 (c-forward-syntactic-ws here)
6952 (= (point) here)))
6953 (goto-char (car elem))
6954 (if (setq paren-state (cdr paren-state))
6955 (setq containing-sexp (car-safe paren-state)))
6956 (c-looking-at-inexpr-block (c-safe-position containing-sexp
6957 paren-state)
6958 containing-sexp)))))
6959
6960 \f
6961 ;; `c-guess-basic-syntax' and the functions that precedes it below
6962 ;; implements the main decision tree for determining the syntactic
6963 ;; analysis of the current line of code.
6964
6965 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
6966 ;; auto newline analysis.
6967 (defvar c-auto-newline-analysis nil)
6968
6969 (defsubst c-add-syntax (symbol &rest args)
6970 ;; A simple function to prepend a new syntax element to
6971 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
6972 ;; should always be dynamically bound but since we read it first
6973 ;; we'll fail properly anyway if this function is misused.
6974 (setq c-syntactic-context (cons (cons symbol args)
6975 c-syntactic-context)))
6976
6977 (defsubst c-append-syntax (symbol &rest args)
6978 ;; Like `c-add-syntax' but appends to the end of the syntax list.
6979 ;; (Normally not necessary.)
6980 (setq c-syntactic-context (nconc c-syntactic-context
6981 (list (cons symbol args)))))
6982
6983 (defun c-add-stmt-syntax (syntax-symbol
6984 syntax-extra-args
6985 stop-at-boi-only
6986 containing-sexp
6987 paren-state)
6988 ;; Do the generic processing to anchor the given syntax symbol on
6989 ;; the preceding statement: Skip over any labels and containing
6990 ;; statements on the same line, and then search backward until we
6991 ;; find a statement or block start that begins at boi without a
6992 ;; label or comment.
6993 ;;
6994 ;; Point is assumed to be at the prospective anchor point for the
6995 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
6996 ;; skip past open parens and containing statements. All the added
6997 ;; syntax elements will get the same anchor point.
6998 ;;
6999 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
7000 ;; syntax symbol. They are appended after the anchor point.
7001 ;;
7002 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
7003 ;; if the current statement starts there.
7004 ;;
7005 ;; Note: It's not a problem if PAREN-STATE "overshoots"
7006 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
7007 ;;
7008 ;; This function might do hidden buffer changes.
7009
7010 (if (= (point) (c-point 'boi))
7011 ;; This is by far the most common case, so let's give it special
7012 ;; treatment.
7013 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
7014
7015 (let ((syntax-last c-syntactic-context)
7016 (boi (c-point 'boi))
7017 ;; Set when we're on a label, so that we don't stop there.
7018 ;; FIXME: To be complete we should check if we're on a label
7019 ;; now at the start.
7020 on-label)
7021
7022 (apply 'c-add-syntax syntax-symbol nil syntax-extra-args)
7023
7024 ;; Loop while we have to back out of containing blocks.
7025 (while
7026 (and
7027 (catch 'back-up-block
7028
7029 ;; Loop while we have to back up statements.
7030 (while (or (/= (point) boi)
7031 on-label
7032 (looking-at c-comment-start-regexp))
7033
7034 ;; Skip past any comments that stands between the
7035 ;; statement start and boi.
7036 (let ((savepos (point)))
7037 (while (and (/= savepos boi)
7038 (c-backward-single-comment))
7039 (setq savepos (point)
7040 boi (c-point 'boi)))
7041 (goto-char savepos))
7042
7043 ;; Skip to the beginning of this statement or backward
7044 ;; another one.
7045 (let ((old-pos (point))
7046 (old-boi boi)
7047 (step-type (c-beginning-of-statement-1 containing-sexp)))
7048 (setq boi (c-point 'boi)
7049 on-label (eq step-type 'label))
7050
7051 (cond ((= (point) old-pos)
7052 ;; If we didn't move we're at the start of a block and
7053 ;; have to continue outside it.
7054 (throw 'back-up-block t))
7055
7056 ((and (eq step-type 'up)
7057 (>= (point) old-boi)
7058 (looking-at "else\\>[^_]")
7059 (save-excursion
7060 (goto-char old-pos)
7061 (looking-at "if\\>[^_]")))
7062 ;; Special case to avoid deeper and deeper indentation
7063 ;; of "else if" clauses.
7064 )
7065
7066 ((and (not stop-at-boi-only)
7067 (/= old-pos old-boi)
7068 (memq step-type '(up previous)))
7069 ;; If stop-at-boi-only is nil, we shouldn't back up
7070 ;; over previous or containing statements to try to
7071 ;; reach boi, so go back to the last position and
7072 ;; exit.
7073 (goto-char old-pos)
7074 (throw 'back-up-block nil))
7075
7076 (t
7077 (if (and (not stop-at-boi-only)
7078 (memq step-type '(up previous beginning)))
7079 ;; If we've moved into another statement then we
7080 ;; should no longer try to stop in the middle of a
7081 ;; line.
7082 (setq stop-at-boi-only t))
7083
7084 ;; Record this as a substatement if we skipped up one
7085 ;; level.
7086 (when (eq step-type 'up)
7087 (c-add-syntax 'substatement nil))))
7088 )))
7089
7090 containing-sexp)
7091
7092 ;; Now we have to go out of this block.
7093 (goto-char containing-sexp)
7094
7095 ;; Don't stop in the middle of a special brace list opener
7096 ;; like "({".
7097 (when c-special-brace-lists
7098 (let ((special-list (c-looking-at-special-brace-list)))
7099 (when (and special-list
7100 (< (car (car special-list)) (point)))
7101 (setq containing-sexp (car (car special-list)))
7102 (goto-char containing-sexp))))
7103
7104 (setq paren-state (c-whack-state-after containing-sexp paren-state)
7105 containing-sexp (c-most-enclosing-brace paren-state)
7106 boi (c-point 'boi))
7107
7108 ;; Analyze the construct in front of the block we've stepped out
7109 ;; from and add the right syntactic element for it.
7110 (let ((paren-pos (point))
7111 (paren-char (char-after))
7112 step-type)
7113
7114 (if (eq paren-char ?\()
7115 ;; Stepped out of a parenthesis block, so we're in an
7116 ;; expression now.
7117 (progn
7118 (when (/= paren-pos boi)
7119 (if (and c-recognize-paren-inexpr-blocks
7120 (progn
7121 (c-backward-syntactic-ws containing-sexp)
7122 (or (not (looking-at "\\>"))
7123 (not (c-on-identifier))))
7124 (save-excursion
7125 (goto-char (1+ paren-pos))
7126 (c-forward-syntactic-ws)
7127 (eq (char-after) ?{)))
7128 ;; Stepped out of an in-expression statement. This
7129 ;; syntactic element won't get an anchor pos.
7130 (c-add-syntax 'inexpr-statement)
7131
7132 ;; A parenthesis normally belongs to an arglist.
7133 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
7134
7135 (goto-char (max boi
7136 (if containing-sexp
7137 (1+ containing-sexp)
7138 (point-min))))
7139 (setq step-type 'same
7140 on-label nil))
7141
7142 (setq step-type (c-beginning-of-statement-1 containing-sexp)
7143 on-label (eq step-type 'label))
7144
7145 (if (and (eq step-type 'same)
7146 (/= paren-pos (point)))
7147 (save-excursion
7148 (goto-char paren-pos)
7149 (let ((inexpr (c-looking-at-inexpr-block
7150 (c-safe-position containing-sexp
7151 paren-state)
7152 containing-sexp)))
7153 (if (and inexpr
7154 (not (eq (car inexpr) 'inlambda)))
7155 (c-add-syntax 'statement-block-intro nil)
7156 (c-add-syntax 'defun-block-intro nil))))
7157 (c-add-syntax 'statement-block-intro nil)))
7158
7159 (if (= paren-pos boi)
7160 ;; Always done if the open brace was at boi. The
7161 ;; c-beginning-of-statement-1 call above is necessary
7162 ;; anyway, to decide the type of block-intro to add.
7163 (goto-char paren-pos)
7164 (setq boi (c-point 'boi)))
7165 ))
7166
7167 ;; Fill in the current point as the anchor for all the symbols
7168 ;; added above.
7169 (let ((p c-syntactic-context))
7170 (while (not (eq p syntax-last))
7171 (if (cdr (car p))
7172 (setcar (cdr (car p)) (point)))
7173 (setq p (cdr p))))
7174 )))
7175
7176 (defun c-add-class-syntax (symbol
7177 containing-decl-open
7178 containing-decl-start
7179 containing-decl-kwd
7180 paren-state)
7181 ;; The inclass and class-close syntactic symbols are added in
7182 ;; several places and some work is needed to fix everything.
7183 ;; Therefore it's collected here.
7184 ;;
7185 ;; This function might do hidden buffer changes.
7186 (goto-char containing-decl-open)
7187 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
7188 (progn
7189 (c-add-syntax symbol containing-decl-open)
7190 containing-decl-open)
7191 (goto-char containing-decl-start)
7192 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
7193 ;; here, but we have to do like this for compatibility.
7194 (back-to-indentation)
7195 (c-add-syntax symbol (point))
7196 (if (and (c-keyword-member containing-decl-kwd
7197 'c-inexpr-class-kwds)
7198 (/= containing-decl-start (c-point 'boi containing-decl-start)))
7199 (c-add-syntax 'inexpr-class))
7200 (point)))
7201
7202 (defun c-guess-continued-construct (indent-point
7203 char-after-ip
7204 beg-of-same-or-containing-stmt
7205 containing-sexp
7206 paren-state)
7207 ;; This function contains the decision tree reached through both
7208 ;; cases 18 and 10. It's a continued statement or top level
7209 ;; construct of some kind.
7210 ;;
7211 ;; This function might do hidden buffer changes.
7212
7213 (let (special-brace-list)
7214 (goto-char indent-point)
7215 (skip-chars-forward " \t")
7216
7217 (cond
7218 ;; (CASE A removed.)
7219 ;; CASE B: open braces for class or brace-lists
7220 ((setq special-brace-list
7221 (or (and c-special-brace-lists
7222 (c-looking-at-special-brace-list))
7223 (eq char-after-ip ?{)))
7224
7225 (cond
7226 ;; CASE B.1: class-open
7227 ((save-excursion
7228 (and (eq (char-after) ?{)
7229 (c-looking-at-decl-block containing-sexp t)
7230 (setq beg-of-same-or-containing-stmt (point))))
7231 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
7232
7233 ;; CASE B.2: brace-list-open
7234 ((or (consp special-brace-list)
7235 (save-excursion
7236 (goto-char beg-of-same-or-containing-stmt)
7237 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
7238 indent-point t t t)))
7239 ;; The most semantically accurate symbol here is
7240 ;; brace-list-open, but we normally report it simply as a
7241 ;; statement-cont. The reason is that one normally adjusts
7242 ;; brace-list-open for brace lists as top-level constructs,
7243 ;; and brace lists inside statements is a completely different
7244 ;; context. C.f. case 5A.3.
7245 (c-beginning-of-statement-1 containing-sexp)
7246 (c-add-stmt-syntax (if c-auto-newline-analysis
7247 ;; Turn off the dwim above when we're
7248 ;; analyzing the nature of the brace
7249 ;; for the auto newline feature.
7250 'brace-list-open
7251 'statement-cont)
7252 nil nil
7253 containing-sexp paren-state))
7254
7255 ;; CASE B.3: The body of a function declared inside a normal
7256 ;; block. Can occur e.g. in Pike and when using gcc
7257 ;; extensions, but watch out for macros followed by blocks.
7258 ;; C.f. cases E, 16F and 17G.
7259 ((and (not (c-at-statement-start-p))
7260 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7261 'same)
7262 (save-excursion
7263 (let ((c-recognize-typeless-decls nil))
7264 ;; Turn off recognition of constructs that lacks a
7265 ;; type in this case, since that's more likely to be
7266 ;; a macro followed by a block.
7267 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7268 (c-add-stmt-syntax 'defun-open nil t
7269 containing-sexp paren-state))
7270
7271 ;; CASE B.4: Continued statement with block open. The most
7272 ;; accurate analysis is perhaps `statement-cont' together with
7273 ;; `block-open' but we play DWIM and use `substatement-open'
7274 ;; instead. The rationaly is that this typically is a macro
7275 ;; followed by a block which makes it very similar to a
7276 ;; statement with a substatement block.
7277 (t
7278 (c-add-stmt-syntax 'substatement-open nil nil
7279 containing-sexp paren-state))
7280 ))
7281
7282 ;; CASE C: iostream insertion or extraction operator
7283 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
7284 (save-excursion
7285 (goto-char beg-of-same-or-containing-stmt)
7286 ;; If there is no preceding streamop in the statement
7287 ;; then indent this line as a normal statement-cont.
7288 (when (c-syntactic-re-search-forward
7289 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
7290 (c-add-syntax 'stream-op (c-point 'boi))
7291 t))))
7292
7293 ;; CASE E: In the "K&R region" of a function declared inside a
7294 ;; normal block. C.f. case B.3.
7295 ((and (save-excursion
7296 ;; Check that the next token is a '{'. This works as
7297 ;; long as no language that allows nested function
7298 ;; definitions allows stuff like member init lists, K&R
7299 ;; declarations or throws clauses there.
7300 ;;
7301 ;; Note that we do a forward search for something ahead
7302 ;; of the indentation line here. That's not good since
7303 ;; the user might not have typed it yet. Unfortunately
7304 ;; it's exceedingly tricky to recognize a function
7305 ;; prototype in a code block without resorting to this.
7306 (c-forward-syntactic-ws)
7307 (eq (char-after) ?{))
7308 (not (c-at-statement-start-p))
7309 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7310 'same)
7311 (save-excursion
7312 (let ((c-recognize-typeless-decls nil))
7313 ;; Turn off recognition of constructs that lacks a
7314 ;; type in this case, since that's more likely to be
7315 ;; a macro followed by a block.
7316 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7317 (c-add-stmt-syntax 'func-decl-cont nil t
7318 containing-sexp paren-state))
7319
7320 ;; CASE D: continued statement.
7321 (t
7322 (c-beginning-of-statement-1 containing-sexp)
7323 (c-add-stmt-syntax 'statement-cont nil nil
7324 containing-sexp paren-state))
7325 )))
7326
7327 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
7328 ;; 2005/11/29).
7329 ;;;###autoload
7330 (defun c-guess-basic-syntax ()
7331 "Return the syntactic context of the current line."
7332 (save-excursion
7333 (beginning-of-line)
7334 (c-save-buffer-state
7335 ((indent-point (point))
7336 (case-fold-search nil)
7337 ;; A whole ugly bunch of various temporary variables. Have
7338 ;; to declare them here since it's not possible to declare
7339 ;; a variable with only the scope of a cond test and the
7340 ;; following result clauses, and most of this function is a
7341 ;; single gigantic cond. :P
7342 literal char-before-ip before-ws-ip char-after-ip macro-start
7343 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
7344 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
7345 ;; The following record some positions for the containing
7346 ;; declaration block if we're directly within one:
7347 ;; `containing-decl-open' is the position of the open
7348 ;; brace. `containing-decl-start' is the start of the
7349 ;; declaration. `containing-decl-kwd' is the keyword
7350 ;; symbol of the keyword that tells what kind of block it
7351 ;; is.
7352 containing-decl-open
7353 containing-decl-start
7354 containing-decl-kwd
7355 ;; The open paren of the closest surrounding sexp or nil if
7356 ;; there is none.
7357 containing-sexp
7358 ;; The position after the closest preceding brace sexp
7359 ;; (nested sexps are ignored), or the position after
7360 ;; `containing-sexp' if there is none, or (point-min) if
7361 ;; `containing-sexp' is nil.
7362 lim
7363 ;; The paren state outside `containing-sexp', or at
7364 ;; `indent-point' if `containing-sexp' is nil.
7365 (paren-state (c-parse-state))
7366 ;; There's always at most one syntactic element which got
7367 ;; an anchor pos. It's stored in syntactic-relpos.
7368 syntactic-relpos
7369 (c-stmt-delim-chars c-stmt-delim-chars))
7370
7371 ;; Check if we're directly inside an enclosing declaration
7372 ;; level block.
7373 (when (and (setq containing-sexp
7374 (c-most-enclosing-brace paren-state))
7375 (progn
7376 (goto-char containing-sexp)
7377 (eq (char-after) ?{))
7378 (setq placeholder
7379 (c-looking-at-decl-block
7380 (c-most-enclosing-brace paren-state
7381 containing-sexp)
7382 t)))
7383 (setq containing-decl-open containing-sexp
7384 containing-decl-start (point)
7385 containing-sexp nil)
7386 (goto-char placeholder)
7387 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
7388 (c-keyword-sym (match-string 1)))))
7389
7390 ;; Init some position variables.
7391 (if c-state-cache
7392 (progn
7393 (setq containing-sexp (car paren-state)
7394 paren-state (cdr paren-state))
7395 (if (consp containing-sexp)
7396 (progn
7397 (setq lim (cdr containing-sexp))
7398 (if (cdr c-state-cache)
7399 ;; Ignore balanced paren. The next entry
7400 ;; can't be another one.
7401 (setq containing-sexp (car (cdr c-state-cache))
7402 paren-state (cdr paren-state))
7403 ;; If there is no surrounding open paren then
7404 ;; put the last balanced pair back on paren-state.
7405 (setq paren-state (cons containing-sexp paren-state)
7406 containing-sexp nil)))
7407 (setq lim (1+ containing-sexp))))
7408 (setq lim (point-min)))
7409
7410 ;; If we're in a parenthesis list then ',' delimits the
7411 ;; "statements" rather than being an operator (with the
7412 ;; exception of the "for" clause). This difference is
7413 ;; typically only noticeable when statements are used in macro
7414 ;; arglists.
7415 (when (and containing-sexp
7416 (eq (char-after containing-sexp) ?\())
7417 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
7418
7419 ;; cache char before and after indent point, and move point to
7420 ;; the most likely position to perform the majority of tests
7421 (goto-char indent-point)
7422 (c-backward-syntactic-ws lim)
7423 (setq before-ws-ip (point)
7424 char-before-ip (char-before))
7425 (goto-char indent-point)
7426 (skip-chars-forward " \t")
7427 (setq char-after-ip (char-after))
7428
7429 ;; are we in a literal?
7430 (setq literal (c-in-literal lim))
7431
7432 ;; now figure out syntactic qualities of the current line
7433 (cond
7434
7435 ;; CASE 1: in a string.
7436 ((eq literal 'string)
7437 (c-add-syntax 'string (c-point 'bopl)))
7438
7439 ;; CASE 2: in a C or C++ style comment.
7440 ((and (memq literal '(c c++))
7441 ;; This is a kludge for XEmacs where we use
7442 ;; `buffer-syntactic-context', which doesn't correctly
7443 ;; recognize "\*/" to end a block comment.
7444 ;; `parse-partial-sexp' which is used by
7445 ;; `c-literal-limits' will however do that in most
7446 ;; versions, which results in that we get nil from
7447 ;; `c-literal-limits' even when `c-in-literal' claims
7448 ;; we're inside a comment.
7449 (setq placeholder (c-literal-limits lim)))
7450 (c-add-syntax literal (car placeholder)))
7451
7452 ;; CASE 3: in a cpp preprocessor macro continuation.
7453 ((and (save-excursion
7454 (when (c-beginning-of-macro)
7455 (setq macro-start (point))))
7456 (/= macro-start (c-point 'boi))
7457 (progn
7458 (setq tmpsymbol 'cpp-macro-cont)
7459 (or (not c-syntactic-indentation-in-macros)
7460 (save-excursion
7461 (goto-char macro-start)
7462 ;; If at the beginning of the body of a #define
7463 ;; directive then analyze as cpp-define-intro
7464 ;; only. Go on with the syntactic analysis
7465 ;; otherwise. in-macro-expr is set if we're in a
7466 ;; cpp expression, i.e. before the #define body
7467 ;; or anywhere in a non-#define directive.
7468 (if (c-forward-to-cpp-define-body)
7469 (let ((indent-boi (c-point 'boi indent-point)))
7470 (setq in-macro-expr (> (point) indent-boi)
7471 tmpsymbol 'cpp-define-intro)
7472 (= (point) indent-boi))
7473 (setq in-macro-expr t)
7474 nil)))))
7475 (c-add-syntax tmpsymbol macro-start)
7476 (setq macro-start nil))
7477
7478 ;; CASE 11: an else clause?
7479 ((looking-at "else\\>[^_]")
7480 (c-beginning-of-statement-1 containing-sexp)
7481 (c-add-stmt-syntax 'else-clause nil t
7482 containing-sexp paren-state))
7483
7484 ;; CASE 12: while closure of a do/while construct?
7485 ((and (looking-at "while\\>[^_]")
7486 (save-excursion
7487 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
7488 'beginning)
7489 (setq placeholder (point)))))
7490 (goto-char placeholder)
7491 (c-add-stmt-syntax 'do-while-closure nil t
7492 containing-sexp paren-state))
7493
7494 ;; CASE 13: A catch or finally clause? This case is simpler
7495 ;; than if-else and do-while, because a block is required
7496 ;; after every try, catch and finally.
7497 ((save-excursion
7498 (and (cond ((c-major-mode-is 'c++-mode)
7499 (looking-at "catch\\>[^_]"))
7500 ((c-major-mode-is 'java-mode)
7501 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
7502 (and (c-safe (c-backward-syntactic-ws)
7503 (c-backward-sexp)
7504 t)
7505 (eq (char-after) ?{)
7506 (c-safe (c-backward-syntactic-ws)
7507 (c-backward-sexp)
7508 t)
7509 (if (eq (char-after) ?\()
7510 (c-safe (c-backward-sexp) t)
7511 t))
7512 (looking-at "\\(try\\|catch\\)\\>[^_]")
7513 (setq placeholder (point))))
7514 (goto-char placeholder)
7515 (c-add-stmt-syntax 'catch-clause nil t
7516 containing-sexp paren-state))
7517
7518 ;; CASE 18: A substatement we can recognize by keyword.
7519 ((save-excursion
7520 (and c-opt-block-stmt-key
7521 (not (eq char-before-ip ?\;))
7522 (not (c-at-vsemi-p before-ws-ip))
7523 (not (memq char-after-ip '(?\) ?\] ?,)))
7524 (or (not (eq char-before-ip ?}))
7525 (c-looking-at-inexpr-block-backward c-state-cache))
7526 (> (point)
7527 (progn
7528 ;; Ought to cache the result from the
7529 ;; c-beginning-of-statement-1 calls here.
7530 (setq placeholder (point))
7531 (while (eq (setq step-type
7532 (c-beginning-of-statement-1 lim))
7533 'label))
7534 (if (eq step-type 'previous)
7535 (goto-char placeholder)
7536 (setq placeholder (point))
7537 (if (and (eq step-type 'same)
7538 (not (looking-at c-opt-block-stmt-key)))
7539 ;; Step up to the containing statement if we
7540 ;; stayed in the same one.
7541 (let (step)
7542 (while (eq
7543 (setq step
7544 (c-beginning-of-statement-1 lim))
7545 'label))
7546 (if (eq step 'up)
7547 (setq placeholder (point))
7548 ;; There was no containing statement afterall.
7549 (goto-char placeholder)))))
7550 placeholder))
7551 (if (looking-at c-block-stmt-2-key)
7552 ;; Require a parenthesis after these keywords.
7553 ;; Necessary to catch e.g. synchronized in Java,
7554 ;; which can be used both as statement and
7555 ;; modifier.
7556 (and (zerop (c-forward-token-2 1 nil))
7557 (eq (char-after) ?\())
7558 (looking-at c-opt-block-stmt-key))))
7559
7560 (if (eq step-type 'up)
7561 ;; CASE 18A: Simple substatement.
7562 (progn
7563 (goto-char placeholder)
7564 (cond
7565 ((eq char-after-ip ?{)
7566 (c-add-stmt-syntax 'substatement-open nil nil
7567 containing-sexp paren-state))
7568 ((save-excursion
7569 (goto-char indent-point)
7570 (back-to-indentation)
7571 (c-forward-label))
7572 (c-add-stmt-syntax 'substatement-label nil nil
7573 containing-sexp paren-state))
7574 (t
7575 (c-add-stmt-syntax 'substatement nil nil
7576 containing-sexp paren-state))))
7577
7578 ;; CASE 18B: Some other substatement. This is shared
7579 ;; with case 10.
7580 (c-guess-continued-construct indent-point
7581 char-after-ip
7582 placeholder
7583 lim
7584 paren-state)))
7585
7586 ;; CASE 14: A case or default label
7587 ((looking-at c-label-kwds-regexp)
7588 (if containing-sexp
7589 (progn
7590 (goto-char containing-sexp)
7591 (setq lim (c-most-enclosing-brace c-state-cache
7592 containing-sexp))
7593 (c-backward-to-block-anchor lim)
7594 (c-add-stmt-syntax 'case-label nil t lim paren-state))
7595 ;; Got a bogus label at the top level. In lack of better
7596 ;; alternatives, anchor it on (point-min).
7597 (c-add-syntax 'case-label (point-min))))
7598
7599 ;; CASE 15: any other label
7600 ((save-excursion
7601 (back-to-indentation)
7602 (and (not (looking-at c-syntactic-ws-start))
7603 (c-forward-label)))
7604 (cond (containing-decl-open
7605 (setq placeholder (c-add-class-syntax 'inclass
7606 containing-decl-open
7607 containing-decl-start
7608 containing-decl-kwd
7609 paren-state))
7610 ;; Append access-label with the same anchor point as
7611 ;; inclass gets.
7612 (c-append-syntax 'access-label placeholder))
7613
7614 (containing-sexp
7615 (goto-char containing-sexp)
7616 (setq lim (c-most-enclosing-brace c-state-cache
7617 containing-sexp))
7618 (save-excursion
7619 (setq tmpsymbol
7620 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
7621 (looking-at "switch\\>[^_]"))
7622 ;; If the surrounding statement is a switch then
7623 ;; let's analyze all labels as switch labels, so
7624 ;; that they get lined up consistently.
7625 'case-label
7626 'label)))
7627 (c-backward-to-block-anchor lim)
7628 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
7629
7630 (t
7631 ;; A label on the top level. Treat it as a class
7632 ;; context. (point-min) is the closest we get to the
7633 ;; class open brace.
7634 (c-add-syntax 'access-label (point-min)))))
7635
7636 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
7637 ;; 17E.
7638 ((setq placeholder (c-looking-at-inexpr-block
7639 (c-safe-position containing-sexp paren-state)
7640 containing-sexp
7641 ;; Have to turn on the heuristics after
7642 ;; the point even though it doesn't work
7643 ;; very well. C.f. test case class-16.pike.
7644 t))
7645 (setq tmpsymbol (assq (car placeholder)
7646 '((inexpr-class . class-open)
7647 (inexpr-statement . block-open))))
7648 (if tmpsymbol
7649 ;; It's a statement block or an anonymous class.
7650 (setq tmpsymbol (cdr tmpsymbol))
7651 ;; It's a Pike lambda. Check whether we are between the
7652 ;; lambda keyword and the argument list or at the defun
7653 ;; opener.
7654 (setq tmpsymbol (if (eq char-after-ip ?{)
7655 'inline-open
7656 'lambda-intro-cont)))
7657 (goto-char (cdr placeholder))
7658 (back-to-indentation)
7659 (c-add-stmt-syntax tmpsymbol nil t
7660 (c-most-enclosing-brace c-state-cache (point))
7661 paren-state)
7662 (unless (eq (point) (cdr placeholder))
7663 (c-add-syntax (car placeholder))))
7664
7665 ;; CASE 5: Line is inside a declaration level block or at top level.
7666 ((or containing-decl-open (null containing-sexp))
7667 (cond
7668
7669 ;; CASE 5A: we are looking at a defun, brace list, class,
7670 ;; or inline-inclass method opening brace
7671 ((setq special-brace-list
7672 (or (and c-special-brace-lists
7673 (c-looking-at-special-brace-list))
7674 (eq char-after-ip ?{)))
7675 (cond
7676
7677 ;; CASE 5A.1: Non-class declaration block open.
7678 ((save-excursion
7679 (let (tmp)
7680 (and (eq char-after-ip ?{)
7681 (setq tmp (c-looking-at-decl-block containing-sexp t))
7682 (progn
7683 (setq placeholder (point))
7684 (goto-char tmp)
7685 (looking-at c-symbol-key))
7686 (c-keyword-member
7687 (c-keyword-sym (setq keyword (match-string 0)))
7688 'c-other-block-decl-kwds))))
7689 (goto-char placeholder)
7690 (c-add-stmt-syntax
7691 (if (string-equal keyword "extern")
7692 ;; Special case for extern-lang-open.
7693 'extern-lang-open
7694 (intern (concat keyword "-open")))
7695 nil t containing-sexp paren-state))
7696
7697 ;; CASE 5A.2: we are looking at a class opening brace
7698 ((save-excursion
7699 (goto-char indent-point)
7700 (skip-chars-forward " \t")
7701 (and (eq (char-after) ?{)
7702 (c-looking-at-decl-block containing-sexp t)
7703 (setq placeholder (point))))
7704 (c-add-syntax 'class-open placeholder))
7705
7706 ;; CASE 5A.3: brace list open
7707 ((save-excursion
7708 (c-beginning-of-decl-1 lim)
7709 (while (looking-at c-specifier-key)
7710 (goto-char (match-end 1))
7711 (c-forward-syntactic-ws indent-point))
7712 (setq placeholder (c-point 'boi))
7713 (or (consp special-brace-list)
7714 (and (or (save-excursion
7715 (goto-char indent-point)
7716 (setq tmpsymbol nil)
7717 (while (and (> (point) placeholder)
7718 (zerop (c-backward-token-2 1 t))
7719 (/= (char-after) ?=))
7720 (and c-opt-inexpr-brace-list-key
7721 (not tmpsymbol)
7722 (looking-at c-opt-inexpr-brace-list-key)
7723 (setq tmpsymbol 'topmost-intro-cont)))
7724 (eq (char-after) ?=))
7725 (looking-at c-brace-list-key))
7726 (save-excursion
7727 (while (and (< (point) indent-point)
7728 (zerop (c-forward-token-2 1 t))
7729 (not (memq (char-after) '(?\; ?\()))))
7730 (not (memq (char-after) '(?\; ?\()))
7731 ))))
7732 (if (and (not c-auto-newline-analysis)
7733 (c-major-mode-is 'java-mode)
7734 (eq tmpsymbol 'topmost-intro-cont))
7735 ;; We're in Java and have found that the open brace
7736 ;; belongs to a "new Foo[]" initialization list,
7737 ;; which means the brace list is part of an
7738 ;; expression and not a top level definition. We
7739 ;; therefore treat it as any topmost continuation
7740 ;; even though the semantically correct symbol still
7741 ;; is brace-list-open, on the same grounds as in
7742 ;; case B.2.
7743 (progn
7744 (c-beginning-of-statement-1 lim)
7745 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
7746 (c-add-syntax 'brace-list-open placeholder)))
7747
7748 ;; CASE 5A.4: inline defun open
7749 ((and containing-decl-open
7750 (not (c-keyword-member containing-decl-kwd
7751 'c-other-block-decl-kwds)))
7752 (c-add-syntax 'inline-open)
7753 (c-add-class-syntax 'inclass
7754 containing-decl-open
7755 containing-decl-start
7756 containing-decl-kwd
7757 paren-state))
7758
7759 ;; CASE 5A.5: ordinary defun open
7760 (t
7761 (goto-char placeholder)
7762 (if (or containing-decl-open macro-start)
7763 (c-add-syntax 'defun-open (c-point 'boi))
7764 ;; Bogus to use bol here, but it's the legacy.
7765 (c-add-syntax 'defun-open (c-point 'bol)))
7766 )))
7767
7768 ;; CASE 5B: After a function header but before the body (or
7769 ;; the ending semicolon if there's no body).
7770 ((save-excursion
7771 (when (setq placeholder (c-just-after-func-arglist-p lim))
7772 (setq tmp-pos (point))))
7773 (cond
7774
7775 ;; CASE 5B.1: Member init list.
7776 ((eq (char-after tmp-pos) ?:)
7777 (if (or (> tmp-pos indent-point)
7778 (= (c-point 'bosws) (1+ tmp-pos)))
7779 (progn
7780 ;; There is no preceding member init clause.
7781 ;; Indent relative to the beginning of indentation
7782 ;; for the topmost-intro line that contains the
7783 ;; prototype's open paren.
7784 (goto-char placeholder)
7785 (c-add-syntax 'member-init-intro (c-point 'boi)))
7786 ;; Indent relative to the first member init clause.
7787 (goto-char (1+ tmp-pos))
7788 (c-forward-syntactic-ws)
7789 (c-add-syntax 'member-init-cont (point))))
7790
7791 ;; CASE 5B.2: K&R arg decl intro
7792 ((and c-recognize-knr-p
7793 (c-in-knr-argdecl lim))
7794 (c-beginning-of-statement-1 lim)
7795 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
7796 (if containing-decl-open
7797 (c-add-class-syntax 'inclass
7798 containing-decl-open
7799 containing-decl-start
7800 containing-decl-kwd
7801 paren-state)))
7802
7803 ;; CASE 5B.4: Nether region after a C++ or Java func
7804 ;; decl, which could include a `throws' declaration.
7805 (t
7806 (c-beginning-of-statement-1 lim)
7807 (c-add-syntax 'func-decl-cont (c-point 'boi))
7808 )))
7809
7810 ;; CASE 5C: inheritance line. could be first inheritance
7811 ;; line, or continuation of a multiple inheritance
7812 ((or (and (c-major-mode-is 'c++-mode)
7813 (progn
7814 (when (eq char-after-ip ?,)
7815 (skip-chars-forward " \t")
7816 (forward-char))
7817 (looking-at c-opt-postfix-decl-spec-key)))
7818 (and (or (eq char-before-ip ?:)
7819 ;; watch out for scope operator
7820 (save-excursion
7821 (and (eq char-after-ip ?:)
7822 (c-safe (forward-char 1) t)
7823 (not (eq (char-after) ?:))
7824 )))
7825 (save-excursion
7826 (c-backward-syntactic-ws lim)
7827 (if (eq char-before-ip ?:)
7828 (progn
7829 (forward-char -1)
7830 (c-backward-syntactic-ws lim)))
7831 (back-to-indentation)
7832 (looking-at c-class-key)))
7833 ;; for Java
7834 (and (c-major-mode-is 'java-mode)
7835 (let ((fence (save-excursion
7836 (c-beginning-of-statement-1 lim)
7837 (point)))
7838 cont done)
7839 (save-excursion
7840 (while (not done)
7841 (cond ((looking-at c-opt-postfix-decl-spec-key)
7842 (setq injava-inher (cons cont (point))
7843 done t))
7844 ((or (not (c-safe (c-forward-sexp -1) t))
7845 (<= (point) fence))
7846 (setq done t))
7847 )
7848 (setq cont t)))
7849 injava-inher)
7850 (not (c-crosses-statement-barrier-p (cdr injava-inher)
7851 (point)))
7852 ))
7853 (cond
7854
7855 ;; CASE 5C.1: non-hanging colon on an inher intro
7856 ((eq char-after-ip ?:)
7857 (c-beginning-of-statement-1 lim)
7858 (c-add-syntax 'inher-intro (c-point 'boi))
7859 ;; don't add inclass symbol since relative point already
7860 ;; contains any class offset
7861 )
7862
7863 ;; CASE 5C.2: hanging colon on an inher intro
7864 ((eq char-before-ip ?:)
7865 (c-beginning-of-statement-1 lim)
7866 (c-add-syntax 'inher-intro (c-point 'boi))
7867 (if containing-decl-open
7868 (c-add-class-syntax 'inclass
7869 containing-decl-open
7870 containing-decl-start
7871 containing-decl-kwd
7872 paren-state)))
7873
7874 ;; CASE 5C.3: in a Java implements/extends
7875 (injava-inher
7876 (let ((where (cdr injava-inher))
7877 (cont (car injava-inher)))
7878 (goto-char where)
7879 (cond ((looking-at "throws\\>[^_]")
7880 (c-add-syntax 'func-decl-cont
7881 (progn (c-beginning-of-statement-1 lim)
7882 (c-point 'boi))))
7883 (cont (c-add-syntax 'inher-cont where))
7884 (t (c-add-syntax 'inher-intro
7885 (progn (goto-char (cdr injava-inher))
7886 (c-beginning-of-statement-1 lim)
7887 (point))))
7888 )))
7889
7890 ;; CASE 5C.4: a continued inheritance line
7891 (t
7892 (c-beginning-of-inheritance-list lim)
7893 (c-add-syntax 'inher-cont (point))
7894 ;; don't add inclass symbol since relative point already
7895 ;; contains any class offset
7896 )))
7897
7898 ;; CASE 5D: this could be a top-level initialization, a
7899 ;; member init list continuation, or a template argument
7900 ;; list continuation.
7901 ((save-excursion
7902 ;; Note: We use the fact that lim always is after any
7903 ;; preceding brace sexp.
7904 (if c-recognize-<>-arglists
7905 (while (and
7906 (progn
7907 (c-syntactic-skip-backward "^;,=<>" lim t)
7908 (> (point) lim))
7909 (or
7910 (when c-overloadable-operators-regexp
7911 (when (setq placeholder (c-after-special-operator-id lim))
7912 (goto-char placeholder)
7913 t))
7914 (cond
7915 ((eq (char-before) ?>)
7916 (or (c-backward-<>-arglist nil lim)
7917 (backward-char))
7918 t)
7919 ((eq (char-before) ?<)
7920 (backward-char)
7921 (if (save-excursion
7922 (c-forward-<>-arglist nil))
7923 (progn (forward-char)
7924 nil)
7925 t))
7926 (t nil)))))
7927 ;; NB: No c-after-special-operator-id stuff in this
7928 ;; clause - we assume only C++ needs it.
7929 (c-syntactic-skip-backward "^;,=" lim t))
7930 (memq (char-before) '(?, ?= ?<)))
7931 (cond
7932
7933 ;; CASE 5D.3: perhaps a template list continuation?
7934 ((and (c-major-mode-is 'c++-mode)
7935 (save-excursion
7936 (save-restriction
7937 (c-with-syntax-table c++-template-syntax-table
7938 (goto-char indent-point)
7939 (setq placeholder (c-up-list-backward))
7940 (and placeholder
7941 (eq (char-after placeholder) ?<))))))
7942 (c-with-syntax-table c++-template-syntax-table
7943 (goto-char placeholder)
7944 (c-beginning-of-statement-1 lim t)
7945 (if (save-excursion
7946 (c-backward-syntactic-ws lim)
7947 (eq (char-before) ?<))
7948 ;; In a nested template arglist.
7949 (progn
7950 (goto-char placeholder)
7951 (c-syntactic-skip-backward "^,;" lim t)
7952 (c-forward-syntactic-ws))
7953 (back-to-indentation)))
7954 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
7955 ;; template aware.
7956 (c-add-syntax 'template-args-cont (point)))
7957
7958 ;; CASE 5D.4: perhaps a multiple inheritance line?
7959 ((and (c-major-mode-is 'c++-mode)
7960 (save-excursion
7961 (c-beginning-of-statement-1 lim)
7962 (setq placeholder (point))
7963 (if (looking-at "static\\>[^_]")
7964 (c-forward-token-2 1 nil indent-point))
7965 (and (looking-at c-class-key)
7966 (zerop (c-forward-token-2 2 nil indent-point))
7967 (if (eq (char-after) ?<)
7968 (c-with-syntax-table c++-template-syntax-table
7969 (zerop (c-forward-token-2 1 t indent-point)))
7970 t)
7971 (eq (char-after) ?:))))
7972 (goto-char placeholder)
7973 (c-add-syntax 'inher-cont (c-point 'boi)))
7974
7975 ;; CASE 5D.5: Continuation of the "expression part" of a
7976 ;; top level construct.
7977 (t
7978 (while (and (eq (car (c-beginning-of-decl-1 containing-sexp))
7979 'same)
7980 (save-excursion
7981 (c-backward-syntactic-ws)
7982 (eq (char-before) ?}))))
7983 (c-add-stmt-syntax
7984 (if (eq char-before-ip ?,)
7985 ;; A preceding comma at the top level means that a
7986 ;; new variable declaration starts here. Use
7987 ;; topmost-intro-cont for it, for consistency with
7988 ;; the first variable declaration. C.f. case 5N.
7989 'topmost-intro-cont
7990 'statement-cont)
7991 nil nil containing-sexp paren-state))
7992 ))
7993
7994 ;; CASE 5F: Close of a non-class declaration level block.
7995 ((and (eq char-after-ip ?})
7996 (c-keyword-member containing-decl-kwd
7997 'c-other-block-decl-kwds))
7998 ;; This is inconsistent: Should use `containing-decl-open'
7999 ;; here if it's at boi, like in case 5J.
8000 (goto-char containing-decl-start)
8001 (c-add-stmt-syntax
8002 (if (string-equal (symbol-name containing-decl-kwd) "extern")
8003 ;; Special case for compatibility with the
8004 ;; extern-lang syntactic symbols.
8005 'extern-lang-close
8006 (intern (concat (symbol-name containing-decl-kwd)
8007 "-close")))
8008 nil t
8009 (c-most-enclosing-brace paren-state (point))
8010 paren-state))
8011
8012 ;; CASE 5G: we are looking at the brace which closes the
8013 ;; enclosing nested class decl
8014 ((and containing-sexp
8015 (eq char-after-ip ?})
8016 (eq containing-decl-open containing-sexp))
8017 (c-add-class-syntax 'class-close
8018 containing-decl-open
8019 containing-decl-start
8020 containing-decl-kwd
8021 paren-state))
8022
8023 ;; CASE 5H: we could be looking at subsequent knr-argdecls
8024 ((and c-recognize-knr-p
8025 (not (eq char-before-ip ?}))
8026 (save-excursion
8027 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
8028 (and placeholder
8029 ;; Do an extra check to avoid tripping up on
8030 ;; statements that occur in invalid contexts
8031 ;; (e.g. in macro bodies where we don't really
8032 ;; know the context of what we're looking at).
8033 (not (and c-opt-block-stmt-key
8034 (looking-at c-opt-block-stmt-key)))))
8035 (< placeholder indent-point))
8036 (goto-char placeholder)
8037 (c-add-syntax 'knr-argdecl (point)))
8038
8039 ;; CASE 5I: ObjC method definition.
8040 ((and c-opt-method-key
8041 (looking-at c-opt-method-key))
8042 (c-beginning-of-statement-1 nil t)
8043 (if (= (point) indent-point)
8044 ;; Handle the case when it's the first (non-comment)
8045 ;; thing in the buffer. Can't look for a 'same return
8046 ;; value from cbos1 since ObjC directives currently
8047 ;; aren't recognized fully, so that we get 'same
8048 ;; instead of 'previous if it moved over a preceding
8049 ;; directive.
8050 (goto-char (point-min)))
8051 (c-add-syntax 'objc-method-intro (c-point 'boi)))
8052
8053 ;; CASE 5P: AWK pattern or function or continuation
8054 ;; thereof.
8055 ((c-major-mode-is 'awk-mode)
8056 (setq placeholder (point))
8057 (c-add-stmt-syntax
8058 (if (and (eq (c-beginning-of-statement-1) 'same)
8059 (/= (point) placeholder))
8060 'topmost-intro-cont
8061 'topmost-intro)
8062 nil nil
8063 containing-sexp paren-state))
8064
8065 ;; CASE 5N: At a variable declaration that follows a class
8066 ;; definition or some other block declaration that doesn't
8067 ;; end at the closing '}'. C.f. case 5D.5.
8068 ((progn
8069 (c-backward-syntactic-ws lim)
8070 (and (eq (char-before) ?})
8071 (save-excursion
8072 (let ((start (point)))
8073 (if c-state-cache
8074 ;; Speed up the backward search a bit.
8075 (goto-char (caar c-state-cache)))
8076 (c-beginning-of-decl-1 containing-sexp)
8077 (setq placeholder (point))
8078 (if (= start (point))
8079 ;; The '}' is unbalanced.
8080 nil
8081 (c-end-of-decl-1)
8082 (>= (point) indent-point))))))
8083 (goto-char placeholder)
8084 (c-add-stmt-syntax 'topmost-intro-cont nil nil
8085 containing-sexp paren-state))
8086
8087 ;; NOTE: The point is at the end of the previous token here.
8088
8089 ;; CASE 5J: we are at the topmost level, make
8090 ;; sure we skip back past any access specifiers
8091 ((save-excursion
8092 (setq placeholder (point))
8093 (or (memq char-before-ip '(?\; ?{ ?} nil))
8094 (c-at-vsemi-p before-ws-ip)
8095 (when (and (eq char-before-ip ?:)
8096 (eq (c-beginning-of-statement-1 lim)
8097 'label))
8098 (c-backward-syntactic-ws lim)
8099 (setq placeholder (point)))
8100 (and (c-major-mode-is 'objc-mode)
8101 (catch 'not-in-directive
8102 (c-beginning-of-statement-1 lim)
8103 (setq placeholder (point))
8104 (while (and (c-forward-objc-directive)
8105 (< (point) indent-point))
8106 (c-forward-syntactic-ws)
8107 (if (>= (point) indent-point)
8108 (throw 'not-in-directive t))
8109 (setq placeholder (point)))
8110 nil))))
8111 ;; For historic reasons we anchor at bol of the last
8112 ;; line of the previous declaration. That's clearly
8113 ;; highly bogus and useless, and it makes our lives hard
8114 ;; to remain compatible. :P
8115 (goto-char placeholder)
8116 (c-add-syntax 'topmost-intro (c-point 'bol))
8117 (if containing-decl-open
8118 (if (c-keyword-member containing-decl-kwd
8119 'c-other-block-decl-kwds)
8120 (progn
8121 (goto-char containing-decl-open)
8122 (unless (= (point) (c-point 'boi))
8123 (goto-char containing-decl-start))
8124 (c-add-stmt-syntax
8125 (if (string-equal (symbol-name containing-decl-kwd)
8126 "extern")
8127 ;; Special case for compatibility with the
8128 ;; extern-lang syntactic symbols.
8129 'inextern-lang
8130 (intern (concat "in"
8131 (symbol-name containing-decl-kwd))))
8132 nil t
8133 (c-most-enclosing-brace paren-state (point))
8134 paren-state))
8135 (c-add-class-syntax 'inclass
8136 containing-decl-open
8137 containing-decl-start
8138 containing-decl-kwd
8139 paren-state)))
8140 (when (and c-syntactic-indentation-in-macros
8141 macro-start
8142 (/= macro-start (c-point 'boi indent-point)))
8143 (c-add-syntax 'cpp-define-intro)
8144 (setq macro-start nil)))
8145
8146 ;; CASE 5K: we are at an ObjC method definition
8147 ;; continuation line.
8148 ((and c-opt-method-key
8149 (save-excursion
8150 (c-beginning-of-statement-1 lim)
8151 (beginning-of-line)
8152 (when (looking-at c-opt-method-key)
8153 (setq placeholder (point)))))
8154 (c-add-syntax 'objc-method-args-cont placeholder))
8155
8156 ;; CASE 5L: we are at the first argument of a template
8157 ;; arglist that begins on the previous line.
8158 ((and c-recognize-<>-arglists
8159 (eq (char-before) ?<)
8160 (not (and c-overloadable-operators-regexp
8161 (c-after-special-operator-id lim))))
8162 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8163 (c-add-syntax 'template-args-cont (c-point 'boi)))
8164
8165 ;; CASE 5M: we are at a topmost continuation line
8166 (t
8167 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8168 (when (c-major-mode-is 'objc-mode)
8169 (setq placeholder (point))
8170 (while (and (c-forward-objc-directive)
8171 (< (point) indent-point))
8172 (c-forward-syntactic-ws)
8173 (setq placeholder (point)))
8174 (goto-char placeholder))
8175 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8176 ))
8177
8178 ;; (CASE 6 has been removed.)
8179
8180 ;; CASE 7: line is an expression, not a statement. Most
8181 ;; likely we are either in a function prototype or a function
8182 ;; call argument list
8183 ((not (or (and c-special-brace-lists
8184 (save-excursion
8185 (goto-char containing-sexp)
8186 (c-looking-at-special-brace-list)))
8187 (eq (char-after containing-sexp) ?{)))
8188 (cond
8189
8190 ;; CASE 7A: we are looking at the arglist closing paren.
8191 ;; C.f. case 7F.
8192 ((memq char-after-ip '(?\) ?\]))
8193 (goto-char containing-sexp)
8194 (setq placeholder (c-point 'boi))
8195 (if (and (c-safe (backward-up-list 1) t)
8196 (>= (point) placeholder))
8197 (progn
8198 (forward-char)
8199 (skip-chars-forward " \t"))
8200 (goto-char placeholder))
8201 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
8202 (c-most-enclosing-brace paren-state (point))
8203 paren-state))
8204
8205 ;; CASE 7B: Looking at the opening brace of an
8206 ;; in-expression block or brace list. C.f. cases 4, 16A
8207 ;; and 17E.
8208 ((and (eq char-after-ip ?{)
8209 (progn
8210 (setq placeholder (c-inside-bracelist-p (point)
8211 paren-state))
8212 (if placeholder
8213 (setq tmpsymbol '(brace-list-open . inexpr-class))
8214 (setq tmpsymbol '(block-open . inexpr-statement)
8215 placeholder
8216 (cdr-safe (c-looking-at-inexpr-block
8217 (c-safe-position containing-sexp
8218 paren-state)
8219 containing-sexp)))
8220 ;; placeholder is nil if it's a block directly in
8221 ;; a function arglist. That makes us skip out of
8222 ;; this case.
8223 )))
8224 (goto-char placeholder)
8225 (back-to-indentation)
8226 (c-add-stmt-syntax (car tmpsymbol) nil t
8227 (c-most-enclosing-brace paren-state (point))
8228 paren-state)
8229 (if (/= (point) placeholder)
8230 (c-add-syntax (cdr tmpsymbol))))
8231
8232 ;; CASE 7C: we are looking at the first argument in an empty
8233 ;; argument list. Use arglist-close if we're actually
8234 ;; looking at a close paren or bracket.
8235 ((memq char-before-ip '(?\( ?\[))
8236 (goto-char containing-sexp)
8237 (setq placeholder (c-point 'boi))
8238 (if (and (c-safe (backward-up-list 1) t)
8239 (>= (point) placeholder))
8240 (progn
8241 (forward-char)
8242 (skip-chars-forward " \t"))
8243 (goto-char placeholder))
8244 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
8245 (c-most-enclosing-brace paren-state (point))
8246 paren-state))
8247
8248 ;; CASE 7D: we are inside a conditional test clause. treat
8249 ;; these things as statements
8250 ((progn
8251 (goto-char containing-sexp)
8252 (and (c-safe (c-forward-sexp -1) t)
8253 (looking-at "\\<for\\>[^_]")))
8254 (goto-char (1+ containing-sexp))
8255 (c-forward-syntactic-ws indent-point)
8256 (if (eq char-before-ip ?\;)
8257 (c-add-syntax 'statement (point))
8258 (c-add-syntax 'statement-cont (point))
8259 ))
8260
8261 ;; CASE 7E: maybe a continued ObjC method call. This is the
8262 ;; case when we are inside a [] bracketed exp, and what
8263 ;; precede the opening bracket is not an identifier.
8264 ((and c-opt-method-key
8265 (eq (char-after containing-sexp) ?\[)
8266 (progn
8267 (goto-char (1- containing-sexp))
8268 (c-backward-syntactic-ws (c-point 'bod))
8269 (if (not (looking-at c-symbol-key))
8270 (c-add-syntax 'objc-method-call-cont containing-sexp))
8271 )))
8272
8273 ;; CASE 7F: we are looking at an arglist continuation line,
8274 ;; but the preceding argument is on the same line as the
8275 ;; opening paren. This case includes multi-line
8276 ;; mathematical paren groupings, but we could be on a
8277 ;; for-list continuation line. C.f. case 7A.
8278 ((progn
8279 (goto-char (1+ containing-sexp))
8280 (< (save-excursion
8281 (c-forward-syntactic-ws)
8282 (point))
8283 (c-point 'bonl)))
8284 (goto-char containing-sexp)
8285 (setq placeholder (c-point 'boi))
8286 (if (and (c-safe (backward-up-list 1) t)
8287 (>= (point) placeholder))
8288 (progn
8289 (forward-char)
8290 (skip-chars-forward " \t"))
8291 (goto-char placeholder))
8292 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
8293 (c-most-enclosing-brace c-state-cache (point))
8294 paren-state))
8295
8296 ;; CASE 7G: we are looking at just a normal arglist
8297 ;; continuation line
8298 (t (c-forward-syntactic-ws indent-point)
8299 (c-add-syntax 'arglist-cont (c-point 'boi)))
8300 ))
8301
8302 ;; CASE 8: func-local multi-inheritance line
8303 ((and (c-major-mode-is 'c++-mode)
8304 (save-excursion
8305 (goto-char indent-point)
8306 (skip-chars-forward " \t")
8307 (looking-at c-opt-postfix-decl-spec-key)))
8308 (goto-char indent-point)
8309 (skip-chars-forward " \t")
8310 (cond
8311
8312 ;; CASE 8A: non-hanging colon on an inher intro
8313 ((eq char-after-ip ?:)
8314 (c-backward-syntactic-ws lim)
8315 (c-add-syntax 'inher-intro (c-point 'boi)))
8316
8317 ;; CASE 8B: hanging colon on an inher intro
8318 ((eq char-before-ip ?:)
8319 (c-add-syntax 'inher-intro (c-point 'boi)))
8320
8321 ;; CASE 8C: a continued inheritance line
8322 (t
8323 (c-beginning-of-inheritance-list lim)
8324 (c-add-syntax 'inher-cont (point))
8325 )))
8326
8327 ;; CASE 9: we are inside a brace-list
8328 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
8329 (setq special-brace-list
8330 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
8331 (save-excursion
8332 (goto-char containing-sexp)
8333 (c-looking-at-special-brace-list)))
8334 (c-inside-bracelist-p containing-sexp paren-state))))
8335 (cond
8336
8337 ;; CASE 9A: In the middle of a special brace list opener.
8338 ((and (consp special-brace-list)
8339 (save-excursion
8340 (goto-char containing-sexp)
8341 (eq (char-after) ?\())
8342 (eq char-after-ip (car (cdr special-brace-list))))
8343 (goto-char (car (car special-brace-list)))
8344 (skip-chars-backward " \t")
8345 (if (and (bolp)
8346 (assoc 'statement-cont
8347 (setq placeholder (c-guess-basic-syntax))))
8348 (setq c-syntactic-context placeholder)
8349 (c-beginning-of-statement-1
8350 (c-safe-position (1- containing-sexp) paren-state))
8351 (c-forward-token-2 0)
8352 (while (looking-at c-specifier-key)
8353 (goto-char (match-end 1))
8354 (c-forward-syntactic-ws))
8355 (c-add-syntax 'brace-list-open (c-point 'boi))))
8356
8357 ;; CASE 9B: brace-list-close brace
8358 ((if (consp special-brace-list)
8359 ;; Check special brace list closer.
8360 (progn
8361 (goto-char (car (car special-brace-list)))
8362 (save-excursion
8363 (goto-char indent-point)
8364 (back-to-indentation)
8365 (or
8366 ;; We were between the special close char and the `)'.
8367 (and (eq (char-after) ?\))
8368 (eq (1+ (point)) (cdr (car special-brace-list))))
8369 ;; We were before the special close char.
8370 (and (eq (char-after) (cdr (cdr special-brace-list)))
8371 (zerop (c-forward-token-2))
8372 (eq (1+ (point)) (cdr (car special-brace-list)))))))
8373 ;; Normal brace list check.
8374 (and (eq char-after-ip ?})
8375 (c-safe (goto-char (c-up-list-backward (point))) t)
8376 (= (point) containing-sexp)))
8377 (if (eq (point) (c-point 'boi))
8378 (c-add-syntax 'brace-list-close (point))
8379 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8380 (c-beginning-of-statement-1 lim)
8381 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
8382
8383 (t
8384 ;; Prepare for the rest of the cases below by going to the
8385 ;; token following the opening brace
8386 (if (consp special-brace-list)
8387 (progn
8388 (goto-char (car (car special-brace-list)))
8389 (c-forward-token-2 1 nil indent-point))
8390 (goto-char containing-sexp))
8391 (forward-char)
8392 (let ((start (point)))
8393 (c-forward-syntactic-ws indent-point)
8394 (goto-char (max start (c-point 'bol))))
8395 (c-skip-ws-forward indent-point)
8396 (cond
8397
8398 ;; CASE 9C: we're looking at the first line in a brace-list
8399 ((= (point) indent-point)
8400 (if (consp special-brace-list)
8401 (goto-char (car (car special-brace-list)))
8402 (goto-char containing-sexp))
8403 (if (eq (point) (c-point 'boi))
8404 (c-add-syntax 'brace-list-intro (point))
8405 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8406 (c-beginning-of-statement-1 lim)
8407 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
8408
8409 ;; CASE 9D: this is just a later brace-list-entry or
8410 ;; brace-entry-open
8411 (t (if (or (eq char-after-ip ?{)
8412 (and c-special-brace-lists
8413 (save-excursion
8414 (goto-char indent-point)
8415 (c-forward-syntactic-ws (c-point 'eol))
8416 (c-looking-at-special-brace-list (point)))))
8417 (c-add-syntax 'brace-entry-open (point))
8418 (c-add-syntax 'brace-list-entry (point))
8419 ))
8420 ))))
8421
8422 ;; CASE 10: A continued statement or top level construct.
8423 ((and (not (memq char-before-ip '(?\; ?:)))
8424 (not (c-at-vsemi-p before-ws-ip))
8425 (or (not (eq char-before-ip ?}))
8426 (c-looking-at-inexpr-block-backward c-state-cache))
8427 (> (point)
8428 (save-excursion
8429 (c-beginning-of-statement-1 containing-sexp)
8430 (setq placeholder (point))))
8431 (/= placeholder containing-sexp))
8432 ;; This is shared with case 18.
8433 (c-guess-continued-construct indent-point
8434 char-after-ip
8435 placeholder
8436 containing-sexp
8437 paren-state))
8438
8439 ;; CASE 16: block close brace, possibly closing the defun or
8440 ;; the class
8441 ((eq char-after-ip ?})
8442 ;; From here on we have the next containing sexp in lim.
8443 (setq lim (c-most-enclosing-brace paren-state))
8444 (goto-char containing-sexp)
8445 (cond
8446
8447 ;; CASE 16E: Closing a statement block? This catches
8448 ;; cases where it's preceded by a statement keyword,
8449 ;; which works even when used in an "invalid" context,
8450 ;; e.g. a macro argument.
8451 ((c-after-conditional)
8452 (c-backward-to-block-anchor lim)
8453 (c-add-stmt-syntax 'block-close nil t lim paren-state))
8454
8455 ;; CASE 16A: closing a lambda defun or an in-expression
8456 ;; block? C.f. cases 4, 7B and 17E.
8457 ((setq placeholder (c-looking-at-inexpr-block
8458 (c-safe-position containing-sexp paren-state)
8459 nil))
8460 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8461 'inline-close
8462 'block-close))
8463 (goto-char containing-sexp)
8464 (back-to-indentation)
8465 (if (= containing-sexp (point))
8466 (c-add-syntax tmpsymbol (point))
8467 (goto-char (cdr placeholder))
8468 (back-to-indentation)
8469 (c-add-stmt-syntax tmpsymbol nil t
8470 (c-most-enclosing-brace paren-state (point))
8471 paren-state)
8472 (if (/= (point) (cdr placeholder))
8473 (c-add-syntax (car placeholder)))))
8474
8475 ;; CASE 16B: does this close an inline or a function in
8476 ;; a non-class declaration level block?
8477 ((save-excursion
8478 (and lim
8479 (progn
8480 (goto-char lim)
8481 (c-looking-at-decl-block
8482 (c-most-enclosing-brace paren-state lim)
8483 nil))
8484 (setq placeholder (point))))
8485 (c-backward-to-decl-anchor lim)
8486 (back-to-indentation)
8487 (if (save-excursion
8488 (goto-char placeholder)
8489 (looking-at c-other-decl-block-key))
8490 (c-add-syntax 'defun-close (point))
8491 (c-add-syntax 'inline-close (point))))
8492
8493 ;; CASE 16F: Can be a defun-close of a function declared
8494 ;; in a statement block, e.g. in Pike or when using gcc
8495 ;; extensions, but watch out for macros followed by
8496 ;; blocks. Let it through to be handled below.
8497 ;; C.f. cases B.3 and 17G.
8498 ((save-excursion
8499 (and (not (c-at-statement-start-p))
8500 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8501 (setq placeholder (point))
8502 (let ((c-recognize-typeless-decls nil))
8503 ;; Turn off recognition of constructs that
8504 ;; lacks a type in this case, since that's more
8505 ;; likely to be a macro followed by a block.
8506 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8507 (back-to-indentation)
8508 (if (/= (point) containing-sexp)
8509 (goto-char placeholder))
8510 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
8511
8512 ;; CASE 16C: If there is an enclosing brace then this is
8513 ;; a block close since defun closes inside declaration
8514 ;; level blocks have been handled above.
8515 (lim
8516 ;; If the block is preceded by a case/switch label on
8517 ;; the same line, we anchor at the first preceding label
8518 ;; at boi. The default handling in c-add-stmt-syntax
8519 ;; really fixes it better, but we do like this to keep
8520 ;; the indentation compatible with version 5.28 and
8521 ;; earlier. C.f. case 17H.
8522 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8523 (eq (c-beginning-of-statement-1 lim) 'label)))
8524 (goto-char placeholder)
8525 (if (looking-at c-label-kwds-regexp)
8526 (c-add-syntax 'block-close (point))
8527 (goto-char containing-sexp)
8528 ;; c-backward-to-block-anchor not necessary here; those
8529 ;; situations are handled in case 16E above.
8530 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
8531
8532 ;; CASE 16D: Only top level defun close left.
8533 (t
8534 (goto-char containing-sexp)
8535 (c-backward-to-decl-anchor lim)
8536 (c-add-stmt-syntax 'defun-close nil nil
8537 (c-most-enclosing-brace paren-state)
8538 paren-state))
8539 ))
8540
8541 ;; CASE 17: Statement or defun catchall.
8542 (t
8543 (goto-char indent-point)
8544 ;; Back up statements until we find one that starts at boi.
8545 (while (let* ((prev-point (point))
8546 (last-step-type (c-beginning-of-statement-1
8547 containing-sexp)))
8548 (if (= (point) prev-point)
8549 (progn
8550 (setq step-type (or step-type last-step-type))
8551 nil)
8552 (setq step-type last-step-type)
8553 (/= (point) (c-point 'boi)))))
8554 (cond
8555
8556 ;; CASE 17B: continued statement
8557 ((and (eq step-type 'same)
8558 (/= (point) indent-point))
8559 (c-add-stmt-syntax 'statement-cont nil nil
8560 containing-sexp paren-state))
8561
8562 ;; CASE 17A: After a case/default label?
8563 ((progn
8564 (while (and (eq step-type 'label)
8565 (not (looking-at c-label-kwds-regexp)))
8566 (setq step-type
8567 (c-beginning-of-statement-1 containing-sexp)))
8568 (eq step-type 'label))
8569 (c-add-stmt-syntax (if (eq char-after-ip ?{)
8570 'statement-case-open
8571 'statement-case-intro)
8572 nil t containing-sexp paren-state))
8573
8574 ;; CASE 17D: any old statement
8575 ((progn
8576 (while (eq step-type 'label)
8577 (setq step-type
8578 (c-beginning-of-statement-1 containing-sexp)))
8579 (eq step-type 'previous))
8580 (c-add-stmt-syntax 'statement nil t
8581 containing-sexp paren-state)
8582 (if (eq char-after-ip ?{)
8583 (c-add-syntax 'block-open)))
8584
8585 ;; CASE 17I: Inside a substatement block.
8586 ((progn
8587 ;; The following tests are all based on containing-sexp.
8588 (goto-char containing-sexp)
8589 ;; From here on we have the next containing sexp in lim.
8590 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
8591 (c-after-conditional))
8592 (c-backward-to-block-anchor lim)
8593 (c-add-stmt-syntax 'statement-block-intro nil t
8594 lim paren-state)
8595 (if (eq char-after-ip ?{)
8596 (c-add-syntax 'block-open)))
8597
8598 ;; CASE 17E: first statement in an in-expression block.
8599 ;; C.f. cases 4, 7B and 16A.
8600 ((setq placeholder (c-looking-at-inexpr-block
8601 (c-safe-position containing-sexp paren-state)
8602 nil))
8603 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8604 'defun-block-intro
8605 'statement-block-intro))
8606 (back-to-indentation)
8607 (if (= containing-sexp (point))
8608 (c-add-syntax tmpsymbol (point))
8609 (goto-char (cdr placeholder))
8610 (back-to-indentation)
8611 (c-add-stmt-syntax tmpsymbol nil t
8612 (c-most-enclosing-brace c-state-cache (point))
8613 paren-state)
8614 (if (/= (point) (cdr placeholder))
8615 (c-add-syntax (car placeholder))))
8616 (if (eq char-after-ip ?{)
8617 (c-add-syntax 'block-open)))
8618
8619 ;; CASE 17F: first statement in an inline, or first
8620 ;; statement in a top-level defun. we can tell this is it
8621 ;; if there are no enclosing braces that haven't been
8622 ;; narrowed out by a class (i.e. don't use bod here).
8623 ((save-excursion
8624 (or (not (setq placeholder (c-most-enclosing-brace
8625 paren-state)))
8626 (and (progn
8627 (goto-char placeholder)
8628 (eq (char-after) ?{))
8629 (c-looking-at-decl-block (c-most-enclosing-brace
8630 paren-state (point))
8631 nil))))
8632 (c-backward-to-decl-anchor lim)
8633 (back-to-indentation)
8634 (c-add-syntax 'defun-block-intro (point)))
8635
8636 ;; CASE 17G: First statement in a function declared inside
8637 ;; a normal block. This can occur in Pike and with
8638 ;; e.g. the gcc extensions, but watch out for macros
8639 ;; followed by blocks. C.f. cases B.3 and 16F.
8640 ((save-excursion
8641 (and (not (c-at-statement-start-p))
8642 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8643 (setq placeholder (point))
8644 (let ((c-recognize-typeless-decls nil))
8645 ;; Turn off recognition of constructs that lacks
8646 ;; a type in this case, since that's more likely
8647 ;; to be a macro followed by a block.
8648 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8649 (back-to-indentation)
8650 (if (/= (point) containing-sexp)
8651 (goto-char placeholder))
8652 (c-add-stmt-syntax 'defun-block-intro nil t
8653 lim paren-state))
8654
8655 ;; CASE 17H: First statement in a block.
8656 (t
8657 ;; If the block is preceded by a case/switch label on the
8658 ;; same line, we anchor at the first preceding label at
8659 ;; boi. The default handling in c-add-stmt-syntax is
8660 ;; really fixes it better, but we do like this to keep the
8661 ;; indentation compatible with version 5.28 and earlier.
8662 ;; C.f. case 16C.
8663 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8664 (eq (c-beginning-of-statement-1 lim) 'label)))
8665 (goto-char placeholder)
8666 (if (looking-at c-label-kwds-regexp)
8667 (c-add-syntax 'statement-block-intro (point))
8668 (goto-char containing-sexp)
8669 ;; c-backward-to-block-anchor not necessary here; those
8670 ;; situations are handled in case 17I above.
8671 (c-add-stmt-syntax 'statement-block-intro nil t
8672 lim paren-state))
8673 (if (eq char-after-ip ?{)
8674 (c-add-syntax 'block-open)))
8675 ))
8676 )
8677
8678 ;; now we need to look at any modifiers
8679 (goto-char indent-point)
8680 (skip-chars-forward " \t")
8681
8682 ;; are we looking at a comment only line?
8683 (when (and (looking-at c-comment-start-regexp)
8684 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
8685 (c-append-syntax 'comment-intro))
8686
8687 ;; we might want to give additional offset to friends (in C++).
8688 (when (and c-opt-friend-key
8689 (looking-at c-opt-friend-key))
8690 (c-append-syntax 'friend))
8691
8692 ;; Set syntactic-relpos.
8693 (let ((p c-syntactic-context))
8694 (while (and p
8695 (if (integerp (c-langelem-pos (car p)))
8696 (progn
8697 (setq syntactic-relpos (c-langelem-pos (car p)))
8698 nil)
8699 t))
8700 (setq p (cdr p))))
8701
8702 ;; Start of or a continuation of a preprocessor directive?
8703 (if (and macro-start
8704 (eq macro-start (c-point 'boi))
8705 (not (and (c-major-mode-is 'pike-mode)
8706 (eq (char-after (1+ macro-start)) ?\"))))
8707 (c-append-syntax 'cpp-macro)
8708 (when (and c-syntactic-indentation-in-macros macro-start)
8709 (if in-macro-expr
8710 (when (or
8711 (< syntactic-relpos macro-start)
8712 (not (or
8713 (assq 'arglist-intro c-syntactic-context)
8714 (assq 'arglist-cont c-syntactic-context)
8715 (assq 'arglist-cont-nonempty c-syntactic-context)
8716 (assq 'arglist-close c-syntactic-context))))
8717 ;; If inside a cpp expression, i.e. anywhere in a
8718 ;; cpp directive except a #define body, we only let
8719 ;; through the syntactic analysis that is internal
8720 ;; in the expression. That means the arglist
8721 ;; elements, if they are anchored inside the cpp
8722 ;; expression.
8723 (setq c-syntactic-context nil)
8724 (c-add-syntax 'cpp-macro-cont macro-start))
8725 (when (and (eq macro-start syntactic-relpos)
8726 (not (assq 'cpp-define-intro c-syntactic-context))
8727 (save-excursion
8728 (goto-char macro-start)
8729 (or (not (c-forward-to-cpp-define-body))
8730 (<= (point) (c-point 'boi indent-point)))))
8731 ;; Inside a #define body and the syntactic analysis is
8732 ;; anchored on the start of the #define. In this case
8733 ;; we add cpp-define-intro to get the extra
8734 ;; indentation of the #define body.
8735 (c-add-syntax 'cpp-define-intro)))))
8736
8737 ;; return the syntax
8738 c-syntactic-context)))
8739
8740 \f
8741 ;; Indentation calculation.
8742
8743 (defun c-evaluate-offset (offset langelem symbol)
8744 ;; offset can be a number, a function, a variable, a list, or one of
8745 ;; the symbols + or -
8746 ;;
8747 ;; This function might do hidden buffer changes.
8748 (let ((res
8749 (cond
8750 ((numberp offset) offset)
8751 ((vectorp offset) offset)
8752 ((null offset) nil)
8753
8754 ((eq offset '+) c-basic-offset)
8755 ((eq offset '-) (- c-basic-offset))
8756 ((eq offset '++) (* 2 c-basic-offset))
8757 ((eq offset '--) (* 2 (- c-basic-offset)))
8758 ((eq offset '*) (/ c-basic-offset 2))
8759 ((eq offset '/) (/ (- c-basic-offset) 2))
8760
8761 ((functionp offset)
8762 (c-evaluate-offset
8763 (funcall offset
8764 (cons (c-langelem-sym langelem)
8765 (c-langelem-pos langelem)))
8766 langelem symbol))
8767
8768 ((listp offset)
8769 (cond
8770 ((eq (car offset) 'quote)
8771 (c-benign-error "The offset %S for %s was mistakenly quoted"
8772 offset symbol)
8773 nil)
8774
8775 ((memq (car offset) '(min max))
8776 (let (res val (method (car offset)))
8777 (setq offset (cdr offset))
8778 (while offset
8779 (setq val (c-evaluate-offset (car offset) langelem symbol))
8780 (cond
8781 ((not val))
8782 ((not res)
8783 (setq res val))
8784 ((integerp val)
8785 (if (vectorp res)
8786 (c-benign-error "\
8787 Error evaluating offset %S for %s: \
8788 Cannot combine absolute offset %S with relative %S in `%s' method"
8789 (car offset) symbol res val method)
8790 (setq res (funcall method res val))))
8791 (t
8792 (if (integerp res)
8793 (c-benign-error "\
8794 Error evaluating offset %S for %s: \
8795 Cannot combine relative offset %S with absolute %S in `%s' method"
8796 (car offset) symbol res val method)
8797 (setq res (vector (funcall method (aref res 0)
8798 (aref val 0)))))))
8799 (setq offset (cdr offset)))
8800 res))
8801
8802 ((eq (car offset) 'add)
8803 (let (res val)
8804 (setq offset (cdr offset))
8805 (while offset
8806 (setq val (c-evaluate-offset (car offset) langelem symbol))
8807 (cond
8808 ((not val))
8809 ((not res)
8810 (setq res val))
8811 ((integerp val)
8812 (if (vectorp res)
8813 (setq res (vector (+ (aref res 0) val)))
8814 (setq res (+ res val))))
8815 (t
8816 (if (vectorp res)
8817 (c-benign-error "\
8818 Error evaluating offset %S for %s: \
8819 Cannot combine absolute offsets %S and %S in `add' method"
8820 (car offset) symbol res val)
8821 (setq res val)))) ; Override.
8822 (setq offset (cdr offset)))
8823 res))
8824
8825 (t
8826 (let (res)
8827 (when (eq (car offset) 'first)
8828 (setq offset (cdr offset)))
8829 (while (and (not res) offset)
8830 (setq res (c-evaluate-offset (car offset) langelem symbol)
8831 offset (cdr offset)))
8832 res))))
8833
8834 ((and (symbolp offset) (boundp offset))
8835 (symbol-value offset))
8836
8837 (t
8838 (c-benign-error "Unknown offset format %S for %s" offset symbol)
8839 nil))))
8840
8841 (if (or (null res) (integerp res)
8842 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
8843 res
8844 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
8845 offset symbol res)
8846 nil)))
8847
8848 (defun c-calc-offset (langelem)
8849 ;; Get offset from LANGELEM which is a list beginning with the
8850 ;; syntactic symbol and followed by any analysis data it provides.
8851 ;; That data may be zero or more elements, but if at least one is
8852 ;; given then the first is the anchor position (or nil). The symbol
8853 ;; is matched against `c-offsets-alist' and the offset calculated
8854 ;; from that is returned.
8855 ;;
8856 ;; This function might do hidden buffer changes.
8857 (let* ((symbol (c-langelem-sym langelem))
8858 (match (assq symbol c-offsets-alist))
8859 (offset (cdr-safe match)))
8860 (if match
8861 (setq offset (c-evaluate-offset offset langelem symbol))
8862 (if c-strict-syntax-p
8863 (c-benign-error "No offset found for syntactic symbol %s" symbol))
8864 (setq offset 0))
8865 (if (vectorp offset)
8866 offset
8867 (or (and (numberp offset) offset)
8868 (and (symbolp offset) (symbol-value offset))
8869 0))
8870 ))
8871
8872 (defun c-get-offset (langelem)
8873 ;; This is a compatibility wrapper for `c-calc-offset' in case
8874 ;; someone is calling it directly. It takes an old style syntactic
8875 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
8876 ;; new list form.
8877 ;;
8878 ;; This function might do hidden buffer changes.
8879 (if (c-langelem-pos langelem)
8880 (c-calc-offset (list (c-langelem-sym langelem)
8881 (c-langelem-pos langelem)))
8882 (c-calc-offset langelem)))
8883
8884 (defun c-get-syntactic-indentation (langelems)
8885 ;; Calculate the syntactic indentation from a syntactic description
8886 ;; as returned by `c-guess-syntax'.
8887 ;;
8888 ;; Note that topmost-intro always has an anchor position at bol, for
8889 ;; historical reasons. It's often used together with other symbols
8890 ;; that has more sane positions. Since we always use the first
8891 ;; found anchor position, we rely on that these other symbols always
8892 ;; precede topmost-intro in the LANGELEMS list.
8893 ;;
8894 ;; This function might do hidden buffer changes.
8895 (let ((indent 0) anchor)
8896
8897 (while langelems
8898 (let* ((c-syntactic-element (car langelems))
8899 (res (c-calc-offset c-syntactic-element)))
8900
8901 (if (vectorp res)
8902 ;; Got an absolute column that overrides any indentation
8903 ;; we've collected so far, but not the relative
8904 ;; indentation we might get for the nested structures
8905 ;; further down the langelems list.
8906 (setq indent (elt res 0)
8907 anchor (point-min)) ; A position at column 0.
8908
8909 ;; Got a relative change of the current calculated
8910 ;; indentation.
8911 (setq indent (+ indent res))
8912
8913 ;; Use the anchor position from the first syntactic
8914 ;; element with one.
8915 (unless anchor
8916 (setq anchor (c-langelem-pos (car langelems)))))
8917
8918 (setq langelems (cdr langelems))))
8919
8920 (if anchor
8921 (+ indent (save-excursion
8922 (goto-char anchor)
8923 (current-column)))
8924 indent)))
8925
8926 \f
8927 (cc-provide 'cc-engine)
8928
8929 ;;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
8930 ;;; cc-engine.el ends here