Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
6
7 ;; Authors: 2001- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs and Stewart Clamen
11 ;; 1985 Richard M. Stallman
12 ;; Maintainer: bug-cc-mode@gnu.org
13 ;; Created: 22-Apr-1997 (split from cc-mode.el)
14 ;; Version: See cc-mode.el
15 ;; Keywords: c languages oop
16
17 ;; This file is part of GNU Emacs.
18
19 ;; GNU Emacs is free software: you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation, either version 3 of the License, or
22 ;; (at your option) any later version.
23
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
28
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31
32 ;;; Commentary:
33
34 ;; The functions which have docstring documentation can be considered
35 ;; part of an API which other packages can use in CC Mode buffers.
36 ;; Otoh, undocumented functions and functions with the documentation
37 ;; in comments are considered purely internal and can change semantics
38 ;; or even disappear in the future.
39 ;;
40 ;; (This policy applies to CC Mode as a whole, not just this file. It
41 ;; probably also applies to many other Emacs packages, but here it's
42 ;; clearly spelled out.)
43
44 ;; Hidden buffer changes
45 ;;
46 ;; Various functions in CC Mode use text properties for caching and
47 ;; syntactic markup purposes, and those of them that might modify such
48 ;; properties but still don't modify the buffer in a visible way are
49 ;; said to do "hidden buffer changes". They should be used within
50 ;; `c-save-buffer-state' or a similar function that saves and restores
51 ;; buffer modifiedness, disables buffer change hooks, etc.
52 ;;
53 ;; Interactive functions are assumed to not do hidden buffer changes,
54 ;; except in the specific parts of them that do real changes.
55 ;;
56 ;; Lineup functions are assumed to do hidden buffer changes. They
57 ;; must not do real changes, though.
58 ;;
59 ;; All other functions that do hidden buffer changes have that noted
60 ;; in their doc string or comment.
61 ;;
62 ;; The intention with this system is to avoid wrapping every leaf
63 ;; function that do hidden buffer changes inside
64 ;; `c-save-buffer-state'. It should be used as near the top of the
65 ;; interactive functions as possible.
66 ;;
67 ;; Functions called during font locking are allowed to do hidden
68 ;; buffer changes since the font-lock package run them in a context
69 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
70 ;; inspired by `save-buffer-state' in the font-lock package).
71
72 ;; Use of text properties
73 ;;
74 ;; CC Mode uses several text properties internally to mark up various
75 ;; positions, e.g. to improve speed and to eliminate glitches in
76 ;; interactive refontification.
77 ;;
78 ;; Note: This doc is for internal use only. Other packages should not
79 ;; assume that these text properties are used as described here.
80 ;;
81 ;; 'syntax-table
82 ;; Used to modify the syntax of some characters. It is used to
83 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
84 ;; to "hide" obtrusive characters in preprocessor lines.
85 ;;
86 ;; This property is used on single characters and is therefore
87 ;; always treated as front and rear nonsticky (or start and end open
88 ;; in XEmacs vocabulary). It's therefore installed on
89 ;; `text-property-default-nonsticky' if that variable exists (Emacs
90 ;; >= 21).
91 ;;
92 ;; 'c-is-sws and 'c-in-sws
93 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
94 ;; speed them up. See the comment blurb before `c-put-is-sws'
95 ;; below for further details.
96 ;;
97 ;; 'c-type
98 ;; This property is used on single characters to mark positions with
99 ;; special syntactic relevance of various sorts. Its primary use is
100 ;; to avoid glitches when multiline constructs are refontified
101 ;; interactively (on font lock decoration level 3). It's cleared in
102 ;; a region before it's fontified and is then put on relevant chars
103 ;; in that region as they are encountered during the fontification.
104 ;; The value specifies the kind of position:
105 ;;
106 ;; 'c-decl-arg-start
107 ;; Put on the last char of the token preceding each declaration
108 ;; inside a declaration style arglist (typically in a function
109 ;; prototype).
110 ;;
111 ;; 'c-decl-end
112 ;; Put on the last char of the token preceding a declaration.
113 ;; This is used in cases where declaration boundaries can't be
114 ;; recognized simply by looking for a token like ";" or "}".
115 ;; `c-type-decl-end-used' must be set if this is used (see also
116 ;; `c-find-decl-spots').
117 ;;
118 ;; 'c-<>-arg-sep
119 ;; Put on the commas that separate arguments in angle bracket
120 ;; arglists like C++ template arglists.
121 ;;
122 ;; 'c-decl-id-start and 'c-decl-type-start
123 ;; Put on the last char of the token preceding each declarator
124 ;; in the declarator list of a declaration. They are also used
125 ;; between the identifiers cases like enum declarations.
126 ;; 'c-decl-type-start is used when the declarators are types,
127 ;; 'c-decl-id-start otherwise.
128 ;;
129 ;; 'c-awk-NL-prop
130 ;; Used in AWK mode to mark the various kinds of newlines. See
131 ;; cc-awk.el.
132
133 ;;; Code:
134
135 (eval-when-compile
136 (let ((load-path
137 (if (and (boundp 'byte-compile-dest-file)
138 (stringp byte-compile-dest-file))
139 (cons (file-name-directory byte-compile-dest-file) load-path)
140 load-path)))
141 (load "cc-bytecomp" nil t)))
142
143 (cc-require 'cc-defs)
144 (cc-require-when-compile 'cc-langs)
145 (cc-require 'cc-vars)
146
147 ;; Silence the compiler.
148 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
149
150 \f
151 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
152
153 (defmacro c-declare-lang-variables ()
154 `(progn
155 ,@(apply 'nconc
156 (mapcar (lambda (init)
157 `(,(if (elt init 2)
158 `(defvar ,(car init) nil ,(elt init 2))
159 `(defvar ,(car init) nil))
160 (make-variable-buffer-local ',(car init))))
161 (cdr c-lang-variable-inits)))))
162 (c-declare-lang-variables)
163
164 \f
165 ;;; Internal state variables.
166
167 ;; Internal state of hungry delete key feature
168 (defvar c-hungry-delete-key nil)
169 (make-variable-buffer-local 'c-hungry-delete-key)
170
171 ;; The electric flag (toggled by `c-toggle-electric-state').
172 ;; If t, electric actions (like automatic reindentation, and (if
173 ;; c-auto-newline is also set) auto newlining) will happen when an electric
174 ;; key like `{' is pressed (or an electric keyword like `else').
175 (defvar c-electric-flag t)
176 (make-variable-buffer-local 'c-electric-flag)
177
178 ;; Internal state of auto newline feature.
179 (defvar c-auto-newline nil)
180 (make-variable-buffer-local 'c-auto-newline)
181
182 ;; Included in the mode line to indicate the active submodes.
183 ;; (defvar c-submode-indicators nil)
184 ;; (make-variable-buffer-local 'c-submode-indicators)
185
186 (defun c-calculate-state (arg prevstate)
187 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
188 ;; arg is nil or zero, toggle the state. If arg is negative, turn
189 ;; the state off, and if arg is positive, turn the state on
190 (if (or (not arg)
191 (zerop (setq arg (prefix-numeric-value arg))))
192 (not prevstate)
193 (> arg 0)))
194
195 ;; Dynamically bound cache for `c-in-literal'.
196 (defvar c-in-literal-cache t)
197
198 \f
199 ;; Basic handling of preprocessor directives.
200
201 ;; This is a dynamically bound cache used together with
202 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
203 ;; works as long as point doesn't cross a macro boundary.
204 (defvar c-macro-start 'unknown)
205
206 (defsubst c-query-and-set-macro-start ()
207 (if (symbolp c-macro-start)
208 (setq c-macro-start (save-excursion
209 (c-save-buffer-state ()
210 (and (c-beginning-of-macro)
211 (point)))))
212 c-macro-start))
213
214 (defsubst c-query-macro-start ()
215 (if (symbolp c-macro-start)
216 (save-excursion
217 (c-save-buffer-state ()
218 (and (c-beginning-of-macro)
219 (point))))
220 c-macro-start))
221
222 (defun c-beginning-of-macro (&optional lim)
223 "Go to the beginning of a preprocessor directive.
224 Leave point at the beginning of the directive and return t if in one,
225 otherwise return nil and leave point unchanged.
226
227 Note that this function might do hidden buffer changes. See the
228 comment at the start of cc-engine.el for more info."
229 (when c-opt-cpp-prefix
230 (let ((here (point)))
231 (save-restriction
232 (if lim (narrow-to-region lim (point-max)))
233 (beginning-of-line)
234 (while (eq (char-before (1- (point))) ?\\)
235 (forward-line -1))
236 (back-to-indentation)
237 (if (and (<= (point) here)
238 (looking-at c-opt-cpp-start))
239 t
240 (goto-char here)
241 nil)))))
242
243 (defun c-end-of-macro ()
244 "Go to the end of a preprocessor directive.
245 More accurately, move the point to the end of the closest following
246 line that doesn't end with a line continuation backslash - no check is
247 done that the point is inside a cpp directive to begin with.
248
249 Note that this function might do hidden buffer changes. See the
250 comment at the start of cc-engine.el for more info."
251 (while (progn
252 (end-of-line)
253 (when (and (eq (char-before) ?\\)
254 (not (eobp)))
255 (forward-char)
256 t))))
257
258 (defun c-forward-over-cpp-define-id ()
259 ;; Assuming point is at the "#" that introduces a preprocessor
260 ;; directive, it's moved forward to the end of the identifier which is
261 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
262 ;; is returned in this case, in all other cases nil is returned and
263 ;; point isn't moved.
264 ;;
265 ;; This function might do hidden buffer changes.
266 (when (and c-opt-cpp-macro-define-id
267 (looking-at c-opt-cpp-macro-define-id))
268 (goto-char (match-end 0))))
269
270 (defun c-forward-to-cpp-define-body ()
271 ;; Assuming point is at the "#" that introduces a preprocessor
272 ;; directive, it's moved forward to the start of the definition body
273 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
274 ;; specifies). Non-nil is returned in this case, in all other cases
275 ;; nil is returned and point isn't moved.
276 ;;
277 ;; This function might do hidden buffer changes.
278 (when (and c-opt-cpp-macro-define-start
279 (looking-at c-opt-cpp-macro-define-start)
280 (not (= (match-end 0) (c-point 'eol))))
281 (goto-char (match-end 0))))
282
283 \f
284 ;;; Basic utility functions.
285
286 (defun c-syntactic-content (from to paren-level)
287 ;; Return the given region as a string where all syntactic
288 ;; whitespace is removed or, where necessary, replaced with a single
289 ;; space. If PAREN-LEVEL is given then all parens in the region are
290 ;; collapsed to "()", "[]" etc.
291 ;;
292 ;; This function might do hidden buffer changes.
293
294 (save-excursion
295 (save-restriction
296 (narrow-to-region from to)
297 (goto-char from)
298 (let* ((parts (list nil)) (tail parts) pos in-paren)
299
300 (while (re-search-forward c-syntactic-ws-start to t)
301 (goto-char (setq pos (match-beginning 0)))
302 (c-forward-syntactic-ws)
303 (if (= (point) pos)
304 (forward-char)
305
306 (when paren-level
307 (save-excursion
308 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
309 pos (point))))
310
311 (if (and (> pos from)
312 (< (point) to)
313 (looking-at "\\w\\|\\s_")
314 (save-excursion
315 (goto-char (1- pos))
316 (looking-at "\\w\\|\\s_")))
317 (progn
318 (setcdr tail (list (buffer-substring-no-properties from pos)
319 " "))
320 (setq tail (cddr tail)))
321 (setcdr tail (list (buffer-substring-no-properties from pos)))
322 (setq tail (cdr tail)))
323
324 (when in-paren
325 (when (= (car (parse-partial-sexp pos to -1)) -1)
326 (setcdr tail (list (buffer-substring-no-properties
327 (1- (point)) (point))))
328 (setq tail (cdr tail))))
329
330 (setq from (point))))
331
332 (setcdr tail (list (buffer-substring-no-properties from to)))
333 (apply 'concat (cdr parts))))))
334
335 (defun c-shift-line-indentation (shift-amt)
336 ;; Shift the indentation of the current line with the specified
337 ;; amount (positive inwards). The buffer is modified only if
338 ;; SHIFT-AMT isn't equal to zero.
339 (let ((pos (- (point-max) (point)))
340 (c-macro-start c-macro-start)
341 tmp-char-inserted)
342 (if (zerop shift-amt)
343 nil
344 ;; If we're on an empty line inside a macro, we take the point
345 ;; to be at the current indentation and shift it to the
346 ;; appropriate column. This way we don't treat the extra
347 ;; whitespace out to the line continuation as indentation.
348 (when (and (c-query-and-set-macro-start)
349 (looking-at "[ \t]*\\\\$")
350 (save-excursion
351 (skip-chars-backward " \t")
352 (bolp)))
353 (insert ?x)
354 (backward-char)
355 (setq tmp-char-inserted t))
356 (unwind-protect
357 (let ((col (current-indentation)))
358 (delete-region (c-point 'bol) (c-point 'boi))
359 (beginning-of-line)
360 (indent-to (+ col shift-amt)))
361 (when tmp-char-inserted
362 (delete-char 1))))
363 ;; If initial point was within line's indentation and we're not on
364 ;; a line with a line continuation in a macro, position after the
365 ;; indentation. Else stay at same point in text.
366 (if (and (< (point) (c-point 'boi))
367 (not tmp-char-inserted))
368 (back-to-indentation)
369 (if (> (- (point-max) pos) (point))
370 (goto-char (- (point-max) pos))))))
371
372 (defsubst c-keyword-sym (keyword)
373 ;; Return non-nil if the string KEYWORD is a known keyword. More
374 ;; precisely, the value is the symbol for the keyword in
375 ;; `c-keywords-obarray'.
376 (intern-soft keyword c-keywords-obarray))
377
378 (defsubst c-keyword-member (keyword-sym lang-constant)
379 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
380 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
381 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
382 ;; nil then the result is nil.
383 (get keyword-sym lang-constant))
384
385 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
386 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
387 "\"|"
388 "\""))
389
390 ;; Regexp matching string limit syntax.
391 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
392 "\\s\"\\|\\s|"
393 "\\s\""))
394
395 ;; Regexp matching WS followed by string limit syntax.
396 (defconst c-ws*-string-limit-regexp
397 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
398
399 ;; Holds formatted error strings for the few cases where parse errors
400 ;; are reported.
401 (defvar c-parsing-error nil)
402 (make-variable-buffer-local 'c-parsing-error)
403
404 (defun c-echo-parsing-error (&optional quiet)
405 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
406 (c-benign-error "%s" c-parsing-error))
407 c-parsing-error)
408
409 ;; Faces given to comments and string literals. This is used in some
410 ;; situations to speed up recognition; it isn't mandatory that font
411 ;; locking is in use. This variable is extended with the face in
412 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
413 (defvar c-literal-faces
414 (append '(font-lock-comment-face font-lock-string-face)
415 (when (facep 'font-lock-comment-delimiter-face)
416 ;; New in Emacs 22.
417 '(font-lock-comment-delimiter-face))))
418
419 (defsubst c-put-c-type-property (pos value)
420 ;; Put a c-type property with the given value at POS.
421 (c-put-char-property pos 'c-type value))
422
423 (defun c-clear-c-type-property (from to value)
424 ;; Remove all occurences of the c-type property that has the given
425 ;; value in the region between FROM and TO. VALUE is assumed to not
426 ;; be nil.
427 ;;
428 ;; Note: This assumes that c-type is put on single chars only; it's
429 ;; very inefficient if matching properties cover large regions.
430 (save-excursion
431 (goto-char from)
432 (while (progn
433 (when (eq (get-text-property (point) 'c-type) value)
434 (c-clear-char-property (point) 'c-type))
435 (goto-char (next-single-property-change (point) 'c-type nil to))
436 (< (point) to)))))
437
438 \f
439 ;; Some debug tools to visualize various special positions. This
440 ;; debug code isn't as portable as the rest of CC Mode.
441
442 (cc-bytecomp-defun overlays-in)
443 (cc-bytecomp-defun overlay-get)
444 (cc-bytecomp-defun overlay-start)
445 (cc-bytecomp-defun overlay-end)
446 (cc-bytecomp-defun delete-overlay)
447 (cc-bytecomp-defun overlay-put)
448 (cc-bytecomp-defun make-overlay)
449
450 (defun c-debug-add-face (beg end face)
451 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
452 (while overlays
453 (setq overlay (car overlays)
454 overlays (cdr overlays))
455 (when (eq (overlay-get overlay 'face) face)
456 (setq beg (min beg (overlay-start overlay))
457 end (max end (overlay-end overlay)))
458 (delete-overlay overlay)))
459 (overlay-put (make-overlay beg end) 'face face)))
460
461 (defun c-debug-remove-face (beg end face)
462 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
463 (ol-beg beg) (ol-end end))
464 (while overlays
465 (setq overlay (car overlays)
466 overlays (cdr overlays))
467 (when (eq (overlay-get overlay 'face) face)
468 (setq ol-beg (min ol-beg (overlay-start overlay))
469 ol-end (max ol-end (overlay-end overlay)))
470 (delete-overlay overlay)))
471 (when (< ol-beg beg)
472 (overlay-put (make-overlay ol-beg beg) 'face face))
473 (when (> ol-end end)
474 (overlay-put (make-overlay end ol-end) 'face face))))
475
476 \f
477 ;; `c-beginning-of-statement-1' and accompanying stuff.
478
479 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
480 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
481 ;; better way should be implemented, but this will at least shut up
482 ;; the byte compiler.
483 (defvar c-maybe-labelp)
484
485 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
486
487 ;; Macros used internally in c-beginning-of-statement-1 for the
488 ;; automaton actions.
489 (defmacro c-bos-push-state ()
490 '(setq stack (cons (cons state saved-pos)
491 stack)))
492 (defmacro c-bos-pop-state (&optional do-if-done)
493 `(if (setq state (car (car stack))
494 saved-pos (cdr (car stack))
495 stack (cdr stack))
496 t
497 ,do-if-done
498 (throw 'loop nil)))
499 (defmacro c-bos-pop-state-and-retry ()
500 '(throw 'loop (setq state (car (car stack))
501 saved-pos (cdr (car stack))
502 ;; Throw nil if stack is empty, else throw non-nil.
503 stack (cdr stack))))
504 (defmacro c-bos-save-pos ()
505 '(setq saved-pos (vector pos tok ptok pptok)))
506 (defmacro c-bos-restore-pos ()
507 '(unless (eq (elt saved-pos 0) start)
508 (setq pos (elt saved-pos 0)
509 tok (elt saved-pos 1)
510 ptok (elt saved-pos 2)
511 pptok (elt saved-pos 3))
512 (goto-char pos)
513 (setq sym nil)))
514 (defmacro c-bos-save-error-info (missing got)
515 `(setq saved-pos (vector pos ,missing ,got)))
516 (defmacro c-bos-report-error ()
517 '(unless noerror
518 (setq c-parsing-error
519 (format "No matching `%s' found for `%s' on line %d"
520 (elt saved-pos 1)
521 (elt saved-pos 2)
522 (1+ (count-lines (point-min)
523 (c-point 'bol (elt saved-pos 0))))))))
524
525 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
526 noerror comma-delim)
527 "Move to the start of the current statement or declaration, or to
528 the previous one if already at the beginning of one. Only
529 statements/declarations on the same level are considered, i.e. don't
530 move into or out of sexps (not even normal expression parentheses).
531
532 Stop at statement continuation tokens like \"else\", \"catch\",
533 \"finally\" and the \"while\" in \"do ... while\" if the start point
534 is within the continuation. If starting at such a token, move to the
535 corresponding statement start. If at the beginning of a statement,
536 move to the closest containing statement if there is any. This might
537 also stop at a continuation clause.
538
539 Labels are treated as part of the following statements if
540 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
541 statement start keyword.) Otherwise, each label is treated as a
542 separate statement.
543
544 Macros are ignored \(i.e. skipped over) unless point is within one, in
545 which case the content of the macro is treated as normal code. Aside
546 from any normal statement starts found in it, stop at the first token
547 of the content in the macro, i.e. the expression of an \"#if\" or the
548 start of the definition in a \"#define\". Also stop at start of
549 macros before leaving them.
550
551 Return 'label if stopped at a label, 'same if stopped at the beginning
552 of the current statement, 'up if stepped to a containing statement,
553 'previous if stepped to a preceding statement, 'beginning if stepped
554 from a statement continuation clause to its start clause, or 'macro if
555 stepped to a macro start. Note that 'same and not 'label is returned
556 if stopped at the same label without crossing the colon character.
557
558 LIM may be given to limit the search. If the search hits the limit,
559 point will be left at the closest following token, or at the start
560 position if that is less ('same is returned in this case).
561
562 NOERROR turns off error logging to `c-parsing-error'.
563
564 Normally only ';' and virtual semicolons are considered to delimit
565 statements, but if COMMA-DELIM is non-nil then ',' is treated
566 as a delimiter too.
567
568 Note that this function might do hidden buffer changes. See the
569 comment at the start of cc-engine.el for more info."
570
571 ;; The bulk of this function is a pushdown automaton that looks at statement
572 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
573 ;; purpose is to keep track of nested statements, ensuring that such
574 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
575 ;; does with nested braces/brackets/parentheses).
576 ;;
577 ;; Note: The position of a boundary is the following token.
578 ;;
579 ;; Beginning with the current token (the one following point), move back one
580 ;; sexp at a time (where a sexp is, more or less, either a token or the
581 ;; entire contents of a brace/bracket/paren pair). Each time a statement
582 ;; boundary is crossed or a "while"-like token is found, update the state of
583 ;; the PDA. Stop at the beginning of a statement when the stack (holding
584 ;; nested statement info) is empty and the position has been moved.
585 ;;
586 ;; The following variables constitute the PDA:
587 ;;
588 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
589 ;; scanned back over, 'boundary if we've just gone back over a
590 ;; statement boundary, or nil otherwise.
591 ;; state: takes one of the values (nil else else-boundary while
592 ;; while-boundary catch catch-boundary).
593 ;; nil means "no "while"-like token yet scanned".
594 ;; 'else, for example, means "just gone back over an else".
595 ;; 'else-boundary means "just gone back over a statement boundary
596 ;; immediately after having gone back over an else".
597 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
598 ;; of error reporting information.
599 ;; stack: The stack onto which the PDA pushes its state. Each entry
600 ;; consists of a saved value of state and saved-pos. An entry is
601 ;; pushed when we move back over a "continuation" token (e.g. else)
602 ;; and popped when we encounter the corresponding opening token
603 ;; (e.g. if).
604 ;;
605 ;;
606 ;; The following diagram briefly outlines the PDA.
607 ;;
608 ;; Common state:
609 ;; "else": Push state, goto state `else'.
610 ;; "while": Push state, goto state `while'.
611 ;; "catch" or "finally": Push state, goto state `catch'.
612 ;; boundary: Pop state.
613 ;; other: Do nothing special.
614 ;;
615 ;; State `else':
616 ;; boundary: Goto state `else-boundary'.
617 ;; other: Error, pop state, retry token.
618 ;;
619 ;; State `else-boundary':
620 ;; "if": Pop state.
621 ;; boundary: Error, pop state.
622 ;; other: See common state.
623 ;;
624 ;; State `while':
625 ;; boundary: Save position, goto state `while-boundary'.
626 ;; other: Pop state, retry token.
627 ;;
628 ;; State `while-boundary':
629 ;; "do": Pop state.
630 ;; boundary: Restore position if it's not at start, pop state. [*see below]
631 ;; other: See common state.
632 ;;
633 ;; State `catch':
634 ;; boundary: Goto state `catch-boundary'.
635 ;; other: Error, pop state, retry token.
636 ;;
637 ;; State `catch-boundary':
638 ;; "try": Pop state.
639 ;; "catch": Goto state `catch'.
640 ;; boundary: Error, pop state.
641 ;; other: See common state.
642 ;;
643 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
644 ;; searching for a "do" which would have opened a do-while. If we didn't
645 ;; find it, we discard the analysis done since the "while", go back to this
646 ;; token in the buffer and restart the scanning there, this time WITHOUT
647 ;; pushing the 'while state onto the stack.
648 ;;
649 ;; In addition to the above there is some special handling of labels
650 ;; and macros.
651
652 (let ((case-fold-search nil)
653 (start (point))
654 macro-start
655 (delims (if comma-delim '(?\; ?,) '(?\;)))
656 (c-stmt-delim-chars (if comma-delim
657 c-stmt-delim-chars-with-comma
658 c-stmt-delim-chars))
659 c-in-literal-cache c-maybe-labelp saved
660 ;; Current position.
661 pos
662 ;; Position of last stmt boundary character (e.g. ;).
663 boundary-pos
664 ;; The position of the last sexp or bound that follows the
665 ;; first found colon, i.e. the start of the nonlabel part of
666 ;; the statement. It's `start' if a colon is found just after
667 ;; the start.
668 after-labels-pos
669 ;; Like `after-labels-pos', but the first such position inside
670 ;; a label, i.e. the start of the last label before the start
671 ;; of the nonlabel part of the statement.
672 last-label-pos
673 ;; The last position where a label is possible provided the
674 ;; statement started there. It's nil as long as no invalid
675 ;; label content has been found (according to
676 ;; `c-nonlabel-token-key'. It's `start' if no valid label
677 ;; content was found in the label. Note that we might still
678 ;; regard it a label if it starts with `c-label-kwds'.
679 label-good-pos
680 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
681 ;; See above.
682 sym
683 ;; Current state in the automaton. See above.
684 state
685 ;; Current saved positions. See above.
686 saved-pos
687 ;; Stack of conses (state . saved-pos).
688 stack
689 ;; Regexp which matches "for", "if", etc.
690 (cond-key (or c-opt-block-stmt-key
691 "\\<\\>")) ; Matches nothing.
692 ;; Return value.
693 (ret 'same)
694 ;; Positions of the last three sexps or bounds we've stopped at.
695 tok ptok pptok)
696
697 (save-restriction
698 (if lim (narrow-to-region lim (point-max)))
699
700 (if (save-excursion
701 (and (c-beginning-of-macro)
702 (/= (point) start)))
703 (setq macro-start (point)))
704
705 ;; Try to skip back over unary operator characters, to register
706 ;; that we've moved.
707 (while (progn
708 (setq pos (point))
709 (c-backward-syntactic-ws)
710 ;; Protect post-++/-- operators just before a virtual semicolon.
711 (and (not (c-at-vsemi-p))
712 (/= (skip-chars-backward "-+!*&~@`#") 0))))
713
714 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
715 ;; done. Later on we ignore the boundaries for statements that don't
716 ;; contain any sexp. The only thing that is affected is that the error
717 ;; checking is a little less strict, and we really don't bother.
718 (if (and (memq (char-before) delims)
719 (progn (forward-char -1)
720 (setq saved (point))
721 (c-backward-syntactic-ws)
722 (or (memq (char-before) delims)
723 (memq (char-before) '(?: nil))
724 (eq (char-syntax (char-before)) ?\()
725 (c-at-vsemi-p))))
726 (setq ret 'previous
727 pos saved)
728
729 ;; Begin at start and not pos to detect macros if we stand
730 ;; directly after the #.
731 (goto-char start)
732 (if (looking-at "\\<\\|\\W")
733 ;; Record this as the first token if not starting inside it.
734 (setq tok start))
735
736 ;; The following while loop goes back one sexp (balanced parens,
737 ;; etc. with contents, or symbol or suchlike) each iteration. This
738 ;; movement is accomplished with a call to scan-sexps approx 130 lines
739 ;; below.
740 (while
741 (catch 'loop ;; Throw nil to break, non-nil to continue.
742 (cond
743 ((save-excursion
744 (and macro-start ; Always NIL for AWK.
745 (progn (skip-chars-backward " \t")
746 (eq (char-before) ?#))
747 (progn (setq saved (1- (point)))
748 (beginning-of-line)
749 (not (eq (char-before (1- (point))) ?\\)))
750 (looking-at c-opt-cpp-start)
751 (progn (skip-chars-forward " \t")
752 (eq (point) saved))))
753 (goto-char saved)
754 (if (and (c-forward-to-cpp-define-body)
755 (progn (c-forward-syntactic-ws start)
756 (< (point) start)))
757 ;; Stop at the first token in the content of the macro.
758 (setq pos (point)
759 ignore-labels t) ; Avoid the label check on exit.
760 (setq pos saved
761 ret 'macro
762 ignore-labels t))
763 (throw 'loop nil))
764
765 ;; Do a round through the automaton if we've just passed a
766 ;; statement boundary or passed a "while"-like token.
767 ((or sym
768 (and (looking-at cond-key)
769 (setq sym (intern (match-string 1)))))
770
771 (when (and (< pos start) (null stack))
772 (throw 'loop nil))
773
774 ;; The PDA state handling.
775 ;;
776 ;; Refer to the description of the PDA in the opening
777 ;; comments. In the following OR form, the first leaf
778 ;; attempts to handles one of the specific actions detailed
779 ;; (e.g., finding token "if" whilst in state `else-boundary').
780 ;; We drop through to the second leaf (which handles common
781 ;; state) if no specific handler is found in the first cond.
782 ;; If a parsing error is detected (e.g. an "else" with no
783 ;; preceding "if"), we throw to the enclosing catch.
784 ;;
785 ;; Note that the (eq state 'else) means
786 ;; "we've just passed an else", NOT "we're looking for an
787 ;; else".
788 (or (cond
789 ((eq state 'else)
790 (if (eq sym 'boundary)
791 (setq state 'else-boundary)
792 (c-bos-report-error)
793 (c-bos-pop-state-and-retry)))
794
795 ((eq state 'else-boundary)
796 (cond ((eq sym 'if)
797 (c-bos-pop-state (setq ret 'beginning)))
798 ((eq sym 'boundary)
799 (c-bos-report-error)
800 (c-bos-pop-state))))
801
802 ((eq state 'while)
803 (if (and (eq sym 'boundary)
804 ;; Since this can cause backtracking we do a
805 ;; little more careful analysis to avoid it:
806 ;; If there's a label in front of the while
807 ;; it can't be part of a do-while.
808 (not after-labels-pos))
809 (progn (c-bos-save-pos)
810 (setq state 'while-boundary))
811 (c-bos-pop-state-and-retry))) ; Can't be a do-while
812
813 ((eq state 'while-boundary)
814 (cond ((eq sym 'do)
815 (c-bos-pop-state (setq ret 'beginning)))
816 ((eq sym 'boundary) ; isn't a do-while
817 (c-bos-restore-pos) ; the position of the while
818 (c-bos-pop-state)))) ; no longer searching for do.
819
820 ((eq state 'catch)
821 (if (eq sym 'boundary)
822 (setq state 'catch-boundary)
823 (c-bos-report-error)
824 (c-bos-pop-state-and-retry)))
825
826 ((eq state 'catch-boundary)
827 (cond
828 ((eq sym 'try)
829 (c-bos-pop-state (setq ret 'beginning)))
830 ((eq sym 'catch)
831 (setq state 'catch))
832 ((eq sym 'boundary)
833 (c-bos-report-error)
834 (c-bos-pop-state)))))
835
836 ;; This is state common. We get here when the previous
837 ;; cond statement found no particular state handler.
838 (cond ((eq sym 'boundary)
839 ;; If we have a boundary at the start
840 ;; position we push a frame to go to the
841 ;; previous statement.
842 (if (>= pos start)
843 (c-bos-push-state)
844 (c-bos-pop-state)))
845 ((eq sym 'else)
846 (c-bos-push-state)
847 (c-bos-save-error-info 'if 'else)
848 (setq state 'else))
849 ((eq sym 'while)
850 ;; Is this a real while, or a do-while?
851 ;; The next `when' triggers unless we are SURE that
852 ;; the `while' is not the tailend of a `do-while'.
853 (when (or (not pptok)
854 (memq (char-after pptok) delims)
855 ;; The following kludge is to prevent
856 ;; infinite recursion when called from
857 ;; c-awk-after-if-for-while-condition-p,
858 ;; or the like.
859 (and (eq (point) start)
860 (c-vsemi-status-unknown-p))
861 (c-at-vsemi-p pptok))
862 ;; Since this can cause backtracking we do a
863 ;; little more careful analysis to avoid it: If
864 ;; the while isn't followed by a (possibly
865 ;; virtual) semicolon it can't be a do-while.
866 (c-bos-push-state)
867 (setq state 'while)))
868 ((memq sym '(catch finally))
869 (c-bos-push-state)
870 (c-bos-save-error-info 'try sym)
871 (setq state 'catch))))
872
873 (when c-maybe-labelp
874 ;; We're either past a statement boundary or at the
875 ;; start of a statement, so throw away any label data
876 ;; for the previous one.
877 (setq after-labels-pos nil
878 last-label-pos nil
879 c-maybe-labelp nil))))
880
881 ;; Step to the previous sexp, but not if we crossed a
882 ;; boundary, since that doesn't consume an sexp.
883 (if (eq sym 'boundary)
884 (setq ret 'previous)
885
886 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
887 ;; BACKWARDS THROUGH THE SOURCE.
888
889 ;; This is typically fast with the caching done by
890 ;; c-(backward|forward)-sws.
891 (c-backward-syntactic-ws)
892
893 (let ((before-sws-pos (point))
894 ;; Set as long as we have to continue jumping by sexps.
895 ;; It's the position to use as end in the next round.
896 sexp-loop-continue-pos
897 ;; The end position of the area to search for statement
898 ;; barriers in this round.
899 (sexp-loop-end-pos pos))
900
901 ;; The following while goes back one sexp per iteration.
902 (while
903 (progn
904 (unless (c-safe (c-backward-sexp) t)
905 ;; Give up if we hit an unbalanced block. Since the
906 ;; stack won't be empty the code below will report a
907 ;; suitable error.
908 (throw 'loop nil))
909
910 ;; Check if the sexp movement crossed a statement or
911 ;; declaration boundary. But first modify the point
912 ;; so that `c-crosses-statement-barrier-p' only looks
913 ;; at the non-sexp chars following the sexp.
914 (save-excursion
915 (when (setq
916 boundary-pos
917 (cond
918 ((if macro-start
919 nil
920 (save-excursion
921 (when (c-beginning-of-macro)
922 ;; Set continuation position in case
923 ;; `c-crosses-statement-barrier-p'
924 ;; doesn't detect anything below.
925 (setq sexp-loop-continue-pos (point)))))
926 ;; If the sexp movement took us into a
927 ;; macro then there were only some non-sexp
928 ;; chars after it. Skip out of the macro
929 ;; to analyze them but not the non-sexp
930 ;; chars that might be inside the macro.
931 (c-end-of-macro)
932 (c-crosses-statement-barrier-p
933 (point) sexp-loop-end-pos))
934
935 ((and
936 (eq (char-after) ?{)
937 (not (c-looking-at-inexpr-block lim nil t)))
938 ;; Passed a block sexp. That's a boundary
939 ;; alright.
940 (point))
941
942 ((looking-at "\\s\(")
943 ;; Passed some other paren. Only analyze
944 ;; the non-sexp chars after it.
945 (goto-char (1+ (c-down-list-backward
946 before-sws-pos)))
947 ;; We're at a valid token start position
948 ;; (outside the `save-excursion') if
949 ;; `c-crosses-statement-barrier-p' failed.
950 (c-crosses-statement-barrier-p
951 (point) sexp-loop-end-pos))
952
953 (t
954 ;; Passed a symbol sexp or line
955 ;; continuation. It doesn't matter that
956 ;; it's included in the analyzed region.
957 (if (c-crosses-statement-barrier-p
958 (point) sexp-loop-end-pos)
959 t
960 ;; If it was a line continuation then we
961 ;; have to continue looping.
962 (if (looking-at "\\\\$")
963 (setq sexp-loop-continue-pos (point)))
964 nil))))
965
966 (setq pptok ptok
967 ptok tok
968 tok boundary-pos
969 sym 'boundary)
970 ;; Like a C "continue". Analyze the next sexp.
971 (throw 'loop t)))
972
973 sexp-loop-continue-pos) ; End of "go back a sexp" loop.
974 (goto-char sexp-loop-continue-pos)
975 (setq sexp-loop-end-pos sexp-loop-continue-pos
976 sexp-loop-continue-pos nil))))
977
978 ;; ObjC method def?
979 (when (and c-opt-method-key
980 (setq saved (c-in-method-def-p)))
981 (setq pos saved
982 ignore-labels t) ; Avoid the label check on exit.
983 (throw 'loop nil))
984
985 ;; Handle labels.
986 (unless (eq ignore-labels t)
987 (when (numberp c-maybe-labelp)
988 ;; `c-crosses-statement-barrier-p' has found a colon, so we
989 ;; might be in a label now. Have we got a real label
990 ;; (including a case label) or something like C++'s "public:"?
991 (if (or (not (looking-at c-nonlabel-token-key)) ; proper label
992 (save-excursion ; e.g. "case 'a':" ?
993 (and (c-safe (c-backward-sexp) t)
994 (looking-at "\\<case\\>")))) ; FIXME!!! this is
995 ; wrong for AWK. 2006/1/14.
996 (progn
997 (if after-labels-pos ; Have we already encountered a label?
998 (if (not last-label-pos)
999 (setq last-label-pos (or tok start)))
1000 (setq after-labels-pos (or tok start)))
1001 (setq c-maybe-labelp t
1002 label-good-pos nil))
1003 (setq c-maybe-labelp nil))) ; bogus "label"
1004
1005 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1006 ; been found.
1007 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1008 ;; We're in a potential label and it's the first
1009 ;; time we've found something that isn't allowed in
1010 ;; one.
1011 (setq label-good-pos (or tok start))))
1012
1013 ;; We've moved back by a sexp, so update the token positions.
1014 (setq sym nil
1015 pptok ptok
1016 ptok tok
1017 tok (point)
1018 pos tok))) ; Not nil (for the while loop).
1019
1020 ;; If the stack isn't empty there might be errors to report.
1021 (while stack
1022 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1023 (c-bos-report-error))
1024 (setq saved-pos (cdr (car stack))
1025 stack (cdr stack)))
1026
1027 (when (and (eq ret 'same)
1028 (not (memq sym '(boundary ignore nil))))
1029 ;; Need to investigate closer whether we've crossed
1030 ;; between a substatement and its containing statement.
1031 (if (setq saved (if (looking-at c-block-stmt-1-key)
1032 ptok
1033 pptok))
1034 (cond ((> start saved) (setq pos saved))
1035 ((= start saved) (setq ret 'up)))))
1036
1037 (when (and (not ignore-labels)
1038 (eq c-maybe-labelp t)
1039 (not (eq ret 'beginning))
1040 after-labels-pos
1041 (or (not label-good-pos)
1042 (<= label-good-pos pos)
1043 (progn
1044 (goto-char (if (and last-label-pos
1045 (< last-label-pos start))
1046 last-label-pos
1047 pos))
1048 (looking-at c-label-kwds-regexp))))
1049 ;; We're in a label. Maybe we should step to the statement
1050 ;; after it.
1051 (if (< after-labels-pos start)
1052 (setq pos after-labels-pos)
1053 (setq ret 'label)
1054 (if (and last-label-pos (< last-label-pos start))
1055 ;; Might have jumped over several labels. Go to the last one.
1056 (setq pos last-label-pos)))))
1057
1058 ;; Skip over the unary operators that can start the statement.
1059 (goto-char pos)
1060 (while (progn
1061 (c-backward-syntactic-ws)
1062 ;; protect AWK post-inc/decrement operators, etc.
1063 (and (not (c-at-vsemi-p (point)))
1064 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1065 (setq pos (point)))
1066 (goto-char pos)
1067 ret)))
1068
1069 (defun c-crosses-statement-barrier-p (from to)
1070 "Return non-nil if buffer positions FROM to TO cross one or more
1071 statement or declaration boundaries. The returned value is actually
1072 the position of the earliest boundary char. FROM must not be within
1073 a string or comment.
1074
1075 The variable `c-maybe-labelp' is set to the position of the first `:' that
1076 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1077 single `?' is found, then `c-maybe-labelp' is cleared.
1078
1079 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1080 regarded as having a \"virtual semicolon\" immediately after the last token on
1081 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1082
1083 Note that this function might do hidden buffer changes. See the
1084 comment at the start of cc-engine.el for more info."
1085 (let ((skip-chars c-stmt-delim-chars)
1086 lit-range)
1087 (save-excursion
1088 (catch 'done
1089 (goto-char from)
1090 (while (progn (skip-chars-forward skip-chars to)
1091 (< (point) to))
1092 (cond
1093 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1094 (goto-char (cdr lit-range)))
1095 ((eq (char-after) ?:)
1096 (forward-char)
1097 (if (and (eq (char-after) ?:)
1098 (< (point) to))
1099 ;; Ignore scope operators.
1100 (forward-char)
1101 (setq c-maybe-labelp (1- (point)))))
1102 ((eq (char-after) ??)
1103 ;; A question mark. Can't be a label, so stop
1104 ;; looking for more : and ?.
1105 (setq c-maybe-labelp nil
1106 skip-chars (substring c-stmt-delim-chars 0 -2)))
1107 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1108 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1109 (backward-char))
1110 (skip-chars-backward " \t" from)
1111 (if (c-at-vsemi-p)
1112 (throw 'done (point))
1113 (forward-line)))
1114 (t (throw 'done (point)))))
1115 ;; In trailing space after an as yet undetected virtual semicolon?
1116 (c-backward-syntactic-ws from)
1117 (if (and (< (point) to)
1118 (c-at-vsemi-p))
1119 (point)
1120 nil)))))
1121
1122 (defun c-at-statement-start-p ()
1123 "Return non-nil if the point is at the first token in a statement
1124 or somewhere in the syntactic whitespace before it.
1125
1126 A \"statement\" here is not restricted to those inside code blocks.
1127 Any kind of declaration-like construct that occur outside function
1128 bodies is also considered a \"statement\".
1129
1130 Note that this function might do hidden buffer changes. See the
1131 comment at the start of cc-engine.el for more info."
1132
1133 (save-excursion
1134 (let ((end (point))
1135 c-maybe-labelp)
1136 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1137 (or (bobp)
1138 (eq (char-before) ?})
1139 (and (eq (char-before) ?{)
1140 (not (and c-special-brace-lists
1141 (progn (backward-char)
1142 (c-looking-at-special-brace-list)))))
1143 (c-crosses-statement-barrier-p (point) end)))))
1144
1145 (defun c-at-expression-start-p ()
1146 "Return non-nil if the point is at the first token in an expression or
1147 statement, or somewhere in the syntactic whitespace before it.
1148
1149 An \"expression\" here is a bit different from the normal language
1150 grammar sense: It's any sequence of expression tokens except commas,
1151 unless they are enclosed inside parentheses of some kind. Also, an
1152 expression never continues past an enclosing parenthesis, but it might
1153 contain parenthesis pairs of any sort except braces.
1154
1155 Since expressions never cross statement boundaries, this function also
1156 recognizes statement beginnings, just like `c-at-statement-start-p'.
1157
1158 Note that this function might do hidden buffer changes. See the
1159 comment at the start of cc-engine.el for more info."
1160
1161 (save-excursion
1162 (let ((end (point))
1163 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1164 c-maybe-labelp)
1165 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1166 (or (bobp)
1167 (memq (char-before) '(?{ ?}))
1168 (save-excursion (backward-char)
1169 (looking-at "\\s("))
1170 (c-crosses-statement-barrier-p (point) end)))))
1171
1172 \f
1173 ;; A set of functions that covers various idiosyncrasies in
1174 ;; implementations of `forward-comment'.
1175
1176 ;; Note: Some emacsen considers incorrectly that any line comment
1177 ;; ending with a backslash continues to the next line. I can't think
1178 ;; of any way to work around that in a reliable way without changing
1179 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1180 ;; changing the syntax for backslash doesn't work since we must treat
1181 ;; escapes in string literals correctly.)
1182
1183 (defun c-forward-single-comment ()
1184 "Move forward past whitespace and the closest following comment, if any.
1185 Return t if a comment was found, nil otherwise. In either case, the
1186 point is moved past the following whitespace. Line continuations,
1187 i.e. a backslashes followed by line breaks, are treated as whitespace.
1188 The line breaks that end line comments are considered to be the
1189 comment enders, so the point will be put on the beginning of the next
1190 line if it moved past a line comment.
1191
1192 This function does not do any hidden buffer changes."
1193
1194 (let ((start (point)))
1195 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1196 (goto-char (match-end 0)))
1197
1198 (when (forward-comment 1)
1199 (if (eobp)
1200 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1201 ;; forwards at eob.
1202 nil
1203
1204 ;; Emacs includes the ending newline in a b-style (c++)
1205 ;; comment, but XEmacs doesn't. We depend on the Emacs
1206 ;; behavior (which also is symmetric).
1207 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1208 (condition-case nil (forward-char 1)))
1209
1210 t))))
1211
1212 (defsubst c-forward-comments ()
1213 "Move forward past all following whitespace and comments.
1214 Line continuations, i.e. a backslashes followed by line breaks, are
1215 treated as whitespace.
1216
1217 Note that this function might do hidden buffer changes. See the
1218 comment at the start of cc-engine.el for more info."
1219
1220 (while (or
1221 ;; If forward-comment in at least XEmacs 21 is given a large
1222 ;; positive value, it'll loop all the way through if it hits
1223 ;; eob.
1224 (and (forward-comment 5)
1225 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1226 ;; forwards at eob.
1227 (not (eobp)))
1228
1229 (when (looking-at "\\\\[\n\r]")
1230 (forward-char 2)
1231 t))))
1232
1233 (defun c-backward-single-comment ()
1234 "Move backward past whitespace and the closest preceding comment, if any.
1235 Return t if a comment was found, nil otherwise. In either case, the
1236 point is moved past the preceding whitespace. Line continuations,
1237 i.e. a backslashes followed by line breaks, are treated as whitespace.
1238 The line breaks that end line comments are considered to be the
1239 comment enders, so the point cannot be at the end of the same line to
1240 move over a line comment.
1241
1242 This function does not do any hidden buffer changes."
1243
1244 (let ((start (point)))
1245 ;; When we got newline terminated comments, forward-comment in all
1246 ;; supported emacsen so far will stop at eol of each line not
1247 ;; ending with a comment when moving backwards. This corrects for
1248 ;; that, and at the same time handles line continuations.
1249 (while (progn
1250 (skip-chars-backward " \t\n\r\f\v")
1251 (and (looking-at "[\n\r]")
1252 (eq (char-before) ?\\)))
1253 (backward-char))
1254
1255 (if (bobp)
1256 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1257 ;; backwards at bob.
1258 nil
1259
1260 ;; Leave point after the closest following newline if we've
1261 ;; backed up over any above, since forward-comment won't move
1262 ;; backward over a line comment if point is at the end of the
1263 ;; same line.
1264 (re-search-forward "\\=\\s *[\n\r]" start t)
1265
1266 (if (if (forward-comment -1)
1267 (if (eolp)
1268 ;; If forward-comment above succeeded and we're at eol
1269 ;; then the newline we moved over above didn't end a
1270 ;; line comment, so we give it another go.
1271 (forward-comment -1)
1272 t))
1273
1274 ;; Emacs <= 20 and XEmacs move back over the closer of a
1275 ;; block comment that lacks an opener.
1276 (if (looking-at "\\*/")
1277 (progn (forward-char 2) nil)
1278 t)))))
1279
1280 (defsubst c-backward-comments ()
1281 "Move backward past all preceding whitespace and comments.
1282 Line continuations, i.e. a backslashes followed by line breaks, are
1283 treated as whitespace. The line breaks that end line comments are
1284 considered to be the comment enders, so the point cannot be at the end
1285 of the same line to move over a line comment. Unlike
1286 c-backward-syntactic-ws, this function doesn't move back over
1287 preprocessor directives.
1288
1289 Note that this function might do hidden buffer changes. See the
1290 comment at the start of cc-engine.el for more info."
1291
1292 (let ((start (point)))
1293 (while (and
1294 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1295 ;; return t when moving backwards at bob.
1296 (not (bobp))
1297
1298 (if (forward-comment -1)
1299 (if (looking-at "\\*/")
1300 ;; Emacs <= 20 and XEmacs move back over the
1301 ;; closer of a block comment that lacks an opener.
1302 (progn (forward-char 2) nil)
1303 t)
1304
1305 ;; XEmacs treats line continuations as whitespace but
1306 ;; only in the backward direction, which seems a bit
1307 ;; odd. Anyway, this is necessary for Emacs.
1308 (when (and (looking-at "[\n\r]")
1309 (eq (char-before) ?\\)
1310 (< (point) start))
1311 (backward-char)
1312 t))))))
1313
1314 \f
1315 ;; Tools for skipping over syntactic whitespace.
1316
1317 ;; The following functions use text properties to cache searches over
1318 ;; large regions of syntactic whitespace. It works as follows:
1319 ;;
1320 ;; o If a syntactic whitespace region contains anything but simple
1321 ;; whitespace (i.e. space, tab and line breaks), the text property
1322 ;; `c-in-sws' is put over it. At places where we have stopped
1323 ;; within that region there's also a `c-is-sws' text property.
1324 ;; That since there typically are nested whitespace inside that
1325 ;; must be handled separately, e.g. whitespace inside a comment or
1326 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1327 ;; to jump to another point with that property within the same
1328 ;; `c-in-sws' region. It can be likened to a ladder where
1329 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1330 ;;
1331 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1332 ;; a "rung position" and also maybe on the first following char.
1333 ;; As many characters as can be conveniently found in this range
1334 ;; are marked, but no assumption can be made that the whole range
1335 ;; is marked (it could be clobbered by later changes, for
1336 ;; instance).
1337 ;;
1338 ;; Note that some part of the beginning of a sequence of simple
1339 ;; whitespace might be part of the end of a preceding line comment
1340 ;; or cpp directive and must not be considered part of the "rung".
1341 ;; Such whitespace is some amount of horizontal whitespace followed
1342 ;; by a newline. In the case of cpp directives it could also be
1343 ;; two newlines with horizontal whitespace between them.
1344 ;;
1345 ;; The reason to include the first following char is to cope with
1346 ;; "rung positions" that doesn't have any ordinary whitespace. If
1347 ;; `c-is-sws' is put on a token character it does not have
1348 ;; `c-in-sws' set simultaneously. That's the only case when that
1349 ;; can occur, and the reason for not extending the `c-in-sws'
1350 ;; region to cover it is that the `c-in-sws' region could then be
1351 ;; accidentally merged with a following one if the token is only
1352 ;; one character long.
1353 ;;
1354 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1355 ;; removed in the changed region. If the change was inside
1356 ;; syntactic whitespace that means that the "ladder" is broken, but
1357 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1358 ;; parts on either side and use an ordinary search only to "repair"
1359 ;; the gap.
1360 ;;
1361 ;; Special care needs to be taken if a region is removed: If there
1362 ;; are `c-in-sws' on both sides of it which do not connect inside
1363 ;; the region then they can't be joined. If e.g. a marked macro is
1364 ;; broken, syntactic whitespace inside the new text might be
1365 ;; marked. If those marks would become connected with the old
1366 ;; `c-in-sws' range around the macro then we could get a ladder
1367 ;; with one end outside the macro and the other at some whitespace
1368 ;; within it.
1369 ;;
1370 ;; The main motivation for this system is to increase the speed in
1371 ;; skipping over the large whitespace regions that can occur at the
1372 ;; top level in e.g. header files that contain a lot of comments and
1373 ;; cpp directives. For small comments inside code it's probably
1374 ;; slower than using `forward-comment' straightforwardly, but speed is
1375 ;; not a significant factor there anyway.
1376
1377 ; (defface c-debug-is-sws-face
1378 ; '((t (:background "GreenYellow")))
1379 ; "Debug face to mark the `c-is-sws' property.")
1380 ; (defface c-debug-in-sws-face
1381 ; '((t (:underline t)))
1382 ; "Debug face to mark the `c-in-sws' property.")
1383
1384 ; (defun c-debug-put-sws-faces ()
1385 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1386 ; ;; properties in the buffer.
1387 ; (interactive)
1388 ; (save-excursion
1389 ; (c-save-buffer-state (in-face)
1390 ; (goto-char (point-min))
1391 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1392 ; (point)))
1393 ; (while (progn
1394 ; (goto-char (next-single-property-change
1395 ; (point) 'c-is-sws nil (point-max)))
1396 ; (if in-face
1397 ; (progn
1398 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1399 ; (setq in-face nil))
1400 ; (setq in-face (point)))
1401 ; (not (eobp))))
1402 ; (goto-char (point-min))
1403 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1404 ; (point)))
1405 ; (while (progn
1406 ; (goto-char (next-single-property-change
1407 ; (point) 'c-in-sws nil (point-max)))
1408 ; (if in-face
1409 ; (progn
1410 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1411 ; (setq in-face nil))
1412 ; (setq in-face (point)))
1413 ; (not (eobp)))))))
1414
1415 (defmacro c-debug-sws-msg (&rest args)
1416 ;;`(message ,@args)
1417 )
1418
1419 (defmacro c-put-is-sws (beg end)
1420 ;; This macro does a hidden buffer change.
1421 `(let ((beg ,beg) (end ,end))
1422 (put-text-property beg end 'c-is-sws t)
1423 ,@(when (facep 'c-debug-is-sws-face)
1424 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1425
1426 (defmacro c-put-in-sws (beg end)
1427 ;; This macro does a hidden buffer change.
1428 `(let ((beg ,beg) (end ,end))
1429 (put-text-property beg end 'c-in-sws t)
1430 ,@(when (facep 'c-debug-is-sws-face)
1431 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1432
1433 (defmacro c-remove-is-sws (beg end)
1434 ;; This macro does a hidden buffer change.
1435 `(let ((beg ,beg) (end ,end))
1436 (remove-text-properties beg end '(c-is-sws nil))
1437 ,@(when (facep 'c-debug-is-sws-face)
1438 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1439
1440 (defmacro c-remove-in-sws (beg end)
1441 ;; This macro does a hidden buffer change.
1442 `(let ((beg ,beg) (end ,end))
1443 (remove-text-properties beg end '(c-in-sws nil))
1444 ,@(when (facep 'c-debug-is-sws-face)
1445 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1446
1447 (defmacro c-remove-is-and-in-sws (beg end)
1448 ;; This macro does a hidden buffer change.
1449 `(let ((beg ,beg) (end ,end))
1450 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1451 ,@(when (facep 'c-debug-is-sws-face)
1452 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1453 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1454
1455 (defsubst c-invalidate-sws-region-after (beg end)
1456 ;; Called from `after-change-functions'. Note that if
1457 ;; `c-forward-sws' or `c-backward-sws' are used outside
1458 ;; `c-save-buffer-state' or similar then this will remove the cache
1459 ;; properties right after they're added.
1460 ;;
1461 ;; This function does hidden buffer changes.
1462
1463 (save-excursion
1464 ;; Adjust the end to remove the properties in any following simple
1465 ;; ws up to and including the next line break, if there is any
1466 ;; after the changed region. This is necessary e.g. when a rung
1467 ;; marked empty line is converted to a line comment by inserting
1468 ;; "//" before the line break. In that case the line break would
1469 ;; keep the rung mark which could make a later `c-backward-sws'
1470 ;; move into the line comment instead of over it.
1471 (goto-char end)
1472 (skip-chars-forward " \t\f\v")
1473 (when (and (eolp) (not (eobp)))
1474 (setq end (1+ (point)))))
1475
1476 (when (and (= beg end)
1477 (get-text-property beg 'c-in-sws)
1478 (> beg (point-min))
1479 (get-text-property (1- beg) 'c-in-sws))
1480 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1481 ;; safe to keep a range that was continuous before the change. E.g:
1482 ;;
1483 ;; #define foo
1484 ;; \
1485 ;; bar
1486 ;;
1487 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1488 ;; after "foo" is removed then "bar" will become part of the cpp
1489 ;; directive instead of a syntactically relevant token. In that
1490 ;; case there's no longer syntactic ws from "#" to "b".
1491 (setq beg (1- beg)))
1492
1493 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1494 (c-remove-is-and-in-sws beg end))
1495
1496 (defun c-forward-sws ()
1497 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1498 ;;
1499 ;; This function might do hidden buffer changes.
1500
1501 (let (;; `rung-pos' is set to a position as early as possible in the
1502 ;; unmarked part of the simple ws region.
1503 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1504 rung-is-marked next-rung-is-marked simple-ws-end
1505 ;; `safe-start' is set when it's safe to cache the start position.
1506 ;; It's not set if we've initially skipped over comments and line
1507 ;; continuations since we might have gone out through the end of a
1508 ;; macro then. This provision makes `c-forward-sws' not populate the
1509 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1510 ;; more common.
1511 safe-start)
1512
1513 ;; Skip simple ws and do a quick check on the following character to see
1514 ;; if it's anything that can't start syntactic ws, so we can bail out
1515 ;; early in the majority of cases when there just are a few ws chars.
1516 (skip-chars-forward " \t\n\r\f\v")
1517 (when (looking-at c-syntactic-ws-start)
1518
1519 (setq rung-end-pos (min (1+ (point)) (point-max)))
1520 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1521 'c-is-sws t))
1522 ;; Find the last rung position to avoid setting properties in all
1523 ;; the cases when the marked rung is complete.
1524 ;; (`next-single-property-change' is certain to move at least one
1525 ;; step forward.)
1526 (setq rung-pos (1- (next-single-property-change
1527 rung-is-marked 'c-is-sws nil rung-end-pos)))
1528 ;; Got no marked rung here. Since the simple ws might have started
1529 ;; inside a line comment or cpp directive we must set `rung-pos' as
1530 ;; high as possible.
1531 (setq rung-pos (point)))
1532
1533 (while
1534 (progn
1535 (while
1536 (when (and rung-is-marked
1537 (get-text-property (point) 'c-in-sws))
1538
1539 ;; The following search is the main reason that `c-in-sws'
1540 ;; and `c-is-sws' aren't combined to one property.
1541 (goto-char (next-single-property-change
1542 (point) 'c-in-sws nil (point-max)))
1543 (unless (get-text-property (point) 'c-is-sws)
1544 ;; If the `c-in-sws' region extended past the last
1545 ;; `c-is-sws' char we have to go back a bit.
1546 (or (get-text-property (1- (point)) 'c-is-sws)
1547 (goto-char (previous-single-property-change
1548 (point) 'c-is-sws)))
1549 (backward-char))
1550
1551 (c-debug-sws-msg
1552 "c-forward-sws cached move %s -> %s (max %s)"
1553 rung-pos (point) (point-max))
1554
1555 (setq rung-pos (point))
1556 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1557 (not (eobp))))
1558
1559 ;; We'll loop here if there is simple ws after the last rung.
1560 ;; That means that there's been some change in it and it's
1561 ;; possible that we've stepped into another ladder, so extend
1562 ;; the previous one to join with it if there is one, and try to
1563 ;; use the cache again.
1564 (c-debug-sws-msg
1565 "c-forward-sws extending rung with [%s..%s] (max %s)"
1566 (1+ rung-pos) (1+ (point)) (point-max))
1567 (unless (get-text-property (point) 'c-is-sws)
1568 ;; Remove any `c-in-sws' property from the last char of
1569 ;; the rung before we mark it with `c-is-sws', so that we
1570 ;; won't connect with the remains of a broken "ladder".
1571 (c-remove-in-sws (point) (1+ (point))))
1572 (c-put-is-sws (1+ rung-pos)
1573 (1+ (point)))
1574 (c-put-in-sws rung-pos
1575 (setq rung-pos (point)
1576 last-put-in-sws-pos rung-pos)))
1577
1578 (setq simple-ws-end (point))
1579 (c-forward-comments)
1580
1581 (cond
1582 ((/= (point) simple-ws-end)
1583 ;; Skipped over comments. Don't cache at eob in case the buffer
1584 ;; is narrowed.
1585 (not (eobp)))
1586
1587 ((save-excursion
1588 (and c-opt-cpp-prefix
1589 (looking-at c-opt-cpp-start)
1590 (progn (skip-chars-backward " \t")
1591 (bolp))
1592 (or (bobp)
1593 (progn (backward-char)
1594 (not (eq (char-before) ?\\))))))
1595 ;; Skip a preprocessor directive.
1596 (end-of-line)
1597 (while (and (eq (char-before) ?\\)
1598 (= (forward-line 1) 0))
1599 (end-of-line))
1600 (forward-line 1)
1601 (setq safe-start t)
1602 ;; Don't cache at eob in case the buffer is narrowed.
1603 (not (eobp)))))
1604
1605 ;; We've searched over a piece of non-white syntactic ws. See if this
1606 ;; can be cached.
1607 (setq next-rung-pos (point))
1608 (skip-chars-forward " \t\n\r\f\v")
1609 (setq rung-end-pos (min (1+ (point)) (point-max)))
1610
1611 (if (or
1612 ;; Cache if we haven't skipped comments only, and if we started
1613 ;; either from a marked rung or from a completely uncached
1614 ;; position.
1615 (and safe-start
1616 (or rung-is-marked
1617 (not (get-text-property simple-ws-end 'c-in-sws))))
1618
1619 ;; See if there's a marked rung in the encountered simple ws. If
1620 ;; so then we can cache, unless `safe-start' is nil. Even then
1621 ;; we need to do this to check if the cache can be used for the
1622 ;; next step.
1623 (and (setq next-rung-is-marked
1624 (text-property-any next-rung-pos rung-end-pos
1625 'c-is-sws t))
1626 safe-start))
1627
1628 (progn
1629 (c-debug-sws-msg
1630 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1631 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1632 (point-max))
1633
1634 ;; Remove the properties for any nested ws that might be cached.
1635 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1636 ;; anyway.
1637 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1638 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1639 (c-put-is-sws rung-pos
1640 (1+ simple-ws-end))
1641 (setq rung-is-marked t))
1642 (c-put-in-sws rung-pos
1643 (setq rung-pos (point)
1644 last-put-in-sws-pos rung-pos))
1645 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1646 ;; Remove any `c-in-sws' property from the last char of
1647 ;; the rung before we mark it with `c-is-sws', so that we
1648 ;; won't connect with the remains of a broken "ladder".
1649 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1650 (c-put-is-sws next-rung-pos
1651 rung-end-pos))
1652
1653 (c-debug-sws-msg
1654 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1655 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1656 (point-max))
1657
1658 ;; Set `rung-pos' for the next rung. It's the same thing here as
1659 ;; initially, except that the rung position is set as early as
1660 ;; possible since we can't be in the ending ws of a line comment or
1661 ;; cpp directive now.
1662 (if (setq rung-is-marked next-rung-is-marked)
1663 (setq rung-pos (1- (next-single-property-change
1664 rung-is-marked 'c-is-sws nil rung-end-pos)))
1665 (setq rung-pos next-rung-pos))
1666 (setq safe-start t)))
1667
1668 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1669 ;; another one after the point (which might occur when editing inside a
1670 ;; comment or macro).
1671 (when (eq last-put-in-sws-pos (point))
1672 (cond ((< last-put-in-sws-pos (point-max))
1673 (c-debug-sws-msg
1674 "c-forward-sws clearing at %s for cache separation"
1675 last-put-in-sws-pos)
1676 (c-remove-in-sws last-put-in-sws-pos
1677 (1+ last-put-in-sws-pos)))
1678 (t
1679 ;; If at eob we have to clear the last character before the end
1680 ;; instead since the buffer might be narrowed and there might
1681 ;; be a `c-in-sws' after (point-max). In this case it's
1682 ;; necessary to clear both properties.
1683 (c-debug-sws-msg
1684 "c-forward-sws clearing thoroughly at %s for cache separation"
1685 (1- last-put-in-sws-pos))
1686 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1687 last-put-in-sws-pos))))
1688 )))
1689
1690 (defun c-backward-sws ()
1691 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1692 ;;
1693 ;; This function might do hidden buffer changes.
1694
1695 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1696 ;; part of the simple ws region.
1697 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1698 rung-is-marked simple-ws-beg cmt-skip-pos)
1699
1700 ;; Skip simple horizontal ws and do a quick check on the preceding
1701 ;; character to see if it's anying that can't end syntactic ws, so we can
1702 ;; bail out early in the majority of cases when there just are a few ws
1703 ;; chars. Newlines are complicated in the backward direction, so we can't
1704 ;; skip over them.
1705 (skip-chars-backward " \t\f")
1706 (when (and (not (bobp))
1707 (save-excursion
1708 (backward-char)
1709 (looking-at c-syntactic-ws-end)))
1710
1711 ;; Try to find a rung position in the simple ws preceding point, so that
1712 ;; we can get a cache hit even if the last bit of the simple ws has
1713 ;; changed recently.
1714 (setq simple-ws-beg (point))
1715 (skip-chars-backward " \t\n\r\f\v")
1716 (if (setq rung-is-marked (text-property-any
1717 (point) (min (1+ rung-pos) (point-max))
1718 'c-is-sws t))
1719 ;; `rung-pos' will be the earliest marked position, which means that
1720 ;; there might be later unmarked parts in the simple ws region.
1721 ;; It's not worth the effort to fix that; the last part of the
1722 ;; simple ws is also typically edited often, so it could be wasted.
1723 (goto-char (setq rung-pos rung-is-marked))
1724 (goto-char simple-ws-beg))
1725
1726 (while
1727 (progn
1728 (while
1729 (when (and rung-is-marked
1730 (not (bobp))
1731 (get-text-property (1- (point)) 'c-in-sws))
1732
1733 ;; The following search is the main reason that `c-in-sws'
1734 ;; and `c-is-sws' aren't combined to one property.
1735 (goto-char (previous-single-property-change
1736 (point) 'c-in-sws nil (point-min)))
1737 (unless (get-text-property (point) 'c-is-sws)
1738 ;; If the `c-in-sws' region extended past the first
1739 ;; `c-is-sws' char we have to go forward a bit.
1740 (goto-char (next-single-property-change
1741 (point) 'c-is-sws)))
1742
1743 (c-debug-sws-msg
1744 "c-backward-sws cached move %s <- %s (min %s)"
1745 (point) rung-pos (point-min))
1746
1747 (setq rung-pos (point))
1748 (if (and (< (min (skip-chars-backward " \t\f\v")
1749 (progn
1750 (setq simple-ws-beg (point))
1751 (skip-chars-backward " \t\n\r\f\v")))
1752 0)
1753 (setq rung-is-marked
1754 (text-property-any (point) rung-pos
1755 'c-is-sws t)))
1756 t
1757 (goto-char simple-ws-beg)
1758 nil))
1759
1760 ;; We'll loop here if there is simple ws before the first rung.
1761 ;; That means that there's been some change in it and it's
1762 ;; possible that we've stepped into another ladder, so extend
1763 ;; the previous one to join with it if there is one, and try to
1764 ;; use the cache again.
1765 (c-debug-sws-msg
1766 "c-backward-sws extending rung with [%s..%s] (min %s)"
1767 rung-is-marked rung-pos (point-min))
1768 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1769 ;; Remove any `c-in-sws' property from the last char of
1770 ;; the rung before we mark it with `c-is-sws', so that we
1771 ;; won't connect with the remains of a broken "ladder".
1772 (c-remove-in-sws (1- rung-pos) rung-pos))
1773 (c-put-is-sws rung-is-marked
1774 rung-pos)
1775 (c-put-in-sws rung-is-marked
1776 (1- rung-pos))
1777 (setq rung-pos rung-is-marked
1778 last-put-in-sws-pos rung-pos))
1779
1780 (c-backward-comments)
1781 (setq cmt-skip-pos (point))
1782
1783 (cond
1784 ((and c-opt-cpp-prefix
1785 (/= cmt-skip-pos simple-ws-beg)
1786 (c-beginning-of-macro))
1787 ;; Inside a cpp directive. See if it should be skipped over.
1788 (let ((cpp-beg (point)))
1789
1790 ;; Move back over all line continuations in the region skipped
1791 ;; over by `c-backward-comments'. If we go past it then we
1792 ;; started inside the cpp directive.
1793 (goto-char simple-ws-beg)
1794 (beginning-of-line)
1795 (while (and (> (point) cmt-skip-pos)
1796 (progn (backward-char)
1797 (eq (char-before) ?\\)))
1798 (beginning-of-line))
1799
1800 (if (< (point) cmt-skip-pos)
1801 ;; Don't move past the cpp directive if we began inside
1802 ;; it. Note that the position at the end of the last line
1803 ;; of the macro is also considered to be within it.
1804 (progn (goto-char cmt-skip-pos)
1805 nil)
1806
1807 ;; It's worthwhile to spend a little bit of effort on finding
1808 ;; the end of the macro, to get a good `simple-ws-beg'
1809 ;; position for the cache. Note that `c-backward-comments'
1810 ;; could have stepped over some comments before going into
1811 ;; the macro, and then `simple-ws-beg' must be kept on the
1812 ;; same side of those comments.
1813 (goto-char simple-ws-beg)
1814 (skip-chars-backward " \t\n\r\f\v")
1815 (if (eq (char-before) ?\\)
1816 (forward-char))
1817 (forward-line 1)
1818 (if (< (point) simple-ws-beg)
1819 ;; Might happen if comments after the macro were skipped
1820 ;; over.
1821 (setq simple-ws-beg (point)))
1822
1823 (goto-char cpp-beg)
1824 t)))
1825
1826 ((/= (save-excursion
1827 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1828 (setq next-rung-pos (point)))
1829 simple-ws-beg)
1830 ;; Skipped over comments. Must put point at the end of
1831 ;; the simple ws at point since we might be after a line
1832 ;; comment or cpp directive that's been partially
1833 ;; narrowed out, and we can't risk marking the simple ws
1834 ;; at the end of it.
1835 (goto-char next-rung-pos)
1836 t)))
1837
1838 ;; We've searched over a piece of non-white syntactic ws. See if this
1839 ;; can be cached.
1840 (setq next-rung-pos (point))
1841 (skip-chars-backward " \t\f\v")
1842
1843 (if (or
1844 ;; Cache if we started either from a marked rung or from a
1845 ;; completely uncached position.
1846 rung-is-marked
1847 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1848
1849 ;; Cache if there's a marked rung in the encountered simple ws.
1850 (save-excursion
1851 (skip-chars-backward " \t\n\r\f\v")
1852 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1853 'c-is-sws t)))
1854
1855 (progn
1856 (c-debug-sws-msg
1857 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1858 (point) (1+ next-rung-pos)
1859 simple-ws-beg (min (1+ rung-pos) (point-max))
1860 (point-min))
1861
1862 ;; Remove the properties for any nested ws that might be cached.
1863 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1864 ;; anyway.
1865 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1866 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1867 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1868 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1869 ;; Remove any `c-in-sws' property from the last char of
1870 ;; the rung before we mark it with `c-is-sws', so that we
1871 ;; won't connect with the remains of a broken "ladder".
1872 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1873 (c-put-is-sws simple-ws-beg
1874 rung-end-pos)
1875 (setq rung-is-marked t)))
1876 (c-put-in-sws (setq simple-ws-beg (point)
1877 last-put-in-sws-pos simple-ws-beg)
1878 rung-pos)
1879 (c-put-is-sws (setq rung-pos simple-ws-beg)
1880 (1+ next-rung-pos)))
1881
1882 (c-debug-sws-msg
1883 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1884 (point) (1+ next-rung-pos)
1885 simple-ws-beg (min (1+ rung-pos) (point-max))
1886 (point-min))
1887 (setq rung-pos next-rung-pos
1888 simple-ws-beg (point))
1889 ))
1890
1891 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1892 ;; another one before the point (which might occur when editing inside a
1893 ;; comment or macro).
1894 (when (eq last-put-in-sws-pos (point))
1895 (cond ((< (point-min) last-put-in-sws-pos)
1896 (c-debug-sws-msg
1897 "c-backward-sws clearing at %s for cache separation"
1898 (1- last-put-in-sws-pos))
1899 (c-remove-in-sws (1- last-put-in-sws-pos)
1900 last-put-in-sws-pos))
1901 ((> (point-min) 1)
1902 ;; If at bob and the buffer is narrowed, we have to clear the
1903 ;; character we're standing on instead since there might be a
1904 ;; `c-in-sws' before (point-min). In this case it's necessary
1905 ;; to clear both properties.
1906 (c-debug-sws-msg
1907 "c-backward-sws clearing thoroughly at %s for cache separation"
1908 last-put-in-sws-pos)
1909 (c-remove-is-and-in-sws last-put-in-sws-pos
1910 (1+ last-put-in-sws-pos)))))
1911 )))
1912
1913 \f
1914 ;; Other whitespace tools
1915 (defun c-partial-ws-p (beg end)
1916 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1917 ;; region? This is a "heuristic" function. .....
1918 ;;
1919 ;; The motivation for the second bit is to check whether removing this
1920 ;; region would coalesce two symbols.
1921 ;;
1922 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1923 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1924 (save-excursion
1925 (let ((end+1 (min (1+ end) (point-max))))
1926 (or (progn (goto-char (max (point-min) (1- beg)))
1927 (c-skip-ws-forward end)
1928 (eq (point) end))
1929 (progn (goto-char beg)
1930 (c-skip-ws-forward end+1)
1931 (eq (point) end+1))))))
1932 \f
1933 ;; A system for finding noteworthy parens before the point.
1934
1935 (defvar c-state-cache nil)
1936 (make-variable-buffer-local 'c-state-cache)
1937 ;; The state cache used by `c-parse-state' to cut down the amount of
1938 ;; searching. It's the result from some earlier `c-parse-state' call.
1939 ;;
1940 ;; The use of the cached info is more effective if the next
1941 ;; `c-parse-state' call is on a line close by the one the cached state
1942 ;; was made at; the cache can actually slow down a little if the
1943 ;; cached state was made very far back in the buffer. The cache is
1944 ;; most effective if `c-parse-state' is used on each line while moving
1945 ;; forward.
1946
1947 (defvar c-state-cache-start 1)
1948 (make-variable-buffer-local 'c-state-cache-start)
1949 ;; This is (point-min) when `c-state-cache' was calculated, since a
1950 ;; change of narrowing is likely to affect the parens that are visible
1951 ;; before the point.
1952
1953 (defvar c-state-cache-good-pos 1)
1954 (make-variable-buffer-local 'c-state-cache-good-pos)
1955 ;; This is a position where `c-state-cache' is known to be correct.
1956 ;; It's a position inside one of the recorded unclosed parens or the
1957 ;; top level, but not further nested inside any literal or subparen
1958 ;; that is closed before the last recorded position.
1959 ;;
1960 ;; The exact position is chosen to try to be close to yet earlier than
1961 ;; the position where `c-state-cache' will be called next. Right now
1962 ;; the heuristic is to set it to the position after the last found
1963 ;; closing paren (of any type) before the line on which
1964 ;; `c-parse-state' was called. That is chosen primarily to work well
1965 ;; with refontification of the current line.
1966
1967 (defsubst c-invalidate-state-cache (pos)
1968 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1969 ;; at POS or higher. This is much like `c-whack-state-after', but
1970 ;; it never changes a paren pair element into an open paren element.
1971 ;; Doing that would mean that the new open paren wouldn't have the
1972 ;; required preceding paren pair element.
1973 (while (and (or c-state-cache
1974 (when (< pos c-state-cache-good-pos)
1975 (setq c-state-cache-good-pos 1)
1976 nil))
1977 (let ((elem (car c-state-cache)))
1978 (if (consp elem)
1979 (or (< pos (cdr elem))
1980 (when (< pos c-state-cache-good-pos)
1981 (setq c-state-cache-good-pos (cdr elem))
1982 nil))
1983 (or (<= pos elem)
1984 (when (< pos c-state-cache-good-pos)
1985 (setq c-state-cache-good-pos (1+ elem))
1986 nil)))))
1987 (setq c-state-cache (cdr c-state-cache))))
1988
1989 (defun c-get-fallback-start-pos (here)
1990 ;; Return the start position for building `c-state-cache' from
1991 ;; scratch.
1992 (save-excursion
1993 ;; Go back 2 bods, but ignore any bogus positions returned by
1994 ;; beginning-of-defun (i.e. open paren in column zero).
1995 (goto-char here)
1996 (let ((cnt 2))
1997 (while (not (or (bobp) (zerop cnt)))
1998 (c-beginning-of-defun-1)
1999 (if (eq (char-after) ?\{)
2000 (setq cnt (1- cnt)))))
2001 (point)))
2002
2003 (defun c-parse-state ()
2004 ;; Find and record all noteworthy parens between some good point
2005 ;; earlier in the file and point. That good point is at least the
2006 ;; beginning of the top-level construct we are in, or the beginning
2007 ;; of the preceding top-level construct if we aren't in one.
2008 ;;
2009 ;; The returned value is a list of the noteworthy parens with the
2010 ;; last one first. If an element in the list is an integer, it's
2011 ;; the position of an open paren which has not been closed before
2012 ;; the point. If an element is a cons, it gives the position of a
2013 ;; closed brace paren pair; the car is the start paren position and
2014 ;; the cdr is the position following the closing paren. Only the
2015 ;; last closed brace paren pair before each open paren and before
2016 ;; the point is recorded, and thus the state never contains two cons
2017 ;; elements in succession.
2018 ;;
2019 ;; Currently no characters which are given paren syntax with the
2020 ;; syntax-table property are recorded, i.e. angle bracket arglist
2021 ;; parens are never present here. Note that this might change.
2022 ;;
2023 ;; BUG: This function doesn't cope entirely well with unbalanced
2024 ;; parens in macros. E.g. in the following case the brace before
2025 ;; the macro isn't balanced with the one after it:
2026 ;;
2027 ;; {
2028 ;; #define X {
2029 ;; }
2030 ;;
2031 ;; This function might do hidden buffer changes.
2032
2033 (save-restriction
2034 (let* ((here (point))
2035 (here-bol (c-point 'bol))
2036 (c-macro-start (c-query-macro-start))
2037 (in-macro-start (or c-macro-start (point)))
2038 old-state last-pos brace-pair-open brace-pair-close
2039 pos save-pos)
2040 (c-invalidate-state-cache here)
2041
2042 ;; If the minimum position has changed due to narrowing then we
2043 ;; have to fix the tail of `c-state-cache' accordingly.
2044 (unless (= c-state-cache-start (point-min))
2045 (if (> (point-min) c-state-cache-start)
2046 ;; If point-min has moved forward then we just need to cut
2047 ;; off a bit of the tail.
2048 (let ((ptr (cons nil c-state-cache)) elem)
2049 (while (and (setq elem (car-safe (cdr ptr)))
2050 (>= (if (consp elem) (car elem) elem)
2051 (point-min)))
2052 (setq ptr (cdr ptr)))
2053 (when (consp ptr)
2054 (if (eq (cdr ptr) c-state-cache)
2055 (setq c-state-cache nil
2056 c-state-cache-good-pos 1)
2057 (setcdr ptr nil))))
2058 ;; If point-min has moved backward then we drop the state
2059 ;; completely. It's possible to do a better job here and
2060 ;; recalculate the top only.
2061 (setq c-state-cache nil
2062 c-state-cache-good-pos 1))
2063 (setq c-state-cache-start (point-min)))
2064
2065 ;; Get the latest position we know are directly inside the
2066 ;; closest containing paren of the cached state.
2067 (setq last-pos (and c-state-cache
2068 (if (consp (car c-state-cache))
2069 (cdr (car c-state-cache))
2070 (1+ (car c-state-cache)))))
2071 (if (or (not last-pos)
2072 (< last-pos c-state-cache-good-pos))
2073 (setq last-pos c-state-cache-good-pos)
2074 ;; Take the opportunity to move the cached good position
2075 ;; further down.
2076 (if (< last-pos here-bol)
2077 (setq c-state-cache-good-pos last-pos)))
2078
2079 ;; Check if `last-pos' is in a macro. If it is, and we're not
2080 ;; in the same macro, we must discard everything on
2081 ;; `c-state-cache' that is inside the macro before using it.
2082 (save-excursion
2083 (goto-char last-pos)
2084 (when (and (c-beginning-of-macro)
2085 (/= (point) in-macro-start))
2086 (c-invalidate-state-cache (point))
2087 ;; Set `last-pos' again just like above except that there's
2088 ;; no use looking at `c-state-cache-good-pos' here.
2089 (setq last-pos (if c-state-cache
2090 (if (consp (car c-state-cache))
2091 (cdr (car c-state-cache))
2092 (1+ (car c-state-cache)))
2093 1))))
2094
2095 ;; If we've moved very far from the last cached position then
2096 ;; it's probably better to redo it from scratch, otherwise we
2097 ;; might spend a lot of time searching from `last-pos' down to
2098 ;; here.
2099 (when (< last-pos (- here 20000))
2100 ;; First get the fallback start position. If it turns out
2101 ;; that it's so far back that the cached state is closer then
2102 ;; we'll keep it afterall.
2103 (setq pos (c-get-fallback-start-pos here))
2104 (if (<= pos last-pos)
2105 (setq pos nil)
2106 (setq last-pos nil
2107 c-state-cache nil
2108 c-state-cache-good-pos 1)))
2109
2110 ;; Find the start position for the forward search. (Can't
2111 ;; search in the backward direction since the point might be in
2112 ;; some kind of literal.)
2113
2114 (unless pos
2115 (setq old-state c-state-cache)
2116
2117 ;; There's a cached state with a containing paren. Pop off
2118 ;; the stale containing sexps from it by going forward out of
2119 ;; parens as far as possible.
2120 (narrow-to-region (point-min) here)
2121 (let (placeholder pair-beg)
2122 (while (and c-state-cache
2123 (setq placeholder
2124 (c-up-list-forward last-pos)))
2125 (setq last-pos placeholder)
2126 (if (consp (car c-state-cache))
2127 (setq pair-beg (car-safe (cdr c-state-cache))
2128 c-state-cache (cdr-safe (cdr c-state-cache)))
2129 (setq pair-beg (car c-state-cache)
2130 c-state-cache (cdr c-state-cache))))
2131
2132 (when (and pair-beg (eq (char-after pair-beg) ?{))
2133 ;; The last paren pair we moved out from was a brace
2134 ;; pair. Modify the state to record this as a closed
2135 ;; pair now.
2136 (if (consp (car-safe c-state-cache))
2137 (setq c-state-cache (cdr c-state-cache)))
2138 (setq c-state-cache (cons (cons pair-beg last-pos)
2139 c-state-cache))))
2140
2141 ;; Check if the preceding balanced paren is within a
2142 ;; macro; it should be ignored if we're outside the
2143 ;; macro. There's no need to check any further upwards;
2144 ;; if the macro contains an unbalanced opening paren then
2145 ;; we're smoked anyway.
2146 (when (and (<= (point) in-macro-start)
2147 (consp (car c-state-cache)))
2148 (save-excursion
2149 (goto-char (car (car c-state-cache)))
2150 (when (c-beginning-of-macro)
2151 (setq here (point)
2152 c-state-cache (cdr c-state-cache)))))
2153
2154 (unless (eq c-state-cache old-state)
2155 ;; Have to adjust the cached good position if state has been
2156 ;; popped off.
2157 (setq c-state-cache-good-pos
2158 (if c-state-cache
2159 (if (consp (car c-state-cache))
2160 (cdr (car c-state-cache))
2161 (1+ (car c-state-cache)))
2162 1)
2163 old-state c-state-cache))
2164
2165 (when c-state-cache
2166 (setq pos last-pos)))
2167
2168 ;; Get the fallback start position.
2169 (unless pos
2170 (setq pos (c-get-fallback-start-pos here)
2171 c-state-cache nil
2172 c-state-cache-good-pos 1))
2173
2174 (narrow-to-region (point-min) here)
2175
2176 (while pos
2177 (setq save-pos pos
2178 brace-pair-open nil)
2179
2180 ;; Find the balanced brace pairs. This loop is hot, so it
2181 ;; does ugly tricks to go faster.
2182 (c-safe
2183 (let (set-good-pos set-brace-pair)
2184 (while t
2185 (setq last-pos nil
2186 last-pos (scan-lists pos 1 -1)) ; Might signal.
2187 (setq pos (scan-lists last-pos 1 1) ; Might signal.
2188 set-good-pos (< pos here-bol)
2189 set-brace-pair (eq (char-before last-pos) ?{))
2190
2191 ;; Update the cached good position and record the brace
2192 ;; pair, whichever is applicable for the paren we've
2193 ;; just jumped over. But first check that it isn't
2194 ;; inside a macro and the point isn't inside the same
2195 ;; one.
2196 (when (and (or set-good-pos set-brace-pair)
2197 (or (>= pos in-macro-start)
2198 (save-excursion
2199 (goto-char pos)
2200 (not (c-beginning-of-macro)))))
2201 (if set-good-pos
2202 (setq c-state-cache-good-pos pos))
2203 (if set-brace-pair
2204 (setq brace-pair-open last-pos
2205 brace-pair-close pos))))))
2206
2207 ;; Record the last brace pair.
2208 (when brace-pair-open
2209 (let ((head (car-safe c-state-cache)))
2210 (if (consp head)
2211 (progn
2212 (setcar head (1- brace-pair-open))
2213 (setcdr head brace-pair-close))
2214 (setq c-state-cache (cons (cons (1- brace-pair-open)
2215 brace-pair-close)
2216 c-state-cache)))))
2217
2218 (if last-pos
2219 ;; Prepare to loop, but record the open paren only if it's
2220 ;; outside a macro or within the same macro as point, and
2221 ;; if it is a legitimate open paren and not some character
2222 ;; that got an open paren syntax-table property.
2223 (progn
2224 (setq pos last-pos)
2225 (when (and (or (>= last-pos in-macro-start)
2226 (save-excursion
2227 (goto-char last-pos)
2228 (not (c-beginning-of-macro))))
2229 ;; Check for known types of parens that we
2230 ;; want to record. The syntax table is not to
2231 ;; be trusted here since the caller might be
2232 ;; using e.g. `c++-template-syntax-table'.
2233 (memq (char-before last-pos) '(?{ ?\( ?\[)))
2234 (if (< last-pos here-bol)
2235 (setq c-state-cache-good-pos last-pos))
2236 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
2237
2238 (if (setq last-pos (c-up-list-forward pos))
2239 ;; Found a close paren without a corresponding opening
2240 ;; one. Maybe we didn't go back far enough, so try to
2241 ;; scan backward for the start paren and then start over.
2242 (progn
2243 (setq pos (c-up-list-backward pos)
2244 c-state-cache nil
2245 c-state-cache-good-pos c-state-cache-start)
2246 (when (or (not pos)
2247 ;; Emacs (up to at least 21.2) can get confused by
2248 ;; open parens in column zero inside comments: The
2249 ;; sexp functions can then misbehave and bring us
2250 ;; back to the same point again. Check this so that
2251 ;; we don't get an infinite loop.
2252 (>= pos save-pos))
2253 (setq pos last-pos
2254 c-parsing-error
2255 (format "Unbalanced close paren at line %d"
2256 (1+ (count-lines (point-min)
2257 (c-point 'bol last-pos)))))))
2258 (setq pos nil))))
2259
2260 ;;(message "c-parse-state: %S end: %S" c-state-cache c-state-cache-good-pos)
2261 c-state-cache)))
2262
2263 ;; Debug tool to catch cache inconsistencies.
2264 (defvar c-debug-parse-state nil)
2265 (unless (fboundp 'c-real-parse-state)
2266 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
2267 (cc-bytecomp-defun c-real-parse-state)
2268 (defun c-debug-parse-state ()
2269 (let ((res1 (c-real-parse-state)) res2)
2270 (let ((c-state-cache nil)
2271 (c-state-cache-start 1)
2272 (c-state-cache-good-pos 1))
2273 (setq res2 (c-real-parse-state)))
2274 (unless (equal res1 res2)
2275 ;; The cache can actually go further back due to the ad-hoc way
2276 ;; the first paren is found, so try to whack off a bit of its
2277 ;; start before complaining.
2278 (save-excursion
2279 (goto-char (or (c-least-enclosing-brace res2) (point)))
2280 (c-beginning-of-defun-1)
2281 (while (not (or (bobp) (eq (char-after) ?{)))
2282 (c-beginning-of-defun-1))
2283 (unless (equal (c-whack-state-before (point) res1) res2)
2284 (message (concat "c-parse-state inconsistency: "
2285 "using cache: %s, from scratch: %s")
2286 res1 res2))))
2287 res1))
2288 (defun c-toggle-parse-state-debug (&optional arg)
2289 (interactive "P")
2290 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
2291 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
2292 'c-debug-parse-state
2293 'c-real-parse-state)))
2294 (c-keep-region-active))
2295 (when c-debug-parse-state
2296 (c-toggle-parse-state-debug 1))
2297
2298 (defun c-whack-state-before (bufpos paren-state)
2299 ;; Whack off any state information from PAREN-STATE which lies
2300 ;; before BUFPOS. Not destructive on PAREN-STATE.
2301 (let* ((newstate (list nil))
2302 (ptr newstate)
2303 car)
2304 (while paren-state
2305 (setq car (car paren-state)
2306 paren-state (cdr paren-state))
2307 (if (< (if (consp car) (car car) car) bufpos)
2308 (setq paren-state nil)
2309 (setcdr ptr (list car))
2310 (setq ptr (cdr ptr))))
2311 (cdr newstate)))
2312
2313 (defun c-whack-state-after (bufpos paren-state)
2314 ;; Whack off any state information from PAREN-STATE which lies at or
2315 ;; after BUFPOS. Not destructive on PAREN-STATE.
2316 (catch 'done
2317 (while paren-state
2318 (let ((car (car paren-state)))
2319 (if (consp car)
2320 ;; just check the car, because in a balanced brace
2321 ;; expression, it must be impossible for the corresponding
2322 ;; close brace to be before point, but the open brace to
2323 ;; be after.
2324 (if (<= bufpos (car car))
2325 nil ; whack it off
2326 (if (< bufpos (cdr car))
2327 ;; its possible that the open brace is before
2328 ;; bufpos, but the close brace is after. In that
2329 ;; case, convert this to a non-cons element. The
2330 ;; rest of the state is before bufpos, so we're
2331 ;; done.
2332 (throw 'done (cons (car car) (cdr paren-state)))
2333 ;; we know that both the open and close braces are
2334 ;; before bufpos, so we also know that everything else
2335 ;; on state is before bufpos.
2336 (throw 'done paren-state)))
2337 (if (<= bufpos car)
2338 nil ; whack it off
2339 ;; it's before bufpos, so everything else should too.
2340 (throw 'done paren-state)))
2341 (setq paren-state (cdr paren-state)))
2342 nil)))
2343
2344 (defun c-most-enclosing-brace (paren-state &optional bufpos)
2345 ;; Return the bufpos of the innermost enclosing open paren before
2346 ;; bufpos, or nil if none was found.
2347 (let (enclosingp)
2348 (or bufpos (setq bufpos 134217727))
2349 (while paren-state
2350 (setq enclosingp (car paren-state)
2351 paren-state (cdr paren-state))
2352 (if (or (consp enclosingp)
2353 (>= enclosingp bufpos))
2354 (setq enclosingp nil)
2355 (setq paren-state nil)))
2356 enclosingp))
2357
2358 (defun c-least-enclosing-brace (paren-state)
2359 ;; Return the bufpos of the outermost enclosing open paren, or nil
2360 ;; if none was found.
2361 (let (pos elem)
2362 (while paren-state
2363 (setq elem (car paren-state)
2364 paren-state (cdr paren-state))
2365 (if (integerp elem)
2366 (setq pos elem)))
2367 pos))
2368
2369 (defun c-safe-position (bufpos paren-state)
2370 ;; Return the closest "safe" position recorded on PAREN-STATE that
2371 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
2372 ;; contain any. Return nil if BUFPOS is nil, which is useful to
2373 ;; find the closest limit before a given limit that might be nil.
2374 ;;
2375 ;; A "safe" position is a position at or after a recorded open
2376 ;; paren, or after a recorded close paren. The returned position is
2377 ;; thus either the first position after a close brace, or the first
2378 ;; position after an enclosing paren, or at the enclosing paren in
2379 ;; case BUFPOS is immediately after it.
2380 (when bufpos
2381 (let (elem)
2382 (catch 'done
2383 (while paren-state
2384 (setq elem (car paren-state))
2385 (if (consp elem)
2386 (cond ((< (cdr elem) bufpos)
2387 (throw 'done (cdr elem)))
2388 ((< (car elem) bufpos)
2389 ;; See below.
2390 (throw 'done (min (1+ (car elem)) bufpos))))
2391 (if (< elem bufpos)
2392 ;; elem is the position at and not after the opening paren, so
2393 ;; we can go forward one more step unless it's equal to
2394 ;; bufpos. This is useful in some cases avoid an extra paren
2395 ;; level between the safe position and bufpos.
2396 (throw 'done (min (1+ elem) bufpos))))
2397 (setq paren-state (cdr paren-state)))))))
2398
2399 (defun c-beginning-of-syntax ()
2400 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2401 ;; goes to the closest previous point that is known to be outside
2402 ;; any string literal or comment. `c-state-cache' is used if it has
2403 ;; a position in the vicinity.
2404 (let* ((paren-state c-state-cache)
2405 elem
2406
2407 (pos (catch 'done
2408 ;; Note: Similar code in `c-safe-position'. The
2409 ;; difference is that we accept a safe position at
2410 ;; the point and don't bother to go forward past open
2411 ;; parens.
2412 (while paren-state
2413 (setq elem (car paren-state))
2414 (if (consp elem)
2415 (cond ((<= (cdr elem) (point))
2416 (throw 'done (cdr elem)))
2417 ((<= (car elem) (point))
2418 (throw 'done (car elem))))
2419 (if (<= elem (point))
2420 (throw 'done elem)))
2421 (setq paren-state (cdr paren-state)))
2422 (point-min))))
2423
2424 (if (> pos (- (point) 4000))
2425 (goto-char pos)
2426 ;; The position is far back. Try `c-beginning-of-defun-1'
2427 ;; (although we can't be entirely sure it will go to a position
2428 ;; outside a comment or string in current emacsen). FIXME:
2429 ;; Consult `syntax-ppss' here.
2430 (c-beginning-of-defun-1)
2431 (if (< (point) pos)
2432 (goto-char pos)))))
2433
2434 \f
2435 ;; Tools for scanning identifiers and other tokens.
2436
2437 (defun c-on-identifier ()
2438 "Return non-nil if the point is on or directly after an identifier.
2439 Keywords are recognized and not considered identifiers. If an
2440 identifier is detected, the returned value is its starting position.
2441 If an identifier ends at the point and another begins at it \(can only
2442 happen in Pike) then the point for the preceding one is returned.
2443
2444 Note that this function might do hidden buffer changes. See the
2445 comment at the start of cc-engine.el for more info."
2446
2447 ;; FIXME: Shouldn't this function handle "operator" in C++?
2448
2449 (save-excursion
2450 (skip-syntax-backward "w_")
2451
2452 (or
2453
2454 ;; Check for a normal (non-keyword) identifier.
2455 (and (looking-at c-symbol-start)
2456 (not (looking-at c-keywords-regexp))
2457 (point))
2458
2459 (when (c-major-mode-is 'pike-mode)
2460 ;; Handle the `<operator> syntax in Pike.
2461 (let ((pos (point)))
2462 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2463 (and (if (< (skip-chars-backward "`") 0)
2464 t
2465 (goto-char pos)
2466 (eq (char-after) ?\`))
2467 (looking-at c-symbol-key)
2468 (>= (match-end 0) pos)
2469 (point))))
2470
2471 ;; Handle the "operator +" syntax in C++.
2472 (when (and c-overloadable-operators-regexp
2473 (= (c-backward-token-2 0) 0))
2474
2475 (cond ((and (looking-at c-overloadable-operators-regexp)
2476 (or (not c-opt-op-identifier-prefix)
2477 (and (= (c-backward-token-2 1) 0)
2478 (looking-at c-opt-op-identifier-prefix))))
2479 (point))
2480
2481 ((save-excursion
2482 (and c-opt-op-identifier-prefix
2483 (looking-at c-opt-op-identifier-prefix)
2484 (= (c-forward-token-2 1) 0)
2485 (looking-at c-overloadable-operators-regexp)))
2486 (point))))
2487
2488 )))
2489
2490 (defsubst c-simple-skip-symbol-backward ()
2491 ;; If the point is at the end of a symbol then skip backward to the
2492 ;; beginning of it. Don't move otherwise. Return non-nil if point
2493 ;; moved.
2494 ;;
2495 ;; This function might do hidden buffer changes.
2496 (or (< (skip-syntax-backward "w_") 0)
2497 (and (c-major-mode-is 'pike-mode)
2498 ;; Handle the `<operator> syntax in Pike.
2499 (let ((pos (point)))
2500 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2501 (< (skip-chars-backward "`") 0)
2502 (looking-at c-symbol-key)
2503 (>= (match-end 0) pos))
2504 t
2505 (goto-char pos)
2506 nil)))))
2507
2508 (defun c-beginning-of-current-token (&optional back-limit)
2509 ;; Move to the beginning of the current token. Do not move if not
2510 ;; in the middle of one. BACK-LIMIT may be used to bound the
2511 ;; backward search; if given it's assumed to be at the boundary
2512 ;; between two tokens. Return non-nil if the point is move, nil
2513 ;; otherwise.
2514 ;;
2515 ;; This function might do hidden buffer changes.
2516 (let ((start (point)))
2517 (if (looking-at "\\w\\|\\s_")
2518 (skip-syntax-backward "w_" back-limit)
2519 (when (< (skip-syntax-backward ".()" back-limit) 0)
2520 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2521 (match-end 0))
2522 ;; `c-nonsymbol-token-regexp' should always match
2523 ;; since we've skipped backward over punctuator
2524 ;; or paren syntax, but consume one char in case
2525 ;; it doesn't so that we don't leave point before
2526 ;; some earlier incorrect token.
2527 (1+ (point)))))
2528 (if (<= pos start)
2529 (goto-char pos))))))
2530 (< (point) start)))
2531
2532 (defun c-end-of-current-token (&optional back-limit)
2533 ;; Move to the end of the current token. Do not move if not in the
2534 ;; middle of one. BACK-LIMIT may be used to bound the backward
2535 ;; search; if given it's assumed to be at the boundary between two
2536 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2537 ;;
2538 ;; This function might do hidden buffer changes.
2539 (let ((start (point)))
2540 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2541 (skip-syntax-forward "w_"))
2542 ((< (skip-syntax-backward ".()" back-limit) 0)
2543 (while (progn
2544 (if (looking-at c-nonsymbol-token-regexp)
2545 (goto-char (match-end 0))
2546 ;; `c-nonsymbol-token-regexp' should always match since
2547 ;; we've skipped backward over punctuator or paren
2548 ;; syntax, but move forward in case it doesn't so that
2549 ;; we don't leave point earlier than we started with.
2550 (forward-char))
2551 (< (point) start)))))
2552 (> (point) start)))
2553
2554 (defconst c-jump-syntax-balanced
2555 (if (memq 'gen-string-delim c-emacs-features)
2556 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2557 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2558
2559 (defconst c-jump-syntax-unbalanced
2560 (if (memq 'gen-string-delim c-emacs-features)
2561 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2562 "\\w\\|\\s_\\|\\s\""))
2563
2564 (defun c-forward-token-2 (&optional count balanced limit)
2565 "Move forward by tokens.
2566 A token is defined as all symbols and identifiers which aren't
2567 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2568 treated properly). Point is always either left at the beginning of a
2569 token or not moved at all. COUNT specifies the number of tokens to
2570 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2571 moves to the next token beginning only if not already at one. If
2572 BALANCED is true, move over balanced parens, otherwise move into them.
2573 Also, if BALANCED is true, never move out of an enclosing paren.
2574
2575 LIMIT sets the limit for the movement and defaults to the point limit.
2576 The case when LIMIT is set in the middle of a token, comment or macro
2577 is handled correctly, i.e. the point won't be left there.
2578
2579 Return the number of tokens left to move \(positive or negative). If
2580 BALANCED is true, a move over a balanced paren counts as one. Note
2581 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2582 be returned. Thus, a return value of 0 guarantees that point is at
2583 the requested position and a return value less \(without signs) than
2584 COUNT guarantees that point is at the beginning of some token.
2585
2586 Note that this function might do hidden buffer changes. See the
2587 comment at the start of cc-engine.el for more info."
2588
2589 (or count (setq count 1))
2590 (if (< count 0)
2591 (- (c-backward-token-2 (- count) balanced limit))
2592
2593 (let ((jump-syntax (if balanced
2594 c-jump-syntax-balanced
2595 c-jump-syntax-unbalanced))
2596 (last (point))
2597 (prev (point)))
2598
2599 (if (zerop count)
2600 ;; If count is zero we should jump if in the middle of a token.
2601 (c-end-of-current-token))
2602
2603 (save-restriction
2604 (if limit (narrow-to-region (point-min) limit))
2605 (if (/= (point)
2606 (progn (c-forward-syntactic-ws) (point)))
2607 ;; Skip whitespace. Count this as a move if we did in
2608 ;; fact move.
2609 (setq count (max (1- count) 0)))
2610
2611 (if (eobp)
2612 ;; Moved out of bounds. Make sure the returned count isn't zero.
2613 (progn
2614 (if (zerop count) (setq count 1))
2615 (goto-char last))
2616
2617 ;; Use `condition-case' to avoid having the limit tests
2618 ;; inside the loop.
2619 (condition-case nil
2620 (while (and
2621 (> count 0)
2622 (progn
2623 (setq last (point))
2624 (cond ((looking-at jump-syntax)
2625 (goto-char (scan-sexps (point) 1))
2626 t)
2627 ((looking-at c-nonsymbol-token-regexp)
2628 (goto-char (match-end 0))
2629 t)
2630 ;; `c-nonsymbol-token-regexp' above should always
2631 ;; match if there are correct tokens. Try to
2632 ;; widen to see if the limit was set in the
2633 ;; middle of one, else fall back to treating
2634 ;; the offending thing as a one character token.
2635 ((and limit
2636 (save-restriction
2637 (widen)
2638 (looking-at c-nonsymbol-token-regexp)))
2639 nil)
2640 (t
2641 (forward-char)
2642 t))))
2643 (c-forward-syntactic-ws)
2644 (setq prev last
2645 count (1- count)))
2646 (error (goto-char last)))
2647
2648 (when (eobp)
2649 (goto-char prev)
2650 (setq count (1+ count)))))
2651
2652 count)))
2653
2654 (defun c-backward-token-2 (&optional count balanced limit)
2655 "Move backward by tokens.
2656 See `c-forward-token-2' for details."
2657
2658 (or count (setq count 1))
2659 (if (< count 0)
2660 (- (c-forward-token-2 (- count) balanced limit))
2661
2662 (or limit (setq limit (point-min)))
2663 (let ((jump-syntax (if balanced
2664 c-jump-syntax-balanced
2665 c-jump-syntax-unbalanced))
2666 (last (point)))
2667
2668 (if (zerop count)
2669 ;; The count is zero so try to skip to the beginning of the
2670 ;; current token.
2671 (if (> (point)
2672 (progn (c-beginning-of-current-token) (point)))
2673 (if (< (point) limit)
2674 ;; The limit is inside the same token, so return 1.
2675 (setq count 1))
2676
2677 ;; We're not in the middle of a token. If there's
2678 ;; whitespace after the point then we must move backward,
2679 ;; so set count to 1 in that case.
2680 (and (looking-at c-syntactic-ws-start)
2681 ;; If we're looking at a '#' that might start a cpp
2682 ;; directive then we have to do a more elaborate check.
2683 (or (/= (char-after) ?#)
2684 (not c-opt-cpp-prefix)
2685 (save-excursion
2686 (and (= (point)
2687 (progn (beginning-of-line)
2688 (looking-at "[ \t]*")
2689 (match-end 0)))
2690 (or (bobp)
2691 (progn (backward-char)
2692 (not (eq (char-before) ?\\)))))))
2693 (setq count 1))))
2694
2695 ;; Use `condition-case' to avoid having to check for buffer
2696 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2697 (condition-case nil
2698 (while (and
2699 (> count 0)
2700 (progn
2701 (c-backward-syntactic-ws)
2702 (backward-char)
2703 (if (looking-at jump-syntax)
2704 (goto-char (scan-sexps (1+ (point)) -1))
2705 ;; This can be very inefficient if there's a long
2706 ;; sequence of operator tokens without any separation.
2707 ;; That doesn't happen in practice, anyway.
2708 (c-beginning-of-current-token))
2709 (>= (point) limit)))
2710 (setq last (point)
2711 count (1- count)))
2712 (error (goto-char last)))
2713
2714 (if (< (point) limit)
2715 (goto-char last))
2716
2717 count)))
2718
2719 (defun c-forward-token-1 (&optional count balanced limit)
2720 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2721 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2722 characters are jumped over character by character. This function is
2723 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2724 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2725 (c-forward-token-2 count balanced limit)))
2726
2727 (defun c-backward-token-1 (&optional count balanced limit)
2728 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2729 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2730 characters are jumped over character by character. This function is
2731 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2732 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2733 (c-backward-token-2 count balanced limit)))
2734
2735 \f
2736 ;; Tools for doing searches restricted to syntactically relevant text.
2737
2738 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2739 paren-level not-inside-token
2740 lookbehind-submatch)
2741 "Like `re-search-forward', but only report matches that are found
2742 in syntactically significant text. I.e. matches in comments, macros
2743 or string literals are ignored. The start point is assumed to be
2744 outside any comment, macro or string literal, or else the content of
2745 that region is taken as syntactically significant text.
2746
2747 If PAREN-LEVEL is non-nil, an additional restriction is added to
2748 ignore matches in nested paren sexps. The search will also not go
2749 outside the current list sexp, which has the effect that if the point
2750 should be moved to BOUND when no match is found \(i.e. NOERROR is
2751 neither nil nor t), then it will be at the closing paren if the end of
2752 the current list sexp is encountered first.
2753
2754 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2755 ignored. Things like multicharacter operators and special symbols
2756 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2757 constants.
2758
2759 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2760 subexpression in REGEXP. The end of that submatch is used as the
2761 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2762 isn't used or if that subexpression didn't match then the start
2763 position of the whole match is used instead. The \"look behind\"
2764 subexpression is never tested before the starting position, so it
2765 might be a good idea to include \\=\\= as a match alternative in it.
2766
2767 Optimization note: Matches might be missed if the \"look behind\"
2768 subexpression can match the end of nonwhite syntactic whitespace,
2769 i.e. the end of comments or cpp directives. This since the function
2770 skips over such things before resuming the search. It's on the other
2771 hand not safe to assume that the \"look behind\" subexpression never
2772 matches syntactic whitespace.
2773
2774 Bug: Unbalanced parens inside cpp directives are currently not handled
2775 correctly \(i.e. they don't get ignored as they should) when
2776 PAREN-LEVEL is set.
2777
2778 Note that this function might do hidden buffer changes. See the
2779 comment at the start of cc-engine.el for more info."
2780
2781 (or bound (setq bound (point-max)))
2782 (if paren-level (setq paren-level -1))
2783
2784 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2785
2786 (let ((start (point))
2787 tmp
2788 ;; Start position for the last search.
2789 search-pos
2790 ;; The `parse-partial-sexp' state between the start position
2791 ;; and the point.
2792 state
2793 ;; The current position after the last state update. The next
2794 ;; `parse-partial-sexp' continues from here.
2795 (state-pos (point))
2796 ;; The position at which to check the state and the state
2797 ;; there. This is separate from `state-pos' since we might
2798 ;; need to back up before doing the next search round.
2799 check-pos check-state
2800 ;; Last position known to end a token.
2801 (last-token-end-pos (point-min))
2802 ;; Set when a valid match is found.
2803 found)
2804
2805 (condition-case err
2806 (while
2807 (and
2808 (progn
2809 (setq search-pos (point))
2810 (re-search-forward regexp bound noerror))
2811
2812 (progn
2813 (setq state (parse-partial-sexp
2814 state-pos (match-beginning 0) paren-level nil state)
2815 state-pos (point))
2816 (if (setq check-pos (and lookbehind-submatch
2817 (or (not paren-level)
2818 (>= (car state) 0))
2819 (match-end lookbehind-submatch)))
2820 (setq check-state (parse-partial-sexp
2821 state-pos check-pos paren-level nil state))
2822 (setq check-pos state-pos
2823 check-state state))
2824
2825 ;; NOTE: If we got a look behind subexpression and get
2826 ;; an insignificant match in something that isn't
2827 ;; syntactic whitespace (i.e. strings or in nested
2828 ;; parentheses), then we can never skip more than a
2829 ;; single character from the match start position
2830 ;; (i.e. `state-pos' here) before continuing the
2831 ;; search. That since the look behind subexpression
2832 ;; might match the end of the insignificant region in
2833 ;; the next search.
2834
2835 (cond
2836 ((elt check-state 7)
2837 ;; Match inside a line comment. Skip to eol. Use
2838 ;; `re-search-forward' instead of `skip-chars-forward' to get
2839 ;; the right bound behavior.
2840 (re-search-forward "[\n\r]" bound noerror))
2841
2842 ((elt check-state 4)
2843 ;; Match inside a block comment. Skip to the '*/'.
2844 (search-forward "*/" bound noerror))
2845
2846 ((and (not (elt check-state 5))
2847 (eq (char-before check-pos) ?/)
2848 (not (c-get-char-property (1- check-pos) 'syntax-table))
2849 (memq (char-after check-pos) '(?/ ?*)))
2850 ;; Match in the middle of the opener of a block or line
2851 ;; comment.
2852 (if (= (char-after check-pos) ?/)
2853 (re-search-forward "[\n\r]" bound noerror)
2854 (search-forward "*/" bound noerror)))
2855
2856 ;; The last `parse-partial-sexp' above might have
2857 ;; stopped short of the real check position if the end
2858 ;; of the current sexp was encountered in paren-level
2859 ;; mode. The checks above are always false in that
2860 ;; case, and since they can do better skipping in
2861 ;; lookbehind-submatch mode, we do them before
2862 ;; checking the paren level.
2863
2864 ((and paren-level
2865 (/= (setq tmp (car check-state)) 0))
2866 ;; Check the paren level first since we're short of the
2867 ;; syntactic checking position if the end of the
2868 ;; current sexp was encountered by `parse-partial-sexp'.
2869 (if (> tmp 0)
2870
2871 ;; Inside a nested paren sexp.
2872 (if lookbehind-submatch
2873 ;; See the NOTE above.
2874 (progn (goto-char state-pos) t)
2875 ;; Skip out of the paren quickly.
2876 (setq state (parse-partial-sexp state-pos bound 0 nil state)
2877 state-pos (point)))
2878
2879 ;; Have exited the current paren sexp.
2880 (if noerror
2881 (progn
2882 ;; The last `parse-partial-sexp' call above
2883 ;; has left us just after the closing paren
2884 ;; in this case, so we can modify the bound
2885 ;; to leave the point at the right position
2886 ;; upon return.
2887 (setq bound (1- (point)))
2888 nil)
2889 (signal 'search-failed (list regexp)))))
2890
2891 ((setq tmp (elt check-state 3))
2892 ;; Match inside a string.
2893 (if (or lookbehind-submatch
2894 (not (integerp tmp)))
2895 ;; See the NOTE above.
2896 (progn (goto-char state-pos) t)
2897 ;; Skip to the end of the string before continuing.
2898 (let ((ender (make-string 1 tmp)) (continue t))
2899 (while (if (search-forward ender bound noerror)
2900 (progn
2901 (setq state (parse-partial-sexp
2902 state-pos (point) nil nil state)
2903 state-pos (point))
2904 (elt state 3))
2905 (setq continue nil)))
2906 continue)))
2907
2908 ((save-excursion
2909 (save-match-data
2910 (c-beginning-of-macro start)))
2911 ;; Match inside a macro. Skip to the end of it.
2912 (c-end-of-macro)
2913 (cond ((<= (point) bound) t)
2914 (noerror nil)
2915 (t (signal 'search-failed (list regexp)))))
2916
2917 ((and not-inside-token
2918 (or (< check-pos last-token-end-pos)
2919 (< check-pos
2920 (save-excursion
2921 (goto-char check-pos)
2922 (save-match-data
2923 (c-end-of-current-token last-token-end-pos))
2924 (setq last-token-end-pos (point))))))
2925 ;; Inside a token.
2926 (if lookbehind-submatch
2927 ;; See the NOTE above.
2928 (goto-char state-pos)
2929 (goto-char (min last-token-end-pos bound))))
2930
2931 (t
2932 ;; A real match.
2933 (setq found t)
2934 nil)))
2935
2936 ;; Should loop to search again, but take care to avoid
2937 ;; looping on the same spot.
2938 (or (/= search-pos (point))
2939 (if (= (point) bound)
2940 (if noerror
2941 nil
2942 (signal 'search-failed (list regexp)))
2943 (forward-char)
2944 t))))
2945
2946 (error
2947 (goto-char start)
2948 (signal (car err) (cdr err))))
2949
2950 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2951
2952 (if found
2953 (progn
2954 (goto-char (match-end 0))
2955 (match-end 0))
2956
2957 ;; Search failed. Set point as appropriate.
2958 (if (eq noerror t)
2959 (goto-char start)
2960 (goto-char bound))
2961 nil)))
2962
2963 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
2964 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2965 i.e. don't stop at positions inside syntactic whitespace or string
2966 literals. Preprocessor directives are also ignored, with the exception
2967 of the one that the point starts within, if any. If LIMIT is given,
2968 it's assumed to be at a syntactically relevant position.
2969
2970 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
2971 sexps, and the search will also not go outside the current paren sexp.
2972 However, if LIMIT or the buffer limit is reached inside a nested paren
2973 then the point will be left at the limit.
2974
2975 Non-nil is returned if the point moved, nil otherwise.
2976
2977 Note that this function might do hidden buffer changes. See the
2978 comment at the start of cc-engine.el for more info."
2979
2980 (let ((start (point))
2981 state
2982 ;; A list of syntactically relevant positions in descending
2983 ;; order. It's used to avoid scanning repeatedly over
2984 ;; potentially large regions with `parse-partial-sexp' to verify
2985 ;; each position.
2986 safe-pos-list
2987 ;; The position at the beginning of `safe-pos-list'.
2988 safe-pos
2989 ;; The result from `c-beginning-of-macro' at the start position or the
2990 ;; start position itself if it isn't within a macro. Evaluated on
2991 ;; demand.
2992 start-macro-beg
2993 ;; The earliest position after the current one with the same paren
2994 ;; level. Used only when `paren-level' is set.
2995 (paren-level-pos (point)))
2996
2997 (while (progn
2998 (while (and
2999 (< (skip-chars-backward skip-chars limit) 0)
3000
3001 ;; Use `parse-partial-sexp' from a safe position down to
3002 ;; the point to check if it's outside comments and
3003 ;; strings.
3004 (let ((pos (point)) state-2 pps-end-pos)
3005 ;; Pick a safe position as close to the point as
3006 ;; possible.
3007 ;;
3008 ;; FIXME: Consult `syntax-ppss' here if our
3009 ;; cache doesn't give a good position.
3010 (while (and safe-pos-list
3011 (> (car safe-pos-list) (point)))
3012 (setq safe-pos-list (cdr safe-pos-list)))
3013 (unless (setq safe-pos (car-safe safe-pos-list))
3014 (setq safe-pos (max (or (c-safe-position
3015 (point) (or c-state-cache
3016 (c-parse-state)))
3017 0)
3018 (point-min))
3019 safe-pos-list (list safe-pos)))
3020
3021 ;; Cache positions along the way to use if we have to
3022 ;; back up more. We cache every closing paren on the
3023 ;; same level. If the paren cache is relevant in this
3024 ;; region then we're typically already on the same
3025 ;; level as the target position. Note that we might
3026 ;; cache positions after opening parens in case
3027 ;; safe-pos is in a nested list. That's both uncommon
3028 ;; and harmless.
3029 (while (progn
3030 (setq state (parse-partial-sexp
3031 safe-pos pos 0))
3032 (< (point) pos))
3033 (setq safe-pos (point)
3034 safe-pos-list (cons safe-pos safe-pos-list)))
3035
3036 (cond
3037 ((or (elt state 3) (elt state 4))
3038 ;; Inside string or comment. Continue search at the
3039 ;; beginning of it.
3040 (goto-char (elt state 8))
3041 t)
3042
3043 ((and paren-level
3044 (save-excursion
3045 (setq state-2 (parse-partial-sexp
3046 pos paren-level-pos -1)
3047 pps-end-pos (point))
3048 (/= (car state-2) 0)))
3049 ;; Not at the right level.
3050
3051 (if (and (< (car state-2) 0)
3052 ;; We stop above if we go out of a paren.
3053 ;; Now check whether it precedes or is
3054 ;; nested in the starting sexp.
3055 (save-excursion
3056 (setq state-2
3057 (parse-partial-sexp
3058 pps-end-pos paren-level-pos
3059 nil nil state-2))
3060 (< (car state-2) 0)))
3061
3062 ;; We've stopped short of the starting position
3063 ;; so the hit was inside a nested list. Go up
3064 ;; until we are at the right level.
3065 (condition-case nil
3066 (progn
3067 (goto-char (scan-lists pos -1
3068 (- (car state-2))))
3069 (setq paren-level-pos (point))
3070 (if (and limit (>= limit paren-level-pos))
3071 (progn
3072 (goto-char limit)
3073 nil)
3074 t))
3075 (error
3076 (goto-char (or limit (point-min)))
3077 nil))
3078
3079 ;; The hit was outside the list at the start
3080 ;; position. Go to the start of the list and exit.
3081 (goto-char (1+ (elt state-2 1)))
3082 nil))
3083
3084 ((c-beginning-of-macro limit)
3085 ;; Inside a macro.
3086 (if (< (point)
3087 (or start-macro-beg
3088 (setq start-macro-beg
3089 (save-excursion
3090 (goto-char start)
3091 (c-beginning-of-macro limit)
3092 (point)))))
3093 t
3094
3095 ;; It's inside the same macro we started in so it's
3096 ;; a relevant match.
3097 (goto-char pos)
3098 nil)))))
3099
3100 ;; If the state contains the start of the containing sexp we
3101 ;; cache that position too, so that parse-partial-sexp in the
3102 ;; next run has a bigger chance of starting at the same level
3103 ;; as the target position and thus will get more good safe
3104 ;; positions into the list.
3105 (if (elt state 1)
3106 (setq safe-pos (1+ (elt state 1))
3107 safe-pos-list (cons safe-pos safe-pos-list))))
3108
3109 (> (point)
3110 (progn
3111 ;; Skip syntactic ws afterwards so that we don't stop at the
3112 ;; end of a comment if `skip-chars' is something like "^/".
3113 (c-backward-syntactic-ws)
3114 (point)))))
3115
3116 ;; We might want to extend this with more useful return values in
3117 ;; the future.
3118 (/= (point) start)))
3119
3120 ;; The following is an alternative implementation of
3121 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3122 ;; track of the syntactic context. It turned out to be generally
3123 ;; slower than the one above which uses forward checks from earlier
3124 ;; safe positions.
3125 ;;
3126 ;;(defconst c-ssb-stop-re
3127 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3128 ;; ;; stop at to avoid going into comments and literals.
3129 ;; (concat
3130 ;; ;; Match comment end syntax and string literal syntax. Also match
3131 ;; ;; '/' for block comment endings (not covered by comment end
3132 ;; ;; syntax).
3133 ;; "\\s>\\|/\\|\\s\""
3134 ;; (if (memq 'gen-string-delim c-emacs-features)
3135 ;; "\\|\\s|"
3136 ;; "")
3137 ;; (if (memq 'gen-comment-delim c-emacs-features)
3138 ;; "\\|\\s!"
3139 ;; "")))
3140 ;;
3141 ;;(defconst c-ssb-stop-paren-re
3142 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3143 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3144 ;;
3145 ;;(defconst c-ssb-sexp-end-re
3146 ;; ;; Regexp matching the ending syntax of a complex sexp.
3147 ;; (concat c-string-limit-regexp "\\|\\s)"))
3148 ;;
3149 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3150 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3151 ;;i.e. don't stop at positions inside syntactic whitespace or string
3152 ;;literals. Preprocessor directives are also ignored. However, if the
3153 ;;point is within a comment, string literal or preprocessor directory to
3154 ;;begin with, its contents is treated as syntactically relevant chars.
3155 ;;If LIMIT is given, it limits the backward search and the point will be
3156 ;;left there if no earlier position is found.
3157 ;;
3158 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3159 ;;sexps, and the search will also not go outside the current paren sexp.
3160 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3161 ;;then the point will be left at the limit.
3162 ;;
3163 ;;Non-nil is returned if the point moved, nil otherwise.
3164 ;;
3165 ;;Note that this function might do hidden buffer changes. See the
3166 ;;comment at the start of cc-engine.el for more info."
3167 ;;
3168 ;; (save-restriction
3169 ;; (when limit
3170 ;; (narrow-to-region limit (point-max)))
3171 ;;
3172 ;; (let ((start (point)))
3173 ;; (catch 'done
3174 ;; (while (let ((last-pos (point))
3175 ;; (stop-pos (progn
3176 ;; (skip-chars-backward skip-chars)
3177 ;; (point))))
3178 ;;
3179 ;; ;; Skip back over the same region as
3180 ;; ;; `skip-chars-backward' above, but keep to
3181 ;; ;; syntactically relevant positions.
3182 ;; (goto-char last-pos)
3183 ;; (while (and
3184 ;; ;; `re-search-backward' with a single char regexp
3185 ;; ;; should be fast.
3186 ;; (re-search-backward
3187 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3188 ;; stop-pos 'move)
3189 ;;
3190 ;; (progn
3191 ;; (cond
3192 ;; ((looking-at "\\s(")
3193 ;; ;; `paren-level' is set and we've found the
3194 ;; ;; start of the containing paren.
3195 ;; (forward-char)
3196 ;; (throw 'done t))
3197 ;;
3198 ;; ((looking-at c-ssb-sexp-end-re)
3199 ;; ;; We're at the end of a string literal or paren
3200 ;; ;; sexp (if `paren-level' is set).
3201 ;; (forward-char)
3202 ;; (condition-case nil
3203 ;; (c-backward-sexp)
3204 ;; (error
3205 ;; (goto-char limit)
3206 ;; (throw 'done t))))
3207 ;;
3208 ;; (t
3209 ;; (forward-char)
3210 ;; ;; At the end of some syntactic ws or possibly
3211 ;; ;; after a plain '/' operator.
3212 ;; (let ((pos (point)))
3213 ;; (c-backward-syntactic-ws)
3214 ;; (if (= pos (point))
3215 ;; ;; Was a plain '/' operator. Go past it.
3216 ;; (backward-char)))))
3217 ;;
3218 ;; (> (point) stop-pos))))
3219 ;;
3220 ;; ;; Now the point is either at `stop-pos' or at some
3221 ;; ;; position further back if `stop-pos' was at a
3222 ;; ;; syntactically irrelevant place.
3223 ;;
3224 ;; ;; Skip additional syntactic ws so that we don't stop
3225 ;; ;; at the end of a comment if `skip-chars' is
3226 ;; ;; something like "^/".
3227 ;; (c-backward-syntactic-ws)
3228 ;;
3229 ;; (< (point) stop-pos))))
3230 ;;
3231 ;; ;; We might want to extend this with more useful return values
3232 ;; ;; in the future.
3233 ;; (/= (point) start))))
3234
3235 \f
3236 ;; Tools for handling comments and string literals.
3237
3238 (defun c-slow-in-literal (&optional lim detect-cpp)
3239 "Return the type of literal point is in, if any.
3240 The return value is `c' if in a C-style comment, `c++' if in a C++
3241 style comment, `string' if in a string literal, `pound' if DETECT-CPP
3242 is non-nil and in a preprocessor line, or nil if somewhere else.
3243 Optional LIM is used as the backward limit of the search. If omitted,
3244 or nil, `c-beginning-of-defun' is used.
3245
3246 The last point calculated is cached if the cache is enabled, i.e. if
3247 `c-in-literal-cache' is bound to a two element vector.
3248
3249 Note that this function might do hidden buffer changes. See the
3250 comment at the start of cc-engine.el for more info."
3251
3252 (if (and (vectorp c-in-literal-cache)
3253 (= (point) (aref c-in-literal-cache 0)))
3254 (aref c-in-literal-cache 1)
3255 (let ((rtn (save-excursion
3256 (let* ((pos (point))
3257 (lim (or lim (progn
3258 (c-beginning-of-syntax)
3259 (point))))
3260 (state (parse-partial-sexp lim pos)))
3261 (cond
3262 ((elt state 3) 'string)
3263 ((elt state 4) (if (elt state 7) 'c++ 'c))
3264 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
3265 (t nil))))))
3266 ;; cache this result if the cache is enabled
3267 (if (not c-in-literal-cache)
3268 (setq c-in-literal-cache (vector (point) rtn)))
3269 rtn)))
3270
3271 ;; XEmacs has a built-in function that should make this much quicker.
3272 ;; I don't think we even need the cache, which makes our lives more
3273 ;; complicated anyway. In this case, lim is only used to detect
3274 ;; cpp directives.
3275 ;;
3276 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
3277 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
3278 ;; Xemacs 21.4.4. It manifested itself thus:
3279 ;;
3280 ;; Starting with an empty AWK Mode buffer, type
3281 ;; /regexp/ {<C-j>
3282 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
3283 ;;
3284 ;; AWK Mode is designed such that when the first / is typed, it gets the
3285 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
3286 ;; are given the s-t property "string". However, buffer-syntactic-context
3287 ;; fails to take account of the change of the s-t property on the opening / to
3288 ;; "string", and reports that the { is within a string started by the second /.
3289 ;;
3290 ;; The workaround for this is for the AWK Mode initialisation to switch the
3291 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
3292 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
3293 ;;
3294 ;; (Alan Mackenzie, 2003/4/30).
3295
3296 (defun c-fast-in-literal (&optional lim detect-cpp)
3297 ;; This function might do hidden buffer changes.
3298 (let ((context (buffer-syntactic-context)))
3299 (cond
3300 ((eq context 'string) 'string)
3301 ((eq context 'comment) 'c++)
3302 ((eq context 'block-comment) 'c)
3303 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
3304
3305 (defalias 'c-in-literal
3306 (if (fboundp 'buffer-syntactic-context)
3307 'c-fast-in-literal ; XEmacs
3308 'c-slow-in-literal)) ; GNU Emacs
3309
3310 ;; The defalias above isn't enough to shut up the byte compiler.
3311 (cc-bytecomp-defun c-in-literal)
3312
3313 (defun c-literal-limits (&optional lim near not-in-delimiter)
3314 "Return a cons of the beginning and end positions of the comment or
3315 string surrounding point (including both delimiters), or nil if point
3316 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
3317 to start parsing from. If NEAR is non-nil, then the limits of any
3318 literal next to point is returned. \"Next to\" means there's only
3319 spaces and tabs between point and the literal. The search for such a
3320 literal is done first in forward direction. If NOT-IN-DELIMITER is
3321 non-nil, the case when point is inside a starting delimiter won't be
3322 recognized. This only has effect for comments, which have starting
3323 delimiters with more than one character.
3324
3325 Note that this function might do hidden buffer changes. See the
3326 comment at the start of cc-engine.el for more info."
3327
3328 (save-excursion
3329 (let* ((pos (point))
3330 (lim (or lim (progn
3331 (c-beginning-of-syntax)
3332 (point))))
3333 (state (parse-partial-sexp lim pos)))
3334
3335 (cond ((elt state 3) ; String.
3336 (goto-char (elt state 8))
3337 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3338 (point-max))))
3339
3340 ((elt state 4) ; Comment.
3341 (goto-char (elt state 8))
3342 (cons (point) (progn (c-forward-single-comment) (point))))
3343
3344 ((and (not not-in-delimiter)
3345 (not (elt state 5))
3346 (eq (char-before) ?/)
3347 (looking-at "[/*]"))
3348 ;; We're standing in a comment starter.
3349 (backward-char 1)
3350 (cons (point) (progn (c-forward-single-comment) (point))))
3351
3352 (near
3353 (goto-char pos)
3354
3355 ;; Search forward for a literal.
3356 (skip-chars-forward " \t")
3357
3358 (cond
3359 ((looking-at c-string-limit-regexp) ; String.
3360 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3361 (point-max))))
3362
3363 ((looking-at c-comment-start-regexp) ; Line or block comment.
3364 (cons (point) (progn (c-forward-single-comment) (point))))
3365
3366 (t
3367 ;; Search backward.
3368 (skip-chars-backward " \t")
3369
3370 (let ((end (point)) beg)
3371 (cond
3372 ((save-excursion
3373 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
3374 (setq beg (c-safe (c-backward-sexp 1) (point))))
3375
3376 ((and (c-safe (forward-char -2) t)
3377 (looking-at "*/"))
3378 ;; Block comment. Due to the nature of line
3379 ;; comments, they will always be covered by the
3380 ;; normal case above.
3381 (goto-char end)
3382 (c-backward-single-comment)
3383 ;; If LIM is bogus, beg will be bogus.
3384 (setq beg (point))))
3385
3386 (if beg (cons beg end))))))
3387 ))))
3388
3389 ;; In case external callers use this; it did have a docstring.
3390 (defalias 'c-literal-limits-fast 'c-literal-limits)
3391
3392 (defun c-collect-line-comments (range)
3393 "If the argument is a cons of two buffer positions (such as returned by
3394 `c-literal-limits'), and that range contains a C++ style line comment,
3395 then an extended range is returned that contains all adjacent line
3396 comments (i.e. all comments that starts in the same column with no
3397 empty lines or non-whitespace characters between them). Otherwise the
3398 argument is returned.
3399
3400 Note that this function might do hidden buffer changes. See the
3401 comment at the start of cc-engine.el for more info."
3402
3403 (save-excursion
3404 (condition-case nil
3405 (if (and (consp range) (progn
3406 (goto-char (car range))
3407 (looking-at c-line-comment-starter)))
3408 (let ((col (current-column))
3409 (beg (point))
3410 (bopl (c-point 'bopl))
3411 (end (cdr range)))
3412 ;; Got to take care in the backward direction to handle
3413 ;; comments which are preceded by code.
3414 (while (and (c-backward-single-comment)
3415 (>= (point) bopl)
3416 (looking-at c-line-comment-starter)
3417 (= col (current-column)))
3418 (setq beg (point)
3419 bopl (c-point 'bopl)))
3420 (goto-char end)
3421 (while (and (progn (skip-chars-forward " \t")
3422 (looking-at c-line-comment-starter))
3423 (= col (current-column))
3424 (prog1 (zerop (forward-line 1))
3425 (setq end (point)))))
3426 (cons beg end))
3427 range)
3428 (error range))))
3429
3430 (defun c-literal-type (range)
3431 "Convenience function that given the result of `c-literal-limits',
3432 returns nil or the type of literal that the range surrounds. It's
3433 much faster than using `c-in-literal' and is intended to be used when
3434 you need both the type of a literal and its limits.
3435
3436 Note that this function might do hidden buffer changes. See the
3437 comment at the start of cc-engine.el for more info."
3438
3439 (if (consp range)
3440 (save-excursion
3441 (goto-char (car range))
3442 (cond ((looking-at c-string-limit-regexp) 'string)
3443 ((or (looking-at "//") ; c++ line comment
3444 (and (looking-at "\\s<") ; comment starter
3445 (looking-at "#"))) ; awk comment.
3446 'c++)
3447 (t 'c))) ; Assuming the range is valid.
3448 range))
3449
3450 \f
3451 ;; `c-find-decl-spots' and accompanying stuff.
3452
3453 ;; Variables used in `c-find-decl-spots' to cache the search done for
3454 ;; the first declaration in the last call. When that function starts,
3455 ;; it needs to back up over syntactic whitespace to look at the last
3456 ;; token before the region being searched. That can sometimes cause
3457 ;; moves back and forth over a quite large region of comments and
3458 ;; macros, which would be repeated for each changed character when
3459 ;; we're called during fontification, since font-lock refontifies the
3460 ;; current line for each change. Thus it's worthwhile to cache the
3461 ;; first match.
3462 ;;
3463 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3464 ;; the syntactic whitespace less or equal to some start position.
3465 ;; There's no cached value if it's nil.
3466 ;;
3467 ;; `c-find-decl-match-pos' is the match position if
3468 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3469 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3470 (defvar c-find-decl-syntactic-pos nil)
3471 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
3472 (defvar c-find-decl-match-pos nil)
3473 (make-variable-buffer-local 'c-find-decl-match-pos)
3474
3475 (defsubst c-invalidate-find-decl-cache (change-min-pos)
3476 (and c-find-decl-syntactic-pos
3477 (< change-min-pos c-find-decl-syntactic-pos)
3478 (setq c-find-decl-syntactic-pos nil)))
3479
3480 ; (defface c-debug-decl-spot-face
3481 ; '((t (:background "Turquoise")))
3482 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3483 ; (defface c-debug-decl-sws-face
3484 ; '((t (:background "Khaki")))
3485 ; "Debug face to mark the syntactic whitespace between the declaration
3486 ; spots and the preceding token end.")
3487
3488 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
3489 (when (facep 'c-debug-decl-spot-face)
3490 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
3491 (c-debug-add-face (max match-pos (point-min)) decl-pos
3492 'c-debug-decl-sws-face)
3493 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
3494 'c-debug-decl-spot-face))))
3495 (defmacro c-debug-remove-decl-spot-faces (beg end)
3496 (when (facep 'c-debug-decl-spot-face)
3497 `(c-save-buffer-state ()
3498 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
3499 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
3500
3501 (defmacro c-find-decl-prefix-search ()
3502 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3503 ;; but it contains lots of free variables that refer to things
3504 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3505 ;; if there is a match, otherwise at `cfd-limit'.
3506 ;;
3507 ;; This macro might do hidden buffer changes.
3508
3509 '(progn
3510 ;; Find the next property match position if we haven't got one already.
3511 (unless cfd-prop-match
3512 (save-excursion
3513 (while (progn
3514 (goto-char (next-single-property-change
3515 (point) 'c-type nil cfd-limit))
3516 (and (< (point) cfd-limit)
3517 (not (eq (c-get-char-property (1- (point)) 'c-type)
3518 'c-decl-end)))))
3519 (setq cfd-prop-match (point))))
3520
3521 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
3522 ;; got one already.
3523 (unless cfd-re-match
3524
3525 (if (> cfd-re-match-end (point))
3526 (goto-char cfd-re-match-end))
3527
3528 (while (if (setq cfd-re-match-end
3529 (re-search-forward c-decl-prefix-or-start-re
3530 cfd-limit 'move))
3531
3532 ;; Match. Check if it's inside a comment or string literal.
3533 (c-got-face-at
3534 (if (setq cfd-re-match (match-end 1))
3535 ;; Matched the end of a token preceding a decl spot.
3536 (progn
3537 (goto-char cfd-re-match)
3538 (1- cfd-re-match))
3539 ;; Matched a token that start a decl spot.
3540 (goto-char (match-beginning 0))
3541 (point))
3542 c-literal-faces)
3543
3544 ;; No match. Finish up and exit the loop.
3545 (setq cfd-re-match cfd-limit)
3546 nil)
3547
3548 ;; Skip out of comments and string literals.
3549 (while (progn
3550 (goto-char (next-single-property-change
3551 (point) 'face nil cfd-limit))
3552 (and (< (point) cfd-limit)
3553 (c-got-face-at (point) c-literal-faces)))))
3554
3555 ;; If we matched at the decl start, we have to back up over the
3556 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
3557 ;; any decl spots in the syntactic ws.
3558 (unless cfd-re-match
3559 (c-backward-syntactic-ws)
3560 (setq cfd-re-match (point))))
3561
3562 ;; Choose whichever match is closer to the start.
3563 (if (< cfd-re-match cfd-prop-match)
3564 (setq cfd-match-pos cfd-re-match
3565 cfd-re-match nil)
3566 (setq cfd-match-pos cfd-prop-match
3567 cfd-prop-match nil))
3568
3569 (goto-char cfd-match-pos)
3570
3571 (when (< cfd-match-pos cfd-limit)
3572 ;; Skip forward past comments only so we don't skip macros.
3573 (c-forward-comments)
3574 ;; Set the position to continue at. We can avoid going over
3575 ;; the comments skipped above a second time, but it's possible
3576 ;; that the comment skipping has taken us past `cfd-prop-match'
3577 ;; since the property might be used inside comments.
3578 (setq cfd-continue-pos (if cfd-prop-match
3579 (min cfd-prop-match (point))
3580 (point))))))
3581
3582 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3583 ;; Call CFD-FUN for each possible spot for a declaration, cast or
3584 ;; label from the point to CFD-LIMIT. Such a spot is:
3585 ;;
3586 ;; o The first token after bob.
3587 ;; o The first token after the end of submatch 1 in
3588 ;; `c-decl-prefix-or-start-re' when that submatch matches.
3589 ;; o The start of each `c-decl-prefix-or-start-re' match when
3590 ;; submatch 1 doesn't match.
3591 ;; o The first token after the end of each occurence of the
3592 ;; `c-type' text property with the value `c-decl-end', provided
3593 ;; `c-type-decl-end-used' is set.
3594 ;;
3595 ;; Only a spot that match CFD-DECL-RE and whose face is in the
3596 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
3597 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
3598 ;;
3599 ;; If the match is inside a macro then the buffer is narrowed to the
3600 ;; end of it, so that CFD-FUN can investigate the following tokens
3601 ;; without matching something that begins inside a macro and ends
3602 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3603 ;; CFD-FACE-CHECKLIST checks exist.
3604 ;;
3605 ;; CFD-FUN is called with point at the start of the spot. It's
3606 ;; passed two arguments: The first is the end position of the token
3607 ;; preceding the spot, or 0 for the implicit match at bob. The
3608 ;; second is a flag that is t when the match is inside a macro. If
3609 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
3610 ;; spot, it should return non-nil to ensure that the next search
3611 ;; will find them.
3612 ;;
3613 ;; The spots are visited approximately in order from top to bottom.
3614 ;; It's however the positions where `c-decl-prefix-or-start-re'
3615 ;; matches and where `c-decl-end' properties are found that are in
3616 ;; order. Since the spots often are at the following token, they
3617 ;; might be visited out of order insofar as more spots are reported
3618 ;; later on within the syntactic whitespace between the match
3619 ;; positions and their spots.
3620 ;;
3621 ;; It's assumed that comments and strings are fontified in the
3622 ;; searched range.
3623 ;;
3624 ;; This is mainly used in fontification, and so has an elaborate
3625 ;; cache to handle repeated calls from the same start position; see
3626 ;; the variables above.
3627 ;;
3628 ;; All variables in this function begin with `cfd-' to avoid name
3629 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3630 ;;
3631 ;; This function might do hidden buffer changes.
3632
3633 (let ((cfd-start-pos (point))
3634 (cfd-buffer-end (point-max))
3635 ;; The end of the token preceding the decl spot last found
3636 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
3637 ;; no match.
3638 cfd-re-match
3639 ;; The end position of the last `c-decl-prefix-or-start-re'
3640 ;; match. If this is greater than `cfd-continue-pos', the
3641 ;; next regexp search is started here instead.
3642 (cfd-re-match-end (point-min))
3643 ;; The end of the last `c-decl-end' found by
3644 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
3645 ;; match. If searching for the property isn't needed then we
3646 ;; disable it by setting it to `cfd-limit' directly.
3647 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3648 ;; The end of the token preceding the decl spot last found by
3649 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
3650 ;; bob. `cfd-limit' if there's no match. In other words,
3651 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
3652 (cfd-match-pos cfd-limit)
3653 ;; The position to continue searching at.
3654 cfd-continue-pos
3655 ;; The position of the last "real" token we've stopped at.
3656 ;; This can be greater than `cfd-continue-pos' when we get
3657 ;; hits inside macros or at `c-decl-end' positions inside
3658 ;; comments.
3659 (cfd-token-pos 0)
3660 ;; The end position of the last entered macro.
3661 (cfd-macro-end 0))
3662
3663 ;; Initialize by finding a syntactically relevant start position
3664 ;; before the point, and do the first `c-decl-prefix-or-start-re'
3665 ;; search unless we're at bob.
3666
3667 (let (start-in-literal start-in-macro syntactic-pos)
3668 ;; Must back up a bit since we look for the end of the previous
3669 ;; statement or declaration, which is earlier than the first
3670 ;; returned match.
3671
3672 (cond
3673 ;; First we need to move to a syntactically relevant position.
3674 ;; Begin by backing out of comment or string literals.
3675 ((and
3676 (when (c-got-face-at (point) c-literal-faces)
3677 ;; Try to use the faces to back up to the start of the
3678 ;; literal. FIXME: What if the point is on a declaration
3679 ;; inside a comment?
3680 (while (and (not (bobp))
3681 (c-got-face-at (1- (point)) c-literal-faces))
3682 (goto-char (previous-single-property-change
3683 (point) 'face nil (point-min))))
3684
3685 ;; XEmacs doesn't fontify the quotes surrounding string
3686 ;; literals.
3687 (and (featurep 'xemacs)
3688 (eq (get-text-property (point) 'face)
3689 'font-lock-string-face)
3690 (not (bobp))
3691 (progn (backward-char)
3692 (not (looking-at c-string-limit-regexp)))
3693 (forward-char))
3694
3695 ;; Don't trust the literal to contain only literal faces
3696 ;; (the font lock package might not have fontified the
3697 ;; start of it at all, for instance) so check that we have
3698 ;; arrived at something that looks like a start or else
3699 ;; resort to `c-literal-limits'.
3700 (unless (looking-at c-literal-start-regexp)
3701 (let ((range (c-literal-limits)))
3702 (if range (goto-char (car range)))))
3703
3704 (setq start-in-literal (point)))
3705
3706 ;; The start is in a literal. If the limit is in the same
3707 ;; one we don't have to find a syntactic position etc. We
3708 ;; only check that if the limit is at or before bonl to save
3709 ;; time; it covers the by far most common case when font-lock
3710 ;; refontifies the current line only.
3711 (<= cfd-limit (c-point 'bonl cfd-start-pos))
3712 (save-excursion
3713 (goto-char cfd-start-pos)
3714 (while (progn
3715 (goto-char (next-single-property-change
3716 (point) 'face nil cfd-limit))
3717 (and (< (point) cfd-limit)
3718 (c-got-face-at (point) c-literal-faces))))
3719 (= (point) cfd-limit)))
3720
3721 ;; Completely inside a literal. Set up variables to trig the
3722 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
3723 ;; find a suitable start position.
3724 (setq cfd-continue-pos start-in-literal))
3725
3726 ;; Check if the region might be completely inside a macro, to
3727 ;; optimize that like the completely-inside-literal above.
3728 ((save-excursion
3729 (and (= (forward-line 1) 0)
3730 (bolp) ; forward-line has funny behavior at eob.
3731 (>= (point) cfd-limit)
3732 (progn (backward-char)
3733 (eq (char-before) ?\\))))
3734 ;; (Maybe) completely inside a macro. Only need to trig the
3735 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
3736 ;; set things up.
3737 (setq cfd-continue-pos (1- cfd-start-pos)
3738 start-in-macro t))
3739
3740 (t
3741 ;; Back out of any macro so we don't miss any declaration
3742 ;; that could follow after it.
3743 (when (c-beginning-of-macro)
3744 (setq start-in-macro t))
3745
3746 ;; Now we're at a proper syntactically relevant position so we
3747 ;; can use the cache. But first clear it if it applied
3748 ;; further down.
3749 (c-invalidate-find-decl-cache cfd-start-pos)
3750
3751 (setq syntactic-pos (point))
3752 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
3753 ;; Don't have to do this if the cache is relevant here,
3754 ;; typically if the same line is refontified again. If
3755 ;; we're just some syntactic whitespace further down we can
3756 ;; still use the cache to limit the skipping.
3757 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
3758
3759 ;; If we hit `c-find-decl-syntactic-pos' and
3760 ;; `c-find-decl-match-pos' is set then we install the cached
3761 ;; values. If we hit `c-find-decl-syntactic-pos' and
3762 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3763 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3764 ;; and so we can continue the search from this point. If we
3765 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
3766 ;; the right spot to begin searching anyway.
3767 (if (and (eq (point) c-find-decl-syntactic-pos)
3768 c-find-decl-match-pos)
3769 (setq cfd-match-pos c-find-decl-match-pos
3770 cfd-continue-pos syntactic-pos)
3771
3772 (setq c-find-decl-syntactic-pos syntactic-pos)
3773
3774 (when (if (bobp)
3775 ;; Always consider bob a match to get the first
3776 ;; declaration in the file. Do this separately instead of
3777 ;; letting `c-decl-prefix-or-start-re' match bob, so that
3778 ;; regexp always can consume at least one character to
3779 ;; ensure that we won't get stuck in an infinite loop.
3780 (setq cfd-re-match 0)
3781 (backward-char)
3782 (c-beginning-of-current-token)
3783 (< (point) cfd-limit))
3784 ;; Do an initial search now. In the bob case above it's
3785 ;; only done to search for a `c-decl-end' spot.
3786 (c-find-decl-prefix-search))
3787
3788 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
3789 cfd-match-pos)))))
3790
3791 ;; Advance `cfd-continue-pos' if it's before the start position.
3792 ;; The closest continue position that might have effect at or
3793 ;; after the start depends on what we started in. This also
3794 ;; finds a suitable start position in the special cases when the
3795 ;; region is completely within a literal or macro.
3796 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
3797
3798 (cond
3799 (start-in-macro
3800 ;; If we're in a macro then it's the closest preceding token
3801 ;; in the macro. Check this before `start-in-literal',
3802 ;; since if we're inside a literal in a macro, the preceding
3803 ;; token is earlier than any `c-decl-end' spot inside the
3804 ;; literal (comment).
3805 (goto-char (or start-in-literal cfd-start-pos))
3806 ;; The only syntactic ws in macros are comments.
3807 (c-backward-comments)
3808 (backward-char)
3809 (c-beginning-of-current-token))
3810
3811 (start-in-literal
3812 ;; If we're in a comment it can only be the closest
3813 ;; preceding `c-decl-end' position within that comment, if
3814 ;; any. Go back to the beginning of such a property so that
3815 ;; `c-find-decl-prefix-search' will find the end of it.
3816 ;; (Can't stop at the end and install it directly on
3817 ;; `cfd-prop-match' since that variable might be cleared
3818 ;; after `cfd-fun' below.)
3819 ;;
3820 ;; Note that if the literal is a string then the property
3821 ;; search will simply skip to the beginning of it right
3822 ;; away.
3823 (if (not c-type-decl-end-used)
3824 (goto-char start-in-literal)
3825 (goto-char cfd-start-pos)
3826 (while (progn
3827 (goto-char (previous-single-property-change
3828 (point) 'c-type nil start-in-literal))
3829 (and (> (point) start-in-literal)
3830 (not (eq (c-get-char-property (point) 'c-type)
3831 'c-decl-end))))))
3832
3833 (when (= (point) start-in-literal)
3834 ;; Didn't find any property inside the comment, so we can
3835 ;; skip it entirely. (This won't skip past a string, but
3836 ;; that'll be handled quickly by the next
3837 ;; `c-find-decl-prefix-search' anyway.)
3838 (c-forward-single-comment)
3839 (if (> (point) cfd-limit)
3840 (goto-char cfd-limit))))
3841
3842 (t
3843 ;; If we started in normal code, the only match that might
3844 ;; apply before the start is what we already got in
3845 ;; `cfd-match-pos' so we can continue at the start position.
3846 ;; (Note that we don't get here if the first match is below
3847 ;; it.)
3848 (goto-char cfd-start-pos)))
3849
3850 ;; Delete found matches if they are before our new continue
3851 ;; position, so that `c-find-decl-prefix-search' won't back up
3852 ;; to them later on.
3853 (setq cfd-continue-pos (point))
3854 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
3855 (setq cfd-re-match nil))
3856 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
3857 (setq cfd-prop-match nil)))
3858
3859 (if syntactic-pos
3860 ;; This is the normal case and we got a proper syntactic
3861 ;; position. If there's a match then it's always outside
3862 ;; macros and comments, so advance to the next token and set
3863 ;; `cfd-token-pos'. The loop below will later go back using
3864 ;; `cfd-continue-pos' to fix declarations inside the
3865 ;; syntactic ws.
3866 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
3867 (goto-char syntactic-pos)
3868 (c-forward-syntactic-ws)
3869 (and cfd-continue-pos
3870 (< cfd-continue-pos (point))
3871 (setq cfd-token-pos (point))))
3872
3873 ;; Have one of the special cases when the region is completely
3874 ;; within a literal or macro. `cfd-continue-pos' is set to a
3875 ;; good start position for the search, so do it.
3876 (c-find-decl-prefix-search)))
3877
3878 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
3879
3880 (while (progn
3881 (while (and
3882 (< cfd-match-pos cfd-limit)
3883
3884 (or
3885 ;; Kludge to filter out matches on the "<" that
3886 ;; aren't open parens, for the sake of languages
3887 ;; that got `c-recognize-<>-arglists' set.
3888 (and (eq (char-before cfd-match-pos) ?<)
3889 (not (c-get-char-property (1- cfd-match-pos)
3890 'syntax-table)))
3891
3892 ;; If `cfd-continue-pos' is less or equal to
3893 ;; `cfd-token-pos', we've got a hit inside a macro
3894 ;; that's in the syntactic whitespace before the last
3895 ;; "real" declaration we've checked. If they're equal
3896 ;; we've arrived at the declaration a second time, so
3897 ;; there's nothing to do.
3898 (= cfd-continue-pos cfd-token-pos)
3899
3900 (progn
3901 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3902 ;; we're still searching for declarations embedded in
3903 ;; the syntactic whitespace. In that case we need
3904 ;; only to skip comments and not macros, since they
3905 ;; can't be nested, and that's already been done in
3906 ;; `c-find-decl-prefix-search'.
3907 (when (> cfd-continue-pos cfd-token-pos)
3908 (c-forward-syntactic-ws)
3909 (setq cfd-token-pos (point)))
3910
3911 ;; Continue if the following token fails the
3912 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3913 (when (or (>= (point) cfd-limit)
3914 (not (looking-at cfd-decl-re))
3915 (and cfd-face-checklist
3916 (not (c-got-face-at
3917 (point) cfd-face-checklist))))
3918 (goto-char cfd-continue-pos)
3919 t)))
3920
3921 (< (point) cfd-limit))
3922 (c-find-decl-prefix-search))
3923
3924 (< (point) cfd-limit))
3925
3926 (when (and
3927 (>= (point) cfd-start-pos)
3928
3929 (progn
3930 ;; Narrow to the end of the macro if we got a hit inside
3931 ;; one, to avoid recognizing things that start inside the
3932 ;; macro and end outside it.
3933 (when (> cfd-match-pos cfd-macro-end)
3934 ;; Not in the same macro as in the previous round.
3935 (save-excursion
3936 (goto-char cfd-match-pos)
3937 (setq cfd-macro-end
3938 (if (save-excursion (and (c-beginning-of-macro)
3939 (< (point) cfd-match-pos)))
3940 (progn (c-end-of-macro)
3941 (point))
3942 0))))
3943
3944 (if (zerop cfd-macro-end)
3945 t
3946 (if (> cfd-macro-end (point))
3947 (progn (narrow-to-region (point-min) cfd-macro-end)
3948 t)
3949 ;; The matched token was the last thing in the macro,
3950 ;; so the whole match is bogus.
3951 (setq cfd-macro-end 0)
3952 nil))))
3953
3954 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3955 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3956 (setq cfd-prop-match nil))
3957
3958 (when (/= cfd-macro-end 0)
3959 ;; Restore limits if we did macro narrowment above.
3960 (narrow-to-region (point-min) cfd-buffer-end)))
3961
3962 (goto-char cfd-continue-pos)
3963 (if (= cfd-continue-pos cfd-limit)
3964 (setq cfd-match-pos cfd-limit)
3965 (c-find-decl-prefix-search)))))
3966
3967 \f
3968 ;; A cache for found types.
3969
3970 ;; Buffer local variable that contains an obarray with the types we've
3971 ;; found. If a declaration is recognized somewhere we record the
3972 ;; fully qualified identifier in it to recognize it as a type
3973 ;; elsewhere in the file too. This is not accurate since we do not
3974 ;; bother with the scoping rules of the languages, but in practice the
3975 ;; same name is seldom used as both a type and something else in a
3976 ;; file, and we only use this as a last resort in ambiguous cases (see
3977 ;; `c-forward-decl-or-cast-1').
3978 ;;
3979 ;; Not every type need be in this cache. However, things which have
3980 ;; ceased to be types must be removed from it.
3981 ;;
3982 ;; Template types in C++ are added here too but with the template
3983 ;; arglist replaced with "<>" in references or "<" for the one in the
3984 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
3985 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
3986 ;; template specs can be fairly sized programs in themselves) and
3987 ;; improves the hit ratio (it's a type regardless of the template
3988 ;; args; it's just not the same type, but we're only interested in
3989 ;; recognizing types, not telling distinct types apart). Note that
3990 ;; template types in references are added here too; from the example
3991 ;; above there will also be an entry "Foo<".
3992 (defvar c-found-types nil)
3993 (make-variable-buffer-local 'c-found-types)
3994
3995 (defsubst c-clear-found-types ()
3996 ;; Clears `c-found-types'.
3997 (setq c-found-types (make-vector 53 0)))
3998
3999 (defun c-add-type (from to)
4000 ;; Add the given region as a type in `c-found-types'. If the region
4001 ;; doesn't match an existing type but there is a type which is equal
4002 ;; to the given one except that the last character is missing, then
4003 ;; the shorter type is removed. That's done to avoid adding all
4004 ;; prefixes of a type as it's being entered and font locked. This
4005 ;; doesn't cover cases like when characters are removed from a type
4006 ;; or added in the middle. We'd need the position of point when the
4007 ;; font locking is invoked to solve this well.
4008 ;;
4009 ;; This function might do hidden buffer changes.
4010 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4011 (unless (intern-soft type c-found-types)
4012 (unintern (substring type 0 -1) c-found-types)
4013 (intern type c-found-types))))
4014
4015 (defun c-unfind-type (name)
4016 ;; Remove the "NAME" from c-found-types, if present.
4017 (unintern name c-found-types))
4018
4019 (defsubst c-check-type (from to)
4020 ;; Return non-nil if the given region contains a type in
4021 ;; `c-found-types'.
4022 ;;
4023 ;; This function might do hidden buffer changes.
4024 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4025 c-found-types))
4026
4027 (defun c-list-found-types ()
4028 ;; Return all the types in `c-found-types' as a sorted list of
4029 ;; strings.
4030 (let (type-list)
4031 (mapatoms (lambda (type)
4032 (setq type-list (cons (symbol-name type)
4033 type-list)))
4034 c-found-types)
4035 (sort type-list 'string-lessp)))
4036
4037 ;; Shut up the byte compiler.
4038 (defvar c-maybe-stale-found-type)
4039
4040 (defun c-trim-found-types (beg end old-len)
4041 ;; An after change function which, in conjunction with the info in
4042 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4043 ;; from `c-found-types', should this type have become stale. For
4044 ;; example, this happens to "foo" when "foo \n bar();" becomes
4045 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4046 ;; the fontification.
4047 ;;
4048 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4049 ;; type?
4050 (when (> end beg)
4051 (save-excursion
4052 (when (< end (point-max))
4053 (goto-char end)
4054 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4055 (progn (goto-char end)
4056 (c-end-of-current-token)))
4057 (c-unfind-type (buffer-substring-no-properties
4058 end (point)))))
4059 (when (> beg (point-min))
4060 (goto-char beg)
4061 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4062 (progn (goto-char beg)
4063 (c-beginning-of-current-token)))
4064 (c-unfind-type (buffer-substring-no-properties
4065 (point) beg))))))
4066
4067 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4068 (cond
4069 ;; Changing the amount of (already existing) whitespace - don't do anything.
4070 ((and (c-partial-ws-p beg end)
4071 (or (= beg end) ; removal of WS
4072 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4073
4074 ;; The syntactic relationship which defined a "found type" has been
4075 ;; destroyed.
4076 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4077 (c-unfind-type (cadr c-maybe-stale-found-type)))
4078 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4079 )))
4080
4081 \f
4082 ;; Handling of small scale constructs like types and names.
4083
4084 (defun c-after-change-check-<>-operators (beg end)
4085 ;; This is called from `after-change-functions' when
4086 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
4087 ;; chars with paren syntax become part of another operator like "<<"
4088 ;; or ">=".
4089 ;;
4090 ;; This function might do hidden buffer changes.
4091
4092 (save-excursion
4093 (goto-char beg)
4094 (when (or (looking-at "[<>]")
4095 (< (skip-chars-backward "<>") 0))
4096
4097 (goto-char beg)
4098 (c-beginning-of-current-token)
4099 (when (and (< (point) beg)
4100 (looking-at c-<>-multichar-token-regexp)
4101 (< beg (setq beg (match-end 0))))
4102 (while (progn (skip-chars-forward "^<>" beg)
4103 (< (point) beg))
4104 (c-clear-char-property (point) 'syntax-table)
4105 (forward-char))))
4106
4107 (when (< beg end)
4108 (goto-char end)
4109 (when (or (looking-at "[<>]")
4110 (< (skip-chars-backward "<>") 0))
4111
4112 (goto-char end)
4113 (c-beginning-of-current-token)
4114 (when (and (< (point) end)
4115 (looking-at c-<>-multichar-token-regexp)
4116 (< end (setq end (match-end 0))))
4117 (while (progn (skip-chars-forward "^<>" end)
4118 (< (point) end))
4119 (c-clear-char-property (point) 'syntax-table)
4120 (forward-char)))))))
4121
4122 ;; Dynamically bound variable that instructs `c-forward-type' to also
4123 ;; treat possible types (i.e. those that it normally returns 'maybe or
4124 ;; 'found for) as actual types (and always return 'found for them).
4125 ;; This means that it records them in `c-record-type-identifiers' if
4126 ;; that is set, and that it adds them to `c-found-types'.
4127 (defvar c-promote-possible-types nil)
4128
4129 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4130 ;; mark up successfully parsed arglists with paren syntax properties on
4131 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
4132 ;; `c-type' property of each argument separating comma.
4133 ;;
4134 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
4135 ;; all arglists for side effects (i.e. recording types), otherwise it
4136 ;; exploits any existing paren syntax properties to quickly jump to the
4137 ;; end of already parsed arglists.
4138 ;;
4139 ;; Marking up the arglists is not the default since doing that correctly
4140 ;; depends on a proper value for `c-restricted-<>-arglists'.
4141 (defvar c-parse-and-markup-<>-arglists nil)
4142
4143 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4144 ;; not accept arglists that contain binary operators.
4145 ;;
4146 ;; This is primarily used to handle C++ template arglists. C++
4147 ;; disambiguates them by checking whether the preceding name is a
4148 ;; template or not. We can't do that, so we assume it is a template
4149 ;; if it can be parsed as one. That usually works well since
4150 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
4151 ;; in almost all cases would be pointless.
4152 ;;
4153 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
4154 ;; should let the comma separate the function arguments instead. And
4155 ;; in a context where the value of the expression is taken, e.g. in
4156 ;; "if (a < b || c > d)", it's probably not a template.
4157 (defvar c-restricted-<>-arglists nil)
4158
4159 ;; Dynamically bound variables that instructs
4160 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
4161 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
4162 ;; `c-forward-label' to record the ranges of all the type and
4163 ;; reference identifiers they encounter. They will build lists on
4164 ;; these variables where each element is a cons of the buffer
4165 ;; positions surrounding each identifier. This recording is only
4166 ;; activated when `c-record-type-identifiers' is non-nil.
4167 ;;
4168 ;; All known types that can't be identifiers are recorded, and also
4169 ;; other possible types if `c-promote-possible-types' is set.
4170 ;; Recording is however disabled inside angle bracket arglists that
4171 ;; are encountered inside names and other angle bracket arglists.
4172 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
4173 ;; instead.
4174 ;;
4175 ;; Only the names in C++ template style references (e.g. "tmpl" in
4176 ;; "tmpl<a,b>::foo") are recorded as references, other references
4177 ;; aren't handled here.
4178 ;;
4179 ;; `c-forward-label' records the label identifier(s) on
4180 ;; `c-record-ref-identifiers'.
4181 (defvar c-record-type-identifiers nil)
4182 (defvar c-record-ref-identifiers nil)
4183
4184 ;; This variable will receive a cons cell of the range of the last
4185 ;; single identifier symbol stepped over by `c-forward-name' if it's
4186 ;; successful. This is the range that should be put on one of the
4187 ;; record lists above by the caller. It's assigned nil if there's no
4188 ;; such symbol in the name.
4189 (defvar c-last-identifier-range nil)
4190
4191 (defmacro c-record-type-id (range)
4192 (if (eq (car-safe range) 'cons)
4193 ;; Always true.
4194 `(setq c-record-type-identifiers
4195 (cons ,range c-record-type-identifiers))
4196 `(let ((range ,range))
4197 (if range
4198 (setq c-record-type-identifiers
4199 (cons range c-record-type-identifiers))))))
4200
4201 (defmacro c-record-ref-id (range)
4202 (if (eq (car-safe range) 'cons)
4203 ;; Always true.
4204 `(setq c-record-ref-identifiers
4205 (cons ,range c-record-ref-identifiers))
4206 `(let ((range ,range))
4207 (if range
4208 (setq c-record-ref-identifiers
4209 (cons range c-record-ref-identifiers))))))
4210
4211 ;; Dynamically bound variable that instructs `c-forward-type' to
4212 ;; record the ranges of types that only are found. Behaves otherwise
4213 ;; like `c-record-type-identifiers'.
4214 (defvar c-record-found-types nil)
4215
4216 (defmacro c-forward-keyword-prefixed-id (type)
4217 ;; Used internally in `c-forward-keyword-clause' to move forward
4218 ;; over a type (if TYPE is 'type) or a name (otherwise) which
4219 ;; possibly is prefixed by keywords and their associated clauses.
4220 ;; Try with a type/name first to not trip up on those that begin
4221 ;; with a keyword. Return t if a known or found type is moved
4222 ;; over. The point is clobbered if nil is returned. If range
4223 ;; recording is enabled, the identifier is recorded on as a type
4224 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
4225 ;;
4226 ;; This macro might do hidden buffer changes.
4227 `(let (res)
4228 (while (if (setq res ,(if (eq type 'type)
4229 `(c-forward-type)
4230 `(c-forward-name)))
4231 nil
4232 (and (looking-at c-keywords-regexp)
4233 (c-forward-keyword-clause 1))))
4234 (when (memq res '(t known found prefix))
4235 ,(when (eq type 'ref)
4236 `(when c-record-type-identifiers
4237 (c-record-ref-id c-last-identifier-range)))
4238 t)))
4239
4240 (defmacro c-forward-id-comma-list (type update-safe-pos)
4241 ;; Used internally in `c-forward-keyword-clause' to move forward
4242 ;; over a comma separated list of types or names using
4243 ;; `c-forward-keyword-prefixed-id'.
4244 ;;
4245 ;; This macro might do hidden buffer changes.
4246 `(while (and (progn
4247 ,(when update-safe-pos
4248 `(setq safe-pos (point)))
4249 (eq (char-after) ?,))
4250 (progn
4251 (forward-char)
4252 (c-forward-syntactic-ws)
4253 (c-forward-keyword-prefixed-id ,type)))))
4254
4255 (defun c-forward-keyword-clause (match)
4256 ;; Submatch MATCH in the current match data is assumed to surround a
4257 ;; token. If it's a keyword, move over it and any immediately
4258 ;; following clauses associated with it, stopping at the start of
4259 ;; the next token. t is returned in that case, otherwise the point
4260 ;; stays and nil is returned. The kind of clauses that are
4261 ;; recognized are those specified by `c-type-list-kwds',
4262 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
4263 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
4264 ;; and `c-<>-arglist-kwds'.
4265 ;;
4266 ;; This function records identifier ranges on
4267 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4268 ;; `c-record-type-identifiers' is non-nil.
4269 ;;
4270 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
4271 ;; apply directly after the keyword, the type list is moved over
4272 ;; only when there is no unaccounted token before it (i.e. a token
4273 ;; that isn't moved over due to some other keyword list). The
4274 ;; identifier ranges in the list are still recorded if that should
4275 ;; be done, though.
4276 ;;
4277 ;; This function might do hidden buffer changes.
4278
4279 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
4280 ;; The call to `c-forward-<>-arglist' below is made after
4281 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
4282 ;; are angle bracket arglists and `c-restricted-<>-arglists'
4283 ;; should therefore be nil.
4284 (c-parse-and-markup-<>-arglists t)
4285 c-restricted-<>-arglists)
4286
4287 (when kwd-sym
4288 (goto-char (match-end match))
4289 (c-forward-syntactic-ws)
4290 (setq safe-pos (point))
4291
4292 (cond
4293 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
4294 (c-forward-keyword-prefixed-id type))
4295 ;; There's a type directly after a keyword in `c-type-list-kwds'.
4296 (c-forward-id-comma-list type t))
4297
4298 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
4299 (c-forward-keyword-prefixed-id ref))
4300 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
4301 (c-forward-id-comma-list ref t))
4302
4303 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
4304 (eq (char-after) ?\())
4305 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
4306
4307 (forward-char)
4308 (when (and (setq pos (c-up-list-forward))
4309 (eq (char-before pos) ?\)))
4310 (when (and c-record-type-identifiers
4311 (c-keyword-member kwd-sym 'c-paren-type-kwds))
4312 ;; Use `c-forward-type' on every identifier we can find
4313 ;; inside the paren, to record the types.
4314 (while (c-syntactic-re-search-forward c-symbol-start pos t)
4315 (goto-char (match-beginning 0))
4316 (unless (c-forward-type)
4317 (looking-at c-symbol-key) ; Always matches.
4318 (goto-char (match-end 0)))))
4319
4320 (goto-char pos)
4321 (c-forward-syntactic-ws)
4322 (setq safe-pos (point))))
4323
4324 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
4325 (eq (char-after) ?<)
4326 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
4327 (c-forward-syntactic-ws)
4328 (setq safe-pos (point)))
4329
4330 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
4331 (not (looking-at c-symbol-start))
4332 (c-safe (c-forward-sexp) t))
4333 (c-forward-syntactic-ws)
4334 (setq safe-pos (point))))
4335
4336 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
4337 (if (eq (char-after) ?:)
4338 ;; If we are at the colon already, we move over the type
4339 ;; list after it.
4340 (progn
4341 (forward-char)
4342 (c-forward-syntactic-ws)
4343 (when (c-forward-keyword-prefixed-id type)
4344 (c-forward-id-comma-list type t)))
4345 ;; Not at the colon, so stop here. But the identifier
4346 ;; ranges in the type list later on should still be
4347 ;; recorded.
4348 (and c-record-type-identifiers
4349 (progn
4350 ;; If a keyword matched both one of the types above and
4351 ;; this one, we match `c-colon-type-list-re' after the
4352 ;; clause matched above.
4353 (goto-char safe-pos)
4354 (looking-at c-colon-type-list-re))
4355 (progn
4356 (goto-char (match-end 0))
4357 (c-forward-syntactic-ws)
4358 (c-forward-keyword-prefixed-id type))
4359 ;; There's a type after the `c-colon-type-list-re' match
4360 ;; after a keyword in `c-colon-type-list-kwds'.
4361 (c-forward-id-comma-list type nil))))
4362
4363 (goto-char safe-pos)
4364 t)))
4365
4366 (defun c-forward-<>-arglist (all-types)
4367 ;; The point is assumed to be at a "<". Try to treat it as the open
4368 ;; paren of an angle bracket arglist and move forward to the the
4369 ;; corresponding ">". If successful, the point is left after the
4370 ;; ">" and t is returned, otherwise the point isn't moved and nil is
4371 ;; returned. If ALL-TYPES is t then all encountered arguments in
4372 ;; the arglist that might be types are treated as found types.
4373 ;;
4374 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
4375 ;; function handles text properties on the angle brackets and argument
4376 ;; separating commas.
4377 ;;
4378 ;; `c-restricted-<>-arglists' controls how lenient the template
4379 ;; arglist recognition should be.
4380 ;;
4381 ;; This function records identifier ranges on
4382 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4383 ;; `c-record-type-identifiers' is non-nil.
4384 ;;
4385 ;; This function might do hidden buffer changes.
4386
4387 (let ((start (point))
4388 ;; If `c-record-type-identifiers' is set then activate
4389 ;; recording of any found types that constitute an argument in
4390 ;; the arglist.
4391 (c-record-found-types (if c-record-type-identifiers t)))
4392 (if (catch 'angle-bracket-arglist-escape
4393 (setq c-record-found-types
4394 (c-forward-<>-arglist-recur all-types)))
4395 (progn
4396 (when (consp c-record-found-types)
4397 (setq c-record-type-identifiers
4398 ;; `nconc' doesn't mind that the tail of
4399 ;; `c-record-found-types' is t.
4400 (nconc c-record-found-types c-record-type-identifiers)))
4401 t)
4402
4403 (goto-char start)
4404 nil)))
4405
4406 (defun c-forward-<>-arglist-recur (all-types)
4407 ;; Recursive part of `c-forward-<>-arglist'.
4408 ;;
4409 ;; This function might do hidden buffer changes.
4410
4411 (let ((start (point)) res pos tmp
4412 ;; Cover this so that any recorded found type ranges are
4413 ;; automatically lost if it turns out to not be an angle
4414 ;; bracket arglist. It's propagated through the return value
4415 ;; on successful completion.
4416 (c-record-found-types c-record-found-types)
4417 ;; List that collects the positions after the argument
4418 ;; separating ',' in the arglist.
4419 arg-start-pos)
4420
4421 ;; If the '<' has paren open syntax then we've marked it as an angle
4422 ;; bracket arglist before, so skip to the end.
4423 (if (and (not c-parse-and-markup-<>-arglists)
4424 (c-get-char-property (point) 'syntax-table))
4425
4426 (progn
4427 (forward-char)
4428 (if (and (c-go-up-list-forward)
4429 (eq (char-before) ?>))
4430 t
4431
4432 ;; Got unmatched paren angle brackets. We don't clear the paren
4433 ;; syntax properties and retry, on the basis that it's very
4434 ;; unlikely that paren angle brackets become operators by code
4435 ;; manipulation. It's far more likely that it doesn't match due
4436 ;; to narrowing or some temporary change.
4437 (goto-char start)
4438 nil))
4439
4440 (forward-char)
4441 (unless (looking-at c-<-op-cont-regexp)
4442 (while (and
4443 (progn
4444
4445 (when c-record-type-identifiers
4446 (if all-types
4447
4448 ;; All encountered identifiers are types, so set the
4449 ;; promote flag and parse the type.
4450 (progn
4451 (c-forward-syntactic-ws)
4452 (when (looking-at c-identifier-start)
4453 (let ((c-promote-possible-types t))
4454 (c-forward-type))))
4455
4456 ;; Check if this arglist argument is a sole type. If
4457 ;; it's known then it's recorded in
4458 ;; `c-record-type-identifiers'. If it only is found
4459 ;; then it's recorded in `c-record-found-types' which we
4460 ;; might roll back if it turns out that this isn't an
4461 ;; angle bracket arglist afterall.
4462 (when (memq (char-before) '(?, ?<))
4463 (let ((orig-record-found-types c-record-found-types))
4464 (c-forward-syntactic-ws)
4465 (and (memq (c-forward-type) '(known found))
4466 (not (looking-at "[,>]"))
4467 ;; A found type was recorded but it's not the
4468 ;; only thing in the arglist argument, so reset
4469 ;; `c-record-found-types'.
4470 (setq c-record-found-types
4471 orig-record-found-types))))))
4472
4473 (setq pos (point))
4474 (or (when (eq (char-after) ?>)
4475 ;; Must check for '>' at the very start separately,
4476 ;; since the regexp below has to avoid ">>" without
4477 ;; using \\=.
4478 (forward-char)
4479 t)
4480
4481 ;; Note: These regexps exploit the match order in \| so
4482 ;; that "<>" is matched by "<" rather than "[^>:-]>".
4483 (c-syntactic-re-search-forward
4484 (if c-restricted-<>-arglists
4485 ;; Stop on ',', '|', '&', '+' and '-' to catch
4486 ;; common binary operators that could be between
4487 ;; two comparison expressions "a<b" and "c>d".
4488 "[<;{},|&+-]\\|\\([^>:-]>\\)"
4489 ;; Otherwise we still stop on ',' to find the
4490 ;; argument start positions.
4491 "[<;{},]\\|\\([^>:-]>\\)")
4492 nil 'move t t 1)
4493
4494 ;; If the arglist starter has lost its open paren
4495 ;; syntax but not the closer, we won't find the
4496 ;; closer above since we only search in the
4497 ;; balanced sexp. In that case we stop just short
4498 ;; of it so check if the following char is the closer.
4499 (when (eq (char-after) ?>)
4500 (forward-char)
4501 t)))
4502
4503 (cond
4504 ((eq (char-before) ?>)
4505 ;; Either an operator starting with '>' or the end of
4506 ;; the angle bracket arglist.
4507
4508 (if (looking-at c->-op-cont-regexp)
4509 (progn
4510 (goto-char (match-end 0))
4511 t) ; Continue the loop.
4512
4513 ;; The angle bracket arglist is finished.
4514 (when c-parse-and-markup-<>-arglists
4515 (while arg-start-pos
4516 (c-put-c-type-property (1- (car arg-start-pos))
4517 'c-<>-arg-sep)
4518 (setq arg-start-pos (cdr arg-start-pos)))
4519 (c-mark-<-as-paren start)
4520 (c-mark->-as-paren (1- (point))))
4521 (setq res t)
4522 nil)) ; Exit the loop.
4523
4524 ((eq (char-before) ?<)
4525 ;; Either an operator starting with '<' or a nested arglist.
4526
4527 (setq pos (point))
4528 (let (id-start id-end subres keyword-match)
4529 (if (if (looking-at c-<-op-cont-regexp)
4530 (setq tmp (match-end 0))
4531 (setq tmp pos)
4532 (backward-char)
4533 (not
4534 (and
4535
4536 (save-excursion
4537 ;; There's always an identifier before an angle
4538 ;; bracket arglist, or a keyword in
4539 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
4540 (c-backward-syntactic-ws)
4541 (setq id-end (point))
4542 (c-simple-skip-symbol-backward)
4543 (when (or (setq keyword-match
4544 (looking-at c-opt-<>-sexp-key))
4545 (not (looking-at c-keywords-regexp)))
4546 (setq id-start (point))))
4547
4548 (setq subres
4549 (let ((c-record-type-identifiers nil)
4550 (c-record-found-types nil))
4551 (c-forward-<>-arglist-recur
4552 (and keyword-match
4553 (c-keyword-member
4554 (c-keyword-sym (match-string 1))
4555 'c-<>-type-kwds)))))
4556 )))
4557
4558 ;; It was not an angle bracket arglist.
4559 (goto-char tmp)
4560
4561 ;; It was an angle bracket arglist.
4562 (setq c-record-found-types subres)
4563
4564 ;; Record the identifier before the template as a type
4565 ;; or reference depending on whether the arglist is last
4566 ;; in a qualified identifier.
4567 (when (and c-record-type-identifiers
4568 (not keyword-match))
4569 (if (and c-opt-identifier-concat-key
4570 (progn
4571 (c-forward-syntactic-ws)
4572 (looking-at c-opt-identifier-concat-key)))
4573 (c-record-ref-id (cons id-start id-end))
4574 (c-record-type-id (cons id-start id-end))))))
4575 t)
4576
4577 ((and (eq (char-before) ?,)
4578 (not c-restricted-<>-arglists))
4579 ;; Just another argument. Record the position. The
4580 ;; type check stuff that made us stop at it is at
4581 ;; the top of the loop.
4582 (setq arg-start-pos (cons (point) arg-start-pos)))
4583
4584 (t
4585 ;; Got a character that can't be in an angle bracket
4586 ;; arglist argument. Abort using `throw', since
4587 ;; it's useless to try to find a surrounding arglist
4588 ;; if we're nested.
4589 (throw 'angle-bracket-arglist-escape nil))))))
4590
4591 (if res
4592 (or c-record-found-types t)))))
4593
4594 (defun c-backward-<>-arglist (all-types &optional limit)
4595 ;; The point is assumed to be directly after a ">". Try to treat it
4596 ;; as the close paren of an angle bracket arglist and move back to
4597 ;; the corresponding "<". If successful, the point is left at
4598 ;; the "<" and t is returned, otherwise the point isn't moved and
4599 ;; nil is returned. ALL-TYPES is passed on to
4600 ;; `c-forward-<>-arglist'.
4601 ;;
4602 ;; If the optional LIMIT is given, it bounds the backward search.
4603 ;; It's then assumed to be at a syntactically relevant position.
4604 ;;
4605 ;; This is a wrapper around `c-forward-<>-arglist'. See that
4606 ;; function for more details.
4607
4608 (let ((start (point)))
4609 (backward-char)
4610 (if (and (not c-parse-and-markup-<>-arglists)
4611 (c-get-char-property (point) 'syntax-table))
4612
4613 (if (and (c-go-up-list-backward)
4614 (eq (char-after) ?<))
4615 t
4616 ;; See corresponding note in `c-forward-<>-arglist'.
4617 (goto-char start)
4618 nil)
4619
4620 (while (progn
4621 (c-syntactic-skip-backward "^<;{}" limit t)
4622
4623 (and
4624 (if (eq (char-before) ?<)
4625 t
4626 ;; Stopped at bob or a char that isn't allowed in an
4627 ;; arglist, so we've failed.
4628 (goto-char start)
4629 nil)
4630
4631 (if (> (point)
4632 (progn (c-beginning-of-current-token)
4633 (point)))
4634 ;; If we moved then the "<" was part of some
4635 ;; multicharacter token.
4636 t
4637
4638 (backward-char)
4639 (let ((beg-pos (point)))
4640 (if (c-forward-<>-arglist all-types)
4641 (cond ((= (point) start)
4642 ;; Matched the arglist. Break the while.
4643 (goto-char beg-pos)
4644 nil)
4645 ((> (point) start)
4646 ;; We started from a non-paren ">" inside an
4647 ;; arglist.
4648 (goto-char start)
4649 nil)
4650 (t
4651 ;; Matched a shorter arglist. Can be a nested
4652 ;; one so continue looking.
4653 (goto-char beg-pos)
4654 t))
4655 t))))))
4656
4657 (/= (point) start))))
4658
4659 (defun c-forward-name ()
4660 ;; Move forward over a complete name if at the beginning of one,
4661 ;; stopping at the next following token. If the point is not at
4662 ;; something that are recognized as name then it stays put. A name
4663 ;; could be something as simple as "foo" in C or something as
4664 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
4665 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
4666 ;; int>::*volatile const" in C++ (this function is actually little
4667 ;; more than a `looking-at' call in all modes except those that,
4668 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
4669 ;; name is found, 'template if it's an identifier ending with an
4670 ;; angle bracket arglist, 'operator of it's an operator identifier,
4671 ;; or t if it's some other kind of name.
4672 ;;
4673 ;; This function records identifier ranges on
4674 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4675 ;; `c-record-type-identifiers' is non-nil.
4676 ;;
4677 ;; This function might do hidden buffer changes.
4678
4679 (let ((pos (point)) (start (point)) res id-start id-end
4680 ;; Turn off `c-promote-possible-types' here since we might
4681 ;; call `c-forward-<>-arglist' and we don't want it to promote
4682 ;; every suspect thing in the arglist to a type. We're
4683 ;; typically called from `c-forward-type' in this case, and
4684 ;; the caller only wants the top level type that it finds to
4685 ;; be promoted.
4686 c-promote-possible-types)
4687 (while
4688 (and
4689 (looking-at c-identifier-key)
4690
4691 (progn
4692 ;; Check for keyword. We go to the last symbol in
4693 ;; `c-identifier-key' first.
4694 (goto-char (setq id-end (match-end 0)))
4695 (c-simple-skip-symbol-backward)
4696 (setq id-start (point))
4697
4698 (if (looking-at c-keywords-regexp)
4699 (when (and (c-major-mode-is 'c++-mode)
4700 (looking-at
4701 (cc-eval-when-compile
4702 (concat "\\(operator\\|\\(template\\)\\)"
4703 "\\(" (c-lang-const c-nonsymbol-key c++)
4704 "\\|$\\)")))
4705 (if (match-beginning 2)
4706 ;; "template" is only valid inside an
4707 ;; identifier if preceded by "::".
4708 (save-excursion
4709 (c-backward-syntactic-ws)
4710 (and (c-safe (backward-char 2) t)
4711 (looking-at "::")))
4712 t))
4713
4714 ;; Handle a C++ operator or template identifier.
4715 (goto-char id-end)
4716 (c-forward-syntactic-ws)
4717 (cond ((eq (char-before id-end) ?e)
4718 ;; Got "... ::template".
4719 (let ((subres (c-forward-name)))
4720 (when subres
4721 (setq pos (point)
4722 res subres))))
4723
4724 ((looking-at c-identifier-start)
4725 ;; Got a cast operator.
4726 (when (c-forward-type)
4727 (setq pos (point)
4728 res 'operator)
4729 ;; Now we should match a sequence of either
4730 ;; '*', '&' or a name followed by ":: *",
4731 ;; where each can be followed by a sequence
4732 ;; of `c-opt-type-modifier-key'.
4733 (while (cond ((looking-at "[*&]")
4734 (goto-char (match-end 0))
4735 t)
4736 ((looking-at c-identifier-start)
4737 (and (c-forward-name)
4738 (looking-at "::")
4739 (progn
4740 (goto-char (match-end 0))
4741 (c-forward-syntactic-ws)
4742 (eq (char-after) ?*))
4743 (progn
4744 (forward-char)
4745 t))))
4746 (while (progn
4747 (c-forward-syntactic-ws)
4748 (setq pos (point))
4749 (looking-at c-opt-type-modifier-key))
4750 (goto-char (match-end 1))))))
4751
4752 ((looking-at c-overloadable-operators-regexp)
4753 ;; Got some other operator.
4754 (setq c-last-identifier-range
4755 (cons (point) (match-end 0)))
4756 (goto-char (match-end 0))
4757 (c-forward-syntactic-ws)
4758 (setq pos (point)
4759 res 'operator)))
4760
4761 nil)
4762
4763 ;; `id-start' is equal to `id-end' if we've jumped over
4764 ;; an identifier that doesn't end with a symbol token.
4765 ;; That can occur e.g. for Java import directives on the
4766 ;; form "foo.bar.*".
4767 (when (and id-start (/= id-start id-end))
4768 (setq c-last-identifier-range
4769 (cons id-start id-end)))
4770 (goto-char id-end)
4771 (c-forward-syntactic-ws)
4772 (setq pos (point)
4773 res t)))
4774
4775 (progn
4776 (goto-char pos)
4777 (when (or c-opt-identifier-concat-key
4778 c-recognize-<>-arglists)
4779
4780 (cond
4781 ((and c-opt-identifier-concat-key
4782 (looking-at c-opt-identifier-concat-key))
4783 ;; Got a concatenated identifier. This handles the
4784 ;; cases with tricky syntactic whitespace that aren't
4785 ;; covered in `c-identifier-key'.
4786 (goto-char (match-end 0))
4787 (c-forward-syntactic-ws)
4788 t)
4789
4790 ((and c-recognize-<>-arglists
4791 (eq (char-after) ?<))
4792 ;; Maybe an angle bracket arglist.
4793
4794 (when (let (c-record-type-identifiers
4795 c-record-found-types)
4796 (c-forward-<>-arglist nil))
4797
4798 (c-add-type start (1+ pos))
4799 (c-forward-syntactic-ws)
4800 (setq pos (point)
4801 c-last-identifier-range nil)
4802
4803 (if (and c-opt-identifier-concat-key
4804 (looking-at c-opt-identifier-concat-key))
4805
4806 ;; Continue if there's an identifier concatenation
4807 ;; operator after the template argument.
4808 (progn
4809 (when (and c-record-type-identifiers id-start)
4810 (c-record-ref-id (cons id-start id-end)))
4811 (forward-char 2)
4812 (c-forward-syntactic-ws)
4813 t)
4814
4815 (when (and c-record-type-identifiers id-start)
4816 (c-record-type-id (cons id-start id-end)))
4817 (setq res 'template)
4818 nil)))
4819 )))))
4820
4821 (goto-char pos)
4822 res))
4823
4824 (defun c-forward-type ()
4825 ;; Move forward over a type spec if at the beginning of one,
4826 ;; stopping at the next following token. Return t if it's a known
4827 ;; type that can't be a name or other expression, 'known if it's an
4828 ;; otherwise known type (according to `*-font-lock-extra-types'),
4829 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4830 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4831 ;; that might be a type, or nil if it can't be a type (the point
4832 ;; isn't moved then). The point is assumed to be at the beginning
4833 ;; of a token.
4834 ;;
4835 ;; Note that this function doesn't skip past the brace definition
4836 ;; that might be considered part of the type, e.g.
4837 ;; "enum {a, b, c} foo".
4838 ;;
4839 ;; This function records identifier ranges on
4840 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4841 ;; `c-record-type-identifiers' is non-nil.
4842 ;;
4843 ;; This function might do hidden buffer changes.
4844
4845 (let ((start (point)) pos res name-res id-start id-end id-range)
4846
4847 ;; Skip leading type modifiers. If any are found we know it's a
4848 ;; prefix of a type.
4849 (when c-opt-type-modifier-key
4850 (while (looking-at c-opt-type-modifier-key)
4851 (goto-char (match-end 1))
4852 (c-forward-syntactic-ws)
4853 (setq res 'prefix)))
4854
4855 (cond
4856 ((looking-at c-type-prefix-key)
4857 ;; Looking at a keyword that prefixes a type identifier,
4858 ;; e.g. "class".
4859 (goto-char (match-end 1))
4860 (c-forward-syntactic-ws)
4861 (setq pos (point))
4862 (if (memq (setq name-res (c-forward-name)) '(t template))
4863 (progn
4864 (when (eq name-res t)
4865 ;; In many languages the name can be used without the
4866 ;; prefix, so we add it to `c-found-types'.
4867 (c-add-type pos (point))
4868 (when (and c-record-type-identifiers
4869 c-last-identifier-range)
4870 (c-record-type-id c-last-identifier-range)))
4871 (setq res t))
4872 ;; Invalid syntax.
4873 (goto-char start)
4874 (setq res nil)))
4875
4876 ((progn
4877 (setq pos nil)
4878 (if (looking-at c-identifier-start)
4879 (save-excursion
4880 (setq id-start (point)
4881 name-res (c-forward-name))
4882 (when name-res
4883 (setq id-end (point)
4884 id-range c-last-identifier-range))))
4885 (and (cond ((looking-at c-primitive-type-key)
4886 (setq res t))
4887 ((c-with-syntax-table c-identifier-syntax-table
4888 (looking-at c-known-type-key))
4889 (setq res 'known)))
4890 (or (not id-end)
4891 (>= (save-excursion
4892 (save-match-data
4893 (goto-char (match-end 1))
4894 (c-forward-syntactic-ws)
4895 (setq pos (point))))
4896 id-end)
4897 (setq res nil))))
4898 ;; Looking at a primitive or known type identifier. We've
4899 ;; checked for a name first so that we don't go here if the
4900 ;; known type match only is a prefix of another name.
4901
4902 (setq id-end (match-end 1))
4903
4904 (when (and c-record-type-identifiers
4905 (or c-promote-possible-types (eq res t)))
4906 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4907
4908 (if (and c-opt-type-component-key
4909 (save-match-data
4910 (looking-at c-opt-type-component-key)))
4911 ;; There might be more keywords for the type.
4912 (let (safe-pos)
4913 (c-forward-keyword-clause 1)
4914 (while (progn
4915 (setq safe-pos (point))
4916 (looking-at c-opt-type-component-key))
4917 (when (and c-record-type-identifiers
4918 (looking-at c-primitive-type-key))
4919 (c-record-type-id (cons (match-beginning 1)
4920 (match-end 1))))
4921 (c-forward-keyword-clause 1))
4922 (if (looking-at c-primitive-type-key)
4923 (progn
4924 (when c-record-type-identifiers
4925 (c-record-type-id (cons (match-beginning 1)
4926 (match-end 1))))
4927 (c-forward-keyword-clause 1)
4928 (setq res t))
4929 (goto-char safe-pos)
4930 (setq res 'prefix)))
4931 (unless (save-match-data (c-forward-keyword-clause 1))
4932 (if pos
4933 (goto-char pos)
4934 (goto-char (match-end 1))
4935 (c-forward-syntactic-ws)))))
4936
4937 (name-res
4938 (cond ((eq name-res t)
4939 ;; A normal identifier.
4940 (goto-char id-end)
4941 (if (or res c-promote-possible-types)
4942 (progn
4943 (c-add-type id-start id-end)
4944 (when (and c-record-type-identifiers id-range)
4945 (c-record-type-id id-range))
4946 (unless res
4947 (setq res 'found)))
4948 (setq res (if (c-check-type id-start id-end)
4949 ;; It's an identifier that has been used as
4950 ;; a type somewhere else.
4951 'found
4952 ;; It's an identifier that might be a type.
4953 'maybe))))
4954 ((eq name-res 'template)
4955 ;; A template is a type.
4956 (goto-char id-end)
4957 (setq res t))
4958 (t
4959 ;; Otherwise it's an operator identifier, which is not a type.
4960 (goto-char start)
4961 (setq res nil)))))
4962
4963 (when res
4964 ;; Skip trailing type modifiers. If any are found we know it's
4965 ;; a type.
4966 (when c-opt-type-modifier-key
4967 (while (looking-at c-opt-type-modifier-key)
4968 (goto-char (match-end 1))
4969 (c-forward-syntactic-ws)
4970 (setq res t)))
4971
4972 ;; Step over any type suffix operator. Do not let the existence
4973 ;; of these alter the classification of the found type, since
4974 ;; these operators typically are allowed in normal expressions
4975 ;; too.
4976 (when c-opt-type-suffix-key
4977 (while (looking-at c-opt-type-suffix-key)
4978 (goto-char (match-end 1))
4979 (c-forward-syntactic-ws)))
4980
4981 (when c-opt-type-concat-key
4982 ;; Look for a trailing operator that concatenates the type
4983 ;; with a following one, and if so step past that one through
4984 ;; a recursive call. Note that we don't record concatenated
4985 ;; types in `c-found-types' - it's the component types that
4986 ;; are recorded when appropriate.
4987 (setq pos (point))
4988 (let* ((c-promote-possible-types (or (memq res '(t known))
4989 c-promote-possible-types))
4990 ;; If we can't promote then set `c-record-found-types' so that
4991 ;; we can merge in the types from the second part afterwards if
4992 ;; it turns out to be a known type there.
4993 (c-record-found-types (and c-record-type-identifiers
4994 (not c-promote-possible-types)))
4995 subres)
4996 (if (and (looking-at c-opt-type-concat-key)
4997
4998 (progn
4999 (goto-char (match-end 1))
5000 (c-forward-syntactic-ws)
5001 (setq subres (c-forward-type))))
5002
5003 (progn
5004 ;; If either operand certainly is a type then both are, but we
5005 ;; don't let the existence of the operator itself promote two
5006 ;; uncertain types to a certain one.
5007 (cond ((eq res t))
5008 ((eq subres t)
5009 (unless (eq name-res 'template)
5010 (c-add-type id-start id-end))
5011 (when (and c-record-type-identifiers id-range)
5012 (c-record-type-id id-range))
5013 (setq res t))
5014 ((eq res 'known))
5015 ((eq subres 'known)
5016 (setq res 'known))
5017 ((eq res 'found))
5018 ((eq subres 'found)
5019 (setq res 'found))
5020 (t
5021 (setq res 'maybe)))
5022
5023 (when (and (eq res t)
5024 (consp c-record-found-types))
5025 ;; Merge in the ranges of any types found by the second
5026 ;; `c-forward-type'.
5027 (setq c-record-type-identifiers
5028 ;; `nconc' doesn't mind that the tail of
5029 ;; `c-record-found-types' is t.
5030 (nconc c-record-found-types
5031 c-record-type-identifiers))))
5032
5033 (goto-char pos))))
5034
5035 (when (and c-record-found-types (memq res '(known found)) id-range)
5036 (setq c-record-found-types
5037 (cons id-range c-record-found-types))))
5038
5039 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
5040
5041 res))
5042
5043 \f
5044 ;; Handling of large scale constructs like statements and declarations.
5045
5046 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
5047 ;; defsubst or perhaps even a defun, but it contains lots of free
5048 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
5049 (defmacro c-fdoc-shift-type-backward (&optional short)
5050 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
5051 ;; of types when parsing a declaration, which means that it
5052 ;; sometimes consumes the identifier in the declaration as a type.
5053 ;; This is used to "backtrack" and make the last type be treated as
5054 ;; an identifier instead.
5055 `(progn
5056 ,(unless short
5057 ;; These identifiers are bound only in the inner let.
5058 '(setq identifier-type at-type
5059 identifier-start type-start
5060 got-parens nil
5061 got-identifier t
5062 got-suffix t
5063 got-suffix-after-parens id-start
5064 paren-depth 0))
5065
5066 (if (setq at-type (if (eq backup-at-type 'prefix)
5067 t
5068 backup-at-type))
5069 (setq type-start backup-type-start
5070 id-start backup-id-start)
5071 (setq type-start start-pos
5072 id-start start-pos))
5073
5074 ;; When these flags already are set we've found specifiers that
5075 ;; unconditionally signal these attributes - backtracking doesn't
5076 ;; change that. So keep them set in that case.
5077 (or at-type-decl
5078 (setq at-type-decl backup-at-type-decl))
5079 (or maybe-typeless
5080 (setq maybe-typeless backup-maybe-typeless))
5081
5082 ,(unless short
5083 ;; This identifier is bound only in the inner let.
5084 '(setq start id-start))))
5085
5086 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
5087 ;; Move forward over a declaration or a cast if at the start of one.
5088 ;; The point is assumed to be at the start of some token. Nil is
5089 ;; returned if no declaration or cast is recognized, and the point
5090 ;; is clobbered in that case.
5091 ;;
5092 ;; If a declaration is parsed:
5093 ;;
5094 ;; The point is left at the first token after the first complete
5095 ;; declarator, if there is one. The return value is a cons where
5096 ;; the car is the position of the first token in the declarator. (See
5097 ;; below for the cdr.)
5098 ;; Some examples:
5099 ;;
5100 ;; void foo (int a, char *b) stuff ...
5101 ;; car ^ ^ point
5102 ;; float (*a)[], b;
5103 ;; car ^ ^ point
5104 ;; unsigned int a = c_style_initializer, b;
5105 ;; car ^ ^ point
5106 ;; unsigned int a (cplusplus_style_initializer), b;
5107 ;; car ^ ^ point (might change)
5108 ;; class Foo : public Bar {}
5109 ;; car ^ ^ point
5110 ;; class PikeClass (int a, string b) stuff ...
5111 ;; car ^ ^ point
5112 ;; enum bool;
5113 ;; car ^ ^ point
5114 ;; enum bool flag;
5115 ;; car ^ ^ point
5116 ;; void cplusplus_function (int x) throw (Bad);
5117 ;; car ^ ^ point
5118 ;; Foo::Foo (int b) : Base (b) {}
5119 ;; car ^ ^ point
5120 ;;
5121 ;; The cdr of the return value is non-nil iff a `c-typedef-decl-kwds'
5122 ;; specifier (e.g. class, struct, enum, typedef) is found in the
5123 ;; declaration, i.e. the declared identifier(s) are types.
5124 ;;
5125 ;; If a cast is parsed:
5126 ;;
5127 ;; The point is left at the first token after the closing paren of
5128 ;; the cast. The return value is `cast'. Note that the start
5129 ;; position must be at the first token inside the cast parenthesis
5130 ;; to recognize it.
5131 ;;
5132 ;; PRECEDING-TOKEN-END is the first position after the preceding
5133 ;; token, i.e. on the other side of the syntactic ws from the point.
5134 ;; Use a value less than or equal to (point-min) if the point is at
5135 ;; the first token in (the visible part of) the buffer.
5136 ;;
5137 ;; CONTEXT is a symbol that describes the context at the point:
5138 ;; 'decl In a comma-separated declaration context (typically
5139 ;; inside a function declaration arglist).
5140 ;; '<> In an angle bracket arglist.
5141 ;; 'arglist Some other type of arglist.
5142 ;; nil Some other context or unknown context.
5143 ;;
5144 ;; LAST-CAST-END is the first token after the closing paren of a
5145 ;; preceding cast, or nil if none is known. If
5146 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
5147 ;; the position after the closest preceding call where a cast was
5148 ;; matched. In that case it's used to discover chains of casts like
5149 ;; "(a) (b) c".
5150 ;;
5151 ;; This function records identifier ranges on
5152 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5153 ;; `c-record-type-identifiers' is non-nil.
5154 ;;
5155 ;; This function might do hidden buffer changes.
5156
5157 (let (;; `start-pos' is used below to point to the start of the
5158 ;; first type, i.e. after any leading specifiers. It might
5159 ;; also point at the beginning of the preceding syntactic
5160 ;; whitespace.
5161 (start-pos (point))
5162 ;; Set to the result of `c-forward-type'.
5163 at-type
5164 ;; The position of the first token in what we currently
5165 ;; believe is the type in the declaration or cast, after any
5166 ;; specifiers and their associated clauses.
5167 type-start
5168 ;; The position of the first token in what we currently
5169 ;; believe is the declarator for the first identifier. Set
5170 ;; when the type is found, and moved forward over any
5171 ;; `c-decl-hangon-kwds' and their associated clauses that
5172 ;; occurs after the type.
5173 id-start
5174 ;; These store `at-type', `type-start' and `id-start' of the
5175 ;; identifier before the one in those variables. The previous
5176 ;; identifier might turn out to be the real type in a
5177 ;; declaration if the last one has to be the declarator in it.
5178 ;; If `backup-at-type' is nil then the other variables have
5179 ;; undefined values.
5180 backup-at-type backup-type-start backup-id-start
5181 ;; Set if we've found a specifier that makes the defined
5182 ;; identifier(s) types.
5183 at-type-decl
5184 ;; Set if we've found a specifier that can start a declaration
5185 ;; where there's no type.
5186 maybe-typeless
5187 ;; If a specifier is found that also can be a type prefix,
5188 ;; these flags are set instead of those above. If we need to
5189 ;; back up an identifier, they are copied to the real flag
5190 ;; variables. Thus they only take effect if we fail to
5191 ;; interpret it as a type.
5192 backup-at-type-decl backup-maybe-typeless
5193 ;; Whether we've found a declaration or a cast. We might know
5194 ;; this before we've found the type in it. It's 'ids if we've
5195 ;; found two consecutive identifiers (usually a sure sign, but
5196 ;; we should allow that in labels too), and t if we've found a
5197 ;; specifier keyword (a 100% sure sign).
5198 at-decl-or-cast
5199 ;; Set when we need to back up to parse this as a declaration
5200 ;; but not as a cast.
5201 backup-if-not-cast
5202 ;; For casts, the return position.
5203 cast-end
5204 ;; Save `c-record-type-identifiers' and
5205 ;; `c-record-ref-identifiers' since ranges are recorded
5206 ;; speculatively and should be thrown away if it turns out
5207 ;; that it isn't a declaration or cast.
5208 (save-rec-type-ids c-record-type-identifiers)
5209 (save-rec-ref-ids c-record-ref-identifiers))
5210
5211 ;; Check for a type. Unknown symbols are treated as possible
5212 ;; types, but they could also be specifiers disguised through
5213 ;; macros like __INLINE__, so we recognize both types and known
5214 ;; specifiers after them too.
5215 (while
5216 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
5217
5218 ;; Look for a specifier keyword clause.
5219 (when (looking-at c-prefix-spec-kwds-re)
5220 (setq kwd-sym (c-keyword-sym (match-string 1)))
5221 (save-excursion
5222 (c-forward-keyword-clause 1)
5223 (setq kwd-clause-end (point))))
5224
5225 (when (setq found-type (c-forward-type))
5226 ;; Found a known or possible type or a prefix of a known type.
5227
5228 (when at-type
5229 ;; Got two identifiers with nothing but whitespace
5230 ;; between them. That can only happen in declarations.
5231 (setq at-decl-or-cast 'ids)
5232
5233 (when (eq at-type 'found)
5234 ;; If the previous identifier is a found type we
5235 ;; record it as a real one; it might be some sort of
5236 ;; alias for a prefix like "unsigned".
5237 (save-excursion
5238 (goto-char type-start)
5239 (let ((c-promote-possible-types t))
5240 (c-forward-type)))))
5241
5242 (setq backup-at-type at-type
5243 backup-type-start type-start
5244 backup-id-start id-start
5245 at-type found-type
5246 type-start start
5247 id-start (point)
5248 ;; The previous ambiguous specifier/type turned out
5249 ;; to be a type since we've parsed another one after
5250 ;; it, so clear these backup flags.
5251 backup-at-type-decl nil
5252 backup-maybe-typeless nil))
5253
5254 (if kwd-sym
5255 (progn
5256 ;; Handle known specifier keywords and
5257 ;; `c-decl-hangon-kwds' which can occur after known
5258 ;; types.
5259
5260 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
5261 ;; It's a hang-on keyword that can occur anywhere.
5262 (progn
5263 (setq at-decl-or-cast t)
5264 (if at-type
5265 ;; Move the identifier start position if
5266 ;; we've passed a type.
5267 (setq id-start kwd-clause-end)
5268 ;; Otherwise treat this as a specifier and
5269 ;; move the fallback position.
5270 (setq start-pos kwd-clause-end))
5271 (goto-char kwd-clause-end))
5272
5273 ;; It's an ordinary specifier so we know that
5274 ;; anything before this can't be the type.
5275 (setq backup-at-type nil
5276 start-pos kwd-clause-end)
5277
5278 (if found-type
5279 ;; It's ambiguous whether this keyword is a
5280 ;; specifier or a type prefix, so set the backup
5281 ;; flags. (It's assumed that `c-forward-type'
5282 ;; moved further than `c-forward-keyword-clause'.)
5283 (progn
5284 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5285 (setq backup-at-type-decl t))
5286 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5287 (setq backup-maybe-typeless t)))
5288
5289 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5290 (setq at-type-decl t))
5291 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5292 (setq maybe-typeless t))
5293
5294 ;; Haven't matched a type so it's an umambiguous
5295 ;; specifier keyword and we know we're in a
5296 ;; declaration.
5297 (setq at-decl-or-cast t)
5298
5299 (goto-char kwd-clause-end))))
5300
5301 ;; If the type isn't known we continue so that we'll jump
5302 ;; over all specifiers and type identifiers. The reason
5303 ;; to do this for a known type prefix is to make things
5304 ;; like "unsigned INT16" work.
5305 (and found-type (not (eq found-type t))))))
5306
5307 (cond
5308 ((eq at-type t)
5309 ;; If a known type was found, we still need to skip over any
5310 ;; hangon keyword clauses after it. Otherwise it has already
5311 ;; been done in the loop above.
5312 (while (looking-at c-decl-hangon-key)
5313 (c-forward-keyword-clause 1))
5314 (setq id-start (point)))
5315
5316 ((eq at-type 'prefix)
5317 ;; A prefix type is itself a primitive type when it's not
5318 ;; followed by another type.
5319 (setq at-type t))
5320
5321 ((not at-type)
5322 ;; Got no type but set things up to continue anyway to handle
5323 ;; the various cases when a declaration doesn't start with a
5324 ;; type.
5325 (setq id-start start-pos))
5326
5327 ((and (eq at-type 'maybe)
5328 (c-major-mode-is 'c++-mode))
5329 ;; If it's C++ then check if the last "type" ends on the form
5330 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
5331 ;; (con|de)structor.
5332 (save-excursion
5333 (let (name end-2 end-1)
5334 (goto-char id-start)
5335 (c-backward-syntactic-ws)
5336 (setq end-2 (point))
5337 (when (and
5338 (c-simple-skip-symbol-backward)
5339 (progn
5340 (setq name
5341 (buffer-substring-no-properties (point) end-2))
5342 ;; Cheating in the handling of syntactic ws below.
5343 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
5344 (progn
5345 (setq end-1 (point))
5346 (c-simple-skip-symbol-backward))
5347 (>= (point) type-start)
5348 (equal (buffer-substring-no-properties (point) end-1)
5349 name))
5350 ;; It is a (con|de)structor name. In that case the
5351 ;; declaration is typeless so zap out any preceding
5352 ;; identifier(s) that we might have taken as types.
5353 (goto-char type-start)
5354 (setq at-type nil
5355 backup-at-type nil
5356 id-start type-start))))))
5357
5358 ;; Check for and step over a type decl expression after the thing
5359 ;; that is or might be a type. This can't be skipped since we
5360 ;; need the correct end position of the declarator for
5361 ;; `max-type-decl-end-*'.
5362 (let ((start (point)) (paren-depth 0) pos
5363 ;; True if there's a non-open-paren match of
5364 ;; `c-type-decl-prefix-key'.
5365 got-prefix
5366 ;; True if the declarator is surrounded by a parenthesis pair.
5367 got-parens
5368 ;; True if there is an identifier in the declarator.
5369 got-identifier
5370 ;; True if there's a non-close-paren match of
5371 ;; `c-type-decl-suffix-key'.
5372 got-suffix
5373 ;; True if there's a prefix match outside the outermost
5374 ;; paren pair that surrounds the declarator.
5375 got-prefix-before-parens
5376 ;; True if there's a suffix match outside the outermost
5377 ;; paren pair that surrounds the declarator. The value is
5378 ;; the position of the first suffix match.
5379 got-suffix-after-parens
5380 ;; True if we've parsed the type decl to a token that is
5381 ;; known to end declarations in this context.
5382 at-decl-end
5383 ;; The earlier values of `at-type' and `type-start' if we've
5384 ;; shifted the type backwards.
5385 identifier-type identifier-start
5386 ;; If `c-parse-and-markup-<>-arglists' is set we need to
5387 ;; turn it off during the name skipping below to avoid
5388 ;; getting `c-type' properties that might be bogus. That
5389 ;; can happen since we don't know if
5390 ;; `c-restricted-<>-arglists' will be correct inside the
5391 ;; arglist paren that gets entered.
5392 c-parse-and-markup-<>-arglists)
5393
5394 (goto-char id-start)
5395
5396 ;; Skip over type decl prefix operators. (Note similar code in
5397 ;; `c-font-lock-declarators'.)
5398 (while (and (looking-at c-type-decl-prefix-key)
5399 (if (and (c-major-mode-is 'c++-mode)
5400 (match-beginning 2))
5401 ;; If the second submatch matches in C++ then
5402 ;; we're looking at an identifier that's a
5403 ;; prefix only if it specifies a member pointer.
5404 (when (setq got-identifier (c-forward-name))
5405 (if (looking-at "\\(::\\)")
5406 ;; We only check for a trailing "::" and
5407 ;; let the "*" that should follow be
5408 ;; matched in the next round.
5409 (progn (setq got-identifier nil) t)
5410 ;; It turned out to be the real identifier,
5411 ;; so stop.
5412 nil))
5413 t))
5414
5415 (if (eq (char-after) ?\()
5416 (progn
5417 (setq paren-depth (1+ paren-depth))
5418 (forward-char))
5419 (unless got-prefix-before-parens
5420 (setq got-prefix-before-parens (= paren-depth 0)))
5421 (setq got-prefix t)
5422 (goto-char (match-end 1)))
5423 (c-forward-syntactic-ws))
5424
5425 (setq got-parens (> paren-depth 0))
5426
5427 ;; Skip over an identifier.
5428 (or got-identifier
5429 (and (looking-at c-identifier-start)
5430 (setq got-identifier (c-forward-name))))
5431
5432 ;; Skip over type decl suffix operators.
5433 (while (if (looking-at c-type-decl-suffix-key)
5434
5435 (if (eq (char-after) ?\))
5436 (when (> paren-depth 0)
5437 (setq paren-depth (1- paren-depth))
5438 (forward-char)
5439 t)
5440 (when (if (save-match-data (looking-at "\\s\("))
5441 (c-safe (c-forward-sexp 1) t)
5442 (goto-char (match-end 1))
5443 t)
5444 (when (and (not got-suffix-after-parens)
5445 (= paren-depth 0))
5446 (setq got-suffix-after-parens (match-beginning 0)))
5447 (setq got-suffix t)))
5448
5449 ;; No suffix matched. We might have matched the
5450 ;; identifier as a type and the open paren of a
5451 ;; function arglist as a type decl prefix. In that
5452 ;; case we should "backtrack": Reinterpret the last
5453 ;; type as the identifier, move out of the arglist and
5454 ;; continue searching for suffix operators.
5455 ;;
5456 ;; Do this even if there's no preceding type, to cope
5457 ;; with old style function declarations in K&R C,
5458 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
5459 ;; style declarations. That isn't applicable in an
5460 ;; arglist context, though.
5461 (when (and (= paren-depth 1)
5462 (not got-prefix-before-parens)
5463 (not (eq at-type t))
5464 (or backup-at-type
5465 maybe-typeless
5466 backup-maybe-typeless
5467 (when c-recognize-typeless-decls
5468 (not context)))
5469 (setq pos (c-up-list-forward (point)))
5470 (eq (char-before pos) ?\)))
5471 (c-fdoc-shift-type-backward)
5472 (goto-char pos)
5473 t))
5474
5475 (c-forward-syntactic-ws))
5476
5477 (when (and (or maybe-typeless backup-maybe-typeless)
5478 (not got-identifier)
5479 (not got-prefix)
5480 at-type)
5481 ;; Have found no identifier but `c-typeless-decl-kwds' has
5482 ;; matched so we know we're inside a declaration. The
5483 ;; preceding type must be the identifier instead.
5484 (c-fdoc-shift-type-backward))
5485
5486 (setq
5487 at-decl-or-cast
5488 (catch 'at-decl-or-cast
5489
5490 (when (> paren-depth 0)
5491 ;; Encountered something inside parens that isn't matched by
5492 ;; the `c-type-decl-*' regexps, so it's not a type decl
5493 ;; expression. Try to skip out to the same paren depth to
5494 ;; not confuse the cast check below.
5495 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
5496 ;; If we've found a specifier keyword then it's a
5497 ;; declaration regardless.
5498 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
5499
5500 (setq at-decl-end
5501 (looking-at (cond ((eq context '<>) "[,>]")
5502 (context "[,\)]")
5503 (t "[,;]"))))
5504
5505 ;; Now we've collected info about various characteristics of
5506 ;; the construct we're looking at. Below follows a decision
5507 ;; tree based on that. It's ordered to check more certain
5508 ;; signs before less certain ones.
5509
5510 (if got-identifier
5511 (progn
5512
5513 (when (and (or at-type maybe-typeless)
5514 (not (or got-prefix got-parens)))
5515 ;; Got another identifier directly after the type, so it's a
5516 ;; declaration.
5517 (throw 'at-decl-or-cast t))
5518
5519 (when (and got-parens
5520 (not got-prefix)
5521 (not got-suffix-after-parens)
5522 (or backup-at-type
5523 maybe-typeless
5524 backup-maybe-typeless))
5525 ;; Got a declaration of the form "foo bar (gnu);" where we've
5526 ;; recognized "bar" as the type and "gnu" as the declarator.
5527 ;; In this case it's however more likely that "bar" is the
5528 ;; declarator and "gnu" a function argument or initializer (if
5529 ;; `c-recognize-paren-inits' is set), since the parens around
5530 ;; "gnu" would be superfluous if it's a declarator. Shift the
5531 ;; type one step backward.
5532 (c-fdoc-shift-type-backward)))
5533
5534 ;; Found no identifier.
5535
5536 (if backup-at-type
5537 (progn
5538
5539 (when (= (point) start)
5540 ;; Got a plain list of identifiers. If a colon follows it's
5541 ;; a valid label. Otherwise the last one probably is the
5542 ;; declared identifier and we should back up to the previous
5543 ;; type, providing it isn't a cast.
5544 (if (eq (char-after) ?:)
5545 ;; If we've found a specifier keyword then it's a
5546 ;; declaration regardless.
5547 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
5548 (setq backup-if-not-cast t)
5549 (throw 'at-decl-or-cast t)))
5550
5551 (when (and got-suffix
5552 (not got-prefix)
5553 (not got-parens))
5554 ;; Got a plain list of identifiers followed by some suffix.
5555 ;; If this isn't a cast then the last identifier probably is
5556 ;; the declared one and we should back up to the previous
5557 ;; type.
5558 (setq backup-if-not-cast t)
5559 (throw 'at-decl-or-cast t)))
5560
5561 (when (eq at-type t)
5562 ;; If the type is known we know that there can't be any
5563 ;; identifier somewhere else, and it's only in declarations in
5564 ;; e.g. function prototypes and in casts that the identifier may
5565 ;; be left out.
5566 (throw 'at-decl-or-cast t))
5567
5568 (when (= (point) start)
5569 ;; Only got a single identifier (parsed as a type so far).
5570 (if (and
5571 ;; Check that the identifier isn't at the start of an
5572 ;; expression.
5573 at-decl-end
5574 (cond
5575 ((eq context 'decl)
5576 ;; Inside an arglist that contains declarations. If K&R
5577 ;; style declarations and parenthesis style initializers
5578 ;; aren't allowed then the single identifier must be a
5579 ;; type, else we require that it's known or found
5580 ;; (primitive types are handled above).
5581 (or (and (not c-recognize-knr-p)
5582 (not c-recognize-paren-inits))
5583 (memq at-type '(known found))))
5584 ((eq context '<>)
5585 ;; Inside a template arglist. Accept known and found
5586 ;; types; other identifiers could just as well be
5587 ;; constants in C++.
5588 (memq at-type '(known found)))))
5589 (throw 'at-decl-or-cast t)
5590 ;; Can't be a valid declaration or cast, but if we've found a
5591 ;; specifier it can't be anything else either, so treat it as
5592 ;; an invalid/unfinished declaration or cast.
5593 (throw 'at-decl-or-cast at-decl-or-cast))))
5594
5595 (if (and got-parens
5596 (not got-prefix)
5597 (not context)
5598 (not (eq at-type t))
5599 (or backup-at-type
5600 maybe-typeless
5601 backup-maybe-typeless
5602 (when c-recognize-typeless-decls
5603 (or (not got-suffix)
5604 (not (looking-at
5605 c-after-suffixed-type-maybe-decl-key))))))
5606 ;; Got an empty paren pair and a preceding type that probably
5607 ;; really is the identifier. Shift the type backwards to make
5608 ;; the last one the identifier. This is analogous to the
5609 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
5610 ;; above.
5611 ;;
5612 ;; Exception: In addition to the conditions in that
5613 ;; "backtracking" code, do not shift backward if we're not
5614 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
5615 ;; Since there's no preceding type, the shift would mean that
5616 ;; the declaration is typeless. But if the regexp doesn't match
5617 ;; then we will simply fall through in the tests below and not
5618 ;; recognize it at all, so it's better to try it as an abstract
5619 ;; declarator instead.
5620 (c-fdoc-shift-type-backward)
5621
5622 ;; Still no identifier.
5623
5624 (when (and got-prefix (or got-parens got-suffix))
5625 ;; Require `got-prefix' together with either `got-parens' or
5626 ;; `got-suffix' to recognize it as an abstract declarator:
5627 ;; `got-parens' only is probably an empty function call.
5628 ;; `got-suffix' only can build an ordinary expression together
5629 ;; with the preceding identifier which we've taken as a type.
5630 ;; We could actually accept on `got-prefix' only, but that can
5631 ;; easily occur temporarily while writing an expression so we
5632 ;; avoid that case anyway. We could do a better job if we knew
5633 ;; the point when the fontification was invoked.
5634 (throw 'at-decl-or-cast t))
5635
5636 (when (and at-type
5637 (not got-prefix)
5638 (not got-parens)
5639 got-suffix-after-parens
5640 (eq (char-after got-suffix-after-parens) ?\())
5641 ;; Got a type, no declarator but a paren suffix. I.e. it's a
5642 ;; normal function call afterall (or perhaps a C++ style object
5643 ;; instantiation expression).
5644 (throw 'at-decl-or-cast nil))))
5645
5646 (when at-decl-or-cast
5647 ;; By now we've located the type in the declaration that we know
5648 ;; we're in.
5649 (throw 'at-decl-or-cast t))
5650
5651 (when (and got-identifier
5652 (not context)
5653 (looking-at c-after-suffixed-type-decl-key)
5654 (if (and got-parens
5655 (not got-prefix)
5656 (not got-suffix)
5657 (not (eq at-type t)))
5658 ;; Shift the type backward in the case that there's a
5659 ;; single identifier inside parens. That can only
5660 ;; occur in K&R style function declarations so it's
5661 ;; more likely that it really is a function call.
5662 ;; Therefore we only do this after
5663 ;; `c-after-suffixed-type-decl-key' has matched.
5664 (progn (c-fdoc-shift-type-backward) t)
5665 got-suffix-after-parens))
5666 ;; A declaration according to `c-after-suffixed-type-decl-key'.
5667 (throw 'at-decl-or-cast t))
5668
5669 (when (and (or got-prefix (not got-parens))
5670 (memq at-type '(t known)))
5671 ;; It's a declaration if a known type precedes it and it can't be a
5672 ;; function call.
5673 (throw 'at-decl-or-cast t))
5674
5675 ;; If we get here we can't tell if this is a type decl or a normal
5676 ;; expression by looking at it alone. (That's under the assumption
5677 ;; that normal expressions always can look like type decl expressions,
5678 ;; which isn't really true but the cases where it doesn't hold are so
5679 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
5680 ;; the effort to look for them.)
5681
5682 (unless (or at-decl-end (looking-at "=[^=]"))
5683 ;; If this is a declaration it should end here or its initializer(*)
5684 ;; should start here, so check for allowed separation tokens. Note
5685 ;; that this rule doesn't work e.g. with a K&R arglist after a
5686 ;; function header.
5687 ;;
5688 ;; *) Don't check for C++ style initializers using parens
5689 ;; since those already have been matched as suffixes.
5690 ;;
5691 ;; If `at-decl-or-cast' is then we've found some other sign that
5692 ;; it's a declaration or cast, so then it's probably an
5693 ;; invalid/unfinished one.
5694 (throw 'at-decl-or-cast at-decl-or-cast))
5695
5696 ;; Below are tests that only should be applied when we're certain to
5697 ;; not have parsed halfway through an expression.
5698
5699 (when (memq at-type '(t known))
5700 ;; The expression starts with a known type so treat it as a
5701 ;; declaration.
5702 (throw 'at-decl-or-cast t))
5703
5704 (when (and (c-major-mode-is 'c++-mode)
5705 ;; In C++ we check if the identifier is a known type, since
5706 ;; (con|de)structors use the class name as identifier.
5707 ;; We've always shifted over the identifier as a type and
5708 ;; then backed up again in this case.
5709 identifier-type
5710 (or (memq identifier-type '(found known))
5711 (and (eq (char-after identifier-start) ?~)
5712 ;; `at-type' probably won't be 'found for
5713 ;; destructors since the "~" is then part of the
5714 ;; type name being checked against the list of
5715 ;; known types, so do a check without that
5716 ;; operator.
5717 (or (save-excursion
5718 (goto-char (1+ identifier-start))
5719 (c-forward-syntactic-ws)
5720 (c-with-syntax-table
5721 c-identifier-syntax-table
5722 (looking-at c-known-type-key)))
5723 (save-excursion
5724 (goto-char (1+ identifier-start))
5725 ;; We have already parsed the type earlier,
5726 ;; so it'd be possible to cache the end
5727 ;; position instead of redoing it here, but
5728 ;; then we'd need to keep track of another
5729 ;; position everywhere.
5730 (c-check-type (point)
5731 (progn (c-forward-type)
5732 (point))))))))
5733 (throw 'at-decl-or-cast t))
5734
5735 (if got-identifier
5736 (progn
5737 (when (and got-prefix-before-parens
5738 at-type
5739 (or at-decl-end (looking-at "=[^=]"))
5740 (not context)
5741 (not got-suffix))
5742 ;; Got something like "foo * bar;". Since we're not inside an
5743 ;; arglist it would be a meaningless expression because the
5744 ;; result isn't used. We therefore choose to recognize it as
5745 ;; a declaration. Do not allow a suffix since it could then
5746 ;; be a function call.
5747 (throw 'at-decl-or-cast t))
5748
5749 (when (and (or got-suffix-after-parens
5750 (looking-at "=[^=]"))
5751 (eq at-type 'found)
5752 (not (eq context 'arglist)))
5753 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
5754 ;; be an odd expression or it could be a declaration. Treat
5755 ;; it as a declaration if "a" has been used as a type
5756 ;; somewhere else (if it's a known type we won't get here).
5757 (throw 'at-decl-or-cast t)))
5758
5759 (when (and context
5760 (or got-prefix
5761 (and (eq context 'decl)
5762 (not c-recognize-paren-inits)
5763 (or got-parens got-suffix))))
5764 ;; Got a type followed by an abstract declarator. If `got-prefix'
5765 ;; is set it's something like "a *" without anything after it. If
5766 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
5767 ;; or similar, which we accept only if the context rules out
5768 ;; expressions.
5769 (throw 'at-decl-or-cast t)))
5770
5771 ;; If we had a complete symbol table here (which rules out
5772 ;; `c-found-types') we should return t due to the disambiguation rule
5773 ;; (in at least C++) that anything that can be parsed as a declaration
5774 ;; is a declaration. Now we're being more defensive and prefer to
5775 ;; highlight things like "foo (bar);" as a declaration only if we're
5776 ;; inside an arglist that contains declarations.
5777 (eq context 'decl))))
5778
5779 ;; The point is now after the type decl expression.
5780
5781 (cond
5782 ;; Check for a cast.
5783 ((save-excursion
5784 (and
5785 c-cast-parens
5786
5787 ;; Should be the first type/identifier in a cast paren.
5788 (> preceding-token-end (point-min))
5789 (memq (char-before preceding-token-end) c-cast-parens)
5790
5791 ;; The closing paren should follow.
5792 (progn
5793 (c-forward-syntactic-ws)
5794 (looking-at "\\s\)"))
5795
5796 ;; There should be a primary expression after it.
5797 (let (pos)
5798 (forward-char)
5799 (c-forward-syntactic-ws)
5800 (setq cast-end (point))
5801 (and (looking-at c-primary-expr-regexp)
5802 (progn
5803 (setq pos (match-end 0))
5804 (or
5805 ;; Check if the expression begins with a prefix keyword.
5806 (match-beginning 2)
5807 (if (match-beginning 1)
5808 ;; Expression begins with an ambiguous operator. Treat
5809 ;; it as a cast if it's a type decl or if we've
5810 ;; recognized the type somewhere else.
5811 (or at-decl-or-cast
5812 (memq at-type '(t known found)))
5813 ;; Unless it's a keyword, it's the beginning of a primary
5814 ;; expression.
5815 (not (looking-at c-keywords-regexp)))))
5816 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
5817 ;; that it matched a whole one so that we don't e.g. confuse
5818 ;; the operator '-' with '->'. It's ok if it matches further,
5819 ;; though, since it e.g. can match the float '.5' while the
5820 ;; operator regexp only matches '.'.
5821 (or (not (looking-at c-nonsymbol-token-regexp))
5822 (<= (match-end 0) pos))))
5823
5824 ;; There should either be a cast before it or something that isn't an
5825 ;; identifier or close paren.
5826 (> preceding-token-end (point-min))
5827 (progn
5828 (goto-char (1- preceding-token-end))
5829 (or (eq (point) last-cast-end)
5830 (progn
5831 (c-backward-syntactic-ws)
5832 (if (< (skip-syntax-backward "w_") 0)
5833 ;; It's a symbol. Accept it only if it's one of the
5834 ;; keywords that can precede an expression (without
5835 ;; surrounding parens).
5836 (looking-at c-simple-stmt-key)
5837 (and
5838 ;; Check that it isn't a close paren (block close is ok,
5839 ;; though).
5840 (not (memq (char-before) '(?\) ?\])))
5841 ;; Check that it isn't a nonsymbol identifier.
5842 (not (c-on-identifier)))))))))
5843
5844 ;; Handle the cast.
5845 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5846 (let ((c-promote-possible-types t))
5847 (goto-char type-start)
5848 (c-forward-type)))
5849
5850 (goto-char cast-end)
5851 'cast)
5852
5853 (at-decl-or-cast
5854 ;; We're at a declaration. Highlight the type and the following
5855 ;; declarators.
5856
5857 (when backup-if-not-cast
5858 (c-fdoc-shift-type-backward t))
5859
5860 (when (and (eq context 'decl) (looking-at ","))
5861 ;; Make sure to propagate the `c-decl-arg-start' property to
5862 ;; the next argument if it's set in this one, to cope with
5863 ;; interactive refontification.
5864 (c-put-c-type-property (point) 'c-decl-arg-start))
5865
5866 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5867 (let ((c-promote-possible-types t))
5868 (save-excursion
5869 (goto-char type-start)
5870 (c-forward-type))))
5871
5872 (cons id-start at-type-decl))
5873
5874 (t
5875 ;; False alarm. Restore the recorded ranges.
5876 (setq c-record-type-identifiers save-rec-type-ids
5877 c-record-ref-identifiers save-rec-ref-ids)
5878 nil))))
5879
5880 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
5881 ;; Assuming that point is at the beginning of a token, check if it starts a
5882 ;; label and if so move over it and return non-nil (t in default situations,
5883 ;; specific symbols (see below) for interesting situations), otherwise don't
5884 ;; move and return nil. "Label" here means "most things with a colon".
5885 ;;
5886 ;; More precisely, a "label" is regarded as one of:
5887 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
5888 ;; (ii) A case label - either the entire construct "case FOO:", or just the
5889 ;; bare "case", should the colon be missing. We return t;
5890 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
5891 ;; return t;
5892 ;; (iv) One of QT's "extended" C++ variants of
5893 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
5894 ;; Returns the symbol `qt-2kwds-colon'.
5895 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
5896 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
5897 ;; colon). Currently (2006-03), this applies only to Objective C's
5898 ;; keywords "@private", "@protected", and "@public". Returns t.
5899 ;;
5900 ;; One of the things which will NOT be recognised as a label is a bit-field
5901 ;; element of a struct, something like "int foo:5".
5902 ;;
5903 ;; The end of the label is taken to be just after the colon, or the end of
5904 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
5905 ;; after the end on return. The terminating char gets marked with
5906 ;; `c-decl-end' to improve recognition of the following declaration or
5907 ;; statement.
5908 ;;
5909 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
5910 ;; label, if any, has already been marked up like that.
5911 ;;
5912 ;; If PRECEDING-TOKEN-END is given, it should be the first position
5913 ;; after the preceding token, i.e. on the other side of the
5914 ;; syntactic ws from the point. Use a value less than or equal to
5915 ;; (point-min) if the point is at the first token in (the visible
5916 ;; part of) the buffer.
5917 ;;
5918 ;; The optional LIMIT limits the forward scan for the colon.
5919 ;;
5920 ;; This function records the ranges of the label symbols on
5921 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
5922 ;; non-nil.
5923 ;;
5924 ;; This function might do hidden buffer changes.
5925
5926 (let ((start (point))
5927 label-end
5928 qt-symbol-idx
5929 macro-start ; if we're in one.
5930 label-type)
5931 (cond
5932 ;; "case" or "default" (Doesn't apply to AWK).
5933 ((looking-at c-label-kwds-regexp)
5934 (let ((kwd-end (match-end 1)))
5935 ;; Record only the keyword itself for fontification, since in
5936 ;; case labels the following is a constant expression and not
5937 ;; a label.
5938 (when c-record-type-identifiers
5939 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
5940
5941 ;; Find the label end.
5942 (goto-char kwd-end)
5943 (setq label-type
5944 (if (and (c-syntactic-re-search-forward
5945 ;; Stop on chars that aren't allowed in expressions,
5946 ;; and on operator chars that would be meaningless
5947 ;; there. FIXME: This doesn't cope with ?: operators.
5948 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
5949 limit t t nil 1)
5950 (match-beginning 2))
5951
5952 (progn ; there's a proper :
5953 (goto-char (match-beginning 2)) ; just after the :
5954 (c-put-c-type-property (1- (point)) 'c-decl-end)
5955 t)
5956
5957 ;; It's an unfinished label. We consider the keyword enough
5958 ;; to recognize it as a label, so that it gets fontified.
5959 ;; Leave the point at the end of it, but don't put any
5960 ;; `c-decl-end' marker.
5961 (goto-char kwd-end)
5962 t))))
5963
5964 ;; @private, @protected, @public, in Objective C, or similar.
5965 ((and c-opt-extra-label-key
5966 (looking-at c-opt-extra-label-key))
5967 ;; For a `c-opt-extra-label-key' match, we record the whole
5968 ;; thing for fontification. That's to get the leading '@' in
5969 ;; Objective-C protection labels fontified.
5970 (goto-char (match-end 1))
5971 (when c-record-type-identifiers
5972 (c-record-ref-id (cons (match-beginning 1) (point))))
5973 (c-put-c-type-property (1- (point)) 'c-decl-end)
5974 (setq label-type t))
5975
5976 ;; All other cases of labels.
5977 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
5978
5979 ;; A colon label must have something before the colon.
5980 (not (eq (char-after) ?:))
5981
5982 ;; Check that we're not after a token that can't precede a label.
5983 (or
5984 ;; Trivially succeeds when there's no preceding token.
5985 (if preceding-token-end
5986 (<= preceding-token-end (point-min))
5987 (save-excursion
5988 (c-backward-syntactic-ws)
5989 (setq preceding-token-end (point))
5990 (bobp)))
5991
5992 ;; Check if we're after a label, if we're after a closing
5993 ;; paren that belong to statement, and with
5994 ;; `c-label-prefix-re'. It's done in different order
5995 ;; depending on `assume-markup' since the checks have
5996 ;; different expensiveness.
5997 (if assume-markup
5998 (or
5999 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
6000 'c-decl-end)
6001
6002 (save-excursion
6003 (goto-char (1- preceding-token-end))
6004 (c-beginning-of-current-token)
6005 (or (looking-at c-label-prefix-re)
6006 (looking-at c-block-stmt-1-key)))
6007
6008 (and (eq (char-before preceding-token-end) ?\))
6009 (c-after-conditional)))
6010
6011 (or
6012 (save-excursion
6013 (goto-char (1- preceding-token-end))
6014 (c-beginning-of-current-token)
6015 (or (looking-at c-label-prefix-re)
6016 (looking-at c-block-stmt-1-key)))
6017
6018 (cond
6019 ((eq (char-before preceding-token-end) ?\))
6020 (c-after-conditional))
6021
6022 ((eq (char-before preceding-token-end) ?:)
6023 ;; Might be after another label, so check it recursively.
6024 (save-restriction
6025 (save-excursion
6026 (goto-char (1- preceding-token-end))
6027 ;; Essentially the same as the
6028 ;; `c-syntactic-re-search-forward' regexp below.
6029 (setq macro-start
6030 (save-excursion (and (c-beginning-of-macro)
6031 (point))))
6032 (if macro-start (narrow-to-region macro-start (point-max)))
6033 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
6034 ;; Note: the following should work instead of the
6035 ;; narrow-to-region above. Investigate why not,
6036 ;; sometime. ACM, 2006-03-31.
6037 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
6038 ;; macro-start t)
6039 (let ((pte (point))
6040 ;; If the caller turned on recording for us,
6041 ;; it shouldn't apply when we check the
6042 ;; preceding label.
6043 c-record-type-identifiers)
6044 ;; A label can't start at a cpp directive. Check for
6045 ;; this, since c-forward-syntactic-ws would foul up on it.
6046 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
6047 (c-forward-syntactic-ws)
6048 (c-forward-label nil pte start))))))))))
6049
6050 ;; Point is still at the beginning of the possible label construct.
6051 ;;
6052 ;; Check that the next nonsymbol token is ":", or that we're in one
6053 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
6054 ;; arguments. FIXME: Should build this regexp from the language
6055 ;; constants.
6056 (cond
6057 ;; public: protected: private:
6058 ((and
6059 (c-major-mode-is 'c++-mode)
6060 (search-forward-regexp
6061 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
6062 (progn (backward-char)
6063 (c-forward-syntactic-ws limit)
6064 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
6065 (forward-char)
6066 (setq label-type t))
6067 ;; QT double keyword like "protected slots:" or goto target.
6068 ((progn (goto-char start) nil))
6069 ((when (c-syntactic-re-search-forward
6070 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
6071 (backward-char)
6072 (setq label-end (point))
6073 (setq qt-symbol-idx
6074 (and (c-major-mode-is 'c++-mode)
6075 (string-match
6076 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
6077 (buffer-substring start (point)))))
6078 (c-forward-syntactic-ws limit)
6079 (cond
6080 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
6081 (forward-char)
6082 (setq label-type
6083 (if (string= "signals" ; Special QT macro
6084 (buffer-substring-no-properties start label-end))
6085 'qt-1kwd-colon
6086 'goto-target)))
6087 ((and qt-symbol-idx
6088 (search-forward-regexp "\\=slots\\>" limit t)
6089 (progn (c-forward-syntactic-ws limit)
6090 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
6091 (forward-char)
6092 (setq label-type 'qt-2kwds-colon)))))))
6093
6094 (save-restriction
6095 (narrow-to-region start (point))
6096
6097 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
6098 (catch 'check-label
6099 (goto-char start)
6100 (while (progn
6101 (when (looking-at c-nonlabel-token-key)
6102 (goto-char start)
6103 (setq label-type nil)
6104 (throw 'check-label nil))
6105 (and (c-safe (c-forward-sexp)
6106 (c-forward-syntactic-ws)
6107 t)
6108 (not (eobp)))))
6109
6110 ;; Record the identifiers in the label for fontification, unless
6111 ;; it begins with `c-label-kwds' in which case the following
6112 ;; identifiers are part of a (constant) expression that
6113 ;; shouldn't be fontified.
6114 (when (and c-record-type-identifiers
6115 (progn (goto-char start)
6116 (not (looking-at c-label-kwds-regexp))))
6117 (while (c-syntactic-re-search-forward c-symbol-key nil t)
6118 (c-record-ref-id (cons (match-beginning 0)
6119 (match-end 0)))))
6120
6121 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
6122 (goto-char (point-max)))))
6123
6124 (t
6125 ;; Not a label.
6126 (goto-char start)))
6127 label-type))
6128
6129 (defun c-forward-objc-directive ()
6130 ;; Assuming the point is at the beginning of a token, try to move
6131 ;; forward to the end of the Objective-C directive that starts
6132 ;; there. Return t if a directive was fully recognized, otherwise
6133 ;; the point is moved as far as one could be successfully parsed and
6134 ;; nil is returned.
6135 ;;
6136 ;; This function records identifier ranges on
6137 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6138 ;; `c-record-type-identifiers' is non-nil.
6139 ;;
6140 ;; This function might do hidden buffer changes.
6141
6142 (let ((start (point))
6143 start-char
6144 (c-promote-possible-types t)
6145 ;; Turn off recognition of angle bracket arglists while parsing
6146 ;; types here since the protocol reference list might then be
6147 ;; considered part of the preceding name or superclass-name.
6148 c-recognize-<>-arglists)
6149
6150 (if (or
6151 (when (looking-at
6152 (eval-when-compile
6153 (c-make-keywords-re t
6154 (append (c-lang-const c-protection-kwds objc)
6155 '("@end"))
6156 'objc-mode)))
6157 (goto-char (match-end 1))
6158 t)
6159
6160 (and
6161 (looking-at
6162 (eval-when-compile
6163 (c-make-keywords-re t
6164 '("@interface" "@implementation" "@protocol")
6165 'objc-mode)))
6166
6167 ;; Handle the name of the class itself.
6168 (progn
6169 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
6170 ; at EOB.
6171 (goto-char (match-end 0))
6172 (c-skip-ws-forward)
6173 (c-forward-type))
6174
6175 (catch 'break
6176 ;; Look for ": superclass-name" or "( category-name )".
6177 (when (looking-at "[:\(]")
6178 (setq start-char (char-after))
6179 (forward-char)
6180 (c-forward-syntactic-ws)
6181 (unless (c-forward-type) (throw 'break nil))
6182 (when (eq start-char ?\()
6183 (unless (eq (char-after) ?\)) (throw 'break nil))
6184 (forward-char)
6185 (c-forward-syntactic-ws)))
6186
6187 ;; Look for a protocol reference list.
6188 (if (eq (char-after) ?<)
6189 (let ((c-recognize-<>-arglists t)
6190 (c-parse-and-markup-<>-arglists t)
6191 c-restricted-<>-arglists)
6192 (c-forward-<>-arglist t))
6193 t))))
6194
6195 (progn
6196 (c-backward-syntactic-ws)
6197 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
6198 (c-put-c-type-property (1- (point)) 'c-decl-end)
6199 t)
6200
6201 (c-clear-c-type-property start (point) 'c-decl-end)
6202 nil)))
6203
6204 (defun c-beginning-of-inheritance-list (&optional lim)
6205 ;; Go to the first non-whitespace after the colon that starts a
6206 ;; multiple inheritance introduction. Optional LIM is the farthest
6207 ;; back we should search.
6208 ;;
6209 ;; This function might do hidden buffer changes.
6210 (c-with-syntax-table c++-template-syntax-table
6211 (c-backward-token-2 0 t lim)
6212 (while (and (or (looking-at c-symbol-start)
6213 (looking-at "[<,]\\|::"))
6214 (zerop (c-backward-token-2 1 t lim))))))
6215
6216 (defun c-in-method-def-p ()
6217 ;; Return nil if we aren't in a method definition, otherwise the
6218 ;; position of the initial [+-].
6219 ;;
6220 ;; This function might do hidden buffer changes.
6221 (save-excursion
6222 (beginning-of-line)
6223 (and c-opt-method-key
6224 (looking-at c-opt-method-key)
6225 (point))
6226 ))
6227
6228 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
6229 (defun c-in-gcc-asm-p ()
6230 ;; Return non-nil if point is within a gcc \"asm\" block.
6231 ;;
6232 ;; This should be called with point inside an argument list.
6233 ;;
6234 ;; Only one level of enclosing parentheses is considered, so for
6235 ;; instance `nil' is returned when in a function call within an asm
6236 ;; operand.
6237 ;;
6238 ;; This function might do hidden buffer changes.
6239
6240 (and c-opt-asm-stmt-key
6241 (save-excursion
6242 (beginning-of-line)
6243 (backward-up-list 1)
6244 (c-beginning-of-statement-1 (point-min) nil t)
6245 (looking-at c-opt-asm-stmt-key))))
6246
6247 (defun c-at-toplevel-p ()
6248 "Return a determination as to whether point is at the `top-level'.
6249 Being at the top-level means that point is either outside any
6250 enclosing block (such function definition), or only inside a class,
6251 namespace or other block that contains another declaration level.
6252
6253 If point is not at the top-level (e.g. it is inside a method
6254 definition), then nil is returned. Otherwise, if point is at a
6255 top-level not enclosed within a class definition, t is returned.
6256 Otherwise, a 2-vector is returned where the zeroth element is the
6257 buffer position of the start of the class declaration, and the first
6258 element is the buffer position of the enclosing class's opening
6259 brace.
6260
6261 Note that this function might do hidden buffer changes. See the
6262 comment at the start of cc-engine.el for more info."
6263 (let ((paren-state (c-parse-state)))
6264 (or (not (c-most-enclosing-brace paren-state))
6265 (c-search-uplist-for-classkey paren-state))))
6266
6267 (defun c-just-after-func-arglist-p (&optional lim)
6268 ;; Return non-nil if the point is in the region after the argument
6269 ;; list of a function and its opening brace (or semicolon in case it
6270 ;; got no body). If there are K&R style argument declarations in
6271 ;; that region, the point has to be inside the first one for this
6272 ;; function to recognize it.
6273 ;;
6274 ;; If successful, the point is moved to the first token after the
6275 ;; function header (see `c-forward-decl-or-cast-1' for details) and
6276 ;; the position of the opening paren of the function arglist is
6277 ;; returned.
6278 ;;
6279 ;; The point is clobbered if not successful.
6280 ;;
6281 ;; LIM is used as bound for backward buffer searches.
6282 ;;
6283 ;; This function might do hidden buffer changes.
6284
6285 (let ((beg (point)) end id-start)
6286 (and
6287 (eq (c-beginning-of-statement-1 lim) 'same)
6288
6289 (not (or (c-major-mode-is 'objc-mode)
6290 (c-forward-objc-directive)))
6291
6292 (setq id-start
6293 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
6294 (< id-start beg)
6295
6296 ;; There should not be a '=' or ',' between beg and the
6297 ;; start of the declaration since that means we were in the
6298 ;; "expression part" of the declaration.
6299 (or (> (point) beg)
6300 (not (looking-at "[=,]")))
6301
6302 (save-excursion
6303 ;; Check that there's an arglist paren in the
6304 ;; declaration.
6305 (goto-char id-start)
6306 (cond ((eq (char-after) ?\()
6307 ;; The declarator is a paren expression, so skip past it
6308 ;; so that we don't get stuck on that instead of the
6309 ;; function arglist.
6310 (c-forward-sexp))
6311 ((and c-opt-op-identifier-prefix
6312 (looking-at c-opt-op-identifier-prefix))
6313 ;; Don't trip up on "operator ()".
6314 (c-forward-token-2 2 t)))
6315 (and (< (point) beg)
6316 (c-syntactic-re-search-forward "(" beg t t)
6317 (1- (point)))))))
6318
6319 (defun c-in-knr-argdecl (&optional lim)
6320 ;; Return the position of the first argument declaration if point is
6321 ;; inside a K&R style argument declaration list, nil otherwise.
6322 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
6323 ;; position that bounds the backward search for the argument list.
6324 ;;
6325 ;; Point must be within a possible K&R region, e.g. just before a top-level
6326 ;; "{". It must be outside of parens and brackets. The test can return
6327 ;; false positives otherwise.
6328 ;;
6329 ;; This function might do hidden buffer changes.
6330
6331 (save-excursion
6332 (save-restriction
6333 ;; If we're in a macro, our search range is restricted to it. Narrow to
6334 ;; the searchable range.
6335 (let* ((macro-start (c-query-macro-start))
6336 (lim (max (or lim (point-min)) (or macro-start (point-min))))
6337 before-lparen after-rparen
6338 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
6339 (narrow-to-region lim (c-point 'eol))
6340
6341 ;; Search backwards for the defun's argument list. We give up if we
6342 ;; encounter a "}" (end of a previous defun) or BOB.
6343 ;;
6344 ;; The criterion for a paren structure being the arg list is:
6345 ;; o - there is non-WS stuff after it but before any "{"; AND
6346 ;; o - the token after it isn't a ";" AND
6347 ;; o - it is preceded by either an identifier (the function name) or
6348 ;; a macro expansion like "DEFUN (...)"; AND
6349 ;; o - its content is a non-empty comma-separated list of identifiers
6350 ;; (an empty arg list won't have a knr region).
6351 ;;
6352 ;; The following snippet illustrates these rules:
6353 ;; int foo (bar, baz, yuk)
6354 ;; int bar [] ;
6355 ;; int (*baz) (my_type) ;
6356 ;; int (*) (void) (*yuk) (void) ;
6357 ;; {
6358
6359 (catch 'knr
6360 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
6361 (setq pp-count-out (1- pp-count-out))
6362 (c-syntactic-skip-backward "^)]}")
6363 (cond ((eq (char-before) ?\))
6364 (setq after-rparen (point)))
6365 ((eq (char-before) ?\])
6366 (setq after-rparen nil))
6367 (t ; either } (hit previous defun) or no more parens/brackets
6368 (throw 'knr nil)))
6369
6370 (if after-rparen
6371 ;; We're inside a paren. Could it be our argument list....?
6372 (if
6373 (and
6374 (progn
6375 (goto-char after-rparen)
6376 (unless (c-go-list-backward) (throw 'knr nil)) ;
6377 ;; FIXME!!! What about macros between the parens? 2007/01/20
6378 (setq before-lparen (point)))
6379
6380 ;; It can't be the arg list if next token is ; or {
6381 (progn (goto-char after-rparen)
6382 (c-forward-syntactic-ws)
6383 (not (memq (char-after) '(?\; ?\{))))
6384
6385 ;; Is the thing preceding the list an identifier (the
6386 ;; function name), or a macro expansion?
6387 (progn
6388 (goto-char before-lparen)
6389 (eq (c-backward-token-2) 0)
6390 (or (c-on-identifier)
6391 (and (eq (char-after) ?\))
6392 (c-go-up-list-backward)
6393 (eq (c-backward-token-2) 0)
6394 (c-on-identifier))))
6395
6396 ;; Have we got a non-empty list of comma-separated
6397 ;; identifiers?
6398 (progn
6399 (goto-char before-lparen)
6400 (c-forward-token-2) ; to first token inside parens
6401 (and
6402 (c-on-identifier)
6403 (c-forward-token-2)
6404 (catch 'id-list
6405 (while (eq (char-after) ?\,)
6406 (c-forward-token-2)
6407 (unless (c-on-identifier) (throw 'id-list nil))
6408 (c-forward-token-2))
6409 (eq (char-after) ?\))))))
6410
6411 ;; ...Yes. We've identified the function's argument list.
6412 (throw 'knr
6413 (progn (goto-char after-rparen)
6414 (c-forward-syntactic-ws)
6415 (point)))
6416
6417 ;; ...No. The current parens aren't the function's arg list.
6418 (goto-char before-lparen))
6419
6420 (or (c-go-list-backward) ; backwards over [ .... ]
6421 (throw 'knr nil)))))))))
6422
6423 (defun c-skip-conditional ()
6424 ;; skip forward over conditional at point, including any predicate
6425 ;; statements in parentheses. No error checking is performed.
6426 ;;
6427 ;; This function might do hidden buffer changes.
6428 (c-forward-sexp (cond
6429 ;; else if()
6430 ((looking-at (concat "\\<else"
6431 "\\([ \t\n]\\|\\\\\n\\)+"
6432 "if\\>\\([^_]\\|$\\)"))
6433 3)
6434 ;; do, else, try, finally
6435 ((looking-at (concat "\\<\\("
6436 "do\\|else\\|try\\|finally"
6437 "\\)\\>\\([^_]\\|$\\)"))
6438 1)
6439 ;; for, if, while, switch, catch, synchronized, foreach
6440 (t 2))))
6441
6442 (defun c-after-conditional (&optional lim)
6443 ;; If looking at the token after a conditional then return the
6444 ;; position of its start, otherwise return nil.
6445 ;;
6446 ;; This function might do hidden buffer changes.
6447 (save-excursion
6448 (and (zerop (c-backward-token-2 1 t lim))
6449 (or (looking-at c-block-stmt-1-key)
6450 (and (eq (char-after) ?\()
6451 (zerop (c-backward-token-2 1 t lim))
6452 (looking-at c-block-stmt-2-key)))
6453 (point))))
6454
6455 (defun c-after-special-operator-id (&optional lim)
6456 ;; If the point is after an operator identifier that isn't handled
6457 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
6458 ;; position of the start of that identifier is returned. nil is
6459 ;; returned otherwise. The point may be anywhere in the syntactic
6460 ;; whitespace after the last token of the operator identifier.
6461 ;;
6462 ;; This function might do hidden buffer changes.
6463 (save-excursion
6464 (and c-overloadable-operators-regexp
6465 (zerop (c-backward-token-2 1 nil lim))
6466 (looking-at c-overloadable-operators-regexp)
6467 (or (not c-opt-op-identifier-prefix)
6468 (and
6469 (zerop (c-backward-token-2 1 nil lim))
6470 (looking-at c-opt-op-identifier-prefix)))
6471 (point))))
6472
6473 (defsubst c-backward-to-block-anchor (&optional lim)
6474 ;; Assuming point is at a brace that opens a statement block of some
6475 ;; kind, move to the proper anchor point for that block. It might
6476 ;; need to be adjusted further by c-add-stmt-syntax, but the
6477 ;; position at return is suitable as start position for that
6478 ;; function.
6479 ;;
6480 ;; This function might do hidden buffer changes.
6481 (unless (= (point) (c-point 'boi))
6482 (let ((start (c-after-conditional lim)))
6483 (if start
6484 (goto-char start)))))
6485
6486 (defsubst c-backward-to-decl-anchor (&optional lim)
6487 ;; Assuming point is at a brace that opens the block of a top level
6488 ;; declaration of some kind, move to the proper anchor point for
6489 ;; that block.
6490 ;;
6491 ;; This function might do hidden buffer changes.
6492 (unless (= (point) (c-point 'boi))
6493 (c-beginning-of-statement-1 lim)))
6494
6495 (defun c-search-decl-header-end ()
6496 ;; Search forward for the end of the "header" of the current
6497 ;; declaration. That's the position where the definition body
6498 ;; starts, or the first variable initializer, or the ending
6499 ;; semicolon. I.e. search forward for the closest following
6500 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
6501 ;; _after_ the first found token, or at point-max if none is found.
6502 ;;
6503 ;; This function might do hidden buffer changes.
6504
6505 (let ((base (point)))
6506 (if (c-major-mode-is 'c++-mode)
6507
6508 ;; In C++ we need to take special care to handle operator
6509 ;; tokens and those pesky template brackets.
6510 (while (and
6511 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
6512 (or
6513 (c-end-of-current-token base)
6514 ;; Handle operator identifiers, i.e. ignore any
6515 ;; operator token preceded by "operator".
6516 (save-excursion
6517 (and (c-safe (c-backward-sexp) t)
6518 (looking-at c-opt-op-identifier-prefix)))
6519 (and (eq (char-before) ?<)
6520 (c-with-syntax-table c++-template-syntax-table
6521 (if (c-safe (goto-char (c-up-list-forward (point))))
6522 t
6523 (goto-char (point-max))
6524 nil)))))
6525 (setq base (point)))
6526
6527 (while (and
6528 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
6529 (c-end-of-current-token base))
6530 (setq base (point))))))
6531
6532 (defun c-beginning-of-decl-1 (&optional lim)
6533 ;; Go to the beginning of the current declaration, or the beginning
6534 ;; of the previous one if already at the start of it. Point won't
6535 ;; be moved out of any surrounding paren. Return a cons cell of the
6536 ;; form (MOVE . KNR-POS). MOVE is like the return value from
6537 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
6538 ;; style argument declarations (and they are to be recognized) then
6539 ;; KNR-POS is set to the start of the first such argument
6540 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
6541 ;; position that bounds the backward search.
6542 ;;
6543 ;; NB: Cases where the declaration continues after the block, as in
6544 ;; "struct foo { ... } bar;", are currently recognized as two
6545 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
6546 ;;
6547 ;; This function might do hidden buffer changes.
6548 (catch 'return
6549 (let* ((start (point))
6550 (last-stmt-start (point))
6551 (move (c-beginning-of-statement-1 lim nil t)))
6552
6553 ;; `c-beginning-of-statement-1' stops at a block start, but we
6554 ;; want to continue if the block doesn't begin a top level
6555 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
6556 ;; or an open paren.
6557 (let ((beg (point)) tentative-move)
6558 ;; Go back one "statement" each time round the loop until we're just
6559 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
6560 ;; an ObjC method. This will move over a multiple declaration whose
6561 ;; components are comma separated.
6562 (while (and
6563 ;; Must check with c-opt-method-key in ObjC mode.
6564 (not (and c-opt-method-key
6565 (looking-at c-opt-method-key)))
6566 (/= last-stmt-start (point))
6567 (progn
6568 (c-backward-syntactic-ws lim)
6569 (not (memq (char-before) '(?\; ?} ?: nil))))
6570 (save-excursion
6571 (backward-char)
6572 (not (looking-at "\\s(")))
6573 ;; Check that we don't move from the first thing in a
6574 ;; macro to its header.
6575 (not (eq (setq tentative-move
6576 (c-beginning-of-statement-1 lim nil t))
6577 'macro)))
6578 (setq last-stmt-start beg
6579 beg (point)
6580 move tentative-move))
6581 (goto-char beg))
6582
6583 (when c-recognize-knr-p
6584 (let ((fallback-pos (point)) knr-argdecl-start)
6585 ;; Handle K&R argdecls. Back up after the "statement" jumped
6586 ;; over by `c-beginning-of-statement-1', unless it was the
6587 ;; function body, in which case we're sitting on the opening
6588 ;; brace now. Then test if we're in a K&R argdecl region and
6589 ;; that we started at the other side of the first argdecl in
6590 ;; it.
6591 (unless (eq (char-after) ?{)
6592 (goto-char last-stmt-start))
6593 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
6594 (< knr-argdecl-start start)
6595 (progn
6596 (goto-char knr-argdecl-start)
6597 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
6598 (throw 'return
6599 (cons (if (eq (char-after fallback-pos) ?{)
6600 'previous
6601 'same)
6602 knr-argdecl-start))
6603 (goto-char fallback-pos))))
6604
6605 ;; `c-beginning-of-statement-1' counts each brace block as a separate
6606 ;; statement, so the result will be 'previous if we've moved over any.
6607 ;; So change our result back to 'same if necessary.
6608 ;;
6609 ;; If they were brace list initializers we might not have moved over a
6610 ;; declaration boundary though, so change it to 'same if we've moved
6611 ;; past a '=' before '{', but not ';'. (This ought to be integrated
6612 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
6613 ;; potentially can search over a large amount of text.). Take special
6614 ;; pains not to get mislead by C++'s "operator=", and the like.
6615 (if (and (eq move 'previous)
6616 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
6617 c++-template-syntax-table
6618 (syntax-table))
6619 (save-excursion
6620 (and
6621 (progn
6622 (while ; keep going back to "[;={"s until we either find
6623 ; no more, or get to one which isn't an "operator ="
6624 (and (c-syntactic-re-search-forward "[;={]" start t t t)
6625 (eq (char-before) ?=)
6626 c-overloadable-operators-regexp
6627 c-opt-op-identifier-prefix
6628 (save-excursion
6629 (eq (c-backward-token-2) 0)
6630 (looking-at c-overloadable-operators-regexp)
6631 (eq (c-backward-token-2) 0)
6632 (looking-at c-opt-op-identifier-prefix))))
6633 (eq (char-before) ?=))
6634 (c-syntactic-re-search-forward "[;{]" start t t)
6635 (eq (char-before) ?{)
6636 (c-safe (goto-char (c-up-list-forward (point))) t)
6637 (not (c-syntactic-re-search-forward ";" start t t))))))
6638 (cons 'same nil)
6639 (cons move nil)))))
6640
6641 (defun c-end-of-decl-1 ()
6642 ;; Assuming point is at the start of a declaration (as detected by
6643 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
6644 ;; `c-beginning-of-decl-1', this function handles the case when a
6645 ;; block is followed by identifiers in e.g. struct declarations in C
6646 ;; or C++. If a proper end was found then t is returned, otherwise
6647 ;; point is moved as far as possible within the current sexp and nil
6648 ;; is returned. This function doesn't handle macros; use
6649 ;; `c-end-of-macro' instead in those cases.
6650 ;;
6651 ;; This function might do hidden buffer changes.
6652 (let ((start (point))
6653 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
6654 c++-template-syntax-table
6655 (syntax-table))))
6656 (catch 'return
6657 (c-search-decl-header-end)
6658
6659 (when (and c-recognize-knr-p
6660 (eq (char-before) ?\;)
6661 (c-in-knr-argdecl start))
6662 ;; Stopped at the ';' in a K&R argdecl section which is
6663 ;; detected using the same criteria as in
6664 ;; `c-beginning-of-decl-1'. Move to the following block
6665 ;; start.
6666 (c-syntactic-re-search-forward "{" nil 'move t))
6667
6668 (when (eq (char-before) ?{)
6669 ;; Encountered a block in the declaration. Jump over it.
6670 (condition-case nil
6671 (goto-char (c-up-list-forward (point)))
6672 (error (goto-char (point-max))
6673 (throw 'return nil)))
6674 (if (or (not c-opt-block-decls-with-vars-key)
6675 (save-excursion
6676 (c-with-syntax-table decl-syntax-table
6677 (let ((lim (point)))
6678 (goto-char start)
6679 (not (and
6680 ;; Check for `c-opt-block-decls-with-vars-key'
6681 ;; before the first paren.
6682 (c-syntactic-re-search-forward
6683 (concat "[;=\(\[{]\\|\\("
6684 c-opt-block-decls-with-vars-key
6685 "\\)")
6686 lim t t t)
6687 (match-beginning 1)
6688 (not (eq (char-before) ?_))
6689 ;; Check that the first following paren is
6690 ;; the block.
6691 (c-syntactic-re-search-forward "[;=\(\[{]"
6692 lim t t t)
6693 (eq (char-before) ?{)))))))
6694 ;; The declaration doesn't have any of the
6695 ;; `c-opt-block-decls-with-vars' keywords in the
6696 ;; beginning, so it ends here at the end of the block.
6697 (throw 'return t)))
6698
6699 (c-with-syntax-table decl-syntax-table
6700 (while (progn
6701 (if (eq (char-before) ?\;)
6702 (throw 'return t))
6703 (c-syntactic-re-search-forward ";" nil 'move t))))
6704 nil)))
6705
6706 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
6707 ;; Assuming the point is at an open brace, check if it starts a
6708 ;; block that contains another declaration level, i.e. that isn't a
6709 ;; statement block or a brace list, and if so return non-nil.
6710 ;;
6711 ;; If the check is successful, the return value is the start of the
6712 ;; keyword that tells what kind of construct it is, i.e. typically
6713 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
6714 ;; the point will be at the start of the construct, before any
6715 ;; leading specifiers, otherwise it's at the returned position.
6716 ;;
6717 ;; The point is clobbered if the check is unsuccessful.
6718 ;;
6719 ;; CONTAINING-SEXP is the position of the open of the surrounding
6720 ;; paren, or nil if none.
6721 ;;
6722 ;; The optional LIMIT limits the backward search for the start of
6723 ;; the construct. It's assumed to be at a syntactically relevant
6724 ;; position.
6725 ;;
6726 ;; If any template arglists are found in the searched region before
6727 ;; the open brace, they get marked with paren syntax.
6728 ;;
6729 ;; This function might do hidden buffer changes.
6730
6731 (let ((open-brace (point)) kwd-start first-specifier-pos)
6732 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6733
6734 (when (and c-recognize-<>-arglists
6735 (eq (char-before) ?>))
6736 ;; Could be at the end of a template arglist.
6737 (let ((c-parse-and-markup-<>-arglists t)
6738 (c-disallow-comma-in-<>-arglists
6739 (and containing-sexp
6740 (not (eq (char-after containing-sexp) ?{)))))
6741 (while (and
6742 (c-backward-<>-arglist nil limit)
6743 (progn
6744 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6745 (eq (char-before) ?>))))))
6746
6747 ;; Note: Can't get bogus hits inside template arglists below since they
6748 ;; have gotten paren syntax above.
6749 (when (and
6750 ;; If `goto-start' is set we begin by searching for the
6751 ;; first possible position of a leading specifier list.
6752 ;; The `c-decl-block-key' search continues from there since
6753 ;; we know it can't match earlier.
6754 (if goto-start
6755 (when (c-syntactic-re-search-forward c-symbol-start
6756 open-brace t t)
6757 (goto-char (setq first-specifier-pos (match-beginning 0)))
6758 t)
6759 t)
6760
6761 (cond
6762 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
6763 (goto-char (setq kwd-start (match-beginning 0)))
6764 (or
6765
6766 ;; Found a keyword that can't be a type?
6767 (match-beginning 1)
6768
6769 ;; Can be a type too, in which case it's the return type of a
6770 ;; function (under the assumption that no declaration level
6771 ;; block construct starts with a type).
6772 (not (c-forward-type))
6773
6774 ;; Jumped over a type, but it could be a declaration keyword
6775 ;; followed by the declared identifier that we've jumped over
6776 ;; instead (e.g. in "class Foo {"). If it indeed is a type
6777 ;; then we should be at the declarator now, so check for a
6778 ;; valid declarator start.
6779 ;;
6780 ;; Note: This doesn't cope with the case when a declared
6781 ;; identifier is followed by e.g. '(' in a language where '('
6782 ;; also might be part of a declarator expression. Currently
6783 ;; there's no such language.
6784 (not (or (looking-at c-symbol-start)
6785 (looking-at c-type-decl-prefix-key)))))
6786
6787 ;; In Pike a list of modifiers may be followed by a brace
6788 ;; to make them apply to many identifiers. Note that the
6789 ;; match data will be empty on return in this case.
6790 ((and (c-major-mode-is 'pike-mode)
6791 (progn
6792 (goto-char open-brace)
6793 (= (c-backward-token-2) 0))
6794 (looking-at c-specifier-key)
6795 ;; Use this variant to avoid yet another special regexp.
6796 (c-keyword-member (c-keyword-sym (match-string 1))
6797 'c-modifier-kwds))
6798 (setq kwd-start (point))
6799 t)))
6800
6801 ;; Got a match.
6802
6803 (if goto-start
6804 ;; Back up over any preceding specifiers and their clauses
6805 ;; by going forward from `first-specifier-pos', which is the
6806 ;; earliest possible position where the specifier list can
6807 ;; start.
6808 (progn
6809 (goto-char first-specifier-pos)
6810
6811 (while (< (point) kwd-start)
6812 (if (looking-at c-symbol-key)
6813 ;; Accept any plain symbol token on the ground that
6814 ;; it's a specifier masked through a macro (just
6815 ;; like `c-forward-decl-or-cast-1' skip forward over
6816 ;; such tokens).
6817 ;;
6818 ;; Could be more restrictive wrt invalid keywords,
6819 ;; but that'd only occur in invalid code so there's
6820 ;; no use spending effort on it.
6821 (let ((end (match-end 0)))
6822 (unless (c-forward-keyword-clause 0)
6823 (goto-char end)
6824 (c-forward-syntactic-ws)))
6825
6826 ;; Can't parse a declaration preamble and is still
6827 ;; before `kwd-start'. That means `first-specifier-pos'
6828 ;; was in some earlier construct. Search again.
6829 (if (c-syntactic-re-search-forward c-symbol-start
6830 kwd-start 'move t)
6831 (goto-char (setq first-specifier-pos (match-beginning 0)))
6832 ;; Got no preamble before the block declaration keyword.
6833 (setq first-specifier-pos kwd-start))))
6834
6835 (goto-char first-specifier-pos))
6836 (goto-char kwd-start))
6837
6838 kwd-start)))
6839
6840 (defun c-search-uplist-for-classkey (paren-state)
6841 ;; Check if the closest containing paren sexp is a declaration
6842 ;; block, returning a 2 element vector in that case. Aref 0
6843 ;; contains the bufpos at boi of the class key line, and aref 1
6844 ;; contains the bufpos of the open brace. This function is an
6845 ;; obsolete wrapper for `c-looking-at-decl-block'.
6846 ;;
6847 ;; This function might do hidden buffer changes.
6848 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
6849 (when open-paren-pos
6850 (save-excursion
6851 (goto-char open-paren-pos)
6852 (when (and (eq (char-after) ?{)
6853 (c-looking-at-decl-block
6854 (c-safe-position open-paren-pos paren-state)
6855 nil))
6856 (back-to-indentation)
6857 (vector (point) open-paren-pos))))))
6858
6859 (defun c-inside-bracelist-p (containing-sexp paren-state)
6860 ;; return the buffer position of the beginning of the brace list
6861 ;; statement if we're inside a brace list, otherwise return nil.
6862 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
6863 ;; paren. PAREN-STATE is the remainder of the state of enclosing
6864 ;; braces
6865 ;;
6866 ;; N.B.: This algorithm can potentially get confused by cpp macros
6867 ;; placed in inconvenient locations. It's a trade-off we make for
6868 ;; speed.
6869 ;;
6870 ;; This function might do hidden buffer changes.
6871 (or
6872 ;; This will pick up brace list declarations.
6873 (c-safe
6874 (save-excursion
6875 (goto-char containing-sexp)
6876 (c-forward-sexp -1)
6877 (let (bracepos)
6878 (if (and (or (looking-at c-brace-list-key)
6879 (progn (c-forward-sexp -1)
6880 (looking-at c-brace-list-key)))
6881 (setq bracepos (c-down-list-forward (point)))
6882 (not (c-crosses-statement-barrier-p (point)
6883 (- bracepos 2))))
6884 (point)))))
6885 ;; this will pick up array/aggregate init lists, even if they are nested.
6886 (save-excursion
6887 (let ((class-key
6888 ;; Pike can have class definitions anywhere, so we must
6889 ;; check for the class key here.
6890 (and (c-major-mode-is 'pike-mode)
6891 c-decl-block-key))
6892 bufpos braceassignp lim next-containing)
6893 (while (and (not bufpos)
6894 containing-sexp)
6895 (when paren-state
6896 (if (consp (car paren-state))
6897 (setq lim (cdr (car paren-state))
6898 paren-state (cdr paren-state))
6899 (setq lim (car paren-state)))
6900 (when paren-state
6901 (setq next-containing (car paren-state)
6902 paren-state (cdr paren-state))))
6903 (goto-char containing-sexp)
6904 (if (c-looking-at-inexpr-block next-containing next-containing)
6905 ;; We're in an in-expression block of some kind. Do not
6906 ;; check nesting. We deliberately set the limit to the
6907 ;; containing sexp, so that c-looking-at-inexpr-block
6908 ;; doesn't check for an identifier before it.
6909 (setq containing-sexp nil)
6910 ;; see if the open brace is preceded by = or [...] in
6911 ;; this statement, but watch out for operator=
6912 (setq braceassignp 'dontknow)
6913 (c-backward-token-2 1 t lim)
6914 ;; Checks to do only on the first sexp before the brace.
6915 (when (and c-opt-inexpr-brace-list-key
6916 (eq (char-after) ?\[))
6917 ;; In Java, an initialization brace list may follow
6918 ;; directly after "new Foo[]", so check for a "new"
6919 ;; earlier.
6920 (while (eq braceassignp 'dontknow)
6921 (setq braceassignp
6922 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
6923 ((looking-at c-opt-inexpr-brace-list-key) t)
6924 ((looking-at "\\sw\\|\\s_\\|[.[]")
6925 ;; Carry on looking if this is an
6926 ;; identifier (may contain "." in Java)
6927 ;; or another "[]" sexp.
6928 'dontknow)
6929 (t nil)))))
6930 ;; Checks to do on all sexps before the brace, up to the
6931 ;; beginning of the statement.
6932 (while (eq braceassignp 'dontknow)
6933 (cond ((eq (char-after) ?\;)
6934 (setq braceassignp nil))
6935 ((and class-key
6936 (looking-at class-key))
6937 (setq braceassignp nil))
6938 ((eq (char-after) ?=)
6939 ;; We've seen a =, but must check earlier tokens so
6940 ;; that it isn't something that should be ignored.
6941 (setq braceassignp 'maybe)
6942 (while (and (eq braceassignp 'maybe)
6943 (zerop (c-backward-token-2 1 t lim)))
6944 (setq braceassignp
6945 (cond
6946 ;; Check for operator =
6947 ((and c-opt-op-identifier-prefix
6948 (looking-at c-opt-op-identifier-prefix))
6949 nil)
6950 ;; Check for `<opchar>= in Pike.
6951 ((and (c-major-mode-is 'pike-mode)
6952 (or (eq (char-after) ?`)
6953 ;; Special case for Pikes
6954 ;; `[]=, since '[' is not in
6955 ;; the punctuation class.
6956 (and (eq (char-after) ?\[)
6957 (eq (char-before) ?`))))
6958 nil)
6959 ((looking-at "\\s.") 'maybe)
6960 ;; make sure we're not in a C++ template
6961 ;; argument assignment
6962 ((and
6963 (c-major-mode-is 'c++-mode)
6964 (save-excursion
6965 (let ((here (point))
6966 (pos< (progn
6967 (skip-chars-backward "^<>")
6968 (point))))
6969 (and (eq (char-before) ?<)
6970 (not (c-crosses-statement-barrier-p
6971 pos< here))
6972 (not (c-in-literal))
6973 ))))
6974 nil)
6975 (t t))))))
6976 (if (and (eq braceassignp 'dontknow)
6977 (/= (c-backward-token-2 1 t lim) 0))
6978 (setq braceassignp nil)))
6979 (if (not braceassignp)
6980 (if (eq (char-after) ?\;)
6981 ;; Brace lists can't contain a semicolon, so we're done.
6982 (setq containing-sexp nil)
6983 ;; Go up one level.
6984 (setq containing-sexp next-containing
6985 lim nil
6986 next-containing nil))
6987 ;; we've hit the beginning of the aggregate list
6988 (c-beginning-of-statement-1
6989 (c-most-enclosing-brace paren-state))
6990 (setq bufpos (point))))
6991 )
6992 bufpos))
6993 ))
6994
6995 (defun c-looking-at-special-brace-list (&optional lim)
6996 ;; If we're looking at the start of a pike-style list, ie `({ })',
6997 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
6998 ;; positions and its entry in c-special-brace-lists is returned, nil
6999 ;; otherwise. The ending position is nil if the list is still open.
7000 ;; LIM is the limit for forward search. The point may either be at
7001 ;; the `(' or at the following paren character. Tries to check the
7002 ;; matching closer, but assumes it's correct if no balanced paren is
7003 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
7004 ;; a special brace list).
7005 ;;
7006 ;; This function might do hidden buffer changes.
7007 (if c-special-brace-lists
7008 (condition-case ()
7009 (save-excursion
7010 (let ((beg (point))
7011 inner-beg end type)
7012 (c-forward-syntactic-ws)
7013 (if (eq (char-after) ?\()
7014 (progn
7015 (forward-char 1)
7016 (c-forward-syntactic-ws)
7017 (setq inner-beg (point))
7018 (setq type (assq (char-after) c-special-brace-lists)))
7019 (if (setq type (assq (char-after) c-special-brace-lists))
7020 (progn
7021 (setq inner-beg (point))
7022 (c-backward-syntactic-ws)
7023 (forward-char -1)
7024 (setq beg (if (eq (char-after) ?\()
7025 (point)
7026 nil)))))
7027 (if (and beg type)
7028 (if (and (c-safe
7029 (goto-char beg)
7030 (c-forward-sexp 1)
7031 (setq end (point))
7032 (= (char-before) ?\)))
7033 (c-safe
7034 (goto-char inner-beg)
7035 (if (looking-at "\\s(")
7036 ;; Check balancing of the inner paren
7037 ;; below.
7038 (progn
7039 (c-forward-sexp 1)
7040 t)
7041 ;; If the inner char isn't a paren then
7042 ;; we can't check balancing, so just
7043 ;; check the char before the outer
7044 ;; closing paren.
7045 (goto-char end)
7046 (backward-char)
7047 (c-backward-syntactic-ws)
7048 (= (char-before) (cdr type)))))
7049 (if (or (/= (char-syntax (char-before)) ?\))
7050 (= (progn
7051 (c-forward-syntactic-ws)
7052 (point))
7053 (1- end)))
7054 (cons (cons beg end) type))
7055 (cons (list beg) type)))))
7056 (error nil))))
7057
7058 (defun c-looking-at-bos (&optional lim)
7059 ;; Return non-nil if between two statements or declarations, assuming
7060 ;; point is not inside a literal or comment.
7061 ;;
7062 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
7063 ;; are recommended instead.
7064 ;;
7065 ;; This function might do hidden buffer changes.
7066 (c-at-statement-start-p))
7067 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p)
7068
7069 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
7070 ;; Return non-nil if we're looking at the beginning of a block
7071 ;; inside an expression. The value returned is actually a cons of
7072 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
7073 ;; position of the beginning of the construct.
7074 ;;
7075 ;; LIM limits the backward search. CONTAINING-SEXP is the start
7076 ;; position of the closest containing list. If it's nil, the
7077 ;; containing paren isn't used to decide whether we're inside an
7078 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
7079 ;; needs to be farther back.
7080 ;;
7081 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
7082 ;; brace block might be done. It should only be used when the
7083 ;; construct can be assumed to be complete, i.e. when the original
7084 ;; starting position was further down than that.
7085 ;;
7086 ;; This function might do hidden buffer changes.
7087
7088 (save-excursion
7089 (let ((res 'maybe) passed-paren
7090 (closest-lim (or containing-sexp lim (point-min)))
7091 ;; Look at the character after point only as a last resort
7092 ;; when we can't disambiguate.
7093 (block-follows (and (eq (char-after) ?{) (point))))
7094
7095 (while (and (eq res 'maybe)
7096 (progn (c-backward-syntactic-ws)
7097 (> (point) closest-lim))
7098 (not (bobp))
7099 (progn (backward-char)
7100 (looking-at "[\]\).]\\|\\w\\|\\s_"))
7101 (c-safe (forward-char)
7102 (goto-char (scan-sexps (point) -1))))
7103
7104 (setq res
7105 (if (looking-at c-keywords-regexp)
7106 (let ((kw-sym (c-keyword-sym (match-string 1))))
7107 (cond
7108 ((and block-follows
7109 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
7110 (and (not (eq passed-paren ?\[))
7111 (or (not (looking-at c-class-key))
7112 ;; If the class definition is at the start of
7113 ;; a statement, we don't consider it an
7114 ;; in-expression class.
7115 (let ((prev (point)))
7116 (while (and
7117 (= (c-backward-token-2 1 nil closest-lim) 0)
7118 (eq (char-syntax (char-after)) ?w))
7119 (setq prev (point)))
7120 (goto-char prev)
7121 (not (c-at-statement-start-p)))
7122 ;; Also, in Pike we treat it as an
7123 ;; in-expression class if it's used in an
7124 ;; object clone expression.
7125 (save-excursion
7126 (and check-at-end
7127 (c-major-mode-is 'pike-mode)
7128 (progn (goto-char block-follows)
7129 (zerop (c-forward-token-2 1 t)))
7130 (eq (char-after) ?\())))
7131 (cons 'inexpr-class (point))))
7132 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
7133 (when (not passed-paren)
7134 (cons 'inexpr-statement (point))))
7135 ((c-keyword-member kw-sym 'c-lambda-kwds)
7136 (when (or (not passed-paren)
7137 (eq passed-paren ?\())
7138 (cons 'inlambda (point))))
7139 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
7140 nil)
7141 (t
7142 'maybe)))
7143
7144 (if (looking-at "\\s(")
7145 (if passed-paren
7146 (if (and (eq passed-paren ?\[)
7147 (eq (char-after) ?\[))
7148 ;; Accept several square bracket sexps for
7149 ;; Java array initializations.
7150 'maybe)
7151 (setq passed-paren (char-after))
7152 'maybe)
7153 'maybe))))
7154
7155 (if (eq res 'maybe)
7156 (when (and c-recognize-paren-inexpr-blocks
7157 block-follows
7158 containing-sexp
7159 (eq (char-after containing-sexp) ?\())
7160 (goto-char containing-sexp)
7161 (if (or (save-excursion
7162 (c-backward-syntactic-ws lim)
7163 (and (> (point) (or lim (point-min)))
7164 (c-on-identifier)))
7165 (and c-special-brace-lists
7166 (c-looking-at-special-brace-list)))
7167 nil
7168 (cons 'inexpr-statement (point))))
7169
7170 res))))
7171
7172 (defun c-looking-at-inexpr-block-backward (paren-state)
7173 ;; Returns non-nil if we're looking at the end of an in-expression
7174 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
7175 ;; PAREN-STATE is the paren state relevant at the current position.
7176 ;;
7177 ;; This function might do hidden buffer changes.
7178 (save-excursion
7179 ;; We currently only recognize a block.
7180 (let ((here (point))
7181 (elem (car-safe paren-state))
7182 containing-sexp)
7183 (when (and (consp elem)
7184 (progn (goto-char (cdr elem))
7185 (c-forward-syntactic-ws here)
7186 (= (point) here)))
7187 (goto-char (car elem))
7188 (if (setq paren-state (cdr paren-state))
7189 (setq containing-sexp (car-safe paren-state)))
7190 (c-looking-at-inexpr-block (c-safe-position containing-sexp
7191 paren-state)
7192 containing-sexp)))))
7193
7194 \f
7195 ;; `c-guess-basic-syntax' and the functions that precedes it below
7196 ;; implements the main decision tree for determining the syntactic
7197 ;; analysis of the current line of code.
7198
7199 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
7200 ;; auto newline analysis.
7201 (defvar c-auto-newline-analysis nil)
7202
7203 (defun c-brace-anchor-point (bracepos)
7204 ;; BRACEPOS is the position of a brace in a construct like "namespace
7205 ;; Bar {". Return the anchor point in this construct; this is the
7206 ;; earliest symbol on the brace's line which isn't earlier than
7207 ;; "namespace".
7208 ;;
7209 ;; Currently (2007-08-17), "like namespace" means "matches
7210 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
7211 ;; or anything like that.
7212 (save-excursion
7213 (let ((boi (c-point 'boi bracepos)))
7214 (goto-char bracepos)
7215 (while (and (> (point) boi)
7216 (not (looking-at c-other-decl-block-key)))
7217 (c-backward-token-2))
7218 (if (> (point) boi) (point) boi))))
7219
7220 (defsubst c-add-syntax (symbol &rest args)
7221 ;; A simple function to prepend a new syntax element to
7222 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
7223 ;; should always be dynamically bound but since we read it first
7224 ;; we'll fail properly anyway if this function is misused.
7225 (setq c-syntactic-context (cons (cons symbol args)
7226 c-syntactic-context)))
7227
7228 (defsubst c-append-syntax (symbol &rest args)
7229 ;; Like `c-add-syntax' but appends to the end of the syntax list.
7230 ;; (Normally not necessary.)
7231 (setq c-syntactic-context (nconc c-syntactic-context
7232 (list (cons symbol args)))))
7233
7234 (defun c-add-stmt-syntax (syntax-symbol
7235 syntax-extra-args
7236 stop-at-boi-only
7237 containing-sexp
7238 paren-state)
7239 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
7240 ;; needed with further syntax elements of the types `substatement',
7241 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
7242 ;; `defun-block-intro'.
7243 ;;
7244 ;; Do the generic processing to anchor the given syntax symbol on
7245 ;; the preceding statement: Skip over any labels and containing
7246 ;; statements on the same line, and then search backward until we
7247 ;; find a statement or block start that begins at boi without a
7248 ;; label or comment.
7249 ;;
7250 ;; Point is assumed to be at the prospective anchor point for the
7251 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
7252 ;; skip past open parens and containing statements. Most of the added
7253 ;; syntax elements will get the same anchor point - the exception is
7254 ;; for an anchor in a construct like "namespace"[*] - this is as early
7255 ;; as possible in the construct but on the same line as the {.
7256 ;;
7257 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
7258 ;;
7259 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
7260 ;; syntax symbol. They are appended after the anchor point.
7261 ;;
7262 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
7263 ;; if the current statement starts there.
7264 ;;
7265 ;; Note: It's not a problem if PAREN-STATE "overshoots"
7266 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
7267 ;;
7268 ;; This function might do hidden buffer changes.
7269
7270 (if (= (point) (c-point 'boi))
7271 ;; This is by far the most common case, so let's give it special
7272 ;; treatment.
7273 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
7274
7275 (let ((syntax-last c-syntactic-context)
7276 (boi (c-point 'boi))
7277 ;; Set when we're on a label, so that we don't stop there.
7278 ;; FIXME: To be complete we should check if we're on a label
7279 ;; now at the start.
7280 on-label)
7281
7282 ;; Use point as the anchor point for "namespace", "extern", etc.
7283 (apply 'c-add-syntax syntax-symbol
7284 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
7285 (point) nil)
7286 syntax-extra-args)
7287
7288 ;; Loop while we have to back out of containing blocks.
7289 (while
7290 (and
7291 (catch 'back-up-block
7292
7293 ;; Loop while we have to back up statements.
7294 (while (or (/= (point) boi)
7295 on-label
7296 (looking-at c-comment-start-regexp))
7297
7298 ;; Skip past any comments that stands between the
7299 ;; statement start and boi.
7300 (let ((savepos (point)))
7301 (while (and (/= savepos boi)
7302 (c-backward-single-comment))
7303 (setq savepos (point)
7304 boi (c-point 'boi)))
7305 (goto-char savepos))
7306
7307 ;; Skip to the beginning of this statement or backward
7308 ;; another one.
7309 (let ((old-pos (point))
7310 (old-boi boi)
7311 (step-type (c-beginning-of-statement-1 containing-sexp)))
7312 (setq boi (c-point 'boi)
7313 on-label (eq step-type 'label))
7314
7315 (cond ((= (point) old-pos)
7316 ;; If we didn't move we're at the start of a block and
7317 ;; have to continue outside it.
7318 (throw 'back-up-block t))
7319
7320 ((and (eq step-type 'up)
7321 (>= (point) old-boi)
7322 (looking-at "else\\>[^_]")
7323 (save-excursion
7324 (goto-char old-pos)
7325 (looking-at "if\\>[^_]")))
7326 ;; Special case to avoid deeper and deeper indentation
7327 ;; of "else if" clauses.
7328 )
7329
7330 ((and (not stop-at-boi-only)
7331 (/= old-pos old-boi)
7332 (memq step-type '(up previous)))
7333 ;; If stop-at-boi-only is nil, we shouldn't back up
7334 ;; over previous or containing statements to try to
7335 ;; reach boi, so go back to the last position and
7336 ;; exit.
7337 (goto-char old-pos)
7338 (throw 'back-up-block nil))
7339
7340 (t
7341 (if (and (not stop-at-boi-only)
7342 (memq step-type '(up previous beginning)))
7343 ;; If we've moved into another statement then we
7344 ;; should no longer try to stop in the middle of a
7345 ;; line.
7346 (setq stop-at-boi-only t))
7347
7348 ;; Record this as a substatement if we skipped up one
7349 ;; level.
7350 (when (eq step-type 'up)
7351 (c-add-syntax 'substatement nil))))
7352 )))
7353
7354 containing-sexp)
7355
7356 ;; Now we have to go out of this block.
7357 (goto-char containing-sexp)
7358
7359 ;; Don't stop in the middle of a special brace list opener
7360 ;; like "({".
7361 (when c-special-brace-lists
7362 (let ((special-list (c-looking-at-special-brace-list)))
7363 (when (and special-list
7364 (< (car (car special-list)) (point)))
7365 (setq containing-sexp (car (car special-list)))
7366 (goto-char containing-sexp))))
7367
7368 (setq paren-state (c-whack-state-after containing-sexp paren-state)
7369 containing-sexp (c-most-enclosing-brace paren-state)
7370 boi (c-point 'boi))
7371
7372 ;; Analyze the construct in front of the block we've stepped out
7373 ;; from and add the right syntactic element for it.
7374 (let ((paren-pos (point))
7375 (paren-char (char-after))
7376 step-type)
7377
7378 (if (eq paren-char ?\()
7379 ;; Stepped out of a parenthesis block, so we're in an
7380 ;; expression now.
7381 (progn
7382 (when (/= paren-pos boi)
7383 (if (and c-recognize-paren-inexpr-blocks
7384 (progn
7385 (c-backward-syntactic-ws containing-sexp)
7386 (or (not (looking-at "\\>"))
7387 (not (c-on-identifier))))
7388 (save-excursion
7389 (goto-char (1+ paren-pos))
7390 (c-forward-syntactic-ws)
7391 (eq (char-after) ?{)))
7392 ;; Stepped out of an in-expression statement. This
7393 ;; syntactic element won't get an anchor pos.
7394 (c-add-syntax 'inexpr-statement)
7395
7396 ;; A parenthesis normally belongs to an arglist.
7397 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
7398
7399 (goto-char (max boi
7400 (if containing-sexp
7401 (1+ containing-sexp)
7402 (point-min))))
7403 (setq step-type 'same
7404 on-label nil))
7405
7406 ;; Stepped out of a brace block.
7407 (setq step-type (c-beginning-of-statement-1 containing-sexp)
7408 on-label (eq step-type 'label))
7409
7410 (if (and (eq step-type 'same)
7411 (/= paren-pos (point)))
7412 (let (inexpr)
7413 (cond
7414 ((save-excursion
7415 (goto-char paren-pos)
7416 (setq inexpr (c-looking-at-inexpr-block
7417 (c-safe-position containing-sexp paren-state)
7418 containing-sexp)))
7419 (c-add-syntax (if (eq (car inexpr) 'inlambda)
7420 'defun-block-intro
7421 'statement-block-intro)
7422 nil))
7423 ((looking-at c-other-decl-block-key)
7424 (c-add-syntax
7425 (cdr (assoc (match-string 1)
7426 c-other-decl-block-key-in-symbols-alist))
7427 (max (c-point 'boi paren-pos) (point))))
7428 (t (c-add-syntax 'defun-block-intro nil))))
7429
7430 (c-add-syntax 'statement-block-intro nil)))
7431
7432 (if (= paren-pos boi)
7433 ;; Always done if the open brace was at boi. The
7434 ;; c-beginning-of-statement-1 call above is necessary
7435 ;; anyway, to decide the type of block-intro to add.
7436 (goto-char paren-pos)
7437 (setq boi (c-point 'boi)))
7438 ))
7439
7440 ;; Fill in the current point as the anchor for all the symbols
7441 ;; added above.
7442 (let ((p c-syntactic-context) q)
7443 (while (not (eq p syntax-last))
7444 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
7445 (while q
7446 (unless (car q)
7447 (setcar q (point)))
7448 (setq q (cdr q)))
7449 (setq p (cdr p))))
7450 )))
7451
7452 (defun c-add-class-syntax (symbol
7453 containing-decl-open
7454 containing-decl-start
7455 containing-decl-kwd
7456 paren-state)
7457 ;; The inclass and class-close syntactic symbols are added in
7458 ;; several places and some work is needed to fix everything.
7459 ;; Therefore it's collected here.
7460 ;;
7461 ;; This function might do hidden buffer changes.
7462 (goto-char containing-decl-open)
7463 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
7464 (progn
7465 (c-add-syntax symbol containing-decl-open)
7466 containing-decl-open)
7467 (goto-char containing-decl-start)
7468 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
7469 ;; here, but we have to do like this for compatibility.
7470 (back-to-indentation)
7471 (c-add-syntax symbol (point))
7472 (if (and (c-keyword-member containing-decl-kwd
7473 'c-inexpr-class-kwds)
7474 (/= containing-decl-start (c-point 'boi containing-decl-start)))
7475 (c-add-syntax 'inexpr-class))
7476 (point)))
7477
7478 (defun c-guess-continued-construct (indent-point
7479 char-after-ip
7480 beg-of-same-or-containing-stmt
7481 containing-sexp
7482 paren-state)
7483 ;; This function contains the decision tree reached through both
7484 ;; cases 18 and 10. It's a continued statement or top level
7485 ;; construct of some kind.
7486 ;;
7487 ;; This function might do hidden buffer changes.
7488
7489 (let (special-brace-list)
7490 (goto-char indent-point)
7491 (skip-chars-forward " \t")
7492
7493 (cond
7494 ;; (CASE A removed.)
7495 ;; CASE B: open braces for class or brace-lists
7496 ((setq special-brace-list
7497 (or (and c-special-brace-lists
7498 (c-looking-at-special-brace-list))
7499 (eq char-after-ip ?{)))
7500
7501 (cond
7502 ;; CASE B.1: class-open
7503 ((save-excursion
7504 (and (eq (char-after) ?{)
7505 (c-looking-at-decl-block containing-sexp t)
7506 (setq beg-of-same-or-containing-stmt (point))))
7507 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
7508
7509 ;; CASE B.2: brace-list-open
7510 ((or (consp special-brace-list)
7511 (save-excursion
7512 (goto-char beg-of-same-or-containing-stmt)
7513 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
7514 indent-point t t t)))
7515 ;; The most semantically accurate symbol here is
7516 ;; brace-list-open, but we normally report it simply as a
7517 ;; statement-cont. The reason is that one normally adjusts
7518 ;; brace-list-open for brace lists as top-level constructs,
7519 ;; and brace lists inside statements is a completely different
7520 ;; context. C.f. case 5A.3.
7521 (c-beginning-of-statement-1 containing-sexp)
7522 (c-add-stmt-syntax (if c-auto-newline-analysis
7523 ;; Turn off the dwim above when we're
7524 ;; analyzing the nature of the brace
7525 ;; for the auto newline feature.
7526 'brace-list-open
7527 'statement-cont)
7528 nil nil
7529 containing-sexp paren-state))
7530
7531 ;; CASE B.3: The body of a function declared inside a normal
7532 ;; block. Can occur e.g. in Pike and when using gcc
7533 ;; extensions, but watch out for macros followed by blocks.
7534 ;; C.f. cases E, 16F and 17G.
7535 ((and (not (c-at-statement-start-p))
7536 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7537 'same)
7538 (save-excursion
7539 (let ((c-recognize-typeless-decls nil))
7540 ;; Turn off recognition of constructs that lacks a
7541 ;; type in this case, since that's more likely to be
7542 ;; a macro followed by a block.
7543 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7544 (c-add-stmt-syntax 'defun-open nil t
7545 containing-sexp paren-state))
7546
7547 ;; CASE B.4: Continued statement with block open. The most
7548 ;; accurate analysis is perhaps `statement-cont' together with
7549 ;; `block-open' but we play DWIM and use `substatement-open'
7550 ;; instead. The rationaly is that this typically is a macro
7551 ;; followed by a block which makes it very similar to a
7552 ;; statement with a substatement block.
7553 (t
7554 (c-add-stmt-syntax 'substatement-open nil nil
7555 containing-sexp paren-state))
7556 ))
7557
7558 ;; CASE C: iostream insertion or extraction operator
7559 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
7560 (save-excursion
7561 (goto-char beg-of-same-or-containing-stmt)
7562 ;; If there is no preceding streamop in the statement
7563 ;; then indent this line as a normal statement-cont.
7564 (when (c-syntactic-re-search-forward
7565 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
7566 (c-add-syntax 'stream-op (c-point 'boi))
7567 t))))
7568
7569 ;; CASE E: In the "K&R region" of a function declared inside a
7570 ;; normal block. C.f. case B.3.
7571 ((and (save-excursion
7572 ;; Check that the next token is a '{'. This works as
7573 ;; long as no language that allows nested function
7574 ;; definitions allows stuff like member init lists, K&R
7575 ;; declarations or throws clauses there.
7576 ;;
7577 ;; Note that we do a forward search for something ahead
7578 ;; of the indentation line here. That's not good since
7579 ;; the user might not have typed it yet. Unfortunately
7580 ;; it's exceedingly tricky to recognize a function
7581 ;; prototype in a code block without resorting to this.
7582 (c-forward-syntactic-ws)
7583 (eq (char-after) ?{))
7584 (not (c-at-statement-start-p))
7585 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7586 'same)
7587 (save-excursion
7588 (let ((c-recognize-typeless-decls nil))
7589 ;; Turn off recognition of constructs that lacks a
7590 ;; type in this case, since that's more likely to be
7591 ;; a macro followed by a block.
7592 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7593 (c-add-stmt-syntax 'func-decl-cont nil t
7594 containing-sexp paren-state))
7595
7596 ;; CASE D: continued statement.
7597 (t
7598 (c-beginning-of-statement-1 containing-sexp)
7599 (c-add-stmt-syntax 'statement-cont nil nil
7600 containing-sexp paren-state))
7601 )))
7602
7603 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
7604 ;; 2005/11/29).
7605 ;;;###autoload
7606 (defun c-guess-basic-syntax ()
7607 "Return the syntactic context of the current line."
7608 (save-excursion
7609 (beginning-of-line)
7610 (c-save-buffer-state
7611 ((indent-point (point))
7612 (case-fold-search nil)
7613 ;; A whole ugly bunch of various temporary variables. Have
7614 ;; to declare them here since it's not possible to declare
7615 ;; a variable with only the scope of a cond test and the
7616 ;; following result clauses, and most of this function is a
7617 ;; single gigantic cond. :P
7618 literal char-before-ip before-ws-ip char-after-ip macro-start
7619 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
7620 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
7621 ;; The following record some positions for the containing
7622 ;; declaration block if we're directly within one:
7623 ;; `containing-decl-open' is the position of the open
7624 ;; brace. `containing-decl-start' is the start of the
7625 ;; declaration. `containing-decl-kwd' is the keyword
7626 ;; symbol of the keyword that tells what kind of block it
7627 ;; is.
7628 containing-decl-open
7629 containing-decl-start
7630 containing-decl-kwd
7631 ;; The open paren of the closest surrounding sexp or nil if
7632 ;; there is none.
7633 containing-sexp
7634 ;; The position after the closest preceding brace sexp
7635 ;; (nested sexps are ignored), or the position after
7636 ;; `containing-sexp' if there is none, or (point-min) if
7637 ;; `containing-sexp' is nil.
7638 lim
7639 ;; The paren state outside `containing-sexp', or at
7640 ;; `indent-point' if `containing-sexp' is nil.
7641 (paren-state (c-parse-state))
7642 ;; There's always at most one syntactic element which got
7643 ;; an anchor pos. It's stored in syntactic-relpos.
7644 syntactic-relpos
7645 (c-stmt-delim-chars c-stmt-delim-chars))
7646
7647 ;; Check if we're directly inside an enclosing declaration
7648 ;; level block.
7649 (when (and (setq containing-sexp
7650 (c-most-enclosing-brace paren-state))
7651 (progn
7652 (goto-char containing-sexp)
7653 (eq (char-after) ?{))
7654 (setq placeholder
7655 (c-looking-at-decl-block
7656 (c-most-enclosing-brace paren-state
7657 containing-sexp)
7658 t)))
7659 (setq containing-decl-open containing-sexp
7660 containing-decl-start (point)
7661 containing-sexp nil)
7662 (goto-char placeholder)
7663 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
7664 (c-keyword-sym (match-string 1)))))
7665
7666 ;; Init some position variables.
7667 (if c-state-cache
7668 (progn
7669 (setq containing-sexp (car paren-state)
7670 paren-state (cdr paren-state))
7671 (if (consp containing-sexp)
7672 (progn
7673 (setq lim (cdr containing-sexp))
7674 (if (cdr c-state-cache)
7675 ;; Ignore balanced paren. The next entry
7676 ;; can't be another one.
7677 (setq containing-sexp (car (cdr c-state-cache))
7678 paren-state (cdr paren-state))
7679 ;; If there is no surrounding open paren then
7680 ;; put the last balanced pair back on paren-state.
7681 (setq paren-state (cons containing-sexp paren-state)
7682 containing-sexp nil)))
7683 (setq lim (1+ containing-sexp))))
7684 (setq lim (point-min)))
7685
7686 ;; If we're in a parenthesis list then ',' delimits the
7687 ;; "statements" rather than being an operator (with the
7688 ;; exception of the "for" clause). This difference is
7689 ;; typically only noticeable when statements are used in macro
7690 ;; arglists.
7691 (when (and containing-sexp
7692 (eq (char-after containing-sexp) ?\())
7693 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
7694
7695 ;; cache char before and after indent point, and move point to
7696 ;; the most likely position to perform the majority of tests
7697 (goto-char indent-point)
7698 (c-backward-syntactic-ws lim)
7699 (setq before-ws-ip (point)
7700 char-before-ip (char-before))
7701 (goto-char indent-point)
7702 (skip-chars-forward " \t")
7703 (setq char-after-ip (char-after))
7704
7705 ;; are we in a literal?
7706 (setq literal (c-in-literal lim))
7707
7708 ;; now figure out syntactic qualities of the current line
7709 (cond
7710
7711 ;; CASE 1: in a string.
7712 ((eq literal 'string)
7713 (c-add-syntax 'string (c-point 'bopl)))
7714
7715 ;; CASE 2: in a C or C++ style comment.
7716 ((and (memq literal '(c c++))
7717 ;; This is a kludge for XEmacs where we use
7718 ;; `buffer-syntactic-context', which doesn't correctly
7719 ;; recognize "\*/" to end a block comment.
7720 ;; `parse-partial-sexp' which is used by
7721 ;; `c-literal-limits' will however do that in most
7722 ;; versions, which results in that we get nil from
7723 ;; `c-literal-limits' even when `c-in-literal' claims
7724 ;; we're inside a comment.
7725 (setq placeholder (c-literal-limits lim)))
7726 (c-add-syntax literal (car placeholder)))
7727
7728 ;; CASE 3: in a cpp preprocessor macro continuation.
7729 ((and (save-excursion
7730 (when (c-beginning-of-macro)
7731 (setq macro-start (point))))
7732 (/= macro-start (c-point 'boi))
7733 (progn
7734 (setq tmpsymbol 'cpp-macro-cont)
7735 (or (not c-syntactic-indentation-in-macros)
7736 (save-excursion
7737 (goto-char macro-start)
7738 ;; If at the beginning of the body of a #define
7739 ;; directive then analyze as cpp-define-intro
7740 ;; only. Go on with the syntactic analysis
7741 ;; otherwise. in-macro-expr is set if we're in a
7742 ;; cpp expression, i.e. before the #define body
7743 ;; or anywhere in a non-#define directive.
7744 (if (c-forward-to-cpp-define-body)
7745 (let ((indent-boi (c-point 'boi indent-point)))
7746 (setq in-macro-expr (> (point) indent-boi)
7747 tmpsymbol 'cpp-define-intro)
7748 (= (point) indent-boi))
7749 (setq in-macro-expr t)
7750 nil)))))
7751 (c-add-syntax tmpsymbol macro-start)
7752 (setq macro-start nil))
7753
7754 ;; CASE 11: an else clause?
7755 ((looking-at "else\\>[^_]")
7756 (c-beginning-of-statement-1 containing-sexp)
7757 (c-add-stmt-syntax 'else-clause nil t
7758 containing-sexp paren-state))
7759
7760 ;; CASE 12: while closure of a do/while construct?
7761 ((and (looking-at "while\\>[^_]")
7762 (save-excursion
7763 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
7764 'beginning)
7765 (setq placeholder (point)))))
7766 (goto-char placeholder)
7767 (c-add-stmt-syntax 'do-while-closure nil t
7768 containing-sexp paren-state))
7769
7770 ;; CASE 13: A catch or finally clause? This case is simpler
7771 ;; than if-else and do-while, because a block is required
7772 ;; after every try, catch and finally.
7773 ((save-excursion
7774 (and (cond ((c-major-mode-is 'c++-mode)
7775 (looking-at "catch\\>[^_]"))
7776 ((c-major-mode-is 'java-mode)
7777 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
7778 (and (c-safe (c-backward-syntactic-ws)
7779 (c-backward-sexp)
7780 t)
7781 (eq (char-after) ?{)
7782 (c-safe (c-backward-syntactic-ws)
7783 (c-backward-sexp)
7784 t)
7785 (if (eq (char-after) ?\()
7786 (c-safe (c-backward-sexp) t)
7787 t))
7788 (looking-at "\\(try\\|catch\\)\\>[^_]")
7789 (setq placeholder (point))))
7790 (goto-char placeholder)
7791 (c-add-stmt-syntax 'catch-clause nil t
7792 containing-sexp paren-state))
7793
7794 ;; CASE 18: A substatement we can recognize by keyword.
7795 ((save-excursion
7796 (and c-opt-block-stmt-key
7797 (not (eq char-before-ip ?\;))
7798 (not (c-at-vsemi-p before-ws-ip))
7799 (not (memq char-after-ip '(?\) ?\] ?,)))
7800 (or (not (eq char-before-ip ?}))
7801 (c-looking-at-inexpr-block-backward c-state-cache))
7802 (> (point)
7803 (progn
7804 ;; Ought to cache the result from the
7805 ;; c-beginning-of-statement-1 calls here.
7806 (setq placeholder (point))
7807 (while (eq (setq step-type
7808 (c-beginning-of-statement-1 lim))
7809 'label))
7810 (if (eq step-type 'previous)
7811 (goto-char placeholder)
7812 (setq placeholder (point))
7813 (if (and (eq step-type 'same)
7814 (not (looking-at c-opt-block-stmt-key)))
7815 ;; Step up to the containing statement if we
7816 ;; stayed in the same one.
7817 (let (step)
7818 (while (eq
7819 (setq step
7820 (c-beginning-of-statement-1 lim))
7821 'label))
7822 (if (eq step 'up)
7823 (setq placeholder (point))
7824 ;; There was no containing statement afterall.
7825 (goto-char placeholder)))))
7826 placeholder))
7827 (if (looking-at c-block-stmt-2-key)
7828 ;; Require a parenthesis after these keywords.
7829 ;; Necessary to catch e.g. synchronized in Java,
7830 ;; which can be used both as statement and
7831 ;; modifier.
7832 (and (zerop (c-forward-token-2 1 nil))
7833 (eq (char-after) ?\())
7834 (looking-at c-opt-block-stmt-key))))
7835
7836 (if (eq step-type 'up)
7837 ;; CASE 18A: Simple substatement.
7838 (progn
7839 (goto-char placeholder)
7840 (cond
7841 ((eq char-after-ip ?{)
7842 (c-add-stmt-syntax 'substatement-open nil nil
7843 containing-sexp paren-state))
7844 ((save-excursion
7845 (goto-char indent-point)
7846 (back-to-indentation)
7847 (c-forward-label))
7848 (c-add-stmt-syntax 'substatement-label nil nil
7849 containing-sexp paren-state))
7850 (t
7851 (c-add-stmt-syntax 'substatement nil nil
7852 containing-sexp paren-state))))
7853
7854 ;; CASE 18B: Some other substatement. This is shared
7855 ;; with case 10.
7856 (c-guess-continued-construct indent-point
7857 char-after-ip
7858 placeholder
7859 lim
7860 paren-state)))
7861
7862 ;; CASE 14: A case or default label
7863 ((looking-at c-label-kwds-regexp)
7864 (if containing-sexp
7865 (progn
7866 (goto-char containing-sexp)
7867 (setq lim (c-most-enclosing-brace c-state-cache
7868 containing-sexp))
7869 (c-backward-to-block-anchor lim)
7870 (c-add-stmt-syntax 'case-label nil t lim paren-state))
7871 ;; Got a bogus label at the top level. In lack of better
7872 ;; alternatives, anchor it on (point-min).
7873 (c-add-syntax 'case-label (point-min))))
7874
7875 ;; CASE 15: any other label
7876 ((save-excursion
7877 (back-to-indentation)
7878 (and (not (looking-at c-syntactic-ws-start))
7879 (c-forward-label)))
7880 (cond (containing-decl-open
7881 (setq placeholder (c-add-class-syntax 'inclass
7882 containing-decl-open
7883 containing-decl-start
7884 containing-decl-kwd
7885 paren-state))
7886 ;; Append access-label with the same anchor point as
7887 ;; inclass gets.
7888 (c-append-syntax 'access-label placeholder))
7889
7890 (containing-sexp
7891 (goto-char containing-sexp)
7892 (setq lim (c-most-enclosing-brace c-state-cache
7893 containing-sexp))
7894 (save-excursion
7895 (setq tmpsymbol
7896 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
7897 (looking-at "switch\\>[^_]"))
7898 ;; If the surrounding statement is a switch then
7899 ;; let's analyze all labels as switch labels, so
7900 ;; that they get lined up consistently.
7901 'case-label
7902 'label)))
7903 (c-backward-to-block-anchor lim)
7904 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
7905
7906 (t
7907 ;; A label on the top level. Treat it as a class
7908 ;; context. (point-min) is the closest we get to the
7909 ;; class open brace.
7910 (c-add-syntax 'access-label (point-min)))))
7911
7912 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
7913 ;; 17E.
7914 ((setq placeholder (c-looking-at-inexpr-block
7915 (c-safe-position containing-sexp paren-state)
7916 containing-sexp
7917 ;; Have to turn on the heuristics after
7918 ;; the point even though it doesn't work
7919 ;; very well. C.f. test case class-16.pike.
7920 t))
7921 (setq tmpsymbol (assq (car placeholder)
7922 '((inexpr-class . class-open)
7923 (inexpr-statement . block-open))))
7924 (if tmpsymbol
7925 ;; It's a statement block or an anonymous class.
7926 (setq tmpsymbol (cdr tmpsymbol))
7927 ;; It's a Pike lambda. Check whether we are between the
7928 ;; lambda keyword and the argument list or at the defun
7929 ;; opener.
7930 (setq tmpsymbol (if (eq char-after-ip ?{)
7931 'inline-open
7932 'lambda-intro-cont)))
7933 (goto-char (cdr placeholder))
7934 (back-to-indentation)
7935 (c-add-stmt-syntax tmpsymbol nil t
7936 (c-most-enclosing-brace c-state-cache (point))
7937 paren-state)
7938 (unless (eq (point) (cdr placeholder))
7939 (c-add-syntax (car placeholder))))
7940
7941 ;; CASE 5: Line is inside a declaration level block or at top level.
7942 ((or containing-decl-open (null containing-sexp))
7943 (cond
7944
7945 ;; CASE 5A: we are looking at a defun, brace list, class,
7946 ;; or inline-inclass method opening brace
7947 ((setq special-brace-list
7948 (or (and c-special-brace-lists
7949 (c-looking-at-special-brace-list))
7950 (eq char-after-ip ?{)))
7951 (cond
7952
7953 ;; CASE 5A.1: Non-class declaration block open.
7954 ((save-excursion
7955 (let (tmp)
7956 (and (eq char-after-ip ?{)
7957 (setq tmp (c-looking-at-decl-block containing-sexp t))
7958 (progn
7959 (setq placeholder (point))
7960 (goto-char tmp)
7961 (looking-at c-symbol-key))
7962 (c-keyword-member
7963 (c-keyword-sym (setq keyword (match-string 0)))
7964 'c-other-block-decl-kwds))))
7965 (goto-char placeholder)
7966 (c-add-stmt-syntax
7967 (if (string-equal keyword "extern")
7968 ;; Special case for extern-lang-open.
7969 'extern-lang-open
7970 (intern (concat keyword "-open")))
7971 nil t containing-sexp paren-state))
7972
7973 ;; CASE 5A.2: we are looking at a class opening brace
7974 ((save-excursion
7975 (goto-char indent-point)
7976 (skip-chars-forward " \t")
7977 (and (eq (char-after) ?{)
7978 (c-looking-at-decl-block containing-sexp t)
7979 (setq placeholder (point))))
7980 (c-add-syntax 'class-open placeholder))
7981
7982 ;; CASE 5A.3: brace list open
7983 ((save-excursion
7984 (c-beginning-of-decl-1 lim)
7985 (while (looking-at c-specifier-key)
7986 (goto-char (match-end 1))
7987 (c-forward-syntactic-ws indent-point))
7988 (setq placeholder (c-point 'boi))
7989 (or (consp special-brace-list)
7990 (and (or (save-excursion
7991 (goto-char indent-point)
7992 (setq tmpsymbol nil)
7993 (while (and (> (point) placeholder)
7994 (zerop (c-backward-token-2 1 t))
7995 (/= (char-after) ?=))
7996 (and c-opt-inexpr-brace-list-key
7997 (not tmpsymbol)
7998 (looking-at c-opt-inexpr-brace-list-key)
7999 (setq tmpsymbol 'topmost-intro-cont)))
8000 (eq (char-after) ?=))
8001 (looking-at c-brace-list-key))
8002 (save-excursion
8003 (while (and (< (point) indent-point)
8004 (zerop (c-forward-token-2 1 t))
8005 (not (memq (char-after) '(?\; ?\()))))
8006 (not (memq (char-after) '(?\; ?\()))
8007 ))))
8008 (if (and (not c-auto-newline-analysis)
8009 (c-major-mode-is 'java-mode)
8010 (eq tmpsymbol 'topmost-intro-cont))
8011 ;; We're in Java and have found that the open brace
8012 ;; belongs to a "new Foo[]" initialization list,
8013 ;; which means the brace list is part of an
8014 ;; expression and not a top level definition. We
8015 ;; therefore treat it as any topmost continuation
8016 ;; even though the semantically correct symbol still
8017 ;; is brace-list-open, on the same grounds as in
8018 ;; case B.2.
8019 (progn
8020 (c-beginning-of-statement-1 lim)
8021 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8022 (c-add-syntax 'brace-list-open placeholder)))
8023
8024 ;; CASE 5A.4: inline defun open
8025 ((and containing-decl-open
8026 (not (c-keyword-member containing-decl-kwd
8027 'c-other-block-decl-kwds)))
8028 (c-add-syntax 'inline-open)
8029 (c-add-class-syntax 'inclass
8030 containing-decl-open
8031 containing-decl-start
8032 containing-decl-kwd
8033 paren-state))
8034
8035 ;; CASE 5A.5: ordinary defun open
8036 (t
8037 (save-excursion
8038 (c-beginning-of-decl-1 lim)
8039 (while (looking-at c-specifier-key)
8040 (goto-char (match-end 1))
8041 (c-forward-syntactic-ws indent-point))
8042 (c-add-syntax 'defun-open (c-point 'boi))
8043 ;; Bogus to use bol here, but it's the legacy. (Resolved,
8044 ;; 2007-11-09)
8045 ))))
8046
8047 ;; CASE 5B: After a function header but before the body (or
8048 ;; the ending semicolon if there's no body).
8049 ((save-excursion
8050 (when (setq placeholder (c-just-after-func-arglist-p lim))
8051 (setq tmp-pos (point))))
8052 (cond
8053
8054 ;; CASE 5B.1: Member init list.
8055 ((eq (char-after tmp-pos) ?:)
8056 (if (or (> tmp-pos indent-point)
8057 (= (c-point 'bosws) (1+ tmp-pos)))
8058 (progn
8059 ;; There is no preceding member init clause.
8060 ;; Indent relative to the beginning of indentation
8061 ;; for the topmost-intro line that contains the
8062 ;; prototype's open paren.
8063 (goto-char placeholder)
8064 (c-add-syntax 'member-init-intro (c-point 'boi)))
8065 ;; Indent relative to the first member init clause.
8066 (goto-char (1+ tmp-pos))
8067 (c-forward-syntactic-ws)
8068 (c-add-syntax 'member-init-cont (point))))
8069
8070 ;; CASE 5B.2: K&R arg decl intro
8071 ((and c-recognize-knr-p
8072 (c-in-knr-argdecl lim))
8073 (c-beginning-of-statement-1 lim)
8074 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
8075 (if containing-decl-open
8076 (c-add-class-syntax 'inclass
8077 containing-decl-open
8078 containing-decl-start
8079 containing-decl-kwd
8080 paren-state)))
8081
8082 ;; CASE 5B.4: Nether region after a C++ or Java func
8083 ;; decl, which could include a `throws' declaration.
8084 (t
8085 (c-beginning-of-statement-1 lim)
8086 (c-add-syntax 'func-decl-cont (c-point 'boi))
8087 )))
8088
8089 ;; CASE 5C: inheritance line. could be first inheritance
8090 ;; line, or continuation of a multiple inheritance
8091 ((or (and (c-major-mode-is 'c++-mode)
8092 (progn
8093 (when (eq char-after-ip ?,)
8094 (skip-chars-forward " \t")
8095 (forward-char))
8096 (looking-at c-opt-postfix-decl-spec-key)))
8097 (and (or (eq char-before-ip ?:)
8098 ;; watch out for scope operator
8099 (save-excursion
8100 (and (eq char-after-ip ?:)
8101 (c-safe (forward-char 1) t)
8102 (not (eq (char-after) ?:))
8103 )))
8104 (save-excursion
8105 (c-backward-syntactic-ws lim)
8106 (if (eq char-before-ip ?:)
8107 (progn
8108 (forward-char -1)
8109 (c-backward-syntactic-ws lim)))
8110 (back-to-indentation)
8111 (looking-at c-class-key)))
8112 ;; for Java
8113 (and (c-major-mode-is 'java-mode)
8114 (let ((fence (save-excursion
8115 (c-beginning-of-statement-1 lim)
8116 (point)))
8117 cont done)
8118 (save-excursion
8119 (while (not done)
8120 (cond ((looking-at c-opt-postfix-decl-spec-key)
8121 (setq injava-inher (cons cont (point))
8122 done t))
8123 ((or (not (c-safe (c-forward-sexp -1) t))
8124 (<= (point) fence))
8125 (setq done t))
8126 )
8127 (setq cont t)))
8128 injava-inher)
8129 (not (c-crosses-statement-barrier-p (cdr injava-inher)
8130 (point)))
8131 ))
8132 (cond
8133
8134 ;; CASE 5C.1: non-hanging colon on an inher intro
8135 ((eq char-after-ip ?:)
8136 (c-beginning-of-statement-1 lim)
8137 (c-add-syntax 'inher-intro (c-point 'boi))
8138 ;; don't add inclass symbol since relative point already
8139 ;; contains any class offset
8140 )
8141
8142 ;; CASE 5C.2: hanging colon on an inher intro
8143 ((eq char-before-ip ?:)
8144 (c-beginning-of-statement-1 lim)
8145 (c-add-syntax 'inher-intro (c-point 'boi))
8146 (if containing-decl-open
8147 (c-add-class-syntax 'inclass
8148 containing-decl-open
8149 containing-decl-start
8150 containing-decl-kwd
8151 paren-state)))
8152
8153 ;; CASE 5C.3: in a Java implements/extends
8154 (injava-inher
8155 (let ((where (cdr injava-inher))
8156 (cont (car injava-inher)))
8157 (goto-char where)
8158 (cond ((looking-at "throws\\>[^_]")
8159 (c-add-syntax 'func-decl-cont
8160 (progn (c-beginning-of-statement-1 lim)
8161 (c-point 'boi))))
8162 (cont (c-add-syntax 'inher-cont where))
8163 (t (c-add-syntax 'inher-intro
8164 (progn (goto-char (cdr injava-inher))
8165 (c-beginning-of-statement-1 lim)
8166 (point))))
8167 )))
8168
8169 ;; CASE 5C.4: a continued inheritance line
8170 (t
8171 (c-beginning-of-inheritance-list lim)
8172 (c-add-syntax 'inher-cont (point))
8173 ;; don't add inclass symbol since relative point already
8174 ;; contains any class offset
8175 )))
8176
8177 ;; CASE 5D: this could be a top-level initialization, a
8178 ;; member init list continuation, or a template argument
8179 ;; list continuation.
8180 ((save-excursion
8181 ;; Note: We use the fact that lim always is after any
8182 ;; preceding brace sexp.
8183 (if c-recognize-<>-arglists
8184 (while (and
8185 (progn
8186 (c-syntactic-skip-backward "^;,=<>" lim t)
8187 (> (point) lim))
8188 (or
8189 (when c-overloadable-operators-regexp
8190 (when (setq placeholder (c-after-special-operator-id lim))
8191 (goto-char placeholder)
8192 t))
8193 (cond
8194 ((eq (char-before) ?>)
8195 (or (c-backward-<>-arglist nil lim)
8196 (backward-char))
8197 t)
8198 ((eq (char-before) ?<)
8199 (backward-char)
8200 (if (save-excursion
8201 (c-forward-<>-arglist nil))
8202 (progn (forward-char)
8203 nil)
8204 t))
8205 (t nil)))))
8206 ;; NB: No c-after-special-operator-id stuff in this
8207 ;; clause - we assume only C++ needs it.
8208 (c-syntactic-skip-backward "^;,=" lim t))
8209 (memq (char-before) '(?, ?= ?<)))
8210 (cond
8211
8212 ;; CASE 5D.3: perhaps a template list continuation?
8213 ((and (c-major-mode-is 'c++-mode)
8214 (save-excursion
8215 (save-restriction
8216 (c-with-syntax-table c++-template-syntax-table
8217 (goto-char indent-point)
8218 (setq placeholder (c-up-list-backward))
8219 (and placeholder
8220 (eq (char-after placeholder) ?<))))))
8221 (c-with-syntax-table c++-template-syntax-table
8222 (goto-char placeholder)
8223 (c-beginning-of-statement-1 lim t)
8224 (if (save-excursion
8225 (c-backward-syntactic-ws lim)
8226 (eq (char-before) ?<))
8227 ;; In a nested template arglist.
8228 (progn
8229 (goto-char placeholder)
8230 (c-syntactic-skip-backward "^,;" lim t)
8231 (c-forward-syntactic-ws))
8232 (back-to-indentation)))
8233 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8234 ;; template aware.
8235 (c-add-syntax 'template-args-cont (point)))
8236
8237 ;; CASE 5D.4: perhaps a multiple inheritance line?
8238 ((and (c-major-mode-is 'c++-mode)
8239 (save-excursion
8240 (c-beginning-of-statement-1 lim)
8241 (setq placeholder (point))
8242 (if (looking-at "static\\>[^_]")
8243 (c-forward-token-2 1 nil indent-point))
8244 (and (looking-at c-class-key)
8245 (zerop (c-forward-token-2 2 nil indent-point))
8246 (if (eq (char-after) ?<)
8247 (c-with-syntax-table c++-template-syntax-table
8248 (zerop (c-forward-token-2 1 t indent-point)))
8249 t)
8250 (eq (char-after) ?:))))
8251 (goto-char placeholder)
8252 (c-add-syntax 'inher-cont (c-point 'boi)))
8253
8254 ;; CASE 5D.5: Continuation of the "expression part" of a
8255 ;; top level construct.
8256 (t
8257 (while (and (eq (car (c-beginning-of-decl-1 containing-sexp))
8258 'same)
8259 (save-excursion
8260 (c-backward-syntactic-ws)
8261 (eq (char-before) ?}))))
8262 (c-add-stmt-syntax
8263 (if (eq char-before-ip ?,)
8264 ;; A preceding comma at the top level means that a
8265 ;; new variable declaration starts here. Use
8266 ;; topmost-intro-cont for it, for consistency with
8267 ;; the first variable declaration. C.f. case 5N.
8268 'topmost-intro-cont
8269 'statement-cont)
8270 nil nil containing-sexp paren-state))
8271 ))
8272
8273 ;; CASE 5F: Close of a non-class declaration level block.
8274 ((and (eq char-after-ip ?})
8275 (c-keyword-member containing-decl-kwd
8276 'c-other-block-decl-kwds))
8277 ;; This is inconsistent: Should use `containing-decl-open'
8278 ;; here if it's at boi, like in case 5J.
8279 (goto-char containing-decl-start)
8280 (c-add-stmt-syntax
8281 (if (string-equal (symbol-name containing-decl-kwd) "extern")
8282 ;; Special case for compatibility with the
8283 ;; extern-lang syntactic symbols.
8284 'extern-lang-close
8285 (intern (concat (symbol-name containing-decl-kwd)
8286 "-close")))
8287 nil t
8288 (c-most-enclosing-brace paren-state (point))
8289 paren-state))
8290
8291 ;; CASE 5G: we are looking at the brace which closes the
8292 ;; enclosing nested class decl
8293 ((and containing-sexp
8294 (eq char-after-ip ?})
8295 (eq containing-decl-open containing-sexp))
8296 (c-add-class-syntax 'class-close
8297 containing-decl-open
8298 containing-decl-start
8299 containing-decl-kwd
8300 paren-state))
8301
8302 ;; CASE 5H: we could be looking at subsequent knr-argdecls
8303 ((and c-recognize-knr-p
8304 (not containing-sexp) ; can't be knr inside braces.
8305 (not (eq char-before-ip ?}))
8306 (save-excursion
8307 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
8308 (and placeholder
8309 ;; Do an extra check to avoid tripping up on
8310 ;; statements that occur in invalid contexts
8311 ;; (e.g. in macro bodies where we don't really
8312 ;; know the context of what we're looking at).
8313 (not (and c-opt-block-stmt-key
8314 (looking-at c-opt-block-stmt-key)))))
8315 (< placeholder indent-point))
8316 (goto-char placeholder)
8317 (c-add-syntax 'knr-argdecl (point)))
8318
8319 ;; CASE 5I: ObjC method definition.
8320 ((and c-opt-method-key
8321 (looking-at c-opt-method-key))
8322 (c-beginning-of-statement-1 nil t)
8323 (if (= (point) indent-point)
8324 ;; Handle the case when it's the first (non-comment)
8325 ;; thing in the buffer. Can't look for a 'same return
8326 ;; value from cbos1 since ObjC directives currently
8327 ;; aren't recognized fully, so that we get 'same
8328 ;; instead of 'previous if it moved over a preceding
8329 ;; directive.
8330 (goto-char (point-min)))
8331 (c-add-syntax 'objc-method-intro (c-point 'boi)))
8332
8333 ;; CASE 5P: AWK pattern or function or continuation
8334 ;; thereof.
8335 ((c-major-mode-is 'awk-mode)
8336 (setq placeholder (point))
8337 (c-add-stmt-syntax
8338 (if (and (eq (c-beginning-of-statement-1) 'same)
8339 (/= (point) placeholder))
8340 'topmost-intro-cont
8341 'topmost-intro)
8342 nil nil
8343 containing-sexp paren-state))
8344
8345 ;; CASE 5N: At a variable declaration that follows a class
8346 ;; definition or some other block declaration that doesn't
8347 ;; end at the closing '}'. C.f. case 5D.5.
8348 ((progn
8349 (c-backward-syntactic-ws lim)
8350 (and (eq (char-before) ?})
8351 (save-excursion
8352 (let ((start (point)))
8353 (if (and c-state-cache
8354 (consp (car c-state-cache))
8355 (eq (cdar c-state-cache) (point)))
8356 ;; Speed up the backward search a bit.
8357 (goto-char (caar c-state-cache)))
8358 (c-beginning-of-decl-1 containing-sexp)
8359 (setq placeholder (point))
8360 (if (= start (point))
8361 ;; The '}' is unbalanced.
8362 nil
8363 (c-end-of-decl-1)
8364 (>= (point) indent-point))))))
8365 (goto-char placeholder)
8366 (c-add-stmt-syntax 'topmost-intro-cont nil nil
8367 containing-sexp paren-state))
8368
8369 ;; NOTE: The point is at the end of the previous token here.
8370
8371 ;; CASE 5J: we are at the topmost level, make
8372 ;; sure we skip back past any access specifiers
8373 ((and
8374 ;; A macro continuation line is never at top level.
8375 (not (and macro-start
8376 (> indent-point macro-start)))
8377 (save-excursion
8378 (setq placeholder (point))
8379 (or (memq char-before-ip '(?\; ?{ ?} nil))
8380 (c-at-vsemi-p before-ws-ip)
8381 (when (and (eq char-before-ip ?:)
8382 (eq (c-beginning-of-statement-1 lim)
8383 'label))
8384 (c-backward-syntactic-ws lim)
8385 (setq placeholder (point)))
8386 (and (c-major-mode-is 'objc-mode)
8387 (catch 'not-in-directive
8388 (c-beginning-of-statement-1 lim)
8389 (setq placeholder (point))
8390 (while (and (c-forward-objc-directive)
8391 (< (point) indent-point))
8392 (c-forward-syntactic-ws)
8393 (if (>= (point) indent-point)
8394 (throw 'not-in-directive t))
8395 (setq placeholder (point)))
8396 nil)))))
8397 ;; For historic reasons we anchor at bol of the last
8398 ;; line of the previous declaration. That's clearly
8399 ;; highly bogus and useless, and it makes our lives hard
8400 ;; to remain compatible. :P
8401 (goto-char placeholder)
8402 (c-add-syntax 'topmost-intro (c-point 'bol))
8403 (if containing-decl-open
8404 (if (c-keyword-member containing-decl-kwd
8405 'c-other-block-decl-kwds)
8406 (progn
8407 (goto-char (c-brace-anchor-point containing-decl-open))
8408 (c-add-stmt-syntax
8409 (if (string-equal (symbol-name containing-decl-kwd)
8410 "extern")
8411 ;; Special case for compatibility with the
8412 ;; extern-lang syntactic symbols.
8413 'inextern-lang
8414 (intern (concat "in"
8415 (symbol-name containing-decl-kwd))))
8416 nil t
8417 (c-most-enclosing-brace paren-state (point))
8418 paren-state))
8419 (c-add-class-syntax 'inclass
8420 containing-decl-open
8421 containing-decl-start
8422 containing-decl-kwd
8423 paren-state)))
8424 (when (and c-syntactic-indentation-in-macros
8425 macro-start
8426 (/= macro-start (c-point 'boi indent-point)))
8427 (c-add-syntax 'cpp-define-intro)
8428 (setq macro-start nil)))
8429
8430 ;; CASE 5K: we are at an ObjC method definition
8431 ;; continuation line.
8432 ((and c-opt-method-key
8433 (save-excursion
8434 (c-beginning-of-statement-1 lim)
8435 (beginning-of-line)
8436 (when (looking-at c-opt-method-key)
8437 (setq placeholder (point)))))
8438 (c-add-syntax 'objc-method-args-cont placeholder))
8439
8440 ;; CASE 5L: we are at the first argument of a template
8441 ;; arglist that begins on the previous line.
8442 ((and c-recognize-<>-arglists
8443 (eq (char-before) ?<)
8444 (not (and c-overloadable-operators-regexp
8445 (c-after-special-operator-id lim))))
8446 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8447 (c-add-syntax 'template-args-cont (c-point 'boi)))
8448
8449 ;; CASE 5Q: we are at a statement within a macro.
8450 (macro-start
8451 (c-beginning-of-statement-1 containing-sexp)
8452 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
8453
8454 ;; CASE 5M: we are at a topmost continuation line
8455 (t
8456 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8457 (when (c-major-mode-is 'objc-mode)
8458 (setq placeholder (point))
8459 (while (and (c-forward-objc-directive)
8460 (< (point) indent-point))
8461 (c-forward-syntactic-ws)
8462 (setq placeholder (point)))
8463 (goto-char placeholder))
8464 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8465 ))
8466
8467 ;; (CASE 6 has been removed.)
8468
8469 ;; CASE 7: line is an expression, not a statement. Most
8470 ;; likely we are either in a function prototype or a function
8471 ;; call argument list
8472 ((not (or (and c-special-brace-lists
8473 (save-excursion
8474 (goto-char containing-sexp)
8475 (c-looking-at-special-brace-list)))
8476 (eq (char-after containing-sexp) ?{)))
8477 (cond
8478
8479 ;; CASE 7A: we are looking at the arglist closing paren.
8480 ;; C.f. case 7F.
8481 ((memq char-after-ip '(?\) ?\]))
8482 (goto-char containing-sexp)
8483 (setq placeholder (c-point 'boi))
8484 (if (and (c-safe (backward-up-list 1) t)
8485 (>= (point) placeholder))
8486 (progn
8487 (forward-char)
8488 (skip-chars-forward " \t"))
8489 (goto-char placeholder))
8490 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
8491 (c-most-enclosing-brace paren-state (point))
8492 paren-state))
8493
8494 ;; CASE 7B: Looking at the opening brace of an
8495 ;; in-expression block or brace list. C.f. cases 4, 16A
8496 ;; and 17E.
8497 ((and (eq char-after-ip ?{)
8498 (progn
8499 (setq placeholder (c-inside-bracelist-p (point)
8500 paren-state))
8501 (if placeholder
8502 (setq tmpsymbol '(brace-list-open . inexpr-class))
8503 (setq tmpsymbol '(block-open . inexpr-statement)
8504 placeholder
8505 (cdr-safe (c-looking-at-inexpr-block
8506 (c-safe-position containing-sexp
8507 paren-state)
8508 containing-sexp)))
8509 ;; placeholder is nil if it's a block directly in
8510 ;; a function arglist. That makes us skip out of
8511 ;; this case.
8512 )))
8513 (goto-char placeholder)
8514 (back-to-indentation)
8515 (c-add-stmt-syntax (car tmpsymbol) nil t
8516 (c-most-enclosing-brace paren-state (point))
8517 paren-state)
8518 (if (/= (point) placeholder)
8519 (c-add-syntax (cdr tmpsymbol))))
8520
8521 ;; CASE 7C: we are looking at the first argument in an empty
8522 ;; argument list. Use arglist-close if we're actually
8523 ;; looking at a close paren or bracket.
8524 ((memq char-before-ip '(?\( ?\[))
8525 (goto-char containing-sexp)
8526 (setq placeholder (c-point 'boi))
8527 (if (and (c-safe (backward-up-list 1) t)
8528 (>= (point) placeholder))
8529 (progn
8530 (forward-char)
8531 (skip-chars-forward " \t"))
8532 (goto-char placeholder))
8533 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
8534 (c-most-enclosing-brace paren-state (point))
8535 paren-state))
8536
8537 ;; CASE 7D: we are inside a conditional test clause. treat
8538 ;; these things as statements
8539 ((progn
8540 (goto-char containing-sexp)
8541 (and (c-safe (c-forward-sexp -1) t)
8542 (looking-at "\\<for\\>[^_]")))
8543 (goto-char (1+ containing-sexp))
8544 (c-forward-syntactic-ws indent-point)
8545 (if (eq char-before-ip ?\;)
8546 (c-add-syntax 'statement (point))
8547 (c-add-syntax 'statement-cont (point))
8548 ))
8549
8550 ;; CASE 7E: maybe a continued ObjC method call. This is the
8551 ;; case when we are inside a [] bracketed exp, and what
8552 ;; precede the opening bracket is not an identifier.
8553 ((and c-opt-method-key
8554 (eq (char-after containing-sexp) ?\[)
8555 (progn
8556 (goto-char (1- containing-sexp))
8557 (c-backward-syntactic-ws (c-point 'bod))
8558 (if (not (looking-at c-symbol-key))
8559 (c-add-syntax 'objc-method-call-cont containing-sexp))
8560 )))
8561
8562 ;; CASE 7F: we are looking at an arglist continuation line,
8563 ;; but the preceding argument is on the same line as the
8564 ;; opening paren. This case includes multi-line
8565 ;; mathematical paren groupings, but we could be on a
8566 ;; for-list continuation line. C.f. case 7A.
8567 ((progn
8568 (goto-char (1+ containing-sexp))
8569 (< (save-excursion
8570 (c-forward-syntactic-ws)
8571 (point))
8572 (c-point 'bonl)))
8573 (goto-char containing-sexp)
8574 (setq placeholder (c-point 'boi))
8575 (if (and (c-safe (backward-up-list 1) t)
8576 (>= (point) placeholder))
8577 (progn
8578 (forward-char)
8579 (skip-chars-forward " \t"))
8580 (goto-char placeholder))
8581 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
8582 (c-most-enclosing-brace c-state-cache (point))
8583 paren-state))
8584
8585 ;; CASE 7G: we are looking at just a normal arglist
8586 ;; continuation line
8587 (t (c-forward-syntactic-ws indent-point)
8588 (c-add-syntax 'arglist-cont (c-point 'boi)))
8589 ))
8590
8591 ;; CASE 8: func-local multi-inheritance line
8592 ((and (c-major-mode-is 'c++-mode)
8593 (save-excursion
8594 (goto-char indent-point)
8595 (skip-chars-forward " \t")
8596 (looking-at c-opt-postfix-decl-spec-key)))
8597 (goto-char indent-point)
8598 (skip-chars-forward " \t")
8599 (cond
8600
8601 ;; CASE 8A: non-hanging colon on an inher intro
8602 ((eq char-after-ip ?:)
8603 (c-backward-syntactic-ws lim)
8604 (c-add-syntax 'inher-intro (c-point 'boi)))
8605
8606 ;; CASE 8B: hanging colon on an inher intro
8607 ((eq char-before-ip ?:)
8608 (c-add-syntax 'inher-intro (c-point 'boi)))
8609
8610 ;; CASE 8C: a continued inheritance line
8611 (t
8612 (c-beginning-of-inheritance-list lim)
8613 (c-add-syntax 'inher-cont (point))
8614 )))
8615
8616 ;; CASE 9: we are inside a brace-list
8617 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
8618 (setq special-brace-list
8619 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
8620 (save-excursion
8621 (goto-char containing-sexp)
8622 (c-looking-at-special-brace-list)))
8623 (c-inside-bracelist-p containing-sexp paren-state))))
8624 (cond
8625
8626 ;; CASE 9A: In the middle of a special brace list opener.
8627 ((and (consp special-brace-list)
8628 (save-excursion
8629 (goto-char containing-sexp)
8630 (eq (char-after) ?\())
8631 (eq char-after-ip (car (cdr special-brace-list))))
8632 (goto-char (car (car special-brace-list)))
8633 (skip-chars-backward " \t")
8634 (if (and (bolp)
8635 (assoc 'statement-cont
8636 (setq placeholder (c-guess-basic-syntax))))
8637 (setq c-syntactic-context placeholder)
8638 (c-beginning-of-statement-1
8639 (c-safe-position (1- containing-sexp) paren-state))
8640 (c-forward-token-2 0)
8641 (while (looking-at c-specifier-key)
8642 (goto-char (match-end 1))
8643 (c-forward-syntactic-ws))
8644 (c-add-syntax 'brace-list-open (c-point 'boi))))
8645
8646 ;; CASE 9B: brace-list-close brace
8647 ((if (consp special-brace-list)
8648 ;; Check special brace list closer.
8649 (progn
8650 (goto-char (car (car special-brace-list)))
8651 (save-excursion
8652 (goto-char indent-point)
8653 (back-to-indentation)
8654 (or
8655 ;; We were between the special close char and the `)'.
8656 (and (eq (char-after) ?\))
8657 (eq (1+ (point)) (cdr (car special-brace-list))))
8658 ;; We were before the special close char.
8659 (and (eq (char-after) (cdr (cdr special-brace-list)))
8660 (zerop (c-forward-token-2))
8661 (eq (1+ (point)) (cdr (car special-brace-list)))))))
8662 ;; Normal brace list check.
8663 (and (eq char-after-ip ?})
8664 (c-safe (goto-char (c-up-list-backward (point))) t)
8665 (= (point) containing-sexp)))
8666 (if (eq (point) (c-point 'boi))
8667 (c-add-syntax 'brace-list-close (point))
8668 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8669 (c-beginning-of-statement-1 lim)
8670 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
8671
8672 (t
8673 ;; Prepare for the rest of the cases below by going to the
8674 ;; token following the opening brace
8675 (if (consp special-brace-list)
8676 (progn
8677 (goto-char (car (car special-brace-list)))
8678 (c-forward-token-2 1 nil indent-point))
8679 (goto-char containing-sexp))
8680 (forward-char)
8681 (let ((start (point)))
8682 (c-forward-syntactic-ws indent-point)
8683 (goto-char (max start (c-point 'bol))))
8684 (c-skip-ws-forward indent-point)
8685 (cond
8686
8687 ;; CASE 9C: we're looking at the first line in a brace-list
8688 ((= (point) indent-point)
8689 (if (consp special-brace-list)
8690 (goto-char (car (car special-brace-list)))
8691 (goto-char containing-sexp))
8692 (if (eq (point) (c-point 'boi))
8693 (c-add-syntax 'brace-list-intro (point))
8694 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8695 (c-beginning-of-statement-1 lim)
8696 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
8697
8698 ;; CASE 9D: this is just a later brace-list-entry or
8699 ;; brace-entry-open
8700 (t (if (or (eq char-after-ip ?{)
8701 (and c-special-brace-lists
8702 (save-excursion
8703 (goto-char indent-point)
8704 (c-forward-syntactic-ws (c-point 'eol))
8705 (c-looking-at-special-brace-list (point)))))
8706 (c-add-syntax 'brace-entry-open (point))
8707 (c-add-syntax 'brace-list-entry (point))
8708 ))
8709 ))))
8710
8711 ;; CASE 10: A continued statement or top level construct.
8712 ((and (not (memq char-before-ip '(?\; ?:)))
8713 (not (c-at-vsemi-p before-ws-ip))
8714 (or (not (eq char-before-ip ?}))
8715 (c-looking-at-inexpr-block-backward c-state-cache))
8716 (> (point)
8717 (save-excursion
8718 (c-beginning-of-statement-1 containing-sexp)
8719 (setq placeholder (point))))
8720 (/= placeholder containing-sexp))
8721 ;; This is shared with case 18.
8722 (c-guess-continued-construct indent-point
8723 char-after-ip
8724 placeholder
8725 containing-sexp
8726 paren-state))
8727
8728 ;; CASE 16: block close brace, possibly closing the defun or
8729 ;; the class
8730 ((eq char-after-ip ?})
8731 ;; From here on we have the next containing sexp in lim.
8732 (setq lim (c-most-enclosing-brace paren-state))
8733 (goto-char containing-sexp)
8734 (cond
8735
8736 ;; CASE 16E: Closing a statement block? This catches
8737 ;; cases where it's preceded by a statement keyword,
8738 ;; which works even when used in an "invalid" context,
8739 ;; e.g. a macro argument.
8740 ((c-after-conditional)
8741 (c-backward-to-block-anchor lim)
8742 (c-add-stmt-syntax 'block-close nil t lim paren-state))
8743
8744 ;; CASE 16A: closing a lambda defun or an in-expression
8745 ;; block? C.f. cases 4, 7B and 17E.
8746 ((setq placeholder (c-looking-at-inexpr-block
8747 (c-safe-position containing-sexp paren-state)
8748 nil))
8749 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8750 'inline-close
8751 'block-close))
8752 (goto-char containing-sexp)
8753 (back-to-indentation)
8754 (if (= containing-sexp (point))
8755 (c-add-syntax tmpsymbol (point))
8756 (goto-char (cdr placeholder))
8757 (back-to-indentation)
8758 (c-add-stmt-syntax tmpsymbol nil t
8759 (c-most-enclosing-brace paren-state (point))
8760 paren-state)
8761 (if (/= (point) (cdr placeholder))
8762 (c-add-syntax (car placeholder)))))
8763
8764 ;; CASE 16B: does this close an inline or a function in
8765 ;; a non-class declaration level block?
8766 ((save-excursion
8767 (and lim
8768 (progn
8769 (goto-char lim)
8770 (c-looking-at-decl-block
8771 (c-most-enclosing-brace paren-state lim)
8772 nil))
8773 (setq placeholder (point))))
8774 (c-backward-to-decl-anchor lim)
8775 (back-to-indentation)
8776 (if (save-excursion
8777 (goto-char placeholder)
8778 (looking-at c-other-decl-block-key))
8779 (c-add-syntax 'defun-close (point))
8780 (c-add-syntax 'inline-close (point))))
8781
8782 ;; CASE 16F: Can be a defun-close of a function declared
8783 ;; in a statement block, e.g. in Pike or when using gcc
8784 ;; extensions, but watch out for macros followed by
8785 ;; blocks. Let it through to be handled below.
8786 ;; C.f. cases B.3 and 17G.
8787 ((save-excursion
8788 (and (not (c-at-statement-start-p))
8789 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8790 (setq placeholder (point))
8791 (let ((c-recognize-typeless-decls nil))
8792 ;; Turn off recognition of constructs that
8793 ;; lacks a type in this case, since that's more
8794 ;; likely to be a macro followed by a block.
8795 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8796 (back-to-indentation)
8797 (if (/= (point) containing-sexp)
8798 (goto-char placeholder))
8799 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
8800
8801 ;; CASE 16C: If there is an enclosing brace then this is
8802 ;; a block close since defun closes inside declaration
8803 ;; level blocks have been handled above.
8804 (lim
8805 ;; If the block is preceded by a case/switch label on
8806 ;; the same line, we anchor at the first preceding label
8807 ;; at boi. The default handling in c-add-stmt-syntax
8808 ;; really fixes it better, but we do like this to keep
8809 ;; the indentation compatible with version 5.28 and
8810 ;; earlier. C.f. case 17H.
8811 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8812 (eq (c-beginning-of-statement-1 lim) 'label)))
8813 (goto-char placeholder)
8814 (if (looking-at c-label-kwds-regexp)
8815 (c-add-syntax 'block-close (point))
8816 (goto-char containing-sexp)
8817 ;; c-backward-to-block-anchor not necessary here; those
8818 ;; situations are handled in case 16E above.
8819 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
8820
8821 ;; CASE 16D: Only top level defun close left.
8822 (t
8823 (goto-char containing-sexp)
8824 (c-backward-to-decl-anchor lim)
8825 (c-add-stmt-syntax 'defun-close nil nil
8826 (c-most-enclosing-brace paren-state)
8827 paren-state))
8828 ))
8829
8830 ;; CASE 17: Statement or defun catchall.
8831 (t
8832 (goto-char indent-point)
8833 ;; Back up statements until we find one that starts at boi.
8834 (while (let* ((prev-point (point))
8835 (last-step-type (c-beginning-of-statement-1
8836 containing-sexp)))
8837 (if (= (point) prev-point)
8838 (progn
8839 (setq step-type (or step-type last-step-type))
8840 nil)
8841 (setq step-type last-step-type)
8842 (/= (point) (c-point 'boi)))))
8843 (cond
8844
8845 ;; CASE 17B: continued statement
8846 ((and (eq step-type 'same)
8847 (/= (point) indent-point))
8848 (c-add-stmt-syntax 'statement-cont nil nil
8849 containing-sexp paren-state))
8850
8851 ;; CASE 17A: After a case/default label?
8852 ((progn
8853 (while (and (eq step-type 'label)
8854 (not (looking-at c-label-kwds-regexp)))
8855 (setq step-type
8856 (c-beginning-of-statement-1 containing-sexp)))
8857 (eq step-type 'label))
8858 (c-add-stmt-syntax (if (eq char-after-ip ?{)
8859 'statement-case-open
8860 'statement-case-intro)
8861 nil t containing-sexp paren-state))
8862
8863 ;; CASE 17D: any old statement
8864 ((progn
8865 (while (eq step-type 'label)
8866 (setq step-type
8867 (c-beginning-of-statement-1 containing-sexp)))
8868 (eq step-type 'previous))
8869 (c-add-stmt-syntax 'statement nil t
8870 containing-sexp paren-state)
8871 (if (eq char-after-ip ?{)
8872 (c-add-syntax 'block-open)))
8873
8874 ;; CASE 17I: Inside a substatement block.
8875 ((progn
8876 ;; The following tests are all based on containing-sexp.
8877 (goto-char containing-sexp)
8878 ;; From here on we have the next containing sexp in lim.
8879 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
8880 (c-after-conditional))
8881 (c-backward-to-block-anchor lim)
8882 (c-add-stmt-syntax 'statement-block-intro nil t
8883 lim paren-state)
8884 (if (eq char-after-ip ?{)
8885 (c-add-syntax 'block-open)))
8886
8887 ;; CASE 17E: first statement in an in-expression block.
8888 ;; C.f. cases 4, 7B and 16A.
8889 ((setq placeholder (c-looking-at-inexpr-block
8890 (c-safe-position containing-sexp paren-state)
8891 nil))
8892 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8893 'defun-block-intro
8894 'statement-block-intro))
8895 (back-to-indentation)
8896 (if (= containing-sexp (point))
8897 (c-add-syntax tmpsymbol (point))
8898 (goto-char (cdr placeholder))
8899 (back-to-indentation)
8900 (c-add-stmt-syntax tmpsymbol nil t
8901 (c-most-enclosing-brace c-state-cache (point))
8902 paren-state)
8903 (if (/= (point) (cdr placeholder))
8904 (c-add-syntax (car placeholder))))
8905 (if (eq char-after-ip ?{)
8906 (c-add-syntax 'block-open)))
8907
8908 ;; CASE 17F: first statement in an inline, or first
8909 ;; statement in a top-level defun. we can tell this is it
8910 ;; if there are no enclosing braces that haven't been
8911 ;; narrowed out by a class (i.e. don't use bod here).
8912 ((save-excursion
8913 (or (not (setq placeholder (c-most-enclosing-brace
8914 paren-state)))
8915 (and (progn
8916 (goto-char placeholder)
8917 (eq (char-after) ?{))
8918 (c-looking-at-decl-block (c-most-enclosing-brace
8919 paren-state (point))
8920 nil))))
8921 (c-backward-to-decl-anchor lim)
8922 (back-to-indentation)
8923 (c-add-syntax 'defun-block-intro (point)))
8924
8925 ;; CASE 17G: First statement in a function declared inside
8926 ;; a normal block. This can occur in Pike and with
8927 ;; e.g. the gcc extensions, but watch out for macros
8928 ;; followed by blocks. C.f. cases B.3 and 16F.
8929 ((save-excursion
8930 (and (not (c-at-statement-start-p))
8931 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8932 (setq placeholder (point))
8933 (let ((c-recognize-typeless-decls nil))
8934 ;; Turn off recognition of constructs that lacks
8935 ;; a type in this case, since that's more likely
8936 ;; to be a macro followed by a block.
8937 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8938 (back-to-indentation)
8939 (if (/= (point) containing-sexp)
8940 (goto-char placeholder))
8941 (c-add-stmt-syntax 'defun-block-intro nil t
8942 lim paren-state))
8943
8944 ;; CASE 17H: First statement in a block.
8945 (t
8946 ;; If the block is preceded by a case/switch label on the
8947 ;; same line, we anchor at the first preceding label at
8948 ;; boi. The default handling in c-add-stmt-syntax is
8949 ;; really fixes it better, but we do like this to keep the
8950 ;; indentation compatible with version 5.28 and earlier.
8951 ;; C.f. case 16C.
8952 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8953 (eq (c-beginning-of-statement-1 lim) 'label)))
8954 (goto-char placeholder)
8955 (if (looking-at c-label-kwds-regexp)
8956 (c-add-syntax 'statement-block-intro (point))
8957 (goto-char containing-sexp)
8958 ;; c-backward-to-block-anchor not necessary here; those
8959 ;; situations are handled in case 17I above.
8960 (c-add-stmt-syntax 'statement-block-intro nil t
8961 lim paren-state))
8962 (if (eq char-after-ip ?{)
8963 (c-add-syntax 'block-open)))
8964 ))
8965 )
8966
8967 ;; now we need to look at any modifiers
8968 (goto-char indent-point)
8969 (skip-chars-forward " \t")
8970
8971 ;; are we looking at a comment only line?
8972 (when (and (looking-at c-comment-start-regexp)
8973 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
8974 (c-append-syntax 'comment-intro))
8975
8976 ;; we might want to give additional offset to friends (in C++).
8977 (when (and c-opt-friend-key
8978 (looking-at c-opt-friend-key))
8979 (c-append-syntax 'friend))
8980
8981 ;; Set syntactic-relpos.
8982 (let ((p c-syntactic-context))
8983 (while (and p
8984 (if (integerp (c-langelem-pos (car p)))
8985 (progn
8986 (setq syntactic-relpos (c-langelem-pos (car p)))
8987 nil)
8988 t))
8989 (setq p (cdr p))))
8990
8991 ;; Start of or a continuation of a preprocessor directive?
8992 (if (and macro-start
8993 (eq macro-start (c-point 'boi))
8994 (not (and (c-major-mode-is 'pike-mode)
8995 (eq (char-after (1+ macro-start)) ?\"))))
8996 (c-append-syntax 'cpp-macro)
8997 (when (and c-syntactic-indentation-in-macros macro-start)
8998 (if in-macro-expr
8999 (when (or
9000 (< syntactic-relpos macro-start)
9001 (not (or
9002 (assq 'arglist-intro c-syntactic-context)
9003 (assq 'arglist-cont c-syntactic-context)
9004 (assq 'arglist-cont-nonempty c-syntactic-context)
9005 (assq 'arglist-close c-syntactic-context))))
9006 ;; If inside a cpp expression, i.e. anywhere in a
9007 ;; cpp directive except a #define body, we only let
9008 ;; through the syntactic analysis that is internal
9009 ;; in the expression. That means the arglist
9010 ;; elements, if they are anchored inside the cpp
9011 ;; expression.
9012 (setq c-syntactic-context nil)
9013 (c-add-syntax 'cpp-macro-cont macro-start))
9014 (when (and (eq macro-start syntactic-relpos)
9015 (not (assq 'cpp-define-intro c-syntactic-context))
9016 (save-excursion
9017 (goto-char macro-start)
9018 (or (not (c-forward-to-cpp-define-body))
9019 (<= (point) (c-point 'boi indent-point)))))
9020 ;; Inside a #define body and the syntactic analysis is
9021 ;; anchored on the start of the #define. In this case
9022 ;; we add cpp-define-intro to get the extra
9023 ;; indentation of the #define body.
9024 (c-add-syntax 'cpp-define-intro)))))
9025
9026 ;; return the syntax
9027 c-syntactic-context)))
9028
9029 \f
9030 ;; Indentation calculation.
9031
9032 (defun c-evaluate-offset (offset langelem symbol)
9033 ;; offset can be a number, a function, a variable, a list, or one of
9034 ;; the symbols + or -
9035 ;;
9036 ;; This function might do hidden buffer changes.
9037 (let ((res
9038 (cond
9039 ((numberp offset) offset)
9040 ((vectorp offset) offset)
9041 ((null offset) nil)
9042
9043 ((eq offset '+) c-basic-offset)
9044 ((eq offset '-) (- c-basic-offset))
9045 ((eq offset '++) (* 2 c-basic-offset))
9046 ((eq offset '--) (* 2 (- c-basic-offset)))
9047 ((eq offset '*) (/ c-basic-offset 2))
9048 ((eq offset '/) (/ (- c-basic-offset) 2))
9049
9050 ((functionp offset)
9051 (c-evaluate-offset
9052 (funcall offset
9053 (cons (c-langelem-sym langelem)
9054 (c-langelem-pos langelem)))
9055 langelem symbol))
9056
9057 ((listp offset)
9058 (cond
9059 ((eq (car offset) 'quote)
9060 (c-benign-error "The offset %S for %s was mistakenly quoted"
9061 offset symbol)
9062 nil)
9063
9064 ((memq (car offset) '(min max))
9065 (let (res val (method (car offset)))
9066 (setq offset (cdr offset))
9067 (while offset
9068 (setq val (c-evaluate-offset (car offset) langelem symbol))
9069 (cond
9070 ((not val))
9071 ((not res)
9072 (setq res val))
9073 ((integerp val)
9074 (if (vectorp res)
9075 (c-benign-error "\
9076 Error evaluating offset %S for %s: \
9077 Cannot combine absolute offset %S with relative %S in `%s' method"
9078 (car offset) symbol res val method)
9079 (setq res (funcall method res val))))
9080 (t
9081 (if (integerp res)
9082 (c-benign-error "\
9083 Error evaluating offset %S for %s: \
9084 Cannot combine relative offset %S with absolute %S in `%s' method"
9085 (car offset) symbol res val method)
9086 (setq res (vector (funcall method (aref res 0)
9087 (aref val 0)))))))
9088 (setq offset (cdr offset)))
9089 res))
9090
9091 ((eq (car offset) 'add)
9092 (let (res val)
9093 (setq offset (cdr offset))
9094 (while offset
9095 (setq val (c-evaluate-offset (car offset) langelem symbol))
9096 (cond
9097 ((not val))
9098 ((not res)
9099 (setq res val))
9100 ((integerp val)
9101 (if (vectorp res)
9102 (setq res (vector (+ (aref res 0) val)))
9103 (setq res (+ res val))))
9104 (t
9105 (if (vectorp res)
9106 (c-benign-error "\
9107 Error evaluating offset %S for %s: \
9108 Cannot combine absolute offsets %S and %S in `add' method"
9109 (car offset) symbol res val)
9110 (setq res val)))) ; Override.
9111 (setq offset (cdr offset)))
9112 res))
9113
9114 (t
9115 (let (res)
9116 (when (eq (car offset) 'first)
9117 (setq offset (cdr offset)))
9118 (while (and (not res) offset)
9119 (setq res (c-evaluate-offset (car offset) langelem symbol)
9120 offset (cdr offset)))
9121 res))))
9122
9123 ((and (symbolp offset) (boundp offset))
9124 (symbol-value offset))
9125
9126 (t
9127 (c-benign-error "Unknown offset format %S for %s" offset symbol)
9128 nil))))
9129
9130 (if (or (null res) (integerp res)
9131 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
9132 res
9133 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
9134 offset symbol res)
9135 nil)))
9136
9137 (defun c-calc-offset (langelem)
9138 ;; Get offset from LANGELEM which is a list beginning with the
9139 ;; syntactic symbol and followed by any analysis data it provides.
9140 ;; That data may be zero or more elements, but if at least one is
9141 ;; given then the first is the anchor position (or nil). The symbol
9142 ;; is matched against `c-offsets-alist' and the offset calculated
9143 ;; from that is returned.
9144 ;;
9145 ;; This function might do hidden buffer changes.
9146 (let* ((symbol (c-langelem-sym langelem))
9147 (match (assq symbol c-offsets-alist))
9148 (offset (cdr-safe match)))
9149 (if match
9150 (setq offset (c-evaluate-offset offset langelem symbol))
9151 (if c-strict-syntax-p
9152 (c-benign-error "No offset found for syntactic symbol %s" symbol))
9153 (setq offset 0))
9154 (if (vectorp offset)
9155 offset
9156 (or (and (numberp offset) offset)
9157 (and (symbolp offset) (symbol-value offset))
9158 0))
9159 ))
9160
9161 (defun c-get-offset (langelem)
9162 ;; This is a compatibility wrapper for `c-calc-offset' in case
9163 ;; someone is calling it directly. It takes an old style syntactic
9164 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
9165 ;; new list form.
9166 ;;
9167 ;; This function might do hidden buffer changes.
9168 (if (c-langelem-pos langelem)
9169 (c-calc-offset (list (c-langelem-sym langelem)
9170 (c-langelem-pos langelem)))
9171 (c-calc-offset langelem)))
9172
9173 (defun c-get-syntactic-indentation (langelems)
9174 ;; Calculate the syntactic indentation from a syntactic description
9175 ;; as returned by `c-guess-syntax'.
9176 ;;
9177 ;; Note that topmost-intro always has an anchor position at bol, for
9178 ;; historical reasons. It's often used together with other symbols
9179 ;; that has more sane positions. Since we always use the first
9180 ;; found anchor position, we rely on that these other symbols always
9181 ;; precede topmost-intro in the LANGELEMS list.
9182 ;;
9183 ;; This function might do hidden buffer changes.
9184 (let ((indent 0) anchor)
9185
9186 (while langelems
9187 (let* ((c-syntactic-element (car langelems))
9188 (res (c-calc-offset c-syntactic-element)))
9189
9190 (if (vectorp res)
9191 ;; Got an absolute column that overrides any indentation
9192 ;; we've collected so far, but not the relative
9193 ;; indentation we might get for the nested structures
9194 ;; further down the langelems list.
9195 (setq indent (elt res 0)
9196 anchor (point-min)) ; A position at column 0.
9197
9198 ;; Got a relative change of the current calculated
9199 ;; indentation.
9200 (setq indent (+ indent res))
9201
9202 ;; Use the anchor position from the first syntactic
9203 ;; element with one.
9204 (unless anchor
9205 (setq anchor (c-langelem-pos (car langelems)))))
9206
9207 (setq langelems (cdr langelems))))
9208
9209 (if anchor
9210 (+ indent (save-excursion
9211 (goto-char anchor)
9212 (current-column)))
9213 indent)))
9214
9215 \f
9216 (cc-provide 'cc-engine)
9217
9218 ;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
9219 ;;; cc-engine.el ends here