]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-engine.el
(c-in-knr-argdecl): Limit number of paren/bracket pairs parsed, to solve
[gnu-emacs] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
6
7 ;; Authors: 2001- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs and Stewart Clamen
11 ;; 1985 Richard M. Stallman
12 ;; Maintainer: bug-cc-mode@gnu.org
13 ;; Created: 22-Apr-1997 (split from cc-mode.el)
14 ;; Version: See cc-mode.el
15 ;; Keywords: c languages oop
16
17 ;; This file is part of GNU Emacs.
18
19 ;; GNU Emacs is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation; either version 3, or (at your option)
22 ;; any later version.
23
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
28
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with this program; see the file COPYING. If not, write to
31 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 ;; Boston, MA 02110-1301, USA.
33
34 ;;; Commentary:
35
36 ;; The functions which have docstring documentation can be considered
37 ;; part of an API which other packages can use in CC Mode buffers.
38 ;; Otoh, undocumented functions and functions with the documentation
39 ;; in comments are considered purely internal and can change semantics
40 ;; or even disappear in the future.
41 ;;
42 ;; (This policy applies to CC Mode as a whole, not just this file. It
43 ;; probably also applies to many other Emacs packages, but here it's
44 ;; clearly spelled out.)
45
46 ;; Hidden buffer changes
47 ;;
48 ;; Various functions in CC Mode use text properties for caching and
49 ;; syntactic markup purposes, and those of them that might modify such
50 ;; properties but still don't modify the buffer in a visible way are
51 ;; said to do "hidden buffer changes". They should be used within
52 ;; `c-save-buffer-state' or a similar function that saves and restores
53 ;; buffer modifiedness, disables buffer change hooks, etc.
54 ;;
55 ;; Interactive functions are assumed to not do hidden buffer changes,
56 ;; except in the specific parts of them that do real changes.
57 ;;
58 ;; Lineup functions are assumed to do hidden buffer changes. They
59 ;; must not do real changes, though.
60 ;;
61 ;; All other functions that do hidden buffer changes have that noted
62 ;; in their doc string or comment.
63 ;;
64 ;; The intention with this system is to avoid wrapping every leaf
65 ;; function that do hidden buffer changes inside
66 ;; `c-save-buffer-state'. It should be used as near the top of the
67 ;; interactive functions as possible.
68 ;;
69 ;; Functions called during font locking are allowed to do hidden
70 ;; buffer changes since the font-lock package run them in a context
71 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
72 ;; inspired by `save-buffer-state' in the font-lock package).
73
74 ;; Use of text properties
75 ;;
76 ;; CC Mode uses several text properties internally to mark up various
77 ;; positions, e.g. to improve speed and to eliminate glitches in
78 ;; interactive refontification.
79 ;;
80 ;; Note: This doc is for internal use only. Other packages should not
81 ;; assume that these text properties are used as described here.
82 ;;
83 ;; 'syntax-table
84 ;; Used to modify the syntax of some characters. It is used to
85 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
86 ;; to "hide" obtrusive characters in preprocessor lines.
87 ;;
88 ;; This property is used on single characters and is therefore
89 ;; always treated as front and rear nonsticky (or start and end open
90 ;; in XEmacs vocabulary). It's therefore installed on
91 ;; `text-property-default-nonsticky' if that variable exists (Emacs
92 ;; >= 21).
93 ;;
94 ;; 'c-is-sws and 'c-in-sws
95 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
96 ;; speed them up. See the comment blurb before `c-put-is-sws'
97 ;; below for further details.
98 ;;
99 ;; 'c-type
100 ;; This property is used on single characters to mark positions with
101 ;; special syntactic relevance of various sorts. Its primary use is
102 ;; to avoid glitches when multiline constructs are refontified
103 ;; interactively (on font lock decoration level 3). It's cleared in
104 ;; a region before it's fontified and is then put on relevant chars
105 ;; in that region as they are encountered during the fontification.
106 ;; The value specifies the kind of position:
107 ;;
108 ;; 'c-decl-arg-start
109 ;; Put on the last char of the token preceding each declaration
110 ;; inside a declaration style arglist (typically in a function
111 ;; prototype).
112 ;;
113 ;; 'c-decl-end
114 ;; Put on the last char of the token preceding a declaration.
115 ;; This is used in cases where declaration boundaries can't be
116 ;; recognized simply by looking for a token like ";" or "}".
117 ;; `c-type-decl-end-used' must be set if this is used (see also
118 ;; `c-find-decl-spots').
119 ;;
120 ;; 'c-<>-arg-sep
121 ;; Put on the commas that separate arguments in angle bracket
122 ;; arglists like C++ template arglists.
123 ;;
124 ;; 'c-decl-id-start and 'c-decl-type-start
125 ;; Put on the last char of the token preceding each declarator
126 ;; in the declarator list of a declaration. They are also used
127 ;; between the identifiers cases like enum declarations.
128 ;; 'c-decl-type-start is used when the declarators are types,
129 ;; 'c-decl-id-start otherwise.
130 ;;
131 ;; 'c-awk-NL-prop
132 ;; Used in AWK mode to mark the various kinds of newlines. See
133 ;; cc-awk.el.
134
135 ;;; Code:
136
137 (eval-when-compile
138 (let ((load-path
139 (if (and (boundp 'byte-compile-dest-file)
140 (stringp byte-compile-dest-file))
141 (cons (file-name-directory byte-compile-dest-file) load-path)
142 load-path)))
143 (load "cc-bytecomp" nil t)))
144
145 (cc-require 'cc-defs)
146 (cc-require-when-compile 'cc-langs)
147 (cc-require 'cc-vars)
148
149 ;; Silence the compiler.
150 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
151
152 \f
153 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
154
155 (defmacro c-declare-lang-variables ()
156 `(progn
157 ,@(apply 'nconc
158 (mapcar (lambda (init)
159 `(,(if (elt init 2)
160 `(defvar ,(car init) nil ,(elt init 2))
161 `(defvar ,(car init) nil))
162 (make-variable-buffer-local ',(car init))))
163 (cdr c-lang-variable-inits)))))
164 (c-declare-lang-variables)
165
166 \f
167 ;;; Internal state variables.
168
169 ;; Internal state of hungry delete key feature
170 (defvar c-hungry-delete-key nil)
171 (make-variable-buffer-local 'c-hungry-delete-key)
172
173 ;; The electric flag (toggled by `c-toggle-electric-state').
174 ;; If t, electric actions (like automatic reindentation, and (if
175 ;; c-auto-newline is also set) auto newlining) will happen when an electric
176 ;; key like `{' is pressed (or an electric keyword like `else').
177 (defvar c-electric-flag t)
178 (make-variable-buffer-local 'c-electric-flag)
179
180 ;; Internal state of auto newline feature.
181 (defvar c-auto-newline nil)
182 (make-variable-buffer-local 'c-auto-newline)
183
184 ;; Included in the mode line to indicate the active submodes.
185 ;; (defvar c-submode-indicators nil)
186 ;; (make-variable-buffer-local 'c-submode-indicators)
187
188 (defun c-calculate-state (arg prevstate)
189 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
190 ;; arg is nil or zero, toggle the state. If arg is negative, turn
191 ;; the state off, and if arg is positive, turn the state on
192 (if (or (not arg)
193 (zerop (setq arg (prefix-numeric-value arg))))
194 (not prevstate)
195 (> arg 0)))
196
197 ;; Dynamically bound cache for `c-in-literal'.
198 (defvar c-in-literal-cache t)
199
200 \f
201 ;; Basic handling of preprocessor directives.
202
203 ;; This is a dynamically bound cache used together with
204 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
205 ;; works as long as point doesn't cross a macro boundary.
206 (defvar c-macro-start 'unknown)
207
208 (defsubst c-query-and-set-macro-start ()
209 (if (symbolp c-macro-start)
210 (setq c-macro-start (save-excursion
211 (c-save-buffer-state ()
212 (and (c-beginning-of-macro)
213 (point)))))
214 c-macro-start))
215
216 (defsubst c-query-macro-start ()
217 (if (symbolp c-macro-start)
218 (save-excursion
219 (c-save-buffer-state ()
220 (and (c-beginning-of-macro)
221 (point))))
222 c-macro-start))
223
224 (defun c-beginning-of-macro (&optional lim)
225 "Go to the beginning of a preprocessor directive.
226 Leave point at the beginning of the directive and return t if in one,
227 otherwise return nil and leave point unchanged.
228
229 Note that this function might do hidden buffer changes. See the
230 comment at the start of cc-engine.el for more info."
231 (when c-opt-cpp-prefix
232 (let ((here (point)))
233 (save-restriction
234 (if lim (narrow-to-region lim (point-max)))
235 (beginning-of-line)
236 (while (eq (char-before (1- (point))) ?\\)
237 (forward-line -1))
238 (back-to-indentation)
239 (if (and (<= (point) here)
240 (looking-at c-opt-cpp-start))
241 t
242 (goto-char here)
243 nil)))))
244
245 (defun c-end-of-macro ()
246 "Go to the end of a preprocessor directive.
247 More accurately, move the point to the end of the closest following
248 line that doesn't end with a line continuation backslash - no check is
249 done that the point is inside a cpp directive to begin with.
250
251 Note that this function might do hidden buffer changes. See the
252 comment at the start of cc-engine.el for more info."
253 (while (progn
254 (end-of-line)
255 (when (and (eq (char-before) ?\\)
256 (not (eobp)))
257 (forward-char)
258 t))))
259
260 (defun c-forward-over-cpp-define-id ()
261 ;; Assuming point is at the "#" that introduces a preprocessor
262 ;; directive, it's moved forward to the end of the identifier which is
263 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
264 ;; is returned in this case, in all other cases nil is returned and
265 ;; point isn't moved.
266 ;;
267 ;; This function might do hidden buffer changes.
268 (when (and c-opt-cpp-macro-define-id
269 (looking-at c-opt-cpp-macro-define-id))
270 (goto-char (match-end 0))))
271
272 (defun c-forward-to-cpp-define-body ()
273 ;; Assuming point is at the "#" that introduces a preprocessor
274 ;; directive, it's moved forward to the start of the definition body
275 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
276 ;; specifies). Non-nil is returned in this case, in all other cases
277 ;; nil is returned and point isn't moved.
278 ;;
279 ;; This function might do hidden buffer changes.
280 (when (and c-opt-cpp-macro-define-start
281 (looking-at c-opt-cpp-macro-define-start)
282 (not (= (match-end 0) (c-point 'eol))))
283 (goto-char (match-end 0))))
284
285 \f
286 ;;; Basic utility functions.
287
288 (defun c-syntactic-content (from to paren-level)
289 ;; Return the given region as a string where all syntactic
290 ;; whitespace is removed or, where necessary, replaced with a single
291 ;; space. If PAREN-LEVEL is given then all parens in the region are
292 ;; collapsed to "()", "[]" etc.
293 ;;
294 ;; This function might do hidden buffer changes.
295
296 (save-excursion
297 (save-restriction
298 (narrow-to-region from to)
299 (goto-char from)
300 (let* ((parts (list nil)) (tail parts) pos in-paren)
301
302 (while (re-search-forward c-syntactic-ws-start to t)
303 (goto-char (setq pos (match-beginning 0)))
304 (c-forward-syntactic-ws)
305 (if (= (point) pos)
306 (forward-char)
307
308 (when paren-level
309 (save-excursion
310 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
311 pos (point))))
312
313 (if (and (> pos from)
314 (< (point) to)
315 (looking-at "\\w\\|\\s_")
316 (save-excursion
317 (goto-char (1- pos))
318 (looking-at "\\w\\|\\s_")))
319 (progn
320 (setcdr tail (list (buffer-substring-no-properties from pos)
321 " "))
322 (setq tail (cddr tail)))
323 (setcdr tail (list (buffer-substring-no-properties from pos)))
324 (setq tail (cdr tail)))
325
326 (when in-paren
327 (when (= (car (parse-partial-sexp pos to -1)) -1)
328 (setcdr tail (list (buffer-substring-no-properties
329 (1- (point)) (point))))
330 (setq tail (cdr tail))))
331
332 (setq from (point))))
333
334 (setcdr tail (list (buffer-substring-no-properties from to)))
335 (apply 'concat (cdr parts))))))
336
337 (defun c-shift-line-indentation (shift-amt)
338 ;; Shift the indentation of the current line with the specified
339 ;; amount (positive inwards). The buffer is modified only if
340 ;; SHIFT-AMT isn't equal to zero.
341 (let ((pos (- (point-max) (point)))
342 (c-macro-start c-macro-start)
343 tmp-char-inserted)
344 (if (zerop shift-amt)
345 nil
346 ;; If we're on an empty line inside a macro, we take the point
347 ;; to be at the current indentation and shift it to the
348 ;; appropriate column. This way we don't treat the extra
349 ;; whitespace out to the line continuation as indentation.
350 (when (and (c-query-and-set-macro-start)
351 (looking-at "[ \t]*\\\\$")
352 (save-excursion
353 (skip-chars-backward " \t")
354 (bolp)))
355 (insert ?x)
356 (backward-char)
357 (setq tmp-char-inserted t))
358 (unwind-protect
359 (let ((col (current-indentation)))
360 (delete-region (c-point 'bol) (c-point 'boi))
361 (beginning-of-line)
362 (indent-to (+ col shift-amt)))
363 (when tmp-char-inserted
364 (delete-char 1))))
365 ;; If initial point was within line's indentation and we're not on
366 ;; a line with a line continuation in a macro, position after the
367 ;; indentation. Else stay at same point in text.
368 (if (and (< (point) (c-point 'boi))
369 (not tmp-char-inserted))
370 (back-to-indentation)
371 (if (> (- (point-max) pos) (point))
372 (goto-char (- (point-max) pos))))))
373
374 (defsubst c-keyword-sym (keyword)
375 ;; Return non-nil if the string KEYWORD is a known keyword. More
376 ;; precisely, the value is the symbol for the keyword in
377 ;; `c-keywords-obarray'.
378 (intern-soft keyword c-keywords-obarray))
379
380 (defsubst c-keyword-member (keyword-sym lang-constant)
381 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
382 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
383 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
384 ;; nil then the result is nil.
385 (get keyword-sym lang-constant))
386
387 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
388 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
389 "\"|"
390 "\""))
391
392 ;; Regexp matching string limit syntax.
393 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
394 "\\s\"\\|\\s|"
395 "\\s\""))
396
397 ;; Regexp matching WS followed by string limit syntax.
398 (defconst c-ws*-string-limit-regexp
399 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
400
401 ;; Holds formatted error strings for the few cases where parse errors
402 ;; are reported.
403 (defvar c-parsing-error nil)
404 (make-variable-buffer-local 'c-parsing-error)
405
406 (defun c-echo-parsing-error (&optional quiet)
407 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
408 (c-benign-error "%s" c-parsing-error))
409 c-parsing-error)
410
411 ;; Faces given to comments and string literals. This is used in some
412 ;; situations to speed up recognition; it isn't mandatory that font
413 ;; locking is in use. This variable is extended with the face in
414 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
415 (defvar c-literal-faces
416 (append '(font-lock-comment-face font-lock-string-face)
417 (when (facep 'font-lock-comment-delimiter-face)
418 ;; New in Emacs 22.
419 '(font-lock-comment-delimiter-face))))
420
421 (defsubst c-put-c-type-property (pos value)
422 ;; Put a c-type property with the given value at POS.
423 (c-put-char-property pos 'c-type value))
424
425 (defun c-clear-c-type-property (from to value)
426 ;; Remove all occurences of the c-type property that has the given
427 ;; value in the region between FROM and TO. VALUE is assumed to not
428 ;; be nil.
429 ;;
430 ;; Note: This assumes that c-type is put on single chars only; it's
431 ;; very inefficient if matching properties cover large regions.
432 (save-excursion
433 (goto-char from)
434 (while (progn
435 (when (eq (get-text-property (point) 'c-type) value)
436 (c-clear-char-property (point) 'c-type))
437 (goto-char (next-single-property-change (point) 'c-type nil to))
438 (< (point) to)))))
439
440 \f
441 ;; Some debug tools to visualize various special positions. This
442 ;; debug code isn't as portable as the rest of CC Mode.
443
444 (cc-bytecomp-defun overlays-in)
445 (cc-bytecomp-defun overlay-get)
446 (cc-bytecomp-defun overlay-start)
447 (cc-bytecomp-defun overlay-end)
448 (cc-bytecomp-defun delete-overlay)
449 (cc-bytecomp-defun overlay-put)
450 (cc-bytecomp-defun make-overlay)
451
452 (defun c-debug-add-face (beg end face)
453 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
454 (while overlays
455 (setq overlay (car overlays)
456 overlays (cdr overlays))
457 (when (eq (overlay-get overlay 'face) face)
458 (setq beg (min beg (overlay-start overlay))
459 end (max end (overlay-end overlay)))
460 (delete-overlay overlay)))
461 (overlay-put (make-overlay beg end) 'face face)))
462
463 (defun c-debug-remove-face (beg end face)
464 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
465 (ol-beg beg) (ol-end end))
466 (while overlays
467 (setq overlay (car overlays)
468 overlays (cdr overlays))
469 (when (eq (overlay-get overlay 'face) face)
470 (setq ol-beg (min ol-beg (overlay-start overlay))
471 ol-end (max ol-end (overlay-end overlay)))
472 (delete-overlay overlay)))
473 (when (< ol-beg beg)
474 (overlay-put (make-overlay ol-beg beg) 'face face))
475 (when (> ol-end end)
476 (overlay-put (make-overlay end ol-end) 'face face))))
477
478 \f
479 ;; `c-beginning-of-statement-1' and accompanying stuff.
480
481 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
482 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
483 ;; better way should be implemented, but this will at least shut up
484 ;; the byte compiler.
485 (defvar c-maybe-labelp)
486
487 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
488
489 ;; Macros used internally in c-beginning-of-statement-1 for the
490 ;; automaton actions.
491 (defmacro c-bos-push-state ()
492 '(setq stack (cons (cons state saved-pos)
493 stack)))
494 (defmacro c-bos-pop-state (&optional do-if-done)
495 `(if (setq state (car (car stack))
496 saved-pos (cdr (car stack))
497 stack (cdr stack))
498 t
499 ,do-if-done
500 (throw 'loop nil)))
501 (defmacro c-bos-pop-state-and-retry ()
502 '(throw 'loop (setq state (car (car stack))
503 saved-pos (cdr (car stack))
504 ;; Throw nil if stack is empty, else throw non-nil.
505 stack (cdr stack))))
506 (defmacro c-bos-save-pos ()
507 '(setq saved-pos (vector pos tok ptok pptok)))
508 (defmacro c-bos-restore-pos ()
509 '(unless (eq (elt saved-pos 0) start)
510 (setq pos (elt saved-pos 0)
511 tok (elt saved-pos 1)
512 ptok (elt saved-pos 2)
513 pptok (elt saved-pos 3))
514 (goto-char pos)
515 (setq sym nil)))
516 (defmacro c-bos-save-error-info (missing got)
517 `(setq saved-pos (vector pos ,missing ,got)))
518 (defmacro c-bos-report-error ()
519 '(unless noerror
520 (setq c-parsing-error
521 (format "No matching `%s' found for `%s' on line %d"
522 (elt saved-pos 1)
523 (elt saved-pos 2)
524 (1+ (count-lines (point-min)
525 (c-point 'bol (elt saved-pos 0))))))))
526
527 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
528 noerror comma-delim)
529 "Move to the start of the current statement or declaration, or to
530 the previous one if already at the beginning of one. Only
531 statements/declarations on the same level are considered, i.e. don't
532 move into or out of sexps (not even normal expression parentheses).
533
534 Stop at statement continuation tokens like \"else\", \"catch\",
535 \"finally\" and the \"while\" in \"do ... while\" if the start point
536 is within the continuation. If starting at such a token, move to the
537 corresponding statement start. If at the beginning of a statement,
538 move to the closest containing statement if there is any. This might
539 also stop at a continuation clause.
540
541 Labels are treated as part of the following statements if
542 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
543 statement start keyword.) Otherwise, each label is treated as a
544 separate statement.
545
546 Macros are ignored \(i.e. skipped over) unless point is within one, in
547 which case the content of the macro is treated as normal code. Aside
548 from any normal statement starts found in it, stop at the first token
549 of the content in the macro, i.e. the expression of an \"#if\" or the
550 start of the definition in a \"#define\". Also stop at start of
551 macros before leaving them.
552
553 Return 'label if stopped at a label, 'same if stopped at the beginning
554 of the current statement, 'up if stepped to a containing statement,
555 'previous if stepped to a preceding statement, 'beginning if stepped
556 from a statement continuation clause to its start clause, or 'macro if
557 stepped to a macro start. Note that 'same and not 'label is returned
558 if stopped at the same label without crossing the colon character.
559
560 LIM may be given to limit the search. If the search hits the limit,
561 point will be left at the closest following token, or at the start
562 position if that is less ('same is returned in this case).
563
564 NOERROR turns off error logging to `c-parsing-error'.
565
566 Normally only ';' and virtual semicolons are considered to delimit
567 statements, but if COMMA-DELIM is non-nil then ',' is treated
568 as a delimiter too.
569
570 Note that this function might do hidden buffer changes. See the
571 comment at the start of cc-engine.el for more info."
572
573 ;; The bulk of this function is a pushdown automaton that looks at statement
574 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
575 ;; purpose is to keep track of nested statements, ensuring that such
576 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
577 ;; does with nested braces/brackets/parentheses).
578 ;;
579 ;; Note: The position of a boundary is the following token.
580 ;;
581 ;; Beginning with the current token (the one following point), move back one
582 ;; sexp at a time (where a sexp is, more or less, either a token or the
583 ;; entire contents of a brace/bracket/paren pair). Each time a statement
584 ;; boundary is crossed or a "while"-like token is found, update the state of
585 ;; the PDA. Stop at the beginning of a statement when the stack (holding
586 ;; nested statement info) is empty and the position has been moved.
587 ;;
588 ;; The following variables constitute the PDA:
589 ;;
590 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
591 ;; scanned back over, 'boundary if we've just gone back over a
592 ;; statement boundary, or nil otherwise.
593 ;; state: takes one of the values (nil else else-boundary while
594 ;; while-boundary catch catch-boundary).
595 ;; nil means "no "while"-like token yet scanned".
596 ;; 'else, for example, means "just gone back over an else".
597 ;; 'else-boundary means "just gone back over a statement boundary
598 ;; immediately after having gone back over an else".
599 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
600 ;; of error reporting information.
601 ;; stack: The stack onto which the PDA pushes its state. Each entry
602 ;; consists of a saved value of state and saved-pos. An entry is
603 ;; pushed when we move back over a "continuation" token (e.g. else)
604 ;; and popped when we encounter the corresponding opening token
605 ;; (e.g. if).
606 ;;
607 ;;
608 ;; The following diagram briefly outlines the PDA.
609 ;;
610 ;; Common state:
611 ;; "else": Push state, goto state `else'.
612 ;; "while": Push state, goto state `while'.
613 ;; "catch" or "finally": Push state, goto state `catch'.
614 ;; boundary: Pop state.
615 ;; other: Do nothing special.
616 ;;
617 ;; State `else':
618 ;; boundary: Goto state `else-boundary'.
619 ;; other: Error, pop state, retry token.
620 ;;
621 ;; State `else-boundary':
622 ;; "if": Pop state.
623 ;; boundary: Error, pop state.
624 ;; other: See common state.
625 ;;
626 ;; State `while':
627 ;; boundary: Save position, goto state `while-boundary'.
628 ;; other: Pop state, retry token.
629 ;;
630 ;; State `while-boundary':
631 ;; "do": Pop state.
632 ;; boundary: Restore position if it's not at start, pop state. [*see below]
633 ;; other: See common state.
634 ;;
635 ;; State `catch':
636 ;; boundary: Goto state `catch-boundary'.
637 ;; other: Error, pop state, retry token.
638 ;;
639 ;; State `catch-boundary':
640 ;; "try": Pop state.
641 ;; "catch": Goto state `catch'.
642 ;; boundary: Error, pop state.
643 ;; other: See common state.
644 ;;
645 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
646 ;; searching for a "do" which would have opened a do-while. If we didn't
647 ;; find it, we discard the analysis done since the "while", go back to this
648 ;; token in the buffer and restart the scanning there, this time WITHOUT
649 ;; pushing the 'while state onto the stack.
650 ;;
651 ;; In addition to the above there is some special handling of labels
652 ;; and macros.
653
654 (let ((case-fold-search nil)
655 (start (point))
656 macro-start
657 (delims (if comma-delim '(?\; ?,) '(?\;)))
658 (c-stmt-delim-chars (if comma-delim
659 c-stmt-delim-chars-with-comma
660 c-stmt-delim-chars))
661 c-in-literal-cache c-maybe-labelp saved
662 ;; Current position.
663 pos
664 ;; Position of last stmt boundary character (e.g. ;).
665 boundary-pos
666 ;; The position of the last sexp or bound that follows the
667 ;; first found colon, i.e. the start of the nonlabel part of
668 ;; the statement. It's `start' if a colon is found just after
669 ;; the start.
670 after-labels-pos
671 ;; Like `after-labels-pos', but the first such position inside
672 ;; a label, i.e. the start of the last label before the start
673 ;; of the nonlabel part of the statement.
674 last-label-pos
675 ;; The last position where a label is possible provided the
676 ;; statement started there. It's nil as long as no invalid
677 ;; label content has been found (according to
678 ;; `c-nonlabel-token-key'. It's `start' if no valid label
679 ;; content was found in the label. Note that we might still
680 ;; regard it a label if it starts with `c-label-kwds'.
681 label-good-pos
682 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
683 ;; See above.
684 sym
685 ;; Current state in the automaton. See above.
686 state
687 ;; Current saved positions. See above.
688 saved-pos
689 ;; Stack of conses (state . saved-pos).
690 stack
691 ;; Regexp which matches "for", "if", etc.
692 (cond-key (or c-opt-block-stmt-key
693 "\\<\\>")) ; Matches nothing.
694 ;; Return value.
695 (ret 'same)
696 ;; Positions of the last three sexps or bounds we've stopped at.
697 tok ptok pptok)
698
699 (save-restriction
700 (if lim (narrow-to-region lim (point-max)))
701
702 (if (save-excursion
703 (and (c-beginning-of-macro)
704 (/= (point) start)))
705 (setq macro-start (point)))
706
707 ;; Try to skip back over unary operator characters, to register
708 ;; that we've moved.
709 (while (progn
710 (setq pos (point))
711 (c-backward-syntactic-ws)
712 ;; Protect post-++/-- operators just before a virtual semicolon.
713 (and (not (c-at-vsemi-p))
714 (/= (skip-chars-backward "-+!*&~@`#") 0))))
715
716 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
717 ;; done. Later on we ignore the boundaries for statements that don't
718 ;; contain any sexp. The only thing that is affected is that the error
719 ;; checking is a little less strict, and we really don't bother.
720 (if (and (memq (char-before) delims)
721 (progn (forward-char -1)
722 (setq saved (point))
723 (c-backward-syntactic-ws)
724 (or (memq (char-before) delims)
725 (memq (char-before) '(?: nil))
726 (eq (char-syntax (char-before)) ?\()
727 (c-at-vsemi-p))))
728 (setq ret 'previous
729 pos saved)
730
731 ;; Begin at start and not pos to detect macros if we stand
732 ;; directly after the #.
733 (goto-char start)
734 (if (looking-at "\\<\\|\\W")
735 ;; Record this as the first token if not starting inside it.
736 (setq tok start))
737
738 ;; The following while loop goes back one sexp (balanced parens,
739 ;; etc. with contents, or symbol or suchlike) each iteration. This
740 ;; movement is accomplished with a call to scan-sexps approx 130 lines
741 ;; below.
742 (while
743 (catch 'loop ;; Throw nil to break, non-nil to continue.
744 (cond
745 ((save-excursion
746 (and macro-start ; Always NIL for AWK.
747 (progn (skip-chars-backward " \t")
748 (eq (char-before) ?#))
749 (progn (setq saved (1- (point)))
750 (beginning-of-line)
751 (not (eq (char-before (1- (point))) ?\\)))
752 (looking-at c-opt-cpp-start)
753 (progn (skip-chars-forward " \t")
754 (eq (point) saved))))
755 (goto-char saved)
756 (if (and (c-forward-to-cpp-define-body)
757 (progn (c-forward-syntactic-ws start)
758 (< (point) start)))
759 ;; Stop at the first token in the content of the macro.
760 (setq pos (point)
761 ignore-labels t) ; Avoid the label check on exit.
762 (setq pos saved
763 ret 'macro
764 ignore-labels t))
765 (throw 'loop nil))
766
767 ;; Do a round through the automaton if we've just passed a
768 ;; statement boundary or passed a "while"-like token.
769 ((or sym
770 (and (looking-at cond-key)
771 (setq sym (intern (match-string 1)))))
772
773 (when (and (< pos start) (null stack))
774 (throw 'loop nil))
775
776 ;; The PDA state handling.
777 ;;
778 ;; Refer to the description of the PDA in the opening
779 ;; comments. In the following OR form, the first leaf
780 ;; attempts to handles one of the specific actions detailed
781 ;; (e.g., finding token "if" whilst in state `else-boundary').
782 ;; We drop through to the second leaf (which handles common
783 ;; state) if no specific handler is found in the first cond.
784 ;; If a parsing error is detected (e.g. an "else" with no
785 ;; preceding "if"), we throw to the enclosing catch.
786 ;;
787 ;; Note that the (eq state 'else) means
788 ;; "we've just passed an else", NOT "we're looking for an
789 ;; else".
790 (or (cond
791 ((eq state 'else)
792 (if (eq sym 'boundary)
793 (setq state 'else-boundary)
794 (c-bos-report-error)
795 (c-bos-pop-state-and-retry)))
796
797 ((eq state 'else-boundary)
798 (cond ((eq sym 'if)
799 (c-bos-pop-state (setq ret 'beginning)))
800 ((eq sym 'boundary)
801 (c-bos-report-error)
802 (c-bos-pop-state))))
803
804 ((eq state 'while)
805 (if (and (eq sym 'boundary)
806 ;; Since this can cause backtracking we do a
807 ;; little more careful analysis to avoid it:
808 ;; If there's a label in front of the while
809 ;; it can't be part of a do-while.
810 (not after-labels-pos))
811 (progn (c-bos-save-pos)
812 (setq state 'while-boundary))
813 (c-bos-pop-state-and-retry))) ; Can't be a do-while
814
815 ((eq state 'while-boundary)
816 (cond ((eq sym 'do)
817 (c-bos-pop-state (setq ret 'beginning)))
818 ((eq sym 'boundary) ; isn't a do-while
819 (c-bos-restore-pos) ; the position of the while
820 (c-bos-pop-state)))) ; no longer searching for do.
821
822 ((eq state 'catch)
823 (if (eq sym 'boundary)
824 (setq state 'catch-boundary)
825 (c-bos-report-error)
826 (c-bos-pop-state-and-retry)))
827
828 ((eq state 'catch-boundary)
829 (cond
830 ((eq sym 'try)
831 (c-bos-pop-state (setq ret 'beginning)))
832 ((eq sym 'catch)
833 (setq state 'catch))
834 ((eq sym 'boundary)
835 (c-bos-report-error)
836 (c-bos-pop-state)))))
837
838 ;; This is state common. We get here when the previous
839 ;; cond statement found no particular state handler.
840 (cond ((eq sym 'boundary)
841 ;; If we have a boundary at the start
842 ;; position we push a frame to go to the
843 ;; previous statement.
844 (if (>= pos start)
845 (c-bos-push-state)
846 (c-bos-pop-state)))
847 ((eq sym 'else)
848 (c-bos-push-state)
849 (c-bos-save-error-info 'if 'else)
850 (setq state 'else))
851 ((eq sym 'while)
852 ;; Is this a real while, or a do-while?
853 ;; The next `when' triggers unless we are SURE that
854 ;; the `while' is not the tailend of a `do-while'.
855 (when (or (not pptok)
856 (memq (char-after pptok) delims)
857 ;; The following kludge is to prevent
858 ;; infinite recursion when called from
859 ;; c-awk-after-if-for-while-condition-p,
860 ;; or the like.
861 (and (eq (point) start)
862 (c-vsemi-status-unknown-p))
863 (c-at-vsemi-p pptok))
864 ;; Since this can cause backtracking we do a
865 ;; little more careful analysis to avoid it: If
866 ;; the while isn't followed by a (possibly
867 ;; virtual) semicolon it can't be a do-while.
868 (c-bos-push-state)
869 (setq state 'while)))
870 ((memq sym '(catch finally))
871 (c-bos-push-state)
872 (c-bos-save-error-info 'try sym)
873 (setq state 'catch))))
874
875 (when c-maybe-labelp
876 ;; We're either past a statement boundary or at the
877 ;; start of a statement, so throw away any label data
878 ;; for the previous one.
879 (setq after-labels-pos nil
880 last-label-pos nil
881 c-maybe-labelp nil))))
882
883 ;; Step to the previous sexp, but not if we crossed a
884 ;; boundary, since that doesn't consume an sexp.
885 (if (eq sym 'boundary)
886 (setq ret 'previous)
887
888 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
889 ;; BACKWARDS THROUGH THE SOURCE.
890
891 ;; This is typically fast with the caching done by
892 ;; c-(backward|forward)-sws.
893 (c-backward-syntactic-ws)
894
895 (let ((before-sws-pos (point))
896 ;; Set as long as we have to continue jumping by sexps.
897 ;; It's the position to use as end in the next round.
898 sexp-loop-continue-pos
899 ;; The end position of the area to search for statement
900 ;; barriers in this round.
901 (sexp-loop-end-pos pos))
902
903 ;; The following while goes back one sexp per iteration.
904 (while
905 (progn
906 (unless (c-safe (c-backward-sexp) t)
907 ;; Give up if we hit an unbalanced block. Since the
908 ;; stack won't be empty the code below will report a
909 ;; suitable error.
910 (throw 'loop nil))
911
912 ;; Check if the sexp movement crossed a statement or
913 ;; declaration boundary. But first modify the point
914 ;; so that `c-crosses-statement-barrier-p' only looks
915 ;; at the non-sexp chars following the sexp.
916 (save-excursion
917 (when (setq
918 boundary-pos
919 (cond
920 ((if macro-start
921 nil
922 (save-excursion
923 (when (c-beginning-of-macro)
924 ;; Set continuation position in case
925 ;; `c-crosses-statement-barrier-p'
926 ;; doesn't detect anything below.
927 (setq sexp-loop-continue-pos (point)))))
928 ;; If the sexp movement took us into a
929 ;; macro then there were only some non-sexp
930 ;; chars after it. Skip out of the macro
931 ;; to analyze them but not the non-sexp
932 ;; chars that might be inside the macro.
933 (c-end-of-macro)
934 (c-crosses-statement-barrier-p
935 (point) sexp-loop-end-pos))
936
937 ((and
938 (eq (char-after) ?{)
939 (not (c-looking-at-inexpr-block lim nil t)))
940 ;; Passed a block sexp. That's a boundary
941 ;; alright.
942 (point))
943
944 ((looking-at "\\s\(")
945 ;; Passed some other paren. Only analyze
946 ;; the non-sexp chars after it.
947 (goto-char (1+ (c-down-list-backward
948 before-sws-pos)))
949 ;; We're at a valid token start position
950 ;; (outside the `save-excursion') if
951 ;; `c-crosses-statement-barrier-p' failed.
952 (c-crosses-statement-barrier-p
953 (point) sexp-loop-end-pos))
954
955 (t
956 ;; Passed a symbol sexp or line
957 ;; continuation. It doesn't matter that
958 ;; it's included in the analyzed region.
959 (if (c-crosses-statement-barrier-p
960 (point) sexp-loop-end-pos)
961 t
962 ;; If it was a line continuation then we
963 ;; have to continue looping.
964 (if (looking-at "\\\\$")
965 (setq sexp-loop-continue-pos (point)))
966 nil))))
967
968 (setq pptok ptok
969 ptok tok
970 tok boundary-pos
971 sym 'boundary)
972 ;; Like a C "continue". Analyze the next sexp.
973 (throw 'loop t)))
974
975 sexp-loop-continue-pos) ; End of "go back a sexp" loop.
976 (goto-char sexp-loop-continue-pos)
977 (setq sexp-loop-end-pos sexp-loop-continue-pos
978 sexp-loop-continue-pos nil))))
979
980 ;; ObjC method def?
981 (when (and c-opt-method-key
982 (setq saved (c-in-method-def-p)))
983 (setq pos saved
984 ignore-labels t) ; Avoid the label check on exit.
985 (throw 'loop nil))
986
987 ;; Handle labels.
988 (unless (eq ignore-labels t)
989 (when (numberp c-maybe-labelp)
990 ;; `c-crosses-statement-barrier-p' has found a colon, so we
991 ;; might be in a label now. Have we got a real label
992 ;; (including a case label) or something like C++'s "public:"?
993 (if (or (not (looking-at c-nonlabel-token-key)) ; proper label
994 (save-excursion ; e.g. "case 'a':" ?
995 (and (c-safe (c-backward-sexp) t)
996 (looking-at "\\<case\\>")))) ; FIXME!!! this is
997 ; wrong for AWK. 2006/1/14.
998 (progn
999 (if after-labels-pos ; Have we already encountered a label?
1000 (if (not last-label-pos)
1001 (setq last-label-pos (or tok start)))
1002 (setq after-labels-pos (or tok start)))
1003 (setq c-maybe-labelp t
1004 label-good-pos nil))
1005 (setq c-maybe-labelp nil))) ; bogus "label"
1006
1007 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1008 ; been found.
1009 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1010 ;; We're in a potential label and it's the first
1011 ;; time we've found something that isn't allowed in
1012 ;; one.
1013 (setq label-good-pos (or tok start))))
1014
1015 ;; We've moved back by a sexp, so update the token positions.
1016 (setq sym nil
1017 pptok ptok
1018 ptok tok
1019 tok (point)
1020 pos tok))) ; Not nil (for the while loop).
1021
1022 ;; If the stack isn't empty there might be errors to report.
1023 (while stack
1024 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1025 (c-bos-report-error))
1026 (setq saved-pos (cdr (car stack))
1027 stack (cdr stack)))
1028
1029 (when (and (eq ret 'same)
1030 (not (memq sym '(boundary ignore nil))))
1031 ;; Need to investigate closer whether we've crossed
1032 ;; between a substatement and its containing statement.
1033 (if (setq saved (if (looking-at c-block-stmt-1-key)
1034 ptok
1035 pptok))
1036 (cond ((> start saved) (setq pos saved))
1037 ((= start saved) (setq ret 'up)))))
1038
1039 (when (and (not ignore-labels)
1040 (eq c-maybe-labelp t)
1041 (not (eq ret 'beginning))
1042 after-labels-pos
1043 (or (not label-good-pos)
1044 (<= label-good-pos pos)
1045 (progn
1046 (goto-char (if (and last-label-pos
1047 (< last-label-pos start))
1048 last-label-pos
1049 pos))
1050 (looking-at c-label-kwds-regexp))))
1051 ;; We're in a label. Maybe we should step to the statement
1052 ;; after it.
1053 (if (< after-labels-pos start)
1054 (setq pos after-labels-pos)
1055 (setq ret 'label)
1056 (if (and last-label-pos (< last-label-pos start))
1057 ;; Might have jumped over several labels. Go to the last one.
1058 (setq pos last-label-pos)))))
1059
1060 ;; Skip over the unary operators that can start the statement.
1061 (goto-char pos)
1062 (while (progn
1063 (c-backward-syntactic-ws)
1064 ;; protect AWK post-inc/decrement operators, etc.
1065 (and (not (c-at-vsemi-p (point)))
1066 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1067 (setq pos (point)))
1068 (goto-char pos)
1069 ret)))
1070
1071 (defun c-crosses-statement-barrier-p (from to)
1072 "Return non-nil if buffer positions FROM to TO cross one or more
1073 statement or declaration boundaries. The returned value is actually
1074 the position of the earliest boundary char. FROM must not be within
1075 a string or comment.
1076
1077 The variable `c-maybe-labelp' is set to the position of the first `:' that
1078 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1079 single `?' is found, then `c-maybe-labelp' is cleared.
1080
1081 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1082 regarded as having a \"virtual semicolon\" immediately after the last token on
1083 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1084
1085 Note that this function might do hidden buffer changes. See the
1086 comment at the start of cc-engine.el for more info."
1087 (let ((skip-chars c-stmt-delim-chars)
1088 lit-range)
1089 (save-excursion
1090 (catch 'done
1091 (goto-char from)
1092 (while (progn (skip-chars-forward skip-chars to)
1093 (< (point) to))
1094 (cond
1095 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1096 (goto-char (cdr lit-range)))
1097 ((eq (char-after) ?:)
1098 (forward-char)
1099 (if (and (eq (char-after) ?:)
1100 (< (point) to))
1101 ;; Ignore scope operators.
1102 (forward-char)
1103 (setq c-maybe-labelp (1- (point)))))
1104 ((eq (char-after) ??)
1105 ;; A question mark. Can't be a label, so stop
1106 ;; looking for more : and ?.
1107 (setq c-maybe-labelp nil
1108 skip-chars (substring c-stmt-delim-chars 0 -2)))
1109 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1110 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1111 (backward-char))
1112 (skip-chars-backward " \t" from)
1113 (if (c-at-vsemi-p)
1114 (throw 'done (point))
1115 (forward-line)))
1116 (t (throw 'done (point)))))
1117 ;; In trailing space after an as yet undetected virtual semicolon?
1118 (c-backward-syntactic-ws from)
1119 (if (and (< (point) to)
1120 (c-at-vsemi-p))
1121 (point)
1122 nil)))))
1123
1124 (defun c-at-statement-start-p ()
1125 "Return non-nil if the point is at the first token in a statement
1126 or somewhere in the syntactic whitespace before it.
1127
1128 A \"statement\" here is not restricted to those inside code blocks.
1129 Any kind of declaration-like construct that occur outside function
1130 bodies is also considered a \"statement\".
1131
1132 Note that this function might do hidden buffer changes. See the
1133 comment at the start of cc-engine.el for more info."
1134
1135 (save-excursion
1136 (let ((end (point))
1137 c-maybe-labelp)
1138 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1139 (or (bobp)
1140 (eq (char-before) ?})
1141 (and (eq (char-before) ?{)
1142 (not (and c-special-brace-lists
1143 (progn (backward-char)
1144 (c-looking-at-special-brace-list)))))
1145 (c-crosses-statement-barrier-p (point) end)))))
1146
1147 (defun c-at-expression-start-p ()
1148 "Return non-nil if the point is at the first token in an expression or
1149 statement, or somewhere in the syntactic whitespace before it.
1150
1151 An \"expression\" here is a bit different from the normal language
1152 grammar sense: It's any sequence of expression tokens except commas,
1153 unless they are enclosed inside parentheses of some kind. Also, an
1154 expression never continues past an enclosing parenthesis, but it might
1155 contain parenthesis pairs of any sort except braces.
1156
1157 Since expressions never cross statement boundaries, this function also
1158 recognizes statement beginnings, just like `c-at-statement-start-p'.
1159
1160 Note that this function might do hidden buffer changes. See the
1161 comment at the start of cc-engine.el for more info."
1162
1163 (save-excursion
1164 (let ((end (point))
1165 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1166 c-maybe-labelp)
1167 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1168 (or (bobp)
1169 (memq (char-before) '(?{ ?}))
1170 (save-excursion (backward-char)
1171 (looking-at "\\s("))
1172 (c-crosses-statement-barrier-p (point) end)))))
1173
1174 \f
1175 ;; A set of functions that covers various idiosyncrasies in
1176 ;; implementations of `forward-comment'.
1177
1178 ;; Note: Some emacsen considers incorrectly that any line comment
1179 ;; ending with a backslash continues to the next line. I can't think
1180 ;; of any way to work around that in a reliable way without changing
1181 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1182 ;; changing the syntax for backslash doesn't work since we must treat
1183 ;; escapes in string literals correctly.)
1184
1185 (defun c-forward-single-comment ()
1186 "Move forward past whitespace and the closest following comment, if any.
1187 Return t if a comment was found, nil otherwise. In either case, the
1188 point is moved past the following whitespace. Line continuations,
1189 i.e. a backslashes followed by line breaks, are treated as whitespace.
1190 The line breaks that end line comments are considered to be the
1191 comment enders, so the point will be put on the beginning of the next
1192 line if it moved past a line comment.
1193
1194 This function does not do any hidden buffer changes."
1195
1196 (let ((start (point)))
1197 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1198 (goto-char (match-end 0)))
1199
1200 (when (forward-comment 1)
1201 (if (eobp)
1202 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1203 ;; forwards at eob.
1204 nil
1205
1206 ;; Emacs includes the ending newline in a b-style (c++)
1207 ;; comment, but XEmacs doesn't. We depend on the Emacs
1208 ;; behavior (which also is symmetric).
1209 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1210 (condition-case nil (forward-char 1)))
1211
1212 t))))
1213
1214 (defsubst c-forward-comments ()
1215 "Move forward past all following whitespace and comments.
1216 Line continuations, i.e. a backslashes followed by line breaks, are
1217 treated as whitespace.
1218
1219 Note that this function might do hidden buffer changes. See the
1220 comment at the start of cc-engine.el for more info."
1221
1222 (while (or
1223 ;; If forward-comment in at least XEmacs 21 is given a large
1224 ;; positive value, it'll loop all the way through if it hits
1225 ;; eob.
1226 (and (forward-comment 5)
1227 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1228 ;; forwards at eob.
1229 (not (eobp)))
1230
1231 (when (looking-at "\\\\[\n\r]")
1232 (forward-char 2)
1233 t))))
1234
1235 (defun c-backward-single-comment ()
1236 "Move backward past whitespace and the closest preceding comment, if any.
1237 Return t if a comment was found, nil otherwise. In either case, the
1238 point is moved past the preceding whitespace. Line continuations,
1239 i.e. a backslashes followed by line breaks, are treated as whitespace.
1240 The line breaks that end line comments are considered to be the
1241 comment enders, so the point cannot be at the end of the same line to
1242 move over a line comment.
1243
1244 This function does not do any hidden buffer changes."
1245
1246 (let ((start (point)))
1247 ;; When we got newline terminated comments, forward-comment in all
1248 ;; supported emacsen so far will stop at eol of each line not
1249 ;; ending with a comment when moving backwards. This corrects for
1250 ;; that, and at the same time handles line continuations.
1251 (while (progn
1252 (skip-chars-backward " \t\n\r\f\v")
1253 (and (looking-at "[\n\r]")
1254 (eq (char-before) ?\\)))
1255 (backward-char))
1256
1257 (if (bobp)
1258 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1259 ;; backwards at bob.
1260 nil
1261
1262 ;; Leave point after the closest following newline if we've
1263 ;; backed up over any above, since forward-comment won't move
1264 ;; backward over a line comment if point is at the end of the
1265 ;; same line.
1266 (re-search-forward "\\=\\s *[\n\r]" start t)
1267
1268 (if (if (forward-comment -1)
1269 (if (eolp)
1270 ;; If forward-comment above succeeded and we're at eol
1271 ;; then the newline we moved over above didn't end a
1272 ;; line comment, so we give it another go.
1273 (forward-comment -1)
1274 t))
1275
1276 ;; Emacs <= 20 and XEmacs move back over the closer of a
1277 ;; block comment that lacks an opener.
1278 (if (looking-at "\\*/")
1279 (progn (forward-char 2) nil)
1280 t)))))
1281
1282 (defsubst c-backward-comments ()
1283 "Move backward past all preceding whitespace and comments.
1284 Line continuations, i.e. a backslashes followed by line breaks, are
1285 treated as whitespace. The line breaks that end line comments are
1286 considered to be the comment enders, so the point cannot be at the end
1287 of the same line to move over a line comment. Unlike
1288 c-backward-syntactic-ws, this function doesn't move back over
1289 preprocessor directives.
1290
1291 Note that this function might do hidden buffer changes. See the
1292 comment at the start of cc-engine.el for more info."
1293
1294 (let ((start (point)))
1295 (while (and
1296 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1297 ;; return t when moving backwards at bob.
1298 (not (bobp))
1299
1300 (if (forward-comment -1)
1301 (if (looking-at "\\*/")
1302 ;; Emacs <= 20 and XEmacs move back over the
1303 ;; closer of a block comment that lacks an opener.
1304 (progn (forward-char 2) nil)
1305 t)
1306
1307 ;; XEmacs treats line continuations as whitespace but
1308 ;; only in the backward direction, which seems a bit
1309 ;; odd. Anyway, this is necessary for Emacs.
1310 (when (and (looking-at "[\n\r]")
1311 (eq (char-before) ?\\)
1312 (< (point) start))
1313 (backward-char)
1314 t))))))
1315
1316 \f
1317 ;; Tools for skipping over syntactic whitespace.
1318
1319 ;; The following functions use text properties to cache searches over
1320 ;; large regions of syntactic whitespace. It works as follows:
1321 ;;
1322 ;; o If a syntactic whitespace region contains anything but simple
1323 ;; whitespace (i.e. space, tab and line breaks), the text property
1324 ;; `c-in-sws' is put over it. At places where we have stopped
1325 ;; within that region there's also a `c-is-sws' text property.
1326 ;; That since there typically are nested whitespace inside that
1327 ;; must be handled separately, e.g. whitespace inside a comment or
1328 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1329 ;; to jump to another point with that property within the same
1330 ;; `c-in-sws' region. It can be likened to a ladder where
1331 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1332 ;;
1333 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1334 ;; a "rung position" and also maybe on the first following char.
1335 ;; As many characters as can be conveniently found in this range
1336 ;; are marked, but no assumption can be made that the whole range
1337 ;; is marked (it could be clobbered by later changes, for
1338 ;; instance).
1339 ;;
1340 ;; Note that some part of the beginning of a sequence of simple
1341 ;; whitespace might be part of the end of a preceding line comment
1342 ;; or cpp directive and must not be considered part of the "rung".
1343 ;; Such whitespace is some amount of horizontal whitespace followed
1344 ;; by a newline. In the case of cpp directives it could also be
1345 ;; two newlines with horizontal whitespace between them.
1346 ;;
1347 ;; The reason to include the first following char is to cope with
1348 ;; "rung positions" that doesn't have any ordinary whitespace. If
1349 ;; `c-is-sws' is put on a token character it does not have
1350 ;; `c-in-sws' set simultaneously. That's the only case when that
1351 ;; can occur, and the reason for not extending the `c-in-sws'
1352 ;; region to cover it is that the `c-in-sws' region could then be
1353 ;; accidentally merged with a following one if the token is only
1354 ;; one character long.
1355 ;;
1356 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1357 ;; removed in the changed region. If the change was inside
1358 ;; syntactic whitespace that means that the "ladder" is broken, but
1359 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1360 ;; parts on either side and use an ordinary search only to "repair"
1361 ;; the gap.
1362 ;;
1363 ;; Special care needs to be taken if a region is removed: If there
1364 ;; are `c-in-sws' on both sides of it which do not connect inside
1365 ;; the region then they can't be joined. If e.g. a marked macro is
1366 ;; broken, syntactic whitespace inside the new text might be
1367 ;; marked. If those marks would become connected with the old
1368 ;; `c-in-sws' range around the macro then we could get a ladder
1369 ;; with one end outside the macro and the other at some whitespace
1370 ;; within it.
1371 ;;
1372 ;; The main motivation for this system is to increase the speed in
1373 ;; skipping over the large whitespace regions that can occur at the
1374 ;; top level in e.g. header files that contain a lot of comments and
1375 ;; cpp directives. For small comments inside code it's probably
1376 ;; slower than using `forward-comment' straightforwardly, but speed is
1377 ;; not a significant factor there anyway.
1378
1379 ; (defface c-debug-is-sws-face
1380 ; '((t (:background "GreenYellow")))
1381 ; "Debug face to mark the `c-is-sws' property.")
1382 ; (defface c-debug-in-sws-face
1383 ; '((t (:underline t)))
1384 ; "Debug face to mark the `c-in-sws' property.")
1385
1386 ; (defun c-debug-put-sws-faces ()
1387 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1388 ; ;; properties in the buffer.
1389 ; (interactive)
1390 ; (save-excursion
1391 ; (c-save-buffer-state (in-face)
1392 ; (goto-char (point-min))
1393 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1394 ; (point)))
1395 ; (while (progn
1396 ; (goto-char (next-single-property-change
1397 ; (point) 'c-is-sws nil (point-max)))
1398 ; (if in-face
1399 ; (progn
1400 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1401 ; (setq in-face nil))
1402 ; (setq in-face (point)))
1403 ; (not (eobp))))
1404 ; (goto-char (point-min))
1405 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1406 ; (point)))
1407 ; (while (progn
1408 ; (goto-char (next-single-property-change
1409 ; (point) 'c-in-sws nil (point-max)))
1410 ; (if in-face
1411 ; (progn
1412 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1413 ; (setq in-face nil))
1414 ; (setq in-face (point)))
1415 ; (not (eobp)))))))
1416
1417 (defmacro c-debug-sws-msg (&rest args)
1418 ;;`(message ,@args)
1419 )
1420
1421 (defmacro c-put-is-sws (beg end)
1422 ;; This macro does a hidden buffer change.
1423 `(let ((beg ,beg) (end ,end))
1424 (put-text-property beg end 'c-is-sws t)
1425 ,@(when (facep 'c-debug-is-sws-face)
1426 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1427
1428 (defmacro c-put-in-sws (beg end)
1429 ;; This macro does a hidden buffer change.
1430 `(let ((beg ,beg) (end ,end))
1431 (put-text-property beg end 'c-in-sws t)
1432 ,@(when (facep 'c-debug-is-sws-face)
1433 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1434
1435 (defmacro c-remove-is-sws (beg end)
1436 ;; This macro does a hidden buffer change.
1437 `(let ((beg ,beg) (end ,end))
1438 (remove-text-properties beg end '(c-is-sws nil))
1439 ,@(when (facep 'c-debug-is-sws-face)
1440 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1441
1442 (defmacro c-remove-in-sws (beg end)
1443 ;; This macro does a hidden buffer change.
1444 `(let ((beg ,beg) (end ,end))
1445 (remove-text-properties beg end '(c-in-sws nil))
1446 ,@(when (facep 'c-debug-is-sws-face)
1447 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1448
1449 (defmacro c-remove-is-and-in-sws (beg end)
1450 ;; This macro does a hidden buffer change.
1451 `(let ((beg ,beg) (end ,end))
1452 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1453 ,@(when (facep 'c-debug-is-sws-face)
1454 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1455 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1456
1457 (defsubst c-invalidate-sws-region-after (beg end)
1458 ;; Called from `after-change-functions'. Note that if
1459 ;; `c-forward-sws' or `c-backward-sws' are used outside
1460 ;; `c-save-buffer-state' or similar then this will remove the cache
1461 ;; properties right after they're added.
1462 ;;
1463 ;; This function does hidden buffer changes.
1464
1465 (save-excursion
1466 ;; Adjust the end to remove the properties in any following simple
1467 ;; ws up to and including the next line break, if there is any
1468 ;; after the changed region. This is necessary e.g. when a rung
1469 ;; marked empty line is converted to a line comment by inserting
1470 ;; "//" before the line break. In that case the line break would
1471 ;; keep the rung mark which could make a later `c-backward-sws'
1472 ;; move into the line comment instead of over it.
1473 (goto-char end)
1474 (skip-chars-forward " \t\f\v")
1475 (when (and (eolp) (not (eobp)))
1476 (setq end (1+ (point)))))
1477
1478 (when (and (= beg end)
1479 (get-text-property beg 'c-in-sws)
1480 (> beg (point-min))
1481 (get-text-property (1- beg) 'c-in-sws))
1482 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1483 ;; safe to keep a range that was continuous before the change. E.g:
1484 ;;
1485 ;; #define foo
1486 ;; \
1487 ;; bar
1488 ;;
1489 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1490 ;; after "foo" is removed then "bar" will become part of the cpp
1491 ;; directive instead of a syntactically relevant token. In that
1492 ;; case there's no longer syntactic ws from "#" to "b".
1493 (setq beg (1- beg)))
1494
1495 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1496 (c-remove-is-and-in-sws beg end))
1497
1498 (defun c-forward-sws ()
1499 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1500 ;;
1501 ;; This function might do hidden buffer changes.
1502
1503 (let (;; `rung-pos' is set to a position as early as possible in the
1504 ;; unmarked part of the simple ws region.
1505 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1506 rung-is-marked next-rung-is-marked simple-ws-end
1507 ;; `safe-start' is set when it's safe to cache the start position.
1508 ;; It's not set if we've initially skipped over comments and line
1509 ;; continuations since we might have gone out through the end of a
1510 ;; macro then. This provision makes `c-forward-sws' not populate the
1511 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1512 ;; more common.
1513 safe-start)
1514
1515 ;; Skip simple ws and do a quick check on the following character to see
1516 ;; if it's anything that can't start syntactic ws, so we can bail out
1517 ;; early in the majority of cases when there just are a few ws chars.
1518 (skip-chars-forward " \t\n\r\f\v")
1519 (when (looking-at c-syntactic-ws-start)
1520
1521 (setq rung-end-pos (min (1+ (point)) (point-max)))
1522 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1523 'c-is-sws t))
1524 ;; Find the last rung position to avoid setting properties in all
1525 ;; the cases when the marked rung is complete.
1526 ;; (`next-single-property-change' is certain to move at least one
1527 ;; step forward.)
1528 (setq rung-pos (1- (next-single-property-change
1529 rung-is-marked 'c-is-sws nil rung-end-pos)))
1530 ;; Got no marked rung here. Since the simple ws might have started
1531 ;; inside a line comment or cpp directive we must set `rung-pos' as
1532 ;; high as possible.
1533 (setq rung-pos (point)))
1534
1535 (while
1536 (progn
1537 (while
1538 (when (and rung-is-marked
1539 (get-text-property (point) 'c-in-sws))
1540
1541 ;; The following search is the main reason that `c-in-sws'
1542 ;; and `c-is-sws' aren't combined to one property.
1543 (goto-char (next-single-property-change
1544 (point) 'c-in-sws nil (point-max)))
1545 (unless (get-text-property (point) 'c-is-sws)
1546 ;; If the `c-in-sws' region extended past the last
1547 ;; `c-is-sws' char we have to go back a bit.
1548 (or (get-text-property (1- (point)) 'c-is-sws)
1549 (goto-char (previous-single-property-change
1550 (point) 'c-is-sws)))
1551 (backward-char))
1552
1553 (c-debug-sws-msg
1554 "c-forward-sws cached move %s -> %s (max %s)"
1555 rung-pos (point) (point-max))
1556
1557 (setq rung-pos (point))
1558 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1559 (not (eobp))))
1560
1561 ;; We'll loop here if there is simple ws after the last rung.
1562 ;; That means that there's been some change in it and it's
1563 ;; possible that we've stepped into another ladder, so extend
1564 ;; the previous one to join with it if there is one, and try to
1565 ;; use the cache again.
1566 (c-debug-sws-msg
1567 "c-forward-sws extending rung with [%s..%s] (max %s)"
1568 (1+ rung-pos) (1+ (point)) (point-max))
1569 (unless (get-text-property (point) 'c-is-sws)
1570 ;; Remove any `c-in-sws' property from the last char of
1571 ;; the rung before we mark it with `c-is-sws', so that we
1572 ;; won't connect with the remains of a broken "ladder".
1573 (c-remove-in-sws (point) (1+ (point))))
1574 (c-put-is-sws (1+ rung-pos)
1575 (1+ (point)))
1576 (c-put-in-sws rung-pos
1577 (setq rung-pos (point)
1578 last-put-in-sws-pos rung-pos)))
1579
1580 (setq simple-ws-end (point))
1581 (c-forward-comments)
1582
1583 (cond
1584 ((/= (point) simple-ws-end)
1585 ;; Skipped over comments. Don't cache at eob in case the buffer
1586 ;; is narrowed.
1587 (not (eobp)))
1588
1589 ((save-excursion
1590 (and c-opt-cpp-prefix
1591 (looking-at c-opt-cpp-start)
1592 (progn (skip-chars-backward " \t")
1593 (bolp))
1594 (or (bobp)
1595 (progn (backward-char)
1596 (not (eq (char-before) ?\\))))))
1597 ;; Skip a preprocessor directive.
1598 (end-of-line)
1599 (while (and (eq (char-before) ?\\)
1600 (= (forward-line 1) 0))
1601 (end-of-line))
1602 (forward-line 1)
1603 (setq safe-start t)
1604 ;; Don't cache at eob in case the buffer is narrowed.
1605 (not (eobp)))))
1606
1607 ;; We've searched over a piece of non-white syntactic ws. See if this
1608 ;; can be cached.
1609 (setq next-rung-pos (point))
1610 (skip-chars-forward " \t\n\r\f\v")
1611 (setq rung-end-pos (min (1+ (point)) (point-max)))
1612
1613 (if (or
1614 ;; Cache if we haven't skipped comments only, and if we started
1615 ;; either from a marked rung or from a completely uncached
1616 ;; position.
1617 (and safe-start
1618 (or rung-is-marked
1619 (not (get-text-property simple-ws-end 'c-in-sws))))
1620
1621 ;; See if there's a marked rung in the encountered simple ws. If
1622 ;; so then we can cache, unless `safe-start' is nil. Even then
1623 ;; we need to do this to check if the cache can be used for the
1624 ;; next step.
1625 (and (setq next-rung-is-marked
1626 (text-property-any next-rung-pos rung-end-pos
1627 'c-is-sws t))
1628 safe-start))
1629
1630 (progn
1631 (c-debug-sws-msg
1632 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1633 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1634 (point-max))
1635
1636 ;; Remove the properties for any nested ws that might be cached.
1637 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1638 ;; anyway.
1639 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1640 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1641 (c-put-is-sws rung-pos
1642 (1+ simple-ws-end))
1643 (setq rung-is-marked t))
1644 (c-put-in-sws rung-pos
1645 (setq rung-pos (point)
1646 last-put-in-sws-pos rung-pos))
1647 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1648 ;; Remove any `c-in-sws' property from the last char of
1649 ;; the rung before we mark it with `c-is-sws', so that we
1650 ;; won't connect with the remains of a broken "ladder".
1651 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1652 (c-put-is-sws next-rung-pos
1653 rung-end-pos))
1654
1655 (c-debug-sws-msg
1656 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1657 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1658 (point-max))
1659
1660 ;; Set `rung-pos' for the next rung. It's the same thing here as
1661 ;; initially, except that the rung position is set as early as
1662 ;; possible since we can't be in the ending ws of a line comment or
1663 ;; cpp directive now.
1664 (if (setq rung-is-marked next-rung-is-marked)
1665 (setq rung-pos (1- (next-single-property-change
1666 rung-is-marked 'c-is-sws nil rung-end-pos)))
1667 (setq rung-pos next-rung-pos))
1668 (setq safe-start t)))
1669
1670 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1671 ;; another one after the point (which might occur when editing inside a
1672 ;; comment or macro).
1673 (when (eq last-put-in-sws-pos (point))
1674 (cond ((< last-put-in-sws-pos (point-max))
1675 (c-debug-sws-msg
1676 "c-forward-sws clearing at %s for cache separation"
1677 last-put-in-sws-pos)
1678 (c-remove-in-sws last-put-in-sws-pos
1679 (1+ last-put-in-sws-pos)))
1680 (t
1681 ;; If at eob we have to clear the last character before the end
1682 ;; instead since the buffer might be narrowed and there might
1683 ;; be a `c-in-sws' after (point-max). In this case it's
1684 ;; necessary to clear both properties.
1685 (c-debug-sws-msg
1686 "c-forward-sws clearing thoroughly at %s for cache separation"
1687 (1- last-put-in-sws-pos))
1688 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1689 last-put-in-sws-pos))))
1690 )))
1691
1692 (defun c-backward-sws ()
1693 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1694 ;;
1695 ;; This function might do hidden buffer changes.
1696
1697 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1698 ;; part of the simple ws region.
1699 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1700 rung-is-marked simple-ws-beg cmt-skip-pos)
1701
1702 ;; Skip simple horizontal ws and do a quick check on the preceding
1703 ;; character to see if it's anying that can't end syntactic ws, so we can
1704 ;; bail out early in the majority of cases when there just are a few ws
1705 ;; chars. Newlines are complicated in the backward direction, so we can't
1706 ;; skip over them.
1707 (skip-chars-backward " \t\f")
1708 (when (and (not (bobp))
1709 (save-excursion
1710 (backward-char)
1711 (looking-at c-syntactic-ws-end)))
1712
1713 ;; Try to find a rung position in the simple ws preceding point, so that
1714 ;; we can get a cache hit even if the last bit of the simple ws has
1715 ;; changed recently.
1716 (setq simple-ws-beg (point))
1717 (skip-chars-backward " \t\n\r\f\v")
1718 (if (setq rung-is-marked (text-property-any
1719 (point) (min (1+ rung-pos) (point-max))
1720 'c-is-sws t))
1721 ;; `rung-pos' will be the earliest marked position, which means that
1722 ;; there might be later unmarked parts in the simple ws region.
1723 ;; It's not worth the effort to fix that; the last part of the
1724 ;; simple ws is also typically edited often, so it could be wasted.
1725 (goto-char (setq rung-pos rung-is-marked))
1726 (goto-char simple-ws-beg))
1727
1728 (while
1729 (progn
1730 (while
1731 (when (and rung-is-marked
1732 (not (bobp))
1733 (get-text-property (1- (point)) 'c-in-sws))
1734
1735 ;; The following search is the main reason that `c-in-sws'
1736 ;; and `c-is-sws' aren't combined to one property.
1737 (goto-char (previous-single-property-change
1738 (point) 'c-in-sws nil (point-min)))
1739 (unless (get-text-property (point) 'c-is-sws)
1740 ;; If the `c-in-sws' region extended past the first
1741 ;; `c-is-sws' char we have to go forward a bit.
1742 (goto-char (next-single-property-change
1743 (point) 'c-is-sws)))
1744
1745 (c-debug-sws-msg
1746 "c-backward-sws cached move %s <- %s (min %s)"
1747 (point) rung-pos (point-min))
1748
1749 (setq rung-pos (point))
1750 (if (and (< (min (skip-chars-backward " \t\f\v")
1751 (progn
1752 (setq simple-ws-beg (point))
1753 (skip-chars-backward " \t\n\r\f\v")))
1754 0)
1755 (setq rung-is-marked
1756 (text-property-any (point) rung-pos
1757 'c-is-sws t)))
1758 t
1759 (goto-char simple-ws-beg)
1760 nil))
1761
1762 ;; We'll loop here if there is simple ws before the first rung.
1763 ;; That means that there's been some change in it and it's
1764 ;; possible that we've stepped into another ladder, so extend
1765 ;; the previous one to join with it if there is one, and try to
1766 ;; use the cache again.
1767 (c-debug-sws-msg
1768 "c-backward-sws extending rung with [%s..%s] (min %s)"
1769 rung-is-marked rung-pos (point-min))
1770 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1771 ;; Remove any `c-in-sws' property from the last char of
1772 ;; the rung before we mark it with `c-is-sws', so that we
1773 ;; won't connect with the remains of a broken "ladder".
1774 (c-remove-in-sws (1- rung-pos) rung-pos))
1775 (c-put-is-sws rung-is-marked
1776 rung-pos)
1777 (c-put-in-sws rung-is-marked
1778 (1- rung-pos))
1779 (setq rung-pos rung-is-marked
1780 last-put-in-sws-pos rung-pos))
1781
1782 (c-backward-comments)
1783 (setq cmt-skip-pos (point))
1784
1785 (cond
1786 ((and c-opt-cpp-prefix
1787 (/= cmt-skip-pos simple-ws-beg)
1788 (c-beginning-of-macro))
1789 ;; Inside a cpp directive. See if it should be skipped over.
1790 (let ((cpp-beg (point)))
1791
1792 ;; Move back over all line continuations in the region skipped
1793 ;; over by `c-backward-comments'. If we go past it then we
1794 ;; started inside the cpp directive.
1795 (goto-char simple-ws-beg)
1796 (beginning-of-line)
1797 (while (and (> (point) cmt-skip-pos)
1798 (progn (backward-char)
1799 (eq (char-before) ?\\)))
1800 (beginning-of-line))
1801
1802 (if (< (point) cmt-skip-pos)
1803 ;; Don't move past the cpp directive if we began inside
1804 ;; it. Note that the position at the end of the last line
1805 ;; of the macro is also considered to be within it.
1806 (progn (goto-char cmt-skip-pos)
1807 nil)
1808
1809 ;; It's worthwhile to spend a little bit of effort on finding
1810 ;; the end of the macro, to get a good `simple-ws-beg'
1811 ;; position for the cache. Note that `c-backward-comments'
1812 ;; could have stepped over some comments before going into
1813 ;; the macro, and then `simple-ws-beg' must be kept on the
1814 ;; same side of those comments.
1815 (goto-char simple-ws-beg)
1816 (skip-chars-backward " \t\n\r\f\v")
1817 (if (eq (char-before) ?\\)
1818 (forward-char))
1819 (forward-line 1)
1820 (if (< (point) simple-ws-beg)
1821 ;; Might happen if comments after the macro were skipped
1822 ;; over.
1823 (setq simple-ws-beg (point)))
1824
1825 (goto-char cpp-beg)
1826 t)))
1827
1828 ((/= (save-excursion
1829 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1830 (setq next-rung-pos (point)))
1831 simple-ws-beg)
1832 ;; Skipped over comments. Must put point at the end of
1833 ;; the simple ws at point since we might be after a line
1834 ;; comment or cpp directive that's been partially
1835 ;; narrowed out, and we can't risk marking the simple ws
1836 ;; at the end of it.
1837 (goto-char next-rung-pos)
1838 t)))
1839
1840 ;; We've searched over a piece of non-white syntactic ws. See if this
1841 ;; can be cached.
1842 (setq next-rung-pos (point))
1843 (skip-chars-backward " \t\f\v")
1844
1845 (if (or
1846 ;; Cache if we started either from a marked rung or from a
1847 ;; completely uncached position.
1848 rung-is-marked
1849 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1850
1851 ;; Cache if there's a marked rung in the encountered simple ws.
1852 (save-excursion
1853 (skip-chars-backward " \t\n\r\f\v")
1854 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1855 'c-is-sws t)))
1856
1857 (progn
1858 (c-debug-sws-msg
1859 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1860 (point) (1+ next-rung-pos)
1861 simple-ws-beg (min (1+ rung-pos) (point-max))
1862 (point-min))
1863
1864 ;; Remove the properties for any nested ws that might be cached.
1865 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1866 ;; anyway.
1867 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1868 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1869 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1870 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1871 ;; Remove any `c-in-sws' property from the last char of
1872 ;; the rung before we mark it with `c-is-sws', so that we
1873 ;; won't connect with the remains of a broken "ladder".
1874 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1875 (c-put-is-sws simple-ws-beg
1876 rung-end-pos)
1877 (setq rung-is-marked t)))
1878 (c-put-in-sws (setq simple-ws-beg (point)
1879 last-put-in-sws-pos simple-ws-beg)
1880 rung-pos)
1881 (c-put-is-sws (setq rung-pos simple-ws-beg)
1882 (1+ next-rung-pos)))
1883
1884 (c-debug-sws-msg
1885 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1886 (point) (1+ next-rung-pos)
1887 simple-ws-beg (min (1+ rung-pos) (point-max))
1888 (point-min))
1889 (setq rung-pos next-rung-pos
1890 simple-ws-beg (point))
1891 ))
1892
1893 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1894 ;; another one before the point (which might occur when editing inside a
1895 ;; comment or macro).
1896 (when (eq last-put-in-sws-pos (point))
1897 (cond ((< (point-min) last-put-in-sws-pos)
1898 (c-debug-sws-msg
1899 "c-backward-sws clearing at %s for cache separation"
1900 (1- last-put-in-sws-pos))
1901 (c-remove-in-sws (1- last-put-in-sws-pos)
1902 last-put-in-sws-pos))
1903 ((> (point-min) 1)
1904 ;; If at bob and the buffer is narrowed, we have to clear the
1905 ;; character we're standing on instead since there might be a
1906 ;; `c-in-sws' before (point-min). In this case it's necessary
1907 ;; to clear both properties.
1908 (c-debug-sws-msg
1909 "c-backward-sws clearing thoroughly at %s for cache separation"
1910 last-put-in-sws-pos)
1911 (c-remove-is-and-in-sws last-put-in-sws-pos
1912 (1+ last-put-in-sws-pos)))))
1913 )))
1914
1915 \f
1916 ;; Other whitespace tools
1917 (defun c-partial-ws-p (beg end)
1918 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1919 ;; region? This is a "heuristic" function. .....
1920 ;;
1921 ;; The motivation for the second bit is to check whether removing this
1922 ;; region would coalesce two symbols.
1923 ;;
1924 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1925 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1926 (save-excursion
1927 (let ((end+1 (min (1+ end) (point-max))))
1928 (or (progn (goto-char (max (point-min) (1- beg)))
1929 (c-skip-ws-forward end)
1930 (eq (point) end))
1931 (progn (goto-char beg)
1932 (c-skip-ws-forward end+1)
1933 (eq (point) end+1))))))
1934 \f
1935 ;; A system for finding noteworthy parens before the point.
1936
1937 (defvar c-state-cache nil)
1938 (make-variable-buffer-local 'c-state-cache)
1939 ;; The state cache used by `c-parse-state' to cut down the amount of
1940 ;; searching. It's the result from some earlier `c-parse-state' call.
1941 ;;
1942 ;; The use of the cached info is more effective if the next
1943 ;; `c-parse-state' call is on a line close by the one the cached state
1944 ;; was made at; the cache can actually slow down a little if the
1945 ;; cached state was made very far back in the buffer. The cache is
1946 ;; most effective if `c-parse-state' is used on each line while moving
1947 ;; forward.
1948
1949 (defvar c-state-cache-start 1)
1950 (make-variable-buffer-local 'c-state-cache-start)
1951 ;; This is (point-min) when `c-state-cache' was calculated, since a
1952 ;; change of narrowing is likely to affect the parens that are visible
1953 ;; before the point.
1954
1955 (defvar c-state-cache-good-pos 1)
1956 (make-variable-buffer-local 'c-state-cache-good-pos)
1957 ;; This is a position where `c-state-cache' is known to be correct.
1958 ;; It's a position inside one of the recorded unclosed parens or the
1959 ;; top level, but not further nested inside any literal or subparen
1960 ;; that is closed before the last recorded position.
1961 ;;
1962 ;; The exact position is chosen to try to be close to yet earlier than
1963 ;; the position where `c-state-cache' will be called next. Right now
1964 ;; the heuristic is to set it to the position after the last found
1965 ;; closing paren (of any type) before the line on which
1966 ;; `c-parse-state' was called. That is chosen primarily to work well
1967 ;; with refontification of the current line.
1968
1969 (defsubst c-invalidate-state-cache (pos)
1970 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1971 ;; at POS or higher. This is much like `c-whack-state-after', but
1972 ;; it never changes a paren pair element into an open paren element.
1973 ;; Doing that would mean that the new open paren wouldn't have the
1974 ;; required preceding paren pair element.
1975 (while (and (or c-state-cache
1976 (when (< pos c-state-cache-good-pos)
1977 (setq c-state-cache-good-pos 1)
1978 nil))
1979 (let ((elem (car c-state-cache)))
1980 (if (consp elem)
1981 (or (< pos (cdr elem))
1982 (when (< pos c-state-cache-good-pos)
1983 (setq c-state-cache-good-pos (cdr elem))
1984 nil))
1985 (or (<= pos elem)
1986 (when (< pos c-state-cache-good-pos)
1987 (setq c-state-cache-good-pos (1+ elem))
1988 nil)))))
1989 (setq c-state-cache (cdr c-state-cache))))
1990
1991 (defun c-get-fallback-start-pos (here)
1992 ;; Return the start position for building `c-state-cache' from
1993 ;; scratch.
1994 (save-excursion
1995 ;; Go back 2 bods, but ignore any bogus positions returned by
1996 ;; beginning-of-defun (i.e. open paren in column zero).
1997 (goto-char here)
1998 (let ((cnt 2))
1999 (while (not (or (bobp) (zerop cnt)))
2000 (c-beginning-of-defun-1)
2001 (if (eq (char-after) ?\{)
2002 (setq cnt (1- cnt)))))
2003 (point)))
2004
2005 (defun c-parse-state ()
2006 ;; Find and record all noteworthy parens between some good point
2007 ;; earlier in the file and point. That good point is at least the
2008 ;; beginning of the top-level construct we are in, or the beginning
2009 ;; of the preceding top-level construct if we aren't in one.
2010 ;;
2011 ;; The returned value is a list of the noteworthy parens with the
2012 ;; last one first. If an element in the list is an integer, it's
2013 ;; the position of an open paren which has not been closed before
2014 ;; the point. If an element is a cons, it gives the position of a
2015 ;; closed brace paren pair; the car is the start paren position and
2016 ;; the cdr is the position following the closing paren. Only the
2017 ;; last closed brace paren pair before each open paren and before
2018 ;; the point is recorded, and thus the state never contains two cons
2019 ;; elements in succession.
2020 ;;
2021 ;; Currently no characters which are given paren syntax with the
2022 ;; syntax-table property are recorded, i.e. angle bracket arglist
2023 ;; parens are never present here. Note that this might change.
2024 ;;
2025 ;; BUG: This function doesn't cope entirely well with unbalanced
2026 ;; parens in macros. E.g. in the following case the brace before
2027 ;; the macro isn't balanced with the one after it:
2028 ;;
2029 ;; {
2030 ;; #define X {
2031 ;; }
2032 ;;
2033 ;; This function might do hidden buffer changes.
2034
2035 (save-restriction
2036 (let* ((here (point))
2037 (here-bol (c-point 'bol))
2038 (c-macro-start (c-query-macro-start))
2039 (in-macro-start (or c-macro-start (point)))
2040 old-state last-pos brace-pair-open brace-pair-close
2041 pos save-pos)
2042 (c-invalidate-state-cache here)
2043
2044 ;; If the minimum position has changed due to narrowing then we
2045 ;; have to fix the tail of `c-state-cache' accordingly.
2046 (unless (= c-state-cache-start (point-min))
2047 (if (> (point-min) c-state-cache-start)
2048 ;; If point-min has moved forward then we just need to cut
2049 ;; off a bit of the tail.
2050 (let ((ptr (cons nil c-state-cache)) elem)
2051 (while (and (setq elem (car-safe (cdr ptr)))
2052 (>= (if (consp elem) (car elem) elem)
2053 (point-min)))
2054 (setq ptr (cdr ptr)))
2055 (when (consp ptr)
2056 (if (eq (cdr ptr) c-state-cache)
2057 (setq c-state-cache nil
2058 c-state-cache-good-pos 1)
2059 (setcdr ptr nil))))
2060 ;; If point-min has moved backward then we drop the state
2061 ;; completely. It's possible to do a better job here and
2062 ;; recalculate the top only.
2063 (setq c-state-cache nil
2064 c-state-cache-good-pos 1))
2065 (setq c-state-cache-start (point-min)))
2066
2067 ;; Get the latest position we know are directly inside the
2068 ;; closest containing paren of the cached state.
2069 (setq last-pos (and c-state-cache
2070 (if (consp (car c-state-cache))
2071 (cdr (car c-state-cache))
2072 (1+ (car c-state-cache)))))
2073 (if (or (not last-pos)
2074 (< last-pos c-state-cache-good-pos))
2075 (setq last-pos c-state-cache-good-pos)
2076 ;; Take the opportunity to move the cached good position
2077 ;; further down.
2078 (if (< last-pos here-bol)
2079 (setq c-state-cache-good-pos last-pos)))
2080
2081 ;; Check if `last-pos' is in a macro. If it is, and we're not
2082 ;; in the same macro, we must discard everything on
2083 ;; `c-state-cache' that is inside the macro before using it.
2084 (save-excursion
2085 (goto-char last-pos)
2086 (when (and (c-beginning-of-macro)
2087 (/= (point) in-macro-start))
2088 (c-invalidate-state-cache (point))
2089 ;; Set `last-pos' again just like above except that there's
2090 ;; no use looking at `c-state-cache-good-pos' here.
2091 (setq last-pos (if c-state-cache
2092 (if (consp (car c-state-cache))
2093 (cdr (car c-state-cache))
2094 (1+ (car c-state-cache)))
2095 1))))
2096
2097 ;; If we've moved very far from the last cached position then
2098 ;; it's probably better to redo it from scratch, otherwise we
2099 ;; might spend a lot of time searching from `last-pos' down to
2100 ;; here.
2101 (when (< last-pos (- here 20000))
2102 ;; First get the fallback start position. If it turns out
2103 ;; that it's so far back that the cached state is closer then
2104 ;; we'll keep it afterall.
2105 (setq pos (c-get-fallback-start-pos here))
2106 (if (<= pos last-pos)
2107 (setq pos nil)
2108 (setq last-pos nil
2109 c-state-cache nil
2110 c-state-cache-good-pos 1)))
2111
2112 ;; Find the start position for the forward search. (Can't
2113 ;; search in the backward direction since the point might be in
2114 ;; some kind of literal.)
2115
2116 (unless pos
2117 (setq old-state c-state-cache)
2118
2119 ;; There's a cached state with a containing paren. Pop off
2120 ;; the stale containing sexps from it by going forward out of
2121 ;; parens as far as possible.
2122 (narrow-to-region (point-min) here)
2123 (let (placeholder pair-beg)
2124 (while (and c-state-cache
2125 (setq placeholder
2126 (c-up-list-forward last-pos)))
2127 (setq last-pos placeholder)
2128 (if (consp (car c-state-cache))
2129 (setq pair-beg (car-safe (cdr c-state-cache))
2130 c-state-cache (cdr-safe (cdr c-state-cache)))
2131 (setq pair-beg (car c-state-cache)
2132 c-state-cache (cdr c-state-cache))))
2133
2134 (when (and pair-beg (eq (char-after pair-beg) ?{))
2135 ;; The last paren pair we moved out from was a brace
2136 ;; pair. Modify the state to record this as a closed
2137 ;; pair now.
2138 (if (consp (car-safe c-state-cache))
2139 (setq c-state-cache (cdr c-state-cache)))
2140 (setq c-state-cache (cons (cons pair-beg last-pos)
2141 c-state-cache))))
2142
2143 ;; Check if the preceding balanced paren is within a
2144 ;; macro; it should be ignored if we're outside the
2145 ;; macro. There's no need to check any further upwards;
2146 ;; if the macro contains an unbalanced opening paren then
2147 ;; we're smoked anyway.
2148 (when (and (<= (point) in-macro-start)
2149 (consp (car c-state-cache)))
2150 (save-excursion
2151 (goto-char (car (car c-state-cache)))
2152 (when (c-beginning-of-macro)
2153 (setq here (point)
2154 c-state-cache (cdr c-state-cache)))))
2155
2156 (unless (eq c-state-cache old-state)
2157 ;; Have to adjust the cached good position if state has been
2158 ;; popped off.
2159 (setq c-state-cache-good-pos
2160 (if c-state-cache
2161 (if (consp (car c-state-cache))
2162 (cdr (car c-state-cache))
2163 (1+ (car c-state-cache)))
2164 1)
2165 old-state c-state-cache))
2166
2167 (when c-state-cache
2168 (setq pos last-pos)))
2169
2170 ;; Get the fallback start position.
2171 (unless pos
2172 (setq pos (c-get-fallback-start-pos here)
2173 c-state-cache nil
2174 c-state-cache-good-pos 1))
2175
2176 (narrow-to-region (point-min) here)
2177
2178 (while pos
2179 (setq save-pos pos
2180 brace-pair-open nil)
2181
2182 ;; Find the balanced brace pairs. This loop is hot, so it
2183 ;; does ugly tricks to go faster.
2184 (c-safe
2185 (let (set-good-pos set-brace-pair)
2186 (while t
2187 (setq last-pos nil
2188 last-pos (scan-lists pos 1 -1)) ; Might signal.
2189 (setq pos (scan-lists last-pos 1 1) ; Might signal.
2190 set-good-pos (< pos here-bol)
2191 set-brace-pair (eq (char-before last-pos) ?{))
2192
2193 ;; Update the cached good position and record the brace
2194 ;; pair, whichever is applicable for the paren we've
2195 ;; just jumped over. But first check that it isn't
2196 ;; inside a macro and the point isn't inside the same
2197 ;; one.
2198 (when (and (or set-good-pos set-brace-pair)
2199 (or (>= pos in-macro-start)
2200 (save-excursion
2201 (goto-char pos)
2202 (not (c-beginning-of-macro)))))
2203 (if set-good-pos
2204 (setq c-state-cache-good-pos pos))
2205 (if set-brace-pair
2206 (setq brace-pair-open last-pos
2207 brace-pair-close pos))))))
2208
2209 ;; Record the last brace pair.
2210 (when brace-pair-open
2211 (let ((head (car-safe c-state-cache)))
2212 (if (consp head)
2213 (progn
2214 (setcar head (1- brace-pair-open))
2215 (setcdr head brace-pair-close))
2216 (setq c-state-cache (cons (cons (1- brace-pair-open)
2217 brace-pair-close)
2218 c-state-cache)))))
2219
2220 (if last-pos
2221 ;; Prepare to loop, but record the open paren only if it's
2222 ;; outside a macro or within the same macro as point, and
2223 ;; if it is a legitimate open paren and not some character
2224 ;; that got an open paren syntax-table property.
2225 (progn
2226 (setq pos last-pos)
2227 (when (and (or (>= last-pos in-macro-start)
2228 (save-excursion
2229 (goto-char last-pos)
2230 (not (c-beginning-of-macro))))
2231 ;; Check for known types of parens that we
2232 ;; want to record. The syntax table is not to
2233 ;; be trusted here since the caller might be
2234 ;; using e.g. `c++-template-syntax-table'.
2235 (memq (char-before last-pos) '(?{ ?\( ?\[)))
2236 (if (< last-pos here-bol)
2237 (setq c-state-cache-good-pos last-pos))
2238 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
2239
2240 (if (setq last-pos (c-up-list-forward pos))
2241 ;; Found a close paren without a corresponding opening
2242 ;; one. Maybe we didn't go back far enough, so try to
2243 ;; scan backward for the start paren and then start over.
2244 (progn
2245 (setq pos (c-up-list-backward pos)
2246 c-state-cache nil
2247 c-state-cache-good-pos c-state-cache-start)
2248 (when (or (not pos)
2249 ;; Emacs (up to at least 21.2) can get confused by
2250 ;; open parens in column zero inside comments: The
2251 ;; sexp functions can then misbehave and bring us
2252 ;; back to the same point again. Check this so that
2253 ;; we don't get an infinite loop.
2254 (>= pos save-pos))
2255 (setq pos last-pos
2256 c-parsing-error
2257 (format "Unbalanced close paren at line %d"
2258 (1+ (count-lines (point-min)
2259 (c-point 'bol last-pos)))))))
2260 (setq pos nil))))
2261
2262 ;;(message "c-parse-state: %S end: %S" c-state-cache c-state-cache-good-pos)
2263 c-state-cache)))
2264
2265 ;; Debug tool to catch cache inconsistencies.
2266 (defvar c-debug-parse-state nil)
2267 (unless (fboundp 'c-real-parse-state)
2268 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
2269 (cc-bytecomp-defun c-real-parse-state)
2270 (defun c-debug-parse-state ()
2271 (let ((res1 (c-real-parse-state)) res2)
2272 (let ((c-state-cache nil)
2273 (c-state-cache-start 1)
2274 (c-state-cache-good-pos 1))
2275 (setq res2 (c-real-parse-state)))
2276 (unless (equal res1 res2)
2277 ;; The cache can actually go further back due to the ad-hoc way
2278 ;; the first paren is found, so try to whack off a bit of its
2279 ;; start before complaining.
2280 (save-excursion
2281 (goto-char (or (c-least-enclosing-brace res2) (point)))
2282 (c-beginning-of-defun-1)
2283 (while (not (or (bobp) (eq (char-after) ?{)))
2284 (c-beginning-of-defun-1))
2285 (unless (equal (c-whack-state-before (point) res1) res2)
2286 (message (concat "c-parse-state inconsistency: "
2287 "using cache: %s, from scratch: %s")
2288 res1 res2))))
2289 res1))
2290 (defun c-toggle-parse-state-debug (&optional arg)
2291 (interactive "P")
2292 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
2293 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
2294 'c-debug-parse-state
2295 'c-real-parse-state)))
2296 (c-keep-region-active))
2297 (when c-debug-parse-state
2298 (c-toggle-parse-state-debug 1))
2299
2300 (defun c-whack-state-before (bufpos paren-state)
2301 ;; Whack off any state information from PAREN-STATE which lies
2302 ;; before BUFPOS. Not destructive on PAREN-STATE.
2303 (let* ((newstate (list nil))
2304 (ptr newstate)
2305 car)
2306 (while paren-state
2307 (setq car (car paren-state)
2308 paren-state (cdr paren-state))
2309 (if (< (if (consp car) (car car) car) bufpos)
2310 (setq paren-state nil)
2311 (setcdr ptr (list car))
2312 (setq ptr (cdr ptr))))
2313 (cdr newstate)))
2314
2315 (defun c-whack-state-after (bufpos paren-state)
2316 ;; Whack off any state information from PAREN-STATE which lies at or
2317 ;; after BUFPOS. Not destructive on PAREN-STATE.
2318 (catch 'done
2319 (while paren-state
2320 (let ((car (car paren-state)))
2321 (if (consp car)
2322 ;; just check the car, because in a balanced brace
2323 ;; expression, it must be impossible for the corresponding
2324 ;; close brace to be before point, but the open brace to
2325 ;; be after.
2326 (if (<= bufpos (car car))
2327 nil ; whack it off
2328 (if (< bufpos (cdr car))
2329 ;; its possible that the open brace is before
2330 ;; bufpos, but the close brace is after. In that
2331 ;; case, convert this to a non-cons element. The
2332 ;; rest of the state is before bufpos, so we're
2333 ;; done.
2334 (throw 'done (cons (car car) (cdr paren-state)))
2335 ;; we know that both the open and close braces are
2336 ;; before bufpos, so we also know that everything else
2337 ;; on state is before bufpos.
2338 (throw 'done paren-state)))
2339 (if (<= bufpos car)
2340 nil ; whack it off
2341 ;; it's before bufpos, so everything else should too.
2342 (throw 'done paren-state)))
2343 (setq paren-state (cdr paren-state)))
2344 nil)))
2345
2346 (defun c-most-enclosing-brace (paren-state &optional bufpos)
2347 ;; Return the bufpos of the innermost enclosing open paren before
2348 ;; bufpos, or nil if none was found.
2349 (let (enclosingp)
2350 (or bufpos (setq bufpos 134217727))
2351 (while paren-state
2352 (setq enclosingp (car paren-state)
2353 paren-state (cdr paren-state))
2354 (if (or (consp enclosingp)
2355 (>= enclosingp bufpos))
2356 (setq enclosingp nil)
2357 (setq paren-state nil)))
2358 enclosingp))
2359
2360 (defun c-least-enclosing-brace (paren-state)
2361 ;; Return the bufpos of the outermost enclosing open paren, or nil
2362 ;; if none was found.
2363 (let (pos elem)
2364 (while paren-state
2365 (setq elem (car paren-state)
2366 paren-state (cdr paren-state))
2367 (if (integerp elem)
2368 (setq pos elem)))
2369 pos))
2370
2371 (defun c-safe-position (bufpos paren-state)
2372 ;; Return the closest "safe" position recorded on PAREN-STATE that
2373 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
2374 ;; contain any. Return nil if BUFPOS is nil, which is useful to
2375 ;; find the closest limit before a given limit that might be nil.
2376 ;;
2377 ;; A "safe" position is a position at or after a recorded open
2378 ;; paren, or after a recorded close paren. The returned position is
2379 ;; thus either the first position after a close brace, or the first
2380 ;; position after an enclosing paren, or at the enclosing paren in
2381 ;; case BUFPOS is immediately after it.
2382 (when bufpos
2383 (let (elem)
2384 (catch 'done
2385 (while paren-state
2386 (setq elem (car paren-state))
2387 (if (consp elem)
2388 (cond ((< (cdr elem) bufpos)
2389 (throw 'done (cdr elem)))
2390 ((< (car elem) bufpos)
2391 ;; See below.
2392 (throw 'done (min (1+ (car elem)) bufpos))))
2393 (if (< elem bufpos)
2394 ;; elem is the position at and not after the opening paren, so
2395 ;; we can go forward one more step unless it's equal to
2396 ;; bufpos. This is useful in some cases avoid an extra paren
2397 ;; level between the safe position and bufpos.
2398 (throw 'done (min (1+ elem) bufpos))))
2399 (setq paren-state (cdr paren-state)))))))
2400
2401 (defun c-beginning-of-syntax ()
2402 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2403 ;; goes to the closest previous point that is known to be outside
2404 ;; any string literal or comment. `c-state-cache' is used if it has
2405 ;; a position in the vicinity.
2406 (let* ((paren-state c-state-cache)
2407 elem
2408
2409 (pos (catch 'done
2410 ;; Note: Similar code in `c-safe-position'. The
2411 ;; difference is that we accept a safe position at
2412 ;; the point and don't bother to go forward past open
2413 ;; parens.
2414 (while paren-state
2415 (setq elem (car paren-state))
2416 (if (consp elem)
2417 (cond ((<= (cdr elem) (point))
2418 (throw 'done (cdr elem)))
2419 ((<= (car elem) (point))
2420 (throw 'done (car elem))))
2421 (if (<= elem (point))
2422 (throw 'done elem)))
2423 (setq paren-state (cdr paren-state)))
2424 (point-min))))
2425
2426 (if (> pos (- (point) 4000))
2427 (goto-char pos)
2428 ;; The position is far back. Try `c-beginning-of-defun-1'
2429 ;; (although we can't be entirely sure it will go to a position
2430 ;; outside a comment or string in current emacsen). FIXME:
2431 ;; Consult `syntax-ppss' here.
2432 (c-beginning-of-defun-1)
2433 (if (< (point) pos)
2434 (goto-char pos)))))
2435
2436 \f
2437 ;; Tools for scanning identifiers and other tokens.
2438
2439 (defun c-on-identifier ()
2440 "Return non-nil if the point is on or directly after an identifier.
2441 Keywords are recognized and not considered identifiers. If an
2442 identifier is detected, the returned value is its starting position.
2443 If an identifier ends at the point and another begins at it \(can only
2444 happen in Pike) then the point for the preceding one is returned.
2445
2446 Note that this function might do hidden buffer changes. See the
2447 comment at the start of cc-engine.el for more info."
2448
2449 ;; FIXME: Shouldn't this function handle "operator" in C++?
2450
2451 (save-excursion
2452 (skip-syntax-backward "w_")
2453
2454 (or
2455
2456 ;; Check for a normal (non-keyword) identifier.
2457 (and (looking-at c-symbol-start)
2458 (not (looking-at c-keywords-regexp))
2459 (point))
2460
2461 (when (c-major-mode-is 'pike-mode)
2462 ;; Handle the `<operator> syntax in Pike.
2463 (let ((pos (point)))
2464 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2465 (and (if (< (skip-chars-backward "`") 0)
2466 t
2467 (goto-char pos)
2468 (eq (char-after) ?\`))
2469 (looking-at c-symbol-key)
2470 (>= (match-end 0) pos)
2471 (point))))
2472
2473 ;; Handle the "operator +" syntax in C++.
2474 (when (and c-overloadable-operators-regexp
2475 (= (c-backward-token-2 0) 0))
2476
2477 (cond ((and (looking-at c-overloadable-operators-regexp)
2478 (or (not c-opt-op-identifier-prefix)
2479 (and (= (c-backward-token-2 1) 0)
2480 (looking-at c-opt-op-identifier-prefix))))
2481 (point))
2482
2483 ((save-excursion
2484 (and c-opt-op-identifier-prefix
2485 (looking-at c-opt-op-identifier-prefix)
2486 (= (c-forward-token-2 1) 0)
2487 (looking-at c-overloadable-operators-regexp)))
2488 (point))))
2489
2490 )))
2491
2492 (defsubst c-simple-skip-symbol-backward ()
2493 ;; If the point is at the end of a symbol then skip backward to the
2494 ;; beginning of it. Don't move otherwise. Return non-nil if point
2495 ;; moved.
2496 ;;
2497 ;; This function might do hidden buffer changes.
2498 (or (< (skip-syntax-backward "w_") 0)
2499 (and (c-major-mode-is 'pike-mode)
2500 ;; Handle the `<operator> syntax in Pike.
2501 (let ((pos (point)))
2502 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2503 (< (skip-chars-backward "`") 0)
2504 (looking-at c-symbol-key)
2505 (>= (match-end 0) pos))
2506 t
2507 (goto-char pos)
2508 nil)))))
2509
2510 (defun c-beginning-of-current-token (&optional back-limit)
2511 ;; Move to the beginning of the current token. Do not move if not
2512 ;; in the middle of one. BACK-LIMIT may be used to bound the
2513 ;; backward search; if given it's assumed to be at the boundary
2514 ;; between two tokens. Return non-nil if the point is move, nil
2515 ;; otherwise.
2516 ;;
2517 ;; This function might do hidden buffer changes.
2518 (let ((start (point)))
2519 (if (looking-at "\\w\\|\\s_")
2520 (skip-syntax-backward "w_" back-limit)
2521 (when (< (skip-syntax-backward ".()" back-limit) 0)
2522 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2523 (match-end 0))
2524 ;; `c-nonsymbol-token-regexp' should always match
2525 ;; since we've skipped backward over punctuator
2526 ;; or paren syntax, but consume one char in case
2527 ;; it doesn't so that we don't leave point before
2528 ;; some earlier incorrect token.
2529 (1+ (point)))))
2530 (if (<= pos start)
2531 (goto-char pos))))))
2532 (< (point) start)))
2533
2534 (defun c-end-of-current-token (&optional back-limit)
2535 ;; Move to the end of the current token. Do not move if not in the
2536 ;; middle of one. BACK-LIMIT may be used to bound the backward
2537 ;; search; if given it's assumed to be at the boundary between two
2538 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2539 ;;
2540 ;; This function might do hidden buffer changes.
2541 (let ((start (point)))
2542 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2543 (skip-syntax-forward "w_"))
2544 ((< (skip-syntax-backward ".()" back-limit) 0)
2545 (while (progn
2546 (if (looking-at c-nonsymbol-token-regexp)
2547 (goto-char (match-end 0))
2548 ;; `c-nonsymbol-token-regexp' should always match since
2549 ;; we've skipped backward over punctuator or paren
2550 ;; syntax, but move forward in case it doesn't so that
2551 ;; we don't leave point earlier than we started with.
2552 (forward-char))
2553 (< (point) start)))))
2554 (> (point) start)))
2555
2556 (defconst c-jump-syntax-balanced
2557 (if (memq 'gen-string-delim c-emacs-features)
2558 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2559 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2560
2561 (defconst c-jump-syntax-unbalanced
2562 (if (memq 'gen-string-delim c-emacs-features)
2563 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2564 "\\w\\|\\s_\\|\\s\""))
2565
2566 (defun c-forward-token-2 (&optional count balanced limit)
2567 "Move forward by tokens.
2568 A token is defined as all symbols and identifiers which aren't
2569 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2570 treated properly). Point is always either left at the beginning of a
2571 token or not moved at all. COUNT specifies the number of tokens to
2572 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2573 moves to the next token beginning only if not already at one. If
2574 BALANCED is true, move over balanced parens, otherwise move into them.
2575 Also, if BALANCED is true, never move out of an enclosing paren.
2576
2577 LIMIT sets the limit for the movement and defaults to the point limit.
2578 The case when LIMIT is set in the middle of a token, comment or macro
2579 is handled correctly, i.e. the point won't be left there.
2580
2581 Return the number of tokens left to move \(positive or negative). If
2582 BALANCED is true, a move over a balanced paren counts as one. Note
2583 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2584 be returned. Thus, a return value of 0 guarantees that point is at
2585 the requested position and a return value less \(without signs) than
2586 COUNT guarantees that point is at the beginning of some token.
2587
2588 Note that this function might do hidden buffer changes. See the
2589 comment at the start of cc-engine.el for more info."
2590
2591 (or count (setq count 1))
2592 (if (< count 0)
2593 (- (c-backward-token-2 (- count) balanced limit))
2594
2595 (let ((jump-syntax (if balanced
2596 c-jump-syntax-balanced
2597 c-jump-syntax-unbalanced))
2598 (last (point))
2599 (prev (point)))
2600
2601 (if (zerop count)
2602 ;; If count is zero we should jump if in the middle of a token.
2603 (c-end-of-current-token))
2604
2605 (save-restriction
2606 (if limit (narrow-to-region (point-min) limit))
2607 (if (/= (point)
2608 (progn (c-forward-syntactic-ws) (point)))
2609 ;; Skip whitespace. Count this as a move if we did in
2610 ;; fact move.
2611 (setq count (max (1- count) 0)))
2612
2613 (if (eobp)
2614 ;; Moved out of bounds. Make sure the returned count isn't zero.
2615 (progn
2616 (if (zerop count) (setq count 1))
2617 (goto-char last))
2618
2619 ;; Use `condition-case' to avoid having the limit tests
2620 ;; inside the loop.
2621 (condition-case nil
2622 (while (and
2623 (> count 0)
2624 (progn
2625 (setq last (point))
2626 (cond ((looking-at jump-syntax)
2627 (goto-char (scan-sexps (point) 1))
2628 t)
2629 ((looking-at c-nonsymbol-token-regexp)
2630 (goto-char (match-end 0))
2631 t)
2632 ;; `c-nonsymbol-token-regexp' above should always
2633 ;; match if there are correct tokens. Try to
2634 ;; widen to see if the limit was set in the
2635 ;; middle of one, else fall back to treating
2636 ;; the offending thing as a one character token.
2637 ((and limit
2638 (save-restriction
2639 (widen)
2640 (looking-at c-nonsymbol-token-regexp)))
2641 nil)
2642 (t
2643 (forward-char)
2644 t))))
2645 (c-forward-syntactic-ws)
2646 (setq prev last
2647 count (1- count)))
2648 (error (goto-char last)))
2649
2650 (when (eobp)
2651 (goto-char prev)
2652 (setq count (1+ count)))))
2653
2654 count)))
2655
2656 (defun c-backward-token-2 (&optional count balanced limit)
2657 "Move backward by tokens.
2658 See `c-forward-token-2' for details."
2659
2660 (or count (setq count 1))
2661 (if (< count 0)
2662 (- (c-forward-token-2 (- count) balanced limit))
2663
2664 (or limit (setq limit (point-min)))
2665 (let ((jump-syntax (if balanced
2666 c-jump-syntax-balanced
2667 c-jump-syntax-unbalanced))
2668 (last (point)))
2669
2670 (if (zerop count)
2671 ;; The count is zero so try to skip to the beginning of the
2672 ;; current token.
2673 (if (> (point)
2674 (progn (c-beginning-of-current-token) (point)))
2675 (if (< (point) limit)
2676 ;; The limit is inside the same token, so return 1.
2677 (setq count 1))
2678
2679 ;; We're not in the middle of a token. If there's
2680 ;; whitespace after the point then we must move backward,
2681 ;; so set count to 1 in that case.
2682 (and (looking-at c-syntactic-ws-start)
2683 ;; If we're looking at a '#' that might start a cpp
2684 ;; directive then we have to do a more elaborate check.
2685 (or (/= (char-after) ?#)
2686 (not c-opt-cpp-prefix)
2687 (save-excursion
2688 (and (= (point)
2689 (progn (beginning-of-line)
2690 (looking-at "[ \t]*")
2691 (match-end 0)))
2692 (or (bobp)
2693 (progn (backward-char)
2694 (not (eq (char-before) ?\\)))))))
2695 (setq count 1))))
2696
2697 ;; Use `condition-case' to avoid having to check for buffer
2698 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2699 (condition-case nil
2700 (while (and
2701 (> count 0)
2702 (progn
2703 (c-backward-syntactic-ws)
2704 (backward-char)
2705 (if (looking-at jump-syntax)
2706 (goto-char (scan-sexps (1+ (point)) -1))
2707 ;; This can be very inefficient if there's a long
2708 ;; sequence of operator tokens without any separation.
2709 ;; That doesn't happen in practice, anyway.
2710 (c-beginning-of-current-token))
2711 (>= (point) limit)))
2712 (setq last (point)
2713 count (1- count)))
2714 (error (goto-char last)))
2715
2716 (if (< (point) limit)
2717 (goto-char last))
2718
2719 count)))
2720
2721 (defun c-forward-token-1 (&optional count balanced limit)
2722 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2723 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2724 characters are jumped over character by character. This function is
2725 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2726 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2727 (c-forward-token-2 count balanced limit)))
2728
2729 (defun c-backward-token-1 (&optional count balanced limit)
2730 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2731 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2732 characters are jumped over character by character. This function is
2733 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2734 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2735 (c-backward-token-2 count balanced limit)))
2736
2737 \f
2738 ;; Tools for doing searches restricted to syntactically relevant text.
2739
2740 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2741 paren-level not-inside-token
2742 lookbehind-submatch)
2743 "Like `re-search-forward', but only report matches that are found
2744 in syntactically significant text. I.e. matches in comments, macros
2745 or string literals are ignored. The start point is assumed to be
2746 outside any comment, macro or string literal, or else the content of
2747 that region is taken as syntactically significant text.
2748
2749 If PAREN-LEVEL is non-nil, an additional restriction is added to
2750 ignore matches in nested paren sexps. The search will also not go
2751 outside the current list sexp, which has the effect that if the point
2752 should be moved to BOUND when no match is found \(i.e. NOERROR is
2753 neither nil nor t), then it will be at the closing paren if the end of
2754 the current list sexp is encountered first.
2755
2756 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2757 ignored. Things like multicharacter operators and special symbols
2758 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2759 constants.
2760
2761 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2762 subexpression in REGEXP. The end of that submatch is used as the
2763 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2764 isn't used or if that subexpression didn't match then the start
2765 position of the whole match is used instead. The \"look behind\"
2766 subexpression is never tested before the starting position, so it
2767 might be a good idea to include \\=\\= as a match alternative in it.
2768
2769 Optimization note: Matches might be missed if the \"look behind\"
2770 subexpression can match the end of nonwhite syntactic whitespace,
2771 i.e. the end of comments or cpp directives. This since the function
2772 skips over such things before resuming the search. It's on the other
2773 hand not safe to assume that the \"look behind\" subexpression never
2774 matches syntactic whitespace.
2775
2776 Bug: Unbalanced parens inside cpp directives are currently not handled
2777 correctly \(i.e. they don't get ignored as they should) when
2778 PAREN-LEVEL is set.
2779
2780 Note that this function might do hidden buffer changes. See the
2781 comment at the start of cc-engine.el for more info."
2782
2783 (or bound (setq bound (point-max)))
2784 (if paren-level (setq paren-level -1))
2785
2786 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2787
2788 (let ((start (point))
2789 tmp
2790 ;; Start position for the last search.
2791 search-pos
2792 ;; The `parse-partial-sexp' state between the start position
2793 ;; and the point.
2794 state
2795 ;; The current position after the last state update. The next
2796 ;; `parse-partial-sexp' continues from here.
2797 (state-pos (point))
2798 ;; The position at which to check the state and the state
2799 ;; there. This is separate from `state-pos' since we might
2800 ;; need to back up before doing the next search round.
2801 check-pos check-state
2802 ;; Last position known to end a token.
2803 (last-token-end-pos (point-min))
2804 ;; Set when a valid match is found.
2805 found)
2806
2807 (condition-case err
2808 (while
2809 (and
2810 (progn
2811 (setq search-pos (point))
2812 (re-search-forward regexp bound noerror))
2813
2814 (progn
2815 (setq state (parse-partial-sexp
2816 state-pos (match-beginning 0) paren-level nil state)
2817 state-pos (point))
2818 (if (setq check-pos (and lookbehind-submatch
2819 (or (not paren-level)
2820 (>= (car state) 0))
2821 (match-end lookbehind-submatch)))
2822 (setq check-state (parse-partial-sexp
2823 state-pos check-pos paren-level nil state))
2824 (setq check-pos state-pos
2825 check-state state))
2826
2827 ;; NOTE: If we got a look behind subexpression and get
2828 ;; an insignificant match in something that isn't
2829 ;; syntactic whitespace (i.e. strings or in nested
2830 ;; parentheses), then we can never skip more than a
2831 ;; single character from the match start position
2832 ;; (i.e. `state-pos' here) before continuing the
2833 ;; search. That since the look behind subexpression
2834 ;; might match the end of the insignificant region in
2835 ;; the next search.
2836
2837 (cond
2838 ((elt check-state 7)
2839 ;; Match inside a line comment. Skip to eol. Use
2840 ;; `re-search-forward' instead of `skip-chars-forward' to get
2841 ;; the right bound behavior.
2842 (re-search-forward "[\n\r]" bound noerror))
2843
2844 ((elt check-state 4)
2845 ;; Match inside a block comment. Skip to the '*/'.
2846 (search-forward "*/" bound noerror))
2847
2848 ((and (not (elt check-state 5))
2849 (eq (char-before check-pos) ?/)
2850 (not (c-get-char-property (1- check-pos) 'syntax-table))
2851 (memq (char-after check-pos) '(?/ ?*)))
2852 ;; Match in the middle of the opener of a block or line
2853 ;; comment.
2854 (if (= (char-after check-pos) ?/)
2855 (re-search-forward "[\n\r]" bound noerror)
2856 (search-forward "*/" bound noerror)))
2857
2858 ;; The last `parse-partial-sexp' above might have
2859 ;; stopped short of the real check position if the end
2860 ;; of the current sexp was encountered in paren-level
2861 ;; mode. The checks above are always false in that
2862 ;; case, and since they can do better skipping in
2863 ;; lookbehind-submatch mode, we do them before
2864 ;; checking the paren level.
2865
2866 ((and paren-level
2867 (/= (setq tmp (car check-state)) 0))
2868 ;; Check the paren level first since we're short of the
2869 ;; syntactic checking position if the end of the
2870 ;; current sexp was encountered by `parse-partial-sexp'.
2871 (if (> tmp 0)
2872
2873 ;; Inside a nested paren sexp.
2874 (if lookbehind-submatch
2875 ;; See the NOTE above.
2876 (progn (goto-char state-pos) t)
2877 ;; Skip out of the paren quickly.
2878 (setq state (parse-partial-sexp state-pos bound 0 nil state)
2879 state-pos (point)))
2880
2881 ;; Have exited the current paren sexp.
2882 (if noerror
2883 (progn
2884 ;; The last `parse-partial-sexp' call above
2885 ;; has left us just after the closing paren
2886 ;; in this case, so we can modify the bound
2887 ;; to leave the point at the right position
2888 ;; upon return.
2889 (setq bound (1- (point)))
2890 nil)
2891 (signal 'search-failed (list regexp)))))
2892
2893 ((setq tmp (elt check-state 3))
2894 ;; Match inside a string.
2895 (if (or lookbehind-submatch
2896 (not (integerp tmp)))
2897 ;; See the NOTE above.
2898 (progn (goto-char state-pos) t)
2899 ;; Skip to the end of the string before continuing.
2900 (let ((ender (make-string 1 tmp)) (continue t))
2901 (while (if (search-forward ender bound noerror)
2902 (progn
2903 (setq state (parse-partial-sexp
2904 state-pos (point) nil nil state)
2905 state-pos (point))
2906 (elt state 3))
2907 (setq continue nil)))
2908 continue)))
2909
2910 ((save-excursion
2911 (save-match-data
2912 (c-beginning-of-macro start)))
2913 ;; Match inside a macro. Skip to the end of it.
2914 (c-end-of-macro)
2915 (cond ((<= (point) bound) t)
2916 (noerror nil)
2917 (t (signal 'search-failed (list regexp)))))
2918
2919 ((and not-inside-token
2920 (or (< check-pos last-token-end-pos)
2921 (< check-pos
2922 (save-excursion
2923 (goto-char check-pos)
2924 (save-match-data
2925 (c-end-of-current-token last-token-end-pos))
2926 (setq last-token-end-pos (point))))))
2927 ;; Inside a token.
2928 (if lookbehind-submatch
2929 ;; See the NOTE above.
2930 (goto-char state-pos)
2931 (goto-char (min last-token-end-pos bound))))
2932
2933 (t
2934 ;; A real match.
2935 (setq found t)
2936 nil)))
2937
2938 ;; Should loop to search again, but take care to avoid
2939 ;; looping on the same spot.
2940 (or (/= search-pos (point))
2941 (if (= (point) bound)
2942 (if noerror
2943 nil
2944 (signal 'search-failed (list regexp)))
2945 (forward-char)
2946 t))))
2947
2948 (error
2949 (goto-char start)
2950 (signal (car err) (cdr err))))
2951
2952 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2953
2954 (if found
2955 (progn
2956 (goto-char (match-end 0))
2957 (match-end 0))
2958
2959 ;; Search failed. Set point as appropriate.
2960 (if (eq noerror t)
2961 (goto-char start)
2962 (goto-char bound))
2963 nil)))
2964
2965 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
2966 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2967 i.e. don't stop at positions inside syntactic whitespace or string
2968 literals. Preprocessor directives are also ignored, with the exception
2969 of the one that the point starts within, if any. If LIMIT is given,
2970 it's assumed to be at a syntactically relevant position.
2971
2972 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
2973 sexps, and the search will also not go outside the current paren sexp.
2974 However, if LIMIT or the buffer limit is reached inside a nested paren
2975 then the point will be left at the limit.
2976
2977 Non-nil is returned if the point moved, nil otherwise.
2978
2979 Note that this function might do hidden buffer changes. See the
2980 comment at the start of cc-engine.el for more info."
2981
2982 (let ((start (point))
2983 state
2984 ;; A list of syntactically relevant positions in descending
2985 ;; order. It's used to avoid scanning repeatedly over
2986 ;; potentially large regions with `parse-partial-sexp' to verify
2987 ;; each position.
2988 safe-pos-list
2989 ;; The position at the beginning of `safe-pos-list'.
2990 safe-pos
2991 ;; The result from `c-beginning-of-macro' at the start position or the
2992 ;; start position itself if it isn't within a macro. Evaluated on
2993 ;; demand.
2994 start-macro-beg
2995 ;; The earliest position after the current one with the same paren
2996 ;; level. Used only when `paren-level' is set.
2997 (paren-level-pos (point)))
2998
2999 (while (progn
3000 (while (and
3001 (< (skip-chars-backward skip-chars limit) 0)
3002
3003 ;; Use `parse-partial-sexp' from a safe position down to
3004 ;; the point to check if it's outside comments and
3005 ;; strings.
3006 (let ((pos (point)) state-2 pps-end-pos)
3007 ;; Pick a safe position as close to the point as
3008 ;; possible.
3009 ;;
3010 ;; FIXME: Consult `syntax-ppss' here if our
3011 ;; cache doesn't give a good position.
3012 (while (and safe-pos-list
3013 (> (car safe-pos-list) (point)))
3014 (setq safe-pos-list (cdr safe-pos-list)))
3015 (unless (setq safe-pos (car-safe safe-pos-list))
3016 (setq safe-pos (max (or (c-safe-position
3017 (point) (or c-state-cache
3018 (c-parse-state)))
3019 0)
3020 (point-min))
3021 safe-pos-list (list safe-pos)))
3022
3023 ;; Cache positions along the way to use if we have to
3024 ;; back up more. We cache every closing paren on the
3025 ;; same level. If the paren cache is relevant in this
3026 ;; region then we're typically already on the same
3027 ;; level as the target position. Note that we might
3028 ;; cache positions after opening parens in case
3029 ;; safe-pos is in a nested list. That's both uncommon
3030 ;; and harmless.
3031 (while (progn
3032 (setq state (parse-partial-sexp
3033 safe-pos pos 0))
3034 (< (point) pos))
3035 (setq safe-pos (point)
3036 safe-pos-list (cons safe-pos safe-pos-list)))
3037
3038 (cond
3039 ((or (elt state 3) (elt state 4))
3040 ;; Inside string or comment. Continue search at the
3041 ;; beginning of it.
3042 (goto-char (elt state 8))
3043 t)
3044
3045 ((and paren-level
3046 (save-excursion
3047 (setq state-2 (parse-partial-sexp
3048 pos paren-level-pos -1)
3049 pps-end-pos (point))
3050 (/= (car state-2) 0)))
3051 ;; Not at the right level.
3052
3053 (if (and (< (car state-2) 0)
3054 ;; We stop above if we go out of a paren.
3055 ;; Now check whether it precedes or is
3056 ;; nested in the starting sexp.
3057 (save-excursion
3058 (setq state-2
3059 (parse-partial-sexp
3060 pps-end-pos paren-level-pos
3061 nil nil state-2))
3062 (< (car state-2) 0)))
3063
3064 ;; We've stopped short of the starting position
3065 ;; so the hit was inside a nested list. Go up
3066 ;; until we are at the right level.
3067 (condition-case nil
3068 (progn
3069 (goto-char (scan-lists pos -1
3070 (- (car state-2))))
3071 (setq paren-level-pos (point))
3072 (if (and limit (>= limit paren-level-pos))
3073 (progn
3074 (goto-char limit)
3075 nil)
3076 t))
3077 (error
3078 (goto-char (or limit (point-min)))
3079 nil))
3080
3081 ;; The hit was outside the list at the start
3082 ;; position. Go to the start of the list and exit.
3083 (goto-char (1+ (elt state-2 1)))
3084 nil))
3085
3086 ((c-beginning-of-macro limit)
3087 ;; Inside a macro.
3088 (if (< (point)
3089 (or start-macro-beg
3090 (setq start-macro-beg
3091 (save-excursion
3092 (goto-char start)
3093 (c-beginning-of-macro limit)
3094 (point)))))
3095 t
3096
3097 ;; It's inside the same macro we started in so it's
3098 ;; a relevant match.
3099 (goto-char pos)
3100 nil)))))
3101
3102 ;; If the state contains the start of the containing sexp we
3103 ;; cache that position too, so that parse-partial-sexp in the
3104 ;; next run has a bigger chance of starting at the same level
3105 ;; as the target position and thus will get more good safe
3106 ;; positions into the list.
3107 (if (elt state 1)
3108 (setq safe-pos (1+ (elt state 1))
3109 safe-pos-list (cons safe-pos safe-pos-list))))
3110
3111 (> (point)
3112 (progn
3113 ;; Skip syntactic ws afterwards so that we don't stop at the
3114 ;; end of a comment if `skip-chars' is something like "^/".
3115 (c-backward-syntactic-ws)
3116 (point)))))
3117
3118 ;; We might want to extend this with more useful return values in
3119 ;; the future.
3120 (/= (point) start)))
3121
3122 ;; The following is an alternative implementation of
3123 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3124 ;; track of the syntactic context. It turned out to be generally
3125 ;; slower than the one above which uses forward checks from earlier
3126 ;; safe positions.
3127 ;;
3128 ;;(defconst c-ssb-stop-re
3129 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3130 ;; ;; stop at to avoid going into comments and literals.
3131 ;; (concat
3132 ;; ;; Match comment end syntax and string literal syntax. Also match
3133 ;; ;; '/' for block comment endings (not covered by comment end
3134 ;; ;; syntax).
3135 ;; "\\s>\\|/\\|\\s\""
3136 ;; (if (memq 'gen-string-delim c-emacs-features)
3137 ;; "\\|\\s|"
3138 ;; "")
3139 ;; (if (memq 'gen-comment-delim c-emacs-features)
3140 ;; "\\|\\s!"
3141 ;; "")))
3142 ;;
3143 ;;(defconst c-ssb-stop-paren-re
3144 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3145 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3146 ;;
3147 ;;(defconst c-ssb-sexp-end-re
3148 ;; ;; Regexp matching the ending syntax of a complex sexp.
3149 ;; (concat c-string-limit-regexp "\\|\\s)"))
3150 ;;
3151 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3152 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3153 ;;i.e. don't stop at positions inside syntactic whitespace or string
3154 ;;literals. Preprocessor directives are also ignored. However, if the
3155 ;;point is within a comment, string literal or preprocessor directory to
3156 ;;begin with, its contents is treated as syntactically relevant chars.
3157 ;;If LIMIT is given, it limits the backward search and the point will be
3158 ;;left there if no earlier position is found.
3159 ;;
3160 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3161 ;;sexps, and the search will also not go outside the current paren sexp.
3162 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3163 ;;then the point will be left at the limit.
3164 ;;
3165 ;;Non-nil is returned if the point moved, nil otherwise.
3166 ;;
3167 ;;Note that this function might do hidden buffer changes. See the
3168 ;;comment at the start of cc-engine.el for more info."
3169 ;;
3170 ;; (save-restriction
3171 ;; (when limit
3172 ;; (narrow-to-region limit (point-max)))
3173 ;;
3174 ;; (let ((start (point)))
3175 ;; (catch 'done
3176 ;; (while (let ((last-pos (point))
3177 ;; (stop-pos (progn
3178 ;; (skip-chars-backward skip-chars)
3179 ;; (point))))
3180 ;;
3181 ;; ;; Skip back over the same region as
3182 ;; ;; `skip-chars-backward' above, but keep to
3183 ;; ;; syntactically relevant positions.
3184 ;; (goto-char last-pos)
3185 ;; (while (and
3186 ;; ;; `re-search-backward' with a single char regexp
3187 ;; ;; should be fast.
3188 ;; (re-search-backward
3189 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3190 ;; stop-pos 'move)
3191 ;;
3192 ;; (progn
3193 ;; (cond
3194 ;; ((looking-at "\\s(")
3195 ;; ;; `paren-level' is set and we've found the
3196 ;; ;; start of the containing paren.
3197 ;; (forward-char)
3198 ;; (throw 'done t))
3199 ;;
3200 ;; ((looking-at c-ssb-sexp-end-re)
3201 ;; ;; We're at the end of a string literal or paren
3202 ;; ;; sexp (if `paren-level' is set).
3203 ;; (forward-char)
3204 ;; (condition-case nil
3205 ;; (c-backward-sexp)
3206 ;; (error
3207 ;; (goto-char limit)
3208 ;; (throw 'done t))))
3209 ;;
3210 ;; (t
3211 ;; (forward-char)
3212 ;; ;; At the end of some syntactic ws or possibly
3213 ;; ;; after a plain '/' operator.
3214 ;; (let ((pos (point)))
3215 ;; (c-backward-syntactic-ws)
3216 ;; (if (= pos (point))
3217 ;; ;; Was a plain '/' operator. Go past it.
3218 ;; (backward-char)))))
3219 ;;
3220 ;; (> (point) stop-pos))))
3221 ;;
3222 ;; ;; Now the point is either at `stop-pos' or at some
3223 ;; ;; position further back if `stop-pos' was at a
3224 ;; ;; syntactically irrelevant place.
3225 ;;
3226 ;; ;; Skip additional syntactic ws so that we don't stop
3227 ;; ;; at the end of a comment if `skip-chars' is
3228 ;; ;; something like "^/".
3229 ;; (c-backward-syntactic-ws)
3230 ;;
3231 ;; (< (point) stop-pos))))
3232 ;;
3233 ;; ;; We might want to extend this with more useful return values
3234 ;; ;; in the future.
3235 ;; (/= (point) start))))
3236
3237 \f
3238 ;; Tools for handling comments and string literals.
3239
3240 (defun c-slow-in-literal (&optional lim detect-cpp)
3241 "Return the type of literal point is in, if any.
3242 The return value is `c' if in a C-style comment, `c++' if in a C++
3243 style comment, `string' if in a string literal, `pound' if DETECT-CPP
3244 is non-nil and in a preprocessor line, or nil if somewhere else.
3245 Optional LIM is used as the backward limit of the search. If omitted,
3246 or nil, `c-beginning-of-defun' is used.
3247
3248 The last point calculated is cached if the cache is enabled, i.e. if
3249 `c-in-literal-cache' is bound to a two element vector.
3250
3251 Note that this function might do hidden buffer changes. See the
3252 comment at the start of cc-engine.el for more info."
3253
3254 (if (and (vectorp c-in-literal-cache)
3255 (= (point) (aref c-in-literal-cache 0)))
3256 (aref c-in-literal-cache 1)
3257 (let ((rtn (save-excursion
3258 (let* ((pos (point))
3259 (lim (or lim (progn
3260 (c-beginning-of-syntax)
3261 (point))))
3262 (state (parse-partial-sexp lim pos)))
3263 (cond
3264 ((elt state 3) 'string)
3265 ((elt state 4) (if (elt state 7) 'c++ 'c))
3266 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
3267 (t nil))))))
3268 ;; cache this result if the cache is enabled
3269 (if (not c-in-literal-cache)
3270 (setq c-in-literal-cache (vector (point) rtn)))
3271 rtn)))
3272
3273 ;; XEmacs has a built-in function that should make this much quicker.
3274 ;; I don't think we even need the cache, which makes our lives more
3275 ;; complicated anyway. In this case, lim is only used to detect
3276 ;; cpp directives.
3277 ;;
3278 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
3279 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
3280 ;; Xemacs 21.4.4. It manifested itself thus:
3281 ;;
3282 ;; Starting with an empty AWK Mode buffer, type
3283 ;; /regexp/ {<C-j>
3284 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
3285 ;;
3286 ;; AWK Mode is designed such that when the first / is typed, it gets the
3287 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
3288 ;; are given the s-t property "string". However, buffer-syntactic-context
3289 ;; fails to take account of the change of the s-t property on the opening / to
3290 ;; "string", and reports that the { is within a string started by the second /.
3291 ;;
3292 ;; The workaround for this is for the AWK Mode initialisation to switch the
3293 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
3294 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
3295 ;;
3296 ;; (Alan Mackenzie, 2003/4/30).
3297
3298 (defun c-fast-in-literal (&optional lim detect-cpp)
3299 ;; This function might do hidden buffer changes.
3300 (let ((context (buffer-syntactic-context)))
3301 (cond
3302 ((eq context 'string) 'string)
3303 ((eq context 'comment) 'c++)
3304 ((eq context 'block-comment) 'c)
3305 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
3306
3307 (defalias 'c-in-literal
3308 (if (fboundp 'buffer-syntactic-context)
3309 'c-fast-in-literal ; XEmacs
3310 'c-slow-in-literal)) ; GNU Emacs
3311
3312 ;; The defalias above isn't enough to shut up the byte compiler.
3313 (cc-bytecomp-defun c-in-literal)
3314
3315 (defun c-literal-limits (&optional lim near not-in-delimiter)
3316 "Return a cons of the beginning and end positions of the comment or
3317 string surrounding point (including both delimiters), or nil if point
3318 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
3319 to start parsing from. If NEAR is non-nil, then the limits of any
3320 literal next to point is returned. \"Next to\" means there's only
3321 spaces and tabs between point and the literal. The search for such a
3322 literal is done first in forward direction. If NOT-IN-DELIMITER is
3323 non-nil, the case when point is inside a starting delimiter won't be
3324 recognized. This only has effect for comments, which have starting
3325 delimiters with more than one character.
3326
3327 Note that this function might do hidden buffer changes. See the
3328 comment at the start of cc-engine.el for more info."
3329
3330 (save-excursion
3331 (let* ((pos (point))
3332 (lim (or lim (progn
3333 (c-beginning-of-syntax)
3334 (point))))
3335 (state (parse-partial-sexp lim pos)))
3336
3337 (cond ((elt state 3) ; String.
3338 (goto-char (elt state 8))
3339 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3340 (point-max))))
3341
3342 ((elt state 4) ; Comment.
3343 (goto-char (elt state 8))
3344 (cons (point) (progn (c-forward-single-comment) (point))))
3345
3346 ((and (not not-in-delimiter)
3347 (not (elt state 5))
3348 (eq (char-before) ?/)
3349 (looking-at "[/*]"))
3350 ;; We're standing in a comment starter.
3351 (backward-char 1)
3352 (cons (point) (progn (c-forward-single-comment) (point))))
3353
3354 (near
3355 (goto-char pos)
3356
3357 ;; Search forward for a literal.
3358 (skip-chars-forward " \t")
3359
3360 (cond
3361 ((looking-at c-string-limit-regexp) ; String.
3362 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
3363 (point-max))))
3364
3365 ((looking-at c-comment-start-regexp) ; Line or block comment.
3366 (cons (point) (progn (c-forward-single-comment) (point))))
3367
3368 (t
3369 ;; Search backward.
3370 (skip-chars-backward " \t")
3371
3372 (let ((end (point)) beg)
3373 (cond
3374 ((save-excursion
3375 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
3376 (setq beg (c-safe (c-backward-sexp 1) (point))))
3377
3378 ((and (c-safe (forward-char -2) t)
3379 (looking-at "*/"))
3380 ;; Block comment. Due to the nature of line
3381 ;; comments, they will always be covered by the
3382 ;; normal case above.
3383 (goto-char end)
3384 (c-backward-single-comment)
3385 ;; If LIM is bogus, beg will be bogus.
3386 (setq beg (point))))
3387
3388 (if beg (cons beg end))))))
3389 ))))
3390
3391 ;; In case external callers use this; it did have a docstring.
3392 (defalias 'c-literal-limits-fast 'c-literal-limits)
3393
3394 (defun c-collect-line-comments (range)
3395 "If the argument is a cons of two buffer positions (such as returned by
3396 `c-literal-limits'), and that range contains a C++ style line comment,
3397 then an extended range is returned that contains all adjacent line
3398 comments (i.e. all comments that starts in the same column with no
3399 empty lines or non-whitespace characters between them). Otherwise the
3400 argument is returned.
3401
3402 Note that this function might do hidden buffer changes. See the
3403 comment at the start of cc-engine.el for more info."
3404
3405 (save-excursion
3406 (condition-case nil
3407 (if (and (consp range) (progn
3408 (goto-char (car range))
3409 (looking-at c-line-comment-starter)))
3410 (let ((col (current-column))
3411 (beg (point))
3412 (bopl (c-point 'bopl))
3413 (end (cdr range)))
3414 ;; Got to take care in the backward direction to handle
3415 ;; comments which are preceded by code.
3416 (while (and (c-backward-single-comment)
3417 (>= (point) bopl)
3418 (looking-at c-line-comment-starter)
3419 (= col (current-column)))
3420 (setq beg (point)
3421 bopl (c-point 'bopl)))
3422 (goto-char end)
3423 (while (and (progn (skip-chars-forward " \t")
3424 (looking-at c-line-comment-starter))
3425 (= col (current-column))
3426 (prog1 (zerop (forward-line 1))
3427 (setq end (point)))))
3428 (cons beg end))
3429 range)
3430 (error range))))
3431
3432 (defun c-literal-type (range)
3433 "Convenience function that given the result of `c-literal-limits',
3434 returns nil or the type of literal that the range surrounds. It's
3435 much faster than using `c-in-literal' and is intended to be used when
3436 you need both the type of a literal and its limits.
3437
3438 Note that this function might do hidden buffer changes. See the
3439 comment at the start of cc-engine.el for more info."
3440
3441 (if (consp range)
3442 (save-excursion
3443 (goto-char (car range))
3444 (cond ((looking-at c-string-limit-regexp) 'string)
3445 ((or (looking-at "//") ; c++ line comment
3446 (and (looking-at "\\s<") ; comment starter
3447 (looking-at "#"))) ; awk comment.
3448 'c++)
3449 (t 'c))) ; Assuming the range is valid.
3450 range))
3451
3452 \f
3453 ;; `c-find-decl-spots' and accompanying stuff.
3454
3455 ;; Variables used in `c-find-decl-spots' to cache the search done for
3456 ;; the first declaration in the last call. When that function starts,
3457 ;; it needs to back up over syntactic whitespace to look at the last
3458 ;; token before the region being searched. That can sometimes cause
3459 ;; moves back and forth over a quite large region of comments and
3460 ;; macros, which would be repeated for each changed character when
3461 ;; we're called during fontification, since font-lock refontifies the
3462 ;; current line for each change. Thus it's worthwhile to cache the
3463 ;; first match.
3464 ;;
3465 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3466 ;; the syntactic whitespace less or equal to some start position.
3467 ;; There's no cached value if it's nil.
3468 ;;
3469 ;; `c-find-decl-match-pos' is the match position if
3470 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3471 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3472 (defvar c-find-decl-syntactic-pos nil)
3473 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
3474 (defvar c-find-decl-match-pos nil)
3475 (make-variable-buffer-local 'c-find-decl-match-pos)
3476
3477 (defsubst c-invalidate-find-decl-cache (change-min-pos)
3478 (and c-find-decl-syntactic-pos
3479 (< change-min-pos c-find-decl-syntactic-pos)
3480 (setq c-find-decl-syntactic-pos nil)))
3481
3482 ; (defface c-debug-decl-spot-face
3483 ; '((t (:background "Turquoise")))
3484 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3485 ; (defface c-debug-decl-sws-face
3486 ; '((t (:background "Khaki")))
3487 ; "Debug face to mark the syntactic whitespace between the declaration
3488 ; spots and the preceding token end.")
3489
3490 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
3491 (when (facep 'c-debug-decl-spot-face)
3492 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
3493 (c-debug-add-face (max match-pos (point-min)) decl-pos
3494 'c-debug-decl-sws-face)
3495 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
3496 'c-debug-decl-spot-face))))
3497 (defmacro c-debug-remove-decl-spot-faces (beg end)
3498 (when (facep 'c-debug-decl-spot-face)
3499 `(c-save-buffer-state ()
3500 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
3501 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
3502
3503 (defmacro c-find-decl-prefix-search ()
3504 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3505 ;; but it contains lots of free variables that refer to things
3506 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3507 ;; if there is a match, otherwise at `cfd-limit'.
3508 ;;
3509 ;; This macro might do hidden buffer changes.
3510
3511 '(progn
3512 ;; Find the next property match position if we haven't got one already.
3513 (unless cfd-prop-match
3514 (save-excursion
3515 (while (progn
3516 (goto-char (next-single-property-change
3517 (point) 'c-type nil cfd-limit))
3518 (and (< (point) cfd-limit)
3519 (not (eq (c-get-char-property (1- (point)) 'c-type)
3520 'c-decl-end)))))
3521 (setq cfd-prop-match (point))))
3522
3523 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
3524 ;; got one already.
3525 (unless cfd-re-match
3526
3527 (if (> cfd-re-match-end (point))
3528 (goto-char cfd-re-match-end))
3529
3530 (while (if (setq cfd-re-match-end
3531 (re-search-forward c-decl-prefix-or-start-re
3532 cfd-limit 'move))
3533
3534 ;; Match. Check if it's inside a comment or string literal.
3535 (c-got-face-at
3536 (if (setq cfd-re-match (match-end 1))
3537 ;; Matched the end of a token preceding a decl spot.
3538 (progn
3539 (goto-char cfd-re-match)
3540 (1- cfd-re-match))
3541 ;; Matched a token that start a decl spot.
3542 (goto-char (match-beginning 0))
3543 (point))
3544 c-literal-faces)
3545
3546 ;; No match. Finish up and exit the loop.
3547 (setq cfd-re-match cfd-limit)
3548 nil)
3549
3550 ;; Skip out of comments and string literals.
3551 (while (progn
3552 (goto-char (next-single-property-change
3553 (point) 'face nil cfd-limit))
3554 (and (< (point) cfd-limit)
3555 (c-got-face-at (point) c-literal-faces)))))
3556
3557 ;; If we matched at the decl start, we have to back up over the
3558 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
3559 ;; any decl spots in the syntactic ws.
3560 (unless cfd-re-match
3561 (c-backward-syntactic-ws)
3562 (setq cfd-re-match (point))))
3563
3564 ;; Choose whichever match is closer to the start.
3565 (if (< cfd-re-match cfd-prop-match)
3566 (setq cfd-match-pos cfd-re-match
3567 cfd-re-match nil)
3568 (setq cfd-match-pos cfd-prop-match
3569 cfd-prop-match nil))
3570
3571 (goto-char cfd-match-pos)
3572
3573 (when (< cfd-match-pos cfd-limit)
3574 ;; Skip forward past comments only so we don't skip macros.
3575 (c-forward-comments)
3576 ;; Set the position to continue at. We can avoid going over
3577 ;; the comments skipped above a second time, but it's possible
3578 ;; that the comment skipping has taken us past `cfd-prop-match'
3579 ;; since the property might be used inside comments.
3580 (setq cfd-continue-pos (if cfd-prop-match
3581 (min cfd-prop-match (point))
3582 (point))))))
3583
3584 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3585 ;; Call CFD-FUN for each possible spot for a declaration, cast or
3586 ;; label from the point to CFD-LIMIT. Such a spot is:
3587 ;;
3588 ;; o The first token after bob.
3589 ;; o The first token after the end of submatch 1 in
3590 ;; `c-decl-prefix-or-start-re' when that submatch matches.
3591 ;; o The start of each `c-decl-prefix-or-start-re' match when
3592 ;; submatch 1 doesn't match.
3593 ;; o The first token after the end of each occurence of the
3594 ;; `c-type' text property with the value `c-decl-end', provided
3595 ;; `c-type-decl-end-used' is set.
3596 ;;
3597 ;; Only a spot that match CFD-DECL-RE and whose face is in the
3598 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
3599 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
3600 ;;
3601 ;; If the match is inside a macro then the buffer is narrowed to the
3602 ;; end of it, so that CFD-FUN can investigate the following tokens
3603 ;; without matching something that begins inside a macro and ends
3604 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3605 ;; CFD-FACE-CHECKLIST checks exist.
3606 ;;
3607 ;; CFD-FUN is called with point at the start of the spot. It's
3608 ;; passed two arguments: The first is the end position of the token
3609 ;; preceding the spot, or 0 for the implicit match at bob. The
3610 ;; second is a flag that is t when the match is inside a macro. If
3611 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
3612 ;; spot, it should return non-nil to ensure that the next search
3613 ;; will find them.
3614 ;;
3615 ;; The spots are visited approximately in order from top to bottom.
3616 ;; It's however the positions where `c-decl-prefix-or-start-re'
3617 ;; matches and where `c-decl-end' properties are found that are in
3618 ;; order. Since the spots often are at the following token, they
3619 ;; might be visited out of order insofar as more spots are reported
3620 ;; later on within the syntactic whitespace between the match
3621 ;; positions and their spots.
3622 ;;
3623 ;; It's assumed that comments and strings are fontified in the
3624 ;; searched range.
3625 ;;
3626 ;; This is mainly used in fontification, and so has an elaborate
3627 ;; cache to handle repeated calls from the same start position; see
3628 ;; the variables above.
3629 ;;
3630 ;; All variables in this function begin with `cfd-' to avoid name
3631 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3632 ;;
3633 ;; This function might do hidden buffer changes.
3634
3635 (let ((cfd-start-pos (point))
3636 (cfd-buffer-end (point-max))
3637 ;; The end of the token preceding the decl spot last found
3638 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
3639 ;; no match.
3640 cfd-re-match
3641 ;; The end position of the last `c-decl-prefix-or-start-re'
3642 ;; match. If this is greater than `cfd-continue-pos', the
3643 ;; next regexp search is started here instead.
3644 (cfd-re-match-end (point-min))
3645 ;; The end of the last `c-decl-end' found by
3646 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
3647 ;; match. If searching for the property isn't needed then we
3648 ;; disable it by setting it to `cfd-limit' directly.
3649 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3650 ;; The end of the token preceding the decl spot last found by
3651 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
3652 ;; bob. `cfd-limit' if there's no match. In other words,
3653 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
3654 (cfd-match-pos cfd-limit)
3655 ;; The position to continue searching at.
3656 cfd-continue-pos
3657 ;; The position of the last "real" token we've stopped at.
3658 ;; This can be greater than `cfd-continue-pos' when we get
3659 ;; hits inside macros or at `c-decl-end' positions inside
3660 ;; comments.
3661 (cfd-token-pos 0)
3662 ;; The end position of the last entered macro.
3663 (cfd-macro-end 0))
3664
3665 ;; Initialize by finding a syntactically relevant start position
3666 ;; before the point, and do the first `c-decl-prefix-or-start-re'
3667 ;; search unless we're at bob.
3668
3669 (let (start-in-literal start-in-macro syntactic-pos)
3670 ;; Must back up a bit since we look for the end of the previous
3671 ;; statement or declaration, which is earlier than the first
3672 ;; returned match.
3673
3674 (cond
3675 ;; First we need to move to a syntactically relevant position.
3676 ;; Begin by backing out of comment or string literals.
3677 ((and
3678 (when (c-got-face-at (point) c-literal-faces)
3679 ;; Try to use the faces to back up to the start of the
3680 ;; literal. FIXME: What if the point is on a declaration
3681 ;; inside a comment?
3682 (while (and (not (bobp))
3683 (c-got-face-at (1- (point)) c-literal-faces))
3684 (goto-char (previous-single-property-change
3685 (point) 'face nil (point-min))))
3686
3687 ;; XEmacs doesn't fontify the quotes surrounding string
3688 ;; literals.
3689 (and (featurep 'xemacs)
3690 (eq (get-text-property (point) 'face)
3691 'font-lock-string-face)
3692 (not (bobp))
3693 (progn (backward-char)
3694 (not (looking-at c-string-limit-regexp)))
3695 (forward-char))
3696
3697 ;; Don't trust the literal to contain only literal faces
3698 ;; (the font lock package might not have fontified the
3699 ;; start of it at all, for instance) so check that we have
3700 ;; arrived at something that looks like a start or else
3701 ;; resort to `c-literal-limits'.
3702 (unless (looking-at c-literal-start-regexp)
3703 (let ((range (c-literal-limits)))
3704 (if range (goto-char (car range)))))
3705
3706 (setq start-in-literal (point)))
3707
3708 ;; The start is in a literal. If the limit is in the same
3709 ;; one we don't have to find a syntactic position etc. We
3710 ;; only check that if the limit is at or before bonl to save
3711 ;; time; it covers the by far most common case when font-lock
3712 ;; refontifies the current line only.
3713 (<= cfd-limit (c-point 'bonl cfd-start-pos))
3714 (save-excursion
3715 (goto-char cfd-start-pos)
3716 (while (progn
3717 (goto-char (next-single-property-change
3718 (point) 'face nil cfd-limit))
3719 (and (< (point) cfd-limit)
3720 (c-got-face-at (point) c-literal-faces))))
3721 (= (point) cfd-limit)))
3722
3723 ;; Completely inside a literal. Set up variables to trig the
3724 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
3725 ;; find a suitable start position.
3726 (setq cfd-continue-pos start-in-literal))
3727
3728 ;; Check if the region might be completely inside a macro, to
3729 ;; optimize that like the completely-inside-literal above.
3730 ((save-excursion
3731 (and (= (forward-line 1) 0)
3732 (bolp) ; forward-line has funny behavior at eob.
3733 (>= (point) cfd-limit)
3734 (progn (backward-char)
3735 (eq (char-before) ?\\))))
3736 ;; (Maybe) completely inside a macro. Only need to trig the
3737 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
3738 ;; set things up.
3739 (setq cfd-continue-pos (1- cfd-start-pos)
3740 start-in-macro t))
3741
3742 (t
3743 ;; Back out of any macro so we don't miss any declaration
3744 ;; that could follow after it.
3745 (when (c-beginning-of-macro)
3746 (setq start-in-macro t))
3747
3748 ;; Now we're at a proper syntactically relevant position so we
3749 ;; can use the cache. But first clear it if it applied
3750 ;; further down.
3751 (c-invalidate-find-decl-cache cfd-start-pos)
3752
3753 (setq syntactic-pos (point))
3754 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
3755 ;; Don't have to do this if the cache is relevant here,
3756 ;; typically if the same line is refontified again. If
3757 ;; we're just some syntactic whitespace further down we can
3758 ;; still use the cache to limit the skipping.
3759 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
3760
3761 ;; If we hit `c-find-decl-syntactic-pos' and
3762 ;; `c-find-decl-match-pos' is set then we install the cached
3763 ;; values. If we hit `c-find-decl-syntactic-pos' and
3764 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3765 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3766 ;; and so we can continue the search from this point. If we
3767 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
3768 ;; the right spot to begin searching anyway.
3769 (if (and (eq (point) c-find-decl-syntactic-pos)
3770 c-find-decl-match-pos)
3771 (setq cfd-match-pos c-find-decl-match-pos
3772 cfd-continue-pos syntactic-pos)
3773
3774 (setq c-find-decl-syntactic-pos syntactic-pos)
3775
3776 (when (if (bobp)
3777 ;; Always consider bob a match to get the first
3778 ;; declaration in the file. Do this separately instead of
3779 ;; letting `c-decl-prefix-or-start-re' match bob, so that
3780 ;; regexp always can consume at least one character to
3781 ;; ensure that we won't get stuck in an infinite loop.
3782 (setq cfd-re-match 0)
3783 (backward-char)
3784 (c-beginning-of-current-token)
3785 (< (point) cfd-limit))
3786 ;; Do an initial search now. In the bob case above it's
3787 ;; only done to search for a `c-decl-end' spot.
3788 (c-find-decl-prefix-search))
3789
3790 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
3791 cfd-match-pos)))))
3792
3793 ;; Advance `cfd-continue-pos' if it's before the start position.
3794 ;; The closest continue position that might have effect at or
3795 ;; after the start depends on what we started in. This also
3796 ;; finds a suitable start position in the special cases when the
3797 ;; region is completely within a literal or macro.
3798 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
3799
3800 (cond
3801 (start-in-macro
3802 ;; If we're in a macro then it's the closest preceding token
3803 ;; in the macro. Check this before `start-in-literal',
3804 ;; since if we're inside a literal in a macro, the preceding
3805 ;; token is earlier than any `c-decl-end' spot inside the
3806 ;; literal (comment).
3807 (goto-char (or start-in-literal cfd-start-pos))
3808 ;; The only syntactic ws in macros are comments.
3809 (c-backward-comments)
3810 (backward-char)
3811 (c-beginning-of-current-token))
3812
3813 (start-in-literal
3814 ;; If we're in a comment it can only be the closest
3815 ;; preceding `c-decl-end' position within that comment, if
3816 ;; any. Go back to the beginning of such a property so that
3817 ;; `c-find-decl-prefix-search' will find the end of it.
3818 ;; (Can't stop at the end and install it directly on
3819 ;; `cfd-prop-match' since that variable might be cleared
3820 ;; after `cfd-fun' below.)
3821 ;;
3822 ;; Note that if the literal is a string then the property
3823 ;; search will simply skip to the beginning of it right
3824 ;; away.
3825 (if (not c-type-decl-end-used)
3826 (goto-char start-in-literal)
3827 (goto-char cfd-start-pos)
3828 (while (progn
3829 (goto-char (previous-single-property-change
3830 (point) 'c-type nil start-in-literal))
3831 (and (> (point) start-in-literal)
3832 (not (eq (c-get-char-property (point) 'c-type)
3833 'c-decl-end))))))
3834
3835 (when (= (point) start-in-literal)
3836 ;; Didn't find any property inside the comment, so we can
3837 ;; skip it entirely. (This won't skip past a string, but
3838 ;; that'll be handled quickly by the next
3839 ;; `c-find-decl-prefix-search' anyway.)
3840 (c-forward-single-comment)
3841 (if (> (point) cfd-limit)
3842 (goto-char cfd-limit))))
3843
3844 (t
3845 ;; If we started in normal code, the only match that might
3846 ;; apply before the start is what we already got in
3847 ;; `cfd-match-pos' so we can continue at the start position.
3848 ;; (Note that we don't get here if the first match is below
3849 ;; it.)
3850 (goto-char cfd-start-pos)))
3851
3852 ;; Delete found matches if they are before our new continue
3853 ;; position, so that `c-find-decl-prefix-search' won't back up
3854 ;; to them later on.
3855 (setq cfd-continue-pos (point))
3856 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
3857 (setq cfd-re-match nil))
3858 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
3859 (setq cfd-prop-match nil)))
3860
3861 (if syntactic-pos
3862 ;; This is the normal case and we got a proper syntactic
3863 ;; position. If there's a match then it's always outside
3864 ;; macros and comments, so advance to the next token and set
3865 ;; `cfd-token-pos'. The loop below will later go back using
3866 ;; `cfd-continue-pos' to fix declarations inside the
3867 ;; syntactic ws.
3868 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
3869 (goto-char syntactic-pos)
3870 (c-forward-syntactic-ws)
3871 (and cfd-continue-pos
3872 (< cfd-continue-pos (point))
3873 (setq cfd-token-pos (point))))
3874
3875 ;; Have one of the special cases when the region is completely
3876 ;; within a literal or macro. `cfd-continue-pos' is set to a
3877 ;; good start position for the search, so do it.
3878 (c-find-decl-prefix-search)))
3879
3880 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
3881
3882 (while (progn
3883 (while (and
3884 (< cfd-match-pos cfd-limit)
3885
3886 (or
3887 ;; Kludge to filter out matches on the "<" that
3888 ;; aren't open parens, for the sake of languages
3889 ;; that got `c-recognize-<>-arglists' set.
3890 (and (eq (char-before cfd-match-pos) ?<)
3891 (not (c-get-char-property (1- cfd-match-pos)
3892 'syntax-table)))
3893
3894 ;; If `cfd-continue-pos' is less or equal to
3895 ;; `cfd-token-pos', we've got a hit inside a macro
3896 ;; that's in the syntactic whitespace before the last
3897 ;; "real" declaration we've checked. If they're equal
3898 ;; we've arrived at the declaration a second time, so
3899 ;; there's nothing to do.
3900 (= cfd-continue-pos cfd-token-pos)
3901
3902 (progn
3903 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3904 ;; we're still searching for declarations embedded in
3905 ;; the syntactic whitespace. In that case we need
3906 ;; only to skip comments and not macros, since they
3907 ;; can't be nested, and that's already been done in
3908 ;; `c-find-decl-prefix-search'.
3909 (when (> cfd-continue-pos cfd-token-pos)
3910 (c-forward-syntactic-ws)
3911 (setq cfd-token-pos (point)))
3912
3913 ;; Continue if the following token fails the
3914 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3915 (when (or (>= (point) cfd-limit)
3916 (not (looking-at cfd-decl-re))
3917 (and cfd-face-checklist
3918 (not (c-got-face-at
3919 (point) cfd-face-checklist))))
3920 (goto-char cfd-continue-pos)
3921 t)))
3922
3923 (< (point) cfd-limit))
3924 (c-find-decl-prefix-search))
3925
3926 (< (point) cfd-limit))
3927
3928 (when (and
3929 (>= (point) cfd-start-pos)
3930
3931 (progn
3932 ;; Narrow to the end of the macro if we got a hit inside
3933 ;; one, to avoid recognizing things that start inside the
3934 ;; macro and end outside it.
3935 (when (> cfd-match-pos cfd-macro-end)
3936 ;; Not in the same macro as in the previous round.
3937 (save-excursion
3938 (goto-char cfd-match-pos)
3939 (setq cfd-macro-end
3940 (if (save-excursion (and (c-beginning-of-macro)
3941 (< (point) cfd-match-pos)))
3942 (progn (c-end-of-macro)
3943 (point))
3944 0))))
3945
3946 (if (zerop cfd-macro-end)
3947 t
3948 (if (> cfd-macro-end (point))
3949 (progn (narrow-to-region (point-min) cfd-macro-end)
3950 t)
3951 ;; The matched token was the last thing in the macro,
3952 ;; so the whole match is bogus.
3953 (setq cfd-macro-end 0)
3954 nil))))
3955
3956 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3957 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3958 (setq cfd-prop-match nil))
3959
3960 (when (/= cfd-macro-end 0)
3961 ;; Restore limits if we did macro narrowment above.
3962 (narrow-to-region (point-min) cfd-buffer-end)))
3963
3964 (goto-char cfd-continue-pos)
3965 (if (= cfd-continue-pos cfd-limit)
3966 (setq cfd-match-pos cfd-limit)
3967 (c-find-decl-prefix-search)))))
3968
3969 \f
3970 ;; A cache for found types.
3971
3972 ;; Buffer local variable that contains an obarray with the types we've
3973 ;; found. If a declaration is recognized somewhere we record the
3974 ;; fully qualified identifier in it to recognize it as a type
3975 ;; elsewhere in the file too. This is not accurate since we do not
3976 ;; bother with the scoping rules of the languages, but in practice the
3977 ;; same name is seldom used as both a type and something else in a
3978 ;; file, and we only use this as a last resort in ambiguous cases (see
3979 ;; `c-forward-decl-or-cast-1').
3980 ;;
3981 ;; Not every type need be in this cache. However, things which have
3982 ;; ceased to be types must be removed from it.
3983 ;;
3984 ;; Template types in C++ are added here too but with the template
3985 ;; arglist replaced with "<>" in references or "<" for the one in the
3986 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
3987 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
3988 ;; template specs can be fairly sized programs in themselves) and
3989 ;; improves the hit ratio (it's a type regardless of the template
3990 ;; args; it's just not the same type, but we're only interested in
3991 ;; recognizing types, not telling distinct types apart). Note that
3992 ;; template types in references are added here too; from the example
3993 ;; above there will also be an entry "Foo<".
3994 (defvar c-found-types nil)
3995 (make-variable-buffer-local 'c-found-types)
3996
3997 (defsubst c-clear-found-types ()
3998 ;; Clears `c-found-types'.
3999 (setq c-found-types (make-vector 53 0)))
4000
4001 (defun c-add-type (from to)
4002 ;; Add the given region as a type in `c-found-types'. If the region
4003 ;; doesn't match an existing type but there is a type which is equal
4004 ;; to the given one except that the last character is missing, then
4005 ;; the shorter type is removed. That's done to avoid adding all
4006 ;; prefixes of a type as it's being entered and font locked. This
4007 ;; doesn't cover cases like when characters are removed from a type
4008 ;; or added in the middle. We'd need the position of point when the
4009 ;; font locking is invoked to solve this well.
4010 ;;
4011 ;; This function might do hidden buffer changes.
4012 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4013 (unless (intern-soft type c-found-types)
4014 (unintern (substring type 0 -1) c-found-types)
4015 (intern type c-found-types))))
4016
4017 (defun c-unfind-type (name)
4018 ;; Remove the "NAME" from c-found-types, if present.
4019 (unintern name c-found-types))
4020
4021 (defsubst c-check-type (from to)
4022 ;; Return non-nil if the given region contains a type in
4023 ;; `c-found-types'.
4024 ;;
4025 ;; This function might do hidden buffer changes.
4026 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4027 c-found-types))
4028
4029 (defun c-list-found-types ()
4030 ;; Return all the types in `c-found-types' as a sorted list of
4031 ;; strings.
4032 (let (type-list)
4033 (mapatoms (lambda (type)
4034 (setq type-list (cons (symbol-name type)
4035 type-list)))
4036 c-found-types)
4037 (sort type-list 'string-lessp)))
4038
4039 (defun c-trim-found-types (beg end old-len)
4040 ;; An after change function which, in conjunction with the info in
4041 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4042 ;; from `c-found-types', should this type have become stale. For
4043 ;; example, this happens to "foo" when "foo \n bar();" becomes
4044 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4045 ;; the fontification.
4046 ;;
4047 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4048 ;; type?
4049 (when (> end beg)
4050 (save-excursion
4051 (when (< end (point-max))
4052 (goto-char end)
4053 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4054 (progn (goto-char end)
4055 (c-end-of-current-token)))
4056 (c-unfind-type (buffer-substring-no-properties
4057 end (point)))))
4058 (when (> beg (point-min))
4059 (goto-char beg)
4060 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4061 (progn (goto-char beg)
4062 (c-beginning-of-current-token)))
4063 (c-unfind-type (buffer-substring-no-properties
4064 (point) beg))))))
4065
4066 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4067 (cond
4068 ;; Changing the amount of (already existing) whitespace - don't do anything.
4069 ((and (c-partial-ws-p beg end)
4070 (or (= beg end) ; removal of WS
4071 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4072
4073 ;; The syntactic relationship which defined a "found type" has been
4074 ;; destroyed.
4075 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4076 (c-unfind-type (cadr c-maybe-stale-found-type)))
4077 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4078 )))
4079
4080 \f
4081 ;; Handling of small scale constructs like types and names.
4082
4083 (defun c-after-change-check-<>-operators (beg end)
4084 ;; This is called from `after-change-functions' when
4085 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
4086 ;; chars with paren syntax become part of another operator like "<<"
4087 ;; or ">=".
4088 ;;
4089 ;; This function might do hidden buffer changes.
4090
4091 (save-excursion
4092 (goto-char beg)
4093 (when (or (looking-at "[<>]")
4094 (< (skip-chars-backward "<>") 0))
4095
4096 (goto-char beg)
4097 (c-beginning-of-current-token)
4098 (when (and (< (point) beg)
4099 (looking-at c-<>-multichar-token-regexp)
4100 (< beg (setq beg (match-end 0))))
4101 (while (progn (skip-chars-forward "^<>" beg)
4102 (< (point) beg))
4103 (c-clear-char-property (point) 'syntax-table)
4104 (forward-char))))
4105
4106 (when (< beg end)
4107 (goto-char end)
4108 (when (or (looking-at "[<>]")
4109 (< (skip-chars-backward "<>") 0))
4110
4111 (goto-char end)
4112 (c-beginning-of-current-token)
4113 (when (and (< (point) end)
4114 (looking-at c-<>-multichar-token-regexp)
4115 (< end (setq end (match-end 0))))
4116 (while (progn (skip-chars-forward "^<>" end)
4117 (< (point) end))
4118 (c-clear-char-property (point) 'syntax-table)
4119 (forward-char)))))))
4120
4121 ;; Dynamically bound variable that instructs `c-forward-type' to also
4122 ;; treat possible types (i.e. those that it normally returns 'maybe or
4123 ;; 'found for) as actual types (and always return 'found for them).
4124 ;; This means that it records them in `c-record-type-identifiers' if
4125 ;; that is set, and that it adds them to `c-found-types'.
4126 (defvar c-promote-possible-types nil)
4127
4128 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4129 ;; mark up successfully parsed arglists with paren syntax properties on
4130 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
4131 ;; `c-type' property of each argument separating comma.
4132 ;;
4133 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
4134 ;; all arglists for side effects (i.e. recording types), otherwise it
4135 ;; exploits any existing paren syntax properties to quickly jump to the
4136 ;; end of already parsed arglists.
4137 ;;
4138 ;; Marking up the arglists is not the default since doing that correctly
4139 ;; depends on a proper value for `c-restricted-<>-arglists'.
4140 (defvar c-parse-and-markup-<>-arglists nil)
4141
4142 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
4143 ;; not accept arglists that contain binary operators.
4144 ;;
4145 ;; This is primarily used to handle C++ template arglists. C++
4146 ;; disambiguates them by checking whether the preceding name is a
4147 ;; template or not. We can't do that, so we assume it is a template
4148 ;; if it can be parsed as one. That usually works well since
4149 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
4150 ;; in almost all cases would be pointless.
4151 ;;
4152 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
4153 ;; should let the comma separate the function arguments instead. And
4154 ;; in a context where the value of the expression is taken, e.g. in
4155 ;; "if (a < b || c > d)", it's probably not a template.
4156 (defvar c-restricted-<>-arglists nil)
4157
4158 ;; Dynamically bound variables that instructs
4159 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
4160 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
4161 ;; `c-forward-label' to record the ranges of all the type and
4162 ;; reference identifiers they encounter. They will build lists on
4163 ;; these variables where each element is a cons of the buffer
4164 ;; positions surrounding each identifier. This recording is only
4165 ;; activated when `c-record-type-identifiers' is non-nil.
4166 ;;
4167 ;; All known types that can't be identifiers are recorded, and also
4168 ;; other possible types if `c-promote-possible-types' is set.
4169 ;; Recording is however disabled inside angle bracket arglists that
4170 ;; are encountered inside names and other angle bracket arglists.
4171 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
4172 ;; instead.
4173 ;;
4174 ;; Only the names in C++ template style references (e.g. "tmpl" in
4175 ;; "tmpl<a,b>::foo") are recorded as references, other references
4176 ;; aren't handled here.
4177 ;;
4178 ;; `c-forward-label' records the label identifier(s) on
4179 ;; `c-record-ref-identifiers'.
4180 (defvar c-record-type-identifiers nil)
4181 (defvar c-record-ref-identifiers nil)
4182
4183 ;; This variable will receive a cons cell of the range of the last
4184 ;; single identifier symbol stepped over by `c-forward-name' if it's
4185 ;; successful. This is the range that should be put on one of the
4186 ;; record lists above by the caller. It's assigned nil if there's no
4187 ;; such symbol in the name.
4188 (defvar c-last-identifier-range nil)
4189
4190 (defmacro c-record-type-id (range)
4191 (if (eq (car-safe range) 'cons)
4192 ;; Always true.
4193 `(setq c-record-type-identifiers
4194 (cons ,range c-record-type-identifiers))
4195 `(let ((range ,range))
4196 (if range
4197 (setq c-record-type-identifiers
4198 (cons range c-record-type-identifiers))))))
4199
4200 (defmacro c-record-ref-id (range)
4201 (if (eq (car-safe range) 'cons)
4202 ;; Always true.
4203 `(setq c-record-ref-identifiers
4204 (cons ,range c-record-ref-identifiers))
4205 `(let ((range ,range))
4206 (if range
4207 (setq c-record-ref-identifiers
4208 (cons range c-record-ref-identifiers))))))
4209
4210 ;; Dynamically bound variable that instructs `c-forward-type' to
4211 ;; record the ranges of types that only are found. Behaves otherwise
4212 ;; like `c-record-type-identifiers'.
4213 (defvar c-record-found-types nil)
4214
4215 (defmacro c-forward-keyword-prefixed-id (type)
4216 ;; Used internally in `c-forward-keyword-clause' to move forward
4217 ;; over a type (if TYPE is 'type) or a name (otherwise) which
4218 ;; possibly is prefixed by keywords and their associated clauses.
4219 ;; Try with a type/name first to not trip up on those that begin
4220 ;; with a keyword. Return t if a known or found type is moved
4221 ;; over. The point is clobbered if nil is returned. If range
4222 ;; recording is enabled, the identifier is recorded on as a type
4223 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
4224 ;;
4225 ;; This macro might do hidden buffer changes.
4226 `(let (res)
4227 (while (if (setq res ,(if (eq type 'type)
4228 `(c-forward-type)
4229 `(c-forward-name)))
4230 nil
4231 (and (looking-at c-keywords-regexp)
4232 (c-forward-keyword-clause 1))))
4233 (when (memq res '(t known found prefix))
4234 ,(when (eq type 'ref)
4235 `(when c-record-type-identifiers
4236 (c-record-ref-id c-last-identifier-range)))
4237 t)))
4238
4239 (defmacro c-forward-id-comma-list (type update-safe-pos)
4240 ;; Used internally in `c-forward-keyword-clause' to move forward
4241 ;; over a comma separated list of types or names using
4242 ;; `c-forward-keyword-prefixed-id'.
4243 ;;
4244 ;; This macro might do hidden buffer changes.
4245 `(while (and (progn
4246 ,(when update-safe-pos
4247 `(setq safe-pos (point)))
4248 (eq (char-after) ?,))
4249 (progn
4250 (forward-char)
4251 (c-forward-syntactic-ws)
4252 (c-forward-keyword-prefixed-id ,type)))))
4253
4254 (defun c-forward-keyword-clause (match)
4255 ;; Submatch MATCH in the current match data is assumed to surround a
4256 ;; token. If it's a keyword, move over it and any immediately
4257 ;; following clauses associated with it, stopping at the start of
4258 ;; the next token. t is returned in that case, otherwise the point
4259 ;; stays and nil is returned. The kind of clauses that are
4260 ;; recognized are those specified by `c-type-list-kwds',
4261 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
4262 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
4263 ;; and `c-<>-arglist-kwds'.
4264 ;;
4265 ;; This function records identifier ranges on
4266 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4267 ;; `c-record-type-identifiers' is non-nil.
4268 ;;
4269 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
4270 ;; apply directly after the keyword, the type list is moved over
4271 ;; only when there is no unaccounted token before it (i.e. a token
4272 ;; that isn't moved over due to some other keyword list). The
4273 ;; identifier ranges in the list are still recorded if that should
4274 ;; be done, though.
4275 ;;
4276 ;; This function might do hidden buffer changes.
4277
4278 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
4279 ;; The call to `c-forward-<>-arglist' below is made after
4280 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
4281 ;; are angle bracket arglists and `c-restricted-<>-arglists'
4282 ;; should therefore be nil.
4283 (c-parse-and-markup-<>-arglists t)
4284 c-restricted-<>-arglists)
4285
4286 (when kwd-sym
4287 (goto-char (match-end match))
4288 (c-forward-syntactic-ws)
4289 (setq safe-pos (point))
4290
4291 (cond
4292 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
4293 (c-forward-keyword-prefixed-id type))
4294 ;; There's a type directly after a keyword in `c-type-list-kwds'.
4295 (c-forward-id-comma-list type t))
4296
4297 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
4298 (c-forward-keyword-prefixed-id ref))
4299 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
4300 (c-forward-id-comma-list ref t))
4301
4302 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
4303 (eq (char-after) ?\())
4304 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
4305
4306 (forward-char)
4307 (when (and (setq pos (c-up-list-forward))
4308 (eq (char-before pos) ?\)))
4309 (when (and c-record-type-identifiers
4310 (c-keyword-member kwd-sym 'c-paren-type-kwds))
4311 ;; Use `c-forward-type' on every identifier we can find
4312 ;; inside the paren, to record the types.
4313 (while (c-syntactic-re-search-forward c-symbol-start pos t)
4314 (goto-char (match-beginning 0))
4315 (unless (c-forward-type)
4316 (looking-at c-symbol-key) ; Always matches.
4317 (goto-char (match-end 0)))))
4318
4319 (goto-char pos)
4320 (c-forward-syntactic-ws)
4321 (setq safe-pos (point))))
4322
4323 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
4324 (eq (char-after) ?<)
4325 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
4326 (c-forward-syntactic-ws)
4327 (setq safe-pos (point)))
4328
4329 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
4330 (not (looking-at c-symbol-start))
4331 (c-safe (c-forward-sexp) t))
4332 (c-forward-syntactic-ws)
4333 (setq safe-pos (point))))
4334
4335 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
4336 (if (eq (char-after) ?:)
4337 ;; If we are at the colon already, we move over the type
4338 ;; list after it.
4339 (progn
4340 (forward-char)
4341 (c-forward-syntactic-ws)
4342 (when (c-forward-keyword-prefixed-id type)
4343 (c-forward-id-comma-list type t)))
4344 ;; Not at the colon, so stop here. But the identifier
4345 ;; ranges in the type list later on should still be
4346 ;; recorded.
4347 (and c-record-type-identifiers
4348 (progn
4349 ;; If a keyword matched both one of the types above and
4350 ;; this one, we match `c-colon-type-list-re' after the
4351 ;; clause matched above.
4352 (goto-char safe-pos)
4353 (looking-at c-colon-type-list-re))
4354 (progn
4355 (goto-char (match-end 0))
4356 (c-forward-syntactic-ws)
4357 (c-forward-keyword-prefixed-id type))
4358 ;; There's a type after the `c-colon-type-list-re' match
4359 ;; after a keyword in `c-colon-type-list-kwds'.
4360 (c-forward-id-comma-list type nil))))
4361
4362 (goto-char safe-pos)
4363 t)))
4364
4365 (defun c-forward-<>-arglist (all-types)
4366 ;; The point is assumed to be at a "<". Try to treat it as the open
4367 ;; paren of an angle bracket arglist and move forward to the the
4368 ;; corresponding ">". If successful, the point is left after the
4369 ;; ">" and t is returned, otherwise the point isn't moved and nil is
4370 ;; returned. If ALL-TYPES is t then all encountered arguments in
4371 ;; the arglist that might be types are treated as found types.
4372 ;;
4373 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
4374 ;; function handles text properties on the angle brackets and argument
4375 ;; separating commas.
4376 ;;
4377 ;; `c-restricted-<>-arglists' controls how lenient the template
4378 ;; arglist recognition should be.
4379 ;;
4380 ;; This function records identifier ranges on
4381 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4382 ;; `c-record-type-identifiers' is non-nil.
4383 ;;
4384 ;; This function might do hidden buffer changes.
4385
4386 (let ((start (point))
4387 ;; If `c-record-type-identifiers' is set then activate
4388 ;; recording of any found types that constitute an argument in
4389 ;; the arglist.
4390 (c-record-found-types (if c-record-type-identifiers t)))
4391 (if (catch 'angle-bracket-arglist-escape
4392 (setq c-record-found-types
4393 (c-forward-<>-arglist-recur all-types)))
4394 (progn
4395 (when (consp c-record-found-types)
4396 (setq c-record-type-identifiers
4397 ;; `nconc' doesn't mind that the tail of
4398 ;; `c-record-found-types' is t.
4399 (nconc c-record-found-types c-record-type-identifiers)))
4400 t)
4401
4402 (goto-char start)
4403 nil)))
4404
4405 (defun c-forward-<>-arglist-recur (all-types)
4406 ;; Recursive part of `c-forward-<>-arglist'.
4407 ;;
4408 ;; This function might do hidden buffer changes.
4409
4410 (let ((start (point)) res pos tmp
4411 ;; Cover this so that any recorded found type ranges are
4412 ;; automatically lost if it turns out to not be an angle
4413 ;; bracket arglist. It's propagated through the return value
4414 ;; on successful completion.
4415 (c-record-found-types c-record-found-types)
4416 ;; List that collects the positions after the argument
4417 ;; separating ',' in the arglist.
4418 arg-start-pos)
4419
4420 ;; If the '<' has paren open syntax then we've marked it as an angle
4421 ;; bracket arglist before, so skip to the end.
4422 (if (and (not c-parse-and-markup-<>-arglists)
4423 (c-get-char-property (point) 'syntax-table))
4424
4425 (progn
4426 (forward-char)
4427 (if (and (c-go-up-list-forward)
4428 (eq (char-before) ?>))
4429 t
4430
4431 ;; Got unmatched paren angle brackets. We don't clear the paren
4432 ;; syntax properties and retry, on the basis that it's very
4433 ;; unlikely that paren angle brackets become operators by code
4434 ;; manipulation. It's far more likely that it doesn't match due
4435 ;; to narrowing or some temporary change.
4436 (goto-char start)
4437 nil))
4438
4439 (forward-char)
4440 (unless (looking-at c-<-op-cont-regexp)
4441 (while (and
4442 (progn
4443
4444 (when c-record-type-identifiers
4445 (if all-types
4446
4447 ;; All encountered identifiers are types, so set the
4448 ;; promote flag and parse the type.
4449 (progn
4450 (c-forward-syntactic-ws)
4451 (when (looking-at c-identifier-start)
4452 (let ((c-promote-possible-types t))
4453 (c-forward-type))))
4454
4455 ;; Check if this arglist argument is a sole type. If
4456 ;; it's known then it's recorded in
4457 ;; `c-record-type-identifiers'. If it only is found
4458 ;; then it's recorded in `c-record-found-types' which we
4459 ;; might roll back if it turns out that this isn't an
4460 ;; angle bracket arglist afterall.
4461 (when (memq (char-before) '(?, ?<))
4462 (let ((orig-record-found-types c-record-found-types))
4463 (c-forward-syntactic-ws)
4464 (and (memq (c-forward-type) '(known found))
4465 (not (looking-at "[,>]"))
4466 ;; A found type was recorded but it's not the
4467 ;; only thing in the arglist argument, so reset
4468 ;; `c-record-found-types'.
4469 (setq c-record-found-types
4470 orig-record-found-types))))))
4471
4472 (setq pos (point))
4473 (or (when (eq (char-after) ?>)
4474 ;; Must check for '>' at the very start separately,
4475 ;; since the regexp below has to avoid ">>" without
4476 ;; using \\=.
4477 (forward-char)
4478 t)
4479
4480 ;; Note: These regexps exploit the match order in \| so
4481 ;; that "<>" is matched by "<" rather than "[^>:-]>".
4482 (c-syntactic-re-search-forward
4483 (if c-restricted-<>-arglists
4484 ;; Stop on ',', '|', '&', '+' and '-' to catch
4485 ;; common binary operators that could be between
4486 ;; two comparison expressions "a<b" and "c>d".
4487 "[<;{},|&+-]\\|\\([^>:-]>\\)"
4488 ;; Otherwise we still stop on ',' to find the
4489 ;; argument start positions.
4490 "[<;{},]\\|\\([^>:-]>\\)")
4491 nil 'move t t 1)
4492
4493 ;; If the arglist starter has lost its open paren
4494 ;; syntax but not the closer, we won't find the
4495 ;; closer above since we only search in the
4496 ;; balanced sexp. In that case we stop just short
4497 ;; of it so check if the following char is the closer.
4498 (when (eq (char-after) ?>)
4499 (forward-char)
4500 t)))
4501
4502 (cond
4503 ((eq (char-before) ?>)
4504 ;; Either an operator starting with '>' or the end of
4505 ;; the angle bracket arglist.
4506
4507 (if (looking-at c->-op-cont-regexp)
4508 (progn
4509 (goto-char (match-end 0))
4510 t) ; Continue the loop.
4511
4512 ;; The angle bracket arglist is finished.
4513 (when c-parse-and-markup-<>-arglists
4514 (while arg-start-pos
4515 (c-put-c-type-property (1- (car arg-start-pos))
4516 'c-<>-arg-sep)
4517 (setq arg-start-pos (cdr arg-start-pos)))
4518 (c-mark-<-as-paren start)
4519 (c-mark->-as-paren (1- (point))))
4520 (setq res t)
4521 nil)) ; Exit the loop.
4522
4523 ((eq (char-before) ?<)
4524 ;; Either an operator starting with '<' or a nested arglist.
4525
4526 (setq pos (point))
4527 (let (id-start id-end subres keyword-match)
4528 (if (if (looking-at c-<-op-cont-regexp)
4529 (setq tmp (match-end 0))
4530 (setq tmp pos)
4531 (backward-char)
4532 (not
4533 (and
4534
4535 (save-excursion
4536 ;; There's always an identifier before an angle
4537 ;; bracket arglist, or a keyword in
4538 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
4539 (c-backward-syntactic-ws)
4540 (setq id-end (point))
4541 (c-simple-skip-symbol-backward)
4542 (when (or (setq keyword-match
4543 (looking-at c-opt-<>-sexp-key))
4544 (not (looking-at c-keywords-regexp)))
4545 (setq id-start (point))))
4546
4547 (setq subres
4548 (let ((c-record-type-identifiers nil)
4549 (c-record-found-types nil))
4550 (c-forward-<>-arglist-recur
4551 (and keyword-match
4552 (c-keyword-member
4553 (c-keyword-sym (match-string 1))
4554 'c-<>-type-kwds)))))
4555 )))
4556
4557 ;; It was not an angle bracket arglist.
4558 (goto-char tmp)
4559
4560 ;; It was an angle bracket arglist.
4561 (setq c-record-found-types subres)
4562
4563 ;; Record the identifier before the template as a type
4564 ;; or reference depending on whether the arglist is last
4565 ;; in a qualified identifier.
4566 (when (and c-record-type-identifiers
4567 (not keyword-match))
4568 (if (and c-opt-identifier-concat-key
4569 (progn
4570 (c-forward-syntactic-ws)
4571 (looking-at c-opt-identifier-concat-key)))
4572 (c-record-ref-id (cons id-start id-end))
4573 (c-record-type-id (cons id-start id-end))))))
4574 t)
4575
4576 ((and (eq (char-before) ?,)
4577 (not c-restricted-<>-arglists))
4578 ;; Just another argument. Record the position. The
4579 ;; type check stuff that made us stop at it is at
4580 ;; the top of the loop.
4581 (setq arg-start-pos (cons (point) arg-start-pos)))
4582
4583 (t
4584 ;; Got a character that can't be in an angle bracket
4585 ;; arglist argument. Abort using `throw', since
4586 ;; it's useless to try to find a surrounding arglist
4587 ;; if we're nested.
4588 (throw 'angle-bracket-arglist-escape nil))))))
4589
4590 (if res
4591 (or c-record-found-types t)))))
4592
4593 (defun c-backward-<>-arglist (all-types &optional limit)
4594 ;; The point is assumed to be directly after a ">". Try to treat it
4595 ;; as the close paren of an angle bracket arglist and move back to
4596 ;; the corresponding "<". If successful, the point is left at
4597 ;; the "<" and t is returned, otherwise the point isn't moved and
4598 ;; nil is returned. ALL-TYPES is passed on to
4599 ;; `c-forward-<>-arglist'.
4600 ;;
4601 ;; If the optional LIMIT is given, it bounds the backward search.
4602 ;; It's then assumed to be at a syntactically relevant position.
4603 ;;
4604 ;; This is a wrapper around `c-forward-<>-arglist'. See that
4605 ;; function for more details.
4606
4607 (let ((start (point)))
4608 (backward-char)
4609 (if (and (not c-parse-and-markup-<>-arglists)
4610 (c-get-char-property (point) 'syntax-table))
4611
4612 (if (and (c-go-up-list-backward)
4613 (eq (char-after) ?<))
4614 t
4615 ;; See corresponding note in `c-forward-<>-arglist'.
4616 (goto-char start)
4617 nil)
4618
4619 (while (progn
4620 (c-syntactic-skip-backward "^<;{}" limit t)
4621
4622 (and
4623 (if (eq (char-before) ?<)
4624 t
4625 ;; Stopped at bob or a char that isn't allowed in an
4626 ;; arglist, so we've failed.
4627 (goto-char start)
4628 nil)
4629
4630 (if (> (point)
4631 (progn (c-beginning-of-current-token)
4632 (point)))
4633 ;; If we moved then the "<" was part of some
4634 ;; multicharacter token.
4635 t
4636
4637 (backward-char)
4638 (let ((beg-pos (point)))
4639 (if (c-forward-<>-arglist all-types)
4640 (cond ((= (point) start)
4641 ;; Matched the arglist. Break the while.
4642 (goto-char beg-pos)
4643 nil)
4644 ((> (point) start)
4645 ;; We started from a non-paren ">" inside an
4646 ;; arglist.
4647 (goto-char start)
4648 nil)
4649 (t
4650 ;; Matched a shorter arglist. Can be a nested
4651 ;; one so continue looking.
4652 (goto-char beg-pos)
4653 t))
4654 t))))))
4655
4656 (/= (point) start))))
4657
4658 (defun c-forward-name ()
4659 ;; Move forward over a complete name if at the beginning of one,
4660 ;; stopping at the next following token. If the point is not at
4661 ;; something that are recognized as name then it stays put. A name
4662 ;; could be something as simple as "foo" in C or something as
4663 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
4664 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
4665 ;; int>::*volatile const" in C++ (this function is actually little
4666 ;; more than a `looking-at' call in all modes except those that,
4667 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
4668 ;; name is found, 'template if it's an identifier ending with an
4669 ;; angle bracket arglist, 'operator of it's an operator identifier,
4670 ;; or t if it's some other kind of name.
4671 ;;
4672 ;; This function records identifier ranges on
4673 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4674 ;; `c-record-type-identifiers' is non-nil.
4675 ;;
4676 ;; This function might do hidden buffer changes.
4677
4678 (let ((pos (point)) (start (point)) res id-start id-end
4679 ;; Turn off `c-promote-possible-types' here since we might
4680 ;; call `c-forward-<>-arglist' and we don't want it to promote
4681 ;; every suspect thing in the arglist to a type. We're
4682 ;; typically called from `c-forward-type' in this case, and
4683 ;; the caller only wants the top level type that it finds to
4684 ;; be promoted.
4685 c-promote-possible-types)
4686 (while
4687 (and
4688 (looking-at c-identifier-key)
4689
4690 (progn
4691 ;; Check for keyword. We go to the last symbol in
4692 ;; `c-identifier-key' first.
4693 (goto-char (setq id-end (match-end 0)))
4694 (c-simple-skip-symbol-backward)
4695 (setq id-start (point))
4696
4697 (if (looking-at c-keywords-regexp)
4698 (when (and (c-major-mode-is 'c++-mode)
4699 (looking-at
4700 (cc-eval-when-compile
4701 (concat "\\(operator\\|\\(template\\)\\)"
4702 "\\(" (c-lang-const c-nonsymbol-key c++)
4703 "\\|$\\)")))
4704 (if (match-beginning 2)
4705 ;; "template" is only valid inside an
4706 ;; identifier if preceded by "::".
4707 (save-excursion
4708 (c-backward-syntactic-ws)
4709 (and (c-safe (backward-char 2) t)
4710 (looking-at "::")))
4711 t))
4712
4713 ;; Handle a C++ operator or template identifier.
4714 (goto-char id-end)
4715 (c-forward-syntactic-ws)
4716 (cond ((eq (char-before id-end) ?e)
4717 ;; Got "... ::template".
4718 (let ((subres (c-forward-name)))
4719 (when subres
4720 (setq pos (point)
4721 res subres))))
4722
4723 ((looking-at c-identifier-start)
4724 ;; Got a cast operator.
4725 (when (c-forward-type)
4726 (setq pos (point)
4727 res 'operator)
4728 ;; Now we should match a sequence of either
4729 ;; '*', '&' or a name followed by ":: *",
4730 ;; where each can be followed by a sequence
4731 ;; of `c-opt-type-modifier-key'.
4732 (while (cond ((looking-at "[*&]")
4733 (goto-char (match-end 0))
4734 t)
4735 ((looking-at c-identifier-start)
4736 (and (c-forward-name)
4737 (looking-at "::")
4738 (progn
4739 (goto-char (match-end 0))
4740 (c-forward-syntactic-ws)
4741 (eq (char-after) ?*))
4742 (progn
4743 (forward-char)
4744 t))))
4745 (while (progn
4746 (c-forward-syntactic-ws)
4747 (setq pos (point))
4748 (looking-at c-opt-type-modifier-key))
4749 (goto-char (match-end 1))))))
4750
4751 ((looking-at c-overloadable-operators-regexp)
4752 ;; Got some other operator.
4753 (setq c-last-identifier-range
4754 (cons (point) (match-end 0)))
4755 (goto-char (match-end 0))
4756 (c-forward-syntactic-ws)
4757 (setq pos (point)
4758 res 'operator)))
4759
4760 nil)
4761
4762 ;; `id-start' is equal to `id-end' if we've jumped over
4763 ;; an identifier that doesn't end with a symbol token.
4764 ;; That can occur e.g. for Java import directives on the
4765 ;; form "foo.bar.*".
4766 (when (and id-start (/= id-start id-end))
4767 (setq c-last-identifier-range
4768 (cons id-start id-end)))
4769 (goto-char id-end)
4770 (c-forward-syntactic-ws)
4771 (setq pos (point)
4772 res t)))
4773
4774 (progn
4775 (goto-char pos)
4776 (when (or c-opt-identifier-concat-key
4777 c-recognize-<>-arglists)
4778
4779 (cond
4780 ((and c-opt-identifier-concat-key
4781 (looking-at c-opt-identifier-concat-key))
4782 ;; Got a concatenated identifier. This handles the
4783 ;; cases with tricky syntactic whitespace that aren't
4784 ;; covered in `c-identifier-key'.
4785 (goto-char (match-end 0))
4786 (c-forward-syntactic-ws)
4787 t)
4788
4789 ((and c-recognize-<>-arglists
4790 (eq (char-after) ?<))
4791 ;; Maybe an angle bracket arglist.
4792
4793 (when (let (c-record-type-identifiers
4794 c-record-found-types)
4795 (c-forward-<>-arglist nil))
4796
4797 (c-add-type start (1+ pos))
4798 (c-forward-syntactic-ws)
4799 (setq pos (point)
4800 c-last-identifier-range nil)
4801
4802 (if (and c-opt-identifier-concat-key
4803 (looking-at c-opt-identifier-concat-key))
4804
4805 ;; Continue if there's an identifier concatenation
4806 ;; operator after the template argument.
4807 (progn
4808 (when (and c-record-type-identifiers id-start)
4809 (c-record-ref-id (cons id-start id-end)))
4810 (forward-char 2)
4811 (c-forward-syntactic-ws)
4812 t)
4813
4814 (when (and c-record-type-identifiers id-start)
4815 (c-record-type-id (cons id-start id-end)))
4816 (setq res 'template)
4817 nil)))
4818 )))))
4819
4820 (goto-char pos)
4821 res))
4822
4823 (defun c-forward-type ()
4824 ;; Move forward over a type spec if at the beginning of one,
4825 ;; stopping at the next following token. Return t if it's a known
4826 ;; type that can't be a name or other expression, 'known if it's an
4827 ;; otherwise known type (according to `*-font-lock-extra-types'),
4828 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4829 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4830 ;; that might be a type, or nil if it can't be a type (the point
4831 ;; isn't moved then). The point is assumed to be at the beginning
4832 ;; of a token.
4833 ;;
4834 ;; Note that this function doesn't skip past the brace definition
4835 ;; that might be considered part of the type, e.g.
4836 ;; "enum {a, b, c} foo".
4837 ;;
4838 ;; This function records identifier ranges on
4839 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
4840 ;; `c-record-type-identifiers' is non-nil.
4841 ;;
4842 ;; This function might do hidden buffer changes.
4843
4844 (let ((start (point)) pos res name-res id-start id-end id-range)
4845
4846 ;; Skip leading type modifiers. If any are found we know it's a
4847 ;; prefix of a type.
4848 (when c-opt-type-modifier-key
4849 (while (looking-at c-opt-type-modifier-key)
4850 (goto-char (match-end 1))
4851 (c-forward-syntactic-ws)
4852 (setq res 'prefix)))
4853
4854 (cond
4855 ((looking-at c-type-prefix-key)
4856 ;; Looking at a keyword that prefixes a type identifier,
4857 ;; e.g. "class".
4858 (goto-char (match-end 1))
4859 (c-forward-syntactic-ws)
4860 (setq pos (point))
4861 (if (memq (setq name-res (c-forward-name)) '(t template))
4862 (progn
4863 (when (eq name-res t)
4864 ;; In many languages the name can be used without the
4865 ;; prefix, so we add it to `c-found-types'.
4866 (c-add-type pos (point))
4867 (when (and c-record-type-identifiers
4868 c-last-identifier-range)
4869 (c-record-type-id c-last-identifier-range)))
4870 (setq res t))
4871 ;; Invalid syntax.
4872 (goto-char start)
4873 (setq res nil)))
4874
4875 ((progn
4876 (setq pos nil)
4877 (if (looking-at c-identifier-start)
4878 (save-excursion
4879 (setq id-start (point)
4880 name-res (c-forward-name))
4881 (when name-res
4882 (setq id-end (point)
4883 id-range c-last-identifier-range))))
4884 (and (cond ((looking-at c-primitive-type-key)
4885 (setq res t))
4886 ((c-with-syntax-table c-identifier-syntax-table
4887 (looking-at c-known-type-key))
4888 (setq res 'known)))
4889 (or (not id-end)
4890 (>= (save-excursion
4891 (save-match-data
4892 (goto-char (match-end 1))
4893 (c-forward-syntactic-ws)
4894 (setq pos (point))))
4895 id-end)
4896 (setq res nil))))
4897 ;; Looking at a primitive or known type identifier. We've
4898 ;; checked for a name first so that we don't go here if the
4899 ;; known type match only is a prefix of another name.
4900
4901 (setq id-end (match-end 1))
4902
4903 (when (and c-record-type-identifiers
4904 (or c-promote-possible-types (eq res t)))
4905 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4906
4907 (if (and c-opt-type-component-key
4908 (save-match-data
4909 (looking-at c-opt-type-component-key)))
4910 ;; There might be more keywords for the type.
4911 (let (safe-pos)
4912 (c-forward-keyword-clause 1)
4913 (while (progn
4914 (setq safe-pos (point))
4915 (looking-at c-opt-type-component-key))
4916 (when (and c-record-type-identifiers
4917 (looking-at c-primitive-type-key))
4918 (c-record-type-id (cons (match-beginning 1)
4919 (match-end 1))))
4920 (c-forward-keyword-clause 1))
4921 (if (looking-at c-primitive-type-key)
4922 (progn
4923 (when c-record-type-identifiers
4924 (c-record-type-id (cons (match-beginning 1)
4925 (match-end 1))))
4926 (c-forward-keyword-clause 1)
4927 (setq res t))
4928 (goto-char safe-pos)
4929 (setq res 'prefix)))
4930 (unless (save-match-data (c-forward-keyword-clause 1))
4931 (if pos
4932 (goto-char pos)
4933 (goto-char (match-end 1))
4934 (c-forward-syntactic-ws)))))
4935
4936 (name-res
4937 (cond ((eq name-res t)
4938 ;; A normal identifier.
4939 (goto-char id-end)
4940 (if (or res c-promote-possible-types)
4941 (progn
4942 (c-add-type id-start id-end)
4943 (when (and c-record-type-identifiers id-range)
4944 (c-record-type-id id-range))
4945 (unless res
4946 (setq res 'found)))
4947 (setq res (if (c-check-type id-start id-end)
4948 ;; It's an identifier that has been used as
4949 ;; a type somewhere else.
4950 'found
4951 ;; It's an identifier that might be a type.
4952 'maybe))))
4953 ((eq name-res 'template)
4954 ;; A template is a type.
4955 (goto-char id-end)
4956 (setq res t))
4957 (t
4958 ;; Otherwise it's an operator identifier, which is not a type.
4959 (goto-char start)
4960 (setq res nil)))))
4961
4962 (when res
4963 ;; Skip trailing type modifiers. If any are found we know it's
4964 ;; a type.
4965 (when c-opt-type-modifier-key
4966 (while (looking-at c-opt-type-modifier-key)
4967 (goto-char (match-end 1))
4968 (c-forward-syntactic-ws)
4969 (setq res t)))
4970
4971 ;; Step over any type suffix operator. Do not let the existence
4972 ;; of these alter the classification of the found type, since
4973 ;; these operators typically are allowed in normal expressions
4974 ;; too.
4975 (when c-opt-type-suffix-key
4976 (while (looking-at c-opt-type-suffix-key)
4977 (goto-char (match-end 1))
4978 (c-forward-syntactic-ws)))
4979
4980 (when c-opt-type-concat-key
4981 ;; Look for a trailing operator that concatenates the type
4982 ;; with a following one, and if so step past that one through
4983 ;; a recursive call. Note that we don't record concatenated
4984 ;; types in `c-found-types' - it's the component types that
4985 ;; are recorded when appropriate.
4986 (setq pos (point))
4987 (let* ((c-promote-possible-types (or (memq res '(t known))
4988 c-promote-possible-types))
4989 ;; If we can't promote then set `c-record-found-types' so that
4990 ;; we can merge in the types from the second part afterwards if
4991 ;; it turns out to be a known type there.
4992 (c-record-found-types (and c-record-type-identifiers
4993 (not c-promote-possible-types)))
4994 subres)
4995 (if (and (looking-at c-opt-type-concat-key)
4996
4997 (progn
4998 (goto-char (match-end 1))
4999 (c-forward-syntactic-ws)
5000 (setq subres (c-forward-type))))
5001
5002 (progn
5003 ;; If either operand certainly is a type then both are, but we
5004 ;; don't let the existence of the operator itself promote two
5005 ;; uncertain types to a certain one.
5006 (cond ((eq res t))
5007 ((eq subres t)
5008 (unless (eq name-res 'template)
5009 (c-add-type id-start id-end))
5010 (when (and c-record-type-identifiers id-range)
5011 (c-record-type-id id-range))
5012 (setq res t))
5013 ((eq res 'known))
5014 ((eq subres 'known)
5015 (setq res 'known))
5016 ((eq res 'found))
5017 ((eq subres 'found)
5018 (setq res 'found))
5019 (t
5020 (setq res 'maybe)))
5021
5022 (when (and (eq res t)
5023 (consp c-record-found-types))
5024 ;; Merge in the ranges of any types found by the second
5025 ;; `c-forward-type'.
5026 (setq c-record-type-identifiers
5027 ;; `nconc' doesn't mind that the tail of
5028 ;; `c-record-found-types' is t.
5029 (nconc c-record-found-types
5030 c-record-type-identifiers))))
5031
5032 (goto-char pos))))
5033
5034 (when (and c-record-found-types (memq res '(known found)) id-range)
5035 (setq c-record-found-types
5036 (cons id-range c-record-found-types))))
5037
5038 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
5039
5040 res))
5041
5042 \f
5043 ;; Handling of large scale constructs like statements and declarations.
5044
5045 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
5046 ;; defsubst or perhaps even a defun, but it contains lots of free
5047 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
5048 (defmacro c-fdoc-shift-type-backward (&optional short)
5049 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
5050 ;; of types when parsing a declaration, which means that it
5051 ;; sometimes consumes the identifier in the declaration as a type.
5052 ;; This is used to "backtrack" and make the last type be treated as
5053 ;; an identifier instead.
5054 `(progn
5055 ,(unless short
5056 ;; These identifiers are bound only in the inner let.
5057 '(setq identifier-type at-type
5058 identifier-start type-start
5059 got-parens nil
5060 got-identifier t
5061 got-suffix t
5062 got-suffix-after-parens id-start
5063 paren-depth 0))
5064
5065 (if (setq at-type (if (eq backup-at-type 'prefix)
5066 t
5067 backup-at-type))
5068 (setq type-start backup-type-start
5069 id-start backup-id-start)
5070 (setq type-start start-pos
5071 id-start start-pos))
5072
5073 ;; When these flags already are set we've found specifiers that
5074 ;; unconditionally signal these attributes - backtracking doesn't
5075 ;; change that. So keep them set in that case.
5076 (or at-type-decl
5077 (setq at-type-decl backup-at-type-decl))
5078 (or maybe-typeless
5079 (setq maybe-typeless backup-maybe-typeless))
5080
5081 ,(unless short
5082 ;; This identifier is bound only in the inner let.
5083 '(setq start id-start))))
5084
5085 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
5086 ;; Move forward over a declaration or a cast if at the start of one.
5087 ;; The point is assumed to be at the start of some token. Nil is
5088 ;; returned if no declaration or cast is recognized, and the point
5089 ;; is clobbered in that case.
5090 ;;
5091 ;; If a declaration is parsed:
5092 ;;
5093 ;; The point is left at the first token after the first complete
5094 ;; declarator, if there is one. The return value is a cons where
5095 ;; the car is the position of the first token in the declarator. (See
5096 ;; below for the cdr.)
5097 ;; Some examples:
5098 ;;
5099 ;; void foo (int a, char *b) stuff ...
5100 ;; car ^ ^ point
5101 ;; float (*a)[], b;
5102 ;; car ^ ^ point
5103 ;; unsigned int a = c_style_initializer, b;
5104 ;; car ^ ^ point
5105 ;; unsigned int a (cplusplus_style_initializer), b;
5106 ;; car ^ ^ point (might change)
5107 ;; class Foo : public Bar {}
5108 ;; car ^ ^ point
5109 ;; class PikeClass (int a, string b) stuff ...
5110 ;; car ^ ^ point
5111 ;; enum bool;
5112 ;; car ^ ^ point
5113 ;; enum bool flag;
5114 ;; car ^ ^ point
5115 ;; void cplusplus_function (int x) throw (Bad);
5116 ;; car ^ ^ point
5117 ;; Foo::Foo (int b) : Base (b) {}
5118 ;; car ^ ^ point
5119 ;;
5120 ;; The cdr of the return value is non-nil iff a `c-typedef-decl-kwds'
5121 ;; specifier (e.g. class, struct, enum, typedef) is found in the
5122 ;; declaration, i.e. the declared identifier(s) are types.
5123 ;;
5124 ;; If a cast is parsed:
5125 ;;
5126 ;; The point is left at the first token after the closing paren of
5127 ;; the cast. The return value is `cast'. Note that the start
5128 ;; position must be at the first token inside the cast parenthesis
5129 ;; to recognize it.
5130 ;;
5131 ;; PRECEDING-TOKEN-END is the first position after the preceding
5132 ;; token, i.e. on the other side of the syntactic ws from the point.
5133 ;; Use a value less than or equal to (point-min) if the point is at
5134 ;; the first token in (the visible part of) the buffer.
5135 ;;
5136 ;; CONTEXT is a symbol that describes the context at the point:
5137 ;; 'decl In a comma-separated declaration context (typically
5138 ;; inside a function declaration arglist).
5139 ;; '<> In an angle bracket arglist.
5140 ;; 'arglist Some other type of arglist.
5141 ;; nil Some other context or unknown context.
5142 ;;
5143 ;; LAST-CAST-END is the first token after the closing paren of a
5144 ;; preceding cast, or nil if none is known. If
5145 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
5146 ;; the position after the closest preceding call where a cast was
5147 ;; matched. In that case it's used to discover chains of casts like
5148 ;; "(a) (b) c".
5149 ;;
5150 ;; This function records identifier ranges on
5151 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5152 ;; `c-record-type-identifiers' is non-nil.
5153 ;;
5154 ;; This function might do hidden buffer changes.
5155
5156 (let (;; `start-pos' is used below to point to the start of the
5157 ;; first type, i.e. after any leading specifiers. It might
5158 ;; also point at the beginning of the preceding syntactic
5159 ;; whitespace.
5160 (start-pos (point))
5161 ;; Set to the result of `c-forward-type'.
5162 at-type
5163 ;; The position of the first token in what we currently
5164 ;; believe is the type in the declaration or cast, after any
5165 ;; specifiers and their associated clauses.
5166 type-start
5167 ;; The position of the first token in what we currently
5168 ;; believe is the declarator for the first identifier. Set
5169 ;; when the type is found, and moved forward over any
5170 ;; `c-decl-hangon-kwds' and their associated clauses that
5171 ;; occurs after the type.
5172 id-start
5173 ;; These store `at-type', `type-start' and `id-start' of the
5174 ;; identifier before the one in those variables. The previous
5175 ;; identifier might turn out to be the real type in a
5176 ;; declaration if the last one has to be the declarator in it.
5177 ;; If `backup-at-type' is nil then the other variables have
5178 ;; undefined values.
5179 backup-at-type backup-type-start backup-id-start
5180 ;; Set if we've found a specifier that makes the defined
5181 ;; identifier(s) types.
5182 at-type-decl
5183 ;; Set if we've found a specifier that can start a declaration
5184 ;; where there's no type.
5185 maybe-typeless
5186 ;; If a specifier is found that also can be a type prefix,
5187 ;; these flags are set instead of those above. If we need to
5188 ;; back up an identifier, they are copied to the real flag
5189 ;; variables. Thus they only take effect if we fail to
5190 ;; interpret it as a type.
5191 backup-at-type-decl backup-maybe-typeless
5192 ;; Whether we've found a declaration or a cast. We might know
5193 ;; this before we've found the type in it. It's 'ids if we've
5194 ;; found two consecutive identifiers (usually a sure sign, but
5195 ;; we should allow that in labels too), and t if we've found a
5196 ;; specifier keyword (a 100% sure sign).
5197 at-decl-or-cast
5198 ;; Set when we need to back up to parse this as a declaration
5199 ;; but not as a cast.
5200 backup-if-not-cast
5201 ;; For casts, the return position.
5202 cast-end
5203 ;; Save `c-record-type-identifiers' and
5204 ;; `c-record-ref-identifiers' since ranges are recorded
5205 ;; speculatively and should be thrown away if it turns out
5206 ;; that it isn't a declaration or cast.
5207 (save-rec-type-ids c-record-type-identifiers)
5208 (save-rec-ref-ids c-record-ref-identifiers))
5209
5210 ;; Check for a type. Unknown symbols are treated as possible
5211 ;; types, but they could also be specifiers disguised through
5212 ;; macros like __INLINE__, so we recognize both types and known
5213 ;; specifiers after them too.
5214 (while
5215 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
5216
5217 ;; Look for a specifier keyword clause.
5218 (when (looking-at c-prefix-spec-kwds-re)
5219 (setq kwd-sym (c-keyword-sym (match-string 1)))
5220 (save-excursion
5221 (c-forward-keyword-clause 1)
5222 (setq kwd-clause-end (point))))
5223
5224 (when (setq found-type (c-forward-type))
5225 ;; Found a known or possible type or a prefix of a known type.
5226
5227 (when at-type
5228 ;; Got two identifiers with nothing but whitespace
5229 ;; between them. That can only happen in declarations.
5230 (setq at-decl-or-cast 'ids)
5231
5232 (when (eq at-type 'found)
5233 ;; If the previous identifier is a found type we
5234 ;; record it as a real one; it might be some sort of
5235 ;; alias for a prefix like "unsigned".
5236 (save-excursion
5237 (goto-char type-start)
5238 (let ((c-promote-possible-types t))
5239 (c-forward-type)))))
5240
5241 (setq backup-at-type at-type
5242 backup-type-start type-start
5243 backup-id-start id-start
5244 at-type found-type
5245 type-start start
5246 id-start (point)
5247 ;; The previous ambiguous specifier/type turned out
5248 ;; to be a type since we've parsed another one after
5249 ;; it, so clear these backup flags.
5250 backup-at-type-decl nil
5251 backup-maybe-typeless nil))
5252
5253 (if kwd-sym
5254 (progn
5255 ;; Handle known specifier keywords and
5256 ;; `c-decl-hangon-kwds' which can occur after known
5257 ;; types.
5258
5259 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
5260 ;; It's a hang-on keyword that can occur anywhere.
5261 (progn
5262 (setq at-decl-or-cast t)
5263 (if at-type
5264 ;; Move the identifier start position if
5265 ;; we've passed a type.
5266 (setq id-start kwd-clause-end)
5267 ;; Otherwise treat this as a specifier and
5268 ;; move the fallback position.
5269 (setq start-pos kwd-clause-end))
5270 (goto-char kwd-clause-end))
5271
5272 ;; It's an ordinary specifier so we know that
5273 ;; anything before this can't be the type.
5274 (setq backup-at-type nil
5275 start-pos kwd-clause-end)
5276
5277 (if found-type
5278 ;; It's ambiguous whether this keyword is a
5279 ;; specifier or a type prefix, so set the backup
5280 ;; flags. (It's assumed that `c-forward-type'
5281 ;; moved further than `c-forward-keyword-clause'.)
5282 (progn
5283 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5284 (setq backup-at-type-decl t))
5285 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5286 (setq backup-maybe-typeless t)))
5287
5288 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
5289 (setq at-type-decl t))
5290 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
5291 (setq maybe-typeless t))
5292
5293 ;; Haven't matched a type so it's an umambiguous
5294 ;; specifier keyword and we know we're in a
5295 ;; declaration.
5296 (setq at-decl-or-cast t)
5297
5298 (goto-char kwd-clause-end))))
5299
5300 ;; If the type isn't known we continue so that we'll jump
5301 ;; over all specifiers and type identifiers. The reason
5302 ;; to do this for a known type prefix is to make things
5303 ;; like "unsigned INT16" work.
5304 (and found-type (not (eq found-type t))))))
5305
5306 (cond
5307 ((eq at-type t)
5308 ;; If a known type was found, we still need to skip over any
5309 ;; hangon keyword clauses after it. Otherwise it has already
5310 ;; been done in the loop above.
5311 (while (looking-at c-decl-hangon-key)
5312 (c-forward-keyword-clause 1))
5313 (setq id-start (point)))
5314
5315 ((eq at-type 'prefix)
5316 ;; A prefix type is itself a primitive type when it's not
5317 ;; followed by another type.
5318 (setq at-type t))
5319
5320 ((not at-type)
5321 ;; Got no type but set things up to continue anyway to handle
5322 ;; the various cases when a declaration doesn't start with a
5323 ;; type.
5324 (setq id-start start-pos))
5325
5326 ((and (eq at-type 'maybe)
5327 (c-major-mode-is 'c++-mode))
5328 ;; If it's C++ then check if the last "type" ends on the form
5329 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
5330 ;; (con|de)structor.
5331 (save-excursion
5332 (let (name end-2 end-1)
5333 (goto-char id-start)
5334 (c-backward-syntactic-ws)
5335 (setq end-2 (point))
5336 (when (and
5337 (c-simple-skip-symbol-backward)
5338 (progn
5339 (setq name
5340 (buffer-substring-no-properties (point) end-2))
5341 ;; Cheating in the handling of syntactic ws below.
5342 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
5343 (progn
5344 (setq end-1 (point))
5345 (c-simple-skip-symbol-backward))
5346 (>= (point) type-start)
5347 (equal (buffer-substring-no-properties (point) end-1)
5348 name))
5349 ;; It is a (con|de)structor name. In that case the
5350 ;; declaration is typeless so zap out any preceding
5351 ;; identifier(s) that we might have taken as types.
5352 (goto-char type-start)
5353 (setq at-type nil
5354 backup-at-type nil
5355 id-start type-start))))))
5356
5357 ;; Check for and step over a type decl expression after the thing
5358 ;; that is or might be a type. This can't be skipped since we
5359 ;; need the correct end position of the declarator for
5360 ;; `max-type-decl-end-*'.
5361 (let ((start (point)) (paren-depth 0) pos
5362 ;; True if there's a non-open-paren match of
5363 ;; `c-type-decl-prefix-key'.
5364 got-prefix
5365 ;; True if the declarator is surrounded by a parenthesis pair.
5366 got-parens
5367 ;; True if there is an identifier in the declarator.
5368 got-identifier
5369 ;; True if there's a non-close-paren match of
5370 ;; `c-type-decl-suffix-key'.
5371 got-suffix
5372 ;; True if there's a prefix match outside the outermost
5373 ;; paren pair that surrounds the declarator.
5374 got-prefix-before-parens
5375 ;; True if there's a suffix match outside the outermost
5376 ;; paren pair that surrounds the declarator. The value is
5377 ;; the position of the first suffix match.
5378 got-suffix-after-parens
5379 ;; True if we've parsed the type decl to a token that is
5380 ;; known to end declarations in this context.
5381 at-decl-end
5382 ;; The earlier values of `at-type' and `type-start' if we've
5383 ;; shifted the type backwards.
5384 identifier-type identifier-start
5385 ;; If `c-parse-and-markup-<>-arglists' is set we need to
5386 ;; turn it off during the name skipping below to avoid
5387 ;; getting `c-type' properties that might be bogus. That
5388 ;; can happen since we don't know if
5389 ;; `c-restricted-<>-arglists' will be correct inside the
5390 ;; arglist paren that gets entered.
5391 c-parse-and-markup-<>-arglists)
5392
5393 (goto-char id-start)
5394
5395 ;; Skip over type decl prefix operators. (Note similar code in
5396 ;; `c-font-lock-declarators'.)
5397 (while (and (looking-at c-type-decl-prefix-key)
5398 (if (and (c-major-mode-is 'c++-mode)
5399 (match-beginning 2))
5400 ;; If the second submatch matches in C++ then
5401 ;; we're looking at an identifier that's a
5402 ;; prefix only if it specifies a member pointer.
5403 (when (setq got-identifier (c-forward-name))
5404 (if (looking-at "\\(::\\)")
5405 ;; We only check for a trailing "::" and
5406 ;; let the "*" that should follow be
5407 ;; matched in the next round.
5408 (progn (setq got-identifier nil) t)
5409 ;; It turned out to be the real identifier,
5410 ;; so stop.
5411 nil))
5412 t))
5413
5414 (if (eq (char-after) ?\()
5415 (progn
5416 (setq paren-depth (1+ paren-depth))
5417 (forward-char))
5418 (unless got-prefix-before-parens
5419 (setq got-prefix-before-parens (= paren-depth 0)))
5420 (setq got-prefix t)
5421 (goto-char (match-end 1)))
5422 (c-forward-syntactic-ws))
5423
5424 (setq got-parens (> paren-depth 0))
5425
5426 ;; Skip over an identifier.
5427 (or got-identifier
5428 (and (looking-at c-identifier-start)
5429 (setq got-identifier (c-forward-name))))
5430
5431 ;; Skip over type decl suffix operators.
5432 (while (if (looking-at c-type-decl-suffix-key)
5433
5434 (if (eq (char-after) ?\))
5435 (when (> paren-depth 0)
5436 (setq paren-depth (1- paren-depth))
5437 (forward-char)
5438 t)
5439 (when (if (save-match-data (looking-at "\\s\("))
5440 (c-safe (c-forward-sexp 1) t)
5441 (goto-char (match-end 1))
5442 t)
5443 (when (and (not got-suffix-after-parens)
5444 (= paren-depth 0))
5445 (setq got-suffix-after-parens (match-beginning 0)))
5446 (setq got-suffix t)))
5447
5448 ;; No suffix matched. We might have matched the
5449 ;; identifier as a type and the open paren of a
5450 ;; function arglist as a type decl prefix. In that
5451 ;; case we should "backtrack": Reinterpret the last
5452 ;; type as the identifier, move out of the arglist and
5453 ;; continue searching for suffix operators.
5454 ;;
5455 ;; Do this even if there's no preceding type, to cope
5456 ;; with old style function declarations in K&R C,
5457 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
5458 ;; style declarations. That isn't applicable in an
5459 ;; arglist context, though.
5460 (when (and (= paren-depth 1)
5461 (not got-prefix-before-parens)
5462 (not (eq at-type t))
5463 (or backup-at-type
5464 maybe-typeless
5465 backup-maybe-typeless
5466 (when c-recognize-typeless-decls
5467 (not context)))
5468 (setq pos (c-up-list-forward (point)))
5469 (eq (char-before pos) ?\)))
5470 (c-fdoc-shift-type-backward)
5471 (goto-char pos)
5472 t))
5473
5474 (c-forward-syntactic-ws))
5475
5476 (when (and (or maybe-typeless backup-maybe-typeless)
5477 (not got-identifier)
5478 (not got-prefix)
5479 at-type)
5480 ;; Have found no identifier but `c-typeless-decl-kwds' has
5481 ;; matched so we know we're inside a declaration. The
5482 ;; preceding type must be the identifier instead.
5483 (c-fdoc-shift-type-backward))
5484
5485 (setq
5486 at-decl-or-cast
5487 (catch 'at-decl-or-cast
5488
5489 (when (> paren-depth 0)
5490 ;; Encountered something inside parens that isn't matched by
5491 ;; the `c-type-decl-*' regexps, so it's not a type decl
5492 ;; expression. Try to skip out to the same paren depth to
5493 ;; not confuse the cast check below.
5494 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
5495 ;; If we've found a specifier keyword then it's a
5496 ;; declaration regardless.
5497 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
5498
5499 (setq at-decl-end
5500 (looking-at (cond ((eq context '<>) "[,>]")
5501 (context "[,\)]")
5502 (t "[,;]"))))
5503
5504 ;; Now we've collected info about various characteristics of
5505 ;; the construct we're looking at. Below follows a decision
5506 ;; tree based on that. It's ordered to check more certain
5507 ;; signs before less certain ones.
5508
5509 (if got-identifier
5510 (progn
5511
5512 (when (and (or at-type maybe-typeless)
5513 (not (or got-prefix got-parens)))
5514 ;; Got another identifier directly after the type, so it's a
5515 ;; declaration.
5516 (throw 'at-decl-or-cast t))
5517
5518 (when (and got-parens
5519 (not got-prefix)
5520 (not got-suffix-after-parens)
5521 (or backup-at-type
5522 maybe-typeless
5523 backup-maybe-typeless))
5524 ;; Got a declaration of the form "foo bar (gnu);" where we've
5525 ;; recognized "bar" as the type and "gnu" as the declarator.
5526 ;; In this case it's however more likely that "bar" is the
5527 ;; declarator and "gnu" a function argument or initializer (if
5528 ;; `c-recognize-paren-inits' is set), since the parens around
5529 ;; "gnu" would be superfluous if it's a declarator. Shift the
5530 ;; type one step backward.
5531 (c-fdoc-shift-type-backward)))
5532
5533 ;; Found no identifier.
5534
5535 (if backup-at-type
5536 (progn
5537
5538 (when (= (point) start)
5539 ;; Got a plain list of identifiers. If a colon follows it's
5540 ;; a valid label. Otherwise the last one probably is the
5541 ;; declared identifier and we should back up to the previous
5542 ;; type, providing it isn't a cast.
5543 (if (eq (char-after) ?:)
5544 ;; If we've found a specifier keyword then it's a
5545 ;; declaration regardless.
5546 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
5547 (setq backup-if-not-cast t)
5548 (throw 'at-decl-or-cast t)))
5549
5550 (when (and got-suffix
5551 (not got-prefix)
5552 (not got-parens))
5553 ;; Got a plain list of identifiers followed by some suffix.
5554 ;; If this isn't a cast then the last identifier probably is
5555 ;; the declared one and we should back up to the previous
5556 ;; type.
5557 (setq backup-if-not-cast t)
5558 (throw 'at-decl-or-cast t)))
5559
5560 (when (eq at-type t)
5561 ;; If the type is known we know that there can't be any
5562 ;; identifier somewhere else, and it's only in declarations in
5563 ;; e.g. function prototypes and in casts that the identifier may
5564 ;; be left out.
5565 (throw 'at-decl-or-cast t))
5566
5567 (when (= (point) start)
5568 ;; Only got a single identifier (parsed as a type so far).
5569 (if (and
5570 ;; Check that the identifier isn't at the start of an
5571 ;; expression.
5572 at-decl-end
5573 (cond
5574 ((eq context 'decl)
5575 ;; Inside an arglist that contains declarations. If K&R
5576 ;; style declarations and parenthesis style initializers
5577 ;; aren't allowed then the single identifier must be a
5578 ;; type, else we require that it's known or found
5579 ;; (primitive types are handled above).
5580 (or (and (not c-recognize-knr-p)
5581 (not c-recognize-paren-inits))
5582 (memq at-type '(known found))))
5583 ((eq context '<>)
5584 ;; Inside a template arglist. Accept known and found
5585 ;; types; other identifiers could just as well be
5586 ;; constants in C++.
5587 (memq at-type '(known found)))))
5588 (throw 'at-decl-or-cast t)
5589 ;; Can't be a valid declaration or cast, but if we've found a
5590 ;; specifier it can't be anything else either, so treat it as
5591 ;; an invalid/unfinished declaration or cast.
5592 (throw 'at-decl-or-cast at-decl-or-cast))))
5593
5594 (if (and got-parens
5595 (not got-prefix)
5596 (not context)
5597 (not (eq at-type t))
5598 (or backup-at-type
5599 maybe-typeless
5600 backup-maybe-typeless
5601 (when c-recognize-typeless-decls
5602 (or (not got-suffix)
5603 (not (looking-at
5604 c-after-suffixed-type-maybe-decl-key))))))
5605 ;; Got an empty paren pair and a preceding type that probably
5606 ;; really is the identifier. Shift the type backwards to make
5607 ;; the last one the identifier. This is analogous to the
5608 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
5609 ;; above.
5610 ;;
5611 ;; Exception: In addition to the conditions in that
5612 ;; "backtracking" code, do not shift backward if we're not
5613 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
5614 ;; Since there's no preceding type, the shift would mean that
5615 ;; the declaration is typeless. But if the regexp doesn't match
5616 ;; then we will simply fall through in the tests below and not
5617 ;; recognize it at all, so it's better to try it as an abstract
5618 ;; declarator instead.
5619 (c-fdoc-shift-type-backward)
5620
5621 ;; Still no identifier.
5622
5623 (when (and got-prefix (or got-parens got-suffix))
5624 ;; Require `got-prefix' together with either `got-parens' or
5625 ;; `got-suffix' to recognize it as an abstract declarator:
5626 ;; `got-parens' only is probably an empty function call.
5627 ;; `got-suffix' only can build an ordinary expression together
5628 ;; with the preceding identifier which we've taken as a type.
5629 ;; We could actually accept on `got-prefix' only, but that can
5630 ;; easily occur temporarily while writing an expression so we
5631 ;; avoid that case anyway. We could do a better job if we knew
5632 ;; the point when the fontification was invoked.
5633 (throw 'at-decl-or-cast t))
5634
5635 (when (and at-type
5636 (not got-prefix)
5637 (not got-parens)
5638 got-suffix-after-parens
5639 (eq (char-after got-suffix-after-parens) ?\())
5640 ;; Got a type, no declarator but a paren suffix. I.e. it's a
5641 ;; normal function call afterall (or perhaps a C++ style object
5642 ;; instantiation expression).
5643 (throw 'at-decl-or-cast nil))))
5644
5645 (when at-decl-or-cast
5646 ;; By now we've located the type in the declaration that we know
5647 ;; we're in.
5648 (throw 'at-decl-or-cast t))
5649
5650 (when (and got-identifier
5651 (not context)
5652 (looking-at c-after-suffixed-type-decl-key)
5653 (if (and got-parens
5654 (not got-prefix)
5655 (not got-suffix)
5656 (not (eq at-type t)))
5657 ;; Shift the type backward in the case that there's a
5658 ;; single identifier inside parens. That can only
5659 ;; occur in K&R style function declarations so it's
5660 ;; more likely that it really is a function call.
5661 ;; Therefore we only do this after
5662 ;; `c-after-suffixed-type-decl-key' has matched.
5663 (progn (c-fdoc-shift-type-backward) t)
5664 got-suffix-after-parens))
5665 ;; A declaration according to `c-after-suffixed-type-decl-key'.
5666 (throw 'at-decl-or-cast t))
5667
5668 (when (and (or got-prefix (not got-parens))
5669 (memq at-type '(t known)))
5670 ;; It's a declaration if a known type precedes it and it can't be a
5671 ;; function call.
5672 (throw 'at-decl-or-cast t))
5673
5674 ;; If we get here we can't tell if this is a type decl or a normal
5675 ;; expression by looking at it alone. (That's under the assumption
5676 ;; that normal expressions always can look like type decl expressions,
5677 ;; which isn't really true but the cases where it doesn't hold are so
5678 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
5679 ;; the effort to look for them.)
5680
5681 (unless (or at-decl-end (looking-at "=[^=]"))
5682 ;; If this is a declaration it should end here or its initializer(*)
5683 ;; should start here, so check for allowed separation tokens. Note
5684 ;; that this rule doesn't work e.g. with a K&R arglist after a
5685 ;; function header.
5686 ;;
5687 ;; *) Don't check for C++ style initializers using parens
5688 ;; since those already have been matched as suffixes.
5689 ;;
5690 ;; If `at-decl-or-cast' is then we've found some other sign that
5691 ;; it's a declaration or cast, so then it's probably an
5692 ;; invalid/unfinished one.
5693 (throw 'at-decl-or-cast at-decl-or-cast))
5694
5695 ;; Below are tests that only should be applied when we're certain to
5696 ;; not have parsed halfway through an expression.
5697
5698 (when (memq at-type '(t known))
5699 ;; The expression starts with a known type so treat it as a
5700 ;; declaration.
5701 (throw 'at-decl-or-cast t))
5702
5703 (when (and (c-major-mode-is 'c++-mode)
5704 ;; In C++ we check if the identifier is a known type, since
5705 ;; (con|de)structors use the class name as identifier.
5706 ;; We've always shifted over the identifier as a type and
5707 ;; then backed up again in this case.
5708 identifier-type
5709 (or (memq identifier-type '(found known))
5710 (and (eq (char-after identifier-start) ?~)
5711 ;; `at-type' probably won't be 'found for
5712 ;; destructors since the "~" is then part of the
5713 ;; type name being checked against the list of
5714 ;; known types, so do a check without that
5715 ;; operator.
5716 (or (save-excursion
5717 (goto-char (1+ identifier-start))
5718 (c-forward-syntactic-ws)
5719 (c-with-syntax-table
5720 c-identifier-syntax-table
5721 (looking-at c-known-type-key)))
5722 (save-excursion
5723 (goto-char (1+ identifier-start))
5724 ;; We have already parsed the type earlier,
5725 ;; so it'd be possible to cache the end
5726 ;; position instead of redoing it here, but
5727 ;; then we'd need to keep track of another
5728 ;; position everywhere.
5729 (c-check-type (point)
5730 (progn (c-forward-type)
5731 (point))))))))
5732 (throw 'at-decl-or-cast t))
5733
5734 (if got-identifier
5735 (progn
5736 (when (and got-prefix-before-parens
5737 at-type
5738 (or at-decl-end (looking-at "=[^=]"))
5739 (not context)
5740 (not got-suffix))
5741 ;; Got something like "foo * bar;". Since we're not inside an
5742 ;; arglist it would be a meaningless expression because the
5743 ;; result isn't used. We therefore choose to recognize it as
5744 ;; a declaration. Do not allow a suffix since it could then
5745 ;; be a function call.
5746 (throw 'at-decl-or-cast t))
5747
5748 (when (and (or got-suffix-after-parens
5749 (looking-at "=[^=]"))
5750 (eq at-type 'found)
5751 (not (eq context 'arglist)))
5752 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
5753 ;; be an odd expression or it could be a declaration. Treat
5754 ;; it as a declaration if "a" has been used as a type
5755 ;; somewhere else (if it's a known type we won't get here).
5756 (throw 'at-decl-or-cast t)))
5757
5758 (when (and context
5759 (or got-prefix
5760 (and (eq context 'decl)
5761 (not c-recognize-paren-inits)
5762 (or got-parens got-suffix))))
5763 ;; Got a type followed by an abstract declarator. If `got-prefix'
5764 ;; is set it's something like "a *" without anything after it. If
5765 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
5766 ;; or similar, which we accept only if the context rules out
5767 ;; expressions.
5768 (throw 'at-decl-or-cast t)))
5769
5770 ;; If we had a complete symbol table here (which rules out
5771 ;; `c-found-types') we should return t due to the disambiguation rule
5772 ;; (in at least C++) that anything that can be parsed as a declaration
5773 ;; is a declaration. Now we're being more defensive and prefer to
5774 ;; highlight things like "foo (bar);" as a declaration only if we're
5775 ;; inside an arglist that contains declarations.
5776 (eq context 'decl))))
5777
5778 ;; The point is now after the type decl expression.
5779
5780 (cond
5781 ;; Check for a cast.
5782 ((save-excursion
5783 (and
5784 c-cast-parens
5785
5786 ;; Should be the first type/identifier in a cast paren.
5787 (> preceding-token-end (point-min))
5788 (memq (char-before preceding-token-end) c-cast-parens)
5789
5790 ;; The closing paren should follow.
5791 (progn
5792 (c-forward-syntactic-ws)
5793 (looking-at "\\s\)"))
5794
5795 ;; There should be a primary expression after it.
5796 (let (pos)
5797 (forward-char)
5798 (c-forward-syntactic-ws)
5799 (setq cast-end (point))
5800 (and (looking-at c-primary-expr-regexp)
5801 (progn
5802 (setq pos (match-end 0))
5803 (or
5804 ;; Check if the expression begins with a prefix keyword.
5805 (match-beginning 2)
5806 (if (match-beginning 1)
5807 ;; Expression begins with an ambiguous operator. Treat
5808 ;; it as a cast if it's a type decl or if we've
5809 ;; recognized the type somewhere else.
5810 (or at-decl-or-cast
5811 (memq at-type '(t known found)))
5812 ;; Unless it's a keyword, it's the beginning of a primary
5813 ;; expression.
5814 (not (looking-at c-keywords-regexp)))))
5815 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
5816 ;; that it matched a whole one so that we don't e.g. confuse
5817 ;; the operator '-' with '->'. It's ok if it matches further,
5818 ;; though, since it e.g. can match the float '.5' while the
5819 ;; operator regexp only matches '.'.
5820 (or (not (looking-at c-nonsymbol-token-regexp))
5821 (<= (match-end 0) pos))))
5822
5823 ;; There should either be a cast before it or something that isn't an
5824 ;; identifier or close paren.
5825 (> preceding-token-end (point-min))
5826 (progn
5827 (goto-char (1- preceding-token-end))
5828 (or (eq (point) last-cast-end)
5829 (progn
5830 (c-backward-syntactic-ws)
5831 (if (< (skip-syntax-backward "w_") 0)
5832 ;; It's a symbol. Accept it only if it's one of the
5833 ;; keywords that can precede an expression (without
5834 ;; surrounding parens).
5835 (looking-at c-simple-stmt-key)
5836 (and
5837 ;; Check that it isn't a close paren (block close is ok,
5838 ;; though).
5839 (not (memq (char-before) '(?\) ?\])))
5840 ;; Check that it isn't a nonsymbol identifier.
5841 (not (c-on-identifier)))))))))
5842
5843 ;; Handle the cast.
5844 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5845 (let ((c-promote-possible-types t))
5846 (goto-char type-start)
5847 (c-forward-type)))
5848
5849 (goto-char cast-end)
5850 'cast)
5851
5852 (at-decl-or-cast
5853 ;; We're at a declaration. Highlight the type and the following
5854 ;; declarators.
5855
5856 (when backup-if-not-cast
5857 (c-fdoc-shift-type-backward t))
5858
5859 (when (and (eq context 'decl) (looking-at ","))
5860 ;; Make sure to propagate the `c-decl-arg-start' property to
5861 ;; the next argument if it's set in this one, to cope with
5862 ;; interactive refontification.
5863 (c-put-c-type-property (point) 'c-decl-arg-start))
5864
5865 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
5866 (let ((c-promote-possible-types t))
5867 (save-excursion
5868 (goto-char type-start)
5869 (c-forward-type))))
5870
5871 (cons id-start at-type-decl))
5872
5873 (t
5874 ;; False alarm. Restore the recorded ranges.
5875 (setq c-record-type-identifiers save-rec-type-ids
5876 c-record-ref-identifiers save-rec-ref-ids)
5877 nil))))
5878
5879 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
5880 ;; Assuming that point is at the beginning of a token, check if it starts a
5881 ;; label and if so move over it and return non-nil (t in default situations,
5882 ;; specific symbols (see below) for interesting situations), otherwise don't
5883 ;; move and return nil. "Label" here means "most things with a colon".
5884 ;;
5885 ;; More precisely, a "label" is regarded as one of:
5886 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
5887 ;; (ii) A case label - either the entire construct "case FOO:", or just the
5888 ;; bare "case", should the colon be missing. We return t;
5889 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
5890 ;; return t;
5891 ;; (iv) One of QT's "extended" C++ variants of
5892 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
5893 ;; Returns the symbol `qt-2kwds-colon'.
5894 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
5895 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
5896 ;; colon). Currently (2006-03), this applies only to Objective C's
5897 ;; keywords "@private", "@protected", and "@public". Returns t.
5898 ;;
5899 ;; One of the things which will NOT be recognised as a label is a bit-field
5900 ;; element of a struct, something like "int foo:5".
5901 ;;
5902 ;; The end of the label is taken to be just after the colon, or the end of
5903 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
5904 ;; after the end on return. The terminating char gets marked with
5905 ;; `c-decl-end' to improve recognition of the following declaration or
5906 ;; statement.
5907 ;;
5908 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
5909 ;; label, if any, has already been marked up like that.
5910 ;;
5911 ;; If PRECEDING-TOKEN-END is given, it should be the first position
5912 ;; after the preceding token, i.e. on the other side of the
5913 ;; syntactic ws from the point. Use a value less than or equal to
5914 ;; (point-min) if the point is at the first token in (the visible
5915 ;; part of) the buffer.
5916 ;;
5917 ;; The optional LIMIT limits the forward scan for the colon.
5918 ;;
5919 ;; This function records the ranges of the label symbols on
5920 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
5921 ;; non-nil.
5922 ;;
5923 ;; This function might do hidden buffer changes.
5924
5925 (let ((start (point))
5926 label-end
5927 qt-symbol-idx
5928 macro-start ; if we're in one.
5929 label-type)
5930 (cond
5931 ;; "case" or "default" (Doesn't apply to AWK).
5932 ((looking-at c-label-kwds-regexp)
5933 (let ((kwd-end (match-end 1)))
5934 ;; Record only the keyword itself for fontification, since in
5935 ;; case labels the following is a constant expression and not
5936 ;; a label.
5937 (when c-record-type-identifiers
5938 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
5939
5940 ;; Find the label end.
5941 (goto-char kwd-end)
5942 (setq label-type
5943 (if (and (c-syntactic-re-search-forward
5944 ;; Stop on chars that aren't allowed in expressions,
5945 ;; and on operator chars that would be meaningless
5946 ;; there. FIXME: This doesn't cope with ?: operators.
5947 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
5948 limit t t nil 1)
5949 (match-beginning 2))
5950
5951 (progn ; there's a proper :
5952 (goto-char (match-beginning 2)) ; just after the :
5953 (c-put-c-type-property (1- (point)) 'c-decl-end)
5954 t)
5955
5956 ;; It's an unfinished label. We consider the keyword enough
5957 ;; to recognize it as a label, so that it gets fontified.
5958 ;; Leave the point at the end of it, but don't put any
5959 ;; `c-decl-end' marker.
5960 (goto-char kwd-end)
5961 t))))
5962
5963 ;; @private, @protected, @public, in Objective C, or similar.
5964 ((and c-opt-extra-label-key
5965 (looking-at c-opt-extra-label-key))
5966 ;; For a `c-opt-extra-label-key' match, we record the whole
5967 ;; thing for fontification. That's to get the leading '@' in
5968 ;; Objective-C protection labels fontified.
5969 (goto-char (match-end 1))
5970 (when c-record-type-identifiers
5971 (c-record-ref-id (cons (match-beginning 1) (point))))
5972 (c-put-c-type-property (1- (point)) 'c-decl-end)
5973 (setq label-type t))
5974
5975 ;; All other cases of labels.
5976 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
5977
5978 ;; A colon label must have something before the colon.
5979 (not (eq (char-after) ?:))
5980
5981 ;; Check that we're not after a token that can't precede a label.
5982 (or
5983 ;; Trivially succeeds when there's no preceding token.
5984 (if preceding-token-end
5985 (<= preceding-token-end (point-min))
5986 (save-excursion
5987 (c-backward-syntactic-ws)
5988 (setq preceding-token-end (point))
5989 (bobp)))
5990
5991 ;; Check if we're after a label, if we're after a closing
5992 ;; paren that belong to statement, and with
5993 ;; `c-label-prefix-re'. It's done in different order
5994 ;; depending on `assume-markup' since the checks have
5995 ;; different expensiveness.
5996 (if assume-markup
5997 (or
5998 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
5999 'c-decl-end)
6000
6001 (save-excursion
6002 (goto-char (1- preceding-token-end))
6003 (c-beginning-of-current-token)
6004 (or (looking-at c-label-prefix-re)
6005 (looking-at c-block-stmt-1-key)))
6006
6007 (and (eq (char-before preceding-token-end) ?\))
6008 (c-after-conditional)))
6009
6010 (or
6011 (save-excursion
6012 (goto-char (1- preceding-token-end))
6013 (c-beginning-of-current-token)
6014 (or (looking-at c-label-prefix-re)
6015 (looking-at c-block-stmt-1-key)))
6016
6017 (cond
6018 ((eq (char-before preceding-token-end) ?\))
6019 (c-after-conditional))
6020
6021 ((eq (char-before preceding-token-end) ?:)
6022 ;; Might be after another label, so check it recursively.
6023 (save-restriction
6024 (save-excursion
6025 (goto-char (1- preceding-token-end))
6026 ;; Essentially the same as the
6027 ;; `c-syntactic-re-search-forward' regexp below.
6028 (setq macro-start
6029 (save-excursion (and (c-beginning-of-macro)
6030 (point))))
6031 (if macro-start (narrow-to-region macro-start (point-max)))
6032 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
6033 ;; Note: the following should work instead of the
6034 ;; narrow-to-region above. Investigate why not,
6035 ;; sometime. ACM, 2006-03-31.
6036 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
6037 ;; macro-start t)
6038 (let ((pte (point))
6039 ;; If the caller turned on recording for us,
6040 ;; it shouldn't apply when we check the
6041 ;; preceding label.
6042 c-record-type-identifiers)
6043 ;; A label can't start at a cpp directive. Check for
6044 ;; this, since c-forward-syntactic-ws would foul up on it.
6045 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
6046 (c-forward-syntactic-ws)
6047 (c-forward-label nil pte start))))))))))
6048
6049 ;; Point is still at the beginning of the possible label construct.
6050 ;;
6051 ;; Check that the next nonsymbol token is ":", or that we're in one
6052 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
6053 ;; arguments. FIXME: Should build this regexp from the language
6054 ;; constants.
6055 (cond
6056 ;; public: protected: private:
6057 ((and
6058 (c-major-mode-is 'c++-mode)
6059 (search-forward-regexp
6060 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
6061 (progn (backward-char)
6062 (c-forward-syntactic-ws limit)
6063 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
6064 (forward-char)
6065 (setq label-type t))
6066 ;; QT double keyword like "protected slots:" or goto target.
6067 ((progn (goto-char start) nil))
6068 ((when (c-syntactic-re-search-forward
6069 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
6070 (backward-char)
6071 (setq label-end (point))
6072 (setq qt-symbol-idx
6073 (and (c-major-mode-is 'c++-mode)
6074 (string-match
6075 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
6076 (buffer-substring start (point)))))
6077 (c-forward-syntactic-ws limit)
6078 (cond
6079 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
6080 (forward-char)
6081 (setq label-type
6082 (if (string= "signals" ; Special QT macro
6083 (buffer-substring-no-properties start label-end))
6084 'qt-1kwd-colon
6085 'goto-target)))
6086 ((and qt-symbol-idx
6087 (search-forward-regexp "\\=slots\\>" limit t)
6088 (progn (c-forward-syntactic-ws limit)
6089 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
6090 (forward-char)
6091 (setq label-type 'qt-2kwds-colon)))))))
6092
6093 (save-restriction
6094 (narrow-to-region start (point))
6095
6096 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
6097 (catch 'check-label
6098 (goto-char start)
6099 (while (progn
6100 (when (looking-at c-nonlabel-token-key)
6101 (goto-char start)
6102 (setq label-type nil)
6103 (throw 'check-label nil))
6104 (and (c-safe (c-forward-sexp)
6105 (c-forward-syntactic-ws)
6106 t)
6107 (not (eobp)))))
6108
6109 ;; Record the identifiers in the label for fontification, unless
6110 ;; it begins with `c-label-kwds' in which case the following
6111 ;; identifiers are part of a (constant) expression that
6112 ;; shouldn't be fontified.
6113 (when (and c-record-type-identifiers
6114 (progn (goto-char start)
6115 (not (looking-at c-label-kwds-regexp))))
6116 (while (c-syntactic-re-search-forward c-symbol-key nil t)
6117 (c-record-ref-id (cons (match-beginning 0)
6118 (match-end 0)))))
6119
6120 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
6121 (goto-char (point-max)))))
6122
6123 (t
6124 ;; Not a label.
6125 (goto-char start)))
6126 label-type))
6127
6128 (defun c-forward-objc-directive ()
6129 ;; Assuming the point is at the beginning of a token, try to move
6130 ;; forward to the end of the Objective-C directive that starts
6131 ;; there. Return t if a directive was fully recognized, otherwise
6132 ;; the point is moved as far as one could be successfully parsed and
6133 ;; nil is returned.
6134 ;;
6135 ;; This function records identifier ranges on
6136 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6137 ;; `c-record-type-identifiers' is non-nil.
6138 ;;
6139 ;; This function might do hidden buffer changes.
6140
6141 (let ((start (point))
6142 start-char
6143 (c-promote-possible-types t)
6144 ;; Turn off recognition of angle bracket arglists while parsing
6145 ;; types here since the protocol reference list might then be
6146 ;; considered part of the preceding name or superclass-name.
6147 c-recognize-<>-arglists)
6148
6149 (if (or
6150 (when (looking-at
6151 (eval-when-compile
6152 (c-make-keywords-re t
6153 (append (c-lang-const c-protection-kwds objc)
6154 '("@end"))
6155 'objc-mode)))
6156 (goto-char (match-end 1))
6157 t)
6158
6159 (and
6160 (looking-at
6161 (eval-when-compile
6162 (c-make-keywords-re t
6163 '("@interface" "@implementation" "@protocol")
6164 'objc-mode)))
6165
6166 ;; Handle the name of the class itself.
6167 (progn
6168 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
6169 ; at EOB.
6170 (goto-char (match-end 0))
6171 (c-skip-ws-forward)
6172 (c-forward-type))
6173
6174 (catch 'break
6175 ;; Look for ": superclass-name" or "( category-name )".
6176 (when (looking-at "[:\(]")
6177 (setq start-char (char-after))
6178 (forward-char)
6179 (c-forward-syntactic-ws)
6180 (unless (c-forward-type) (throw 'break nil))
6181 (when (eq start-char ?\()
6182 (unless (eq (char-after) ?\)) (throw 'break nil))
6183 (forward-char)
6184 (c-forward-syntactic-ws)))
6185
6186 ;; Look for a protocol reference list.
6187 (if (eq (char-after) ?<)
6188 (let ((c-recognize-<>-arglists t)
6189 (c-parse-and-markup-<>-arglists t)
6190 c-restricted-<>-arglists)
6191 (c-forward-<>-arglist t))
6192 t))))
6193
6194 (progn
6195 (c-backward-syntactic-ws)
6196 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
6197 (c-put-c-type-property (1- (point)) 'c-decl-end)
6198 t)
6199
6200 (c-clear-c-type-property start (point) 'c-decl-end)
6201 nil)))
6202
6203 (defun c-beginning-of-inheritance-list (&optional lim)
6204 ;; Go to the first non-whitespace after the colon that starts a
6205 ;; multiple inheritance introduction. Optional LIM is the farthest
6206 ;; back we should search.
6207 ;;
6208 ;; This function might do hidden buffer changes.
6209 (c-with-syntax-table c++-template-syntax-table
6210 (c-backward-token-2 0 t lim)
6211 (while (and (or (looking-at c-symbol-start)
6212 (looking-at "[<,]\\|::"))
6213 (zerop (c-backward-token-2 1 t lim))))))
6214
6215 (defun c-in-method-def-p ()
6216 ;; Return nil if we aren't in a method definition, otherwise the
6217 ;; position of the initial [+-].
6218 ;;
6219 ;; This function might do hidden buffer changes.
6220 (save-excursion
6221 (beginning-of-line)
6222 (and c-opt-method-key
6223 (looking-at c-opt-method-key)
6224 (point))
6225 ))
6226
6227 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
6228 (defun c-in-gcc-asm-p ()
6229 ;; Return non-nil if point is within a gcc \"asm\" block.
6230 ;;
6231 ;; This should be called with point inside an argument list.
6232 ;;
6233 ;; Only one level of enclosing parentheses is considered, so for
6234 ;; instance `nil' is returned when in a function call within an asm
6235 ;; operand.
6236 ;;
6237 ;; This function might do hidden buffer changes.
6238
6239 (and c-opt-asm-stmt-key
6240 (save-excursion
6241 (beginning-of-line)
6242 (backward-up-list 1)
6243 (c-beginning-of-statement-1 (point-min) nil t)
6244 (looking-at c-opt-asm-stmt-key))))
6245
6246 (defun c-at-toplevel-p ()
6247 "Return a determination as to whether point is at the `top-level'.
6248 Being at the top-level means that point is either outside any
6249 enclosing block (such function definition), or only inside a class,
6250 namespace or other block that contains another declaration level.
6251
6252 If point is not at the top-level (e.g. it is inside a method
6253 definition), then nil is returned. Otherwise, if point is at a
6254 top-level not enclosed within a class definition, t is returned.
6255 Otherwise, a 2-vector is returned where the zeroth element is the
6256 buffer position of the start of the class declaration, and the first
6257 element is the buffer position of the enclosing class's opening
6258 brace.
6259
6260 Note that this function might do hidden buffer changes. See the
6261 comment at the start of cc-engine.el for more info."
6262 (let ((paren-state (c-parse-state)))
6263 (or (not (c-most-enclosing-brace paren-state))
6264 (c-search-uplist-for-classkey paren-state))))
6265
6266 (defun c-just-after-func-arglist-p (&optional lim)
6267 ;; Return non-nil if the point is in the region after the argument
6268 ;; list of a function and its opening brace (or semicolon in case it
6269 ;; got no body). If there are K&R style argument declarations in
6270 ;; that region, the point has to be inside the first one for this
6271 ;; function to recognize it.
6272 ;;
6273 ;; If successful, the point is moved to the first token after the
6274 ;; function header (see `c-forward-decl-or-cast-1' for details) and
6275 ;; the position of the opening paren of the function arglist is
6276 ;; returned.
6277 ;;
6278 ;; The point is clobbered if not successful.
6279 ;;
6280 ;; LIM is used as bound for backward buffer searches.
6281 ;;
6282 ;; This function might do hidden buffer changes.
6283
6284 (let ((beg (point)) end id-start)
6285 (and
6286 (eq (c-beginning-of-statement-1 lim) 'same)
6287
6288 (not (or (c-major-mode-is 'objc-mode)
6289 (c-forward-objc-directive)))
6290
6291 (setq id-start
6292 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
6293 (< id-start beg)
6294
6295 ;; There should not be a '=' or ',' between beg and the
6296 ;; start of the declaration since that means we were in the
6297 ;; "expression part" of the declaration.
6298 (or (> (point) beg)
6299 (not (looking-at "[=,]")))
6300
6301 (save-excursion
6302 ;; Check that there's an arglist paren in the
6303 ;; declaration.
6304 (goto-char id-start)
6305 (cond ((eq (char-after) ?\()
6306 ;; The declarator is a paren expression, so skip past it
6307 ;; so that we don't get stuck on that instead of the
6308 ;; function arglist.
6309 (c-forward-sexp))
6310 ((and c-opt-op-identifier-prefix
6311 (looking-at c-opt-op-identifier-prefix))
6312 ;; Don't trip up on "operator ()".
6313 (c-forward-token-2 2 t)))
6314 (and (< (point) beg)
6315 (c-syntactic-re-search-forward "(" beg t t)
6316 (1- (point)))))))
6317
6318 (defun c-in-knr-argdecl (&optional lim)
6319 ;; Return the position of the first argument declaration if point is
6320 ;; inside a K&R style argument declaration list, nil otherwise.
6321 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
6322 ;; position that bounds the backward search for the argument list.
6323 ;;
6324 ;; Point must be within a possible K&R region, e.g. just before a top-level
6325 ;; "{". It must be outside of parens and brackets. The test can return
6326 ;; false positives otherwise.
6327 ;;
6328 ;; This function might do hidden buffer changes.
6329
6330 (save-excursion
6331 (save-restriction
6332 ;; If we're in a macro, our search range is restricted to it. Narrow to
6333 ;; the searchable range.
6334 (let* ((macro-start (c-query-macro-start))
6335 (lim (max (or lim (point-min)) (or macro-start (point-min))))
6336 before-lparen after-rparen
6337 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
6338 (narrow-to-region lim (c-point 'eol))
6339
6340 ;; Search backwards for the defun's argument list. We give up if we
6341 ;; encounter a "}" (end of a previous defun) or BOB.
6342 ;;
6343 ;; The criterion for a paren structure being the arg list is:
6344 ;; o - there is non-WS stuff after it but before any "{"; AND
6345 ;; o - the token after it isn't a ";" AND
6346 ;; o - it is preceded by either an identifier (the function name) or
6347 ;; a macro expansion like "DEFUN (...)"; AND
6348 ;; o - its content is a non-empty comma-separated list of identifiers
6349 ;; (an empty arg list won't have a knr region).
6350 ;;
6351 ;; The following snippet illustrates these rules:
6352 ;; int foo (bar, baz, yuk)
6353 ;; int bar [] ;
6354 ;; int (*baz) (my_type) ;
6355 ;; int (*) (void) (*yuk) (void) ;
6356 ;; {
6357
6358 (catch 'knr
6359 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
6360 (setq pp-count-out (1- pp-count-out))
6361 (c-syntactic-skip-backward "^)]}")
6362 (cond ((eq (char-before) ?\))
6363 (setq after-rparen (point)))
6364 ((eq (char-before) ?\])
6365 (setq after-rparen nil))
6366 (t ; either } (hit previous defun) or no more parens/brackets
6367 (throw 'knr nil)))
6368
6369 (if after-rparen
6370 ;; We're inside a paren. Could it be our argument list....?
6371 (if
6372 (and
6373 (progn
6374 (goto-char after-rparen)
6375 (unless (c-go-list-backward) (throw 'knr nil)) ;
6376 ;; FIXME!!! What about macros between the parens? 2007/01/20
6377 (setq before-lparen (point)))
6378
6379 ;; It can't be the arg list if next token is ; or {
6380 (progn (goto-char after-rparen)
6381 (c-forward-syntactic-ws)
6382 (not (memq (char-after) '(?\; ?\{))))
6383
6384 ;; Is the thing preceding the list an identifier (the
6385 ;; function name), or a macro expansion?
6386 (progn
6387 (goto-char before-lparen)
6388 (eq (c-backward-token-2) 0)
6389 (or (c-on-identifier)
6390 (and (eq (char-after) ?\))
6391 (c-go-up-list-backward)
6392 (eq (c-backward-token-2) 0)
6393 (c-on-identifier))))
6394
6395 ;; Have we got a non-empty list of comma-separated
6396 ;; identifiers?
6397 (progn
6398 (goto-char before-lparen)
6399 (c-forward-token-2) ; to first token inside parens
6400 (and
6401 (c-on-identifier)
6402 (c-forward-token-2)
6403 (catch 'id-list
6404 (while (eq (char-after) ?\,)
6405 (c-forward-token-2)
6406 (unless (c-on-identifier) (throw 'id-list nil))
6407 (c-forward-token-2))
6408 (eq (char-after) ?\))))))
6409
6410 ;; ...Yes. We've identified the function's argument list.
6411 (throw 'knr
6412 (progn (goto-char after-rparen)
6413 (c-forward-syntactic-ws)
6414 (point)))
6415
6416 ;; ...No. The current parens aren't the function's arg list.
6417 (goto-char before-lparen))
6418
6419 (or (c-go-list-backward) ; backwards over [ .... ]
6420 (throw 'knr nil)))))))))
6421
6422 (defun c-skip-conditional ()
6423 ;; skip forward over conditional at point, including any predicate
6424 ;; statements in parentheses. No error checking is performed.
6425 ;;
6426 ;; This function might do hidden buffer changes.
6427 (c-forward-sexp (cond
6428 ;; else if()
6429 ((looking-at (concat "\\<else"
6430 "\\([ \t\n]\\|\\\\\n\\)+"
6431 "if\\>\\([^_]\\|$\\)"))
6432 3)
6433 ;; do, else, try, finally
6434 ((looking-at (concat "\\<\\("
6435 "do\\|else\\|try\\|finally"
6436 "\\)\\>\\([^_]\\|$\\)"))
6437 1)
6438 ;; for, if, while, switch, catch, synchronized, foreach
6439 (t 2))))
6440
6441 (defun c-after-conditional (&optional lim)
6442 ;; If looking at the token after a conditional then return the
6443 ;; position of its start, otherwise return nil.
6444 ;;
6445 ;; This function might do hidden buffer changes.
6446 (save-excursion
6447 (and (zerop (c-backward-token-2 1 t lim))
6448 (or (looking-at c-block-stmt-1-key)
6449 (and (eq (char-after) ?\()
6450 (zerop (c-backward-token-2 1 t lim))
6451 (looking-at c-block-stmt-2-key)))
6452 (point))))
6453
6454 (defun c-after-special-operator-id (&optional lim)
6455 ;; If the point is after an operator identifier that isn't handled
6456 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
6457 ;; position of the start of that identifier is returned. nil is
6458 ;; returned otherwise. The point may be anywhere in the syntactic
6459 ;; whitespace after the last token of the operator identifier.
6460 ;;
6461 ;; This function might do hidden buffer changes.
6462 (save-excursion
6463 (and c-overloadable-operators-regexp
6464 (zerop (c-backward-token-2 1 nil lim))
6465 (looking-at c-overloadable-operators-regexp)
6466 (or (not c-opt-op-identifier-prefix)
6467 (and
6468 (zerop (c-backward-token-2 1 nil lim))
6469 (looking-at c-opt-op-identifier-prefix)))
6470 (point))))
6471
6472 (defsubst c-backward-to-block-anchor (&optional lim)
6473 ;; Assuming point is at a brace that opens a statement block of some
6474 ;; kind, move to the proper anchor point for that block. It might
6475 ;; need to be adjusted further by c-add-stmt-syntax, but the
6476 ;; position at return is suitable as start position for that
6477 ;; function.
6478 ;;
6479 ;; This function might do hidden buffer changes.
6480 (unless (= (point) (c-point 'boi))
6481 (let ((start (c-after-conditional lim)))
6482 (if start
6483 (goto-char start)))))
6484
6485 (defsubst c-backward-to-decl-anchor (&optional lim)
6486 ;; Assuming point is at a brace that opens the block of a top level
6487 ;; declaration of some kind, move to the proper anchor point for
6488 ;; that block.
6489 ;;
6490 ;; This function might do hidden buffer changes.
6491 (unless (= (point) (c-point 'boi))
6492 (c-beginning-of-statement-1 lim)))
6493
6494 (defun c-search-decl-header-end ()
6495 ;; Search forward for the end of the "header" of the current
6496 ;; declaration. That's the position where the definition body
6497 ;; starts, or the first variable initializer, or the ending
6498 ;; semicolon. I.e. search forward for the closest following
6499 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
6500 ;; _after_ the first found token, or at point-max if none is found.
6501 ;;
6502 ;; This function might do hidden buffer changes.
6503
6504 (let ((base (point)))
6505 (if (c-major-mode-is 'c++-mode)
6506
6507 ;; In C++ we need to take special care to handle operator
6508 ;; tokens and those pesky template brackets.
6509 (while (and
6510 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
6511 (or
6512 (c-end-of-current-token base)
6513 ;; Handle operator identifiers, i.e. ignore any
6514 ;; operator token preceded by "operator".
6515 (save-excursion
6516 (and (c-safe (c-backward-sexp) t)
6517 (looking-at c-opt-op-identifier-prefix)))
6518 (and (eq (char-before) ?<)
6519 (c-with-syntax-table c++-template-syntax-table
6520 (if (c-safe (goto-char (c-up-list-forward (point))))
6521 t
6522 (goto-char (point-max))
6523 nil)))))
6524 (setq base (point)))
6525
6526 (while (and
6527 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
6528 (c-end-of-current-token base))
6529 (setq base (point))))))
6530
6531 (defun c-beginning-of-decl-1 (&optional lim)
6532 ;; Go to the beginning of the current declaration, or the beginning
6533 ;; of the previous one if already at the start of it. Point won't
6534 ;; be moved out of any surrounding paren. Return a cons cell of the
6535 ;; form (MOVE . KNR-POS). MOVE is like the return value from
6536 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
6537 ;; style argument declarations (and they are to be recognized) then
6538 ;; KNR-POS is set to the start of the first such argument
6539 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
6540 ;; position that bounds the backward search.
6541 ;;
6542 ;; NB: Cases where the declaration continues after the block, as in
6543 ;; "struct foo { ... } bar;", are currently recognized as two
6544 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
6545 ;;
6546 ;; This function might do hidden buffer changes.
6547 (catch 'return
6548 (let* ((start (point))
6549 (last-stmt-start (point))
6550 (move (c-beginning-of-statement-1 lim nil t)))
6551
6552 ;; `c-beginning-of-statement-1' stops at a block start, but we
6553 ;; want to continue if the block doesn't begin a top level
6554 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
6555 ;; or an open paren.
6556 (let ((beg (point)) tentative-move)
6557 ;; Go back one "statement" each time round the loop until we're just
6558 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
6559 ;; an ObjC method. This will move over a multiple declaration whose
6560 ;; components are comma separated.
6561 (while (and
6562 ;; Must check with c-opt-method-key in ObjC mode.
6563 (not (and c-opt-method-key
6564 (looking-at c-opt-method-key)))
6565 (/= last-stmt-start (point))
6566 (progn
6567 (c-backward-syntactic-ws lim)
6568 (not (memq (char-before) '(?\; ?} ?: nil))))
6569 (save-excursion
6570 (backward-char)
6571 (not (looking-at "\\s(")))
6572 ;; Check that we don't move from the first thing in a
6573 ;; macro to its header.
6574 (not (eq (setq tentative-move
6575 (c-beginning-of-statement-1 lim nil t))
6576 'macro)))
6577 (setq last-stmt-start beg
6578 beg (point)
6579 move tentative-move))
6580 (goto-char beg))
6581
6582 (when c-recognize-knr-p
6583 (let ((fallback-pos (point)) knr-argdecl-start)
6584 ;; Handle K&R argdecls. Back up after the "statement" jumped
6585 ;; over by `c-beginning-of-statement-1', unless it was the
6586 ;; function body, in which case we're sitting on the opening
6587 ;; brace now. Then test if we're in a K&R argdecl region and
6588 ;; that we started at the other side of the first argdecl in
6589 ;; it.
6590 (unless (eq (char-after) ?{)
6591 (goto-char last-stmt-start))
6592 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
6593 (< knr-argdecl-start start)
6594 (progn
6595 (goto-char knr-argdecl-start)
6596 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
6597 (throw 'return
6598 (cons (if (eq (char-after fallback-pos) ?{)
6599 'previous
6600 'same)
6601 knr-argdecl-start))
6602 (goto-char fallback-pos))))
6603
6604 ;; `c-beginning-of-statement-1' counts each brace block as a separate
6605 ;; statement, so the result will be 'previous if we've moved over any.
6606 ;; So change our result back to 'same if necessary.
6607 ;;
6608 ;; If they were brace list initializers we might not have moved over a
6609 ;; declaration boundary though, so change it to 'same if we've moved
6610 ;; past a '=' before '{', but not ';'. (This ought to be integrated
6611 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
6612 ;; potentially can search over a large amount of text.). Take special
6613 ;; pains not to get mislead by C++'s "operator=", and the like.
6614 (if (and (eq move 'previous)
6615 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
6616 c++-template-syntax-table
6617 (syntax-table))
6618 (save-excursion
6619 (and
6620 (progn
6621 (while ; keep going back to "[;={"s until we either find
6622 ; no more, or get to one which isn't an "operator ="
6623 (and (c-syntactic-re-search-forward "[;={]" start t t t)
6624 (eq (char-before) ?=)
6625 c-overloadable-operators-regexp
6626 c-opt-op-identifier-prefix
6627 (save-excursion
6628 (eq (c-backward-token-2) 0)
6629 (looking-at c-overloadable-operators-regexp)
6630 (eq (c-backward-token-2) 0)
6631 (looking-at c-opt-op-identifier-prefix))))
6632 (eq (char-before) ?=))
6633 (c-syntactic-re-search-forward "[;{]" start t t)
6634 (eq (char-before) ?{)
6635 (c-safe (goto-char (c-up-list-forward (point))) t)
6636 (not (c-syntactic-re-search-forward ";" start t t))))))
6637 (cons 'same nil)
6638 (cons move nil)))))
6639
6640 (defun c-end-of-decl-1 ()
6641 ;; Assuming point is at the start of a declaration (as detected by
6642 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
6643 ;; `c-beginning-of-decl-1', this function handles the case when a
6644 ;; block is followed by identifiers in e.g. struct declarations in C
6645 ;; or C++. If a proper end was found then t is returned, otherwise
6646 ;; point is moved as far as possible within the current sexp and nil
6647 ;; is returned. This function doesn't handle macros; use
6648 ;; `c-end-of-macro' instead in those cases.
6649 ;;
6650 ;; This function might do hidden buffer changes.
6651 (let ((start (point))
6652 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
6653 c++-template-syntax-table
6654 (syntax-table))))
6655 (catch 'return
6656 (c-search-decl-header-end)
6657
6658 (when (and c-recognize-knr-p
6659 (eq (char-before) ?\;)
6660 (c-in-knr-argdecl start))
6661 ;; Stopped at the ';' in a K&R argdecl section which is
6662 ;; detected using the same criteria as in
6663 ;; `c-beginning-of-decl-1'. Move to the following block
6664 ;; start.
6665 (c-syntactic-re-search-forward "{" nil 'move t))
6666
6667 (when (eq (char-before) ?{)
6668 ;; Encountered a block in the declaration. Jump over it.
6669 (condition-case nil
6670 (goto-char (c-up-list-forward (point)))
6671 (error (goto-char (point-max))
6672 (throw 'return nil)))
6673 (if (or (not c-opt-block-decls-with-vars-key)
6674 (save-excursion
6675 (c-with-syntax-table decl-syntax-table
6676 (let ((lim (point)))
6677 (goto-char start)
6678 (not (and
6679 ;; Check for `c-opt-block-decls-with-vars-key'
6680 ;; before the first paren.
6681 (c-syntactic-re-search-forward
6682 (concat "[;=\(\[{]\\|\\("
6683 c-opt-block-decls-with-vars-key
6684 "\\)")
6685 lim t t t)
6686 (match-beginning 1)
6687 (not (eq (char-before) ?_))
6688 ;; Check that the first following paren is
6689 ;; the block.
6690 (c-syntactic-re-search-forward "[;=\(\[{]"
6691 lim t t t)
6692 (eq (char-before) ?{)))))))
6693 ;; The declaration doesn't have any of the
6694 ;; `c-opt-block-decls-with-vars' keywords in the
6695 ;; beginning, so it ends here at the end of the block.
6696 (throw 'return t)))
6697
6698 (c-with-syntax-table decl-syntax-table
6699 (while (progn
6700 (if (eq (char-before) ?\;)
6701 (throw 'return t))
6702 (c-syntactic-re-search-forward ";" nil 'move t))))
6703 nil)))
6704
6705 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
6706 ;; Assuming the point is at an open brace, check if it starts a
6707 ;; block that contains another declaration level, i.e. that isn't a
6708 ;; statement block or a brace list, and if so return non-nil.
6709 ;;
6710 ;; If the check is successful, the return value is the start of the
6711 ;; keyword that tells what kind of construct it is, i.e. typically
6712 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
6713 ;; the point will be at the start of the construct, before any
6714 ;; leading specifiers, otherwise it's at the returned position.
6715 ;;
6716 ;; The point is clobbered if the check is unsuccessful.
6717 ;;
6718 ;; CONTAINING-SEXP is the position of the open of the surrounding
6719 ;; paren, or nil if none.
6720 ;;
6721 ;; The optional LIMIT limits the backward search for the start of
6722 ;; the construct. It's assumed to be at a syntactically relevant
6723 ;; position.
6724 ;;
6725 ;; If any template arglists are found in the searched region before
6726 ;; the open brace, they get marked with paren syntax.
6727 ;;
6728 ;; This function might do hidden buffer changes.
6729
6730 (let ((open-brace (point)) kwd-start first-specifier-pos)
6731 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6732
6733 (when (and c-recognize-<>-arglists
6734 (eq (char-before) ?>))
6735 ;; Could be at the end of a template arglist.
6736 (let ((c-parse-and-markup-<>-arglists t)
6737 (c-disallow-comma-in-<>-arglists
6738 (and containing-sexp
6739 (not (eq (char-after containing-sexp) ?{)))))
6740 (while (and
6741 (c-backward-<>-arglist nil limit)
6742 (progn
6743 (c-syntactic-skip-backward c-block-prefix-charset limit t)
6744 (eq (char-before) ?>))))))
6745
6746 ;; Note: Can't get bogus hits inside template arglists below since they
6747 ;; have gotten paren syntax above.
6748 (when (and
6749 ;; If `goto-start' is set we begin by searching for the
6750 ;; first possible position of a leading specifier list.
6751 ;; The `c-decl-block-key' search continues from there since
6752 ;; we know it can't match earlier.
6753 (if goto-start
6754 (when (c-syntactic-re-search-forward c-symbol-start
6755 open-brace t t)
6756 (goto-char (setq first-specifier-pos (match-beginning 0)))
6757 t)
6758 t)
6759
6760 (cond
6761 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
6762 (goto-char (setq kwd-start (match-beginning 0)))
6763 (or
6764
6765 ;; Found a keyword that can't be a type?
6766 (match-beginning 1)
6767
6768 ;; Can be a type too, in which case it's the return type of a
6769 ;; function (under the assumption that no declaration level
6770 ;; block construct starts with a type).
6771 (not (c-forward-type))
6772
6773 ;; Jumped over a type, but it could be a declaration keyword
6774 ;; followed by the declared identifier that we've jumped over
6775 ;; instead (e.g. in "class Foo {"). If it indeed is a type
6776 ;; then we should be at the declarator now, so check for a
6777 ;; valid declarator start.
6778 ;;
6779 ;; Note: This doesn't cope with the case when a declared
6780 ;; identifier is followed by e.g. '(' in a language where '('
6781 ;; also might be part of a declarator expression. Currently
6782 ;; there's no such language.
6783 (not (or (looking-at c-symbol-start)
6784 (looking-at c-type-decl-prefix-key)))))
6785
6786 ;; In Pike a list of modifiers may be followed by a brace
6787 ;; to make them apply to many identifiers. Note that the
6788 ;; match data will be empty on return in this case.
6789 ((and (c-major-mode-is 'pike-mode)
6790 (progn
6791 (goto-char open-brace)
6792 (= (c-backward-token-2) 0))
6793 (looking-at c-specifier-key)
6794 ;; Use this variant to avoid yet another special regexp.
6795 (c-keyword-member (c-keyword-sym (match-string 1))
6796 'c-modifier-kwds))
6797 (setq kwd-start (point))
6798 t)))
6799
6800 ;; Got a match.
6801
6802 (if goto-start
6803 ;; Back up over any preceding specifiers and their clauses
6804 ;; by going forward from `first-specifier-pos', which is the
6805 ;; earliest possible position where the specifier list can
6806 ;; start.
6807 (progn
6808 (goto-char first-specifier-pos)
6809
6810 (while (< (point) kwd-start)
6811 (if (looking-at c-symbol-key)
6812 ;; Accept any plain symbol token on the ground that
6813 ;; it's a specifier masked through a macro (just
6814 ;; like `c-forward-decl-or-cast-1' skip forward over
6815 ;; such tokens).
6816 ;;
6817 ;; Could be more restrictive wrt invalid keywords,
6818 ;; but that'd only occur in invalid code so there's
6819 ;; no use spending effort on it.
6820 (let ((end (match-end 0)))
6821 (unless (c-forward-keyword-clause 0)
6822 (goto-char end)
6823 (c-forward-syntactic-ws)))
6824
6825 ;; Can't parse a declaration preamble and is still
6826 ;; before `kwd-start'. That means `first-specifier-pos'
6827 ;; was in some earlier construct. Search again.
6828 (if (c-syntactic-re-search-forward c-symbol-start
6829 kwd-start 'move t)
6830 (goto-char (setq first-specifier-pos (match-beginning 0)))
6831 ;; Got no preamble before the block declaration keyword.
6832 (setq first-specifier-pos kwd-start))))
6833
6834 (goto-char first-specifier-pos))
6835 (goto-char kwd-start))
6836
6837 kwd-start)))
6838
6839 (defun c-search-uplist-for-classkey (paren-state)
6840 ;; Check if the closest containing paren sexp is a declaration
6841 ;; block, returning a 2 element vector in that case. Aref 0
6842 ;; contains the bufpos at boi of the class key line, and aref 1
6843 ;; contains the bufpos of the open brace. This function is an
6844 ;; obsolete wrapper for `c-looking-at-decl-block'.
6845 ;;
6846 ;; This function might do hidden buffer changes.
6847 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
6848 (when open-paren-pos
6849 (save-excursion
6850 (goto-char open-paren-pos)
6851 (when (and (eq (char-after) ?{)
6852 (c-looking-at-decl-block
6853 (c-safe-position open-paren-pos paren-state)
6854 nil))
6855 (back-to-indentation)
6856 (vector (point) open-paren-pos))))))
6857
6858 (defun c-inside-bracelist-p (containing-sexp paren-state)
6859 ;; return the buffer position of the beginning of the brace list
6860 ;; statement if we're inside a brace list, otherwise return nil.
6861 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
6862 ;; paren. PAREN-STATE is the remainder of the state of enclosing
6863 ;; braces
6864 ;;
6865 ;; N.B.: This algorithm can potentially get confused by cpp macros
6866 ;; placed in inconvenient locations. It's a trade-off we make for
6867 ;; speed.
6868 ;;
6869 ;; This function might do hidden buffer changes.
6870 (or
6871 ;; This will pick up brace list declarations.
6872 (c-safe
6873 (save-excursion
6874 (goto-char containing-sexp)
6875 (c-forward-sexp -1)
6876 (let (bracepos)
6877 (if (and (or (looking-at c-brace-list-key)
6878 (progn (c-forward-sexp -1)
6879 (looking-at c-brace-list-key)))
6880 (setq bracepos (c-down-list-forward (point)))
6881 (not (c-crosses-statement-barrier-p (point)
6882 (- bracepos 2))))
6883 (point)))))
6884 ;; this will pick up array/aggregate init lists, even if they are nested.
6885 (save-excursion
6886 (let ((class-key
6887 ;; Pike can have class definitions anywhere, so we must
6888 ;; check for the class key here.
6889 (and (c-major-mode-is 'pike-mode)
6890 c-decl-block-key))
6891 bufpos braceassignp lim next-containing)
6892 (while (and (not bufpos)
6893 containing-sexp)
6894 (when paren-state
6895 (if (consp (car paren-state))
6896 (setq lim (cdr (car paren-state))
6897 paren-state (cdr paren-state))
6898 (setq lim (car paren-state)))
6899 (when paren-state
6900 (setq next-containing (car paren-state)
6901 paren-state (cdr paren-state))))
6902 (goto-char containing-sexp)
6903 (if (c-looking-at-inexpr-block next-containing next-containing)
6904 ;; We're in an in-expression block of some kind. Do not
6905 ;; check nesting. We deliberately set the limit to the
6906 ;; containing sexp, so that c-looking-at-inexpr-block
6907 ;; doesn't check for an identifier before it.
6908 (setq containing-sexp nil)
6909 ;; see if the open brace is preceded by = or [...] in
6910 ;; this statement, but watch out for operator=
6911 (setq braceassignp 'dontknow)
6912 (c-backward-token-2 1 t lim)
6913 ;; Checks to do only on the first sexp before the brace.
6914 (when (and c-opt-inexpr-brace-list-key
6915 (eq (char-after) ?\[))
6916 ;; In Java, an initialization brace list may follow
6917 ;; directly after "new Foo[]", so check for a "new"
6918 ;; earlier.
6919 (while (eq braceassignp 'dontknow)
6920 (setq braceassignp
6921 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
6922 ((looking-at c-opt-inexpr-brace-list-key) t)
6923 ((looking-at "\\sw\\|\\s_\\|[.[]")
6924 ;; Carry on looking if this is an
6925 ;; identifier (may contain "." in Java)
6926 ;; or another "[]" sexp.
6927 'dontknow)
6928 (t nil)))))
6929 ;; Checks to do on all sexps before the brace, up to the
6930 ;; beginning of the statement.
6931 (while (eq braceassignp 'dontknow)
6932 (cond ((eq (char-after) ?\;)
6933 (setq braceassignp nil))
6934 ((and class-key
6935 (looking-at class-key))
6936 (setq braceassignp nil))
6937 ((eq (char-after) ?=)
6938 ;; We've seen a =, but must check earlier tokens so
6939 ;; that it isn't something that should be ignored.
6940 (setq braceassignp 'maybe)
6941 (while (and (eq braceassignp 'maybe)
6942 (zerop (c-backward-token-2 1 t lim)))
6943 (setq braceassignp
6944 (cond
6945 ;; Check for operator =
6946 ((and c-opt-op-identifier-prefix
6947 (looking-at c-opt-op-identifier-prefix))
6948 nil)
6949 ;; Check for `<opchar>= in Pike.
6950 ((and (c-major-mode-is 'pike-mode)
6951 (or (eq (char-after) ?`)
6952 ;; Special case for Pikes
6953 ;; `[]=, since '[' is not in
6954 ;; the punctuation class.
6955 (and (eq (char-after) ?\[)
6956 (eq (char-before) ?`))))
6957 nil)
6958 ((looking-at "\\s.") 'maybe)
6959 ;; make sure we're not in a C++ template
6960 ;; argument assignment
6961 ((and
6962 (c-major-mode-is 'c++-mode)
6963 (save-excursion
6964 (let ((here (point))
6965 (pos< (progn
6966 (skip-chars-backward "^<>")
6967 (point))))
6968 (and (eq (char-before) ?<)
6969 (not (c-crosses-statement-barrier-p
6970 pos< here))
6971 (not (c-in-literal))
6972 ))))
6973 nil)
6974 (t t))))))
6975 (if (and (eq braceassignp 'dontknow)
6976 (/= (c-backward-token-2 1 t lim) 0))
6977 (setq braceassignp nil)))
6978 (if (not braceassignp)
6979 (if (eq (char-after) ?\;)
6980 ;; Brace lists can't contain a semicolon, so we're done.
6981 (setq containing-sexp nil)
6982 ;; Go up one level.
6983 (setq containing-sexp next-containing
6984 lim nil
6985 next-containing nil))
6986 ;; we've hit the beginning of the aggregate list
6987 (c-beginning-of-statement-1
6988 (c-most-enclosing-brace paren-state))
6989 (setq bufpos (point))))
6990 )
6991 bufpos))
6992 ))
6993
6994 (defun c-looking-at-special-brace-list (&optional lim)
6995 ;; If we're looking at the start of a pike-style list, ie `({ })',
6996 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
6997 ;; positions and its entry in c-special-brace-lists is returned, nil
6998 ;; otherwise. The ending position is nil if the list is still open.
6999 ;; LIM is the limit for forward search. The point may either be at
7000 ;; the `(' or at the following paren character. Tries to check the
7001 ;; matching closer, but assumes it's correct if no balanced paren is
7002 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
7003 ;; a special brace list).
7004 ;;
7005 ;; This function might do hidden buffer changes.
7006 (if c-special-brace-lists
7007 (condition-case ()
7008 (save-excursion
7009 (let ((beg (point))
7010 inner-beg end type)
7011 (c-forward-syntactic-ws)
7012 (if (eq (char-after) ?\()
7013 (progn
7014 (forward-char 1)
7015 (c-forward-syntactic-ws)
7016 (setq inner-beg (point))
7017 (setq type (assq (char-after) c-special-brace-lists)))
7018 (if (setq type (assq (char-after) c-special-brace-lists))
7019 (progn
7020 (setq inner-beg (point))
7021 (c-backward-syntactic-ws)
7022 (forward-char -1)
7023 (setq beg (if (eq (char-after) ?\()
7024 (point)
7025 nil)))))
7026 (if (and beg type)
7027 (if (and (c-safe
7028 (goto-char beg)
7029 (c-forward-sexp 1)
7030 (setq end (point))
7031 (= (char-before) ?\)))
7032 (c-safe
7033 (goto-char inner-beg)
7034 (if (looking-at "\\s(")
7035 ;; Check balancing of the inner paren
7036 ;; below.
7037 (progn
7038 (c-forward-sexp 1)
7039 t)
7040 ;; If the inner char isn't a paren then
7041 ;; we can't check balancing, so just
7042 ;; check the char before the outer
7043 ;; closing paren.
7044 (goto-char end)
7045 (backward-char)
7046 (c-backward-syntactic-ws)
7047 (= (char-before) (cdr type)))))
7048 (if (or (/= (char-syntax (char-before)) ?\))
7049 (= (progn
7050 (c-forward-syntactic-ws)
7051 (point))
7052 (1- end)))
7053 (cons (cons beg end) type))
7054 (cons (list beg) type)))))
7055 (error nil))))
7056
7057 (defun c-looking-at-bos (&optional lim)
7058 ;; Return non-nil if between two statements or declarations, assuming
7059 ;; point is not inside a literal or comment.
7060 ;;
7061 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
7062 ;; are recommended instead.
7063 ;;
7064 ;; This function might do hidden buffer changes.
7065 (c-at-statement-start-p))
7066 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p)
7067
7068 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
7069 ;; Return non-nil if we're looking at the beginning of a block
7070 ;; inside an expression. The value returned is actually a cons of
7071 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
7072 ;; position of the beginning of the construct.
7073 ;;
7074 ;; LIM limits the backward search. CONTAINING-SEXP is the start
7075 ;; position of the closest containing list. If it's nil, the
7076 ;; containing paren isn't used to decide whether we're inside an
7077 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
7078 ;; needs to be farther back.
7079 ;;
7080 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
7081 ;; brace block might be done. It should only be used when the
7082 ;; construct can be assumed to be complete, i.e. when the original
7083 ;; starting position was further down than that.
7084 ;;
7085 ;; This function might do hidden buffer changes.
7086
7087 (save-excursion
7088 (let ((res 'maybe) passed-paren
7089 (closest-lim (or containing-sexp lim (point-min)))
7090 ;; Look at the character after point only as a last resort
7091 ;; when we can't disambiguate.
7092 (block-follows (and (eq (char-after) ?{) (point))))
7093
7094 (while (and (eq res 'maybe)
7095 (progn (c-backward-syntactic-ws)
7096 (> (point) closest-lim))
7097 (not (bobp))
7098 (progn (backward-char)
7099 (looking-at "[\]\).]\\|\\w\\|\\s_"))
7100 (c-safe (forward-char)
7101 (goto-char (scan-sexps (point) -1))))
7102
7103 (setq res
7104 (if (looking-at c-keywords-regexp)
7105 (let ((kw-sym (c-keyword-sym (match-string 1))))
7106 (cond
7107 ((and block-follows
7108 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
7109 (and (not (eq passed-paren ?\[))
7110 (or (not (looking-at c-class-key))
7111 ;; If the class definition is at the start of
7112 ;; a statement, we don't consider it an
7113 ;; in-expression class.
7114 (let ((prev (point)))
7115 (while (and
7116 (= (c-backward-token-2 1 nil closest-lim) 0)
7117 (eq (char-syntax (char-after)) ?w))
7118 (setq prev (point)))
7119 (goto-char prev)
7120 (not (c-at-statement-start-p)))
7121 ;; Also, in Pike we treat it as an
7122 ;; in-expression class if it's used in an
7123 ;; object clone expression.
7124 (save-excursion
7125 (and check-at-end
7126 (c-major-mode-is 'pike-mode)
7127 (progn (goto-char block-follows)
7128 (zerop (c-forward-token-2 1 t)))
7129 (eq (char-after) ?\())))
7130 (cons 'inexpr-class (point))))
7131 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
7132 (when (not passed-paren)
7133 (cons 'inexpr-statement (point))))
7134 ((c-keyword-member kw-sym 'c-lambda-kwds)
7135 (when (or (not passed-paren)
7136 (eq passed-paren ?\())
7137 (cons 'inlambda (point))))
7138 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
7139 nil)
7140 (t
7141 'maybe)))
7142
7143 (if (looking-at "\\s(")
7144 (if passed-paren
7145 (if (and (eq passed-paren ?\[)
7146 (eq (char-after) ?\[))
7147 ;; Accept several square bracket sexps for
7148 ;; Java array initializations.
7149 'maybe)
7150 (setq passed-paren (char-after))
7151 'maybe)
7152 'maybe))))
7153
7154 (if (eq res 'maybe)
7155 (when (and c-recognize-paren-inexpr-blocks
7156 block-follows
7157 containing-sexp
7158 (eq (char-after containing-sexp) ?\())
7159 (goto-char containing-sexp)
7160 (if (or (save-excursion
7161 (c-backward-syntactic-ws lim)
7162 (and (> (point) (or lim (point-min)))
7163 (c-on-identifier)))
7164 (and c-special-brace-lists
7165 (c-looking-at-special-brace-list)))
7166 nil
7167 (cons 'inexpr-statement (point))))
7168
7169 res))))
7170
7171 (defun c-looking-at-inexpr-block-backward (paren-state)
7172 ;; Returns non-nil if we're looking at the end of an in-expression
7173 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
7174 ;; PAREN-STATE is the paren state relevant at the current position.
7175 ;;
7176 ;; This function might do hidden buffer changes.
7177 (save-excursion
7178 ;; We currently only recognize a block.
7179 (let ((here (point))
7180 (elem (car-safe paren-state))
7181 containing-sexp)
7182 (when (and (consp elem)
7183 (progn (goto-char (cdr elem))
7184 (c-forward-syntactic-ws here)
7185 (= (point) here)))
7186 (goto-char (car elem))
7187 (if (setq paren-state (cdr paren-state))
7188 (setq containing-sexp (car-safe paren-state)))
7189 (c-looking-at-inexpr-block (c-safe-position containing-sexp
7190 paren-state)
7191 containing-sexp)))))
7192
7193 \f
7194 ;; `c-guess-basic-syntax' and the functions that precedes it below
7195 ;; implements the main decision tree for determining the syntactic
7196 ;; analysis of the current line of code.
7197
7198 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
7199 ;; auto newline analysis.
7200 (defvar c-auto-newline-analysis nil)
7201
7202 (defun c-brace-anchor-point (bracepos)
7203 ;; BRACEPOS is the position of a brace in a construct like "namespace
7204 ;; Bar {". Return the anchor point in this construct; this is the
7205 ;; earliest symbol on the brace's line which isn't earlier than
7206 ;; "namespace".
7207 ;;
7208 ;; Currently (2007-08-17), "like namespace" means "matches
7209 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
7210 ;; or anything like that.
7211 (save-excursion
7212 (let ((boi (c-point 'boi bracepos)))
7213 (goto-char bracepos)
7214 (while (and (> (point) boi)
7215 (not (looking-at c-other-decl-block-key)))
7216 (c-backward-token-2))
7217 (if (> (point) boi) (point) boi))))
7218
7219 (defsubst c-add-syntax (symbol &rest args)
7220 ;; A simple function to prepend a new syntax element to
7221 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
7222 ;; should always be dynamically bound but since we read it first
7223 ;; we'll fail properly anyway if this function is misused.
7224 (setq c-syntactic-context (cons (cons symbol args)
7225 c-syntactic-context)))
7226
7227 (defsubst c-append-syntax (symbol &rest args)
7228 ;; Like `c-add-syntax' but appends to the end of the syntax list.
7229 ;; (Normally not necessary.)
7230 (setq c-syntactic-context (nconc c-syntactic-context
7231 (list (cons symbol args)))))
7232
7233 (defun c-add-stmt-syntax (syntax-symbol
7234 syntax-extra-args
7235 stop-at-boi-only
7236 containing-sexp
7237 paren-state)
7238 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
7239 ;; needed with further syntax elements of the types `substatement',
7240 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
7241 ;; `defun-block-intro'.
7242 ;;
7243 ;; Do the generic processing to anchor the given syntax symbol on
7244 ;; the preceding statement: Skip over any labels and containing
7245 ;; statements on the same line, and then search backward until we
7246 ;; find a statement or block start that begins at boi without a
7247 ;; label or comment.
7248 ;;
7249 ;; Point is assumed to be at the prospective anchor point for the
7250 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
7251 ;; skip past open parens and containing statements. Most of the added
7252 ;; syntax elements will get the same anchor point - the exception is
7253 ;; for an anchor in a construct like "namespace"[*] - this is as early
7254 ;; as possible in the construct but on the same line as the {.
7255 ;;
7256 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
7257 ;;
7258 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
7259 ;; syntax symbol. They are appended after the anchor point.
7260 ;;
7261 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
7262 ;; if the current statement starts there.
7263 ;;
7264 ;; Note: It's not a problem if PAREN-STATE "overshoots"
7265 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
7266 ;;
7267 ;; This function might do hidden buffer changes.
7268
7269 (if (= (point) (c-point 'boi))
7270 ;; This is by far the most common case, so let's give it special
7271 ;; treatment.
7272 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
7273
7274 (let ((syntax-last c-syntactic-context)
7275 (boi (c-point 'boi))
7276 ;; Set when we're on a label, so that we don't stop there.
7277 ;; FIXME: To be complete we should check if we're on a label
7278 ;; now at the start.
7279 on-label)
7280
7281 ;; Use point as the anchor point for "namespace", "extern", etc.
7282 (apply 'c-add-syntax syntax-symbol
7283 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
7284 (point) nil)
7285 syntax-extra-args)
7286
7287 ;; Loop while we have to back out of containing blocks.
7288 (while
7289 (and
7290 (catch 'back-up-block
7291
7292 ;; Loop while we have to back up statements.
7293 (while (or (/= (point) boi)
7294 on-label
7295 (looking-at c-comment-start-regexp))
7296
7297 ;; Skip past any comments that stands between the
7298 ;; statement start and boi.
7299 (let ((savepos (point)))
7300 (while (and (/= savepos boi)
7301 (c-backward-single-comment))
7302 (setq savepos (point)
7303 boi (c-point 'boi)))
7304 (goto-char savepos))
7305
7306 ;; Skip to the beginning of this statement or backward
7307 ;; another one.
7308 (let ((old-pos (point))
7309 (old-boi boi)
7310 (step-type (c-beginning-of-statement-1 containing-sexp)))
7311 (setq boi (c-point 'boi)
7312 on-label (eq step-type 'label))
7313
7314 (cond ((= (point) old-pos)
7315 ;; If we didn't move we're at the start of a block and
7316 ;; have to continue outside it.
7317 (throw 'back-up-block t))
7318
7319 ((and (eq step-type 'up)
7320 (>= (point) old-boi)
7321 (looking-at "else\\>[^_]")
7322 (save-excursion
7323 (goto-char old-pos)
7324 (looking-at "if\\>[^_]")))
7325 ;; Special case to avoid deeper and deeper indentation
7326 ;; of "else if" clauses.
7327 )
7328
7329 ((and (not stop-at-boi-only)
7330 (/= old-pos old-boi)
7331 (memq step-type '(up previous)))
7332 ;; If stop-at-boi-only is nil, we shouldn't back up
7333 ;; over previous or containing statements to try to
7334 ;; reach boi, so go back to the last position and
7335 ;; exit.
7336 (goto-char old-pos)
7337 (throw 'back-up-block nil))
7338
7339 (t
7340 (if (and (not stop-at-boi-only)
7341 (memq step-type '(up previous beginning)))
7342 ;; If we've moved into another statement then we
7343 ;; should no longer try to stop in the middle of a
7344 ;; line.
7345 (setq stop-at-boi-only t))
7346
7347 ;; Record this as a substatement if we skipped up one
7348 ;; level.
7349 (when (eq step-type 'up)
7350 (c-add-syntax 'substatement nil))))
7351 )))
7352
7353 containing-sexp)
7354
7355 ;; Now we have to go out of this block.
7356 (goto-char containing-sexp)
7357
7358 ;; Don't stop in the middle of a special brace list opener
7359 ;; like "({".
7360 (when c-special-brace-lists
7361 (let ((special-list (c-looking-at-special-brace-list)))
7362 (when (and special-list
7363 (< (car (car special-list)) (point)))
7364 (setq containing-sexp (car (car special-list)))
7365 (goto-char containing-sexp))))
7366
7367 (setq paren-state (c-whack-state-after containing-sexp paren-state)
7368 containing-sexp (c-most-enclosing-brace paren-state)
7369 boi (c-point 'boi))
7370
7371 ;; Analyze the construct in front of the block we've stepped out
7372 ;; from and add the right syntactic element for it.
7373 (let ((paren-pos (point))
7374 (paren-char (char-after))
7375 step-type)
7376
7377 (if (eq paren-char ?\()
7378 ;; Stepped out of a parenthesis block, so we're in an
7379 ;; expression now.
7380 (progn
7381 (when (/= paren-pos boi)
7382 (if (and c-recognize-paren-inexpr-blocks
7383 (progn
7384 (c-backward-syntactic-ws containing-sexp)
7385 (or (not (looking-at "\\>"))
7386 (not (c-on-identifier))))
7387 (save-excursion
7388 (goto-char (1+ paren-pos))
7389 (c-forward-syntactic-ws)
7390 (eq (char-after) ?{)))
7391 ;; Stepped out of an in-expression statement. This
7392 ;; syntactic element won't get an anchor pos.
7393 (c-add-syntax 'inexpr-statement)
7394
7395 ;; A parenthesis normally belongs to an arglist.
7396 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
7397
7398 (goto-char (max boi
7399 (if containing-sexp
7400 (1+ containing-sexp)
7401 (point-min))))
7402 (setq step-type 'same
7403 on-label nil))
7404
7405 ;; Stepped out of a brace block.
7406 (setq step-type (c-beginning-of-statement-1 containing-sexp)
7407 on-label (eq step-type 'label))
7408
7409 (if (and (eq step-type 'same)
7410 (/= paren-pos (point)))
7411 (let (inexpr)
7412 (cond
7413 ((save-excursion
7414 (goto-char paren-pos)
7415 (setq inexpr (c-looking-at-inexpr-block
7416 (c-safe-position containing-sexp paren-state)
7417 containing-sexp)))
7418 (c-add-syntax (if (eq (car inexpr) 'inlambda)
7419 'defun-block-intro
7420 'statement-block-intro)
7421 nil))
7422 ((looking-at c-other-decl-block-key)
7423 (c-add-syntax
7424 (cdr (assoc (match-string 1)
7425 c-other-decl-block-key-in-symbols-alist))
7426 (max (c-point 'boi paren-pos) (point))))
7427 (t (c-add-syntax 'defun-block-intro nil))))
7428
7429 (c-add-syntax 'statement-block-intro nil)))
7430
7431 (if (= paren-pos boi)
7432 ;; Always done if the open brace was at boi. The
7433 ;; c-beginning-of-statement-1 call above is necessary
7434 ;; anyway, to decide the type of block-intro to add.
7435 (goto-char paren-pos)
7436 (setq boi (c-point 'boi)))
7437 ))
7438
7439 ;; Fill in the current point as the anchor for all the symbols
7440 ;; added above.
7441 (let ((p c-syntactic-context) q)
7442 (while (not (eq p syntax-last))
7443 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
7444 (while q
7445 (unless (car q)
7446 (setcar q (point)))
7447 (setq q (cdr q)))
7448 (setq p (cdr p))))
7449 )))
7450
7451 (defun c-add-class-syntax (symbol
7452 containing-decl-open
7453 containing-decl-start
7454 containing-decl-kwd
7455 paren-state)
7456 ;; The inclass and class-close syntactic symbols are added in
7457 ;; several places and some work is needed to fix everything.
7458 ;; Therefore it's collected here.
7459 ;;
7460 ;; This function might do hidden buffer changes.
7461 (goto-char containing-decl-open)
7462 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
7463 (progn
7464 (c-add-syntax symbol containing-decl-open)
7465 containing-decl-open)
7466 (goto-char containing-decl-start)
7467 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
7468 ;; here, but we have to do like this for compatibility.
7469 (back-to-indentation)
7470 (c-add-syntax symbol (point))
7471 (if (and (c-keyword-member containing-decl-kwd
7472 'c-inexpr-class-kwds)
7473 (/= containing-decl-start (c-point 'boi containing-decl-start)))
7474 (c-add-syntax 'inexpr-class))
7475 (point)))
7476
7477 (defun c-guess-continued-construct (indent-point
7478 char-after-ip
7479 beg-of-same-or-containing-stmt
7480 containing-sexp
7481 paren-state)
7482 ;; This function contains the decision tree reached through both
7483 ;; cases 18 and 10. It's a continued statement or top level
7484 ;; construct of some kind.
7485 ;;
7486 ;; This function might do hidden buffer changes.
7487
7488 (let (special-brace-list)
7489 (goto-char indent-point)
7490 (skip-chars-forward " \t")
7491
7492 (cond
7493 ;; (CASE A removed.)
7494 ;; CASE B: open braces for class or brace-lists
7495 ((setq special-brace-list
7496 (or (and c-special-brace-lists
7497 (c-looking-at-special-brace-list))
7498 (eq char-after-ip ?{)))
7499
7500 (cond
7501 ;; CASE B.1: class-open
7502 ((save-excursion
7503 (and (eq (char-after) ?{)
7504 (c-looking-at-decl-block containing-sexp t)
7505 (setq beg-of-same-or-containing-stmt (point))))
7506 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
7507
7508 ;; CASE B.2: brace-list-open
7509 ((or (consp special-brace-list)
7510 (save-excursion
7511 (goto-char beg-of-same-or-containing-stmt)
7512 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
7513 indent-point t t t)))
7514 ;; The most semantically accurate symbol here is
7515 ;; brace-list-open, but we normally report it simply as a
7516 ;; statement-cont. The reason is that one normally adjusts
7517 ;; brace-list-open for brace lists as top-level constructs,
7518 ;; and brace lists inside statements is a completely different
7519 ;; context. C.f. case 5A.3.
7520 (c-beginning-of-statement-1 containing-sexp)
7521 (c-add-stmt-syntax (if c-auto-newline-analysis
7522 ;; Turn off the dwim above when we're
7523 ;; analyzing the nature of the brace
7524 ;; for the auto newline feature.
7525 'brace-list-open
7526 'statement-cont)
7527 nil nil
7528 containing-sexp paren-state))
7529
7530 ;; CASE B.3: The body of a function declared inside a normal
7531 ;; block. Can occur e.g. in Pike and when using gcc
7532 ;; extensions, but watch out for macros followed by blocks.
7533 ;; C.f. cases E, 16F and 17G.
7534 ((and (not (c-at-statement-start-p))
7535 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7536 'same)
7537 (save-excursion
7538 (let ((c-recognize-typeless-decls nil))
7539 ;; Turn off recognition of constructs that lacks a
7540 ;; type in this case, since that's more likely to be
7541 ;; a macro followed by a block.
7542 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7543 (c-add-stmt-syntax 'defun-open nil t
7544 containing-sexp paren-state))
7545
7546 ;; CASE B.4: Continued statement with block open. The most
7547 ;; accurate analysis is perhaps `statement-cont' together with
7548 ;; `block-open' but we play DWIM and use `substatement-open'
7549 ;; instead. The rationaly is that this typically is a macro
7550 ;; followed by a block which makes it very similar to a
7551 ;; statement with a substatement block.
7552 (t
7553 (c-add-stmt-syntax 'substatement-open nil nil
7554 containing-sexp paren-state))
7555 ))
7556
7557 ;; CASE C: iostream insertion or extraction operator
7558 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
7559 (save-excursion
7560 (goto-char beg-of-same-or-containing-stmt)
7561 ;; If there is no preceding streamop in the statement
7562 ;; then indent this line as a normal statement-cont.
7563 (when (c-syntactic-re-search-forward
7564 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
7565 (c-add-syntax 'stream-op (c-point 'boi))
7566 t))))
7567
7568 ;; CASE E: In the "K&R region" of a function declared inside a
7569 ;; normal block. C.f. case B.3.
7570 ((and (save-excursion
7571 ;; Check that the next token is a '{'. This works as
7572 ;; long as no language that allows nested function
7573 ;; definitions allows stuff like member init lists, K&R
7574 ;; declarations or throws clauses there.
7575 ;;
7576 ;; Note that we do a forward search for something ahead
7577 ;; of the indentation line here. That's not good since
7578 ;; the user might not have typed it yet. Unfortunately
7579 ;; it's exceedingly tricky to recognize a function
7580 ;; prototype in a code block without resorting to this.
7581 (c-forward-syntactic-ws)
7582 (eq (char-after) ?{))
7583 (not (c-at-statement-start-p))
7584 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
7585 'same)
7586 (save-excursion
7587 (let ((c-recognize-typeless-decls nil))
7588 ;; Turn off recognition of constructs that lacks a
7589 ;; type in this case, since that's more likely to be
7590 ;; a macro followed by a block.
7591 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
7592 (c-add-stmt-syntax 'func-decl-cont nil t
7593 containing-sexp paren-state))
7594
7595 ;; CASE D: continued statement.
7596 (t
7597 (c-beginning-of-statement-1 containing-sexp)
7598 (c-add-stmt-syntax 'statement-cont nil nil
7599 containing-sexp paren-state))
7600 )))
7601
7602 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
7603 ;; 2005/11/29).
7604 ;;;###autoload
7605 (defun c-guess-basic-syntax ()
7606 "Return the syntactic context of the current line."
7607 (save-excursion
7608 (beginning-of-line)
7609 (c-save-buffer-state
7610 ((indent-point (point))
7611 (case-fold-search nil)
7612 ;; A whole ugly bunch of various temporary variables. Have
7613 ;; to declare them here since it's not possible to declare
7614 ;; a variable with only the scope of a cond test and the
7615 ;; following result clauses, and most of this function is a
7616 ;; single gigantic cond. :P
7617 literal char-before-ip before-ws-ip char-after-ip macro-start
7618 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
7619 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
7620 ;; The following record some positions for the containing
7621 ;; declaration block if we're directly within one:
7622 ;; `containing-decl-open' is the position of the open
7623 ;; brace. `containing-decl-start' is the start of the
7624 ;; declaration. `containing-decl-kwd' is the keyword
7625 ;; symbol of the keyword that tells what kind of block it
7626 ;; is.
7627 containing-decl-open
7628 containing-decl-start
7629 containing-decl-kwd
7630 ;; The open paren of the closest surrounding sexp or nil if
7631 ;; there is none.
7632 containing-sexp
7633 ;; The position after the closest preceding brace sexp
7634 ;; (nested sexps are ignored), or the position after
7635 ;; `containing-sexp' if there is none, or (point-min) if
7636 ;; `containing-sexp' is nil.
7637 lim
7638 ;; The paren state outside `containing-sexp', or at
7639 ;; `indent-point' if `containing-sexp' is nil.
7640 (paren-state (c-parse-state))
7641 ;; There's always at most one syntactic element which got
7642 ;; an anchor pos. It's stored in syntactic-relpos.
7643 syntactic-relpos
7644 (c-stmt-delim-chars c-stmt-delim-chars))
7645
7646 ;; Check if we're directly inside an enclosing declaration
7647 ;; level block.
7648 (when (and (setq containing-sexp
7649 (c-most-enclosing-brace paren-state))
7650 (progn
7651 (goto-char containing-sexp)
7652 (eq (char-after) ?{))
7653 (setq placeholder
7654 (c-looking-at-decl-block
7655 (c-most-enclosing-brace paren-state
7656 containing-sexp)
7657 t)))
7658 (setq containing-decl-open containing-sexp
7659 containing-decl-start (point)
7660 containing-sexp nil)
7661 (goto-char placeholder)
7662 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
7663 (c-keyword-sym (match-string 1)))))
7664
7665 ;; Init some position variables.
7666 (if c-state-cache
7667 (progn
7668 (setq containing-sexp (car paren-state)
7669 paren-state (cdr paren-state))
7670 (if (consp containing-sexp)
7671 (progn
7672 (setq lim (cdr containing-sexp))
7673 (if (cdr c-state-cache)
7674 ;; Ignore balanced paren. The next entry
7675 ;; can't be another one.
7676 (setq containing-sexp (car (cdr c-state-cache))
7677 paren-state (cdr paren-state))
7678 ;; If there is no surrounding open paren then
7679 ;; put the last balanced pair back on paren-state.
7680 (setq paren-state (cons containing-sexp paren-state)
7681 containing-sexp nil)))
7682 (setq lim (1+ containing-sexp))))
7683 (setq lim (point-min)))
7684
7685 ;; If we're in a parenthesis list then ',' delimits the
7686 ;; "statements" rather than being an operator (with the
7687 ;; exception of the "for" clause). This difference is
7688 ;; typically only noticeable when statements are used in macro
7689 ;; arglists.
7690 (when (and containing-sexp
7691 (eq (char-after containing-sexp) ?\())
7692 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
7693
7694 ;; cache char before and after indent point, and move point to
7695 ;; the most likely position to perform the majority of tests
7696 (goto-char indent-point)
7697 (c-backward-syntactic-ws lim)
7698 (setq before-ws-ip (point)
7699 char-before-ip (char-before))
7700 (goto-char indent-point)
7701 (skip-chars-forward " \t")
7702 (setq char-after-ip (char-after))
7703
7704 ;; are we in a literal?
7705 (setq literal (c-in-literal lim))
7706
7707 ;; now figure out syntactic qualities of the current line
7708 (cond
7709
7710 ;; CASE 1: in a string.
7711 ((eq literal 'string)
7712 (c-add-syntax 'string (c-point 'bopl)))
7713
7714 ;; CASE 2: in a C or C++ style comment.
7715 ((and (memq literal '(c c++))
7716 ;; This is a kludge for XEmacs where we use
7717 ;; `buffer-syntactic-context', which doesn't correctly
7718 ;; recognize "\*/" to end a block comment.
7719 ;; `parse-partial-sexp' which is used by
7720 ;; `c-literal-limits' will however do that in most
7721 ;; versions, which results in that we get nil from
7722 ;; `c-literal-limits' even when `c-in-literal' claims
7723 ;; we're inside a comment.
7724 (setq placeholder (c-literal-limits lim)))
7725 (c-add-syntax literal (car placeholder)))
7726
7727 ;; CASE 3: in a cpp preprocessor macro continuation.
7728 ((and (save-excursion
7729 (when (c-beginning-of-macro)
7730 (setq macro-start (point))))
7731 (/= macro-start (c-point 'boi))
7732 (progn
7733 (setq tmpsymbol 'cpp-macro-cont)
7734 (or (not c-syntactic-indentation-in-macros)
7735 (save-excursion
7736 (goto-char macro-start)
7737 ;; If at the beginning of the body of a #define
7738 ;; directive then analyze as cpp-define-intro
7739 ;; only. Go on with the syntactic analysis
7740 ;; otherwise. in-macro-expr is set if we're in a
7741 ;; cpp expression, i.e. before the #define body
7742 ;; or anywhere in a non-#define directive.
7743 (if (c-forward-to-cpp-define-body)
7744 (let ((indent-boi (c-point 'boi indent-point)))
7745 (setq in-macro-expr (> (point) indent-boi)
7746 tmpsymbol 'cpp-define-intro)
7747 (= (point) indent-boi))
7748 (setq in-macro-expr t)
7749 nil)))))
7750 (c-add-syntax tmpsymbol macro-start)
7751 (setq macro-start nil))
7752
7753 ;; CASE 11: an else clause?
7754 ((looking-at "else\\>[^_]")
7755 (c-beginning-of-statement-1 containing-sexp)
7756 (c-add-stmt-syntax 'else-clause nil t
7757 containing-sexp paren-state))
7758
7759 ;; CASE 12: while closure of a do/while construct?
7760 ((and (looking-at "while\\>[^_]")
7761 (save-excursion
7762 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
7763 'beginning)
7764 (setq placeholder (point)))))
7765 (goto-char placeholder)
7766 (c-add-stmt-syntax 'do-while-closure nil t
7767 containing-sexp paren-state))
7768
7769 ;; CASE 13: A catch or finally clause? This case is simpler
7770 ;; than if-else and do-while, because a block is required
7771 ;; after every try, catch and finally.
7772 ((save-excursion
7773 (and (cond ((c-major-mode-is 'c++-mode)
7774 (looking-at "catch\\>[^_]"))
7775 ((c-major-mode-is 'java-mode)
7776 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
7777 (and (c-safe (c-backward-syntactic-ws)
7778 (c-backward-sexp)
7779 t)
7780 (eq (char-after) ?{)
7781 (c-safe (c-backward-syntactic-ws)
7782 (c-backward-sexp)
7783 t)
7784 (if (eq (char-after) ?\()
7785 (c-safe (c-backward-sexp) t)
7786 t))
7787 (looking-at "\\(try\\|catch\\)\\>[^_]")
7788 (setq placeholder (point))))
7789 (goto-char placeholder)
7790 (c-add-stmt-syntax 'catch-clause nil t
7791 containing-sexp paren-state))
7792
7793 ;; CASE 18: A substatement we can recognize by keyword.
7794 ((save-excursion
7795 (and c-opt-block-stmt-key
7796 (not (eq char-before-ip ?\;))
7797 (not (c-at-vsemi-p before-ws-ip))
7798 (not (memq char-after-ip '(?\) ?\] ?,)))
7799 (or (not (eq char-before-ip ?}))
7800 (c-looking-at-inexpr-block-backward c-state-cache))
7801 (> (point)
7802 (progn
7803 ;; Ought to cache the result from the
7804 ;; c-beginning-of-statement-1 calls here.
7805 (setq placeholder (point))
7806 (while (eq (setq step-type
7807 (c-beginning-of-statement-1 lim))
7808 'label))
7809 (if (eq step-type 'previous)
7810 (goto-char placeholder)
7811 (setq placeholder (point))
7812 (if (and (eq step-type 'same)
7813 (not (looking-at c-opt-block-stmt-key)))
7814 ;; Step up to the containing statement if we
7815 ;; stayed in the same one.
7816 (let (step)
7817 (while (eq
7818 (setq step
7819 (c-beginning-of-statement-1 lim))
7820 'label))
7821 (if (eq step 'up)
7822 (setq placeholder (point))
7823 ;; There was no containing statement afterall.
7824 (goto-char placeholder)))))
7825 placeholder))
7826 (if (looking-at c-block-stmt-2-key)
7827 ;; Require a parenthesis after these keywords.
7828 ;; Necessary to catch e.g. synchronized in Java,
7829 ;; which can be used both as statement and
7830 ;; modifier.
7831 (and (zerop (c-forward-token-2 1 nil))
7832 (eq (char-after) ?\())
7833 (looking-at c-opt-block-stmt-key))))
7834
7835 (if (eq step-type 'up)
7836 ;; CASE 18A: Simple substatement.
7837 (progn
7838 (goto-char placeholder)
7839 (cond
7840 ((eq char-after-ip ?{)
7841 (c-add-stmt-syntax 'substatement-open nil nil
7842 containing-sexp paren-state))
7843 ((save-excursion
7844 (goto-char indent-point)
7845 (back-to-indentation)
7846 (c-forward-label))
7847 (c-add-stmt-syntax 'substatement-label nil nil
7848 containing-sexp paren-state))
7849 (t
7850 (c-add-stmt-syntax 'substatement nil nil
7851 containing-sexp paren-state))))
7852
7853 ;; CASE 18B: Some other substatement. This is shared
7854 ;; with case 10.
7855 (c-guess-continued-construct indent-point
7856 char-after-ip
7857 placeholder
7858 lim
7859 paren-state)))
7860
7861 ;; CASE 14: A case or default label
7862 ((looking-at c-label-kwds-regexp)
7863 (if containing-sexp
7864 (progn
7865 (goto-char containing-sexp)
7866 (setq lim (c-most-enclosing-brace c-state-cache
7867 containing-sexp))
7868 (c-backward-to-block-anchor lim)
7869 (c-add-stmt-syntax 'case-label nil t lim paren-state))
7870 ;; Got a bogus label at the top level. In lack of better
7871 ;; alternatives, anchor it on (point-min).
7872 (c-add-syntax 'case-label (point-min))))
7873
7874 ;; CASE 15: any other label
7875 ((save-excursion
7876 (back-to-indentation)
7877 (and (not (looking-at c-syntactic-ws-start))
7878 (c-forward-label)))
7879 (cond (containing-decl-open
7880 (setq placeholder (c-add-class-syntax 'inclass
7881 containing-decl-open
7882 containing-decl-start
7883 containing-decl-kwd
7884 paren-state))
7885 ;; Append access-label with the same anchor point as
7886 ;; inclass gets.
7887 (c-append-syntax 'access-label placeholder))
7888
7889 (containing-sexp
7890 (goto-char containing-sexp)
7891 (setq lim (c-most-enclosing-brace c-state-cache
7892 containing-sexp))
7893 (save-excursion
7894 (setq tmpsymbol
7895 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
7896 (looking-at "switch\\>[^_]"))
7897 ;; If the surrounding statement is a switch then
7898 ;; let's analyze all labels as switch labels, so
7899 ;; that they get lined up consistently.
7900 'case-label
7901 'label)))
7902 (c-backward-to-block-anchor lim)
7903 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
7904
7905 (t
7906 ;; A label on the top level. Treat it as a class
7907 ;; context. (point-min) is the closest we get to the
7908 ;; class open brace.
7909 (c-add-syntax 'access-label (point-min)))))
7910
7911 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
7912 ;; 17E.
7913 ((setq placeholder (c-looking-at-inexpr-block
7914 (c-safe-position containing-sexp paren-state)
7915 containing-sexp
7916 ;; Have to turn on the heuristics after
7917 ;; the point even though it doesn't work
7918 ;; very well. C.f. test case class-16.pike.
7919 t))
7920 (setq tmpsymbol (assq (car placeholder)
7921 '((inexpr-class . class-open)
7922 (inexpr-statement . block-open))))
7923 (if tmpsymbol
7924 ;; It's a statement block or an anonymous class.
7925 (setq tmpsymbol (cdr tmpsymbol))
7926 ;; It's a Pike lambda. Check whether we are between the
7927 ;; lambda keyword and the argument list or at the defun
7928 ;; opener.
7929 (setq tmpsymbol (if (eq char-after-ip ?{)
7930 'inline-open
7931 'lambda-intro-cont)))
7932 (goto-char (cdr placeholder))
7933 (back-to-indentation)
7934 (c-add-stmt-syntax tmpsymbol nil t
7935 (c-most-enclosing-brace c-state-cache (point))
7936 paren-state)
7937 (unless (eq (point) (cdr placeholder))
7938 (c-add-syntax (car placeholder))))
7939
7940 ;; CASE 5: Line is inside a declaration level block or at top level.
7941 ((or containing-decl-open (null containing-sexp))
7942 (cond
7943
7944 ;; CASE 5A: we are looking at a defun, brace list, class,
7945 ;; or inline-inclass method opening brace
7946 ((setq special-brace-list
7947 (or (and c-special-brace-lists
7948 (c-looking-at-special-brace-list))
7949 (eq char-after-ip ?{)))
7950 (cond
7951
7952 ;; CASE 5A.1: Non-class declaration block open.
7953 ((save-excursion
7954 (let (tmp)
7955 (and (eq char-after-ip ?{)
7956 (setq tmp (c-looking-at-decl-block containing-sexp t))
7957 (progn
7958 (setq placeholder (point))
7959 (goto-char tmp)
7960 (looking-at c-symbol-key))
7961 (c-keyword-member
7962 (c-keyword-sym (setq keyword (match-string 0)))
7963 'c-other-block-decl-kwds))))
7964 (goto-char placeholder)
7965 (c-add-stmt-syntax
7966 (if (string-equal keyword "extern")
7967 ;; Special case for extern-lang-open.
7968 'extern-lang-open
7969 (intern (concat keyword "-open")))
7970 nil t containing-sexp paren-state))
7971
7972 ;; CASE 5A.2: we are looking at a class opening brace
7973 ((save-excursion
7974 (goto-char indent-point)
7975 (skip-chars-forward " \t")
7976 (and (eq (char-after) ?{)
7977 (c-looking-at-decl-block containing-sexp t)
7978 (setq placeholder (point))))
7979 (c-add-syntax 'class-open placeholder))
7980
7981 ;; CASE 5A.3: brace list open
7982 ((save-excursion
7983 (c-beginning-of-decl-1 lim)
7984 (while (looking-at c-specifier-key)
7985 (goto-char (match-end 1))
7986 (c-forward-syntactic-ws indent-point))
7987 (setq placeholder (c-point 'boi))
7988 (or (consp special-brace-list)
7989 (and (or (save-excursion
7990 (goto-char indent-point)
7991 (setq tmpsymbol nil)
7992 (while (and (> (point) placeholder)
7993 (zerop (c-backward-token-2 1 t))
7994 (/= (char-after) ?=))
7995 (and c-opt-inexpr-brace-list-key
7996 (not tmpsymbol)
7997 (looking-at c-opt-inexpr-brace-list-key)
7998 (setq tmpsymbol 'topmost-intro-cont)))
7999 (eq (char-after) ?=))
8000 (looking-at c-brace-list-key))
8001 (save-excursion
8002 (while (and (< (point) indent-point)
8003 (zerop (c-forward-token-2 1 t))
8004 (not (memq (char-after) '(?\; ?\()))))
8005 (not (memq (char-after) '(?\; ?\()))
8006 ))))
8007 (if (and (not c-auto-newline-analysis)
8008 (c-major-mode-is 'java-mode)
8009 (eq tmpsymbol 'topmost-intro-cont))
8010 ;; We're in Java and have found that the open brace
8011 ;; belongs to a "new Foo[]" initialization list,
8012 ;; which means the brace list is part of an
8013 ;; expression and not a top level definition. We
8014 ;; therefore treat it as any topmost continuation
8015 ;; even though the semantically correct symbol still
8016 ;; is brace-list-open, on the same grounds as in
8017 ;; case B.2.
8018 (progn
8019 (c-beginning-of-statement-1 lim)
8020 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8021 (c-add-syntax 'brace-list-open placeholder)))
8022
8023 ;; CASE 5A.4: inline defun open
8024 ((and containing-decl-open
8025 (not (c-keyword-member containing-decl-kwd
8026 'c-other-block-decl-kwds)))
8027 (c-add-syntax 'inline-open)
8028 (c-add-class-syntax 'inclass
8029 containing-decl-open
8030 containing-decl-start
8031 containing-decl-kwd
8032 paren-state))
8033
8034 ;; CASE 5A.5: ordinary defun open
8035 (t
8036 (save-excursion
8037 (c-beginning-of-decl-1 lim)
8038 (while (looking-at c-specifier-key)
8039 (goto-char (match-end 1))
8040 (c-forward-syntactic-ws indent-point))
8041 (c-add-syntax 'defun-open (c-point 'boi))
8042 ;; Bogus to use bol here, but it's the legacy. (Resolved,
8043 ;; 2007-11-09)
8044 ))))
8045
8046 ;; CASE 5B: After a function header but before the body (or
8047 ;; the ending semicolon if there's no body).
8048 ((save-excursion
8049 (when (setq placeholder (c-just-after-func-arglist-p lim))
8050 (setq tmp-pos (point))))
8051 (cond
8052
8053 ;; CASE 5B.1: Member init list.
8054 ((eq (char-after tmp-pos) ?:)
8055 (if (or (> tmp-pos indent-point)
8056 (= (c-point 'bosws) (1+ tmp-pos)))
8057 (progn
8058 ;; There is no preceding member init clause.
8059 ;; Indent relative to the beginning of indentation
8060 ;; for the topmost-intro line that contains the
8061 ;; prototype's open paren.
8062 (goto-char placeholder)
8063 (c-add-syntax 'member-init-intro (c-point 'boi)))
8064 ;; Indent relative to the first member init clause.
8065 (goto-char (1+ tmp-pos))
8066 (c-forward-syntactic-ws)
8067 (c-add-syntax 'member-init-cont (point))))
8068
8069 ;; CASE 5B.2: K&R arg decl intro
8070 ((and c-recognize-knr-p
8071 (c-in-knr-argdecl lim))
8072 (c-beginning-of-statement-1 lim)
8073 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
8074 (if containing-decl-open
8075 (c-add-class-syntax 'inclass
8076 containing-decl-open
8077 containing-decl-start
8078 containing-decl-kwd
8079 paren-state)))
8080
8081 ;; CASE 5B.4: Nether region after a C++ or Java func
8082 ;; decl, which could include a `throws' declaration.
8083 (t
8084 (c-beginning-of-statement-1 lim)
8085 (c-add-syntax 'func-decl-cont (c-point 'boi))
8086 )))
8087
8088 ;; CASE 5C: inheritance line. could be first inheritance
8089 ;; line, or continuation of a multiple inheritance
8090 ((or (and (c-major-mode-is 'c++-mode)
8091 (progn
8092 (when (eq char-after-ip ?,)
8093 (skip-chars-forward " \t")
8094 (forward-char))
8095 (looking-at c-opt-postfix-decl-spec-key)))
8096 (and (or (eq char-before-ip ?:)
8097 ;; watch out for scope operator
8098 (save-excursion
8099 (and (eq char-after-ip ?:)
8100 (c-safe (forward-char 1) t)
8101 (not (eq (char-after) ?:))
8102 )))
8103 (save-excursion
8104 (c-backward-syntactic-ws lim)
8105 (if (eq char-before-ip ?:)
8106 (progn
8107 (forward-char -1)
8108 (c-backward-syntactic-ws lim)))
8109 (back-to-indentation)
8110 (looking-at c-class-key)))
8111 ;; for Java
8112 (and (c-major-mode-is 'java-mode)
8113 (let ((fence (save-excursion
8114 (c-beginning-of-statement-1 lim)
8115 (point)))
8116 cont done)
8117 (save-excursion
8118 (while (not done)
8119 (cond ((looking-at c-opt-postfix-decl-spec-key)
8120 (setq injava-inher (cons cont (point))
8121 done t))
8122 ((or (not (c-safe (c-forward-sexp -1) t))
8123 (<= (point) fence))
8124 (setq done t))
8125 )
8126 (setq cont t)))
8127 injava-inher)
8128 (not (c-crosses-statement-barrier-p (cdr injava-inher)
8129 (point)))
8130 ))
8131 (cond
8132
8133 ;; CASE 5C.1: non-hanging colon on an inher intro
8134 ((eq char-after-ip ?:)
8135 (c-beginning-of-statement-1 lim)
8136 (c-add-syntax 'inher-intro (c-point 'boi))
8137 ;; don't add inclass symbol since relative point already
8138 ;; contains any class offset
8139 )
8140
8141 ;; CASE 5C.2: hanging colon on an inher intro
8142 ((eq char-before-ip ?:)
8143 (c-beginning-of-statement-1 lim)
8144 (c-add-syntax 'inher-intro (c-point 'boi))
8145 (if containing-decl-open
8146 (c-add-class-syntax 'inclass
8147 containing-decl-open
8148 containing-decl-start
8149 containing-decl-kwd
8150 paren-state)))
8151
8152 ;; CASE 5C.3: in a Java implements/extends
8153 (injava-inher
8154 (let ((where (cdr injava-inher))
8155 (cont (car injava-inher)))
8156 (goto-char where)
8157 (cond ((looking-at "throws\\>[^_]")
8158 (c-add-syntax 'func-decl-cont
8159 (progn (c-beginning-of-statement-1 lim)
8160 (c-point 'boi))))
8161 (cont (c-add-syntax 'inher-cont where))
8162 (t (c-add-syntax 'inher-intro
8163 (progn (goto-char (cdr injava-inher))
8164 (c-beginning-of-statement-1 lim)
8165 (point))))
8166 )))
8167
8168 ;; CASE 5C.4: a continued inheritance line
8169 (t
8170 (c-beginning-of-inheritance-list lim)
8171 (c-add-syntax 'inher-cont (point))
8172 ;; don't add inclass symbol since relative point already
8173 ;; contains any class offset
8174 )))
8175
8176 ;; CASE 5D: this could be a top-level initialization, a
8177 ;; member init list continuation, or a template argument
8178 ;; list continuation.
8179 ((save-excursion
8180 ;; Note: We use the fact that lim always is after any
8181 ;; preceding brace sexp.
8182 (if c-recognize-<>-arglists
8183 (while (and
8184 (progn
8185 (c-syntactic-skip-backward "^;,=<>" lim t)
8186 (> (point) lim))
8187 (or
8188 (when c-overloadable-operators-regexp
8189 (when (setq placeholder (c-after-special-operator-id lim))
8190 (goto-char placeholder)
8191 t))
8192 (cond
8193 ((eq (char-before) ?>)
8194 (or (c-backward-<>-arglist nil lim)
8195 (backward-char))
8196 t)
8197 ((eq (char-before) ?<)
8198 (backward-char)
8199 (if (save-excursion
8200 (c-forward-<>-arglist nil))
8201 (progn (forward-char)
8202 nil)
8203 t))
8204 (t nil)))))
8205 ;; NB: No c-after-special-operator-id stuff in this
8206 ;; clause - we assume only C++ needs it.
8207 (c-syntactic-skip-backward "^;,=" lim t))
8208 (memq (char-before) '(?, ?= ?<)))
8209 (cond
8210
8211 ;; CASE 5D.3: perhaps a template list continuation?
8212 ((and (c-major-mode-is 'c++-mode)
8213 (save-excursion
8214 (save-restriction
8215 (c-with-syntax-table c++-template-syntax-table
8216 (goto-char indent-point)
8217 (setq placeholder (c-up-list-backward))
8218 (and placeholder
8219 (eq (char-after placeholder) ?<))))))
8220 (c-with-syntax-table c++-template-syntax-table
8221 (goto-char placeholder)
8222 (c-beginning-of-statement-1 lim t)
8223 (if (save-excursion
8224 (c-backward-syntactic-ws lim)
8225 (eq (char-before) ?<))
8226 ;; In a nested template arglist.
8227 (progn
8228 (goto-char placeholder)
8229 (c-syntactic-skip-backward "^,;" lim t)
8230 (c-forward-syntactic-ws))
8231 (back-to-indentation)))
8232 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
8233 ;; template aware.
8234 (c-add-syntax 'template-args-cont (point)))
8235
8236 ;; CASE 5D.4: perhaps a multiple inheritance line?
8237 ((and (c-major-mode-is 'c++-mode)
8238 (save-excursion
8239 (c-beginning-of-statement-1 lim)
8240 (setq placeholder (point))
8241 (if (looking-at "static\\>[^_]")
8242 (c-forward-token-2 1 nil indent-point))
8243 (and (looking-at c-class-key)
8244 (zerop (c-forward-token-2 2 nil indent-point))
8245 (if (eq (char-after) ?<)
8246 (c-with-syntax-table c++-template-syntax-table
8247 (zerop (c-forward-token-2 1 t indent-point)))
8248 t)
8249 (eq (char-after) ?:))))
8250 (goto-char placeholder)
8251 (c-add-syntax 'inher-cont (c-point 'boi)))
8252
8253 ;; CASE 5D.5: Continuation of the "expression part" of a
8254 ;; top level construct.
8255 (t
8256 (while (and (eq (car (c-beginning-of-decl-1 containing-sexp))
8257 'same)
8258 (save-excursion
8259 (c-backward-syntactic-ws)
8260 (eq (char-before) ?}))))
8261 (c-add-stmt-syntax
8262 (if (eq char-before-ip ?,)
8263 ;; A preceding comma at the top level means that a
8264 ;; new variable declaration starts here. Use
8265 ;; topmost-intro-cont for it, for consistency with
8266 ;; the first variable declaration. C.f. case 5N.
8267 'topmost-intro-cont
8268 'statement-cont)
8269 nil nil containing-sexp paren-state))
8270 ))
8271
8272 ;; CASE 5F: Close of a non-class declaration level block.
8273 ((and (eq char-after-ip ?})
8274 (c-keyword-member containing-decl-kwd
8275 'c-other-block-decl-kwds))
8276 ;; This is inconsistent: Should use `containing-decl-open'
8277 ;; here if it's at boi, like in case 5J.
8278 (goto-char containing-decl-start)
8279 (c-add-stmt-syntax
8280 (if (string-equal (symbol-name containing-decl-kwd) "extern")
8281 ;; Special case for compatibility with the
8282 ;; extern-lang syntactic symbols.
8283 'extern-lang-close
8284 (intern (concat (symbol-name containing-decl-kwd)
8285 "-close")))
8286 nil t
8287 (c-most-enclosing-brace paren-state (point))
8288 paren-state))
8289
8290 ;; CASE 5G: we are looking at the brace which closes the
8291 ;; enclosing nested class decl
8292 ((and containing-sexp
8293 (eq char-after-ip ?})
8294 (eq containing-decl-open containing-sexp))
8295 (c-add-class-syntax 'class-close
8296 containing-decl-open
8297 containing-decl-start
8298 containing-decl-kwd
8299 paren-state))
8300
8301 ;; CASE 5H: we could be looking at subsequent knr-argdecls
8302 ((and c-recognize-knr-p
8303 (not containing-sexp) ; can't be knr inside braces.
8304 (not (eq char-before-ip ?}))
8305 (save-excursion
8306 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
8307 (and placeholder
8308 ;; Do an extra check to avoid tripping up on
8309 ;; statements that occur in invalid contexts
8310 ;; (e.g. in macro bodies where we don't really
8311 ;; know the context of what we're looking at).
8312 (not (and c-opt-block-stmt-key
8313 (looking-at c-opt-block-stmt-key)))))
8314 (< placeholder indent-point))
8315 (goto-char placeholder)
8316 (c-add-syntax 'knr-argdecl (point)))
8317
8318 ;; CASE 5I: ObjC method definition.
8319 ((and c-opt-method-key
8320 (looking-at c-opt-method-key))
8321 (c-beginning-of-statement-1 nil t)
8322 (if (= (point) indent-point)
8323 ;; Handle the case when it's the first (non-comment)
8324 ;; thing in the buffer. Can't look for a 'same return
8325 ;; value from cbos1 since ObjC directives currently
8326 ;; aren't recognized fully, so that we get 'same
8327 ;; instead of 'previous if it moved over a preceding
8328 ;; directive.
8329 (goto-char (point-min)))
8330 (c-add-syntax 'objc-method-intro (c-point 'boi)))
8331
8332 ;; CASE 5P: AWK pattern or function or continuation
8333 ;; thereof.
8334 ((c-major-mode-is 'awk-mode)
8335 (setq placeholder (point))
8336 (c-add-stmt-syntax
8337 (if (and (eq (c-beginning-of-statement-1) 'same)
8338 (/= (point) placeholder))
8339 'topmost-intro-cont
8340 'topmost-intro)
8341 nil nil
8342 containing-sexp paren-state))
8343
8344 ;; CASE 5N: At a variable declaration that follows a class
8345 ;; definition or some other block declaration that doesn't
8346 ;; end at the closing '}'. C.f. case 5D.5.
8347 ((progn
8348 (c-backward-syntactic-ws lim)
8349 (and (eq (char-before) ?})
8350 (save-excursion
8351 (let ((start (point)))
8352 (if (and c-state-cache
8353 (consp (car c-state-cache))
8354 (eq (cdar c-state-cache) (point)))
8355 ;; Speed up the backward search a bit.
8356 (goto-char (caar c-state-cache)))
8357 (c-beginning-of-decl-1 containing-sexp)
8358 (setq placeholder (point))
8359 (if (= start (point))
8360 ;; The '}' is unbalanced.
8361 nil
8362 (c-end-of-decl-1)
8363 (>= (point) indent-point))))))
8364 (goto-char placeholder)
8365 (c-add-stmt-syntax 'topmost-intro-cont nil nil
8366 containing-sexp paren-state))
8367
8368 ;; NOTE: The point is at the end of the previous token here.
8369
8370 ;; CASE 5J: we are at the topmost level, make
8371 ;; sure we skip back past any access specifiers
8372 ((and
8373 ;; A macro continuation line is never at top level.
8374 (not (and macro-start
8375 (> indent-point macro-start)))
8376 (save-excursion
8377 (setq placeholder (point))
8378 (or (memq char-before-ip '(?\; ?{ ?} nil))
8379 (c-at-vsemi-p before-ws-ip)
8380 (when (and (eq char-before-ip ?:)
8381 (eq (c-beginning-of-statement-1 lim)
8382 'label))
8383 (c-backward-syntactic-ws lim)
8384 (setq placeholder (point)))
8385 (and (c-major-mode-is 'objc-mode)
8386 (catch 'not-in-directive
8387 (c-beginning-of-statement-1 lim)
8388 (setq placeholder (point))
8389 (while (and (c-forward-objc-directive)
8390 (< (point) indent-point))
8391 (c-forward-syntactic-ws)
8392 (if (>= (point) indent-point)
8393 (throw 'not-in-directive t))
8394 (setq placeholder (point)))
8395 nil)))))
8396 ;; For historic reasons we anchor at bol of the last
8397 ;; line of the previous declaration. That's clearly
8398 ;; highly bogus and useless, and it makes our lives hard
8399 ;; to remain compatible. :P
8400 (goto-char placeholder)
8401 (c-add-syntax 'topmost-intro (c-point 'bol))
8402 (if containing-decl-open
8403 (if (c-keyword-member containing-decl-kwd
8404 'c-other-block-decl-kwds)
8405 (progn
8406 (goto-char (c-brace-anchor-point containing-decl-open))
8407 (c-add-stmt-syntax
8408 (if (string-equal (symbol-name containing-decl-kwd)
8409 "extern")
8410 ;; Special case for compatibility with the
8411 ;; extern-lang syntactic symbols.
8412 'inextern-lang
8413 (intern (concat "in"
8414 (symbol-name containing-decl-kwd))))
8415 nil t
8416 (c-most-enclosing-brace paren-state (point))
8417 paren-state))
8418 (c-add-class-syntax 'inclass
8419 containing-decl-open
8420 containing-decl-start
8421 containing-decl-kwd
8422 paren-state)))
8423 (when (and c-syntactic-indentation-in-macros
8424 macro-start
8425 (/= macro-start (c-point 'boi indent-point)))
8426 (c-add-syntax 'cpp-define-intro)
8427 (setq macro-start nil)))
8428
8429 ;; CASE 5K: we are at an ObjC method definition
8430 ;; continuation line.
8431 ((and c-opt-method-key
8432 (save-excursion
8433 (c-beginning-of-statement-1 lim)
8434 (beginning-of-line)
8435 (when (looking-at c-opt-method-key)
8436 (setq placeholder (point)))))
8437 (c-add-syntax 'objc-method-args-cont placeholder))
8438
8439 ;; CASE 5L: we are at the first argument of a template
8440 ;; arglist that begins on the previous line.
8441 ((and c-recognize-<>-arglists
8442 (eq (char-before) ?<)
8443 (not (and c-overloadable-operators-regexp
8444 (c-after-special-operator-id lim))))
8445 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8446 (c-add-syntax 'template-args-cont (c-point 'boi)))
8447
8448 ;; CASE 5Q: we are at a statement within a macro.
8449 (macro-start
8450 (c-beginning-of-statement-1 containing-sexp)
8451 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
8452
8453 ;; CASE 5M: we are at a topmost continuation line
8454 (t
8455 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
8456 (when (c-major-mode-is 'objc-mode)
8457 (setq placeholder (point))
8458 (while (and (c-forward-objc-directive)
8459 (< (point) indent-point))
8460 (c-forward-syntactic-ws)
8461 (setq placeholder (point)))
8462 (goto-char placeholder))
8463 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
8464 ))
8465
8466 ;; (CASE 6 has been removed.)
8467
8468 ;; CASE 7: line is an expression, not a statement. Most
8469 ;; likely we are either in a function prototype or a function
8470 ;; call argument list
8471 ((not (or (and c-special-brace-lists
8472 (save-excursion
8473 (goto-char containing-sexp)
8474 (c-looking-at-special-brace-list)))
8475 (eq (char-after containing-sexp) ?{)))
8476 (cond
8477
8478 ;; CASE 7A: we are looking at the arglist closing paren.
8479 ;; C.f. case 7F.
8480 ((memq char-after-ip '(?\) ?\]))
8481 (goto-char containing-sexp)
8482 (setq placeholder (c-point 'boi))
8483 (if (and (c-safe (backward-up-list 1) t)
8484 (>= (point) placeholder))
8485 (progn
8486 (forward-char)
8487 (skip-chars-forward " \t"))
8488 (goto-char placeholder))
8489 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
8490 (c-most-enclosing-brace paren-state (point))
8491 paren-state))
8492
8493 ;; CASE 7B: Looking at the opening brace of an
8494 ;; in-expression block or brace list. C.f. cases 4, 16A
8495 ;; and 17E.
8496 ((and (eq char-after-ip ?{)
8497 (progn
8498 (setq placeholder (c-inside-bracelist-p (point)
8499 paren-state))
8500 (if placeholder
8501 (setq tmpsymbol '(brace-list-open . inexpr-class))
8502 (setq tmpsymbol '(block-open . inexpr-statement)
8503 placeholder
8504 (cdr-safe (c-looking-at-inexpr-block
8505 (c-safe-position containing-sexp
8506 paren-state)
8507 containing-sexp)))
8508 ;; placeholder is nil if it's a block directly in
8509 ;; a function arglist. That makes us skip out of
8510 ;; this case.
8511 )))
8512 (goto-char placeholder)
8513 (back-to-indentation)
8514 (c-add-stmt-syntax (car tmpsymbol) nil t
8515 (c-most-enclosing-brace paren-state (point))
8516 paren-state)
8517 (if (/= (point) placeholder)
8518 (c-add-syntax (cdr tmpsymbol))))
8519
8520 ;; CASE 7C: we are looking at the first argument in an empty
8521 ;; argument list. Use arglist-close if we're actually
8522 ;; looking at a close paren or bracket.
8523 ((memq char-before-ip '(?\( ?\[))
8524 (goto-char containing-sexp)
8525 (setq placeholder (c-point 'boi))
8526 (if (and (c-safe (backward-up-list 1) t)
8527 (>= (point) placeholder))
8528 (progn
8529 (forward-char)
8530 (skip-chars-forward " \t"))
8531 (goto-char placeholder))
8532 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
8533 (c-most-enclosing-brace paren-state (point))
8534 paren-state))
8535
8536 ;; CASE 7D: we are inside a conditional test clause. treat
8537 ;; these things as statements
8538 ((progn
8539 (goto-char containing-sexp)
8540 (and (c-safe (c-forward-sexp -1) t)
8541 (looking-at "\\<for\\>[^_]")))
8542 (goto-char (1+ containing-sexp))
8543 (c-forward-syntactic-ws indent-point)
8544 (if (eq char-before-ip ?\;)
8545 (c-add-syntax 'statement (point))
8546 (c-add-syntax 'statement-cont (point))
8547 ))
8548
8549 ;; CASE 7E: maybe a continued ObjC method call. This is the
8550 ;; case when we are inside a [] bracketed exp, and what
8551 ;; precede the opening bracket is not an identifier.
8552 ((and c-opt-method-key
8553 (eq (char-after containing-sexp) ?\[)
8554 (progn
8555 (goto-char (1- containing-sexp))
8556 (c-backward-syntactic-ws (c-point 'bod))
8557 (if (not (looking-at c-symbol-key))
8558 (c-add-syntax 'objc-method-call-cont containing-sexp))
8559 )))
8560
8561 ;; CASE 7F: we are looking at an arglist continuation line,
8562 ;; but the preceding argument is on the same line as the
8563 ;; opening paren. This case includes multi-line
8564 ;; mathematical paren groupings, but we could be on a
8565 ;; for-list continuation line. C.f. case 7A.
8566 ((progn
8567 (goto-char (1+ containing-sexp))
8568 (< (save-excursion
8569 (c-forward-syntactic-ws)
8570 (point))
8571 (c-point 'bonl)))
8572 (goto-char containing-sexp)
8573 (setq placeholder (c-point 'boi))
8574 (if (and (c-safe (backward-up-list 1) t)
8575 (>= (point) placeholder))
8576 (progn
8577 (forward-char)
8578 (skip-chars-forward " \t"))
8579 (goto-char placeholder))
8580 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
8581 (c-most-enclosing-brace c-state-cache (point))
8582 paren-state))
8583
8584 ;; CASE 7G: we are looking at just a normal arglist
8585 ;; continuation line
8586 (t (c-forward-syntactic-ws indent-point)
8587 (c-add-syntax 'arglist-cont (c-point 'boi)))
8588 ))
8589
8590 ;; CASE 8: func-local multi-inheritance line
8591 ((and (c-major-mode-is 'c++-mode)
8592 (save-excursion
8593 (goto-char indent-point)
8594 (skip-chars-forward " \t")
8595 (looking-at c-opt-postfix-decl-spec-key)))
8596 (goto-char indent-point)
8597 (skip-chars-forward " \t")
8598 (cond
8599
8600 ;; CASE 8A: non-hanging colon on an inher intro
8601 ((eq char-after-ip ?:)
8602 (c-backward-syntactic-ws lim)
8603 (c-add-syntax 'inher-intro (c-point 'boi)))
8604
8605 ;; CASE 8B: hanging colon on an inher intro
8606 ((eq char-before-ip ?:)
8607 (c-add-syntax 'inher-intro (c-point 'boi)))
8608
8609 ;; CASE 8C: a continued inheritance line
8610 (t
8611 (c-beginning-of-inheritance-list lim)
8612 (c-add-syntax 'inher-cont (point))
8613 )))
8614
8615 ;; CASE 9: we are inside a brace-list
8616 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
8617 (setq special-brace-list
8618 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
8619 (save-excursion
8620 (goto-char containing-sexp)
8621 (c-looking-at-special-brace-list)))
8622 (c-inside-bracelist-p containing-sexp paren-state))))
8623 (cond
8624
8625 ;; CASE 9A: In the middle of a special brace list opener.
8626 ((and (consp special-brace-list)
8627 (save-excursion
8628 (goto-char containing-sexp)
8629 (eq (char-after) ?\())
8630 (eq char-after-ip (car (cdr special-brace-list))))
8631 (goto-char (car (car special-brace-list)))
8632 (skip-chars-backward " \t")
8633 (if (and (bolp)
8634 (assoc 'statement-cont
8635 (setq placeholder (c-guess-basic-syntax))))
8636 (setq c-syntactic-context placeholder)
8637 (c-beginning-of-statement-1
8638 (c-safe-position (1- containing-sexp) paren-state))
8639 (c-forward-token-2 0)
8640 (while (looking-at c-specifier-key)
8641 (goto-char (match-end 1))
8642 (c-forward-syntactic-ws))
8643 (c-add-syntax 'brace-list-open (c-point 'boi))))
8644
8645 ;; CASE 9B: brace-list-close brace
8646 ((if (consp special-brace-list)
8647 ;; Check special brace list closer.
8648 (progn
8649 (goto-char (car (car special-brace-list)))
8650 (save-excursion
8651 (goto-char indent-point)
8652 (back-to-indentation)
8653 (or
8654 ;; We were between the special close char and the `)'.
8655 (and (eq (char-after) ?\))
8656 (eq (1+ (point)) (cdr (car special-brace-list))))
8657 ;; We were before the special close char.
8658 (and (eq (char-after) (cdr (cdr special-brace-list)))
8659 (zerop (c-forward-token-2))
8660 (eq (1+ (point)) (cdr (car special-brace-list)))))))
8661 ;; Normal brace list check.
8662 (and (eq char-after-ip ?})
8663 (c-safe (goto-char (c-up-list-backward (point))) t)
8664 (= (point) containing-sexp)))
8665 (if (eq (point) (c-point 'boi))
8666 (c-add-syntax 'brace-list-close (point))
8667 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8668 (c-beginning-of-statement-1 lim)
8669 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
8670
8671 (t
8672 ;; Prepare for the rest of the cases below by going to the
8673 ;; token following the opening brace
8674 (if (consp special-brace-list)
8675 (progn
8676 (goto-char (car (car special-brace-list)))
8677 (c-forward-token-2 1 nil indent-point))
8678 (goto-char containing-sexp))
8679 (forward-char)
8680 (let ((start (point)))
8681 (c-forward-syntactic-ws indent-point)
8682 (goto-char (max start (c-point 'bol))))
8683 (c-skip-ws-forward indent-point)
8684 (cond
8685
8686 ;; CASE 9C: we're looking at the first line in a brace-list
8687 ((= (point) indent-point)
8688 (if (consp special-brace-list)
8689 (goto-char (car (car special-brace-list)))
8690 (goto-char containing-sexp))
8691 (if (eq (point) (c-point 'boi))
8692 (c-add-syntax 'brace-list-intro (point))
8693 (setq lim (c-most-enclosing-brace c-state-cache (point)))
8694 (c-beginning-of-statement-1 lim)
8695 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
8696
8697 ;; CASE 9D: this is just a later brace-list-entry or
8698 ;; brace-entry-open
8699 (t (if (or (eq char-after-ip ?{)
8700 (and c-special-brace-lists
8701 (save-excursion
8702 (goto-char indent-point)
8703 (c-forward-syntactic-ws (c-point 'eol))
8704 (c-looking-at-special-brace-list (point)))))
8705 (c-add-syntax 'brace-entry-open (point))
8706 (c-add-syntax 'brace-list-entry (point))
8707 ))
8708 ))))
8709
8710 ;; CASE 10: A continued statement or top level construct.
8711 ((and (not (memq char-before-ip '(?\; ?:)))
8712 (not (c-at-vsemi-p before-ws-ip))
8713 (or (not (eq char-before-ip ?}))
8714 (c-looking-at-inexpr-block-backward c-state-cache))
8715 (> (point)
8716 (save-excursion
8717 (c-beginning-of-statement-1 containing-sexp)
8718 (setq placeholder (point))))
8719 (/= placeholder containing-sexp))
8720 ;; This is shared with case 18.
8721 (c-guess-continued-construct indent-point
8722 char-after-ip
8723 placeholder
8724 containing-sexp
8725 paren-state))
8726
8727 ;; CASE 16: block close brace, possibly closing the defun or
8728 ;; the class
8729 ((eq char-after-ip ?})
8730 ;; From here on we have the next containing sexp in lim.
8731 (setq lim (c-most-enclosing-brace paren-state))
8732 (goto-char containing-sexp)
8733 (cond
8734
8735 ;; CASE 16E: Closing a statement block? This catches
8736 ;; cases where it's preceded by a statement keyword,
8737 ;; which works even when used in an "invalid" context,
8738 ;; e.g. a macro argument.
8739 ((c-after-conditional)
8740 (c-backward-to-block-anchor lim)
8741 (c-add-stmt-syntax 'block-close nil t lim paren-state))
8742
8743 ;; CASE 16A: closing a lambda defun or an in-expression
8744 ;; block? C.f. cases 4, 7B and 17E.
8745 ((setq placeholder (c-looking-at-inexpr-block
8746 (c-safe-position containing-sexp paren-state)
8747 nil))
8748 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8749 'inline-close
8750 'block-close))
8751 (goto-char containing-sexp)
8752 (back-to-indentation)
8753 (if (= containing-sexp (point))
8754 (c-add-syntax tmpsymbol (point))
8755 (goto-char (cdr placeholder))
8756 (back-to-indentation)
8757 (c-add-stmt-syntax tmpsymbol nil t
8758 (c-most-enclosing-brace paren-state (point))
8759 paren-state)
8760 (if (/= (point) (cdr placeholder))
8761 (c-add-syntax (car placeholder)))))
8762
8763 ;; CASE 16B: does this close an inline or a function in
8764 ;; a non-class declaration level block?
8765 ((save-excursion
8766 (and lim
8767 (progn
8768 (goto-char lim)
8769 (c-looking-at-decl-block
8770 (c-most-enclosing-brace paren-state lim)
8771 nil))
8772 (setq placeholder (point))))
8773 (c-backward-to-decl-anchor lim)
8774 (back-to-indentation)
8775 (if (save-excursion
8776 (goto-char placeholder)
8777 (looking-at c-other-decl-block-key))
8778 (c-add-syntax 'defun-close (point))
8779 (c-add-syntax 'inline-close (point))))
8780
8781 ;; CASE 16F: Can be a defun-close of a function declared
8782 ;; in a statement block, e.g. in Pike or when using gcc
8783 ;; extensions, but watch out for macros followed by
8784 ;; blocks. Let it through to be handled below.
8785 ;; C.f. cases B.3 and 17G.
8786 ((save-excursion
8787 (and (not (c-at-statement-start-p))
8788 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8789 (setq placeholder (point))
8790 (let ((c-recognize-typeless-decls nil))
8791 ;; Turn off recognition of constructs that
8792 ;; lacks a type in this case, since that's more
8793 ;; likely to be a macro followed by a block.
8794 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8795 (back-to-indentation)
8796 (if (/= (point) containing-sexp)
8797 (goto-char placeholder))
8798 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
8799
8800 ;; CASE 16C: If there is an enclosing brace then this is
8801 ;; a block close since defun closes inside declaration
8802 ;; level blocks have been handled above.
8803 (lim
8804 ;; If the block is preceded by a case/switch label on
8805 ;; the same line, we anchor at the first preceding label
8806 ;; at boi. The default handling in c-add-stmt-syntax
8807 ;; really fixes it better, but we do like this to keep
8808 ;; the indentation compatible with version 5.28 and
8809 ;; earlier. C.f. case 17H.
8810 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8811 (eq (c-beginning-of-statement-1 lim) 'label)))
8812 (goto-char placeholder)
8813 (if (looking-at c-label-kwds-regexp)
8814 (c-add-syntax 'block-close (point))
8815 (goto-char containing-sexp)
8816 ;; c-backward-to-block-anchor not necessary here; those
8817 ;; situations are handled in case 16E above.
8818 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
8819
8820 ;; CASE 16D: Only top level defun close left.
8821 (t
8822 (goto-char containing-sexp)
8823 (c-backward-to-decl-anchor lim)
8824 (c-add-stmt-syntax 'defun-close nil nil
8825 (c-most-enclosing-brace paren-state)
8826 paren-state))
8827 ))
8828
8829 ;; CASE 17: Statement or defun catchall.
8830 (t
8831 (goto-char indent-point)
8832 ;; Back up statements until we find one that starts at boi.
8833 (while (let* ((prev-point (point))
8834 (last-step-type (c-beginning-of-statement-1
8835 containing-sexp)))
8836 (if (= (point) prev-point)
8837 (progn
8838 (setq step-type (or step-type last-step-type))
8839 nil)
8840 (setq step-type last-step-type)
8841 (/= (point) (c-point 'boi)))))
8842 (cond
8843
8844 ;; CASE 17B: continued statement
8845 ((and (eq step-type 'same)
8846 (/= (point) indent-point))
8847 (c-add-stmt-syntax 'statement-cont nil nil
8848 containing-sexp paren-state))
8849
8850 ;; CASE 17A: After a case/default label?
8851 ((progn
8852 (while (and (eq step-type 'label)
8853 (not (looking-at c-label-kwds-regexp)))
8854 (setq step-type
8855 (c-beginning-of-statement-1 containing-sexp)))
8856 (eq step-type 'label))
8857 (c-add-stmt-syntax (if (eq char-after-ip ?{)
8858 'statement-case-open
8859 'statement-case-intro)
8860 nil t containing-sexp paren-state))
8861
8862 ;; CASE 17D: any old statement
8863 ((progn
8864 (while (eq step-type 'label)
8865 (setq step-type
8866 (c-beginning-of-statement-1 containing-sexp)))
8867 (eq step-type 'previous))
8868 (c-add-stmt-syntax 'statement nil t
8869 containing-sexp paren-state)
8870 (if (eq char-after-ip ?{)
8871 (c-add-syntax 'block-open)))
8872
8873 ;; CASE 17I: Inside a substatement block.
8874 ((progn
8875 ;; The following tests are all based on containing-sexp.
8876 (goto-char containing-sexp)
8877 ;; From here on we have the next containing sexp in lim.
8878 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
8879 (c-after-conditional))
8880 (c-backward-to-block-anchor lim)
8881 (c-add-stmt-syntax 'statement-block-intro nil t
8882 lim paren-state)
8883 (if (eq char-after-ip ?{)
8884 (c-add-syntax 'block-open)))
8885
8886 ;; CASE 17E: first statement in an in-expression block.
8887 ;; C.f. cases 4, 7B and 16A.
8888 ((setq placeholder (c-looking-at-inexpr-block
8889 (c-safe-position containing-sexp paren-state)
8890 nil))
8891 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
8892 'defun-block-intro
8893 'statement-block-intro))
8894 (back-to-indentation)
8895 (if (= containing-sexp (point))
8896 (c-add-syntax tmpsymbol (point))
8897 (goto-char (cdr placeholder))
8898 (back-to-indentation)
8899 (c-add-stmt-syntax tmpsymbol nil t
8900 (c-most-enclosing-brace c-state-cache (point))
8901 paren-state)
8902 (if (/= (point) (cdr placeholder))
8903 (c-add-syntax (car placeholder))))
8904 (if (eq char-after-ip ?{)
8905 (c-add-syntax 'block-open)))
8906
8907 ;; CASE 17F: first statement in an inline, or first
8908 ;; statement in a top-level defun. we can tell this is it
8909 ;; if there are no enclosing braces that haven't been
8910 ;; narrowed out by a class (i.e. don't use bod here).
8911 ((save-excursion
8912 (or (not (setq placeholder (c-most-enclosing-brace
8913 paren-state)))
8914 (and (progn
8915 (goto-char placeholder)
8916 (eq (char-after) ?{))
8917 (c-looking-at-decl-block (c-most-enclosing-brace
8918 paren-state (point))
8919 nil))))
8920 (c-backward-to-decl-anchor lim)
8921 (back-to-indentation)
8922 (c-add-syntax 'defun-block-intro (point)))
8923
8924 ;; CASE 17G: First statement in a function declared inside
8925 ;; a normal block. This can occur in Pike and with
8926 ;; e.g. the gcc extensions, but watch out for macros
8927 ;; followed by blocks. C.f. cases B.3 and 16F.
8928 ((save-excursion
8929 (and (not (c-at-statement-start-p))
8930 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
8931 (setq placeholder (point))
8932 (let ((c-recognize-typeless-decls nil))
8933 ;; Turn off recognition of constructs that lacks
8934 ;; a type in this case, since that's more likely
8935 ;; to be a macro followed by a block.
8936 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8937 (back-to-indentation)
8938 (if (/= (point) containing-sexp)
8939 (goto-char placeholder))
8940 (c-add-stmt-syntax 'defun-block-intro nil t
8941 lim paren-state))
8942
8943 ;; CASE 17H: First statement in a block.
8944 (t
8945 ;; If the block is preceded by a case/switch label on the
8946 ;; same line, we anchor at the first preceding label at
8947 ;; boi. The default handling in c-add-stmt-syntax is
8948 ;; really fixes it better, but we do like this to keep the
8949 ;; indentation compatible with version 5.28 and earlier.
8950 ;; C.f. case 16C.
8951 (while (and (/= (setq placeholder (point)) (c-point 'boi))
8952 (eq (c-beginning-of-statement-1 lim) 'label)))
8953 (goto-char placeholder)
8954 (if (looking-at c-label-kwds-regexp)
8955 (c-add-syntax 'statement-block-intro (point))
8956 (goto-char containing-sexp)
8957 ;; c-backward-to-block-anchor not necessary here; those
8958 ;; situations are handled in case 17I above.
8959 (c-add-stmt-syntax 'statement-block-intro nil t
8960 lim paren-state))
8961 (if (eq char-after-ip ?{)
8962 (c-add-syntax 'block-open)))
8963 ))
8964 )
8965
8966 ;; now we need to look at any modifiers
8967 (goto-char indent-point)
8968 (skip-chars-forward " \t")
8969
8970 ;; are we looking at a comment only line?
8971 (when (and (looking-at c-comment-start-regexp)
8972 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
8973 (c-append-syntax 'comment-intro))
8974
8975 ;; we might want to give additional offset to friends (in C++).
8976 (when (and c-opt-friend-key
8977 (looking-at c-opt-friend-key))
8978 (c-append-syntax 'friend))
8979
8980 ;; Set syntactic-relpos.
8981 (let ((p c-syntactic-context))
8982 (while (and p
8983 (if (integerp (c-langelem-pos (car p)))
8984 (progn
8985 (setq syntactic-relpos (c-langelem-pos (car p)))
8986 nil)
8987 t))
8988 (setq p (cdr p))))
8989
8990 ;; Start of or a continuation of a preprocessor directive?
8991 (if (and macro-start
8992 (eq macro-start (c-point 'boi))
8993 (not (and (c-major-mode-is 'pike-mode)
8994 (eq (char-after (1+ macro-start)) ?\"))))
8995 (c-append-syntax 'cpp-macro)
8996 (when (and c-syntactic-indentation-in-macros macro-start)
8997 (if in-macro-expr
8998 (when (or
8999 (< syntactic-relpos macro-start)
9000 (not (or
9001 (assq 'arglist-intro c-syntactic-context)
9002 (assq 'arglist-cont c-syntactic-context)
9003 (assq 'arglist-cont-nonempty c-syntactic-context)
9004 (assq 'arglist-close c-syntactic-context))))
9005 ;; If inside a cpp expression, i.e. anywhere in a
9006 ;; cpp directive except a #define body, we only let
9007 ;; through the syntactic analysis that is internal
9008 ;; in the expression. That means the arglist
9009 ;; elements, if they are anchored inside the cpp
9010 ;; expression.
9011 (setq c-syntactic-context nil)
9012 (c-add-syntax 'cpp-macro-cont macro-start))
9013 (when (and (eq macro-start syntactic-relpos)
9014 (not (assq 'cpp-define-intro c-syntactic-context))
9015 (save-excursion
9016 (goto-char macro-start)
9017 (or (not (c-forward-to-cpp-define-body))
9018 (<= (point) (c-point 'boi indent-point)))))
9019 ;; Inside a #define body and the syntactic analysis is
9020 ;; anchored on the start of the #define. In this case
9021 ;; we add cpp-define-intro to get the extra
9022 ;; indentation of the #define body.
9023 (c-add-syntax 'cpp-define-intro)))))
9024
9025 ;; return the syntax
9026 c-syntactic-context)))
9027
9028 \f
9029 ;; Indentation calculation.
9030
9031 (defun c-evaluate-offset (offset langelem symbol)
9032 ;; offset can be a number, a function, a variable, a list, or one of
9033 ;; the symbols + or -
9034 ;;
9035 ;; This function might do hidden buffer changes.
9036 (let ((res
9037 (cond
9038 ((numberp offset) offset)
9039 ((vectorp offset) offset)
9040 ((null offset) nil)
9041
9042 ((eq offset '+) c-basic-offset)
9043 ((eq offset '-) (- c-basic-offset))
9044 ((eq offset '++) (* 2 c-basic-offset))
9045 ((eq offset '--) (* 2 (- c-basic-offset)))
9046 ((eq offset '*) (/ c-basic-offset 2))
9047 ((eq offset '/) (/ (- c-basic-offset) 2))
9048
9049 ((functionp offset)
9050 (c-evaluate-offset
9051 (funcall offset
9052 (cons (c-langelem-sym langelem)
9053 (c-langelem-pos langelem)))
9054 langelem symbol))
9055
9056 ((listp offset)
9057 (cond
9058 ((eq (car offset) 'quote)
9059 (c-benign-error "The offset %S for %s was mistakenly quoted"
9060 offset symbol)
9061 nil)
9062
9063 ((memq (car offset) '(min max))
9064 (let (res val (method (car offset)))
9065 (setq offset (cdr offset))
9066 (while offset
9067 (setq val (c-evaluate-offset (car offset) langelem symbol))
9068 (cond
9069 ((not val))
9070 ((not res)
9071 (setq res val))
9072 ((integerp val)
9073 (if (vectorp res)
9074 (c-benign-error "\
9075 Error evaluating offset %S for %s: \
9076 Cannot combine absolute offset %S with relative %S in `%s' method"
9077 (car offset) symbol res val method)
9078 (setq res (funcall method res val))))
9079 (t
9080 (if (integerp res)
9081 (c-benign-error "\
9082 Error evaluating offset %S for %s: \
9083 Cannot combine relative offset %S with absolute %S in `%s' method"
9084 (car offset) symbol res val method)
9085 (setq res (vector (funcall method (aref res 0)
9086 (aref val 0)))))))
9087 (setq offset (cdr offset)))
9088 res))
9089
9090 ((eq (car offset) 'add)
9091 (let (res val)
9092 (setq offset (cdr offset))
9093 (while offset
9094 (setq val (c-evaluate-offset (car offset) langelem symbol))
9095 (cond
9096 ((not val))
9097 ((not res)
9098 (setq res val))
9099 ((integerp val)
9100 (if (vectorp res)
9101 (setq res (vector (+ (aref res 0) val)))
9102 (setq res (+ res val))))
9103 (t
9104 (if (vectorp res)
9105 (c-benign-error "\
9106 Error evaluating offset %S for %s: \
9107 Cannot combine absolute offsets %S and %S in `add' method"
9108 (car offset) symbol res val)
9109 (setq res val)))) ; Override.
9110 (setq offset (cdr offset)))
9111 res))
9112
9113 (t
9114 (let (res)
9115 (when (eq (car offset) 'first)
9116 (setq offset (cdr offset)))
9117 (while (and (not res) offset)
9118 (setq res (c-evaluate-offset (car offset) langelem symbol)
9119 offset (cdr offset)))
9120 res))))
9121
9122 ((and (symbolp offset) (boundp offset))
9123 (symbol-value offset))
9124
9125 (t
9126 (c-benign-error "Unknown offset format %S for %s" offset symbol)
9127 nil))))
9128
9129 (if (or (null res) (integerp res)
9130 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
9131 res
9132 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
9133 offset symbol res)
9134 nil)))
9135
9136 (defun c-calc-offset (langelem)
9137 ;; Get offset from LANGELEM which is a list beginning with the
9138 ;; syntactic symbol and followed by any analysis data it provides.
9139 ;; That data may be zero or more elements, but if at least one is
9140 ;; given then the first is the anchor position (or nil). The symbol
9141 ;; is matched against `c-offsets-alist' and the offset calculated
9142 ;; from that is returned.
9143 ;;
9144 ;; This function might do hidden buffer changes.
9145 (let* ((symbol (c-langelem-sym langelem))
9146 (match (assq symbol c-offsets-alist))
9147 (offset (cdr-safe match)))
9148 (if match
9149 (setq offset (c-evaluate-offset offset langelem symbol))
9150 (if c-strict-syntax-p
9151 (c-benign-error "No offset found for syntactic symbol %s" symbol))
9152 (setq offset 0))
9153 (if (vectorp offset)
9154 offset
9155 (or (and (numberp offset) offset)
9156 (and (symbolp offset) (symbol-value offset))
9157 0))
9158 ))
9159
9160 (defun c-get-offset (langelem)
9161 ;; This is a compatibility wrapper for `c-calc-offset' in case
9162 ;; someone is calling it directly. It takes an old style syntactic
9163 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
9164 ;; new list form.
9165 ;;
9166 ;; This function might do hidden buffer changes.
9167 (if (c-langelem-pos langelem)
9168 (c-calc-offset (list (c-langelem-sym langelem)
9169 (c-langelem-pos langelem)))
9170 (c-calc-offset langelem)))
9171
9172 (defun c-get-syntactic-indentation (langelems)
9173 ;; Calculate the syntactic indentation from a syntactic description
9174 ;; as returned by `c-guess-syntax'.
9175 ;;
9176 ;; Note that topmost-intro always has an anchor position at bol, for
9177 ;; historical reasons. It's often used together with other symbols
9178 ;; that has more sane positions. Since we always use the first
9179 ;; found anchor position, we rely on that these other symbols always
9180 ;; precede topmost-intro in the LANGELEMS list.
9181 ;;
9182 ;; This function might do hidden buffer changes.
9183 (let ((indent 0) anchor)
9184
9185 (while langelems
9186 (let* ((c-syntactic-element (car langelems))
9187 (res (c-calc-offset c-syntactic-element)))
9188
9189 (if (vectorp res)
9190 ;; Got an absolute column that overrides any indentation
9191 ;; we've collected so far, but not the relative
9192 ;; indentation we might get for the nested structures
9193 ;; further down the langelems list.
9194 (setq indent (elt res 0)
9195 anchor (point-min)) ; A position at column 0.
9196
9197 ;; Got a relative change of the current calculated
9198 ;; indentation.
9199 (setq indent (+ indent res))
9200
9201 ;; Use the anchor position from the first syntactic
9202 ;; element with one.
9203 (unless anchor
9204 (setq anchor (c-langelem-pos (car langelems)))))
9205
9206 (setq langelems (cdr langelems))))
9207
9208 (if anchor
9209 (+ indent (save-excursion
9210 (goto-char anchor)
9211 (current-column)))
9212 indent)))
9213
9214 \f
9215 (cc-provide 'cc-engine)
9216
9217 ;;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
9218 ;;; cc-engine.el ends here