]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-engine.el
Require rmail not just when compiling.
[gnu-emacs] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
6
7 ;; Authors: 2001- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs
11 ;; 1987 Stewart Clamen
12 ;; 1985 Richard M. Stallman
13 ;; Maintainer: bug-cc-mode@gnu.org
14 ;; Created: 22-Apr-1997 (split from cc-mode.el)
15 ;; Version: See cc-mode.el
16 ;; Keywords: c languages oop
17
18 ;; This file is part of GNU Emacs.
19
20 ;; GNU Emacs is free software: you can redistribute it and/or modify
21 ;; it under the terms of the GNU General Public License as published by
22 ;; the Free Software Foundation, either version 3 of the License, or
23 ;; (at your option) any later version.
24
25 ;; GNU Emacs is distributed in the hope that it will be useful,
26 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;; GNU General Public License for more details.
29
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32
33 ;;; Commentary:
34
35 ;; The functions which have docstring documentation can be considered
36 ;; part of an API which other packages can use in CC Mode buffers.
37 ;; Otoh, undocumented functions and functions with the documentation
38 ;; in comments are considered purely internal and can change semantics
39 ;; or even disappear in the future.
40 ;;
41 ;; (This policy applies to CC Mode as a whole, not just this file. It
42 ;; probably also applies to many other Emacs packages, but here it's
43 ;; clearly spelled out.)
44
45 ;; Hidden buffer changes
46 ;;
47 ;; Various functions in CC Mode use text properties for caching and
48 ;; syntactic markup purposes, and those of them that might modify such
49 ;; properties but still don't modify the buffer in a visible way are
50 ;; said to do "hidden buffer changes". They should be used within
51 ;; `c-save-buffer-state' or a similar function that saves and restores
52 ;; buffer modifiedness, disables buffer change hooks, etc.
53 ;;
54 ;; Interactive functions are assumed to not do hidden buffer changes,
55 ;; except in the specific parts of them that do real changes.
56 ;;
57 ;; Lineup functions are assumed to do hidden buffer changes. They
58 ;; must not do real changes, though.
59 ;;
60 ;; All other functions that do hidden buffer changes have that noted
61 ;; in their doc string or comment.
62 ;;
63 ;; The intention with this system is to avoid wrapping every leaf
64 ;; function that do hidden buffer changes inside
65 ;; `c-save-buffer-state'. It should be used as near the top of the
66 ;; interactive functions as possible.
67 ;;
68 ;; Functions called during font locking are allowed to do hidden
69 ;; buffer changes since the font-lock package run them in a context
70 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
71 ;; inspired by `save-buffer-state' in the font-lock package).
72
73 ;; Use of text properties
74 ;;
75 ;; CC Mode uses several text properties internally to mark up various
76 ;; positions, e.g. to improve speed and to eliminate glitches in
77 ;; interactive refontification.
78 ;;
79 ;; Note: This doc is for internal use only. Other packages should not
80 ;; assume that these text properties are used as described here.
81 ;;
82 ;; 'syntax-table
83 ;; Used to modify the syntax of some characters. It is used to
84 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
85 ;; to "hide" obtrusive characters in preprocessor lines.
86 ;;
87 ;; This property is used on single characters and is therefore
88 ;; always treated as front and rear nonsticky (or start and end open
89 ;; in XEmacs vocabulary). It's therefore installed on
90 ;; `text-property-default-nonsticky' if that variable exists (Emacs
91 ;; >= 21).
92 ;;
93 ;; 'c-is-sws and 'c-in-sws
94 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
95 ;; speed them up. See the comment blurb before `c-put-is-sws'
96 ;; below for further details.
97 ;;
98 ;; 'c-type
99 ;; This property is used on single characters to mark positions with
100 ;; special syntactic relevance of various sorts. Its primary use is
101 ;; to avoid glitches when multiline constructs are refontified
102 ;; interactively (on font lock decoration level 3). It's cleared in
103 ;; a region before it's fontified and is then put on relevant chars
104 ;; in that region as they are encountered during the fontification.
105 ;; The value specifies the kind of position:
106 ;;
107 ;; 'c-decl-arg-start
108 ;; Put on the last char of the token preceding each declaration
109 ;; inside a declaration style arglist (typically in a function
110 ;; prototype).
111 ;;
112 ;; 'c-decl-end
113 ;; Put on the last char of the token preceding a declaration.
114 ;; This is used in cases where declaration boundaries can't be
115 ;; recognized simply by looking for a token like ";" or "}".
116 ;; `c-type-decl-end-used' must be set if this is used (see also
117 ;; `c-find-decl-spots').
118 ;;
119 ;; 'c-<>-arg-sep
120 ;; Put on the commas that separate arguments in angle bracket
121 ;; arglists like C++ template arglists.
122 ;;
123 ;; 'c-decl-id-start and 'c-decl-type-start
124 ;; Put on the last char of the token preceding each declarator
125 ;; in the declarator list of a declaration. They are also used
126 ;; between the identifiers cases like enum declarations.
127 ;; 'c-decl-type-start is used when the declarators are types,
128 ;; 'c-decl-id-start otherwise.
129 ;;
130 ;; 'c-awk-NL-prop
131 ;; Used in AWK mode to mark the various kinds of newlines. See
132 ;; cc-awk.el.
133
134 ;;; Code:
135
136 (eval-when-compile
137 (let ((load-path
138 (if (and (boundp 'byte-compile-dest-file)
139 (stringp byte-compile-dest-file))
140 (cons (file-name-directory byte-compile-dest-file) load-path)
141 load-path)))
142 (load "cc-bytecomp" nil t)))
143
144 (cc-require 'cc-defs)
145 (cc-require-when-compile 'cc-langs)
146 (cc-require 'cc-vars)
147
148 ;; Silence the compiler.
149 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
150
151 \f
152 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
153
154 (defmacro c-declare-lang-variables ()
155 `(progn
156 ,@(apply 'nconc
157 (mapcar (lambda (init)
158 `(,(if (elt init 2)
159 `(defvar ,(car init) nil ,(elt init 2))
160 `(defvar ,(car init) nil))
161 (make-variable-buffer-local ',(car init))))
162 (cdr c-lang-variable-inits)))))
163 (c-declare-lang-variables)
164
165 \f
166 ;;; Internal state variables.
167
168 ;; Internal state of hungry delete key feature
169 (defvar c-hungry-delete-key nil)
170 (make-variable-buffer-local 'c-hungry-delete-key)
171
172 ;; The electric flag (toggled by `c-toggle-electric-state').
173 ;; If t, electric actions (like automatic reindentation, and (if
174 ;; c-auto-newline is also set) auto newlining) will happen when an electric
175 ;; key like `{' is pressed (or an electric keyword like `else').
176 (defvar c-electric-flag t)
177 (make-variable-buffer-local 'c-electric-flag)
178
179 ;; Internal state of auto newline feature.
180 (defvar c-auto-newline nil)
181 (make-variable-buffer-local 'c-auto-newline)
182
183 ;; Included in the mode line to indicate the active submodes.
184 ;; (defvar c-submode-indicators nil)
185 ;; (make-variable-buffer-local 'c-submode-indicators)
186
187 (defun c-calculate-state (arg prevstate)
188 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
189 ;; arg is nil or zero, toggle the state. If arg is negative, turn
190 ;; the state off, and if arg is positive, turn the state on
191 (if (or (not arg)
192 (zerop (setq arg (prefix-numeric-value arg))))
193 (not prevstate)
194 (> arg 0)))
195
196 ;; Dynamically bound cache for `c-in-literal'.
197 (defvar c-in-literal-cache t)
198
199 \f
200 ;; Basic handling of preprocessor directives.
201
202 ;; This is a dynamically bound cache used together with
203 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
204 ;; works as long as point doesn't cross a macro boundary.
205 (defvar c-macro-start 'unknown)
206
207 (defsubst c-query-and-set-macro-start ()
208 (if (symbolp c-macro-start)
209 (setq c-macro-start (save-excursion
210 (c-save-buffer-state ()
211 (and (c-beginning-of-macro)
212 (point)))))
213 c-macro-start))
214
215 (defsubst c-query-macro-start ()
216 (if (symbolp c-macro-start)
217 (save-excursion
218 (c-save-buffer-state ()
219 (and (c-beginning-of-macro)
220 (point))))
221 c-macro-start))
222
223 (defun c-beginning-of-macro (&optional lim)
224 "Go to the beginning of a preprocessor directive.
225 Leave point at the beginning of the directive and return t if in one,
226 otherwise return nil and leave point unchanged.
227
228 Note that this function might do hidden buffer changes. See the
229 comment at the start of cc-engine.el for more info."
230 (when c-opt-cpp-prefix
231 (let ((here (point)))
232 (save-restriction
233 (if lim (narrow-to-region lim (point-max)))
234 (beginning-of-line)
235 (while (eq (char-before (1- (point))) ?\\)
236 (forward-line -1))
237 (back-to-indentation)
238 (if (and (<= (point) here)
239 (looking-at c-opt-cpp-start))
240 t
241 (goto-char here)
242 nil)))))
243
244 (defun c-end-of-macro ()
245 "Go to the end of a preprocessor directive.
246 More accurately, move the point to the end of the closest following
247 line that doesn't end with a line continuation backslash - no check is
248 done that the point is inside a cpp directive to begin with.
249
250 Note that this function might do hidden buffer changes. See the
251 comment at the start of cc-engine.el for more info."
252 (while (progn
253 (end-of-line)
254 (when (and (eq (char-before) ?\\)
255 (not (eobp)))
256 (forward-char)
257 t))))
258
259 (defun c-forward-over-cpp-define-id ()
260 ;; Assuming point is at the "#" that introduces a preprocessor
261 ;; directive, it's moved forward to the end of the identifier which is
262 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
263 ;; is returned in this case, in all other cases nil is returned and
264 ;; point isn't moved.
265 ;;
266 ;; This function might do hidden buffer changes.
267 (when (and c-opt-cpp-macro-define-id
268 (looking-at c-opt-cpp-macro-define-id))
269 (goto-char (match-end 0))))
270
271 (defun c-forward-to-cpp-define-body ()
272 ;; Assuming point is at the "#" that introduces a preprocessor
273 ;; directive, it's moved forward to the start of the definition body
274 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
275 ;; specifies). Non-nil is returned in this case, in all other cases
276 ;; nil is returned and point isn't moved.
277 ;;
278 ;; This function might do hidden buffer changes.
279 (when (and c-opt-cpp-macro-define-start
280 (looking-at c-opt-cpp-macro-define-start)
281 (not (= (match-end 0) (c-point 'eol))))
282 (goto-char (match-end 0))))
283
284 \f
285 ;;; Basic utility functions.
286
287 (defun c-syntactic-content (from to paren-level)
288 ;; Return the given region as a string where all syntactic
289 ;; whitespace is removed or, where necessary, replaced with a single
290 ;; space. If PAREN-LEVEL is given then all parens in the region are
291 ;; collapsed to "()", "[]" etc.
292 ;;
293 ;; This function might do hidden buffer changes.
294
295 (save-excursion
296 (save-restriction
297 (narrow-to-region from to)
298 (goto-char from)
299 (let* ((parts (list nil)) (tail parts) pos in-paren)
300
301 (while (re-search-forward c-syntactic-ws-start to t)
302 (goto-char (setq pos (match-beginning 0)))
303 (c-forward-syntactic-ws)
304 (if (= (point) pos)
305 (forward-char)
306
307 (when paren-level
308 (save-excursion
309 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
310 pos (point))))
311
312 (if (and (> pos from)
313 (< (point) to)
314 (looking-at "\\w\\|\\s_")
315 (save-excursion
316 (goto-char (1- pos))
317 (looking-at "\\w\\|\\s_")))
318 (progn
319 (setcdr tail (list (buffer-substring-no-properties from pos)
320 " "))
321 (setq tail (cddr tail)))
322 (setcdr tail (list (buffer-substring-no-properties from pos)))
323 (setq tail (cdr tail)))
324
325 (when in-paren
326 (when (= (car (parse-partial-sexp pos to -1)) -1)
327 (setcdr tail (list (buffer-substring-no-properties
328 (1- (point)) (point))))
329 (setq tail (cdr tail))))
330
331 (setq from (point))))
332
333 (setcdr tail (list (buffer-substring-no-properties from to)))
334 (apply 'concat (cdr parts))))))
335
336 (defun c-shift-line-indentation (shift-amt)
337 ;; Shift the indentation of the current line with the specified
338 ;; amount (positive inwards). The buffer is modified only if
339 ;; SHIFT-AMT isn't equal to zero.
340 (let ((pos (- (point-max) (point)))
341 (c-macro-start c-macro-start)
342 tmp-char-inserted)
343 (if (zerop shift-amt)
344 nil
345 ;; If we're on an empty line inside a macro, we take the point
346 ;; to be at the current indentation and shift it to the
347 ;; appropriate column. This way we don't treat the extra
348 ;; whitespace out to the line continuation as indentation.
349 (when (and (c-query-and-set-macro-start)
350 (looking-at "[ \t]*\\\\$")
351 (save-excursion
352 (skip-chars-backward " \t")
353 (bolp)))
354 (insert ?x)
355 (backward-char)
356 (setq tmp-char-inserted t))
357 (unwind-protect
358 (let ((col (current-indentation)))
359 (delete-region (c-point 'bol) (c-point 'boi))
360 (beginning-of-line)
361 (indent-to (+ col shift-amt)))
362 (when tmp-char-inserted
363 (delete-char 1))))
364 ;; If initial point was within line's indentation and we're not on
365 ;; a line with a line continuation in a macro, position after the
366 ;; indentation. Else stay at same point in text.
367 (if (and (< (point) (c-point 'boi))
368 (not tmp-char-inserted))
369 (back-to-indentation)
370 (if (> (- (point-max) pos) (point))
371 (goto-char (- (point-max) pos))))))
372
373 (defsubst c-keyword-sym (keyword)
374 ;; Return non-nil if the string KEYWORD is a known keyword. More
375 ;; precisely, the value is the symbol for the keyword in
376 ;; `c-keywords-obarray'.
377 (intern-soft keyword c-keywords-obarray))
378
379 (defsubst c-keyword-member (keyword-sym lang-constant)
380 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
381 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
382 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
383 ;; nil then the result is nil.
384 (get keyword-sym lang-constant))
385
386 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
387 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
388 "\"|"
389 "\""))
390
391 ;; Regexp matching string limit syntax.
392 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
393 "\\s\"\\|\\s|"
394 "\\s\""))
395
396 ;; Regexp matching WS followed by string limit syntax.
397 (defconst c-ws*-string-limit-regexp
398 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
399
400 ;; Holds formatted error strings for the few cases where parse errors
401 ;; are reported.
402 (defvar c-parsing-error nil)
403 (make-variable-buffer-local 'c-parsing-error)
404
405 (defun c-echo-parsing-error (&optional quiet)
406 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
407 (c-benign-error "%s" c-parsing-error))
408 c-parsing-error)
409
410 ;; Faces given to comments and string literals. This is used in some
411 ;; situations to speed up recognition; it isn't mandatory that font
412 ;; locking is in use. This variable is extended with the face in
413 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
414 (defvar c-literal-faces
415 (append '(font-lock-comment-face font-lock-string-face)
416 (when (facep 'font-lock-comment-delimiter-face)
417 ;; New in Emacs 22.
418 '(font-lock-comment-delimiter-face))))
419
420 (defsubst c-put-c-type-property (pos value)
421 ;; Put a c-type property with the given value at POS.
422 (c-put-char-property pos 'c-type value))
423
424 (defun c-clear-c-type-property (from to value)
425 ;; Remove all occurences of the c-type property that has the given
426 ;; value in the region between FROM and TO. VALUE is assumed to not
427 ;; be nil.
428 ;;
429 ;; Note: This assumes that c-type is put on single chars only; it's
430 ;; very inefficient if matching properties cover large regions.
431 (save-excursion
432 (goto-char from)
433 (while (progn
434 (when (eq (get-text-property (point) 'c-type) value)
435 (c-clear-char-property (point) 'c-type))
436 (goto-char (next-single-property-change (point) 'c-type nil to))
437 (< (point) to)))))
438
439 \f
440 ;; Some debug tools to visualize various special positions. This
441 ;; debug code isn't as portable as the rest of CC Mode.
442
443 (cc-bytecomp-defun overlays-in)
444 (cc-bytecomp-defun overlay-get)
445 (cc-bytecomp-defun overlay-start)
446 (cc-bytecomp-defun overlay-end)
447 (cc-bytecomp-defun delete-overlay)
448 (cc-bytecomp-defun overlay-put)
449 (cc-bytecomp-defun make-overlay)
450
451 (defun c-debug-add-face (beg end face)
452 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
453 (while overlays
454 (setq overlay (car overlays)
455 overlays (cdr overlays))
456 (when (eq (overlay-get overlay 'face) face)
457 (setq beg (min beg (overlay-start overlay))
458 end (max end (overlay-end overlay)))
459 (delete-overlay overlay)))
460 (overlay-put (make-overlay beg end) 'face face)))
461
462 (defun c-debug-remove-face (beg end face)
463 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
464 (ol-beg beg) (ol-end end))
465 (while overlays
466 (setq overlay (car overlays)
467 overlays (cdr overlays))
468 (when (eq (overlay-get overlay 'face) face)
469 (setq ol-beg (min ol-beg (overlay-start overlay))
470 ol-end (max ol-end (overlay-end overlay)))
471 (delete-overlay overlay)))
472 (when (< ol-beg beg)
473 (overlay-put (make-overlay ol-beg beg) 'face face))
474 (when (> ol-end end)
475 (overlay-put (make-overlay end ol-end) 'face face))))
476
477 \f
478 ;; `c-beginning-of-statement-1' and accompanying stuff.
479
480 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
481 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
482 ;; better way should be implemented, but this will at least shut up
483 ;; the byte compiler.
484 (defvar c-maybe-labelp)
485
486 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
487
488 ;; Macros used internally in c-beginning-of-statement-1 for the
489 ;; automaton actions.
490 (defmacro c-bos-push-state ()
491 '(setq stack (cons (cons state saved-pos)
492 stack)))
493 (defmacro c-bos-pop-state (&optional do-if-done)
494 `(if (setq state (car (car stack))
495 saved-pos (cdr (car stack))
496 stack (cdr stack))
497 t
498 ,do-if-done
499 (throw 'loop nil)))
500 (defmacro c-bos-pop-state-and-retry ()
501 '(throw 'loop (setq state (car (car stack))
502 saved-pos (cdr (car stack))
503 ;; Throw nil if stack is empty, else throw non-nil.
504 stack (cdr stack))))
505 (defmacro c-bos-save-pos ()
506 '(setq saved-pos (vector pos tok ptok pptok)))
507 (defmacro c-bos-restore-pos ()
508 '(unless (eq (elt saved-pos 0) start)
509 (setq pos (elt saved-pos 0)
510 tok (elt saved-pos 1)
511 ptok (elt saved-pos 2)
512 pptok (elt saved-pos 3))
513 (goto-char pos)
514 (setq sym nil)))
515 (defmacro c-bos-save-error-info (missing got)
516 `(setq saved-pos (vector pos ,missing ,got)))
517 (defmacro c-bos-report-error ()
518 '(unless noerror
519 (setq c-parsing-error
520 (format "No matching `%s' found for `%s' on line %d"
521 (elt saved-pos 1)
522 (elt saved-pos 2)
523 (1+ (count-lines (point-min)
524 (c-point 'bol (elt saved-pos 0))))))))
525
526 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
527 noerror comma-delim)
528 "Move to the start of the current statement or declaration, or to
529 the previous one if already at the beginning of one. Only
530 statements/declarations on the same level are considered, i.e. don't
531 move into or out of sexps (not even normal expression parentheses).
532
533 If point is already at the earliest statment within braces or parens,
534 this function doesn't move back into any whitespace preceding it; it
535 returns 'same in this case.
536
537 Stop at statement continuation tokens like \"else\", \"catch\",
538 \"finally\" and the \"while\" in \"do ... while\" if the start point
539 is within the continuation. If starting at such a token, move to the
540 corresponding statement start. If at the beginning of a statement,
541 move to the closest containing statement if there is any. This might
542 also stop at a continuation clause.
543
544 Labels are treated as part of the following statements if
545 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
546 statement start keyword.) Otherwise, each label is treated as a
547 separate statement.
548
549 Macros are ignored \(i.e. skipped over) unless point is within one, in
550 which case the content of the macro is treated as normal code. Aside
551 from any normal statement starts found in it, stop at the first token
552 of the content in the macro, i.e. the expression of an \"#if\" or the
553 start of the definition in a \"#define\". Also stop at start of
554 macros before leaving them.
555
556 Return:
557 'label if stopped at a label;
558 'same if stopped at the beginning of the current statement;
559 'up if stepped to a containing statement;
560 'previous if stepped to a preceding statement;
561 'beginning if stepped from a statement continuation clause to
562 its start clause; or
563 'macro if stepped to a macro start.
564 Note that 'same and not 'label is returned if stopped at the same
565 label without crossing the colon character.
566
567 LIM may be given to limit the search. If the search hits the limit,
568 point will be left at the closest following token, or at the start
569 position if that is less ('same is returned in this case).
570
571 NOERROR turns off error logging to `c-parsing-error'.
572
573 Normally only ';' and virtual semicolons are considered to delimit
574 statements, but if COMMA-DELIM is non-nil then ',' is treated
575 as a delimiter too.
576
577 Note that this function might do hidden buffer changes. See the
578 comment at the start of cc-engine.el for more info."
579
580 ;; The bulk of this function is a pushdown automaton that looks at statement
581 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
582 ;; purpose is to keep track of nested statements, ensuring that such
583 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
584 ;; does with nested braces/brackets/parentheses).
585 ;;
586 ;; Note: The position of a boundary is the following token.
587 ;;
588 ;; Beginning with the current token (the one following point), move back one
589 ;; sexp at a time (where a sexp is, more or less, either a token or the
590 ;; entire contents of a brace/bracket/paren pair). Each time a statement
591 ;; boundary is crossed or a "while"-like token is found, update the state of
592 ;; the PDA. Stop at the beginning of a statement when the stack (holding
593 ;; nested statement info) is empty and the position has been moved.
594 ;;
595 ;; The following variables constitute the PDA:
596 ;;
597 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
598 ;; scanned back over, 'boundary if we've just gone back over a
599 ;; statement boundary, or nil otherwise.
600 ;; state: takes one of the values (nil else else-boundary while
601 ;; while-boundary catch catch-boundary).
602 ;; nil means "no "while"-like token yet scanned".
603 ;; 'else, for example, means "just gone back over an else".
604 ;; 'else-boundary means "just gone back over a statement boundary
605 ;; immediately after having gone back over an else".
606 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
607 ;; of error reporting information.
608 ;; stack: The stack onto which the PDA pushes its state. Each entry
609 ;; consists of a saved value of state and saved-pos. An entry is
610 ;; pushed when we move back over a "continuation" token (e.g. else)
611 ;; and popped when we encounter the corresponding opening token
612 ;; (e.g. if).
613 ;;
614 ;;
615 ;; The following diagram briefly outlines the PDA.
616 ;;
617 ;; Common state:
618 ;; "else": Push state, goto state `else'.
619 ;; "while": Push state, goto state `while'.
620 ;; "catch" or "finally": Push state, goto state `catch'.
621 ;; boundary: Pop state.
622 ;; other: Do nothing special.
623 ;;
624 ;; State `else':
625 ;; boundary: Goto state `else-boundary'.
626 ;; other: Error, pop state, retry token.
627 ;;
628 ;; State `else-boundary':
629 ;; "if": Pop state.
630 ;; boundary: Error, pop state.
631 ;; other: See common state.
632 ;;
633 ;; State `while':
634 ;; boundary: Save position, goto state `while-boundary'.
635 ;; other: Pop state, retry token.
636 ;;
637 ;; State `while-boundary':
638 ;; "do": Pop state.
639 ;; boundary: Restore position if it's not at start, pop state. [*see below]
640 ;; other: See common state.
641 ;;
642 ;; State `catch':
643 ;; boundary: Goto state `catch-boundary'.
644 ;; other: Error, pop state, retry token.
645 ;;
646 ;; State `catch-boundary':
647 ;; "try": Pop state.
648 ;; "catch": Goto state `catch'.
649 ;; boundary: Error, pop state.
650 ;; other: See common state.
651 ;;
652 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
653 ;; searching for a "do" which would have opened a do-while. If we didn't
654 ;; find it, we discard the analysis done since the "while", go back to this
655 ;; token in the buffer and restart the scanning there, this time WITHOUT
656 ;; pushing the 'while state onto the stack.
657 ;;
658 ;; In addition to the above there is some special handling of labels
659 ;; and macros.
660
661 (let ((case-fold-search nil)
662 (start (point))
663 macro-start
664 (delims (if comma-delim '(?\; ?,) '(?\;)))
665 (c-stmt-delim-chars (if comma-delim
666 c-stmt-delim-chars-with-comma
667 c-stmt-delim-chars))
668 c-in-literal-cache c-maybe-labelp saved
669 ;; Current position.
670 pos
671 ;; Position of last stmt boundary character (e.g. ;).
672 boundary-pos
673 ;; The position of the last sexp or bound that follows the
674 ;; first found colon, i.e. the start of the nonlabel part of
675 ;; the statement. It's `start' if a colon is found just after
676 ;; the start.
677 after-labels-pos
678 ;; Like `after-labels-pos', but the first such position inside
679 ;; a label, i.e. the start of the last label before the start
680 ;; of the nonlabel part of the statement.
681 last-label-pos
682 ;; The last position where a label is possible provided the
683 ;; statement started there. It's nil as long as no invalid
684 ;; label content has been found (according to
685 ;; `c-nonlabel-token-key'. It's `start' if no valid label
686 ;; content was found in the label. Note that we might still
687 ;; regard it a label if it starts with `c-label-kwds'.
688 label-good-pos
689 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
690 ;; See above.
691 sym
692 ;; Current state in the automaton. See above.
693 state
694 ;; Current saved positions. See above.
695 saved-pos
696 ;; Stack of conses (state . saved-pos).
697 stack
698 ;; Regexp which matches "for", "if", etc.
699 (cond-key (or c-opt-block-stmt-key
700 "\\<\\>")) ; Matches nothing.
701 ;; Return value.
702 (ret 'same)
703 ;; Positions of the last three sexps or bounds we've stopped at.
704 tok ptok pptok)
705
706 (save-restriction
707 (if lim (narrow-to-region lim (point-max)))
708
709 (if (save-excursion
710 (and (c-beginning-of-macro)
711 (/= (point) start)))
712 (setq macro-start (point)))
713
714 ;; Try to skip back over unary operator characters, to register
715 ;; that we've moved.
716 (while (progn
717 (setq pos (point))
718 (c-backward-syntactic-ws)
719 ;; Protect post-++/-- operators just before a virtual semicolon.
720 (and (not (c-at-vsemi-p))
721 (/= (skip-chars-backward "-+!*&~@`#") 0))))
722
723 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
724 ;; done. Later on we ignore the boundaries for statements that don't
725 ;; contain any sexp. The only thing that is affected is that the error
726 ;; checking is a little less strict, and we really don't bother.
727 (if (and (memq (char-before) delims)
728 (progn (forward-char -1)
729 (setq saved (point))
730 (c-backward-syntactic-ws)
731 (or (memq (char-before) delims)
732 (memq (char-before) '(?: nil))
733 (eq (char-syntax (char-before)) ?\()
734 (c-at-vsemi-p))))
735 (setq ret 'previous
736 pos saved)
737
738 ;; Begin at start and not pos to detect macros if we stand
739 ;; directly after the #.
740 (goto-char start)
741 (if (looking-at "\\<\\|\\W")
742 ;; Record this as the first token if not starting inside it.
743 (setq tok start))
744
745 ;; The following while loop goes back one sexp (balanced parens,
746 ;; etc. with contents, or symbol or suchlike) each iteration. This
747 ;; movement is accomplished with a call to scan-sexps approx 130 lines
748 ;; below.
749 (while
750 (catch 'loop ;; Throw nil to break, non-nil to continue.
751 (cond
752 ((save-excursion
753 (and macro-start ; Always NIL for AWK.
754 (progn (skip-chars-backward " \t")
755 (eq (char-before) ?#))
756 (progn (setq saved (1- (point)))
757 (beginning-of-line)
758 (not (eq (char-before (1- (point))) ?\\)))
759 (looking-at c-opt-cpp-start)
760 (progn (skip-chars-forward " \t")
761 (eq (point) saved))))
762 (goto-char saved)
763 (if (and (c-forward-to-cpp-define-body)
764 (progn (c-forward-syntactic-ws start)
765 (< (point) start)))
766 ;; Stop at the first token in the content of the macro.
767 (setq pos (point)
768 ignore-labels t) ; Avoid the label check on exit.
769 (setq pos saved
770 ret 'macro
771 ignore-labels t))
772 (throw 'loop nil))
773
774 ;; Do a round through the automaton if we've just passed a
775 ;; statement boundary or passed a "while"-like token.
776 ((or sym
777 (and (looking-at cond-key)
778 (setq sym (intern (match-string 1)))))
779
780 (when (and (< pos start) (null stack))
781 (throw 'loop nil))
782
783 ;; The PDA state handling.
784 ;;
785 ;; Refer to the description of the PDA in the opening
786 ;; comments. In the following OR form, the first leaf
787 ;; attempts to handles one of the specific actions detailed
788 ;; (e.g., finding token "if" whilst in state `else-boundary').
789 ;; We drop through to the second leaf (which handles common
790 ;; state) if no specific handler is found in the first cond.
791 ;; If a parsing error is detected (e.g. an "else" with no
792 ;; preceding "if"), we throw to the enclosing catch.
793 ;;
794 ;; Note that the (eq state 'else) means
795 ;; "we've just passed an else", NOT "we're looking for an
796 ;; else".
797 (or (cond
798 ((eq state 'else)
799 (if (eq sym 'boundary)
800 (setq state 'else-boundary)
801 (c-bos-report-error)
802 (c-bos-pop-state-and-retry)))
803
804 ((eq state 'else-boundary)
805 (cond ((eq sym 'if)
806 (c-bos-pop-state (setq ret 'beginning)))
807 ((eq sym 'boundary)
808 (c-bos-report-error)
809 (c-bos-pop-state))))
810
811 ((eq state 'while)
812 (if (and (eq sym 'boundary)
813 ;; Since this can cause backtracking we do a
814 ;; little more careful analysis to avoid it:
815 ;; If there's a label in front of the while
816 ;; it can't be part of a do-while.
817 (not after-labels-pos))
818 (progn (c-bos-save-pos)
819 (setq state 'while-boundary))
820 (c-bos-pop-state-and-retry))) ; Can't be a do-while
821
822 ((eq state 'while-boundary)
823 (cond ((eq sym 'do)
824 (c-bos-pop-state (setq ret 'beginning)))
825 ((eq sym 'boundary) ; isn't a do-while
826 (c-bos-restore-pos) ; the position of the while
827 (c-bos-pop-state)))) ; no longer searching for do.
828
829 ((eq state 'catch)
830 (if (eq sym 'boundary)
831 (setq state 'catch-boundary)
832 (c-bos-report-error)
833 (c-bos-pop-state-and-retry)))
834
835 ((eq state 'catch-boundary)
836 (cond
837 ((eq sym 'try)
838 (c-bos-pop-state (setq ret 'beginning)))
839 ((eq sym 'catch)
840 (setq state 'catch))
841 ((eq sym 'boundary)
842 (c-bos-report-error)
843 (c-bos-pop-state)))))
844
845 ;; This is state common. We get here when the previous
846 ;; cond statement found no particular state handler.
847 (cond ((eq sym 'boundary)
848 ;; If we have a boundary at the start
849 ;; position we push a frame to go to the
850 ;; previous statement.
851 (if (>= pos start)
852 (c-bos-push-state)
853 (c-bos-pop-state)))
854 ((eq sym 'else)
855 (c-bos-push-state)
856 (c-bos-save-error-info 'if 'else)
857 (setq state 'else))
858 ((eq sym 'while)
859 ;; Is this a real while, or a do-while?
860 ;; The next `when' triggers unless we are SURE that
861 ;; the `while' is not the tailend of a `do-while'.
862 (when (or (not pptok)
863 (memq (char-after pptok) delims)
864 ;; The following kludge is to prevent
865 ;; infinite recursion when called from
866 ;; c-awk-after-if-for-while-condition-p,
867 ;; or the like.
868 (and (eq (point) start)
869 (c-vsemi-status-unknown-p))
870 (c-at-vsemi-p pptok))
871 ;; Since this can cause backtracking we do a
872 ;; little more careful analysis to avoid it: If
873 ;; the while isn't followed by a (possibly
874 ;; virtual) semicolon it can't be a do-while.
875 (c-bos-push-state)
876 (setq state 'while)))
877 ((memq sym '(catch finally))
878 (c-bos-push-state)
879 (c-bos-save-error-info 'try sym)
880 (setq state 'catch))))
881
882 (when c-maybe-labelp
883 ;; We're either past a statement boundary or at the
884 ;; start of a statement, so throw away any label data
885 ;; for the previous one.
886 (setq after-labels-pos nil
887 last-label-pos nil
888 c-maybe-labelp nil))))
889
890 ;; Step to the previous sexp, but not if we crossed a
891 ;; boundary, since that doesn't consume an sexp.
892 (if (eq sym 'boundary)
893 (setq ret 'previous)
894
895 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
896 ;; BACKWARDS THROUGH THE SOURCE.
897
898 ;; This is typically fast with the caching done by
899 ;; c-(backward|forward)-sws.
900 (c-backward-syntactic-ws)
901
902 (let ((before-sws-pos (point))
903 ;; Set as long as we have to continue jumping by sexps.
904 ;; It's the position to use as end in the next round.
905 sexp-loop-continue-pos
906 ;; The end position of the area to search for statement
907 ;; barriers in this round.
908 (sexp-loop-end-pos pos))
909
910 ;; The following while goes back one sexp per iteration.
911 (while
912 (progn
913 (unless (c-safe (c-backward-sexp) t)
914 ;; Give up if we hit an unbalanced block. Since the
915 ;; stack won't be empty the code below will report a
916 ;; suitable error.
917 (throw 'loop nil))
918
919 ;; Check if the sexp movement crossed a statement or
920 ;; declaration boundary. But first modify the point
921 ;; so that `c-crosses-statement-barrier-p' only looks
922 ;; at the non-sexp chars following the sexp.
923 (save-excursion
924 (when (setq
925 boundary-pos
926 (cond
927 ((if macro-start
928 nil
929 (save-excursion
930 (when (c-beginning-of-macro)
931 ;; Set continuation position in case
932 ;; `c-crosses-statement-barrier-p'
933 ;; doesn't detect anything below.
934 (setq sexp-loop-continue-pos (point)))))
935 ;; If the sexp movement took us into a
936 ;; macro then there were only some non-sexp
937 ;; chars after it. Skip out of the macro
938 ;; to analyze them but not the non-sexp
939 ;; chars that might be inside the macro.
940 (c-end-of-macro)
941 (c-crosses-statement-barrier-p
942 (point) sexp-loop-end-pos))
943
944 ((and
945 (eq (char-after) ?{)
946 (not (c-looking-at-inexpr-block lim nil t)))
947 ;; Passed a block sexp. That's a boundary
948 ;; alright.
949 (point))
950
951 ((looking-at "\\s\(")
952 ;; Passed some other paren. Only analyze
953 ;; the non-sexp chars after it.
954 (goto-char (1+ (c-down-list-backward
955 before-sws-pos)))
956 ;; We're at a valid token start position
957 ;; (outside the `save-excursion') if
958 ;; `c-crosses-statement-barrier-p' failed.
959 (c-crosses-statement-barrier-p
960 (point) sexp-loop-end-pos))
961
962 (t
963 ;; Passed a symbol sexp or line
964 ;; continuation. It doesn't matter that
965 ;; it's included in the analyzed region.
966 (if (c-crosses-statement-barrier-p
967 (point) sexp-loop-end-pos)
968 t
969 ;; If it was a line continuation then we
970 ;; have to continue looping.
971 (if (looking-at "\\\\$")
972 (setq sexp-loop-continue-pos (point)))
973 nil))))
974
975 (setq pptok ptok
976 ptok tok
977 tok boundary-pos
978 sym 'boundary)
979 ;; Like a C "continue". Analyze the next sexp.
980 (throw 'loop t)))
981
982 sexp-loop-continue-pos) ; End of "go back a sexp" loop.
983 (goto-char sexp-loop-continue-pos)
984 (setq sexp-loop-end-pos sexp-loop-continue-pos
985 sexp-loop-continue-pos nil))))
986
987 ;; ObjC method def?
988 (when (and c-opt-method-key
989 (setq saved (c-in-method-def-p)))
990 (setq pos saved
991 ignore-labels t) ; Avoid the label check on exit.
992 (throw 'loop nil))
993
994 ;; Handle labels.
995 (unless (eq ignore-labels t)
996 (when (numberp c-maybe-labelp)
997 ;; `c-crosses-statement-barrier-p' has found a colon, so we
998 ;; might be in a label now. Have we got a real label
999 ;; (including a case label) or something like C++'s "public:"?
1000 (if (or (not (looking-at c-nonlabel-token-key)) ; proper label
1001 (save-excursion ; e.g. "case 'a':" ?
1002 (and (c-safe (c-backward-sexp) t)
1003 (looking-at "\\<case\\>")))) ; FIXME!!! this is
1004 ; wrong for AWK. 2006/1/14.
1005 (progn
1006 (if after-labels-pos ; Have we already encountered a label?
1007 (if (not last-label-pos)
1008 (setq last-label-pos (or tok start)))
1009 (setq after-labels-pos (or tok start)))
1010 (setq c-maybe-labelp t
1011 label-good-pos nil))
1012 (setq c-maybe-labelp nil))) ; bogus "label"
1013
1014 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1015 ; been found.
1016 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1017 ;; We're in a potential label and it's the first
1018 ;; time we've found something that isn't allowed in
1019 ;; one.
1020 (setq label-good-pos (or tok start))))
1021
1022 ;; We've moved back by a sexp, so update the token positions.
1023 (setq sym nil
1024 pptok ptok
1025 ptok tok
1026 tok (point)
1027 pos tok))) ; Not nil (for the while loop).
1028
1029 ;; If the stack isn't empty there might be errors to report.
1030 (while stack
1031 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1032 (c-bos-report-error))
1033 (setq saved-pos (cdr (car stack))
1034 stack (cdr stack)))
1035
1036 (when (and (eq ret 'same)
1037 (not (memq sym '(boundary ignore nil))))
1038 ;; Need to investigate closer whether we've crossed
1039 ;; between a substatement and its containing statement.
1040 (if (setq saved (if (looking-at c-block-stmt-1-key)
1041 ptok
1042 pptok))
1043 (cond ((> start saved) (setq pos saved))
1044 ((= start saved) (setq ret 'up)))))
1045
1046 (when (and (not ignore-labels)
1047 (eq c-maybe-labelp t)
1048 (not (eq ret 'beginning))
1049 after-labels-pos
1050 (or (not label-good-pos)
1051 (<= label-good-pos pos)
1052 (progn
1053 (goto-char (if (and last-label-pos
1054 (< last-label-pos start))
1055 last-label-pos
1056 pos))
1057 (looking-at c-label-kwds-regexp))))
1058 ;; We're in a label. Maybe we should step to the statement
1059 ;; after it.
1060 (if (< after-labels-pos start)
1061 (setq pos after-labels-pos)
1062 (setq ret 'label)
1063 (if (and last-label-pos (< last-label-pos start))
1064 ;; Might have jumped over several labels. Go to the last one.
1065 (setq pos last-label-pos)))))
1066
1067 ;; Skip over the unary operators that can start the statement.
1068 (goto-char pos)
1069 (while (progn
1070 (c-backward-syntactic-ws)
1071 ;; protect AWK post-inc/decrement operators, etc.
1072 (and (not (c-at-vsemi-p (point)))
1073 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1074 (setq pos (point)))
1075 (goto-char pos)
1076 ret)))
1077
1078 (defun c-crosses-statement-barrier-p (from to)
1079 "Return non-nil if buffer positions FROM to TO cross one or more
1080 statement or declaration boundaries. The returned value is actually
1081 the position of the earliest boundary char. FROM must not be within
1082 a string or comment.
1083
1084 The variable `c-maybe-labelp' is set to the position of the first `:' that
1085 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1086 single `?' is found, then `c-maybe-labelp' is cleared.
1087
1088 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1089 regarded as having a \"virtual semicolon\" immediately after the last token on
1090 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1091
1092 Note that this function might do hidden buffer changes. See the
1093 comment at the start of cc-engine.el for more info."
1094 (let ((skip-chars c-stmt-delim-chars)
1095 lit-range)
1096 (save-excursion
1097 (catch 'done
1098 (goto-char from)
1099 (while (progn (skip-chars-forward skip-chars to)
1100 (< (point) to))
1101 (cond
1102 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1103 (goto-char (cdr lit-range)))
1104 ((eq (char-after) ?:)
1105 (forward-char)
1106 (if (and (eq (char-after) ?:)
1107 (< (point) to))
1108 ;; Ignore scope operators.
1109 (forward-char)
1110 (setq c-maybe-labelp (1- (point)))))
1111 ((eq (char-after) ??)
1112 ;; A question mark. Can't be a label, so stop
1113 ;; looking for more : and ?.
1114 (setq c-maybe-labelp nil
1115 skip-chars (substring c-stmt-delim-chars 0 -2)))
1116 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1117 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1118 (backward-char))
1119 (skip-chars-backward " \t" from)
1120 (if (c-at-vsemi-p)
1121 (throw 'done (point))
1122 (forward-line)))
1123 (t (throw 'done (point)))))
1124 ;; In trailing space after an as yet undetected virtual semicolon?
1125 (c-backward-syntactic-ws from)
1126 (if (and (< (point) to)
1127 (c-at-vsemi-p))
1128 (point)
1129 nil)))))
1130
1131 (defun c-at-statement-start-p ()
1132 "Return non-nil if the point is at the first token in a statement
1133 or somewhere in the syntactic whitespace before it.
1134
1135 A \"statement\" here is not restricted to those inside code blocks.
1136 Any kind of declaration-like construct that occur outside function
1137 bodies is also considered a \"statement\".
1138
1139 Note that this function might do hidden buffer changes. See the
1140 comment at the start of cc-engine.el for more info."
1141
1142 (save-excursion
1143 (let ((end (point))
1144 c-maybe-labelp)
1145 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1146 (or (bobp)
1147 (eq (char-before) ?})
1148 (and (eq (char-before) ?{)
1149 (not (and c-special-brace-lists
1150 (progn (backward-char)
1151 (c-looking-at-special-brace-list)))))
1152 (c-crosses-statement-barrier-p (point) end)))))
1153
1154 (defun c-at-expression-start-p ()
1155 "Return non-nil if the point is at the first token in an expression or
1156 statement, or somewhere in the syntactic whitespace before it.
1157
1158 An \"expression\" here is a bit different from the normal language
1159 grammar sense: It's any sequence of expression tokens except commas,
1160 unless they are enclosed inside parentheses of some kind. Also, an
1161 expression never continues past an enclosing parenthesis, but it might
1162 contain parenthesis pairs of any sort except braces.
1163
1164 Since expressions never cross statement boundaries, this function also
1165 recognizes statement beginnings, just like `c-at-statement-start-p'.
1166
1167 Note that this function might do hidden buffer changes. See the
1168 comment at the start of cc-engine.el for more info."
1169
1170 (save-excursion
1171 (let ((end (point))
1172 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1173 c-maybe-labelp)
1174 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1175 (or (bobp)
1176 (memq (char-before) '(?{ ?}))
1177 (save-excursion (backward-char)
1178 (looking-at "\\s("))
1179 (c-crosses-statement-barrier-p (point) end)))))
1180
1181 \f
1182 ;; A set of functions that covers various idiosyncrasies in
1183 ;; implementations of `forward-comment'.
1184
1185 ;; Note: Some emacsen considers incorrectly that any line comment
1186 ;; ending with a backslash continues to the next line. I can't think
1187 ;; of any way to work around that in a reliable way without changing
1188 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1189 ;; changing the syntax for backslash doesn't work since we must treat
1190 ;; escapes in string literals correctly.)
1191
1192 (defun c-forward-single-comment ()
1193 "Move forward past whitespace and the closest following comment, if any.
1194 Return t if a comment was found, nil otherwise. In either case, the
1195 point is moved past the following whitespace. Line continuations,
1196 i.e. a backslashes followed by line breaks, are treated as whitespace.
1197 The line breaks that end line comments are considered to be the
1198 comment enders, so the point will be put on the beginning of the next
1199 line if it moved past a line comment.
1200
1201 This function does not do any hidden buffer changes."
1202
1203 (let ((start (point)))
1204 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1205 (goto-char (match-end 0)))
1206
1207 (when (forward-comment 1)
1208 (if (eobp)
1209 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1210 ;; forwards at eob.
1211 nil
1212
1213 ;; Emacs includes the ending newline in a b-style (c++)
1214 ;; comment, but XEmacs doesn't. We depend on the Emacs
1215 ;; behavior (which also is symmetric).
1216 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1217 (condition-case nil (forward-char 1)))
1218
1219 t))))
1220
1221 (defsubst c-forward-comments ()
1222 "Move forward past all following whitespace and comments.
1223 Line continuations, i.e. a backslashes followed by line breaks, are
1224 treated as whitespace.
1225
1226 Note that this function might do hidden buffer changes. See the
1227 comment at the start of cc-engine.el for more info."
1228
1229 (while (or
1230 ;; If forward-comment in at least XEmacs 21 is given a large
1231 ;; positive value, it'll loop all the way through if it hits
1232 ;; eob.
1233 (and (forward-comment 5)
1234 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1235 ;; forwards at eob.
1236 (not (eobp)))
1237
1238 (when (looking-at "\\\\[\n\r]")
1239 (forward-char 2)
1240 t))))
1241
1242 (defun c-backward-single-comment ()
1243 "Move backward past whitespace and the closest preceding comment, if any.
1244 Return t if a comment was found, nil otherwise. In either case, the
1245 point is moved past the preceding whitespace. Line continuations,
1246 i.e. a backslashes followed by line breaks, are treated as whitespace.
1247 The line breaks that end line comments are considered to be the
1248 comment enders, so the point cannot be at the end of the same line to
1249 move over a line comment.
1250
1251 This function does not do any hidden buffer changes."
1252
1253 (let ((start (point)))
1254 ;; When we got newline terminated comments, forward-comment in all
1255 ;; supported emacsen so far will stop at eol of each line not
1256 ;; ending with a comment when moving backwards. This corrects for
1257 ;; that, and at the same time handles line continuations.
1258 (while (progn
1259 (skip-chars-backward " \t\n\r\f\v")
1260 (and (looking-at "[\n\r]")
1261 (eq (char-before) ?\\)))
1262 (backward-char))
1263
1264 (if (bobp)
1265 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1266 ;; backwards at bob.
1267 nil
1268
1269 ;; Leave point after the closest following newline if we've
1270 ;; backed up over any above, since forward-comment won't move
1271 ;; backward over a line comment if point is at the end of the
1272 ;; same line.
1273 (re-search-forward "\\=\\s *[\n\r]" start t)
1274
1275 (if (if (forward-comment -1)
1276 (if (eolp)
1277 ;; If forward-comment above succeeded and we're at eol
1278 ;; then the newline we moved over above didn't end a
1279 ;; line comment, so we give it another go.
1280 (forward-comment -1)
1281 t))
1282
1283 ;; Emacs <= 20 and XEmacs move back over the closer of a
1284 ;; block comment that lacks an opener.
1285 (if (looking-at "\\*/")
1286 (progn (forward-char 2) nil)
1287 t)))))
1288
1289 (defsubst c-backward-comments ()
1290 "Move backward past all preceding whitespace and comments.
1291 Line continuations, i.e. a backslashes followed by line breaks, are
1292 treated as whitespace. The line breaks that end line comments are
1293 considered to be the comment enders, so the point cannot be at the end
1294 of the same line to move over a line comment. Unlike
1295 c-backward-syntactic-ws, this function doesn't move back over
1296 preprocessor directives.
1297
1298 Note that this function might do hidden buffer changes. See the
1299 comment at the start of cc-engine.el for more info."
1300
1301 (let ((start (point)))
1302 (while (and
1303 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1304 ;; return t when moving backwards at bob.
1305 (not (bobp))
1306
1307 (if (forward-comment -1)
1308 (if (looking-at "\\*/")
1309 ;; Emacs <= 20 and XEmacs move back over the
1310 ;; closer of a block comment that lacks an opener.
1311 (progn (forward-char 2) nil)
1312 t)
1313
1314 ;; XEmacs treats line continuations as whitespace but
1315 ;; only in the backward direction, which seems a bit
1316 ;; odd. Anyway, this is necessary for Emacs.
1317 (when (and (looking-at "[\n\r]")
1318 (eq (char-before) ?\\)
1319 (< (point) start))
1320 (backward-char)
1321 t))))))
1322
1323 \f
1324 ;; Tools for skipping over syntactic whitespace.
1325
1326 ;; The following functions use text properties to cache searches over
1327 ;; large regions of syntactic whitespace. It works as follows:
1328 ;;
1329 ;; o If a syntactic whitespace region contains anything but simple
1330 ;; whitespace (i.e. space, tab and line breaks), the text property
1331 ;; `c-in-sws' is put over it. At places where we have stopped
1332 ;; within that region there's also a `c-is-sws' text property.
1333 ;; That since there typically are nested whitespace inside that
1334 ;; must be handled separately, e.g. whitespace inside a comment or
1335 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1336 ;; to jump to another point with that property within the same
1337 ;; `c-in-sws' region. It can be likened to a ladder where
1338 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1339 ;;
1340 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1341 ;; a "rung position" and also maybe on the first following char.
1342 ;; As many characters as can be conveniently found in this range
1343 ;; are marked, but no assumption can be made that the whole range
1344 ;; is marked (it could be clobbered by later changes, for
1345 ;; instance).
1346 ;;
1347 ;; Note that some part of the beginning of a sequence of simple
1348 ;; whitespace might be part of the end of a preceding line comment
1349 ;; or cpp directive and must not be considered part of the "rung".
1350 ;; Such whitespace is some amount of horizontal whitespace followed
1351 ;; by a newline. In the case of cpp directives it could also be
1352 ;; two newlines with horizontal whitespace between them.
1353 ;;
1354 ;; The reason to include the first following char is to cope with
1355 ;; "rung positions" that doesn't have any ordinary whitespace. If
1356 ;; `c-is-sws' is put on a token character it does not have
1357 ;; `c-in-sws' set simultaneously. That's the only case when that
1358 ;; can occur, and the reason for not extending the `c-in-sws'
1359 ;; region to cover it is that the `c-in-sws' region could then be
1360 ;; accidentally merged with a following one if the token is only
1361 ;; one character long.
1362 ;;
1363 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1364 ;; removed in the changed region. If the change was inside
1365 ;; syntactic whitespace that means that the "ladder" is broken, but
1366 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1367 ;; parts on either side and use an ordinary search only to "repair"
1368 ;; the gap.
1369 ;;
1370 ;; Special care needs to be taken if a region is removed: If there
1371 ;; are `c-in-sws' on both sides of it which do not connect inside
1372 ;; the region then they can't be joined. If e.g. a marked macro is
1373 ;; broken, syntactic whitespace inside the new text might be
1374 ;; marked. If those marks would become connected with the old
1375 ;; `c-in-sws' range around the macro then we could get a ladder
1376 ;; with one end outside the macro and the other at some whitespace
1377 ;; within it.
1378 ;;
1379 ;; The main motivation for this system is to increase the speed in
1380 ;; skipping over the large whitespace regions that can occur at the
1381 ;; top level in e.g. header files that contain a lot of comments and
1382 ;; cpp directives. For small comments inside code it's probably
1383 ;; slower than using `forward-comment' straightforwardly, but speed is
1384 ;; not a significant factor there anyway.
1385
1386 ; (defface c-debug-is-sws-face
1387 ; '((t (:background "GreenYellow")))
1388 ; "Debug face to mark the `c-is-sws' property.")
1389 ; (defface c-debug-in-sws-face
1390 ; '((t (:underline t)))
1391 ; "Debug face to mark the `c-in-sws' property.")
1392
1393 ; (defun c-debug-put-sws-faces ()
1394 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1395 ; ;; properties in the buffer.
1396 ; (interactive)
1397 ; (save-excursion
1398 ; (c-save-buffer-state (in-face)
1399 ; (goto-char (point-min))
1400 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1401 ; (point)))
1402 ; (while (progn
1403 ; (goto-char (next-single-property-change
1404 ; (point) 'c-is-sws nil (point-max)))
1405 ; (if in-face
1406 ; (progn
1407 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1408 ; (setq in-face nil))
1409 ; (setq in-face (point)))
1410 ; (not (eobp))))
1411 ; (goto-char (point-min))
1412 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1413 ; (point)))
1414 ; (while (progn
1415 ; (goto-char (next-single-property-change
1416 ; (point) 'c-in-sws nil (point-max)))
1417 ; (if in-face
1418 ; (progn
1419 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1420 ; (setq in-face nil))
1421 ; (setq in-face (point)))
1422 ; (not (eobp)))))))
1423
1424 (defmacro c-debug-sws-msg (&rest args)
1425 ;;`(message ,@args)
1426 )
1427
1428 (defmacro c-put-is-sws (beg end)
1429 ;; This macro does a hidden buffer change.
1430 `(let ((beg ,beg) (end ,end))
1431 (put-text-property beg end 'c-is-sws t)
1432 ,@(when (facep 'c-debug-is-sws-face)
1433 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1434
1435 (defmacro c-put-in-sws (beg end)
1436 ;; This macro does a hidden buffer change.
1437 `(let ((beg ,beg) (end ,end))
1438 (put-text-property beg end 'c-in-sws t)
1439 ,@(when (facep 'c-debug-is-sws-face)
1440 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1441
1442 (defmacro c-remove-is-sws (beg end)
1443 ;; This macro does a hidden buffer change.
1444 `(let ((beg ,beg) (end ,end))
1445 (remove-text-properties beg end '(c-is-sws nil))
1446 ,@(when (facep 'c-debug-is-sws-face)
1447 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1448
1449 (defmacro c-remove-in-sws (beg end)
1450 ;; This macro does a hidden buffer change.
1451 `(let ((beg ,beg) (end ,end))
1452 (remove-text-properties beg end '(c-in-sws nil))
1453 ,@(when (facep 'c-debug-is-sws-face)
1454 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1455
1456 (defmacro c-remove-is-and-in-sws (beg end)
1457 ;; This macro does a hidden buffer change.
1458 `(let ((beg ,beg) (end ,end))
1459 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1460 ,@(when (facep 'c-debug-is-sws-face)
1461 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1462 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1463
1464 (defsubst c-invalidate-sws-region-after (beg end)
1465 ;; Called from `after-change-functions'. Note that if
1466 ;; `c-forward-sws' or `c-backward-sws' are used outside
1467 ;; `c-save-buffer-state' or similar then this will remove the cache
1468 ;; properties right after they're added.
1469 ;;
1470 ;; This function does hidden buffer changes.
1471
1472 (save-excursion
1473 ;; Adjust the end to remove the properties in any following simple
1474 ;; ws up to and including the next line break, if there is any
1475 ;; after the changed region. This is necessary e.g. when a rung
1476 ;; marked empty line is converted to a line comment by inserting
1477 ;; "//" before the line break. In that case the line break would
1478 ;; keep the rung mark which could make a later `c-backward-sws'
1479 ;; move into the line comment instead of over it.
1480 (goto-char end)
1481 (skip-chars-forward " \t\f\v")
1482 (when (and (eolp) (not (eobp)))
1483 (setq end (1+ (point)))))
1484
1485 (when (and (= beg end)
1486 (get-text-property beg 'c-in-sws)
1487 (> beg (point-min))
1488 (get-text-property (1- beg) 'c-in-sws))
1489 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1490 ;; safe to keep a range that was continuous before the change. E.g:
1491 ;;
1492 ;; #define foo
1493 ;; \
1494 ;; bar
1495 ;;
1496 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1497 ;; after "foo" is removed then "bar" will become part of the cpp
1498 ;; directive instead of a syntactically relevant token. In that
1499 ;; case there's no longer syntactic ws from "#" to "b".
1500 (setq beg (1- beg)))
1501
1502 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1503 (c-remove-is-and-in-sws beg end))
1504
1505 (defun c-forward-sws ()
1506 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1507 ;;
1508 ;; This function might do hidden buffer changes.
1509
1510 (let (;; `rung-pos' is set to a position as early as possible in the
1511 ;; unmarked part of the simple ws region.
1512 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1513 rung-is-marked next-rung-is-marked simple-ws-end
1514 ;; `safe-start' is set when it's safe to cache the start position.
1515 ;; It's not set if we've initially skipped over comments and line
1516 ;; continuations since we might have gone out through the end of a
1517 ;; macro then. This provision makes `c-forward-sws' not populate the
1518 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1519 ;; more common.
1520 safe-start)
1521
1522 ;; Skip simple ws and do a quick check on the following character to see
1523 ;; if it's anything that can't start syntactic ws, so we can bail out
1524 ;; early in the majority of cases when there just are a few ws chars.
1525 (skip-chars-forward " \t\n\r\f\v")
1526 (when (looking-at c-syntactic-ws-start)
1527
1528 (setq rung-end-pos (min (1+ (point)) (point-max)))
1529 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1530 'c-is-sws t))
1531 ;; Find the last rung position to avoid setting properties in all
1532 ;; the cases when the marked rung is complete.
1533 ;; (`next-single-property-change' is certain to move at least one
1534 ;; step forward.)
1535 (setq rung-pos (1- (next-single-property-change
1536 rung-is-marked 'c-is-sws nil rung-end-pos)))
1537 ;; Got no marked rung here. Since the simple ws might have started
1538 ;; inside a line comment or cpp directive we must set `rung-pos' as
1539 ;; high as possible.
1540 (setq rung-pos (point)))
1541
1542 (while
1543 (progn
1544 (while
1545 (when (and rung-is-marked
1546 (get-text-property (point) 'c-in-sws))
1547
1548 ;; The following search is the main reason that `c-in-sws'
1549 ;; and `c-is-sws' aren't combined to one property.
1550 (goto-char (next-single-property-change
1551 (point) 'c-in-sws nil (point-max)))
1552 (unless (get-text-property (point) 'c-is-sws)
1553 ;; If the `c-in-sws' region extended past the last
1554 ;; `c-is-sws' char we have to go back a bit.
1555 (or (get-text-property (1- (point)) 'c-is-sws)
1556 (goto-char (previous-single-property-change
1557 (point) 'c-is-sws)))
1558 (backward-char))
1559
1560 (c-debug-sws-msg
1561 "c-forward-sws cached move %s -> %s (max %s)"
1562 rung-pos (point) (point-max))
1563
1564 (setq rung-pos (point))
1565 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1566 (not (eobp))))
1567
1568 ;; We'll loop here if there is simple ws after the last rung.
1569 ;; That means that there's been some change in it and it's
1570 ;; possible that we've stepped into another ladder, so extend
1571 ;; the previous one to join with it if there is one, and try to
1572 ;; use the cache again.
1573 (c-debug-sws-msg
1574 "c-forward-sws extending rung with [%s..%s] (max %s)"
1575 (1+ rung-pos) (1+ (point)) (point-max))
1576 (unless (get-text-property (point) 'c-is-sws)
1577 ;; Remove any `c-in-sws' property from the last char of
1578 ;; the rung before we mark it with `c-is-sws', so that we
1579 ;; won't connect with the remains of a broken "ladder".
1580 (c-remove-in-sws (point) (1+ (point))))
1581 (c-put-is-sws (1+ rung-pos)
1582 (1+ (point)))
1583 (c-put-in-sws rung-pos
1584 (setq rung-pos (point)
1585 last-put-in-sws-pos rung-pos)))
1586
1587 (setq simple-ws-end (point))
1588 (c-forward-comments)
1589
1590 (cond
1591 ((/= (point) simple-ws-end)
1592 ;; Skipped over comments. Don't cache at eob in case the buffer
1593 ;; is narrowed.
1594 (not (eobp)))
1595
1596 ((save-excursion
1597 (and c-opt-cpp-prefix
1598 (looking-at c-opt-cpp-start)
1599 (progn (skip-chars-backward " \t")
1600 (bolp))
1601 (or (bobp)
1602 (progn (backward-char)
1603 (not (eq (char-before) ?\\))))))
1604 ;; Skip a preprocessor directive.
1605 (end-of-line)
1606 (while (and (eq (char-before) ?\\)
1607 (= (forward-line 1) 0))
1608 (end-of-line))
1609 (forward-line 1)
1610 (setq safe-start t)
1611 ;; Don't cache at eob in case the buffer is narrowed.
1612 (not (eobp)))))
1613
1614 ;; We've searched over a piece of non-white syntactic ws. See if this
1615 ;; can be cached.
1616 (setq next-rung-pos (point))
1617 (skip-chars-forward " \t\n\r\f\v")
1618 (setq rung-end-pos (min (1+ (point)) (point-max)))
1619
1620 (if (or
1621 ;; Cache if we haven't skipped comments only, and if we started
1622 ;; either from a marked rung or from a completely uncached
1623 ;; position.
1624 (and safe-start
1625 (or rung-is-marked
1626 (not (get-text-property simple-ws-end 'c-in-sws))))
1627
1628 ;; See if there's a marked rung in the encountered simple ws. If
1629 ;; so then we can cache, unless `safe-start' is nil. Even then
1630 ;; we need to do this to check if the cache can be used for the
1631 ;; next step.
1632 (and (setq next-rung-is-marked
1633 (text-property-any next-rung-pos rung-end-pos
1634 'c-is-sws t))
1635 safe-start))
1636
1637 (progn
1638 (c-debug-sws-msg
1639 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1640 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1641 (point-max))
1642
1643 ;; Remove the properties for any nested ws that might be cached.
1644 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1645 ;; anyway.
1646 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1647 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1648 (c-put-is-sws rung-pos
1649 (1+ simple-ws-end))
1650 (setq rung-is-marked t))
1651 (c-put-in-sws rung-pos
1652 (setq rung-pos (point)
1653 last-put-in-sws-pos rung-pos))
1654 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1655 ;; Remove any `c-in-sws' property from the last char of
1656 ;; the rung before we mark it with `c-is-sws', so that we
1657 ;; won't connect with the remains of a broken "ladder".
1658 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1659 (c-put-is-sws next-rung-pos
1660 rung-end-pos))
1661
1662 (c-debug-sws-msg
1663 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1664 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1665 (point-max))
1666
1667 ;; Set `rung-pos' for the next rung. It's the same thing here as
1668 ;; initially, except that the rung position is set as early as
1669 ;; possible since we can't be in the ending ws of a line comment or
1670 ;; cpp directive now.
1671 (if (setq rung-is-marked next-rung-is-marked)
1672 (setq rung-pos (1- (next-single-property-change
1673 rung-is-marked 'c-is-sws nil rung-end-pos)))
1674 (setq rung-pos next-rung-pos))
1675 (setq safe-start t)))
1676
1677 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1678 ;; another one after the point (which might occur when editing inside a
1679 ;; comment or macro).
1680 (when (eq last-put-in-sws-pos (point))
1681 (cond ((< last-put-in-sws-pos (point-max))
1682 (c-debug-sws-msg
1683 "c-forward-sws clearing at %s for cache separation"
1684 last-put-in-sws-pos)
1685 (c-remove-in-sws last-put-in-sws-pos
1686 (1+ last-put-in-sws-pos)))
1687 (t
1688 ;; If at eob we have to clear the last character before the end
1689 ;; instead since the buffer might be narrowed and there might
1690 ;; be a `c-in-sws' after (point-max). In this case it's
1691 ;; necessary to clear both properties.
1692 (c-debug-sws-msg
1693 "c-forward-sws clearing thoroughly at %s for cache separation"
1694 (1- last-put-in-sws-pos))
1695 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1696 last-put-in-sws-pos))))
1697 )))
1698
1699 (defun c-backward-sws ()
1700 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1701 ;;
1702 ;; This function might do hidden buffer changes.
1703
1704 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1705 ;; part of the simple ws region.
1706 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1707 rung-is-marked simple-ws-beg cmt-skip-pos)
1708
1709 ;; Skip simple horizontal ws and do a quick check on the preceding
1710 ;; character to see if it's anying that can't end syntactic ws, so we can
1711 ;; bail out early in the majority of cases when there just are a few ws
1712 ;; chars. Newlines are complicated in the backward direction, so we can't
1713 ;; skip over them.
1714 (skip-chars-backward " \t\f")
1715 (when (and (not (bobp))
1716 (save-excursion
1717 (backward-char)
1718 (looking-at c-syntactic-ws-end)))
1719
1720 ;; Try to find a rung position in the simple ws preceding point, so that
1721 ;; we can get a cache hit even if the last bit of the simple ws has
1722 ;; changed recently.
1723 (setq simple-ws-beg (point))
1724 (skip-chars-backward " \t\n\r\f\v")
1725 (if (setq rung-is-marked (text-property-any
1726 (point) (min (1+ rung-pos) (point-max))
1727 'c-is-sws t))
1728 ;; `rung-pos' will be the earliest marked position, which means that
1729 ;; there might be later unmarked parts in the simple ws region.
1730 ;; It's not worth the effort to fix that; the last part of the
1731 ;; simple ws is also typically edited often, so it could be wasted.
1732 (goto-char (setq rung-pos rung-is-marked))
1733 (goto-char simple-ws-beg))
1734
1735 (while
1736 (progn
1737 (while
1738 (when (and rung-is-marked
1739 (not (bobp))
1740 (get-text-property (1- (point)) 'c-in-sws))
1741
1742 ;; The following search is the main reason that `c-in-sws'
1743 ;; and `c-is-sws' aren't combined to one property.
1744 (goto-char (previous-single-property-change
1745 (point) 'c-in-sws nil (point-min)))
1746 (unless (get-text-property (point) 'c-is-sws)
1747 ;; If the `c-in-sws' region extended past the first
1748 ;; `c-is-sws' char we have to go forward a bit.
1749 (goto-char (next-single-property-change
1750 (point) 'c-is-sws)))
1751
1752 (c-debug-sws-msg
1753 "c-backward-sws cached move %s <- %s (min %s)"
1754 (point) rung-pos (point-min))
1755
1756 (setq rung-pos (point))
1757 (if (and (< (min (skip-chars-backward " \t\f\v")
1758 (progn
1759 (setq simple-ws-beg (point))
1760 (skip-chars-backward " \t\n\r\f\v")))
1761 0)
1762 (setq rung-is-marked
1763 (text-property-any (point) rung-pos
1764 'c-is-sws t)))
1765 t
1766 (goto-char simple-ws-beg)
1767 nil))
1768
1769 ;; We'll loop here if there is simple ws before the first rung.
1770 ;; That means that there's been some change in it and it's
1771 ;; possible that we've stepped into another ladder, so extend
1772 ;; the previous one to join with it if there is one, and try to
1773 ;; use the cache again.
1774 (c-debug-sws-msg
1775 "c-backward-sws extending rung with [%s..%s] (min %s)"
1776 rung-is-marked rung-pos (point-min))
1777 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1778 ;; Remove any `c-in-sws' property from the last char of
1779 ;; the rung before we mark it with `c-is-sws', so that we
1780 ;; won't connect with the remains of a broken "ladder".
1781 (c-remove-in-sws (1- rung-pos) rung-pos))
1782 (c-put-is-sws rung-is-marked
1783 rung-pos)
1784 (c-put-in-sws rung-is-marked
1785 (1- rung-pos))
1786 (setq rung-pos rung-is-marked
1787 last-put-in-sws-pos rung-pos))
1788
1789 (c-backward-comments)
1790 (setq cmt-skip-pos (point))
1791
1792 (cond
1793 ((and c-opt-cpp-prefix
1794 (/= cmt-skip-pos simple-ws-beg)
1795 (c-beginning-of-macro))
1796 ;; Inside a cpp directive. See if it should be skipped over.
1797 (let ((cpp-beg (point)))
1798
1799 ;; Move back over all line continuations in the region skipped
1800 ;; over by `c-backward-comments'. If we go past it then we
1801 ;; started inside the cpp directive.
1802 (goto-char simple-ws-beg)
1803 (beginning-of-line)
1804 (while (and (> (point) cmt-skip-pos)
1805 (progn (backward-char)
1806 (eq (char-before) ?\\)))
1807 (beginning-of-line))
1808
1809 (if (< (point) cmt-skip-pos)
1810 ;; Don't move past the cpp directive if we began inside
1811 ;; it. Note that the position at the end of the last line
1812 ;; of the macro is also considered to be within it.
1813 (progn (goto-char cmt-skip-pos)
1814 nil)
1815
1816 ;; It's worthwhile to spend a little bit of effort on finding
1817 ;; the end of the macro, to get a good `simple-ws-beg'
1818 ;; position for the cache. Note that `c-backward-comments'
1819 ;; could have stepped over some comments before going into
1820 ;; the macro, and then `simple-ws-beg' must be kept on the
1821 ;; same side of those comments.
1822 (goto-char simple-ws-beg)
1823 (skip-chars-backward " \t\n\r\f\v")
1824 (if (eq (char-before) ?\\)
1825 (forward-char))
1826 (forward-line 1)
1827 (if (< (point) simple-ws-beg)
1828 ;; Might happen if comments after the macro were skipped
1829 ;; over.
1830 (setq simple-ws-beg (point)))
1831
1832 (goto-char cpp-beg)
1833 t)))
1834
1835 ((/= (save-excursion
1836 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1837 (setq next-rung-pos (point)))
1838 simple-ws-beg)
1839 ;; Skipped over comments. Must put point at the end of
1840 ;; the simple ws at point since we might be after a line
1841 ;; comment or cpp directive that's been partially
1842 ;; narrowed out, and we can't risk marking the simple ws
1843 ;; at the end of it.
1844 (goto-char next-rung-pos)
1845 t)))
1846
1847 ;; We've searched over a piece of non-white syntactic ws. See if this
1848 ;; can be cached.
1849 (setq next-rung-pos (point))
1850 (skip-chars-backward " \t\f\v")
1851
1852 (if (or
1853 ;; Cache if we started either from a marked rung or from a
1854 ;; completely uncached position.
1855 rung-is-marked
1856 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1857
1858 ;; Cache if there's a marked rung in the encountered simple ws.
1859 (save-excursion
1860 (skip-chars-backward " \t\n\r\f\v")
1861 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1862 'c-is-sws t)))
1863
1864 (progn
1865 (c-debug-sws-msg
1866 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1867 (point) (1+ next-rung-pos)
1868 simple-ws-beg (min (1+ rung-pos) (point-max))
1869 (point-min))
1870
1871 ;; Remove the properties for any nested ws that might be cached.
1872 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1873 ;; anyway.
1874 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1875 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1876 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1877 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1878 ;; Remove any `c-in-sws' property from the last char of
1879 ;; the rung before we mark it with `c-is-sws', so that we
1880 ;; won't connect with the remains of a broken "ladder".
1881 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1882 (c-put-is-sws simple-ws-beg
1883 rung-end-pos)
1884 (setq rung-is-marked t)))
1885 (c-put-in-sws (setq simple-ws-beg (point)
1886 last-put-in-sws-pos simple-ws-beg)
1887 rung-pos)
1888 (c-put-is-sws (setq rung-pos simple-ws-beg)
1889 (1+ next-rung-pos)))
1890
1891 (c-debug-sws-msg
1892 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1893 (point) (1+ next-rung-pos)
1894 simple-ws-beg (min (1+ rung-pos) (point-max))
1895 (point-min))
1896 (setq rung-pos next-rung-pos
1897 simple-ws-beg (point))
1898 ))
1899
1900 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1901 ;; another one before the point (which might occur when editing inside a
1902 ;; comment or macro).
1903 (when (eq last-put-in-sws-pos (point))
1904 (cond ((< (point-min) last-put-in-sws-pos)
1905 (c-debug-sws-msg
1906 "c-backward-sws clearing at %s for cache separation"
1907 (1- last-put-in-sws-pos))
1908 (c-remove-in-sws (1- last-put-in-sws-pos)
1909 last-put-in-sws-pos))
1910 ((> (point-min) 1)
1911 ;; If at bob and the buffer is narrowed, we have to clear the
1912 ;; character we're standing on instead since there might be a
1913 ;; `c-in-sws' before (point-min). In this case it's necessary
1914 ;; to clear both properties.
1915 (c-debug-sws-msg
1916 "c-backward-sws clearing thoroughly at %s for cache separation"
1917 last-put-in-sws-pos)
1918 (c-remove-is-and-in-sws last-put-in-sws-pos
1919 (1+ last-put-in-sws-pos)))))
1920 )))
1921
1922 \f
1923 ;; Other whitespace tools
1924 (defun c-partial-ws-p (beg end)
1925 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1926 ;; region? This is a "heuristic" function. .....
1927 ;;
1928 ;; The motivation for the second bit is to check whether removing this
1929 ;; region would coalesce two symbols.
1930 ;;
1931 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1932 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1933 (save-excursion
1934 (let ((end+1 (min (1+ end) (point-max))))
1935 (or (progn (goto-char (max (point-min) (1- beg)))
1936 (c-skip-ws-forward end)
1937 (eq (point) end))
1938 (progn (goto-char beg)
1939 (c-skip-ws-forward end+1)
1940 (eq (point) end+1))))))
1941 \f
1942 ;; A system for finding noteworthy parens before the point.
1943
1944 (defvar c-state-cache nil)
1945 (make-variable-buffer-local 'c-state-cache)
1946 ;; The state cache used by `c-parse-state' to cut down the amount of
1947 ;; searching. It's the result from some earlier `c-parse-state' call.
1948 ;;
1949 ;; The use of the cached info is more effective if the next
1950 ;; `c-parse-state' call is on a line close by the one the cached state
1951 ;; was made at; the cache can actually slow down a little if the
1952 ;; cached state was made very far back in the buffer. The cache is
1953 ;; most effective if `c-parse-state' is used on each line while moving
1954 ;; forward.
1955
1956 (defvar c-state-cache-start 1)
1957 (make-variable-buffer-local 'c-state-cache-start)
1958 ;; This is (point-min) when `c-state-cache' was calculated, since a
1959 ;; change of narrowing is likely to affect the parens that are visible
1960 ;; before the point.
1961
1962 (defvar c-state-cache-good-pos 1)
1963 (make-variable-buffer-local 'c-state-cache-good-pos)
1964 ;; This is a position where `c-state-cache' is known to be correct.
1965 ;; It's a position inside one of the recorded unclosed parens or the
1966 ;; top level, but not further nested inside any literal or subparen
1967 ;; that is closed before the last recorded position.
1968 ;;
1969 ;; The exact position is chosen to try to be close to yet earlier than
1970 ;; the position where `c-state-cache' will be called next. Right now
1971 ;; the heuristic is to set it to the position after the last found
1972 ;; closing paren (of any type) before the line on which
1973 ;; `c-parse-state' was called. That is chosen primarily to work well
1974 ;; with refontification of the current line.
1975
1976 (defsubst c-invalidate-state-cache (pos)
1977 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1978 ;; at POS or higher. This is much like `c-whack-state-after', but
1979 ;; it never changes a paren pair element into an open paren element.
1980 ;; Doing that would mean that the new open paren wouldn't have the
1981 ;; required preceding paren pair element.
1982 (while (and (or c-state-cache
1983 (when (< pos c-state-cache-good-pos)
1984 (setq c-state-cache-good-pos 1)
1985 nil))
1986 (let ((elem (car c-state-cache)))
1987 (if (consp elem)
1988 (or (< pos (cdr elem))
1989 (when (< pos c-state-cache-good-pos)
1990 (setq c-state-cache-good-pos (cdr elem))
1991 nil))
1992 (or (<= pos elem)
1993 (when (< pos c-state-cache-good-pos)
1994 (setq c-state-cache-good-pos (1+ elem))
1995 nil)))))
1996 (setq c-state-cache (cdr c-state-cache))))
1997
1998 (defun c-get-fallback-start-pos (here)
1999 ;; Return the start position for building `c-state-cache' from
2000 ;; scratch.
2001 (save-excursion
2002 ;; Go back 2 bods, but ignore any bogus positions returned by
2003 ;; beginning-of-defun (i.e. open paren in column zero).
2004 (goto-char here)
2005 (let ((cnt 2))
2006 (while (not (or (bobp) (zerop cnt)))
2007 (c-beginning-of-defun-1)
2008 (if (eq (char-after) ?\{)
2009 (setq cnt (1- cnt)))))
2010 (point)))
2011
2012 (defun c-parse-state ()
2013 ;; Find and record all noteworthy parens between some good point
2014 ;; earlier in the file and point. That good point is at least the
2015 ;; beginning of the top-level construct we are in, or the beginning
2016 ;; of the preceding top-level construct if we aren't in one.
2017 ;;
2018 ;; The returned value is a list of the noteworthy parens with the
2019 ;; last one first. If an element in the list is an integer, it's
2020 ;; the position of an open paren which has not been closed before
2021 ;; the point. If an element is a cons, it gives the position of a
2022 ;; closed brace paren pair; the car is the start paren position and
2023 ;; the cdr is the position following the closing paren. Only the
2024 ;; last closed brace paren pair before each open paren and before
2025 ;; the point is recorded, and thus the state never contains two cons
2026 ;; elements in succession.
2027 ;;
2028 ;; Currently no characters which are given paren syntax with the
2029 ;; syntax-table property are recorded, i.e. angle bracket arglist
2030 ;; parens are never present here. Note that this might change.
2031 ;;
2032 ;; BUG: This function doesn't cope entirely well with unbalanced
2033 ;; parens in macros. E.g. in the following case the brace before
2034 ;; the macro isn't balanced with the one after it:
2035 ;;
2036 ;; {
2037 ;; #define X {
2038 ;; }
2039 ;;
2040 ;; This function might do hidden buffer changes.
2041
2042 (save-restriction
2043 (let* ((here (point))
2044 (here-bol (c-point 'bol))
2045 (c-macro-start (c-query-macro-start))
2046 (in-macro-start (or c-macro-start (point)))
2047 old-state last-pos brace-pair-open brace-pair-close
2048 pos save-pos)
2049 (c-invalidate-state-cache here)
2050
2051 ;; If the minimum position has changed due to narrowing then we
2052 ;; have to fix the tail of `c-state-cache' accordingly.
2053 (unless (= c-state-cache-start (point-min))
2054 (if (> (point-min) c-state-cache-start)
2055 ;; If point-min has moved forward then we just need to cut
2056 ;; off a bit of the tail.
2057 (let ((ptr (cons nil c-state-cache)) elem)
2058 (while (and (setq elem (car-safe (cdr ptr)))
2059 (>= (if (consp elem) (car elem) elem)
2060 (point-min)))
2061 (setq ptr (cdr ptr)))
2062 (when (consp ptr)
2063 (if (eq (cdr ptr) c-state-cache)
2064 (setq c-state-cache nil
2065 c-state-cache-good-pos 1)
2066 (setcdr ptr nil))))
2067 ;; If point-min has moved backward then we drop the state
2068 ;; completely. It's possible to do a better job here and
2069 ;; recalculate the top only.
2070 (setq c-state-cache nil
2071 c-state-cache-good-pos 1))
2072 (setq c-state-cache-start (point-min)))
2073
2074 ;; Get the latest position we know are directly inside the
2075 ;; closest containing paren of the cached state.
2076 (setq last-pos (and c-state-cache
2077 (if (consp (car c-state-cache))
2078 (cdr (car c-state-cache))
2079 (1+ (car c-state-cache)))))
2080 (if (or (not last-pos)
2081 (< last-pos c-state-cache-good-pos))
2082 (setq last-pos c-state-cache-good-pos)
2083 ;; Take the opportunity to move the cached good position
2084 ;; further down.
2085 (if (< last-pos here-bol)
2086 (setq c-state-cache-good-pos last-pos)))
2087
2088 ;; Check if `last-pos' is in a macro. If it is, and we're not
2089 ;; in the same macro, we must discard everything on
2090 ;; `c-state-cache' that is inside the macro before using it.
2091 (save-excursion
2092 (goto-char last-pos)
2093 (when (and (c-beginning-of-macro)
2094 (/= (point) in-macro-start))
2095 (c-invalidate-state-cache (point))
2096 ;; Set `last-pos' again just like above except that there's
2097 ;; no use looking at `c-state-cache-good-pos' here.
2098 (setq last-pos (if c-state-cache
2099 (if (consp (car c-state-cache))
2100 (cdr (car c-state-cache))
2101 (1+ (car c-state-cache)))
2102 1))))
2103
2104 ;; If we've moved very far from the last cached position then
2105 ;; it's probably better to redo it from scratch, otherwise we
2106 ;; might spend a lot of time searching from `last-pos' down to
2107 ;; here.
2108 (when (< last-pos (- here 20000))
2109 ;; First get the fallback start position. If it turns out
2110 ;; that it's so far back that the cached state is closer then
2111 ;; we'll keep it afterall.
2112 (setq pos (c-get-fallback-start-pos here))
2113 (if (<= pos last-pos)
2114 (setq pos nil)
2115 (setq last-pos nil
2116 c-state-cache nil
2117 c-state-cache-good-pos 1)))
2118
2119 ;; Find the start position for the forward search. (Can't
2120 ;; search in the backward direction since the point might be in
2121 ;; some kind of literal.)
2122
2123 (unless pos
2124 (setq old-state c-state-cache)
2125
2126 ;; There's a cached state with a containing paren. Pop off
2127 ;; the stale containing sexps from it by going forward out of
2128 ;; parens as far as possible.
2129 (narrow-to-region (point-min) here)
2130 (let (placeholder pair-beg)
2131 (while (and c-state-cache
2132 (setq placeholder
2133 (c-up-list-forward last-pos)))
2134 (setq last-pos placeholder)
2135 (if (consp (car c-state-cache))
2136 (setq pair-beg (car-safe (cdr c-state-cache))
2137 c-state-cache (cdr-safe (cdr c-state-cache)))
2138 (setq pair-beg (car c-state-cache)
2139 c-state-cache (cdr c-state-cache))))
2140
2141 (when (and pair-beg (eq (char-after pair-beg) ?{))
2142 ;; The last paren pair we moved out from was a brace
2143 ;; pair. Modify the state to record this as a closed
2144 ;; pair now.
2145 (if (consp (car-safe c-state-cache))
2146 (setq c-state-cache (cdr c-state-cache)))
2147 (setq c-state-cache (cons (cons pair-beg last-pos)
2148 c-state-cache))))
2149
2150 ;; Check if the preceding balanced paren is within a
2151 ;; macro; it should be ignored if we're outside the
2152 ;; macro. There's no need to check any further upwards;
2153 ;; if the macro contains an unbalanced opening paren then
2154 ;; we're smoked anyway.
2155 (when (and (<= (point) in-macro-start)
2156 (consp (car c-state-cache)))
2157 (save-excursion
2158 (goto-char (car (car c-state-cache)))
2159 (when (c-beginning-of-macro)
2160 (setq here (point)
2161 c-state-cache (cdr c-state-cache)))))
2162
2163 (unless (eq c-state-cache old-state)
2164 ;; Have to adjust the cached good position if state has been
2165 ;; popped off.
2166 (setq c-state-cache-good-pos
2167 (if c-state-cache
2168 (if (consp (car c-state-cache))
2169 (cdr (car c-state-cache))
2170 (1+ (car c-state-cache)))
2171 1)
2172 old-state c-state-cache))
2173
2174 (when c-state-cache
2175 (setq pos last-pos)))
2176
2177 ;; Get the fallback start position.
2178 (unless pos
2179 (setq pos (c-get-fallback-start-pos here)
2180 c-state-cache nil
2181 c-state-cache-good-pos 1))
2182
2183 (narrow-to-region (point-min) here)
2184
2185 (while pos
2186 (setq save-pos pos
2187 brace-pair-open nil)
2188
2189 ;; Find the balanced brace pairs. This loop is hot, so it
2190 ;; does ugly tricks to go faster.
2191 (c-safe
2192 (let (set-good-pos set-brace-pair)
2193 (while t
2194 (setq last-pos nil
2195 last-pos (scan-lists pos 1 -1)) ; Might signal.
2196 (setq pos (scan-lists last-pos 1 1) ; Might signal.
2197 set-good-pos (< pos here-bol)
2198 set-brace-pair (eq (char-before last-pos) ?{))
2199
2200 ;; Update the cached good position and record the brace
2201 ;; pair, whichever is applicable for the paren we've
2202 ;; just jumped over. But first check that it isn't
2203 ;; inside a macro and the point isn't inside the same
2204 ;; one.
2205 (when (and (or set-good-pos set-brace-pair)
2206 (or (>= pos in-macro-start)
2207 (save-excursion
2208 (goto-char pos)
2209 (not (c-beginning-of-macro)))))
2210 (if set-good-pos
2211 (setq c-state-cache-good-pos pos))
2212 (if set-brace-pair
2213 (setq brace-pair-open last-pos
2214 brace-pair-close pos))))))
2215
2216 ;; Record the last brace pair.
2217 (when brace-pair-open
2218 (let ((head (car-safe c-state-cache)))
2219 (if (consp head)
2220 (progn
2221 (setcar head (1- brace-pair-open))
2222 (setcdr head brace-pair-close))
2223 (setq c-state-cache (cons (cons (1- brace-pair-open)
2224 brace-pair-close)
2225 c-state-cache)))))
2226
2227 (if last-pos
2228 ;; Prepare to loop, but record the open paren only if it's
2229 ;; outside a macro or within the same macro as point, and
2230 ;; if it is a legitimate open paren and not some character
2231 ;; that got an open paren syntax-table property.
2232 (progn
2233 (setq pos last-pos)
2234 (when (and (or (>= last-pos in-macro-start)
2235 (save-excursion
2236 (goto-char last-pos)
2237 (not (c-beginning-of-macro))))
2238 ;; Check for known types of parens that we
2239 ;; want to record. The syntax table is not to
2240 ;; be trusted here since the caller might be
2241 ;; using e.g. `c++-template-syntax-table'.
2242 (memq (char-before last-pos) '(?{ ?\( ?\[)))
2243 (if (< last-pos here-bol)
2244 (setq c-state-cache-good-pos last-pos))
2245 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
2246
2247 (if (setq last-pos (c-up-list-forward pos))
2248 ;; Found a close paren without a corresponding opening
2249 ;; one. Maybe we didn't go back far enough, so try to
2250 ;; scan backward for the start paren and then start over.
2251 (progn
2252 (setq pos (c-up-list-backward pos)
2253 c-state-cache nil
2254 c-state-cache-good-pos c-state-cache-start)
2255 (when (or (not pos)
2256 ;; Emacs (up to at least 21.2) can get confused by
2257 ;; open parens in column zero inside comments: The
2258 ;; sexp functions can then misbehave and bring us
2259 ;; back to the same point again. Check this so that
2260 ;; we don't get an infinite loop.
2261 (>= pos save-pos))
2262 (setq pos last-pos
2263 c-parsing-error
2264 (format "Unbalanced close paren at line %d"
2265 (1+ (count-lines (point-min)
2266 (c-point 'bol last-pos)))))))
2267 (setq pos nil))))
2268
2269 ;;(message "c-parse-state: %S end: %S" c-state-cache c-state-cache-good-pos)
2270 c-state-cache)))
2271
2272 ;; Debug tool to catch cache inconsistencies.
2273 (defvar c-debug-parse-state nil)
2274 (unless (fboundp 'c-real-parse-state)
2275 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
2276 (cc-bytecomp-defun c-real-parse-state)
2277 (defun c-debug-parse-state ()
2278 (let ((res1 (c-real-parse-state)) res2)
2279 (let ((c-state-cache nil)
2280 (c-state-cache-start 1)
2281 (c-state-cache-good-pos 1))
2282 (setq res2 (c-real-parse-state)))
2283 (unless (equal res1 res2)
2284 ;; The cache can actually go further back due to the ad-hoc way
2285 ;; the first paren is found, so try to whack off a bit of its
2286 ;; start before complaining.
2287 (save-excursion
2288 (goto-char (or (c-least-enclosing-brace res2) (point)))
2289 (c-beginning-of-defun-1)
2290 (while (not (or (bobp) (eq (char-after) ?{)))
2291 (c-beginning-of-defun-1))
2292 (unless (equal (c-whack-state-before (point) res1) res2)
2293 (message (concat "c-parse-state inconsistency: "
2294 "using cache: %s, from scratch: %s")
2295 res1 res2))))
2296 res1))
2297 (defun c-toggle-parse-state-debug (&optional arg)
2298 (interactive "P")
2299 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
2300 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
2301 'c-debug-parse-state
2302 'c-real-parse-state)))
2303 (c-keep-region-active))
2304 (when c-debug-parse-state
2305 (c-toggle-parse-state-debug 1))
2306
2307 (defun c-whack-state-before (bufpos paren-state)
2308 ;; Whack off any state information from PAREN-STATE which lies
2309 ;; before BUFPOS. Not destructive on PAREN-STATE.
2310 (let* ((newstate (list nil))
2311 (ptr newstate)
2312 car)
2313 (while paren-state
2314 (setq car (car paren-state)
2315 paren-state (cdr paren-state))
2316 (if (< (if (consp car) (car car) car) bufpos)
2317 (setq paren-state nil)
2318 (setcdr ptr (list car))
2319 (setq ptr (cdr ptr))))
2320 (cdr newstate)))
2321
2322 (defun c-whack-state-after (bufpos paren-state)
2323 ;; Whack off any state information from PAREN-STATE which lies at or
2324 ;; after BUFPOS. Not destructive on PAREN-STATE.
2325 (catch 'done
2326 (while paren-state
2327 (let ((car (car paren-state)))
2328 (if (consp car)
2329 ;; just check the car, because in a balanced brace
2330 ;; expression, it must be impossible for the corresponding
2331 ;; close brace to be before point, but the open brace to
2332 ;; be after.
2333 (if (<= bufpos (car car))
2334 nil ; whack it off
2335 (if (< bufpos (cdr car))
2336 ;; its possible that the open brace is before
2337 ;; bufpos, but the close brace is after. In that
2338 ;; case, convert this to a non-cons element. The
2339 ;; rest of the state is before bufpos, so we're
2340 ;; done.
2341 (throw 'done (cons (car car) (cdr paren-state)))
2342 ;; we know that both the open and close braces are
2343 ;; before bufpos, so we also know that everything else
2344 ;; on state is before bufpos.
2345 (throw 'done paren-state)))
2346 (if (<= bufpos car)
2347 nil ; whack it off
2348 ;; it's before bufpos, so everything else should too.
2349 (throw 'done paren-state)))
2350 (setq paren-state (cdr paren-state)))
2351 nil)))
2352
2353 (defun c-most-enclosing-brace (paren-state &optional bufpos)
2354 ;; Return the bufpos of the innermost enclosing open paren before
2355 ;; bufpos, or nil if none was found.
2356 (let (enclosingp)
2357 (or bufpos (setq bufpos 134217727))
2358 (while paren-state
2359 (setq enclosingp (car paren-state)
2360 paren-state (cdr paren-state))
2361 (if (or (consp enclosingp)
2362 (>= enclosingp bufpos))
2363 (setq enclosingp nil)
2364 (setq paren-state nil)))
2365 enclosingp))
2366
2367 (defun c-least-enclosing-brace (paren-state)
2368 ;; Return the bufpos of the outermost enclosing open paren, or nil
2369 ;; if none was found.
2370 (let (pos elem)
2371 (while paren-state
2372 (setq elem (car paren-state)
2373 paren-state (cdr paren-state))
2374 (if (integerp elem)
2375 (setq pos elem)))
2376 pos))
2377
2378 (defun c-safe-position (bufpos paren-state)
2379 ;; Return the closest "safe" position recorded on PAREN-STATE that
2380 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
2381 ;; contain any. Return nil if BUFPOS is nil, which is useful to
2382 ;; find the closest limit before a given limit that might be nil.
2383 ;;
2384 ;; A "safe" position is a position at or after a recorded open
2385 ;; paren, or after a recorded close paren. The returned position is
2386 ;; thus either the first position after a close brace, or the first
2387 ;; position after an enclosing paren, or at the enclosing paren in
2388 ;; case BUFPOS is immediately after it.
2389 (when bufpos
2390 (let (elem)
2391 (catch 'done
2392 (while paren-state
2393 (setq elem (car paren-state))
2394 (if (consp elem)
2395 (cond ((< (cdr elem) bufpos)
2396 (throw 'done (cdr elem)))
2397 ((< (car elem) bufpos)
2398 ;; See below.
2399 (throw 'done (min (1+ (car elem)) bufpos))))
2400 (if (< elem bufpos)
2401 ;; elem is the position at and not after the opening paren, so
2402 ;; we can go forward one more step unless it's equal to
2403 ;; bufpos. This is useful in some cases avoid an extra paren
2404 ;; level between the safe position and bufpos.
2405 (throw 'done (min (1+ elem) bufpos))))
2406 (setq paren-state (cdr paren-state)))))))
2407
2408 (defun c-beginning-of-syntax ()
2409 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2410 ;; goes to the closest previous point that is known to be outside
2411 ;; any string literal or comment. `c-state-cache' is used if it has
2412 ;; a position in the vicinity.
2413 (let* ((paren-state c-state-cache)
2414 elem
2415
2416 (pos (catch 'done
2417 ;; Note: Similar code in `c-safe-position'. The
2418 ;; difference is that we accept a safe position at
2419 ;; the point and don't bother to go forward past open
2420 ;; parens.
2421 (while paren-state
2422 (setq elem (car paren-state))
2423 (if (consp elem)
2424 (cond ((<= (cdr elem) (point))
2425 (throw 'done (cdr elem)))
2426 ((<= (car elem) (point))
2427 (throw 'done (car elem))))
2428 (if (<= elem (point))
2429 (throw 'done elem)))
2430 (setq paren-state (cdr paren-state)))
2431 (point-min))))
2432
2433 (if (> pos (- (point) 4000))
2434 (goto-char pos)
2435 ;; The position is far back. Try `c-beginning-of-defun-1'
2436 ;; (although we can't be entirely sure it will go to a position
2437 ;; outside a comment or string in current emacsen). FIXME:
2438 ;; Consult `syntax-ppss' here.
2439 (c-beginning-of-defun-1)
2440 (if (< (point) pos)
2441 (goto-char pos)))))
2442
2443 \f
2444 ;; Tools for scanning identifiers and other tokens.
2445
2446 (defun c-on-identifier ()
2447 "Return non-nil if the point is on or directly after an identifier.
2448 Keywords are recognized and not considered identifiers. If an
2449 identifier is detected, the returned value is its starting position.
2450 If an identifier ends at the point and another begins at it \(can only
2451 happen in Pike) then the point for the preceding one is returned.
2452
2453 Note that this function might do hidden buffer changes. See the
2454 comment at the start of cc-engine.el for more info."
2455
2456 ;; FIXME: Shouldn't this function handle "operator" in C++?
2457
2458 (save-excursion
2459 (skip-syntax-backward "w_")
2460
2461 (or
2462
2463 ;; Check for a normal (non-keyword) identifier.
2464 (and (looking-at c-symbol-start)
2465 (not (looking-at c-keywords-regexp))
2466 (point))
2467
2468 (when (c-major-mode-is 'pike-mode)
2469 ;; Handle the `<operator> syntax in Pike.
2470 (let ((pos (point)))
2471 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2472 (and (if (< (skip-chars-backward "`") 0)
2473 t
2474 (goto-char pos)
2475 (eq (char-after) ?\`))
2476 (looking-at c-symbol-key)
2477 (>= (match-end 0) pos)
2478 (point))))
2479
2480 ;; Handle the "operator +" syntax in C++.
2481 (when (and c-overloadable-operators-regexp
2482 (= (c-backward-token-2 0) 0))
2483
2484 (cond ((and (looking-at c-overloadable-operators-regexp)
2485 (or (not c-opt-op-identifier-prefix)
2486 (and (= (c-backward-token-2 1) 0)
2487 (looking-at c-opt-op-identifier-prefix))))
2488 (point))
2489
2490 ((save-excursion
2491 (and c-opt-op-identifier-prefix
2492 (looking-at c-opt-op-identifier-prefix)
2493 (= (c-forward-token-2 1) 0)
2494 (looking-at c-overloadable-operators-regexp)))
2495 (point))))
2496
2497 )))
2498
2499 (defsubst c-simple-skip-symbol-backward ()
2500 ;; If the point is at the end of a symbol then skip backward to the
2501 ;; beginning of it. Don't move otherwise. Return non-nil if point
2502 ;; moved.
2503 ;;
2504 ;; This function might do hidden buffer changes.
2505 (or (< (skip-syntax-backward "w_") 0)
2506 (and (c-major-mode-is 'pike-mode)
2507 ;; Handle the `<operator> syntax in Pike.
2508 (let ((pos (point)))
2509 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2510 (< (skip-chars-backward "`") 0)
2511 (looking-at c-symbol-key)
2512 (>= (match-end 0) pos))
2513 t
2514 (goto-char pos)
2515 nil)))))
2516
2517 (defun c-beginning-of-current-token (&optional back-limit)
2518 ;; Move to the beginning of the current token. Do not move if not
2519 ;; in the middle of one. BACK-LIMIT may be used to bound the
2520 ;; backward search; if given it's assumed to be at the boundary
2521 ;; between two tokens. Return non-nil if the point is moved, nil
2522 ;; otherwise.
2523 ;;
2524 ;; This function might do hidden buffer changes.
2525 (let ((start (point)))
2526 (if (looking-at "\\w\\|\\s_")
2527 (skip-syntax-backward "w_" back-limit)
2528 (when (< (skip-syntax-backward ".()" back-limit) 0)
2529 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2530 (match-end 0))
2531 ;; `c-nonsymbol-token-regexp' should always match
2532 ;; since we've skipped backward over punctuator
2533 ;; or paren syntax, but consume one char in case
2534 ;; it doesn't so that we don't leave point before
2535 ;; some earlier incorrect token.
2536 (1+ (point)))))
2537 (if (<= pos start)
2538 (goto-char pos))))))
2539 (< (point) start)))
2540
2541 (defun c-end-of-current-token (&optional back-limit)
2542 ;; Move to the end of the current token. Do not move if not in the
2543 ;; middle of one. BACK-LIMIT may be used to bound the backward
2544 ;; search; if given it's assumed to be at the boundary between two
2545 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2546 ;;
2547 ;; This function might do hidden buffer changes.
2548 (let ((start (point)))
2549 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2550 (skip-syntax-forward "w_"))
2551 ((< (skip-syntax-backward ".()" back-limit) 0)
2552 (while (progn
2553 (if (looking-at c-nonsymbol-token-regexp)
2554 (goto-char (match-end 0))
2555 ;; `c-nonsymbol-token-regexp' should always match since
2556 ;; we've skipped backward over punctuator or paren
2557 ;; syntax, but move forward in case it doesn't so that
2558 ;; we don't leave point earlier than we started with.
2559 (forward-char))
2560 (< (point) start)))))
2561 (> (point) start)))
2562
2563 (defconst c-jump-syntax-balanced
2564 (if (memq 'gen-string-delim c-emacs-features)
2565 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2566 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2567
2568 (defconst c-jump-syntax-unbalanced
2569 (if (memq 'gen-string-delim c-emacs-features)
2570 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2571 "\\w\\|\\s_\\|\\s\""))
2572
2573 (defun c-forward-token-2 (&optional count balanced limit)
2574 "Move forward by tokens.
2575 A token is defined as all symbols and identifiers which aren't
2576 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2577 treated properly). Point is always either left at the beginning of a
2578 token or not moved at all. COUNT specifies the number of tokens to
2579 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2580 moves to the next token beginning only if not already at one. If
2581 BALANCED is true, move over balanced parens, otherwise move into them.
2582 Also, if BALANCED is true, never move out of an enclosing paren.
2583
2584 LIMIT sets the limit for the movement and defaults to the point limit.
2585 The case when LIMIT is set in the middle of a token, comment or macro
2586 is handled correctly, i.e. the point won't be left there.
2587
2588 Return the number of tokens left to move \(positive or negative). If
2589 BALANCED is true, a move over a balanced paren counts as one. Note
2590 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2591 be returned. Thus, a return value of 0 guarantees that point is at
2592 the requested position and a return value less \(without signs) than
2593 COUNT guarantees that point is at the beginning of some token.
2594
2595 Note that this function might do hidden buffer changes. See the
2596 comment at the start of cc-engine.el for more info."
2597
2598 (or count (setq count 1))
2599 (if (< count 0)
2600 (- (c-backward-token-2 (- count) balanced limit))
2601
2602 (let ((jump-syntax (if balanced
2603 c-jump-syntax-balanced
2604 c-jump-syntax-unbalanced))
2605 (last (point))
2606 (prev (point)))
2607
2608 (if (zerop count)
2609 ;; If count is zero we should jump if in the middle of a token.
2610 (c-end-of-current-token))
2611
2612 (save-restriction
2613 (if limit (narrow-to-region (point-min) limit))
2614 (if (/= (point)
2615 (progn (c-forward-syntactic-ws) (point)))
2616 ;; Skip whitespace. Count this as a move if we did in
2617 ;; fact move.
2618 (setq count (max (1- count) 0)))
2619
2620 (if (eobp)
2621 ;; Moved out of bounds. Make sure the returned count isn't zero.
2622 (progn
2623 (if (zerop count) (setq count 1))
2624 (goto-char last))
2625
2626 ;; Use `condition-case' to avoid having the limit tests
2627 ;; inside the loop.
2628 (condition-case nil
2629 (while (and
2630 (> count 0)
2631 (progn
2632 (setq last (point))
2633 (cond ((looking-at jump-syntax)
2634 (goto-char (scan-sexps (point) 1))
2635 t)
2636 ((looking-at c-nonsymbol-token-regexp)
2637 (goto-char (match-end 0))
2638 t)
2639 ;; `c-nonsymbol-token-regexp' above should always
2640 ;; match if there are correct tokens. Try to
2641 ;; widen to see if the limit was set in the
2642 ;; middle of one, else fall back to treating
2643 ;; the offending thing as a one character token.
2644 ((and limit
2645 (save-restriction
2646 (widen)
2647 (looking-at c-nonsymbol-token-regexp)))
2648 nil)
2649 (t
2650 (forward-char)
2651 t))))
2652 (c-forward-syntactic-ws)
2653 (setq prev last
2654 count (1- count)))
2655 (error (goto-char last)))
2656
2657 (when (eobp)
2658 (goto-char prev)
2659 (setq count (1+ count)))))
2660
2661 count)))
2662
2663 (defun c-backward-token-2 (&optional count balanced limit)
2664 "Move backward by tokens.
2665 See `c-forward-token-2' for details."
2666
2667 (or count (setq count 1))
2668 (if (< count 0)
2669 (- (c-forward-token-2 (- count) balanced limit))
2670
2671 (or limit (setq limit (point-min)))
2672 (let ((jump-syntax (if balanced
2673 c-jump-syntax-balanced
2674 c-jump-syntax-unbalanced))
2675 (last (point)))
2676
2677 (if (zerop count)
2678 ;; The count is zero so try to skip to the beginning of the
2679 ;; current token.
2680 (if (> (point)
2681 (progn (c-beginning-of-current-token) (point)))
2682 (if (< (point) limit)
2683 ;; The limit is inside the same token, so return 1.
2684 (setq count 1))
2685
2686 ;; We're not in the middle of a token. If there's
2687 ;; whitespace after the point then we must move backward,
2688 ;; so set count to 1 in that case.
2689 (and (looking-at c-syntactic-ws-start)
2690 ;; If we're looking at a '#' that might start a cpp
2691 ;; directive then we have to do a more elaborate check.
2692 (or (/= (char-after) ?#)
2693 (not c-opt-cpp-prefix)
2694 (save-excursion
2695 (and (= (point)
2696 (progn (beginning-of-line)
2697 (looking-at "[ \t]*")
2698 (match-end 0)))
2699 (or (bobp)
2700 (progn (backward-char)
2701 (not (eq (char-before) ?\\)))))))
2702 (setq count 1))))
2703
2704 ;; Use `condition-case' to avoid having to check for buffer
2705 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2706 (condition-case nil
2707 (while (and
2708 (> count 0)
2709 (progn
2710 (c-backward-syntactic-ws)
2711 (backward-char)
2712 (if (looking-at jump-syntax)
2713 (goto-char (scan-sexps (1+ (point)) -1))
2714 ;; This can be very inefficient if there's a long
2715 ;; sequence of operator tokens without any separation.
2716 ;; That doesn't happen in practice, anyway.
2717 (c-beginning-of-current-token))
2718 (>= (point) limit)))
2719 (setq last (point)
2720 count (1- count)))
2721 (error (goto-char last)))
2722
2723 (if (< (point) limit)
2724 (goto-char last))
2725
2726 count)))
2727
2728 (defun c-forward-token-1 (&optional count balanced limit)
2729 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2730 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2731 characters are jumped over character by character. This function is
2732 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2733 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2734 (c-forward-token-2 count balanced limit)))
2735
2736 (defun c-backward-token-1 (&optional count balanced limit)
2737 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2738 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2739 characters are jumped over character by character. This function is
2740 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2741 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2742 (c-backward-token-2 count balanced limit)))
2743
2744 \f
2745 ;; Tools for doing searches restricted to syntactically relevant text.
2746
2747 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2748 paren-level not-inside-token
2749 lookbehind-submatch)
2750 "Like `re-search-forward', but only report matches that are found
2751 in syntactically significant text. I.e. matches in comments, macros
2752 or string literals are ignored. The start point is assumed to be
2753 outside any comment, macro or string literal, or else the content of
2754 that region is taken as syntactically significant text.
2755
2756 If PAREN-LEVEL is non-nil, an additional restriction is added to
2757 ignore matches in nested paren sexps. The search will also not go
2758 outside the current list sexp, which has the effect that if the point
2759 should be moved to BOUND when no match is found \(i.e. NOERROR is
2760 neither nil nor t), then it will be at the closing paren if the end of
2761 the current list sexp is encountered first.
2762
2763 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2764 ignored. Things like multicharacter operators and special symbols
2765 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2766 constants.
2767
2768 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2769 subexpression in REGEXP. The end of that submatch is used as the
2770 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2771 isn't used or if that subexpression didn't match then the start
2772 position of the whole match is used instead. The \"look behind\"
2773 subexpression is never tested before the starting position, so it
2774 might be a good idea to include \\=\\= as a match alternative in it.
2775
2776 Optimization note: Matches might be missed if the \"look behind\"
2777 subexpression can match the end of nonwhite syntactic whitespace,
2778 i.e. the end of comments or cpp directives. This since the function
2779 skips over such things before resuming the search. It's on the other
2780 hand not safe to assume that the \"look behind\" subexpression never
2781 matches syntactic whitespace.
2782
2783 Bug: Unbalanced parens inside cpp directives are currently not handled
2784 correctly \(i.e. they don't get ignored as they should) when
2785 PAREN-LEVEL is set.
2786
2787 Note that this function might do hidden buffer changes. See the
2788 comment at the start of cc-engine.el for more info."
2789
2790 (or bound (setq bound (point-max)))
2791 (if paren-level (setq paren-level -1))
2792
2793 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2794
2795 (let ((start (point))
2796 tmp
2797 ;; Start position for the last search.
2798 search-pos
2799 ;; The `parse-partial-sexp' state between the start position
2800 ;; and the point.
2801 state
2802 ;; The current position after the last state update. The next
2803 ;; `parse-partial-sexp' continues from here.
2804 (state-pos (point))
2805 ;; The position at which to check the state and the state
2806 ;; there. This is separate from `state-pos' since we might
2807 ;; need to back up before doing the next search round.
2808 check-pos check-state
2809 ;; Last position known to end a token.
2810 (last-token-end-pos (point-min))
2811 ;; Set when a valid match is found.
2812 found)
2813
2814 (condition-case err
2815 (while
2816 (and
2817 (progn
2818 (setq search-pos (point))
2819 (re-search-forward regexp bound noerror))
2820
2821 (progn
2822 (setq state (parse-partial-sexp
2823 state-pos (match-beginning 0) paren-level nil state)
2824 state-pos (point))
2825 (if (setq check-pos (and lookbehind-submatch
2826 (or (not paren-level)
2827 (>= (car state) 0))
2828 (match-end lookbehind-submatch)))
2829 (setq check-state (parse-partial-sexp
2830 state-pos check-pos paren-level nil state))
2831 (setq check-pos state-pos
2832 check-state state))
2833
2834 ;; NOTE: If we got a look behind subexpression and get
2835 ;; an insignificant match in something that isn't
2836 ;; syntactic whitespace (i.e. strings or in nested
2837 ;; parentheses), then we can never skip more than a
2838 ;; single character from the match start position
2839 ;; (i.e. `state-pos' here) before continuing the
2840 ;; search. That since the look behind subexpression
2841 ;; might match the end of the insignificant region in
2842 ;; the next search.
2843
2844 (cond
2845 ((elt check-state 7)
2846 ;; Match inside a line comment. Skip to eol. Use
2847 ;; `re-search-forward' instead of `skip-chars-forward' to get
2848 ;; the right bound behavior.
2849 (re-search-forward "[\n\r]" bound noerror))
2850
2851 ((elt check-state 4)
2852 ;; Match inside a block comment. Skip to the '*/'.
2853 (search-forward "*/" bound noerror))
2854
2855 ((and (not (elt check-state 5))
2856 (eq (char-before check-pos) ?/)
2857 (not (c-get-char-property (1- check-pos) 'syntax-table))
2858 (memq (char-after check-pos) '(?/ ?*)))
2859 ;; Match in the middle of the opener of a block or line
2860 ;; comment.
2861 (if (= (char-after check-pos) ?/)
2862 (re-search-forward "[\n\r]" bound noerror)
2863 (search-forward "*/" bound noerror)))
2864
2865 ;; The last `parse-partial-sexp' above might have
2866 ;; stopped short of the real check position if the end
2867 ;; of the current sexp was encountered in paren-level
2868 ;; mode. The checks above are always false in that
2869 ;; case, and since they can do better skipping in
2870 ;; lookbehind-submatch mode, we do them before
2871 ;; checking the paren level.
2872
2873 ((and paren-level
2874 (/= (setq tmp (car check-state)) 0))
2875 ;; Check the paren level first since we're short of the
2876 ;; syntactic checking position if the end of the
2877 ;; current sexp was encountered by `parse-partial-sexp'.
2878 (if (> tmp 0)
2879
2880 ;; Inside a nested paren sexp.
2881 (if lookbehind-submatch
2882 ;; See the NOTE above.
2883 (progn (goto-char state-pos) t)
2884 ;; Skip out of the paren quickly.
2885 (setq state (parse-partial-sexp state-pos bound 0 nil state)
2886 state-pos (point)))
2887
2888 ;; Have exited the current paren sexp.
2889 (if noerror
2890 (progn
2891 ;; The last `parse-partial-sexp' call above
2892 ;; has left us just after the closing paren
2893 ;; in this case, so we can modify the bound
2894 ;; to leave the point at the right position
2895 ;; upon return.
2896 (setq bound (1- (point)))
2897 nil)
2898 (signal 'search-failed (list regexp)))))
2899
2900 ((setq tmp (elt check-state 3))
2901 ;; Match inside a string.
2902 (if (or lookbehind-submatch
2903 (not (integerp tmp)))
2904 ;; See the NOTE above.
2905 (progn (goto-char state-pos) t)
2906 ;; Skip to the end of the string before continuing.
2907 (let ((ender (make-string 1 tmp)) (continue t))
2908 (while (if (search-forward ender bound noerror)
2909 (progn
2910 (setq state (parse-partial-sexp
2911 state-pos (point) nil nil state)
2912 state-pos (point))
2913 (elt state 3))
2914 (setq continue nil)))
2915 continue)))
2916
2917 ((save-excursion
2918 (save-match-data
2919 (c-beginning-of-macro start)))
2920 ;; Match inside a macro. Skip to the end of it.
2921 (c-end-of-macro)
2922 (cond ((<= (point) bound) t)
2923 (noerror nil)
2924 (t (signal 'search-failed (list regexp)))))
2925
2926 ((and not-inside-token
2927 (or (< check-pos last-token-end-pos)
2928 (< check-pos
2929 (save-excursion
2930 (goto-char check-pos)
2931 (save-match-data
2932 (c-end-of-current-token last-token-end-pos))
2933 (setq last-token-end-pos (point))))))
2934 ;; Inside a token.
2935 (if lookbehind-submatch
2936 ;; See the NOTE above.
2937 (goto-char state-pos)
2938 (goto-char (min last-token-end-pos bound))))
2939
2940 (t
2941 ;; A real match.
2942 (setq found t)
2943 nil)))
2944
2945 ;; Should loop to search again, but take care to avoid
2946 ;; looping on the same spot.
2947 (or (/= search-pos (point))
2948 (if (= (point) bound)
2949 (if noerror
2950 nil
2951 (signal 'search-failed (list regexp)))
2952 (forward-char)
2953 t))))
2954
2955 (error
2956 (goto-char start)
2957 (signal (car err) (cdr err))))
2958
2959 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2960
2961 (if found
2962 (progn
2963 (goto-char (match-end 0))
2964 (match-end 0))
2965
2966 ;; Search failed. Set point as appropriate.
2967 (if (eq noerror t)
2968 (goto-char start)
2969 (goto-char bound))
2970 nil)))
2971
2972 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
2973 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2974 i.e. don't stop at positions inside syntactic whitespace or string
2975 literals. Preprocessor directives are also ignored, with the exception
2976 of the one that the point starts within, if any. If LIMIT is given,
2977 it's assumed to be at a syntactically relevant position.
2978
2979 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
2980 sexps, and the search will also not go outside the current paren sexp.
2981 However, if LIMIT or the buffer limit is reached inside a nested paren
2982 then the point will be left at the limit.
2983
2984 Non-nil is returned if the point moved, nil otherwise.
2985
2986 Note that this function might do hidden buffer changes. See the
2987 comment at the start of cc-engine.el for more info."
2988
2989 (let ((start (point))
2990 state
2991 ;; A list of syntactically relevant positions in descending
2992 ;; order. It's used to avoid scanning repeatedly over
2993 ;; potentially large regions with `parse-partial-sexp' to verify
2994 ;; each position.
2995 safe-pos-list
2996 ;; The position at the beginning of `safe-pos-list'.
2997 safe-pos
2998 ;; The result from `c-beginning-of-macro' at the start position or the
2999 ;; start position itself if it isn't within a macro. Evaluated on
3000 ;; demand.
3001 start-macro-beg
3002 ;; The earliest position after the current one with the same paren
3003 ;; level. Used only when `paren-level' is set.
3004 (paren-level-pos (point)))
3005
3006 (while (progn
3007 (while (and
3008 (< (skip-chars-backward skip-chars limit) 0)
3009
3010 ;; Use `parse-partial-sexp' from a safe position down to
3011 ;; the point to check if it's outside comments and
3012 ;; strings.
3013 (let ((pos (point)) state-2 pps-end-pos)
3014 ;; Pick a safe position as close to the point as
3015 ;; possible.
3016 ;;
3017 ;; FIXME: Consult `syntax-ppss' here if our
3018 ;; cache doesn't give a good position.
3019 (while (and safe-pos-list
3020 (> (car safe-pos-list) (point)))
3021 (setq safe-pos-list (cdr safe-pos-list)))
3022 (unless (setq safe-pos (car-safe safe-pos-list))
3023 (setq safe-pos (max (or (c-safe-position
3024 (point) (or c-state-cache
3025 (c-parse-state)))
3026 0)
3027 (point-min))
3028 safe-pos-list (list safe-pos)))
3029
3030 ;; Cache positions along the way to use if we have to
3031 ;; back up more. We cache every closing paren on the
3032 ;; same level. If the paren cache is relevant in this
3033 ;; region then we're typically already on the same
3034 ;; level as the target position. Note that we might
3035 ;; cache positions after opening parens in case
3036 ;; safe-pos is in a nested list. That's both uncommon
3037 ;; and harmless.
3038 (while (progn
3039 (setq state (parse-partial-sexp
3040 safe-pos pos 0))
3041 (< (point) pos))
3042 (setq safe-pos (point)
3043 safe-pos-list (cons safe-pos safe-pos-list)))
3044
3045 (cond
3046 ((or (elt state 3) (elt state 4))
3047 ;; Inside string or comment. Continue search at the
3048 ;; beginning of it.
3049 (goto-char (elt state 8))
3050 t)
3051
3052 ((and paren-level
3053 (save-excursion
3054 (setq state-2 (parse-partial-sexp
3055 pos paren-level-pos -1)
3056 pps-end-pos (point))
3057 (/= (car state-2) 0)))
3058 ;; Not at the right level.
3059
3060 (if (and (< (car state-2) 0)
3061 ;; We stop above if we go out of a paren.
3062 ;; Now check whether it precedes or is
3063 ;; nested in the starting sexp.
3064 (save-excursion
3065 (setq state-2
3066 (parse-partial-sexp
3067 pps-end-pos paren-level-pos
3068 nil nil state-2))
3069 (< (car state-2) 0)))
3070
3071 ;; We've stopped short of the starting position
3072 ;; so the hit was inside a nested list. Go up
3073 ;; until we are at the right level.
3074 (condition-case nil
3075 (progn
3076 (goto-char (scan-lists pos -1
3077 (- (car state-2))))
3078 (setq paren-level-pos (point))
3079 (if (and limit (>= limit paren-level-pos))
3080 (progn
3081 (goto-char limit)
3082 nil)
3083 t))
3084 (error
3085 (goto-char (or limit (point-min)))
3086 nil))
3087
3088 ;; The hit was outside the list at the start
3089 ;; position. Go to the start of the list and exit.
3090 (goto-char (1+ (elt state-2 1)))
3091 nil))
3092
3093 ((c-beginning-of-macro limit)
3094 ;; Inside a macro.
3095 (if (< (point)
3096 (or start-macro-beg
3097 (setq start-macro-beg
3098 (save-excursion
3099 (goto-char start)
3100 (c-beginning-of-macro limit)
3101 (point)))))
3102 t
3103
3104 ;; It's inside the same macro we started in so it's
3105 ;; a relevant match.
3106 (goto-char pos)
3107 nil)))))
3108
3109 ;; If the state contains the start of the containing sexp we
3110 ;; cache that position too, so that parse-partial-sexp in the
3111 ;; next run has a bigger chance of starting at the same level
3112 ;; as the target position and thus will get more good safe
3113 ;; positions into the list.
3114 (if (elt state 1)
3115 (setq safe-pos (1+ (elt state 1))
3116 safe-pos-list (cons safe-pos safe-pos-list))))
3117
3118 (> (point)
3119 (progn
3120 ;; Skip syntactic ws afterwards so that we don't stop at the
3121 ;; end of a comment if `skip-chars' is something like "^/".
3122 (c-backward-syntactic-ws)
3123 (point)))))
3124
3125 ;; We might want to extend this with more useful return values in
3126 ;; the future.
3127 (/= (point) start)))
3128
3129 ;; The following is an alternative implementation of
3130 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3131 ;; track of the syntactic context. It turned out to be generally
3132 ;; slower than the one above which uses forward checks from earlier
3133 ;; safe positions.
3134 ;;
3135 ;;(defconst c-ssb-stop-re
3136 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3137 ;; ;; stop at to avoid going into comments and literals.
3138 ;; (concat
3139 ;; ;; Match comment end syntax and string literal syntax. Also match
3140 ;; ;; '/' for block comment endings (not covered by comment end
3141 ;; ;; syntax).
3142 ;; "\\s>\\|/\\|\\s\""
3143 ;; (if (memq 'gen-string-delim c-emacs-features)
3144 ;; "\\|\\s|"
3145 ;; "")
3146 ;; (if (memq 'gen-comment-delim c-emacs-features)
3147 ;; "\\|\\s!"
3148 ;; "")))
3149 ;;
3150 ;;(defconst c-ssb-stop-paren-re
3151 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3152 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3153 ;;
3154 ;;(defconst c-ssb-sexp-end-re
3155 ;; ;; Regexp matching the ending syntax of a complex sexp.
3156 ;; (concat c-string-limit-regexp "\\|\\s)"))
3157 ;;
3158 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3159 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3160 ;;i.e. don't stop at positions inside syntactic whitespace or string
3161 ;;literals. Preprocessor directives are also ignored. However, if the
3162 ;;point is within a comment, string literal or preprocessor directory to
3163 ;;begin with, its contents is treated as syntactically relevant chars.
3164 ;;If LIMIT is given, it limits the backward search and the point will be
3165 ;;left there if no earlier position is found.
3166 ;;
3167 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3168 ;;sexps, and the search will also not go outside the current paren sexp.
3169 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3170 ;;then the point will be left at the limit.
3171 ;;
3172 ;;Non-nil is returned if the point moved, nil otherwise.
3173 ;;
3174 ;;Note that this function might do hidden buffer changes. See the
3175 ;;comment at the start of cc-engine.el for more info."
3176 ;;
3177 ;; (save-restriction
3178 ;; (when limit
3179 ;; (narrow-to-region limit (point-max)))
3180 ;;
3181 ;; (let ((start (point)))
3182 ;; (catch 'done
3183 ;; (while (let ((last-pos (point))
3184 ;; (stop-pos (progn
3185 ;; (skip-chars-backward skip-chars)
3186 ;; (point))))
3187 ;;
3188 ;; ;; Skip back over the same region as
3189 ;; ;; `skip-chars-backward' above, but keep to
3190 ;; ;; syntactically relevant positions.
3191 ;; (goto-char last-pos)
3192 ;; (while (and
3193 ;; ;; `re-search-backward' with a single char regexp
3194 ;; ;; should be fast.
3195 ;; (re-search-backward
3196 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3197 ;; stop-pos 'move)
3198 ;;
3199 ;; (progn
3200 ;; (cond
3201 ;; ((looking-at "\\s(")
3202 ;; ;; `paren-level' is set and we've found the
3203 ;; ;; start of the containing paren.
3204 ;; (forward-char)
3205 ;; (throw 'done t))
3206 ;;
3207 ;; ((looking-at c-ssb-sexp-end-re)
3208 ;; ;; We're at the end of a string literal or paren
3209 ;; ;; sexp (if `paren-level' is set).
3210 ;; (forward-char)
3211 ;; (condition-case nil
3212 ;; (c-backward-sexp)
3213 ;; (error
3214 ;; (goto-char limit)
3215 ;; (throw 'done t))))
3216 ;;
3217 ;; (t
3218 ;; (forward-char)
3219 ;; ;; At the end of some syntactic ws or possibly
3220 ;; ;; after a plain '/' operator.
3221 ;; (let ((pos (point)))
3222 ;; (c-backward-syntactic-ws)
3223 ;; (if (= pos (point))
3224 ;; ;; Was a plain '/' operator. Go past it.
3225 ;; (backward-char)))))
3226 ;;
3227 ;; (> (point) stop-pos))))
3228 ;;
3229 ;; ;; Now the point is either at `stop-pos' or at some
3230 ;; ;; position further back if `stop-pos' was at a
3231 ;; ;; syntactically irrelevant place.
3232 ;;
3233 ;; ;; Skip additional syntactic ws so that we don't stop
3234 ;; ;; at the end of a comment if `skip-chars' is
3235 ;; ;; something like "^/".
3236 ;; (c-backward-syntactic-ws)
3237 ;;
3238 ;; (< (point) stop-pos))))
3239 ;;
3240 ;; ;; We might want to extend this with more useful return values
3241 ;; ;; in the future.
3242 ;; (/= (point) start))))
3243
3244 \f
3245 ;; Tools for handling comments and string literals.
3246
3247 (defun c-slow-in-literal (&optional lim detect-cpp)
3248 "Return the type of literal point is in, if any.
3249 The return value is `c' if in a C-style comment, `c++' if in a C++
3250 style comment, `string' if in a string literal, `pound' if DETECT-CPP
3251 is non-nil and in a preprocessor line, or nil if somewhere else.
3252 Optional LIM is used as the backward limit of the search. If omitted,
3253 or nil, `c-beginning-of-defun' is used.
3254
3255 The last point calculated is cached if the cache is enabled, i.e. if
3256 `c-in-literal-cache' is bound to a two element vector.
3257
3258 Note that this function might do hidden buffer changes. See the
3259 comment at the start of cc-engine.el for more info."
3260
3261 (if (and (vectorp c-in-literal-cache)
3262 (= (point) (aref c-in-literal-cache 0)))
3263 (aref c-in-literal-cache 1)
3264 (let ((rtn (save-excursion
3265 (let* ((pos (point))
3266 (lim (or lim (progn
3267 (c-beginning-of-syntax)
3268 (point))))
3269 (state (parse-partial-sexp lim pos)))
3270 (cond
3271 ((elt state 3) 'string)
3272 ((elt state 4) (if (elt state 7) 'c++ 'c))
3273 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
3274 (t nil))))))
3275 ;; cache this result if the cache is enabled
3276 (if (not c-in-literal-cache)
3277 (setq c-in-literal-cache (vector (point) rtn)))
3278 rtn)))
3279
3280 ;; XEmacs has a built-in function that should make this much quicker.
3281 ;; I don't think we even need the cache, which makes our lives more
3282 ;; complicated anyway. In this case, lim is only used to detect
3283 ;; cpp directives.
3284 ;;
3285 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
3286 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
3287 ;; Xemacs 21.4.4. It manifested itself thus:
3288 ;;
3289 ;; Starting with an empty AWK Mode buffer, type
3290 ;; /regexp/ {<C-j>
3291 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
3292 ;;
3293 ;; AWK Mode is designed such that when the first / is typed, it gets the
3294 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
3295 ;; are given the s-t property "string". However, buffer-syntactic-context
3296 ;; fails to take account of the change of the s-t property on the opening / to
3297 ;; "string", and reports that the { is within a string started by the second /.
3298 ;;
3299 ;; The workaround for this is for the AWK Mode initialisation to switch the
3300 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
3301 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
3302 ;;
3303 ;; (Alan Mackenzie, 2003/4/30).
3304
3305 (defun c-fast-in-literal (&optional lim detect-cpp)
3306 ;; This function might do hidden buffer changes.
3307 (let ((context (buffer-syntactic-context)))
3308 (cond
3309 ((eq context 'string) 'string)
3310 ((eq context 'comment) 'c++)
3311 ((eq context 'block-comment) 'c)
3312 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
3313
3314 (defalias 'c-in-literal
3315 (if (fboundp 'buffer-syntactic-context)
3316 'c-fast-in-literal ; XEmacs
3317 'c-slow-in-literal)) ; GNU Emacs
3318
3319 ;; The defalias above isn't enough to shut up the byte compiler.
3320 (cc-bytecomp-defun c-in-literal)
3321
3322 (defun c-literal-limits (&optional lim near not-in-delimiter)
3323 "Return a cons of the beginning and end positions of the comment or
3324 string surrounding point (including both delimiters), or nil if point
3325 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
3326 to start parsing from. If NEAR is non-nil, then the limits of any
3327 literal next to point is returned. \"Next to\" means there's only
3328 spaces and tabs between point and the literal. The search for such a
3329 literal is done first in forward direction. If NOT-IN-DELIMITER is
3330 non-nil, the case when point is inside a starting delimiter won't be
3331 recognized. This only has effect for comments which have starting
3332 delimiters with more than one character.
3333
3334 Note that this function might do hidden buffer changes. See the
3335 comment at the start of cc-engine.el for more info."
3336
3337 (save-excursion
3338 (let* ((pos (point))
3339 (lim (or lim (progn
3340 (c-beginning-of-syntax)
3341 (point))))
3342 (state (parse-partial-sexp lim pos)))
3343
3344 (cond ((elt state 3) ; String.
3345 (goto-char (elt state 8))
3346 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3347 (point-max))))
3348
3349 ((elt state 4) ; Comment.
3350 (goto-char (elt state 8))
3351 (cons (point) (progn (c-forward-single-comment) (point))))
3352
3353 ((and (not not-in-delimiter)
3354 (not (elt state 5))
3355 (eq (char-before) ?/)
3356 (looking-at "[/*]"))
3357 ;; We're standing in a comment starter.
3358 (backward-char 1)
3359 (cons (point) (progn (c-forward-single-comment) (point))))
3360
3361 (near
3362 (goto-char pos)
3363
3364 ;; Search forward for a literal.
3365 (skip-chars-forward " \t")
3366
3367 (cond
3368 ((looking-at c-string-limit-regexp) ; String.
3369 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3370 (point-max))))
3371
3372 ((looking-at c-comment-start-regexp) ; Line or block comment.
3373 (cons (point) (progn (c-forward-single-comment) (point))))
3374
3375 (t
3376 ;; Search backward.
3377 (skip-chars-backward " \t")
3378
3379 (let ((end (point)) beg)
3380 (cond
3381 ((save-excursion
3382 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
3383 (setq beg (c-safe (c-backward-sexp 1) (point))))
3384
3385 ((and (c-safe (forward-char -2) t)
3386 (looking-at "*/"))
3387 ;; Block comment. Due to the nature of line
3388 ;; comments, they will always be covered by the
3389 ;; normal case above.
3390 (goto-char end)
3391 (c-backward-single-comment)
3392 ;; If LIM is bogus, beg will be bogus.
3393 (setq beg (point))))
3394
3395 (if beg (cons beg end))))))
3396 ))))
3397
3398 ;; In case external callers use this; it did have a docstring.
3399 (defalias 'c-literal-limits-fast 'c-literal-limits)
3400
3401 (defun c-collect-line-comments (range)
3402 "If the argument is a cons of two buffer positions (such as returned by
3403 `c-literal-limits'), and that range contains a C++ style line comment,
3404 then an extended range is returned that contains all adjacent line
3405 comments (i.e. all comments that starts in the same column with no
3406 empty lines or non-whitespace characters between them). Otherwise the
3407 argument is returned.
3408
3409 Note that this function might do hidden buffer changes. See the
3410 comment at the start of cc-engine.el for more info."
3411
3412 (save-excursion
3413 (condition-case nil
3414 (if (and (consp range) (progn
3415 (goto-char (car range))
3416 (looking-at c-line-comment-starter)))
3417 (let ((col (current-column))
3418 (beg (point))
3419 (bopl (c-point 'bopl))
3420 (end (cdr range)))
3421 ;; Got to take care in the backward direction to handle
3422 ;; comments which are preceded by code.
3423 (while (and (c-backward-single-comment)
3424 (>= (point) bopl)
3425 (looking-at c-line-comment-starter)
3426 (= col (current-column)))
3427 (setq beg (point)
3428 bopl (c-point 'bopl)))
3429 (goto-char end)
3430 (while (and (progn (skip-chars-forward " \t")
3431 (looking-at c-line-comment-starter))
3432 (= col (current-column))
3433 (prog1 (zerop (forward-line 1))
3434 (setq end (point)))))
3435 (cons beg end))
3436 range)
3437 (error range))))
3438
3439 (defun c-literal-type (range)
3440 "Convenience function that given the result of `c-literal-limits',
3441 returns nil or the type of literal that the range surrounds, one
3442 of the symbols 'c, 'c++ or 'string. It's much faster than using
3443 `c-in-literal' and is intended to be used when you need both the
3444 type of a literal and its limits.
3445
3446 Note that this function might do hidden buffer changes. See the
3447 comment at the start of cc-engine.el for more info."
3448
3449 (if (consp range)
3450 (save-excursion
3451 (goto-char (car range))
3452 (cond ((looking-at c-string-limit-regexp) 'string)
3453 ((or (looking-at "//") ; c++ line comment
3454 (and (looking-at "\\s<") ; comment starter
3455 (looking-at "#"))) ; awk comment.
3456 'c++)
3457 (t 'c))) ; Assuming the range is valid.
3458 range))
3459
3460 \f
3461 ;; `c-find-decl-spots' and accompanying stuff.
3462
3463 ;; Variables used in `c-find-decl-spots' to cache the search done for
3464 ;; the first declaration in the last call. When that function starts,
3465 ;; it needs to back up over syntactic whitespace to look at the last
3466 ;; token before the region being searched. That can sometimes cause
3467 ;; moves back and forth over a quite large region of comments and
3468 ;; macros, which would be repeated for each changed character when
3469 ;; we're called during fontification, since font-lock refontifies the
3470 ;; current line for each change. Thus it's worthwhile to cache the
3471 ;; first match.
3472 ;;
3473 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3474 ;; the syntactic whitespace less or equal to some start position.
3475 ;; There's no cached value if it's nil.
3476 ;;
3477 ;; `c-find-decl-match-pos' is the match position if
3478 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3479 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3480 (defvar c-find-decl-syntactic-pos nil)
3481 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
3482 (defvar c-find-decl-match-pos nil)
3483 (make-variable-buffer-local 'c-find-decl-match-pos)
3484
3485 (defsubst c-invalidate-find-decl-cache (change-min-pos)
3486 (and c-find-decl-syntactic-pos
3487 (< change-min-pos c-find-decl-syntactic-pos)
3488 (setq c-find-decl-syntactic-pos nil)))
3489
3490 ; (defface c-debug-decl-spot-face
3491 ; '((t (:background "Turquoise")))
3492 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3493 ; (defface c-debug-decl-sws-face
3494 ; '((t (:background "Khaki")))
3495 ; "Debug face to mark the syntactic whitespace between the declaration
3496 ; spots and the preceding token end.")
3497
3498 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
3499 (when (facep 'c-debug-decl-spot-face)
3500 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
3501 (c-debug-add-face (max match-pos (point-min)) decl-pos
3502 'c-debug-decl-sws-face)
3503 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
3504 'c-debug-decl-spot-face))))
3505 (defmacro c-debug-remove-decl-spot-faces (beg end)
3506 (when (facep 'c-debug-decl-spot-face)
3507 `(c-save-buffer-state ()
3508 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
3509 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
3510
3511 (defmacro c-find-decl-prefix-search ()
3512 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3513 ;; but it contains lots of free variables that refer to things
3514 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3515 ;; if there is a match, otherwise at `cfd-limit'.
3516 ;;
3517 ;; This macro might do hidden buffer changes.
3518
3519 '(progn
3520 ;; Find the next property match position if we haven't got one already.
3521 (unless cfd-prop-match
3522 (save-excursion
3523 (while (progn
3524 (goto-char (next-single-property-change
3525 (point) 'c-type nil cfd-limit))
3526 (and (< (point) cfd-limit)
3527 (not (eq (c-get-char-property (1- (point)) 'c-type)
3528 'c-decl-end)))))
3529 (setq cfd-prop-match (point))))
3530
3531 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
3532 ;; got one already.
3533 (unless cfd-re-match
3534
3535 (if (> cfd-re-match-end (point))
3536 (goto-char cfd-re-match-end))
3537
3538 (while (if (setq cfd-re-match-end
3539 (re-search-forward c-decl-prefix-or-start-re
3540 cfd-limit 'move))
3541
3542 ;; Match. Check if it's inside a comment or string literal.
3543 (c-got-face-at
3544 (if (setq cfd-re-match (match-end 1))
3545 ;; Matched the end of a token preceding a decl spot.
3546 (progn
3547 (goto-char cfd-re-match)
3548 (1- cfd-re-match))
3549 ;; Matched a token that start a decl spot.
3550 (goto-char (match-beginning 0))
3551 (point))
3552 c-literal-faces)
3553
3554 ;; No match. Finish up and exit the loop.
3555 (setq cfd-re-match cfd-limit)
3556 nil)
3557
3558 ;; Skip out of comments and string literals.
3559 (while (progn
3560 (goto-char (next-single-property-change
3561 (point) 'face nil cfd-limit))
3562 (and (< (point) cfd-limit)
3563 (c-got-face-at (point) c-literal-faces)))))
3564
3565 ;; If we matched at the decl start, we have to back up over the
3566 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
3567 ;; any decl spots in the syntactic ws.
3568 (unless cfd-re-match
3569 (c-backward-syntactic-ws)
3570 (setq cfd-re-match (point))))
3571
3572 ;; Choose whichever match is closer to the start.
3573 (if (< cfd-re-match cfd-prop-match)
3574 (setq cfd-match-pos cfd-re-match
3575 cfd-re-match nil)
3576 (setq cfd-match-pos cfd-prop-match
3577 cfd-prop-match nil))
3578
3579 (goto-char cfd-match-pos)
3580
3581 (when (< cfd-match-pos cfd-limit)
3582 ;; Skip forward past comments only so we don't skip macros.
3583 (c-forward-comments)
3584 ;; Set the position to continue at. We can avoid going over
3585 ;; the comments skipped above a second time, but it's possible
3586 ;; that the comment skipping has taken us past `cfd-prop-match'
3587 ;; since the property might be used inside comments.
3588 (setq cfd-continue-pos (if cfd-prop-match
3589 (min cfd-prop-match (point))
3590 (point))))))
3591
3592 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3593 ;; Call CFD-FUN for each possible spot for a declaration, cast or
3594 ;; label from the point to CFD-LIMIT.
3595 ;;
3596 ;; CFD-FUN is called with point at the start of the spot. It's
3597 ;; passed two arguments: The first is the end position of the token
3598 ;; preceding the spot, or 0 for the implicit match at bob. The
3599 ;; second is a flag that is t when the match is inside a macro. If
3600 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
3601 ;; spot, it should return non-nil to ensure that the next search
3602 ;; will find them.
3603 ;;
3604 ;; Such a spot is:
3605 ;; o The first token after bob.
3606 ;; o The first token after the end of submatch 1 in
3607 ;; `c-decl-prefix-or-start-re' when that submatch matches.
3608 ;; o The start of each `c-decl-prefix-or-start-re' match when
3609 ;; submatch 1 doesn't match.
3610 ;; o The first token after the end of each occurence of the
3611 ;; `c-type' text property with the value `c-decl-end', provided
3612 ;; `c-type-decl-end-used' is set.
3613 ;;
3614 ;; Only a spot that match CFD-DECL-RE and whose face is in the
3615 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
3616 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
3617 ;;
3618 ;; If the match is inside a macro then the buffer is narrowed to the
3619 ;; end of it, so that CFD-FUN can investigate the following tokens
3620 ;; without matching something that begins inside a macro and ends
3621 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3622 ;; CFD-FACE-CHECKLIST checks exist.
3623 ;;
3624 ;; The spots are visited approximately in order from top to bottom.
3625 ;; It's however the positions where `c-decl-prefix-or-start-re'
3626 ;; matches and where `c-decl-end' properties are found that are in
3627 ;; order. Since the spots often are at the following token, they
3628 ;; might be visited out of order insofar as more spots are reported
3629 ;; later on within the syntactic whitespace between the match
3630 ;; positions and their spots.
3631 ;;
3632 ;; It's assumed that comments and strings are fontified in the
3633 ;; searched range.
3634 ;;
3635 ;; This is mainly used in fontification, and so has an elaborate
3636 ;; cache to handle repeated calls from the same start position; see
3637 ;; the variables above.
3638 ;;
3639 ;; All variables in this function begin with `cfd-' to avoid name
3640 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3641 ;;
3642 ;; This function might do hidden buffer changes.
3643
3644 (let ((cfd-start-pos (point))
3645 (cfd-buffer-end (point-max))
3646 ;; The end of the token preceding the decl spot last found
3647 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
3648 ;; no match.
3649 cfd-re-match
3650 ;; The end position of the last `c-decl-prefix-or-start-re'
3651 ;; match. If this is greater than `cfd-continue-pos', the
3652 ;; next regexp search is started here instead.
3653 (cfd-re-match-end (point-min))
3654 ;; The end of the last `c-decl-end' found by
3655 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
3656 ;; match. If searching for the property isn't needed then we
3657 ;; disable it by setting it to `cfd-limit' directly.
3658 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3659 ;; The end of the token preceding the decl spot last found by
3660 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
3661 ;; bob. `cfd-limit' if there's no match. In other words,
3662 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
3663 (cfd-match-pos cfd-limit)
3664 ;; The position to continue searching at.
3665 cfd-continue-pos
3666 ;; The position of the last "real" token we've stopped at.
3667 ;; This can be greater than `cfd-continue-pos' when we get
3668 ;; hits inside macros or at `c-decl-end' positions inside
3669 ;; comments.
3670 (cfd-token-pos 0)
3671 ;; The end position of the last entered macro.
3672 (cfd-macro-end 0))
3673
3674 ;; Initialize by finding a syntactically relevant start position
3675 ;; before the point, and do the first `c-decl-prefix-or-start-re'
3676 ;; search unless we're at bob.
3677
3678 (let (start-in-literal start-in-macro syntactic-pos)
3679 ;; Must back up a bit since we look for the end of the previous
3680 ;; statement or declaration, which is earlier than the first
3681 ;; returned match.
3682
3683 (cond
3684 ;; First we need to move to a syntactically relevant position.
3685 ;; Begin by backing out of comment or string literals.
3686 ((and
3687 (when (c-got-face-at (point) c-literal-faces)
3688 ;; Try to use the faces to back up to the start of the
3689 ;; literal. FIXME: What if the point is on a declaration
3690 ;; inside a comment?
3691 (while (and (not (bobp))
3692 (c-got-face-at (1- (point)) c-literal-faces))
3693 (goto-char (previous-single-property-change
3694 (point) 'face nil (point-min))))
3695
3696 ;; XEmacs doesn't fontify the quotes surrounding string
3697 ;; literals.
3698 (and (featurep 'xemacs)
3699 (eq (get-text-property (point) 'face)
3700 'font-lock-string-face)
3701 (not (bobp))
3702 (progn (backward-char)
3703 (not (looking-at c-string-limit-regexp)))
3704 (forward-char))
3705
3706 ;; Don't trust the literal to contain only literal faces
3707 ;; (the font lock package might not have fontified the
3708 ;; start of it at all, for instance) so check that we have
3709 ;; arrived at something that looks like a start or else
3710 ;; resort to `c-literal-limits'.
3711 (unless (looking-at c-literal-start-regexp)
3712 (let ((range (c-literal-limits)))
3713 (if range (goto-char (car range)))))
3714
3715 (setq start-in-literal (point)))
3716
3717 ;; The start is in a literal. If the limit is in the same
3718 ;; one we don't have to find a syntactic position etc. We
3719 ;; only check that if the limit is at or before bonl to save
3720 ;; time; it covers the by far most common case when font-lock
3721 ;; refontifies the current line only.
3722 (<= cfd-limit (c-point 'bonl cfd-start-pos))
3723 (save-excursion
3724 (goto-char cfd-start-pos)
3725 (while (progn
3726 (goto-char (next-single-property-change
3727 (point) 'face nil cfd-limit))
3728 (and (< (point) cfd-limit)
3729 (c-got-face-at (point) c-literal-faces))))
3730 (= (point) cfd-limit)))
3731
3732 ;; Completely inside a literal. Set up variables to trig the
3733 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
3734 ;; find a suitable start position.
3735 (setq cfd-continue-pos start-in-literal))
3736
3737 ;; Check if the region might be completely inside a macro, to
3738 ;; optimize that like the completely-inside-literal above.
3739 ((save-excursion
3740 (and (= (forward-line 1) 0)
3741 (bolp) ; forward-line has funny behavior at eob.
3742 (>= (point) cfd-limit)
3743 (progn (backward-char)
3744 (eq (char-before) ?\\))))
3745 ;; (Maybe) completely inside a macro. Only need to trig the
3746 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
3747 ;; set things up.
3748 (setq cfd-continue-pos (1- cfd-start-pos)
3749 start-in-macro t))
3750
3751 (t
3752 ;; Back out of any macro so we don't miss any declaration
3753 ;; that could follow after it.
3754 (when (c-beginning-of-macro)
3755 (setq start-in-macro t))
3756
3757 ;; Now we're at a proper syntactically relevant position so we
3758 ;; can use the cache. But first clear it if it applied
3759 ;; further down.
3760 (c-invalidate-find-decl-cache cfd-start-pos)
3761
3762 (setq syntactic-pos (point))
3763 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
3764 ;; Don't have to do this if the cache is relevant here,
3765 ;; typically if the same line is refontified again. If
3766 ;; we're just some syntactic whitespace further down we can
3767 ;; still use the cache to limit the skipping.
3768 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
3769
3770 ;; If we hit `c-find-decl-syntactic-pos' and
3771 ;; `c-find-decl-match-pos' is set then we install the cached
3772 ;; values. If we hit `c-find-decl-syntactic-pos' and
3773 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3774 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3775 ;; and so we can continue the search from this point. If we
3776 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
3777 ;; the right spot to begin searching anyway.
3778 (if (and (eq (point) c-find-decl-syntactic-pos)
3779 c-find-decl-match-pos)
3780 (setq cfd-match-pos c-find-decl-match-pos
3781 cfd-continue-pos syntactic-pos)
3782
3783 (setq c-find-decl-syntactic-pos syntactic-pos)
3784
3785 (when (if (bobp)
3786 ;; Always consider bob a match to get the first
3787 ;; declaration in the file. Do this separately instead of
3788 ;; letting `c-decl-prefix-or-start-re' match bob, so that
3789 ;; regexp always can consume at least one character to
3790 ;; ensure that we won't get stuck in an infinite loop.
3791 (setq cfd-re-match 0)
3792 (backward-char)
3793 (c-beginning-of-current-token)
3794 (< (point) cfd-limit))
3795 ;; Do an initial search now. In the bob case above it's
3796 ;; only done to search for a `c-decl-end' spot.
3797 (c-find-decl-prefix-search))
3798
3799 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
3800 cfd-match-pos)))))
3801
3802 ;; Advance `cfd-continue-pos' if it's before the start position.
3803 ;; The closest continue position that might have effect at or
3804 ;; after the start depends on what we started in. This also
3805 ;; finds a suitable start position in the special cases when the
3806 ;; region is completely within a literal or macro.
3807 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
3808
3809 (cond
3810 (start-in-macro
3811 ;; If we're in a macro then it's the closest preceding token
3812 ;; in the macro. Check this before `start-in-literal',
3813 ;; since if we're inside a literal in a macro, the preceding
3814 ;; token is earlier than any `c-decl-end' spot inside the
3815 ;; literal (comment).
3816 (goto-char (or start-in-literal cfd-start-pos))
3817 ;; The only syntactic ws in macros are comments.
3818 (c-backward-comments)
3819 (backward-char)
3820 (c-beginning-of-current-token))
3821
3822 (start-in-literal
3823 ;; If we're in a comment it can only be the closest
3824 ;; preceding `c-decl-end' position within that comment, if
3825 ;; any. Go back to the beginning of such a property so that
3826 ;; `c-find-decl-prefix-search' will find the end of it.
3827 ;; (Can't stop at the end and install it directly on
3828 ;; `cfd-prop-match' since that variable might be cleared
3829 ;; after `cfd-fun' below.)
3830 ;;
3831 ;; Note that if the literal is a string then the property
3832 ;; search will simply skip to the beginning of it right
3833 ;; away.
3834 (if (not c-type-decl-end-used)
3835 (goto-char start-in-literal)
3836 (goto-char cfd-start-pos)
3837 (while (progn
3838 (goto-char (previous-single-property-change
3839 (point) 'c-type nil start-in-literal))
3840 (and (> (point) start-in-literal)
3841 (not (eq (c-get-char-property (point) 'c-type)
3842 'c-decl-end))))))
3843
3844 (when (= (point) start-in-literal)
3845 ;; Didn't find any property inside the comment, so we can
3846 ;; skip it entirely. (This won't skip past a string, but
3847 ;; that'll be handled quickly by the next
3848 ;; `c-find-decl-prefix-search' anyway.)
3849 (c-forward-single-comment)
3850 (if (> (point) cfd-limit)
3851 (goto-char cfd-limit))))
3852
3853 (t
3854 ;; If we started in normal code, the only match that might
3855 ;; apply before the start is what we already got in
3856 ;; `cfd-match-pos' so we can continue at the start position.
3857 ;; (Note that we don't get here if the first match is below
3858 ;; it.)
3859 (goto-char cfd-start-pos)))
3860
3861 ;; Delete found matches if they are before our new continue
3862 ;; position, so that `c-find-decl-prefix-search' won't back up
3863 ;; to them later on.
3864 (setq cfd-continue-pos (point))
3865 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
3866 (setq cfd-re-match nil))
3867 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
3868 (setq cfd-prop-match nil)))
3869
3870 (if syntactic-pos
3871 ;; This is the normal case and we got a proper syntactic
3872 ;; position. If there's a match then it's always outside
3873 ;; macros and comments, so advance to the next token and set
3874 ;; `cfd-token-pos'. The loop below will later go back using
3875 ;; `cfd-continue-pos' to fix declarations inside the
3876 ;; syntactic ws.
3877 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
3878 (goto-char syntactic-pos)
3879 (c-forward-syntactic-ws)
3880 (and cfd-continue-pos
3881 (< cfd-continue-pos (point))
3882 (setq cfd-token-pos (point))))
3883
3884 ;; Have one of the special cases when the region is completely
3885 ;; within a literal or macro. `cfd-continue-pos' is set to a
3886 ;; good start position for the search, so do it.
3887 (c-find-decl-prefix-search)))
3888
3889 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
3890
3891 (while (progn
3892 (while (and
3893 (< cfd-match-pos cfd-limit)
3894
3895 (or
3896 ;; Kludge to filter out matches on the "<" that
3897 ;; aren't open parens, for the sake of languages
3898 ;; that got `c-recognize-<>-arglists' set.
3899 (and (eq (char-before cfd-match-pos) ?<)
3900 (not (c-get-char-property (1- cfd-match-pos)
3901 'syntax-table)))
3902
3903 ;; If `cfd-continue-pos' is less or equal to
3904 ;; `cfd-token-pos', we've got a hit inside a macro
3905 ;; that's in the syntactic whitespace before the last
3906 ;; "real" declaration we've checked. If they're equal
3907 ;; we've arrived at the declaration a second time, so
3908 ;; there's nothing to do.
3909 (= cfd-continue-pos cfd-token-pos)
3910
3911 (progn
3912 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3913 ;; we're still searching for declarations embedded in
3914 ;; the syntactic whitespace. In that case we need
3915 ;; only to skip comments and not macros, since they
3916 ;; can't be nested, and that's already been done in
3917 ;; `c-find-decl-prefix-search'.
3918 (when (> cfd-continue-pos cfd-token-pos)
3919 (c-forward-syntactic-ws)
3920 (setq cfd-token-pos (point)))
3921
3922 ;; Continue if the following token fails the
3923 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3924 (when (or (>= (point) cfd-limit)
3925 (not (looking-at cfd-decl-re))
3926 (and cfd-face-checklist
3927 (not (c-got-face-at
3928 (point) cfd-face-checklist))))
3929 (goto-char cfd-continue-pos)
3930 t)))
3931
3932 (< (point) cfd-limit))
3933 (c-find-decl-prefix-search))
3934
3935 (< (point) cfd-limit))
3936
3937 (when (and
3938 (>= (point) cfd-start-pos)
3939
3940 (progn
3941 ;; Narrow to the end of the macro if we got a hit inside
3942 ;; one, to avoid recognizing things that start inside the
3943 ;; macro and end outside it.
3944 (when (> cfd-match-pos cfd-macro-end)
3945 ;; Not in the same macro as in the previous round.
3946 (save-excursion
3947 (goto-char cfd-match-pos)
3948 (setq cfd-macro-end
3949 (if (save-excursion (and (c-beginning-of-macro)
3950 (< (point) cfd-match-pos)))
3951 (progn (c-end-of-macro)
3952 (point))
3953 0))))
3954
3955 (if (zerop cfd-macro-end)
3956 t
3957 (if (> cfd-macro-end (point))
3958 (progn (narrow-to-region (point-min) cfd-macro-end)
3959 t)
3960 ;; The matched token was the last thing in the macro,
3961 ;; so the whole match is bogus.
3962 (setq cfd-macro-end 0)
3963 nil))))
3964
3965 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3966 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3967 (setq cfd-prop-match nil))
3968
3969 (when (/= cfd-macro-end 0)
3970 ;; Restore limits if we did macro narrowment above.
3971 (narrow-to-region (point-min) cfd-buffer-end)))
3972
3973 (goto-char cfd-continue-pos)
3974 (if (= cfd-continue-pos cfd-limit)
3975 (setq cfd-match-pos cfd-limit)
3976 (c-find-decl-prefix-search)))))
3977
3978 \f
3979 ;; A cache for found types.
3980
3981 ;; Buffer local variable that contains an obarray with the types we've
3982 ;; found. If a declaration is recognized somewhere we record the
3983 ;; fully qualified identifier in it to recognize it as a type
3984 ;; elsewhere in the file too. This is not accurate since we do not
3985 ;; bother with the scoping rules of the languages, but in practice the
3986 ;; same name is seldom used as both a type and something else in a
3987 ;; file, and we only use this as a last resort in ambiguous cases (see
3988 ;; `c-forward-decl-or-cast-1').
3989 ;;
3990 ;; Not every type need be in this cache. However, things which have
3991 ;; ceased to be types must be removed from it.
3992 ;;
3993 ;; Template types in C++ are added here too but with the template
3994 ;; arglist replaced with "<>" in references or "<" for the one in the
3995 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
3996 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
3997 ;; template specs can be fairly sized programs in themselves) and
3998 ;; improves the hit ratio (it's a type regardless of the template
3999 ;; args; it's just not the same type, but we're only interested in
4000 ;; recognizing types, not telling distinct types apart). Note that
4001 ;; template types in references are added here too; from the example
4002 ;; above there will also be an entry "Foo<".
4003 (defvar c-found-types nil)
4004 (make-variable-buffer-local 'c-found-types)
4005
4006 (defsubst c-clear-found-types ()
4007 ;; Clears `c-found-types'.
4008 (setq c-found-types (make-vector 53 0)))
4009
4010 (defun c-add-type (from to)
4011 ;; Add the given region as a type in `c-found-types'. If the region
4012 ;; doesn't match an existing type but there is a type which is equal
4013 ;; to the given one except that the last character is missing, then
4014 ;; the shorter type is removed. That's done to avoid adding all
4015 ;; prefixes of a type as it's being entered and font locked. This
4016 ;; doesn't cover cases like when characters are removed from a type
4017 ;; or added in the middle. We'd need the position of point when the
4018 ;; font locking is invoked to solve this well.
4019 ;;
4020 ;; This function might do hidden buffer changes.
4021 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4022 (unless (intern-soft type c-found-types)
4023 (unintern (substring type 0 -1) c-found-types)
4024 (intern type c-found-types))))
4025
4026 (defun c-unfind-type (name)
4027 ;; Remove the "NAME" from c-found-types, if present.
4028 (unintern name c-found-types))
4029
4030 (defsubst c-check-type (from to)
4031 ;; Return non-nil if the given region contains a type in
4032 ;; `c-found-types'.
4033 ;;
4034 ;; This function might do hidden buffer changes.
4035 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4036 c-found-types))
4037
4038 (defun c-list-found-types ()
4039 ;; Return all the types in `c-found-types' as a sorted list of
4040 ;; strings.
4041 (let (type-list)
4042 (mapatoms (lambda (type)
4043 (setq type-list (cons (symbol-name type)
4044 type-list)))
4045 c-found-types)
4046 (sort type-list 'string-lessp)))
4047
4048 ;; Shut up the byte compiler.
4049 (defvar c-maybe-stale-found-type)
4050
4051 (defun c-trim-found-types (beg end old-len)
4052 ;; An after change function which, in conjunction with the info in
4053 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4054 ;; from `c-found-types', should this type have become stale. For
4055 ;; example, this happens to "foo" when "foo \n bar();" becomes
4056 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4057 ;; the fontification.
4058 ;;
4059 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4060 ;; type?
4061 (when (> end beg)
4062 (save-excursion
4063 (when (< end (point-max))
4064 (goto-char end)
4065 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4066 (progn (goto-char end)
4067 (c-end-of-current-token)))
4068 (c-unfind-type (buffer-substring-no-properties
4069 end (point)))))
4070 (when (> beg (point-min))
4071 (goto-char beg)
4072 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4073 (progn (goto-char beg)
4074 (c-beginning-of-current-token)))
4075 (c-unfind-type (buffer-substring-no-properties
4076 (point) beg))))))
4077
4078 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4079 (cond
4080 ;; Changing the amount of (already existing) whitespace - don't do anything.
4081 ((and (c-partial-ws-p beg end)
4082 (or (= beg end) ; removal of WS
4083 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4084
4085 ;; The syntactic relationship which defined a "found type" has been
4086 ;; destroyed.
4087 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4088 (c-unfind-type (cadr c-maybe-stale-found-type)))
4089 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4090 )))
4091
4092 \f
4093 ;; Handling of small scale constructs like types and names.
4094
4095 (defun c-after-change-check-<>-operators (beg end)
4096 ;; This is called from `after-change-functions' when
4097 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
4098 ;; chars with paren syntax become part of another operator like "<<"
4099 ;; or ">=".
4100 ;;
4101 ;; This function might do hidden buffer changes.
4102
4103 (save-excursion
4104 (goto-char beg)
4105 (when (or (looking-at "[<>]")
4106 (< (skip-chars-backward "<>") 0))
4107
4108 (goto-char beg)
4109 (c-beginning-of-current-token)
4110 (when (and (< (point) beg)
4111 (looking-at c-<>-multichar-token-regexp)
4112 (< beg (setq beg (match-end 0))))
4113 (while (progn (skip-chars-forward "^<>" beg)
4114 (< (point) beg))
4115 (c-clear-char-property (point) 'syntax-table)
4116 (forward-char))))
4117
4118 (when (< beg end)
4119 (goto-char end)
4120 (when (or (looking-at "[<>]")
4121 (< (skip-chars-backward "<>") 0))
4122
4123 (goto-char end)
4124 (c-beginning-of-current-token)
4125 (when (and (< (point) end)
4126 (looking-at c-<>-multichar-token-regexp)
4127 (< end (setq end (match-end 0))))
4128 (while (progn (skip-chars-forward "^<>" end)
4129 (< (point) end))
4130 (c-clear-char-property (point) 'syntax-table)
4131 (forward-char)))))))
4132
4133 ;; Dynamically bound variable that instructs `c-forward-type' to also
4134 ;; treat possible types (i.e. those that it normally returns 'maybe or
4135 ;; 'found for) as actual types (and always return 'found for them).
4136 ;; This means that it records them in `c-record-type-identifiers' if
4137 ;; that is set, and that it adds them to `c-found-types'.
4138 (defvar c-promote-possible-types nil)
4139
4140 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4141 ;; mark up successfully parsed arglists with paren syntax properties on
4142 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
4143 ;; `c-type' property of each argument separating comma.
4144 ;;
4145 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
4146 ;; all arglists for side effects (i.e. recording types), otherwise it
4147 ;; exploits any existing paren syntax properties to quickly jump to the
4148 ;; end of already parsed arglists.
4149 ;;
4150 ;; Marking up the arglists is not the default since doing that correctly
4151 ;; depends on a proper value for `c-restricted-<>-arglists'.
4152 (defvar c-parse-and-markup-<>-arglists nil)
4153
4154 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4155 ;; not accept arglists that contain binary operators.
4156 ;;
4157 ;; This is primarily used to handle C++ template arglists. C++
4158 ;; disambiguates them by checking whether the preceding name is a
4159 ;; template or not. We can't do that, so we assume it is a template
4160 ;; if it can be parsed as one. That usually works well since
4161 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
4162 ;; in almost all cases would be pointless.
4163 ;;
4164 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
4165 ;; should let the comma separate the function arguments instead. And
4166 ;; in a context where the value of the expression is taken, e.g. in
4167 ;; "if (a < b || c > d)", it's probably not a template.
4168 (defvar c-restricted-<>-arglists nil)
4169
4170 ;; Dynamically bound variables that instructs
4171 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
4172 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
4173 ;; `c-forward-label' to record the ranges of all the type and
4174 ;; reference identifiers they encounter. They will build lists on
4175 ;; these variables where each element is a cons of the buffer
4176 ;; positions surrounding each identifier. This recording is only
4177 ;; activated when `c-record-type-identifiers' is non-nil.
4178 ;;
4179 ;; All known types that can't be identifiers are recorded, and also
4180 ;; other possible types if `c-promote-possible-types' is set.
4181 ;; Recording is however disabled inside angle bracket arglists that
4182 ;; are encountered inside names and other angle bracket arglists.
4183 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
4184 ;; instead.
4185 ;;
4186 ;; Only the names in C++ template style references (e.g. "tmpl" in
4187 ;; "tmpl<a,b>::foo") are recorded as references, other references
4188 ;; aren't handled here.
4189 ;;
4190 ;; `c-forward-label' records the label identifier(s) on
4191 ;; `c-record-ref-identifiers'.
4192 (defvar c-record-type-identifiers nil)
4193 (defvar c-record-ref-identifiers nil)
4194
4195 ;; This variable will receive a cons cell of the range of the last
4196 ;; single identifier symbol stepped over by `c-forward-name' if it's
4197 ;; successful. This is the range that should be put on one of the
4198 ;; record lists above by the caller. It's assigned nil if there's no
4199 ;; such symbol in the name.
4200 (defvar c-last-identifier-range nil)
4201
4202 (defmacro c-record-type-id (range)
4203 (if (eq (car-safe range) 'cons)
4204 ;; Always true.
4205 `(setq c-record-type-identifiers
4206 (cons ,range c-record-type-identifiers))
4207 `(let ((range ,range))
4208 (if range
4209 (setq c-record-type-identifiers
4210 (cons range c-record-type-identifiers))))))
4211
4212 (defmacro c-record-ref-id (range)
4213 (if (eq (car-safe range) 'cons)
4214 ;; Always true.
4215 `(setq c-record-ref-identifiers
4216 (cons ,range c-record-ref-identifiers))
4217 `(let ((range ,range))
4218 (if range
4219 (setq c-record-ref-identifiers
4220 (cons range c-record-ref-identifiers))))))
4221
4222 ;; Dynamically bound variable that instructs `c-forward-type' to
4223 ;; record the ranges of types that only are found. Behaves otherwise
4224 ;; like `c-record-type-identifiers'.
4225 (defvar c-record-found-types nil)
4226
4227 (defmacro c-forward-keyword-prefixed-id (type)
4228 ;; Used internally in `c-forward-keyword-clause' to move forward
4229 ;; over a type (if TYPE is 'type) or a name (otherwise) which
4230 ;; possibly is prefixed by keywords and their associated clauses.
4231 ;; Try with a type/name first to not trip up on those that begin
4232 ;; with a keyword. Return t if a known or found type is moved
4233 ;; over. The point is clobbered if nil is returned. If range
4234 ;; recording is enabled, the identifier is recorded on as a type
4235 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
4236 ;;
4237 ;; This macro might do hidden buffer changes.
4238 `(let (res)
4239 (while (if (setq res ,(if (eq type 'type)
4240 `(c-forward-type)
4241 `(c-forward-name)))
4242 nil
4243 (and (looking-at c-keywords-regexp)
4244 (c-forward-keyword-clause 1))))
4245 (when (memq res '(t known found prefix))
4246 ,(when (eq type 'ref)
4247 `(when c-record-type-identifiers
4248 (c-record-ref-id c-last-identifier-range)))
4249 t)))
4250
4251 (defmacro c-forward-id-comma-list (type update-safe-pos)
4252 ;; Used internally in `c-forward-keyword-clause' to move forward
4253 ;; over a comma separated list of types or names using
4254 ;; `c-forward-keyword-prefixed-id'.
4255 ;;
4256 ;; This macro might do hidden buffer changes.
4257 `(while (and (progn
4258 ,(when update-safe-pos
4259 `(setq safe-pos (point)))
4260 (eq (char-after) ?,))
4261 (progn
4262 (forward-char)
4263 (c-forward-syntactic-ws)
4264 (c-forward-keyword-prefixed-id ,type)))))
4265
4266 (defun c-forward-keyword-clause (match)
4267 ;; Submatch MATCH in the current match data is assumed to surround a
4268 ;; token. If it's a keyword, move over it and any immediately
4269 ;; following clauses associated with it, stopping at the start of
4270 ;; the next token. t is returned in that case, otherwise the point
4271 ;; stays and nil is returned. The kind of clauses that are
4272 ;; recognized are those specified by `c-type-list-kwds',
4273 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
4274 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
4275 ;; and `c-<>-arglist-kwds'.
4276 ;;
4277 ;; This function records identifier ranges on
4278 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4279 ;; `c-record-type-identifiers' is non-nil.
4280 ;;
4281 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
4282 ;; apply directly after the keyword, the type list is moved over
4283 ;; only when there is no unaccounted token before it (i.e. a token
4284 ;; that isn't moved over due to some other keyword list). The
4285 ;; identifier ranges in the list are still recorded if that should
4286 ;; be done, though.
4287 ;;
4288 ;; This function might do hidden buffer changes.
4289
4290 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
4291 ;; The call to `c-forward-<>-arglist' below is made after
4292 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
4293 ;; are angle bracket arglists and `c-restricted-<>-arglists'
4294 ;; should therefore be nil.
4295 (c-parse-and-markup-<>-arglists t)
4296 c-restricted-<>-arglists)
4297
4298 (when kwd-sym
4299 (goto-char (match-end match))
4300 (c-forward-syntactic-ws)
4301 (setq safe-pos (point))
4302
4303 (cond
4304 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
4305 (c-forward-keyword-prefixed-id type))
4306 ;; There's a type directly after a keyword in `c-type-list-kwds'.
4307 (c-forward-id-comma-list type t))
4308
4309 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
4310 (c-forward-keyword-prefixed-id ref))
4311 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
4312 (c-forward-id-comma-list ref t))
4313
4314 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
4315 (eq (char-after) ?\())
4316 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
4317
4318 (forward-char)
4319 (when (and (setq pos (c-up-list-forward))
4320 (eq (char-before pos) ?\)))
4321 (when (and c-record-type-identifiers
4322 (c-keyword-member kwd-sym 'c-paren-type-kwds))
4323 ;; Use `c-forward-type' on every identifier we can find
4324 ;; inside the paren, to record the types.
4325 (while (c-syntactic-re-search-forward c-symbol-start pos t)
4326 (goto-char (match-beginning 0))
4327 (unless (c-forward-type)
4328 (looking-at c-symbol-key) ; Always matches.
4329 (goto-char (match-end 0)))))
4330
4331 (goto-char pos)
4332 (c-forward-syntactic-ws)
4333 (setq safe-pos (point))))
4334
4335 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
4336 (eq (char-after) ?<)
4337 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
4338 (c-forward-syntactic-ws)
4339 (setq safe-pos (point)))
4340
4341 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
4342 (not (looking-at c-symbol-start))
4343 (c-safe (c-forward-sexp) t))
4344 (c-forward-syntactic-ws)
4345 (setq safe-pos (point))))
4346
4347 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
4348 (if (eq (char-after) ?:)
4349 ;; If we are at the colon already, we move over the type
4350 ;; list after it.
4351 (progn
4352 (forward-char)
4353 (c-forward-syntactic-ws)
4354 (when (c-forward-keyword-prefixed-id type)
4355 (c-forward-id-comma-list type t)))
4356 ;; Not at the colon, so stop here. But the identifier
4357 ;; ranges in the type list later on should still be
4358 ;; recorded.
4359 (and c-record-type-identifiers
4360 (progn
4361 ;; If a keyword matched both one of the types above and
4362 ;; this one, we match `c-colon-type-list-re' after the
4363 ;; clause matched above.
4364 (goto-char safe-pos)
4365 (looking-at c-colon-type-list-re))
4366 (progn
4367 (goto-char (match-end 0))
4368 (c-forward-syntactic-ws)
4369 (c-forward-keyword-prefixed-id type))
4370 ;; There's a type after the `c-colon-type-list-re' match
4371 ;; after a keyword in `c-colon-type-list-kwds'.
4372 (c-forward-id-comma-list type nil))))
4373
4374 (goto-char safe-pos)
4375 t)))
4376
4377 (defun c-forward-<>-arglist (all-types)
4378 ;; The point is assumed to be at a "<". Try to treat it as the open
4379 ;; paren of an angle bracket arglist and move forward to the the
4380 ;; corresponding ">". If successful, the point is left after the
4381 ;; ">" and t is returned, otherwise the point isn't moved and nil is
4382 ;; returned. If ALL-TYPES is t then all encountered arguments in
4383 ;; the arglist that might be types are treated as found types.
4384 ;;
4385 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
4386 ;; function handles text properties on the angle brackets and argument
4387 ;; separating commas.
4388 ;;
4389 ;; `c-restricted-<>-arglists' controls how lenient the template
4390 ;; arglist recognition should be.
4391 ;;
4392 ;; This function records identifier ranges on
4393 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4394 ;; `c-record-type-identifiers' is non-nil.
4395 ;;
4396 ;; This function might do hidden buffer changes.
4397
4398 (let ((start (point))
4399 ;; If `c-record-type-identifiers' is set then activate
4400 ;; recording of any found types that constitute an argument in
4401 ;; the arglist.
4402 (c-record-found-types (if c-record-type-identifiers t)))
4403 (if (catch 'angle-bracket-arglist-escape
4404 (setq c-record-found-types
4405 (c-forward-<>-arglist-recur all-types)))
4406 (progn
4407 (when (consp c-record-found-types)
4408 (setq c-record-type-identifiers
4409 ;; `nconc' doesn't mind that the tail of
4410 ;; `c-record-found-types' is t.
4411 (nconc c-record-found-types c-record-type-identifiers)))
4412 t)
4413
4414 (goto-char start)
4415 nil)))
4416
4417 (defun c-forward-<>-arglist-recur (all-types)
4418 ;; Recursive part of `c-forward-<>-arglist'.
4419 ;;
4420 ;; This function might do hidden buffer changes.
4421
4422 (let ((start (point)) res pos tmp
4423 ;; Cover this so that any recorded found type ranges are
4424 ;; automatically lost if it turns out to not be an angle
4425 ;; bracket arglist. It's propagated through the return value
4426 ;; on successful completion.
4427 (c-record-found-types c-record-found-types)
4428 ;; List that collects the positions after the argument
4429 ;; separating ',' in the arglist.
4430 arg-start-pos)
4431
4432 ;; If the '<' has paren open syntax then we've marked it as an angle
4433 ;; bracket arglist before, so skip to the end.
4434 (if (and (not c-parse-and-markup-<>-arglists)
4435 (c-get-char-property (point) 'syntax-table))
4436
4437 (progn
4438 (forward-char)
4439 (if (and (c-go-up-list-forward)
4440 (eq (char-before) ?>))
4441 t
4442
4443 ;; Got unmatched paren angle brackets. We don't clear the paren
4444 ;; syntax properties and retry, on the basis that it's very
4445 ;; unlikely that paren angle brackets become operators by code
4446 ;; manipulation. It's far more likely that it doesn't match due
4447 ;; to narrowing or some temporary change.
4448 (goto-char start)
4449 nil))
4450
4451 (forward-char)
4452 (unless (looking-at c-<-op-cont-regexp)
4453 (while (and
4454 (progn
4455
4456 (when c-record-type-identifiers
4457 (if all-types
4458
4459 ;; All encountered identifiers are types, so set the
4460 ;; promote flag and parse the type.
4461 (progn
4462 (c-forward-syntactic-ws)
4463 (when (looking-at c-identifier-start)
4464 (let ((c-promote-possible-types t))
4465 (c-forward-type))))
4466
4467 ;; Check if this arglist argument is a sole type. If
4468 ;; it's known then it's recorded in
4469 ;; `c-record-type-identifiers'. If it only is found
4470 ;; then it's recorded in `c-record-found-types' which we
4471 ;; might roll back if it turns out that this isn't an
4472 ;; angle bracket arglist afterall.
4473 (when (memq (char-before) '(?, ?<))
4474 (let ((orig-record-found-types c-record-found-types))
4475 (c-forward-syntactic-ws)
4476 (and (memq (c-forward-type) '(known found))
4477 (not (looking-at "[,>]"))
4478 ;; A found type was recorded but it's not the
4479 ;; only thing in the arglist argument, so reset
4480 ;; `c-record-found-types'.
4481 (setq c-record-found-types
4482 orig-record-found-types))))))
4483
4484 (setq pos (point))
4485 (or (when (eq (char-after) ?>)
4486 ;; Must check for '>' at the very start separately,
4487 ;; since the regexp below has to avoid ">>" without
4488 ;; using \\=.
4489 (forward-char)
4490 t)
4491
4492 ;; Note: These regexps exploit the match order in \| so
4493 ;; that "<>" is matched by "<" rather than "[^>:-]>".
4494 (c-syntactic-re-search-forward
4495 (if c-restricted-<>-arglists
4496 ;; Stop on ',', '|', '&', '+' and '-' to catch
4497 ;; common binary operators that could be between
4498 ;; two comparison expressions "a<b" and "c>d".
4499 "[<;{},|&+-]\\|\\([^>:-]>\\)"
4500 ;; Otherwise we still stop on ',' to find the
4501 ;; argument start positions.
4502 "[<;{},]\\|\\([^>:-]>\\)")
4503 nil 'move t t 1)
4504
4505 ;; If the arglist starter has lost its open paren
4506 ;; syntax but not the closer, we won't find the
4507 ;; closer above since we only search in the
4508 ;; balanced sexp. In that case we stop just short
4509 ;; of it so check if the following char is the closer.
4510 (when (eq (char-after) ?>)
4511 (forward-char)
4512 t)))
4513
4514 (cond
4515 ((eq (char-before) ?>)
4516 ;; Either an operator starting with '>' or the end of
4517 ;; the angle bracket arglist.
4518
4519 (if (looking-at c->-op-cont-regexp)
4520 (progn
4521 (goto-char (match-end 0))
4522 t) ; Continue the loop.
4523
4524 ;; The angle bracket arglist is finished.
4525 (when c-parse-and-markup-<>-arglists
4526 (while arg-start-pos
4527 (c-put-c-type-property (1- (car arg-start-pos))
4528 'c-<>-arg-sep)
4529 (setq arg-start-pos (cdr arg-start-pos)))
4530 (c-mark-<-as-paren start)
4531 (c-mark->-as-paren (1- (point))))
4532 (setq res t)
4533 nil)) ; Exit the loop.
4534
4535 ((eq (char-before) ?<)
4536 ;; Either an operator starting with '<' or a nested arglist.
4537
4538 (setq pos (point))
4539 (let (id-start id-end subres keyword-match)
4540 (if (if (looking-at c-<-op-cont-regexp)
4541 (setq tmp (match-end 0))
4542 (setq tmp pos)
4543 (backward-char)
4544 (not
4545 (and
4546
4547 (save-excursion
4548 ;; There's always an identifier before an angle
4549 ;; bracket arglist, or a keyword in
4550 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
4551 (c-backward-syntactic-ws)
4552 (setq id-end (point))
4553 (c-simple-skip-symbol-backward)
4554 (when (or (setq keyword-match
4555 (looking-at c-opt-<>-sexp-key))
4556 (not (looking-at c-keywords-regexp)))
4557 (setq id-start (point))))
4558
4559 (setq subres
4560 (let ((c-record-type-identifiers nil)
4561 (c-record-found-types nil))
4562 (c-forward-<>-arglist-recur
4563 (and keyword-match
4564 (c-keyword-member
4565 (c-keyword-sym (match-string 1))
4566 'c-<>-type-kwds)))))
4567 )))
4568
4569 ;; It was not an angle bracket arglist.
4570 (goto-char tmp)
4571
4572 ;; It was an angle bracket arglist.
4573 (setq c-record-found-types subres)
4574
4575 ;; Record the identifier before the template as a type
4576 ;; or reference depending on whether the arglist is last
4577 ;; in a qualified identifier.
4578 (when (and c-record-type-identifiers
4579 (not keyword-match))
4580 (if (and c-opt-identifier-concat-key
4581 (progn
4582 (c-forward-syntactic-ws)
4583 (looking-at c-opt-identifier-concat-key)))
4584 (c-record-ref-id (cons id-start id-end))
4585 (c-record-type-id (cons id-start id-end))))))
4586 t)
4587
4588 ((and (eq (char-before) ?,)
4589 (not c-restricted-<>-arglists))
4590 ;; Just another argument. Record the position. The
4591 ;; type check stuff that made us stop at it is at
4592 ;; the top of the loop.
4593 (setq arg-start-pos (cons (point) arg-start-pos)))
4594
4595 (t
4596 ;; Got a character that can't be in an angle bracket
4597 ;; arglist argument. Abort using `throw', since
4598 ;; it's useless to try to find a surrounding arglist
4599 ;; if we're nested.
4600 (throw 'angle-bracket-arglist-escape nil))))))
4601
4602 (if res
4603 (or c-record-found-types t)))))
4604
4605 (defun c-backward-<>-arglist (all-types &optional limit)
4606 ;; The point is assumed to be directly after a ">". Try to treat it
4607 ;; as the close paren of an angle bracket arglist and move back to
4608 ;; the corresponding "<". If successful, the point is left at
4609 ;; the "<" and t is returned, otherwise the point isn't moved and
4610 ;; nil is returned. ALL-TYPES is passed on to
4611 ;; `c-forward-<>-arglist'.
4612 ;;
4613 ;; If the optional LIMIT is given, it bounds the backward search.
4614 ;; It's then assumed to be at a syntactically relevant position.
4615 ;;
4616 ;; This is a wrapper around `c-forward-<>-arglist'. See that
4617 ;; function for more details.
4618
4619 (let ((start (point)))
4620 (backward-char)
4621 (if (and (not c-parse-and-markup-<>-arglists)
4622 (c-get-char-property (point) 'syntax-table))
4623
4624 (if (and (c-go-up-list-backward)
4625 (eq (char-after) ?<))
4626 t
4627 ;; See corresponding note in `c-forward-<>-arglist'.
4628 (goto-char start)
4629 nil)
4630
4631 (while (progn
4632 (c-syntactic-skip-backward "^<;{}" limit t)
4633
4634 (and
4635 (if (eq (char-before) ?<)
4636 t
4637 ;; Stopped at bob or a char that isn't allowed in an
4638 ;; arglist, so we've failed.
4639 (goto-char start)
4640 nil)
4641
4642 (if (> (point)
4643 (progn (c-beginning-of-current-token)
4644 (point)))
4645 ;; If we moved then the "<" was part of some
4646 ;; multicharacter token.
4647 t
4648
4649 (backward-char)
4650 (let ((beg-pos (point)))
4651 (if (c-forward-<>-arglist all-types)
4652 (cond ((= (point) start)
4653 ;; Matched the arglist. Break the while.
4654 (goto-char beg-pos)
4655 nil)
4656 ((> (point) start)
4657 ;; We started from a non-paren ">" inside an
4658 ;; arglist.
4659 (goto-char start)
4660 nil)
4661 (t
4662 ;; Matched a shorter arglist. Can be a nested
4663 ;; one so continue looking.
4664 (goto-char beg-pos)
4665 t))
4666 t))))))
4667
4668 (/= (point) start))))
4669
4670 (defun c-forward-name ()
4671 ;; Move forward over a complete name if at the beginning of one,
4672 ;; stopping at the next following token. If the point is not at
4673 ;; something that are recognized as name then it stays put. A name
4674 ;; could be something as simple as "foo" in C or something as
4675 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
4676 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
4677 ;; int>::*volatile const" in C++ (this function is actually little
4678 ;; more than a `looking-at' call in all modes except those that,
4679 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
4680 ;; name is found, 'template if it's an identifier ending with an
4681 ;; angle bracket arglist, 'operator of it's an operator identifier,
4682 ;; or t if it's some other kind of name.
4683 ;;
4684 ;; This function records identifier ranges on
4685 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4686 ;; `c-record-type-identifiers' is non-nil.
4687 ;;
4688 ;; This function might do hidden buffer changes.
4689
4690 (let ((pos (point)) (start (point)) res id-start id-end
4691 ;; Turn off `c-promote-possible-types' here since we might
4692 ;; call `c-forward-<>-arglist' and we don't want it to promote
4693 ;; every suspect thing in the arglist to a type. We're
4694 ;; typically called from `c-forward-type' in this case, and
4695 ;; the caller only wants the top level type that it finds to
4696 ;; be promoted.
4697 c-promote-possible-types)
4698 (while
4699 (and
4700 (looking-at c-identifier-key)
4701
4702 (progn
4703 ;; Check for keyword. We go to the last symbol in
4704 ;; `c-identifier-key' first.
4705 (goto-char (setq id-end (match-end 0)))
4706 (c-simple-skip-symbol-backward)
4707 (setq id-start (point))
4708
4709 (if (looking-at c-keywords-regexp)
4710 (when (and (c-major-mode-is 'c++-mode)
4711 (looking-at
4712 (cc-eval-when-compile
4713 (concat "\\(operator\\|\\(template\\)\\)"
4714 "\\(" (c-lang-const c-nonsymbol-key c++)
4715 "\\|$\\)")))
4716 (if (match-beginning 2)
4717 ;; "template" is only valid inside an
4718 ;; identifier if preceded by "::".
4719 (save-excursion
4720 (c-backward-syntactic-ws)
4721 (and (c-safe (backward-char 2) t)
4722 (looking-at "::")))
4723 t))
4724
4725 ;; Handle a C++ operator or template identifier.
4726 (goto-char id-end)
4727 (c-forward-syntactic-ws)
4728 (cond ((eq (char-before id-end) ?e)
4729 ;; Got "... ::template".
4730 (let ((subres (c-forward-name)))
4731 (when subres
4732 (setq pos (point)
4733 res subres))))
4734
4735 ((looking-at c-identifier-start)
4736 ;; Got a cast operator.
4737 (when (c-forward-type)
4738 (setq pos (point)
4739 res 'operator)
4740 ;; Now we should match a sequence of either
4741 ;; '*', '&' or a name followed by ":: *",
4742 ;; where each can be followed by a sequence
4743 ;; of `c-opt-type-modifier-key'.
4744 (while (cond ((looking-at "[*&]")
4745 (goto-char (match-end 0))
4746 t)
4747 ((looking-at c-identifier-start)
4748 (and (c-forward-name)
4749 (looking-at "::")
4750 (progn
4751 (goto-char (match-end 0))
4752 (c-forward-syntactic-ws)
4753 (eq (char-after) ?*))
4754 (progn
4755 (forward-char)
4756 t))))
4757 (while (progn
4758 (c-forward-syntactic-ws)
4759 (setq pos (point))
4760 (looking-at c-opt-type-modifier-key))
4761 (goto-char (match-end 1))))))
4762
4763 ((looking-at c-overloadable-operators-regexp)
4764 ;; Got some other operator.
4765 (setq c-last-identifier-range
4766 (cons (point) (match-end 0)))
4767 (goto-char (match-end 0))
4768 (c-forward-syntactic-ws)
4769 (setq pos (point)
4770 res 'operator)))
4771
4772 nil)
4773
4774 ;; `id-start' is equal to `id-end' if we've jumped over
4775 ;; an identifier that doesn't end with a symbol token.
4776 ;; That can occur e.g. for Java import directives on the
4777 ;; form "foo.bar.*".
4778 (when (and id-start (/= id-start id-end))
4779 (setq c-last-identifier-range
4780 (cons id-start id-end)))
4781 (goto-char id-end)
4782 (c-forward-syntactic-ws)
4783 (setq pos (point)
4784 res t)))
4785
4786 (progn
4787 (goto-char pos)
4788 (when (or c-opt-identifier-concat-key
4789 c-recognize-<>-arglists)
4790
4791 (cond
4792 ((and c-opt-identifier-concat-key
4793 (looking-at c-opt-identifier-concat-key))
4794 ;; Got a concatenated identifier. This handles the
4795 ;; cases with tricky syntactic whitespace that aren't
4796 ;; covered in `c-identifier-key'.
4797 (goto-char (match-end 0))
4798 (c-forward-syntactic-ws)
4799 t)
4800
4801 ((and c-recognize-<>-arglists
4802 (eq (char-after) ?<))
4803 ;; Maybe an angle bracket arglist.
4804
4805 (when (let (c-record-type-identifiers
4806 c-record-found-types)
4807 (c-forward-<>-arglist nil))
4808
4809 (c-add-type start (1+ pos))
4810 (c-forward-syntactic-ws)
4811 (setq pos (point)
4812 c-last-identifier-range nil)
4813
4814 (if (and c-opt-identifier-concat-key
4815 (looking-at c-opt-identifier-concat-key))
4816
4817 ;; Continue if there's an identifier concatenation
4818 ;; operator after the template argument.
4819 (progn
4820 (when (and c-record-type-identifiers id-start)
4821 (c-record-ref-id (cons id-start id-end)))
4822 (forward-char 2)
4823 (c-forward-syntactic-ws)
4824 t)
4825
4826 (when (and c-record-type-identifiers id-start)
4827 (c-record-type-id (cons id-start id-end)))
4828 (setq res 'template)
4829 nil)))
4830 )))))
4831
4832 (goto-char pos)
4833 res))
4834
4835 (defun c-forward-type ()
4836 ;; Move forward over a type spec if at the beginning of one,
4837 ;; stopping at the next following token. Return t if it's a known
4838 ;; type that can't be a name or other expression, 'known if it's an
4839 ;; otherwise known type (according to `*-font-lock-extra-types'),
4840 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4841 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4842 ;; that might be a type, or nil if it can't be a type (the point
4843 ;; isn't moved then). The point is assumed to be at the beginning
4844 ;; of a token.
4845 ;;
4846 ;; Note that this function doesn't skip past the brace definition
4847 ;; that might be considered part of the type, e.g.
4848 ;; "enum {a, b, c} foo".
4849 ;;
4850 ;; This function records identifier ranges on
4851 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4852 ;; `c-record-type-identifiers' is non-nil.
4853 ;;
4854 ;; This function might do hidden buffer changes.
4855
4856 (let ((start (point)) pos res name-res id-start id-end id-range)
4857
4858 ;; Skip leading type modifiers. If any are found we know it's a
4859 ;; prefix of a type.
4860 (when c-opt-type-modifier-key
4861 (while (looking-at c-opt-type-modifier-key)
4862 (goto-char (match-end 1))
4863 (c-forward-syntactic-ws)
4864 (setq res 'prefix)))
4865
4866 (cond
4867 ((looking-at c-type-prefix-key)
4868 ;; Looking at a keyword that prefixes a type identifier,
4869 ;; e.g. "class".
4870 (goto-char (match-end 1))
4871 (c-forward-syntactic-ws)
4872 (setq pos (point))
4873 (if (memq (setq name-res (c-forward-name)) '(t template))
4874 (progn
4875 (when (eq name-res t)
4876 ;; In many languages the name can be used without the
4877 ;; prefix, so we add it to `c-found-types'.
4878 (c-add-type pos (point))
4879 (when (and c-record-type-identifiers
4880 c-last-identifier-range)
4881 (c-record-type-id c-last-identifier-range)))
4882 (setq res t))
4883 ;; Invalid syntax.
4884 (goto-char start)
4885 (setq res nil)))
4886
4887 ((progn
4888 (setq pos nil)
4889 (if (looking-at c-identifier-start)
4890 (save-excursion
4891 (setq id-start (point)
4892 name-res (c-forward-name))
4893 (when name-res
4894 (setq id-end (point)
4895 id-range c-last-identifier-range))))
4896 (and (cond ((looking-at c-primitive-type-key)
4897 (setq res t))
4898 ((c-with-syntax-table c-identifier-syntax-table
4899 (looking-at c-known-type-key))
4900 (setq res 'known)))
4901 (or (not id-end)
4902 (>= (save-excursion
4903 (save-match-data
4904 (goto-char (match-end 1))
4905 (c-forward-syntactic-ws)
4906 (setq pos (point))))
4907 id-end)
4908 (setq res nil))))
4909 ;; Looking at a primitive or known type identifier. We've
4910 ;; checked for a name first so that we don't go here if the
4911 ;; known type match only is a prefix of another name.
4912
4913 (setq id-end (match-end 1))
4914
4915 (when (and c-record-type-identifiers
4916 (or c-promote-possible-types (eq res t)))
4917 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4918
4919 (if (and c-opt-type-component-key
4920 (save-match-data
4921 (looking-at c-opt-type-component-key)))
4922 ;; There might be more keywords for the type.
4923 (let (safe-pos)
4924 (c-forward-keyword-clause 1)
4925 (while (progn
4926 (setq safe-pos (point))
4927 (looking-at c-opt-type-component-key))
4928 (when (and c-record-type-identifiers
4929 (looking-at c-primitive-type-key))
4930 (c-record-type-id (cons (match-beginning 1)
4931 (match-end 1))))
4932 (c-forward-keyword-clause 1))
4933 (if (looking-at c-primitive-type-key)
4934 (progn
4935 (when c-record-type-identifiers
4936 (c-record-type-id (cons (match-beginning 1)
4937 (match-end 1))))
4938 (c-forward-keyword-clause 1)
4939 (setq res t))
4940 (goto-char safe-pos)
4941 (setq res 'prefix)))
4942 (unless (save-match-data (c-forward-keyword-clause 1))
4943 (if pos
4944 (goto-char pos)
4945 (goto-char (match-end 1))
4946 (c-forward-syntactic-ws)))))
4947
4948 (name-res
4949 (cond ((eq name-res t)
4950 ;; A normal identifier.
4951 (goto-char id-end)
4952 (if (or res c-promote-possible-types)
4953 (progn
4954 (c-add-type id-start id-end)
4955 (when (and c-record-type-identifiers id-range)
4956 (c-record-type-id id-range))
4957 (unless res
4958 (setq res 'found)))
4959 (setq res (if (c-check-type id-start id-end)
4960 ;; It's an identifier that has been used as
4961 ;; a type somewhere else.
4962 'found
4963 ;; It's an identifier that might be a type.
4964 'maybe))))
4965 ((eq name-res 'template)
4966 ;; A template is a type.
4967 (goto-char id-end)
4968 (setq res t))
4969 (t
4970 ;; Otherwise it's an operator identifier, which is not a type.
4971 (goto-char start)
4972 (setq res nil)))))
4973
4974 (when res
4975 ;; Skip trailing type modifiers. If any are found we know it's
4976 ;; a type.
4977 (when c-opt-type-modifier-key
4978 (while (looking-at c-opt-type-modifier-key)
4979 (goto-char (match-end 1))
4980 (c-forward-syntactic-ws)
4981 (setq res t)))
4982
4983 ;; Step over any type suffix operator. Do not let the existence
4984 ;; of these alter the classification of the found type, since
4985 ;; these operators typically are allowed in normal expressions
4986 ;; too.
4987 (when c-opt-type-suffix-key
4988 (while (looking-at c-opt-type-suffix-key)
4989 (goto-char (match-end 1))
4990 (c-forward-syntactic-ws)))
4991
4992 (when c-opt-type-concat-key
4993 ;; Look for a trailing operator that concatenates the type
4994 ;; with a following one, and if so step past that one through
4995 ;; a recursive call. Note that we don't record concatenated
4996 ;; types in `c-found-types' - it's the component types that
4997 ;; are recorded when appropriate.
4998 (setq pos (point))
4999 (let* ((c-promote-possible-types (or (memq res '(t known))
5000 c-promote-possible-types))
5001 ;; If we can't promote then set `c-record-found-types' so that
5002 ;; we can merge in the types from the second part afterwards if
5003 ;; it turns out to be a known type there.
5004 (c-record-found-types (and c-record-type-identifiers
5005 (not c-promote-possible-types)))
5006 subres)
5007 (if (and (looking-at c-opt-type-concat-key)
5008
5009 (progn
5010 (goto-char (match-end 1))
5011 (c-forward-syntactic-ws)
5012 (setq subres (c-forward-type))))
5013
5014 (progn
5015 ;; If either operand certainly is a type then both are, but we
5016 ;; don't let the existence of the operator itself promote two
5017 ;; uncertain types to a certain one.
5018 (cond ((eq res t))
5019 ((eq subres t)
5020 (unless (eq name-res 'template)
5021 (c-add-type id-start id-end))
5022 (when (and c-record-type-identifiers id-range)
5023 (c-record-type-id id-range))
5024 (setq res t))
5025 ((eq res 'known))
5026 ((eq subres 'known)
5027 (setq res 'known))
5028 ((eq res 'found))
5029 ((eq subres 'found)
5030 (setq res 'found))
5031 (t
5032 (setq res 'maybe)))
5033
5034 (when (and (eq res t)
5035 (consp c-record-found-types))
5036 ;; Merge in the ranges of any types found by the second
5037 ;; `c-forward-type'.
5038 (setq c-record-type-identifiers
5039 ;; `nconc' doesn't mind that the tail of
5040 ;; `c-record-found-types' is t.
5041 (nconc c-record-found-types
5042 c-record-type-identifiers))))
5043
5044 (goto-char pos))))
5045
5046 (when (and c-record-found-types (memq res '(known found)) id-range)
5047 (setq c-record-found-types
5048 (cons id-range c-record-found-types))))
5049
5050 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
5051
5052 res))
5053
5054 \f
5055 ;; Handling of large scale constructs like statements and declarations.
5056
5057 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
5058 ;; defsubst or perhaps even a defun, but it contains lots of free
5059 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
5060 (defmacro c-fdoc-shift-type-backward (&optional short)
5061 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
5062 ;; of types when parsing a declaration, which means that it
5063 ;; sometimes consumes the identifier in the declaration as a type.
5064 ;; This is used to "backtrack" and make the last type be treated as
5065 ;; an identifier instead.
5066 `(progn
5067 ,(unless short
5068 ;; These identifiers are bound only in the inner let.
5069 '(setq identifier-type at-type
5070 identifier-start type-start
5071 got-parens nil
5072 got-identifier t
5073 got-suffix t
5074 got-suffix-after-parens id-start
5075 paren-depth 0))
5076
5077 (if (setq at-type (if (eq backup-at-type 'prefix)
5078 t
5079 backup-at-type))
5080 (setq type-start backup-type-start
5081 id-start backup-id-start)
5082 (setq type-start start-pos
5083 id-start start-pos))
5084
5085 ;; When these flags already are set we've found specifiers that
5086 ;; unconditionally signal these attributes - backtracking doesn't
5087 ;; change that. So keep them set in that case.
5088 (or at-type-decl
5089 (setq at-type-decl backup-at-type-decl))
5090 (or maybe-typeless
5091 (setq maybe-typeless backup-maybe-typeless))
5092
5093 ,(unless short
5094 ;; This identifier is bound only in the inner let.
5095 '(setq start id-start))))
5096
5097 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
5098 ;; Move forward over a declaration or a cast if at the start of one.
5099 ;; The point is assumed to be at the start of some token. Nil is
5100 ;; returned if no declaration or cast is recognized, and the point
5101 ;; is clobbered in that case.
5102 ;;
5103 ;; If a declaration is parsed:
5104 ;;
5105 ;; The point is left at the first token after the first complete
5106 ;; declarator, if there is one. The return value is a cons where
5107 ;; the car is the position of the first token in the declarator. (See
5108 ;; below for the cdr.)
5109 ;; Some examples:
5110 ;;
5111 ;; void foo (int a, char *b) stuff ...
5112 ;; car ^ ^ point
5113 ;; float (*a)[], b;
5114 ;; car ^ ^ point
5115 ;; unsigned int a = c_style_initializer, b;
5116 ;; car ^ ^ point
5117 ;; unsigned int a (cplusplus_style_initializer), b;
5118 ;; car ^ ^ point (might change)
5119 ;; class Foo : public Bar {}
5120 ;; car ^ ^ point
5121 ;; class PikeClass (int a, string b) stuff ...
5122 ;; car ^ ^ point
5123 ;; enum bool;
5124 ;; car ^ ^ point
5125 ;; enum bool flag;
5126 ;; car ^ ^ point
5127 ;; void cplusplus_function (int x) throw (Bad);
5128 ;; car ^ ^ point
5129 ;; Foo::Foo (int b) : Base (b) {}
5130 ;; car ^ ^ point
5131 ;;
5132 ;; The cdr of the return value is non-nil iff a `c-typedef-decl-kwds'
5133 ;; specifier (e.g. class, struct, enum, typedef) is found in the
5134 ;; declaration, i.e. the declared identifier(s) are types.
5135 ;;
5136 ;; If a cast is parsed:
5137 ;;
5138 ;; The point is left at the first token after the closing paren of
5139 ;; the cast. The return value is `cast'. Note that the start
5140 ;; position must be at the first token inside the cast parenthesis
5141 ;; to recognize it.
5142 ;;
5143 ;; PRECEDING-TOKEN-END is the first position after the preceding
5144 ;; token, i.e. on the other side of the syntactic ws from the point.
5145 ;; Use a value less than or equal to (point-min) if the point is at
5146 ;; the first token in (the visible part of) the buffer.
5147 ;;
5148 ;; CONTEXT is a symbol that describes the context at the point:
5149 ;; 'decl In a comma-separated declaration context (typically
5150 ;; inside a function declaration arglist).
5151 ;; '<> In an angle bracket arglist.
5152 ;; 'arglist Some other type of arglist.
5153 ;; nil Some other context or unknown context. Includes
5154 ;; within the parens of an if, for, ... construct.
5155 ;;
5156 ;; LAST-CAST-END is the first token after the closing paren of a
5157 ;; preceding cast, or nil if none is known. If
5158 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
5159 ;; the position after the closest preceding call where a cast was
5160 ;; matched. In that case it's used to discover chains of casts like
5161 ;; "(a) (b) c".
5162 ;;
5163 ;; This function records identifier ranges on
5164 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5165 ;; `c-record-type-identifiers' is non-nil.
5166 ;;
5167 ;; This function might do hidden buffer changes.
5168
5169 (let (;; `start-pos' is used below to point to the start of the
5170 ;; first type, i.e. after any leading specifiers. It might
5171 ;; also point at the beginning of the preceding syntactic
5172 ;; whitespace.
5173 (start-pos (point))
5174 ;; Set to the result of `c-forward-type'.
5175 at-type
5176 ;; The position of the first token in what we currently
5177 ;; believe is the type in the declaration or cast, after any
5178 ;; specifiers and their associated clauses.
5179 type-start
5180 ;; The position of the first token in what we currently
5181 ;; believe is the declarator for the first identifier. Set
5182 ;; when the type is found, and moved forward over any
5183 ;; `c-decl-hangon-kwds' and their associated clauses that
5184 ;; occurs after the type.
5185 id-start
5186 ;; These store `at-type', `type-start' and `id-start' of the
5187 ;; identifier before the one in those variables. The previous
5188 ;; identifier might turn out to be the real type in a
5189 ;; declaration if the last one has to be the declarator in it.
5190 ;; If `backup-at-type' is nil then the other variables have
5191 ;; undefined values.
5192 backup-at-type backup-type-start backup-id-start
5193 ;; Set if we've found a specifier that makes the defined
5194 ;; identifier(s) types.
5195 at-type-decl
5196 ;; Set if we've found a specifier that can start a declaration
5197 ;; where there's no type.
5198 maybe-typeless
5199 ;; If a specifier is found that also can be a type prefix,
5200 ;; these flags are set instead of those above. If we need to
5201 ;; back up an identifier, they are copied to the real flag
5202 ;; variables. Thus they only take effect if we fail to
5203 ;; interpret it as a type.
5204 backup-at-type-decl backup-maybe-typeless
5205 ;; Whether we've found a declaration or a cast. We might know
5206 ;; this before we've found the type in it. It's 'ids if we've
5207 ;; found two consecutive identifiers (usually a sure sign, but
5208 ;; we should allow that in labels too), and t if we've found a
5209 ;; specifier keyword (a 100% sure sign).
5210 at-decl-or-cast
5211 ;; Set when we need to back up to parse this as a declaration
5212 ;; but not as a cast.
5213 backup-if-not-cast
5214 ;; For casts, the return position.
5215 cast-end
5216 ;; Save `c-record-type-identifiers' and
5217 ;; `c-record-ref-identifiers' since ranges are recorded
5218 ;; speculatively and should be thrown away if it turns out
5219 ;; that it isn't a declaration or cast.
5220 (save-rec-type-ids c-record-type-identifiers)
5221 (save-rec-ref-ids c-record-ref-identifiers))
5222
5223 ;; Check for a type. Unknown symbols are treated as possible
5224 ;; types, but they could also be specifiers disguised through
5225 ;; macros like __INLINE__, so we recognize both types and known
5226 ;; specifiers after them too.
5227 (while
5228 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
5229
5230 ;; Look for a specifier keyword clause.
5231 (when (looking-at c-prefix-spec-kwds-re)
5232 (setq kwd-sym (c-keyword-sym (match-string 1)))
5233 (save-excursion
5234 (c-forward-keyword-clause 1)
5235 (setq kwd-clause-end (point))))
5236
5237 (when (setq found-type (c-forward-type))
5238 ;; Found a known or possible type or a prefix of a known type.
5239
5240 (when at-type
5241 ;; Got two identifiers with nothing but whitespace
5242 ;; between them. That can only happen in declarations.
5243 (setq at-decl-or-cast 'ids)
5244
5245 (when (eq at-type 'found)
5246 ;; If the previous identifier is a found type we
5247 ;; record it as a real one; it might be some sort of
5248 ;; alias for a prefix like "unsigned".
5249 (save-excursion
5250 (goto-char type-start)
5251 (let ((c-promote-possible-types t))
5252 (c-forward-type)))))
5253
5254 (setq backup-at-type at-type
5255 backup-type-start type-start
5256 backup-id-start id-start
5257 at-type found-type
5258 type-start start
5259 id-start (point)
5260 ;; The previous ambiguous specifier/type turned out
5261 ;; to be a type since we've parsed another one after
5262 ;; it, so clear these backup flags.
5263 backup-at-type-decl nil
5264 backup-maybe-typeless nil))
5265
5266 (if kwd-sym
5267 (progn
5268 ;; Handle known specifier keywords and
5269 ;; `c-decl-hangon-kwds' which can occur after known
5270 ;; types.
5271
5272 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
5273 ;; It's a hang-on keyword that can occur anywhere.
5274 (progn
5275 (setq at-decl-or-cast t)
5276 (if at-type
5277 ;; Move the identifier start position if
5278 ;; we've passed a type.
5279 (setq id-start kwd-clause-end)
5280 ;; Otherwise treat this as a specifier and
5281 ;; move the fallback position.
5282 (setq start-pos kwd-clause-end))
5283 (goto-char kwd-clause-end))
5284
5285 ;; It's an ordinary specifier so we know that
5286 ;; anything before this can't be the type.
5287 (setq backup-at-type nil
5288 start-pos kwd-clause-end)
5289
5290 (if found-type
5291 ;; It's ambiguous whether this keyword is a
5292 ;; specifier or a type prefix, so set the backup
5293 ;; flags. (It's assumed that `c-forward-type'
5294 ;; moved further than `c-forward-keyword-clause'.)
5295 (progn
5296 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5297 (setq backup-at-type-decl t))
5298 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5299 (setq backup-maybe-typeless t)))
5300
5301 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5302 (setq at-type-decl t))
5303 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5304 (setq maybe-typeless t))
5305
5306 ;; Haven't matched a type so it's an umambiguous
5307 ;; specifier keyword and we know we're in a
5308 ;; declaration.
5309 (setq at-decl-or-cast t)
5310
5311 (goto-char kwd-clause-end))))
5312
5313 ;; If the type isn't known we continue so that we'll jump
5314 ;; over all specifiers and type identifiers. The reason
5315 ;; to do this for a known type prefix is to make things
5316 ;; like "unsigned INT16" work.
5317 (and found-type (not (eq found-type t))))))
5318
5319 (cond
5320 ((eq at-type t)
5321 ;; If a known type was found, we still need to skip over any
5322 ;; hangon keyword clauses after it. Otherwise it has already
5323 ;; been done in the loop above.
5324 (while (looking-at c-decl-hangon-key)
5325 (c-forward-keyword-clause 1))
5326 (setq id-start (point)))
5327
5328 ((eq at-type 'prefix)
5329 ;; A prefix type is itself a primitive type when it's not
5330 ;; followed by another type.
5331 (setq at-type t))
5332
5333 ((not at-type)
5334 ;; Got no type but set things up to continue anyway to handle
5335 ;; the various cases when a declaration doesn't start with a
5336 ;; type.
5337 (setq id-start start-pos))
5338
5339 ((and (eq at-type 'maybe)
5340 (c-major-mode-is 'c++-mode))
5341 ;; If it's C++ then check if the last "type" ends on the form
5342 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
5343 ;; (con|de)structor.
5344 (save-excursion
5345 (let (name end-2 end-1)
5346 (goto-char id-start)
5347 (c-backward-syntactic-ws)
5348 (setq end-2 (point))
5349 (when (and
5350 (c-simple-skip-symbol-backward)
5351 (progn
5352 (setq name
5353 (buffer-substring-no-properties (point) end-2))
5354 ;; Cheating in the handling of syntactic ws below.
5355 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
5356 (progn
5357 (setq end-1 (point))
5358 (c-simple-skip-symbol-backward))
5359 (>= (point) type-start)
5360 (equal (buffer-substring-no-properties (point) end-1)
5361 name))
5362 ;; It is a (con|de)structor name. In that case the
5363 ;; declaration is typeless so zap out any preceding
5364 ;; identifier(s) that we might have taken as types.
5365 (goto-char type-start)
5366 (setq at-type nil
5367 backup-at-type nil
5368 id-start type-start))))))
5369
5370 ;; Check for and step over a type decl expression after the thing
5371 ;; that is or might be a type. This can't be skipped since we
5372 ;; need the correct end position of the declarator for
5373 ;; `max-type-decl-end-*'.
5374 (let ((start (point)) (paren-depth 0) pos
5375 ;; True if there's a non-open-paren match of
5376 ;; `c-type-decl-prefix-key'.
5377 got-prefix
5378 ;; True if the declarator is surrounded by a parenthesis pair.
5379 got-parens
5380 ;; True if there is an identifier in the declarator.
5381 got-identifier
5382 ;; True if there's a non-close-paren match of
5383 ;; `c-type-decl-suffix-key'.
5384 got-suffix
5385 ;; True if there's a prefix match outside the outermost
5386 ;; paren pair that surrounds the declarator.
5387 got-prefix-before-parens
5388 ;; True if there's a suffix match outside the outermost
5389 ;; paren pair that surrounds the declarator. The value is
5390 ;; the position of the first suffix match.
5391 got-suffix-after-parens
5392 ;; True if we've parsed the type decl to a token that is
5393 ;; known to end declarations in this context.
5394 at-decl-end
5395 ;; The earlier values of `at-type' and `type-start' if we've
5396 ;; shifted the type backwards.
5397 identifier-type identifier-start
5398 ;; If `c-parse-and-markup-<>-arglists' is set we need to
5399 ;; turn it off during the name skipping below to avoid
5400 ;; getting `c-type' properties that might be bogus. That
5401 ;; can happen since we don't know if
5402 ;; `c-restricted-<>-arglists' will be correct inside the
5403 ;; arglist paren that gets entered.
5404 c-parse-and-markup-<>-arglists)
5405
5406 (goto-char id-start)
5407
5408 ;; Skip over type decl prefix operators. (Note similar code in
5409 ;; `c-font-lock-declarators'.)
5410 (while (and (looking-at c-type-decl-prefix-key)
5411 (if (and (c-major-mode-is 'c++-mode)
5412 (match-beginning 2))
5413 ;; If the second submatch matches in C++ then
5414 ;; we're looking at an identifier that's a
5415 ;; prefix only if it specifies a member pointer.
5416 (when (setq got-identifier (c-forward-name))
5417 (if (looking-at "\\(::\\)")
5418 ;; We only check for a trailing "::" and
5419 ;; let the "*" that should follow be
5420 ;; matched in the next round.
5421 (progn (setq got-identifier nil) t)
5422 ;; It turned out to be the real identifier,
5423 ;; so stop.
5424 nil))
5425 t))
5426
5427 (if (eq (char-after) ?\()
5428 (progn
5429 (setq paren-depth (1+ paren-depth))
5430 (forward-char))
5431 (unless got-prefix-before-parens
5432 (setq got-prefix-before-parens (= paren-depth 0)))
5433 (setq got-prefix t)
5434 (goto-char (match-end 1)))
5435 (c-forward-syntactic-ws))
5436
5437 (setq got-parens (> paren-depth 0))
5438
5439 ;; Skip over an identifier.
5440 (or got-identifier
5441 (and (looking-at c-identifier-start)
5442 (setq got-identifier (c-forward-name))))
5443
5444 ;; Skip over type decl suffix operators.
5445 (while (if (looking-at c-type-decl-suffix-key)
5446
5447 (if (eq (char-after) ?\))
5448 (when (> paren-depth 0)
5449 (setq paren-depth (1- paren-depth))
5450 (forward-char)
5451 t)
5452 (when (if (save-match-data (looking-at "\\s\("))
5453 (c-safe (c-forward-sexp 1) t)
5454 (goto-char (match-end 1))
5455 t)
5456 (when (and (not got-suffix-after-parens)
5457 (= paren-depth 0))
5458 (setq got-suffix-after-parens (match-beginning 0)))
5459 (setq got-suffix t)))
5460
5461 ;; No suffix matched. We might have matched the
5462 ;; identifier as a type and the open paren of a
5463 ;; function arglist as a type decl prefix. In that
5464 ;; case we should "backtrack": Reinterpret the last
5465 ;; type as the identifier, move out of the arglist and
5466 ;; continue searching for suffix operators.
5467 ;;
5468 ;; Do this even if there's no preceding type, to cope
5469 ;; with old style function declarations in K&R C,
5470 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
5471 ;; style declarations. That isn't applicable in an
5472 ;; arglist context, though.
5473 (when (and (= paren-depth 1)
5474 (not got-prefix-before-parens)
5475 (not (eq at-type t))
5476 (or backup-at-type
5477 maybe-typeless
5478 backup-maybe-typeless
5479 (when c-recognize-typeless-decls
5480 (not context)))
5481 (setq pos (c-up-list-forward (point)))
5482 (eq (char-before pos) ?\)))
5483 (c-fdoc-shift-type-backward)
5484 (goto-char pos)
5485 t))
5486
5487 (c-forward-syntactic-ws))
5488
5489 (when (and (or maybe-typeless backup-maybe-typeless)
5490 (not got-identifier)
5491 (not got-prefix)
5492 at-type)
5493 ;; Have found no identifier but `c-typeless-decl-kwds' has
5494 ;; matched so we know we're inside a declaration. The
5495 ;; preceding type must be the identifier instead.
5496 (c-fdoc-shift-type-backward))
5497
5498 (setq
5499 at-decl-or-cast
5500 (catch 'at-decl-or-cast
5501
5502 ;; CASE 1
5503 (when (> paren-depth 0)
5504 ;; Encountered something inside parens that isn't matched by
5505 ;; the `c-type-decl-*' regexps, so it's not a type decl
5506 ;; expression. Try to skip out to the same paren depth to
5507 ;; not confuse the cast check below.
5508 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
5509 ;; If we've found a specifier keyword then it's a
5510 ;; declaration regardless.
5511 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
5512
5513 (setq at-decl-end
5514 (looking-at (cond ((eq context '<>) "[,>]")
5515 (context "[,\)]")
5516 (t "[,;]"))))
5517
5518 ;; Now we've collected info about various characteristics of
5519 ;; the construct we're looking at. Below follows a decision
5520 ;; tree based on that. It's ordered to check more certain
5521 ;; signs before less certain ones.
5522
5523 (if got-identifier
5524 (progn
5525
5526 ;; CASE 2
5527 (when (and (or at-type maybe-typeless)
5528 (not (or got-prefix got-parens)))
5529 ;; Got another identifier directly after the type, so it's a
5530 ;; declaration.
5531 (throw 'at-decl-or-cast t))
5532
5533 (when (and got-parens
5534 (not got-prefix)
5535 (not got-suffix-after-parens)
5536 (or backup-at-type
5537 maybe-typeless
5538 backup-maybe-typeless))
5539 ;; Got a declaration of the form "foo bar (gnu);" where we've
5540 ;; recognized "bar" as the type and "gnu" as the declarator.
5541 ;; In this case it's however more likely that "bar" is the
5542 ;; declarator and "gnu" a function argument or initializer (if
5543 ;; `c-recognize-paren-inits' is set), since the parens around
5544 ;; "gnu" would be superfluous if it's a declarator. Shift the
5545 ;; type one step backward.
5546 (c-fdoc-shift-type-backward)))
5547
5548 ;; Found no identifier.
5549
5550 (if backup-at-type
5551 (progn
5552
5553 ;; CASE 3
5554 (when (= (point) start)
5555 ;; Got a plain list of identifiers. If a colon follows it's
5556 ;; a valid label. Otherwise the last one probably is the
5557 ;; declared identifier and we should back up to the previous
5558 ;; type, providing it isn't a cast.
5559 (if (eq (char-after) ?:)
5560 ;; If we've found a specifier keyword then it's a
5561 ;; declaration regardless.
5562 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
5563 (setq backup-if-not-cast t)
5564 (throw 'at-decl-or-cast t)))
5565
5566 ;; CASE 4
5567 (when (and got-suffix
5568 (not got-prefix)
5569 (not got-parens))
5570 ;; Got a plain list of identifiers followed by some suffix.
5571 ;; If this isn't a cast then the last identifier probably is
5572 ;; the declared one and we should back up to the previous
5573 ;; type.
5574 (setq backup-if-not-cast t)
5575 (throw 'at-decl-or-cast t)))
5576
5577 ;; CASE 5
5578 (when (eq at-type t)
5579 ;; If the type is known we know that there can't be any
5580 ;; identifier somewhere else, and it's only in declarations in
5581 ;; e.g. function prototypes and in casts that the identifier may
5582 ;; be left out.
5583 (throw 'at-decl-or-cast t))
5584
5585 (when (= (point) start)
5586 ;; Only got a single identifier (parsed as a type so far).
5587 ;; CASE 6
5588 (if (and
5589 ;; Check that the identifier isn't at the start of an
5590 ;; expression.
5591 at-decl-end
5592 (cond
5593 ((eq context 'decl)
5594 ;; Inside an arglist that contains declarations. If K&R
5595 ;; style declarations and parenthesis style initializers
5596 ;; aren't allowed then the single identifier must be a
5597 ;; type, else we require that it's known or found
5598 ;; (primitive types are handled above).
5599 (or (and (not c-recognize-knr-p)
5600 (not c-recognize-paren-inits))
5601 (memq at-type '(known found))))
5602 ((eq context '<>)
5603 ;; Inside a template arglist. Accept known and found
5604 ;; types; other identifiers could just as well be
5605 ;; constants in C++.
5606 (memq at-type '(known found)))))
5607 (throw 'at-decl-or-cast t)
5608 ;; CASE 7
5609 ;; Can't be a valid declaration or cast, but if we've found a
5610 ;; specifier it can't be anything else either, so treat it as
5611 ;; an invalid/unfinished declaration or cast.
5612 (throw 'at-decl-or-cast at-decl-or-cast))))
5613
5614 (if (and got-parens
5615 (not got-prefix)
5616 (not context)
5617 (not (eq at-type t))
5618 (or backup-at-type
5619 maybe-typeless
5620 backup-maybe-typeless
5621 (when c-recognize-typeless-decls
5622 (or (not got-suffix)
5623 (not (looking-at
5624 c-after-suffixed-type-maybe-decl-key))))))
5625 ;; Got an empty paren pair and a preceding type that probably
5626 ;; really is the identifier. Shift the type backwards to make
5627 ;; the last one the identifier. This is analogous to the
5628 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
5629 ;; above.
5630 ;;
5631 ;; Exception: In addition to the conditions in that
5632 ;; "backtracking" code, do not shift backward if we're not
5633 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
5634 ;; Since there's no preceding type, the shift would mean that
5635 ;; the declaration is typeless. But if the regexp doesn't match
5636 ;; then we will simply fall through in the tests below and not
5637 ;; recognize it at all, so it's better to try it as an abstract
5638 ;; declarator instead.
5639 (c-fdoc-shift-type-backward)
5640
5641 ;; Still no identifier.
5642 ;; CASE 8
5643 (when (and got-prefix (or got-parens got-suffix))
5644 ;; Require `got-prefix' together with either `got-parens' or
5645 ;; `got-suffix' to recognize it as an abstract declarator:
5646 ;; `got-parens' only is probably an empty function call.
5647 ;; `got-suffix' only can build an ordinary expression together
5648 ;; with the preceding identifier which we've taken as a type.
5649 ;; We could actually accept on `got-prefix' only, but that can
5650 ;; easily occur temporarily while writing an expression so we
5651 ;; avoid that case anyway. We could do a better job if we knew
5652 ;; the point when the fontification was invoked.
5653 (throw 'at-decl-or-cast t))
5654
5655 ;; CASE 9
5656 (when (and at-type
5657 (not got-prefix)
5658 (not got-parens)
5659 got-suffix-after-parens
5660 (eq (char-after got-suffix-after-parens) ?\())
5661 ;; Got a type, no declarator but a paren suffix. I.e. it's a
5662 ;; normal function call afterall (or perhaps a C++ style object
5663 ;; instantiation expression).
5664 (throw 'at-decl-or-cast nil))))
5665
5666 ;; CASE 10
5667 (when at-decl-or-cast
5668 ;; By now we've located the type in the declaration that we know
5669 ;; we're in.
5670 (throw 'at-decl-or-cast t))
5671
5672 ;; CASE 11
5673 (when (and got-identifier
5674 (not context)
5675 (looking-at c-after-suffixed-type-decl-key)
5676 (if (and got-parens
5677 (not got-prefix)
5678 (not got-suffix)
5679 (not (eq at-type t)))
5680 ;; Shift the type backward in the case that there's a
5681 ;; single identifier inside parens. That can only
5682 ;; occur in K&R style function declarations so it's
5683 ;; more likely that it really is a function call.
5684 ;; Therefore we only do this after
5685 ;; `c-after-suffixed-type-decl-key' has matched.
5686 (progn (c-fdoc-shift-type-backward) t)
5687 got-suffix-after-parens))
5688 ;; A declaration according to `c-after-suffixed-type-decl-key'.
5689 (throw 'at-decl-or-cast t))
5690
5691 ;; CASE 12
5692 (when (and (or got-prefix (not got-parens))
5693 (memq at-type '(t known)))
5694 ;; It's a declaration if a known type precedes it and it can't be a
5695 ;; function call.
5696 (throw 'at-decl-or-cast t))
5697
5698 ;; If we get here we can't tell if this is a type decl or a normal
5699 ;; expression by looking at it alone. (That's under the assumption
5700 ;; that normal expressions always can look like type decl expressions,
5701 ;; which isn't really true but the cases where it doesn't hold are so
5702 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
5703 ;; the effort to look for them.)
5704
5705 (unless (or at-decl-end (looking-at "=[^=]"))
5706 ;; If this is a declaration it should end here or its initializer(*)
5707 ;; should start here, so check for allowed separation tokens. Note
5708 ;; that this rule doesn't work e.g. with a K&R arglist after a
5709 ;; function header.
5710 ;;
5711 ;; *) Don't check for C++ style initializers using parens
5712 ;; since those already have been matched as suffixes.
5713 ;;
5714 ;; If `at-decl-or-cast' is then we've found some other sign that
5715 ;; it's a declaration or cast, so then it's probably an
5716 ;; invalid/unfinished one.
5717 (throw 'at-decl-or-cast at-decl-or-cast))
5718
5719 ;; Below are tests that only should be applied when we're certain to
5720 ;; not have parsed halfway through an expression.
5721
5722 ;; CASE 14
5723 (when (memq at-type '(t known))
5724 ;; The expression starts with a known type so treat it as a
5725 ;; declaration.
5726 (throw 'at-decl-or-cast t))
5727
5728 ;; CASE 15
5729 (when (and (c-major-mode-is 'c++-mode)
5730 ;; In C++ we check if the identifier is a known type, since
5731 ;; (con|de)structors use the class name as identifier.
5732 ;; We've always shifted over the identifier as a type and
5733 ;; then backed up again in this case.
5734 identifier-type
5735 (or (memq identifier-type '(found known))
5736 (and (eq (char-after identifier-start) ?~)
5737 ;; `at-type' probably won't be 'found for
5738 ;; destructors since the "~" is then part of the
5739 ;; type name being checked against the list of
5740 ;; known types, so do a check without that
5741 ;; operator.
5742 (or (save-excursion
5743 (goto-char (1+ identifier-start))
5744 (c-forward-syntactic-ws)
5745 (c-with-syntax-table
5746 c-identifier-syntax-table
5747 (looking-at c-known-type-key)))
5748 (save-excursion
5749 (goto-char (1+ identifier-start))
5750 ;; We have already parsed the type earlier,
5751 ;; so it'd be possible to cache the end
5752 ;; position instead of redoing it here, but
5753 ;; then we'd need to keep track of another
5754 ;; position everywhere.
5755 (c-check-type (point)
5756 (progn (c-forward-type)
5757 (point))))))))
5758 (throw 'at-decl-or-cast t))
5759
5760 (if got-identifier
5761 (progn
5762 ;; CASE 16
5763 (when (and got-prefix-before-parens
5764 at-type
5765 (or at-decl-end (looking-at "=[^=]"))
5766 (not context)
5767 (not got-suffix))
5768 ;; Got something like "foo * bar;". Since we're not inside an
5769 ;; arglist it would be a meaningless expression because the
5770 ;; result isn't used. We therefore choose to recognize it as
5771 ;; a declaration. Do not allow a suffix since it could then
5772 ;; be a function call.
5773 (throw 'at-decl-or-cast t))
5774
5775 ;; CASE 17
5776 (when (and (or got-suffix-after-parens
5777 (looking-at "=[^=]"))
5778 (eq at-type 'found)
5779 (not (eq context 'arglist)))
5780 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
5781 ;; be an odd expression or it could be a declaration. Treat
5782 ;; it as a declaration if "a" has been used as a type
5783 ;; somewhere else (if it's a known type we won't get here).
5784 (throw 'at-decl-or-cast t)))
5785
5786 ;; CASE 18
5787 (when (and context
5788 (or got-prefix
5789 (and (eq context 'decl)
5790 (not c-recognize-paren-inits)
5791 (or got-parens got-suffix))))
5792 ;; Got a type followed by an abstract declarator. If `got-prefix'
5793 ;; is set it's something like "a *" without anything after it. If
5794 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
5795 ;; or similar, which we accept only if the context rules out
5796 ;; expressions.
5797 (throw 'at-decl-or-cast t)))
5798
5799 ;; If we had a complete symbol table here (which rules out
5800 ;; `c-found-types') we should return t due to the disambiguation rule
5801 ;; (in at least C++) that anything that can be parsed as a declaration
5802 ;; is a declaration. Now we're being more defensive and prefer to
5803 ;; highlight things like "foo (bar);" as a declaration only if we're
5804 ;; inside an arglist that contains declarations.
5805 (eq context 'decl))))
5806
5807 ;; The point is now after the type decl expression.
5808
5809 (cond
5810 ;; Check for a cast.
5811 ((save-excursion
5812 (and
5813 c-cast-parens
5814
5815 ;; Should be the first type/identifier in a cast paren.
5816 (> preceding-token-end (point-min))
5817 (memq (char-before preceding-token-end) c-cast-parens)
5818
5819 ;; The closing paren should follow.
5820 (progn
5821 (c-forward-syntactic-ws)
5822 (looking-at "\\s\)"))
5823
5824 ;; There should be a primary expression after it.
5825 (let (pos)
5826 (forward-char)
5827 (c-forward-syntactic-ws)
5828 (setq cast-end (point))
5829 (and (looking-at c-primary-expr-regexp)
5830 (progn
5831 (setq pos (match-end 0))
5832 (or
5833 ;; Check if the expression begins with a prefix keyword.
5834 (match-beginning 2)
5835 (if (match-beginning 1)
5836 ;; Expression begins with an ambiguous operator. Treat
5837 ;; it as a cast if it's a type decl or if we've
5838 ;; recognized the type somewhere else.
5839 (or at-decl-or-cast
5840 (memq at-type '(t known found)))
5841 ;; Unless it's a keyword, it's the beginning of a primary
5842 ;; expression.
5843 (not (looking-at c-keywords-regexp)))))
5844 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
5845 ;; that it matched a whole one so that we don't e.g. confuse
5846 ;; the operator '-' with '->'. It's ok if it matches further,
5847 ;; though, since it e.g. can match the float '.5' while the
5848 ;; operator regexp only matches '.'.
5849 (or (not (looking-at c-nonsymbol-token-regexp))
5850 (<= (match-end 0) pos))))
5851
5852 ;; There should either be a cast before it or something that isn't an
5853 ;; identifier or close paren.
5854 (> preceding-token-end (point-min))
5855 (progn
5856 (goto-char (1- preceding-token-end))
5857 (or (eq (point) last-cast-end)
5858 (progn
5859 (c-backward-syntactic-ws)
5860 (if (< (skip-syntax-backward "w_") 0)
5861 ;; It's a symbol. Accept it only if it's one of the
5862 ;; keywords that can precede an expression (without
5863 ;; surrounding parens).
5864 (looking-at c-simple-stmt-key)
5865 (and
5866 ;; Check that it isn't a close paren (block close is ok,
5867 ;; though).
5868 (not (memq (char-before) '(?\) ?\])))
5869 ;; Check that it isn't a nonsymbol identifier.
5870 (not (c-on-identifier)))))))))
5871
5872 ;; Handle the cast.
5873 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5874 (let ((c-promote-possible-types t))
5875 (goto-char type-start)
5876 (c-forward-type)))
5877
5878 (goto-char cast-end)
5879 'cast)
5880
5881 (at-decl-or-cast
5882 ;; We're at a declaration. Highlight the type and the following
5883 ;; declarators.
5884
5885 (when backup-if-not-cast
5886 (c-fdoc-shift-type-backward t))
5887
5888 (when (and (eq context 'decl) (looking-at ","))
5889 ;; Make sure to propagate the `c-decl-arg-start' property to
5890 ;; the next argument if it's set in this one, to cope with
5891 ;; interactive refontification.
5892 (c-put-c-type-property (point) 'c-decl-arg-start))
5893
5894 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5895 (let ((c-promote-possible-types t))
5896 (save-excursion
5897 (goto-char type-start)
5898 (c-forward-type))))
5899
5900 (cons id-start at-type-decl))
5901
5902 (t
5903 ;; False alarm. Restore the recorded ranges.
5904 (setq c-record-type-identifiers save-rec-type-ids
5905 c-record-ref-identifiers save-rec-ref-ids)
5906 nil))))
5907
5908 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
5909 ;; Assuming that point is at the beginning of a token, check if it starts a
5910 ;; label and if so move over it and return non-nil (t in default situations,
5911 ;; specific symbols (see below) for interesting situations), otherwise don't
5912 ;; move and return nil. "Label" here means "most things with a colon".
5913 ;;
5914 ;; More precisely, a "label" is regarded as one of:
5915 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
5916 ;; (ii) A case label - either the entire construct "case FOO:", or just the
5917 ;; bare "case", should the colon be missing. We return t;
5918 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
5919 ;; return t;
5920 ;; (iv) One of QT's "extended" C++ variants of
5921 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
5922 ;; Returns the symbol `qt-2kwds-colon'.
5923 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
5924 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
5925 ;; colon). Currently (2006-03), this applies only to Objective C's
5926 ;; keywords "@private", "@protected", and "@public". Returns t.
5927 ;;
5928 ;; One of the things which will NOT be recognised as a label is a bit-field
5929 ;; element of a struct, something like "int foo:5".
5930 ;;
5931 ;; The end of the label is taken to be just after the colon, or the end of
5932 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
5933 ;; after the end on return. The terminating char gets marked with
5934 ;; `c-decl-end' to improve recognition of the following declaration or
5935 ;; statement.
5936 ;;
5937 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
5938 ;; label, if any, has already been marked up like that.
5939 ;;
5940 ;; If PRECEDING-TOKEN-END is given, it should be the first position
5941 ;; after the preceding token, i.e. on the other side of the
5942 ;; syntactic ws from the point. Use a value less than or equal to
5943 ;; (point-min) if the point is at the first token in (the visible
5944 ;; part of) the buffer.
5945 ;;
5946 ;; The optional LIMIT limits the forward scan for the colon.
5947 ;;
5948 ;; This function records the ranges of the label symbols on
5949 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
5950 ;; non-nil.
5951 ;;
5952 ;; This function might do hidden buffer changes.
5953
5954 (let ((start (point))
5955 label-end
5956 qt-symbol-idx
5957 macro-start ; if we're in one.
5958 label-type
5959 kwd)
5960 (cond
5961 ;; "case" or "default" (Doesn't apply to AWK).
5962 ((looking-at c-label-kwds-regexp)
5963 (let ((kwd-end (match-end 1)))
5964 ;; Record only the keyword itself for fontification, since in
5965 ;; case labels the following is a constant expression and not
5966 ;; a label.
5967 (when c-record-type-identifiers
5968 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
5969
5970 ;; Find the label end.
5971 (goto-char kwd-end)
5972 (setq label-type
5973 (if (and (c-syntactic-re-search-forward
5974 ;; Stop on chars that aren't allowed in expressions,
5975 ;; and on operator chars that would be meaningless
5976 ;; there. FIXME: This doesn't cope with ?: operators.
5977 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
5978 limit t t nil 1)
5979 (match-beginning 2))
5980
5981 (progn ; there's a proper :
5982 (goto-char (match-beginning 2)) ; just after the :
5983 (c-put-c-type-property (1- (point)) 'c-decl-end)
5984 t)
5985
5986 ;; It's an unfinished label. We consider the keyword enough
5987 ;; to recognize it as a label, so that it gets fontified.
5988 ;; Leave the point at the end of it, but don't put any
5989 ;; `c-decl-end' marker.
5990 (goto-char kwd-end)
5991 t))))
5992
5993 ;; @private, @protected, @public, in Objective C, or similar.
5994 ((and c-opt-extra-label-key
5995 (looking-at c-opt-extra-label-key))
5996 ;; For a `c-opt-extra-label-key' match, we record the whole
5997 ;; thing for fontification. That's to get the leading '@' in
5998 ;; Objective-C protection labels fontified.
5999 (goto-char (match-end 1))
6000 (when c-record-type-identifiers
6001 (c-record-ref-id (cons (match-beginning 1) (point))))
6002 (c-put-c-type-property (1- (point)) 'c-decl-end)
6003 (setq label-type t))
6004
6005 ;; All other cases of labels.
6006 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
6007
6008 ;; A colon label must have something before the colon.
6009 (not (eq (char-after) ?:))
6010
6011 ;; Check that we're not after a token that can't precede a label.
6012 (or
6013 ;; Trivially succeeds when there's no preceding token.
6014 (if preceding-token-end
6015 (<= preceding-token-end (point-min))
6016 (save-excursion
6017 (c-backward-syntactic-ws)
6018 (setq preceding-token-end (point))
6019 (bobp)))
6020
6021 ;; Check if we're after a label, if we're after a closing
6022 ;; paren that belong to statement, and with
6023 ;; `c-label-prefix-re'. It's done in different order
6024 ;; depending on `assume-markup' since the checks have
6025 ;; different expensiveness.
6026 (if assume-markup
6027 (or
6028 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
6029 'c-decl-end)
6030
6031 (save-excursion
6032 (goto-char (1- preceding-token-end))
6033 (c-beginning-of-current-token)
6034 (or (looking-at c-label-prefix-re)
6035 (looking-at c-block-stmt-1-key)))
6036
6037 (and (eq (char-before preceding-token-end) ?\))
6038 (c-after-conditional)))
6039
6040 (or
6041 (save-excursion
6042 (goto-char (1- preceding-token-end))
6043 (c-beginning-of-current-token)
6044 (or (looking-at c-label-prefix-re)
6045 (looking-at c-block-stmt-1-key)))
6046
6047 (cond
6048 ((eq (char-before preceding-token-end) ?\))
6049 (c-after-conditional))
6050
6051 ((eq (char-before preceding-token-end) ?:)
6052 ;; Might be after another label, so check it recursively.
6053 (save-restriction
6054 (save-excursion
6055 (goto-char (1- preceding-token-end))
6056 ;; Essentially the same as the
6057 ;; `c-syntactic-re-search-forward' regexp below.
6058 (setq macro-start
6059 (save-excursion (and (c-beginning-of-macro)
6060 (point))))
6061 (if macro-start (narrow-to-region macro-start (point-max)))
6062 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
6063 ;; Note: the following should work instead of the
6064 ;; narrow-to-region above. Investigate why not,
6065 ;; sometime. ACM, 2006-03-31.
6066 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
6067 ;; macro-start t)
6068 (let ((pte (point))
6069 ;; If the caller turned on recording for us,
6070 ;; it shouldn't apply when we check the
6071 ;; preceding label.
6072 c-record-type-identifiers)
6073 ;; A label can't start at a cpp directive. Check for
6074 ;; this, since c-forward-syntactic-ws would foul up on it.
6075 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
6076 (c-forward-syntactic-ws)
6077 (c-forward-label nil pte start))))))))))
6078
6079 ;; Point is still at the beginning of the possible label construct.
6080 ;;
6081 ;; Check that the next nonsymbol token is ":", or that we're in one
6082 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
6083 ;; arguments. FIXME: Should build this regexp from the language
6084 ;; constants.
6085 (cond
6086 ;; public: protected: private:
6087 ((and
6088 (c-major-mode-is 'c++-mode)
6089 (search-forward-regexp
6090 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
6091 (progn (backward-char)
6092 (c-forward-syntactic-ws limit)
6093 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
6094 (forward-char)
6095 (setq label-type t))
6096 ;; QT double keyword like "protected slots:" or goto target.
6097 ((progn (goto-char start) nil))
6098 ((when (c-syntactic-re-search-forward
6099 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
6100 (backward-char)
6101 (setq label-end (point))
6102 (setq qt-symbol-idx
6103 (and (c-major-mode-is 'c++-mode)
6104 (string-match
6105 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
6106 (buffer-substring start (point)))))
6107 (c-forward-syntactic-ws limit)
6108 (cond
6109 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
6110 (forward-char)
6111 (setq label-type
6112 (if (or (string= "signals" ; Special QT macro
6113 (setq kwd (buffer-substring-no-properties start label-end)))
6114 (string= "Q_SIGNALS" kwd))
6115 'qt-1kwd-colon
6116 'goto-target)))
6117 ((and qt-symbol-idx
6118 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
6119 (progn (c-forward-syntactic-ws limit)
6120 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
6121 (forward-char)
6122 (setq label-type 'qt-2kwds-colon)))))))
6123
6124 (save-restriction
6125 (narrow-to-region start (point))
6126
6127 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
6128 (catch 'check-label
6129 (goto-char start)
6130 (while (progn
6131 (when (looking-at c-nonlabel-token-key)
6132 (goto-char start)
6133 (setq label-type nil)
6134 (throw 'check-label nil))
6135 (and (c-safe (c-forward-sexp)
6136 (c-forward-syntactic-ws)
6137 t)
6138 (not (eobp)))))
6139
6140 ;; Record the identifiers in the label for fontification, unless
6141 ;; it begins with `c-label-kwds' in which case the following
6142 ;; identifiers are part of a (constant) expression that
6143 ;; shouldn't be fontified.
6144 (when (and c-record-type-identifiers
6145 (progn (goto-char start)
6146 (not (looking-at c-label-kwds-regexp))))
6147 (while (c-syntactic-re-search-forward c-symbol-key nil t)
6148 (c-record-ref-id (cons (match-beginning 0)
6149 (match-end 0)))))
6150
6151 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
6152 (goto-char (point-max)))))
6153
6154 (t
6155 ;; Not a label.
6156 (goto-char start)))
6157 label-type))
6158
6159 (defun c-forward-objc-directive ()
6160 ;; Assuming the point is at the beginning of a token, try to move
6161 ;; forward to the end of the Objective-C directive that starts
6162 ;; there. Return t if a directive was fully recognized, otherwise
6163 ;; the point is moved as far as one could be successfully parsed and
6164 ;; nil is returned.
6165 ;;
6166 ;; This function records identifier ranges on
6167 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6168 ;; `c-record-type-identifiers' is non-nil.
6169 ;;
6170 ;; This function might do hidden buffer changes.
6171
6172 (let ((start (point))
6173 start-char
6174 (c-promote-possible-types t)
6175 ;; Turn off recognition of angle bracket arglists while parsing
6176 ;; types here since the protocol reference list might then be
6177 ;; considered part of the preceding name or superclass-name.
6178 c-recognize-<>-arglists)
6179
6180 (if (or
6181 (when (looking-at
6182 (eval-when-compile
6183 (c-make-keywords-re t
6184 (append (c-lang-const c-protection-kwds objc)
6185 '("@end"))
6186 'objc-mode)))
6187 (goto-char (match-end 1))
6188 t)
6189
6190 (and
6191 (looking-at
6192 (eval-when-compile
6193 (c-make-keywords-re t
6194 '("@interface" "@implementation" "@protocol")
6195 'objc-mode)))
6196
6197 ;; Handle the name of the class itself.
6198 (progn
6199 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
6200 ; at EOB.
6201 (goto-char (match-end 0))
6202 (c-skip-ws-forward)
6203 (c-forward-type))
6204
6205 (catch 'break
6206 ;; Look for ": superclass-name" or "( category-name )".
6207 (when (looking-at "[:\(]")
6208 (setq start-char (char-after))
6209 (forward-char)
6210 (c-forward-syntactic-ws)
6211 (unless (c-forward-type) (throw 'break nil))
6212 (when (eq start-char ?\()
6213 (unless (eq (char-after) ?\)) (throw 'break nil))
6214 (forward-char)
6215 (c-forward-syntactic-ws)))
6216
6217 ;; Look for a protocol reference list.
6218 (if (eq (char-after) ?<)
6219 (let ((c-recognize-<>-arglists t)
6220 (c-parse-and-markup-<>-arglists t)
6221 c-restricted-<>-arglists)
6222 (c-forward-<>-arglist t))
6223 t))))
6224
6225 (progn
6226 (c-backward-syntactic-ws)
6227 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
6228 (c-put-c-type-property (1- (point)) 'c-decl-end)
6229 t)
6230
6231 (c-clear-c-type-property start (point) 'c-decl-end)
6232 nil)))
6233
6234 (defun c-beginning-of-inheritance-list (&optional lim)
6235 ;; Go to the first non-whitespace after the colon that starts a
6236 ;; multiple inheritance introduction. Optional LIM is the farthest
6237 ;; back we should search.
6238 ;;
6239 ;; This function might do hidden buffer changes.
6240 (c-with-syntax-table c++-template-syntax-table
6241 (c-backward-token-2 0 t lim)
6242 (while (and (or (looking-at c-symbol-start)
6243 (looking-at "[<,]\\|::"))
6244 (zerop (c-backward-token-2 1 t lim))))))
6245
6246 (defun c-in-method-def-p ()
6247 ;; Return nil if we aren't in a method definition, otherwise the
6248 ;; position of the initial [+-].
6249 ;;
6250 ;; This function might do hidden buffer changes.
6251 (save-excursion
6252 (beginning-of-line)
6253 (and c-opt-method-key
6254 (looking-at c-opt-method-key)
6255 (point))
6256 ))
6257
6258 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
6259 (defun c-in-gcc-asm-p ()
6260 ;; Return non-nil if point is within a gcc \"asm\" block.
6261 ;;
6262 ;; This should be called with point inside an argument list.
6263 ;;
6264 ;; Only one level of enclosing parentheses is considered, so for
6265 ;; instance `nil' is returned when in a function call within an asm
6266 ;; operand.
6267 ;;
6268 ;; This function might do hidden buffer changes.
6269
6270 (and c-opt-asm-stmt-key
6271 (save-excursion
6272 (beginning-of-line)
6273 (backward-up-list 1)
6274 (c-beginning-of-statement-1 (point-min) nil t)
6275 (looking-at c-opt-asm-stmt-key))))
6276
6277 (defun c-at-toplevel-p ()
6278 "Return a determination as to whether point is \"at the top level\".
6279 Informally, \"at the top level\" is anywhere where you can write
6280 a function.
6281
6282 More precisely, being at the top-level means that point is either
6283 outside any enclosing block (such as a function definition), or
6284 directly inside a class, namespace or other block that contains
6285 another declaration level.
6286
6287 If point is not at the top-level (e.g. it is inside a method
6288 definition), then nil is returned. Otherwise, if point is at a
6289 top-level not enclosed within a class definition, t is returned.
6290 Otherwise, a 2-vector is returned where the zeroth element is the
6291 buffer position of the start of the class declaration, and the first
6292 element is the buffer position of the enclosing class's opening
6293 brace.
6294
6295 Note that this function might do hidden buffer changes. See the
6296 comment at the start of cc-engine.el for more info."
6297 (let ((paren-state (c-parse-state)))
6298 (or (not (c-most-enclosing-brace paren-state))
6299 (c-search-uplist-for-classkey paren-state))))
6300
6301 (defun c-just-after-func-arglist-p (&optional lim)
6302 ;; Return non-nil if the point is in the region after the argument
6303 ;; list of a function and its opening brace (or semicolon in case it
6304 ;; got no body). If there are K&R style argument declarations in
6305 ;; that region, the point has to be inside the first one for this
6306 ;; function to recognize it.
6307 ;;
6308 ;; If successful, the point is moved to the first token after the
6309 ;; function header (see `c-forward-decl-or-cast-1' for details) and
6310 ;; the position of the opening paren of the function arglist is
6311 ;; returned.
6312 ;;
6313 ;; The point is clobbered if not successful.
6314 ;;
6315 ;; LIM is used as bound for backward buffer searches.
6316 ;;
6317 ;; This function might do hidden buffer changes.
6318
6319 (let ((beg (point)) end id-start)
6320 (and
6321 (eq (c-beginning-of-statement-1 lim) 'same)
6322
6323 (not (or (c-major-mode-is 'objc-mode)
6324 (c-forward-objc-directive)))
6325
6326 (setq id-start
6327 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
6328 (< id-start beg)
6329
6330 ;; There should not be a '=' or ',' between beg and the
6331 ;; start of the declaration since that means we were in the
6332 ;; "expression part" of the declaration.
6333 (or (> (point) beg)
6334 (not (looking-at "[=,]")))
6335
6336 (save-excursion
6337 ;; Check that there's an arglist paren in the
6338 ;; declaration.
6339 (goto-char id-start)
6340 (cond ((eq (char-after) ?\()
6341 ;; The declarator is a paren expression, so skip past it
6342 ;; so that we don't get stuck on that instead of the
6343 ;; function arglist.
6344 (c-forward-sexp))
6345 ((and c-opt-op-identifier-prefix
6346 (looking-at c-opt-op-identifier-prefix))
6347 ;; Don't trip up on "operator ()".
6348 (c-forward-token-2 2 t)))
6349 (and (< (point) beg)
6350 (c-syntactic-re-search-forward "(" beg t t)
6351 (1- (point)))))))
6352
6353 (defun c-in-knr-argdecl (&optional lim)
6354 ;; Return the position of the first argument declaration if point is
6355 ;; inside a K&R style argument declaration list, nil otherwise.
6356 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
6357 ;; position that bounds the backward search for the argument list.
6358 ;;
6359 ;; Point must be within a possible K&R region, e.g. just before a top-level
6360 ;; "{". It must be outside of parens and brackets. The test can return
6361 ;; false positives otherwise.
6362 ;;
6363 ;; This function might do hidden buffer changes.
6364
6365 (save-excursion
6366 (save-restriction
6367 ;; If we're in a macro, our search range is restricted to it. Narrow to
6368 ;; the searchable range.
6369 (let* ((macro-start (c-query-macro-start))
6370 (lim (max (or lim (point-min)) (or macro-start (point-min))))
6371 before-lparen after-rparen
6372 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
6373 (narrow-to-region lim (c-point 'eol))
6374
6375 ;; Search backwards for the defun's argument list. We give up if we
6376 ;; encounter a "}" (end of a previous defun) or BOB.
6377 ;;
6378 ;; The criterion for a paren structure being the arg list is:
6379 ;; o - there is non-WS stuff after it but before any "{"; AND
6380 ;; o - the token after it isn't a ";" AND
6381 ;; o - it is preceded by either an identifier (the function name) or
6382 ;; a macro expansion like "DEFUN (...)"; AND
6383 ;; o - its content is a non-empty comma-separated list of identifiers
6384 ;; (an empty arg list won't have a knr region).
6385 ;;
6386 ;; The following snippet illustrates these rules:
6387 ;; int foo (bar, baz, yuk)
6388 ;; int bar [] ;
6389 ;; int (*baz) (my_type) ;
6390 ;; int (*) (void) (*yuk) (void) ;
6391 ;; {
6392
6393 (catch 'knr
6394 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
6395 (setq pp-count-out (1- pp-count-out))
6396 (c-syntactic-skip-backward "^)]}")
6397 (cond ((eq (char-before) ?\))
6398 (setq after-rparen (point)))
6399 ((eq (char-before) ?\])
6400 (setq after-rparen nil))
6401 (t ; either } (hit previous defun) or no more parens/brackets
6402 (throw 'knr nil)))
6403
6404 (if after-rparen
6405 ;; We're inside a paren. Could it be our argument list....?
6406 (if
6407 (and
6408 (progn
6409 (goto-char after-rparen)
6410 (unless (c-go-list-backward) (throw 'knr nil)) ;
6411 ;; FIXME!!! What about macros between the parens? 2007/01/20
6412 (setq before-lparen (point)))
6413
6414 ;; It can't be the arg list if next token is ; or {
6415 (progn (goto-char after-rparen)
6416 (c-forward-syntactic-ws)
6417 (not (memq (char-after) '(?\; ?\{))))
6418
6419 ;; Is the thing preceding the list an identifier (the
6420 ;; function name), or a macro expansion?
6421 (progn
6422 (goto-char before-lparen)
6423 (eq (c-backward-token-2) 0)
6424 (or (c-on-identifier)
6425 (and (eq (char-after) ?\))
6426 (c-go-up-list-backward)
6427 (eq (c-backward-token-2) 0)
6428 (c-on-identifier))))
6429
6430 ;; Have we got a non-empty list of comma-separated
6431 ;; identifiers?
6432 (progn
6433 (goto-char before-lparen)
6434 (c-forward-token-2) ; to first token inside parens
6435 (and
6436 (c-on-identifier)
6437 (c-forward-token-2)
6438 (catch 'id-list
6439 (while (eq (char-after) ?\,)
6440 (c-forward-token-2)
6441 (unless (c-on-identifier) (throw 'id-list nil))
6442 (c-forward-token-2))
6443 (eq (char-after) ?\))))))
6444
6445 ;; ...Yes. We've identified the function's argument list.
6446 (throw 'knr
6447 (progn (goto-char after-rparen)
6448 (c-forward-syntactic-ws)
6449 (point)))
6450
6451 ;; ...No. The current parens aren't the function's arg list.
6452 (goto-char before-lparen))
6453
6454 (or (c-go-list-backward) ; backwards over [ .... ]
6455 (throw 'knr nil)))))))))
6456
6457 (defun c-skip-conditional ()
6458 ;; skip forward over conditional at point, including any predicate
6459 ;; statements in parentheses. No error checking is performed.
6460 ;;
6461 ;; This function might do hidden buffer changes.
6462 (c-forward-sexp (cond
6463 ;; else if()
6464 ((looking-at (concat "\\<else"
6465 "\\([ \t\n]\\|\\\\\n\\)+"
6466 "if\\>\\([^_]\\|$\\)"))
6467 3)
6468 ;; do, else, try, finally
6469 ((looking-at (concat "\\<\\("
6470 "do\\|else\\|try\\|finally"
6471 "\\)\\>\\([^_]\\|$\\)"))
6472 1)
6473 ;; for, if, while, switch, catch, synchronized, foreach
6474 (t 2))))
6475
6476 (defun c-after-conditional (&optional lim)
6477 ;; If looking at the token after a conditional then return the
6478 ;; position of its start, otherwise return nil.
6479 ;;
6480 ;; This function might do hidden buffer changes.
6481 (save-excursion
6482 (and (zerop (c-backward-token-2 1 t lim))
6483 (or (looking-at c-block-stmt-1-key)
6484 (and (eq (char-after) ?\()
6485 (zerop (c-backward-token-2 1 t lim))
6486 (looking-at c-block-stmt-2-key)))
6487 (point))))
6488
6489 (defun c-after-special-operator-id (&optional lim)
6490 ;; If the point is after an operator identifier that isn't handled
6491 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
6492 ;; position of the start of that identifier is returned. nil is
6493 ;; returned otherwise. The point may be anywhere in the syntactic
6494 ;; whitespace after the last token of the operator identifier.
6495 ;;
6496 ;; This function might do hidden buffer changes.
6497 (save-excursion
6498 (and c-overloadable-operators-regexp
6499 (zerop (c-backward-token-2 1 nil lim))
6500 (looking-at c-overloadable-operators-regexp)
6501 (or (not c-opt-op-identifier-prefix)
6502 (and
6503 (zerop (c-backward-token-2 1 nil lim))
6504 (looking-at c-opt-op-identifier-prefix)))
6505 (point))))
6506
6507 (defsubst c-backward-to-block-anchor (&optional lim)
6508 ;; Assuming point is at a brace that opens a statement block of some
6509 ;; kind, move to the proper anchor point for that block. It might
6510 ;; need to be adjusted further by c-add-stmt-syntax, but the
6511 ;; position at return is suitable as start position for that
6512 ;; function.
6513 ;;
6514 ;; This function might do hidden buffer changes.
6515 (unless (= (point) (c-point 'boi))
6516 (let ((start (c-after-conditional lim)))
6517 (if start
6518 (goto-char start)))))
6519
6520 (defsubst c-backward-to-decl-anchor (&optional lim)
6521 ;; Assuming point is at a brace that opens the block of a top level
6522 ;; declaration of some kind, move to the proper anchor point for
6523 ;; that block.
6524 ;;
6525 ;; This function might do hidden buffer changes.
6526 (unless (= (point) (c-point 'boi))
6527 (c-beginning-of-statement-1 lim)))
6528
6529 (defun c-search-decl-header-end ()
6530 ;; Search forward for the end of the "header" of the current
6531 ;; declaration. That's the position where the definition body
6532 ;; starts, or the first variable initializer, or the ending
6533 ;; semicolon. I.e. search forward for the closest following
6534 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
6535 ;; _after_ the first found token, or at point-max if none is found.
6536 ;;
6537 ;; This function might do hidden buffer changes.
6538
6539 (let ((base (point)))
6540 (if (c-major-mode-is 'c++-mode)
6541
6542 ;; In C++ we need to take special care to handle operator
6543 ;; tokens and those pesky template brackets.
6544 (while (and
6545 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
6546 (or
6547 (c-end-of-current-token base)
6548 ;; Handle operator identifiers, i.e. ignore any
6549 ;; operator token preceded by "operator".
6550 (save-excursion
6551 (and (c-safe (c-backward-sexp) t)
6552 (looking-at c-opt-op-identifier-prefix)))
6553 (and (eq (char-before) ?<)
6554 (c-with-syntax-table c++-template-syntax-table
6555 (if (c-safe (goto-char (c-up-list-forward (point))))
6556 t
6557 (goto-char (point-max))
6558 nil)))))
6559 (setq base (point)))
6560
6561 (while (and
6562 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
6563 (c-end-of-current-token base))
6564 (setq base (point))))))
6565
6566 (defun c-beginning-of-decl-1 (&optional lim)
6567 ;; Go to the beginning of the current declaration, or the beginning
6568 ;; of the previous one if already at the start of it. Point won't
6569 ;; be moved out of any surrounding paren. Return a cons cell of the
6570 ;; form (MOVE . KNR-POS). MOVE is like the return value from
6571 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
6572 ;; style argument declarations (and they are to be recognized) then
6573 ;; KNR-POS is set to the start of the first such argument
6574 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
6575 ;; position that bounds the backward search.
6576 ;;
6577 ;; NB: Cases where the declaration continues after the block, as in
6578 ;; "struct foo { ... } bar;", are currently recognized as two
6579 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
6580 ;;
6581 ;; This function might do hidden buffer changes.
6582 (catch 'return
6583 (let* ((start (point))
6584 (last-stmt-start (point))
6585 (move (c-beginning-of-statement-1 lim nil t)))
6586
6587 ;; `c-beginning-of-statement-1' stops at a block start, but we
6588 ;; want to continue if the block doesn't begin a top level
6589 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
6590 ;; or an open paren.
6591 (let ((beg (point)) tentative-move)
6592 ;; Go back one "statement" each time round the loop until we're just
6593 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
6594 ;; an ObjC method. This will move over a multiple declaration whose
6595 ;; components are comma separated.
6596 (while (and
6597 ;; Must check with c-opt-method-key in ObjC mode.
6598 (not (and c-opt-method-key
6599 (looking-at c-opt-method-key)))
6600 (/= last-stmt-start (point))
6601 (progn
6602 (c-backward-syntactic-ws lim)
6603 (not (memq (char-before) '(?\; ?} ?: nil))))
6604 (save-excursion
6605 (backward-char)
6606 (not (looking-at "\\s(")))
6607 ;; Check that we don't move from the first thing in a
6608 ;; macro to its header.
6609 (not (eq (setq tentative-move
6610 (c-beginning-of-statement-1 lim nil t))
6611 'macro)))
6612 (setq last-stmt-start beg
6613 beg (point)
6614 move tentative-move))
6615 (goto-char beg))
6616
6617 (when c-recognize-knr-p
6618 (let ((fallback-pos (point)) knr-argdecl-start)
6619 ;; Handle K&R argdecls. Back up after the "statement" jumped
6620 ;; over by `c-beginning-of-statement-1', unless it was the
6621 ;; function body, in which case we're sitting on the opening
6622 ;; brace now. Then test if we're in a K&R argdecl region and
6623 ;; that we started at the other side of the first argdecl in
6624 ;; it.
6625 (unless (eq (char-after) ?{)
6626 (goto-char last-stmt-start))
6627 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
6628 (< knr-argdecl-start start)
6629 (progn
6630 (goto-char knr-argdecl-start)
6631 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
6632 (throw 'return
6633 (cons (if (eq (char-after fallback-pos) ?{)
6634 'previous
6635 'same)
6636 knr-argdecl-start))
6637 (goto-char fallback-pos))))
6638
6639 ;; `c-beginning-of-statement-1' counts each brace block as a separate
6640 ;; statement, so the result will be 'previous if we've moved over any.
6641 ;; So change our result back to 'same if necessary.
6642 ;;
6643 ;; If they were brace list initializers we might not have moved over a
6644 ;; declaration boundary though, so change it to 'same if we've moved
6645 ;; past a '=' before '{', but not ';'. (This ought to be integrated
6646 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
6647 ;; potentially can search over a large amount of text.). Take special
6648 ;; pains not to get mislead by C++'s "operator=", and the like.
6649 (if (and (eq move 'previous)
6650 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
6651 c++-template-syntax-table
6652 (syntax-table))
6653 (save-excursion
6654 (and
6655 (progn
6656 (while ; keep going back to "[;={"s until we either find
6657 ; no more, or get to one which isn't an "operator ="
6658 (and (c-syntactic-re-search-forward "[;={]" start t t t)
6659 (eq (char-before) ?=)
6660 c-overloadable-operators-regexp
6661 c-opt-op-identifier-prefix
6662 (save-excursion
6663 (eq (c-backward-token-2) 0)
6664 (looking-at c-overloadable-operators-regexp)
6665 (eq (c-backward-token-2) 0)
6666 (looking-at c-opt-op-identifier-prefix))))
6667 (eq (char-before) ?=))
6668 (c-syntactic-re-search-forward "[;{]" start t t)
6669 (eq (char-before) ?{)
6670 (c-safe (goto-char (c-up-list-forward (point))) t)
6671 (not (c-syntactic-re-search-forward ";" start t t))))))
6672 (cons 'same nil)
6673 (cons move nil)))))
6674
6675 (defun c-end-of-decl-1 ()
6676 ;; Assuming point is at the start of a declaration (as detected by
6677 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
6678 ;; `c-beginning-of-decl-1', this function handles the case when a
6679 ;; block is followed by identifiers in e.g. struct declarations in C
6680 ;; or C++. If a proper end was found then t is returned, otherwise
6681 ;; point is moved as far as possible within the current sexp and nil
6682 ;; is returned. This function doesn't handle macros; use
6683 ;; `c-end-of-macro' instead in those cases.
6684 ;;
6685 ;; This function might do hidden buffer changes.
6686 (let ((start (point))
6687 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
6688 c++-template-syntax-table
6689 (syntax-table))))
6690 (catch 'return
6691 (c-search-decl-header-end)
6692
6693 (when (and c-recognize-knr-p
6694 (eq (char-before) ?\;)
6695 (c-in-knr-argdecl start))
6696 ;; Stopped at the ';' in a K&R argdecl section which is
6697 ;; detected using the same criteria as in
6698 ;; `c-beginning-of-decl-1'. Move to the following block
6699 ;; start.
6700 (c-syntactic-re-search-forward "{" nil 'move t))
6701
6702 (when (eq (char-before) ?{)
6703 ;; Encountered a block in the declaration. Jump over it.
6704 (condition-case nil
6705 (goto-char (c-up-list-forward (point)))
6706 (error (goto-char (point-max))
6707 (throw 'return nil)))
6708 (if (or (not c-opt-block-decls-with-vars-key)
6709 (save-excursion
6710 (c-with-syntax-table decl-syntax-table
6711 (let ((lim (point)))
6712 (goto-char start)
6713 (not (and
6714 ;; Check for `c-opt-block-decls-with-vars-key'
6715 ;; before the first paren.
6716 (c-syntactic-re-search-forward
6717 (concat "[;=\(\[{]\\|\\("
6718 c-opt-block-decls-with-vars-key
6719 "\\)")
6720 lim t t t)
6721 (match-beginning 1)
6722 (not (eq (char-before) ?_))
6723 ;; Check that the first following paren is
6724 ;; the block.
6725 (c-syntactic-re-search-forward "[;=\(\[{]"
6726 lim t t t)
6727 (eq (char-before) ?{)))))))
6728 ;; The declaration doesn't have any of the
6729 ;; `c-opt-block-decls-with-vars' keywords in the
6730 ;; beginning, so it ends here at the end of the block.
6731 (throw 'return t)))
6732
6733 (c-with-syntax-table decl-syntax-table
6734 (while (progn
6735 (if (eq (char-before) ?\;)
6736 (throw 'return t))
6737 (c-syntactic-re-search-forward ";" nil 'move t))))
6738 nil)))
6739
6740 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
6741 ;; Assuming the point is at an open brace, check if it starts a
6742 ;; block that contains another declaration level, i.e. that isn't a
6743 ;; statement block or a brace list, and if so return non-nil.
6744 ;;
6745 ;; If the check is successful, the return value is the start of the
6746 ;; keyword that tells what kind of construct it is, i.e. typically
6747 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
6748 ;; the point will be at the start of the construct, before any
6749 ;; leading specifiers, otherwise it's at the returned position.
6750 ;;
6751 ;; The point is clobbered if the check is unsuccessful.
6752 ;;
6753 ;; CONTAINING-SEXP is the position of the open of the surrounding
6754 ;; paren, or nil if none.
6755 ;;
6756 ;; The optional LIMIT limits the backward search for the start of
6757 ;; the construct. It's assumed to be at a syntactically relevant
6758 ;; position.
6759 ;;
6760 ;; If any template arglists are found in the searched region before
6761 ;; the open brace, they get marked with paren syntax.
6762 ;;
6763 ;; This function might do hidden buffer changes.
6764
6765 (let ((open-brace (point)) kwd-start first-specifier-pos)
6766 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6767
6768 (when (and c-recognize-<>-arglists
6769 (eq (char-before) ?>))
6770 ;; Could be at the end of a template arglist.
6771 (let ((c-parse-and-markup-<>-arglists t)
6772 (c-disallow-comma-in-<>-arglists
6773 (and containing-sexp
6774 (not (eq (char-after containing-sexp) ?{)))))
6775 (while (and
6776 (c-backward-<>-arglist nil limit)
6777 (progn
6778 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6779 (eq (char-before) ?>))))))
6780
6781 ;; Note: Can't get bogus hits inside template arglists below since they
6782 ;; have gotten paren syntax above.
6783 (when (and
6784 ;; If `goto-start' is set we begin by searching for the
6785 ;; first possible position of a leading specifier list.
6786 ;; The `c-decl-block-key' search continues from there since
6787 ;; we know it can't match earlier.
6788 (if goto-start
6789 (when (c-syntactic-re-search-forward c-symbol-start
6790 open-brace t t)
6791 (goto-char (setq first-specifier-pos (match-beginning 0)))
6792 t)
6793 t)
6794
6795 (cond
6796 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
6797 (goto-char (setq kwd-start (match-beginning 0)))
6798 (or
6799
6800 ;; Found a keyword that can't be a type?
6801 (match-beginning 1)
6802
6803 ;; Can be a type too, in which case it's the return type of a
6804 ;; function (under the assumption that no declaration level
6805 ;; block construct starts with a type).
6806 (not (c-forward-type))
6807
6808 ;; Jumped over a type, but it could be a declaration keyword
6809 ;; followed by the declared identifier that we've jumped over
6810 ;; instead (e.g. in "class Foo {"). If it indeed is a type
6811 ;; then we should be at the declarator now, so check for a
6812 ;; valid declarator start.
6813 ;;
6814 ;; Note: This doesn't cope with the case when a declared
6815 ;; identifier is followed by e.g. '(' in a language where '('
6816 ;; also might be part of a declarator expression. Currently
6817 ;; there's no such language.
6818 (not (or (looking-at c-symbol-start)
6819 (looking-at c-type-decl-prefix-key)))))
6820
6821 ;; In Pike a list of modifiers may be followed by a brace
6822 ;; to make them apply to many identifiers. Note that the
6823 ;; match data will be empty on return in this case.
6824 ((and (c-major-mode-is 'pike-mode)
6825 (progn
6826 (goto-char open-brace)
6827 (= (c-backward-token-2) 0))
6828 (looking-at c-specifier-key)
6829 ;; Use this variant to avoid yet another special regexp.
6830 (c-keyword-member (c-keyword-sym (match-string 1))
6831 'c-modifier-kwds))
6832 (setq kwd-start (point))
6833 t)))
6834
6835 ;; Got a match.
6836
6837 (if goto-start
6838 ;; Back up over any preceding specifiers and their clauses
6839 ;; by going forward from `first-specifier-pos', which is the
6840 ;; earliest possible position where the specifier list can
6841 ;; start.
6842 (progn
6843 (goto-char first-specifier-pos)
6844
6845 (while (< (point) kwd-start)
6846 (if (looking-at c-symbol-key)
6847 ;; Accept any plain symbol token on the ground that
6848 ;; it's a specifier masked through a macro (just
6849 ;; like `c-forward-decl-or-cast-1' skip forward over
6850 ;; such tokens).
6851 ;;
6852 ;; Could be more restrictive wrt invalid keywords,
6853 ;; but that'd only occur in invalid code so there's
6854 ;; no use spending effort on it.
6855 (let ((end (match-end 0)))
6856 (unless (c-forward-keyword-clause 0)
6857 (goto-char end)
6858 (c-forward-syntactic-ws)))
6859
6860 ;; Can't parse a declaration preamble and is still
6861 ;; before `kwd-start'. That means `first-specifier-pos'
6862 ;; was in some earlier construct. Search again.
6863 (if (c-syntactic-re-search-forward c-symbol-start
6864 kwd-start 'move t)
6865 (goto-char (setq first-specifier-pos (match-beginning 0)))
6866 ;; Got no preamble before the block declaration keyword.
6867 (setq first-specifier-pos kwd-start))))
6868
6869 (goto-char first-specifier-pos))
6870 (goto-char kwd-start))
6871
6872 kwd-start)))
6873
6874 (defun c-search-uplist-for-classkey (paren-state)
6875 ;; Check if the closest containing paren sexp is a declaration
6876 ;; block, returning a 2 element vector in that case. Aref 0
6877 ;; contains the bufpos at boi of the class key line, and aref 1
6878 ;; contains the bufpos of the open brace. This function is an
6879 ;; obsolete wrapper for `c-looking-at-decl-block'.
6880 ;;
6881 ;; This function might do hidden buffer changes.
6882 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
6883 (when open-paren-pos
6884 (save-excursion
6885 (goto-char open-paren-pos)
6886 (when (and (eq (char-after) ?{)
6887 (c-looking-at-decl-block
6888 (c-safe-position open-paren-pos paren-state)
6889 nil))
6890 (back-to-indentation)
6891 (vector (point) open-paren-pos))))))
6892
6893 (defun c-inside-bracelist-p (containing-sexp paren-state)
6894 ;; return the buffer position of the beginning of the brace list
6895 ;; statement if we're inside a brace list, otherwise return nil.
6896 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
6897 ;; paren. PAREN-STATE is the remainder of the state of enclosing
6898 ;; braces
6899 ;;
6900 ;; N.B.: This algorithm can potentially get confused by cpp macros
6901 ;; placed in inconvenient locations. It's a trade-off we make for
6902 ;; speed.
6903 ;;
6904 ;; This function might do hidden buffer changes.
6905 (or
6906 ;; This will pick up brace list declarations.
6907 (c-safe
6908 (save-excursion
6909 (goto-char containing-sexp)
6910 (c-forward-sexp -1)
6911 (let (bracepos)
6912 (if (and (or (looking-at c-brace-list-key)
6913 (progn (c-forward-sexp -1)
6914 (looking-at c-brace-list-key)))
6915 (setq bracepos (c-down-list-forward (point)))
6916 (not (c-crosses-statement-barrier-p (point)
6917 (- bracepos 2))))
6918 (point)))))
6919 ;; this will pick up array/aggregate init lists, even if they are nested.
6920 (save-excursion
6921 (let ((class-key
6922 ;; Pike can have class definitions anywhere, so we must
6923 ;; check for the class key here.
6924 (and (c-major-mode-is 'pike-mode)
6925 c-decl-block-key))
6926 bufpos braceassignp lim next-containing)
6927 (while (and (not bufpos)
6928 containing-sexp)
6929 (when paren-state
6930 (if (consp (car paren-state))
6931 (setq lim (cdr (car paren-state))
6932 paren-state (cdr paren-state))
6933 (setq lim (car paren-state)))
6934 (when paren-state
6935 (setq next-containing (car paren-state)
6936 paren-state (cdr paren-state))))
6937 (goto-char containing-sexp)
6938 (if (c-looking-at-inexpr-block next-containing next-containing)
6939 ;; We're in an in-expression block of some kind. Do not
6940 ;; check nesting. We deliberately set the limit to the
6941 ;; containing sexp, so that c-looking-at-inexpr-block
6942 ;; doesn't check for an identifier before it.
6943 (setq containing-sexp nil)
6944 ;; see if the open brace is preceded by = or [...] in
6945 ;; this statement, but watch out for operator=
6946 (setq braceassignp 'dontknow)
6947 (c-backward-token-2 1 t lim)
6948 ;; Checks to do only on the first sexp before the brace.
6949 (when (and c-opt-inexpr-brace-list-key
6950 (eq (char-after) ?\[))
6951 ;; In Java, an initialization brace list may follow
6952 ;; directly after "new Foo[]", so check for a "new"
6953 ;; earlier.
6954 (while (eq braceassignp 'dontknow)
6955 (setq braceassignp
6956 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
6957 ((looking-at c-opt-inexpr-brace-list-key) t)
6958 ((looking-at "\\sw\\|\\s_\\|[.[]")
6959 ;; Carry on looking if this is an
6960 ;; identifier (may contain "." in Java)
6961 ;; or another "[]" sexp.
6962 'dontknow)
6963 (t nil)))))
6964 ;; Checks to do on all sexps before the brace, up to the
6965 ;; beginning of the statement.
6966 (while (eq braceassignp 'dontknow)
6967 (cond ((eq (char-after) ?\;)
6968 (setq braceassignp nil))
6969 ((and class-key
6970 (looking-at class-key))
6971 (setq braceassignp nil))
6972 ((eq (char-after) ?=)
6973 ;; We've seen a =, but must check earlier tokens so
6974 ;; that it isn't something that should be ignored.
6975 (setq braceassignp 'maybe)
6976 (while (and (eq braceassignp 'maybe)
6977 (zerop (c-backward-token-2 1 t lim)))
6978 (setq braceassignp
6979 (cond
6980 ;; Check for operator =
6981 ((and c-opt-op-identifier-prefix
6982 (looking-at c-opt-op-identifier-prefix))
6983 nil)
6984 ;; Check for `<opchar>= in Pike.
6985 ((and (c-major-mode-is 'pike-mode)
6986 (or (eq (char-after) ?`)
6987 ;; Special case for Pikes
6988 ;; `[]=, since '[' is not in
6989 ;; the punctuation class.
6990 (and (eq (char-after) ?\[)
6991 (eq (char-before) ?`))))
6992 nil)
6993 ((looking-at "\\s.") 'maybe)
6994 ;; make sure we're not in a C++ template
6995 ;; argument assignment
6996 ((and
6997 (c-major-mode-is 'c++-mode)
6998 (save-excursion
6999 (let ((here (point))
7000 (pos< (progn
7001 (skip-chars-backward "^<>")
7002 (point))))
7003 (and (eq (char-before) ?<)
7004 (not (c-crosses-statement-barrier-p
7005 pos< here))
7006 (not (c-in-literal))
7007 ))))
7008 nil)
7009 (t t))))))
7010 (if (and (eq braceassignp 'dontknow)
7011 (/= (c-backward-token-2 1 t lim) 0))
7012 (setq braceassignp nil)))
7013 (if (not braceassignp)
7014 (if (eq (char-after) ?\;)
7015 ;; Brace lists can't contain a semicolon, so we're done.
7016 (setq containing-sexp nil)
7017 ;; Go up one level.
7018 (setq containing-sexp next-containing
7019 lim nil
7020 next-containing nil))
7021 ;; we've hit the beginning of the aggregate list
7022 (c-beginning-of-statement-1
7023 (c-most-enclosing-brace paren-state))
7024 (setq bufpos (point))))
7025 )
7026 bufpos))
7027 ))
7028
7029 (defun c-looking-at-special-brace-list (&optional lim)
7030 ;; If we're looking at the start of a pike-style list, ie `({ })',
7031 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
7032 ;; positions and its entry in c-special-brace-lists is returned, nil
7033 ;; otherwise. The ending position is nil if the list is still open.
7034 ;; LIM is the limit for forward search. The point may either be at
7035 ;; the `(' or at the following paren character. Tries to check the
7036 ;; matching closer, but assumes it's correct if no balanced paren is
7037 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
7038 ;; a special brace list).
7039 ;;
7040 ;; This function might do hidden buffer changes.
7041 (if c-special-brace-lists
7042 (condition-case ()
7043 (save-excursion
7044 (let ((beg (point))
7045 inner-beg end type)
7046 (c-forward-syntactic-ws)
7047 (if (eq (char-after) ?\()
7048 (progn
7049 (forward-char 1)
7050 (c-forward-syntactic-ws)
7051 (setq inner-beg (point))
7052 (setq type (assq (char-after) c-special-brace-lists)))
7053 (if (setq type (assq (char-after) c-special-brace-lists))
7054 (progn
7055 (setq inner-beg (point))
7056 (c-backward-syntactic-ws)
7057 (forward-char -1)
7058 (setq beg (if (eq (char-after) ?\()
7059 (point)
7060 nil)))))
7061 (if (and beg type)
7062 (if (and (c-safe
7063 (goto-char beg)
7064 (c-forward-sexp 1)
7065 (setq end (point))
7066 (= (char-before) ?\)))
7067 (c-safe
7068 (goto-char inner-beg)
7069 (if (looking-at "\\s(")
7070 ;; Check balancing of the inner paren
7071 ;; below.
7072 (progn
7073 (c-forward-sexp 1)
7074 t)
7075 ;; If the inner char isn't a paren then
7076 ;; we can't check balancing, so just
7077 ;; check the char before the outer
7078 ;; closing paren.
7079 (goto-char end)
7080 (backward-char)
7081 (c-backward-syntactic-ws)
7082 (= (char-before) (cdr type)))))
7083 (if (or (/= (char-syntax (char-before)) ?\))
7084 (= (progn
7085 (c-forward-syntactic-ws)
7086 (point))
7087 (1- end)))
7088 (cons (cons beg end) type))
7089 (cons (list beg) type)))))
7090 (error nil))))
7091
7092 (defun c-looking-at-bos (&optional lim)
7093 ;; Return non-nil if between two statements or declarations, assuming
7094 ;; point is not inside a literal or comment.
7095 ;;
7096 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
7097 ;; are recommended instead.
7098 ;;
7099 ;; This function might do hidden buffer changes.
7100 (c-at-statement-start-p))
7101 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p)
7102
7103 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
7104 ;; Return non-nil if we're looking at the beginning of a block
7105 ;; inside an expression. The value returned is actually a cons of
7106 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
7107 ;; position of the beginning of the construct.
7108 ;;
7109 ;; LIM limits the backward search. CONTAINING-SEXP is the start
7110 ;; position of the closest containing list. If it's nil, the
7111 ;; containing paren isn't used to decide whether we're inside an
7112 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
7113 ;; needs to be farther back.
7114 ;;
7115 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
7116 ;; brace block might be done. It should only be used when the
7117 ;; construct can be assumed to be complete, i.e. when the original
7118 ;; starting position was further down than that.
7119 ;;
7120 ;; This function might do hidden buffer changes.
7121
7122 (save-excursion
7123 (let ((res 'maybe) passed-paren
7124 (closest-lim (or containing-sexp lim (point-min)))
7125 ;; Look at the character after point only as a last resort
7126 ;; when we can't disambiguate.
7127 (block-follows (and (eq (char-after) ?{) (point))))
7128
7129 (while (and (eq res 'maybe)
7130 (progn (c-backward-syntactic-ws)
7131 (> (point) closest-lim))
7132 (not (bobp))
7133 (progn (backward-char)
7134 (looking-at "[\]\).]\\|\\w\\|\\s_"))
7135 (c-safe (forward-char)
7136 (goto-char (scan-sexps (point) -1))))
7137
7138 (setq res
7139 (if (looking-at c-keywords-regexp)
7140 (let ((kw-sym (c-keyword-sym (match-string 1))))
7141 (cond
7142 ((and block-follows
7143 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
7144 (and (not (eq passed-paren ?\[))
7145 (or (not (looking-at c-class-key))
7146 ;; If the class definition is at the start of
7147 ;; a statement, we don't consider it an
7148 ;; in-expression class.
7149 (let ((prev (point)))
7150 (while (and
7151 (= (c-backward-token-2 1 nil closest-lim) 0)
7152 (eq (char-syntax (char-after)) ?w))
7153 (setq prev (point)))
7154 (goto-char prev)
7155 (not (c-at-statement-start-p)))
7156 ;; Also, in Pike we treat it as an
7157 ;; in-expression class if it's used in an
7158 ;; object clone expression.
7159 (save-excursion
7160 (and check-at-end
7161 (c-major-mode-is 'pike-mode)
7162 (progn (goto-char block-follows)
7163 (zerop (c-forward-token-2 1 t)))
7164 (eq (char-after) ?\())))
7165 (cons 'inexpr-class (point))))
7166 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
7167 (when (not passed-paren)
7168 (cons 'inexpr-statement (point))))
7169 ((c-keyword-member kw-sym 'c-lambda-kwds)
7170 (when (or (not passed-paren)
7171 (eq passed-paren ?\())
7172 (cons 'inlambda (point))))
7173 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
7174 nil)
7175 (t
7176 'maybe)))
7177
7178 (if (looking-at "\\s(")
7179 (if passed-paren
7180 (if (and (eq passed-paren ?\[)
7181 (eq (char-after) ?\[))
7182 ;; Accept several square bracket sexps for
7183 ;; Java array initializations.
7184 'maybe)
7185 (setq passed-paren (char-after))
7186 'maybe)
7187 'maybe))))
7188
7189 (if (eq res 'maybe)
7190 (when (and c-recognize-paren-inexpr-blocks
7191 block-follows
7192 containing-sexp
7193 (eq (char-after containing-sexp) ?\())
7194 (goto-char containing-sexp)
7195 (if (or (save-excursion
7196 (c-backward-syntactic-ws lim)
7197 (and (> (point) (or lim (point-min)))
7198 (c-on-identifier)))
7199 (and c-special-brace-lists
7200 (c-looking-at-special-brace-list)))
7201 nil
7202 (cons 'inexpr-statement (point))))
7203
7204 res))))
7205
7206 (defun c-looking-at-inexpr-block-backward (paren-state)
7207 ;; Returns non-nil if we're looking at the end of an in-expression
7208 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
7209 ;; PAREN-STATE is the paren state relevant at the current position.
7210 ;;
7211 ;; This function might do hidden buffer changes.
7212 (save-excursion
7213 ;; We currently only recognize a block.
7214 (let ((here (point))
7215 (elem (car-safe paren-state))
7216 containing-sexp)
7217 (when (and (consp elem)
7218 (progn (goto-char (cdr elem))
7219 (c-forward-syntactic-ws here)
7220 (= (point) here)))
7221 (goto-char (car elem))
7222 (if (setq paren-state (cdr paren-state))
7223 (setq containing-sexp (car-safe paren-state)))
7224 (c-looking-at-inexpr-block (c-safe-position containing-sexp
7225 paren-state)
7226 containing-sexp)))))
7227
7228 \f
7229 ;; `c-guess-basic-syntax' and the functions that precedes it below
7230 ;; implements the main decision tree for determining the syntactic
7231 ;; analysis of the current line of code.
7232
7233 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
7234 ;; auto newline analysis.
7235 (defvar c-auto-newline-analysis nil)
7236
7237 (defun c-brace-anchor-point (bracepos)
7238 ;; BRACEPOS is the position of a brace in a construct like "namespace
7239 ;; Bar {". Return the anchor point in this construct; this is the
7240 ;; earliest symbol on the brace's line which isn't earlier than
7241 ;; "namespace".
7242 ;;
7243 ;; Currently (2007-08-17), "like namespace" means "matches
7244 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
7245 ;; or anything like that.
7246 (save-excursion
7247 (let ((boi (c-point 'boi bracepos)))
7248 (goto-char bracepos)
7249 (while (and (> (point) boi)
7250 (not (looking-at c-other-decl-block-key)))
7251 (c-backward-token-2))
7252 (if (> (point) boi) (point) boi))))
7253
7254 (defsubst c-add-syntax (symbol &rest args)
7255 ;; A simple function to prepend a new syntax element to
7256 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
7257 ;; should always be dynamically bound but since we read it first
7258 ;; we'll fail properly anyway if this function is misused.
7259 (setq c-syntactic-context (cons (cons symbol args)
7260 c-syntactic-context)))
7261
7262 (defsubst c-append-syntax (symbol &rest args)
7263 ;; Like `c-add-syntax' but appends to the end of the syntax list.
7264 ;; (Normally not necessary.)
7265 (setq c-syntactic-context (nconc c-syntactic-context
7266 (list (cons symbol args)))))
7267
7268 (defun c-add-stmt-syntax (syntax-symbol
7269 syntax-extra-args
7270 stop-at-boi-only
7271 containing-sexp
7272 paren-state)
7273 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
7274 ;; needed with further syntax elements of the types `substatement',
7275 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
7276 ;; `defun-block-intro'.
7277 ;;
7278 ;; Do the generic processing to anchor the given syntax symbol on
7279 ;; the preceding statement: Skip over any labels and containing
7280 ;; statements on the same line, and then search backward until we
7281 ;; find a statement or block start that begins at boi without a
7282 ;; label or comment.
7283 ;;
7284 ;; Point is assumed to be at the prospective anchor point for the
7285 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
7286 ;; skip past open parens and containing statements. Most of the added
7287 ;; syntax elements will get the same anchor point - the exception is
7288 ;; for an anchor in a construct like "namespace"[*] - this is as early
7289 ;; as possible in the construct but on the same line as the {.
7290 ;;
7291 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
7292 ;;
7293 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
7294 ;; syntax symbol. They are appended after the anchor point.
7295 ;;
7296 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
7297 ;; if the current statement starts there.
7298 ;;
7299 ;; Note: It's not a problem if PAREN-STATE "overshoots"
7300 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
7301 ;;
7302 ;; This function might do hidden buffer changes.
7303
7304 (if (= (point) (c-point 'boi))
7305 ;; This is by far the most common case, so let's give it special
7306 ;; treatment.
7307 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
7308
7309 (let ((syntax-last c-syntactic-context)
7310 (boi (c-point 'boi))
7311 ;; Set when we're on a label, so that we don't stop there.
7312 ;; FIXME: To be complete we should check if we're on a label
7313 ;; now at the start.
7314 on-label)
7315
7316 ;; Use point as the anchor point for "namespace", "extern", etc.
7317 (apply 'c-add-syntax syntax-symbol
7318 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
7319 (point) nil)
7320 syntax-extra-args)
7321
7322 ;; Loop while we have to back out of containing blocks.
7323 (while
7324 (and
7325 (catch 'back-up-block
7326
7327 ;; Loop while we have to back up statements.
7328 (while (or (/= (point) boi)
7329 on-label
7330 (looking-at c-comment-start-regexp))
7331
7332 ;; Skip past any comments that stands between the
7333 ;; statement start and boi.
7334 (let ((savepos (point)))
7335 (while (and (/= savepos boi)
7336 (c-backward-single-comment))
7337 (setq savepos (point)
7338 boi (c-point 'boi)))
7339 (goto-char savepos))
7340
7341 ;; Skip to the beginning of this statement or backward
7342 ;; another one.
7343 (let ((old-pos (point))
7344 (old-boi boi)
7345 (step-type (c-beginning-of-statement-1 containing-sexp)))
7346 (setq boi (c-point 'boi)
7347 on-label (eq step-type 'label))
7348
7349 (cond ((= (point) old-pos)
7350 ;; If we didn't move we're at the start of a block and
7351 ;; have to continue outside it.
7352 (throw 'back-up-block t))
7353
7354 ((and (eq step-type 'up)
7355 (>= (point) old-boi)
7356 (looking-at "else\\>[^_]")
7357 (save-excursion
7358 (goto-char old-pos)
7359 (looking-at "if\\>[^_]")))
7360 ;; Special case to avoid deeper and deeper indentation
7361 ;; of "else if" clauses.
7362 )
7363
7364 ((and (not stop-at-boi-only)
7365 (/= old-pos old-boi)
7366 (memq step-type '(up previous)))
7367 ;; If stop-at-boi-only is nil, we shouldn't back up
7368 ;; over previous or containing statements to try to
7369 ;; reach boi, so go back to the last position and
7370 ;; exit.
7371 (goto-char old-pos)
7372 (throw 'back-up-block nil))
7373
7374 (t
7375 (if (and (not stop-at-boi-only)
7376 (memq step-type '(up previous beginning)))
7377 ;; If we've moved into another statement then we
7378 ;; should no longer try to stop in the middle of a
7379 ;; line.
7380 (setq stop-at-boi-only t))
7381
7382 ;; Record this as a substatement if we skipped up one
7383 ;; level.
7384 (when (eq step-type 'up)
7385 (c-add-syntax 'substatement nil))))
7386 )))
7387
7388 containing-sexp)
7389
7390 ;; Now we have to go out of this block.
7391 (goto-char containing-sexp)
7392
7393 ;; Don't stop in the middle of a special brace list opener
7394 ;; like "({".
7395 (when c-special-brace-lists
7396 (let ((special-list (c-looking-at-special-brace-list)))
7397 (when (and special-list
7398 (< (car (car special-list)) (point)))
7399 (setq containing-sexp (car (car special-list)))
7400 (goto-char containing-sexp))))
7401
7402 (setq paren-state (c-whack-state-after containing-sexp paren-state)
7403 containing-sexp (c-most-enclosing-brace paren-state)
7404 boi (c-point 'boi))
7405
7406 ;; Analyze the construct in front of the block we've stepped out
7407 ;; from and add the right syntactic element for it.
7408 (let ((paren-pos (point))
7409 (paren-char (char-after))
7410 step-type)
7411
7412 (if (eq paren-char ?\()
7413 ;; Stepped out of a parenthesis block, so we're in an
7414 ;; expression now.
7415 (progn
7416 (when (/= paren-pos boi)
7417 (if (and c-recognize-paren-inexpr-blocks
7418 (progn
7419 (c-backward-syntactic-ws containing-sexp)
7420 (or (not (looking-at "\\>"))
7421 (not (c-on-identifier))))
7422 (save-excursion
7423 (goto-char (1+ paren-pos))
7424 (c-forward-syntactic-ws)
7425 (eq (char-after) ?{)))
7426 ;; Stepped out of an in-expression statement. This
7427 ;; syntactic element won't get an anchor pos.
7428 (c-add-syntax 'inexpr-statement)
7429
7430 ;; A parenthesis normally belongs to an arglist.
7431 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
7432
7433 (goto-char (max boi
7434 (if containing-sexp
7435 (1+ containing-sexp)
7436 (point-min))))
7437 (setq step-type 'same
7438 on-label nil))
7439
7440 ;; Stepped out of a brace block.
7441 (setq step-type (c-beginning-of-statement-1 containing-sexp)
7442 on-label (eq step-type 'label))
7443
7444 (if (and (eq step-type 'same)
7445 (/= paren-pos (point)))
7446 (let (inexpr)
7447 (cond
7448 ((save-excursion
7449 (goto-char paren-pos)
7450 (setq inexpr (c-looking-at-inexpr-block
7451 (c-safe-position containing-sexp paren-state)
7452 containing-sexp)))
7453 (c-add-syntax (if (eq (car inexpr) 'inlambda)
7454 'defun-block-intro
7455 'statement-block-intro)
7456 nil))
7457 ((looking-at c-other-decl-block-key)
7458 (c-add-syntax
7459 (cdr (assoc (match-string 1)
7460 c-other-decl-block-key-in-symbols-alist))
7461 (max (c-point 'boi paren-pos) (point))))
7462 (t (c-add-syntax 'defun-block-intro nil))))
7463
7464 (c-add-syntax 'statement-block-intro nil)))
7465
7466 (if (= paren-pos boi)
7467 ;; Always done if the open brace was at boi. The
7468 ;; c-beginning-of-statement-1 call above is necessary
7469 ;; anyway, to decide the type of block-intro to add.
7470 (goto-char paren-pos)
7471 (setq boi (c-point 'boi)))
7472 ))
7473
7474 ;; Fill in the current point as the anchor for all the symbols
7475 ;; added above.
7476 (let ((p c-syntactic-context) q)
7477 (while (not (eq p syntax-last))
7478 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
7479 (while q
7480 (unless (car q)
7481 (setcar q (point)))
7482 (setq q (cdr q)))
7483 (setq p (cdr p))))
7484 )))
7485
7486 (defun c-add-class-syntax (symbol
7487 containing-decl-open
7488 containing-decl-start
7489 containing-decl-kwd
7490 paren-state)
7491 ;; The inclass and class-close syntactic symbols are added in
7492 ;; several places and some work is needed to fix everything.
7493 ;; Therefore it's collected here.
7494 ;;
7495 ;; This function might do hidden buffer changes.
7496 (goto-char containing-decl-open)
7497 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
7498 (progn
7499 (c-add-syntax symbol containing-decl-open)
7500 containing-decl-open)
7501 (goto-char containing-decl-start)
7502 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
7503 ;; here, but we have to do like this for compatibility.
7504 (back-to-indentation)
7505 (c-add-syntax symbol (point))
7506 (if (and (c-keyword-member containing-decl-kwd
7507 'c-inexpr-class-kwds)
7508 (/= containing-decl-start (c-point 'boi containing-decl-start)))
7509 (c-add-syntax 'inexpr-class))
7510 (point)))
7511
7512 (defun c-guess-continued-construct (indent-point
7513 char-after-ip
7514 beg-of-same-or-containing-stmt
7515 containing-sexp
7516 paren-state)
7517 ;; This function contains the decision tree reached through both
7518 ;; cases 18 and 10. It's a continued statement or top level
7519 ;; construct of some kind.
7520 ;;
7521 ;; This function might do hidden buffer changes.
7522
7523 (let (special-brace-list)
7524 (goto-char indent-point)
7525 (skip-chars-forward " \t")
7526
7527 (cond
7528 ;; (CASE A removed.)
7529 ;; CASE B: open braces for class or brace-lists
7530 ((setq special-brace-list
7531 (or (and c-special-brace-lists
7532 (c-looking-at-special-brace-list))
7533 (eq char-after-ip ?{)))
7534
7535 (cond
7536 ;; CASE B.1: class-open
7537 ((save-excursion
7538 (and (eq (char-after) ?{)
7539 (c-looking-at-decl-block containing-sexp t)
7540 (setq beg-of-same-or-containing-stmt (point))))
7541 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
7542
7543 ;; CASE B.2: brace-list-open
7544 ((or (consp special-brace-list)
7545 (save-excursion
7546 (goto-char beg-of-same-or-containing-stmt)
7547 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
7548 indent-point t t t)))
7549 ;; The most semantically accurate symbol here is
7550 ;; brace-list-open, but we normally report it simply as a
7551 ;; statement-cont. The reason is that one normally adjusts
7552 ;; brace-list-open for brace lists as top-level constructs,
7553 ;; and brace lists inside statements is a completely different
7554 ;; context. C.f. case 5A.3.
7555 (c-beginning-of-statement-1 containing-sexp)
7556 (c-add-stmt-syntax (if c-auto-newline-analysis
7557 ;; Turn off the dwim above when we're
7558 ;; analyzing the nature of the brace
7559 ;; for the auto newline feature.
7560 'brace-list-open
7561 'statement-cont)
7562 nil nil
7563 containing-sexp paren-state))
7564
7565 ;; CASE B.3: The body of a function declared inside a normal
7566 ;; block. Can occur e.g. in Pike and when using gcc
7567 ;; extensions, but watch out for macros followed by blocks.
7568 ;; C.f. cases E, 16F and 17G.
7569 ((and (not (c-at-statement-start-p))
7570 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7571 'same)
7572 (save-excursion
7573 (let ((c-recognize-typeless-decls nil))
7574 ;; Turn off recognition of constructs that lacks a
7575 ;; type in this case, since that's more likely to be
7576 ;; a macro followed by a block.
7577 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7578 (c-add-stmt-syntax 'defun-open nil t
7579 containing-sexp paren-state))
7580
7581 ;; CASE B.4: Continued statement with block open. The most
7582 ;; accurate analysis is perhaps `statement-cont' together with
7583 ;; `block-open' but we play DWIM and use `substatement-open'
7584 ;; instead. The rationaly is that this typically is a macro
7585 ;; followed by a block which makes it very similar to a
7586 ;; statement with a substatement block.
7587 (t
7588 (c-add-stmt-syntax 'substatement-open nil nil
7589 containing-sexp paren-state))
7590 ))
7591
7592 ;; CASE C: iostream insertion or extraction operator
7593 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
7594 (save-excursion
7595 (goto-char beg-of-same-or-containing-stmt)
7596 ;; If there is no preceding streamop in the statement
7597 ;; then indent this line as a normal statement-cont.
7598 (when (c-syntactic-re-search-forward
7599 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
7600 (c-add-syntax 'stream-op (c-point 'boi))
7601 t))))
7602
7603 ;; CASE E: In the "K&R region" of a function declared inside a
7604 ;; normal block. C.f. case B.3.
7605 ((and (save-excursion
7606 ;; Check that the next token is a '{'. This works as
7607 ;; long as no language that allows nested function
7608 ;; definitions allows stuff like member init lists, K&R
7609 ;; declarations or throws clauses there.
7610 ;;
7611 ;; Note that we do a forward search for something ahead
7612 ;; of the indentation line here. That's not good since
7613 ;; the user might not have typed it yet. Unfortunately
7614 ;; it's exceedingly tricky to recognize a function
7615 ;; prototype in a code block without resorting to this.
7616 (c-forward-syntactic-ws)
7617 (eq (char-after) ?{))
7618 (not (c-at-statement-start-p))
7619 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7620 'same)
7621 (save-excursion
7622 (let ((c-recognize-typeless-decls nil))
7623 ;; Turn off recognition of constructs that lacks a
7624 ;; type in this case, since that's more likely to be
7625 ;; a macro followed by a block.
7626 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7627 (c-add-stmt-syntax 'func-decl-cont nil t
7628 containing-sexp paren-state))
7629
7630 ;; CASE D: continued statement.
7631 (t
7632 (c-beginning-of-statement-1 containing-sexp)
7633 (c-add-stmt-syntax 'statement-cont nil nil
7634 containing-sexp paren-state))
7635 )))
7636
7637 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
7638 ;; 2005/11/29).
7639 ;;;###autoload
7640 (defun c-guess-basic-syntax ()
7641 "Return the syntactic context of the current line."
7642 (save-excursion
7643 (beginning-of-line)
7644 (c-save-buffer-state
7645 ((indent-point (point))
7646 (case-fold-search nil)
7647 ;; A whole ugly bunch of various temporary variables. Have
7648 ;; to declare them here since it's not possible to declare
7649 ;; a variable with only the scope of a cond test and the
7650 ;; following result clauses, and most of this function is a
7651 ;; single gigantic cond. :P
7652 literal char-before-ip before-ws-ip char-after-ip macro-start
7653 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
7654 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
7655 ;; The following record some positions for the containing
7656 ;; declaration block if we're directly within one:
7657 ;; `containing-decl-open' is the position of the open
7658 ;; brace. `containing-decl-start' is the start of the
7659 ;; declaration. `containing-decl-kwd' is the keyword
7660 ;; symbol of the keyword that tells what kind of block it
7661 ;; is.
7662 containing-decl-open
7663 containing-decl-start
7664 containing-decl-kwd
7665 ;; The open paren of the closest surrounding sexp or nil if
7666 ;; there is none.
7667 containing-sexp
7668 ;; The position after the closest preceding brace sexp
7669 ;; (nested sexps are ignored), or the position after
7670 ;; `containing-sexp' if there is none, or (point-min) if
7671 ;; `containing-sexp' is nil.
7672 lim
7673 ;; The paren state outside `containing-sexp', or at
7674 ;; `indent-point' if `containing-sexp' is nil.
7675 (paren-state (c-parse-state))
7676 ;; There's always at most one syntactic element which got
7677 ;; an anchor pos. It's stored in syntactic-relpos.
7678 syntactic-relpos
7679 (c-stmt-delim-chars c-stmt-delim-chars))
7680
7681 ;; Check if we're directly inside an enclosing declaration
7682 ;; level block.
7683 (when (and (setq containing-sexp
7684 (c-most-enclosing-brace paren-state))
7685 (progn
7686 (goto-char containing-sexp)
7687 (eq (char-after) ?{))
7688 (setq placeholder
7689 (c-looking-at-decl-block
7690 (c-most-enclosing-brace paren-state
7691 containing-sexp)
7692 t)))
7693 (setq containing-decl-open containing-sexp
7694 containing-decl-start (point)
7695 containing-sexp nil)
7696 (goto-char placeholder)
7697 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
7698 (c-keyword-sym (match-string 1)))))
7699
7700 ;; Init some position variables.
7701 (if c-state-cache
7702 (progn
7703 (setq containing-sexp (car paren-state)
7704 paren-state (cdr paren-state))
7705 (if (consp containing-sexp)
7706 (progn
7707 (setq lim (cdr containing-sexp))
7708 (if (cdr c-state-cache)
7709 ;; Ignore balanced paren. The next entry
7710 ;; can't be another one.
7711 (setq containing-sexp (car (cdr c-state-cache))
7712 paren-state (cdr paren-state))
7713 ;; If there is no surrounding open paren then
7714 ;; put the last balanced pair back on paren-state.
7715 (setq paren-state (cons containing-sexp paren-state)
7716 containing-sexp nil)))
7717 (setq lim (1+ containing-sexp))))
7718 (setq lim (point-min)))
7719
7720 ;; If we're in a parenthesis list then ',' delimits the
7721 ;; "statements" rather than being an operator (with the
7722 ;; exception of the "for" clause). This difference is
7723 ;; typically only noticeable when statements are used in macro
7724 ;; arglists.
7725 (when (and containing-sexp
7726 (eq (char-after containing-sexp) ?\())
7727 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
7728
7729 ;; cache char before and after indent point, and move point to
7730 ;; the most likely position to perform the majority of tests
7731 (goto-char indent-point)
7732 (c-backward-syntactic-ws lim)
7733 (setq before-ws-ip (point)
7734 char-before-ip (char-before))
7735 (goto-char indent-point)
7736 (skip-chars-forward " \t")
7737 (setq char-after-ip (char-after))
7738
7739 ;; are we in a literal?
7740 (setq literal (c-in-literal lim))
7741
7742 ;; now figure out syntactic qualities of the current line
7743 (cond
7744
7745 ;; CASE 1: in a string.
7746 ((eq literal 'string)
7747 (c-add-syntax 'string (c-point 'bopl)))
7748
7749 ;; CASE 2: in a C or C++ style comment.
7750 ((and (memq literal '(c c++))
7751 ;; This is a kludge for XEmacs where we use
7752 ;; `buffer-syntactic-context', which doesn't correctly
7753 ;; recognize "\*/" to end a block comment.
7754 ;; `parse-partial-sexp' which is used by
7755 ;; `c-literal-limits' will however do that in most
7756 ;; versions, which results in that we get nil from
7757 ;; `c-literal-limits' even when `c-in-literal' claims
7758 ;; we're inside a comment.
7759 (setq placeholder (c-literal-limits lim)))
7760 (c-add-syntax literal (car placeholder)))
7761
7762 ;; CASE 3: in a cpp preprocessor macro continuation.
7763 ((and (save-excursion
7764 (when (c-beginning-of-macro)
7765 (setq macro-start (point))))
7766 (/= macro-start (c-point 'boi))
7767 (progn
7768 (setq tmpsymbol 'cpp-macro-cont)
7769 (or (not c-syntactic-indentation-in-macros)
7770 (save-excursion
7771 (goto-char macro-start)
7772 ;; If at the beginning of the body of a #define
7773 ;; directive then analyze as cpp-define-intro
7774 ;; only. Go on with the syntactic analysis
7775 ;; otherwise. in-macro-expr is set if we're in a
7776 ;; cpp expression, i.e. before the #define body
7777 ;; or anywhere in a non-#define directive.
7778 (if (c-forward-to-cpp-define-body)
7779 (let ((indent-boi (c-point 'boi indent-point)))
7780 (setq in-macro-expr (> (point) indent-boi)
7781 tmpsymbol 'cpp-define-intro)
7782 (= (point) indent-boi))
7783 (setq in-macro-expr t)
7784 nil)))))
7785 (c-add-syntax tmpsymbol macro-start)
7786 (setq macro-start nil))
7787
7788 ;; CASE 11: an else clause?
7789 ((looking-at "else\\>[^_]")
7790 (c-beginning-of-statement-1 containing-sexp)
7791 (c-add-stmt-syntax 'else-clause nil t
7792 containing-sexp paren-state))
7793
7794 ;; CASE 12: while closure of a do/while construct?
7795 ((and (looking-at "while\\>[^_]")
7796 (save-excursion
7797 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
7798 'beginning)
7799 (setq placeholder (point)))))
7800 (goto-char placeholder)
7801 (c-add-stmt-syntax 'do-while-closure nil t
7802 containing-sexp paren-state))
7803
7804 ;; CASE 13: A catch or finally clause? This case is simpler
7805 ;; than if-else and do-while, because a block is required
7806 ;; after every try, catch and finally.
7807 ((save-excursion
7808 (and (cond ((c-major-mode-is 'c++-mode)
7809 (looking-at "catch\\>[^_]"))
7810 ((c-major-mode-is 'java-mode)
7811 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
7812 (and (c-safe (c-backward-syntactic-ws)
7813 (c-backward-sexp)
7814 t)
7815 (eq (char-after) ?{)
7816 (c-safe (c-backward-syntactic-ws)
7817 (c-backward-sexp)
7818 t)
7819 (if (eq (char-after) ?\()
7820 (c-safe (c-backward-sexp) t)
7821 t))
7822 (looking-at "\\(try\\|catch\\)\\>[^_]")
7823 (setq placeholder (point))))
7824 (goto-char placeholder)
7825 (c-add-stmt-syntax 'catch-clause nil t
7826 containing-sexp paren-state))
7827
7828 ;; CASE 18: A substatement we can recognize by keyword.
7829 ((save-excursion
7830 (and c-opt-block-stmt-key
7831 (not (eq char-before-ip ?\;))
7832 (not (c-at-vsemi-p before-ws-ip))
7833 (not (memq char-after-ip '(?\) ?\] ?,)))
7834 (or (not (eq char-before-ip ?}))
7835 (c-looking-at-inexpr-block-backward c-state-cache))
7836 (> (point)
7837 (progn
7838 ;; Ought to cache the result from the
7839 ;; c-beginning-of-statement-1 calls here.
7840 (setq placeholder (point))
7841 (while (eq (setq step-type
7842 (c-beginning-of-statement-1 lim))
7843 'label))
7844 (if (eq step-type 'previous)
7845 (goto-char placeholder)
7846 (setq placeholder (point))
7847 (if (and (eq step-type 'same)
7848 (not (looking-at c-opt-block-stmt-key)))
7849 ;; Step up to the containing statement if we
7850 ;; stayed in the same one.
7851 (let (step)
7852 (while (eq
7853 (setq step
7854 (c-beginning-of-statement-1 lim))
7855 'label))
7856 (if (eq step 'up)
7857 (setq placeholder (point))
7858 ;; There was no containing statement afterall.
7859 (goto-char placeholder)))))
7860 placeholder))
7861 (if (looking-at c-block-stmt-2-key)
7862 ;; Require a parenthesis after these keywords.
7863 ;; Necessary to catch e.g. synchronized in Java,
7864 ;; which can be used both as statement and
7865 ;; modifier.
7866 (and (zerop (c-forward-token-2 1 nil))
7867 (eq (char-after) ?\())
7868 (looking-at c-opt-block-stmt-key))))
7869
7870 (if (eq step-type 'up)
7871 ;; CASE 18A: Simple substatement.
7872 (progn
7873 (goto-char placeholder)
7874 (cond
7875 ((eq char-after-ip ?{)
7876 (c-add-stmt-syntax 'substatement-open nil nil
7877 containing-sexp paren-state))
7878 ((save-excursion
7879 (goto-char indent-point)
7880 (back-to-indentation)
7881 (c-forward-label))
7882 (c-add-stmt-syntax 'substatement-label nil nil
7883 containing-sexp paren-state))
7884 (t
7885 (c-add-stmt-syntax 'substatement nil nil
7886 containing-sexp paren-state))))
7887
7888 ;; CASE 18B: Some other substatement. This is shared
7889 ;; with case 10.
7890 (c-guess-continued-construct indent-point
7891 char-after-ip
7892 placeholder
7893 lim
7894 paren-state)))
7895
7896 ;; CASE 14: A case or default label
7897 ((looking-at c-label-kwds-regexp)
7898 (if containing-sexp
7899 (progn
7900 (goto-char containing-sexp)
7901 (setq lim (c-most-enclosing-brace c-state-cache
7902 containing-sexp))
7903 (c-backward-to-block-anchor lim)
7904 (c-add-stmt-syntax 'case-label nil t lim paren-state))
7905 ;; Got a bogus label at the top level. In lack of better
7906 ;; alternatives, anchor it on (point-min).
7907 (c-add-syntax 'case-label (point-min))))
7908
7909 ;; CASE 15: any other label
7910 ((save-excursion
7911 (back-to-indentation)
7912 (and (not (looking-at c-syntactic-ws-start))
7913 (c-forward-label)))
7914 (cond (containing-decl-open
7915 (setq placeholder (c-add-class-syntax 'inclass
7916 containing-decl-open
7917 containing-decl-start
7918 containing-decl-kwd
7919 paren-state))
7920 ;; Append access-label with the same anchor point as
7921 ;; inclass gets.
7922 (c-append-syntax 'access-label placeholder))
7923
7924 (containing-sexp
7925 (goto-char containing-sexp)
7926 (setq lim (c-most-enclosing-brace c-state-cache
7927 containing-sexp))
7928 (save-excursion
7929 (setq tmpsymbol
7930 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
7931 (looking-at "switch\\>[^_]"))
7932 ;; If the surrounding statement is a switch then
7933 ;; let's analyze all labels as switch labels, so
7934 ;; that they get lined up consistently.
7935 'case-label
7936 'label)))
7937 (c-backward-to-block-anchor lim)
7938 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
7939
7940 (t
7941 ;; A label on the top level. Treat it as a class
7942 ;; context. (point-min) is the closest we get to the
7943 ;; class open brace.
7944 (c-add-syntax 'access-label (point-min)))))
7945
7946 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
7947 ;; 17E.
7948 ((setq placeholder (c-looking-at-inexpr-block
7949 (c-safe-position containing-sexp paren-state)
7950 containing-sexp
7951 ;; Have to turn on the heuristics after
7952 ;; the point even though it doesn't work
7953 ;; very well. C.f. test case class-16.pike.
7954 t))
7955 (setq tmpsymbol (assq (car placeholder)
7956 '((inexpr-class . class-open)
7957 (inexpr-statement . block-open))))
7958 (if tmpsymbol
7959 ;; It's a statement block or an anonymous class.
7960 (setq tmpsymbol (cdr tmpsymbol))
7961 ;; It's a Pike lambda. Check whether we are between the
7962 ;; lambda keyword and the argument list or at the defun
7963 ;; opener.
7964 (setq tmpsymbol (if (eq char-after-ip ?{)
7965 'inline-open
7966 'lambda-intro-cont)))
7967 (goto-char (cdr placeholder))
7968 (back-to-indentation)
7969 (c-add-stmt-syntax tmpsymbol nil t
7970 (c-most-enclosing-brace c-state-cache (point))
7971 paren-state)
7972 (unless (eq (point) (cdr placeholder))
7973 (c-add-syntax (car placeholder))))
7974
7975 ;; CASE 5: Line is inside a declaration level block or at top level.
7976 ((or containing-decl-open (null containing-sexp))
7977 (cond
7978
7979 ;; CASE 5A: we are looking at a defun, brace list, class,
7980 ;; or inline-inclass method opening brace
7981 ((setq special-brace-list
7982 (or (and c-special-brace-lists
7983 (c-looking-at-special-brace-list))
7984 (eq char-after-ip ?{)))
7985 (cond
7986
7987 ;; CASE 5A.1: Non-class declaration block open.
7988 ((save-excursion
7989 (let (tmp)
7990 (and (eq char-after-ip ?{)
7991 (setq tmp (c-looking-at-decl-block containing-sexp t))
7992 (progn
7993 (setq placeholder (point))
7994 (goto-char tmp)
7995 (looking-at c-symbol-key))
7996 (c-keyword-member
7997 (c-keyword-sym (setq keyword (match-string 0)))
7998 'c-other-block-decl-kwds))))
7999 (goto-char placeholder)
8000 (c-add-stmt-syntax
8001 (if (string-equal keyword "extern")
8002 ;; Special case for extern-lang-open.
8003 'extern-lang-open
8004 (intern (concat keyword "-open")))
8005 nil t containing-sexp paren-state))
8006
8007 ;; CASE 5A.2: we are looking at a class opening brace
8008 ((save-excursion
8009 (goto-char indent-point)
8010 (skip-chars-forward " \t")
8011 (and (eq (char-after) ?{)
8012 (c-looking-at-decl-block containing-sexp t)
8013 (setq placeholder (point))))
8014 (c-add-syntax 'class-open placeholder))
8015
8016 ;; CASE 5A.3: brace list open
8017 ((save-excursion
8018 (c-beginning-of-decl-1 lim)
8019 (while (looking-at c-specifier-key)
8020 (goto-char (match-end 1))
8021 (c-forward-syntactic-ws indent-point))
8022 (setq placeholder (c-point 'boi))
8023 (or (consp special-brace-list)
8024 (and (or (save-excursion
8025 (goto-char indent-point)
8026 (setq tmpsymbol nil)
8027 (while (and (> (point) placeholder)
8028 (zerop (c-backward-token-2 1 t))
8029 (/= (char-after) ?=))
8030 (and c-opt-inexpr-brace-list-key
8031 (not tmpsymbol)
8032 (looking-at c-opt-inexpr-brace-list-key)
8033 (setq tmpsymbol 'topmost-intro-cont)))
8034 (eq (char-after) ?=))
8035 (looking-at c-brace-list-key))
8036 (save-excursion
8037 (while (and (< (point) indent-point)
8038 (zerop (c-forward-token-2 1 t))
8039 (not (memq (char-after) '(?\; ?\()))))
8040 (not (memq (char-after) '(?\; ?\()))
8041 ))))
8042 (if (and (not c-auto-newline-analysis)
8043 (c-major-mode-is 'java-mode)
8044 (eq tmpsymbol 'topmost-intro-cont))
8045 ;; We're in Java and have found that the open brace
8046 ;; belongs to a "new Foo[]" initialization list,
8047 ;; which means the brace list is part of an
8048 ;; expression and not a top level definition. We
8049 ;; therefore treat it as any topmost continuation
8050 ;; even though the semantically correct symbol still
8051 ;; is brace-list-open, on the same grounds as in
8052 ;; case B.2.
8053 (progn
8054 (c-beginning-of-statement-1 lim)
8055 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8056 (c-add-syntax 'brace-list-open placeholder)))
8057
8058 ;; CASE 5A.4: inline defun open
8059 ((and containing-decl-open
8060 (not (c-keyword-member containing-decl-kwd
8061 'c-other-block-decl-kwds)))
8062 (c-add-syntax 'inline-open)
8063 (c-add-class-syntax 'inclass
8064 containing-decl-open
8065 containing-decl-start
8066 containing-decl-kwd
8067 paren-state))
8068
8069 ;; CASE 5A.5: ordinary defun open
8070 (t
8071 (save-excursion
8072 (c-beginning-of-decl-1 lim)
8073 (while (looking-at c-specifier-key)
8074 (goto-char (match-end 1))
8075 (c-forward-syntactic-ws indent-point))
8076 (c-add-syntax 'defun-open (c-point 'boi))
8077 ;; Bogus to use bol here, but it's the legacy. (Resolved,
8078 ;; 2007-11-09)
8079 ))))
8080
8081 ;; CASE 5B: After a function header but before the body (or
8082 ;; the ending semicolon if there's no body).
8083 ((save-excursion
8084 (when (setq placeholder (c-just-after-func-arglist-p lim))
8085 (setq tmp-pos (point))))
8086 (cond
8087
8088 ;; CASE 5B.1: Member init list.
8089 ((eq (char-after tmp-pos) ?:)
8090 (if (or (> tmp-pos indent-point)
8091 (= (c-point 'bosws) (1+ tmp-pos)))
8092 (progn
8093 ;; There is no preceding member init clause.
8094 ;; Indent relative to the beginning of indentation
8095 ;; for the topmost-intro line that contains the
8096 ;; prototype's open paren.
8097 (goto-char placeholder)
8098 (c-add-syntax 'member-init-intro (c-point 'boi)))
8099 ;; Indent relative to the first member init clause.
8100 (goto-char (1+ tmp-pos))
8101 (c-forward-syntactic-ws)
8102 (c-add-syntax 'member-init-cont (point))))
8103
8104 ;; CASE 5B.2: K&R arg decl intro
8105 ((and c-recognize-knr-p
8106 (c-in-knr-argdecl lim))
8107 (c-beginning-of-statement-1 lim)
8108 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
8109 (if containing-decl-open
8110 (c-add-class-syntax 'inclass
8111 containing-decl-open
8112 containing-decl-start
8113 containing-decl-kwd
8114 paren-state)))
8115
8116 ;; CASE 5B.4: Nether region after a C++ or Java func
8117 ;; decl, which could include a `throws' declaration.
8118 (t
8119 (c-beginning-of-statement-1 lim)
8120 (c-add-syntax 'func-decl-cont (c-point 'boi))
8121 )))
8122
8123 ;; CASE 5C: inheritance line. could be first inheritance
8124 ;; line, or continuation of a multiple inheritance
8125 ((or (and (c-major-mode-is 'c++-mode)
8126 (progn
8127 (when (eq char-after-ip ?,)
8128 (skip-chars-forward " \t")
8129 (forward-char))
8130 (looking-at c-opt-postfix-decl-spec-key)))
8131 (and (or (eq char-before-ip ?:)
8132 ;; watch out for scope operator
8133 (save-excursion
8134 (and (eq char-after-ip ?:)
8135 (c-safe (forward-char 1) t)
8136 (not (eq (char-after) ?:))
8137 )))
8138 (save-excursion
8139 (c-backward-syntactic-ws lim)
8140 (if (eq char-before-ip ?:)
8141 (progn
8142 (forward-char -1)
8143 (c-backward-syntactic-ws lim)))
8144 (back-to-indentation)
8145 (looking-at c-class-key)))
8146 ;; for Java
8147 (and (c-major-mode-is 'java-mode)
8148 (let ((fence (save-excursion
8149 (c-beginning-of-statement-1 lim)
8150 (point)))
8151 cont done)
8152 (save-excursion
8153 (while (not done)
8154 (cond ((looking-at c-opt-postfix-decl-spec-key)
8155 (setq injava-inher (cons cont (point))
8156 done t))
8157 ((or (not (c-safe (c-forward-sexp -1) t))
8158 (<= (point) fence))
8159 (setq done t))
8160 )
8161 (setq cont t)))
8162 injava-inher)
8163 (not (c-crosses-statement-barrier-p (cdr injava-inher)
8164 (point)))
8165 ))
8166 (cond
8167
8168 ;; CASE 5C.1: non-hanging colon on an inher intro
8169 ((eq char-after-ip ?:)
8170 (c-beginning-of-statement-1 lim)
8171 (c-add-syntax 'inher-intro (c-point 'boi))
8172 ;; don't add inclass symbol since relative point already
8173 ;; contains any class offset
8174 )
8175
8176 ;; CASE 5C.2: hanging colon on an inher intro
8177 ((eq char-before-ip ?:)
8178 (c-beginning-of-statement-1 lim)
8179 (c-add-syntax 'inher-intro (c-point 'boi))
8180 (if containing-decl-open
8181 (c-add-class-syntax 'inclass
8182 containing-decl-open
8183 containing-decl-start
8184 containing-decl-kwd
8185 paren-state)))
8186
8187 ;; CASE 5C.3: in a Java implements/extends
8188 (injava-inher
8189 (let ((where (cdr injava-inher))
8190 (cont (car injava-inher)))
8191 (goto-char where)
8192 (cond ((looking-at "throws\\>[^_]")
8193 (c-add-syntax 'func-decl-cont
8194 (progn (c-beginning-of-statement-1 lim)
8195 (c-point 'boi))))
8196 (cont (c-add-syntax 'inher-cont where))
8197 (t (c-add-syntax 'inher-intro
8198 (progn (goto-char (cdr injava-inher))
8199 (c-beginning-of-statement-1 lim)
8200 (point))))
8201 )))
8202
8203 ;; CASE 5C.4: a continued inheritance line
8204 (t
8205 (c-beginning-of-inheritance-list lim)
8206 (c-add-syntax 'inher-cont (point))
8207 ;; don't add inclass symbol since relative point already
8208 ;; contains any class offset
8209 )))
8210
8211 ;; CASE 5D: this could be a top-level initialization, a
8212 ;; member init list continuation, or a template argument
8213 ;; list continuation.
8214 ((save-excursion
8215 ;; Note: We use the fact that lim is always after any
8216 ;; preceding brace sexp.
8217 (if c-recognize-<>-arglists
8218 (while (and
8219 (progn
8220 (c-syntactic-skip-backward "^;,=<>" lim t)
8221 (> (point) lim))
8222 (or
8223 (when c-overloadable-operators-regexp
8224 (when (setq placeholder (c-after-special-operator-id lim))
8225 (goto-char placeholder)
8226 t))
8227 (cond
8228 ((eq (char-before) ?>)
8229 (or (c-backward-<>-arglist nil lim)
8230 (backward-char))
8231 t)
8232 ((eq (char-before) ?<)
8233 (backward-char)
8234 (if (save-excursion
8235 (c-forward-<>-arglist nil))
8236 (progn (forward-char)
8237 nil)
8238 t))
8239 (t nil)))))
8240 ;; NB: No c-after-special-operator-id stuff in this
8241 ;; clause - we assume only C++ needs it.
8242 (c-syntactic-skip-backward "^;,=" lim t))
8243 (memq (char-before) '(?, ?= ?<)))
8244 (cond
8245
8246 ;; CASE 5D.3: perhaps a template list continuation?
8247 ((and (c-major-mode-is 'c++-mode)
8248 (save-excursion
8249 (save-restriction
8250 (c-with-syntax-table c++-template-syntax-table
8251 (goto-char indent-point)
8252 (setq placeholder (c-up-list-backward))
8253 (and placeholder
8254 (eq (char-after placeholder) ?<))))))
8255 (c-with-syntax-table c++-template-syntax-table
8256 (goto-char placeholder)
8257 (c-beginning-of-statement-1 lim t)
8258 (if (save-excursion
8259 (c-backward-syntactic-ws lim)
8260 (eq (char-before) ?<))
8261 ;; In a nested template arglist.
8262 (progn
8263 (goto-char placeholder)
8264 (c-syntactic-skip-backward "^,;" lim t)
8265 (c-forward-syntactic-ws))
8266 (back-to-indentation)))
8267 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8268 ;; template aware.
8269 (c-add-syntax 'template-args-cont (point)))
8270
8271 ;; CASE 5D.4: perhaps a multiple inheritance line?
8272 ((and (c-major-mode-is 'c++-mode)
8273 (save-excursion
8274 (c-beginning-of-statement-1 lim)
8275 (setq placeholder (point))
8276 (if (looking-at "static\\>[^_]")
8277 (c-forward-token-2 1 nil indent-point))
8278 (and (looking-at c-class-key)
8279 (zerop (c-forward-token-2 2 nil indent-point))
8280 (if (eq (char-after) ?<)
8281 (c-with-syntax-table c++-template-syntax-table
8282 (zerop (c-forward-token-2 1 t indent-point)))
8283 t)
8284 (eq (char-after) ?:))))
8285 (goto-char placeholder)
8286 (c-add-syntax 'inher-cont (c-point 'boi)))
8287
8288 ;; CASE 5D.5: Continuation of the "expression part" of a
8289 ;; top level construct. Or, perhaps, an unrecognised construct.
8290 (t
8291 (while (and (setq placeholder (point))
8292 (eq (car (c-beginning-of-decl-1 containing-sexp))
8293 'same)
8294 (save-excursion
8295 (c-backward-syntactic-ws)
8296 (eq (char-before) ?}))
8297 (< (point) placeholder)))
8298 (c-add-stmt-syntax
8299 (cond
8300 ((eq (point) placeholder) 'statement) ; unrecognised construct
8301 ;; A preceding comma at the top level means that a
8302 ;; new variable declaration starts here. Use
8303 ;; topmost-intro-cont for it, for consistency with
8304 ;; the first variable declaration. C.f. case 5N.
8305 ((eq char-before-ip ?,) 'topmost-intro-cont)
8306 (t 'statement-cont))
8307 nil nil containing-sexp paren-state))
8308 ))
8309
8310 ;; CASE 5F: Close of a non-class declaration level block.
8311 ((and (eq char-after-ip ?})
8312 (c-keyword-member containing-decl-kwd
8313 'c-other-block-decl-kwds))
8314 ;; This is inconsistent: Should use `containing-decl-open'
8315 ;; here if it's at boi, like in case 5J.
8316 (goto-char containing-decl-start)
8317 (c-add-stmt-syntax
8318 (if (string-equal (symbol-name containing-decl-kwd) "extern")
8319 ;; Special case for compatibility with the
8320 ;; extern-lang syntactic symbols.
8321 'extern-lang-close
8322 (intern (concat (symbol-name containing-decl-kwd)
8323 "-close")))
8324 nil t
8325 (c-most-enclosing-brace paren-state (point))
8326 paren-state))
8327
8328 ;; CASE 5G: we are looking at the brace which closes the
8329 ;; enclosing nested class decl
8330 ((and containing-sexp
8331 (eq char-after-ip ?})
8332 (eq containing-decl-open containing-sexp))
8333 (c-add-class-syntax 'class-close
8334 containing-decl-open
8335 containing-decl-start
8336 containing-decl-kwd
8337 paren-state))
8338
8339 ;; CASE 5H: we could be looking at subsequent knr-argdecls
8340 ((and c-recognize-knr-p
8341 (not containing-sexp) ; can't be knr inside braces.
8342 (not (eq char-before-ip ?}))
8343 (save-excursion
8344 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
8345 (and placeholder
8346 ;; Do an extra check to avoid tripping up on
8347 ;; statements that occur in invalid contexts
8348 ;; (e.g. in macro bodies where we don't really
8349 ;; know the context of what we're looking at).
8350 (not (and c-opt-block-stmt-key
8351 (looking-at c-opt-block-stmt-key)))))
8352 (< placeholder indent-point))
8353 (goto-char placeholder)
8354 (c-add-syntax 'knr-argdecl (point)))
8355
8356 ;; CASE 5I: ObjC method definition.
8357 ((and c-opt-method-key
8358 (looking-at c-opt-method-key))
8359 (c-beginning-of-statement-1 nil t)
8360 (if (= (point) indent-point)
8361 ;; Handle the case when it's the first (non-comment)
8362 ;; thing in the buffer. Can't look for a 'same return
8363 ;; value from cbos1 since ObjC directives currently
8364 ;; aren't recognized fully, so that we get 'same
8365 ;; instead of 'previous if it moved over a preceding
8366 ;; directive.
8367 (goto-char (point-min)))
8368 (c-add-syntax 'objc-method-intro (c-point 'boi)))
8369
8370 ;; CASE 5P: AWK pattern or function or continuation
8371 ;; thereof.
8372 ((c-major-mode-is 'awk-mode)
8373 (setq placeholder (point))
8374 (c-add-stmt-syntax
8375 (if (and (eq (c-beginning-of-statement-1) 'same)
8376 (/= (point) placeholder))
8377 'topmost-intro-cont
8378 'topmost-intro)
8379 nil nil
8380 containing-sexp paren-state))
8381
8382 ;; CASE 5N: At a variable declaration that follows a class
8383 ;; definition or some other block declaration that doesn't
8384 ;; end at the closing '}'. C.f. case 5D.5.
8385 ((progn
8386 (c-backward-syntactic-ws lim)
8387 (and (eq (char-before) ?})
8388 (save-excursion
8389 (let ((start (point)))
8390 (if (and c-state-cache
8391 (consp (car c-state-cache))
8392 (eq (cdar c-state-cache) (point)))
8393 ;; Speed up the backward search a bit.
8394 (goto-char (caar c-state-cache)))
8395 (c-beginning-of-decl-1 containing-sexp)
8396 (setq placeholder (point))
8397 (if (= start (point))
8398 ;; The '}' is unbalanced.
8399 nil
8400 (c-end-of-decl-1)
8401 (>= (point) indent-point))))))
8402 (goto-char placeholder)
8403 (c-add-stmt-syntax 'topmost-intro-cont nil nil
8404 containing-sexp paren-state))
8405
8406 ;; NOTE: The point is at the end of the previous token here.
8407
8408 ;; CASE 5J: we are at the topmost level, make
8409 ;; sure we skip back past any access specifiers
8410 ((and
8411 ;; A macro continuation line is never at top level.
8412 (not (and macro-start
8413 (> indent-point macro-start)))
8414 (save-excursion
8415 (setq placeholder (point))
8416 (or (memq char-before-ip '(?\; ?{ ?} nil))
8417 (c-at-vsemi-p before-ws-ip)
8418 (when (and (eq char-before-ip ?:)
8419 (eq (c-beginning-of-statement-1 lim)
8420 'label))
8421 (c-backward-syntactic-ws lim)
8422 (setq placeholder (point)))
8423 (and (c-major-mode-is 'objc-mode)
8424 (catch 'not-in-directive
8425 (c-beginning-of-statement-1 lim)
8426 (setq placeholder (point))
8427 (while (and (c-forward-objc-directive)
8428 (< (point) indent-point))
8429 (c-forward-syntactic-ws)
8430 (if (>= (point) indent-point)
8431 (throw 'not-in-directive t))
8432 (setq placeholder (point)))
8433 nil)))))
8434 ;; For historic reasons we anchor at bol of the last
8435 ;; line of the previous declaration. That's clearly
8436 ;; highly bogus and useless, and it makes our lives hard
8437 ;; to remain compatible. :P
8438 (goto-char placeholder)
8439 (c-add-syntax 'topmost-intro (c-point 'bol))
8440 (if containing-decl-open
8441 (if (c-keyword-member containing-decl-kwd
8442 'c-other-block-decl-kwds)
8443 (progn
8444 (goto-char (c-brace-anchor-point containing-decl-open))
8445 (c-add-stmt-syntax
8446 (if (string-equal (symbol-name containing-decl-kwd)
8447 "extern")
8448 ;; Special case for compatibility with the
8449 ;; extern-lang syntactic symbols.
8450 'inextern-lang
8451 (intern (concat "in"
8452 (symbol-name containing-decl-kwd))))
8453 nil t
8454 (c-most-enclosing-brace paren-state (point))
8455 paren-state))
8456 (c-add-class-syntax 'inclass
8457 containing-decl-open
8458 containing-decl-start
8459 containing-decl-kwd
8460 paren-state)))
8461 (when (and c-syntactic-indentation-in-macros
8462 macro-start
8463 (/= macro-start (c-point 'boi indent-point)))
8464 (c-add-syntax 'cpp-define-intro)
8465 (setq macro-start nil)))
8466
8467 ;; CASE 5K: we are at an ObjC method definition
8468 ;; continuation line.
8469 ((and c-opt-method-key
8470 (save-excursion
8471 (c-beginning-of-statement-1 lim)
8472 (beginning-of-line)
8473 (when (looking-at c-opt-method-key)
8474 (setq placeholder (point)))))
8475 (c-add-syntax 'objc-method-args-cont placeholder))
8476
8477 ;; CASE 5L: we are at the first argument of a template
8478 ;; arglist that begins on the previous line.
8479 ((and c-recognize-<>-arglists
8480 (eq (char-before) ?<)
8481 (not (and c-overloadable-operators-regexp
8482 (c-after-special-operator-id lim))))
8483 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8484 (c-add-syntax 'template-args-cont (c-point 'boi)))
8485
8486 ;; CASE 5Q: we are at a statement within a macro.
8487 (macro-start
8488 (c-beginning-of-statement-1 containing-sexp)
8489 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
8490
8491 ;; CASE 5M: we are at a topmost continuation line
8492 (t
8493 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8494 (when (c-major-mode-is 'objc-mode)
8495 (setq placeholder (point))
8496 (while (and (c-forward-objc-directive)
8497 (< (point) indent-point))
8498 (c-forward-syntactic-ws)
8499 (setq placeholder (point)))
8500 (goto-char placeholder))
8501 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8502 ))
8503
8504 ;; (CASE 6 has been removed.)
8505
8506 ;; CASE 7: line is an expression, not a statement. Most
8507 ;; likely we are either in a function prototype or a function
8508 ;; call argument list
8509 ((not (or (and c-special-brace-lists
8510 (save-excursion
8511 (goto-char containing-sexp)
8512 (c-looking-at-special-brace-list)))
8513 (eq (char-after containing-sexp) ?{)))
8514 (cond
8515
8516 ;; CASE 7A: we are looking at the arglist closing paren.
8517 ;; C.f. case 7F.
8518 ((memq char-after-ip '(?\) ?\]))
8519 (goto-char containing-sexp)
8520 (setq placeholder (c-point 'boi))
8521 (if (and (c-safe (backward-up-list 1) t)
8522 (>= (point) placeholder))
8523 (progn
8524 (forward-char)
8525 (skip-chars-forward " \t"))
8526 (goto-char placeholder))
8527 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
8528 (c-most-enclosing-brace paren-state (point))
8529 paren-state))
8530
8531 ;; CASE 7B: Looking at the opening brace of an
8532 ;; in-expression block or brace list. C.f. cases 4, 16A
8533 ;; and 17E.
8534 ((and (eq char-after-ip ?{)
8535 (progn
8536 (setq placeholder (c-inside-bracelist-p (point)
8537 paren-state))
8538 (if placeholder
8539 (setq tmpsymbol '(brace-list-open . inexpr-class))
8540 (setq tmpsymbol '(block-open . inexpr-statement)
8541 placeholder
8542 (cdr-safe (c-looking-at-inexpr-block
8543 (c-safe-position containing-sexp
8544 paren-state)
8545 containing-sexp)))
8546 ;; placeholder is nil if it's a block directly in
8547 ;; a function arglist. That makes us skip out of
8548 ;; this case.
8549 )))
8550 (goto-char placeholder)
8551 (back-to-indentation)
8552 (c-add-stmt-syntax (car tmpsymbol) nil t
8553 (c-most-enclosing-brace paren-state (point))
8554 paren-state)
8555 (if (/= (point) placeholder)
8556 (c-add-syntax (cdr tmpsymbol))))
8557
8558 ;; CASE 7C: we are looking at the first argument in an empty
8559 ;; argument list. Use arglist-close if we're actually
8560 ;; looking at a close paren or bracket.
8561 ((memq char-before-ip '(?\( ?\[))
8562 (goto-char containing-sexp)
8563 (setq placeholder (c-point 'boi))
8564 (if (and (c-safe (backward-up-list 1) t)
8565 (>= (point) placeholder))
8566 (progn
8567 (forward-char)
8568 (skip-chars-forward " \t"))
8569 (goto-char placeholder))
8570 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
8571 (c-most-enclosing-brace paren-state (point))
8572 paren-state))
8573
8574 ;; CASE 7D: we are inside a conditional test clause. treat
8575 ;; these things as statements
8576 ((progn
8577 (goto-char containing-sexp)
8578 (and (c-safe (c-forward-sexp -1) t)
8579 (looking-at "\\<for\\>[^_]")))
8580 (goto-char (1+ containing-sexp))
8581 (c-forward-syntactic-ws indent-point)
8582 (if (eq char-before-ip ?\;)
8583 (c-add-syntax 'statement (point))
8584 (c-add-syntax 'statement-cont (point))
8585 ))
8586
8587 ;; CASE 7E: maybe a continued ObjC method call. This is the
8588 ;; case when we are inside a [] bracketed exp, and what
8589 ;; precede the opening bracket is not an identifier.
8590 ((and c-opt-method-key
8591 (eq (char-after containing-sexp) ?\[)
8592 (progn
8593 (goto-char (1- containing-sexp))
8594 (c-backward-syntactic-ws (c-point 'bod))
8595 (if (not (looking-at c-symbol-key))
8596 (c-add-syntax 'objc-method-call-cont containing-sexp))
8597 )))
8598
8599 ;; CASE 7F: we are looking at an arglist continuation line,
8600 ;; but the preceding argument is on the same line as the
8601 ;; opening paren. This case includes multi-line
8602 ;; mathematical paren groupings, but we could be on a
8603 ;; for-list continuation line. C.f. case 7A.
8604 ((progn
8605 (goto-char (1+ containing-sexp))
8606 (< (save-excursion
8607 (c-forward-syntactic-ws)
8608 (point))
8609 (c-point 'bonl)))
8610 (goto-char containing-sexp)
8611 (setq placeholder (c-point 'boi))
8612 (if (and (c-safe (backward-up-list 1) t)
8613 (>= (point) placeholder))
8614 (progn
8615 (forward-char)
8616 (skip-chars-forward " \t"))
8617 (goto-char placeholder))
8618 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
8619 (c-most-enclosing-brace c-state-cache (point))
8620 paren-state))
8621
8622 ;; CASE 7G: we are looking at just a normal arglist
8623 ;; continuation line
8624 (t (c-forward-syntactic-ws indent-point)
8625 (c-add-syntax 'arglist-cont (c-point 'boi)))
8626 ))
8627
8628 ;; CASE 8: func-local multi-inheritance line
8629 ((and (c-major-mode-is 'c++-mode)
8630 (save-excursion
8631 (goto-char indent-point)
8632 (skip-chars-forward " \t")
8633 (looking-at c-opt-postfix-decl-spec-key)))
8634 (goto-char indent-point)
8635 (skip-chars-forward " \t")
8636 (cond
8637
8638 ;; CASE 8A: non-hanging colon on an inher intro
8639 ((eq char-after-ip ?:)
8640 (c-backward-syntactic-ws lim)
8641 (c-add-syntax 'inher-intro (c-point 'boi)))
8642
8643 ;; CASE 8B: hanging colon on an inher intro
8644 ((eq char-before-ip ?:)
8645 (c-add-syntax 'inher-intro (c-point 'boi)))
8646
8647 ;; CASE 8C: a continued inheritance line
8648 (t
8649 (c-beginning-of-inheritance-list lim)
8650 (c-add-syntax 'inher-cont (point))
8651 )))
8652
8653 ;; CASE 9: we are inside a brace-list
8654 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
8655 (setq special-brace-list
8656 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
8657 (save-excursion
8658 (goto-char containing-sexp)
8659 (c-looking-at-special-brace-list)))
8660 (c-inside-bracelist-p containing-sexp paren-state))))
8661 (cond
8662
8663 ;; CASE 9A: In the middle of a special brace list opener.
8664 ((and (consp special-brace-list)
8665 (save-excursion
8666 (goto-char containing-sexp)
8667 (eq (char-after) ?\())
8668 (eq char-after-ip (car (cdr special-brace-list))))
8669 (goto-char (car (car special-brace-list)))
8670 (skip-chars-backward " \t")
8671 (if (and (bolp)
8672 (assoc 'statement-cont
8673 (setq placeholder (c-guess-basic-syntax))))
8674 (setq c-syntactic-context placeholder)
8675 (c-beginning-of-statement-1
8676 (c-safe-position (1- containing-sexp) paren-state))
8677 (c-forward-token-2 0)
8678 (while (looking-at c-specifier-key)
8679 (goto-char (match-end 1))
8680 (c-forward-syntactic-ws))
8681 (c-add-syntax 'brace-list-open (c-point 'boi))))
8682
8683 ;; CASE 9B: brace-list-close brace
8684 ((if (consp special-brace-list)
8685 ;; Check special brace list closer.
8686 (progn
8687 (goto-char (car (car special-brace-list)))
8688 (save-excursion
8689 (goto-char indent-point)
8690 (back-to-indentation)
8691 (or
8692 ;; We were between the special close char and the `)'.
8693 (and (eq (char-after) ?\))
8694 (eq (1+ (point)) (cdr (car special-brace-list))))
8695 ;; We were before the special close char.
8696 (and (eq (char-after) (cdr (cdr special-brace-list)))
8697 (zerop (c-forward-token-2))
8698 (eq (1+ (point)) (cdr (car special-brace-list)))))))
8699 ;; Normal brace list check.
8700 (and (eq char-after-ip ?})
8701 (c-safe (goto-char (c-up-list-backward (point))) t)
8702 (= (point) containing-sexp)))
8703 (if (eq (point) (c-point 'boi))
8704 (c-add-syntax 'brace-list-close (point))
8705 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8706 (c-beginning-of-statement-1 lim)
8707 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
8708
8709 (t
8710 ;; Prepare for the rest of the cases below by going to the
8711 ;; token following the opening brace
8712 (if (consp special-brace-list)
8713 (progn
8714 (goto-char (car (car special-brace-list)))
8715 (c-forward-token-2 1 nil indent-point))
8716 (goto-char containing-sexp))
8717 (forward-char)
8718 (let ((start (point)))
8719 (c-forward-syntactic-ws indent-point)
8720 (goto-char (max start (c-point 'bol))))
8721 (c-skip-ws-forward indent-point)
8722 (cond
8723
8724 ;; CASE 9C: we're looking at the first line in a brace-list
8725 ((= (point) indent-point)
8726 (if (consp special-brace-list)
8727 (goto-char (car (car special-brace-list)))
8728 (goto-char containing-sexp))
8729 (if (eq (point) (c-point 'boi))
8730 (c-add-syntax 'brace-list-intro (point))
8731 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8732 (c-beginning-of-statement-1 lim)
8733 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
8734
8735 ;; CASE 9D: this is just a later brace-list-entry or
8736 ;; brace-entry-open
8737 (t (if (or (eq char-after-ip ?{)
8738 (and c-special-brace-lists
8739 (save-excursion
8740 (goto-char indent-point)
8741 (c-forward-syntactic-ws (c-point 'eol))
8742 (c-looking-at-special-brace-list (point)))))
8743 (c-add-syntax 'brace-entry-open (point))
8744 (c-add-syntax 'brace-list-entry (point))
8745 ))
8746 ))))
8747
8748 ;; CASE 10: A continued statement or top level construct.
8749 ((and (not (memq char-before-ip '(?\; ?:)))
8750 (not (c-at-vsemi-p before-ws-ip))
8751 (or (not (eq char-before-ip ?}))
8752 (c-looking-at-inexpr-block-backward c-state-cache))
8753 (> (point)
8754 (save-excursion
8755 (c-beginning-of-statement-1 containing-sexp)
8756 (setq placeholder (point))))
8757 (/= placeholder containing-sexp))
8758 ;; This is shared with case 18.
8759 (c-guess-continued-construct indent-point
8760 char-after-ip
8761 placeholder
8762 containing-sexp
8763 paren-state))
8764
8765 ;; CASE 16: block close brace, possibly closing the defun or
8766 ;; the class
8767 ((eq char-after-ip ?})
8768 ;; From here on we have the next containing sexp in lim.
8769 (setq lim (c-most-enclosing-brace paren-state))
8770 (goto-char containing-sexp)
8771 (cond
8772
8773 ;; CASE 16E: Closing a statement block? This catches
8774 ;; cases where it's preceded by a statement keyword,
8775 ;; which works even when used in an "invalid" context,
8776 ;; e.g. a macro argument.
8777 ((c-after-conditional)
8778 (c-backward-to-block-anchor lim)
8779 (c-add-stmt-syntax 'block-close nil t lim paren-state))
8780
8781 ;; CASE 16A: closing a lambda defun or an in-expression
8782 ;; block? C.f. cases 4, 7B and 17E.
8783 ((setq placeholder (c-looking-at-inexpr-block
8784 (c-safe-position containing-sexp paren-state)
8785 nil))
8786 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8787 'inline-close
8788 'block-close))
8789 (goto-char containing-sexp)
8790 (back-to-indentation)
8791 (if (= containing-sexp (point))
8792 (c-add-syntax tmpsymbol (point))
8793 (goto-char (cdr placeholder))
8794 (back-to-indentation)
8795 (c-add-stmt-syntax tmpsymbol nil t
8796 (c-most-enclosing-brace paren-state (point))
8797 paren-state)
8798 (if (/= (point) (cdr placeholder))
8799 (c-add-syntax (car placeholder)))))
8800
8801 ;; CASE 16B: does this close an inline or a function in
8802 ;; a non-class declaration level block?
8803 ((save-excursion
8804 (and lim
8805 (progn
8806 (goto-char lim)
8807 (c-looking-at-decl-block
8808 (c-most-enclosing-brace paren-state lim)
8809 nil))
8810 (setq placeholder (point))))
8811 (c-backward-to-decl-anchor lim)
8812 (back-to-indentation)
8813 (if (save-excursion
8814 (goto-char placeholder)
8815 (looking-at c-other-decl-block-key))
8816 (c-add-syntax 'defun-close (point))
8817 (c-add-syntax 'inline-close (point))))
8818
8819 ;; CASE 16F: Can be a defun-close of a function declared
8820 ;; in a statement block, e.g. in Pike or when using gcc
8821 ;; extensions, but watch out for macros followed by
8822 ;; blocks. Let it through to be handled below.
8823 ;; C.f. cases B.3 and 17G.
8824 ((save-excursion
8825 (and (not (c-at-statement-start-p))
8826 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8827 (setq placeholder (point))
8828 (let ((c-recognize-typeless-decls nil))
8829 ;; Turn off recognition of constructs that
8830 ;; lacks a type in this case, since that's more
8831 ;; likely to be a macro followed by a block.
8832 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8833 (back-to-indentation)
8834 (if (/= (point) containing-sexp)
8835 (goto-char placeholder))
8836 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
8837
8838 ;; CASE 16C: If there is an enclosing brace then this is
8839 ;; a block close since defun closes inside declaration
8840 ;; level blocks have been handled above.
8841 (lim
8842 ;; If the block is preceded by a case/switch label on
8843 ;; the same line, we anchor at the first preceding label
8844 ;; at boi. The default handling in c-add-stmt-syntax
8845 ;; really fixes it better, but we do like this to keep
8846 ;; the indentation compatible with version 5.28 and
8847 ;; earlier. C.f. case 17H.
8848 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8849 (eq (c-beginning-of-statement-1 lim) 'label)))
8850 (goto-char placeholder)
8851 (if (looking-at c-label-kwds-regexp)
8852 (c-add-syntax 'block-close (point))
8853 (goto-char containing-sexp)
8854 ;; c-backward-to-block-anchor not necessary here; those
8855 ;; situations are handled in case 16E above.
8856 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
8857
8858 ;; CASE 16D: Only top level defun close left.
8859 (t
8860 (goto-char containing-sexp)
8861 (c-backward-to-decl-anchor lim)
8862 (c-add-stmt-syntax 'defun-close nil nil
8863 (c-most-enclosing-brace paren-state)
8864 paren-state))
8865 ))
8866
8867 ;; CASE 17: Statement or defun catchall.
8868 (t
8869 (goto-char indent-point)
8870 ;; Back up statements until we find one that starts at boi.
8871 (while (let* ((prev-point (point))
8872 (last-step-type (c-beginning-of-statement-1
8873 containing-sexp)))
8874 (if (= (point) prev-point)
8875 (progn
8876 (setq step-type (or step-type last-step-type))
8877 nil)
8878 (setq step-type last-step-type)
8879 (/= (point) (c-point 'boi)))))
8880 (cond
8881
8882 ;; CASE 17B: continued statement
8883 ((and (eq step-type 'same)
8884 (/= (point) indent-point))
8885 (c-add-stmt-syntax 'statement-cont nil nil
8886 containing-sexp paren-state))
8887
8888 ;; CASE 17A: After a case/default label?
8889 ((progn
8890 (while (and (eq step-type 'label)
8891 (not (looking-at c-label-kwds-regexp)))
8892 (setq step-type
8893 (c-beginning-of-statement-1 containing-sexp)))
8894 (eq step-type 'label))
8895 (c-add-stmt-syntax (if (eq char-after-ip ?{)
8896 'statement-case-open
8897 'statement-case-intro)
8898 nil t containing-sexp paren-state))
8899
8900 ;; CASE 17D: any old statement
8901 ((progn
8902 (while (eq step-type 'label)
8903 (setq step-type
8904 (c-beginning-of-statement-1 containing-sexp)))
8905 (eq step-type 'previous))
8906 (c-add-stmt-syntax 'statement nil t
8907 containing-sexp paren-state)
8908 (if (eq char-after-ip ?{)
8909 (c-add-syntax 'block-open)))
8910
8911 ;; CASE 17I: Inside a substatement block.
8912 ((progn
8913 ;; The following tests are all based on containing-sexp.
8914 (goto-char containing-sexp)
8915 ;; From here on we have the next containing sexp in lim.
8916 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
8917 (c-after-conditional))
8918 (c-backward-to-block-anchor lim)
8919 (c-add-stmt-syntax 'statement-block-intro nil t
8920 lim paren-state)
8921 (if (eq char-after-ip ?{)
8922 (c-add-syntax 'block-open)))
8923
8924 ;; CASE 17E: first statement in an in-expression block.
8925 ;; C.f. cases 4, 7B and 16A.
8926 ((setq placeholder (c-looking-at-inexpr-block
8927 (c-safe-position containing-sexp paren-state)
8928 nil))
8929 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8930 'defun-block-intro
8931 'statement-block-intro))
8932 (back-to-indentation)
8933 (if (= containing-sexp (point))
8934 (c-add-syntax tmpsymbol (point))
8935 (goto-char (cdr placeholder))
8936 (back-to-indentation)
8937 (c-add-stmt-syntax tmpsymbol nil t
8938 (c-most-enclosing-brace c-state-cache (point))
8939 paren-state)
8940 (if (/= (point) (cdr placeholder))
8941 (c-add-syntax (car placeholder))))
8942 (if (eq char-after-ip ?{)
8943 (c-add-syntax 'block-open)))
8944
8945 ;; CASE 17F: first statement in an inline, or first
8946 ;; statement in a top-level defun. we can tell this is it
8947 ;; if there are no enclosing braces that haven't been
8948 ;; narrowed out by a class (i.e. don't use bod here).
8949 ((save-excursion
8950 (or (not (setq placeholder (c-most-enclosing-brace
8951 paren-state)))
8952 (and (progn
8953 (goto-char placeholder)
8954 (eq (char-after) ?{))
8955 (c-looking-at-decl-block (c-most-enclosing-brace
8956 paren-state (point))
8957 nil))))
8958 (c-backward-to-decl-anchor lim)
8959 (back-to-indentation)
8960 (c-add-syntax 'defun-block-intro (point)))
8961
8962 ;; CASE 17G: First statement in a function declared inside
8963 ;; a normal block. This can occur in Pike and with
8964 ;; e.g. the gcc extensions, but watch out for macros
8965 ;; followed by blocks. C.f. cases B.3 and 16F.
8966 ((save-excursion
8967 (and (not (c-at-statement-start-p))
8968 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8969 (setq placeholder (point))
8970 (let ((c-recognize-typeless-decls nil))
8971 ;; Turn off recognition of constructs that lacks
8972 ;; a type in this case, since that's more likely
8973 ;; to be a macro followed by a block.
8974 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8975 (back-to-indentation)
8976 (if (/= (point) containing-sexp)
8977 (goto-char placeholder))
8978 (c-add-stmt-syntax 'defun-block-intro nil t
8979 lim paren-state))
8980
8981 ;; CASE 17H: First statement in a block.
8982 (t
8983 ;; If the block is preceded by a case/switch label on the
8984 ;; same line, we anchor at the first preceding label at
8985 ;; boi. The default handling in c-add-stmt-syntax is
8986 ;; really fixes it better, but we do like this to keep the
8987 ;; indentation compatible with version 5.28 and earlier.
8988 ;; C.f. case 16C.
8989 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8990 (eq (c-beginning-of-statement-1 lim) 'label)))
8991 (goto-char placeholder)
8992 (if (looking-at c-label-kwds-regexp)
8993 (c-add-syntax 'statement-block-intro (point))
8994 (goto-char containing-sexp)
8995 ;; c-backward-to-block-anchor not necessary here; those
8996 ;; situations are handled in case 17I above.
8997 (c-add-stmt-syntax 'statement-block-intro nil t
8998 lim paren-state))
8999 (if (eq char-after-ip ?{)
9000 (c-add-syntax 'block-open)))
9001 ))
9002 )
9003
9004 ;; now we need to look at any modifiers
9005 (goto-char indent-point)
9006 (skip-chars-forward " \t")
9007
9008 ;; are we looking at a comment only line?
9009 (when (and (looking-at c-comment-start-regexp)
9010 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
9011 (c-append-syntax 'comment-intro))
9012
9013 ;; we might want to give additional offset to friends (in C++).
9014 (when (and c-opt-friend-key
9015 (looking-at c-opt-friend-key))
9016 (c-append-syntax 'friend))
9017
9018 ;; Set syntactic-relpos.
9019 (let ((p c-syntactic-context))
9020 (while (and p
9021 (if (integerp (c-langelem-pos (car p)))
9022 (progn
9023 (setq syntactic-relpos (c-langelem-pos (car p)))
9024 nil)
9025 t))
9026 (setq p (cdr p))))
9027
9028 ;; Start of or a continuation of a preprocessor directive?
9029 (if (and macro-start
9030 (eq macro-start (c-point 'boi))
9031 (not (and (c-major-mode-is 'pike-mode)
9032 (eq (char-after (1+ macro-start)) ?\"))))
9033 (c-append-syntax 'cpp-macro)
9034 (when (and c-syntactic-indentation-in-macros macro-start)
9035 (if in-macro-expr
9036 (when (or
9037 (< syntactic-relpos macro-start)
9038 (not (or
9039 (assq 'arglist-intro c-syntactic-context)
9040 (assq 'arglist-cont c-syntactic-context)
9041 (assq 'arglist-cont-nonempty c-syntactic-context)
9042 (assq 'arglist-close c-syntactic-context))))
9043 ;; If inside a cpp expression, i.e. anywhere in a
9044 ;; cpp directive except a #define body, we only let
9045 ;; through the syntactic analysis that is internal
9046 ;; in the expression. That means the arglist
9047 ;; elements, if they are anchored inside the cpp
9048 ;; expression.
9049 (setq c-syntactic-context nil)
9050 (c-add-syntax 'cpp-macro-cont macro-start))
9051 (when (and (eq macro-start syntactic-relpos)
9052 (not (assq 'cpp-define-intro c-syntactic-context))
9053 (save-excursion
9054 (goto-char macro-start)
9055 (or (not (c-forward-to-cpp-define-body))
9056 (<= (point) (c-point 'boi indent-point)))))
9057 ;; Inside a #define body and the syntactic analysis is
9058 ;; anchored on the start of the #define. In this case
9059 ;; we add cpp-define-intro to get the extra
9060 ;; indentation of the #define body.
9061 (c-add-syntax 'cpp-define-intro)))))
9062
9063 ;; return the syntax
9064 c-syntactic-context)))
9065
9066 \f
9067 ;; Indentation calculation.
9068
9069 (defun c-evaluate-offset (offset langelem symbol)
9070 ;; offset can be a number, a function, a variable, a list, or one of
9071 ;; the symbols + or -
9072 ;;
9073 ;; This function might do hidden buffer changes.
9074 (let ((res
9075 (cond
9076 ((numberp offset) offset)
9077 ((vectorp offset) offset)
9078 ((null offset) nil)
9079
9080 ((eq offset '+) c-basic-offset)
9081 ((eq offset '-) (- c-basic-offset))
9082 ((eq offset '++) (* 2 c-basic-offset))
9083 ((eq offset '--) (* 2 (- c-basic-offset)))
9084 ((eq offset '*) (/ c-basic-offset 2))
9085 ((eq offset '/) (/ (- c-basic-offset) 2))
9086
9087 ((functionp offset)
9088 (c-evaluate-offset
9089 (funcall offset
9090 (cons (c-langelem-sym langelem)
9091 (c-langelem-pos langelem)))
9092 langelem symbol))
9093
9094 ((listp offset)
9095 (cond
9096 ((eq (car offset) 'quote)
9097 (c-benign-error "The offset %S for %s was mistakenly quoted"
9098 offset symbol)
9099 nil)
9100
9101 ((memq (car offset) '(min max))
9102 (let (res val (method (car offset)))
9103 (setq offset (cdr offset))
9104 (while offset
9105 (setq val (c-evaluate-offset (car offset) langelem symbol))
9106 (cond
9107 ((not val))
9108 ((not res)
9109 (setq res val))
9110 ((integerp val)
9111 (if (vectorp res)
9112 (c-benign-error "\
9113 Error evaluating offset %S for %s: \
9114 Cannot combine absolute offset %S with relative %S in `%s' method"
9115 (car offset) symbol res val method)
9116 (setq res (funcall method res val))))
9117 (t
9118 (if (integerp res)
9119 (c-benign-error "\
9120 Error evaluating offset %S for %s: \
9121 Cannot combine relative offset %S with absolute %S in `%s' method"
9122 (car offset) symbol res val method)
9123 (setq res (vector (funcall method (aref res 0)
9124 (aref val 0)))))))
9125 (setq offset (cdr offset)))
9126 res))
9127
9128 ((eq (car offset) 'add)
9129 (let (res val)
9130 (setq offset (cdr offset))
9131 (while offset
9132 (setq val (c-evaluate-offset (car offset) langelem symbol))
9133 (cond
9134 ((not val))
9135 ((not res)
9136 (setq res val))
9137 ((integerp val)
9138 (if (vectorp res)
9139 (setq res (vector (+ (aref res 0) val)))
9140 (setq res (+ res val))))
9141 (t
9142 (if (vectorp res)
9143 (c-benign-error "\
9144 Error evaluating offset %S for %s: \
9145 Cannot combine absolute offsets %S and %S in `add' method"
9146 (car offset) symbol res val)
9147 (setq res val)))) ; Override.
9148 (setq offset (cdr offset)))
9149 res))
9150
9151 (t
9152 (let (res)
9153 (when (eq (car offset) 'first)
9154 (setq offset (cdr offset)))
9155 (while (and (not res) offset)
9156 (setq res (c-evaluate-offset (car offset) langelem symbol)
9157 offset (cdr offset)))
9158 res))))
9159
9160 ((and (symbolp offset) (boundp offset))
9161 (symbol-value offset))
9162
9163 (t
9164 (c-benign-error "Unknown offset format %S for %s" offset symbol)
9165 nil))))
9166
9167 (if (or (null res) (integerp res)
9168 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
9169 res
9170 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
9171 offset symbol res)
9172 nil)))
9173
9174 (defun c-calc-offset (langelem)
9175 ;; Get offset from LANGELEM which is a list beginning with the
9176 ;; syntactic symbol and followed by any analysis data it provides.
9177 ;; That data may be zero or more elements, but if at least one is
9178 ;; given then the first is the anchor position (or nil). The symbol
9179 ;; is matched against `c-offsets-alist' and the offset calculated
9180 ;; from that is returned.
9181 ;;
9182 ;; This function might do hidden buffer changes.
9183 (let* ((symbol (c-langelem-sym langelem))
9184 (match (assq symbol c-offsets-alist))
9185 (offset (cdr-safe match)))
9186 (if match
9187 (setq offset (c-evaluate-offset offset langelem symbol))
9188 (if c-strict-syntax-p
9189 (c-benign-error "No offset found for syntactic symbol %s" symbol))
9190 (setq offset 0))
9191 (if (vectorp offset)
9192 offset
9193 (or (and (numberp offset) offset)
9194 (and (symbolp offset) (symbol-value offset))
9195 0))
9196 ))
9197
9198 (defun c-get-offset (langelem)
9199 ;; This is a compatibility wrapper for `c-calc-offset' in case
9200 ;; someone is calling it directly. It takes an old style syntactic
9201 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
9202 ;; new list form.
9203 ;;
9204 ;; This function might do hidden buffer changes.
9205 (if (c-langelem-pos langelem)
9206 (c-calc-offset (list (c-langelem-sym langelem)
9207 (c-langelem-pos langelem)))
9208 (c-calc-offset langelem)))
9209
9210 (defun c-get-syntactic-indentation (langelems)
9211 ;; Calculate the syntactic indentation from a syntactic description
9212 ;; as returned by `c-guess-syntax'.
9213 ;;
9214 ;; Note that topmost-intro always has an anchor position at bol, for
9215 ;; historical reasons. It's often used together with other symbols
9216 ;; that has more sane positions. Since we always use the first
9217 ;; found anchor position, we rely on that these other symbols always
9218 ;; precede topmost-intro in the LANGELEMS list.
9219 ;;
9220 ;; This function might do hidden buffer changes.
9221 (let ((indent 0) anchor)
9222
9223 (while langelems
9224 (let* ((c-syntactic-element (car langelems))
9225 (res (c-calc-offset c-syntactic-element)))
9226
9227 (if (vectorp res)
9228 ;; Got an absolute column that overrides any indentation
9229 ;; we've collected so far, but not the relative
9230 ;; indentation we might get for the nested structures
9231 ;; further down the langelems list.
9232 (setq indent (elt res 0)
9233 anchor (point-min)) ; A position at column 0.
9234
9235 ;; Got a relative change of the current calculated
9236 ;; indentation.
9237 (setq indent (+ indent res))
9238
9239 ;; Use the anchor position from the first syntactic
9240 ;; element with one.
9241 (unless anchor
9242 (setq anchor (c-langelem-pos (car langelems)))))
9243
9244 (setq langelems (cdr langelems))))
9245
9246 (if anchor
9247 (+ indent (save-excursion
9248 (goto-char anchor)
9249 (current-column)))
9250 indent)))
9251
9252 \f
9253 (cc-provide 'cc-engine)
9254
9255 ;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
9256 ;;; cc-engine.el ends here