]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-engine.el
(c-guess-basic-syntax, CASE 5A.5): Anchor the "{" of a template function
[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, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
6
7 ;; Authors: 2001- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs and Stewart Clamen
11 ;; 1985 Richard M. Stallman
12 ;; Maintainer: bug-cc-mode@gnu.org
13 ;; Created: 22-Apr-1997 (split from cc-mode.el)
14 ;; Version: See cc-mode.el
15 ;; Keywords: c languages oop
16
17 ;; This file is part of GNU Emacs.
18
19 ;; GNU Emacs is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation; either version 3, or (at your option)
22 ;; any later version.
23
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
28
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with this program; see the file COPYING. If not, write to
31 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 ;; Boston, MA 02110-1301, USA.
33
34 ;;; Commentary:
35
36 ;; The functions which have docstring documentation can be considered
37 ;; part of an API which other packages can use in CC Mode buffers.
38 ;; Otoh, undocumented functions and functions with the documentation
39 ;; in comments are considered purely internal and can change semantics
40 ;; or even disappear in the future.
41 ;;
42 ;; (This policy applies to CC Mode as a whole, not just this file. It
43 ;; probably also applies to many other Emacs packages, but here it's
44 ;; clearly spelled out.)
45
46 ;; Hidden buffer changes
47 ;;
48 ;; Various functions in CC Mode use text properties for caching and
49 ;; syntactic markup purposes, and those of them that might modify such
50 ;; properties but still don't modify the buffer in a visible way are
51 ;; said to do "hidden buffer changes". They should be used within
52 ;; `c-save-buffer-state' or a similar function that saves and restores
53 ;; buffer modifiedness, disables buffer change hooks, etc.
54 ;;
55 ;; Interactive functions are assumed to not do hidden buffer changes,
56 ;; except in the specific parts of them that do real changes.
57 ;;
58 ;; Lineup functions are assumed to do hidden buffer changes. They
59 ;; must not do real changes, though.
60 ;;
61 ;; All other functions that do hidden buffer changes have that noted
62 ;; in their doc string or comment.
63 ;;
64 ;; The intention with this system is to avoid wrapping every leaf
65 ;; function that do hidden buffer changes inside
66 ;; `c-save-buffer-state'. It should be used as near the top of the
67 ;; interactive functions as possible.
68 ;;
69 ;; Functions called during font locking are allowed to do hidden
70 ;; buffer changes since the font-lock package run them in a context
71 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
72 ;; inspired by `save-buffer-state' in the font-lock package).
73
74 ;; Use of text properties
75 ;;
76 ;; CC Mode uses several text properties internally to mark up various
77 ;; positions, e.g. to improve speed and to eliminate glitches in
78 ;; interactive refontification.
79 ;;
80 ;; Note: This doc is for internal use only. Other packages should not
81 ;; assume that these text properties are used as described here.
82 ;;
83 ;; 'syntax-table
84 ;; Used to modify the syntax of some characters. Currently used to
85 ;; mark the "<" and ">" of angle bracket parens with paren syntax.
86 ;;
87 ;; This property is used on single characters and is therefore
88 ;; always treated as front and rear nonsticky (or start and end open
89 ;; in XEmacs vocabulary). It's therefore installed on
90 ;; `text-property-default-nonsticky' if that variable exists (Emacs
91 ;; >= 21).
92 ;;
93 ;; 'c-is-sws and 'c-in-sws
94 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
95 ;; speed them up. See the comment blurb before `c-put-is-sws'
96 ;; below for further details.
97 ;;
98 ;; 'c-type
99 ;; This property is used on single characters to mark positions with
100 ;; special syntactic relevance of various sorts. Its primary use is
101 ;; to avoid glitches when multiline constructs are refontified
102 ;; interactively (on font lock decoration level 3). It's cleared in
103 ;; a region before it's fontified and is then put on relevant chars
104 ;; in that region as they are encountered during the fontification.
105 ;; The value specifies the kind of position:
106 ;;
107 ;; 'c-decl-arg-start
108 ;; Put on the last char of the token preceding each declaration
109 ;; inside a declaration style arglist (typically in a function
110 ;; prototype).
111 ;;
112 ;; 'c-decl-end
113 ;; Put on the last char of the token preceding a declaration.
114 ;; This is used in cases where declaration boundaries can't be
115 ;; recognized simply by looking for a token like ";" or "}".
116 ;; `c-type-decl-end-used' must be set if this is used (see also
117 ;; `c-find-decl-spots').
118 ;;
119 ;; 'c-<>-arg-sep
120 ;; Put on the commas that separate arguments in angle bracket
121 ;; arglists like C++ template arglists.
122 ;;
123 ;; 'c-decl-id-start and 'c-decl-type-start
124 ;; Put on the last char of the token preceding each declarator
125 ;; in the declarator list of a declaration. They are also used
126 ;; between the identifiers cases like enum declarations.
127 ;; 'c-decl-type-start is used when the declarators are types,
128 ;; 'c-decl-id-start otherwise.
129 ;;
130 ;; 'c-awk-NL-prop
131 ;; Used in AWK mode to mark the various kinds of newlines. See
132 ;; cc-awk.el.
133
134 ;;; Code:
135
136 (eval-when-compile
137 (let ((load-path
138 (if (and (boundp 'byte-compile-dest-file)
139 (stringp byte-compile-dest-file))
140 (cons (file-name-directory byte-compile-dest-file) load-path)
141 load-path)))
142 (load "cc-bytecomp" nil t)))
143
144 (cc-require 'cc-defs)
145 (cc-require-when-compile 'cc-langs)
146 (cc-require 'cc-vars)
147
148 ;; Silence the compiler.
149 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
150
151 \f
152 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
153
154 (defmacro c-declare-lang-variables ()
155 `(progn
156 ,@(apply 'nconc
157 (mapcar (lambda (init)
158 `(,(if (elt init 2)
159 `(defvar ,(car init) nil ,(elt init 2))
160 `(defvar ,(car init) nil))
161 (make-variable-buffer-local ',(car init))))
162 (cdr c-lang-variable-inits)))))
163 (c-declare-lang-variables)
164
165 \f
166 ;;; Internal state variables.
167
168 ;; Internal state of hungry delete key feature
169 (defvar c-hungry-delete-key nil)
170 (make-variable-buffer-local 'c-hungry-delete-key)
171
172 ;; The electric flag (toggled by `c-toggle-electric-state').
173 ;; If t, electric actions (like automatic reindentation, and (if
174 ;; c-auto-newline is also set) auto newlining) will happen when an electric
175 ;; key like `{' is pressed (or an electric keyword like `else').
176 (defvar c-electric-flag t)
177 (make-variable-buffer-local 'c-electric-flag)
178
179 ;; Internal state of auto newline feature.
180 (defvar c-auto-newline nil)
181 (make-variable-buffer-local 'c-auto-newline)
182
183 ;; Included in the mode line to indicate the active submodes.
184 ;; (defvar c-submode-indicators nil)
185 ;; (make-variable-buffer-local 'c-submode-indicators)
186
187 (defun c-calculate-state (arg prevstate)
188 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
189 ;; arg is nil or zero, toggle the state. If arg is negative, turn
190 ;; the state off, and if arg is positive, turn the state on
191 (if (or (not arg)
192 (zerop (setq arg (prefix-numeric-value arg))))
193 (not prevstate)
194 (> arg 0)))
195
196 ;; Dynamically bound cache for `c-in-literal'.
197 (defvar c-in-literal-cache t)
198
199 \f
200 ;; Basic handling of preprocessor directives.
201
202 ;; This is a dynamically bound cache used together with
203 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
204 ;; works as long as point doesn't cross a macro boundary.
205 (defvar c-macro-start 'unknown)
206
207 (defsubst c-query-and-set-macro-start ()
208 (if (symbolp c-macro-start)
209 (setq c-macro-start (save-excursion
210 (c-save-buffer-state ()
211 (and (c-beginning-of-macro)
212 (point)))))
213 c-macro-start))
214
215 (defsubst c-query-macro-start ()
216 (if (symbolp c-macro-start)
217 (save-excursion
218 (c-save-buffer-state ()
219 (and (c-beginning-of-macro)
220 (point))))
221 c-macro-start))
222
223 (defun c-beginning-of-macro (&optional lim)
224 "Go to the beginning of a preprocessor directive.
225 Leave point at the beginning of the directive and return t if in one,
226 otherwise return nil and leave point unchanged.
227
228 Note that this function might do hidden buffer changes. See the
229 comment at the start of cc-engine.el for more info."
230 (when c-opt-cpp-prefix
231 (let ((here (point)))
232 (save-restriction
233 (if lim (narrow-to-region lim (point-max)))
234 (beginning-of-line)
235 (while (eq (char-before (1- (point))) ?\\)
236 (forward-line -1))
237 (back-to-indentation)
238 (if (and (<= (point) here)
239 (looking-at c-opt-cpp-start))
240 t
241 (goto-char here)
242 nil)))))
243
244 (defun c-end-of-macro ()
245 "Go to the end of a preprocessor directive.
246 More accurately, move the point to the end of the closest following
247 line that doesn't end with a line continuation backslash - no check is
248 done that the point is inside a cpp directive to begin with.
249
250 Note that this function might do hidden buffer changes. See the
251 comment at the start of cc-engine.el for more info."
252 (while (progn
253 (end-of-line)
254 (when (and (eq (char-before) ?\\)
255 (not (eobp)))
256 (forward-char)
257 t))))
258
259 (defun c-forward-over-cpp-define-id ()
260 ;; Assuming point is at the "#" that introduces a preprocessor
261 ;; directive, it's moved forward to the end of the identifier which is
262 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
263 ;; is returned in this case, in all other cases nil is returned and
264 ;; point isn't moved.
265 ;;
266 ;; This function might do hidden buffer changes.
267 (when (and c-opt-cpp-macro-define-id
268 (looking-at c-opt-cpp-macro-define-id))
269 (goto-char (match-end 0))))
270
271 (defun c-forward-to-cpp-define-body ()
272 ;; Assuming point is at the "#" that introduces a preprocessor
273 ;; directive, it's moved forward to the start of the definition body
274 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
275 ;; specifies). Non-nil is returned in this case, in all other cases
276 ;; nil is returned and point isn't moved.
277 ;;
278 ;; This function might do hidden buffer changes.
279 (when (and c-opt-cpp-macro-define-start
280 (looking-at c-opt-cpp-macro-define-start)
281 (not (= (match-end 0) (c-point 'eol))))
282 (goto-char (match-end 0))))
283
284 \f
285 ;;; Basic utility functions.
286
287 (defun c-syntactic-content (from to paren-level)
288 ;; Return the given region as a string where all syntactic
289 ;; whitespace is removed or, where necessary, replaced with a single
290 ;; space. If PAREN-LEVEL is given then all parens in the region are
291 ;; collapsed to "()", "[]" etc.
292 ;;
293 ;; This function might do hidden buffer changes.
294
295 (save-excursion
296 (save-restriction
297 (narrow-to-region from to)
298 (goto-char from)
299 (let* ((parts (list nil)) (tail parts) pos in-paren)
300
301 (while (re-search-forward c-syntactic-ws-start to t)
302 (goto-char (setq pos (match-beginning 0)))
303 (c-forward-syntactic-ws)
304 (if (= (point) pos)
305 (forward-char)
306
307 (when paren-level
308 (save-excursion
309 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
310 pos (point))))
311
312 (if (and (> pos from)
313 (< (point) to)
314 (looking-at "\\w\\|\\s_")
315 (save-excursion
316 (goto-char (1- pos))
317 (looking-at "\\w\\|\\s_")))
318 (progn
319 (setcdr tail (list (buffer-substring-no-properties from pos)
320 " "))
321 (setq tail (cddr tail)))
322 (setcdr tail (list (buffer-substring-no-properties from pos)))
323 (setq tail (cdr tail)))
324
325 (when in-paren
326 (when (= (car (parse-partial-sexp pos to -1)) -1)
327 (setcdr tail (list (buffer-substring-no-properties
328 (1- (point)) (point))))
329 (setq tail (cdr tail))))
330
331 (setq from (point))))
332
333 (setcdr tail (list (buffer-substring-no-properties from to)))
334 (apply 'concat (cdr parts))))))
335
336 (defun c-shift-line-indentation (shift-amt)
337 ;; Shift the indentation of the current line with the specified
338 ;; amount (positive inwards). The buffer is modified only if
339 ;; SHIFT-AMT isn't equal to zero.
340 (let ((pos (- (point-max) (point)))
341 (c-macro-start c-macro-start)
342 tmp-char-inserted)
343 (if (zerop shift-amt)
344 nil
345 ;; If we're on an empty line inside a macro, we take the point
346 ;; to be at the current indentation and shift it to the
347 ;; appropriate column. This way we don't treat the extra
348 ;; whitespace out to the line continuation as indentation.
349 (when (and (c-query-and-set-macro-start)
350 (looking-at "[ \t]*\\\\$")
351 (save-excursion
352 (skip-chars-backward " \t")
353 (bolp)))
354 (insert ?x)
355 (backward-char)
356 (setq tmp-char-inserted t))
357 (unwind-protect
358 (let ((col (current-indentation)))
359 (delete-region (c-point 'bol) (c-point 'boi))
360 (beginning-of-line)
361 (indent-to (+ col shift-amt)))
362 (when tmp-char-inserted
363 (delete-char 1))))
364 ;; If initial point was within line's indentation and we're not on
365 ;; a line with a line continuation in a macro, position after the
366 ;; indentation. Else stay at same point in text.
367 (if (and (< (point) (c-point 'boi))
368 (not tmp-char-inserted))
369 (back-to-indentation)
370 (if (> (- (point-max) pos) (point))
371 (goto-char (- (point-max) pos))))))
372
373 (defsubst c-keyword-sym (keyword)
374 ;; Return non-nil if the string KEYWORD is a known keyword. More
375 ;; precisely, the value is the symbol for the keyword in
376 ;; `c-keywords-obarray'.
377 (intern-soft keyword c-keywords-obarray))
378
379 (defsubst c-keyword-member (keyword-sym lang-constant)
380 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
381 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
382 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
383 ;; nil then the result is nil.
384 (get keyword-sym lang-constant))
385
386 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
387 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
388 "\"|"
389 "\""))
390
391 ;; Regexp matching string limit syntax.
392 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
393 "\\s\"\\|\\s|"
394 "\\s\""))
395
396 ;; Regexp matching WS followed by string limit syntax.
397 (defconst c-ws*-string-limit-regexp
398 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
399
400 ;; Holds formatted error strings for the few cases where parse errors
401 ;; are reported.
402 (defvar c-parsing-error nil)
403 (make-variable-buffer-local 'c-parsing-error)
404
405 (defun c-echo-parsing-error (&optional quiet)
406 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
407 (c-benign-error "%s" c-parsing-error))
408 c-parsing-error)
409
410 ;; Faces given to comments and string literals. This is used in some
411 ;; situations to speed up recognition; it isn't mandatory that font
412 ;; locking is in use. This variable is extended with the face in
413 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
414 (defvar c-literal-faces
415 (append '(font-lock-comment-face font-lock-string-face)
416 (when (facep 'font-lock-comment-delimiter-face)
417 ;; New in Emacs 22.
418 '(font-lock-comment-delimiter-face))))
419
420 (defsubst c-put-c-type-property (pos value)
421 ;; Put a c-type property with the given value at POS.
422 (c-put-char-property pos 'c-type value))
423
424 (defun c-clear-c-type-property (from to value)
425 ;; Remove all occurences of the c-type property that has the given
426 ;; value in the region between FROM and TO. VALUE is assumed to not
427 ;; be nil.
428 ;;
429 ;; Note: This assumes that c-type is put on single chars only; it's
430 ;; very inefficient if matching properties cover large regions.
431 (save-excursion
432 (goto-char from)
433 (while (progn
434 (when (eq (get-text-property (point) 'c-type) value)
435 (c-clear-char-property (point) 'c-type))
436 (goto-char (next-single-property-change (point) 'c-type nil to))
437 (< (point) to)))))
438
439 \f
440 ;; Some debug tools to visualize various special positions. This
441 ;; debug code isn't as portable as the rest of CC Mode.
442
443 (cc-bytecomp-defun overlays-in)
444 (cc-bytecomp-defun overlay-get)
445 (cc-bytecomp-defun overlay-start)
446 (cc-bytecomp-defun overlay-end)
447 (cc-bytecomp-defun delete-overlay)
448 (cc-bytecomp-defun overlay-put)
449 (cc-bytecomp-defun make-overlay)
450
451 (defun c-debug-add-face (beg end face)
452 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
453 (while overlays
454 (setq overlay (car overlays)
455 overlays (cdr overlays))
456 (when (eq (overlay-get overlay 'face) face)
457 (setq beg (min beg (overlay-start overlay))
458 end (max end (overlay-end overlay)))
459 (delete-overlay overlay)))
460 (overlay-put (make-overlay beg end) 'face face)))
461
462 (defun c-debug-remove-face (beg end face)
463 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
464 (ol-beg beg) (ol-end end))
465 (while overlays
466 (setq overlay (car overlays)
467 overlays (cdr overlays))
468 (when (eq (overlay-get overlay 'face) face)
469 (setq ol-beg (min ol-beg (overlay-start overlay))
470 ol-end (max ol-end (overlay-end overlay)))
471 (delete-overlay overlay)))
472 (when (< ol-beg beg)
473 (overlay-put (make-overlay ol-beg beg) 'face face))
474 (when (> ol-end end)
475 (overlay-put (make-overlay end ol-end) 'face face))))
476
477 \f
478 ;; `c-beginning-of-statement-1' and accompanying stuff.
479
480 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
481 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
482 ;; better way should be implemented, but this will at least shut up
483 ;; the byte compiler.
484 (defvar c-maybe-labelp)
485
486 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
487
488 ;; Macros used internally in c-beginning-of-statement-1 for the
489 ;; automaton actions.
490 (defmacro c-bos-push-state ()
491 '(setq stack (cons (cons state saved-pos)
492 stack)))
493 (defmacro c-bos-pop-state (&optional do-if-done)
494 `(if (setq state (car (car stack))
495 saved-pos (cdr (car stack))
496 stack (cdr stack))
497 t
498 ,do-if-done
499 (throw 'loop nil)))
500 (defmacro c-bos-pop-state-and-retry ()
501 '(throw 'loop (setq state (car (car stack))
502 saved-pos (cdr (car stack))
503 ;; Throw nil if stack is empty, else throw non-nil.
504 stack (cdr stack))))
505 (defmacro c-bos-save-pos ()
506 '(setq saved-pos (vector pos tok ptok pptok)))
507 (defmacro c-bos-restore-pos ()
508 '(unless (eq (elt saved-pos 0) start)
509 (setq pos (elt saved-pos 0)
510 tok (elt saved-pos 1)
511 ptok (elt saved-pos 2)
512 pptok (elt saved-pos 3))
513 (goto-char pos)
514 (setq sym nil)))
515 (defmacro c-bos-save-error-info (missing got)
516 `(setq saved-pos (vector pos ,missing ,got)))
517 (defmacro c-bos-report-error ()
518 '(unless noerror
519 (setq c-parsing-error
520 (format "No matching `%s' found for `%s' on line %d"
521 (elt saved-pos 1)
522 (elt saved-pos 2)
523 (1+ (count-lines (point-min)
524 (c-point 'bol (elt saved-pos 0))))))))
525
526 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
527 noerror comma-delim)
528 "Move to the start of the current statement or declaration, or to
529 the previous one if already at the beginning of one. Only
530 statements/declarations on the same level are considered, i.e. don't
531 move into or out of sexps (not even normal expression parentheses).
532
533 Stop at statement continuation tokens like \"else\", \"catch\",
534 \"finally\" and the \"while\" in \"do ... while\" if the start point
535 is within the continuation. If starting at such a token, move to the
536 corresponding statement start. If at the beginning of a statement,
537 move to the closest containing statement if there is any. This might
538 also stop at a continuation clause.
539
540 Labels are treated as part of the following statements if
541 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
542 statement start keyword.) Otherwise, each label is treated as a
543 separate statement.
544
545 Macros are ignored \(i.e. skipped over) unless point is within one, in
546 which case the content of the macro is treated as normal code. Aside
547 from any normal statement starts found in it, stop at the first token
548 of the content in the macro, i.e. the expression of an \"#if\" or the
549 start of the definition in a \"#define\". Also stop at start of
550 macros before leaving them.
551
552 Return 'label if stopped at a label, 'same if stopped at the beginning
553 of the current statement, 'up if stepped to a containing statement,
554 'previous if stepped to a preceding statement, 'beginning if stepped
555 from a statement continuation clause to its start clause, or 'macro if
556 stepped to a macro start. Note that 'same and not 'label is returned
557 if stopped at the same label without crossing the colon character.
558
559 LIM may be given to limit the search. If the search hits the limit,
560 point will be left at the closest following token, or at the start
561 position if that is less ('same is returned in this case).
562
563 NOERROR turns off error logging to `c-parsing-error'.
564
565 Normally only ';' and virtual semicolons are considered to delimit
566 statements, but if COMMA-DELIM is non-nil then ',' is treated
567 as a delimiter too.
568
569 Note that this function might do hidden buffer changes. See the
570 comment at the start of cc-engine.el for more info."
571
572 ;; The bulk of this function is a pushdown automaton that looks at statement
573 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
574 ;; purpose is to keep track of nested statements, ensuring that such
575 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
576 ;; does with nested braces/brackets/parentheses).
577 ;;
578 ;; Note: The position of a boundary is the following token.
579 ;;
580 ;; Beginning with the current token (the one following point), move back one
581 ;; sexp at a time (where a sexp is, more or less, either a token or the
582 ;; entire contents of a brace/bracket/paren pair). Each time a statement
583 ;; boundary is crossed or a "while"-like token is found, update the state of
584 ;; the PDA. Stop at the beginning of a statement when the stack (holding
585 ;; nested statement info) is empty and the position has been moved.
586 ;;
587 ;; The following variables constitute the PDA:
588 ;;
589 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
590 ;; scanned back over, 'boundary if we've just gone back over a
591 ;; statement boundary, or nil otherwise.
592 ;; state: takes one of the values (nil else else-boundary while
593 ;; while-boundary catch catch-boundary).
594 ;; nil means "no "while"-like token yet scanned".
595 ;; 'else, for example, means "just gone back over an else".
596 ;; 'else-boundary means "just gone back over a statement boundary
597 ;; immediately after having gone back over an else".
598 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
599 ;; of error reporting information.
600 ;; stack: The stack onto which the PDA pushes its state. Each entry
601 ;; consists of a saved value of state and saved-pos. An entry is
602 ;; pushed when we move back over a "continuation" token (e.g. else)
603 ;; and popped when we encounter the corresponding opening token
604 ;; (e.g. if).
605 ;;
606 ;;
607 ;; The following diagram briefly outlines the PDA.
608 ;;
609 ;; Common state:
610 ;; "else": Push state, goto state `else'.
611 ;; "while": Push state, goto state `while'.
612 ;; "catch" or "finally": Push state, goto state `catch'.
613 ;; boundary: Pop state.
614 ;; other: Do nothing special.
615 ;;
616 ;; State `else':
617 ;; boundary: Goto state `else-boundary'.
618 ;; other: Error, pop state, retry token.
619 ;;
620 ;; State `else-boundary':
621 ;; "if": Pop state.
622 ;; boundary: Error, pop state.
623 ;; other: See common state.
624 ;;
625 ;; State `while':
626 ;; boundary: Save position, goto state `while-boundary'.
627 ;; other: Pop state, retry token.
628 ;;
629 ;; State `while-boundary':
630 ;; "do": Pop state.
631 ;; boundary: Restore position if it's not at start, pop state. [*see below]
632 ;; other: See common state.
633 ;;
634 ;; State `catch':
635 ;; boundary: Goto state `catch-boundary'.
636 ;; other: Error, pop state, retry token.
637 ;;
638 ;; State `catch-boundary':
639 ;; "try": Pop state.
640 ;; "catch": Goto state `catch'.
641 ;; boundary: Error, pop state.
642 ;; other: See common state.
643 ;;
644 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
645 ;; searching for a "do" which would have opened a do-while. If we didn't
646 ;; find it, we discard the analysis done since the "while", go back to this
647 ;; token in the buffer and restart the scanning there, this time WITHOUT
648 ;; pushing the 'while state onto the stack.
649 ;;
650 ;; In addition to the above there is some special handling of labels
651 ;; and macros.
652
653 (let ((case-fold-search nil)
654 (start (point))
655 macro-start
656 (delims (if comma-delim '(?\; ?,) '(?\;)))
657 (c-stmt-delim-chars (if comma-delim
658 c-stmt-delim-chars-with-comma
659 c-stmt-delim-chars))
660 c-in-literal-cache c-maybe-labelp saved
661 ;; Current position.
662 pos
663 ;; Position of last stmt boundary character (e.g. ;).
664 boundary-pos
665 ;; The position of the last sexp or bound that follows the
666 ;; first found colon, i.e. the start of the nonlabel part of
667 ;; the statement. It's `start' if a colon is found just after
668 ;; the start.
669 after-labels-pos
670 ;; Like `after-labels-pos', but the first such position inside
671 ;; a label, i.e. the start of the last label before the start
672 ;; of the nonlabel part of the statement.
673 last-label-pos
674 ;; The last position where a label is possible provided the
675 ;; statement started there. It's nil as long as no invalid
676 ;; label content has been found (according to
677 ;; `c-nonlabel-token-key'. It's `start' if no valid label
678 ;; content was found in the label. Note that we might still
679 ;; regard it a label if it starts with `c-label-kwds'.
680 label-good-pos
681 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
682 ;; See above.
683 sym
684 ;; Current state in the automaton. See above.
685 state
686 ;; Current saved positions. See above.
687 saved-pos
688 ;; Stack of conses (state . saved-pos).
689 stack
690 ;; Regexp which matches "for", "if", etc.
691 (cond-key (or c-opt-block-stmt-key
692 "\\<\\>")) ; Matches nothing.
693 ;; Return value.
694 (ret 'same)
695 ;; Positions of the last three sexps or bounds we've stopped at.
696 tok ptok pptok)
697
698 (save-restriction
699 (if lim (narrow-to-region lim (point-max)))
700
701 (if (save-excursion
702 (and (c-beginning-of-macro)
703 (/= (point) start)))
704 (setq macro-start (point)))
705
706 ;; Try to skip back over unary operator characters, to register
707 ;; that we've moved.
708 (while (progn
709 (setq pos (point))
710 (c-backward-syntactic-ws)
711 ;; Protect post-++/-- operators just before a virtual semicolon.
712 (and (not (c-at-vsemi-p))
713 (/= (skip-chars-backward "-+!*&~@`#") 0))))
714
715 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
716 ;; done. Later on we ignore the boundaries for statements that don't
717 ;; contain any sexp. The only thing that is affected is that the error
718 ;; checking is a little less strict, and we really don't bother.
719 (if (and (memq (char-before) delims)
720 (progn (forward-char -1)
721 (setq saved (point))
722 (c-backward-syntactic-ws)
723 (or (memq (char-before) delims)
724 (memq (char-before) '(?: nil))
725 (eq (char-syntax (char-before)) ?\()
726 (c-at-vsemi-p))))
727 (setq ret 'previous
728 pos saved)
729
730 ;; Begin at start and not pos to detect macros if we stand
731 ;; directly after the #.
732 (goto-char start)
733 (if (looking-at "\\<\\|\\W")
734 ;; Record this as the first token if not starting inside it.
735 (setq tok start))
736
737 ;; The following while loop goes back one sexp (balanced parens,
738 ;; etc. with contents, or symbol or suchlike) each iteration. This
739 ;; movement is accomplished with a call to scan-sexps approx 130 lines
740 ;; below.
741 (while
742 (catch 'loop ;; Throw nil to break, non-nil to continue.
743 (cond
744 ((save-excursion
745 (and macro-start ; Always NIL for AWK.
746 (progn (skip-chars-backward " \t")
747 (eq (char-before) ?#))
748 (progn (setq saved (1- (point)))
749 (beginning-of-line)
750 (not (eq (char-before (1- (point))) ?\\)))
751 (looking-at c-opt-cpp-start)
752 (progn (skip-chars-forward " \t")
753 (eq (point) saved))))
754 (goto-char saved)
755 (if (and (c-forward-to-cpp-define-body)
756 (progn (c-forward-syntactic-ws start)
757 (< (point) start)))
758 ;; Stop at the first token in the content of the macro.
759 (setq pos (point)
760 ignore-labels t) ; Avoid the label check on exit.
761 (setq pos saved
762 ret 'macro
763 ignore-labels t))
764 (throw 'loop nil))
765
766 ;; Do a round through the automaton if we've just passed a
767 ;; statement boundary or passed a "while"-like token.
768 ((or sym
769 (and (looking-at cond-key)
770 (setq sym (intern (match-string 1)))))
771
772 (when (and (< pos start) (null stack))
773 (throw 'loop nil))
774
775 ;; The PDA state handling.
776 ;;
777 ;; Refer to the description of the PDA in the opening
778 ;; comments. In the following OR form, the first leaf
779 ;; attempts to handles one of the specific actions detailed
780 ;; (e.g., finding token "if" whilst in state `else-boundary').
781 ;; We drop through to the second leaf (which handles common
782 ;; state) if no specific handler is found in the first cond.
783 ;; If a parsing error is detected (e.g. an "else" with no
784 ;; preceding "if"), we throw to the enclosing catch.
785 ;;
786 ;; Note that the (eq state 'else) means
787 ;; "we've just passed an else", NOT "we're looking for an
788 ;; else".
789 (or (cond
790 ((eq state 'else)
791 (if (eq sym 'boundary)
792 (setq state 'else-boundary)
793 (c-bos-report-error)
794 (c-bos-pop-state-and-retry)))
795
796 ((eq state 'else-boundary)
797 (cond ((eq sym 'if)
798 (c-bos-pop-state (setq ret 'beginning)))
799 ((eq sym 'boundary)
800 (c-bos-report-error)
801 (c-bos-pop-state))))
802
803 ((eq state 'while)
804 (if (and (eq sym 'boundary)
805 ;; Since this can cause backtracking we do a
806 ;; little more careful analysis to avoid it:
807 ;; If there's a label in front of the while
808 ;; it can't be part of a do-while.
809 (not after-labels-pos))
810 (progn (c-bos-save-pos)
811 (setq state 'while-boundary))
812 (c-bos-pop-state-and-retry))) ; Can't be a do-while
813
814 ((eq state 'while-boundary)
815 (cond ((eq sym 'do)
816 (c-bos-pop-state (setq ret 'beginning)))
817 ((eq sym 'boundary) ; isn't a do-while
818 (c-bos-restore-pos) ; the position of the while
819 (c-bos-pop-state)))) ; no longer searching for do.
820
821 ((eq state 'catch)
822 (if (eq sym 'boundary)
823 (setq state 'catch-boundary)
824 (c-bos-report-error)
825 (c-bos-pop-state-and-retry)))
826
827 ((eq state 'catch-boundary)
828 (cond
829 ((eq sym 'try)
830 (c-bos-pop-state (setq ret 'beginning)))
831 ((eq sym 'catch)
832 (setq state 'catch))
833 ((eq sym 'boundary)
834 (c-bos-report-error)
835 (c-bos-pop-state)))))
836
837 ;; This is state common. We get here when the previous
838 ;; cond statement found no particular state handler.
839 (cond ((eq sym 'boundary)
840 ;; If we have a boundary at the start
841 ;; position we push a frame to go to the
842 ;; previous statement.
843 (if (>= pos start)
844 (c-bos-push-state)
845 (c-bos-pop-state)))
846 ((eq sym 'else)
847 (c-bos-push-state)
848 (c-bos-save-error-info 'if 'else)
849 (setq state 'else))
850 ((eq sym 'while)
851 ;; Is this a real while, or a do-while?
852 ;; The next `when' triggers unless we are SURE that
853 ;; the `while' is not the tailend of a `do-while'.
854 (when (or (not pptok)
855 (memq (char-after pptok) delims)
856 ;; The following kludge is to prevent
857 ;; infinite recursion when called from
858 ;; c-awk-after-if-for-while-condition-p,
859 ;; or the like.
860 (and (eq (point) start)
861 (c-vsemi-status-unknown-p))
862 (c-at-vsemi-p pptok))
863 ;; Since this can cause backtracking we do a
864 ;; little more careful analysis to avoid it: If
865 ;; the while isn't followed by a (possibly
866 ;; virtual) semicolon it can't be a do-while.
867 (c-bos-push-state)
868 (setq state 'while)))
869 ((memq sym '(catch finally))
870 (c-bos-push-state)
871 (c-bos-save-error-info 'try sym)
872 (setq state 'catch))))
873
874 (when c-maybe-labelp
875 ;; We're either past a statement boundary or at the
876 ;; start of a statement, so throw away any label data
877 ;; for the previous one.
878 (setq after-labels-pos nil
879 last-label-pos nil
880 c-maybe-labelp nil))))
881
882 ;; Step to the previous sexp, but not if we crossed a
883 ;; boundary, since that doesn't consume an sexp.
884 (if (eq sym 'boundary)
885 (setq ret 'previous)
886
887 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
888 ;; BACKWARDS THROUGH THE SOURCE.
889
890 ;; This is typically fast with the caching done by
891 ;; c-(backward|forward)-sws.
892 (c-backward-syntactic-ws)
893
894 (let ((before-sws-pos (point))
895 ;; Set as long as we have to continue jumping by sexps.
896 ;; It's the position to use as end in the next round.
897 sexp-loop-continue-pos
898 ;; The end position of the area to search for statement
899 ;; barriers in this round.
900 (sexp-loop-end-pos pos))
901
902 ;; The following while goes back one sexp per iteration.
903 (while
904 (progn
905 (unless (c-safe (c-backward-sexp) t)
906 ;; Give up if we hit an unbalanced block. Since the
907 ;; stack won't be empty the code below will report a
908 ;; suitable error.
909 (throw 'loop nil))
910
911 ;; Check if the sexp movement crossed a statement or
912 ;; declaration boundary. But first modify the point
913 ;; so that `c-crosses-statement-barrier-p' only looks
914 ;; at the non-sexp chars following the sexp.
915 (save-excursion
916 (when (setq
917 boundary-pos
918 (cond
919 ((if macro-start
920 nil
921 (save-excursion
922 (when (c-beginning-of-macro)
923 ;; Set continuation position in case
924 ;; `c-crosses-statement-barrier-p'
925 ;; doesn't detect anything below.
926 (setq sexp-loop-continue-pos (point)))))
927 ;; If the sexp movement took us into a
928 ;; macro then there were only some non-sexp
929 ;; chars after it. Skip out of the macro
930 ;; to analyze them but not the non-sexp
931 ;; chars that might be inside the macro.
932 (c-end-of-macro)
933 (c-crosses-statement-barrier-p
934 (point) sexp-loop-end-pos))
935
936 ((and
937 (eq (char-after) ?{)
938 (not (c-looking-at-inexpr-block lim nil t)))
939 ;; Passed a block sexp. That's a boundary
940 ;; alright.
941 (point))
942
943 ((looking-at "\\s\(")
944 ;; Passed some other paren. Only analyze
945 ;; the non-sexp chars after it.
946 (goto-char (1+ (c-down-list-backward
947 before-sws-pos)))
948 ;; We're at a valid token start position
949 ;; (outside the `save-excursion') if
950 ;; `c-crosses-statement-barrier-p' failed.
951 (c-crosses-statement-barrier-p
952 (point) sexp-loop-end-pos))
953
954 (t
955 ;; Passed a symbol sexp or line
956 ;; continuation. It doesn't matter that
957 ;; it's included in the analyzed region.
958 (if (c-crosses-statement-barrier-p
959 (point) sexp-loop-end-pos)
960 t
961 ;; If it was a line continuation then we
962 ;; have to continue looping.
963 (if (looking-at "\\\\$")
964 (setq sexp-loop-continue-pos (point)))
965 nil))))
966
967 (setq pptok ptok
968 ptok tok
969 tok boundary-pos
970 sym 'boundary)
971 ;; Like a C "continue". Analyze the next sexp.
972 (throw 'loop t)))
973
974 sexp-loop-continue-pos) ; End of "go back a sexp" loop.
975 (goto-char sexp-loop-continue-pos)
976 (setq sexp-loop-end-pos sexp-loop-continue-pos
977 sexp-loop-continue-pos nil))))
978
979 ;; ObjC method def?
980 (when (and c-opt-method-key
981 (setq saved (c-in-method-def-p)))
982 (setq pos saved
983 ignore-labels t) ; Avoid the label check on exit.
984 (throw 'loop nil))
985
986 ;; Handle labels.
987 (unless (eq ignore-labels t)
988 (when (numberp c-maybe-labelp)
989 ;; `c-crosses-statement-barrier-p' has found a colon, so we
990 ;; might be in a label now. Have we got a real label
991 ;; (including a case label) or something like C++'s "public:"?
992 (if (or (not (looking-at c-nonlabel-token-key)) ; proper label
993 (save-excursion ; e.g. "case 'a':" ?
994 (and (c-safe (c-backward-sexp) t)
995 (looking-at "\\<case\\>")))) ; FIXME!!! this is
996 ; wrong for AWK. 2006/1/14.
997 (progn
998 (if after-labels-pos ; Have we already encountered a label?
999 (if (not last-label-pos)
1000 (setq last-label-pos (or tok start)))
1001 (setq after-labels-pos (or tok start)))
1002 (setq c-maybe-labelp t
1003 label-good-pos nil))
1004 (setq c-maybe-labelp nil))) ; bogus "label"
1005
1006 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1007 ; been found.
1008 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1009 ;; We're in a potential label and it's the first
1010 ;; time we've found something that isn't allowed in
1011 ;; one.
1012 (setq label-good-pos (or tok start))))
1013
1014 ;; We've moved back by a sexp, so update the token positions.
1015 (setq sym nil
1016 pptok ptok
1017 ptok tok
1018 tok (point)
1019 pos tok))) ; Not nil (for the while loop).
1020
1021 ;; If the stack isn't empty there might be errors to report.
1022 (while stack
1023 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1024 (c-bos-report-error))
1025 (setq saved-pos (cdr (car stack))
1026 stack (cdr stack)))
1027
1028 (when (and (eq ret 'same)
1029 (not (memq sym '(boundary ignore nil))))
1030 ;; Need to investigate closer whether we've crossed
1031 ;; between a substatement and its containing statement.
1032 (if (setq saved (if (looking-at c-block-stmt-1-key)
1033 ptok
1034 pptok))
1035 (cond ((> start saved) (setq pos saved))
1036 ((= start saved) (setq ret 'up)))))
1037
1038 (when (and (not ignore-labels)
1039 (eq c-maybe-labelp t)
1040 (not (eq ret 'beginning))
1041 after-labels-pos
1042 (or (not label-good-pos)
1043 (<= label-good-pos pos)
1044 (progn
1045 (goto-char (if (and last-label-pos
1046 (< last-label-pos start))
1047 last-label-pos
1048 pos))
1049 (looking-at c-label-kwds-regexp))))
1050 ;; We're in a label. Maybe we should step to the statement
1051 ;; after it.
1052 (if (< after-labels-pos start)
1053 (setq pos after-labels-pos)
1054 (setq ret 'label)
1055 (if (and last-label-pos (< last-label-pos start))
1056 ;; Might have jumped over several labels. Go to the last one.
1057 (setq pos last-label-pos)))))
1058
1059 ;; Skip over the unary operators that can start the statement.
1060 (goto-char pos)
1061 (while (progn
1062 (c-backward-syntactic-ws)
1063 ;; protect AWK post-inc/decrement operators, etc.
1064 (and (not (c-at-vsemi-p (point)))
1065 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1066 (setq pos (point)))
1067 (goto-char pos)
1068 ret)))
1069
1070 (defun c-crosses-statement-barrier-p (from to)
1071 "Return non-nil if buffer positions FROM to TO cross one or more
1072 statement or declaration boundaries. The returned value is actually
1073 the position of the earliest boundary char. FROM must not be within
1074 a string or comment.
1075
1076 The variable `c-maybe-labelp' is set to the position of the first `:' that
1077 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1078 single `?' is found, then `c-maybe-labelp' is cleared.
1079
1080 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1081 regarded as having a \"virtual semicolon\" immediately after the last token on
1082 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1083
1084 Note that this function might do hidden buffer changes. See the
1085 comment at the start of cc-engine.el for more info."
1086 (let ((skip-chars c-stmt-delim-chars)
1087 lit-range)
1088 (save-excursion
1089 (catch 'done
1090 (goto-char from)
1091 (while (progn (skip-chars-forward skip-chars to)
1092 (< (point) to))
1093 (cond
1094 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1095 (goto-char (cdr lit-range)))
1096 ((eq (char-after) ?:)
1097 (forward-char)
1098 (if (and (eq (char-after) ?:)
1099 (< (point) to))
1100 ;; Ignore scope operators.
1101 (forward-char)
1102 (setq c-maybe-labelp (1- (point)))))
1103 ((eq (char-after) ??)
1104 ;; A question mark. Can't be a label, so stop
1105 ;; looking for more : and ?.
1106 (setq c-maybe-labelp nil
1107 skip-chars (substring c-stmt-delim-chars 0 -2)))
1108 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1109 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1110 (backward-char))
1111 (skip-chars-backward " \t" from)
1112 (if (c-at-vsemi-p)
1113 (throw 'done (point))
1114 (forward-line)))
1115 (t (throw 'done (point)))))
1116 ;; In trailing space after an as yet undetected virtual semicolon?
1117 (c-backward-syntactic-ws from)
1118 (if (and (< (point) to)
1119 (c-at-vsemi-p))
1120 (point)
1121 nil)))))
1122
1123 (defun c-at-statement-start-p ()
1124 "Return non-nil if the point is at the first token in a statement
1125 or somewhere in the syntactic whitespace before it.
1126
1127 A \"statement\" here is not restricted to those inside code blocks.
1128 Any kind of declaration-like construct that occur outside function
1129 bodies is also considered a \"statement\".
1130
1131 Note that this function might do hidden buffer changes. See the
1132 comment at the start of cc-engine.el for more info."
1133
1134 (save-excursion
1135 (let ((end (point))
1136 c-maybe-labelp)
1137 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1138 (or (bobp)
1139 (eq (char-before) ?})
1140 (and (eq (char-before) ?{)
1141 (not (and c-special-brace-lists
1142 (progn (backward-char)
1143 (c-looking-at-special-brace-list)))))
1144 (c-crosses-statement-barrier-p (point) end)))))
1145
1146 (defun c-at-expression-start-p ()
1147 "Return non-nil if the point is at the first token in an expression or
1148 statement, or somewhere in the syntactic whitespace before it.
1149
1150 An \"expression\" here is a bit different from the normal language
1151 grammar sense: It's any sequence of expression tokens except commas,
1152 unless they are enclosed inside parentheses of some kind. Also, an
1153 expression never continues past an enclosing parenthesis, but it might
1154 contain parenthesis pairs of any sort except braces.
1155
1156 Since expressions never cross statement boundaries, this function also
1157 recognizes statement beginnings, just like `c-at-statement-start-p'.
1158
1159 Note that this function might do hidden buffer changes. See the
1160 comment at the start of cc-engine.el for more info."
1161
1162 (save-excursion
1163 (let ((end (point))
1164 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1165 c-maybe-labelp)
1166 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1167 (or (bobp)
1168 (memq (char-before) '(?{ ?}))
1169 (save-excursion (backward-char)
1170 (looking-at "\\s("))
1171 (c-crosses-statement-barrier-p (point) end)))))
1172
1173 \f
1174 ;; A set of functions that covers various idiosyncrasies in
1175 ;; implementations of `forward-comment'.
1176
1177 ;; Note: Some emacsen considers incorrectly that any line comment
1178 ;; ending with a backslash continues to the next line. I can't think
1179 ;; of any way to work around that in a reliable way without changing
1180 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1181 ;; changing the syntax for backslash doesn't work since we must treat
1182 ;; escapes in string literals correctly.)
1183
1184 (defun c-forward-single-comment ()
1185 "Move forward past whitespace and the closest following comment, if any.
1186 Return t if a comment was found, nil otherwise. In either case, the
1187 point is moved past the following whitespace. Line continuations,
1188 i.e. a backslashes followed by line breaks, are treated as whitespace.
1189 The line breaks that end line comments are considered to be the
1190 comment enders, so the point will be put on the beginning of the next
1191 line if it moved past a line comment.
1192
1193 This function does not do any hidden buffer changes."
1194
1195 (let ((start (point)))
1196 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1197 (goto-char (match-end 0)))
1198
1199 (when (forward-comment 1)
1200 (if (eobp)
1201 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1202 ;; forwards at eob.
1203 nil
1204
1205 ;; Emacs includes the ending newline in a b-style (c++)
1206 ;; comment, but XEmacs doesn't. We depend on the Emacs
1207 ;; behavior (which also is symmetric).
1208 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1209 (condition-case nil (forward-char 1)))
1210
1211 t))))
1212
1213 (defsubst c-forward-comments ()
1214 "Move forward past all following whitespace and comments.
1215 Line continuations, i.e. a backslashes followed by line breaks, are
1216 treated as whitespace.
1217
1218 Note that this function might do hidden buffer changes. See the
1219 comment at the start of cc-engine.el for more info."
1220
1221 (while (or
1222 ;; If forward-comment in at least XEmacs 21 is given a large
1223 ;; positive value, it'll loop all the way through if it hits
1224 ;; eob.
1225 (and (forward-comment 5)
1226 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1227 ;; forwards at eob.
1228 (not (eobp)))
1229
1230 (when (looking-at "\\\\[\n\r]")
1231 (forward-char 2)
1232 t))))
1233
1234 (defun c-backward-single-comment ()
1235 "Move backward past whitespace and the closest preceding comment, if any.
1236 Return t if a comment was found, nil otherwise. In either case, the
1237 point is moved past the preceding whitespace. Line continuations,
1238 i.e. a backslashes followed by line breaks, are treated as whitespace.
1239 The line breaks that end line comments are considered to be the
1240 comment enders, so the point cannot be at the end of the same line to
1241 move over a line comment.
1242
1243 This function does not do any hidden buffer changes."
1244
1245 (let ((start (point)))
1246 ;; When we got newline terminated comments, forward-comment in all
1247 ;; supported emacsen so far will stop at eol of each line not
1248 ;; ending with a comment when moving backwards. This corrects for
1249 ;; that, and at the same time handles line continuations.
1250 (while (progn
1251 (skip-chars-backward " \t\n\r\f\v")
1252 (and (looking-at "[\n\r]")
1253 (eq (char-before) ?\\)))
1254 (backward-char))
1255
1256 (if (bobp)
1257 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1258 ;; backwards at bob.
1259 nil
1260
1261 ;; Leave point after the closest following newline if we've
1262 ;; backed up over any above, since forward-comment won't move
1263 ;; backward over a line comment if point is at the end of the
1264 ;; same line.
1265 (re-search-forward "\\=\\s *[\n\r]" start t)
1266
1267 (if (if (forward-comment -1)
1268 (if (eolp)
1269 ;; If forward-comment above succeeded and we're at eol
1270 ;; then the newline we moved over above didn't end a
1271 ;; line comment, so we give it another go.
1272 (forward-comment -1)
1273 t))
1274
1275 ;; Emacs <= 20 and XEmacs move back over the closer of a
1276 ;; block comment that lacks an opener.
1277 (if (looking-at "\\*/")
1278 (progn (forward-char 2) nil)
1279 t)))))
1280
1281 (defsubst c-backward-comments ()
1282 "Move backward past all preceding whitespace and comments.
1283 Line continuations, i.e. a backslashes followed by line breaks, are
1284 treated as whitespace. The line breaks that end line comments are
1285 considered to be the comment enders, so the point cannot be at the end
1286 of the same line to move over a line comment. Unlike
1287 c-backward-syntactic-ws, this function doesn't move back over
1288 preprocessor directives.
1289
1290 Note that this function might do hidden buffer changes. See the
1291 comment at the start of cc-engine.el for more info."
1292
1293 (let ((start (point)))
1294 (while (and
1295 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1296 ;; return t when moving backwards at bob.
1297 (not (bobp))
1298
1299 (if (forward-comment -1)
1300 (if (looking-at "\\*/")
1301 ;; Emacs <= 20 and XEmacs move back over the
1302 ;; closer of a block comment that lacks an opener.
1303 (progn (forward-char 2) nil)
1304 t)
1305
1306 ;; XEmacs treats line continuations as whitespace but
1307 ;; only in the backward direction, which seems a bit
1308 ;; odd. Anyway, this is necessary for Emacs.
1309 (when (and (looking-at "[\n\r]")
1310 (eq (char-before) ?\\)
1311 (< (point) start))
1312 (backward-char)
1313 t))))))
1314
1315 \f
1316 ;; Tools for skipping over syntactic whitespace.
1317
1318 ;; The following functions use text properties to cache searches over
1319 ;; large regions of syntactic whitespace. It works as follows:
1320 ;;
1321 ;; o If a syntactic whitespace region contains anything but simple
1322 ;; whitespace (i.e. space, tab and line breaks), the text property
1323 ;; `c-in-sws' is put over it. At places where we have stopped
1324 ;; within that region there's also a `c-is-sws' text property.
1325 ;; That since there typically are nested whitespace inside that
1326 ;; must be handled separately, e.g. whitespace inside a comment or
1327 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1328 ;; to jump to another point with that property within the same
1329 ;; `c-in-sws' region. It can be likened to a ladder where
1330 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1331 ;;
1332 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1333 ;; a "rung position" and also maybe on the first following char.
1334 ;; As many characters as can be conveniently found in this range
1335 ;; are marked, but no assumption can be made that the whole range
1336 ;; is marked (it could be clobbered by later changes, for
1337 ;; instance).
1338 ;;
1339 ;; Note that some part of the beginning of a sequence of simple
1340 ;; whitespace might be part of the end of a preceding line comment
1341 ;; or cpp directive and must not be considered part of the "rung".
1342 ;; Such whitespace is some amount of horizontal whitespace followed
1343 ;; by a newline. In the case of cpp directives it could also be
1344 ;; two newlines with horizontal whitespace between them.
1345 ;;
1346 ;; The reason to include the first following char is to cope with
1347 ;; "rung positions" that doesn't have any ordinary whitespace. If
1348 ;; `c-is-sws' is put on a token character it does not have
1349 ;; `c-in-sws' set simultaneously. That's the only case when that
1350 ;; can occur, and the reason for not extending the `c-in-sws'
1351 ;; region to cover it is that the `c-in-sws' region could then be
1352 ;; accidentally merged with a following one if the token is only
1353 ;; one character long.
1354 ;;
1355 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1356 ;; removed in the changed region. If the change was inside
1357 ;; syntactic whitespace that means that the "ladder" is broken, but
1358 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1359 ;; parts on either side and use an ordinary search only to "repair"
1360 ;; the gap.
1361 ;;
1362 ;; Special care needs to be taken if a region is removed: If there
1363 ;; are `c-in-sws' on both sides of it which do not connect inside
1364 ;; the region then they can't be joined. If e.g. a marked macro is
1365 ;; broken, syntactic whitespace inside the new text might be
1366 ;; marked. If those marks would become connected with the old
1367 ;; `c-in-sws' range around the macro then we could get a ladder
1368 ;; with one end outside the macro and the other at some whitespace
1369 ;; within it.
1370 ;;
1371 ;; The main motivation for this system is to increase the speed in
1372 ;; skipping over the large whitespace regions that can occur at the
1373 ;; top level in e.g. header files that contain a lot of comments and
1374 ;; cpp directives. For small comments inside code it's probably
1375 ;; slower than using `forward-comment' straightforwardly, but speed is
1376 ;; not a significant factor there anyway.
1377
1378 ; (defface c-debug-is-sws-face
1379 ; '((t (:background "GreenYellow")))
1380 ; "Debug face to mark the `c-is-sws' property.")
1381 ; (defface c-debug-in-sws-face
1382 ; '((t (:underline t)))
1383 ; "Debug face to mark the `c-in-sws' property.")
1384
1385 ; (defun c-debug-put-sws-faces ()
1386 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1387 ; ;; properties in the buffer.
1388 ; (interactive)
1389 ; (save-excursion
1390 ; (c-save-buffer-state (in-face)
1391 ; (goto-char (point-min))
1392 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1393 ; (point)))
1394 ; (while (progn
1395 ; (goto-char (next-single-property-change
1396 ; (point) 'c-is-sws nil (point-max)))
1397 ; (if in-face
1398 ; (progn
1399 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1400 ; (setq in-face nil))
1401 ; (setq in-face (point)))
1402 ; (not (eobp))))
1403 ; (goto-char (point-min))
1404 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1405 ; (point)))
1406 ; (while (progn
1407 ; (goto-char (next-single-property-change
1408 ; (point) 'c-in-sws nil (point-max)))
1409 ; (if in-face
1410 ; (progn
1411 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1412 ; (setq in-face nil))
1413 ; (setq in-face (point)))
1414 ; (not (eobp)))))))
1415
1416 (defmacro c-debug-sws-msg (&rest args)
1417 ;;`(message ,@args)
1418 )
1419
1420 (defmacro c-put-is-sws (beg end)
1421 ;; This macro does a hidden buffer change.
1422 `(let ((beg ,beg) (end ,end))
1423 (put-text-property beg end 'c-is-sws t)
1424 ,@(when (facep 'c-debug-is-sws-face)
1425 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1426
1427 (defmacro c-put-in-sws (beg end)
1428 ;; This macro does a hidden buffer change.
1429 `(let ((beg ,beg) (end ,end))
1430 (put-text-property beg end 'c-in-sws t)
1431 ,@(when (facep 'c-debug-is-sws-face)
1432 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1433
1434 (defmacro c-remove-is-sws (beg end)
1435 ;; This macro does a hidden buffer change.
1436 `(let ((beg ,beg) (end ,end))
1437 (remove-text-properties beg end '(c-is-sws nil))
1438 ,@(when (facep 'c-debug-is-sws-face)
1439 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1440
1441 (defmacro c-remove-in-sws (beg end)
1442 ;; This macro does a hidden buffer change.
1443 `(let ((beg ,beg) (end ,end))
1444 (remove-text-properties beg end '(c-in-sws nil))
1445 ,@(when (facep 'c-debug-is-sws-face)
1446 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1447
1448 (defmacro c-remove-is-and-in-sws (beg end)
1449 ;; This macro does a hidden buffer change.
1450 `(let ((beg ,beg) (end ,end))
1451 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1452 ,@(when (facep 'c-debug-is-sws-face)
1453 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1454 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1455
1456 (defsubst c-invalidate-sws-region-after (beg end)
1457 ;; Called from `after-change-functions'. Note that if
1458 ;; `c-forward-sws' or `c-backward-sws' are used outside
1459 ;; `c-save-buffer-state' or similar then this will remove the cache
1460 ;; properties right after they're added.
1461 ;;
1462 ;; This function does hidden buffer changes.
1463
1464 (save-excursion
1465 ;; Adjust the end to remove the properties in any following simple
1466 ;; ws up to and including the next line break, if there is any
1467 ;; after the changed region. This is necessary e.g. when a rung
1468 ;; marked empty line is converted to a line comment by inserting
1469 ;; "//" before the line break. In that case the line break would
1470 ;; keep the rung mark which could make a later `c-backward-sws'
1471 ;; move into the line comment instead of over it.
1472 (goto-char end)
1473 (skip-chars-forward " \t\f\v")
1474 (when (and (eolp) (not (eobp)))
1475 (setq end (1+ (point)))))
1476
1477 (when (and (= beg end)
1478 (get-text-property beg 'c-in-sws)
1479 (> beg (point-min))
1480 (get-text-property (1- beg) 'c-in-sws))
1481 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1482 ;; safe to keep a range that was continuous before the change. E.g:
1483 ;;
1484 ;; #define foo
1485 ;; \
1486 ;; bar
1487 ;;
1488 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1489 ;; after "foo" is removed then "bar" will become part of the cpp
1490 ;; directive instead of a syntactically relevant token. In that
1491 ;; case there's no longer syntactic ws from "#" to "b".
1492 (setq beg (1- beg)))
1493
1494 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1495 (c-remove-is-and-in-sws beg end))
1496
1497 (defun c-forward-sws ()
1498 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1499 ;;
1500 ;; This function might do hidden buffer changes.
1501
1502 (let (;; `rung-pos' is set to a position as early as possible in the
1503 ;; unmarked part of the simple ws region.
1504 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1505 rung-is-marked next-rung-is-marked simple-ws-end
1506 ;; `safe-start' is set when it's safe to cache the start position.
1507 ;; It's not set if we've initially skipped over comments and line
1508 ;; continuations since we might have gone out through the end of a
1509 ;; macro then. This provision makes `c-forward-sws' not populate the
1510 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1511 ;; more common.
1512 safe-start)
1513
1514 ;; Skip simple ws and do a quick check on the following character to see
1515 ;; if it's anything that can't start syntactic ws, so we can bail out
1516 ;; early in the majority of cases when there just are a few ws chars.
1517 (skip-chars-forward " \t\n\r\f\v")
1518 (when (looking-at c-syntactic-ws-start)
1519
1520 (setq rung-end-pos (min (1+ (point)) (point-max)))
1521 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1522 'c-is-sws t))
1523 ;; Find the last rung position to avoid setting properties in all
1524 ;; the cases when the marked rung is complete.
1525 ;; (`next-single-property-change' is certain to move at least one
1526 ;; step forward.)
1527 (setq rung-pos (1- (next-single-property-change
1528 rung-is-marked 'c-is-sws nil rung-end-pos)))
1529 ;; Got no marked rung here. Since the simple ws might have started
1530 ;; inside a line comment or cpp directive we must set `rung-pos' as
1531 ;; high as possible.
1532 (setq rung-pos (point)))
1533
1534 (while
1535 (progn
1536 (while
1537 (when (and rung-is-marked
1538 (get-text-property (point) 'c-in-sws))
1539
1540 ;; The following search is the main reason that `c-in-sws'
1541 ;; and `c-is-sws' aren't combined to one property.
1542 (goto-char (next-single-property-change
1543 (point) 'c-in-sws nil (point-max)))
1544 (unless (get-text-property (point) 'c-is-sws)
1545 ;; If the `c-in-sws' region extended past the last
1546 ;; `c-is-sws' char we have to go back a bit.
1547 (or (get-text-property (1- (point)) 'c-is-sws)
1548 (goto-char (previous-single-property-change
1549 (point) 'c-is-sws)))
1550 (backward-char))
1551
1552 (c-debug-sws-msg
1553 "c-forward-sws cached move %s -> %s (max %s)"
1554 rung-pos (point) (point-max))
1555
1556 (setq rung-pos (point))
1557 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1558 (not (eobp))))
1559
1560 ;; We'll loop here if there is simple ws after the last rung.
1561 ;; That means that there's been some change in it and it's
1562 ;; possible that we've stepped into another ladder, so extend
1563 ;; the previous one to join with it if there is one, and try to
1564 ;; use the cache again.
1565 (c-debug-sws-msg
1566 "c-forward-sws extending rung with [%s..%s] (max %s)"
1567 (1+ rung-pos) (1+ (point)) (point-max))
1568 (unless (get-text-property (point) 'c-is-sws)
1569 ;; Remove any `c-in-sws' property from the last char of
1570 ;; the rung before we mark it with `c-is-sws', so that we
1571 ;; won't connect with the remains of a broken "ladder".
1572 (c-remove-in-sws (point) (1+ (point))))
1573 (c-put-is-sws (1+ rung-pos)
1574 (1+ (point)))
1575 (c-put-in-sws rung-pos
1576 (setq rung-pos (point)
1577 last-put-in-sws-pos rung-pos)))
1578
1579 (setq simple-ws-end (point))
1580 (c-forward-comments)
1581
1582 (cond
1583 ((/= (point) simple-ws-end)
1584 ;; Skipped over comments. Don't cache at eob in case the buffer
1585 ;; is narrowed.
1586 (not (eobp)))
1587
1588 ((save-excursion
1589 (and c-opt-cpp-prefix
1590 (looking-at c-opt-cpp-start)
1591 (progn (skip-chars-backward " \t")
1592 (bolp))
1593 (or (bobp)
1594 (progn (backward-char)
1595 (not (eq (char-before) ?\\))))))
1596 ;; Skip a preprocessor directive.
1597 (end-of-line)
1598 (while (and (eq (char-before) ?\\)
1599 (= (forward-line 1) 0))
1600 (end-of-line))
1601 (forward-line 1)
1602 (setq safe-start t)
1603 ;; Don't cache at eob in case the buffer is narrowed.
1604 (not (eobp)))))
1605
1606 ;; We've searched over a piece of non-white syntactic ws. See if this
1607 ;; can be cached.
1608 (setq next-rung-pos (point))
1609 (skip-chars-forward " \t\n\r\f\v")
1610 (setq rung-end-pos (min (1+ (point)) (point-max)))
1611
1612 (if (or
1613 ;; Cache if we haven't skipped comments only, and if we started
1614 ;; either from a marked rung or from a completely uncached
1615 ;; position.
1616 (and safe-start
1617 (or rung-is-marked
1618 (not (get-text-property simple-ws-end 'c-in-sws))))
1619
1620 ;; See if there's a marked rung in the encountered simple ws. If
1621 ;; so then we can cache, unless `safe-start' is nil. Even then
1622 ;; we need to do this to check if the cache can be used for the
1623 ;; next step.
1624 (and (setq next-rung-is-marked
1625 (text-property-any next-rung-pos rung-end-pos
1626 'c-is-sws t))
1627 safe-start))
1628
1629 (progn
1630 (c-debug-sws-msg
1631 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1632 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1633 (point-max))
1634
1635 ;; Remove the properties for any nested ws that might be cached.
1636 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1637 ;; anyway.
1638 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1639 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1640 (c-put-is-sws rung-pos
1641 (1+ simple-ws-end))
1642 (setq rung-is-marked t))
1643 (c-put-in-sws rung-pos
1644 (setq rung-pos (point)
1645 last-put-in-sws-pos rung-pos))
1646 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1647 ;; Remove any `c-in-sws' property from the last char of
1648 ;; the rung before we mark it with `c-is-sws', so that we
1649 ;; won't connect with the remains of a broken "ladder".
1650 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1651 (c-put-is-sws next-rung-pos
1652 rung-end-pos))
1653
1654 (c-debug-sws-msg
1655 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1656 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1657 (point-max))
1658
1659 ;; Set `rung-pos' for the next rung. It's the same thing here as
1660 ;; initially, except that the rung position is set as early as
1661 ;; possible since we can't be in the ending ws of a line comment or
1662 ;; cpp directive now.
1663 (if (setq rung-is-marked next-rung-is-marked)
1664 (setq rung-pos (1- (next-single-property-change
1665 rung-is-marked 'c-is-sws nil rung-end-pos)))
1666 (setq rung-pos next-rung-pos))
1667 (setq safe-start t)))
1668
1669 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1670 ;; another one after the point (which might occur when editing inside a
1671 ;; comment or macro).
1672 (when (eq last-put-in-sws-pos (point))
1673 (cond ((< last-put-in-sws-pos (point-max))
1674 (c-debug-sws-msg
1675 "c-forward-sws clearing at %s for cache separation"
1676 last-put-in-sws-pos)
1677 (c-remove-in-sws last-put-in-sws-pos
1678 (1+ last-put-in-sws-pos)))
1679 (t
1680 ;; If at eob we have to clear the last character before the end
1681 ;; instead since the buffer might be narrowed and there might
1682 ;; be a `c-in-sws' after (point-max). In this case it's
1683 ;; necessary to clear both properties.
1684 (c-debug-sws-msg
1685 "c-forward-sws clearing thoroughly at %s for cache separation"
1686 (1- last-put-in-sws-pos))
1687 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1688 last-put-in-sws-pos))))
1689 )))
1690
1691 (defun c-backward-sws ()
1692 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1693 ;;
1694 ;; This function might do hidden buffer changes.
1695
1696 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1697 ;; part of the simple ws region.
1698 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1699 rung-is-marked simple-ws-beg cmt-skip-pos)
1700
1701 ;; Skip simple horizontal ws and do a quick check on the preceding
1702 ;; character to see if it's anying that can't end syntactic ws, so we can
1703 ;; bail out early in the majority of cases when there just are a few ws
1704 ;; chars. Newlines are complicated in the backward direction, so we can't
1705 ;; skip over them.
1706 (skip-chars-backward " \t\f")
1707 (when (and (not (bobp))
1708 (save-excursion
1709 (backward-char)
1710 (looking-at c-syntactic-ws-end)))
1711
1712 ;; Try to find a rung position in the simple ws preceding point, so that
1713 ;; we can get a cache hit even if the last bit of the simple ws has
1714 ;; changed recently.
1715 (setq simple-ws-beg (point))
1716 (skip-chars-backward " \t\n\r\f\v")
1717 (if (setq rung-is-marked (text-property-any
1718 (point) (min (1+ rung-pos) (point-max))
1719 'c-is-sws t))
1720 ;; `rung-pos' will be the earliest marked position, which means that
1721 ;; there might be later unmarked parts in the simple ws region.
1722 ;; It's not worth the effort to fix that; the last part of the
1723 ;; simple ws is also typically edited often, so it could be wasted.
1724 (goto-char (setq rung-pos rung-is-marked))
1725 (goto-char simple-ws-beg))
1726
1727 (while
1728 (progn
1729 (while
1730 (when (and rung-is-marked
1731 (not (bobp))
1732 (get-text-property (1- (point)) 'c-in-sws))
1733
1734 ;; The following search is the main reason that `c-in-sws'
1735 ;; and `c-is-sws' aren't combined to one property.
1736 (goto-char (previous-single-property-change
1737 (point) 'c-in-sws nil (point-min)))
1738 (unless (get-text-property (point) 'c-is-sws)
1739 ;; If the `c-in-sws' region extended past the first
1740 ;; `c-is-sws' char we have to go forward a bit.
1741 (goto-char (next-single-property-change
1742 (point) 'c-is-sws)))
1743
1744 (c-debug-sws-msg
1745 "c-backward-sws cached move %s <- %s (min %s)"
1746 (point) rung-pos (point-min))
1747
1748 (setq rung-pos (point))
1749 (if (and (< (min (skip-chars-backward " \t\f\v")
1750 (progn
1751 (setq simple-ws-beg (point))
1752 (skip-chars-backward " \t\n\r\f\v")))
1753 0)
1754 (setq rung-is-marked
1755 (text-property-any (point) rung-pos
1756 'c-is-sws t)))
1757 t
1758 (goto-char simple-ws-beg)
1759 nil))
1760
1761 ;; We'll loop here if there is simple ws before the first rung.
1762 ;; That means that there's been some change in it and it's
1763 ;; possible that we've stepped into another ladder, so extend
1764 ;; the previous one to join with it if there is one, and try to
1765 ;; use the cache again.
1766 (c-debug-sws-msg
1767 "c-backward-sws extending rung with [%s..%s] (min %s)"
1768 rung-is-marked rung-pos (point-min))
1769 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1770 ;; Remove any `c-in-sws' property from the last char of
1771 ;; the rung before we mark it with `c-is-sws', so that we
1772 ;; won't connect with the remains of a broken "ladder".
1773 (c-remove-in-sws (1- rung-pos) rung-pos))
1774 (c-put-is-sws rung-is-marked
1775 rung-pos)
1776 (c-put-in-sws rung-is-marked
1777 (1- rung-pos))
1778 (setq rung-pos rung-is-marked
1779 last-put-in-sws-pos rung-pos))
1780
1781 (c-backward-comments)
1782 (setq cmt-skip-pos (point))
1783
1784 (cond
1785 ((and c-opt-cpp-prefix
1786 (/= cmt-skip-pos simple-ws-beg)
1787 (c-beginning-of-macro))
1788 ;; Inside a cpp directive. See if it should be skipped over.
1789 (let ((cpp-beg (point)))
1790
1791 ;; Move back over all line continuations in the region skipped
1792 ;; over by `c-backward-comments'. If we go past it then we
1793 ;; started inside the cpp directive.
1794 (goto-char simple-ws-beg)
1795 (beginning-of-line)
1796 (while (and (> (point) cmt-skip-pos)
1797 (progn (backward-char)
1798 (eq (char-before) ?\\)))
1799 (beginning-of-line))
1800
1801 (if (< (point) cmt-skip-pos)
1802 ;; Don't move past the cpp directive if we began inside
1803 ;; it. Note that the position at the end of the last line
1804 ;; of the macro is also considered to be within it.
1805 (progn (goto-char cmt-skip-pos)
1806 nil)
1807
1808 ;; It's worthwhile to spend a little bit of effort on finding
1809 ;; the end of the macro, to get a good `simple-ws-beg'
1810 ;; position for the cache. Note that `c-backward-comments'
1811 ;; could have stepped over some comments before going into
1812 ;; the macro, and then `simple-ws-beg' must be kept on the
1813 ;; same side of those comments.
1814 (goto-char simple-ws-beg)
1815 (skip-chars-backward " \t\n\r\f\v")
1816 (if (eq (char-before) ?\\)
1817 (forward-char))
1818 (forward-line 1)
1819 (if (< (point) simple-ws-beg)
1820 ;; Might happen if comments after the macro were skipped
1821 ;; over.
1822 (setq simple-ws-beg (point)))
1823
1824 (goto-char cpp-beg)
1825 t)))
1826
1827 ((/= (save-excursion
1828 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1829 (setq next-rung-pos (point)))
1830 simple-ws-beg)
1831 ;; Skipped over comments. Must put point at the end of
1832 ;; the simple ws at point since we might be after a line
1833 ;; comment or cpp directive that's been partially
1834 ;; narrowed out, and we can't risk marking the simple ws
1835 ;; at the end of it.
1836 (goto-char next-rung-pos)
1837 t)))
1838
1839 ;; We've searched over a piece of non-white syntactic ws. See if this
1840 ;; can be cached.
1841 (setq next-rung-pos (point))
1842 (skip-chars-backward " \t\f\v")
1843
1844 (if (or
1845 ;; Cache if we started either from a marked rung or from a
1846 ;; completely uncached position.
1847 rung-is-marked
1848 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1849
1850 ;; Cache if there's a marked rung in the encountered simple ws.
1851 (save-excursion
1852 (skip-chars-backward " \t\n\r\f\v")
1853 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1854 'c-is-sws t)))
1855
1856 (progn
1857 (c-debug-sws-msg
1858 "c-backward-sws 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
1863 ;; Remove the properties for any nested ws that might be cached.
1864 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1865 ;; anyway.
1866 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1867 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1868 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1869 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1870 ;; Remove any `c-in-sws' property from the last char of
1871 ;; the rung before we mark it with `c-is-sws', so that we
1872 ;; won't connect with the remains of a broken "ladder".
1873 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1874 (c-put-is-sws simple-ws-beg
1875 rung-end-pos)
1876 (setq rung-is-marked t)))
1877 (c-put-in-sws (setq simple-ws-beg (point)
1878 last-put-in-sws-pos simple-ws-beg)
1879 rung-pos)
1880 (c-put-is-sws (setq rung-pos simple-ws-beg)
1881 (1+ next-rung-pos)))
1882
1883 (c-debug-sws-msg
1884 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1885 (point) (1+ next-rung-pos)
1886 simple-ws-beg (min (1+ rung-pos) (point-max))
1887 (point-min))
1888 (setq rung-pos next-rung-pos
1889 simple-ws-beg (point))
1890 ))
1891
1892 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1893 ;; another one before the point (which might occur when editing inside a
1894 ;; comment or macro).
1895 (when (eq last-put-in-sws-pos (point))
1896 (cond ((< (point-min) last-put-in-sws-pos)
1897 (c-debug-sws-msg
1898 "c-backward-sws clearing at %s for cache separation"
1899 (1- last-put-in-sws-pos))
1900 (c-remove-in-sws (1- last-put-in-sws-pos)
1901 last-put-in-sws-pos))
1902 ((> (point-min) 1)
1903 ;; If at bob and the buffer is narrowed, we have to clear the
1904 ;; character we're standing on instead since there might be a
1905 ;; `c-in-sws' before (point-min). In this case it's necessary
1906 ;; to clear both properties.
1907 (c-debug-sws-msg
1908 "c-backward-sws clearing thoroughly at %s for cache separation"
1909 last-put-in-sws-pos)
1910 (c-remove-is-and-in-sws last-put-in-sws-pos
1911 (1+ last-put-in-sws-pos)))))
1912 )))
1913
1914 \f
1915 ;; Other whitespace tools
1916 (defun c-partial-ws-p (beg end)
1917 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1918 ;; region? This is a "heuristic" function. .....
1919 ;;
1920 ;; The motivation for the second bit is to check whether removing this
1921 ;; region would coalesce two symbols.
1922 ;;
1923 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1924 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1925 (save-excursion
1926 (let ((end+1 (min (1+ end) (point-max))))
1927 (or (progn (goto-char (max (point-min) (1- beg)))
1928 (c-skip-ws-forward end)
1929 (eq (point) end))
1930 (progn (goto-char beg)
1931 (c-skip-ws-forward end+1)
1932 (eq (point) end+1))))))
1933 \f
1934 ;; A system for finding noteworthy parens before the point.
1935
1936 (defvar c-state-cache nil)
1937 (make-variable-buffer-local 'c-state-cache)
1938 ;; The state cache used by `c-parse-state' to cut down the amount of
1939 ;; searching. It's the result from some earlier `c-parse-state' call.
1940 ;;
1941 ;; The use of the cached info is more effective if the next
1942 ;; `c-parse-state' call is on a line close by the one the cached state
1943 ;; was made at; the cache can actually slow down a little if the
1944 ;; cached state was made very far back in the buffer. The cache is
1945 ;; most effective if `c-parse-state' is used on each line while moving
1946 ;; forward.
1947
1948 (defvar c-state-cache-start 1)
1949 (make-variable-buffer-local 'c-state-cache-start)
1950 ;; This is (point-min) when `c-state-cache' was calculated, since a
1951 ;; change of narrowing is likely to affect the parens that are visible
1952 ;; before the point.
1953
1954 (defvar c-state-cache-good-pos 1)
1955 (make-variable-buffer-local 'c-state-cache-good-pos)
1956 ;; This is a position where `c-state-cache' is known to be correct.
1957 ;; It's a position inside one of the recorded unclosed parens or the
1958 ;; top level, but not further nested inside any literal or subparen
1959 ;; that is closed before the last recorded position.
1960 ;;
1961 ;; The exact position is chosen to try to be close to yet earlier than
1962 ;; the position where `c-state-cache' will be called next. Right now
1963 ;; the heuristic is to set it to the position after the last found
1964 ;; closing paren (of any type) before the line on which
1965 ;; `c-parse-state' was called. That is chosen primarily to work well
1966 ;; with refontification of the current line.
1967
1968 (defsubst c-invalidate-state-cache (pos)
1969 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1970 ;; at POS or higher. This is much like `c-whack-state-after', but
1971 ;; it never changes a paren pair element into an open paren element.
1972 ;; Doing that would mean that the new open paren wouldn't have the
1973 ;; required preceding paren pair element.
1974 (while (and (or c-state-cache
1975 (when (< pos c-state-cache-good-pos)
1976 (setq c-state-cache-good-pos 1)
1977 nil))
1978 (let ((elem (car c-state-cache)))
1979 (if (consp elem)
1980 (or (< pos (cdr elem))
1981 (when (< pos c-state-cache-good-pos)
1982 (setq c-state-cache-good-pos (cdr elem))
1983 nil))
1984 (or (<= pos elem)
1985 (when (< pos c-state-cache-good-pos)
1986 (setq c-state-cache-good-pos (1+ elem))
1987 nil)))))
1988 (setq c-state-cache (cdr c-state-cache))))
1989
1990 (defun c-get-fallback-start-pos (here)
1991 ;; Return the start position for building `c-state-cache' from
1992 ;; scratch.
1993 (save-excursion
1994 ;; Go back 2 bods, but ignore any bogus positions returned by
1995 ;; beginning-of-defun (i.e. open paren in column zero).
1996 (goto-char here)
1997 (let ((cnt 2))
1998 (while (not (or (bobp) (zerop cnt)))
1999 (c-beginning-of-defun-1)
2000 (if (eq (char-after) ?\{)
2001 (setq cnt (1- cnt)))))
2002 (point)))
2003
2004 (defun c-parse-state ()
2005 ;; Find and record all noteworthy parens between some good point
2006 ;; earlier in the file and point. That good point is at least the
2007 ;; beginning of the top-level construct we are in, or the beginning
2008 ;; of the preceding top-level construct if we aren't in one.
2009 ;;
2010 ;; The returned value is a list of the noteworthy parens with the
2011 ;; last one first. If an element in the list is an integer, it's
2012 ;; the position of an open paren which has not been closed before
2013 ;; the point. If an element is a cons, it gives the position of a
2014 ;; closed brace paren pair; the car is the start paren position and
2015 ;; the cdr is the position following the closing paren. Only the
2016 ;; last closed brace paren pair before each open paren and before
2017 ;; the point is recorded, and thus the state never contains two cons
2018 ;; elements in succession.
2019 ;;
2020 ;; Currently no characters which are given paren syntax with the
2021 ;; syntax-table property are recorded, i.e. angle bracket arglist
2022 ;; parens are never present here. Note that this might change.
2023 ;;
2024 ;; BUG: This function doesn't cope entirely well with unbalanced
2025 ;; parens in macros. E.g. in the following case the brace before
2026 ;; the macro isn't balanced with the one after it:
2027 ;;
2028 ;; {
2029 ;; #define X {
2030 ;; }
2031 ;;
2032 ;; This function might do hidden buffer changes.
2033
2034 (save-restriction
2035 (let* ((here (point))
2036 (here-bol (c-point 'bol))
2037 (c-macro-start (c-query-macro-start))
2038 (in-macro-start (or c-macro-start (point)))
2039 old-state last-pos brace-pair-open brace-pair-close
2040 pos save-pos)
2041 (c-invalidate-state-cache here)
2042
2043 ;; If the minimum position has changed due to narrowing then we
2044 ;; have to fix the tail of `c-state-cache' accordingly.
2045 (unless (= c-state-cache-start (point-min))
2046 (if (> (point-min) c-state-cache-start)
2047 ;; If point-min has moved forward then we just need to cut
2048 ;; off a bit of the tail.
2049 (let ((ptr (cons nil c-state-cache)) elem)
2050 (while (and (setq elem (car-safe (cdr ptr)))
2051 (>= (if (consp elem) (car elem) elem)
2052 (point-min)))
2053 (setq ptr (cdr ptr)))
2054 (when (consp ptr)
2055 (if (eq (cdr ptr) c-state-cache)
2056 (setq c-state-cache nil
2057 c-state-cache-good-pos 1)
2058 (setcdr ptr nil))))
2059 ;; If point-min has moved backward then we drop the state
2060 ;; completely. It's possible to do a better job here and
2061 ;; recalculate the top only.
2062 (setq c-state-cache nil
2063 c-state-cache-good-pos 1))
2064 (setq c-state-cache-start (point-min)))
2065
2066 ;; Get the latest position we know are directly inside the
2067 ;; closest containing paren of the cached state.
2068 (setq last-pos (and c-state-cache
2069 (if (consp (car c-state-cache))
2070 (cdr (car c-state-cache))
2071 (1+ (car c-state-cache)))))
2072 (if (or (not last-pos)
2073 (< last-pos c-state-cache-good-pos))
2074 (setq last-pos c-state-cache-good-pos)
2075 ;; Take the opportunity to move the cached good position
2076 ;; further down.
2077 (if (< last-pos here-bol)
2078 (setq c-state-cache-good-pos last-pos)))
2079
2080 ;; Check if `last-pos' is in a macro. If it is, and we're not
2081 ;; in the same macro, we must discard everything on
2082 ;; `c-state-cache' that is inside the macro before using it.
2083 (save-excursion
2084 (goto-char last-pos)
2085 (when (and (c-beginning-of-macro)
2086 (/= (point) in-macro-start))
2087 (c-invalidate-state-cache (point))
2088 ;; Set `last-pos' again just like above except that there's
2089 ;; no use looking at `c-state-cache-good-pos' here.
2090 (setq last-pos (if c-state-cache
2091 (if (consp (car c-state-cache))
2092 (cdr (car c-state-cache))
2093 (1+ (car c-state-cache)))
2094 1))))
2095
2096 ;; If we've moved very far from the last cached position then
2097 ;; it's probably better to redo it from scratch, otherwise we
2098 ;; might spend a lot of time searching from `last-pos' down to
2099 ;; here.
2100 (when (< last-pos (- here 20000))
2101 ;; First get the fallback start position. If it turns out
2102 ;; that it's so far back that the cached state is closer then
2103 ;; we'll keep it afterall.
2104 (setq pos (c-get-fallback-start-pos here))
2105 (if (<= pos last-pos)
2106 (setq pos nil)
2107 (setq last-pos nil
2108 c-state-cache nil
2109 c-state-cache-good-pos 1)))
2110
2111 ;; Find the start position for the forward search. (Can't
2112 ;; search in the backward direction since the point might be in
2113 ;; some kind of literal.)
2114
2115 (unless pos
2116 (setq old-state c-state-cache)
2117
2118 ;; There's a cached state with a containing paren. Pop off
2119 ;; the stale containing sexps from it by going forward out of
2120 ;; parens as far as possible.
2121 (narrow-to-region (point-min) here)
2122 (let (placeholder pair-beg)
2123 (while (and c-state-cache
2124 (setq placeholder
2125 (c-up-list-forward last-pos)))
2126 (setq last-pos placeholder)
2127 (if (consp (car c-state-cache))
2128 (setq pair-beg (car-safe (cdr c-state-cache))
2129 c-state-cache (cdr-safe (cdr c-state-cache)))
2130 (setq pair-beg (car c-state-cache)
2131 c-state-cache (cdr c-state-cache))))
2132
2133 (when (and pair-beg (eq (char-after pair-beg) ?{))
2134 ;; The last paren pair we moved out from was a brace
2135 ;; pair. Modify the state to record this as a closed
2136 ;; pair now.
2137 (if (consp (car-safe c-state-cache))
2138 (setq c-state-cache (cdr c-state-cache)))
2139 (setq c-state-cache (cons (cons pair-beg last-pos)
2140 c-state-cache))))
2141
2142 ;; Check if the preceding balanced paren is within a
2143 ;; macro; it should be ignored if we're outside the
2144 ;; macro. There's no need to check any further upwards;
2145 ;; if the macro contains an unbalanced opening paren then
2146 ;; we're smoked anyway.
2147 (when (and (<= (point) in-macro-start)
2148 (consp (car c-state-cache)))
2149 (save-excursion
2150 (goto-char (car (car c-state-cache)))
2151 (when (c-beginning-of-macro)
2152 (setq here (point)
2153 c-state-cache (cdr c-state-cache)))))
2154
2155 (unless (eq c-state-cache old-state)
2156 ;; Have to adjust the cached good position if state has been
2157 ;; popped off.
2158 (setq c-state-cache-good-pos
2159 (if c-state-cache
2160 (if (consp (car c-state-cache))
2161 (cdr (car c-state-cache))
2162 (1+ (car c-state-cache)))
2163 1)
2164 old-state c-state-cache))
2165
2166 (when c-state-cache
2167 (setq pos last-pos)))
2168
2169 ;; Get the fallback start position.
2170 (unless pos
2171 (setq pos (c-get-fallback-start-pos here)
2172 c-state-cache nil
2173 c-state-cache-good-pos 1))
2174
2175 (narrow-to-region (point-min) here)
2176
2177 (while pos
2178 (setq save-pos pos
2179 brace-pair-open nil)
2180
2181 ;; Find the balanced brace pairs. This loop is hot, so it
2182 ;; does ugly tricks to go faster.
2183 (c-safe
2184 (let (set-good-pos set-brace-pair)
2185 (while t
2186 (setq last-pos nil
2187 last-pos (scan-lists pos 1 -1)) ; Might signal.
2188 (setq pos (scan-lists last-pos 1 1) ; Might signal.
2189 set-good-pos (< pos here-bol)
2190 set-brace-pair (eq (char-before last-pos) ?{))
2191
2192 ;; Update the cached good position and record the brace
2193 ;; pair, whichever is applicable for the paren we've
2194 ;; just jumped over. But first check that it isn't
2195 ;; inside a macro and the point isn't inside the same
2196 ;; one.
2197 (when (and (or set-good-pos set-brace-pair)
2198 (or (>= pos in-macro-start)
2199 (save-excursion
2200 (goto-char pos)
2201 (not (c-beginning-of-macro)))))
2202 (if set-good-pos
2203 (setq c-state-cache-good-pos pos))
2204 (if set-brace-pair
2205 (setq brace-pair-open last-pos
2206 brace-pair-close pos))))))
2207
2208 ;; Record the last brace pair.
2209 (when brace-pair-open
2210 (let ((head (car-safe c-state-cache)))
2211 (if (consp head)
2212 (progn
2213 (setcar head (1- brace-pair-open))
2214 (setcdr head brace-pair-close))
2215 (setq c-state-cache (cons (cons (1- brace-pair-open)
2216 brace-pair-close)
2217 c-state-cache)))))
2218
2219 (if last-pos
2220 ;; Prepare to loop, but record the open paren only if it's
2221 ;; outside a macro or within the same macro as point, and
2222 ;; if it is a legitimate open paren and not some character
2223 ;; that got an open paren syntax-table property.
2224 (progn
2225 (setq pos last-pos)
2226 (when (and (or (>= last-pos in-macro-start)
2227 (save-excursion
2228 (goto-char last-pos)
2229 (not (c-beginning-of-macro))))
2230 ;; Check for known types of parens that we
2231 ;; want to record. The syntax table is not to
2232 ;; be trusted here since the caller might be
2233 ;; using e.g. `c++-template-syntax-table'.
2234 (memq (char-before last-pos) '(?{ ?\( ?\[)))
2235 (if (< last-pos here-bol)
2236 (setq c-state-cache-good-pos last-pos))
2237 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
2238
2239 (if (setq last-pos (c-up-list-forward pos))
2240 ;; Found a close paren without a corresponding opening
2241 ;; one. Maybe we didn't go back far enough, so try to
2242 ;; scan backward for the start paren and then start over.
2243 (progn
2244 (setq pos (c-up-list-backward pos)
2245 c-state-cache nil
2246 c-state-cache-good-pos c-state-cache-start)
2247 (when (or (not pos)
2248 ;; Emacs (up to at least 21.2) can get confused by
2249 ;; open parens in column zero inside comments: The
2250 ;; sexp functions can then misbehave and bring us
2251 ;; back to the same point again. Check this so that
2252 ;; we don't get an infinite loop.
2253 (>= pos save-pos))
2254 (setq pos last-pos
2255 c-parsing-error
2256 (format "Unbalanced close paren at line %d"
2257 (1+ (count-lines (point-min)
2258 (c-point 'bol last-pos)))))))
2259 (setq pos nil))))
2260
2261 ;;(message "c-parse-state: %S end: %S" c-state-cache c-state-cache-good-pos)
2262 c-state-cache)))
2263
2264 ;; Debug tool to catch cache inconsistencies.
2265 (defvar c-debug-parse-state nil)
2266 (unless (fboundp 'c-real-parse-state)
2267 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
2268 (cc-bytecomp-defun c-real-parse-state)
2269 (defun c-debug-parse-state ()
2270 (let ((res1 (c-real-parse-state)) res2)
2271 (let ((c-state-cache nil)
2272 (c-state-cache-start 1)
2273 (c-state-cache-good-pos 1))
2274 (setq res2 (c-real-parse-state)))
2275 (unless (equal res1 res2)
2276 ;; The cache can actually go further back due to the ad-hoc way
2277 ;; the first paren is found, so try to whack off a bit of its
2278 ;; start before complaining.
2279 (save-excursion
2280 (goto-char (or (c-least-enclosing-brace res2) (point)))
2281 (c-beginning-of-defun-1)
2282 (while (not (or (bobp) (eq (char-after) ?{)))
2283 (c-beginning-of-defun-1))
2284 (unless (equal (c-whack-state-before (point) res1) res2)
2285 (message (concat "c-parse-state inconsistency: "
2286 "using cache: %s, from scratch: %s")
2287 res1 res2))))
2288 res1))
2289 (defun c-toggle-parse-state-debug (&optional arg)
2290 (interactive "P")
2291 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
2292 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
2293 'c-debug-parse-state
2294 'c-real-parse-state)))
2295 (c-keep-region-active))
2296 (when c-debug-parse-state
2297 (c-toggle-parse-state-debug 1))
2298
2299 (defun c-whack-state-before (bufpos paren-state)
2300 ;; Whack off any state information from PAREN-STATE which lies
2301 ;; before BUFPOS. Not destructive on PAREN-STATE.
2302 (let* ((newstate (list nil))
2303 (ptr newstate)
2304 car)
2305 (while paren-state
2306 (setq car (car paren-state)
2307 paren-state (cdr paren-state))
2308 (if (< (if (consp car) (car car) car) bufpos)
2309 (setq paren-state nil)
2310 (setcdr ptr (list car))
2311 (setq ptr (cdr ptr))))
2312 (cdr newstate)))
2313
2314 (defun c-whack-state-after (bufpos paren-state)
2315 ;; Whack off any state information from PAREN-STATE which lies at or
2316 ;; after BUFPOS. Not destructive on PAREN-STATE.
2317 (catch 'done
2318 (while paren-state
2319 (let ((car (car paren-state)))
2320 (if (consp car)
2321 ;; just check the car, because in a balanced brace
2322 ;; expression, it must be impossible for the corresponding
2323 ;; close brace to be before point, but the open brace to
2324 ;; be after.
2325 (if (<= bufpos (car car))
2326 nil ; whack it off
2327 (if (< bufpos (cdr car))
2328 ;; its possible that the open brace is before
2329 ;; bufpos, but the close brace is after. In that
2330 ;; case, convert this to a non-cons element. The
2331 ;; rest of the state is before bufpos, so we're
2332 ;; done.
2333 (throw 'done (cons (car car) (cdr paren-state)))
2334 ;; we know that both the open and close braces are
2335 ;; before bufpos, so we also know that everything else
2336 ;; on state is before bufpos.
2337 (throw 'done paren-state)))
2338 (if (<= bufpos car)
2339 nil ; whack it off
2340 ;; it's before bufpos, so everything else should too.
2341 (throw 'done paren-state)))
2342 (setq paren-state (cdr paren-state)))
2343 nil)))
2344
2345 (defun c-most-enclosing-brace (paren-state &optional bufpos)
2346 ;; Return the bufpos of the innermost enclosing open paren before
2347 ;; bufpos, or nil if none was found.
2348 (let (enclosingp)
2349 (or bufpos (setq bufpos 134217727))
2350 (while paren-state
2351 (setq enclosingp (car paren-state)
2352 paren-state (cdr paren-state))
2353 (if (or (consp enclosingp)
2354 (>= enclosingp bufpos))
2355 (setq enclosingp nil)
2356 (setq paren-state nil)))
2357 enclosingp))
2358
2359 (defun c-least-enclosing-brace (paren-state)
2360 ;; Return the bufpos of the outermost enclosing open paren, or nil
2361 ;; if none was found.
2362 (let (pos elem)
2363 (while paren-state
2364 (setq elem (car paren-state)
2365 paren-state (cdr paren-state))
2366 (if (integerp elem)
2367 (setq pos elem)))
2368 pos))
2369
2370 (defun c-safe-position (bufpos paren-state)
2371 ;; Return the closest "safe" position recorded on PAREN-STATE that
2372 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
2373 ;; contain any. Return nil if BUFPOS is nil, which is useful to
2374 ;; find the closest limit before a given limit that might be nil.
2375 ;;
2376 ;; A "safe" position is a position at or after a recorded open
2377 ;; paren, or after a recorded close paren. The returned position is
2378 ;; thus either the first position after a close brace, or the first
2379 ;; position after an enclosing paren, or at the enclosing paren in
2380 ;; case BUFPOS is immediately after it.
2381 (when bufpos
2382 (let (elem)
2383 (catch 'done
2384 (while paren-state
2385 (setq elem (car paren-state))
2386 (if (consp elem)
2387 (cond ((< (cdr elem) bufpos)
2388 (throw 'done (cdr elem)))
2389 ((< (car elem) bufpos)
2390 ;; See below.
2391 (throw 'done (min (1+ (car elem)) bufpos))))
2392 (if (< elem bufpos)
2393 ;; elem is the position at and not after the opening paren, so
2394 ;; we can go forward one more step unless it's equal to
2395 ;; bufpos. This is useful in some cases avoid an extra paren
2396 ;; level between the safe position and bufpos.
2397 (throw 'done (min (1+ elem) bufpos))))
2398 (setq paren-state (cdr paren-state)))))))
2399
2400 (defun c-beginning-of-syntax ()
2401 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2402 ;; goes to the closest previous point that is known to be outside
2403 ;; any string literal or comment. `c-state-cache' is used if it has
2404 ;; a position in the vicinity.
2405 (let* ((paren-state c-state-cache)
2406 elem
2407
2408 (pos (catch 'done
2409 ;; Note: Similar code in `c-safe-position'. The
2410 ;; difference is that we accept a safe position at
2411 ;; the point and don't bother to go forward past open
2412 ;; parens.
2413 (while paren-state
2414 (setq elem (car paren-state))
2415 (if (consp elem)
2416 (cond ((<= (cdr elem) (point))
2417 (throw 'done (cdr elem)))
2418 ((<= (car elem) (point))
2419 (throw 'done (car elem))))
2420 (if (<= elem (point))
2421 (throw 'done elem)))
2422 (setq paren-state (cdr paren-state)))
2423 (point-min))))
2424
2425 (if (> pos (- (point) 4000))
2426 (goto-char pos)
2427 ;; The position is far back. Try `c-beginning-of-defun-1'
2428 ;; (although we can't be entirely sure it will go to a position
2429 ;; outside a comment or string in current emacsen). FIXME:
2430 ;; Consult `syntax-ppss' here.
2431 (c-beginning-of-defun-1)
2432 (if (< (point) pos)
2433 (goto-char pos)))))
2434
2435 \f
2436 ;; Tools for scanning identifiers and other tokens.
2437
2438 (defun c-on-identifier ()
2439 "Return non-nil if the point is on or directly after an identifier.
2440 Keywords are recognized and not considered identifiers. If an
2441 identifier is detected, the returned value is its starting position.
2442 If an identifier ends at the point and another begins at it \(can only
2443 happen in Pike) then the point for the preceding one is returned.
2444
2445 Note that this function might do hidden buffer changes. See the
2446 comment at the start of cc-engine.el for more info."
2447
2448 ;; FIXME: Shouldn't this function handle "operator" in C++?
2449
2450 (save-excursion
2451 (skip-syntax-backward "w_")
2452
2453 (or
2454
2455 ;; Check for a normal (non-keyword) identifier.
2456 (and (looking-at c-symbol-start)
2457 (not (looking-at c-keywords-regexp))
2458 (point))
2459
2460 (when (c-major-mode-is 'pike-mode)
2461 ;; Handle the `<operator> syntax in Pike.
2462 (let ((pos (point)))
2463 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2464 (and (if (< (skip-chars-backward "`") 0)
2465 t
2466 (goto-char pos)
2467 (eq (char-after) ?\`))
2468 (looking-at c-symbol-key)
2469 (>= (match-end 0) pos)
2470 (point))))
2471
2472 ;; Handle the "operator +" syntax in C++.
2473 (when (and c-overloadable-operators-regexp
2474 (= (c-backward-token-2 0) 0))
2475
2476 (cond ((and (looking-at c-overloadable-operators-regexp)
2477 (or (not c-opt-op-identifier-prefix)
2478 (and (= (c-backward-token-2 1) 0)
2479 (looking-at c-opt-op-identifier-prefix))))
2480 (point))
2481
2482 ((save-excursion
2483 (and c-opt-op-identifier-prefix
2484 (looking-at c-opt-op-identifier-prefix)
2485 (= (c-forward-token-2 1) 0)
2486 (looking-at c-overloadable-operators-regexp)))
2487 (point))))
2488
2489 )))
2490
2491 (defsubst c-simple-skip-symbol-backward ()
2492 ;; If the point is at the end of a symbol then skip backward to the
2493 ;; beginning of it. Don't move otherwise. Return non-nil if point
2494 ;; moved.
2495 ;;
2496 ;; This function might do hidden buffer changes.
2497 (or (< (skip-syntax-backward "w_") 0)
2498 (and (c-major-mode-is 'pike-mode)
2499 ;; Handle the `<operator> syntax in Pike.
2500 (let ((pos (point)))
2501 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2502 (< (skip-chars-backward "`") 0)
2503 (looking-at c-symbol-key)
2504 (>= (match-end 0) pos))
2505 t
2506 (goto-char pos)
2507 nil)))))
2508
2509 (defun c-beginning-of-current-token (&optional back-limit)
2510 ;; Move to the beginning of the current token. Do not move if not
2511 ;; in the middle of one. BACK-LIMIT may be used to bound the
2512 ;; backward search; if given it's assumed to be at the boundary
2513 ;; between two tokens. Return non-nil if the point is move, nil
2514 ;; otherwise.
2515 ;;
2516 ;; This function might do hidden buffer changes.
2517 (let ((start (point)))
2518 (if (looking-at "\\w\\|\\s_")
2519 (skip-syntax-backward "w_" back-limit)
2520 (when (< (skip-syntax-backward ".()" back-limit) 0)
2521 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2522 (match-end 0))
2523 ;; `c-nonsymbol-token-regexp' should always match
2524 ;; since we've skipped backward over punctuator
2525 ;; or paren syntax, but consume one char in case
2526 ;; it doesn't so that we don't leave point before
2527 ;; some earlier incorrect token.
2528 (1+ (point)))))
2529 (if (<= pos start)
2530 (goto-char pos))))))
2531 (< (point) start)))
2532
2533 (defun c-end-of-current-token (&optional back-limit)
2534 ;; Move to the end of the current token. Do not move if not in the
2535 ;; middle of one. BACK-LIMIT may be used to bound the backward
2536 ;; search; if given it's assumed to be at the boundary between two
2537 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2538 ;;
2539 ;; This function might do hidden buffer changes.
2540 (let ((start (point)))
2541 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2542 (skip-syntax-forward "w_"))
2543 ((< (skip-syntax-backward ".()" back-limit) 0)
2544 (while (progn
2545 (if (looking-at c-nonsymbol-token-regexp)
2546 (goto-char (match-end 0))
2547 ;; `c-nonsymbol-token-regexp' should always match since
2548 ;; we've skipped backward over punctuator or paren
2549 ;; syntax, but move forward in case it doesn't so that
2550 ;; we don't leave point earlier than we started with.
2551 (forward-char))
2552 (< (point) start)))))
2553 (> (point) start)))
2554
2555 (defconst c-jump-syntax-balanced
2556 (if (memq 'gen-string-delim c-emacs-features)
2557 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2558 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2559
2560 (defconst c-jump-syntax-unbalanced
2561 (if (memq 'gen-string-delim c-emacs-features)
2562 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2563 "\\w\\|\\s_\\|\\s\""))
2564
2565 (defun c-forward-token-2 (&optional count balanced limit)
2566 "Move forward by tokens.
2567 A token is defined as all symbols and identifiers which aren't
2568 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2569 treated properly). Point is always either left at the beginning of a
2570 token or not moved at all. COUNT specifies the number of tokens to
2571 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2572 moves to the next token beginning only if not already at one. If
2573 BALANCED is true, move over balanced parens, otherwise move into them.
2574 Also, if BALANCED is true, never move out of an enclosing paren.
2575
2576 LIMIT sets the limit for the movement and defaults to the point limit.
2577 The case when LIMIT is set in the middle of a token, comment or macro
2578 is handled correctly, i.e. the point won't be left there.
2579
2580 Return the number of tokens left to move \(positive or negative). If
2581 BALANCED is true, a move over a balanced paren counts as one. Note
2582 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2583 be returned. Thus, a return value of 0 guarantees that point is at
2584 the requested position and a return value less \(without signs) than
2585 COUNT guarantees that point is at the beginning of some token.
2586
2587 Note that this function might do hidden buffer changes. See the
2588 comment at the start of cc-engine.el for more info."
2589
2590 (or count (setq count 1))
2591 (if (< count 0)
2592 (- (c-backward-token-2 (- count) balanced limit))
2593
2594 (let ((jump-syntax (if balanced
2595 c-jump-syntax-balanced
2596 c-jump-syntax-unbalanced))
2597 (last (point))
2598 (prev (point)))
2599
2600 (if (zerop count)
2601 ;; If count is zero we should jump if in the middle of a token.
2602 (c-end-of-current-token))
2603
2604 (save-restriction
2605 (if limit (narrow-to-region (point-min) limit))
2606 (if (/= (point)
2607 (progn (c-forward-syntactic-ws) (point)))
2608 ;; Skip whitespace. Count this as a move if we did in
2609 ;; fact move.
2610 (setq count (max (1- count) 0)))
2611
2612 (if (eobp)
2613 ;; Moved out of bounds. Make sure the returned count isn't zero.
2614 (progn
2615 (if (zerop count) (setq count 1))
2616 (goto-char last))
2617
2618 ;; Use `condition-case' to avoid having the limit tests
2619 ;; inside the loop.
2620 (condition-case nil
2621 (while (and
2622 (> count 0)
2623 (progn
2624 (setq last (point))
2625 (cond ((looking-at jump-syntax)
2626 (goto-char (scan-sexps (point) 1))
2627 t)
2628 ((looking-at c-nonsymbol-token-regexp)
2629 (goto-char (match-end 0))
2630 t)
2631 ;; `c-nonsymbol-token-regexp' above should always
2632 ;; match if there are correct tokens. Try to
2633 ;; widen to see if the limit was set in the
2634 ;; middle of one, else fall back to treating
2635 ;; the offending thing as a one character token.
2636 ((and limit
2637 (save-restriction
2638 (widen)
2639 (looking-at c-nonsymbol-token-regexp)))
2640 nil)
2641 (t
2642 (forward-char)
2643 t))))
2644 (c-forward-syntactic-ws)
2645 (setq prev last
2646 count (1- count)))
2647 (error (goto-char last)))
2648
2649 (when (eobp)
2650 (goto-char prev)
2651 (setq count (1+ count)))))
2652
2653 count)))
2654
2655 (defun c-backward-token-2 (&optional count balanced limit)
2656 "Move backward by tokens.
2657 See `c-forward-token-2' for details."
2658
2659 (or count (setq count 1))
2660 (if (< count 0)
2661 (- (c-forward-token-2 (- count) balanced limit))
2662
2663 (or limit (setq limit (point-min)))
2664 (let ((jump-syntax (if balanced
2665 c-jump-syntax-balanced
2666 c-jump-syntax-unbalanced))
2667 (last (point)))
2668
2669 (if (zerop count)
2670 ;; The count is zero so try to skip to the beginning of the
2671 ;; current token.
2672 (if (> (point)
2673 (progn (c-beginning-of-current-token) (point)))
2674 (if (< (point) limit)
2675 ;; The limit is inside the same token, so return 1.
2676 (setq count 1))
2677
2678 ;; We're not in the middle of a token. If there's
2679 ;; whitespace after the point then we must move backward,
2680 ;; so set count to 1 in that case.
2681 (and (looking-at c-syntactic-ws-start)
2682 ;; If we're looking at a '#' that might start a cpp
2683 ;; directive then we have to do a more elaborate check.
2684 (or (/= (char-after) ?#)
2685 (not c-opt-cpp-prefix)
2686 (save-excursion
2687 (and (= (point)
2688 (progn (beginning-of-line)
2689 (looking-at "[ \t]*")
2690 (match-end 0)))
2691 (or (bobp)
2692 (progn (backward-char)
2693 (not (eq (char-before) ?\\)))))))
2694 (setq count 1))))
2695
2696 ;; Use `condition-case' to avoid having to check for buffer
2697 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2698 (condition-case nil
2699 (while (and
2700 (> count 0)
2701 (progn
2702 (c-backward-syntactic-ws)
2703 (backward-char)
2704 (if (looking-at jump-syntax)
2705 (goto-char (scan-sexps (1+ (point)) -1))
2706 ;; This can be very inefficient if there's a long
2707 ;; sequence of operator tokens without any separation.
2708 ;; That doesn't happen in practice, anyway.
2709 (c-beginning-of-current-token))
2710 (>= (point) limit)))
2711 (setq last (point)
2712 count (1- count)))
2713 (error (goto-char last)))
2714
2715 (if (< (point) limit)
2716 (goto-char last))
2717
2718 count)))
2719
2720 (defun c-forward-token-1 (&optional count balanced limit)
2721 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2722 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2723 characters are jumped over character by character. This function is
2724 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2725 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2726 (c-forward-token-2 count balanced limit)))
2727
2728 (defun c-backward-token-1 (&optional count balanced limit)
2729 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2730 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2731 characters are jumped over character by character. This function is
2732 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2733 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2734 (c-backward-token-2 count balanced limit)))
2735
2736 \f
2737 ;; Tools for doing searches restricted to syntactically relevant text.
2738
2739 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2740 paren-level not-inside-token
2741 lookbehind-submatch)
2742 "Like `re-search-forward', but only report matches that are found
2743 in syntactically significant text. I.e. matches in comments, macros
2744 or string literals are ignored. The start point is assumed to be
2745 outside any comment, macro or string literal, or else the content of
2746 that region is taken as syntactically significant text.
2747
2748 If PAREN-LEVEL is non-nil, an additional restriction is added to
2749 ignore matches in nested paren sexps. The search will also not go
2750 outside the current list sexp, which has the effect that if the point
2751 should be moved to BOUND when no match is found \(i.e. NOERROR is
2752 neither nil nor t), then it will be at the closing paren if the end of
2753 the current list sexp is encountered first.
2754
2755 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2756 ignored. Things like multicharacter operators and special symbols
2757 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2758 constants.
2759
2760 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2761 subexpression in REGEXP. The end of that submatch is used as the
2762 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2763 isn't used or if that subexpression didn't match then the start
2764 position of the whole match is used instead. The \"look behind\"
2765 subexpression is never tested before the starting position, so it
2766 might be a good idea to include \\=\\= as a match alternative in it.
2767
2768 Optimization note: Matches might be missed if the \"look behind\"
2769 subexpression can match the end of nonwhite syntactic whitespace,
2770 i.e. the end of comments or cpp directives. This since the function
2771 skips over such things before resuming the search. It's on the other
2772 hand not safe to assume that the \"look behind\" subexpression never
2773 matches syntactic whitespace.
2774
2775 Bug: Unbalanced parens inside cpp directives are currently not handled
2776 correctly \(i.e. they don't get ignored as they should) when
2777 PAREN-LEVEL is set.
2778
2779 Note that this function might do hidden buffer changes. See the
2780 comment at the start of cc-engine.el for more info."
2781
2782 (or bound (setq bound (point-max)))
2783 (if paren-level (setq paren-level -1))
2784
2785 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2786
2787 (let ((start (point))
2788 tmp
2789 ;; Start position for the last search.
2790 search-pos
2791 ;; The `parse-partial-sexp' state between the start position
2792 ;; and the point.
2793 state
2794 ;; The current position after the last state update. The next
2795 ;; `parse-partial-sexp' continues from here.
2796 (state-pos (point))
2797 ;; The position at which to check the state and the state
2798 ;; there. This is separate from `state-pos' since we might
2799 ;; need to back up before doing the next search round.
2800 check-pos check-state
2801 ;; Last position known to end a token.
2802 (last-token-end-pos (point-min))
2803 ;; Set when a valid match is found.
2804 found)
2805
2806 (condition-case err
2807 (while
2808 (and
2809 (progn
2810 (setq search-pos (point))
2811 (re-search-forward regexp bound noerror))
2812
2813 (progn
2814 (setq state (parse-partial-sexp
2815 state-pos (match-beginning 0) paren-level nil state)
2816 state-pos (point))
2817 (if (setq check-pos (and lookbehind-submatch
2818 (or (not paren-level)
2819 (>= (car state) 0))
2820 (match-end lookbehind-submatch)))
2821 (setq check-state (parse-partial-sexp
2822 state-pos check-pos paren-level nil state))
2823 (setq check-pos state-pos
2824 check-state state))
2825
2826 ;; NOTE: If we got a look behind subexpression and get
2827 ;; an insignificant match in something that isn't
2828 ;; syntactic whitespace (i.e. strings or in nested
2829 ;; parentheses), then we can never skip more than a
2830 ;; single character from the match start position
2831 ;; (i.e. `state-pos' here) before continuing the
2832 ;; search. That since the look behind subexpression
2833 ;; might match the end of the insignificant region in
2834 ;; the next search.
2835
2836 (cond
2837 ((elt check-state 7)
2838 ;; Match inside a line comment. Skip to eol. Use
2839 ;; `re-search-forward' instead of `skip-chars-forward' to get
2840 ;; the right bound behavior.
2841 (re-search-forward "[\n\r]" bound noerror))
2842
2843 ((elt check-state 4)
2844 ;; Match inside a block comment. Skip to the '*/'.
2845 (search-forward "*/" bound noerror))
2846
2847 ((and (not (elt check-state 5))
2848 (eq (char-before check-pos) ?/)
2849 (not (c-get-char-property (1- check-pos) 'syntax-table))
2850 (memq (char-after check-pos) '(?/ ?*)))
2851 ;; Match in the middle of the opener of a block or line
2852 ;; comment.
2853 (if (= (char-after check-pos) ?/)
2854 (re-search-forward "[\n\r]" bound noerror)
2855 (search-forward "*/" bound noerror)))
2856
2857 ;; The last `parse-partial-sexp' above might have
2858 ;; stopped short of the real check position if the end
2859 ;; of the current sexp was encountered in paren-level
2860 ;; mode. The checks above are always false in that
2861 ;; case, and since they can do better skipping in
2862 ;; lookbehind-submatch mode, we do them before
2863 ;; checking the paren level.
2864
2865 ((and paren-level
2866 (/= (setq tmp (car check-state)) 0))
2867 ;; Check the paren level first since we're short of the
2868 ;; syntactic checking position if the end of the
2869 ;; current sexp was encountered by `parse-partial-sexp'.
2870 (if (> tmp 0)
2871
2872 ;; Inside a nested paren sexp.
2873 (if lookbehind-submatch
2874 ;; See the NOTE above.
2875 (progn (goto-char state-pos) t)
2876 ;; Skip out of the paren quickly.
2877 (setq state (parse-partial-sexp state-pos bound 0 nil state)
2878 state-pos (point)))
2879
2880 ;; Have exited the current paren sexp.
2881 (if noerror
2882 (progn
2883 ;; The last `parse-partial-sexp' call above
2884 ;; has left us just after the closing paren
2885 ;; in this case, so we can modify the bound
2886 ;; to leave the point at the right position
2887 ;; upon return.
2888 (setq bound (1- (point)))
2889 nil)
2890 (signal 'search-failed (list regexp)))))
2891
2892 ((setq tmp (elt check-state 3))
2893 ;; Match inside a string.
2894 (if (or lookbehind-submatch
2895 (not (integerp tmp)))
2896 ;; See the NOTE above.
2897 (progn (goto-char state-pos) t)
2898 ;; Skip to the end of the string before continuing.
2899 (let ((ender (make-string 1 tmp)) (continue t))
2900 (while (if (search-forward ender bound noerror)
2901 (progn
2902 (setq state (parse-partial-sexp
2903 state-pos (point) nil nil state)
2904 state-pos (point))
2905 (elt state 3))
2906 (setq continue nil)))
2907 continue)))
2908
2909 ((save-excursion
2910 (save-match-data
2911 (c-beginning-of-macro start)))
2912 ;; Match inside a macro. Skip to the end of it.
2913 (c-end-of-macro)
2914 (cond ((<= (point) bound) t)
2915 (noerror nil)
2916 (t (signal 'search-failed (list regexp)))))
2917
2918 ((and not-inside-token
2919 (or (< check-pos last-token-end-pos)
2920 (< check-pos
2921 (save-excursion
2922 (goto-char check-pos)
2923 (save-match-data
2924 (c-end-of-current-token last-token-end-pos))
2925 (setq last-token-end-pos (point))))))
2926 ;; Inside a token.
2927 (if lookbehind-submatch
2928 ;; See the NOTE above.
2929 (goto-char state-pos)
2930 (goto-char (min last-token-end-pos bound))))
2931
2932 (t
2933 ;; A real match.
2934 (setq found t)
2935 nil)))
2936
2937 ;; Should loop to search again, but take care to avoid
2938 ;; looping on the same spot.
2939 (or (/= search-pos (point))
2940 (if (= (point) bound)
2941 (if noerror
2942 nil
2943 (signal 'search-failed (list regexp)))
2944 (forward-char)
2945 t))))
2946
2947 (error
2948 (goto-char start)
2949 (signal (car err) (cdr err))))
2950
2951 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2952
2953 (if found
2954 (progn
2955 (goto-char (match-end 0))
2956 (match-end 0))
2957
2958 ;; Search failed. Set point as appropriate.
2959 (if (eq noerror t)
2960 (goto-char start)
2961 (goto-char bound))
2962 nil)))
2963
2964 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
2965 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2966 i.e. don't stop at positions inside syntactic whitespace or string
2967 literals. Preprocessor directives are also ignored, with the exception
2968 of the one that the point starts within, if any. If LIMIT is given,
2969 it's assumed to be at a syntactically relevant position.
2970
2971 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
2972 sexps, and the search will also not go outside the current paren sexp.
2973 However, if LIMIT or the buffer limit is reached inside a nested paren
2974 then the point will be left at the limit.
2975
2976 Non-nil is returned if the point moved, nil otherwise.
2977
2978 Note that this function might do hidden buffer changes. See the
2979 comment at the start of cc-engine.el for more info."
2980
2981 (let ((start (point))
2982 state
2983 ;; A list of syntactically relevant positions in descending
2984 ;; order. It's used to avoid scanning repeatedly over
2985 ;; potentially large regions with `parse-partial-sexp' to verify
2986 ;; each position.
2987 safe-pos-list
2988 ;; The position at the beginning of `safe-pos-list'.
2989 safe-pos
2990 ;; The result from `c-beginning-of-macro' at the start position or the
2991 ;; start position itself if it isn't within a macro. Evaluated on
2992 ;; demand.
2993 start-macro-beg
2994 ;; The earliest position after the current one with the same paren
2995 ;; level. Used only when `paren-level' is set.
2996 (paren-level-pos (point)))
2997
2998 (while (progn
2999 (while (and
3000 (< (skip-chars-backward skip-chars limit) 0)
3001
3002 ;; Use `parse-partial-sexp' from a safe position down to
3003 ;; the point to check if it's outside comments and
3004 ;; strings.
3005 (let ((pos (point)) state-2 pps-end-pos)
3006 ;; Pick a safe position as close to the point as
3007 ;; possible.
3008 ;;
3009 ;; FIXME: Consult `syntax-ppss' here if our
3010 ;; cache doesn't give a good position.
3011 (while (and safe-pos-list
3012 (> (car safe-pos-list) (point)))
3013 (setq safe-pos-list (cdr safe-pos-list)))
3014 (unless (setq safe-pos (car-safe safe-pos-list))
3015 (setq safe-pos (max (or (c-safe-position
3016 (point) (or c-state-cache
3017 (c-parse-state)))
3018 0)
3019 (point-min))
3020 safe-pos-list (list safe-pos)))
3021
3022 ;; Cache positions along the way to use if we have to
3023 ;; back up more. We cache every closing paren on the
3024 ;; same level. If the paren cache is relevant in this
3025 ;; region then we're typically already on the same
3026 ;; level as the target position. Note that we might
3027 ;; cache positions after opening parens in case
3028 ;; safe-pos is in a nested list. That's both uncommon
3029 ;; and harmless.
3030 (while (progn
3031 (setq state (parse-partial-sexp
3032 safe-pos pos 0))
3033 (< (point) pos))
3034 (setq safe-pos (point)
3035 safe-pos-list (cons safe-pos safe-pos-list)))
3036
3037 (cond
3038 ((or (elt state 3) (elt state 4))
3039 ;; Inside string or comment. Continue search at the
3040 ;; beginning of it.
3041 (goto-char (elt state 8))
3042 t)
3043
3044 ((and paren-level
3045 (save-excursion
3046 (setq state-2 (parse-partial-sexp
3047 pos paren-level-pos -1)
3048 pps-end-pos (point))
3049 (/= (car state-2) 0)))
3050 ;; Not at the right level.
3051
3052 (if (and (< (car state-2) 0)
3053 ;; We stop above if we go out of a paren.
3054 ;; Now check whether it precedes or is
3055 ;; nested in the starting sexp.
3056 (save-excursion
3057 (setq state-2
3058 (parse-partial-sexp
3059 pps-end-pos paren-level-pos
3060 nil nil state-2))
3061 (< (car state-2) 0)))
3062
3063 ;; We've stopped short of the starting position
3064 ;; so the hit was inside a nested list. Go up
3065 ;; until we are at the right level.
3066 (condition-case nil
3067 (progn
3068 (goto-char (scan-lists pos -1
3069 (- (car state-2))))
3070 (setq paren-level-pos (point))
3071 (if (and limit (>= limit paren-level-pos))
3072 (progn
3073 (goto-char limit)
3074 nil)
3075 t))
3076 (error
3077 (goto-char (or limit (point-min)))
3078 nil))
3079
3080 ;; The hit was outside the list at the start
3081 ;; position. Go to the start of the list and exit.
3082 (goto-char (1+ (elt state-2 1)))
3083 nil))
3084
3085 ((c-beginning-of-macro limit)
3086 ;; Inside a macro.
3087 (if (< (point)
3088 (or start-macro-beg
3089 (setq start-macro-beg
3090 (save-excursion
3091 (goto-char start)
3092 (c-beginning-of-macro limit)
3093 (point)))))
3094 t
3095
3096 ;; It's inside the same macro we started in so it's
3097 ;; a relevant match.
3098 (goto-char pos)
3099 nil)))))
3100
3101 ;; If the state contains the start of the containing sexp we
3102 ;; cache that position too, so that parse-partial-sexp in the
3103 ;; next run has a bigger chance of starting at the same level
3104 ;; as the target position and thus will get more good safe
3105 ;; positions into the list.
3106 (if (elt state 1)
3107 (setq safe-pos (1+ (elt state 1))
3108 safe-pos-list (cons safe-pos safe-pos-list))))
3109
3110 (> (point)
3111 (progn
3112 ;; Skip syntactic ws afterwards so that we don't stop at the
3113 ;; end of a comment if `skip-chars' is something like "^/".
3114 (c-backward-syntactic-ws)
3115 (point)))))
3116
3117 ;; We might want to extend this with more useful return values in
3118 ;; the future.
3119 (/= (point) start)))
3120
3121 ;; The following is an alternative implementation of
3122 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3123 ;; track of the syntactic context. It turned out to be generally
3124 ;; slower than the one above which uses forward checks from earlier
3125 ;; safe positions.
3126 ;;
3127 ;;(defconst c-ssb-stop-re
3128 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3129 ;; ;; stop at to avoid going into comments and literals.
3130 ;; (concat
3131 ;; ;; Match comment end syntax and string literal syntax. Also match
3132 ;; ;; '/' for block comment endings (not covered by comment end
3133 ;; ;; syntax).
3134 ;; "\\s>\\|/\\|\\s\""
3135 ;; (if (memq 'gen-string-delim c-emacs-features)
3136 ;; "\\|\\s|"
3137 ;; "")
3138 ;; (if (memq 'gen-comment-delim c-emacs-features)
3139 ;; "\\|\\s!"
3140 ;; "")))
3141 ;;
3142 ;;(defconst c-ssb-stop-paren-re
3143 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3144 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3145 ;;
3146 ;;(defconst c-ssb-sexp-end-re
3147 ;; ;; Regexp matching the ending syntax of a complex sexp.
3148 ;; (concat c-string-limit-regexp "\\|\\s)"))
3149 ;;
3150 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3151 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3152 ;;i.e. don't stop at positions inside syntactic whitespace or string
3153 ;;literals. Preprocessor directives are also ignored. However, if the
3154 ;;point is within a comment, string literal or preprocessor directory to
3155 ;;begin with, its contents is treated as syntactically relevant chars.
3156 ;;If LIMIT is given, it limits the backward search and the point will be
3157 ;;left there if no earlier position is found.
3158 ;;
3159 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3160 ;;sexps, and the search will also not go outside the current paren sexp.
3161 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3162 ;;then the point will be left at the limit.
3163 ;;
3164 ;;Non-nil is returned if the point moved, nil otherwise.
3165 ;;
3166 ;;Note that this function might do hidden buffer changes. See the
3167 ;;comment at the start of cc-engine.el for more info."
3168 ;;
3169 ;; (save-restriction
3170 ;; (when limit
3171 ;; (narrow-to-region limit (point-max)))
3172 ;;
3173 ;; (let ((start (point)))
3174 ;; (catch 'done
3175 ;; (while (let ((last-pos (point))
3176 ;; (stop-pos (progn
3177 ;; (skip-chars-backward skip-chars)
3178 ;; (point))))
3179 ;;
3180 ;; ;; Skip back over the same region as
3181 ;; ;; `skip-chars-backward' above, but keep to
3182 ;; ;; syntactically relevant positions.
3183 ;; (goto-char last-pos)
3184 ;; (while (and
3185 ;; ;; `re-search-backward' with a single char regexp
3186 ;; ;; should be fast.
3187 ;; (re-search-backward
3188 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3189 ;; stop-pos 'move)
3190 ;;
3191 ;; (progn
3192 ;; (cond
3193 ;; ((looking-at "\\s(")
3194 ;; ;; `paren-level' is set and we've found the
3195 ;; ;; start of the containing paren.
3196 ;; (forward-char)
3197 ;; (throw 'done t))
3198 ;;
3199 ;; ((looking-at c-ssb-sexp-end-re)
3200 ;; ;; We're at the end of a string literal or paren
3201 ;; ;; sexp (if `paren-level' is set).
3202 ;; (forward-char)
3203 ;; (condition-case nil
3204 ;; (c-backward-sexp)
3205 ;; (error
3206 ;; (goto-char limit)
3207 ;; (throw 'done t))))
3208 ;;
3209 ;; (t
3210 ;; (forward-char)
3211 ;; ;; At the end of some syntactic ws or possibly
3212 ;; ;; after a plain '/' operator.
3213 ;; (let ((pos (point)))
3214 ;; (c-backward-syntactic-ws)
3215 ;; (if (= pos (point))
3216 ;; ;; Was a plain '/' operator. Go past it.
3217 ;; (backward-char)))))
3218 ;;
3219 ;; (> (point) stop-pos))))
3220 ;;
3221 ;; ;; Now the point is either at `stop-pos' or at some
3222 ;; ;; position further back if `stop-pos' was at a
3223 ;; ;; syntactically irrelevant place.
3224 ;;
3225 ;; ;; Skip additional syntactic ws so that we don't stop
3226 ;; ;; at the end of a comment if `skip-chars' is
3227 ;; ;; something like "^/".
3228 ;; (c-backward-syntactic-ws)
3229 ;;
3230 ;; (< (point) stop-pos))))
3231 ;;
3232 ;; ;; We might want to extend this with more useful return values
3233 ;; ;; in the future.
3234 ;; (/= (point) start))))
3235
3236 \f
3237 ;; Tools for handling comments and string literals.
3238
3239 (defun c-slow-in-literal (&optional lim detect-cpp)
3240 "Return the type of literal point is in, if any.
3241 The return value is `c' if in a C-style comment, `c++' if in a C++
3242 style comment, `string' if in a string literal, `pound' if DETECT-CPP
3243 is non-nil and in a preprocessor line, or nil if somewhere else.
3244 Optional LIM is used as the backward limit of the search. If omitted,
3245 or nil, `c-beginning-of-defun' is used.
3246
3247 The last point calculated is cached if the cache is enabled, i.e. if
3248 `c-in-literal-cache' is bound to a two element vector.
3249
3250 Note that this function might do hidden buffer changes. See the
3251 comment at the start of cc-engine.el for more info."
3252
3253 (if (and (vectorp c-in-literal-cache)
3254 (= (point) (aref c-in-literal-cache 0)))
3255 (aref c-in-literal-cache 1)
3256 (let ((rtn (save-excursion
3257 (let* ((pos (point))
3258 (lim (or lim (progn
3259 (c-beginning-of-syntax)
3260 (point))))
3261 (state (parse-partial-sexp lim pos)))
3262 (cond
3263 ((elt state 3) 'string)
3264 ((elt state 4) (if (elt state 7) 'c++ 'c))
3265 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
3266 (t nil))))))
3267 ;; cache this result if the cache is enabled
3268 (if (not c-in-literal-cache)
3269 (setq c-in-literal-cache (vector (point) rtn)))
3270 rtn)))
3271
3272 ;; XEmacs has a built-in function that should make this much quicker.
3273 ;; I don't think we even need the cache, which makes our lives more
3274 ;; complicated anyway. In this case, lim is only used to detect
3275 ;; cpp directives.
3276 ;;
3277 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
3278 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
3279 ;; Xemacs 21.4.4. It manifested itself thus:
3280 ;;
3281 ;; Starting with an empty AWK Mode buffer, type
3282 ;; /regexp/ {<C-j>
3283 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
3284 ;;
3285 ;; AWK Mode is designed such that when the first / is typed, it gets the
3286 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
3287 ;; are given the s-t property "string". However, buffer-syntactic-context
3288 ;; fails to take account of the change of the s-t property on the opening / to
3289 ;; "string", and reports that the { is within a string started by the second /.
3290 ;;
3291 ;; The workaround for this is for the AWK Mode initialisation to switch the
3292 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
3293 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
3294 ;;
3295 ;; (Alan Mackenzie, 2003/4/30).
3296
3297 (defun c-fast-in-literal (&optional lim detect-cpp)
3298 ;; This function might do hidden buffer changes.
3299 (let ((context (buffer-syntactic-context)))
3300 (cond
3301 ((eq context 'string) 'string)
3302 ((eq context 'comment) 'c++)
3303 ((eq context 'block-comment) 'c)
3304 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
3305
3306 (defalias 'c-in-literal
3307 (if (fboundp 'buffer-syntactic-context)
3308 'c-fast-in-literal ; XEmacs
3309 'c-slow-in-literal)) ; GNU Emacs
3310
3311 ;; The defalias above isn't enough to shut up the byte compiler.
3312 (cc-bytecomp-defun c-in-literal)
3313
3314 (defun c-literal-limits (&optional lim near not-in-delimiter)
3315 "Return a cons of the beginning and end positions of the comment or
3316 string surrounding point (including both delimiters), or nil if point
3317 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
3318 to start parsing from. If NEAR is non-nil, then the limits of any
3319 literal next to point is returned. \"Next to\" means there's only
3320 spaces and tabs between point and the literal. The search for such a
3321 literal is done first in forward direction. If NOT-IN-DELIMITER is
3322 non-nil, the case when point is inside a starting delimiter won't be
3323 recognized. This only has effect for comments, which have starting
3324 delimiters with more than one character.
3325
3326 Note that this function might do hidden buffer changes. See the
3327 comment at the start of cc-engine.el for more info."
3328
3329 (save-excursion
3330 (let* ((pos (point))
3331 (lim (or lim (progn
3332 (c-beginning-of-syntax)
3333 (point))))
3334 (state (parse-partial-sexp lim pos)))
3335
3336 (cond ((elt state 3) ; String.
3337 (goto-char (elt state 8))
3338 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3339 (point-max))))
3340
3341 ((elt state 4) ; Comment.
3342 (goto-char (elt state 8))
3343 (cons (point) (progn (c-forward-single-comment) (point))))
3344
3345 ((and (not not-in-delimiter)
3346 (not (elt state 5))
3347 (eq (char-before) ?/)
3348 (looking-at "[/*]"))
3349 ;; We're standing in a comment starter.
3350 (backward-char 1)
3351 (cons (point) (progn (c-forward-single-comment) (point))))
3352
3353 (near
3354 (goto-char pos)
3355
3356 ;; Search forward for a literal.
3357 (skip-chars-forward " \t")
3358
3359 (cond
3360 ((looking-at c-string-limit-regexp) ; String.
3361 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3362 (point-max))))
3363
3364 ((looking-at c-comment-start-regexp) ; Line or block comment.
3365 (cons (point) (progn (c-forward-single-comment) (point))))
3366
3367 (t
3368 ;; Search backward.
3369 (skip-chars-backward " \t")
3370
3371 (let ((end (point)) beg)
3372 (cond
3373 ((save-excursion
3374 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
3375 (setq beg (c-safe (c-backward-sexp 1) (point))))
3376
3377 ((and (c-safe (forward-char -2) t)
3378 (looking-at "*/"))
3379 ;; Block comment. Due to the nature of line
3380 ;; comments, they will always be covered by the
3381 ;; normal case above.
3382 (goto-char end)
3383 (c-backward-single-comment)
3384 ;; If LIM is bogus, beg will be bogus.
3385 (setq beg (point))))
3386
3387 (if beg (cons beg end))))))
3388 ))))
3389
3390 ;; In case external callers use this; it did have a docstring.
3391 (defalias 'c-literal-limits-fast 'c-literal-limits)
3392
3393 (defun c-collect-line-comments (range)
3394 "If the argument is a cons of two buffer positions (such as returned by
3395 `c-literal-limits'), and that range contains a C++ style line comment,
3396 then an extended range is returned that contains all adjacent line
3397 comments (i.e. all comments that starts in the same column with no
3398 empty lines or non-whitespace characters between them). Otherwise the
3399 argument is returned.
3400
3401 Note that this function might do hidden buffer changes. See the
3402 comment at the start of cc-engine.el for more info."
3403
3404 (save-excursion
3405 (condition-case nil
3406 (if (and (consp range) (progn
3407 (goto-char (car range))
3408 (looking-at c-line-comment-starter)))
3409 (let ((col (current-column))
3410 (beg (point))
3411 (bopl (c-point 'bopl))
3412 (end (cdr range)))
3413 ;; Got to take care in the backward direction to handle
3414 ;; comments which are preceded by code.
3415 (while (and (c-backward-single-comment)
3416 (>= (point) bopl)
3417 (looking-at c-line-comment-starter)
3418 (= col (current-column)))
3419 (setq beg (point)
3420 bopl (c-point 'bopl)))
3421 (goto-char end)
3422 (while (and (progn (skip-chars-forward " \t")
3423 (looking-at c-line-comment-starter))
3424 (= col (current-column))
3425 (prog1 (zerop (forward-line 1))
3426 (setq end (point)))))
3427 (cons beg end))
3428 range)
3429 (error range))))
3430
3431 (defun c-literal-type (range)
3432 "Convenience function that given the result of `c-literal-limits',
3433 returns nil or the type of literal that the range surrounds. It's
3434 much faster than using `c-in-literal' and is intended to be used when
3435 you need both the type of a literal and its limits.
3436
3437 Note that this function might do hidden buffer changes. See the
3438 comment at the start of cc-engine.el for more info."
3439
3440 (if (consp range)
3441 (save-excursion
3442 (goto-char (car range))
3443 (cond ((looking-at c-string-limit-regexp) 'string)
3444 ((or (looking-at "//") ; c++ line comment
3445 (and (looking-at "\\s<") ; comment starter
3446 (looking-at "#"))) ; awk comment.
3447 'c++)
3448 (t 'c))) ; Assuming the range is valid.
3449 range))
3450
3451 \f
3452 ;; `c-find-decl-spots' and accompanying stuff.
3453
3454 ;; Variables used in `c-find-decl-spots' to cache the search done for
3455 ;; the first declaration in the last call. When that function starts,
3456 ;; it needs to back up over syntactic whitespace to look at the last
3457 ;; token before the region being searched. That can sometimes cause
3458 ;; moves back and forth over a quite large region of comments and
3459 ;; macros, which would be repeated for each changed character when
3460 ;; we're called during fontification, since font-lock refontifies the
3461 ;; current line for each change. Thus it's worthwhile to cache the
3462 ;; first match.
3463 ;;
3464 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3465 ;; the syntactic whitespace less or equal to some start position.
3466 ;; There's no cached value if it's nil.
3467 ;;
3468 ;; `c-find-decl-match-pos' is the match position if
3469 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3470 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3471 (defvar c-find-decl-syntactic-pos nil)
3472 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
3473 (defvar c-find-decl-match-pos nil)
3474 (make-variable-buffer-local 'c-find-decl-match-pos)
3475
3476 (defsubst c-invalidate-find-decl-cache (change-min-pos)
3477 (and c-find-decl-syntactic-pos
3478 (< change-min-pos c-find-decl-syntactic-pos)
3479 (setq c-find-decl-syntactic-pos nil)))
3480
3481 ; (defface c-debug-decl-spot-face
3482 ; '((t (:background "Turquoise")))
3483 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3484 ; (defface c-debug-decl-sws-face
3485 ; '((t (:background "Khaki")))
3486 ; "Debug face to mark the syntactic whitespace between the declaration
3487 ; spots and the preceding token end.")
3488
3489 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
3490 (when (facep 'c-debug-decl-spot-face)
3491 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
3492 (c-debug-add-face (max match-pos (point-min)) decl-pos
3493 'c-debug-decl-sws-face)
3494 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
3495 'c-debug-decl-spot-face))))
3496 (defmacro c-debug-remove-decl-spot-faces (beg end)
3497 (when (facep 'c-debug-decl-spot-face)
3498 `(c-save-buffer-state ()
3499 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
3500 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
3501
3502 (defmacro c-find-decl-prefix-search ()
3503 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3504 ;; but it contains lots of free variables that refer to things
3505 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3506 ;; if there is a match, otherwise at `cfd-limit'.
3507 ;;
3508 ;; This macro might do hidden buffer changes.
3509
3510 '(progn
3511 ;; Find the next property match position if we haven't got one already.
3512 (unless cfd-prop-match
3513 (save-excursion
3514 (while (progn
3515 (goto-char (next-single-property-change
3516 (point) 'c-type nil cfd-limit))
3517 (and (< (point) cfd-limit)
3518 (not (eq (c-get-char-property (1- (point)) 'c-type)
3519 'c-decl-end)))))
3520 (setq cfd-prop-match (point))))
3521
3522 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
3523 ;; got one already.
3524 (unless cfd-re-match
3525
3526 (if (> cfd-re-match-end (point))
3527 (goto-char cfd-re-match-end))
3528
3529 (while (if (setq cfd-re-match-end
3530 (re-search-forward c-decl-prefix-or-start-re
3531 cfd-limit 'move))
3532
3533 ;; Match. Check if it's inside a comment or string literal.
3534 (c-got-face-at
3535 (if (setq cfd-re-match (match-end 1))
3536 ;; Matched the end of a token preceding a decl spot.
3537 (progn
3538 (goto-char cfd-re-match)
3539 (1- cfd-re-match))
3540 ;; Matched a token that start a decl spot.
3541 (goto-char (match-beginning 0))
3542 (point))
3543 c-literal-faces)
3544
3545 ;; No match. Finish up and exit the loop.
3546 (setq cfd-re-match cfd-limit)
3547 nil)
3548
3549 ;; Skip out of comments and string literals.
3550 (while (progn
3551 (goto-char (next-single-property-change
3552 (point) 'face nil cfd-limit))
3553 (and (< (point) cfd-limit)
3554 (c-got-face-at (point) c-literal-faces)))))
3555
3556 ;; If we matched at the decl start, we have to back up over the
3557 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
3558 ;; any decl spots in the syntactic ws.
3559 (unless cfd-re-match
3560 (c-backward-syntactic-ws)
3561 (setq cfd-re-match (point))))
3562
3563 ;; Choose whichever match is closer to the start.
3564 (if (< cfd-re-match cfd-prop-match)
3565 (setq cfd-match-pos cfd-re-match
3566 cfd-re-match nil)
3567 (setq cfd-match-pos cfd-prop-match
3568 cfd-prop-match nil))
3569
3570 (goto-char cfd-match-pos)
3571
3572 (when (< cfd-match-pos cfd-limit)
3573 ;; Skip forward past comments only so we don't skip macros.
3574 (c-forward-comments)
3575 ;; Set the position to continue at. We can avoid going over
3576 ;; the comments skipped above a second time, but it's possible
3577 ;; that the comment skipping has taken us past `cfd-prop-match'
3578 ;; since the property might be used inside comments.
3579 (setq cfd-continue-pos (if cfd-prop-match
3580 (min cfd-prop-match (point))
3581 (point))))))
3582
3583 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3584 ;; Call CFD-FUN for each possible spot for a declaration, cast or
3585 ;; label from the point to CFD-LIMIT. Such a spot is:
3586 ;;
3587 ;; o The first token after bob.
3588 ;; o The first token after the end of submatch 1 in
3589 ;; `c-decl-prefix-or-start-re' when that submatch matches.
3590 ;; o The start of each `c-decl-prefix-or-start-re' match when
3591 ;; submatch 1 doesn't match.
3592 ;; o The first token after the end of each occurence of the
3593 ;; `c-type' text property with the value `c-decl-end', provided
3594 ;; `c-type-decl-end-used' is set.
3595 ;;
3596 ;; Only a spot that match CFD-DECL-RE and whose face is in the
3597 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
3598 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
3599 ;;
3600 ;; If the match is inside a macro then the buffer is narrowed to the
3601 ;; end of it, so that CFD-FUN can investigate the following tokens
3602 ;; without matching something that begins inside a macro and ends
3603 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3604 ;; CFD-FACE-CHECKLIST checks exist.
3605 ;;
3606 ;; CFD-FUN is called with point at the start of the spot. It's
3607 ;; passed two arguments: The first is the end position of the token
3608 ;; preceding the spot, or 0 for the implicit match at bob. The
3609 ;; second is a flag that is t when the match is inside a macro. If
3610 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
3611 ;; spot, it should return non-nil to ensure that the next search
3612 ;; will find them.
3613 ;;
3614 ;; The spots are visited approximately in order from top to bottom.
3615 ;; It's however the positions where `c-decl-prefix-or-start-re'
3616 ;; matches and where `c-decl-end' properties are found that are in
3617 ;; order. Since the spots often are at the following token, they
3618 ;; might be visited out of order insofar as more spots are reported
3619 ;; later on within the syntactic whitespace between the match
3620 ;; positions and their spots.
3621 ;;
3622 ;; It's assumed that comments and strings are fontified in the
3623 ;; searched range.
3624 ;;
3625 ;; This is mainly used in fontification, and so has an elaborate
3626 ;; cache to handle repeated calls from the same start position; see
3627 ;; the variables above.
3628 ;;
3629 ;; All variables in this function begin with `cfd-' to avoid name
3630 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3631 ;;
3632 ;; This function might do hidden buffer changes.
3633
3634 (let ((cfd-start-pos (point))
3635 (cfd-buffer-end (point-max))
3636 ;; The end of the token preceding the decl spot last found
3637 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
3638 ;; no match.
3639 cfd-re-match
3640 ;; The end position of the last `c-decl-prefix-or-start-re'
3641 ;; match. If this is greater than `cfd-continue-pos', the
3642 ;; next regexp search is started here instead.
3643 (cfd-re-match-end (point-min))
3644 ;; The end of the last `c-decl-end' found by
3645 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
3646 ;; match. If searching for the property isn't needed then we
3647 ;; disable it by setting it to `cfd-limit' directly.
3648 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3649 ;; The end of the token preceding the decl spot last found by
3650 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
3651 ;; bob. `cfd-limit' if there's no match. In other words,
3652 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
3653 (cfd-match-pos cfd-limit)
3654 ;; The position to continue searching at.
3655 cfd-continue-pos
3656 ;; The position of the last "real" token we've stopped at.
3657 ;; This can be greater than `cfd-continue-pos' when we get
3658 ;; hits inside macros or at `c-decl-end' positions inside
3659 ;; comments.
3660 (cfd-token-pos 0)
3661 ;; The end position of the last entered macro.
3662 (cfd-macro-end 0))
3663
3664 ;; Initialize by finding a syntactically relevant start position
3665 ;; before the point, and do the first `c-decl-prefix-or-start-re'
3666 ;; search unless we're at bob.
3667
3668 (let (start-in-literal start-in-macro syntactic-pos)
3669 ;; Must back up a bit since we look for the end of the previous
3670 ;; statement or declaration, which is earlier than the first
3671 ;; returned match.
3672
3673 (cond
3674 ;; First we need to move to a syntactically relevant position.
3675 ;; Begin by backing out of comment or string literals.
3676 ((and
3677 (when (c-got-face-at (point) c-literal-faces)
3678 ;; Try to use the faces to back up to the start of the
3679 ;; literal. FIXME: What if the point is on a declaration
3680 ;; inside a comment?
3681 (while (and (not (bobp))
3682 (c-got-face-at (1- (point)) c-literal-faces))
3683 (goto-char (previous-single-property-change
3684 (point) 'face nil (point-min))))
3685
3686 ;; XEmacs doesn't fontify the quotes surrounding string
3687 ;; literals.
3688 (and (featurep 'xemacs)
3689 (eq (get-text-property (point) 'face)
3690 'font-lock-string-face)
3691 (not (bobp))
3692 (progn (backward-char)
3693 (not (looking-at c-string-limit-regexp)))
3694 (forward-char))
3695
3696 ;; Don't trust the literal to contain only literal faces
3697 ;; (the font lock package might not have fontified the
3698 ;; start of it at all, for instance) so check that we have
3699 ;; arrived at something that looks like a start or else
3700 ;; resort to `c-literal-limits'.
3701 (unless (looking-at c-literal-start-regexp)
3702 (let ((range (c-literal-limits)))
3703 (if range (goto-char (car range)))))
3704
3705 (setq start-in-literal (point)))
3706
3707 ;; The start is in a literal. If the limit is in the same
3708 ;; one we don't have to find a syntactic position etc. We
3709 ;; only check that if the limit is at or before bonl to save
3710 ;; time; it covers the by far most common case when font-lock
3711 ;; refontifies the current line only.
3712 (<= cfd-limit (c-point 'bonl cfd-start-pos))
3713 (save-excursion
3714 (goto-char cfd-start-pos)
3715 (while (progn
3716 (goto-char (next-single-property-change
3717 (point) 'face nil cfd-limit))
3718 (and (< (point) cfd-limit)
3719 (c-got-face-at (point) c-literal-faces))))
3720 (= (point) cfd-limit)))
3721
3722 ;; Completely inside a literal. Set up variables to trig the
3723 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
3724 ;; find a suitable start position.
3725 (setq cfd-continue-pos start-in-literal))
3726
3727 ;; Check if the region might be completely inside a macro, to
3728 ;; optimize that like the completely-inside-literal above.
3729 ((save-excursion
3730 (and (= (forward-line 1) 0)
3731 (bolp) ; forward-line has funny behavior at eob.
3732 (>= (point) cfd-limit)
3733 (progn (backward-char)
3734 (eq (char-before) ?\\))))
3735 ;; (Maybe) completely inside a macro. Only need to trig the
3736 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
3737 ;; set things up.
3738 (setq cfd-continue-pos (1- cfd-start-pos)
3739 start-in-macro t))
3740
3741 (t
3742 ;; Back out of any macro so we don't miss any declaration
3743 ;; that could follow after it.
3744 (when (c-beginning-of-macro)
3745 (setq start-in-macro t))
3746
3747 ;; Now we're at a proper syntactically relevant position so we
3748 ;; can use the cache. But first clear it if it applied
3749 ;; further down.
3750 (c-invalidate-find-decl-cache cfd-start-pos)
3751
3752 (setq syntactic-pos (point))
3753 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
3754 ;; Don't have to do this if the cache is relevant here,
3755 ;; typically if the same line is refontified again. If
3756 ;; we're just some syntactic whitespace further down we can
3757 ;; still use the cache to limit the skipping.
3758 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
3759
3760 ;; If we hit `c-find-decl-syntactic-pos' and
3761 ;; `c-find-decl-match-pos' is set then we install the cached
3762 ;; values. If we hit `c-find-decl-syntactic-pos' and
3763 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3764 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3765 ;; and so we can continue the search from this point. If we
3766 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
3767 ;; the right spot to begin searching anyway.
3768 (if (and (eq (point) c-find-decl-syntactic-pos)
3769 c-find-decl-match-pos)
3770 (setq cfd-match-pos c-find-decl-match-pos
3771 cfd-continue-pos syntactic-pos)
3772
3773 (setq c-find-decl-syntactic-pos syntactic-pos)
3774
3775 (when (if (bobp)
3776 ;; Always consider bob a match to get the first
3777 ;; declaration in the file. Do this separately instead of
3778 ;; letting `c-decl-prefix-or-start-re' match bob, so that
3779 ;; regexp always can consume at least one character to
3780 ;; ensure that we won't get stuck in an infinite loop.
3781 (setq cfd-re-match 0)
3782 (backward-char)
3783 (c-beginning-of-current-token)
3784 (< (point) cfd-limit))
3785 ;; Do an initial search now. In the bob case above it's
3786 ;; only done to search for a `c-decl-end' spot.
3787 (c-find-decl-prefix-search))
3788
3789 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
3790 cfd-match-pos)))))
3791
3792 ;; Advance `cfd-continue-pos' if it's before the start position.
3793 ;; The closest continue position that might have effect at or
3794 ;; after the start depends on what we started in. This also
3795 ;; finds a suitable start position in the special cases when the
3796 ;; region is completely within a literal or macro.
3797 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
3798
3799 (cond
3800 (start-in-macro
3801 ;; If we're in a macro then it's the closest preceding token
3802 ;; in the macro. Check this before `start-in-literal',
3803 ;; since if we're inside a literal in a macro, the preceding
3804 ;; token is earlier than any `c-decl-end' spot inside the
3805 ;; literal (comment).
3806 (goto-char (or start-in-literal cfd-start-pos))
3807 ;; The only syntactic ws in macros are comments.
3808 (c-backward-comments)
3809 (backward-char)
3810 (c-beginning-of-current-token))
3811
3812 (start-in-literal
3813 ;; If we're in a comment it can only be the closest
3814 ;; preceding `c-decl-end' position within that comment, if
3815 ;; any. Go back to the beginning of such a property so that
3816 ;; `c-find-decl-prefix-search' will find the end of it.
3817 ;; (Can't stop at the end and install it directly on
3818 ;; `cfd-prop-match' since that variable might be cleared
3819 ;; after `cfd-fun' below.)
3820 ;;
3821 ;; Note that if the literal is a string then the property
3822 ;; search will simply skip to the beginning of it right
3823 ;; away.
3824 (if (not c-type-decl-end-used)
3825 (goto-char start-in-literal)
3826 (goto-char cfd-start-pos)
3827 (while (progn
3828 (goto-char (previous-single-property-change
3829 (point) 'c-type nil start-in-literal))
3830 (and (> (point) start-in-literal)
3831 (not (eq (c-get-char-property (point) 'c-type)
3832 'c-decl-end))))))
3833
3834 (when (= (point) start-in-literal)
3835 ;; Didn't find any property inside the comment, so we can
3836 ;; skip it entirely. (This won't skip past a string, but
3837 ;; that'll be handled quickly by the next
3838 ;; `c-find-decl-prefix-search' anyway.)
3839 (c-forward-single-comment)
3840 (if (> (point) cfd-limit)
3841 (goto-char cfd-limit))))
3842
3843 (t
3844 ;; If we started in normal code, the only match that might
3845 ;; apply before the start is what we already got in
3846 ;; `cfd-match-pos' so we can continue at the start position.
3847 ;; (Note that we don't get here if the first match is below
3848 ;; it.)
3849 (goto-char cfd-start-pos)))
3850
3851 ;; Delete found matches if they are before our new continue
3852 ;; position, so that `c-find-decl-prefix-search' won't back up
3853 ;; to them later on.
3854 (setq cfd-continue-pos (point))
3855 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
3856 (setq cfd-re-match nil))
3857 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
3858 (setq cfd-prop-match nil)))
3859
3860 (if syntactic-pos
3861 ;; This is the normal case and we got a proper syntactic
3862 ;; position. If there's a match then it's always outside
3863 ;; macros and comments, so advance to the next token and set
3864 ;; `cfd-token-pos'. The loop below will later go back using
3865 ;; `cfd-continue-pos' to fix declarations inside the
3866 ;; syntactic ws.
3867 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
3868 (goto-char syntactic-pos)
3869 (c-forward-syntactic-ws)
3870 (and cfd-continue-pos
3871 (< cfd-continue-pos (point))
3872 (setq cfd-token-pos (point))))
3873
3874 ;; Have one of the special cases when the region is completely
3875 ;; within a literal or macro. `cfd-continue-pos' is set to a
3876 ;; good start position for the search, so do it.
3877 (c-find-decl-prefix-search)))
3878
3879 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
3880
3881 (while (progn
3882 (while (and
3883 (< cfd-match-pos cfd-limit)
3884
3885 (or
3886 ;; Kludge to filter out matches on the "<" that
3887 ;; aren't open parens, for the sake of languages
3888 ;; that got `c-recognize-<>-arglists' set.
3889 (and (eq (char-before cfd-match-pos) ?<)
3890 (not (c-get-char-property (1- cfd-match-pos)
3891 'syntax-table)))
3892
3893 ;; If `cfd-continue-pos' is less or equal to
3894 ;; `cfd-token-pos', we've got a hit inside a macro
3895 ;; that's in the syntactic whitespace before the last
3896 ;; "real" declaration we've checked. If they're equal
3897 ;; we've arrived at the declaration a second time, so
3898 ;; there's nothing to do.
3899 (= cfd-continue-pos cfd-token-pos)
3900
3901 (progn
3902 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3903 ;; we're still searching for declarations embedded in
3904 ;; the syntactic whitespace. In that case we need
3905 ;; only to skip comments and not macros, since they
3906 ;; can't be nested, and that's already been done in
3907 ;; `c-find-decl-prefix-search'.
3908 (when (> cfd-continue-pos cfd-token-pos)
3909 (c-forward-syntactic-ws)
3910 (setq cfd-token-pos (point)))
3911
3912 ;; Continue if the following token fails the
3913 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3914 (when (or (>= (point) cfd-limit)
3915 (not (looking-at cfd-decl-re))
3916 (and cfd-face-checklist
3917 (not (c-got-face-at
3918 (point) cfd-face-checklist))))
3919 (goto-char cfd-continue-pos)
3920 t)))
3921
3922 (< (point) cfd-limit))
3923 (c-find-decl-prefix-search))
3924
3925 (< (point) cfd-limit))
3926
3927 (when (and
3928 (>= (point) cfd-start-pos)
3929
3930 (progn
3931 ;; Narrow to the end of the macro if we got a hit inside
3932 ;; one, to avoid recognizing things that start inside the
3933 ;; macro and end outside it.
3934 (when (> cfd-match-pos cfd-macro-end)
3935 ;; Not in the same macro as in the previous round.
3936 (save-excursion
3937 (goto-char cfd-match-pos)
3938 (setq cfd-macro-end
3939 (if (save-excursion (and (c-beginning-of-macro)
3940 (< (point) cfd-match-pos)))
3941 (progn (c-end-of-macro)
3942 (point))
3943 0))))
3944
3945 (if (zerop cfd-macro-end)
3946 t
3947 (if (> cfd-macro-end (point))
3948 (progn (narrow-to-region (point-min) cfd-macro-end)
3949 t)
3950 ;; The matched token was the last thing in the macro,
3951 ;; so the whole match is bogus.
3952 (setq cfd-macro-end 0)
3953 nil))))
3954
3955 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3956 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3957 (setq cfd-prop-match nil))
3958
3959 (when (/= cfd-macro-end 0)
3960 ;; Restore limits if we did macro narrowment above.
3961 (narrow-to-region (point-min) cfd-buffer-end)))
3962
3963 (goto-char cfd-continue-pos)
3964 (if (= cfd-continue-pos cfd-limit)
3965 (setq cfd-match-pos cfd-limit)
3966 (c-find-decl-prefix-search)))))
3967
3968 \f
3969 ;; A cache for found types.
3970
3971 ;; Buffer local variable that contains an obarray with the types we've
3972 ;; found. If a declaration is recognized somewhere we record the
3973 ;; fully qualified identifier in it to recognize it as a type
3974 ;; elsewhere in the file too. This is not accurate since we do not
3975 ;; bother with the scoping rules of the languages, but in practice the
3976 ;; same name is seldom used as both a type and something else in a
3977 ;; file, and we only use this as a last resort in ambiguous cases (see
3978 ;; `c-forward-decl-or-cast-1').
3979 ;;
3980 ;; Not every type need be in this cache. However, things which have
3981 ;; ceased to be types must be removed from it.
3982 ;;
3983 ;; Template types in C++ are added here too but with the template
3984 ;; arglist replaced with "<>" in references or "<" for the one in the
3985 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
3986 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
3987 ;; template specs can be fairly sized programs in themselves) and
3988 ;; improves the hit ratio (it's a type regardless of the template
3989 ;; args; it's just not the same type, but we're only interested in
3990 ;; recognizing types, not telling distinct types apart). Note that
3991 ;; template types in references are added here too; from the example
3992 ;; above there will also be an entry "Foo<".
3993 (defvar c-found-types nil)
3994 (make-variable-buffer-local 'c-found-types)
3995
3996 (defsubst c-clear-found-types ()
3997 ;; Clears `c-found-types'.
3998 (setq c-found-types (make-vector 53 0)))
3999
4000 (defun c-add-type (from to)
4001 ;; Add the given region as a type in `c-found-types'. If the region
4002 ;; doesn't match an existing type but there is a type which is equal
4003 ;; to the given one except that the last character is missing, then
4004 ;; the shorter type is removed. That's done to avoid adding all
4005 ;; prefixes of a type as it's being entered and font locked. This
4006 ;; doesn't cover cases like when characters are removed from a type
4007 ;; or added in the middle. We'd need the position of point when the
4008 ;; font locking is invoked to solve this well.
4009 ;;
4010 ;; This function might do hidden buffer changes.
4011 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4012 (unless (intern-soft type c-found-types)
4013 (unintern (substring type 0 -1) c-found-types)
4014 (intern type c-found-types))))
4015
4016 (defun c-unfind-type (name)
4017 ;; Remove the "NAME" from c-found-types, if present.
4018 (unintern name c-found-types))
4019
4020 (defsubst c-check-type (from to)
4021 ;; Return non-nil if the given region contains a type in
4022 ;; `c-found-types'.
4023 ;;
4024 ;; This function might do hidden buffer changes.
4025 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4026 c-found-types))
4027
4028 (defun c-list-found-types ()
4029 ;; Return all the types in `c-found-types' as a sorted list of
4030 ;; strings.
4031 (let (type-list)
4032 (mapatoms (lambda (type)
4033 (setq type-list (cons (symbol-name type)
4034 type-list)))
4035 c-found-types)
4036 (sort type-list 'string-lessp)))
4037
4038 (defun c-trim-found-types (beg end old-len)
4039 ;; An after change function which, in conjunction with the info in
4040 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4041 ;; from `c-found-types', should this type have become stale. For
4042 ;; example, this happens to "foo" when "foo \n bar();" becomes
4043 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4044 ;; the fontification.
4045 ;;
4046 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4047 ;; type?
4048 (when (> end beg)
4049 (save-excursion
4050 (when (< end (point-max))
4051 (goto-char end)
4052 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4053 (progn (goto-char end)
4054 (c-end-of-current-token)))
4055 (c-unfind-type (buffer-substring-no-properties
4056 end (point)))))
4057 (when (> beg (point-min))
4058 (goto-char beg)
4059 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4060 (progn (goto-char beg)
4061 (c-beginning-of-current-token)))
4062 (c-unfind-type (buffer-substring-no-properties
4063 (point) beg))))))
4064
4065 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4066 (cond
4067 ;; Changing the amount of (already existing) whitespace - don't do anything.
4068 ((and (c-partial-ws-p beg end)
4069 (or (= beg end) ; removal of WS
4070 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4071
4072 ;; The syntactic relationship which defined a "found type" has been
4073 ;; destroyed.
4074 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4075 (c-unfind-type (cadr c-maybe-stale-found-type)))
4076 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4077 )))
4078
4079 \f
4080 ;; Handling of small scale constructs like types and names.
4081
4082 (defun c-after-change-check-<>-operators (beg end)
4083 ;; This is called from `after-change-functions' when
4084 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
4085 ;; chars with paren syntax become part of another operator like "<<"
4086 ;; or ">=".
4087 ;;
4088 ;; This function might do hidden buffer changes.
4089
4090 (save-excursion
4091 (goto-char beg)
4092 (when (or (looking-at "[<>]")
4093 (< (skip-chars-backward "<>") 0))
4094
4095 (goto-char beg)
4096 (c-beginning-of-current-token)
4097 (when (and (< (point) beg)
4098 (looking-at c-<>-multichar-token-regexp)
4099 (< beg (setq beg (match-end 0))))
4100 (while (progn (skip-chars-forward "^<>" beg)
4101 (< (point) beg))
4102 (c-clear-char-property (point) 'syntax-table)
4103 (forward-char))))
4104
4105 (when (< beg end)
4106 (goto-char end)
4107 (when (or (looking-at "[<>]")
4108 (< (skip-chars-backward "<>") 0))
4109
4110 (goto-char end)
4111 (c-beginning-of-current-token)
4112 (when (and (< (point) end)
4113 (looking-at c-<>-multichar-token-regexp)
4114 (< end (setq end (match-end 0))))
4115 (while (progn (skip-chars-forward "^<>" end)
4116 (< (point) end))
4117 (c-clear-char-property (point) 'syntax-table)
4118 (forward-char)))))))
4119
4120 ;; Dynamically bound variable that instructs `c-forward-type' to also
4121 ;; treat possible types (i.e. those that it normally returns 'maybe or
4122 ;; 'found for) as actual types (and always return 'found for them).
4123 ;; This means that it records them in `c-record-type-identifiers' if
4124 ;; that is set, and that it adds them to `c-found-types'.
4125 (defvar c-promote-possible-types nil)
4126
4127 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4128 ;; mark up successfully parsed arglists with paren syntax properties on
4129 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
4130 ;; `c-type' property of each argument separating comma.
4131 ;;
4132 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
4133 ;; all arglists for side effects (i.e. recording types), otherwise it
4134 ;; exploits any existing paren syntax properties to quickly jump to the
4135 ;; end of already parsed arglists.
4136 ;;
4137 ;; Marking up the arglists is not the default since doing that correctly
4138 ;; depends on a proper value for `c-restricted-<>-arglists'.
4139 (defvar c-parse-and-markup-<>-arglists nil)
4140
4141 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4142 ;; not accept arglists that contain binary operators.
4143 ;;
4144 ;; This is primarily used to handle C++ template arglists. C++
4145 ;; disambiguates them by checking whether the preceding name is a
4146 ;; template or not. We can't do that, so we assume it is a template
4147 ;; if it can be parsed as one. That usually works well since
4148 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
4149 ;; in almost all cases would be pointless.
4150 ;;
4151 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
4152 ;; should let the comma separate the function arguments instead. And
4153 ;; in a context where the value of the expression is taken, e.g. in
4154 ;; "if (a < b || c > d)", it's probably not a template.
4155 (defvar c-restricted-<>-arglists nil)
4156
4157 ;; Dynamically bound variables that instructs
4158 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
4159 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
4160 ;; `c-forward-label' to record the ranges of all the type and
4161 ;; reference identifiers they encounter. They will build lists on
4162 ;; these variables where each element is a cons of the buffer
4163 ;; positions surrounding each identifier. This recording is only
4164 ;; activated when `c-record-type-identifiers' is non-nil.
4165 ;;
4166 ;; All known types that can't be identifiers are recorded, and also
4167 ;; other possible types if `c-promote-possible-types' is set.
4168 ;; Recording is however disabled inside angle bracket arglists that
4169 ;; are encountered inside names and other angle bracket arglists.
4170 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
4171 ;; instead.
4172 ;;
4173 ;; Only the names in C++ template style references (e.g. "tmpl" in
4174 ;; "tmpl<a,b>::foo") are recorded as references, other references
4175 ;; aren't handled here.
4176 ;;
4177 ;; `c-forward-label' records the label identifier(s) on
4178 ;; `c-record-ref-identifiers'.
4179 (defvar c-record-type-identifiers nil)
4180 (defvar c-record-ref-identifiers nil)
4181
4182 ;; This variable will receive a cons cell of the range of the last
4183 ;; single identifier symbol stepped over by `c-forward-name' if it's
4184 ;; successful. This is the range that should be put on one of the
4185 ;; record lists above by the caller. It's assigned nil if there's no
4186 ;; such symbol in the name.
4187 (defvar c-last-identifier-range nil)
4188
4189 (defmacro c-record-type-id (range)
4190 (if (eq (car-safe range) 'cons)
4191 ;; Always true.
4192 `(setq c-record-type-identifiers
4193 (cons ,range c-record-type-identifiers))
4194 `(let ((range ,range))
4195 (if range
4196 (setq c-record-type-identifiers
4197 (cons range c-record-type-identifiers))))))
4198
4199 (defmacro c-record-ref-id (range)
4200 (if (eq (car-safe range) 'cons)
4201 ;; Always true.
4202 `(setq c-record-ref-identifiers
4203 (cons ,range c-record-ref-identifiers))
4204 `(let ((range ,range))
4205 (if range
4206 (setq c-record-ref-identifiers
4207 (cons range c-record-ref-identifiers))))))
4208
4209 ;; Dynamically bound variable that instructs `c-forward-type' to
4210 ;; record the ranges of types that only are found. Behaves otherwise
4211 ;; like `c-record-type-identifiers'.
4212 (defvar c-record-found-types nil)
4213
4214 (defmacro c-forward-keyword-prefixed-id (type)
4215 ;; Used internally in `c-forward-keyword-clause' to move forward
4216 ;; over a type (if TYPE is 'type) or a name (otherwise) which
4217 ;; possibly is prefixed by keywords and their associated clauses.
4218 ;; Try with a type/name first to not trip up on those that begin
4219 ;; with a keyword. Return t if a known or found type is moved
4220 ;; over. The point is clobbered if nil is returned. If range
4221 ;; recording is enabled, the identifier is recorded on as a type
4222 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
4223 ;;
4224 ;; This macro might do hidden buffer changes.
4225 `(let (res)
4226 (while (if (setq res ,(if (eq type 'type)
4227 `(c-forward-type)
4228 `(c-forward-name)))
4229 nil
4230 (and (looking-at c-keywords-regexp)
4231 (c-forward-keyword-clause 1))))
4232 (when (memq res '(t known found prefix))
4233 ,(when (eq type 'ref)
4234 `(when c-record-type-identifiers
4235 (c-record-ref-id c-last-identifier-range)))
4236 t)))
4237
4238 (defmacro c-forward-id-comma-list (type update-safe-pos)
4239 ;; Used internally in `c-forward-keyword-clause' to move forward
4240 ;; over a comma separated list of types or names using
4241 ;; `c-forward-keyword-prefixed-id'.
4242 ;;
4243 ;; This macro might do hidden buffer changes.
4244 `(while (and (progn
4245 ,(when update-safe-pos
4246 `(setq safe-pos (point)))
4247 (eq (char-after) ?,))
4248 (progn
4249 (forward-char)
4250 (c-forward-syntactic-ws)
4251 (c-forward-keyword-prefixed-id ,type)))))
4252
4253 (defun c-forward-keyword-clause (match)
4254 ;; Submatch MATCH in the current match data is assumed to surround a
4255 ;; token. If it's a keyword, move over it and any immediately
4256 ;; following clauses associated with it, stopping at the start of
4257 ;; the next token. t is returned in that case, otherwise the point
4258 ;; stays and nil is returned. The kind of clauses that are
4259 ;; recognized are those specified by `c-type-list-kwds',
4260 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
4261 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
4262 ;; and `c-<>-arglist-kwds'.
4263 ;;
4264 ;; This function records identifier ranges on
4265 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4266 ;; `c-record-type-identifiers' is non-nil.
4267 ;;
4268 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
4269 ;; apply directly after the keyword, the type list is moved over
4270 ;; only when there is no unaccounted token before it (i.e. a token
4271 ;; that isn't moved over due to some other keyword list). The
4272 ;; identifier ranges in the list are still recorded if that should
4273 ;; be done, though.
4274 ;;
4275 ;; This function might do hidden buffer changes.
4276
4277 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
4278 ;; The call to `c-forward-<>-arglist' below is made after
4279 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
4280 ;; are angle bracket arglists and `c-restricted-<>-arglists'
4281 ;; should therefore be nil.
4282 (c-parse-and-markup-<>-arglists t)
4283 c-restricted-<>-arglists)
4284
4285 (when kwd-sym
4286 (goto-char (match-end match))
4287 (c-forward-syntactic-ws)
4288 (setq safe-pos (point))
4289
4290 (cond
4291 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
4292 (c-forward-keyword-prefixed-id type))
4293 ;; There's a type directly after a keyword in `c-type-list-kwds'.
4294 (c-forward-id-comma-list type t))
4295
4296 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
4297 (c-forward-keyword-prefixed-id ref))
4298 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
4299 (c-forward-id-comma-list ref t))
4300
4301 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
4302 (eq (char-after) ?\())
4303 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
4304
4305 (forward-char)
4306 (when (and (setq pos (c-up-list-forward))
4307 (eq (char-before pos) ?\)))
4308 (when (and c-record-type-identifiers
4309 (c-keyword-member kwd-sym 'c-paren-type-kwds))
4310 ;; Use `c-forward-type' on every identifier we can find
4311 ;; inside the paren, to record the types.
4312 (while (c-syntactic-re-search-forward c-symbol-start pos t)
4313 (goto-char (match-beginning 0))
4314 (unless (c-forward-type)
4315 (looking-at c-symbol-key) ; Always matches.
4316 (goto-char (match-end 0)))))
4317
4318 (goto-char pos)
4319 (c-forward-syntactic-ws)
4320 (setq safe-pos (point))))
4321
4322 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
4323 (eq (char-after) ?<)
4324 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
4325 (c-forward-syntactic-ws)
4326 (setq safe-pos (point)))
4327
4328 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
4329 (not (looking-at c-symbol-start))
4330 (c-safe (c-forward-sexp) t))
4331 (c-forward-syntactic-ws)
4332 (setq safe-pos (point))))
4333
4334 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
4335 (if (eq (char-after) ?:)
4336 ;; If we are at the colon already, we move over the type
4337 ;; list after it.
4338 (progn
4339 (forward-char)
4340 (c-forward-syntactic-ws)
4341 (when (c-forward-keyword-prefixed-id type)
4342 (c-forward-id-comma-list type t)))
4343 ;; Not at the colon, so stop here. But the identifier
4344 ;; ranges in the type list later on should still be
4345 ;; recorded.
4346 (and c-record-type-identifiers
4347 (progn
4348 ;; If a keyword matched both one of the types above and
4349 ;; this one, we match `c-colon-type-list-re' after the
4350 ;; clause matched above.
4351 (goto-char safe-pos)
4352 (looking-at c-colon-type-list-re))
4353 (progn
4354 (goto-char (match-end 0))
4355 (c-forward-syntactic-ws)
4356 (c-forward-keyword-prefixed-id type))
4357 ;; There's a type after the `c-colon-type-list-re' match
4358 ;; after a keyword in `c-colon-type-list-kwds'.
4359 (c-forward-id-comma-list type nil))))
4360
4361 (goto-char safe-pos)
4362 t)))
4363
4364 (defun c-forward-<>-arglist (all-types)
4365 ;; The point is assumed to be at a "<". Try to treat it as the open
4366 ;; paren of an angle bracket arglist and move forward to the the
4367 ;; corresponding ">". If successful, the point is left after the
4368 ;; ">" and t is returned, otherwise the point isn't moved and nil is
4369 ;; returned. If ALL-TYPES is t then all encountered arguments in
4370 ;; the arglist that might be types are treated as found types.
4371 ;;
4372 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
4373 ;; function handles text properties on the angle brackets and argument
4374 ;; separating commas.
4375 ;;
4376 ;; `c-restricted-<>-arglists' controls how lenient the template
4377 ;; arglist recognition should be.
4378 ;;
4379 ;; This function records identifier ranges on
4380 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4381 ;; `c-record-type-identifiers' is non-nil.
4382 ;;
4383 ;; This function might do hidden buffer changes.
4384
4385 (let ((start (point))
4386 ;; If `c-record-type-identifiers' is set then activate
4387 ;; recording of any found types that constitute an argument in
4388 ;; the arglist.
4389 (c-record-found-types (if c-record-type-identifiers t)))
4390 (if (catch 'angle-bracket-arglist-escape
4391 (setq c-record-found-types
4392 (c-forward-<>-arglist-recur all-types)))
4393 (progn
4394 (when (consp c-record-found-types)
4395 (setq c-record-type-identifiers
4396 ;; `nconc' doesn't mind that the tail of
4397 ;; `c-record-found-types' is t.
4398 (nconc c-record-found-types c-record-type-identifiers)))
4399 t)
4400
4401 (goto-char start)
4402 nil)))
4403
4404 (defun c-forward-<>-arglist-recur (all-types)
4405 ;; Recursive part of `c-forward-<>-arglist'.
4406 ;;
4407 ;; This function might do hidden buffer changes.
4408
4409 (let ((start (point)) res pos tmp
4410 ;; Cover this so that any recorded found type ranges are
4411 ;; automatically lost if it turns out to not be an angle
4412 ;; bracket arglist. It's propagated through the return value
4413 ;; on successful completion.
4414 (c-record-found-types c-record-found-types)
4415 ;; List that collects the positions after the argument
4416 ;; separating ',' in the arglist.
4417 arg-start-pos)
4418
4419 ;; If the '<' has paren open syntax then we've marked it as an angle
4420 ;; bracket arglist before, so skip to the end.
4421 (if (and (not c-parse-and-markup-<>-arglists)
4422 (c-get-char-property (point) 'syntax-table))
4423
4424 (progn
4425 (forward-char)
4426 (if (and (c-go-up-list-forward)
4427 (eq (char-before) ?>))
4428 t
4429
4430 ;; Got unmatched paren angle brackets. We don't clear the paren
4431 ;; syntax properties and retry, on the basis that it's very
4432 ;; unlikely that paren angle brackets become operators by code
4433 ;; manipulation. It's far more likely that it doesn't match due
4434 ;; to narrowing or some temporary change.
4435 (goto-char start)
4436 nil))
4437
4438 (forward-char)
4439 (unless (looking-at c-<-op-cont-regexp)
4440 (while (and
4441 (progn
4442
4443 (when c-record-type-identifiers
4444 (if all-types
4445
4446 ;; All encountered identifiers are types, so set the
4447 ;; promote flag and parse the type.
4448 (progn
4449 (c-forward-syntactic-ws)
4450 (when (looking-at c-identifier-start)
4451 (let ((c-promote-possible-types t))
4452 (c-forward-type))))
4453
4454 ;; Check if this arglist argument is a sole type. If
4455 ;; it's known then it's recorded in
4456 ;; `c-record-type-identifiers'. If it only is found
4457 ;; then it's recorded in `c-record-found-types' which we
4458 ;; might roll back if it turns out that this isn't an
4459 ;; angle bracket arglist afterall.
4460 (when (memq (char-before) '(?, ?<))
4461 (let ((orig-record-found-types c-record-found-types))
4462 (c-forward-syntactic-ws)
4463 (and (memq (c-forward-type) '(known found))
4464 (not (looking-at "[,>]"))
4465 ;; A found type was recorded but it's not the
4466 ;; only thing in the arglist argument, so reset
4467 ;; `c-record-found-types'.
4468 (setq c-record-found-types
4469 orig-record-found-types))))))
4470
4471 (setq pos (point))
4472 (or (when (eq (char-after) ?>)
4473 ;; Must check for '>' at the very start separately,
4474 ;; since the regexp below has to avoid ">>" without
4475 ;; using \\=.
4476 (forward-char)
4477 t)
4478
4479 ;; Note: These regexps exploit the match order in \| so
4480 ;; that "<>" is matched by "<" rather than "[^>:-]>".
4481 (c-syntactic-re-search-forward
4482 (if c-restricted-<>-arglists
4483 ;; Stop on ',', '|', '&', '+' and '-' to catch
4484 ;; common binary operators that could be between
4485 ;; two comparison expressions "a<b" and "c>d".
4486 "[<;{},|&+-]\\|\\([^>:-]>\\)"
4487 ;; Otherwise we still stop on ',' to find the
4488 ;; argument start positions.
4489 "[<;{},]\\|\\([^>:-]>\\)")
4490 nil 'move t t 1)
4491
4492 ;; If the arglist starter has lost its open paren
4493 ;; syntax but not the closer, we won't find the
4494 ;; closer above since we only search in the
4495 ;; balanced sexp. In that case we stop just short
4496 ;; of it so check if the following char is the closer.
4497 (when (eq (char-after) ?>)
4498 (forward-char)
4499 t)))
4500
4501 (cond
4502 ((eq (char-before) ?>)
4503 ;; Either an operator starting with '>' or the end of
4504 ;; the angle bracket arglist.
4505
4506 (if (looking-at c->-op-cont-regexp)
4507 (progn
4508 (goto-char (match-end 0))
4509 t) ; Continue the loop.
4510
4511 ;; The angle bracket arglist is finished.
4512 (when c-parse-and-markup-<>-arglists
4513 (while arg-start-pos
4514 (c-put-c-type-property (1- (car arg-start-pos))
4515 'c-<>-arg-sep)
4516 (setq arg-start-pos (cdr arg-start-pos)))
4517 (c-mark-<-as-paren start)
4518 (c-mark->-as-paren (1- (point))))
4519 (setq res t)
4520 nil)) ; Exit the loop.
4521
4522 ((eq (char-before) ?<)
4523 ;; Either an operator starting with '<' or a nested arglist.
4524
4525 (setq pos (point))
4526 (let (id-start id-end subres keyword-match)
4527 (if (if (looking-at c-<-op-cont-regexp)
4528 (setq tmp (match-end 0))
4529 (setq tmp pos)
4530 (backward-char)
4531 (not
4532 (and
4533
4534 (save-excursion
4535 ;; There's always an identifier before an angle
4536 ;; bracket arglist, or a keyword in
4537 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
4538 (c-backward-syntactic-ws)
4539 (setq id-end (point))
4540 (c-simple-skip-symbol-backward)
4541 (when (or (setq keyword-match
4542 (looking-at c-opt-<>-sexp-key))
4543 (not (looking-at c-keywords-regexp)))
4544 (setq id-start (point))))
4545
4546 (setq subres
4547 (let ((c-record-type-identifiers nil)
4548 (c-record-found-types nil))
4549 (c-forward-<>-arglist-recur
4550 (and keyword-match
4551 (c-keyword-member
4552 (c-keyword-sym (match-string 1))
4553 'c-<>-type-kwds)))))
4554 )))
4555
4556 ;; It was not an angle bracket arglist.
4557 (goto-char tmp)
4558
4559 ;; It was an angle bracket arglist.
4560 (setq c-record-found-types subres)
4561
4562 ;; Record the identifier before the template as a type
4563 ;; or reference depending on whether the arglist is last
4564 ;; in a qualified identifier.
4565 (when (and c-record-type-identifiers
4566 (not keyword-match))
4567 (if (and c-opt-identifier-concat-key
4568 (progn
4569 (c-forward-syntactic-ws)
4570 (looking-at c-opt-identifier-concat-key)))
4571 (c-record-ref-id (cons id-start id-end))
4572 (c-record-type-id (cons id-start id-end))))))
4573 t)
4574
4575 ((and (eq (char-before) ?,)
4576 (not c-restricted-<>-arglists))
4577 ;; Just another argument. Record the position. The
4578 ;; type check stuff that made us stop at it is at
4579 ;; the top of the loop.
4580 (setq arg-start-pos (cons (point) arg-start-pos)))
4581
4582 (t
4583 ;; Got a character that can't be in an angle bracket
4584 ;; arglist argument. Abort using `throw', since
4585 ;; it's useless to try to find a surrounding arglist
4586 ;; if we're nested.
4587 (throw 'angle-bracket-arglist-escape nil))))))
4588
4589 (if res
4590 (or c-record-found-types t)))))
4591
4592 (defun c-backward-<>-arglist (all-types &optional limit)
4593 ;; The point is assumed to be directly after a ">". Try to treat it
4594 ;; as the close paren of an angle bracket arglist and move back to
4595 ;; the corresponding "<". If successful, the point is left at
4596 ;; the "<" and t is returned, otherwise the point isn't moved and
4597 ;; nil is returned. ALL-TYPES is passed on to
4598 ;; `c-forward-<>-arglist'.
4599 ;;
4600 ;; If the optional LIMIT is given, it bounds the backward search.
4601 ;; It's then assumed to be at a syntactically relevant position.
4602 ;;
4603 ;; This is a wrapper around `c-forward-<>-arglist'. See that
4604 ;; function for more details.
4605
4606 (let ((start (point)))
4607 (backward-char)
4608 (if (and (not c-parse-and-markup-<>-arglists)
4609 (c-get-char-property (point) 'syntax-table))
4610
4611 (if (and (c-go-up-list-backward)
4612 (eq (char-after) ?<))
4613 t
4614 ;; See corresponding note in `c-forward-<>-arglist'.
4615 (goto-char start)
4616 nil)
4617
4618 (while (progn
4619 (c-syntactic-skip-backward "^<;{}" limit t)
4620
4621 (and
4622 (if (eq (char-before) ?<)
4623 t
4624 ;; Stopped at bob or a char that isn't allowed in an
4625 ;; arglist, so we've failed.
4626 (goto-char start)
4627 nil)
4628
4629 (if (> (point)
4630 (progn (c-beginning-of-current-token)
4631 (point)))
4632 ;; If we moved then the "<" was part of some
4633 ;; multicharacter token.
4634 t
4635
4636 (backward-char)
4637 (let ((beg-pos (point)))
4638 (if (c-forward-<>-arglist all-types)
4639 (cond ((= (point) start)
4640 ;; Matched the arglist. Break the while.
4641 (goto-char beg-pos)
4642 nil)
4643 ((> (point) start)
4644 ;; We started from a non-paren ">" inside an
4645 ;; arglist.
4646 (goto-char start)
4647 nil)
4648 (t
4649 ;; Matched a shorter arglist. Can be a nested
4650 ;; one so continue looking.
4651 (goto-char beg-pos)
4652 t))
4653 t))))))
4654
4655 (/= (point) start))))
4656
4657 (defun c-forward-name ()
4658 ;; Move forward over a complete name if at the beginning of one,
4659 ;; stopping at the next following token. If the point is not at
4660 ;; something that are recognized as name then it stays put. A name
4661 ;; could be something as simple as "foo" in C or something as
4662 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
4663 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
4664 ;; int>::*volatile const" in C++ (this function is actually little
4665 ;; more than a `looking-at' call in all modes except those that,
4666 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
4667 ;; name is found, 'template if it's an identifier ending with an
4668 ;; angle bracket arglist, 'operator of it's an operator identifier,
4669 ;; or t if it's some other kind of name.
4670 ;;
4671 ;; This function records identifier ranges on
4672 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4673 ;; `c-record-type-identifiers' is non-nil.
4674 ;;
4675 ;; This function might do hidden buffer changes.
4676
4677 (let ((pos (point)) (start (point)) res id-start id-end
4678 ;; Turn off `c-promote-possible-types' here since we might
4679 ;; call `c-forward-<>-arglist' and we don't want it to promote
4680 ;; every suspect thing in the arglist to a type. We're
4681 ;; typically called from `c-forward-type' in this case, and
4682 ;; the caller only wants the top level type that it finds to
4683 ;; be promoted.
4684 c-promote-possible-types)
4685 (while
4686 (and
4687 (looking-at c-identifier-key)
4688
4689 (progn
4690 ;; Check for keyword. We go to the last symbol in
4691 ;; `c-identifier-key' first.
4692 (goto-char (setq id-end (match-end 0)))
4693 (c-simple-skip-symbol-backward)
4694 (setq id-start (point))
4695
4696 (if (looking-at c-keywords-regexp)
4697 (when (and (c-major-mode-is 'c++-mode)
4698 (looking-at
4699 (cc-eval-when-compile
4700 (concat "\\(operator\\|\\(template\\)\\)"
4701 "\\(" (c-lang-const c-nonsymbol-key c++)
4702 "\\|$\\)")))
4703 (if (match-beginning 2)
4704 ;; "template" is only valid inside an
4705 ;; identifier if preceded by "::".
4706 (save-excursion
4707 (c-backward-syntactic-ws)
4708 (and (c-safe (backward-char 2) t)
4709 (looking-at "::")))
4710 t))
4711
4712 ;; Handle a C++ operator or template identifier.
4713 (goto-char id-end)
4714 (c-forward-syntactic-ws)
4715 (cond ((eq (char-before id-end) ?e)
4716 ;; Got "... ::template".
4717 (let ((subres (c-forward-name)))
4718 (when subres
4719 (setq pos (point)
4720 res subres))))
4721
4722 ((looking-at c-identifier-start)
4723 ;; Got a cast operator.
4724 (when (c-forward-type)
4725 (setq pos (point)
4726 res 'operator)
4727 ;; Now we should match a sequence of either
4728 ;; '*', '&' or a name followed by ":: *",
4729 ;; where each can be followed by a sequence
4730 ;; of `c-opt-type-modifier-key'.
4731 (while (cond ((looking-at "[*&]")
4732 (goto-char (match-end 0))
4733 t)
4734 ((looking-at c-identifier-start)
4735 (and (c-forward-name)
4736 (looking-at "::")
4737 (progn
4738 (goto-char (match-end 0))
4739 (c-forward-syntactic-ws)
4740 (eq (char-after) ?*))
4741 (progn
4742 (forward-char)
4743 t))))
4744 (while (progn
4745 (c-forward-syntactic-ws)
4746 (setq pos (point))
4747 (looking-at c-opt-type-modifier-key))
4748 (goto-char (match-end 1))))))
4749
4750 ((looking-at c-overloadable-operators-regexp)
4751 ;; Got some other operator.
4752 (setq c-last-identifier-range
4753 (cons (point) (match-end 0)))
4754 (goto-char (match-end 0))
4755 (c-forward-syntactic-ws)
4756 (setq pos (point)
4757 res 'operator)))
4758
4759 nil)
4760
4761 ;; `id-start' is equal to `id-end' if we've jumped over
4762 ;; an identifier that doesn't end with a symbol token.
4763 ;; That can occur e.g. for Java import directives on the
4764 ;; form "foo.bar.*".
4765 (when (and id-start (/= id-start id-end))
4766 (setq c-last-identifier-range
4767 (cons id-start id-end)))
4768 (goto-char id-end)
4769 (c-forward-syntactic-ws)
4770 (setq pos (point)
4771 res t)))
4772
4773 (progn
4774 (goto-char pos)
4775 (when (or c-opt-identifier-concat-key
4776 c-recognize-<>-arglists)
4777
4778 (cond
4779 ((and c-opt-identifier-concat-key
4780 (looking-at c-opt-identifier-concat-key))
4781 ;; Got a concatenated identifier. This handles the
4782 ;; cases with tricky syntactic whitespace that aren't
4783 ;; covered in `c-identifier-key'.
4784 (goto-char (match-end 0))
4785 (c-forward-syntactic-ws)
4786 t)
4787
4788 ((and c-recognize-<>-arglists
4789 (eq (char-after) ?<))
4790 ;; Maybe an angle bracket arglist.
4791
4792 (when (let (c-record-type-identifiers
4793 c-record-found-types)
4794 (c-forward-<>-arglist nil))
4795
4796 (c-add-type start (1+ pos))
4797 (c-forward-syntactic-ws)
4798 (setq pos (point)
4799 c-last-identifier-range nil)
4800
4801 (if (and c-opt-identifier-concat-key
4802 (looking-at c-opt-identifier-concat-key))
4803
4804 ;; Continue if there's an identifier concatenation
4805 ;; operator after the template argument.
4806 (progn
4807 (when (and c-record-type-identifiers id-start)
4808 (c-record-ref-id (cons id-start id-end)))
4809 (forward-char 2)
4810 (c-forward-syntactic-ws)
4811 t)
4812
4813 (when (and c-record-type-identifiers id-start)
4814 (c-record-type-id (cons id-start id-end)))
4815 (setq res 'template)
4816 nil)))
4817 )))))
4818
4819 (goto-char pos)
4820 res))
4821
4822 (defun c-forward-type ()
4823 ;; Move forward over a type spec if at the beginning of one,
4824 ;; stopping at the next following token. Return t if it's a known
4825 ;; type that can't be a name or other expression, 'known if it's an
4826 ;; otherwise known type (according to `*-font-lock-extra-types'),
4827 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4828 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4829 ;; that might be a type, or nil if it can't be a type (the point
4830 ;; isn't moved then). The point is assumed to be at the beginning
4831 ;; of a token.
4832 ;;
4833 ;; Note that this function doesn't skip past the brace definition
4834 ;; that might be considered part of the type, e.g.
4835 ;; "enum {a, b, c} foo".
4836 ;;
4837 ;; This function records identifier ranges on
4838 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4839 ;; `c-record-type-identifiers' is non-nil.
4840 ;;
4841 ;; This function might do hidden buffer changes.
4842
4843 (let ((start (point)) pos res name-res id-start id-end id-range)
4844
4845 ;; Skip leading type modifiers. If any are found we know it's a
4846 ;; prefix of a type.
4847 (when c-opt-type-modifier-key
4848 (while (looking-at c-opt-type-modifier-key)
4849 (goto-char (match-end 1))
4850 (c-forward-syntactic-ws)
4851 (setq res 'prefix)))
4852
4853 (cond
4854 ((looking-at c-type-prefix-key)
4855 ;; Looking at a keyword that prefixes a type identifier,
4856 ;; e.g. "class".
4857 (goto-char (match-end 1))
4858 (c-forward-syntactic-ws)
4859 (setq pos (point))
4860 (if (memq (setq name-res (c-forward-name)) '(t template))
4861 (progn
4862 (when (eq name-res t)
4863 ;; In many languages the name can be used without the
4864 ;; prefix, so we add it to `c-found-types'.
4865 (c-add-type pos (point))
4866 (when (and c-record-type-identifiers
4867 c-last-identifier-range)
4868 (c-record-type-id c-last-identifier-range)))
4869 (setq res t))
4870 ;; Invalid syntax.
4871 (goto-char start)
4872 (setq res nil)))
4873
4874 ((progn
4875 (setq pos nil)
4876 (if (looking-at c-identifier-start)
4877 (save-excursion
4878 (setq id-start (point)
4879 name-res (c-forward-name))
4880 (when name-res
4881 (setq id-end (point)
4882 id-range c-last-identifier-range))))
4883 (and (cond ((looking-at c-primitive-type-key)
4884 (setq res t))
4885 ((c-with-syntax-table c-identifier-syntax-table
4886 (looking-at c-known-type-key))
4887 (setq res 'known)))
4888 (or (not id-end)
4889 (>= (save-excursion
4890 (save-match-data
4891 (goto-char (match-end 1))
4892 (c-forward-syntactic-ws)
4893 (setq pos (point))))
4894 id-end)
4895 (setq res nil))))
4896 ;; Looking at a primitive or known type identifier. We've
4897 ;; checked for a name first so that we don't go here if the
4898 ;; known type match only is a prefix of another name.
4899
4900 (setq id-end (match-end 1))
4901
4902 (when (and c-record-type-identifiers
4903 (or c-promote-possible-types (eq res t)))
4904 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4905
4906 (if (and c-opt-type-component-key
4907 (save-match-data
4908 (looking-at c-opt-type-component-key)))
4909 ;; There might be more keywords for the type.
4910 (let (safe-pos)
4911 (c-forward-keyword-clause 1)
4912 (while (progn
4913 (setq safe-pos (point))
4914 (looking-at c-opt-type-component-key))
4915 (when (and c-record-type-identifiers
4916 (looking-at c-primitive-type-key))
4917 (c-record-type-id (cons (match-beginning 1)
4918 (match-end 1))))
4919 (c-forward-keyword-clause 1))
4920 (if (looking-at c-primitive-type-key)
4921 (progn
4922 (when c-record-type-identifiers
4923 (c-record-type-id (cons (match-beginning 1)
4924 (match-end 1))))
4925 (c-forward-keyword-clause 1)
4926 (setq res t))
4927 (goto-char safe-pos)
4928 (setq res 'prefix)))
4929 (unless (save-match-data (c-forward-keyword-clause 1))
4930 (if pos
4931 (goto-char pos)
4932 (goto-char (match-end 1))
4933 (c-forward-syntactic-ws)))))
4934
4935 (name-res
4936 (cond ((eq name-res t)
4937 ;; A normal identifier.
4938 (goto-char id-end)
4939 (if (or res c-promote-possible-types)
4940 (progn
4941 (c-add-type id-start id-end)
4942 (when (and c-record-type-identifiers id-range)
4943 (c-record-type-id id-range))
4944 (unless res
4945 (setq res 'found)))
4946 (setq res (if (c-check-type id-start id-end)
4947 ;; It's an identifier that has been used as
4948 ;; a type somewhere else.
4949 'found
4950 ;; It's an identifier that might be a type.
4951 'maybe))))
4952 ((eq name-res 'template)
4953 ;; A template is a type.
4954 (goto-char id-end)
4955 (setq res t))
4956 (t
4957 ;; Otherwise it's an operator identifier, which is not a type.
4958 (goto-char start)
4959 (setq res nil)))))
4960
4961 (when res
4962 ;; Skip trailing type modifiers. If any are found we know it's
4963 ;; a type.
4964 (when c-opt-type-modifier-key
4965 (while (looking-at c-opt-type-modifier-key)
4966 (goto-char (match-end 1))
4967 (c-forward-syntactic-ws)
4968 (setq res t)))
4969
4970 ;; Step over any type suffix operator. Do not let the existence
4971 ;; of these alter the classification of the found type, since
4972 ;; these operators typically are allowed in normal expressions
4973 ;; too.
4974 (when c-opt-type-suffix-key
4975 (while (looking-at c-opt-type-suffix-key)
4976 (goto-char (match-end 1))
4977 (c-forward-syntactic-ws)))
4978
4979 (when c-opt-type-concat-key
4980 ;; Look for a trailing operator that concatenates the type
4981 ;; with a following one, and if so step past that one through
4982 ;; a recursive call. Note that we don't record concatenated
4983 ;; types in `c-found-types' - it's the component types that
4984 ;; are recorded when appropriate.
4985 (setq pos (point))
4986 (let* ((c-promote-possible-types (or (memq res '(t known))
4987 c-promote-possible-types))
4988 ;; If we can't promote then set `c-record-found-types' so that
4989 ;; we can merge in the types from the second part afterwards if
4990 ;; it turns out to be a known type there.
4991 (c-record-found-types (and c-record-type-identifiers
4992 (not c-promote-possible-types)))
4993 subres)
4994 (if (and (looking-at c-opt-type-concat-key)
4995
4996 (progn
4997 (goto-char (match-end 1))
4998 (c-forward-syntactic-ws)
4999 (setq subres (c-forward-type))))
5000
5001 (progn
5002 ;; If either operand certainly is a type then both are, but we
5003 ;; don't let the existence of the operator itself promote two
5004 ;; uncertain types to a certain one.
5005 (cond ((eq res t))
5006 ((eq subres t)
5007 (unless (eq name-res 'template)
5008 (c-add-type id-start id-end))
5009 (when (and c-record-type-identifiers id-range)
5010 (c-record-type-id id-range))
5011 (setq res t))
5012 ((eq res 'known))
5013 ((eq subres 'known)
5014 (setq res 'known))
5015 ((eq res 'found))
5016 ((eq subres 'found)
5017 (setq res 'found))
5018 (t
5019 (setq res 'maybe)))
5020
5021 (when (and (eq res t)
5022 (consp c-record-found-types))
5023 ;; Merge in the ranges of any types found by the second
5024 ;; `c-forward-type'.
5025 (setq c-record-type-identifiers
5026 ;; `nconc' doesn't mind that the tail of
5027 ;; `c-record-found-types' is t.
5028 (nconc c-record-found-types
5029 c-record-type-identifiers))))
5030
5031 (goto-char pos))))
5032
5033 (when (and c-record-found-types (memq res '(known found)) id-range)
5034 (setq c-record-found-types
5035 (cons id-range c-record-found-types))))
5036
5037 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
5038
5039 res))
5040
5041 \f
5042 ;; Handling of large scale constructs like statements and declarations.
5043
5044 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
5045 ;; defsubst or perhaps even a defun, but it contains lots of free
5046 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
5047 (defmacro c-fdoc-shift-type-backward (&optional short)
5048 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
5049 ;; of types when parsing a declaration, which means that it
5050 ;; sometimes consumes the identifier in the declaration as a type.
5051 ;; This is used to "backtrack" and make the last type be treated as
5052 ;; an identifier instead.
5053 `(progn
5054 ,(unless short
5055 ;; These identifiers are bound only in the inner let.
5056 '(setq identifier-type at-type
5057 identifier-start type-start
5058 got-parens nil
5059 got-identifier t
5060 got-suffix t
5061 got-suffix-after-parens id-start
5062 paren-depth 0))
5063
5064 (if (setq at-type (if (eq backup-at-type 'prefix)
5065 t
5066 backup-at-type))
5067 (setq type-start backup-type-start
5068 id-start backup-id-start)
5069 (setq type-start start-pos
5070 id-start start-pos))
5071
5072 ;; When these flags already are set we've found specifiers that
5073 ;; unconditionally signal these attributes - backtracking doesn't
5074 ;; change that. So keep them set in that case.
5075 (or at-type-decl
5076 (setq at-type-decl backup-at-type-decl))
5077 (or maybe-typeless
5078 (setq maybe-typeless backup-maybe-typeless))
5079
5080 ,(unless short
5081 ;; This identifier is bound only in the inner let.
5082 '(setq start id-start))))
5083
5084 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
5085 ;; Move forward over a declaration or a cast if at the start of one.
5086 ;; The point is assumed to be at the start of some token. Nil is
5087 ;; returned if no declaration or cast is recognized, and the point
5088 ;; is clobbered in that case.
5089 ;;
5090 ;; If a declaration is parsed:
5091 ;;
5092 ;; The point is left at the first token after the first complete
5093 ;; declarator, if there is one. The return value is a cons where
5094 ;; the car is the position of the first token in the declarator. (See
5095 ;; below for the cdr.)
5096 ;; Some examples:
5097 ;;
5098 ;; void foo (int a, char *b) stuff ...
5099 ;; car ^ ^ point
5100 ;; float (*a)[], b;
5101 ;; car ^ ^ point
5102 ;; unsigned int a = c_style_initializer, b;
5103 ;; car ^ ^ point
5104 ;; unsigned int a (cplusplus_style_initializer), b;
5105 ;; car ^ ^ point (might change)
5106 ;; class Foo : public Bar {}
5107 ;; car ^ ^ point
5108 ;; class PikeClass (int a, string b) stuff ...
5109 ;; car ^ ^ point
5110 ;; enum bool;
5111 ;; car ^ ^ point
5112 ;; enum bool flag;
5113 ;; car ^ ^ point
5114 ;; void cplusplus_function (int x) throw (Bad);
5115 ;; car ^ ^ point
5116 ;; Foo::Foo (int b) : Base (b) {}
5117 ;; car ^ ^ point
5118 ;;
5119 ;; The cdr of the return value is non-nil iff a `c-typedef-decl-kwds'
5120 ;; specifier (e.g. class, struct, enum, typedef) is found in the
5121 ;; declaration, i.e. the declared identifier(s) are types.
5122 ;;
5123 ;; If a cast is parsed:
5124 ;;
5125 ;; The point is left at the first token after the closing paren of
5126 ;; the cast. The return value is `cast'. Note that the start
5127 ;; position must be at the first token inside the cast parenthesis
5128 ;; to recognize it.
5129 ;;
5130 ;; PRECEDING-TOKEN-END is the first position after the preceding
5131 ;; token, i.e. on the other side of the syntactic ws from the point.
5132 ;; Use a value less than or equal to (point-min) if the point is at
5133 ;; the first token in (the visible part of) the buffer.
5134 ;;
5135 ;; CONTEXT is a symbol that describes the context at the point:
5136 ;; 'decl In a comma-separated declaration context (typically
5137 ;; inside a function declaration arglist).
5138 ;; '<> In an angle bracket arglist.
5139 ;; 'arglist Some other type of arglist.
5140 ;; nil Some other context or unknown context.
5141 ;;
5142 ;; LAST-CAST-END is the first token after the closing paren of a
5143 ;; preceding cast, or nil if none is known. If
5144 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
5145 ;; the position after the closest preceding call where a cast was
5146 ;; matched. In that case it's used to discover chains of casts like
5147 ;; "(a) (b) c".
5148 ;;
5149 ;; This function records identifier ranges on
5150 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5151 ;; `c-record-type-identifiers' is non-nil.
5152 ;;
5153 ;; This function might do hidden buffer changes.
5154
5155 (let (;; `start-pos' is used below to point to the start of the
5156 ;; first type, i.e. after any leading specifiers. It might
5157 ;; also point at the beginning of the preceding syntactic
5158 ;; whitespace.
5159 (start-pos (point))
5160 ;; Set to the result of `c-forward-type'.
5161 at-type
5162 ;; The position of the first token in what we currently
5163 ;; believe is the type in the declaration or cast, after any
5164 ;; specifiers and their associated clauses.
5165 type-start
5166 ;; The position of the first token in what we currently
5167 ;; believe is the declarator for the first identifier. Set
5168 ;; when the type is found, and moved forward over any
5169 ;; `c-decl-hangon-kwds' and their associated clauses that
5170 ;; occurs after the type.
5171 id-start
5172 ;; These store `at-type', `type-start' and `id-start' of the
5173 ;; identifier before the one in those variables. The previous
5174 ;; identifier might turn out to be the real type in a
5175 ;; declaration if the last one has to be the declarator in it.
5176 ;; If `backup-at-type' is nil then the other variables have
5177 ;; undefined values.
5178 backup-at-type backup-type-start backup-id-start
5179 ;; Set if we've found a specifier that makes the defined
5180 ;; identifier(s) types.
5181 at-type-decl
5182 ;; Set if we've found a specifier that can start a declaration
5183 ;; where there's no type.
5184 maybe-typeless
5185 ;; If a specifier is found that also can be a type prefix,
5186 ;; these flags are set instead of those above. If we need to
5187 ;; back up an identifier, they are copied to the real flag
5188 ;; variables. Thus they only take effect if we fail to
5189 ;; interpret it as a type.
5190 backup-at-type-decl backup-maybe-typeless
5191 ;; Whether we've found a declaration or a cast. We might know
5192 ;; this before we've found the type in it. It's 'ids if we've
5193 ;; found two consecutive identifiers (usually a sure sign, but
5194 ;; we should allow that in labels too), and t if we've found a
5195 ;; specifier keyword (a 100% sure sign).
5196 at-decl-or-cast
5197 ;; Set when we need to back up to parse this as a declaration
5198 ;; but not as a cast.
5199 backup-if-not-cast
5200 ;; For casts, the return position.
5201 cast-end
5202 ;; Save `c-record-type-identifiers' and
5203 ;; `c-record-ref-identifiers' since ranges are recorded
5204 ;; speculatively and should be thrown away if it turns out
5205 ;; that it isn't a declaration or cast.
5206 (save-rec-type-ids c-record-type-identifiers)
5207 (save-rec-ref-ids c-record-ref-identifiers))
5208
5209 ;; Check for a type. Unknown symbols are treated as possible
5210 ;; types, but they could also be specifiers disguised through
5211 ;; macros like __INLINE__, so we recognize both types and known
5212 ;; specifiers after them too.
5213 (while
5214 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
5215
5216 ;; Look for a specifier keyword clause.
5217 (when (looking-at c-prefix-spec-kwds-re)
5218 (setq kwd-sym (c-keyword-sym (match-string 1)))
5219 (save-excursion
5220 (c-forward-keyword-clause 1)
5221 (setq kwd-clause-end (point))))
5222
5223 (when (setq found-type (c-forward-type))
5224 ;; Found a known or possible type or a prefix of a known type.
5225
5226 (when at-type
5227 ;; Got two identifiers with nothing but whitespace
5228 ;; between them. That can only happen in declarations.
5229 (setq at-decl-or-cast 'ids)
5230
5231 (when (eq at-type 'found)
5232 ;; If the previous identifier is a found type we
5233 ;; record it as a real one; it might be some sort of
5234 ;; alias for a prefix like "unsigned".
5235 (save-excursion
5236 (goto-char type-start)
5237 (let ((c-promote-possible-types t))
5238 (c-forward-type)))))
5239
5240 (setq backup-at-type at-type
5241 backup-type-start type-start
5242 backup-id-start id-start
5243 at-type found-type
5244 type-start start
5245 id-start (point)
5246 ;; The previous ambiguous specifier/type turned out
5247 ;; to be a type since we've parsed another one after
5248 ;; it, so clear these backup flags.
5249 backup-at-type-decl nil
5250 backup-maybe-typeless nil))
5251
5252 (if kwd-sym
5253 (progn
5254 ;; Handle known specifier keywords and
5255 ;; `c-decl-hangon-kwds' which can occur after known
5256 ;; types.
5257
5258 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
5259 ;; It's a hang-on keyword that can occur anywhere.
5260 (progn
5261 (setq at-decl-or-cast t)
5262 (if at-type
5263 ;; Move the identifier start position if
5264 ;; we've passed a type.
5265 (setq id-start kwd-clause-end)
5266 ;; Otherwise treat this as a specifier and
5267 ;; move the fallback position.
5268 (setq start-pos kwd-clause-end))
5269 (goto-char kwd-clause-end))
5270
5271 ;; It's an ordinary specifier so we know that
5272 ;; anything before this can't be the type.
5273 (setq backup-at-type nil
5274 start-pos kwd-clause-end)
5275
5276 (if found-type
5277 ;; It's ambiguous whether this keyword is a
5278 ;; specifier or a type prefix, so set the backup
5279 ;; flags. (It's assumed that `c-forward-type'
5280 ;; moved further than `c-forward-keyword-clause'.)
5281 (progn
5282 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5283 (setq backup-at-type-decl t))
5284 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5285 (setq backup-maybe-typeless t)))
5286
5287 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5288 (setq at-type-decl t))
5289 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5290 (setq maybe-typeless t))
5291
5292 ;; Haven't matched a type so it's an umambiguous
5293 ;; specifier keyword and we know we're in a
5294 ;; declaration.
5295 (setq at-decl-or-cast t)
5296
5297 (goto-char kwd-clause-end))))
5298
5299 ;; If the type isn't known we continue so that we'll jump
5300 ;; over all specifiers and type identifiers. The reason
5301 ;; to do this for a known type prefix is to make things
5302 ;; like "unsigned INT16" work.
5303 (and found-type (not (eq found-type t))))))
5304
5305 (cond
5306 ((eq at-type t)
5307 ;; If a known type was found, we still need to skip over any
5308 ;; hangon keyword clauses after it. Otherwise it has already
5309 ;; been done in the loop above.
5310 (while (looking-at c-decl-hangon-key)
5311 (c-forward-keyword-clause 1))
5312 (setq id-start (point)))
5313
5314 ((eq at-type 'prefix)
5315 ;; A prefix type is itself a primitive type when it's not
5316 ;; followed by another type.
5317 (setq at-type t))
5318
5319 ((not at-type)
5320 ;; Got no type but set things up to continue anyway to handle
5321 ;; the various cases when a declaration doesn't start with a
5322 ;; type.
5323 (setq id-start start-pos))
5324
5325 ((and (eq at-type 'maybe)
5326 (c-major-mode-is 'c++-mode))
5327 ;; If it's C++ then check if the last "type" ends on the form
5328 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
5329 ;; (con|de)structor.
5330 (save-excursion
5331 (let (name end-2 end-1)
5332 (goto-char id-start)
5333 (c-backward-syntactic-ws)
5334 (setq end-2 (point))
5335 (when (and
5336 (c-simple-skip-symbol-backward)
5337 (progn
5338 (setq name
5339 (buffer-substring-no-properties (point) end-2))
5340 ;; Cheating in the handling of syntactic ws below.
5341 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
5342 (progn
5343 (setq end-1 (point))
5344 (c-simple-skip-symbol-backward))
5345 (>= (point) type-start)
5346 (equal (buffer-substring-no-properties (point) end-1)
5347 name))
5348 ;; It is a (con|de)structor name. In that case the
5349 ;; declaration is typeless so zap out any preceding
5350 ;; identifier(s) that we might have taken as types.
5351 (goto-char type-start)
5352 (setq at-type nil
5353 backup-at-type nil
5354 id-start type-start))))))
5355
5356 ;; Check for and step over a type decl expression after the thing
5357 ;; that is or might be a type. This can't be skipped since we
5358 ;; need the correct end position of the declarator for
5359 ;; `max-type-decl-end-*'.
5360 (let ((start (point)) (paren-depth 0) pos
5361 ;; True if there's a non-open-paren match of
5362 ;; `c-type-decl-prefix-key'.
5363 got-prefix
5364 ;; True if the declarator is surrounded by a parenthesis pair.
5365 got-parens
5366 ;; True if there is an identifier in the declarator.
5367 got-identifier
5368 ;; True if there's a non-close-paren match of
5369 ;; `c-type-decl-suffix-key'.
5370 got-suffix
5371 ;; True if there's a prefix match outside the outermost
5372 ;; paren pair that surrounds the declarator.
5373 got-prefix-before-parens
5374 ;; True if there's a suffix match outside the outermost
5375 ;; paren pair that surrounds the declarator. The value is
5376 ;; the position of the first suffix match.
5377 got-suffix-after-parens
5378 ;; True if we've parsed the type decl to a token that is
5379 ;; known to end declarations in this context.
5380 at-decl-end
5381 ;; The earlier values of `at-type' and `type-start' if we've
5382 ;; shifted the type backwards.
5383 identifier-type identifier-start
5384 ;; If `c-parse-and-markup-<>-arglists' is set we need to
5385 ;; turn it off during the name skipping below to avoid
5386 ;; getting `c-type' properties that might be bogus. That
5387 ;; can happen since we don't know if
5388 ;; `c-restricted-<>-arglists' will be correct inside the
5389 ;; arglist paren that gets entered.
5390 c-parse-and-markup-<>-arglists)
5391
5392 (goto-char id-start)
5393
5394 ;; Skip over type decl prefix operators. (Note similar code in
5395 ;; `c-font-lock-declarators'.)
5396 (while (and (looking-at c-type-decl-prefix-key)
5397 (if (and (c-major-mode-is 'c++-mode)
5398 (match-beginning 2))
5399 ;; If the second submatch matches in C++ then
5400 ;; we're looking at an identifier that's a
5401 ;; prefix only if it specifies a member pointer.
5402 (when (setq got-identifier (c-forward-name))
5403 (if (looking-at "\\(::\\)")
5404 ;; We only check for a trailing "::" and
5405 ;; let the "*" that should follow be
5406 ;; matched in the next round.
5407 (progn (setq got-identifier nil) t)
5408 ;; It turned out to be the real identifier,
5409 ;; so stop.
5410 nil))
5411 t))
5412
5413 (if (eq (char-after) ?\()
5414 (progn
5415 (setq paren-depth (1+ paren-depth))
5416 (forward-char))
5417 (unless got-prefix-before-parens
5418 (setq got-prefix-before-parens (= paren-depth 0)))
5419 (setq got-prefix t)
5420 (goto-char (match-end 1)))
5421 (c-forward-syntactic-ws))
5422
5423 (setq got-parens (> paren-depth 0))
5424
5425 ;; Skip over an identifier.
5426 (or got-identifier
5427 (and (looking-at c-identifier-start)
5428 (setq got-identifier (c-forward-name))))
5429
5430 ;; Skip over type decl suffix operators.
5431 (while (if (looking-at c-type-decl-suffix-key)
5432
5433 (if (eq (char-after) ?\))
5434 (when (> paren-depth 0)
5435 (setq paren-depth (1- paren-depth))
5436 (forward-char)
5437 t)
5438 (when (if (save-match-data (looking-at "\\s\("))
5439 (c-safe (c-forward-sexp 1) t)
5440 (goto-char (match-end 1))
5441 t)
5442 (when (and (not got-suffix-after-parens)
5443 (= paren-depth 0))
5444 (setq got-suffix-after-parens (match-beginning 0)))
5445 (setq got-suffix t)))
5446
5447 ;; No suffix matched. We might have matched the
5448 ;; identifier as a type and the open paren of a
5449 ;; function arglist as a type decl prefix. In that
5450 ;; case we should "backtrack": Reinterpret the last
5451 ;; type as the identifier, move out of the arglist and
5452 ;; continue searching for suffix operators.
5453 ;;
5454 ;; Do this even if there's no preceding type, to cope
5455 ;; with old style function declarations in K&R C,
5456 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
5457 ;; style declarations. That isn't applicable in an
5458 ;; arglist context, though.
5459 (when (and (= paren-depth 1)
5460 (not got-prefix-before-parens)
5461 (not (eq at-type t))
5462 (or backup-at-type
5463 maybe-typeless
5464 backup-maybe-typeless
5465 (when c-recognize-typeless-decls
5466 (not context)))
5467 (setq pos (c-up-list-forward (point)))
5468 (eq (char-before pos) ?\)))
5469 (c-fdoc-shift-type-backward)
5470 (goto-char pos)
5471 t))
5472
5473 (c-forward-syntactic-ws))
5474
5475 (when (and (or maybe-typeless backup-maybe-typeless)
5476 (not got-identifier)
5477 (not got-prefix)
5478 at-type)
5479 ;; Have found no identifier but `c-typeless-decl-kwds' has
5480 ;; matched so we know we're inside a declaration. The
5481 ;; preceding type must be the identifier instead.
5482 (c-fdoc-shift-type-backward))
5483
5484 (setq
5485 at-decl-or-cast
5486 (catch 'at-decl-or-cast
5487
5488 (when (> paren-depth 0)
5489 ;; Encountered something inside parens that isn't matched by
5490 ;; the `c-type-decl-*' regexps, so it's not a type decl
5491 ;; expression. Try to skip out to the same paren depth to
5492 ;; not confuse the cast check below.
5493 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
5494 ;; If we've found a specifier keyword then it's a
5495 ;; declaration regardless.
5496 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
5497
5498 (setq at-decl-end
5499 (looking-at (cond ((eq context '<>) "[,>]")
5500 (context "[,\)]")
5501 (t "[,;]"))))
5502
5503 ;; Now we've collected info about various characteristics of
5504 ;; the construct we're looking at. Below follows a decision
5505 ;; tree based on that. It's ordered to check more certain
5506 ;; signs before less certain ones.
5507
5508 (if got-identifier
5509 (progn
5510
5511 (when (and (or at-type maybe-typeless)
5512 (not (or got-prefix got-parens)))
5513 ;; Got another identifier directly after the type, so it's a
5514 ;; declaration.
5515 (throw 'at-decl-or-cast t))
5516
5517 (when (and got-parens
5518 (not got-prefix)
5519 (not got-suffix-after-parens)
5520 (or backup-at-type
5521 maybe-typeless
5522 backup-maybe-typeless))
5523 ;; Got a declaration of the form "foo bar (gnu);" where we've
5524 ;; recognized "bar" as the type and "gnu" as the declarator.
5525 ;; In this case it's however more likely that "bar" is the
5526 ;; declarator and "gnu" a function argument or initializer (if
5527 ;; `c-recognize-paren-inits' is set), since the parens around
5528 ;; "gnu" would be superfluous if it's a declarator. Shift the
5529 ;; type one step backward.
5530 (c-fdoc-shift-type-backward)))
5531
5532 ;; Found no identifier.
5533
5534 (if backup-at-type
5535 (progn
5536
5537 (when (= (point) start)
5538 ;; Got a plain list of identifiers. If a colon follows it's
5539 ;; a valid label. Otherwise the last one probably is the
5540 ;; declared identifier and we should back up to the previous
5541 ;; type, providing it isn't a cast.
5542 (if (eq (char-after) ?:)
5543 ;; If we've found a specifier keyword then it's a
5544 ;; declaration regardless.
5545 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
5546 (setq backup-if-not-cast t)
5547 (throw 'at-decl-or-cast t)))
5548
5549 (when (and got-suffix
5550 (not got-prefix)
5551 (not got-parens))
5552 ;; Got a plain list of identifiers followed by some suffix.
5553 ;; If this isn't a cast then the last identifier probably is
5554 ;; the declared one and we should back up to the previous
5555 ;; type.
5556 (setq backup-if-not-cast t)
5557 (throw 'at-decl-or-cast t)))
5558
5559 (when (eq at-type t)
5560 ;; If the type is known we know that there can't be any
5561 ;; identifier somewhere else, and it's only in declarations in
5562 ;; e.g. function prototypes and in casts that the identifier may
5563 ;; be left out.
5564 (throw 'at-decl-or-cast t))
5565
5566 (when (= (point) start)
5567 ;; Only got a single identifier (parsed as a type so far).
5568 (if (and
5569 ;; Check that the identifier isn't at the start of an
5570 ;; expression.
5571 at-decl-end
5572 (cond
5573 ((eq context 'decl)
5574 ;; Inside an arglist that contains declarations. If K&R
5575 ;; style declarations and parenthesis style initializers
5576 ;; aren't allowed then the single identifier must be a
5577 ;; type, else we require that it's known or found
5578 ;; (primitive types are handled above).
5579 (or (and (not c-recognize-knr-p)
5580 (not c-recognize-paren-inits))
5581 (memq at-type '(known found))))
5582 ((eq context '<>)
5583 ;; Inside a template arglist. Accept known and found
5584 ;; types; other identifiers could just as well be
5585 ;; constants in C++.
5586 (memq at-type '(known found)))))
5587 (throw 'at-decl-or-cast t)
5588 ;; Can't be a valid declaration or cast, but if we've found a
5589 ;; specifier it can't be anything else either, so treat it as
5590 ;; an invalid/unfinished declaration or cast.
5591 (throw 'at-decl-or-cast at-decl-or-cast))))
5592
5593 (if (and got-parens
5594 (not got-prefix)
5595 (not context)
5596 (not (eq at-type t))
5597 (or backup-at-type
5598 maybe-typeless
5599 backup-maybe-typeless
5600 (when c-recognize-typeless-decls
5601 (or (not got-suffix)
5602 (not (looking-at
5603 c-after-suffixed-type-maybe-decl-key))))))
5604 ;; Got an empty paren pair and a preceding type that probably
5605 ;; really is the identifier. Shift the type backwards to make
5606 ;; the last one the identifier. This is analogous to the
5607 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
5608 ;; above.
5609 ;;
5610 ;; Exception: In addition to the conditions in that
5611 ;; "backtracking" code, do not shift backward if we're not
5612 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
5613 ;; Since there's no preceding type, the shift would mean that
5614 ;; the declaration is typeless. But if the regexp doesn't match
5615 ;; then we will simply fall through in the tests below and not
5616 ;; recognize it at all, so it's better to try it as an abstract
5617 ;; declarator instead.
5618 (c-fdoc-shift-type-backward)
5619
5620 ;; Still no identifier.
5621
5622 (when (and got-prefix (or got-parens got-suffix))
5623 ;; Require `got-prefix' together with either `got-parens' or
5624 ;; `got-suffix' to recognize it as an abstract declarator:
5625 ;; `got-parens' only is probably an empty function call.
5626 ;; `got-suffix' only can build an ordinary expression together
5627 ;; with the preceding identifier which we've taken as a type.
5628 ;; We could actually accept on `got-prefix' only, but that can
5629 ;; easily occur temporarily while writing an expression so we
5630 ;; avoid that case anyway. We could do a better job if we knew
5631 ;; the point when the fontification was invoked.
5632 (throw 'at-decl-or-cast t))
5633
5634 (when (and at-type
5635 (not got-prefix)
5636 (not got-parens)
5637 got-suffix-after-parens
5638 (eq (char-after got-suffix-after-parens) ?\())
5639 ;; Got a type, no declarator but a paren suffix. I.e. it's a
5640 ;; normal function call afterall (or perhaps a C++ style object
5641 ;; instantiation expression).
5642 (throw 'at-decl-or-cast nil))))
5643
5644 (when at-decl-or-cast
5645 ;; By now we've located the type in the declaration that we know
5646 ;; we're in.
5647 (throw 'at-decl-or-cast t))
5648
5649 (when (and got-identifier
5650 (not context)
5651 (looking-at c-after-suffixed-type-decl-key)
5652 (if (and got-parens
5653 (not got-prefix)
5654 (not got-suffix)
5655 (not (eq at-type t)))
5656 ;; Shift the type backward in the case that there's a
5657 ;; single identifier inside parens. That can only
5658 ;; occur in K&R style function declarations so it's
5659 ;; more likely that it really is a function call.
5660 ;; Therefore we only do this after
5661 ;; `c-after-suffixed-type-decl-key' has matched.
5662 (progn (c-fdoc-shift-type-backward) t)
5663 got-suffix-after-parens))
5664 ;; A declaration according to `c-after-suffixed-type-decl-key'.
5665 (throw 'at-decl-or-cast t))
5666
5667 (when (and (or got-prefix (not got-parens))
5668 (memq at-type '(t known)))
5669 ;; It's a declaration if a known type precedes it and it can't be a
5670 ;; function call.
5671 (throw 'at-decl-or-cast t))
5672
5673 ;; If we get here we can't tell if this is a type decl or a normal
5674 ;; expression by looking at it alone. (That's under the assumption
5675 ;; that normal expressions always can look like type decl expressions,
5676 ;; which isn't really true but the cases where it doesn't hold are so
5677 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
5678 ;; the effort to look for them.)
5679
5680 (unless (or at-decl-end (looking-at "=[^=]"))
5681 ;; If this is a declaration it should end here or its initializer(*)
5682 ;; should start here, so check for allowed separation tokens. Note
5683 ;; that this rule doesn't work e.g. with a K&R arglist after a
5684 ;; function header.
5685 ;;
5686 ;; *) Don't check for C++ style initializers using parens
5687 ;; since those already have been matched as suffixes.
5688 ;;
5689 ;; If `at-decl-or-cast' is then we've found some other sign that
5690 ;; it's a declaration or cast, so then it's probably an
5691 ;; invalid/unfinished one.
5692 (throw 'at-decl-or-cast at-decl-or-cast))
5693
5694 ;; Below are tests that only should be applied when we're certain to
5695 ;; not have parsed halfway through an expression.
5696
5697 (when (memq at-type '(t known))
5698 ;; The expression starts with a known type so treat it as a
5699 ;; declaration.
5700 (throw 'at-decl-or-cast t))
5701
5702 (when (and (c-major-mode-is 'c++-mode)
5703 ;; In C++ we check if the identifier is a known type, since
5704 ;; (con|de)structors use the class name as identifier.
5705 ;; We've always shifted over the identifier as a type and
5706 ;; then backed up again in this case.
5707 identifier-type
5708 (or (memq identifier-type '(found known))
5709 (and (eq (char-after identifier-start) ?~)
5710 ;; `at-type' probably won't be 'found for
5711 ;; destructors since the "~" is then part of the
5712 ;; type name being checked against the list of
5713 ;; known types, so do a check without that
5714 ;; operator.
5715 (or (save-excursion
5716 (goto-char (1+ identifier-start))
5717 (c-forward-syntactic-ws)
5718 (c-with-syntax-table
5719 c-identifier-syntax-table
5720 (looking-at c-known-type-key)))
5721 (save-excursion
5722 (goto-char (1+ identifier-start))
5723 ;; We have already parsed the type earlier,
5724 ;; so it'd be possible to cache the end
5725 ;; position instead of redoing it here, but
5726 ;; then we'd need to keep track of another
5727 ;; position everywhere.
5728 (c-check-type (point)
5729 (progn (c-forward-type)
5730 (point))))))))
5731 (throw 'at-decl-or-cast t))
5732
5733 (if got-identifier
5734 (progn
5735 (when (and got-prefix-before-parens
5736 at-type
5737 (or at-decl-end (looking-at "=[^=]"))
5738 (not context)
5739 (not got-suffix))
5740 ;; Got something like "foo * bar;". Since we're not inside an
5741 ;; arglist it would be a meaningless expression because the
5742 ;; result isn't used. We therefore choose to recognize it as
5743 ;; a declaration. Do not allow a suffix since it could then
5744 ;; be a function call.
5745 (throw 'at-decl-or-cast t))
5746
5747 (when (and (or got-suffix-after-parens
5748 (looking-at "=[^=]"))
5749 (eq at-type 'found)
5750 (not (eq context 'arglist)))
5751 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
5752 ;; be an odd expression or it could be a declaration. Treat
5753 ;; it as a declaration if "a" has been used as a type
5754 ;; somewhere else (if it's a known type we won't get here).
5755 (throw 'at-decl-or-cast t)))
5756
5757 (when (and context
5758 (or got-prefix
5759 (and (eq context 'decl)
5760 (not c-recognize-paren-inits)
5761 (or got-parens got-suffix))))
5762 ;; Got a type followed by an abstract declarator. If `got-prefix'
5763 ;; is set it's something like "a *" without anything after it. If
5764 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
5765 ;; or similar, which we accept only if the context rules out
5766 ;; expressions.
5767 (throw 'at-decl-or-cast t)))
5768
5769 ;; If we had a complete symbol table here (which rules out
5770 ;; `c-found-types') we should return t due to the disambiguation rule
5771 ;; (in at least C++) that anything that can be parsed as a declaration
5772 ;; is a declaration. Now we're being more defensive and prefer to
5773 ;; highlight things like "foo (bar);" as a declaration only if we're
5774 ;; inside an arglist that contains declarations.
5775 (eq context 'decl))))
5776
5777 ;; The point is now after the type decl expression.
5778
5779 (cond
5780 ;; Check for a cast.
5781 ((save-excursion
5782 (and
5783 c-cast-parens
5784
5785 ;; Should be the first type/identifier in a cast paren.
5786 (> preceding-token-end (point-min))
5787 (memq (char-before preceding-token-end) c-cast-parens)
5788
5789 ;; The closing paren should follow.
5790 (progn
5791 (c-forward-syntactic-ws)
5792 (looking-at "\\s\)"))
5793
5794 ;; There should be a primary expression after it.
5795 (let (pos)
5796 (forward-char)
5797 (c-forward-syntactic-ws)
5798 (setq cast-end (point))
5799 (and (looking-at c-primary-expr-regexp)
5800 (progn
5801 (setq pos (match-end 0))
5802 (or
5803 ;; Check if the expression begins with a prefix keyword.
5804 (match-beginning 2)
5805 (if (match-beginning 1)
5806 ;; Expression begins with an ambiguous operator. Treat
5807 ;; it as a cast if it's a type decl or if we've
5808 ;; recognized the type somewhere else.
5809 (or at-decl-or-cast
5810 (memq at-type '(t known found)))
5811 ;; Unless it's a keyword, it's the beginning of a primary
5812 ;; expression.
5813 (not (looking-at c-keywords-regexp)))))
5814 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
5815 ;; that it matched a whole one so that we don't e.g. confuse
5816 ;; the operator '-' with '->'. It's ok if it matches further,
5817 ;; though, since it e.g. can match the float '.5' while the
5818 ;; operator regexp only matches '.'.
5819 (or (not (looking-at c-nonsymbol-token-regexp))
5820 (<= (match-end 0) pos))))
5821
5822 ;; There should either be a cast before it or something that isn't an
5823 ;; identifier or close paren.
5824 (> preceding-token-end (point-min))
5825 (progn
5826 (goto-char (1- preceding-token-end))
5827 (or (eq (point) last-cast-end)
5828 (progn
5829 (c-backward-syntactic-ws)
5830 (if (< (skip-syntax-backward "w_") 0)
5831 ;; It's a symbol. Accept it only if it's one of the
5832 ;; keywords that can precede an expression (without
5833 ;; surrounding parens).
5834 (looking-at c-simple-stmt-key)
5835 (and
5836 ;; Check that it isn't a close paren (block close is ok,
5837 ;; though).
5838 (not (memq (char-before) '(?\) ?\])))
5839 ;; Check that it isn't a nonsymbol identifier.
5840 (not (c-on-identifier)))))))))
5841
5842 ;; Handle the cast.
5843 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5844 (let ((c-promote-possible-types t))
5845 (goto-char type-start)
5846 (c-forward-type)))
5847
5848 (goto-char cast-end)
5849 'cast)
5850
5851 (at-decl-or-cast
5852 ;; We're at a declaration. Highlight the type and the following
5853 ;; declarators.
5854
5855 (when backup-if-not-cast
5856 (c-fdoc-shift-type-backward t))
5857
5858 (when (and (eq context 'decl) (looking-at ","))
5859 ;; Make sure to propagate the `c-decl-arg-start' property to
5860 ;; the next argument if it's set in this one, to cope with
5861 ;; interactive refontification.
5862 (c-put-c-type-property (point) 'c-decl-arg-start))
5863
5864 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5865 (let ((c-promote-possible-types t))
5866 (save-excursion
5867 (goto-char type-start)
5868 (c-forward-type))))
5869
5870 (cons id-start at-type-decl))
5871
5872 (t
5873 ;; False alarm. Restore the recorded ranges.
5874 (setq c-record-type-identifiers save-rec-type-ids
5875 c-record-ref-identifiers save-rec-ref-ids)
5876 nil))))
5877
5878 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
5879 ;; Assuming that point is at the beginning of a token, check if it starts a
5880 ;; label and if so move over it and return non-nil (t in default situations,
5881 ;; specific symbols (see below) for interesting situations), otherwise don't
5882 ;; move and return nil. "Label" here means "most things with a colon".
5883 ;;
5884 ;; More precisely, a "label" is regarded as one of:
5885 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
5886 ;; (ii) A case label - either the entire construct "case FOO:", or just the
5887 ;; bare "case", should the colon be missing. We return t;
5888 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
5889 ;; return t;
5890 ;; (iv) One of QT's "extended" C++ variants of
5891 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
5892 ;; Returns the symbol `qt-2kwds-colon'.
5893 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
5894 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
5895 ;; colon). Currently (2006-03), this applies only to Objective C's
5896 ;; keywords "@private", "@protected", and "@public". Returns t.
5897 ;;
5898 ;; One of the things which will NOT be recognised as a label is a bit-field
5899 ;; element of a struct, something like "int foo:5".
5900 ;;
5901 ;; The end of the label is taken to be just after the colon, or the end of
5902 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
5903 ;; after the end on return. The terminating char gets marked with
5904 ;; `c-decl-end' to improve recognition of the following declaration or
5905 ;; statement.
5906 ;;
5907 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
5908 ;; label, if any, has already been marked up like that.
5909 ;;
5910 ;; If PRECEDING-TOKEN-END is given, it should be the first position
5911 ;; after the preceding token, i.e. on the other side of the
5912 ;; syntactic ws from the point. Use a value less than or equal to
5913 ;; (point-min) if the point is at the first token in (the visible
5914 ;; part of) the buffer.
5915 ;;
5916 ;; The optional LIMIT limits the forward scan for the colon.
5917 ;;
5918 ;; This function records the ranges of the label symbols on
5919 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
5920 ;; non-nil.
5921 ;;
5922 ;; This function might do hidden buffer changes.
5923
5924 (let ((start (point))
5925 label-end
5926 qt-symbol-idx
5927 macro-start ; if we're in one.
5928 label-type)
5929 (cond
5930 ;; "case" or "default" (Doesn't apply to AWK).
5931 ((looking-at c-label-kwds-regexp)
5932 (let ((kwd-end (match-end 1)))
5933 ;; Record only the keyword itself for fontification, since in
5934 ;; case labels the following is a constant expression and not
5935 ;; a label.
5936 (when c-record-type-identifiers
5937 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
5938
5939 ;; Find the label end.
5940 (goto-char kwd-end)
5941 (setq label-type
5942 (if (and (c-syntactic-re-search-forward
5943 ;; Stop on chars that aren't allowed in expressions,
5944 ;; and on operator chars that would be meaningless
5945 ;; there. FIXME: This doesn't cope with ?: operators.
5946 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
5947 limit t t nil 1)
5948 (match-beginning 2))
5949
5950 (progn ; there's a proper :
5951 (goto-char (match-beginning 2)) ; just after the :
5952 (c-put-c-type-property (1- (point)) 'c-decl-end)
5953 t)
5954
5955 ;; It's an unfinished label. We consider the keyword enough
5956 ;; to recognize it as a label, so that it gets fontified.
5957 ;; Leave the point at the end of it, but don't put any
5958 ;; `c-decl-end' marker.
5959 (goto-char kwd-end)
5960 t))))
5961
5962 ;; @private, @protected, @public, in Objective C, or similar.
5963 ((and c-opt-extra-label-key
5964 (looking-at c-opt-extra-label-key))
5965 ;; For a `c-opt-extra-label-key' match, we record the whole
5966 ;; thing for fontification. That's to get the leading '@' in
5967 ;; Objective-C protection labels fontified.
5968 (goto-char (match-end 1))
5969 (when c-record-type-identifiers
5970 (c-record-ref-id (cons (match-beginning 1) (point))))
5971 (c-put-c-type-property (1- (point)) 'c-decl-end)
5972 (setq label-type t))
5973
5974 ;; All other cases of labels.
5975 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
5976
5977 ;; A colon label must have something before the colon.
5978 (not (eq (char-after) ?:))
5979
5980 ;; Check that we're not after a token that can't precede a label.
5981 (or
5982 ;; Trivially succeeds when there's no preceding token.
5983 (if preceding-token-end
5984 (<= preceding-token-end (point-min))
5985 (save-excursion
5986 (c-backward-syntactic-ws)
5987 (setq preceding-token-end (point))
5988 (bobp)))
5989
5990 ;; Check if we're after a label, if we're after a closing
5991 ;; paren that belong to statement, and with
5992 ;; `c-label-prefix-re'. It's done in different order
5993 ;; depending on `assume-markup' since the checks have
5994 ;; different expensiveness.
5995 (if assume-markup
5996 (or
5997 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
5998 'c-decl-end)
5999
6000 (save-excursion
6001 (goto-char (1- preceding-token-end))
6002 (c-beginning-of-current-token)
6003 (or (looking-at c-label-prefix-re)
6004 (looking-at c-block-stmt-1-key)))
6005
6006 (and (eq (char-before preceding-token-end) ?\))
6007 (c-after-conditional)))
6008
6009 (or
6010 (save-excursion
6011 (goto-char (1- preceding-token-end))
6012 (c-beginning-of-current-token)
6013 (or (looking-at c-label-prefix-re)
6014 (looking-at c-block-stmt-1-key)))
6015
6016 (cond
6017 ((eq (char-before preceding-token-end) ?\))
6018 (c-after-conditional))
6019
6020 ((eq (char-before preceding-token-end) ?:)
6021 ;; Might be after another label, so check it recursively.
6022 (save-restriction
6023 (save-excursion
6024 (goto-char (1- preceding-token-end))
6025 ;; Essentially the same as the
6026 ;; `c-syntactic-re-search-forward' regexp below.
6027 (setq macro-start
6028 (save-excursion (and (c-beginning-of-macro)
6029 (point))))
6030 (if macro-start (narrow-to-region macro-start (point-max)))
6031 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
6032 ;; Note: the following should work instead of the
6033 ;; narrow-to-region above. Investigate why not,
6034 ;; sometime. ACM, 2006-03-31.
6035 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
6036 ;; macro-start t)
6037 (let ((pte (point))
6038 ;; If the caller turned on recording for us,
6039 ;; it shouldn't apply when we check the
6040 ;; preceding label.
6041 c-record-type-identifiers)
6042 ;; A label can't start at a cpp directive. Check for
6043 ;; this, since c-forward-syntactic-ws would foul up on it.
6044 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
6045 (c-forward-syntactic-ws)
6046 (c-forward-label nil pte start))))))))))
6047
6048 ;; Point is still at the beginning of the possible label construct.
6049 ;;
6050 ;; Check that the next nonsymbol token is ":", or that we're in one
6051 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
6052 ;; arguments. FIXME: Should build this regexp from the language
6053 ;; constants.
6054 (cond
6055 ;; public: protected: private:
6056 ((and
6057 (c-major-mode-is 'c++-mode)
6058 (search-forward-regexp
6059 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
6060 (progn (backward-char)
6061 (c-forward-syntactic-ws limit)
6062 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
6063 (forward-char)
6064 (setq label-type t))
6065 ;; QT double keyword like "protected slots:" or goto target.
6066 ((progn (goto-char start) nil))
6067 ((when (c-syntactic-re-search-forward
6068 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
6069 (backward-char)
6070 (setq label-end (point))
6071 (setq qt-symbol-idx
6072 (and (c-major-mode-is 'c++-mode)
6073 (string-match
6074 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
6075 (buffer-substring start (point)))))
6076 (c-forward-syntactic-ws limit)
6077 (cond
6078 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
6079 (forward-char)
6080 (setq label-type
6081 (if (string= "signals" ; Special QT macro
6082 (buffer-substring-no-properties start label-end))
6083 'qt-1kwd-colon
6084 'goto-target)))
6085 ((and qt-symbol-idx
6086 (search-forward-regexp "\\=slots\\>" limit t)
6087 (progn (c-forward-syntactic-ws limit)
6088 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
6089 (forward-char)
6090 (setq label-type 'qt-2kwds-colon)))))))
6091
6092 (save-restriction
6093 (narrow-to-region start (point))
6094
6095 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
6096 (catch 'check-label
6097 (goto-char start)
6098 (while (progn
6099 (when (looking-at c-nonlabel-token-key)
6100 (goto-char start)
6101 (setq label-type nil)
6102 (throw 'check-label nil))
6103 (and (c-safe (c-forward-sexp)
6104 (c-forward-syntactic-ws)
6105 t)
6106 (not (eobp)))))
6107
6108 ;; Record the identifiers in the label for fontification, unless
6109 ;; it begins with `c-label-kwds' in which case the following
6110 ;; identifiers are part of a (constant) expression that
6111 ;; shouldn't be fontified.
6112 (when (and c-record-type-identifiers
6113 (progn (goto-char start)
6114 (not (looking-at c-label-kwds-regexp))))
6115 (while (c-syntactic-re-search-forward c-symbol-key nil t)
6116 (c-record-ref-id (cons (match-beginning 0)
6117 (match-end 0)))))
6118
6119 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
6120 (goto-char (point-max)))))
6121
6122 (t
6123 ;; Not a label.
6124 (goto-char start)))
6125 label-type))
6126
6127 (defun c-forward-objc-directive ()
6128 ;; Assuming the point is at the beginning of a token, try to move
6129 ;; forward to the end of the Objective-C directive that starts
6130 ;; there. Return t if a directive was fully recognized, otherwise
6131 ;; the point is moved as far as one could be successfully parsed and
6132 ;; nil is returned.
6133 ;;
6134 ;; This function records identifier ranges on
6135 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6136 ;; `c-record-type-identifiers' is non-nil.
6137 ;;
6138 ;; This function might do hidden buffer changes.
6139
6140 (let ((start (point))
6141 start-char
6142 (c-promote-possible-types t)
6143 ;; Turn off recognition of angle bracket arglists while parsing
6144 ;; types here since the protocol reference list might then be
6145 ;; considered part of the preceding name or superclass-name.
6146 c-recognize-<>-arglists)
6147
6148 (if (or
6149 (when (looking-at
6150 (eval-when-compile
6151 (c-make-keywords-re t
6152 (append (c-lang-const c-protection-kwds objc)
6153 '("@end"))
6154 'objc-mode)))
6155 (goto-char (match-end 1))
6156 t)
6157
6158 (and
6159 (looking-at
6160 (eval-when-compile
6161 (c-make-keywords-re t
6162 '("@interface" "@implementation" "@protocol")
6163 'objc-mode)))
6164
6165 ;; Handle the name of the class itself.
6166 (progn
6167 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
6168 ; at EOB.
6169 (goto-char (match-end 0))
6170 (c-skip-ws-forward)
6171 (c-forward-type))
6172
6173 (catch 'break
6174 ;; Look for ": superclass-name" or "( category-name )".
6175 (when (looking-at "[:\(]")
6176 (setq start-char (char-after))
6177 (forward-char)
6178 (c-forward-syntactic-ws)
6179 (unless (c-forward-type) (throw 'break nil))
6180 (when (eq start-char ?\()
6181 (unless (eq (char-after) ?\)) (throw 'break nil))
6182 (forward-char)
6183 (c-forward-syntactic-ws)))
6184
6185 ;; Look for a protocol reference list.
6186 (if (eq (char-after) ?<)
6187 (let ((c-recognize-<>-arglists t)
6188 (c-parse-and-markup-<>-arglists t)
6189 c-restricted-<>-arglists)
6190 (c-forward-<>-arglist t))
6191 t))))
6192
6193 (progn
6194 (c-backward-syntactic-ws)
6195 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
6196 (c-put-c-type-property (1- (point)) 'c-decl-end)
6197 t)
6198
6199 (c-clear-c-type-property start (point) 'c-decl-end)
6200 nil)))
6201
6202 (defun c-beginning-of-inheritance-list (&optional lim)
6203 ;; Go to the first non-whitespace after the colon that starts a
6204 ;; multiple inheritance introduction. Optional LIM is the farthest
6205 ;; back we should search.
6206 ;;
6207 ;; This function might do hidden buffer changes.
6208 (c-with-syntax-table c++-template-syntax-table
6209 (c-backward-token-2 0 t lim)
6210 (while (and (or (looking-at c-symbol-start)
6211 (looking-at "[<,]\\|::"))
6212 (zerop (c-backward-token-2 1 t lim))))))
6213
6214 (defun c-in-method-def-p ()
6215 ;; Return nil if we aren't in a method definition, otherwise the
6216 ;; position of the initial [+-].
6217 ;;
6218 ;; This function might do hidden buffer changes.
6219 (save-excursion
6220 (beginning-of-line)
6221 (and c-opt-method-key
6222 (looking-at c-opt-method-key)
6223 (point))
6224 ))
6225
6226 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
6227 (defun c-in-gcc-asm-p ()
6228 ;; Return non-nil if point is within a gcc \"asm\" block.
6229 ;;
6230 ;; This should be called with point inside an argument list.
6231 ;;
6232 ;; Only one level of enclosing parentheses is considered, so for
6233 ;; instance `nil' is returned when in a function call within an asm
6234 ;; operand.
6235 ;;
6236 ;; This function might do hidden buffer changes.
6237
6238 (and c-opt-asm-stmt-key
6239 (save-excursion
6240 (beginning-of-line)
6241 (backward-up-list 1)
6242 (c-beginning-of-statement-1 (point-min) nil t)
6243 (looking-at c-opt-asm-stmt-key))))
6244
6245 (defun c-at-toplevel-p ()
6246 "Return a determination as to whether point is at the `top-level'.
6247 Being at the top-level means that point is either outside any
6248 enclosing block (such function definition), or only inside a class,
6249 namespace or other block that contains another declaration level.
6250
6251 If point is not at the top-level (e.g. it is inside a method
6252 definition), then nil is returned. Otherwise, if point is at a
6253 top-level not enclosed within a class definition, t is returned.
6254 Otherwise, a 2-vector is returned where the zeroth element is the
6255 buffer position of the start of the class declaration, and the first
6256 element is the buffer position of the enclosing class's opening
6257 brace.
6258
6259 Note that this function might do hidden buffer changes. See the
6260 comment at the start of cc-engine.el for more info."
6261 (let ((paren-state (c-parse-state)))
6262 (or (not (c-most-enclosing-brace paren-state))
6263 (c-search-uplist-for-classkey paren-state))))
6264
6265 (defun c-just-after-func-arglist-p (&optional lim)
6266 ;; Return non-nil if the point is in the region after the argument
6267 ;; list of a function and its opening brace (or semicolon in case it
6268 ;; got no body). If there are K&R style argument declarations in
6269 ;; that region, the point has to be inside the first one for this
6270 ;; function to recognize it.
6271 ;;
6272 ;; If successful, the point is moved to the first token after the
6273 ;; function header (see `c-forward-decl-or-cast-1' for details) and
6274 ;; the position of the opening paren of the function arglist is
6275 ;; returned.
6276 ;;
6277 ;; The point is clobbered if not successful.
6278 ;;
6279 ;; LIM is used as bound for backward buffer searches.
6280 ;;
6281 ;; This function might do hidden buffer changes.
6282
6283 (let ((beg (point)) end id-start)
6284 (and
6285 (eq (c-beginning-of-statement-1 lim) 'same)
6286
6287 (not (or (c-major-mode-is 'objc-mode)
6288 (c-forward-objc-directive)))
6289
6290 (setq id-start
6291 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
6292 (< id-start beg)
6293
6294 ;; There should not be a '=' or ',' between beg and the
6295 ;; start of the declaration since that means we were in the
6296 ;; "expression part" of the declaration.
6297 (or (> (point) beg)
6298 (not (looking-at "[=,]")))
6299
6300 (save-excursion
6301 ;; Check that there's an arglist paren in the
6302 ;; declaration.
6303 (goto-char id-start)
6304 (cond ((eq (char-after) ?\()
6305 ;; The declarator is a paren expression, so skip past it
6306 ;; so that we don't get stuck on that instead of the
6307 ;; function arglist.
6308 (c-forward-sexp))
6309 ((and c-opt-op-identifier-prefix
6310 (looking-at c-opt-op-identifier-prefix))
6311 ;; Don't trip up on "operator ()".
6312 (c-forward-token-2 2 t)))
6313 (and (< (point) beg)
6314 (c-syntactic-re-search-forward "(" beg t t)
6315 (1- (point)))))))
6316
6317 (defun c-in-knr-argdecl (&optional lim)
6318 ;; Return the position of the first argument declaration if point is
6319 ;; inside a K&R style argument declaration list, nil otherwise.
6320 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
6321 ;; position that bounds the backward search for the argument list.
6322 ;;
6323 ;; Point must be within a possible K&R region, e.g. just before a top-level
6324 ;; "{". It must be outside of parens and brackets. The test can return
6325 ;; false positives otherwise.
6326 ;;
6327 ;; This function might do hidden buffer changes.
6328
6329 (save-excursion
6330 (save-restriction
6331 ;; If we're in a macro, our search range is restricted to it. Narrow to
6332 ;; the searchable range.
6333 (let* ((macro-start (c-query-macro-start))
6334 (lim (max (or lim (point-min)) (or macro-start (point-min))))
6335 before-lparen after-rparen)
6336 (narrow-to-region lim (c-point 'eol))
6337
6338 ;; Search backwards for the defun's argument list. We give up if we
6339 ;; encounter a "}" (end of a previous defun) or BOB.
6340 ;;
6341 ;; The criterion for a paren structure being the arg list is:
6342 ;; o - there is non-WS stuff after it but before any "{"; AND
6343 ;; o - the token after it isn't a ";" AND
6344 ;; o - it is preceded by either an identifier (the function name) or
6345 ;; a macro expansion like "DEFUN (...)"; AND
6346 ;; o - its content is a non-empty comma-separated list of identifiers
6347 ;; (an empty arg list won't have a knr region).
6348 ;;
6349 ;; The following snippet illustrates these rules:
6350 ;; int foo (bar, baz, yuk)
6351 ;; int bar [] ;
6352 ;; int (*baz) (my_type) ;
6353 ;; int (*) (void) (*yuk) (void) ;
6354 ;; {
6355
6356 (catch 'knr
6357 (while t ; go round one paren/bracket construct each time round.
6358 (c-syntactic-skip-backward "^)]}")
6359 (cond ((eq (char-before) ?\))
6360 (setq after-rparen (point)))
6361 ((eq (char-before) ?\])
6362 (setq after-rparen nil))
6363 (t ; either } (hit previous defun) or no more parens/brackets
6364 (throw 'knr nil)))
6365
6366 (if after-rparen
6367 ;; We're inside a paren. Could it be our argument list....?
6368 (if
6369 (and
6370 (progn
6371 (goto-char after-rparen)
6372 (unless (c-go-list-backward) (throw 'knr nil)) ;
6373 ;; FIXME!!! What about macros between the parens? 2007/01/20
6374 (setq before-lparen (point)))
6375
6376 ;; It can't be the arg list if next token is ; or {
6377 (progn (goto-char after-rparen)
6378 (c-forward-syntactic-ws)
6379 (not (memq (char-after) '(?\; ?\{))))
6380
6381 ;; Is the thing preceding the list an identifier (the
6382 ;; function name), or a macro expansion?
6383 (progn
6384 (goto-char before-lparen)
6385 (eq (c-backward-token-2) 0)
6386 (or (c-on-identifier)
6387 (and (eq (char-after) ?\))
6388 (c-go-up-list-backward)
6389 (eq (c-backward-token-2) 0)
6390 (c-on-identifier))))
6391
6392 ;; Have we got a non-empty list of comma-separated
6393 ;; identifiers?
6394 (progn
6395 (goto-char before-lparen)
6396 (c-forward-token-2) ; to first token inside parens
6397 (and
6398 (c-on-identifier)
6399 (c-forward-token-2)
6400 (catch 'id-list
6401 (while (eq (char-after) ?\,)
6402 (c-forward-token-2)
6403 (unless (c-on-identifier) (throw 'id-list nil))
6404 (c-forward-token-2))
6405 (eq (char-after) ?\))))))
6406
6407 ;; ...Yes. We've identified the function's argument list.
6408 (throw 'knr
6409 (progn (goto-char after-rparen)
6410 (c-forward-syntactic-ws)
6411 (point)))
6412
6413 ;; ...No. The current parens aren't the function's arg list.
6414 (goto-char before-lparen))
6415
6416 (or (c-go-list-backward) ; backwards over [ .... ]
6417 (throw 'knr nil)))))))))
6418
6419 (defun c-skip-conditional ()
6420 ;; skip forward over conditional at point, including any predicate
6421 ;; statements in parentheses. No error checking is performed.
6422 ;;
6423 ;; This function might do hidden buffer changes.
6424 (c-forward-sexp (cond
6425 ;; else if()
6426 ((looking-at (concat "\\<else"
6427 "\\([ \t\n]\\|\\\\\n\\)+"
6428 "if\\>\\([^_]\\|$\\)"))
6429 3)
6430 ;; do, else, try, finally
6431 ((looking-at (concat "\\<\\("
6432 "do\\|else\\|try\\|finally"
6433 "\\)\\>\\([^_]\\|$\\)"))
6434 1)
6435 ;; for, if, while, switch, catch, synchronized, foreach
6436 (t 2))))
6437
6438 (defun c-after-conditional (&optional lim)
6439 ;; If looking at the token after a conditional then return the
6440 ;; position of its start, otherwise return nil.
6441 ;;
6442 ;; This function might do hidden buffer changes.
6443 (save-excursion
6444 (and (zerop (c-backward-token-2 1 t lim))
6445 (or (looking-at c-block-stmt-1-key)
6446 (and (eq (char-after) ?\()
6447 (zerop (c-backward-token-2 1 t lim))
6448 (looking-at c-block-stmt-2-key)))
6449 (point))))
6450
6451 (defun c-after-special-operator-id (&optional lim)
6452 ;; If the point is after an operator identifier that isn't handled
6453 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
6454 ;; position of the start of that identifier is returned. nil is
6455 ;; returned otherwise. The point may be anywhere in the syntactic
6456 ;; whitespace after the last token of the operator identifier.
6457 ;;
6458 ;; This function might do hidden buffer changes.
6459 (save-excursion
6460 (and c-overloadable-operators-regexp
6461 (zerop (c-backward-token-2 1 nil lim))
6462 (looking-at c-overloadable-operators-regexp)
6463 (or (not c-opt-op-identifier-prefix)
6464 (and
6465 (zerop (c-backward-token-2 1 nil lim))
6466 (looking-at c-opt-op-identifier-prefix)))
6467 (point))))
6468
6469 (defsubst c-backward-to-block-anchor (&optional lim)
6470 ;; Assuming point is at a brace that opens a statement block of some
6471 ;; kind, move to the proper anchor point for that block. It might
6472 ;; need to be adjusted further by c-add-stmt-syntax, but the
6473 ;; position at return is suitable as start position for that
6474 ;; function.
6475 ;;
6476 ;; This function might do hidden buffer changes.
6477 (unless (= (point) (c-point 'boi))
6478 (let ((start (c-after-conditional lim)))
6479 (if start
6480 (goto-char start)))))
6481
6482 (defsubst c-backward-to-decl-anchor (&optional lim)
6483 ;; Assuming point is at a brace that opens the block of a top level
6484 ;; declaration of some kind, move to the proper anchor point for
6485 ;; that block.
6486 ;;
6487 ;; This function might do hidden buffer changes.
6488 (unless (= (point) (c-point 'boi))
6489 (c-beginning-of-statement-1 lim)))
6490
6491 (defun c-search-decl-header-end ()
6492 ;; Search forward for the end of the "header" of the current
6493 ;; declaration. That's the position where the definition body
6494 ;; starts, or the first variable initializer, or the ending
6495 ;; semicolon. I.e. search forward for the closest following
6496 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
6497 ;; _after_ the first found token, or at point-max if none is found.
6498 ;;
6499 ;; This function might do hidden buffer changes.
6500
6501 (let ((base (point)))
6502 (if (c-major-mode-is 'c++-mode)
6503
6504 ;; In C++ we need to take special care to handle operator
6505 ;; tokens and those pesky template brackets.
6506 (while (and
6507 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
6508 (or
6509 (c-end-of-current-token base)
6510 ;; Handle operator identifiers, i.e. ignore any
6511 ;; operator token preceded by "operator".
6512 (save-excursion
6513 (and (c-safe (c-backward-sexp) t)
6514 (looking-at c-opt-op-identifier-prefix)))
6515 (and (eq (char-before) ?<)
6516 (c-with-syntax-table c++-template-syntax-table
6517 (if (c-safe (goto-char (c-up-list-forward (point))))
6518 t
6519 (goto-char (point-max))
6520 nil)))))
6521 (setq base (point)))
6522
6523 (while (and
6524 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
6525 (c-end-of-current-token base))
6526 (setq base (point))))))
6527
6528 (defun c-beginning-of-decl-1 (&optional lim)
6529 ;; Go to the beginning of the current declaration, or the beginning
6530 ;; of the previous one if already at the start of it. Point won't
6531 ;; be moved out of any surrounding paren. Return a cons cell of the
6532 ;; form (MOVE . KNR-POS). MOVE is like the return value from
6533 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
6534 ;; style argument declarations (and they are to be recognized) then
6535 ;; KNR-POS is set to the start of the first such argument
6536 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
6537 ;; position that bounds the backward search.
6538 ;;
6539 ;; NB: Cases where the declaration continues after the block, as in
6540 ;; "struct foo { ... } bar;", are currently recognized as two
6541 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
6542 ;;
6543 ;; This function might do hidden buffer changes.
6544 (catch 'return
6545 (let* ((start (point))
6546 (last-stmt-start (point))
6547 (move (c-beginning-of-statement-1 lim nil t)))
6548
6549 ;; `c-beginning-of-statement-1' stops at a block start, but we
6550 ;; want to continue if the block doesn't begin a top level
6551 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
6552 ;; or an open paren.
6553 (let ((beg (point)) tentative-move)
6554 ;; Go back one "statement" each time round the loop until we're just
6555 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
6556 ;; an ObjC method. This will move over a multiple declaration whose
6557 ;; components are comma separated.
6558 (while (and
6559 ;; Must check with c-opt-method-key in ObjC mode.
6560 (not (and c-opt-method-key
6561 (looking-at c-opt-method-key)))
6562 (/= last-stmt-start (point))
6563 (progn
6564 (c-backward-syntactic-ws lim)
6565 (not (memq (char-before) '(?\; ?} ?: nil))))
6566 (save-excursion
6567 (backward-char)
6568 (not (looking-at "\\s(")))
6569 ;; Check that we don't move from the first thing in a
6570 ;; macro to its header.
6571 (not (eq (setq tentative-move
6572 (c-beginning-of-statement-1 lim nil t))
6573 'macro)))
6574 (setq last-stmt-start beg
6575 beg (point)
6576 move tentative-move))
6577 (goto-char beg))
6578
6579 (when c-recognize-knr-p
6580 (let ((fallback-pos (point)) knr-argdecl-start)
6581 ;; Handle K&R argdecls. Back up after the "statement" jumped
6582 ;; over by `c-beginning-of-statement-1', unless it was the
6583 ;; function body, in which case we're sitting on the opening
6584 ;; brace now. Then test if we're in a K&R argdecl region and
6585 ;; that we started at the other side of the first argdecl in
6586 ;; it.
6587 (unless (eq (char-after) ?{)
6588 (goto-char last-stmt-start))
6589 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
6590 (< knr-argdecl-start start)
6591 (progn
6592 (goto-char knr-argdecl-start)
6593 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
6594 (throw 'return
6595 (cons (if (eq (char-after fallback-pos) ?{)
6596 'previous
6597 'same)
6598 knr-argdecl-start))
6599 (goto-char fallback-pos))))
6600
6601 ;; `c-beginning-of-statement-1' counts each brace block as a separate
6602 ;; statement, so the result will be 'previous if we've moved over any.
6603 ;; So change our result back to 'same if necessary.
6604 ;;
6605 ;; If they were brace list initializers we might not have moved over a
6606 ;; declaration boundary though, so change it to 'same if we've moved
6607 ;; past a '=' before '{', but not ';'. (This ought to be integrated
6608 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
6609 ;; potentially can search over a large amount of text.). Take special
6610 ;; pains not to get mislead by C++'s "operator=", and the like.
6611 (if (and (eq move 'previous)
6612 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
6613 c++-template-syntax-table
6614 (syntax-table))
6615 (save-excursion
6616 (and
6617 (progn
6618 (while ; keep going back to "[;={"s until we either find
6619 ; no more, or get to one which isn't an "operator ="
6620 (and (c-syntactic-re-search-forward "[;={]" start t t t)
6621 (eq (char-before) ?=)
6622 c-overloadable-operators-regexp
6623 c-opt-op-identifier-prefix
6624 (save-excursion
6625 (eq (c-backward-token-2) 0)
6626 (looking-at c-overloadable-operators-regexp)
6627 (eq (c-backward-token-2) 0)
6628 (looking-at c-opt-op-identifier-prefix))))
6629 (eq (char-before) ?=))
6630 (c-syntactic-re-search-forward "[;{]" start t t)
6631 (eq (char-before) ?{)
6632 (c-safe (goto-char (c-up-list-forward (point))) t)
6633 (not (c-syntactic-re-search-forward ";" start t t))))))
6634 (cons 'same nil)
6635 (cons move nil)))))
6636
6637 (defun c-end-of-decl-1 ()
6638 ;; Assuming point is at the start of a declaration (as detected by
6639 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
6640 ;; `c-beginning-of-decl-1', this function handles the case when a
6641 ;; block is followed by identifiers in e.g. struct declarations in C
6642 ;; or C++. If a proper end was found then t is returned, otherwise
6643 ;; point is moved as far as possible within the current sexp and nil
6644 ;; is returned. This function doesn't handle macros; use
6645 ;; `c-end-of-macro' instead in those cases.
6646 ;;
6647 ;; This function might do hidden buffer changes.
6648 (let ((start (point))
6649 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
6650 c++-template-syntax-table
6651 (syntax-table))))
6652 (catch 'return
6653 (c-search-decl-header-end)
6654
6655 (when (and c-recognize-knr-p
6656 (eq (char-before) ?\;)
6657 (c-in-knr-argdecl start))
6658 ;; Stopped at the ';' in a K&R argdecl section which is
6659 ;; detected using the same criteria as in
6660 ;; `c-beginning-of-decl-1'. Move to the following block
6661 ;; start.
6662 (c-syntactic-re-search-forward "{" nil 'move t))
6663
6664 (when (eq (char-before) ?{)
6665 ;; Encountered a block in the declaration. Jump over it.
6666 (condition-case nil
6667 (goto-char (c-up-list-forward (point)))
6668 (error (goto-char (point-max))
6669 (throw 'return nil)))
6670 (if (or (not c-opt-block-decls-with-vars-key)
6671 (save-excursion
6672 (c-with-syntax-table decl-syntax-table
6673 (let ((lim (point)))
6674 (goto-char start)
6675 (not (and
6676 ;; Check for `c-opt-block-decls-with-vars-key'
6677 ;; before the first paren.
6678 (c-syntactic-re-search-forward
6679 (concat "[;=\(\[{]\\|\\("
6680 c-opt-block-decls-with-vars-key
6681 "\\)")
6682 lim t t t)
6683 (match-beginning 1)
6684 (not (eq (char-before) ?_))
6685 ;; Check that the first following paren is
6686 ;; the block.
6687 (c-syntactic-re-search-forward "[;=\(\[{]"
6688 lim t t t)
6689 (eq (char-before) ?{)))))))
6690 ;; The declaration doesn't have any of the
6691 ;; `c-opt-block-decls-with-vars' keywords in the
6692 ;; beginning, so it ends here at the end of the block.
6693 (throw 'return t)))
6694
6695 (c-with-syntax-table decl-syntax-table
6696 (while (progn
6697 (if (eq (char-before) ?\;)
6698 (throw 'return t))
6699 (c-syntactic-re-search-forward ";" nil 'move t))))
6700 nil)))
6701
6702 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
6703 ;; Assuming the point is at an open brace, check if it starts a
6704 ;; block that contains another declaration level, i.e. that isn't a
6705 ;; statement block or a brace list, and if so return non-nil.
6706 ;;
6707 ;; If the check is successful, the return value is the start of the
6708 ;; keyword that tells what kind of construct it is, i.e. typically
6709 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
6710 ;; the point will be at the start of the construct, before any
6711 ;; leading specifiers, otherwise it's at the returned position.
6712 ;;
6713 ;; The point is clobbered if the check is unsuccessful.
6714 ;;
6715 ;; CONTAINING-SEXP is the position of the open of the surrounding
6716 ;; paren, or nil if none.
6717 ;;
6718 ;; The optional LIMIT limits the backward search for the start of
6719 ;; the construct. It's assumed to be at a syntactically relevant
6720 ;; position.
6721 ;;
6722 ;; If any template arglists are found in the searched region before
6723 ;; the open brace, they get marked with paren syntax.
6724 ;;
6725 ;; This function might do hidden buffer changes.
6726
6727 (let ((open-brace (point)) kwd-start first-specifier-pos)
6728 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6729
6730 (when (and c-recognize-<>-arglists
6731 (eq (char-before) ?>))
6732 ;; Could be at the end of a template arglist.
6733 (let ((c-parse-and-markup-<>-arglists t)
6734 (c-disallow-comma-in-<>-arglists
6735 (and containing-sexp
6736 (not (eq (char-after containing-sexp) ?{)))))
6737 (while (and
6738 (c-backward-<>-arglist nil limit)
6739 (progn
6740 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6741 (eq (char-before) ?>))))))
6742
6743 ;; Note: Can't get bogus hits inside template arglists below since they
6744 ;; have gotten paren syntax above.
6745 (when (and
6746 ;; If `goto-start' is set we begin by searching for the
6747 ;; first possible position of a leading specifier list.
6748 ;; The `c-decl-block-key' search continues from there since
6749 ;; we know it can't match earlier.
6750 (if goto-start
6751 (when (c-syntactic-re-search-forward c-symbol-start
6752 open-brace t t)
6753 (goto-char (setq first-specifier-pos (match-beginning 0)))
6754 t)
6755 t)
6756
6757 (cond
6758 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
6759 (goto-char (setq kwd-start (match-beginning 0)))
6760 (or
6761
6762 ;; Found a keyword that can't be a type?
6763 (match-beginning 1)
6764
6765 ;; Can be a type too, in which case it's the return type of a
6766 ;; function (under the assumption that no declaration level
6767 ;; block construct starts with a type).
6768 (not (c-forward-type))
6769
6770 ;; Jumped over a type, but it could be a declaration keyword
6771 ;; followed by the declared identifier that we've jumped over
6772 ;; instead (e.g. in "class Foo {"). If it indeed is a type
6773 ;; then we should be at the declarator now, so check for a
6774 ;; valid declarator start.
6775 ;;
6776 ;; Note: This doesn't cope with the case when a declared
6777 ;; identifier is followed by e.g. '(' in a language where '('
6778 ;; also might be part of a declarator expression. Currently
6779 ;; there's no such language.
6780 (not (or (looking-at c-symbol-start)
6781 (looking-at c-type-decl-prefix-key)))))
6782
6783 ;; In Pike a list of modifiers may be followed by a brace
6784 ;; to make them apply to many identifiers. Note that the
6785 ;; match data will be empty on return in this case.
6786 ((and (c-major-mode-is 'pike-mode)
6787 (progn
6788 (goto-char open-brace)
6789 (= (c-backward-token-2) 0))
6790 (looking-at c-specifier-key)
6791 ;; Use this variant to avoid yet another special regexp.
6792 (c-keyword-member (c-keyword-sym (match-string 1))
6793 'c-modifier-kwds))
6794 (setq kwd-start (point))
6795 t)))
6796
6797 ;; Got a match.
6798
6799 (if goto-start
6800 ;; Back up over any preceding specifiers and their clauses
6801 ;; by going forward from `first-specifier-pos', which is the
6802 ;; earliest possible position where the specifier list can
6803 ;; start.
6804 (progn
6805 (goto-char first-specifier-pos)
6806
6807 (while (< (point) kwd-start)
6808 (if (looking-at c-symbol-key)
6809 ;; Accept any plain symbol token on the ground that
6810 ;; it's a specifier masked through a macro (just
6811 ;; like `c-forward-decl-or-cast-1' skip forward over
6812 ;; such tokens).
6813 ;;
6814 ;; Could be more restrictive wrt invalid keywords,
6815 ;; but that'd only occur in invalid code so there's
6816 ;; no use spending effort on it.
6817 (let ((end (match-end 0)))
6818 (unless (c-forward-keyword-clause 0)
6819 (goto-char end)
6820 (c-forward-syntactic-ws)))
6821
6822 ;; Can't parse a declaration preamble and is still
6823 ;; before `kwd-start'. That means `first-specifier-pos'
6824 ;; was in some earlier construct. Search again.
6825 (if (c-syntactic-re-search-forward c-symbol-start
6826 kwd-start 'move t)
6827 (goto-char (setq first-specifier-pos (match-beginning 0)))
6828 ;; Got no preamble before the block declaration keyword.
6829 (setq first-specifier-pos kwd-start))))
6830
6831 (goto-char first-specifier-pos))
6832 (goto-char kwd-start))
6833
6834 kwd-start)))
6835
6836 (defun c-search-uplist-for-classkey (paren-state)
6837 ;; Check if the closest containing paren sexp is a declaration
6838 ;; block, returning a 2 element vector in that case. Aref 0
6839 ;; contains the bufpos at boi of the class key line, and aref 1
6840 ;; contains the bufpos of the open brace. This function is an
6841 ;; obsolete wrapper for `c-looking-at-decl-block'.
6842 ;;
6843 ;; This function might do hidden buffer changes.
6844 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
6845 (when open-paren-pos
6846 (save-excursion
6847 (goto-char open-paren-pos)
6848 (when (and (eq (char-after) ?{)
6849 (c-looking-at-decl-block
6850 (c-safe-position open-paren-pos paren-state)
6851 nil))
6852 (back-to-indentation)
6853 (vector (point) open-paren-pos))))))
6854
6855 (defun c-inside-bracelist-p (containing-sexp paren-state)
6856 ;; return the buffer position of the beginning of the brace list
6857 ;; statement if we're inside a brace list, otherwise return nil.
6858 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
6859 ;; paren. PAREN-STATE is the remainder of the state of enclosing
6860 ;; braces
6861 ;;
6862 ;; N.B.: This algorithm can potentially get confused by cpp macros
6863 ;; placed in inconvenient locations. It's a trade-off we make for
6864 ;; speed.
6865 ;;
6866 ;; This function might do hidden buffer changes.
6867 (or
6868 ;; This will pick up brace list declarations.
6869 (c-safe
6870 (save-excursion
6871 (goto-char containing-sexp)
6872 (c-forward-sexp -1)
6873 (let (bracepos)
6874 (if (and (or (looking-at c-brace-list-key)
6875 (progn (c-forward-sexp -1)
6876 (looking-at c-brace-list-key)))
6877 (setq bracepos (c-down-list-forward (point)))
6878 (not (c-crosses-statement-barrier-p (point)
6879 (- bracepos 2))))
6880 (point)))))
6881 ;; this will pick up array/aggregate init lists, even if they are nested.
6882 (save-excursion
6883 (let ((class-key
6884 ;; Pike can have class definitions anywhere, so we must
6885 ;; check for the class key here.
6886 (and (c-major-mode-is 'pike-mode)
6887 c-decl-block-key))
6888 bufpos braceassignp lim next-containing)
6889 (while (and (not bufpos)
6890 containing-sexp)
6891 (when paren-state
6892 (if (consp (car paren-state))
6893 (setq lim (cdr (car paren-state))
6894 paren-state (cdr paren-state))
6895 (setq lim (car paren-state)))
6896 (when paren-state
6897 (setq next-containing (car paren-state)
6898 paren-state (cdr paren-state))))
6899 (goto-char containing-sexp)
6900 (if (c-looking-at-inexpr-block next-containing next-containing)
6901 ;; We're in an in-expression block of some kind. Do not
6902 ;; check nesting. We deliberately set the limit to the
6903 ;; containing sexp, so that c-looking-at-inexpr-block
6904 ;; doesn't check for an identifier before it.
6905 (setq containing-sexp nil)
6906 ;; see if the open brace is preceded by = or [...] in
6907 ;; this statement, but watch out for operator=
6908 (setq braceassignp 'dontknow)
6909 (c-backward-token-2 1 t lim)
6910 ;; Checks to do only on the first sexp before the brace.
6911 (when (and c-opt-inexpr-brace-list-key
6912 (eq (char-after) ?\[))
6913 ;; In Java, an initialization brace list may follow
6914 ;; directly after "new Foo[]", so check for a "new"
6915 ;; earlier.
6916 (while (eq braceassignp 'dontknow)
6917 (setq braceassignp
6918 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
6919 ((looking-at c-opt-inexpr-brace-list-key) t)
6920 ((looking-at "\\sw\\|\\s_\\|[.[]")
6921 ;; Carry on looking if this is an
6922 ;; identifier (may contain "." in Java)
6923 ;; or another "[]" sexp.
6924 'dontknow)
6925 (t nil)))))
6926 ;; Checks to do on all sexps before the brace, up to the
6927 ;; beginning of the statement.
6928 (while (eq braceassignp 'dontknow)
6929 (cond ((eq (char-after) ?\;)
6930 (setq braceassignp nil))
6931 ((and class-key
6932 (looking-at class-key))
6933 (setq braceassignp nil))
6934 ((eq (char-after) ?=)
6935 ;; We've seen a =, but must check earlier tokens so
6936 ;; that it isn't something that should be ignored.
6937 (setq braceassignp 'maybe)
6938 (while (and (eq braceassignp 'maybe)
6939 (zerop (c-backward-token-2 1 t lim)))
6940 (setq braceassignp
6941 (cond
6942 ;; Check for operator =
6943 ((and c-opt-op-identifier-prefix
6944 (looking-at c-opt-op-identifier-prefix))
6945 nil)
6946 ;; Check for `<opchar>= in Pike.
6947 ((and (c-major-mode-is 'pike-mode)
6948 (or (eq (char-after) ?`)
6949 ;; Special case for Pikes
6950 ;; `[]=, since '[' is not in
6951 ;; the punctuation class.
6952 (and (eq (char-after) ?\[)
6953 (eq (char-before) ?`))))
6954 nil)
6955 ((looking-at "\\s.") 'maybe)
6956 ;; make sure we're not in a C++ template
6957 ;; argument assignment
6958 ((and
6959 (c-major-mode-is 'c++-mode)
6960 (save-excursion
6961 (let ((here (point))
6962 (pos< (progn
6963 (skip-chars-backward "^<>")
6964 (point))))
6965 (and (eq (char-before) ?<)
6966 (not (c-crosses-statement-barrier-p
6967 pos< here))
6968 (not (c-in-literal))
6969 ))))
6970 nil)
6971 (t t))))))
6972 (if (and (eq braceassignp 'dontknow)
6973 (/= (c-backward-token-2 1 t lim) 0))
6974 (setq braceassignp nil)))
6975 (if (not braceassignp)
6976 (if (eq (char-after) ?\;)
6977 ;; Brace lists can't contain a semicolon, so we're done.
6978 (setq containing-sexp nil)
6979 ;; Go up one level.
6980 (setq containing-sexp next-containing
6981 lim nil
6982 next-containing nil))
6983 ;; we've hit the beginning of the aggregate list
6984 (c-beginning-of-statement-1
6985 (c-most-enclosing-brace paren-state))
6986 (setq bufpos (point))))
6987 )
6988 bufpos))
6989 ))
6990
6991 (defun c-looking-at-special-brace-list (&optional lim)
6992 ;; If we're looking at the start of a pike-style list, ie `({ })',
6993 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
6994 ;; positions and its entry in c-special-brace-lists is returned, nil
6995 ;; otherwise. The ending position is nil if the list is still open.
6996 ;; LIM is the limit for forward search. The point may either be at
6997 ;; the `(' or at the following paren character. Tries to check the
6998 ;; matching closer, but assumes it's correct if no balanced paren is
6999 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
7000 ;; a special brace list).
7001 ;;
7002 ;; This function might do hidden buffer changes.
7003 (if c-special-brace-lists
7004 (condition-case ()
7005 (save-excursion
7006 (let ((beg (point))
7007 inner-beg end type)
7008 (c-forward-syntactic-ws)
7009 (if (eq (char-after) ?\()
7010 (progn
7011 (forward-char 1)
7012 (c-forward-syntactic-ws)
7013 (setq inner-beg (point))
7014 (setq type (assq (char-after) c-special-brace-lists)))
7015 (if (setq type (assq (char-after) c-special-brace-lists))
7016 (progn
7017 (setq inner-beg (point))
7018 (c-backward-syntactic-ws)
7019 (forward-char -1)
7020 (setq beg (if (eq (char-after) ?\()
7021 (point)
7022 nil)))))
7023 (if (and beg type)
7024 (if (and (c-safe
7025 (goto-char beg)
7026 (c-forward-sexp 1)
7027 (setq end (point))
7028 (= (char-before) ?\)))
7029 (c-safe
7030 (goto-char inner-beg)
7031 (if (looking-at "\\s(")
7032 ;; Check balancing of the inner paren
7033 ;; below.
7034 (progn
7035 (c-forward-sexp 1)
7036 t)
7037 ;; If the inner char isn't a paren then
7038 ;; we can't check balancing, so just
7039 ;; check the char before the outer
7040 ;; closing paren.
7041 (goto-char end)
7042 (backward-char)
7043 (c-backward-syntactic-ws)
7044 (= (char-before) (cdr type)))))
7045 (if (or (/= (char-syntax (char-before)) ?\))
7046 (= (progn
7047 (c-forward-syntactic-ws)
7048 (point))
7049 (1- end)))
7050 (cons (cons beg end) type))
7051 (cons (list beg) type)))))
7052 (error nil))))
7053
7054 (defun c-looking-at-bos (&optional lim)
7055 ;; Return non-nil if between two statements or declarations, assuming
7056 ;; point is not inside a literal or comment.
7057 ;;
7058 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
7059 ;; are recommended instead.
7060 ;;
7061 ;; This function might do hidden buffer changes.
7062 (c-at-statement-start-p))
7063 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p)
7064
7065 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
7066 ;; Return non-nil if we're looking at the beginning of a block
7067 ;; inside an expression. The value returned is actually a cons of
7068 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
7069 ;; position of the beginning of the construct.
7070 ;;
7071 ;; LIM limits the backward search. CONTAINING-SEXP is the start
7072 ;; position of the closest containing list. If it's nil, the
7073 ;; containing paren isn't used to decide whether we're inside an
7074 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
7075 ;; needs to be farther back.
7076 ;;
7077 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
7078 ;; brace block might be done. It should only be used when the
7079 ;; construct can be assumed to be complete, i.e. when the original
7080 ;; starting position was further down than that.
7081 ;;
7082 ;; This function might do hidden buffer changes.
7083
7084 (save-excursion
7085 (let ((res 'maybe) passed-paren
7086 (closest-lim (or containing-sexp lim (point-min)))
7087 ;; Look at the character after point only as a last resort
7088 ;; when we can't disambiguate.
7089 (block-follows (and (eq (char-after) ?{) (point))))
7090
7091 (while (and (eq res 'maybe)
7092 (progn (c-backward-syntactic-ws)
7093 (> (point) closest-lim))
7094 (not (bobp))
7095 (progn (backward-char)
7096 (looking-at "[\]\).]\\|\\w\\|\\s_"))
7097 (c-safe (forward-char)
7098 (goto-char (scan-sexps (point) -1))))
7099
7100 (setq res
7101 (if (looking-at c-keywords-regexp)
7102 (let ((kw-sym (c-keyword-sym (match-string 1))))
7103 (cond
7104 ((and block-follows
7105 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
7106 (and (not (eq passed-paren ?\[))
7107 (or (not (looking-at c-class-key))
7108 ;; If the class definition is at the start of
7109 ;; a statement, we don't consider it an
7110 ;; in-expression class.
7111 (let ((prev (point)))
7112 (while (and
7113 (= (c-backward-token-2 1 nil closest-lim) 0)
7114 (eq (char-syntax (char-after)) ?w))
7115 (setq prev (point)))
7116 (goto-char prev)
7117 (not (c-at-statement-start-p)))
7118 ;; Also, in Pike we treat it as an
7119 ;; in-expression class if it's used in an
7120 ;; object clone expression.
7121 (save-excursion
7122 (and check-at-end
7123 (c-major-mode-is 'pike-mode)
7124 (progn (goto-char block-follows)
7125 (zerop (c-forward-token-2 1 t)))
7126 (eq (char-after) ?\())))
7127 (cons 'inexpr-class (point))))
7128 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
7129 (when (not passed-paren)
7130 (cons 'inexpr-statement (point))))
7131 ((c-keyword-member kw-sym 'c-lambda-kwds)
7132 (when (or (not passed-paren)
7133 (eq passed-paren ?\())
7134 (cons 'inlambda (point))))
7135 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
7136 nil)
7137 (t
7138 'maybe)))
7139
7140 (if (looking-at "\\s(")
7141 (if passed-paren
7142 (if (and (eq passed-paren ?\[)
7143 (eq (char-after) ?\[))
7144 ;; Accept several square bracket sexps for
7145 ;; Java array initializations.
7146 'maybe)
7147 (setq passed-paren (char-after))
7148 'maybe)
7149 'maybe))))
7150
7151 (if (eq res 'maybe)
7152 (when (and c-recognize-paren-inexpr-blocks
7153 block-follows
7154 containing-sexp
7155 (eq (char-after containing-sexp) ?\())
7156 (goto-char containing-sexp)
7157 (if (or (save-excursion
7158 (c-backward-syntactic-ws lim)
7159 (and (> (point) (or lim (point-min)))
7160 (c-on-identifier)))
7161 (and c-special-brace-lists
7162 (c-looking-at-special-brace-list)))
7163 nil
7164 (cons 'inexpr-statement (point))))
7165
7166 res))))
7167
7168 (defun c-looking-at-inexpr-block-backward (paren-state)
7169 ;; Returns non-nil if we're looking at the end of an in-expression
7170 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
7171 ;; PAREN-STATE is the paren state relevant at the current position.
7172 ;;
7173 ;; This function might do hidden buffer changes.
7174 (save-excursion
7175 ;; We currently only recognize a block.
7176 (let ((here (point))
7177 (elem (car-safe paren-state))
7178 containing-sexp)
7179 (when (and (consp elem)
7180 (progn (goto-char (cdr elem))
7181 (c-forward-syntactic-ws here)
7182 (= (point) here)))
7183 (goto-char (car elem))
7184 (if (setq paren-state (cdr paren-state))
7185 (setq containing-sexp (car-safe paren-state)))
7186 (c-looking-at-inexpr-block (c-safe-position containing-sexp
7187 paren-state)
7188 containing-sexp)))))
7189
7190 \f
7191 ;; `c-guess-basic-syntax' and the functions that precedes it below
7192 ;; implements the main decision tree for determining the syntactic
7193 ;; analysis of the current line of code.
7194
7195 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
7196 ;; auto newline analysis.
7197 (defvar c-auto-newline-analysis nil)
7198
7199 (defun c-brace-anchor-point (bracepos)
7200 ;; BRACEPOS is the position of a brace in a construct like "namespace
7201 ;; Bar {". Return the anchor point in this construct; this is the
7202 ;; earliest symbol on the brace's line which isn't earlier than
7203 ;; "namespace".
7204 ;;
7205 ;; Currently (2007-08-17), "like namespace" means "matches
7206 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
7207 ;; or anything like that.
7208 (save-excursion
7209 (let ((boi (c-point 'boi bracepos)))
7210 (goto-char bracepos)
7211 (while (and (> (point) boi)
7212 (not (looking-at c-other-decl-block-key)))
7213 (c-backward-token-2))
7214 (if (> (point) boi) (point) boi))))
7215
7216 (defsubst c-add-syntax (symbol &rest args)
7217 ;; A simple function to prepend a new syntax element to
7218 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
7219 ;; should always be dynamically bound but since we read it first
7220 ;; we'll fail properly anyway if this function is misused.
7221 (setq c-syntactic-context (cons (cons symbol args)
7222 c-syntactic-context)))
7223
7224 (defsubst c-append-syntax (symbol &rest args)
7225 ;; Like `c-add-syntax' but appends to the end of the syntax list.
7226 ;; (Normally not necessary.)
7227 (setq c-syntactic-context (nconc c-syntactic-context
7228 (list (cons symbol args)))))
7229
7230 (defun c-add-stmt-syntax (syntax-symbol
7231 syntax-extra-args
7232 stop-at-boi-only
7233 containing-sexp
7234 paren-state)
7235 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
7236 ;; needed with further syntax elements of the types `substatement',
7237 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
7238 ;; `defun-block-intro'.
7239 ;;
7240 ;; Do the generic processing to anchor the given syntax symbol on
7241 ;; the preceding statement: Skip over any labels and containing
7242 ;; statements on the same line, and then search backward until we
7243 ;; find a statement or block start that begins at boi without a
7244 ;; label or comment.
7245 ;;
7246 ;; Point is assumed to be at the prospective anchor point for the
7247 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
7248 ;; skip past open parens and containing statements. Most of the added
7249 ;; syntax elements will get the same anchor point - the exception is
7250 ;; for an anchor in a construct like "namespace"[*] - this is as early
7251 ;; as possible in the construct but on the same line as the {.
7252 ;;
7253 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
7254 ;;
7255 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
7256 ;; syntax symbol. They are appended after the anchor point.
7257 ;;
7258 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
7259 ;; if the current statement starts there.
7260 ;;
7261 ;; Note: It's not a problem if PAREN-STATE "overshoots"
7262 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
7263 ;;
7264 ;; This function might do hidden buffer changes.
7265
7266 (if (= (point) (c-point 'boi))
7267 ;; This is by far the most common case, so let's give it special
7268 ;; treatment.
7269 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
7270
7271 (let ((syntax-last c-syntactic-context)
7272 (boi (c-point 'boi))
7273 ;; Set when we're on a label, so that we don't stop there.
7274 ;; FIXME: To be complete we should check if we're on a label
7275 ;; now at the start.
7276 on-label)
7277
7278 ;; Use point as the anchor point for "namespace", "extern", etc.
7279 (apply 'c-add-syntax syntax-symbol
7280 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
7281 (point) nil)
7282 syntax-extra-args)
7283
7284 ;; Loop while we have to back out of containing blocks.
7285 (while
7286 (and
7287 (catch 'back-up-block
7288
7289 ;; Loop while we have to back up statements.
7290 (while (or (/= (point) boi)
7291 on-label
7292 (looking-at c-comment-start-regexp))
7293
7294 ;; Skip past any comments that stands between the
7295 ;; statement start and boi.
7296 (let ((savepos (point)))
7297 (while (and (/= savepos boi)
7298 (c-backward-single-comment))
7299 (setq savepos (point)
7300 boi (c-point 'boi)))
7301 (goto-char savepos))
7302
7303 ;; Skip to the beginning of this statement or backward
7304 ;; another one.
7305 (let ((old-pos (point))
7306 (old-boi boi)
7307 (step-type (c-beginning-of-statement-1 containing-sexp)))
7308 (setq boi (c-point 'boi)
7309 on-label (eq step-type 'label))
7310
7311 (cond ((= (point) old-pos)
7312 ;; If we didn't move we're at the start of a block and
7313 ;; have to continue outside it.
7314 (throw 'back-up-block t))
7315
7316 ((and (eq step-type 'up)
7317 (>= (point) old-boi)
7318 (looking-at "else\\>[^_]")
7319 (save-excursion
7320 (goto-char old-pos)
7321 (looking-at "if\\>[^_]")))
7322 ;; Special case to avoid deeper and deeper indentation
7323 ;; of "else if" clauses.
7324 )
7325
7326 ((and (not stop-at-boi-only)
7327 (/= old-pos old-boi)
7328 (memq step-type '(up previous)))
7329 ;; If stop-at-boi-only is nil, we shouldn't back up
7330 ;; over previous or containing statements to try to
7331 ;; reach boi, so go back to the last position and
7332 ;; exit.
7333 (goto-char old-pos)
7334 (throw 'back-up-block nil))
7335
7336 (t
7337 (if (and (not stop-at-boi-only)
7338 (memq step-type '(up previous beginning)))
7339 ;; If we've moved into another statement then we
7340 ;; should no longer try to stop in the middle of a
7341 ;; line.
7342 (setq stop-at-boi-only t))
7343
7344 ;; Record this as a substatement if we skipped up one
7345 ;; level.
7346 (when (eq step-type 'up)
7347 (c-add-syntax 'substatement nil))))
7348 )))
7349
7350 containing-sexp)
7351
7352 ;; Now we have to go out of this block.
7353 (goto-char containing-sexp)
7354
7355 ;; Don't stop in the middle of a special brace list opener
7356 ;; like "({".
7357 (when c-special-brace-lists
7358 (let ((special-list (c-looking-at-special-brace-list)))
7359 (when (and special-list
7360 (< (car (car special-list)) (point)))
7361 (setq containing-sexp (car (car special-list)))
7362 (goto-char containing-sexp))))
7363
7364 (setq paren-state (c-whack-state-after containing-sexp paren-state)
7365 containing-sexp (c-most-enclosing-brace paren-state)
7366 boi (c-point 'boi))
7367
7368 ;; Analyze the construct in front of the block we've stepped out
7369 ;; from and add the right syntactic element for it.
7370 (let ((paren-pos (point))
7371 (paren-char (char-after))
7372 step-type)
7373
7374 (if (eq paren-char ?\()
7375 ;; Stepped out of a parenthesis block, so we're in an
7376 ;; expression now.
7377 (progn
7378 (when (/= paren-pos boi)
7379 (if (and c-recognize-paren-inexpr-blocks
7380 (progn
7381 (c-backward-syntactic-ws containing-sexp)
7382 (or (not (looking-at "\\>"))
7383 (not (c-on-identifier))))
7384 (save-excursion
7385 (goto-char (1+ paren-pos))
7386 (c-forward-syntactic-ws)
7387 (eq (char-after) ?{)))
7388 ;; Stepped out of an in-expression statement. This
7389 ;; syntactic element won't get an anchor pos.
7390 (c-add-syntax 'inexpr-statement)
7391
7392 ;; A parenthesis normally belongs to an arglist.
7393 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
7394
7395 (goto-char (max boi
7396 (if containing-sexp
7397 (1+ containing-sexp)
7398 (point-min))))
7399 (setq step-type 'same
7400 on-label nil))
7401
7402 ;; Stepped out of a brace block.
7403 (setq step-type (c-beginning-of-statement-1 containing-sexp)
7404 on-label (eq step-type 'label))
7405
7406 (if (and (eq step-type 'same)
7407 (/= paren-pos (point)))
7408 (let (inexpr)
7409 (cond
7410 ((save-excursion
7411 (goto-char paren-pos)
7412 (setq inexpr (c-looking-at-inexpr-block
7413 (c-safe-position containing-sexp paren-state)
7414 containing-sexp)))
7415 (c-add-syntax (if (eq (car inexpr) 'inlambda)
7416 'defun-block-intro
7417 'statement-block-intro)
7418 nil))
7419 ((looking-at c-other-decl-block-key)
7420 (c-add-syntax
7421 (cdr (assoc (match-string 1)
7422 c-other-decl-block-key-in-symbols-alist))
7423 (max (c-point 'boi paren-pos) (point))))
7424 (t (c-add-syntax 'defun-block-intro nil))))
7425
7426 (c-add-syntax 'statement-block-intro nil)))
7427
7428 (if (= paren-pos boi)
7429 ;; Always done if the open brace was at boi. The
7430 ;; c-beginning-of-statement-1 call above is necessary
7431 ;; anyway, to decide the type of block-intro to add.
7432 (goto-char paren-pos)
7433 (setq boi (c-point 'boi)))
7434 ))
7435
7436 ;; Fill in the current point as the anchor for all the symbols
7437 ;; added above.
7438 (let ((p c-syntactic-context) q)
7439 (while (not (eq p syntax-last))
7440 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
7441 (while q
7442 (unless (car q)
7443 (setcar q (point)))
7444 (setq q (cdr q)))
7445 (setq p (cdr p))))
7446 )))
7447
7448 (defun c-add-class-syntax (symbol
7449 containing-decl-open
7450 containing-decl-start
7451 containing-decl-kwd
7452 paren-state)
7453 ;; The inclass and class-close syntactic symbols are added in
7454 ;; several places and some work is needed to fix everything.
7455 ;; Therefore it's collected here.
7456 ;;
7457 ;; This function might do hidden buffer changes.
7458 (goto-char containing-decl-open)
7459 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
7460 (progn
7461 (c-add-syntax symbol containing-decl-open)
7462 containing-decl-open)
7463 (goto-char containing-decl-start)
7464 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
7465 ;; here, but we have to do like this for compatibility.
7466 (back-to-indentation)
7467 (c-add-syntax symbol (point))
7468 (if (and (c-keyword-member containing-decl-kwd
7469 'c-inexpr-class-kwds)
7470 (/= containing-decl-start (c-point 'boi containing-decl-start)))
7471 (c-add-syntax 'inexpr-class))
7472 (point)))
7473
7474 (defun c-guess-continued-construct (indent-point
7475 char-after-ip
7476 beg-of-same-or-containing-stmt
7477 containing-sexp
7478 paren-state)
7479 ;; This function contains the decision tree reached through both
7480 ;; cases 18 and 10. It's a continued statement or top level
7481 ;; construct of some kind.
7482 ;;
7483 ;; This function might do hidden buffer changes.
7484
7485 (let (special-brace-list)
7486 (goto-char indent-point)
7487 (skip-chars-forward " \t")
7488
7489 (cond
7490 ;; (CASE A removed.)
7491 ;; CASE B: open braces for class or brace-lists
7492 ((setq special-brace-list
7493 (or (and c-special-brace-lists
7494 (c-looking-at-special-brace-list))
7495 (eq char-after-ip ?{)))
7496
7497 (cond
7498 ;; CASE B.1: class-open
7499 ((save-excursion
7500 (and (eq (char-after) ?{)
7501 (c-looking-at-decl-block containing-sexp t)
7502 (setq beg-of-same-or-containing-stmt (point))))
7503 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
7504
7505 ;; CASE B.2: brace-list-open
7506 ((or (consp special-brace-list)
7507 (save-excursion
7508 (goto-char beg-of-same-or-containing-stmt)
7509 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
7510 indent-point t t t)))
7511 ;; The most semantically accurate symbol here is
7512 ;; brace-list-open, but we normally report it simply as a
7513 ;; statement-cont. The reason is that one normally adjusts
7514 ;; brace-list-open for brace lists as top-level constructs,
7515 ;; and brace lists inside statements is a completely different
7516 ;; context. C.f. case 5A.3.
7517 (c-beginning-of-statement-1 containing-sexp)
7518 (c-add-stmt-syntax (if c-auto-newline-analysis
7519 ;; Turn off the dwim above when we're
7520 ;; analyzing the nature of the brace
7521 ;; for the auto newline feature.
7522 'brace-list-open
7523 'statement-cont)
7524 nil nil
7525 containing-sexp paren-state))
7526
7527 ;; CASE B.3: The body of a function declared inside a normal
7528 ;; block. Can occur e.g. in Pike and when using gcc
7529 ;; extensions, but watch out for macros followed by blocks.
7530 ;; C.f. cases E, 16F and 17G.
7531 ((and (not (c-at-statement-start-p))
7532 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7533 'same)
7534 (save-excursion
7535 (let ((c-recognize-typeless-decls nil))
7536 ;; Turn off recognition of constructs that lacks a
7537 ;; type in this case, since that's more likely to be
7538 ;; a macro followed by a block.
7539 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7540 (c-add-stmt-syntax 'defun-open nil t
7541 containing-sexp paren-state))
7542
7543 ;; CASE B.4: Continued statement with block open. The most
7544 ;; accurate analysis is perhaps `statement-cont' together with
7545 ;; `block-open' but we play DWIM and use `substatement-open'
7546 ;; instead. The rationaly is that this typically is a macro
7547 ;; followed by a block which makes it very similar to a
7548 ;; statement with a substatement block.
7549 (t
7550 (c-add-stmt-syntax 'substatement-open nil nil
7551 containing-sexp paren-state))
7552 ))
7553
7554 ;; CASE C: iostream insertion or extraction operator
7555 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
7556 (save-excursion
7557 (goto-char beg-of-same-or-containing-stmt)
7558 ;; If there is no preceding streamop in the statement
7559 ;; then indent this line as a normal statement-cont.
7560 (when (c-syntactic-re-search-forward
7561 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
7562 (c-add-syntax 'stream-op (c-point 'boi))
7563 t))))
7564
7565 ;; CASE E: In the "K&R region" of a function declared inside a
7566 ;; normal block. C.f. case B.3.
7567 ((and (save-excursion
7568 ;; Check that the next token is a '{'. This works as
7569 ;; long as no language that allows nested function
7570 ;; definitions allows stuff like member init lists, K&R
7571 ;; declarations or throws clauses there.
7572 ;;
7573 ;; Note that we do a forward search for something ahead
7574 ;; of the indentation line here. That's not good since
7575 ;; the user might not have typed it yet. Unfortunately
7576 ;; it's exceedingly tricky to recognize a function
7577 ;; prototype in a code block without resorting to this.
7578 (c-forward-syntactic-ws)
7579 (eq (char-after) ?{))
7580 (not (c-at-statement-start-p))
7581 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7582 'same)
7583 (save-excursion
7584 (let ((c-recognize-typeless-decls nil))
7585 ;; Turn off recognition of constructs that lacks a
7586 ;; type in this case, since that's more likely to be
7587 ;; a macro followed by a block.
7588 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7589 (c-add-stmt-syntax 'func-decl-cont nil t
7590 containing-sexp paren-state))
7591
7592 ;; CASE D: continued statement.
7593 (t
7594 (c-beginning-of-statement-1 containing-sexp)
7595 (c-add-stmt-syntax 'statement-cont nil nil
7596 containing-sexp paren-state))
7597 )))
7598
7599 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
7600 ;; 2005/11/29).
7601 ;;;###autoload
7602 (defun c-guess-basic-syntax ()
7603 "Return the syntactic context of the current line."
7604 (save-excursion
7605 (beginning-of-line)
7606 (c-save-buffer-state
7607 ((indent-point (point))
7608 (case-fold-search nil)
7609 ;; A whole ugly bunch of various temporary variables. Have
7610 ;; to declare them here since it's not possible to declare
7611 ;; a variable with only the scope of a cond test and the
7612 ;; following result clauses, and most of this function is a
7613 ;; single gigantic cond. :P
7614 literal char-before-ip before-ws-ip char-after-ip macro-start
7615 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
7616 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
7617 ;; The following record some positions for the containing
7618 ;; declaration block if we're directly within one:
7619 ;; `containing-decl-open' is the position of the open
7620 ;; brace. `containing-decl-start' is the start of the
7621 ;; declaration. `containing-decl-kwd' is the keyword
7622 ;; symbol of the keyword that tells what kind of block it
7623 ;; is.
7624 containing-decl-open
7625 containing-decl-start
7626 containing-decl-kwd
7627 ;; The open paren of the closest surrounding sexp or nil if
7628 ;; there is none.
7629 containing-sexp
7630 ;; The position after the closest preceding brace sexp
7631 ;; (nested sexps are ignored), or the position after
7632 ;; `containing-sexp' if there is none, or (point-min) if
7633 ;; `containing-sexp' is nil.
7634 lim
7635 ;; The paren state outside `containing-sexp', or at
7636 ;; `indent-point' if `containing-sexp' is nil.
7637 (paren-state (c-parse-state))
7638 ;; There's always at most one syntactic element which got
7639 ;; an anchor pos. It's stored in syntactic-relpos.
7640 syntactic-relpos
7641 (c-stmt-delim-chars c-stmt-delim-chars))
7642
7643 ;; Check if we're directly inside an enclosing declaration
7644 ;; level block.
7645 (when (and (setq containing-sexp
7646 (c-most-enclosing-brace paren-state))
7647 (progn
7648 (goto-char containing-sexp)
7649 (eq (char-after) ?{))
7650 (setq placeholder
7651 (c-looking-at-decl-block
7652 (c-most-enclosing-brace paren-state
7653 containing-sexp)
7654 t)))
7655 (setq containing-decl-open containing-sexp
7656 containing-decl-start (point)
7657 containing-sexp nil)
7658 (goto-char placeholder)
7659 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
7660 (c-keyword-sym (match-string 1)))))
7661
7662 ;; Init some position variables.
7663 (if c-state-cache
7664 (progn
7665 (setq containing-sexp (car paren-state)
7666 paren-state (cdr paren-state))
7667 (if (consp containing-sexp)
7668 (progn
7669 (setq lim (cdr containing-sexp))
7670 (if (cdr c-state-cache)
7671 ;; Ignore balanced paren. The next entry
7672 ;; can't be another one.
7673 (setq containing-sexp (car (cdr c-state-cache))
7674 paren-state (cdr paren-state))
7675 ;; If there is no surrounding open paren then
7676 ;; put the last balanced pair back on paren-state.
7677 (setq paren-state (cons containing-sexp paren-state)
7678 containing-sexp nil)))
7679 (setq lim (1+ containing-sexp))))
7680 (setq lim (point-min)))
7681
7682 ;; If we're in a parenthesis list then ',' delimits the
7683 ;; "statements" rather than being an operator (with the
7684 ;; exception of the "for" clause). This difference is
7685 ;; typically only noticeable when statements are used in macro
7686 ;; arglists.
7687 (when (and containing-sexp
7688 (eq (char-after containing-sexp) ?\())
7689 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
7690
7691 ;; cache char before and after indent point, and move point to
7692 ;; the most likely position to perform the majority of tests
7693 (goto-char indent-point)
7694 (c-backward-syntactic-ws lim)
7695 (setq before-ws-ip (point)
7696 char-before-ip (char-before))
7697 (goto-char indent-point)
7698 (skip-chars-forward " \t")
7699 (setq char-after-ip (char-after))
7700
7701 ;; are we in a literal?
7702 (setq literal (c-in-literal lim))
7703
7704 ;; now figure out syntactic qualities of the current line
7705 (cond
7706
7707 ;; CASE 1: in a string.
7708 ((eq literal 'string)
7709 (c-add-syntax 'string (c-point 'bopl)))
7710
7711 ;; CASE 2: in a C or C++ style comment.
7712 ((and (memq literal '(c c++))
7713 ;; This is a kludge for XEmacs where we use
7714 ;; `buffer-syntactic-context', which doesn't correctly
7715 ;; recognize "\*/" to end a block comment.
7716 ;; `parse-partial-sexp' which is used by
7717 ;; `c-literal-limits' will however do that in most
7718 ;; versions, which results in that we get nil from
7719 ;; `c-literal-limits' even when `c-in-literal' claims
7720 ;; we're inside a comment.
7721 (setq placeholder (c-literal-limits lim)))
7722 (c-add-syntax literal (car placeholder)))
7723
7724 ;; CASE 3: in a cpp preprocessor macro continuation.
7725 ((and (save-excursion
7726 (when (c-beginning-of-macro)
7727 (setq macro-start (point))))
7728 (/= macro-start (c-point 'boi))
7729 (progn
7730 (setq tmpsymbol 'cpp-macro-cont)
7731 (or (not c-syntactic-indentation-in-macros)
7732 (save-excursion
7733 (goto-char macro-start)
7734 ;; If at the beginning of the body of a #define
7735 ;; directive then analyze as cpp-define-intro
7736 ;; only. Go on with the syntactic analysis
7737 ;; otherwise. in-macro-expr is set if we're in a
7738 ;; cpp expression, i.e. before the #define body
7739 ;; or anywhere in a non-#define directive.
7740 (if (c-forward-to-cpp-define-body)
7741 (let ((indent-boi (c-point 'boi indent-point)))
7742 (setq in-macro-expr (> (point) indent-boi)
7743 tmpsymbol 'cpp-define-intro)
7744 (= (point) indent-boi))
7745 (setq in-macro-expr t)
7746 nil)))))
7747 (c-add-syntax tmpsymbol macro-start)
7748 (setq macro-start nil))
7749
7750 ;; CASE 11: an else clause?
7751 ((looking-at "else\\>[^_]")
7752 (c-beginning-of-statement-1 containing-sexp)
7753 (c-add-stmt-syntax 'else-clause nil t
7754 containing-sexp paren-state))
7755
7756 ;; CASE 12: while closure of a do/while construct?
7757 ((and (looking-at "while\\>[^_]")
7758 (save-excursion
7759 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
7760 'beginning)
7761 (setq placeholder (point)))))
7762 (goto-char placeholder)
7763 (c-add-stmt-syntax 'do-while-closure nil t
7764 containing-sexp paren-state))
7765
7766 ;; CASE 13: A catch or finally clause? This case is simpler
7767 ;; than if-else and do-while, because a block is required
7768 ;; after every try, catch and finally.
7769 ((save-excursion
7770 (and (cond ((c-major-mode-is 'c++-mode)
7771 (looking-at "catch\\>[^_]"))
7772 ((c-major-mode-is 'java-mode)
7773 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
7774 (and (c-safe (c-backward-syntactic-ws)
7775 (c-backward-sexp)
7776 t)
7777 (eq (char-after) ?{)
7778 (c-safe (c-backward-syntactic-ws)
7779 (c-backward-sexp)
7780 t)
7781 (if (eq (char-after) ?\()
7782 (c-safe (c-backward-sexp) t)
7783 t))
7784 (looking-at "\\(try\\|catch\\)\\>[^_]")
7785 (setq placeholder (point))))
7786 (goto-char placeholder)
7787 (c-add-stmt-syntax 'catch-clause nil t
7788 containing-sexp paren-state))
7789
7790 ;; CASE 18: A substatement we can recognize by keyword.
7791 ((save-excursion
7792 (and c-opt-block-stmt-key
7793 (not (eq char-before-ip ?\;))
7794 (not (c-at-vsemi-p before-ws-ip))
7795 (not (memq char-after-ip '(?\) ?\] ?,)))
7796 (or (not (eq char-before-ip ?}))
7797 (c-looking-at-inexpr-block-backward c-state-cache))
7798 (> (point)
7799 (progn
7800 ;; Ought to cache the result from the
7801 ;; c-beginning-of-statement-1 calls here.
7802 (setq placeholder (point))
7803 (while (eq (setq step-type
7804 (c-beginning-of-statement-1 lim))
7805 'label))
7806 (if (eq step-type 'previous)
7807 (goto-char placeholder)
7808 (setq placeholder (point))
7809 (if (and (eq step-type 'same)
7810 (not (looking-at c-opt-block-stmt-key)))
7811 ;; Step up to the containing statement if we
7812 ;; stayed in the same one.
7813 (let (step)
7814 (while (eq
7815 (setq step
7816 (c-beginning-of-statement-1 lim))
7817 'label))
7818 (if (eq step 'up)
7819 (setq placeholder (point))
7820 ;; There was no containing statement afterall.
7821 (goto-char placeholder)))))
7822 placeholder))
7823 (if (looking-at c-block-stmt-2-key)
7824 ;; Require a parenthesis after these keywords.
7825 ;; Necessary to catch e.g. synchronized in Java,
7826 ;; which can be used both as statement and
7827 ;; modifier.
7828 (and (zerop (c-forward-token-2 1 nil))
7829 (eq (char-after) ?\())
7830 (looking-at c-opt-block-stmt-key))))
7831
7832 (if (eq step-type 'up)
7833 ;; CASE 18A: Simple substatement.
7834 (progn
7835 (goto-char placeholder)
7836 (cond
7837 ((eq char-after-ip ?{)
7838 (c-add-stmt-syntax 'substatement-open nil nil
7839 containing-sexp paren-state))
7840 ((save-excursion
7841 (goto-char indent-point)
7842 (back-to-indentation)
7843 (c-forward-label))
7844 (c-add-stmt-syntax 'substatement-label nil nil
7845 containing-sexp paren-state))
7846 (t
7847 (c-add-stmt-syntax 'substatement nil nil
7848 containing-sexp paren-state))))
7849
7850 ;; CASE 18B: Some other substatement. This is shared
7851 ;; with case 10.
7852 (c-guess-continued-construct indent-point
7853 char-after-ip
7854 placeholder
7855 lim
7856 paren-state)))
7857
7858 ;; CASE 14: A case or default label
7859 ((looking-at c-label-kwds-regexp)
7860 (if containing-sexp
7861 (progn
7862 (goto-char containing-sexp)
7863 (setq lim (c-most-enclosing-brace c-state-cache
7864 containing-sexp))
7865 (c-backward-to-block-anchor lim)
7866 (c-add-stmt-syntax 'case-label nil t lim paren-state))
7867 ;; Got a bogus label at the top level. In lack of better
7868 ;; alternatives, anchor it on (point-min).
7869 (c-add-syntax 'case-label (point-min))))
7870
7871 ;; CASE 15: any other label
7872 ((save-excursion
7873 (back-to-indentation)
7874 (and (not (looking-at c-syntactic-ws-start))
7875 (c-forward-label)))
7876 (cond (containing-decl-open
7877 (setq placeholder (c-add-class-syntax 'inclass
7878 containing-decl-open
7879 containing-decl-start
7880 containing-decl-kwd
7881 paren-state))
7882 ;; Append access-label with the same anchor point as
7883 ;; inclass gets.
7884 (c-append-syntax 'access-label placeholder))
7885
7886 (containing-sexp
7887 (goto-char containing-sexp)
7888 (setq lim (c-most-enclosing-brace c-state-cache
7889 containing-sexp))
7890 (save-excursion
7891 (setq tmpsymbol
7892 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
7893 (looking-at "switch\\>[^_]"))
7894 ;; If the surrounding statement is a switch then
7895 ;; let's analyze all labels as switch labels, so
7896 ;; that they get lined up consistently.
7897 'case-label
7898 'label)))
7899 (c-backward-to-block-anchor lim)
7900 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
7901
7902 (t
7903 ;; A label on the top level. Treat it as a class
7904 ;; context. (point-min) is the closest we get to the
7905 ;; class open brace.
7906 (c-add-syntax 'access-label (point-min)))))
7907
7908 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
7909 ;; 17E.
7910 ((setq placeholder (c-looking-at-inexpr-block
7911 (c-safe-position containing-sexp paren-state)
7912 containing-sexp
7913 ;; Have to turn on the heuristics after
7914 ;; the point even though it doesn't work
7915 ;; very well. C.f. test case class-16.pike.
7916 t))
7917 (setq tmpsymbol (assq (car placeholder)
7918 '((inexpr-class . class-open)
7919 (inexpr-statement . block-open))))
7920 (if tmpsymbol
7921 ;; It's a statement block or an anonymous class.
7922 (setq tmpsymbol (cdr tmpsymbol))
7923 ;; It's a Pike lambda. Check whether we are between the
7924 ;; lambda keyword and the argument list or at the defun
7925 ;; opener.
7926 (setq tmpsymbol (if (eq char-after-ip ?{)
7927 'inline-open
7928 'lambda-intro-cont)))
7929 (goto-char (cdr placeholder))
7930 (back-to-indentation)
7931 (c-add-stmt-syntax tmpsymbol nil t
7932 (c-most-enclosing-brace c-state-cache (point))
7933 paren-state)
7934 (unless (eq (point) (cdr placeholder))
7935 (c-add-syntax (car placeholder))))
7936
7937 ;; CASE 5: Line is inside a declaration level block or at top level.
7938 ((or containing-decl-open (null containing-sexp))
7939 (cond
7940
7941 ;; CASE 5A: we are looking at a defun, brace list, class,
7942 ;; or inline-inclass method opening brace
7943 ((setq special-brace-list
7944 (or (and c-special-brace-lists
7945 (c-looking-at-special-brace-list))
7946 (eq char-after-ip ?{)))
7947 (cond
7948
7949 ;; CASE 5A.1: Non-class declaration block open.
7950 ((save-excursion
7951 (let (tmp)
7952 (and (eq char-after-ip ?{)
7953 (setq tmp (c-looking-at-decl-block containing-sexp t))
7954 (progn
7955 (setq placeholder (point))
7956 (goto-char tmp)
7957 (looking-at c-symbol-key))
7958 (c-keyword-member
7959 (c-keyword-sym (setq keyword (match-string 0)))
7960 'c-other-block-decl-kwds))))
7961 (goto-char placeholder)
7962 (c-add-stmt-syntax
7963 (if (string-equal keyword "extern")
7964 ;; Special case for extern-lang-open.
7965 'extern-lang-open
7966 (intern (concat keyword "-open")))
7967 nil t containing-sexp paren-state))
7968
7969 ;; CASE 5A.2: we are looking at a class opening brace
7970 ((save-excursion
7971 (goto-char indent-point)
7972 (skip-chars-forward " \t")
7973 (and (eq (char-after) ?{)
7974 (c-looking-at-decl-block containing-sexp t)
7975 (setq placeholder (point))))
7976 (c-add-syntax 'class-open placeholder))
7977
7978 ;; CASE 5A.3: brace list open
7979 ((save-excursion
7980 (c-beginning-of-decl-1 lim)
7981 (while (looking-at c-specifier-key)
7982 (goto-char (match-end 1))
7983 (c-forward-syntactic-ws indent-point))
7984 (setq placeholder (c-point 'boi))
7985 (or (consp special-brace-list)
7986 (and (or (save-excursion
7987 (goto-char indent-point)
7988 (setq tmpsymbol nil)
7989 (while (and (> (point) placeholder)
7990 (zerop (c-backward-token-2 1 t))
7991 (/= (char-after) ?=))
7992 (and c-opt-inexpr-brace-list-key
7993 (not tmpsymbol)
7994 (looking-at c-opt-inexpr-brace-list-key)
7995 (setq tmpsymbol 'topmost-intro-cont)))
7996 (eq (char-after) ?=))
7997 (looking-at c-brace-list-key))
7998 (save-excursion
7999 (while (and (< (point) indent-point)
8000 (zerop (c-forward-token-2 1 t))
8001 (not (memq (char-after) '(?\; ?\()))))
8002 (not (memq (char-after) '(?\; ?\()))
8003 ))))
8004 (if (and (not c-auto-newline-analysis)
8005 (c-major-mode-is 'java-mode)
8006 (eq tmpsymbol 'topmost-intro-cont))
8007 ;; We're in Java and have found that the open brace
8008 ;; belongs to a "new Foo[]" initialization list,
8009 ;; which means the brace list is part of an
8010 ;; expression and not a top level definition. We
8011 ;; therefore treat it as any topmost continuation
8012 ;; even though the semantically correct symbol still
8013 ;; is brace-list-open, on the same grounds as in
8014 ;; case B.2.
8015 (progn
8016 (c-beginning-of-statement-1 lim)
8017 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8018 (c-add-syntax 'brace-list-open placeholder)))
8019
8020 ;; CASE 5A.4: inline defun open
8021 ((and containing-decl-open
8022 (not (c-keyword-member containing-decl-kwd
8023 'c-other-block-decl-kwds)))
8024 (c-add-syntax 'inline-open)
8025 (c-add-class-syntax 'inclass
8026 containing-decl-open
8027 containing-decl-start
8028 containing-decl-kwd
8029 paren-state))
8030
8031 ;; CASE 5A.5: ordinary defun open
8032 (t
8033 (save-excursion
8034 (c-beginning-of-decl-1 lim)
8035 (while (looking-at c-specifier-key)
8036 (goto-char (match-end 1))
8037 (c-forward-syntactic-ws indent-point))
8038 (c-add-syntax 'defun-open (c-point 'boi))
8039 ;; Bogus to use bol here, but it's the legacy. (Resolved,
8040 ;; 2007-11-09)
8041 ))))
8042
8043 ;; CASE 5B: After a function header but before the body (or
8044 ;; the ending semicolon if there's no body).
8045 ((save-excursion
8046 (when (setq placeholder (c-just-after-func-arglist-p lim))
8047 (setq tmp-pos (point))))
8048 (cond
8049
8050 ;; CASE 5B.1: Member init list.
8051 ((eq (char-after tmp-pos) ?:)
8052 (if (or (> tmp-pos indent-point)
8053 (= (c-point 'bosws) (1+ tmp-pos)))
8054 (progn
8055 ;; There is no preceding member init clause.
8056 ;; Indent relative to the beginning of indentation
8057 ;; for the topmost-intro line that contains the
8058 ;; prototype's open paren.
8059 (goto-char placeholder)
8060 (c-add-syntax 'member-init-intro (c-point 'boi)))
8061 ;; Indent relative to the first member init clause.
8062 (goto-char (1+ tmp-pos))
8063 (c-forward-syntactic-ws)
8064 (c-add-syntax 'member-init-cont (point))))
8065
8066 ;; CASE 5B.2: K&R arg decl intro
8067 ((and c-recognize-knr-p
8068 (c-in-knr-argdecl lim))
8069 (c-beginning-of-statement-1 lim)
8070 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
8071 (if containing-decl-open
8072 (c-add-class-syntax 'inclass
8073 containing-decl-open
8074 containing-decl-start
8075 containing-decl-kwd
8076 paren-state)))
8077
8078 ;; CASE 5B.4: Nether region after a C++ or Java func
8079 ;; decl, which could include a `throws' declaration.
8080 (t
8081 (c-beginning-of-statement-1 lim)
8082 (c-add-syntax 'func-decl-cont (c-point 'boi))
8083 )))
8084
8085 ;; CASE 5C: inheritance line. could be first inheritance
8086 ;; line, or continuation of a multiple inheritance
8087 ((or (and (c-major-mode-is 'c++-mode)
8088 (progn
8089 (when (eq char-after-ip ?,)
8090 (skip-chars-forward " \t")
8091 (forward-char))
8092 (looking-at c-opt-postfix-decl-spec-key)))
8093 (and (or (eq char-before-ip ?:)
8094 ;; watch out for scope operator
8095 (save-excursion
8096 (and (eq char-after-ip ?:)
8097 (c-safe (forward-char 1) t)
8098 (not (eq (char-after) ?:))
8099 )))
8100 (save-excursion
8101 (c-backward-syntactic-ws lim)
8102 (if (eq char-before-ip ?:)
8103 (progn
8104 (forward-char -1)
8105 (c-backward-syntactic-ws lim)))
8106 (back-to-indentation)
8107 (looking-at c-class-key)))
8108 ;; for Java
8109 (and (c-major-mode-is 'java-mode)
8110 (let ((fence (save-excursion
8111 (c-beginning-of-statement-1 lim)
8112 (point)))
8113 cont done)
8114 (save-excursion
8115 (while (not done)
8116 (cond ((looking-at c-opt-postfix-decl-spec-key)
8117 (setq injava-inher (cons cont (point))
8118 done t))
8119 ((or (not (c-safe (c-forward-sexp -1) t))
8120 (<= (point) fence))
8121 (setq done t))
8122 )
8123 (setq cont t)))
8124 injava-inher)
8125 (not (c-crosses-statement-barrier-p (cdr injava-inher)
8126 (point)))
8127 ))
8128 (cond
8129
8130 ;; CASE 5C.1: non-hanging colon on an inher intro
8131 ((eq char-after-ip ?:)
8132 (c-beginning-of-statement-1 lim)
8133 (c-add-syntax 'inher-intro (c-point 'boi))
8134 ;; don't add inclass symbol since relative point already
8135 ;; contains any class offset
8136 )
8137
8138 ;; CASE 5C.2: hanging colon on an inher intro
8139 ((eq char-before-ip ?:)
8140 (c-beginning-of-statement-1 lim)
8141 (c-add-syntax 'inher-intro (c-point 'boi))
8142 (if containing-decl-open
8143 (c-add-class-syntax 'inclass
8144 containing-decl-open
8145 containing-decl-start
8146 containing-decl-kwd
8147 paren-state)))
8148
8149 ;; CASE 5C.3: in a Java implements/extends
8150 (injava-inher
8151 (let ((where (cdr injava-inher))
8152 (cont (car injava-inher)))
8153 (goto-char where)
8154 (cond ((looking-at "throws\\>[^_]")
8155 (c-add-syntax 'func-decl-cont
8156 (progn (c-beginning-of-statement-1 lim)
8157 (c-point 'boi))))
8158 (cont (c-add-syntax 'inher-cont where))
8159 (t (c-add-syntax 'inher-intro
8160 (progn (goto-char (cdr injava-inher))
8161 (c-beginning-of-statement-1 lim)
8162 (point))))
8163 )))
8164
8165 ;; CASE 5C.4: a continued inheritance line
8166 (t
8167 (c-beginning-of-inheritance-list lim)
8168 (c-add-syntax 'inher-cont (point))
8169 ;; don't add inclass symbol since relative point already
8170 ;; contains any class offset
8171 )))
8172
8173 ;; CASE 5D: this could be a top-level initialization, a
8174 ;; member init list continuation, or a template argument
8175 ;; list continuation.
8176 ((save-excursion
8177 ;; Note: We use the fact that lim always is after any
8178 ;; preceding brace sexp.
8179 (if c-recognize-<>-arglists
8180 (while (and
8181 (progn
8182 (c-syntactic-skip-backward "^;,=<>" lim t)
8183 (> (point) lim))
8184 (or
8185 (when c-overloadable-operators-regexp
8186 (when (setq placeholder (c-after-special-operator-id lim))
8187 (goto-char placeholder)
8188 t))
8189 (cond
8190 ((eq (char-before) ?>)
8191 (or (c-backward-<>-arglist nil lim)
8192 (backward-char))
8193 t)
8194 ((eq (char-before) ?<)
8195 (backward-char)
8196 (if (save-excursion
8197 (c-forward-<>-arglist nil))
8198 (progn (forward-char)
8199 nil)
8200 t))
8201 (t nil)))))
8202 ;; NB: No c-after-special-operator-id stuff in this
8203 ;; clause - we assume only C++ needs it.
8204 (c-syntactic-skip-backward "^;,=" lim t))
8205 (memq (char-before) '(?, ?= ?<)))
8206 (cond
8207
8208 ;; CASE 5D.3: perhaps a template list continuation?
8209 ((and (c-major-mode-is 'c++-mode)
8210 (save-excursion
8211 (save-restriction
8212 (c-with-syntax-table c++-template-syntax-table
8213 (goto-char indent-point)
8214 (setq placeholder (c-up-list-backward))
8215 (and placeholder
8216 (eq (char-after placeholder) ?<))))))
8217 (c-with-syntax-table c++-template-syntax-table
8218 (goto-char placeholder)
8219 (c-beginning-of-statement-1 lim t)
8220 (if (save-excursion
8221 (c-backward-syntactic-ws lim)
8222 (eq (char-before) ?<))
8223 ;; In a nested template arglist.
8224 (progn
8225 (goto-char placeholder)
8226 (c-syntactic-skip-backward "^,;" lim t)
8227 (c-forward-syntactic-ws))
8228 (back-to-indentation)))
8229 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8230 ;; template aware.
8231 (c-add-syntax 'template-args-cont (point)))
8232
8233 ;; CASE 5D.4: perhaps a multiple inheritance line?
8234 ((and (c-major-mode-is 'c++-mode)
8235 (save-excursion
8236 (c-beginning-of-statement-1 lim)
8237 (setq placeholder (point))
8238 (if (looking-at "static\\>[^_]")
8239 (c-forward-token-2 1 nil indent-point))
8240 (and (looking-at c-class-key)
8241 (zerop (c-forward-token-2 2 nil indent-point))
8242 (if (eq (char-after) ?<)
8243 (c-with-syntax-table c++-template-syntax-table
8244 (zerop (c-forward-token-2 1 t indent-point)))
8245 t)
8246 (eq (char-after) ?:))))
8247 (goto-char placeholder)
8248 (c-add-syntax 'inher-cont (c-point 'boi)))
8249
8250 ;; CASE 5D.5: Continuation of the "expression part" of a
8251 ;; top level construct.
8252 (t
8253 (while (and (eq (car (c-beginning-of-decl-1 containing-sexp))
8254 'same)
8255 (save-excursion
8256 (c-backward-syntactic-ws)
8257 (eq (char-before) ?}))))
8258 (c-add-stmt-syntax
8259 (if (eq char-before-ip ?,)
8260 ;; A preceding comma at the top level means that a
8261 ;; new variable declaration starts here. Use
8262 ;; topmost-intro-cont for it, for consistency with
8263 ;; the first variable declaration. C.f. case 5N.
8264 'topmost-intro-cont
8265 'statement-cont)
8266 nil nil containing-sexp paren-state))
8267 ))
8268
8269 ;; CASE 5F: Close of a non-class declaration level block.
8270 ((and (eq char-after-ip ?})
8271 (c-keyword-member containing-decl-kwd
8272 'c-other-block-decl-kwds))
8273 ;; This is inconsistent: Should use `containing-decl-open'
8274 ;; here if it's at boi, like in case 5J.
8275 (goto-char containing-decl-start)
8276 (c-add-stmt-syntax
8277 (if (string-equal (symbol-name containing-decl-kwd) "extern")
8278 ;; Special case for compatibility with the
8279 ;; extern-lang syntactic symbols.
8280 'extern-lang-close
8281 (intern (concat (symbol-name containing-decl-kwd)
8282 "-close")))
8283 nil t
8284 (c-most-enclosing-brace paren-state (point))
8285 paren-state))
8286
8287 ;; CASE 5G: we are looking at the brace which closes the
8288 ;; enclosing nested class decl
8289 ((and containing-sexp
8290 (eq char-after-ip ?})
8291 (eq containing-decl-open containing-sexp))
8292 (c-add-class-syntax 'class-close
8293 containing-decl-open
8294 containing-decl-start
8295 containing-decl-kwd
8296 paren-state))
8297
8298 ;; CASE 5H: we could be looking at subsequent knr-argdecls
8299 ((and c-recognize-knr-p
8300 (not containing-sexp) ; can't be knr inside braces.
8301 (not (eq char-before-ip ?}))
8302 (save-excursion
8303 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
8304 (and placeholder
8305 ;; Do an extra check to avoid tripping up on
8306 ;; statements that occur in invalid contexts
8307 ;; (e.g. in macro bodies where we don't really
8308 ;; know the context of what we're looking at).
8309 (not (and c-opt-block-stmt-key
8310 (looking-at c-opt-block-stmt-key)))))
8311 (< placeholder indent-point))
8312 (goto-char placeholder)
8313 (c-add-syntax 'knr-argdecl (point)))
8314
8315 ;; CASE 5I: ObjC method definition.
8316 ((and c-opt-method-key
8317 (looking-at c-opt-method-key))
8318 (c-beginning-of-statement-1 nil t)
8319 (if (= (point) indent-point)
8320 ;; Handle the case when it's the first (non-comment)
8321 ;; thing in the buffer. Can't look for a 'same return
8322 ;; value from cbos1 since ObjC directives currently
8323 ;; aren't recognized fully, so that we get 'same
8324 ;; instead of 'previous if it moved over a preceding
8325 ;; directive.
8326 (goto-char (point-min)))
8327 (c-add-syntax 'objc-method-intro (c-point 'boi)))
8328
8329 ;; CASE 5P: AWK pattern or function or continuation
8330 ;; thereof.
8331 ((c-major-mode-is 'awk-mode)
8332 (setq placeholder (point))
8333 (c-add-stmt-syntax
8334 (if (and (eq (c-beginning-of-statement-1) 'same)
8335 (/= (point) placeholder))
8336 'topmost-intro-cont
8337 'topmost-intro)
8338 nil nil
8339 containing-sexp paren-state))
8340
8341 ;; CASE 5N: At a variable declaration that follows a class
8342 ;; definition or some other block declaration that doesn't
8343 ;; end at the closing '}'. C.f. case 5D.5.
8344 ((progn
8345 (c-backward-syntactic-ws lim)
8346 (and (eq (char-before) ?})
8347 (save-excursion
8348 (let ((start (point)))
8349 (if (and c-state-cache
8350 (consp (car c-state-cache))
8351 (eq (cdar c-state-cache) (point)))
8352 ;; Speed up the backward search a bit.
8353 (goto-char (caar c-state-cache)))
8354 (c-beginning-of-decl-1 containing-sexp)
8355 (setq placeholder (point))
8356 (if (= start (point))
8357 ;; The '}' is unbalanced.
8358 nil
8359 (c-end-of-decl-1)
8360 (>= (point) indent-point))))))
8361 (goto-char placeholder)
8362 (c-add-stmt-syntax 'topmost-intro-cont nil nil
8363 containing-sexp paren-state))
8364
8365 ;; NOTE: The point is at the end of the previous token here.
8366
8367 ;; CASE 5J: we are at the topmost level, make
8368 ;; sure we skip back past any access specifiers
8369 ((and
8370 ;; A macro continuation line is never at top level.
8371 (not (and macro-start
8372 (> indent-point macro-start)))
8373 (save-excursion
8374 (setq placeholder (point))
8375 (or (memq char-before-ip '(?\; ?{ ?} nil))
8376 (c-at-vsemi-p before-ws-ip)
8377 (when (and (eq char-before-ip ?:)
8378 (eq (c-beginning-of-statement-1 lim)
8379 'label))
8380 (c-backward-syntactic-ws lim)
8381 (setq placeholder (point)))
8382 (and (c-major-mode-is 'objc-mode)
8383 (catch 'not-in-directive
8384 (c-beginning-of-statement-1 lim)
8385 (setq placeholder (point))
8386 (while (and (c-forward-objc-directive)
8387 (< (point) indent-point))
8388 (c-forward-syntactic-ws)
8389 (if (>= (point) indent-point)
8390 (throw 'not-in-directive t))
8391 (setq placeholder (point)))
8392 nil)))))
8393 ;; For historic reasons we anchor at bol of the last
8394 ;; line of the previous declaration. That's clearly
8395 ;; highly bogus and useless, and it makes our lives hard
8396 ;; to remain compatible. :P
8397 (goto-char placeholder)
8398 (c-add-syntax 'topmost-intro (c-point 'bol))
8399 (if containing-decl-open
8400 (if (c-keyword-member containing-decl-kwd
8401 'c-other-block-decl-kwds)
8402 (progn
8403 (goto-char (c-brace-anchor-point containing-decl-open))
8404 (c-add-stmt-syntax
8405 (if (string-equal (symbol-name containing-decl-kwd)
8406 "extern")
8407 ;; Special case for compatibility with the
8408 ;; extern-lang syntactic symbols.
8409 'inextern-lang
8410 (intern (concat "in"
8411 (symbol-name containing-decl-kwd))))
8412 nil t
8413 (c-most-enclosing-brace paren-state (point))
8414 paren-state))
8415 (c-add-class-syntax 'inclass
8416 containing-decl-open
8417 containing-decl-start
8418 containing-decl-kwd
8419 paren-state)))
8420 (when (and c-syntactic-indentation-in-macros
8421 macro-start
8422 (/= macro-start (c-point 'boi indent-point)))
8423 (c-add-syntax 'cpp-define-intro)
8424 (setq macro-start nil)))
8425
8426 ;; CASE 5K: we are at an ObjC method definition
8427 ;; continuation line.
8428 ((and c-opt-method-key
8429 (save-excursion
8430 (c-beginning-of-statement-1 lim)
8431 (beginning-of-line)
8432 (when (looking-at c-opt-method-key)
8433 (setq placeholder (point)))))
8434 (c-add-syntax 'objc-method-args-cont placeholder))
8435
8436 ;; CASE 5L: we are at the first argument of a template
8437 ;; arglist that begins on the previous line.
8438 ((and c-recognize-<>-arglists
8439 (eq (char-before) ?<)
8440 (not (and c-overloadable-operators-regexp
8441 (c-after-special-operator-id lim))))
8442 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8443 (c-add-syntax 'template-args-cont (c-point 'boi)))
8444
8445 ;; CASE 5Q: we are at a statement within a macro.
8446 (macro-start
8447 (c-beginning-of-statement-1 containing-sexp)
8448 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
8449
8450 ;; CASE 5M: we are at a topmost continuation line
8451 (t
8452 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8453 (when (c-major-mode-is 'objc-mode)
8454 (setq placeholder (point))
8455 (while (and (c-forward-objc-directive)
8456 (< (point) indent-point))
8457 (c-forward-syntactic-ws)
8458 (setq placeholder (point)))
8459 (goto-char placeholder))
8460 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8461 ))
8462
8463 ;; (CASE 6 has been removed.)
8464
8465 ;; CASE 7: line is an expression, not a statement. Most
8466 ;; likely we are either in a function prototype or a function
8467 ;; call argument list
8468 ((not (or (and c-special-brace-lists
8469 (save-excursion
8470 (goto-char containing-sexp)
8471 (c-looking-at-special-brace-list)))
8472 (eq (char-after containing-sexp) ?{)))
8473 (cond
8474
8475 ;; CASE 7A: we are looking at the arglist closing paren.
8476 ;; C.f. case 7F.
8477 ((memq char-after-ip '(?\) ?\]))
8478 (goto-char containing-sexp)
8479 (setq placeholder (c-point 'boi))
8480 (if (and (c-safe (backward-up-list 1) t)
8481 (>= (point) placeholder))
8482 (progn
8483 (forward-char)
8484 (skip-chars-forward " \t"))
8485 (goto-char placeholder))
8486 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
8487 (c-most-enclosing-brace paren-state (point))
8488 paren-state))
8489
8490 ;; CASE 7B: Looking at the opening brace of an
8491 ;; in-expression block or brace list. C.f. cases 4, 16A
8492 ;; and 17E.
8493 ((and (eq char-after-ip ?{)
8494 (progn
8495 (setq placeholder (c-inside-bracelist-p (point)
8496 paren-state))
8497 (if placeholder
8498 (setq tmpsymbol '(brace-list-open . inexpr-class))
8499 (setq tmpsymbol '(block-open . inexpr-statement)
8500 placeholder
8501 (cdr-safe (c-looking-at-inexpr-block
8502 (c-safe-position containing-sexp
8503 paren-state)
8504 containing-sexp)))
8505 ;; placeholder is nil if it's a block directly in
8506 ;; a function arglist. That makes us skip out of
8507 ;; this case.
8508 )))
8509 (goto-char placeholder)
8510 (back-to-indentation)
8511 (c-add-stmt-syntax (car tmpsymbol) nil t
8512 (c-most-enclosing-brace paren-state (point))
8513 paren-state)
8514 (if (/= (point) placeholder)
8515 (c-add-syntax (cdr tmpsymbol))))
8516
8517 ;; CASE 7C: we are looking at the first argument in an empty
8518 ;; argument list. Use arglist-close if we're actually
8519 ;; looking at a close paren or bracket.
8520 ((memq char-before-ip '(?\( ?\[))
8521 (goto-char containing-sexp)
8522 (setq placeholder (c-point 'boi))
8523 (if (and (c-safe (backward-up-list 1) t)
8524 (>= (point) placeholder))
8525 (progn
8526 (forward-char)
8527 (skip-chars-forward " \t"))
8528 (goto-char placeholder))
8529 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
8530 (c-most-enclosing-brace paren-state (point))
8531 paren-state))
8532
8533 ;; CASE 7D: we are inside a conditional test clause. treat
8534 ;; these things as statements
8535 ((progn
8536 (goto-char containing-sexp)
8537 (and (c-safe (c-forward-sexp -1) t)
8538 (looking-at "\\<for\\>[^_]")))
8539 (goto-char (1+ containing-sexp))
8540 (c-forward-syntactic-ws indent-point)
8541 (if (eq char-before-ip ?\;)
8542 (c-add-syntax 'statement (point))
8543 (c-add-syntax 'statement-cont (point))
8544 ))
8545
8546 ;; CASE 7E: maybe a continued ObjC method call. This is the
8547 ;; case when we are inside a [] bracketed exp, and what
8548 ;; precede the opening bracket is not an identifier.
8549 ((and c-opt-method-key
8550 (eq (char-after containing-sexp) ?\[)
8551 (progn
8552 (goto-char (1- containing-sexp))
8553 (c-backward-syntactic-ws (c-point 'bod))
8554 (if (not (looking-at c-symbol-key))
8555 (c-add-syntax 'objc-method-call-cont containing-sexp))
8556 )))
8557
8558 ;; CASE 7F: we are looking at an arglist continuation line,
8559 ;; but the preceding argument is on the same line as the
8560 ;; opening paren. This case includes multi-line
8561 ;; mathematical paren groupings, but we could be on a
8562 ;; for-list continuation line. C.f. case 7A.
8563 ((progn
8564 (goto-char (1+ containing-sexp))
8565 (< (save-excursion
8566 (c-forward-syntactic-ws)
8567 (point))
8568 (c-point 'bonl)))
8569 (goto-char containing-sexp)
8570 (setq placeholder (c-point 'boi))
8571 (if (and (c-safe (backward-up-list 1) t)
8572 (>= (point) placeholder))
8573 (progn
8574 (forward-char)
8575 (skip-chars-forward " \t"))
8576 (goto-char placeholder))
8577 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
8578 (c-most-enclosing-brace c-state-cache (point))
8579 paren-state))
8580
8581 ;; CASE 7G: we are looking at just a normal arglist
8582 ;; continuation line
8583 (t (c-forward-syntactic-ws indent-point)
8584 (c-add-syntax 'arglist-cont (c-point 'boi)))
8585 ))
8586
8587 ;; CASE 8: func-local multi-inheritance line
8588 ((and (c-major-mode-is 'c++-mode)
8589 (save-excursion
8590 (goto-char indent-point)
8591 (skip-chars-forward " \t")
8592 (looking-at c-opt-postfix-decl-spec-key)))
8593 (goto-char indent-point)
8594 (skip-chars-forward " \t")
8595 (cond
8596
8597 ;; CASE 8A: non-hanging colon on an inher intro
8598 ((eq char-after-ip ?:)
8599 (c-backward-syntactic-ws lim)
8600 (c-add-syntax 'inher-intro (c-point 'boi)))
8601
8602 ;; CASE 8B: hanging colon on an inher intro
8603 ((eq char-before-ip ?:)
8604 (c-add-syntax 'inher-intro (c-point 'boi)))
8605
8606 ;; CASE 8C: a continued inheritance line
8607 (t
8608 (c-beginning-of-inheritance-list lim)
8609 (c-add-syntax 'inher-cont (point))
8610 )))
8611
8612 ;; CASE 9: we are inside a brace-list
8613 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
8614 (setq special-brace-list
8615 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
8616 (save-excursion
8617 (goto-char containing-sexp)
8618 (c-looking-at-special-brace-list)))
8619 (c-inside-bracelist-p containing-sexp paren-state))))
8620 (cond
8621
8622 ;; CASE 9A: In the middle of a special brace list opener.
8623 ((and (consp special-brace-list)
8624 (save-excursion
8625 (goto-char containing-sexp)
8626 (eq (char-after) ?\())
8627 (eq char-after-ip (car (cdr special-brace-list))))
8628 (goto-char (car (car special-brace-list)))
8629 (skip-chars-backward " \t")
8630 (if (and (bolp)
8631 (assoc 'statement-cont
8632 (setq placeholder (c-guess-basic-syntax))))
8633 (setq c-syntactic-context placeholder)
8634 (c-beginning-of-statement-1
8635 (c-safe-position (1- containing-sexp) paren-state))
8636 (c-forward-token-2 0)
8637 (while (looking-at c-specifier-key)
8638 (goto-char (match-end 1))
8639 (c-forward-syntactic-ws))
8640 (c-add-syntax 'brace-list-open (c-point 'boi))))
8641
8642 ;; CASE 9B: brace-list-close brace
8643 ((if (consp special-brace-list)
8644 ;; Check special brace list closer.
8645 (progn
8646 (goto-char (car (car special-brace-list)))
8647 (save-excursion
8648 (goto-char indent-point)
8649 (back-to-indentation)
8650 (or
8651 ;; We were between the special close char and the `)'.
8652 (and (eq (char-after) ?\))
8653 (eq (1+ (point)) (cdr (car special-brace-list))))
8654 ;; We were before the special close char.
8655 (and (eq (char-after) (cdr (cdr special-brace-list)))
8656 (zerop (c-forward-token-2))
8657 (eq (1+ (point)) (cdr (car special-brace-list)))))))
8658 ;; Normal brace list check.
8659 (and (eq char-after-ip ?})
8660 (c-safe (goto-char (c-up-list-backward (point))) t)
8661 (= (point) containing-sexp)))
8662 (if (eq (point) (c-point 'boi))
8663 (c-add-syntax 'brace-list-close (point))
8664 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8665 (c-beginning-of-statement-1 lim)
8666 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
8667
8668 (t
8669 ;; Prepare for the rest of the cases below by going to the
8670 ;; token following the opening brace
8671 (if (consp special-brace-list)
8672 (progn
8673 (goto-char (car (car special-brace-list)))
8674 (c-forward-token-2 1 nil indent-point))
8675 (goto-char containing-sexp))
8676 (forward-char)
8677 (let ((start (point)))
8678 (c-forward-syntactic-ws indent-point)
8679 (goto-char (max start (c-point 'bol))))
8680 (c-skip-ws-forward indent-point)
8681 (cond
8682
8683 ;; CASE 9C: we're looking at the first line in a brace-list
8684 ((= (point) indent-point)
8685 (if (consp special-brace-list)
8686 (goto-char (car (car special-brace-list)))
8687 (goto-char containing-sexp))
8688 (if (eq (point) (c-point 'boi))
8689 (c-add-syntax 'brace-list-intro (point))
8690 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8691 (c-beginning-of-statement-1 lim)
8692 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
8693
8694 ;; CASE 9D: this is just a later brace-list-entry or
8695 ;; brace-entry-open
8696 (t (if (or (eq char-after-ip ?{)
8697 (and c-special-brace-lists
8698 (save-excursion
8699 (goto-char indent-point)
8700 (c-forward-syntactic-ws (c-point 'eol))
8701 (c-looking-at-special-brace-list (point)))))
8702 (c-add-syntax 'brace-entry-open (point))
8703 (c-add-syntax 'brace-list-entry (point))
8704 ))
8705 ))))
8706
8707 ;; CASE 10: A continued statement or top level construct.
8708 ((and (not (memq char-before-ip '(?\; ?:)))
8709 (not (c-at-vsemi-p before-ws-ip))
8710 (or (not (eq char-before-ip ?}))
8711 (c-looking-at-inexpr-block-backward c-state-cache))
8712 (> (point)
8713 (save-excursion
8714 (c-beginning-of-statement-1 containing-sexp)
8715 (setq placeholder (point))))
8716 (/= placeholder containing-sexp))
8717 ;; This is shared with case 18.
8718 (c-guess-continued-construct indent-point
8719 char-after-ip
8720 placeholder
8721 containing-sexp
8722 paren-state))
8723
8724 ;; CASE 16: block close brace, possibly closing the defun or
8725 ;; the class
8726 ((eq char-after-ip ?})
8727 ;; From here on we have the next containing sexp in lim.
8728 (setq lim (c-most-enclosing-brace paren-state))
8729 (goto-char containing-sexp)
8730 (cond
8731
8732 ;; CASE 16E: Closing a statement block? This catches
8733 ;; cases where it's preceded by a statement keyword,
8734 ;; which works even when used in an "invalid" context,
8735 ;; e.g. a macro argument.
8736 ((c-after-conditional)
8737 (c-backward-to-block-anchor lim)
8738 (c-add-stmt-syntax 'block-close nil t lim paren-state))
8739
8740 ;; CASE 16A: closing a lambda defun or an in-expression
8741 ;; block? C.f. cases 4, 7B and 17E.
8742 ((setq placeholder (c-looking-at-inexpr-block
8743 (c-safe-position containing-sexp paren-state)
8744 nil))
8745 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8746 'inline-close
8747 'block-close))
8748 (goto-char containing-sexp)
8749 (back-to-indentation)
8750 (if (= containing-sexp (point))
8751 (c-add-syntax tmpsymbol (point))
8752 (goto-char (cdr placeholder))
8753 (back-to-indentation)
8754 (c-add-stmt-syntax tmpsymbol nil t
8755 (c-most-enclosing-brace paren-state (point))
8756 paren-state)
8757 (if (/= (point) (cdr placeholder))
8758 (c-add-syntax (car placeholder)))))
8759
8760 ;; CASE 16B: does this close an inline or a function in
8761 ;; a non-class declaration level block?
8762 ((save-excursion
8763 (and lim
8764 (progn
8765 (goto-char lim)
8766 (c-looking-at-decl-block
8767 (c-most-enclosing-brace paren-state lim)
8768 nil))
8769 (setq placeholder (point))))
8770 (c-backward-to-decl-anchor lim)
8771 (back-to-indentation)
8772 (if (save-excursion
8773 (goto-char placeholder)
8774 (looking-at c-other-decl-block-key))
8775 (c-add-syntax 'defun-close (point))
8776 (c-add-syntax 'inline-close (point))))
8777
8778 ;; CASE 16F: Can be a defun-close of a function declared
8779 ;; in a statement block, e.g. in Pike or when using gcc
8780 ;; extensions, but watch out for macros followed by
8781 ;; blocks. Let it through to be handled below.
8782 ;; C.f. cases B.3 and 17G.
8783 ((save-excursion
8784 (and (not (c-at-statement-start-p))
8785 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8786 (setq placeholder (point))
8787 (let ((c-recognize-typeless-decls nil))
8788 ;; Turn off recognition of constructs that
8789 ;; lacks a type in this case, since that's more
8790 ;; likely to be a macro followed by a block.
8791 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8792 (back-to-indentation)
8793 (if (/= (point) containing-sexp)
8794 (goto-char placeholder))
8795 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
8796
8797 ;; CASE 16C: If there is an enclosing brace then this is
8798 ;; a block close since defun closes inside declaration
8799 ;; level blocks have been handled above.
8800 (lim
8801 ;; If the block is preceded by a case/switch label on
8802 ;; the same line, we anchor at the first preceding label
8803 ;; at boi. The default handling in c-add-stmt-syntax
8804 ;; really fixes it better, but we do like this to keep
8805 ;; the indentation compatible with version 5.28 and
8806 ;; earlier. C.f. case 17H.
8807 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8808 (eq (c-beginning-of-statement-1 lim) 'label)))
8809 (goto-char placeholder)
8810 (if (looking-at c-label-kwds-regexp)
8811 (c-add-syntax 'block-close (point))
8812 (goto-char containing-sexp)
8813 ;; c-backward-to-block-anchor not necessary here; those
8814 ;; situations are handled in case 16E above.
8815 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
8816
8817 ;; CASE 16D: Only top level defun close left.
8818 (t
8819 (goto-char containing-sexp)
8820 (c-backward-to-decl-anchor lim)
8821 (c-add-stmt-syntax 'defun-close nil nil
8822 (c-most-enclosing-brace paren-state)
8823 paren-state))
8824 ))
8825
8826 ;; CASE 17: Statement or defun catchall.
8827 (t
8828 (goto-char indent-point)
8829 ;; Back up statements until we find one that starts at boi.
8830 (while (let* ((prev-point (point))
8831 (last-step-type (c-beginning-of-statement-1
8832 containing-sexp)))
8833 (if (= (point) prev-point)
8834 (progn
8835 (setq step-type (or step-type last-step-type))
8836 nil)
8837 (setq step-type last-step-type)
8838 (/= (point) (c-point 'boi)))))
8839 (cond
8840
8841 ;; CASE 17B: continued statement
8842 ((and (eq step-type 'same)
8843 (/= (point) indent-point))
8844 (c-add-stmt-syntax 'statement-cont nil nil
8845 containing-sexp paren-state))
8846
8847 ;; CASE 17A: After a case/default label?
8848 ((progn
8849 (while (and (eq step-type 'label)
8850 (not (looking-at c-label-kwds-regexp)))
8851 (setq step-type
8852 (c-beginning-of-statement-1 containing-sexp)))
8853 (eq step-type 'label))
8854 (c-add-stmt-syntax (if (eq char-after-ip ?{)
8855 'statement-case-open
8856 'statement-case-intro)
8857 nil t containing-sexp paren-state))
8858
8859 ;; CASE 17D: any old statement
8860 ((progn
8861 (while (eq step-type 'label)
8862 (setq step-type
8863 (c-beginning-of-statement-1 containing-sexp)))
8864 (eq step-type 'previous))
8865 (c-add-stmt-syntax 'statement nil t
8866 containing-sexp paren-state)
8867 (if (eq char-after-ip ?{)
8868 (c-add-syntax 'block-open)))
8869
8870 ;; CASE 17I: Inside a substatement block.
8871 ((progn
8872 ;; The following tests are all based on containing-sexp.
8873 (goto-char containing-sexp)
8874 ;; From here on we have the next containing sexp in lim.
8875 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
8876 (c-after-conditional))
8877 (c-backward-to-block-anchor lim)
8878 (c-add-stmt-syntax 'statement-block-intro nil t
8879 lim paren-state)
8880 (if (eq char-after-ip ?{)
8881 (c-add-syntax 'block-open)))
8882
8883 ;; CASE 17E: first statement in an in-expression block.
8884 ;; C.f. cases 4, 7B and 16A.
8885 ((setq placeholder (c-looking-at-inexpr-block
8886 (c-safe-position containing-sexp paren-state)
8887 nil))
8888 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8889 'defun-block-intro
8890 'statement-block-intro))
8891 (back-to-indentation)
8892 (if (= containing-sexp (point))
8893 (c-add-syntax tmpsymbol (point))
8894 (goto-char (cdr placeholder))
8895 (back-to-indentation)
8896 (c-add-stmt-syntax tmpsymbol nil t
8897 (c-most-enclosing-brace c-state-cache (point))
8898 paren-state)
8899 (if (/= (point) (cdr placeholder))
8900 (c-add-syntax (car placeholder))))
8901 (if (eq char-after-ip ?{)
8902 (c-add-syntax 'block-open)))
8903
8904 ;; CASE 17F: first statement in an inline, or first
8905 ;; statement in a top-level defun. we can tell this is it
8906 ;; if there are no enclosing braces that haven't been
8907 ;; narrowed out by a class (i.e. don't use bod here).
8908 ((save-excursion
8909 (or (not (setq placeholder (c-most-enclosing-brace
8910 paren-state)))
8911 (and (progn
8912 (goto-char placeholder)
8913 (eq (char-after) ?{))
8914 (c-looking-at-decl-block (c-most-enclosing-brace
8915 paren-state (point))
8916 nil))))
8917 (c-backward-to-decl-anchor lim)
8918 (back-to-indentation)
8919 (c-add-syntax 'defun-block-intro (point)))
8920
8921 ;; CASE 17G: First statement in a function declared inside
8922 ;; a normal block. This can occur in Pike and with
8923 ;; e.g. the gcc extensions, but watch out for macros
8924 ;; followed by blocks. C.f. cases B.3 and 16F.
8925 ((save-excursion
8926 (and (not (c-at-statement-start-p))
8927 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8928 (setq placeholder (point))
8929 (let ((c-recognize-typeless-decls nil))
8930 ;; Turn off recognition of constructs that lacks
8931 ;; a type in this case, since that's more likely
8932 ;; to be a macro followed by a block.
8933 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8934 (back-to-indentation)
8935 (if (/= (point) containing-sexp)
8936 (goto-char placeholder))
8937 (c-add-stmt-syntax 'defun-block-intro nil t
8938 lim paren-state))
8939
8940 ;; CASE 17H: First statement in a block.
8941 (t
8942 ;; If the block is preceded by a case/switch label on the
8943 ;; same line, we anchor at the first preceding label at
8944 ;; boi. The default handling in c-add-stmt-syntax is
8945 ;; really fixes it better, but we do like this to keep the
8946 ;; indentation compatible with version 5.28 and earlier.
8947 ;; C.f. case 16C.
8948 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8949 (eq (c-beginning-of-statement-1 lim) 'label)))
8950 (goto-char placeholder)
8951 (if (looking-at c-label-kwds-regexp)
8952 (c-add-syntax 'statement-block-intro (point))
8953 (goto-char containing-sexp)
8954 ;; c-backward-to-block-anchor not necessary here; those
8955 ;; situations are handled in case 17I above.
8956 (c-add-stmt-syntax 'statement-block-intro nil t
8957 lim paren-state))
8958 (if (eq char-after-ip ?{)
8959 (c-add-syntax 'block-open)))
8960 ))
8961 )
8962
8963 ;; now we need to look at any modifiers
8964 (goto-char indent-point)
8965 (skip-chars-forward " \t")
8966
8967 ;; are we looking at a comment only line?
8968 (when (and (looking-at c-comment-start-regexp)
8969 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
8970 (c-append-syntax 'comment-intro))
8971
8972 ;; we might want to give additional offset to friends (in C++).
8973 (when (and c-opt-friend-key
8974 (looking-at c-opt-friend-key))
8975 (c-append-syntax 'friend))
8976
8977 ;; Set syntactic-relpos.
8978 (let ((p c-syntactic-context))
8979 (while (and p
8980 (if (integerp (c-langelem-pos (car p)))
8981 (progn
8982 (setq syntactic-relpos (c-langelem-pos (car p)))
8983 nil)
8984 t))
8985 (setq p (cdr p))))
8986
8987 ;; Start of or a continuation of a preprocessor directive?
8988 (if (and macro-start
8989 (eq macro-start (c-point 'boi))
8990 (not (and (c-major-mode-is 'pike-mode)
8991 (eq (char-after (1+ macro-start)) ?\"))))
8992 (c-append-syntax 'cpp-macro)
8993 (when (and c-syntactic-indentation-in-macros macro-start)
8994 (if in-macro-expr
8995 (when (or
8996 (< syntactic-relpos macro-start)
8997 (not (or
8998 (assq 'arglist-intro c-syntactic-context)
8999 (assq 'arglist-cont c-syntactic-context)
9000 (assq 'arglist-cont-nonempty c-syntactic-context)
9001 (assq 'arglist-close c-syntactic-context))))
9002 ;; If inside a cpp expression, i.e. anywhere in a
9003 ;; cpp directive except a #define body, we only let
9004 ;; through the syntactic analysis that is internal
9005 ;; in the expression. That means the arglist
9006 ;; elements, if they are anchored inside the cpp
9007 ;; expression.
9008 (setq c-syntactic-context nil)
9009 (c-add-syntax 'cpp-macro-cont macro-start))
9010 (when (and (eq macro-start syntactic-relpos)
9011 (not (assq 'cpp-define-intro c-syntactic-context))
9012 (save-excursion
9013 (goto-char macro-start)
9014 (or (not (c-forward-to-cpp-define-body))
9015 (<= (point) (c-point 'boi indent-point)))))
9016 ;; Inside a #define body and the syntactic analysis is
9017 ;; anchored on the start of the #define. In this case
9018 ;; we add cpp-define-intro to get the extra
9019 ;; indentation of the #define body.
9020 (c-add-syntax 'cpp-define-intro)))))
9021
9022 ;; return the syntax
9023 c-syntactic-context)))
9024
9025 \f
9026 ;; Indentation calculation.
9027
9028 (defun c-evaluate-offset (offset langelem symbol)
9029 ;; offset can be a number, a function, a variable, a list, or one of
9030 ;; the symbols + or -
9031 ;;
9032 ;; This function might do hidden buffer changes.
9033 (let ((res
9034 (cond
9035 ((numberp offset) offset)
9036 ((vectorp offset) offset)
9037 ((null offset) nil)
9038
9039 ((eq offset '+) c-basic-offset)
9040 ((eq offset '-) (- c-basic-offset))
9041 ((eq offset '++) (* 2 c-basic-offset))
9042 ((eq offset '--) (* 2 (- c-basic-offset)))
9043 ((eq offset '*) (/ c-basic-offset 2))
9044 ((eq offset '/) (/ (- c-basic-offset) 2))
9045
9046 ((functionp offset)
9047 (c-evaluate-offset
9048 (funcall offset
9049 (cons (c-langelem-sym langelem)
9050 (c-langelem-pos langelem)))
9051 langelem symbol))
9052
9053 ((listp offset)
9054 (cond
9055 ((eq (car offset) 'quote)
9056 (c-benign-error "The offset %S for %s was mistakenly quoted"
9057 offset symbol)
9058 nil)
9059
9060 ((memq (car offset) '(min max))
9061 (let (res val (method (car offset)))
9062 (setq offset (cdr offset))
9063 (while offset
9064 (setq val (c-evaluate-offset (car offset) langelem symbol))
9065 (cond
9066 ((not val))
9067 ((not res)
9068 (setq res val))
9069 ((integerp val)
9070 (if (vectorp res)
9071 (c-benign-error "\
9072 Error evaluating offset %S for %s: \
9073 Cannot combine absolute offset %S with relative %S in `%s' method"
9074 (car offset) symbol res val method)
9075 (setq res (funcall method res val))))
9076 (t
9077 (if (integerp res)
9078 (c-benign-error "\
9079 Error evaluating offset %S for %s: \
9080 Cannot combine relative offset %S with absolute %S in `%s' method"
9081 (car offset) symbol res val method)
9082 (setq res (vector (funcall method (aref res 0)
9083 (aref val 0)))))))
9084 (setq offset (cdr offset)))
9085 res))
9086
9087 ((eq (car offset) 'add)
9088 (let (res val)
9089 (setq offset (cdr offset))
9090 (while offset
9091 (setq val (c-evaluate-offset (car offset) langelem symbol))
9092 (cond
9093 ((not val))
9094 ((not res)
9095 (setq res val))
9096 ((integerp val)
9097 (if (vectorp res)
9098 (setq res (vector (+ (aref res 0) val)))
9099 (setq res (+ res val))))
9100 (t
9101 (if (vectorp res)
9102 (c-benign-error "\
9103 Error evaluating offset %S for %s: \
9104 Cannot combine absolute offsets %S and %S in `add' method"
9105 (car offset) symbol res val)
9106 (setq res val)))) ; Override.
9107 (setq offset (cdr offset)))
9108 res))
9109
9110 (t
9111 (let (res)
9112 (when (eq (car offset) 'first)
9113 (setq offset (cdr offset)))
9114 (while (and (not res) offset)
9115 (setq res (c-evaluate-offset (car offset) langelem symbol)
9116 offset (cdr offset)))
9117 res))))
9118
9119 ((and (symbolp offset) (boundp offset))
9120 (symbol-value offset))
9121
9122 (t
9123 (c-benign-error "Unknown offset format %S for %s" offset symbol)
9124 nil))))
9125
9126 (if (or (null res) (integerp res)
9127 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
9128 res
9129 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
9130 offset symbol res)
9131 nil)))
9132
9133 (defun c-calc-offset (langelem)
9134 ;; Get offset from LANGELEM which is a list beginning with the
9135 ;; syntactic symbol and followed by any analysis data it provides.
9136 ;; That data may be zero or more elements, but if at least one is
9137 ;; given then the first is the anchor position (or nil). The symbol
9138 ;; is matched against `c-offsets-alist' and the offset calculated
9139 ;; from that is returned.
9140 ;;
9141 ;; This function might do hidden buffer changes.
9142 (let* ((symbol (c-langelem-sym langelem))
9143 (match (assq symbol c-offsets-alist))
9144 (offset (cdr-safe match)))
9145 (if match
9146 (setq offset (c-evaluate-offset offset langelem symbol))
9147 (if c-strict-syntax-p
9148 (c-benign-error "No offset found for syntactic symbol %s" symbol))
9149 (setq offset 0))
9150 (if (vectorp offset)
9151 offset
9152 (or (and (numberp offset) offset)
9153 (and (symbolp offset) (symbol-value offset))
9154 0))
9155 ))
9156
9157 (defun c-get-offset (langelem)
9158 ;; This is a compatibility wrapper for `c-calc-offset' in case
9159 ;; someone is calling it directly. It takes an old style syntactic
9160 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
9161 ;; new list form.
9162 ;;
9163 ;; This function might do hidden buffer changes.
9164 (if (c-langelem-pos langelem)
9165 (c-calc-offset (list (c-langelem-sym langelem)
9166 (c-langelem-pos langelem)))
9167 (c-calc-offset langelem)))
9168
9169 (defun c-get-syntactic-indentation (langelems)
9170 ;; Calculate the syntactic indentation from a syntactic description
9171 ;; as returned by `c-guess-syntax'.
9172 ;;
9173 ;; Note that topmost-intro always has an anchor position at bol, for
9174 ;; historical reasons. It's often used together with other symbols
9175 ;; that has more sane positions. Since we always use the first
9176 ;; found anchor position, we rely on that these other symbols always
9177 ;; precede topmost-intro in the LANGELEMS list.
9178 ;;
9179 ;; This function might do hidden buffer changes.
9180 (let ((indent 0) anchor)
9181
9182 (while langelems
9183 (let* ((c-syntactic-element (car langelems))
9184 (res (c-calc-offset c-syntactic-element)))
9185
9186 (if (vectorp res)
9187 ;; Got an absolute column that overrides any indentation
9188 ;; we've collected so far, but not the relative
9189 ;; indentation we might get for the nested structures
9190 ;; further down the langelems list.
9191 (setq indent (elt res 0)
9192 anchor (point-min)) ; A position at column 0.
9193
9194 ;; Got a relative change of the current calculated
9195 ;; indentation.
9196 (setq indent (+ indent res))
9197
9198 ;; Use the anchor position from the first syntactic
9199 ;; element with one.
9200 (unless anchor
9201 (setq anchor (c-langelem-pos (car langelems)))))
9202
9203 (setq langelems (cdr langelems))))
9204
9205 (if anchor
9206 (+ indent (save-excursion
9207 (goto-char anchor)
9208 (current-column)))
9209 indent)))
9210
9211 \f
9212 (cc-provide 'cc-engine)
9213
9214 ;;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
9215 ;;; cc-engine.el ends here