]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-engine.el
a53dd61c81e1b6fdbc8b092888ec3c19cc953c36
[gnu-emacs] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 ;; Free Software Foundation, Inc.
6
7 ;; Authors: 2001- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs
11 ;; 1987 Stewart Clamen
12 ;; 1985 Richard M. Stallman
13 ;; Maintainer: bug-cc-mode@gnu.org
14 ;; Created: 22-Apr-1997 (split from cc-mode.el)
15 ;; Version: See cc-mode.el
16 ;; Keywords: c languages oop
17
18 ;; This file is part of GNU Emacs.
19
20 ;; GNU Emacs is free software: you can redistribute it and/or modify
21 ;; it under the terms of the GNU General Public License as published by
22 ;; the Free Software Foundation, either version 3 of the License, or
23 ;; (at your option) any later version.
24
25 ;; GNU Emacs is distributed in the hope that it will be useful,
26 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;; GNU General Public License for more details.
29
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32
33 ;;; Commentary:
34
35 ;; The functions which have docstring documentation can be considered
36 ;; part of an API which other packages can use in CC Mode buffers.
37 ;; Otoh, undocumented functions and functions with the documentation
38 ;; in comments are considered purely internal and can change semantics
39 ;; or even disappear in the future.
40 ;;
41 ;; (This policy applies to CC Mode as a whole, not just this file. It
42 ;; probably also applies to many other Emacs packages, but here it's
43 ;; clearly spelled out.)
44
45 ;; Hidden buffer changes
46 ;;
47 ;; Various functions in CC Mode use text properties for caching and
48 ;; syntactic markup purposes, and those of them that might modify such
49 ;; properties but still don't modify the buffer in a visible way are
50 ;; said to do "hidden buffer changes". They should be used within
51 ;; `c-save-buffer-state' or a similar function that saves and restores
52 ;; buffer modifiedness, disables buffer change hooks, etc.
53 ;;
54 ;; Interactive functions are assumed to not do hidden buffer changes,
55 ;; except in the specific parts of them that do real changes.
56 ;;
57 ;; Lineup functions are assumed to do hidden buffer changes. They
58 ;; must not do real changes, though.
59 ;;
60 ;; All other functions that do hidden buffer changes have that noted
61 ;; in their doc string or comment.
62 ;;
63 ;; The intention with this system is to avoid wrapping every leaf
64 ;; function that do hidden buffer changes inside
65 ;; `c-save-buffer-state'. It should be used as near the top of the
66 ;; interactive functions as possible.
67 ;;
68 ;; Functions called during font locking are allowed to do hidden
69 ;; buffer changes since the font-lock package run them in a context
70 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
71 ;; inspired by `save-buffer-state' in the font-lock package).
72
73 ;; Use of text properties
74 ;;
75 ;; CC Mode uses several text properties internally to mark up various
76 ;; positions, e.g. to improve speed and to eliminate glitches in
77 ;; interactive refontification.
78 ;;
79 ;; Note: This doc is for internal use only. Other packages should not
80 ;; assume that these text properties are used as described here.
81 ;;
82 ;; 'syntax-table
83 ;; Used to modify the syntax of some characters. It is used to
84 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
85 ;; to "hide" obtrusive characters in preprocessor lines.
86 ;;
87 ;; This property is used on single characters and is therefore
88 ;; always treated as front and rear nonsticky (or start and end open
89 ;; in XEmacs vocabulary). It's therefore installed on
90 ;; `text-property-default-nonsticky' if that variable exists (Emacs
91 ;; >= 21).
92 ;;
93 ;; 'c-is-sws and 'c-in-sws
94 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
95 ;; speed them up. See the comment blurb before `c-put-is-sws'
96 ;; below for further details.
97 ;;
98 ;; 'c-type
99 ;; This property is used on single characters to mark positions with
100 ;; special syntactic relevance of various sorts. Its primary use is
101 ;; to avoid glitches when multiline constructs are refontified
102 ;; interactively (on font lock decoration level 3). It's cleared in
103 ;; a region before it's fontified and is then put on relevant chars
104 ;; in that region as they are encountered during the fontification.
105 ;; The value specifies the kind of position:
106 ;;
107 ;; 'c-decl-arg-start
108 ;; Put on the last char of the token preceding each declaration
109 ;; inside a declaration style arglist (typically in a function
110 ;; prototype).
111 ;;
112 ;; 'c-decl-end
113 ;; Put on the last char of the token preceding a declaration.
114 ;; This is used in cases where declaration boundaries can't be
115 ;; recognized simply by looking for a token like ";" or "}".
116 ;; `c-type-decl-end-used' must be set if this is used (see also
117 ;; `c-find-decl-spots').
118 ;;
119 ;; 'c-<>-arg-sep
120 ;; Put on the commas that separate arguments in angle bracket
121 ;; arglists like C++ template arglists.
122 ;;
123 ;; 'c-decl-id-start and 'c-decl-type-start
124 ;; Put on the last char of the token preceding each declarator
125 ;; in the declarator list of a declaration. They are also used
126 ;; between the identifiers cases like enum declarations.
127 ;; 'c-decl-type-start is used when the declarators are types,
128 ;; 'c-decl-id-start otherwise.
129 ;;
130 ;; 'c-awk-NL-prop
131 ;; Used in AWK mode to mark the various kinds of newlines. See
132 ;; cc-awk.el.
133
134 ;;; Code:
135
136 (eval-when-compile
137 (let ((load-path
138 (if (and (boundp 'byte-compile-dest-file)
139 (stringp byte-compile-dest-file))
140 (cons (file-name-directory byte-compile-dest-file) load-path)
141 load-path)))
142 (load "cc-bytecomp" nil t)))
143
144 (cc-require 'cc-defs)
145 (cc-require-when-compile 'cc-langs)
146 (cc-require 'cc-vars)
147
148 ;; Silence the compiler.
149 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
150
151 \f
152 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
153
154 (defmacro c-declare-lang-variables ()
155 `(progn
156 ,@(apply 'nconc
157 (mapcar (lambda (init)
158 `(,(if (elt init 2)
159 `(defvar ,(car init) nil ,(elt init 2))
160 `(defvar ,(car init) nil))
161 (make-variable-buffer-local ',(car init))))
162 (cdr c-lang-variable-inits)))))
163 (c-declare-lang-variables)
164
165 \f
166 ;;; Internal state variables.
167
168 ;; Internal state of hungry delete key feature
169 (defvar c-hungry-delete-key nil)
170 (make-variable-buffer-local 'c-hungry-delete-key)
171
172 ;; The electric flag (toggled by `c-toggle-electric-state').
173 ;; If t, electric actions (like automatic reindentation, and (if
174 ;; c-auto-newline is also set) auto newlining) will happen when an electric
175 ;; key like `{' is pressed (or an electric keyword like `else').
176 (defvar c-electric-flag t)
177 (make-variable-buffer-local 'c-electric-flag)
178
179 ;; Internal state of auto newline feature.
180 (defvar c-auto-newline nil)
181 (make-variable-buffer-local 'c-auto-newline)
182
183 ;; Included in the mode line to indicate the active submodes.
184 ;; (defvar c-submode-indicators nil)
185 ;; (make-variable-buffer-local 'c-submode-indicators)
186
187 (defun c-calculate-state (arg prevstate)
188 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
189 ;; arg is nil or zero, toggle the state. If arg is negative, turn
190 ;; the state off, and if arg is positive, turn the state on
191 (if (or (not arg)
192 (zerop (setq arg (prefix-numeric-value arg))))
193 (not prevstate)
194 (> arg 0)))
195
196 ;; Dynamically bound cache for `c-in-literal'.
197 (defvar c-in-literal-cache t)
198
199 \f
200 ;; Basic handling of preprocessor directives.
201
202 ;; This is a dynamically bound cache used together with
203 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
204 ;; works as long as point doesn't cross a macro boundary.
205 (defvar c-macro-start 'unknown)
206
207 (defsubst c-query-and-set-macro-start ()
208 (if (symbolp c-macro-start)
209 (setq c-macro-start (save-excursion
210 (c-save-buffer-state ()
211 (and (c-beginning-of-macro)
212 (point)))))
213 c-macro-start))
214
215 (defsubst c-query-macro-start ()
216 (if (symbolp c-macro-start)
217 (save-excursion
218 (c-save-buffer-state ()
219 (and (c-beginning-of-macro)
220 (point))))
221 c-macro-start))
222
223 (defun c-beginning-of-macro (&optional lim)
224 "Go to the beginning of a preprocessor directive.
225 Leave point at the beginning of the directive and return t if in one,
226 otherwise return nil and leave point unchanged.
227
228 Note that this function might do hidden buffer changes. See the
229 comment at the start of cc-engine.el for more info."
230 (when c-opt-cpp-prefix
231 (let ((here (point)))
232 (save-restriction
233 (if lim (narrow-to-region lim (point-max)))
234 (beginning-of-line)
235 (while (eq (char-before (1- (point))) ?\\)
236 (forward-line -1))
237 (back-to-indentation)
238 (if (and (<= (point) here)
239 (looking-at c-opt-cpp-start))
240 t
241 (goto-char here)
242 nil)))))
243
244 (defun c-end-of-macro ()
245 "Go to the end of a preprocessor directive.
246 More accurately, move the point to the end of the closest following
247 line that doesn't end with a line continuation backslash - no check is
248 done that the point is inside a cpp directive to begin with.
249
250 Note that this function might do hidden buffer changes. See the
251 comment at the start of cc-engine.el for more info."
252 (while (progn
253 (end-of-line)
254 (when (and (eq (char-before) ?\\)
255 (not (eobp)))
256 (forward-char)
257 t))))
258
259 (defun c-forward-over-cpp-define-id ()
260 ;; Assuming point is at the "#" that introduces a preprocessor
261 ;; directive, it's moved forward to the end of the identifier which is
262 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
263 ;; is returned in this case, in all other cases nil is returned and
264 ;; point isn't moved.
265 ;;
266 ;; This function might do hidden buffer changes.
267 (when (and c-opt-cpp-macro-define-id
268 (looking-at c-opt-cpp-macro-define-id))
269 (goto-char (match-end 0))))
270
271 (defun c-forward-to-cpp-define-body ()
272 ;; Assuming point is at the "#" that introduces a preprocessor
273 ;; directive, it's moved forward to the start of the definition body
274 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
275 ;; specifies). Non-nil is returned in this case, in all other cases
276 ;; nil is returned and point isn't moved.
277 ;;
278 ;; This function might do hidden buffer changes.
279 (when (and c-opt-cpp-macro-define-start
280 (looking-at c-opt-cpp-macro-define-start)
281 (not (= (match-end 0) (c-point 'eol))))
282 (goto-char (match-end 0))))
283
284 \f
285 ;;; Basic utility functions.
286
287 (defun c-syntactic-content (from to paren-level)
288 ;; Return the given region as a string where all syntactic
289 ;; whitespace is removed or, where necessary, replaced with a single
290 ;; space. If PAREN-LEVEL is given then all parens in the region are
291 ;; collapsed to "()", "[]" etc.
292 ;;
293 ;; This function might do hidden buffer changes.
294
295 (save-excursion
296 (save-restriction
297 (narrow-to-region from to)
298 (goto-char from)
299 (let* ((parts (list nil)) (tail parts) pos in-paren)
300
301 (while (re-search-forward c-syntactic-ws-start to t)
302 (goto-char (setq pos (match-beginning 0)))
303 (c-forward-syntactic-ws)
304 (if (= (point) pos)
305 (forward-char)
306
307 (when paren-level
308 (save-excursion
309 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
310 pos (point))))
311
312 (if (and (> pos from)
313 (< (point) to)
314 (looking-at "\\w\\|\\s_")
315 (save-excursion
316 (goto-char (1- pos))
317 (looking-at "\\w\\|\\s_")))
318 (progn
319 (setcdr tail (list (buffer-substring-no-properties from pos)
320 " "))
321 (setq tail (cddr tail)))
322 (setcdr tail (list (buffer-substring-no-properties from pos)))
323 (setq tail (cdr tail)))
324
325 (when in-paren
326 (when (= (car (parse-partial-sexp pos to -1)) -1)
327 (setcdr tail (list (buffer-substring-no-properties
328 (1- (point)) (point))))
329 (setq tail (cdr tail))))
330
331 (setq from (point))))
332
333 (setcdr tail (list (buffer-substring-no-properties from to)))
334 (apply 'concat (cdr parts))))))
335
336 (defun c-shift-line-indentation (shift-amt)
337 ;; Shift the indentation of the current line with the specified
338 ;; amount (positive inwards). The buffer is modified only if
339 ;; SHIFT-AMT isn't equal to zero.
340 (let ((pos (- (point-max) (point)))
341 (c-macro-start c-macro-start)
342 tmp-char-inserted)
343 (if (zerop shift-amt)
344 nil
345 ;; If we're on an empty line inside a macro, we take the point
346 ;; to be at the current indentation and shift it to the
347 ;; appropriate column. This way we don't treat the extra
348 ;; whitespace out to the line continuation as indentation.
349 (when (and (c-query-and-set-macro-start)
350 (looking-at "[ \t]*\\\\$")
351 (save-excursion
352 (skip-chars-backward " \t")
353 (bolp)))
354 (insert ?x)
355 (backward-char)
356 (setq tmp-char-inserted t))
357 (unwind-protect
358 (let ((col (current-indentation)))
359 (delete-region (c-point 'bol) (c-point 'boi))
360 (beginning-of-line)
361 (indent-to (+ col shift-amt)))
362 (when tmp-char-inserted
363 (delete-char 1))))
364 ;; If initial point was within line's indentation and we're not on
365 ;; a line with a line continuation in a macro, position after the
366 ;; indentation. Else stay at same point in text.
367 (if (and (< (point) (c-point 'boi))
368 (not tmp-char-inserted))
369 (back-to-indentation)
370 (if (> (- (point-max) pos) (point))
371 (goto-char (- (point-max) pos))))))
372
373 (defsubst c-keyword-sym (keyword)
374 ;; Return non-nil if the string KEYWORD is a known keyword. More
375 ;; precisely, the value is the symbol for the keyword in
376 ;; `c-keywords-obarray'.
377 (intern-soft keyword c-keywords-obarray))
378
379 (defsubst c-keyword-member (keyword-sym lang-constant)
380 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
381 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
382 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
383 ;; nil then the result is nil.
384 (get keyword-sym lang-constant))
385
386 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
387 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
388 "\"|"
389 "\""))
390
391 ;; Regexp matching string limit syntax.
392 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
393 "\\s\"\\|\\s|"
394 "\\s\""))
395
396 ;; Regexp matching WS followed by string limit syntax.
397 (defconst c-ws*-string-limit-regexp
398 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
399
400 ;; Holds formatted error strings for the few cases where parse errors
401 ;; are reported.
402 (defvar c-parsing-error nil)
403 (make-variable-buffer-local 'c-parsing-error)
404
405 (defun c-echo-parsing-error (&optional quiet)
406 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
407 (c-benign-error "%s" c-parsing-error))
408 c-parsing-error)
409
410 ;; Faces given to comments and string literals. This is used in some
411 ;; situations to speed up recognition; it isn't mandatory that font
412 ;; locking is in use. This variable is extended with the face in
413 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
414 (defvar c-literal-faces
415 (append '(font-lock-comment-face font-lock-string-face)
416 (when (facep 'font-lock-comment-delimiter-face)
417 ;; New in Emacs 22.
418 '(font-lock-comment-delimiter-face))))
419
420 (defsubst c-put-c-type-property (pos value)
421 ;; Put a c-type property with the given value at POS.
422 (c-put-char-property pos 'c-type value))
423
424 (defun c-clear-c-type-property (from to value)
425 ;; Remove all occurrences of the c-type property that has the given
426 ;; value in the region between FROM and TO. VALUE is assumed to not
427 ;; be nil.
428 ;;
429 ;; Note: This assumes that c-type is put on single chars only; it's
430 ;; very inefficient if matching properties cover large regions.
431 (save-excursion
432 (goto-char from)
433 (while (progn
434 (when (eq (get-text-property (point) 'c-type) value)
435 (c-clear-char-property (point) 'c-type))
436 (goto-char (next-single-property-change (point) 'c-type nil to))
437 (< (point) to)))))
438
439 \f
440 ;; Some debug tools to visualize various special positions. This
441 ;; debug code isn't as portable as the rest of CC Mode.
442
443 (cc-bytecomp-defun overlays-in)
444 (cc-bytecomp-defun overlay-get)
445 (cc-bytecomp-defun overlay-start)
446 (cc-bytecomp-defun overlay-end)
447 (cc-bytecomp-defun delete-overlay)
448 (cc-bytecomp-defun overlay-put)
449 (cc-bytecomp-defun make-overlay)
450
451 (defun c-debug-add-face (beg end face)
452 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
453 (while overlays
454 (setq overlay (car overlays)
455 overlays (cdr overlays))
456 (when (eq (overlay-get overlay 'face) face)
457 (setq beg (min beg (overlay-start overlay))
458 end (max end (overlay-end overlay)))
459 (delete-overlay overlay)))
460 (overlay-put (make-overlay beg end) 'face face)))
461
462 (defun c-debug-remove-face (beg end face)
463 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
464 (ol-beg beg) (ol-end end))
465 (while overlays
466 (setq overlay (car overlays)
467 overlays (cdr overlays))
468 (when (eq (overlay-get overlay 'face) face)
469 (setq ol-beg (min ol-beg (overlay-start overlay))
470 ol-end (max ol-end (overlay-end overlay)))
471 (delete-overlay overlay)))
472 (when (< ol-beg beg)
473 (overlay-put (make-overlay ol-beg beg) 'face face))
474 (when (> ol-end end)
475 (overlay-put (make-overlay end ol-end) 'face face))))
476
477 \f
478 ;; `c-beginning-of-statement-1' and accompanying stuff.
479
480 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
481 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
482 ;; better way should be implemented, but this will at least shut up
483 ;; the byte compiler.
484 (defvar c-maybe-labelp)
485
486 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
487
488 ;; Macros used internally in c-beginning-of-statement-1 for the
489 ;; automaton actions.
490 (defmacro c-bos-push-state ()
491 '(setq stack (cons (cons state saved-pos)
492 stack)))
493 (defmacro c-bos-pop-state (&optional do-if-done)
494 `(if (setq state (car (car stack))
495 saved-pos (cdr (car stack))
496 stack (cdr stack))
497 t
498 ,do-if-done
499 (throw 'loop nil)))
500 (defmacro c-bos-pop-state-and-retry ()
501 '(throw 'loop (setq state (car (car stack))
502 saved-pos (cdr (car stack))
503 ;; Throw nil if stack is empty, else throw non-nil.
504 stack (cdr stack))))
505 (defmacro c-bos-save-pos ()
506 '(setq saved-pos (vector pos tok ptok pptok)))
507 (defmacro c-bos-restore-pos ()
508 '(unless (eq (elt saved-pos 0) start)
509 (setq pos (elt saved-pos 0)
510 tok (elt saved-pos 1)
511 ptok (elt saved-pos 2)
512 pptok (elt saved-pos 3))
513 (goto-char pos)
514 (setq sym nil)))
515 (defmacro c-bos-save-error-info (missing got)
516 `(setq saved-pos (vector pos ,missing ,got)))
517 (defmacro c-bos-report-error ()
518 '(unless noerror
519 (setq c-parsing-error
520 (format "No matching `%s' found for `%s' on line %d"
521 (elt saved-pos 1)
522 (elt saved-pos 2)
523 (1+ (count-lines (point-min)
524 (c-point 'bol (elt saved-pos 0))))))))
525
526 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
527 noerror comma-delim)
528 "Move to the start of the current statement or declaration, or to
529 the previous one if already at the beginning of one. Only
530 statements/declarations on the same level are considered, i.e. don't
531 move into or out of sexps (not even normal expression parentheses).
532
533 If point is already at the earliest statement within braces or parens,
534 this function doesn't move back into any whitespace preceding it; it
535 returns 'same in this case.
536
537 Stop at statement continuation tokens like \"else\", \"catch\",
538 \"finally\" and the \"while\" in \"do ... while\" if the start point
539 is within the continuation. If starting at such a token, move to the
540 corresponding statement start. If at the beginning of a statement,
541 move to the closest containing statement if there is any. This might
542 also stop at a continuation clause.
543
544 Labels are treated as part of the following statements if
545 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
546 statement start keyword.) Otherwise, each label is treated as a
547 separate statement.
548
549 Macros are ignored \(i.e. skipped over) unless point is within one, in
550 which case the content of the macro is treated as normal code. Aside
551 from any normal statement starts found in it, stop at the first token
552 of the content in the macro, i.e. the expression of an \"#if\" or the
553 start of the definition in a \"#define\". Also stop at start of
554 macros before leaving them.
555
556 Return:
557 'label if stopped at a label or \"case...:\" or \"default:\";
558 'same if stopped at the beginning of the current statement;
559 'up if stepped to a containing statement;
560 'previous if stepped to a preceding statement;
561 'beginning if stepped from a statement continuation clause to
562 its start clause; or
563 'macro if stepped to a macro start.
564 Note that 'same and not 'label is returned if stopped at the same
565 label without crossing the colon character.
566
567 LIM may be given to limit the search. If the search hits the limit,
568 point will be left at the closest following token, or at the start
569 position if that is less ('same is returned in this case).
570
571 NOERROR turns off error logging to `c-parsing-error'.
572
573 Normally only ';' and virtual semicolons are considered to delimit
574 statements, but if COMMA-DELIM is non-nil then ',' is treated
575 as a delimiter too.
576
577 Note that this function might do hidden buffer changes. See the
578 comment at the start of cc-engine.el for more info."
579
580 ;; The bulk of this function is a pushdown automaton that looks at statement
581 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
582 ;; purpose is to keep track of nested statements, ensuring that such
583 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
584 ;; does with nested braces/brackets/parentheses).
585 ;;
586 ;; Note: The position of a boundary is the following token.
587 ;;
588 ;; Beginning with the current token (the one following point), move back one
589 ;; sexp at a time (where a sexp is, more or less, either a token or the
590 ;; entire contents of a brace/bracket/paren pair). Each time a statement
591 ;; boundary is crossed or a "while"-like token is found, update the state of
592 ;; the PDA. Stop at the beginning of a statement when the stack (holding
593 ;; nested statement info) is empty and the position has been moved.
594 ;;
595 ;; The following variables constitute the PDA:
596 ;;
597 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
598 ;; scanned back over, 'boundary if we've just gone back over a
599 ;; statement boundary, or nil otherwise.
600 ;; state: takes one of the values (nil else else-boundary while
601 ;; while-boundary catch catch-boundary).
602 ;; nil means "no "while"-like token yet scanned".
603 ;; 'else, for example, means "just gone back over an else".
604 ;; 'else-boundary means "just gone back over a statement boundary
605 ;; immediately after having gone back over an else".
606 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
607 ;; of error reporting information.
608 ;; stack: The stack onto which the PDA pushes its state. Each entry
609 ;; consists of a saved value of state and saved-pos. An entry is
610 ;; pushed when we move back over a "continuation" token (e.g. else)
611 ;; and popped when we encounter the corresponding opening token
612 ;; (e.g. if).
613 ;;
614 ;;
615 ;; The following diagram briefly outlines the PDA.
616 ;;
617 ;; Common state:
618 ;; "else": Push state, goto state `else'.
619 ;; "while": Push state, goto state `while'.
620 ;; "catch" or "finally": Push state, goto state `catch'.
621 ;; boundary: Pop state.
622 ;; other: Do nothing special.
623 ;;
624 ;; State `else':
625 ;; boundary: Goto state `else-boundary'.
626 ;; other: Error, pop state, retry token.
627 ;;
628 ;; State `else-boundary':
629 ;; "if": Pop state.
630 ;; boundary: Error, pop state.
631 ;; other: See common state.
632 ;;
633 ;; State `while':
634 ;; boundary: Save position, goto state `while-boundary'.
635 ;; other: Pop state, retry token.
636 ;;
637 ;; State `while-boundary':
638 ;; "do": Pop state.
639 ;; boundary: Restore position if it's not at start, pop state. [*see below]
640 ;; other: See common state.
641 ;;
642 ;; State `catch':
643 ;; boundary: Goto state `catch-boundary'.
644 ;; other: Error, pop state, retry token.
645 ;;
646 ;; State `catch-boundary':
647 ;; "try": Pop state.
648 ;; "catch": Goto state `catch'.
649 ;; boundary: Error, pop state.
650 ;; other: See common state.
651 ;;
652 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
653 ;; searching for a "do" which would have opened a do-while. If we didn't
654 ;; find it, we discard the analysis done since the "while", go back to this
655 ;; token in the buffer and restart the scanning there, this time WITHOUT
656 ;; pushing the 'while state onto the stack.
657 ;;
658 ;; In addition to the above there is some special handling of labels
659 ;; and macros.
660
661 (let ((case-fold-search nil)
662 (start (point))
663 macro-start
664 (delims (if comma-delim '(?\; ?,) '(?\;)))
665 (c-stmt-delim-chars (if comma-delim
666 c-stmt-delim-chars-with-comma
667 c-stmt-delim-chars))
668 c-in-literal-cache c-maybe-labelp after-case:-pos saved
669 ;; Current position.
670 pos
671 ;; Position of last stmt boundary character (e.g. ;).
672 boundary-pos
673 ;; The position of the last sexp or bound that follows the
674 ;; first found colon, i.e. the start of the nonlabel part of
675 ;; the statement. It's `start' if a colon is found just after
676 ;; the start.
677 after-labels-pos
678 ;; Like `after-labels-pos', but the first such position inside
679 ;; a label, i.e. the start of the last label before the start
680 ;; of the nonlabel part of the statement.
681 last-label-pos
682 ;; The last position where a label is possible provided the
683 ;; statement started there. It's nil as long as no invalid
684 ;; label content has been found (according to
685 ;; `c-nonlabel-token-key'. It's `start' if no valid label
686 ;; content was found in the label. Note that we might still
687 ;; regard it a label if it starts with `c-label-kwds'.
688 label-good-pos
689 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
690 ;; See above.
691 sym
692 ;; Current state in the automaton. See above.
693 state
694 ;; Current saved positions. See above.
695 saved-pos
696 ;; Stack of conses (state . saved-pos).
697 stack
698 ;; Regexp which matches "for", "if", etc.
699 (cond-key (or c-opt-block-stmt-key
700 "\\<\\>")) ; Matches nothing.
701 ;; Return value.
702 (ret 'same)
703 ;; Positions of the last three sexps or bounds we've stopped at.
704 tok ptok pptok)
705
706 (save-restriction
707 (if lim (narrow-to-region lim (point-max)))
708
709 (if (save-excursion
710 (and (c-beginning-of-macro)
711 (/= (point) start)))
712 (setq macro-start (point)))
713
714 ;; Try to skip back over unary operator characters, to register
715 ;; that we've moved.
716 (while (progn
717 (setq pos (point))
718 (c-backward-syntactic-ws)
719 ;; Protect post-++/-- operators just before a virtual semicolon.
720 (and (not (c-at-vsemi-p))
721 (/= (skip-chars-backward "-+!*&~@`#") 0))))
722
723 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
724 ;; done. Later on we ignore the boundaries for statements that don't
725 ;; contain any sexp. The only thing that is affected is that the error
726 ;; checking is a little less strict, and we really don't bother.
727 (if (and (memq (char-before) delims)
728 (progn (forward-char -1)
729 (setq saved (point))
730 (c-backward-syntactic-ws)
731 (or (memq (char-before) delims)
732 (memq (char-before) '(?: nil))
733 (eq (char-syntax (char-before)) ?\()
734 (c-at-vsemi-p))))
735 (setq ret 'previous
736 pos saved)
737
738 ;; Begin at start and not pos to detect macros if we stand
739 ;; directly after the #.
740 (goto-char start)
741 (if (looking-at "\\<\\|\\W")
742 ;; Record this as the first token if not starting inside it.
743 (setq tok start))
744
745 ;; The following while loop goes back one sexp (balanced parens,
746 ;; etc. with contents, or symbol or suchlike) each iteration. This
747 ;; movement is accomplished with a call to scan-sexps approx 130 lines
748 ;; below.
749 (while
750 (catch 'loop ;; Throw nil to break, non-nil to continue.
751 (cond
752 ((save-excursion
753 (and macro-start ; Always NIL for AWK.
754 (progn (skip-chars-backward " \t")
755 (eq (char-before) ?#))
756 (progn (setq saved (1- (point)))
757 (beginning-of-line)
758 (not (eq (char-before (1- (point))) ?\\)))
759 (looking-at c-opt-cpp-start)
760 (progn (skip-chars-forward " \t")
761 (eq (point) saved))))
762 (goto-char saved)
763 (if (and (c-forward-to-cpp-define-body)
764 (progn (c-forward-syntactic-ws start)
765 (< (point) start)))
766 ;; Stop at the first token in the content of the macro.
767 (setq pos (point)
768 ignore-labels t) ; Avoid the label check on exit.
769 (setq pos saved
770 ret 'macro
771 ignore-labels t))
772 (throw 'loop nil))
773
774 ;; Do a round through the automaton if we've just passed a
775 ;; statement boundary or passed a "while"-like token.
776 ((or sym
777 (and (looking-at cond-key)
778 (setq sym (intern (match-string 1)))))
779
780 (when (and (< pos start) (null stack))
781 (throw 'loop nil))
782
783 ;; The PDA state handling.
784 ;;
785 ;; Refer to the description of the PDA in the opening
786 ;; comments. In the following OR form, the first leaf
787 ;; attempts to handles one of the specific actions detailed
788 ;; (e.g., finding token "if" whilst in state `else-boundary').
789 ;; We drop through to the second leaf (which handles common
790 ;; state) if no specific handler is found in the first cond.
791 ;; If a parsing error is detected (e.g. an "else" with no
792 ;; preceding "if"), we throw to the enclosing catch.
793 ;;
794 ;; Note that the (eq state 'else) means
795 ;; "we've just passed an else", NOT "we're looking for an
796 ;; else".
797 (or (cond
798 ((eq state 'else)
799 (if (eq sym 'boundary)
800 (setq state 'else-boundary)
801 (c-bos-report-error)
802 (c-bos-pop-state-and-retry)))
803
804 ((eq state 'else-boundary)
805 (cond ((eq sym 'if)
806 (c-bos-pop-state (setq ret 'beginning)))
807 ((eq sym 'boundary)
808 (c-bos-report-error)
809 (c-bos-pop-state))))
810
811 ((eq state 'while)
812 (if (and (eq sym 'boundary)
813 ;; Since this can cause backtracking we do a
814 ;; little more careful analysis to avoid it:
815 ;; If there's a label in front of the while
816 ;; it can't be part of a do-while.
817 (not after-labels-pos))
818 (progn (c-bos-save-pos)
819 (setq state 'while-boundary))
820 (c-bos-pop-state-and-retry))) ; Can't be a do-while
821
822 ((eq state 'while-boundary)
823 (cond ((eq sym 'do)
824 (c-bos-pop-state (setq ret 'beginning)))
825 ((eq sym 'boundary) ; isn't a do-while
826 (c-bos-restore-pos) ; the position of the while
827 (c-bos-pop-state)))) ; no longer searching for do.
828
829 ((eq state 'catch)
830 (if (eq sym 'boundary)
831 (setq state 'catch-boundary)
832 (c-bos-report-error)
833 (c-bos-pop-state-and-retry)))
834
835 ((eq state 'catch-boundary)
836 (cond
837 ((eq sym 'try)
838 (c-bos-pop-state (setq ret 'beginning)))
839 ((eq sym 'catch)
840 (setq state 'catch))
841 ((eq sym 'boundary)
842 (c-bos-report-error)
843 (c-bos-pop-state)))))
844
845 ;; This is state common. We get here when the previous
846 ;; cond statement found no particular state handler.
847 (cond ((eq sym 'boundary)
848 ;; If we have a boundary at the start
849 ;; position we push a frame to go to the
850 ;; previous statement.
851 (if (>= pos start)
852 (c-bos-push-state)
853 (c-bos-pop-state)))
854 ((eq sym 'else)
855 (c-bos-push-state)
856 (c-bos-save-error-info 'if 'else)
857 (setq state 'else))
858 ((eq sym 'while)
859 ;; Is this a real while, or a do-while?
860 ;; The next `when' triggers unless we are SURE that
861 ;; the `while' is not the tailend of a `do-while'.
862 (when (or (not pptok)
863 (memq (char-after pptok) delims)
864 ;; The following kludge is to prevent
865 ;; infinite recursion when called from
866 ;; c-awk-after-if-for-while-condition-p,
867 ;; or the like.
868 (and (eq (point) start)
869 (c-vsemi-status-unknown-p))
870 (c-at-vsemi-p pptok))
871 ;; Since this can cause backtracking we do a
872 ;; little more careful analysis to avoid it: If
873 ;; the while isn't followed by a (possibly
874 ;; virtual) semicolon it can't be a do-while.
875 (c-bos-push-state)
876 (setq state 'while)))
877 ((memq sym '(catch finally))
878 (c-bos-push-state)
879 (c-bos-save-error-info 'try sym)
880 (setq state 'catch))))
881
882 (when c-maybe-labelp
883 ;; We're either past a statement boundary or at the
884 ;; start of a statement, so throw away any label data
885 ;; for the previous one.
886 (setq after-labels-pos nil
887 last-label-pos nil
888 c-maybe-labelp nil))))
889
890 ;; Step to the previous sexp, but not if we crossed a
891 ;; boundary, since that doesn't consume an sexp.
892 (if (eq sym 'boundary)
893 (setq ret 'previous)
894
895 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
896 ;; BACKWARDS THROUGH THE SOURCE.
897
898 ;; This is typically fast with the caching done by
899 ;; c-(backward|forward)-sws.
900 (c-backward-syntactic-ws)
901
902 (let ((before-sws-pos (point))
903 ;; Set as long as we have to continue jumping by sexps.
904 ;; It's the position to use as end in the next round.
905 sexp-loop-continue-pos
906 ;; The end position of the area to search for statement
907 ;; barriers in this round.
908 (sexp-loop-end-pos pos))
909
910 ;; The following while goes back one sexp per iteration.
911 (while
912 (progn
913 (unless (c-safe (c-backward-sexp) t)
914 ;; Give up if we hit an unbalanced block. Since the
915 ;; stack won't be empty the code below will report a
916 ;; suitable error.
917 (throw 'loop nil))
918
919 ;; Check if the sexp movement crossed a statement or
920 ;; declaration boundary. But first modify the point
921 ;; so that `c-crosses-statement-barrier-p' only looks
922 ;; at the non-sexp chars following the sexp.
923 (save-excursion
924 (when (setq
925 boundary-pos
926 (cond
927 ((if macro-start
928 nil
929 (save-excursion
930 (when (c-beginning-of-macro)
931 ;; Set continuation position in case
932 ;; `c-crosses-statement-barrier-p'
933 ;; doesn't detect anything below.
934 (setq sexp-loop-continue-pos (point)))))
935 ;; If the sexp movement took us into a
936 ;; macro then there were only some non-sexp
937 ;; chars after it. Skip out of the macro
938 ;; to analyze them but not the non-sexp
939 ;; chars that might be inside the macro.
940 (c-end-of-macro)
941 (c-crosses-statement-barrier-p
942 (point) sexp-loop-end-pos))
943
944 ((and
945 (eq (char-after) ?{)
946 (not (c-looking-at-inexpr-block lim nil t)))
947 ;; Passed a block sexp. That's a boundary
948 ;; alright.
949 (point))
950
951 ((looking-at "\\s\(")
952 ;; Passed some other paren. Only analyze
953 ;; the non-sexp chars after it.
954 (goto-char (1+ (c-down-list-backward
955 before-sws-pos)))
956 ;; We're at a valid token start position
957 ;; (outside the `save-excursion') if
958 ;; `c-crosses-statement-barrier-p' failed.
959 (c-crosses-statement-barrier-p
960 (point) sexp-loop-end-pos))
961
962 (t
963 ;; Passed a symbol sexp or line
964 ;; continuation. It doesn't matter that
965 ;; it's included in the analyzed region.
966 (if (c-crosses-statement-barrier-p
967 (point) sexp-loop-end-pos)
968 t
969 ;; If it was a line continuation then we
970 ;; have to continue looping.
971 (if (looking-at "\\\\$")
972 (setq sexp-loop-continue-pos (point)))
973 nil))))
974
975 (setq pptok ptok
976 ptok tok
977 tok boundary-pos
978 sym 'boundary)
979 ;; Like a C "continue". Analyze the next sexp.
980 (throw 'loop t)))
981
982 sexp-loop-continue-pos) ; End of "go back a sexp" loop condition.
983 (goto-char sexp-loop-continue-pos)
984 (setq sexp-loop-end-pos sexp-loop-continue-pos
985 sexp-loop-continue-pos nil))))
986
987 ;; ObjC method def?
988 (when (and c-opt-method-key
989 (setq saved (c-in-method-def-p)))
990 (setq pos saved
991 ignore-labels t) ; Avoid the label check on exit.
992 (throw 'loop nil))
993
994 ;; Handle labels.
995 (unless (eq ignore-labels t)
996 (when (numberp c-maybe-labelp)
997 ;; `c-crosses-statement-barrier-p' has found a colon, so we
998 ;; might be in a label now. Have we got a real label
999 ;; (including a case label) or something like C++'s "public:"?
1000 ;; A case label might use an expression rather than a token.
1001 (setq after-case:-pos (or tok start))
1002 (if (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1003 (setq c-maybe-labelp nil)
1004 (if after-labels-pos ; Have we already encountered a label?
1005 (if (not last-label-pos)
1006 (setq last-label-pos (or tok start)))
1007 (setq after-labels-pos (or tok start)))
1008 (setq c-maybe-labelp t
1009 label-good-pos nil))) ; bogus "label"
1010
1011 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1012 ; been found.
1013 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1014 ;; We're in a potential label and it's the first
1015 ;; time we've found something that isn't allowed in
1016 ;; one.
1017 (setq label-good-pos (or tok start))))
1018
1019 ;; We've moved back by a sexp, so update the token positions.
1020 (setq sym nil
1021 pptok ptok
1022 ptok tok
1023 tok (point)
1024 pos tok))) ; Not nil (for the while loop).
1025
1026 ;; If the stack isn't empty there might be errors to report.
1027 (while stack
1028 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1029 (c-bos-report-error))
1030 (setq saved-pos (cdr (car stack))
1031 stack (cdr stack)))
1032
1033 (when (and (eq ret 'same)
1034 (not (memq sym '(boundary ignore nil))))
1035 ;; Need to investigate closer whether we've crossed
1036 ;; between a substatement and its containing statement.
1037 (if (setq saved (if (looking-at c-block-stmt-1-key)
1038 ptok
1039 pptok))
1040 (cond ((> start saved) (setq pos saved))
1041 ((= start saved) (setq ret 'up)))))
1042
1043 (when (and (not ignore-labels)
1044 (eq c-maybe-labelp t)
1045 (not (eq ret 'beginning))
1046 after-labels-pos
1047 (or (not label-good-pos)
1048 (<= label-good-pos pos)
1049 (progn
1050 (goto-char (if (and last-label-pos
1051 (< last-label-pos start))
1052 last-label-pos
1053 pos))
1054 (looking-at c-label-kwds-regexp))))
1055 ;; We're in a label. Maybe we should step to the statement
1056 ;; after it.
1057 (if (< after-labels-pos start)
1058 (setq pos after-labels-pos)
1059 (setq ret 'label)
1060 (if (and last-label-pos (< last-label-pos start))
1061 ;; Might have jumped over several labels. Go to the last one.
1062 (setq pos last-label-pos)))))
1063
1064 ;; Have we got "case <expression>:"?
1065 (goto-char pos)
1066 (when (and after-case:-pos
1067 (not (eq ret 'beginning))
1068 (looking-at c-case-kwds-regexp))
1069 (if (< after-case:-pos start)
1070 (setq pos after-case:-pos))
1071 (if (eq ret 'same)
1072 (setq ret 'label)))
1073
1074 ;; Skip over the unary operators that can start the statement.
1075 (while (progn
1076 (c-backward-syntactic-ws)
1077 ;; protect AWK post-inc/decrement operators, etc.
1078 (and (not (c-at-vsemi-p (point)))
1079 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1080 (setq pos (point)))
1081 (goto-char pos)
1082 ret)))
1083
1084 (defun c-crosses-statement-barrier-p (from to)
1085 "Return non-nil if buffer positions FROM to TO cross one or more
1086 statement or declaration boundaries. The returned value is actually
1087 the position of the earliest boundary char. FROM must not be within
1088 a string or comment.
1089
1090 The variable `c-maybe-labelp' is set to the position of the first `:' that
1091 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1092 single `?' is found, then `c-maybe-labelp' is cleared.
1093
1094 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1095 regarded as having a \"virtual semicolon\" immediately after the last token on
1096 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1097
1098 Note that this function might do hidden buffer changes. See the
1099 comment at the start of cc-engine.el for more info."
1100 (let ((skip-chars c-stmt-delim-chars)
1101 lit-range)
1102 (save-excursion
1103 (catch 'done
1104 (goto-char from)
1105 (while (progn (skip-chars-forward skip-chars to)
1106 (< (point) to))
1107 (cond
1108 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1109 (goto-char (cdr lit-range)))
1110 ((eq (char-after) ?:)
1111 (forward-char)
1112 (if (and (eq (char-after) ?:)
1113 (< (point) to))
1114 ;; Ignore scope operators.
1115 (forward-char)
1116 (setq c-maybe-labelp (1- (point)))))
1117 ((eq (char-after) ??)
1118 ;; A question mark. Can't be a label, so stop
1119 ;; looking for more : and ?.
1120 (setq c-maybe-labelp nil
1121 skip-chars (substring c-stmt-delim-chars 0 -2)))
1122 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1123 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1124 (backward-char))
1125 (skip-chars-backward " \t" from)
1126 (if (c-at-vsemi-p)
1127 (throw 'done (point))
1128 (forward-line)))
1129 (t (throw 'done (point)))))
1130 ;; In trailing space after an as yet undetected virtual semicolon?
1131 (c-backward-syntactic-ws from)
1132 (if (and (< (point) to)
1133 (c-at-vsemi-p))
1134 (point)
1135 nil)))))
1136
1137 (defun c-at-statement-start-p ()
1138 "Return non-nil if the point is at the first token in a statement
1139 or somewhere in the syntactic whitespace before it.
1140
1141 A \"statement\" here is not restricted to those inside code blocks.
1142 Any kind of declaration-like construct that occur outside function
1143 bodies is also considered a \"statement\".
1144
1145 Note that this function might do hidden buffer changes. See the
1146 comment at the start of cc-engine.el for more info."
1147
1148 (save-excursion
1149 (let ((end (point))
1150 c-maybe-labelp)
1151 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1152 (or (bobp)
1153 (eq (char-before) ?})
1154 (and (eq (char-before) ?{)
1155 (not (and c-special-brace-lists
1156 (progn (backward-char)
1157 (c-looking-at-special-brace-list)))))
1158 (c-crosses-statement-barrier-p (point) end)))))
1159
1160 (defun c-at-expression-start-p ()
1161 "Return non-nil if the point is at the first token in an expression or
1162 statement, or somewhere in the syntactic whitespace before it.
1163
1164 An \"expression\" here is a bit different from the normal language
1165 grammar sense: It's any sequence of expression tokens except commas,
1166 unless they are enclosed inside parentheses of some kind. Also, an
1167 expression never continues past an enclosing parenthesis, but it might
1168 contain parenthesis pairs of any sort except braces.
1169
1170 Since expressions never cross statement boundaries, this function also
1171 recognizes statement beginnings, just like `c-at-statement-start-p'.
1172
1173 Note that this function might do hidden buffer changes. See the
1174 comment at the start of cc-engine.el for more info."
1175
1176 (save-excursion
1177 (let ((end (point))
1178 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1179 c-maybe-labelp)
1180 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1181 (or (bobp)
1182 (memq (char-before) '(?{ ?}))
1183 (save-excursion (backward-char)
1184 (looking-at "\\s("))
1185 (c-crosses-statement-barrier-p (point) end)))))
1186
1187 \f
1188 ;; A set of functions that covers various idiosyncrasies in
1189 ;; implementations of `forward-comment'.
1190
1191 ;; Note: Some emacsen considers incorrectly that any line comment
1192 ;; ending with a backslash continues to the next line. I can't think
1193 ;; of any way to work around that in a reliable way without changing
1194 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1195 ;; changing the syntax for backslash doesn't work since we must treat
1196 ;; escapes in string literals correctly.)
1197
1198 (defun c-forward-single-comment ()
1199 "Move forward past whitespace and the closest following comment, if any.
1200 Return t if a comment was found, nil otherwise. In either case, the
1201 point is moved past the following whitespace. Line continuations,
1202 i.e. a backslashes followed by line breaks, are treated as whitespace.
1203 The line breaks that end line comments are considered to be the
1204 comment enders, so the point will be put on the beginning of the next
1205 line if it moved past a line comment.
1206
1207 This function does not do any hidden buffer changes."
1208
1209 (let ((start (point)))
1210 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1211 (goto-char (match-end 0)))
1212
1213 (when (forward-comment 1)
1214 (if (eobp)
1215 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1216 ;; forwards at eob.
1217 nil
1218
1219 ;; Emacs includes the ending newline in a b-style (c++)
1220 ;; comment, but XEmacs doesn't. We depend on the Emacs
1221 ;; behavior (which also is symmetric).
1222 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1223 (condition-case nil (forward-char 1)))
1224
1225 t))))
1226
1227 (defsubst c-forward-comments ()
1228 "Move forward past all following whitespace and comments.
1229 Line continuations, i.e. a backslashes followed by line breaks, are
1230 treated as whitespace.
1231
1232 Note that this function might do hidden buffer changes. See the
1233 comment at the start of cc-engine.el for more info."
1234
1235 (while (or
1236 ;; If forward-comment in at least XEmacs 21 is given a large
1237 ;; positive value, it'll loop all the way through if it hits
1238 ;; eob.
1239 (and (forward-comment 5)
1240 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1241 ;; forwards at eob.
1242 (not (eobp)))
1243
1244 (when (looking-at "\\\\[\n\r]")
1245 (forward-char 2)
1246 t))))
1247
1248 (defun c-backward-single-comment ()
1249 "Move backward past whitespace and the closest preceding comment, if any.
1250 Return t if a comment was found, nil otherwise. In either case, the
1251 point is moved past the preceding whitespace. Line continuations,
1252 i.e. a backslashes followed by line breaks, are treated as whitespace.
1253 The line breaks that end line comments are considered to be the
1254 comment enders, so the point cannot be at the end of the same line to
1255 move over a line comment.
1256
1257 This function does not do any hidden buffer changes."
1258
1259 (let ((start (point)))
1260 ;; When we got newline terminated comments, forward-comment in all
1261 ;; supported emacsen so far will stop at eol of each line not
1262 ;; ending with a comment when moving backwards. This corrects for
1263 ;; that, and at the same time handles line continuations.
1264 (while (progn
1265 (skip-chars-backward " \t\n\r\f\v")
1266 (and (looking-at "[\n\r]")
1267 (eq (char-before) ?\\)))
1268 (backward-char))
1269
1270 (if (bobp)
1271 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1272 ;; backwards at bob.
1273 nil
1274
1275 ;; Leave point after the closest following newline if we've
1276 ;; backed up over any above, since forward-comment won't move
1277 ;; backward over a line comment if point is at the end of the
1278 ;; same line.
1279 (re-search-forward "\\=\\s *[\n\r]" start t)
1280
1281 (if (if (forward-comment -1)
1282 (if (eolp)
1283 ;; If forward-comment above succeeded and we're at eol
1284 ;; then the newline we moved over above didn't end a
1285 ;; line comment, so we give it another go.
1286 (forward-comment -1)
1287 t))
1288
1289 ;; Emacs <= 20 and XEmacs move back over the closer of a
1290 ;; block comment that lacks an opener.
1291 (if (looking-at "\\*/")
1292 (progn (forward-char 2) nil)
1293 t)))))
1294
1295 (defsubst c-backward-comments ()
1296 "Move backward past all preceding whitespace and comments.
1297 Line continuations, i.e. a backslashes followed by line breaks, are
1298 treated as whitespace. The line breaks that end line comments are
1299 considered to be the comment enders, so the point cannot be at the end
1300 of the same line to move over a line comment. Unlike
1301 c-backward-syntactic-ws, this function doesn't move back over
1302 preprocessor directives.
1303
1304 Note that this function might do hidden buffer changes. See the
1305 comment at the start of cc-engine.el for more info."
1306
1307 (let ((start (point)))
1308 (while (and
1309 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1310 ;; return t when moving backwards at bob.
1311 (not (bobp))
1312
1313 (if (forward-comment -1)
1314 (if (looking-at "\\*/")
1315 ;; Emacs <= 20 and XEmacs move back over the
1316 ;; closer of a block comment that lacks an opener.
1317 (progn (forward-char 2) nil)
1318 t)
1319
1320 ;; XEmacs treats line continuations as whitespace but
1321 ;; only in the backward direction, which seems a bit
1322 ;; odd. Anyway, this is necessary for Emacs.
1323 (when (and (looking-at "[\n\r]")
1324 (eq (char-before) ?\\)
1325 (< (point) start))
1326 (backward-char)
1327 t))))))
1328
1329 \f
1330 ;; Tools for skipping over syntactic whitespace.
1331
1332 ;; The following functions use text properties to cache searches over
1333 ;; large regions of syntactic whitespace. It works as follows:
1334 ;;
1335 ;; o If a syntactic whitespace region contains anything but simple
1336 ;; whitespace (i.e. space, tab and line breaks), the text property
1337 ;; `c-in-sws' is put over it. At places where we have stopped
1338 ;; within that region there's also a `c-is-sws' text property.
1339 ;; That since there typically are nested whitespace inside that
1340 ;; must be handled separately, e.g. whitespace inside a comment or
1341 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1342 ;; to jump to another point with that property within the same
1343 ;; `c-in-sws' region. It can be likened to a ladder where
1344 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1345 ;;
1346 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1347 ;; a "rung position" and also maybe on the first following char.
1348 ;; As many characters as can be conveniently found in this range
1349 ;; are marked, but no assumption can be made that the whole range
1350 ;; is marked (it could be clobbered by later changes, for
1351 ;; instance).
1352 ;;
1353 ;; Note that some part of the beginning of a sequence of simple
1354 ;; whitespace might be part of the end of a preceding line comment
1355 ;; or cpp directive and must not be considered part of the "rung".
1356 ;; Such whitespace is some amount of horizontal whitespace followed
1357 ;; by a newline. In the case of cpp directives it could also be
1358 ;; two newlines with horizontal whitespace between them.
1359 ;;
1360 ;; The reason to include the first following char is to cope with
1361 ;; "rung positions" that doesn't have any ordinary whitespace. If
1362 ;; `c-is-sws' is put on a token character it does not have
1363 ;; `c-in-sws' set simultaneously. That's the only case when that
1364 ;; can occur, and the reason for not extending the `c-in-sws'
1365 ;; region to cover it is that the `c-in-sws' region could then be
1366 ;; accidentally merged with a following one if the token is only
1367 ;; one character long.
1368 ;;
1369 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1370 ;; removed in the changed region. If the change was inside
1371 ;; syntactic whitespace that means that the "ladder" is broken, but
1372 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1373 ;; parts on either side and use an ordinary search only to "repair"
1374 ;; the gap.
1375 ;;
1376 ;; Special care needs to be taken if a region is removed: If there
1377 ;; are `c-in-sws' on both sides of it which do not connect inside
1378 ;; the region then they can't be joined. If e.g. a marked macro is
1379 ;; broken, syntactic whitespace inside the new text might be
1380 ;; marked. If those marks would become connected with the old
1381 ;; `c-in-sws' range around the macro then we could get a ladder
1382 ;; with one end outside the macro and the other at some whitespace
1383 ;; within it.
1384 ;;
1385 ;; The main motivation for this system is to increase the speed in
1386 ;; skipping over the large whitespace regions that can occur at the
1387 ;; top level in e.g. header files that contain a lot of comments and
1388 ;; cpp directives. For small comments inside code it's probably
1389 ;; slower than using `forward-comment' straightforwardly, but speed is
1390 ;; not a significant factor there anyway.
1391
1392 ; (defface c-debug-is-sws-face
1393 ; '((t (:background "GreenYellow")))
1394 ; "Debug face to mark the `c-is-sws' property.")
1395 ; (defface c-debug-in-sws-face
1396 ; '((t (:underline t)))
1397 ; "Debug face to mark the `c-in-sws' property.")
1398
1399 ; (defun c-debug-put-sws-faces ()
1400 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1401 ; ;; properties in the buffer.
1402 ; (interactive)
1403 ; (save-excursion
1404 ; (c-save-buffer-state (in-face)
1405 ; (goto-char (point-min))
1406 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1407 ; (point)))
1408 ; (while (progn
1409 ; (goto-char (next-single-property-change
1410 ; (point) 'c-is-sws nil (point-max)))
1411 ; (if in-face
1412 ; (progn
1413 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1414 ; (setq in-face nil))
1415 ; (setq in-face (point)))
1416 ; (not (eobp))))
1417 ; (goto-char (point-min))
1418 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1419 ; (point)))
1420 ; (while (progn
1421 ; (goto-char (next-single-property-change
1422 ; (point) 'c-in-sws nil (point-max)))
1423 ; (if in-face
1424 ; (progn
1425 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1426 ; (setq in-face nil))
1427 ; (setq in-face (point)))
1428 ; (not (eobp)))))))
1429
1430 (defmacro c-debug-sws-msg (&rest args)
1431 ;;`(message ,@args)
1432 )
1433
1434 (defmacro c-put-is-sws (beg end)
1435 ;; This macro does a hidden buffer change.
1436 `(let ((beg ,beg) (end ,end))
1437 (put-text-property beg end 'c-is-sws t)
1438 ,@(when (facep 'c-debug-is-sws-face)
1439 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1440
1441 (defmacro c-put-in-sws (beg end)
1442 ;; This macro does a hidden buffer change.
1443 `(let ((beg ,beg) (end ,end))
1444 (put-text-property beg end 'c-in-sws t)
1445 ,@(when (facep 'c-debug-is-sws-face)
1446 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1447
1448 (defmacro c-remove-is-sws (beg end)
1449 ;; This macro does a hidden buffer change.
1450 `(let ((beg ,beg) (end ,end))
1451 (remove-text-properties beg end '(c-is-sws nil))
1452 ,@(when (facep 'c-debug-is-sws-face)
1453 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1454
1455 (defmacro c-remove-in-sws (beg end)
1456 ;; This macro does a hidden buffer change.
1457 `(let ((beg ,beg) (end ,end))
1458 (remove-text-properties beg end '(c-in-sws nil))
1459 ,@(when (facep 'c-debug-is-sws-face)
1460 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1461
1462 (defmacro c-remove-is-and-in-sws (beg end)
1463 ;; This macro does a hidden buffer change.
1464 `(let ((beg ,beg) (end ,end))
1465 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1466 ,@(when (facep 'c-debug-is-sws-face)
1467 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1468 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1469
1470 (defsubst c-invalidate-sws-region-after (beg end)
1471 ;; Called from `after-change-functions'. Note that if
1472 ;; `c-forward-sws' or `c-backward-sws' are used outside
1473 ;; `c-save-buffer-state' or similar then this will remove the cache
1474 ;; properties right after they're added.
1475 ;;
1476 ;; This function does hidden buffer changes.
1477
1478 (save-excursion
1479 ;; Adjust the end to remove the properties in any following simple
1480 ;; ws up to and including the next line break, if there is any
1481 ;; after the changed region. This is necessary e.g. when a rung
1482 ;; marked empty line is converted to a line comment by inserting
1483 ;; "//" before the line break. In that case the line break would
1484 ;; keep the rung mark which could make a later `c-backward-sws'
1485 ;; move into the line comment instead of over it.
1486 (goto-char end)
1487 (skip-chars-forward " \t\f\v")
1488 (when (and (eolp) (not (eobp)))
1489 (setq end (1+ (point)))))
1490
1491 (when (and (= beg end)
1492 (get-text-property beg 'c-in-sws)
1493 (> beg (point-min))
1494 (get-text-property (1- beg) 'c-in-sws))
1495 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1496 ;; safe to keep a range that was continuous before the change. E.g:
1497 ;;
1498 ;; #define foo
1499 ;; \
1500 ;; bar
1501 ;;
1502 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1503 ;; after "foo" is removed then "bar" will become part of the cpp
1504 ;; directive instead of a syntactically relevant token. In that
1505 ;; case there's no longer syntactic ws from "#" to "b".
1506 (setq beg (1- beg)))
1507
1508 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1509 (c-remove-is-and-in-sws beg end))
1510
1511 (defun c-forward-sws ()
1512 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1513 ;;
1514 ;; This function might do hidden buffer changes.
1515
1516 (let (;; `rung-pos' is set to a position as early as possible in the
1517 ;; unmarked part of the simple ws region.
1518 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1519 rung-is-marked next-rung-is-marked simple-ws-end
1520 ;; `safe-start' is set when it's safe to cache the start position.
1521 ;; It's not set if we've initially skipped over comments and line
1522 ;; continuations since we might have gone out through the end of a
1523 ;; macro then. This provision makes `c-forward-sws' not populate the
1524 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1525 ;; more common.
1526 safe-start)
1527
1528 ;; Skip simple ws and do a quick check on the following character to see
1529 ;; if it's anything that can't start syntactic ws, so we can bail out
1530 ;; early in the majority of cases when there just are a few ws chars.
1531 (skip-chars-forward " \t\n\r\f\v")
1532 (when (looking-at c-syntactic-ws-start)
1533
1534 (setq rung-end-pos (min (1+ (point)) (point-max)))
1535 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1536 'c-is-sws t))
1537 ;; Find the last rung position to avoid setting properties in all
1538 ;; the cases when the marked rung is complete.
1539 ;; (`next-single-property-change' is certain to move at least one
1540 ;; step forward.)
1541 (setq rung-pos (1- (next-single-property-change
1542 rung-is-marked 'c-is-sws nil rung-end-pos)))
1543 ;; Got no marked rung here. Since the simple ws might have started
1544 ;; inside a line comment or cpp directive we must set `rung-pos' as
1545 ;; high as possible.
1546 (setq rung-pos (point)))
1547
1548 (while
1549 (progn
1550 (while
1551 (when (and rung-is-marked
1552 (get-text-property (point) 'c-in-sws))
1553
1554 ;; The following search is the main reason that `c-in-sws'
1555 ;; and `c-is-sws' aren't combined to one property.
1556 (goto-char (next-single-property-change
1557 (point) 'c-in-sws nil (point-max)))
1558 (unless (get-text-property (point) 'c-is-sws)
1559 ;; If the `c-in-sws' region extended past the last
1560 ;; `c-is-sws' char we have to go back a bit.
1561 (or (get-text-property (1- (point)) 'c-is-sws)
1562 (goto-char (previous-single-property-change
1563 (point) 'c-is-sws)))
1564 (backward-char))
1565
1566 (c-debug-sws-msg
1567 "c-forward-sws cached move %s -> %s (max %s)"
1568 rung-pos (point) (point-max))
1569
1570 (setq rung-pos (point))
1571 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1572 (not (eobp))))
1573
1574 ;; We'll loop here if there is simple ws after the last rung.
1575 ;; That means that there's been some change in it and it's
1576 ;; possible that we've stepped into another ladder, so extend
1577 ;; the previous one to join with it if there is one, and try to
1578 ;; use the cache again.
1579 (c-debug-sws-msg
1580 "c-forward-sws extending rung with [%s..%s] (max %s)"
1581 (1+ rung-pos) (1+ (point)) (point-max))
1582 (unless (get-text-property (point) 'c-is-sws)
1583 ;; Remove any `c-in-sws' property from the last char of
1584 ;; the rung before we mark it with `c-is-sws', so that we
1585 ;; won't connect with the remains of a broken "ladder".
1586 (c-remove-in-sws (point) (1+ (point))))
1587 (c-put-is-sws (1+ rung-pos)
1588 (1+ (point)))
1589 (c-put-in-sws rung-pos
1590 (setq rung-pos (point)
1591 last-put-in-sws-pos rung-pos)))
1592
1593 (setq simple-ws-end (point))
1594 (c-forward-comments)
1595
1596 (cond
1597 ((/= (point) simple-ws-end)
1598 ;; Skipped over comments. Don't cache at eob in case the buffer
1599 ;; is narrowed.
1600 (not (eobp)))
1601
1602 ((save-excursion
1603 (and c-opt-cpp-prefix
1604 (looking-at c-opt-cpp-start)
1605 (progn (skip-chars-backward " \t")
1606 (bolp))
1607 (or (bobp)
1608 (progn (backward-char)
1609 (not (eq (char-before) ?\\))))))
1610 ;; Skip a preprocessor directive.
1611 (end-of-line)
1612 (while (and (eq (char-before) ?\\)
1613 (= (forward-line 1) 0))
1614 (end-of-line))
1615 (forward-line 1)
1616 (setq safe-start t)
1617 ;; Don't cache at eob in case the buffer is narrowed.
1618 (not (eobp)))))
1619
1620 ;; We've searched over a piece of non-white syntactic ws. See if this
1621 ;; can be cached.
1622 (setq next-rung-pos (point))
1623 (skip-chars-forward " \t\n\r\f\v")
1624 (setq rung-end-pos (min (1+ (point)) (point-max)))
1625
1626 (if (or
1627 ;; Cache if we haven't skipped comments only, and if we started
1628 ;; either from a marked rung or from a completely uncached
1629 ;; position.
1630 (and safe-start
1631 (or rung-is-marked
1632 (not (get-text-property simple-ws-end 'c-in-sws))))
1633
1634 ;; See if there's a marked rung in the encountered simple ws. If
1635 ;; so then we can cache, unless `safe-start' is nil. Even then
1636 ;; we need to do this to check if the cache can be used for the
1637 ;; next step.
1638 (and (setq next-rung-is-marked
1639 (text-property-any next-rung-pos rung-end-pos
1640 'c-is-sws t))
1641 safe-start))
1642
1643 (progn
1644 (c-debug-sws-msg
1645 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1646 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1647 (point-max))
1648
1649 ;; Remove the properties for any nested ws that might be cached.
1650 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1651 ;; anyway.
1652 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1653 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1654 (c-put-is-sws rung-pos
1655 (1+ simple-ws-end))
1656 (setq rung-is-marked t))
1657 (c-put-in-sws rung-pos
1658 (setq rung-pos (point)
1659 last-put-in-sws-pos rung-pos))
1660 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1661 ;; Remove any `c-in-sws' property from the last char of
1662 ;; the rung before we mark it with `c-is-sws', so that we
1663 ;; won't connect with the remains of a broken "ladder".
1664 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1665 (c-put-is-sws next-rung-pos
1666 rung-end-pos))
1667
1668 (c-debug-sws-msg
1669 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1670 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1671 (point-max))
1672
1673 ;; Set `rung-pos' for the next rung. It's the same thing here as
1674 ;; initially, except that the rung position is set as early as
1675 ;; possible since we can't be in the ending ws of a line comment or
1676 ;; cpp directive now.
1677 (if (setq rung-is-marked next-rung-is-marked)
1678 (setq rung-pos (1- (next-single-property-change
1679 rung-is-marked 'c-is-sws nil rung-end-pos)))
1680 (setq rung-pos next-rung-pos))
1681 (setq safe-start t)))
1682
1683 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1684 ;; another one after the point (which might occur when editing inside a
1685 ;; comment or macro).
1686 (when (eq last-put-in-sws-pos (point))
1687 (cond ((< last-put-in-sws-pos (point-max))
1688 (c-debug-sws-msg
1689 "c-forward-sws clearing at %s for cache separation"
1690 last-put-in-sws-pos)
1691 (c-remove-in-sws last-put-in-sws-pos
1692 (1+ last-put-in-sws-pos)))
1693 (t
1694 ;; If at eob we have to clear the last character before the end
1695 ;; instead since the buffer might be narrowed and there might
1696 ;; be a `c-in-sws' after (point-max). In this case it's
1697 ;; necessary to clear both properties.
1698 (c-debug-sws-msg
1699 "c-forward-sws clearing thoroughly at %s for cache separation"
1700 (1- last-put-in-sws-pos))
1701 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1702 last-put-in-sws-pos))))
1703 )))
1704
1705 (defun c-backward-sws ()
1706 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1707 ;;
1708 ;; This function might do hidden buffer changes.
1709
1710 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1711 ;; part of the simple ws region.
1712 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1713 rung-is-marked simple-ws-beg cmt-skip-pos)
1714
1715 ;; Skip simple horizontal ws and do a quick check on the preceding
1716 ;; character to see if it's anying that can't end syntactic ws, so we can
1717 ;; bail out early in the majority of cases when there just are a few ws
1718 ;; chars. Newlines are complicated in the backward direction, so we can't
1719 ;; skip over them.
1720 (skip-chars-backward " \t\f")
1721 (when (and (not (bobp))
1722 (save-excursion
1723 (backward-char)
1724 (looking-at c-syntactic-ws-end)))
1725
1726 ;; Try to find a rung position in the simple ws preceding point, so that
1727 ;; we can get a cache hit even if the last bit of the simple ws has
1728 ;; changed recently.
1729 (setq simple-ws-beg (point))
1730 (skip-chars-backward " \t\n\r\f\v")
1731 (if (setq rung-is-marked (text-property-any
1732 (point) (min (1+ rung-pos) (point-max))
1733 'c-is-sws t))
1734 ;; `rung-pos' will be the earliest marked position, which means that
1735 ;; there might be later unmarked parts in the simple ws region.
1736 ;; It's not worth the effort to fix that; the last part of the
1737 ;; simple ws is also typically edited often, so it could be wasted.
1738 (goto-char (setq rung-pos rung-is-marked))
1739 (goto-char simple-ws-beg))
1740
1741 (while
1742 (progn
1743 (while
1744 (when (and rung-is-marked
1745 (not (bobp))
1746 (get-text-property (1- (point)) 'c-in-sws))
1747
1748 ;; The following search is the main reason that `c-in-sws'
1749 ;; and `c-is-sws' aren't combined to one property.
1750 (goto-char (previous-single-property-change
1751 (point) 'c-in-sws nil (point-min)))
1752 (unless (get-text-property (point) 'c-is-sws)
1753 ;; If the `c-in-sws' region extended past the first
1754 ;; `c-is-sws' char we have to go forward a bit.
1755 (goto-char (next-single-property-change
1756 (point) 'c-is-sws)))
1757
1758 (c-debug-sws-msg
1759 "c-backward-sws cached move %s <- %s (min %s)"
1760 (point) rung-pos (point-min))
1761
1762 (setq rung-pos (point))
1763 (if (and (< (min (skip-chars-backward " \t\f\v")
1764 (progn
1765 (setq simple-ws-beg (point))
1766 (skip-chars-backward " \t\n\r\f\v")))
1767 0)
1768 (setq rung-is-marked
1769 (text-property-any (point) rung-pos
1770 'c-is-sws t)))
1771 t
1772 (goto-char simple-ws-beg)
1773 nil))
1774
1775 ;; We'll loop here if there is simple ws before the first rung.
1776 ;; That means that there's been some change in it and it's
1777 ;; possible that we've stepped into another ladder, so extend
1778 ;; the previous one to join with it if there is one, and try to
1779 ;; use the cache again.
1780 (c-debug-sws-msg
1781 "c-backward-sws extending rung with [%s..%s] (min %s)"
1782 rung-is-marked rung-pos (point-min))
1783 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1784 ;; Remove any `c-in-sws' property from the last char of
1785 ;; the rung before we mark it with `c-is-sws', so that we
1786 ;; won't connect with the remains of a broken "ladder".
1787 (c-remove-in-sws (1- rung-pos) rung-pos))
1788 (c-put-is-sws rung-is-marked
1789 rung-pos)
1790 (c-put-in-sws rung-is-marked
1791 (1- rung-pos))
1792 (setq rung-pos rung-is-marked
1793 last-put-in-sws-pos rung-pos))
1794
1795 (c-backward-comments)
1796 (setq cmt-skip-pos (point))
1797
1798 (cond
1799 ((and c-opt-cpp-prefix
1800 (/= cmt-skip-pos simple-ws-beg)
1801 (c-beginning-of-macro))
1802 ;; Inside a cpp directive. See if it should be skipped over.
1803 (let ((cpp-beg (point)))
1804
1805 ;; Move back over all line continuations in the region skipped
1806 ;; over by `c-backward-comments'. If we go past it then we
1807 ;; started inside the cpp directive.
1808 (goto-char simple-ws-beg)
1809 (beginning-of-line)
1810 (while (and (> (point) cmt-skip-pos)
1811 (progn (backward-char)
1812 (eq (char-before) ?\\)))
1813 (beginning-of-line))
1814
1815 (if (< (point) cmt-skip-pos)
1816 ;; Don't move past the cpp directive if we began inside
1817 ;; it. Note that the position at the end of the last line
1818 ;; of the macro is also considered to be within it.
1819 (progn (goto-char cmt-skip-pos)
1820 nil)
1821
1822 ;; It's worthwhile to spend a little bit of effort on finding
1823 ;; the end of the macro, to get a good `simple-ws-beg'
1824 ;; position for the cache. Note that `c-backward-comments'
1825 ;; could have stepped over some comments before going into
1826 ;; the macro, and then `simple-ws-beg' must be kept on the
1827 ;; same side of those comments.
1828 (goto-char simple-ws-beg)
1829 (skip-chars-backward " \t\n\r\f\v")
1830 (if (eq (char-before) ?\\)
1831 (forward-char))
1832 (forward-line 1)
1833 (if (< (point) simple-ws-beg)
1834 ;; Might happen if comments after the macro were skipped
1835 ;; over.
1836 (setq simple-ws-beg (point)))
1837
1838 (goto-char cpp-beg)
1839 t)))
1840
1841 ((/= (save-excursion
1842 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1843 (setq next-rung-pos (point)))
1844 simple-ws-beg)
1845 ;; Skipped over comments. Must put point at the end of
1846 ;; the simple ws at point since we might be after a line
1847 ;; comment or cpp directive that's been partially
1848 ;; narrowed out, and we can't risk marking the simple ws
1849 ;; at the end of it.
1850 (goto-char next-rung-pos)
1851 t)))
1852
1853 ;; We've searched over a piece of non-white syntactic ws. See if this
1854 ;; can be cached.
1855 (setq next-rung-pos (point))
1856 (skip-chars-backward " \t\f\v")
1857
1858 (if (or
1859 ;; Cache if we started either from a marked rung or from a
1860 ;; completely uncached position.
1861 rung-is-marked
1862 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1863
1864 ;; Cache if there's a marked rung in the encountered simple ws.
1865 (save-excursion
1866 (skip-chars-backward " \t\n\r\f\v")
1867 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1868 'c-is-sws t)))
1869
1870 (progn
1871 (c-debug-sws-msg
1872 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1873 (point) (1+ next-rung-pos)
1874 simple-ws-beg (min (1+ rung-pos) (point-max))
1875 (point-min))
1876
1877 ;; Remove the properties for any nested ws that might be cached.
1878 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1879 ;; anyway.
1880 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1881 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1882 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1883 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1884 ;; Remove any `c-in-sws' property from the last char of
1885 ;; the rung before we mark it with `c-is-sws', so that we
1886 ;; won't connect with the remains of a broken "ladder".
1887 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1888 (c-put-is-sws simple-ws-beg
1889 rung-end-pos)
1890 (setq rung-is-marked t)))
1891 (c-put-in-sws (setq simple-ws-beg (point)
1892 last-put-in-sws-pos simple-ws-beg)
1893 rung-pos)
1894 (c-put-is-sws (setq rung-pos simple-ws-beg)
1895 (1+ next-rung-pos)))
1896
1897 (c-debug-sws-msg
1898 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1899 (point) (1+ next-rung-pos)
1900 simple-ws-beg (min (1+ rung-pos) (point-max))
1901 (point-min))
1902 (setq rung-pos next-rung-pos
1903 simple-ws-beg (point))
1904 ))
1905
1906 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1907 ;; another one before the point (which might occur when editing inside a
1908 ;; comment or macro).
1909 (when (eq last-put-in-sws-pos (point))
1910 (cond ((< (point-min) last-put-in-sws-pos)
1911 (c-debug-sws-msg
1912 "c-backward-sws clearing at %s for cache separation"
1913 (1- last-put-in-sws-pos))
1914 (c-remove-in-sws (1- last-put-in-sws-pos)
1915 last-put-in-sws-pos))
1916 ((> (point-min) 1)
1917 ;; If at bob and the buffer is narrowed, we have to clear the
1918 ;; character we're standing on instead since there might be a
1919 ;; `c-in-sws' before (point-min). In this case it's necessary
1920 ;; to clear both properties.
1921 (c-debug-sws-msg
1922 "c-backward-sws clearing thoroughly at %s for cache separation"
1923 last-put-in-sws-pos)
1924 (c-remove-is-and-in-sws last-put-in-sws-pos
1925 (1+ last-put-in-sws-pos)))))
1926 )))
1927
1928 \f
1929 ;; Other whitespace tools
1930 (defun c-partial-ws-p (beg end)
1931 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1932 ;; region? This is a "heuristic" function. .....
1933 ;;
1934 ;; The motivation for the second bit is to check whether removing this
1935 ;; region would coalesce two symbols.
1936 ;;
1937 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1938 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1939 (save-excursion
1940 (let ((end+1 (min (1+ end) (point-max))))
1941 (or (progn (goto-char (max (point-min) (1- beg)))
1942 (c-skip-ws-forward end)
1943 (eq (point) end))
1944 (progn (goto-char beg)
1945 (c-skip-ws-forward end+1)
1946 (eq (point) end+1))))))
1947 \f
1948 ;; A system for finding noteworthy parens before the point.
1949
1950 (defvar c-state-cache nil)
1951 (make-variable-buffer-local 'c-state-cache)
1952 ;; The state cache used by `c-parse-state' to cut down the amount of
1953 ;; searching. It's the result from some earlier `c-parse-state' call.
1954 ;;
1955 ;; The use of the cached info is more effective if the next
1956 ;; `c-parse-state' call is on a line close by the one the cached state
1957 ;; was made at; the cache can actually slow down a little if the
1958 ;; cached state was made very far back in the buffer. The cache is
1959 ;; most effective if `c-parse-state' is used on each line while moving
1960 ;; forward.
1961
1962 (defvar c-state-cache-start 1)
1963 (make-variable-buffer-local 'c-state-cache-start)
1964 ;; This is (point-min) when `c-state-cache' was calculated, since a
1965 ;; change of narrowing is likely to affect the parens that are visible
1966 ;; before the point.
1967
1968 (defvar c-state-cache-good-pos 1)
1969 (make-variable-buffer-local 'c-state-cache-good-pos)
1970 ;; This is a position where `c-state-cache' is known to be correct.
1971 ;; It's a position inside one of the recorded unclosed parens or the
1972 ;; top level, but not further nested inside any literal or subparen
1973 ;; that is closed before the last recorded position.
1974 ;;
1975 ;; The exact position is chosen to try to be close to yet earlier than
1976 ;; the position where `c-state-cache' will be called next. Right now
1977 ;; the heuristic is to set it to the position after the last found
1978 ;; closing paren (of any type) before the line on which
1979 ;; `c-parse-state' was called. That is chosen primarily to work well
1980 ;; with refontification of the current line.
1981
1982 (defsubst c-invalidate-state-cache (pos)
1983 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1984 ;; at POS or higher. This is much like `c-whack-state-after', but
1985 ;; it never changes a paren pair element into an open paren element.
1986 ;; Doing that would mean that the new open paren wouldn't have the
1987 ;; required preceding paren pair element.
1988 (while (and (or c-state-cache
1989 (when (< pos c-state-cache-good-pos)
1990 (setq c-state-cache-good-pos 1)
1991 nil))
1992 (let ((elem (car c-state-cache)))
1993 (if (consp elem)
1994 (or (< pos (cdr elem))
1995 (when (< pos c-state-cache-good-pos)
1996 (setq c-state-cache-good-pos (cdr elem))
1997 nil))
1998 (or (<= pos elem)
1999 (when (< pos c-state-cache-good-pos)
2000 (setq c-state-cache-good-pos (1+ elem))
2001 nil)))))
2002 (setq c-state-cache (cdr c-state-cache))))
2003
2004 (defun c-get-fallback-start-pos (here)
2005 ;; Return the start position for building `c-state-cache' from
2006 ;; scratch.
2007 (save-excursion
2008 ;; Go back 2 bods, but ignore any bogus positions returned by
2009 ;; beginning-of-defun (i.e. open paren in column zero).
2010 (goto-char here)
2011 (let ((cnt 2))
2012 (while (not (or (bobp) (zerop cnt)))
2013 (c-beginning-of-defun-1)
2014 (if (eq (char-after) ?\{)
2015 (setq cnt (1- cnt)))))
2016 (point)))
2017
2018 (defun c-parse-state ()
2019 ;; Find and record all noteworthy parens between some good point
2020 ;; earlier in the file and point. That good point is at least the
2021 ;; beginning of the top-level construct we are in, or the beginning
2022 ;; of the preceding top-level construct if we aren't in one.
2023 ;;
2024 ;; The returned value is a list of the noteworthy parens with the
2025 ;; last one first. If an element in the list is an integer, it's
2026 ;; the position of an open paren which has not been closed before
2027 ;; the point. If an element is a cons, it gives the position of a
2028 ;; closed brace paren pair; the car is the start paren position and
2029 ;; the cdr is the position following the closing paren. Only the
2030 ;; last closed brace paren pair before each open paren and before
2031 ;; the point is recorded, and thus the state never contains two cons
2032 ;; elements in succession.
2033 ;;
2034 ;; Currently no characters which are given paren syntax with the
2035 ;; syntax-table property are recorded, i.e. angle bracket arglist
2036 ;; parens are never present here. Note that this might change.
2037 ;;
2038 ;; BUG: This function doesn't cope entirely well with unbalanced
2039 ;; parens in macros. E.g. in the following case the brace before
2040 ;; the macro isn't balanced with the one after it:
2041 ;;
2042 ;; {
2043 ;; #define X {
2044 ;; }
2045 ;;
2046 ;; This function might do hidden buffer changes.
2047
2048 (save-restriction
2049 (let* ((here (point))
2050 (here-bol (c-point 'bol))
2051 (c-macro-start (c-query-macro-start))
2052 (in-macro-start (or c-macro-start (point)))
2053 old-state last-pos brace-pair-open brace-pair-close
2054 pos save-pos)
2055 (c-invalidate-state-cache here)
2056
2057 ;; If the minimum position has changed due to narrowing then we
2058 ;; have to fix the tail of `c-state-cache' accordingly.
2059 (unless (= c-state-cache-start (point-min))
2060 (if (> (point-min) c-state-cache-start)
2061 ;; If point-min has moved forward then we just need to cut
2062 ;; off a bit of the tail.
2063 (let ((ptr (cons nil c-state-cache)) elem)
2064 (while (and (setq elem (car-safe (cdr ptr)))
2065 (>= (if (consp elem) (car elem) elem)
2066 (point-min)))
2067 (setq ptr (cdr ptr)))
2068 (when (consp ptr)
2069 (if (eq (cdr ptr) c-state-cache)
2070 (setq c-state-cache nil
2071 c-state-cache-good-pos 1)
2072 (setcdr ptr nil))))
2073 ;; If point-min has moved backward then we drop the state
2074 ;; completely. It's possible to do a better job here and
2075 ;; recalculate the top only.
2076 (setq c-state-cache nil
2077 c-state-cache-good-pos 1))
2078 (setq c-state-cache-start (point-min)))
2079
2080 ;; Get the latest position we know are directly inside the
2081 ;; closest containing paren of the cached state.
2082 (setq last-pos (and c-state-cache
2083 (if (consp (car c-state-cache))
2084 (cdr (car c-state-cache))
2085 (1+ (car c-state-cache)))))
2086 (if (or (not last-pos)
2087 (< last-pos c-state-cache-good-pos))
2088 (setq last-pos c-state-cache-good-pos)
2089 ;; Take the opportunity to move the cached good position
2090 ;; further down.
2091 (if (< last-pos here-bol)
2092 (setq c-state-cache-good-pos last-pos)))
2093
2094 ;; Check if `last-pos' is in a macro. If it is, and we're not
2095 ;; in the same macro, we must discard everything on
2096 ;; `c-state-cache' that is inside the macro before using it.
2097 (save-excursion
2098 (goto-char last-pos)
2099 (when (and (c-beginning-of-macro)
2100 (/= (point) in-macro-start))
2101 (c-invalidate-state-cache (point))
2102 ;; Set `last-pos' again just like above except that there's
2103 ;; no use looking at `c-state-cache-good-pos' here.
2104 (setq last-pos (if c-state-cache
2105 (if (consp (car c-state-cache))
2106 (cdr (car c-state-cache))
2107 (1+ (car c-state-cache)))
2108 1))))
2109
2110 ;; If we've moved very far from the last cached position then
2111 ;; it's probably better to redo it from scratch, otherwise we
2112 ;; might spend a lot of time searching from `last-pos' down to
2113 ;; here.
2114 (when (< last-pos (- here 20000))
2115 ;; First get the fallback start position. If it turns out
2116 ;; that it's so far back that the cached state is closer then
2117 ;; we'll keep it afterall.
2118 (setq pos (c-get-fallback-start-pos here))
2119 (if (<= pos last-pos)
2120 (setq pos nil)
2121 (setq last-pos nil
2122 c-state-cache nil
2123 c-state-cache-good-pos 1)))
2124
2125 ;; Find the start position for the forward search. (Can't
2126 ;; search in the backward direction since the point might be in
2127 ;; some kind of literal.)
2128
2129 (unless pos
2130 (setq old-state c-state-cache)
2131
2132 ;; There's a cached state with a containing paren. Pop off
2133 ;; the stale containing sexps from it by going forward out of
2134 ;; parens as far as possible.
2135 (narrow-to-region (point-min) here)
2136 (let (placeholder pair-beg)
2137 (while (and c-state-cache
2138 (setq placeholder
2139 (c-up-list-forward last-pos)))
2140 (setq last-pos placeholder)
2141 (if (consp (car c-state-cache))
2142 (setq pair-beg (car-safe (cdr c-state-cache))
2143 c-state-cache (cdr-safe (cdr c-state-cache)))
2144 (setq pair-beg (car c-state-cache)
2145 c-state-cache (cdr c-state-cache))))
2146
2147 (when (and pair-beg (eq (char-after pair-beg) ?{))
2148 ;; The last paren pair we moved out from was a brace
2149 ;; pair. Modify the state to record this as a closed
2150 ;; pair now.
2151 (if (consp (car-safe c-state-cache))
2152 (setq c-state-cache (cdr c-state-cache)))
2153 (setq c-state-cache (cons (cons pair-beg last-pos)
2154 c-state-cache))))
2155
2156 ;; Check if the preceding balanced paren is within a
2157 ;; macro; it should be ignored if we're outside the
2158 ;; macro. There's no need to check any further upwards;
2159 ;; if the macro contains an unbalanced opening paren then
2160 ;; we're smoked anyway.
2161 (when (and (<= (point) in-macro-start)
2162 (consp (car c-state-cache)))
2163 (save-excursion
2164 (goto-char (car (car c-state-cache)))
2165 (when (c-beginning-of-macro)
2166 (setq here (point)
2167 c-state-cache (cdr c-state-cache)))))
2168
2169 (unless (eq c-state-cache old-state)
2170 ;; Have to adjust the cached good position if state has been
2171 ;; popped off.
2172 (setq c-state-cache-good-pos
2173 (if c-state-cache
2174 (if (consp (car c-state-cache))
2175 (cdr (car c-state-cache))
2176 (1+ (car c-state-cache)))
2177 1)
2178 old-state c-state-cache))
2179
2180 (when c-state-cache
2181 (setq pos last-pos)))
2182
2183 ;; Get the fallback start position.
2184 (unless pos
2185 (setq pos (c-get-fallback-start-pos here)
2186 c-state-cache nil
2187 c-state-cache-good-pos 1))
2188
2189 (narrow-to-region (point-min) here)
2190
2191 (while pos
2192 (setq save-pos pos
2193 brace-pair-open nil)
2194
2195 ;; Find the balanced brace pairs. This loop is hot, so it
2196 ;; does ugly tricks to go faster.
2197 (c-safe
2198 (let (set-good-pos set-brace-pair)
2199 (while t
2200 (setq last-pos nil
2201 last-pos (scan-lists pos 1 -1)) ; Might signal.
2202 (setq pos (scan-lists last-pos 1 1) ; Might signal.
2203 set-good-pos (< pos here-bol)
2204 set-brace-pair (eq (char-before last-pos) ?{))
2205
2206 ;; Update the cached good position and record the brace
2207 ;; pair, whichever is applicable for the paren we've
2208 ;; just jumped over. But first check that it isn't
2209 ;; inside a macro and the point isn't inside the same
2210 ;; one.
2211 (when (and (or set-good-pos set-brace-pair)
2212 (or (>= pos in-macro-start)
2213 (save-excursion
2214 (goto-char pos)
2215 (not (c-beginning-of-macro)))))
2216 (if set-good-pos
2217 (setq c-state-cache-good-pos pos))
2218 (if set-brace-pair
2219 (setq brace-pair-open last-pos
2220 brace-pair-close pos))))))
2221
2222 ;; Record the last brace pair.
2223 (when brace-pair-open
2224 (let ((head (car-safe c-state-cache)))
2225 (if (consp head)
2226 (progn
2227 (setcar head (1- brace-pair-open))
2228 (setcdr head brace-pair-close))
2229 (setq c-state-cache (cons (cons (1- brace-pair-open)
2230 brace-pair-close)
2231 c-state-cache)))))
2232
2233 (if last-pos
2234 ;; Prepare to loop, but record the open paren only if it's
2235 ;; outside a macro or within the same macro as point, and
2236 ;; if it is a legitimate open paren and not some character
2237 ;; that got an open paren syntax-table property.
2238 (progn
2239 (setq pos last-pos)
2240 (when (and (or (>= last-pos in-macro-start)
2241 (save-excursion
2242 (goto-char last-pos)
2243 (not (c-beginning-of-macro))))
2244 ;; Check for known types of parens that we
2245 ;; want to record. The syntax table is not to
2246 ;; be trusted here since the caller might be
2247 ;; using e.g. `c++-template-syntax-table'.
2248 (memq (char-before last-pos) '(?{ ?\( ?\[)))
2249 (if (< last-pos here-bol)
2250 (setq c-state-cache-good-pos last-pos))
2251 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
2252
2253 (if (setq last-pos (c-up-list-forward pos))
2254 ;; Found a close paren without a corresponding opening
2255 ;; one. Maybe we didn't go back far enough, so try to
2256 ;; scan backward for the start paren and then start over.
2257 (progn
2258 (setq pos (c-up-list-backward pos)
2259 c-state-cache nil
2260 c-state-cache-good-pos c-state-cache-start)
2261 (when (or (not pos)
2262 ;; Emacs (up to at least 21.2) can get confused by
2263 ;; open parens in column zero inside comments: The
2264 ;; sexp functions can then misbehave and bring us
2265 ;; back to the same point again. Check this so that
2266 ;; we don't get an infinite loop.
2267 (>= pos save-pos))
2268 (setq pos last-pos
2269 c-parsing-error
2270 (format "Unbalanced close paren at line %d"
2271 (1+ (count-lines (point-min)
2272 (c-point 'bol last-pos)))))))
2273 (setq pos nil))))
2274
2275 ;;(message "c-parse-state: %S end: %S" c-state-cache c-state-cache-good-pos)
2276 c-state-cache)))
2277
2278 ;; Debug tool to catch cache inconsistencies.
2279 (defvar c-debug-parse-state nil)
2280 (unless (fboundp 'c-real-parse-state)
2281 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
2282 (cc-bytecomp-defun c-real-parse-state)
2283 (defun c-debug-parse-state ()
2284 (let ((res1 (c-real-parse-state)) res2)
2285 (let ((c-state-cache nil)
2286 (c-state-cache-start 1)
2287 (c-state-cache-good-pos 1))
2288 (setq res2 (c-real-parse-state)))
2289 (unless (equal res1 res2)
2290 ;; The cache can actually go further back due to the ad-hoc way
2291 ;; the first paren is found, so try to whack off a bit of its
2292 ;; start before complaining.
2293 (save-excursion
2294 (goto-char (or (c-least-enclosing-brace res2) (point)))
2295 (c-beginning-of-defun-1)
2296 (while (not (or (bobp) (eq (char-after) ?{)))
2297 (c-beginning-of-defun-1))
2298 (unless (equal (c-whack-state-before (point) res1) res2)
2299 (message (concat "c-parse-state inconsistency: "
2300 "using cache: %s, from scratch: %s")
2301 res1 res2))))
2302 res1))
2303 (defun c-toggle-parse-state-debug (&optional arg)
2304 (interactive "P")
2305 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
2306 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
2307 'c-debug-parse-state
2308 'c-real-parse-state)))
2309 (c-keep-region-active))
2310 (when c-debug-parse-state
2311 (c-toggle-parse-state-debug 1))
2312
2313 (defun c-whack-state-before (bufpos paren-state)
2314 ;; Whack off any state information from PAREN-STATE which lies
2315 ;; before BUFPOS. Not destructive on PAREN-STATE.
2316 (let* ((newstate (list nil))
2317 (ptr newstate)
2318 car)
2319 (while paren-state
2320 (setq car (car paren-state)
2321 paren-state (cdr paren-state))
2322 (if (< (if (consp car) (car car) car) bufpos)
2323 (setq paren-state nil)
2324 (setcdr ptr (list car))
2325 (setq ptr (cdr ptr))))
2326 (cdr newstate)))
2327
2328 (defun c-whack-state-after (bufpos paren-state)
2329 ;; Whack off any state information from PAREN-STATE which lies at or
2330 ;; after BUFPOS. Not destructive on PAREN-STATE.
2331 (catch 'done
2332 (while paren-state
2333 (let ((car (car paren-state)))
2334 (if (consp car)
2335 ;; just check the car, because in a balanced brace
2336 ;; expression, it must be impossible for the corresponding
2337 ;; close brace to be before point, but the open brace to
2338 ;; be after.
2339 (if (<= bufpos (car car))
2340 nil ; whack it off
2341 (if (< bufpos (cdr car))
2342 ;; its possible that the open brace is before
2343 ;; bufpos, but the close brace is after. In that
2344 ;; case, convert this to a non-cons element. The
2345 ;; rest of the state is before bufpos, so we're
2346 ;; done.
2347 (throw 'done (cons (car car) (cdr paren-state)))
2348 ;; we know that both the open and close braces are
2349 ;; before bufpos, so we also know that everything else
2350 ;; on state is before bufpos.
2351 (throw 'done paren-state)))
2352 (if (<= bufpos car)
2353 nil ; whack it off
2354 ;; it's before bufpos, so everything else should too.
2355 (throw 'done paren-state)))
2356 (setq paren-state (cdr paren-state)))
2357 nil)))
2358
2359 (defun c-most-enclosing-brace (paren-state &optional bufpos)
2360 ;; Return the bufpos of the innermost enclosing open paren before
2361 ;; bufpos, or nil if none was found.
2362 (let (enclosingp)
2363 (or bufpos (setq bufpos 134217727))
2364 (while paren-state
2365 (setq enclosingp (car paren-state)
2366 paren-state (cdr paren-state))
2367 (if (or (consp enclosingp)
2368 (>= enclosingp bufpos))
2369 (setq enclosingp nil)
2370 (setq paren-state nil)))
2371 enclosingp))
2372
2373 (defun c-least-enclosing-brace (paren-state)
2374 ;; Return the bufpos of the outermost enclosing open paren, or nil
2375 ;; if none was found.
2376 (let (pos elem)
2377 (while paren-state
2378 (setq elem (car paren-state)
2379 paren-state (cdr paren-state))
2380 (if (integerp elem)
2381 (setq pos elem)))
2382 pos))
2383
2384 (defun c-safe-position (bufpos paren-state)
2385 ;; Return the closest "safe" position recorded on PAREN-STATE that
2386 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
2387 ;; contain any. Return nil if BUFPOS is nil, which is useful to
2388 ;; find the closest limit before a given limit that might be nil.
2389 ;;
2390 ;; A "safe" position is a position at or after a recorded open
2391 ;; paren, or after a recorded close paren. The returned position is
2392 ;; thus either the first position after a close brace, or the first
2393 ;; position after an enclosing paren, or at the enclosing paren in
2394 ;; case BUFPOS is immediately after it.
2395 (when bufpos
2396 (let (elem)
2397 (catch 'done
2398 (while paren-state
2399 (setq elem (car paren-state))
2400 (if (consp elem)
2401 (cond ((< (cdr elem) bufpos)
2402 (throw 'done (cdr elem)))
2403 ((< (car elem) bufpos)
2404 ;; See below.
2405 (throw 'done (min (1+ (car elem)) bufpos))))
2406 (if (< elem bufpos)
2407 ;; elem is the position at and not after the opening paren, so
2408 ;; we can go forward one more step unless it's equal to
2409 ;; bufpos. This is useful in some cases avoid an extra paren
2410 ;; level between the safe position and bufpos.
2411 (throw 'done (min (1+ elem) bufpos))))
2412 (setq paren-state (cdr paren-state)))))))
2413
2414 (defun c-beginning-of-syntax ()
2415 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2416 ;; goes to the closest previous point that is known to be outside
2417 ;; any string literal or comment. `c-state-cache' is used if it has
2418 ;; a position in the vicinity.
2419 (let* ((paren-state c-state-cache)
2420 elem
2421
2422 (pos (catch 'done
2423 ;; Note: Similar code in `c-safe-position'. The
2424 ;; difference is that we accept a safe position at
2425 ;; the point and don't bother to go forward past open
2426 ;; parens.
2427 (while paren-state
2428 (setq elem (car paren-state))
2429 (if (consp elem)
2430 (cond ((<= (cdr elem) (point))
2431 (throw 'done (cdr elem)))
2432 ((<= (car elem) (point))
2433 (throw 'done (car elem))))
2434 (if (<= elem (point))
2435 (throw 'done elem)))
2436 (setq paren-state (cdr paren-state)))
2437 (point-min))))
2438
2439 (if (> pos (- (point) 4000))
2440 (goto-char pos)
2441 ;; The position is far back. Try `c-beginning-of-defun-1'
2442 ;; (although we can't be entirely sure it will go to a position
2443 ;; outside a comment or string in current emacsen). FIXME:
2444 ;; Consult `syntax-ppss' here.
2445 (c-beginning-of-defun-1)
2446 (if (< (point) pos)
2447 (goto-char pos)))))
2448
2449 \f
2450 ;; Tools for scanning identifiers and other tokens.
2451
2452 (defun c-on-identifier ()
2453 "Return non-nil if the point is on or directly after an identifier.
2454 Keywords are recognized and not considered identifiers. If an
2455 identifier is detected, the returned value is its starting position.
2456 If an identifier ends at the point and another begins at it \(can only
2457 happen in Pike) then the point for the preceding one is returned.
2458
2459 Note that this function might do hidden buffer changes. See the
2460 comment at the start of cc-engine.el for more info."
2461
2462 ;; FIXME: Shouldn't this function handle "operator" in C++?
2463
2464 (save-excursion
2465 (skip-syntax-backward "w_")
2466
2467 (or
2468
2469 ;; Check for a normal (non-keyword) identifier.
2470 (and (looking-at c-symbol-start)
2471 (not (looking-at c-keywords-regexp))
2472 (point))
2473
2474 (when (c-major-mode-is 'pike-mode)
2475 ;; Handle the `<operator> syntax in Pike.
2476 (let ((pos (point)))
2477 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2478 (and (if (< (skip-chars-backward "`") 0)
2479 t
2480 (goto-char pos)
2481 (eq (char-after) ?\`))
2482 (looking-at c-symbol-key)
2483 (>= (match-end 0) pos)
2484 (point))))
2485
2486 ;; Handle the "operator +" syntax in C++.
2487 (when (and c-overloadable-operators-regexp
2488 (= (c-backward-token-2 0) 0))
2489
2490 (cond ((and (looking-at c-overloadable-operators-regexp)
2491 (or (not c-opt-op-identifier-prefix)
2492 (and (= (c-backward-token-2 1) 0)
2493 (looking-at c-opt-op-identifier-prefix))))
2494 (point))
2495
2496 ((save-excursion
2497 (and c-opt-op-identifier-prefix
2498 (looking-at c-opt-op-identifier-prefix)
2499 (= (c-forward-token-2 1) 0)
2500 (looking-at c-overloadable-operators-regexp)))
2501 (point))))
2502
2503 )))
2504
2505 (defsubst c-simple-skip-symbol-backward ()
2506 ;; If the point is at the end of a symbol then skip backward to the
2507 ;; beginning of it. Don't move otherwise. Return non-nil if point
2508 ;; moved.
2509 ;;
2510 ;; This function might do hidden buffer changes.
2511 (or (< (skip-syntax-backward "w_") 0)
2512 (and (c-major-mode-is 'pike-mode)
2513 ;; Handle the `<operator> syntax in Pike.
2514 (let ((pos (point)))
2515 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2516 (< (skip-chars-backward "`") 0)
2517 (looking-at c-symbol-key)
2518 (>= (match-end 0) pos))
2519 t
2520 (goto-char pos)
2521 nil)))))
2522
2523 (defun c-beginning-of-current-token (&optional back-limit)
2524 ;; Move to the beginning of the current token. Do not move if not
2525 ;; in the middle of one. BACK-LIMIT may be used to bound the
2526 ;; backward search; if given it's assumed to be at the boundary
2527 ;; between two tokens. Return non-nil if the point is moved, nil
2528 ;; otherwise.
2529 ;;
2530 ;; This function might do hidden buffer changes.
2531 (let ((start (point)))
2532 (if (looking-at "\\w\\|\\s_")
2533 (skip-syntax-backward "w_" back-limit)
2534 (when (< (skip-syntax-backward ".()" back-limit) 0)
2535 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2536 (match-end 0))
2537 ;; `c-nonsymbol-token-regexp' should always match
2538 ;; since we've skipped backward over punctuator
2539 ;; or paren syntax, but consume one char in case
2540 ;; it doesn't so that we don't leave point before
2541 ;; some earlier incorrect token.
2542 (1+ (point)))))
2543 (if (<= pos start)
2544 (goto-char pos))))))
2545 (< (point) start)))
2546
2547 (defun c-end-of-current-token (&optional back-limit)
2548 ;; Move to the end of the current token. Do not move if not in the
2549 ;; middle of one. BACK-LIMIT may be used to bound the backward
2550 ;; search; if given it's assumed to be at the boundary between two
2551 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2552 ;;
2553 ;; This function might do hidden buffer changes.
2554 (let ((start (point)))
2555 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2556 (skip-syntax-forward "w_"))
2557 ((< (skip-syntax-backward ".()" back-limit) 0)
2558 (while (progn
2559 (if (looking-at c-nonsymbol-token-regexp)
2560 (goto-char (match-end 0))
2561 ;; `c-nonsymbol-token-regexp' should always match since
2562 ;; we've skipped backward over punctuator or paren
2563 ;; syntax, but move forward in case it doesn't so that
2564 ;; we don't leave point earlier than we started with.
2565 (forward-char))
2566 (< (point) start)))))
2567 (> (point) start)))
2568
2569 (defconst c-jump-syntax-balanced
2570 (if (memq 'gen-string-delim c-emacs-features)
2571 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2572 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2573
2574 (defconst c-jump-syntax-unbalanced
2575 (if (memq 'gen-string-delim c-emacs-features)
2576 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2577 "\\w\\|\\s_\\|\\s\""))
2578
2579 (defun c-forward-token-2 (&optional count balanced limit)
2580 "Move forward by tokens.
2581 A token is defined as all symbols and identifiers which aren't
2582 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2583 treated properly). Point is always either left at the beginning of a
2584 token or not moved at all. COUNT specifies the number of tokens to
2585 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2586 moves to the next token beginning only if not already at one. If
2587 BALANCED is true, move over balanced parens, otherwise move into them.
2588 Also, if BALANCED is true, never move out of an enclosing paren.
2589
2590 LIMIT sets the limit for the movement and defaults to the point limit.
2591 The case when LIMIT is set in the middle of a token, comment or macro
2592 is handled correctly, i.e. the point won't be left there.
2593
2594 Return the number of tokens left to move \(positive or negative). If
2595 BALANCED is true, a move over a balanced paren counts as one. Note
2596 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2597 be returned. Thus, a return value of 0 guarantees that point is at
2598 the requested position and a return value less \(without signs) than
2599 COUNT guarantees that point is at the beginning of some token.
2600
2601 Note that this function might do hidden buffer changes. See the
2602 comment at the start of cc-engine.el for more info."
2603
2604 (or count (setq count 1))
2605 (if (< count 0)
2606 (- (c-backward-token-2 (- count) balanced limit))
2607
2608 (let ((jump-syntax (if balanced
2609 c-jump-syntax-balanced
2610 c-jump-syntax-unbalanced))
2611 (last (point))
2612 (prev (point)))
2613
2614 (if (zerop count)
2615 ;; If count is zero we should jump if in the middle of a token.
2616 (c-end-of-current-token))
2617
2618 (save-restriction
2619 (if limit (narrow-to-region (point-min) limit))
2620 (if (/= (point)
2621 (progn (c-forward-syntactic-ws) (point)))
2622 ;; Skip whitespace. Count this as a move if we did in
2623 ;; fact move.
2624 (setq count (max (1- count) 0)))
2625
2626 (if (eobp)
2627 ;; Moved out of bounds. Make sure the returned count isn't zero.
2628 (progn
2629 (if (zerop count) (setq count 1))
2630 (goto-char last))
2631
2632 ;; Use `condition-case' to avoid having the limit tests
2633 ;; inside the loop.
2634 (condition-case nil
2635 (while (and
2636 (> count 0)
2637 (progn
2638 (setq last (point))
2639 (cond ((looking-at jump-syntax)
2640 (goto-char (scan-sexps (point) 1))
2641 t)
2642 ((looking-at c-nonsymbol-token-regexp)
2643 (goto-char (match-end 0))
2644 t)
2645 ;; `c-nonsymbol-token-regexp' above should always
2646 ;; match if there are correct tokens. Try to
2647 ;; widen to see if the limit was set in the
2648 ;; middle of one, else fall back to treating
2649 ;; the offending thing as a one character token.
2650 ((and limit
2651 (save-restriction
2652 (widen)
2653 (looking-at c-nonsymbol-token-regexp)))
2654 nil)
2655 (t
2656 (forward-char)
2657 t))))
2658 (c-forward-syntactic-ws)
2659 (setq prev last
2660 count (1- count)))
2661 (error (goto-char last)))
2662
2663 (when (eobp)
2664 (goto-char prev)
2665 (setq count (1+ count)))))
2666
2667 count)))
2668
2669 (defun c-backward-token-2 (&optional count balanced limit)
2670 "Move backward by tokens.
2671 See `c-forward-token-2' for details."
2672
2673 (or count (setq count 1))
2674 (if (< count 0)
2675 (- (c-forward-token-2 (- count) balanced limit))
2676
2677 (or limit (setq limit (point-min)))
2678 (let ((jump-syntax (if balanced
2679 c-jump-syntax-balanced
2680 c-jump-syntax-unbalanced))
2681 (last (point)))
2682
2683 (if (zerop count)
2684 ;; The count is zero so try to skip to the beginning of the
2685 ;; current token.
2686 (if (> (point)
2687 (progn (c-beginning-of-current-token) (point)))
2688 (if (< (point) limit)
2689 ;; The limit is inside the same token, so return 1.
2690 (setq count 1))
2691
2692 ;; We're not in the middle of a token. If there's
2693 ;; whitespace after the point then we must move backward,
2694 ;; so set count to 1 in that case.
2695 (and (looking-at c-syntactic-ws-start)
2696 ;; If we're looking at a '#' that might start a cpp
2697 ;; directive then we have to do a more elaborate check.
2698 (or (/= (char-after) ?#)
2699 (not c-opt-cpp-prefix)
2700 (save-excursion
2701 (and (= (point)
2702 (progn (beginning-of-line)
2703 (looking-at "[ \t]*")
2704 (match-end 0)))
2705 (or (bobp)
2706 (progn (backward-char)
2707 (not (eq (char-before) ?\\)))))))
2708 (setq count 1))))
2709
2710 ;; Use `condition-case' to avoid having to check for buffer
2711 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2712 (condition-case nil
2713 (while (and
2714 (> count 0)
2715 (progn
2716 (c-backward-syntactic-ws)
2717 (backward-char)
2718 (if (looking-at jump-syntax)
2719 (goto-char (scan-sexps (1+ (point)) -1))
2720 ;; This can be very inefficient if there's a long
2721 ;; sequence of operator tokens without any separation.
2722 ;; That doesn't happen in practice, anyway.
2723 (c-beginning-of-current-token))
2724 (>= (point) limit)))
2725 (setq last (point)
2726 count (1- count)))
2727 (error (goto-char last)))
2728
2729 (if (< (point) limit)
2730 (goto-char last))
2731
2732 count)))
2733
2734 (defun c-forward-token-1 (&optional count balanced limit)
2735 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2736 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2737 characters are jumped over character by character. This function is
2738 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2739 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2740 (c-forward-token-2 count balanced limit)))
2741
2742 (defun c-backward-token-1 (&optional count balanced limit)
2743 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2744 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2745 characters are jumped over character by character. This function is
2746 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2747 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2748 (c-backward-token-2 count balanced limit)))
2749
2750 \f
2751 ;; Tools for doing searches restricted to syntactically relevant text.
2752
2753 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2754 paren-level not-inside-token
2755 lookbehind-submatch)
2756 "Like `re-search-forward', but only report matches that are found
2757 in syntactically significant text. I.e. matches in comments, macros
2758 or string literals are ignored. The start point is assumed to be
2759 outside any comment, macro or string literal, or else the content of
2760 that region is taken as syntactically significant text.
2761
2762 If PAREN-LEVEL is non-nil, an additional restriction is added to
2763 ignore matches in nested paren sexps. The search will also not go
2764 outside the current list sexp, which has the effect that if the point
2765 should be moved to BOUND when no match is found \(i.e. NOERROR is
2766 neither nil nor t), then it will be at the closing paren if the end of
2767 the current list sexp is encountered first.
2768
2769 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2770 ignored. Things like multicharacter operators and special symbols
2771 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2772 constants.
2773
2774 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2775 subexpression in REGEXP. The end of that submatch is used as the
2776 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2777 isn't used or if that subexpression didn't match then the start
2778 position of the whole match is used instead. The \"look behind\"
2779 subexpression is never tested before the starting position, so it
2780 might be a good idea to include \\=\\= as a match alternative in it.
2781
2782 Optimization note: Matches might be missed if the \"look behind\"
2783 subexpression can match the end of nonwhite syntactic whitespace,
2784 i.e. the end of comments or cpp directives. This since the function
2785 skips over such things before resuming the search. It's on the other
2786 hand not safe to assume that the \"look behind\" subexpression never
2787 matches syntactic whitespace.
2788
2789 Bug: Unbalanced parens inside cpp directives are currently not handled
2790 correctly \(i.e. they don't get ignored as they should) when
2791 PAREN-LEVEL is set.
2792
2793 Note that this function might do hidden buffer changes. See the
2794 comment at the start of cc-engine.el for more info."
2795
2796 (or bound (setq bound (point-max)))
2797 (if paren-level (setq paren-level -1))
2798
2799 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2800
2801 (let ((start (point))
2802 tmp
2803 ;; Start position for the last search.
2804 search-pos
2805 ;; The `parse-partial-sexp' state between the start position
2806 ;; and the point.
2807 state
2808 ;; The current position after the last state update. The next
2809 ;; `parse-partial-sexp' continues from here.
2810 (state-pos (point))
2811 ;; The position at which to check the state and the state
2812 ;; there. This is separate from `state-pos' since we might
2813 ;; need to back up before doing the next search round.
2814 check-pos check-state
2815 ;; Last position known to end a token.
2816 (last-token-end-pos (point-min))
2817 ;; Set when a valid match is found.
2818 found)
2819
2820 (condition-case err
2821 (while
2822 (and
2823 (progn
2824 (setq search-pos (point))
2825 (re-search-forward regexp bound noerror))
2826
2827 (progn
2828 (setq state (parse-partial-sexp
2829 state-pos (match-beginning 0) paren-level nil state)
2830 state-pos (point))
2831 (if (setq check-pos (and lookbehind-submatch
2832 (or (not paren-level)
2833 (>= (car state) 0))
2834 (match-end lookbehind-submatch)))
2835 (setq check-state (parse-partial-sexp
2836 state-pos check-pos paren-level nil state))
2837 (setq check-pos state-pos
2838 check-state state))
2839
2840 ;; NOTE: If we got a look behind subexpression and get
2841 ;; an insignificant match in something that isn't
2842 ;; syntactic whitespace (i.e. strings or in nested
2843 ;; parentheses), then we can never skip more than a
2844 ;; single character from the match start position
2845 ;; (i.e. `state-pos' here) before continuing the
2846 ;; search. That since the look behind subexpression
2847 ;; might match the end of the insignificant region in
2848 ;; the next search.
2849
2850 (cond
2851 ((elt check-state 7)
2852 ;; Match inside a line comment. Skip to eol. Use
2853 ;; `re-search-forward' instead of `skip-chars-forward' to get
2854 ;; the right bound behavior.
2855 (re-search-forward "[\n\r]" bound noerror))
2856
2857 ((elt check-state 4)
2858 ;; Match inside a block comment. Skip to the '*/'.
2859 (search-forward "*/" bound noerror))
2860
2861 ((and (not (elt check-state 5))
2862 (eq (char-before check-pos) ?/)
2863 (not (c-get-char-property (1- check-pos) 'syntax-table))
2864 (memq (char-after check-pos) '(?/ ?*)))
2865 ;; Match in the middle of the opener of a block or line
2866 ;; comment.
2867 (if (= (char-after check-pos) ?/)
2868 (re-search-forward "[\n\r]" bound noerror)
2869 (search-forward "*/" bound noerror)))
2870
2871 ;; The last `parse-partial-sexp' above might have
2872 ;; stopped short of the real check position if the end
2873 ;; of the current sexp was encountered in paren-level
2874 ;; mode. The checks above are always false in that
2875 ;; case, and since they can do better skipping in
2876 ;; lookbehind-submatch mode, we do them before
2877 ;; checking the paren level.
2878
2879 ((and paren-level
2880 (/= (setq tmp (car check-state)) 0))
2881 ;; Check the paren level first since we're short of the
2882 ;; syntactic checking position if the end of the
2883 ;; current sexp was encountered by `parse-partial-sexp'.
2884 (if (> tmp 0)
2885
2886 ;; Inside a nested paren sexp.
2887 (if lookbehind-submatch
2888 ;; See the NOTE above.
2889 (progn (goto-char state-pos) t)
2890 ;; Skip out of the paren quickly.
2891 (setq state (parse-partial-sexp state-pos bound 0 nil state)
2892 state-pos (point)))
2893
2894 ;; Have exited the current paren sexp.
2895 (if noerror
2896 (progn
2897 ;; The last `parse-partial-sexp' call above
2898 ;; has left us just after the closing paren
2899 ;; in this case, so we can modify the bound
2900 ;; to leave the point at the right position
2901 ;; upon return.
2902 (setq bound (1- (point)))
2903 nil)
2904 (signal 'search-failed (list regexp)))))
2905
2906 ((setq tmp (elt check-state 3))
2907 ;; Match inside a string.
2908 (if (or lookbehind-submatch
2909 (not (integerp tmp)))
2910 ;; See the NOTE above.
2911 (progn (goto-char state-pos) t)
2912 ;; Skip to the end of the string before continuing.
2913 (let ((ender (make-string 1 tmp)) (continue t))
2914 (while (if (search-forward ender bound noerror)
2915 (progn
2916 (setq state (parse-partial-sexp
2917 state-pos (point) nil nil state)
2918 state-pos (point))
2919 (elt state 3))
2920 (setq continue nil)))
2921 continue)))
2922
2923 ((save-excursion
2924 (save-match-data
2925 (c-beginning-of-macro start)))
2926 ;; Match inside a macro. Skip to the end of it.
2927 (c-end-of-macro)
2928 (cond ((<= (point) bound) t)
2929 (noerror nil)
2930 (t (signal 'search-failed (list regexp)))))
2931
2932 ((and not-inside-token
2933 (or (< check-pos last-token-end-pos)
2934 (< check-pos
2935 (save-excursion
2936 (goto-char check-pos)
2937 (save-match-data
2938 (c-end-of-current-token last-token-end-pos))
2939 (setq last-token-end-pos (point))))))
2940 ;; Inside a token.
2941 (if lookbehind-submatch
2942 ;; See the NOTE above.
2943 (goto-char state-pos)
2944 (goto-char (min last-token-end-pos bound))))
2945
2946 (t
2947 ;; A real match.
2948 (setq found t)
2949 nil)))
2950
2951 ;; Should loop to search again, but take care to avoid
2952 ;; looping on the same spot.
2953 (or (/= search-pos (point))
2954 (if (= (point) bound)
2955 (if noerror
2956 nil
2957 (signal 'search-failed (list regexp)))
2958 (forward-char)
2959 t))))
2960
2961 (error
2962 (goto-char start)
2963 (signal (car err) (cdr err))))
2964
2965 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2966
2967 (if found
2968 (progn
2969 (goto-char (match-end 0))
2970 (match-end 0))
2971
2972 ;; Search failed. Set point as appropriate.
2973 (if (eq noerror t)
2974 (goto-char start)
2975 (goto-char bound))
2976 nil)))
2977
2978 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
2979
2980 (defsubst c-ssb-lit-begin ()
2981 ;; Return the start of the literal point is in, or nil.
2982 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
2983 ;; bound in the caller.
2984
2985 ;; Use `parse-partial-sexp' from a safe position down to the point to check
2986 ;; if it's outside comments and strings.
2987 (save-excursion
2988 (let ((pos (point)) safe-pos state pps-end-pos)
2989 ;; Pick a safe position as close to the point as possible.
2990 ;;
2991 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
2992 ;; position.
2993
2994 (while (and safe-pos-list
2995 (> (car safe-pos-list) (point)))
2996 (setq safe-pos-list (cdr safe-pos-list)))
2997 (unless (setq safe-pos (car-safe safe-pos-list))
2998 (setq safe-pos (max (or (c-safe-position
2999 (point) (or c-state-cache
3000 (c-parse-state)))
3001 0)
3002 (point-min))
3003 safe-pos-list (list safe-pos)))
3004
3005 ;; Cache positions along the way to use if we have to back up more. We
3006 ;; cache every closing paren on the same level. If the paren cache is
3007 ;; relevant in this region then we're typically already on the same
3008 ;; level as the target position. Note that we might cache positions
3009 ;; after opening parens in case safe-pos is in a nested list. That's
3010 ;; both uncommon and harmless.
3011 (while (progn
3012 (setq state (parse-partial-sexp
3013 safe-pos pos 0))
3014 (< (point) pos))
3015 (setq safe-pos (point)
3016 safe-pos-list (cons safe-pos safe-pos-list)))
3017
3018 ;; If the state contains the start of the containing sexp we cache that
3019 ;; position too, so that parse-partial-sexp in the next run has a bigger
3020 ;; chance of starting at the same level as the target position and thus
3021 ;; will get more good safe positions into the list.
3022 (if (elt state 1)
3023 (setq safe-pos (1+ (elt state 1))
3024 safe-pos-list (cons safe-pos safe-pos-list)))
3025
3026 (if (or (elt state 3) (elt state 4))
3027 ;; Inside string or comment. Continue search at the
3028 ;; beginning of it.
3029 (elt state 8)))))
3030
3031 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3032 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3033 i.e. don't stop at positions inside syntactic whitespace or string
3034 literals. Preprocessor directives are also ignored, with the exception
3035 of the one that the point starts within, if any. If LIMIT is given,
3036 it's assumed to be at a syntactically relevant position.
3037
3038 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3039 sexps, and the search will also not go outside the current paren sexp.
3040 However, if LIMIT or the buffer limit is reached inside a nested paren
3041 then the point will be left at the limit.
3042
3043 Non-nil is returned if the point moved, nil otherwise.
3044
3045 Note that this function might do hidden buffer changes. See the
3046 comment at the start of cc-engine.el for more info."
3047
3048 (let ((start (point))
3049 state-2
3050 ;; A list of syntactically relevant positions in descending
3051 ;; order. It's used to avoid scanning repeatedly over
3052 ;; potentially large regions with `parse-partial-sexp' to verify
3053 ;; each position. Used in `c-ssb-lit-begin'
3054 safe-pos-list
3055 ;; The result from `c-beginning-of-macro' at the start position or the
3056 ;; start position itself if it isn't within a macro. Evaluated on
3057 ;; demand.
3058 start-macro-beg
3059 ;; The earliest position after the current one with the same paren
3060 ;; level. Used only when `paren-level' is set.
3061 lit-beg
3062 (paren-level-pos (point)))
3063
3064 (while
3065 (progn
3066 ;; The next loop "tries" to find the end point each time round,
3067 ;; loops when it hasn't succeeded.
3068 (while
3069 (and
3070 (< (skip-chars-backward skip-chars limit) 0)
3071
3072 (let ((pos (point)) state-2 pps-end-pos)
3073
3074 (cond
3075 ;; Don't stop inside a literal
3076 ((setq lit-beg (c-ssb-lit-begin))
3077 (goto-char lit-beg)
3078 t)
3079
3080 ((and paren-level
3081 (save-excursion
3082 (setq state-2 (parse-partial-sexp
3083 pos paren-level-pos -1)
3084 pps-end-pos (point))
3085 (/= (car state-2) 0)))
3086 ;; Not at the right level.
3087
3088 (if (and (< (car state-2) 0)
3089 ;; We stop above if we go out of a paren.
3090 ;; Now check whether it precedes or is
3091 ;; nested in the starting sexp.
3092 (save-excursion
3093 (setq state-2
3094 (parse-partial-sexp
3095 pps-end-pos paren-level-pos
3096 nil nil state-2))
3097 (< (car state-2) 0)))
3098
3099 ;; We've stopped short of the starting position
3100 ;; so the hit was inside a nested list. Go up
3101 ;; until we are at the right level.
3102 (condition-case nil
3103 (progn
3104 (goto-char (scan-lists pos -1
3105 (- (car state-2))))
3106 (setq paren-level-pos (point))
3107 (if (and limit (>= limit paren-level-pos))
3108 (progn
3109 (goto-char limit)
3110 nil)
3111 t))
3112 (error
3113 (goto-char (or limit (point-min)))
3114 nil))
3115
3116 ;; The hit was outside the list at the start
3117 ;; position. Go to the start of the list and exit.
3118 (goto-char (1+ (elt state-2 1)))
3119 nil))
3120
3121 ((c-beginning-of-macro limit)
3122 ;; Inside a macro.
3123 (if (< (point)
3124 (or start-macro-beg
3125 (setq start-macro-beg
3126 (save-excursion
3127 (goto-char start)
3128 (c-beginning-of-macro limit)
3129 (point)))))
3130 t
3131
3132 ;; It's inside the same macro we started in so it's
3133 ;; a relevant match.
3134 (goto-char pos)
3135 nil))))))
3136
3137 (> (point)
3138 (progn
3139 ;; Skip syntactic ws afterwards so that we don't stop at the
3140 ;; end of a comment if `skip-chars' is something like "^/".
3141 (c-backward-syntactic-ws)
3142 (point)))))
3143
3144 ;; We might want to extend this with more useful return values in
3145 ;; the future.
3146 (/= (point) start)))
3147
3148 ;; The following is an alternative implementation of
3149 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3150 ;; track of the syntactic context. It turned out to be generally
3151 ;; slower than the one above which uses forward checks from earlier
3152 ;; safe positions.
3153 ;;
3154 ;;(defconst c-ssb-stop-re
3155 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3156 ;; ;; stop at to avoid going into comments and literals.
3157 ;; (concat
3158 ;; ;; Match comment end syntax and string literal syntax. Also match
3159 ;; ;; '/' for block comment endings (not covered by comment end
3160 ;; ;; syntax).
3161 ;; "\\s>\\|/\\|\\s\""
3162 ;; (if (memq 'gen-string-delim c-emacs-features)
3163 ;; "\\|\\s|"
3164 ;; "")
3165 ;; (if (memq 'gen-comment-delim c-emacs-features)
3166 ;; "\\|\\s!"
3167 ;; "")))
3168 ;;
3169 ;;(defconst c-ssb-stop-paren-re
3170 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3171 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3172 ;;
3173 ;;(defconst c-ssb-sexp-end-re
3174 ;; ;; Regexp matching the ending syntax of a complex sexp.
3175 ;; (concat c-string-limit-regexp "\\|\\s)"))
3176 ;;
3177 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3178 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3179 ;;i.e. don't stop at positions inside syntactic whitespace or string
3180 ;;literals. Preprocessor directives are also ignored. However, if the
3181 ;;point is within a comment, string literal or preprocessor directory to
3182 ;;begin with, its contents is treated as syntactically relevant chars.
3183 ;;If LIMIT is given, it limits the backward search and the point will be
3184 ;;left there if no earlier position is found.
3185 ;;
3186 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3187 ;;sexps, and the search will also not go outside the current paren sexp.
3188 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3189 ;;then the point will be left at the limit.
3190 ;;
3191 ;;Non-nil is returned if the point moved, nil otherwise.
3192 ;;
3193 ;;Note that this function might do hidden buffer changes. See the
3194 ;;comment at the start of cc-engine.el for more info."
3195 ;;
3196 ;; (save-restriction
3197 ;; (when limit
3198 ;; (narrow-to-region limit (point-max)))
3199 ;;
3200 ;; (let ((start (point)))
3201 ;; (catch 'done
3202 ;; (while (let ((last-pos (point))
3203 ;; (stop-pos (progn
3204 ;; (skip-chars-backward skip-chars)
3205 ;; (point))))
3206 ;;
3207 ;; ;; Skip back over the same region as
3208 ;; ;; `skip-chars-backward' above, but keep to
3209 ;; ;; syntactically relevant positions.
3210 ;; (goto-char last-pos)
3211 ;; (while (and
3212 ;; ;; `re-search-backward' with a single char regexp
3213 ;; ;; should be fast.
3214 ;; (re-search-backward
3215 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3216 ;; stop-pos 'move)
3217 ;;
3218 ;; (progn
3219 ;; (cond
3220 ;; ((looking-at "\\s(")
3221 ;; ;; `paren-level' is set and we've found the
3222 ;; ;; start of the containing paren.
3223 ;; (forward-char)
3224 ;; (throw 'done t))
3225 ;;
3226 ;; ((looking-at c-ssb-sexp-end-re)
3227 ;; ;; We're at the end of a string literal or paren
3228 ;; ;; sexp (if `paren-level' is set).
3229 ;; (forward-char)
3230 ;; (condition-case nil
3231 ;; (c-backward-sexp)
3232 ;; (error
3233 ;; (goto-char limit)
3234 ;; (throw 'done t))))
3235 ;;
3236 ;; (t
3237 ;; (forward-char)
3238 ;; ;; At the end of some syntactic ws or possibly
3239 ;; ;; after a plain '/' operator.
3240 ;; (let ((pos (point)))
3241 ;; (c-backward-syntactic-ws)
3242 ;; (if (= pos (point))
3243 ;; ;; Was a plain '/' operator. Go past it.
3244 ;; (backward-char)))))
3245 ;;
3246 ;; (> (point) stop-pos))))
3247 ;;
3248 ;; ;; Now the point is either at `stop-pos' or at some
3249 ;; ;; position further back if `stop-pos' was at a
3250 ;; ;; syntactically irrelevant place.
3251 ;;
3252 ;; ;; Skip additional syntactic ws so that we don't stop
3253 ;; ;; at the end of a comment if `skip-chars' is
3254 ;; ;; something like "^/".
3255 ;; (c-backward-syntactic-ws)
3256 ;;
3257 ;; (< (point) stop-pos))))
3258 ;;
3259 ;; ;; We might want to extend this with more useful return values
3260 ;; ;; in the future.
3261 ;; (/= (point) start))))
3262
3263 \f
3264 ;; Tools for handling comments and string literals.
3265
3266 (defun c-slow-in-literal (&optional lim detect-cpp)
3267 "Return the type of literal point is in, if any.
3268 The return value is `c' if in a C-style comment, `c++' if in a C++
3269 style comment, `string' if in a string literal, `pound' if DETECT-CPP
3270 is non-nil and in a preprocessor line, or nil if somewhere else.
3271 Optional LIM is used as the backward limit of the search. If omitted,
3272 or nil, `c-beginning-of-defun' is used.
3273
3274 The last point calculated is cached if the cache is enabled, i.e. if
3275 `c-in-literal-cache' is bound to a two element vector.
3276
3277 Note that this function might do hidden buffer changes. See the
3278 comment at the start of cc-engine.el for more info."
3279
3280 (if (and (vectorp c-in-literal-cache)
3281 (= (point) (aref c-in-literal-cache 0)))
3282 (aref c-in-literal-cache 1)
3283 (let ((rtn (save-excursion
3284 (let* ((pos (point))
3285 (lim (or lim (progn
3286 (c-beginning-of-syntax)
3287 (point))))
3288 (state (parse-partial-sexp lim pos)))
3289 (cond
3290 ((elt state 3) 'string)
3291 ((elt state 4) (if (elt state 7) 'c++ 'c))
3292 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
3293 (t nil))))))
3294 ;; cache this result if the cache is enabled
3295 (if (not c-in-literal-cache)
3296 (setq c-in-literal-cache (vector (point) rtn)))
3297 rtn)))
3298
3299 ;; XEmacs has a built-in function that should make this much quicker.
3300 ;; I don't think we even need the cache, which makes our lives more
3301 ;; complicated anyway. In this case, lim is only used to detect
3302 ;; cpp directives.
3303 ;;
3304 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
3305 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
3306 ;; Xemacs 21.4.4. It manifested itself thus:
3307 ;;
3308 ;; Starting with an empty AWK Mode buffer, type
3309 ;; /regexp/ {<C-j>
3310 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
3311 ;;
3312 ;; AWK Mode is designed such that when the first / is typed, it gets the
3313 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
3314 ;; are given the s-t property "string". However, buffer-syntactic-context
3315 ;; fails to take account of the change of the s-t property on the opening / to
3316 ;; "string", and reports that the { is within a string started by the second /.
3317 ;;
3318 ;; The workaround for this is for the AWK Mode initialisation to switch the
3319 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
3320 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
3321 ;;
3322 ;; (Alan Mackenzie, 2003/4/30).
3323
3324 (defun c-fast-in-literal (&optional lim detect-cpp)
3325 ;; This function might do hidden buffer changes.
3326 (let ((context (buffer-syntactic-context)))
3327 (cond
3328 ((eq context 'string) 'string)
3329 ((eq context 'comment) 'c++)
3330 ((eq context 'block-comment) 'c)
3331 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
3332
3333 (defalias 'c-in-literal
3334 (if (fboundp 'buffer-syntactic-context)
3335 'c-fast-in-literal ; XEmacs
3336 'c-slow-in-literal)) ; GNU Emacs
3337
3338 ;; The defalias above isn't enough to shut up the byte compiler.
3339 (cc-bytecomp-defun c-in-literal)
3340
3341 (defun c-literal-limits (&optional lim near not-in-delimiter)
3342 "Return a cons of the beginning and end positions of the comment or
3343 string surrounding point (including both delimiters), or nil if point
3344 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
3345 to start parsing from. If NEAR is non-nil, then the limits of any
3346 literal next to point is returned. \"Next to\" means there's only
3347 spaces and tabs between point and the literal. The search for such a
3348 literal is done first in forward direction. If NOT-IN-DELIMITER is
3349 non-nil, the case when point is inside a starting delimiter won't be
3350 recognized. This only has effect for comments which have starting
3351 delimiters with more than one character.
3352
3353 Note that this function might do hidden buffer changes. See the
3354 comment at the start of cc-engine.el for more info."
3355
3356 (save-excursion
3357 (let* ((pos (point))
3358 (lim (or lim (progn
3359 (c-beginning-of-syntax)
3360 (point))))
3361 (state (parse-partial-sexp lim pos)))
3362
3363 (cond ((elt state 3) ; String.
3364 (goto-char (elt state 8))
3365 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3366 (point-max))))
3367
3368 ((elt state 4) ; Comment.
3369 (goto-char (elt state 8))
3370 (cons (point) (progn (c-forward-single-comment) (point))))
3371
3372 ((and (not not-in-delimiter)
3373 (not (elt state 5))
3374 (eq (char-before) ?/)
3375 (looking-at "[/*]"))
3376 ;; We're standing in a comment starter.
3377 (backward-char 1)
3378 (cons (point) (progn (c-forward-single-comment) (point))))
3379
3380 (near
3381 (goto-char pos)
3382
3383 ;; Search forward for a literal.
3384 (skip-chars-forward " \t")
3385
3386 (cond
3387 ((looking-at c-string-limit-regexp) ; String.
3388 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3389 (point-max))))
3390
3391 ((looking-at c-comment-start-regexp) ; Line or block comment.
3392 (cons (point) (progn (c-forward-single-comment) (point))))
3393
3394 (t
3395 ;; Search backward.
3396 (skip-chars-backward " \t")
3397
3398 (let ((end (point)) beg)
3399 (cond
3400 ((save-excursion
3401 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
3402 (setq beg (c-safe (c-backward-sexp 1) (point))))
3403
3404 ((and (c-safe (forward-char -2) t)
3405 (looking-at "*/"))
3406 ;; Block comment. Due to the nature of line
3407 ;; comments, they will always be covered by the
3408 ;; normal case above.
3409 (goto-char end)
3410 (c-backward-single-comment)
3411 ;; If LIM is bogus, beg will be bogus.
3412 (setq beg (point))))
3413
3414 (if beg (cons beg end))))))
3415 ))))
3416
3417 ;; In case external callers use this; it did have a docstring.
3418 (defalias 'c-literal-limits-fast 'c-literal-limits)
3419
3420 (defun c-collect-line-comments (range)
3421 "If the argument is a cons of two buffer positions (such as returned by
3422 `c-literal-limits'), and that range contains a C++ style line comment,
3423 then an extended range is returned that contains all adjacent line
3424 comments (i.e. all comments that starts in the same column with no
3425 empty lines or non-whitespace characters between them). Otherwise the
3426 argument is returned.
3427
3428 Note that this function might do hidden buffer changes. See the
3429 comment at the start of cc-engine.el for more info."
3430
3431 (save-excursion
3432 (condition-case nil
3433 (if (and (consp range) (progn
3434 (goto-char (car range))
3435 (looking-at c-line-comment-starter)))
3436 (let ((col (current-column))
3437 (beg (point))
3438 (bopl (c-point 'bopl))
3439 (end (cdr range)))
3440 ;; Got to take care in the backward direction to handle
3441 ;; comments which are preceded by code.
3442 (while (and (c-backward-single-comment)
3443 (>= (point) bopl)
3444 (looking-at c-line-comment-starter)
3445 (= col (current-column)))
3446 (setq beg (point)
3447 bopl (c-point 'bopl)))
3448 (goto-char end)
3449 (while (and (progn (skip-chars-forward " \t")
3450 (looking-at c-line-comment-starter))
3451 (= col (current-column))
3452 (prog1 (zerop (forward-line 1))
3453 (setq end (point)))))
3454 (cons beg end))
3455 range)
3456 (error range))))
3457
3458 (defun c-literal-type (range)
3459 "Convenience function that given the result of `c-literal-limits',
3460 returns nil or the type of literal that the range surrounds, one
3461 of the symbols 'c, 'c++ or 'string. It's much faster than using
3462 `c-in-literal' and is intended to be used when you need both the
3463 type of a literal and its limits.
3464
3465 Note that this function might do hidden buffer changes. See the
3466 comment at the start of cc-engine.el for more info."
3467
3468 (if (consp range)
3469 (save-excursion
3470 (goto-char (car range))
3471 (cond ((looking-at c-string-limit-regexp) 'string)
3472 ((or (looking-at "//") ; c++ line comment
3473 (and (looking-at "\\s<") ; comment starter
3474 (looking-at "#"))) ; awk comment.
3475 'c++)
3476 (t 'c))) ; Assuming the range is valid.
3477 range))
3478
3479 \f
3480 ;; `c-find-decl-spots' and accompanying stuff.
3481
3482 ;; Variables used in `c-find-decl-spots' to cache the search done for
3483 ;; the first declaration in the last call. When that function starts,
3484 ;; it needs to back up over syntactic whitespace to look at the last
3485 ;; token before the region being searched. That can sometimes cause
3486 ;; moves back and forth over a quite large region of comments and
3487 ;; macros, which would be repeated for each changed character when
3488 ;; we're called during fontification, since font-lock refontifies the
3489 ;; current line for each change. Thus it's worthwhile to cache the
3490 ;; first match.
3491 ;;
3492 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3493 ;; the syntactic whitespace less or equal to some start position.
3494 ;; There's no cached value if it's nil.
3495 ;;
3496 ;; `c-find-decl-match-pos' is the match position if
3497 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3498 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3499 (defvar c-find-decl-syntactic-pos nil)
3500 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
3501 (defvar c-find-decl-match-pos nil)
3502 (make-variable-buffer-local 'c-find-decl-match-pos)
3503
3504 (defsubst c-invalidate-find-decl-cache (change-min-pos)
3505 (and c-find-decl-syntactic-pos
3506 (< change-min-pos c-find-decl-syntactic-pos)
3507 (setq c-find-decl-syntactic-pos nil)))
3508
3509 ; (defface c-debug-decl-spot-face
3510 ; '((t (:background "Turquoise")))
3511 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3512 ; (defface c-debug-decl-sws-face
3513 ; '((t (:background "Khaki")))
3514 ; "Debug face to mark the syntactic whitespace between the declaration
3515 ; spots and the preceding token end.")
3516
3517 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
3518 (when (facep 'c-debug-decl-spot-face)
3519 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
3520 (c-debug-add-face (max match-pos (point-min)) decl-pos
3521 'c-debug-decl-sws-face)
3522 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
3523 'c-debug-decl-spot-face))))
3524 (defmacro c-debug-remove-decl-spot-faces (beg end)
3525 (when (facep 'c-debug-decl-spot-face)
3526 `(c-save-buffer-state ()
3527 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
3528 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
3529
3530 (defmacro c-find-decl-prefix-search ()
3531 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3532 ;; but it contains lots of free variables that refer to things
3533 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3534 ;; if there is a match, otherwise at `cfd-limit'.
3535 ;;
3536 ;; This macro might do hidden buffer changes.
3537
3538 '(progn
3539 ;; Find the next property match position if we haven't got one already.
3540 (unless cfd-prop-match
3541 (save-excursion
3542 (while (progn
3543 (goto-char (next-single-property-change
3544 (point) 'c-type nil cfd-limit))
3545 (and (< (point) cfd-limit)
3546 (not (eq (c-get-char-property (1- (point)) 'c-type)
3547 'c-decl-end)))))
3548 (setq cfd-prop-match (point))))
3549
3550 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
3551 ;; got one already.
3552 (unless cfd-re-match
3553
3554 (if (> cfd-re-match-end (point))
3555 (goto-char cfd-re-match-end))
3556
3557 (while (if (setq cfd-re-match-end
3558 (re-search-forward c-decl-prefix-or-start-re
3559 cfd-limit 'move))
3560
3561 ;; Match. Check if it's inside a comment or string literal.
3562 (c-got-face-at
3563 (if (setq cfd-re-match (match-end 1))
3564 ;; Matched the end of a token preceding a decl spot.
3565 (progn
3566 (goto-char cfd-re-match)
3567 (1- cfd-re-match))
3568 ;; Matched a token that start a decl spot.
3569 (goto-char (match-beginning 0))
3570 (point))
3571 c-literal-faces)
3572
3573 ;; No match. Finish up and exit the loop.
3574 (setq cfd-re-match cfd-limit)
3575 nil)
3576
3577 ;; Skip out of comments and string literals.
3578 (while (progn
3579 (goto-char (next-single-property-change
3580 (point) 'face nil cfd-limit))
3581 (and (< (point) cfd-limit)
3582 (c-got-face-at (point) c-literal-faces)))))
3583
3584 ;; If we matched at the decl start, we have to back up over the
3585 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
3586 ;; any decl spots in the syntactic ws.
3587 (unless cfd-re-match
3588 (c-backward-syntactic-ws)
3589 (setq cfd-re-match (point))))
3590
3591 ;; Choose whichever match is closer to the start.
3592 (if (< cfd-re-match cfd-prop-match)
3593 (setq cfd-match-pos cfd-re-match
3594 cfd-re-match nil)
3595 (setq cfd-match-pos cfd-prop-match
3596 cfd-prop-match nil))
3597
3598 (goto-char cfd-match-pos)
3599
3600 (when (< cfd-match-pos cfd-limit)
3601 ;; Skip forward past comments only so we don't skip macros.
3602 (c-forward-comments)
3603 ;; Set the position to continue at. We can avoid going over
3604 ;; the comments skipped above a second time, but it's possible
3605 ;; that the comment skipping has taken us past `cfd-prop-match'
3606 ;; since the property might be used inside comments.
3607 (setq cfd-continue-pos (if cfd-prop-match
3608 (min cfd-prop-match (point))
3609 (point))))))
3610
3611 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3612 ;; Call CFD-FUN for each possible spot for a declaration, cast or
3613 ;; label from the point to CFD-LIMIT.
3614 ;;
3615 ;; CFD-FUN is called with point at the start of the spot. It's
3616 ;; passed two arguments: The first is the end position of the token
3617 ;; preceding the spot, or 0 for the implicit match at bob. The
3618 ;; second is a flag that is t when the match is inside a macro. If
3619 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
3620 ;; spot, it should return non-nil to ensure that the next search
3621 ;; will find them.
3622 ;;
3623 ;; Such a spot is:
3624 ;; o The first token after bob.
3625 ;; o The first token after the end of submatch 1 in
3626 ;; `c-decl-prefix-or-start-re' when that submatch matches.
3627 ;; o The start of each `c-decl-prefix-or-start-re' match when
3628 ;; submatch 1 doesn't match.
3629 ;; o The first token after the end of each occurrence of the
3630 ;; `c-type' text property with the value `c-decl-end', provided
3631 ;; `c-type-decl-end-used' is set.
3632 ;;
3633 ;; Only a spot that match CFD-DECL-RE and whose face is in the
3634 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
3635 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
3636 ;;
3637 ;; If the match is inside a macro then the buffer is narrowed to the
3638 ;; end of it, so that CFD-FUN can investigate the following tokens
3639 ;; without matching something that begins inside a macro and ends
3640 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3641 ;; CFD-FACE-CHECKLIST checks exist.
3642 ;;
3643 ;; The spots are visited approximately in order from top to bottom.
3644 ;; It's however the positions where `c-decl-prefix-or-start-re'
3645 ;; matches and where `c-decl-end' properties are found that are in
3646 ;; order. Since the spots often are at the following token, they
3647 ;; might be visited out of order insofar as more spots are reported
3648 ;; later on within the syntactic whitespace between the match
3649 ;; positions and their spots.
3650 ;;
3651 ;; It's assumed that comments and strings are fontified in the
3652 ;; searched range.
3653 ;;
3654 ;; This is mainly used in fontification, and so has an elaborate
3655 ;; cache to handle repeated calls from the same start position; see
3656 ;; the variables above.
3657 ;;
3658 ;; All variables in this function begin with `cfd-' to avoid name
3659 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3660 ;;
3661 ;; This function might do hidden buffer changes.
3662
3663 (let ((cfd-start-pos (point))
3664 (cfd-buffer-end (point-max))
3665 ;; The end of the token preceding the decl spot last found
3666 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
3667 ;; no match.
3668 cfd-re-match
3669 ;; The end position of the last `c-decl-prefix-or-start-re'
3670 ;; match. If this is greater than `cfd-continue-pos', the
3671 ;; next regexp search is started here instead.
3672 (cfd-re-match-end (point-min))
3673 ;; The end of the last `c-decl-end' found by
3674 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
3675 ;; match. If searching for the property isn't needed then we
3676 ;; disable it by setting it to `cfd-limit' directly.
3677 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3678 ;; The end of the token preceding the decl spot last found by
3679 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
3680 ;; bob. `cfd-limit' if there's no match. In other words,
3681 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
3682 (cfd-match-pos cfd-limit)
3683 ;; The position to continue searching at.
3684 cfd-continue-pos
3685 ;; The position of the last "real" token we've stopped at.
3686 ;; This can be greater than `cfd-continue-pos' when we get
3687 ;; hits inside macros or at `c-decl-end' positions inside
3688 ;; comments.
3689 (cfd-token-pos 0)
3690 ;; The end position of the last entered macro.
3691 (cfd-macro-end 0))
3692
3693 ;; Initialize by finding a syntactically relevant start position
3694 ;; before the point, and do the first `c-decl-prefix-or-start-re'
3695 ;; search unless we're at bob.
3696
3697 (let (start-in-literal start-in-macro syntactic-pos)
3698 ;; Must back up a bit since we look for the end of the previous
3699 ;; statement or declaration, which is earlier than the first
3700 ;; returned match.
3701
3702 (cond
3703 ;; First we need to move to a syntactically relevant position.
3704 ;; Begin by backing out of comment or string literals.
3705 ((and
3706 (when (c-got-face-at (point) c-literal-faces)
3707 ;; Try to use the faces to back up to the start of the
3708 ;; literal. FIXME: What if the point is on a declaration
3709 ;; inside a comment?
3710 (while (and (not (bobp))
3711 (c-got-face-at (1- (point)) c-literal-faces))
3712 (goto-char (previous-single-property-change
3713 (point) 'face nil (point-min))))
3714
3715 ;; XEmacs doesn't fontify the quotes surrounding string
3716 ;; literals.
3717 (and (featurep 'xemacs)
3718 (eq (get-text-property (point) 'face)
3719 'font-lock-string-face)
3720 (not (bobp))
3721 (progn (backward-char)
3722 (not (looking-at c-string-limit-regexp)))
3723 (forward-char))
3724
3725 ;; Don't trust the literal to contain only literal faces
3726 ;; (the font lock package might not have fontified the
3727 ;; start of it at all, for instance) so check that we have
3728 ;; arrived at something that looks like a start or else
3729 ;; resort to `c-literal-limits'.
3730 (unless (looking-at c-literal-start-regexp)
3731 (let ((range (c-literal-limits)))
3732 (if range (goto-char (car range)))))
3733
3734 (setq start-in-literal (point)))
3735
3736 ;; The start is in a literal. If the limit is in the same
3737 ;; one we don't have to find a syntactic position etc. We
3738 ;; only check that if the limit is at or before bonl to save
3739 ;; time; it covers the by far most common case when font-lock
3740 ;; refontifies the current line only.
3741 (<= cfd-limit (c-point 'bonl cfd-start-pos))
3742 (save-excursion
3743 (goto-char cfd-start-pos)
3744 (while (progn
3745 (goto-char (next-single-property-change
3746 (point) 'face nil cfd-limit))
3747 (and (< (point) cfd-limit)
3748 (c-got-face-at (point) c-literal-faces))))
3749 (= (point) cfd-limit)))
3750
3751 ;; Completely inside a literal. Set up variables to trig the
3752 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
3753 ;; find a suitable start position.
3754 (setq cfd-continue-pos start-in-literal))
3755
3756 ;; Check if the region might be completely inside a macro, to
3757 ;; optimize that like the completely-inside-literal above.
3758 ((save-excursion
3759 (and (= (forward-line 1) 0)
3760 (bolp) ; forward-line has funny behavior at eob.
3761 (>= (point) cfd-limit)
3762 (progn (backward-char)
3763 (eq (char-before) ?\\))))
3764 ;; (Maybe) completely inside a macro. Only need to trig the
3765 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
3766 ;; set things up.
3767 (setq cfd-continue-pos (1- cfd-start-pos)
3768 start-in-macro t))
3769
3770 (t
3771 ;; Back out of any macro so we don't miss any declaration
3772 ;; that could follow after it.
3773 (when (c-beginning-of-macro)
3774 (setq start-in-macro t))
3775
3776 ;; Now we're at a proper syntactically relevant position so we
3777 ;; can use the cache. But first clear it if it applied
3778 ;; further down.
3779 (c-invalidate-find-decl-cache cfd-start-pos)
3780
3781 (setq syntactic-pos (point))
3782 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
3783 ;; Don't have to do this if the cache is relevant here,
3784 ;; typically if the same line is refontified again. If
3785 ;; we're just some syntactic whitespace further down we can
3786 ;; still use the cache to limit the skipping.
3787 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
3788
3789 ;; If we hit `c-find-decl-syntactic-pos' and
3790 ;; `c-find-decl-match-pos' is set then we install the cached
3791 ;; values. If we hit `c-find-decl-syntactic-pos' and
3792 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3793 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3794 ;; and so we can continue the search from this point. If we
3795 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
3796 ;; the right spot to begin searching anyway.
3797 (if (and (eq (point) c-find-decl-syntactic-pos)
3798 c-find-decl-match-pos)
3799 (setq cfd-match-pos c-find-decl-match-pos
3800 cfd-continue-pos syntactic-pos)
3801
3802 (setq c-find-decl-syntactic-pos syntactic-pos)
3803
3804 (when (if (bobp)
3805 ;; Always consider bob a match to get the first
3806 ;; declaration in the file. Do this separately instead of
3807 ;; letting `c-decl-prefix-or-start-re' match bob, so that
3808 ;; regexp always can consume at least one character to
3809 ;; ensure that we won't get stuck in an infinite loop.
3810 (setq cfd-re-match 0)
3811 (backward-char)
3812 (c-beginning-of-current-token)
3813 (< (point) cfd-limit))
3814 ;; Do an initial search now. In the bob case above it's
3815 ;; only done to search for a `c-decl-end' spot.
3816 (c-find-decl-prefix-search))
3817
3818 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
3819 cfd-match-pos)))))
3820
3821 ;; Advance `cfd-continue-pos' if it's before the start position.
3822 ;; The closest continue position that might have effect at or
3823 ;; after the start depends on what we started in. This also
3824 ;; finds a suitable start position in the special cases when the
3825 ;; region is completely within a literal or macro.
3826 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
3827
3828 (cond
3829 (start-in-macro
3830 ;; If we're in a macro then it's the closest preceding token
3831 ;; in the macro. Check this before `start-in-literal',
3832 ;; since if we're inside a literal in a macro, the preceding
3833 ;; token is earlier than any `c-decl-end' spot inside the
3834 ;; literal (comment).
3835 (goto-char (or start-in-literal cfd-start-pos))
3836 ;; The only syntactic ws in macros are comments.
3837 (c-backward-comments)
3838 (backward-char)
3839 (c-beginning-of-current-token))
3840
3841 (start-in-literal
3842 ;; If we're in a comment it can only be the closest
3843 ;; preceding `c-decl-end' position within that comment, if
3844 ;; any. Go back to the beginning of such a property so that
3845 ;; `c-find-decl-prefix-search' will find the end of it.
3846 ;; (Can't stop at the end and install it directly on
3847 ;; `cfd-prop-match' since that variable might be cleared
3848 ;; after `cfd-fun' below.)
3849 ;;
3850 ;; Note that if the literal is a string then the property
3851 ;; search will simply skip to the beginning of it right
3852 ;; away.
3853 (if (not c-type-decl-end-used)
3854 (goto-char start-in-literal)
3855 (goto-char cfd-start-pos)
3856 (while (progn
3857 (goto-char (previous-single-property-change
3858 (point) 'c-type nil start-in-literal))
3859 (and (> (point) start-in-literal)
3860 (not (eq (c-get-char-property (point) 'c-type)
3861 'c-decl-end))))))
3862
3863 (when (= (point) start-in-literal)
3864 ;; Didn't find any property inside the comment, so we can
3865 ;; skip it entirely. (This won't skip past a string, but
3866 ;; that'll be handled quickly by the next
3867 ;; `c-find-decl-prefix-search' anyway.)
3868 (c-forward-single-comment)
3869 (if (> (point) cfd-limit)
3870 (goto-char cfd-limit))))
3871
3872 (t
3873 ;; If we started in normal code, the only match that might
3874 ;; apply before the start is what we already got in
3875 ;; `cfd-match-pos' so we can continue at the start position.
3876 ;; (Note that we don't get here if the first match is below
3877 ;; it.)
3878 (goto-char cfd-start-pos)))
3879
3880 ;; Delete found matches if they are before our new continue
3881 ;; position, so that `c-find-decl-prefix-search' won't back up
3882 ;; to them later on.
3883 (setq cfd-continue-pos (point))
3884 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
3885 (setq cfd-re-match nil))
3886 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
3887 (setq cfd-prop-match nil)))
3888
3889 (if syntactic-pos
3890 ;; This is the normal case and we got a proper syntactic
3891 ;; position. If there's a match then it's always outside
3892 ;; macros and comments, so advance to the next token and set
3893 ;; `cfd-token-pos'. The loop below will later go back using
3894 ;; `cfd-continue-pos' to fix declarations inside the
3895 ;; syntactic ws.
3896 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
3897 (goto-char syntactic-pos)
3898 (c-forward-syntactic-ws)
3899 (and cfd-continue-pos
3900 (< cfd-continue-pos (point))
3901 (setq cfd-token-pos (point))))
3902
3903 ;; Have one of the special cases when the region is completely
3904 ;; within a literal or macro. `cfd-continue-pos' is set to a
3905 ;; good start position for the search, so do it.
3906 (c-find-decl-prefix-search)))
3907
3908 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
3909
3910 (while (progn
3911 (while (and
3912 (< cfd-match-pos cfd-limit)
3913
3914 (or
3915 ;; Kludge to filter out matches on the "<" that
3916 ;; aren't open parens, for the sake of languages
3917 ;; that got `c-recognize-<>-arglists' set.
3918 (and (eq (char-before cfd-match-pos) ?<)
3919 (not (c-get-char-property (1- cfd-match-pos)
3920 'syntax-table)))
3921
3922 ;; If `cfd-continue-pos' is less or equal to
3923 ;; `cfd-token-pos', we've got a hit inside a macro
3924 ;; that's in the syntactic whitespace before the last
3925 ;; "real" declaration we've checked. If they're equal
3926 ;; we've arrived at the declaration a second time, so
3927 ;; there's nothing to do.
3928 (= cfd-continue-pos cfd-token-pos)
3929
3930 (progn
3931 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3932 ;; we're still searching for declarations embedded in
3933 ;; the syntactic whitespace. In that case we need
3934 ;; only to skip comments and not macros, since they
3935 ;; can't be nested, and that's already been done in
3936 ;; `c-find-decl-prefix-search'.
3937 (when (> cfd-continue-pos cfd-token-pos)
3938 (c-forward-syntactic-ws)
3939 (setq cfd-token-pos (point)))
3940
3941 ;; Continue if the following token fails the
3942 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3943 (when (or (>= (point) cfd-limit)
3944 (not (looking-at cfd-decl-re))
3945 (and cfd-face-checklist
3946 (not (c-got-face-at
3947 (point) cfd-face-checklist))))
3948 (goto-char cfd-continue-pos)
3949 t)))
3950
3951 (< (point) cfd-limit))
3952 (c-find-decl-prefix-search))
3953
3954 (< (point) cfd-limit))
3955
3956 (when (and
3957 (>= (point) cfd-start-pos)
3958
3959 (progn
3960 ;; Narrow to the end of the macro if we got a hit inside
3961 ;; one, to avoid recognizing things that start inside the
3962 ;; macro and end outside it.
3963 (when (> cfd-match-pos cfd-macro-end)
3964 ;; Not in the same macro as in the previous round.
3965 (save-excursion
3966 (goto-char cfd-match-pos)
3967 (setq cfd-macro-end
3968 (if (save-excursion (and (c-beginning-of-macro)
3969 (< (point) cfd-match-pos)))
3970 (progn (c-end-of-macro)
3971 (point))
3972 0))))
3973
3974 (if (zerop cfd-macro-end)
3975 t
3976 (if (> cfd-macro-end (point))
3977 (progn (narrow-to-region (point-min) cfd-macro-end)
3978 t)
3979 ;; The matched token was the last thing in the macro,
3980 ;; so the whole match is bogus.
3981 (setq cfd-macro-end 0)
3982 nil))))
3983
3984 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3985 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3986 (setq cfd-prop-match nil))
3987
3988 (when (/= cfd-macro-end 0)
3989 ;; Restore limits if we did macro narrowment above.
3990 (narrow-to-region (point-min) cfd-buffer-end)))
3991
3992 (goto-char cfd-continue-pos)
3993 (if (= cfd-continue-pos cfd-limit)
3994 (setq cfd-match-pos cfd-limit)
3995 (c-find-decl-prefix-search)))))
3996
3997 \f
3998 ;; A cache for found types.
3999
4000 ;; Buffer local variable that contains an obarray with the types we've
4001 ;; found. If a declaration is recognized somewhere we record the
4002 ;; fully qualified identifier in it to recognize it as a type
4003 ;; elsewhere in the file too. This is not accurate since we do not
4004 ;; bother with the scoping rules of the languages, but in practice the
4005 ;; same name is seldom used as both a type and something else in a
4006 ;; file, and we only use this as a last resort in ambiguous cases (see
4007 ;; `c-forward-decl-or-cast-1').
4008 ;;
4009 ;; Not every type need be in this cache. However, things which have
4010 ;; ceased to be types must be removed from it.
4011 ;;
4012 ;; Template types in C++ are added here too but with the template
4013 ;; arglist replaced with "<>" in references or "<" for the one in the
4014 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4015 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4016 ;; template specs can be fairly sized programs in themselves) and
4017 ;; improves the hit ratio (it's a type regardless of the template
4018 ;; args; it's just not the same type, but we're only interested in
4019 ;; recognizing types, not telling distinct types apart). Note that
4020 ;; template types in references are added here too; from the example
4021 ;; above there will also be an entry "Foo<".
4022 (defvar c-found-types nil)
4023 (make-variable-buffer-local 'c-found-types)
4024
4025 (defsubst c-clear-found-types ()
4026 ;; Clears `c-found-types'.
4027 (setq c-found-types (make-vector 53 0)))
4028
4029 (defun c-add-type (from to)
4030 ;; Add the given region as a type in `c-found-types'. If the region
4031 ;; doesn't match an existing type but there is a type which is equal
4032 ;; to the given one except that the last character is missing, then
4033 ;; the shorter type is removed. That's done to avoid adding all
4034 ;; prefixes of a type as it's being entered and font locked. This
4035 ;; doesn't cover cases like when characters are removed from a type
4036 ;; or added in the middle. We'd need the position of point when the
4037 ;; font locking is invoked to solve this well.
4038 ;;
4039 ;; This function might do hidden buffer changes.
4040 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4041 (unless (intern-soft type c-found-types)
4042 (unintern (substring type 0 -1) c-found-types)
4043 (intern type c-found-types))))
4044
4045 (defun c-unfind-type (name)
4046 ;; Remove the "NAME" from c-found-types, if present.
4047 (unintern name c-found-types))
4048
4049 (defsubst c-check-type (from to)
4050 ;; Return non-nil if the given region contains a type in
4051 ;; `c-found-types'.
4052 ;;
4053 ;; This function might do hidden buffer changes.
4054 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4055 c-found-types))
4056
4057 (defun c-list-found-types ()
4058 ;; Return all the types in `c-found-types' as a sorted list of
4059 ;; strings.
4060 (let (type-list)
4061 (mapatoms (lambda (type)
4062 (setq type-list (cons (symbol-name type)
4063 type-list)))
4064 c-found-types)
4065 (sort type-list 'string-lessp)))
4066
4067 ;; Shut up the byte compiler.
4068 (defvar c-maybe-stale-found-type)
4069
4070 (defun c-trim-found-types (beg end old-len)
4071 ;; An after change function which, in conjunction with the info in
4072 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4073 ;; from `c-found-types', should this type have become stale. For
4074 ;; example, this happens to "foo" when "foo \n bar();" becomes
4075 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4076 ;; the fontification.
4077 ;;
4078 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4079 ;; type?
4080 (when (> end beg)
4081 (save-excursion
4082 (when (< end (point-max))
4083 (goto-char end)
4084 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4085 (progn (goto-char end)
4086 (c-end-of-current-token)))
4087 (c-unfind-type (buffer-substring-no-properties
4088 end (point)))))
4089 (when (> beg (point-min))
4090 (goto-char beg)
4091 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4092 (progn (goto-char beg)
4093 (c-beginning-of-current-token)))
4094 (c-unfind-type (buffer-substring-no-properties
4095 (point) beg))))))
4096
4097 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4098 (cond
4099 ;; Changing the amount of (already existing) whitespace - don't do anything.
4100 ((and (c-partial-ws-p beg end)
4101 (or (= beg end) ; removal of WS
4102 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4103
4104 ;; The syntactic relationship which defined a "found type" has been
4105 ;; destroyed.
4106 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4107 (c-unfind-type (cadr c-maybe-stale-found-type)))
4108 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4109 )))
4110
4111 \f
4112 ;; Handling of small scale constructs like types and names.
4113
4114 (defun c-after-change-check-<>-operators (beg end)
4115 ;; This is called from `after-change-functions' when
4116 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
4117 ;; chars with paren syntax become part of another operator like "<<"
4118 ;; or ">=".
4119 ;;
4120 ;; This function might do hidden buffer changes.
4121
4122 (save-excursion
4123 (goto-char beg)
4124 (when (or (looking-at "[<>]")
4125 (< (skip-chars-backward "<>") 0))
4126
4127 (goto-char beg)
4128 (c-beginning-of-current-token)
4129 (when (and (< (point) beg)
4130 (looking-at c-<>-multichar-token-regexp)
4131 (< beg (setq beg (match-end 0))))
4132 (while (progn (skip-chars-forward "^<>" beg)
4133 (< (point) beg))
4134 (c-clear-char-property (point) 'syntax-table)
4135 (forward-char))))
4136
4137 (when (< beg end)
4138 (goto-char end)
4139 (when (or (looking-at "[<>]")
4140 (< (skip-chars-backward "<>") 0))
4141
4142 (goto-char end)
4143 (c-beginning-of-current-token)
4144 (when (and (< (point) end)
4145 (looking-at c-<>-multichar-token-regexp)
4146 (< end (setq end (match-end 0))))
4147 (while (progn (skip-chars-forward "^<>" end)
4148 (< (point) end))
4149 (c-clear-char-property (point) 'syntax-table)
4150 (forward-char)))))))
4151
4152 ;; Dynamically bound variable that instructs `c-forward-type' to also
4153 ;; treat possible types (i.e. those that it normally returns 'maybe or
4154 ;; 'found for) as actual types (and always return 'found for them).
4155 ;; This means that it records them in `c-record-type-identifiers' if
4156 ;; that is set, and that it adds them to `c-found-types'.
4157 (defvar c-promote-possible-types nil)
4158
4159 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4160 ;; mark up successfully parsed arglists with paren syntax properties on
4161 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
4162 ;; `c-type' property of each argument separating comma.
4163 ;;
4164 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
4165 ;; all arglists for side effects (i.e. recording types), otherwise it
4166 ;; exploits any existing paren syntax properties to quickly jump to the
4167 ;; end of already parsed arglists.
4168 ;;
4169 ;; Marking up the arglists is not the default since doing that correctly
4170 ;; depends on a proper value for `c-restricted-<>-arglists'.
4171 (defvar c-parse-and-markup-<>-arglists nil)
4172
4173 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4174 ;; not accept arglists that contain binary operators.
4175 ;;
4176 ;; This is primarily used to handle C++ template arglists. C++
4177 ;; disambiguates them by checking whether the preceding name is a
4178 ;; template or not. We can't do that, so we assume it is a template
4179 ;; if it can be parsed as one. That usually works well since
4180 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
4181 ;; in almost all cases would be pointless.
4182 ;;
4183 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
4184 ;; should let the comma separate the function arguments instead. And
4185 ;; in a context where the value of the expression is taken, e.g. in
4186 ;; "if (a < b || c > d)", it's probably not a template.
4187 (defvar c-restricted-<>-arglists nil)
4188
4189 ;; Dynamically bound variables that instructs
4190 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
4191 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
4192 ;; `c-forward-label' to record the ranges of all the type and
4193 ;; reference identifiers they encounter. They will build lists on
4194 ;; these variables where each element is a cons of the buffer
4195 ;; positions surrounding each identifier. This recording is only
4196 ;; activated when `c-record-type-identifiers' is non-nil.
4197 ;;
4198 ;; All known types that can't be identifiers are recorded, and also
4199 ;; other possible types if `c-promote-possible-types' is set.
4200 ;; Recording is however disabled inside angle bracket arglists that
4201 ;; are encountered inside names and other angle bracket arglists.
4202 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
4203 ;; instead.
4204 ;;
4205 ;; Only the names in C++ template style references (e.g. "tmpl" in
4206 ;; "tmpl<a,b>::foo") are recorded as references, other references
4207 ;; aren't handled here.
4208 ;;
4209 ;; `c-forward-label' records the label identifier(s) on
4210 ;; `c-record-ref-identifiers'.
4211 (defvar c-record-type-identifiers nil)
4212 (defvar c-record-ref-identifiers nil)
4213
4214 ;; This variable will receive a cons cell of the range of the last
4215 ;; single identifier symbol stepped over by `c-forward-name' if it's
4216 ;; successful. This is the range that should be put on one of the
4217 ;; record lists above by the caller. It's assigned nil if there's no
4218 ;; such symbol in the name.
4219 (defvar c-last-identifier-range nil)
4220
4221 (defmacro c-record-type-id (range)
4222 (if (eq (car-safe range) 'cons)
4223 ;; Always true.
4224 `(setq c-record-type-identifiers
4225 (cons ,range c-record-type-identifiers))
4226 `(let ((range ,range))
4227 (if range
4228 (setq c-record-type-identifiers
4229 (cons range c-record-type-identifiers))))))
4230
4231 (defmacro c-record-ref-id (range)
4232 (if (eq (car-safe range) 'cons)
4233 ;; Always true.
4234 `(setq c-record-ref-identifiers
4235 (cons ,range c-record-ref-identifiers))
4236 `(let ((range ,range))
4237 (if range
4238 (setq c-record-ref-identifiers
4239 (cons range c-record-ref-identifiers))))))
4240
4241 ;; Dynamically bound variable that instructs `c-forward-type' to
4242 ;; record the ranges of types that only are found. Behaves otherwise
4243 ;; like `c-record-type-identifiers'.
4244 (defvar c-record-found-types nil)
4245
4246 (defmacro c-forward-keyword-prefixed-id (type)
4247 ;; Used internally in `c-forward-keyword-clause' to move forward
4248 ;; over a type (if TYPE is 'type) or a name (otherwise) which
4249 ;; possibly is prefixed by keywords and their associated clauses.
4250 ;; Try with a type/name first to not trip up on those that begin
4251 ;; with a keyword. Return t if a known or found type is moved
4252 ;; over. The point is clobbered if nil is returned. If range
4253 ;; recording is enabled, the identifier is recorded on as a type
4254 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
4255 ;;
4256 ;; This macro might do hidden buffer changes.
4257 `(let (res)
4258 (while (if (setq res ,(if (eq type 'type)
4259 `(c-forward-type)
4260 `(c-forward-name)))
4261 nil
4262 (and (looking-at c-keywords-regexp)
4263 (c-forward-keyword-clause 1))))
4264 (when (memq res '(t known found prefix))
4265 ,(when (eq type 'ref)
4266 `(when c-record-type-identifiers
4267 (c-record-ref-id c-last-identifier-range)))
4268 t)))
4269
4270 (defmacro c-forward-id-comma-list (type update-safe-pos)
4271 ;; Used internally in `c-forward-keyword-clause' to move forward
4272 ;; over a comma separated list of types or names using
4273 ;; `c-forward-keyword-prefixed-id'.
4274 ;;
4275 ;; This macro might do hidden buffer changes.
4276 `(while (and (progn
4277 ,(when update-safe-pos
4278 `(setq safe-pos (point)))
4279 (eq (char-after) ?,))
4280 (progn
4281 (forward-char)
4282 (c-forward-syntactic-ws)
4283 (c-forward-keyword-prefixed-id ,type)))))
4284
4285 (defun c-forward-keyword-clause (match)
4286 ;; Submatch MATCH in the current match data is assumed to surround a
4287 ;; token. If it's a keyword, move over it and any immediately
4288 ;; following clauses associated with it, stopping at the start of
4289 ;; the next token. t is returned in that case, otherwise the point
4290 ;; stays and nil is returned. The kind of clauses that are
4291 ;; recognized are those specified by `c-type-list-kwds',
4292 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
4293 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
4294 ;; and `c-<>-arglist-kwds'.
4295 ;;
4296 ;; This function records identifier ranges on
4297 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4298 ;; `c-record-type-identifiers' is non-nil.
4299 ;;
4300 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
4301 ;; apply directly after the keyword, the type list is moved over
4302 ;; only when there is no unaccounted token before it (i.e. a token
4303 ;; that isn't moved over due to some other keyword list). The
4304 ;; identifier ranges in the list are still recorded if that should
4305 ;; be done, though.
4306 ;;
4307 ;; This function might do hidden buffer changes.
4308
4309 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
4310 ;; The call to `c-forward-<>-arglist' below is made after
4311 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
4312 ;; are angle bracket arglists and `c-restricted-<>-arglists'
4313 ;; should therefore be nil.
4314 (c-parse-and-markup-<>-arglists t)
4315 c-restricted-<>-arglists)
4316
4317 (when kwd-sym
4318 (goto-char (match-end match))
4319 (c-forward-syntactic-ws)
4320 (setq safe-pos (point))
4321
4322 (cond
4323 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
4324 (c-forward-keyword-prefixed-id type))
4325 ;; There's a type directly after a keyword in `c-type-list-kwds'.
4326 (c-forward-id-comma-list type t))
4327
4328 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
4329 (c-forward-keyword-prefixed-id ref))
4330 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
4331 (c-forward-id-comma-list ref t))
4332
4333 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
4334 (eq (char-after) ?\())
4335 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
4336
4337 (forward-char)
4338 (when (and (setq pos (c-up-list-forward))
4339 (eq (char-before pos) ?\)))
4340 (when (and c-record-type-identifiers
4341 (c-keyword-member kwd-sym 'c-paren-type-kwds))
4342 ;; Use `c-forward-type' on every identifier we can find
4343 ;; inside the paren, to record the types.
4344 (while (c-syntactic-re-search-forward c-symbol-start pos t)
4345 (goto-char (match-beginning 0))
4346 (unless (c-forward-type)
4347 (looking-at c-symbol-key) ; Always matches.
4348 (goto-char (match-end 0)))))
4349
4350 (goto-char pos)
4351 (c-forward-syntactic-ws)
4352 (setq safe-pos (point))))
4353
4354 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
4355 (eq (char-after) ?<)
4356 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
4357 (c-forward-syntactic-ws)
4358 (setq safe-pos (point)))
4359
4360 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
4361 (not (looking-at c-symbol-start))
4362 (c-safe (c-forward-sexp) t))
4363 (c-forward-syntactic-ws)
4364 (setq safe-pos (point))))
4365
4366 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
4367 (if (eq (char-after) ?:)
4368 ;; If we are at the colon already, we move over the type
4369 ;; list after it.
4370 (progn
4371 (forward-char)
4372 (c-forward-syntactic-ws)
4373 (when (c-forward-keyword-prefixed-id type)
4374 (c-forward-id-comma-list type t)))
4375 ;; Not at the colon, so stop here. But the identifier
4376 ;; ranges in the type list later on should still be
4377 ;; recorded.
4378 (and c-record-type-identifiers
4379 (progn
4380 ;; If a keyword matched both one of the types above and
4381 ;; this one, we match `c-colon-type-list-re' after the
4382 ;; clause matched above.
4383 (goto-char safe-pos)
4384 (looking-at c-colon-type-list-re))
4385 (progn
4386 (goto-char (match-end 0))
4387 (c-forward-syntactic-ws)
4388 (c-forward-keyword-prefixed-id type))
4389 ;; There's a type after the `c-colon-type-list-re' match
4390 ;; after a keyword in `c-colon-type-list-kwds'.
4391 (c-forward-id-comma-list type nil))))
4392
4393 (goto-char safe-pos)
4394 t)))
4395
4396 (defvar c-forward-<>-arglist-recur-depth)
4397
4398 (defun c-forward-<>-arglist (all-types)
4399 ;; The point is assumed to be at a "<". Try to treat it as the open
4400 ;; paren of an angle bracket arglist and move forward to the
4401 ;; corresponding ">". If successful, the point is left after the
4402 ;; ">" and t is returned, otherwise the point isn't moved and nil is
4403 ;; returned. If ALL-TYPES is t then all encountered arguments in
4404 ;; the arglist that might be types are treated as found types.
4405 ;;
4406 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
4407 ;; function handles text properties on the angle brackets and argument
4408 ;; separating commas.
4409 ;;
4410 ;; `c-restricted-<>-arglists' controls how lenient the template
4411 ;; arglist recognition should be.
4412 ;;
4413 ;; This function records identifier ranges on
4414 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4415 ;; `c-record-type-identifiers' is non-nil.
4416 ;;
4417 ;; This function might do hidden buffer changes.
4418
4419 (let ((start (point))
4420 ;; If `c-record-type-identifiers' is set then activate
4421 ;; recording of any found types that constitute an argument in
4422 ;; the arglist.
4423 (c-record-found-types (if c-record-type-identifiers t))
4424 (c-forward-<>-arglist-recur--depth 0))
4425 (if (catch 'angle-bracket-arglist-escape
4426 (setq c-record-found-types
4427 (c-forward-<>-arglist-recur all-types)))
4428 (progn
4429 (when (consp c-record-found-types)
4430 (setq c-record-type-identifiers
4431 ;; `nconc' doesn't mind that the tail of
4432 ;; `c-record-found-types' is t.
4433 (nconc c-record-found-types c-record-type-identifiers)))
4434 t)
4435
4436 (goto-char start)
4437 nil)))
4438
4439 (defun c-forward-<>-arglist-recur (all-types)
4440
4441 ;; Temporary workaround for Bug#7722.
4442 (when (boundp 'c-forward-<>-arglist-recur--depth)
4443 (if (> c-forward-<>-arglist-recur--depth 200)
4444 (error "Max recursion depth reached in <> arglist")
4445 (setq c-forward-<>-arglist-recur--depth
4446 (1+ c-forward-<>-arglist-recur--depth))))
4447
4448 ;; Recursive part of `c-forward-<>-arglist'.
4449 ;;
4450 ;; This function might do hidden buffer changes.
4451
4452 (let ((start (point)) res pos tmp
4453 ;; Cover this so that any recorded found type ranges are
4454 ;; automatically lost if it turns out to not be an angle
4455 ;; bracket arglist. It's propagated through the return value
4456 ;; on successful completion.
4457 (c-record-found-types c-record-found-types)
4458 ;; List that collects the positions after the argument
4459 ;; separating ',' in the arglist.
4460 arg-start-pos)
4461
4462 ;; If the '<' has paren open syntax then we've marked it as an angle
4463 ;; bracket arglist before, so skip to the end.
4464 (if (and (not c-parse-and-markup-<>-arglists)
4465 (c-get-char-property (point) 'syntax-table))
4466
4467 (progn
4468 (forward-char)
4469 (if (and (c-go-up-list-forward)
4470 (eq (char-before) ?>))
4471 t
4472
4473 ;; Got unmatched paren angle brackets. We don't clear the paren
4474 ;; syntax properties and retry, on the basis that it's very
4475 ;; unlikely that paren angle brackets become operators by code
4476 ;; manipulation. It's far more likely that it doesn't match due
4477 ;; to narrowing or some temporary change.
4478 (goto-char start)
4479 nil))
4480
4481 (forward-char)
4482 (unless (looking-at c-<-op-cont-regexp)
4483 (while (and
4484 (progn
4485
4486 (when c-record-type-identifiers
4487 (if all-types
4488
4489 ;; All encountered identifiers are types, so set the
4490 ;; promote flag and parse the type.
4491 (progn
4492 (c-forward-syntactic-ws)
4493 (when (looking-at c-identifier-start)
4494 (let ((c-promote-possible-types t))
4495 (c-forward-type))))
4496
4497 ;; Check if this arglist argument is a sole type. If
4498 ;; it's known then it's recorded in
4499 ;; `c-record-type-identifiers'. If it only is found
4500 ;; then it's recorded in `c-record-found-types' which we
4501 ;; might roll back if it turns out that this isn't an
4502 ;; angle bracket arglist afterall.
4503 (when (memq (char-before) '(?, ?<))
4504 (let ((orig-record-found-types c-record-found-types))
4505 (c-forward-syntactic-ws)
4506 (and (memq (c-forward-type) '(known found))
4507 (not (looking-at "[,>]"))
4508 ;; A found type was recorded but it's not the
4509 ;; only thing in the arglist argument, so reset
4510 ;; `c-record-found-types'.
4511 (setq c-record-found-types
4512 orig-record-found-types))))))
4513
4514 (setq pos (point))
4515 (or (when (eq (char-after) ?>)
4516 ;; Must check for '>' at the very start separately,
4517 ;; since the regexp below has to avoid ">>" without
4518 ;; using \\=.
4519 (forward-char)
4520 t)
4521
4522 ;; Note: These regexps exploit the match order in \| so
4523 ;; that "<>" is matched by "<" rather than "[^>:-]>".
4524 (c-syntactic-re-search-forward
4525 (if c-restricted-<>-arglists
4526 ;; Stop on ',', '|', '&', '+' and '-' to catch
4527 ;; common binary operators that could be between
4528 ;; two comparison expressions "a<b" and "c>d".
4529 "[<;{},|&+-]\\|\\([^>:-]>\\)"
4530 ;; Otherwise we still stop on ',' to find the
4531 ;; argument start positions.
4532 "[<;{},]\\|\\([^>:-]>\\)")
4533 nil 'move t t 1)
4534
4535 ;; If the arglist starter has lost its open paren
4536 ;; syntax but not the closer, we won't find the
4537 ;; closer above since we only search in the
4538 ;; balanced sexp. In that case we stop just short
4539 ;; of it so check if the following char is the closer.
4540 (when (eq (char-after) ?>)
4541 (forward-char)
4542 t)))
4543
4544 (cond
4545 ((eq (char-before) ?>)
4546 ;; Either an operator starting with '>' or the end of
4547 ;; the angle bracket arglist.
4548
4549 (if (looking-at c->-op-cont-regexp)
4550 (progn
4551 (goto-char (match-end 0))
4552 t) ; Continue the loop.
4553
4554 ;; The angle bracket arglist is finished.
4555 (when c-parse-and-markup-<>-arglists
4556 (while arg-start-pos
4557 (c-put-c-type-property (1- (car arg-start-pos))
4558 'c-<>-arg-sep)
4559 (setq arg-start-pos (cdr arg-start-pos)))
4560 (c-mark-<-as-paren start)
4561 (c-mark->-as-paren (1- (point))))
4562 (setq res t)
4563 nil)) ; Exit the loop.
4564
4565 ((eq (char-before) ?<)
4566 ;; Either an operator starting with '<' or a nested arglist.
4567
4568 (setq pos (point))
4569 (let (id-start id-end subres keyword-match)
4570 (if (if (looking-at c-<-op-cont-regexp)
4571 (setq tmp (match-end 0))
4572 (setq tmp pos)
4573 (backward-char)
4574 (not
4575 (and
4576
4577 (save-excursion
4578 ;; There's always an identifier before an angle
4579 ;; bracket arglist, or a keyword in
4580 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
4581 (c-backward-syntactic-ws)
4582 (setq id-end (point))
4583 (c-simple-skip-symbol-backward)
4584 (when (or (setq keyword-match
4585 (looking-at c-opt-<>-sexp-key))
4586 (not (looking-at c-keywords-regexp)))
4587 (setq id-start (point))))
4588
4589 (setq subres
4590 (let ((c-record-type-identifiers nil)
4591 (c-record-found-types nil))
4592 (c-forward-<>-arglist-recur
4593 (and keyword-match
4594 (c-keyword-member
4595 (c-keyword-sym (match-string 1))
4596 'c-<>-type-kwds)))))
4597 )))
4598
4599 ;; It was not an angle bracket arglist.
4600 (goto-char tmp)
4601
4602 ;; It was an angle bracket arglist.
4603 (setq c-record-found-types subres)
4604
4605 ;; Record the identifier before the template as a type
4606 ;; or reference depending on whether the arglist is last
4607 ;; in a qualified identifier.
4608 (when (and c-record-type-identifiers
4609 (not keyword-match))
4610 (if (and c-opt-identifier-concat-key
4611 (progn
4612 (c-forward-syntactic-ws)
4613 (looking-at c-opt-identifier-concat-key)))
4614 (c-record-ref-id (cons id-start id-end))
4615 (c-record-type-id (cons id-start id-end))))))
4616 t)
4617
4618 ((and (eq (char-before) ?,)
4619 (not c-restricted-<>-arglists))
4620 ;; Just another argument. Record the position. The
4621 ;; type check stuff that made us stop at it is at
4622 ;; the top of the loop.
4623 (setq arg-start-pos (cons (point) arg-start-pos)))
4624
4625 (t
4626 ;; Got a character that can't be in an angle bracket
4627 ;; arglist argument. Abort using `throw', since
4628 ;; it's useless to try to find a surrounding arglist
4629 ;; if we're nested.
4630 (throw 'angle-bracket-arglist-escape nil))))))
4631
4632 (if res
4633 (or c-record-found-types t)))))
4634
4635 (defun c-backward-<>-arglist (all-types &optional limit)
4636 ;; The point is assumed to be directly after a ">". Try to treat it
4637 ;; as the close paren of an angle bracket arglist and move back to
4638 ;; the corresponding "<". If successful, the point is left at
4639 ;; the "<" and t is returned, otherwise the point isn't moved and
4640 ;; nil is returned. ALL-TYPES is passed on to
4641 ;; `c-forward-<>-arglist'.
4642 ;;
4643 ;; If the optional LIMIT is given, it bounds the backward search.
4644 ;; It's then assumed to be at a syntactically relevant position.
4645 ;;
4646 ;; This is a wrapper around `c-forward-<>-arglist'. See that
4647 ;; function for more details.
4648
4649 (let ((start (point)))
4650 (backward-char)
4651 (if (and (not c-parse-and-markup-<>-arglists)
4652 (c-get-char-property (point) 'syntax-table))
4653
4654 (if (and (c-go-up-list-backward)
4655 (eq (char-after) ?<))
4656 t
4657 ;; See corresponding note in `c-forward-<>-arglist'.
4658 (goto-char start)
4659 nil)
4660
4661 (while (progn
4662 (c-syntactic-skip-backward "^<;{}" limit t)
4663
4664 (and
4665 (if (eq (char-before) ?<)
4666 t
4667 ;; Stopped at bob or a char that isn't allowed in an
4668 ;; arglist, so we've failed.
4669 (goto-char start)
4670 nil)
4671
4672 (if (> (point)
4673 (progn (c-beginning-of-current-token)
4674 (point)))
4675 ;; If we moved then the "<" was part of some
4676 ;; multicharacter token.
4677 t
4678
4679 (backward-char)
4680 (let ((beg-pos (point)))
4681 (if (c-forward-<>-arglist all-types)
4682 (cond ((= (point) start)
4683 ;; Matched the arglist. Break the while.
4684 (goto-char beg-pos)
4685 nil)
4686 ((> (point) start)
4687 ;; We started from a non-paren ">" inside an
4688 ;; arglist.
4689 (goto-char start)
4690 nil)
4691 (t
4692 ;; Matched a shorter arglist. Can be a nested
4693 ;; one so continue looking.
4694 (goto-char beg-pos)
4695 t))
4696 t))))))
4697
4698 (/= (point) start))))
4699
4700 (defun c-forward-name ()
4701 ;; Move forward over a complete name if at the beginning of one,
4702 ;; stopping at the next following token. If the point is not at
4703 ;; something that are recognized as name then it stays put. A name
4704 ;; could be something as simple as "foo" in C or something as
4705 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
4706 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
4707 ;; int>::*volatile const" in C++ (this function is actually little
4708 ;; more than a `looking-at' call in all modes except those that,
4709 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
4710 ;; name is found, 'template if it's an identifier ending with an
4711 ;; angle bracket arglist, 'operator of it's an operator identifier,
4712 ;; or t if it's some other kind of name.
4713 ;;
4714 ;; This function records identifier ranges on
4715 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4716 ;; `c-record-type-identifiers' is non-nil.
4717 ;;
4718 ;; This function might do hidden buffer changes.
4719
4720 (let ((pos (point)) (start (point)) res id-start id-end
4721 ;; Turn off `c-promote-possible-types' here since we might
4722 ;; call `c-forward-<>-arglist' and we don't want it to promote
4723 ;; every suspect thing in the arglist to a type. We're
4724 ;; typically called from `c-forward-type' in this case, and
4725 ;; the caller only wants the top level type that it finds to
4726 ;; be promoted.
4727 c-promote-possible-types)
4728 (while
4729 (and
4730 (looking-at c-identifier-key)
4731
4732 (progn
4733 ;; Check for keyword. We go to the last symbol in
4734 ;; `c-identifier-key' first.
4735 (goto-char (setq id-end (match-end 0)))
4736 (c-simple-skip-symbol-backward)
4737 (setq id-start (point))
4738
4739 (if (looking-at c-keywords-regexp)
4740 (when (and (c-major-mode-is 'c++-mode)
4741 (looking-at
4742 (cc-eval-when-compile
4743 (concat "\\(operator\\|\\(template\\)\\)"
4744 "\\(" (c-lang-const c-nonsymbol-key c++)
4745 "\\|$\\)")))
4746 (if (match-beginning 2)
4747 ;; "template" is only valid inside an
4748 ;; identifier if preceded by "::".
4749 (save-excursion
4750 (c-backward-syntactic-ws)
4751 (and (c-safe (backward-char 2) t)
4752 (looking-at "::")))
4753 t))
4754
4755 ;; Handle a C++ operator or template identifier.
4756 (goto-char id-end)
4757 (c-forward-syntactic-ws)
4758 (cond ((eq (char-before id-end) ?e)
4759 ;; Got "... ::template".
4760 (let ((subres (c-forward-name)))
4761 (when subres
4762 (setq pos (point)
4763 res subres))))
4764
4765 ((looking-at c-identifier-start)
4766 ;; Got a cast operator.
4767 (when (c-forward-type)
4768 (setq pos (point)
4769 res 'operator)
4770 ;; Now we should match a sequence of either
4771 ;; '*', '&' or a name followed by ":: *",
4772 ;; where each can be followed by a sequence
4773 ;; of `c-opt-type-modifier-key'.
4774 (while (cond ((looking-at "[*&]")
4775 (goto-char (match-end 0))
4776 t)
4777 ((looking-at c-identifier-start)
4778 (and (c-forward-name)
4779 (looking-at "::")
4780 (progn
4781 (goto-char (match-end 0))
4782 (c-forward-syntactic-ws)
4783 (eq (char-after) ?*))
4784 (progn
4785 (forward-char)
4786 t))))
4787 (while (progn
4788 (c-forward-syntactic-ws)
4789 (setq pos (point))
4790 (looking-at c-opt-type-modifier-key))
4791 (goto-char (match-end 1))))))
4792
4793 ((looking-at c-overloadable-operators-regexp)
4794 ;; Got some other operator.
4795 (setq c-last-identifier-range
4796 (cons (point) (match-end 0)))
4797 (goto-char (match-end 0))
4798 (c-forward-syntactic-ws)
4799 (setq pos (point)
4800 res 'operator)))
4801
4802 nil)
4803
4804 ;; `id-start' is equal to `id-end' if we've jumped over
4805 ;; an identifier that doesn't end with a symbol token.
4806 ;; That can occur e.g. for Java import directives on the
4807 ;; form "foo.bar.*".
4808 (when (and id-start (/= id-start id-end))
4809 (setq c-last-identifier-range
4810 (cons id-start id-end)))
4811 (goto-char id-end)
4812 (c-forward-syntactic-ws)
4813 (setq pos (point)
4814 res t)))
4815
4816 (progn
4817 (goto-char pos)
4818 (when (or c-opt-identifier-concat-key
4819 c-recognize-<>-arglists)
4820
4821 (cond
4822 ((and c-opt-identifier-concat-key
4823 (looking-at c-opt-identifier-concat-key))
4824 ;; Got a concatenated identifier. This handles the
4825 ;; cases with tricky syntactic whitespace that aren't
4826 ;; covered in `c-identifier-key'.
4827 (goto-char (match-end 0))
4828 (c-forward-syntactic-ws)
4829 t)
4830
4831 ((and c-recognize-<>-arglists
4832 (eq (char-after) ?<))
4833 ;; Maybe an angle bracket arglist.
4834
4835 (when (let (c-record-type-identifiers
4836 c-record-found-types)
4837 (c-forward-<>-arglist nil))
4838
4839 (c-add-type start (1+ pos))
4840 (c-forward-syntactic-ws)
4841 (setq pos (point)
4842 c-last-identifier-range nil)
4843
4844 (if (and c-opt-identifier-concat-key
4845 (looking-at c-opt-identifier-concat-key))
4846
4847 ;; Continue if there's an identifier concatenation
4848 ;; operator after the template argument.
4849 (progn
4850 (when (and c-record-type-identifiers id-start)
4851 (c-record-ref-id (cons id-start id-end)))
4852 (forward-char 2)
4853 (c-forward-syntactic-ws)
4854 t)
4855
4856 (when (and c-record-type-identifiers id-start)
4857 (c-record-type-id (cons id-start id-end)))
4858 (setq res 'template)
4859 nil)))
4860 )))))
4861
4862 (goto-char pos)
4863 res))
4864
4865 (defun c-forward-type ()
4866 ;; Move forward over a type spec if at the beginning of one,
4867 ;; stopping at the next following token. Return t if it's a known
4868 ;; type that can't be a name or other expression, 'known if it's an
4869 ;; otherwise known type (according to `*-font-lock-extra-types'),
4870 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4871 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4872 ;; that might be a type, or nil if it can't be a type (the point
4873 ;; isn't moved then). The point is assumed to be at the beginning
4874 ;; of a token.
4875 ;;
4876 ;; Note that this function doesn't skip past the brace definition
4877 ;; that might be considered part of the type, e.g.
4878 ;; "enum {a, b, c} foo".
4879 ;;
4880 ;; This function records identifier ranges on
4881 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4882 ;; `c-record-type-identifiers' is non-nil.
4883 ;;
4884 ;; This function might do hidden buffer changes.
4885
4886 (let ((start (point)) pos res name-res id-start id-end id-range)
4887
4888 ;; Skip leading type modifiers. If any are found we know it's a
4889 ;; prefix of a type.
4890 (when c-opt-type-modifier-key
4891 (while (looking-at c-opt-type-modifier-key)
4892 (goto-char (match-end 1))
4893 (c-forward-syntactic-ws)
4894 (setq res 'prefix)))
4895
4896 (cond
4897 ((looking-at c-type-prefix-key)
4898 ;; Looking at a keyword that prefixes a type identifier,
4899 ;; e.g. "class".
4900 (goto-char (match-end 1))
4901 (c-forward-syntactic-ws)
4902 (setq pos (point))
4903 (if (memq (setq name-res (c-forward-name)) '(t template))
4904 (progn
4905 (when (eq name-res t)
4906 ;; In many languages the name can be used without the
4907 ;; prefix, so we add it to `c-found-types'.
4908 (c-add-type pos (point))
4909 (when (and c-record-type-identifiers
4910 c-last-identifier-range)
4911 (c-record-type-id c-last-identifier-range)))
4912 (setq res t))
4913 ;; Invalid syntax.
4914 (goto-char start)
4915 (setq res nil)))
4916
4917 ((progn
4918 (setq pos nil)
4919 (if (looking-at c-identifier-start)
4920 (save-excursion
4921 (setq id-start (point)
4922 name-res (c-forward-name))
4923 (when name-res
4924 (setq id-end (point)
4925 id-range c-last-identifier-range))))
4926 (and (cond ((looking-at c-primitive-type-key)
4927 (setq res t))
4928 ((c-with-syntax-table c-identifier-syntax-table
4929 (looking-at c-known-type-key))
4930 (setq res 'known)))
4931 (or (not id-end)
4932 (>= (save-excursion
4933 (save-match-data
4934 (goto-char (match-end 1))
4935 (c-forward-syntactic-ws)
4936 (setq pos (point))))
4937 id-end)
4938 (setq res nil))))
4939 ;; Looking at a primitive or known type identifier. We've
4940 ;; checked for a name first so that we don't go here if the
4941 ;; known type match only is a prefix of another name.
4942
4943 (setq id-end (match-end 1))
4944
4945 (when (and c-record-type-identifiers
4946 (or c-promote-possible-types (eq res t)))
4947 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4948
4949 (if (and c-opt-type-component-key
4950 (save-match-data
4951 (looking-at c-opt-type-component-key)))
4952 ;; There might be more keywords for the type.
4953 (let (safe-pos)
4954 (c-forward-keyword-clause 1)
4955 (while (progn
4956 (setq safe-pos (point))
4957 (looking-at c-opt-type-component-key))
4958 (when (and c-record-type-identifiers
4959 (looking-at c-primitive-type-key))
4960 (c-record-type-id (cons (match-beginning 1)
4961 (match-end 1))))
4962 (c-forward-keyword-clause 1))
4963 (if (looking-at c-primitive-type-key)
4964 (progn
4965 (when c-record-type-identifiers
4966 (c-record-type-id (cons (match-beginning 1)
4967 (match-end 1))))
4968 (c-forward-keyword-clause 1)
4969 (setq res t))
4970 (goto-char safe-pos)
4971 (setq res 'prefix)))
4972 (unless (save-match-data (c-forward-keyword-clause 1))
4973 (if pos
4974 (goto-char pos)
4975 (goto-char (match-end 1))
4976 (c-forward-syntactic-ws)))))
4977
4978 (name-res
4979 (cond ((eq name-res t)
4980 ;; A normal identifier.
4981 (goto-char id-end)
4982 (if (or res c-promote-possible-types)
4983 (progn
4984 (c-add-type id-start id-end)
4985 (when (and c-record-type-identifiers id-range)
4986 (c-record-type-id id-range))
4987 (unless res
4988 (setq res 'found)))
4989 (setq res (if (c-check-type id-start id-end)
4990 ;; It's an identifier that has been used as
4991 ;; a type somewhere else.
4992 'found
4993 ;; It's an identifier that might be a type.
4994 'maybe))))
4995 ((eq name-res 'template)
4996 ;; A template is a type.
4997 (goto-char id-end)
4998 (setq res t))
4999 (t
5000 ;; Otherwise it's an operator identifier, which is not a type.
5001 (goto-char start)
5002 (setq res nil)))))
5003
5004 (when res
5005 ;; Skip trailing type modifiers. If any are found we know it's
5006 ;; a type.
5007 (when c-opt-type-modifier-key
5008 (while (looking-at c-opt-type-modifier-key)
5009 (goto-char (match-end 1))
5010 (c-forward-syntactic-ws)
5011 (setq res t)))
5012
5013 ;; Step over any type suffix operator. Do not let the existence
5014 ;; of these alter the classification of the found type, since
5015 ;; these operators typically are allowed in normal expressions
5016 ;; too.
5017 (when c-opt-type-suffix-key
5018 (while (looking-at c-opt-type-suffix-key)
5019 (goto-char (match-end 1))
5020 (c-forward-syntactic-ws)))
5021
5022 (when c-opt-type-concat-key
5023 ;; Look for a trailing operator that concatenates the type
5024 ;; with a following one, and if so step past that one through
5025 ;; a recursive call. Note that we don't record concatenated
5026 ;; types in `c-found-types' - it's the component types that
5027 ;; are recorded when appropriate.
5028 (setq pos (point))
5029 (let* ((c-promote-possible-types (or (memq res '(t known))
5030 c-promote-possible-types))
5031 ;; If we can't promote then set `c-record-found-types' so that
5032 ;; we can merge in the types from the second part afterwards if
5033 ;; it turns out to be a known type there.
5034 (c-record-found-types (and c-record-type-identifiers
5035 (not c-promote-possible-types)))
5036 subres)
5037 (if (and (looking-at c-opt-type-concat-key)
5038
5039 (progn
5040 (goto-char (match-end 1))
5041 (c-forward-syntactic-ws)
5042 (setq subres (c-forward-type))))
5043
5044 (progn
5045 ;; If either operand certainly is a type then both are, but we
5046 ;; don't let the existence of the operator itself promote two
5047 ;; uncertain types to a certain one.
5048 (cond ((eq res t))
5049 ((eq subres t)
5050 (unless (eq name-res 'template)
5051 (c-add-type id-start id-end))
5052 (when (and c-record-type-identifiers id-range)
5053 (c-record-type-id id-range))
5054 (setq res t))
5055 ((eq res 'known))
5056 ((eq subres 'known)
5057 (setq res 'known))
5058 ((eq res 'found))
5059 ((eq subres 'found)
5060 (setq res 'found))
5061 (t
5062 (setq res 'maybe)))
5063
5064 (when (and (eq res t)
5065 (consp c-record-found-types))
5066 ;; Merge in the ranges of any types found by the second
5067 ;; `c-forward-type'.
5068 (setq c-record-type-identifiers
5069 ;; `nconc' doesn't mind that the tail of
5070 ;; `c-record-found-types' is t.
5071 (nconc c-record-found-types
5072 c-record-type-identifiers))))
5073
5074 (goto-char pos))))
5075
5076 (when (and c-record-found-types (memq res '(known found)) id-range)
5077 (setq c-record-found-types
5078 (cons id-range c-record-found-types))))
5079
5080 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
5081
5082 res))
5083
5084 \f
5085 ;; Handling of large scale constructs like statements and declarations.
5086
5087 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
5088 ;; defsubst or perhaps even a defun, but it contains lots of free
5089 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
5090 (defmacro c-fdoc-shift-type-backward (&optional short)
5091 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
5092 ;; of types when parsing a declaration, which means that it
5093 ;; sometimes consumes the identifier in the declaration as a type.
5094 ;; This is used to "backtrack" and make the last type be treated as
5095 ;; an identifier instead.
5096 `(progn
5097 ,(unless short
5098 ;; These identifiers are bound only in the inner let.
5099 '(setq identifier-type at-type
5100 identifier-start type-start
5101 got-parens nil
5102 got-identifier t
5103 got-suffix t
5104 got-suffix-after-parens id-start
5105 paren-depth 0))
5106
5107 (if (setq at-type (if (eq backup-at-type 'prefix)
5108 t
5109 backup-at-type))
5110 (setq type-start backup-type-start
5111 id-start backup-id-start)
5112 (setq type-start start-pos
5113 id-start start-pos))
5114
5115 ;; When these flags already are set we've found specifiers that
5116 ;; unconditionally signal these attributes - backtracking doesn't
5117 ;; change that. So keep them set in that case.
5118 (or at-type-decl
5119 (setq at-type-decl backup-at-type-decl))
5120 (or maybe-typeless
5121 (setq maybe-typeless backup-maybe-typeless))
5122
5123 ,(unless short
5124 ;; This identifier is bound only in the inner let.
5125 '(setq start id-start))))
5126
5127 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
5128 ;; Move forward over a declaration or a cast if at the start of one.
5129 ;; The point is assumed to be at the start of some token. Nil is
5130 ;; returned if no declaration or cast is recognized, and the point
5131 ;; is clobbered in that case.
5132 ;;
5133 ;; If a declaration is parsed:
5134 ;;
5135 ;; The point is left at the first token after the first complete
5136 ;; declarator, if there is one. The return value is a cons where
5137 ;; the car is the position of the first token in the declarator. (See
5138 ;; below for the cdr.)
5139 ;; Some examples:
5140 ;;
5141 ;; void foo (int a, char *b) stuff ...
5142 ;; car ^ ^ point
5143 ;; float (*a)[], b;
5144 ;; car ^ ^ point
5145 ;; unsigned int a = c_style_initializer, b;
5146 ;; car ^ ^ point
5147 ;; unsigned int a (cplusplus_style_initializer), b;
5148 ;; car ^ ^ point (might change)
5149 ;; class Foo : public Bar {}
5150 ;; car ^ ^ point
5151 ;; class PikeClass (int a, string b) stuff ...
5152 ;; car ^ ^ point
5153 ;; enum bool;
5154 ;; car ^ ^ point
5155 ;; enum bool flag;
5156 ;; car ^ ^ point
5157 ;; void cplusplus_function (int x) throw (Bad);
5158 ;; car ^ ^ point
5159 ;; Foo::Foo (int b) : Base (b) {}
5160 ;; car ^ ^ point
5161 ;;
5162 ;; The cdr of the return value is non-nil iff a `c-typedef-decl-kwds'
5163 ;; specifier (e.g. class, struct, enum, typedef) is found in the
5164 ;; declaration, i.e. the declared identifier(s) are types.
5165 ;;
5166 ;; If a cast is parsed:
5167 ;;
5168 ;; The point is left at the first token after the closing paren of
5169 ;; the cast. The return value is `cast'. Note that the start
5170 ;; position must be at the first token inside the cast parenthesis
5171 ;; to recognize it.
5172 ;;
5173 ;; PRECEDING-TOKEN-END is the first position after the preceding
5174 ;; token, i.e. on the other side of the syntactic ws from the point.
5175 ;; Use a value less than or equal to (point-min) if the point is at
5176 ;; the first token in (the visible part of) the buffer.
5177 ;;
5178 ;; CONTEXT is a symbol that describes the context at the point:
5179 ;; 'decl In a comma-separated declaration context (typically
5180 ;; inside a function declaration arglist).
5181 ;; '<> In an angle bracket arglist.
5182 ;; 'arglist Some other type of arglist.
5183 ;; nil Some other context or unknown context. Includes
5184 ;; within the parens of an if, for, ... construct.
5185 ;;
5186 ;; LAST-CAST-END is the first token after the closing paren of a
5187 ;; preceding cast, or nil if none is known. If
5188 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
5189 ;; the position after the closest preceding call where a cast was
5190 ;; matched. In that case it's used to discover chains of casts like
5191 ;; "(a) (b) c".
5192 ;;
5193 ;; This function records identifier ranges on
5194 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5195 ;; `c-record-type-identifiers' is non-nil.
5196 ;;
5197 ;; This function might do hidden buffer changes.
5198
5199 (let (;; `start-pos' is used below to point to the start of the
5200 ;; first type, i.e. after any leading specifiers. It might
5201 ;; also point at the beginning of the preceding syntactic
5202 ;; whitespace.
5203 (start-pos (point))
5204 ;; Set to the result of `c-forward-type'.
5205 at-type
5206 ;; The position of the first token in what we currently
5207 ;; believe is the type in the declaration or cast, after any
5208 ;; specifiers and their associated clauses.
5209 type-start
5210 ;; The position of the first token in what we currently
5211 ;; believe is the declarator for the first identifier. Set
5212 ;; when the type is found, and moved forward over any
5213 ;; `c-decl-hangon-kwds' and their associated clauses that
5214 ;; occurs after the type.
5215 id-start
5216 ;; These store `at-type', `type-start' and `id-start' of the
5217 ;; identifier before the one in those variables. The previous
5218 ;; identifier might turn out to be the real type in a
5219 ;; declaration if the last one has to be the declarator in it.
5220 ;; If `backup-at-type' is nil then the other variables have
5221 ;; undefined values.
5222 backup-at-type backup-type-start backup-id-start
5223 ;; Set if we've found a specifier that makes the defined
5224 ;; identifier(s) types.
5225 at-type-decl
5226 ;; Set if we've found a specifier that can start a declaration
5227 ;; where there's no type.
5228 maybe-typeless
5229 ;; If a specifier is found that also can be a type prefix,
5230 ;; these flags are set instead of those above. If we need to
5231 ;; back up an identifier, they are copied to the real flag
5232 ;; variables. Thus they only take effect if we fail to
5233 ;; interpret it as a type.
5234 backup-at-type-decl backup-maybe-typeless
5235 ;; Whether we've found a declaration or a cast. We might know
5236 ;; this before we've found the type in it. It's 'ids if we've
5237 ;; found two consecutive identifiers (usually a sure sign, but
5238 ;; we should allow that in labels too), and t if we've found a
5239 ;; specifier keyword (a 100% sure sign).
5240 at-decl-or-cast
5241 ;; Set when we need to back up to parse this as a declaration
5242 ;; but not as a cast.
5243 backup-if-not-cast
5244 ;; For casts, the return position.
5245 cast-end
5246 ;; Save `c-record-type-identifiers' and
5247 ;; `c-record-ref-identifiers' since ranges are recorded
5248 ;; speculatively and should be thrown away if it turns out
5249 ;; that it isn't a declaration or cast.
5250 (save-rec-type-ids c-record-type-identifiers)
5251 (save-rec-ref-ids c-record-ref-identifiers))
5252
5253 ;; Check for a type. Unknown symbols are treated as possible
5254 ;; types, but they could also be specifiers disguised through
5255 ;; macros like __INLINE__, so we recognize both types and known
5256 ;; specifiers after them too.
5257 (while
5258 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
5259
5260 ;; Look for a specifier keyword clause.
5261 (when (looking-at c-prefix-spec-kwds-re)
5262 (setq kwd-sym (c-keyword-sym (match-string 1)))
5263 (save-excursion
5264 (c-forward-keyword-clause 1)
5265 (setq kwd-clause-end (point))))
5266
5267 (when (setq found-type (c-forward-type))
5268 ;; Found a known or possible type or a prefix of a known type.
5269
5270 (when at-type
5271 ;; Got two identifiers with nothing but whitespace
5272 ;; between them. That can only happen in declarations.
5273 (setq at-decl-or-cast 'ids)
5274
5275 (when (eq at-type 'found)
5276 ;; If the previous identifier is a found type we
5277 ;; record it as a real one; it might be some sort of
5278 ;; alias for a prefix like "unsigned".
5279 (save-excursion
5280 (goto-char type-start)
5281 (let ((c-promote-possible-types t))
5282 (c-forward-type)))))
5283
5284 (setq backup-at-type at-type
5285 backup-type-start type-start
5286 backup-id-start id-start
5287 at-type found-type
5288 type-start start
5289 id-start (point)
5290 ;; The previous ambiguous specifier/type turned out
5291 ;; to be a type since we've parsed another one after
5292 ;; it, so clear these backup flags.
5293 backup-at-type-decl nil
5294 backup-maybe-typeless nil))
5295
5296 (if kwd-sym
5297 (progn
5298 ;; Handle known specifier keywords and
5299 ;; `c-decl-hangon-kwds' which can occur after known
5300 ;; types.
5301
5302 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
5303 ;; It's a hang-on keyword that can occur anywhere.
5304 (progn
5305 (setq at-decl-or-cast t)
5306 (if at-type
5307 ;; Move the identifier start position if
5308 ;; we've passed a type.
5309 (setq id-start kwd-clause-end)
5310 ;; Otherwise treat this as a specifier and
5311 ;; move the fallback position.
5312 (setq start-pos kwd-clause-end))
5313 (goto-char kwd-clause-end))
5314
5315 ;; It's an ordinary specifier so we know that
5316 ;; anything before this can't be the type.
5317 (setq backup-at-type nil
5318 start-pos kwd-clause-end)
5319
5320 (if found-type
5321 ;; It's ambiguous whether this keyword is a
5322 ;; specifier or a type prefix, so set the backup
5323 ;; flags. (It's assumed that `c-forward-type'
5324 ;; moved further than `c-forward-keyword-clause'.)
5325 (progn
5326 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5327 (setq backup-at-type-decl t))
5328 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5329 (setq backup-maybe-typeless t)))
5330
5331 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5332 (setq at-type-decl t))
5333 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5334 (setq maybe-typeless t))
5335
5336 ;; Haven't matched a type so it's an umambiguous
5337 ;; specifier keyword and we know we're in a
5338 ;; declaration.
5339 (setq at-decl-or-cast t)
5340
5341 (goto-char kwd-clause-end))))
5342
5343 ;; If the type isn't known we continue so that we'll jump
5344 ;; over all specifiers and type identifiers. The reason
5345 ;; to do this for a known type prefix is to make things
5346 ;; like "unsigned INT16" work.
5347 (and found-type (not (eq found-type t))))))
5348
5349 (cond
5350 ((eq at-type t)
5351 ;; If a known type was found, we still need to skip over any
5352 ;; hangon keyword clauses after it. Otherwise it has already
5353 ;; been done in the loop above.
5354 (while (looking-at c-decl-hangon-key)
5355 (c-forward-keyword-clause 1))
5356 (setq id-start (point)))
5357
5358 ((eq at-type 'prefix)
5359 ;; A prefix type is itself a primitive type when it's not
5360 ;; followed by another type.
5361 (setq at-type t))
5362
5363 ((not at-type)
5364 ;; Got no type but set things up to continue anyway to handle
5365 ;; the various cases when a declaration doesn't start with a
5366 ;; type.
5367 (setq id-start start-pos))
5368
5369 ((and (eq at-type 'maybe)
5370 (c-major-mode-is 'c++-mode))
5371 ;; If it's C++ then check if the last "type" ends on the form
5372 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
5373 ;; (con|de)structor.
5374 (save-excursion
5375 (let (name end-2 end-1)
5376 (goto-char id-start)
5377 (c-backward-syntactic-ws)
5378 (setq end-2 (point))
5379 (when (and
5380 (c-simple-skip-symbol-backward)
5381 (progn
5382 (setq name
5383 (buffer-substring-no-properties (point) end-2))
5384 ;; Cheating in the handling of syntactic ws below.
5385 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
5386 (progn
5387 (setq end-1 (point))
5388 (c-simple-skip-symbol-backward))
5389 (>= (point) type-start)
5390 (equal (buffer-substring-no-properties (point) end-1)
5391 name))
5392 ;; It is a (con|de)structor name. In that case the
5393 ;; declaration is typeless so zap out any preceding
5394 ;; identifier(s) that we might have taken as types.
5395 (goto-char type-start)
5396 (setq at-type nil
5397 backup-at-type nil
5398 id-start type-start))))))
5399
5400 ;; Check for and step over a type decl expression after the thing
5401 ;; that is or might be a type. This can't be skipped since we
5402 ;; need the correct end position of the declarator for
5403 ;; `max-type-decl-end-*'.
5404 (let ((start (point)) (paren-depth 0) pos
5405 ;; True if there's a non-open-paren match of
5406 ;; `c-type-decl-prefix-key'.
5407 got-prefix
5408 ;; True if the declarator is surrounded by a parenthesis pair.
5409 got-parens
5410 ;; True if there is an identifier in the declarator.
5411 got-identifier
5412 ;; True if there's a non-close-paren match of
5413 ;; `c-type-decl-suffix-key'.
5414 got-suffix
5415 ;; True if there's a prefix match outside the outermost
5416 ;; paren pair that surrounds the declarator.
5417 got-prefix-before-parens
5418 ;; True if there's a suffix match outside the outermost
5419 ;; paren pair that surrounds the declarator. The value is
5420 ;; the position of the first suffix match.
5421 got-suffix-after-parens
5422 ;; True if we've parsed the type decl to a token that is
5423 ;; known to end declarations in this context.
5424 at-decl-end
5425 ;; The earlier values of `at-type' and `type-start' if we've
5426 ;; shifted the type backwards.
5427 identifier-type identifier-start
5428 ;; If `c-parse-and-markup-<>-arglists' is set we need to
5429 ;; turn it off during the name skipping below to avoid
5430 ;; getting `c-type' properties that might be bogus. That
5431 ;; can happen since we don't know if
5432 ;; `c-restricted-<>-arglists' will be correct inside the
5433 ;; arglist paren that gets entered.
5434 c-parse-and-markup-<>-arglists)
5435
5436 (goto-char id-start)
5437
5438 ;; Skip over type decl prefix operators. (Note similar code in
5439 ;; `c-font-lock-declarators'.)
5440 (while (and (looking-at c-type-decl-prefix-key)
5441 (if (and (c-major-mode-is 'c++-mode)
5442 (match-beginning 3))
5443 ;; If the second submatch matches in C++ then
5444 ;; we're looking at an identifier that's a
5445 ;; prefix only if it specifies a member pointer.
5446 (when (setq got-identifier (c-forward-name))
5447 (if (looking-at "\\(::\\)")
5448 ;; We only check for a trailing "::" and
5449 ;; let the "*" that should follow be
5450 ;; matched in the next round.
5451 (progn (setq got-identifier nil) t)
5452 ;; It turned out to be the real identifier,
5453 ;; so stop.
5454 nil))
5455 t))
5456
5457 (if (eq (char-after) ?\()
5458 (progn
5459 (setq paren-depth (1+ paren-depth))
5460 (forward-char))
5461 (unless got-prefix-before-parens
5462 (setq got-prefix-before-parens (= paren-depth 0)))
5463 (setq got-prefix t)
5464 (goto-char (match-end 1)))
5465 (c-forward-syntactic-ws))
5466
5467 (setq got-parens (> paren-depth 0))
5468
5469 ;; Skip over an identifier.
5470 (or got-identifier
5471 (and (looking-at c-identifier-start)
5472 (setq got-identifier (c-forward-name))))
5473
5474 ;; Skip over type decl suffix operators.
5475 (while (if (looking-at c-type-decl-suffix-key)
5476
5477 (if (eq (char-after) ?\))
5478 (when (> paren-depth 0)
5479 (setq paren-depth (1- paren-depth))
5480 (forward-char)
5481 t)
5482 (when (if (save-match-data (looking-at "\\s\("))
5483 (c-safe (c-forward-sexp 1) t)
5484 (goto-char (match-end 1))
5485 t)
5486 (when (and (not got-suffix-after-parens)
5487 (= paren-depth 0))
5488 (setq got-suffix-after-parens (match-beginning 0)))
5489 (setq got-suffix t)))
5490
5491 ;; No suffix matched. We might have matched the
5492 ;; identifier as a type and the open paren of a
5493 ;; function arglist as a type decl prefix. In that
5494 ;; case we should "backtrack": Reinterpret the last
5495 ;; type as the identifier, move out of the arglist and
5496 ;; continue searching for suffix operators.
5497 ;;
5498 ;; Do this even if there's no preceding type, to cope
5499 ;; with old style function declarations in K&R C,
5500 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
5501 ;; style declarations. That isn't applicable in an
5502 ;; arglist context, though.
5503 (when (and (= paren-depth 1)
5504 (not got-prefix-before-parens)
5505 (not (eq at-type t))
5506 (or backup-at-type
5507 maybe-typeless
5508 backup-maybe-typeless
5509 (when c-recognize-typeless-decls
5510 (not context)))
5511 (setq pos (c-up-list-forward (point)))
5512 (eq (char-before pos) ?\)))
5513 (c-fdoc-shift-type-backward)
5514 (goto-char pos)
5515 t))
5516
5517 (c-forward-syntactic-ws))
5518
5519 (when (and (or maybe-typeless backup-maybe-typeless)
5520 (not got-identifier)
5521 (not got-prefix)
5522 at-type)
5523 ;; Have found no identifier but `c-typeless-decl-kwds' has
5524 ;; matched so we know we're inside a declaration. The
5525 ;; preceding type must be the identifier instead.
5526 (c-fdoc-shift-type-backward))
5527
5528 (setq
5529 at-decl-or-cast
5530 (catch 'at-decl-or-cast
5531
5532 ;; CASE 1
5533 (when (> paren-depth 0)
5534 ;; Encountered something inside parens that isn't matched by
5535 ;; the `c-type-decl-*' regexps, so it's not a type decl
5536 ;; expression. Try to skip out to the same paren depth to
5537 ;; not confuse the cast check below.
5538 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
5539 ;; If we've found a specifier keyword then it's a
5540 ;; declaration regardless.
5541 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
5542
5543 (setq at-decl-end
5544 (looking-at (cond ((eq context '<>) "[,>]")
5545 (context "[,\)]")
5546 (t "[,;]"))))
5547
5548 ;; Now we've collected info about various characteristics of
5549 ;; the construct we're looking at. Below follows a decision
5550 ;; tree based on that. It's ordered to check more certain
5551 ;; signs before less certain ones.
5552
5553 (if got-identifier
5554 (progn
5555
5556 ;; CASE 2
5557 (when (and (or at-type maybe-typeless)
5558 (not (or got-prefix got-parens)))
5559 ;; Got another identifier directly after the type, so it's a
5560 ;; declaration.
5561 (throw 'at-decl-or-cast t))
5562
5563 (when (and got-parens
5564 (not got-prefix)
5565 (not got-suffix-after-parens)
5566 (or backup-at-type
5567 maybe-typeless
5568 backup-maybe-typeless))
5569 ;; Got a declaration of the form "foo bar (gnu);" where we've
5570 ;; recognized "bar" as the type and "gnu" as the declarator.
5571 ;; In this case it's however more likely that "bar" is the
5572 ;; declarator and "gnu" a function argument or initializer (if
5573 ;; `c-recognize-paren-inits' is set), since the parens around
5574 ;; "gnu" would be superfluous if it's a declarator. Shift the
5575 ;; type one step backward.
5576 (c-fdoc-shift-type-backward)))
5577
5578 ;; Found no identifier.
5579
5580 (if backup-at-type
5581 (progn
5582
5583 ;; CASE 3
5584 (when (= (point) start)
5585 ;; Got a plain list of identifiers. If a colon follows it's
5586 ;; a valid label. Otherwise the last one probably is the
5587 ;; declared identifier and we should back up to the previous
5588 ;; type, providing it isn't a cast.
5589 (if (eq (char-after) ?:)
5590 ;; If we've found a specifier keyword then it's a
5591 ;; declaration regardless.
5592 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
5593 (setq backup-if-not-cast t)
5594 (throw 'at-decl-or-cast t)))
5595
5596 ;; CASE 4
5597 (when (and got-suffix
5598 (not got-prefix)
5599 (not got-parens))
5600 ;; Got a plain list of identifiers followed by some suffix.
5601 ;; If this isn't a cast then the last identifier probably is
5602 ;; the declared one and we should back up to the previous
5603 ;; type.
5604 (setq backup-if-not-cast t)
5605 (throw 'at-decl-or-cast t)))
5606
5607 ;; CASE 5
5608 (when (eq at-type t)
5609 ;; If the type is known we know that there can't be any
5610 ;; identifier somewhere else, and it's only in declarations in
5611 ;; e.g. function prototypes and in casts that the identifier may
5612 ;; be left out.
5613 (throw 'at-decl-or-cast t))
5614
5615 (when (= (point) start)
5616 ;; Only got a single identifier (parsed as a type so far).
5617 ;; CASE 6
5618 (if (and
5619 ;; Check that the identifier isn't at the start of an
5620 ;; expression.
5621 at-decl-end
5622 (cond
5623 ((eq context 'decl)
5624 ;; Inside an arglist that contains declarations. If K&R
5625 ;; style declarations and parenthesis style initializers
5626 ;; aren't allowed then the single identifier must be a
5627 ;; type, else we require that it's known or found
5628 ;; (primitive types are handled above).
5629 (or (and (not c-recognize-knr-p)
5630 (not c-recognize-paren-inits))
5631 (memq at-type '(known found))))
5632 ((eq context '<>)
5633 ;; Inside a template arglist. Accept known and found
5634 ;; types; other identifiers could just as well be
5635 ;; constants in C++.
5636 (memq at-type '(known found)))))
5637 (throw 'at-decl-or-cast t)
5638 ;; CASE 7
5639 ;; Can't be a valid declaration or cast, but if we've found a
5640 ;; specifier it can't be anything else either, so treat it as
5641 ;; an invalid/unfinished declaration or cast.
5642 (throw 'at-decl-or-cast at-decl-or-cast))))
5643
5644 (if (and got-parens
5645 (not got-prefix)
5646 (not context)
5647 (not (eq at-type t))
5648 (or backup-at-type
5649 maybe-typeless
5650 backup-maybe-typeless
5651 (when c-recognize-typeless-decls
5652 (or (not got-suffix)
5653 (not (looking-at
5654 c-after-suffixed-type-maybe-decl-key))))))
5655 ;; Got an empty paren pair and a preceding type that probably
5656 ;; really is the identifier. Shift the type backwards to make
5657 ;; the last one the identifier. This is analogous to the
5658 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
5659 ;; above.
5660 ;;
5661 ;; Exception: In addition to the conditions in that
5662 ;; "backtracking" code, do not shift backward if we're not
5663 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
5664 ;; Since there's no preceding type, the shift would mean that
5665 ;; the declaration is typeless. But if the regexp doesn't match
5666 ;; then we will simply fall through in the tests below and not
5667 ;; recognize it at all, so it's better to try it as an abstract
5668 ;; declarator instead.
5669 (c-fdoc-shift-type-backward)
5670
5671 ;; Still no identifier.
5672 ;; CASE 8
5673 (when (and got-prefix (or got-parens got-suffix))
5674 ;; Require `got-prefix' together with either `got-parens' or
5675 ;; `got-suffix' to recognize it as an abstract declarator:
5676 ;; `got-parens' only is probably an empty function call.
5677 ;; `got-suffix' only can build an ordinary expression together
5678 ;; with the preceding identifier which we've taken as a type.
5679 ;; We could actually accept on `got-prefix' only, but that can
5680 ;; easily occur temporarily while writing an expression so we
5681 ;; avoid that case anyway. We could do a better job if we knew
5682 ;; the point when the fontification was invoked.
5683 (throw 'at-decl-or-cast t))
5684
5685 ;; CASE 9
5686 (when (and at-type
5687 (not got-prefix)
5688 (not got-parens)
5689 got-suffix-after-parens
5690 (eq (char-after got-suffix-after-parens) ?\())
5691 ;; Got a type, no declarator but a paren suffix. I.e. it's a
5692 ;; normal function call afterall (or perhaps a C++ style object
5693 ;; instantiation expression).
5694 (throw 'at-decl-or-cast nil))))
5695
5696 ;; CASE 10
5697 (when at-decl-or-cast
5698 ;; By now we've located the type in the declaration that we know
5699 ;; we're in.
5700 (throw 'at-decl-or-cast t))
5701
5702 ;; CASE 11
5703 (when (and got-identifier
5704 (not context)
5705 (looking-at c-after-suffixed-type-decl-key)
5706 (if (and got-parens
5707 (not got-prefix)
5708 (not got-suffix)
5709 (not (eq at-type t)))
5710 ;; Shift the type backward in the case that there's a
5711 ;; single identifier inside parens. That can only
5712 ;; occur in K&R style function declarations so it's
5713 ;; more likely that it really is a function call.
5714 ;; Therefore we only do this after
5715 ;; `c-after-suffixed-type-decl-key' has matched.
5716 (progn (c-fdoc-shift-type-backward) t)
5717 got-suffix-after-parens))
5718 ;; A declaration according to `c-after-suffixed-type-decl-key'.
5719 (throw 'at-decl-or-cast t))
5720
5721 ;; CASE 12
5722 (when (and (or got-prefix (not got-parens))
5723 (memq at-type '(t known)))
5724 ;; It's a declaration if a known type precedes it and it can't be a
5725 ;; function call.
5726 (throw 'at-decl-or-cast t))
5727
5728 ;; If we get here we can't tell if this is a type decl or a normal
5729 ;; expression by looking at it alone. (That's under the assumption
5730 ;; that normal expressions always can look like type decl expressions,
5731 ;; which isn't really true but the cases where it doesn't hold are so
5732 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
5733 ;; the effort to look for them.)
5734
5735 (unless (or at-decl-end (looking-at "=[^=]"))
5736 ;; If this is a declaration it should end here or its initializer(*)
5737 ;; should start here, so check for allowed separation tokens. Note
5738 ;; that this rule doesn't work e.g. with a K&R arglist after a
5739 ;; function header.
5740 ;;
5741 ;; *) Don't check for C++ style initializers using parens
5742 ;; since those already have been matched as suffixes.
5743 ;;
5744 ;; If `at-decl-or-cast' is then we've found some other sign that
5745 ;; it's a declaration or cast, so then it's probably an
5746 ;; invalid/unfinished one.
5747 (throw 'at-decl-or-cast at-decl-or-cast))
5748
5749 ;; Below are tests that only should be applied when we're certain to
5750 ;; not have parsed halfway through an expression.
5751
5752 ;; CASE 14
5753 (when (memq at-type '(t known))
5754 ;; The expression starts with a known type so treat it as a
5755 ;; declaration.
5756 (throw 'at-decl-or-cast t))
5757
5758 ;; CASE 15
5759 (when (and (c-major-mode-is 'c++-mode)
5760 ;; In C++ we check if the identifier is a known type, since
5761 ;; (con|de)structors use the class name as identifier.
5762 ;; We've always shifted over the identifier as a type and
5763 ;; then backed up again in this case.
5764 identifier-type
5765 (or (memq identifier-type '(found known))
5766 (and (eq (char-after identifier-start) ?~)
5767 ;; `at-type' probably won't be 'found for
5768 ;; destructors since the "~" is then part of the
5769 ;; type name being checked against the list of
5770 ;; known types, so do a check without that
5771 ;; operator.
5772 (or (save-excursion
5773 (goto-char (1+ identifier-start))
5774 (c-forward-syntactic-ws)
5775 (c-with-syntax-table
5776 c-identifier-syntax-table
5777 (looking-at c-known-type-key)))
5778 (save-excursion
5779 (goto-char (1+ identifier-start))
5780 ;; We have already parsed the type earlier,
5781 ;; so it'd be possible to cache the end
5782 ;; position instead of redoing it here, but
5783 ;; then we'd need to keep track of another
5784 ;; position everywhere.
5785 (c-check-type (point)
5786 (progn (c-forward-type)
5787 (point))))))))
5788 (throw 'at-decl-or-cast t))
5789
5790 (if got-identifier
5791 (progn
5792 ;; CASE 16
5793 (when (and got-prefix-before-parens
5794 at-type
5795 (or at-decl-end (looking-at "=[^=]"))
5796 (not context)
5797 (not got-suffix))
5798 ;; Got something like "foo * bar;". Since we're not inside an
5799 ;; arglist it would be a meaningless expression because the
5800 ;; result isn't used. We therefore choose to recognize it as
5801 ;; a declaration. Do not allow a suffix since it could then
5802 ;; be a function call.
5803 (throw 'at-decl-or-cast t))
5804
5805 ;; CASE 17
5806 (when (and (or got-suffix-after-parens
5807 (looking-at "=[^=]"))
5808 (eq at-type 'found)
5809 (not (eq context 'arglist)))
5810 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
5811 ;; be an odd expression or it could be a declaration. Treat
5812 ;; it as a declaration if "a" has been used as a type
5813 ;; somewhere else (if it's a known type we won't get here).
5814 (throw 'at-decl-or-cast t)))
5815
5816 ;; CASE 18
5817 (when (and context
5818 (or got-prefix
5819 (and (eq context 'decl)
5820 (not c-recognize-paren-inits)
5821 (or got-parens got-suffix))))
5822 ;; Got a type followed by an abstract declarator. If `got-prefix'
5823 ;; is set it's something like "a *" without anything after it. If
5824 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
5825 ;; or similar, which we accept only if the context rules out
5826 ;; expressions.
5827 (throw 'at-decl-or-cast t)))
5828
5829 ;; If we had a complete symbol table here (which rules out
5830 ;; `c-found-types') we should return t due to the disambiguation rule
5831 ;; (in at least C++) that anything that can be parsed as a declaration
5832 ;; is a declaration. Now we're being more defensive and prefer to
5833 ;; highlight things like "foo (bar);" as a declaration only if we're
5834 ;; inside an arglist that contains declarations.
5835 (eq context 'decl))))
5836
5837 ;; The point is now after the type decl expression.
5838
5839 (cond
5840 ;; Check for a cast.
5841 ((save-excursion
5842 (and
5843 c-cast-parens
5844
5845 ;; Should be the first type/identifier in a cast paren.
5846 (> preceding-token-end (point-min))
5847 (memq (char-before preceding-token-end) c-cast-parens)
5848
5849 ;; The closing paren should follow.
5850 (progn
5851 (c-forward-syntactic-ws)
5852 (looking-at "\\s\)"))
5853
5854 ;; There should be a primary expression after it.
5855 (let (pos)
5856 (forward-char)
5857 (c-forward-syntactic-ws)
5858 (setq cast-end (point))
5859 (and (looking-at c-primary-expr-regexp)
5860 (progn
5861 (setq pos (match-end 0))
5862 (or
5863 ;; Check if the expression begins with a prefix keyword.
5864 (match-beginning 2)
5865 (if (match-beginning 1)
5866 ;; Expression begins with an ambiguous operator. Treat
5867 ;; it as a cast if it's a type decl or if we've
5868 ;; recognized the type somewhere else.
5869 (or at-decl-or-cast
5870 (memq at-type '(t known found)))
5871 ;; Unless it's a keyword, it's the beginning of a primary
5872 ;; expression.
5873 (not (looking-at c-keywords-regexp)))))
5874 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
5875 ;; that it matched a whole one so that we don't e.g. confuse
5876 ;; the operator '-' with '->'. It's ok if it matches further,
5877 ;; though, since it e.g. can match the float '.5' while the
5878 ;; operator regexp only matches '.'.
5879 (or (not (looking-at c-nonsymbol-token-regexp))
5880 (<= (match-end 0) pos))))
5881
5882 ;; There should either be a cast before it or something that isn't an
5883 ;; identifier or close paren.
5884 (> preceding-token-end (point-min))
5885 (progn
5886 (goto-char (1- preceding-token-end))
5887 (or (eq (point) last-cast-end)
5888 (progn
5889 (c-backward-syntactic-ws)
5890 (if (< (skip-syntax-backward "w_") 0)
5891 ;; It's a symbol. Accept it only if it's one of the
5892 ;; keywords that can precede an expression (without
5893 ;; surrounding parens).
5894 (looking-at c-simple-stmt-key)
5895 (and
5896 ;; Check that it isn't a close paren (block close is ok,
5897 ;; though).
5898 (not (memq (char-before) '(?\) ?\])))
5899 ;; Check that it isn't a nonsymbol identifier.
5900 (not (c-on-identifier)))))))))
5901
5902 ;; Handle the cast.
5903 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5904 (let ((c-promote-possible-types t))
5905 (goto-char type-start)
5906 (c-forward-type)))
5907
5908 (goto-char cast-end)
5909 'cast)
5910
5911 (at-decl-or-cast
5912 ;; We're at a declaration. Highlight the type and the following
5913 ;; declarators.
5914
5915 (when backup-if-not-cast
5916 (c-fdoc-shift-type-backward t))
5917
5918 (when (and (eq context 'decl) (looking-at ","))
5919 ;; Make sure to propagate the `c-decl-arg-start' property to
5920 ;; the next argument if it's set in this one, to cope with
5921 ;; interactive refontification.
5922 (c-put-c-type-property (point) 'c-decl-arg-start))
5923
5924 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5925 (let ((c-promote-possible-types t))
5926 (save-excursion
5927 (goto-char type-start)
5928 (c-forward-type))))
5929
5930 (cons id-start at-type-decl))
5931
5932 (t
5933 ;; False alarm. Restore the recorded ranges.
5934 (setq c-record-type-identifiers save-rec-type-ids
5935 c-record-ref-identifiers save-rec-ref-ids)
5936 nil))))
5937
5938 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
5939 ;; Assuming that point is at the beginning of a token, check if it starts a
5940 ;; label and if so move over it and return non-nil (t in default situations,
5941 ;; specific symbols (see below) for interesting situations), otherwise don't
5942 ;; move and return nil. "Label" here means "most things with a colon".
5943 ;;
5944 ;; More precisely, a "label" is regarded as one of:
5945 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
5946 ;; (ii) A case label - either the entire construct "case FOO:", or just the
5947 ;; bare "case", should the colon be missing. We return t;
5948 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
5949 ;; return t;
5950 ;; (iv) One of QT's "extended" C++ variants of
5951 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
5952 ;; Returns the symbol `qt-2kwds-colon'.
5953 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
5954 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
5955 ;; colon). Currently (2006-03), this applies only to Objective C's
5956 ;; keywords "@private", "@protected", and "@public". Returns t.
5957 ;;
5958 ;; One of the things which will NOT be recognised as a label is a bit-field
5959 ;; element of a struct, something like "int foo:5".
5960 ;;
5961 ;; The end of the label is taken to be just after the colon, or the end of
5962 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
5963 ;; after the end on return. The terminating char gets marked with
5964 ;; `c-decl-end' to improve recognition of the following declaration or
5965 ;; statement.
5966 ;;
5967 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
5968 ;; label, if any, has already been marked up like that.
5969 ;;
5970 ;; If PRECEDING-TOKEN-END is given, it should be the first position
5971 ;; after the preceding token, i.e. on the other side of the
5972 ;; syntactic ws from the point. Use a value less than or equal to
5973 ;; (point-min) if the point is at the first token in (the visible
5974 ;; part of) the buffer.
5975 ;;
5976 ;; The optional LIMIT limits the forward scan for the colon.
5977 ;;
5978 ;; This function records the ranges of the label symbols on
5979 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
5980 ;; non-nil.
5981 ;;
5982 ;; This function might do hidden buffer changes.
5983
5984 (let ((start (point))
5985 label-end
5986 qt-symbol-idx
5987 macro-start ; if we're in one.
5988 label-type
5989 kwd)
5990 (cond
5991 ;; "case" or "default" (Doesn't apply to AWK).
5992 ((looking-at c-label-kwds-regexp)
5993 (let ((kwd-end (match-end 1)))
5994 ;; Record only the keyword itself for fontification, since in
5995 ;; case labels the following is a constant expression and not
5996 ;; a label.
5997 (when c-record-type-identifiers
5998 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
5999
6000 ;; Find the label end.
6001 (goto-char kwd-end)
6002 (setq label-type
6003 (if (and (c-syntactic-re-search-forward
6004 ;; Stop on chars that aren't allowed in expressions,
6005 ;; and on operator chars that would be meaningless
6006 ;; there. FIXME: This doesn't cope with ?: operators.
6007 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
6008 limit t t nil 1)
6009 (match-beginning 2))
6010
6011 (progn ; there's a proper :
6012 (goto-char (match-beginning 2)) ; just after the :
6013 (c-put-c-type-property (1- (point)) 'c-decl-end)
6014 t)
6015
6016 ;; It's an unfinished label. We consider the keyword enough
6017 ;; to recognize it as a label, so that it gets fontified.
6018 ;; Leave the point at the end of it, but don't put any
6019 ;; `c-decl-end' marker.
6020 (goto-char kwd-end)
6021 t))))
6022
6023 ;; @private, @protected, @public, in Objective C, or similar.
6024 ((and c-opt-extra-label-key
6025 (looking-at c-opt-extra-label-key))
6026 ;; For a `c-opt-extra-label-key' match, we record the whole
6027 ;; thing for fontification. That's to get the leading '@' in
6028 ;; Objective-C protection labels fontified.
6029 (goto-char (match-end 1))
6030 (when c-record-type-identifiers
6031 (c-record-ref-id (cons (match-beginning 1) (point))))
6032 (c-put-c-type-property (1- (point)) 'c-decl-end)
6033 (setq label-type t))
6034
6035 ;; All other cases of labels.
6036 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
6037
6038 ;; A colon label must have something before the colon.
6039 (not (eq (char-after) ?:))
6040
6041 ;; Check that we're not after a token that can't precede a label.
6042 (or
6043 ;; Trivially succeeds when there's no preceding token.
6044 (if preceding-token-end
6045 (<= preceding-token-end (point-min))
6046 (save-excursion
6047 (c-backward-syntactic-ws)
6048 (setq preceding-token-end (point))
6049 (bobp)))
6050
6051 ;; Check if we're after a label, if we're after a closing
6052 ;; paren that belong to statement, and with
6053 ;; `c-label-prefix-re'. It's done in different order
6054 ;; depending on `assume-markup' since the checks have
6055 ;; different expensiveness.
6056 (if assume-markup
6057 (or
6058 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
6059 'c-decl-end)
6060
6061 (save-excursion
6062 (goto-char (1- preceding-token-end))
6063 (c-beginning-of-current-token)
6064 (or (looking-at c-label-prefix-re)
6065 (looking-at c-block-stmt-1-key)))
6066
6067 (and (eq (char-before preceding-token-end) ?\))
6068 (c-after-conditional)))
6069
6070 (or
6071 (save-excursion
6072 (goto-char (1- preceding-token-end))
6073 (c-beginning-of-current-token)
6074 (or (looking-at c-label-prefix-re)
6075 (looking-at c-block-stmt-1-key)))
6076
6077 (cond
6078 ((eq (char-before preceding-token-end) ?\))
6079 (c-after-conditional))
6080
6081 ((eq (char-before preceding-token-end) ?:)
6082 ;; Might be after another label, so check it recursively.
6083 (save-restriction
6084 (save-excursion
6085 (goto-char (1- preceding-token-end))
6086 ;; Essentially the same as the
6087 ;; `c-syntactic-re-search-forward' regexp below.
6088 (setq macro-start
6089 (save-excursion (and (c-beginning-of-macro)
6090 (point))))
6091 (if macro-start (narrow-to-region macro-start (point-max)))
6092 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
6093 ;; Note: the following should work instead of the
6094 ;; narrow-to-region above. Investigate why not,
6095 ;; sometime. ACM, 2006-03-31.
6096 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
6097 ;; macro-start t)
6098 (let ((pte (point))
6099 ;; If the caller turned on recording for us,
6100 ;; it shouldn't apply when we check the
6101 ;; preceding label.
6102 c-record-type-identifiers)
6103 ;; A label can't start at a cpp directive. Check for
6104 ;; this, since c-forward-syntactic-ws would foul up on it.
6105 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
6106 (c-forward-syntactic-ws)
6107 (c-forward-label nil pte start))))))))))
6108
6109 ;; Point is still at the beginning of the possible label construct.
6110 ;;
6111 ;; Check that the next nonsymbol token is ":", or that we're in one
6112 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
6113 ;; arguments. FIXME: Should build this regexp from the language
6114 ;; constants.
6115 (cond
6116 ;; public: protected: private:
6117 ((and
6118 (c-major-mode-is 'c++-mode)
6119 (search-forward-regexp
6120 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
6121 (progn (backward-char)
6122 (c-forward-syntactic-ws limit)
6123 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
6124 (forward-char)
6125 (setq label-type t))
6126 ;; QT double keyword like "protected slots:" or goto target.
6127 ((progn (goto-char start) nil))
6128 ((when (c-syntactic-re-search-forward
6129 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
6130 (backward-char)
6131 (setq label-end (point))
6132 (setq qt-symbol-idx
6133 (and (c-major-mode-is 'c++-mode)
6134 (string-match
6135 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
6136 (buffer-substring start (point)))))
6137 (c-forward-syntactic-ws limit)
6138 (cond
6139 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
6140 (forward-char)
6141 (setq label-type
6142 (if (or (string= "signals" ; Special QT macro
6143 (setq kwd (buffer-substring-no-properties start label-end)))
6144 (string= "Q_SIGNALS" kwd))
6145 'qt-1kwd-colon
6146 'goto-target)))
6147 ((and qt-symbol-idx
6148 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
6149 (progn (c-forward-syntactic-ws limit)
6150 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
6151 (forward-char)
6152 (setq label-type 'qt-2kwds-colon)))))))
6153
6154 (save-restriction
6155 (narrow-to-region start (point))
6156
6157 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
6158 (catch 'check-label
6159 (goto-char start)
6160 (while (progn
6161 (when (looking-at c-nonlabel-token-key)
6162 (goto-char start)
6163 (setq label-type nil)
6164 (throw 'check-label nil))
6165 (and (c-safe (c-forward-sexp)
6166 (c-forward-syntactic-ws)
6167 t)
6168 (not (eobp)))))
6169
6170 ;; Record the identifiers in the label for fontification, unless
6171 ;; it begins with `c-label-kwds' in which case the following
6172 ;; identifiers are part of a (constant) expression that
6173 ;; shouldn't be fontified.
6174 (when (and c-record-type-identifiers
6175 (progn (goto-char start)
6176 (not (looking-at c-label-kwds-regexp))))
6177 (while (c-syntactic-re-search-forward c-symbol-key nil t)
6178 (c-record-ref-id (cons (match-beginning 0)
6179 (match-end 0)))))
6180
6181 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
6182 (goto-char (point-max)))))
6183
6184 (t
6185 ;; Not a label.
6186 (goto-char start)))
6187 label-type))
6188
6189 (defun c-forward-objc-directive ()
6190 ;; Assuming the point is at the beginning of a token, try to move
6191 ;; forward to the end of the Objective-C directive that starts
6192 ;; there. Return t if a directive was fully recognized, otherwise
6193 ;; the point is moved as far as one could be successfully parsed and
6194 ;; nil is returned.
6195 ;;
6196 ;; This function records identifier ranges on
6197 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6198 ;; `c-record-type-identifiers' is non-nil.
6199 ;;
6200 ;; This function might do hidden buffer changes.
6201
6202 (let ((start (point))
6203 start-char
6204 (c-promote-possible-types t)
6205 ;; Turn off recognition of angle bracket arglists while parsing
6206 ;; types here since the protocol reference list might then be
6207 ;; considered part of the preceding name or superclass-name.
6208 c-recognize-<>-arglists)
6209
6210 (if (or
6211 (when (looking-at
6212 (eval-when-compile
6213 (c-make-keywords-re t
6214 (append (c-lang-const c-protection-kwds objc)
6215 '("@end"))
6216 'objc-mode)))
6217 (goto-char (match-end 1))
6218 t)
6219
6220 (and
6221 (looking-at
6222 (eval-when-compile
6223 (c-make-keywords-re t
6224 '("@interface" "@implementation" "@protocol")
6225 'objc-mode)))
6226
6227 ;; Handle the name of the class itself.
6228 (progn
6229 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
6230 ; at EOB.
6231 (goto-char (match-end 0))
6232 (c-skip-ws-forward)
6233 (c-forward-type))
6234
6235 (catch 'break
6236 ;; Look for ": superclass-name" or "( category-name )".
6237 (when (looking-at "[:\(]")
6238 (setq start-char (char-after))
6239 (forward-char)
6240 (c-forward-syntactic-ws)
6241 (unless (c-forward-type) (throw 'break nil))
6242 (when (eq start-char ?\()
6243 (unless (eq (char-after) ?\)) (throw 'break nil))
6244 (forward-char)
6245 (c-forward-syntactic-ws)))
6246
6247 ;; Look for a protocol reference list.
6248 (if (eq (char-after) ?<)
6249 (let ((c-recognize-<>-arglists t)
6250 (c-parse-and-markup-<>-arglists t)
6251 c-restricted-<>-arglists)
6252 (c-forward-<>-arglist t))
6253 t))))
6254
6255 (progn
6256 (c-backward-syntactic-ws)
6257 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
6258 (c-put-c-type-property (1- (point)) 'c-decl-end)
6259 t)
6260
6261 (c-clear-c-type-property start (point) 'c-decl-end)
6262 nil)))
6263
6264 (defun c-beginning-of-inheritance-list (&optional lim)
6265 ;; Go to the first non-whitespace after the colon that starts a
6266 ;; multiple inheritance introduction. Optional LIM is the farthest
6267 ;; back we should search.
6268 ;;
6269 ;; This function might do hidden buffer changes.
6270 (c-with-syntax-table c++-template-syntax-table
6271 (c-backward-token-2 0 t lim)
6272 (while (and (or (looking-at c-symbol-start)
6273 (looking-at "[<,]\\|::"))
6274 (zerop (c-backward-token-2 1 t lim))))))
6275
6276 (defun c-in-method-def-p ()
6277 ;; Return nil if we aren't in a method definition, otherwise the
6278 ;; position of the initial [+-].
6279 ;;
6280 ;; This function might do hidden buffer changes.
6281 (save-excursion
6282 (beginning-of-line)
6283 (and c-opt-method-key
6284 (looking-at c-opt-method-key)
6285 (point))
6286 ))
6287
6288 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
6289 (defun c-in-gcc-asm-p ()
6290 ;; Return non-nil if point is within a gcc \"asm\" block.
6291 ;;
6292 ;; This should be called with point inside an argument list.
6293 ;;
6294 ;; Only one level of enclosing parentheses is considered, so for
6295 ;; instance `nil' is returned when in a function call within an asm
6296 ;; operand.
6297 ;;
6298 ;; This function might do hidden buffer changes.
6299
6300 (and c-opt-asm-stmt-key
6301 (save-excursion
6302 (beginning-of-line)
6303 (backward-up-list 1)
6304 (c-beginning-of-statement-1 (point-min) nil t)
6305 (looking-at c-opt-asm-stmt-key))))
6306
6307 (defun c-at-toplevel-p ()
6308 "Return a determination as to whether point is \"at the top level\".
6309 Informally, \"at the top level\" is anywhere where you can write
6310 a function.
6311
6312 More precisely, being at the top-level means that point is either
6313 outside any enclosing block (such as a function definition), or
6314 directly inside a class, namespace or other block that contains
6315 another declaration level.
6316
6317 If point is not at the top-level (e.g. it is inside a method
6318 definition), then nil is returned. Otherwise, if point is at a
6319 top-level not enclosed within a class definition, t is returned.
6320 Otherwise, a 2-vector is returned where the zeroth element is the
6321 buffer position of the start of the class declaration, and the first
6322 element is the buffer position of the enclosing class's opening
6323 brace.
6324
6325 Note that this function might do hidden buffer changes. See the
6326 comment at the start of cc-engine.el for more info."
6327 (let ((paren-state (c-parse-state)))
6328 (or (not (c-most-enclosing-brace paren-state))
6329 (c-search-uplist-for-classkey paren-state))))
6330
6331 (defun c-just-after-func-arglist-p (&optional lim)
6332 ;; Return non-nil if the point is in the region after the argument
6333 ;; list of a function and its opening brace (or semicolon in case it
6334 ;; got no body). If there are K&R style argument declarations in
6335 ;; that region, the point has to be inside the first one for this
6336 ;; function to recognize it.
6337 ;;
6338 ;; If successful, the point is moved to the first token after the
6339 ;; function header (see `c-forward-decl-or-cast-1' for details) and
6340 ;; the position of the opening paren of the function arglist is
6341 ;; returned.
6342 ;;
6343 ;; The point is clobbered if not successful.
6344 ;;
6345 ;; LIM is used as bound for backward buffer searches.
6346 ;;
6347 ;; This function might do hidden buffer changes.
6348
6349 (let ((beg (point)) end id-start)
6350 (and
6351 (eq (c-beginning-of-statement-1 lim) 'same)
6352
6353 (not (or (c-major-mode-is 'objc-mode)
6354 (c-forward-objc-directive)))
6355
6356 (setq id-start
6357 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
6358 (< id-start beg)
6359
6360 ;; There should not be a '=' or ',' between beg and the
6361 ;; start of the declaration since that means we were in the
6362 ;; "expression part" of the declaration.
6363 (or (> (point) beg)
6364 (not (looking-at "[=,]")))
6365
6366 (save-excursion
6367 ;; Check that there's an arglist paren in the
6368 ;; declaration.
6369 (goto-char id-start)
6370 (cond ((eq (char-after) ?\()
6371 ;; The declarator is a paren expression, so skip past it
6372 ;; so that we don't get stuck on that instead of the
6373 ;; function arglist.
6374 (c-forward-sexp))
6375 ((and c-opt-op-identifier-prefix
6376 (looking-at c-opt-op-identifier-prefix))
6377 ;; Don't trip up on "operator ()".
6378 (c-forward-token-2 2 t)))
6379 (and (< (point) beg)
6380 (c-syntactic-re-search-forward "(" beg t t)
6381 (1- (point)))))))
6382
6383 (defun c-in-knr-argdecl (&optional lim)
6384 ;; Return the position of the first argument declaration if point is
6385 ;; inside a K&R style argument declaration list, nil otherwise.
6386 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
6387 ;; position that bounds the backward search for the argument list.
6388 ;;
6389 ;; Point must be within a possible K&R region, e.g. just before a top-level
6390 ;; "{". It must be outside of parens and brackets. The test can return
6391 ;; false positives otherwise.
6392 ;;
6393 ;; This function might do hidden buffer changes.
6394
6395 (save-excursion
6396 (save-restriction
6397 ;; If we're in a macro, our search range is restricted to it. Narrow to
6398 ;; the searchable range.
6399 (let* ((macro-start (c-query-macro-start))
6400 (lim (max (or lim (point-min)) (or macro-start (point-min))))
6401 before-lparen after-rparen
6402 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
6403 (narrow-to-region lim (c-point 'eol))
6404
6405 ;; Search backwards for the defun's argument list. We give up if we
6406 ;; encounter a "}" (end of a previous defun) or BOB.
6407 ;;
6408 ;; The criterion for a paren structure being the arg list is:
6409 ;; o - there is non-WS stuff after it but before any "{"; AND
6410 ;; o - the token after it isn't a ";" AND
6411 ;; o - it is preceded by either an identifier (the function name) or
6412 ;; a macro expansion like "DEFUN (...)"; AND
6413 ;; o - its content is a non-empty comma-separated list of identifiers
6414 ;; (an empty arg list won't have a knr region).
6415 ;;
6416 ;; The following snippet illustrates these rules:
6417 ;; int foo (bar, baz, yuk)
6418 ;; int bar [] ;
6419 ;; int (*baz) (my_type) ;
6420 ;; int (*) (void) (*yuk) (void) ;
6421 ;; {
6422
6423 (catch 'knr
6424 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
6425 (setq pp-count-out (1- pp-count-out))
6426 (c-syntactic-skip-backward "^)]}")
6427 (cond ((eq (char-before) ?\))
6428 (setq after-rparen (point)))
6429 ((eq (char-before) ?\])
6430 (setq after-rparen nil))
6431 (t ; either } (hit previous defun) or no more parens/brackets
6432 (throw 'knr nil)))
6433
6434 (if after-rparen
6435 ;; We're inside a paren. Could it be our argument list....?
6436 (if
6437 (and
6438 (progn
6439 (goto-char after-rparen)
6440 (unless (c-go-list-backward) (throw 'knr nil)) ;
6441 ;; FIXME!!! What about macros between the parens? 2007/01/20
6442 (setq before-lparen (point)))
6443
6444 ;; It can't be the arg list if next token is ; or {
6445 (progn (goto-char after-rparen)
6446 (c-forward-syntactic-ws)
6447 (not (memq (char-after) '(?\; ?\{))))
6448
6449 ;; Is the thing preceding the list an identifier (the
6450 ;; function name), or a macro expansion?
6451 (progn
6452 (goto-char before-lparen)
6453 (eq (c-backward-token-2) 0)
6454 (or (c-on-identifier)
6455 (and (eq (char-after) ?\))
6456 (c-go-up-list-backward)
6457 (eq (c-backward-token-2) 0)
6458 (c-on-identifier))))
6459
6460 ;; Have we got a non-empty list of comma-separated
6461 ;; identifiers?
6462 (progn
6463 (goto-char before-lparen)
6464 (c-forward-token-2) ; to first token inside parens
6465 (and
6466 (c-on-identifier)
6467 (c-forward-token-2)
6468 (catch 'id-list
6469 (while (eq (char-after) ?\,)
6470 (c-forward-token-2)
6471 (unless (c-on-identifier) (throw 'id-list nil))
6472 (c-forward-token-2))
6473 (eq (char-after) ?\))))))
6474
6475 ;; ...Yes. We've identified the function's argument list.
6476 (throw 'knr
6477 (progn (goto-char after-rparen)
6478 (c-forward-syntactic-ws)
6479 (point)))
6480
6481 ;; ...No. The current parens aren't the function's arg list.
6482 (goto-char before-lparen))
6483
6484 (or (c-go-list-backward) ; backwards over [ .... ]
6485 (throw 'knr nil)))))))))
6486
6487 (defun c-skip-conditional ()
6488 ;; skip forward over conditional at point, including any predicate
6489 ;; statements in parentheses. No error checking is performed.
6490 ;;
6491 ;; This function might do hidden buffer changes.
6492 (c-forward-sexp (cond
6493 ;; else if()
6494 ((looking-at (concat "\\<else"
6495 "\\([ \t\n]\\|\\\\\n\\)+"
6496 "if\\>\\([^_]\\|$\\)"))
6497 3)
6498 ;; do, else, try, finally
6499 ((looking-at (concat "\\<\\("
6500 "do\\|else\\|try\\|finally"
6501 "\\)\\>\\([^_]\\|$\\)"))
6502 1)
6503 ;; for, if, while, switch, catch, synchronized, foreach
6504 (t 2))))
6505
6506 (defun c-after-conditional (&optional lim)
6507 ;; If looking at the token after a conditional then return the
6508 ;; position of its start, otherwise return nil.
6509 ;;
6510 ;; This function might do hidden buffer changes.
6511 (save-excursion
6512 (and (zerop (c-backward-token-2 1 t lim))
6513 (or (looking-at c-block-stmt-1-key)
6514 (and (eq (char-after) ?\()
6515 (zerop (c-backward-token-2 1 t lim))
6516 (looking-at c-block-stmt-2-key)))
6517 (point))))
6518
6519 (defun c-after-special-operator-id (&optional lim)
6520 ;; If the point is after an operator identifier that isn't handled
6521 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
6522 ;; position of the start of that identifier is returned. nil is
6523 ;; returned otherwise. The point may be anywhere in the syntactic
6524 ;; whitespace after the last token of the operator identifier.
6525 ;;
6526 ;; This function might do hidden buffer changes.
6527 (save-excursion
6528 (and c-overloadable-operators-regexp
6529 (zerop (c-backward-token-2 1 nil lim))
6530 (looking-at c-overloadable-operators-regexp)
6531 (or (not c-opt-op-identifier-prefix)
6532 (and
6533 (zerop (c-backward-token-2 1 nil lim))
6534 (looking-at c-opt-op-identifier-prefix)))
6535 (point))))
6536
6537 (defsubst c-backward-to-block-anchor (&optional lim)
6538 ;; Assuming point is at a brace that opens a statement block of some
6539 ;; kind, move to the proper anchor point for that block. It might
6540 ;; need to be adjusted further by c-add-stmt-syntax, but the
6541 ;; position at return is suitable as start position for that
6542 ;; function.
6543 ;;
6544 ;; This function might do hidden buffer changes.
6545 (unless (= (point) (c-point 'boi))
6546 (let ((start (c-after-conditional lim)))
6547 (if start
6548 (goto-char start)))))
6549
6550 (defsubst c-backward-to-decl-anchor (&optional lim)
6551 ;; Assuming point is at a brace that opens the block of a top level
6552 ;; declaration of some kind, move to the proper anchor point for
6553 ;; that block.
6554 ;;
6555 ;; This function might do hidden buffer changes.
6556 (unless (= (point) (c-point 'boi))
6557 (c-beginning-of-statement-1 lim)))
6558
6559 (defun c-search-decl-header-end ()
6560 ;; Search forward for the end of the "header" of the current
6561 ;; declaration. That's the position where the definition body
6562 ;; starts, or the first variable initializer, or the ending
6563 ;; semicolon. I.e. search forward for the closest following
6564 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
6565 ;; _after_ the first found token, or at point-max if none is found.
6566 ;;
6567 ;; This function might do hidden buffer changes.
6568
6569 (let ((base (point)))
6570 (if (c-major-mode-is 'c++-mode)
6571
6572 ;; In C++ we need to take special care to handle operator
6573 ;; tokens and those pesky template brackets.
6574 (while (and
6575 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
6576 (or
6577 (c-end-of-current-token base)
6578 ;; Handle operator identifiers, i.e. ignore any
6579 ;; operator token preceded by "operator".
6580 (save-excursion
6581 (and (c-safe (c-backward-sexp) t)
6582 (looking-at c-opt-op-identifier-prefix)))
6583 (and (eq (char-before) ?<)
6584 (c-with-syntax-table c++-template-syntax-table
6585 (if (c-safe (goto-char (c-up-list-forward (point))))
6586 t
6587 (goto-char (point-max))
6588 nil)))))
6589 (setq base (point)))
6590
6591 (while (and
6592 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
6593 (c-end-of-current-token base))
6594 (setq base (point))))))
6595
6596 (defun c-beginning-of-decl-1 (&optional lim)
6597 ;; Go to the beginning of the current declaration, or the beginning
6598 ;; of the previous one if already at the start of it. Point won't
6599 ;; be moved out of any surrounding paren. Return a cons cell of the
6600 ;; form (MOVE . KNR-POS). MOVE is like the return value from
6601 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
6602 ;; style argument declarations (and they are to be recognized) then
6603 ;; KNR-POS is set to the start of the first such argument
6604 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
6605 ;; position that bounds the backward search.
6606 ;;
6607 ;; NB: Cases where the declaration continues after the block, as in
6608 ;; "struct foo { ... } bar;", are currently recognized as two
6609 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
6610 ;;
6611 ;; This function might do hidden buffer changes.
6612 (catch 'return
6613 (let* ((start (point))
6614 (last-stmt-start (point))
6615 (move (c-beginning-of-statement-1 lim nil t)))
6616
6617 ;; `c-beginning-of-statement-1' stops at a block start, but we
6618 ;; want to continue if the block doesn't begin a top level
6619 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
6620 ;; or an open paren.
6621 (let ((beg (point)) tentative-move)
6622 ;; Go back one "statement" each time round the loop until we're just
6623 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
6624 ;; an ObjC method. This will move over a multiple declaration whose
6625 ;; components are comma separated.
6626 (while (and
6627 ;; Must check with c-opt-method-key in ObjC mode.
6628 (not (and c-opt-method-key
6629 (looking-at c-opt-method-key)))
6630 (/= last-stmt-start (point))
6631 (progn
6632 (c-backward-syntactic-ws lim)
6633 (not (memq (char-before) '(?\; ?} ?: nil))))
6634 (save-excursion
6635 (backward-char)
6636 (not (looking-at "\\s(")))
6637 ;; Check that we don't move from the first thing in a
6638 ;; macro to its header.
6639 (not (eq (setq tentative-move
6640 (c-beginning-of-statement-1 lim nil t))
6641 'macro)))
6642 (setq last-stmt-start beg
6643 beg (point)
6644 move tentative-move))
6645 (goto-char beg))
6646
6647 (when c-recognize-knr-p
6648 (let ((fallback-pos (point)) knr-argdecl-start)
6649 ;; Handle K&R argdecls. Back up after the "statement" jumped
6650 ;; over by `c-beginning-of-statement-1', unless it was the
6651 ;; function body, in which case we're sitting on the opening
6652 ;; brace now. Then test if we're in a K&R argdecl region and
6653 ;; that we started at the other side of the first argdecl in
6654 ;; it.
6655 (unless (eq (char-after) ?{)
6656 (goto-char last-stmt-start))
6657 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
6658 (< knr-argdecl-start start)
6659 (progn
6660 (goto-char knr-argdecl-start)
6661 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
6662 (throw 'return
6663 (cons (if (eq (char-after fallback-pos) ?{)
6664 'previous
6665 'same)
6666 knr-argdecl-start))
6667 (goto-char fallback-pos))))
6668
6669 ;; `c-beginning-of-statement-1' counts each brace block as a separate
6670 ;; statement, so the result will be 'previous if we've moved over any.
6671 ;; So change our result back to 'same if necessary.
6672 ;;
6673 ;; If they were brace list initializers we might not have moved over a
6674 ;; declaration boundary though, so change it to 'same if we've moved
6675 ;; past a '=' before '{', but not ';'. (This ought to be integrated
6676 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
6677 ;; potentially can search over a large amount of text.). Take special
6678 ;; pains not to get mislead by C++'s "operator=", and the like.
6679 (if (and (eq move 'previous)
6680 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
6681 c++-template-syntax-table
6682 (syntax-table))
6683 (save-excursion
6684 (and
6685 (progn
6686 (while ; keep going back to "[;={"s until we either find
6687 ; no more, or get to one which isn't an "operator ="
6688 (and (c-syntactic-re-search-forward "[;={]" start t t t)
6689 (eq (char-before) ?=)
6690 c-overloadable-operators-regexp
6691 c-opt-op-identifier-prefix
6692 (save-excursion
6693 (eq (c-backward-token-2) 0)
6694 (looking-at c-overloadable-operators-regexp)
6695 (eq (c-backward-token-2) 0)
6696 (looking-at c-opt-op-identifier-prefix))))
6697 (eq (char-before) ?=))
6698 (c-syntactic-re-search-forward "[;{]" start t t)
6699 (eq (char-before) ?{)
6700 (c-safe (goto-char (c-up-list-forward (point))) t)
6701 (not (c-syntactic-re-search-forward ";" start t t))))))
6702 (cons 'same nil)
6703 (cons move nil)))))
6704
6705 (defun c-end-of-decl-1 ()
6706 ;; Assuming point is at the start of a declaration (as detected by
6707 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
6708 ;; `c-beginning-of-decl-1', this function handles the case when a
6709 ;; block is followed by identifiers in e.g. struct declarations in C
6710 ;; or C++. If a proper end was found then t is returned, otherwise
6711 ;; point is moved as far as possible within the current sexp and nil
6712 ;; is returned. This function doesn't handle macros; use
6713 ;; `c-end-of-macro' instead in those cases.
6714 ;;
6715 ;; This function might do hidden buffer changes.
6716 (let ((start (point))
6717 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
6718 c++-template-syntax-table
6719 (syntax-table))))
6720 (catch 'return
6721 (c-search-decl-header-end)
6722
6723 (when (and c-recognize-knr-p
6724 (eq (char-before) ?\;)
6725 (c-in-knr-argdecl start))
6726 ;; Stopped at the ';' in a K&R argdecl section which is
6727 ;; detected using the same criteria as in
6728 ;; `c-beginning-of-decl-1'. Move to the following block
6729 ;; start.
6730 (c-syntactic-re-search-forward "{" nil 'move t))
6731
6732 (when (eq (char-before) ?{)
6733 ;; Encountered a block in the declaration. Jump over it.
6734 (condition-case nil
6735 (goto-char (c-up-list-forward (point)))
6736 (error (goto-char (point-max))
6737 (throw 'return nil)))
6738 (if (or (not c-opt-block-decls-with-vars-key)
6739 (save-excursion
6740 (c-with-syntax-table decl-syntax-table
6741 (let ((lim (point)))
6742 (goto-char start)
6743 (not (and
6744 ;; Check for `c-opt-block-decls-with-vars-key'
6745 ;; before the first paren.
6746 (c-syntactic-re-search-forward
6747 (concat "[;=\(\[{]\\|\\("
6748 c-opt-block-decls-with-vars-key
6749 "\\)")
6750 lim t t t)
6751 (match-beginning 1)
6752 (not (eq (char-before) ?_))
6753 ;; Check that the first following paren is
6754 ;; the block.
6755 (c-syntactic-re-search-forward "[;=\(\[{]"
6756 lim t t t)
6757 (eq (char-before) ?{)))))))
6758 ;; The declaration doesn't have any of the
6759 ;; `c-opt-block-decls-with-vars' keywords in the
6760 ;; beginning, so it ends here at the end of the block.
6761 (throw 'return t)))
6762
6763 (c-with-syntax-table decl-syntax-table
6764 (while (progn
6765 (if (eq (char-before) ?\;)
6766 (throw 'return t))
6767 (c-syntactic-re-search-forward ";" nil 'move t))))
6768 nil)))
6769
6770 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
6771 ;; Assuming the point is at an open brace, check if it starts a
6772 ;; block that contains another declaration level, i.e. that isn't a
6773 ;; statement block or a brace list, and if so return non-nil.
6774 ;;
6775 ;; If the check is successful, the return value is the start of the
6776 ;; keyword that tells what kind of construct it is, i.e. typically
6777 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
6778 ;; the point will be at the start of the construct, before any
6779 ;; leading specifiers, otherwise it's at the returned position.
6780 ;;
6781 ;; The point is clobbered if the check is unsuccessful.
6782 ;;
6783 ;; CONTAINING-SEXP is the position of the open of the surrounding
6784 ;; paren, or nil if none.
6785 ;;
6786 ;; The optional LIMIT limits the backward search for the start of
6787 ;; the construct. It's assumed to be at a syntactically relevant
6788 ;; position.
6789 ;;
6790 ;; If any template arglists are found in the searched region before
6791 ;; the open brace, they get marked with paren syntax.
6792 ;;
6793 ;; This function might do hidden buffer changes.
6794
6795 (let ((open-brace (point)) kwd-start first-specifier-pos)
6796 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6797
6798 (when (and c-recognize-<>-arglists
6799 (eq (char-before) ?>))
6800 ;; Could be at the end of a template arglist.
6801 (let ((c-parse-and-markup-<>-arglists t)
6802 (c-disallow-comma-in-<>-arglists
6803 (and containing-sexp
6804 (not (eq (char-after containing-sexp) ?{)))))
6805 (while (and
6806 (c-backward-<>-arglist nil limit)
6807 (progn
6808 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6809 (eq (char-before) ?>))))))
6810
6811 ;; Note: Can't get bogus hits inside template arglists below since they
6812 ;; have gotten paren syntax above.
6813 (when (and
6814 ;; If `goto-start' is set we begin by searching for the
6815 ;; first possible position of a leading specifier list.
6816 ;; The `c-decl-block-key' search continues from there since
6817 ;; we know it can't match earlier.
6818 (if goto-start
6819 (when (c-syntactic-re-search-forward c-symbol-start
6820 open-brace t t)
6821 (goto-char (setq first-specifier-pos (match-beginning 0)))
6822 t)
6823 t)
6824
6825 (cond
6826 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
6827 (goto-char (setq kwd-start (match-beginning 0)))
6828 (or
6829
6830 ;; Found a keyword that can't be a type?
6831 (match-beginning 1)
6832
6833 ;; Can be a type too, in which case it's the return type of a
6834 ;; function (under the assumption that no declaration level
6835 ;; block construct starts with a type).
6836 (not (c-forward-type))
6837
6838 ;; Jumped over a type, but it could be a declaration keyword
6839 ;; followed by the declared identifier that we've jumped over
6840 ;; instead (e.g. in "class Foo {"). If it indeed is a type
6841 ;; then we should be at the declarator now, so check for a
6842 ;; valid declarator start.
6843 ;;
6844 ;; Note: This doesn't cope with the case when a declared
6845 ;; identifier is followed by e.g. '(' in a language where '('
6846 ;; also might be part of a declarator expression. Currently
6847 ;; there's no such language.
6848 (not (or (looking-at c-symbol-start)
6849 (looking-at c-type-decl-prefix-key)))))
6850
6851 ;; In Pike a list of modifiers may be followed by a brace
6852 ;; to make them apply to many identifiers. Note that the
6853 ;; match data will be empty on return in this case.
6854 ((and (c-major-mode-is 'pike-mode)
6855 (progn
6856 (goto-char open-brace)
6857 (= (c-backward-token-2) 0))
6858 (looking-at c-specifier-key)
6859 ;; Use this variant to avoid yet another special regexp.
6860 (c-keyword-member (c-keyword-sym (match-string 1))
6861 'c-modifier-kwds))
6862 (setq kwd-start (point))
6863 t)))
6864
6865 ;; Got a match.
6866
6867 (if goto-start
6868 ;; Back up over any preceding specifiers and their clauses
6869 ;; by going forward from `first-specifier-pos', which is the
6870 ;; earliest possible position where the specifier list can
6871 ;; start.
6872 (progn
6873 (goto-char first-specifier-pos)
6874
6875 (while (< (point) kwd-start)
6876 (if (looking-at c-symbol-key)
6877 ;; Accept any plain symbol token on the ground that
6878 ;; it's a specifier masked through a macro (just
6879 ;; like `c-forward-decl-or-cast-1' skip forward over
6880 ;; such tokens).
6881 ;;
6882 ;; Could be more restrictive wrt invalid keywords,
6883 ;; but that'd only occur in invalid code so there's
6884 ;; no use spending effort on it.
6885 (let ((end (match-end 0)))
6886 (unless (c-forward-keyword-clause 0)
6887 (goto-char end)
6888 (c-forward-syntactic-ws)))
6889
6890 ;; Can't parse a declaration preamble and is still
6891 ;; before `kwd-start'. That means `first-specifier-pos'
6892 ;; was in some earlier construct. Search again.
6893 (if (c-syntactic-re-search-forward c-symbol-start
6894 kwd-start 'move t)
6895 (goto-char (setq first-specifier-pos (match-beginning 0)))
6896 ;; Got no preamble before the block declaration keyword.
6897 (setq first-specifier-pos kwd-start))))
6898
6899 (goto-char first-specifier-pos))
6900 (goto-char kwd-start))
6901
6902 kwd-start)))
6903
6904 (defun c-search-uplist-for-classkey (paren-state)
6905 ;; Check if the closest containing paren sexp is a declaration
6906 ;; block, returning a 2 element vector in that case. Aref 0
6907 ;; contains the bufpos at boi of the class key line, and aref 1
6908 ;; contains the bufpos of the open brace. This function is an
6909 ;; obsolete wrapper for `c-looking-at-decl-block'.
6910 ;;
6911 ;; This function might do hidden buffer changes.
6912 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
6913 (when open-paren-pos
6914 (save-excursion
6915 (goto-char open-paren-pos)
6916 (when (and (eq (char-after) ?{)
6917 (c-looking-at-decl-block
6918 (c-safe-position open-paren-pos paren-state)
6919 nil))
6920 (back-to-indentation)
6921 (vector (point) open-paren-pos))))))
6922
6923 (defun c-inside-bracelist-p (containing-sexp paren-state)
6924 ;; return the buffer position of the beginning of the brace list
6925 ;; statement if we're inside a brace list, otherwise return nil.
6926 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
6927 ;; paren. PAREN-STATE is the remainder of the state of enclosing
6928 ;; braces
6929 ;;
6930 ;; N.B.: This algorithm can potentially get confused by cpp macros
6931 ;; placed in inconvenient locations. It's a trade-off we make for
6932 ;; speed.
6933 ;;
6934 ;; This function might do hidden buffer changes.
6935 (or
6936 ;; This will pick up brace list declarations.
6937 (c-safe
6938 (save-excursion
6939 (goto-char containing-sexp)
6940 (c-forward-sexp -1)
6941 (let (bracepos)
6942 (if (and (or (looking-at c-brace-list-key)
6943 (progn (c-forward-sexp -1)
6944 (looking-at c-brace-list-key)))
6945 (setq bracepos (c-down-list-forward (point)))
6946 (not (c-crosses-statement-barrier-p (point)
6947 (- bracepos 2))))
6948 (point)))))
6949 ;; this will pick up array/aggregate init lists, even if they are nested.
6950 (save-excursion
6951 (let ((class-key
6952 ;; Pike can have class definitions anywhere, so we must
6953 ;; check for the class key here.
6954 (and (c-major-mode-is 'pike-mode)
6955 c-decl-block-key))
6956 bufpos braceassignp lim next-containing)
6957 (while (and (not bufpos)
6958 containing-sexp)
6959 (when paren-state
6960 (if (consp (car paren-state))
6961 (setq lim (cdr (car paren-state))
6962 paren-state (cdr paren-state))
6963 (setq lim (car paren-state)))
6964 (when paren-state
6965 (setq next-containing (car paren-state)
6966 paren-state (cdr paren-state))))
6967 (goto-char containing-sexp)
6968 (if (c-looking-at-inexpr-block next-containing next-containing)
6969 ;; We're in an in-expression block of some kind. Do not
6970 ;; check nesting. We deliberately set the limit to the
6971 ;; containing sexp, so that c-looking-at-inexpr-block
6972 ;; doesn't check for an identifier before it.
6973 (setq containing-sexp nil)
6974 ;; see if the open brace is preceded by = or [...] in
6975 ;; this statement, but watch out for operator=
6976 (setq braceassignp 'dontknow)
6977 (c-backward-token-2 1 t lim)
6978 ;; Checks to do only on the first sexp before the brace.
6979 (when (and c-opt-inexpr-brace-list-key
6980 (eq (char-after) ?\[))
6981 ;; In Java, an initialization brace list may follow
6982 ;; directly after "new Foo[]", so check for a "new"
6983 ;; earlier.
6984 (while (eq braceassignp 'dontknow)
6985 (setq braceassignp
6986 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
6987 ((looking-at c-opt-inexpr-brace-list-key) t)
6988 ((looking-at "\\sw\\|\\s_\\|[.[]")
6989 ;; Carry on looking if this is an
6990 ;; identifier (may contain "." in Java)
6991 ;; or another "[]" sexp.
6992 'dontknow)
6993 (t nil)))))
6994 ;; Checks to do on all sexps before the brace, up to the
6995 ;; beginning of the statement.
6996 (while (eq braceassignp 'dontknow)
6997 (cond ((eq (char-after) ?\;)
6998 (setq braceassignp nil))
6999 ((and class-key
7000 (looking-at class-key))
7001 (setq braceassignp nil))
7002 ((eq (char-after) ?=)
7003 ;; We've seen a =, but must check earlier tokens so
7004 ;; that it isn't something that should be ignored.
7005 (setq braceassignp 'maybe)
7006 (while (and (eq braceassignp 'maybe)
7007 (zerop (c-backward-token-2 1 t lim)))
7008 (setq braceassignp
7009 (cond
7010 ;; Check for operator =
7011 ((and c-opt-op-identifier-prefix
7012 (looking-at c-opt-op-identifier-prefix))
7013 nil)
7014 ;; Check for `<opchar>= in Pike.
7015 ((and (c-major-mode-is 'pike-mode)
7016 (or (eq (char-after) ?`)
7017 ;; Special case for Pikes
7018 ;; `[]=, since '[' is not in
7019 ;; the punctuation class.
7020 (and (eq (char-after) ?\[)
7021 (eq (char-before) ?`))))
7022 nil)
7023 ((looking-at "\\s.") 'maybe)
7024 ;; make sure we're not in a C++ template
7025 ;; argument assignment
7026 ((and
7027 (c-major-mode-is 'c++-mode)
7028 (save-excursion
7029 (let ((here (point))
7030 (pos< (progn
7031 (skip-chars-backward "^<>")
7032 (point))))
7033 (and (eq (char-before) ?<)
7034 (not (c-crosses-statement-barrier-p
7035 pos< here))
7036 (not (c-in-literal))
7037 ))))
7038 nil)
7039 (t t))))))
7040 (if (and (eq braceassignp 'dontknow)
7041 (/= (c-backward-token-2 1 t lim) 0))
7042 (setq braceassignp nil)))
7043 (if (not braceassignp)
7044 (if (eq (char-after) ?\;)
7045 ;; Brace lists can't contain a semicolon, so we're done.
7046 (setq containing-sexp nil)
7047 ;; Go up one level.
7048 (setq containing-sexp next-containing
7049 lim nil
7050 next-containing nil))
7051 ;; we've hit the beginning of the aggregate list
7052 (c-beginning-of-statement-1
7053 (c-most-enclosing-brace paren-state))
7054 (setq bufpos (point))))
7055 )
7056 bufpos))
7057 ))
7058
7059 (defun c-looking-at-special-brace-list (&optional lim)
7060 ;; If we're looking at the start of a pike-style list, ie `({ })',
7061 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
7062 ;; positions and its entry in c-special-brace-lists is returned, nil
7063 ;; otherwise. The ending position is nil if the list is still open.
7064 ;; LIM is the limit for forward search. The point may either be at
7065 ;; the `(' or at the following paren character. Tries to check the
7066 ;; matching closer, but assumes it's correct if no balanced paren is
7067 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
7068 ;; a special brace list).
7069 ;;
7070 ;; This function might do hidden buffer changes.
7071 (if c-special-brace-lists
7072 (condition-case ()
7073 (save-excursion
7074 (let ((beg (point))
7075 inner-beg end type)
7076 (c-forward-syntactic-ws)
7077 (if (eq (char-after) ?\()
7078 (progn
7079 (forward-char 1)
7080 (c-forward-syntactic-ws)
7081 (setq inner-beg (point))
7082 (setq type (assq (char-after) c-special-brace-lists)))
7083 (if (setq type (assq (char-after) c-special-brace-lists))
7084 (progn
7085 (setq inner-beg (point))
7086 (c-backward-syntactic-ws)
7087 (forward-char -1)
7088 (setq beg (if (eq (char-after) ?\()
7089 (point)
7090 nil)))))
7091 (if (and beg type)
7092 (if (and (c-safe
7093 (goto-char beg)
7094 (c-forward-sexp 1)
7095 (setq end (point))
7096 (= (char-before) ?\)))
7097 (c-safe
7098 (goto-char inner-beg)
7099 (if (looking-at "\\s(")
7100 ;; Check balancing of the inner paren
7101 ;; below.
7102 (progn
7103 (c-forward-sexp 1)
7104 t)
7105 ;; If the inner char isn't a paren then
7106 ;; we can't check balancing, so just
7107 ;; check the char before the outer
7108 ;; closing paren.
7109 (goto-char end)
7110 (backward-char)
7111 (c-backward-syntactic-ws)
7112 (= (char-before) (cdr type)))))
7113 (if (or (/= (char-syntax (char-before)) ?\))
7114 (= (progn
7115 (c-forward-syntactic-ws)
7116 (point))
7117 (1- end)))
7118 (cons (cons beg end) type))
7119 (cons (list beg) type)))))
7120 (error nil))))
7121
7122 (defun c-looking-at-bos (&optional lim)
7123 ;; Return non-nil if between two statements or declarations, assuming
7124 ;; point is not inside a literal or comment.
7125 ;;
7126 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
7127 ;; are recommended instead.
7128 ;;
7129 ;; This function might do hidden buffer changes.
7130 (c-at-statement-start-p))
7131 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
7132
7133 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
7134 ;; Return non-nil if we're looking at the beginning of a block
7135 ;; inside an expression. The value returned is actually a cons of
7136 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
7137 ;; position of the beginning of the construct.
7138 ;;
7139 ;; LIM limits the backward search. CONTAINING-SEXP is the start
7140 ;; position of the closest containing list. If it's nil, the
7141 ;; containing paren isn't used to decide whether we're inside an
7142 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
7143 ;; needs to be farther back.
7144 ;;
7145 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
7146 ;; brace block might be done. It should only be used when the
7147 ;; construct can be assumed to be complete, i.e. when the original
7148 ;; starting position was further down than that.
7149 ;;
7150 ;; This function might do hidden buffer changes.
7151
7152 (save-excursion
7153 (let ((res 'maybe) passed-paren
7154 (closest-lim (or containing-sexp lim (point-min)))
7155 ;; Look at the character after point only as a last resort
7156 ;; when we can't disambiguate.
7157 (block-follows (and (eq (char-after) ?{) (point))))
7158
7159 (while (and (eq res 'maybe)
7160 (progn (c-backward-syntactic-ws)
7161 (> (point) closest-lim))
7162 (not (bobp))
7163 (progn (backward-char)
7164 (looking-at "[\]\).]\\|\\w\\|\\s_"))
7165 (c-safe (forward-char)
7166 (goto-char (scan-sexps (point) -1))))
7167
7168 (setq res
7169 (if (looking-at c-keywords-regexp)
7170 (let ((kw-sym (c-keyword-sym (match-string 1))))
7171 (cond
7172 ((and block-follows
7173 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
7174 (and (not (eq passed-paren ?\[))
7175 (or (not (looking-at c-class-key))
7176 ;; If the class definition is at the start of
7177 ;; a statement, we don't consider it an
7178 ;; in-expression class.
7179 (let ((prev (point)))
7180 (while (and
7181 (= (c-backward-token-2 1 nil closest-lim) 0)
7182 (eq (char-syntax (char-after)) ?w))
7183 (setq prev (point)))
7184 (goto-char prev)
7185 (not (c-at-statement-start-p)))
7186 ;; Also, in Pike we treat it as an
7187 ;; in-expression class if it's used in an
7188 ;; object clone expression.
7189 (save-excursion
7190 (and check-at-end
7191 (c-major-mode-is 'pike-mode)
7192 (progn (goto-char block-follows)
7193 (zerop (c-forward-token-2 1 t)))
7194 (eq (char-after) ?\())))
7195 (cons 'inexpr-class (point))))
7196 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
7197 (when (not passed-paren)
7198 (cons 'inexpr-statement (point))))
7199 ((c-keyword-member kw-sym 'c-lambda-kwds)
7200 (when (or (not passed-paren)
7201 (eq passed-paren ?\())
7202 (cons 'inlambda (point))))
7203 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
7204 nil)
7205 (t
7206 'maybe)))
7207
7208 (if (looking-at "\\s(")
7209 (if passed-paren
7210 (if (and (eq passed-paren ?\[)
7211 (eq (char-after) ?\[))
7212 ;; Accept several square bracket sexps for
7213 ;; Java array initializations.
7214 'maybe)
7215 (setq passed-paren (char-after))
7216 'maybe)
7217 'maybe))))
7218
7219 (if (eq res 'maybe)
7220 (when (and c-recognize-paren-inexpr-blocks
7221 block-follows
7222 containing-sexp
7223 (eq (char-after containing-sexp) ?\())
7224 (goto-char containing-sexp)
7225 (if (or (save-excursion
7226 (c-backward-syntactic-ws lim)
7227 (and (> (point) (or lim (point-min)))
7228 (c-on-identifier)))
7229 (and c-special-brace-lists
7230 (c-looking-at-special-brace-list)))
7231 nil
7232 (cons 'inexpr-statement (point))))
7233
7234 res))))
7235
7236 (defun c-looking-at-inexpr-block-backward (paren-state)
7237 ;; Returns non-nil if we're looking at the end of an in-expression
7238 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
7239 ;; PAREN-STATE is the paren state relevant at the current position.
7240 ;;
7241 ;; This function might do hidden buffer changes.
7242 (save-excursion
7243 ;; We currently only recognize a block.
7244 (let ((here (point))
7245 (elem (car-safe paren-state))
7246 containing-sexp)
7247 (when (and (consp elem)
7248 (progn (goto-char (cdr elem))
7249 (c-forward-syntactic-ws here)
7250 (= (point) here)))
7251 (goto-char (car elem))
7252 (if (setq paren-state (cdr paren-state))
7253 (setq containing-sexp (car-safe paren-state)))
7254 (c-looking-at-inexpr-block (c-safe-position containing-sexp
7255 paren-state)
7256 containing-sexp)))))
7257
7258 \f
7259 ;; `c-guess-basic-syntax' and the functions that precedes it below
7260 ;; implements the main decision tree for determining the syntactic
7261 ;; analysis of the current line of code.
7262
7263 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
7264 ;; auto newline analysis.
7265 (defvar c-auto-newline-analysis nil)
7266
7267 (defun c-brace-anchor-point (bracepos)
7268 ;; BRACEPOS is the position of a brace in a construct like "namespace
7269 ;; Bar {". Return the anchor point in this construct; this is the
7270 ;; earliest symbol on the brace's line which isn't earlier than
7271 ;; "namespace".
7272 ;;
7273 ;; Currently (2007-08-17), "like namespace" means "matches
7274 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
7275 ;; or anything like that.
7276 (save-excursion
7277 (let ((boi (c-point 'boi bracepos)))
7278 (goto-char bracepos)
7279 (while (and (> (point) boi)
7280 (not (looking-at c-other-decl-block-key)))
7281 (c-backward-token-2))
7282 (if (> (point) boi) (point) boi))))
7283
7284 (defsubst c-add-syntax (symbol &rest args)
7285 ;; A simple function to prepend a new syntax element to
7286 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
7287 ;; should always be dynamically bound but since we read it first
7288 ;; we'll fail properly anyway if this function is misused.
7289 (setq c-syntactic-context (cons (cons symbol args)
7290 c-syntactic-context)))
7291
7292 (defsubst c-append-syntax (symbol &rest args)
7293 ;; Like `c-add-syntax' but appends to the end of the syntax list.
7294 ;; (Normally not necessary.)
7295 (setq c-syntactic-context (nconc c-syntactic-context
7296 (list (cons symbol args)))))
7297
7298 (defun c-add-stmt-syntax (syntax-symbol
7299 syntax-extra-args
7300 stop-at-boi-only
7301 containing-sexp
7302 paren-state)
7303 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
7304 ;; needed with further syntax elements of the types `substatement',
7305 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
7306 ;; `defun-block-intro'.
7307 ;;
7308 ;; Do the generic processing to anchor the given syntax symbol on
7309 ;; the preceding statement: Skip over any labels and containing
7310 ;; statements on the same line, and then search backward until we
7311 ;; find a statement or block start that begins at boi without a
7312 ;; label or comment.
7313 ;;
7314 ;; Point is assumed to be at the prospective anchor point for the
7315 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
7316 ;; skip past open parens and containing statements. Most of the added
7317 ;; syntax elements will get the same anchor point - the exception is
7318 ;; for an anchor in a construct like "namespace"[*] - this is as early
7319 ;; as possible in the construct but on the same line as the {.
7320 ;;
7321 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
7322 ;;
7323 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
7324 ;; syntax symbol. They are appended after the anchor point.
7325 ;;
7326 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
7327 ;; if the current statement starts there.
7328 ;;
7329 ;; Note: It's not a problem if PAREN-STATE "overshoots"
7330 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
7331 ;;
7332 ;; This function might do hidden buffer changes.
7333
7334 (if (= (point) (c-point 'boi))
7335 ;; This is by far the most common case, so let's give it special
7336 ;; treatment.
7337 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
7338
7339 (let ((syntax-last c-syntactic-context)
7340 (boi (c-point 'boi))
7341 ;; Set when we're on a label, so that we don't stop there.
7342 ;; FIXME: To be complete we should check if we're on a label
7343 ;; now at the start.
7344 on-label)
7345
7346 ;; Use point as the anchor point for "namespace", "extern", etc.
7347 (apply 'c-add-syntax syntax-symbol
7348 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
7349 (point) nil)
7350 syntax-extra-args)
7351
7352 ;; Loop while we have to back out of containing blocks.
7353 (while
7354 (and
7355 (catch 'back-up-block
7356
7357 ;; Loop while we have to back up statements.
7358 (while (or (/= (point) boi)
7359 on-label
7360 (looking-at c-comment-start-regexp))
7361
7362 ;; Skip past any comments that stands between the
7363 ;; statement start and boi.
7364 (let ((savepos (point)))
7365 (while (and (/= savepos boi)
7366 (c-backward-single-comment))
7367 (setq savepos (point)
7368 boi (c-point 'boi)))
7369 (goto-char savepos))
7370
7371 ;; Skip to the beginning of this statement or backward
7372 ;; another one.
7373 (let ((old-pos (point))
7374 (old-boi boi)
7375 (step-type (c-beginning-of-statement-1 containing-sexp)))
7376 (setq boi (c-point 'boi)
7377 on-label (eq step-type 'label))
7378
7379 (cond ((= (point) old-pos)
7380 ;; If we didn't move we're at the start of a block and
7381 ;; have to continue outside it.
7382 (throw 'back-up-block t))
7383
7384 ((and (eq step-type 'up)
7385 (>= (point) old-boi)
7386 (looking-at "else\\>[^_]")
7387 (save-excursion
7388 (goto-char old-pos)
7389 (looking-at "if\\>[^_]")))
7390 ;; Special case to avoid deeper and deeper indentation
7391 ;; of "else if" clauses.
7392 )
7393
7394 ((and (not stop-at-boi-only)
7395 (/= old-pos old-boi)
7396 (memq step-type '(up previous)))
7397 ;; If stop-at-boi-only is nil, we shouldn't back up
7398 ;; over previous or containing statements to try to
7399 ;; reach boi, so go back to the last position and
7400 ;; exit.
7401 (goto-char old-pos)
7402 (throw 'back-up-block nil))
7403
7404 (t
7405 (if (and (not stop-at-boi-only)
7406 (memq step-type '(up previous beginning)))
7407 ;; If we've moved into another statement then we
7408 ;; should no longer try to stop in the middle of a
7409 ;; line.
7410 (setq stop-at-boi-only t))
7411
7412 ;; Record this as a substatement if we skipped up one
7413 ;; level.
7414 (when (eq step-type 'up)
7415 (c-add-syntax 'substatement nil))))
7416 )))
7417
7418 containing-sexp)
7419
7420 ;; Now we have to go out of this block.
7421 (goto-char containing-sexp)
7422
7423 ;; Don't stop in the middle of a special brace list opener
7424 ;; like "({".
7425 (when c-special-brace-lists
7426 (let ((special-list (c-looking-at-special-brace-list)))
7427 (when (and special-list
7428 (< (car (car special-list)) (point)))
7429 (setq containing-sexp (car (car special-list)))
7430 (goto-char containing-sexp))))
7431
7432 (setq paren-state (c-whack-state-after containing-sexp paren-state)
7433 containing-sexp (c-most-enclosing-brace paren-state)
7434 boi (c-point 'boi))
7435
7436 ;; Analyze the construct in front of the block we've stepped out
7437 ;; from and add the right syntactic element for it.
7438 (let ((paren-pos (point))
7439 (paren-char (char-after))
7440 step-type)
7441
7442 (if (eq paren-char ?\()
7443 ;; Stepped out of a parenthesis block, so we're in an
7444 ;; expression now.
7445 (progn
7446 (when (/= paren-pos boi)
7447 (if (and c-recognize-paren-inexpr-blocks
7448 (progn
7449 (c-backward-syntactic-ws containing-sexp)
7450 (or (not (looking-at "\\>"))
7451 (not (c-on-identifier))))
7452 (save-excursion
7453 (goto-char (1+ paren-pos))
7454 (c-forward-syntactic-ws)
7455 (eq (char-after) ?{)))
7456 ;; Stepped out of an in-expression statement. This
7457 ;; syntactic element won't get an anchor pos.
7458 (c-add-syntax 'inexpr-statement)
7459
7460 ;; A parenthesis normally belongs to an arglist.
7461 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
7462
7463 (goto-char (max boi
7464 (if containing-sexp
7465 (1+ containing-sexp)
7466 (point-min))))
7467 (setq step-type 'same
7468 on-label nil))
7469
7470 ;; Stepped out of a brace block.
7471 (setq step-type (c-beginning-of-statement-1 containing-sexp)
7472 on-label (eq step-type 'label))
7473
7474 (if (and (eq step-type 'same)
7475 (/= paren-pos (point)))
7476 (let (inexpr)
7477 (cond
7478 ((save-excursion
7479 (goto-char paren-pos)
7480 (setq inexpr (c-looking-at-inexpr-block
7481 (c-safe-position containing-sexp paren-state)
7482 containing-sexp)))
7483 (c-add-syntax (if (eq (car inexpr) 'inlambda)
7484 'defun-block-intro
7485 'statement-block-intro)
7486 nil))
7487 ((looking-at c-other-decl-block-key)
7488 (c-add-syntax
7489 (cdr (assoc (match-string 1)
7490 c-other-decl-block-key-in-symbols-alist))
7491 (max (c-point 'boi paren-pos) (point))))
7492 (t (c-add-syntax 'defun-block-intro nil))))
7493
7494 (c-add-syntax 'statement-block-intro nil)))
7495
7496 (if (= paren-pos boi)
7497 ;; Always done if the open brace was at boi. The
7498 ;; c-beginning-of-statement-1 call above is necessary
7499 ;; anyway, to decide the type of block-intro to add.
7500 (goto-char paren-pos)
7501 (setq boi (c-point 'boi)))
7502 ))
7503
7504 ;; Fill in the current point as the anchor for all the symbols
7505 ;; added above.
7506 (let ((p c-syntactic-context) q)
7507 (while (not (eq p syntax-last))
7508 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
7509 (while q
7510 (unless (car q)
7511 (setcar q (point)))
7512 (setq q (cdr q)))
7513 (setq p (cdr p))))
7514 )))
7515
7516 (defun c-add-class-syntax (symbol
7517 containing-decl-open
7518 containing-decl-start
7519 containing-decl-kwd
7520 paren-state)
7521 ;; The inclass and class-close syntactic symbols are added in
7522 ;; several places and some work is needed to fix everything.
7523 ;; Therefore it's collected here.
7524 ;;
7525 ;; This function might do hidden buffer changes.
7526 (goto-char containing-decl-open)
7527 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
7528 (progn
7529 (c-add-syntax symbol containing-decl-open)
7530 containing-decl-open)
7531 (goto-char containing-decl-start)
7532 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
7533 ;; here, but we have to do like this for compatibility.
7534 (back-to-indentation)
7535 (c-add-syntax symbol (point))
7536 (if (and (c-keyword-member containing-decl-kwd
7537 'c-inexpr-class-kwds)
7538 (/= containing-decl-start (c-point 'boi containing-decl-start)))
7539 (c-add-syntax 'inexpr-class))
7540 (point)))
7541
7542 (defun c-guess-continued-construct (indent-point
7543 char-after-ip
7544 beg-of-same-or-containing-stmt
7545 containing-sexp
7546 paren-state)
7547 ;; This function contains the decision tree reached through both
7548 ;; cases 18 and 10. It's a continued statement or top level
7549 ;; construct of some kind.
7550 ;;
7551 ;; This function might do hidden buffer changes.
7552
7553 (let (special-brace-list)
7554 (goto-char indent-point)
7555 (skip-chars-forward " \t")
7556
7557 (cond
7558 ;; (CASE A removed.)
7559 ;; CASE B: open braces for class or brace-lists
7560 ((setq special-brace-list
7561 (or (and c-special-brace-lists
7562 (c-looking-at-special-brace-list))
7563 (eq char-after-ip ?{)))
7564
7565 (cond
7566 ;; CASE B.1: class-open
7567 ((save-excursion
7568 (and (eq (char-after) ?{)
7569 (c-looking-at-decl-block containing-sexp t)
7570 (setq beg-of-same-or-containing-stmt (point))))
7571 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
7572
7573 ;; CASE B.2: brace-list-open
7574 ((or (consp special-brace-list)
7575 (save-excursion
7576 (goto-char beg-of-same-or-containing-stmt)
7577 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
7578 indent-point t t t)))
7579 ;; The most semantically accurate symbol here is
7580 ;; brace-list-open, but we normally report it simply as a
7581 ;; statement-cont. The reason is that one normally adjusts
7582 ;; brace-list-open for brace lists as top-level constructs,
7583 ;; and brace lists inside statements is a completely different
7584 ;; context. C.f. case 5A.3.
7585 (c-beginning-of-statement-1 containing-sexp)
7586 (c-add-stmt-syntax (if c-auto-newline-analysis
7587 ;; Turn off the dwim above when we're
7588 ;; analyzing the nature of the brace
7589 ;; for the auto newline feature.
7590 'brace-list-open
7591 'statement-cont)
7592 nil nil
7593 containing-sexp paren-state))
7594
7595 ;; CASE B.3: The body of a function declared inside a normal
7596 ;; block. Can occur e.g. in Pike and when using gcc
7597 ;; extensions, but watch out for macros followed by blocks.
7598 ;; C.f. cases E, 16F and 17G.
7599 ((and (not (c-at-statement-start-p))
7600 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7601 'same)
7602 (save-excursion
7603 (let ((c-recognize-typeless-decls nil))
7604 ;; Turn off recognition of constructs that lacks a
7605 ;; type in this case, since that's more likely to be
7606 ;; a macro followed by a block.
7607 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7608 (c-add-stmt-syntax 'defun-open nil t
7609 containing-sexp paren-state))
7610
7611 ;; CASE B.4: Continued statement with block open. The most
7612 ;; accurate analysis is perhaps `statement-cont' together with
7613 ;; `block-open' but we play DWIM and use `substatement-open'
7614 ;; instead. The rationaly is that this typically is a macro
7615 ;; followed by a block which makes it very similar to a
7616 ;; statement with a substatement block.
7617 (t
7618 (c-add-stmt-syntax 'substatement-open nil nil
7619 containing-sexp paren-state))
7620 ))
7621
7622 ;; CASE C: iostream insertion or extraction operator
7623 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
7624 (save-excursion
7625 (goto-char beg-of-same-or-containing-stmt)
7626 ;; If there is no preceding streamop in the statement
7627 ;; then indent this line as a normal statement-cont.
7628 (when (c-syntactic-re-search-forward
7629 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
7630 (c-add-syntax 'stream-op (c-point 'boi))
7631 t))))
7632
7633 ;; CASE E: In the "K&R region" of a function declared inside a
7634 ;; normal block. C.f. case B.3.
7635 ((and (save-excursion
7636 ;; Check that the next token is a '{'. This works as
7637 ;; long as no language that allows nested function
7638 ;; definitions allows stuff like member init lists, K&R
7639 ;; declarations or throws clauses there.
7640 ;;
7641 ;; Note that we do a forward search for something ahead
7642 ;; of the indentation line here. That's not good since
7643 ;; the user might not have typed it yet. Unfortunately
7644 ;; it's exceedingly tricky to recognize a function
7645 ;; prototype in a code block without resorting to this.
7646 (c-forward-syntactic-ws)
7647 (eq (char-after) ?{))
7648 (not (c-at-statement-start-p))
7649 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7650 'same)
7651 (save-excursion
7652 (let ((c-recognize-typeless-decls nil))
7653 ;; Turn off recognition of constructs that lacks a
7654 ;; type in this case, since that's more likely to be
7655 ;; a macro followed by a block.
7656 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7657 (c-add-stmt-syntax 'func-decl-cont nil t
7658 containing-sexp paren-state))
7659
7660 ;; CASE D: continued statement.
7661 (t
7662 (c-beginning-of-statement-1 containing-sexp)
7663 (c-add-stmt-syntax 'statement-cont nil nil
7664 containing-sexp paren-state))
7665 )))
7666
7667 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
7668 ;; 2005/11/29).
7669 ;;;###autoload
7670 (defun c-guess-basic-syntax ()
7671 "Return the syntactic context of the current line."
7672 (save-excursion
7673 (beginning-of-line)
7674 (c-save-buffer-state
7675 ((indent-point (point))
7676 (case-fold-search nil)
7677 ;; A whole ugly bunch of various temporary variables. Have
7678 ;; to declare them here since it's not possible to declare
7679 ;; a variable with only the scope of a cond test and the
7680 ;; following result clauses, and most of this function is a
7681 ;; single gigantic cond. :P
7682 literal char-before-ip before-ws-ip char-after-ip macro-start
7683 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
7684 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
7685 containing-<
7686 ;; The following record some positions for the containing
7687 ;; declaration block if we're directly within one:
7688 ;; `containing-decl-open' is the position of the open
7689 ;; brace. `containing-decl-start' is the start of the
7690 ;; declaration. `containing-decl-kwd' is the keyword
7691 ;; symbol of the keyword that tells what kind of block it
7692 ;; is.
7693 containing-decl-open
7694 containing-decl-start
7695 containing-decl-kwd
7696 ;; The open paren of the closest surrounding sexp or nil if
7697 ;; there is none.
7698 containing-sexp
7699 ;; The position after the closest preceding brace sexp
7700 ;; (nested sexps are ignored), or the position after
7701 ;; `containing-sexp' if there is none, or (point-min) if
7702 ;; `containing-sexp' is nil.
7703 lim
7704 ;; The paren state outside `containing-sexp', or at
7705 ;; `indent-point' if `containing-sexp' is nil.
7706 (paren-state (c-parse-state))
7707 ;; There's always at most one syntactic element which got
7708 ;; an anchor pos. It's stored in syntactic-relpos.
7709 syntactic-relpos
7710 (c-stmt-delim-chars c-stmt-delim-chars))
7711
7712 ;; Check if we're directly inside an enclosing declaration
7713 ;; level block.
7714 (when (and (setq containing-sexp
7715 (c-most-enclosing-brace paren-state))
7716 (progn
7717 (goto-char containing-sexp)
7718 (eq (char-after) ?{))
7719 (setq placeholder
7720 (c-looking-at-decl-block
7721 (c-most-enclosing-brace paren-state
7722 containing-sexp)
7723 t)))
7724 (setq containing-decl-open containing-sexp
7725 containing-decl-start (point)
7726 containing-sexp nil)
7727 (goto-char placeholder)
7728 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
7729 (c-keyword-sym (match-string 1)))))
7730
7731 ;; Init some position variables.
7732 (if c-state-cache
7733 (progn
7734 (setq containing-sexp (car paren-state)
7735 paren-state (cdr paren-state))
7736 (if (consp containing-sexp)
7737 (progn
7738 (setq lim (cdr containing-sexp))
7739 (if (cdr c-state-cache)
7740 ;; Ignore balanced paren. The next entry
7741 ;; can't be another one.
7742 (setq containing-sexp (car (cdr c-state-cache))
7743 paren-state (cdr paren-state))
7744 ;; If there is no surrounding open paren then
7745 ;; put the last balanced pair back on paren-state.
7746 (setq paren-state (cons containing-sexp paren-state)
7747 containing-sexp nil)))
7748 (setq lim (1+ containing-sexp))))
7749 (setq lim (point-min)))
7750
7751 ;; If we're in a parenthesis list then ',' delimits the
7752 ;; "statements" rather than being an operator (with the
7753 ;; exception of the "for" clause). This difference is
7754 ;; typically only noticeable when statements are used in macro
7755 ;; arglists.
7756 (when (and containing-sexp
7757 (eq (char-after containing-sexp) ?\())
7758 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
7759
7760 ;; cache char before and after indent point, and move point to
7761 ;; the most likely position to perform the majority of tests
7762 (goto-char indent-point)
7763 (c-backward-syntactic-ws lim)
7764 (setq before-ws-ip (point)
7765 char-before-ip (char-before))
7766 (goto-char indent-point)
7767 (skip-chars-forward " \t")
7768 (setq char-after-ip (char-after))
7769
7770 ;; are we in a literal?
7771 (setq literal (c-in-literal lim))
7772
7773 ;; now figure out syntactic qualities of the current line
7774 (cond
7775
7776 ;; CASE 1: in a string.
7777 ((eq literal 'string)
7778 (c-add-syntax 'string (c-point 'bopl)))
7779
7780 ;; CASE 2: in a C or C++ style comment.
7781 ((and (memq literal '(c c++))
7782 ;; This is a kludge for XEmacs where we use
7783 ;; `buffer-syntactic-context', which doesn't correctly
7784 ;; recognize "\*/" to end a block comment.
7785 ;; `parse-partial-sexp' which is used by
7786 ;; `c-literal-limits' will however do that in most
7787 ;; versions, which results in that we get nil from
7788 ;; `c-literal-limits' even when `c-in-literal' claims
7789 ;; we're inside a comment.
7790 (setq placeholder (c-literal-limits lim)))
7791 (c-add-syntax literal (car placeholder)))
7792
7793 ;; CASE 3: in a cpp preprocessor macro continuation.
7794 ((and (save-excursion
7795 (when (c-beginning-of-macro)
7796 (setq macro-start (point))))
7797 (/= macro-start (c-point 'boi))
7798 (progn
7799 (setq tmpsymbol 'cpp-macro-cont)
7800 (or (not c-syntactic-indentation-in-macros)
7801 (save-excursion
7802 (goto-char macro-start)
7803 ;; If at the beginning of the body of a #define
7804 ;; directive then analyze as cpp-define-intro
7805 ;; only. Go on with the syntactic analysis
7806 ;; otherwise. in-macro-expr is set if we're in a
7807 ;; cpp expression, i.e. before the #define body
7808 ;; or anywhere in a non-#define directive.
7809 (if (c-forward-to-cpp-define-body)
7810 (let ((indent-boi (c-point 'boi indent-point)))
7811 (setq in-macro-expr (> (point) indent-boi)
7812 tmpsymbol 'cpp-define-intro)
7813 (= (point) indent-boi))
7814 (setq in-macro-expr t)
7815 nil)))))
7816 (c-add-syntax tmpsymbol macro-start)
7817 (setq macro-start nil))
7818
7819 ;; CASE 11: an else clause?
7820 ((looking-at "else\\>[^_]")
7821 (c-beginning-of-statement-1 containing-sexp)
7822 (c-add-stmt-syntax 'else-clause nil t
7823 containing-sexp paren-state))
7824
7825 ;; CASE 12: while closure of a do/while construct?
7826 ((and (looking-at "while\\>[^_]")
7827 (save-excursion
7828 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
7829 'beginning)
7830 (setq placeholder (point)))))
7831 (goto-char placeholder)
7832 (c-add-stmt-syntax 'do-while-closure nil t
7833 containing-sexp paren-state))
7834
7835 ;; CASE 13: A catch or finally clause? This case is simpler
7836 ;; than if-else and do-while, because a block is required
7837 ;; after every try, catch and finally.
7838 ((save-excursion
7839 (and (cond ((c-major-mode-is 'c++-mode)
7840 (looking-at "catch\\>[^_]"))
7841 ((c-major-mode-is 'java-mode)
7842 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
7843 (and (c-safe (c-backward-syntactic-ws)
7844 (c-backward-sexp)
7845 t)
7846 (eq (char-after) ?{)
7847 (c-safe (c-backward-syntactic-ws)
7848 (c-backward-sexp)
7849 t)
7850 (if (eq (char-after) ?\()
7851 (c-safe (c-backward-sexp) t)
7852 t))
7853 (looking-at "\\(try\\|catch\\)\\>[^_]")
7854 (setq placeholder (point))))
7855 (goto-char placeholder)
7856 (c-add-stmt-syntax 'catch-clause nil t
7857 containing-sexp paren-state))
7858
7859 ;; CASE 18: A substatement we can recognize by keyword.
7860 ((save-excursion
7861 (and c-opt-block-stmt-key
7862 (not (eq char-before-ip ?\;))
7863 (not (c-at-vsemi-p before-ws-ip))
7864 (not (memq char-after-ip '(?\) ?\] ?,)))
7865 (or (not (eq char-before-ip ?}))
7866 (c-looking-at-inexpr-block-backward c-state-cache))
7867 (> (point)
7868 (progn
7869 ;; Ought to cache the result from the
7870 ;; c-beginning-of-statement-1 calls here.
7871 (setq placeholder (point))
7872 (while (eq (setq step-type
7873 (c-beginning-of-statement-1 lim))
7874 'label))
7875 (if (eq step-type 'previous)
7876 (goto-char placeholder)
7877 (setq placeholder (point))
7878 (if (and (eq step-type 'same)
7879 (not (looking-at c-opt-block-stmt-key)))
7880 ;; Step up to the containing statement if we
7881 ;; stayed in the same one.
7882 (let (step)
7883 (while (eq
7884 (setq step
7885 (c-beginning-of-statement-1 lim))
7886 'label))
7887 (if (eq step 'up)
7888 (setq placeholder (point))
7889 ;; There was no containing statement afterall.
7890 (goto-char placeholder)))))
7891 placeholder))
7892 (if (looking-at c-block-stmt-2-key)
7893 ;; Require a parenthesis after these keywords.
7894 ;; Necessary to catch e.g. synchronized in Java,
7895 ;; which can be used both as statement and
7896 ;; modifier.
7897 (and (zerop (c-forward-token-2 1 nil))
7898 (eq (char-after) ?\())
7899 (looking-at c-opt-block-stmt-key))))
7900
7901 (if (eq step-type 'up)
7902 ;; CASE 18A: Simple substatement.
7903 (progn
7904 (goto-char placeholder)
7905 (cond
7906 ((eq char-after-ip ?{)
7907 (c-add-stmt-syntax 'substatement-open nil nil
7908 containing-sexp paren-state))
7909 ((save-excursion
7910 (goto-char indent-point)
7911 (back-to-indentation)
7912 (c-forward-label))
7913 (c-add-stmt-syntax 'substatement-label nil nil
7914 containing-sexp paren-state))
7915 (t
7916 (c-add-stmt-syntax 'substatement nil nil
7917 containing-sexp paren-state))))
7918
7919 ;; CASE 18B: Some other substatement. This is shared
7920 ;; with case 10.
7921 (c-guess-continued-construct indent-point
7922 char-after-ip
7923 placeholder
7924 lim
7925 paren-state)))
7926
7927 ;; CASE 14: A case or default label
7928 ((looking-at c-label-kwds-regexp)
7929 (if containing-sexp
7930 (progn
7931 (goto-char containing-sexp)
7932 (setq lim (c-most-enclosing-brace c-state-cache
7933 containing-sexp))
7934 (c-backward-to-block-anchor lim)
7935 (c-add-stmt-syntax 'case-label nil t lim paren-state))
7936 ;; Got a bogus label at the top level. In lack of better
7937 ;; alternatives, anchor it on (point-min).
7938 (c-add-syntax 'case-label (point-min))))
7939
7940 ;; CASE 15: any other label
7941 ((save-excursion
7942 (back-to-indentation)
7943 (and (not (looking-at c-syntactic-ws-start))
7944 (c-forward-label)))
7945 (cond (containing-decl-open
7946 (setq placeholder (c-add-class-syntax 'inclass
7947 containing-decl-open
7948 containing-decl-start
7949 containing-decl-kwd
7950 paren-state))
7951 ;; Append access-label with the same anchor point as
7952 ;; inclass gets.
7953 (c-append-syntax 'access-label placeholder))
7954
7955 (containing-sexp
7956 (goto-char containing-sexp)
7957 (setq lim (c-most-enclosing-brace c-state-cache
7958 containing-sexp))
7959 (save-excursion
7960 (setq tmpsymbol
7961 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
7962 (looking-at "switch\\>[^_]"))
7963 ;; If the surrounding statement is a switch then
7964 ;; let's analyze all labels as switch labels, so
7965 ;; that they get lined up consistently.
7966 'case-label
7967 'label)))
7968 (c-backward-to-block-anchor lim)
7969 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
7970
7971 (t
7972 ;; A label on the top level. Treat it as a class
7973 ;; context. (point-min) is the closest we get to the
7974 ;; class open brace.
7975 (c-add-syntax 'access-label (point-min)))))
7976
7977 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
7978 ;; 17E.
7979 ((setq placeholder (c-looking-at-inexpr-block
7980 (c-safe-position containing-sexp paren-state)
7981 containing-sexp
7982 ;; Have to turn on the heuristics after
7983 ;; the point even though it doesn't work
7984 ;; very well. C.f. test case class-16.pike.
7985 t))
7986 (setq tmpsymbol (assq (car placeholder)
7987 '((inexpr-class . class-open)
7988 (inexpr-statement . block-open))))
7989 (if tmpsymbol
7990 ;; It's a statement block or an anonymous class.
7991 (setq tmpsymbol (cdr tmpsymbol))
7992 ;; It's a Pike lambda. Check whether we are between the
7993 ;; lambda keyword and the argument list or at the defun
7994 ;; opener.
7995 (setq tmpsymbol (if (eq char-after-ip ?{)
7996 'inline-open
7997 'lambda-intro-cont)))
7998 (goto-char (cdr placeholder))
7999 (back-to-indentation)
8000 (c-add-stmt-syntax tmpsymbol nil t
8001 (c-most-enclosing-brace c-state-cache (point))
8002 paren-state)
8003 (unless (eq (point) (cdr placeholder))
8004 (c-add-syntax (car placeholder))))
8005
8006 ;; CASE 5: Line is inside a declaration level block or at top level.
8007 ((or containing-decl-open (null containing-sexp))
8008 (cond
8009
8010 ;; CASE 5A: we are looking at a defun, brace list, class,
8011 ;; or inline-inclass method opening brace
8012 ((setq special-brace-list
8013 (or (and c-special-brace-lists
8014 (c-looking-at-special-brace-list))
8015 (eq char-after-ip ?{)))
8016 (cond
8017
8018 ;; CASE 5A.1: Non-class declaration block open.
8019 ((save-excursion
8020 (let (tmp)
8021 (and (eq char-after-ip ?{)
8022 (setq tmp (c-looking-at-decl-block containing-sexp t))
8023 (progn
8024 (setq placeholder (point))
8025 (goto-char tmp)
8026 (looking-at c-symbol-key))
8027 (c-keyword-member
8028 (c-keyword-sym (setq keyword (match-string 0)))
8029 'c-other-block-decl-kwds))))
8030 (goto-char placeholder)
8031 (c-add-stmt-syntax
8032 (if (string-equal keyword "extern")
8033 ;; Special case for extern-lang-open.
8034 'extern-lang-open
8035 (intern (concat keyword "-open")))
8036 nil t containing-sexp paren-state))
8037
8038 ;; CASE 5A.2: we are looking at a class opening brace
8039 ((save-excursion
8040 (goto-char indent-point)
8041 (skip-chars-forward " \t")
8042 (and (eq (char-after) ?{)
8043 (c-looking-at-decl-block containing-sexp t)
8044 (setq placeholder (point))))
8045 (c-add-syntax 'class-open placeholder))
8046
8047 ;; CASE 5A.3: brace list open
8048 ((save-excursion
8049 (c-beginning-of-decl-1 lim)
8050 (while (looking-at c-specifier-key)
8051 (goto-char (match-end 1))
8052 (c-forward-syntactic-ws indent-point))
8053 (setq placeholder (c-point 'boi))
8054 (or (consp special-brace-list)
8055 (and (or (save-excursion
8056 (goto-char indent-point)
8057 (setq tmpsymbol nil)
8058 (while (and (> (point) placeholder)
8059 (zerop (c-backward-token-2 1 t))
8060 (/= (char-after) ?=))
8061 (and c-opt-inexpr-brace-list-key
8062 (not tmpsymbol)
8063 (looking-at c-opt-inexpr-brace-list-key)
8064 (setq tmpsymbol 'topmost-intro-cont)))
8065 (eq (char-after) ?=))
8066 (looking-at c-brace-list-key))
8067 (save-excursion
8068 (while (and (< (point) indent-point)
8069 (zerop (c-forward-token-2 1 t))
8070 (not (memq (char-after) '(?\; ?\()))))
8071 (not (memq (char-after) '(?\; ?\()))
8072 ))))
8073 (if (and (not c-auto-newline-analysis)
8074 (c-major-mode-is 'java-mode)
8075 (eq tmpsymbol 'topmost-intro-cont))
8076 ;; We're in Java and have found that the open brace
8077 ;; belongs to a "new Foo[]" initialization list,
8078 ;; which means the brace list is part of an
8079 ;; expression and not a top level definition. We
8080 ;; therefore treat it as any topmost continuation
8081 ;; even though the semantically correct symbol still
8082 ;; is brace-list-open, on the same grounds as in
8083 ;; case B.2.
8084 (progn
8085 (c-beginning-of-statement-1 lim)
8086 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8087 (c-add-syntax 'brace-list-open placeholder)))
8088
8089 ;; CASE 5A.4: inline defun open
8090 ((and containing-decl-open
8091 (not (c-keyword-member containing-decl-kwd
8092 'c-other-block-decl-kwds)))
8093 (c-add-syntax 'inline-open)
8094 (c-add-class-syntax 'inclass
8095 containing-decl-open
8096 containing-decl-start
8097 containing-decl-kwd
8098 paren-state))
8099
8100 ;; CASE 5A.5: ordinary defun open
8101 (t
8102 (save-excursion
8103 (c-beginning-of-decl-1 lim)
8104 (while (looking-at c-specifier-key)
8105 (goto-char (match-end 1))
8106 (c-forward-syntactic-ws indent-point))
8107 (c-add-syntax 'defun-open (c-point 'boi))
8108 ;; Bogus to use bol here, but it's the legacy. (Resolved,
8109 ;; 2007-11-09)
8110 ))))
8111
8112 ;; CASE 5B: After a function header but before the body (or
8113 ;; the ending semicolon if there's no body).
8114 ((save-excursion
8115 (when (setq placeholder (c-just-after-func-arglist-p lim))
8116 (setq tmp-pos (point))))
8117 (cond
8118
8119 ;; CASE 5B.1: Member init list.
8120 ((eq (char-after tmp-pos) ?:)
8121 (if (or (> tmp-pos indent-point)
8122 (= (c-point 'bosws) (1+ tmp-pos)))
8123 (progn
8124 ;; There is no preceding member init clause.
8125 ;; Indent relative to the beginning of indentation
8126 ;; for the topmost-intro line that contains the
8127 ;; prototype's open paren.
8128 (goto-char placeholder)
8129 (c-add-syntax 'member-init-intro (c-point 'boi)))
8130 ;; Indent relative to the first member init clause.
8131 (goto-char (1+ tmp-pos))
8132 (c-forward-syntactic-ws)
8133 (c-add-syntax 'member-init-cont (point))))
8134
8135 ;; CASE 5B.2: K&R arg decl intro
8136 ((and c-recognize-knr-p
8137 (c-in-knr-argdecl lim))
8138 (c-beginning-of-statement-1 lim)
8139 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
8140 (if containing-decl-open
8141 (c-add-class-syntax 'inclass
8142 containing-decl-open
8143 containing-decl-start
8144 containing-decl-kwd
8145 paren-state)))
8146
8147 ;; CASE 5B.4: Nether region after a C++ or Java func
8148 ;; decl, which could include a `throws' declaration.
8149 (t
8150 (c-beginning-of-statement-1 lim)
8151 (c-add-syntax 'func-decl-cont (c-point 'boi))
8152 )))
8153
8154 ;; CASE 5C: inheritance line. could be first inheritance
8155 ;; line, or continuation of a multiple inheritance
8156 ((or (and (c-major-mode-is 'c++-mode)
8157 (progn
8158 (when (eq char-after-ip ?,)
8159 (skip-chars-forward " \t")
8160 (forward-char))
8161 (looking-at c-opt-postfix-decl-spec-key)))
8162 (and (or (eq char-before-ip ?:)
8163 ;; watch out for scope operator
8164 (save-excursion
8165 (and (eq char-after-ip ?:)
8166 (c-safe (forward-char 1) t)
8167 (not (eq (char-after) ?:))
8168 )))
8169 (save-excursion
8170 (c-backward-syntactic-ws lim)
8171 (if (eq char-before-ip ?:)
8172 (progn
8173 (forward-char -1)
8174 (c-backward-syntactic-ws lim)))
8175 (back-to-indentation)
8176 (looking-at c-class-key)))
8177 ;; for Java
8178 (and (c-major-mode-is 'java-mode)
8179 (let ((fence (save-excursion
8180 (c-beginning-of-statement-1 lim)
8181 (point)))
8182 cont done)
8183 (save-excursion
8184 (while (not done)
8185 (cond ((looking-at c-opt-postfix-decl-spec-key)
8186 (setq injava-inher (cons cont (point))
8187 done t))
8188 ((or (not (c-safe (c-forward-sexp -1) t))
8189 (<= (point) fence))
8190 (setq done t))
8191 )
8192 (setq cont t)))
8193 injava-inher)
8194 (not (c-crosses-statement-barrier-p (cdr injava-inher)
8195 (point)))
8196 ))
8197 (cond
8198
8199 ;; CASE 5C.1: non-hanging colon on an inher intro
8200 ((eq char-after-ip ?:)
8201 (c-beginning-of-statement-1 lim)
8202 (c-add-syntax 'inher-intro (c-point 'boi))
8203 ;; don't add inclass symbol since relative point already
8204 ;; contains any class offset
8205 )
8206
8207 ;; CASE 5C.2: hanging colon on an inher intro
8208 ((eq char-before-ip ?:)
8209 (c-beginning-of-statement-1 lim)
8210 (c-add-syntax 'inher-intro (c-point 'boi))
8211 (if containing-decl-open
8212 (c-add-class-syntax 'inclass
8213 containing-decl-open
8214 containing-decl-start
8215 containing-decl-kwd
8216 paren-state)))
8217
8218 ;; CASE 5C.3: in a Java implements/extends
8219 (injava-inher
8220 (let ((where (cdr injava-inher))
8221 (cont (car injava-inher)))
8222 (goto-char where)
8223 (cond ((looking-at "throws\\>[^_]")
8224 (c-add-syntax 'func-decl-cont
8225 (progn (c-beginning-of-statement-1 lim)
8226 (c-point 'boi))))
8227 (cont (c-add-syntax 'inher-cont where))
8228 (t (c-add-syntax 'inher-intro
8229 (progn (goto-char (cdr injava-inher))
8230 (c-beginning-of-statement-1 lim)
8231 (point))))
8232 )))
8233
8234 ;; CASE 5C.4: a continued inheritance line
8235 (t
8236 (c-beginning-of-inheritance-list lim)
8237 (c-add-syntax 'inher-cont (point))
8238 ;; don't add inclass symbol since relative point already
8239 ;; contains any class offset
8240 )))
8241
8242 ;; CASE 5D: this could be a top-level initialization, a
8243 ;; member init list continuation, or a template argument
8244 ;; list continuation.
8245 ((save-excursion
8246 ;; Note: We use the fact that lim is always after any
8247 ;; preceding brace sexp.
8248 (if c-recognize-<>-arglists
8249 (while (and
8250 (progn
8251 (c-syntactic-skip-backward "^;,=<>" lim t)
8252 (> (point) lim))
8253 (or
8254 (when c-overloadable-operators-regexp
8255 (when (setq placeholder (c-after-special-operator-id lim))
8256 (goto-char placeholder)
8257 t))
8258 (cond
8259 ((eq (char-before) ?>)
8260 (or (c-backward-<>-arglist nil lim)
8261 (backward-char))
8262 t)
8263 ((eq (char-before) ?<)
8264 (backward-char)
8265 (if (save-excursion
8266 (c-forward-<>-arglist nil))
8267 (progn (forward-char)
8268 nil)
8269 t))
8270 (t nil)))))
8271 ;; NB: No c-after-special-operator-id stuff in this
8272 ;; clause - we assume only C++ needs it.
8273 (c-syntactic-skip-backward "^;,=" lim t))
8274 (memq (char-before) '(?, ?= ?<)))
8275 (cond
8276
8277 ;; CASE 5D.3: perhaps a template list continuation?
8278 ((and (c-major-mode-is 'c++-mode)
8279 (save-excursion
8280 (save-restriction
8281 (c-with-syntax-table c++-template-syntax-table
8282 (goto-char indent-point)
8283 (setq placeholder (c-up-list-backward))
8284 (and placeholder
8285 (eq (char-after placeholder) ?<))))))
8286 (c-with-syntax-table c++-template-syntax-table
8287 (goto-char placeholder)
8288 (c-beginning-of-statement-1 lim t)
8289 (if (save-excursion
8290 (c-backward-syntactic-ws lim)
8291 (eq (char-before) ?<))
8292 ;; In a nested template arglist.
8293 (progn
8294 (goto-char placeholder)
8295 (c-syntactic-skip-backward "^,;" lim t)
8296 (c-forward-syntactic-ws))
8297 (back-to-indentation)))
8298 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8299 ;; template aware.
8300 (c-add-syntax 'template-args-cont (point) placeholder))
8301
8302 ;; CASE 5D.4: perhaps a multiple inheritance line?
8303 ((and (c-major-mode-is 'c++-mode)
8304 (save-excursion
8305 (c-beginning-of-statement-1 lim)
8306 (setq placeholder (point))
8307 (if (looking-at "static\\>[^_]")
8308 (c-forward-token-2 1 nil indent-point))
8309 (and (looking-at c-class-key)
8310 (zerop (c-forward-token-2 2 nil indent-point))
8311 (if (eq (char-after) ?<)
8312 (c-with-syntax-table c++-template-syntax-table
8313 (zerop (c-forward-token-2 1 t indent-point)))
8314 t)
8315 (eq (char-after) ?:))))
8316 (goto-char placeholder)
8317 (c-add-syntax 'inher-cont (c-point 'boi)))
8318
8319 ;; CASE 5D.5: Continuation of the "expression part" of a
8320 ;; top level construct. Or, perhaps, an unrecognised construct.
8321 (t
8322 (while (and (setq placeholder (point))
8323 (eq (car (c-beginning-of-decl-1 containing-sexp))
8324 'same)
8325 (save-excursion
8326 (c-backward-syntactic-ws)
8327 (eq (char-before) ?}))
8328 (< (point) placeholder)))
8329 (c-add-stmt-syntax
8330 (cond
8331 ((eq (point) placeholder) 'statement) ; unrecognised construct
8332 ;; A preceding comma at the top level means that a
8333 ;; new variable declaration starts here. Use
8334 ;; topmost-intro-cont for it, for consistency with
8335 ;; the first variable declaration. C.f. case 5N.
8336 ((eq char-before-ip ?,) 'topmost-intro-cont)
8337 (t 'statement-cont))
8338 nil nil containing-sexp paren-state))
8339 ))
8340
8341 ;; CASE 5F: Close of a non-class declaration level block.
8342 ((and (eq char-after-ip ?})
8343 (c-keyword-member containing-decl-kwd
8344 'c-other-block-decl-kwds))
8345 ;; This is inconsistent: Should use `containing-decl-open'
8346 ;; here if it's at boi, like in case 5J.
8347 (goto-char containing-decl-start)
8348 (c-add-stmt-syntax
8349 (if (string-equal (symbol-name containing-decl-kwd) "extern")
8350 ;; Special case for compatibility with the
8351 ;; extern-lang syntactic symbols.
8352 'extern-lang-close
8353 (intern (concat (symbol-name containing-decl-kwd)
8354 "-close")))
8355 nil t
8356 (c-most-enclosing-brace paren-state (point))
8357 paren-state))
8358
8359 ;; CASE 5G: we are looking at the brace which closes the
8360 ;; enclosing nested class decl
8361 ((and containing-sexp
8362 (eq char-after-ip ?})
8363 (eq containing-decl-open containing-sexp))
8364 (c-add-class-syntax 'class-close
8365 containing-decl-open
8366 containing-decl-start
8367 containing-decl-kwd
8368 paren-state))
8369
8370 ;; CASE 5H: we could be looking at subsequent knr-argdecls
8371 ((and c-recognize-knr-p
8372 (not containing-sexp) ; can't be knr inside braces.
8373 (not (eq char-before-ip ?}))
8374 (save-excursion
8375 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
8376 (and placeholder
8377 ;; Do an extra check to avoid tripping up on
8378 ;; statements that occur in invalid contexts
8379 ;; (e.g. in macro bodies where we don't really
8380 ;; know the context of what we're looking at).
8381 (not (and c-opt-block-stmt-key
8382 (looking-at c-opt-block-stmt-key)))))
8383 (< placeholder indent-point))
8384 (goto-char placeholder)
8385 (c-add-syntax 'knr-argdecl (point)))
8386
8387 ;; CASE 5I: ObjC method definition.
8388 ((and c-opt-method-key
8389 (looking-at c-opt-method-key))
8390 (c-beginning-of-statement-1 nil t)
8391 (if (= (point) indent-point)
8392 ;; Handle the case when it's the first (non-comment)
8393 ;; thing in the buffer. Can't look for a 'same return
8394 ;; value from cbos1 since ObjC directives currently
8395 ;; aren't recognized fully, so that we get 'same
8396 ;; instead of 'previous if it moved over a preceding
8397 ;; directive.
8398 (goto-char (point-min)))
8399 (c-add-syntax 'objc-method-intro (c-point 'boi)))
8400
8401 ;; CASE 5P: AWK pattern or function or continuation
8402 ;; thereof.
8403 ((c-major-mode-is 'awk-mode)
8404 (setq placeholder (point))
8405 (c-add-stmt-syntax
8406 (if (and (eq (c-beginning-of-statement-1) 'same)
8407 (/= (point) placeholder))
8408 'topmost-intro-cont
8409 'topmost-intro)
8410 nil nil
8411 containing-sexp paren-state))
8412
8413 ;; CASE 5N: At a variable declaration that follows a class
8414 ;; definition or some other block declaration that doesn't
8415 ;; end at the closing '}'. C.f. case 5D.5.
8416 ((progn
8417 (c-backward-syntactic-ws lim)
8418 (and (eq (char-before) ?})
8419 (save-excursion
8420 (let ((start (point)))
8421 (if (and c-state-cache
8422 (consp (car c-state-cache))
8423 (eq (cdar c-state-cache) (point)))
8424 ;; Speed up the backward search a bit.
8425 (goto-char (caar c-state-cache)))
8426 (c-beginning-of-decl-1 containing-sexp)
8427 (setq placeholder (point))
8428 (if (= start (point))
8429 ;; The '}' is unbalanced.
8430 nil
8431 (c-end-of-decl-1)
8432 (>= (point) indent-point))))))
8433 (goto-char placeholder)
8434 (c-add-stmt-syntax 'topmost-intro-cont nil nil
8435 containing-sexp paren-state))
8436
8437 ;; NOTE: The point is at the end of the previous token here.
8438
8439 ;; CASE 5J: we are at the topmost level, make
8440 ;; sure we skip back past any access specifiers
8441 ((and
8442 ;; A macro continuation line is never at top level.
8443 (not (and macro-start
8444 (> indent-point macro-start)))
8445 (save-excursion
8446 (setq placeholder (point))
8447 (or (memq char-before-ip '(?\; ?{ ?} nil))
8448 (c-at-vsemi-p before-ws-ip)
8449 (when (and (eq char-before-ip ?:)
8450 (eq (c-beginning-of-statement-1 lim)
8451 'label))
8452 (c-backward-syntactic-ws lim)
8453 (setq placeholder (point)))
8454 (and (c-major-mode-is 'objc-mode)
8455 (catch 'not-in-directive
8456 (c-beginning-of-statement-1 lim)
8457 (setq placeholder (point))
8458 (while (and (c-forward-objc-directive)
8459 (< (point) indent-point))
8460 (c-forward-syntactic-ws)
8461 (if (>= (point) indent-point)
8462 (throw 'not-in-directive t))
8463 (setq placeholder (point)))
8464 nil)))))
8465 ;; For historic reasons we anchor at bol of the last
8466 ;; line of the previous declaration. That's clearly
8467 ;; highly bogus and useless, and it makes our lives hard
8468 ;; to remain compatible. :P
8469 (goto-char placeholder)
8470 (c-add-syntax 'topmost-intro (c-point 'bol))
8471 (if containing-decl-open
8472 (if (c-keyword-member containing-decl-kwd
8473 'c-other-block-decl-kwds)
8474 (progn
8475 (goto-char (c-brace-anchor-point containing-decl-open))
8476 (c-add-stmt-syntax
8477 (if (string-equal (symbol-name containing-decl-kwd)
8478 "extern")
8479 ;; Special case for compatibility with the
8480 ;; extern-lang syntactic symbols.
8481 'inextern-lang
8482 (intern (concat "in"
8483 (symbol-name containing-decl-kwd))))
8484 nil t
8485 (c-most-enclosing-brace paren-state (point))
8486 paren-state))
8487 (c-add-class-syntax 'inclass
8488 containing-decl-open
8489 containing-decl-start
8490 containing-decl-kwd
8491 paren-state)))
8492 (when (and c-syntactic-indentation-in-macros
8493 macro-start
8494 (/= macro-start (c-point 'boi indent-point)))
8495 (c-add-syntax 'cpp-define-intro)
8496 (setq macro-start nil)))
8497
8498 ;; CASE 5K: we are at an ObjC method definition
8499 ;; continuation line.
8500 ((and c-opt-method-key
8501 (save-excursion
8502 (c-beginning-of-statement-1 lim)
8503 (beginning-of-line)
8504 (when (looking-at c-opt-method-key)
8505 (setq placeholder (point)))))
8506 (c-add-syntax 'objc-method-args-cont placeholder))
8507
8508 ;; CASE 5L: we are at the first argument of a template
8509 ;; arglist that begins on the previous line.
8510 ((and c-recognize-<>-arglists
8511 (eq (char-before) ?<)
8512 (setq placeholder (1- (point)))
8513 (not (and c-overloadable-operators-regexp
8514 (c-after-special-operator-id lim))))
8515 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8516 (c-add-syntax 'template-args-cont (c-point 'boi) placeholder))
8517
8518 ;; CASE 5Q: we are at a statement within a macro.
8519 (macro-start
8520 (c-beginning-of-statement-1 containing-sexp)
8521 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
8522
8523 ;; CASE 5M: we are at a topmost continuation line
8524 (t
8525 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8526 (when (c-major-mode-is 'objc-mode)
8527 (setq placeholder (point))
8528 (while (and (c-forward-objc-directive)
8529 (< (point) indent-point))
8530 (c-forward-syntactic-ws)
8531 (setq placeholder (point)))
8532 (goto-char placeholder))
8533 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8534 ))
8535
8536 ;; (CASE 6 has been removed.)
8537
8538 ;; CASE 19: line is an expression, not a statement, and is directly
8539 ;; contained by a template delimiter. Most likely, we are in a
8540 ;; template arglist within a statement. This case is based on CASE
8541 ;; 7. At some point in the future, we may wish to create more
8542 ;; syntactic symbols such as `template-intro',
8543 ;; `template-cont-nonempty', etc., and distinguish between them as we
8544 ;; do for `arglist-intro' etc. (2009-12-07).
8545 ((and c-recognize-<>-arglists
8546 (setq containing-< (c-up-list-backward indent-point containing-sexp))
8547 (eq (char-after containing-<) ?\<))
8548 (setq placeholder (c-point 'boi containing-<))
8549 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
8550 ; '<') before indent-point.
8551 (if (>= (point) placeholder)
8552 (progn
8553 (forward-char)
8554 (skip-chars-forward " \t"))
8555 (goto-char placeholder))
8556 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
8557 (c-most-enclosing-brace c-state-cache (point))
8558 paren-state))
8559
8560
8561 ;; CASE 7: line is an expression, not a statement. Most
8562 ;; likely we are either in a function prototype or a function
8563 ;; call argument list, or a template argument list.
8564 ((not (or (and c-special-brace-lists
8565 (save-excursion
8566 (goto-char containing-sexp)
8567 (c-looking-at-special-brace-list)))
8568 (eq (char-after containing-sexp) ?{)
8569 (eq (char-after containing-sexp) ?<)))
8570 (cond
8571
8572 ;; CASE 7A: we are looking at the arglist closing paren.
8573 ;; C.f. case 7F.
8574 ((memq char-after-ip '(?\) ?\]))
8575 (goto-char containing-sexp)
8576 (setq placeholder (c-point 'boi))
8577 (if (and (c-safe (backward-up-list 1) t)
8578 (>= (point) placeholder))
8579 (progn
8580 (forward-char)
8581 (skip-chars-forward " \t"))
8582 (goto-char placeholder))
8583 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
8584 (c-most-enclosing-brace paren-state (point))
8585 paren-state))
8586
8587 ;; CASE 7B: Looking at the opening brace of an
8588 ;; in-expression block or brace list. C.f. cases 4, 16A
8589 ;; and 17E.
8590 ((and (eq char-after-ip ?{)
8591 (progn
8592 (setq placeholder (c-inside-bracelist-p (point)
8593 paren-state))
8594 (if placeholder
8595 (setq tmpsymbol '(brace-list-open . inexpr-class))
8596 (setq tmpsymbol '(block-open . inexpr-statement)
8597 placeholder
8598 (cdr-safe (c-looking-at-inexpr-block
8599 (c-safe-position containing-sexp
8600 paren-state)
8601 containing-sexp)))
8602 ;; placeholder is nil if it's a block directly in
8603 ;; a function arglist. That makes us skip out of
8604 ;; this case.
8605 )))
8606 (goto-char placeholder)
8607 (back-to-indentation)
8608 (c-add-stmt-syntax (car tmpsymbol) nil t
8609 (c-most-enclosing-brace paren-state (point))
8610 paren-state)
8611 (if (/= (point) placeholder)
8612 (c-add-syntax (cdr tmpsymbol))))
8613
8614 ;; CASE 7C: we are looking at the first argument in an empty
8615 ;; argument list. Use arglist-close if we're actually
8616 ;; looking at a close paren or bracket.
8617 ((memq char-before-ip '(?\( ?\[))
8618 (goto-char containing-sexp)
8619 (setq placeholder (c-point 'boi))
8620 (if (and (c-safe (backward-up-list 1) t)
8621 (>= (point) placeholder))
8622 (progn
8623 (forward-char)
8624 (skip-chars-forward " \t"))
8625 (goto-char placeholder))
8626 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
8627 (c-most-enclosing-brace paren-state (point))
8628 paren-state))
8629
8630 ;; CASE 7D: we are inside a conditional test clause. treat
8631 ;; these things as statements
8632 ((progn
8633 (goto-char containing-sexp)
8634 (and (c-safe (c-forward-sexp -1) t)
8635 (looking-at "\\<for\\>[^_]")))
8636 (goto-char (1+ containing-sexp))
8637 (c-forward-syntactic-ws indent-point)
8638 (if (eq char-before-ip ?\;)
8639 (c-add-syntax 'statement (point))
8640 (c-add-syntax 'statement-cont (point))
8641 ))
8642
8643 ;; CASE 7E: maybe a continued ObjC method call. This is the
8644 ;; case when we are inside a [] bracketed exp, and what
8645 ;; precede the opening bracket is not an identifier.
8646 ((and c-opt-method-key
8647 (eq (char-after containing-sexp) ?\[)
8648 (progn
8649 (goto-char (1- containing-sexp))
8650 (c-backward-syntactic-ws (c-point 'bod))
8651 (if (not (looking-at c-symbol-key))
8652 (c-add-syntax 'objc-method-call-cont containing-sexp))
8653 )))
8654
8655 ;; CASE 7F: we are looking at an arglist continuation line,
8656 ;; but the preceding argument is on the same line as the
8657 ;; opening paren. This case includes multi-line
8658 ;; mathematical paren groupings, but we could be on a
8659 ;; for-list continuation line. C.f. case 7A.
8660 ((progn
8661 (goto-char (1+ containing-sexp))
8662 (< (save-excursion
8663 (c-forward-syntactic-ws)
8664 (point))
8665 (c-point 'bonl)))
8666 (goto-char containing-sexp) ; paren opening the arglist
8667 (setq placeholder (c-point 'boi))
8668 (if (and (c-safe (backward-up-list 1) t)
8669 (>= (point) placeholder))
8670 (progn
8671 (forward-char)
8672 (skip-chars-forward " \t"))
8673 (goto-char placeholder))
8674 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
8675 (c-most-enclosing-brace c-state-cache (point))
8676 paren-state))
8677
8678 ;; CASE 7G: we are looking at just a normal arglist
8679 ;; continuation line
8680 (t (c-forward-syntactic-ws indent-point)
8681 (c-add-syntax 'arglist-cont (c-point 'boi)))
8682 ))
8683
8684 ;; CASE 8: func-local multi-inheritance line
8685 ((and (c-major-mode-is 'c++-mode)
8686 (save-excursion
8687 (goto-char indent-point)
8688 (skip-chars-forward " \t")
8689 (looking-at c-opt-postfix-decl-spec-key)))
8690 (goto-char indent-point)
8691 (skip-chars-forward " \t")
8692 (cond
8693
8694 ;; CASE 8A: non-hanging colon on an inher intro
8695 ((eq char-after-ip ?:)
8696 (c-backward-syntactic-ws lim)
8697 (c-add-syntax 'inher-intro (c-point 'boi)))
8698
8699 ;; CASE 8B: hanging colon on an inher intro
8700 ((eq char-before-ip ?:)
8701 (c-add-syntax 'inher-intro (c-point 'boi)))
8702
8703 ;; CASE 8C: a continued inheritance line
8704 (t
8705 (c-beginning-of-inheritance-list lim)
8706 (c-add-syntax 'inher-cont (point))
8707 )))
8708
8709 ;; CASE 9: we are inside a brace-list
8710 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
8711 (setq special-brace-list
8712 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
8713 (save-excursion
8714 (goto-char containing-sexp)
8715 (c-looking-at-special-brace-list)))
8716 (c-inside-bracelist-p containing-sexp paren-state))))
8717 (cond
8718
8719 ;; CASE 9A: In the middle of a special brace list opener.
8720 ((and (consp special-brace-list)
8721 (save-excursion
8722 (goto-char containing-sexp)
8723 (eq (char-after) ?\())
8724 (eq char-after-ip (car (cdr special-brace-list))))
8725 (goto-char (car (car special-brace-list)))
8726 (skip-chars-backward " \t")
8727 (if (and (bolp)
8728 (assoc 'statement-cont
8729 (setq placeholder (c-guess-basic-syntax))))
8730 (setq c-syntactic-context placeholder)
8731 (c-beginning-of-statement-1
8732 (c-safe-position (1- containing-sexp) paren-state))
8733 (c-forward-token-2 0)
8734 (while (looking-at c-specifier-key)
8735 (goto-char (match-end 1))
8736 (c-forward-syntactic-ws))
8737 (c-add-syntax 'brace-list-open (c-point 'boi))))
8738
8739 ;; CASE 9B: brace-list-close brace
8740 ((if (consp special-brace-list)
8741 ;; Check special brace list closer.
8742 (progn
8743 (goto-char (car (car special-brace-list)))
8744 (save-excursion
8745 (goto-char indent-point)
8746 (back-to-indentation)
8747 (or
8748 ;; We were between the special close char and the `)'.
8749 (and (eq (char-after) ?\))
8750 (eq (1+ (point)) (cdr (car special-brace-list))))
8751 ;; We were before the special close char.
8752 (and (eq (char-after) (cdr (cdr special-brace-list)))
8753 (zerop (c-forward-token-2))
8754 (eq (1+ (point)) (cdr (car special-brace-list)))))))
8755 ;; Normal brace list check.
8756 (and (eq char-after-ip ?})
8757 (c-safe (goto-char (c-up-list-backward (point))) t)
8758 (= (point) containing-sexp)))
8759 (if (eq (point) (c-point 'boi))
8760 (c-add-syntax 'brace-list-close (point))
8761 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8762 (c-beginning-of-statement-1 lim)
8763 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
8764
8765 (t
8766 ;; Prepare for the rest of the cases below by going to the
8767 ;; token following the opening brace
8768 (if (consp special-brace-list)
8769 (progn
8770 (goto-char (car (car special-brace-list)))
8771 (c-forward-token-2 1 nil indent-point))
8772 (goto-char containing-sexp))
8773 (forward-char)
8774 (let ((start (point)))
8775 (c-forward-syntactic-ws indent-point)
8776 (goto-char (max start (c-point 'bol))))
8777 (c-skip-ws-forward indent-point)
8778 (cond
8779
8780 ;; CASE 9C: we're looking at the first line in a brace-list
8781 ((= (point) indent-point)
8782 (if (consp special-brace-list)
8783 (goto-char (car (car special-brace-list)))
8784 (goto-char containing-sexp))
8785 (if (eq (point) (c-point 'boi))
8786 (c-add-syntax 'brace-list-intro (point))
8787 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8788 (c-beginning-of-statement-1 lim)
8789 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
8790
8791 ;; CASE 9D: this is just a later brace-list-entry or
8792 ;; brace-entry-open
8793 (t (if (or (eq char-after-ip ?{)
8794 (and c-special-brace-lists
8795 (save-excursion
8796 (goto-char indent-point)
8797 (c-forward-syntactic-ws (c-point 'eol))
8798 (c-looking-at-special-brace-list (point)))))
8799 (c-add-syntax 'brace-entry-open (point))
8800 (c-add-syntax 'brace-list-entry (point))
8801 ))
8802 ))))
8803
8804 ;; CASE 10: A continued statement or top level construct.
8805 ((and (not (memq char-before-ip '(?\; ?:)))
8806 (not (c-at-vsemi-p before-ws-ip))
8807 (or (not (eq char-before-ip ?}))
8808 (c-looking-at-inexpr-block-backward c-state-cache))
8809 (> (point)
8810 (save-excursion
8811 (c-beginning-of-statement-1 containing-sexp)
8812 (setq placeholder (point))))
8813 (/= placeholder containing-sexp))
8814 ;; This is shared with case 18.
8815 (c-guess-continued-construct indent-point
8816 char-after-ip
8817 placeholder
8818 containing-sexp
8819 paren-state))
8820
8821 ;; CASE 16: block close brace, possibly closing the defun or
8822 ;; the class
8823 ((eq char-after-ip ?})
8824 ;; From here on we have the next containing sexp in lim.
8825 (setq lim (c-most-enclosing-brace paren-state))
8826 (goto-char containing-sexp)
8827 (cond
8828
8829 ;; CASE 16E: Closing a statement block? This catches
8830 ;; cases where it's preceded by a statement keyword,
8831 ;; which works even when used in an "invalid" context,
8832 ;; e.g. a macro argument.
8833 ((c-after-conditional)
8834 (c-backward-to-block-anchor lim)
8835 (c-add-stmt-syntax 'block-close nil t lim paren-state))
8836
8837 ;; CASE 16A: closing a lambda defun or an in-expression
8838 ;; block? C.f. cases 4, 7B and 17E.
8839 ((setq placeholder (c-looking-at-inexpr-block
8840 (c-safe-position containing-sexp paren-state)
8841 nil))
8842 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8843 'inline-close
8844 'block-close))
8845 (goto-char containing-sexp)
8846 (back-to-indentation)
8847 (if (= containing-sexp (point))
8848 (c-add-syntax tmpsymbol (point))
8849 (goto-char (cdr placeholder))
8850 (back-to-indentation)
8851 (c-add-stmt-syntax tmpsymbol nil t
8852 (c-most-enclosing-brace paren-state (point))
8853 paren-state)
8854 (if (/= (point) (cdr placeholder))
8855 (c-add-syntax (car placeholder)))))
8856
8857 ;; CASE 16B: does this close an inline or a function in
8858 ;; a non-class declaration level block?
8859 ((save-excursion
8860 (and lim
8861 (progn
8862 (goto-char lim)
8863 (c-looking-at-decl-block
8864 (c-most-enclosing-brace paren-state lim)
8865 nil))
8866 (setq placeholder (point))))
8867 (c-backward-to-decl-anchor lim)
8868 (back-to-indentation)
8869 (if (save-excursion
8870 (goto-char placeholder)
8871 (looking-at c-other-decl-block-key))
8872 (c-add-syntax 'defun-close (point))
8873 (c-add-syntax 'inline-close (point))))
8874
8875 ;; CASE 16F: Can be a defun-close of a function declared
8876 ;; in a statement block, e.g. in Pike or when using gcc
8877 ;; extensions, but watch out for macros followed by
8878 ;; blocks. Let it through to be handled below.
8879 ;; C.f. cases B.3 and 17G.
8880 ((save-excursion
8881 (and (not (c-at-statement-start-p))
8882 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8883 (setq placeholder (point))
8884 (let ((c-recognize-typeless-decls nil))
8885 ;; Turn off recognition of constructs that
8886 ;; lacks a type in this case, since that's more
8887 ;; likely to be a macro followed by a block.
8888 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8889 (back-to-indentation)
8890 (if (/= (point) containing-sexp)
8891 (goto-char placeholder))
8892 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
8893
8894 ;; CASE 16C: If there is an enclosing brace then this is
8895 ;; a block close since defun closes inside declaration
8896 ;; level blocks have been handled above.
8897 (lim
8898 ;; If the block is preceded by a case/switch label on
8899 ;; the same line, we anchor at the first preceding label
8900 ;; at boi. The default handling in c-add-stmt-syntax
8901 ;; really fixes it better, but we do like this to keep
8902 ;; the indentation compatible with version 5.28 and
8903 ;; earlier. C.f. case 17H.
8904 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8905 (eq (c-beginning-of-statement-1 lim) 'label)))
8906 (goto-char placeholder)
8907 (if (looking-at c-label-kwds-regexp)
8908 (c-add-syntax 'block-close (point))
8909 (goto-char containing-sexp)
8910 ;; c-backward-to-block-anchor not necessary here; those
8911 ;; situations are handled in case 16E above.
8912 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
8913
8914 ;; CASE 16D: Only top level defun close left.
8915 (t
8916 (goto-char containing-sexp)
8917 (c-backward-to-decl-anchor lim)
8918 (c-add-stmt-syntax 'defun-close nil nil
8919 (c-most-enclosing-brace paren-state)
8920 paren-state))
8921 ))
8922
8923 ;; CASE 17: Statement or defun catchall.
8924 (t
8925 (goto-char indent-point)
8926 ;; Back up statements until we find one that starts at boi.
8927 (while (let* ((prev-point (point))
8928 (last-step-type (c-beginning-of-statement-1
8929 containing-sexp)))
8930 (if (= (point) prev-point)
8931 (progn
8932 (setq step-type (or step-type last-step-type))
8933 nil)
8934 (setq step-type last-step-type)
8935 (/= (point) (c-point 'boi)))))
8936 (cond
8937
8938 ;; CASE 17B: continued statement
8939 ((and (eq step-type 'same)
8940 (/= (point) indent-point))
8941 (c-add-stmt-syntax 'statement-cont nil nil
8942 containing-sexp paren-state))
8943
8944 ;; CASE 17A: After a case/default label?
8945 ((progn
8946 (while (and (eq step-type 'label)
8947 (not (looking-at c-label-kwds-regexp)))
8948 (setq step-type
8949 (c-beginning-of-statement-1 containing-sexp)))
8950 (eq step-type 'label))
8951 (c-add-stmt-syntax (if (eq char-after-ip ?{)
8952 'statement-case-open
8953 'statement-case-intro)
8954 nil t containing-sexp paren-state))
8955
8956 ;; CASE 17D: any old statement
8957 ((progn
8958 (while (eq step-type 'label)
8959 (setq step-type
8960 (c-beginning-of-statement-1 containing-sexp)))
8961 (eq step-type 'previous))
8962 (c-add-stmt-syntax 'statement nil t
8963 containing-sexp paren-state)
8964 (if (eq char-after-ip ?{)
8965 (c-add-syntax 'block-open)))
8966
8967 ;; CASE 17I: Inside a substatement block.
8968 ((progn
8969 ;; The following tests are all based on containing-sexp.
8970 (goto-char containing-sexp)
8971 ;; From here on we have the next containing sexp in lim.
8972 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
8973 (c-after-conditional))
8974 (c-backward-to-block-anchor lim)
8975 (c-add-stmt-syntax 'statement-block-intro nil t
8976 lim paren-state)
8977 (if (eq char-after-ip ?{)
8978 (c-add-syntax 'block-open)))
8979
8980 ;; CASE 17E: first statement in an in-expression block.
8981 ;; C.f. cases 4, 7B and 16A.
8982 ((setq placeholder (c-looking-at-inexpr-block
8983 (c-safe-position containing-sexp paren-state)
8984 nil))
8985 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8986 'defun-block-intro
8987 'statement-block-intro))
8988 (back-to-indentation)
8989 (if (= containing-sexp (point))
8990 (c-add-syntax tmpsymbol (point))
8991 (goto-char (cdr placeholder))
8992 (back-to-indentation)
8993 (c-add-stmt-syntax tmpsymbol nil t
8994 (c-most-enclosing-brace c-state-cache (point))
8995 paren-state)
8996 (if (/= (point) (cdr placeholder))
8997 (c-add-syntax (car placeholder))))
8998 (if (eq char-after-ip ?{)
8999 (c-add-syntax 'block-open)))
9000
9001 ;; CASE 17F: first statement in an inline, or first
9002 ;; statement in a top-level defun. we can tell this is it
9003 ;; if there are no enclosing braces that haven't been
9004 ;; narrowed out by a class (i.e. don't use bod here).
9005 ((save-excursion
9006 (or (not (setq placeholder (c-most-enclosing-brace
9007 paren-state)))
9008 (and (progn
9009 (goto-char placeholder)
9010 (eq (char-after) ?{))
9011 (c-looking-at-decl-block (c-most-enclosing-brace
9012 paren-state (point))
9013 nil))))
9014 (c-backward-to-decl-anchor lim)
9015 (back-to-indentation)
9016 (c-add-syntax 'defun-block-intro (point)))
9017
9018 ;; CASE 17G: First statement in a function declared inside
9019 ;; a normal block. This can occur in Pike and with
9020 ;; e.g. the gcc extensions, but watch out for macros
9021 ;; followed by blocks. C.f. cases B.3 and 16F.
9022 ((save-excursion
9023 (and (not (c-at-statement-start-p))
9024 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
9025 (setq placeholder (point))
9026 (let ((c-recognize-typeless-decls nil))
9027 ;; Turn off recognition of constructs that lacks
9028 ;; a type in this case, since that's more likely
9029 ;; to be a macro followed by a block.
9030 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
9031 (back-to-indentation)
9032 (if (/= (point) containing-sexp)
9033 (goto-char placeholder))
9034 (c-add-stmt-syntax 'defun-block-intro nil t
9035 lim paren-state))
9036
9037 ;; CASE 17H: First statement in a block.
9038 (t
9039 ;; If the block is preceded by a case/switch label on the
9040 ;; same line, we anchor at the first preceding label at
9041 ;; boi. The default handling in c-add-stmt-syntax is
9042 ;; really fixes it better, but we do like this to keep the
9043 ;; indentation compatible with version 5.28 and earlier.
9044 ;; C.f. case 16C.
9045 (while (and (/= (setq placeholder (point)) (c-point 'boi))
9046 (eq (c-beginning-of-statement-1 lim) 'label)))
9047 (goto-char placeholder)
9048 (if (looking-at c-label-kwds-regexp)
9049 (c-add-syntax 'statement-block-intro (point))
9050 (goto-char containing-sexp)
9051 ;; c-backward-to-block-anchor not necessary here; those
9052 ;; situations are handled in case 17I above.
9053 (c-add-stmt-syntax 'statement-block-intro nil t
9054 lim paren-state))
9055 (if (eq char-after-ip ?{)
9056 (c-add-syntax 'block-open)))
9057 ))
9058 )
9059
9060 ;; now we need to look at any modifiers
9061 (goto-char indent-point)
9062 (skip-chars-forward " \t")
9063
9064 ;; are we looking at a comment only line?
9065 (when (and (looking-at c-comment-start-regexp)
9066 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
9067 (c-append-syntax 'comment-intro))
9068
9069 ;; we might want to give additional offset to friends (in C++).
9070 (when (and c-opt-friend-key
9071 (looking-at c-opt-friend-key))
9072 (c-append-syntax 'friend))
9073
9074 ;; Set syntactic-relpos.
9075 (let ((p c-syntactic-context))
9076 (while (and p
9077 (if (integerp (c-langelem-pos (car p)))
9078 (progn
9079 (setq syntactic-relpos (c-langelem-pos (car p)))
9080 nil)
9081 t))
9082 (setq p (cdr p))))
9083
9084 ;; Start of or a continuation of a preprocessor directive?
9085 (if (and macro-start
9086 (eq macro-start (c-point 'boi))
9087 (not (and (c-major-mode-is 'pike-mode)
9088 (eq (char-after (1+ macro-start)) ?\"))))
9089 (c-append-syntax 'cpp-macro)
9090 (when (and c-syntactic-indentation-in-macros macro-start)
9091 (if in-macro-expr
9092 (when (or
9093 (< syntactic-relpos macro-start)
9094 (not (or
9095 (assq 'arglist-intro c-syntactic-context)
9096 (assq 'arglist-cont c-syntactic-context)
9097 (assq 'arglist-cont-nonempty c-syntactic-context)
9098 (assq 'arglist-close c-syntactic-context))))
9099 ;; If inside a cpp expression, i.e. anywhere in a
9100 ;; cpp directive except a #define body, we only let
9101 ;; through the syntactic analysis that is internal
9102 ;; in the expression. That means the arglist
9103 ;; elements, if they are anchored inside the cpp
9104 ;; expression.
9105 (setq c-syntactic-context nil)
9106 (c-add-syntax 'cpp-macro-cont macro-start))
9107 (when (and (eq macro-start syntactic-relpos)
9108 (not (assq 'cpp-define-intro c-syntactic-context))
9109 (save-excursion
9110 (goto-char macro-start)
9111 (or (not (c-forward-to-cpp-define-body))
9112 (<= (point) (c-point 'boi indent-point)))))
9113 ;; Inside a #define body and the syntactic analysis is
9114 ;; anchored on the start of the #define. In this case
9115 ;; we add cpp-define-intro to get the extra
9116 ;; indentation of the #define body.
9117 (c-add-syntax 'cpp-define-intro)))))
9118
9119 ;; return the syntax
9120 c-syntactic-context)))
9121
9122 \f
9123 ;; Indentation calculation.
9124
9125 (defun c-evaluate-offset (offset langelem symbol)
9126 ;; offset can be a number, a function, a variable, a list, or one of
9127 ;; the symbols + or -
9128 ;;
9129 ;; This function might do hidden buffer changes.
9130 (let ((res
9131 (cond
9132 ((numberp offset) offset)
9133 ((vectorp offset) offset)
9134 ((null offset) nil)
9135
9136 ((eq offset '+) c-basic-offset)
9137 ((eq offset '-) (- c-basic-offset))
9138 ((eq offset '++) (* 2 c-basic-offset))
9139 ((eq offset '--) (* 2 (- c-basic-offset)))
9140 ((eq offset '*) (/ c-basic-offset 2))
9141 ((eq offset '/) (/ (- c-basic-offset) 2))
9142
9143 ((functionp offset)
9144 (c-evaluate-offset
9145 (funcall offset
9146 (cons (c-langelem-sym langelem)
9147 (c-langelem-pos langelem)))
9148 langelem symbol))
9149
9150 ((listp offset)
9151 (cond
9152 ((eq (car offset) 'quote)
9153 (c-benign-error "The offset %S for %s was mistakenly quoted"
9154 offset symbol)
9155 nil)
9156
9157 ((memq (car offset) '(min max))
9158 (let (res val (method (car offset)))
9159 (setq offset (cdr offset))
9160 (while offset
9161 (setq val (c-evaluate-offset (car offset) langelem symbol))
9162 (cond
9163 ((not val))
9164 ((not res)
9165 (setq res val))
9166 ((integerp val)
9167 (if (vectorp res)
9168 (c-benign-error "\
9169 Error evaluating offset %S for %s: \
9170 Cannot combine absolute offset %S with relative %S in `%s' method"
9171 (car offset) symbol res val method)
9172 (setq res (funcall method res val))))
9173 (t
9174 (if (integerp res)
9175 (c-benign-error "\
9176 Error evaluating offset %S for %s: \
9177 Cannot combine relative offset %S with absolute %S in `%s' method"
9178 (car offset) symbol res val method)
9179 (setq res (vector (funcall method (aref res 0)
9180 (aref val 0)))))))
9181 (setq offset (cdr offset)))
9182 res))
9183
9184 ((eq (car offset) 'add)
9185 (let (res val)
9186 (setq offset (cdr offset))
9187 (while offset
9188 (setq val (c-evaluate-offset (car offset) langelem symbol))
9189 (cond
9190 ((not val))
9191 ((not res)
9192 (setq res val))
9193 ((integerp val)
9194 (if (vectorp res)
9195 (setq res (vector (+ (aref res 0) val)))
9196 (setq res (+ res val))))
9197 (t
9198 (if (vectorp res)
9199 (c-benign-error "\
9200 Error evaluating offset %S for %s: \
9201 Cannot combine absolute offsets %S and %S in `add' method"
9202 (car offset) symbol res val)
9203 (setq res val)))) ; Override.
9204 (setq offset (cdr offset)))
9205 res))
9206
9207 (t
9208 (let (res)
9209 (when (eq (car offset) 'first)
9210 (setq offset (cdr offset)))
9211 (while (and (not res) offset)
9212 (setq res (c-evaluate-offset (car offset) langelem symbol)
9213 offset (cdr offset)))
9214 res))))
9215
9216 ((and (symbolp offset) (boundp offset))
9217 (symbol-value offset))
9218
9219 (t
9220 (c-benign-error "Unknown offset format %S for %s" offset symbol)
9221 nil))))
9222
9223 (if (or (null res) (integerp res)
9224 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
9225 res
9226 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
9227 offset symbol res)
9228 nil)))
9229
9230 (defun c-calc-offset (langelem)
9231 ;; Get offset from LANGELEM which is a list beginning with the
9232 ;; syntactic symbol and followed by any analysis data it provides.
9233 ;; That data may be zero or more elements, but if at least one is
9234 ;; given then the first is the anchor position (or nil). The symbol
9235 ;; is matched against `c-offsets-alist' and the offset calculated
9236 ;; from that is returned.
9237 ;;
9238 ;; This function might do hidden buffer changes.
9239 (let* ((symbol (c-langelem-sym langelem))
9240 (match (assq symbol c-offsets-alist))
9241 (offset (cdr-safe match)))
9242 (if match
9243 (setq offset (c-evaluate-offset offset langelem symbol))
9244 (if c-strict-syntax-p
9245 (c-benign-error "No offset found for syntactic symbol %s" symbol))
9246 (setq offset 0))
9247 (if (vectorp offset)
9248 offset
9249 (or (and (numberp offset) offset)
9250 (and (symbolp offset) (symbol-value offset))
9251 0))
9252 ))
9253
9254 (defun c-get-offset (langelem)
9255 ;; This is a compatibility wrapper for `c-calc-offset' in case
9256 ;; someone is calling it directly. It takes an old style syntactic
9257 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
9258 ;; new list form.
9259 ;;
9260 ;; This function might do hidden buffer changes.
9261 (if (c-langelem-pos langelem)
9262 (c-calc-offset (list (c-langelem-sym langelem)
9263 (c-langelem-pos langelem)))
9264 (c-calc-offset langelem)))
9265
9266 (defun c-get-syntactic-indentation (langelems)
9267 ;; Calculate the syntactic indentation from a syntactic description
9268 ;; as returned by `c-guess-syntax'.
9269 ;;
9270 ;; Note that topmost-intro always has an anchor position at bol, for
9271 ;; historical reasons. It's often used together with other symbols
9272 ;; that has more sane positions. Since we always use the first
9273 ;; found anchor position, we rely on that these other symbols always
9274 ;; precede topmost-intro in the LANGELEMS list.
9275 ;;
9276 ;; This function might do hidden buffer changes.
9277 (let ((indent 0) anchor)
9278
9279 (while langelems
9280 (let* ((c-syntactic-element (car langelems))
9281 (res (c-calc-offset c-syntactic-element)))
9282
9283 (if (vectorp res)
9284 ;; Got an absolute column that overrides any indentation
9285 ;; we've collected so far, but not the relative
9286 ;; indentation we might get for the nested structures
9287 ;; further down the langelems list.
9288 (setq indent (elt res 0)
9289 anchor (point-min)) ; A position at column 0.
9290
9291 ;; Got a relative change of the current calculated
9292 ;; indentation.
9293 (setq indent (+ indent res))
9294
9295 ;; Use the anchor position from the first syntactic
9296 ;; element with one.
9297 (unless anchor
9298 (setq anchor (c-langelem-pos (car langelems)))))
9299
9300 (setq langelems (cdr langelems))))
9301
9302 (if anchor
9303 (+ indent (save-excursion
9304 (goto-char anchor)
9305 (current-column)))
9306 indent)))
9307
9308 \f
9309 (cc-provide 'cc-engine)
9310
9311 ;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
9312 ;;; cc-engine.el ends here