]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-engine.el
merge trunk
[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-2012 Free Software Foundation, Inc.
4
5 ;; Authors: 2001- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
14 ;; Package: cc-mode
15
16 ;; This file is part of GNU Emacs.
17
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
22
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
27
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30
31 ;;; Commentary:
32
33 ;; The functions which have docstring documentation can be considered
34 ;; part of an API which other packages can use in CC Mode buffers.
35 ;; Otoh, undocumented functions and functions with the documentation
36 ;; in comments are considered purely internal and can change semantics
37 ;; or even disappear in the future.
38 ;;
39 ;; (This policy applies to CC Mode as a whole, not just this file. It
40 ;; probably also applies to many other Emacs packages, but here it's
41 ;; clearly spelled out.)
42
43 ;; Hidden buffer changes
44 ;;
45 ;; Various functions in CC Mode use text properties for caching and
46 ;; syntactic markup purposes, and those of them that might modify such
47 ;; properties but still don't modify the buffer in a visible way are
48 ;; said to do "hidden buffer changes". They should be used within
49 ;; `c-save-buffer-state' or a similar function that saves and restores
50 ;; buffer modifiedness, disables buffer change hooks, etc.
51 ;;
52 ;; Interactive functions are assumed to not do hidden buffer changes,
53 ;; except in the specific parts of them that do real changes.
54 ;;
55 ;; Lineup functions are assumed to do hidden buffer changes. They
56 ;; must not do real changes, though.
57 ;;
58 ;; All other functions that do hidden buffer changes have that noted
59 ;; in their doc string or comment.
60 ;;
61 ;; The intention with this system is to avoid wrapping every leaf
62 ;; function that do hidden buffer changes inside
63 ;; `c-save-buffer-state'. It should be used as near the top of the
64 ;; interactive functions as possible.
65 ;;
66 ;; Functions called during font locking are allowed to do hidden
67 ;; buffer changes since the font-lock package run them in a context
68 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
69 ;; inspired by `save-buffer-state' in the font-lock package).
70
71 ;; Use of text properties
72 ;;
73 ;; CC Mode uses several text properties internally to mark up various
74 ;; positions, e.g. to improve speed and to eliminate glitches in
75 ;; interactive refontification.
76 ;;
77 ;; Note: This doc is for internal use only. Other packages should not
78 ;; assume that these text properties are used as described here.
79 ;;
80 ;; 'category
81 ;; Used for "indirection". With its help, some other property can
82 ;; be cheaply and easily switched on or off everywhere it occurs.
83 ;;
84 ;; 'syntax-table
85 ;; Used to modify the syntax of some characters. It is used to
86 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
87 ;; to "hide" obtrusive characters in preprocessor lines.
88 ;;
89 ;; This property is used on single characters and is therefore
90 ;; always treated as front and rear nonsticky (or start and end open
91 ;; in XEmacs vocabulary). It's therefore installed on
92 ;; `text-property-default-nonsticky' if that variable exists (Emacs
93 ;; >= 21).
94 ;;
95 ;; 'c-is-sws and 'c-in-sws
96 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
97 ;; speed them up. See the comment blurb before `c-put-is-sws'
98 ;; below for further details.
99 ;;
100 ;; 'c-type
101 ;; This property is used on single characters to mark positions with
102 ;; special syntactic relevance of various sorts. Its primary use is
103 ;; to avoid glitches when multiline constructs are refontified
104 ;; interactively (on font lock decoration level 3). It's cleared in
105 ;; a region before it's fontified and is then put on relevant chars
106 ;; in that region as they are encountered during the fontification.
107 ;; The value specifies the kind of position:
108 ;;
109 ;; 'c-decl-arg-start
110 ;; Put on the last char of the token preceding each declaration
111 ;; inside a declaration style arglist (typically in a function
112 ;; prototype).
113 ;;
114 ;; 'c-decl-end
115 ;; Put on the last char of the token preceding a declaration.
116 ;; This is used in cases where declaration boundaries can't be
117 ;; recognized simply by looking for a token like ";" or "}".
118 ;; `c-type-decl-end-used' must be set if this is used (see also
119 ;; `c-find-decl-spots').
120 ;;
121 ;; 'c-<>-arg-sep
122 ;; Put on the commas that separate arguments in angle bracket
123 ;; arglists like C++ template arglists.
124 ;;
125 ;; 'c-decl-id-start and 'c-decl-type-start
126 ;; Put on the last char of the token preceding each declarator
127 ;; in the declarator list of a declaration. They are also used
128 ;; between the identifiers cases like enum declarations.
129 ;; 'c-decl-type-start is used when the declarators are types,
130 ;; 'c-decl-id-start otherwise.
131 ;;
132 ;; 'c-awk-NL-prop
133 ;; Used in AWK mode to mark the various kinds of newlines. See
134 ;; cc-awk.el.
135
136 ;;; Code:
137
138 (eval-when-compile
139 (let ((load-path
140 (if (and (boundp 'byte-compile-dest-file)
141 (stringp byte-compile-dest-file))
142 (cons (file-name-directory byte-compile-dest-file) load-path)
143 load-path)))
144 (load "cc-bytecomp" nil t)))
145
146 (cc-require 'cc-defs)
147 (cc-require-when-compile 'cc-langs)
148 (cc-require 'cc-vars)
149
150 ;; Silence the compiler.
151 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
152
153 \f
154 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
155
156 (defmacro c-declare-lang-variables ()
157 `(progn
158 ,@(apply 'nconc
159 (mapcar (lambda (init)
160 `(,(if (elt init 2)
161 `(defvar ,(car init) nil ,(elt init 2))
162 `(defvar ,(car init) nil))
163 (make-variable-buffer-local ',(car init))))
164 (cdr c-lang-variable-inits)))))
165 (c-declare-lang-variables)
166
167 \f
168 ;;; Internal state variables.
169
170 ;; Internal state of hungry delete key feature
171 (defvar c-hungry-delete-key nil)
172 (make-variable-buffer-local 'c-hungry-delete-key)
173
174 ;; The electric flag (toggled by `c-toggle-electric-state').
175 ;; If t, electric actions (like automatic reindentation, and (if
176 ;; c-auto-newline is also set) auto newlining) will happen when an electric
177 ;; key like `{' is pressed (or an electric keyword like `else').
178 (defvar c-electric-flag t)
179 (make-variable-buffer-local 'c-electric-flag)
180
181 ;; Internal state of auto newline feature.
182 (defvar c-auto-newline nil)
183 (make-variable-buffer-local 'c-auto-newline)
184
185 ;; Included in the mode line to indicate the active submodes.
186 ;; (defvar c-submode-indicators nil)
187 ;; (make-variable-buffer-local 'c-submode-indicators)
188
189 (defun c-calculate-state (arg prevstate)
190 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
191 ;; arg is nil or zero, toggle the state. If arg is negative, turn
192 ;; the state off, and if arg is positive, turn the state on
193 (if (or (not arg)
194 (zerop (setq arg (prefix-numeric-value arg))))
195 (not prevstate)
196 (> arg 0)))
197
198 \f
199 ;; Basic handling of preprocessor directives.
200
201 ;; This is a dynamically bound cache used together with
202 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
203 ;; works as long as point doesn't cross a macro boundary.
204 (defvar c-macro-start 'unknown)
205
206 (defsubst c-query-and-set-macro-start ()
207 (if (symbolp c-macro-start)
208 (setq c-macro-start (save-excursion
209 (c-save-buffer-state ()
210 (and (c-beginning-of-macro)
211 (point)))))
212 c-macro-start))
213
214 (defsubst c-query-macro-start ()
215 (if (symbolp c-macro-start)
216 (save-excursion
217 (c-save-buffer-state ()
218 (and (c-beginning-of-macro)
219 (point))))
220 c-macro-start))
221
222 (defun c-beginning-of-macro (&optional lim)
223 "Go to the beginning of a preprocessor directive.
224 Leave point at the beginning of the directive and return t if in one,
225 otherwise return nil and leave point unchanged.
226
227 Note that this function might do hidden buffer changes. See the
228 comment at the start of cc-engine.el for more info."
229 (when c-opt-cpp-prefix
230 (let ((here (point)))
231 (save-restriction
232 (if lim (narrow-to-region lim (point-max)))
233 (beginning-of-line)
234 (while (eq (char-before (1- (point))) ?\\)
235 (forward-line -1))
236 (back-to-indentation)
237 (if (and (<= (point) here)
238 (looking-at c-opt-cpp-start))
239 t
240 (goto-char here)
241 nil)))))
242
243 (defun c-end-of-macro ()
244 "Go to the end of a preprocessor directive.
245 More accurately, move the point to the end of the closest following
246 line that doesn't end with a line continuation backslash - no check is
247 done that the point is inside a cpp directive to begin with.
248
249 Note that this function might do hidden buffer changes. See the
250 comment at the start of cc-engine.el for more info."
251 (while (progn
252 (end-of-line)
253 (when (and (eq (char-before) ?\\)
254 (not (eobp)))
255 (forward-char)
256 t))))
257
258 (defun c-syntactic-end-of-macro ()
259 ;; Go to the end of a CPP directive, or a "safe" pos just before.
260 ;;
261 ;; This is normally the end of the next non-escaped line. A "safe"
262 ;; position is one not within a string or comment. (The EOL on a line
263 ;; comment is NOT "safe").
264 ;;
265 ;; This function must only be called from the beginning of a CPP construct.
266 ;;
267 ;; Note that this function might do hidden buffer changes. See the comment
268 ;; at the start of cc-engine.el for more info.
269 (let* ((here (point))
270 (there (progn (c-end-of-macro) (point)))
271 (s (parse-partial-sexp here there)))
272 (while (and (or (nth 3 s) ; in a string
273 (nth 4 s)) ; in a comment (maybe at end of line comment)
274 (> there here)) ; No infinite loops, please.
275 (setq there (1- (nth 8 s)))
276 (setq s (parse-partial-sexp here there)))
277 (point)))
278
279 (defun c-forward-over-cpp-define-id ()
280 ;; Assuming point is at the "#" that introduces a preprocessor
281 ;; directive, it's moved forward to the end of the identifier which is
282 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
283 ;; is returned in this case, in all other cases nil is returned and
284 ;; point isn't moved.
285 ;;
286 ;; This function might do hidden buffer changes.
287 (when (and c-opt-cpp-macro-define-id
288 (looking-at c-opt-cpp-macro-define-id))
289 (goto-char (match-end 0))))
290
291 (defun c-forward-to-cpp-define-body ()
292 ;; Assuming point is at the "#" that introduces a preprocessor
293 ;; directive, it's moved forward to the start of the definition body
294 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
295 ;; specifies). Non-nil is returned in this case, in all other cases
296 ;; nil is returned and point isn't moved.
297 ;;
298 ;; This function might do hidden buffer changes.
299 (when (and c-opt-cpp-macro-define-start
300 (looking-at c-opt-cpp-macro-define-start)
301 (not (= (match-end 0) (c-point 'eol))))
302 (goto-char (match-end 0))))
303
304 \f
305 ;;; Basic utility functions.
306
307 (defun c-syntactic-content (from to paren-level)
308 ;; Return the given region as a string where all syntactic
309 ;; whitespace is removed or, where necessary, replaced with a single
310 ;; space. If PAREN-LEVEL is given then all parens in the region are
311 ;; collapsed to "()", "[]" etc.
312 ;;
313 ;; This function might do hidden buffer changes.
314
315 (save-excursion
316 (save-restriction
317 (narrow-to-region from to)
318 (goto-char from)
319 (let* ((parts (list nil)) (tail parts) pos in-paren)
320
321 (while (re-search-forward c-syntactic-ws-start to t)
322 (goto-char (setq pos (match-beginning 0)))
323 (c-forward-syntactic-ws)
324 (if (= (point) pos)
325 (forward-char)
326
327 (when paren-level
328 (save-excursion
329 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
330 pos (point))))
331
332 (if (and (> pos from)
333 (< (point) to)
334 (looking-at "\\w\\|\\s_")
335 (save-excursion
336 (goto-char (1- pos))
337 (looking-at "\\w\\|\\s_")))
338 (progn
339 (setcdr tail (list (buffer-substring-no-properties from pos)
340 " "))
341 (setq tail (cddr tail)))
342 (setcdr tail (list (buffer-substring-no-properties from pos)))
343 (setq tail (cdr tail)))
344
345 (when in-paren
346 (when (= (car (parse-partial-sexp pos to -1)) -1)
347 (setcdr tail (list (buffer-substring-no-properties
348 (1- (point)) (point))))
349 (setq tail (cdr tail))))
350
351 (setq from (point))))
352
353 (setcdr tail (list (buffer-substring-no-properties from to)))
354 (apply 'concat (cdr parts))))))
355
356 (defun c-shift-line-indentation (shift-amt)
357 ;; Shift the indentation of the current line with the specified
358 ;; amount (positive inwards). The buffer is modified only if
359 ;; SHIFT-AMT isn't equal to zero.
360 (let ((pos (- (point-max) (point)))
361 (c-macro-start c-macro-start)
362 tmp-char-inserted)
363 (if (zerop shift-amt)
364 nil
365 ;; If we're on an empty line inside a macro, we take the point
366 ;; to be at the current indentation and shift it to the
367 ;; appropriate column. This way we don't treat the extra
368 ;; whitespace out to the line continuation as indentation.
369 (when (and (c-query-and-set-macro-start)
370 (looking-at "[ \t]*\\\\$")
371 (save-excursion
372 (skip-chars-backward " \t")
373 (bolp)))
374 (insert ?x)
375 (backward-char)
376 (setq tmp-char-inserted t))
377 (unwind-protect
378 (let ((col (current-indentation)))
379 (delete-region (c-point 'bol) (c-point 'boi))
380 (beginning-of-line)
381 (indent-to (+ col shift-amt)))
382 (when tmp-char-inserted
383 (delete-char 1))))
384 ;; If initial point was within line's indentation and we're not on
385 ;; a line with a line continuation in a macro, position after the
386 ;; indentation. Else stay at same point in text.
387 (if (and (< (point) (c-point 'boi))
388 (not tmp-char-inserted))
389 (back-to-indentation)
390 (if (> (- (point-max) pos) (point))
391 (goto-char (- (point-max) pos))))))
392
393 (defsubst c-keyword-sym (keyword)
394 ;; Return non-nil if the string KEYWORD is a known keyword. More
395 ;; precisely, the value is the symbol for the keyword in
396 ;; `c-keywords-obarray'.
397 (intern-soft keyword c-keywords-obarray))
398
399 (defsubst c-keyword-member (keyword-sym lang-constant)
400 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
401 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
402 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
403 ;; nil then the result is nil.
404 (get keyword-sym lang-constant))
405
406 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
407 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
408 "\"|"
409 "\""))
410
411 ;; Regexp matching string limit syntax.
412 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
413 "\\s\"\\|\\s|"
414 "\\s\""))
415
416 ;; Regexp matching WS followed by string limit syntax.
417 (defconst c-ws*-string-limit-regexp
418 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
419
420 ;; Holds formatted error strings for the few cases where parse errors
421 ;; are reported.
422 (defvar c-parsing-error nil)
423 (make-variable-buffer-local 'c-parsing-error)
424
425 (defun c-echo-parsing-error (&optional quiet)
426 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
427 (c-benign-error "%s" c-parsing-error))
428 c-parsing-error)
429
430 ;; Faces given to comments and string literals. This is used in some
431 ;; situations to speed up recognition; it isn't mandatory that font
432 ;; locking is in use. This variable is extended with the face in
433 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
434 (defvar c-literal-faces
435 (append '(font-lock-comment-face font-lock-string-face)
436 (when (facep 'font-lock-comment-delimiter-face)
437 ;; New in Emacs 22.
438 '(font-lock-comment-delimiter-face))))
439
440 (defsubst c-put-c-type-property (pos value)
441 ;; Put a c-type property with the given value at POS.
442 (c-put-char-property pos 'c-type value))
443
444 (defun c-clear-c-type-property (from to value)
445 ;; Remove all occurrences of the c-type property that has the given
446 ;; value in the region between FROM and TO. VALUE is assumed to not
447 ;; be nil.
448 ;;
449 ;; Note: This assumes that c-type is put on single chars only; it's
450 ;; very inefficient if matching properties cover large regions.
451 (save-excursion
452 (goto-char from)
453 (while (progn
454 (when (eq (get-text-property (point) 'c-type) value)
455 (c-clear-char-property (point) 'c-type))
456 (goto-char (next-single-property-change (point) 'c-type nil to))
457 (< (point) to)))))
458
459 \f
460 ;; Some debug tools to visualize various special positions. This
461 ;; debug code isn't as portable as the rest of CC Mode.
462
463 (cc-bytecomp-defun overlays-in)
464 (cc-bytecomp-defun overlay-get)
465 (cc-bytecomp-defun overlay-start)
466 (cc-bytecomp-defun overlay-end)
467 (cc-bytecomp-defun delete-overlay)
468 (cc-bytecomp-defun overlay-put)
469 (cc-bytecomp-defun make-overlay)
470
471 (defun c-debug-add-face (beg end face)
472 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
473 (while overlays
474 (setq overlay (car overlays)
475 overlays (cdr overlays))
476 (when (eq (overlay-get overlay 'face) face)
477 (setq beg (min beg (overlay-start overlay))
478 end (max end (overlay-end overlay)))
479 (delete-overlay overlay)))
480 (overlay-put (make-overlay beg end) 'face face)))
481
482 (defun c-debug-remove-face (beg end face)
483 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
484 (ol-beg beg) (ol-end end))
485 (while overlays
486 (setq overlay (car overlays)
487 overlays (cdr overlays))
488 (when (eq (overlay-get overlay 'face) face)
489 (setq ol-beg (min ol-beg (overlay-start overlay))
490 ol-end (max ol-end (overlay-end overlay)))
491 (delete-overlay overlay)))
492 (when (< ol-beg beg)
493 (overlay-put (make-overlay ol-beg beg) 'face face))
494 (when (> ol-end end)
495 (overlay-put (make-overlay end ol-end) 'face face))))
496
497 \f
498 ;; `c-beginning-of-statement-1' and accompanying stuff.
499
500 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
501 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
502 ;; better way should be implemented, but this will at least shut up
503 ;; the byte compiler.
504 (defvar c-maybe-labelp)
505
506 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
507
508 ;; Macros used internally in c-beginning-of-statement-1 for the
509 ;; automaton actions.
510 (defmacro c-bos-push-state ()
511 '(setq stack (cons (cons state saved-pos)
512 stack)))
513 (defmacro c-bos-pop-state (&optional do-if-done)
514 `(if (setq state (car (car stack))
515 saved-pos (cdr (car stack))
516 stack (cdr stack))
517 t
518 ,do-if-done
519 (throw 'loop nil)))
520 (defmacro c-bos-pop-state-and-retry ()
521 '(throw 'loop (setq state (car (car stack))
522 saved-pos (cdr (car stack))
523 ;; Throw nil if stack is empty, else throw non-nil.
524 stack (cdr stack))))
525 (defmacro c-bos-save-pos ()
526 '(setq saved-pos (vector pos tok ptok pptok)))
527 (defmacro c-bos-restore-pos ()
528 '(unless (eq (elt saved-pos 0) start)
529 (setq pos (elt saved-pos 0)
530 tok (elt saved-pos 1)
531 ptok (elt saved-pos 2)
532 pptok (elt saved-pos 3))
533 (goto-char pos)
534 (setq sym nil)))
535 (defmacro c-bos-save-error-info (missing got)
536 `(setq saved-pos (vector pos ,missing ,got)))
537 (defmacro c-bos-report-error ()
538 '(unless noerror
539 (setq c-parsing-error
540 (format "No matching `%s' found for `%s' on line %d"
541 (elt saved-pos 1)
542 (elt saved-pos 2)
543 (1+ (count-lines (point-min)
544 (c-point 'bol (elt saved-pos 0))))))))
545
546 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
547 noerror comma-delim)
548 "Move to the start of the current statement or declaration, or to
549 the previous one if already at the beginning of one. Only
550 statements/declarations on the same level are considered, i.e. don't
551 move into or out of sexps (not even normal expression parentheses).
552
553 If point is already at the earliest statement within braces or parens,
554 this function doesn't move back into any whitespace preceding it; it
555 returns 'same in this case.
556
557 Stop at statement continuation tokens like \"else\", \"catch\",
558 \"finally\" and the \"while\" in \"do ... while\" if the start point
559 is within the continuation. If starting at such a token, move to the
560 corresponding statement start. If at the beginning of a statement,
561 move to the closest containing statement if there is any. This might
562 also stop at a continuation clause.
563
564 Labels are treated as part of the following statements if
565 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
566 statement start keyword.) Otherwise, each label is treated as a
567 separate statement.
568
569 Macros are ignored \(i.e. skipped over) unless point is within one, in
570 which case the content of the macro is treated as normal code. Aside
571 from any normal statement starts found in it, stop at the first token
572 of the content in the macro, i.e. the expression of an \"#if\" or the
573 start of the definition in a \"#define\". Also stop at start of
574 macros before leaving them.
575
576 Return:
577 'label if stopped at a label or \"case...:\" or \"default:\";
578 'same if stopped at the beginning of the current statement;
579 'up if stepped to a containing statement;
580 'previous if stepped to a preceding statement;
581 'beginning if stepped from a statement continuation clause to
582 its start clause; or
583 'macro if stepped to a macro start.
584 Note that 'same and not 'label is returned if stopped at the same
585 label without crossing the colon character.
586
587 LIM may be given to limit the search. If the search hits the limit,
588 point will be left at the closest following token, or at the start
589 position if that is less ('same is returned in this case).
590
591 NOERROR turns off error logging to `c-parsing-error'.
592
593 Normally only ';' and virtual semicolons are considered to delimit
594 statements, but if COMMA-DELIM is non-nil then ',' is treated
595 as a delimiter too.
596
597 Note that this function might do hidden buffer changes. See the
598 comment at the start of cc-engine.el for more info."
599
600 ;; The bulk of this function is a pushdown automaton that looks at statement
601 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
602 ;; purpose is to keep track of nested statements, ensuring that such
603 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
604 ;; does with nested braces/brackets/parentheses).
605 ;;
606 ;; Note: The position of a boundary is the following token.
607 ;;
608 ;; Beginning with the current token (the one following point), move back one
609 ;; sexp at a time (where a sexp is, more or less, either a token or the
610 ;; entire contents of a brace/bracket/paren pair). Each time a statement
611 ;; boundary is crossed or a "while"-like token is found, update the state of
612 ;; the PDA. Stop at the beginning of a statement when the stack (holding
613 ;; nested statement info) is empty and the position has been moved.
614 ;;
615 ;; The following variables constitute the PDA:
616 ;;
617 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
618 ;; scanned back over, 'boundary if we've just gone back over a
619 ;; statement boundary, or nil otherwise.
620 ;; state: takes one of the values (nil else else-boundary while
621 ;; while-boundary catch catch-boundary).
622 ;; nil means "no "while"-like token yet scanned".
623 ;; 'else, for example, means "just gone back over an else".
624 ;; 'else-boundary means "just gone back over a statement boundary
625 ;; immediately after having gone back over an else".
626 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
627 ;; of error reporting information.
628 ;; stack: The stack onto which the PDA pushes its state. Each entry
629 ;; consists of a saved value of state and saved-pos. An entry is
630 ;; pushed when we move back over a "continuation" token (e.g. else)
631 ;; and popped when we encounter the corresponding opening token
632 ;; (e.g. if).
633 ;;
634 ;;
635 ;; The following diagram briefly outlines the PDA.
636 ;;
637 ;; Common state:
638 ;; "else": Push state, goto state `else'.
639 ;; "while": Push state, goto state `while'.
640 ;; "catch" or "finally": Push state, goto state `catch'.
641 ;; boundary: Pop state.
642 ;; other: Do nothing special.
643 ;;
644 ;; State `else':
645 ;; boundary: Goto state `else-boundary'.
646 ;; other: Error, pop state, retry token.
647 ;;
648 ;; State `else-boundary':
649 ;; "if": Pop state.
650 ;; boundary: Error, pop state.
651 ;; other: See common state.
652 ;;
653 ;; State `while':
654 ;; boundary: Save position, goto state `while-boundary'.
655 ;; other: Pop state, retry token.
656 ;;
657 ;; State `while-boundary':
658 ;; "do": Pop state.
659 ;; boundary: Restore position if it's not at start, pop state. [*see below]
660 ;; other: See common state.
661 ;;
662 ;; State `catch':
663 ;; boundary: Goto state `catch-boundary'.
664 ;; other: Error, pop state, retry token.
665 ;;
666 ;; State `catch-boundary':
667 ;; "try": Pop state.
668 ;; "catch": Goto state `catch'.
669 ;; boundary: Error, pop state.
670 ;; other: See common state.
671 ;;
672 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
673 ;; searching for a "do" which would have opened a do-while. If we didn't
674 ;; find it, we discard the analysis done since the "while", go back to this
675 ;; token in the buffer and restart the scanning there, this time WITHOUT
676 ;; pushing the 'while state onto the stack.
677 ;;
678 ;; In addition to the above there is some special handling of labels
679 ;; and macros.
680
681 (let ((case-fold-search nil)
682 (start (point))
683 macro-start
684 (delims (if comma-delim '(?\; ?,) '(?\;)))
685 (c-stmt-delim-chars (if comma-delim
686 c-stmt-delim-chars-with-comma
687 c-stmt-delim-chars))
688 c-in-literal-cache c-maybe-labelp after-case:-pos saved
689 ;; Current position.
690 pos
691 ;; Position of last stmt boundary character (e.g. ;).
692 boundary-pos
693 ;; The position of the last sexp or bound that follows the
694 ;; first found colon, i.e. the start of the nonlabel part of
695 ;; the statement. It's `start' if a colon is found just after
696 ;; the start.
697 after-labels-pos
698 ;; Like `after-labels-pos', but the first such position inside
699 ;; a label, i.e. the start of the last label before the start
700 ;; of the nonlabel part of the statement.
701 last-label-pos
702 ;; The last position where a label is possible provided the
703 ;; statement started there. It's nil as long as no invalid
704 ;; label content has been found (according to
705 ;; `c-nonlabel-token-key'). It's `start' if no valid label
706 ;; content was found in the label. Note that we might still
707 ;; regard it a label if it starts with `c-label-kwds'.
708 label-good-pos
709 ;; Putative positions of the components of a bitfield declaration,
710 ;; e.g. "int foo : NUM_FOO_BITS ;"
711 bitfield-type-pos bitfield-id-pos bitfield-size-pos
712 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
713 ;; See above.
714 sym
715 ;; Current state in the automaton. See above.
716 state
717 ;; Current saved positions. See above.
718 saved-pos
719 ;; Stack of conses (state . saved-pos).
720 stack
721 ;; Regexp which matches "for", "if", etc.
722 (cond-key (or c-opt-block-stmt-key
723 "\\<\\>")) ; Matches nothing.
724 ;; Return value.
725 (ret 'same)
726 ;; Positions of the last three sexps or bounds we've stopped at.
727 tok ptok pptok)
728
729 (save-restriction
730 (if lim (narrow-to-region lim (point-max)))
731
732 (if (save-excursion
733 (and (c-beginning-of-macro)
734 (/= (point) start)))
735 (setq macro-start (point)))
736
737 ;; Try to skip back over unary operator characters, to register
738 ;; that we've moved.
739 (while (progn
740 (setq pos (point))
741 (c-backward-syntactic-ws)
742 ;; Protect post-++/-- operators just before a virtual semicolon.
743 (and (not (c-at-vsemi-p))
744 (/= (skip-chars-backward "-+!*&~@`#") 0))))
745
746 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
747 ;; done. Later on we ignore the boundaries for statements that don't
748 ;; contain any sexp. The only thing that is affected is that the error
749 ;; checking is a little less strict, and we really don't bother.
750 (if (and (memq (char-before) delims)
751 (progn (forward-char -1)
752 (setq saved (point))
753 (c-backward-syntactic-ws)
754 (or (memq (char-before) delims)
755 (memq (char-before) '(?: nil))
756 (eq (char-syntax (char-before)) ?\()
757 (c-at-vsemi-p))))
758 (setq ret 'previous
759 pos saved)
760
761 ;; Begin at start and not pos to detect macros if we stand
762 ;; directly after the #.
763 (goto-char start)
764 (if (looking-at "\\<\\|\\W")
765 ;; Record this as the first token if not starting inside it.
766 (setq tok start))
767
768
769 ;; The following while loop goes back one sexp (balanced parens,
770 ;; etc. with contents, or symbol or suchlike) each iteration. This
771 ;; movement is accomplished with a call to c-backward-sexp approx 170
772 ;; lines below.
773 ;;
774 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
775 ;; 1. On reaching the start of a macro;
776 ;; 2. On having passed a stmt boundary with the PDA stack empty;
777 ;; 3. On reaching the start of an Objective C method def;
778 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
779 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
780 (while
781 (catch 'loop ;; Throw nil to break, non-nil to continue.
782 (cond
783 ;; Are we in a macro, just after the opening #?
784 ((save-excursion
785 (and macro-start ; Always NIL for AWK.
786 (progn (skip-chars-backward " \t")
787 (eq (char-before) ?#))
788 (progn (setq saved (1- (point)))
789 (beginning-of-line)
790 (not (eq (char-before (1- (point))) ?\\)))
791 (looking-at c-opt-cpp-start)
792 (progn (skip-chars-forward " \t")
793 (eq (point) saved))))
794 (goto-char saved)
795 (if (and (c-forward-to-cpp-define-body)
796 (progn (c-forward-syntactic-ws start)
797 (< (point) start)))
798 ;; Stop at the first token in the content of the macro.
799 (setq pos (point)
800 ignore-labels t) ; Avoid the label check on exit.
801 (setq pos saved
802 ret 'macro
803 ignore-labels t))
804 (throw 'loop nil)) ; 1. Start of macro.
805
806 ;; Do a round through the automaton if we've just passed a
807 ;; statement boundary or passed a "while"-like token.
808 ((or sym
809 (and (looking-at cond-key)
810 (setq sym (intern (match-string 1)))))
811
812 (when (and (< pos start) (null stack))
813 (throw 'loop nil)) ; 2. Statement boundary.
814
815 ;; The PDA state handling.
816 ;;
817 ;; Refer to the description of the PDA in the opening
818 ;; comments. In the following OR form, the first leaf
819 ;; attempts to handles one of the specific actions detailed
820 ;; (e.g., finding token "if" whilst in state `else-boundary').
821 ;; We drop through to the second leaf (which handles common
822 ;; state) if no specific handler is found in the first cond.
823 ;; If a parsing error is detected (e.g. an "else" with no
824 ;; preceding "if"), we throw to the enclosing catch.
825 ;;
826 ;; Note that the (eq state 'else) means
827 ;; "we've just passed an else", NOT "we're looking for an
828 ;; else".
829 (or (cond
830 ((eq state 'else)
831 (if (eq sym 'boundary)
832 (setq state 'else-boundary)
833 (c-bos-report-error)
834 (c-bos-pop-state-and-retry)))
835
836 ((eq state 'else-boundary)
837 (cond ((eq sym 'if)
838 (c-bos-pop-state (setq ret 'beginning)))
839 ((eq sym 'boundary)
840 (c-bos-report-error)
841 (c-bos-pop-state))))
842
843 ((eq state 'while)
844 (if (and (eq sym 'boundary)
845 ;; Since this can cause backtracking we do a
846 ;; little more careful analysis to avoid it:
847 ;; If there's a label in front of the while
848 ;; it can't be part of a do-while.
849 (not after-labels-pos))
850 (progn (c-bos-save-pos)
851 (setq state 'while-boundary))
852 (c-bos-pop-state-and-retry))) ; Can't be a do-while
853
854 ((eq state 'while-boundary)
855 (cond ((eq sym 'do)
856 (c-bos-pop-state (setq ret 'beginning)))
857 ((eq sym 'boundary) ; isn't a do-while
858 (c-bos-restore-pos) ; the position of the while
859 (c-bos-pop-state)))) ; no longer searching for do.
860
861 ((eq state 'catch)
862 (if (eq sym 'boundary)
863 (setq state 'catch-boundary)
864 (c-bos-report-error)
865 (c-bos-pop-state-and-retry)))
866
867 ((eq state 'catch-boundary)
868 (cond
869 ((eq sym 'try)
870 (c-bos-pop-state (setq ret 'beginning)))
871 ((eq sym 'catch)
872 (setq state 'catch))
873 ((eq sym 'boundary)
874 (c-bos-report-error)
875 (c-bos-pop-state)))))
876
877 ;; This is state common. We get here when the previous
878 ;; cond statement found no particular state handler.
879 (cond ((eq sym 'boundary)
880 ;; If we have a boundary at the start
881 ;; position we push a frame to go to the
882 ;; previous statement.
883 (if (>= pos start)
884 (c-bos-push-state)
885 (c-bos-pop-state)))
886 ((eq sym 'else)
887 (c-bos-push-state)
888 (c-bos-save-error-info 'if 'else)
889 (setq state 'else))
890 ((eq sym 'while)
891 ;; Is this a real while, or a do-while?
892 ;; The next `when' triggers unless we are SURE that
893 ;; the `while' is not the tail end of a `do-while'.
894 (when (or (not pptok)
895 (memq (char-after pptok) delims)
896 ;; The following kludge is to prevent
897 ;; infinite recursion when called from
898 ;; c-awk-after-if-for-while-condition-p,
899 ;; or the like.
900 (and (eq (point) start)
901 (c-vsemi-status-unknown-p))
902 (c-at-vsemi-p pptok))
903 ;; Since this can cause backtracking we do a
904 ;; little more careful analysis to avoid it: If
905 ;; the while isn't followed by a (possibly
906 ;; virtual) semicolon it can't be a do-while.
907 (c-bos-push-state)
908 (setq state 'while)))
909 ((memq sym '(catch finally))
910 (c-bos-push-state)
911 (c-bos-save-error-info 'try sym)
912 (setq state 'catch))))
913
914 (when c-maybe-labelp
915 ;; We're either past a statement boundary or at the
916 ;; start of a statement, so throw away any label data
917 ;; for the previous one.
918 (setq after-labels-pos nil
919 last-label-pos nil
920 c-maybe-labelp nil))))
921
922 ;; Step to the previous sexp, but not if we crossed a
923 ;; boundary, since that doesn't consume an sexp.
924 (if (eq sym 'boundary)
925 (setq ret 'previous)
926
927 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
928 ;; BACKWARDS THROUGH THE SOURCE.
929
930 (c-backward-syntactic-ws)
931 (let ((before-sws-pos (point))
932 ;; The end position of the area to search for statement
933 ;; barriers in this round.
934 (maybe-after-boundary-pos pos))
935
936 ;; Go back over exactly one logical sexp, taking proper
937 ;; account of macros and escaped EOLs.
938 (while
939 (progn
940 (unless (c-safe (c-backward-sexp) t)
941 ;; Give up if we hit an unbalanced block. Since the
942 ;; stack won't be empty the code below will report a
943 ;; suitable error.
944 (throw 'loop nil))
945 (cond
946 ;; Have we moved into a macro?
947 ((and (not macro-start)
948 (c-beginning-of-macro))
949 ;; Have we crossed a statement boundary? If not,
950 ;; keep going back until we find one or a "real" sexp.
951 (and
952 (save-excursion
953 (c-end-of-macro)
954 (not (c-crosses-statement-barrier-p
955 (point) maybe-after-boundary-pos)))
956 (setq maybe-after-boundary-pos (point))))
957 ;; Have we just gone back over an escaped NL? This
958 ;; doesn't count as a sexp.
959 ((looking-at "\\\\$")))))
960
961 ;; Have we crossed a statement boundary?
962 (setq boundary-pos
963 (cond
964 ;; Are we at a macro beginning?
965 ((and (not macro-start)
966 c-opt-cpp-prefix
967 (looking-at c-opt-cpp-prefix))
968 (save-excursion
969 (c-end-of-macro)
970 (c-crosses-statement-barrier-p
971 (point) maybe-after-boundary-pos)))
972 ;; Just gone back over a brace block?
973 ((and
974 (eq (char-after) ?{)
975 (not (c-looking-at-inexpr-block lim nil t)))
976 (save-excursion
977 (c-forward-sexp) (point)))
978 ;; Just gone back over some paren block?
979 ((looking-at "\\s\(")
980 (save-excursion
981 (goto-char (1+ (c-down-list-backward
982 before-sws-pos)))
983 (c-crosses-statement-barrier-p
984 (point) maybe-after-boundary-pos)))
985 ;; Just gone back over an ordinary symbol of some sort?
986 (t (c-crosses-statement-barrier-p
987 (point) maybe-after-boundary-pos))))
988
989 (when boundary-pos
990 (setq pptok ptok
991 ptok tok
992 tok boundary-pos
993 sym 'boundary)
994 ;; Like a C "continue". Analyze the next sexp.
995 (throw 'loop t))))
996
997 ;; ObjC method def?
998 (when (and c-opt-method-key
999 (setq saved (c-in-method-def-p)))
1000 (setq pos saved
1001 ignore-labels t) ; Avoid the label check on exit.
1002 (throw 'loop nil)) ; 3. ObjC method def.
1003
1004 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1005 (if c-has-bitfields
1006 (cond
1007 ;; The : <size> and <id> fields?
1008 ((and (numberp c-maybe-labelp)
1009 (not bitfield-size-pos)
1010 (save-excursion
1011 (goto-char (or tok start))
1012 (not (looking-at c-keywords-regexp)))
1013 (not (looking-at c-keywords-regexp))
1014 (not (c-punctuation-in (point) c-maybe-labelp)))
1015 (setq bitfield-size-pos (or tok start)
1016 bitfield-id-pos (point)))
1017 ;; The <type> field?
1018 ((and bitfield-id-pos
1019 (not bitfield-type-pos))
1020 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1021 (not (looking-at c-not-primitive-type-keywords-regexp))
1022 (not (c-punctuation-in (point) tok)))
1023 (setq bitfield-type-pos (point))
1024 (setq bitfield-size-pos nil
1025 bitfield-id-pos nil)))))
1026
1027 ;; Handle labels.
1028 (unless (eq ignore-labels t)
1029 (when (numberp c-maybe-labelp)
1030 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1031 ;; might be in a label now. Have we got a real label
1032 ;; (including a case label) or something like C++'s "public:"?
1033 ;; A case label might use an expression rather than a token.
1034 (setq after-case:-pos (or tok start))
1035 (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1036 ;; Catch C++'s inheritance construct "class foo : bar".
1037 (save-excursion
1038 (and
1039 (c-safe (c-backward-sexp) t)
1040 (looking-at c-nonlabel-token-2-key))))
1041 (setq c-maybe-labelp nil)
1042 (if after-labels-pos ; Have we already encountered a label?
1043 (if (not last-label-pos)
1044 (setq last-label-pos (or tok start)))
1045 (setq after-labels-pos (or tok start)))
1046 (setq c-maybe-labelp t
1047 label-good-pos nil))) ; bogus "label"
1048
1049 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1050 ; been found.
1051 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1052 ;; We're in a potential label and it's the first
1053 ;; time we've found something that isn't allowed in
1054 ;; one.
1055 (setq label-good-pos (or tok start))))
1056
1057 ;; We've moved back by a sexp, so update the token positions.
1058 (setq sym nil
1059 pptok ptok
1060 ptok tok
1061 tok (point)
1062 pos tok) ; always non-nil
1063 ) ; end of (catch loop ....)
1064 ) ; end of sexp-at-a-time (while ....)
1065
1066 ;; If the stack isn't empty there might be errors to report.
1067 (while stack
1068 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1069 (c-bos-report-error))
1070 (setq saved-pos (cdr (car stack))
1071 stack (cdr stack)))
1072
1073 (when (and (eq ret 'same)
1074 (not (memq sym '(boundary ignore nil))))
1075 ;; Need to investigate closer whether we've crossed
1076 ;; between a substatement and its containing statement.
1077 (if (setq saved (if (looking-at c-block-stmt-1-key)
1078 ptok
1079 pptok))
1080 (cond ((> start saved) (setq pos saved))
1081 ((= start saved) (setq ret 'up)))))
1082
1083 (when (and (not ignore-labels)
1084 (eq c-maybe-labelp t)
1085 (not (eq ret 'beginning))
1086 after-labels-pos
1087 (not bitfield-type-pos) ; Bitfields take precedence over labels.
1088 (or (not label-good-pos)
1089 (<= label-good-pos pos)
1090 (progn
1091 (goto-char (if (and last-label-pos
1092 (< last-label-pos start))
1093 last-label-pos
1094 pos))
1095 (looking-at c-label-kwds-regexp))))
1096 ;; We're in a label. Maybe we should step to the statement
1097 ;; after it.
1098 (if (< after-labels-pos start)
1099 (setq pos after-labels-pos)
1100 (setq ret 'label)
1101 (if (and last-label-pos (< last-label-pos start))
1102 ;; Might have jumped over several labels. Go to the last one.
1103 (setq pos last-label-pos)))))
1104
1105 ;; Have we got "case <expression>:"?
1106 (goto-char pos)
1107 (when (and after-case:-pos
1108 (not (eq ret 'beginning))
1109 (looking-at c-case-kwds-regexp))
1110 (if (< after-case:-pos start)
1111 (setq pos after-case:-pos))
1112 (if (eq ret 'same)
1113 (setq ret 'label)))
1114
1115 ;; Skip over the unary operators that can start the statement.
1116 (while (progn
1117 (c-backward-syntactic-ws)
1118 ;; protect AWK post-inc/decrement operators, etc.
1119 (and (not (c-at-vsemi-p (point)))
1120 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1121 (setq pos (point)))
1122 (goto-char pos)
1123 ret)))
1124
1125 (defun c-punctuation-in (from to)
1126 "Return non-nil if there is a non-comment non-macro punctuation character
1127 between FROM and TO. FROM must not be in a string or comment. The returned
1128 value is the position of the first such character."
1129 (save-excursion
1130 (goto-char from)
1131 (let ((pos (point)))
1132 (while (progn (skip-chars-forward c-symbol-chars to)
1133 (c-forward-syntactic-ws to)
1134 (> (point) pos))
1135 (setq pos (point))))
1136 (and (< (point) to) (point))))
1137
1138 (defun c-crosses-statement-barrier-p (from to)
1139 "Return non-nil if buffer positions FROM to TO cross one or more
1140 statement or declaration boundaries. The returned value is actually
1141 the position of the earliest boundary char. FROM must not be within
1142 a string or comment.
1143
1144 The variable `c-maybe-labelp' is set to the position of the first `:' that
1145 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1146 single `?' is found, then `c-maybe-labelp' is cleared.
1147
1148 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1149 regarded as having a \"virtual semicolon\" immediately after the last token on
1150 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1151
1152 Note that this function might do hidden buffer changes. See the
1153 comment at the start of cc-engine.el for more info."
1154 (let* ((skip-chars
1155 ;; If the current language has CPP macros, insert # into skip-chars.
1156 (if c-opt-cpp-symbol
1157 (concat (substring c-stmt-delim-chars 0 1) ; "^"
1158 c-opt-cpp-symbol ; usually "#"
1159 (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
1160 c-stmt-delim-chars))
1161 (non-skip-list
1162 (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1163 lit-range vsemi-pos)
1164 (save-restriction
1165 (widen)
1166 (save-excursion
1167 (catch 'done
1168 (goto-char from)
1169 (while (progn (skip-chars-forward
1170 skip-chars
1171 (min to (c-point 'bonl)))
1172 (< (point) to))
1173 (cond
1174 ;; Virtual semicolon?
1175 ((and (bolp)
1176 (save-excursion
1177 (progn
1178 (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1179 (goto-char (car lit-range)))
1180 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1181 (setq vsemi-pos (point))
1182 (c-at-vsemi-p))))
1183 (throw 'done vsemi-pos))
1184 ;; In a string/comment?
1185 ((setq lit-range (c-literal-limits))
1186 (goto-char (cdr lit-range)))
1187 ((eq (char-after) ?:)
1188 (forward-char)
1189 (if (and (eq (char-after) ?:)
1190 (< (point) to))
1191 ;; Ignore scope operators.
1192 (forward-char)
1193 (setq c-maybe-labelp (1- (point)))))
1194 ((eq (char-after) ??)
1195 ;; A question mark. Can't be a label, so stop
1196 ;; looking for more : and ?.
1197 (setq c-maybe-labelp nil
1198 skip-chars (substring c-stmt-delim-chars 0 -2)))
1199 ;; At a CPP construct?
1200 ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol)
1201 (save-excursion
1202 (forward-line 0)
1203 (looking-at c-opt-cpp-prefix)))
1204 (c-end-of-macro))
1205 ((memq (char-after) non-skip-list)
1206 (throw 'done (point)))))
1207 ;; In trailing space after an as yet undetected virtual semicolon?
1208 (c-backward-syntactic-ws from)
1209 (if (and (< (point) to)
1210 (c-at-vsemi-p))
1211 (point)
1212 nil))))))
1213
1214 (defun c-at-statement-start-p ()
1215 "Return non-nil if the point is at the first token in a statement
1216 or somewhere in the syntactic whitespace before it.
1217
1218 A \"statement\" here is not restricted to those inside code blocks.
1219 Any kind of declaration-like construct that occur outside function
1220 bodies is also considered a \"statement\".
1221
1222 Note that this function might do hidden buffer changes. See the
1223 comment at the start of cc-engine.el for more info."
1224
1225 (save-excursion
1226 (let ((end (point))
1227 c-maybe-labelp)
1228 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1229 (or (bobp)
1230 (eq (char-before) ?})
1231 (and (eq (char-before) ?{)
1232 (not (and c-special-brace-lists
1233 (progn (backward-char)
1234 (c-looking-at-special-brace-list)))))
1235 (c-crosses-statement-barrier-p (point) end)))))
1236
1237 (defun c-at-expression-start-p ()
1238 "Return non-nil if the point is at the first token in an expression or
1239 statement, or somewhere in the syntactic whitespace before it.
1240
1241 An \"expression\" here is a bit different from the normal language
1242 grammar sense: It's any sequence of expression tokens except commas,
1243 unless they are enclosed inside parentheses of some kind. Also, an
1244 expression never continues past an enclosing parenthesis, but it might
1245 contain parenthesis pairs of any sort except braces.
1246
1247 Since expressions never cross statement boundaries, this function also
1248 recognizes statement beginnings, just like `c-at-statement-start-p'.
1249
1250 Note that this function might do hidden buffer changes. See the
1251 comment at the start of cc-engine.el for more info."
1252
1253 (save-excursion
1254 (let ((end (point))
1255 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1256 c-maybe-labelp)
1257 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1258 (or (bobp)
1259 (memq (char-before) '(?{ ?}))
1260 (save-excursion (backward-char)
1261 (looking-at "\\s("))
1262 (c-crosses-statement-barrier-p (point) end)))))
1263
1264 \f
1265 ;; A set of functions that covers various idiosyncrasies in
1266 ;; implementations of `forward-comment'.
1267
1268 ;; Note: Some emacsen considers incorrectly that any line comment
1269 ;; ending with a backslash continues to the next line. I can't think
1270 ;; of any way to work around that in a reliable way without changing
1271 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1272 ;; changing the syntax for backslash doesn't work since we must treat
1273 ;; escapes in string literals correctly.)
1274
1275 (defun c-forward-single-comment ()
1276 "Move forward past whitespace and the closest following comment, if any.
1277 Return t if a comment was found, nil otherwise. In either case, the
1278 point is moved past the following whitespace. Line continuations,
1279 i.e. a backslashes followed by line breaks, are treated as whitespace.
1280 The line breaks that end line comments are considered to be the
1281 comment enders, so the point will be put on the beginning of the next
1282 line if it moved past a line comment.
1283
1284 This function does not do any hidden buffer changes."
1285
1286 (let ((start (point)))
1287 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1288 (goto-char (match-end 0)))
1289
1290 (when (forward-comment 1)
1291 (if (eobp)
1292 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1293 ;; forwards at eob.
1294 nil
1295
1296 ;; Emacs includes the ending newline in a b-style (c++)
1297 ;; comment, but XEmacs doesn't. We depend on the Emacs
1298 ;; behavior (which also is symmetric).
1299 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1300 (condition-case nil (forward-char 1)))
1301
1302 t))))
1303
1304 (defsubst c-forward-comments ()
1305 "Move forward past all following whitespace and comments.
1306 Line continuations, i.e. a backslashes followed by line breaks, are
1307 treated as whitespace.
1308
1309 Note that this function might do hidden buffer changes. See the
1310 comment at the start of cc-engine.el for more info."
1311
1312 (while (or
1313 ;; If forward-comment in at least XEmacs 21 is given a large
1314 ;; positive value, it'll loop all the way through if it hits
1315 ;; eob.
1316 (and (forward-comment 5)
1317 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1318 ;; forwards at eob.
1319 (not (eobp)))
1320
1321 (when (looking-at "\\\\[\n\r]")
1322 (forward-char 2)
1323 t))))
1324
1325 (defun c-backward-single-comment ()
1326 "Move backward past whitespace and the closest preceding comment, if any.
1327 Return t if a comment was found, nil otherwise. In either case, the
1328 point is moved past the preceding whitespace. Line continuations,
1329 i.e. a backslashes followed by line breaks, are treated as whitespace.
1330 The line breaks that end line comments are considered to be the
1331 comment enders, so the point cannot be at the end of the same line to
1332 move over a line comment.
1333
1334 This function does not do any hidden buffer changes."
1335
1336 (let ((start (point)))
1337 ;; When we got newline terminated comments, forward-comment in all
1338 ;; supported emacsen so far will stop at eol of each line not
1339 ;; ending with a comment when moving backwards. This corrects for
1340 ;; that, and at the same time handles line continuations.
1341 (while (progn
1342 (skip-chars-backward " \t\n\r\f\v")
1343 (and (looking-at "[\n\r]")
1344 (eq (char-before) ?\\)))
1345 (backward-char))
1346
1347 (if (bobp)
1348 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1349 ;; backwards at bob.
1350 nil
1351
1352 ;; Leave point after the closest following newline if we've
1353 ;; backed up over any above, since forward-comment won't move
1354 ;; backward over a line comment if point is at the end of the
1355 ;; same line.
1356 (re-search-forward "\\=\\s *[\n\r]" start t)
1357
1358 (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
1359 (if (eolp)
1360 ;; If forward-comment above succeeded and we're at eol
1361 ;; then the newline we moved over above didn't end a
1362 ;; line comment, so we give it another go.
1363 (let (open-paren-in-column-0-is-defun-start)
1364 (forward-comment -1))
1365 t))
1366
1367 ;; Emacs <= 20 and XEmacs move back over the closer of a
1368 ;; block comment that lacks an opener.
1369 (if (looking-at "\\*/")
1370 (progn (forward-char 2) nil)
1371 t)))))
1372
1373 (defsubst c-backward-comments ()
1374 "Move backward past all preceding whitespace and comments.
1375 Line continuations, i.e. a backslashes followed by line breaks, are
1376 treated as whitespace. The line breaks that end line comments are
1377 considered to be the comment enders, so the point cannot be at the end
1378 of the same line to move over a line comment. Unlike
1379 c-backward-syntactic-ws, this function doesn't move back over
1380 preprocessor directives.
1381
1382 Note that this function might do hidden buffer changes. See the
1383 comment at the start of cc-engine.el for more info."
1384
1385 (let ((start (point)))
1386 (while (and
1387 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1388 ;; return t when moving backwards at bob.
1389 (not (bobp))
1390
1391 (if (let (open-paren-in-column-0-is-defun-start)
1392 (forward-comment -1))
1393 (if (looking-at "\\*/")
1394 ;; Emacs <= 20 and XEmacs move back over the
1395 ;; closer of a block comment that lacks an opener.
1396 (progn (forward-char 2) nil)
1397 t)
1398
1399 ;; XEmacs treats line continuations as whitespace but
1400 ;; only in the backward direction, which seems a bit
1401 ;; odd. Anyway, this is necessary for Emacs.
1402 (when (and (looking-at "[\n\r]")
1403 (eq (char-before) ?\\)
1404 (< (point) start))
1405 (backward-char)
1406 t))))))
1407
1408 \f
1409 ;; Tools for skipping over syntactic whitespace.
1410
1411 ;; The following functions use text properties to cache searches over
1412 ;; large regions of syntactic whitespace. It works as follows:
1413 ;;
1414 ;; o If a syntactic whitespace region contains anything but simple
1415 ;; whitespace (i.e. space, tab and line breaks), the text property
1416 ;; `c-in-sws' is put over it. At places where we have stopped
1417 ;; within that region there's also a `c-is-sws' text property.
1418 ;; That since there typically are nested whitespace inside that
1419 ;; must be handled separately, e.g. whitespace inside a comment or
1420 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1421 ;; to jump to another point with that property within the same
1422 ;; `c-in-sws' region. It can be likened to a ladder where
1423 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1424 ;;
1425 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1426 ;; a "rung position" and also maybe on the first following char.
1427 ;; As many characters as can be conveniently found in this range
1428 ;; are marked, but no assumption can be made that the whole range
1429 ;; is marked (it could be clobbered by later changes, for
1430 ;; instance).
1431 ;;
1432 ;; Note that some part of the beginning of a sequence of simple
1433 ;; whitespace might be part of the end of a preceding line comment
1434 ;; or cpp directive and must not be considered part of the "rung".
1435 ;; Such whitespace is some amount of horizontal whitespace followed
1436 ;; by a newline. In the case of cpp directives it could also be
1437 ;; two newlines with horizontal whitespace between them.
1438 ;;
1439 ;; The reason to include the first following char is to cope with
1440 ;; "rung positions" that doesn't have any ordinary whitespace. If
1441 ;; `c-is-sws' is put on a token character it does not have
1442 ;; `c-in-sws' set simultaneously. That's the only case when that
1443 ;; can occur, and the reason for not extending the `c-in-sws'
1444 ;; region to cover it is that the `c-in-sws' region could then be
1445 ;; accidentally merged with a following one if the token is only
1446 ;; one character long.
1447 ;;
1448 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1449 ;; removed in the changed region. If the change was inside
1450 ;; syntactic whitespace that means that the "ladder" is broken, but
1451 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1452 ;; parts on either side and use an ordinary search only to "repair"
1453 ;; the gap.
1454 ;;
1455 ;; Special care needs to be taken if a region is removed: If there
1456 ;; are `c-in-sws' on both sides of it which do not connect inside
1457 ;; the region then they can't be joined. If e.g. a marked macro is
1458 ;; broken, syntactic whitespace inside the new text might be
1459 ;; marked. If those marks would become connected with the old
1460 ;; `c-in-sws' range around the macro then we could get a ladder
1461 ;; with one end outside the macro and the other at some whitespace
1462 ;; within it.
1463 ;;
1464 ;; The main motivation for this system is to increase the speed in
1465 ;; skipping over the large whitespace regions that can occur at the
1466 ;; top level in e.g. header files that contain a lot of comments and
1467 ;; cpp directives. For small comments inside code it's probably
1468 ;; slower than using `forward-comment' straightforwardly, but speed is
1469 ;; not a significant factor there anyway.
1470
1471 ; (defface c-debug-is-sws-face
1472 ; '((t (:background "GreenYellow")))
1473 ; "Debug face to mark the `c-is-sws' property.")
1474 ; (defface c-debug-in-sws-face
1475 ; '((t (:underline t)))
1476 ; "Debug face to mark the `c-in-sws' property.")
1477
1478 ; (defun c-debug-put-sws-faces ()
1479 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1480 ; ;; properties in the buffer.
1481 ; (interactive)
1482 ; (save-excursion
1483 ; (c-save-buffer-state (in-face)
1484 ; (goto-char (point-min))
1485 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1486 ; (point)))
1487 ; (while (progn
1488 ; (goto-char (next-single-property-change
1489 ; (point) 'c-is-sws nil (point-max)))
1490 ; (if in-face
1491 ; (progn
1492 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1493 ; (setq in-face nil))
1494 ; (setq in-face (point)))
1495 ; (not (eobp))))
1496 ; (goto-char (point-min))
1497 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1498 ; (point)))
1499 ; (while (progn
1500 ; (goto-char (next-single-property-change
1501 ; (point) 'c-in-sws nil (point-max)))
1502 ; (if in-face
1503 ; (progn
1504 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1505 ; (setq in-face nil))
1506 ; (setq in-face (point)))
1507 ; (not (eobp)))))))
1508
1509 (defmacro c-debug-sws-msg (&rest args)
1510 ;;`(message ,@args)
1511 )
1512
1513 (defmacro c-put-is-sws (beg end)
1514 ;; This macro does a hidden buffer change.
1515 `(let ((beg ,beg) (end ,end))
1516 (put-text-property beg end 'c-is-sws t)
1517 ,@(when (facep 'c-debug-is-sws-face)
1518 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1519
1520 (defmacro c-put-in-sws (beg end)
1521 ;; This macro does a hidden buffer change.
1522 `(let ((beg ,beg) (end ,end))
1523 (put-text-property beg end 'c-in-sws t)
1524 ,@(when (facep 'c-debug-is-sws-face)
1525 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1526
1527 (defmacro c-remove-is-sws (beg end)
1528 ;; This macro does a hidden buffer change.
1529 `(let ((beg ,beg) (end ,end))
1530 (remove-text-properties beg end '(c-is-sws nil))
1531 ,@(when (facep 'c-debug-is-sws-face)
1532 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1533
1534 (defmacro c-remove-in-sws (beg end)
1535 ;; This macro does a hidden buffer change.
1536 `(let ((beg ,beg) (end ,end))
1537 (remove-text-properties beg end '(c-in-sws nil))
1538 ,@(when (facep 'c-debug-is-sws-face)
1539 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1540
1541 (defmacro c-remove-is-and-in-sws (beg end)
1542 ;; This macro does a hidden buffer change.
1543 `(let ((beg ,beg) (end ,end))
1544 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1545 ,@(when (facep 'c-debug-is-sws-face)
1546 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1547 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1548
1549 (defsubst c-invalidate-sws-region-after (beg end)
1550 ;; Called from `after-change-functions'. Note that if
1551 ;; `c-forward-sws' or `c-backward-sws' are used outside
1552 ;; `c-save-buffer-state' or similar then this will remove the cache
1553 ;; properties right after they're added.
1554 ;;
1555 ;; This function does hidden buffer changes.
1556
1557 (save-excursion
1558 ;; Adjust the end to remove the properties in any following simple
1559 ;; ws up to and including the next line break, if there is any
1560 ;; after the changed region. This is necessary e.g. when a rung
1561 ;; marked empty line is converted to a line comment by inserting
1562 ;; "//" before the line break. In that case the line break would
1563 ;; keep the rung mark which could make a later `c-backward-sws'
1564 ;; move into the line comment instead of over it.
1565 (goto-char end)
1566 (skip-chars-forward " \t\f\v")
1567 (when (and (eolp) (not (eobp)))
1568 (setq end (1+ (point)))))
1569
1570 (when (and (= beg end)
1571 (get-text-property beg 'c-in-sws)
1572 (> beg (point-min))
1573 (get-text-property (1- beg) 'c-in-sws))
1574 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1575 ;; safe to keep a range that was continuous before the change. E.g:
1576 ;;
1577 ;; #define foo
1578 ;; \
1579 ;; bar
1580 ;;
1581 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1582 ;; after "foo" is removed then "bar" will become part of the cpp
1583 ;; directive instead of a syntactically relevant token. In that
1584 ;; case there's no longer syntactic ws from "#" to "b".
1585 (setq beg (1- beg)))
1586
1587 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1588 (c-remove-is-and-in-sws beg end))
1589
1590 (defun c-forward-sws ()
1591 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1592 ;;
1593 ;; This function might do hidden buffer changes.
1594
1595 (let (;; `rung-pos' is set to a position as early as possible in the
1596 ;; unmarked part of the simple ws region.
1597 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1598 rung-is-marked next-rung-is-marked simple-ws-end
1599 ;; `safe-start' is set when it's safe to cache the start position.
1600 ;; It's not set if we've initially skipped over comments and line
1601 ;; continuations since we might have gone out through the end of a
1602 ;; macro then. This provision makes `c-forward-sws' not populate the
1603 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1604 ;; more common.
1605 safe-start)
1606
1607 ;; Skip simple ws and do a quick check on the following character to see
1608 ;; if it's anything that can't start syntactic ws, so we can bail out
1609 ;; early in the majority of cases when there just are a few ws chars.
1610 (skip-chars-forward " \t\n\r\f\v")
1611 (when (looking-at c-syntactic-ws-start)
1612
1613 (setq rung-end-pos (min (1+ (point)) (point-max)))
1614 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1615 'c-is-sws t))
1616 ;; Find the last rung position to avoid setting properties in all
1617 ;; the cases when the marked rung is complete.
1618 ;; (`next-single-property-change' is certain to move at least one
1619 ;; step forward.)
1620 (setq rung-pos (1- (next-single-property-change
1621 rung-is-marked 'c-is-sws nil rung-end-pos)))
1622 ;; Got no marked rung here. Since the simple ws might have started
1623 ;; inside a line comment or cpp directive we must set `rung-pos' as
1624 ;; high as possible.
1625 (setq rung-pos (point)))
1626
1627 (while
1628 (progn
1629 (while
1630 (when (and rung-is-marked
1631 (get-text-property (point) 'c-in-sws))
1632
1633 ;; The following search is the main reason that `c-in-sws'
1634 ;; and `c-is-sws' aren't combined to one property.
1635 (goto-char (next-single-property-change
1636 (point) 'c-in-sws nil (point-max)))
1637 (unless (get-text-property (point) 'c-is-sws)
1638 ;; If the `c-in-sws' region extended past the last
1639 ;; `c-is-sws' char we have to go back a bit.
1640 (or (get-text-property (1- (point)) 'c-is-sws)
1641 (goto-char (previous-single-property-change
1642 (point) 'c-is-sws)))
1643 (backward-char))
1644
1645 (c-debug-sws-msg
1646 "c-forward-sws cached move %s -> %s (max %s)"
1647 rung-pos (point) (point-max))
1648
1649 (setq rung-pos (point))
1650 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1651 (not (eobp))))
1652
1653 ;; We'll loop here if there is simple ws after the last rung.
1654 ;; That means that there's been some change in it and it's
1655 ;; possible that we've stepped into another ladder, so extend
1656 ;; the previous one to join with it if there is one, and try to
1657 ;; use the cache again.
1658 (c-debug-sws-msg
1659 "c-forward-sws extending rung with [%s..%s] (max %s)"
1660 (1+ rung-pos) (1+ (point)) (point-max))
1661 (unless (get-text-property (point) 'c-is-sws)
1662 ;; Remove any `c-in-sws' property from the last char of
1663 ;; the rung before we mark it with `c-is-sws', so that we
1664 ;; won't connect with the remains of a broken "ladder".
1665 (c-remove-in-sws (point) (1+ (point))))
1666 (c-put-is-sws (1+ rung-pos)
1667 (1+ (point)))
1668 (c-put-in-sws rung-pos
1669 (setq rung-pos (point)
1670 last-put-in-sws-pos rung-pos)))
1671
1672 (setq simple-ws-end (point))
1673 (c-forward-comments)
1674
1675 (cond
1676 ((/= (point) simple-ws-end)
1677 ;; Skipped over comments. Don't cache at eob in case the buffer
1678 ;; is narrowed.
1679 (not (eobp)))
1680
1681 ((save-excursion
1682 (and c-opt-cpp-prefix
1683 (looking-at c-opt-cpp-start)
1684 (progn (skip-chars-backward " \t")
1685 (bolp))
1686 (or (bobp)
1687 (progn (backward-char)
1688 (not (eq (char-before) ?\\))))))
1689 ;; Skip a preprocessor directive.
1690 (end-of-line)
1691 (while (and (eq (char-before) ?\\)
1692 (= (forward-line 1) 0))
1693 (end-of-line))
1694 (forward-line 1)
1695 (setq safe-start t)
1696 ;; Don't cache at eob in case the buffer is narrowed.
1697 (not (eobp)))))
1698
1699 ;; We've searched over a piece of non-white syntactic ws. See if this
1700 ;; can be cached.
1701 (setq next-rung-pos (point))
1702 (skip-chars-forward " \t\n\r\f\v")
1703 (setq rung-end-pos (min (1+ (point)) (point-max)))
1704
1705 (if (or
1706 ;; Cache if we haven't skipped comments only, and if we started
1707 ;; either from a marked rung or from a completely uncached
1708 ;; position.
1709 (and safe-start
1710 (or rung-is-marked
1711 (not (get-text-property simple-ws-end 'c-in-sws))))
1712
1713 ;; See if there's a marked rung in the encountered simple ws. If
1714 ;; so then we can cache, unless `safe-start' is nil. Even then
1715 ;; we need to do this to check if the cache can be used for the
1716 ;; next step.
1717 (and (setq next-rung-is-marked
1718 (text-property-any next-rung-pos rung-end-pos
1719 'c-is-sws t))
1720 safe-start))
1721
1722 (progn
1723 (c-debug-sws-msg
1724 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1725 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1726 (point-max))
1727
1728 ;; Remove the properties for any nested ws that might be cached.
1729 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1730 ;; anyway.
1731 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1732 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1733 (c-put-is-sws rung-pos
1734 (1+ simple-ws-end))
1735 (setq rung-is-marked t))
1736 (c-put-in-sws rung-pos
1737 (setq rung-pos (point)
1738 last-put-in-sws-pos rung-pos))
1739 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1740 ;; Remove any `c-in-sws' property from the last char of
1741 ;; the rung before we mark it with `c-is-sws', so that we
1742 ;; won't connect with the remains of a broken "ladder".
1743 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1744 (c-put-is-sws next-rung-pos
1745 rung-end-pos))
1746
1747 (c-debug-sws-msg
1748 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1749 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1750 (point-max))
1751
1752 ;; Set `rung-pos' for the next rung. It's the same thing here as
1753 ;; initially, except that the rung position is set as early as
1754 ;; possible since we can't be in the ending ws of a line comment or
1755 ;; cpp directive now.
1756 (if (setq rung-is-marked next-rung-is-marked)
1757 (setq rung-pos (1- (next-single-property-change
1758 rung-is-marked 'c-is-sws nil rung-end-pos)))
1759 (setq rung-pos next-rung-pos))
1760 (setq safe-start t)))
1761
1762 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1763 ;; another one after the point (which might occur when editing inside a
1764 ;; comment or macro).
1765 (when (eq last-put-in-sws-pos (point))
1766 (cond ((< last-put-in-sws-pos (point-max))
1767 (c-debug-sws-msg
1768 "c-forward-sws clearing at %s for cache separation"
1769 last-put-in-sws-pos)
1770 (c-remove-in-sws last-put-in-sws-pos
1771 (1+ last-put-in-sws-pos)))
1772 (t
1773 ;; If at eob we have to clear the last character before the end
1774 ;; instead since the buffer might be narrowed and there might
1775 ;; be a `c-in-sws' after (point-max). In this case it's
1776 ;; necessary to clear both properties.
1777 (c-debug-sws-msg
1778 "c-forward-sws clearing thoroughly at %s for cache separation"
1779 (1- last-put-in-sws-pos))
1780 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1781 last-put-in-sws-pos))))
1782 )))
1783
1784 (defun c-backward-sws ()
1785 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1786 ;;
1787 ;; This function might do hidden buffer changes.
1788
1789 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1790 ;; part of the simple ws region.
1791 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1792 rung-is-marked simple-ws-beg cmt-skip-pos)
1793
1794 ;; Skip simple horizontal ws and do a quick check on the preceding
1795 ;; character to see if it's anything that can't end syntactic ws, so we can
1796 ;; bail out early in the majority of cases when there just are a few ws
1797 ;; chars. Newlines are complicated in the backward direction, so we can't
1798 ;; skip over them.
1799 (skip-chars-backward " \t\f")
1800 (when (and (not (bobp))
1801 (save-excursion
1802 (backward-char)
1803 (looking-at c-syntactic-ws-end)))
1804
1805 ;; Try to find a rung position in the simple ws preceding point, so that
1806 ;; we can get a cache hit even if the last bit of the simple ws has
1807 ;; changed recently.
1808 (setq simple-ws-beg (point))
1809 (skip-chars-backward " \t\n\r\f\v")
1810 (if (setq rung-is-marked (text-property-any
1811 (point) (min (1+ rung-pos) (point-max))
1812 'c-is-sws t))
1813 ;; `rung-pos' will be the earliest marked position, which means that
1814 ;; there might be later unmarked parts in the simple ws region.
1815 ;; It's not worth the effort to fix that; the last part of the
1816 ;; simple ws is also typically edited often, so it could be wasted.
1817 (goto-char (setq rung-pos rung-is-marked))
1818 (goto-char simple-ws-beg))
1819
1820 (while
1821 (progn
1822 (while
1823 (when (and rung-is-marked
1824 (not (bobp))
1825 (get-text-property (1- (point)) 'c-in-sws))
1826
1827 ;; The following search is the main reason that `c-in-sws'
1828 ;; and `c-is-sws' aren't combined to one property.
1829 (goto-char (previous-single-property-change
1830 (point) 'c-in-sws nil (point-min)))
1831 (unless (get-text-property (point) 'c-is-sws)
1832 ;; If the `c-in-sws' region extended past the first
1833 ;; `c-is-sws' char we have to go forward a bit.
1834 (goto-char (next-single-property-change
1835 (point) 'c-is-sws)))
1836
1837 (c-debug-sws-msg
1838 "c-backward-sws cached move %s <- %s (min %s)"
1839 (point) rung-pos (point-min))
1840
1841 (setq rung-pos (point))
1842 (if (and (< (min (skip-chars-backward " \t\f\v")
1843 (progn
1844 (setq simple-ws-beg (point))
1845 (skip-chars-backward " \t\n\r\f\v")))
1846 0)
1847 (setq rung-is-marked
1848 (text-property-any (point) rung-pos
1849 'c-is-sws t)))
1850 t
1851 (goto-char simple-ws-beg)
1852 nil))
1853
1854 ;; We'll loop here if there is simple ws before the first rung.
1855 ;; That means that there's been some change in it and it's
1856 ;; possible that we've stepped into another ladder, so extend
1857 ;; the previous one to join with it if there is one, and try to
1858 ;; use the cache again.
1859 (c-debug-sws-msg
1860 "c-backward-sws extending rung with [%s..%s] (min %s)"
1861 rung-is-marked rung-pos (point-min))
1862 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1863 ;; Remove any `c-in-sws' property from the last char of
1864 ;; the rung before we mark it with `c-is-sws', so that we
1865 ;; won't connect with the remains of a broken "ladder".
1866 (c-remove-in-sws (1- rung-pos) rung-pos))
1867 (c-put-is-sws rung-is-marked
1868 rung-pos)
1869 (c-put-in-sws rung-is-marked
1870 (1- rung-pos))
1871 (setq rung-pos rung-is-marked
1872 last-put-in-sws-pos rung-pos))
1873
1874 (c-backward-comments)
1875 (setq cmt-skip-pos (point))
1876
1877 (cond
1878 ((and c-opt-cpp-prefix
1879 (/= cmt-skip-pos simple-ws-beg)
1880 (c-beginning-of-macro))
1881 ;; Inside a cpp directive. See if it should be skipped over.
1882 (let ((cpp-beg (point)))
1883
1884 ;; Move back over all line continuations in the region skipped
1885 ;; over by `c-backward-comments'. If we go past it then we
1886 ;; started inside the cpp directive.
1887 (goto-char simple-ws-beg)
1888 (beginning-of-line)
1889 (while (and (> (point) cmt-skip-pos)
1890 (progn (backward-char)
1891 (eq (char-before) ?\\)))
1892 (beginning-of-line))
1893
1894 (if (< (point) cmt-skip-pos)
1895 ;; Don't move past the cpp directive if we began inside
1896 ;; it. Note that the position at the end of the last line
1897 ;; of the macro is also considered to be within it.
1898 (progn (goto-char cmt-skip-pos)
1899 nil)
1900
1901 ;; It's worthwhile to spend a little bit of effort on finding
1902 ;; the end of the macro, to get a good `simple-ws-beg'
1903 ;; position for the cache. Note that `c-backward-comments'
1904 ;; could have stepped over some comments before going into
1905 ;; the macro, and then `simple-ws-beg' must be kept on the
1906 ;; same side of those comments.
1907 (goto-char simple-ws-beg)
1908 (skip-chars-backward " \t\n\r\f\v")
1909 (if (eq (char-before) ?\\)
1910 (forward-char))
1911 (forward-line 1)
1912 (if (< (point) simple-ws-beg)
1913 ;; Might happen if comments after the macro were skipped
1914 ;; over.
1915 (setq simple-ws-beg (point)))
1916
1917 (goto-char cpp-beg)
1918 t)))
1919
1920 ((/= (save-excursion
1921 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1922 (setq next-rung-pos (point)))
1923 simple-ws-beg)
1924 ;; Skipped over comments. Must put point at the end of
1925 ;; the simple ws at point since we might be after a line
1926 ;; comment or cpp directive that's been partially
1927 ;; narrowed out, and we can't risk marking the simple ws
1928 ;; at the end of it.
1929 (goto-char next-rung-pos)
1930 t)))
1931
1932 ;; We've searched over a piece of non-white syntactic ws. See if this
1933 ;; can be cached.
1934 (setq next-rung-pos (point))
1935 (skip-chars-backward " \t\f\v")
1936
1937 (if (or
1938 ;; Cache if we started either from a marked rung or from a
1939 ;; completely uncached position.
1940 rung-is-marked
1941 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1942
1943 ;; Cache if there's a marked rung in the encountered simple ws.
1944 (save-excursion
1945 (skip-chars-backward " \t\n\r\f\v")
1946 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1947 'c-is-sws t)))
1948
1949 (progn
1950 (c-debug-sws-msg
1951 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1952 (point) (1+ next-rung-pos)
1953 simple-ws-beg (min (1+ rung-pos) (point-max))
1954 (point-min))
1955
1956 ;; Remove the properties for any nested ws that might be cached.
1957 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1958 ;; anyway.
1959 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1960 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1961 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1962 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1963 ;; Remove any `c-in-sws' property from the last char of
1964 ;; the rung before we mark it with `c-is-sws', so that we
1965 ;; won't connect with the remains of a broken "ladder".
1966 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1967 (c-put-is-sws simple-ws-beg
1968 rung-end-pos)
1969 (setq rung-is-marked t)))
1970 (c-put-in-sws (setq simple-ws-beg (point)
1971 last-put-in-sws-pos simple-ws-beg)
1972 rung-pos)
1973 (c-put-is-sws (setq rung-pos simple-ws-beg)
1974 (1+ next-rung-pos)))
1975
1976 (c-debug-sws-msg
1977 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1978 (point) (1+ next-rung-pos)
1979 simple-ws-beg (min (1+ rung-pos) (point-max))
1980 (point-min))
1981 (setq rung-pos next-rung-pos
1982 simple-ws-beg (point))
1983 ))
1984
1985 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1986 ;; another one before the point (which might occur when editing inside a
1987 ;; comment or macro).
1988 (when (eq last-put-in-sws-pos (point))
1989 (cond ((< (point-min) last-put-in-sws-pos)
1990 (c-debug-sws-msg
1991 "c-backward-sws clearing at %s for cache separation"
1992 (1- last-put-in-sws-pos))
1993 (c-remove-in-sws (1- last-put-in-sws-pos)
1994 last-put-in-sws-pos))
1995 ((> (point-min) 1)
1996 ;; If at bob and the buffer is narrowed, we have to clear the
1997 ;; character we're standing on instead since there might be a
1998 ;; `c-in-sws' before (point-min). In this case it's necessary
1999 ;; to clear both properties.
2000 (c-debug-sws-msg
2001 "c-backward-sws clearing thoroughly at %s for cache separation"
2002 last-put-in-sws-pos)
2003 (c-remove-is-and-in-sws last-put-in-sws-pos
2004 (1+ last-put-in-sws-pos)))))
2005 )))
2006
2007 \f
2008 ;; Other whitespace tools
2009 (defun c-partial-ws-p (beg end)
2010 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2011 ;; region? This is a "heuristic" function. .....
2012 ;;
2013 ;; The motivation for the second bit is to check whether removing this
2014 ;; region would coalesce two symbols.
2015 ;;
2016 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2017 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2018 (save-excursion
2019 (let ((end+1 (min (1+ end) (point-max))))
2020 (or (progn (goto-char (max (point-min) (1- beg)))
2021 (c-skip-ws-forward end)
2022 (eq (point) end))
2023 (progn (goto-char beg)
2024 (c-skip-ws-forward end+1)
2025 (eq (point) end+1))))))
2026 \f
2027 ;; A system for finding noteworthy parens before the point.
2028
2029 (defconst c-state-cache-too-far 5000)
2030 ;; A maximum comfortable scanning distance, e.g. between
2031 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2032 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2033 ;; the cache and starting again from point-min or a beginning of defun. This
2034 ;; value can be tuned for efficiency or set to a lower value for testing.
2035
2036 (defvar c-state-cache nil)
2037 (make-variable-buffer-local 'c-state-cache)
2038 ;; The state cache used by `c-parse-state' to cut down the amount of
2039 ;; searching. It's the result from some earlier `c-parse-state' call. See
2040 ;; `c-parse-state''s doc string for details of its structure.
2041 ;;
2042 ;; The use of the cached info is more effective if the next
2043 ;; `c-parse-state' call is on a line close by the one the cached state
2044 ;; was made at; the cache can actually slow down a little if the
2045 ;; cached state was made very far back in the buffer. The cache is
2046 ;; most effective if `c-parse-state' is used on each line while moving
2047 ;; forward.
2048
2049 (defvar c-state-cache-good-pos 1)
2050 (make-variable-buffer-local 'c-state-cache-good-pos)
2051 ;; This is a position where `c-state-cache' is known to be correct, or
2052 ;; nil (see below). It's a position inside one of the recorded unclosed
2053 ;; parens or the top level, but not further nested inside any literal or
2054 ;; subparen that is closed before the last recorded position.
2055 ;;
2056 ;; The exact position is chosen to try to be close to yet earlier than
2057 ;; the position where `c-state-cache' will be called next. Right now
2058 ;; the heuristic is to set it to the position after the last found
2059 ;; closing paren (of any type) before the line on which
2060 ;; `c-parse-state' was called. That is chosen primarily to work well
2061 ;; with refontification of the current line.
2062 ;;
2063 ;; 2009-07-28: When `c-state-point-min' and the last position where
2064 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2065 ;; both in the same literal, there is no such "good position", and
2066 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2067 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2068 ;;
2069 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2070 ;; the middle of the desert, as long as it is not within a brace pair
2071 ;; recorded in `c-state-cache' or a paren/bracket pair.
2072
2073
2074 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2075 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2076 ;; speed up testing for non-literality.
2077 (defconst c-state-nonlit-pos-interval 3000)
2078 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2079
2080 (defvar c-state-nonlit-pos-cache nil)
2081 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2082 ;; A list of buffer positions which are known not to be in a literal or a cpp
2083 ;; construct. This is ordered with higher positions at the front of the list.
2084 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2085
2086 (defvar c-state-nonlit-pos-cache-limit 1)
2087 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2088 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2089 ;; reduced by buffer changes, and increased by invocations of
2090 ;; `c-state-literal-at'.
2091
2092 (defsubst c-state-pp-to-literal (from to)
2093 ;; Do a parse-partial-sexp from FROM to TO, returning either
2094 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2095 ;; (STATE) otherwise,
2096 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2097 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal.
2098 ;;
2099 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2100 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2101 ;; STATE are valid.
2102 (save-excursion
2103 (let ((s (parse-partial-sexp from to))
2104 ty)
2105 (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
2106 (setq ty (cond
2107 ((nth 3 s) 'string)
2108 ((eq (nth 7 s) t) 'c++)
2109 (t 'c)))
2110 (parse-partial-sexp (point) (point-max)
2111 nil ; TARGETDEPTH
2112 nil ; STOPBEFORE
2113 s ; OLDSTATE
2114 'syntax-table)) ; stop at end of literal
2115 (if ty
2116 `(,s ,ty (,(nth 8 s) . ,(point)))
2117 `(,s)))))
2118
2119 (defun c-state-safe-place (here)
2120 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2121 ;; string, comment, or macro.
2122 ;;
2123 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2124 ;; MAY NOT contain any positions within macros, since macros are frequently
2125 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2126 ;; We cannot rely on this mechanism whilst determining a cache pos since
2127 ;; this function is also called from outwith `c-parse-state'.
2128 (save-restriction
2129 (widen)
2130 (save-excursion
2131 (let ((c c-state-nonlit-pos-cache)
2132 pos npos lit macro-beg)
2133 ;; Trim the cache to take account of buffer changes.
2134 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2135 (setq c (cdr c)))
2136 (setq c-state-nonlit-pos-cache c)
2137
2138 (while (and c (> (car c) here))
2139 (setq c (cdr c)))
2140 (setq pos (or (car c) (point-min)))
2141
2142 (while
2143 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2144 (and
2145 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2146 (progn
2147 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2148 (cond
2149 ((null lit)
2150 (setq pos npos)
2151 t)
2152 ((<= (cdr lit) here)
2153 (setq pos (cdr lit))
2154 t)
2155 (t
2156 (setq pos (car lit))
2157 nil))))
2158
2159 (goto-char pos)
2160 (when (and (c-beginning-of-macro) (/= (point) pos))
2161 (setq macro-beg (point))
2162 (c-syntactic-end-of-macro)
2163 (or (eobp) (forward-char))
2164 (setq pos (if (<= (point) here)
2165 (point)
2166 macro-beg)))
2167 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2168
2169 (if (> pos c-state-nonlit-pos-cache-limit)
2170 (setq c-state-nonlit-pos-cache-limit pos))
2171 pos))))
2172
2173 (defun c-state-literal-at (here)
2174 ;; If position HERE is inside a literal, return (START . END), the
2175 ;; boundaries of the literal (which may be outside the accessible bit of the
2176 ;; buffer). Otherwise, return nil.
2177 ;;
2178 ;; This function is almost the same as `c-literal-limits'. Previously, it
2179 ;; differed in that it was a lower level function, and that it rigorously
2180 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2181 ;; virtually identical to this function.
2182 (save-restriction
2183 (widen)
2184 (save-excursion
2185 (let ((pos (c-state-safe-place here)))
2186 (car (cddr (c-state-pp-to-literal pos here)))))))
2187
2188 (defsubst c-state-lit-beg (pos)
2189 ;; Return the start of the literal containing POS, or POS itself.
2190 (or (car (c-state-literal-at pos))
2191 pos))
2192
2193 (defsubst c-state-cache-non-literal-place (pos state)
2194 ;; Return a position outside of a string/comment/macro at or before POS.
2195 ;; STATE is the parse-partial-sexp state at POS.
2196 (let ((res (if (or (nth 3 state) ; in a string?
2197 (nth 4 state)) ; in a comment?
2198 (nth 8 state)
2199 pos)))
2200 (save-excursion
2201 (goto-char res)
2202 (if (c-beginning-of-macro)
2203 (point)
2204 res))))
2205
2206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2207 ;; Stuff to do with point-min, and coping with any literal there.
2208 (defvar c-state-point-min 1)
2209 (make-variable-buffer-local 'c-state-point-min)
2210 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2211 ;; narrowing is likely to affect the parens that are visible before the point.
2212
2213 (defvar c-state-point-min-lit-type nil)
2214 (make-variable-buffer-local 'c-state-point-min-lit-type)
2215 (defvar c-state-point-min-lit-start nil)
2216 (make-variable-buffer-local 'c-state-point-min-lit-start)
2217 ;; These two variables define the literal, if any, containing point-min.
2218 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2219 ;; literal. If there's no literal there, they're both nil.
2220
2221 (defvar c-state-min-scan-pos 1)
2222 (make-variable-buffer-local 'c-state-min-scan-pos)
2223 ;; This is the earliest buffer-pos from which scanning can be done. It is
2224 ;; either the end of the literal containing point-min, or point-min itself.
2225 ;; It becomes nil if the buffer is changed earlier than this point.
2226 (defun c-state-get-min-scan-pos ()
2227 ;; Return the lowest valid scanning pos. This will be the end of the
2228 ;; literal enclosing point-min, or point-min itself.
2229 (or c-state-min-scan-pos
2230 (save-restriction
2231 (save-excursion
2232 (widen)
2233 (goto-char c-state-point-min-lit-start)
2234 (if (eq c-state-point-min-lit-type 'string)
2235 (forward-sexp)
2236 (forward-comment 1))
2237 (setq c-state-min-scan-pos (point))))))
2238
2239 (defun c-state-mark-point-min-literal ()
2240 ;; Determine the properties of any literal containing POINT-MIN, setting the
2241 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2242 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2243 (let ((p-min (point-min))
2244 lit)
2245 (save-restriction
2246 (widen)
2247 (setq lit (c-state-literal-at p-min))
2248 (if lit
2249 (setq c-state-point-min-lit-type
2250 (save-excursion
2251 (goto-char (car lit))
2252 (cond
2253 ((looking-at c-block-comment-start-regexp) 'c)
2254 ((looking-at c-line-comment-starter) 'c++)
2255 (t 'string)))
2256 c-state-point-min-lit-start (car lit)
2257 c-state-min-scan-pos (cdr lit))
2258 (setq c-state-point-min-lit-type nil
2259 c-state-point-min-lit-start nil
2260 c-state-min-scan-pos p-min)))))
2261
2262
2263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2264 ;; A variable which signals a brace dessert - helpful for reducing the number
2265 ;; of fruitless backward scans.
2266 (defvar c-state-brace-pair-desert nil)
2267 (make-variable-buffer-local 'c-state-brace-pair-desert)
2268 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2269 ;; that defun has searched backwards for a brace pair and not found one. Its
2270 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2271 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2272 ;; nil when at top level) and FROM is where the backward search started. It
2273 ;; is reset to nil in `c-invalidate-state-cache'.
2274
2275
2276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2277 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2278 ;; list of like structure.
2279 (defmacro c-state-cache-top-lparen (&optional cache)
2280 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2281 ;; (default `c-state-cache') (or nil).
2282 (let ((cash (or cache 'c-state-cache)))
2283 `(if (consp (car ,cash))
2284 (caar ,cash)
2285 (car ,cash))))
2286
2287 (defmacro c-state-cache-top-paren (&optional cache)
2288 ;; Return the address of the latest brace/bracket/paren (whether left or
2289 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2290 (let ((cash (or cache 'c-state-cache)))
2291 `(if (consp (car ,cash))
2292 (cdar ,cash)
2293 (car ,cash))))
2294
2295 (defmacro c-state-cache-after-top-paren (&optional cache)
2296 ;; Return the position just after the latest brace/bracket/paren (whether
2297 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2298 (let ((cash (or cache 'c-state-cache)))
2299 `(if (consp (car ,cash))
2300 (cdar ,cash)
2301 (and (car ,cash)
2302 (1+ (car ,cash))))))
2303
2304 (defun c-get-cache-scan-pos (here)
2305 ;; From the state-cache, determine the buffer position from which we might
2306 ;; scan forward to HERE to update this cache. This position will be just
2307 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2308 ;; return the earliest position in the accessible region which isn't within
2309 ;; a literal. If the visible portion of the buffer is entirely within a
2310 ;; literal, return NIL.
2311 (let ((c c-state-cache) elt)
2312 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2313 (while (and c
2314 (>= (c-state-cache-top-lparen c) here))
2315 (setq c (cdr c)))
2316
2317 (setq elt (car c))
2318 (cond
2319 ((consp elt)
2320 (if (> (cdr elt) here)
2321 (1+ (car elt))
2322 (cdr elt)))
2323 (elt (1+ elt))
2324 ((<= (c-state-get-min-scan-pos) here)
2325 (c-state-get-min-scan-pos))
2326 (t nil))))
2327
2328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2329 ;; Variables which keep track of preprocessor constructs.
2330 (defvar c-state-old-cpp-beg nil)
2331 (make-variable-buffer-local 'c-state-old-cpp-beg)
2332 (defvar c-state-old-cpp-end nil)
2333 (make-variable-buffer-local 'c-state-old-cpp-end)
2334 ;; These are the limits of the macro containing point at the previous call of
2335 ;; `c-parse-state', or nil.
2336
2337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2338 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2339 (defun c-get-fallback-scan-pos (here)
2340 ;; Return a start position for building `c-state-cache' from
2341 ;; scratch. This will be at the top level, 2 defuns back.
2342 (save-excursion
2343 ;; Go back 2 bods, but ignore any bogus positions returned by
2344 ;; beginning-of-defun (i.e. open paren in column zero).
2345 (goto-char here)
2346 (let ((cnt 2))
2347 (while (not (or (bobp) (zerop cnt)))
2348 (c-beginning-of-defun-1) ; Pure elisp BOD.
2349 (if (eq (char-after) ?\{)
2350 (setq cnt (1- cnt)))))
2351 (point)))
2352
2353 (defun c-state-balance-parens-backwards (here- here+ top)
2354 ;; Return the position of the opening paren/brace/bracket before HERE- which
2355 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2356 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2357 ;;
2358 ;; ............................................
2359 ;; | |
2360 ;; ( [ ( .........#macro.. ) ( ) ] )
2361 ;; ^ ^ ^ ^
2362 ;; | | | |
2363 ;; return HERE- HERE+ TOP
2364 ;;
2365 ;; If there aren't enough opening paren/brace/brackets, return the position
2366 ;; of the outermost one found, or HERE- if there are none. If there are no
2367 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2368 ;; must not be inside literals. Only the accessible portion of the buffer
2369 ;; will be scanned.
2370
2371 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2372 ;; `here'. Go round the next loop each time we pass over such a ")". These
2373 ;; probably match "("s before `here-'.
2374 (let (pos pa ren+1 lonely-rens)
2375 (save-excursion
2376 (save-restriction
2377 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2378 (setq pos here+)
2379 (c-safe
2380 (while
2381 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2382 (setq lonely-rens (cons ren+1 lonely-rens)
2383 pos ren+1)))))
2384
2385 ;; PART 2: Scan back before `here-' searching for the "("s
2386 ;; matching/mismatching the ")"s found above. We only need to direct the
2387 ;; caller to scan when we've encountered unmatched right parens.
2388 (setq pos here-)
2389 (when lonely-rens
2390 (c-safe
2391 (while
2392 (and lonely-rens ; actual values aren't used.
2393 (setq pa (scan-lists pos -1 1)))
2394 (setq pos pa)
2395 (setq lonely-rens (cdr lonely-rens)))))
2396 pos))
2397
2398 (defun c-parse-state-get-strategy (here good-pos)
2399 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2400 ;; to minimize the amount of scanning. HERE is the pertinent position in
2401 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2402 ;; its head trimmed) is known to be good, or nil if there is no such
2403 ;; position.
2404 ;;
2405 ;; The return value is a list, one of the following:
2406 ;;
2407 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2408 ;; which is not less than CACHE-POS.
2409 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2410 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2411 ;; top level.
2412 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2413 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2414 ;; or below HERE.
2415 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2416 BOD-pos ; position of 2nd BOD before HERE.
2417 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2418 start-point
2419 how-far) ; putative scanning distance.
2420 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2421 (cond
2422 ((< here (c-state-get-min-scan-pos))
2423 (setq strategy 'IN-LIT
2424 start-point nil
2425 cache-pos nil
2426 how-far 0))
2427 ((<= good-pos here)
2428 (setq strategy 'forward
2429 start-point (max good-pos cache-pos)
2430 how-far (- here start-point)))
2431 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2432 (setq strategy 'backward
2433 how-far (- good-pos here)))
2434 (t
2435 (setq strategy 'forward
2436 how-far (- here cache-pos)
2437 start-point cache-pos)))
2438
2439 ;; Might we be better off starting from the top level, two defuns back,
2440 ;; instead?
2441 (when (> how-far c-state-cache-too-far)
2442 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2443 (if (< (- here BOD-pos) how-far)
2444 (setq strategy 'BOD
2445 start-point BOD-pos)))
2446
2447 (list
2448 strategy
2449 (and (memq strategy '(forward backward)) cache-pos)
2450 (and (memq strategy '(forward BOD)) start-point))))
2451
2452
2453 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2454 ;; Routines which change `c-state-cache' and associated values.
2455 (defun c-renarrow-state-cache ()
2456 ;; The region (more precisely, point-min) has changed since we
2457 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2458 (if (< (point-min) c-state-point-min)
2459 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2460 ;; It would be possible to do a better job here and recalculate the top
2461 ;; only.
2462 (progn
2463 (c-state-mark-point-min-literal)
2464 (setq c-state-cache nil
2465 c-state-cache-good-pos c-state-min-scan-pos
2466 c-state-brace-pair-desert nil))
2467
2468 ;; point-min has MOVED FORWARD.
2469
2470 ;; Is the new point-min inside a (different) literal?
2471 (unless (and c-state-point-min-lit-start ; at prev. point-min
2472 (< (point-min) (c-state-get-min-scan-pos)))
2473 (c-state-mark-point-min-literal))
2474
2475 ;; Cut off a bit of the tail from `c-state-cache'.
2476 (let ((ptr (cons nil c-state-cache))
2477 pa)
2478 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2479 (>= pa (point-min)))
2480 (setq ptr (cdr ptr)))
2481
2482 (when (consp ptr)
2483 (if (eq (cdr ptr) c-state-cache)
2484 (setq c-state-cache nil
2485 c-state-cache-good-pos c-state-min-scan-pos)
2486 (setcdr ptr nil)
2487 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2488 )))
2489
2490 (setq c-state-point-min (point-min)))
2491
2492 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2493 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2494 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2495 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2496 ;; the highest brace pair whose "}" is below UPPER-LIM.
2497 ;;
2498 ;; Return non-nil when this has been done.
2499 ;;
2500 ;; This routine should be fast. Since it can get called a LOT, we maintain
2501 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2502 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2503 (save-excursion
2504 (save-restriction
2505 (let ((bra from) ce ; Positions of "{" and "}".
2506 new-cons
2507 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2508 (macro-start-or-from
2509 (progn (goto-char from)
2510 (c-beginning-of-macro)
2511 (point))))
2512 (or upper-lim (setq upper-lim from))
2513
2514 ;; If we're essentially repeating a fruitless search, just give up.
2515 (unless (and c-state-brace-pair-desert
2516 (eq cache-pos (car c-state-brace-pair-desert))
2517 (<= from (cdr c-state-brace-pair-desert)))
2518 ;; Only search what we absolutely need to:
2519 (if (and c-state-brace-pair-desert
2520 (eq cache-pos (car c-state-brace-pair-desert)))
2521 (narrow-to-region (cdr c-state-brace-pair-desert) (point-max)))
2522
2523 ;; In the next pair of nested loops, the inner one moves back past a
2524 ;; pair of (mis-)matching parens or brackets; the outer one moves
2525 ;; back over a sequence of unmatched close brace/paren/bracket each
2526 ;; time round.
2527 (while
2528 (progn
2529 (c-safe
2530 (while
2531 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2532 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2533 (or (> ce upper-lim)
2534 (not (eq (char-after bra) ?\{))
2535 (and (goto-char bra)
2536 (c-beginning-of-macro)
2537 (< (point) macro-start-or-from))))))
2538 (and ce (< ce bra)))
2539 (setq bra ce)) ; If we just backed over an unbalanced closing
2540 ; brace, ignore it.
2541
2542 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2543 ;; We've found the desired brace-pair.
2544 (progn
2545 (setq new-cons (cons bra (1+ ce)))
2546 (cond
2547 ((consp (car c-state-cache))
2548 (setcar c-state-cache new-cons))
2549 ((and (numberp (car c-state-cache)) ; probably never happens
2550 (< ce (car c-state-cache)))
2551 (setcdr c-state-cache
2552 (cons new-cons (cdr c-state-cache))))
2553 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2554
2555 ;; We haven't found a brace pair. Record this.
2556 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2557
2558 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2559 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2560 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2561 ;; "push" "a" brace pair onto `c-state-cache'.
2562 ;;
2563 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2564 ;; otherwise push it normally.
2565 ;;
2566 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2567 ;; latter is inside a macro, not being a macro containing
2568 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2569 ;; base pair. This latter case is assumed to be rare.
2570 ;;
2571 ;; Note: POINT is not preserved in this routine.
2572 (if bra+1
2573 (if (or (> bra+1 macro-start-or-here)
2574 (progn (goto-char bra+1)
2575 (not (c-beginning-of-macro))))
2576 (setq c-state-cache
2577 (cons (cons (1- bra+1)
2578 (scan-lists bra+1 1 1))
2579 (if (consp (car c-state-cache))
2580 (cdr c-state-cache)
2581 c-state-cache)))
2582 ;; N.B. This defsubst codes one method for the simple, normal case,
2583 ;; and a more sophisticated, slower way for the general case. Don't
2584 ;; eliminate this defsubst - it's a speed optimization.
2585 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2586
2587 (defun c-append-to-state-cache (from)
2588 ;; Scan the buffer from FROM to (point-max), adding elements into
2589 ;; `c-state-cache' for braces etc. Return a candidate for
2590 ;; `c-state-cache-good-pos'.
2591 ;;
2592 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2593 ;; any. Typically, it is immediately after it. It must not be inside a
2594 ;; literal.
2595 (let ((here-bol (c-point 'bol (point-max)))
2596 (macro-start-or-here
2597 (save-excursion (goto-char (point-max))
2598 (if (c-beginning-of-macro)
2599 (point)
2600 (point-max))))
2601 pa+1 ; pos just after an opening PAren (or brace).
2602 (ren+1 from) ; usually a pos just after an closing paREN etc.
2603 ; Is actually the pos. to scan for a (/{/[ from,
2604 ; which sometimes is after a silly )/}/].
2605 paren+1 ; Pos after some opening or closing paren.
2606 paren+1s ; A list of `paren+1's; used to determine a
2607 ; good-pos.
2608 bra+1 ce+1 ; just after L/R bra-ces.
2609 bra+1s ; list of OLD values of bra+1.
2610 mstart) ; start of a macro.
2611
2612 (save-excursion
2613 ;; Each time round the following loop, we enter a successively deeper
2614 ;; level of brace/paren nesting. (Except sometimes we "continue at
2615 ;; the existing level".) `pa+1' is a pos inside an opening
2616 ;; brace/paren/bracket, usually just after it.
2617 (while
2618 (progn
2619 ;; Each time round the next loop moves forward over an opening then
2620 ;; a closing brace/bracket/paren. This loop is white hot, so it
2621 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2622 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2623 ;; call of `scan-lists' signals an error, which happens when there
2624 ;; are no more b/b/p's to scan.
2625 (c-safe
2626 (while t
2627 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2628 paren+1s (cons pa+1 paren+1s))
2629 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2630 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2631 (setq bra+1 pa+1))
2632 (setcar paren+1s ren+1)))
2633
2634 (if (and pa+1 (> pa+1 ren+1))
2635 ;; We've just entered a deeper nesting level.
2636 (progn
2637 ;; Insert the brace pair (if present) and the single open
2638 ;; paren/brace/bracket into `c-state-cache' It cannot be
2639 ;; inside a macro, except one around point, because of what
2640 ;; `c-neutralize-syntax-in-CPP' has done.
2641 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2642 ;; Insert the opening brace/bracket/paren position.
2643 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2644 ;; Clear admin stuff for the next more nested part of the scan.
2645 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2646 t) ; Carry on the loop
2647
2648 ;; All open p/b/b's at this nesting level, if any, have probably
2649 ;; been closed by matching/mismatching ones. We're probably
2650 ;; finished - we just need to check for having found an
2651 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2652 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2653 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2654
2655 ;; Record the final, innermost, brace-pair if there is one.
2656 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2657
2658 ;; Determine a good pos
2659 (while (and (setq paren+1 (car paren+1s))
2660 (> (if (> paren+1 macro-start-or-here)
2661 paren+1
2662 (goto-char paren+1)
2663 (setq mstart (and (c-beginning-of-macro)
2664 (point)))
2665 (or mstart paren+1))
2666 here-bol))
2667 (setq paren+1s (cdr paren+1s)))
2668 (cond
2669 ((and paren+1 mstart)
2670 (min paren+1 mstart))
2671 (paren+1)
2672 (t from)))))
2673
2674 (defun c-remove-stale-state-cache (good-pos pps-point)
2675 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2676 ;; not be in it when it is amended for position (point-max).
2677 ;; Additionally, the "outermost" open-brace entry before (point-max)
2678 ;; will be converted to a cons if the matching close-brace is scanned.
2679 ;;
2680 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2681 ;; parens/braces/brackets between GOOD-POS and (point-max).
2682 ;;
2683 ;; As a second thing, calculate the result of parse-partial-sexp at
2684 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2685 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2686 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2687 ;; needs to be FAST).
2688 ;;
2689 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2690 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2691 ;; to be good (we aim for this to be as high as possible);
2692 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2693 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2694 ;; position to scan backwards from.
2695 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2696 (save-restriction
2697 (narrow-to-region 1 (point-max))
2698 (save-excursion
2699 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2700 (save-excursion
2701 (goto-char (point-max))
2702 (and (c-beginning-of-macro)
2703 (point))))
2704 (good-pos-actual-macro-start ; Start of macro containing good-pos
2705 ; or nil
2706 (and (< good-pos (point-max))
2707 (save-excursion
2708 (goto-char good-pos)
2709 (and (c-beginning-of-macro)
2710 (point)))))
2711 (good-pos-actual-macro-end ; End of this macro, (maybe
2712 ; (point-max)), or nil.
2713 (and good-pos-actual-macro-start
2714 (save-excursion
2715 (goto-char good-pos-actual-macro-start)
2716 (c-end-of-macro)
2717 (point))))
2718 pps-state ; Will be 9 or 10 elements long.
2719 pos
2720 upper-lim ; ,beyond which `c-state-cache' entries are removed
2721 scan-back-pos
2722 pair-beg pps-point-state target-depth)
2723
2724 ;; Remove entries beyond (point-max). Also remove any entries inside
2725 ;; a macro, unless (point-max) is in the same macro.
2726 (setq upper-lim
2727 (if (or (null c-state-old-cpp-beg)
2728 (and (> (point-max) c-state-old-cpp-beg)
2729 (< (point-max) c-state-old-cpp-end)))
2730 (point-max)
2731 (min (point-max) c-state-old-cpp-beg)))
2732 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2733 (setq c-state-cache (cdr c-state-cache)))
2734 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2735 ;; RBrace and indicate we'll need to search backwards for a previous
2736 ;; brace pair.
2737 (when (and c-state-cache
2738 (consp (car c-state-cache))
2739 (> (cdar c-state-cache) upper-lim))
2740 (setcar c-state-cache (caar c-state-cache))
2741 (setq scan-back-pos (car c-state-cache)))
2742
2743 ;; The next loop jumps forward out of a nested level of parens each
2744 ;; time round; the corresponding elements in `c-state-cache' are
2745 ;; removed. `pos' is just after the brace-pair or the open paren at
2746 ;; (car c-state-cache). There can be no open parens/braces/brackets
2747 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2748 ;; due to the interface spec to this function.
2749 (setq pos (if (and good-pos-actual-macro-end
2750 (not (eq good-pos-actual-macro-start
2751 in-macro-start)))
2752 (1+ good-pos-actual-macro-end) ; get outside the macro as
2753 ; marked by a `category' text property.
2754 good-pos))
2755 (goto-char pos)
2756 (while (and c-state-cache
2757 (< (point) (point-max)))
2758 (cond
2759 ((null pps-state) ; first time through
2760 (setq target-depth -1))
2761 ((eq (car pps-state) target-depth) ; found closing ),},]
2762 (setq target-depth (1- (car pps-state))))
2763 ;; Do nothing when we've merely reached pps-point.
2764 )
2765
2766 ;; Scan!
2767 (setq pps-state
2768 (parse-partial-sexp
2769 (point) (if (< (point) pps-point) pps-point (point-max))
2770 target-depth
2771 nil pps-state))
2772
2773 (if (= (point) pps-point)
2774 (setq pps-point-state pps-state))
2775
2776 (when (eq (car pps-state) target-depth)
2777 (setq pos (point)) ; POS is now just after an R-paren/brace.
2778 (cond
2779 ((and (consp (car c-state-cache))
2780 (eq (point) (cdar c-state-cache)))
2781 ;; We've just moved out of the paren pair containing the brace-pair
2782 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2783 ;; and is potentially where the open brace of a cons in
2784 ;; c-state-cache will be.
2785 (setq pair-beg (car-safe (cdr c-state-cache))
2786 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2787 ((numberp (car c-state-cache))
2788 (setq pair-beg (car c-state-cache)
2789 c-state-cache (cdr c-state-cache))) ; remove this
2790 ; containing Lparen
2791 ((numberp (cadr c-state-cache))
2792 (setq pair-beg (cadr c-state-cache)
2793 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2794 ; together with enclosed brace pair.
2795 ;; (t nil) ; Ignore an unmated Rparen.
2796 )))
2797
2798 (if (< (point) pps-point)
2799 (setq pps-state (parse-partial-sexp (point) pps-point
2800 nil nil ; TARGETDEPTH, STOPBEFORE
2801 pps-state)))
2802
2803 ;; If the last paren pair we moved out of was actually a brace pair,
2804 ;; insert it into `c-state-cache'.
2805 (when (and pair-beg (eq (char-after pair-beg) ?{))
2806 (if (consp (car-safe c-state-cache))
2807 (setq c-state-cache (cdr c-state-cache)))
2808 (setq c-state-cache (cons (cons pair-beg pos)
2809 c-state-cache)))
2810
2811 (list pos scan-back-pos pps-state)))))
2812
2813 (defun c-remove-stale-state-cache-backwards (here cache-pos)
2814 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2815 ;; buffer, and inform the caller of the scenario detected.
2816 ;;
2817 ;; HERE is the position we're setting `c-state-cache' for.
2818 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2819 ;; before HERE, or a position at or near point-min which isn't in a
2820 ;; literal.
2821 ;;
2822 ;; This function must only be called only when (> `c-state-cache-good-pos'
2823 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2824 ;; optimized to eliminate (or minimize) scanning between these two
2825 ;; positions.
2826 ;;
2827 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2828 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2829 ;; could become so after missing elements are inserted into
2830 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2831 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2832 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2833 ;; before `here''s line, or the start of the literal containing it.
2834 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2835 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2836 ;; to scan backwards from.
2837 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2838 ;; POS and HERE which aren't recorded in `c-state-cache'.
2839 ;;
2840 ;; The comments in this defun use "paren" to mean parenthesis or square
2841 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2842 ;;
2843 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2844 ;; | | | | | |
2845 ;; CP E here D C good
2846 (let ((pos c-state-cache-good-pos)
2847 pa ren ; positions of "(" and ")"
2848 dropped-cons ; whether the last element dropped from `c-state-cache'
2849 ; was a cons (representing a brace-pair)
2850 good-pos ; see above.
2851 lit ; (START . END) of a literal containing some point.
2852 here-lit-start here-lit-end ; bounds of literal containing `here'
2853 ; or `here' itself.
2854 here- here+ ; start/end of macro around HERE, or HERE
2855 (here-bol (c-point 'bol here))
2856 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
2857
2858 ;; Remove completely irrelevant entries from `c-state-cache'.
2859 (while (and c-state-cache
2860 (>= (setq pa (c-state-cache-top-lparen)) here))
2861 (setq dropped-cons (consp (car c-state-cache)))
2862 (setq c-state-cache (cdr c-state-cache))
2863 (setq pos pa))
2864 ;; At this stage, (> pos here);
2865 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2866
2867 (cond
2868 ((and (consp (car c-state-cache))
2869 (> (cdar c-state-cache) here))
2870 ;; CASE 1: The top of the cache is a brace pair which now encloses
2871 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2872 ;; knowledge of what's inside these braces, we have no alternative but
2873 ;; to direct the caller to scan the buffer from the opening brace.
2874 (setq pos (caar c-state-cache))
2875 (setcar c-state-cache pos)
2876 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
2877 ; entry into a { entry, so the caller needs to
2878 ; search for a brace pair before the {.
2879
2880 ;; `here' might be inside a literal. Check for this.
2881 ((progn
2882 (setq lit (c-state-literal-at here)
2883 here-lit-start (or (car lit) here)
2884 here-lit-end (or (cdr lit) here))
2885 ;; Has `here' just "newly entered" a macro?
2886 (save-excursion
2887 (goto-char here-lit-start)
2888 (if (and (c-beginning-of-macro)
2889 (or (null c-state-old-cpp-beg)
2890 (not (= (point) c-state-old-cpp-beg))))
2891 (progn
2892 (setq here- (point))
2893 (c-end-of-macro)
2894 (setq here+ (point)))
2895 (setq here- here-lit-start
2896 here+ here-lit-end)))
2897
2898 ;; `here' might be nested inside any depth of parens (or brackets but
2899 ;; not braces). Scan backwards to find the outermost such opening
2900 ;; paren, if there is one. This will be the scan position to return.
2901 (save-restriction
2902 (narrow-to-region cache-pos (point-max))
2903 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
2904 nil)) ; for the cond
2905
2906 ((< pos here-lit-start)
2907 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2908 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2909 ;; a brace pair preceding this, it will already be in `c-state-cache',
2910 ;; unless there was a brace pair after it, i.e. there'll only be one to
2911 ;; scan for if we've just deleted one.
2912 (list pos (and dropped-cons pos) t)) ; Return value.
2913
2914 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2915 ;; Further forward scanning isn't needed, but we still need to find a
2916 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2917 ((progn
2918 (save-restriction
2919 (narrow-to-region here-bol (point-max))
2920 (setq pos here-lit-start)
2921 (c-safe (while (setq pa (scan-lists pos -1 1))
2922 (setq pos pa)))) ; might signal
2923 nil)) ; for the cond
2924
2925 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
2926 ;; CASE 3: After a }/)/] before `here''s BOL.
2927 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
2928
2929 (t
2930 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2931 ;; literal containing it.
2932 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
2933 (list good-pos (and dropped-cons good-pos) nil)))))
2934
2935
2936 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2937 ;; Externally visible routines.
2938
2939 (defun c-state-cache-init ()
2940 (setq c-state-cache nil
2941 c-state-cache-good-pos 1
2942 c-state-nonlit-pos-cache nil
2943 c-state-nonlit-pos-cache-limit 1
2944 c-state-brace-pair-desert nil
2945 c-state-point-min 1
2946 c-state-point-min-lit-type nil
2947 c-state-point-min-lit-start nil
2948 c-state-min-scan-pos 1
2949 c-state-old-cpp-beg nil
2950 c-state-old-cpp-end nil)
2951 (c-state-mark-point-min-literal))
2952
2953 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2954 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
2955 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
2956 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
2957 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
2958 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
2959 ;; (defun c-state-dump ()
2960 ;; ;; For debugging.
2961 ;; ;(message
2962 ;; (concat
2963 ;; (c-sc-qde c-state-cache)
2964 ;; (c-sc-de c-state-cache-good-pos)
2965 ;; (c-sc-qde c-state-nonlit-pos-cache)
2966 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
2967 ;; (c-sc-qde c-state-brace-pair-desert)
2968 ;; (c-sc-de c-state-point-min)
2969 ;; (c-sc-de c-state-point-min-lit-type)
2970 ;; (c-sc-de c-state-point-min-lit-start)
2971 ;; (c-sc-de c-state-min-scan-pos)
2972 ;; (c-sc-de c-state-old-cpp-beg)
2973 ;; (c-sc-de c-state-old-cpp-end)))
2974 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2975
2976 (defun c-invalidate-state-cache-1 (here)
2977 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2978 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2979 ;; left in a consistent state.
2980 ;;
2981 ;; This is much like `c-whack-state-after', but it never changes a paren
2982 ;; pair element into an open paren element. Doing that would mean that the
2983 ;; new open paren wouldn't have the required preceding paren pair element.
2984 ;;
2985 ;; This function is called from c-after-change.
2986
2987 ;; The cache of non-literals:
2988 (if (< here c-state-nonlit-pos-cache-limit)
2989 (setq c-state-nonlit-pos-cache-limit here))
2990
2991 ;; `c-state-cache':
2992 ;; Case 1: if `here' is in a literal containing point-min, everything
2993 ;; becomes (or is already) nil.
2994 (if (or (null c-state-cache-good-pos)
2995 (< here (c-state-get-min-scan-pos)))
2996 (setq c-state-cache nil
2997 c-state-cache-good-pos nil
2998 c-state-min-scan-pos nil)
2999
3000 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3001 ;; below `here'. To maintain its consistency, we may need to insert a new
3002 ;; brace pair.
3003 (let ((here-bol (c-point 'bol here))
3004 too-high-pa ; recorded {/(/[ next above here, or nil.
3005 dropped-cons ; was the last removed element a brace pair?
3006 pa)
3007 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3008 (while (and c-state-cache
3009 (>= (setq pa (c-state-cache-top-paren)) here))
3010 (setq dropped-cons (consp (car c-state-cache))
3011 too-high-pa (c-state-cache-top-lparen)
3012 c-state-cache (cdr c-state-cache)))
3013
3014 ;; Do we need to add in an earlier brace pair, having lopped one off?
3015 (if (and dropped-cons
3016 (< too-high-pa (+ here c-state-cache-too-far)))
3017 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
3018 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3019 (c-state-get-min-scan-pos)))))
3020
3021 ;; The brace-pair desert marker:
3022 (when (car c-state-brace-pair-desert)
3023 (if (< here (car c-state-brace-pair-desert))
3024 (setq c-state-brace-pair-desert nil)
3025 (if (< here (cdr c-state-brace-pair-desert))
3026 (setcdr c-state-brace-pair-desert here)))))
3027
3028 (defun c-parse-state-1 ()
3029 ;; Find and record all noteworthy parens between some good point earlier in
3030 ;; the file and point. That good point is at least the beginning of the
3031 ;; top-level construct we are in, or the beginning of the preceding
3032 ;; top-level construct if we aren't in one.
3033 ;;
3034 ;; The returned value is a list of the noteworthy parens with the last one
3035 ;; first. If an element in the list is an integer, it's the position of an
3036 ;; open paren (of any type) which has not been closed before the point. If
3037 ;; an element is a cons, it gives the position of a closed BRACE paren
3038 ;; pair[*]; the car is the start brace position and the cdr is the position
3039 ;; following the closing brace. Only the last closed brace paren pair
3040 ;; before each open paren and before the point is recorded, and thus the
3041 ;; state never contains two cons elements in succession. When a close brace
3042 ;; has no matching open brace (e.g., the matching brace is outside the
3043 ;; visible region), it is not represented in the returned value.
3044 ;;
3045 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3046 ;; This defun explicitly treats mismatching parens/braces/brackets as
3047 ;; matching. It is the open brace which makes it a "brace" pair.
3048 ;;
3049 ;; If POINT is within a macro, open parens and brace pairs within
3050 ;; THIS macro MIGHT be recorded. This depends on whether their
3051 ;; syntactic properties have been suppressed by
3052 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3053 ;;
3054 ;; Currently no characters which are given paren syntax with the
3055 ;; syntax-table property are recorded, i.e. angle bracket arglist
3056 ;; parens are never present here. Note that this might change.
3057 ;;
3058 ;; BUG: This function doesn't cope entirely well with unbalanced
3059 ;; parens in macros. (2008-12-11: this has probably been resolved
3060 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3061 ;; following case the brace before the macro isn't balanced with the
3062 ;; one after it:
3063 ;;
3064 ;; {
3065 ;; #define X {
3066 ;; }
3067 ;;
3068 ;; Note to maintainers: this function DOES get called with point
3069 ;; within comments and strings, so don't assume it doesn't!
3070 ;;
3071 ;; This function might do hidden buffer changes.
3072 (let* ((here (point))
3073 (here-bopl (c-point 'bopl))
3074 strategy ; 'forward, 'backward etc..
3075 ;; Candidate positions to start scanning from:
3076 cache-pos ; highest position below HERE already existing in
3077 ; cache (or 1).
3078 good-pos
3079 start-point
3080 bopl-state
3081 res
3082 scan-backward-pos scan-forward-p) ; used for 'backward.
3083 ;; If POINT-MIN has changed, adjust the cache
3084 (unless (= (point-min) c-state-point-min)
3085 (c-renarrow-state-cache))
3086
3087 ;; Strategy?
3088 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3089 strategy (car res)
3090 cache-pos (cadr res)
3091 start-point (nth 2 res))
3092
3093 (when (eq strategy 'BOD)
3094 (setq c-state-cache nil
3095 c-state-cache-good-pos start-point))
3096
3097 ;; SCAN!
3098 (save-restriction
3099 (cond
3100 ((memq strategy '(forward BOD))
3101 (narrow-to-region (point-min) here)
3102 (setq res (c-remove-stale-state-cache start-point here-bopl))
3103 (setq cache-pos (car res)
3104 scan-backward-pos (cadr res)
3105 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
3106 ; start-point)
3107 (if scan-backward-pos
3108 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
3109 (setq good-pos
3110 (c-append-to-state-cache cache-pos))
3111 (setq c-state-cache-good-pos
3112 (if (and bopl-state
3113 (< good-pos (- here c-state-cache-too-far)))
3114 (c-state-cache-non-literal-place here-bopl bopl-state)
3115 good-pos)))
3116
3117 ((eq strategy 'backward)
3118 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
3119 good-pos (car res)
3120 scan-backward-pos (cadr res)
3121 scan-forward-p (car (cddr res)))
3122 (if scan-backward-pos
3123 (c-append-lower-brace-pair-to-state-cache
3124 scan-backward-pos))
3125 (setq c-state-cache-good-pos
3126 (if scan-forward-p
3127 (progn (narrow-to-region (point-min) here)
3128 (c-append-to-state-cache good-pos))
3129
3130 (c-get-cache-scan-pos good-pos))))
3131
3132 (t ; (eq strategy 'IN-LIT)
3133 (setq c-state-cache nil
3134 c-state-cache-good-pos nil)))))
3135
3136 c-state-cache)
3137
3138 (defun c-invalidate-state-cache (here)
3139 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3140 ;;
3141 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3142 ;; of all parens in preprocessor constructs, except for any such construct
3143 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3144 ;; worrying further about macros and template delimiters.
3145 (c-with-<->-as-parens-suppressed
3146 (if (and c-state-old-cpp-beg
3147 (< c-state-old-cpp-beg here))
3148 (c-with-all-but-one-cpps-commented-out
3149 c-state-old-cpp-beg
3150 (min c-state-old-cpp-end here)
3151 (c-invalidate-state-cache-1 here))
3152 (c-with-cpps-commented-out
3153 (c-invalidate-state-cache-1 here)))))
3154
3155 (defun c-parse-state ()
3156 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3157 ;; description of the functionality and return value.
3158 ;;
3159 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3160 ;; of all parens in preprocessor constructs, except for any such construct
3161 ;; containing point. We can then call `c-parse-state-1' without worrying
3162 ;; further about macros and template delimiters.
3163 (let (here-cpp-beg here-cpp-end)
3164 (save-excursion
3165 (when (c-beginning-of-macro)
3166 (setq here-cpp-beg (point))
3167 (unless
3168 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3169 here-cpp-beg)
3170 (setq here-cpp-beg nil here-cpp-end nil))))
3171 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3172 ;; subsystem.
3173 (prog1
3174 (c-with-<->-as-parens-suppressed
3175 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3176 (c-with-all-but-one-cpps-commented-out
3177 here-cpp-beg here-cpp-end
3178 (c-parse-state-1))
3179 (c-with-cpps-commented-out
3180 (c-parse-state-1))))
3181 (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
3182 c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
3183 )))
3184
3185 ;; Debug tool to catch cache inconsistencies. This is called from
3186 ;; 000tests.el.
3187 (defvar c-debug-parse-state nil)
3188 (unless (fboundp 'c-real-parse-state)
3189 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3190 (cc-bytecomp-defun c-real-parse-state)
3191
3192 (defvar c-parse-state-state nil)
3193 (defun c-record-parse-state-state ()
3194 (setq c-parse-state-state
3195 (mapcar
3196 (lambda (arg)
3197 (cons arg (symbol-value arg)))
3198 '(c-state-cache
3199 c-state-cache-good-pos
3200 c-state-nonlit-pos-cache
3201 c-state-nonlit-pos-cache-limit
3202 c-state-brace-pair-desert
3203 c-state-point-min
3204 c-state-point-min-lit-type
3205 c-state-point-min-lit-start
3206 c-state-min-scan-pos
3207 c-state-old-cpp-beg
3208 c-state-old-cpp-end))))
3209 (defun c-replay-parse-state-state ()
3210 (message
3211 (concat "(setq "
3212 (mapconcat
3213 (lambda (arg)
3214 (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
3215 c-parse-state-state " ")
3216 ")")))
3217
3218 (defun c-debug-parse-state ()
3219 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3220 (let ((c-state-cache nil)
3221 (c-state-cache-good-pos 1)
3222 (c-state-nonlit-pos-cache nil)
3223 (c-state-nonlit-pos-cache-limit 1)
3224 (c-state-brace-pair-desert nil)
3225 (c-state-point-min 1)
3226 (c-state-point-min-lit-type nil)
3227 (c-state-point-min-lit-start nil)
3228 (c-state-min-scan-pos 1)
3229 (c-state-old-cpp-beg nil)
3230 (c-state-old-cpp-end nil))
3231 (setq res2 (c-real-parse-state)))
3232 (unless (equal res1 res2)
3233 ;; The cache can actually go further back due to the ad-hoc way
3234 ;; the first paren is found, so try to whack off a bit of its
3235 ;; start before complaining.
3236 ;; (save-excursion
3237 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3238 ;; (c-beginning-of-defun-1)
3239 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3240 ;; (c-beginning-of-defun-1))
3241 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3242 ;; (message (concat "c-parse-state inconsistency at %s: "
3243 ;; "using cache: %s, from scratch: %s")
3244 ;; here res1 res2)))
3245 (message (concat "c-parse-state inconsistency at %s: "
3246 "using cache: %s, from scratch: %s")
3247 here res1 res2)
3248 (message "Old state:")
3249 (c-replay-parse-state-state))
3250 (c-record-parse-state-state)
3251 res1))
3252
3253 (defun c-toggle-parse-state-debug (&optional arg)
3254 (interactive "P")
3255 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3256 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3257 'c-debug-parse-state
3258 'c-real-parse-state)))
3259 (c-keep-region-active))
3260 (when c-debug-parse-state
3261 (c-toggle-parse-state-debug 1))
3262
3263 \f
3264 (defun c-whack-state-before (bufpos paren-state)
3265 ;; Whack off any state information from PAREN-STATE which lies
3266 ;; before BUFPOS. Not destructive on PAREN-STATE.
3267 (let* ((newstate (list nil))
3268 (ptr newstate)
3269 car)
3270 (while paren-state
3271 (setq car (car paren-state)
3272 paren-state (cdr paren-state))
3273 (if (< (if (consp car) (car car) car) bufpos)
3274 (setq paren-state nil)
3275 (setcdr ptr (list car))
3276 (setq ptr (cdr ptr))))
3277 (cdr newstate)))
3278
3279 (defun c-whack-state-after (bufpos paren-state)
3280 ;; Whack off any state information from PAREN-STATE which lies at or
3281 ;; after BUFPOS. Not destructive on PAREN-STATE.
3282 (catch 'done
3283 (while paren-state
3284 (let ((car (car paren-state)))
3285 (if (consp car)
3286 ;; just check the car, because in a balanced brace
3287 ;; expression, it must be impossible for the corresponding
3288 ;; close brace to be before point, but the open brace to
3289 ;; be after.
3290 (if (<= bufpos (car car))
3291 nil ; whack it off
3292 (if (< bufpos (cdr car))
3293 ;; its possible that the open brace is before
3294 ;; bufpos, but the close brace is after. In that
3295 ;; case, convert this to a non-cons element. The
3296 ;; rest of the state is before bufpos, so we're
3297 ;; done.
3298 (throw 'done (cons (car car) (cdr paren-state)))
3299 ;; we know that both the open and close braces are
3300 ;; before bufpos, so we also know that everything else
3301 ;; on state is before bufpos.
3302 (throw 'done paren-state)))
3303 (if (<= bufpos car)
3304 nil ; whack it off
3305 ;; it's before bufpos, so everything else should too.
3306 (throw 'done paren-state)))
3307 (setq paren-state (cdr paren-state)))
3308 nil)))
3309
3310 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3311 ;; Return the bufpos of the innermost enclosing open paren before
3312 ;; bufpos, or nil if none was found.
3313 (let (enclosingp)
3314 (or bufpos (setq bufpos 134217727))
3315 (while paren-state
3316 (setq enclosingp (car paren-state)
3317 paren-state (cdr paren-state))
3318 (if (or (consp enclosingp)
3319 (>= enclosingp bufpos))
3320 (setq enclosingp nil)
3321 (setq paren-state nil)))
3322 enclosingp))
3323
3324 (defun c-least-enclosing-brace (paren-state)
3325 ;; Return the bufpos of the outermost enclosing open paren, or nil
3326 ;; if none was found.
3327 (let (pos elem)
3328 (while paren-state
3329 (setq elem (car paren-state)
3330 paren-state (cdr paren-state))
3331 (if (integerp elem)
3332 (setq pos elem)))
3333 pos))
3334
3335 (defun c-safe-position (bufpos paren-state)
3336 ;; Return the closest "safe" position recorded on PAREN-STATE that
3337 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3338 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3339 ;; find the closest limit before a given limit that might be nil.
3340 ;;
3341 ;; A "safe" position is a position at or after a recorded open
3342 ;; paren, or after a recorded close paren. The returned position is
3343 ;; thus either the first position after a close brace, or the first
3344 ;; position after an enclosing paren, or at the enclosing paren in
3345 ;; case BUFPOS is immediately after it.
3346 (when bufpos
3347 (let (elem)
3348 (catch 'done
3349 (while paren-state
3350 (setq elem (car paren-state))
3351 (if (consp elem)
3352 (cond ((< (cdr elem) bufpos)
3353 (throw 'done (cdr elem)))
3354 ((< (car elem) bufpos)
3355 ;; See below.
3356 (throw 'done (min (1+ (car elem)) bufpos))))
3357 (if (< elem bufpos)
3358 ;; elem is the position at and not after the opening paren, so
3359 ;; we can go forward one more step unless it's equal to
3360 ;; bufpos. This is useful in some cases avoid an extra paren
3361 ;; level between the safe position and bufpos.
3362 (throw 'done (min (1+ elem) bufpos))))
3363 (setq paren-state (cdr paren-state)))))))
3364
3365 (defun c-beginning-of-syntax ()
3366 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3367 ;; goes to the closest previous point that is known to be outside
3368 ;; any string literal or comment. `c-state-cache' is used if it has
3369 ;; a position in the vicinity.
3370 (let* ((paren-state c-state-cache)
3371 elem
3372
3373 (pos (catch 'done
3374 ;; Note: Similar code in `c-safe-position'. The
3375 ;; difference is that we accept a safe position at
3376 ;; the point and don't bother to go forward past open
3377 ;; parens.
3378 (while paren-state
3379 (setq elem (car paren-state))
3380 (if (consp elem)
3381 (cond ((<= (cdr elem) (point))
3382 (throw 'done (cdr elem)))
3383 ((<= (car elem) (point))
3384 (throw 'done (car elem))))
3385 (if (<= elem (point))
3386 (throw 'done elem)))
3387 (setq paren-state (cdr paren-state)))
3388 (point-min))))
3389
3390 (if (> pos (- (point) 4000))
3391 (goto-char pos)
3392 ;; The position is far back. Try `c-beginning-of-defun-1'
3393 ;; (although we can't be entirely sure it will go to a position
3394 ;; outside a comment or string in current emacsen). FIXME:
3395 ;; Consult `syntax-ppss' here.
3396 (c-beginning-of-defun-1)
3397 (if (< (point) pos)
3398 (goto-char pos)))))
3399
3400 \f
3401 ;; Tools for scanning identifiers and other tokens.
3402
3403 (defun c-on-identifier ()
3404 "Return non-nil if the point is on or directly after an identifier.
3405 Keywords are recognized and not considered identifiers. If an
3406 identifier is detected, the returned value is its starting position.
3407 If an identifier ends at the point and another begins at it \(can only
3408 happen in Pike) then the point for the preceding one is returned.
3409
3410 Note that this function might do hidden buffer changes. See the
3411 comment at the start of cc-engine.el for more info."
3412
3413 ;; FIXME: Shouldn't this function handle "operator" in C++?
3414
3415 (save-excursion
3416 (skip-syntax-backward "w_")
3417
3418 (or
3419
3420 ;; Check for a normal (non-keyword) identifier.
3421 (and (looking-at c-symbol-start)
3422 (not (looking-at c-keywords-regexp))
3423 (point))
3424
3425 (when (c-major-mode-is 'pike-mode)
3426 ;; Handle the `<operator> syntax in Pike.
3427 (let ((pos (point)))
3428 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3429 (and (if (< (skip-chars-backward "`") 0)
3430 t
3431 (goto-char pos)
3432 (eq (char-after) ?\`))
3433 (looking-at c-symbol-key)
3434 (>= (match-end 0) pos)
3435 (point))))
3436
3437 ;; Handle the "operator +" syntax in C++.
3438 (when (and c-overloadable-operators-regexp
3439 (= (c-backward-token-2 0) 0))
3440
3441 (cond ((and (looking-at c-overloadable-operators-regexp)
3442 (or (not c-opt-op-identifier-prefix)
3443 (and (= (c-backward-token-2 1) 0)
3444 (looking-at c-opt-op-identifier-prefix))))
3445 (point))
3446
3447 ((save-excursion
3448 (and c-opt-op-identifier-prefix
3449 (looking-at c-opt-op-identifier-prefix)
3450 (= (c-forward-token-2 1) 0)
3451 (looking-at c-overloadable-operators-regexp)))
3452 (point))))
3453
3454 )))
3455
3456 (defsubst c-simple-skip-symbol-backward ()
3457 ;; If the point is at the end of a symbol then skip backward to the
3458 ;; beginning of it. Don't move otherwise. Return non-nil if point
3459 ;; moved.
3460 ;;
3461 ;; This function might do hidden buffer changes.
3462 (or (< (skip-syntax-backward "w_") 0)
3463 (and (c-major-mode-is 'pike-mode)
3464 ;; Handle the `<operator> syntax in Pike.
3465 (let ((pos (point)))
3466 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3467 (< (skip-chars-backward "`") 0)
3468 (looking-at c-symbol-key)
3469 (>= (match-end 0) pos))
3470 t
3471 (goto-char pos)
3472 nil)))))
3473
3474 (defun c-beginning-of-current-token (&optional back-limit)
3475 ;; Move to the beginning of the current token. Do not move if not
3476 ;; in the middle of one. BACK-LIMIT may be used to bound the
3477 ;; backward search; if given it's assumed to be at the boundary
3478 ;; between two tokens. Return non-nil if the point is moved, nil
3479 ;; otherwise.
3480 ;;
3481 ;; This function might do hidden buffer changes.
3482 (let ((start (point)))
3483 (if (looking-at "\\w\\|\\s_")
3484 (skip-syntax-backward "w_" back-limit)
3485 (when (< (skip-syntax-backward ".()" back-limit) 0)
3486 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3487 (match-end 0))
3488 ;; `c-nonsymbol-token-regexp' should always match
3489 ;; since we've skipped backward over punctuator
3490 ;; or paren syntax, but consume one char in case
3491 ;; it doesn't so that we don't leave point before
3492 ;; some earlier incorrect token.
3493 (1+ (point)))))
3494 (if (<= pos start)
3495 (goto-char pos))))))
3496 (< (point) start)))
3497
3498 (defun c-end-of-current-token (&optional back-limit)
3499 ;; Move to the end of the current token. Do not move if not in the
3500 ;; middle of one. BACK-LIMIT may be used to bound the backward
3501 ;; search; if given it's assumed to be at the boundary between two
3502 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3503 ;;
3504 ;; This function might do hidden buffer changes.
3505 (let ((start (point)))
3506 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3507 (skip-syntax-forward "w_"))
3508 ((< (skip-syntax-backward ".()" back-limit) 0)
3509 (while (progn
3510 (if (looking-at c-nonsymbol-token-regexp)
3511 (goto-char (match-end 0))
3512 ;; `c-nonsymbol-token-regexp' should always match since
3513 ;; we've skipped backward over punctuator or paren
3514 ;; syntax, but move forward in case it doesn't so that
3515 ;; we don't leave point earlier than we started with.
3516 (forward-char))
3517 (< (point) start)))))
3518 (> (point) start)))
3519
3520 (defconst c-jump-syntax-balanced
3521 (if (memq 'gen-string-delim c-emacs-features)
3522 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3523 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3524
3525 (defconst c-jump-syntax-unbalanced
3526 (if (memq 'gen-string-delim c-emacs-features)
3527 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3528 "\\w\\|\\s_\\|\\s\""))
3529
3530 (defun c-forward-token-2 (&optional count balanced limit)
3531 "Move forward by tokens.
3532 A token is defined as all symbols and identifiers which aren't
3533 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3534 treated properly). Point is always either left at the beginning of a
3535 token or not moved at all. COUNT specifies the number of tokens to
3536 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3537 moves to the next token beginning only if not already at one. If
3538 BALANCED is true, move over balanced parens, otherwise move into them.
3539 Also, if BALANCED is true, never move out of an enclosing paren.
3540
3541 LIMIT sets the limit for the movement and defaults to the point limit.
3542 The case when LIMIT is set in the middle of a token, comment or macro
3543 is handled correctly, i.e. the point won't be left there.
3544
3545 Return the number of tokens left to move \(positive or negative). If
3546 BALANCED is true, a move over a balanced paren counts as one. Note
3547 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3548 be returned. Thus, a return value of 0 guarantees that point is at
3549 the requested position and a return value less \(without signs) than
3550 COUNT guarantees that point is at the beginning of some token.
3551
3552 Note that this function might do hidden buffer changes. See the
3553 comment at the start of cc-engine.el for more info."
3554
3555 (or count (setq count 1))
3556 (if (< count 0)
3557 (- (c-backward-token-2 (- count) balanced limit))
3558
3559 (let ((jump-syntax (if balanced
3560 c-jump-syntax-balanced
3561 c-jump-syntax-unbalanced))
3562 (last (point))
3563 (prev (point)))
3564
3565 (if (zerop count)
3566 ;; If count is zero we should jump if in the middle of a token.
3567 (c-end-of-current-token))
3568
3569 (save-restriction
3570 (if limit (narrow-to-region (point-min) limit))
3571 (if (/= (point)
3572 (progn (c-forward-syntactic-ws) (point)))
3573 ;; Skip whitespace. Count this as a move if we did in
3574 ;; fact move.
3575 (setq count (max (1- count) 0)))
3576
3577 (if (eobp)
3578 ;; Moved out of bounds. Make sure the returned count isn't zero.
3579 (progn
3580 (if (zerop count) (setq count 1))
3581 (goto-char last))
3582
3583 ;; Use `condition-case' to avoid having the limit tests
3584 ;; inside the loop.
3585 (condition-case nil
3586 (while (and
3587 (> count 0)
3588 (progn
3589 (setq last (point))
3590 (cond ((looking-at jump-syntax)
3591 (goto-char (scan-sexps (point) 1))
3592 t)
3593 ((looking-at c-nonsymbol-token-regexp)
3594 (goto-char (match-end 0))
3595 t)
3596 ;; `c-nonsymbol-token-regexp' above should always
3597 ;; match if there are correct tokens. Try to
3598 ;; widen to see if the limit was set in the
3599 ;; middle of one, else fall back to treating
3600 ;; the offending thing as a one character token.
3601 ((and limit
3602 (save-restriction
3603 (widen)
3604 (looking-at c-nonsymbol-token-regexp)))
3605 nil)
3606 (t
3607 (forward-char)
3608 t))))
3609 (c-forward-syntactic-ws)
3610 (setq prev last
3611 count (1- count)))
3612 (error (goto-char last)))
3613
3614 (when (eobp)
3615 (goto-char prev)
3616 (setq count (1+ count)))))
3617
3618 count)))
3619
3620 (defun c-backward-token-2 (&optional count balanced limit)
3621 "Move backward by tokens.
3622 See `c-forward-token-2' for details."
3623
3624 (or count (setq count 1))
3625 (if (< count 0)
3626 (- (c-forward-token-2 (- count) balanced limit))
3627
3628 (or limit (setq limit (point-min)))
3629 (let ((jump-syntax (if balanced
3630 c-jump-syntax-balanced
3631 c-jump-syntax-unbalanced))
3632 (last (point)))
3633
3634 (if (zerop count)
3635 ;; The count is zero so try to skip to the beginning of the
3636 ;; current token.
3637 (if (> (point)
3638 (progn (c-beginning-of-current-token) (point)))
3639 (if (< (point) limit)
3640 ;; The limit is inside the same token, so return 1.
3641 (setq count 1))
3642
3643 ;; We're not in the middle of a token. If there's
3644 ;; whitespace after the point then we must move backward,
3645 ;; so set count to 1 in that case.
3646 (and (looking-at c-syntactic-ws-start)
3647 ;; If we're looking at a '#' that might start a cpp
3648 ;; directive then we have to do a more elaborate check.
3649 (or (/= (char-after) ?#)
3650 (not c-opt-cpp-prefix)
3651 (save-excursion
3652 (and (= (point)
3653 (progn (beginning-of-line)
3654 (looking-at "[ \t]*")
3655 (match-end 0)))
3656 (or (bobp)
3657 (progn (backward-char)
3658 (not (eq (char-before) ?\\)))))))
3659 (setq count 1))))
3660
3661 ;; Use `condition-case' to avoid having to check for buffer
3662 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3663 (condition-case nil
3664 (while (and
3665 (> count 0)
3666 (progn
3667 (c-backward-syntactic-ws)
3668 (backward-char)
3669 (if (looking-at jump-syntax)
3670 (goto-char (scan-sexps (1+ (point)) -1))
3671 ;; This can be very inefficient if there's a long
3672 ;; sequence of operator tokens without any separation.
3673 ;; That doesn't happen in practice, anyway.
3674 (c-beginning-of-current-token))
3675 (>= (point) limit)))
3676 (setq last (point)
3677 count (1- count)))
3678 (error (goto-char last)))
3679
3680 (if (< (point) limit)
3681 (goto-char last))
3682
3683 count)))
3684
3685 (defun c-forward-token-1 (&optional count balanced limit)
3686 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3687 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3688 characters are jumped over character by character. This function is
3689 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3690 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3691 (c-forward-token-2 count balanced limit)))
3692
3693 (defun c-backward-token-1 (&optional count balanced limit)
3694 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3695 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3696 characters are jumped over character by character. This function is
3697 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3698 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3699 (c-backward-token-2 count balanced limit)))
3700
3701 \f
3702 ;; Tools for doing searches restricted to syntactically relevant text.
3703
3704 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3705 paren-level not-inside-token
3706 lookbehind-submatch)
3707 "Like `re-search-forward', but only report matches that are found
3708 in syntactically significant text. I.e. matches in comments, macros
3709 or string literals are ignored. The start point is assumed to be
3710 outside any comment, macro or string literal, or else the content of
3711 that region is taken as syntactically significant text.
3712
3713 If PAREN-LEVEL is non-nil, an additional restriction is added to
3714 ignore matches in nested paren sexps. The search will also not go
3715 outside the current list sexp, which has the effect that if the point
3716 should be moved to BOUND when no match is found \(i.e. NOERROR is
3717 neither nil nor t), then it will be at the closing paren if the end of
3718 the current list sexp is encountered first.
3719
3720 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3721 ignored. Things like multicharacter operators and special symbols
3722 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3723 constants.
3724
3725 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3726 subexpression in REGEXP. The end of that submatch is used as the
3727 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3728 isn't used or if that subexpression didn't match then the start
3729 position of the whole match is used instead. The \"look behind\"
3730 subexpression is never tested before the starting position, so it
3731 might be a good idea to include \\=\\= as a match alternative in it.
3732
3733 Optimization note: Matches might be missed if the \"look behind\"
3734 subexpression can match the end of nonwhite syntactic whitespace,
3735 i.e. the end of comments or cpp directives. This since the function
3736 skips over such things before resuming the search. It's on the other
3737 hand not safe to assume that the \"look behind\" subexpression never
3738 matches syntactic whitespace.
3739
3740 Bug: Unbalanced parens inside cpp directives are currently not handled
3741 correctly \(i.e. they don't get ignored as they should) when
3742 PAREN-LEVEL is set.
3743
3744 Note that this function might do hidden buffer changes. See the
3745 comment at the start of cc-engine.el for more info."
3746
3747 (or bound (setq bound (point-max)))
3748 (if paren-level (setq paren-level -1))
3749
3750 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3751
3752 (let ((start (point))
3753 tmp
3754 ;; Start position for the last search.
3755 search-pos
3756 ;; The `parse-partial-sexp' state between the start position
3757 ;; and the point.
3758 state
3759 ;; The current position after the last state update. The next
3760 ;; `parse-partial-sexp' continues from here.
3761 (state-pos (point))
3762 ;; The position at which to check the state and the state
3763 ;; there. This is separate from `state-pos' since we might
3764 ;; need to back up before doing the next search round.
3765 check-pos check-state
3766 ;; Last position known to end a token.
3767 (last-token-end-pos (point-min))
3768 ;; Set when a valid match is found.
3769 found)
3770
3771 (condition-case err
3772 (while
3773 (and
3774 (progn
3775 (setq search-pos (point))
3776 (re-search-forward regexp bound noerror))
3777
3778 (progn
3779 (setq state (parse-partial-sexp
3780 state-pos (match-beginning 0) paren-level nil state)
3781 state-pos (point))
3782 (if (setq check-pos (and lookbehind-submatch
3783 (or (not paren-level)
3784 (>= (car state) 0))
3785 (match-end lookbehind-submatch)))
3786 (setq check-state (parse-partial-sexp
3787 state-pos check-pos paren-level nil state))
3788 (setq check-pos state-pos
3789 check-state state))
3790
3791 ;; NOTE: If we got a look behind subexpression and get
3792 ;; an insignificant match in something that isn't
3793 ;; syntactic whitespace (i.e. strings or in nested
3794 ;; parentheses), then we can never skip more than a
3795 ;; single character from the match start position
3796 ;; (i.e. `state-pos' here) before continuing the
3797 ;; search. That since the look behind subexpression
3798 ;; might match the end of the insignificant region in
3799 ;; the next search.
3800
3801 (cond
3802 ((elt check-state 7)
3803 ;; Match inside a line comment. Skip to eol. Use
3804 ;; `re-search-forward' instead of `skip-chars-forward' to get
3805 ;; the right bound behavior.
3806 (re-search-forward "[\n\r]" bound noerror))
3807
3808 ((elt check-state 4)
3809 ;; Match inside a block comment. Skip to the '*/'.
3810 (search-forward "*/" bound noerror))
3811
3812 ((and (not (elt check-state 5))
3813 (eq (char-before check-pos) ?/)
3814 (not (c-get-char-property (1- check-pos) 'syntax-table))
3815 (memq (char-after check-pos) '(?/ ?*)))
3816 ;; Match in the middle of the opener of a block or line
3817 ;; comment.
3818 (if (= (char-after check-pos) ?/)
3819 (re-search-forward "[\n\r]" bound noerror)
3820 (search-forward "*/" bound noerror)))
3821
3822 ;; The last `parse-partial-sexp' above might have
3823 ;; stopped short of the real check position if the end
3824 ;; of the current sexp was encountered in paren-level
3825 ;; mode. The checks above are always false in that
3826 ;; case, and since they can do better skipping in
3827 ;; lookbehind-submatch mode, we do them before
3828 ;; checking the paren level.
3829
3830 ((and paren-level
3831 (/= (setq tmp (car check-state)) 0))
3832 ;; Check the paren level first since we're short of the
3833 ;; syntactic checking position if the end of the
3834 ;; current sexp was encountered by `parse-partial-sexp'.
3835 (if (> tmp 0)
3836
3837 ;; Inside a nested paren sexp.
3838 (if lookbehind-submatch
3839 ;; See the NOTE above.
3840 (progn (goto-char state-pos) t)
3841 ;; Skip out of the paren quickly.
3842 (setq state (parse-partial-sexp state-pos bound 0 nil state)
3843 state-pos (point)))
3844
3845 ;; Have exited the current paren sexp.
3846 (if noerror
3847 (progn
3848 ;; The last `parse-partial-sexp' call above
3849 ;; has left us just after the closing paren
3850 ;; in this case, so we can modify the bound
3851 ;; to leave the point at the right position
3852 ;; upon return.
3853 (setq bound (1- (point)))
3854 nil)
3855 (signal 'search-failed (list regexp)))))
3856
3857 ((setq tmp (elt check-state 3))
3858 ;; Match inside a string.
3859 (if (or lookbehind-submatch
3860 (not (integerp tmp)))
3861 ;; See the NOTE above.
3862 (progn (goto-char state-pos) t)
3863 ;; Skip to the end of the string before continuing.
3864 (let ((ender (make-string 1 tmp)) (continue t))
3865 (while (if (search-forward ender bound noerror)
3866 (progn
3867 (setq state (parse-partial-sexp
3868 state-pos (point) nil nil state)
3869 state-pos (point))
3870 (elt state 3))
3871 (setq continue nil)))
3872 continue)))
3873
3874 ((save-excursion
3875 (save-match-data
3876 (c-beginning-of-macro start)))
3877 ;; Match inside a macro. Skip to the end of it.
3878 (c-end-of-macro)
3879 (cond ((<= (point) bound) t)
3880 (noerror nil)
3881 (t (signal 'search-failed (list regexp)))))
3882
3883 ((and not-inside-token
3884 (or (< check-pos last-token-end-pos)
3885 (< check-pos
3886 (save-excursion
3887 (goto-char check-pos)
3888 (save-match-data
3889 (c-end-of-current-token last-token-end-pos))
3890 (setq last-token-end-pos (point))))))
3891 ;; Inside a token.
3892 (if lookbehind-submatch
3893 ;; See the NOTE above.
3894 (goto-char state-pos)
3895 (goto-char (min last-token-end-pos bound))))
3896
3897 (t
3898 ;; A real match.
3899 (setq found t)
3900 nil)))
3901
3902 ;; Should loop to search again, but take care to avoid
3903 ;; looping on the same spot.
3904 (or (/= search-pos (point))
3905 (if (= (point) bound)
3906 (if noerror
3907 nil
3908 (signal 'search-failed (list regexp)))
3909 (forward-char)
3910 t))))
3911
3912 (error
3913 (goto-char start)
3914 (signal (car err) (cdr err))))
3915
3916 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3917
3918 (if found
3919 (progn
3920 (goto-char (match-end 0))
3921 (match-end 0))
3922
3923 ;; Search failed. Set point as appropriate.
3924 (if (eq noerror t)
3925 (goto-char start)
3926 (goto-char bound))
3927 nil)))
3928
3929 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
3930
3931 (defsubst c-ssb-lit-begin ()
3932 ;; Return the start of the literal point is in, or nil.
3933 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3934 ;; bound in the caller.
3935
3936 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3937 ;; if it's outside comments and strings.
3938 (save-excursion
3939 (let ((pos (point)) safe-pos state pps-end-pos)
3940 ;; Pick a safe position as close to the point as possible.
3941 ;;
3942 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3943 ;; position.
3944
3945 (while (and safe-pos-list
3946 (> (car safe-pos-list) (point)))
3947 (setq safe-pos-list (cdr safe-pos-list)))
3948 (unless (setq safe-pos (car-safe safe-pos-list))
3949 (setq safe-pos (max (or (c-safe-position
3950 (point) (or c-state-cache
3951 (c-parse-state)))
3952 0)
3953 (point-min))
3954 safe-pos-list (list safe-pos)))
3955
3956 ;; Cache positions along the way to use if we have to back up more. We
3957 ;; cache every closing paren on the same level. If the paren cache is
3958 ;; relevant in this region then we're typically already on the same
3959 ;; level as the target position. Note that we might cache positions
3960 ;; after opening parens in case safe-pos is in a nested list. That's
3961 ;; both uncommon and harmless.
3962 (while (progn
3963 (setq state (parse-partial-sexp
3964 safe-pos pos 0))
3965 (< (point) pos))
3966 (setq safe-pos (point)
3967 safe-pos-list (cons safe-pos safe-pos-list)))
3968
3969 ;; If the state contains the start of the containing sexp we cache that
3970 ;; position too, so that parse-partial-sexp in the next run has a bigger
3971 ;; chance of starting at the same level as the target position and thus
3972 ;; will get more good safe positions into the list.
3973 (if (elt state 1)
3974 (setq safe-pos (1+ (elt state 1))
3975 safe-pos-list (cons safe-pos safe-pos-list)))
3976
3977 (if (or (elt state 3) (elt state 4))
3978 ;; Inside string or comment. Continue search at the
3979 ;; beginning of it.
3980 (elt state 8)))))
3981
3982 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3983 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3984 i.e. don't stop at positions inside syntactic whitespace or string
3985 literals. Preprocessor directives are also ignored, with the exception
3986 of the one that the point starts within, if any. If LIMIT is given,
3987 it's assumed to be at a syntactically relevant position.
3988
3989 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3990 sexps, and the search will also not go outside the current paren sexp.
3991 However, if LIMIT or the buffer limit is reached inside a nested paren
3992 then the point will be left at the limit.
3993
3994 Non-nil is returned if the point moved, nil otherwise.
3995
3996 Note that this function might do hidden buffer changes. See the
3997 comment at the start of cc-engine.el for more info."
3998
3999 (let ((start (point))
4000 state-2
4001 ;; A list of syntactically relevant positions in descending
4002 ;; order. It's used to avoid scanning repeatedly over
4003 ;; potentially large regions with `parse-partial-sexp' to verify
4004 ;; each position. Used in `c-ssb-lit-begin'
4005 safe-pos-list
4006 ;; The result from `c-beginning-of-macro' at the start position or the
4007 ;; start position itself if it isn't within a macro. Evaluated on
4008 ;; demand.
4009 start-macro-beg
4010 ;; The earliest position after the current one with the same paren
4011 ;; level. Used only when `paren-level' is set.
4012 lit-beg
4013 (paren-level-pos (point)))
4014
4015 (while
4016 (progn
4017 ;; The next loop "tries" to find the end point each time round,
4018 ;; loops when it hasn't succeeded.
4019 (while
4020 (and
4021 (< (skip-chars-backward skip-chars limit) 0)
4022
4023 (let ((pos (point)) state-2 pps-end-pos)
4024
4025 (cond
4026 ;; Don't stop inside a literal
4027 ((setq lit-beg (c-ssb-lit-begin))
4028 (goto-char lit-beg)
4029 t)
4030
4031 ((and paren-level
4032 (save-excursion
4033 (setq state-2 (parse-partial-sexp
4034 pos paren-level-pos -1)
4035 pps-end-pos (point))
4036 (/= (car state-2) 0)))
4037 ;; Not at the right level.
4038
4039 (if (and (< (car state-2) 0)
4040 ;; We stop above if we go out of a paren.
4041 ;; Now check whether it precedes or is
4042 ;; nested in the starting sexp.
4043 (save-excursion
4044 (setq state-2
4045 (parse-partial-sexp
4046 pps-end-pos paren-level-pos
4047 nil nil state-2))
4048 (< (car state-2) 0)))
4049
4050 ;; We've stopped short of the starting position
4051 ;; so the hit was inside a nested list. Go up
4052 ;; until we are at the right level.
4053 (condition-case nil
4054 (progn
4055 (goto-char (scan-lists pos -1
4056 (- (car state-2))))
4057 (setq paren-level-pos (point))
4058 (if (and limit (>= limit paren-level-pos))
4059 (progn
4060 (goto-char limit)
4061 nil)
4062 t))
4063 (error
4064 (goto-char (or limit (point-min)))
4065 nil))
4066
4067 ;; The hit was outside the list at the start
4068 ;; position. Go to the start of the list and exit.
4069 (goto-char (1+ (elt state-2 1)))
4070 nil))
4071
4072 ((c-beginning-of-macro limit)
4073 ;; Inside a macro.
4074 (if (< (point)
4075 (or start-macro-beg
4076 (setq start-macro-beg
4077 (save-excursion
4078 (goto-char start)
4079 (c-beginning-of-macro limit)
4080 (point)))))
4081 t
4082
4083 ;; It's inside the same macro we started in so it's
4084 ;; a relevant match.
4085 (goto-char pos)
4086 nil))))))
4087
4088 (> (point)
4089 (progn
4090 ;; Skip syntactic ws afterwards so that we don't stop at the
4091 ;; end of a comment if `skip-chars' is something like "^/".
4092 (c-backward-syntactic-ws)
4093 (point)))))
4094
4095 ;; We might want to extend this with more useful return values in
4096 ;; the future.
4097 (/= (point) start)))
4098
4099 ;; The following is an alternative implementation of
4100 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4101 ;; track of the syntactic context. It turned out to be generally
4102 ;; slower than the one above which uses forward checks from earlier
4103 ;; safe positions.
4104 ;;
4105 ;;(defconst c-ssb-stop-re
4106 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4107 ;; ;; stop at to avoid going into comments and literals.
4108 ;; (concat
4109 ;; ;; Match comment end syntax and string literal syntax. Also match
4110 ;; ;; '/' for block comment endings (not covered by comment end
4111 ;; ;; syntax).
4112 ;; "\\s>\\|/\\|\\s\""
4113 ;; (if (memq 'gen-string-delim c-emacs-features)
4114 ;; "\\|\\s|"
4115 ;; "")
4116 ;; (if (memq 'gen-comment-delim c-emacs-features)
4117 ;; "\\|\\s!"
4118 ;; "")))
4119 ;;
4120 ;;(defconst c-ssb-stop-paren-re
4121 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4122 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4123 ;;
4124 ;;(defconst c-ssb-sexp-end-re
4125 ;; ;; Regexp matching the ending syntax of a complex sexp.
4126 ;; (concat c-string-limit-regexp "\\|\\s)"))
4127 ;;
4128 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4129 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4130 ;;i.e. don't stop at positions inside syntactic whitespace or string
4131 ;;literals. Preprocessor directives are also ignored. However, if the
4132 ;;point is within a comment, string literal or preprocessor directory to
4133 ;;begin with, its contents is treated as syntactically relevant chars.
4134 ;;If LIMIT is given, it limits the backward search and the point will be
4135 ;;left there if no earlier position is found.
4136 ;;
4137 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4138 ;;sexps, and the search will also not go outside the current paren sexp.
4139 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4140 ;;then the point will be left at the limit.
4141 ;;
4142 ;;Non-nil is returned if the point moved, nil otherwise.
4143 ;;
4144 ;;Note that this function might do hidden buffer changes. See the
4145 ;;comment at the start of cc-engine.el for more info."
4146 ;;
4147 ;; (save-restriction
4148 ;; (when limit
4149 ;; (narrow-to-region limit (point-max)))
4150 ;;
4151 ;; (let ((start (point)))
4152 ;; (catch 'done
4153 ;; (while (let ((last-pos (point))
4154 ;; (stop-pos (progn
4155 ;; (skip-chars-backward skip-chars)
4156 ;; (point))))
4157 ;;
4158 ;; ;; Skip back over the same region as
4159 ;; ;; `skip-chars-backward' above, but keep to
4160 ;; ;; syntactically relevant positions.
4161 ;; (goto-char last-pos)
4162 ;; (while (and
4163 ;; ;; `re-search-backward' with a single char regexp
4164 ;; ;; should be fast.
4165 ;; (re-search-backward
4166 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4167 ;; stop-pos 'move)
4168 ;;
4169 ;; (progn
4170 ;; (cond
4171 ;; ((looking-at "\\s(")
4172 ;; ;; `paren-level' is set and we've found the
4173 ;; ;; start of the containing paren.
4174 ;; (forward-char)
4175 ;; (throw 'done t))
4176 ;;
4177 ;; ((looking-at c-ssb-sexp-end-re)
4178 ;; ;; We're at the end of a string literal or paren
4179 ;; ;; sexp (if `paren-level' is set).
4180 ;; (forward-char)
4181 ;; (condition-case nil
4182 ;; (c-backward-sexp)
4183 ;; (error
4184 ;; (goto-char limit)
4185 ;; (throw 'done t))))
4186 ;;
4187 ;; (t
4188 ;; (forward-char)
4189 ;; ;; At the end of some syntactic ws or possibly
4190 ;; ;; after a plain '/' operator.
4191 ;; (let ((pos (point)))
4192 ;; (c-backward-syntactic-ws)
4193 ;; (if (= pos (point))
4194 ;; ;; Was a plain '/' operator. Go past it.
4195 ;; (backward-char)))))
4196 ;;
4197 ;; (> (point) stop-pos))))
4198 ;;
4199 ;; ;; Now the point is either at `stop-pos' or at some
4200 ;; ;; position further back if `stop-pos' was at a
4201 ;; ;; syntactically irrelevant place.
4202 ;;
4203 ;; ;; Skip additional syntactic ws so that we don't stop
4204 ;; ;; at the end of a comment if `skip-chars' is
4205 ;; ;; something like "^/".
4206 ;; (c-backward-syntactic-ws)
4207 ;;
4208 ;; (< (point) stop-pos))))
4209 ;;
4210 ;; ;; We might want to extend this with more useful return values
4211 ;; ;; in the future.
4212 ;; (/= (point) start))))
4213
4214 \f
4215 ;; Tools for handling comments and string literals.
4216
4217 (defun c-in-literal (&optional lim detect-cpp)
4218 "Return the type of literal point is in, if any.
4219 The return value is `c' if in a C-style comment, `c++' if in a C++
4220 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4221 is non-nil and in a preprocessor line, or nil if somewhere else.
4222 Optional LIM is used as the backward limit of the search. If omitted,
4223 or nil, `c-beginning-of-defun' is used.
4224
4225 The last point calculated is cached if the cache is enabled, i.e. if
4226 `c-in-literal-cache' is bound to a two element vector.
4227
4228 Note that this function might do hidden buffer changes. See the
4229 comment at the start of cc-engine.el for more info."
4230 (save-restriction
4231 (widen)
4232 (let* ((safe-place (c-state-safe-place (point)))
4233 (lit (c-state-pp-to-literal safe-place (point))))
4234 (or (cadr lit)
4235 (and detect-cpp
4236 (save-excursion (c-beginning-of-macro))
4237 'pound)))))
4238
4239 (defun c-literal-limits (&optional lim near not-in-delimiter)
4240 "Return a cons of the beginning and end positions of the comment or
4241 string surrounding point (including both delimiters), or nil if point
4242 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4243 to start parsing from. If NEAR is non-nil, then the limits of any
4244 literal next to point is returned. \"Next to\" means there's only
4245 spaces and tabs between point and the literal. The search for such a
4246 literal is done first in forward direction. If NOT-IN-DELIMITER is
4247 non-nil, the case when point is inside a starting delimiter won't be
4248 recognized. This only has effect for comments which have starting
4249 delimiters with more than one character.
4250
4251 Note that this function might do hidden buffer changes. See the
4252 comment at the start of cc-engine.el for more info."
4253
4254 (save-excursion
4255 (let* ((pos (point))
4256 (lim (or lim (c-state-safe-place pos)))
4257 (pp-to-lit (save-restriction
4258 (widen)
4259 (c-state-pp-to-literal lim pos)))
4260 (state (car pp-to-lit))
4261 (lit-limits (car (cddr pp-to-lit))))
4262
4263 (cond
4264 (lit-limits)
4265 ((and (not not-in-delimiter)
4266 (not (elt state 5))
4267 (eq (char-before) ?/)
4268 (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28.
4269 ;; We're standing in a comment starter.
4270 (backward-char 1)
4271 (cons (point) (progn (c-forward-single-comment) (point))))
4272
4273 (near
4274 (goto-char pos)
4275 ;; Search forward for a literal.
4276 (skip-chars-forward " \t")
4277 (cond
4278 ((looking-at c-string-limit-regexp) ; String.
4279 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4280 (point-max))))
4281
4282 ((looking-at c-comment-start-regexp) ; Line or block comment.
4283 (cons (point) (progn (c-forward-single-comment) (point))))
4284
4285 (t
4286 ;; Search backward.
4287 (skip-chars-backward " \t")
4288
4289 (let ((end (point)) beg)
4290 (cond
4291 ((save-excursion
4292 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4293 (setq beg (c-safe (c-backward-sexp 1) (point))))
4294
4295 ((and (c-safe (forward-char -2) t)
4296 (looking-at "*/"))
4297 ;; Block comment. Due to the nature of line
4298 ;; comments, they will always be covered by the
4299 ;; normal case above.
4300 (goto-char end)
4301 (c-backward-single-comment)
4302 ;; If LIM is bogus, beg will be bogus.
4303 (setq beg (point))))
4304
4305 (if beg (cons beg end))))))
4306 ))))
4307
4308 ;; In case external callers use this; it did have a docstring.
4309 (defalias 'c-literal-limits-fast 'c-literal-limits)
4310
4311 (defun c-collect-line-comments (range)
4312 "If the argument is a cons of two buffer positions (such as returned by
4313 `c-literal-limits'), and that range contains a C++ style line comment,
4314 then an extended range is returned that contains all adjacent line
4315 comments (i.e. all comments that starts in the same column with no
4316 empty lines or non-whitespace characters between them). Otherwise the
4317 argument is returned.
4318
4319 Note that this function might do hidden buffer changes. See the
4320 comment at the start of cc-engine.el for more info."
4321
4322 (save-excursion
4323 (condition-case nil
4324 (if (and (consp range) (progn
4325 (goto-char (car range))
4326 (looking-at c-line-comment-starter)))
4327 (let ((col (current-column))
4328 (beg (point))
4329 (bopl (c-point 'bopl))
4330 (end (cdr range)))
4331 ;; Got to take care in the backward direction to handle
4332 ;; comments which are preceded by code.
4333 (while (and (c-backward-single-comment)
4334 (>= (point) bopl)
4335 (looking-at c-line-comment-starter)
4336 (= col (current-column)))
4337 (setq beg (point)
4338 bopl (c-point 'bopl)))
4339 (goto-char end)
4340 (while (and (progn (skip-chars-forward " \t")
4341 (looking-at c-line-comment-starter))
4342 (= col (current-column))
4343 (prog1 (zerop (forward-line 1))
4344 (setq end (point)))))
4345 (cons beg end))
4346 range)
4347 (error range))))
4348
4349 (defun c-literal-type (range)
4350 "Convenience function that given the result of `c-literal-limits',
4351 returns nil or the type of literal that the range surrounds, one
4352 of the symbols 'c, 'c++ or 'string. It's much faster than using
4353 `c-in-literal' and is intended to be used when you need both the
4354 type of a literal and its limits.
4355
4356 Note that this function might do hidden buffer changes. See the
4357 comment at the start of cc-engine.el for more info."
4358
4359 (if (consp range)
4360 (save-excursion
4361 (goto-char (car range))
4362 (cond ((looking-at c-string-limit-regexp) 'string)
4363 ((or (looking-at "//") ; c++ line comment
4364 (and (looking-at "\\s<") ; comment starter
4365 (looking-at "#"))) ; awk comment.
4366 'c++)
4367 (t 'c))) ; Assuming the range is valid.
4368 range))
4369
4370 (defsubst c-determine-limit-get-base (start try-size)
4371 ;; Get a "safe place" approximately TRY-SIZE characters before START.
4372 ;; This doesn't preserve point.
4373 (let* ((pos (max (- start try-size) (point-min)))
4374 (base (c-state-safe-place pos))
4375 (s (parse-partial-sexp base pos)))
4376 (if (or (nth 4 s) (nth 3 s)) ; comment or string
4377 (nth 8 s)
4378 (point))))
4379
4380 (defun c-determine-limit (how-far-back &optional start try-size)
4381 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
4382 ;; (default point). This is done by going back further in the buffer then
4383 ;; searching forward for literals. The position found won't be in a
4384 ;; literal. We start searching for the sought position TRY-SIZE (default
4385 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
4386 ;; :-)
4387 (save-excursion
4388 (let* ((start (or start (point)))
4389 (try-size (or try-size (* 2 how-far-back)))
4390 (base (c-determine-limit-get-base start try-size))
4391 (pos base)
4392
4393 (s (parse-partial-sexp pos pos)) ; null state.
4394 stack elt size
4395 (count 0))
4396 (while (< pos start)
4397 ;; Move forward one literal each time round this loop.
4398 ;; Move forward to the start of a comment or string.
4399 (setq s (parse-partial-sexp
4400 pos
4401 start
4402 nil ; target-depth
4403 nil ; stop-before
4404 s ; state
4405 'syntax-table)) ; stop-comment
4406
4407 ;; Gather details of the non-literal-bit - starting pos and size.
4408 (setq size (- (if (or (nth 4 s) (nth 3 s))
4409 (nth 8 s)
4410 (point))
4411 pos))
4412 (if (> size 0)
4413 (setq stack (cons (cons pos size) stack)))
4414
4415 ;; Move forward to the end of the comment/string.
4416 (if (or (nth 4 s) (nth 3 s))
4417 (setq s (parse-partial-sexp
4418 (point)
4419 start
4420 nil ; target-depth
4421 nil ; stop-before
4422 s ; state
4423 'syntax-table))) ; stop-comment
4424 (setq pos (point)))
4425
4426 ;; Now try and find enough non-literal characters recorded on the stack.
4427 ;; Go back one recorded literal each time round this loop.
4428 (while (and (< count how-far-back)
4429 stack)
4430 (setq elt (car stack)
4431 stack (cdr stack))
4432 (setq count (+ count (cdr elt))))
4433
4434 ;; Have we found enough yet?
4435 (cond
4436 ((>= count how-far-back)
4437 (+ (car elt) (- count how-far-back)))
4438 ((eq base (point-min))
4439 (point-min))
4440 (t
4441 (c-determine-limit (- how-far-back count) base try-size))))))
4442 \f
4443 ;; `c-find-decl-spots' and accompanying stuff.
4444
4445 ;; Variables used in `c-find-decl-spots' to cache the search done for
4446 ;; the first declaration in the last call. When that function starts,
4447 ;; it needs to back up over syntactic whitespace to look at the last
4448 ;; token before the region being searched. That can sometimes cause
4449 ;; moves back and forth over a quite large region of comments and
4450 ;; macros, which would be repeated for each changed character when
4451 ;; we're called during fontification, since font-lock refontifies the
4452 ;; current line for each change. Thus it's worthwhile to cache the
4453 ;; first match.
4454 ;;
4455 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4456 ;; the syntactic whitespace less or equal to some start position.
4457 ;; There's no cached value if it's nil.
4458 ;;
4459 ;; `c-find-decl-match-pos' is the match position if
4460 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4461 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4462 (defvar c-find-decl-syntactic-pos nil)
4463 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4464 (defvar c-find-decl-match-pos nil)
4465 (make-variable-buffer-local 'c-find-decl-match-pos)
4466
4467 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4468 (and c-find-decl-syntactic-pos
4469 (< change-min-pos c-find-decl-syntactic-pos)
4470 (setq c-find-decl-syntactic-pos nil)))
4471
4472 ; (defface c-debug-decl-spot-face
4473 ; '((t (:background "Turquoise")))
4474 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4475 ; (defface c-debug-decl-sws-face
4476 ; '((t (:background "Khaki")))
4477 ; "Debug face to mark the syntactic whitespace between the declaration
4478 ; spots and the preceding token end.")
4479
4480 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4481 (when (facep 'c-debug-decl-spot-face)
4482 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4483 (c-debug-add-face (max match-pos (point-min)) decl-pos
4484 'c-debug-decl-sws-face)
4485 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4486 'c-debug-decl-spot-face))))
4487 (defmacro c-debug-remove-decl-spot-faces (beg end)
4488 (when (facep 'c-debug-decl-spot-face)
4489 `(c-save-buffer-state ()
4490 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4491 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4492
4493 (defmacro c-find-decl-prefix-search ()
4494 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4495 ;; but it contains lots of free variables that refer to things
4496 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4497 ;; if there is a match, otherwise at `cfd-limit'.
4498 ;;
4499 ;; This macro might do hidden buffer changes.
4500
4501 '(progn
4502 ;; Find the next property match position if we haven't got one already.
4503 (unless cfd-prop-match
4504 (save-excursion
4505 (while (progn
4506 (goto-char (next-single-property-change
4507 (point) 'c-type nil cfd-limit))
4508 (and (< (point) cfd-limit)
4509 (not (eq (c-get-char-property (1- (point)) 'c-type)
4510 'c-decl-end)))))
4511 (setq cfd-prop-match (point))))
4512
4513 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4514 ;; got one already.
4515 (unless cfd-re-match
4516
4517 (if (> cfd-re-match-end (point))
4518 (goto-char cfd-re-match-end))
4519
4520 (while (if (setq cfd-re-match-end
4521 (re-search-forward c-decl-prefix-or-start-re
4522 cfd-limit 'move))
4523
4524 ;; Match. Check if it's inside a comment or string literal.
4525 (c-got-face-at
4526 (if (setq cfd-re-match (match-end 1))
4527 ;; Matched the end of a token preceding a decl spot.
4528 (progn
4529 (goto-char cfd-re-match)
4530 (1- cfd-re-match))
4531 ;; Matched a token that start a decl spot.
4532 (goto-char (match-beginning 0))
4533 (point))
4534 c-literal-faces)
4535
4536 ;; No match. Finish up and exit the loop.
4537 (setq cfd-re-match cfd-limit)
4538 nil)
4539
4540 ;; Skip out of comments and string literals.
4541 (while (progn
4542 (goto-char (next-single-property-change
4543 (point) 'face nil cfd-limit))
4544 (and (< (point) cfd-limit)
4545 (c-got-face-at (point) c-literal-faces)))))
4546
4547 ;; If we matched at the decl start, we have to back up over the
4548 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4549 ;; any decl spots in the syntactic ws.
4550 (unless cfd-re-match
4551 (c-backward-syntactic-ws)
4552 (setq cfd-re-match (point))))
4553
4554 ;; Choose whichever match is closer to the start.
4555 (if (< cfd-re-match cfd-prop-match)
4556 (setq cfd-match-pos cfd-re-match
4557 cfd-re-match nil)
4558 (setq cfd-match-pos cfd-prop-match
4559 cfd-prop-match nil))
4560
4561 (goto-char cfd-match-pos)
4562
4563 (when (< cfd-match-pos cfd-limit)
4564 ;; Skip forward past comments only so we don't skip macros.
4565 (c-forward-comments)
4566 ;; Set the position to continue at. We can avoid going over
4567 ;; the comments skipped above a second time, but it's possible
4568 ;; that the comment skipping has taken us past `cfd-prop-match'
4569 ;; since the property might be used inside comments.
4570 (setq cfd-continue-pos (if cfd-prop-match
4571 (min cfd-prop-match (point))
4572 (point))))))
4573
4574 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4575 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4576 ;; label from the point to CFD-LIMIT.
4577 ;;
4578 ;; CFD-FUN is called with point at the start of the spot. It's passed two
4579 ;; arguments: The first is the end position of the token preceding the spot,
4580 ;; or 0 for the implicit match at bob. The second is a flag that is t when
4581 ;; the match is inside a macro. Point should be moved forward by at least
4582 ;; one token.
4583 ;;
4584 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
4585 ;; it should return non-nil to ensure that the next search will find them.
4586 ;;
4587 ;; Such a spot is:
4588 ;; o The first token after bob.
4589 ;; o The first token after the end of submatch 1 in
4590 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4591 ;; o The start of each `c-decl-prefix-or-start-re' match when
4592 ;; submatch 1 doesn't match.
4593 ;; o The first token after the end of each occurrence of the
4594 ;; `c-type' text property with the value `c-decl-end', provided
4595 ;; `c-type-decl-end-used' is set.
4596 ;;
4597 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4598 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4599 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4600 ;;
4601 ;; If the match is inside a macro then the buffer is narrowed to the
4602 ;; end of it, so that CFD-FUN can investigate the following tokens
4603 ;; without matching something that begins inside a macro and ends
4604 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4605 ;; CFD-FACE-CHECKLIST checks exist.
4606 ;;
4607 ;; The spots are visited approximately in order from top to bottom.
4608 ;; It's however the positions where `c-decl-prefix-or-start-re'
4609 ;; matches and where `c-decl-end' properties are found that are in
4610 ;; order. Since the spots often are at the following token, they
4611 ;; might be visited out of order insofar as more spots are reported
4612 ;; later on within the syntactic whitespace between the match
4613 ;; positions and their spots.
4614 ;;
4615 ;; It's assumed that comments and strings are fontified in the
4616 ;; searched range.
4617 ;;
4618 ;; This is mainly used in fontification, and so has an elaborate
4619 ;; cache to handle repeated calls from the same start position; see
4620 ;; the variables above.
4621 ;;
4622 ;; All variables in this function begin with `cfd-' to avoid name
4623 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4624 ;;
4625 ;; This function might do hidden buffer changes.
4626
4627 (let ((cfd-start-pos (point))
4628 (cfd-buffer-end (point-max))
4629 ;; The end of the token preceding the decl spot last found
4630 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4631 ;; no match.
4632 cfd-re-match
4633 ;; The end position of the last `c-decl-prefix-or-start-re'
4634 ;; match. If this is greater than `cfd-continue-pos', the
4635 ;; next regexp search is started here instead.
4636 (cfd-re-match-end (point-min))
4637 ;; The end of the last `c-decl-end' found by
4638 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4639 ;; match. If searching for the property isn't needed then we
4640 ;; disable it by setting it to `cfd-limit' directly.
4641 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4642 ;; The end of the token preceding the decl spot last found by
4643 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4644 ;; bob. `cfd-limit' if there's no match. In other words,
4645 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4646 (cfd-match-pos cfd-limit)
4647 ;; The position to continue searching at.
4648 cfd-continue-pos
4649 ;; The position of the last "real" token we've stopped at.
4650 ;; This can be greater than `cfd-continue-pos' when we get
4651 ;; hits inside macros or at `c-decl-end' positions inside
4652 ;; comments.
4653 (cfd-token-pos 0)
4654 ;; The end position of the last entered macro.
4655 (cfd-macro-end 0))
4656
4657 ;; Initialize by finding a syntactically relevant start position
4658 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4659 ;; search unless we're at bob.
4660
4661 (let (start-in-literal start-in-macro syntactic-pos)
4662 ;; Must back up a bit since we look for the end of the previous
4663 ;; statement or declaration, which is earlier than the first
4664 ;; returned match.
4665
4666 (cond
4667 ;; First we need to move to a syntactically relevant position.
4668 ;; Begin by backing out of comment or string literals.
4669 ((and
4670 (when (c-got-face-at (point) c-literal-faces)
4671 ;; Try to use the faces to back up to the start of the
4672 ;; literal. FIXME: What if the point is on a declaration
4673 ;; inside a comment?
4674 (while (and (not (bobp))
4675 (c-got-face-at (1- (point)) c-literal-faces))
4676 (goto-char (previous-single-property-change
4677 (point) 'face nil (point-min))))
4678
4679 ;; XEmacs doesn't fontify the quotes surrounding string
4680 ;; literals.
4681 (and (featurep 'xemacs)
4682 (eq (get-text-property (point) 'face)
4683 'font-lock-string-face)
4684 (not (bobp))
4685 (progn (backward-char)
4686 (not (looking-at c-string-limit-regexp)))
4687 (forward-char))
4688
4689 ;; Don't trust the literal to contain only literal faces
4690 ;; (the font lock package might not have fontified the
4691 ;; start of it at all, for instance) so check that we have
4692 ;; arrived at something that looks like a start or else
4693 ;; resort to `c-literal-limits'.
4694 (unless (looking-at c-literal-start-regexp)
4695 (let ((range (c-literal-limits)))
4696 (if range (goto-char (car range)))))
4697
4698 (setq start-in-literal (point)))
4699
4700 ;; The start is in a literal. If the limit is in the same
4701 ;; one we don't have to find a syntactic position etc. We
4702 ;; only check that if the limit is at or before bonl to save
4703 ;; time; it covers the by far most common case when font-lock
4704 ;; refontifies the current line only.
4705 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4706 (save-excursion
4707 (goto-char cfd-start-pos)
4708 (while (progn
4709 (goto-char (next-single-property-change
4710 (point) 'face nil cfd-limit))
4711 (and (< (point) cfd-limit)
4712 (c-got-face-at (point) c-literal-faces))))
4713 (= (point) cfd-limit)))
4714
4715 ;; Completely inside a literal. Set up variables to trig the
4716 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4717 ;; find a suitable start position.
4718 (setq cfd-continue-pos start-in-literal))
4719
4720 ;; Check if the region might be completely inside a macro, to
4721 ;; optimize that like the completely-inside-literal above.
4722 ((save-excursion
4723 (and (= (forward-line 1) 0)
4724 (bolp) ; forward-line has funny behavior at eob.
4725 (>= (point) cfd-limit)
4726 (progn (backward-char)
4727 (eq (char-before) ?\\))))
4728 ;; (Maybe) completely inside a macro. Only need to trig the
4729 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4730 ;; set things up.
4731 (setq cfd-continue-pos (1- cfd-start-pos)
4732 start-in-macro t))
4733
4734 (t
4735 ;; Back out of any macro so we don't miss any declaration
4736 ;; that could follow after it.
4737 (when (c-beginning-of-macro)
4738 (setq start-in-macro t))
4739
4740 ;; Now we're at a proper syntactically relevant position so we
4741 ;; can use the cache. But first clear it if it applied
4742 ;; further down.
4743 (c-invalidate-find-decl-cache cfd-start-pos)
4744
4745 (setq syntactic-pos (point))
4746 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
4747 ;; Don't have to do this if the cache is relevant here,
4748 ;; typically if the same line is refontified again. If
4749 ;; we're just some syntactic whitespace further down we can
4750 ;; still use the cache to limit the skipping.
4751 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
4752
4753 ;; If we hit `c-find-decl-syntactic-pos' and
4754 ;; `c-find-decl-match-pos' is set then we install the cached
4755 ;; values. If we hit `c-find-decl-syntactic-pos' and
4756 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4757 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4758 ;; and so we can continue the search from this point. If we
4759 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4760 ;; the right spot to begin searching anyway.
4761 (if (and (eq (point) c-find-decl-syntactic-pos)
4762 c-find-decl-match-pos)
4763 (setq cfd-match-pos c-find-decl-match-pos
4764 cfd-continue-pos syntactic-pos)
4765
4766 (setq c-find-decl-syntactic-pos syntactic-pos)
4767
4768 (when (if (bobp)
4769 ;; Always consider bob a match to get the first
4770 ;; declaration in the file. Do this separately instead of
4771 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4772 ;; regexp always can consume at least one character to
4773 ;; ensure that we won't get stuck in an infinite loop.
4774 (setq cfd-re-match 0)
4775 (backward-char)
4776 (c-beginning-of-current-token)
4777 (< (point) cfd-limit))
4778 ;; Do an initial search now. In the bob case above it's
4779 ;; only done to search for a `c-decl-end' spot.
4780 (c-find-decl-prefix-search))
4781
4782 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
4783 cfd-match-pos)))))
4784
4785 ;; Advance `cfd-continue-pos' if it's before the start position.
4786 ;; The closest continue position that might have effect at or
4787 ;; after the start depends on what we started in. This also
4788 ;; finds a suitable start position in the special cases when the
4789 ;; region is completely within a literal or macro.
4790 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
4791
4792 (cond
4793 (start-in-macro
4794 ;; If we're in a macro then it's the closest preceding token
4795 ;; in the macro. Check this before `start-in-literal',
4796 ;; since if we're inside a literal in a macro, the preceding
4797 ;; token is earlier than any `c-decl-end' spot inside the
4798 ;; literal (comment).
4799 (goto-char (or start-in-literal cfd-start-pos))
4800 ;; The only syntactic ws in macros are comments.
4801 (c-backward-comments)
4802 (backward-char)
4803 (c-beginning-of-current-token))
4804
4805 (start-in-literal
4806 ;; If we're in a comment it can only be the closest
4807 ;; preceding `c-decl-end' position within that comment, if
4808 ;; any. Go back to the beginning of such a property so that
4809 ;; `c-find-decl-prefix-search' will find the end of it.
4810 ;; (Can't stop at the end and install it directly on
4811 ;; `cfd-prop-match' since that variable might be cleared
4812 ;; after `cfd-fun' below.)
4813 ;;
4814 ;; Note that if the literal is a string then the property
4815 ;; search will simply skip to the beginning of it right
4816 ;; away.
4817 (if (not c-type-decl-end-used)
4818 (goto-char start-in-literal)
4819 (goto-char cfd-start-pos)
4820 (while (progn
4821 (goto-char (previous-single-property-change
4822 (point) 'c-type nil start-in-literal))
4823 (and (> (point) start-in-literal)
4824 (not (eq (c-get-char-property (point) 'c-type)
4825 'c-decl-end))))))
4826
4827 (when (= (point) start-in-literal)
4828 ;; Didn't find any property inside the comment, so we can
4829 ;; skip it entirely. (This won't skip past a string, but
4830 ;; that'll be handled quickly by the next
4831 ;; `c-find-decl-prefix-search' anyway.)
4832 (c-forward-single-comment)
4833 (if (> (point) cfd-limit)
4834 (goto-char cfd-limit))))
4835
4836 (t
4837 ;; If we started in normal code, the only match that might
4838 ;; apply before the start is what we already got in
4839 ;; `cfd-match-pos' so we can continue at the start position.
4840 ;; (Note that we don't get here if the first match is below
4841 ;; it.)
4842 (goto-char cfd-start-pos)))
4843
4844 ;; Delete found matches if they are before our new continue
4845 ;; position, so that `c-find-decl-prefix-search' won't back up
4846 ;; to them later on.
4847 (setq cfd-continue-pos (point))
4848 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
4849 (setq cfd-re-match nil))
4850 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
4851 (setq cfd-prop-match nil)))
4852
4853 (if syntactic-pos
4854 ;; This is the normal case and we got a proper syntactic
4855 ;; position. If there's a match then it's always outside
4856 ;; macros and comments, so advance to the next token and set
4857 ;; `cfd-token-pos'. The loop below will later go back using
4858 ;; `cfd-continue-pos' to fix declarations inside the
4859 ;; syntactic ws.
4860 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
4861 (goto-char syntactic-pos)
4862 (c-forward-syntactic-ws)
4863 (and cfd-continue-pos
4864 (< cfd-continue-pos (point))
4865 (setq cfd-token-pos (point))))
4866
4867 ;; Have one of the special cases when the region is completely
4868 ;; within a literal or macro. `cfd-continue-pos' is set to a
4869 ;; good start position for the search, so do it.
4870 (c-find-decl-prefix-search)))
4871
4872 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4873
4874 (while (progn
4875 (while (and
4876 (< cfd-match-pos cfd-limit)
4877
4878 (or
4879 ;; Kludge to filter out matches on the "<" that
4880 ;; aren't open parens, for the sake of languages
4881 ;; that got `c-recognize-<>-arglists' set.
4882 (and (eq (char-before cfd-match-pos) ?<)
4883 (not (c-get-char-property (1- cfd-match-pos)
4884 'syntax-table)))
4885
4886 ;; If `cfd-continue-pos' is less or equal to
4887 ;; `cfd-token-pos', we've got a hit inside a macro
4888 ;; that's in the syntactic whitespace before the last
4889 ;; "real" declaration we've checked. If they're equal
4890 ;; we've arrived at the declaration a second time, so
4891 ;; there's nothing to do.
4892 (= cfd-continue-pos cfd-token-pos)
4893
4894 (progn
4895 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4896 ;; we're still searching for declarations embedded in
4897 ;; the syntactic whitespace. In that case we need
4898 ;; only to skip comments and not macros, since they
4899 ;; can't be nested, and that's already been done in
4900 ;; `c-find-decl-prefix-search'.
4901 (when (> cfd-continue-pos cfd-token-pos)
4902 (c-forward-syntactic-ws)
4903 (setq cfd-token-pos (point)))
4904
4905 ;; Continue if the following token fails the
4906 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4907 (when (or (>= (point) cfd-limit)
4908 (not (looking-at cfd-decl-re))
4909 (and cfd-face-checklist
4910 (not (c-got-face-at
4911 (point) cfd-face-checklist))))
4912 (goto-char cfd-continue-pos)
4913 t)))
4914
4915 (< (point) cfd-limit))
4916 (c-find-decl-prefix-search))
4917
4918 (< (point) cfd-limit))
4919
4920 (when (and
4921 (>= (point) cfd-start-pos)
4922
4923 (progn
4924 ;; Narrow to the end of the macro if we got a hit inside
4925 ;; one, to avoid recognizing things that start inside the
4926 ;; macro and end outside it.
4927 (when (> cfd-match-pos cfd-macro-end)
4928 ;; Not in the same macro as in the previous round.
4929 (save-excursion
4930 (goto-char cfd-match-pos)
4931 (setq cfd-macro-end
4932 (if (save-excursion (and (c-beginning-of-macro)
4933 (< (point) cfd-match-pos)))
4934 (progn (c-end-of-macro)
4935 (point))
4936 0))))
4937
4938 (if (zerop cfd-macro-end)
4939 t
4940 (if (> cfd-macro-end (point))
4941 (progn (narrow-to-region (point-min) cfd-macro-end)
4942 t)
4943 ;; The matched token was the last thing in the macro,
4944 ;; so the whole match is bogus.
4945 (setq cfd-macro-end 0)
4946 nil))))
4947
4948 (c-debug-put-decl-spot-faces cfd-match-pos (point))
4949 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
4950 (setq cfd-prop-match nil))
4951
4952 (when (/= cfd-macro-end 0)
4953 ;; Restore limits if we did macro narrowing above.
4954 (narrow-to-region (point-min) cfd-buffer-end)))
4955
4956 (goto-char cfd-continue-pos)
4957 (if (= cfd-continue-pos cfd-limit)
4958 (setq cfd-match-pos cfd-limit)
4959 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
4960 ; cfd-match-pos, etc.
4961
4962 \f
4963 ;; A cache for found types.
4964
4965 ;; Buffer local variable that contains an obarray with the types we've
4966 ;; found. If a declaration is recognized somewhere we record the
4967 ;; fully qualified identifier in it to recognize it as a type
4968 ;; elsewhere in the file too. This is not accurate since we do not
4969 ;; bother with the scoping rules of the languages, but in practice the
4970 ;; same name is seldom used as both a type and something else in a
4971 ;; file, and we only use this as a last resort in ambiguous cases (see
4972 ;; `c-forward-decl-or-cast-1').
4973 ;;
4974 ;; Not every type need be in this cache. However, things which have
4975 ;; ceased to be types must be removed from it.
4976 ;;
4977 ;; Template types in C++ are added here too but with the template
4978 ;; arglist replaced with "<>" in references or "<" for the one in the
4979 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4980 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4981 ;; template specs can be fairly sized programs in themselves) and
4982 ;; improves the hit ratio (it's a type regardless of the template
4983 ;; args; it's just not the same type, but we're only interested in
4984 ;; recognizing types, not telling distinct types apart). Note that
4985 ;; template types in references are added here too; from the example
4986 ;; above there will also be an entry "Foo<".
4987 (defvar c-found-types nil)
4988 (make-variable-buffer-local 'c-found-types)
4989
4990 (defsubst c-clear-found-types ()
4991 ;; Clears `c-found-types'.
4992 (setq c-found-types (make-vector 53 0)))
4993
4994 (defun c-add-type (from to)
4995 ;; Add the given region as a type in `c-found-types'. If the region
4996 ;; doesn't match an existing type but there is a type which is equal
4997 ;; to the given one except that the last character is missing, then
4998 ;; the shorter type is removed. That's done to avoid adding all
4999 ;; prefixes of a type as it's being entered and font locked. This
5000 ;; doesn't cover cases like when characters are removed from a type
5001 ;; or added in the middle. We'd need the position of point when the
5002 ;; font locking is invoked to solve this well.
5003 ;;
5004 ;; This function might do hidden buffer changes.
5005 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
5006 (unless (intern-soft type c-found-types)
5007 (unintern (substring type 0 -1) c-found-types)
5008 (intern type c-found-types))))
5009
5010 (defun c-unfind-type (name)
5011 ;; Remove the "NAME" from c-found-types, if present.
5012 (unintern name c-found-types))
5013
5014 (defsubst c-check-type (from to)
5015 ;; Return non-nil if the given region contains a type in
5016 ;; `c-found-types'.
5017 ;;
5018 ;; This function might do hidden buffer changes.
5019 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
5020 c-found-types))
5021
5022 (defun c-list-found-types ()
5023 ;; Return all the types in `c-found-types' as a sorted list of
5024 ;; strings.
5025 (let (type-list)
5026 (mapatoms (lambda (type)
5027 (setq type-list (cons (symbol-name type)
5028 type-list)))
5029 c-found-types)
5030 (sort type-list 'string-lessp)))
5031
5032 ;; Shut up the byte compiler.
5033 (defvar c-maybe-stale-found-type)
5034
5035 (defun c-trim-found-types (beg end old-len)
5036 ;; An after change function which, in conjunction with the info in
5037 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
5038 ;; from `c-found-types', should this type have become stale. For
5039 ;; example, this happens to "foo" when "foo \n bar();" becomes
5040 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
5041 ;; the fontification.
5042 ;;
5043 ;; Have we, perhaps, added non-ws characters to the front/back of a found
5044 ;; type?
5045 (when (> end beg)
5046 (save-excursion
5047 (when (< end (point-max))
5048 (goto-char end)
5049 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
5050 (progn (goto-char end)
5051 (c-end-of-current-token)))
5052 (c-unfind-type (buffer-substring-no-properties
5053 end (point)))))
5054 (when (> beg (point-min))
5055 (goto-char beg)
5056 (if (and (c-end-of-current-token) ; only moves when we started in the middle
5057 (progn (goto-char beg)
5058 (c-beginning-of-current-token)))
5059 (c-unfind-type (buffer-substring-no-properties
5060 (point) beg))))))
5061
5062 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
5063 (cond
5064 ;; Changing the amount of (already existing) whitespace - don't do anything.
5065 ((and (c-partial-ws-p beg end)
5066 (or (= beg end) ; removal of WS
5067 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
5068
5069 ;; The syntactic relationship which defined a "found type" has been
5070 ;; destroyed.
5071 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
5072 (c-unfind-type (cadr c-maybe-stale-found-type)))
5073 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5074 )))
5075
5076 \f
5077 ;; Setting and removing syntax properties on < and > in languages (C++
5078 ;; and Java) where they can be template/generic delimiters as well as
5079 ;; their normal meaning of "less/greater than".
5080
5081 ;; Normally, < and > have syntax 'punctuation'. When they are found to
5082 ;; be delimiters, they are marked as such with the category properties
5083 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5084
5085 ;; STRATEGY:
5086 ;;
5087 ;; It is impossible to determine with certainty whether a <..> pair in
5088 ;; C++ is two comparison operators or is template delimiters, unless
5089 ;; one duplicates a lot of a C++ compiler. For example, the following
5090 ;; code fragment:
5091 ;;
5092 ;; foo (a < b, c > d) ;
5093 ;;
5094 ;; could be a function call with two integer parameters (each a
5095 ;; relational expression), or it could be a constructor for class foo
5096 ;; taking one parameter d of templated type "a < b, c >". They are
5097 ;; somewhat easier to distinguish in Java.
5098 ;;
5099 ;; The strategy now (2010-01) adopted is to mark and unmark < and
5100 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5101 ;; individually when their context so indicated. This gave rise to
5102 ;; intractable problems when one of a matching pair was deleted, or
5103 ;; pulled into a literal.]
5104 ;;
5105 ;; At each buffer change, the syntax-table properties are removed in a
5106 ;; before-change function and reapplied, when needed, in an
5107 ;; after-change function. It is far more important that the
5108 ;; properties get removed when they they are spurious than that they
5109 ;; be present when wanted.
5110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5111 (defun c-clear-<-pair-props (&optional pos)
5112 ;; POS (default point) is at a < character. If it is marked with
5113 ;; open paren syntax-table text property, remove the property,
5114 ;; together with the close paren property on the matching > (if
5115 ;; any).
5116 (save-excursion
5117 (if pos
5118 (goto-char pos)
5119 (setq pos (point)))
5120 (when (equal (c-get-char-property (point) 'syntax-table)
5121 c-<-as-paren-syntax)
5122 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5123 (c-go-list-forward))
5124 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5125 c->-as-paren-syntax) ; should always be true.
5126 (c-clear-char-property (1- (point)) 'category))
5127 (c-clear-char-property pos 'category))))
5128
5129 (defun c-clear->-pair-props (&optional pos)
5130 ;; POS (default point) is at a > character. If it is marked with
5131 ;; close paren syntax-table property, remove the property, together
5132 ;; with the open paren property on the matching < (if any).
5133 (save-excursion
5134 (if pos
5135 (goto-char pos)
5136 (setq pos (point)))
5137 (when (equal (c-get-char-property (point) 'syntax-table)
5138 c->-as-paren-syntax)
5139 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5140 (c-go-up-list-backward))
5141 (when (equal (c-get-char-property (point) 'syntax-table)
5142 c-<-as-paren-syntax) ; should always be true.
5143 (c-clear-char-property (point) 'category))
5144 (c-clear-char-property pos 'category))))
5145
5146 (defun c-clear-<>-pair-props (&optional pos)
5147 ;; POS (default point) is at a < or > character. If it has an
5148 ;; open/close paren syntax-table property, remove this property both
5149 ;; from the current character and its partner (which will also be
5150 ;; thusly marked).
5151 (cond
5152 ((eq (char-after) ?\<)
5153 (c-clear-<-pair-props pos))
5154 ((eq (char-after) ?\>)
5155 (c-clear->-pair-props pos))
5156 (t (c-benign-error
5157 "c-clear-<>-pair-props called from wrong position"))))
5158
5159 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5160 ;; POS (default point) is at a < character. If it is both marked
5161 ;; with open/close paren syntax-table property, and has a matching >
5162 ;; (also marked) which is after LIM, remove the property both from
5163 ;; the current > and its partner. Return t when this happens, nil
5164 ;; when it doesn't.
5165 (save-excursion
5166 (if pos
5167 (goto-char pos)
5168 (setq pos (point)))
5169 (when (equal (c-get-char-property (point) 'syntax-table)
5170 c-<-as-paren-syntax)
5171 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5172 (c-go-list-forward))
5173 (when (and (>= (point) lim)
5174 (equal (c-get-char-property (1- (point)) 'syntax-table)
5175 c->-as-paren-syntax)) ; should always be true.
5176 (c-unmark-<->-as-paren (1- (point)))
5177 (c-unmark-<->-as-paren pos))
5178 t)))
5179
5180 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
5181 ;; POS (default point) is at a > character. If it is both marked
5182 ;; with open/close paren syntax-table property, and has a matching <
5183 ;; (also marked) which is before LIM, remove the property both from
5184 ;; the current < and its partner. Return t when this happens, nil
5185 ;; when it doesn't.
5186 (save-excursion
5187 (if pos
5188 (goto-char pos)
5189 (setq pos (point)))
5190 (when (equal (c-get-char-property (point) 'syntax-table)
5191 c->-as-paren-syntax)
5192 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5193 (c-go-up-list-backward))
5194 (when (and (<= (point) lim)
5195 (equal (c-get-char-property (point) 'syntax-table)
5196 c-<-as-paren-syntax)) ; should always be true.
5197 (c-unmark-<->-as-paren (point))
5198 (c-unmark-<->-as-paren pos))
5199 t)))
5200
5201 ;; Set by c-common-init in cc-mode.el.
5202 (defvar c-new-BEG)
5203 (defvar c-new-END)
5204
5205 (defun c-before-change-check-<>-operators (beg end)
5206 ;; Unmark certain pairs of "< .... >" which are currently marked as
5207 ;; template/generic delimiters. (This marking is via syntax-table
5208 ;; text properties).
5209 ;;
5210 ;; These pairs are those which are in the current "statement" (i.e.,
5211 ;; the region between the {, }, or ; before BEG and the one after
5212 ;; END), and which enclose any part of the interval (BEG END).
5213 ;;
5214 ;; Note that in C++ (?and Java), template/generic parens cannot
5215 ;; enclose a brace or semicolon, so we use these as bounds on the
5216 ;; region we must work on.
5217 ;;
5218 ;; This function is called from before-change-functions (via
5219 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5220 ;; and point is undefined, both at entry and exit.
5221 ;;
5222 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5223 ;; 2010-01-29.
5224 (save-excursion
5225 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5226 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5227 new-beg new-end need-new-beg need-new-end)
5228 ;; Locate the barrier before the changed region
5229 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5230 (c-syntactic-skip-backward "^;{}" (max (- beg 2048) (point-min)))
5231 (setq new-beg (point))
5232
5233 ;; Remove the syntax-table properties from each pertinent <...> pair.
5234 ;; Firsly, the ones with the < before beg and > after beg.
5235 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5236 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5237 (setq need-new-beg t)))
5238
5239 ;; Locate the barrier after END.
5240 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5241 (c-syntactic-re-search-forward "[;{}]"
5242 (min (+ end 2048) (point-max)) 'end)
5243 (setq new-end (point))
5244
5245 ;; Remove syntax-table properties from the remaining pertinent <...>
5246 ;; pairs, those with a > after end and < before end.
5247 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5248 (if (c-clear->-pair-props-if-match-before end)
5249 (setq need-new-end t)))
5250
5251 ;; Extend the fontification region, if needed.
5252 (when need-new-beg
5253 (goto-char new-beg)
5254 (c-forward-syntactic-ws)
5255 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5256
5257 (when need-new-end
5258 (and (> new-end c-new-END) (setq c-new-END new-end))))))
5259
5260
5261
5262 (defun c-after-change-check-<>-operators (beg end)
5263 ;; This is called from `after-change-functions' when
5264 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5265 ;; chars with paren syntax become part of another operator like "<<"
5266 ;; or ">=".
5267 ;;
5268 ;; This function might do hidden buffer changes.
5269
5270 (save-excursion
5271 (goto-char beg)
5272 (when (or (looking-at "[<>]")
5273 (< (skip-chars-backward "<>") 0))
5274
5275 (goto-char beg)
5276 (c-beginning-of-current-token)
5277 (when (and (< (point) beg)
5278 (looking-at c-<>-multichar-token-regexp)
5279 (< beg (setq beg (match-end 0))))
5280 (while (progn (skip-chars-forward "^<>" beg)
5281 (< (point) beg))
5282 (c-clear-<>-pair-props)
5283 (forward-char))))
5284
5285 (when (< beg end)
5286 (goto-char end)
5287 (when (or (looking-at "[<>]")
5288 (< (skip-chars-backward "<>") 0))
5289
5290 (goto-char end)
5291 (c-beginning-of-current-token)
5292 (when (and (< (point) end)
5293 (looking-at c-<>-multichar-token-regexp)
5294 (< end (setq end (match-end 0))))
5295 (while (progn (skip-chars-forward "^<>" end)
5296 (< (point) end))
5297 (c-clear-<>-pair-props)
5298 (forward-char)))))))
5299
5300
5301 \f
5302 ;; Handling of small scale constructs like types and names.
5303
5304 ;; Dynamically bound variable that instructs `c-forward-type' to also
5305 ;; treat possible types (i.e. those that it normally returns 'maybe or
5306 ;; 'found for) as actual types (and always return 'found for them).
5307 ;; This means that it records them in `c-record-type-identifiers' if
5308 ;; that is set, and that it adds them to `c-found-types'.
5309 (defvar c-promote-possible-types nil)
5310
5311 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5312 ;; mark up successfully parsed arglists with paren syntax properties on
5313 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5314 ;; `c-type' property of each argument separating comma.
5315 ;;
5316 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5317 ;; all arglists for side effects (i.e. recording types), otherwise it
5318 ;; exploits any existing paren syntax properties to quickly jump to the
5319 ;; end of already parsed arglists.
5320 ;;
5321 ;; Marking up the arglists is not the default since doing that correctly
5322 ;; depends on a proper value for `c-restricted-<>-arglists'.
5323 (defvar c-parse-and-markup-<>-arglists nil)
5324
5325 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5326 ;; not accept arglists that contain binary operators.
5327 ;;
5328 ;; This is primarily used to handle C++ template arglists. C++
5329 ;; disambiguates them by checking whether the preceding name is a
5330 ;; template or not. We can't do that, so we assume it is a template
5331 ;; if it can be parsed as one. That usually works well since
5332 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5333 ;; in almost all cases would be pointless.
5334 ;;
5335 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5336 ;; should let the comma separate the function arguments instead. And
5337 ;; in a context where the value of the expression is taken, e.g. in
5338 ;; "if (a < b || c > d)", it's probably not a template.
5339 (defvar c-restricted-<>-arglists nil)
5340
5341 ;; Dynamically bound variables that instructs
5342 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5343 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5344 ;; `c-forward-label' to record the ranges of all the type and
5345 ;; reference identifiers they encounter. They will build lists on
5346 ;; these variables where each element is a cons of the buffer
5347 ;; positions surrounding each identifier. This recording is only
5348 ;; activated when `c-record-type-identifiers' is non-nil.
5349 ;;
5350 ;; All known types that can't be identifiers are recorded, and also
5351 ;; other possible types if `c-promote-possible-types' is set.
5352 ;; Recording is however disabled inside angle bracket arglists that
5353 ;; are encountered inside names and other angle bracket arglists.
5354 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5355 ;; instead.
5356 ;;
5357 ;; Only the names in C++ template style references (e.g. "tmpl" in
5358 ;; "tmpl<a,b>::foo") are recorded as references, other references
5359 ;; aren't handled here.
5360 ;;
5361 ;; `c-forward-label' records the label identifier(s) on
5362 ;; `c-record-ref-identifiers'.
5363 (defvar c-record-type-identifiers nil)
5364 (defvar c-record-ref-identifiers nil)
5365
5366 ;; This variable will receive a cons cell of the range of the last
5367 ;; single identifier symbol stepped over by `c-forward-name' if it's
5368 ;; successful. This is the range that should be put on one of the
5369 ;; record lists above by the caller. It's assigned nil if there's no
5370 ;; such symbol in the name.
5371 (defvar c-last-identifier-range nil)
5372
5373 (defmacro c-record-type-id (range)
5374 (if (eq (car-safe range) 'cons)
5375 ;; Always true.
5376 `(setq c-record-type-identifiers
5377 (cons ,range c-record-type-identifiers))
5378 `(let ((range ,range))
5379 (if range
5380 (setq c-record-type-identifiers
5381 (cons range c-record-type-identifiers))))))
5382
5383 (defmacro c-record-ref-id (range)
5384 (if (eq (car-safe range) 'cons)
5385 ;; Always true.
5386 `(setq c-record-ref-identifiers
5387 (cons ,range c-record-ref-identifiers))
5388 `(let ((range ,range))
5389 (if range
5390 (setq c-record-ref-identifiers
5391 (cons range c-record-ref-identifiers))))))
5392
5393 ;; Dynamically bound variable that instructs `c-forward-type' to
5394 ;; record the ranges of types that only are found. Behaves otherwise
5395 ;; like `c-record-type-identifiers'.
5396 (defvar c-record-found-types nil)
5397
5398 (defmacro c-forward-keyword-prefixed-id (type)
5399 ;; Used internally in `c-forward-keyword-clause' to move forward
5400 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5401 ;; possibly is prefixed by keywords and their associated clauses.
5402 ;; Try with a type/name first to not trip up on those that begin
5403 ;; with a keyword. Return t if a known or found type is moved
5404 ;; over. The point is clobbered if nil is returned. If range
5405 ;; recording is enabled, the identifier is recorded on as a type
5406 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5407 ;;
5408 ;; This macro might do hidden buffer changes.
5409 `(let (res)
5410 (while (if (setq res ,(if (eq type 'type)
5411 `(c-forward-type)
5412 `(c-forward-name)))
5413 nil
5414 (and (looking-at c-keywords-regexp)
5415 (c-forward-keyword-clause 1))))
5416 (when (memq res '(t known found prefix))
5417 ,(when (eq type 'ref)
5418 `(when c-record-type-identifiers
5419 (c-record-ref-id c-last-identifier-range)))
5420 t)))
5421
5422 (defmacro c-forward-id-comma-list (type update-safe-pos)
5423 ;; Used internally in `c-forward-keyword-clause' to move forward
5424 ;; over a comma separated list of types or names using
5425 ;; `c-forward-keyword-prefixed-id'.
5426 ;;
5427 ;; This macro might do hidden buffer changes.
5428 `(while (and (progn
5429 ,(when update-safe-pos
5430 `(setq safe-pos (point)))
5431 (eq (char-after) ?,))
5432 (progn
5433 (forward-char)
5434 (c-forward-syntactic-ws)
5435 (c-forward-keyword-prefixed-id ,type)))))
5436
5437 (defun c-forward-keyword-clause (match)
5438 ;; Submatch MATCH in the current match data is assumed to surround a
5439 ;; token. If it's a keyword, move over it and any immediately
5440 ;; following clauses associated with it, stopping at the start of
5441 ;; the next token. t is returned in that case, otherwise the point
5442 ;; stays and nil is returned. The kind of clauses that are
5443 ;; recognized are those specified by `c-type-list-kwds',
5444 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5445 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5446 ;; and `c-<>-arglist-kwds'.
5447 ;;
5448 ;; This function records identifier ranges on
5449 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5450 ;; `c-record-type-identifiers' is non-nil.
5451 ;;
5452 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5453 ;; apply directly after the keyword, the type list is moved over
5454 ;; only when there is no unaccounted token before it (i.e. a token
5455 ;; that isn't moved over due to some other keyword list). The
5456 ;; identifier ranges in the list are still recorded if that should
5457 ;; be done, though.
5458 ;;
5459 ;; This function might do hidden buffer changes.
5460
5461 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5462 ;; The call to `c-forward-<>-arglist' below is made after
5463 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5464 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5465 ;; should therefore be nil.
5466 (c-parse-and-markup-<>-arglists t)
5467 c-restricted-<>-arglists)
5468
5469 (when kwd-sym
5470 (goto-char (match-end match))
5471 (c-forward-syntactic-ws)
5472 (setq safe-pos (point))
5473
5474 (cond
5475 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5476 (c-forward-keyword-prefixed-id type))
5477 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5478 (c-forward-id-comma-list type t))
5479
5480 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5481 (c-forward-keyword-prefixed-id ref))
5482 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5483 (c-forward-id-comma-list ref t))
5484
5485 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5486 (eq (char-after) ?\())
5487 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5488
5489 (forward-char)
5490 (when (and (setq pos (c-up-list-forward))
5491 (eq (char-before pos) ?\)))
5492 (when (and c-record-type-identifiers
5493 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5494 ;; Use `c-forward-type' on every identifier we can find
5495 ;; inside the paren, to record the types.
5496 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5497 (goto-char (match-beginning 0))
5498 (unless (c-forward-type)
5499 (looking-at c-symbol-key) ; Always matches.
5500 (goto-char (match-end 0)))))
5501
5502 (goto-char pos)
5503 (c-forward-syntactic-ws)
5504 (setq safe-pos (point))))
5505
5506 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5507 (eq (char-after) ?<)
5508 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5509 (c-forward-syntactic-ws)
5510 (setq safe-pos (point)))
5511
5512 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5513 (not (looking-at c-symbol-start))
5514 (c-safe (c-forward-sexp) t))
5515 (c-forward-syntactic-ws)
5516 (setq safe-pos (point))))
5517
5518 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5519 (if (eq (char-after) ?:)
5520 ;; If we are at the colon already, we move over the type
5521 ;; list after it.
5522 (progn
5523 (forward-char)
5524 (c-forward-syntactic-ws)
5525 (when (c-forward-keyword-prefixed-id type)
5526 (c-forward-id-comma-list type t)))
5527 ;; Not at the colon, so stop here. But the identifier
5528 ;; ranges in the type list later on should still be
5529 ;; recorded.
5530 (and c-record-type-identifiers
5531 (progn
5532 ;; If a keyword matched both one of the types above and
5533 ;; this one, we match `c-colon-type-list-re' after the
5534 ;; clause matched above.
5535 (goto-char safe-pos)
5536 (looking-at c-colon-type-list-re))
5537 (progn
5538 (goto-char (match-end 0))
5539 (c-forward-syntactic-ws)
5540 (c-forward-keyword-prefixed-id type))
5541 ;; There's a type after the `c-colon-type-list-re' match
5542 ;; after a keyword in `c-colon-type-list-kwds'.
5543 (c-forward-id-comma-list type nil))))
5544
5545 (goto-char safe-pos)
5546 t)))
5547
5548 ;; cc-mode requires cc-fonts.
5549 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5550
5551 (defun c-forward-<>-arglist (all-types)
5552 ;; The point is assumed to be at a "<". Try to treat it as the open
5553 ;; paren of an angle bracket arglist and move forward to the
5554 ;; corresponding ">". If successful, the point is left after the
5555 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5556 ;; returned. If ALL-TYPES is t then all encountered arguments in
5557 ;; the arglist that might be types are treated as found types.
5558 ;;
5559 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5560 ;; function handles text properties on the angle brackets and argument
5561 ;; separating commas.
5562 ;;
5563 ;; `c-restricted-<>-arglists' controls how lenient the template
5564 ;; arglist recognition should be.
5565 ;;
5566 ;; This function records identifier ranges on
5567 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5568 ;; `c-record-type-identifiers' is non-nil.
5569 ;;
5570 ;; This function might do hidden buffer changes.
5571
5572 (let ((start (point))
5573 ;; If `c-record-type-identifiers' is set then activate
5574 ;; recording of any found types that constitute an argument in
5575 ;; the arglist.
5576 (c-record-found-types (if c-record-type-identifiers t)))
5577 (if (catch 'angle-bracket-arglist-escape
5578 (setq c-record-found-types
5579 (c-forward-<>-arglist-recur all-types)))
5580 (progn
5581 (when (consp c-record-found-types)
5582 (setq c-record-type-identifiers
5583 ;; `nconc' doesn't mind that the tail of
5584 ;; `c-record-found-types' is t.
5585 (nconc c-record-found-types c-record-type-identifiers)))
5586 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
5587 t)
5588
5589 (goto-char start)
5590 nil)))
5591
5592 (defun c-forward-<>-arglist-recur (all-types)
5593 ;; Recursive part of `c-forward-<>-arglist'.
5594 ;;
5595 ;; This function might do hidden buffer changes.
5596
5597 (let ((start (point)) res pos tmp
5598 ;; Cover this so that any recorded found type ranges are
5599 ;; automatically lost if it turns out to not be an angle
5600 ;; bracket arglist. It's propagated through the return value
5601 ;; on successful completion.
5602 (c-record-found-types c-record-found-types)
5603 ;; List that collects the positions after the argument
5604 ;; separating ',' in the arglist.
5605 arg-start-pos)
5606 ;; If the '<' has paren open syntax then we've marked it as an angle
5607 ;; bracket arglist before, so skip to the end.
5608 (if (and (not c-parse-and-markup-<>-arglists)
5609 (c-get-char-property (point) 'syntax-table))
5610
5611 (progn
5612 (forward-char)
5613 (if (and (c-go-up-list-forward)
5614 (eq (char-before) ?>))
5615 t
5616 ;; Got unmatched paren angle brackets. We don't clear the paren
5617 ;; syntax properties and retry, on the basis that it's very
5618 ;; unlikely that paren angle brackets become operators by code
5619 ;; manipulation. It's far more likely that it doesn't match due
5620 ;; to narrowing or some temporary change.
5621 (goto-char start)
5622 nil))
5623
5624 (forward-char) ; Forward over the opening '<'.
5625
5626 (unless (looking-at c-<-op-cont-regexp)
5627 ;; go forward one non-alphanumeric character (group) per iteration of
5628 ;; this loop.
5629 (while (and
5630 (progn
5631 (c-forward-syntactic-ws)
5632 (let ((orig-record-found-types c-record-found-types))
5633 (when (or (and c-record-type-identifiers all-types)
5634 (c-major-mode-is 'java-mode))
5635 ;; All encountered identifiers are types, so set the
5636 ;; promote flag and parse the type.
5637 (progn
5638 (c-forward-syntactic-ws)
5639 (if (looking-at "\\?")
5640 (forward-char)
5641 (when (looking-at c-identifier-start)
5642 (let ((c-promote-possible-types t)
5643 (c-record-found-types t))
5644 (c-forward-type))))
5645
5646 (c-forward-syntactic-ws)
5647
5648 (when (or (looking-at "extends")
5649 (looking-at "super"))
5650 (forward-word)
5651 (c-forward-syntactic-ws)
5652 (let ((c-promote-possible-types t)
5653 (c-record-found-types t))
5654 (c-forward-type)
5655 (c-forward-syntactic-ws))))))
5656
5657 (setq pos (point)) ; e.g. first token inside the '<'
5658
5659 ;; Note: These regexps exploit the match order in \| so
5660 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5661 (c-syntactic-re-search-forward
5662 ;; Stop on ',', '|', '&', '+' and '-' to catch
5663 ;; common binary operators that could be between
5664 ;; two comparison expressions "a<b" and "c>d".
5665 "[<;{},|+&-]\\|[>)]"
5666 nil t t))
5667
5668 (cond
5669 ((eq (char-before) ?>)
5670 ;; Either an operator starting with '>' or the end of
5671 ;; the angle bracket arglist.
5672
5673 (if (looking-at c->-op-cont-regexp)
5674 (progn
5675 (goto-char (match-end 0))
5676 t) ; Continue the loop.
5677
5678 ;; The angle bracket arglist is finished.
5679 (when c-parse-and-markup-<>-arglists
5680 (while arg-start-pos
5681 (c-put-c-type-property (1- (car arg-start-pos))
5682 'c-<>-arg-sep)
5683 (setq arg-start-pos (cdr arg-start-pos)))
5684 (c-mark-<-as-paren start)
5685 (c-mark->-as-paren (1- (point))))
5686 (setq res t)
5687 nil)) ; Exit the loop.
5688
5689 ((eq (char-before) ?<)
5690 ;; Either an operator starting with '<' or a nested arglist.
5691 (setq pos (point))
5692 (let (id-start id-end subres keyword-match)
5693 (cond
5694 ;; The '<' begins a multi-char operator.
5695 ((looking-at c-<-op-cont-regexp)
5696 (setq tmp (match-end 0))
5697 (goto-char (match-end 0)))
5698 ;; We're at a nested <.....>
5699 ((progn
5700 (setq tmp pos)
5701 (backward-char) ; to the '<'
5702 (and
5703 (save-excursion
5704 ;; There's always an identifier before an angle
5705 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5706 ;; or `c-<>-arglist-kwds'.
5707 (c-backward-syntactic-ws)
5708 (setq id-end (point))
5709 (c-simple-skip-symbol-backward)
5710 (when (or (setq keyword-match
5711 (looking-at c-opt-<>-sexp-key))
5712 (not (looking-at c-keywords-regexp)))
5713 (setq id-start (point))))
5714 (setq subres
5715 (let ((c-promote-possible-types t)
5716 (c-record-found-types t))
5717 (c-forward-<>-arglist-recur
5718 (and keyword-match
5719 (c-keyword-member
5720 (c-keyword-sym (match-string 1))
5721 'c-<>-type-kwds)))))))
5722
5723 ;; It was an angle bracket arglist.
5724 (setq c-record-found-types subres)
5725
5726 ;; Record the identifier before the template as a type
5727 ;; or reference depending on whether the arglist is last
5728 ;; in a qualified identifier.
5729 (when (and c-record-type-identifiers
5730 (not keyword-match))
5731 (if (and c-opt-identifier-concat-key
5732 (progn
5733 (c-forward-syntactic-ws)
5734 (looking-at c-opt-identifier-concat-key)))
5735 (c-record-ref-id (cons id-start id-end))
5736 (c-record-type-id (cons id-start id-end)))))
5737
5738 ;; At a "less than" operator.
5739 (t
5740 (forward-char)
5741 )))
5742 t) ; carry on looping.
5743
5744 ((and (not c-restricted-<>-arglists)
5745 (or (and (eq (char-before) ?&)
5746 (not (eq (char-after) ?&)))
5747 (eq (char-before) ?,)))
5748 ;; Just another argument. Record the position. The
5749 ;; type check stuff that made us stop at it is at
5750 ;; the top of the loop.
5751 (setq arg-start-pos (cons (point) arg-start-pos)))
5752
5753 (t
5754 ;; Got a character that can't be in an angle bracket
5755 ;; arglist argument. Abort using `throw', since
5756 ;; it's useless to try to find a surrounding arglist
5757 ;; if we're nested.
5758 (throw 'angle-bracket-arglist-escape nil))))))
5759 (if res
5760 (or c-record-found-types t)))))
5761
5762 (defun c-backward-<>-arglist (all-types &optional limit)
5763 ;; The point is assumed to be directly after a ">". Try to treat it
5764 ;; as the close paren of an angle bracket arglist and move back to
5765 ;; the corresponding "<". If successful, the point is left at
5766 ;; the "<" and t is returned, otherwise the point isn't moved and
5767 ;; nil is returned. ALL-TYPES is passed on to
5768 ;; `c-forward-<>-arglist'.
5769 ;;
5770 ;; If the optional LIMIT is given, it bounds the backward search.
5771 ;; It's then assumed to be at a syntactically relevant position.
5772 ;;
5773 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5774 ;; function for more details.
5775
5776 (let ((start (point)))
5777 (backward-char)
5778 (if (and (not c-parse-and-markup-<>-arglists)
5779 (c-get-char-property (point) 'syntax-table))
5780
5781 (if (and (c-go-up-list-backward)
5782 (eq (char-after) ?<))
5783 t
5784 ;; See corresponding note in `c-forward-<>-arglist'.
5785 (goto-char start)
5786 nil)
5787
5788 (while (progn
5789 (c-syntactic-skip-backward "^<;{}" limit t)
5790
5791 (and
5792 (if (eq (char-before) ?<)
5793 t
5794 ;; Stopped at bob or a char that isn't allowed in an
5795 ;; arglist, so we've failed.
5796 (goto-char start)
5797 nil)
5798
5799 (if (> (point)
5800 (progn (c-beginning-of-current-token)
5801 (point)))
5802 ;; If we moved then the "<" was part of some
5803 ;; multicharacter token.
5804 t
5805
5806 (backward-char)
5807 (let ((beg-pos (point)))
5808 (if (c-forward-<>-arglist all-types)
5809 (cond ((= (point) start)
5810 ;; Matched the arglist. Break the while.
5811 (goto-char beg-pos)
5812 nil)
5813 ((> (point) start)
5814 ;; We started from a non-paren ">" inside an
5815 ;; arglist.
5816 (goto-char start)
5817 nil)
5818 (t
5819 ;; Matched a shorter arglist. Can be a nested
5820 ;; one so continue looking.
5821 (goto-char beg-pos)
5822 t))
5823 t))))))
5824
5825 (/= (point) start))))
5826
5827 (defun c-forward-name ()
5828 ;; Move forward over a complete name if at the beginning of one,
5829 ;; stopping at the next following token. A keyword, as such,
5830 ;; doesn't count as a name. If the point is not at something that
5831 ;; is recognized as a name then it stays put.
5832 ;;
5833 ;; A name could be something as simple as "foo" in C or something as
5834 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5835 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5836 ;; int>::*volatile const" in C++ (this function is actually little
5837 ;; more than a `looking-at' call in all modes except those that,
5838 ;; like C++, have `c-recognize-<>-arglists' set).
5839 ;;
5840 ;; Return
5841 ;; o - nil if no name is found;
5842 ;; o - 'template if it's an identifier ending with an angle bracket
5843 ;; arglist;
5844 ;; o - 'operator of it's an operator identifier;
5845 ;; o - t if it's some other kind of name.
5846 ;;
5847 ;; This function records identifier ranges on
5848 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5849 ;; `c-record-type-identifiers' is non-nil.
5850 ;;
5851 ;; This function might do hidden buffer changes.
5852
5853 (let ((pos (point)) (start (point)) res id-start id-end
5854 ;; Turn off `c-promote-possible-types' here since we might
5855 ;; call `c-forward-<>-arglist' and we don't want it to promote
5856 ;; every suspect thing in the arglist to a type. We're
5857 ;; typically called from `c-forward-type' in this case, and
5858 ;; the caller only wants the top level type that it finds to
5859 ;; be promoted.
5860 c-promote-possible-types)
5861 (while
5862 (and
5863 (looking-at c-identifier-key)
5864
5865 (progn
5866 ;; Check for keyword. We go to the last symbol in
5867 ;; `c-identifier-key' first.
5868 (goto-char (setq id-end (match-end 0)))
5869 (c-simple-skip-symbol-backward)
5870 (setq id-start (point))
5871
5872 (if (looking-at c-keywords-regexp)
5873 (when (and (c-major-mode-is 'c++-mode)
5874 (looking-at
5875 (cc-eval-when-compile
5876 (concat "\\(operator\\|\\(template\\)\\)"
5877 "\\(" (c-lang-const c-nonsymbol-key c++)
5878 "\\|$\\)")))
5879 (if (match-beginning 2)
5880 ;; "template" is only valid inside an
5881 ;; identifier if preceded by "::".
5882 (save-excursion
5883 (c-backward-syntactic-ws)
5884 (and (c-safe (backward-char 2) t)
5885 (looking-at "::")))
5886 t))
5887
5888 ;; Handle a C++ operator or template identifier.
5889 (goto-char id-end)
5890 (c-forward-syntactic-ws)
5891 (cond ((eq (char-before id-end) ?e)
5892 ;; Got "... ::template".
5893 (let ((subres (c-forward-name)))
5894 (when subres
5895 (setq pos (point)
5896 res subres))))
5897
5898 ((looking-at c-identifier-start)
5899 ;; Got a cast operator.
5900 (when (c-forward-type)
5901 (setq pos (point)
5902 res 'operator)
5903 ;; Now we should match a sequence of either
5904 ;; '*', '&' or a name followed by ":: *",
5905 ;; where each can be followed by a sequence
5906 ;; of `c-opt-type-modifier-key'.
5907 (while (cond ((looking-at "[*&]")
5908 (goto-char (match-end 0))
5909 t)
5910 ((looking-at c-identifier-start)
5911 (and (c-forward-name)
5912 (looking-at "::")
5913 (progn
5914 (goto-char (match-end 0))
5915 (c-forward-syntactic-ws)
5916 (eq (char-after) ?*))
5917 (progn
5918 (forward-char)
5919 t))))
5920 (while (progn
5921 (c-forward-syntactic-ws)
5922 (setq pos (point))
5923 (looking-at c-opt-type-modifier-key))
5924 (goto-char (match-end 1))))))
5925
5926 ((looking-at c-overloadable-operators-regexp)
5927 ;; Got some other operator.
5928 (setq c-last-identifier-range
5929 (cons (point) (match-end 0)))
5930 (goto-char (match-end 0))
5931 (c-forward-syntactic-ws)
5932 (setq pos (point)
5933 res 'operator)))
5934
5935 nil)
5936
5937 ;; `id-start' is equal to `id-end' if we've jumped over
5938 ;; an identifier that doesn't end with a symbol token.
5939 ;; That can occur e.g. for Java import directives on the
5940 ;; form "foo.bar.*".
5941 (when (and id-start (/= id-start id-end))
5942 (setq c-last-identifier-range
5943 (cons id-start id-end)))
5944 (goto-char id-end)
5945 (c-forward-syntactic-ws)
5946 (setq pos (point)
5947 res t)))
5948
5949 (progn
5950 (goto-char pos)
5951 (when (or c-opt-identifier-concat-key
5952 c-recognize-<>-arglists)
5953
5954 (cond
5955 ((and c-opt-identifier-concat-key
5956 (looking-at c-opt-identifier-concat-key))
5957 ;; Got a concatenated identifier. This handles the
5958 ;; cases with tricky syntactic whitespace that aren't
5959 ;; covered in `c-identifier-key'.
5960 (goto-char (match-end 0))
5961 (c-forward-syntactic-ws)
5962 t)
5963
5964 ((and c-recognize-<>-arglists
5965 (eq (char-after) ?<))
5966 ;; Maybe an angle bracket arglist.
5967 (when (let ((c-record-type-identifiers t)
5968 (c-record-found-types t))
5969 (c-forward-<>-arglist nil))
5970
5971 (c-add-type start (1+ pos))
5972 (c-forward-syntactic-ws)
5973 (setq pos (point)
5974 c-last-identifier-range nil)
5975
5976 (if (and c-opt-identifier-concat-key
5977 (looking-at c-opt-identifier-concat-key))
5978
5979 ;; Continue if there's an identifier concatenation
5980 ;; operator after the template argument.
5981 (progn
5982 (when (and c-record-type-identifiers id-start)
5983 (c-record-ref-id (cons id-start id-end)))
5984 (forward-char 2)
5985 (c-forward-syntactic-ws)
5986 t)
5987
5988 (when (and c-record-type-identifiers id-start)
5989 (c-record-type-id (cons id-start id-end)))
5990 (setq res 'template)
5991 nil)))
5992 )))))
5993
5994 (goto-char pos)
5995 res))
5996
5997 (defun c-forward-type (&optional brace-block-too)
5998 ;; Move forward over a type spec if at the beginning of one,
5999 ;; stopping at the next following token. The keyword "typedef"
6000 ;; isn't part of a type spec here.
6001 ;;
6002 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
6003 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
6004 ;; The current (2009-03-10) intention is to convert all uses of
6005 ;; `c-forward-type' to call with this parameter set, then to
6006 ;; eliminate it.
6007 ;;
6008 ;; Return
6009 ;; o - t if it's a known type that can't be a name or other
6010 ;; expression;
6011 ;; o - 'known if it's an otherwise known type (according to
6012 ;; `*-font-lock-extra-types');
6013 ;; o - 'prefix if it's a known prefix of a type;
6014 ;; o - 'found if it's a type that matches one in `c-found-types';
6015 ;; o - 'maybe if it's an identifier that might be a type; or
6016 ;; o - nil if it can't be a type (the point isn't moved then).
6017 ;;
6018 ;; The point is assumed to be at the beginning of a token.
6019 ;;
6020 ;; Note that this function doesn't skip past the brace definition
6021 ;; that might be considered part of the type, e.g.
6022 ;; "enum {a, b, c} foo".
6023 ;;
6024 ;; This function records identifier ranges on
6025 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6026 ;; `c-record-type-identifiers' is non-nil.
6027 ;;
6028 ;; This function might do hidden buffer changes.
6029 (when (and c-recognize-<>-arglists
6030 (looking-at "<"))
6031 (c-forward-<>-arglist t)
6032 (c-forward-syntactic-ws))
6033
6034 (let ((start (point)) pos res name-res id-start id-end id-range)
6035
6036 ;; Skip leading type modifiers. If any are found we know it's a
6037 ;; prefix of a type.
6038 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
6039 (while (looking-at c-opt-type-modifier-key)
6040 (goto-char (match-end 1))
6041 (c-forward-syntactic-ws)
6042 (setq res 'prefix)))
6043
6044 (cond
6045 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
6046 ; "typedef".
6047 (goto-char (match-end 1))
6048 (c-forward-syntactic-ws)
6049 (setq pos (point))
6050
6051 (setq name-res (c-forward-name))
6052 (setq res (not (null name-res)))
6053 (when (eq name-res t)
6054 ;; In many languages the name can be used without the
6055 ;; prefix, so we add it to `c-found-types'.
6056 (c-add-type pos (point))
6057 (when (and c-record-type-identifiers
6058 c-last-identifier-range)
6059 (c-record-type-id c-last-identifier-range)))
6060 (when (and brace-block-too
6061 (memq res '(t nil))
6062 (eq (char-after) ?\{)
6063 (save-excursion
6064 (c-safe
6065 (progn (c-forward-sexp)
6066 (c-forward-syntactic-ws)
6067 (setq pos (point))))))
6068 (goto-char pos)
6069 (setq res t))
6070 (unless res (goto-char start))) ; invalid syntax
6071
6072 ((progn
6073 (setq pos nil)
6074 (if (looking-at c-identifier-start)
6075 (save-excursion
6076 (setq id-start (point)
6077 name-res (c-forward-name))
6078 (when name-res
6079 (setq id-end (point)
6080 id-range c-last-identifier-range))))
6081 (and (cond ((looking-at c-primitive-type-key)
6082 (setq res t))
6083 ((c-with-syntax-table c-identifier-syntax-table
6084 (looking-at c-known-type-key))
6085 (setq res 'known)))
6086 (or (not id-end)
6087 (>= (save-excursion
6088 (save-match-data
6089 (goto-char (match-end 1))
6090 (c-forward-syntactic-ws)
6091 (setq pos (point))))
6092 id-end)
6093 (setq res nil))))
6094 ;; Looking at a primitive or known type identifier. We've
6095 ;; checked for a name first so that we don't go here if the
6096 ;; known type match only is a prefix of another name.
6097
6098 (setq id-end (match-end 1))
6099
6100 (when (and c-record-type-identifiers
6101 (or c-promote-possible-types (eq res t)))
6102 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6103
6104 (if (and c-opt-type-component-key
6105 (save-match-data
6106 (looking-at c-opt-type-component-key)))
6107 ;; There might be more keywords for the type.
6108 (let (safe-pos)
6109 (c-forward-keyword-clause 1)
6110 (while (progn
6111 (setq safe-pos (point))
6112 (looking-at c-opt-type-component-key))
6113 (when (and c-record-type-identifiers
6114 (looking-at c-primitive-type-key))
6115 (c-record-type-id (cons (match-beginning 1)
6116 (match-end 1))))
6117 (c-forward-keyword-clause 1))
6118 (if (looking-at c-primitive-type-key)
6119 (progn
6120 (when c-record-type-identifiers
6121 (c-record-type-id (cons (match-beginning 1)
6122 (match-end 1))))
6123 (c-forward-keyword-clause 1)
6124 (setq res t))
6125 (goto-char safe-pos)
6126 (setq res 'prefix)))
6127 (unless (save-match-data (c-forward-keyword-clause 1))
6128 (if pos
6129 (goto-char pos)
6130 (goto-char (match-end 1))
6131 (c-forward-syntactic-ws)))))
6132
6133 (name-res
6134 (cond ((eq name-res t)
6135 ;; A normal identifier.
6136 (goto-char id-end)
6137 (if (or res c-promote-possible-types)
6138 (progn
6139 (c-add-type id-start id-end)
6140 (when (and c-record-type-identifiers id-range)
6141 (c-record-type-id id-range))
6142 (unless res
6143 (setq res 'found)))
6144 (setq res (if (c-check-type id-start id-end)
6145 ;; It's an identifier that has been used as
6146 ;; a type somewhere else.
6147 'found
6148 ;; It's an identifier that might be a type.
6149 'maybe))))
6150 ((eq name-res 'template)
6151 ;; A template is a type.
6152 (goto-char id-end)
6153 (setq res t))
6154 (t
6155 ;; Otherwise it's an operator identifier, which is not a type.
6156 (goto-char start)
6157 (setq res nil)))))
6158
6159 (when res
6160 ;; Skip trailing type modifiers. If any are found we know it's
6161 ;; a type.
6162 (when c-opt-type-modifier-key
6163 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
6164 (goto-char (match-end 1))
6165 (c-forward-syntactic-ws)
6166 (setq res t)))
6167 ;; Step over any type suffix operator. Do not let the existence
6168 ;; of these alter the classification of the found type, since
6169 ;; these operators typically are allowed in normal expressions
6170 ;; too.
6171 (when c-opt-type-suffix-key
6172 (while (looking-at c-opt-type-suffix-key)
6173 (goto-char (match-end 1))
6174 (c-forward-syntactic-ws)))
6175
6176 (when c-opt-type-concat-key ; Only/mainly for pike.
6177 ;; Look for a trailing operator that concatenates the type
6178 ;; with a following one, and if so step past that one through
6179 ;; a recursive call. Note that we don't record concatenated
6180 ;; types in `c-found-types' - it's the component types that
6181 ;; are recorded when appropriate.
6182 (setq pos (point))
6183 (let* ((c-promote-possible-types (or (memq res '(t known))
6184 c-promote-possible-types))
6185 ;; If we can't promote then set `c-record-found-types' so that
6186 ;; we can merge in the types from the second part afterwards if
6187 ;; it turns out to be a known type there.
6188 (c-record-found-types (and c-record-type-identifiers
6189 (not c-promote-possible-types)))
6190 subres)
6191 (if (and (looking-at c-opt-type-concat-key)
6192
6193 (progn
6194 (goto-char (match-end 1))
6195 (c-forward-syntactic-ws)
6196 (setq subres (c-forward-type))))
6197
6198 (progn
6199 ;; If either operand certainly is a type then both are, but we
6200 ;; don't let the existence of the operator itself promote two
6201 ;; uncertain types to a certain one.
6202 (cond ((eq res t))
6203 ((eq subres t)
6204 (unless (eq name-res 'template)
6205 (c-add-type id-start id-end))
6206 (when (and c-record-type-identifiers id-range)
6207 (c-record-type-id id-range))
6208 (setq res t))
6209 ((eq res 'known))
6210 ((eq subres 'known)
6211 (setq res 'known))
6212 ((eq res 'found))
6213 ((eq subres 'found)
6214 (setq res 'found))
6215 (t
6216 (setq res 'maybe)))
6217
6218 (when (and (eq res t)
6219 (consp c-record-found-types))
6220 ;; Merge in the ranges of any types found by the second
6221 ;; `c-forward-type'.
6222 (setq c-record-type-identifiers
6223 ;; `nconc' doesn't mind that the tail of
6224 ;; `c-record-found-types' is t.
6225 (nconc c-record-found-types
6226 c-record-type-identifiers))))
6227
6228 (goto-char pos))))
6229
6230 (when (and c-record-found-types (memq res '(known found)) id-range)
6231 (setq c-record-found-types
6232 (cons id-range c-record-found-types))))
6233
6234 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6235
6236 res))
6237
6238 (defun c-forward-annotation ()
6239 ;; Used for Java code only at the moment. Assumes point is on the
6240 ;; @, moves forward an annotation. returns nil if there is no
6241 ;; annotation at point.
6242 (and (looking-at "@")
6243 (progn (forward-char) t)
6244 (c-forward-type)
6245 (progn (c-forward-syntactic-ws) t)
6246 (if (looking-at "(")
6247 (c-go-list-forward)
6248 t)))
6249
6250 \f
6251 ;; Handling of large scale constructs like statements and declarations.
6252
6253 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6254 ;; defsubst or perhaps even a defun, but it contains lots of free
6255 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6256 (defmacro c-fdoc-shift-type-backward (&optional short)
6257 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6258 ;; of types when parsing a declaration, which means that it
6259 ;; sometimes consumes the identifier in the declaration as a type.
6260 ;; This is used to "backtrack" and make the last type be treated as
6261 ;; an identifier instead.
6262 `(progn
6263 ,(unless short
6264 ;; These identifiers are bound only in the inner let.
6265 '(setq identifier-type at-type
6266 identifier-start type-start
6267 got-parens nil
6268 got-identifier t
6269 got-suffix t
6270 got-suffix-after-parens id-start
6271 paren-depth 0))
6272
6273 (if (setq at-type (if (eq backup-at-type 'prefix)
6274 t
6275 backup-at-type))
6276 (setq type-start backup-type-start
6277 id-start backup-id-start)
6278 (setq type-start start-pos
6279 id-start start-pos))
6280
6281 ;; When these flags already are set we've found specifiers that
6282 ;; unconditionally signal these attributes - backtracking doesn't
6283 ;; change that. So keep them set in that case.
6284 (or at-type-decl
6285 (setq at-type-decl backup-at-type-decl))
6286 (or maybe-typeless
6287 (setq maybe-typeless backup-maybe-typeless))
6288
6289 ,(unless short
6290 ;; This identifier is bound only in the inner let.
6291 '(setq start id-start))))
6292
6293 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6294 ;; Move forward over a declaration or a cast if at the start of one.
6295 ;; The point is assumed to be at the start of some token. Nil is
6296 ;; returned if no declaration or cast is recognized, and the point
6297 ;; is clobbered in that case.
6298 ;;
6299 ;; If a declaration is parsed:
6300 ;;
6301 ;; The point is left at the first token after the first complete
6302 ;; declarator, if there is one. The return value is a cons where
6303 ;; the car is the position of the first token in the declarator. (See
6304 ;; below for the cdr.)
6305 ;; Some examples:
6306 ;;
6307 ;; void foo (int a, char *b) stuff ...
6308 ;; car ^ ^ point
6309 ;; float (*a)[], b;
6310 ;; car ^ ^ point
6311 ;; unsigned int a = c_style_initializer, b;
6312 ;; car ^ ^ point
6313 ;; unsigned int a (cplusplus_style_initializer), b;
6314 ;; car ^ ^ point (might change)
6315 ;; class Foo : public Bar {}
6316 ;; car ^ ^ point
6317 ;; class PikeClass (int a, string b) stuff ...
6318 ;; car ^ ^ point
6319 ;; enum bool;
6320 ;; car ^ ^ point
6321 ;; enum bool flag;
6322 ;; car ^ ^ point
6323 ;; void cplusplus_function (int x) throw (Bad);
6324 ;; car ^ ^ point
6325 ;; Foo::Foo (int b) : Base (b) {}
6326 ;; car ^ ^ point
6327 ;;
6328 ;; The cdr of the return value is non-nil when a
6329 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6330 ;; Specifically it is a dotted pair (A . B) where B is t when a
6331 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6332 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6333 ;; specifier is present. I.e., (some of) the declared
6334 ;; identifier(s) are types.
6335 ;;
6336 ;; If a cast is parsed:
6337 ;;
6338 ;; The point is left at the first token after the closing paren of
6339 ;; the cast. The return value is `cast'. Note that the start
6340 ;; position must be at the first token inside the cast parenthesis
6341 ;; to recognize it.
6342 ;;
6343 ;; PRECEDING-TOKEN-END is the first position after the preceding
6344 ;; token, i.e. on the other side of the syntactic ws from the point.
6345 ;; Use a value less than or equal to (point-min) if the point is at
6346 ;; the first token in (the visible part of) the buffer.
6347 ;;
6348 ;; CONTEXT is a symbol that describes the context at the point:
6349 ;; 'decl In a comma-separated declaration context (typically
6350 ;; inside a function declaration arglist).
6351 ;; '<> In an angle bracket arglist.
6352 ;; 'arglist Some other type of arglist.
6353 ;; nil Some other context or unknown context. Includes
6354 ;; within the parens of an if, for, ... construct.
6355 ;;
6356 ;; LAST-CAST-END is the first token after the closing paren of a
6357 ;; preceding cast, or nil if none is known. If
6358 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6359 ;; the position after the closest preceding call where a cast was
6360 ;; matched. In that case it's used to discover chains of casts like
6361 ;; "(a) (b) c".
6362 ;;
6363 ;; This function records identifier ranges on
6364 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6365 ;; `c-record-type-identifiers' is non-nil.
6366 ;;
6367 ;; This function might do hidden buffer changes.
6368
6369 (let (;; `start-pos' is used below to point to the start of the
6370 ;; first type, i.e. after any leading specifiers. It might
6371 ;; also point at the beginning of the preceding syntactic
6372 ;; whitespace.
6373 (start-pos (point))
6374 ;; Set to the result of `c-forward-type'.
6375 at-type
6376 ;; The position of the first token in what we currently
6377 ;; believe is the type in the declaration or cast, after any
6378 ;; specifiers and their associated clauses.
6379 type-start
6380 ;; The position of the first token in what we currently
6381 ;; believe is the declarator for the first identifier. Set
6382 ;; when the type is found, and moved forward over any
6383 ;; `c-decl-hangon-kwds' and their associated clauses that
6384 ;; occurs after the type.
6385 id-start
6386 ;; These store `at-type', `type-start' and `id-start' of the
6387 ;; identifier before the one in those variables. The previous
6388 ;; identifier might turn out to be the real type in a
6389 ;; declaration if the last one has to be the declarator in it.
6390 ;; If `backup-at-type' is nil then the other variables have
6391 ;; undefined values.
6392 backup-at-type backup-type-start backup-id-start
6393 ;; Set if we've found a specifier (apart from "typedef") that makes
6394 ;; the defined identifier(s) types.
6395 at-type-decl
6396 ;; Set if we've a "typedef" keyword.
6397 at-typedef
6398 ;; Set if we've found a specifier that can start a declaration
6399 ;; where there's no type.
6400 maybe-typeless
6401 ;; If a specifier is found that also can be a type prefix,
6402 ;; these flags are set instead of those above. If we need to
6403 ;; back up an identifier, they are copied to the real flag
6404 ;; variables. Thus they only take effect if we fail to
6405 ;; interpret it as a type.
6406 backup-at-type-decl backup-maybe-typeless
6407 ;; Whether we've found a declaration or a cast. We might know
6408 ;; this before we've found the type in it. It's 'ids if we've
6409 ;; found two consecutive identifiers (usually a sure sign, but
6410 ;; we should allow that in labels too), and t if we've found a
6411 ;; specifier keyword (a 100% sure sign).
6412 at-decl-or-cast
6413 ;; Set when we need to back up to parse this as a declaration
6414 ;; but not as a cast.
6415 backup-if-not-cast
6416 ;; For casts, the return position.
6417 cast-end
6418 ;; Save `c-record-type-identifiers' and
6419 ;; `c-record-ref-identifiers' since ranges are recorded
6420 ;; speculatively and should be thrown away if it turns out
6421 ;; that it isn't a declaration or cast.
6422 (save-rec-type-ids c-record-type-identifiers)
6423 (save-rec-ref-ids c-record-ref-identifiers))
6424
6425 (while (c-forward-annotation)
6426 (c-forward-syntactic-ws))
6427
6428 ;; Check for a type. Unknown symbols are treated as possible
6429 ;; types, but they could also be specifiers disguised through
6430 ;; macros like __INLINE__, so we recognize both types and known
6431 ;; specifiers after them too.
6432 (while
6433 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6434
6435 ;; Look for a specifier keyword clause.
6436 (when (or (looking-at c-prefix-spec-kwds-re)
6437 (and (c-major-mode-is 'java-mode)
6438 (looking-at "@[A-Za-z0-9]+")))
6439 (if (looking-at c-typedef-key)
6440 (setq at-typedef t))
6441 (setq kwd-sym (c-keyword-sym (match-string 1)))
6442 (save-excursion
6443 (c-forward-keyword-clause 1)
6444 (setq kwd-clause-end (point))))
6445
6446 (when (setq found-type (c-forward-type t)) ; brace-block-too
6447 ;; Found a known or possible type or a prefix of a known type.
6448
6449 (when at-type
6450 ;; Got two identifiers with nothing but whitespace
6451 ;; between them. That can only happen in declarations.
6452 (setq at-decl-or-cast 'ids)
6453
6454 (when (eq at-type 'found)
6455 ;; If the previous identifier is a found type we
6456 ;; record it as a real one; it might be some sort of
6457 ;; alias for a prefix like "unsigned".
6458 (save-excursion
6459 (goto-char type-start)
6460 (let ((c-promote-possible-types t))
6461 (c-forward-type)))))
6462
6463 (setq backup-at-type at-type
6464 backup-type-start type-start
6465 backup-id-start id-start
6466 at-type found-type
6467 type-start start
6468 id-start (point)
6469 ;; The previous ambiguous specifier/type turned out
6470 ;; to be a type since we've parsed another one after
6471 ;; it, so clear these backup flags.
6472 backup-at-type-decl nil
6473 backup-maybe-typeless nil))
6474
6475 (if kwd-sym
6476 (progn
6477 ;; Handle known specifier keywords and
6478 ;; `c-decl-hangon-kwds' which can occur after known
6479 ;; types.
6480
6481 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6482 ;; It's a hang-on keyword that can occur anywhere.
6483 (progn
6484 (setq at-decl-or-cast t)
6485 (if at-type
6486 ;; Move the identifier start position if
6487 ;; we've passed a type.
6488 (setq id-start kwd-clause-end)
6489 ;; Otherwise treat this as a specifier and
6490 ;; move the fallback position.
6491 (setq start-pos kwd-clause-end))
6492 (goto-char kwd-clause-end))
6493
6494 ;; It's an ordinary specifier so we know that
6495 ;; anything before this can't be the type.
6496 (setq backup-at-type nil
6497 start-pos kwd-clause-end)
6498
6499 (if found-type
6500 ;; It's ambiguous whether this keyword is a
6501 ;; specifier or a type prefix, so set the backup
6502 ;; flags. (It's assumed that `c-forward-type'
6503 ;; moved further than `c-forward-keyword-clause'.)
6504 (progn
6505 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6506 (setq backup-at-type-decl t))
6507 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6508 (setq backup-maybe-typeless t)))
6509
6510 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6511 ;; This test only happens after we've scanned a type.
6512 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6513 (setq at-type-decl t))
6514 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6515 (setq maybe-typeless t))
6516
6517 ;; Haven't matched a type so it's an unambiguous
6518 ;; specifier keyword and we know we're in a
6519 ;; declaration.
6520 (setq at-decl-or-cast t)
6521
6522 (goto-char kwd-clause-end))))
6523
6524 ;; If the type isn't known we continue so that we'll jump
6525 ;; over all specifiers and type identifiers. The reason
6526 ;; to do this for a known type prefix is to make things
6527 ;; like "unsigned INT16" work.
6528 (and found-type (not (eq found-type t))))))
6529
6530 (cond
6531 ((eq at-type t)
6532 ;; If a known type was found, we still need to skip over any
6533 ;; hangon keyword clauses after it. Otherwise it has already
6534 ;; been done in the loop above.
6535 (while (looking-at c-decl-hangon-key)
6536 (c-forward-keyword-clause 1))
6537 (setq id-start (point)))
6538
6539 ((eq at-type 'prefix)
6540 ;; A prefix type is itself a primitive type when it's not
6541 ;; followed by another type.
6542 (setq at-type t))
6543
6544 ((not at-type)
6545 ;; Got no type but set things up to continue anyway to handle
6546 ;; the various cases when a declaration doesn't start with a
6547 ;; type.
6548 (setq id-start start-pos))
6549
6550 ((and (eq at-type 'maybe)
6551 (c-major-mode-is 'c++-mode))
6552 ;; If it's C++ then check if the last "type" ends on the form
6553 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6554 ;; (con|de)structor.
6555 (save-excursion
6556 (let (name end-2 end-1)
6557 (goto-char id-start)
6558 (c-backward-syntactic-ws)
6559 (setq end-2 (point))
6560 (when (and
6561 (c-simple-skip-symbol-backward)
6562 (progn
6563 (setq name
6564 (buffer-substring-no-properties (point) end-2))
6565 ;; Cheating in the handling of syntactic ws below.
6566 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6567 (progn
6568 (setq end-1 (point))
6569 (c-simple-skip-symbol-backward))
6570 (>= (point) type-start)
6571 (equal (buffer-substring-no-properties (point) end-1)
6572 name))
6573 ;; It is a (con|de)structor name. In that case the
6574 ;; declaration is typeless so zap out any preceding
6575 ;; identifier(s) that we might have taken as types.
6576 (goto-char type-start)
6577 (setq at-type nil
6578 backup-at-type nil
6579 id-start type-start))))))
6580
6581 ;; Check for and step over a type decl expression after the thing
6582 ;; that is or might be a type. This can't be skipped since we
6583 ;; need the correct end position of the declarator for
6584 ;; `max-type-decl-end-*'.
6585 (let ((start (point)) (paren-depth 0) pos
6586 ;; True if there's a non-open-paren match of
6587 ;; `c-type-decl-prefix-key'.
6588 got-prefix
6589 ;; True if the declarator is surrounded by a parenthesis pair.
6590 got-parens
6591 ;; True if there is an identifier in the declarator.
6592 got-identifier
6593 ;; True if there's a non-close-paren match of
6594 ;; `c-type-decl-suffix-key'.
6595 got-suffix
6596 ;; True if there's a prefix match outside the outermost
6597 ;; paren pair that surrounds the declarator.
6598 got-prefix-before-parens
6599 ;; True if there's a suffix match outside the outermost
6600 ;; paren pair that surrounds the declarator. The value is
6601 ;; the position of the first suffix match.
6602 got-suffix-after-parens
6603 ;; True if we've parsed the type decl to a token that is
6604 ;; known to end declarations in this context.
6605 at-decl-end
6606 ;; The earlier values of `at-type' and `type-start' if we've
6607 ;; shifted the type backwards.
6608 identifier-type identifier-start
6609 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6610 ;; turn it off during the name skipping below to avoid
6611 ;; getting `c-type' properties that might be bogus. That
6612 ;; can happen since we don't know if
6613 ;; `c-restricted-<>-arglists' will be correct inside the
6614 ;; arglist paren that gets entered.
6615 c-parse-and-markup-<>-arglists)
6616
6617 (goto-char id-start)
6618
6619 ;; Skip over type decl prefix operators. (Note similar code in
6620 ;; `c-font-lock-declarators'.)
6621 (while (and (looking-at c-type-decl-prefix-key)
6622 (if (and (c-major-mode-is 'c++-mode)
6623 (match-beginning 3))
6624 ;; If the second submatch matches in C++ then
6625 ;; we're looking at an identifier that's a
6626 ;; prefix only if it specifies a member pointer.
6627 (when (setq got-identifier (c-forward-name))
6628 (if (looking-at "\\(::\\)")
6629 ;; We only check for a trailing "::" and
6630 ;; let the "*" that should follow be
6631 ;; matched in the next round.
6632 (progn (setq got-identifier nil) t)
6633 ;; It turned out to be the real identifier,
6634 ;; so stop.
6635 nil))
6636 t))
6637
6638 (if (eq (char-after) ?\()
6639 (progn
6640 (setq paren-depth (1+ paren-depth))
6641 (forward-char))
6642 (unless got-prefix-before-parens
6643 (setq got-prefix-before-parens (= paren-depth 0)))
6644 (setq got-prefix t)
6645 (goto-char (match-end 1)))
6646 (c-forward-syntactic-ws))
6647
6648 (setq got-parens (> paren-depth 0))
6649
6650 ;; Skip over an identifier.
6651 (or got-identifier
6652 (and (looking-at c-identifier-start)
6653 (setq got-identifier (c-forward-name))))
6654
6655 ;; Skip over type decl suffix operators.
6656 (while (if (looking-at c-type-decl-suffix-key)
6657
6658 (if (eq (char-after) ?\))
6659 (when (> paren-depth 0)
6660 (setq paren-depth (1- paren-depth))
6661 (forward-char)
6662 t)
6663 (when (if (save-match-data (looking-at "\\s\("))
6664 (c-safe (c-forward-sexp 1) t)
6665 (goto-char (match-end 1))
6666 t)
6667 (when (and (not got-suffix-after-parens)
6668 (= paren-depth 0))
6669 (setq got-suffix-after-parens (match-beginning 0)))
6670 (setq got-suffix t)))
6671
6672 ;; No suffix matched. We might have matched the
6673 ;; identifier as a type and the open paren of a
6674 ;; function arglist as a type decl prefix. In that
6675 ;; case we should "backtrack": Reinterpret the last
6676 ;; type as the identifier, move out of the arglist and
6677 ;; continue searching for suffix operators.
6678 ;;
6679 ;; Do this even if there's no preceding type, to cope
6680 ;; with old style function declarations in K&R C,
6681 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6682 ;; style declarations. That isn't applicable in an
6683 ;; arglist context, though.
6684 (when (and (= paren-depth 1)
6685 (not got-prefix-before-parens)
6686 (not (eq at-type t))
6687 (or backup-at-type
6688 maybe-typeless
6689 backup-maybe-typeless
6690 (when c-recognize-typeless-decls
6691 (not context)))
6692 (setq pos (c-up-list-forward (point)))
6693 (eq (char-before pos) ?\)))
6694 (c-fdoc-shift-type-backward)
6695 (goto-char pos)
6696 t))
6697
6698 (c-forward-syntactic-ws))
6699
6700 (when (and (or maybe-typeless backup-maybe-typeless)
6701 (not got-identifier)
6702 (not got-prefix)
6703 at-type)
6704 ;; Have found no identifier but `c-typeless-decl-kwds' has
6705 ;; matched so we know we're inside a declaration. The
6706 ;; preceding type must be the identifier instead.
6707 (c-fdoc-shift-type-backward))
6708
6709 (setq
6710 at-decl-or-cast
6711 (catch 'at-decl-or-cast
6712
6713 ;; CASE 1
6714 (when (> paren-depth 0)
6715 ;; Encountered something inside parens that isn't matched by
6716 ;; the `c-type-decl-*' regexps, so it's not a type decl
6717 ;; expression. Try to skip out to the same paren depth to
6718 ;; not confuse the cast check below.
6719 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6720 ;; If we've found a specifier keyword then it's a
6721 ;; declaration regardless.
6722 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6723
6724 (setq at-decl-end
6725 (looking-at (cond ((eq context '<>) "[,>]")
6726 (context "[,\)]")
6727 (t "[,;]"))))
6728
6729 ;; Now we've collected info about various characteristics of
6730 ;; the construct we're looking at. Below follows a decision
6731 ;; tree based on that. It's ordered to check more certain
6732 ;; signs before less certain ones.
6733
6734 (if got-identifier
6735 (progn
6736
6737 ;; CASE 2
6738 (when (and (or at-type maybe-typeless)
6739 (not (or got-prefix got-parens)))
6740 ;; Got another identifier directly after the type, so it's a
6741 ;; declaration.
6742 (throw 'at-decl-or-cast t))
6743
6744 (when (and got-parens
6745 (not got-prefix)
6746 (not got-suffix-after-parens)
6747 (or backup-at-type
6748 maybe-typeless
6749 backup-maybe-typeless))
6750 ;; Got a declaration of the form "foo bar (gnu);" where we've
6751 ;; recognized "bar" as the type and "gnu" as the declarator.
6752 ;; In this case it's however more likely that "bar" is the
6753 ;; declarator and "gnu" a function argument or initializer (if
6754 ;; `c-recognize-paren-inits' is set), since the parens around
6755 ;; "gnu" would be superfluous if it's a declarator. Shift the
6756 ;; type one step backward.
6757 (c-fdoc-shift-type-backward)))
6758
6759 ;; Found no identifier.
6760
6761 (if backup-at-type
6762 (progn
6763
6764
6765 ;; CASE 3
6766 (when (= (point) start)
6767 ;; Got a plain list of identifiers. If a colon follows it's
6768 ;; a valid label, or maybe a bitfield. Otherwise the last
6769 ;; one probably is the declared identifier and we should
6770 ;; back up to the previous type, providing it isn't a cast.
6771 (if (and (eq (char-after) ?:)
6772 (not (c-major-mode-is 'java-mode)))
6773 (cond
6774 ;; If we've found a specifier keyword then it's a
6775 ;; declaration regardless.
6776 ((eq at-decl-or-cast t)
6777 (throw 'at-decl-or-cast t))
6778 ((and c-has-bitfields
6779 (eq at-decl-or-cast 'ids)) ; bitfield.
6780 (setq backup-if-not-cast t)
6781 (throw 'at-decl-or-cast t)))
6782
6783 (setq backup-if-not-cast t)
6784 (throw 'at-decl-or-cast t)))
6785
6786 ;; CASE 4
6787 (when (and got-suffix
6788 (not got-prefix)
6789 (not got-parens))
6790 ;; Got a plain list of identifiers followed by some suffix.
6791 ;; If this isn't a cast then the last identifier probably is
6792 ;; the declared one and we should back up to the previous
6793 ;; type.
6794 (setq backup-if-not-cast t)
6795 (throw 'at-decl-or-cast t)))
6796
6797 ;; CASE 5
6798 (when (eq at-type t)
6799 ;; If the type is known we know that there can't be any
6800 ;; identifier somewhere else, and it's only in declarations in
6801 ;; e.g. function prototypes and in casts that the identifier may
6802 ;; be left out.
6803 (throw 'at-decl-or-cast t))
6804
6805 (when (= (point) start)
6806 ;; Only got a single identifier (parsed as a type so far).
6807 ;; CASE 6
6808 (if (and
6809 ;; Check that the identifier isn't at the start of an
6810 ;; expression.
6811 at-decl-end
6812 (cond
6813 ((eq context 'decl)
6814 ;; Inside an arglist that contains declarations. If K&R
6815 ;; style declarations and parenthesis style initializers
6816 ;; aren't allowed then the single identifier must be a
6817 ;; type, else we require that it's known or found
6818 ;; (primitive types are handled above).
6819 (or (and (not c-recognize-knr-p)
6820 (not c-recognize-paren-inits))
6821 (memq at-type '(known found))))
6822 ((eq context '<>)
6823 ;; Inside a template arglist. Accept known and found
6824 ;; types; other identifiers could just as well be
6825 ;; constants in C++.
6826 (memq at-type '(known found)))))
6827 (throw 'at-decl-or-cast t)
6828 ;; CASE 7
6829 ;; Can't be a valid declaration or cast, but if we've found a
6830 ;; specifier it can't be anything else either, so treat it as
6831 ;; an invalid/unfinished declaration or cast.
6832 (throw 'at-decl-or-cast at-decl-or-cast))))
6833
6834 (if (and got-parens
6835 (not got-prefix)
6836 (not context)
6837 (not (eq at-type t))
6838 (or backup-at-type
6839 maybe-typeless
6840 backup-maybe-typeless
6841 (when c-recognize-typeless-decls
6842 (or (not got-suffix)
6843 (not (looking-at
6844 c-after-suffixed-type-maybe-decl-key))))))
6845 ;; Got an empty paren pair and a preceding type that probably
6846 ;; really is the identifier. Shift the type backwards to make
6847 ;; the last one the identifier. This is analogous to the
6848 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6849 ;; above.
6850 ;;
6851 ;; Exception: In addition to the conditions in that
6852 ;; "backtracking" code, do not shift backward if we're not
6853 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6854 ;; Since there's no preceding type, the shift would mean that
6855 ;; the declaration is typeless. But if the regexp doesn't match
6856 ;; then we will simply fall through in the tests below and not
6857 ;; recognize it at all, so it's better to try it as an abstract
6858 ;; declarator instead.
6859 (c-fdoc-shift-type-backward)
6860
6861 ;; Still no identifier.
6862 ;; CASE 8
6863 (when (and got-prefix (or got-parens got-suffix))
6864 ;; Require `got-prefix' together with either `got-parens' or
6865 ;; `got-suffix' to recognize it as an abstract declarator:
6866 ;; `got-parens' only is probably an empty function call.
6867 ;; `got-suffix' only can build an ordinary expression together
6868 ;; with the preceding identifier which we've taken as a type.
6869 ;; We could actually accept on `got-prefix' only, but that can
6870 ;; easily occur temporarily while writing an expression so we
6871 ;; avoid that case anyway. We could do a better job if we knew
6872 ;; the point when the fontification was invoked.
6873 (throw 'at-decl-or-cast t))
6874
6875 ;; CASE 9
6876 (when (and at-type
6877 (not got-prefix)
6878 (not got-parens)
6879 got-suffix-after-parens
6880 (eq (char-after got-suffix-after-parens) ?\())
6881 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6882 ;; normal function call after all (or perhaps a C++ style object
6883 ;; instantiation expression).
6884 (throw 'at-decl-or-cast nil))))
6885
6886 ;; CASE 10
6887 (when at-decl-or-cast
6888 ;; By now we've located the type in the declaration that we know
6889 ;; we're in.
6890 (throw 'at-decl-or-cast t))
6891
6892 ;; CASE 11
6893 (when (and got-identifier
6894 (not context)
6895 (looking-at c-after-suffixed-type-decl-key)
6896 (if (and got-parens
6897 (not got-prefix)
6898 (not got-suffix)
6899 (not (eq at-type t)))
6900 ;; Shift the type backward in the case that there's a
6901 ;; single identifier inside parens. That can only
6902 ;; occur in K&R style function declarations so it's
6903 ;; more likely that it really is a function call.
6904 ;; Therefore we only do this after
6905 ;; `c-after-suffixed-type-decl-key' has matched.
6906 (progn (c-fdoc-shift-type-backward) t)
6907 got-suffix-after-parens))
6908 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6909 (throw 'at-decl-or-cast t))
6910
6911 ;; CASE 12
6912 (when (and (or got-prefix (not got-parens))
6913 (memq at-type '(t known)))
6914 ;; It's a declaration if a known type precedes it and it can't be a
6915 ;; function call.
6916 (throw 'at-decl-or-cast t))
6917
6918 ;; If we get here we can't tell if this is a type decl or a normal
6919 ;; expression by looking at it alone. (That's under the assumption
6920 ;; that normal expressions always can look like type decl expressions,
6921 ;; which isn't really true but the cases where it doesn't hold are so
6922 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6923 ;; the effort to look for them.)
6924
6925 (unless (or at-decl-end (looking-at "=[^=]"))
6926 ;; If this is a declaration it should end here or its initializer(*)
6927 ;; should start here, so check for allowed separation tokens. Note
6928 ;; that this rule doesn't work e.g. with a K&R arglist after a
6929 ;; function header.
6930 ;;
6931 ;; *) Don't check for C++ style initializers using parens
6932 ;; since those already have been matched as suffixes.
6933 ;;
6934 ;; If `at-decl-or-cast' is then we've found some other sign that
6935 ;; it's a declaration or cast, so then it's probably an
6936 ;; invalid/unfinished one.
6937 (throw 'at-decl-or-cast at-decl-or-cast))
6938
6939 ;; Below are tests that only should be applied when we're certain to
6940 ;; not have parsed halfway through an expression.
6941
6942 ;; CASE 14
6943 (when (memq at-type '(t known))
6944 ;; The expression starts with a known type so treat it as a
6945 ;; declaration.
6946 (throw 'at-decl-or-cast t))
6947
6948 ;; CASE 15
6949 (when (and (c-major-mode-is 'c++-mode)
6950 ;; In C++ we check if the identifier is a known type, since
6951 ;; (con|de)structors use the class name as identifier.
6952 ;; We've always shifted over the identifier as a type and
6953 ;; then backed up again in this case.
6954 identifier-type
6955 (or (memq identifier-type '(found known))
6956 (and (eq (char-after identifier-start) ?~)
6957 ;; `at-type' probably won't be 'found for
6958 ;; destructors since the "~" is then part of the
6959 ;; type name being checked against the list of
6960 ;; known types, so do a check without that
6961 ;; operator.
6962 (or (save-excursion
6963 (goto-char (1+ identifier-start))
6964 (c-forward-syntactic-ws)
6965 (c-with-syntax-table
6966 c-identifier-syntax-table
6967 (looking-at c-known-type-key)))
6968 (save-excursion
6969 (goto-char (1+ identifier-start))
6970 ;; We have already parsed the type earlier,
6971 ;; so it'd be possible to cache the end
6972 ;; position instead of redoing it here, but
6973 ;; then we'd need to keep track of another
6974 ;; position everywhere.
6975 (c-check-type (point)
6976 (progn (c-forward-type)
6977 (point))))))))
6978 (throw 'at-decl-or-cast t))
6979
6980 (if got-identifier
6981 (progn
6982 ;; CASE 16
6983 (when (and got-prefix-before-parens
6984 at-type
6985 (or at-decl-end (looking-at "=[^=]"))
6986 (not context)
6987 (not got-suffix))
6988 ;; Got something like "foo * bar;". Since we're not inside an
6989 ;; arglist it would be a meaningless expression because the
6990 ;; result isn't used. We therefore choose to recognize it as
6991 ;; a declaration. Do not allow a suffix since it could then
6992 ;; be a function call.
6993 (throw 'at-decl-or-cast t))
6994
6995 ;; CASE 17
6996 (when (and (or got-suffix-after-parens
6997 (looking-at "=[^=]"))
6998 (eq at-type 'found)
6999 (not (eq context 'arglist)))
7000 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
7001 ;; be an odd expression or it could be a declaration. Treat
7002 ;; it as a declaration if "a" has been used as a type
7003 ;; somewhere else (if it's a known type we won't get here).
7004 (throw 'at-decl-or-cast t)))
7005
7006 ;; CASE 18
7007 (when (and context
7008 (or got-prefix
7009 (and (eq context 'decl)
7010 (not c-recognize-paren-inits)
7011 (or got-parens got-suffix))))
7012 ;; Got a type followed by an abstract declarator. If `got-prefix'
7013 ;; is set it's something like "a *" without anything after it. If
7014 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
7015 ;; or similar, which we accept only if the context rules out
7016 ;; expressions.
7017 (throw 'at-decl-or-cast t)))
7018
7019 ;; If we had a complete symbol table here (which rules out
7020 ;; `c-found-types') we should return t due to the disambiguation rule
7021 ;; (in at least C++) that anything that can be parsed as a declaration
7022 ;; is a declaration. Now we're being more defensive and prefer to
7023 ;; highlight things like "foo (bar);" as a declaration only if we're
7024 ;; inside an arglist that contains declarations.
7025 (eq context 'decl))))
7026
7027 ;; The point is now after the type decl expression.
7028
7029 (cond
7030 ;; Check for a cast.
7031 ((save-excursion
7032 (and
7033 c-cast-parens
7034
7035 ;; Should be the first type/identifier in a cast paren.
7036 (> preceding-token-end (point-min))
7037 (memq (char-before preceding-token-end) c-cast-parens)
7038
7039 ;; The closing paren should follow.
7040 (progn
7041 (c-forward-syntactic-ws)
7042 (looking-at "\\s\)"))
7043
7044 ;; There should be a primary expression after it.
7045 (let (pos)
7046 (forward-char)
7047 (c-forward-syntactic-ws)
7048 (setq cast-end (point))
7049 (and (looking-at c-primary-expr-regexp)
7050 (progn
7051 (setq pos (match-end 0))
7052 (or
7053 ;; Check if the expression begins with a prefix keyword.
7054 (match-beginning 2)
7055 (if (match-beginning 1)
7056 ;; Expression begins with an ambiguous operator. Treat
7057 ;; it as a cast if it's a type decl or if we've
7058 ;; recognized the type somewhere else.
7059 (or at-decl-or-cast
7060 (memq at-type '(t known found)))
7061 ;; Unless it's a keyword, it's the beginning of a primary
7062 ;; expression.
7063 (not (looking-at c-keywords-regexp)))))
7064 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
7065 ;; that it matched a whole one so that we don't e.g. confuse
7066 ;; the operator '-' with '->'. It's ok if it matches further,
7067 ;; though, since it e.g. can match the float '.5' while the
7068 ;; operator regexp only matches '.'.
7069 (or (not (looking-at c-nonsymbol-token-regexp))
7070 (<= (match-end 0) pos))))
7071
7072 ;; There should either be a cast before it or something that isn't an
7073 ;; identifier or close paren.
7074 (> preceding-token-end (point-min))
7075 (progn
7076 (goto-char (1- preceding-token-end))
7077 (or (eq (point) last-cast-end)
7078 (progn
7079 (c-backward-syntactic-ws)
7080 (if (< (skip-syntax-backward "w_") 0)
7081 ;; It's a symbol. Accept it only if it's one of the
7082 ;; keywords that can precede an expression (without
7083 ;; surrounding parens).
7084 (looking-at c-simple-stmt-key)
7085 (and
7086 ;; Check that it isn't a close paren (block close is ok,
7087 ;; though).
7088 (not (memq (char-before) '(?\) ?\])))
7089 ;; Check that it isn't a nonsymbol identifier.
7090 (not (c-on-identifier)))))))))
7091
7092 ;; Handle the cast.
7093 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7094 (let ((c-promote-possible-types t))
7095 (goto-char type-start)
7096 (c-forward-type)))
7097
7098 (goto-char cast-end)
7099 'cast)
7100
7101 (at-decl-or-cast
7102 ;; We're at a declaration. Highlight the type and the following
7103 ;; declarators.
7104
7105 (when backup-if-not-cast
7106 (c-fdoc-shift-type-backward t))
7107
7108 (when (and (eq context 'decl) (looking-at ","))
7109 ;; Make sure to propagate the `c-decl-arg-start' property to
7110 ;; the next argument if it's set in this one, to cope with
7111 ;; interactive refontification.
7112 (c-put-c-type-property (point) 'c-decl-arg-start))
7113
7114 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7115 (let ((c-promote-possible-types t))
7116 (save-excursion
7117 (goto-char type-start)
7118 (c-forward-type))))
7119
7120 (cons id-start
7121 (and (or at-type-decl at-typedef)
7122 (cons at-type-decl at-typedef))))
7123
7124 (t
7125 ;; False alarm. Restore the recorded ranges.
7126 (setq c-record-type-identifiers save-rec-type-ids
7127 c-record-ref-identifiers save-rec-ref-ids)
7128 nil))))
7129
7130 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
7131 ;; Assuming that point is at the beginning of a token, check if it starts a
7132 ;; label and if so move over it and return non-nil (t in default situations,
7133 ;; specific symbols (see below) for interesting situations), otherwise don't
7134 ;; move and return nil. "Label" here means "most things with a colon".
7135 ;;
7136 ;; More precisely, a "label" is regarded as one of:
7137 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7138 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7139 ;; bare "case", should the colon be missing. We return t;
7140 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7141 ;; return t;
7142 ;; (iv) One of QT's "extended" C++ variants of
7143 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7144 ;; Returns the symbol `qt-2kwds-colon'.
7145 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
7146 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
7147 ;; colon). Currently (2006-03), this applies only to Objective C's
7148 ;; keywords "@private", "@protected", and "@public". Returns t.
7149 ;;
7150 ;; One of the things which will NOT be recognized as a label is a bit-field
7151 ;; element of a struct, something like "int foo:5".
7152 ;;
7153 ;; The end of the label is taken to be just after the colon, or the end of
7154 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7155 ;; after the end on return. The terminating char gets marked with
7156 ;; `c-decl-end' to improve recognition of the following declaration or
7157 ;; statement.
7158 ;;
7159 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7160 ;; label, if any, has already been marked up like that.
7161 ;;
7162 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7163 ;; after the preceding token, i.e. on the other side of the
7164 ;; syntactic ws from the point. Use a value less than or equal to
7165 ;; (point-min) if the point is at the first token in (the visible
7166 ;; part of) the buffer.
7167 ;;
7168 ;; The optional LIMIT limits the forward scan for the colon.
7169 ;;
7170 ;; This function records the ranges of the label symbols on
7171 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7172 ;; non-nil.
7173 ;;
7174 ;; This function might do hidden buffer changes.
7175
7176 (let ((start (point))
7177 label-end
7178 qt-symbol-idx
7179 macro-start ; if we're in one.
7180 label-type
7181 kwd)
7182 (cond
7183 ;; "case" or "default" (Doesn't apply to AWK).
7184 ((looking-at c-label-kwds-regexp)
7185 (let ((kwd-end (match-end 1)))
7186 ;; Record only the keyword itself for fontification, since in
7187 ;; case labels the following is a constant expression and not
7188 ;; a label.
7189 (when c-record-type-identifiers
7190 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7191
7192 ;; Find the label end.
7193 (goto-char kwd-end)
7194 (setq label-type
7195 (if (and (c-syntactic-re-search-forward
7196 ;; Stop on chars that aren't allowed in expressions,
7197 ;; and on operator chars that would be meaningless
7198 ;; there. FIXME: This doesn't cope with ?: operators.
7199 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7200 limit t t nil 1)
7201 (match-beginning 2))
7202
7203 (progn ; there's a proper :
7204 (goto-char (match-beginning 2)) ; just after the :
7205 (c-put-c-type-property (1- (point)) 'c-decl-end)
7206 t)
7207
7208 ;; It's an unfinished label. We consider the keyword enough
7209 ;; to recognize it as a label, so that it gets fontified.
7210 ;; Leave the point at the end of it, but don't put any
7211 ;; `c-decl-end' marker.
7212 (goto-char kwd-end)
7213 t))))
7214
7215 ;; @private, @protected, @public, in Objective C, or similar.
7216 ((and c-opt-extra-label-key
7217 (looking-at c-opt-extra-label-key))
7218 ;; For a `c-opt-extra-label-key' match, we record the whole
7219 ;; thing for fontification. That's to get the leading '@' in
7220 ;; Objective-C protection labels fontified.
7221 (goto-char (match-end 1))
7222 (when c-record-type-identifiers
7223 (c-record-ref-id (cons (match-beginning 1) (point))))
7224 (c-put-c-type-property (1- (point)) 'c-decl-end)
7225 (setq label-type t))
7226
7227 ;; All other cases of labels.
7228 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
7229
7230 ;; A colon label must have something before the colon.
7231 (not (eq (char-after) ?:))
7232
7233 ;; Check that we're not after a token that can't precede a label.
7234 (or
7235 ;; Trivially succeeds when there's no preceding token.
7236 ;; Succeeds when we're at a virtual semicolon.
7237 (if preceding-token-end
7238 (<= preceding-token-end (point-min))
7239 (save-excursion
7240 (c-backward-syntactic-ws)
7241 (setq preceding-token-end (point))
7242 (or (bobp)
7243 (c-at-vsemi-p))))
7244
7245 ;; Check if we're after a label, if we're after a closing
7246 ;; paren that belong to statement, and with
7247 ;; `c-label-prefix-re'. It's done in different order
7248 ;; depending on `assume-markup' since the checks have
7249 ;; different expensiveness.
7250 (if assume-markup
7251 (or
7252 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7253 'c-decl-end)
7254
7255 (save-excursion
7256 (goto-char (1- preceding-token-end))
7257 (c-beginning-of-current-token)
7258 (or (looking-at c-label-prefix-re)
7259 (looking-at c-block-stmt-1-key)))
7260
7261 (and (eq (char-before preceding-token-end) ?\))
7262 (c-after-conditional)))
7263
7264 (or
7265 (save-excursion
7266 (goto-char (1- preceding-token-end))
7267 (c-beginning-of-current-token)
7268 (or (looking-at c-label-prefix-re)
7269 (looking-at c-block-stmt-1-key)))
7270
7271 (cond
7272 ((eq (char-before preceding-token-end) ?\))
7273 (c-after-conditional))
7274
7275 ((eq (char-before preceding-token-end) ?:)
7276 ;; Might be after another label, so check it recursively.
7277 (save-restriction
7278 (save-excursion
7279 (goto-char (1- preceding-token-end))
7280 ;; Essentially the same as the
7281 ;; `c-syntactic-re-search-forward' regexp below.
7282 (setq macro-start
7283 (save-excursion (and (c-beginning-of-macro)
7284 (point))))
7285 (if macro-start (narrow-to-region macro-start (point-max)))
7286 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7287 ;; Note: the following should work instead of the
7288 ;; narrow-to-region above. Investigate why not,
7289 ;; sometime. ACM, 2006-03-31.
7290 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7291 ;; macro-start t)
7292 (let ((pte (point))
7293 ;; If the caller turned on recording for us,
7294 ;; it shouldn't apply when we check the
7295 ;; preceding label.
7296 c-record-type-identifiers)
7297 ;; A label can't start at a cpp directive. Check for
7298 ;; this, since c-forward-syntactic-ws would foul up on it.
7299 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7300 (c-forward-syntactic-ws)
7301 (c-forward-label nil pte start))))))))))
7302
7303 ;; Point is still at the beginning of the possible label construct.
7304 ;;
7305 ;; Check that the next nonsymbol token is ":", or that we're in one
7306 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7307 ;; arguments. FIXME: Should build this regexp from the language
7308 ;; constants.
7309 (cond
7310 ;; public: protected: private:
7311 ((and
7312 (c-major-mode-is 'c++-mode)
7313 (search-forward-regexp
7314 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7315 (progn (backward-char)
7316 (c-forward-syntactic-ws limit)
7317 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7318 (forward-char)
7319 (setq label-type t))
7320 ;; QT double keyword like "protected slots:" or goto target.
7321 ((progn (goto-char start) nil))
7322 ((when (c-syntactic-re-search-forward
7323 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7324 (backward-char)
7325 (setq label-end (point))
7326 (setq qt-symbol-idx
7327 (and (c-major-mode-is 'c++-mode)
7328 (string-match
7329 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7330 (buffer-substring start (point)))))
7331 (c-forward-syntactic-ws limit)
7332 (cond
7333 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7334 (forward-char)
7335 (setq label-type
7336 (if (or (string= "signals" ; Special QT macro
7337 (setq kwd (buffer-substring-no-properties start label-end)))
7338 (string= "Q_SIGNALS" kwd))
7339 'qt-1kwd-colon
7340 'goto-target)))
7341 ((and qt-symbol-idx
7342 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7343 (progn (c-forward-syntactic-ws limit)
7344 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7345 (forward-char)
7346 (setq label-type 'qt-2kwds-colon)))))))
7347
7348 (save-restriction
7349 (narrow-to-region start (point))
7350
7351 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7352 (catch 'check-label
7353 (goto-char start)
7354 (while (progn
7355 (when (looking-at c-nonlabel-token-key)
7356 (goto-char start)
7357 (setq label-type nil)
7358 (throw 'check-label nil))
7359 (and (c-safe (c-forward-sexp)
7360 (c-forward-syntactic-ws)
7361 t)
7362 (not (eobp)))))
7363
7364 ;; Record the identifiers in the label for fontification, unless
7365 ;; it begins with `c-label-kwds' in which case the following
7366 ;; identifiers are part of a (constant) expression that
7367 ;; shouldn't be fontified.
7368 (when (and c-record-type-identifiers
7369 (progn (goto-char start)
7370 (not (looking-at c-label-kwds-regexp))))
7371 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7372 (c-record-ref-id (cons (match-beginning 0)
7373 (match-end 0)))))
7374
7375 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7376 (goto-char (point-max)))))
7377
7378 (t
7379 ;; Not a label.
7380 (goto-char start)))
7381 label-type))
7382
7383 (defun c-forward-objc-directive ()
7384 ;; Assuming the point is at the beginning of a token, try to move
7385 ;; forward to the end of the Objective-C directive that starts
7386 ;; there. Return t if a directive was fully recognized, otherwise
7387 ;; the point is moved as far as one could be successfully parsed and
7388 ;; nil is returned.
7389 ;;
7390 ;; This function records identifier ranges on
7391 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7392 ;; `c-record-type-identifiers' is non-nil.
7393 ;;
7394 ;; This function might do hidden buffer changes.
7395
7396 (let ((start (point))
7397 start-char
7398 (c-promote-possible-types t)
7399 ;; Turn off recognition of angle bracket arglists while parsing
7400 ;; types here since the protocol reference list might then be
7401 ;; considered part of the preceding name or superclass-name.
7402 c-recognize-<>-arglists)
7403
7404 (if (or
7405 (when (looking-at
7406 (eval-when-compile
7407 (c-make-keywords-re t
7408 (append (c-lang-const c-protection-kwds objc)
7409 '("@end"))
7410 'objc-mode)))
7411 (goto-char (match-end 1))
7412 t)
7413
7414 (and
7415 (looking-at
7416 (eval-when-compile
7417 (c-make-keywords-re t
7418 '("@interface" "@implementation" "@protocol")
7419 'objc-mode)))
7420
7421 ;; Handle the name of the class itself.
7422 (progn
7423 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7424 ; at EOB.
7425 (goto-char (match-end 0))
7426 (c-skip-ws-forward)
7427 (c-forward-type))
7428
7429 (catch 'break
7430 ;; Look for ": superclass-name" or "( category-name )".
7431 (when (looking-at "[:\(]")
7432 (setq start-char (char-after))
7433 (forward-char)
7434 (c-forward-syntactic-ws)
7435 (unless (c-forward-type) (throw 'break nil))
7436 (when (eq start-char ?\()
7437 (unless (eq (char-after) ?\)) (throw 'break nil))
7438 (forward-char)
7439 (c-forward-syntactic-ws)))
7440
7441 ;; Look for a protocol reference list.
7442 (if (eq (char-after) ?<)
7443 (let ((c-recognize-<>-arglists t)
7444 (c-parse-and-markup-<>-arglists t)
7445 c-restricted-<>-arglists)
7446 (c-forward-<>-arglist t))
7447 t))))
7448
7449 (progn
7450 (c-backward-syntactic-ws)
7451 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7452 (c-put-c-type-property (1- (point)) 'c-decl-end)
7453 t)
7454
7455 (c-clear-c-type-property start (point) 'c-decl-end)
7456 nil)))
7457
7458 (defun c-beginning-of-inheritance-list (&optional lim)
7459 ;; Go to the first non-whitespace after the colon that starts a
7460 ;; multiple inheritance introduction. Optional LIM is the farthest
7461 ;; back we should search.
7462 ;;
7463 ;; This function might do hidden buffer changes.
7464 (c-with-syntax-table c++-template-syntax-table
7465 (c-backward-token-2 0 t lim)
7466 (while (and (or (looking-at c-symbol-start)
7467 (looking-at "[<,]\\|::"))
7468 (zerop (c-backward-token-2 1 t lim))))))
7469
7470 (defun c-in-method-def-p ()
7471 ;; Return nil if we aren't in a method definition, otherwise the
7472 ;; position of the initial [+-].
7473 ;;
7474 ;; This function might do hidden buffer changes.
7475 (save-excursion
7476 (beginning-of-line)
7477 (and c-opt-method-key
7478 (looking-at c-opt-method-key)
7479 (point))
7480 ))
7481
7482 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7483 (defun c-in-gcc-asm-p ()
7484 ;; Return non-nil if point is within a gcc \"asm\" block.
7485 ;;
7486 ;; This should be called with point inside an argument list.
7487 ;;
7488 ;; Only one level of enclosing parentheses is considered, so for
7489 ;; instance `nil' is returned when in a function call within an asm
7490 ;; operand.
7491 ;;
7492 ;; This function might do hidden buffer changes.
7493
7494 (and c-opt-asm-stmt-key
7495 (save-excursion
7496 (beginning-of-line)
7497 (backward-up-list 1)
7498 (c-beginning-of-statement-1 (point-min) nil t)
7499 (looking-at c-opt-asm-stmt-key))))
7500
7501 (defun c-at-toplevel-p ()
7502 "Return a determination as to whether point is \"at the top level\".
7503 Informally, \"at the top level\" is anywhere where you can write
7504 a function.
7505
7506 More precisely, being at the top-level means that point is either
7507 outside any enclosing block (such as a function definition), or
7508 directly inside a class, namespace or other block that contains
7509 another declaration level.
7510
7511 If point is not at the top-level (e.g. it is inside a method
7512 definition), then nil is returned. Otherwise, if point is at a
7513 top-level not enclosed within a class definition, t is returned.
7514 Otherwise, a 2-vector is returned where the zeroth element is the
7515 buffer position of the start of the class declaration, and the first
7516 element is the buffer position of the enclosing class's opening
7517 brace.
7518
7519 Note that this function might do hidden buffer changes. See the
7520 comment at the start of cc-engine.el for more info."
7521 (let ((paren-state (c-parse-state)))
7522 (or (not (c-most-enclosing-brace paren-state))
7523 (c-search-uplist-for-classkey paren-state))))
7524
7525 (defun c-just-after-func-arglist-p (&optional lim)
7526 ;; Return non-nil if the point is in the region after the argument
7527 ;; list of a function and its opening brace (or semicolon in case it
7528 ;; got no body). If there are K&R style argument declarations in
7529 ;; that region, the point has to be inside the first one for this
7530 ;; function to recognize it.
7531 ;;
7532 ;; If successful, the point is moved to the first token after the
7533 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7534 ;; the position of the opening paren of the function arglist is
7535 ;; returned.
7536 ;;
7537 ;; The point is clobbered if not successful.
7538 ;;
7539 ;; LIM is used as bound for backward buffer searches.
7540 ;;
7541 ;; This function might do hidden buffer changes.
7542
7543 (let ((beg (point)) end id-start)
7544 (and
7545 (eq (c-beginning-of-statement-1 lim) 'same)
7546
7547 (not (or (c-major-mode-is 'objc-mode)
7548 (c-forward-objc-directive)))
7549
7550 (setq id-start
7551 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7552 (< id-start beg)
7553
7554 ;; There should not be a '=' or ',' between beg and the
7555 ;; start of the declaration since that means we were in the
7556 ;; "expression part" of the declaration.
7557 (or (> (point) beg)
7558 (not (looking-at "[=,]")))
7559
7560 (save-excursion
7561 ;; Check that there's an arglist paren in the
7562 ;; declaration.
7563 (goto-char id-start)
7564 (cond ((eq (char-after) ?\()
7565 ;; The declarator is a paren expression, so skip past it
7566 ;; so that we don't get stuck on that instead of the
7567 ;; function arglist.
7568 (c-forward-sexp))
7569 ((and c-opt-op-identifier-prefix
7570 (looking-at c-opt-op-identifier-prefix))
7571 ;; Don't trip up on "operator ()".
7572 (c-forward-token-2 2 t)))
7573 (and (< (point) beg)
7574 (c-syntactic-re-search-forward "(" beg t t)
7575 (1- (point)))))))
7576
7577 (defun c-in-knr-argdecl (&optional lim)
7578 ;; Return the position of the first argument declaration if point is
7579 ;; inside a K&R style argument declaration list, nil otherwise.
7580 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7581 ;; position that bounds the backward search for the argument list.
7582 ;;
7583 ;; Point must be within a possible K&R region, e.g. just before a top-level
7584 ;; "{". It must be outside of parens and brackets. The test can return
7585 ;; false positives otherwise.
7586 ;;
7587 ;; This function might do hidden buffer changes.
7588
7589 (save-excursion
7590 (save-restriction
7591 ;; If we're in a macro, our search range is restricted to it. Narrow to
7592 ;; the searchable range.
7593 (let* ((macro-start (c-query-macro-start))
7594 (lim (max (or lim (point-min)) (or macro-start (point-min))))
7595 before-lparen after-rparen
7596 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
7597 (narrow-to-region lim (c-point 'eol))
7598
7599 ;; Search backwards for the defun's argument list. We give up if we
7600 ;; encounter a "}" (end of a previous defun) or BOB.
7601 ;;
7602 ;; The criterion for a paren structure being the arg list is:
7603 ;; o - there is non-WS stuff after it but before any "{"; AND
7604 ;; o - the token after it isn't a ";" AND
7605 ;; o - it is preceded by either an identifier (the function name) or
7606 ;; a macro expansion like "DEFUN (...)"; AND
7607 ;; o - its content is a non-empty comma-separated list of identifiers
7608 ;; (an empty arg list won't have a knr region).
7609 ;;
7610 ;; The following snippet illustrates these rules:
7611 ;; int foo (bar, baz, yuk)
7612 ;; int bar [] ;
7613 ;; int (*baz) (my_type) ;
7614 ;; int (*) (void) (*yuk) (void) ;
7615 ;; {
7616
7617 (catch 'knr
7618 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7619 (setq pp-count-out (1- pp-count-out))
7620 (c-syntactic-skip-backward "^)]}")
7621 (cond ((eq (char-before) ?\))
7622 (setq after-rparen (point)))
7623 ((eq (char-before) ?\])
7624 (setq after-rparen nil))
7625 (t ; either } (hit previous defun) or no more parens/brackets
7626 (throw 'knr nil)))
7627
7628 (if after-rparen
7629 ;; We're inside a paren. Could it be our argument list....?
7630 (if
7631 (and
7632 (progn
7633 (goto-char after-rparen)
7634 (unless (c-go-list-backward) (throw 'knr nil)) ;
7635 ;; FIXME!!! What about macros between the parens? 2007/01/20
7636 (setq before-lparen (point)))
7637
7638 ;; It can't be the arg list if next token is ; or {
7639 (progn (goto-char after-rparen)
7640 (c-forward-syntactic-ws)
7641 (not (memq (char-after) '(?\; ?\{))))
7642
7643 ;; Is the thing preceding the list an identifier (the
7644 ;; function name), or a macro expansion?
7645 (progn
7646 (goto-char before-lparen)
7647 (eq (c-backward-token-2) 0)
7648 (or (c-on-identifier)
7649 (and (eq (char-after) ?\))
7650 (c-go-up-list-backward)
7651 (eq (c-backward-token-2) 0)
7652 (c-on-identifier))))
7653
7654 ;; Have we got a non-empty list of comma-separated
7655 ;; identifiers?
7656 (progn
7657 (goto-char before-lparen)
7658 (c-forward-token-2) ; to first token inside parens
7659 (and
7660 (c-on-identifier)
7661 (c-forward-token-2)
7662 (catch 'id-list
7663 (while (eq (char-after) ?\,)
7664 (c-forward-token-2)
7665 (unless (c-on-identifier) (throw 'id-list nil))
7666 (c-forward-token-2))
7667 (eq (char-after) ?\))))))
7668
7669 ;; ...Yes. We've identified the function's argument list.
7670 (throw 'knr
7671 (progn (goto-char after-rparen)
7672 (c-forward-syntactic-ws)
7673 (point)))
7674
7675 ;; ...No. The current parens aren't the function's arg list.
7676 (goto-char before-lparen))
7677
7678 (or (c-go-list-backward) ; backwards over [ .... ]
7679 (throw 'knr nil)))))))))
7680
7681 (defun c-skip-conditional ()
7682 ;; skip forward over conditional at point, including any predicate
7683 ;; statements in parentheses. No error checking is performed.
7684 ;;
7685 ;; This function might do hidden buffer changes.
7686 (c-forward-sexp (cond
7687 ;; else if()
7688 ((looking-at (concat "\\<else"
7689 "\\([ \t\n]\\|\\\\\n\\)+"
7690 "if\\>\\([^_]\\|$\\)"))
7691 3)
7692 ;; do, else, try, finally
7693 ((looking-at (concat "\\<\\("
7694 "do\\|else\\|try\\|finally"
7695 "\\)\\>\\([^_]\\|$\\)"))
7696 1)
7697 ;; for, if, while, switch, catch, synchronized, foreach
7698 (t 2))))
7699
7700 (defun c-after-conditional (&optional lim)
7701 ;; If looking at the token after a conditional then return the
7702 ;; position of its start, otherwise return nil.
7703 ;;
7704 ;; This function might do hidden buffer changes.
7705 (save-excursion
7706 (and (zerop (c-backward-token-2 1 t lim))
7707 (or (looking-at c-block-stmt-1-key)
7708 (and (eq (char-after) ?\()
7709 (zerop (c-backward-token-2 1 t lim))
7710 (looking-at c-block-stmt-2-key)))
7711 (point))))
7712
7713 (defun c-after-special-operator-id (&optional lim)
7714 ;; If the point is after an operator identifier that isn't handled
7715 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7716 ;; position of the start of that identifier is returned. nil is
7717 ;; returned otherwise. The point may be anywhere in the syntactic
7718 ;; whitespace after the last token of the operator identifier.
7719 ;;
7720 ;; This function might do hidden buffer changes.
7721 (save-excursion
7722 (and c-overloadable-operators-regexp
7723 (zerop (c-backward-token-2 1 nil lim))
7724 (looking-at c-overloadable-operators-regexp)
7725 (or (not c-opt-op-identifier-prefix)
7726 (and
7727 (zerop (c-backward-token-2 1 nil lim))
7728 (looking-at c-opt-op-identifier-prefix)))
7729 (point))))
7730
7731 (defsubst c-backward-to-block-anchor (&optional lim)
7732 ;; Assuming point is at a brace that opens a statement block of some
7733 ;; kind, move to the proper anchor point for that block. It might
7734 ;; need to be adjusted further by c-add-stmt-syntax, but the
7735 ;; position at return is suitable as start position for that
7736 ;; function.
7737 ;;
7738 ;; This function might do hidden buffer changes.
7739 (unless (= (point) (c-point 'boi))
7740 (let ((start (c-after-conditional lim)))
7741 (if start
7742 (goto-char start)))))
7743
7744 (defsubst c-backward-to-decl-anchor (&optional lim)
7745 ;; Assuming point is at a brace that opens the block of a top level
7746 ;; declaration of some kind, move to the proper anchor point for
7747 ;; that block.
7748 ;;
7749 ;; This function might do hidden buffer changes.
7750 (unless (= (point) (c-point 'boi))
7751 (c-beginning-of-statement-1 lim)))
7752
7753 (defun c-search-decl-header-end ()
7754 ;; Search forward for the end of the "header" of the current
7755 ;; declaration. That's the position where the definition body
7756 ;; starts, or the first variable initializer, or the ending
7757 ;; semicolon. I.e. search forward for the closest following
7758 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7759 ;; _after_ the first found token, or at point-max if none is found.
7760 ;;
7761 ;; This function might do hidden buffer changes.
7762
7763 (let ((base (point)))
7764 (if (c-major-mode-is 'c++-mode)
7765
7766 ;; In C++ we need to take special care to handle operator
7767 ;; tokens and those pesky template brackets.
7768 (while (and
7769 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
7770 (or
7771 (c-end-of-current-token base)
7772 ;; Handle operator identifiers, i.e. ignore any
7773 ;; operator token preceded by "operator".
7774 (save-excursion
7775 (and (c-safe (c-backward-sexp) t)
7776 (looking-at c-opt-op-identifier-prefix)))
7777 (and (eq (char-before) ?<)
7778 (c-with-syntax-table c++-template-syntax-table
7779 (if (c-safe (goto-char (c-up-list-forward (point))))
7780 t
7781 (goto-char (point-max))
7782 nil)))))
7783 (setq base (point)))
7784
7785 (while (and
7786 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
7787 (c-end-of-current-token base))
7788 (setq base (point))))))
7789
7790 (defun c-beginning-of-decl-1 (&optional lim)
7791 ;; Go to the beginning of the current declaration, or the beginning
7792 ;; of the previous one if already at the start of it. Point won't
7793 ;; be moved out of any surrounding paren. Return a cons cell of the
7794 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7795 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7796 ;; style argument declarations (and they are to be recognized) then
7797 ;; KNR-POS is set to the start of the first such argument
7798 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7799 ;; position that bounds the backward search.
7800 ;;
7801 ;; NB: Cases where the declaration continues after the block, as in
7802 ;; "struct foo { ... } bar;", are currently recognized as two
7803 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7804 ;;
7805 ;; This function might do hidden buffer changes.
7806 (catch 'return
7807 (let* ((start (point))
7808 (last-stmt-start (point))
7809 (move (c-beginning-of-statement-1 lim nil t)))
7810
7811 ;; `c-beginning-of-statement-1' stops at a block start, but we
7812 ;; want to continue if the block doesn't begin a top level
7813 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7814 ;; or an open paren.
7815 (let ((beg (point)) tentative-move)
7816 ;; Go back one "statement" each time round the loop until we're just
7817 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7818 ;; an ObjC method. This will move over a multiple declaration whose
7819 ;; components are comma separated.
7820 (while (and
7821 ;; Must check with c-opt-method-key in ObjC mode.
7822 (not (and c-opt-method-key
7823 (looking-at c-opt-method-key)))
7824 (/= last-stmt-start (point))
7825 (progn
7826 (c-backward-syntactic-ws lim)
7827 (not (memq (char-before) '(?\; ?} ?: nil))))
7828 (save-excursion
7829 (backward-char)
7830 (not (looking-at "\\s(")))
7831 ;; Check that we don't move from the first thing in a
7832 ;; macro to its header.
7833 (not (eq (setq tentative-move
7834 (c-beginning-of-statement-1 lim nil t))
7835 'macro)))
7836 (setq last-stmt-start beg
7837 beg (point)
7838 move tentative-move))
7839 (goto-char beg))
7840
7841 (when c-recognize-knr-p
7842 (let ((fallback-pos (point)) knr-argdecl-start)
7843 ;; Handle K&R argdecls. Back up after the "statement" jumped
7844 ;; over by `c-beginning-of-statement-1', unless it was the
7845 ;; function body, in which case we're sitting on the opening
7846 ;; brace now. Then test if we're in a K&R argdecl region and
7847 ;; that we started at the other side of the first argdecl in
7848 ;; it.
7849 (unless (eq (char-after) ?{)
7850 (goto-char last-stmt-start))
7851 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
7852 (< knr-argdecl-start start)
7853 (progn
7854 (goto-char knr-argdecl-start)
7855 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
7856 (throw 'return
7857 (cons (if (eq (char-after fallback-pos) ?{)
7858 'previous
7859 'same)
7860 knr-argdecl-start))
7861 (goto-char fallback-pos))))
7862
7863 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7864 ;; statement, so the result will be 'previous if we've moved over any.
7865 ;; So change our result back to 'same if necessary.
7866 ;;
7867 ;; If they were brace list initializers we might not have moved over a
7868 ;; declaration boundary though, so change it to 'same if we've moved
7869 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7870 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7871 ;; potentially can search over a large amount of text.). Take special
7872 ;; pains not to get mislead by C++'s "operator=", and the like.
7873 (if (and (eq move 'previous)
7874 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
7875 c++-template-syntax-table
7876 (syntax-table))
7877 (save-excursion
7878 (and
7879 (progn
7880 (while ; keep going back to "[;={"s until we either find
7881 ; no more, or get to one which isn't an "operator ="
7882 (and (c-syntactic-re-search-forward "[;={]" start t t t)
7883 (eq (char-before) ?=)
7884 c-overloadable-operators-regexp
7885 c-opt-op-identifier-prefix
7886 (save-excursion
7887 (eq (c-backward-token-2) 0)
7888 (looking-at c-overloadable-operators-regexp)
7889 (eq (c-backward-token-2) 0)
7890 (looking-at c-opt-op-identifier-prefix))))
7891 (eq (char-before) ?=))
7892 (c-syntactic-re-search-forward "[;{]" start t t)
7893 (eq (char-before) ?{)
7894 (c-safe (goto-char (c-up-list-forward (point))) t)
7895 (not (c-syntactic-re-search-forward ";" start t t))))))
7896 (cons 'same nil)
7897 (cons move nil)))))
7898
7899 (defun c-end-of-decl-1 ()
7900 ;; Assuming point is at the start of a declaration (as detected by
7901 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7902 ;; `c-beginning-of-decl-1', this function handles the case when a
7903 ;; block is followed by identifiers in e.g. struct declarations in C
7904 ;; or C++. If a proper end was found then t is returned, otherwise
7905 ;; point is moved as far as possible within the current sexp and nil
7906 ;; is returned. This function doesn't handle macros; use
7907 ;; `c-end-of-macro' instead in those cases.
7908 ;;
7909 ;; This function might do hidden buffer changes.
7910 (let ((start (point))
7911 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
7912 c++-template-syntax-table
7913 (syntax-table))))
7914 (catch 'return
7915 (c-search-decl-header-end)
7916
7917 (when (and c-recognize-knr-p
7918 (eq (char-before) ?\;)
7919 (c-in-knr-argdecl start))
7920 ;; Stopped at the ';' in a K&R argdecl section which is
7921 ;; detected using the same criteria as in
7922 ;; `c-beginning-of-decl-1'. Move to the following block
7923 ;; start.
7924 (c-syntactic-re-search-forward "{" nil 'move t))
7925
7926 (when (eq (char-before) ?{)
7927 ;; Encountered a block in the declaration. Jump over it.
7928 (condition-case nil
7929 (goto-char (c-up-list-forward (point)))
7930 (error (goto-char (point-max))
7931 (throw 'return nil)))
7932 (if (or (not c-opt-block-decls-with-vars-key)
7933 (save-excursion
7934 (c-with-syntax-table decl-syntax-table
7935 (let ((lim (point)))
7936 (goto-char start)
7937 (not (and
7938 ;; Check for `c-opt-block-decls-with-vars-key'
7939 ;; before the first paren.
7940 (c-syntactic-re-search-forward
7941 (concat "[;=\(\[{]\\|\\("
7942 c-opt-block-decls-with-vars-key
7943 "\\)")
7944 lim t t t)
7945 (match-beginning 1)
7946 (not (eq (char-before) ?_))
7947 ;; Check that the first following paren is
7948 ;; the block.
7949 (c-syntactic-re-search-forward "[;=\(\[{]"
7950 lim t t t)
7951 (eq (char-before) ?{)))))))
7952 ;; The declaration doesn't have any of the
7953 ;; `c-opt-block-decls-with-vars' keywords in the
7954 ;; beginning, so it ends here at the end of the block.
7955 (throw 'return t)))
7956
7957 (c-with-syntax-table decl-syntax-table
7958 (while (progn
7959 (if (eq (char-before) ?\;)
7960 (throw 'return t))
7961 (c-syntactic-re-search-forward ";" nil 'move t))))
7962 nil)))
7963
7964 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
7965 ;; Assuming the point is at an open brace, check if it starts a
7966 ;; block that contains another declaration level, i.e. that isn't a
7967 ;; statement block or a brace list, and if so return non-nil.
7968 ;;
7969 ;; If the check is successful, the return value is the start of the
7970 ;; keyword that tells what kind of construct it is, i.e. typically
7971 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7972 ;; the point will be at the start of the construct, before any
7973 ;; leading specifiers, otherwise it's at the returned position.
7974 ;;
7975 ;; The point is clobbered if the check is unsuccessful.
7976 ;;
7977 ;; CONTAINING-SEXP is the position of the open of the surrounding
7978 ;; paren, or nil if none.
7979 ;;
7980 ;; The optional LIMIT limits the backward search for the start of
7981 ;; the construct. It's assumed to be at a syntactically relevant
7982 ;; position.
7983 ;;
7984 ;; If any template arglists are found in the searched region before
7985 ;; the open brace, they get marked with paren syntax.
7986 ;;
7987 ;; This function might do hidden buffer changes.
7988
7989 (let ((open-brace (point)) kwd-start first-specifier-pos)
7990 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7991
7992 (when (and c-recognize-<>-arglists
7993 (eq (char-before) ?>))
7994 ;; Could be at the end of a template arglist.
7995 (let ((c-parse-and-markup-<>-arglists t)
7996 (c-disallow-comma-in-<>-arglists
7997 (and containing-sexp
7998 (not (eq (char-after containing-sexp) ?{)))))
7999 (while (and
8000 (c-backward-<>-arglist nil limit)
8001 (progn
8002 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8003 (eq (char-before) ?>))))))
8004
8005 ;; Note: Can't get bogus hits inside template arglists below since they
8006 ;; have gotten paren syntax above.
8007 (when (and
8008 ;; If `goto-start' is set we begin by searching for the
8009 ;; first possible position of a leading specifier list.
8010 ;; The `c-decl-block-key' search continues from there since
8011 ;; we know it can't match earlier.
8012 (if goto-start
8013 (when (c-syntactic-re-search-forward c-symbol-start
8014 open-brace t t)
8015 (goto-char (setq first-specifier-pos (match-beginning 0)))
8016 t)
8017 t)
8018
8019 (cond
8020 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
8021 (goto-char (setq kwd-start (match-beginning 0)))
8022 (or
8023
8024 ;; Found a keyword that can't be a type?
8025 (match-beginning 1)
8026
8027 ;; Can be a type too, in which case it's the return type of a
8028 ;; function (under the assumption that no declaration level
8029 ;; block construct starts with a type).
8030 (not (c-forward-type))
8031
8032 ;; Jumped over a type, but it could be a declaration keyword
8033 ;; followed by the declared identifier that we've jumped over
8034 ;; instead (e.g. in "class Foo {"). If it indeed is a type
8035 ;; then we should be at the declarator now, so check for a
8036 ;; valid declarator start.
8037 ;;
8038 ;; Note: This doesn't cope with the case when a declared
8039 ;; identifier is followed by e.g. '(' in a language where '('
8040 ;; also might be part of a declarator expression. Currently
8041 ;; there's no such language.
8042 (not (or (looking-at c-symbol-start)
8043 (looking-at c-type-decl-prefix-key)))))
8044
8045 ;; In Pike a list of modifiers may be followed by a brace
8046 ;; to make them apply to many identifiers. Note that the
8047 ;; match data will be empty on return in this case.
8048 ((and (c-major-mode-is 'pike-mode)
8049 (progn
8050 (goto-char open-brace)
8051 (= (c-backward-token-2) 0))
8052 (looking-at c-specifier-key)
8053 ;; Use this variant to avoid yet another special regexp.
8054 (c-keyword-member (c-keyword-sym (match-string 1))
8055 'c-modifier-kwds))
8056 (setq kwd-start (point))
8057 t)))
8058
8059 ;; Got a match.
8060
8061 (if goto-start
8062 ;; Back up over any preceding specifiers and their clauses
8063 ;; by going forward from `first-specifier-pos', which is the
8064 ;; earliest possible position where the specifier list can
8065 ;; start.
8066 (progn
8067 (goto-char first-specifier-pos)
8068
8069 (while (< (point) kwd-start)
8070 (if (looking-at c-symbol-key)
8071 ;; Accept any plain symbol token on the ground that
8072 ;; it's a specifier masked through a macro (just
8073 ;; like `c-forward-decl-or-cast-1' skip forward over
8074 ;; such tokens).
8075 ;;
8076 ;; Could be more restrictive wrt invalid keywords,
8077 ;; but that'd only occur in invalid code so there's
8078 ;; no use spending effort on it.
8079 (let ((end (match-end 0)))
8080 (unless (c-forward-keyword-clause 0)
8081 (goto-char end)
8082 (c-forward-syntactic-ws)))
8083
8084 ;; Can't parse a declaration preamble and is still
8085 ;; before `kwd-start'. That means `first-specifier-pos'
8086 ;; was in some earlier construct. Search again.
8087 (if (c-syntactic-re-search-forward c-symbol-start
8088 kwd-start 'move t)
8089 (goto-char (setq first-specifier-pos (match-beginning 0)))
8090 ;; Got no preamble before the block declaration keyword.
8091 (setq first-specifier-pos kwd-start))))
8092
8093 (goto-char first-specifier-pos))
8094 (goto-char kwd-start))
8095
8096 kwd-start)))
8097
8098 (defun c-search-uplist-for-classkey (paren-state)
8099 ;; Check if the closest containing paren sexp is a declaration
8100 ;; block, returning a 2 element vector in that case. Aref 0
8101 ;; contains the bufpos at boi of the class key line, and aref 1
8102 ;; contains the bufpos of the open brace. This function is an
8103 ;; obsolete wrapper for `c-looking-at-decl-block'.
8104 ;;
8105 ;; This function might do hidden buffer changes.
8106 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
8107 (when open-paren-pos
8108 (save-excursion
8109 (goto-char open-paren-pos)
8110 (when (and (eq (char-after) ?{)
8111 (c-looking-at-decl-block
8112 (c-safe-position open-paren-pos paren-state)
8113 nil))
8114 (back-to-indentation)
8115 (vector (point) open-paren-pos))))))
8116
8117 (defmacro c-pull-open-brace (ps)
8118 ;; Pull the next open brace from PS (which has the form of paren-state),
8119 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
8120 `(progn
8121 (while (consp (car ,ps))
8122 (setq ,ps (cdr ,ps)))
8123 (prog1 (car ,ps)
8124 (setq ,ps (cdr ,ps)))))
8125
8126 (defun c-most-enclosing-decl-block (paren-state)
8127 ;; Return the buffer position of the most enclosing decl-block brace (in the
8128 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
8129 ;; none was found.
8130 (let* ((open-brace (c-pull-open-brace paren-state))
8131 (next-open-brace (c-pull-open-brace paren-state)))
8132 (while (and open-brace
8133 (save-excursion
8134 (goto-char open-brace)
8135 (not (c-looking-at-decl-block next-open-brace nil))))
8136 (setq open-brace next-open-brace
8137 next-open-brace (c-pull-open-brace paren-state)))
8138 open-brace))
8139
8140 (defun c-cheap-inside-bracelist-p (paren-state)
8141 ;; Return the position of the L-brace if point is inside a brace list
8142 ;; initialization of an array, etc. This is an approximate function,
8143 ;; designed for speed over accuracy. It will not find every bracelist, but
8144 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
8145 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
8146 ;; is everywhere else.
8147 (let (b-pos)
8148 (save-excursion
8149 (while
8150 (and (setq b-pos (c-pull-open-brace paren-state))
8151 (progn (goto-char b-pos)
8152 (c-backward-sws)
8153 (c-backward-token-2)
8154 (not (looking-at "=")))))
8155 b-pos)))
8156
8157 (defun c-inside-bracelist-p (containing-sexp paren-state)
8158 ;; return the buffer position of the beginning of the brace list
8159 ;; statement if we're inside a brace list, otherwise return nil.
8160 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
8161 ;; paren. PAREN-STATE is the remainder of the state of enclosing
8162 ;; braces
8163 ;;
8164 ;; N.B.: This algorithm can potentially get confused by cpp macros
8165 ;; placed in inconvenient locations. It's a trade-off we make for
8166 ;; speed.
8167 ;;
8168 ;; This function might do hidden buffer changes.
8169 (or
8170 ;; This will pick up brace list declarations.
8171 (c-safe
8172 (save-excursion
8173 (goto-char containing-sexp)
8174 (c-forward-sexp -1)
8175 (let (bracepos)
8176 (if (and (or (looking-at c-brace-list-key)
8177 (progn (c-forward-sexp -1)
8178 (looking-at c-brace-list-key)))
8179 (setq bracepos (c-down-list-forward (point)))
8180 (not (c-crosses-statement-barrier-p (point)
8181 (- bracepos 2))))
8182 (point)))))
8183 ;; this will pick up array/aggregate init lists, even if they are nested.
8184 (save-excursion
8185 (let ((class-key
8186 ;; Pike can have class definitions anywhere, so we must
8187 ;; check for the class key here.
8188 (and (c-major-mode-is 'pike-mode)
8189 c-decl-block-key))
8190 bufpos braceassignp lim next-containing)
8191 (while (and (not bufpos)
8192 containing-sexp)
8193 (when paren-state
8194 (if (consp (car paren-state))
8195 (setq lim (cdr (car paren-state))
8196 paren-state (cdr paren-state))
8197 (setq lim (car paren-state)))
8198 (when paren-state
8199 (setq next-containing (car paren-state)
8200 paren-state (cdr paren-state))))
8201 (goto-char containing-sexp)
8202 (if (c-looking-at-inexpr-block next-containing next-containing)
8203 ;; We're in an in-expression block of some kind. Do not
8204 ;; check nesting. We deliberately set the limit to the
8205 ;; containing sexp, so that c-looking-at-inexpr-block
8206 ;; doesn't check for an identifier before it.
8207 (setq containing-sexp nil)
8208 ;; see if the open brace is preceded by = or [...] in
8209 ;; this statement, but watch out for operator=
8210 (setq braceassignp 'dontknow)
8211 (c-backward-token-2 1 t lim)
8212 ;; Checks to do only on the first sexp before the brace.
8213 (when (and c-opt-inexpr-brace-list-key
8214 (eq (char-after) ?\[))
8215 ;; In Java, an initialization brace list may follow
8216 ;; directly after "new Foo[]", so check for a "new"
8217 ;; earlier.
8218 (while (eq braceassignp 'dontknow)
8219 (setq braceassignp
8220 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8221 ((looking-at c-opt-inexpr-brace-list-key) t)
8222 ((looking-at "\\sw\\|\\s_\\|[.[]")
8223 ;; Carry on looking if this is an
8224 ;; identifier (may contain "." in Java)
8225 ;; or another "[]" sexp.
8226 'dontknow)
8227 (t nil)))))
8228 ;; Checks to do on all sexps before the brace, up to the
8229 ;; beginning of the statement.
8230 (while (eq braceassignp 'dontknow)
8231 (cond ((eq (char-after) ?\;)
8232 (setq braceassignp nil))
8233 ((and class-key
8234 (looking-at class-key))
8235 (setq braceassignp nil))
8236 ((eq (char-after) ?=)
8237 ;; We've seen a =, but must check earlier tokens so
8238 ;; that it isn't something that should be ignored.
8239 (setq braceassignp 'maybe)
8240 (while (and (eq braceassignp 'maybe)
8241 (zerop (c-backward-token-2 1 t lim)))
8242 (setq braceassignp
8243 (cond
8244 ;; Check for operator =
8245 ((and c-opt-op-identifier-prefix
8246 (looking-at c-opt-op-identifier-prefix))
8247 nil)
8248 ;; Check for `<opchar>= in Pike.
8249 ((and (c-major-mode-is 'pike-mode)
8250 (or (eq (char-after) ?`)
8251 ;; Special case for Pikes
8252 ;; `[]=, since '[' is not in
8253 ;; the punctuation class.
8254 (and (eq (char-after) ?\[)
8255 (eq (char-before) ?`))))
8256 nil)
8257 ((looking-at "\\s.") 'maybe)
8258 ;; make sure we're not in a C++ template
8259 ;; argument assignment
8260 ((and
8261 (c-major-mode-is 'c++-mode)
8262 (save-excursion
8263 (let ((here (point))
8264 (pos< (progn
8265 (skip-chars-backward "^<>")
8266 (point))))
8267 (and (eq (char-before) ?<)
8268 (not (c-crosses-statement-barrier-p
8269 pos< here))
8270 (not (c-in-literal))
8271 ))))
8272 nil)
8273 (t t))))))
8274 (if (and (eq braceassignp 'dontknow)
8275 (/= (c-backward-token-2 1 t lim) 0))
8276 (setq braceassignp nil)))
8277 (if (not braceassignp)
8278 (if (eq (char-after) ?\;)
8279 ;; Brace lists can't contain a semicolon, so we're done.
8280 (setq containing-sexp nil)
8281 ;; Go up one level.
8282 (setq containing-sexp next-containing
8283 lim nil
8284 next-containing nil))
8285 ;; we've hit the beginning of the aggregate list
8286 (c-beginning-of-statement-1
8287 (c-most-enclosing-brace paren-state))
8288 (setq bufpos (point))))
8289 )
8290 bufpos))
8291 ))
8292
8293 (defun c-looking-at-special-brace-list (&optional lim)
8294 ;; If we're looking at the start of a pike-style list, ie `({ })',
8295 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
8296 ;; positions and its entry in c-special-brace-lists is returned, nil
8297 ;; otherwise. The ending position is nil if the list is still open.
8298 ;; LIM is the limit for forward search. The point may either be at
8299 ;; the `(' or at the following paren character. Tries to check the
8300 ;; matching closer, but assumes it's correct if no balanced paren is
8301 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8302 ;; a special brace list).
8303 ;;
8304 ;; This function might do hidden buffer changes.
8305 (if c-special-brace-lists
8306 (condition-case ()
8307 (save-excursion
8308 (let ((beg (point))
8309 inner-beg end type)
8310 (c-forward-syntactic-ws)
8311 (if (eq (char-after) ?\()
8312 (progn
8313 (forward-char 1)
8314 (c-forward-syntactic-ws)
8315 (setq inner-beg (point))
8316 (setq type (assq (char-after) c-special-brace-lists)))
8317 (if (setq type (assq (char-after) c-special-brace-lists))
8318 (progn
8319 (setq inner-beg (point))
8320 (c-backward-syntactic-ws)
8321 (forward-char -1)
8322 (setq beg (if (eq (char-after) ?\()
8323 (point)
8324 nil)))))
8325 (if (and beg type)
8326 (if (and (c-safe
8327 (goto-char beg)
8328 (c-forward-sexp 1)
8329 (setq end (point))
8330 (= (char-before) ?\)))
8331 (c-safe
8332 (goto-char inner-beg)
8333 (if (looking-at "\\s(")
8334 ;; Check balancing of the inner paren
8335 ;; below.
8336 (progn
8337 (c-forward-sexp 1)
8338 t)
8339 ;; If the inner char isn't a paren then
8340 ;; we can't check balancing, so just
8341 ;; check the char before the outer
8342 ;; closing paren.
8343 (goto-char end)
8344 (backward-char)
8345 (c-backward-syntactic-ws)
8346 (= (char-before) (cdr type)))))
8347 (if (or (/= (char-syntax (char-before)) ?\))
8348 (= (progn
8349 (c-forward-syntactic-ws)
8350 (point))
8351 (1- end)))
8352 (cons (cons beg end) type))
8353 (cons (list beg) type)))))
8354 (error nil))))
8355
8356 (defun c-looking-at-bos (&optional lim)
8357 ;; Return non-nil if between two statements or declarations, assuming
8358 ;; point is not inside a literal or comment.
8359 ;;
8360 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8361 ;; are recommended instead.
8362 ;;
8363 ;; This function might do hidden buffer changes.
8364 (c-at-statement-start-p))
8365 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8366
8367 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8368 ;; Return non-nil if we're looking at the beginning of a block
8369 ;; inside an expression. The value returned is actually a cons of
8370 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8371 ;; position of the beginning of the construct.
8372 ;;
8373 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8374 ;; position of the closest containing list. If it's nil, the
8375 ;; containing paren isn't used to decide whether we're inside an
8376 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8377 ;; needs to be farther back.
8378 ;;
8379 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8380 ;; brace block might be done. It should only be used when the
8381 ;; construct can be assumed to be complete, i.e. when the original
8382 ;; starting position was further down than that.
8383 ;;
8384 ;; This function might do hidden buffer changes.
8385
8386 (save-excursion
8387 (let ((res 'maybe) passed-paren
8388 (closest-lim (or containing-sexp lim (point-min)))
8389 ;; Look at the character after point only as a last resort
8390 ;; when we can't disambiguate.
8391 (block-follows (and (eq (char-after) ?{) (point))))
8392
8393 (while (and (eq res 'maybe)
8394 (progn (c-backward-syntactic-ws)
8395 (> (point) closest-lim))
8396 (not (bobp))
8397 (progn (backward-char)
8398 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8399 (c-safe (forward-char)
8400 (goto-char (scan-sexps (point) -1))))
8401
8402 (setq res
8403 (if (looking-at c-keywords-regexp)
8404 (let ((kw-sym (c-keyword-sym (match-string 1))))
8405 (cond
8406 ((and block-follows
8407 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8408 (and (not (eq passed-paren ?\[))
8409 (or (not (looking-at c-class-key))
8410 ;; If the class definition is at the start of
8411 ;; a statement, we don't consider it an
8412 ;; in-expression class.
8413 (let ((prev (point)))
8414 (while (and
8415 (= (c-backward-token-2 1 nil closest-lim) 0)
8416 (eq (char-syntax (char-after)) ?w))
8417 (setq prev (point)))
8418 (goto-char prev)
8419 (not (c-at-statement-start-p)))
8420 ;; Also, in Pike we treat it as an
8421 ;; in-expression class if it's used in an
8422 ;; object clone expression.
8423 (save-excursion
8424 (and check-at-end
8425 (c-major-mode-is 'pike-mode)
8426 (progn (goto-char block-follows)
8427 (zerop (c-forward-token-2 1 t)))
8428 (eq (char-after) ?\())))
8429 (cons 'inexpr-class (point))))
8430 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8431 (when (not passed-paren)
8432 (cons 'inexpr-statement (point))))
8433 ((c-keyword-member kw-sym 'c-lambda-kwds)
8434 (when (or (not passed-paren)
8435 (eq passed-paren ?\())
8436 (cons 'inlambda (point))))
8437 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8438 nil)
8439 (t
8440 'maybe)))
8441
8442 (if (looking-at "\\s(")
8443 (if passed-paren
8444 (if (and (eq passed-paren ?\[)
8445 (eq (char-after) ?\[))
8446 ;; Accept several square bracket sexps for
8447 ;; Java array initializations.
8448 'maybe)
8449 (setq passed-paren (char-after))
8450 'maybe)
8451 'maybe))))
8452
8453 (if (eq res 'maybe)
8454 (when (and c-recognize-paren-inexpr-blocks
8455 block-follows
8456 containing-sexp
8457 (eq (char-after containing-sexp) ?\())
8458 (goto-char containing-sexp)
8459 (if (or (save-excursion
8460 (c-backward-syntactic-ws lim)
8461 (and (> (point) (or lim (point-min)))
8462 (c-on-identifier)))
8463 (and c-special-brace-lists
8464 (c-looking-at-special-brace-list)))
8465 nil
8466 (cons 'inexpr-statement (point))))
8467
8468 res))))
8469
8470 (defun c-looking-at-inexpr-block-backward (paren-state)
8471 ;; Returns non-nil if we're looking at the end of an in-expression
8472 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8473 ;; PAREN-STATE is the paren state relevant at the current position.
8474 ;;
8475 ;; This function might do hidden buffer changes.
8476 (save-excursion
8477 ;; We currently only recognize a block.
8478 (let ((here (point))
8479 (elem (car-safe paren-state))
8480 containing-sexp)
8481 (when (and (consp elem)
8482 (progn (goto-char (cdr elem))
8483 (c-forward-syntactic-ws here)
8484 (= (point) here)))
8485 (goto-char (car elem))
8486 (if (setq paren-state (cdr paren-state))
8487 (setq containing-sexp (car-safe paren-state)))
8488 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8489 paren-state)
8490 containing-sexp)))))
8491
8492 (defun c-at-macro-vsemi-p (&optional pos)
8493 ;; Is there a "virtual semicolon" at POS or point?
8494 ;; (See cc-defs.el for full details of "virtual semicolons".)
8495 ;;
8496 ;; This is true when point is at the last non syntactic WS position on the
8497 ;; line, there is a macro call last on the line, and this particular macro's
8498 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
8499 ;; semicolon.
8500 (save-excursion
8501 (save-restriction
8502 (widen)
8503 (if pos
8504 (goto-char pos)
8505 (setq pos (point)))
8506 (and
8507 c-macro-with-semi-re
8508 (not (c-in-literal))
8509 (eq (skip-chars-backward " \t") 0)
8510
8511 ;; Check we've got nothing after this except comments and empty lines
8512 ;; joined by escaped EOLs.
8513 (skip-chars-forward " \t") ; always returns non-nil.
8514 (progn
8515 (while ; go over 1 block comment per iteration.
8516 (and
8517 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
8518 (goto-char (match-end 0))
8519 (cond
8520 ((looking-at c-block-comment-start-regexp)
8521 (and (forward-comment 1)
8522 (skip-chars-forward " \t"))) ; always returns non-nil
8523 ((looking-at c-line-comment-start-regexp)
8524 (end-of-line)
8525 nil)
8526 (t nil))))
8527 (eolp))
8528
8529 (goto-char pos)
8530 (progn (c-backward-syntactic-ws)
8531 (eq (point) pos))
8532
8533 ;; Check for one of the listed macros being before point.
8534 (or (not (eq (char-before) ?\)))
8535 (when (c-go-list-backward)
8536 (c-backward-syntactic-ws)
8537 t))
8538 (c-simple-skip-symbol-backward)
8539 (looking-at c-macro-with-semi-re)))))
8540
8541 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
8542
8543 \f
8544 ;; `c-guess-basic-syntax' and the functions that precedes it below
8545 ;; implements the main decision tree for determining the syntactic
8546 ;; analysis of the current line of code.
8547
8548 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8549 ;; auto newline analysis.
8550 (defvar c-auto-newline-analysis nil)
8551
8552 (defun c-brace-anchor-point (bracepos)
8553 ;; BRACEPOS is the position of a brace in a construct like "namespace
8554 ;; Bar {". Return the anchor point in this construct; this is the
8555 ;; earliest symbol on the brace's line which isn't earlier than
8556 ;; "namespace".
8557 ;;
8558 ;; Currently (2007-08-17), "like namespace" means "matches
8559 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8560 ;; or anything like that.
8561 (save-excursion
8562 (let ((boi (c-point 'boi bracepos)))
8563 (goto-char bracepos)
8564 (while (and (> (point) boi)
8565 (not (looking-at c-other-decl-block-key)))
8566 (c-backward-token-2))
8567 (if (> (point) boi) (point) boi))))
8568
8569 (defsubst c-add-syntax (symbol &rest args)
8570 ;; A simple function to prepend a new syntax element to
8571 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8572 ;; should always be dynamically bound but since we read it first
8573 ;; we'll fail properly anyway if this function is misused.
8574 (setq c-syntactic-context (cons (cons symbol args)
8575 c-syntactic-context)))
8576
8577 (defsubst c-append-syntax (symbol &rest args)
8578 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8579 ;; (Normally not necessary.)
8580 (setq c-syntactic-context (nconc c-syntactic-context
8581 (list (cons symbol args)))))
8582
8583 (defun c-add-stmt-syntax (syntax-symbol
8584 syntax-extra-args
8585 stop-at-boi-only
8586 containing-sexp
8587 paren-state)
8588 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8589 ;; needed with further syntax elements of the types `substatement',
8590 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8591 ;; `defun-block-intro'.
8592 ;;
8593 ;; Do the generic processing to anchor the given syntax symbol on
8594 ;; the preceding statement: Skip over any labels and containing
8595 ;; statements on the same line, and then search backward until we
8596 ;; find a statement or block start that begins at boi without a
8597 ;; label or comment.
8598 ;;
8599 ;; Point is assumed to be at the prospective anchor point for the
8600 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8601 ;; skip past open parens and containing statements. Most of the added
8602 ;; syntax elements will get the same anchor point - the exception is
8603 ;; for an anchor in a construct like "namespace"[*] - this is as early
8604 ;; as possible in the construct but on the same line as the {.
8605 ;;
8606 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8607 ;;
8608 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8609 ;; syntax symbol. They are appended after the anchor point.
8610 ;;
8611 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8612 ;; if the current statement starts there.
8613 ;;
8614 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8615 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8616 ;;
8617 ;; This function might do hidden buffer changes.
8618
8619 (if (= (point) (c-point 'boi))
8620 ;; This is by far the most common case, so let's give it special
8621 ;; treatment.
8622 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
8623
8624 (let ((syntax-last c-syntactic-context)
8625 (boi (c-point 'boi))
8626 ;; Set when we're on a label, so that we don't stop there.
8627 ;; FIXME: To be complete we should check if we're on a label
8628 ;; now at the start.
8629 on-label)
8630
8631 ;; Use point as the anchor point for "namespace", "extern", etc.
8632 (apply 'c-add-syntax syntax-symbol
8633 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
8634 (point) nil)
8635 syntax-extra-args)
8636
8637 ;; Loop while we have to back out of containing blocks.
8638 (while
8639 (and
8640 (catch 'back-up-block
8641
8642 ;; Loop while we have to back up statements.
8643 (while (or (/= (point) boi)
8644 on-label
8645 (looking-at c-comment-start-regexp))
8646
8647 ;; Skip past any comments that stands between the
8648 ;; statement start and boi.
8649 (let ((savepos (point)))
8650 (while (and (/= savepos boi)
8651 (c-backward-single-comment))
8652 (setq savepos (point)
8653 boi (c-point 'boi)))
8654 (goto-char savepos))
8655
8656 ;; Skip to the beginning of this statement or backward
8657 ;; another one.
8658 (let ((old-pos (point))
8659 (old-boi boi)
8660 (step-type (c-beginning-of-statement-1 containing-sexp)))
8661 (setq boi (c-point 'boi)
8662 on-label (eq step-type 'label))
8663
8664 (cond ((= (point) old-pos)
8665 ;; If we didn't move we're at the start of a block and
8666 ;; have to continue outside it.
8667 (throw 'back-up-block t))
8668
8669 ((and (eq step-type 'up)
8670 (>= (point) old-boi)
8671 (looking-at "else\\>[^_]")
8672 (save-excursion
8673 (goto-char old-pos)
8674 (looking-at "if\\>[^_]")))
8675 ;; Special case to avoid deeper and deeper indentation
8676 ;; of "else if" clauses.
8677 )
8678
8679 ((and (not stop-at-boi-only)
8680 (/= old-pos old-boi)
8681 (memq step-type '(up previous)))
8682 ;; If stop-at-boi-only is nil, we shouldn't back up
8683 ;; over previous or containing statements to try to
8684 ;; reach boi, so go back to the last position and
8685 ;; exit.
8686 (goto-char old-pos)
8687 (throw 'back-up-block nil))
8688
8689 (t
8690 (if (and (not stop-at-boi-only)
8691 (memq step-type '(up previous beginning)))
8692 ;; If we've moved into another statement then we
8693 ;; should no longer try to stop in the middle of a
8694 ;; line.
8695 (setq stop-at-boi-only t))
8696
8697 ;; Record this as a substatement if we skipped up one
8698 ;; level.
8699 (when (eq step-type 'up)
8700 (c-add-syntax 'substatement nil))))
8701 )))
8702
8703 containing-sexp)
8704
8705 ;; Now we have to go out of this block.
8706 (goto-char containing-sexp)
8707
8708 ;; Don't stop in the middle of a special brace list opener
8709 ;; like "({".
8710 (when c-special-brace-lists
8711 (let ((special-list (c-looking-at-special-brace-list)))
8712 (when (and special-list
8713 (< (car (car special-list)) (point)))
8714 (setq containing-sexp (car (car special-list)))
8715 (goto-char containing-sexp))))
8716
8717 (setq paren-state (c-whack-state-after containing-sexp paren-state)
8718 containing-sexp (c-most-enclosing-brace paren-state)
8719 boi (c-point 'boi))
8720
8721 ;; Analyze the construct in front of the block we've stepped out
8722 ;; from and add the right syntactic element for it.
8723 (let ((paren-pos (point))
8724 (paren-char (char-after))
8725 step-type)
8726
8727 (if (eq paren-char ?\()
8728 ;; Stepped out of a parenthesis block, so we're in an
8729 ;; expression now.
8730 (progn
8731 (when (/= paren-pos boi)
8732 (if (and c-recognize-paren-inexpr-blocks
8733 (progn
8734 (c-backward-syntactic-ws containing-sexp)
8735 (or (not (looking-at "\\>"))
8736 (not (c-on-identifier))))
8737 (save-excursion
8738 (goto-char (1+ paren-pos))
8739 (c-forward-syntactic-ws)
8740 (eq (char-after) ?{)))
8741 ;; Stepped out of an in-expression statement. This
8742 ;; syntactic element won't get an anchor pos.
8743 (c-add-syntax 'inexpr-statement)
8744
8745 ;; A parenthesis normally belongs to an arglist.
8746 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
8747
8748 (goto-char (max boi
8749 (if containing-sexp
8750 (1+ containing-sexp)
8751 (point-min))))
8752 (setq step-type 'same
8753 on-label nil))
8754
8755 ;; Stepped out of a brace block.
8756 (setq step-type (c-beginning-of-statement-1 containing-sexp)
8757 on-label (eq step-type 'label))
8758
8759 (if (and (eq step-type 'same)
8760 (/= paren-pos (point)))
8761 (let (inexpr)
8762 (cond
8763 ((save-excursion
8764 (goto-char paren-pos)
8765 (setq inexpr (c-looking-at-inexpr-block
8766 (c-safe-position containing-sexp paren-state)
8767 containing-sexp)))
8768 (c-add-syntax (if (eq (car inexpr) 'inlambda)
8769 'defun-block-intro
8770 'statement-block-intro)
8771 nil))
8772 ((looking-at c-other-decl-block-key)
8773 (c-add-syntax
8774 (cdr (assoc (match-string 1)
8775 c-other-decl-block-key-in-symbols-alist))
8776 (max (c-point 'boi paren-pos) (point))))
8777 (t (c-add-syntax 'defun-block-intro nil))))
8778
8779 (c-add-syntax 'statement-block-intro nil)))
8780
8781 (if (= paren-pos boi)
8782 ;; Always done if the open brace was at boi. The
8783 ;; c-beginning-of-statement-1 call above is necessary
8784 ;; anyway, to decide the type of block-intro to add.
8785 (goto-char paren-pos)
8786 (setq boi (c-point 'boi)))
8787 ))
8788
8789 ;; Fill in the current point as the anchor for all the symbols
8790 ;; added above.
8791 (let ((p c-syntactic-context) q)
8792 (while (not (eq p syntax-last))
8793 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8794 (while q
8795 (unless (car q)
8796 (setcar q (point)))
8797 (setq q (cdr q)))
8798 (setq p (cdr p))))
8799 )))
8800
8801 (defun c-add-class-syntax (symbol
8802 containing-decl-open
8803 containing-decl-start
8804 containing-decl-kwd
8805 paren-state)
8806 ;; The inclass and class-close syntactic symbols are added in
8807 ;; several places and some work is needed to fix everything.
8808 ;; Therefore it's collected here.
8809 ;;
8810 ;; This function might do hidden buffer changes.
8811 (goto-char containing-decl-open)
8812 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
8813 (progn
8814 (c-add-syntax symbol containing-decl-open)
8815 containing-decl-open)
8816 (goto-char containing-decl-start)
8817 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8818 ;; here, but we have to do like this for compatibility.
8819 (back-to-indentation)
8820 (c-add-syntax symbol (point))
8821 (if (and (c-keyword-member containing-decl-kwd
8822 'c-inexpr-class-kwds)
8823 (/= containing-decl-start (c-point 'boi containing-decl-start)))
8824 (c-add-syntax 'inexpr-class))
8825 (point)))
8826
8827 (defun c-guess-continued-construct (indent-point
8828 char-after-ip
8829 beg-of-same-or-containing-stmt
8830 containing-sexp
8831 paren-state)
8832 ;; This function contains the decision tree reached through both
8833 ;; cases 18 and 10. It's a continued statement or top level
8834 ;; construct of some kind.
8835 ;;
8836 ;; This function might do hidden buffer changes.
8837
8838 (let (special-brace-list placeholder)
8839 (goto-char indent-point)
8840 (skip-chars-forward " \t")
8841
8842 (cond
8843 ;; (CASE A removed.)
8844 ;; CASE B: open braces for class or brace-lists
8845 ((setq special-brace-list
8846 (or (and c-special-brace-lists
8847 (c-looking-at-special-brace-list))
8848 (eq char-after-ip ?{)))
8849
8850 (cond
8851 ;; CASE B.1: class-open
8852 ((save-excursion
8853 (and (eq (char-after) ?{)
8854 (c-looking-at-decl-block containing-sexp t)
8855 (setq beg-of-same-or-containing-stmt (point))))
8856 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
8857
8858 ;; CASE B.2: brace-list-open
8859 ((or (consp special-brace-list)
8860 (save-excursion
8861 (goto-char beg-of-same-or-containing-stmt)
8862 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8863 indent-point t t t)))
8864 ;; The most semantically accurate symbol here is
8865 ;; brace-list-open, but we normally report it simply as a
8866 ;; statement-cont. The reason is that one normally adjusts
8867 ;; brace-list-open for brace lists as top-level constructs,
8868 ;; and brace lists inside statements is a completely different
8869 ;; context. C.f. case 5A.3.
8870 (c-beginning-of-statement-1 containing-sexp)
8871 (c-add-stmt-syntax (if c-auto-newline-analysis
8872 ;; Turn off the dwim above when we're
8873 ;; analyzing the nature of the brace
8874 ;; for the auto newline feature.
8875 'brace-list-open
8876 'statement-cont)
8877 nil nil
8878 containing-sexp paren-state))
8879
8880 ;; CASE B.3: The body of a function declared inside a normal
8881 ;; block. Can occur e.g. in Pike and when using gcc
8882 ;; extensions, but watch out for macros followed by blocks.
8883 ;; C.f. cases E, 16F and 17G.
8884 ((and (not (c-at-statement-start-p))
8885 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8886 'same)
8887 (save-excursion
8888 (let ((c-recognize-typeless-decls nil))
8889 ;; Turn off recognition of constructs that lacks a
8890 ;; type in this case, since that's more likely to be
8891 ;; a macro followed by a block.
8892 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8893 (c-add-stmt-syntax 'defun-open nil t
8894 containing-sexp paren-state))
8895
8896 ;; CASE B.4: Continued statement with block open. The most
8897 ;; accurate analysis is perhaps `statement-cont' together with
8898 ;; `block-open' but we play DWIM and use `substatement-open'
8899 ;; instead. The rationale is that this typically is a macro
8900 ;; followed by a block which makes it very similar to a
8901 ;; statement with a substatement block.
8902 (t
8903 (c-add-stmt-syntax 'substatement-open nil nil
8904 containing-sexp paren-state))
8905 ))
8906
8907 ;; CASE C: iostream insertion or extraction operator
8908 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8909 (save-excursion
8910 (goto-char beg-of-same-or-containing-stmt)
8911 ;; If there is no preceding streamop in the statement
8912 ;; then indent this line as a normal statement-cont.
8913 (when (c-syntactic-re-search-forward
8914 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
8915 (c-add-syntax 'stream-op (c-point 'boi))
8916 t))))
8917
8918 ;; CASE E: In the "K&R region" of a function declared inside a
8919 ;; normal block. C.f. case B.3.
8920 ((and (save-excursion
8921 ;; Check that the next token is a '{'. This works as
8922 ;; long as no language that allows nested function
8923 ;; definitions allows stuff like member init lists, K&R
8924 ;; declarations or throws clauses there.
8925 ;;
8926 ;; Note that we do a forward search for something ahead
8927 ;; of the indentation line here. That's not good since
8928 ;; the user might not have typed it yet. Unfortunately
8929 ;; it's exceedingly tricky to recognize a function
8930 ;; prototype in a code block without resorting to this.
8931 (c-forward-syntactic-ws)
8932 (eq (char-after) ?{))
8933 (not (c-at-statement-start-p))
8934 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8935 'same)
8936 (save-excursion
8937 (let ((c-recognize-typeless-decls nil))
8938 ;; Turn off recognition of constructs that lacks a
8939 ;; type in this case, since that's more likely to be
8940 ;; a macro followed by a block.
8941 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8942 (c-add-stmt-syntax 'func-decl-cont nil t
8943 containing-sexp paren-state))
8944
8945 ;;CASE F: continued statement and the only preceding items are
8946 ;;annotations.
8947 ((and (c-major-mode-is 'java-mode)
8948 (setq placeholder (point))
8949 (c-beginning-of-statement-1)
8950 (progn
8951 (while (and (c-forward-annotation)
8952 (< (point) placeholder))
8953 (c-forward-syntactic-ws))
8954 t)
8955 (prog1
8956 (>= (point) placeholder)
8957 (goto-char placeholder)))
8958 (c-beginning-of-statement-1 containing-sexp)
8959 (c-add-syntax 'annotation-var-cont (point)))
8960
8961 ;; CASE G: a template list continuation?
8962 ;; Mostly a duplication of case 5D.3 to fix templates-19:
8963 ((and (c-major-mode-is 'c++-mode)
8964 (save-excursion
8965 (goto-char indent-point)
8966 (c-with-syntax-table c++-template-syntax-table
8967 (setq placeholder (c-up-list-backward)))
8968 (and placeholder
8969 (eq (char-after placeholder) ?<)
8970 (/= (char-before placeholder) ?<)
8971 (progn
8972 (goto-char (1+ placeholder))
8973 (not (looking-at c-<-op-cont-regexp))))))
8974 (c-with-syntax-table c++-template-syntax-table
8975 (goto-char placeholder)
8976 (c-beginning-of-statement-1 containing-sexp t)
8977 (if (save-excursion
8978 (c-backward-syntactic-ws containing-sexp)
8979 (eq (char-before) ?<))
8980 ;; In a nested template arglist.
8981 (progn
8982 (goto-char placeholder)
8983 (c-syntactic-skip-backward "^,;" containing-sexp t)
8984 (c-forward-syntactic-ws))
8985 (back-to-indentation)))
8986 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8987 ;; template aware.
8988 (c-add-syntax 'template-args-cont (point) placeholder))
8989
8990 ;; CASE D: continued statement.
8991 (t
8992 (c-beginning-of-statement-1 containing-sexp)
8993 (c-add-stmt-syntax 'statement-cont nil nil
8994 containing-sexp paren-state))
8995 )))
8996
8997 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
8998 ;; 2005/11/29).
8999 ;;;###autoload
9000 (defun c-guess-basic-syntax ()
9001 "Return the syntactic context of the current line."
9002 (save-excursion
9003 (beginning-of-line)
9004 (c-save-buffer-state
9005 ((indent-point (point))
9006 (case-fold-search nil)
9007 ;; A whole ugly bunch of various temporary variables. Have
9008 ;; to declare them here since it's not possible to declare
9009 ;; a variable with only the scope of a cond test and the
9010 ;; following result clauses, and most of this function is a
9011 ;; single gigantic cond. :P
9012 literal char-before-ip before-ws-ip char-after-ip macro-start
9013 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
9014 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
9015 containing-<
9016 ;; The following record some positions for the containing
9017 ;; declaration block if we're directly within one:
9018 ;; `containing-decl-open' is the position of the open
9019 ;; brace. `containing-decl-start' is the start of the
9020 ;; declaration. `containing-decl-kwd' is the keyword
9021 ;; symbol of the keyword that tells what kind of block it
9022 ;; is.
9023 containing-decl-open
9024 containing-decl-start
9025 containing-decl-kwd
9026 ;; The open paren of the closest surrounding sexp or nil if
9027 ;; there is none.
9028 containing-sexp
9029 ;; The position after the closest preceding brace sexp
9030 ;; (nested sexps are ignored), or the position after
9031 ;; `containing-sexp' if there is none, or (point-min) if
9032 ;; `containing-sexp' is nil.
9033 lim
9034 ;; The paren state outside `containing-sexp', or at
9035 ;; `indent-point' if `containing-sexp' is nil.
9036 (paren-state (c-parse-state))
9037 ;; There's always at most one syntactic element which got
9038 ;; an anchor pos. It's stored in syntactic-relpos.
9039 syntactic-relpos
9040 (c-stmt-delim-chars c-stmt-delim-chars))
9041
9042 ;; Check if we're directly inside an enclosing declaration
9043 ;; level block.
9044 (when (and (setq containing-sexp
9045 (c-most-enclosing-brace paren-state))
9046 (progn
9047 (goto-char containing-sexp)
9048 (eq (char-after) ?{))
9049 (setq placeholder
9050 (c-looking-at-decl-block
9051 (c-most-enclosing-brace paren-state
9052 containing-sexp)
9053 t)))
9054 (setq containing-decl-open containing-sexp
9055 containing-decl-start (point)
9056 containing-sexp nil)
9057 (goto-char placeholder)
9058 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
9059 (c-keyword-sym (match-string 1)))))
9060
9061 ;; Init some position variables.
9062 (if c-state-cache
9063 (progn
9064 (setq containing-sexp (car paren-state)
9065 paren-state (cdr paren-state))
9066 (if (consp containing-sexp)
9067 (progn
9068 (setq lim (cdr containing-sexp))
9069 (if (cdr c-state-cache)
9070 ;; Ignore balanced paren. The next entry
9071 ;; can't be another one.
9072 (setq containing-sexp (car (cdr c-state-cache))
9073 paren-state (cdr paren-state))
9074 ;; If there is no surrounding open paren then
9075 ;; put the last balanced pair back on paren-state.
9076 (setq paren-state (cons containing-sexp paren-state)
9077 containing-sexp nil)))
9078 (setq lim (1+ containing-sexp))))
9079 (setq lim (point-min)))
9080
9081 ;; If we're in a parenthesis list then ',' delimits the
9082 ;; "statements" rather than being an operator (with the
9083 ;; exception of the "for" clause). This difference is
9084 ;; typically only noticeable when statements are used in macro
9085 ;; arglists.
9086 (when (and containing-sexp
9087 (eq (char-after containing-sexp) ?\())
9088 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
9089 ;; cache char before and after indent point, and move point to
9090 ;; the most likely position to perform the majority of tests
9091 (goto-char indent-point)
9092 (c-backward-syntactic-ws lim)
9093 (setq before-ws-ip (point)
9094 char-before-ip (char-before))
9095 (goto-char indent-point)
9096 (skip-chars-forward " \t")
9097 (setq char-after-ip (char-after))
9098
9099 ;; are we in a literal?
9100 (setq literal (c-in-literal lim))
9101
9102 ;; now figure out syntactic qualities of the current line
9103 (cond
9104
9105 ;; CASE 1: in a string.
9106 ((eq literal 'string)
9107 (c-add-syntax 'string (c-point 'bopl)))
9108
9109 ;; CASE 2: in a C or C++ style comment.
9110 ((and (memq literal '(c c++))
9111 ;; This is a kludge for XEmacs where we use
9112 ;; `buffer-syntactic-context', which doesn't correctly
9113 ;; recognize "\*/" to end a block comment.
9114 ;; `parse-partial-sexp' which is used by
9115 ;; `c-literal-limits' will however do that in most
9116 ;; versions, which results in that we get nil from
9117 ;; `c-literal-limits' even when `c-in-literal' claims
9118 ;; we're inside a comment.
9119 (setq placeholder (c-literal-limits lim)))
9120 (c-add-syntax literal (car placeholder)))
9121
9122 ;; CASE 3: in a cpp preprocessor macro continuation.
9123 ((and (save-excursion
9124 (when (c-beginning-of-macro)
9125 (setq macro-start (point))))
9126 (/= macro-start (c-point 'boi))
9127 (progn
9128 (setq tmpsymbol 'cpp-macro-cont)
9129 (or (not c-syntactic-indentation-in-macros)
9130 (save-excursion
9131 (goto-char macro-start)
9132 ;; If at the beginning of the body of a #define
9133 ;; directive then analyze as cpp-define-intro
9134 ;; only. Go on with the syntactic analysis
9135 ;; otherwise. in-macro-expr is set if we're in a
9136 ;; cpp expression, i.e. before the #define body
9137 ;; or anywhere in a non-#define directive.
9138 (if (c-forward-to-cpp-define-body)
9139 (let ((indent-boi (c-point 'boi indent-point)))
9140 (setq in-macro-expr (> (point) indent-boi)
9141 tmpsymbol 'cpp-define-intro)
9142 (= (point) indent-boi))
9143 (setq in-macro-expr t)
9144 nil)))))
9145 (c-add-syntax tmpsymbol macro-start)
9146 (setq macro-start nil))
9147
9148 ;; CASE 11: an else clause?
9149 ((looking-at "else\\>[^_]")
9150 (c-beginning-of-statement-1 containing-sexp)
9151 (c-add-stmt-syntax 'else-clause nil t
9152 containing-sexp paren-state))
9153
9154 ;; CASE 12: while closure of a do/while construct?
9155 ((and (looking-at "while\\>[^_]")
9156 (save-excursion
9157 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
9158 'beginning)
9159 (setq placeholder (point)))))
9160 (goto-char placeholder)
9161 (c-add-stmt-syntax 'do-while-closure nil t
9162 containing-sexp paren-state))
9163
9164 ;; CASE 13: A catch or finally clause? This case is simpler
9165 ;; than if-else and do-while, because a block is required
9166 ;; after every try, catch and finally.
9167 ((save-excursion
9168 (and (cond ((c-major-mode-is 'c++-mode)
9169 (looking-at "catch\\>[^_]"))
9170 ((c-major-mode-is 'java-mode)
9171 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
9172 (and (c-safe (c-backward-syntactic-ws)
9173 (c-backward-sexp)
9174 t)
9175 (eq (char-after) ?{)
9176 (c-safe (c-backward-syntactic-ws)
9177 (c-backward-sexp)
9178 t)
9179 (if (eq (char-after) ?\()
9180 (c-safe (c-backward-sexp) t)
9181 t))
9182 (looking-at "\\(try\\|catch\\)\\>[^_]")
9183 (setq placeholder (point))))
9184 (goto-char placeholder)
9185 (c-add-stmt-syntax 'catch-clause nil t
9186 containing-sexp paren-state))
9187
9188 ;; CASE 18: A substatement we can recognize by keyword.
9189 ((save-excursion
9190 (and c-opt-block-stmt-key
9191 (not (eq char-before-ip ?\;))
9192 (not (c-at-vsemi-p before-ws-ip))
9193 (not (memq char-after-ip '(?\) ?\] ?,)))
9194 (or (not (eq char-before-ip ?}))
9195 (c-looking-at-inexpr-block-backward c-state-cache))
9196 (> (point)
9197 (progn
9198 ;; Ought to cache the result from the
9199 ;; c-beginning-of-statement-1 calls here.
9200 (setq placeholder (point))
9201 (while (eq (setq step-type
9202 (c-beginning-of-statement-1 lim))
9203 'label))
9204 (if (eq step-type 'previous)
9205 (goto-char placeholder)
9206 (setq placeholder (point))
9207 (if (and (eq step-type 'same)
9208 (not (looking-at c-opt-block-stmt-key)))
9209 ;; Step up to the containing statement if we
9210 ;; stayed in the same one.
9211 (let (step)
9212 (while (eq
9213 (setq step
9214 (c-beginning-of-statement-1 lim))
9215 'label))
9216 (if (eq step 'up)
9217 (setq placeholder (point))
9218 ;; There was no containing statement after all.
9219 (goto-char placeholder)))))
9220 placeholder))
9221 (if (looking-at c-block-stmt-2-key)
9222 ;; Require a parenthesis after these keywords.
9223 ;; Necessary to catch e.g. synchronized in Java,
9224 ;; which can be used both as statement and
9225 ;; modifier.
9226 (and (zerop (c-forward-token-2 1 nil))
9227 (eq (char-after) ?\())
9228 (looking-at c-opt-block-stmt-key))))
9229
9230 (if (eq step-type 'up)
9231 ;; CASE 18A: Simple substatement.
9232 (progn
9233 (goto-char placeholder)
9234 (cond
9235 ((eq char-after-ip ?{)
9236 (c-add-stmt-syntax 'substatement-open nil nil
9237 containing-sexp paren-state))
9238 ((save-excursion
9239 (goto-char indent-point)
9240 (back-to-indentation)
9241 (c-forward-label))
9242 (c-add-stmt-syntax 'substatement-label nil nil
9243 containing-sexp paren-state))
9244 (t
9245 (c-add-stmt-syntax 'substatement nil nil
9246 containing-sexp paren-state))))
9247
9248 ;; CASE 18B: Some other substatement. This is shared
9249 ;; with case 10.
9250 (c-guess-continued-construct indent-point
9251 char-after-ip
9252 placeholder
9253 lim
9254 paren-state)))
9255
9256 ;; CASE 14: A case or default label
9257 ((looking-at c-label-kwds-regexp)
9258 (if containing-sexp
9259 (progn
9260 (goto-char containing-sexp)
9261 (setq lim (c-most-enclosing-brace c-state-cache
9262 containing-sexp))
9263 (c-backward-to-block-anchor lim)
9264 (c-add-stmt-syntax 'case-label nil t lim paren-state))
9265 ;; Got a bogus label at the top level. In lack of better
9266 ;; alternatives, anchor it on (point-min).
9267 (c-add-syntax 'case-label (point-min))))
9268
9269 ;; CASE 15: any other label
9270 ((save-excursion
9271 (back-to-indentation)
9272 (and (not (looking-at c-syntactic-ws-start))
9273 (c-forward-label)))
9274 (cond (containing-decl-open
9275 (setq placeholder (c-add-class-syntax 'inclass
9276 containing-decl-open
9277 containing-decl-start
9278 containing-decl-kwd
9279 paren-state))
9280 ;; Append access-label with the same anchor point as
9281 ;; inclass gets.
9282 (c-append-syntax 'access-label placeholder))
9283
9284 (containing-sexp
9285 (goto-char containing-sexp)
9286 (setq lim (c-most-enclosing-brace c-state-cache
9287 containing-sexp))
9288 (save-excursion
9289 (setq tmpsymbol
9290 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
9291 (looking-at "switch\\>[^_]"))
9292 ;; If the surrounding statement is a switch then
9293 ;; let's analyze all labels as switch labels, so
9294 ;; that they get lined up consistently.
9295 'case-label
9296 'label)))
9297 (c-backward-to-block-anchor lim)
9298 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
9299
9300 (t
9301 ;; A label on the top level. Treat it as a class
9302 ;; context. (point-min) is the closest we get to the
9303 ;; class open brace.
9304 (c-add-syntax 'access-label (point-min)))))
9305
9306 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9307 ;; 17E.
9308 ((setq placeholder (c-looking-at-inexpr-block
9309 (c-safe-position containing-sexp paren-state)
9310 containing-sexp
9311 ;; Have to turn on the heuristics after
9312 ;; the point even though it doesn't work
9313 ;; very well. C.f. test case class-16.pike.
9314 t))
9315 (setq tmpsymbol (assq (car placeholder)
9316 '((inexpr-class . class-open)
9317 (inexpr-statement . block-open))))
9318 (if tmpsymbol
9319 ;; It's a statement block or an anonymous class.
9320 (setq tmpsymbol (cdr tmpsymbol))
9321 ;; It's a Pike lambda. Check whether we are between the
9322 ;; lambda keyword and the argument list or at the defun
9323 ;; opener.
9324 (setq tmpsymbol (if (eq char-after-ip ?{)
9325 'inline-open
9326 'lambda-intro-cont)))
9327 (goto-char (cdr placeholder))
9328 (back-to-indentation)
9329 (c-add-stmt-syntax tmpsymbol nil t
9330 (c-most-enclosing-brace c-state-cache (point))
9331 paren-state)
9332 (unless (eq (point) (cdr placeholder))
9333 (c-add-syntax (car placeholder))))
9334
9335 ;; CASE 5: Line is inside a declaration level block or at top level.
9336 ((or containing-decl-open (null containing-sexp))
9337 (cond
9338
9339 ;; CASE 5A: we are looking at a defun, brace list, class,
9340 ;; or inline-inclass method opening brace
9341 ((setq special-brace-list
9342 (or (and c-special-brace-lists
9343 (c-looking-at-special-brace-list))
9344 (eq char-after-ip ?{)))
9345 (cond
9346
9347 ;; CASE 5A.1: Non-class declaration block open.
9348 ((save-excursion
9349 (let (tmp)
9350 (and (eq char-after-ip ?{)
9351 (setq tmp (c-looking-at-decl-block containing-sexp t))
9352 (progn
9353 (setq placeholder (point))
9354 (goto-char tmp)
9355 (looking-at c-symbol-key))
9356 (c-keyword-member
9357 (c-keyword-sym (setq keyword (match-string 0)))
9358 'c-other-block-decl-kwds))))
9359 (goto-char placeholder)
9360 (c-add-stmt-syntax
9361 (if (string-equal keyword "extern")
9362 ;; Special case for extern-lang-open.
9363 'extern-lang-open
9364 (intern (concat keyword "-open")))
9365 nil t containing-sexp paren-state))
9366
9367 ;; CASE 5A.2: we are looking at a class opening brace
9368 ((save-excursion
9369 (goto-char indent-point)
9370 (skip-chars-forward " \t")
9371 (and (eq (char-after) ?{)
9372 (c-looking-at-decl-block containing-sexp t)
9373 (setq placeholder (point))))
9374 (c-add-syntax 'class-open placeholder))
9375
9376 ;; CASE 5A.3: brace list open
9377 ((save-excursion
9378 (c-beginning-of-decl-1 lim)
9379 (while (looking-at c-specifier-key)
9380 (goto-char (match-end 1))
9381 (c-forward-syntactic-ws indent-point))
9382 (setq placeholder (c-point 'boi))
9383 (or (consp special-brace-list)
9384 (and (or (save-excursion
9385 (goto-char indent-point)
9386 (setq tmpsymbol nil)
9387 (while (and (> (point) placeholder)
9388 (zerop (c-backward-token-2 1 t))
9389 (/= (char-after) ?=))
9390 (and c-opt-inexpr-brace-list-key
9391 (not tmpsymbol)
9392 (looking-at c-opt-inexpr-brace-list-key)
9393 (setq tmpsymbol 'topmost-intro-cont)))
9394 (eq (char-after) ?=))
9395 (looking-at c-brace-list-key))
9396 (save-excursion
9397 (while (and (< (point) indent-point)
9398 (zerop (c-forward-token-2 1 t))
9399 (not (memq (char-after) '(?\; ?\()))))
9400 (not (memq (char-after) '(?\; ?\()))
9401 ))))
9402 (if (and (not c-auto-newline-analysis)
9403 (c-major-mode-is 'java-mode)
9404 (eq tmpsymbol 'topmost-intro-cont))
9405 ;; We're in Java and have found that the open brace
9406 ;; belongs to a "new Foo[]" initialization list,
9407 ;; which means the brace list is part of an
9408 ;; expression and not a top level definition. We
9409 ;; therefore treat it as any topmost continuation
9410 ;; even though the semantically correct symbol still
9411 ;; is brace-list-open, on the same grounds as in
9412 ;; case B.2.
9413 (progn
9414 (c-beginning-of-statement-1 lim)
9415 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9416 (c-add-syntax 'brace-list-open placeholder)))
9417
9418 ;; CASE 5A.4: inline defun open
9419 ((and containing-decl-open
9420 (not (c-keyword-member containing-decl-kwd
9421 'c-other-block-decl-kwds)))
9422 (c-add-syntax 'inline-open)
9423 (c-add-class-syntax 'inclass
9424 containing-decl-open
9425 containing-decl-start
9426 containing-decl-kwd
9427 paren-state))
9428
9429 ;; CASE 5A.5: ordinary defun open
9430 (t
9431 (save-excursion
9432 (c-beginning-of-decl-1 lim)
9433 (while (looking-at c-specifier-key)
9434 (goto-char (match-end 1))
9435 (c-forward-syntactic-ws indent-point))
9436 (c-add-syntax 'defun-open (c-point 'boi))
9437 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9438 ;; 2007-11-09)
9439 ))))
9440
9441 ;; CASE 5B: After a function header but before the body (or
9442 ;; the ending semicolon if there's no body).
9443 ((save-excursion
9444 (when (setq placeholder (c-just-after-func-arglist-p lim))
9445 (setq tmp-pos (point))))
9446 (cond
9447
9448 ;; CASE 5B.1: Member init list.
9449 ((eq (char-after tmp-pos) ?:)
9450 (if (or (> tmp-pos indent-point)
9451 (= (c-point 'bosws) (1+ tmp-pos)))
9452 (progn
9453 ;; There is no preceding member init clause.
9454 ;; Indent relative to the beginning of indentation
9455 ;; for the topmost-intro line that contains the
9456 ;; prototype's open paren.
9457 (goto-char placeholder)
9458 (c-add-syntax 'member-init-intro (c-point 'boi)))
9459 ;; Indent relative to the first member init clause.
9460 (goto-char (1+ tmp-pos))
9461 (c-forward-syntactic-ws)
9462 (c-add-syntax 'member-init-cont (point))))
9463
9464 ;; CASE 5B.2: K&R arg decl intro
9465 ((and c-recognize-knr-p
9466 (c-in-knr-argdecl lim))
9467 (c-beginning-of-statement-1 lim)
9468 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9469 (if containing-decl-open
9470 (c-add-class-syntax 'inclass
9471 containing-decl-open
9472 containing-decl-start
9473 containing-decl-kwd
9474 paren-state)))
9475
9476 ;; CASE 5B.4: Nether region after a C++ or Java func
9477 ;; decl, which could include a `throws' declaration.
9478 (t
9479 (c-beginning-of-statement-1 lim)
9480 (c-add-syntax 'func-decl-cont (c-point 'boi))
9481 )))
9482
9483 ;; CASE 5C: inheritance line. could be first inheritance
9484 ;; line, or continuation of a multiple inheritance
9485 ((or (and (c-major-mode-is 'c++-mode)
9486 (progn
9487 (when (eq char-after-ip ?,)
9488 (skip-chars-forward " \t")
9489 (forward-char))
9490 (looking-at c-opt-postfix-decl-spec-key)))
9491 (and (or (eq char-before-ip ?:)
9492 ;; watch out for scope operator
9493 (save-excursion
9494 (and (eq char-after-ip ?:)
9495 (c-safe (forward-char 1) t)
9496 (not (eq (char-after) ?:))
9497 )))
9498 (save-excursion
9499 (c-backward-syntactic-ws lim)
9500 (if (eq char-before-ip ?:)
9501 (progn
9502 (forward-char -1)
9503 (c-backward-syntactic-ws lim)))
9504 (back-to-indentation)
9505 (looking-at c-class-key)))
9506 ;; for Java
9507 (and (c-major-mode-is 'java-mode)
9508 (let ((fence (save-excursion
9509 (c-beginning-of-statement-1 lim)
9510 (point)))
9511 cont done)
9512 (save-excursion
9513 (while (not done)
9514 (cond ((looking-at c-opt-postfix-decl-spec-key)
9515 (setq injava-inher (cons cont (point))
9516 done t))
9517 ((or (not (c-safe (c-forward-sexp -1) t))
9518 (<= (point) fence))
9519 (setq done t))
9520 )
9521 (setq cont t)))
9522 injava-inher)
9523 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9524 (point)))
9525 ))
9526 (cond
9527
9528 ;; CASE 5C.1: non-hanging colon on an inher intro
9529 ((eq char-after-ip ?:)
9530 (c-beginning-of-statement-1 lim)
9531 (c-add-syntax 'inher-intro (c-point 'boi))
9532 ;; don't add inclass symbol since relative point already
9533 ;; contains any class offset
9534 )
9535
9536 ;; CASE 5C.2: hanging colon on an inher intro
9537 ((eq char-before-ip ?:)
9538 (c-beginning-of-statement-1 lim)
9539 (c-add-syntax 'inher-intro (c-point 'boi))
9540 (if containing-decl-open
9541 (c-add-class-syntax 'inclass
9542 containing-decl-open
9543 containing-decl-start
9544 containing-decl-kwd
9545 paren-state)))
9546
9547 ;; CASE 5C.3: in a Java implements/extends
9548 (injava-inher
9549 (let ((where (cdr injava-inher))
9550 (cont (car injava-inher)))
9551 (goto-char where)
9552 (cond ((looking-at "throws\\>[^_]")
9553 (c-add-syntax 'func-decl-cont
9554 (progn (c-beginning-of-statement-1 lim)
9555 (c-point 'boi))))
9556 (cont (c-add-syntax 'inher-cont where))
9557 (t (c-add-syntax 'inher-intro
9558 (progn (goto-char (cdr injava-inher))
9559 (c-beginning-of-statement-1 lim)
9560 (point))))
9561 )))
9562
9563 ;; CASE 5C.4: a continued inheritance line
9564 (t
9565 (c-beginning-of-inheritance-list lim)
9566 (c-add-syntax 'inher-cont (point))
9567 ;; don't add inclass symbol since relative point already
9568 ;; contains any class offset
9569 )))
9570
9571 ;; CASE 5D: this could be a top-level initialization, a
9572 ;; member init list continuation, or a template argument
9573 ;; list continuation.
9574 ((save-excursion
9575 ;; Note: We use the fact that lim is always after any
9576 ;; preceding brace sexp.
9577 (if c-recognize-<>-arglists
9578 (while (and
9579 (progn
9580 (c-syntactic-skip-backward "^;,=<>" lim t)
9581 (> (point) lim))
9582 (or
9583 (when c-overloadable-operators-regexp
9584 (when (setq placeholder (c-after-special-operator-id lim))
9585 (goto-char placeholder)
9586 t))
9587 (cond
9588 ((eq (char-before) ?>)
9589 (or (c-backward-<>-arglist nil lim)
9590 (backward-char))
9591 t)
9592 ((eq (char-before) ?<)
9593 (backward-char)
9594 (if (save-excursion
9595 (c-forward-<>-arglist nil))
9596 (progn (forward-char)
9597 nil)
9598 t))
9599 (t nil)))))
9600 ;; NB: No c-after-special-operator-id stuff in this
9601 ;; clause - we assume only C++ needs it.
9602 (c-syntactic-skip-backward "^;,=" lim t))
9603 (memq (char-before) '(?, ?= ?<)))
9604 (cond
9605
9606 ;; CASE 5D.3: perhaps a template list continuation?
9607 ((and (c-major-mode-is 'c++-mode)
9608 (save-excursion
9609 (save-restriction
9610 (c-with-syntax-table c++-template-syntax-table
9611 (goto-char indent-point)
9612 (setq placeholder (c-up-list-backward))
9613 (and placeholder
9614 (eq (char-after placeholder) ?<))))))
9615 (c-with-syntax-table c++-template-syntax-table
9616 (goto-char placeholder)
9617 (c-beginning-of-statement-1 lim t)
9618 (if (save-excursion
9619 (c-backward-syntactic-ws lim)
9620 (eq (char-before) ?<))
9621 ;; In a nested template arglist.
9622 (progn
9623 (goto-char placeholder)
9624 (c-syntactic-skip-backward "^,;" lim t)
9625 (c-forward-syntactic-ws))
9626 (back-to-indentation)))
9627 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9628 ;; template aware.
9629 (c-add-syntax 'template-args-cont (point) placeholder))
9630
9631 ;; CASE 5D.4: perhaps a multiple inheritance line?
9632 ((and (c-major-mode-is 'c++-mode)
9633 (save-excursion
9634 (c-beginning-of-statement-1 lim)
9635 (setq placeholder (point))
9636 (if (looking-at "static\\>[^_]")
9637 (c-forward-token-2 1 nil indent-point))
9638 (and (looking-at c-class-key)
9639 (zerop (c-forward-token-2 2 nil indent-point))
9640 (if (eq (char-after) ?<)
9641 (c-with-syntax-table c++-template-syntax-table
9642 (zerop (c-forward-token-2 1 t indent-point)))
9643 t)
9644 (eq (char-after) ?:))))
9645 (goto-char placeholder)
9646 (c-add-syntax 'inher-cont (c-point 'boi)))
9647
9648 ;; CASE 5D.5: Continuation of the "expression part" of a
9649 ;; top level construct. Or, perhaps, an unrecognized construct.
9650 (t
9651 (while (and (setq placeholder (point))
9652 (eq (car (c-beginning-of-decl-1 containing-sexp))
9653 'same)
9654 (save-excursion
9655 (c-backward-syntactic-ws)
9656 (eq (char-before) ?}))
9657 (< (point) placeholder)))
9658 (c-add-stmt-syntax
9659 (cond
9660 ((eq (point) placeholder) 'statement) ; unrecognized construct
9661 ;; A preceding comma at the top level means that a
9662 ;; new variable declaration starts here. Use
9663 ;; topmost-intro-cont for it, for consistency with
9664 ;; the first variable declaration. C.f. case 5N.
9665 ((eq char-before-ip ?,) 'topmost-intro-cont)
9666 (t 'statement-cont))
9667 nil nil containing-sexp paren-state))
9668 ))
9669
9670 ;; CASE 5F: Close of a non-class declaration level block.
9671 ((and (eq char-after-ip ?})
9672 (c-keyword-member containing-decl-kwd
9673 'c-other-block-decl-kwds))
9674 ;; This is inconsistent: Should use `containing-decl-open'
9675 ;; here if it's at boi, like in case 5J.
9676 (goto-char containing-decl-start)
9677 (c-add-stmt-syntax
9678 (if (string-equal (symbol-name containing-decl-kwd) "extern")
9679 ;; Special case for compatibility with the
9680 ;; extern-lang syntactic symbols.
9681 'extern-lang-close
9682 (intern (concat (symbol-name containing-decl-kwd)
9683 "-close")))
9684 nil t
9685 (c-most-enclosing-brace paren-state (point))
9686 paren-state))
9687
9688 ;; CASE 5G: we are looking at the brace which closes the
9689 ;; enclosing nested class decl
9690 ((and containing-sexp
9691 (eq char-after-ip ?})
9692 (eq containing-decl-open containing-sexp))
9693 (c-add-class-syntax 'class-close
9694 containing-decl-open
9695 containing-decl-start
9696 containing-decl-kwd
9697 paren-state))
9698
9699 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9700 ((and c-recognize-knr-p
9701 (not containing-sexp) ; can't be knr inside braces.
9702 (not (eq char-before-ip ?}))
9703 (save-excursion
9704 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
9705 (and placeholder
9706 ;; Do an extra check to avoid tripping up on
9707 ;; statements that occur in invalid contexts
9708 ;; (e.g. in macro bodies where we don't really
9709 ;; know the context of what we're looking at).
9710 (not (and c-opt-block-stmt-key
9711 (looking-at c-opt-block-stmt-key)))))
9712 (< placeholder indent-point))
9713 (goto-char placeholder)
9714 (c-add-syntax 'knr-argdecl (point)))
9715
9716 ;; CASE 5I: ObjC method definition.
9717 ((and c-opt-method-key
9718 (looking-at c-opt-method-key))
9719 (c-beginning-of-statement-1 nil t)
9720 (if (= (point) indent-point)
9721 ;; Handle the case when it's the first (non-comment)
9722 ;; thing in the buffer. Can't look for a 'same return
9723 ;; value from cbos1 since ObjC directives currently
9724 ;; aren't recognized fully, so that we get 'same
9725 ;; instead of 'previous if it moved over a preceding
9726 ;; directive.
9727 (goto-char (point-min)))
9728 (c-add-syntax 'objc-method-intro (c-point 'boi)))
9729
9730 ;; CASE 5P: AWK pattern or function or continuation
9731 ;; thereof.
9732 ((c-major-mode-is 'awk-mode)
9733 (setq placeholder (point))
9734 (c-add-stmt-syntax
9735 (if (and (eq (c-beginning-of-statement-1) 'same)
9736 (/= (point) placeholder))
9737 'topmost-intro-cont
9738 'topmost-intro)
9739 nil nil
9740 containing-sexp paren-state))
9741
9742 ;; CASE 5N: At a variable declaration that follows a class
9743 ;; definition or some other block declaration that doesn't
9744 ;; end at the closing '}'. C.f. case 5D.5.
9745 ((progn
9746 (c-backward-syntactic-ws lim)
9747 (and (eq (char-before) ?})
9748 (save-excursion
9749 (let ((start (point)))
9750 (if (and c-state-cache
9751 (consp (car c-state-cache))
9752 (eq (cdar c-state-cache) (point)))
9753 ;; Speed up the backward search a bit.
9754 (goto-char (caar c-state-cache)))
9755 (c-beginning-of-decl-1 containing-sexp)
9756 (setq placeholder (point))
9757 (if (= start (point))
9758 ;; The '}' is unbalanced.
9759 nil
9760 (c-end-of-decl-1)
9761 (>= (point) indent-point))))))
9762 (goto-char placeholder)
9763 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9764 containing-sexp paren-state))
9765
9766 ;; NOTE: The point is at the end of the previous token here.
9767
9768 ;; CASE 5J: we are at the topmost level, make
9769 ;; sure we skip back past any access specifiers
9770 ((and
9771 ;; A macro continuation line is never at top level.
9772 (not (and macro-start
9773 (> indent-point macro-start)))
9774 (save-excursion
9775 (setq placeholder (point))
9776 (or (memq char-before-ip '(?\; ?{ ?} nil))
9777 (c-at-vsemi-p before-ws-ip)
9778 (when (and (eq char-before-ip ?:)
9779 (eq (c-beginning-of-statement-1 lim)
9780 'label))
9781 (c-backward-syntactic-ws lim)
9782 (setq placeholder (point)))
9783 (and (c-major-mode-is 'objc-mode)
9784 (catch 'not-in-directive
9785 (c-beginning-of-statement-1 lim)
9786 (setq placeholder (point))
9787 (while (and (c-forward-objc-directive)
9788 (< (point) indent-point))
9789 (c-forward-syntactic-ws)
9790 (if (>= (point) indent-point)
9791 (throw 'not-in-directive t))
9792 (setq placeholder (point)))
9793 nil)))))
9794 ;; For historic reasons we anchor at bol of the last
9795 ;; line of the previous declaration. That's clearly
9796 ;; highly bogus and useless, and it makes our lives hard
9797 ;; to remain compatible. :P
9798 (goto-char placeholder)
9799 (c-add-syntax 'topmost-intro (c-point 'bol))
9800 (if containing-decl-open
9801 (if (c-keyword-member containing-decl-kwd
9802 'c-other-block-decl-kwds)
9803 (progn
9804 (goto-char (c-brace-anchor-point containing-decl-open))
9805 (c-add-stmt-syntax
9806 (if (string-equal (symbol-name containing-decl-kwd)
9807 "extern")
9808 ;; Special case for compatibility with the
9809 ;; extern-lang syntactic symbols.
9810 'inextern-lang
9811 (intern (concat "in"
9812 (symbol-name containing-decl-kwd))))
9813 nil t
9814 (c-most-enclosing-brace paren-state (point))
9815 paren-state))
9816 (c-add-class-syntax 'inclass
9817 containing-decl-open
9818 containing-decl-start
9819 containing-decl-kwd
9820 paren-state)))
9821 (when (and c-syntactic-indentation-in-macros
9822 macro-start
9823 (/= macro-start (c-point 'boi indent-point)))
9824 (c-add-syntax 'cpp-define-intro)
9825 (setq macro-start nil)))
9826
9827 ;; CASE 5K: we are at an ObjC method definition
9828 ;; continuation line.
9829 ((and c-opt-method-key
9830 (save-excursion
9831 (c-beginning-of-statement-1 lim)
9832 (beginning-of-line)
9833 (when (looking-at c-opt-method-key)
9834 (setq placeholder (point)))))
9835 (c-add-syntax 'objc-method-args-cont placeholder))
9836
9837 ;; CASE 5L: we are at the first argument of a template
9838 ;; arglist that begins on the previous line.
9839 ((and c-recognize-<>-arglists
9840 (eq (char-before) ?<)
9841 (not (and c-overloadable-operators-regexp
9842 (c-after-special-operator-id lim))))
9843 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9844 (c-add-syntax 'template-args-cont (c-point 'boi)))
9845
9846 ;; CASE 5Q: we are at a statement within a macro.
9847 (macro-start
9848 (c-beginning-of-statement-1 containing-sexp)
9849 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
9850
9851 ;;CASE 5N: We are at a topmost continuation line and the only
9852 ;;preceding items are annotations.
9853 ((and (c-major-mode-is 'java-mode)
9854 (setq placeholder (point))
9855 (c-beginning-of-statement-1)
9856 (progn
9857 (while (and (c-forward-annotation))
9858 (c-forward-syntactic-ws))
9859 t)
9860 (prog1
9861 (>= (point) placeholder)
9862 (goto-char placeholder)))
9863 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
9864
9865 ;; CASE 5M: we are at a topmost continuation line
9866 (t
9867 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9868 (when (c-major-mode-is 'objc-mode)
9869 (setq placeholder (point))
9870 (while (and (c-forward-objc-directive)
9871 (< (point) indent-point))
9872 (c-forward-syntactic-ws)
9873 (setq placeholder (point)))
9874 (goto-char placeholder))
9875 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9876 ))
9877
9878
9879 ;; (CASE 6 has been removed.)
9880
9881 ;; CASE 7: line is an expression, not a statement. Most
9882 ;; likely we are either in a function prototype or a function
9883 ;; call argument list
9884 ((not (or (and c-special-brace-lists
9885 (save-excursion
9886 (goto-char containing-sexp)
9887 (c-looking-at-special-brace-list)))
9888 (eq (char-after containing-sexp) ?{)))
9889 (cond
9890
9891 ;; CASE 7A: we are looking at the arglist closing paren.
9892 ;; C.f. case 7F.
9893 ((memq char-after-ip '(?\) ?\]))
9894 (goto-char containing-sexp)
9895 (setq placeholder (c-point 'boi))
9896 (if (and (c-safe (backward-up-list 1) t)
9897 (>= (point) placeholder))
9898 (progn
9899 (forward-char)
9900 (skip-chars-forward " \t"))
9901 (goto-char placeholder))
9902 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
9903 (c-most-enclosing-brace paren-state (point))
9904 paren-state))
9905
9906 ;; CASE 7B: Looking at the opening brace of an
9907 ;; in-expression block or brace list. C.f. cases 4, 16A
9908 ;; and 17E.
9909 ((and (eq char-after-ip ?{)
9910 (progn
9911 (setq placeholder (c-inside-bracelist-p (point)
9912 paren-state))
9913 (if placeholder
9914 (setq tmpsymbol '(brace-list-open . inexpr-class))
9915 (setq tmpsymbol '(block-open . inexpr-statement)
9916 placeholder
9917 (cdr-safe (c-looking-at-inexpr-block
9918 (c-safe-position containing-sexp
9919 paren-state)
9920 containing-sexp)))
9921 ;; placeholder is nil if it's a block directly in
9922 ;; a function arglist. That makes us skip out of
9923 ;; this case.
9924 )))
9925 (goto-char placeholder)
9926 (back-to-indentation)
9927 (c-add-stmt-syntax (car tmpsymbol) nil t
9928 (c-most-enclosing-brace paren-state (point))
9929 paren-state)
9930 (if (/= (point) placeholder)
9931 (c-add-syntax (cdr tmpsymbol))))
9932
9933 ;; CASE 7C: we are looking at the first argument in an empty
9934 ;; argument list. Use arglist-close if we're actually
9935 ;; looking at a close paren or bracket.
9936 ((memq char-before-ip '(?\( ?\[))
9937 (goto-char containing-sexp)
9938 (setq placeholder (c-point 'boi))
9939 (if (and (c-safe (backward-up-list 1) t)
9940 (>= (point) placeholder))
9941 (progn
9942 (forward-char)
9943 (skip-chars-forward " \t"))
9944 (goto-char placeholder))
9945 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
9946 (c-most-enclosing-brace paren-state (point))
9947 paren-state))
9948
9949 ;; CASE 7D: we are inside a conditional test clause. treat
9950 ;; these things as statements
9951 ((progn
9952 (goto-char containing-sexp)
9953 (and (c-safe (c-forward-sexp -1) t)
9954 (looking-at "\\<for\\>[^_]")))
9955 (goto-char (1+ containing-sexp))
9956 (c-forward-syntactic-ws indent-point)
9957 (if (eq char-before-ip ?\;)
9958 (c-add-syntax 'statement (point))
9959 (c-add-syntax 'statement-cont (point))
9960 ))
9961
9962 ;; CASE 7E: maybe a continued ObjC method call. This is the
9963 ;; case when we are inside a [] bracketed exp, and what
9964 ;; precede the opening bracket is not an identifier.
9965 ((and c-opt-method-key
9966 (eq (char-after containing-sexp) ?\[)
9967 (progn
9968 (goto-char (1- containing-sexp))
9969 (c-backward-syntactic-ws (c-point 'bod))
9970 (if (not (looking-at c-symbol-key))
9971 (c-add-syntax 'objc-method-call-cont containing-sexp))
9972 )))
9973
9974 ;; CASE 7F: we are looking at an arglist continuation line,
9975 ;; but the preceding argument is on the same line as the
9976 ;; opening paren. This case includes multi-line
9977 ;; mathematical paren groupings, but we could be on a
9978 ;; for-list continuation line. C.f. case 7A.
9979 ((progn
9980 (goto-char (1+ containing-sexp))
9981 (< (save-excursion
9982 (c-forward-syntactic-ws)
9983 (point))
9984 (c-point 'bonl)))
9985 (goto-char containing-sexp) ; paren opening the arglist
9986 (setq placeholder (c-point 'boi))
9987 (if (and (c-safe (backward-up-list 1) t)
9988 (>= (point) placeholder))
9989 (progn
9990 (forward-char)
9991 (skip-chars-forward " \t"))
9992 (goto-char placeholder))
9993 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
9994 (c-most-enclosing-brace c-state-cache (point))
9995 paren-state))
9996
9997 ;; CASE 7G: we are looking at just a normal arglist
9998 ;; continuation line
9999 (t (c-forward-syntactic-ws indent-point)
10000 (c-add-syntax 'arglist-cont (c-point 'boi)))
10001 ))
10002
10003 ;; CASE 8: func-local multi-inheritance line
10004 ((and (c-major-mode-is 'c++-mode)
10005 (save-excursion
10006 (goto-char indent-point)
10007 (skip-chars-forward " \t")
10008 (looking-at c-opt-postfix-decl-spec-key)))
10009 (goto-char indent-point)
10010 (skip-chars-forward " \t")
10011 (cond
10012
10013 ;; CASE 8A: non-hanging colon on an inher intro
10014 ((eq char-after-ip ?:)
10015 (c-backward-syntactic-ws lim)
10016 (c-add-syntax 'inher-intro (c-point 'boi)))
10017
10018 ;; CASE 8B: hanging colon on an inher intro
10019 ((eq char-before-ip ?:)
10020 (c-add-syntax 'inher-intro (c-point 'boi)))
10021
10022 ;; CASE 8C: a continued inheritance line
10023 (t
10024 (c-beginning-of-inheritance-list lim)
10025 (c-add-syntax 'inher-cont (point))
10026 )))
10027
10028 ;; CASE 9: we are inside a brace-list
10029 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
10030 (setq special-brace-list
10031 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
10032 (save-excursion
10033 (goto-char containing-sexp)
10034 (c-looking-at-special-brace-list)))
10035 (c-inside-bracelist-p containing-sexp paren-state))))
10036 (cond
10037
10038 ;; CASE 9A: In the middle of a special brace list opener.
10039 ((and (consp special-brace-list)
10040 (save-excursion
10041 (goto-char containing-sexp)
10042 (eq (char-after) ?\())
10043 (eq char-after-ip (car (cdr special-brace-list))))
10044 (goto-char (car (car special-brace-list)))
10045 (skip-chars-backward " \t")
10046 (if (and (bolp)
10047 (assoc 'statement-cont
10048 (setq placeholder (c-guess-basic-syntax))))
10049 (setq c-syntactic-context placeholder)
10050 (c-beginning-of-statement-1
10051 (c-safe-position (1- containing-sexp) paren-state))
10052 (c-forward-token-2 0)
10053 (while (looking-at c-specifier-key)
10054 (goto-char (match-end 1))
10055 (c-forward-syntactic-ws))
10056 (c-add-syntax 'brace-list-open (c-point 'boi))))
10057
10058 ;; CASE 9B: brace-list-close brace
10059 ((if (consp special-brace-list)
10060 ;; Check special brace list closer.
10061 (progn
10062 (goto-char (car (car special-brace-list)))
10063 (save-excursion
10064 (goto-char indent-point)
10065 (back-to-indentation)
10066 (or
10067 ;; We were between the special close char and the `)'.
10068 (and (eq (char-after) ?\))
10069 (eq (1+ (point)) (cdr (car special-brace-list))))
10070 ;; We were before the special close char.
10071 (and (eq (char-after) (cdr (cdr special-brace-list)))
10072 (zerop (c-forward-token-2))
10073 (eq (1+ (point)) (cdr (car special-brace-list)))))))
10074 ;; Normal brace list check.
10075 (and (eq char-after-ip ?})
10076 (c-safe (goto-char (c-up-list-backward (point))) t)
10077 (= (point) containing-sexp)))
10078 (if (eq (point) (c-point 'boi))
10079 (c-add-syntax 'brace-list-close (point))
10080 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10081 (c-beginning-of-statement-1 lim)
10082 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
10083
10084 (t
10085 ;; Prepare for the rest of the cases below by going to the
10086 ;; token following the opening brace
10087 (if (consp special-brace-list)
10088 (progn
10089 (goto-char (car (car special-brace-list)))
10090 (c-forward-token-2 1 nil indent-point))
10091 (goto-char containing-sexp))
10092 (forward-char)
10093 (let ((start (point)))
10094 (c-forward-syntactic-ws indent-point)
10095 (goto-char (max start (c-point 'bol))))
10096 (c-skip-ws-forward indent-point)
10097 (cond
10098
10099 ;; CASE 9C: we're looking at the first line in a brace-list
10100 ((= (point) indent-point)
10101 (if (consp special-brace-list)
10102 (goto-char (car (car special-brace-list)))
10103 (goto-char containing-sexp))
10104 (if (eq (point) (c-point 'boi))
10105 (c-add-syntax 'brace-list-intro (point))
10106 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10107 (c-beginning-of-statement-1 lim)
10108 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
10109
10110 ;; CASE 9D: this is just a later brace-list-entry or
10111 ;; brace-entry-open
10112 (t (if (or (eq char-after-ip ?{)
10113 (and c-special-brace-lists
10114 (save-excursion
10115 (goto-char indent-point)
10116 (c-forward-syntactic-ws (c-point 'eol))
10117 (c-looking-at-special-brace-list (point)))))
10118 (c-add-syntax 'brace-entry-open (point))
10119 (c-add-syntax 'brace-list-entry (point))
10120 ))
10121 ))))
10122
10123 ;; CASE 10: A continued statement or top level construct.
10124 ((and (not (memq char-before-ip '(?\; ?:)))
10125 (not (c-at-vsemi-p before-ws-ip))
10126 (or (not (eq char-before-ip ?}))
10127 (c-looking-at-inexpr-block-backward c-state-cache))
10128 (> (point)
10129 (save-excursion
10130 (c-beginning-of-statement-1 containing-sexp)
10131 (setq placeholder (point))))
10132 (/= placeholder containing-sexp))
10133 ;; This is shared with case 18.
10134 (c-guess-continued-construct indent-point
10135 char-after-ip
10136 placeholder
10137 containing-sexp
10138 paren-state))
10139
10140 ;; CASE 16: block close brace, possibly closing the defun or
10141 ;; the class
10142 ((eq char-after-ip ?})
10143 ;; From here on we have the next containing sexp in lim.
10144 (setq lim (c-most-enclosing-brace paren-state))
10145 (goto-char containing-sexp)
10146 (cond
10147
10148 ;; CASE 16E: Closing a statement block? This catches
10149 ;; cases where it's preceded by a statement keyword,
10150 ;; which works even when used in an "invalid" context,
10151 ;; e.g. a macro argument.
10152 ((c-after-conditional)
10153 (c-backward-to-block-anchor lim)
10154 (c-add-stmt-syntax 'block-close nil t lim paren-state))
10155
10156 ;; CASE 16A: closing a lambda defun or an in-expression
10157 ;; block? C.f. cases 4, 7B and 17E.
10158 ((setq placeholder (c-looking-at-inexpr-block
10159 (c-safe-position containing-sexp paren-state)
10160 nil))
10161 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10162 'inline-close
10163 'block-close))
10164 (goto-char containing-sexp)
10165 (back-to-indentation)
10166 (if (= containing-sexp (point))
10167 (c-add-syntax tmpsymbol (point))
10168 (goto-char (cdr placeholder))
10169 (back-to-indentation)
10170 (c-add-stmt-syntax tmpsymbol nil t
10171 (c-most-enclosing-brace paren-state (point))
10172 paren-state)
10173 (if (/= (point) (cdr placeholder))
10174 (c-add-syntax (car placeholder)))))
10175
10176 ;; CASE 16B: does this close an inline or a function in
10177 ;; a non-class declaration level block?
10178 ((save-excursion
10179 (and lim
10180 (progn
10181 (goto-char lim)
10182 (c-looking-at-decl-block
10183 (c-most-enclosing-brace paren-state lim)
10184 nil))
10185 (setq placeholder (point))))
10186 (c-backward-to-decl-anchor lim)
10187 (back-to-indentation)
10188 (if (save-excursion
10189 (goto-char placeholder)
10190 (looking-at c-other-decl-block-key))
10191 (c-add-syntax 'defun-close (point))
10192 (c-add-syntax 'inline-close (point))))
10193
10194 ;; CASE 16F: Can be a defun-close of a function declared
10195 ;; in a statement block, e.g. in Pike or when using gcc
10196 ;; extensions, but watch out for macros followed by
10197 ;; blocks. Let it through to be handled below.
10198 ;; C.f. cases B.3 and 17G.
10199 ((save-excursion
10200 (and (not (c-at-statement-start-p))
10201 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10202 (setq placeholder (point))
10203 (let ((c-recognize-typeless-decls nil))
10204 ;; Turn off recognition of constructs that
10205 ;; lacks a type in this case, since that's more
10206 ;; likely to be a macro followed by a block.
10207 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10208 (back-to-indentation)
10209 (if (/= (point) containing-sexp)
10210 (goto-char placeholder))
10211 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
10212
10213 ;; CASE 16C: If there is an enclosing brace then this is
10214 ;; a block close since defun closes inside declaration
10215 ;; level blocks have been handled above.
10216 (lim
10217 ;; If the block is preceded by a case/switch label on
10218 ;; the same line, we anchor at the first preceding label
10219 ;; at boi. The default handling in c-add-stmt-syntax
10220 ;; really fixes it better, but we do like this to keep
10221 ;; the indentation compatible with version 5.28 and
10222 ;; earlier. C.f. case 17H.
10223 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10224 (eq (c-beginning-of-statement-1 lim) 'label)))
10225 (goto-char placeholder)
10226 (if (looking-at c-label-kwds-regexp)
10227 (c-add-syntax 'block-close (point))
10228 (goto-char containing-sexp)
10229 ;; c-backward-to-block-anchor not necessary here; those
10230 ;; situations are handled in case 16E above.
10231 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
10232
10233 ;; CASE 16D: Only top level defun close left.
10234 (t
10235 (goto-char containing-sexp)
10236 (c-backward-to-decl-anchor lim)
10237 (c-add-stmt-syntax 'defun-close nil nil
10238 (c-most-enclosing-brace paren-state)
10239 paren-state))
10240 ))
10241
10242 ;; CASE 19: line is an expression, not a statement, and is directly
10243 ;; contained by a template delimiter. Most likely, we are in a
10244 ;; template arglist within a statement. This case is based on CASE
10245 ;; 7. At some point in the future, we may wish to create more
10246 ;; syntactic symbols such as `template-intro',
10247 ;; `template-cont-nonempty', etc., and distinguish between them as we
10248 ;; do for `arglist-intro' etc. (2009-12-07).
10249 ((and c-recognize-<>-arglists
10250 (setq containing-< (c-up-list-backward indent-point containing-sexp))
10251 (eq (char-after containing-<) ?\<))
10252 (setq placeholder (c-point 'boi containing-<))
10253 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
10254 ; '<') before indent-point.
10255 (if (>= (point) placeholder)
10256 (progn
10257 (forward-char)
10258 (skip-chars-forward " \t"))
10259 (goto-char placeholder))
10260 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
10261 (c-most-enclosing-brace c-state-cache (point))
10262 paren-state))
10263
10264 ;; CASE 17: Statement or defun catchall.
10265 (t
10266 (goto-char indent-point)
10267 ;; Back up statements until we find one that starts at boi.
10268 (while (let* ((prev-point (point))
10269 (last-step-type (c-beginning-of-statement-1
10270 containing-sexp)))
10271 (if (= (point) prev-point)
10272 (progn
10273 (setq step-type (or step-type last-step-type))
10274 nil)
10275 (setq step-type last-step-type)
10276 (/= (point) (c-point 'boi)))))
10277 (cond
10278
10279 ;; CASE 17B: continued statement
10280 ((and (eq step-type 'same)
10281 (/= (point) indent-point))
10282 (c-add-stmt-syntax 'statement-cont nil nil
10283 containing-sexp paren-state))
10284
10285 ;; CASE 17A: After a case/default label?
10286 ((progn
10287 (while (and (eq step-type 'label)
10288 (not (looking-at c-label-kwds-regexp)))
10289 (setq step-type
10290 (c-beginning-of-statement-1 containing-sexp)))
10291 (eq step-type 'label))
10292 (c-add-stmt-syntax (if (eq char-after-ip ?{)
10293 'statement-case-open
10294 'statement-case-intro)
10295 nil t containing-sexp paren-state))
10296
10297 ;; CASE 17D: any old statement
10298 ((progn
10299 (while (eq step-type 'label)
10300 (setq step-type
10301 (c-beginning-of-statement-1 containing-sexp)))
10302 (eq step-type 'previous))
10303 (c-add-stmt-syntax 'statement nil t
10304 containing-sexp paren-state)
10305 (if (eq char-after-ip ?{)
10306 (c-add-syntax 'block-open)))
10307
10308 ;; CASE 17I: Inside a substatement block.
10309 ((progn
10310 ;; The following tests are all based on containing-sexp.
10311 (goto-char containing-sexp)
10312 ;; From here on we have the next containing sexp in lim.
10313 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
10314 (c-after-conditional))
10315 (c-backward-to-block-anchor lim)
10316 (c-add-stmt-syntax 'statement-block-intro nil t
10317 lim paren-state)
10318 (if (eq char-after-ip ?{)
10319 (c-add-syntax 'block-open)))
10320
10321 ;; CASE 17E: first statement in an in-expression block.
10322 ;; C.f. cases 4, 7B and 16A.
10323 ((setq placeholder (c-looking-at-inexpr-block
10324 (c-safe-position containing-sexp paren-state)
10325 nil))
10326 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10327 'defun-block-intro
10328 'statement-block-intro))
10329 (back-to-indentation)
10330 (if (= containing-sexp (point))
10331 (c-add-syntax tmpsymbol (point))
10332 (goto-char (cdr placeholder))
10333 (back-to-indentation)
10334 (c-add-stmt-syntax tmpsymbol nil t
10335 (c-most-enclosing-brace c-state-cache (point))
10336 paren-state)
10337 (if (/= (point) (cdr placeholder))
10338 (c-add-syntax (car placeholder))))
10339 (if (eq char-after-ip ?{)
10340 (c-add-syntax 'block-open)))
10341
10342 ;; CASE 17F: first statement in an inline, or first
10343 ;; statement in a top-level defun. we can tell this is it
10344 ;; if there are no enclosing braces that haven't been
10345 ;; narrowed out by a class (i.e. don't use bod here).
10346 ((save-excursion
10347 (or (not (setq placeholder (c-most-enclosing-brace
10348 paren-state)))
10349 (and (progn
10350 (goto-char placeholder)
10351 (eq (char-after) ?{))
10352 (c-looking-at-decl-block (c-most-enclosing-brace
10353 paren-state (point))
10354 nil))))
10355 (c-backward-to-decl-anchor lim)
10356 (back-to-indentation)
10357 (c-add-syntax 'defun-block-intro (point)))
10358
10359 ;; CASE 17G: First statement in a function declared inside
10360 ;; a normal block. This can occur in Pike and with
10361 ;; e.g. the gcc extensions, but watch out for macros
10362 ;; followed by blocks. C.f. cases B.3 and 16F.
10363 ((save-excursion
10364 (and (not (c-at-statement-start-p))
10365 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10366 (setq placeholder (point))
10367 (let ((c-recognize-typeless-decls nil))
10368 ;; Turn off recognition of constructs that lacks
10369 ;; a type in this case, since that's more likely
10370 ;; to be a macro followed by a block.
10371 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10372 (back-to-indentation)
10373 (if (/= (point) containing-sexp)
10374 (goto-char placeholder))
10375 (c-add-stmt-syntax 'defun-block-intro nil t
10376 lim paren-state))
10377
10378 ;; CASE 17H: First statement in a block.
10379 (t
10380 ;; If the block is preceded by a case/switch label on the
10381 ;; same line, we anchor at the first preceding label at
10382 ;; boi. The default handling in c-add-stmt-syntax is
10383 ;; really fixes it better, but we do like this to keep the
10384 ;; indentation compatible with version 5.28 and earlier.
10385 ;; C.f. case 16C.
10386 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10387 (eq (c-beginning-of-statement-1 lim) 'label)))
10388 (goto-char placeholder)
10389 (if (looking-at c-label-kwds-regexp)
10390 (c-add-syntax 'statement-block-intro (point))
10391 (goto-char containing-sexp)
10392 ;; c-backward-to-block-anchor not necessary here; those
10393 ;; situations are handled in case 17I above.
10394 (c-add-stmt-syntax 'statement-block-intro nil t
10395 lim paren-state))
10396 (if (eq char-after-ip ?{)
10397 (c-add-syntax 'block-open)))
10398 ))
10399 )
10400
10401 ;; now we need to look at any modifiers
10402 (goto-char indent-point)
10403 (skip-chars-forward " \t")
10404
10405 ;; are we looking at a comment only line?
10406 (when (and (looking-at c-comment-start-regexp)
10407 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
10408 (c-append-syntax 'comment-intro))
10409
10410 ;; we might want to give additional offset to friends (in C++).
10411 (when (and c-opt-friend-key
10412 (looking-at c-opt-friend-key))
10413 (c-append-syntax 'friend))
10414
10415 ;; Set syntactic-relpos.
10416 (let ((p c-syntactic-context))
10417 (while (and p
10418 (if (integerp (c-langelem-pos (car p)))
10419 (progn
10420 (setq syntactic-relpos (c-langelem-pos (car p)))
10421 nil)
10422 t))
10423 (setq p (cdr p))))
10424
10425 ;; Start of or a continuation of a preprocessor directive?
10426 (if (and macro-start
10427 (eq macro-start (c-point 'boi))
10428 (not (and (c-major-mode-is 'pike-mode)
10429 (eq (char-after (1+ macro-start)) ?\"))))
10430 (c-append-syntax 'cpp-macro)
10431 (when (and c-syntactic-indentation-in-macros macro-start)
10432 (if in-macro-expr
10433 (when (or
10434 (< syntactic-relpos macro-start)
10435 (not (or
10436 (assq 'arglist-intro c-syntactic-context)
10437 (assq 'arglist-cont c-syntactic-context)
10438 (assq 'arglist-cont-nonempty c-syntactic-context)
10439 (assq 'arglist-close c-syntactic-context))))
10440 ;; If inside a cpp expression, i.e. anywhere in a
10441 ;; cpp directive except a #define body, we only let
10442 ;; through the syntactic analysis that is internal
10443 ;; in the expression. That means the arglist
10444 ;; elements, if they are anchored inside the cpp
10445 ;; expression.
10446 (setq c-syntactic-context nil)
10447 (c-add-syntax 'cpp-macro-cont macro-start))
10448 (when (and (eq macro-start syntactic-relpos)
10449 (not (assq 'cpp-define-intro c-syntactic-context))
10450 (save-excursion
10451 (goto-char macro-start)
10452 (or (not (c-forward-to-cpp-define-body))
10453 (<= (point) (c-point 'boi indent-point)))))
10454 ;; Inside a #define body and the syntactic analysis is
10455 ;; anchored on the start of the #define. In this case
10456 ;; we add cpp-define-intro to get the extra
10457 ;; indentation of the #define body.
10458 (c-add-syntax 'cpp-define-intro)))))
10459
10460 ;; return the syntax
10461 c-syntactic-context)))
10462
10463 \f
10464 ;; Indentation calculation.
10465
10466 (defun c-evaluate-offset (offset langelem symbol)
10467 ;; offset can be a number, a function, a variable, a list, or one of
10468 ;; the symbols + or -
10469 ;;
10470 ;; This function might do hidden buffer changes.
10471 (let ((res
10472 (cond
10473 ((numberp offset) offset)
10474 ((vectorp offset) offset)
10475 ((null offset) nil)
10476
10477 ((eq offset '+) c-basic-offset)
10478 ((eq offset '-) (- c-basic-offset))
10479 ((eq offset '++) (* 2 c-basic-offset))
10480 ((eq offset '--) (* 2 (- c-basic-offset)))
10481 ((eq offset '*) (/ c-basic-offset 2))
10482 ((eq offset '/) (/ (- c-basic-offset) 2))
10483
10484 ((functionp offset)
10485 (c-evaluate-offset
10486 (funcall offset
10487 (cons (c-langelem-sym langelem)
10488 (c-langelem-pos langelem)))
10489 langelem symbol))
10490
10491 ((listp offset)
10492 (cond
10493 ((eq (car offset) 'quote)
10494 (c-benign-error "The offset %S for %s was mistakenly quoted"
10495 offset symbol)
10496 nil)
10497
10498 ((memq (car offset) '(min max))
10499 (let (res val (method (car offset)))
10500 (setq offset (cdr offset))
10501 (while offset
10502 (setq val (c-evaluate-offset (car offset) langelem symbol))
10503 (cond
10504 ((not val))
10505 ((not res)
10506 (setq res val))
10507 ((integerp val)
10508 (if (vectorp res)
10509 (c-benign-error "\
10510 Error evaluating offset %S for %s: \
10511 Cannot combine absolute offset %S with relative %S in `%s' method"
10512 (car offset) symbol res val method)
10513 (setq res (funcall method res val))))
10514 (t
10515 (if (integerp res)
10516 (c-benign-error "\
10517 Error evaluating offset %S for %s: \
10518 Cannot combine relative offset %S with absolute %S in `%s' method"
10519 (car offset) symbol res val method)
10520 (setq res (vector (funcall method (aref res 0)
10521 (aref val 0)))))))
10522 (setq offset (cdr offset)))
10523 res))
10524
10525 ((eq (car offset) 'add)
10526 (let (res val)
10527 (setq offset (cdr offset))
10528 (while offset
10529 (setq val (c-evaluate-offset (car offset) langelem symbol))
10530 (cond
10531 ((not val))
10532 ((not res)
10533 (setq res val))
10534 ((integerp val)
10535 (if (vectorp res)
10536 (setq res (vector (+ (aref res 0) val)))
10537 (setq res (+ res val))))
10538 (t
10539 (if (vectorp res)
10540 (c-benign-error "\
10541 Error evaluating offset %S for %s: \
10542 Cannot combine absolute offsets %S and %S in `add' method"
10543 (car offset) symbol res val)
10544 (setq res val)))) ; Override.
10545 (setq offset (cdr offset)))
10546 res))
10547
10548 (t
10549 (let (res)
10550 (when (eq (car offset) 'first)
10551 (setq offset (cdr offset)))
10552 (while (and (not res) offset)
10553 (setq res (c-evaluate-offset (car offset) langelem symbol)
10554 offset (cdr offset)))
10555 res))))
10556
10557 ((and (symbolp offset) (boundp offset))
10558 (symbol-value offset))
10559
10560 (t
10561 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10562 nil))))
10563
10564 (if (or (null res) (integerp res)
10565 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10566 res
10567 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10568 offset symbol res)
10569 nil)))
10570
10571 (defun c-calc-offset (langelem)
10572 ;; Get offset from LANGELEM which is a list beginning with the
10573 ;; syntactic symbol and followed by any analysis data it provides.
10574 ;; That data may be zero or more elements, but if at least one is
10575 ;; given then the first is the anchor position (or nil). The symbol
10576 ;; is matched against `c-offsets-alist' and the offset calculated
10577 ;; from that is returned.
10578 ;;
10579 ;; This function might do hidden buffer changes.
10580 (let* ((symbol (c-langelem-sym langelem))
10581 (match (assq symbol c-offsets-alist))
10582 (offset (cdr-safe match)))
10583 (if match
10584 (setq offset (c-evaluate-offset offset langelem symbol))
10585 (if c-strict-syntax-p
10586 (c-benign-error "No offset found for syntactic symbol %s" symbol))
10587 (setq offset 0))
10588 (if (vectorp offset)
10589 offset
10590 (or (and (numberp offset) offset)
10591 (and (symbolp offset) (symbol-value offset))
10592 0))
10593 ))
10594
10595 (defun c-get-offset (langelem)
10596 ;; This is a compatibility wrapper for `c-calc-offset' in case
10597 ;; someone is calling it directly. It takes an old style syntactic
10598 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10599 ;; new list form.
10600 ;;
10601 ;; This function might do hidden buffer changes.
10602 (if (c-langelem-pos langelem)
10603 (c-calc-offset (list (c-langelem-sym langelem)
10604 (c-langelem-pos langelem)))
10605 (c-calc-offset langelem)))
10606
10607 (defun c-get-syntactic-indentation (langelems)
10608 ;; Calculate the syntactic indentation from a syntactic description
10609 ;; as returned by `c-guess-syntax'.
10610 ;;
10611 ;; Note that topmost-intro always has an anchor position at bol, for
10612 ;; historical reasons. It's often used together with other symbols
10613 ;; that has more sane positions. Since we always use the first
10614 ;; found anchor position, we rely on that these other symbols always
10615 ;; precede topmost-intro in the LANGELEMS list.
10616 ;;
10617 ;; This function might do hidden buffer changes.
10618 (let ((indent 0) anchor)
10619
10620 (while langelems
10621 (let* ((c-syntactic-element (car langelems))
10622 (res (c-calc-offset c-syntactic-element)))
10623
10624 (if (vectorp res)
10625 ;; Got an absolute column that overrides any indentation
10626 ;; we've collected so far, but not the relative
10627 ;; indentation we might get for the nested structures
10628 ;; further down the langelems list.
10629 (setq indent (elt res 0)
10630 anchor (point-min)) ; A position at column 0.
10631
10632 ;; Got a relative change of the current calculated
10633 ;; indentation.
10634 (setq indent (+ indent res))
10635
10636 ;; Use the anchor position from the first syntactic
10637 ;; element with one.
10638 (unless anchor
10639 (setq anchor (c-langelem-pos (car langelems)))))
10640
10641 (setq langelems (cdr langelems))))
10642
10643 (if anchor
10644 (+ indent (save-excursion
10645 (goto-char anchor)
10646 (current-column)))
10647 indent)))
10648
10649 \f
10650 (cc-provide 'cc-engine)
10651
10652 ;;; cc-engine.el ends here